xref: /openbmc/linux/drivers/infiniband/hw/mlx5/main.c (revision b1383aa64121778d9419cc982e387e27d9e96c64)
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>
537c2344c3SMaor Gottlieb #include <linux/list.h>
54e126ba97SEli Cohen #include <rdma/ib_smi.h>
55e126ba97SEli Cohen #include <rdma/ib_umem.h>
56038d2ef8SMaor Gottlieb #include <linux/in.h>
57038d2ef8SMaor Gottlieb #include <linux/etherdevice.h>
58038d2ef8SMaor Gottlieb #include <linux/mlx5/fs.h>
5978984898SOr Gerlitz #include <linux/mlx5/vport.h>
60e126ba97SEli Cohen #include "mlx5_ib.h"
61e1f24a79SParav Pandit #include "cmd.h"
62c85023e1SHuy Nguyen #include <linux/mlx5/vport.h>
63e126ba97SEli Cohen 
64e126ba97SEli Cohen #define DRIVER_NAME "mlx5_ib"
65b359911dSTariq Toukan #define DRIVER_VERSION "5.0-0"
66e126ba97SEli Cohen 
67e126ba97SEli Cohen MODULE_AUTHOR("Eli Cohen <eli@mellanox.com>");
68e126ba97SEli Cohen MODULE_DESCRIPTION("Mellanox Connect-IB HCA IB driver");
69e126ba97SEli Cohen MODULE_LICENSE("Dual BSD/GPL");
70e126ba97SEli Cohen 
71e126ba97SEli Cohen static char mlx5_version[] =
72e126ba97SEli Cohen 	DRIVER_NAME ": Mellanox Connect-IB Infiniband driver v"
73b359911dSTariq Toukan 	DRIVER_VERSION "\n";
74e126ba97SEli Cohen 
75da7525d2SEran Ben Elisha enum {
76da7525d2SEran Ben Elisha 	MLX5_ATOMIC_SIZE_QP_8BYTES = 1 << 3,
77da7525d2SEran Ben Elisha };
781b5daf11SMajd Dibbiny 
791b5daf11SMajd Dibbiny static enum rdma_link_layer
80ebd61f68SAchiad Shochat mlx5_port_type_cap_to_rdma_ll(int port_type_cap)
811b5daf11SMajd Dibbiny {
82ebd61f68SAchiad Shochat 	switch (port_type_cap) {
831b5daf11SMajd Dibbiny 	case MLX5_CAP_PORT_TYPE_IB:
841b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_INFINIBAND;
851b5daf11SMajd Dibbiny 	case MLX5_CAP_PORT_TYPE_ETH:
861b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_ETHERNET;
871b5daf11SMajd Dibbiny 	default:
881b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_UNSPECIFIED;
891b5daf11SMajd Dibbiny 	}
901b5daf11SMajd Dibbiny }
911b5daf11SMajd Dibbiny 
92ebd61f68SAchiad Shochat static enum rdma_link_layer
93ebd61f68SAchiad Shochat mlx5_ib_port_link_layer(struct ib_device *device, u8 port_num)
94ebd61f68SAchiad Shochat {
95ebd61f68SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(device);
96ebd61f68SAchiad Shochat 	int port_type_cap = MLX5_CAP_GEN(dev->mdev, port_type);
97ebd61f68SAchiad Shochat 
98ebd61f68SAchiad Shochat 	return mlx5_port_type_cap_to_rdma_ll(port_type_cap);
99ebd61f68SAchiad Shochat }
100ebd61f68SAchiad Shochat 
101fd65f1b8SMoni Shoua static int get_port_state(struct ib_device *ibdev,
102fd65f1b8SMoni Shoua 			  u8 port_num,
103fd65f1b8SMoni Shoua 			  enum ib_port_state *state)
104fd65f1b8SMoni Shoua {
105fd65f1b8SMoni Shoua 	struct ib_port_attr attr;
106fd65f1b8SMoni Shoua 	int ret;
107fd65f1b8SMoni Shoua 
108fd65f1b8SMoni Shoua 	memset(&attr, 0, sizeof(attr));
109fd65f1b8SMoni Shoua 	ret = mlx5_ib_query_port(ibdev, port_num, &attr);
110fd65f1b8SMoni Shoua 	if (!ret)
111fd65f1b8SMoni Shoua 		*state = attr.state;
112fd65f1b8SMoni Shoua 	return ret;
113fd65f1b8SMoni Shoua }
114fd65f1b8SMoni Shoua 
115fc24fc5eSAchiad Shochat static int mlx5_netdev_event(struct notifier_block *this,
116fc24fc5eSAchiad Shochat 			     unsigned long event, void *ptr)
117fc24fc5eSAchiad Shochat {
118fc24fc5eSAchiad Shochat 	struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
119fc24fc5eSAchiad Shochat 	struct mlx5_ib_dev *ibdev = container_of(this, struct mlx5_ib_dev,
120fc24fc5eSAchiad Shochat 						 roce.nb);
121fc24fc5eSAchiad Shochat 
1225ec8c83eSAviv Heller 	switch (event) {
1235ec8c83eSAviv Heller 	case NETDEV_REGISTER:
1245ec8c83eSAviv Heller 	case NETDEV_UNREGISTER:
125fc24fc5eSAchiad Shochat 		write_lock(&ibdev->roce.netdev_lock);
126fc24fc5eSAchiad Shochat 		if (ndev->dev.parent == &ibdev->mdev->pdev->dev)
1275ec8c83eSAviv Heller 			ibdev->roce.netdev = (event == NETDEV_UNREGISTER) ?
1285ec8c83eSAviv Heller 					     NULL : ndev;
129fc24fc5eSAchiad Shochat 		write_unlock(&ibdev->roce.netdev_lock);
1305ec8c83eSAviv Heller 		break;
1315ec8c83eSAviv Heller 
132fd65f1b8SMoni Shoua 	case NETDEV_CHANGE:
1335ec8c83eSAviv Heller 	case NETDEV_UP:
13488621dfeSAviv Heller 	case NETDEV_DOWN: {
13588621dfeSAviv Heller 		struct net_device *lag_ndev = mlx5_lag_get_roce_netdev(ibdev->mdev);
13688621dfeSAviv Heller 		struct net_device *upper = NULL;
13788621dfeSAviv Heller 
13888621dfeSAviv Heller 		if (lag_ndev) {
13988621dfeSAviv Heller 			upper = netdev_master_upper_dev_get(lag_ndev);
14088621dfeSAviv Heller 			dev_put(lag_ndev);
14188621dfeSAviv Heller 		}
14288621dfeSAviv Heller 
14388621dfeSAviv Heller 		if ((upper == ndev || (!upper && ndev == ibdev->roce.netdev))
14488621dfeSAviv Heller 		    && ibdev->ib_active) {
145626bc02dSBart Van Assche 			struct ib_event ibev = { };
146fd65f1b8SMoni Shoua 			enum ib_port_state port_state;
1475ec8c83eSAviv Heller 
148fd65f1b8SMoni Shoua 			if (get_port_state(&ibdev->ib_dev, 1, &port_state))
149fd65f1b8SMoni Shoua 				return NOTIFY_DONE;
150fd65f1b8SMoni Shoua 
151fd65f1b8SMoni Shoua 			if (ibdev->roce.last_port_state == port_state)
152fd65f1b8SMoni Shoua 				return NOTIFY_DONE;
153fd65f1b8SMoni Shoua 
154fd65f1b8SMoni Shoua 			ibdev->roce.last_port_state = port_state;
1555ec8c83eSAviv Heller 			ibev.device = &ibdev->ib_dev;
156fd65f1b8SMoni Shoua 			if (port_state == IB_PORT_DOWN)
157fd65f1b8SMoni Shoua 				ibev.event = IB_EVENT_PORT_ERR;
158fd65f1b8SMoni Shoua 			else if (port_state == IB_PORT_ACTIVE)
159fd65f1b8SMoni Shoua 				ibev.event = IB_EVENT_PORT_ACTIVE;
160fd65f1b8SMoni Shoua 			else
161fd65f1b8SMoni Shoua 				return NOTIFY_DONE;
162fd65f1b8SMoni Shoua 
1635ec8c83eSAviv Heller 			ibev.element.port_num = 1;
1645ec8c83eSAviv Heller 			ib_dispatch_event(&ibev);
1655ec8c83eSAviv Heller 		}
1665ec8c83eSAviv Heller 		break;
16788621dfeSAviv Heller 	}
1685ec8c83eSAviv Heller 
1695ec8c83eSAviv Heller 	default:
1705ec8c83eSAviv Heller 		break;
1715ec8c83eSAviv Heller 	}
172fc24fc5eSAchiad Shochat 
173fc24fc5eSAchiad Shochat 	return NOTIFY_DONE;
174fc24fc5eSAchiad Shochat }
175fc24fc5eSAchiad Shochat 
176fc24fc5eSAchiad Shochat static struct net_device *mlx5_ib_get_netdev(struct ib_device *device,
177fc24fc5eSAchiad Shochat 					     u8 port_num)
178fc24fc5eSAchiad Shochat {
179fc24fc5eSAchiad Shochat 	struct mlx5_ib_dev *ibdev = to_mdev(device);
180fc24fc5eSAchiad Shochat 	struct net_device *ndev;
181fc24fc5eSAchiad Shochat 
18288621dfeSAviv Heller 	ndev = mlx5_lag_get_roce_netdev(ibdev->mdev);
18388621dfeSAviv Heller 	if (ndev)
18488621dfeSAviv Heller 		return ndev;
18588621dfeSAviv Heller 
186fc24fc5eSAchiad Shochat 	/* Ensure ndev does not disappear before we invoke dev_hold()
187fc24fc5eSAchiad Shochat 	 */
188fc24fc5eSAchiad Shochat 	read_lock(&ibdev->roce.netdev_lock);
189fc24fc5eSAchiad Shochat 	ndev = ibdev->roce.netdev;
190fc24fc5eSAchiad Shochat 	if (ndev)
191fc24fc5eSAchiad Shochat 		dev_hold(ndev);
192fc24fc5eSAchiad Shochat 	read_unlock(&ibdev->roce.netdev_lock);
193fc24fc5eSAchiad Shochat 
194fc24fc5eSAchiad Shochat 	return ndev;
195fc24fc5eSAchiad Shochat }
196fc24fc5eSAchiad Shochat 
197f1b65df5SNoa Osherovich static int translate_eth_proto_oper(u32 eth_proto_oper, u8 *active_speed,
198f1b65df5SNoa Osherovich 				    u8 *active_width)
199f1b65df5SNoa Osherovich {
200f1b65df5SNoa Osherovich 	switch (eth_proto_oper) {
201f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_1000BASE_CX_SGMII):
202f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_1000BASE_KX):
203f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100BASE_TX):
204f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_1000BASE_T):
205f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
206f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_SDR;
207f1b65df5SNoa Osherovich 		break;
208f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_T):
209f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_CX4):
210f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_KX4):
211f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_KR):
212f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_CR):
213f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_SR):
214f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_ER):
215f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
216f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_QDR;
217f1b65df5SNoa Osherovich 		break;
218f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_25GBASE_CR):
219f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_25GBASE_KR):
220f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_25GBASE_SR):
221f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
222f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_EDR;
223f1b65df5SNoa Osherovich 		break;
224f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_CR4):
225f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_KR4):
226f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_SR4):
227f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_LR4):
228f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_4X;
229f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_QDR;
230f1b65df5SNoa Osherovich 		break;
231f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_50GBASE_CR2):
232f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_50GBASE_KR2):
233f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_50GBASE_SR2):
234f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
235f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_HDR;
236f1b65df5SNoa Osherovich 		break;
237f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_56GBASE_R4):
238f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_4X;
239f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_FDR;
240f1b65df5SNoa Osherovich 		break;
241f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_CR4):
242f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_SR4):
243f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_KR4):
244f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_LR4):
245f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_4X;
246f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_EDR;
247f1b65df5SNoa Osherovich 		break;
248f1b65df5SNoa Osherovich 	default:
249f1b65df5SNoa Osherovich 		return -EINVAL;
250f1b65df5SNoa Osherovich 	}
251f1b65df5SNoa Osherovich 
252f1b65df5SNoa Osherovich 	return 0;
253f1b65df5SNoa Osherovich }
254f1b65df5SNoa Osherovich 
255095b0927SIlan Tayari static int mlx5_query_port_roce(struct ib_device *device, u8 port_num,
2563f89a643SAchiad Shochat 				struct ib_port_attr *props)
2573f89a643SAchiad Shochat {
2583f89a643SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(device);
259f1b65df5SNoa Osherovich 	struct mlx5_core_dev *mdev = dev->mdev;
26088621dfeSAviv Heller 	struct net_device *ndev, *upper;
2613f89a643SAchiad Shochat 	enum ib_mtu ndev_ib_mtu;
262c876a1b7SLeon Romanovsky 	u16 qkey_viol_cntr;
263f1b65df5SNoa Osherovich 	u32 eth_prot_oper;
264095b0927SIlan Tayari 	int err;
2653f89a643SAchiad Shochat 
266f1b65df5SNoa Osherovich 	/* Possible bad flows are checked before filling out props so in case
267f1b65df5SNoa Osherovich 	 * of an error it will still be zeroed out.
26850f22fd8SNoa Osherovich 	 */
269095b0927SIlan Tayari 	err = mlx5_query_port_eth_proto_oper(mdev, &eth_prot_oper, port_num);
270095b0927SIlan Tayari 	if (err)
271095b0927SIlan Tayari 		return err;
272f1b65df5SNoa Osherovich 
273f1b65df5SNoa Osherovich 	translate_eth_proto_oper(eth_prot_oper, &props->active_speed,
274f1b65df5SNoa Osherovich 				 &props->active_width);
2753f89a643SAchiad Shochat 
2763f89a643SAchiad Shochat 	props->port_cap_flags  |= IB_PORT_CM_SUP;
2773f89a643SAchiad Shochat 	props->port_cap_flags  |= IB_PORT_IP_BASED_GIDS;
2783f89a643SAchiad Shochat 
2793f89a643SAchiad Shochat 	props->gid_tbl_len      = MLX5_CAP_ROCE(dev->mdev,
2803f89a643SAchiad Shochat 						roce_address_table_size);
2813f89a643SAchiad Shochat 	props->max_mtu          = IB_MTU_4096;
2823f89a643SAchiad Shochat 	props->max_msg_sz       = 1 << MLX5_CAP_GEN(dev->mdev, log_max_msg);
2833f89a643SAchiad Shochat 	props->pkey_tbl_len     = 1;
2843f89a643SAchiad Shochat 	props->state            = IB_PORT_DOWN;
2853f89a643SAchiad Shochat 	props->phys_state       = 3;
2863f89a643SAchiad Shochat 
287c876a1b7SLeon Romanovsky 	mlx5_query_nic_vport_qkey_viol_cntr(dev->mdev, &qkey_viol_cntr);
288c876a1b7SLeon Romanovsky 	props->qkey_viol_cntr = qkey_viol_cntr;
2893f89a643SAchiad Shochat 
2903f89a643SAchiad Shochat 	ndev = mlx5_ib_get_netdev(device, port_num);
2913f89a643SAchiad Shochat 	if (!ndev)
292095b0927SIlan Tayari 		return 0;
2933f89a643SAchiad Shochat 
29488621dfeSAviv Heller 	if (mlx5_lag_is_active(dev->mdev)) {
29588621dfeSAviv Heller 		rcu_read_lock();
29688621dfeSAviv Heller 		upper = netdev_master_upper_dev_get_rcu(ndev);
29788621dfeSAviv Heller 		if (upper) {
29888621dfeSAviv Heller 			dev_put(ndev);
29988621dfeSAviv Heller 			ndev = upper;
30088621dfeSAviv Heller 			dev_hold(ndev);
30188621dfeSAviv Heller 		}
30288621dfeSAviv Heller 		rcu_read_unlock();
30388621dfeSAviv Heller 	}
30488621dfeSAviv Heller 
3053f89a643SAchiad Shochat 	if (netif_running(ndev) && netif_carrier_ok(ndev)) {
3063f89a643SAchiad Shochat 		props->state      = IB_PORT_ACTIVE;
3073f89a643SAchiad Shochat 		props->phys_state = 5;
3083f89a643SAchiad Shochat 	}
3093f89a643SAchiad Shochat 
3103f89a643SAchiad Shochat 	ndev_ib_mtu = iboe_get_mtu(ndev->mtu);
3113f89a643SAchiad Shochat 
3123f89a643SAchiad Shochat 	dev_put(ndev);
3133f89a643SAchiad Shochat 
3143f89a643SAchiad Shochat 	props->active_mtu	= min(props->max_mtu, ndev_ib_mtu);
315095b0927SIlan Tayari 	return 0;
3163f89a643SAchiad Shochat }
3173f89a643SAchiad Shochat 
318095b0927SIlan Tayari static int set_roce_addr(struct mlx5_ib_dev *dev, u8 port_num,
319095b0927SIlan Tayari 			 unsigned int index, const union ib_gid *gid,
320095b0927SIlan Tayari 			 const struct ib_gid_attr *attr)
3213cca2606SAchiad Shochat {
322095b0927SIlan Tayari 	enum ib_gid_type gid_type = IB_GID_TYPE_IB;
323095b0927SIlan Tayari 	u8 roce_version = 0;
324095b0927SIlan Tayari 	u8 roce_l3_type = 0;
325095b0927SIlan Tayari 	bool vlan = false;
326095b0927SIlan Tayari 	u8 mac[ETH_ALEN];
327095b0927SIlan Tayari 	u16 vlan_id = 0;
3283cca2606SAchiad Shochat 
329095b0927SIlan Tayari 	if (gid) {
330095b0927SIlan Tayari 		gid_type = attr->gid_type;
331095b0927SIlan Tayari 		ether_addr_copy(mac, attr->ndev->dev_addr);
3323cca2606SAchiad Shochat 
3333cca2606SAchiad Shochat 		if (is_vlan_dev(attr->ndev)) {
334095b0927SIlan Tayari 			vlan = true;
335095b0927SIlan Tayari 			vlan_id = vlan_dev_vlan_id(attr->ndev);
336095b0927SIlan Tayari 		}
3373cca2606SAchiad Shochat 	}
3383cca2606SAchiad Shochat 
339095b0927SIlan Tayari 	switch (gid_type) {
3403cca2606SAchiad Shochat 	case IB_GID_TYPE_IB:
341095b0927SIlan Tayari 		roce_version = MLX5_ROCE_VERSION_1;
3423cca2606SAchiad Shochat 		break;
3433cca2606SAchiad Shochat 	case IB_GID_TYPE_ROCE_UDP_ENCAP:
344095b0927SIlan Tayari 		roce_version = MLX5_ROCE_VERSION_2;
345095b0927SIlan Tayari 		if (ipv6_addr_v4mapped((void *)gid))
346095b0927SIlan Tayari 			roce_l3_type = MLX5_ROCE_L3_TYPE_IPV4;
347095b0927SIlan Tayari 		else
348095b0927SIlan Tayari 			roce_l3_type = MLX5_ROCE_L3_TYPE_IPV6;
3493cca2606SAchiad Shochat 		break;
3503cca2606SAchiad Shochat 
3513cca2606SAchiad Shochat 	default:
352095b0927SIlan Tayari 		mlx5_ib_warn(dev, "Unexpected GID type %u\n", gid_type);
3533cca2606SAchiad Shochat 	}
3543cca2606SAchiad Shochat 
355095b0927SIlan Tayari 	return mlx5_core_roce_gid_set(dev->mdev, index, roce_version,
356095b0927SIlan Tayari 				      roce_l3_type, gid->raw, mac, vlan,
357095b0927SIlan Tayari 				      vlan_id);
3583cca2606SAchiad Shochat }
3593cca2606SAchiad Shochat 
3603cca2606SAchiad Shochat static int mlx5_ib_add_gid(struct ib_device *device, u8 port_num,
3613cca2606SAchiad Shochat 			   unsigned int index, const union ib_gid *gid,
3623cca2606SAchiad Shochat 			   const struct ib_gid_attr *attr,
3633cca2606SAchiad Shochat 			   __always_unused void **context)
3643cca2606SAchiad Shochat {
365095b0927SIlan Tayari 	return set_roce_addr(to_mdev(device), port_num, index, gid, attr);
3663cca2606SAchiad Shochat }
3673cca2606SAchiad Shochat 
3683cca2606SAchiad Shochat static int mlx5_ib_del_gid(struct ib_device *device, u8 port_num,
3693cca2606SAchiad Shochat 			   unsigned int index, __always_unused void **context)
3703cca2606SAchiad Shochat {
371095b0927SIlan Tayari 	return set_roce_addr(to_mdev(device), port_num, index, NULL, NULL);
3723cca2606SAchiad Shochat }
3733cca2606SAchiad Shochat 
3742811ba51SAchiad Shochat __be16 mlx5_get_roce_udp_sport(struct mlx5_ib_dev *dev, u8 port_num,
3752811ba51SAchiad Shochat 			       int index)
3762811ba51SAchiad Shochat {
3772811ba51SAchiad Shochat 	struct ib_gid_attr attr;
3782811ba51SAchiad Shochat 	union ib_gid gid;
3792811ba51SAchiad Shochat 
3802811ba51SAchiad Shochat 	if (ib_get_cached_gid(&dev->ib_dev, port_num, index, &gid, &attr))
3812811ba51SAchiad Shochat 		return 0;
3822811ba51SAchiad Shochat 
3832811ba51SAchiad Shochat 	if (!attr.ndev)
3842811ba51SAchiad Shochat 		return 0;
3852811ba51SAchiad Shochat 
3862811ba51SAchiad Shochat 	dev_put(attr.ndev);
3872811ba51SAchiad Shochat 
3882811ba51SAchiad Shochat 	if (attr.gid_type != IB_GID_TYPE_ROCE_UDP_ENCAP)
3892811ba51SAchiad Shochat 		return 0;
3902811ba51SAchiad Shochat 
3912811ba51SAchiad Shochat 	return cpu_to_be16(MLX5_CAP_ROCE(dev->mdev, r_roce_min_src_udp_port));
3922811ba51SAchiad Shochat }
3932811ba51SAchiad Shochat 
394ed88451eSMajd Dibbiny int mlx5_get_roce_gid_type(struct mlx5_ib_dev *dev, u8 port_num,
395ed88451eSMajd Dibbiny 			   int index, enum ib_gid_type *gid_type)
396ed88451eSMajd Dibbiny {
397ed88451eSMajd Dibbiny 	struct ib_gid_attr attr;
398ed88451eSMajd Dibbiny 	union ib_gid gid;
399ed88451eSMajd Dibbiny 	int ret;
400ed88451eSMajd Dibbiny 
401ed88451eSMajd Dibbiny 	ret = ib_get_cached_gid(&dev->ib_dev, port_num, index, &gid, &attr);
402ed88451eSMajd Dibbiny 	if (ret)
403ed88451eSMajd Dibbiny 		return ret;
404ed88451eSMajd Dibbiny 
405ed88451eSMajd Dibbiny 	if (!attr.ndev)
406ed88451eSMajd Dibbiny 		return -ENODEV;
407ed88451eSMajd Dibbiny 
408ed88451eSMajd Dibbiny 	dev_put(attr.ndev);
409ed88451eSMajd Dibbiny 
410ed88451eSMajd Dibbiny 	*gid_type = attr.gid_type;
411ed88451eSMajd Dibbiny 
412ed88451eSMajd Dibbiny 	return 0;
413ed88451eSMajd Dibbiny }
414ed88451eSMajd Dibbiny 
4151b5daf11SMajd Dibbiny static int mlx5_use_mad_ifc(struct mlx5_ib_dev *dev)
4161b5daf11SMajd Dibbiny {
4177fae6655SNoa Osherovich 	if (MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_IB)
418d603c809SEli Cohen 		return !MLX5_CAP_GEN(dev->mdev, ib_virt);
4197fae6655SNoa Osherovich 	return 0;
4201b5daf11SMajd Dibbiny }
4211b5daf11SMajd Dibbiny 
4221b5daf11SMajd Dibbiny enum {
4231b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_MAD,
4241b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_HCA,
4251b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_NIC,
4261b5daf11SMajd Dibbiny };
4271b5daf11SMajd Dibbiny 
4281b5daf11SMajd Dibbiny static int mlx5_get_vport_access_method(struct ib_device *ibdev)
4291b5daf11SMajd Dibbiny {
4301b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(to_mdev(ibdev)))
4311b5daf11SMajd Dibbiny 		return MLX5_VPORT_ACCESS_METHOD_MAD;
4321b5daf11SMajd Dibbiny 
433ebd61f68SAchiad Shochat 	if (mlx5_ib_port_link_layer(ibdev, 1) ==
4341b5daf11SMajd Dibbiny 	    IB_LINK_LAYER_ETHERNET)
4351b5daf11SMajd Dibbiny 		return MLX5_VPORT_ACCESS_METHOD_NIC;
4361b5daf11SMajd Dibbiny 
4371b5daf11SMajd Dibbiny 	return MLX5_VPORT_ACCESS_METHOD_HCA;
4381b5daf11SMajd Dibbiny }
4391b5daf11SMajd Dibbiny 
440da7525d2SEran Ben Elisha static void get_atomic_caps(struct mlx5_ib_dev *dev,
441da7525d2SEran Ben Elisha 			    struct ib_device_attr *props)
442da7525d2SEran Ben Elisha {
443da7525d2SEran Ben Elisha 	u8 tmp;
444da7525d2SEran Ben Elisha 	u8 atomic_operations = MLX5_CAP_ATOMIC(dev->mdev, atomic_operations);
445da7525d2SEran Ben Elisha 	u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_qp);
446da7525d2SEran Ben Elisha 	u8 atomic_req_8B_endianness_mode =
447bd10838aSOr Gerlitz 		MLX5_CAP_ATOMIC(dev->mdev, atomic_req_8B_endianness_mode);
448da7525d2SEran Ben Elisha 
449da7525d2SEran Ben Elisha 	/* Check if HW supports 8 bytes standard atomic operations and capable
450da7525d2SEran Ben Elisha 	 * of host endianness respond
451da7525d2SEran Ben Elisha 	 */
452da7525d2SEran Ben Elisha 	tmp = MLX5_ATOMIC_OPS_CMP_SWAP | MLX5_ATOMIC_OPS_FETCH_ADD;
453da7525d2SEran Ben Elisha 	if (((atomic_operations & tmp) == tmp) &&
454da7525d2SEran Ben Elisha 	    (atomic_size_qp & MLX5_ATOMIC_SIZE_QP_8BYTES) &&
455da7525d2SEran Ben Elisha 	    (atomic_req_8B_endianness_mode)) {
456da7525d2SEran Ben Elisha 		props->atomic_cap = IB_ATOMIC_HCA;
457da7525d2SEran Ben Elisha 	} else {
458da7525d2SEran Ben Elisha 		props->atomic_cap = IB_ATOMIC_NONE;
459da7525d2SEran Ben Elisha 	}
460da7525d2SEran Ben Elisha }
461da7525d2SEran Ben Elisha 
4621b5daf11SMajd Dibbiny static int mlx5_query_system_image_guid(struct ib_device *ibdev,
4631b5daf11SMajd Dibbiny 					__be64 *sys_image_guid)
4641b5daf11SMajd Dibbiny {
4651b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
4661b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
4671b5daf11SMajd Dibbiny 	u64 tmp;
4681b5daf11SMajd Dibbiny 	int err;
4691b5daf11SMajd Dibbiny 
4701b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
4711b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
4721b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_system_image_guid(ibdev,
4731b5daf11SMajd Dibbiny 							    sys_image_guid);
4741b5daf11SMajd Dibbiny 
4751b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
4761b5daf11SMajd Dibbiny 		err = mlx5_query_hca_vport_system_image_guid(mdev, &tmp);
4773f89a643SAchiad Shochat 		break;
4783f89a643SAchiad Shochat 
4793f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
4803f89a643SAchiad Shochat 		err = mlx5_query_nic_vport_system_image_guid(mdev, &tmp);
4813f89a643SAchiad Shochat 		break;
4821b5daf11SMajd Dibbiny 
4831b5daf11SMajd Dibbiny 	default:
4841b5daf11SMajd Dibbiny 		return -EINVAL;
4851b5daf11SMajd Dibbiny 	}
4863f89a643SAchiad Shochat 
4873f89a643SAchiad Shochat 	if (!err)
4883f89a643SAchiad Shochat 		*sys_image_guid = cpu_to_be64(tmp);
4893f89a643SAchiad Shochat 
4903f89a643SAchiad Shochat 	return err;
4913f89a643SAchiad Shochat 
4921b5daf11SMajd Dibbiny }
4931b5daf11SMajd Dibbiny 
4941b5daf11SMajd Dibbiny static int mlx5_query_max_pkeys(struct ib_device *ibdev,
4951b5daf11SMajd Dibbiny 				u16 *max_pkeys)
4961b5daf11SMajd Dibbiny {
4971b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
4981b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
4991b5daf11SMajd Dibbiny 
5001b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
5011b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
5021b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_max_pkeys(ibdev, max_pkeys);
5031b5daf11SMajd Dibbiny 
5041b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
5051b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
5061b5daf11SMajd Dibbiny 		*max_pkeys = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev,
5071b5daf11SMajd Dibbiny 						pkey_table_size));
5081b5daf11SMajd Dibbiny 		return 0;
5091b5daf11SMajd Dibbiny 
5101b5daf11SMajd Dibbiny 	default:
5111b5daf11SMajd Dibbiny 		return -EINVAL;
5121b5daf11SMajd Dibbiny 	}
5131b5daf11SMajd Dibbiny }
5141b5daf11SMajd Dibbiny 
5151b5daf11SMajd Dibbiny static int mlx5_query_vendor_id(struct ib_device *ibdev,
5161b5daf11SMajd Dibbiny 				u32 *vendor_id)
5171b5daf11SMajd Dibbiny {
5181b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
5191b5daf11SMajd Dibbiny 
5201b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
5211b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
5221b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_vendor_id(ibdev, vendor_id);
5231b5daf11SMajd Dibbiny 
5241b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
5251b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
5261b5daf11SMajd Dibbiny 		return mlx5_core_query_vendor_id(dev->mdev, vendor_id);
5271b5daf11SMajd Dibbiny 
5281b5daf11SMajd Dibbiny 	default:
5291b5daf11SMajd Dibbiny 		return -EINVAL;
5301b5daf11SMajd Dibbiny 	}
5311b5daf11SMajd Dibbiny }
5321b5daf11SMajd Dibbiny 
5331b5daf11SMajd Dibbiny static int mlx5_query_node_guid(struct mlx5_ib_dev *dev,
5341b5daf11SMajd Dibbiny 				__be64 *node_guid)
5351b5daf11SMajd Dibbiny {
5361b5daf11SMajd Dibbiny 	u64 tmp;
5371b5daf11SMajd Dibbiny 	int err;
5381b5daf11SMajd Dibbiny 
5391b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(&dev->ib_dev)) {
5401b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
5411b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_node_guid(dev, node_guid);
5421b5daf11SMajd Dibbiny 
5431b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
5441b5daf11SMajd Dibbiny 		err = mlx5_query_hca_vport_node_guid(dev->mdev, &tmp);
5453f89a643SAchiad Shochat 		break;
5463f89a643SAchiad Shochat 
5473f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
5483f89a643SAchiad Shochat 		err = mlx5_query_nic_vport_node_guid(dev->mdev, &tmp);
5493f89a643SAchiad Shochat 		break;
5501b5daf11SMajd Dibbiny 
5511b5daf11SMajd Dibbiny 	default:
5521b5daf11SMajd Dibbiny 		return -EINVAL;
5531b5daf11SMajd Dibbiny 	}
5543f89a643SAchiad Shochat 
5553f89a643SAchiad Shochat 	if (!err)
5563f89a643SAchiad Shochat 		*node_guid = cpu_to_be64(tmp);
5573f89a643SAchiad Shochat 
5583f89a643SAchiad Shochat 	return err;
5591b5daf11SMajd Dibbiny }
5601b5daf11SMajd Dibbiny 
5611b5daf11SMajd Dibbiny struct mlx5_reg_node_desc {
562bd99fdeaSYuval Shaia 	u8	desc[IB_DEVICE_NODE_DESC_MAX];
5631b5daf11SMajd Dibbiny };
5641b5daf11SMajd Dibbiny 
5651b5daf11SMajd Dibbiny static int mlx5_query_node_desc(struct mlx5_ib_dev *dev, char *node_desc)
5661b5daf11SMajd Dibbiny {
5671b5daf11SMajd Dibbiny 	struct mlx5_reg_node_desc in;
5681b5daf11SMajd Dibbiny 
5691b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(dev))
5701b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_node_desc(dev, node_desc);
5711b5daf11SMajd Dibbiny 
5721b5daf11SMajd Dibbiny 	memset(&in, 0, sizeof(in));
5731b5daf11SMajd Dibbiny 
5741b5daf11SMajd Dibbiny 	return mlx5_core_access_reg(dev->mdev, &in, sizeof(in), node_desc,
5751b5daf11SMajd Dibbiny 				    sizeof(struct mlx5_reg_node_desc),
5761b5daf11SMajd Dibbiny 				    MLX5_REG_NODE_DESC, 0, 0);
5771b5daf11SMajd Dibbiny }
5781b5daf11SMajd Dibbiny 
579e126ba97SEli Cohen static int mlx5_ib_query_device(struct ib_device *ibdev,
5802528e33eSMatan Barak 				struct ib_device_attr *props,
5812528e33eSMatan Barak 				struct ib_udata *uhw)
582e126ba97SEli Cohen {
583e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
584938fe83cSSaeed Mahameed 	struct mlx5_core_dev *mdev = dev->mdev;
585e126ba97SEli Cohen 	int err = -ENOMEM;
586288c01b7SEli Cohen 	int max_sq_desc;
587e126ba97SEli Cohen 	int max_rq_sg;
588e126ba97SEli Cohen 	int max_sq_sg;
589e0238a6aSSagi Grimberg 	u64 min_page_size = 1ull << MLX5_CAP_GEN(mdev, log_pg_sz);
590402ca536SBodong Wang 	struct mlx5_ib_query_device_resp resp = {};
591402ca536SBodong Wang 	size_t resp_len;
592402ca536SBodong Wang 	u64 max_tso;
593e126ba97SEli Cohen 
594402ca536SBodong Wang 	resp_len = sizeof(resp.comp_mask) + sizeof(resp.response_length);
595402ca536SBodong Wang 	if (uhw->outlen && uhw->outlen < resp_len)
596402ca536SBodong Wang 		return -EINVAL;
597402ca536SBodong Wang 	else
598402ca536SBodong Wang 		resp.response_length = resp_len;
599402ca536SBodong Wang 
600402ca536SBodong Wang 	if (uhw->inlen && !ib_is_udata_cleared(uhw, 0, uhw->inlen))
6012528e33eSMatan Barak 		return -EINVAL;
6022528e33eSMatan Barak 
603e126ba97SEli Cohen 	memset(props, 0, sizeof(*props));
6041b5daf11SMajd Dibbiny 	err = mlx5_query_system_image_guid(ibdev,
6051b5daf11SMajd Dibbiny 					   &props->sys_image_guid);
6061b5daf11SMajd Dibbiny 	if (err)
6071b5daf11SMajd Dibbiny 		return err;
6081b5daf11SMajd Dibbiny 
6091b5daf11SMajd Dibbiny 	err = mlx5_query_max_pkeys(ibdev, &props->max_pkeys);
6101b5daf11SMajd Dibbiny 	if (err)
6111b5daf11SMajd Dibbiny 		return err;
6121b5daf11SMajd Dibbiny 
6131b5daf11SMajd Dibbiny 	err = mlx5_query_vendor_id(ibdev, &props->vendor_id);
6141b5daf11SMajd Dibbiny 	if (err)
6151b5daf11SMajd Dibbiny 		return err;
616e126ba97SEli Cohen 
6179603b61dSJack Morgenstein 	props->fw_ver = ((u64)fw_rev_maj(dev->mdev) << 32) |
6189603b61dSJack Morgenstein 		(fw_rev_min(dev->mdev) << 16) |
6199603b61dSJack Morgenstein 		fw_rev_sub(dev->mdev);
620e126ba97SEli Cohen 	props->device_cap_flags    = IB_DEVICE_CHANGE_PHY_PORT |
621e126ba97SEli Cohen 		IB_DEVICE_PORT_ACTIVE_EVENT		|
622e126ba97SEli Cohen 		IB_DEVICE_SYS_IMAGE_GUID		|
6231a4c3a3dSEli Cohen 		IB_DEVICE_RC_RNR_NAK_GEN;
624938fe83cSSaeed Mahameed 
625938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, pkv))
626e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
627938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, qkv))
628e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
629938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, apm))
630e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
631938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, xrc))
632e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_XRC;
633d2370e0aSMatan Barak 	if (MLX5_CAP_GEN(mdev, imaicl)) {
634d2370e0aSMatan Barak 		props->device_cap_flags |= IB_DEVICE_MEM_WINDOW |
635d2370e0aSMatan Barak 					   IB_DEVICE_MEM_WINDOW_TYPE_2B;
636d2370e0aSMatan Barak 		props->max_mw = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
637b005d316SSagi Grimberg 		/* We support 'Gappy' memory registration too */
638b005d316SSagi Grimberg 		props->device_cap_flags |= IB_DEVICE_SG_GAPS_REG;
639d2370e0aSMatan Barak 	}
640e126ba97SEli Cohen 	props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
641938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, sho)) {
6422dea9094SSagi Grimberg 		props->device_cap_flags |= IB_DEVICE_SIGNATURE_HANDOVER;
6432dea9094SSagi Grimberg 		/* At this stage no support for signature handover */
6442dea9094SSagi Grimberg 		props->sig_prot_cap = IB_PROT_T10DIF_TYPE_1 |
6452dea9094SSagi Grimberg 				      IB_PROT_T10DIF_TYPE_2 |
6462dea9094SSagi Grimberg 				      IB_PROT_T10DIF_TYPE_3;
6472dea9094SSagi Grimberg 		props->sig_guard_cap = IB_GUARD_T10DIF_CRC |
6482dea9094SSagi Grimberg 				       IB_GUARD_T10DIF_CSUM;
6492dea9094SSagi Grimberg 	}
650938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, block_lb_mc))
651f360d88aSEli Cohen 		props->device_cap_flags |= IB_DEVICE_BLOCK_MULTICAST_LOOPBACK;
652e126ba97SEli Cohen 
653402ca536SBodong Wang 	if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads)) {
654e8161334SNoa Osherovich 		if (MLX5_CAP_ETH(mdev, csum_cap)) {
655e8161334SNoa Osherovich 			/* Legacy bit to support old userspace libraries */
65688115fe7SBodong Wang 			props->device_cap_flags |= IB_DEVICE_RAW_IP_CSUM;
657e8161334SNoa Osherovich 			props->raw_packet_caps |= IB_RAW_PACKET_CAP_IP_CSUM;
658e8161334SNoa Osherovich 		}
659e8161334SNoa Osherovich 
660e8161334SNoa Osherovich 		if (MLX5_CAP_ETH(dev->mdev, vlan_cap))
661e8161334SNoa Osherovich 			props->raw_packet_caps |=
662e8161334SNoa Osherovich 				IB_RAW_PACKET_CAP_CVLAN_STRIPPING;
66388115fe7SBodong Wang 
664402ca536SBodong Wang 		if (field_avail(typeof(resp), tso_caps, uhw->outlen)) {
665402ca536SBodong Wang 			max_tso = MLX5_CAP_ETH(mdev, max_lso_cap);
666402ca536SBodong Wang 			if (max_tso) {
667402ca536SBodong Wang 				resp.tso_caps.max_tso = 1 << max_tso;
668402ca536SBodong Wang 				resp.tso_caps.supported_qpts |=
669402ca536SBodong Wang 					1 << IB_QPT_RAW_PACKET;
670402ca536SBodong Wang 				resp.response_length += sizeof(resp.tso_caps);
671402ca536SBodong Wang 			}
672402ca536SBodong Wang 		}
67331f69a82SYishai Hadas 
67431f69a82SYishai Hadas 		if (field_avail(typeof(resp), rss_caps, uhw->outlen)) {
67531f69a82SYishai Hadas 			resp.rss_caps.rx_hash_function =
67631f69a82SYishai Hadas 						MLX5_RX_HASH_FUNC_TOEPLITZ;
67731f69a82SYishai Hadas 			resp.rss_caps.rx_hash_fields_mask =
67831f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_IPV4 |
67931f69a82SYishai Hadas 						MLX5_RX_HASH_DST_IPV4 |
68031f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_IPV6 |
68131f69a82SYishai Hadas 						MLX5_RX_HASH_DST_IPV6 |
68231f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_PORT_TCP |
68331f69a82SYishai Hadas 						MLX5_RX_HASH_DST_PORT_TCP |
68431f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_PORT_UDP |
68531f69a82SYishai Hadas 						MLX5_RX_HASH_DST_PORT_UDP;
68631f69a82SYishai Hadas 			resp.response_length += sizeof(resp.rss_caps);
68731f69a82SYishai Hadas 		}
68831f69a82SYishai Hadas 	} else {
68931f69a82SYishai Hadas 		if (field_avail(typeof(resp), tso_caps, uhw->outlen))
69031f69a82SYishai Hadas 			resp.response_length += sizeof(resp.tso_caps);
69131f69a82SYishai Hadas 		if (field_avail(typeof(resp), rss_caps, uhw->outlen))
69231f69a82SYishai Hadas 			resp.response_length += sizeof(resp.rss_caps);
693402ca536SBodong Wang 	}
694402ca536SBodong Wang 
695f0313965SErez Shitrit 	if (MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) {
696f0313965SErez Shitrit 		props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
697f0313965SErez Shitrit 		props->device_cap_flags |= IB_DEVICE_UD_TSO;
698f0313965SErez Shitrit 	}
699f0313965SErez Shitrit 
70003404e8aSMaor Gottlieb 	if (MLX5_CAP_GEN(dev->mdev, rq_delay_drop) &&
70103404e8aSMaor Gottlieb 	    MLX5_CAP_GEN(dev->mdev, general_notification_event))
70203404e8aSMaor Gottlieb 		props->raw_packet_caps |= IB_RAW_PACKET_CAP_DELAY_DROP;
70303404e8aSMaor Gottlieb 
7041d54f890SYishai Hadas 	if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads) &&
7051d54f890SYishai Hadas 	    MLX5_CAP_IPOIB_ENHANCED(mdev, csum_cap))
7061d54f890SYishai Hadas 		props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
7071d54f890SYishai Hadas 
708cff5a0f3SMajd Dibbiny 	if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
709e8161334SNoa Osherovich 	    MLX5_CAP_ETH(dev->mdev, scatter_fcs)) {
710e8161334SNoa Osherovich 		/* Legacy bit to support old userspace libraries */
711cff5a0f3SMajd Dibbiny 		props->device_cap_flags |= IB_DEVICE_RAW_SCATTER_FCS;
712e8161334SNoa Osherovich 		props->raw_packet_caps |= IB_RAW_PACKET_CAP_SCATTER_FCS;
713e8161334SNoa Osherovich 	}
714cff5a0f3SMajd Dibbiny 
715da6d6ba3SMaor Gottlieb 	if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS))
716da6d6ba3SMaor Gottlieb 		props->device_cap_flags |= IB_DEVICE_MANAGED_FLOW_STEERING;
717da6d6ba3SMaor Gottlieb 
718*b1383aa6SNoa Osherovich 	if (MLX5_CAP_GEN(mdev, end_pad))
719*b1383aa6SNoa Osherovich 		props->device_cap_flags |= IB_DEVICE_PCI_WRITE_END_PADDING;
720*b1383aa6SNoa Osherovich 
7211b5daf11SMajd Dibbiny 	props->vendor_part_id	   = mdev->pdev->device;
7221b5daf11SMajd Dibbiny 	props->hw_ver		   = mdev->pdev->revision;
723e126ba97SEli Cohen 
724e126ba97SEli Cohen 	props->max_mr_size	   = ~0ull;
725e0238a6aSSagi Grimberg 	props->page_size_cap	   = ~(min_page_size - 1);
726938fe83cSSaeed Mahameed 	props->max_qp		   = 1 << MLX5_CAP_GEN(mdev, log_max_qp);
727938fe83cSSaeed Mahameed 	props->max_qp_wr	   = 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
728938fe83cSSaeed Mahameed 	max_rq_sg =  MLX5_CAP_GEN(mdev, max_wqe_sz_rq) /
729938fe83cSSaeed Mahameed 		     sizeof(struct mlx5_wqe_data_seg);
730288c01b7SEli Cohen 	max_sq_desc = min_t(int, MLX5_CAP_GEN(mdev, max_wqe_sz_sq), 512);
731288c01b7SEli Cohen 	max_sq_sg = (max_sq_desc - sizeof(struct mlx5_wqe_ctrl_seg) -
732288c01b7SEli Cohen 		     sizeof(struct mlx5_wqe_raddr_seg)) /
733e126ba97SEli Cohen 		sizeof(struct mlx5_wqe_data_seg);
734e126ba97SEli Cohen 	props->max_sge = min(max_rq_sg, max_sq_sg);
735986ef95eSSagi Grimberg 	props->max_sge_rd	   = MLX5_MAX_SGE_RD;
736938fe83cSSaeed Mahameed 	props->max_cq		   = 1 << MLX5_CAP_GEN(mdev, log_max_cq);
7379f177686SLeon Romanovsky 	props->max_cqe = (1 << MLX5_CAP_GEN(mdev, log_max_cq_sz)) - 1;
738938fe83cSSaeed Mahameed 	props->max_mr		   = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
739938fe83cSSaeed Mahameed 	props->max_pd		   = 1 << MLX5_CAP_GEN(mdev, log_max_pd);
740938fe83cSSaeed Mahameed 	props->max_qp_rd_atom	   = 1 << MLX5_CAP_GEN(mdev, log_max_ra_req_qp);
741938fe83cSSaeed Mahameed 	props->max_qp_init_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_res_qp);
742938fe83cSSaeed Mahameed 	props->max_srq		   = 1 << MLX5_CAP_GEN(mdev, log_max_srq);
743938fe83cSSaeed Mahameed 	props->max_srq_wr = (1 << MLX5_CAP_GEN(mdev, log_max_srq_sz)) - 1;
744938fe83cSSaeed Mahameed 	props->local_ca_ack_delay  = MLX5_CAP_GEN(mdev, local_ca_ack_delay);
745e126ba97SEli Cohen 	props->max_res_rd_atom	   = props->max_qp_rd_atom * props->max_qp;
746e126ba97SEli Cohen 	props->max_srq_sge	   = max_rq_sg - 1;
747911f4331SSagi Grimberg 	props->max_fast_reg_page_list_len =
748911f4331SSagi Grimberg 		1 << MLX5_CAP_GEN(mdev, log_max_klm_list_size);
749da7525d2SEran Ben Elisha 	get_atomic_caps(dev, props);
75081bea28fSEli Cohen 	props->masked_atomic_cap   = IB_ATOMIC_NONE;
751938fe83cSSaeed Mahameed 	props->max_mcast_grp	   = 1 << MLX5_CAP_GEN(mdev, log_max_mcg);
752938fe83cSSaeed Mahameed 	props->max_mcast_qp_attach = MLX5_CAP_GEN(mdev, max_qp_mcg);
753e126ba97SEli Cohen 	props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
754e126ba97SEli Cohen 					   props->max_mcast_grp;
755e126ba97SEli Cohen 	props->max_map_per_fmr = INT_MAX; /* no limit in ConnectIB */
75686695a65SMaor Gottlieb 	props->max_ah = INT_MAX;
7577c60bcbbSMatan Barak 	props->hca_core_clock = MLX5_CAP_GEN(mdev, device_frequency_khz);
7587c60bcbbSMatan Barak 	props->timestamp_mask = 0x7FFFFFFFFFFFFFFFULL;
759e126ba97SEli Cohen 
7608cdd312cSHaggai Eran #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
761938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, pg))
7628cdd312cSHaggai Eran 		props->device_cap_flags |= IB_DEVICE_ON_DEMAND_PAGING;
7638cdd312cSHaggai Eran 	props->odp_caps = dev->odp_caps;
7648cdd312cSHaggai Eran #endif
7658cdd312cSHaggai Eran 
766051f2630SLeon Romanovsky 	if (MLX5_CAP_GEN(mdev, cd))
767051f2630SLeon Romanovsky 		props->device_cap_flags |= IB_DEVICE_CROSS_CHANNEL;
768051f2630SLeon Romanovsky 
769eff901d3SEli Cohen 	if (!mlx5_core_is_pf(mdev))
770eff901d3SEli Cohen 		props->device_cap_flags |= IB_DEVICE_VIRTUAL_FUNCTION;
771eff901d3SEli Cohen 
77231f69a82SYishai Hadas 	if (mlx5_ib_port_link_layer(ibdev, 1) ==
77331f69a82SYishai Hadas 	    IB_LINK_LAYER_ETHERNET) {
77431f69a82SYishai Hadas 		props->rss_caps.max_rwq_indirection_tables =
77531f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt);
77631f69a82SYishai Hadas 		props->rss_caps.max_rwq_indirection_table_size =
77731f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt_size);
77831f69a82SYishai Hadas 		props->rss_caps.supported_qpts = 1 << IB_QPT_RAW_PACKET;
77931f69a82SYishai Hadas 		props->max_wq_type_rq =
78031f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rq);
78131f69a82SYishai Hadas 	}
78231f69a82SYishai Hadas 
783eb761894SArtemy Kovalyov 	if (MLX5_CAP_GEN(mdev, tag_matching)) {
78478b1beb0SLeon Romanovsky 		props->tm_caps.max_rndv_hdr_size = MLX5_TM_MAX_RNDV_MSG_SIZE;
78578b1beb0SLeon Romanovsky 		props->tm_caps.max_num_tags =
786eb761894SArtemy Kovalyov 			(1 << MLX5_CAP_GEN(mdev, log_tag_matching_list_sz)) - 1;
78778b1beb0SLeon Romanovsky 		props->tm_caps.flags = IB_TM_CAP_RC;
78878b1beb0SLeon Romanovsky 		props->tm_caps.max_ops =
789eb761894SArtemy Kovalyov 			1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
79078b1beb0SLeon Romanovsky 		props->tm_caps.max_sge = MLX5_TM_MAX_SGE;
791eb761894SArtemy Kovalyov 	}
792eb761894SArtemy Kovalyov 
7937e43a2a5SBodong Wang 	if (field_avail(typeof(resp), cqe_comp_caps, uhw->outlen)) {
7947e43a2a5SBodong Wang 		resp.cqe_comp_caps.max_num =
7957e43a2a5SBodong Wang 			MLX5_CAP_GEN(dev->mdev, cqe_compression) ?
7967e43a2a5SBodong Wang 			MLX5_CAP_GEN(dev->mdev, cqe_compression_max_num) : 0;
7977e43a2a5SBodong Wang 		resp.cqe_comp_caps.supported_format =
7987e43a2a5SBodong Wang 			MLX5_IB_CQE_RES_FORMAT_HASH |
7997e43a2a5SBodong Wang 			MLX5_IB_CQE_RES_FORMAT_CSUM;
8007e43a2a5SBodong Wang 		resp.response_length += sizeof(resp.cqe_comp_caps);
8017e43a2a5SBodong Wang 	}
8027e43a2a5SBodong Wang 
803d949167dSBodong Wang 	if (field_avail(typeof(resp), packet_pacing_caps, uhw->outlen)) {
804d949167dSBodong Wang 		if (MLX5_CAP_QOS(mdev, packet_pacing) &&
805d949167dSBodong Wang 		    MLX5_CAP_GEN(mdev, qos)) {
806d949167dSBodong Wang 			resp.packet_pacing_caps.qp_rate_limit_max =
807d949167dSBodong Wang 				MLX5_CAP_QOS(mdev, packet_pacing_max_rate);
808d949167dSBodong Wang 			resp.packet_pacing_caps.qp_rate_limit_min =
809d949167dSBodong Wang 				MLX5_CAP_QOS(mdev, packet_pacing_min_rate);
810d949167dSBodong Wang 			resp.packet_pacing_caps.supported_qpts |=
811d949167dSBodong Wang 				1 << IB_QPT_RAW_PACKET;
812d949167dSBodong Wang 		}
813d949167dSBodong Wang 		resp.response_length += sizeof(resp.packet_pacing_caps);
814d949167dSBodong Wang 	}
815d949167dSBodong Wang 
8169f885201SLeon Romanovsky 	if (field_avail(typeof(resp), mlx5_ib_support_multi_pkt_send_wqes,
8179f885201SLeon Romanovsky 			uhw->outlen)) {
818795b609cSBodong Wang 		if (MLX5_CAP_ETH(mdev, multi_pkt_send_wqe))
8199f885201SLeon Romanovsky 			resp.mlx5_ib_support_multi_pkt_send_wqes =
820795b609cSBodong Wang 				MLX5_IB_ALLOW_MPW;
821050da902SBodong Wang 
822050da902SBodong Wang 		if (MLX5_CAP_ETH(mdev, enhanced_multi_pkt_send_wqe))
823050da902SBodong Wang 			resp.mlx5_ib_support_multi_pkt_send_wqes |=
824050da902SBodong Wang 				MLX5_IB_SUPPORT_EMPW;
825050da902SBodong Wang 
8269f885201SLeon Romanovsky 		resp.response_length +=
8279f885201SLeon Romanovsky 			sizeof(resp.mlx5_ib_support_multi_pkt_send_wqes);
8289f885201SLeon Romanovsky 	}
8299f885201SLeon Romanovsky 
830de57f2adSGuy Levi 	if (field_avail(typeof(resp), flags, uhw->outlen)) {
831de57f2adSGuy Levi 		resp.response_length += sizeof(resp.flags);
8327a0c8f42SGuy Levi 
833de57f2adSGuy Levi 		if (MLX5_CAP_GEN(mdev, cqe_compression_128))
834de57f2adSGuy Levi 			resp.flags |=
835de57f2adSGuy Levi 				MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_COMP;
8367a0c8f42SGuy Levi 
8377a0c8f42SGuy Levi 		if (MLX5_CAP_GEN(mdev, cqe_128_always))
8387a0c8f42SGuy Levi 			resp.flags |= MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_PAD;
839de57f2adSGuy Levi 	}
8409f885201SLeon Romanovsky 
84196dc3fc5SNoa Osherovich 	if (field_avail(typeof(resp), sw_parsing_caps,
84296dc3fc5SNoa Osherovich 			uhw->outlen)) {
84396dc3fc5SNoa Osherovich 		resp.response_length += sizeof(resp.sw_parsing_caps);
84496dc3fc5SNoa Osherovich 		if (MLX5_CAP_ETH(mdev, swp)) {
84596dc3fc5SNoa Osherovich 			resp.sw_parsing_caps.sw_parsing_offloads |=
84696dc3fc5SNoa Osherovich 				MLX5_IB_SW_PARSING;
84796dc3fc5SNoa Osherovich 
84896dc3fc5SNoa Osherovich 			if (MLX5_CAP_ETH(mdev, swp_csum))
84996dc3fc5SNoa Osherovich 				resp.sw_parsing_caps.sw_parsing_offloads |=
85096dc3fc5SNoa Osherovich 					MLX5_IB_SW_PARSING_CSUM;
85196dc3fc5SNoa Osherovich 
85296dc3fc5SNoa Osherovich 			if (MLX5_CAP_ETH(mdev, swp_lso))
85396dc3fc5SNoa Osherovich 				resp.sw_parsing_caps.sw_parsing_offloads |=
85496dc3fc5SNoa Osherovich 					MLX5_IB_SW_PARSING_LSO;
85596dc3fc5SNoa Osherovich 
85696dc3fc5SNoa Osherovich 			if (resp.sw_parsing_caps.sw_parsing_offloads)
85796dc3fc5SNoa Osherovich 				resp.sw_parsing_caps.supported_qpts =
85896dc3fc5SNoa Osherovich 					BIT(IB_QPT_RAW_PACKET);
85996dc3fc5SNoa Osherovich 		}
86096dc3fc5SNoa Osherovich 	}
86196dc3fc5SNoa Osherovich 
862b4f34597SNoa Osherovich 	if (field_avail(typeof(resp), striding_rq_caps, uhw->outlen)) {
863b4f34597SNoa Osherovich 		resp.response_length += sizeof(resp.striding_rq_caps);
864b4f34597SNoa Osherovich 		if (MLX5_CAP_GEN(mdev, striding_rq)) {
865b4f34597SNoa Osherovich 			resp.striding_rq_caps.min_single_stride_log_num_of_bytes =
866b4f34597SNoa Osherovich 				MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES;
867b4f34597SNoa Osherovich 			resp.striding_rq_caps.max_single_stride_log_num_of_bytes =
868b4f34597SNoa Osherovich 				MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES;
869b4f34597SNoa Osherovich 			resp.striding_rq_caps.min_single_wqe_log_num_of_strides =
870b4f34597SNoa Osherovich 				MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES;
871b4f34597SNoa Osherovich 			resp.striding_rq_caps.max_single_wqe_log_num_of_strides =
872b4f34597SNoa Osherovich 				MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES;
873b4f34597SNoa Osherovich 			resp.striding_rq_caps.supported_qpts =
874b4f34597SNoa Osherovich 				BIT(IB_QPT_RAW_PACKET);
875b4f34597SNoa Osherovich 		}
876b4f34597SNoa Osherovich 	}
877b4f34597SNoa Osherovich 
878f95ef6cbSMaor Gottlieb 	if (field_avail(typeof(resp), tunnel_offloads_caps,
879f95ef6cbSMaor Gottlieb 			uhw->outlen)) {
880f95ef6cbSMaor Gottlieb 		resp.response_length += sizeof(resp.tunnel_offloads_caps);
881f95ef6cbSMaor Gottlieb 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_vxlan))
882f95ef6cbSMaor Gottlieb 			resp.tunnel_offloads_caps |=
883f95ef6cbSMaor Gottlieb 				MLX5_IB_TUNNELED_OFFLOADS_VXLAN;
884f95ef6cbSMaor Gottlieb 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_geneve_rx))
885f95ef6cbSMaor Gottlieb 			resp.tunnel_offloads_caps |=
886f95ef6cbSMaor Gottlieb 				MLX5_IB_TUNNELED_OFFLOADS_GENEVE;
887f95ef6cbSMaor Gottlieb 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_gre))
888f95ef6cbSMaor Gottlieb 			resp.tunnel_offloads_caps |=
889f95ef6cbSMaor Gottlieb 				MLX5_IB_TUNNELED_OFFLOADS_GRE;
890f95ef6cbSMaor Gottlieb 	}
891f95ef6cbSMaor Gottlieb 
892402ca536SBodong Wang 	if (uhw->outlen) {
893402ca536SBodong Wang 		err = ib_copy_to_udata(uhw, &resp, resp.response_length);
894402ca536SBodong Wang 
895402ca536SBodong Wang 		if (err)
896402ca536SBodong Wang 			return err;
897402ca536SBodong Wang 	}
898402ca536SBodong Wang 
8991b5daf11SMajd Dibbiny 	return 0;
9001b5daf11SMajd Dibbiny }
901e126ba97SEli Cohen 
9021b5daf11SMajd Dibbiny enum mlx5_ib_width {
9031b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_1X	= 1 << 0,
9041b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_2X	= 1 << 1,
9051b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_4X	= 1 << 2,
9061b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_8X	= 1 << 3,
9071b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_12X	= 1 << 4
9081b5daf11SMajd Dibbiny };
9091b5daf11SMajd Dibbiny 
9101b5daf11SMajd Dibbiny static int translate_active_width(struct ib_device *ibdev, u8 active_width,
9111b5daf11SMajd Dibbiny 				  u8 *ib_width)
9121b5daf11SMajd Dibbiny {
9131b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
9141b5daf11SMajd Dibbiny 	int err = 0;
9151b5daf11SMajd Dibbiny 
9161b5daf11SMajd Dibbiny 	if (active_width & MLX5_IB_WIDTH_1X) {
9171b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_1X;
9181b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_2X) {
9191b5daf11SMajd Dibbiny 		mlx5_ib_dbg(dev, "active_width %d is not supported by IB spec\n",
9201b5daf11SMajd Dibbiny 			    (int)active_width);
9211b5daf11SMajd Dibbiny 		err = -EINVAL;
9221b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_4X) {
9231b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_4X;
9241b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_8X) {
9251b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_8X;
9261b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_12X) {
9271b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_12X;
9281b5daf11SMajd Dibbiny 	} else {
9291b5daf11SMajd Dibbiny 		mlx5_ib_dbg(dev, "Invalid active_width %d\n",
9301b5daf11SMajd Dibbiny 			    (int)active_width);
9311b5daf11SMajd Dibbiny 		err = -EINVAL;
9321b5daf11SMajd Dibbiny 	}
9331b5daf11SMajd Dibbiny 
9341b5daf11SMajd Dibbiny 	return err;
9351b5daf11SMajd Dibbiny }
9361b5daf11SMajd Dibbiny 
9371b5daf11SMajd Dibbiny static int mlx5_mtu_to_ib_mtu(int mtu)
9381b5daf11SMajd Dibbiny {
9391b5daf11SMajd Dibbiny 	switch (mtu) {
9401b5daf11SMajd Dibbiny 	case 256: return 1;
9411b5daf11SMajd Dibbiny 	case 512: return 2;
9421b5daf11SMajd Dibbiny 	case 1024: return 3;
9431b5daf11SMajd Dibbiny 	case 2048: return 4;
9441b5daf11SMajd Dibbiny 	case 4096: return 5;
9451b5daf11SMajd Dibbiny 	default:
9461b5daf11SMajd Dibbiny 		pr_warn("invalid mtu\n");
9471b5daf11SMajd Dibbiny 		return -1;
9481b5daf11SMajd Dibbiny 	}
9491b5daf11SMajd Dibbiny }
9501b5daf11SMajd Dibbiny 
9511b5daf11SMajd Dibbiny enum ib_max_vl_num {
9521b5daf11SMajd Dibbiny 	__IB_MAX_VL_0		= 1,
9531b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_1		= 2,
9541b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_3		= 3,
9551b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_7		= 4,
9561b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_14	= 5,
9571b5daf11SMajd Dibbiny };
9581b5daf11SMajd Dibbiny 
9591b5daf11SMajd Dibbiny enum mlx5_vl_hw_cap {
9601b5daf11SMajd Dibbiny 	MLX5_VL_HW_0	= 1,
9611b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_1	= 2,
9621b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_2	= 3,
9631b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_3	= 4,
9641b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_4	= 5,
9651b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_5	= 6,
9661b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_6	= 7,
9671b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_7	= 8,
9681b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_14	= 15
9691b5daf11SMajd Dibbiny };
9701b5daf11SMajd Dibbiny 
9711b5daf11SMajd Dibbiny static int translate_max_vl_num(struct ib_device *ibdev, u8 vl_hw_cap,
9721b5daf11SMajd Dibbiny 				u8 *max_vl_num)
9731b5daf11SMajd Dibbiny {
9741b5daf11SMajd Dibbiny 	switch (vl_hw_cap) {
9751b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0:
9761b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0;
9771b5daf11SMajd Dibbiny 		break;
9781b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_1:
9791b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_1;
9801b5daf11SMajd Dibbiny 		break;
9811b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_3:
9821b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_3;
9831b5daf11SMajd Dibbiny 		break;
9841b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_7:
9851b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_7;
9861b5daf11SMajd Dibbiny 		break;
9871b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_14:
9881b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_14;
9891b5daf11SMajd Dibbiny 		break;
9901b5daf11SMajd Dibbiny 
9911b5daf11SMajd Dibbiny 	default:
9921b5daf11SMajd Dibbiny 		return -EINVAL;
9931b5daf11SMajd Dibbiny 	}
9941b5daf11SMajd Dibbiny 
9951b5daf11SMajd Dibbiny 	return 0;
9961b5daf11SMajd Dibbiny }
9971b5daf11SMajd Dibbiny 
9981b5daf11SMajd Dibbiny static int mlx5_query_hca_port(struct ib_device *ibdev, u8 port,
9991b5daf11SMajd Dibbiny 			       struct ib_port_attr *props)
10001b5daf11SMajd Dibbiny {
10011b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
10021b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
10031b5daf11SMajd Dibbiny 	struct mlx5_hca_vport_context *rep;
1004046339eaSSaeed Mahameed 	u16 max_mtu;
1005046339eaSSaeed Mahameed 	u16 oper_mtu;
10061b5daf11SMajd Dibbiny 	int err;
10071b5daf11SMajd Dibbiny 	u8 ib_link_width_oper;
10081b5daf11SMajd Dibbiny 	u8 vl_hw_cap;
10091b5daf11SMajd Dibbiny 
10101b5daf11SMajd Dibbiny 	rep = kzalloc(sizeof(*rep), GFP_KERNEL);
10111b5daf11SMajd Dibbiny 	if (!rep) {
10121b5daf11SMajd Dibbiny 		err = -ENOMEM;
10131b5daf11SMajd Dibbiny 		goto out;
10141b5daf11SMajd Dibbiny 	}
10151b5daf11SMajd Dibbiny 
1016c4550c63SOr Gerlitz 	/* props being zeroed by the caller, avoid zeroing it here */
10171b5daf11SMajd Dibbiny 
10181b5daf11SMajd Dibbiny 	err = mlx5_query_hca_vport_context(mdev, 0, port, 0, rep);
10191b5daf11SMajd Dibbiny 	if (err)
10201b5daf11SMajd Dibbiny 		goto out;
10211b5daf11SMajd Dibbiny 
10221b5daf11SMajd Dibbiny 	props->lid		= rep->lid;
10231b5daf11SMajd Dibbiny 	props->lmc		= rep->lmc;
10241b5daf11SMajd Dibbiny 	props->sm_lid		= rep->sm_lid;
10251b5daf11SMajd Dibbiny 	props->sm_sl		= rep->sm_sl;
10261b5daf11SMajd Dibbiny 	props->state		= rep->vport_state;
10271b5daf11SMajd Dibbiny 	props->phys_state	= rep->port_physical_state;
10281b5daf11SMajd Dibbiny 	props->port_cap_flags	= rep->cap_mask1;
10291b5daf11SMajd Dibbiny 	props->gid_tbl_len	= mlx5_get_gid_table_len(MLX5_CAP_GEN(mdev, gid_table_size));
10301b5daf11SMajd Dibbiny 	props->max_msg_sz	= 1 << MLX5_CAP_GEN(mdev, log_max_msg);
10311b5daf11SMajd Dibbiny 	props->pkey_tbl_len	= mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, pkey_table_size));
10321b5daf11SMajd Dibbiny 	props->bad_pkey_cntr	= rep->pkey_violation_counter;
10331b5daf11SMajd Dibbiny 	props->qkey_viol_cntr	= rep->qkey_violation_counter;
10341b5daf11SMajd Dibbiny 	props->subnet_timeout	= rep->subnet_timeout;
10351b5daf11SMajd Dibbiny 	props->init_type_reply	= rep->init_type_reply;
1036eff901d3SEli Cohen 	props->grh_required	= rep->grh_required;
10371b5daf11SMajd Dibbiny 
10381b5daf11SMajd Dibbiny 	err = mlx5_query_port_link_width_oper(mdev, &ib_link_width_oper, port);
10391b5daf11SMajd Dibbiny 	if (err)
10401b5daf11SMajd Dibbiny 		goto out;
10411b5daf11SMajd Dibbiny 
10421b5daf11SMajd Dibbiny 	err = translate_active_width(ibdev, ib_link_width_oper,
10431b5daf11SMajd Dibbiny 				     &props->active_width);
10441b5daf11SMajd Dibbiny 	if (err)
10451b5daf11SMajd Dibbiny 		goto out;
1046d5beb7f2SNoa Osherovich 	err = mlx5_query_port_ib_proto_oper(mdev, &props->active_speed, port);
10471b5daf11SMajd Dibbiny 	if (err)
10481b5daf11SMajd Dibbiny 		goto out;
10491b5daf11SMajd Dibbiny 
1050facc9699SSaeed Mahameed 	mlx5_query_port_max_mtu(mdev, &max_mtu, port);
10511b5daf11SMajd Dibbiny 
10521b5daf11SMajd Dibbiny 	props->max_mtu = mlx5_mtu_to_ib_mtu(max_mtu);
10531b5daf11SMajd Dibbiny 
1054facc9699SSaeed Mahameed 	mlx5_query_port_oper_mtu(mdev, &oper_mtu, port);
10551b5daf11SMajd Dibbiny 
10561b5daf11SMajd Dibbiny 	props->active_mtu = mlx5_mtu_to_ib_mtu(oper_mtu);
10571b5daf11SMajd Dibbiny 
10581b5daf11SMajd Dibbiny 	err = mlx5_query_port_vl_hw_cap(mdev, &vl_hw_cap, port);
10591b5daf11SMajd Dibbiny 	if (err)
10601b5daf11SMajd Dibbiny 		goto out;
10611b5daf11SMajd Dibbiny 
10621b5daf11SMajd Dibbiny 	err = translate_max_vl_num(ibdev, vl_hw_cap,
10631b5daf11SMajd Dibbiny 				   &props->max_vl_num);
10641b5daf11SMajd Dibbiny out:
10651b5daf11SMajd Dibbiny 	kfree(rep);
1066e126ba97SEli Cohen 	return err;
1067e126ba97SEli Cohen }
1068e126ba97SEli Cohen 
1069e126ba97SEli Cohen int mlx5_ib_query_port(struct ib_device *ibdev, u8 port,
1070e126ba97SEli Cohen 		       struct ib_port_attr *props)
1071e126ba97SEli Cohen {
1072095b0927SIlan Tayari 	unsigned int count;
1073095b0927SIlan Tayari 	int ret;
1074095b0927SIlan Tayari 
10751b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
10761b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
1077095b0927SIlan Tayari 		ret = mlx5_query_mad_ifc_port(ibdev, port, props);
1078095b0927SIlan Tayari 		break;
1079e126ba97SEli Cohen 
10801b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
1081095b0927SIlan Tayari 		ret = mlx5_query_hca_port(ibdev, port, props);
1082095b0927SIlan Tayari 		break;
10831b5daf11SMajd Dibbiny 
10843f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
1085095b0927SIlan Tayari 		ret = mlx5_query_port_roce(ibdev, port, props);
1086095b0927SIlan Tayari 		break;
10873f89a643SAchiad Shochat 
10881b5daf11SMajd Dibbiny 	default:
1089095b0927SIlan Tayari 		ret = -EINVAL;
1090e126ba97SEli Cohen 	}
1091095b0927SIlan Tayari 
1092095b0927SIlan Tayari 	if (!ret && props) {
1093095b0927SIlan Tayari 		count = mlx5_core_reserved_gids_count(to_mdev(ibdev)->mdev);
1094095b0927SIlan Tayari 		props->gid_tbl_len -= count;
1095095b0927SIlan Tayari 	}
1096095b0927SIlan Tayari 	return ret;
1097e126ba97SEli Cohen }
1098e126ba97SEli Cohen 
1099e126ba97SEli Cohen static int mlx5_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
1100e126ba97SEli Cohen 			     union ib_gid *gid)
1101e126ba97SEli Cohen {
11021b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
11031b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
1104e126ba97SEli Cohen 
11051b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
11061b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
11071b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_gids(ibdev, port, index, gid);
1108e126ba97SEli Cohen 
11091b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
11101b5daf11SMajd Dibbiny 		return mlx5_query_hca_vport_gid(mdev, 0, port, 0, index, gid);
1111e126ba97SEli Cohen 
11121b5daf11SMajd Dibbiny 	default:
11131b5daf11SMajd Dibbiny 		return -EINVAL;
11141b5daf11SMajd Dibbiny 	}
1115e126ba97SEli Cohen 
1116e126ba97SEli Cohen }
1117e126ba97SEli Cohen 
1118e126ba97SEli Cohen static int mlx5_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
1119e126ba97SEli Cohen 			      u16 *pkey)
1120e126ba97SEli Cohen {
11211b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
11221b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
1123e126ba97SEli Cohen 
11241b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
11251b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
11261b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_pkey(ibdev, port, index, pkey);
1127e126ba97SEli Cohen 
11281b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
11291b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
11301b5daf11SMajd Dibbiny 		return mlx5_query_hca_vport_pkey(mdev, 0, port,  0, index,
11311b5daf11SMajd Dibbiny 						 pkey);
11321b5daf11SMajd Dibbiny 	default:
11331b5daf11SMajd Dibbiny 		return -EINVAL;
1134e126ba97SEli Cohen 	}
11351b5daf11SMajd Dibbiny }
1136e126ba97SEli Cohen 
1137e126ba97SEli Cohen static int mlx5_ib_modify_device(struct ib_device *ibdev, int mask,
1138e126ba97SEli Cohen 				 struct ib_device_modify *props)
1139e126ba97SEli Cohen {
1140e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1141e126ba97SEli Cohen 	struct mlx5_reg_node_desc in;
1142e126ba97SEli Cohen 	struct mlx5_reg_node_desc out;
1143e126ba97SEli Cohen 	int err;
1144e126ba97SEli Cohen 
1145e126ba97SEli Cohen 	if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
1146e126ba97SEli Cohen 		return -EOPNOTSUPP;
1147e126ba97SEli Cohen 
1148e126ba97SEli Cohen 	if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
1149e126ba97SEli Cohen 		return 0;
1150e126ba97SEli Cohen 
1151e126ba97SEli Cohen 	/*
1152e126ba97SEli Cohen 	 * If possible, pass node desc to FW, so it can generate
1153e126ba97SEli Cohen 	 * a 144 trap.  If cmd fails, just ignore.
1154e126ba97SEli Cohen 	 */
1155bd99fdeaSYuval Shaia 	memcpy(&in, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
11569603b61dSJack Morgenstein 	err = mlx5_core_access_reg(dev->mdev, &in, sizeof(in), &out,
1157e126ba97SEli Cohen 				   sizeof(out), MLX5_REG_NODE_DESC, 0, 1);
1158e126ba97SEli Cohen 	if (err)
1159e126ba97SEli Cohen 		return err;
1160e126ba97SEli Cohen 
1161bd99fdeaSYuval Shaia 	memcpy(ibdev->node_desc, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
1162e126ba97SEli Cohen 
1163e126ba97SEli Cohen 	return err;
1164e126ba97SEli Cohen }
1165e126ba97SEli Cohen 
1166cdbe33d0SEli Cohen static int set_port_caps_atomic(struct mlx5_ib_dev *dev, u8 port_num, u32 mask,
1167cdbe33d0SEli Cohen 				u32 value)
1168cdbe33d0SEli Cohen {
1169cdbe33d0SEli Cohen 	struct mlx5_hca_vport_context ctx = {};
1170cdbe33d0SEli Cohen 	int err;
1171cdbe33d0SEli Cohen 
1172cdbe33d0SEli Cohen 	err = mlx5_query_hca_vport_context(dev->mdev, 0,
1173cdbe33d0SEli Cohen 					   port_num, 0, &ctx);
1174cdbe33d0SEli Cohen 	if (err)
1175cdbe33d0SEli Cohen 		return err;
1176cdbe33d0SEli Cohen 
1177cdbe33d0SEli Cohen 	if (~ctx.cap_mask1_perm & mask) {
1178cdbe33d0SEli Cohen 		mlx5_ib_warn(dev, "trying to change bitmask 0x%X but change supported 0x%X\n",
1179cdbe33d0SEli Cohen 			     mask, ctx.cap_mask1_perm);
1180cdbe33d0SEli Cohen 		return -EINVAL;
1181cdbe33d0SEli Cohen 	}
1182cdbe33d0SEli Cohen 
1183cdbe33d0SEli Cohen 	ctx.cap_mask1 = value;
1184cdbe33d0SEli Cohen 	ctx.cap_mask1_perm = mask;
1185cdbe33d0SEli Cohen 	err = mlx5_core_modify_hca_vport_context(dev->mdev, 0,
1186cdbe33d0SEli Cohen 						 port_num, 0, &ctx);
1187cdbe33d0SEli Cohen 
1188cdbe33d0SEli Cohen 	return err;
1189cdbe33d0SEli Cohen }
1190cdbe33d0SEli Cohen 
1191e126ba97SEli Cohen static int mlx5_ib_modify_port(struct ib_device *ibdev, u8 port, int mask,
1192e126ba97SEli Cohen 			       struct ib_port_modify *props)
1193e126ba97SEli Cohen {
1194e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1195e126ba97SEli Cohen 	struct ib_port_attr attr;
1196e126ba97SEli Cohen 	u32 tmp;
1197e126ba97SEli Cohen 	int err;
1198cdbe33d0SEli Cohen 	u32 change_mask;
1199cdbe33d0SEli Cohen 	u32 value;
1200cdbe33d0SEli Cohen 	bool is_ib = (mlx5_ib_port_link_layer(ibdev, port) ==
1201cdbe33d0SEli Cohen 		      IB_LINK_LAYER_INFINIBAND);
1202cdbe33d0SEli Cohen 
1203ec255879SMajd Dibbiny 	/* CM layer calls ib_modify_port() regardless of the link layer. For
1204ec255879SMajd Dibbiny 	 * Ethernet ports, qkey violation and Port capabilities are meaningless.
1205ec255879SMajd Dibbiny 	 */
1206ec255879SMajd Dibbiny 	if (!is_ib)
1207ec255879SMajd Dibbiny 		return 0;
1208ec255879SMajd Dibbiny 
1209cdbe33d0SEli Cohen 	if (MLX5_CAP_GEN(dev->mdev, ib_virt) && is_ib) {
1210cdbe33d0SEli Cohen 		change_mask = props->clr_port_cap_mask | props->set_port_cap_mask;
1211cdbe33d0SEli Cohen 		value = ~props->clr_port_cap_mask | props->set_port_cap_mask;
1212cdbe33d0SEli Cohen 		return set_port_caps_atomic(dev, port, change_mask, value);
1213cdbe33d0SEli Cohen 	}
1214e126ba97SEli Cohen 
1215e126ba97SEli Cohen 	mutex_lock(&dev->cap_mask_mutex);
1216e126ba97SEli Cohen 
1217c4550c63SOr Gerlitz 	err = ib_query_port(ibdev, port, &attr);
1218e126ba97SEli Cohen 	if (err)
1219e126ba97SEli Cohen 		goto out;
1220e126ba97SEli Cohen 
1221e126ba97SEli Cohen 	tmp = (attr.port_cap_flags | props->set_port_cap_mask) &
1222e126ba97SEli Cohen 		~props->clr_port_cap_mask;
1223e126ba97SEli Cohen 
12249603b61dSJack Morgenstein 	err = mlx5_set_port_caps(dev->mdev, port, tmp);
1225e126ba97SEli Cohen 
1226e126ba97SEli Cohen out:
1227e126ba97SEli Cohen 	mutex_unlock(&dev->cap_mask_mutex);
1228e126ba97SEli Cohen 	return err;
1229e126ba97SEli Cohen }
1230e126ba97SEli Cohen 
123130aa60b3SEli Cohen static void print_lib_caps(struct mlx5_ib_dev *dev, u64 caps)
123230aa60b3SEli Cohen {
123330aa60b3SEli Cohen 	mlx5_ib_dbg(dev, "MLX5_LIB_CAP_4K_UAR = %s\n",
123430aa60b3SEli Cohen 		    caps & MLX5_LIB_CAP_4K_UAR ? "y" : "n");
123530aa60b3SEli Cohen }
123630aa60b3SEli Cohen 
1237b037c29aSEli Cohen static int calc_total_bfregs(struct mlx5_ib_dev *dev, bool lib_uar_4k,
1238b037c29aSEli Cohen 			     struct mlx5_ib_alloc_ucontext_req_v2 *req,
1239b037c29aSEli Cohen 			     u32 *num_sys_pages)
1240b037c29aSEli Cohen {
1241b037c29aSEli Cohen 	int uars_per_sys_page;
1242b037c29aSEli Cohen 	int bfregs_per_sys_page;
1243b037c29aSEli Cohen 	int ref_bfregs = req->total_num_bfregs;
1244b037c29aSEli Cohen 
1245b037c29aSEli Cohen 	if (req->total_num_bfregs == 0)
1246b037c29aSEli Cohen 		return -EINVAL;
1247b037c29aSEli Cohen 
1248b037c29aSEli Cohen 	BUILD_BUG_ON(MLX5_MAX_BFREGS % MLX5_NON_FP_BFREGS_IN_PAGE);
1249b037c29aSEli Cohen 	BUILD_BUG_ON(MLX5_MAX_BFREGS < MLX5_NON_FP_BFREGS_IN_PAGE);
1250b037c29aSEli Cohen 
1251b037c29aSEli Cohen 	if (req->total_num_bfregs > MLX5_MAX_BFREGS)
1252b037c29aSEli Cohen 		return -ENOMEM;
1253b037c29aSEli Cohen 
1254b037c29aSEli Cohen 	uars_per_sys_page = get_uars_per_sys_page(dev, lib_uar_4k);
1255b037c29aSEli Cohen 	bfregs_per_sys_page = uars_per_sys_page * MLX5_NON_FP_BFREGS_PER_UAR;
1256b037c29aSEli Cohen 	req->total_num_bfregs = ALIGN(req->total_num_bfregs, bfregs_per_sys_page);
1257b037c29aSEli Cohen 	*num_sys_pages = req->total_num_bfregs / bfregs_per_sys_page;
1258b037c29aSEli Cohen 
1259b037c29aSEli Cohen 	if (req->num_low_latency_bfregs > req->total_num_bfregs - 1)
1260b037c29aSEli Cohen 		return -EINVAL;
1261b037c29aSEli Cohen 
12629c2d33d4SColin 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",
1263b037c29aSEli Cohen 		    MLX5_CAP_GEN(dev->mdev, uar_4k) ? "yes" : "no",
1264b037c29aSEli Cohen 		    lib_uar_4k ? "yes" : "no", ref_bfregs,
1265b037c29aSEli Cohen 		    req->total_num_bfregs, *num_sys_pages);
1266b037c29aSEli Cohen 
1267b037c29aSEli Cohen 	return 0;
1268b037c29aSEli Cohen }
1269b037c29aSEli Cohen 
1270b037c29aSEli Cohen static int allocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
1271b037c29aSEli Cohen {
1272b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1273b037c29aSEli Cohen 	int err;
1274b037c29aSEli Cohen 	int i;
1275b037c29aSEli Cohen 
1276b037c29aSEli Cohen 	bfregi = &context->bfregi;
1277b037c29aSEli Cohen 	for (i = 0; i < bfregi->num_sys_pages; i++) {
1278b037c29aSEli Cohen 		err = mlx5_cmd_alloc_uar(dev->mdev, &bfregi->sys_pages[i]);
1279b037c29aSEli Cohen 		if (err)
1280b037c29aSEli Cohen 			goto error;
1281b037c29aSEli Cohen 
1282b037c29aSEli Cohen 		mlx5_ib_dbg(dev, "allocated uar %d\n", bfregi->sys_pages[i]);
1283b037c29aSEli Cohen 	}
1284b037c29aSEli Cohen 	return 0;
1285b037c29aSEli Cohen 
1286b037c29aSEli Cohen error:
1287b037c29aSEli Cohen 	for (--i; i >= 0; i--)
1288b037c29aSEli Cohen 		if (mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]))
1289b037c29aSEli Cohen 			mlx5_ib_warn(dev, "failed to free uar %d\n", i);
1290b037c29aSEli Cohen 
1291b037c29aSEli Cohen 	return err;
1292b037c29aSEli Cohen }
1293b037c29aSEli Cohen 
1294b037c29aSEli Cohen static int deallocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
1295b037c29aSEli Cohen {
1296b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1297b037c29aSEli Cohen 	int err;
1298b037c29aSEli Cohen 	int i;
1299b037c29aSEli Cohen 
1300b037c29aSEli Cohen 	bfregi = &context->bfregi;
1301b037c29aSEli Cohen 	for (i = 0; i < bfregi->num_sys_pages; i++) {
1302b037c29aSEli Cohen 		err = mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]);
1303b037c29aSEli Cohen 		if (err) {
1304b037c29aSEli Cohen 			mlx5_ib_warn(dev, "failed to free uar %d\n", i);
1305b037c29aSEli Cohen 			return err;
1306b037c29aSEli Cohen 		}
1307b037c29aSEli Cohen 	}
1308b037c29aSEli Cohen 	return 0;
1309b037c29aSEli Cohen }
1310b037c29aSEli Cohen 
1311c85023e1SHuy Nguyen static int mlx5_ib_alloc_transport_domain(struct mlx5_ib_dev *dev, u32 *tdn)
1312c85023e1SHuy Nguyen {
1313c85023e1SHuy Nguyen 	int err;
1314c85023e1SHuy Nguyen 
1315c85023e1SHuy Nguyen 	err = mlx5_core_alloc_transport_domain(dev->mdev, tdn);
1316c85023e1SHuy Nguyen 	if (err)
1317c85023e1SHuy Nguyen 		return err;
1318c85023e1SHuy Nguyen 
1319c85023e1SHuy Nguyen 	if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
1320c85023e1SHuy Nguyen 	    !MLX5_CAP_GEN(dev->mdev, disable_local_lb))
1321c85023e1SHuy Nguyen 		return err;
1322c85023e1SHuy Nguyen 
1323c85023e1SHuy Nguyen 	mutex_lock(&dev->lb_mutex);
1324c85023e1SHuy Nguyen 	dev->user_td++;
1325c85023e1SHuy Nguyen 
1326c85023e1SHuy Nguyen 	if (dev->user_td == 2)
1327c85023e1SHuy Nguyen 		err = mlx5_nic_vport_update_local_lb(dev->mdev, true);
1328c85023e1SHuy Nguyen 
1329c85023e1SHuy Nguyen 	mutex_unlock(&dev->lb_mutex);
1330c85023e1SHuy Nguyen 	return err;
1331c85023e1SHuy Nguyen }
1332c85023e1SHuy Nguyen 
1333c85023e1SHuy Nguyen static void mlx5_ib_dealloc_transport_domain(struct mlx5_ib_dev *dev, u32 tdn)
1334c85023e1SHuy Nguyen {
1335c85023e1SHuy Nguyen 	mlx5_core_dealloc_transport_domain(dev->mdev, tdn);
1336c85023e1SHuy Nguyen 
1337c85023e1SHuy Nguyen 	if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
1338c85023e1SHuy Nguyen 	    !MLX5_CAP_GEN(dev->mdev, disable_local_lb))
1339c85023e1SHuy Nguyen 		return;
1340c85023e1SHuy Nguyen 
1341c85023e1SHuy Nguyen 	mutex_lock(&dev->lb_mutex);
1342c85023e1SHuy Nguyen 	dev->user_td--;
1343c85023e1SHuy Nguyen 
1344c85023e1SHuy Nguyen 	if (dev->user_td < 2)
1345c85023e1SHuy Nguyen 		mlx5_nic_vport_update_local_lb(dev->mdev, false);
1346c85023e1SHuy Nguyen 
1347c85023e1SHuy Nguyen 	mutex_unlock(&dev->lb_mutex);
1348c85023e1SHuy Nguyen }
1349c85023e1SHuy Nguyen 
1350e126ba97SEli Cohen static struct ib_ucontext *mlx5_ib_alloc_ucontext(struct ib_device *ibdev,
1351e126ba97SEli Cohen 						  struct ib_udata *udata)
1352e126ba97SEli Cohen {
1353e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1354b368d7cbSMatan Barak 	struct mlx5_ib_alloc_ucontext_req_v2 req = {};
1355b368d7cbSMatan Barak 	struct mlx5_ib_alloc_ucontext_resp resp = {};
1356e126ba97SEli Cohen 	struct mlx5_ib_ucontext *context;
13572f5ff264SEli Cohen 	struct mlx5_bfreg_info *bfregi;
135878c0f98cSEli Cohen 	int ver;
1359e126ba97SEli Cohen 	int err;
1360a168a41cSMajd Dibbiny 	size_t min_req_v2 = offsetof(struct mlx5_ib_alloc_ucontext_req_v2,
1361a168a41cSMajd Dibbiny 				     max_cqe_version);
1362b037c29aSEli Cohen 	bool lib_uar_4k;
1363e126ba97SEli Cohen 
1364e126ba97SEli Cohen 	if (!dev->ib_active)
1365e126ba97SEli Cohen 		return ERR_PTR(-EAGAIN);
1366e126ba97SEli Cohen 
1367e093111dSAmrani, Ram 	if (udata->inlen == sizeof(struct mlx5_ib_alloc_ucontext_req))
136878c0f98cSEli Cohen 		ver = 0;
1369e093111dSAmrani, Ram 	else if (udata->inlen >= min_req_v2)
137078c0f98cSEli Cohen 		ver = 2;
137178c0f98cSEli Cohen 	else
137278c0f98cSEli Cohen 		return ERR_PTR(-EINVAL);
137378c0f98cSEli Cohen 
1374e093111dSAmrani, Ram 	err = ib_copy_from_udata(&req, udata, min(udata->inlen, sizeof(req)));
1375e126ba97SEli Cohen 	if (err)
1376e126ba97SEli Cohen 		return ERR_PTR(err);
1377e126ba97SEli Cohen 
1378b368d7cbSMatan Barak 	if (req.flags)
137978c0f98cSEli Cohen 		return ERR_PTR(-EINVAL);
138078c0f98cSEli Cohen 
1381f72300c5SHaggai Abramovsky 	if (req.comp_mask || req.reserved0 || req.reserved1 || req.reserved2)
1382b368d7cbSMatan Barak 		return ERR_PTR(-EOPNOTSUPP);
1383b368d7cbSMatan Barak 
13842f5ff264SEli Cohen 	req.total_num_bfregs = ALIGN(req.total_num_bfregs,
13852f5ff264SEli Cohen 				    MLX5_NON_FP_BFREGS_PER_UAR);
13862f5ff264SEli Cohen 	if (req.num_low_latency_bfregs > req.total_num_bfregs - 1)
1387e126ba97SEli Cohen 		return ERR_PTR(-EINVAL);
1388e126ba97SEli Cohen 
1389938fe83cSSaeed Mahameed 	resp.qp_tab_size = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp);
13902cc6ad5fSNoa Osherovich 	if (mlx5_core_is_pf(dev->mdev) && MLX5_CAP_GEN(dev->mdev, bf))
1391938fe83cSSaeed Mahameed 		resp.bf_reg_size = 1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size);
1392b47bd6eaSDaniel Jurgens 	resp.cache_line_size = cache_line_size();
1393938fe83cSSaeed Mahameed 	resp.max_sq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq);
1394938fe83cSSaeed Mahameed 	resp.max_rq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq);
1395938fe83cSSaeed Mahameed 	resp.max_send_wqebb = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1396938fe83cSSaeed Mahameed 	resp.max_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1397938fe83cSSaeed Mahameed 	resp.max_srq_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz);
1398f72300c5SHaggai Abramovsky 	resp.cqe_version = min_t(__u8,
1399f72300c5SHaggai Abramovsky 				 (__u8)MLX5_CAP_GEN(dev->mdev, cqe_version),
1400f72300c5SHaggai Abramovsky 				 req.max_cqe_version);
140130aa60b3SEli Cohen 	resp.log_uar_size = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
140230aa60b3SEli Cohen 				MLX5_ADAPTER_PAGE_SHIFT : PAGE_SHIFT;
140330aa60b3SEli Cohen 	resp.num_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
140430aa60b3SEli Cohen 					MLX5_CAP_GEN(dev->mdev, num_of_uars_per_page) : 1;
1405b368d7cbSMatan Barak 	resp.response_length = min(offsetof(typeof(resp), response_length) +
1406b368d7cbSMatan Barak 				   sizeof(resp.response_length), udata->outlen);
1407e126ba97SEli Cohen 
1408e126ba97SEli Cohen 	context = kzalloc(sizeof(*context), GFP_KERNEL);
1409e126ba97SEli Cohen 	if (!context)
1410e126ba97SEli Cohen 		return ERR_PTR(-ENOMEM);
1411e126ba97SEli Cohen 
141230aa60b3SEli Cohen 	lib_uar_4k = req.lib_caps & MLX5_LIB_CAP_4K_UAR;
14132f5ff264SEli Cohen 	bfregi = &context->bfregi;
1414b037c29aSEli Cohen 
1415b037c29aSEli Cohen 	/* updates req->total_num_bfregs */
1416b037c29aSEli Cohen 	err = calc_total_bfregs(dev, lib_uar_4k, &req, &bfregi->num_sys_pages);
1417b037c29aSEli Cohen 	if (err)
1418b037c29aSEli Cohen 		goto out_ctx;
1419b037c29aSEli Cohen 
14202f5ff264SEli Cohen 	mutex_init(&bfregi->lock);
1421b037c29aSEli Cohen 	bfregi->lib_uar_4k = lib_uar_4k;
1422b037c29aSEli Cohen 	bfregi->count = kcalloc(req.total_num_bfregs, sizeof(*bfregi->count),
1423b037c29aSEli Cohen 				GFP_KERNEL);
1424b037c29aSEli Cohen 	if (!bfregi->count) {
1425e126ba97SEli Cohen 		err = -ENOMEM;
1426e126ba97SEli Cohen 		goto out_ctx;
1427e126ba97SEli Cohen 	}
1428e126ba97SEli Cohen 
1429b037c29aSEli Cohen 	bfregi->sys_pages = kcalloc(bfregi->num_sys_pages,
1430b037c29aSEli Cohen 				    sizeof(*bfregi->sys_pages),
1431e126ba97SEli Cohen 				    GFP_KERNEL);
1432b037c29aSEli Cohen 	if (!bfregi->sys_pages) {
1433e126ba97SEli Cohen 		err = -ENOMEM;
1434e126ba97SEli Cohen 		goto out_count;
1435e126ba97SEli Cohen 	}
1436e126ba97SEli Cohen 
1437b037c29aSEli Cohen 	err = allocate_uars(dev, context);
1438b037c29aSEli Cohen 	if (err)
1439b037c29aSEli Cohen 		goto out_sys_pages;
1440b037c29aSEli Cohen 
1441b4cfe447SHaggai Eran #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
1442b4cfe447SHaggai Eran 	context->ibucontext.invalidate_range = &mlx5_ib_invalidate_range;
1443b4cfe447SHaggai Eran #endif
1444b4cfe447SHaggai Eran 
14457d0cc6edSArtemy Kovalyov 	context->upd_xlt_page = __get_free_page(GFP_KERNEL);
14467d0cc6edSArtemy Kovalyov 	if (!context->upd_xlt_page) {
14477d0cc6edSArtemy Kovalyov 		err = -ENOMEM;
14487d0cc6edSArtemy Kovalyov 		goto out_uars;
14497d0cc6edSArtemy Kovalyov 	}
14507d0cc6edSArtemy Kovalyov 	mutex_init(&context->upd_xlt_page_mutex);
14517d0cc6edSArtemy Kovalyov 
1452146d2f1aSmajd@mellanox.com 	if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain)) {
1453c85023e1SHuy Nguyen 		err = mlx5_ib_alloc_transport_domain(dev, &context->tdn);
1454146d2f1aSmajd@mellanox.com 		if (err)
14557d0cc6edSArtemy Kovalyov 			goto out_page;
1456146d2f1aSmajd@mellanox.com 	}
1457146d2f1aSmajd@mellanox.com 
14587c2344c3SMaor Gottlieb 	INIT_LIST_HEAD(&context->vma_private_list);
1459e126ba97SEli Cohen 	INIT_LIST_HEAD(&context->db_page_list);
1460e126ba97SEli Cohen 	mutex_init(&context->db_page_mutex);
1461e126ba97SEli Cohen 
14622f5ff264SEli Cohen 	resp.tot_bfregs = req.total_num_bfregs;
1463938fe83cSSaeed Mahameed 	resp.num_ports = MLX5_CAP_GEN(dev->mdev, num_ports);
1464b368d7cbSMatan Barak 
1465f72300c5SHaggai Abramovsky 	if (field_avail(typeof(resp), cqe_version, udata->outlen))
1466f72300c5SHaggai Abramovsky 		resp.response_length += sizeof(resp.cqe_version);
1467b368d7cbSMatan Barak 
1468402ca536SBodong Wang 	if (field_avail(typeof(resp), cmds_supp_uhw, udata->outlen)) {
14696ad279c5SMoni Shoua 		resp.cmds_supp_uhw |= MLX5_USER_CMDS_SUPP_UHW_QUERY_DEVICE |
14706ad279c5SMoni Shoua 				      MLX5_USER_CMDS_SUPP_UHW_CREATE_AH;
1471402ca536SBodong Wang 		resp.response_length += sizeof(resp.cmds_supp_uhw);
1472402ca536SBodong Wang 	}
1473402ca536SBodong Wang 
147478984898SOr Gerlitz 	if (field_avail(typeof(resp), eth_min_inline, udata->outlen)) {
147578984898SOr Gerlitz 		if (mlx5_ib_port_link_layer(ibdev, 1) == IB_LINK_LAYER_ETHERNET) {
147678984898SOr Gerlitz 			mlx5_query_min_inline(dev->mdev, &resp.eth_min_inline);
147778984898SOr Gerlitz 			resp.eth_min_inline++;
147878984898SOr Gerlitz 		}
147978984898SOr Gerlitz 		resp.response_length += sizeof(resp.eth_min_inline);
148078984898SOr Gerlitz 	}
148178984898SOr Gerlitz 
1482bc5c6eedSNoa Osherovich 	/*
1483bc5c6eedSNoa Osherovich 	 * We don't want to expose information from the PCI bar that is located
1484bc5c6eedSNoa Osherovich 	 * after 4096 bytes, so if the arch only supports larger pages, let's
1485bc5c6eedSNoa Osherovich 	 * pretend we don't support reading the HCA's core clock. This is also
1486bc5c6eedSNoa Osherovich 	 * forced by mmap function.
1487bc5c6eedSNoa Osherovich 	 */
1488de8d6e02SEli Cohen 	if (field_avail(typeof(resp), hca_core_clock_offset, udata->outlen)) {
1489de8d6e02SEli Cohen 		if (PAGE_SIZE <= 4096) {
1490b368d7cbSMatan Barak 			resp.comp_mask |=
1491b368d7cbSMatan Barak 				MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_CORE_CLOCK_OFFSET;
1492b368d7cbSMatan Barak 			resp.hca_core_clock_offset =
1493de8d6e02SEli Cohen 				offsetof(struct mlx5_init_seg, internal_timer_h) % PAGE_SIZE;
1494de8d6e02SEli Cohen 		}
1495f72300c5SHaggai Abramovsky 		resp.response_length += sizeof(resp.hca_core_clock_offset) +
1496402ca536SBodong Wang 					sizeof(resp.reserved2);
1497b368d7cbSMatan Barak 	}
1498b368d7cbSMatan Barak 
149930aa60b3SEli Cohen 	if (field_avail(typeof(resp), log_uar_size, udata->outlen))
150030aa60b3SEli Cohen 		resp.response_length += sizeof(resp.log_uar_size);
150130aa60b3SEli Cohen 
150230aa60b3SEli Cohen 	if (field_avail(typeof(resp), num_uars_per_page, udata->outlen))
150330aa60b3SEli Cohen 		resp.response_length += sizeof(resp.num_uars_per_page);
150430aa60b3SEli Cohen 
1505b368d7cbSMatan Barak 	err = ib_copy_to_udata(udata, &resp, resp.response_length);
1506e126ba97SEli Cohen 	if (err)
1507146d2f1aSmajd@mellanox.com 		goto out_td;
1508e126ba97SEli Cohen 
15092f5ff264SEli Cohen 	bfregi->ver = ver;
15102f5ff264SEli Cohen 	bfregi->num_low_latency_bfregs = req.num_low_latency_bfregs;
1511f72300c5SHaggai Abramovsky 	context->cqe_version = resp.cqe_version;
151230aa60b3SEli Cohen 	context->lib_caps = req.lib_caps;
151330aa60b3SEli Cohen 	print_lib_caps(dev, context->lib_caps);
1514f72300c5SHaggai Abramovsky 
1515e126ba97SEli Cohen 	return &context->ibucontext;
1516e126ba97SEli Cohen 
1517146d2f1aSmajd@mellanox.com out_td:
1518146d2f1aSmajd@mellanox.com 	if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1519c85023e1SHuy Nguyen 		mlx5_ib_dealloc_transport_domain(dev, context->tdn);
1520146d2f1aSmajd@mellanox.com 
15217d0cc6edSArtemy Kovalyov out_page:
15227d0cc6edSArtemy Kovalyov 	free_page(context->upd_xlt_page);
15237d0cc6edSArtemy Kovalyov 
1524e126ba97SEli Cohen out_uars:
1525b037c29aSEli Cohen 	deallocate_uars(dev, context);
1526b037c29aSEli Cohen 
1527b037c29aSEli Cohen out_sys_pages:
1528b037c29aSEli Cohen 	kfree(bfregi->sys_pages);
1529b037c29aSEli Cohen 
1530e126ba97SEli Cohen out_count:
15312f5ff264SEli Cohen 	kfree(bfregi->count);
1532e126ba97SEli Cohen 
1533e126ba97SEli Cohen out_ctx:
1534e126ba97SEli Cohen 	kfree(context);
1535b037c29aSEli Cohen 
1536e126ba97SEli Cohen 	return ERR_PTR(err);
1537e126ba97SEli Cohen }
1538e126ba97SEli Cohen 
1539e126ba97SEli Cohen static int mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
1540e126ba97SEli Cohen {
1541e126ba97SEli Cohen 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1542e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
1543b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1544e126ba97SEli Cohen 
1545b037c29aSEli Cohen 	bfregi = &context->bfregi;
1546146d2f1aSmajd@mellanox.com 	if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1547c85023e1SHuy Nguyen 		mlx5_ib_dealloc_transport_domain(dev, context->tdn);
1548146d2f1aSmajd@mellanox.com 
15497d0cc6edSArtemy Kovalyov 	free_page(context->upd_xlt_page);
1550b037c29aSEli Cohen 	deallocate_uars(dev, context);
1551b037c29aSEli Cohen 	kfree(bfregi->sys_pages);
15522f5ff264SEli Cohen 	kfree(bfregi->count);
1553e126ba97SEli Cohen 	kfree(context);
1554e126ba97SEli Cohen 
1555e126ba97SEli Cohen 	return 0;
1556e126ba97SEli Cohen }
1557e126ba97SEli Cohen 
1558b037c29aSEli Cohen static phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev,
1559b037c29aSEli Cohen 				 struct mlx5_bfreg_info *bfregi,
1560b037c29aSEli Cohen 				 int idx)
1561e126ba97SEli Cohen {
1562b037c29aSEli Cohen 	int fw_uars_per_page;
1563b037c29aSEli Cohen 
1564b037c29aSEli Cohen 	fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ? MLX5_UARS_IN_PAGE : 1;
1565b037c29aSEli Cohen 
1566b037c29aSEli Cohen 	return (pci_resource_start(dev->mdev->pdev, 0) >> PAGE_SHIFT) +
1567b037c29aSEli Cohen 			bfregi->sys_pages[idx] / fw_uars_per_page;
1568e126ba97SEli Cohen }
1569e126ba97SEli Cohen 
1570e126ba97SEli Cohen static int get_command(unsigned long offset)
1571e126ba97SEli Cohen {
1572e126ba97SEli Cohen 	return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK;
1573e126ba97SEli Cohen }
1574e126ba97SEli Cohen 
1575e126ba97SEli Cohen static int get_arg(unsigned long offset)
1576e126ba97SEli Cohen {
1577e126ba97SEli Cohen 	return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1);
1578e126ba97SEli Cohen }
1579e126ba97SEli Cohen 
1580e126ba97SEli Cohen static int get_index(unsigned long offset)
1581e126ba97SEli Cohen {
1582e126ba97SEli Cohen 	return get_arg(offset);
1583e126ba97SEli Cohen }
1584e126ba97SEli Cohen 
15857c2344c3SMaor Gottlieb static void  mlx5_ib_vma_open(struct vm_area_struct *area)
15867c2344c3SMaor Gottlieb {
15877c2344c3SMaor Gottlieb 	/* vma_open is called when a new VMA is created on top of our VMA.  This
15887c2344c3SMaor Gottlieb 	 * is done through either mremap flow or split_vma (usually due to
15897c2344c3SMaor Gottlieb 	 * mlock, madvise, munmap, etc.) We do not support a clone of the VMA,
15907c2344c3SMaor Gottlieb 	 * as this VMA is strongly hardware related.  Therefore we set the
15917c2344c3SMaor Gottlieb 	 * vm_ops of the newly created/cloned VMA to NULL, to prevent it from
15927c2344c3SMaor Gottlieb 	 * calling us again and trying to do incorrect actions.  We assume that
15937c2344c3SMaor Gottlieb 	 * the original VMA size is exactly a single page, and therefore all
15947c2344c3SMaor Gottlieb 	 * "splitting" operation will not happen to it.
15957c2344c3SMaor Gottlieb 	 */
15967c2344c3SMaor Gottlieb 	area->vm_ops = NULL;
15977c2344c3SMaor Gottlieb }
15987c2344c3SMaor Gottlieb 
15997c2344c3SMaor Gottlieb static void  mlx5_ib_vma_close(struct vm_area_struct *area)
16007c2344c3SMaor Gottlieb {
16017c2344c3SMaor Gottlieb 	struct mlx5_ib_vma_private_data *mlx5_ib_vma_priv_data;
16027c2344c3SMaor Gottlieb 
16037c2344c3SMaor Gottlieb 	/* It's guaranteed that all VMAs opened on a FD are closed before the
16047c2344c3SMaor Gottlieb 	 * file itself is closed, therefore no sync is needed with the regular
16057c2344c3SMaor Gottlieb 	 * closing flow. (e.g. mlx5 ib_dealloc_ucontext)
16067c2344c3SMaor Gottlieb 	 * However need a sync with accessing the vma as part of
16077c2344c3SMaor Gottlieb 	 * mlx5_ib_disassociate_ucontext.
16087c2344c3SMaor Gottlieb 	 * The close operation is usually called under mm->mmap_sem except when
16097c2344c3SMaor Gottlieb 	 * process is exiting.
16107c2344c3SMaor Gottlieb 	 * The exiting case is handled explicitly as part of
16117c2344c3SMaor Gottlieb 	 * mlx5_ib_disassociate_ucontext.
16127c2344c3SMaor Gottlieb 	 */
16137c2344c3SMaor Gottlieb 	mlx5_ib_vma_priv_data = (struct mlx5_ib_vma_private_data *)area->vm_private_data;
16147c2344c3SMaor Gottlieb 
16157c2344c3SMaor Gottlieb 	/* setting the vma context pointer to null in the mlx5_ib driver's
16167c2344c3SMaor Gottlieb 	 * private data, to protect a race condition in
16177c2344c3SMaor Gottlieb 	 * mlx5_ib_disassociate_ucontext().
16187c2344c3SMaor Gottlieb 	 */
16197c2344c3SMaor Gottlieb 	mlx5_ib_vma_priv_data->vma = NULL;
16207c2344c3SMaor Gottlieb 	list_del(&mlx5_ib_vma_priv_data->list);
16217c2344c3SMaor Gottlieb 	kfree(mlx5_ib_vma_priv_data);
16227c2344c3SMaor Gottlieb }
16237c2344c3SMaor Gottlieb 
16247c2344c3SMaor Gottlieb static const struct vm_operations_struct mlx5_ib_vm_ops = {
16257c2344c3SMaor Gottlieb 	.open = mlx5_ib_vma_open,
16267c2344c3SMaor Gottlieb 	.close = mlx5_ib_vma_close
16277c2344c3SMaor Gottlieb };
16287c2344c3SMaor Gottlieb 
16297c2344c3SMaor Gottlieb static int mlx5_ib_set_vma_data(struct vm_area_struct *vma,
16307c2344c3SMaor Gottlieb 				struct mlx5_ib_ucontext *ctx)
16317c2344c3SMaor Gottlieb {
16327c2344c3SMaor Gottlieb 	struct mlx5_ib_vma_private_data *vma_prv;
16337c2344c3SMaor Gottlieb 	struct list_head *vma_head = &ctx->vma_private_list;
16347c2344c3SMaor Gottlieb 
16357c2344c3SMaor Gottlieb 	vma_prv = kzalloc(sizeof(*vma_prv), GFP_KERNEL);
16367c2344c3SMaor Gottlieb 	if (!vma_prv)
16377c2344c3SMaor Gottlieb 		return -ENOMEM;
16387c2344c3SMaor Gottlieb 
16397c2344c3SMaor Gottlieb 	vma_prv->vma = vma;
16407c2344c3SMaor Gottlieb 	vma->vm_private_data = vma_prv;
16417c2344c3SMaor Gottlieb 	vma->vm_ops =  &mlx5_ib_vm_ops;
16427c2344c3SMaor Gottlieb 
16437c2344c3SMaor Gottlieb 	list_add(&vma_prv->list, vma_head);
16447c2344c3SMaor Gottlieb 
16457c2344c3SMaor Gottlieb 	return 0;
16467c2344c3SMaor Gottlieb }
16477c2344c3SMaor Gottlieb 
16487c2344c3SMaor Gottlieb static void mlx5_ib_disassociate_ucontext(struct ib_ucontext *ibcontext)
16497c2344c3SMaor Gottlieb {
16507c2344c3SMaor Gottlieb 	int ret;
16517c2344c3SMaor Gottlieb 	struct vm_area_struct *vma;
16527c2344c3SMaor Gottlieb 	struct mlx5_ib_vma_private_data *vma_private, *n;
16537c2344c3SMaor Gottlieb 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
16547c2344c3SMaor Gottlieb 	struct task_struct *owning_process  = NULL;
16557c2344c3SMaor Gottlieb 	struct mm_struct   *owning_mm       = NULL;
16567c2344c3SMaor Gottlieb 
16577c2344c3SMaor Gottlieb 	owning_process = get_pid_task(ibcontext->tgid, PIDTYPE_PID);
16587c2344c3SMaor Gottlieb 	if (!owning_process)
16597c2344c3SMaor Gottlieb 		return;
16607c2344c3SMaor Gottlieb 
16617c2344c3SMaor Gottlieb 	owning_mm = get_task_mm(owning_process);
16627c2344c3SMaor Gottlieb 	if (!owning_mm) {
16637c2344c3SMaor Gottlieb 		pr_info("no mm, disassociate ucontext is pending task termination\n");
16647c2344c3SMaor Gottlieb 		while (1) {
16657c2344c3SMaor Gottlieb 			put_task_struct(owning_process);
16667c2344c3SMaor Gottlieb 			usleep_range(1000, 2000);
16677c2344c3SMaor Gottlieb 			owning_process = get_pid_task(ibcontext->tgid,
16687c2344c3SMaor Gottlieb 						      PIDTYPE_PID);
16697c2344c3SMaor Gottlieb 			if (!owning_process ||
16707c2344c3SMaor Gottlieb 			    owning_process->state == TASK_DEAD) {
16717c2344c3SMaor Gottlieb 				pr_info("disassociate ucontext done, task was terminated\n");
16727c2344c3SMaor Gottlieb 				/* in case task was dead need to release the
16737c2344c3SMaor Gottlieb 				 * task struct.
16747c2344c3SMaor Gottlieb 				 */
16757c2344c3SMaor Gottlieb 				if (owning_process)
16767c2344c3SMaor Gottlieb 					put_task_struct(owning_process);
16777c2344c3SMaor Gottlieb 				return;
16787c2344c3SMaor Gottlieb 			}
16797c2344c3SMaor Gottlieb 		}
16807c2344c3SMaor Gottlieb 	}
16817c2344c3SMaor Gottlieb 
16827c2344c3SMaor Gottlieb 	/* need to protect from a race on closing the vma as part of
16837c2344c3SMaor Gottlieb 	 * mlx5_ib_vma_close.
16847c2344c3SMaor Gottlieb 	 */
1685ecc7d83bSMaor Gottlieb 	down_write(&owning_mm->mmap_sem);
16867c2344c3SMaor Gottlieb 	list_for_each_entry_safe(vma_private, n, &context->vma_private_list,
16877c2344c3SMaor Gottlieb 				 list) {
16887c2344c3SMaor Gottlieb 		vma = vma_private->vma;
16897c2344c3SMaor Gottlieb 		ret = zap_vma_ptes(vma, vma->vm_start,
16907c2344c3SMaor Gottlieb 				   PAGE_SIZE);
16917c2344c3SMaor Gottlieb 		WARN_ONCE(ret, "%s: zap_vma_ptes failed", __func__);
16927c2344c3SMaor Gottlieb 		/* context going to be destroyed, should
16937c2344c3SMaor Gottlieb 		 * not access ops any more.
16947c2344c3SMaor Gottlieb 		 */
169513776612SMaor Gottlieb 		vma->vm_flags &= ~(VM_SHARED | VM_MAYSHARE);
16967c2344c3SMaor Gottlieb 		vma->vm_ops = NULL;
16977c2344c3SMaor Gottlieb 		list_del(&vma_private->list);
16987c2344c3SMaor Gottlieb 		kfree(vma_private);
16997c2344c3SMaor Gottlieb 	}
1700ecc7d83bSMaor Gottlieb 	up_write(&owning_mm->mmap_sem);
17017c2344c3SMaor Gottlieb 	mmput(owning_mm);
17027c2344c3SMaor Gottlieb 	put_task_struct(owning_process);
17037c2344c3SMaor Gottlieb }
17047c2344c3SMaor Gottlieb 
170537aa5c36SGuy Levi static inline char *mmap_cmd2str(enum mlx5_ib_mmap_cmd cmd)
1706e126ba97SEli Cohen {
170737aa5c36SGuy Levi 	switch (cmd) {
170837aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
170937aa5c36SGuy Levi 		return "WC";
1710e126ba97SEli Cohen 	case MLX5_IB_MMAP_REGULAR_PAGE:
171137aa5c36SGuy Levi 		return "best effort WC";
171237aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
171337aa5c36SGuy Levi 		return "NC";
171437aa5c36SGuy Levi 	default:
171537aa5c36SGuy Levi 		return NULL;
171637aa5c36SGuy Levi 	}
171737aa5c36SGuy Levi }
171837aa5c36SGuy Levi 
171937aa5c36SGuy Levi static int uar_mmap(struct mlx5_ib_dev *dev, enum mlx5_ib_mmap_cmd cmd,
17207c2344c3SMaor Gottlieb 		    struct vm_area_struct *vma,
17217c2344c3SMaor Gottlieb 		    struct mlx5_ib_ucontext *context)
172237aa5c36SGuy Levi {
17232f5ff264SEli Cohen 	struct mlx5_bfreg_info *bfregi = &context->bfregi;
172437aa5c36SGuy Levi 	int err;
172537aa5c36SGuy Levi 	unsigned long idx;
172637aa5c36SGuy Levi 	phys_addr_t pfn, pa;
172737aa5c36SGuy Levi 	pgprot_t prot;
1728b037c29aSEli Cohen 	int uars_per_page;
1729b037c29aSEli Cohen 
1730b037c29aSEli Cohen 	if (vma->vm_end - vma->vm_start != PAGE_SIZE)
1731b037c29aSEli Cohen 		return -EINVAL;
1732b037c29aSEli Cohen 
1733b037c29aSEli Cohen 	uars_per_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k);
1734b037c29aSEli Cohen 	idx = get_index(vma->vm_pgoff);
1735b037c29aSEli Cohen 	if (idx % uars_per_page ||
1736b037c29aSEli Cohen 	    idx * uars_per_page >= bfregi->num_sys_pages) {
1737b037c29aSEli Cohen 		mlx5_ib_warn(dev, "invalid uar index %lu\n", idx);
1738b037c29aSEli Cohen 		return -EINVAL;
1739b037c29aSEli Cohen 	}
174037aa5c36SGuy Levi 
174137aa5c36SGuy Levi 	switch (cmd) {
174237aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
174337aa5c36SGuy Levi /* Some architectures don't support WC memory */
174437aa5c36SGuy Levi #if defined(CONFIG_X86)
174537aa5c36SGuy Levi 		if (!pat_enabled())
174637aa5c36SGuy Levi 			return -EPERM;
174737aa5c36SGuy Levi #elif !(defined(CONFIG_PPC) || (defined(CONFIG_ARM) && defined(CONFIG_MMU)))
174837aa5c36SGuy Levi 			return -EPERM;
174937aa5c36SGuy Levi #endif
175037aa5c36SGuy Levi 	/* fall through */
175137aa5c36SGuy Levi 	case MLX5_IB_MMAP_REGULAR_PAGE:
175237aa5c36SGuy Levi 		/* For MLX5_IB_MMAP_REGULAR_PAGE do the best effort to get WC */
175337aa5c36SGuy Levi 		prot = pgprot_writecombine(vma->vm_page_prot);
175437aa5c36SGuy Levi 		break;
175537aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
175637aa5c36SGuy Levi 		prot = pgprot_noncached(vma->vm_page_prot);
175737aa5c36SGuy Levi 		break;
175837aa5c36SGuy Levi 	default:
175937aa5c36SGuy Levi 		return -EINVAL;
176037aa5c36SGuy Levi 	}
176137aa5c36SGuy Levi 
1762b037c29aSEli Cohen 	pfn = uar_index2pfn(dev, bfregi, idx);
176337aa5c36SGuy Levi 	mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn %pa\n", idx, &pfn);
1764e126ba97SEli Cohen 
176537aa5c36SGuy Levi 	vma->vm_page_prot = prot;
176637aa5c36SGuy Levi 	err = io_remap_pfn_range(vma, vma->vm_start, pfn,
176737aa5c36SGuy Levi 				 PAGE_SIZE, vma->vm_page_prot);
176837aa5c36SGuy Levi 	if (err) {
176937aa5c36SGuy Levi 		mlx5_ib_err(dev, "io_remap_pfn_range failed with error=%d, vm_start=0x%lx, pfn=%pa, mmap_cmd=%s\n",
177037aa5c36SGuy Levi 			    err, vma->vm_start, &pfn, mmap_cmd2str(cmd));
1771e126ba97SEli Cohen 		return -EAGAIN;
177237aa5c36SGuy Levi 	}
1773e126ba97SEli Cohen 
177437aa5c36SGuy Levi 	pa = pfn << PAGE_SHIFT;
177537aa5c36SGuy Levi 	mlx5_ib_dbg(dev, "mapped %s at 0x%lx, PA %pa\n", mmap_cmd2str(cmd),
177637aa5c36SGuy Levi 		    vma->vm_start, &pa);
177737aa5c36SGuy Levi 
17787c2344c3SMaor Gottlieb 	return mlx5_ib_set_vma_data(vma, context);
177937aa5c36SGuy Levi }
178037aa5c36SGuy Levi 
178137aa5c36SGuy Levi static int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma)
178237aa5c36SGuy Levi {
178337aa5c36SGuy Levi 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
178437aa5c36SGuy Levi 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
178537aa5c36SGuy Levi 	unsigned long command;
178637aa5c36SGuy Levi 	phys_addr_t pfn;
178737aa5c36SGuy Levi 
178837aa5c36SGuy Levi 	command = get_command(vma->vm_pgoff);
178937aa5c36SGuy Levi 	switch (command) {
179037aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
179137aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
179237aa5c36SGuy Levi 	case MLX5_IB_MMAP_REGULAR_PAGE:
17937c2344c3SMaor Gottlieb 		return uar_mmap(dev, command, vma, context);
1794e126ba97SEli Cohen 
1795e126ba97SEli Cohen 	case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES:
1796e126ba97SEli Cohen 		return -ENOSYS;
1797e126ba97SEli Cohen 
1798d69e3bcfSMatan Barak 	case MLX5_IB_MMAP_CORE_CLOCK:
1799d69e3bcfSMatan Barak 		if (vma->vm_end - vma->vm_start != PAGE_SIZE)
1800d69e3bcfSMatan Barak 			return -EINVAL;
1801d69e3bcfSMatan Barak 
18026cbac1e4SMatan Barak 		if (vma->vm_flags & VM_WRITE)
1803d69e3bcfSMatan Barak 			return -EPERM;
1804d69e3bcfSMatan Barak 
1805d69e3bcfSMatan Barak 		/* Don't expose to user-space information it shouldn't have */
1806d69e3bcfSMatan Barak 		if (PAGE_SIZE > 4096)
1807d69e3bcfSMatan Barak 			return -EOPNOTSUPP;
1808d69e3bcfSMatan Barak 
1809d69e3bcfSMatan Barak 		vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1810d69e3bcfSMatan Barak 		pfn = (dev->mdev->iseg_base +
1811d69e3bcfSMatan Barak 		       offsetof(struct mlx5_init_seg, internal_timer_h)) >>
1812d69e3bcfSMatan Barak 			PAGE_SHIFT;
1813d69e3bcfSMatan Barak 		if (io_remap_pfn_range(vma, vma->vm_start, pfn,
1814d69e3bcfSMatan Barak 				       PAGE_SIZE, vma->vm_page_prot))
1815d69e3bcfSMatan Barak 			return -EAGAIN;
1816d69e3bcfSMatan Barak 
1817d69e3bcfSMatan Barak 		mlx5_ib_dbg(dev, "mapped internal timer at 0x%lx, PA 0x%llx\n",
1818d69e3bcfSMatan Barak 			    vma->vm_start,
1819d69e3bcfSMatan Barak 			    (unsigned long long)pfn << PAGE_SHIFT);
1820d69e3bcfSMatan Barak 		break;
1821d69e3bcfSMatan Barak 
1822e126ba97SEli Cohen 	default:
1823e126ba97SEli Cohen 		return -EINVAL;
1824e126ba97SEli Cohen 	}
1825e126ba97SEli Cohen 
1826e126ba97SEli Cohen 	return 0;
1827e126ba97SEli Cohen }
1828e126ba97SEli Cohen 
1829e126ba97SEli Cohen static struct ib_pd *mlx5_ib_alloc_pd(struct ib_device *ibdev,
1830e126ba97SEli Cohen 				      struct ib_ucontext *context,
1831e126ba97SEli Cohen 				      struct ib_udata *udata)
1832e126ba97SEli Cohen {
1833e126ba97SEli Cohen 	struct mlx5_ib_alloc_pd_resp resp;
1834e126ba97SEli Cohen 	struct mlx5_ib_pd *pd;
1835e126ba97SEli Cohen 	int err;
1836e126ba97SEli Cohen 
1837e126ba97SEli Cohen 	pd = kmalloc(sizeof(*pd), GFP_KERNEL);
1838e126ba97SEli Cohen 	if (!pd)
1839e126ba97SEli Cohen 		return ERR_PTR(-ENOMEM);
1840e126ba97SEli Cohen 
18419603b61dSJack Morgenstein 	err = mlx5_core_alloc_pd(to_mdev(ibdev)->mdev, &pd->pdn);
1842e126ba97SEli Cohen 	if (err) {
1843e126ba97SEli Cohen 		kfree(pd);
1844e126ba97SEli Cohen 		return ERR_PTR(err);
1845e126ba97SEli Cohen 	}
1846e126ba97SEli Cohen 
1847e126ba97SEli Cohen 	if (context) {
1848e126ba97SEli Cohen 		resp.pdn = pd->pdn;
1849e126ba97SEli Cohen 		if (ib_copy_to_udata(udata, &resp, sizeof(resp))) {
18509603b61dSJack Morgenstein 			mlx5_core_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn);
1851e126ba97SEli Cohen 			kfree(pd);
1852e126ba97SEli Cohen 			return ERR_PTR(-EFAULT);
1853e126ba97SEli Cohen 		}
1854e126ba97SEli Cohen 	}
1855e126ba97SEli Cohen 
1856e126ba97SEli Cohen 	return &pd->ibpd;
1857e126ba97SEli Cohen }
1858e126ba97SEli Cohen 
1859e126ba97SEli Cohen static int mlx5_ib_dealloc_pd(struct ib_pd *pd)
1860e126ba97SEli Cohen {
1861e126ba97SEli Cohen 	struct mlx5_ib_dev *mdev = to_mdev(pd->device);
1862e126ba97SEli Cohen 	struct mlx5_ib_pd *mpd = to_mpd(pd);
1863e126ba97SEli Cohen 
18649603b61dSJack Morgenstein 	mlx5_core_dealloc_pd(mdev->mdev, mpd->pdn);
1865e126ba97SEli Cohen 	kfree(mpd);
1866e126ba97SEli Cohen 
1867e126ba97SEli Cohen 	return 0;
1868e126ba97SEli Cohen }
1869e126ba97SEli Cohen 
1870466fa6d2SMaor Gottlieb enum {
1871466fa6d2SMaor Gottlieb 	MATCH_CRITERIA_ENABLE_OUTER_BIT,
1872466fa6d2SMaor Gottlieb 	MATCH_CRITERIA_ENABLE_MISC_BIT,
1873466fa6d2SMaor Gottlieb 	MATCH_CRITERIA_ENABLE_INNER_BIT
1874466fa6d2SMaor Gottlieb };
1875038d2ef8SMaor Gottlieb 
1876466fa6d2SMaor Gottlieb #define HEADER_IS_ZERO(match_criteria, headers)			           \
1877466fa6d2SMaor Gottlieb 	!(memchr_inv(MLX5_ADDR_OF(fte_match_param, match_criteria, headers), \
1878466fa6d2SMaor Gottlieb 		    0, MLX5_FLD_SZ_BYTES(fte_match_param, headers)))       \
1879466fa6d2SMaor Gottlieb 
1880466fa6d2SMaor Gottlieb static u8 get_match_criteria_enable(u32 *match_criteria)
1881466fa6d2SMaor Gottlieb {
1882466fa6d2SMaor Gottlieb 	u8 match_criteria_enable;
1883466fa6d2SMaor Gottlieb 
1884466fa6d2SMaor Gottlieb 	match_criteria_enable =
1885466fa6d2SMaor Gottlieb 		(!HEADER_IS_ZERO(match_criteria, outer_headers)) <<
1886466fa6d2SMaor Gottlieb 		MATCH_CRITERIA_ENABLE_OUTER_BIT;
1887466fa6d2SMaor Gottlieb 	match_criteria_enable |=
1888466fa6d2SMaor Gottlieb 		(!HEADER_IS_ZERO(match_criteria, misc_parameters)) <<
1889466fa6d2SMaor Gottlieb 		MATCH_CRITERIA_ENABLE_MISC_BIT;
1890466fa6d2SMaor Gottlieb 	match_criteria_enable |=
1891466fa6d2SMaor Gottlieb 		(!HEADER_IS_ZERO(match_criteria, inner_headers)) <<
1892466fa6d2SMaor Gottlieb 		MATCH_CRITERIA_ENABLE_INNER_BIT;
1893466fa6d2SMaor Gottlieb 
1894466fa6d2SMaor Gottlieb 	return match_criteria_enable;
1895038d2ef8SMaor Gottlieb }
1896038d2ef8SMaor Gottlieb 
1897ca0d4753SMaor Gottlieb static void set_proto(void *outer_c, void *outer_v, u8 mask, u8 val)
1898ca0d4753SMaor Gottlieb {
1899ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_protocol, mask);
1900ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_protocol, val);
1901ca0d4753SMaor Gottlieb }
1902ca0d4753SMaor Gottlieb 
19032d1e697eSMoses Reuben static void set_flow_label(void *misc_c, void *misc_v, u8 mask, u8 val,
19042d1e697eSMoses Reuben 			   bool inner)
19052d1e697eSMoses Reuben {
19062d1e697eSMoses Reuben 	if (inner) {
19072d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
19082d1e697eSMoses Reuben 			 misc_c, inner_ipv6_flow_label, mask);
19092d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
19102d1e697eSMoses Reuben 			 misc_v, inner_ipv6_flow_label, val);
19112d1e697eSMoses Reuben 	} else {
19122d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
19132d1e697eSMoses Reuben 			 misc_c, outer_ipv6_flow_label, mask);
19142d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
19152d1e697eSMoses Reuben 			 misc_v, outer_ipv6_flow_label, val);
19162d1e697eSMoses Reuben 	}
19172d1e697eSMoses Reuben }
19182d1e697eSMoses Reuben 
1919ca0d4753SMaor Gottlieb static void set_tos(void *outer_c, void *outer_v, u8 mask, u8 val)
1920ca0d4753SMaor Gottlieb {
1921ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_ecn, mask);
1922ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_ecn, val);
1923ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_dscp, mask >> 2);
1924ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_dscp, val >> 2);
1925ca0d4753SMaor Gottlieb }
1926ca0d4753SMaor Gottlieb 
1927c47ac6aeSMaor Gottlieb #define LAST_ETH_FIELD vlan_tag
1928c47ac6aeSMaor Gottlieb #define LAST_IB_FIELD sl
1929ca0d4753SMaor Gottlieb #define LAST_IPV4_FIELD tos
1930466fa6d2SMaor Gottlieb #define LAST_IPV6_FIELD traffic_class
1931c47ac6aeSMaor Gottlieb #define LAST_TCP_UDP_FIELD src_port
1932ffb30d8fSMoses Reuben #define LAST_TUNNEL_FIELD tunnel_id
19332ac693f9SMoses Reuben #define LAST_FLOW_TAG_FIELD tag_id
1934a22ed86cSSlava Shwartsman #define LAST_DROP_FIELD size
1935c47ac6aeSMaor Gottlieb 
1936c47ac6aeSMaor Gottlieb /* Field is the last supported field */
1937c47ac6aeSMaor Gottlieb #define FIELDS_NOT_SUPPORTED(filter, field)\
1938c47ac6aeSMaor Gottlieb 	memchr_inv((void *)&filter.field  +\
1939c47ac6aeSMaor Gottlieb 		   sizeof(filter.field), 0,\
1940c47ac6aeSMaor Gottlieb 		   sizeof(filter) -\
1941c47ac6aeSMaor Gottlieb 		   offsetof(typeof(filter), field) -\
1942c47ac6aeSMaor Gottlieb 		   sizeof(filter.field))
1943c47ac6aeSMaor Gottlieb 
194419cc7524SAriel Levkovich #define IPV4_VERSION 4
194519cc7524SAriel Levkovich #define IPV6_VERSION 6
194619cc7524SAriel Levkovich static int parse_flow_attr(struct mlx5_core_dev *mdev, u32 *match_c,
194719cc7524SAriel Levkovich 			   u32 *match_v, const union ib_flow_spec *ib_spec,
1948a22ed86cSSlava Shwartsman 			   u32 *tag_id, bool *is_drop)
1949038d2ef8SMaor Gottlieb {
1950466fa6d2SMaor Gottlieb 	void *misc_params_c = MLX5_ADDR_OF(fte_match_param, match_c,
1951466fa6d2SMaor Gottlieb 					   misc_parameters);
1952466fa6d2SMaor Gottlieb 	void *misc_params_v = MLX5_ADDR_OF(fte_match_param, match_v,
1953466fa6d2SMaor Gottlieb 					   misc_parameters);
19542d1e697eSMoses Reuben 	void *headers_c;
19552d1e697eSMoses Reuben 	void *headers_v;
195619cc7524SAriel Levkovich 	int match_ipv;
1957466fa6d2SMaor Gottlieb 
19582d1e697eSMoses Reuben 	if (ib_spec->type & IB_FLOW_SPEC_INNER) {
19592d1e697eSMoses Reuben 		headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
19602d1e697eSMoses Reuben 					 inner_headers);
19612d1e697eSMoses Reuben 		headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
19622d1e697eSMoses Reuben 					 inner_headers);
196319cc7524SAriel Levkovich 		match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
196419cc7524SAriel Levkovich 					ft_field_support.inner_ip_version);
19652d1e697eSMoses Reuben 	} else {
19662d1e697eSMoses Reuben 		headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
19672d1e697eSMoses Reuben 					 outer_headers);
19682d1e697eSMoses Reuben 		headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
19692d1e697eSMoses Reuben 					 outer_headers);
197019cc7524SAriel Levkovich 		match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
197119cc7524SAriel Levkovich 					ft_field_support.outer_ip_version);
19722d1e697eSMoses Reuben 	}
19732d1e697eSMoses Reuben 
19742d1e697eSMoses Reuben 	switch (ib_spec->type & ~IB_FLOW_SPEC_INNER) {
1975038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_ETH:
1976c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->eth.mask, LAST_ETH_FIELD))
19771ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
1978038d2ef8SMaor Gottlieb 
19792d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
1980038d2ef8SMaor Gottlieb 					     dmac_47_16),
1981038d2ef8SMaor Gottlieb 				ib_spec->eth.mask.dst_mac);
19822d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
1983038d2ef8SMaor Gottlieb 					     dmac_47_16),
1984038d2ef8SMaor Gottlieb 				ib_spec->eth.val.dst_mac);
1985038d2ef8SMaor Gottlieb 
19862d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
1987ee3da804SMaor Gottlieb 					     smac_47_16),
1988ee3da804SMaor Gottlieb 				ib_spec->eth.mask.src_mac);
19892d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
1990ee3da804SMaor Gottlieb 					     smac_47_16),
1991ee3da804SMaor Gottlieb 				ib_spec->eth.val.src_mac);
1992ee3da804SMaor Gottlieb 
1993038d2ef8SMaor Gottlieb 		if (ib_spec->eth.mask.vlan_tag) {
19942d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
199510543365SMohamad Haj Yahia 				 cvlan_tag, 1);
19962d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
199710543365SMohamad Haj Yahia 				 cvlan_tag, 1);
1998038d2ef8SMaor Gottlieb 
19992d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2000038d2ef8SMaor Gottlieb 				 first_vid, ntohs(ib_spec->eth.mask.vlan_tag));
20012d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2002038d2ef8SMaor Gottlieb 				 first_vid, ntohs(ib_spec->eth.val.vlan_tag));
2003038d2ef8SMaor Gottlieb 
20042d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2005038d2ef8SMaor Gottlieb 				 first_cfi,
2006038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.mask.vlan_tag) >> 12);
20072d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2008038d2ef8SMaor Gottlieb 				 first_cfi,
2009038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.val.vlan_tag) >> 12);
2010038d2ef8SMaor Gottlieb 
20112d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2012038d2ef8SMaor Gottlieb 				 first_prio,
2013038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.mask.vlan_tag) >> 13);
20142d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2015038d2ef8SMaor Gottlieb 				 first_prio,
2016038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.val.vlan_tag) >> 13);
2017038d2ef8SMaor Gottlieb 		}
20182d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2019038d2ef8SMaor Gottlieb 			 ethertype, ntohs(ib_spec->eth.mask.ether_type));
20202d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2021038d2ef8SMaor Gottlieb 			 ethertype, ntohs(ib_spec->eth.val.ether_type));
2022038d2ef8SMaor Gottlieb 		break;
2023038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_IPV4:
2024c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->ipv4.mask, LAST_IPV4_FIELD))
20251ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2026038d2ef8SMaor Gottlieb 
202719cc7524SAriel Levkovich 		if (match_ipv) {
202819cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
202919cc7524SAriel Levkovich 				 ip_version, 0xf);
203019cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
203119cc7524SAriel Levkovich 				 ip_version, IPV4_VERSION);
203219cc7524SAriel Levkovich 		} else {
20332d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2034038d2ef8SMaor Gottlieb 				 ethertype, 0xffff);
20352d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2036038d2ef8SMaor Gottlieb 				 ethertype, ETH_P_IP);
203719cc7524SAriel Levkovich 		}
2038038d2ef8SMaor Gottlieb 
20392d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2040038d2ef8SMaor Gottlieb 				    src_ipv4_src_ipv6.ipv4_layout.ipv4),
2041038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.mask.src_ip,
2042038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.mask.src_ip));
20432d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2044038d2ef8SMaor Gottlieb 				    src_ipv4_src_ipv6.ipv4_layout.ipv4),
2045038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.val.src_ip,
2046038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.val.src_ip));
20472d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2048038d2ef8SMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2049038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.mask.dst_ip,
2050038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.mask.dst_ip));
20512d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2052038d2ef8SMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2053038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.val.dst_ip,
2054038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.val.dst_ip));
2055ca0d4753SMaor Gottlieb 
20562d1e697eSMoses Reuben 		set_tos(headers_c, headers_v,
2057ca0d4753SMaor Gottlieb 			ib_spec->ipv4.mask.tos, ib_spec->ipv4.val.tos);
2058ca0d4753SMaor Gottlieb 
20592d1e697eSMoses Reuben 		set_proto(headers_c, headers_v,
2060ca0d4753SMaor Gottlieb 			  ib_spec->ipv4.mask.proto, ib_spec->ipv4.val.proto);
2061038d2ef8SMaor Gottlieb 		break;
2062026bae0cSMaor Gottlieb 	case IB_FLOW_SPEC_IPV6:
2063c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->ipv6.mask, LAST_IPV6_FIELD))
20641ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2065026bae0cSMaor Gottlieb 
206619cc7524SAriel Levkovich 		if (match_ipv) {
206719cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
206819cc7524SAriel Levkovich 				 ip_version, 0xf);
206919cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
207019cc7524SAriel Levkovich 				 ip_version, IPV6_VERSION);
207119cc7524SAriel Levkovich 		} else {
20722d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2073026bae0cSMaor Gottlieb 				 ethertype, 0xffff);
20742d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2075026bae0cSMaor Gottlieb 				 ethertype, ETH_P_IPV6);
207619cc7524SAriel Levkovich 		}
2077026bae0cSMaor Gottlieb 
20782d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2079026bae0cSMaor Gottlieb 				    src_ipv4_src_ipv6.ipv6_layout.ipv6),
2080026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.mask.src_ip,
2081026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.mask.src_ip));
20822d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2083026bae0cSMaor Gottlieb 				    src_ipv4_src_ipv6.ipv6_layout.ipv6),
2084026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.val.src_ip,
2085026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.val.src_ip));
20862d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2087026bae0cSMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2088026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.mask.dst_ip,
2089026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.mask.dst_ip));
20902d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2091026bae0cSMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2092026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.val.dst_ip,
2093026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.val.dst_ip));
2094466fa6d2SMaor Gottlieb 
20952d1e697eSMoses Reuben 		set_tos(headers_c, headers_v,
2096466fa6d2SMaor Gottlieb 			ib_spec->ipv6.mask.traffic_class,
2097466fa6d2SMaor Gottlieb 			ib_spec->ipv6.val.traffic_class);
2098466fa6d2SMaor Gottlieb 
20992d1e697eSMoses Reuben 		set_proto(headers_c, headers_v,
2100466fa6d2SMaor Gottlieb 			  ib_spec->ipv6.mask.next_hdr,
2101466fa6d2SMaor Gottlieb 			  ib_spec->ipv6.val.next_hdr);
2102466fa6d2SMaor Gottlieb 
21032d1e697eSMoses Reuben 		set_flow_label(misc_params_c, misc_params_v,
21042d1e697eSMoses Reuben 			       ntohl(ib_spec->ipv6.mask.flow_label),
21052d1e697eSMoses Reuben 			       ntohl(ib_spec->ipv6.val.flow_label),
21062d1e697eSMoses Reuben 			       ib_spec->type & IB_FLOW_SPEC_INNER);
21072d1e697eSMoses Reuben 
2108026bae0cSMaor Gottlieb 		break;
2109038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_TCP:
2110c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
2111c47ac6aeSMaor Gottlieb 					 LAST_TCP_UDP_FIELD))
21121ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2113038d2ef8SMaor Gottlieb 
21142d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
2115038d2ef8SMaor Gottlieb 			 0xff);
21162d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
2117038d2ef8SMaor Gottlieb 			 IPPROTO_TCP);
2118038d2ef8SMaor Gottlieb 
21192d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_sport,
2120038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.src_port));
21212d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_sport,
2122038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.src_port));
2123038d2ef8SMaor Gottlieb 
21242d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_dport,
2125038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.dst_port));
21262d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_dport,
2127038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.dst_port));
2128038d2ef8SMaor Gottlieb 		break;
2129038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_UDP:
2130c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
2131c47ac6aeSMaor Gottlieb 					 LAST_TCP_UDP_FIELD))
21321ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2133038d2ef8SMaor Gottlieb 
21342d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
2135038d2ef8SMaor Gottlieb 			 0xff);
21362d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
2137038d2ef8SMaor Gottlieb 			 IPPROTO_UDP);
2138038d2ef8SMaor Gottlieb 
21392d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_sport,
2140038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.src_port));
21412d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_sport,
2142038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.src_port));
2143038d2ef8SMaor Gottlieb 
21442d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_dport,
2145038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.dst_port));
21462d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport,
2147038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.dst_port));
2148038d2ef8SMaor Gottlieb 		break;
2149ffb30d8fSMoses Reuben 	case IB_FLOW_SPEC_VXLAN_TUNNEL:
2150ffb30d8fSMoses Reuben 		if (FIELDS_NOT_SUPPORTED(ib_spec->tunnel.mask,
2151ffb30d8fSMoses Reuben 					 LAST_TUNNEL_FIELD))
21521ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2153ffb30d8fSMoses Reuben 
2154ffb30d8fSMoses Reuben 		MLX5_SET(fte_match_set_misc, misc_params_c, vxlan_vni,
2155ffb30d8fSMoses Reuben 			 ntohl(ib_spec->tunnel.mask.tunnel_id));
2156ffb30d8fSMoses Reuben 		MLX5_SET(fte_match_set_misc, misc_params_v, vxlan_vni,
2157ffb30d8fSMoses Reuben 			 ntohl(ib_spec->tunnel.val.tunnel_id));
2158ffb30d8fSMoses Reuben 		break;
21592ac693f9SMoses Reuben 	case IB_FLOW_SPEC_ACTION_TAG:
21602ac693f9SMoses Reuben 		if (FIELDS_NOT_SUPPORTED(ib_spec->flow_tag,
21612ac693f9SMoses Reuben 					 LAST_FLOW_TAG_FIELD))
21622ac693f9SMoses Reuben 			return -EOPNOTSUPP;
21632ac693f9SMoses Reuben 		if (ib_spec->flow_tag.tag_id >= BIT(24))
21642ac693f9SMoses Reuben 			return -EINVAL;
21652ac693f9SMoses Reuben 
21662ac693f9SMoses Reuben 		*tag_id = ib_spec->flow_tag.tag_id;
21672ac693f9SMoses Reuben 		break;
2168a22ed86cSSlava Shwartsman 	case IB_FLOW_SPEC_ACTION_DROP:
2169a22ed86cSSlava Shwartsman 		if (FIELDS_NOT_SUPPORTED(ib_spec->drop,
2170a22ed86cSSlava Shwartsman 					 LAST_DROP_FIELD))
2171a22ed86cSSlava Shwartsman 			return -EOPNOTSUPP;
2172a22ed86cSSlava Shwartsman 		*is_drop = true;
2173a22ed86cSSlava Shwartsman 		break;
2174038d2ef8SMaor Gottlieb 	default:
2175038d2ef8SMaor Gottlieb 		return -EINVAL;
2176038d2ef8SMaor Gottlieb 	}
2177038d2ef8SMaor Gottlieb 
2178038d2ef8SMaor Gottlieb 	return 0;
2179038d2ef8SMaor Gottlieb }
2180038d2ef8SMaor Gottlieb 
2181038d2ef8SMaor Gottlieb /* If a flow could catch both multicast and unicast packets,
2182038d2ef8SMaor Gottlieb  * it won't fall into the multicast flow steering table and this rule
2183038d2ef8SMaor Gottlieb  * could steal other multicast packets.
2184038d2ef8SMaor Gottlieb  */
2185a550ddfcSYishai Hadas static bool flow_is_multicast_only(const struct ib_flow_attr *ib_attr)
2186038d2ef8SMaor Gottlieb {
218781e30880SYishai Hadas 	union ib_flow_spec *flow_spec;
2188038d2ef8SMaor Gottlieb 
2189038d2ef8SMaor Gottlieb 	if (ib_attr->type != IB_FLOW_ATTR_NORMAL ||
2190038d2ef8SMaor Gottlieb 	    ib_attr->num_of_specs < 1)
2191038d2ef8SMaor Gottlieb 		return false;
2192038d2ef8SMaor Gottlieb 
219381e30880SYishai Hadas 	flow_spec = (union ib_flow_spec *)(ib_attr + 1);
219481e30880SYishai Hadas 	if (flow_spec->type == IB_FLOW_SPEC_IPV4) {
219581e30880SYishai Hadas 		struct ib_flow_spec_ipv4 *ipv4_spec;
2196038d2ef8SMaor Gottlieb 
219781e30880SYishai Hadas 		ipv4_spec = (struct ib_flow_spec_ipv4 *)flow_spec;
219881e30880SYishai Hadas 		if (ipv4_is_multicast(ipv4_spec->val.dst_ip))
219981e30880SYishai Hadas 			return true;
220081e30880SYishai Hadas 
220181e30880SYishai Hadas 		return false;
220281e30880SYishai Hadas 	}
220381e30880SYishai Hadas 
220481e30880SYishai Hadas 	if (flow_spec->type == IB_FLOW_SPEC_ETH) {
220581e30880SYishai Hadas 		struct ib_flow_spec_eth *eth_spec;
220681e30880SYishai Hadas 
220781e30880SYishai Hadas 		eth_spec = (struct ib_flow_spec_eth *)flow_spec;
2208038d2ef8SMaor Gottlieb 		return is_multicast_ether_addr(eth_spec->mask.dst_mac) &&
2209038d2ef8SMaor Gottlieb 		       is_multicast_ether_addr(eth_spec->val.dst_mac);
2210038d2ef8SMaor Gottlieb 	}
2211038d2ef8SMaor Gottlieb 
221281e30880SYishai Hadas 	return false;
221381e30880SYishai Hadas }
221481e30880SYishai Hadas 
221519cc7524SAriel Levkovich static bool is_valid_ethertype(struct mlx5_core_dev *mdev,
221619cc7524SAriel Levkovich 			       const struct ib_flow_attr *flow_attr,
22170f750966SAriel Levkovich 			       bool check_inner)
2218038d2ef8SMaor Gottlieb {
2219038d2ef8SMaor Gottlieb 	union ib_flow_spec *ib_spec = (union ib_flow_spec *)(flow_attr + 1);
222019cc7524SAriel Levkovich 	int match_ipv = check_inner ?
222119cc7524SAriel Levkovich 			MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
222219cc7524SAriel Levkovich 					ft_field_support.inner_ip_version) :
222319cc7524SAriel Levkovich 			MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
222419cc7524SAriel Levkovich 					ft_field_support.outer_ip_version);
22250f750966SAriel Levkovich 	int inner_bit = check_inner ? IB_FLOW_SPEC_INNER : 0;
22260f750966SAriel Levkovich 	bool ipv4_spec_valid, ipv6_spec_valid;
22270f750966SAriel Levkovich 	unsigned int ip_spec_type = 0;
22280f750966SAriel Levkovich 	bool has_ethertype = false;
2229038d2ef8SMaor Gottlieb 	unsigned int spec_index;
22300f750966SAriel Levkovich 	bool mask_valid = true;
22310f750966SAriel Levkovich 	u16 eth_type = 0;
22320f750966SAriel Levkovich 	bool type_valid;
2233038d2ef8SMaor Gottlieb 
2234038d2ef8SMaor Gottlieb 	/* Validate that ethertype is correct */
2235038d2ef8SMaor Gottlieb 	for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
22360f750966SAriel Levkovich 		if ((ib_spec->type == (IB_FLOW_SPEC_ETH | inner_bit)) &&
2237038d2ef8SMaor Gottlieb 		    ib_spec->eth.mask.ether_type) {
22380f750966SAriel Levkovich 			mask_valid = (ib_spec->eth.mask.ether_type ==
22390f750966SAriel Levkovich 				      htons(0xffff));
22400f750966SAriel Levkovich 			has_ethertype = true;
22410f750966SAriel Levkovich 			eth_type = ntohs(ib_spec->eth.val.ether_type);
22420f750966SAriel Levkovich 		} else if ((ib_spec->type == (IB_FLOW_SPEC_IPV4 | inner_bit)) ||
22430f750966SAriel Levkovich 			   (ib_spec->type == (IB_FLOW_SPEC_IPV6 | inner_bit))) {
22440f750966SAriel Levkovich 			ip_spec_type = ib_spec->type;
2245038d2ef8SMaor Gottlieb 		}
2246038d2ef8SMaor Gottlieb 		ib_spec = (void *)ib_spec + ib_spec->size;
2247038d2ef8SMaor Gottlieb 	}
22480f750966SAriel Levkovich 
22490f750966SAriel Levkovich 	type_valid = (!has_ethertype) || (!ip_spec_type);
22500f750966SAriel Levkovich 	if (!type_valid && mask_valid) {
22510f750966SAriel Levkovich 		ipv4_spec_valid = (eth_type == ETH_P_IP) &&
22520f750966SAriel Levkovich 			(ip_spec_type == (IB_FLOW_SPEC_IPV4 | inner_bit));
22530f750966SAriel Levkovich 		ipv6_spec_valid = (eth_type == ETH_P_IPV6) &&
22540f750966SAriel Levkovich 			(ip_spec_type == (IB_FLOW_SPEC_IPV6 | inner_bit));
225519cc7524SAriel Levkovich 
225619cc7524SAriel Levkovich 		type_valid = (ipv4_spec_valid) || (ipv6_spec_valid) ||
225719cc7524SAriel Levkovich 			     (((eth_type == ETH_P_MPLS_UC) ||
225819cc7524SAriel Levkovich 			       (eth_type == ETH_P_MPLS_MC)) && match_ipv);
22590f750966SAriel Levkovich 	}
22600f750966SAriel Levkovich 
22610f750966SAriel Levkovich 	return type_valid;
22620f750966SAriel Levkovich }
22630f750966SAriel Levkovich 
226419cc7524SAriel Levkovich static bool is_valid_attr(struct mlx5_core_dev *mdev,
226519cc7524SAriel Levkovich 			  const struct ib_flow_attr *flow_attr)
22660f750966SAriel Levkovich {
226719cc7524SAriel Levkovich 	return is_valid_ethertype(mdev, flow_attr, false) &&
226819cc7524SAriel Levkovich 	       is_valid_ethertype(mdev, flow_attr, true);
2269038d2ef8SMaor Gottlieb }
2270038d2ef8SMaor Gottlieb 
2271038d2ef8SMaor Gottlieb static void put_flow_table(struct mlx5_ib_dev *dev,
2272038d2ef8SMaor Gottlieb 			   struct mlx5_ib_flow_prio *prio, bool ft_added)
2273038d2ef8SMaor Gottlieb {
2274038d2ef8SMaor Gottlieb 	prio->refcount -= !!ft_added;
2275038d2ef8SMaor Gottlieb 	if (!prio->refcount) {
2276038d2ef8SMaor Gottlieb 		mlx5_destroy_flow_table(prio->flow_table);
2277038d2ef8SMaor Gottlieb 		prio->flow_table = NULL;
2278038d2ef8SMaor Gottlieb 	}
2279038d2ef8SMaor Gottlieb }
2280038d2ef8SMaor Gottlieb 
2281038d2ef8SMaor Gottlieb static int mlx5_ib_destroy_flow(struct ib_flow *flow_id)
2282038d2ef8SMaor Gottlieb {
2283038d2ef8SMaor Gottlieb 	struct mlx5_ib_dev *dev = to_mdev(flow_id->qp->device);
2284038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = container_of(flow_id,
2285038d2ef8SMaor Gottlieb 							  struct mlx5_ib_flow_handler,
2286038d2ef8SMaor Gottlieb 							  ibflow);
2287038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *iter, *tmp;
2288038d2ef8SMaor Gottlieb 
2289038d2ef8SMaor Gottlieb 	mutex_lock(&dev->flow_db.lock);
2290038d2ef8SMaor Gottlieb 
2291038d2ef8SMaor Gottlieb 	list_for_each_entry_safe(iter, tmp, &handler->list, list) {
229274491de9SMark Bloch 		mlx5_del_flow_rules(iter->rule);
2293cc0e5d42SMaor Gottlieb 		put_flow_table(dev, iter->prio, true);
2294038d2ef8SMaor Gottlieb 		list_del(&iter->list);
2295038d2ef8SMaor Gottlieb 		kfree(iter);
2296038d2ef8SMaor Gottlieb 	}
2297038d2ef8SMaor Gottlieb 
229874491de9SMark Bloch 	mlx5_del_flow_rules(handler->rule);
22995497adc6SMaor Gottlieb 	put_flow_table(dev, handler->prio, true);
2300038d2ef8SMaor Gottlieb 	mutex_unlock(&dev->flow_db.lock);
2301038d2ef8SMaor Gottlieb 
2302038d2ef8SMaor Gottlieb 	kfree(handler);
2303038d2ef8SMaor Gottlieb 
2304038d2ef8SMaor Gottlieb 	return 0;
2305038d2ef8SMaor Gottlieb }
2306038d2ef8SMaor Gottlieb 
230735d19011SMaor Gottlieb static int ib_prio_to_core_prio(unsigned int priority, bool dont_trap)
230835d19011SMaor Gottlieb {
230935d19011SMaor Gottlieb 	priority *= 2;
231035d19011SMaor Gottlieb 	if (!dont_trap)
231135d19011SMaor Gottlieb 		priority++;
231235d19011SMaor Gottlieb 	return priority;
231335d19011SMaor Gottlieb }
231435d19011SMaor Gottlieb 
2315cc0e5d42SMaor Gottlieb enum flow_table_type {
2316cc0e5d42SMaor Gottlieb 	MLX5_IB_FT_RX,
2317cc0e5d42SMaor Gottlieb 	MLX5_IB_FT_TX
2318cc0e5d42SMaor Gottlieb };
2319cc0e5d42SMaor Gottlieb 
232000b7c2abSMaor Gottlieb #define MLX5_FS_MAX_TYPES	 6
232100b7c2abSMaor Gottlieb #define MLX5_FS_MAX_ENTRIES	 BIT(16)
2322038d2ef8SMaor Gottlieb static struct mlx5_ib_flow_prio *get_flow_table(struct mlx5_ib_dev *dev,
2323cc0e5d42SMaor Gottlieb 						struct ib_flow_attr *flow_attr,
2324cc0e5d42SMaor Gottlieb 						enum flow_table_type ft_type)
2325038d2ef8SMaor Gottlieb {
232635d19011SMaor Gottlieb 	bool dont_trap = flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP;
2327038d2ef8SMaor Gottlieb 	struct mlx5_flow_namespace *ns = NULL;
2328038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_prio *prio;
2329038d2ef8SMaor Gottlieb 	struct mlx5_flow_table *ft;
2330dac388efSMaor Gottlieb 	int max_table_size;
2331038d2ef8SMaor Gottlieb 	int num_entries;
2332038d2ef8SMaor Gottlieb 	int num_groups;
2333038d2ef8SMaor Gottlieb 	int priority;
2334038d2ef8SMaor Gottlieb 	int err = 0;
2335038d2ef8SMaor Gottlieb 
2336dac388efSMaor Gottlieb 	max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
2337dac388efSMaor Gottlieb 						       log_max_ft_size));
2338038d2ef8SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
233935d19011SMaor Gottlieb 		if (flow_is_multicast_only(flow_attr) &&
234035d19011SMaor Gottlieb 		    !dont_trap)
2341038d2ef8SMaor Gottlieb 			priority = MLX5_IB_FLOW_MCAST_PRIO;
2342038d2ef8SMaor Gottlieb 		else
234335d19011SMaor Gottlieb 			priority = ib_prio_to_core_prio(flow_attr->priority,
234435d19011SMaor Gottlieb 							dont_trap);
2345038d2ef8SMaor Gottlieb 		ns = mlx5_get_flow_namespace(dev->mdev,
2346038d2ef8SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_BYPASS);
2347038d2ef8SMaor Gottlieb 		num_entries = MLX5_FS_MAX_ENTRIES;
2348038d2ef8SMaor Gottlieb 		num_groups = MLX5_FS_MAX_TYPES;
2349038d2ef8SMaor Gottlieb 		prio = &dev->flow_db.prios[priority];
2350038d2ef8SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
2351038d2ef8SMaor Gottlieb 		   flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
2352038d2ef8SMaor Gottlieb 		ns = mlx5_get_flow_namespace(dev->mdev,
2353038d2ef8SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_LEFTOVERS);
2354038d2ef8SMaor Gottlieb 		build_leftovers_ft_param(&priority,
2355038d2ef8SMaor Gottlieb 					 &num_entries,
2356038d2ef8SMaor Gottlieb 					 &num_groups);
2357038d2ef8SMaor Gottlieb 		prio = &dev->flow_db.prios[MLX5_IB_FLOW_LEFTOVERS_PRIO];
2358cc0e5d42SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
2359cc0e5d42SMaor Gottlieb 		if (!MLX5_CAP_FLOWTABLE(dev->mdev,
2360cc0e5d42SMaor Gottlieb 					allow_sniffer_and_nic_rx_shared_tir))
2361cc0e5d42SMaor Gottlieb 			return ERR_PTR(-ENOTSUPP);
2362cc0e5d42SMaor Gottlieb 
2363cc0e5d42SMaor Gottlieb 		ns = mlx5_get_flow_namespace(dev->mdev, ft_type == MLX5_IB_FT_RX ?
2364cc0e5d42SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_SNIFFER_RX :
2365cc0e5d42SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_SNIFFER_TX);
2366cc0e5d42SMaor Gottlieb 
2367cc0e5d42SMaor Gottlieb 		prio = &dev->flow_db.sniffer[ft_type];
2368cc0e5d42SMaor Gottlieb 		priority = 0;
2369cc0e5d42SMaor Gottlieb 		num_entries = 1;
2370cc0e5d42SMaor Gottlieb 		num_groups = 1;
2371038d2ef8SMaor Gottlieb 	}
2372038d2ef8SMaor Gottlieb 
2373038d2ef8SMaor Gottlieb 	if (!ns)
2374038d2ef8SMaor Gottlieb 		return ERR_PTR(-ENOTSUPP);
2375038d2ef8SMaor Gottlieb 
2376dac388efSMaor Gottlieb 	if (num_entries > max_table_size)
2377dac388efSMaor Gottlieb 		return ERR_PTR(-ENOMEM);
2378dac388efSMaor Gottlieb 
2379038d2ef8SMaor Gottlieb 	ft = prio->flow_table;
2380038d2ef8SMaor Gottlieb 	if (!ft) {
2381038d2ef8SMaor Gottlieb 		ft = mlx5_create_auto_grouped_flow_table(ns, priority,
2382038d2ef8SMaor Gottlieb 							 num_entries,
2383d63cd286SMaor Gottlieb 							 num_groups,
2384c9f1b073SHadar Hen Zion 							 0, 0);
2385038d2ef8SMaor Gottlieb 
2386038d2ef8SMaor Gottlieb 		if (!IS_ERR(ft)) {
2387038d2ef8SMaor Gottlieb 			prio->refcount = 0;
2388038d2ef8SMaor Gottlieb 			prio->flow_table = ft;
2389038d2ef8SMaor Gottlieb 		} else {
2390038d2ef8SMaor Gottlieb 			err = PTR_ERR(ft);
2391038d2ef8SMaor Gottlieb 		}
2392038d2ef8SMaor Gottlieb 	}
2393038d2ef8SMaor Gottlieb 
2394038d2ef8SMaor Gottlieb 	return err ? ERR_PTR(err) : prio;
2395038d2ef8SMaor Gottlieb }
2396038d2ef8SMaor Gottlieb 
2397a550ddfcSYishai Hadas static void set_underlay_qp(struct mlx5_ib_dev *dev,
2398a550ddfcSYishai Hadas 			    struct mlx5_flow_spec *spec,
2399a550ddfcSYishai Hadas 			    u32 underlay_qpn)
2400a550ddfcSYishai Hadas {
2401a550ddfcSYishai Hadas 	void *misc_params_c = MLX5_ADDR_OF(fte_match_param,
2402a550ddfcSYishai Hadas 					   spec->match_criteria,
2403a550ddfcSYishai Hadas 					   misc_parameters);
2404a550ddfcSYishai Hadas 	void *misc_params_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
2405a550ddfcSYishai Hadas 					   misc_parameters);
2406a550ddfcSYishai Hadas 
2407a550ddfcSYishai Hadas 	if (underlay_qpn &&
2408a550ddfcSYishai Hadas 	    MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
2409a550ddfcSYishai Hadas 				      ft_field_support.bth_dst_qp)) {
2410a550ddfcSYishai Hadas 		MLX5_SET(fte_match_set_misc,
2411a550ddfcSYishai Hadas 			 misc_params_v, bth_dst_qp, underlay_qpn);
2412a550ddfcSYishai Hadas 		MLX5_SET(fte_match_set_misc,
2413a550ddfcSYishai Hadas 			 misc_params_c, bth_dst_qp, 0xffffff);
2414a550ddfcSYishai Hadas 	}
2415a550ddfcSYishai Hadas }
2416a550ddfcSYishai Hadas 
2417a550ddfcSYishai Hadas static struct mlx5_ib_flow_handler *_create_flow_rule(struct mlx5_ib_dev *dev,
2418038d2ef8SMaor Gottlieb 						      struct mlx5_ib_flow_prio *ft_prio,
2419dd063d0eSMaor Gottlieb 						      const struct ib_flow_attr *flow_attr,
2420a550ddfcSYishai Hadas 						      struct mlx5_flow_destination *dst,
2421a550ddfcSYishai Hadas 						      u32 underlay_qpn)
2422038d2ef8SMaor Gottlieb {
2423038d2ef8SMaor Gottlieb 	struct mlx5_flow_table	*ft = ft_prio->flow_table;
2424038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler;
242566958ed9SHadar Hen Zion 	struct mlx5_flow_act flow_act = {0};
2426c5bb1730SMaor Gottlieb 	struct mlx5_flow_spec *spec;
2427a22ed86cSSlava Shwartsman 	struct mlx5_flow_destination *rule_dst = dst;
2428dd063d0eSMaor Gottlieb 	const void *ib_flow = (const void *)flow_attr + sizeof(*flow_attr);
2429038d2ef8SMaor Gottlieb 	unsigned int spec_index;
24302ac693f9SMoses Reuben 	u32 flow_tag = MLX5_FS_DEFAULT_FLOW_TAG;
2431a22ed86cSSlava Shwartsman 	bool is_drop = false;
2432038d2ef8SMaor Gottlieb 	int err = 0;
2433a22ed86cSSlava Shwartsman 	int dest_num = 1;
2434038d2ef8SMaor Gottlieb 
243519cc7524SAriel Levkovich 	if (!is_valid_attr(dev->mdev, flow_attr))
2436038d2ef8SMaor Gottlieb 		return ERR_PTR(-EINVAL);
2437038d2ef8SMaor Gottlieb 
24381b9a07eeSLeon Romanovsky 	spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
2439038d2ef8SMaor Gottlieb 	handler = kzalloc(sizeof(*handler), GFP_KERNEL);
2440c5bb1730SMaor Gottlieb 	if (!handler || !spec) {
2441038d2ef8SMaor Gottlieb 		err = -ENOMEM;
2442038d2ef8SMaor Gottlieb 		goto free;
2443038d2ef8SMaor Gottlieb 	}
2444038d2ef8SMaor Gottlieb 
2445038d2ef8SMaor Gottlieb 	INIT_LIST_HEAD(&handler->list);
2446038d2ef8SMaor Gottlieb 
2447038d2ef8SMaor Gottlieb 	for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
244819cc7524SAriel Levkovich 		err = parse_flow_attr(dev->mdev, spec->match_criteria,
2449a22ed86cSSlava Shwartsman 				      spec->match_value,
2450a22ed86cSSlava Shwartsman 				      ib_flow, &flow_tag, &is_drop);
2451038d2ef8SMaor Gottlieb 		if (err < 0)
2452038d2ef8SMaor Gottlieb 			goto free;
2453038d2ef8SMaor Gottlieb 
2454038d2ef8SMaor Gottlieb 		ib_flow += ((union ib_flow_spec *)ib_flow)->size;
2455038d2ef8SMaor Gottlieb 	}
2456038d2ef8SMaor Gottlieb 
2457a550ddfcSYishai Hadas 	if (!flow_is_multicast_only(flow_attr))
2458a550ddfcSYishai Hadas 		set_underlay_qp(dev, spec, underlay_qpn);
2459a550ddfcSYishai Hadas 
2460466fa6d2SMaor Gottlieb 	spec->match_criteria_enable = get_match_criteria_enable(spec->match_criteria);
2461a22ed86cSSlava Shwartsman 	if (is_drop) {
2462a22ed86cSSlava Shwartsman 		flow_act.action = MLX5_FLOW_CONTEXT_ACTION_DROP;
2463a22ed86cSSlava Shwartsman 		rule_dst = NULL;
2464a22ed86cSSlava Shwartsman 		dest_num = 0;
2465a22ed86cSSlava Shwartsman 	} else {
246666958ed9SHadar Hen Zion 		flow_act.action = dst ? MLX5_FLOW_CONTEXT_ACTION_FWD_DEST :
246735d19011SMaor Gottlieb 		    MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO;
2468a22ed86cSSlava Shwartsman 	}
24692ac693f9SMoses Reuben 
24702ac693f9SMoses Reuben 	if (flow_tag != MLX5_FS_DEFAULT_FLOW_TAG &&
24712ac693f9SMoses Reuben 	    (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
24722ac693f9SMoses Reuben 	     flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT)) {
24732ac693f9SMoses Reuben 		mlx5_ib_warn(dev, "Flow tag %u and attribute type %x isn't allowed in leftovers\n",
24742ac693f9SMoses Reuben 			     flow_tag, flow_attr->type);
24752ac693f9SMoses Reuben 		err = -EINVAL;
24762ac693f9SMoses Reuben 		goto free;
24772ac693f9SMoses Reuben 	}
24782ac693f9SMoses Reuben 	flow_act.flow_tag = flow_tag;
247974491de9SMark Bloch 	handler->rule = mlx5_add_flow_rules(ft, spec,
248066958ed9SHadar Hen Zion 					    &flow_act,
2481a22ed86cSSlava Shwartsman 					    rule_dst, dest_num);
2482038d2ef8SMaor Gottlieb 
2483038d2ef8SMaor Gottlieb 	if (IS_ERR(handler->rule)) {
2484038d2ef8SMaor Gottlieb 		err = PTR_ERR(handler->rule);
2485038d2ef8SMaor Gottlieb 		goto free;
2486038d2ef8SMaor Gottlieb 	}
2487038d2ef8SMaor Gottlieb 
2488d9d4980aSMaor Gottlieb 	ft_prio->refcount++;
24895497adc6SMaor Gottlieb 	handler->prio = ft_prio;
2490038d2ef8SMaor Gottlieb 
2491038d2ef8SMaor Gottlieb 	ft_prio->flow_table = ft;
2492038d2ef8SMaor Gottlieb free:
2493038d2ef8SMaor Gottlieb 	if (err)
2494038d2ef8SMaor Gottlieb 		kfree(handler);
2495c5bb1730SMaor Gottlieb 	kvfree(spec);
2496038d2ef8SMaor Gottlieb 	return err ? ERR_PTR(err) : handler;
2497038d2ef8SMaor Gottlieb }
2498038d2ef8SMaor Gottlieb 
2499a550ddfcSYishai Hadas static struct mlx5_ib_flow_handler *create_flow_rule(struct mlx5_ib_dev *dev,
2500a550ddfcSYishai Hadas 						     struct mlx5_ib_flow_prio *ft_prio,
2501a550ddfcSYishai Hadas 						     const struct ib_flow_attr *flow_attr,
2502a550ddfcSYishai Hadas 						     struct mlx5_flow_destination *dst)
2503a550ddfcSYishai Hadas {
2504a550ddfcSYishai Hadas 	return _create_flow_rule(dev, ft_prio, flow_attr, dst, 0);
2505a550ddfcSYishai Hadas }
2506a550ddfcSYishai Hadas 
250735d19011SMaor Gottlieb static struct mlx5_ib_flow_handler *create_dont_trap_rule(struct mlx5_ib_dev *dev,
250835d19011SMaor Gottlieb 							  struct mlx5_ib_flow_prio *ft_prio,
250935d19011SMaor Gottlieb 							  struct ib_flow_attr *flow_attr,
251035d19011SMaor Gottlieb 							  struct mlx5_flow_destination *dst)
251135d19011SMaor Gottlieb {
251235d19011SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_dst = NULL;
251335d19011SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
251435d19011SMaor Gottlieb 
251535d19011SMaor Gottlieb 	handler = create_flow_rule(dev, ft_prio, flow_attr, NULL);
251635d19011SMaor Gottlieb 	if (!IS_ERR(handler)) {
251735d19011SMaor Gottlieb 		handler_dst = create_flow_rule(dev, ft_prio,
251835d19011SMaor Gottlieb 					       flow_attr, dst);
251935d19011SMaor Gottlieb 		if (IS_ERR(handler_dst)) {
252074491de9SMark Bloch 			mlx5_del_flow_rules(handler->rule);
2521d9d4980aSMaor Gottlieb 			ft_prio->refcount--;
252235d19011SMaor Gottlieb 			kfree(handler);
252335d19011SMaor Gottlieb 			handler = handler_dst;
252435d19011SMaor Gottlieb 		} else {
252535d19011SMaor Gottlieb 			list_add(&handler_dst->list, &handler->list);
252635d19011SMaor Gottlieb 		}
252735d19011SMaor Gottlieb 	}
252835d19011SMaor Gottlieb 
252935d19011SMaor Gottlieb 	return handler;
253035d19011SMaor Gottlieb }
2531038d2ef8SMaor Gottlieb enum {
2532038d2ef8SMaor Gottlieb 	LEFTOVERS_MC,
2533038d2ef8SMaor Gottlieb 	LEFTOVERS_UC,
2534038d2ef8SMaor Gottlieb };
2535038d2ef8SMaor Gottlieb 
2536038d2ef8SMaor Gottlieb static struct mlx5_ib_flow_handler *create_leftovers_rule(struct mlx5_ib_dev *dev,
2537038d2ef8SMaor Gottlieb 							  struct mlx5_ib_flow_prio *ft_prio,
2538038d2ef8SMaor Gottlieb 							  struct ib_flow_attr *flow_attr,
2539038d2ef8SMaor Gottlieb 							  struct mlx5_flow_destination *dst)
2540038d2ef8SMaor Gottlieb {
2541038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_ucast = NULL;
2542038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
2543038d2ef8SMaor Gottlieb 
2544038d2ef8SMaor Gottlieb 	static struct {
2545038d2ef8SMaor Gottlieb 		struct ib_flow_attr	flow_attr;
2546038d2ef8SMaor Gottlieb 		struct ib_flow_spec_eth eth_flow;
2547038d2ef8SMaor Gottlieb 	} leftovers_specs[] = {
2548038d2ef8SMaor Gottlieb 		[LEFTOVERS_MC] = {
2549038d2ef8SMaor Gottlieb 			.flow_attr = {
2550038d2ef8SMaor Gottlieb 				.num_of_specs = 1,
2551038d2ef8SMaor Gottlieb 				.size = sizeof(leftovers_specs[0])
2552038d2ef8SMaor Gottlieb 			},
2553038d2ef8SMaor Gottlieb 			.eth_flow = {
2554038d2ef8SMaor Gottlieb 				.type = IB_FLOW_SPEC_ETH,
2555038d2ef8SMaor Gottlieb 				.size = sizeof(struct ib_flow_spec_eth),
2556038d2ef8SMaor Gottlieb 				.mask = {.dst_mac = {0x1} },
2557038d2ef8SMaor Gottlieb 				.val =  {.dst_mac = {0x1} }
2558038d2ef8SMaor Gottlieb 			}
2559038d2ef8SMaor Gottlieb 		},
2560038d2ef8SMaor Gottlieb 		[LEFTOVERS_UC] = {
2561038d2ef8SMaor Gottlieb 			.flow_attr = {
2562038d2ef8SMaor Gottlieb 				.num_of_specs = 1,
2563038d2ef8SMaor Gottlieb 				.size = sizeof(leftovers_specs[0])
2564038d2ef8SMaor Gottlieb 			},
2565038d2ef8SMaor Gottlieb 			.eth_flow = {
2566038d2ef8SMaor Gottlieb 				.type = IB_FLOW_SPEC_ETH,
2567038d2ef8SMaor Gottlieb 				.size = sizeof(struct ib_flow_spec_eth),
2568038d2ef8SMaor Gottlieb 				.mask = {.dst_mac = {0x1} },
2569038d2ef8SMaor Gottlieb 				.val = {.dst_mac = {} }
2570038d2ef8SMaor Gottlieb 			}
2571038d2ef8SMaor Gottlieb 		}
2572038d2ef8SMaor Gottlieb 	};
2573038d2ef8SMaor Gottlieb 
2574038d2ef8SMaor Gottlieb 	handler = create_flow_rule(dev, ft_prio,
2575038d2ef8SMaor Gottlieb 				   &leftovers_specs[LEFTOVERS_MC].flow_attr,
2576038d2ef8SMaor Gottlieb 				   dst);
2577038d2ef8SMaor Gottlieb 	if (!IS_ERR(handler) &&
2578038d2ef8SMaor Gottlieb 	    flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT) {
2579038d2ef8SMaor Gottlieb 		handler_ucast = create_flow_rule(dev, ft_prio,
2580038d2ef8SMaor Gottlieb 						 &leftovers_specs[LEFTOVERS_UC].flow_attr,
2581038d2ef8SMaor Gottlieb 						 dst);
2582038d2ef8SMaor Gottlieb 		if (IS_ERR(handler_ucast)) {
258374491de9SMark Bloch 			mlx5_del_flow_rules(handler->rule);
2584d9d4980aSMaor Gottlieb 			ft_prio->refcount--;
2585038d2ef8SMaor Gottlieb 			kfree(handler);
2586038d2ef8SMaor Gottlieb 			handler = handler_ucast;
2587038d2ef8SMaor Gottlieb 		} else {
2588038d2ef8SMaor Gottlieb 			list_add(&handler_ucast->list, &handler->list);
2589038d2ef8SMaor Gottlieb 		}
2590038d2ef8SMaor Gottlieb 	}
2591038d2ef8SMaor Gottlieb 
2592038d2ef8SMaor Gottlieb 	return handler;
2593038d2ef8SMaor Gottlieb }
2594038d2ef8SMaor Gottlieb 
2595cc0e5d42SMaor Gottlieb static struct mlx5_ib_flow_handler *create_sniffer_rule(struct mlx5_ib_dev *dev,
2596cc0e5d42SMaor Gottlieb 							struct mlx5_ib_flow_prio *ft_rx,
2597cc0e5d42SMaor Gottlieb 							struct mlx5_ib_flow_prio *ft_tx,
2598cc0e5d42SMaor Gottlieb 							struct mlx5_flow_destination *dst)
2599cc0e5d42SMaor Gottlieb {
2600cc0e5d42SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_rx;
2601cc0e5d42SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_tx;
2602cc0e5d42SMaor Gottlieb 	int err;
2603cc0e5d42SMaor Gottlieb 	static const struct ib_flow_attr flow_attr  = {
2604cc0e5d42SMaor Gottlieb 		.num_of_specs = 0,
2605cc0e5d42SMaor Gottlieb 		.size = sizeof(flow_attr)
2606cc0e5d42SMaor Gottlieb 	};
2607cc0e5d42SMaor Gottlieb 
2608cc0e5d42SMaor Gottlieb 	handler_rx = create_flow_rule(dev, ft_rx, &flow_attr, dst);
2609cc0e5d42SMaor Gottlieb 	if (IS_ERR(handler_rx)) {
2610cc0e5d42SMaor Gottlieb 		err = PTR_ERR(handler_rx);
2611cc0e5d42SMaor Gottlieb 		goto err;
2612cc0e5d42SMaor Gottlieb 	}
2613cc0e5d42SMaor Gottlieb 
2614cc0e5d42SMaor Gottlieb 	handler_tx = create_flow_rule(dev, ft_tx, &flow_attr, dst);
2615cc0e5d42SMaor Gottlieb 	if (IS_ERR(handler_tx)) {
2616cc0e5d42SMaor Gottlieb 		err = PTR_ERR(handler_tx);
2617cc0e5d42SMaor Gottlieb 		goto err_tx;
2618cc0e5d42SMaor Gottlieb 	}
2619cc0e5d42SMaor Gottlieb 
2620cc0e5d42SMaor Gottlieb 	list_add(&handler_tx->list, &handler_rx->list);
2621cc0e5d42SMaor Gottlieb 
2622cc0e5d42SMaor Gottlieb 	return handler_rx;
2623cc0e5d42SMaor Gottlieb 
2624cc0e5d42SMaor Gottlieb err_tx:
262574491de9SMark Bloch 	mlx5_del_flow_rules(handler_rx->rule);
2626cc0e5d42SMaor Gottlieb 	ft_rx->refcount--;
2627cc0e5d42SMaor Gottlieb 	kfree(handler_rx);
2628cc0e5d42SMaor Gottlieb err:
2629cc0e5d42SMaor Gottlieb 	return ERR_PTR(err);
2630cc0e5d42SMaor Gottlieb }
2631cc0e5d42SMaor Gottlieb 
2632038d2ef8SMaor Gottlieb static struct ib_flow *mlx5_ib_create_flow(struct ib_qp *qp,
2633038d2ef8SMaor Gottlieb 					   struct ib_flow_attr *flow_attr,
2634038d2ef8SMaor Gottlieb 					   int domain)
2635038d2ef8SMaor Gottlieb {
2636038d2ef8SMaor Gottlieb 	struct mlx5_ib_dev *dev = to_mdev(qp->device);
2637d9f88e5aSYishai Hadas 	struct mlx5_ib_qp *mqp = to_mqp(qp);
2638038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
2639038d2ef8SMaor Gottlieb 	struct mlx5_flow_destination *dst = NULL;
2640cc0e5d42SMaor Gottlieb 	struct mlx5_ib_flow_prio *ft_prio_tx = NULL;
2641038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_prio *ft_prio;
2642038d2ef8SMaor Gottlieb 	int err;
2643a550ddfcSYishai Hadas 	int underlay_qpn;
2644038d2ef8SMaor Gottlieb 
2645038d2ef8SMaor Gottlieb 	if (flow_attr->priority > MLX5_IB_FLOW_LAST_PRIO)
2646dac388efSMaor Gottlieb 		return ERR_PTR(-ENOMEM);
2647038d2ef8SMaor Gottlieb 
2648038d2ef8SMaor Gottlieb 	if (domain != IB_FLOW_DOMAIN_USER ||
2649038d2ef8SMaor Gottlieb 	    flow_attr->port > MLX5_CAP_GEN(dev->mdev, num_ports) ||
265035d19011SMaor Gottlieb 	    (flow_attr->flags & ~IB_FLOW_ATTR_FLAGS_DONT_TRAP))
2651038d2ef8SMaor Gottlieb 		return ERR_PTR(-EINVAL);
2652038d2ef8SMaor Gottlieb 
2653038d2ef8SMaor Gottlieb 	dst = kzalloc(sizeof(*dst), GFP_KERNEL);
2654038d2ef8SMaor Gottlieb 	if (!dst)
2655038d2ef8SMaor Gottlieb 		return ERR_PTR(-ENOMEM);
2656038d2ef8SMaor Gottlieb 
2657038d2ef8SMaor Gottlieb 	mutex_lock(&dev->flow_db.lock);
2658038d2ef8SMaor Gottlieb 
2659cc0e5d42SMaor Gottlieb 	ft_prio = get_flow_table(dev, flow_attr, MLX5_IB_FT_RX);
2660038d2ef8SMaor Gottlieb 	if (IS_ERR(ft_prio)) {
2661038d2ef8SMaor Gottlieb 		err = PTR_ERR(ft_prio);
2662038d2ef8SMaor Gottlieb 		goto unlock;
2663038d2ef8SMaor Gottlieb 	}
2664cc0e5d42SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
2665cc0e5d42SMaor Gottlieb 		ft_prio_tx = get_flow_table(dev, flow_attr, MLX5_IB_FT_TX);
2666cc0e5d42SMaor Gottlieb 		if (IS_ERR(ft_prio_tx)) {
2667cc0e5d42SMaor Gottlieb 			err = PTR_ERR(ft_prio_tx);
2668cc0e5d42SMaor Gottlieb 			ft_prio_tx = NULL;
2669cc0e5d42SMaor Gottlieb 			goto destroy_ft;
2670cc0e5d42SMaor Gottlieb 		}
2671cc0e5d42SMaor Gottlieb 	}
2672038d2ef8SMaor Gottlieb 
2673038d2ef8SMaor Gottlieb 	dst->type = MLX5_FLOW_DESTINATION_TYPE_TIR;
2674d9f88e5aSYishai Hadas 	if (mqp->flags & MLX5_IB_QP_RSS)
2675d9f88e5aSYishai Hadas 		dst->tir_num = mqp->rss_qp.tirn;
2676d9f88e5aSYishai Hadas 	else
2677d9f88e5aSYishai Hadas 		dst->tir_num = mqp->raw_packet_qp.rq.tirn;
2678038d2ef8SMaor Gottlieb 
2679038d2ef8SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
268035d19011SMaor Gottlieb 		if (flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP)  {
268135d19011SMaor Gottlieb 			handler = create_dont_trap_rule(dev, ft_prio,
268235d19011SMaor Gottlieb 							flow_attr, dst);
268335d19011SMaor Gottlieb 		} else {
2684a550ddfcSYishai Hadas 			underlay_qpn = (mqp->flags & MLX5_IB_QP_UNDERLAY) ?
2685a550ddfcSYishai Hadas 					mqp->underlay_qpn : 0;
2686a550ddfcSYishai Hadas 			handler = _create_flow_rule(dev, ft_prio, flow_attr,
2687a550ddfcSYishai Hadas 						    dst, underlay_qpn);
268835d19011SMaor Gottlieb 		}
2689038d2ef8SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
2690038d2ef8SMaor Gottlieb 		   flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
2691038d2ef8SMaor Gottlieb 		handler = create_leftovers_rule(dev, ft_prio, flow_attr,
2692038d2ef8SMaor Gottlieb 						dst);
2693cc0e5d42SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
2694cc0e5d42SMaor Gottlieb 		handler = create_sniffer_rule(dev, ft_prio, ft_prio_tx, dst);
2695038d2ef8SMaor Gottlieb 	} else {
2696038d2ef8SMaor Gottlieb 		err = -EINVAL;
2697038d2ef8SMaor Gottlieb 		goto destroy_ft;
2698038d2ef8SMaor Gottlieb 	}
2699038d2ef8SMaor Gottlieb 
2700038d2ef8SMaor Gottlieb 	if (IS_ERR(handler)) {
2701038d2ef8SMaor Gottlieb 		err = PTR_ERR(handler);
2702038d2ef8SMaor Gottlieb 		handler = NULL;
2703038d2ef8SMaor Gottlieb 		goto destroy_ft;
2704038d2ef8SMaor Gottlieb 	}
2705038d2ef8SMaor Gottlieb 
2706038d2ef8SMaor Gottlieb 	mutex_unlock(&dev->flow_db.lock);
2707038d2ef8SMaor Gottlieb 	kfree(dst);
2708038d2ef8SMaor Gottlieb 
2709038d2ef8SMaor Gottlieb 	return &handler->ibflow;
2710038d2ef8SMaor Gottlieb 
2711038d2ef8SMaor Gottlieb destroy_ft:
2712038d2ef8SMaor Gottlieb 	put_flow_table(dev, ft_prio, false);
2713cc0e5d42SMaor Gottlieb 	if (ft_prio_tx)
2714cc0e5d42SMaor Gottlieb 		put_flow_table(dev, ft_prio_tx, false);
2715038d2ef8SMaor Gottlieb unlock:
2716038d2ef8SMaor Gottlieb 	mutex_unlock(&dev->flow_db.lock);
2717038d2ef8SMaor Gottlieb 	kfree(dst);
2718038d2ef8SMaor Gottlieb 	kfree(handler);
2719038d2ef8SMaor Gottlieb 	return ERR_PTR(err);
2720038d2ef8SMaor Gottlieb }
2721038d2ef8SMaor Gottlieb 
2722e126ba97SEli Cohen static int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
2723e126ba97SEli Cohen {
2724e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
272581e30880SYishai Hadas 	struct mlx5_ib_qp *mqp = to_mqp(ibqp);
2726e126ba97SEli Cohen 	int err;
2727e126ba97SEli Cohen 
272881e30880SYishai Hadas 	if (mqp->flags & MLX5_IB_QP_UNDERLAY) {
272981e30880SYishai Hadas 		mlx5_ib_dbg(dev, "Attaching a multi cast group to underlay QP is not supported\n");
273081e30880SYishai Hadas 		return -EOPNOTSUPP;
273181e30880SYishai Hadas 	}
273281e30880SYishai Hadas 
27339603b61dSJack Morgenstein 	err = mlx5_core_attach_mcg(dev->mdev, gid, ibqp->qp_num);
2734e126ba97SEli Cohen 	if (err)
2735e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n",
2736e126ba97SEli Cohen 			     ibqp->qp_num, gid->raw);
2737e126ba97SEli Cohen 
2738e126ba97SEli Cohen 	return err;
2739e126ba97SEli Cohen }
2740e126ba97SEli Cohen 
2741e126ba97SEli Cohen static int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
2742e126ba97SEli Cohen {
2743e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
2744e126ba97SEli Cohen 	int err;
2745e126ba97SEli Cohen 
27469603b61dSJack Morgenstein 	err = mlx5_core_detach_mcg(dev->mdev, gid, ibqp->qp_num);
2747e126ba97SEli Cohen 	if (err)
2748e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n",
2749e126ba97SEli Cohen 			     ibqp->qp_num, gid->raw);
2750e126ba97SEli Cohen 
2751e126ba97SEli Cohen 	return err;
2752e126ba97SEli Cohen }
2753e126ba97SEli Cohen 
2754e126ba97SEli Cohen static int init_node_data(struct mlx5_ib_dev *dev)
2755e126ba97SEli Cohen {
27561b5daf11SMajd Dibbiny 	int err;
2757e126ba97SEli Cohen 
27581b5daf11SMajd Dibbiny 	err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc);
2759e126ba97SEli Cohen 	if (err)
2760e126ba97SEli Cohen 		return err;
27611b5daf11SMajd Dibbiny 
27621b5daf11SMajd Dibbiny 	dev->mdev->rev_id = dev->mdev->pdev->revision;
27631b5daf11SMajd Dibbiny 
27641b5daf11SMajd Dibbiny 	return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid);
2765e126ba97SEli Cohen }
2766e126ba97SEli Cohen 
2767e126ba97SEli Cohen static ssize_t show_fw_pages(struct device *device, struct device_attribute *attr,
2768e126ba97SEli Cohen 			     char *buf)
2769e126ba97SEli Cohen {
2770e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
2771e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
2772e126ba97SEli Cohen 
27739603b61dSJack Morgenstein 	return sprintf(buf, "%d\n", dev->mdev->priv.fw_pages);
2774e126ba97SEli Cohen }
2775e126ba97SEli Cohen 
2776e126ba97SEli Cohen static ssize_t show_reg_pages(struct device *device,
2777e126ba97SEli Cohen 			      struct device_attribute *attr, char *buf)
2778e126ba97SEli Cohen {
2779e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
2780e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
2781e126ba97SEli Cohen 
27826aec21f6SHaggai Eran 	return sprintf(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages));
2783e126ba97SEli Cohen }
2784e126ba97SEli Cohen 
2785e126ba97SEli Cohen static ssize_t show_hca(struct device *device, struct device_attribute *attr,
2786e126ba97SEli Cohen 			char *buf)
2787e126ba97SEli Cohen {
2788e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
2789e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
27909603b61dSJack Morgenstein 	return sprintf(buf, "MT%d\n", dev->mdev->pdev->device);
2791e126ba97SEli Cohen }
2792e126ba97SEli Cohen 
2793e126ba97SEli Cohen static ssize_t show_rev(struct device *device, struct device_attribute *attr,
2794e126ba97SEli Cohen 			char *buf)
2795e126ba97SEli Cohen {
2796e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
2797e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
27989603b61dSJack Morgenstein 	return sprintf(buf, "%x\n", dev->mdev->rev_id);
2799e126ba97SEli Cohen }
2800e126ba97SEli Cohen 
2801e126ba97SEli Cohen static ssize_t show_board(struct device *device, struct device_attribute *attr,
2802e126ba97SEli Cohen 			  char *buf)
2803e126ba97SEli Cohen {
2804e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
2805e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
2806e126ba97SEli Cohen 	return sprintf(buf, "%.*s\n", MLX5_BOARD_ID_LEN,
28079603b61dSJack Morgenstein 		       dev->mdev->board_id);
2808e126ba97SEli Cohen }
2809e126ba97SEli Cohen 
2810e126ba97SEli Cohen static DEVICE_ATTR(hw_rev,   S_IRUGO, show_rev,    NULL);
2811e126ba97SEli Cohen static DEVICE_ATTR(hca_type, S_IRUGO, show_hca,    NULL);
2812e126ba97SEli Cohen static DEVICE_ATTR(board_id, S_IRUGO, show_board,  NULL);
2813e126ba97SEli Cohen static DEVICE_ATTR(fw_pages, S_IRUGO, show_fw_pages, NULL);
2814e126ba97SEli Cohen static DEVICE_ATTR(reg_pages, S_IRUGO, show_reg_pages, NULL);
2815e126ba97SEli Cohen 
2816e126ba97SEli Cohen static struct device_attribute *mlx5_class_attributes[] = {
2817e126ba97SEli Cohen 	&dev_attr_hw_rev,
2818e126ba97SEli Cohen 	&dev_attr_hca_type,
2819e126ba97SEli Cohen 	&dev_attr_board_id,
2820e126ba97SEli Cohen 	&dev_attr_fw_pages,
2821e126ba97SEli Cohen 	&dev_attr_reg_pages,
2822e126ba97SEli Cohen };
2823e126ba97SEli Cohen 
28247722f47eSHaggai Eran static void pkey_change_handler(struct work_struct *work)
28257722f47eSHaggai Eran {
28267722f47eSHaggai Eran 	struct mlx5_ib_port_resources *ports =
28277722f47eSHaggai Eran 		container_of(work, struct mlx5_ib_port_resources,
28287722f47eSHaggai Eran 			     pkey_change_work);
28297722f47eSHaggai Eran 
28307722f47eSHaggai Eran 	mutex_lock(&ports->devr->mutex);
28317722f47eSHaggai Eran 	mlx5_ib_gsi_pkey_change(ports->gsi);
28327722f47eSHaggai Eran 	mutex_unlock(&ports->devr->mutex);
28337722f47eSHaggai Eran }
28347722f47eSHaggai Eran 
283589ea94a7SMaor Gottlieb static void mlx5_ib_handle_internal_error(struct mlx5_ib_dev *ibdev)
283689ea94a7SMaor Gottlieb {
283789ea94a7SMaor Gottlieb 	struct mlx5_ib_qp *mqp;
283889ea94a7SMaor Gottlieb 	struct mlx5_ib_cq *send_mcq, *recv_mcq;
283989ea94a7SMaor Gottlieb 	struct mlx5_core_cq *mcq;
284089ea94a7SMaor Gottlieb 	struct list_head cq_armed_list;
284189ea94a7SMaor Gottlieb 	unsigned long flags_qp;
284289ea94a7SMaor Gottlieb 	unsigned long flags_cq;
284389ea94a7SMaor Gottlieb 	unsigned long flags;
284489ea94a7SMaor Gottlieb 
284589ea94a7SMaor Gottlieb 	INIT_LIST_HEAD(&cq_armed_list);
284689ea94a7SMaor Gottlieb 
284789ea94a7SMaor Gottlieb 	/* Go over qp list reside on that ibdev, sync with create/destroy qp.*/
284889ea94a7SMaor Gottlieb 	spin_lock_irqsave(&ibdev->reset_flow_resource_lock, flags);
284989ea94a7SMaor Gottlieb 	list_for_each_entry(mqp, &ibdev->qp_list, qps_list) {
285089ea94a7SMaor Gottlieb 		spin_lock_irqsave(&mqp->sq.lock, flags_qp);
285189ea94a7SMaor Gottlieb 		if (mqp->sq.tail != mqp->sq.head) {
285289ea94a7SMaor Gottlieb 			send_mcq = to_mcq(mqp->ibqp.send_cq);
285389ea94a7SMaor Gottlieb 			spin_lock_irqsave(&send_mcq->lock, flags_cq);
285489ea94a7SMaor Gottlieb 			if (send_mcq->mcq.comp &&
285589ea94a7SMaor Gottlieb 			    mqp->ibqp.send_cq->comp_handler) {
285689ea94a7SMaor Gottlieb 				if (!send_mcq->mcq.reset_notify_added) {
285789ea94a7SMaor Gottlieb 					send_mcq->mcq.reset_notify_added = 1;
285889ea94a7SMaor Gottlieb 					list_add_tail(&send_mcq->mcq.reset_notify,
285989ea94a7SMaor Gottlieb 						      &cq_armed_list);
286089ea94a7SMaor Gottlieb 				}
286189ea94a7SMaor Gottlieb 			}
286289ea94a7SMaor Gottlieb 			spin_unlock_irqrestore(&send_mcq->lock, flags_cq);
286389ea94a7SMaor Gottlieb 		}
286489ea94a7SMaor Gottlieb 		spin_unlock_irqrestore(&mqp->sq.lock, flags_qp);
286589ea94a7SMaor Gottlieb 		spin_lock_irqsave(&mqp->rq.lock, flags_qp);
286689ea94a7SMaor Gottlieb 		/* no handling is needed for SRQ */
286789ea94a7SMaor Gottlieb 		if (!mqp->ibqp.srq) {
286889ea94a7SMaor Gottlieb 			if (mqp->rq.tail != mqp->rq.head) {
286989ea94a7SMaor Gottlieb 				recv_mcq = to_mcq(mqp->ibqp.recv_cq);
287089ea94a7SMaor Gottlieb 				spin_lock_irqsave(&recv_mcq->lock, flags_cq);
287189ea94a7SMaor Gottlieb 				if (recv_mcq->mcq.comp &&
287289ea94a7SMaor Gottlieb 				    mqp->ibqp.recv_cq->comp_handler) {
287389ea94a7SMaor Gottlieb 					if (!recv_mcq->mcq.reset_notify_added) {
287489ea94a7SMaor Gottlieb 						recv_mcq->mcq.reset_notify_added = 1;
287589ea94a7SMaor Gottlieb 						list_add_tail(&recv_mcq->mcq.reset_notify,
287689ea94a7SMaor Gottlieb 							      &cq_armed_list);
287789ea94a7SMaor Gottlieb 					}
287889ea94a7SMaor Gottlieb 				}
287989ea94a7SMaor Gottlieb 				spin_unlock_irqrestore(&recv_mcq->lock,
288089ea94a7SMaor Gottlieb 						       flags_cq);
288189ea94a7SMaor Gottlieb 			}
288289ea94a7SMaor Gottlieb 		}
288389ea94a7SMaor Gottlieb 		spin_unlock_irqrestore(&mqp->rq.lock, flags_qp);
288489ea94a7SMaor Gottlieb 	}
288589ea94a7SMaor Gottlieb 	/*At that point all inflight post send were put to be executed as of we
288689ea94a7SMaor Gottlieb 	 * lock/unlock above locks Now need to arm all involved CQs.
288789ea94a7SMaor Gottlieb 	 */
288889ea94a7SMaor Gottlieb 	list_for_each_entry(mcq, &cq_armed_list, reset_notify) {
288989ea94a7SMaor Gottlieb 		mcq->comp(mcq);
289089ea94a7SMaor Gottlieb 	}
289189ea94a7SMaor Gottlieb 	spin_unlock_irqrestore(&ibdev->reset_flow_resource_lock, flags);
289289ea94a7SMaor Gottlieb }
289389ea94a7SMaor Gottlieb 
289403404e8aSMaor Gottlieb static void delay_drop_handler(struct work_struct *work)
289503404e8aSMaor Gottlieb {
289603404e8aSMaor Gottlieb 	int err;
289703404e8aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop =
289803404e8aSMaor Gottlieb 		container_of(work, struct mlx5_ib_delay_drop,
289903404e8aSMaor Gottlieb 			     delay_drop_work);
290003404e8aSMaor Gottlieb 
2901fe248c3aSMaor Gottlieb 	atomic_inc(&delay_drop->events_cnt);
2902fe248c3aSMaor Gottlieb 
290303404e8aSMaor Gottlieb 	mutex_lock(&delay_drop->lock);
290403404e8aSMaor Gottlieb 	err = mlx5_core_set_delay_drop(delay_drop->dev->mdev,
290503404e8aSMaor Gottlieb 				       delay_drop->timeout);
290603404e8aSMaor Gottlieb 	if (err) {
290703404e8aSMaor Gottlieb 		mlx5_ib_warn(delay_drop->dev, "Failed to set delay drop, timeout=%u\n",
290803404e8aSMaor Gottlieb 			     delay_drop->timeout);
290903404e8aSMaor Gottlieb 		delay_drop->activate = false;
291003404e8aSMaor Gottlieb 	}
291103404e8aSMaor Gottlieb 	mutex_unlock(&delay_drop->lock);
291203404e8aSMaor Gottlieb }
291303404e8aSMaor Gottlieb 
29149603b61dSJack Morgenstein static void mlx5_ib_event(struct mlx5_core_dev *dev, void *context,
29154d2f9bbbSJack Morgenstein 			  enum mlx5_dev_event event, unsigned long param)
2916e126ba97SEli Cohen {
29179603b61dSJack Morgenstein 	struct mlx5_ib_dev *ibdev = (struct mlx5_ib_dev *)context;
2918e126ba97SEli Cohen 	struct ib_event ibev;
2919dbaaff2aSEli Cohen 	bool fatal = false;
2920e126ba97SEli Cohen 	u8 port = 0;
2921e126ba97SEli Cohen 
2922e126ba97SEli Cohen 	switch (event) {
2923e126ba97SEli Cohen 	case MLX5_DEV_EVENT_SYS_ERROR:
2924e126ba97SEli Cohen 		ibev.event = IB_EVENT_DEVICE_FATAL;
292589ea94a7SMaor Gottlieb 		mlx5_ib_handle_internal_error(ibdev);
2926dbaaff2aSEli Cohen 		fatal = true;
2927e126ba97SEli Cohen 		break;
2928e126ba97SEli Cohen 
2929e126ba97SEli Cohen 	case MLX5_DEV_EVENT_PORT_UP:
2930e126ba97SEli Cohen 	case MLX5_DEV_EVENT_PORT_DOWN:
29312788cf3bSNoa Osherovich 	case MLX5_DEV_EVENT_PORT_INITIALIZED:
29324d2f9bbbSJack Morgenstein 		port = (u8)param;
29335ec8c83eSAviv Heller 
29345ec8c83eSAviv Heller 		/* In RoCE, port up/down events are handled in
29355ec8c83eSAviv Heller 		 * mlx5_netdev_event().
29365ec8c83eSAviv Heller 		 */
29375ec8c83eSAviv Heller 		if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) ==
29385ec8c83eSAviv Heller 			IB_LINK_LAYER_ETHERNET)
29395ec8c83eSAviv Heller 			return;
29405ec8c83eSAviv Heller 
29415ec8c83eSAviv Heller 		ibev.event = (event == MLX5_DEV_EVENT_PORT_UP) ?
29425ec8c83eSAviv Heller 			     IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
2943e126ba97SEli Cohen 		break;
2944e126ba97SEli Cohen 
2945e126ba97SEli Cohen 	case MLX5_DEV_EVENT_LID_CHANGE:
2946e126ba97SEli Cohen 		ibev.event = IB_EVENT_LID_CHANGE;
29474d2f9bbbSJack Morgenstein 		port = (u8)param;
2948e126ba97SEli Cohen 		break;
2949e126ba97SEli Cohen 
2950e126ba97SEli Cohen 	case MLX5_DEV_EVENT_PKEY_CHANGE:
2951e126ba97SEli Cohen 		ibev.event = IB_EVENT_PKEY_CHANGE;
29524d2f9bbbSJack Morgenstein 		port = (u8)param;
29537722f47eSHaggai Eran 
29547722f47eSHaggai Eran 		schedule_work(&ibdev->devr.ports[port - 1].pkey_change_work);
2955e126ba97SEli Cohen 		break;
2956e126ba97SEli Cohen 
2957e126ba97SEli Cohen 	case MLX5_DEV_EVENT_GUID_CHANGE:
2958e126ba97SEli Cohen 		ibev.event = IB_EVENT_GID_CHANGE;
29594d2f9bbbSJack Morgenstein 		port = (u8)param;
2960e126ba97SEli Cohen 		break;
2961e126ba97SEli Cohen 
2962e126ba97SEli Cohen 	case MLX5_DEV_EVENT_CLIENT_REREG:
2963e126ba97SEli Cohen 		ibev.event = IB_EVENT_CLIENT_REREGISTER;
29644d2f9bbbSJack Morgenstein 		port = (u8)param;
2965e126ba97SEli Cohen 		break;
296603404e8aSMaor Gottlieb 	case MLX5_DEV_EVENT_DELAY_DROP_TIMEOUT:
296703404e8aSMaor Gottlieb 		schedule_work(&ibdev->delay_drop.delay_drop_work);
296803404e8aSMaor Gottlieb 		goto out;
2969bdc37924SSaeed Mahameed 	default:
297003404e8aSMaor Gottlieb 		goto out;
2971e126ba97SEli Cohen 	}
2972e126ba97SEli Cohen 
2973e126ba97SEli Cohen 	ibev.device	      = &ibdev->ib_dev;
2974e126ba97SEli Cohen 	ibev.element.port_num = port;
2975e126ba97SEli Cohen 
2976a0c84c32SEli Cohen 	if (port < 1 || port > ibdev->num_ports) {
2977a0c84c32SEli Cohen 		mlx5_ib_warn(ibdev, "warning: event on port %d\n", port);
297803404e8aSMaor Gottlieb 		goto out;
2979a0c84c32SEli Cohen 	}
2980a0c84c32SEli Cohen 
2981e126ba97SEli Cohen 	if (ibdev->ib_active)
2982e126ba97SEli Cohen 		ib_dispatch_event(&ibev);
2983dbaaff2aSEli Cohen 
2984dbaaff2aSEli Cohen 	if (fatal)
2985dbaaff2aSEli Cohen 		ibdev->ib_active = false;
298603404e8aSMaor Gottlieb 
298703404e8aSMaor Gottlieb out:
298803404e8aSMaor Gottlieb 	return;
2989e126ba97SEli Cohen }
2990e126ba97SEli Cohen 
2991c43f1112SMaor Gottlieb static int set_has_smi_cap(struct mlx5_ib_dev *dev)
2992c43f1112SMaor Gottlieb {
2993c43f1112SMaor Gottlieb 	struct mlx5_hca_vport_context vport_ctx;
2994c43f1112SMaor Gottlieb 	int err;
2995c43f1112SMaor Gottlieb 	int port;
2996c43f1112SMaor Gottlieb 
2997c43f1112SMaor Gottlieb 	for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++) {
2998c43f1112SMaor Gottlieb 		dev->mdev->port_caps[port - 1].has_smi = false;
2999c43f1112SMaor Gottlieb 		if (MLX5_CAP_GEN(dev->mdev, port_type) ==
3000c43f1112SMaor Gottlieb 		    MLX5_CAP_PORT_TYPE_IB) {
3001c43f1112SMaor Gottlieb 			if (MLX5_CAP_GEN(dev->mdev, ib_virt)) {
3002c43f1112SMaor Gottlieb 				err = mlx5_query_hca_vport_context(dev->mdev, 0,
3003c43f1112SMaor Gottlieb 								   port, 0,
3004c43f1112SMaor Gottlieb 								   &vport_ctx);
3005c43f1112SMaor Gottlieb 				if (err) {
3006c43f1112SMaor Gottlieb 					mlx5_ib_err(dev, "query_hca_vport_context for port=%d failed %d\n",
3007c43f1112SMaor Gottlieb 						    port, err);
3008c43f1112SMaor Gottlieb 					return err;
3009c43f1112SMaor Gottlieb 				}
3010c43f1112SMaor Gottlieb 				dev->mdev->port_caps[port - 1].has_smi =
3011c43f1112SMaor Gottlieb 					vport_ctx.has_smi;
3012c43f1112SMaor Gottlieb 			} else {
3013c43f1112SMaor Gottlieb 				dev->mdev->port_caps[port - 1].has_smi = true;
3014c43f1112SMaor Gottlieb 			}
3015c43f1112SMaor Gottlieb 		}
3016c43f1112SMaor Gottlieb 	}
3017c43f1112SMaor Gottlieb 	return 0;
3018c43f1112SMaor Gottlieb }
3019c43f1112SMaor Gottlieb 
3020e126ba97SEli Cohen static void get_ext_port_caps(struct mlx5_ib_dev *dev)
3021e126ba97SEli Cohen {
3022e126ba97SEli Cohen 	int port;
3023e126ba97SEli Cohen 
3024938fe83cSSaeed Mahameed 	for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++)
3025e126ba97SEli Cohen 		mlx5_query_ext_port_caps(dev, port);
3026e126ba97SEli Cohen }
3027e126ba97SEli Cohen 
3028e126ba97SEli Cohen static int get_port_caps(struct mlx5_ib_dev *dev)
3029e126ba97SEli Cohen {
3030e126ba97SEli Cohen 	struct ib_device_attr *dprops = NULL;
3031e126ba97SEli Cohen 	struct ib_port_attr *pprops = NULL;
3032f614fc15SDan Carpenter 	int err = -ENOMEM;
3033e126ba97SEli Cohen 	int port;
30342528e33eSMatan Barak 	struct ib_udata uhw = {.inlen = 0, .outlen = 0};
3035e126ba97SEli Cohen 
3036e126ba97SEli Cohen 	pprops = kmalloc(sizeof(*pprops), GFP_KERNEL);
3037e126ba97SEli Cohen 	if (!pprops)
3038e126ba97SEli Cohen 		goto out;
3039e126ba97SEli Cohen 
3040e126ba97SEli Cohen 	dprops = kmalloc(sizeof(*dprops), GFP_KERNEL);
3041e126ba97SEli Cohen 	if (!dprops)
3042e126ba97SEli Cohen 		goto out;
3043e126ba97SEli Cohen 
3044c43f1112SMaor Gottlieb 	err = set_has_smi_cap(dev);
3045c43f1112SMaor Gottlieb 	if (err)
3046c43f1112SMaor Gottlieb 		goto out;
3047c43f1112SMaor Gottlieb 
30482528e33eSMatan Barak 	err = mlx5_ib_query_device(&dev->ib_dev, dprops, &uhw);
3049e126ba97SEli Cohen 	if (err) {
3050e126ba97SEli Cohen 		mlx5_ib_warn(dev, "query_device failed %d\n", err);
3051e126ba97SEli Cohen 		goto out;
3052e126ba97SEli Cohen 	}
3053e126ba97SEli Cohen 
3054938fe83cSSaeed Mahameed 	for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++) {
3055c4550c63SOr Gerlitz 		memset(pprops, 0, sizeof(*pprops));
3056e126ba97SEli Cohen 		err = mlx5_ib_query_port(&dev->ib_dev, port, pprops);
3057e126ba97SEli Cohen 		if (err) {
3058938fe83cSSaeed Mahameed 			mlx5_ib_warn(dev, "query_port %d failed %d\n",
3059938fe83cSSaeed Mahameed 				     port, err);
3060e126ba97SEli Cohen 			break;
3061e126ba97SEli Cohen 		}
3062938fe83cSSaeed Mahameed 		dev->mdev->port_caps[port - 1].pkey_table_len =
3063938fe83cSSaeed Mahameed 						dprops->max_pkeys;
3064938fe83cSSaeed Mahameed 		dev->mdev->port_caps[port - 1].gid_table_len =
3065938fe83cSSaeed Mahameed 						pprops->gid_tbl_len;
3066e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "pkey_table_len %d, gid_table_len %d\n",
3067e126ba97SEli Cohen 			    dprops->max_pkeys, pprops->gid_tbl_len);
3068e126ba97SEli Cohen 	}
3069e126ba97SEli Cohen 
3070e126ba97SEli Cohen out:
3071e126ba97SEli Cohen 	kfree(pprops);
3072e126ba97SEli Cohen 	kfree(dprops);
3073e126ba97SEli Cohen 
3074e126ba97SEli Cohen 	return err;
3075e126ba97SEli Cohen }
3076e126ba97SEli Cohen 
3077e126ba97SEli Cohen static void destroy_umrc_res(struct mlx5_ib_dev *dev)
3078e126ba97SEli Cohen {
3079e126ba97SEli Cohen 	int err;
3080e126ba97SEli Cohen 
3081e126ba97SEli Cohen 	err = mlx5_mr_cache_cleanup(dev);
3082e126ba97SEli Cohen 	if (err)
3083e126ba97SEli Cohen 		mlx5_ib_warn(dev, "mr cache cleanup failed\n");
3084e126ba97SEli Cohen 
3085e126ba97SEli Cohen 	mlx5_ib_destroy_qp(dev->umrc.qp);
3086add08d76SChristoph Hellwig 	ib_free_cq(dev->umrc.cq);
3087e126ba97SEli Cohen 	ib_dealloc_pd(dev->umrc.pd);
3088e126ba97SEli Cohen }
3089e126ba97SEli Cohen 
3090e126ba97SEli Cohen enum {
3091e126ba97SEli Cohen 	MAX_UMR_WR = 128,
3092e126ba97SEli Cohen };
3093e126ba97SEli Cohen 
3094e126ba97SEli Cohen static int create_umr_res(struct mlx5_ib_dev *dev)
3095e126ba97SEli Cohen {
3096e126ba97SEli Cohen 	struct ib_qp_init_attr *init_attr = NULL;
3097e126ba97SEli Cohen 	struct ib_qp_attr *attr = NULL;
3098e126ba97SEli Cohen 	struct ib_pd *pd;
3099e126ba97SEli Cohen 	struct ib_cq *cq;
3100e126ba97SEli Cohen 	struct ib_qp *qp;
3101e126ba97SEli Cohen 	int ret;
3102e126ba97SEli Cohen 
3103e126ba97SEli Cohen 	attr = kzalloc(sizeof(*attr), GFP_KERNEL);
3104e126ba97SEli Cohen 	init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL);
3105e126ba97SEli Cohen 	if (!attr || !init_attr) {
3106e126ba97SEli Cohen 		ret = -ENOMEM;
3107e126ba97SEli Cohen 		goto error_0;
3108e126ba97SEli Cohen 	}
3109e126ba97SEli Cohen 
3110ed082d36SChristoph Hellwig 	pd = ib_alloc_pd(&dev->ib_dev, 0);
3111e126ba97SEli Cohen 	if (IS_ERR(pd)) {
3112e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create PD for sync UMR QP\n");
3113e126ba97SEli Cohen 		ret = PTR_ERR(pd);
3114e126ba97SEli Cohen 		goto error_0;
3115e126ba97SEli Cohen 	}
3116e126ba97SEli Cohen 
3117add08d76SChristoph Hellwig 	cq = ib_alloc_cq(&dev->ib_dev, NULL, 128, 0, IB_POLL_SOFTIRQ);
3118e126ba97SEli Cohen 	if (IS_ERR(cq)) {
3119e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create CQ for sync UMR QP\n");
3120e126ba97SEli Cohen 		ret = PTR_ERR(cq);
3121e126ba97SEli Cohen 		goto error_2;
3122e126ba97SEli Cohen 	}
3123e126ba97SEli Cohen 
3124e126ba97SEli Cohen 	init_attr->send_cq = cq;
3125e126ba97SEli Cohen 	init_attr->recv_cq = cq;
3126e126ba97SEli Cohen 	init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
3127e126ba97SEli Cohen 	init_attr->cap.max_send_wr = MAX_UMR_WR;
3128e126ba97SEli Cohen 	init_attr->cap.max_send_sge = 1;
3129e126ba97SEli Cohen 	init_attr->qp_type = MLX5_IB_QPT_REG_UMR;
3130e126ba97SEli Cohen 	init_attr->port_num = 1;
3131e126ba97SEli Cohen 	qp = mlx5_ib_create_qp(pd, init_attr, NULL);
3132e126ba97SEli Cohen 	if (IS_ERR(qp)) {
3133e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create sync UMR QP\n");
3134e126ba97SEli Cohen 		ret = PTR_ERR(qp);
3135e126ba97SEli Cohen 		goto error_3;
3136e126ba97SEli Cohen 	}
3137e126ba97SEli Cohen 	qp->device     = &dev->ib_dev;
3138e126ba97SEli Cohen 	qp->real_qp    = qp;
3139e126ba97SEli Cohen 	qp->uobject    = NULL;
3140e126ba97SEli Cohen 	qp->qp_type    = MLX5_IB_QPT_REG_UMR;
3141e126ba97SEli Cohen 
3142e126ba97SEli Cohen 	attr->qp_state = IB_QPS_INIT;
3143e126ba97SEli Cohen 	attr->port_num = 1;
3144e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_PKEY_INDEX |
3145e126ba97SEli Cohen 				IB_QP_PORT, NULL);
3146e126ba97SEli Cohen 	if (ret) {
3147e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify UMR QP\n");
3148e126ba97SEli Cohen 		goto error_4;
3149e126ba97SEli Cohen 	}
3150e126ba97SEli Cohen 
3151e126ba97SEli Cohen 	memset(attr, 0, sizeof(*attr));
3152e126ba97SEli Cohen 	attr->qp_state = IB_QPS_RTR;
3153e126ba97SEli Cohen 	attr->path_mtu = IB_MTU_256;
3154e126ba97SEli Cohen 
3155e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
3156e126ba97SEli Cohen 	if (ret) {
3157e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify umr QP to rtr\n");
3158e126ba97SEli Cohen 		goto error_4;
3159e126ba97SEli Cohen 	}
3160e126ba97SEli Cohen 
3161e126ba97SEli Cohen 	memset(attr, 0, sizeof(*attr));
3162e126ba97SEli Cohen 	attr->qp_state = IB_QPS_RTS;
3163e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
3164e126ba97SEli Cohen 	if (ret) {
3165e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify umr QP to rts\n");
3166e126ba97SEli Cohen 		goto error_4;
3167e126ba97SEli Cohen 	}
3168e126ba97SEli Cohen 
3169e126ba97SEli Cohen 	dev->umrc.qp = qp;
3170e126ba97SEli Cohen 	dev->umrc.cq = cq;
3171e126ba97SEli Cohen 	dev->umrc.pd = pd;
3172e126ba97SEli Cohen 
3173e126ba97SEli Cohen 	sema_init(&dev->umrc.sem, MAX_UMR_WR);
3174e126ba97SEli Cohen 	ret = mlx5_mr_cache_init(dev);
3175e126ba97SEli Cohen 	if (ret) {
3176e126ba97SEli Cohen 		mlx5_ib_warn(dev, "mr cache init failed %d\n", ret);
3177e126ba97SEli Cohen 		goto error_4;
3178e126ba97SEli Cohen 	}
3179e126ba97SEli Cohen 
3180e126ba97SEli Cohen 	kfree(attr);
3181e126ba97SEli Cohen 	kfree(init_attr);
3182e126ba97SEli Cohen 
3183e126ba97SEli Cohen 	return 0;
3184e126ba97SEli Cohen 
3185e126ba97SEli Cohen error_4:
3186e126ba97SEli Cohen 	mlx5_ib_destroy_qp(qp);
3187e126ba97SEli Cohen 
3188e126ba97SEli Cohen error_3:
3189add08d76SChristoph Hellwig 	ib_free_cq(cq);
3190e126ba97SEli Cohen 
3191e126ba97SEli Cohen error_2:
3192e126ba97SEli Cohen 	ib_dealloc_pd(pd);
3193e126ba97SEli Cohen 
3194e126ba97SEli Cohen error_0:
3195e126ba97SEli Cohen 	kfree(attr);
3196e126ba97SEli Cohen 	kfree(init_attr);
3197e126ba97SEli Cohen 	return ret;
3198e126ba97SEli Cohen }
3199e126ba97SEli Cohen 
32006e8484c5SMax Gurtovoy static u8 mlx5_get_umr_fence(u8 umr_fence_cap)
32016e8484c5SMax Gurtovoy {
32026e8484c5SMax Gurtovoy 	switch (umr_fence_cap) {
32036e8484c5SMax Gurtovoy 	case MLX5_CAP_UMR_FENCE_NONE:
32046e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_NONE;
32056e8484c5SMax Gurtovoy 	case MLX5_CAP_UMR_FENCE_SMALL:
32066e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_INITIATOR_SMALL;
32076e8484c5SMax Gurtovoy 	default:
32086e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_STRONG_ORDERING;
32096e8484c5SMax Gurtovoy 	}
32106e8484c5SMax Gurtovoy }
32116e8484c5SMax Gurtovoy 
3212e126ba97SEli Cohen static int create_dev_resources(struct mlx5_ib_resources *devr)
3213e126ba97SEli Cohen {
3214e126ba97SEli Cohen 	struct ib_srq_init_attr attr;
3215e126ba97SEli Cohen 	struct mlx5_ib_dev *dev;
3216bcf4c1eaSMatan Barak 	struct ib_cq_init_attr cq_attr = {.cqe = 1};
32177722f47eSHaggai Eran 	int port;
3218e126ba97SEli Cohen 	int ret = 0;
3219e126ba97SEli Cohen 
3220e126ba97SEli Cohen 	dev = container_of(devr, struct mlx5_ib_dev, devr);
3221e126ba97SEli Cohen 
3222d16e91daSHaggai Eran 	mutex_init(&devr->mutex);
3223d16e91daSHaggai Eran 
3224e126ba97SEli Cohen 	devr->p0 = mlx5_ib_alloc_pd(&dev->ib_dev, NULL, NULL);
3225e126ba97SEli Cohen 	if (IS_ERR(devr->p0)) {
3226e126ba97SEli Cohen 		ret = PTR_ERR(devr->p0);
3227e126ba97SEli Cohen 		goto error0;
3228e126ba97SEli Cohen 	}
3229e126ba97SEli Cohen 	devr->p0->device  = &dev->ib_dev;
3230e126ba97SEli Cohen 	devr->p0->uobject = NULL;
3231e126ba97SEli Cohen 	atomic_set(&devr->p0->usecnt, 0);
3232e126ba97SEli Cohen 
3233bcf4c1eaSMatan Barak 	devr->c0 = mlx5_ib_create_cq(&dev->ib_dev, &cq_attr, NULL, NULL);
3234e126ba97SEli Cohen 	if (IS_ERR(devr->c0)) {
3235e126ba97SEli Cohen 		ret = PTR_ERR(devr->c0);
3236e126ba97SEli Cohen 		goto error1;
3237e126ba97SEli Cohen 	}
3238e126ba97SEli Cohen 	devr->c0->device        = &dev->ib_dev;
3239e126ba97SEli Cohen 	devr->c0->uobject       = NULL;
3240e126ba97SEli Cohen 	devr->c0->comp_handler  = NULL;
3241e126ba97SEli Cohen 	devr->c0->event_handler = NULL;
3242e126ba97SEli Cohen 	devr->c0->cq_context    = NULL;
3243e126ba97SEli Cohen 	atomic_set(&devr->c0->usecnt, 0);
3244e126ba97SEli Cohen 
3245e126ba97SEli Cohen 	devr->x0 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
3246e126ba97SEli Cohen 	if (IS_ERR(devr->x0)) {
3247e126ba97SEli Cohen 		ret = PTR_ERR(devr->x0);
3248e126ba97SEli Cohen 		goto error2;
3249e126ba97SEli Cohen 	}
3250e126ba97SEli Cohen 	devr->x0->device = &dev->ib_dev;
3251e126ba97SEli Cohen 	devr->x0->inode = NULL;
3252e126ba97SEli Cohen 	atomic_set(&devr->x0->usecnt, 0);
3253e126ba97SEli Cohen 	mutex_init(&devr->x0->tgt_qp_mutex);
3254e126ba97SEli Cohen 	INIT_LIST_HEAD(&devr->x0->tgt_qp_list);
3255e126ba97SEli Cohen 
3256e126ba97SEli Cohen 	devr->x1 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
3257e126ba97SEli Cohen 	if (IS_ERR(devr->x1)) {
3258e126ba97SEli Cohen 		ret = PTR_ERR(devr->x1);
3259e126ba97SEli Cohen 		goto error3;
3260e126ba97SEli Cohen 	}
3261e126ba97SEli Cohen 	devr->x1->device = &dev->ib_dev;
3262e126ba97SEli Cohen 	devr->x1->inode = NULL;
3263e126ba97SEli Cohen 	atomic_set(&devr->x1->usecnt, 0);
3264e126ba97SEli Cohen 	mutex_init(&devr->x1->tgt_qp_mutex);
3265e126ba97SEli Cohen 	INIT_LIST_HEAD(&devr->x1->tgt_qp_list);
3266e126ba97SEli Cohen 
3267e126ba97SEli Cohen 	memset(&attr, 0, sizeof(attr));
3268e126ba97SEli Cohen 	attr.attr.max_sge = 1;
3269e126ba97SEli Cohen 	attr.attr.max_wr = 1;
3270e126ba97SEli Cohen 	attr.srq_type = IB_SRQT_XRC;
32711a56ff6dSArtemy Kovalyov 	attr.ext.cq = devr->c0;
3272e126ba97SEli Cohen 	attr.ext.xrc.xrcd = devr->x0;
3273e126ba97SEli Cohen 
3274e126ba97SEli Cohen 	devr->s0 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
3275e126ba97SEli Cohen 	if (IS_ERR(devr->s0)) {
3276e126ba97SEli Cohen 		ret = PTR_ERR(devr->s0);
3277e126ba97SEli Cohen 		goto error4;
3278e126ba97SEli Cohen 	}
3279e126ba97SEli Cohen 	devr->s0->device	= &dev->ib_dev;
3280e126ba97SEli Cohen 	devr->s0->pd		= devr->p0;
3281e126ba97SEli Cohen 	devr->s0->uobject       = NULL;
3282e126ba97SEli Cohen 	devr->s0->event_handler = NULL;
3283e126ba97SEli Cohen 	devr->s0->srq_context   = NULL;
3284e126ba97SEli Cohen 	devr->s0->srq_type      = IB_SRQT_XRC;
3285e126ba97SEli Cohen 	devr->s0->ext.xrc.xrcd	= devr->x0;
32861a56ff6dSArtemy Kovalyov 	devr->s0->ext.cq	= devr->c0;
3287e126ba97SEli Cohen 	atomic_inc(&devr->s0->ext.xrc.xrcd->usecnt);
32881a56ff6dSArtemy Kovalyov 	atomic_inc(&devr->s0->ext.cq->usecnt);
3289e126ba97SEli Cohen 	atomic_inc(&devr->p0->usecnt);
3290e126ba97SEli Cohen 	atomic_set(&devr->s0->usecnt, 0);
3291e126ba97SEli Cohen 
32924aa17b28SHaggai Abramonvsky 	memset(&attr, 0, sizeof(attr));
32934aa17b28SHaggai Abramonvsky 	attr.attr.max_sge = 1;
32944aa17b28SHaggai Abramonvsky 	attr.attr.max_wr = 1;
32954aa17b28SHaggai Abramonvsky 	attr.srq_type = IB_SRQT_BASIC;
32964aa17b28SHaggai Abramonvsky 	devr->s1 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
32974aa17b28SHaggai Abramonvsky 	if (IS_ERR(devr->s1)) {
32984aa17b28SHaggai Abramonvsky 		ret = PTR_ERR(devr->s1);
32994aa17b28SHaggai Abramonvsky 		goto error5;
33004aa17b28SHaggai Abramonvsky 	}
33014aa17b28SHaggai Abramonvsky 	devr->s1->device	= &dev->ib_dev;
33024aa17b28SHaggai Abramonvsky 	devr->s1->pd		= devr->p0;
33034aa17b28SHaggai Abramonvsky 	devr->s1->uobject       = NULL;
33044aa17b28SHaggai Abramonvsky 	devr->s1->event_handler = NULL;
33054aa17b28SHaggai Abramonvsky 	devr->s1->srq_context   = NULL;
33064aa17b28SHaggai Abramonvsky 	devr->s1->srq_type      = IB_SRQT_BASIC;
33071a56ff6dSArtemy Kovalyov 	devr->s1->ext.cq	= devr->c0;
33084aa17b28SHaggai Abramonvsky 	atomic_inc(&devr->p0->usecnt);
33091a56ff6dSArtemy Kovalyov 	atomic_set(&devr->s1->usecnt, 0);
33104aa17b28SHaggai Abramonvsky 
33117722f47eSHaggai Eran 	for (port = 0; port < ARRAY_SIZE(devr->ports); ++port) {
33127722f47eSHaggai Eran 		INIT_WORK(&devr->ports[port].pkey_change_work,
33137722f47eSHaggai Eran 			  pkey_change_handler);
33147722f47eSHaggai Eran 		devr->ports[port].devr = devr;
33157722f47eSHaggai Eran 	}
33167722f47eSHaggai Eran 
3317e126ba97SEli Cohen 	return 0;
3318e126ba97SEli Cohen 
33194aa17b28SHaggai Abramonvsky error5:
33204aa17b28SHaggai Abramonvsky 	mlx5_ib_destroy_srq(devr->s0);
3321e126ba97SEli Cohen error4:
3322e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x1);
3323e126ba97SEli Cohen error3:
3324e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x0);
3325e126ba97SEli Cohen error2:
3326e126ba97SEli Cohen 	mlx5_ib_destroy_cq(devr->c0);
3327e126ba97SEli Cohen error1:
3328e126ba97SEli Cohen 	mlx5_ib_dealloc_pd(devr->p0);
3329e126ba97SEli Cohen error0:
3330e126ba97SEli Cohen 	return ret;
3331e126ba97SEli Cohen }
3332e126ba97SEli Cohen 
3333e126ba97SEli Cohen static void destroy_dev_resources(struct mlx5_ib_resources *devr)
3334e126ba97SEli Cohen {
33357722f47eSHaggai Eran 	struct mlx5_ib_dev *dev =
33367722f47eSHaggai Eran 		container_of(devr, struct mlx5_ib_dev, devr);
33377722f47eSHaggai Eran 	int port;
33387722f47eSHaggai Eran 
33394aa17b28SHaggai Abramonvsky 	mlx5_ib_destroy_srq(devr->s1);
3340e126ba97SEli Cohen 	mlx5_ib_destroy_srq(devr->s0);
3341e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x0);
3342e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x1);
3343e126ba97SEli Cohen 	mlx5_ib_destroy_cq(devr->c0);
3344e126ba97SEli Cohen 	mlx5_ib_dealloc_pd(devr->p0);
33457722f47eSHaggai Eran 
33467722f47eSHaggai Eran 	/* Make sure no change P_Key work items are still executing */
33477722f47eSHaggai Eran 	for (port = 0; port < dev->num_ports; ++port)
33487722f47eSHaggai Eran 		cancel_work_sync(&devr->ports[port].pkey_change_work);
3349e126ba97SEli Cohen }
3350e126ba97SEli Cohen 
3351e53505a8SAchiad Shochat static u32 get_core_cap_flags(struct ib_device *ibdev)
3352e53505a8SAchiad Shochat {
3353e53505a8SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
3354e53505a8SAchiad Shochat 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, 1);
3355e53505a8SAchiad Shochat 	u8 l3_type_cap = MLX5_CAP_ROCE(dev->mdev, l3_type);
3356e53505a8SAchiad Shochat 	u8 roce_version_cap = MLX5_CAP_ROCE(dev->mdev, roce_version);
3357e53505a8SAchiad Shochat 	u32 ret = 0;
3358e53505a8SAchiad Shochat 
3359e53505a8SAchiad Shochat 	if (ll == IB_LINK_LAYER_INFINIBAND)
3360e53505a8SAchiad Shochat 		return RDMA_CORE_PORT_IBA_IB;
3361e53505a8SAchiad Shochat 
336272cd5717SOr Gerlitz 	ret = RDMA_CORE_PORT_RAW_PACKET;
336372cd5717SOr Gerlitz 
3364e53505a8SAchiad Shochat 	if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV4_CAP))
336572cd5717SOr Gerlitz 		return ret;
3366e53505a8SAchiad Shochat 
3367e53505a8SAchiad Shochat 	if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV6_CAP))
336872cd5717SOr Gerlitz 		return ret;
3369e53505a8SAchiad Shochat 
3370e53505a8SAchiad Shochat 	if (roce_version_cap & MLX5_ROCE_VERSION_1_CAP)
3371e53505a8SAchiad Shochat 		ret |= RDMA_CORE_PORT_IBA_ROCE;
3372e53505a8SAchiad Shochat 
3373e53505a8SAchiad Shochat 	if (roce_version_cap & MLX5_ROCE_VERSION_2_CAP)
3374e53505a8SAchiad Shochat 		ret |= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
3375e53505a8SAchiad Shochat 
3376e53505a8SAchiad Shochat 	return ret;
3377e53505a8SAchiad Shochat }
3378e53505a8SAchiad Shochat 
33797738613eSIra Weiny static int mlx5_port_immutable(struct ib_device *ibdev, u8 port_num,
33807738613eSIra Weiny 			       struct ib_port_immutable *immutable)
33817738613eSIra Weiny {
33827738613eSIra Weiny 	struct ib_port_attr attr;
3383ca5b91d6SOr Gerlitz 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
3384ca5b91d6SOr Gerlitz 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, port_num);
33857738613eSIra Weiny 	int err;
33867738613eSIra Weiny 
3387c4550c63SOr Gerlitz 	immutable->core_cap_flags = get_core_cap_flags(ibdev);
3388c4550c63SOr Gerlitz 
3389c4550c63SOr Gerlitz 	err = ib_query_port(ibdev, port_num, &attr);
33907738613eSIra Weiny 	if (err)
33917738613eSIra Weiny 		return err;
33927738613eSIra Weiny 
33937738613eSIra Weiny 	immutable->pkey_tbl_len = attr.pkey_tbl_len;
33947738613eSIra Weiny 	immutable->gid_tbl_len = attr.gid_tbl_len;
3395e53505a8SAchiad Shochat 	immutable->core_cap_flags = get_core_cap_flags(ibdev);
3396ca5b91d6SOr Gerlitz 	if ((ll == IB_LINK_LAYER_INFINIBAND) || MLX5_CAP_GEN(dev->mdev, roce))
3397337877a4SIra Weiny 		immutable->max_mad_size = IB_MGMT_MAD_SIZE;
33987738613eSIra Weiny 
33997738613eSIra Weiny 	return 0;
34007738613eSIra Weiny }
34017738613eSIra Weiny 
34029abb0d1bSLeon Romanovsky static void get_dev_fw_str(struct ib_device *ibdev, char *str)
3403c7342823SIra Weiny {
3404c7342823SIra Weiny 	struct mlx5_ib_dev *dev =
3405c7342823SIra Weiny 		container_of(ibdev, struct mlx5_ib_dev, ib_dev);
34069abb0d1bSLeon Romanovsky 	snprintf(str, IB_FW_VERSION_NAME_MAX, "%d.%d.%04d",
34079abb0d1bSLeon Romanovsky 		 fw_rev_maj(dev->mdev), fw_rev_min(dev->mdev),
34089abb0d1bSLeon Romanovsky 		 fw_rev_sub(dev->mdev));
3409c7342823SIra Weiny }
3410c7342823SIra Weiny 
341145f95acdSOr Gerlitz static int mlx5_eth_lag_init(struct mlx5_ib_dev *dev)
34129ef9c640SAviv Heller {
34139ef9c640SAviv Heller 	struct mlx5_core_dev *mdev = dev->mdev;
34149ef9c640SAviv Heller 	struct mlx5_flow_namespace *ns = mlx5_get_flow_namespace(mdev,
34159ef9c640SAviv Heller 								 MLX5_FLOW_NAMESPACE_LAG);
34169ef9c640SAviv Heller 	struct mlx5_flow_table *ft;
34179ef9c640SAviv Heller 	int err;
34189ef9c640SAviv Heller 
34199ef9c640SAviv Heller 	if (!ns || !mlx5_lag_is_active(mdev))
34209ef9c640SAviv Heller 		return 0;
34219ef9c640SAviv Heller 
34229ef9c640SAviv Heller 	err = mlx5_cmd_create_vport_lag(mdev);
34239ef9c640SAviv Heller 	if (err)
34249ef9c640SAviv Heller 		return err;
34259ef9c640SAviv Heller 
34269ef9c640SAviv Heller 	ft = mlx5_create_lag_demux_flow_table(ns, 0, 0);
34279ef9c640SAviv Heller 	if (IS_ERR(ft)) {
34289ef9c640SAviv Heller 		err = PTR_ERR(ft);
34299ef9c640SAviv Heller 		goto err_destroy_vport_lag;
34309ef9c640SAviv Heller 	}
34319ef9c640SAviv Heller 
34329ef9c640SAviv Heller 	dev->flow_db.lag_demux_ft = ft;
34339ef9c640SAviv Heller 	return 0;
34349ef9c640SAviv Heller 
34359ef9c640SAviv Heller err_destroy_vport_lag:
34369ef9c640SAviv Heller 	mlx5_cmd_destroy_vport_lag(mdev);
34379ef9c640SAviv Heller 	return err;
34389ef9c640SAviv Heller }
34399ef9c640SAviv Heller 
344045f95acdSOr Gerlitz static void mlx5_eth_lag_cleanup(struct mlx5_ib_dev *dev)
34419ef9c640SAviv Heller {
34429ef9c640SAviv Heller 	struct mlx5_core_dev *mdev = dev->mdev;
34439ef9c640SAviv Heller 
34449ef9c640SAviv Heller 	if (dev->flow_db.lag_demux_ft) {
34459ef9c640SAviv Heller 		mlx5_destroy_flow_table(dev->flow_db.lag_demux_ft);
34469ef9c640SAviv Heller 		dev->flow_db.lag_demux_ft = NULL;
34479ef9c640SAviv Heller 
34489ef9c640SAviv Heller 		mlx5_cmd_destroy_vport_lag(mdev);
34499ef9c640SAviv Heller 	}
34509ef9c640SAviv Heller }
34519ef9c640SAviv Heller 
3452d012f5d6SOr Gerlitz static int mlx5_add_netdev_notifier(struct mlx5_ib_dev *dev)
3453fc24fc5eSAchiad Shochat {
3454e53505a8SAchiad Shochat 	int err;
3455e53505a8SAchiad Shochat 
3456fc24fc5eSAchiad Shochat 	dev->roce.nb.notifier_call = mlx5_netdev_event;
3457e53505a8SAchiad Shochat 	err = register_netdevice_notifier(&dev->roce.nb);
34585ec8c83eSAviv Heller 	if (err) {
34595ec8c83eSAviv Heller 		dev->roce.nb.notifier_call = NULL;
3460e53505a8SAchiad Shochat 		return err;
34615ec8c83eSAviv Heller 	}
3462e53505a8SAchiad Shochat 
3463d012f5d6SOr Gerlitz 	return 0;
3464d012f5d6SOr Gerlitz }
3465d012f5d6SOr Gerlitz 
3466d012f5d6SOr Gerlitz static void mlx5_remove_netdev_notifier(struct mlx5_ib_dev *dev)
3467e126ba97SEli Cohen {
3468e126ba97SEli Cohen 	if (dev->roce.nb.notifier_call) {
3469e126ba97SEli Cohen 		unregister_netdevice_notifier(&dev->roce.nb);
3470e126ba97SEli Cohen 		dev->roce.nb.notifier_call = NULL;
3471e126ba97SEli Cohen 	}
3472e126ba97SEli Cohen }
3473e126ba97SEli Cohen 
347445f95acdSOr Gerlitz static int mlx5_enable_eth(struct mlx5_ib_dev *dev)
3475e126ba97SEli Cohen {
3476e126ba97SEli Cohen 	int err;
3477e126ba97SEli Cohen 
3478d012f5d6SOr Gerlitz 	err = mlx5_add_netdev_notifier(dev);
3479d012f5d6SOr Gerlitz 	if (err)
3480e53505a8SAchiad Shochat 		return err;
3481e53505a8SAchiad Shochat 
3482ca5b91d6SOr Gerlitz 	if (MLX5_CAP_GEN(dev->mdev, roce)) {
3483e53505a8SAchiad Shochat 		err = mlx5_nic_vport_enable_roce(dev->mdev);
3484e53505a8SAchiad Shochat 		if (err)
3485e53505a8SAchiad Shochat 			goto err_unregister_netdevice_notifier;
3486ca5b91d6SOr Gerlitz 	}
3487e53505a8SAchiad Shochat 
348845f95acdSOr Gerlitz 	err = mlx5_eth_lag_init(dev);
34899ef9c640SAviv Heller 	if (err)
34909ef9c640SAviv Heller 		goto err_disable_roce;
34919ef9c640SAviv Heller 
3492e53505a8SAchiad Shochat 	return 0;
3493e53505a8SAchiad Shochat 
34949ef9c640SAviv Heller err_disable_roce:
3495ca5b91d6SOr Gerlitz 	if (MLX5_CAP_GEN(dev->mdev, roce))
34969ef9c640SAviv Heller 		mlx5_nic_vport_disable_roce(dev->mdev);
34979ef9c640SAviv Heller 
3498e53505a8SAchiad Shochat err_unregister_netdevice_notifier:
3499d012f5d6SOr Gerlitz 	mlx5_remove_netdev_notifier(dev);
3500e53505a8SAchiad Shochat 	return err;
3501fc24fc5eSAchiad Shochat }
3502fc24fc5eSAchiad Shochat 
350345f95acdSOr Gerlitz static void mlx5_disable_eth(struct mlx5_ib_dev *dev)
3504fc24fc5eSAchiad Shochat {
350545f95acdSOr Gerlitz 	mlx5_eth_lag_cleanup(dev);
3506ca5b91d6SOr Gerlitz 	if (MLX5_CAP_GEN(dev->mdev, roce))
3507e53505a8SAchiad Shochat 		mlx5_nic_vport_disable_roce(dev->mdev);
3508fc24fc5eSAchiad Shochat }
3509fc24fc5eSAchiad Shochat 
3510e1f24a79SParav Pandit struct mlx5_ib_counter {
35117c16f477SKamal Heib 	const char *name;
35127c16f477SKamal Heib 	size_t offset;
35137c16f477SKamal Heib };
35147c16f477SKamal Heib 
35157c16f477SKamal Heib #define INIT_Q_COUNTER(_name)		\
35167c16f477SKamal Heib 	{ .name = #_name, .offset = MLX5_BYTE_OFF(query_q_counter_out, _name)}
35177c16f477SKamal Heib 
3518e1f24a79SParav Pandit static const struct mlx5_ib_counter basic_q_cnts[] = {
35197c16f477SKamal Heib 	INIT_Q_COUNTER(rx_write_requests),
35207c16f477SKamal Heib 	INIT_Q_COUNTER(rx_read_requests),
35217c16f477SKamal Heib 	INIT_Q_COUNTER(rx_atomic_requests),
35227c16f477SKamal Heib 	INIT_Q_COUNTER(out_of_buffer),
35237c16f477SKamal Heib };
35247c16f477SKamal Heib 
3525e1f24a79SParav Pandit static const struct mlx5_ib_counter out_of_seq_q_cnts[] = {
35267c16f477SKamal Heib 	INIT_Q_COUNTER(out_of_sequence),
35277c16f477SKamal Heib };
35287c16f477SKamal Heib 
3529e1f24a79SParav Pandit static const struct mlx5_ib_counter retrans_q_cnts[] = {
35307c16f477SKamal Heib 	INIT_Q_COUNTER(duplicate_request),
35317c16f477SKamal Heib 	INIT_Q_COUNTER(rnr_nak_retry_err),
35327c16f477SKamal Heib 	INIT_Q_COUNTER(packet_seq_err),
35337c16f477SKamal Heib 	INIT_Q_COUNTER(implied_nak_seq_err),
35347c16f477SKamal Heib 	INIT_Q_COUNTER(local_ack_timeout_err),
35357c16f477SKamal Heib };
35367c16f477SKamal Heib 
3537e1f24a79SParav Pandit #define INIT_CONG_COUNTER(_name)		\
3538e1f24a79SParav Pandit 	{ .name = #_name, .offset =	\
3539e1f24a79SParav Pandit 		MLX5_BYTE_OFF(query_cong_statistics_out, _name ## _high)}
3540e1f24a79SParav Pandit 
3541e1f24a79SParav Pandit static const struct mlx5_ib_counter cong_cnts[] = {
3542e1f24a79SParav Pandit 	INIT_CONG_COUNTER(rp_cnp_ignored),
3543e1f24a79SParav Pandit 	INIT_CONG_COUNTER(rp_cnp_handled),
3544e1f24a79SParav Pandit 	INIT_CONG_COUNTER(np_ecn_marked_roce_packets),
3545e1f24a79SParav Pandit 	INIT_CONG_COUNTER(np_cnp_sent),
3546e1f24a79SParav Pandit };
3547e1f24a79SParav Pandit 
354858dcb60aSParav Pandit static const struct mlx5_ib_counter extended_err_cnts[] = {
354958dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_local_length_error),
355058dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_cqe_error),
355158dcb60aSParav Pandit 	INIT_Q_COUNTER(req_cqe_error),
355258dcb60aSParav Pandit 	INIT_Q_COUNTER(req_remote_invalid_request),
355358dcb60aSParav Pandit 	INIT_Q_COUNTER(req_remote_access_errors),
355458dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_remote_access_errors),
355558dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_cqe_flush_error),
355658dcb60aSParav Pandit 	INIT_Q_COUNTER(req_cqe_flush_error),
355758dcb60aSParav Pandit };
355858dcb60aSParav Pandit 
3559e1f24a79SParav Pandit static void mlx5_ib_dealloc_counters(struct mlx5_ib_dev *dev)
35600837e86aSMark Bloch {
35610837e86aSMark Bloch 	unsigned int i;
35620837e86aSMark Bloch 
35637c16f477SKamal Heib 	for (i = 0; i < dev->num_ports; i++) {
35640837e86aSMark Bloch 		mlx5_core_dealloc_q_counter(dev->mdev,
3565e1f24a79SParav Pandit 					    dev->port[i].cnts.set_id);
3566e1f24a79SParav Pandit 		kfree(dev->port[i].cnts.names);
3567e1f24a79SParav Pandit 		kfree(dev->port[i].cnts.offsets);
35687c16f477SKamal Heib 	}
35697c16f477SKamal Heib }
35707c16f477SKamal Heib 
3571e1f24a79SParav Pandit static int __mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev,
3572e1f24a79SParav Pandit 				    struct mlx5_ib_counters *cnts)
35737c16f477SKamal Heib {
35747c16f477SKamal Heib 	u32 num_counters;
35757c16f477SKamal Heib 
35767c16f477SKamal Heib 	num_counters = ARRAY_SIZE(basic_q_cnts);
35777c16f477SKamal Heib 
35787c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt))
35797c16f477SKamal Heib 		num_counters += ARRAY_SIZE(out_of_seq_q_cnts);
35807c16f477SKamal Heib 
35817c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters))
35827c16f477SKamal Heib 		num_counters += ARRAY_SIZE(retrans_q_cnts);
358358dcb60aSParav Pandit 
358458dcb60aSParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters))
358558dcb60aSParav Pandit 		num_counters += ARRAY_SIZE(extended_err_cnts);
358658dcb60aSParav Pandit 
3587e1f24a79SParav Pandit 	cnts->num_q_counters = num_counters;
35887c16f477SKamal Heib 
3589e1f24a79SParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
3590e1f24a79SParav Pandit 		cnts->num_cong_counters = ARRAY_SIZE(cong_cnts);
3591e1f24a79SParav Pandit 		num_counters += ARRAY_SIZE(cong_cnts);
3592e1f24a79SParav Pandit 	}
3593e1f24a79SParav Pandit 
3594e1f24a79SParav Pandit 	cnts->names = kcalloc(num_counters, sizeof(cnts->names), GFP_KERNEL);
3595e1f24a79SParav Pandit 	if (!cnts->names)
35967c16f477SKamal Heib 		return -ENOMEM;
35977c16f477SKamal Heib 
3598e1f24a79SParav Pandit 	cnts->offsets = kcalloc(num_counters,
3599e1f24a79SParav Pandit 				sizeof(cnts->offsets), GFP_KERNEL);
3600e1f24a79SParav Pandit 	if (!cnts->offsets)
36017c16f477SKamal Heib 		goto err_names;
36027c16f477SKamal Heib 
36037c16f477SKamal Heib 	return 0;
36047c16f477SKamal Heib 
36057c16f477SKamal Heib err_names:
3606e1f24a79SParav Pandit 	kfree(cnts->names);
36077c16f477SKamal Heib 	return -ENOMEM;
36087c16f477SKamal Heib }
36097c16f477SKamal Heib 
3610e1f24a79SParav Pandit static void mlx5_ib_fill_counters(struct mlx5_ib_dev *dev,
36117c16f477SKamal Heib 				  const char **names,
36127c16f477SKamal Heib 				  size_t *offsets)
36137c16f477SKamal Heib {
36147c16f477SKamal Heib 	int i;
36157c16f477SKamal Heib 	int j = 0;
36167c16f477SKamal Heib 
36177c16f477SKamal Heib 	for (i = 0; i < ARRAY_SIZE(basic_q_cnts); i++, j++) {
36187c16f477SKamal Heib 		names[j] = basic_q_cnts[i].name;
36197c16f477SKamal Heib 		offsets[j] = basic_q_cnts[i].offset;
36207c16f477SKamal Heib 	}
36217c16f477SKamal Heib 
36227c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt)) {
36237c16f477SKamal Heib 		for (i = 0; i < ARRAY_SIZE(out_of_seq_q_cnts); i++, j++) {
36247c16f477SKamal Heib 			names[j] = out_of_seq_q_cnts[i].name;
36257c16f477SKamal Heib 			offsets[j] = out_of_seq_q_cnts[i].offset;
36267c16f477SKamal Heib 		}
36277c16f477SKamal Heib 	}
36287c16f477SKamal Heib 
36297c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters)) {
36307c16f477SKamal Heib 		for (i = 0; i < ARRAY_SIZE(retrans_q_cnts); i++, j++) {
36317c16f477SKamal Heib 			names[j] = retrans_q_cnts[i].name;
36327c16f477SKamal Heib 			offsets[j] = retrans_q_cnts[i].offset;
36337c16f477SKamal Heib 		}
36347c16f477SKamal Heib 	}
3635e1f24a79SParav Pandit 
363658dcb60aSParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters)) {
363758dcb60aSParav Pandit 		for (i = 0; i < ARRAY_SIZE(extended_err_cnts); i++, j++) {
363858dcb60aSParav Pandit 			names[j] = extended_err_cnts[i].name;
363958dcb60aSParav Pandit 			offsets[j] = extended_err_cnts[i].offset;
364058dcb60aSParav Pandit 		}
364158dcb60aSParav Pandit 	}
364258dcb60aSParav Pandit 
3643e1f24a79SParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
3644e1f24a79SParav Pandit 		for (i = 0; i < ARRAY_SIZE(cong_cnts); i++, j++) {
3645e1f24a79SParav Pandit 			names[j] = cong_cnts[i].name;
3646e1f24a79SParav Pandit 			offsets[j] = cong_cnts[i].offset;
3647e1f24a79SParav Pandit 		}
3648e1f24a79SParav Pandit 	}
36490837e86aSMark Bloch }
36500837e86aSMark Bloch 
3651e1f24a79SParav Pandit static int mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev)
36520837e86aSMark Bloch {
36530837e86aSMark Bloch 	int i;
36540837e86aSMark Bloch 	int ret;
36550837e86aSMark Bloch 
36560837e86aSMark Bloch 	for (i = 0; i < dev->num_ports; i++) {
36577c16f477SKamal Heib 		struct mlx5_ib_port *port = &dev->port[i];
36587c16f477SKamal Heib 
36590837e86aSMark Bloch 		ret = mlx5_core_alloc_q_counter(dev->mdev,
3660e1f24a79SParav Pandit 						&port->cnts.set_id);
36610837e86aSMark Bloch 		if (ret) {
36620837e86aSMark Bloch 			mlx5_ib_warn(dev,
36630837e86aSMark Bloch 				     "couldn't allocate queue counter for port %d, err %d\n",
36640837e86aSMark Bloch 				     i + 1, ret);
36650837e86aSMark Bloch 			goto dealloc_counters;
36660837e86aSMark Bloch 		}
36677c16f477SKamal Heib 
3668e1f24a79SParav Pandit 		ret = __mlx5_ib_alloc_counters(dev, &port->cnts);
36697c16f477SKamal Heib 		if (ret)
36707c16f477SKamal Heib 			goto dealloc_counters;
36717c16f477SKamal Heib 
3672e1f24a79SParav Pandit 		mlx5_ib_fill_counters(dev, port->cnts.names,
3673e1f24a79SParav Pandit 				      port->cnts.offsets);
36740837e86aSMark Bloch 	}
36750837e86aSMark Bloch 
36760837e86aSMark Bloch 	return 0;
36770837e86aSMark Bloch 
36780837e86aSMark Bloch dealloc_counters:
36790837e86aSMark Bloch 	while (--i >= 0)
36800837e86aSMark Bloch 		mlx5_core_dealloc_q_counter(dev->mdev,
3681e1f24a79SParav Pandit 					    dev->port[i].cnts.set_id);
36820837e86aSMark Bloch 
36830837e86aSMark Bloch 	return ret;
36840837e86aSMark Bloch }
36850837e86aSMark Bloch 
36860ad17a8fSMark Bloch static struct rdma_hw_stats *mlx5_ib_alloc_hw_stats(struct ib_device *ibdev,
36870ad17a8fSMark Bloch 						    u8 port_num)
36880ad17a8fSMark Bloch {
36897c16f477SKamal Heib 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
36907c16f477SKamal Heib 	struct mlx5_ib_port *port = &dev->port[port_num - 1];
36910ad17a8fSMark Bloch 
36920ad17a8fSMark Bloch 	/* We support only per port stats */
36930ad17a8fSMark Bloch 	if (port_num == 0)
36940ad17a8fSMark Bloch 		return NULL;
36950ad17a8fSMark Bloch 
3696e1f24a79SParav Pandit 	return rdma_alloc_hw_stats_struct(port->cnts.names,
3697e1f24a79SParav Pandit 					  port->cnts.num_q_counters +
3698e1f24a79SParav Pandit 					  port->cnts.num_cong_counters,
36990ad17a8fSMark Bloch 					  RDMA_HW_STATS_DEFAULT_LIFESPAN);
37000ad17a8fSMark Bloch }
37010ad17a8fSMark Bloch 
3702e1f24a79SParav Pandit static int mlx5_ib_query_q_counters(struct mlx5_ib_dev *dev,
3703e1f24a79SParav Pandit 				    struct mlx5_ib_port *port,
3704e1f24a79SParav Pandit 				    struct rdma_hw_stats *stats)
3705e1f24a79SParav Pandit {
3706e1f24a79SParav Pandit 	int outlen = MLX5_ST_SZ_BYTES(query_q_counter_out);
3707e1f24a79SParav Pandit 	void *out;
3708e1f24a79SParav Pandit 	__be32 val;
3709e1f24a79SParav Pandit 	int ret, i;
3710e1f24a79SParav Pandit 
37111b9a07eeSLeon Romanovsky 	out = kvzalloc(outlen, GFP_KERNEL);
3712e1f24a79SParav Pandit 	if (!out)
3713e1f24a79SParav Pandit 		return -ENOMEM;
3714e1f24a79SParav Pandit 
3715e1f24a79SParav Pandit 	ret = mlx5_core_query_q_counter(dev->mdev,
3716e1f24a79SParav Pandit 					port->cnts.set_id, 0,
3717e1f24a79SParav Pandit 					out, outlen);
3718e1f24a79SParav Pandit 	if (ret)
3719e1f24a79SParav Pandit 		goto free;
3720e1f24a79SParav Pandit 
3721e1f24a79SParav Pandit 	for (i = 0; i < port->cnts.num_q_counters; i++) {
3722e1f24a79SParav Pandit 		val = *(__be32 *)(out + port->cnts.offsets[i]);
3723e1f24a79SParav Pandit 		stats->value[i] = (u64)be32_to_cpu(val);
3724e1f24a79SParav Pandit 	}
3725e1f24a79SParav Pandit 
3726e1f24a79SParav Pandit free:
3727e1f24a79SParav Pandit 	kvfree(out);
3728e1f24a79SParav Pandit 	return ret;
3729e1f24a79SParav Pandit }
3730e1f24a79SParav Pandit 
3731e1f24a79SParav Pandit static int mlx5_ib_query_cong_counters(struct mlx5_ib_dev *dev,
3732e1f24a79SParav Pandit 				       struct mlx5_ib_port *port,
3733e1f24a79SParav Pandit 				       struct rdma_hw_stats *stats)
3734e1f24a79SParav Pandit {
3735e1f24a79SParav Pandit 	int outlen = MLX5_ST_SZ_BYTES(query_cong_statistics_out);
3736e1f24a79SParav Pandit 	void *out;
3737e1f24a79SParav Pandit 	int ret, i;
3738e1f24a79SParav Pandit 	int offset = port->cnts.num_q_counters;
3739e1f24a79SParav Pandit 
37401b9a07eeSLeon Romanovsky 	out = kvzalloc(outlen, GFP_KERNEL);
3741e1f24a79SParav Pandit 	if (!out)
3742e1f24a79SParav Pandit 		return -ENOMEM;
3743e1f24a79SParav Pandit 
3744e1f24a79SParav Pandit 	ret = mlx5_cmd_query_cong_counter(dev->mdev, false, out, outlen);
3745e1f24a79SParav Pandit 	if (ret)
3746e1f24a79SParav Pandit 		goto free;
3747e1f24a79SParav Pandit 
3748e1f24a79SParav Pandit 	for (i = 0; i < port->cnts.num_cong_counters; i++) {
3749e1f24a79SParav Pandit 		stats->value[i + offset] =
3750e1f24a79SParav Pandit 			be64_to_cpup((__be64 *)(out +
3751e1f24a79SParav Pandit 				     port->cnts.offsets[i + offset]));
3752e1f24a79SParav Pandit 	}
3753e1f24a79SParav Pandit 
3754e1f24a79SParav Pandit free:
3755e1f24a79SParav Pandit 	kvfree(out);
3756e1f24a79SParav Pandit 	return ret;
3757e1f24a79SParav Pandit }
3758e1f24a79SParav Pandit 
37590ad17a8fSMark Bloch static int mlx5_ib_get_hw_stats(struct ib_device *ibdev,
37600ad17a8fSMark Bloch 				struct rdma_hw_stats *stats,
37617c16f477SKamal Heib 				u8 port_num, int index)
37620ad17a8fSMark Bloch {
37630ad17a8fSMark Bloch 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
37647c16f477SKamal Heib 	struct mlx5_ib_port *port = &dev->port[port_num - 1];
3765e1f24a79SParav Pandit 	int ret, num_counters;
37660ad17a8fSMark Bloch 
37677c16f477SKamal Heib 	if (!stats)
3768e1f24a79SParav Pandit 		return -EINVAL;
37690ad17a8fSMark Bloch 
3770e1f24a79SParav Pandit 	ret = mlx5_ib_query_q_counters(dev, port, stats);
37710ad17a8fSMark Bloch 	if (ret)
3772e1f24a79SParav Pandit 		return ret;
3773e1f24a79SParav Pandit 	num_counters = port->cnts.num_q_counters;
37740ad17a8fSMark Bloch 
3775e1f24a79SParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
3776e1f24a79SParav Pandit 		ret = mlx5_ib_query_cong_counters(dev, port, stats);
3777e1f24a79SParav Pandit 		if (ret)
3778e1f24a79SParav Pandit 			return ret;
3779e1f24a79SParav Pandit 		num_counters += port->cnts.num_cong_counters;
37800ad17a8fSMark Bloch 	}
37817c16f477SKamal Heib 
3782e1f24a79SParav Pandit 	return num_counters;
37830ad17a8fSMark Bloch }
37840ad17a8fSMark Bloch 
37858e959601SNiranjana Vishwanathapura static void mlx5_ib_free_rdma_netdev(struct net_device *netdev)
37868e959601SNiranjana Vishwanathapura {
37878e959601SNiranjana Vishwanathapura 	return mlx5_rdma_netdev_free(netdev);
37888e959601SNiranjana Vishwanathapura }
37898e959601SNiranjana Vishwanathapura 
3790693dfd5aSErez Shitrit static struct net_device*
3791693dfd5aSErez Shitrit mlx5_ib_alloc_rdma_netdev(struct ib_device *hca,
3792693dfd5aSErez Shitrit 			  u8 port_num,
3793693dfd5aSErez Shitrit 			  enum rdma_netdev_t type,
3794693dfd5aSErez Shitrit 			  const char *name,
3795693dfd5aSErez Shitrit 			  unsigned char name_assign_type,
3796693dfd5aSErez Shitrit 			  void (*setup)(struct net_device *))
3797693dfd5aSErez Shitrit {
37988e959601SNiranjana Vishwanathapura 	struct net_device *netdev;
37998e959601SNiranjana Vishwanathapura 	struct rdma_netdev *rn;
38008e959601SNiranjana Vishwanathapura 
3801693dfd5aSErez Shitrit 	if (type != RDMA_NETDEV_IPOIB)
3802693dfd5aSErez Shitrit 		return ERR_PTR(-EOPNOTSUPP);
3803693dfd5aSErez Shitrit 
38048e959601SNiranjana Vishwanathapura 	netdev = mlx5_rdma_netdev_alloc(to_mdev(hca)->mdev, hca,
3805693dfd5aSErez Shitrit 					name, setup);
38068e959601SNiranjana Vishwanathapura 	if (likely(!IS_ERR_OR_NULL(netdev))) {
38078e959601SNiranjana Vishwanathapura 		rn = netdev_priv(netdev);
38088e959601SNiranjana Vishwanathapura 		rn->free_rdma_netdev = mlx5_ib_free_rdma_netdev;
3809693dfd5aSErez Shitrit 	}
38108e959601SNiranjana Vishwanathapura 	return netdev;
3811693dfd5aSErez Shitrit }
3812693dfd5aSErez Shitrit 
3813fe248c3aSMaor Gottlieb static void delay_drop_debugfs_cleanup(struct mlx5_ib_dev *dev)
3814fe248c3aSMaor Gottlieb {
3815fe248c3aSMaor Gottlieb 	if (!dev->delay_drop.dbg)
3816fe248c3aSMaor Gottlieb 		return;
3817fe248c3aSMaor Gottlieb 	debugfs_remove_recursive(dev->delay_drop.dbg->dir_debugfs);
3818fe248c3aSMaor Gottlieb 	kfree(dev->delay_drop.dbg);
3819fe248c3aSMaor Gottlieb 	dev->delay_drop.dbg = NULL;
3820fe248c3aSMaor Gottlieb }
3821fe248c3aSMaor Gottlieb 
382203404e8aSMaor Gottlieb static void cancel_delay_drop(struct mlx5_ib_dev *dev)
382303404e8aSMaor Gottlieb {
382403404e8aSMaor Gottlieb 	if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
382503404e8aSMaor Gottlieb 		return;
382603404e8aSMaor Gottlieb 
382703404e8aSMaor Gottlieb 	cancel_work_sync(&dev->delay_drop.delay_drop_work);
3828fe248c3aSMaor Gottlieb 	delay_drop_debugfs_cleanup(dev);
3829fe248c3aSMaor Gottlieb }
3830fe248c3aSMaor Gottlieb 
3831fe248c3aSMaor Gottlieb static ssize_t delay_drop_timeout_read(struct file *filp, char __user *buf,
3832fe248c3aSMaor Gottlieb 				       size_t count, loff_t *pos)
3833fe248c3aSMaor Gottlieb {
3834fe248c3aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
3835fe248c3aSMaor Gottlieb 	char lbuf[20];
3836fe248c3aSMaor Gottlieb 	int len;
3837fe248c3aSMaor Gottlieb 
3838fe248c3aSMaor Gottlieb 	len = snprintf(lbuf, sizeof(lbuf), "%u\n", delay_drop->timeout);
3839fe248c3aSMaor Gottlieb 	return simple_read_from_buffer(buf, count, pos, lbuf, len);
3840fe248c3aSMaor Gottlieb }
3841fe248c3aSMaor Gottlieb 
3842fe248c3aSMaor Gottlieb static ssize_t delay_drop_timeout_write(struct file *filp, const char __user *buf,
3843fe248c3aSMaor Gottlieb 					size_t count, loff_t *pos)
3844fe248c3aSMaor Gottlieb {
3845fe248c3aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
3846fe248c3aSMaor Gottlieb 	u32 timeout;
3847fe248c3aSMaor Gottlieb 	u32 var;
3848fe248c3aSMaor Gottlieb 
3849fe248c3aSMaor Gottlieb 	if (kstrtouint_from_user(buf, count, 0, &var))
3850fe248c3aSMaor Gottlieb 		return -EFAULT;
3851fe248c3aSMaor Gottlieb 
3852fe248c3aSMaor Gottlieb 	timeout = min_t(u32, roundup(var, 100), MLX5_MAX_DELAY_DROP_TIMEOUT_MS *
3853fe248c3aSMaor Gottlieb 			1000);
3854fe248c3aSMaor Gottlieb 	if (timeout != var)
3855fe248c3aSMaor Gottlieb 		mlx5_ib_dbg(delay_drop->dev, "Round delay drop timeout to %u usec\n",
3856fe248c3aSMaor Gottlieb 			    timeout);
3857fe248c3aSMaor Gottlieb 
3858fe248c3aSMaor Gottlieb 	delay_drop->timeout = timeout;
3859fe248c3aSMaor Gottlieb 
3860fe248c3aSMaor Gottlieb 	return count;
3861fe248c3aSMaor Gottlieb }
3862fe248c3aSMaor Gottlieb 
3863fe248c3aSMaor Gottlieb static const struct file_operations fops_delay_drop_timeout = {
3864fe248c3aSMaor Gottlieb 	.owner	= THIS_MODULE,
3865fe248c3aSMaor Gottlieb 	.open	= simple_open,
3866fe248c3aSMaor Gottlieb 	.write	= delay_drop_timeout_write,
3867fe248c3aSMaor Gottlieb 	.read	= delay_drop_timeout_read,
3868fe248c3aSMaor Gottlieb };
3869fe248c3aSMaor Gottlieb 
3870fe248c3aSMaor Gottlieb static int delay_drop_debugfs_init(struct mlx5_ib_dev *dev)
3871fe248c3aSMaor Gottlieb {
3872fe248c3aSMaor Gottlieb 	struct mlx5_ib_dbg_delay_drop *dbg;
3873fe248c3aSMaor Gottlieb 
3874fe248c3aSMaor Gottlieb 	if (!mlx5_debugfs_root)
3875fe248c3aSMaor Gottlieb 		return 0;
3876fe248c3aSMaor Gottlieb 
3877fe248c3aSMaor Gottlieb 	dbg = kzalloc(sizeof(*dbg), GFP_KERNEL);
3878fe248c3aSMaor Gottlieb 	if (!dbg)
3879fe248c3aSMaor Gottlieb 		return -ENOMEM;
3880fe248c3aSMaor Gottlieb 
3881cbafad87SSudip Mukherjee 	dev->delay_drop.dbg = dbg;
3882cbafad87SSudip Mukherjee 
3883fe248c3aSMaor Gottlieb 	dbg->dir_debugfs =
3884fe248c3aSMaor Gottlieb 		debugfs_create_dir("delay_drop",
3885fe248c3aSMaor Gottlieb 				   dev->mdev->priv.dbg_root);
3886fe248c3aSMaor Gottlieb 	if (!dbg->dir_debugfs)
3887cbafad87SSudip Mukherjee 		goto out_debugfs;
3888fe248c3aSMaor Gottlieb 
3889fe248c3aSMaor Gottlieb 	dbg->events_cnt_debugfs =
3890fe248c3aSMaor Gottlieb 		debugfs_create_atomic_t("num_timeout_events", 0400,
3891fe248c3aSMaor Gottlieb 					dbg->dir_debugfs,
3892fe248c3aSMaor Gottlieb 					&dev->delay_drop.events_cnt);
3893fe248c3aSMaor Gottlieb 	if (!dbg->events_cnt_debugfs)
3894fe248c3aSMaor Gottlieb 		goto out_debugfs;
3895fe248c3aSMaor Gottlieb 
3896fe248c3aSMaor Gottlieb 	dbg->rqs_cnt_debugfs =
3897fe248c3aSMaor Gottlieb 		debugfs_create_atomic_t("num_rqs", 0400,
3898fe248c3aSMaor Gottlieb 					dbg->dir_debugfs,
3899fe248c3aSMaor Gottlieb 					&dev->delay_drop.rqs_cnt);
3900fe248c3aSMaor Gottlieb 	if (!dbg->rqs_cnt_debugfs)
3901fe248c3aSMaor Gottlieb 		goto out_debugfs;
3902fe248c3aSMaor Gottlieb 
3903fe248c3aSMaor Gottlieb 	dbg->timeout_debugfs =
3904fe248c3aSMaor Gottlieb 		debugfs_create_file("timeout", 0600,
3905fe248c3aSMaor Gottlieb 				    dbg->dir_debugfs,
3906fe248c3aSMaor Gottlieb 				    &dev->delay_drop,
3907fe248c3aSMaor Gottlieb 				    &fops_delay_drop_timeout);
3908fe248c3aSMaor Gottlieb 	if (!dbg->timeout_debugfs)
3909fe248c3aSMaor Gottlieb 		goto out_debugfs;
3910fe248c3aSMaor Gottlieb 
3911fe248c3aSMaor Gottlieb 	return 0;
3912fe248c3aSMaor Gottlieb 
3913fe248c3aSMaor Gottlieb out_debugfs:
3914fe248c3aSMaor Gottlieb 	delay_drop_debugfs_cleanup(dev);
3915fe248c3aSMaor Gottlieb 	return -ENOMEM;
391603404e8aSMaor Gottlieb }
391703404e8aSMaor Gottlieb 
391803404e8aSMaor Gottlieb static void init_delay_drop(struct mlx5_ib_dev *dev)
391903404e8aSMaor Gottlieb {
392003404e8aSMaor Gottlieb 	if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
392103404e8aSMaor Gottlieb 		return;
392203404e8aSMaor Gottlieb 
392303404e8aSMaor Gottlieb 	mutex_init(&dev->delay_drop.lock);
392403404e8aSMaor Gottlieb 	dev->delay_drop.dev = dev;
392503404e8aSMaor Gottlieb 	dev->delay_drop.activate = false;
392603404e8aSMaor Gottlieb 	dev->delay_drop.timeout = MLX5_MAX_DELAY_DROP_TIMEOUT_MS * 1000;
392703404e8aSMaor Gottlieb 	INIT_WORK(&dev->delay_drop.delay_drop_work, delay_drop_handler);
3928fe248c3aSMaor Gottlieb 	atomic_set(&dev->delay_drop.rqs_cnt, 0);
3929fe248c3aSMaor Gottlieb 	atomic_set(&dev->delay_drop.events_cnt, 0);
3930fe248c3aSMaor Gottlieb 
3931fe248c3aSMaor Gottlieb 	if (delay_drop_debugfs_init(dev))
3932fe248c3aSMaor Gottlieb 		mlx5_ib_warn(dev, "Failed to init delay drop debugfs\n");
393303404e8aSMaor Gottlieb }
393403404e8aSMaor Gottlieb 
393584305d71SLeon Romanovsky static const struct cpumask *
393684305d71SLeon Romanovsky mlx5_ib_get_vector_affinity(struct ib_device *ibdev, int comp_vector)
393740b24403SSagi Grimberg {
393840b24403SSagi Grimberg 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
393940b24403SSagi Grimberg 
394040b24403SSagi Grimberg 	return mlx5_get_vector_affinity(dev->mdev, comp_vector);
394140b24403SSagi Grimberg }
394240b24403SSagi Grimberg 
39439603b61dSJack Morgenstein static void *mlx5_ib_add(struct mlx5_core_dev *mdev)
3944e126ba97SEli Cohen {
3945e126ba97SEli Cohen 	struct mlx5_ib_dev *dev;
3946ebd61f68SAchiad Shochat 	enum rdma_link_layer ll;
3947ebd61f68SAchiad Shochat 	int port_type_cap;
39484babcf97SAviv Heller 	const char *name;
3949e126ba97SEli Cohen 	int err;
3950e126ba97SEli Cohen 	int i;
3951e126ba97SEli Cohen 
3952ebd61f68SAchiad Shochat 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
3953ebd61f68SAchiad Shochat 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
3954ebd61f68SAchiad Shochat 
3955e126ba97SEli Cohen 	printk_once(KERN_INFO "%s", mlx5_version);
3956e126ba97SEli Cohen 
3957e126ba97SEli Cohen 	dev = (struct mlx5_ib_dev *)ib_alloc_device(sizeof(*dev));
3958e126ba97SEli Cohen 	if (!dev)
39599603b61dSJack Morgenstein 		return NULL;
3960e126ba97SEli Cohen 
39619603b61dSJack Morgenstein 	dev->mdev = mdev;
3962e126ba97SEli Cohen 
39630837e86aSMark Bloch 	dev->port = kcalloc(MLX5_CAP_GEN(mdev, num_ports), sizeof(*dev->port),
39640837e86aSMark Bloch 			    GFP_KERNEL);
39650837e86aSMark Bloch 	if (!dev->port)
39660837e86aSMark Bloch 		goto err_dealloc;
39670837e86aSMark Bloch 
3968fc24fc5eSAchiad Shochat 	rwlock_init(&dev->roce.netdev_lock);
3969e126ba97SEli Cohen 	err = get_port_caps(dev);
3970e126ba97SEli Cohen 	if (err)
39710837e86aSMark Bloch 		goto err_free_port;
3972e126ba97SEli Cohen 
39731b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(dev))
3974e126ba97SEli Cohen 		get_ext_port_caps(dev);
3975e126ba97SEli Cohen 
39764babcf97SAviv Heller 	if (!mlx5_lag_is_active(mdev))
39774babcf97SAviv Heller 		name = "mlx5_%d";
39784babcf97SAviv Heller 	else
39794babcf97SAviv Heller 		name = "mlx5_bond_%d";
39804babcf97SAviv Heller 
39814babcf97SAviv Heller 	strlcpy(dev->ib_dev.name, name, IB_DEVICE_NAME_MAX);
3982e126ba97SEli Cohen 	dev->ib_dev.owner		= THIS_MODULE;
3983e126ba97SEli Cohen 	dev->ib_dev.node_type		= RDMA_NODE_IB_CA;
3984c6790aa9SSagi Grimberg 	dev->ib_dev.local_dma_lkey	= 0 /* not supported for now */;
3985938fe83cSSaeed Mahameed 	dev->num_ports		= MLX5_CAP_GEN(mdev, num_ports);
3986e126ba97SEli Cohen 	dev->ib_dev.phys_port_cnt     = dev->num_ports;
3987233d05d2SSaeed Mahameed 	dev->ib_dev.num_comp_vectors    =
3988233d05d2SSaeed Mahameed 		dev->mdev->priv.eq_table.num_comp_vectors;
39899b0c289eSBart Van Assche 	dev->ib_dev.dev.parent		= &mdev->pdev->dev;
3990e126ba97SEli Cohen 
3991e126ba97SEli Cohen 	dev->ib_dev.uverbs_abi_ver	= MLX5_IB_UVERBS_ABI_VERSION;
3992e126ba97SEli Cohen 	dev->ib_dev.uverbs_cmd_mask	=
3993e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_GET_CONTEXT)		|
3994e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_DEVICE)	|
3995e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_PORT)		|
3996e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_ALLOC_PD)		|
3997e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DEALLOC_PD)		|
399841c450fdSMoni Shoua 		(1ull << IB_USER_VERBS_CMD_CREATE_AH)		|
399941c450fdSMoni Shoua 		(1ull << IB_USER_VERBS_CMD_DESTROY_AH)		|
4000e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_REG_MR)		|
400156e11d62SNoa Osherovich 		(1ull << IB_USER_VERBS_CMD_REREG_MR)		|
4002e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DEREG_MR)		|
4003e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL)	|
4004e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_CQ)		|
4005e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_RESIZE_CQ)		|
4006e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_CQ)		|
4007e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_QP)		|
4008e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_MODIFY_QP)		|
4009e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_QP)		|
4010e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_QP)		|
4011e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_ATTACH_MCAST)	|
4012e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DETACH_MCAST)	|
4013e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_SRQ)		|
4014e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_MODIFY_SRQ)		|
4015e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_SRQ)		|
4016e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_SRQ)		|
4017e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_XSRQ)		|
4018e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_OPEN_QP);
40191707cb4aSHaggai Eran 	dev->ib_dev.uverbs_ex_cmd_mask =
4020d4584ddfSMatan Barak 		(1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE)	|
4021d4584ddfSMatan Barak 		(1ull << IB_USER_VERBS_EX_CMD_CREATE_CQ)	|
40227d29f349SBodong Wang 		(1ull << IB_USER_VERBS_EX_CMD_CREATE_QP)	|
40237d29f349SBodong Wang 		(1ull << IB_USER_VERBS_EX_CMD_MODIFY_QP);
4024e126ba97SEli Cohen 
4025e126ba97SEli Cohen 	dev->ib_dev.query_device	= mlx5_ib_query_device;
4026e126ba97SEli Cohen 	dev->ib_dev.query_port		= mlx5_ib_query_port;
4027ebd61f68SAchiad Shochat 	dev->ib_dev.get_link_layer	= mlx5_ib_port_link_layer;
4028fc24fc5eSAchiad Shochat 	if (ll == IB_LINK_LAYER_ETHERNET)
4029fc24fc5eSAchiad Shochat 		dev->ib_dev.get_netdev	= mlx5_ib_get_netdev;
4030e126ba97SEli Cohen 	dev->ib_dev.query_gid		= mlx5_ib_query_gid;
40313cca2606SAchiad Shochat 	dev->ib_dev.add_gid		= mlx5_ib_add_gid;
40323cca2606SAchiad Shochat 	dev->ib_dev.del_gid		= mlx5_ib_del_gid;
4033e126ba97SEli Cohen 	dev->ib_dev.query_pkey		= mlx5_ib_query_pkey;
4034e126ba97SEli Cohen 	dev->ib_dev.modify_device	= mlx5_ib_modify_device;
4035e126ba97SEli Cohen 	dev->ib_dev.modify_port		= mlx5_ib_modify_port;
4036e126ba97SEli Cohen 	dev->ib_dev.alloc_ucontext	= mlx5_ib_alloc_ucontext;
4037e126ba97SEli Cohen 	dev->ib_dev.dealloc_ucontext	= mlx5_ib_dealloc_ucontext;
4038e126ba97SEli Cohen 	dev->ib_dev.mmap		= mlx5_ib_mmap;
4039e126ba97SEli Cohen 	dev->ib_dev.alloc_pd		= mlx5_ib_alloc_pd;
4040e126ba97SEli Cohen 	dev->ib_dev.dealloc_pd		= mlx5_ib_dealloc_pd;
4041e126ba97SEli Cohen 	dev->ib_dev.create_ah		= mlx5_ib_create_ah;
4042e126ba97SEli Cohen 	dev->ib_dev.query_ah		= mlx5_ib_query_ah;
4043e126ba97SEli Cohen 	dev->ib_dev.destroy_ah		= mlx5_ib_destroy_ah;
4044e126ba97SEli Cohen 	dev->ib_dev.create_srq		= mlx5_ib_create_srq;
4045e126ba97SEli Cohen 	dev->ib_dev.modify_srq		= mlx5_ib_modify_srq;
4046e126ba97SEli Cohen 	dev->ib_dev.query_srq		= mlx5_ib_query_srq;
4047e126ba97SEli Cohen 	dev->ib_dev.destroy_srq		= mlx5_ib_destroy_srq;
4048e126ba97SEli Cohen 	dev->ib_dev.post_srq_recv	= mlx5_ib_post_srq_recv;
4049e126ba97SEli Cohen 	dev->ib_dev.create_qp		= mlx5_ib_create_qp;
4050e126ba97SEli Cohen 	dev->ib_dev.modify_qp		= mlx5_ib_modify_qp;
4051e126ba97SEli Cohen 	dev->ib_dev.query_qp		= mlx5_ib_query_qp;
4052e126ba97SEli Cohen 	dev->ib_dev.destroy_qp		= mlx5_ib_destroy_qp;
4053e126ba97SEli Cohen 	dev->ib_dev.post_send		= mlx5_ib_post_send;
4054e126ba97SEli Cohen 	dev->ib_dev.post_recv		= mlx5_ib_post_recv;
4055e126ba97SEli Cohen 	dev->ib_dev.create_cq		= mlx5_ib_create_cq;
4056e126ba97SEli Cohen 	dev->ib_dev.modify_cq		= mlx5_ib_modify_cq;
4057e126ba97SEli Cohen 	dev->ib_dev.resize_cq		= mlx5_ib_resize_cq;
4058e126ba97SEli Cohen 	dev->ib_dev.destroy_cq		= mlx5_ib_destroy_cq;
4059e126ba97SEli Cohen 	dev->ib_dev.poll_cq		= mlx5_ib_poll_cq;
4060e126ba97SEli Cohen 	dev->ib_dev.req_notify_cq	= mlx5_ib_arm_cq;
4061e126ba97SEli Cohen 	dev->ib_dev.get_dma_mr		= mlx5_ib_get_dma_mr;
4062e126ba97SEli Cohen 	dev->ib_dev.reg_user_mr		= mlx5_ib_reg_user_mr;
406356e11d62SNoa Osherovich 	dev->ib_dev.rereg_user_mr	= mlx5_ib_rereg_user_mr;
4064e126ba97SEli Cohen 	dev->ib_dev.dereg_mr		= mlx5_ib_dereg_mr;
4065e126ba97SEli Cohen 	dev->ib_dev.attach_mcast	= mlx5_ib_mcg_attach;
4066e126ba97SEli Cohen 	dev->ib_dev.detach_mcast	= mlx5_ib_mcg_detach;
4067e126ba97SEli Cohen 	dev->ib_dev.process_mad		= mlx5_ib_process_mad;
40689bee178bSSagi Grimberg 	dev->ib_dev.alloc_mr		= mlx5_ib_alloc_mr;
40698a187ee5SSagi Grimberg 	dev->ib_dev.map_mr_sg		= mlx5_ib_map_mr_sg;
4070d5436ba0SSagi Grimberg 	dev->ib_dev.check_mr_status	= mlx5_ib_check_mr_status;
40717738613eSIra Weiny 	dev->ib_dev.get_port_immutable  = mlx5_port_immutable;
4072c7342823SIra Weiny 	dev->ib_dev.get_dev_fw_str      = get_dev_fw_str;
407340b24403SSagi Grimberg 	dev->ib_dev.get_vector_affinity	= mlx5_ib_get_vector_affinity;
40748e959601SNiranjana Vishwanathapura 	if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads))
4075693dfd5aSErez Shitrit 		dev->ib_dev.alloc_rdma_netdev	= mlx5_ib_alloc_rdma_netdev;
40768e959601SNiranjana Vishwanathapura 
4077eff901d3SEli Cohen 	if (mlx5_core_is_pf(mdev)) {
4078eff901d3SEli Cohen 		dev->ib_dev.get_vf_config	= mlx5_ib_get_vf_config;
4079eff901d3SEli Cohen 		dev->ib_dev.set_vf_link_state	= mlx5_ib_set_vf_link_state;
4080eff901d3SEli Cohen 		dev->ib_dev.get_vf_stats	= mlx5_ib_get_vf_stats;
4081eff901d3SEli Cohen 		dev->ib_dev.set_vf_guid		= mlx5_ib_set_vf_guid;
4082eff901d3SEli Cohen 	}
4083e126ba97SEli Cohen 
40847c2344c3SMaor Gottlieb 	dev->ib_dev.disassociate_ucontext = mlx5_ib_disassociate_ucontext;
40857c2344c3SMaor Gottlieb 
4086938fe83cSSaeed Mahameed 	mlx5_ib_internal_fill_odp_caps(dev);
40878cdd312cSHaggai Eran 
40886e8484c5SMax Gurtovoy 	dev->umr_fence = mlx5_get_umr_fence(MLX5_CAP_GEN(mdev, umr_fence));
40896e8484c5SMax Gurtovoy 
4090d2370e0aSMatan Barak 	if (MLX5_CAP_GEN(mdev, imaicl)) {
4091d2370e0aSMatan Barak 		dev->ib_dev.alloc_mw		= mlx5_ib_alloc_mw;
4092d2370e0aSMatan Barak 		dev->ib_dev.dealloc_mw		= mlx5_ib_dealloc_mw;
4093d2370e0aSMatan Barak 		dev->ib_dev.uverbs_cmd_mask |=
4094d2370e0aSMatan Barak 			(1ull << IB_USER_VERBS_CMD_ALLOC_MW)	|
4095d2370e0aSMatan Barak 			(1ull << IB_USER_VERBS_CMD_DEALLOC_MW);
4096d2370e0aSMatan Barak 	}
4097d2370e0aSMatan Barak 
40987c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt)) {
40990ad17a8fSMark Bloch 		dev->ib_dev.get_hw_stats	= mlx5_ib_get_hw_stats;
41000ad17a8fSMark Bloch 		dev->ib_dev.alloc_hw_stats	= mlx5_ib_alloc_hw_stats;
41010ad17a8fSMark Bloch 	}
41020ad17a8fSMark Bloch 
4103938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, xrc)) {
4104e126ba97SEli Cohen 		dev->ib_dev.alloc_xrcd = mlx5_ib_alloc_xrcd;
4105e126ba97SEli Cohen 		dev->ib_dev.dealloc_xrcd = mlx5_ib_dealloc_xrcd;
4106e126ba97SEli Cohen 		dev->ib_dev.uverbs_cmd_mask |=
4107e126ba97SEli Cohen 			(1ull << IB_USER_VERBS_CMD_OPEN_XRCD) |
4108e126ba97SEli Cohen 			(1ull << IB_USER_VERBS_CMD_CLOSE_XRCD);
4109e126ba97SEli Cohen 	}
4110e126ba97SEli Cohen 
4111038d2ef8SMaor Gottlieb 	dev->ib_dev.create_flow	= mlx5_ib_create_flow;
4112038d2ef8SMaor Gottlieb 	dev->ib_dev.destroy_flow = mlx5_ib_destroy_flow;
411381e30880SYishai Hadas 	dev->ib_dev.uverbs_ex_cmd_mask |=
411481e30880SYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_CREATE_FLOW) |
411581e30880SYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_DESTROY_FLOW);
411681e30880SYishai Hadas 
411781e30880SYishai Hadas 	if (mlx5_ib_port_link_layer(&dev->ib_dev, 1) ==
411881e30880SYishai Hadas 	    IB_LINK_LAYER_ETHERNET) {
411979b20a6cSYishai Hadas 		dev->ib_dev.create_wq	 = mlx5_ib_create_wq;
412079b20a6cSYishai Hadas 		dev->ib_dev.modify_wq	 = mlx5_ib_modify_wq;
412179b20a6cSYishai Hadas 		dev->ib_dev.destroy_wq	 = mlx5_ib_destroy_wq;
4122c5f90929SYishai Hadas 		dev->ib_dev.create_rwq_ind_table = mlx5_ib_create_rwq_ind_table;
4123c5f90929SYishai Hadas 		dev->ib_dev.destroy_rwq_ind_table = mlx5_ib_destroy_rwq_ind_table;
4124038d2ef8SMaor Gottlieb 		dev->ib_dev.uverbs_ex_cmd_mask |=
412579b20a6cSYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_CREATE_WQ) |
412679b20a6cSYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_MODIFY_WQ) |
4127c5f90929SYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_DESTROY_WQ) |
4128c5f90929SYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_CREATE_RWQ_IND_TBL) |
4129c5f90929SYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_DESTROY_RWQ_IND_TBL);
4130038d2ef8SMaor Gottlieb 	}
4131e126ba97SEli Cohen 	err = init_node_data(dev);
4132e126ba97SEli Cohen 	if (err)
413390be7c8aSMajd Dibbiny 		goto err_free_port;
4134e126ba97SEli Cohen 
4135038d2ef8SMaor Gottlieb 	mutex_init(&dev->flow_db.lock);
4136e126ba97SEli Cohen 	mutex_init(&dev->cap_mask_mutex);
413789ea94a7SMaor Gottlieb 	INIT_LIST_HEAD(&dev->qp_list);
413889ea94a7SMaor Gottlieb 	spin_lock_init(&dev->reset_flow_resource_lock);
4139e126ba97SEli Cohen 
4140fc24fc5eSAchiad Shochat 	if (ll == IB_LINK_LAYER_ETHERNET) {
414145f95acdSOr Gerlitz 		err = mlx5_enable_eth(dev);
4142e126ba97SEli Cohen 		if (err)
414390be7c8aSMajd Dibbiny 			goto err_free_port;
4144fd65f1b8SMoni Shoua 		dev->roce.last_port_state = IB_PORT_DOWN;
4145fc24fc5eSAchiad Shochat 	}
4146fc24fc5eSAchiad Shochat 
4147fc24fc5eSAchiad Shochat 	err = create_dev_resources(&dev->devr);
4148fc24fc5eSAchiad Shochat 	if (err)
414945f95acdSOr Gerlitz 		goto err_disable_eth;
4150e126ba97SEli Cohen 
41516aec21f6SHaggai Eran 	err = mlx5_ib_odp_init_one(dev);
4152281d1a92SWei Yongjun 	if (err)
4153e126ba97SEli Cohen 		goto err_rsrc;
4154e126ba97SEli Cohen 
415545bded2cSKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt)) {
4156e1f24a79SParav Pandit 		err = mlx5_ib_alloc_counters(dev);
41576aec21f6SHaggai Eran 		if (err)
41586aec21f6SHaggai Eran 			goto err_odp;
415945bded2cSKamal Heib 	}
41606aec21f6SHaggai Eran 
41614a2da0b8SParav Pandit 	err = mlx5_ib_init_cong_debugfs(dev);
41624a2da0b8SParav Pandit 	if (err)
41634a2da0b8SParav Pandit 		goto err_cnt;
41644a2da0b8SParav Pandit 
41655fe9dec0SEli Cohen 	dev->mdev->priv.uar = mlx5_get_uars_page(dev->mdev);
41665fe9dec0SEli Cohen 	if (!dev->mdev->priv.uar)
41674a2da0b8SParav Pandit 		goto err_cong;
41685fe9dec0SEli Cohen 
41695fe9dec0SEli Cohen 	err = mlx5_alloc_bfreg(dev->mdev, &dev->bfreg, false, false);
41705fe9dec0SEli Cohen 	if (err)
41715fe9dec0SEli Cohen 		goto err_uar_page;
41725fe9dec0SEli Cohen 
41735fe9dec0SEli Cohen 	err = mlx5_alloc_bfreg(dev->mdev, &dev->fp_bfreg, false, true);
41745fe9dec0SEli Cohen 	if (err)
41755fe9dec0SEli Cohen 		goto err_bfreg;
41765fe9dec0SEli Cohen 
41770837e86aSMark Bloch 	err = ib_register_device(&dev->ib_dev, NULL);
41780837e86aSMark Bloch 	if (err)
41795fe9dec0SEli Cohen 		goto err_fp_bfreg;
41800837e86aSMark Bloch 
4181e126ba97SEli Cohen 	err = create_umr_res(dev);
4182e126ba97SEli Cohen 	if (err)
4183e126ba97SEli Cohen 		goto err_dev;
4184e126ba97SEli Cohen 
418503404e8aSMaor Gottlieb 	init_delay_drop(dev);
418603404e8aSMaor Gottlieb 
4187e126ba97SEli Cohen 	for (i = 0; i < ARRAY_SIZE(mlx5_class_attributes); i++) {
4188281d1a92SWei Yongjun 		err = device_create_file(&dev->ib_dev.dev,
4189281d1a92SWei Yongjun 					 mlx5_class_attributes[i]);
4190281d1a92SWei Yongjun 		if (err)
419103404e8aSMaor Gottlieb 			goto err_delay_drop;
4192e126ba97SEli Cohen 	}
4193e126ba97SEli Cohen 
4194c85023e1SHuy Nguyen 	if ((MLX5_CAP_GEN(mdev, port_type) == MLX5_CAP_PORT_TYPE_ETH) &&
4195c85023e1SHuy Nguyen 	    MLX5_CAP_GEN(mdev, disable_local_lb))
4196c85023e1SHuy Nguyen 		mutex_init(&dev->lb_mutex);
4197c85023e1SHuy Nguyen 
4198e126ba97SEli Cohen 	dev->ib_active = true;
4199e126ba97SEli Cohen 
42009603b61dSJack Morgenstein 	return dev;
4201e126ba97SEli Cohen 
420203404e8aSMaor Gottlieb err_delay_drop:
420303404e8aSMaor Gottlieb 	cancel_delay_drop(dev);
4204e126ba97SEli Cohen 	destroy_umrc_res(dev);
4205e126ba97SEli Cohen 
4206e126ba97SEli Cohen err_dev:
4207e126ba97SEli Cohen 	ib_unregister_device(&dev->ib_dev);
4208e126ba97SEli Cohen 
42095fe9dec0SEli Cohen err_fp_bfreg:
42105fe9dec0SEli Cohen 	mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
42115fe9dec0SEli Cohen 
42125fe9dec0SEli Cohen err_bfreg:
42135fe9dec0SEli Cohen 	mlx5_free_bfreg(dev->mdev, &dev->bfreg);
42145fe9dec0SEli Cohen 
42155fe9dec0SEli Cohen err_uar_page:
42165fe9dec0SEli Cohen 	mlx5_put_uars_page(dev->mdev, dev->mdev->priv.uar);
42175fe9dec0SEli Cohen 
42184a2da0b8SParav Pandit err_cong:
4219e19cd282SParav Pandit 	mlx5_ib_cleanup_cong_debugfs(dev);
4220e19cd282SParav Pandit err_cnt:
422145bded2cSKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt))
4222e1f24a79SParav Pandit 		mlx5_ib_dealloc_counters(dev);
42230837e86aSMark Bloch 
42246aec21f6SHaggai Eran err_odp:
42256aec21f6SHaggai Eran 	mlx5_ib_odp_remove_one(dev);
42266aec21f6SHaggai Eran 
4227e126ba97SEli Cohen err_rsrc:
4228e126ba97SEli Cohen 	destroy_dev_resources(&dev->devr);
4229e126ba97SEli Cohen 
423045f95acdSOr Gerlitz err_disable_eth:
42315ec8c83eSAviv Heller 	if (ll == IB_LINK_LAYER_ETHERNET) {
423245f95acdSOr Gerlitz 		mlx5_disable_eth(dev);
4233d012f5d6SOr Gerlitz 		mlx5_remove_netdev_notifier(dev);
42345ec8c83eSAviv Heller 	}
4235fc24fc5eSAchiad Shochat 
42360837e86aSMark Bloch err_free_port:
42370837e86aSMark Bloch 	kfree(dev->port);
42380837e86aSMark Bloch 
42399603b61dSJack Morgenstein err_dealloc:
4240e126ba97SEli Cohen 	ib_dealloc_device((struct ib_device *)dev);
4241e126ba97SEli Cohen 
42429603b61dSJack Morgenstein 	return NULL;
4243e126ba97SEli Cohen }
4244e126ba97SEli Cohen 
42459603b61dSJack Morgenstein static void mlx5_ib_remove(struct mlx5_core_dev *mdev, void *context)
4246e126ba97SEli Cohen {
42479603b61dSJack Morgenstein 	struct mlx5_ib_dev *dev = context;
4248fc24fc5eSAchiad Shochat 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev, 1);
42496aec21f6SHaggai Eran 
425003404e8aSMaor Gottlieb 	cancel_delay_drop(dev);
4251d012f5d6SOr Gerlitz 	mlx5_remove_netdev_notifier(dev);
4252e126ba97SEli Cohen 	ib_unregister_device(&dev->ib_dev);
42535fe9dec0SEli Cohen 	mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
42545fe9dec0SEli Cohen 	mlx5_free_bfreg(dev->mdev, &dev->bfreg);
42555fe9dec0SEli Cohen 	mlx5_put_uars_page(dev->mdev, mdev->priv.uar);
42564a2da0b8SParav Pandit 	mlx5_ib_cleanup_cong_debugfs(dev);
425745bded2cSKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt))
4258e1f24a79SParav Pandit 		mlx5_ib_dealloc_counters(dev);
4259eefd56e5SEli Cohen 	destroy_umrc_res(dev);
42606aec21f6SHaggai Eran 	mlx5_ib_odp_remove_one(dev);
4261e126ba97SEli Cohen 	destroy_dev_resources(&dev->devr);
4262fc24fc5eSAchiad Shochat 	if (ll == IB_LINK_LAYER_ETHERNET)
426345f95acdSOr Gerlitz 		mlx5_disable_eth(dev);
42640837e86aSMark Bloch 	kfree(dev->port);
4265e126ba97SEli Cohen 	ib_dealloc_device(&dev->ib_dev);
4266e126ba97SEli Cohen }
4267e126ba97SEli Cohen 
42689603b61dSJack Morgenstein static struct mlx5_interface mlx5_ib_interface = {
42699603b61dSJack Morgenstein 	.add            = mlx5_ib_add,
42709603b61dSJack Morgenstein 	.remove         = mlx5_ib_remove,
42719603b61dSJack Morgenstein 	.event          = mlx5_ib_event,
4272d9aaed83SArtemy Kovalyov #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
4273d9aaed83SArtemy Kovalyov 	.pfault		= mlx5_ib_pfault,
4274d9aaed83SArtemy Kovalyov #endif
427564613d94SSaeed Mahameed 	.protocol	= MLX5_INTERFACE_PROTOCOL_IB,
4276e126ba97SEli Cohen };
4277e126ba97SEli Cohen 
4278e126ba97SEli Cohen static int __init mlx5_ib_init(void)
4279e126ba97SEli Cohen {
42806aec21f6SHaggai Eran 	int err;
42816aec21f6SHaggai Eran 
428281713d37SArtemy Kovalyov 	mlx5_ib_odp_init();
42839603b61dSJack Morgenstein 
42846aec21f6SHaggai Eran 	err = mlx5_register_interface(&mlx5_ib_interface);
42856aec21f6SHaggai Eran 
42866aec21f6SHaggai Eran 	return err;
4287e126ba97SEli Cohen }
4288e126ba97SEli Cohen 
4289e126ba97SEli Cohen static void __exit mlx5_ib_cleanup(void)
4290e126ba97SEli Cohen {
42919603b61dSJack Morgenstein 	mlx5_unregister_interface(&mlx5_ib_interface);
4292e126ba97SEli Cohen }
4293e126ba97SEli Cohen 
4294e126ba97SEli Cohen module_init(mlx5_ib_init);
4295e126ba97SEli Cohen module_exit(mlx5_ib_cleanup);
4296