xref: /openbmc/linux/drivers/infiniband/hw/mlx5/main.c (revision 72c7fe90ee7a16a8e350183712b2a639b4054ace)
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>
53*72c7fe90SPravin 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 |
68331f69a82SYishai Hadas 						MLX5_RX_HASH_DST_PORT_UDP;
68431f69a82SYishai Hadas 			resp.response_length += sizeof(resp.rss_caps);
68531f69a82SYishai Hadas 		}
68631f69a82SYishai Hadas 	} else {
68731f69a82SYishai Hadas 		if (field_avail(typeof(resp), tso_caps, uhw->outlen))
68831f69a82SYishai Hadas 			resp.response_length += sizeof(resp.tso_caps);
68931f69a82SYishai Hadas 		if (field_avail(typeof(resp), rss_caps, uhw->outlen))
69031f69a82SYishai Hadas 			resp.response_length += sizeof(resp.rss_caps);
691402ca536SBodong Wang 	}
692402ca536SBodong Wang 
693f0313965SErez Shitrit 	if (MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) {
694f0313965SErez Shitrit 		props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
695f0313965SErez Shitrit 		props->device_cap_flags |= IB_DEVICE_UD_TSO;
696f0313965SErez Shitrit 	}
697f0313965SErez Shitrit 
69803404e8aSMaor Gottlieb 	if (MLX5_CAP_GEN(dev->mdev, rq_delay_drop) &&
69903404e8aSMaor Gottlieb 	    MLX5_CAP_GEN(dev->mdev, general_notification_event))
70003404e8aSMaor Gottlieb 		props->raw_packet_caps |= IB_RAW_PACKET_CAP_DELAY_DROP;
70103404e8aSMaor Gottlieb 
7021d54f890SYishai Hadas 	if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads) &&
7031d54f890SYishai Hadas 	    MLX5_CAP_IPOIB_ENHANCED(mdev, csum_cap))
7041d54f890SYishai Hadas 		props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
7051d54f890SYishai Hadas 
706cff5a0f3SMajd Dibbiny 	if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
707e8161334SNoa Osherovich 	    MLX5_CAP_ETH(dev->mdev, scatter_fcs)) {
708e8161334SNoa Osherovich 		/* Legacy bit to support old userspace libraries */
709cff5a0f3SMajd Dibbiny 		props->device_cap_flags |= IB_DEVICE_RAW_SCATTER_FCS;
710e8161334SNoa Osherovich 		props->raw_packet_caps |= IB_RAW_PACKET_CAP_SCATTER_FCS;
711e8161334SNoa Osherovich 	}
712cff5a0f3SMajd Dibbiny 
713da6d6ba3SMaor Gottlieb 	if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS))
714da6d6ba3SMaor Gottlieb 		props->device_cap_flags |= IB_DEVICE_MANAGED_FLOW_STEERING;
715da6d6ba3SMaor Gottlieb 
716b1383aa6SNoa Osherovich 	if (MLX5_CAP_GEN(mdev, end_pad))
717b1383aa6SNoa Osherovich 		props->device_cap_flags |= IB_DEVICE_PCI_WRITE_END_PADDING;
718b1383aa6SNoa Osherovich 
7191b5daf11SMajd Dibbiny 	props->vendor_part_id	   = mdev->pdev->device;
7201b5daf11SMajd Dibbiny 	props->hw_ver		   = mdev->pdev->revision;
721e126ba97SEli Cohen 
722e126ba97SEli Cohen 	props->max_mr_size	   = ~0ull;
723e0238a6aSSagi Grimberg 	props->page_size_cap	   = ~(min_page_size - 1);
724938fe83cSSaeed Mahameed 	props->max_qp		   = 1 << MLX5_CAP_GEN(mdev, log_max_qp);
725938fe83cSSaeed Mahameed 	props->max_qp_wr	   = 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
726938fe83cSSaeed Mahameed 	max_rq_sg =  MLX5_CAP_GEN(mdev, max_wqe_sz_rq) /
727938fe83cSSaeed Mahameed 		     sizeof(struct mlx5_wqe_data_seg);
728288c01b7SEli Cohen 	max_sq_desc = min_t(int, MLX5_CAP_GEN(mdev, max_wqe_sz_sq), 512);
729288c01b7SEli Cohen 	max_sq_sg = (max_sq_desc - sizeof(struct mlx5_wqe_ctrl_seg) -
730288c01b7SEli Cohen 		     sizeof(struct mlx5_wqe_raddr_seg)) /
731e126ba97SEli Cohen 		sizeof(struct mlx5_wqe_data_seg);
732e126ba97SEli Cohen 	props->max_sge = min(max_rq_sg, max_sq_sg);
733986ef95eSSagi Grimberg 	props->max_sge_rd	   = MLX5_MAX_SGE_RD;
734938fe83cSSaeed Mahameed 	props->max_cq		   = 1 << MLX5_CAP_GEN(mdev, log_max_cq);
7359f177686SLeon Romanovsky 	props->max_cqe = (1 << MLX5_CAP_GEN(mdev, log_max_cq_sz)) - 1;
736938fe83cSSaeed Mahameed 	props->max_mr		   = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
737938fe83cSSaeed Mahameed 	props->max_pd		   = 1 << MLX5_CAP_GEN(mdev, log_max_pd);
738938fe83cSSaeed Mahameed 	props->max_qp_rd_atom	   = 1 << MLX5_CAP_GEN(mdev, log_max_ra_req_qp);
739938fe83cSSaeed Mahameed 	props->max_qp_init_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_res_qp);
740938fe83cSSaeed Mahameed 	props->max_srq		   = 1 << MLX5_CAP_GEN(mdev, log_max_srq);
741938fe83cSSaeed Mahameed 	props->max_srq_wr = (1 << MLX5_CAP_GEN(mdev, log_max_srq_sz)) - 1;
742938fe83cSSaeed Mahameed 	props->local_ca_ack_delay  = MLX5_CAP_GEN(mdev, local_ca_ack_delay);
743e126ba97SEli Cohen 	props->max_res_rd_atom	   = props->max_qp_rd_atom * props->max_qp;
744e126ba97SEli Cohen 	props->max_srq_sge	   = max_rq_sg - 1;
745911f4331SSagi Grimberg 	props->max_fast_reg_page_list_len =
746911f4331SSagi Grimberg 		1 << MLX5_CAP_GEN(mdev, log_max_klm_list_size);
747da7525d2SEran Ben Elisha 	get_atomic_caps(dev, props);
74881bea28fSEli Cohen 	props->masked_atomic_cap   = IB_ATOMIC_NONE;
749938fe83cSSaeed Mahameed 	props->max_mcast_grp	   = 1 << MLX5_CAP_GEN(mdev, log_max_mcg);
750938fe83cSSaeed Mahameed 	props->max_mcast_qp_attach = MLX5_CAP_GEN(mdev, max_qp_mcg);
751e126ba97SEli Cohen 	props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
752e126ba97SEli Cohen 					   props->max_mcast_grp;
753e126ba97SEli Cohen 	props->max_map_per_fmr = INT_MAX; /* no limit in ConnectIB */
75486695a65SMaor Gottlieb 	props->max_ah = INT_MAX;
7557c60bcbbSMatan Barak 	props->hca_core_clock = MLX5_CAP_GEN(mdev, device_frequency_khz);
7567c60bcbbSMatan Barak 	props->timestamp_mask = 0x7FFFFFFFFFFFFFFFULL;
757e126ba97SEli Cohen 
7588cdd312cSHaggai Eran #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
759938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, pg))
7608cdd312cSHaggai Eran 		props->device_cap_flags |= IB_DEVICE_ON_DEMAND_PAGING;
7618cdd312cSHaggai Eran 	props->odp_caps = dev->odp_caps;
7628cdd312cSHaggai Eran #endif
7638cdd312cSHaggai Eran 
764051f2630SLeon Romanovsky 	if (MLX5_CAP_GEN(mdev, cd))
765051f2630SLeon Romanovsky 		props->device_cap_flags |= IB_DEVICE_CROSS_CHANNEL;
766051f2630SLeon Romanovsky 
767eff901d3SEli Cohen 	if (!mlx5_core_is_pf(mdev))
768eff901d3SEli Cohen 		props->device_cap_flags |= IB_DEVICE_VIRTUAL_FUNCTION;
769eff901d3SEli Cohen 
77031f69a82SYishai Hadas 	if (mlx5_ib_port_link_layer(ibdev, 1) ==
77131f69a82SYishai Hadas 	    IB_LINK_LAYER_ETHERNET) {
77231f69a82SYishai Hadas 		props->rss_caps.max_rwq_indirection_tables =
77331f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt);
77431f69a82SYishai Hadas 		props->rss_caps.max_rwq_indirection_table_size =
77531f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt_size);
77631f69a82SYishai Hadas 		props->rss_caps.supported_qpts = 1 << IB_QPT_RAW_PACKET;
77731f69a82SYishai Hadas 		props->max_wq_type_rq =
77831f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rq);
77931f69a82SYishai Hadas 	}
78031f69a82SYishai Hadas 
781eb761894SArtemy Kovalyov 	if (MLX5_CAP_GEN(mdev, tag_matching)) {
78278b1beb0SLeon Romanovsky 		props->tm_caps.max_rndv_hdr_size = MLX5_TM_MAX_RNDV_MSG_SIZE;
78378b1beb0SLeon Romanovsky 		props->tm_caps.max_num_tags =
784eb761894SArtemy Kovalyov 			(1 << MLX5_CAP_GEN(mdev, log_tag_matching_list_sz)) - 1;
78578b1beb0SLeon Romanovsky 		props->tm_caps.flags = IB_TM_CAP_RC;
78678b1beb0SLeon Romanovsky 		props->tm_caps.max_ops =
787eb761894SArtemy Kovalyov 			1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
78878b1beb0SLeon Romanovsky 		props->tm_caps.max_sge = MLX5_TM_MAX_SGE;
789eb761894SArtemy Kovalyov 	}
790eb761894SArtemy Kovalyov 
79187ab3f52SYonatan Cohen 	if (MLX5_CAP_GEN(dev->mdev, cq_moderation)) {
79287ab3f52SYonatan Cohen 		props->cq_caps.max_cq_moderation_count =
79387ab3f52SYonatan Cohen 						MLX5_MAX_CQ_COUNT;
79487ab3f52SYonatan Cohen 		props->cq_caps.max_cq_moderation_period =
79587ab3f52SYonatan Cohen 						MLX5_MAX_CQ_PERIOD;
79687ab3f52SYonatan Cohen 	}
79787ab3f52SYonatan Cohen 
7987e43a2a5SBodong Wang 	if (field_avail(typeof(resp), cqe_comp_caps, uhw->outlen)) {
7997e43a2a5SBodong Wang 		resp.cqe_comp_caps.max_num =
8007e43a2a5SBodong Wang 			MLX5_CAP_GEN(dev->mdev, cqe_compression) ?
8017e43a2a5SBodong Wang 			MLX5_CAP_GEN(dev->mdev, cqe_compression_max_num) : 0;
8027e43a2a5SBodong Wang 		resp.cqe_comp_caps.supported_format =
8037e43a2a5SBodong Wang 			MLX5_IB_CQE_RES_FORMAT_HASH |
8047e43a2a5SBodong Wang 			MLX5_IB_CQE_RES_FORMAT_CSUM;
8057e43a2a5SBodong Wang 		resp.response_length += sizeof(resp.cqe_comp_caps);
8067e43a2a5SBodong Wang 	}
8077e43a2a5SBodong Wang 
808d949167dSBodong Wang 	if (field_avail(typeof(resp), packet_pacing_caps, uhw->outlen)) {
809d949167dSBodong Wang 		if (MLX5_CAP_QOS(mdev, packet_pacing) &&
810d949167dSBodong Wang 		    MLX5_CAP_GEN(mdev, qos)) {
811d949167dSBodong Wang 			resp.packet_pacing_caps.qp_rate_limit_max =
812d949167dSBodong Wang 				MLX5_CAP_QOS(mdev, packet_pacing_max_rate);
813d949167dSBodong Wang 			resp.packet_pacing_caps.qp_rate_limit_min =
814d949167dSBodong Wang 				MLX5_CAP_QOS(mdev, packet_pacing_min_rate);
815d949167dSBodong Wang 			resp.packet_pacing_caps.supported_qpts |=
816d949167dSBodong Wang 				1 << IB_QPT_RAW_PACKET;
817d949167dSBodong Wang 		}
818d949167dSBodong Wang 		resp.response_length += sizeof(resp.packet_pacing_caps);
819d949167dSBodong Wang 	}
820d949167dSBodong Wang 
8219f885201SLeon Romanovsky 	if (field_avail(typeof(resp), mlx5_ib_support_multi_pkt_send_wqes,
8229f885201SLeon Romanovsky 			uhw->outlen)) {
823795b609cSBodong Wang 		if (MLX5_CAP_ETH(mdev, multi_pkt_send_wqe))
8249f885201SLeon Romanovsky 			resp.mlx5_ib_support_multi_pkt_send_wqes =
825795b609cSBodong Wang 				MLX5_IB_ALLOW_MPW;
826050da902SBodong Wang 
827050da902SBodong Wang 		if (MLX5_CAP_ETH(mdev, enhanced_multi_pkt_send_wqe))
828050da902SBodong Wang 			resp.mlx5_ib_support_multi_pkt_send_wqes |=
829050da902SBodong Wang 				MLX5_IB_SUPPORT_EMPW;
830050da902SBodong Wang 
8319f885201SLeon Romanovsky 		resp.response_length +=
8329f885201SLeon Romanovsky 			sizeof(resp.mlx5_ib_support_multi_pkt_send_wqes);
8339f885201SLeon Romanovsky 	}
8349f885201SLeon Romanovsky 
835de57f2adSGuy Levi 	if (field_avail(typeof(resp), flags, uhw->outlen)) {
836de57f2adSGuy Levi 		resp.response_length += sizeof(resp.flags);
8377a0c8f42SGuy Levi 
838de57f2adSGuy Levi 		if (MLX5_CAP_GEN(mdev, cqe_compression_128))
839de57f2adSGuy Levi 			resp.flags |=
840de57f2adSGuy Levi 				MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_COMP;
8417a0c8f42SGuy Levi 
8427a0c8f42SGuy Levi 		if (MLX5_CAP_GEN(mdev, cqe_128_always))
8437a0c8f42SGuy Levi 			resp.flags |= MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_PAD;
844de57f2adSGuy Levi 	}
8459f885201SLeon Romanovsky 
84696dc3fc5SNoa Osherovich 	if (field_avail(typeof(resp), sw_parsing_caps,
84796dc3fc5SNoa Osherovich 			uhw->outlen)) {
84896dc3fc5SNoa Osherovich 		resp.response_length += sizeof(resp.sw_parsing_caps);
84996dc3fc5SNoa Osherovich 		if (MLX5_CAP_ETH(mdev, swp)) {
85096dc3fc5SNoa Osherovich 			resp.sw_parsing_caps.sw_parsing_offloads |=
85196dc3fc5SNoa Osherovich 				MLX5_IB_SW_PARSING;
85296dc3fc5SNoa Osherovich 
85396dc3fc5SNoa Osherovich 			if (MLX5_CAP_ETH(mdev, swp_csum))
85496dc3fc5SNoa Osherovich 				resp.sw_parsing_caps.sw_parsing_offloads |=
85596dc3fc5SNoa Osherovich 					MLX5_IB_SW_PARSING_CSUM;
85696dc3fc5SNoa Osherovich 
85796dc3fc5SNoa Osherovich 			if (MLX5_CAP_ETH(mdev, swp_lso))
85896dc3fc5SNoa Osherovich 				resp.sw_parsing_caps.sw_parsing_offloads |=
85996dc3fc5SNoa Osherovich 					MLX5_IB_SW_PARSING_LSO;
86096dc3fc5SNoa Osherovich 
86196dc3fc5SNoa Osherovich 			if (resp.sw_parsing_caps.sw_parsing_offloads)
86296dc3fc5SNoa Osherovich 				resp.sw_parsing_caps.supported_qpts =
86396dc3fc5SNoa Osherovich 					BIT(IB_QPT_RAW_PACKET);
86496dc3fc5SNoa Osherovich 		}
86596dc3fc5SNoa Osherovich 	}
86696dc3fc5SNoa Osherovich 
867b4f34597SNoa Osherovich 	if (field_avail(typeof(resp), striding_rq_caps, uhw->outlen)) {
868b4f34597SNoa Osherovich 		resp.response_length += sizeof(resp.striding_rq_caps);
869b4f34597SNoa Osherovich 		if (MLX5_CAP_GEN(mdev, striding_rq)) {
870b4f34597SNoa Osherovich 			resp.striding_rq_caps.min_single_stride_log_num_of_bytes =
871b4f34597SNoa Osherovich 				MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES;
872b4f34597SNoa Osherovich 			resp.striding_rq_caps.max_single_stride_log_num_of_bytes =
873b4f34597SNoa Osherovich 				MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES;
874b4f34597SNoa Osherovich 			resp.striding_rq_caps.min_single_wqe_log_num_of_strides =
875b4f34597SNoa Osherovich 				MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES;
876b4f34597SNoa Osherovich 			resp.striding_rq_caps.max_single_wqe_log_num_of_strides =
877b4f34597SNoa Osherovich 				MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES;
878b4f34597SNoa Osherovich 			resp.striding_rq_caps.supported_qpts =
879b4f34597SNoa Osherovich 				BIT(IB_QPT_RAW_PACKET);
880b4f34597SNoa Osherovich 		}
881b4f34597SNoa Osherovich 	}
882b4f34597SNoa Osherovich 
883f95ef6cbSMaor Gottlieb 	if (field_avail(typeof(resp), tunnel_offloads_caps,
884f95ef6cbSMaor Gottlieb 			uhw->outlen)) {
885f95ef6cbSMaor Gottlieb 		resp.response_length += sizeof(resp.tunnel_offloads_caps);
886f95ef6cbSMaor Gottlieb 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_vxlan))
887f95ef6cbSMaor Gottlieb 			resp.tunnel_offloads_caps |=
888f95ef6cbSMaor Gottlieb 				MLX5_IB_TUNNELED_OFFLOADS_VXLAN;
889f95ef6cbSMaor Gottlieb 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_geneve_rx))
890f95ef6cbSMaor Gottlieb 			resp.tunnel_offloads_caps |=
891f95ef6cbSMaor Gottlieb 				MLX5_IB_TUNNELED_OFFLOADS_GENEVE;
892f95ef6cbSMaor Gottlieb 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_gre))
893f95ef6cbSMaor Gottlieb 			resp.tunnel_offloads_caps |=
894f95ef6cbSMaor Gottlieb 				MLX5_IB_TUNNELED_OFFLOADS_GRE;
895f95ef6cbSMaor Gottlieb 	}
896f95ef6cbSMaor Gottlieb 
897402ca536SBodong Wang 	if (uhw->outlen) {
898402ca536SBodong Wang 		err = ib_copy_to_udata(uhw, &resp, resp.response_length);
899402ca536SBodong Wang 
900402ca536SBodong Wang 		if (err)
901402ca536SBodong Wang 			return err;
902402ca536SBodong Wang 	}
903402ca536SBodong Wang 
9041b5daf11SMajd Dibbiny 	return 0;
9051b5daf11SMajd Dibbiny }
906e126ba97SEli Cohen 
9071b5daf11SMajd Dibbiny enum mlx5_ib_width {
9081b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_1X	= 1 << 0,
9091b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_2X	= 1 << 1,
9101b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_4X	= 1 << 2,
9111b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_8X	= 1 << 3,
9121b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_12X	= 1 << 4
9131b5daf11SMajd Dibbiny };
9141b5daf11SMajd Dibbiny 
9151b5daf11SMajd Dibbiny static int translate_active_width(struct ib_device *ibdev, u8 active_width,
9161b5daf11SMajd Dibbiny 				  u8 *ib_width)
9171b5daf11SMajd Dibbiny {
9181b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
9191b5daf11SMajd Dibbiny 	int err = 0;
9201b5daf11SMajd Dibbiny 
9211b5daf11SMajd Dibbiny 	if (active_width & MLX5_IB_WIDTH_1X) {
9221b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_1X;
9231b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_2X) {
9241b5daf11SMajd Dibbiny 		mlx5_ib_dbg(dev, "active_width %d is not supported by IB spec\n",
9251b5daf11SMajd Dibbiny 			    (int)active_width);
9261b5daf11SMajd Dibbiny 		err = -EINVAL;
9271b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_4X) {
9281b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_4X;
9291b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_8X) {
9301b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_8X;
9311b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_12X) {
9321b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_12X;
9331b5daf11SMajd Dibbiny 	} else {
9341b5daf11SMajd Dibbiny 		mlx5_ib_dbg(dev, "Invalid active_width %d\n",
9351b5daf11SMajd Dibbiny 			    (int)active_width);
9361b5daf11SMajd Dibbiny 		err = -EINVAL;
9371b5daf11SMajd Dibbiny 	}
9381b5daf11SMajd Dibbiny 
9391b5daf11SMajd Dibbiny 	return err;
9401b5daf11SMajd Dibbiny }
9411b5daf11SMajd Dibbiny 
9421b5daf11SMajd Dibbiny static int mlx5_mtu_to_ib_mtu(int mtu)
9431b5daf11SMajd Dibbiny {
9441b5daf11SMajd Dibbiny 	switch (mtu) {
9451b5daf11SMajd Dibbiny 	case 256: return 1;
9461b5daf11SMajd Dibbiny 	case 512: return 2;
9471b5daf11SMajd Dibbiny 	case 1024: return 3;
9481b5daf11SMajd Dibbiny 	case 2048: return 4;
9491b5daf11SMajd Dibbiny 	case 4096: return 5;
9501b5daf11SMajd Dibbiny 	default:
9511b5daf11SMajd Dibbiny 		pr_warn("invalid mtu\n");
9521b5daf11SMajd Dibbiny 		return -1;
9531b5daf11SMajd Dibbiny 	}
9541b5daf11SMajd Dibbiny }
9551b5daf11SMajd Dibbiny 
9561b5daf11SMajd Dibbiny enum ib_max_vl_num {
9571b5daf11SMajd Dibbiny 	__IB_MAX_VL_0		= 1,
9581b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_1		= 2,
9591b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_3		= 3,
9601b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_7		= 4,
9611b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_14	= 5,
9621b5daf11SMajd Dibbiny };
9631b5daf11SMajd Dibbiny 
9641b5daf11SMajd Dibbiny enum mlx5_vl_hw_cap {
9651b5daf11SMajd Dibbiny 	MLX5_VL_HW_0	= 1,
9661b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_1	= 2,
9671b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_2	= 3,
9681b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_3	= 4,
9691b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_4	= 5,
9701b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_5	= 6,
9711b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_6	= 7,
9721b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_7	= 8,
9731b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_14	= 15
9741b5daf11SMajd Dibbiny };
9751b5daf11SMajd Dibbiny 
9761b5daf11SMajd Dibbiny static int translate_max_vl_num(struct ib_device *ibdev, u8 vl_hw_cap,
9771b5daf11SMajd Dibbiny 				u8 *max_vl_num)
9781b5daf11SMajd Dibbiny {
9791b5daf11SMajd Dibbiny 	switch (vl_hw_cap) {
9801b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0:
9811b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0;
9821b5daf11SMajd Dibbiny 		break;
9831b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_1:
9841b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_1;
9851b5daf11SMajd Dibbiny 		break;
9861b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_3:
9871b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_3;
9881b5daf11SMajd Dibbiny 		break;
9891b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_7:
9901b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_7;
9911b5daf11SMajd Dibbiny 		break;
9921b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_14:
9931b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_14;
9941b5daf11SMajd Dibbiny 		break;
9951b5daf11SMajd Dibbiny 
9961b5daf11SMajd Dibbiny 	default:
9971b5daf11SMajd Dibbiny 		return -EINVAL;
9981b5daf11SMajd Dibbiny 	}
9991b5daf11SMajd Dibbiny 
10001b5daf11SMajd Dibbiny 	return 0;
10011b5daf11SMajd Dibbiny }
10021b5daf11SMajd Dibbiny 
10031b5daf11SMajd Dibbiny static int mlx5_query_hca_port(struct ib_device *ibdev, u8 port,
10041b5daf11SMajd Dibbiny 			       struct ib_port_attr *props)
10051b5daf11SMajd Dibbiny {
10061b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
10071b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
10081b5daf11SMajd Dibbiny 	struct mlx5_hca_vport_context *rep;
1009046339eaSSaeed Mahameed 	u16 max_mtu;
1010046339eaSSaeed Mahameed 	u16 oper_mtu;
10111b5daf11SMajd Dibbiny 	int err;
10121b5daf11SMajd Dibbiny 	u8 ib_link_width_oper;
10131b5daf11SMajd Dibbiny 	u8 vl_hw_cap;
10141b5daf11SMajd Dibbiny 
10151b5daf11SMajd Dibbiny 	rep = kzalloc(sizeof(*rep), GFP_KERNEL);
10161b5daf11SMajd Dibbiny 	if (!rep) {
10171b5daf11SMajd Dibbiny 		err = -ENOMEM;
10181b5daf11SMajd Dibbiny 		goto out;
10191b5daf11SMajd Dibbiny 	}
10201b5daf11SMajd Dibbiny 
1021c4550c63SOr Gerlitz 	/* props being zeroed by the caller, avoid zeroing it here */
10221b5daf11SMajd Dibbiny 
10231b5daf11SMajd Dibbiny 	err = mlx5_query_hca_vport_context(mdev, 0, port, 0, rep);
10241b5daf11SMajd Dibbiny 	if (err)
10251b5daf11SMajd Dibbiny 		goto out;
10261b5daf11SMajd Dibbiny 
10271b5daf11SMajd Dibbiny 	props->lid		= rep->lid;
10281b5daf11SMajd Dibbiny 	props->lmc		= rep->lmc;
10291b5daf11SMajd Dibbiny 	props->sm_lid		= rep->sm_lid;
10301b5daf11SMajd Dibbiny 	props->sm_sl		= rep->sm_sl;
10311b5daf11SMajd Dibbiny 	props->state		= rep->vport_state;
10321b5daf11SMajd Dibbiny 	props->phys_state	= rep->port_physical_state;
10331b5daf11SMajd Dibbiny 	props->port_cap_flags	= rep->cap_mask1;
10341b5daf11SMajd Dibbiny 	props->gid_tbl_len	= mlx5_get_gid_table_len(MLX5_CAP_GEN(mdev, gid_table_size));
10351b5daf11SMajd Dibbiny 	props->max_msg_sz	= 1 << MLX5_CAP_GEN(mdev, log_max_msg);
10361b5daf11SMajd Dibbiny 	props->pkey_tbl_len	= mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, pkey_table_size));
10371b5daf11SMajd Dibbiny 	props->bad_pkey_cntr	= rep->pkey_violation_counter;
10381b5daf11SMajd Dibbiny 	props->qkey_viol_cntr	= rep->qkey_violation_counter;
10391b5daf11SMajd Dibbiny 	props->subnet_timeout	= rep->subnet_timeout;
10401b5daf11SMajd Dibbiny 	props->init_type_reply	= rep->init_type_reply;
1041eff901d3SEli Cohen 	props->grh_required	= rep->grh_required;
10421b5daf11SMajd Dibbiny 
10431b5daf11SMajd Dibbiny 	err = mlx5_query_port_link_width_oper(mdev, &ib_link_width_oper, port);
10441b5daf11SMajd Dibbiny 	if (err)
10451b5daf11SMajd Dibbiny 		goto out;
10461b5daf11SMajd Dibbiny 
10471b5daf11SMajd Dibbiny 	err = translate_active_width(ibdev, ib_link_width_oper,
10481b5daf11SMajd Dibbiny 				     &props->active_width);
10491b5daf11SMajd Dibbiny 	if (err)
10501b5daf11SMajd Dibbiny 		goto out;
1051d5beb7f2SNoa Osherovich 	err = mlx5_query_port_ib_proto_oper(mdev, &props->active_speed, port);
10521b5daf11SMajd Dibbiny 	if (err)
10531b5daf11SMajd Dibbiny 		goto out;
10541b5daf11SMajd Dibbiny 
1055facc9699SSaeed Mahameed 	mlx5_query_port_max_mtu(mdev, &max_mtu, port);
10561b5daf11SMajd Dibbiny 
10571b5daf11SMajd Dibbiny 	props->max_mtu = mlx5_mtu_to_ib_mtu(max_mtu);
10581b5daf11SMajd Dibbiny 
1059facc9699SSaeed Mahameed 	mlx5_query_port_oper_mtu(mdev, &oper_mtu, port);
10601b5daf11SMajd Dibbiny 
10611b5daf11SMajd Dibbiny 	props->active_mtu = mlx5_mtu_to_ib_mtu(oper_mtu);
10621b5daf11SMajd Dibbiny 
10631b5daf11SMajd Dibbiny 	err = mlx5_query_port_vl_hw_cap(mdev, &vl_hw_cap, port);
10641b5daf11SMajd Dibbiny 	if (err)
10651b5daf11SMajd Dibbiny 		goto out;
10661b5daf11SMajd Dibbiny 
10671b5daf11SMajd Dibbiny 	err = translate_max_vl_num(ibdev, vl_hw_cap,
10681b5daf11SMajd Dibbiny 				   &props->max_vl_num);
10691b5daf11SMajd Dibbiny out:
10701b5daf11SMajd Dibbiny 	kfree(rep);
1071e126ba97SEli Cohen 	return err;
1072e126ba97SEli Cohen }
1073e126ba97SEli Cohen 
1074e126ba97SEli Cohen int mlx5_ib_query_port(struct ib_device *ibdev, u8 port,
1075e126ba97SEli Cohen 		       struct ib_port_attr *props)
1076e126ba97SEli Cohen {
1077095b0927SIlan Tayari 	unsigned int count;
1078095b0927SIlan Tayari 	int ret;
1079095b0927SIlan Tayari 
10801b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
10811b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
1082095b0927SIlan Tayari 		ret = mlx5_query_mad_ifc_port(ibdev, port, props);
1083095b0927SIlan Tayari 		break;
1084e126ba97SEli Cohen 
10851b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
1086095b0927SIlan Tayari 		ret = mlx5_query_hca_port(ibdev, port, props);
1087095b0927SIlan Tayari 		break;
10881b5daf11SMajd Dibbiny 
10893f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
1090095b0927SIlan Tayari 		ret = mlx5_query_port_roce(ibdev, port, props);
1091095b0927SIlan Tayari 		break;
10923f89a643SAchiad Shochat 
10931b5daf11SMajd Dibbiny 	default:
1094095b0927SIlan Tayari 		ret = -EINVAL;
1095e126ba97SEli Cohen 	}
1096095b0927SIlan Tayari 
1097095b0927SIlan Tayari 	if (!ret && props) {
1098095b0927SIlan Tayari 		count = mlx5_core_reserved_gids_count(to_mdev(ibdev)->mdev);
1099095b0927SIlan Tayari 		props->gid_tbl_len -= count;
1100095b0927SIlan Tayari 	}
1101095b0927SIlan Tayari 	return ret;
1102e126ba97SEli Cohen }
1103e126ba97SEli Cohen 
1104e126ba97SEli Cohen static int mlx5_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
1105e126ba97SEli Cohen 			     union ib_gid *gid)
1106e126ba97SEli Cohen {
11071b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
11081b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
1109e126ba97SEli Cohen 
11101b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
11111b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
11121b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_gids(ibdev, port, index, gid);
1113e126ba97SEli Cohen 
11141b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
11151b5daf11SMajd Dibbiny 		return mlx5_query_hca_vport_gid(mdev, 0, port, 0, index, gid);
1116e126ba97SEli Cohen 
11171b5daf11SMajd Dibbiny 	default:
11181b5daf11SMajd Dibbiny 		return -EINVAL;
11191b5daf11SMajd Dibbiny 	}
1120e126ba97SEli Cohen 
1121e126ba97SEli Cohen }
1122e126ba97SEli Cohen 
1123e126ba97SEli Cohen static int mlx5_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
1124e126ba97SEli Cohen 			      u16 *pkey)
1125e126ba97SEli Cohen {
11261b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
11271b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
1128e126ba97SEli Cohen 
11291b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
11301b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
11311b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_pkey(ibdev, port, index, pkey);
1132e126ba97SEli Cohen 
11331b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
11341b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
11351b5daf11SMajd Dibbiny 		return mlx5_query_hca_vport_pkey(mdev, 0, port,  0, index,
11361b5daf11SMajd Dibbiny 						 pkey);
11371b5daf11SMajd Dibbiny 	default:
11381b5daf11SMajd Dibbiny 		return -EINVAL;
1139e126ba97SEli Cohen 	}
11401b5daf11SMajd Dibbiny }
1141e126ba97SEli Cohen 
1142e126ba97SEli Cohen static int mlx5_ib_modify_device(struct ib_device *ibdev, int mask,
1143e126ba97SEli Cohen 				 struct ib_device_modify *props)
1144e126ba97SEli Cohen {
1145e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1146e126ba97SEli Cohen 	struct mlx5_reg_node_desc in;
1147e126ba97SEli Cohen 	struct mlx5_reg_node_desc out;
1148e126ba97SEli Cohen 	int err;
1149e126ba97SEli Cohen 
1150e126ba97SEli Cohen 	if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
1151e126ba97SEli Cohen 		return -EOPNOTSUPP;
1152e126ba97SEli Cohen 
1153e126ba97SEli Cohen 	if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
1154e126ba97SEli Cohen 		return 0;
1155e126ba97SEli Cohen 
1156e126ba97SEli Cohen 	/*
1157e126ba97SEli Cohen 	 * If possible, pass node desc to FW, so it can generate
1158e126ba97SEli Cohen 	 * a 144 trap.  If cmd fails, just ignore.
1159e126ba97SEli Cohen 	 */
1160bd99fdeaSYuval Shaia 	memcpy(&in, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
11619603b61dSJack Morgenstein 	err = mlx5_core_access_reg(dev->mdev, &in, sizeof(in), &out,
1162e126ba97SEli Cohen 				   sizeof(out), MLX5_REG_NODE_DESC, 0, 1);
1163e126ba97SEli Cohen 	if (err)
1164e126ba97SEli Cohen 		return err;
1165e126ba97SEli Cohen 
1166bd99fdeaSYuval Shaia 	memcpy(ibdev->node_desc, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
1167e126ba97SEli Cohen 
1168e126ba97SEli Cohen 	return err;
1169e126ba97SEli Cohen }
1170e126ba97SEli Cohen 
1171cdbe33d0SEli Cohen static int set_port_caps_atomic(struct mlx5_ib_dev *dev, u8 port_num, u32 mask,
1172cdbe33d0SEli Cohen 				u32 value)
1173cdbe33d0SEli Cohen {
1174cdbe33d0SEli Cohen 	struct mlx5_hca_vport_context ctx = {};
1175cdbe33d0SEli Cohen 	int err;
1176cdbe33d0SEli Cohen 
1177cdbe33d0SEli Cohen 	err = mlx5_query_hca_vport_context(dev->mdev, 0,
1178cdbe33d0SEli Cohen 					   port_num, 0, &ctx);
1179cdbe33d0SEli Cohen 	if (err)
1180cdbe33d0SEli Cohen 		return err;
1181cdbe33d0SEli Cohen 
1182cdbe33d0SEli Cohen 	if (~ctx.cap_mask1_perm & mask) {
1183cdbe33d0SEli Cohen 		mlx5_ib_warn(dev, "trying to change bitmask 0x%X but change supported 0x%X\n",
1184cdbe33d0SEli Cohen 			     mask, ctx.cap_mask1_perm);
1185cdbe33d0SEli Cohen 		return -EINVAL;
1186cdbe33d0SEli Cohen 	}
1187cdbe33d0SEli Cohen 
1188cdbe33d0SEli Cohen 	ctx.cap_mask1 = value;
1189cdbe33d0SEli Cohen 	ctx.cap_mask1_perm = mask;
1190cdbe33d0SEli Cohen 	err = mlx5_core_modify_hca_vport_context(dev->mdev, 0,
1191cdbe33d0SEli Cohen 						 port_num, 0, &ctx);
1192cdbe33d0SEli Cohen 
1193cdbe33d0SEli Cohen 	return err;
1194cdbe33d0SEli Cohen }
1195cdbe33d0SEli Cohen 
1196e126ba97SEli Cohen static int mlx5_ib_modify_port(struct ib_device *ibdev, u8 port, int mask,
1197e126ba97SEli Cohen 			       struct ib_port_modify *props)
1198e126ba97SEli Cohen {
1199e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1200e126ba97SEli Cohen 	struct ib_port_attr attr;
1201e126ba97SEli Cohen 	u32 tmp;
1202e126ba97SEli Cohen 	int err;
1203cdbe33d0SEli Cohen 	u32 change_mask;
1204cdbe33d0SEli Cohen 	u32 value;
1205cdbe33d0SEli Cohen 	bool is_ib = (mlx5_ib_port_link_layer(ibdev, port) ==
1206cdbe33d0SEli Cohen 		      IB_LINK_LAYER_INFINIBAND);
1207cdbe33d0SEli Cohen 
1208ec255879SMajd Dibbiny 	/* CM layer calls ib_modify_port() regardless of the link layer. For
1209ec255879SMajd Dibbiny 	 * Ethernet ports, qkey violation and Port capabilities are meaningless.
1210ec255879SMajd Dibbiny 	 */
1211ec255879SMajd Dibbiny 	if (!is_ib)
1212ec255879SMajd Dibbiny 		return 0;
1213ec255879SMajd Dibbiny 
1214cdbe33d0SEli Cohen 	if (MLX5_CAP_GEN(dev->mdev, ib_virt) && is_ib) {
1215cdbe33d0SEli Cohen 		change_mask = props->clr_port_cap_mask | props->set_port_cap_mask;
1216cdbe33d0SEli Cohen 		value = ~props->clr_port_cap_mask | props->set_port_cap_mask;
1217cdbe33d0SEli Cohen 		return set_port_caps_atomic(dev, port, change_mask, value);
1218cdbe33d0SEli Cohen 	}
1219e126ba97SEli Cohen 
1220e126ba97SEli Cohen 	mutex_lock(&dev->cap_mask_mutex);
1221e126ba97SEli Cohen 
1222c4550c63SOr Gerlitz 	err = ib_query_port(ibdev, port, &attr);
1223e126ba97SEli Cohen 	if (err)
1224e126ba97SEli Cohen 		goto out;
1225e126ba97SEli Cohen 
1226e126ba97SEli Cohen 	tmp = (attr.port_cap_flags | props->set_port_cap_mask) &
1227e126ba97SEli Cohen 		~props->clr_port_cap_mask;
1228e126ba97SEli Cohen 
12299603b61dSJack Morgenstein 	err = mlx5_set_port_caps(dev->mdev, port, tmp);
1230e126ba97SEli Cohen 
1231e126ba97SEli Cohen out:
1232e126ba97SEli Cohen 	mutex_unlock(&dev->cap_mask_mutex);
1233e126ba97SEli Cohen 	return err;
1234e126ba97SEli Cohen }
1235e126ba97SEli Cohen 
123630aa60b3SEli Cohen static void print_lib_caps(struct mlx5_ib_dev *dev, u64 caps)
123730aa60b3SEli Cohen {
123830aa60b3SEli Cohen 	mlx5_ib_dbg(dev, "MLX5_LIB_CAP_4K_UAR = %s\n",
123930aa60b3SEli Cohen 		    caps & MLX5_LIB_CAP_4K_UAR ? "y" : "n");
124030aa60b3SEli Cohen }
124130aa60b3SEli Cohen 
1242b037c29aSEli Cohen static int calc_total_bfregs(struct mlx5_ib_dev *dev, bool lib_uar_4k,
1243b037c29aSEli Cohen 			     struct mlx5_ib_alloc_ucontext_req_v2 *req,
1244b037c29aSEli Cohen 			     u32 *num_sys_pages)
1245b037c29aSEli Cohen {
1246b037c29aSEli Cohen 	int uars_per_sys_page;
1247b037c29aSEli Cohen 	int bfregs_per_sys_page;
1248b037c29aSEli Cohen 	int ref_bfregs = req->total_num_bfregs;
1249b037c29aSEli Cohen 
1250b037c29aSEli Cohen 	if (req->total_num_bfregs == 0)
1251b037c29aSEli Cohen 		return -EINVAL;
1252b037c29aSEli Cohen 
1253b037c29aSEli Cohen 	BUILD_BUG_ON(MLX5_MAX_BFREGS % MLX5_NON_FP_BFREGS_IN_PAGE);
1254b037c29aSEli Cohen 	BUILD_BUG_ON(MLX5_MAX_BFREGS < MLX5_NON_FP_BFREGS_IN_PAGE);
1255b037c29aSEli Cohen 
1256b037c29aSEli Cohen 	if (req->total_num_bfregs > MLX5_MAX_BFREGS)
1257b037c29aSEli Cohen 		return -ENOMEM;
1258b037c29aSEli Cohen 
1259b037c29aSEli Cohen 	uars_per_sys_page = get_uars_per_sys_page(dev, lib_uar_4k);
1260b037c29aSEli Cohen 	bfregs_per_sys_page = uars_per_sys_page * MLX5_NON_FP_BFREGS_PER_UAR;
1261b037c29aSEli Cohen 	req->total_num_bfregs = ALIGN(req->total_num_bfregs, bfregs_per_sys_page);
1262b037c29aSEli Cohen 	*num_sys_pages = req->total_num_bfregs / bfregs_per_sys_page;
1263b037c29aSEli Cohen 
1264b037c29aSEli Cohen 	if (req->num_low_latency_bfregs > req->total_num_bfregs - 1)
1265b037c29aSEli Cohen 		return -EINVAL;
1266b037c29aSEli Cohen 
12679c2d33d4SColin Ian King 	mlx5_ib_dbg(dev, "uar_4k: fw support %s, lib support %s, user requested %d bfregs, allocated %d, using %d sys pages\n",
1268b037c29aSEli Cohen 		    MLX5_CAP_GEN(dev->mdev, uar_4k) ? "yes" : "no",
1269b037c29aSEli Cohen 		    lib_uar_4k ? "yes" : "no", ref_bfregs,
1270b037c29aSEli Cohen 		    req->total_num_bfregs, *num_sys_pages);
1271b037c29aSEli Cohen 
1272b037c29aSEli Cohen 	return 0;
1273b037c29aSEli Cohen }
1274b037c29aSEli Cohen 
1275b037c29aSEli Cohen static int allocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
1276b037c29aSEli Cohen {
1277b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1278b037c29aSEli Cohen 	int err;
1279b037c29aSEli Cohen 	int i;
1280b037c29aSEli Cohen 
1281b037c29aSEli Cohen 	bfregi = &context->bfregi;
1282b037c29aSEli Cohen 	for (i = 0; i < bfregi->num_sys_pages; i++) {
1283b037c29aSEli Cohen 		err = mlx5_cmd_alloc_uar(dev->mdev, &bfregi->sys_pages[i]);
1284b037c29aSEli Cohen 		if (err)
1285b037c29aSEli Cohen 			goto error;
1286b037c29aSEli Cohen 
1287b037c29aSEli Cohen 		mlx5_ib_dbg(dev, "allocated uar %d\n", bfregi->sys_pages[i]);
1288b037c29aSEli Cohen 	}
1289b037c29aSEli Cohen 	return 0;
1290b037c29aSEli Cohen 
1291b037c29aSEli Cohen error:
1292b037c29aSEli Cohen 	for (--i; i >= 0; i--)
1293b037c29aSEli Cohen 		if (mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]))
1294b037c29aSEli Cohen 			mlx5_ib_warn(dev, "failed to free uar %d\n", i);
1295b037c29aSEli Cohen 
1296b037c29aSEli Cohen 	return err;
1297b037c29aSEli Cohen }
1298b037c29aSEli Cohen 
1299b037c29aSEli Cohen static int deallocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
1300b037c29aSEli Cohen {
1301b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1302b037c29aSEli Cohen 	int err;
1303b037c29aSEli Cohen 	int i;
1304b037c29aSEli Cohen 
1305b037c29aSEli Cohen 	bfregi = &context->bfregi;
1306b037c29aSEli Cohen 	for (i = 0; i < bfregi->num_sys_pages; i++) {
1307b037c29aSEli Cohen 		err = mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]);
1308b037c29aSEli Cohen 		if (err) {
1309b037c29aSEli Cohen 			mlx5_ib_warn(dev, "failed to free uar %d\n", i);
1310b037c29aSEli Cohen 			return err;
1311b037c29aSEli Cohen 		}
1312b037c29aSEli Cohen 	}
1313b037c29aSEli Cohen 	return 0;
1314b037c29aSEli Cohen }
1315b037c29aSEli Cohen 
1316c85023e1SHuy Nguyen static int mlx5_ib_alloc_transport_domain(struct mlx5_ib_dev *dev, u32 *tdn)
1317c85023e1SHuy Nguyen {
1318c85023e1SHuy Nguyen 	int err;
1319c85023e1SHuy Nguyen 
1320c85023e1SHuy Nguyen 	err = mlx5_core_alloc_transport_domain(dev->mdev, tdn);
1321c85023e1SHuy Nguyen 	if (err)
1322c85023e1SHuy Nguyen 		return err;
1323c85023e1SHuy Nguyen 
1324c85023e1SHuy Nguyen 	if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
1325c85023e1SHuy Nguyen 	    !MLX5_CAP_GEN(dev->mdev, disable_local_lb))
1326c85023e1SHuy Nguyen 		return err;
1327c85023e1SHuy Nguyen 
1328c85023e1SHuy Nguyen 	mutex_lock(&dev->lb_mutex);
1329c85023e1SHuy Nguyen 	dev->user_td++;
1330c85023e1SHuy Nguyen 
1331c85023e1SHuy Nguyen 	if (dev->user_td == 2)
1332c85023e1SHuy Nguyen 		err = mlx5_nic_vport_update_local_lb(dev->mdev, true);
1333c85023e1SHuy Nguyen 
1334c85023e1SHuy Nguyen 	mutex_unlock(&dev->lb_mutex);
1335c85023e1SHuy Nguyen 	return err;
1336c85023e1SHuy Nguyen }
1337c85023e1SHuy Nguyen 
1338c85023e1SHuy Nguyen static void mlx5_ib_dealloc_transport_domain(struct mlx5_ib_dev *dev, u32 tdn)
1339c85023e1SHuy Nguyen {
1340c85023e1SHuy Nguyen 	mlx5_core_dealloc_transport_domain(dev->mdev, tdn);
1341c85023e1SHuy Nguyen 
1342c85023e1SHuy Nguyen 	if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
1343c85023e1SHuy Nguyen 	    !MLX5_CAP_GEN(dev->mdev, disable_local_lb))
1344c85023e1SHuy Nguyen 		return;
1345c85023e1SHuy Nguyen 
1346c85023e1SHuy Nguyen 	mutex_lock(&dev->lb_mutex);
1347c85023e1SHuy Nguyen 	dev->user_td--;
1348c85023e1SHuy Nguyen 
1349c85023e1SHuy Nguyen 	if (dev->user_td < 2)
1350c85023e1SHuy Nguyen 		mlx5_nic_vport_update_local_lb(dev->mdev, false);
1351c85023e1SHuy Nguyen 
1352c85023e1SHuy Nguyen 	mutex_unlock(&dev->lb_mutex);
1353c85023e1SHuy Nguyen }
1354c85023e1SHuy Nguyen 
1355e126ba97SEli Cohen static struct ib_ucontext *mlx5_ib_alloc_ucontext(struct ib_device *ibdev,
1356e126ba97SEli Cohen 						  struct ib_udata *udata)
1357e126ba97SEli Cohen {
1358e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1359b368d7cbSMatan Barak 	struct mlx5_ib_alloc_ucontext_req_v2 req = {};
1360b368d7cbSMatan Barak 	struct mlx5_ib_alloc_ucontext_resp resp = {};
1361e126ba97SEli Cohen 	struct mlx5_ib_ucontext *context;
13622f5ff264SEli Cohen 	struct mlx5_bfreg_info *bfregi;
136378c0f98cSEli Cohen 	int ver;
1364e126ba97SEli Cohen 	int err;
1365a168a41cSMajd Dibbiny 	size_t min_req_v2 = offsetof(struct mlx5_ib_alloc_ucontext_req_v2,
1366a168a41cSMajd Dibbiny 				     max_cqe_version);
1367b037c29aSEli Cohen 	bool lib_uar_4k;
1368e126ba97SEli Cohen 
1369e126ba97SEli Cohen 	if (!dev->ib_active)
1370e126ba97SEli Cohen 		return ERR_PTR(-EAGAIN);
1371e126ba97SEli Cohen 
1372e093111dSAmrani, Ram 	if (udata->inlen == sizeof(struct mlx5_ib_alloc_ucontext_req))
137378c0f98cSEli Cohen 		ver = 0;
1374e093111dSAmrani, Ram 	else if (udata->inlen >= min_req_v2)
137578c0f98cSEli Cohen 		ver = 2;
137678c0f98cSEli Cohen 	else
137778c0f98cSEli Cohen 		return ERR_PTR(-EINVAL);
137878c0f98cSEli Cohen 
1379e093111dSAmrani, Ram 	err = ib_copy_from_udata(&req, udata, min(udata->inlen, sizeof(req)));
1380e126ba97SEli Cohen 	if (err)
1381e126ba97SEli Cohen 		return ERR_PTR(err);
1382e126ba97SEli Cohen 
1383b368d7cbSMatan Barak 	if (req.flags)
138478c0f98cSEli Cohen 		return ERR_PTR(-EINVAL);
138578c0f98cSEli Cohen 
1386f72300c5SHaggai Abramovsky 	if (req.comp_mask || req.reserved0 || req.reserved1 || req.reserved2)
1387b368d7cbSMatan Barak 		return ERR_PTR(-EOPNOTSUPP);
1388b368d7cbSMatan Barak 
13892f5ff264SEli Cohen 	req.total_num_bfregs = ALIGN(req.total_num_bfregs,
13902f5ff264SEli Cohen 				    MLX5_NON_FP_BFREGS_PER_UAR);
13912f5ff264SEli Cohen 	if (req.num_low_latency_bfregs > req.total_num_bfregs - 1)
1392e126ba97SEli Cohen 		return ERR_PTR(-EINVAL);
1393e126ba97SEli Cohen 
1394938fe83cSSaeed Mahameed 	resp.qp_tab_size = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp);
13952cc6ad5fSNoa Osherovich 	if (mlx5_core_is_pf(dev->mdev) && MLX5_CAP_GEN(dev->mdev, bf))
1396938fe83cSSaeed Mahameed 		resp.bf_reg_size = 1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size);
1397b47bd6eaSDaniel Jurgens 	resp.cache_line_size = cache_line_size();
1398938fe83cSSaeed Mahameed 	resp.max_sq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq);
1399938fe83cSSaeed Mahameed 	resp.max_rq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq);
1400938fe83cSSaeed Mahameed 	resp.max_send_wqebb = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1401938fe83cSSaeed Mahameed 	resp.max_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1402938fe83cSSaeed Mahameed 	resp.max_srq_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz);
1403f72300c5SHaggai Abramovsky 	resp.cqe_version = min_t(__u8,
1404f72300c5SHaggai Abramovsky 				 (__u8)MLX5_CAP_GEN(dev->mdev, cqe_version),
1405f72300c5SHaggai Abramovsky 				 req.max_cqe_version);
140630aa60b3SEli Cohen 	resp.log_uar_size = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
140730aa60b3SEli Cohen 				MLX5_ADAPTER_PAGE_SHIFT : PAGE_SHIFT;
140830aa60b3SEli Cohen 	resp.num_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
140930aa60b3SEli Cohen 					MLX5_CAP_GEN(dev->mdev, num_of_uars_per_page) : 1;
1410b368d7cbSMatan Barak 	resp.response_length = min(offsetof(typeof(resp), response_length) +
1411b368d7cbSMatan Barak 				   sizeof(resp.response_length), udata->outlen);
1412e126ba97SEli Cohen 
1413e126ba97SEli Cohen 	context = kzalloc(sizeof(*context), GFP_KERNEL);
1414e126ba97SEli Cohen 	if (!context)
1415e126ba97SEli Cohen 		return ERR_PTR(-ENOMEM);
1416e126ba97SEli Cohen 
141730aa60b3SEli Cohen 	lib_uar_4k = req.lib_caps & MLX5_LIB_CAP_4K_UAR;
14182f5ff264SEli Cohen 	bfregi = &context->bfregi;
1419b037c29aSEli Cohen 
1420b037c29aSEli Cohen 	/* updates req->total_num_bfregs */
1421b037c29aSEli Cohen 	err = calc_total_bfregs(dev, lib_uar_4k, &req, &bfregi->num_sys_pages);
1422b037c29aSEli Cohen 	if (err)
1423b037c29aSEli Cohen 		goto out_ctx;
1424b037c29aSEli Cohen 
14252f5ff264SEli Cohen 	mutex_init(&bfregi->lock);
1426b037c29aSEli Cohen 	bfregi->lib_uar_4k = lib_uar_4k;
1427b037c29aSEli Cohen 	bfregi->count = kcalloc(req.total_num_bfregs, sizeof(*bfregi->count),
1428b037c29aSEli Cohen 				GFP_KERNEL);
1429b037c29aSEli Cohen 	if (!bfregi->count) {
1430e126ba97SEli Cohen 		err = -ENOMEM;
1431e126ba97SEli Cohen 		goto out_ctx;
1432e126ba97SEli Cohen 	}
1433e126ba97SEli Cohen 
1434b037c29aSEli Cohen 	bfregi->sys_pages = kcalloc(bfregi->num_sys_pages,
1435b037c29aSEli Cohen 				    sizeof(*bfregi->sys_pages),
1436e126ba97SEli Cohen 				    GFP_KERNEL);
1437b037c29aSEli Cohen 	if (!bfregi->sys_pages) {
1438e126ba97SEli Cohen 		err = -ENOMEM;
1439e126ba97SEli Cohen 		goto out_count;
1440e126ba97SEli Cohen 	}
1441e126ba97SEli Cohen 
1442b037c29aSEli Cohen 	err = allocate_uars(dev, context);
1443b037c29aSEli Cohen 	if (err)
1444b037c29aSEli Cohen 		goto out_sys_pages;
1445b037c29aSEli Cohen 
1446b4cfe447SHaggai Eran #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
1447b4cfe447SHaggai Eran 	context->ibucontext.invalidate_range = &mlx5_ib_invalidate_range;
1448b4cfe447SHaggai Eran #endif
1449b4cfe447SHaggai Eran 
14507d0cc6edSArtemy Kovalyov 	context->upd_xlt_page = __get_free_page(GFP_KERNEL);
14517d0cc6edSArtemy Kovalyov 	if (!context->upd_xlt_page) {
14527d0cc6edSArtemy Kovalyov 		err = -ENOMEM;
14537d0cc6edSArtemy Kovalyov 		goto out_uars;
14547d0cc6edSArtemy Kovalyov 	}
14557d0cc6edSArtemy Kovalyov 	mutex_init(&context->upd_xlt_page_mutex);
14567d0cc6edSArtemy Kovalyov 
1457146d2f1aSmajd@mellanox.com 	if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain)) {
1458c85023e1SHuy Nguyen 		err = mlx5_ib_alloc_transport_domain(dev, &context->tdn);
1459146d2f1aSmajd@mellanox.com 		if (err)
14607d0cc6edSArtemy Kovalyov 			goto out_page;
1461146d2f1aSmajd@mellanox.com 	}
1462146d2f1aSmajd@mellanox.com 
14637c2344c3SMaor Gottlieb 	INIT_LIST_HEAD(&context->vma_private_list);
1464e126ba97SEli Cohen 	INIT_LIST_HEAD(&context->db_page_list);
1465e126ba97SEli Cohen 	mutex_init(&context->db_page_mutex);
1466e126ba97SEli Cohen 
14672f5ff264SEli Cohen 	resp.tot_bfregs = req.total_num_bfregs;
1468938fe83cSSaeed Mahameed 	resp.num_ports = MLX5_CAP_GEN(dev->mdev, num_ports);
1469b368d7cbSMatan Barak 
1470f72300c5SHaggai Abramovsky 	if (field_avail(typeof(resp), cqe_version, udata->outlen))
1471f72300c5SHaggai Abramovsky 		resp.response_length += sizeof(resp.cqe_version);
1472b368d7cbSMatan Barak 
1473402ca536SBodong Wang 	if (field_avail(typeof(resp), cmds_supp_uhw, udata->outlen)) {
14746ad279c5SMoni Shoua 		resp.cmds_supp_uhw |= MLX5_USER_CMDS_SUPP_UHW_QUERY_DEVICE |
14756ad279c5SMoni Shoua 				      MLX5_USER_CMDS_SUPP_UHW_CREATE_AH;
1476402ca536SBodong Wang 		resp.response_length += sizeof(resp.cmds_supp_uhw);
1477402ca536SBodong Wang 	}
1478402ca536SBodong Wang 
147978984898SOr Gerlitz 	if (field_avail(typeof(resp), eth_min_inline, udata->outlen)) {
148078984898SOr Gerlitz 		if (mlx5_ib_port_link_layer(ibdev, 1) == IB_LINK_LAYER_ETHERNET) {
148178984898SOr Gerlitz 			mlx5_query_min_inline(dev->mdev, &resp.eth_min_inline);
148278984898SOr Gerlitz 			resp.eth_min_inline++;
148378984898SOr Gerlitz 		}
148478984898SOr Gerlitz 		resp.response_length += sizeof(resp.eth_min_inline);
148578984898SOr Gerlitz 	}
148678984898SOr Gerlitz 
1487bc5c6eedSNoa Osherovich 	/*
1488bc5c6eedSNoa Osherovich 	 * We don't want to expose information from the PCI bar that is located
1489bc5c6eedSNoa Osherovich 	 * after 4096 bytes, so if the arch only supports larger pages, let's
1490bc5c6eedSNoa Osherovich 	 * pretend we don't support reading the HCA's core clock. This is also
1491bc5c6eedSNoa Osherovich 	 * forced by mmap function.
1492bc5c6eedSNoa Osherovich 	 */
1493de8d6e02SEli Cohen 	if (field_avail(typeof(resp), hca_core_clock_offset, udata->outlen)) {
1494de8d6e02SEli Cohen 		if (PAGE_SIZE <= 4096) {
1495b368d7cbSMatan Barak 			resp.comp_mask |=
1496b368d7cbSMatan Barak 				MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_CORE_CLOCK_OFFSET;
1497b368d7cbSMatan Barak 			resp.hca_core_clock_offset =
1498de8d6e02SEli Cohen 				offsetof(struct mlx5_init_seg, internal_timer_h) % PAGE_SIZE;
1499de8d6e02SEli Cohen 		}
1500f72300c5SHaggai Abramovsky 		resp.response_length += sizeof(resp.hca_core_clock_offset) +
1501402ca536SBodong Wang 					sizeof(resp.reserved2);
1502b368d7cbSMatan Barak 	}
1503b368d7cbSMatan Barak 
150430aa60b3SEli Cohen 	if (field_avail(typeof(resp), log_uar_size, udata->outlen))
150530aa60b3SEli Cohen 		resp.response_length += sizeof(resp.log_uar_size);
150630aa60b3SEli Cohen 
150730aa60b3SEli Cohen 	if (field_avail(typeof(resp), num_uars_per_page, udata->outlen))
150830aa60b3SEli Cohen 		resp.response_length += sizeof(resp.num_uars_per_page);
150930aa60b3SEli Cohen 
1510b368d7cbSMatan Barak 	err = ib_copy_to_udata(udata, &resp, resp.response_length);
1511e126ba97SEli Cohen 	if (err)
1512146d2f1aSmajd@mellanox.com 		goto out_td;
1513e126ba97SEli Cohen 
15142f5ff264SEli Cohen 	bfregi->ver = ver;
15152f5ff264SEli Cohen 	bfregi->num_low_latency_bfregs = req.num_low_latency_bfregs;
1516f72300c5SHaggai Abramovsky 	context->cqe_version = resp.cqe_version;
151730aa60b3SEli Cohen 	context->lib_caps = req.lib_caps;
151830aa60b3SEli Cohen 	print_lib_caps(dev, context->lib_caps);
1519f72300c5SHaggai Abramovsky 
1520e126ba97SEli Cohen 	return &context->ibucontext;
1521e126ba97SEli Cohen 
1522146d2f1aSmajd@mellanox.com out_td:
1523146d2f1aSmajd@mellanox.com 	if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1524c85023e1SHuy Nguyen 		mlx5_ib_dealloc_transport_domain(dev, context->tdn);
1525146d2f1aSmajd@mellanox.com 
15267d0cc6edSArtemy Kovalyov out_page:
15277d0cc6edSArtemy Kovalyov 	free_page(context->upd_xlt_page);
15287d0cc6edSArtemy Kovalyov 
1529e126ba97SEli Cohen out_uars:
1530b037c29aSEli Cohen 	deallocate_uars(dev, context);
1531b037c29aSEli Cohen 
1532b037c29aSEli Cohen out_sys_pages:
1533b037c29aSEli Cohen 	kfree(bfregi->sys_pages);
1534b037c29aSEli Cohen 
1535e126ba97SEli Cohen out_count:
15362f5ff264SEli Cohen 	kfree(bfregi->count);
1537e126ba97SEli Cohen 
1538e126ba97SEli Cohen out_ctx:
1539e126ba97SEli Cohen 	kfree(context);
1540b037c29aSEli Cohen 
1541e126ba97SEli Cohen 	return ERR_PTR(err);
1542e126ba97SEli Cohen }
1543e126ba97SEli Cohen 
1544e126ba97SEli Cohen static int mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
1545e126ba97SEli Cohen {
1546e126ba97SEli Cohen 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1547e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
1548b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1549e126ba97SEli Cohen 
1550b037c29aSEli Cohen 	bfregi = &context->bfregi;
1551146d2f1aSmajd@mellanox.com 	if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1552c85023e1SHuy Nguyen 		mlx5_ib_dealloc_transport_domain(dev, context->tdn);
1553146d2f1aSmajd@mellanox.com 
15547d0cc6edSArtemy Kovalyov 	free_page(context->upd_xlt_page);
1555b037c29aSEli Cohen 	deallocate_uars(dev, context);
1556b037c29aSEli Cohen 	kfree(bfregi->sys_pages);
15572f5ff264SEli Cohen 	kfree(bfregi->count);
1558e126ba97SEli Cohen 	kfree(context);
1559e126ba97SEli Cohen 
1560e126ba97SEli Cohen 	return 0;
1561e126ba97SEli Cohen }
1562e126ba97SEli Cohen 
1563b037c29aSEli Cohen static phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev,
1564b037c29aSEli Cohen 				 struct mlx5_bfreg_info *bfregi,
1565b037c29aSEli Cohen 				 int idx)
1566e126ba97SEli Cohen {
1567b037c29aSEli Cohen 	int fw_uars_per_page;
1568b037c29aSEli Cohen 
1569b037c29aSEli Cohen 	fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ? MLX5_UARS_IN_PAGE : 1;
1570b037c29aSEli Cohen 
1571b037c29aSEli Cohen 	return (pci_resource_start(dev->mdev->pdev, 0) >> PAGE_SHIFT) +
1572b037c29aSEli Cohen 			bfregi->sys_pages[idx] / fw_uars_per_page;
1573e126ba97SEli Cohen }
1574e126ba97SEli Cohen 
1575e126ba97SEli Cohen static int get_command(unsigned long offset)
1576e126ba97SEli Cohen {
1577e126ba97SEli Cohen 	return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK;
1578e126ba97SEli Cohen }
1579e126ba97SEli Cohen 
1580e126ba97SEli Cohen static int get_arg(unsigned long offset)
1581e126ba97SEli Cohen {
1582e126ba97SEli Cohen 	return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1);
1583e126ba97SEli Cohen }
1584e126ba97SEli Cohen 
1585e126ba97SEli Cohen static int get_index(unsigned long offset)
1586e126ba97SEli Cohen {
1587e126ba97SEli Cohen 	return get_arg(offset);
1588e126ba97SEli Cohen }
1589e126ba97SEli Cohen 
15907c2344c3SMaor Gottlieb static void  mlx5_ib_vma_open(struct vm_area_struct *area)
15917c2344c3SMaor Gottlieb {
15927c2344c3SMaor Gottlieb 	/* vma_open is called when a new VMA is created on top of our VMA.  This
15937c2344c3SMaor Gottlieb 	 * is done through either mremap flow or split_vma (usually due to
15947c2344c3SMaor Gottlieb 	 * mlock, madvise, munmap, etc.) We do not support a clone of the VMA,
15957c2344c3SMaor Gottlieb 	 * as this VMA is strongly hardware related.  Therefore we set the
15967c2344c3SMaor Gottlieb 	 * vm_ops of the newly created/cloned VMA to NULL, to prevent it from
15977c2344c3SMaor Gottlieb 	 * calling us again and trying to do incorrect actions.  We assume that
15987c2344c3SMaor Gottlieb 	 * the original VMA size is exactly a single page, and therefore all
15997c2344c3SMaor Gottlieb 	 * "splitting" operation will not happen to it.
16007c2344c3SMaor Gottlieb 	 */
16017c2344c3SMaor Gottlieb 	area->vm_ops = NULL;
16027c2344c3SMaor Gottlieb }
16037c2344c3SMaor Gottlieb 
16047c2344c3SMaor Gottlieb static void  mlx5_ib_vma_close(struct vm_area_struct *area)
16057c2344c3SMaor Gottlieb {
16067c2344c3SMaor Gottlieb 	struct mlx5_ib_vma_private_data *mlx5_ib_vma_priv_data;
16077c2344c3SMaor Gottlieb 
16087c2344c3SMaor Gottlieb 	/* It's guaranteed that all VMAs opened on a FD are closed before the
16097c2344c3SMaor Gottlieb 	 * file itself is closed, therefore no sync is needed with the regular
16107c2344c3SMaor Gottlieb 	 * closing flow. (e.g. mlx5 ib_dealloc_ucontext)
16117c2344c3SMaor Gottlieb 	 * However need a sync with accessing the vma as part of
16127c2344c3SMaor Gottlieb 	 * mlx5_ib_disassociate_ucontext.
16137c2344c3SMaor Gottlieb 	 * The close operation is usually called under mm->mmap_sem except when
16147c2344c3SMaor Gottlieb 	 * process is exiting.
16157c2344c3SMaor Gottlieb 	 * The exiting case is handled explicitly as part of
16167c2344c3SMaor Gottlieb 	 * mlx5_ib_disassociate_ucontext.
16177c2344c3SMaor Gottlieb 	 */
16187c2344c3SMaor Gottlieb 	mlx5_ib_vma_priv_data = (struct mlx5_ib_vma_private_data *)area->vm_private_data;
16197c2344c3SMaor Gottlieb 
16207c2344c3SMaor Gottlieb 	/* setting the vma context pointer to null in the mlx5_ib driver's
16217c2344c3SMaor Gottlieb 	 * private data, to protect a race condition in
16227c2344c3SMaor Gottlieb 	 * mlx5_ib_disassociate_ucontext().
16237c2344c3SMaor Gottlieb 	 */
16247c2344c3SMaor Gottlieb 	mlx5_ib_vma_priv_data->vma = NULL;
16257c2344c3SMaor Gottlieb 	list_del(&mlx5_ib_vma_priv_data->list);
16267c2344c3SMaor Gottlieb 	kfree(mlx5_ib_vma_priv_data);
16277c2344c3SMaor Gottlieb }
16287c2344c3SMaor Gottlieb 
16297c2344c3SMaor Gottlieb static const struct vm_operations_struct mlx5_ib_vm_ops = {
16307c2344c3SMaor Gottlieb 	.open = mlx5_ib_vma_open,
16317c2344c3SMaor Gottlieb 	.close = mlx5_ib_vma_close
16327c2344c3SMaor Gottlieb };
16337c2344c3SMaor Gottlieb 
16347c2344c3SMaor Gottlieb static int mlx5_ib_set_vma_data(struct vm_area_struct *vma,
16357c2344c3SMaor Gottlieb 				struct mlx5_ib_ucontext *ctx)
16367c2344c3SMaor Gottlieb {
16377c2344c3SMaor Gottlieb 	struct mlx5_ib_vma_private_data *vma_prv;
16387c2344c3SMaor Gottlieb 	struct list_head *vma_head = &ctx->vma_private_list;
16397c2344c3SMaor Gottlieb 
16407c2344c3SMaor Gottlieb 	vma_prv = kzalloc(sizeof(*vma_prv), GFP_KERNEL);
16417c2344c3SMaor Gottlieb 	if (!vma_prv)
16427c2344c3SMaor Gottlieb 		return -ENOMEM;
16437c2344c3SMaor Gottlieb 
16447c2344c3SMaor Gottlieb 	vma_prv->vma = vma;
16457c2344c3SMaor Gottlieb 	vma->vm_private_data = vma_prv;
16467c2344c3SMaor Gottlieb 	vma->vm_ops =  &mlx5_ib_vm_ops;
16477c2344c3SMaor Gottlieb 
16487c2344c3SMaor Gottlieb 	list_add(&vma_prv->list, vma_head);
16497c2344c3SMaor Gottlieb 
16507c2344c3SMaor Gottlieb 	return 0;
16517c2344c3SMaor Gottlieb }
16527c2344c3SMaor Gottlieb 
16537c2344c3SMaor Gottlieb static void mlx5_ib_disassociate_ucontext(struct ib_ucontext *ibcontext)
16547c2344c3SMaor Gottlieb {
16557c2344c3SMaor Gottlieb 	int ret;
16567c2344c3SMaor Gottlieb 	struct vm_area_struct *vma;
16577c2344c3SMaor Gottlieb 	struct mlx5_ib_vma_private_data *vma_private, *n;
16587c2344c3SMaor Gottlieb 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
16597c2344c3SMaor Gottlieb 	struct task_struct *owning_process  = NULL;
16607c2344c3SMaor Gottlieb 	struct mm_struct   *owning_mm       = NULL;
16617c2344c3SMaor Gottlieb 
16627c2344c3SMaor Gottlieb 	owning_process = get_pid_task(ibcontext->tgid, PIDTYPE_PID);
16637c2344c3SMaor Gottlieb 	if (!owning_process)
16647c2344c3SMaor Gottlieb 		return;
16657c2344c3SMaor Gottlieb 
16667c2344c3SMaor Gottlieb 	owning_mm = get_task_mm(owning_process);
16677c2344c3SMaor Gottlieb 	if (!owning_mm) {
16687c2344c3SMaor Gottlieb 		pr_info("no mm, disassociate ucontext is pending task termination\n");
16697c2344c3SMaor Gottlieb 		while (1) {
16707c2344c3SMaor Gottlieb 			put_task_struct(owning_process);
16717c2344c3SMaor Gottlieb 			usleep_range(1000, 2000);
16727c2344c3SMaor Gottlieb 			owning_process = get_pid_task(ibcontext->tgid,
16737c2344c3SMaor Gottlieb 						      PIDTYPE_PID);
16747c2344c3SMaor Gottlieb 			if (!owning_process ||
16757c2344c3SMaor Gottlieb 			    owning_process->state == TASK_DEAD) {
16767c2344c3SMaor Gottlieb 				pr_info("disassociate ucontext done, task was terminated\n");
16777c2344c3SMaor Gottlieb 				/* in case task was dead need to release the
16787c2344c3SMaor Gottlieb 				 * task struct.
16797c2344c3SMaor Gottlieb 				 */
16807c2344c3SMaor Gottlieb 				if (owning_process)
16817c2344c3SMaor Gottlieb 					put_task_struct(owning_process);
16827c2344c3SMaor Gottlieb 				return;
16837c2344c3SMaor Gottlieb 			}
16847c2344c3SMaor Gottlieb 		}
16857c2344c3SMaor Gottlieb 	}
16867c2344c3SMaor Gottlieb 
16877c2344c3SMaor Gottlieb 	/* need to protect from a race on closing the vma as part of
16887c2344c3SMaor Gottlieb 	 * mlx5_ib_vma_close.
16897c2344c3SMaor Gottlieb 	 */
1690ecc7d83bSMaor Gottlieb 	down_write(&owning_mm->mmap_sem);
16917c2344c3SMaor Gottlieb 	list_for_each_entry_safe(vma_private, n, &context->vma_private_list,
16927c2344c3SMaor Gottlieb 				 list) {
16937c2344c3SMaor Gottlieb 		vma = vma_private->vma;
16947c2344c3SMaor Gottlieb 		ret = zap_vma_ptes(vma, vma->vm_start,
16957c2344c3SMaor Gottlieb 				   PAGE_SIZE);
16967c2344c3SMaor Gottlieb 		WARN_ONCE(ret, "%s: zap_vma_ptes failed", __func__);
16977c2344c3SMaor Gottlieb 		/* context going to be destroyed, should
16987c2344c3SMaor Gottlieb 		 * not access ops any more.
16997c2344c3SMaor Gottlieb 		 */
170013776612SMaor Gottlieb 		vma->vm_flags &= ~(VM_SHARED | VM_MAYSHARE);
17017c2344c3SMaor Gottlieb 		vma->vm_ops = NULL;
17027c2344c3SMaor Gottlieb 		list_del(&vma_private->list);
17037c2344c3SMaor Gottlieb 		kfree(vma_private);
17047c2344c3SMaor Gottlieb 	}
1705ecc7d83bSMaor Gottlieb 	up_write(&owning_mm->mmap_sem);
17067c2344c3SMaor Gottlieb 	mmput(owning_mm);
17077c2344c3SMaor Gottlieb 	put_task_struct(owning_process);
17087c2344c3SMaor Gottlieb }
17097c2344c3SMaor Gottlieb 
171037aa5c36SGuy Levi static inline char *mmap_cmd2str(enum mlx5_ib_mmap_cmd cmd)
1711e126ba97SEli Cohen {
171237aa5c36SGuy Levi 	switch (cmd) {
171337aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
171437aa5c36SGuy Levi 		return "WC";
1715e126ba97SEli Cohen 	case MLX5_IB_MMAP_REGULAR_PAGE:
171637aa5c36SGuy Levi 		return "best effort WC";
171737aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
171837aa5c36SGuy Levi 		return "NC";
171937aa5c36SGuy Levi 	default:
172037aa5c36SGuy Levi 		return NULL;
172137aa5c36SGuy Levi 	}
172237aa5c36SGuy Levi }
172337aa5c36SGuy Levi 
172437aa5c36SGuy Levi static int uar_mmap(struct mlx5_ib_dev *dev, enum mlx5_ib_mmap_cmd cmd,
17257c2344c3SMaor Gottlieb 		    struct vm_area_struct *vma,
17267c2344c3SMaor Gottlieb 		    struct mlx5_ib_ucontext *context)
172737aa5c36SGuy Levi {
17282f5ff264SEli Cohen 	struct mlx5_bfreg_info *bfregi = &context->bfregi;
172937aa5c36SGuy Levi 	int err;
173037aa5c36SGuy Levi 	unsigned long idx;
173137aa5c36SGuy Levi 	phys_addr_t pfn, pa;
173237aa5c36SGuy Levi 	pgprot_t prot;
1733b037c29aSEli Cohen 	int uars_per_page;
1734b037c29aSEli Cohen 
1735b037c29aSEli Cohen 	if (vma->vm_end - vma->vm_start != PAGE_SIZE)
1736b037c29aSEli Cohen 		return -EINVAL;
1737b037c29aSEli Cohen 
1738b037c29aSEli Cohen 	uars_per_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k);
1739b037c29aSEli Cohen 	idx = get_index(vma->vm_pgoff);
1740b037c29aSEli Cohen 	if (idx % uars_per_page ||
1741b037c29aSEli Cohen 	    idx * uars_per_page >= bfregi->num_sys_pages) {
1742b037c29aSEli Cohen 		mlx5_ib_warn(dev, "invalid uar index %lu\n", idx);
1743b037c29aSEli Cohen 		return -EINVAL;
1744b037c29aSEli Cohen 	}
174537aa5c36SGuy Levi 
174637aa5c36SGuy Levi 	switch (cmd) {
174737aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
174837aa5c36SGuy Levi /* Some architectures don't support WC memory */
174937aa5c36SGuy Levi #if defined(CONFIG_X86)
175037aa5c36SGuy Levi 		if (!pat_enabled())
175137aa5c36SGuy Levi 			return -EPERM;
175237aa5c36SGuy Levi #elif !(defined(CONFIG_PPC) || (defined(CONFIG_ARM) && defined(CONFIG_MMU)))
175337aa5c36SGuy Levi 			return -EPERM;
175437aa5c36SGuy Levi #endif
175537aa5c36SGuy Levi 	/* fall through */
175637aa5c36SGuy Levi 	case MLX5_IB_MMAP_REGULAR_PAGE:
175737aa5c36SGuy Levi 		/* For MLX5_IB_MMAP_REGULAR_PAGE do the best effort to get WC */
175837aa5c36SGuy Levi 		prot = pgprot_writecombine(vma->vm_page_prot);
175937aa5c36SGuy Levi 		break;
176037aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
176137aa5c36SGuy Levi 		prot = pgprot_noncached(vma->vm_page_prot);
176237aa5c36SGuy Levi 		break;
176337aa5c36SGuy Levi 	default:
176437aa5c36SGuy Levi 		return -EINVAL;
176537aa5c36SGuy Levi 	}
176637aa5c36SGuy Levi 
1767b037c29aSEli Cohen 	pfn = uar_index2pfn(dev, bfregi, idx);
176837aa5c36SGuy Levi 	mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn %pa\n", idx, &pfn);
1769e126ba97SEli Cohen 
177037aa5c36SGuy Levi 	vma->vm_page_prot = prot;
177137aa5c36SGuy Levi 	err = io_remap_pfn_range(vma, vma->vm_start, pfn,
177237aa5c36SGuy Levi 				 PAGE_SIZE, vma->vm_page_prot);
177337aa5c36SGuy Levi 	if (err) {
177437aa5c36SGuy Levi 		mlx5_ib_err(dev, "io_remap_pfn_range failed with error=%d, vm_start=0x%lx, pfn=%pa, mmap_cmd=%s\n",
177537aa5c36SGuy Levi 			    err, vma->vm_start, &pfn, mmap_cmd2str(cmd));
1776e126ba97SEli Cohen 		return -EAGAIN;
177737aa5c36SGuy Levi 	}
1778e126ba97SEli Cohen 
177937aa5c36SGuy Levi 	pa = pfn << PAGE_SHIFT;
178037aa5c36SGuy Levi 	mlx5_ib_dbg(dev, "mapped %s at 0x%lx, PA %pa\n", mmap_cmd2str(cmd),
178137aa5c36SGuy Levi 		    vma->vm_start, &pa);
178237aa5c36SGuy Levi 
17837c2344c3SMaor Gottlieb 	return mlx5_ib_set_vma_data(vma, context);
178437aa5c36SGuy Levi }
178537aa5c36SGuy Levi 
178637aa5c36SGuy Levi static int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma)
178737aa5c36SGuy Levi {
178837aa5c36SGuy Levi 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
178937aa5c36SGuy Levi 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
179037aa5c36SGuy Levi 	unsigned long command;
179137aa5c36SGuy Levi 	phys_addr_t pfn;
179237aa5c36SGuy Levi 
179337aa5c36SGuy Levi 	command = get_command(vma->vm_pgoff);
179437aa5c36SGuy Levi 	switch (command) {
179537aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
179637aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
179737aa5c36SGuy Levi 	case MLX5_IB_MMAP_REGULAR_PAGE:
17987c2344c3SMaor Gottlieb 		return uar_mmap(dev, command, vma, context);
1799e126ba97SEli Cohen 
1800e126ba97SEli Cohen 	case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES:
1801e126ba97SEli Cohen 		return -ENOSYS;
1802e126ba97SEli Cohen 
1803d69e3bcfSMatan Barak 	case MLX5_IB_MMAP_CORE_CLOCK:
1804d69e3bcfSMatan Barak 		if (vma->vm_end - vma->vm_start != PAGE_SIZE)
1805d69e3bcfSMatan Barak 			return -EINVAL;
1806d69e3bcfSMatan Barak 
18076cbac1e4SMatan Barak 		if (vma->vm_flags & VM_WRITE)
1808d69e3bcfSMatan Barak 			return -EPERM;
1809d69e3bcfSMatan Barak 
1810d69e3bcfSMatan Barak 		/* Don't expose to user-space information it shouldn't have */
1811d69e3bcfSMatan Barak 		if (PAGE_SIZE > 4096)
1812d69e3bcfSMatan Barak 			return -EOPNOTSUPP;
1813d69e3bcfSMatan Barak 
1814d69e3bcfSMatan Barak 		vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1815d69e3bcfSMatan Barak 		pfn = (dev->mdev->iseg_base +
1816d69e3bcfSMatan Barak 		       offsetof(struct mlx5_init_seg, internal_timer_h)) >>
1817d69e3bcfSMatan Barak 			PAGE_SHIFT;
1818d69e3bcfSMatan Barak 		if (io_remap_pfn_range(vma, vma->vm_start, pfn,
1819d69e3bcfSMatan Barak 				       PAGE_SIZE, vma->vm_page_prot))
1820d69e3bcfSMatan Barak 			return -EAGAIN;
1821d69e3bcfSMatan Barak 
1822d69e3bcfSMatan Barak 		mlx5_ib_dbg(dev, "mapped internal timer at 0x%lx, PA 0x%llx\n",
1823d69e3bcfSMatan Barak 			    vma->vm_start,
1824d69e3bcfSMatan Barak 			    (unsigned long long)pfn << PAGE_SHIFT);
1825d69e3bcfSMatan Barak 		break;
1826d69e3bcfSMatan Barak 
1827e126ba97SEli Cohen 	default:
1828e126ba97SEli Cohen 		return -EINVAL;
1829e126ba97SEli Cohen 	}
1830e126ba97SEli Cohen 
1831e126ba97SEli Cohen 	return 0;
1832e126ba97SEli Cohen }
1833e126ba97SEli Cohen 
1834e126ba97SEli Cohen static struct ib_pd *mlx5_ib_alloc_pd(struct ib_device *ibdev,
1835e126ba97SEli Cohen 				      struct ib_ucontext *context,
1836e126ba97SEli Cohen 				      struct ib_udata *udata)
1837e126ba97SEli Cohen {
1838e126ba97SEli Cohen 	struct mlx5_ib_alloc_pd_resp resp;
1839e126ba97SEli Cohen 	struct mlx5_ib_pd *pd;
1840e126ba97SEli Cohen 	int err;
1841e126ba97SEli Cohen 
1842e126ba97SEli Cohen 	pd = kmalloc(sizeof(*pd), GFP_KERNEL);
1843e126ba97SEli Cohen 	if (!pd)
1844e126ba97SEli Cohen 		return ERR_PTR(-ENOMEM);
1845e126ba97SEli Cohen 
18469603b61dSJack Morgenstein 	err = mlx5_core_alloc_pd(to_mdev(ibdev)->mdev, &pd->pdn);
1847e126ba97SEli Cohen 	if (err) {
1848e126ba97SEli Cohen 		kfree(pd);
1849e126ba97SEli Cohen 		return ERR_PTR(err);
1850e126ba97SEli Cohen 	}
1851e126ba97SEli Cohen 
1852e126ba97SEli Cohen 	if (context) {
1853e126ba97SEli Cohen 		resp.pdn = pd->pdn;
1854e126ba97SEli Cohen 		if (ib_copy_to_udata(udata, &resp, sizeof(resp))) {
18559603b61dSJack Morgenstein 			mlx5_core_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn);
1856e126ba97SEli Cohen 			kfree(pd);
1857e126ba97SEli Cohen 			return ERR_PTR(-EFAULT);
1858e126ba97SEli Cohen 		}
1859e126ba97SEli Cohen 	}
1860e126ba97SEli Cohen 
1861e126ba97SEli Cohen 	return &pd->ibpd;
1862e126ba97SEli Cohen }
1863e126ba97SEli Cohen 
1864e126ba97SEli Cohen static int mlx5_ib_dealloc_pd(struct ib_pd *pd)
1865e126ba97SEli Cohen {
1866e126ba97SEli Cohen 	struct mlx5_ib_dev *mdev = to_mdev(pd->device);
1867e126ba97SEli Cohen 	struct mlx5_ib_pd *mpd = to_mpd(pd);
1868e126ba97SEli Cohen 
18699603b61dSJack Morgenstein 	mlx5_core_dealloc_pd(mdev->mdev, mpd->pdn);
1870e126ba97SEli Cohen 	kfree(mpd);
1871e126ba97SEli Cohen 
1872e126ba97SEli Cohen 	return 0;
1873e126ba97SEli Cohen }
1874e126ba97SEli Cohen 
1875466fa6d2SMaor Gottlieb enum {
1876466fa6d2SMaor Gottlieb 	MATCH_CRITERIA_ENABLE_OUTER_BIT,
1877466fa6d2SMaor Gottlieb 	MATCH_CRITERIA_ENABLE_MISC_BIT,
1878466fa6d2SMaor Gottlieb 	MATCH_CRITERIA_ENABLE_INNER_BIT
1879466fa6d2SMaor Gottlieb };
1880038d2ef8SMaor Gottlieb 
1881466fa6d2SMaor Gottlieb #define HEADER_IS_ZERO(match_criteria, headers)			           \
1882466fa6d2SMaor Gottlieb 	!(memchr_inv(MLX5_ADDR_OF(fte_match_param, match_criteria, headers), \
1883466fa6d2SMaor Gottlieb 		    0, MLX5_FLD_SZ_BYTES(fte_match_param, headers)))       \
1884466fa6d2SMaor Gottlieb 
1885466fa6d2SMaor Gottlieb static u8 get_match_criteria_enable(u32 *match_criteria)
1886466fa6d2SMaor Gottlieb {
1887466fa6d2SMaor Gottlieb 	u8 match_criteria_enable;
1888466fa6d2SMaor Gottlieb 
1889466fa6d2SMaor Gottlieb 	match_criteria_enable =
1890466fa6d2SMaor Gottlieb 		(!HEADER_IS_ZERO(match_criteria, outer_headers)) <<
1891466fa6d2SMaor Gottlieb 		MATCH_CRITERIA_ENABLE_OUTER_BIT;
1892466fa6d2SMaor Gottlieb 	match_criteria_enable |=
1893466fa6d2SMaor Gottlieb 		(!HEADER_IS_ZERO(match_criteria, misc_parameters)) <<
1894466fa6d2SMaor Gottlieb 		MATCH_CRITERIA_ENABLE_MISC_BIT;
1895466fa6d2SMaor Gottlieb 	match_criteria_enable |=
1896466fa6d2SMaor Gottlieb 		(!HEADER_IS_ZERO(match_criteria, inner_headers)) <<
1897466fa6d2SMaor Gottlieb 		MATCH_CRITERIA_ENABLE_INNER_BIT;
1898466fa6d2SMaor Gottlieb 
1899466fa6d2SMaor Gottlieb 	return match_criteria_enable;
1900038d2ef8SMaor Gottlieb }
1901038d2ef8SMaor Gottlieb 
1902ca0d4753SMaor Gottlieb static void set_proto(void *outer_c, void *outer_v, u8 mask, u8 val)
1903ca0d4753SMaor Gottlieb {
1904ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_protocol, mask);
1905ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_protocol, val);
1906ca0d4753SMaor Gottlieb }
1907ca0d4753SMaor Gottlieb 
19082d1e697eSMoses Reuben static void set_flow_label(void *misc_c, void *misc_v, u8 mask, u8 val,
19092d1e697eSMoses Reuben 			   bool inner)
19102d1e697eSMoses Reuben {
19112d1e697eSMoses Reuben 	if (inner) {
19122d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
19132d1e697eSMoses Reuben 			 misc_c, inner_ipv6_flow_label, mask);
19142d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
19152d1e697eSMoses Reuben 			 misc_v, inner_ipv6_flow_label, val);
19162d1e697eSMoses Reuben 	} else {
19172d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
19182d1e697eSMoses Reuben 			 misc_c, outer_ipv6_flow_label, mask);
19192d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
19202d1e697eSMoses Reuben 			 misc_v, outer_ipv6_flow_label, val);
19212d1e697eSMoses Reuben 	}
19222d1e697eSMoses Reuben }
19232d1e697eSMoses Reuben 
1924ca0d4753SMaor Gottlieb static void set_tos(void *outer_c, void *outer_v, u8 mask, u8 val)
1925ca0d4753SMaor Gottlieb {
1926ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_ecn, mask);
1927ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_ecn, val);
1928ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_dscp, mask >> 2);
1929ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_dscp, val >> 2);
1930ca0d4753SMaor Gottlieb }
1931ca0d4753SMaor Gottlieb 
1932c47ac6aeSMaor Gottlieb #define LAST_ETH_FIELD vlan_tag
1933c47ac6aeSMaor Gottlieb #define LAST_IB_FIELD sl
1934ca0d4753SMaor Gottlieb #define LAST_IPV4_FIELD tos
1935466fa6d2SMaor Gottlieb #define LAST_IPV6_FIELD traffic_class
1936c47ac6aeSMaor Gottlieb #define LAST_TCP_UDP_FIELD src_port
1937ffb30d8fSMoses Reuben #define LAST_TUNNEL_FIELD tunnel_id
19382ac693f9SMoses Reuben #define LAST_FLOW_TAG_FIELD tag_id
1939a22ed86cSSlava Shwartsman #define LAST_DROP_FIELD size
1940c47ac6aeSMaor Gottlieb 
1941c47ac6aeSMaor Gottlieb /* Field is the last supported field */
1942c47ac6aeSMaor Gottlieb #define FIELDS_NOT_SUPPORTED(filter, field)\
1943c47ac6aeSMaor Gottlieb 	memchr_inv((void *)&filter.field  +\
1944c47ac6aeSMaor Gottlieb 		   sizeof(filter.field), 0,\
1945c47ac6aeSMaor Gottlieb 		   sizeof(filter) -\
1946c47ac6aeSMaor Gottlieb 		   offsetof(typeof(filter), field) -\
1947c47ac6aeSMaor Gottlieb 		   sizeof(filter.field))
1948c47ac6aeSMaor Gottlieb 
194919cc7524SAriel Levkovich #define IPV4_VERSION 4
195019cc7524SAriel Levkovich #define IPV6_VERSION 6
195119cc7524SAriel Levkovich static int parse_flow_attr(struct mlx5_core_dev *mdev, u32 *match_c,
195219cc7524SAriel Levkovich 			   u32 *match_v, const union ib_flow_spec *ib_spec,
1953a22ed86cSSlava Shwartsman 			   u32 *tag_id, bool *is_drop)
1954038d2ef8SMaor Gottlieb {
1955466fa6d2SMaor Gottlieb 	void *misc_params_c = MLX5_ADDR_OF(fte_match_param, match_c,
1956466fa6d2SMaor Gottlieb 					   misc_parameters);
1957466fa6d2SMaor Gottlieb 	void *misc_params_v = MLX5_ADDR_OF(fte_match_param, match_v,
1958466fa6d2SMaor Gottlieb 					   misc_parameters);
19592d1e697eSMoses Reuben 	void *headers_c;
19602d1e697eSMoses Reuben 	void *headers_v;
196119cc7524SAriel Levkovich 	int match_ipv;
1962466fa6d2SMaor Gottlieb 
19632d1e697eSMoses Reuben 	if (ib_spec->type & IB_FLOW_SPEC_INNER) {
19642d1e697eSMoses Reuben 		headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
19652d1e697eSMoses Reuben 					 inner_headers);
19662d1e697eSMoses Reuben 		headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
19672d1e697eSMoses Reuben 					 inner_headers);
196819cc7524SAriel Levkovich 		match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
196919cc7524SAriel Levkovich 					ft_field_support.inner_ip_version);
19702d1e697eSMoses Reuben 	} else {
19712d1e697eSMoses Reuben 		headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
19722d1e697eSMoses Reuben 					 outer_headers);
19732d1e697eSMoses Reuben 		headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
19742d1e697eSMoses Reuben 					 outer_headers);
197519cc7524SAriel Levkovich 		match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
197619cc7524SAriel Levkovich 					ft_field_support.outer_ip_version);
19772d1e697eSMoses Reuben 	}
19782d1e697eSMoses Reuben 
19792d1e697eSMoses Reuben 	switch (ib_spec->type & ~IB_FLOW_SPEC_INNER) {
1980038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_ETH:
1981c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->eth.mask, LAST_ETH_FIELD))
19821ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
1983038d2ef8SMaor Gottlieb 
19842d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
1985038d2ef8SMaor Gottlieb 					     dmac_47_16),
1986038d2ef8SMaor Gottlieb 				ib_spec->eth.mask.dst_mac);
19872d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
1988038d2ef8SMaor Gottlieb 					     dmac_47_16),
1989038d2ef8SMaor Gottlieb 				ib_spec->eth.val.dst_mac);
1990038d2ef8SMaor Gottlieb 
19912d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
1992ee3da804SMaor Gottlieb 					     smac_47_16),
1993ee3da804SMaor Gottlieb 				ib_spec->eth.mask.src_mac);
19942d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
1995ee3da804SMaor Gottlieb 					     smac_47_16),
1996ee3da804SMaor Gottlieb 				ib_spec->eth.val.src_mac);
1997ee3da804SMaor Gottlieb 
1998038d2ef8SMaor Gottlieb 		if (ib_spec->eth.mask.vlan_tag) {
19992d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
200010543365SMohamad Haj Yahia 				 cvlan_tag, 1);
20012d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
200210543365SMohamad Haj Yahia 				 cvlan_tag, 1);
2003038d2ef8SMaor Gottlieb 
20042d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2005038d2ef8SMaor Gottlieb 				 first_vid, ntohs(ib_spec->eth.mask.vlan_tag));
20062d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2007038d2ef8SMaor Gottlieb 				 first_vid, ntohs(ib_spec->eth.val.vlan_tag));
2008038d2ef8SMaor Gottlieb 
20092d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2010038d2ef8SMaor Gottlieb 				 first_cfi,
2011038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.mask.vlan_tag) >> 12);
20122d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2013038d2ef8SMaor Gottlieb 				 first_cfi,
2014038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.val.vlan_tag) >> 12);
2015038d2ef8SMaor Gottlieb 
20162d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2017038d2ef8SMaor Gottlieb 				 first_prio,
2018038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.mask.vlan_tag) >> 13);
20192d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2020038d2ef8SMaor Gottlieb 				 first_prio,
2021038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.val.vlan_tag) >> 13);
2022038d2ef8SMaor Gottlieb 		}
20232d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2024038d2ef8SMaor Gottlieb 			 ethertype, ntohs(ib_spec->eth.mask.ether_type));
20252d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2026038d2ef8SMaor Gottlieb 			 ethertype, ntohs(ib_spec->eth.val.ether_type));
2027038d2ef8SMaor Gottlieb 		break;
2028038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_IPV4:
2029c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->ipv4.mask, LAST_IPV4_FIELD))
20301ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2031038d2ef8SMaor Gottlieb 
203219cc7524SAriel Levkovich 		if (match_ipv) {
203319cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
203419cc7524SAriel Levkovich 				 ip_version, 0xf);
203519cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
203619cc7524SAriel Levkovich 				 ip_version, IPV4_VERSION);
203719cc7524SAriel Levkovich 		} else {
20382d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2039038d2ef8SMaor Gottlieb 				 ethertype, 0xffff);
20402d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2041038d2ef8SMaor Gottlieb 				 ethertype, ETH_P_IP);
204219cc7524SAriel Levkovich 		}
2043038d2ef8SMaor Gottlieb 
20442d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2045038d2ef8SMaor Gottlieb 				    src_ipv4_src_ipv6.ipv4_layout.ipv4),
2046038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.mask.src_ip,
2047038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.mask.src_ip));
20482d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2049038d2ef8SMaor Gottlieb 				    src_ipv4_src_ipv6.ipv4_layout.ipv4),
2050038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.val.src_ip,
2051038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.val.src_ip));
20522d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2053038d2ef8SMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2054038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.mask.dst_ip,
2055038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.mask.dst_ip));
20562d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2057038d2ef8SMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2058038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.val.dst_ip,
2059038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.val.dst_ip));
2060ca0d4753SMaor Gottlieb 
20612d1e697eSMoses Reuben 		set_tos(headers_c, headers_v,
2062ca0d4753SMaor Gottlieb 			ib_spec->ipv4.mask.tos, ib_spec->ipv4.val.tos);
2063ca0d4753SMaor Gottlieb 
20642d1e697eSMoses Reuben 		set_proto(headers_c, headers_v,
2065ca0d4753SMaor Gottlieb 			  ib_spec->ipv4.mask.proto, ib_spec->ipv4.val.proto);
2066038d2ef8SMaor Gottlieb 		break;
2067026bae0cSMaor Gottlieb 	case IB_FLOW_SPEC_IPV6:
2068c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->ipv6.mask, LAST_IPV6_FIELD))
20691ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2070026bae0cSMaor Gottlieb 
207119cc7524SAriel Levkovich 		if (match_ipv) {
207219cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
207319cc7524SAriel Levkovich 				 ip_version, 0xf);
207419cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
207519cc7524SAriel Levkovich 				 ip_version, IPV6_VERSION);
207619cc7524SAriel Levkovich 		} else {
20772d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2078026bae0cSMaor Gottlieb 				 ethertype, 0xffff);
20792d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2080026bae0cSMaor Gottlieb 				 ethertype, ETH_P_IPV6);
208119cc7524SAriel Levkovich 		}
2082026bae0cSMaor Gottlieb 
20832d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2084026bae0cSMaor Gottlieb 				    src_ipv4_src_ipv6.ipv6_layout.ipv6),
2085026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.mask.src_ip,
2086026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.mask.src_ip));
20872d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2088026bae0cSMaor Gottlieb 				    src_ipv4_src_ipv6.ipv6_layout.ipv6),
2089026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.val.src_ip,
2090026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.val.src_ip));
20912d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2092026bae0cSMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2093026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.mask.dst_ip,
2094026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.mask.dst_ip));
20952d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2096026bae0cSMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2097026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.val.dst_ip,
2098026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.val.dst_ip));
2099466fa6d2SMaor Gottlieb 
21002d1e697eSMoses Reuben 		set_tos(headers_c, headers_v,
2101466fa6d2SMaor Gottlieb 			ib_spec->ipv6.mask.traffic_class,
2102466fa6d2SMaor Gottlieb 			ib_spec->ipv6.val.traffic_class);
2103466fa6d2SMaor Gottlieb 
21042d1e697eSMoses Reuben 		set_proto(headers_c, headers_v,
2105466fa6d2SMaor Gottlieb 			  ib_spec->ipv6.mask.next_hdr,
2106466fa6d2SMaor Gottlieb 			  ib_spec->ipv6.val.next_hdr);
2107466fa6d2SMaor Gottlieb 
21082d1e697eSMoses Reuben 		set_flow_label(misc_params_c, misc_params_v,
21092d1e697eSMoses Reuben 			       ntohl(ib_spec->ipv6.mask.flow_label),
21102d1e697eSMoses Reuben 			       ntohl(ib_spec->ipv6.val.flow_label),
21112d1e697eSMoses Reuben 			       ib_spec->type & IB_FLOW_SPEC_INNER);
21122d1e697eSMoses Reuben 
2113026bae0cSMaor Gottlieb 		break;
2114038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_TCP:
2115c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
2116c47ac6aeSMaor Gottlieb 					 LAST_TCP_UDP_FIELD))
21171ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2118038d2ef8SMaor Gottlieb 
21192d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
2120038d2ef8SMaor Gottlieb 			 0xff);
21212d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
2122038d2ef8SMaor Gottlieb 			 IPPROTO_TCP);
2123038d2ef8SMaor Gottlieb 
21242d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_sport,
2125038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.src_port));
21262d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_sport,
2127038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.src_port));
2128038d2ef8SMaor Gottlieb 
21292d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_dport,
2130038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.dst_port));
21312d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_dport,
2132038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.dst_port));
2133038d2ef8SMaor Gottlieb 		break;
2134038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_UDP:
2135c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
2136c47ac6aeSMaor Gottlieb 					 LAST_TCP_UDP_FIELD))
21371ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2138038d2ef8SMaor Gottlieb 
21392d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
2140038d2ef8SMaor Gottlieb 			 0xff);
21412d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
2142038d2ef8SMaor Gottlieb 			 IPPROTO_UDP);
2143038d2ef8SMaor Gottlieb 
21442d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_sport,
2145038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.src_port));
21462d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_sport,
2147038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.src_port));
2148038d2ef8SMaor Gottlieb 
21492d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_dport,
2150038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.dst_port));
21512d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport,
2152038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.dst_port));
2153038d2ef8SMaor Gottlieb 		break;
2154ffb30d8fSMoses Reuben 	case IB_FLOW_SPEC_VXLAN_TUNNEL:
2155ffb30d8fSMoses Reuben 		if (FIELDS_NOT_SUPPORTED(ib_spec->tunnel.mask,
2156ffb30d8fSMoses Reuben 					 LAST_TUNNEL_FIELD))
21571ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2158ffb30d8fSMoses Reuben 
2159ffb30d8fSMoses Reuben 		MLX5_SET(fte_match_set_misc, misc_params_c, vxlan_vni,
2160ffb30d8fSMoses Reuben 			 ntohl(ib_spec->tunnel.mask.tunnel_id));
2161ffb30d8fSMoses Reuben 		MLX5_SET(fte_match_set_misc, misc_params_v, vxlan_vni,
2162ffb30d8fSMoses Reuben 			 ntohl(ib_spec->tunnel.val.tunnel_id));
2163ffb30d8fSMoses Reuben 		break;
21642ac693f9SMoses Reuben 	case IB_FLOW_SPEC_ACTION_TAG:
21652ac693f9SMoses Reuben 		if (FIELDS_NOT_SUPPORTED(ib_spec->flow_tag,
21662ac693f9SMoses Reuben 					 LAST_FLOW_TAG_FIELD))
21672ac693f9SMoses Reuben 			return -EOPNOTSUPP;
21682ac693f9SMoses Reuben 		if (ib_spec->flow_tag.tag_id >= BIT(24))
21692ac693f9SMoses Reuben 			return -EINVAL;
21702ac693f9SMoses Reuben 
21712ac693f9SMoses Reuben 		*tag_id = ib_spec->flow_tag.tag_id;
21722ac693f9SMoses Reuben 		break;
2173a22ed86cSSlava Shwartsman 	case IB_FLOW_SPEC_ACTION_DROP:
2174a22ed86cSSlava Shwartsman 		if (FIELDS_NOT_SUPPORTED(ib_spec->drop,
2175a22ed86cSSlava Shwartsman 					 LAST_DROP_FIELD))
2176a22ed86cSSlava Shwartsman 			return -EOPNOTSUPP;
2177a22ed86cSSlava Shwartsman 		*is_drop = true;
2178a22ed86cSSlava Shwartsman 		break;
2179038d2ef8SMaor Gottlieb 	default:
2180038d2ef8SMaor Gottlieb 		return -EINVAL;
2181038d2ef8SMaor Gottlieb 	}
2182038d2ef8SMaor Gottlieb 
2183038d2ef8SMaor Gottlieb 	return 0;
2184038d2ef8SMaor Gottlieb }
2185038d2ef8SMaor Gottlieb 
2186038d2ef8SMaor Gottlieb /* If a flow could catch both multicast and unicast packets,
2187038d2ef8SMaor Gottlieb  * it won't fall into the multicast flow steering table and this rule
2188038d2ef8SMaor Gottlieb  * could steal other multicast packets.
2189038d2ef8SMaor Gottlieb  */
2190a550ddfcSYishai Hadas static bool flow_is_multicast_only(const struct ib_flow_attr *ib_attr)
2191038d2ef8SMaor Gottlieb {
219281e30880SYishai Hadas 	union ib_flow_spec *flow_spec;
2193038d2ef8SMaor Gottlieb 
2194038d2ef8SMaor Gottlieb 	if (ib_attr->type != IB_FLOW_ATTR_NORMAL ||
2195038d2ef8SMaor Gottlieb 	    ib_attr->num_of_specs < 1)
2196038d2ef8SMaor Gottlieb 		return false;
2197038d2ef8SMaor Gottlieb 
219881e30880SYishai Hadas 	flow_spec = (union ib_flow_spec *)(ib_attr + 1);
219981e30880SYishai Hadas 	if (flow_spec->type == IB_FLOW_SPEC_IPV4) {
220081e30880SYishai Hadas 		struct ib_flow_spec_ipv4 *ipv4_spec;
2201038d2ef8SMaor Gottlieb 
220281e30880SYishai Hadas 		ipv4_spec = (struct ib_flow_spec_ipv4 *)flow_spec;
220381e30880SYishai Hadas 		if (ipv4_is_multicast(ipv4_spec->val.dst_ip))
220481e30880SYishai Hadas 			return true;
220581e30880SYishai Hadas 
220681e30880SYishai Hadas 		return false;
220781e30880SYishai Hadas 	}
220881e30880SYishai Hadas 
220981e30880SYishai Hadas 	if (flow_spec->type == IB_FLOW_SPEC_ETH) {
221081e30880SYishai Hadas 		struct ib_flow_spec_eth *eth_spec;
221181e30880SYishai Hadas 
221281e30880SYishai Hadas 		eth_spec = (struct ib_flow_spec_eth *)flow_spec;
2213038d2ef8SMaor Gottlieb 		return is_multicast_ether_addr(eth_spec->mask.dst_mac) &&
2214038d2ef8SMaor Gottlieb 		       is_multicast_ether_addr(eth_spec->val.dst_mac);
2215038d2ef8SMaor Gottlieb 	}
2216038d2ef8SMaor Gottlieb 
221781e30880SYishai Hadas 	return false;
221881e30880SYishai Hadas }
221981e30880SYishai Hadas 
222019cc7524SAriel Levkovich static bool is_valid_ethertype(struct mlx5_core_dev *mdev,
222119cc7524SAriel Levkovich 			       const struct ib_flow_attr *flow_attr,
22220f750966SAriel Levkovich 			       bool check_inner)
2223038d2ef8SMaor Gottlieb {
2224038d2ef8SMaor Gottlieb 	union ib_flow_spec *ib_spec = (union ib_flow_spec *)(flow_attr + 1);
222519cc7524SAriel Levkovich 	int match_ipv = check_inner ?
222619cc7524SAriel Levkovich 			MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
222719cc7524SAriel Levkovich 					ft_field_support.inner_ip_version) :
222819cc7524SAriel Levkovich 			MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
222919cc7524SAriel Levkovich 					ft_field_support.outer_ip_version);
22300f750966SAriel Levkovich 	int inner_bit = check_inner ? IB_FLOW_SPEC_INNER : 0;
22310f750966SAriel Levkovich 	bool ipv4_spec_valid, ipv6_spec_valid;
22320f750966SAriel Levkovich 	unsigned int ip_spec_type = 0;
22330f750966SAriel Levkovich 	bool has_ethertype = false;
2234038d2ef8SMaor Gottlieb 	unsigned int spec_index;
22350f750966SAriel Levkovich 	bool mask_valid = true;
22360f750966SAriel Levkovich 	u16 eth_type = 0;
22370f750966SAriel Levkovich 	bool type_valid;
2238038d2ef8SMaor Gottlieb 
2239038d2ef8SMaor Gottlieb 	/* Validate that ethertype is correct */
2240038d2ef8SMaor Gottlieb 	for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
22410f750966SAriel Levkovich 		if ((ib_spec->type == (IB_FLOW_SPEC_ETH | inner_bit)) &&
2242038d2ef8SMaor Gottlieb 		    ib_spec->eth.mask.ether_type) {
22430f750966SAriel Levkovich 			mask_valid = (ib_spec->eth.mask.ether_type ==
22440f750966SAriel Levkovich 				      htons(0xffff));
22450f750966SAriel Levkovich 			has_ethertype = true;
22460f750966SAriel Levkovich 			eth_type = ntohs(ib_spec->eth.val.ether_type);
22470f750966SAriel Levkovich 		} else if ((ib_spec->type == (IB_FLOW_SPEC_IPV4 | inner_bit)) ||
22480f750966SAriel Levkovich 			   (ib_spec->type == (IB_FLOW_SPEC_IPV6 | inner_bit))) {
22490f750966SAriel Levkovich 			ip_spec_type = ib_spec->type;
2250038d2ef8SMaor Gottlieb 		}
2251038d2ef8SMaor Gottlieb 		ib_spec = (void *)ib_spec + ib_spec->size;
2252038d2ef8SMaor Gottlieb 	}
22530f750966SAriel Levkovich 
22540f750966SAriel Levkovich 	type_valid = (!has_ethertype) || (!ip_spec_type);
22550f750966SAriel Levkovich 	if (!type_valid && mask_valid) {
22560f750966SAriel Levkovich 		ipv4_spec_valid = (eth_type == ETH_P_IP) &&
22570f750966SAriel Levkovich 			(ip_spec_type == (IB_FLOW_SPEC_IPV4 | inner_bit));
22580f750966SAriel Levkovich 		ipv6_spec_valid = (eth_type == ETH_P_IPV6) &&
22590f750966SAriel Levkovich 			(ip_spec_type == (IB_FLOW_SPEC_IPV6 | inner_bit));
226019cc7524SAriel Levkovich 
226119cc7524SAriel Levkovich 		type_valid = (ipv4_spec_valid) || (ipv6_spec_valid) ||
226219cc7524SAriel Levkovich 			     (((eth_type == ETH_P_MPLS_UC) ||
226319cc7524SAriel Levkovich 			       (eth_type == ETH_P_MPLS_MC)) && match_ipv);
22640f750966SAriel Levkovich 	}
22650f750966SAriel Levkovich 
22660f750966SAriel Levkovich 	return type_valid;
22670f750966SAriel Levkovich }
22680f750966SAriel Levkovich 
226919cc7524SAriel Levkovich static bool is_valid_attr(struct mlx5_core_dev *mdev,
227019cc7524SAriel Levkovich 			  const struct ib_flow_attr *flow_attr)
22710f750966SAriel Levkovich {
227219cc7524SAriel Levkovich 	return is_valid_ethertype(mdev, flow_attr, false) &&
227319cc7524SAriel Levkovich 	       is_valid_ethertype(mdev, flow_attr, true);
2274038d2ef8SMaor Gottlieb }
2275038d2ef8SMaor Gottlieb 
2276038d2ef8SMaor Gottlieb static void put_flow_table(struct mlx5_ib_dev *dev,
2277038d2ef8SMaor Gottlieb 			   struct mlx5_ib_flow_prio *prio, bool ft_added)
2278038d2ef8SMaor Gottlieb {
2279038d2ef8SMaor Gottlieb 	prio->refcount -= !!ft_added;
2280038d2ef8SMaor Gottlieb 	if (!prio->refcount) {
2281038d2ef8SMaor Gottlieb 		mlx5_destroy_flow_table(prio->flow_table);
2282038d2ef8SMaor Gottlieb 		prio->flow_table = NULL;
2283038d2ef8SMaor Gottlieb 	}
2284038d2ef8SMaor Gottlieb }
2285038d2ef8SMaor Gottlieb 
2286038d2ef8SMaor Gottlieb static int mlx5_ib_destroy_flow(struct ib_flow *flow_id)
2287038d2ef8SMaor Gottlieb {
2288038d2ef8SMaor Gottlieb 	struct mlx5_ib_dev *dev = to_mdev(flow_id->qp->device);
2289038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = container_of(flow_id,
2290038d2ef8SMaor Gottlieb 							  struct mlx5_ib_flow_handler,
2291038d2ef8SMaor Gottlieb 							  ibflow);
2292038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *iter, *tmp;
2293038d2ef8SMaor Gottlieb 
2294038d2ef8SMaor Gottlieb 	mutex_lock(&dev->flow_db.lock);
2295038d2ef8SMaor Gottlieb 
2296038d2ef8SMaor Gottlieb 	list_for_each_entry_safe(iter, tmp, &handler->list, list) {
229774491de9SMark Bloch 		mlx5_del_flow_rules(iter->rule);
2298cc0e5d42SMaor Gottlieb 		put_flow_table(dev, iter->prio, true);
2299038d2ef8SMaor Gottlieb 		list_del(&iter->list);
2300038d2ef8SMaor Gottlieb 		kfree(iter);
2301038d2ef8SMaor Gottlieb 	}
2302038d2ef8SMaor Gottlieb 
230374491de9SMark Bloch 	mlx5_del_flow_rules(handler->rule);
23045497adc6SMaor Gottlieb 	put_flow_table(dev, handler->prio, true);
2305038d2ef8SMaor Gottlieb 	mutex_unlock(&dev->flow_db.lock);
2306038d2ef8SMaor Gottlieb 
2307038d2ef8SMaor Gottlieb 	kfree(handler);
2308038d2ef8SMaor Gottlieb 
2309038d2ef8SMaor Gottlieb 	return 0;
2310038d2ef8SMaor Gottlieb }
2311038d2ef8SMaor Gottlieb 
231235d19011SMaor Gottlieb static int ib_prio_to_core_prio(unsigned int priority, bool dont_trap)
231335d19011SMaor Gottlieb {
231435d19011SMaor Gottlieb 	priority *= 2;
231535d19011SMaor Gottlieb 	if (!dont_trap)
231635d19011SMaor Gottlieb 		priority++;
231735d19011SMaor Gottlieb 	return priority;
231835d19011SMaor Gottlieb }
231935d19011SMaor Gottlieb 
2320cc0e5d42SMaor Gottlieb enum flow_table_type {
2321cc0e5d42SMaor Gottlieb 	MLX5_IB_FT_RX,
2322cc0e5d42SMaor Gottlieb 	MLX5_IB_FT_TX
2323cc0e5d42SMaor Gottlieb };
2324cc0e5d42SMaor Gottlieb 
232500b7c2abSMaor Gottlieb #define MLX5_FS_MAX_TYPES	 6
232600b7c2abSMaor Gottlieb #define MLX5_FS_MAX_ENTRIES	 BIT(16)
2327038d2ef8SMaor Gottlieb static struct mlx5_ib_flow_prio *get_flow_table(struct mlx5_ib_dev *dev,
2328cc0e5d42SMaor Gottlieb 						struct ib_flow_attr *flow_attr,
2329cc0e5d42SMaor Gottlieb 						enum flow_table_type ft_type)
2330038d2ef8SMaor Gottlieb {
233135d19011SMaor Gottlieb 	bool dont_trap = flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP;
2332038d2ef8SMaor Gottlieb 	struct mlx5_flow_namespace *ns = NULL;
2333038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_prio *prio;
2334038d2ef8SMaor Gottlieb 	struct mlx5_flow_table *ft;
2335dac388efSMaor Gottlieb 	int max_table_size;
2336038d2ef8SMaor Gottlieb 	int num_entries;
2337038d2ef8SMaor Gottlieb 	int num_groups;
2338038d2ef8SMaor Gottlieb 	int priority;
2339038d2ef8SMaor Gottlieb 	int err = 0;
2340038d2ef8SMaor Gottlieb 
2341dac388efSMaor Gottlieb 	max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
2342dac388efSMaor Gottlieb 						       log_max_ft_size));
2343038d2ef8SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
234435d19011SMaor Gottlieb 		if (flow_is_multicast_only(flow_attr) &&
234535d19011SMaor Gottlieb 		    !dont_trap)
2346038d2ef8SMaor Gottlieb 			priority = MLX5_IB_FLOW_MCAST_PRIO;
2347038d2ef8SMaor Gottlieb 		else
234835d19011SMaor Gottlieb 			priority = ib_prio_to_core_prio(flow_attr->priority,
234935d19011SMaor Gottlieb 							dont_trap);
2350038d2ef8SMaor Gottlieb 		ns = mlx5_get_flow_namespace(dev->mdev,
2351038d2ef8SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_BYPASS);
2352038d2ef8SMaor Gottlieb 		num_entries = MLX5_FS_MAX_ENTRIES;
2353038d2ef8SMaor Gottlieb 		num_groups = MLX5_FS_MAX_TYPES;
2354038d2ef8SMaor Gottlieb 		prio = &dev->flow_db.prios[priority];
2355038d2ef8SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
2356038d2ef8SMaor Gottlieb 		   flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
2357038d2ef8SMaor Gottlieb 		ns = mlx5_get_flow_namespace(dev->mdev,
2358038d2ef8SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_LEFTOVERS);
2359038d2ef8SMaor Gottlieb 		build_leftovers_ft_param(&priority,
2360038d2ef8SMaor Gottlieb 					 &num_entries,
2361038d2ef8SMaor Gottlieb 					 &num_groups);
2362038d2ef8SMaor Gottlieb 		prio = &dev->flow_db.prios[MLX5_IB_FLOW_LEFTOVERS_PRIO];
2363cc0e5d42SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
2364cc0e5d42SMaor Gottlieb 		if (!MLX5_CAP_FLOWTABLE(dev->mdev,
2365cc0e5d42SMaor Gottlieb 					allow_sniffer_and_nic_rx_shared_tir))
2366cc0e5d42SMaor Gottlieb 			return ERR_PTR(-ENOTSUPP);
2367cc0e5d42SMaor Gottlieb 
2368cc0e5d42SMaor Gottlieb 		ns = mlx5_get_flow_namespace(dev->mdev, ft_type == MLX5_IB_FT_RX ?
2369cc0e5d42SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_SNIFFER_RX :
2370cc0e5d42SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_SNIFFER_TX);
2371cc0e5d42SMaor Gottlieb 
2372cc0e5d42SMaor Gottlieb 		prio = &dev->flow_db.sniffer[ft_type];
2373cc0e5d42SMaor Gottlieb 		priority = 0;
2374cc0e5d42SMaor Gottlieb 		num_entries = 1;
2375cc0e5d42SMaor Gottlieb 		num_groups = 1;
2376038d2ef8SMaor Gottlieb 	}
2377038d2ef8SMaor Gottlieb 
2378038d2ef8SMaor Gottlieb 	if (!ns)
2379038d2ef8SMaor Gottlieb 		return ERR_PTR(-ENOTSUPP);
2380038d2ef8SMaor Gottlieb 
2381dac388efSMaor Gottlieb 	if (num_entries > max_table_size)
2382dac388efSMaor Gottlieb 		return ERR_PTR(-ENOMEM);
2383dac388efSMaor Gottlieb 
2384038d2ef8SMaor Gottlieb 	ft = prio->flow_table;
2385038d2ef8SMaor Gottlieb 	if (!ft) {
2386038d2ef8SMaor Gottlieb 		ft = mlx5_create_auto_grouped_flow_table(ns, priority,
2387038d2ef8SMaor Gottlieb 							 num_entries,
2388d63cd286SMaor Gottlieb 							 num_groups,
2389c9f1b073SHadar Hen Zion 							 0, 0);
2390038d2ef8SMaor Gottlieb 
2391038d2ef8SMaor Gottlieb 		if (!IS_ERR(ft)) {
2392038d2ef8SMaor Gottlieb 			prio->refcount = 0;
2393038d2ef8SMaor Gottlieb 			prio->flow_table = ft;
2394038d2ef8SMaor Gottlieb 		} else {
2395038d2ef8SMaor Gottlieb 			err = PTR_ERR(ft);
2396038d2ef8SMaor Gottlieb 		}
2397038d2ef8SMaor Gottlieb 	}
2398038d2ef8SMaor Gottlieb 
2399038d2ef8SMaor Gottlieb 	return err ? ERR_PTR(err) : prio;
2400038d2ef8SMaor Gottlieb }
2401038d2ef8SMaor Gottlieb 
2402a550ddfcSYishai Hadas static void set_underlay_qp(struct mlx5_ib_dev *dev,
2403a550ddfcSYishai Hadas 			    struct mlx5_flow_spec *spec,
2404a550ddfcSYishai Hadas 			    u32 underlay_qpn)
2405a550ddfcSYishai Hadas {
2406a550ddfcSYishai Hadas 	void *misc_params_c = MLX5_ADDR_OF(fte_match_param,
2407a550ddfcSYishai Hadas 					   spec->match_criteria,
2408a550ddfcSYishai Hadas 					   misc_parameters);
2409a550ddfcSYishai Hadas 	void *misc_params_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
2410a550ddfcSYishai Hadas 					   misc_parameters);
2411a550ddfcSYishai Hadas 
2412a550ddfcSYishai Hadas 	if (underlay_qpn &&
2413a550ddfcSYishai Hadas 	    MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
2414a550ddfcSYishai Hadas 				      ft_field_support.bth_dst_qp)) {
2415a550ddfcSYishai Hadas 		MLX5_SET(fte_match_set_misc,
2416a550ddfcSYishai Hadas 			 misc_params_v, bth_dst_qp, underlay_qpn);
2417a550ddfcSYishai Hadas 		MLX5_SET(fte_match_set_misc,
2418a550ddfcSYishai Hadas 			 misc_params_c, bth_dst_qp, 0xffffff);
2419a550ddfcSYishai Hadas 	}
2420a550ddfcSYishai Hadas }
2421a550ddfcSYishai Hadas 
2422a550ddfcSYishai Hadas static struct mlx5_ib_flow_handler *_create_flow_rule(struct mlx5_ib_dev *dev,
2423038d2ef8SMaor Gottlieb 						      struct mlx5_ib_flow_prio *ft_prio,
2424dd063d0eSMaor Gottlieb 						      const struct ib_flow_attr *flow_attr,
2425a550ddfcSYishai Hadas 						      struct mlx5_flow_destination *dst,
2426a550ddfcSYishai Hadas 						      u32 underlay_qpn)
2427038d2ef8SMaor Gottlieb {
2428038d2ef8SMaor Gottlieb 	struct mlx5_flow_table	*ft = ft_prio->flow_table;
2429038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler;
243066958ed9SHadar Hen Zion 	struct mlx5_flow_act flow_act = {0};
2431c5bb1730SMaor Gottlieb 	struct mlx5_flow_spec *spec;
2432a22ed86cSSlava Shwartsman 	struct mlx5_flow_destination *rule_dst = dst;
2433dd063d0eSMaor Gottlieb 	const void *ib_flow = (const void *)flow_attr + sizeof(*flow_attr);
2434038d2ef8SMaor Gottlieb 	unsigned int spec_index;
24352ac693f9SMoses Reuben 	u32 flow_tag = MLX5_FS_DEFAULT_FLOW_TAG;
2436a22ed86cSSlava Shwartsman 	bool is_drop = false;
2437038d2ef8SMaor Gottlieb 	int err = 0;
2438a22ed86cSSlava Shwartsman 	int dest_num = 1;
2439038d2ef8SMaor Gottlieb 
244019cc7524SAriel Levkovich 	if (!is_valid_attr(dev->mdev, flow_attr))
2441038d2ef8SMaor Gottlieb 		return ERR_PTR(-EINVAL);
2442038d2ef8SMaor Gottlieb 
24431b9a07eeSLeon Romanovsky 	spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
2444038d2ef8SMaor Gottlieb 	handler = kzalloc(sizeof(*handler), GFP_KERNEL);
2445c5bb1730SMaor Gottlieb 	if (!handler || !spec) {
2446038d2ef8SMaor Gottlieb 		err = -ENOMEM;
2447038d2ef8SMaor Gottlieb 		goto free;
2448038d2ef8SMaor Gottlieb 	}
2449038d2ef8SMaor Gottlieb 
2450038d2ef8SMaor Gottlieb 	INIT_LIST_HEAD(&handler->list);
2451038d2ef8SMaor Gottlieb 
2452038d2ef8SMaor Gottlieb 	for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
245319cc7524SAriel Levkovich 		err = parse_flow_attr(dev->mdev, spec->match_criteria,
2454a22ed86cSSlava Shwartsman 				      spec->match_value,
2455a22ed86cSSlava Shwartsman 				      ib_flow, &flow_tag, &is_drop);
2456038d2ef8SMaor Gottlieb 		if (err < 0)
2457038d2ef8SMaor Gottlieb 			goto free;
2458038d2ef8SMaor Gottlieb 
2459038d2ef8SMaor Gottlieb 		ib_flow += ((union ib_flow_spec *)ib_flow)->size;
2460038d2ef8SMaor Gottlieb 	}
2461038d2ef8SMaor Gottlieb 
2462a550ddfcSYishai Hadas 	if (!flow_is_multicast_only(flow_attr))
2463a550ddfcSYishai Hadas 		set_underlay_qp(dev, spec, underlay_qpn);
2464a550ddfcSYishai Hadas 
2465466fa6d2SMaor Gottlieb 	spec->match_criteria_enable = get_match_criteria_enable(spec->match_criteria);
2466a22ed86cSSlava Shwartsman 	if (is_drop) {
2467a22ed86cSSlava Shwartsman 		flow_act.action = MLX5_FLOW_CONTEXT_ACTION_DROP;
2468a22ed86cSSlava Shwartsman 		rule_dst = NULL;
2469a22ed86cSSlava Shwartsman 		dest_num = 0;
2470a22ed86cSSlava Shwartsman 	} else {
247166958ed9SHadar Hen Zion 		flow_act.action = dst ? MLX5_FLOW_CONTEXT_ACTION_FWD_DEST :
247235d19011SMaor Gottlieb 		    MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO;
2473a22ed86cSSlava Shwartsman 	}
24742ac693f9SMoses Reuben 
24752ac693f9SMoses Reuben 	if (flow_tag != MLX5_FS_DEFAULT_FLOW_TAG &&
24762ac693f9SMoses Reuben 	    (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
24772ac693f9SMoses Reuben 	     flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT)) {
24782ac693f9SMoses Reuben 		mlx5_ib_warn(dev, "Flow tag %u and attribute type %x isn't allowed in leftovers\n",
24792ac693f9SMoses Reuben 			     flow_tag, flow_attr->type);
24802ac693f9SMoses Reuben 		err = -EINVAL;
24812ac693f9SMoses Reuben 		goto free;
24822ac693f9SMoses Reuben 	}
24832ac693f9SMoses Reuben 	flow_act.flow_tag = flow_tag;
248474491de9SMark Bloch 	handler->rule = mlx5_add_flow_rules(ft, spec,
248566958ed9SHadar Hen Zion 					    &flow_act,
2486a22ed86cSSlava Shwartsman 					    rule_dst, dest_num);
2487038d2ef8SMaor Gottlieb 
2488038d2ef8SMaor Gottlieb 	if (IS_ERR(handler->rule)) {
2489038d2ef8SMaor Gottlieb 		err = PTR_ERR(handler->rule);
2490038d2ef8SMaor Gottlieb 		goto free;
2491038d2ef8SMaor Gottlieb 	}
2492038d2ef8SMaor Gottlieb 
2493d9d4980aSMaor Gottlieb 	ft_prio->refcount++;
24945497adc6SMaor Gottlieb 	handler->prio = ft_prio;
2495038d2ef8SMaor Gottlieb 
2496038d2ef8SMaor Gottlieb 	ft_prio->flow_table = ft;
2497038d2ef8SMaor Gottlieb free:
2498038d2ef8SMaor Gottlieb 	if (err)
2499038d2ef8SMaor Gottlieb 		kfree(handler);
2500c5bb1730SMaor Gottlieb 	kvfree(spec);
2501038d2ef8SMaor Gottlieb 	return err ? ERR_PTR(err) : handler;
2502038d2ef8SMaor Gottlieb }
2503038d2ef8SMaor Gottlieb 
2504a550ddfcSYishai Hadas static struct mlx5_ib_flow_handler *create_flow_rule(struct mlx5_ib_dev *dev,
2505a550ddfcSYishai Hadas 						     struct mlx5_ib_flow_prio *ft_prio,
2506a550ddfcSYishai Hadas 						     const struct ib_flow_attr *flow_attr,
2507a550ddfcSYishai Hadas 						     struct mlx5_flow_destination *dst)
2508a550ddfcSYishai Hadas {
2509a550ddfcSYishai Hadas 	return _create_flow_rule(dev, ft_prio, flow_attr, dst, 0);
2510a550ddfcSYishai Hadas }
2511a550ddfcSYishai Hadas 
251235d19011SMaor Gottlieb static struct mlx5_ib_flow_handler *create_dont_trap_rule(struct mlx5_ib_dev *dev,
251335d19011SMaor Gottlieb 							  struct mlx5_ib_flow_prio *ft_prio,
251435d19011SMaor Gottlieb 							  struct ib_flow_attr *flow_attr,
251535d19011SMaor Gottlieb 							  struct mlx5_flow_destination *dst)
251635d19011SMaor Gottlieb {
251735d19011SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_dst = NULL;
251835d19011SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
251935d19011SMaor Gottlieb 
252035d19011SMaor Gottlieb 	handler = create_flow_rule(dev, ft_prio, flow_attr, NULL);
252135d19011SMaor Gottlieb 	if (!IS_ERR(handler)) {
252235d19011SMaor Gottlieb 		handler_dst = create_flow_rule(dev, ft_prio,
252335d19011SMaor Gottlieb 					       flow_attr, dst);
252435d19011SMaor Gottlieb 		if (IS_ERR(handler_dst)) {
252574491de9SMark Bloch 			mlx5_del_flow_rules(handler->rule);
2526d9d4980aSMaor Gottlieb 			ft_prio->refcount--;
252735d19011SMaor Gottlieb 			kfree(handler);
252835d19011SMaor Gottlieb 			handler = handler_dst;
252935d19011SMaor Gottlieb 		} else {
253035d19011SMaor Gottlieb 			list_add(&handler_dst->list, &handler->list);
253135d19011SMaor Gottlieb 		}
253235d19011SMaor Gottlieb 	}
253335d19011SMaor Gottlieb 
253435d19011SMaor Gottlieb 	return handler;
253535d19011SMaor Gottlieb }
2536038d2ef8SMaor Gottlieb enum {
2537038d2ef8SMaor Gottlieb 	LEFTOVERS_MC,
2538038d2ef8SMaor Gottlieb 	LEFTOVERS_UC,
2539038d2ef8SMaor Gottlieb };
2540038d2ef8SMaor Gottlieb 
2541038d2ef8SMaor Gottlieb static struct mlx5_ib_flow_handler *create_leftovers_rule(struct mlx5_ib_dev *dev,
2542038d2ef8SMaor Gottlieb 							  struct mlx5_ib_flow_prio *ft_prio,
2543038d2ef8SMaor Gottlieb 							  struct ib_flow_attr *flow_attr,
2544038d2ef8SMaor Gottlieb 							  struct mlx5_flow_destination *dst)
2545038d2ef8SMaor Gottlieb {
2546038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_ucast = NULL;
2547038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
2548038d2ef8SMaor Gottlieb 
2549038d2ef8SMaor Gottlieb 	static struct {
2550038d2ef8SMaor Gottlieb 		struct ib_flow_attr	flow_attr;
2551038d2ef8SMaor Gottlieb 		struct ib_flow_spec_eth eth_flow;
2552038d2ef8SMaor Gottlieb 	} leftovers_specs[] = {
2553038d2ef8SMaor Gottlieb 		[LEFTOVERS_MC] = {
2554038d2ef8SMaor Gottlieb 			.flow_attr = {
2555038d2ef8SMaor Gottlieb 				.num_of_specs = 1,
2556038d2ef8SMaor Gottlieb 				.size = sizeof(leftovers_specs[0])
2557038d2ef8SMaor Gottlieb 			},
2558038d2ef8SMaor Gottlieb 			.eth_flow = {
2559038d2ef8SMaor Gottlieb 				.type = IB_FLOW_SPEC_ETH,
2560038d2ef8SMaor Gottlieb 				.size = sizeof(struct ib_flow_spec_eth),
2561038d2ef8SMaor Gottlieb 				.mask = {.dst_mac = {0x1} },
2562038d2ef8SMaor Gottlieb 				.val =  {.dst_mac = {0x1} }
2563038d2ef8SMaor Gottlieb 			}
2564038d2ef8SMaor Gottlieb 		},
2565038d2ef8SMaor Gottlieb 		[LEFTOVERS_UC] = {
2566038d2ef8SMaor Gottlieb 			.flow_attr = {
2567038d2ef8SMaor Gottlieb 				.num_of_specs = 1,
2568038d2ef8SMaor Gottlieb 				.size = sizeof(leftovers_specs[0])
2569038d2ef8SMaor Gottlieb 			},
2570038d2ef8SMaor Gottlieb 			.eth_flow = {
2571038d2ef8SMaor Gottlieb 				.type = IB_FLOW_SPEC_ETH,
2572038d2ef8SMaor Gottlieb 				.size = sizeof(struct ib_flow_spec_eth),
2573038d2ef8SMaor Gottlieb 				.mask = {.dst_mac = {0x1} },
2574038d2ef8SMaor Gottlieb 				.val = {.dst_mac = {} }
2575038d2ef8SMaor Gottlieb 			}
2576038d2ef8SMaor Gottlieb 		}
2577038d2ef8SMaor Gottlieb 	};
2578038d2ef8SMaor Gottlieb 
2579038d2ef8SMaor Gottlieb 	handler = create_flow_rule(dev, ft_prio,
2580038d2ef8SMaor Gottlieb 				   &leftovers_specs[LEFTOVERS_MC].flow_attr,
2581038d2ef8SMaor Gottlieb 				   dst);
2582038d2ef8SMaor Gottlieb 	if (!IS_ERR(handler) &&
2583038d2ef8SMaor Gottlieb 	    flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT) {
2584038d2ef8SMaor Gottlieb 		handler_ucast = create_flow_rule(dev, ft_prio,
2585038d2ef8SMaor Gottlieb 						 &leftovers_specs[LEFTOVERS_UC].flow_attr,
2586038d2ef8SMaor Gottlieb 						 dst);
2587038d2ef8SMaor Gottlieb 		if (IS_ERR(handler_ucast)) {
258874491de9SMark Bloch 			mlx5_del_flow_rules(handler->rule);
2589d9d4980aSMaor Gottlieb 			ft_prio->refcount--;
2590038d2ef8SMaor Gottlieb 			kfree(handler);
2591038d2ef8SMaor Gottlieb 			handler = handler_ucast;
2592038d2ef8SMaor Gottlieb 		} else {
2593038d2ef8SMaor Gottlieb 			list_add(&handler_ucast->list, &handler->list);
2594038d2ef8SMaor Gottlieb 		}
2595038d2ef8SMaor Gottlieb 	}
2596038d2ef8SMaor Gottlieb 
2597038d2ef8SMaor Gottlieb 	return handler;
2598038d2ef8SMaor Gottlieb }
2599038d2ef8SMaor Gottlieb 
2600cc0e5d42SMaor Gottlieb static struct mlx5_ib_flow_handler *create_sniffer_rule(struct mlx5_ib_dev *dev,
2601cc0e5d42SMaor Gottlieb 							struct mlx5_ib_flow_prio *ft_rx,
2602cc0e5d42SMaor Gottlieb 							struct mlx5_ib_flow_prio *ft_tx,
2603cc0e5d42SMaor Gottlieb 							struct mlx5_flow_destination *dst)
2604cc0e5d42SMaor Gottlieb {
2605cc0e5d42SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_rx;
2606cc0e5d42SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_tx;
2607cc0e5d42SMaor Gottlieb 	int err;
2608cc0e5d42SMaor Gottlieb 	static const struct ib_flow_attr flow_attr  = {
2609cc0e5d42SMaor Gottlieb 		.num_of_specs = 0,
2610cc0e5d42SMaor Gottlieb 		.size = sizeof(flow_attr)
2611cc0e5d42SMaor Gottlieb 	};
2612cc0e5d42SMaor Gottlieb 
2613cc0e5d42SMaor Gottlieb 	handler_rx = create_flow_rule(dev, ft_rx, &flow_attr, dst);
2614cc0e5d42SMaor Gottlieb 	if (IS_ERR(handler_rx)) {
2615cc0e5d42SMaor Gottlieb 		err = PTR_ERR(handler_rx);
2616cc0e5d42SMaor Gottlieb 		goto err;
2617cc0e5d42SMaor Gottlieb 	}
2618cc0e5d42SMaor Gottlieb 
2619cc0e5d42SMaor Gottlieb 	handler_tx = create_flow_rule(dev, ft_tx, &flow_attr, dst);
2620cc0e5d42SMaor Gottlieb 	if (IS_ERR(handler_tx)) {
2621cc0e5d42SMaor Gottlieb 		err = PTR_ERR(handler_tx);
2622cc0e5d42SMaor Gottlieb 		goto err_tx;
2623cc0e5d42SMaor Gottlieb 	}
2624cc0e5d42SMaor Gottlieb 
2625cc0e5d42SMaor Gottlieb 	list_add(&handler_tx->list, &handler_rx->list);
2626cc0e5d42SMaor Gottlieb 
2627cc0e5d42SMaor Gottlieb 	return handler_rx;
2628cc0e5d42SMaor Gottlieb 
2629cc0e5d42SMaor Gottlieb err_tx:
263074491de9SMark Bloch 	mlx5_del_flow_rules(handler_rx->rule);
2631cc0e5d42SMaor Gottlieb 	ft_rx->refcount--;
2632cc0e5d42SMaor Gottlieb 	kfree(handler_rx);
2633cc0e5d42SMaor Gottlieb err:
2634cc0e5d42SMaor Gottlieb 	return ERR_PTR(err);
2635cc0e5d42SMaor Gottlieb }
2636cc0e5d42SMaor Gottlieb 
2637038d2ef8SMaor Gottlieb static struct ib_flow *mlx5_ib_create_flow(struct ib_qp *qp,
2638038d2ef8SMaor Gottlieb 					   struct ib_flow_attr *flow_attr,
2639038d2ef8SMaor Gottlieb 					   int domain)
2640038d2ef8SMaor Gottlieb {
2641038d2ef8SMaor Gottlieb 	struct mlx5_ib_dev *dev = to_mdev(qp->device);
2642d9f88e5aSYishai Hadas 	struct mlx5_ib_qp *mqp = to_mqp(qp);
2643038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
2644038d2ef8SMaor Gottlieb 	struct mlx5_flow_destination *dst = NULL;
2645cc0e5d42SMaor Gottlieb 	struct mlx5_ib_flow_prio *ft_prio_tx = NULL;
2646038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_prio *ft_prio;
2647038d2ef8SMaor Gottlieb 	int err;
2648a550ddfcSYishai Hadas 	int underlay_qpn;
2649038d2ef8SMaor Gottlieb 
2650038d2ef8SMaor Gottlieb 	if (flow_attr->priority > MLX5_IB_FLOW_LAST_PRIO)
2651dac388efSMaor Gottlieb 		return ERR_PTR(-ENOMEM);
2652038d2ef8SMaor Gottlieb 
2653038d2ef8SMaor Gottlieb 	if (domain != IB_FLOW_DOMAIN_USER ||
2654038d2ef8SMaor Gottlieb 	    flow_attr->port > MLX5_CAP_GEN(dev->mdev, num_ports) ||
265535d19011SMaor Gottlieb 	    (flow_attr->flags & ~IB_FLOW_ATTR_FLAGS_DONT_TRAP))
2656038d2ef8SMaor Gottlieb 		return ERR_PTR(-EINVAL);
2657038d2ef8SMaor Gottlieb 
2658038d2ef8SMaor Gottlieb 	dst = kzalloc(sizeof(*dst), GFP_KERNEL);
2659038d2ef8SMaor Gottlieb 	if (!dst)
2660038d2ef8SMaor Gottlieb 		return ERR_PTR(-ENOMEM);
2661038d2ef8SMaor Gottlieb 
2662038d2ef8SMaor Gottlieb 	mutex_lock(&dev->flow_db.lock);
2663038d2ef8SMaor Gottlieb 
2664cc0e5d42SMaor Gottlieb 	ft_prio = get_flow_table(dev, flow_attr, MLX5_IB_FT_RX);
2665038d2ef8SMaor Gottlieb 	if (IS_ERR(ft_prio)) {
2666038d2ef8SMaor Gottlieb 		err = PTR_ERR(ft_prio);
2667038d2ef8SMaor Gottlieb 		goto unlock;
2668038d2ef8SMaor Gottlieb 	}
2669cc0e5d42SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
2670cc0e5d42SMaor Gottlieb 		ft_prio_tx = get_flow_table(dev, flow_attr, MLX5_IB_FT_TX);
2671cc0e5d42SMaor Gottlieb 		if (IS_ERR(ft_prio_tx)) {
2672cc0e5d42SMaor Gottlieb 			err = PTR_ERR(ft_prio_tx);
2673cc0e5d42SMaor Gottlieb 			ft_prio_tx = NULL;
2674cc0e5d42SMaor Gottlieb 			goto destroy_ft;
2675cc0e5d42SMaor Gottlieb 		}
2676cc0e5d42SMaor Gottlieb 	}
2677038d2ef8SMaor Gottlieb 
2678038d2ef8SMaor Gottlieb 	dst->type = MLX5_FLOW_DESTINATION_TYPE_TIR;
2679d9f88e5aSYishai Hadas 	if (mqp->flags & MLX5_IB_QP_RSS)
2680d9f88e5aSYishai Hadas 		dst->tir_num = mqp->rss_qp.tirn;
2681d9f88e5aSYishai Hadas 	else
2682d9f88e5aSYishai Hadas 		dst->tir_num = mqp->raw_packet_qp.rq.tirn;
2683038d2ef8SMaor Gottlieb 
2684038d2ef8SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
268535d19011SMaor Gottlieb 		if (flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP)  {
268635d19011SMaor Gottlieb 			handler = create_dont_trap_rule(dev, ft_prio,
268735d19011SMaor Gottlieb 							flow_attr, dst);
268835d19011SMaor Gottlieb 		} else {
2689a550ddfcSYishai Hadas 			underlay_qpn = (mqp->flags & MLX5_IB_QP_UNDERLAY) ?
2690a550ddfcSYishai Hadas 					mqp->underlay_qpn : 0;
2691a550ddfcSYishai Hadas 			handler = _create_flow_rule(dev, ft_prio, flow_attr,
2692a550ddfcSYishai Hadas 						    dst, underlay_qpn);
269335d19011SMaor Gottlieb 		}
2694038d2ef8SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
2695038d2ef8SMaor Gottlieb 		   flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
2696038d2ef8SMaor Gottlieb 		handler = create_leftovers_rule(dev, ft_prio, flow_attr,
2697038d2ef8SMaor Gottlieb 						dst);
2698cc0e5d42SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
2699cc0e5d42SMaor Gottlieb 		handler = create_sniffer_rule(dev, ft_prio, ft_prio_tx, dst);
2700038d2ef8SMaor Gottlieb 	} else {
2701038d2ef8SMaor Gottlieb 		err = -EINVAL;
2702038d2ef8SMaor Gottlieb 		goto destroy_ft;
2703038d2ef8SMaor Gottlieb 	}
2704038d2ef8SMaor Gottlieb 
2705038d2ef8SMaor Gottlieb 	if (IS_ERR(handler)) {
2706038d2ef8SMaor Gottlieb 		err = PTR_ERR(handler);
2707038d2ef8SMaor Gottlieb 		handler = NULL;
2708038d2ef8SMaor Gottlieb 		goto destroy_ft;
2709038d2ef8SMaor Gottlieb 	}
2710038d2ef8SMaor Gottlieb 
2711038d2ef8SMaor Gottlieb 	mutex_unlock(&dev->flow_db.lock);
2712038d2ef8SMaor Gottlieb 	kfree(dst);
2713038d2ef8SMaor Gottlieb 
2714038d2ef8SMaor Gottlieb 	return &handler->ibflow;
2715038d2ef8SMaor Gottlieb 
2716038d2ef8SMaor Gottlieb destroy_ft:
2717038d2ef8SMaor Gottlieb 	put_flow_table(dev, ft_prio, false);
2718cc0e5d42SMaor Gottlieb 	if (ft_prio_tx)
2719cc0e5d42SMaor Gottlieb 		put_flow_table(dev, ft_prio_tx, false);
2720038d2ef8SMaor Gottlieb unlock:
2721038d2ef8SMaor Gottlieb 	mutex_unlock(&dev->flow_db.lock);
2722038d2ef8SMaor Gottlieb 	kfree(dst);
2723038d2ef8SMaor Gottlieb 	kfree(handler);
2724038d2ef8SMaor Gottlieb 	return ERR_PTR(err);
2725038d2ef8SMaor Gottlieb }
2726038d2ef8SMaor Gottlieb 
2727e126ba97SEli Cohen static int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
2728e126ba97SEli Cohen {
2729e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
273081e30880SYishai Hadas 	struct mlx5_ib_qp *mqp = to_mqp(ibqp);
2731e126ba97SEli Cohen 	int err;
2732e126ba97SEli Cohen 
273381e30880SYishai Hadas 	if (mqp->flags & MLX5_IB_QP_UNDERLAY) {
273481e30880SYishai Hadas 		mlx5_ib_dbg(dev, "Attaching a multi cast group to underlay QP is not supported\n");
273581e30880SYishai Hadas 		return -EOPNOTSUPP;
273681e30880SYishai Hadas 	}
273781e30880SYishai Hadas 
27389603b61dSJack Morgenstein 	err = mlx5_core_attach_mcg(dev->mdev, gid, ibqp->qp_num);
2739e126ba97SEli Cohen 	if (err)
2740e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n",
2741e126ba97SEli Cohen 			     ibqp->qp_num, gid->raw);
2742e126ba97SEli Cohen 
2743e126ba97SEli Cohen 	return err;
2744e126ba97SEli Cohen }
2745e126ba97SEli Cohen 
2746e126ba97SEli Cohen static int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
2747e126ba97SEli Cohen {
2748e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
2749e126ba97SEli Cohen 	int err;
2750e126ba97SEli Cohen 
27519603b61dSJack Morgenstein 	err = mlx5_core_detach_mcg(dev->mdev, gid, ibqp->qp_num);
2752e126ba97SEli Cohen 	if (err)
2753e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n",
2754e126ba97SEli Cohen 			     ibqp->qp_num, gid->raw);
2755e126ba97SEli Cohen 
2756e126ba97SEli Cohen 	return err;
2757e126ba97SEli Cohen }
2758e126ba97SEli Cohen 
2759e126ba97SEli Cohen static int init_node_data(struct mlx5_ib_dev *dev)
2760e126ba97SEli Cohen {
27611b5daf11SMajd Dibbiny 	int err;
2762e126ba97SEli Cohen 
27631b5daf11SMajd Dibbiny 	err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc);
2764e126ba97SEli Cohen 	if (err)
2765e126ba97SEli Cohen 		return err;
27661b5daf11SMajd Dibbiny 
27671b5daf11SMajd Dibbiny 	dev->mdev->rev_id = dev->mdev->pdev->revision;
27681b5daf11SMajd Dibbiny 
27691b5daf11SMajd Dibbiny 	return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid);
2770e126ba97SEli Cohen }
2771e126ba97SEli Cohen 
2772e126ba97SEli Cohen static ssize_t show_fw_pages(struct device *device, struct device_attribute *attr,
2773e126ba97SEli Cohen 			     char *buf)
2774e126ba97SEli Cohen {
2775e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
2776e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
2777e126ba97SEli Cohen 
27789603b61dSJack Morgenstein 	return sprintf(buf, "%d\n", dev->mdev->priv.fw_pages);
2779e126ba97SEli Cohen }
2780e126ba97SEli Cohen 
2781e126ba97SEli Cohen static ssize_t show_reg_pages(struct device *device,
2782e126ba97SEli Cohen 			      struct device_attribute *attr, char *buf)
2783e126ba97SEli Cohen {
2784e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
2785e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
2786e126ba97SEli Cohen 
27876aec21f6SHaggai Eran 	return sprintf(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages));
2788e126ba97SEli Cohen }
2789e126ba97SEli Cohen 
2790e126ba97SEli Cohen static ssize_t show_hca(struct device *device, struct device_attribute *attr,
2791e126ba97SEli Cohen 			char *buf)
2792e126ba97SEli Cohen {
2793e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
2794e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
27959603b61dSJack Morgenstein 	return sprintf(buf, "MT%d\n", dev->mdev->pdev->device);
2796e126ba97SEli Cohen }
2797e126ba97SEli Cohen 
2798e126ba97SEli Cohen static ssize_t show_rev(struct device *device, struct device_attribute *attr,
2799e126ba97SEli Cohen 			char *buf)
2800e126ba97SEli Cohen {
2801e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
2802e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
28039603b61dSJack Morgenstein 	return sprintf(buf, "%x\n", dev->mdev->rev_id);
2804e126ba97SEli Cohen }
2805e126ba97SEli Cohen 
2806e126ba97SEli Cohen static ssize_t show_board(struct device *device, struct device_attribute *attr,
2807e126ba97SEli Cohen 			  char *buf)
2808e126ba97SEli Cohen {
2809e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
2810e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
2811e126ba97SEli Cohen 	return sprintf(buf, "%.*s\n", MLX5_BOARD_ID_LEN,
28129603b61dSJack Morgenstein 		       dev->mdev->board_id);
2813e126ba97SEli Cohen }
2814e126ba97SEli Cohen 
2815e126ba97SEli Cohen static DEVICE_ATTR(hw_rev,   S_IRUGO, show_rev,    NULL);
2816e126ba97SEli Cohen static DEVICE_ATTR(hca_type, S_IRUGO, show_hca,    NULL);
2817e126ba97SEli Cohen static DEVICE_ATTR(board_id, S_IRUGO, show_board,  NULL);
2818e126ba97SEli Cohen static DEVICE_ATTR(fw_pages, S_IRUGO, show_fw_pages, NULL);
2819e126ba97SEli Cohen static DEVICE_ATTR(reg_pages, S_IRUGO, show_reg_pages, NULL);
2820e126ba97SEli Cohen 
2821e126ba97SEli Cohen static struct device_attribute *mlx5_class_attributes[] = {
2822e126ba97SEli Cohen 	&dev_attr_hw_rev,
2823e126ba97SEli Cohen 	&dev_attr_hca_type,
2824e126ba97SEli Cohen 	&dev_attr_board_id,
2825e126ba97SEli Cohen 	&dev_attr_fw_pages,
2826e126ba97SEli Cohen 	&dev_attr_reg_pages,
2827e126ba97SEli Cohen };
2828e126ba97SEli Cohen 
28297722f47eSHaggai Eran static void pkey_change_handler(struct work_struct *work)
28307722f47eSHaggai Eran {
28317722f47eSHaggai Eran 	struct mlx5_ib_port_resources *ports =
28327722f47eSHaggai Eran 		container_of(work, struct mlx5_ib_port_resources,
28337722f47eSHaggai Eran 			     pkey_change_work);
28347722f47eSHaggai Eran 
28357722f47eSHaggai Eran 	mutex_lock(&ports->devr->mutex);
28367722f47eSHaggai Eran 	mlx5_ib_gsi_pkey_change(ports->gsi);
28377722f47eSHaggai Eran 	mutex_unlock(&ports->devr->mutex);
28387722f47eSHaggai Eran }
28397722f47eSHaggai Eran 
284089ea94a7SMaor Gottlieb static void mlx5_ib_handle_internal_error(struct mlx5_ib_dev *ibdev)
284189ea94a7SMaor Gottlieb {
284289ea94a7SMaor Gottlieb 	struct mlx5_ib_qp *mqp;
284389ea94a7SMaor Gottlieb 	struct mlx5_ib_cq *send_mcq, *recv_mcq;
284489ea94a7SMaor Gottlieb 	struct mlx5_core_cq *mcq;
284589ea94a7SMaor Gottlieb 	struct list_head cq_armed_list;
284689ea94a7SMaor Gottlieb 	unsigned long flags_qp;
284789ea94a7SMaor Gottlieb 	unsigned long flags_cq;
284889ea94a7SMaor Gottlieb 	unsigned long flags;
284989ea94a7SMaor Gottlieb 
285089ea94a7SMaor Gottlieb 	INIT_LIST_HEAD(&cq_armed_list);
285189ea94a7SMaor Gottlieb 
285289ea94a7SMaor Gottlieb 	/* Go over qp list reside on that ibdev, sync with create/destroy qp.*/
285389ea94a7SMaor Gottlieb 	spin_lock_irqsave(&ibdev->reset_flow_resource_lock, flags);
285489ea94a7SMaor Gottlieb 	list_for_each_entry(mqp, &ibdev->qp_list, qps_list) {
285589ea94a7SMaor Gottlieb 		spin_lock_irqsave(&mqp->sq.lock, flags_qp);
285689ea94a7SMaor Gottlieb 		if (mqp->sq.tail != mqp->sq.head) {
285789ea94a7SMaor Gottlieb 			send_mcq = to_mcq(mqp->ibqp.send_cq);
285889ea94a7SMaor Gottlieb 			spin_lock_irqsave(&send_mcq->lock, flags_cq);
285989ea94a7SMaor Gottlieb 			if (send_mcq->mcq.comp &&
286089ea94a7SMaor Gottlieb 			    mqp->ibqp.send_cq->comp_handler) {
286189ea94a7SMaor Gottlieb 				if (!send_mcq->mcq.reset_notify_added) {
286289ea94a7SMaor Gottlieb 					send_mcq->mcq.reset_notify_added = 1;
286389ea94a7SMaor Gottlieb 					list_add_tail(&send_mcq->mcq.reset_notify,
286489ea94a7SMaor Gottlieb 						      &cq_armed_list);
286589ea94a7SMaor Gottlieb 				}
286689ea94a7SMaor Gottlieb 			}
286789ea94a7SMaor Gottlieb 			spin_unlock_irqrestore(&send_mcq->lock, flags_cq);
286889ea94a7SMaor Gottlieb 		}
286989ea94a7SMaor Gottlieb 		spin_unlock_irqrestore(&mqp->sq.lock, flags_qp);
287089ea94a7SMaor Gottlieb 		spin_lock_irqsave(&mqp->rq.lock, flags_qp);
287189ea94a7SMaor Gottlieb 		/* no handling is needed for SRQ */
287289ea94a7SMaor Gottlieb 		if (!mqp->ibqp.srq) {
287389ea94a7SMaor Gottlieb 			if (mqp->rq.tail != mqp->rq.head) {
287489ea94a7SMaor Gottlieb 				recv_mcq = to_mcq(mqp->ibqp.recv_cq);
287589ea94a7SMaor Gottlieb 				spin_lock_irqsave(&recv_mcq->lock, flags_cq);
287689ea94a7SMaor Gottlieb 				if (recv_mcq->mcq.comp &&
287789ea94a7SMaor Gottlieb 				    mqp->ibqp.recv_cq->comp_handler) {
287889ea94a7SMaor Gottlieb 					if (!recv_mcq->mcq.reset_notify_added) {
287989ea94a7SMaor Gottlieb 						recv_mcq->mcq.reset_notify_added = 1;
288089ea94a7SMaor Gottlieb 						list_add_tail(&recv_mcq->mcq.reset_notify,
288189ea94a7SMaor Gottlieb 							      &cq_armed_list);
288289ea94a7SMaor Gottlieb 					}
288389ea94a7SMaor Gottlieb 				}
288489ea94a7SMaor Gottlieb 				spin_unlock_irqrestore(&recv_mcq->lock,
288589ea94a7SMaor Gottlieb 						       flags_cq);
288689ea94a7SMaor Gottlieb 			}
288789ea94a7SMaor Gottlieb 		}
288889ea94a7SMaor Gottlieb 		spin_unlock_irqrestore(&mqp->rq.lock, flags_qp);
288989ea94a7SMaor Gottlieb 	}
289089ea94a7SMaor Gottlieb 	/*At that point all inflight post send were put to be executed as of we
289189ea94a7SMaor Gottlieb 	 * lock/unlock above locks Now need to arm all involved CQs.
289289ea94a7SMaor Gottlieb 	 */
289389ea94a7SMaor Gottlieb 	list_for_each_entry(mcq, &cq_armed_list, reset_notify) {
289489ea94a7SMaor Gottlieb 		mcq->comp(mcq);
289589ea94a7SMaor Gottlieb 	}
289689ea94a7SMaor Gottlieb 	spin_unlock_irqrestore(&ibdev->reset_flow_resource_lock, flags);
289789ea94a7SMaor Gottlieb }
289889ea94a7SMaor Gottlieb 
289903404e8aSMaor Gottlieb static void delay_drop_handler(struct work_struct *work)
290003404e8aSMaor Gottlieb {
290103404e8aSMaor Gottlieb 	int err;
290203404e8aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop =
290303404e8aSMaor Gottlieb 		container_of(work, struct mlx5_ib_delay_drop,
290403404e8aSMaor Gottlieb 			     delay_drop_work);
290503404e8aSMaor Gottlieb 
2906fe248c3aSMaor Gottlieb 	atomic_inc(&delay_drop->events_cnt);
2907fe248c3aSMaor Gottlieb 
290803404e8aSMaor Gottlieb 	mutex_lock(&delay_drop->lock);
290903404e8aSMaor Gottlieb 	err = mlx5_core_set_delay_drop(delay_drop->dev->mdev,
291003404e8aSMaor Gottlieb 				       delay_drop->timeout);
291103404e8aSMaor Gottlieb 	if (err) {
291203404e8aSMaor Gottlieb 		mlx5_ib_warn(delay_drop->dev, "Failed to set delay drop, timeout=%u\n",
291303404e8aSMaor Gottlieb 			     delay_drop->timeout);
291403404e8aSMaor Gottlieb 		delay_drop->activate = false;
291503404e8aSMaor Gottlieb 	}
291603404e8aSMaor Gottlieb 	mutex_unlock(&delay_drop->lock);
291703404e8aSMaor Gottlieb }
291803404e8aSMaor Gottlieb 
29199603b61dSJack Morgenstein static void mlx5_ib_event(struct mlx5_core_dev *dev, void *context,
29204d2f9bbbSJack Morgenstein 			  enum mlx5_dev_event event, unsigned long param)
2921e126ba97SEli Cohen {
29229603b61dSJack Morgenstein 	struct mlx5_ib_dev *ibdev = (struct mlx5_ib_dev *)context;
2923e126ba97SEli Cohen 	struct ib_event ibev;
2924dbaaff2aSEli Cohen 	bool fatal = false;
2925e126ba97SEli Cohen 	u8 port = 0;
2926e126ba97SEli Cohen 
2927e126ba97SEli Cohen 	switch (event) {
2928e126ba97SEli Cohen 	case MLX5_DEV_EVENT_SYS_ERROR:
2929e126ba97SEli Cohen 		ibev.event = IB_EVENT_DEVICE_FATAL;
293089ea94a7SMaor Gottlieb 		mlx5_ib_handle_internal_error(ibdev);
2931dbaaff2aSEli Cohen 		fatal = true;
2932e126ba97SEli Cohen 		break;
2933e126ba97SEli Cohen 
2934e126ba97SEli Cohen 	case MLX5_DEV_EVENT_PORT_UP:
2935e126ba97SEli Cohen 	case MLX5_DEV_EVENT_PORT_DOWN:
29362788cf3bSNoa Osherovich 	case MLX5_DEV_EVENT_PORT_INITIALIZED:
29374d2f9bbbSJack Morgenstein 		port = (u8)param;
29385ec8c83eSAviv Heller 
29395ec8c83eSAviv Heller 		/* In RoCE, port up/down events are handled in
29405ec8c83eSAviv Heller 		 * mlx5_netdev_event().
29415ec8c83eSAviv Heller 		 */
29425ec8c83eSAviv Heller 		if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) ==
29435ec8c83eSAviv Heller 			IB_LINK_LAYER_ETHERNET)
29445ec8c83eSAviv Heller 			return;
29455ec8c83eSAviv Heller 
29465ec8c83eSAviv Heller 		ibev.event = (event == MLX5_DEV_EVENT_PORT_UP) ?
29475ec8c83eSAviv Heller 			     IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
2948e126ba97SEli Cohen 		break;
2949e126ba97SEli Cohen 
2950e126ba97SEli Cohen 	case MLX5_DEV_EVENT_LID_CHANGE:
2951e126ba97SEli Cohen 		ibev.event = IB_EVENT_LID_CHANGE;
29524d2f9bbbSJack Morgenstein 		port = (u8)param;
2953e126ba97SEli Cohen 		break;
2954e126ba97SEli Cohen 
2955e126ba97SEli Cohen 	case MLX5_DEV_EVENT_PKEY_CHANGE:
2956e126ba97SEli Cohen 		ibev.event = IB_EVENT_PKEY_CHANGE;
29574d2f9bbbSJack Morgenstein 		port = (u8)param;
29587722f47eSHaggai Eran 
29597722f47eSHaggai Eran 		schedule_work(&ibdev->devr.ports[port - 1].pkey_change_work);
2960e126ba97SEli Cohen 		break;
2961e126ba97SEli Cohen 
2962e126ba97SEli Cohen 	case MLX5_DEV_EVENT_GUID_CHANGE:
2963e126ba97SEli Cohen 		ibev.event = IB_EVENT_GID_CHANGE;
29644d2f9bbbSJack Morgenstein 		port = (u8)param;
2965e126ba97SEli Cohen 		break;
2966e126ba97SEli Cohen 
2967e126ba97SEli Cohen 	case MLX5_DEV_EVENT_CLIENT_REREG:
2968e126ba97SEli Cohen 		ibev.event = IB_EVENT_CLIENT_REREGISTER;
29694d2f9bbbSJack Morgenstein 		port = (u8)param;
2970e126ba97SEli Cohen 		break;
297103404e8aSMaor Gottlieb 	case MLX5_DEV_EVENT_DELAY_DROP_TIMEOUT:
297203404e8aSMaor Gottlieb 		schedule_work(&ibdev->delay_drop.delay_drop_work);
297303404e8aSMaor Gottlieb 		goto out;
2974bdc37924SSaeed Mahameed 	default:
297503404e8aSMaor Gottlieb 		goto out;
2976e126ba97SEli Cohen 	}
2977e126ba97SEli Cohen 
2978e126ba97SEli Cohen 	ibev.device	      = &ibdev->ib_dev;
2979e126ba97SEli Cohen 	ibev.element.port_num = port;
2980e126ba97SEli Cohen 
2981a0c84c32SEli Cohen 	if (port < 1 || port > ibdev->num_ports) {
2982a0c84c32SEli Cohen 		mlx5_ib_warn(ibdev, "warning: event on port %d\n", port);
298303404e8aSMaor Gottlieb 		goto out;
2984a0c84c32SEli Cohen 	}
2985a0c84c32SEli Cohen 
2986e126ba97SEli Cohen 	if (ibdev->ib_active)
2987e126ba97SEli Cohen 		ib_dispatch_event(&ibev);
2988dbaaff2aSEli Cohen 
2989dbaaff2aSEli Cohen 	if (fatal)
2990dbaaff2aSEli Cohen 		ibdev->ib_active = false;
299103404e8aSMaor Gottlieb 
299203404e8aSMaor Gottlieb out:
299303404e8aSMaor Gottlieb 	return;
2994e126ba97SEli Cohen }
2995e126ba97SEli Cohen 
2996c43f1112SMaor Gottlieb static int set_has_smi_cap(struct mlx5_ib_dev *dev)
2997c43f1112SMaor Gottlieb {
2998c43f1112SMaor Gottlieb 	struct mlx5_hca_vport_context vport_ctx;
2999c43f1112SMaor Gottlieb 	int err;
3000c43f1112SMaor Gottlieb 	int port;
3001c43f1112SMaor Gottlieb 
3002c43f1112SMaor Gottlieb 	for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++) {
3003c43f1112SMaor Gottlieb 		dev->mdev->port_caps[port - 1].has_smi = false;
3004c43f1112SMaor Gottlieb 		if (MLX5_CAP_GEN(dev->mdev, port_type) ==
3005c43f1112SMaor Gottlieb 		    MLX5_CAP_PORT_TYPE_IB) {
3006c43f1112SMaor Gottlieb 			if (MLX5_CAP_GEN(dev->mdev, ib_virt)) {
3007c43f1112SMaor Gottlieb 				err = mlx5_query_hca_vport_context(dev->mdev, 0,
3008c43f1112SMaor Gottlieb 								   port, 0,
3009c43f1112SMaor Gottlieb 								   &vport_ctx);
3010c43f1112SMaor Gottlieb 				if (err) {
3011c43f1112SMaor Gottlieb 					mlx5_ib_err(dev, "query_hca_vport_context for port=%d failed %d\n",
3012c43f1112SMaor Gottlieb 						    port, err);
3013c43f1112SMaor Gottlieb 					return err;
3014c43f1112SMaor Gottlieb 				}
3015c43f1112SMaor Gottlieb 				dev->mdev->port_caps[port - 1].has_smi =
3016c43f1112SMaor Gottlieb 					vport_ctx.has_smi;
3017c43f1112SMaor Gottlieb 			} else {
3018c43f1112SMaor Gottlieb 				dev->mdev->port_caps[port - 1].has_smi = true;
3019c43f1112SMaor Gottlieb 			}
3020c43f1112SMaor Gottlieb 		}
3021c43f1112SMaor Gottlieb 	}
3022c43f1112SMaor Gottlieb 	return 0;
3023c43f1112SMaor Gottlieb }
3024c43f1112SMaor Gottlieb 
3025e126ba97SEli Cohen static void get_ext_port_caps(struct mlx5_ib_dev *dev)
3026e126ba97SEli Cohen {
3027e126ba97SEli Cohen 	int port;
3028e126ba97SEli Cohen 
3029938fe83cSSaeed Mahameed 	for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++)
3030e126ba97SEli Cohen 		mlx5_query_ext_port_caps(dev, port);
3031e126ba97SEli Cohen }
3032e126ba97SEli Cohen 
3033e126ba97SEli Cohen static int get_port_caps(struct mlx5_ib_dev *dev)
3034e126ba97SEli Cohen {
3035e126ba97SEli Cohen 	struct ib_device_attr *dprops = NULL;
3036e126ba97SEli Cohen 	struct ib_port_attr *pprops = NULL;
3037f614fc15SDan Carpenter 	int err = -ENOMEM;
3038e126ba97SEli Cohen 	int port;
30392528e33eSMatan Barak 	struct ib_udata uhw = {.inlen = 0, .outlen = 0};
3040e126ba97SEli Cohen 
3041e126ba97SEli Cohen 	pprops = kmalloc(sizeof(*pprops), GFP_KERNEL);
3042e126ba97SEli Cohen 	if (!pprops)
3043e126ba97SEli Cohen 		goto out;
3044e126ba97SEli Cohen 
3045e126ba97SEli Cohen 	dprops = kmalloc(sizeof(*dprops), GFP_KERNEL);
3046e126ba97SEli Cohen 	if (!dprops)
3047e126ba97SEli Cohen 		goto out;
3048e126ba97SEli Cohen 
3049c43f1112SMaor Gottlieb 	err = set_has_smi_cap(dev);
3050c43f1112SMaor Gottlieb 	if (err)
3051c43f1112SMaor Gottlieb 		goto out;
3052c43f1112SMaor Gottlieb 
30532528e33eSMatan Barak 	err = mlx5_ib_query_device(&dev->ib_dev, dprops, &uhw);
3054e126ba97SEli Cohen 	if (err) {
3055e126ba97SEli Cohen 		mlx5_ib_warn(dev, "query_device failed %d\n", err);
3056e126ba97SEli Cohen 		goto out;
3057e126ba97SEli Cohen 	}
3058e126ba97SEli Cohen 
3059938fe83cSSaeed Mahameed 	for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++) {
3060c4550c63SOr Gerlitz 		memset(pprops, 0, sizeof(*pprops));
3061e126ba97SEli Cohen 		err = mlx5_ib_query_port(&dev->ib_dev, port, pprops);
3062e126ba97SEli Cohen 		if (err) {
3063938fe83cSSaeed Mahameed 			mlx5_ib_warn(dev, "query_port %d failed %d\n",
3064938fe83cSSaeed Mahameed 				     port, err);
3065e126ba97SEli Cohen 			break;
3066e126ba97SEli Cohen 		}
3067938fe83cSSaeed Mahameed 		dev->mdev->port_caps[port - 1].pkey_table_len =
3068938fe83cSSaeed Mahameed 						dprops->max_pkeys;
3069938fe83cSSaeed Mahameed 		dev->mdev->port_caps[port - 1].gid_table_len =
3070938fe83cSSaeed Mahameed 						pprops->gid_tbl_len;
3071e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "pkey_table_len %d, gid_table_len %d\n",
3072e126ba97SEli Cohen 			    dprops->max_pkeys, pprops->gid_tbl_len);
3073e126ba97SEli Cohen 	}
3074e126ba97SEli Cohen 
3075e126ba97SEli Cohen out:
3076e126ba97SEli Cohen 	kfree(pprops);
3077e126ba97SEli Cohen 	kfree(dprops);
3078e126ba97SEli Cohen 
3079e126ba97SEli Cohen 	return err;
3080e126ba97SEli Cohen }
3081e126ba97SEli Cohen 
3082e126ba97SEli Cohen static void destroy_umrc_res(struct mlx5_ib_dev *dev)
3083e126ba97SEli Cohen {
3084e126ba97SEli Cohen 	int err;
3085e126ba97SEli Cohen 
3086e126ba97SEli Cohen 	err = mlx5_mr_cache_cleanup(dev);
3087e126ba97SEli Cohen 	if (err)
3088e126ba97SEli Cohen 		mlx5_ib_warn(dev, "mr cache cleanup failed\n");
3089e126ba97SEli Cohen 
3090e126ba97SEli Cohen 	mlx5_ib_destroy_qp(dev->umrc.qp);
3091add08d76SChristoph Hellwig 	ib_free_cq(dev->umrc.cq);
3092e126ba97SEli Cohen 	ib_dealloc_pd(dev->umrc.pd);
3093e126ba97SEli Cohen }
3094e126ba97SEli Cohen 
3095e126ba97SEli Cohen enum {
3096e126ba97SEli Cohen 	MAX_UMR_WR = 128,
3097e126ba97SEli Cohen };
3098e126ba97SEli Cohen 
3099e126ba97SEli Cohen static int create_umr_res(struct mlx5_ib_dev *dev)
3100e126ba97SEli Cohen {
3101e126ba97SEli Cohen 	struct ib_qp_init_attr *init_attr = NULL;
3102e126ba97SEli Cohen 	struct ib_qp_attr *attr = NULL;
3103e126ba97SEli Cohen 	struct ib_pd *pd;
3104e126ba97SEli Cohen 	struct ib_cq *cq;
3105e126ba97SEli Cohen 	struct ib_qp *qp;
3106e126ba97SEli Cohen 	int ret;
3107e126ba97SEli Cohen 
3108e126ba97SEli Cohen 	attr = kzalloc(sizeof(*attr), GFP_KERNEL);
3109e126ba97SEli Cohen 	init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL);
3110e126ba97SEli Cohen 	if (!attr || !init_attr) {
3111e126ba97SEli Cohen 		ret = -ENOMEM;
3112e126ba97SEli Cohen 		goto error_0;
3113e126ba97SEli Cohen 	}
3114e126ba97SEli Cohen 
3115ed082d36SChristoph Hellwig 	pd = ib_alloc_pd(&dev->ib_dev, 0);
3116e126ba97SEli Cohen 	if (IS_ERR(pd)) {
3117e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create PD for sync UMR QP\n");
3118e126ba97SEli Cohen 		ret = PTR_ERR(pd);
3119e126ba97SEli Cohen 		goto error_0;
3120e126ba97SEli Cohen 	}
3121e126ba97SEli Cohen 
3122add08d76SChristoph Hellwig 	cq = ib_alloc_cq(&dev->ib_dev, NULL, 128, 0, IB_POLL_SOFTIRQ);
3123e126ba97SEli Cohen 	if (IS_ERR(cq)) {
3124e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create CQ for sync UMR QP\n");
3125e126ba97SEli Cohen 		ret = PTR_ERR(cq);
3126e126ba97SEli Cohen 		goto error_2;
3127e126ba97SEli Cohen 	}
3128e126ba97SEli Cohen 
3129e126ba97SEli Cohen 	init_attr->send_cq = cq;
3130e126ba97SEli Cohen 	init_attr->recv_cq = cq;
3131e126ba97SEli Cohen 	init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
3132e126ba97SEli Cohen 	init_attr->cap.max_send_wr = MAX_UMR_WR;
3133e126ba97SEli Cohen 	init_attr->cap.max_send_sge = 1;
3134e126ba97SEli Cohen 	init_attr->qp_type = MLX5_IB_QPT_REG_UMR;
3135e126ba97SEli Cohen 	init_attr->port_num = 1;
3136e126ba97SEli Cohen 	qp = mlx5_ib_create_qp(pd, init_attr, NULL);
3137e126ba97SEli Cohen 	if (IS_ERR(qp)) {
3138e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create sync UMR QP\n");
3139e126ba97SEli Cohen 		ret = PTR_ERR(qp);
3140e126ba97SEli Cohen 		goto error_3;
3141e126ba97SEli Cohen 	}
3142e126ba97SEli Cohen 	qp->device     = &dev->ib_dev;
3143e126ba97SEli Cohen 	qp->real_qp    = qp;
3144e126ba97SEli Cohen 	qp->uobject    = NULL;
3145e126ba97SEli Cohen 	qp->qp_type    = MLX5_IB_QPT_REG_UMR;
314631fde034SMajd Dibbiny 	qp->send_cq    = init_attr->send_cq;
314731fde034SMajd Dibbiny 	qp->recv_cq    = init_attr->recv_cq;
3148e126ba97SEli Cohen 
3149e126ba97SEli Cohen 	attr->qp_state = IB_QPS_INIT;
3150e126ba97SEli Cohen 	attr->port_num = 1;
3151e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_PKEY_INDEX |
3152e126ba97SEli Cohen 				IB_QP_PORT, NULL);
3153e126ba97SEli Cohen 	if (ret) {
3154e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify UMR QP\n");
3155e126ba97SEli Cohen 		goto error_4;
3156e126ba97SEli Cohen 	}
3157e126ba97SEli Cohen 
3158e126ba97SEli Cohen 	memset(attr, 0, sizeof(*attr));
3159e126ba97SEli Cohen 	attr->qp_state = IB_QPS_RTR;
3160e126ba97SEli Cohen 	attr->path_mtu = IB_MTU_256;
3161e126ba97SEli Cohen 
3162e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
3163e126ba97SEli Cohen 	if (ret) {
3164e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify umr QP to rtr\n");
3165e126ba97SEli Cohen 		goto error_4;
3166e126ba97SEli Cohen 	}
3167e126ba97SEli Cohen 
3168e126ba97SEli Cohen 	memset(attr, 0, sizeof(*attr));
3169e126ba97SEli Cohen 	attr->qp_state = IB_QPS_RTS;
3170e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
3171e126ba97SEli Cohen 	if (ret) {
3172e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify umr QP to rts\n");
3173e126ba97SEli Cohen 		goto error_4;
3174e126ba97SEli Cohen 	}
3175e126ba97SEli Cohen 
3176e126ba97SEli Cohen 	dev->umrc.qp = qp;
3177e126ba97SEli Cohen 	dev->umrc.cq = cq;
3178e126ba97SEli Cohen 	dev->umrc.pd = pd;
3179e126ba97SEli Cohen 
3180e126ba97SEli Cohen 	sema_init(&dev->umrc.sem, MAX_UMR_WR);
3181e126ba97SEli Cohen 	ret = mlx5_mr_cache_init(dev);
3182e126ba97SEli Cohen 	if (ret) {
3183e126ba97SEli Cohen 		mlx5_ib_warn(dev, "mr cache init failed %d\n", ret);
3184e126ba97SEli Cohen 		goto error_4;
3185e126ba97SEli Cohen 	}
3186e126ba97SEli Cohen 
3187e126ba97SEli Cohen 	kfree(attr);
3188e126ba97SEli Cohen 	kfree(init_attr);
3189e126ba97SEli Cohen 
3190e126ba97SEli Cohen 	return 0;
3191e126ba97SEli Cohen 
3192e126ba97SEli Cohen error_4:
3193e126ba97SEli Cohen 	mlx5_ib_destroy_qp(qp);
3194e126ba97SEli Cohen 
3195e126ba97SEli Cohen error_3:
3196add08d76SChristoph Hellwig 	ib_free_cq(cq);
3197e126ba97SEli Cohen 
3198e126ba97SEli Cohen error_2:
3199e126ba97SEli Cohen 	ib_dealloc_pd(pd);
3200e126ba97SEli Cohen 
3201e126ba97SEli Cohen error_0:
3202e126ba97SEli Cohen 	kfree(attr);
3203e126ba97SEli Cohen 	kfree(init_attr);
3204e126ba97SEli Cohen 	return ret;
3205e126ba97SEli Cohen }
3206e126ba97SEli Cohen 
32076e8484c5SMax Gurtovoy static u8 mlx5_get_umr_fence(u8 umr_fence_cap)
32086e8484c5SMax Gurtovoy {
32096e8484c5SMax Gurtovoy 	switch (umr_fence_cap) {
32106e8484c5SMax Gurtovoy 	case MLX5_CAP_UMR_FENCE_NONE:
32116e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_NONE;
32126e8484c5SMax Gurtovoy 	case MLX5_CAP_UMR_FENCE_SMALL:
32136e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_INITIATOR_SMALL;
32146e8484c5SMax Gurtovoy 	default:
32156e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_STRONG_ORDERING;
32166e8484c5SMax Gurtovoy 	}
32176e8484c5SMax Gurtovoy }
32186e8484c5SMax Gurtovoy 
3219e126ba97SEli Cohen static int create_dev_resources(struct mlx5_ib_resources *devr)
3220e126ba97SEli Cohen {
3221e126ba97SEli Cohen 	struct ib_srq_init_attr attr;
3222e126ba97SEli Cohen 	struct mlx5_ib_dev *dev;
3223bcf4c1eaSMatan Barak 	struct ib_cq_init_attr cq_attr = {.cqe = 1};
32247722f47eSHaggai Eran 	int port;
3225e126ba97SEli Cohen 	int ret = 0;
3226e126ba97SEli Cohen 
3227e126ba97SEli Cohen 	dev = container_of(devr, struct mlx5_ib_dev, devr);
3228e126ba97SEli Cohen 
3229d16e91daSHaggai Eran 	mutex_init(&devr->mutex);
3230d16e91daSHaggai Eran 
3231e126ba97SEli Cohen 	devr->p0 = mlx5_ib_alloc_pd(&dev->ib_dev, NULL, NULL);
3232e126ba97SEli Cohen 	if (IS_ERR(devr->p0)) {
3233e126ba97SEli Cohen 		ret = PTR_ERR(devr->p0);
3234e126ba97SEli Cohen 		goto error0;
3235e126ba97SEli Cohen 	}
3236e126ba97SEli Cohen 	devr->p0->device  = &dev->ib_dev;
3237e126ba97SEli Cohen 	devr->p0->uobject = NULL;
3238e126ba97SEli Cohen 	atomic_set(&devr->p0->usecnt, 0);
3239e126ba97SEli Cohen 
3240bcf4c1eaSMatan Barak 	devr->c0 = mlx5_ib_create_cq(&dev->ib_dev, &cq_attr, NULL, NULL);
3241e126ba97SEli Cohen 	if (IS_ERR(devr->c0)) {
3242e126ba97SEli Cohen 		ret = PTR_ERR(devr->c0);
3243e126ba97SEli Cohen 		goto error1;
3244e126ba97SEli Cohen 	}
3245e126ba97SEli Cohen 	devr->c0->device        = &dev->ib_dev;
3246e126ba97SEli Cohen 	devr->c0->uobject       = NULL;
3247e126ba97SEli Cohen 	devr->c0->comp_handler  = NULL;
3248e126ba97SEli Cohen 	devr->c0->event_handler = NULL;
3249e126ba97SEli Cohen 	devr->c0->cq_context    = NULL;
3250e126ba97SEli Cohen 	atomic_set(&devr->c0->usecnt, 0);
3251e126ba97SEli Cohen 
3252e126ba97SEli Cohen 	devr->x0 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
3253e126ba97SEli Cohen 	if (IS_ERR(devr->x0)) {
3254e126ba97SEli Cohen 		ret = PTR_ERR(devr->x0);
3255e126ba97SEli Cohen 		goto error2;
3256e126ba97SEli Cohen 	}
3257e126ba97SEli Cohen 	devr->x0->device = &dev->ib_dev;
3258e126ba97SEli Cohen 	devr->x0->inode = NULL;
3259e126ba97SEli Cohen 	atomic_set(&devr->x0->usecnt, 0);
3260e126ba97SEli Cohen 	mutex_init(&devr->x0->tgt_qp_mutex);
3261e126ba97SEli Cohen 	INIT_LIST_HEAD(&devr->x0->tgt_qp_list);
3262e126ba97SEli Cohen 
3263e126ba97SEli Cohen 	devr->x1 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
3264e126ba97SEli Cohen 	if (IS_ERR(devr->x1)) {
3265e126ba97SEli Cohen 		ret = PTR_ERR(devr->x1);
3266e126ba97SEli Cohen 		goto error3;
3267e126ba97SEli Cohen 	}
3268e126ba97SEli Cohen 	devr->x1->device = &dev->ib_dev;
3269e126ba97SEli Cohen 	devr->x1->inode = NULL;
3270e126ba97SEli Cohen 	atomic_set(&devr->x1->usecnt, 0);
3271e126ba97SEli Cohen 	mutex_init(&devr->x1->tgt_qp_mutex);
3272e126ba97SEli Cohen 	INIT_LIST_HEAD(&devr->x1->tgt_qp_list);
3273e126ba97SEli Cohen 
3274e126ba97SEli Cohen 	memset(&attr, 0, sizeof(attr));
3275e126ba97SEli Cohen 	attr.attr.max_sge = 1;
3276e126ba97SEli Cohen 	attr.attr.max_wr = 1;
3277e126ba97SEli Cohen 	attr.srq_type = IB_SRQT_XRC;
32781a56ff6dSArtemy Kovalyov 	attr.ext.cq = devr->c0;
3279e126ba97SEli Cohen 	attr.ext.xrc.xrcd = devr->x0;
3280e126ba97SEli Cohen 
3281e126ba97SEli Cohen 	devr->s0 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
3282e126ba97SEli Cohen 	if (IS_ERR(devr->s0)) {
3283e126ba97SEli Cohen 		ret = PTR_ERR(devr->s0);
3284e126ba97SEli Cohen 		goto error4;
3285e126ba97SEli Cohen 	}
3286e126ba97SEli Cohen 	devr->s0->device	= &dev->ib_dev;
3287e126ba97SEli Cohen 	devr->s0->pd		= devr->p0;
3288e126ba97SEli Cohen 	devr->s0->uobject       = NULL;
3289e126ba97SEli Cohen 	devr->s0->event_handler = NULL;
3290e126ba97SEli Cohen 	devr->s0->srq_context   = NULL;
3291e126ba97SEli Cohen 	devr->s0->srq_type      = IB_SRQT_XRC;
3292e126ba97SEli Cohen 	devr->s0->ext.xrc.xrcd	= devr->x0;
32931a56ff6dSArtemy Kovalyov 	devr->s0->ext.cq	= devr->c0;
3294e126ba97SEli Cohen 	atomic_inc(&devr->s0->ext.xrc.xrcd->usecnt);
32951a56ff6dSArtemy Kovalyov 	atomic_inc(&devr->s0->ext.cq->usecnt);
3296e126ba97SEli Cohen 	atomic_inc(&devr->p0->usecnt);
3297e126ba97SEli Cohen 	atomic_set(&devr->s0->usecnt, 0);
3298e126ba97SEli Cohen 
32994aa17b28SHaggai Abramonvsky 	memset(&attr, 0, sizeof(attr));
33004aa17b28SHaggai Abramonvsky 	attr.attr.max_sge = 1;
33014aa17b28SHaggai Abramonvsky 	attr.attr.max_wr = 1;
33024aa17b28SHaggai Abramonvsky 	attr.srq_type = IB_SRQT_BASIC;
33034aa17b28SHaggai Abramonvsky 	devr->s1 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
33044aa17b28SHaggai Abramonvsky 	if (IS_ERR(devr->s1)) {
33054aa17b28SHaggai Abramonvsky 		ret = PTR_ERR(devr->s1);
33064aa17b28SHaggai Abramonvsky 		goto error5;
33074aa17b28SHaggai Abramonvsky 	}
33084aa17b28SHaggai Abramonvsky 	devr->s1->device	= &dev->ib_dev;
33094aa17b28SHaggai Abramonvsky 	devr->s1->pd		= devr->p0;
33104aa17b28SHaggai Abramonvsky 	devr->s1->uobject       = NULL;
33114aa17b28SHaggai Abramonvsky 	devr->s1->event_handler = NULL;
33124aa17b28SHaggai Abramonvsky 	devr->s1->srq_context   = NULL;
33134aa17b28SHaggai Abramonvsky 	devr->s1->srq_type      = IB_SRQT_BASIC;
33141a56ff6dSArtemy Kovalyov 	devr->s1->ext.cq	= devr->c0;
33154aa17b28SHaggai Abramonvsky 	atomic_inc(&devr->p0->usecnt);
33161a56ff6dSArtemy Kovalyov 	atomic_set(&devr->s1->usecnt, 0);
33174aa17b28SHaggai Abramonvsky 
33187722f47eSHaggai Eran 	for (port = 0; port < ARRAY_SIZE(devr->ports); ++port) {
33197722f47eSHaggai Eran 		INIT_WORK(&devr->ports[port].pkey_change_work,
33207722f47eSHaggai Eran 			  pkey_change_handler);
33217722f47eSHaggai Eran 		devr->ports[port].devr = devr;
33227722f47eSHaggai Eran 	}
33237722f47eSHaggai Eran 
3324e126ba97SEli Cohen 	return 0;
3325e126ba97SEli Cohen 
33264aa17b28SHaggai Abramonvsky error5:
33274aa17b28SHaggai Abramonvsky 	mlx5_ib_destroy_srq(devr->s0);
3328e126ba97SEli Cohen error4:
3329e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x1);
3330e126ba97SEli Cohen error3:
3331e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x0);
3332e126ba97SEli Cohen error2:
3333e126ba97SEli Cohen 	mlx5_ib_destroy_cq(devr->c0);
3334e126ba97SEli Cohen error1:
3335e126ba97SEli Cohen 	mlx5_ib_dealloc_pd(devr->p0);
3336e126ba97SEli Cohen error0:
3337e126ba97SEli Cohen 	return ret;
3338e126ba97SEli Cohen }
3339e126ba97SEli Cohen 
3340e126ba97SEli Cohen static void destroy_dev_resources(struct mlx5_ib_resources *devr)
3341e126ba97SEli Cohen {
33427722f47eSHaggai Eran 	struct mlx5_ib_dev *dev =
33437722f47eSHaggai Eran 		container_of(devr, struct mlx5_ib_dev, devr);
33447722f47eSHaggai Eran 	int port;
33457722f47eSHaggai Eran 
33464aa17b28SHaggai Abramonvsky 	mlx5_ib_destroy_srq(devr->s1);
3347e126ba97SEli Cohen 	mlx5_ib_destroy_srq(devr->s0);
3348e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x0);
3349e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x1);
3350e126ba97SEli Cohen 	mlx5_ib_destroy_cq(devr->c0);
3351e126ba97SEli Cohen 	mlx5_ib_dealloc_pd(devr->p0);
33527722f47eSHaggai Eran 
33537722f47eSHaggai Eran 	/* Make sure no change P_Key work items are still executing */
33547722f47eSHaggai Eran 	for (port = 0; port < dev->num_ports; ++port)
33557722f47eSHaggai Eran 		cancel_work_sync(&devr->ports[port].pkey_change_work);
3356e126ba97SEli Cohen }
3357e126ba97SEli Cohen 
3358e53505a8SAchiad Shochat static u32 get_core_cap_flags(struct ib_device *ibdev)
3359e53505a8SAchiad Shochat {
3360e53505a8SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
3361e53505a8SAchiad Shochat 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, 1);
3362e53505a8SAchiad Shochat 	u8 l3_type_cap = MLX5_CAP_ROCE(dev->mdev, l3_type);
3363e53505a8SAchiad Shochat 	u8 roce_version_cap = MLX5_CAP_ROCE(dev->mdev, roce_version);
3364e53505a8SAchiad Shochat 	u32 ret = 0;
3365e53505a8SAchiad Shochat 
3366e53505a8SAchiad Shochat 	if (ll == IB_LINK_LAYER_INFINIBAND)
3367e53505a8SAchiad Shochat 		return RDMA_CORE_PORT_IBA_IB;
3368e53505a8SAchiad Shochat 
336972cd5717SOr Gerlitz 	ret = RDMA_CORE_PORT_RAW_PACKET;
337072cd5717SOr Gerlitz 
3371e53505a8SAchiad Shochat 	if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV4_CAP))
337272cd5717SOr Gerlitz 		return ret;
3373e53505a8SAchiad Shochat 
3374e53505a8SAchiad Shochat 	if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV6_CAP))
337572cd5717SOr Gerlitz 		return ret;
3376e53505a8SAchiad Shochat 
3377e53505a8SAchiad Shochat 	if (roce_version_cap & MLX5_ROCE_VERSION_1_CAP)
3378e53505a8SAchiad Shochat 		ret |= RDMA_CORE_PORT_IBA_ROCE;
3379e53505a8SAchiad Shochat 
3380e53505a8SAchiad Shochat 	if (roce_version_cap & MLX5_ROCE_VERSION_2_CAP)
3381e53505a8SAchiad Shochat 		ret |= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
3382e53505a8SAchiad Shochat 
3383e53505a8SAchiad Shochat 	return ret;
3384e53505a8SAchiad Shochat }
3385e53505a8SAchiad Shochat 
33867738613eSIra Weiny static int mlx5_port_immutable(struct ib_device *ibdev, u8 port_num,
33877738613eSIra Weiny 			       struct ib_port_immutable *immutable)
33887738613eSIra Weiny {
33897738613eSIra Weiny 	struct ib_port_attr attr;
3390ca5b91d6SOr Gerlitz 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
3391ca5b91d6SOr Gerlitz 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, port_num);
33927738613eSIra Weiny 	int err;
33937738613eSIra Weiny 
3394c4550c63SOr Gerlitz 	immutable->core_cap_flags = get_core_cap_flags(ibdev);
3395c4550c63SOr Gerlitz 
3396c4550c63SOr Gerlitz 	err = ib_query_port(ibdev, port_num, &attr);
33977738613eSIra Weiny 	if (err)
33987738613eSIra Weiny 		return err;
33997738613eSIra Weiny 
34007738613eSIra Weiny 	immutable->pkey_tbl_len = attr.pkey_tbl_len;
34017738613eSIra Weiny 	immutable->gid_tbl_len = attr.gid_tbl_len;
3402e53505a8SAchiad Shochat 	immutable->core_cap_flags = get_core_cap_flags(ibdev);
3403ca5b91d6SOr Gerlitz 	if ((ll == IB_LINK_LAYER_INFINIBAND) || MLX5_CAP_GEN(dev->mdev, roce))
3404337877a4SIra Weiny 		immutable->max_mad_size = IB_MGMT_MAD_SIZE;
34057738613eSIra Weiny 
34067738613eSIra Weiny 	return 0;
34077738613eSIra Weiny }
34087738613eSIra Weiny 
34099abb0d1bSLeon Romanovsky static void get_dev_fw_str(struct ib_device *ibdev, char *str)
3410c7342823SIra Weiny {
3411c7342823SIra Weiny 	struct mlx5_ib_dev *dev =
3412c7342823SIra Weiny 		container_of(ibdev, struct mlx5_ib_dev, ib_dev);
34139abb0d1bSLeon Romanovsky 	snprintf(str, IB_FW_VERSION_NAME_MAX, "%d.%d.%04d",
34149abb0d1bSLeon Romanovsky 		 fw_rev_maj(dev->mdev), fw_rev_min(dev->mdev),
34159abb0d1bSLeon Romanovsky 		 fw_rev_sub(dev->mdev));
3416c7342823SIra Weiny }
3417c7342823SIra Weiny 
341845f95acdSOr Gerlitz static int mlx5_eth_lag_init(struct mlx5_ib_dev *dev)
34199ef9c640SAviv Heller {
34209ef9c640SAviv Heller 	struct mlx5_core_dev *mdev = dev->mdev;
34219ef9c640SAviv Heller 	struct mlx5_flow_namespace *ns = mlx5_get_flow_namespace(mdev,
34229ef9c640SAviv Heller 								 MLX5_FLOW_NAMESPACE_LAG);
34239ef9c640SAviv Heller 	struct mlx5_flow_table *ft;
34249ef9c640SAviv Heller 	int err;
34259ef9c640SAviv Heller 
34269ef9c640SAviv Heller 	if (!ns || !mlx5_lag_is_active(mdev))
34279ef9c640SAviv Heller 		return 0;
34289ef9c640SAviv Heller 
34299ef9c640SAviv Heller 	err = mlx5_cmd_create_vport_lag(mdev);
34309ef9c640SAviv Heller 	if (err)
34319ef9c640SAviv Heller 		return err;
34329ef9c640SAviv Heller 
34339ef9c640SAviv Heller 	ft = mlx5_create_lag_demux_flow_table(ns, 0, 0);
34349ef9c640SAviv Heller 	if (IS_ERR(ft)) {
34359ef9c640SAviv Heller 		err = PTR_ERR(ft);
34369ef9c640SAviv Heller 		goto err_destroy_vport_lag;
34379ef9c640SAviv Heller 	}
34389ef9c640SAviv Heller 
34399ef9c640SAviv Heller 	dev->flow_db.lag_demux_ft = ft;
34409ef9c640SAviv Heller 	return 0;
34419ef9c640SAviv Heller 
34429ef9c640SAviv Heller err_destroy_vport_lag:
34439ef9c640SAviv Heller 	mlx5_cmd_destroy_vport_lag(mdev);
34449ef9c640SAviv Heller 	return err;
34459ef9c640SAviv Heller }
34469ef9c640SAviv Heller 
344745f95acdSOr Gerlitz static void mlx5_eth_lag_cleanup(struct mlx5_ib_dev *dev)
34489ef9c640SAviv Heller {
34499ef9c640SAviv Heller 	struct mlx5_core_dev *mdev = dev->mdev;
34509ef9c640SAviv Heller 
34519ef9c640SAviv Heller 	if (dev->flow_db.lag_demux_ft) {
34529ef9c640SAviv Heller 		mlx5_destroy_flow_table(dev->flow_db.lag_demux_ft);
34539ef9c640SAviv Heller 		dev->flow_db.lag_demux_ft = NULL;
34549ef9c640SAviv Heller 
34559ef9c640SAviv Heller 		mlx5_cmd_destroy_vport_lag(mdev);
34569ef9c640SAviv Heller 	}
34579ef9c640SAviv Heller }
34589ef9c640SAviv Heller 
3459d012f5d6SOr Gerlitz static int mlx5_add_netdev_notifier(struct mlx5_ib_dev *dev)
3460fc24fc5eSAchiad Shochat {
3461e53505a8SAchiad Shochat 	int err;
3462e53505a8SAchiad Shochat 
3463fc24fc5eSAchiad Shochat 	dev->roce.nb.notifier_call = mlx5_netdev_event;
3464e53505a8SAchiad Shochat 	err = register_netdevice_notifier(&dev->roce.nb);
34655ec8c83eSAviv Heller 	if (err) {
34665ec8c83eSAviv Heller 		dev->roce.nb.notifier_call = NULL;
3467e53505a8SAchiad Shochat 		return err;
34685ec8c83eSAviv Heller 	}
3469e53505a8SAchiad Shochat 
3470d012f5d6SOr Gerlitz 	return 0;
3471d012f5d6SOr Gerlitz }
3472d012f5d6SOr Gerlitz 
3473d012f5d6SOr Gerlitz static void mlx5_remove_netdev_notifier(struct mlx5_ib_dev *dev)
3474e126ba97SEli Cohen {
3475e126ba97SEli Cohen 	if (dev->roce.nb.notifier_call) {
3476e126ba97SEli Cohen 		unregister_netdevice_notifier(&dev->roce.nb);
3477e126ba97SEli Cohen 		dev->roce.nb.notifier_call = NULL;
3478e126ba97SEli Cohen 	}
3479e126ba97SEli Cohen }
3480e126ba97SEli Cohen 
348145f95acdSOr Gerlitz static int mlx5_enable_eth(struct mlx5_ib_dev *dev)
3482e126ba97SEli Cohen {
3483e126ba97SEli Cohen 	int err;
3484e126ba97SEli Cohen 
3485d012f5d6SOr Gerlitz 	err = mlx5_add_netdev_notifier(dev);
3486d012f5d6SOr Gerlitz 	if (err)
3487e53505a8SAchiad Shochat 		return err;
3488e53505a8SAchiad Shochat 
3489ca5b91d6SOr Gerlitz 	if (MLX5_CAP_GEN(dev->mdev, roce)) {
3490e53505a8SAchiad Shochat 		err = mlx5_nic_vport_enable_roce(dev->mdev);
3491e53505a8SAchiad Shochat 		if (err)
3492e53505a8SAchiad Shochat 			goto err_unregister_netdevice_notifier;
3493ca5b91d6SOr Gerlitz 	}
3494e53505a8SAchiad Shochat 
349545f95acdSOr Gerlitz 	err = mlx5_eth_lag_init(dev);
34969ef9c640SAviv Heller 	if (err)
34979ef9c640SAviv Heller 		goto err_disable_roce;
34989ef9c640SAviv Heller 
3499e53505a8SAchiad Shochat 	return 0;
3500e53505a8SAchiad Shochat 
35019ef9c640SAviv Heller err_disable_roce:
3502ca5b91d6SOr Gerlitz 	if (MLX5_CAP_GEN(dev->mdev, roce))
35039ef9c640SAviv Heller 		mlx5_nic_vport_disable_roce(dev->mdev);
35049ef9c640SAviv Heller 
3505e53505a8SAchiad Shochat err_unregister_netdevice_notifier:
3506d012f5d6SOr Gerlitz 	mlx5_remove_netdev_notifier(dev);
3507e53505a8SAchiad Shochat 	return err;
3508fc24fc5eSAchiad Shochat }
3509fc24fc5eSAchiad Shochat 
351045f95acdSOr Gerlitz static void mlx5_disable_eth(struct mlx5_ib_dev *dev)
3511fc24fc5eSAchiad Shochat {
351245f95acdSOr Gerlitz 	mlx5_eth_lag_cleanup(dev);
3513ca5b91d6SOr Gerlitz 	if (MLX5_CAP_GEN(dev->mdev, roce))
3514e53505a8SAchiad Shochat 		mlx5_nic_vport_disable_roce(dev->mdev);
3515fc24fc5eSAchiad Shochat }
3516fc24fc5eSAchiad Shochat 
3517e1f24a79SParav Pandit struct mlx5_ib_counter {
35187c16f477SKamal Heib 	const char *name;
35197c16f477SKamal Heib 	size_t offset;
35207c16f477SKamal Heib };
35217c16f477SKamal Heib 
35227c16f477SKamal Heib #define INIT_Q_COUNTER(_name)		\
35237c16f477SKamal Heib 	{ .name = #_name, .offset = MLX5_BYTE_OFF(query_q_counter_out, _name)}
35247c16f477SKamal Heib 
3525e1f24a79SParav Pandit static const struct mlx5_ib_counter basic_q_cnts[] = {
35267c16f477SKamal Heib 	INIT_Q_COUNTER(rx_write_requests),
35277c16f477SKamal Heib 	INIT_Q_COUNTER(rx_read_requests),
35287c16f477SKamal Heib 	INIT_Q_COUNTER(rx_atomic_requests),
35297c16f477SKamal Heib 	INIT_Q_COUNTER(out_of_buffer),
35307c16f477SKamal Heib };
35317c16f477SKamal Heib 
3532e1f24a79SParav Pandit static const struct mlx5_ib_counter out_of_seq_q_cnts[] = {
35337c16f477SKamal Heib 	INIT_Q_COUNTER(out_of_sequence),
35347c16f477SKamal Heib };
35357c16f477SKamal Heib 
3536e1f24a79SParav Pandit static const struct mlx5_ib_counter retrans_q_cnts[] = {
35377c16f477SKamal Heib 	INIT_Q_COUNTER(duplicate_request),
35387c16f477SKamal Heib 	INIT_Q_COUNTER(rnr_nak_retry_err),
35397c16f477SKamal Heib 	INIT_Q_COUNTER(packet_seq_err),
35407c16f477SKamal Heib 	INIT_Q_COUNTER(implied_nak_seq_err),
35417c16f477SKamal Heib 	INIT_Q_COUNTER(local_ack_timeout_err),
35427c16f477SKamal Heib };
35437c16f477SKamal Heib 
3544e1f24a79SParav Pandit #define INIT_CONG_COUNTER(_name)		\
3545e1f24a79SParav Pandit 	{ .name = #_name, .offset =	\
3546e1f24a79SParav Pandit 		MLX5_BYTE_OFF(query_cong_statistics_out, _name ## _high)}
3547e1f24a79SParav Pandit 
3548e1f24a79SParav Pandit static const struct mlx5_ib_counter cong_cnts[] = {
3549e1f24a79SParav Pandit 	INIT_CONG_COUNTER(rp_cnp_ignored),
3550e1f24a79SParav Pandit 	INIT_CONG_COUNTER(rp_cnp_handled),
3551e1f24a79SParav Pandit 	INIT_CONG_COUNTER(np_ecn_marked_roce_packets),
3552e1f24a79SParav Pandit 	INIT_CONG_COUNTER(np_cnp_sent),
3553e1f24a79SParav Pandit };
3554e1f24a79SParav Pandit 
355558dcb60aSParav Pandit static const struct mlx5_ib_counter extended_err_cnts[] = {
355658dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_local_length_error),
355758dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_cqe_error),
355858dcb60aSParav Pandit 	INIT_Q_COUNTER(req_cqe_error),
355958dcb60aSParav Pandit 	INIT_Q_COUNTER(req_remote_invalid_request),
356058dcb60aSParav Pandit 	INIT_Q_COUNTER(req_remote_access_errors),
356158dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_remote_access_errors),
356258dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_cqe_flush_error),
356358dcb60aSParav Pandit 	INIT_Q_COUNTER(req_cqe_flush_error),
356458dcb60aSParav Pandit };
356558dcb60aSParav Pandit 
3566e1f24a79SParav Pandit static void mlx5_ib_dealloc_counters(struct mlx5_ib_dev *dev)
35670837e86aSMark Bloch {
35680837e86aSMark Bloch 	unsigned int i;
35690837e86aSMark Bloch 
35707c16f477SKamal Heib 	for (i = 0; i < dev->num_ports; i++) {
35710837e86aSMark Bloch 		mlx5_core_dealloc_q_counter(dev->mdev,
3572e1f24a79SParav Pandit 					    dev->port[i].cnts.set_id);
3573e1f24a79SParav Pandit 		kfree(dev->port[i].cnts.names);
3574e1f24a79SParav Pandit 		kfree(dev->port[i].cnts.offsets);
35757c16f477SKamal Heib 	}
35767c16f477SKamal Heib }
35777c16f477SKamal Heib 
3578e1f24a79SParav Pandit static int __mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev,
3579e1f24a79SParav Pandit 				    struct mlx5_ib_counters *cnts)
35807c16f477SKamal Heib {
35817c16f477SKamal Heib 	u32 num_counters;
35827c16f477SKamal Heib 
35837c16f477SKamal Heib 	num_counters = ARRAY_SIZE(basic_q_cnts);
35847c16f477SKamal Heib 
35857c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt))
35867c16f477SKamal Heib 		num_counters += ARRAY_SIZE(out_of_seq_q_cnts);
35877c16f477SKamal Heib 
35887c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters))
35897c16f477SKamal Heib 		num_counters += ARRAY_SIZE(retrans_q_cnts);
359058dcb60aSParav Pandit 
359158dcb60aSParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters))
359258dcb60aSParav Pandit 		num_counters += ARRAY_SIZE(extended_err_cnts);
359358dcb60aSParav Pandit 
3594e1f24a79SParav Pandit 	cnts->num_q_counters = num_counters;
35957c16f477SKamal Heib 
3596e1f24a79SParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
3597e1f24a79SParav Pandit 		cnts->num_cong_counters = ARRAY_SIZE(cong_cnts);
3598e1f24a79SParav Pandit 		num_counters += ARRAY_SIZE(cong_cnts);
3599e1f24a79SParav Pandit 	}
3600e1f24a79SParav Pandit 
3601e1f24a79SParav Pandit 	cnts->names = kcalloc(num_counters, sizeof(cnts->names), GFP_KERNEL);
3602e1f24a79SParav Pandit 	if (!cnts->names)
36037c16f477SKamal Heib 		return -ENOMEM;
36047c16f477SKamal Heib 
3605e1f24a79SParav Pandit 	cnts->offsets = kcalloc(num_counters,
3606e1f24a79SParav Pandit 				sizeof(cnts->offsets), GFP_KERNEL);
3607e1f24a79SParav Pandit 	if (!cnts->offsets)
36087c16f477SKamal Heib 		goto err_names;
36097c16f477SKamal Heib 
36107c16f477SKamal Heib 	return 0;
36117c16f477SKamal Heib 
36127c16f477SKamal Heib err_names:
3613e1f24a79SParav Pandit 	kfree(cnts->names);
36147c16f477SKamal Heib 	return -ENOMEM;
36157c16f477SKamal Heib }
36167c16f477SKamal Heib 
3617e1f24a79SParav Pandit static void mlx5_ib_fill_counters(struct mlx5_ib_dev *dev,
36187c16f477SKamal Heib 				  const char **names,
36197c16f477SKamal Heib 				  size_t *offsets)
36207c16f477SKamal Heib {
36217c16f477SKamal Heib 	int i;
36227c16f477SKamal Heib 	int j = 0;
36237c16f477SKamal Heib 
36247c16f477SKamal Heib 	for (i = 0; i < ARRAY_SIZE(basic_q_cnts); i++, j++) {
36257c16f477SKamal Heib 		names[j] = basic_q_cnts[i].name;
36267c16f477SKamal Heib 		offsets[j] = basic_q_cnts[i].offset;
36277c16f477SKamal Heib 	}
36287c16f477SKamal Heib 
36297c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt)) {
36307c16f477SKamal Heib 		for (i = 0; i < ARRAY_SIZE(out_of_seq_q_cnts); i++, j++) {
36317c16f477SKamal Heib 			names[j] = out_of_seq_q_cnts[i].name;
36327c16f477SKamal Heib 			offsets[j] = out_of_seq_q_cnts[i].offset;
36337c16f477SKamal Heib 		}
36347c16f477SKamal Heib 	}
36357c16f477SKamal Heib 
36367c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters)) {
36377c16f477SKamal Heib 		for (i = 0; i < ARRAY_SIZE(retrans_q_cnts); i++, j++) {
36387c16f477SKamal Heib 			names[j] = retrans_q_cnts[i].name;
36397c16f477SKamal Heib 			offsets[j] = retrans_q_cnts[i].offset;
36407c16f477SKamal Heib 		}
36417c16f477SKamal Heib 	}
3642e1f24a79SParav Pandit 
364358dcb60aSParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters)) {
364458dcb60aSParav Pandit 		for (i = 0; i < ARRAY_SIZE(extended_err_cnts); i++, j++) {
364558dcb60aSParav Pandit 			names[j] = extended_err_cnts[i].name;
364658dcb60aSParav Pandit 			offsets[j] = extended_err_cnts[i].offset;
364758dcb60aSParav Pandit 		}
364858dcb60aSParav Pandit 	}
364958dcb60aSParav Pandit 
3650e1f24a79SParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
3651e1f24a79SParav Pandit 		for (i = 0; i < ARRAY_SIZE(cong_cnts); i++, j++) {
3652e1f24a79SParav Pandit 			names[j] = cong_cnts[i].name;
3653e1f24a79SParav Pandit 			offsets[j] = cong_cnts[i].offset;
3654e1f24a79SParav Pandit 		}
3655e1f24a79SParav Pandit 	}
36560837e86aSMark Bloch }
36570837e86aSMark Bloch 
3658e1f24a79SParav Pandit static int mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev)
36590837e86aSMark Bloch {
36600837e86aSMark Bloch 	int i;
36610837e86aSMark Bloch 	int ret;
36620837e86aSMark Bloch 
36630837e86aSMark Bloch 	for (i = 0; i < dev->num_ports; i++) {
36647c16f477SKamal Heib 		struct mlx5_ib_port *port = &dev->port[i];
36657c16f477SKamal Heib 
36660837e86aSMark Bloch 		ret = mlx5_core_alloc_q_counter(dev->mdev,
3667e1f24a79SParav Pandit 						&port->cnts.set_id);
36680837e86aSMark Bloch 		if (ret) {
36690837e86aSMark Bloch 			mlx5_ib_warn(dev,
36700837e86aSMark Bloch 				     "couldn't allocate queue counter for port %d, err %d\n",
36710837e86aSMark Bloch 				     i + 1, ret);
36720837e86aSMark Bloch 			goto dealloc_counters;
36730837e86aSMark Bloch 		}
36747c16f477SKamal Heib 
3675e1f24a79SParav Pandit 		ret = __mlx5_ib_alloc_counters(dev, &port->cnts);
36767c16f477SKamal Heib 		if (ret)
36777c16f477SKamal Heib 			goto dealloc_counters;
36787c16f477SKamal Heib 
3679e1f24a79SParav Pandit 		mlx5_ib_fill_counters(dev, port->cnts.names,
3680e1f24a79SParav Pandit 				      port->cnts.offsets);
36810837e86aSMark Bloch 	}
36820837e86aSMark Bloch 
36830837e86aSMark Bloch 	return 0;
36840837e86aSMark Bloch 
36850837e86aSMark Bloch dealloc_counters:
36860837e86aSMark Bloch 	while (--i >= 0)
36870837e86aSMark Bloch 		mlx5_core_dealloc_q_counter(dev->mdev,
3688e1f24a79SParav Pandit 					    dev->port[i].cnts.set_id);
36890837e86aSMark Bloch 
36900837e86aSMark Bloch 	return ret;
36910837e86aSMark Bloch }
36920837e86aSMark Bloch 
36930ad17a8fSMark Bloch static struct rdma_hw_stats *mlx5_ib_alloc_hw_stats(struct ib_device *ibdev,
36940ad17a8fSMark Bloch 						    u8 port_num)
36950ad17a8fSMark Bloch {
36967c16f477SKamal Heib 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
36977c16f477SKamal Heib 	struct mlx5_ib_port *port = &dev->port[port_num - 1];
36980ad17a8fSMark Bloch 
36990ad17a8fSMark Bloch 	/* We support only per port stats */
37000ad17a8fSMark Bloch 	if (port_num == 0)
37010ad17a8fSMark Bloch 		return NULL;
37020ad17a8fSMark Bloch 
3703e1f24a79SParav Pandit 	return rdma_alloc_hw_stats_struct(port->cnts.names,
3704e1f24a79SParav Pandit 					  port->cnts.num_q_counters +
3705e1f24a79SParav Pandit 					  port->cnts.num_cong_counters,
37060ad17a8fSMark Bloch 					  RDMA_HW_STATS_DEFAULT_LIFESPAN);
37070ad17a8fSMark Bloch }
37080ad17a8fSMark Bloch 
3709e1f24a79SParav Pandit static int mlx5_ib_query_q_counters(struct mlx5_ib_dev *dev,
3710e1f24a79SParav Pandit 				    struct mlx5_ib_port *port,
3711e1f24a79SParav Pandit 				    struct rdma_hw_stats *stats)
3712e1f24a79SParav Pandit {
3713e1f24a79SParav Pandit 	int outlen = MLX5_ST_SZ_BYTES(query_q_counter_out);
3714e1f24a79SParav Pandit 	void *out;
3715e1f24a79SParav Pandit 	__be32 val;
3716e1f24a79SParav Pandit 	int ret, i;
3717e1f24a79SParav Pandit 
37181b9a07eeSLeon Romanovsky 	out = kvzalloc(outlen, GFP_KERNEL);
3719e1f24a79SParav Pandit 	if (!out)
3720e1f24a79SParav Pandit 		return -ENOMEM;
3721e1f24a79SParav Pandit 
3722e1f24a79SParav Pandit 	ret = mlx5_core_query_q_counter(dev->mdev,
3723e1f24a79SParav Pandit 					port->cnts.set_id, 0,
3724e1f24a79SParav Pandit 					out, outlen);
3725e1f24a79SParav Pandit 	if (ret)
3726e1f24a79SParav Pandit 		goto free;
3727e1f24a79SParav Pandit 
3728e1f24a79SParav Pandit 	for (i = 0; i < port->cnts.num_q_counters; i++) {
3729e1f24a79SParav Pandit 		val = *(__be32 *)(out + port->cnts.offsets[i]);
3730e1f24a79SParav Pandit 		stats->value[i] = (u64)be32_to_cpu(val);
3731e1f24a79SParav Pandit 	}
3732e1f24a79SParav Pandit 
3733e1f24a79SParav Pandit free:
3734e1f24a79SParav Pandit 	kvfree(out);
3735e1f24a79SParav Pandit 	return ret;
3736e1f24a79SParav Pandit }
3737e1f24a79SParav Pandit 
3738e1f24a79SParav Pandit static int mlx5_ib_query_cong_counters(struct mlx5_ib_dev *dev,
3739e1f24a79SParav Pandit 				       struct mlx5_ib_port *port,
3740e1f24a79SParav Pandit 				       struct rdma_hw_stats *stats)
3741e1f24a79SParav Pandit {
3742e1f24a79SParav Pandit 	int outlen = MLX5_ST_SZ_BYTES(query_cong_statistics_out);
3743e1f24a79SParav Pandit 	void *out;
3744e1f24a79SParav Pandit 	int ret, i;
3745e1f24a79SParav Pandit 	int offset = port->cnts.num_q_counters;
3746e1f24a79SParav Pandit 
37471b9a07eeSLeon Romanovsky 	out = kvzalloc(outlen, GFP_KERNEL);
3748e1f24a79SParav Pandit 	if (!out)
3749e1f24a79SParav Pandit 		return -ENOMEM;
3750e1f24a79SParav Pandit 
3751e1f24a79SParav Pandit 	ret = mlx5_cmd_query_cong_counter(dev->mdev, false, out, outlen);
3752e1f24a79SParav Pandit 	if (ret)
3753e1f24a79SParav Pandit 		goto free;
3754e1f24a79SParav Pandit 
3755e1f24a79SParav Pandit 	for (i = 0; i < port->cnts.num_cong_counters; i++) {
3756e1f24a79SParav Pandit 		stats->value[i + offset] =
3757e1f24a79SParav Pandit 			be64_to_cpup((__be64 *)(out +
3758e1f24a79SParav Pandit 				     port->cnts.offsets[i + offset]));
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)) {
3783e1f24a79SParav Pandit 		ret = mlx5_ib_query_cong_counters(dev, port, stats);
3784e1f24a79SParav Pandit 		if (ret)
3785e1f24a79SParav Pandit 			return ret;
3786e1f24a79SParav Pandit 		num_counters += port->cnts.num_cong_counters;
37870ad17a8fSMark Bloch 	}
37887c16f477SKamal Heib 
3789e1f24a79SParav Pandit 	return num_counters;
37900ad17a8fSMark Bloch }
37910ad17a8fSMark Bloch 
37928e959601SNiranjana Vishwanathapura static void mlx5_ib_free_rdma_netdev(struct net_device *netdev)
37938e959601SNiranjana Vishwanathapura {
37948e959601SNiranjana Vishwanathapura 	return mlx5_rdma_netdev_free(netdev);
37958e959601SNiranjana Vishwanathapura }
37968e959601SNiranjana Vishwanathapura 
3797693dfd5aSErez Shitrit static struct net_device*
3798693dfd5aSErez Shitrit mlx5_ib_alloc_rdma_netdev(struct ib_device *hca,
3799693dfd5aSErez Shitrit 			  u8 port_num,
3800693dfd5aSErez Shitrit 			  enum rdma_netdev_t type,
3801693dfd5aSErez Shitrit 			  const char *name,
3802693dfd5aSErez Shitrit 			  unsigned char name_assign_type,
3803693dfd5aSErez Shitrit 			  void (*setup)(struct net_device *))
3804693dfd5aSErez Shitrit {
38058e959601SNiranjana Vishwanathapura 	struct net_device *netdev;
38068e959601SNiranjana Vishwanathapura 	struct rdma_netdev *rn;
38078e959601SNiranjana Vishwanathapura 
3808693dfd5aSErez Shitrit 	if (type != RDMA_NETDEV_IPOIB)
3809693dfd5aSErez Shitrit 		return ERR_PTR(-EOPNOTSUPP);
3810693dfd5aSErez Shitrit 
38118e959601SNiranjana Vishwanathapura 	netdev = mlx5_rdma_netdev_alloc(to_mdev(hca)->mdev, hca,
3812693dfd5aSErez Shitrit 					name, setup);
38138e959601SNiranjana Vishwanathapura 	if (likely(!IS_ERR_OR_NULL(netdev))) {
38148e959601SNiranjana Vishwanathapura 		rn = netdev_priv(netdev);
38158e959601SNiranjana Vishwanathapura 		rn->free_rdma_netdev = mlx5_ib_free_rdma_netdev;
3816693dfd5aSErez Shitrit 	}
38178e959601SNiranjana Vishwanathapura 	return netdev;
3818693dfd5aSErez Shitrit }
3819693dfd5aSErez Shitrit 
3820fe248c3aSMaor Gottlieb static void delay_drop_debugfs_cleanup(struct mlx5_ib_dev *dev)
3821fe248c3aSMaor Gottlieb {
3822fe248c3aSMaor Gottlieb 	if (!dev->delay_drop.dbg)
3823fe248c3aSMaor Gottlieb 		return;
3824fe248c3aSMaor Gottlieb 	debugfs_remove_recursive(dev->delay_drop.dbg->dir_debugfs);
3825fe248c3aSMaor Gottlieb 	kfree(dev->delay_drop.dbg);
3826fe248c3aSMaor Gottlieb 	dev->delay_drop.dbg = NULL;
3827fe248c3aSMaor Gottlieb }
3828fe248c3aSMaor Gottlieb 
382903404e8aSMaor Gottlieb static void cancel_delay_drop(struct mlx5_ib_dev *dev)
383003404e8aSMaor Gottlieb {
383103404e8aSMaor Gottlieb 	if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
383203404e8aSMaor Gottlieb 		return;
383303404e8aSMaor Gottlieb 
383403404e8aSMaor Gottlieb 	cancel_work_sync(&dev->delay_drop.delay_drop_work);
3835fe248c3aSMaor Gottlieb 	delay_drop_debugfs_cleanup(dev);
3836fe248c3aSMaor Gottlieb }
3837fe248c3aSMaor Gottlieb 
3838fe248c3aSMaor Gottlieb static ssize_t delay_drop_timeout_read(struct file *filp, char __user *buf,
3839fe248c3aSMaor Gottlieb 				       size_t count, loff_t *pos)
3840fe248c3aSMaor Gottlieb {
3841fe248c3aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
3842fe248c3aSMaor Gottlieb 	char lbuf[20];
3843fe248c3aSMaor Gottlieb 	int len;
3844fe248c3aSMaor Gottlieb 
3845fe248c3aSMaor Gottlieb 	len = snprintf(lbuf, sizeof(lbuf), "%u\n", delay_drop->timeout);
3846fe248c3aSMaor Gottlieb 	return simple_read_from_buffer(buf, count, pos, lbuf, len);
3847fe248c3aSMaor Gottlieb }
3848fe248c3aSMaor Gottlieb 
3849fe248c3aSMaor Gottlieb static ssize_t delay_drop_timeout_write(struct file *filp, const char __user *buf,
3850fe248c3aSMaor Gottlieb 					size_t count, loff_t *pos)
3851fe248c3aSMaor Gottlieb {
3852fe248c3aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
3853fe248c3aSMaor Gottlieb 	u32 timeout;
3854fe248c3aSMaor Gottlieb 	u32 var;
3855fe248c3aSMaor Gottlieb 
3856fe248c3aSMaor Gottlieb 	if (kstrtouint_from_user(buf, count, 0, &var))
3857fe248c3aSMaor Gottlieb 		return -EFAULT;
3858fe248c3aSMaor Gottlieb 
3859fe248c3aSMaor Gottlieb 	timeout = min_t(u32, roundup(var, 100), MLX5_MAX_DELAY_DROP_TIMEOUT_MS *
3860fe248c3aSMaor Gottlieb 			1000);
3861fe248c3aSMaor Gottlieb 	if (timeout != var)
3862fe248c3aSMaor Gottlieb 		mlx5_ib_dbg(delay_drop->dev, "Round delay drop timeout to %u usec\n",
3863fe248c3aSMaor Gottlieb 			    timeout);
3864fe248c3aSMaor Gottlieb 
3865fe248c3aSMaor Gottlieb 	delay_drop->timeout = timeout;
3866fe248c3aSMaor Gottlieb 
3867fe248c3aSMaor Gottlieb 	return count;
3868fe248c3aSMaor Gottlieb }
3869fe248c3aSMaor Gottlieb 
3870fe248c3aSMaor Gottlieb static const struct file_operations fops_delay_drop_timeout = {
3871fe248c3aSMaor Gottlieb 	.owner	= THIS_MODULE,
3872fe248c3aSMaor Gottlieb 	.open	= simple_open,
3873fe248c3aSMaor Gottlieb 	.write	= delay_drop_timeout_write,
3874fe248c3aSMaor Gottlieb 	.read	= delay_drop_timeout_read,
3875fe248c3aSMaor Gottlieb };
3876fe248c3aSMaor Gottlieb 
3877fe248c3aSMaor Gottlieb static int delay_drop_debugfs_init(struct mlx5_ib_dev *dev)
3878fe248c3aSMaor Gottlieb {
3879fe248c3aSMaor Gottlieb 	struct mlx5_ib_dbg_delay_drop *dbg;
3880fe248c3aSMaor Gottlieb 
3881fe248c3aSMaor Gottlieb 	if (!mlx5_debugfs_root)
3882fe248c3aSMaor Gottlieb 		return 0;
3883fe248c3aSMaor Gottlieb 
3884fe248c3aSMaor Gottlieb 	dbg = kzalloc(sizeof(*dbg), GFP_KERNEL);
3885fe248c3aSMaor Gottlieb 	if (!dbg)
3886fe248c3aSMaor Gottlieb 		return -ENOMEM;
3887fe248c3aSMaor Gottlieb 
3888cbafad87SSudip Mukherjee 	dev->delay_drop.dbg = dbg;
3889cbafad87SSudip Mukherjee 
3890fe248c3aSMaor Gottlieb 	dbg->dir_debugfs =
3891fe248c3aSMaor Gottlieb 		debugfs_create_dir("delay_drop",
3892fe248c3aSMaor Gottlieb 				   dev->mdev->priv.dbg_root);
3893fe248c3aSMaor Gottlieb 	if (!dbg->dir_debugfs)
3894cbafad87SSudip Mukherjee 		goto out_debugfs;
3895fe248c3aSMaor Gottlieb 
3896fe248c3aSMaor Gottlieb 	dbg->events_cnt_debugfs =
3897fe248c3aSMaor Gottlieb 		debugfs_create_atomic_t("num_timeout_events", 0400,
3898fe248c3aSMaor Gottlieb 					dbg->dir_debugfs,
3899fe248c3aSMaor Gottlieb 					&dev->delay_drop.events_cnt);
3900fe248c3aSMaor Gottlieb 	if (!dbg->events_cnt_debugfs)
3901fe248c3aSMaor Gottlieb 		goto out_debugfs;
3902fe248c3aSMaor Gottlieb 
3903fe248c3aSMaor Gottlieb 	dbg->rqs_cnt_debugfs =
3904fe248c3aSMaor Gottlieb 		debugfs_create_atomic_t("num_rqs", 0400,
3905fe248c3aSMaor Gottlieb 					dbg->dir_debugfs,
3906fe248c3aSMaor Gottlieb 					&dev->delay_drop.rqs_cnt);
3907fe248c3aSMaor Gottlieb 	if (!dbg->rqs_cnt_debugfs)
3908fe248c3aSMaor Gottlieb 		goto out_debugfs;
3909fe248c3aSMaor Gottlieb 
3910fe248c3aSMaor Gottlieb 	dbg->timeout_debugfs =
3911fe248c3aSMaor Gottlieb 		debugfs_create_file("timeout", 0600,
3912fe248c3aSMaor Gottlieb 				    dbg->dir_debugfs,
3913fe248c3aSMaor Gottlieb 				    &dev->delay_drop,
3914fe248c3aSMaor Gottlieb 				    &fops_delay_drop_timeout);
3915fe248c3aSMaor Gottlieb 	if (!dbg->timeout_debugfs)
3916fe248c3aSMaor Gottlieb 		goto out_debugfs;
3917fe248c3aSMaor Gottlieb 
3918fe248c3aSMaor Gottlieb 	return 0;
3919fe248c3aSMaor Gottlieb 
3920fe248c3aSMaor Gottlieb out_debugfs:
3921fe248c3aSMaor Gottlieb 	delay_drop_debugfs_cleanup(dev);
3922fe248c3aSMaor Gottlieb 	return -ENOMEM;
392303404e8aSMaor Gottlieb }
392403404e8aSMaor Gottlieb 
392503404e8aSMaor Gottlieb static void init_delay_drop(struct mlx5_ib_dev *dev)
392603404e8aSMaor Gottlieb {
392703404e8aSMaor Gottlieb 	if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
392803404e8aSMaor Gottlieb 		return;
392903404e8aSMaor Gottlieb 
393003404e8aSMaor Gottlieb 	mutex_init(&dev->delay_drop.lock);
393103404e8aSMaor Gottlieb 	dev->delay_drop.dev = dev;
393203404e8aSMaor Gottlieb 	dev->delay_drop.activate = false;
393303404e8aSMaor Gottlieb 	dev->delay_drop.timeout = MLX5_MAX_DELAY_DROP_TIMEOUT_MS * 1000;
393403404e8aSMaor Gottlieb 	INIT_WORK(&dev->delay_drop.delay_drop_work, delay_drop_handler);
3935fe248c3aSMaor Gottlieb 	atomic_set(&dev->delay_drop.rqs_cnt, 0);
3936fe248c3aSMaor Gottlieb 	atomic_set(&dev->delay_drop.events_cnt, 0);
3937fe248c3aSMaor Gottlieb 
3938fe248c3aSMaor Gottlieb 	if (delay_drop_debugfs_init(dev))
3939fe248c3aSMaor Gottlieb 		mlx5_ib_warn(dev, "Failed to init delay drop debugfs\n");
394003404e8aSMaor Gottlieb }
394103404e8aSMaor Gottlieb 
394284305d71SLeon Romanovsky static const struct cpumask *
394384305d71SLeon Romanovsky mlx5_ib_get_vector_affinity(struct ib_device *ibdev, int comp_vector)
394440b24403SSagi Grimberg {
394540b24403SSagi Grimberg 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
394640b24403SSagi Grimberg 
394740b24403SSagi Grimberg 	return mlx5_get_vector_affinity(dev->mdev, comp_vector);
394840b24403SSagi Grimberg }
394940b24403SSagi Grimberg 
39509603b61dSJack Morgenstein static void *mlx5_ib_add(struct mlx5_core_dev *mdev)
3951e126ba97SEli Cohen {
3952e126ba97SEli Cohen 	struct mlx5_ib_dev *dev;
3953ebd61f68SAchiad Shochat 	enum rdma_link_layer ll;
3954ebd61f68SAchiad Shochat 	int port_type_cap;
39554babcf97SAviv Heller 	const char *name;
3956e126ba97SEli Cohen 	int err;
3957e126ba97SEli Cohen 	int i;
3958e126ba97SEli Cohen 
3959ebd61f68SAchiad Shochat 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
3960ebd61f68SAchiad Shochat 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
3961ebd61f68SAchiad Shochat 
3962e126ba97SEli Cohen 	printk_once(KERN_INFO "%s", mlx5_version);
3963e126ba97SEli Cohen 
3964e126ba97SEli Cohen 	dev = (struct mlx5_ib_dev *)ib_alloc_device(sizeof(*dev));
3965e126ba97SEli Cohen 	if (!dev)
39669603b61dSJack Morgenstein 		return NULL;
3967e126ba97SEli Cohen 
39689603b61dSJack Morgenstein 	dev->mdev = mdev;
3969e126ba97SEli Cohen 
39700837e86aSMark Bloch 	dev->port = kcalloc(MLX5_CAP_GEN(mdev, num_ports), sizeof(*dev->port),
39710837e86aSMark Bloch 			    GFP_KERNEL);
39720837e86aSMark Bloch 	if (!dev->port)
39730837e86aSMark Bloch 		goto err_dealloc;
39740837e86aSMark Bloch 
3975fc24fc5eSAchiad Shochat 	rwlock_init(&dev->roce.netdev_lock);
3976e126ba97SEli Cohen 	err = get_port_caps(dev);
3977e126ba97SEli Cohen 	if (err)
39780837e86aSMark Bloch 		goto err_free_port;
3979e126ba97SEli Cohen 
39801b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(dev))
3981e126ba97SEli Cohen 		get_ext_port_caps(dev);
3982e126ba97SEli Cohen 
39834babcf97SAviv Heller 	if (!mlx5_lag_is_active(mdev))
39844babcf97SAviv Heller 		name = "mlx5_%d";
39854babcf97SAviv Heller 	else
39864babcf97SAviv Heller 		name = "mlx5_bond_%d";
39874babcf97SAviv Heller 
39884babcf97SAviv Heller 	strlcpy(dev->ib_dev.name, name, IB_DEVICE_NAME_MAX);
3989e126ba97SEli Cohen 	dev->ib_dev.owner		= THIS_MODULE;
3990e126ba97SEli Cohen 	dev->ib_dev.node_type		= RDMA_NODE_IB_CA;
3991c6790aa9SSagi Grimberg 	dev->ib_dev.local_dma_lkey	= 0 /* not supported for now */;
3992938fe83cSSaeed Mahameed 	dev->num_ports		= MLX5_CAP_GEN(mdev, num_ports);
3993e126ba97SEli Cohen 	dev->ib_dev.phys_port_cnt     = dev->num_ports;
3994233d05d2SSaeed Mahameed 	dev->ib_dev.num_comp_vectors    =
3995233d05d2SSaeed Mahameed 		dev->mdev->priv.eq_table.num_comp_vectors;
39969b0c289eSBart Van Assche 	dev->ib_dev.dev.parent		= &mdev->pdev->dev;
3997e126ba97SEli Cohen 
3998e126ba97SEli Cohen 	dev->ib_dev.uverbs_abi_ver	= MLX5_IB_UVERBS_ABI_VERSION;
3999e126ba97SEli Cohen 	dev->ib_dev.uverbs_cmd_mask	=
4000e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_GET_CONTEXT)		|
4001e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_DEVICE)	|
4002e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_PORT)		|
4003e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_ALLOC_PD)		|
4004e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DEALLOC_PD)		|
400541c450fdSMoni Shoua 		(1ull << IB_USER_VERBS_CMD_CREATE_AH)		|
400641c450fdSMoni Shoua 		(1ull << IB_USER_VERBS_CMD_DESTROY_AH)		|
4007e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_REG_MR)		|
400856e11d62SNoa Osherovich 		(1ull << IB_USER_VERBS_CMD_REREG_MR)		|
4009e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DEREG_MR)		|
4010e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL)	|
4011e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_CQ)		|
4012e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_RESIZE_CQ)		|
4013e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_CQ)		|
4014e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_QP)		|
4015e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_MODIFY_QP)		|
4016e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_QP)		|
4017e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_QP)		|
4018e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_ATTACH_MCAST)	|
4019e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DETACH_MCAST)	|
4020e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_SRQ)		|
4021e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_MODIFY_SRQ)		|
4022e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_SRQ)		|
4023e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_SRQ)		|
4024e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_XSRQ)		|
4025e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_OPEN_QP);
40261707cb4aSHaggai Eran 	dev->ib_dev.uverbs_ex_cmd_mask =
4027d4584ddfSMatan Barak 		(1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE)	|
4028d4584ddfSMatan Barak 		(1ull << IB_USER_VERBS_EX_CMD_CREATE_CQ)	|
40297d29f349SBodong Wang 		(1ull << IB_USER_VERBS_EX_CMD_CREATE_QP)	|
4030b0e9df6dSYonatan Cohen 		(1ull << IB_USER_VERBS_EX_CMD_MODIFY_QP)	|
4031b0e9df6dSYonatan Cohen 		(1ull << IB_USER_VERBS_EX_CMD_MODIFY_CQ);
4032e126ba97SEli Cohen 
4033e126ba97SEli Cohen 	dev->ib_dev.query_device	= mlx5_ib_query_device;
4034e126ba97SEli Cohen 	dev->ib_dev.query_port		= mlx5_ib_query_port;
4035ebd61f68SAchiad Shochat 	dev->ib_dev.get_link_layer	= mlx5_ib_port_link_layer;
4036fc24fc5eSAchiad Shochat 	if (ll == IB_LINK_LAYER_ETHERNET)
4037fc24fc5eSAchiad Shochat 		dev->ib_dev.get_netdev	= mlx5_ib_get_netdev;
4038e126ba97SEli Cohen 	dev->ib_dev.query_gid		= mlx5_ib_query_gid;
40393cca2606SAchiad Shochat 	dev->ib_dev.add_gid		= mlx5_ib_add_gid;
40403cca2606SAchiad Shochat 	dev->ib_dev.del_gid		= mlx5_ib_del_gid;
4041e126ba97SEli Cohen 	dev->ib_dev.query_pkey		= mlx5_ib_query_pkey;
4042e126ba97SEli Cohen 	dev->ib_dev.modify_device	= mlx5_ib_modify_device;
4043e126ba97SEli Cohen 	dev->ib_dev.modify_port		= mlx5_ib_modify_port;
4044e126ba97SEli Cohen 	dev->ib_dev.alloc_ucontext	= mlx5_ib_alloc_ucontext;
4045e126ba97SEli Cohen 	dev->ib_dev.dealloc_ucontext	= mlx5_ib_dealloc_ucontext;
4046e126ba97SEli Cohen 	dev->ib_dev.mmap		= mlx5_ib_mmap;
4047e126ba97SEli Cohen 	dev->ib_dev.alloc_pd		= mlx5_ib_alloc_pd;
4048e126ba97SEli Cohen 	dev->ib_dev.dealloc_pd		= mlx5_ib_dealloc_pd;
4049e126ba97SEli Cohen 	dev->ib_dev.create_ah		= mlx5_ib_create_ah;
4050e126ba97SEli Cohen 	dev->ib_dev.query_ah		= mlx5_ib_query_ah;
4051e126ba97SEli Cohen 	dev->ib_dev.destroy_ah		= mlx5_ib_destroy_ah;
4052e126ba97SEli Cohen 	dev->ib_dev.create_srq		= mlx5_ib_create_srq;
4053e126ba97SEli Cohen 	dev->ib_dev.modify_srq		= mlx5_ib_modify_srq;
4054e126ba97SEli Cohen 	dev->ib_dev.query_srq		= mlx5_ib_query_srq;
4055e126ba97SEli Cohen 	dev->ib_dev.destroy_srq		= mlx5_ib_destroy_srq;
4056e126ba97SEli Cohen 	dev->ib_dev.post_srq_recv	= mlx5_ib_post_srq_recv;
4057e126ba97SEli Cohen 	dev->ib_dev.create_qp		= mlx5_ib_create_qp;
4058e126ba97SEli Cohen 	dev->ib_dev.modify_qp		= mlx5_ib_modify_qp;
4059e126ba97SEli Cohen 	dev->ib_dev.query_qp		= mlx5_ib_query_qp;
4060e126ba97SEli Cohen 	dev->ib_dev.destroy_qp		= mlx5_ib_destroy_qp;
4061e126ba97SEli Cohen 	dev->ib_dev.post_send		= mlx5_ib_post_send;
4062e126ba97SEli Cohen 	dev->ib_dev.post_recv		= mlx5_ib_post_recv;
4063e126ba97SEli Cohen 	dev->ib_dev.create_cq		= mlx5_ib_create_cq;
4064e126ba97SEli Cohen 	dev->ib_dev.modify_cq		= mlx5_ib_modify_cq;
4065e126ba97SEli Cohen 	dev->ib_dev.resize_cq		= mlx5_ib_resize_cq;
4066e126ba97SEli Cohen 	dev->ib_dev.destroy_cq		= mlx5_ib_destroy_cq;
4067e126ba97SEli Cohen 	dev->ib_dev.poll_cq		= mlx5_ib_poll_cq;
4068e126ba97SEli Cohen 	dev->ib_dev.req_notify_cq	= mlx5_ib_arm_cq;
4069e126ba97SEli Cohen 	dev->ib_dev.get_dma_mr		= mlx5_ib_get_dma_mr;
4070e126ba97SEli Cohen 	dev->ib_dev.reg_user_mr		= mlx5_ib_reg_user_mr;
407156e11d62SNoa Osherovich 	dev->ib_dev.rereg_user_mr	= mlx5_ib_rereg_user_mr;
4072e126ba97SEli Cohen 	dev->ib_dev.dereg_mr		= mlx5_ib_dereg_mr;
4073e126ba97SEli Cohen 	dev->ib_dev.attach_mcast	= mlx5_ib_mcg_attach;
4074e126ba97SEli Cohen 	dev->ib_dev.detach_mcast	= mlx5_ib_mcg_detach;
4075e126ba97SEli Cohen 	dev->ib_dev.process_mad		= mlx5_ib_process_mad;
40769bee178bSSagi Grimberg 	dev->ib_dev.alloc_mr		= mlx5_ib_alloc_mr;
40778a187ee5SSagi Grimberg 	dev->ib_dev.map_mr_sg		= mlx5_ib_map_mr_sg;
4078d5436ba0SSagi Grimberg 	dev->ib_dev.check_mr_status	= mlx5_ib_check_mr_status;
40797738613eSIra Weiny 	dev->ib_dev.get_port_immutable  = mlx5_port_immutable;
4080c7342823SIra Weiny 	dev->ib_dev.get_dev_fw_str      = get_dev_fw_str;
408140b24403SSagi Grimberg 	dev->ib_dev.get_vector_affinity	= mlx5_ib_get_vector_affinity;
40828e959601SNiranjana Vishwanathapura 	if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads))
4083693dfd5aSErez Shitrit 		dev->ib_dev.alloc_rdma_netdev	= mlx5_ib_alloc_rdma_netdev;
40848e959601SNiranjana Vishwanathapura 
4085eff901d3SEli Cohen 	if (mlx5_core_is_pf(mdev)) {
4086eff901d3SEli Cohen 		dev->ib_dev.get_vf_config	= mlx5_ib_get_vf_config;
4087eff901d3SEli Cohen 		dev->ib_dev.set_vf_link_state	= mlx5_ib_set_vf_link_state;
4088eff901d3SEli Cohen 		dev->ib_dev.get_vf_stats	= mlx5_ib_get_vf_stats;
4089eff901d3SEli Cohen 		dev->ib_dev.set_vf_guid		= mlx5_ib_set_vf_guid;
4090eff901d3SEli Cohen 	}
4091e126ba97SEli Cohen 
40927c2344c3SMaor Gottlieb 	dev->ib_dev.disassociate_ucontext = mlx5_ib_disassociate_ucontext;
40937c2344c3SMaor Gottlieb 
4094938fe83cSSaeed Mahameed 	mlx5_ib_internal_fill_odp_caps(dev);
40958cdd312cSHaggai Eran 
40966e8484c5SMax Gurtovoy 	dev->umr_fence = mlx5_get_umr_fence(MLX5_CAP_GEN(mdev, umr_fence));
40976e8484c5SMax Gurtovoy 
4098d2370e0aSMatan Barak 	if (MLX5_CAP_GEN(mdev, imaicl)) {
4099d2370e0aSMatan Barak 		dev->ib_dev.alloc_mw		= mlx5_ib_alloc_mw;
4100d2370e0aSMatan Barak 		dev->ib_dev.dealloc_mw		= mlx5_ib_dealloc_mw;
4101d2370e0aSMatan Barak 		dev->ib_dev.uverbs_cmd_mask |=
4102d2370e0aSMatan Barak 			(1ull << IB_USER_VERBS_CMD_ALLOC_MW)	|
4103d2370e0aSMatan Barak 			(1ull << IB_USER_VERBS_CMD_DEALLOC_MW);
4104d2370e0aSMatan Barak 	}
4105d2370e0aSMatan Barak 
41067c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt)) {
41070ad17a8fSMark Bloch 		dev->ib_dev.get_hw_stats	= mlx5_ib_get_hw_stats;
41080ad17a8fSMark Bloch 		dev->ib_dev.alloc_hw_stats	= mlx5_ib_alloc_hw_stats;
41090ad17a8fSMark Bloch 	}
41100ad17a8fSMark Bloch 
4111938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, xrc)) {
4112e126ba97SEli Cohen 		dev->ib_dev.alloc_xrcd = mlx5_ib_alloc_xrcd;
4113e126ba97SEli Cohen 		dev->ib_dev.dealloc_xrcd = mlx5_ib_dealloc_xrcd;
4114e126ba97SEli Cohen 		dev->ib_dev.uverbs_cmd_mask |=
4115e126ba97SEli Cohen 			(1ull << IB_USER_VERBS_CMD_OPEN_XRCD) |
4116e126ba97SEli Cohen 			(1ull << IB_USER_VERBS_CMD_CLOSE_XRCD);
4117e126ba97SEli Cohen 	}
4118e126ba97SEli Cohen 
4119038d2ef8SMaor Gottlieb 	dev->ib_dev.create_flow	= mlx5_ib_create_flow;
4120038d2ef8SMaor Gottlieb 	dev->ib_dev.destroy_flow = mlx5_ib_destroy_flow;
412181e30880SYishai Hadas 	dev->ib_dev.uverbs_ex_cmd_mask |=
412281e30880SYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_CREATE_FLOW) |
412381e30880SYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_DESTROY_FLOW);
412481e30880SYishai Hadas 
412581e30880SYishai Hadas 	if (mlx5_ib_port_link_layer(&dev->ib_dev, 1) ==
412681e30880SYishai Hadas 	    IB_LINK_LAYER_ETHERNET) {
412779b20a6cSYishai Hadas 		dev->ib_dev.create_wq	 = mlx5_ib_create_wq;
412879b20a6cSYishai Hadas 		dev->ib_dev.modify_wq	 = mlx5_ib_modify_wq;
412979b20a6cSYishai Hadas 		dev->ib_dev.destroy_wq	 = mlx5_ib_destroy_wq;
4130c5f90929SYishai Hadas 		dev->ib_dev.create_rwq_ind_table = mlx5_ib_create_rwq_ind_table;
4131c5f90929SYishai Hadas 		dev->ib_dev.destroy_rwq_ind_table = mlx5_ib_destroy_rwq_ind_table;
4132038d2ef8SMaor Gottlieb 		dev->ib_dev.uverbs_ex_cmd_mask |=
413379b20a6cSYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_CREATE_WQ) |
413479b20a6cSYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_MODIFY_WQ) |
4135c5f90929SYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_DESTROY_WQ) |
4136c5f90929SYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_CREATE_RWQ_IND_TBL) |
4137c5f90929SYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_DESTROY_RWQ_IND_TBL);
4138038d2ef8SMaor Gottlieb 	}
4139e126ba97SEli Cohen 	err = init_node_data(dev);
4140e126ba97SEli Cohen 	if (err)
414190be7c8aSMajd Dibbiny 		goto err_free_port;
4142e126ba97SEli Cohen 
4143038d2ef8SMaor Gottlieb 	mutex_init(&dev->flow_db.lock);
4144e126ba97SEli Cohen 	mutex_init(&dev->cap_mask_mutex);
414589ea94a7SMaor Gottlieb 	INIT_LIST_HEAD(&dev->qp_list);
414689ea94a7SMaor Gottlieb 	spin_lock_init(&dev->reset_flow_resource_lock);
4147e126ba97SEli Cohen 
4148fc24fc5eSAchiad Shochat 	if (ll == IB_LINK_LAYER_ETHERNET) {
414945f95acdSOr Gerlitz 		err = mlx5_enable_eth(dev);
4150e126ba97SEli Cohen 		if (err)
415190be7c8aSMajd Dibbiny 			goto err_free_port;
4152fd65f1b8SMoni Shoua 		dev->roce.last_port_state = IB_PORT_DOWN;
4153fc24fc5eSAchiad Shochat 	}
4154fc24fc5eSAchiad Shochat 
4155fc24fc5eSAchiad Shochat 	err = create_dev_resources(&dev->devr);
4156fc24fc5eSAchiad Shochat 	if (err)
415745f95acdSOr Gerlitz 		goto err_disable_eth;
4158e126ba97SEli Cohen 
41596aec21f6SHaggai Eran 	err = mlx5_ib_odp_init_one(dev);
4160281d1a92SWei Yongjun 	if (err)
4161e126ba97SEli Cohen 		goto err_rsrc;
4162e126ba97SEli Cohen 
416345bded2cSKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt)) {
4164e1f24a79SParav Pandit 		err = mlx5_ib_alloc_counters(dev);
41656aec21f6SHaggai Eran 		if (err)
41666aec21f6SHaggai Eran 			goto err_odp;
416745bded2cSKamal Heib 	}
41686aec21f6SHaggai Eran 
41694a2da0b8SParav Pandit 	err = mlx5_ib_init_cong_debugfs(dev);
41704a2da0b8SParav Pandit 	if (err)
41714a2da0b8SParav Pandit 		goto err_cnt;
41724a2da0b8SParav Pandit 
41735fe9dec0SEli Cohen 	dev->mdev->priv.uar = mlx5_get_uars_page(dev->mdev);
41745fe9dec0SEli Cohen 	if (!dev->mdev->priv.uar)
41754a2da0b8SParav Pandit 		goto err_cong;
41765fe9dec0SEli Cohen 
41775fe9dec0SEli Cohen 	err = mlx5_alloc_bfreg(dev->mdev, &dev->bfreg, false, false);
41785fe9dec0SEli Cohen 	if (err)
41795fe9dec0SEli Cohen 		goto err_uar_page;
41805fe9dec0SEli Cohen 
41815fe9dec0SEli Cohen 	err = mlx5_alloc_bfreg(dev->mdev, &dev->fp_bfreg, false, true);
41825fe9dec0SEli Cohen 	if (err)
41835fe9dec0SEli Cohen 		goto err_bfreg;
41845fe9dec0SEli Cohen 
41850837e86aSMark Bloch 	err = ib_register_device(&dev->ib_dev, NULL);
41860837e86aSMark Bloch 	if (err)
41875fe9dec0SEli Cohen 		goto err_fp_bfreg;
41880837e86aSMark Bloch 
4189e126ba97SEli Cohen 	err = create_umr_res(dev);
4190e126ba97SEli Cohen 	if (err)
4191e126ba97SEli Cohen 		goto err_dev;
4192e126ba97SEli Cohen 
419303404e8aSMaor Gottlieb 	init_delay_drop(dev);
419403404e8aSMaor Gottlieb 
4195e126ba97SEli Cohen 	for (i = 0; i < ARRAY_SIZE(mlx5_class_attributes); i++) {
4196281d1a92SWei Yongjun 		err = device_create_file(&dev->ib_dev.dev,
4197281d1a92SWei Yongjun 					 mlx5_class_attributes[i]);
4198281d1a92SWei Yongjun 		if (err)
419903404e8aSMaor Gottlieb 			goto err_delay_drop;
4200e126ba97SEli Cohen 	}
4201e126ba97SEli Cohen 
4202c85023e1SHuy Nguyen 	if ((MLX5_CAP_GEN(mdev, port_type) == MLX5_CAP_PORT_TYPE_ETH) &&
4203c85023e1SHuy Nguyen 	    MLX5_CAP_GEN(mdev, disable_local_lb))
4204c85023e1SHuy Nguyen 		mutex_init(&dev->lb_mutex);
4205c85023e1SHuy Nguyen 
4206e126ba97SEli Cohen 	dev->ib_active = true;
4207e126ba97SEli Cohen 
42089603b61dSJack Morgenstein 	return dev;
4209e126ba97SEli Cohen 
421003404e8aSMaor Gottlieb err_delay_drop:
421103404e8aSMaor Gottlieb 	cancel_delay_drop(dev);
4212e126ba97SEli Cohen 	destroy_umrc_res(dev);
4213e126ba97SEli Cohen 
4214e126ba97SEli Cohen err_dev:
4215e126ba97SEli Cohen 	ib_unregister_device(&dev->ib_dev);
4216e126ba97SEli Cohen 
42175fe9dec0SEli Cohen err_fp_bfreg:
42185fe9dec0SEli Cohen 	mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
42195fe9dec0SEli Cohen 
42205fe9dec0SEli Cohen err_bfreg:
42215fe9dec0SEli Cohen 	mlx5_free_bfreg(dev->mdev, &dev->bfreg);
42225fe9dec0SEli Cohen 
42235fe9dec0SEli Cohen err_uar_page:
42245fe9dec0SEli Cohen 	mlx5_put_uars_page(dev->mdev, dev->mdev->priv.uar);
42255fe9dec0SEli Cohen 
42264a2da0b8SParav Pandit err_cong:
4227e19cd282SParav Pandit 	mlx5_ib_cleanup_cong_debugfs(dev);
4228e19cd282SParav Pandit err_cnt:
422945bded2cSKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt))
4230e1f24a79SParav Pandit 		mlx5_ib_dealloc_counters(dev);
42310837e86aSMark Bloch 
42326aec21f6SHaggai Eran err_odp:
42336aec21f6SHaggai Eran 	mlx5_ib_odp_remove_one(dev);
42346aec21f6SHaggai Eran 
4235e126ba97SEli Cohen err_rsrc:
4236e126ba97SEli Cohen 	destroy_dev_resources(&dev->devr);
4237e126ba97SEli Cohen 
423845f95acdSOr Gerlitz err_disable_eth:
42395ec8c83eSAviv Heller 	if (ll == IB_LINK_LAYER_ETHERNET) {
424045f95acdSOr Gerlitz 		mlx5_disable_eth(dev);
4241d012f5d6SOr Gerlitz 		mlx5_remove_netdev_notifier(dev);
42425ec8c83eSAviv Heller 	}
4243fc24fc5eSAchiad Shochat 
42440837e86aSMark Bloch err_free_port:
42450837e86aSMark Bloch 	kfree(dev->port);
42460837e86aSMark Bloch 
42479603b61dSJack Morgenstein err_dealloc:
4248e126ba97SEli Cohen 	ib_dealloc_device((struct ib_device *)dev);
4249e126ba97SEli Cohen 
42509603b61dSJack Morgenstein 	return NULL;
4251e126ba97SEli Cohen }
4252e126ba97SEli Cohen 
42539603b61dSJack Morgenstein static void mlx5_ib_remove(struct mlx5_core_dev *mdev, void *context)
4254e126ba97SEli Cohen {
42559603b61dSJack Morgenstein 	struct mlx5_ib_dev *dev = context;
4256fc24fc5eSAchiad Shochat 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev, 1);
42576aec21f6SHaggai Eran 
425803404e8aSMaor Gottlieb 	cancel_delay_drop(dev);
4259d012f5d6SOr Gerlitz 	mlx5_remove_netdev_notifier(dev);
4260e126ba97SEli Cohen 	ib_unregister_device(&dev->ib_dev);
42615fe9dec0SEli Cohen 	mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
42625fe9dec0SEli Cohen 	mlx5_free_bfreg(dev->mdev, &dev->bfreg);
42635fe9dec0SEli Cohen 	mlx5_put_uars_page(dev->mdev, mdev->priv.uar);
42644a2da0b8SParav Pandit 	mlx5_ib_cleanup_cong_debugfs(dev);
426545bded2cSKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt))
4266e1f24a79SParav Pandit 		mlx5_ib_dealloc_counters(dev);
4267eefd56e5SEli Cohen 	destroy_umrc_res(dev);
42686aec21f6SHaggai Eran 	mlx5_ib_odp_remove_one(dev);
4269e126ba97SEli Cohen 	destroy_dev_resources(&dev->devr);
4270fc24fc5eSAchiad Shochat 	if (ll == IB_LINK_LAYER_ETHERNET)
427145f95acdSOr Gerlitz 		mlx5_disable_eth(dev);
42720837e86aSMark Bloch 	kfree(dev->port);
4273e126ba97SEli Cohen 	ib_dealloc_device(&dev->ib_dev);
4274e126ba97SEli Cohen }
4275e126ba97SEli Cohen 
42769603b61dSJack Morgenstein static struct mlx5_interface mlx5_ib_interface = {
42779603b61dSJack Morgenstein 	.add            = mlx5_ib_add,
42789603b61dSJack Morgenstein 	.remove         = mlx5_ib_remove,
42799603b61dSJack Morgenstein 	.event          = mlx5_ib_event,
4280d9aaed83SArtemy Kovalyov #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
4281d9aaed83SArtemy Kovalyov 	.pfault		= mlx5_ib_pfault,
4282d9aaed83SArtemy Kovalyov #endif
428364613d94SSaeed Mahameed 	.protocol	= MLX5_INTERFACE_PROTOCOL_IB,
4284e126ba97SEli Cohen };
4285e126ba97SEli Cohen 
4286e126ba97SEli Cohen static int __init mlx5_ib_init(void)
4287e126ba97SEli Cohen {
42886aec21f6SHaggai Eran 	int err;
42896aec21f6SHaggai Eran 
429081713d37SArtemy Kovalyov 	mlx5_ib_odp_init();
42919603b61dSJack Morgenstein 
42926aec21f6SHaggai Eran 	err = mlx5_register_interface(&mlx5_ib_interface);
42936aec21f6SHaggai Eran 
42946aec21f6SHaggai Eran 	return err;
4295e126ba97SEli Cohen }
4296e126ba97SEli Cohen 
4297e126ba97SEli Cohen static void __exit mlx5_ib_cleanup(void)
4298e126ba97SEli Cohen {
42999603b61dSJack Morgenstein 	mlx5_unregister_interface(&mlx5_ib_interface);
4300e126ba97SEli Cohen }
4301e126ba97SEli Cohen 
4302e126ba97SEli Cohen module_init(mlx5_ib_init);
4303e126ba97SEli Cohen module_exit(mlx5_ib_cleanup);
4304