xref: /openbmc/linux/drivers/infiniband/hw/mlx5/main.c (revision eb761894351d0372248f2636c213d7b822e8775f)
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 
7181b5daf11SMajd Dibbiny 	props->vendor_part_id	   = mdev->pdev->device;
7191b5daf11SMajd Dibbiny 	props->hw_ver		   = mdev->pdev->revision;
720e126ba97SEli Cohen 
721e126ba97SEli Cohen 	props->max_mr_size	   = ~0ull;
722e0238a6aSSagi Grimberg 	props->page_size_cap	   = ~(min_page_size - 1);
723938fe83cSSaeed Mahameed 	props->max_qp		   = 1 << MLX5_CAP_GEN(mdev, log_max_qp);
724938fe83cSSaeed Mahameed 	props->max_qp_wr	   = 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
725938fe83cSSaeed Mahameed 	max_rq_sg =  MLX5_CAP_GEN(mdev, max_wqe_sz_rq) /
726938fe83cSSaeed Mahameed 		     sizeof(struct mlx5_wqe_data_seg);
727288c01b7SEli Cohen 	max_sq_desc = min_t(int, MLX5_CAP_GEN(mdev, max_wqe_sz_sq), 512);
728288c01b7SEli Cohen 	max_sq_sg = (max_sq_desc - sizeof(struct mlx5_wqe_ctrl_seg) -
729288c01b7SEli Cohen 		     sizeof(struct mlx5_wqe_raddr_seg)) /
730e126ba97SEli Cohen 		sizeof(struct mlx5_wqe_data_seg);
731e126ba97SEli Cohen 	props->max_sge = min(max_rq_sg, max_sq_sg);
732986ef95eSSagi Grimberg 	props->max_sge_rd	   = MLX5_MAX_SGE_RD;
733938fe83cSSaeed Mahameed 	props->max_cq		   = 1 << MLX5_CAP_GEN(mdev, log_max_cq);
7349f177686SLeon Romanovsky 	props->max_cqe = (1 << MLX5_CAP_GEN(mdev, log_max_cq_sz)) - 1;
735938fe83cSSaeed Mahameed 	props->max_mr		   = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
736938fe83cSSaeed Mahameed 	props->max_pd		   = 1 << MLX5_CAP_GEN(mdev, log_max_pd);
737938fe83cSSaeed Mahameed 	props->max_qp_rd_atom	   = 1 << MLX5_CAP_GEN(mdev, log_max_ra_req_qp);
738938fe83cSSaeed Mahameed 	props->max_qp_init_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_res_qp);
739938fe83cSSaeed Mahameed 	props->max_srq		   = 1 << MLX5_CAP_GEN(mdev, log_max_srq);
740938fe83cSSaeed Mahameed 	props->max_srq_wr = (1 << MLX5_CAP_GEN(mdev, log_max_srq_sz)) - 1;
741938fe83cSSaeed Mahameed 	props->local_ca_ack_delay  = MLX5_CAP_GEN(mdev, local_ca_ack_delay);
742e126ba97SEli Cohen 	props->max_res_rd_atom	   = props->max_qp_rd_atom * props->max_qp;
743e126ba97SEli Cohen 	props->max_srq_sge	   = max_rq_sg - 1;
744911f4331SSagi Grimberg 	props->max_fast_reg_page_list_len =
745911f4331SSagi Grimberg 		1 << MLX5_CAP_GEN(mdev, log_max_klm_list_size);
746da7525d2SEran Ben Elisha 	get_atomic_caps(dev, props);
74781bea28fSEli Cohen 	props->masked_atomic_cap   = IB_ATOMIC_NONE;
748938fe83cSSaeed Mahameed 	props->max_mcast_grp	   = 1 << MLX5_CAP_GEN(mdev, log_max_mcg);
749938fe83cSSaeed Mahameed 	props->max_mcast_qp_attach = MLX5_CAP_GEN(mdev, max_qp_mcg);
750e126ba97SEli Cohen 	props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
751e126ba97SEli Cohen 					   props->max_mcast_grp;
752e126ba97SEli Cohen 	props->max_map_per_fmr = INT_MAX; /* no limit in ConnectIB */
75386695a65SMaor Gottlieb 	props->max_ah = INT_MAX;
7547c60bcbbSMatan Barak 	props->hca_core_clock = MLX5_CAP_GEN(mdev, device_frequency_khz);
7557c60bcbbSMatan Barak 	props->timestamp_mask = 0x7FFFFFFFFFFFFFFFULL;
756e126ba97SEli Cohen 
7578cdd312cSHaggai Eran #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
758938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, pg))
7598cdd312cSHaggai Eran 		props->device_cap_flags |= IB_DEVICE_ON_DEMAND_PAGING;
7608cdd312cSHaggai Eran 	props->odp_caps = dev->odp_caps;
7618cdd312cSHaggai Eran #endif
7628cdd312cSHaggai Eran 
763051f2630SLeon Romanovsky 	if (MLX5_CAP_GEN(mdev, cd))
764051f2630SLeon Romanovsky 		props->device_cap_flags |= IB_DEVICE_CROSS_CHANNEL;
765051f2630SLeon Romanovsky 
766eff901d3SEli Cohen 	if (!mlx5_core_is_pf(mdev))
767eff901d3SEli Cohen 		props->device_cap_flags |= IB_DEVICE_VIRTUAL_FUNCTION;
768eff901d3SEli Cohen 
76931f69a82SYishai Hadas 	if (mlx5_ib_port_link_layer(ibdev, 1) ==
77031f69a82SYishai Hadas 	    IB_LINK_LAYER_ETHERNET) {
77131f69a82SYishai Hadas 		props->rss_caps.max_rwq_indirection_tables =
77231f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt);
77331f69a82SYishai Hadas 		props->rss_caps.max_rwq_indirection_table_size =
77431f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt_size);
77531f69a82SYishai Hadas 		props->rss_caps.supported_qpts = 1 << IB_QPT_RAW_PACKET;
77631f69a82SYishai Hadas 		props->max_wq_type_rq =
77731f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rq);
77831f69a82SYishai Hadas 	}
77931f69a82SYishai Hadas 
780*eb761894SArtemy Kovalyov 	if (MLX5_CAP_GEN(mdev, tag_matching)) {
781*eb761894SArtemy Kovalyov 		props->xrq_caps.max_rndv_hdr_size = MLX5_TM_MAX_RNDV_MSG_SIZE;
782*eb761894SArtemy Kovalyov 		props->xrq_caps.max_num_tags =
783*eb761894SArtemy Kovalyov 			(1 << MLX5_CAP_GEN(mdev, log_tag_matching_list_sz)) - 1;
784*eb761894SArtemy Kovalyov 		props->xrq_caps.flags = IB_TM_CAP_RC;
785*eb761894SArtemy Kovalyov 		props->xrq_caps.max_ops =
786*eb761894SArtemy Kovalyov 			1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
787*eb761894SArtemy Kovalyov 		props->xrq_caps.max_sge = MLX5_TM_MAX_SGE;
788*eb761894SArtemy Kovalyov 	}
789*eb761894SArtemy Kovalyov 
7907e43a2a5SBodong Wang 	if (field_avail(typeof(resp), cqe_comp_caps, uhw->outlen)) {
7917e43a2a5SBodong Wang 		resp.cqe_comp_caps.max_num =
7927e43a2a5SBodong Wang 			MLX5_CAP_GEN(dev->mdev, cqe_compression) ?
7937e43a2a5SBodong Wang 			MLX5_CAP_GEN(dev->mdev, cqe_compression_max_num) : 0;
7947e43a2a5SBodong Wang 		resp.cqe_comp_caps.supported_format =
7957e43a2a5SBodong Wang 			MLX5_IB_CQE_RES_FORMAT_HASH |
7967e43a2a5SBodong Wang 			MLX5_IB_CQE_RES_FORMAT_CSUM;
7977e43a2a5SBodong Wang 		resp.response_length += sizeof(resp.cqe_comp_caps);
7987e43a2a5SBodong Wang 	}
7997e43a2a5SBodong Wang 
800d949167dSBodong Wang 	if (field_avail(typeof(resp), packet_pacing_caps, uhw->outlen)) {
801d949167dSBodong Wang 		if (MLX5_CAP_QOS(mdev, packet_pacing) &&
802d949167dSBodong Wang 		    MLX5_CAP_GEN(mdev, qos)) {
803d949167dSBodong Wang 			resp.packet_pacing_caps.qp_rate_limit_max =
804d949167dSBodong Wang 				MLX5_CAP_QOS(mdev, packet_pacing_max_rate);
805d949167dSBodong Wang 			resp.packet_pacing_caps.qp_rate_limit_min =
806d949167dSBodong Wang 				MLX5_CAP_QOS(mdev, packet_pacing_min_rate);
807d949167dSBodong Wang 			resp.packet_pacing_caps.supported_qpts |=
808d949167dSBodong Wang 				1 << IB_QPT_RAW_PACKET;
809d949167dSBodong Wang 		}
810d949167dSBodong Wang 		resp.response_length += sizeof(resp.packet_pacing_caps);
811d949167dSBodong Wang 	}
812d949167dSBodong Wang 
8139f885201SLeon Romanovsky 	if (field_avail(typeof(resp), mlx5_ib_support_multi_pkt_send_wqes,
8149f885201SLeon Romanovsky 			uhw->outlen)) {
815795b609cSBodong Wang 		if (MLX5_CAP_ETH(mdev, multi_pkt_send_wqe))
8169f885201SLeon Romanovsky 			resp.mlx5_ib_support_multi_pkt_send_wqes =
817795b609cSBodong Wang 				MLX5_IB_ALLOW_MPW;
818050da902SBodong Wang 
819050da902SBodong Wang 		if (MLX5_CAP_ETH(mdev, enhanced_multi_pkt_send_wqe))
820050da902SBodong Wang 			resp.mlx5_ib_support_multi_pkt_send_wqes |=
821050da902SBodong Wang 				MLX5_IB_SUPPORT_EMPW;
822050da902SBodong Wang 
8239f885201SLeon Romanovsky 		resp.response_length +=
8249f885201SLeon Romanovsky 			sizeof(resp.mlx5_ib_support_multi_pkt_send_wqes);
8259f885201SLeon Romanovsky 	}
8269f885201SLeon Romanovsky 
8279f885201SLeon Romanovsky 	if (field_avail(typeof(resp), reserved, uhw->outlen))
8289f885201SLeon Romanovsky 		resp.response_length += sizeof(resp.reserved);
8299f885201SLeon Romanovsky 
83096dc3fc5SNoa Osherovich 	if (field_avail(typeof(resp), sw_parsing_caps,
83196dc3fc5SNoa Osherovich 			uhw->outlen)) {
83296dc3fc5SNoa Osherovich 		resp.response_length += sizeof(resp.sw_parsing_caps);
83396dc3fc5SNoa Osherovich 		if (MLX5_CAP_ETH(mdev, swp)) {
83496dc3fc5SNoa Osherovich 			resp.sw_parsing_caps.sw_parsing_offloads |=
83596dc3fc5SNoa Osherovich 				MLX5_IB_SW_PARSING;
83696dc3fc5SNoa Osherovich 
83796dc3fc5SNoa Osherovich 			if (MLX5_CAP_ETH(mdev, swp_csum))
83896dc3fc5SNoa Osherovich 				resp.sw_parsing_caps.sw_parsing_offloads |=
83996dc3fc5SNoa Osherovich 					MLX5_IB_SW_PARSING_CSUM;
84096dc3fc5SNoa Osherovich 
84196dc3fc5SNoa Osherovich 			if (MLX5_CAP_ETH(mdev, swp_lso))
84296dc3fc5SNoa Osherovich 				resp.sw_parsing_caps.sw_parsing_offloads |=
84396dc3fc5SNoa Osherovich 					MLX5_IB_SW_PARSING_LSO;
84496dc3fc5SNoa Osherovich 
84596dc3fc5SNoa Osherovich 			if (resp.sw_parsing_caps.sw_parsing_offloads)
84696dc3fc5SNoa Osherovich 				resp.sw_parsing_caps.supported_qpts =
84796dc3fc5SNoa Osherovich 					BIT(IB_QPT_RAW_PACKET);
84896dc3fc5SNoa Osherovich 		}
84996dc3fc5SNoa Osherovich 	}
85096dc3fc5SNoa Osherovich 
851402ca536SBodong Wang 	if (uhw->outlen) {
852402ca536SBodong Wang 		err = ib_copy_to_udata(uhw, &resp, resp.response_length);
853402ca536SBodong Wang 
854402ca536SBodong Wang 		if (err)
855402ca536SBodong Wang 			return err;
856402ca536SBodong Wang 	}
857402ca536SBodong Wang 
8581b5daf11SMajd Dibbiny 	return 0;
8591b5daf11SMajd Dibbiny }
860e126ba97SEli Cohen 
8611b5daf11SMajd Dibbiny enum mlx5_ib_width {
8621b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_1X	= 1 << 0,
8631b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_2X	= 1 << 1,
8641b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_4X	= 1 << 2,
8651b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_8X	= 1 << 3,
8661b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_12X	= 1 << 4
8671b5daf11SMajd Dibbiny };
8681b5daf11SMajd Dibbiny 
8691b5daf11SMajd Dibbiny static int translate_active_width(struct ib_device *ibdev, u8 active_width,
8701b5daf11SMajd Dibbiny 				  u8 *ib_width)
8711b5daf11SMajd Dibbiny {
8721b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
8731b5daf11SMajd Dibbiny 	int err = 0;
8741b5daf11SMajd Dibbiny 
8751b5daf11SMajd Dibbiny 	if (active_width & MLX5_IB_WIDTH_1X) {
8761b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_1X;
8771b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_2X) {
8781b5daf11SMajd Dibbiny 		mlx5_ib_dbg(dev, "active_width %d is not supported by IB spec\n",
8791b5daf11SMajd Dibbiny 			    (int)active_width);
8801b5daf11SMajd Dibbiny 		err = -EINVAL;
8811b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_4X) {
8821b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_4X;
8831b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_8X) {
8841b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_8X;
8851b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_12X) {
8861b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_12X;
8871b5daf11SMajd Dibbiny 	} else {
8881b5daf11SMajd Dibbiny 		mlx5_ib_dbg(dev, "Invalid active_width %d\n",
8891b5daf11SMajd Dibbiny 			    (int)active_width);
8901b5daf11SMajd Dibbiny 		err = -EINVAL;
8911b5daf11SMajd Dibbiny 	}
8921b5daf11SMajd Dibbiny 
8931b5daf11SMajd Dibbiny 	return err;
8941b5daf11SMajd Dibbiny }
8951b5daf11SMajd Dibbiny 
8961b5daf11SMajd Dibbiny static int mlx5_mtu_to_ib_mtu(int mtu)
8971b5daf11SMajd Dibbiny {
8981b5daf11SMajd Dibbiny 	switch (mtu) {
8991b5daf11SMajd Dibbiny 	case 256: return 1;
9001b5daf11SMajd Dibbiny 	case 512: return 2;
9011b5daf11SMajd Dibbiny 	case 1024: return 3;
9021b5daf11SMajd Dibbiny 	case 2048: return 4;
9031b5daf11SMajd Dibbiny 	case 4096: return 5;
9041b5daf11SMajd Dibbiny 	default:
9051b5daf11SMajd Dibbiny 		pr_warn("invalid mtu\n");
9061b5daf11SMajd Dibbiny 		return -1;
9071b5daf11SMajd Dibbiny 	}
9081b5daf11SMajd Dibbiny }
9091b5daf11SMajd Dibbiny 
9101b5daf11SMajd Dibbiny enum ib_max_vl_num {
9111b5daf11SMajd Dibbiny 	__IB_MAX_VL_0		= 1,
9121b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_1		= 2,
9131b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_3		= 3,
9141b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_7		= 4,
9151b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_14	= 5,
9161b5daf11SMajd Dibbiny };
9171b5daf11SMajd Dibbiny 
9181b5daf11SMajd Dibbiny enum mlx5_vl_hw_cap {
9191b5daf11SMajd Dibbiny 	MLX5_VL_HW_0	= 1,
9201b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_1	= 2,
9211b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_2	= 3,
9221b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_3	= 4,
9231b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_4	= 5,
9241b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_5	= 6,
9251b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_6	= 7,
9261b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_7	= 8,
9271b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_14	= 15
9281b5daf11SMajd Dibbiny };
9291b5daf11SMajd Dibbiny 
9301b5daf11SMajd Dibbiny static int translate_max_vl_num(struct ib_device *ibdev, u8 vl_hw_cap,
9311b5daf11SMajd Dibbiny 				u8 *max_vl_num)
9321b5daf11SMajd Dibbiny {
9331b5daf11SMajd Dibbiny 	switch (vl_hw_cap) {
9341b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0:
9351b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0;
9361b5daf11SMajd Dibbiny 		break;
9371b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_1:
9381b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_1;
9391b5daf11SMajd Dibbiny 		break;
9401b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_3:
9411b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_3;
9421b5daf11SMajd Dibbiny 		break;
9431b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_7:
9441b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_7;
9451b5daf11SMajd Dibbiny 		break;
9461b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_14:
9471b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_14;
9481b5daf11SMajd Dibbiny 		break;
9491b5daf11SMajd Dibbiny 
9501b5daf11SMajd Dibbiny 	default:
9511b5daf11SMajd Dibbiny 		return -EINVAL;
9521b5daf11SMajd Dibbiny 	}
9531b5daf11SMajd Dibbiny 
9541b5daf11SMajd Dibbiny 	return 0;
9551b5daf11SMajd Dibbiny }
9561b5daf11SMajd Dibbiny 
9571b5daf11SMajd Dibbiny static int mlx5_query_hca_port(struct ib_device *ibdev, u8 port,
9581b5daf11SMajd Dibbiny 			       struct ib_port_attr *props)
9591b5daf11SMajd Dibbiny {
9601b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
9611b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
9621b5daf11SMajd Dibbiny 	struct mlx5_hca_vport_context *rep;
963046339eaSSaeed Mahameed 	u16 max_mtu;
964046339eaSSaeed Mahameed 	u16 oper_mtu;
9651b5daf11SMajd Dibbiny 	int err;
9661b5daf11SMajd Dibbiny 	u8 ib_link_width_oper;
9671b5daf11SMajd Dibbiny 	u8 vl_hw_cap;
9681b5daf11SMajd Dibbiny 
9691b5daf11SMajd Dibbiny 	rep = kzalloc(sizeof(*rep), GFP_KERNEL);
9701b5daf11SMajd Dibbiny 	if (!rep) {
9711b5daf11SMajd Dibbiny 		err = -ENOMEM;
9721b5daf11SMajd Dibbiny 		goto out;
9731b5daf11SMajd Dibbiny 	}
9741b5daf11SMajd Dibbiny 
975c4550c63SOr Gerlitz 	/* props being zeroed by the caller, avoid zeroing it here */
9761b5daf11SMajd Dibbiny 
9771b5daf11SMajd Dibbiny 	err = mlx5_query_hca_vport_context(mdev, 0, port, 0, rep);
9781b5daf11SMajd Dibbiny 	if (err)
9791b5daf11SMajd Dibbiny 		goto out;
9801b5daf11SMajd Dibbiny 
9811b5daf11SMajd Dibbiny 	props->lid		= rep->lid;
9821b5daf11SMajd Dibbiny 	props->lmc		= rep->lmc;
9831b5daf11SMajd Dibbiny 	props->sm_lid		= rep->sm_lid;
9841b5daf11SMajd Dibbiny 	props->sm_sl		= rep->sm_sl;
9851b5daf11SMajd Dibbiny 	props->state		= rep->vport_state;
9861b5daf11SMajd Dibbiny 	props->phys_state	= rep->port_physical_state;
9871b5daf11SMajd Dibbiny 	props->port_cap_flags	= rep->cap_mask1;
9881b5daf11SMajd Dibbiny 	props->gid_tbl_len	= mlx5_get_gid_table_len(MLX5_CAP_GEN(mdev, gid_table_size));
9891b5daf11SMajd Dibbiny 	props->max_msg_sz	= 1 << MLX5_CAP_GEN(mdev, log_max_msg);
9901b5daf11SMajd Dibbiny 	props->pkey_tbl_len	= mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, pkey_table_size));
9911b5daf11SMajd Dibbiny 	props->bad_pkey_cntr	= rep->pkey_violation_counter;
9921b5daf11SMajd Dibbiny 	props->qkey_viol_cntr	= rep->qkey_violation_counter;
9931b5daf11SMajd Dibbiny 	props->subnet_timeout	= rep->subnet_timeout;
9941b5daf11SMajd Dibbiny 	props->init_type_reply	= rep->init_type_reply;
995eff901d3SEli Cohen 	props->grh_required	= rep->grh_required;
9961b5daf11SMajd Dibbiny 
9971b5daf11SMajd Dibbiny 	err = mlx5_query_port_link_width_oper(mdev, &ib_link_width_oper, port);
9981b5daf11SMajd Dibbiny 	if (err)
9991b5daf11SMajd Dibbiny 		goto out;
10001b5daf11SMajd Dibbiny 
10011b5daf11SMajd Dibbiny 	err = translate_active_width(ibdev, ib_link_width_oper,
10021b5daf11SMajd Dibbiny 				     &props->active_width);
10031b5daf11SMajd Dibbiny 	if (err)
10041b5daf11SMajd Dibbiny 		goto out;
1005d5beb7f2SNoa Osherovich 	err = mlx5_query_port_ib_proto_oper(mdev, &props->active_speed, port);
10061b5daf11SMajd Dibbiny 	if (err)
10071b5daf11SMajd Dibbiny 		goto out;
10081b5daf11SMajd Dibbiny 
1009facc9699SSaeed Mahameed 	mlx5_query_port_max_mtu(mdev, &max_mtu, port);
10101b5daf11SMajd Dibbiny 
10111b5daf11SMajd Dibbiny 	props->max_mtu = mlx5_mtu_to_ib_mtu(max_mtu);
10121b5daf11SMajd Dibbiny 
1013facc9699SSaeed Mahameed 	mlx5_query_port_oper_mtu(mdev, &oper_mtu, port);
10141b5daf11SMajd Dibbiny 
10151b5daf11SMajd Dibbiny 	props->active_mtu = mlx5_mtu_to_ib_mtu(oper_mtu);
10161b5daf11SMajd Dibbiny 
10171b5daf11SMajd Dibbiny 	err = mlx5_query_port_vl_hw_cap(mdev, &vl_hw_cap, port);
10181b5daf11SMajd Dibbiny 	if (err)
10191b5daf11SMajd Dibbiny 		goto out;
10201b5daf11SMajd Dibbiny 
10211b5daf11SMajd Dibbiny 	err = translate_max_vl_num(ibdev, vl_hw_cap,
10221b5daf11SMajd Dibbiny 				   &props->max_vl_num);
10231b5daf11SMajd Dibbiny out:
10241b5daf11SMajd Dibbiny 	kfree(rep);
1025e126ba97SEli Cohen 	return err;
1026e126ba97SEli Cohen }
1027e126ba97SEli Cohen 
1028e126ba97SEli Cohen int mlx5_ib_query_port(struct ib_device *ibdev, u8 port,
1029e126ba97SEli Cohen 		       struct ib_port_attr *props)
1030e126ba97SEli Cohen {
1031095b0927SIlan Tayari 	unsigned int count;
1032095b0927SIlan Tayari 	int ret;
1033095b0927SIlan Tayari 
10341b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
10351b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
1036095b0927SIlan Tayari 		ret = mlx5_query_mad_ifc_port(ibdev, port, props);
1037095b0927SIlan Tayari 		break;
1038e126ba97SEli Cohen 
10391b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
1040095b0927SIlan Tayari 		ret = mlx5_query_hca_port(ibdev, port, props);
1041095b0927SIlan Tayari 		break;
10421b5daf11SMajd Dibbiny 
10433f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
1044095b0927SIlan Tayari 		ret = mlx5_query_port_roce(ibdev, port, props);
1045095b0927SIlan Tayari 		break;
10463f89a643SAchiad Shochat 
10471b5daf11SMajd Dibbiny 	default:
1048095b0927SIlan Tayari 		ret = -EINVAL;
1049e126ba97SEli Cohen 	}
1050095b0927SIlan Tayari 
1051095b0927SIlan Tayari 	if (!ret && props) {
1052095b0927SIlan Tayari 		count = mlx5_core_reserved_gids_count(to_mdev(ibdev)->mdev);
1053095b0927SIlan Tayari 		props->gid_tbl_len -= count;
1054095b0927SIlan Tayari 	}
1055095b0927SIlan Tayari 	return ret;
1056e126ba97SEli Cohen }
1057e126ba97SEli Cohen 
1058e126ba97SEli Cohen static int mlx5_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
1059e126ba97SEli Cohen 			     union ib_gid *gid)
1060e126ba97SEli Cohen {
10611b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
10621b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
1063e126ba97SEli Cohen 
10641b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
10651b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
10661b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_gids(ibdev, port, index, gid);
1067e126ba97SEli Cohen 
10681b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
10691b5daf11SMajd Dibbiny 		return mlx5_query_hca_vport_gid(mdev, 0, port, 0, index, gid);
1070e126ba97SEli Cohen 
10711b5daf11SMajd Dibbiny 	default:
10721b5daf11SMajd Dibbiny 		return -EINVAL;
10731b5daf11SMajd Dibbiny 	}
1074e126ba97SEli Cohen 
1075e126ba97SEli Cohen }
1076e126ba97SEli Cohen 
1077e126ba97SEli Cohen static int mlx5_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
1078e126ba97SEli Cohen 			      u16 *pkey)
1079e126ba97SEli Cohen {
10801b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
10811b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
1082e126ba97SEli Cohen 
10831b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
10841b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
10851b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_pkey(ibdev, port, index, pkey);
1086e126ba97SEli Cohen 
10871b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
10881b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
10891b5daf11SMajd Dibbiny 		return mlx5_query_hca_vport_pkey(mdev, 0, port,  0, index,
10901b5daf11SMajd Dibbiny 						 pkey);
10911b5daf11SMajd Dibbiny 	default:
10921b5daf11SMajd Dibbiny 		return -EINVAL;
1093e126ba97SEli Cohen 	}
10941b5daf11SMajd Dibbiny }
1095e126ba97SEli Cohen 
1096e126ba97SEli Cohen static int mlx5_ib_modify_device(struct ib_device *ibdev, int mask,
1097e126ba97SEli Cohen 				 struct ib_device_modify *props)
1098e126ba97SEli Cohen {
1099e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1100e126ba97SEli Cohen 	struct mlx5_reg_node_desc in;
1101e126ba97SEli Cohen 	struct mlx5_reg_node_desc out;
1102e126ba97SEli Cohen 	int err;
1103e126ba97SEli Cohen 
1104e126ba97SEli Cohen 	if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
1105e126ba97SEli Cohen 		return -EOPNOTSUPP;
1106e126ba97SEli Cohen 
1107e126ba97SEli Cohen 	if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
1108e126ba97SEli Cohen 		return 0;
1109e126ba97SEli Cohen 
1110e126ba97SEli Cohen 	/*
1111e126ba97SEli Cohen 	 * If possible, pass node desc to FW, so it can generate
1112e126ba97SEli Cohen 	 * a 144 trap.  If cmd fails, just ignore.
1113e126ba97SEli Cohen 	 */
1114bd99fdeaSYuval Shaia 	memcpy(&in, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
11159603b61dSJack Morgenstein 	err = mlx5_core_access_reg(dev->mdev, &in, sizeof(in), &out,
1116e126ba97SEli Cohen 				   sizeof(out), MLX5_REG_NODE_DESC, 0, 1);
1117e126ba97SEli Cohen 	if (err)
1118e126ba97SEli Cohen 		return err;
1119e126ba97SEli Cohen 
1120bd99fdeaSYuval Shaia 	memcpy(ibdev->node_desc, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
1121e126ba97SEli Cohen 
1122e126ba97SEli Cohen 	return err;
1123e126ba97SEli Cohen }
1124e126ba97SEli Cohen 
1125cdbe33d0SEli Cohen static int set_port_caps_atomic(struct mlx5_ib_dev *dev, u8 port_num, u32 mask,
1126cdbe33d0SEli Cohen 				u32 value)
1127cdbe33d0SEli Cohen {
1128cdbe33d0SEli Cohen 	struct mlx5_hca_vport_context ctx = {};
1129cdbe33d0SEli Cohen 	int err;
1130cdbe33d0SEli Cohen 
1131cdbe33d0SEli Cohen 	err = mlx5_query_hca_vport_context(dev->mdev, 0,
1132cdbe33d0SEli Cohen 					   port_num, 0, &ctx);
1133cdbe33d0SEli Cohen 	if (err)
1134cdbe33d0SEli Cohen 		return err;
1135cdbe33d0SEli Cohen 
1136cdbe33d0SEli Cohen 	if (~ctx.cap_mask1_perm & mask) {
1137cdbe33d0SEli Cohen 		mlx5_ib_warn(dev, "trying to change bitmask 0x%X but change supported 0x%X\n",
1138cdbe33d0SEli Cohen 			     mask, ctx.cap_mask1_perm);
1139cdbe33d0SEli Cohen 		return -EINVAL;
1140cdbe33d0SEli Cohen 	}
1141cdbe33d0SEli Cohen 
1142cdbe33d0SEli Cohen 	ctx.cap_mask1 = value;
1143cdbe33d0SEli Cohen 	ctx.cap_mask1_perm = mask;
1144cdbe33d0SEli Cohen 	err = mlx5_core_modify_hca_vport_context(dev->mdev, 0,
1145cdbe33d0SEli Cohen 						 port_num, 0, &ctx);
1146cdbe33d0SEli Cohen 
1147cdbe33d0SEli Cohen 	return err;
1148cdbe33d0SEli Cohen }
1149cdbe33d0SEli Cohen 
1150e126ba97SEli Cohen static int mlx5_ib_modify_port(struct ib_device *ibdev, u8 port, int mask,
1151e126ba97SEli Cohen 			       struct ib_port_modify *props)
1152e126ba97SEli Cohen {
1153e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1154e126ba97SEli Cohen 	struct ib_port_attr attr;
1155e126ba97SEli Cohen 	u32 tmp;
1156e126ba97SEli Cohen 	int err;
1157cdbe33d0SEli Cohen 	u32 change_mask;
1158cdbe33d0SEli Cohen 	u32 value;
1159cdbe33d0SEli Cohen 	bool is_ib = (mlx5_ib_port_link_layer(ibdev, port) ==
1160cdbe33d0SEli Cohen 		      IB_LINK_LAYER_INFINIBAND);
1161cdbe33d0SEli Cohen 
1162ec255879SMajd Dibbiny 	/* CM layer calls ib_modify_port() regardless of the link layer. For
1163ec255879SMajd Dibbiny 	 * Ethernet ports, qkey violation and Port capabilities are meaningless.
1164ec255879SMajd Dibbiny 	 */
1165ec255879SMajd Dibbiny 	if (!is_ib)
1166ec255879SMajd Dibbiny 		return 0;
1167ec255879SMajd Dibbiny 
1168cdbe33d0SEli Cohen 	if (MLX5_CAP_GEN(dev->mdev, ib_virt) && is_ib) {
1169cdbe33d0SEli Cohen 		change_mask = props->clr_port_cap_mask | props->set_port_cap_mask;
1170cdbe33d0SEli Cohen 		value = ~props->clr_port_cap_mask | props->set_port_cap_mask;
1171cdbe33d0SEli Cohen 		return set_port_caps_atomic(dev, port, change_mask, value);
1172cdbe33d0SEli Cohen 	}
1173e126ba97SEli Cohen 
1174e126ba97SEli Cohen 	mutex_lock(&dev->cap_mask_mutex);
1175e126ba97SEli Cohen 
1176c4550c63SOr Gerlitz 	err = ib_query_port(ibdev, port, &attr);
1177e126ba97SEli Cohen 	if (err)
1178e126ba97SEli Cohen 		goto out;
1179e126ba97SEli Cohen 
1180e126ba97SEli Cohen 	tmp = (attr.port_cap_flags | props->set_port_cap_mask) &
1181e126ba97SEli Cohen 		~props->clr_port_cap_mask;
1182e126ba97SEli Cohen 
11839603b61dSJack Morgenstein 	err = mlx5_set_port_caps(dev->mdev, port, tmp);
1184e126ba97SEli Cohen 
1185e126ba97SEli Cohen out:
1186e126ba97SEli Cohen 	mutex_unlock(&dev->cap_mask_mutex);
1187e126ba97SEli Cohen 	return err;
1188e126ba97SEli Cohen }
1189e126ba97SEli Cohen 
119030aa60b3SEli Cohen static void print_lib_caps(struct mlx5_ib_dev *dev, u64 caps)
119130aa60b3SEli Cohen {
119230aa60b3SEli Cohen 	mlx5_ib_dbg(dev, "MLX5_LIB_CAP_4K_UAR = %s\n",
119330aa60b3SEli Cohen 		    caps & MLX5_LIB_CAP_4K_UAR ? "y" : "n");
119430aa60b3SEli Cohen }
119530aa60b3SEli Cohen 
1196b037c29aSEli Cohen static int calc_total_bfregs(struct mlx5_ib_dev *dev, bool lib_uar_4k,
1197b037c29aSEli Cohen 			     struct mlx5_ib_alloc_ucontext_req_v2 *req,
1198b037c29aSEli Cohen 			     u32 *num_sys_pages)
1199b037c29aSEli Cohen {
1200b037c29aSEli Cohen 	int uars_per_sys_page;
1201b037c29aSEli Cohen 	int bfregs_per_sys_page;
1202b037c29aSEli Cohen 	int ref_bfregs = req->total_num_bfregs;
1203b037c29aSEli Cohen 
1204b037c29aSEli Cohen 	if (req->total_num_bfregs == 0)
1205b037c29aSEli Cohen 		return -EINVAL;
1206b037c29aSEli Cohen 
1207b037c29aSEli Cohen 	BUILD_BUG_ON(MLX5_MAX_BFREGS % MLX5_NON_FP_BFREGS_IN_PAGE);
1208b037c29aSEli Cohen 	BUILD_BUG_ON(MLX5_MAX_BFREGS < MLX5_NON_FP_BFREGS_IN_PAGE);
1209b037c29aSEli Cohen 
1210b037c29aSEli Cohen 	if (req->total_num_bfregs > MLX5_MAX_BFREGS)
1211b037c29aSEli Cohen 		return -ENOMEM;
1212b037c29aSEli Cohen 
1213b037c29aSEli Cohen 	uars_per_sys_page = get_uars_per_sys_page(dev, lib_uar_4k);
1214b037c29aSEli Cohen 	bfregs_per_sys_page = uars_per_sys_page * MLX5_NON_FP_BFREGS_PER_UAR;
1215b037c29aSEli Cohen 	req->total_num_bfregs = ALIGN(req->total_num_bfregs, bfregs_per_sys_page);
1216b037c29aSEli Cohen 	*num_sys_pages = req->total_num_bfregs / bfregs_per_sys_page;
1217b037c29aSEli Cohen 
1218b037c29aSEli Cohen 	if (req->num_low_latency_bfregs > req->total_num_bfregs - 1)
1219b037c29aSEli Cohen 		return -EINVAL;
1220b037c29aSEli Cohen 
12219c2d33d4SColin 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",
1222b037c29aSEli Cohen 		    MLX5_CAP_GEN(dev->mdev, uar_4k) ? "yes" : "no",
1223b037c29aSEli Cohen 		    lib_uar_4k ? "yes" : "no", ref_bfregs,
1224b037c29aSEli Cohen 		    req->total_num_bfregs, *num_sys_pages);
1225b037c29aSEli Cohen 
1226b037c29aSEli Cohen 	return 0;
1227b037c29aSEli Cohen }
1228b037c29aSEli Cohen 
1229b037c29aSEli Cohen static int allocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
1230b037c29aSEli Cohen {
1231b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1232b037c29aSEli Cohen 	int err;
1233b037c29aSEli Cohen 	int i;
1234b037c29aSEli Cohen 
1235b037c29aSEli Cohen 	bfregi = &context->bfregi;
1236b037c29aSEli Cohen 	for (i = 0; i < bfregi->num_sys_pages; i++) {
1237b037c29aSEli Cohen 		err = mlx5_cmd_alloc_uar(dev->mdev, &bfregi->sys_pages[i]);
1238b037c29aSEli Cohen 		if (err)
1239b037c29aSEli Cohen 			goto error;
1240b037c29aSEli Cohen 
1241b037c29aSEli Cohen 		mlx5_ib_dbg(dev, "allocated uar %d\n", bfregi->sys_pages[i]);
1242b037c29aSEli Cohen 	}
1243b037c29aSEli Cohen 	return 0;
1244b037c29aSEli Cohen 
1245b037c29aSEli Cohen error:
1246b037c29aSEli Cohen 	for (--i; i >= 0; i--)
1247b037c29aSEli Cohen 		if (mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]))
1248b037c29aSEli Cohen 			mlx5_ib_warn(dev, "failed to free uar %d\n", i);
1249b037c29aSEli Cohen 
1250b037c29aSEli Cohen 	return err;
1251b037c29aSEli Cohen }
1252b037c29aSEli Cohen 
1253b037c29aSEli Cohen static int deallocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
1254b037c29aSEli Cohen {
1255b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1256b037c29aSEli Cohen 	int err;
1257b037c29aSEli Cohen 	int i;
1258b037c29aSEli Cohen 
1259b037c29aSEli Cohen 	bfregi = &context->bfregi;
1260b037c29aSEli Cohen 	for (i = 0; i < bfregi->num_sys_pages; i++) {
1261b037c29aSEli Cohen 		err = mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]);
1262b037c29aSEli Cohen 		if (err) {
1263b037c29aSEli Cohen 			mlx5_ib_warn(dev, "failed to free uar %d\n", i);
1264b037c29aSEli Cohen 			return err;
1265b037c29aSEli Cohen 		}
1266b037c29aSEli Cohen 	}
1267b037c29aSEli Cohen 	return 0;
1268b037c29aSEli Cohen }
1269b037c29aSEli Cohen 
1270c85023e1SHuy Nguyen static int mlx5_ib_alloc_transport_domain(struct mlx5_ib_dev *dev, u32 *tdn)
1271c85023e1SHuy Nguyen {
1272c85023e1SHuy Nguyen 	int err;
1273c85023e1SHuy Nguyen 
1274c85023e1SHuy Nguyen 	err = mlx5_core_alloc_transport_domain(dev->mdev, tdn);
1275c85023e1SHuy Nguyen 	if (err)
1276c85023e1SHuy Nguyen 		return err;
1277c85023e1SHuy Nguyen 
1278c85023e1SHuy Nguyen 	if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
1279c85023e1SHuy Nguyen 	    !MLX5_CAP_GEN(dev->mdev, disable_local_lb))
1280c85023e1SHuy Nguyen 		return err;
1281c85023e1SHuy Nguyen 
1282c85023e1SHuy Nguyen 	mutex_lock(&dev->lb_mutex);
1283c85023e1SHuy Nguyen 	dev->user_td++;
1284c85023e1SHuy Nguyen 
1285c85023e1SHuy Nguyen 	if (dev->user_td == 2)
1286c85023e1SHuy Nguyen 		err = mlx5_nic_vport_update_local_lb(dev->mdev, true);
1287c85023e1SHuy Nguyen 
1288c85023e1SHuy Nguyen 	mutex_unlock(&dev->lb_mutex);
1289c85023e1SHuy Nguyen 	return err;
1290c85023e1SHuy Nguyen }
1291c85023e1SHuy Nguyen 
1292c85023e1SHuy Nguyen static void mlx5_ib_dealloc_transport_domain(struct mlx5_ib_dev *dev, u32 tdn)
1293c85023e1SHuy Nguyen {
1294c85023e1SHuy Nguyen 	mlx5_core_dealloc_transport_domain(dev->mdev, tdn);
1295c85023e1SHuy Nguyen 
1296c85023e1SHuy Nguyen 	if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
1297c85023e1SHuy Nguyen 	    !MLX5_CAP_GEN(dev->mdev, disable_local_lb))
1298c85023e1SHuy Nguyen 		return;
1299c85023e1SHuy Nguyen 
1300c85023e1SHuy Nguyen 	mutex_lock(&dev->lb_mutex);
1301c85023e1SHuy Nguyen 	dev->user_td--;
1302c85023e1SHuy Nguyen 
1303c85023e1SHuy Nguyen 	if (dev->user_td < 2)
1304c85023e1SHuy Nguyen 		mlx5_nic_vport_update_local_lb(dev->mdev, false);
1305c85023e1SHuy Nguyen 
1306c85023e1SHuy Nguyen 	mutex_unlock(&dev->lb_mutex);
1307c85023e1SHuy Nguyen }
1308c85023e1SHuy Nguyen 
1309e126ba97SEli Cohen static struct ib_ucontext *mlx5_ib_alloc_ucontext(struct ib_device *ibdev,
1310e126ba97SEli Cohen 						  struct ib_udata *udata)
1311e126ba97SEli Cohen {
1312e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1313b368d7cbSMatan Barak 	struct mlx5_ib_alloc_ucontext_req_v2 req = {};
1314b368d7cbSMatan Barak 	struct mlx5_ib_alloc_ucontext_resp resp = {};
1315e126ba97SEli Cohen 	struct mlx5_ib_ucontext *context;
13162f5ff264SEli Cohen 	struct mlx5_bfreg_info *bfregi;
131778c0f98cSEli Cohen 	int ver;
1318e126ba97SEli Cohen 	int err;
1319a168a41cSMajd Dibbiny 	size_t min_req_v2 = offsetof(struct mlx5_ib_alloc_ucontext_req_v2,
1320a168a41cSMajd Dibbiny 				     max_cqe_version);
1321b037c29aSEli Cohen 	bool lib_uar_4k;
1322e126ba97SEli Cohen 
1323e126ba97SEli Cohen 	if (!dev->ib_active)
1324e126ba97SEli Cohen 		return ERR_PTR(-EAGAIN);
1325e126ba97SEli Cohen 
1326e093111dSAmrani, Ram 	if (udata->inlen == sizeof(struct mlx5_ib_alloc_ucontext_req))
132778c0f98cSEli Cohen 		ver = 0;
1328e093111dSAmrani, Ram 	else if (udata->inlen >= min_req_v2)
132978c0f98cSEli Cohen 		ver = 2;
133078c0f98cSEli Cohen 	else
133178c0f98cSEli Cohen 		return ERR_PTR(-EINVAL);
133278c0f98cSEli Cohen 
1333e093111dSAmrani, Ram 	err = ib_copy_from_udata(&req, udata, min(udata->inlen, sizeof(req)));
1334e126ba97SEli Cohen 	if (err)
1335e126ba97SEli Cohen 		return ERR_PTR(err);
1336e126ba97SEli Cohen 
1337b368d7cbSMatan Barak 	if (req.flags)
133878c0f98cSEli Cohen 		return ERR_PTR(-EINVAL);
133978c0f98cSEli Cohen 
1340f72300c5SHaggai Abramovsky 	if (req.comp_mask || req.reserved0 || req.reserved1 || req.reserved2)
1341b368d7cbSMatan Barak 		return ERR_PTR(-EOPNOTSUPP);
1342b368d7cbSMatan Barak 
13432f5ff264SEli Cohen 	req.total_num_bfregs = ALIGN(req.total_num_bfregs,
13442f5ff264SEli Cohen 				    MLX5_NON_FP_BFREGS_PER_UAR);
13452f5ff264SEli Cohen 	if (req.num_low_latency_bfregs > req.total_num_bfregs - 1)
1346e126ba97SEli Cohen 		return ERR_PTR(-EINVAL);
1347e126ba97SEli Cohen 
1348938fe83cSSaeed Mahameed 	resp.qp_tab_size = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp);
13492cc6ad5fSNoa Osherovich 	if (mlx5_core_is_pf(dev->mdev) && MLX5_CAP_GEN(dev->mdev, bf))
1350938fe83cSSaeed Mahameed 		resp.bf_reg_size = 1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size);
1351b47bd6eaSDaniel Jurgens 	resp.cache_line_size = cache_line_size();
1352938fe83cSSaeed Mahameed 	resp.max_sq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq);
1353938fe83cSSaeed Mahameed 	resp.max_rq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq);
1354938fe83cSSaeed Mahameed 	resp.max_send_wqebb = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1355938fe83cSSaeed Mahameed 	resp.max_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1356938fe83cSSaeed Mahameed 	resp.max_srq_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz);
1357f72300c5SHaggai Abramovsky 	resp.cqe_version = min_t(__u8,
1358f72300c5SHaggai Abramovsky 				 (__u8)MLX5_CAP_GEN(dev->mdev, cqe_version),
1359f72300c5SHaggai Abramovsky 				 req.max_cqe_version);
136030aa60b3SEli Cohen 	resp.log_uar_size = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
136130aa60b3SEli Cohen 				MLX5_ADAPTER_PAGE_SHIFT : PAGE_SHIFT;
136230aa60b3SEli Cohen 	resp.num_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
136330aa60b3SEli Cohen 					MLX5_CAP_GEN(dev->mdev, num_of_uars_per_page) : 1;
1364b368d7cbSMatan Barak 	resp.response_length = min(offsetof(typeof(resp), response_length) +
1365b368d7cbSMatan Barak 				   sizeof(resp.response_length), udata->outlen);
1366e126ba97SEli Cohen 
1367e126ba97SEli Cohen 	context = kzalloc(sizeof(*context), GFP_KERNEL);
1368e126ba97SEli Cohen 	if (!context)
1369e126ba97SEli Cohen 		return ERR_PTR(-ENOMEM);
1370e126ba97SEli Cohen 
137130aa60b3SEli Cohen 	lib_uar_4k = req.lib_caps & MLX5_LIB_CAP_4K_UAR;
13722f5ff264SEli Cohen 	bfregi = &context->bfregi;
1373b037c29aSEli Cohen 
1374b037c29aSEli Cohen 	/* updates req->total_num_bfregs */
1375b037c29aSEli Cohen 	err = calc_total_bfregs(dev, lib_uar_4k, &req, &bfregi->num_sys_pages);
1376b037c29aSEli Cohen 	if (err)
1377b037c29aSEli Cohen 		goto out_ctx;
1378b037c29aSEli Cohen 
13792f5ff264SEli Cohen 	mutex_init(&bfregi->lock);
1380b037c29aSEli Cohen 	bfregi->lib_uar_4k = lib_uar_4k;
1381b037c29aSEli Cohen 	bfregi->count = kcalloc(req.total_num_bfregs, sizeof(*bfregi->count),
1382b037c29aSEli Cohen 				GFP_KERNEL);
1383b037c29aSEli Cohen 	if (!bfregi->count) {
1384e126ba97SEli Cohen 		err = -ENOMEM;
1385e126ba97SEli Cohen 		goto out_ctx;
1386e126ba97SEli Cohen 	}
1387e126ba97SEli Cohen 
1388b037c29aSEli Cohen 	bfregi->sys_pages = kcalloc(bfregi->num_sys_pages,
1389b037c29aSEli Cohen 				    sizeof(*bfregi->sys_pages),
1390e126ba97SEli Cohen 				    GFP_KERNEL);
1391b037c29aSEli Cohen 	if (!bfregi->sys_pages) {
1392e126ba97SEli Cohen 		err = -ENOMEM;
1393e126ba97SEli Cohen 		goto out_count;
1394e126ba97SEli Cohen 	}
1395e126ba97SEli Cohen 
1396b037c29aSEli Cohen 	err = allocate_uars(dev, context);
1397b037c29aSEli Cohen 	if (err)
1398b037c29aSEli Cohen 		goto out_sys_pages;
1399b037c29aSEli Cohen 
1400b4cfe447SHaggai Eran #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
1401b4cfe447SHaggai Eran 	context->ibucontext.invalidate_range = &mlx5_ib_invalidate_range;
1402b4cfe447SHaggai Eran #endif
1403b4cfe447SHaggai Eran 
14047d0cc6edSArtemy Kovalyov 	context->upd_xlt_page = __get_free_page(GFP_KERNEL);
14057d0cc6edSArtemy Kovalyov 	if (!context->upd_xlt_page) {
14067d0cc6edSArtemy Kovalyov 		err = -ENOMEM;
14077d0cc6edSArtemy Kovalyov 		goto out_uars;
14087d0cc6edSArtemy Kovalyov 	}
14097d0cc6edSArtemy Kovalyov 	mutex_init(&context->upd_xlt_page_mutex);
14107d0cc6edSArtemy Kovalyov 
1411146d2f1aSmajd@mellanox.com 	if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain)) {
1412c85023e1SHuy Nguyen 		err = mlx5_ib_alloc_transport_domain(dev, &context->tdn);
1413146d2f1aSmajd@mellanox.com 		if (err)
14147d0cc6edSArtemy Kovalyov 			goto out_page;
1415146d2f1aSmajd@mellanox.com 	}
1416146d2f1aSmajd@mellanox.com 
14177c2344c3SMaor Gottlieb 	INIT_LIST_HEAD(&context->vma_private_list);
1418e126ba97SEli Cohen 	INIT_LIST_HEAD(&context->db_page_list);
1419e126ba97SEli Cohen 	mutex_init(&context->db_page_mutex);
1420e126ba97SEli Cohen 
14212f5ff264SEli Cohen 	resp.tot_bfregs = req.total_num_bfregs;
1422938fe83cSSaeed Mahameed 	resp.num_ports = MLX5_CAP_GEN(dev->mdev, num_ports);
1423b368d7cbSMatan Barak 
1424f72300c5SHaggai Abramovsky 	if (field_avail(typeof(resp), cqe_version, udata->outlen))
1425f72300c5SHaggai Abramovsky 		resp.response_length += sizeof(resp.cqe_version);
1426b368d7cbSMatan Barak 
1427402ca536SBodong Wang 	if (field_avail(typeof(resp), cmds_supp_uhw, udata->outlen)) {
14286ad279c5SMoni Shoua 		resp.cmds_supp_uhw |= MLX5_USER_CMDS_SUPP_UHW_QUERY_DEVICE |
14296ad279c5SMoni Shoua 				      MLX5_USER_CMDS_SUPP_UHW_CREATE_AH;
1430402ca536SBodong Wang 		resp.response_length += sizeof(resp.cmds_supp_uhw);
1431402ca536SBodong Wang 	}
1432402ca536SBodong Wang 
143378984898SOr Gerlitz 	if (field_avail(typeof(resp), eth_min_inline, udata->outlen)) {
143478984898SOr Gerlitz 		if (mlx5_ib_port_link_layer(ibdev, 1) == IB_LINK_LAYER_ETHERNET) {
143578984898SOr Gerlitz 			mlx5_query_min_inline(dev->mdev, &resp.eth_min_inline);
143678984898SOr Gerlitz 			resp.eth_min_inline++;
143778984898SOr Gerlitz 		}
143878984898SOr Gerlitz 		resp.response_length += sizeof(resp.eth_min_inline);
143978984898SOr Gerlitz 	}
144078984898SOr Gerlitz 
1441bc5c6eedSNoa Osherovich 	/*
1442bc5c6eedSNoa Osherovich 	 * We don't want to expose information from the PCI bar that is located
1443bc5c6eedSNoa Osherovich 	 * after 4096 bytes, so if the arch only supports larger pages, let's
1444bc5c6eedSNoa Osherovich 	 * pretend we don't support reading the HCA's core clock. This is also
1445bc5c6eedSNoa Osherovich 	 * forced by mmap function.
1446bc5c6eedSNoa Osherovich 	 */
1447de8d6e02SEli Cohen 	if (field_avail(typeof(resp), hca_core_clock_offset, udata->outlen)) {
1448de8d6e02SEli Cohen 		if (PAGE_SIZE <= 4096) {
1449b368d7cbSMatan Barak 			resp.comp_mask |=
1450b368d7cbSMatan Barak 				MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_CORE_CLOCK_OFFSET;
1451b368d7cbSMatan Barak 			resp.hca_core_clock_offset =
1452de8d6e02SEli Cohen 				offsetof(struct mlx5_init_seg, internal_timer_h) % PAGE_SIZE;
1453de8d6e02SEli Cohen 		}
1454f72300c5SHaggai Abramovsky 		resp.response_length += sizeof(resp.hca_core_clock_offset) +
1455402ca536SBodong Wang 					sizeof(resp.reserved2);
1456b368d7cbSMatan Barak 	}
1457b368d7cbSMatan Barak 
145830aa60b3SEli Cohen 	if (field_avail(typeof(resp), log_uar_size, udata->outlen))
145930aa60b3SEli Cohen 		resp.response_length += sizeof(resp.log_uar_size);
146030aa60b3SEli Cohen 
146130aa60b3SEli Cohen 	if (field_avail(typeof(resp), num_uars_per_page, udata->outlen))
146230aa60b3SEli Cohen 		resp.response_length += sizeof(resp.num_uars_per_page);
146330aa60b3SEli Cohen 
1464b368d7cbSMatan Barak 	err = ib_copy_to_udata(udata, &resp, resp.response_length);
1465e126ba97SEli Cohen 	if (err)
1466146d2f1aSmajd@mellanox.com 		goto out_td;
1467e126ba97SEli Cohen 
14682f5ff264SEli Cohen 	bfregi->ver = ver;
14692f5ff264SEli Cohen 	bfregi->num_low_latency_bfregs = req.num_low_latency_bfregs;
1470f72300c5SHaggai Abramovsky 	context->cqe_version = resp.cqe_version;
147130aa60b3SEli Cohen 	context->lib_caps = req.lib_caps;
147230aa60b3SEli Cohen 	print_lib_caps(dev, context->lib_caps);
1473f72300c5SHaggai Abramovsky 
1474e126ba97SEli Cohen 	return &context->ibucontext;
1475e126ba97SEli Cohen 
1476146d2f1aSmajd@mellanox.com out_td:
1477146d2f1aSmajd@mellanox.com 	if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1478c85023e1SHuy Nguyen 		mlx5_ib_dealloc_transport_domain(dev, context->tdn);
1479146d2f1aSmajd@mellanox.com 
14807d0cc6edSArtemy Kovalyov out_page:
14817d0cc6edSArtemy Kovalyov 	free_page(context->upd_xlt_page);
14827d0cc6edSArtemy Kovalyov 
1483e126ba97SEli Cohen out_uars:
1484b037c29aSEli Cohen 	deallocate_uars(dev, context);
1485b037c29aSEli Cohen 
1486b037c29aSEli Cohen out_sys_pages:
1487b037c29aSEli Cohen 	kfree(bfregi->sys_pages);
1488b037c29aSEli Cohen 
1489e126ba97SEli Cohen out_count:
14902f5ff264SEli Cohen 	kfree(bfregi->count);
1491e126ba97SEli Cohen 
1492e126ba97SEli Cohen out_ctx:
1493e126ba97SEli Cohen 	kfree(context);
1494b037c29aSEli Cohen 
1495e126ba97SEli Cohen 	return ERR_PTR(err);
1496e126ba97SEli Cohen }
1497e126ba97SEli Cohen 
1498e126ba97SEli Cohen static int mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
1499e126ba97SEli Cohen {
1500e126ba97SEli Cohen 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1501e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
1502b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1503e126ba97SEli Cohen 
1504b037c29aSEli Cohen 	bfregi = &context->bfregi;
1505146d2f1aSmajd@mellanox.com 	if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1506c85023e1SHuy Nguyen 		mlx5_ib_dealloc_transport_domain(dev, context->tdn);
1507146d2f1aSmajd@mellanox.com 
15087d0cc6edSArtemy Kovalyov 	free_page(context->upd_xlt_page);
1509b037c29aSEli Cohen 	deallocate_uars(dev, context);
1510b037c29aSEli Cohen 	kfree(bfregi->sys_pages);
15112f5ff264SEli Cohen 	kfree(bfregi->count);
1512e126ba97SEli Cohen 	kfree(context);
1513e126ba97SEli Cohen 
1514e126ba97SEli Cohen 	return 0;
1515e126ba97SEli Cohen }
1516e126ba97SEli Cohen 
1517b037c29aSEli Cohen static phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev,
1518b037c29aSEli Cohen 				 struct mlx5_bfreg_info *bfregi,
1519b037c29aSEli Cohen 				 int idx)
1520e126ba97SEli Cohen {
1521b037c29aSEli Cohen 	int fw_uars_per_page;
1522b037c29aSEli Cohen 
1523b037c29aSEli Cohen 	fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ? MLX5_UARS_IN_PAGE : 1;
1524b037c29aSEli Cohen 
1525b037c29aSEli Cohen 	return (pci_resource_start(dev->mdev->pdev, 0) >> PAGE_SHIFT) +
1526b037c29aSEli Cohen 			bfregi->sys_pages[idx] / fw_uars_per_page;
1527e126ba97SEli Cohen }
1528e126ba97SEli Cohen 
1529e126ba97SEli Cohen static int get_command(unsigned long offset)
1530e126ba97SEli Cohen {
1531e126ba97SEli Cohen 	return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK;
1532e126ba97SEli Cohen }
1533e126ba97SEli Cohen 
1534e126ba97SEli Cohen static int get_arg(unsigned long offset)
1535e126ba97SEli Cohen {
1536e126ba97SEli Cohen 	return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1);
1537e126ba97SEli Cohen }
1538e126ba97SEli Cohen 
1539e126ba97SEli Cohen static int get_index(unsigned long offset)
1540e126ba97SEli Cohen {
1541e126ba97SEli Cohen 	return get_arg(offset);
1542e126ba97SEli Cohen }
1543e126ba97SEli Cohen 
15447c2344c3SMaor Gottlieb static void  mlx5_ib_vma_open(struct vm_area_struct *area)
15457c2344c3SMaor Gottlieb {
15467c2344c3SMaor Gottlieb 	/* vma_open is called when a new VMA is created on top of our VMA.  This
15477c2344c3SMaor Gottlieb 	 * is done through either mremap flow or split_vma (usually due to
15487c2344c3SMaor Gottlieb 	 * mlock, madvise, munmap, etc.) We do not support a clone of the VMA,
15497c2344c3SMaor Gottlieb 	 * as this VMA is strongly hardware related.  Therefore we set the
15507c2344c3SMaor Gottlieb 	 * vm_ops of the newly created/cloned VMA to NULL, to prevent it from
15517c2344c3SMaor Gottlieb 	 * calling us again and trying to do incorrect actions.  We assume that
15527c2344c3SMaor Gottlieb 	 * the original VMA size is exactly a single page, and therefore all
15537c2344c3SMaor Gottlieb 	 * "splitting" operation will not happen to it.
15547c2344c3SMaor Gottlieb 	 */
15557c2344c3SMaor Gottlieb 	area->vm_ops = NULL;
15567c2344c3SMaor Gottlieb }
15577c2344c3SMaor Gottlieb 
15587c2344c3SMaor Gottlieb static void  mlx5_ib_vma_close(struct vm_area_struct *area)
15597c2344c3SMaor Gottlieb {
15607c2344c3SMaor Gottlieb 	struct mlx5_ib_vma_private_data *mlx5_ib_vma_priv_data;
15617c2344c3SMaor Gottlieb 
15627c2344c3SMaor Gottlieb 	/* It's guaranteed that all VMAs opened on a FD are closed before the
15637c2344c3SMaor Gottlieb 	 * file itself is closed, therefore no sync is needed with the regular
15647c2344c3SMaor Gottlieb 	 * closing flow. (e.g. mlx5 ib_dealloc_ucontext)
15657c2344c3SMaor Gottlieb 	 * However need a sync with accessing the vma as part of
15667c2344c3SMaor Gottlieb 	 * mlx5_ib_disassociate_ucontext.
15677c2344c3SMaor Gottlieb 	 * The close operation is usually called under mm->mmap_sem except when
15687c2344c3SMaor Gottlieb 	 * process is exiting.
15697c2344c3SMaor Gottlieb 	 * The exiting case is handled explicitly as part of
15707c2344c3SMaor Gottlieb 	 * mlx5_ib_disassociate_ucontext.
15717c2344c3SMaor Gottlieb 	 */
15727c2344c3SMaor Gottlieb 	mlx5_ib_vma_priv_data = (struct mlx5_ib_vma_private_data *)area->vm_private_data;
15737c2344c3SMaor Gottlieb 
15747c2344c3SMaor Gottlieb 	/* setting the vma context pointer to null in the mlx5_ib driver's
15757c2344c3SMaor Gottlieb 	 * private data, to protect a race condition in
15767c2344c3SMaor Gottlieb 	 * mlx5_ib_disassociate_ucontext().
15777c2344c3SMaor Gottlieb 	 */
15787c2344c3SMaor Gottlieb 	mlx5_ib_vma_priv_data->vma = NULL;
15797c2344c3SMaor Gottlieb 	list_del(&mlx5_ib_vma_priv_data->list);
15807c2344c3SMaor Gottlieb 	kfree(mlx5_ib_vma_priv_data);
15817c2344c3SMaor Gottlieb }
15827c2344c3SMaor Gottlieb 
15837c2344c3SMaor Gottlieb static const struct vm_operations_struct mlx5_ib_vm_ops = {
15847c2344c3SMaor Gottlieb 	.open = mlx5_ib_vma_open,
15857c2344c3SMaor Gottlieb 	.close = mlx5_ib_vma_close
15867c2344c3SMaor Gottlieb };
15877c2344c3SMaor Gottlieb 
15887c2344c3SMaor Gottlieb static int mlx5_ib_set_vma_data(struct vm_area_struct *vma,
15897c2344c3SMaor Gottlieb 				struct mlx5_ib_ucontext *ctx)
15907c2344c3SMaor Gottlieb {
15917c2344c3SMaor Gottlieb 	struct mlx5_ib_vma_private_data *vma_prv;
15927c2344c3SMaor Gottlieb 	struct list_head *vma_head = &ctx->vma_private_list;
15937c2344c3SMaor Gottlieb 
15947c2344c3SMaor Gottlieb 	vma_prv = kzalloc(sizeof(*vma_prv), GFP_KERNEL);
15957c2344c3SMaor Gottlieb 	if (!vma_prv)
15967c2344c3SMaor Gottlieb 		return -ENOMEM;
15977c2344c3SMaor Gottlieb 
15987c2344c3SMaor Gottlieb 	vma_prv->vma = vma;
15997c2344c3SMaor Gottlieb 	vma->vm_private_data = vma_prv;
16007c2344c3SMaor Gottlieb 	vma->vm_ops =  &mlx5_ib_vm_ops;
16017c2344c3SMaor Gottlieb 
16027c2344c3SMaor Gottlieb 	list_add(&vma_prv->list, vma_head);
16037c2344c3SMaor Gottlieb 
16047c2344c3SMaor Gottlieb 	return 0;
16057c2344c3SMaor Gottlieb }
16067c2344c3SMaor Gottlieb 
16077c2344c3SMaor Gottlieb static void mlx5_ib_disassociate_ucontext(struct ib_ucontext *ibcontext)
16087c2344c3SMaor Gottlieb {
16097c2344c3SMaor Gottlieb 	int ret;
16107c2344c3SMaor Gottlieb 	struct vm_area_struct *vma;
16117c2344c3SMaor Gottlieb 	struct mlx5_ib_vma_private_data *vma_private, *n;
16127c2344c3SMaor Gottlieb 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
16137c2344c3SMaor Gottlieb 	struct task_struct *owning_process  = NULL;
16147c2344c3SMaor Gottlieb 	struct mm_struct   *owning_mm       = NULL;
16157c2344c3SMaor Gottlieb 
16167c2344c3SMaor Gottlieb 	owning_process = get_pid_task(ibcontext->tgid, PIDTYPE_PID);
16177c2344c3SMaor Gottlieb 	if (!owning_process)
16187c2344c3SMaor Gottlieb 		return;
16197c2344c3SMaor Gottlieb 
16207c2344c3SMaor Gottlieb 	owning_mm = get_task_mm(owning_process);
16217c2344c3SMaor Gottlieb 	if (!owning_mm) {
16227c2344c3SMaor Gottlieb 		pr_info("no mm, disassociate ucontext is pending task termination\n");
16237c2344c3SMaor Gottlieb 		while (1) {
16247c2344c3SMaor Gottlieb 			put_task_struct(owning_process);
16257c2344c3SMaor Gottlieb 			usleep_range(1000, 2000);
16267c2344c3SMaor Gottlieb 			owning_process = get_pid_task(ibcontext->tgid,
16277c2344c3SMaor Gottlieb 						      PIDTYPE_PID);
16287c2344c3SMaor Gottlieb 			if (!owning_process ||
16297c2344c3SMaor Gottlieb 			    owning_process->state == TASK_DEAD) {
16307c2344c3SMaor Gottlieb 				pr_info("disassociate ucontext done, task was terminated\n");
16317c2344c3SMaor Gottlieb 				/* in case task was dead need to release the
16327c2344c3SMaor Gottlieb 				 * task struct.
16337c2344c3SMaor Gottlieb 				 */
16347c2344c3SMaor Gottlieb 				if (owning_process)
16357c2344c3SMaor Gottlieb 					put_task_struct(owning_process);
16367c2344c3SMaor Gottlieb 				return;
16377c2344c3SMaor Gottlieb 			}
16387c2344c3SMaor Gottlieb 		}
16397c2344c3SMaor Gottlieb 	}
16407c2344c3SMaor Gottlieb 
16417c2344c3SMaor Gottlieb 	/* need to protect from a race on closing the vma as part of
16427c2344c3SMaor Gottlieb 	 * mlx5_ib_vma_close.
16437c2344c3SMaor Gottlieb 	 */
1644ecc7d83bSMaor Gottlieb 	down_write(&owning_mm->mmap_sem);
16457c2344c3SMaor Gottlieb 	list_for_each_entry_safe(vma_private, n, &context->vma_private_list,
16467c2344c3SMaor Gottlieb 				 list) {
16477c2344c3SMaor Gottlieb 		vma = vma_private->vma;
16487c2344c3SMaor Gottlieb 		ret = zap_vma_ptes(vma, vma->vm_start,
16497c2344c3SMaor Gottlieb 				   PAGE_SIZE);
16507c2344c3SMaor Gottlieb 		WARN_ONCE(ret, "%s: zap_vma_ptes failed", __func__);
16517c2344c3SMaor Gottlieb 		/* context going to be destroyed, should
16527c2344c3SMaor Gottlieb 		 * not access ops any more.
16537c2344c3SMaor Gottlieb 		 */
165413776612SMaor Gottlieb 		vma->vm_flags &= ~(VM_SHARED | VM_MAYSHARE);
16557c2344c3SMaor Gottlieb 		vma->vm_ops = NULL;
16567c2344c3SMaor Gottlieb 		list_del(&vma_private->list);
16577c2344c3SMaor Gottlieb 		kfree(vma_private);
16587c2344c3SMaor Gottlieb 	}
1659ecc7d83bSMaor Gottlieb 	up_write(&owning_mm->mmap_sem);
16607c2344c3SMaor Gottlieb 	mmput(owning_mm);
16617c2344c3SMaor Gottlieb 	put_task_struct(owning_process);
16627c2344c3SMaor Gottlieb }
16637c2344c3SMaor Gottlieb 
166437aa5c36SGuy Levi static inline char *mmap_cmd2str(enum mlx5_ib_mmap_cmd cmd)
1665e126ba97SEli Cohen {
166637aa5c36SGuy Levi 	switch (cmd) {
166737aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
166837aa5c36SGuy Levi 		return "WC";
1669e126ba97SEli Cohen 	case MLX5_IB_MMAP_REGULAR_PAGE:
167037aa5c36SGuy Levi 		return "best effort WC";
167137aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
167237aa5c36SGuy Levi 		return "NC";
167337aa5c36SGuy Levi 	default:
167437aa5c36SGuy Levi 		return NULL;
167537aa5c36SGuy Levi 	}
167637aa5c36SGuy Levi }
167737aa5c36SGuy Levi 
167837aa5c36SGuy Levi static int uar_mmap(struct mlx5_ib_dev *dev, enum mlx5_ib_mmap_cmd cmd,
16797c2344c3SMaor Gottlieb 		    struct vm_area_struct *vma,
16807c2344c3SMaor Gottlieb 		    struct mlx5_ib_ucontext *context)
168137aa5c36SGuy Levi {
16822f5ff264SEli Cohen 	struct mlx5_bfreg_info *bfregi = &context->bfregi;
168337aa5c36SGuy Levi 	int err;
168437aa5c36SGuy Levi 	unsigned long idx;
168537aa5c36SGuy Levi 	phys_addr_t pfn, pa;
168637aa5c36SGuy Levi 	pgprot_t prot;
1687b037c29aSEli Cohen 	int uars_per_page;
1688b037c29aSEli Cohen 
1689b037c29aSEli Cohen 	if (vma->vm_end - vma->vm_start != PAGE_SIZE)
1690b037c29aSEli Cohen 		return -EINVAL;
1691b037c29aSEli Cohen 
1692b037c29aSEli Cohen 	uars_per_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k);
1693b037c29aSEli Cohen 	idx = get_index(vma->vm_pgoff);
1694b037c29aSEli Cohen 	if (idx % uars_per_page ||
1695b037c29aSEli Cohen 	    idx * uars_per_page >= bfregi->num_sys_pages) {
1696b037c29aSEli Cohen 		mlx5_ib_warn(dev, "invalid uar index %lu\n", idx);
1697b037c29aSEli Cohen 		return -EINVAL;
1698b037c29aSEli Cohen 	}
169937aa5c36SGuy Levi 
170037aa5c36SGuy Levi 	switch (cmd) {
170137aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
170237aa5c36SGuy Levi /* Some architectures don't support WC memory */
170337aa5c36SGuy Levi #if defined(CONFIG_X86)
170437aa5c36SGuy Levi 		if (!pat_enabled())
170537aa5c36SGuy Levi 			return -EPERM;
170637aa5c36SGuy Levi #elif !(defined(CONFIG_PPC) || (defined(CONFIG_ARM) && defined(CONFIG_MMU)))
170737aa5c36SGuy Levi 			return -EPERM;
170837aa5c36SGuy Levi #endif
170937aa5c36SGuy Levi 	/* fall through */
171037aa5c36SGuy Levi 	case MLX5_IB_MMAP_REGULAR_PAGE:
171137aa5c36SGuy Levi 		/* For MLX5_IB_MMAP_REGULAR_PAGE do the best effort to get WC */
171237aa5c36SGuy Levi 		prot = pgprot_writecombine(vma->vm_page_prot);
171337aa5c36SGuy Levi 		break;
171437aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
171537aa5c36SGuy Levi 		prot = pgprot_noncached(vma->vm_page_prot);
171637aa5c36SGuy Levi 		break;
171737aa5c36SGuy Levi 	default:
171837aa5c36SGuy Levi 		return -EINVAL;
171937aa5c36SGuy Levi 	}
172037aa5c36SGuy Levi 
1721b037c29aSEli Cohen 	pfn = uar_index2pfn(dev, bfregi, idx);
172237aa5c36SGuy Levi 	mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn %pa\n", idx, &pfn);
1723e126ba97SEli Cohen 
172437aa5c36SGuy Levi 	vma->vm_page_prot = prot;
172537aa5c36SGuy Levi 	err = io_remap_pfn_range(vma, vma->vm_start, pfn,
172637aa5c36SGuy Levi 				 PAGE_SIZE, vma->vm_page_prot);
172737aa5c36SGuy Levi 	if (err) {
172837aa5c36SGuy Levi 		mlx5_ib_err(dev, "io_remap_pfn_range failed with error=%d, vm_start=0x%lx, pfn=%pa, mmap_cmd=%s\n",
172937aa5c36SGuy Levi 			    err, vma->vm_start, &pfn, mmap_cmd2str(cmd));
1730e126ba97SEli Cohen 		return -EAGAIN;
173137aa5c36SGuy Levi 	}
1732e126ba97SEli Cohen 
173337aa5c36SGuy Levi 	pa = pfn << PAGE_SHIFT;
173437aa5c36SGuy Levi 	mlx5_ib_dbg(dev, "mapped %s at 0x%lx, PA %pa\n", mmap_cmd2str(cmd),
173537aa5c36SGuy Levi 		    vma->vm_start, &pa);
173637aa5c36SGuy Levi 
17377c2344c3SMaor Gottlieb 	return mlx5_ib_set_vma_data(vma, context);
173837aa5c36SGuy Levi }
173937aa5c36SGuy Levi 
174037aa5c36SGuy Levi static int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma)
174137aa5c36SGuy Levi {
174237aa5c36SGuy Levi 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
174337aa5c36SGuy Levi 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
174437aa5c36SGuy Levi 	unsigned long command;
174537aa5c36SGuy Levi 	phys_addr_t pfn;
174637aa5c36SGuy Levi 
174737aa5c36SGuy Levi 	command = get_command(vma->vm_pgoff);
174837aa5c36SGuy Levi 	switch (command) {
174937aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
175037aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
175137aa5c36SGuy Levi 	case MLX5_IB_MMAP_REGULAR_PAGE:
17527c2344c3SMaor Gottlieb 		return uar_mmap(dev, command, vma, context);
1753e126ba97SEli Cohen 
1754e126ba97SEli Cohen 	case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES:
1755e126ba97SEli Cohen 		return -ENOSYS;
1756e126ba97SEli Cohen 
1757d69e3bcfSMatan Barak 	case MLX5_IB_MMAP_CORE_CLOCK:
1758d69e3bcfSMatan Barak 		if (vma->vm_end - vma->vm_start != PAGE_SIZE)
1759d69e3bcfSMatan Barak 			return -EINVAL;
1760d69e3bcfSMatan Barak 
17616cbac1e4SMatan Barak 		if (vma->vm_flags & VM_WRITE)
1762d69e3bcfSMatan Barak 			return -EPERM;
1763d69e3bcfSMatan Barak 
1764d69e3bcfSMatan Barak 		/* Don't expose to user-space information it shouldn't have */
1765d69e3bcfSMatan Barak 		if (PAGE_SIZE > 4096)
1766d69e3bcfSMatan Barak 			return -EOPNOTSUPP;
1767d69e3bcfSMatan Barak 
1768d69e3bcfSMatan Barak 		vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1769d69e3bcfSMatan Barak 		pfn = (dev->mdev->iseg_base +
1770d69e3bcfSMatan Barak 		       offsetof(struct mlx5_init_seg, internal_timer_h)) >>
1771d69e3bcfSMatan Barak 			PAGE_SHIFT;
1772d69e3bcfSMatan Barak 		if (io_remap_pfn_range(vma, vma->vm_start, pfn,
1773d69e3bcfSMatan Barak 				       PAGE_SIZE, vma->vm_page_prot))
1774d69e3bcfSMatan Barak 			return -EAGAIN;
1775d69e3bcfSMatan Barak 
1776d69e3bcfSMatan Barak 		mlx5_ib_dbg(dev, "mapped internal timer at 0x%lx, PA 0x%llx\n",
1777d69e3bcfSMatan Barak 			    vma->vm_start,
1778d69e3bcfSMatan Barak 			    (unsigned long long)pfn << PAGE_SHIFT);
1779d69e3bcfSMatan Barak 		break;
1780d69e3bcfSMatan Barak 
1781e126ba97SEli Cohen 	default:
1782e126ba97SEli Cohen 		return -EINVAL;
1783e126ba97SEli Cohen 	}
1784e126ba97SEli Cohen 
1785e126ba97SEli Cohen 	return 0;
1786e126ba97SEli Cohen }
1787e126ba97SEli Cohen 
1788e126ba97SEli Cohen static struct ib_pd *mlx5_ib_alloc_pd(struct ib_device *ibdev,
1789e126ba97SEli Cohen 				      struct ib_ucontext *context,
1790e126ba97SEli Cohen 				      struct ib_udata *udata)
1791e126ba97SEli Cohen {
1792e126ba97SEli Cohen 	struct mlx5_ib_alloc_pd_resp resp;
1793e126ba97SEli Cohen 	struct mlx5_ib_pd *pd;
1794e126ba97SEli Cohen 	int err;
1795e126ba97SEli Cohen 
1796e126ba97SEli Cohen 	pd = kmalloc(sizeof(*pd), GFP_KERNEL);
1797e126ba97SEli Cohen 	if (!pd)
1798e126ba97SEli Cohen 		return ERR_PTR(-ENOMEM);
1799e126ba97SEli Cohen 
18009603b61dSJack Morgenstein 	err = mlx5_core_alloc_pd(to_mdev(ibdev)->mdev, &pd->pdn);
1801e126ba97SEli Cohen 	if (err) {
1802e126ba97SEli Cohen 		kfree(pd);
1803e126ba97SEli Cohen 		return ERR_PTR(err);
1804e126ba97SEli Cohen 	}
1805e126ba97SEli Cohen 
1806e126ba97SEli Cohen 	if (context) {
1807e126ba97SEli Cohen 		resp.pdn = pd->pdn;
1808e126ba97SEli Cohen 		if (ib_copy_to_udata(udata, &resp, sizeof(resp))) {
18099603b61dSJack Morgenstein 			mlx5_core_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn);
1810e126ba97SEli Cohen 			kfree(pd);
1811e126ba97SEli Cohen 			return ERR_PTR(-EFAULT);
1812e126ba97SEli Cohen 		}
1813e126ba97SEli Cohen 	}
1814e126ba97SEli Cohen 
1815e126ba97SEli Cohen 	return &pd->ibpd;
1816e126ba97SEli Cohen }
1817e126ba97SEli Cohen 
1818e126ba97SEli Cohen static int mlx5_ib_dealloc_pd(struct ib_pd *pd)
1819e126ba97SEli Cohen {
1820e126ba97SEli Cohen 	struct mlx5_ib_dev *mdev = to_mdev(pd->device);
1821e126ba97SEli Cohen 	struct mlx5_ib_pd *mpd = to_mpd(pd);
1822e126ba97SEli Cohen 
18239603b61dSJack Morgenstein 	mlx5_core_dealloc_pd(mdev->mdev, mpd->pdn);
1824e126ba97SEli Cohen 	kfree(mpd);
1825e126ba97SEli Cohen 
1826e126ba97SEli Cohen 	return 0;
1827e126ba97SEli Cohen }
1828e126ba97SEli Cohen 
1829466fa6d2SMaor Gottlieb enum {
1830466fa6d2SMaor Gottlieb 	MATCH_CRITERIA_ENABLE_OUTER_BIT,
1831466fa6d2SMaor Gottlieb 	MATCH_CRITERIA_ENABLE_MISC_BIT,
1832466fa6d2SMaor Gottlieb 	MATCH_CRITERIA_ENABLE_INNER_BIT
1833466fa6d2SMaor Gottlieb };
1834038d2ef8SMaor Gottlieb 
1835466fa6d2SMaor Gottlieb #define HEADER_IS_ZERO(match_criteria, headers)			           \
1836466fa6d2SMaor Gottlieb 	!(memchr_inv(MLX5_ADDR_OF(fte_match_param, match_criteria, headers), \
1837466fa6d2SMaor Gottlieb 		    0, MLX5_FLD_SZ_BYTES(fte_match_param, headers)))       \
1838466fa6d2SMaor Gottlieb 
1839466fa6d2SMaor Gottlieb static u8 get_match_criteria_enable(u32 *match_criteria)
1840466fa6d2SMaor Gottlieb {
1841466fa6d2SMaor Gottlieb 	u8 match_criteria_enable;
1842466fa6d2SMaor Gottlieb 
1843466fa6d2SMaor Gottlieb 	match_criteria_enable =
1844466fa6d2SMaor Gottlieb 		(!HEADER_IS_ZERO(match_criteria, outer_headers)) <<
1845466fa6d2SMaor Gottlieb 		MATCH_CRITERIA_ENABLE_OUTER_BIT;
1846466fa6d2SMaor Gottlieb 	match_criteria_enable |=
1847466fa6d2SMaor Gottlieb 		(!HEADER_IS_ZERO(match_criteria, misc_parameters)) <<
1848466fa6d2SMaor Gottlieb 		MATCH_CRITERIA_ENABLE_MISC_BIT;
1849466fa6d2SMaor Gottlieb 	match_criteria_enable |=
1850466fa6d2SMaor Gottlieb 		(!HEADER_IS_ZERO(match_criteria, inner_headers)) <<
1851466fa6d2SMaor Gottlieb 		MATCH_CRITERIA_ENABLE_INNER_BIT;
1852466fa6d2SMaor Gottlieb 
1853466fa6d2SMaor Gottlieb 	return match_criteria_enable;
1854038d2ef8SMaor Gottlieb }
1855038d2ef8SMaor Gottlieb 
1856ca0d4753SMaor Gottlieb static void set_proto(void *outer_c, void *outer_v, u8 mask, u8 val)
1857ca0d4753SMaor Gottlieb {
1858ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_protocol, mask);
1859ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_protocol, val);
1860ca0d4753SMaor Gottlieb }
1861ca0d4753SMaor Gottlieb 
18622d1e697eSMoses Reuben static void set_flow_label(void *misc_c, void *misc_v, u8 mask, u8 val,
18632d1e697eSMoses Reuben 			   bool inner)
18642d1e697eSMoses Reuben {
18652d1e697eSMoses Reuben 	if (inner) {
18662d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
18672d1e697eSMoses Reuben 			 misc_c, inner_ipv6_flow_label, mask);
18682d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
18692d1e697eSMoses Reuben 			 misc_v, inner_ipv6_flow_label, val);
18702d1e697eSMoses Reuben 	} else {
18712d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
18722d1e697eSMoses Reuben 			 misc_c, outer_ipv6_flow_label, mask);
18732d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
18742d1e697eSMoses Reuben 			 misc_v, outer_ipv6_flow_label, val);
18752d1e697eSMoses Reuben 	}
18762d1e697eSMoses Reuben }
18772d1e697eSMoses Reuben 
1878ca0d4753SMaor Gottlieb static void set_tos(void *outer_c, void *outer_v, u8 mask, u8 val)
1879ca0d4753SMaor Gottlieb {
1880ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_ecn, mask);
1881ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_ecn, val);
1882ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_dscp, mask >> 2);
1883ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_dscp, val >> 2);
1884ca0d4753SMaor Gottlieb }
1885ca0d4753SMaor Gottlieb 
1886c47ac6aeSMaor Gottlieb #define LAST_ETH_FIELD vlan_tag
1887c47ac6aeSMaor Gottlieb #define LAST_IB_FIELD sl
1888ca0d4753SMaor Gottlieb #define LAST_IPV4_FIELD tos
1889466fa6d2SMaor Gottlieb #define LAST_IPV6_FIELD traffic_class
1890c47ac6aeSMaor Gottlieb #define LAST_TCP_UDP_FIELD src_port
1891ffb30d8fSMoses Reuben #define LAST_TUNNEL_FIELD tunnel_id
18922ac693f9SMoses Reuben #define LAST_FLOW_TAG_FIELD tag_id
1893a22ed86cSSlava Shwartsman #define LAST_DROP_FIELD size
1894c47ac6aeSMaor Gottlieb 
1895c47ac6aeSMaor Gottlieb /* Field is the last supported field */
1896c47ac6aeSMaor Gottlieb #define FIELDS_NOT_SUPPORTED(filter, field)\
1897c47ac6aeSMaor Gottlieb 	memchr_inv((void *)&filter.field  +\
1898c47ac6aeSMaor Gottlieb 		   sizeof(filter.field), 0,\
1899c47ac6aeSMaor Gottlieb 		   sizeof(filter) -\
1900c47ac6aeSMaor Gottlieb 		   offsetof(typeof(filter), field) -\
1901c47ac6aeSMaor Gottlieb 		   sizeof(filter.field))
1902c47ac6aeSMaor Gottlieb 
190319cc7524SAriel Levkovich #define IPV4_VERSION 4
190419cc7524SAriel Levkovich #define IPV6_VERSION 6
190519cc7524SAriel Levkovich static int parse_flow_attr(struct mlx5_core_dev *mdev, u32 *match_c,
190619cc7524SAriel Levkovich 			   u32 *match_v, const union ib_flow_spec *ib_spec,
1907a22ed86cSSlava Shwartsman 			   u32 *tag_id, bool *is_drop)
1908038d2ef8SMaor Gottlieb {
1909466fa6d2SMaor Gottlieb 	void *misc_params_c = MLX5_ADDR_OF(fte_match_param, match_c,
1910466fa6d2SMaor Gottlieb 					   misc_parameters);
1911466fa6d2SMaor Gottlieb 	void *misc_params_v = MLX5_ADDR_OF(fte_match_param, match_v,
1912466fa6d2SMaor Gottlieb 					   misc_parameters);
19132d1e697eSMoses Reuben 	void *headers_c;
19142d1e697eSMoses Reuben 	void *headers_v;
191519cc7524SAriel Levkovich 	int match_ipv;
1916466fa6d2SMaor Gottlieb 
19172d1e697eSMoses Reuben 	if (ib_spec->type & IB_FLOW_SPEC_INNER) {
19182d1e697eSMoses Reuben 		headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
19192d1e697eSMoses Reuben 					 inner_headers);
19202d1e697eSMoses Reuben 		headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
19212d1e697eSMoses Reuben 					 inner_headers);
192219cc7524SAriel Levkovich 		match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
192319cc7524SAriel Levkovich 					ft_field_support.inner_ip_version);
19242d1e697eSMoses Reuben 	} else {
19252d1e697eSMoses Reuben 		headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
19262d1e697eSMoses Reuben 					 outer_headers);
19272d1e697eSMoses Reuben 		headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
19282d1e697eSMoses Reuben 					 outer_headers);
192919cc7524SAriel Levkovich 		match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
193019cc7524SAriel Levkovich 					ft_field_support.outer_ip_version);
19312d1e697eSMoses Reuben 	}
19322d1e697eSMoses Reuben 
19332d1e697eSMoses Reuben 	switch (ib_spec->type & ~IB_FLOW_SPEC_INNER) {
1934038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_ETH:
1935c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->eth.mask, LAST_ETH_FIELD))
19361ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
1937038d2ef8SMaor Gottlieb 
19382d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
1939038d2ef8SMaor Gottlieb 					     dmac_47_16),
1940038d2ef8SMaor Gottlieb 				ib_spec->eth.mask.dst_mac);
19412d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
1942038d2ef8SMaor Gottlieb 					     dmac_47_16),
1943038d2ef8SMaor Gottlieb 				ib_spec->eth.val.dst_mac);
1944038d2ef8SMaor Gottlieb 
19452d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
1946ee3da804SMaor Gottlieb 					     smac_47_16),
1947ee3da804SMaor Gottlieb 				ib_spec->eth.mask.src_mac);
19482d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
1949ee3da804SMaor Gottlieb 					     smac_47_16),
1950ee3da804SMaor Gottlieb 				ib_spec->eth.val.src_mac);
1951ee3da804SMaor Gottlieb 
1952038d2ef8SMaor Gottlieb 		if (ib_spec->eth.mask.vlan_tag) {
19532d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
195410543365SMohamad Haj Yahia 				 cvlan_tag, 1);
19552d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
195610543365SMohamad Haj Yahia 				 cvlan_tag, 1);
1957038d2ef8SMaor Gottlieb 
19582d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1959038d2ef8SMaor Gottlieb 				 first_vid, ntohs(ib_spec->eth.mask.vlan_tag));
19602d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1961038d2ef8SMaor Gottlieb 				 first_vid, ntohs(ib_spec->eth.val.vlan_tag));
1962038d2ef8SMaor Gottlieb 
19632d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1964038d2ef8SMaor Gottlieb 				 first_cfi,
1965038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.mask.vlan_tag) >> 12);
19662d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1967038d2ef8SMaor Gottlieb 				 first_cfi,
1968038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.val.vlan_tag) >> 12);
1969038d2ef8SMaor Gottlieb 
19702d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1971038d2ef8SMaor Gottlieb 				 first_prio,
1972038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.mask.vlan_tag) >> 13);
19732d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1974038d2ef8SMaor Gottlieb 				 first_prio,
1975038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.val.vlan_tag) >> 13);
1976038d2ef8SMaor Gottlieb 		}
19772d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1978038d2ef8SMaor Gottlieb 			 ethertype, ntohs(ib_spec->eth.mask.ether_type));
19792d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1980038d2ef8SMaor Gottlieb 			 ethertype, ntohs(ib_spec->eth.val.ether_type));
1981038d2ef8SMaor Gottlieb 		break;
1982038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_IPV4:
1983c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->ipv4.mask, LAST_IPV4_FIELD))
19841ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
1985038d2ef8SMaor Gottlieb 
198619cc7524SAriel Levkovich 		if (match_ipv) {
198719cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
198819cc7524SAriel Levkovich 				 ip_version, 0xf);
198919cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
199019cc7524SAriel Levkovich 				 ip_version, IPV4_VERSION);
199119cc7524SAriel Levkovich 		} else {
19922d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1993038d2ef8SMaor Gottlieb 				 ethertype, 0xffff);
19942d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1995038d2ef8SMaor Gottlieb 				 ethertype, ETH_P_IP);
199619cc7524SAriel Levkovich 		}
1997038d2ef8SMaor Gottlieb 
19982d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
1999038d2ef8SMaor Gottlieb 				    src_ipv4_src_ipv6.ipv4_layout.ipv4),
2000038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.mask.src_ip,
2001038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.mask.src_ip));
20022d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2003038d2ef8SMaor Gottlieb 				    src_ipv4_src_ipv6.ipv4_layout.ipv4),
2004038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.val.src_ip,
2005038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.val.src_ip));
20062d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2007038d2ef8SMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2008038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.mask.dst_ip,
2009038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.mask.dst_ip));
20102d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2011038d2ef8SMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2012038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.val.dst_ip,
2013038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.val.dst_ip));
2014ca0d4753SMaor Gottlieb 
20152d1e697eSMoses Reuben 		set_tos(headers_c, headers_v,
2016ca0d4753SMaor Gottlieb 			ib_spec->ipv4.mask.tos, ib_spec->ipv4.val.tos);
2017ca0d4753SMaor Gottlieb 
20182d1e697eSMoses Reuben 		set_proto(headers_c, headers_v,
2019ca0d4753SMaor Gottlieb 			  ib_spec->ipv4.mask.proto, ib_spec->ipv4.val.proto);
2020038d2ef8SMaor Gottlieb 		break;
2021026bae0cSMaor Gottlieb 	case IB_FLOW_SPEC_IPV6:
2022c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->ipv6.mask, LAST_IPV6_FIELD))
20231ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2024026bae0cSMaor Gottlieb 
202519cc7524SAriel Levkovich 		if (match_ipv) {
202619cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
202719cc7524SAriel Levkovich 				 ip_version, 0xf);
202819cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
202919cc7524SAriel Levkovich 				 ip_version, IPV6_VERSION);
203019cc7524SAriel Levkovich 		} else {
20312d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2032026bae0cSMaor Gottlieb 				 ethertype, 0xffff);
20332d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2034026bae0cSMaor Gottlieb 				 ethertype, ETH_P_IPV6);
203519cc7524SAriel Levkovich 		}
2036026bae0cSMaor Gottlieb 
20372d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2038026bae0cSMaor Gottlieb 				    src_ipv4_src_ipv6.ipv6_layout.ipv6),
2039026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.mask.src_ip,
2040026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.mask.src_ip));
20412d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2042026bae0cSMaor Gottlieb 				    src_ipv4_src_ipv6.ipv6_layout.ipv6),
2043026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.val.src_ip,
2044026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.val.src_ip));
20452d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2046026bae0cSMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2047026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.mask.dst_ip,
2048026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.mask.dst_ip));
20492d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2050026bae0cSMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2051026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.val.dst_ip,
2052026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.val.dst_ip));
2053466fa6d2SMaor Gottlieb 
20542d1e697eSMoses Reuben 		set_tos(headers_c, headers_v,
2055466fa6d2SMaor Gottlieb 			ib_spec->ipv6.mask.traffic_class,
2056466fa6d2SMaor Gottlieb 			ib_spec->ipv6.val.traffic_class);
2057466fa6d2SMaor Gottlieb 
20582d1e697eSMoses Reuben 		set_proto(headers_c, headers_v,
2059466fa6d2SMaor Gottlieb 			  ib_spec->ipv6.mask.next_hdr,
2060466fa6d2SMaor Gottlieb 			  ib_spec->ipv6.val.next_hdr);
2061466fa6d2SMaor Gottlieb 
20622d1e697eSMoses Reuben 		set_flow_label(misc_params_c, misc_params_v,
20632d1e697eSMoses Reuben 			       ntohl(ib_spec->ipv6.mask.flow_label),
20642d1e697eSMoses Reuben 			       ntohl(ib_spec->ipv6.val.flow_label),
20652d1e697eSMoses Reuben 			       ib_spec->type & IB_FLOW_SPEC_INNER);
20662d1e697eSMoses Reuben 
2067026bae0cSMaor Gottlieb 		break;
2068038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_TCP:
2069c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
2070c47ac6aeSMaor Gottlieb 					 LAST_TCP_UDP_FIELD))
20711ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2072038d2ef8SMaor Gottlieb 
20732d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
2074038d2ef8SMaor Gottlieb 			 0xff);
20752d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
2076038d2ef8SMaor Gottlieb 			 IPPROTO_TCP);
2077038d2ef8SMaor Gottlieb 
20782d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_sport,
2079038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.src_port));
20802d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_sport,
2081038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.src_port));
2082038d2ef8SMaor Gottlieb 
20832d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_dport,
2084038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.dst_port));
20852d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_dport,
2086038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.dst_port));
2087038d2ef8SMaor Gottlieb 		break;
2088038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_UDP:
2089c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
2090c47ac6aeSMaor Gottlieb 					 LAST_TCP_UDP_FIELD))
20911ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2092038d2ef8SMaor Gottlieb 
20932d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
2094038d2ef8SMaor Gottlieb 			 0xff);
20952d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
2096038d2ef8SMaor Gottlieb 			 IPPROTO_UDP);
2097038d2ef8SMaor Gottlieb 
20982d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_sport,
2099038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.src_port));
21002d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_sport,
2101038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.src_port));
2102038d2ef8SMaor Gottlieb 
21032d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_dport,
2104038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.dst_port));
21052d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport,
2106038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.dst_port));
2107038d2ef8SMaor Gottlieb 		break;
2108ffb30d8fSMoses Reuben 	case IB_FLOW_SPEC_VXLAN_TUNNEL:
2109ffb30d8fSMoses Reuben 		if (FIELDS_NOT_SUPPORTED(ib_spec->tunnel.mask,
2110ffb30d8fSMoses Reuben 					 LAST_TUNNEL_FIELD))
21111ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2112ffb30d8fSMoses Reuben 
2113ffb30d8fSMoses Reuben 		MLX5_SET(fte_match_set_misc, misc_params_c, vxlan_vni,
2114ffb30d8fSMoses Reuben 			 ntohl(ib_spec->tunnel.mask.tunnel_id));
2115ffb30d8fSMoses Reuben 		MLX5_SET(fte_match_set_misc, misc_params_v, vxlan_vni,
2116ffb30d8fSMoses Reuben 			 ntohl(ib_spec->tunnel.val.tunnel_id));
2117ffb30d8fSMoses Reuben 		break;
21182ac693f9SMoses Reuben 	case IB_FLOW_SPEC_ACTION_TAG:
21192ac693f9SMoses Reuben 		if (FIELDS_NOT_SUPPORTED(ib_spec->flow_tag,
21202ac693f9SMoses Reuben 					 LAST_FLOW_TAG_FIELD))
21212ac693f9SMoses Reuben 			return -EOPNOTSUPP;
21222ac693f9SMoses Reuben 		if (ib_spec->flow_tag.tag_id >= BIT(24))
21232ac693f9SMoses Reuben 			return -EINVAL;
21242ac693f9SMoses Reuben 
21252ac693f9SMoses Reuben 		*tag_id = ib_spec->flow_tag.tag_id;
21262ac693f9SMoses Reuben 		break;
2127a22ed86cSSlava Shwartsman 	case IB_FLOW_SPEC_ACTION_DROP:
2128a22ed86cSSlava Shwartsman 		if (FIELDS_NOT_SUPPORTED(ib_spec->drop,
2129a22ed86cSSlava Shwartsman 					 LAST_DROP_FIELD))
2130a22ed86cSSlava Shwartsman 			return -EOPNOTSUPP;
2131a22ed86cSSlava Shwartsman 		*is_drop = true;
2132a22ed86cSSlava Shwartsman 		break;
2133038d2ef8SMaor Gottlieb 	default:
2134038d2ef8SMaor Gottlieb 		return -EINVAL;
2135038d2ef8SMaor Gottlieb 	}
2136038d2ef8SMaor Gottlieb 
2137038d2ef8SMaor Gottlieb 	return 0;
2138038d2ef8SMaor Gottlieb }
2139038d2ef8SMaor Gottlieb 
2140038d2ef8SMaor Gottlieb /* If a flow could catch both multicast and unicast packets,
2141038d2ef8SMaor Gottlieb  * it won't fall into the multicast flow steering table and this rule
2142038d2ef8SMaor Gottlieb  * could steal other multicast packets.
2143038d2ef8SMaor Gottlieb  */
2144a550ddfcSYishai Hadas static bool flow_is_multicast_only(const struct ib_flow_attr *ib_attr)
2145038d2ef8SMaor Gottlieb {
214681e30880SYishai Hadas 	union ib_flow_spec *flow_spec;
2147038d2ef8SMaor Gottlieb 
2148038d2ef8SMaor Gottlieb 	if (ib_attr->type != IB_FLOW_ATTR_NORMAL ||
2149038d2ef8SMaor Gottlieb 	    ib_attr->num_of_specs < 1)
2150038d2ef8SMaor Gottlieb 		return false;
2151038d2ef8SMaor Gottlieb 
215281e30880SYishai Hadas 	flow_spec = (union ib_flow_spec *)(ib_attr + 1);
215381e30880SYishai Hadas 	if (flow_spec->type == IB_FLOW_SPEC_IPV4) {
215481e30880SYishai Hadas 		struct ib_flow_spec_ipv4 *ipv4_spec;
2155038d2ef8SMaor Gottlieb 
215681e30880SYishai Hadas 		ipv4_spec = (struct ib_flow_spec_ipv4 *)flow_spec;
215781e30880SYishai Hadas 		if (ipv4_is_multicast(ipv4_spec->val.dst_ip))
215881e30880SYishai Hadas 			return true;
215981e30880SYishai Hadas 
216081e30880SYishai Hadas 		return false;
216181e30880SYishai Hadas 	}
216281e30880SYishai Hadas 
216381e30880SYishai Hadas 	if (flow_spec->type == IB_FLOW_SPEC_ETH) {
216481e30880SYishai Hadas 		struct ib_flow_spec_eth *eth_spec;
216581e30880SYishai Hadas 
216681e30880SYishai Hadas 		eth_spec = (struct ib_flow_spec_eth *)flow_spec;
2167038d2ef8SMaor Gottlieb 		return is_multicast_ether_addr(eth_spec->mask.dst_mac) &&
2168038d2ef8SMaor Gottlieb 		       is_multicast_ether_addr(eth_spec->val.dst_mac);
2169038d2ef8SMaor Gottlieb 	}
2170038d2ef8SMaor Gottlieb 
217181e30880SYishai Hadas 	return false;
217281e30880SYishai Hadas }
217381e30880SYishai Hadas 
217419cc7524SAriel Levkovich static bool is_valid_ethertype(struct mlx5_core_dev *mdev,
217519cc7524SAriel Levkovich 			       const struct ib_flow_attr *flow_attr,
21760f750966SAriel Levkovich 			       bool check_inner)
2177038d2ef8SMaor Gottlieb {
2178038d2ef8SMaor Gottlieb 	union ib_flow_spec *ib_spec = (union ib_flow_spec *)(flow_attr + 1);
217919cc7524SAriel Levkovich 	int match_ipv = check_inner ?
218019cc7524SAriel Levkovich 			MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
218119cc7524SAriel Levkovich 					ft_field_support.inner_ip_version) :
218219cc7524SAriel Levkovich 			MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
218319cc7524SAriel Levkovich 					ft_field_support.outer_ip_version);
21840f750966SAriel Levkovich 	int inner_bit = check_inner ? IB_FLOW_SPEC_INNER : 0;
21850f750966SAriel Levkovich 	bool ipv4_spec_valid, ipv6_spec_valid;
21860f750966SAriel Levkovich 	unsigned int ip_spec_type = 0;
21870f750966SAriel Levkovich 	bool has_ethertype = false;
2188038d2ef8SMaor Gottlieb 	unsigned int spec_index;
21890f750966SAriel Levkovich 	bool mask_valid = true;
21900f750966SAriel Levkovich 	u16 eth_type = 0;
21910f750966SAriel Levkovich 	bool type_valid;
2192038d2ef8SMaor Gottlieb 
2193038d2ef8SMaor Gottlieb 	/* Validate that ethertype is correct */
2194038d2ef8SMaor Gottlieb 	for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
21950f750966SAriel Levkovich 		if ((ib_spec->type == (IB_FLOW_SPEC_ETH | inner_bit)) &&
2196038d2ef8SMaor Gottlieb 		    ib_spec->eth.mask.ether_type) {
21970f750966SAriel Levkovich 			mask_valid = (ib_spec->eth.mask.ether_type ==
21980f750966SAriel Levkovich 				      htons(0xffff));
21990f750966SAriel Levkovich 			has_ethertype = true;
22000f750966SAriel Levkovich 			eth_type = ntohs(ib_spec->eth.val.ether_type);
22010f750966SAriel Levkovich 		} else if ((ib_spec->type == (IB_FLOW_SPEC_IPV4 | inner_bit)) ||
22020f750966SAriel Levkovich 			   (ib_spec->type == (IB_FLOW_SPEC_IPV6 | inner_bit))) {
22030f750966SAriel Levkovich 			ip_spec_type = ib_spec->type;
2204038d2ef8SMaor Gottlieb 		}
2205038d2ef8SMaor Gottlieb 		ib_spec = (void *)ib_spec + ib_spec->size;
2206038d2ef8SMaor Gottlieb 	}
22070f750966SAriel Levkovich 
22080f750966SAriel Levkovich 	type_valid = (!has_ethertype) || (!ip_spec_type);
22090f750966SAriel Levkovich 	if (!type_valid && mask_valid) {
22100f750966SAriel Levkovich 		ipv4_spec_valid = (eth_type == ETH_P_IP) &&
22110f750966SAriel Levkovich 			(ip_spec_type == (IB_FLOW_SPEC_IPV4 | inner_bit));
22120f750966SAriel Levkovich 		ipv6_spec_valid = (eth_type == ETH_P_IPV6) &&
22130f750966SAriel Levkovich 			(ip_spec_type == (IB_FLOW_SPEC_IPV6 | inner_bit));
221419cc7524SAriel Levkovich 
221519cc7524SAriel Levkovich 		type_valid = (ipv4_spec_valid) || (ipv6_spec_valid) ||
221619cc7524SAriel Levkovich 			     (((eth_type == ETH_P_MPLS_UC) ||
221719cc7524SAriel Levkovich 			       (eth_type == ETH_P_MPLS_MC)) && match_ipv);
22180f750966SAriel Levkovich 	}
22190f750966SAriel Levkovich 
22200f750966SAriel Levkovich 	return type_valid;
22210f750966SAriel Levkovich }
22220f750966SAriel Levkovich 
222319cc7524SAriel Levkovich static bool is_valid_attr(struct mlx5_core_dev *mdev,
222419cc7524SAriel Levkovich 			  const struct ib_flow_attr *flow_attr)
22250f750966SAriel Levkovich {
222619cc7524SAriel Levkovich 	return is_valid_ethertype(mdev, flow_attr, false) &&
222719cc7524SAriel Levkovich 	       is_valid_ethertype(mdev, flow_attr, true);
2228038d2ef8SMaor Gottlieb }
2229038d2ef8SMaor Gottlieb 
2230038d2ef8SMaor Gottlieb static void put_flow_table(struct mlx5_ib_dev *dev,
2231038d2ef8SMaor Gottlieb 			   struct mlx5_ib_flow_prio *prio, bool ft_added)
2232038d2ef8SMaor Gottlieb {
2233038d2ef8SMaor Gottlieb 	prio->refcount -= !!ft_added;
2234038d2ef8SMaor Gottlieb 	if (!prio->refcount) {
2235038d2ef8SMaor Gottlieb 		mlx5_destroy_flow_table(prio->flow_table);
2236038d2ef8SMaor Gottlieb 		prio->flow_table = NULL;
2237038d2ef8SMaor Gottlieb 	}
2238038d2ef8SMaor Gottlieb }
2239038d2ef8SMaor Gottlieb 
2240038d2ef8SMaor Gottlieb static int mlx5_ib_destroy_flow(struct ib_flow *flow_id)
2241038d2ef8SMaor Gottlieb {
2242038d2ef8SMaor Gottlieb 	struct mlx5_ib_dev *dev = to_mdev(flow_id->qp->device);
2243038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = container_of(flow_id,
2244038d2ef8SMaor Gottlieb 							  struct mlx5_ib_flow_handler,
2245038d2ef8SMaor Gottlieb 							  ibflow);
2246038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *iter, *tmp;
2247038d2ef8SMaor Gottlieb 
2248038d2ef8SMaor Gottlieb 	mutex_lock(&dev->flow_db.lock);
2249038d2ef8SMaor Gottlieb 
2250038d2ef8SMaor Gottlieb 	list_for_each_entry_safe(iter, tmp, &handler->list, list) {
225174491de9SMark Bloch 		mlx5_del_flow_rules(iter->rule);
2252cc0e5d42SMaor Gottlieb 		put_flow_table(dev, iter->prio, true);
2253038d2ef8SMaor Gottlieb 		list_del(&iter->list);
2254038d2ef8SMaor Gottlieb 		kfree(iter);
2255038d2ef8SMaor Gottlieb 	}
2256038d2ef8SMaor Gottlieb 
225774491de9SMark Bloch 	mlx5_del_flow_rules(handler->rule);
22585497adc6SMaor Gottlieb 	put_flow_table(dev, handler->prio, true);
2259038d2ef8SMaor Gottlieb 	mutex_unlock(&dev->flow_db.lock);
2260038d2ef8SMaor Gottlieb 
2261038d2ef8SMaor Gottlieb 	kfree(handler);
2262038d2ef8SMaor Gottlieb 
2263038d2ef8SMaor Gottlieb 	return 0;
2264038d2ef8SMaor Gottlieb }
2265038d2ef8SMaor Gottlieb 
226635d19011SMaor Gottlieb static int ib_prio_to_core_prio(unsigned int priority, bool dont_trap)
226735d19011SMaor Gottlieb {
226835d19011SMaor Gottlieb 	priority *= 2;
226935d19011SMaor Gottlieb 	if (!dont_trap)
227035d19011SMaor Gottlieb 		priority++;
227135d19011SMaor Gottlieb 	return priority;
227235d19011SMaor Gottlieb }
227335d19011SMaor Gottlieb 
2274cc0e5d42SMaor Gottlieb enum flow_table_type {
2275cc0e5d42SMaor Gottlieb 	MLX5_IB_FT_RX,
2276cc0e5d42SMaor Gottlieb 	MLX5_IB_FT_TX
2277cc0e5d42SMaor Gottlieb };
2278cc0e5d42SMaor Gottlieb 
227900b7c2abSMaor Gottlieb #define MLX5_FS_MAX_TYPES	 6
228000b7c2abSMaor Gottlieb #define MLX5_FS_MAX_ENTRIES	 BIT(16)
2281038d2ef8SMaor Gottlieb static struct mlx5_ib_flow_prio *get_flow_table(struct mlx5_ib_dev *dev,
2282cc0e5d42SMaor Gottlieb 						struct ib_flow_attr *flow_attr,
2283cc0e5d42SMaor Gottlieb 						enum flow_table_type ft_type)
2284038d2ef8SMaor Gottlieb {
228535d19011SMaor Gottlieb 	bool dont_trap = flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP;
2286038d2ef8SMaor Gottlieb 	struct mlx5_flow_namespace *ns = NULL;
2287038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_prio *prio;
2288038d2ef8SMaor Gottlieb 	struct mlx5_flow_table *ft;
2289dac388efSMaor Gottlieb 	int max_table_size;
2290038d2ef8SMaor Gottlieb 	int num_entries;
2291038d2ef8SMaor Gottlieb 	int num_groups;
2292038d2ef8SMaor Gottlieb 	int priority;
2293038d2ef8SMaor Gottlieb 	int err = 0;
2294038d2ef8SMaor Gottlieb 
2295dac388efSMaor Gottlieb 	max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
2296dac388efSMaor Gottlieb 						       log_max_ft_size));
2297038d2ef8SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
229835d19011SMaor Gottlieb 		if (flow_is_multicast_only(flow_attr) &&
229935d19011SMaor Gottlieb 		    !dont_trap)
2300038d2ef8SMaor Gottlieb 			priority = MLX5_IB_FLOW_MCAST_PRIO;
2301038d2ef8SMaor Gottlieb 		else
230235d19011SMaor Gottlieb 			priority = ib_prio_to_core_prio(flow_attr->priority,
230335d19011SMaor Gottlieb 							dont_trap);
2304038d2ef8SMaor Gottlieb 		ns = mlx5_get_flow_namespace(dev->mdev,
2305038d2ef8SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_BYPASS);
2306038d2ef8SMaor Gottlieb 		num_entries = MLX5_FS_MAX_ENTRIES;
2307038d2ef8SMaor Gottlieb 		num_groups = MLX5_FS_MAX_TYPES;
2308038d2ef8SMaor Gottlieb 		prio = &dev->flow_db.prios[priority];
2309038d2ef8SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
2310038d2ef8SMaor Gottlieb 		   flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
2311038d2ef8SMaor Gottlieb 		ns = mlx5_get_flow_namespace(dev->mdev,
2312038d2ef8SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_LEFTOVERS);
2313038d2ef8SMaor Gottlieb 		build_leftovers_ft_param(&priority,
2314038d2ef8SMaor Gottlieb 					 &num_entries,
2315038d2ef8SMaor Gottlieb 					 &num_groups);
2316038d2ef8SMaor Gottlieb 		prio = &dev->flow_db.prios[MLX5_IB_FLOW_LEFTOVERS_PRIO];
2317cc0e5d42SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
2318cc0e5d42SMaor Gottlieb 		if (!MLX5_CAP_FLOWTABLE(dev->mdev,
2319cc0e5d42SMaor Gottlieb 					allow_sniffer_and_nic_rx_shared_tir))
2320cc0e5d42SMaor Gottlieb 			return ERR_PTR(-ENOTSUPP);
2321cc0e5d42SMaor Gottlieb 
2322cc0e5d42SMaor Gottlieb 		ns = mlx5_get_flow_namespace(dev->mdev, ft_type == MLX5_IB_FT_RX ?
2323cc0e5d42SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_SNIFFER_RX :
2324cc0e5d42SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_SNIFFER_TX);
2325cc0e5d42SMaor Gottlieb 
2326cc0e5d42SMaor Gottlieb 		prio = &dev->flow_db.sniffer[ft_type];
2327cc0e5d42SMaor Gottlieb 		priority = 0;
2328cc0e5d42SMaor Gottlieb 		num_entries = 1;
2329cc0e5d42SMaor Gottlieb 		num_groups = 1;
2330038d2ef8SMaor Gottlieb 	}
2331038d2ef8SMaor Gottlieb 
2332038d2ef8SMaor Gottlieb 	if (!ns)
2333038d2ef8SMaor Gottlieb 		return ERR_PTR(-ENOTSUPP);
2334038d2ef8SMaor Gottlieb 
2335dac388efSMaor Gottlieb 	if (num_entries > max_table_size)
2336dac388efSMaor Gottlieb 		return ERR_PTR(-ENOMEM);
2337dac388efSMaor Gottlieb 
2338038d2ef8SMaor Gottlieb 	ft = prio->flow_table;
2339038d2ef8SMaor Gottlieb 	if (!ft) {
2340038d2ef8SMaor Gottlieb 		ft = mlx5_create_auto_grouped_flow_table(ns, priority,
2341038d2ef8SMaor Gottlieb 							 num_entries,
2342d63cd286SMaor Gottlieb 							 num_groups,
2343c9f1b073SHadar Hen Zion 							 0, 0);
2344038d2ef8SMaor Gottlieb 
2345038d2ef8SMaor Gottlieb 		if (!IS_ERR(ft)) {
2346038d2ef8SMaor Gottlieb 			prio->refcount = 0;
2347038d2ef8SMaor Gottlieb 			prio->flow_table = ft;
2348038d2ef8SMaor Gottlieb 		} else {
2349038d2ef8SMaor Gottlieb 			err = PTR_ERR(ft);
2350038d2ef8SMaor Gottlieb 		}
2351038d2ef8SMaor Gottlieb 	}
2352038d2ef8SMaor Gottlieb 
2353038d2ef8SMaor Gottlieb 	return err ? ERR_PTR(err) : prio;
2354038d2ef8SMaor Gottlieb }
2355038d2ef8SMaor Gottlieb 
2356a550ddfcSYishai Hadas static void set_underlay_qp(struct mlx5_ib_dev *dev,
2357a550ddfcSYishai Hadas 			    struct mlx5_flow_spec *spec,
2358a550ddfcSYishai Hadas 			    u32 underlay_qpn)
2359a550ddfcSYishai Hadas {
2360a550ddfcSYishai Hadas 	void *misc_params_c = MLX5_ADDR_OF(fte_match_param,
2361a550ddfcSYishai Hadas 					   spec->match_criteria,
2362a550ddfcSYishai Hadas 					   misc_parameters);
2363a550ddfcSYishai Hadas 	void *misc_params_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
2364a550ddfcSYishai Hadas 					   misc_parameters);
2365a550ddfcSYishai Hadas 
2366a550ddfcSYishai Hadas 	if (underlay_qpn &&
2367a550ddfcSYishai Hadas 	    MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
2368a550ddfcSYishai Hadas 				      ft_field_support.bth_dst_qp)) {
2369a550ddfcSYishai Hadas 		MLX5_SET(fte_match_set_misc,
2370a550ddfcSYishai Hadas 			 misc_params_v, bth_dst_qp, underlay_qpn);
2371a550ddfcSYishai Hadas 		MLX5_SET(fte_match_set_misc,
2372a550ddfcSYishai Hadas 			 misc_params_c, bth_dst_qp, 0xffffff);
2373a550ddfcSYishai Hadas 	}
2374a550ddfcSYishai Hadas }
2375a550ddfcSYishai Hadas 
2376a550ddfcSYishai Hadas static struct mlx5_ib_flow_handler *_create_flow_rule(struct mlx5_ib_dev *dev,
2377038d2ef8SMaor Gottlieb 						      struct mlx5_ib_flow_prio *ft_prio,
2378dd063d0eSMaor Gottlieb 						      const struct ib_flow_attr *flow_attr,
2379a550ddfcSYishai Hadas 						      struct mlx5_flow_destination *dst,
2380a550ddfcSYishai Hadas 						      u32 underlay_qpn)
2381038d2ef8SMaor Gottlieb {
2382038d2ef8SMaor Gottlieb 	struct mlx5_flow_table	*ft = ft_prio->flow_table;
2383038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler;
238466958ed9SHadar Hen Zion 	struct mlx5_flow_act flow_act = {0};
2385c5bb1730SMaor Gottlieb 	struct mlx5_flow_spec *spec;
2386a22ed86cSSlava Shwartsman 	struct mlx5_flow_destination *rule_dst = dst;
2387dd063d0eSMaor Gottlieb 	const void *ib_flow = (const void *)flow_attr + sizeof(*flow_attr);
2388038d2ef8SMaor Gottlieb 	unsigned int spec_index;
23892ac693f9SMoses Reuben 	u32 flow_tag = MLX5_FS_DEFAULT_FLOW_TAG;
2390a22ed86cSSlava Shwartsman 	bool is_drop = false;
2391038d2ef8SMaor Gottlieb 	int err = 0;
2392a22ed86cSSlava Shwartsman 	int dest_num = 1;
2393038d2ef8SMaor Gottlieb 
239419cc7524SAriel Levkovich 	if (!is_valid_attr(dev->mdev, flow_attr))
2395038d2ef8SMaor Gottlieb 		return ERR_PTR(-EINVAL);
2396038d2ef8SMaor Gottlieb 
23971b9a07eeSLeon Romanovsky 	spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
2398038d2ef8SMaor Gottlieb 	handler = kzalloc(sizeof(*handler), GFP_KERNEL);
2399c5bb1730SMaor Gottlieb 	if (!handler || !spec) {
2400038d2ef8SMaor Gottlieb 		err = -ENOMEM;
2401038d2ef8SMaor Gottlieb 		goto free;
2402038d2ef8SMaor Gottlieb 	}
2403038d2ef8SMaor Gottlieb 
2404038d2ef8SMaor Gottlieb 	INIT_LIST_HEAD(&handler->list);
2405038d2ef8SMaor Gottlieb 
2406038d2ef8SMaor Gottlieb 	for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
240719cc7524SAriel Levkovich 		err = parse_flow_attr(dev->mdev, spec->match_criteria,
2408a22ed86cSSlava Shwartsman 				      spec->match_value,
2409a22ed86cSSlava Shwartsman 				      ib_flow, &flow_tag, &is_drop);
2410038d2ef8SMaor Gottlieb 		if (err < 0)
2411038d2ef8SMaor Gottlieb 			goto free;
2412038d2ef8SMaor Gottlieb 
2413038d2ef8SMaor Gottlieb 		ib_flow += ((union ib_flow_spec *)ib_flow)->size;
2414038d2ef8SMaor Gottlieb 	}
2415038d2ef8SMaor Gottlieb 
2416a550ddfcSYishai Hadas 	if (!flow_is_multicast_only(flow_attr))
2417a550ddfcSYishai Hadas 		set_underlay_qp(dev, spec, underlay_qpn);
2418a550ddfcSYishai Hadas 
2419466fa6d2SMaor Gottlieb 	spec->match_criteria_enable = get_match_criteria_enable(spec->match_criteria);
2420a22ed86cSSlava Shwartsman 	if (is_drop) {
2421a22ed86cSSlava Shwartsman 		flow_act.action = MLX5_FLOW_CONTEXT_ACTION_DROP;
2422a22ed86cSSlava Shwartsman 		rule_dst = NULL;
2423a22ed86cSSlava Shwartsman 		dest_num = 0;
2424a22ed86cSSlava Shwartsman 	} else {
242566958ed9SHadar Hen Zion 		flow_act.action = dst ? MLX5_FLOW_CONTEXT_ACTION_FWD_DEST :
242635d19011SMaor Gottlieb 		    MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO;
2427a22ed86cSSlava Shwartsman 	}
24282ac693f9SMoses Reuben 
24292ac693f9SMoses Reuben 	if (flow_tag != MLX5_FS_DEFAULT_FLOW_TAG &&
24302ac693f9SMoses Reuben 	    (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
24312ac693f9SMoses Reuben 	     flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT)) {
24322ac693f9SMoses Reuben 		mlx5_ib_warn(dev, "Flow tag %u and attribute type %x isn't allowed in leftovers\n",
24332ac693f9SMoses Reuben 			     flow_tag, flow_attr->type);
24342ac693f9SMoses Reuben 		err = -EINVAL;
24352ac693f9SMoses Reuben 		goto free;
24362ac693f9SMoses Reuben 	}
24372ac693f9SMoses Reuben 	flow_act.flow_tag = flow_tag;
243874491de9SMark Bloch 	handler->rule = mlx5_add_flow_rules(ft, spec,
243966958ed9SHadar Hen Zion 					    &flow_act,
2440a22ed86cSSlava Shwartsman 					    rule_dst, dest_num);
2441038d2ef8SMaor Gottlieb 
2442038d2ef8SMaor Gottlieb 	if (IS_ERR(handler->rule)) {
2443038d2ef8SMaor Gottlieb 		err = PTR_ERR(handler->rule);
2444038d2ef8SMaor Gottlieb 		goto free;
2445038d2ef8SMaor Gottlieb 	}
2446038d2ef8SMaor Gottlieb 
2447d9d4980aSMaor Gottlieb 	ft_prio->refcount++;
24485497adc6SMaor Gottlieb 	handler->prio = ft_prio;
2449038d2ef8SMaor Gottlieb 
2450038d2ef8SMaor Gottlieb 	ft_prio->flow_table = ft;
2451038d2ef8SMaor Gottlieb free:
2452038d2ef8SMaor Gottlieb 	if (err)
2453038d2ef8SMaor Gottlieb 		kfree(handler);
2454c5bb1730SMaor Gottlieb 	kvfree(spec);
2455038d2ef8SMaor Gottlieb 	return err ? ERR_PTR(err) : handler;
2456038d2ef8SMaor Gottlieb }
2457038d2ef8SMaor Gottlieb 
2458a550ddfcSYishai Hadas static struct mlx5_ib_flow_handler *create_flow_rule(struct mlx5_ib_dev *dev,
2459a550ddfcSYishai Hadas 						     struct mlx5_ib_flow_prio *ft_prio,
2460a550ddfcSYishai Hadas 						     const struct ib_flow_attr *flow_attr,
2461a550ddfcSYishai Hadas 						     struct mlx5_flow_destination *dst)
2462a550ddfcSYishai Hadas {
2463a550ddfcSYishai Hadas 	return _create_flow_rule(dev, ft_prio, flow_attr, dst, 0);
2464a550ddfcSYishai Hadas }
2465a550ddfcSYishai Hadas 
246635d19011SMaor Gottlieb static struct mlx5_ib_flow_handler *create_dont_trap_rule(struct mlx5_ib_dev *dev,
246735d19011SMaor Gottlieb 							  struct mlx5_ib_flow_prio *ft_prio,
246835d19011SMaor Gottlieb 							  struct ib_flow_attr *flow_attr,
246935d19011SMaor Gottlieb 							  struct mlx5_flow_destination *dst)
247035d19011SMaor Gottlieb {
247135d19011SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_dst = NULL;
247235d19011SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
247335d19011SMaor Gottlieb 
247435d19011SMaor Gottlieb 	handler = create_flow_rule(dev, ft_prio, flow_attr, NULL);
247535d19011SMaor Gottlieb 	if (!IS_ERR(handler)) {
247635d19011SMaor Gottlieb 		handler_dst = create_flow_rule(dev, ft_prio,
247735d19011SMaor Gottlieb 					       flow_attr, dst);
247835d19011SMaor Gottlieb 		if (IS_ERR(handler_dst)) {
247974491de9SMark Bloch 			mlx5_del_flow_rules(handler->rule);
2480d9d4980aSMaor Gottlieb 			ft_prio->refcount--;
248135d19011SMaor Gottlieb 			kfree(handler);
248235d19011SMaor Gottlieb 			handler = handler_dst;
248335d19011SMaor Gottlieb 		} else {
248435d19011SMaor Gottlieb 			list_add(&handler_dst->list, &handler->list);
248535d19011SMaor Gottlieb 		}
248635d19011SMaor Gottlieb 	}
248735d19011SMaor Gottlieb 
248835d19011SMaor Gottlieb 	return handler;
248935d19011SMaor Gottlieb }
2490038d2ef8SMaor Gottlieb enum {
2491038d2ef8SMaor Gottlieb 	LEFTOVERS_MC,
2492038d2ef8SMaor Gottlieb 	LEFTOVERS_UC,
2493038d2ef8SMaor Gottlieb };
2494038d2ef8SMaor Gottlieb 
2495038d2ef8SMaor Gottlieb static struct mlx5_ib_flow_handler *create_leftovers_rule(struct mlx5_ib_dev *dev,
2496038d2ef8SMaor Gottlieb 							  struct mlx5_ib_flow_prio *ft_prio,
2497038d2ef8SMaor Gottlieb 							  struct ib_flow_attr *flow_attr,
2498038d2ef8SMaor Gottlieb 							  struct mlx5_flow_destination *dst)
2499038d2ef8SMaor Gottlieb {
2500038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_ucast = NULL;
2501038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
2502038d2ef8SMaor Gottlieb 
2503038d2ef8SMaor Gottlieb 	static struct {
2504038d2ef8SMaor Gottlieb 		struct ib_flow_attr	flow_attr;
2505038d2ef8SMaor Gottlieb 		struct ib_flow_spec_eth eth_flow;
2506038d2ef8SMaor Gottlieb 	} leftovers_specs[] = {
2507038d2ef8SMaor Gottlieb 		[LEFTOVERS_MC] = {
2508038d2ef8SMaor Gottlieb 			.flow_attr = {
2509038d2ef8SMaor Gottlieb 				.num_of_specs = 1,
2510038d2ef8SMaor Gottlieb 				.size = sizeof(leftovers_specs[0])
2511038d2ef8SMaor Gottlieb 			},
2512038d2ef8SMaor Gottlieb 			.eth_flow = {
2513038d2ef8SMaor Gottlieb 				.type = IB_FLOW_SPEC_ETH,
2514038d2ef8SMaor Gottlieb 				.size = sizeof(struct ib_flow_spec_eth),
2515038d2ef8SMaor Gottlieb 				.mask = {.dst_mac = {0x1} },
2516038d2ef8SMaor Gottlieb 				.val =  {.dst_mac = {0x1} }
2517038d2ef8SMaor Gottlieb 			}
2518038d2ef8SMaor Gottlieb 		},
2519038d2ef8SMaor Gottlieb 		[LEFTOVERS_UC] = {
2520038d2ef8SMaor Gottlieb 			.flow_attr = {
2521038d2ef8SMaor Gottlieb 				.num_of_specs = 1,
2522038d2ef8SMaor Gottlieb 				.size = sizeof(leftovers_specs[0])
2523038d2ef8SMaor Gottlieb 			},
2524038d2ef8SMaor Gottlieb 			.eth_flow = {
2525038d2ef8SMaor Gottlieb 				.type = IB_FLOW_SPEC_ETH,
2526038d2ef8SMaor Gottlieb 				.size = sizeof(struct ib_flow_spec_eth),
2527038d2ef8SMaor Gottlieb 				.mask = {.dst_mac = {0x1} },
2528038d2ef8SMaor Gottlieb 				.val = {.dst_mac = {} }
2529038d2ef8SMaor Gottlieb 			}
2530038d2ef8SMaor Gottlieb 		}
2531038d2ef8SMaor Gottlieb 	};
2532038d2ef8SMaor Gottlieb 
2533038d2ef8SMaor Gottlieb 	handler = create_flow_rule(dev, ft_prio,
2534038d2ef8SMaor Gottlieb 				   &leftovers_specs[LEFTOVERS_MC].flow_attr,
2535038d2ef8SMaor Gottlieb 				   dst);
2536038d2ef8SMaor Gottlieb 	if (!IS_ERR(handler) &&
2537038d2ef8SMaor Gottlieb 	    flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT) {
2538038d2ef8SMaor Gottlieb 		handler_ucast = create_flow_rule(dev, ft_prio,
2539038d2ef8SMaor Gottlieb 						 &leftovers_specs[LEFTOVERS_UC].flow_attr,
2540038d2ef8SMaor Gottlieb 						 dst);
2541038d2ef8SMaor Gottlieb 		if (IS_ERR(handler_ucast)) {
254274491de9SMark Bloch 			mlx5_del_flow_rules(handler->rule);
2543d9d4980aSMaor Gottlieb 			ft_prio->refcount--;
2544038d2ef8SMaor Gottlieb 			kfree(handler);
2545038d2ef8SMaor Gottlieb 			handler = handler_ucast;
2546038d2ef8SMaor Gottlieb 		} else {
2547038d2ef8SMaor Gottlieb 			list_add(&handler_ucast->list, &handler->list);
2548038d2ef8SMaor Gottlieb 		}
2549038d2ef8SMaor Gottlieb 	}
2550038d2ef8SMaor Gottlieb 
2551038d2ef8SMaor Gottlieb 	return handler;
2552038d2ef8SMaor Gottlieb }
2553038d2ef8SMaor Gottlieb 
2554cc0e5d42SMaor Gottlieb static struct mlx5_ib_flow_handler *create_sniffer_rule(struct mlx5_ib_dev *dev,
2555cc0e5d42SMaor Gottlieb 							struct mlx5_ib_flow_prio *ft_rx,
2556cc0e5d42SMaor Gottlieb 							struct mlx5_ib_flow_prio *ft_tx,
2557cc0e5d42SMaor Gottlieb 							struct mlx5_flow_destination *dst)
2558cc0e5d42SMaor Gottlieb {
2559cc0e5d42SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_rx;
2560cc0e5d42SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_tx;
2561cc0e5d42SMaor Gottlieb 	int err;
2562cc0e5d42SMaor Gottlieb 	static const struct ib_flow_attr flow_attr  = {
2563cc0e5d42SMaor Gottlieb 		.num_of_specs = 0,
2564cc0e5d42SMaor Gottlieb 		.size = sizeof(flow_attr)
2565cc0e5d42SMaor Gottlieb 	};
2566cc0e5d42SMaor Gottlieb 
2567cc0e5d42SMaor Gottlieb 	handler_rx = create_flow_rule(dev, ft_rx, &flow_attr, dst);
2568cc0e5d42SMaor Gottlieb 	if (IS_ERR(handler_rx)) {
2569cc0e5d42SMaor Gottlieb 		err = PTR_ERR(handler_rx);
2570cc0e5d42SMaor Gottlieb 		goto err;
2571cc0e5d42SMaor Gottlieb 	}
2572cc0e5d42SMaor Gottlieb 
2573cc0e5d42SMaor Gottlieb 	handler_tx = create_flow_rule(dev, ft_tx, &flow_attr, dst);
2574cc0e5d42SMaor Gottlieb 	if (IS_ERR(handler_tx)) {
2575cc0e5d42SMaor Gottlieb 		err = PTR_ERR(handler_tx);
2576cc0e5d42SMaor Gottlieb 		goto err_tx;
2577cc0e5d42SMaor Gottlieb 	}
2578cc0e5d42SMaor Gottlieb 
2579cc0e5d42SMaor Gottlieb 	list_add(&handler_tx->list, &handler_rx->list);
2580cc0e5d42SMaor Gottlieb 
2581cc0e5d42SMaor Gottlieb 	return handler_rx;
2582cc0e5d42SMaor Gottlieb 
2583cc0e5d42SMaor Gottlieb err_tx:
258474491de9SMark Bloch 	mlx5_del_flow_rules(handler_rx->rule);
2585cc0e5d42SMaor Gottlieb 	ft_rx->refcount--;
2586cc0e5d42SMaor Gottlieb 	kfree(handler_rx);
2587cc0e5d42SMaor Gottlieb err:
2588cc0e5d42SMaor Gottlieb 	return ERR_PTR(err);
2589cc0e5d42SMaor Gottlieb }
2590cc0e5d42SMaor Gottlieb 
2591038d2ef8SMaor Gottlieb static struct ib_flow *mlx5_ib_create_flow(struct ib_qp *qp,
2592038d2ef8SMaor Gottlieb 					   struct ib_flow_attr *flow_attr,
2593038d2ef8SMaor Gottlieb 					   int domain)
2594038d2ef8SMaor Gottlieb {
2595038d2ef8SMaor Gottlieb 	struct mlx5_ib_dev *dev = to_mdev(qp->device);
2596d9f88e5aSYishai Hadas 	struct mlx5_ib_qp *mqp = to_mqp(qp);
2597038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
2598038d2ef8SMaor Gottlieb 	struct mlx5_flow_destination *dst = NULL;
2599cc0e5d42SMaor Gottlieb 	struct mlx5_ib_flow_prio *ft_prio_tx = NULL;
2600038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_prio *ft_prio;
2601038d2ef8SMaor Gottlieb 	int err;
2602a550ddfcSYishai Hadas 	int underlay_qpn;
2603038d2ef8SMaor Gottlieb 
2604038d2ef8SMaor Gottlieb 	if (flow_attr->priority > MLX5_IB_FLOW_LAST_PRIO)
2605dac388efSMaor Gottlieb 		return ERR_PTR(-ENOMEM);
2606038d2ef8SMaor Gottlieb 
2607038d2ef8SMaor Gottlieb 	if (domain != IB_FLOW_DOMAIN_USER ||
2608038d2ef8SMaor Gottlieb 	    flow_attr->port > MLX5_CAP_GEN(dev->mdev, num_ports) ||
260935d19011SMaor Gottlieb 	    (flow_attr->flags & ~IB_FLOW_ATTR_FLAGS_DONT_TRAP))
2610038d2ef8SMaor Gottlieb 		return ERR_PTR(-EINVAL);
2611038d2ef8SMaor Gottlieb 
2612038d2ef8SMaor Gottlieb 	dst = kzalloc(sizeof(*dst), GFP_KERNEL);
2613038d2ef8SMaor Gottlieb 	if (!dst)
2614038d2ef8SMaor Gottlieb 		return ERR_PTR(-ENOMEM);
2615038d2ef8SMaor Gottlieb 
2616038d2ef8SMaor Gottlieb 	mutex_lock(&dev->flow_db.lock);
2617038d2ef8SMaor Gottlieb 
2618cc0e5d42SMaor Gottlieb 	ft_prio = get_flow_table(dev, flow_attr, MLX5_IB_FT_RX);
2619038d2ef8SMaor Gottlieb 	if (IS_ERR(ft_prio)) {
2620038d2ef8SMaor Gottlieb 		err = PTR_ERR(ft_prio);
2621038d2ef8SMaor Gottlieb 		goto unlock;
2622038d2ef8SMaor Gottlieb 	}
2623cc0e5d42SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
2624cc0e5d42SMaor Gottlieb 		ft_prio_tx = get_flow_table(dev, flow_attr, MLX5_IB_FT_TX);
2625cc0e5d42SMaor Gottlieb 		if (IS_ERR(ft_prio_tx)) {
2626cc0e5d42SMaor Gottlieb 			err = PTR_ERR(ft_prio_tx);
2627cc0e5d42SMaor Gottlieb 			ft_prio_tx = NULL;
2628cc0e5d42SMaor Gottlieb 			goto destroy_ft;
2629cc0e5d42SMaor Gottlieb 		}
2630cc0e5d42SMaor Gottlieb 	}
2631038d2ef8SMaor Gottlieb 
2632038d2ef8SMaor Gottlieb 	dst->type = MLX5_FLOW_DESTINATION_TYPE_TIR;
2633d9f88e5aSYishai Hadas 	if (mqp->flags & MLX5_IB_QP_RSS)
2634d9f88e5aSYishai Hadas 		dst->tir_num = mqp->rss_qp.tirn;
2635d9f88e5aSYishai Hadas 	else
2636d9f88e5aSYishai Hadas 		dst->tir_num = mqp->raw_packet_qp.rq.tirn;
2637038d2ef8SMaor Gottlieb 
2638038d2ef8SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
263935d19011SMaor Gottlieb 		if (flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP)  {
264035d19011SMaor Gottlieb 			handler = create_dont_trap_rule(dev, ft_prio,
264135d19011SMaor Gottlieb 							flow_attr, dst);
264235d19011SMaor Gottlieb 		} else {
2643a550ddfcSYishai Hadas 			underlay_qpn = (mqp->flags & MLX5_IB_QP_UNDERLAY) ?
2644a550ddfcSYishai Hadas 					mqp->underlay_qpn : 0;
2645a550ddfcSYishai Hadas 			handler = _create_flow_rule(dev, ft_prio, flow_attr,
2646a550ddfcSYishai Hadas 						    dst, underlay_qpn);
264735d19011SMaor Gottlieb 		}
2648038d2ef8SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
2649038d2ef8SMaor Gottlieb 		   flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
2650038d2ef8SMaor Gottlieb 		handler = create_leftovers_rule(dev, ft_prio, flow_attr,
2651038d2ef8SMaor Gottlieb 						dst);
2652cc0e5d42SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
2653cc0e5d42SMaor Gottlieb 		handler = create_sniffer_rule(dev, ft_prio, ft_prio_tx, dst);
2654038d2ef8SMaor Gottlieb 	} else {
2655038d2ef8SMaor Gottlieb 		err = -EINVAL;
2656038d2ef8SMaor Gottlieb 		goto destroy_ft;
2657038d2ef8SMaor Gottlieb 	}
2658038d2ef8SMaor Gottlieb 
2659038d2ef8SMaor Gottlieb 	if (IS_ERR(handler)) {
2660038d2ef8SMaor Gottlieb 		err = PTR_ERR(handler);
2661038d2ef8SMaor Gottlieb 		handler = NULL;
2662038d2ef8SMaor Gottlieb 		goto destroy_ft;
2663038d2ef8SMaor Gottlieb 	}
2664038d2ef8SMaor Gottlieb 
2665038d2ef8SMaor Gottlieb 	mutex_unlock(&dev->flow_db.lock);
2666038d2ef8SMaor Gottlieb 	kfree(dst);
2667038d2ef8SMaor Gottlieb 
2668038d2ef8SMaor Gottlieb 	return &handler->ibflow;
2669038d2ef8SMaor Gottlieb 
2670038d2ef8SMaor Gottlieb destroy_ft:
2671038d2ef8SMaor Gottlieb 	put_flow_table(dev, ft_prio, false);
2672cc0e5d42SMaor Gottlieb 	if (ft_prio_tx)
2673cc0e5d42SMaor Gottlieb 		put_flow_table(dev, ft_prio_tx, false);
2674038d2ef8SMaor Gottlieb unlock:
2675038d2ef8SMaor Gottlieb 	mutex_unlock(&dev->flow_db.lock);
2676038d2ef8SMaor Gottlieb 	kfree(dst);
2677038d2ef8SMaor Gottlieb 	kfree(handler);
2678038d2ef8SMaor Gottlieb 	return ERR_PTR(err);
2679038d2ef8SMaor Gottlieb }
2680038d2ef8SMaor Gottlieb 
2681e126ba97SEli Cohen static int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
2682e126ba97SEli Cohen {
2683e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
268481e30880SYishai Hadas 	struct mlx5_ib_qp *mqp = to_mqp(ibqp);
2685e126ba97SEli Cohen 	int err;
2686e126ba97SEli Cohen 
268781e30880SYishai Hadas 	if (mqp->flags & MLX5_IB_QP_UNDERLAY) {
268881e30880SYishai Hadas 		mlx5_ib_dbg(dev, "Attaching a multi cast group to underlay QP is not supported\n");
268981e30880SYishai Hadas 		return -EOPNOTSUPP;
269081e30880SYishai Hadas 	}
269181e30880SYishai Hadas 
26929603b61dSJack Morgenstein 	err = mlx5_core_attach_mcg(dev->mdev, gid, ibqp->qp_num);
2693e126ba97SEli Cohen 	if (err)
2694e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n",
2695e126ba97SEli Cohen 			     ibqp->qp_num, gid->raw);
2696e126ba97SEli Cohen 
2697e126ba97SEli Cohen 	return err;
2698e126ba97SEli Cohen }
2699e126ba97SEli Cohen 
2700e126ba97SEli Cohen static int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
2701e126ba97SEli Cohen {
2702e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
2703e126ba97SEli Cohen 	int err;
2704e126ba97SEli Cohen 
27059603b61dSJack Morgenstein 	err = mlx5_core_detach_mcg(dev->mdev, gid, ibqp->qp_num);
2706e126ba97SEli Cohen 	if (err)
2707e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n",
2708e126ba97SEli Cohen 			     ibqp->qp_num, gid->raw);
2709e126ba97SEli Cohen 
2710e126ba97SEli Cohen 	return err;
2711e126ba97SEli Cohen }
2712e126ba97SEli Cohen 
2713e126ba97SEli Cohen static int init_node_data(struct mlx5_ib_dev *dev)
2714e126ba97SEli Cohen {
27151b5daf11SMajd Dibbiny 	int err;
2716e126ba97SEli Cohen 
27171b5daf11SMajd Dibbiny 	err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc);
2718e126ba97SEli Cohen 	if (err)
2719e126ba97SEli Cohen 		return err;
27201b5daf11SMajd Dibbiny 
27211b5daf11SMajd Dibbiny 	dev->mdev->rev_id = dev->mdev->pdev->revision;
27221b5daf11SMajd Dibbiny 
27231b5daf11SMajd Dibbiny 	return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid);
2724e126ba97SEli Cohen }
2725e126ba97SEli Cohen 
2726e126ba97SEli Cohen static ssize_t show_fw_pages(struct device *device, struct device_attribute *attr,
2727e126ba97SEli Cohen 			     char *buf)
2728e126ba97SEli Cohen {
2729e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
2730e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
2731e126ba97SEli Cohen 
27329603b61dSJack Morgenstein 	return sprintf(buf, "%d\n", dev->mdev->priv.fw_pages);
2733e126ba97SEli Cohen }
2734e126ba97SEli Cohen 
2735e126ba97SEli Cohen static ssize_t show_reg_pages(struct device *device,
2736e126ba97SEli Cohen 			      struct device_attribute *attr, char *buf)
2737e126ba97SEli Cohen {
2738e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
2739e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
2740e126ba97SEli Cohen 
27416aec21f6SHaggai Eran 	return sprintf(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages));
2742e126ba97SEli Cohen }
2743e126ba97SEli Cohen 
2744e126ba97SEli Cohen static ssize_t show_hca(struct device *device, struct device_attribute *attr,
2745e126ba97SEli Cohen 			char *buf)
2746e126ba97SEli Cohen {
2747e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
2748e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
27499603b61dSJack Morgenstein 	return sprintf(buf, "MT%d\n", dev->mdev->pdev->device);
2750e126ba97SEli Cohen }
2751e126ba97SEli Cohen 
2752e126ba97SEli Cohen static ssize_t show_rev(struct device *device, struct device_attribute *attr,
2753e126ba97SEli Cohen 			char *buf)
2754e126ba97SEli Cohen {
2755e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
2756e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
27579603b61dSJack Morgenstein 	return sprintf(buf, "%x\n", dev->mdev->rev_id);
2758e126ba97SEli Cohen }
2759e126ba97SEli Cohen 
2760e126ba97SEli Cohen static ssize_t show_board(struct device *device, struct device_attribute *attr,
2761e126ba97SEli Cohen 			  char *buf)
2762e126ba97SEli Cohen {
2763e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
2764e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
2765e126ba97SEli Cohen 	return sprintf(buf, "%.*s\n", MLX5_BOARD_ID_LEN,
27669603b61dSJack Morgenstein 		       dev->mdev->board_id);
2767e126ba97SEli Cohen }
2768e126ba97SEli Cohen 
2769e126ba97SEli Cohen static DEVICE_ATTR(hw_rev,   S_IRUGO, show_rev,    NULL);
2770e126ba97SEli Cohen static DEVICE_ATTR(hca_type, S_IRUGO, show_hca,    NULL);
2771e126ba97SEli Cohen static DEVICE_ATTR(board_id, S_IRUGO, show_board,  NULL);
2772e126ba97SEli Cohen static DEVICE_ATTR(fw_pages, S_IRUGO, show_fw_pages, NULL);
2773e126ba97SEli Cohen static DEVICE_ATTR(reg_pages, S_IRUGO, show_reg_pages, NULL);
2774e126ba97SEli Cohen 
2775e126ba97SEli Cohen static struct device_attribute *mlx5_class_attributes[] = {
2776e126ba97SEli Cohen 	&dev_attr_hw_rev,
2777e126ba97SEli Cohen 	&dev_attr_hca_type,
2778e126ba97SEli Cohen 	&dev_attr_board_id,
2779e126ba97SEli Cohen 	&dev_attr_fw_pages,
2780e126ba97SEli Cohen 	&dev_attr_reg_pages,
2781e126ba97SEli Cohen };
2782e126ba97SEli Cohen 
27837722f47eSHaggai Eran static void pkey_change_handler(struct work_struct *work)
27847722f47eSHaggai Eran {
27857722f47eSHaggai Eran 	struct mlx5_ib_port_resources *ports =
27867722f47eSHaggai Eran 		container_of(work, struct mlx5_ib_port_resources,
27877722f47eSHaggai Eran 			     pkey_change_work);
27887722f47eSHaggai Eran 
27897722f47eSHaggai Eran 	mutex_lock(&ports->devr->mutex);
27907722f47eSHaggai Eran 	mlx5_ib_gsi_pkey_change(ports->gsi);
27917722f47eSHaggai Eran 	mutex_unlock(&ports->devr->mutex);
27927722f47eSHaggai Eran }
27937722f47eSHaggai Eran 
279489ea94a7SMaor Gottlieb static void mlx5_ib_handle_internal_error(struct mlx5_ib_dev *ibdev)
279589ea94a7SMaor Gottlieb {
279689ea94a7SMaor Gottlieb 	struct mlx5_ib_qp *mqp;
279789ea94a7SMaor Gottlieb 	struct mlx5_ib_cq *send_mcq, *recv_mcq;
279889ea94a7SMaor Gottlieb 	struct mlx5_core_cq *mcq;
279989ea94a7SMaor Gottlieb 	struct list_head cq_armed_list;
280089ea94a7SMaor Gottlieb 	unsigned long flags_qp;
280189ea94a7SMaor Gottlieb 	unsigned long flags_cq;
280289ea94a7SMaor Gottlieb 	unsigned long flags;
280389ea94a7SMaor Gottlieb 
280489ea94a7SMaor Gottlieb 	INIT_LIST_HEAD(&cq_armed_list);
280589ea94a7SMaor Gottlieb 
280689ea94a7SMaor Gottlieb 	/* Go over qp list reside on that ibdev, sync with create/destroy qp.*/
280789ea94a7SMaor Gottlieb 	spin_lock_irqsave(&ibdev->reset_flow_resource_lock, flags);
280889ea94a7SMaor Gottlieb 	list_for_each_entry(mqp, &ibdev->qp_list, qps_list) {
280989ea94a7SMaor Gottlieb 		spin_lock_irqsave(&mqp->sq.lock, flags_qp);
281089ea94a7SMaor Gottlieb 		if (mqp->sq.tail != mqp->sq.head) {
281189ea94a7SMaor Gottlieb 			send_mcq = to_mcq(mqp->ibqp.send_cq);
281289ea94a7SMaor Gottlieb 			spin_lock_irqsave(&send_mcq->lock, flags_cq);
281389ea94a7SMaor Gottlieb 			if (send_mcq->mcq.comp &&
281489ea94a7SMaor Gottlieb 			    mqp->ibqp.send_cq->comp_handler) {
281589ea94a7SMaor Gottlieb 				if (!send_mcq->mcq.reset_notify_added) {
281689ea94a7SMaor Gottlieb 					send_mcq->mcq.reset_notify_added = 1;
281789ea94a7SMaor Gottlieb 					list_add_tail(&send_mcq->mcq.reset_notify,
281889ea94a7SMaor Gottlieb 						      &cq_armed_list);
281989ea94a7SMaor Gottlieb 				}
282089ea94a7SMaor Gottlieb 			}
282189ea94a7SMaor Gottlieb 			spin_unlock_irqrestore(&send_mcq->lock, flags_cq);
282289ea94a7SMaor Gottlieb 		}
282389ea94a7SMaor Gottlieb 		spin_unlock_irqrestore(&mqp->sq.lock, flags_qp);
282489ea94a7SMaor Gottlieb 		spin_lock_irqsave(&mqp->rq.lock, flags_qp);
282589ea94a7SMaor Gottlieb 		/* no handling is needed for SRQ */
282689ea94a7SMaor Gottlieb 		if (!mqp->ibqp.srq) {
282789ea94a7SMaor Gottlieb 			if (mqp->rq.tail != mqp->rq.head) {
282889ea94a7SMaor Gottlieb 				recv_mcq = to_mcq(mqp->ibqp.recv_cq);
282989ea94a7SMaor Gottlieb 				spin_lock_irqsave(&recv_mcq->lock, flags_cq);
283089ea94a7SMaor Gottlieb 				if (recv_mcq->mcq.comp &&
283189ea94a7SMaor Gottlieb 				    mqp->ibqp.recv_cq->comp_handler) {
283289ea94a7SMaor Gottlieb 					if (!recv_mcq->mcq.reset_notify_added) {
283389ea94a7SMaor Gottlieb 						recv_mcq->mcq.reset_notify_added = 1;
283489ea94a7SMaor Gottlieb 						list_add_tail(&recv_mcq->mcq.reset_notify,
283589ea94a7SMaor Gottlieb 							      &cq_armed_list);
283689ea94a7SMaor Gottlieb 					}
283789ea94a7SMaor Gottlieb 				}
283889ea94a7SMaor Gottlieb 				spin_unlock_irqrestore(&recv_mcq->lock,
283989ea94a7SMaor Gottlieb 						       flags_cq);
284089ea94a7SMaor Gottlieb 			}
284189ea94a7SMaor Gottlieb 		}
284289ea94a7SMaor Gottlieb 		spin_unlock_irqrestore(&mqp->rq.lock, flags_qp);
284389ea94a7SMaor Gottlieb 	}
284489ea94a7SMaor Gottlieb 	/*At that point all inflight post send were put to be executed as of we
284589ea94a7SMaor Gottlieb 	 * lock/unlock above locks Now need to arm all involved CQs.
284689ea94a7SMaor Gottlieb 	 */
284789ea94a7SMaor Gottlieb 	list_for_each_entry(mcq, &cq_armed_list, reset_notify) {
284889ea94a7SMaor Gottlieb 		mcq->comp(mcq);
284989ea94a7SMaor Gottlieb 	}
285089ea94a7SMaor Gottlieb 	spin_unlock_irqrestore(&ibdev->reset_flow_resource_lock, flags);
285189ea94a7SMaor Gottlieb }
285289ea94a7SMaor Gottlieb 
285303404e8aSMaor Gottlieb static void delay_drop_handler(struct work_struct *work)
285403404e8aSMaor Gottlieb {
285503404e8aSMaor Gottlieb 	int err;
285603404e8aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop =
285703404e8aSMaor Gottlieb 		container_of(work, struct mlx5_ib_delay_drop,
285803404e8aSMaor Gottlieb 			     delay_drop_work);
285903404e8aSMaor Gottlieb 
2860fe248c3aSMaor Gottlieb 	atomic_inc(&delay_drop->events_cnt);
2861fe248c3aSMaor Gottlieb 
286203404e8aSMaor Gottlieb 	mutex_lock(&delay_drop->lock);
286303404e8aSMaor Gottlieb 	err = mlx5_core_set_delay_drop(delay_drop->dev->mdev,
286403404e8aSMaor Gottlieb 				       delay_drop->timeout);
286503404e8aSMaor Gottlieb 	if (err) {
286603404e8aSMaor Gottlieb 		mlx5_ib_warn(delay_drop->dev, "Failed to set delay drop, timeout=%u\n",
286703404e8aSMaor Gottlieb 			     delay_drop->timeout);
286803404e8aSMaor Gottlieb 		delay_drop->activate = false;
286903404e8aSMaor Gottlieb 	}
287003404e8aSMaor Gottlieb 	mutex_unlock(&delay_drop->lock);
287103404e8aSMaor Gottlieb }
287203404e8aSMaor Gottlieb 
28739603b61dSJack Morgenstein static void mlx5_ib_event(struct mlx5_core_dev *dev, void *context,
28744d2f9bbbSJack Morgenstein 			  enum mlx5_dev_event event, unsigned long param)
2875e126ba97SEli Cohen {
28769603b61dSJack Morgenstein 	struct mlx5_ib_dev *ibdev = (struct mlx5_ib_dev *)context;
2877e126ba97SEli Cohen 	struct ib_event ibev;
2878dbaaff2aSEli Cohen 	bool fatal = false;
2879e126ba97SEli Cohen 	u8 port = 0;
2880e126ba97SEli Cohen 
2881e126ba97SEli Cohen 	switch (event) {
2882e126ba97SEli Cohen 	case MLX5_DEV_EVENT_SYS_ERROR:
2883e126ba97SEli Cohen 		ibev.event = IB_EVENT_DEVICE_FATAL;
288489ea94a7SMaor Gottlieb 		mlx5_ib_handle_internal_error(ibdev);
2885dbaaff2aSEli Cohen 		fatal = true;
2886e126ba97SEli Cohen 		break;
2887e126ba97SEli Cohen 
2888e126ba97SEli Cohen 	case MLX5_DEV_EVENT_PORT_UP:
2889e126ba97SEli Cohen 	case MLX5_DEV_EVENT_PORT_DOWN:
28902788cf3bSNoa Osherovich 	case MLX5_DEV_EVENT_PORT_INITIALIZED:
28914d2f9bbbSJack Morgenstein 		port = (u8)param;
28925ec8c83eSAviv Heller 
28935ec8c83eSAviv Heller 		/* In RoCE, port up/down events are handled in
28945ec8c83eSAviv Heller 		 * mlx5_netdev_event().
28955ec8c83eSAviv Heller 		 */
28965ec8c83eSAviv Heller 		if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) ==
28975ec8c83eSAviv Heller 			IB_LINK_LAYER_ETHERNET)
28985ec8c83eSAviv Heller 			return;
28995ec8c83eSAviv Heller 
29005ec8c83eSAviv Heller 		ibev.event = (event == MLX5_DEV_EVENT_PORT_UP) ?
29015ec8c83eSAviv Heller 			     IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
2902e126ba97SEli Cohen 		break;
2903e126ba97SEli Cohen 
2904e126ba97SEli Cohen 	case MLX5_DEV_EVENT_LID_CHANGE:
2905e126ba97SEli Cohen 		ibev.event = IB_EVENT_LID_CHANGE;
29064d2f9bbbSJack Morgenstein 		port = (u8)param;
2907e126ba97SEli Cohen 		break;
2908e126ba97SEli Cohen 
2909e126ba97SEli Cohen 	case MLX5_DEV_EVENT_PKEY_CHANGE:
2910e126ba97SEli Cohen 		ibev.event = IB_EVENT_PKEY_CHANGE;
29114d2f9bbbSJack Morgenstein 		port = (u8)param;
29127722f47eSHaggai Eran 
29137722f47eSHaggai Eran 		schedule_work(&ibdev->devr.ports[port - 1].pkey_change_work);
2914e126ba97SEli Cohen 		break;
2915e126ba97SEli Cohen 
2916e126ba97SEli Cohen 	case MLX5_DEV_EVENT_GUID_CHANGE:
2917e126ba97SEli Cohen 		ibev.event = IB_EVENT_GID_CHANGE;
29184d2f9bbbSJack Morgenstein 		port = (u8)param;
2919e126ba97SEli Cohen 		break;
2920e126ba97SEli Cohen 
2921e126ba97SEli Cohen 	case MLX5_DEV_EVENT_CLIENT_REREG:
2922e126ba97SEli Cohen 		ibev.event = IB_EVENT_CLIENT_REREGISTER;
29234d2f9bbbSJack Morgenstein 		port = (u8)param;
2924e126ba97SEli Cohen 		break;
292503404e8aSMaor Gottlieb 	case MLX5_DEV_EVENT_DELAY_DROP_TIMEOUT:
292603404e8aSMaor Gottlieb 		schedule_work(&ibdev->delay_drop.delay_drop_work);
292703404e8aSMaor Gottlieb 		goto out;
2928bdc37924SSaeed Mahameed 	default:
292903404e8aSMaor Gottlieb 		goto out;
2930e126ba97SEli Cohen 	}
2931e126ba97SEli Cohen 
2932e126ba97SEli Cohen 	ibev.device	      = &ibdev->ib_dev;
2933e126ba97SEli Cohen 	ibev.element.port_num = port;
2934e126ba97SEli Cohen 
2935a0c84c32SEli Cohen 	if (port < 1 || port > ibdev->num_ports) {
2936a0c84c32SEli Cohen 		mlx5_ib_warn(ibdev, "warning: event on port %d\n", port);
293703404e8aSMaor Gottlieb 		goto out;
2938a0c84c32SEli Cohen 	}
2939a0c84c32SEli Cohen 
2940e126ba97SEli Cohen 	if (ibdev->ib_active)
2941e126ba97SEli Cohen 		ib_dispatch_event(&ibev);
2942dbaaff2aSEli Cohen 
2943dbaaff2aSEli Cohen 	if (fatal)
2944dbaaff2aSEli Cohen 		ibdev->ib_active = false;
294503404e8aSMaor Gottlieb 
294603404e8aSMaor Gottlieb out:
294703404e8aSMaor Gottlieb 	return;
2948e126ba97SEli Cohen }
2949e126ba97SEli Cohen 
2950c43f1112SMaor Gottlieb static int set_has_smi_cap(struct mlx5_ib_dev *dev)
2951c43f1112SMaor Gottlieb {
2952c43f1112SMaor Gottlieb 	struct mlx5_hca_vport_context vport_ctx;
2953c43f1112SMaor Gottlieb 	int err;
2954c43f1112SMaor Gottlieb 	int port;
2955c43f1112SMaor Gottlieb 
2956c43f1112SMaor Gottlieb 	for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++) {
2957c43f1112SMaor Gottlieb 		dev->mdev->port_caps[port - 1].has_smi = false;
2958c43f1112SMaor Gottlieb 		if (MLX5_CAP_GEN(dev->mdev, port_type) ==
2959c43f1112SMaor Gottlieb 		    MLX5_CAP_PORT_TYPE_IB) {
2960c43f1112SMaor Gottlieb 			if (MLX5_CAP_GEN(dev->mdev, ib_virt)) {
2961c43f1112SMaor Gottlieb 				err = mlx5_query_hca_vport_context(dev->mdev, 0,
2962c43f1112SMaor Gottlieb 								   port, 0,
2963c43f1112SMaor Gottlieb 								   &vport_ctx);
2964c43f1112SMaor Gottlieb 				if (err) {
2965c43f1112SMaor Gottlieb 					mlx5_ib_err(dev, "query_hca_vport_context for port=%d failed %d\n",
2966c43f1112SMaor Gottlieb 						    port, err);
2967c43f1112SMaor Gottlieb 					return err;
2968c43f1112SMaor Gottlieb 				}
2969c43f1112SMaor Gottlieb 				dev->mdev->port_caps[port - 1].has_smi =
2970c43f1112SMaor Gottlieb 					vport_ctx.has_smi;
2971c43f1112SMaor Gottlieb 			} else {
2972c43f1112SMaor Gottlieb 				dev->mdev->port_caps[port - 1].has_smi = true;
2973c43f1112SMaor Gottlieb 			}
2974c43f1112SMaor Gottlieb 		}
2975c43f1112SMaor Gottlieb 	}
2976c43f1112SMaor Gottlieb 	return 0;
2977c43f1112SMaor Gottlieb }
2978c43f1112SMaor Gottlieb 
2979e126ba97SEli Cohen static void get_ext_port_caps(struct mlx5_ib_dev *dev)
2980e126ba97SEli Cohen {
2981e126ba97SEli Cohen 	int port;
2982e126ba97SEli Cohen 
2983938fe83cSSaeed Mahameed 	for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++)
2984e126ba97SEli Cohen 		mlx5_query_ext_port_caps(dev, port);
2985e126ba97SEli Cohen }
2986e126ba97SEli Cohen 
2987e126ba97SEli Cohen static int get_port_caps(struct mlx5_ib_dev *dev)
2988e126ba97SEli Cohen {
2989e126ba97SEli Cohen 	struct ib_device_attr *dprops = NULL;
2990e126ba97SEli Cohen 	struct ib_port_attr *pprops = NULL;
2991f614fc15SDan Carpenter 	int err = -ENOMEM;
2992e126ba97SEli Cohen 	int port;
29932528e33eSMatan Barak 	struct ib_udata uhw = {.inlen = 0, .outlen = 0};
2994e126ba97SEli Cohen 
2995e126ba97SEli Cohen 	pprops = kmalloc(sizeof(*pprops), GFP_KERNEL);
2996e126ba97SEli Cohen 	if (!pprops)
2997e126ba97SEli Cohen 		goto out;
2998e126ba97SEli Cohen 
2999e126ba97SEli Cohen 	dprops = kmalloc(sizeof(*dprops), GFP_KERNEL);
3000e126ba97SEli Cohen 	if (!dprops)
3001e126ba97SEli Cohen 		goto out;
3002e126ba97SEli Cohen 
3003c43f1112SMaor Gottlieb 	err = set_has_smi_cap(dev);
3004c43f1112SMaor Gottlieb 	if (err)
3005c43f1112SMaor Gottlieb 		goto out;
3006c43f1112SMaor Gottlieb 
30072528e33eSMatan Barak 	err = mlx5_ib_query_device(&dev->ib_dev, dprops, &uhw);
3008e126ba97SEli Cohen 	if (err) {
3009e126ba97SEli Cohen 		mlx5_ib_warn(dev, "query_device failed %d\n", err);
3010e126ba97SEli Cohen 		goto out;
3011e126ba97SEli Cohen 	}
3012e126ba97SEli Cohen 
3013938fe83cSSaeed Mahameed 	for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++) {
3014c4550c63SOr Gerlitz 		memset(pprops, 0, sizeof(*pprops));
3015e126ba97SEli Cohen 		err = mlx5_ib_query_port(&dev->ib_dev, port, pprops);
3016e126ba97SEli Cohen 		if (err) {
3017938fe83cSSaeed Mahameed 			mlx5_ib_warn(dev, "query_port %d failed %d\n",
3018938fe83cSSaeed Mahameed 				     port, err);
3019e126ba97SEli Cohen 			break;
3020e126ba97SEli Cohen 		}
3021938fe83cSSaeed Mahameed 		dev->mdev->port_caps[port - 1].pkey_table_len =
3022938fe83cSSaeed Mahameed 						dprops->max_pkeys;
3023938fe83cSSaeed Mahameed 		dev->mdev->port_caps[port - 1].gid_table_len =
3024938fe83cSSaeed Mahameed 						pprops->gid_tbl_len;
3025e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "pkey_table_len %d, gid_table_len %d\n",
3026e126ba97SEli Cohen 			    dprops->max_pkeys, pprops->gid_tbl_len);
3027e126ba97SEli Cohen 	}
3028e126ba97SEli Cohen 
3029e126ba97SEli Cohen out:
3030e126ba97SEli Cohen 	kfree(pprops);
3031e126ba97SEli Cohen 	kfree(dprops);
3032e126ba97SEli Cohen 
3033e126ba97SEli Cohen 	return err;
3034e126ba97SEli Cohen }
3035e126ba97SEli Cohen 
3036e126ba97SEli Cohen static void destroy_umrc_res(struct mlx5_ib_dev *dev)
3037e126ba97SEli Cohen {
3038e126ba97SEli Cohen 	int err;
3039e126ba97SEli Cohen 
3040e126ba97SEli Cohen 	err = mlx5_mr_cache_cleanup(dev);
3041e126ba97SEli Cohen 	if (err)
3042e126ba97SEli Cohen 		mlx5_ib_warn(dev, "mr cache cleanup failed\n");
3043e126ba97SEli Cohen 
3044e126ba97SEli Cohen 	mlx5_ib_destroy_qp(dev->umrc.qp);
3045add08d76SChristoph Hellwig 	ib_free_cq(dev->umrc.cq);
3046e126ba97SEli Cohen 	ib_dealloc_pd(dev->umrc.pd);
3047e126ba97SEli Cohen }
3048e126ba97SEli Cohen 
3049e126ba97SEli Cohen enum {
3050e126ba97SEli Cohen 	MAX_UMR_WR = 128,
3051e126ba97SEli Cohen };
3052e126ba97SEli Cohen 
3053e126ba97SEli Cohen static int create_umr_res(struct mlx5_ib_dev *dev)
3054e126ba97SEli Cohen {
3055e126ba97SEli Cohen 	struct ib_qp_init_attr *init_attr = NULL;
3056e126ba97SEli Cohen 	struct ib_qp_attr *attr = NULL;
3057e126ba97SEli Cohen 	struct ib_pd *pd;
3058e126ba97SEli Cohen 	struct ib_cq *cq;
3059e126ba97SEli Cohen 	struct ib_qp *qp;
3060e126ba97SEli Cohen 	int ret;
3061e126ba97SEli Cohen 
3062e126ba97SEli Cohen 	attr = kzalloc(sizeof(*attr), GFP_KERNEL);
3063e126ba97SEli Cohen 	init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL);
3064e126ba97SEli Cohen 	if (!attr || !init_attr) {
3065e126ba97SEli Cohen 		ret = -ENOMEM;
3066e126ba97SEli Cohen 		goto error_0;
3067e126ba97SEli Cohen 	}
3068e126ba97SEli Cohen 
3069ed082d36SChristoph Hellwig 	pd = ib_alloc_pd(&dev->ib_dev, 0);
3070e126ba97SEli Cohen 	if (IS_ERR(pd)) {
3071e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create PD for sync UMR QP\n");
3072e126ba97SEli Cohen 		ret = PTR_ERR(pd);
3073e126ba97SEli Cohen 		goto error_0;
3074e126ba97SEli Cohen 	}
3075e126ba97SEli Cohen 
3076add08d76SChristoph Hellwig 	cq = ib_alloc_cq(&dev->ib_dev, NULL, 128, 0, IB_POLL_SOFTIRQ);
3077e126ba97SEli Cohen 	if (IS_ERR(cq)) {
3078e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create CQ for sync UMR QP\n");
3079e126ba97SEli Cohen 		ret = PTR_ERR(cq);
3080e126ba97SEli Cohen 		goto error_2;
3081e126ba97SEli Cohen 	}
3082e126ba97SEli Cohen 
3083e126ba97SEli Cohen 	init_attr->send_cq = cq;
3084e126ba97SEli Cohen 	init_attr->recv_cq = cq;
3085e126ba97SEli Cohen 	init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
3086e126ba97SEli Cohen 	init_attr->cap.max_send_wr = MAX_UMR_WR;
3087e126ba97SEli Cohen 	init_attr->cap.max_send_sge = 1;
3088e126ba97SEli Cohen 	init_attr->qp_type = MLX5_IB_QPT_REG_UMR;
3089e126ba97SEli Cohen 	init_attr->port_num = 1;
3090e126ba97SEli Cohen 	qp = mlx5_ib_create_qp(pd, init_attr, NULL);
3091e126ba97SEli Cohen 	if (IS_ERR(qp)) {
3092e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create sync UMR QP\n");
3093e126ba97SEli Cohen 		ret = PTR_ERR(qp);
3094e126ba97SEli Cohen 		goto error_3;
3095e126ba97SEli Cohen 	}
3096e126ba97SEli Cohen 	qp->device     = &dev->ib_dev;
3097e126ba97SEli Cohen 	qp->real_qp    = qp;
3098e126ba97SEli Cohen 	qp->uobject    = NULL;
3099e126ba97SEli Cohen 	qp->qp_type    = MLX5_IB_QPT_REG_UMR;
3100e126ba97SEli Cohen 
3101e126ba97SEli Cohen 	attr->qp_state = IB_QPS_INIT;
3102e126ba97SEli Cohen 	attr->port_num = 1;
3103e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_PKEY_INDEX |
3104e126ba97SEli Cohen 				IB_QP_PORT, NULL);
3105e126ba97SEli Cohen 	if (ret) {
3106e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify UMR QP\n");
3107e126ba97SEli Cohen 		goto error_4;
3108e126ba97SEli Cohen 	}
3109e126ba97SEli Cohen 
3110e126ba97SEli Cohen 	memset(attr, 0, sizeof(*attr));
3111e126ba97SEli Cohen 	attr->qp_state = IB_QPS_RTR;
3112e126ba97SEli Cohen 	attr->path_mtu = IB_MTU_256;
3113e126ba97SEli Cohen 
3114e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
3115e126ba97SEli Cohen 	if (ret) {
3116e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify umr QP to rtr\n");
3117e126ba97SEli Cohen 		goto error_4;
3118e126ba97SEli Cohen 	}
3119e126ba97SEli Cohen 
3120e126ba97SEli Cohen 	memset(attr, 0, sizeof(*attr));
3121e126ba97SEli Cohen 	attr->qp_state = IB_QPS_RTS;
3122e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
3123e126ba97SEli Cohen 	if (ret) {
3124e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify umr QP to rts\n");
3125e126ba97SEli Cohen 		goto error_4;
3126e126ba97SEli Cohen 	}
3127e126ba97SEli Cohen 
3128e126ba97SEli Cohen 	dev->umrc.qp = qp;
3129e126ba97SEli Cohen 	dev->umrc.cq = cq;
3130e126ba97SEli Cohen 	dev->umrc.pd = pd;
3131e126ba97SEli Cohen 
3132e126ba97SEli Cohen 	sema_init(&dev->umrc.sem, MAX_UMR_WR);
3133e126ba97SEli Cohen 	ret = mlx5_mr_cache_init(dev);
3134e126ba97SEli Cohen 	if (ret) {
3135e126ba97SEli Cohen 		mlx5_ib_warn(dev, "mr cache init failed %d\n", ret);
3136e126ba97SEli Cohen 		goto error_4;
3137e126ba97SEli Cohen 	}
3138e126ba97SEli Cohen 
3139e126ba97SEli Cohen 	kfree(attr);
3140e126ba97SEli Cohen 	kfree(init_attr);
3141e126ba97SEli Cohen 
3142e126ba97SEli Cohen 	return 0;
3143e126ba97SEli Cohen 
3144e126ba97SEli Cohen error_4:
3145e126ba97SEli Cohen 	mlx5_ib_destroy_qp(qp);
3146e126ba97SEli Cohen 
3147e126ba97SEli Cohen error_3:
3148add08d76SChristoph Hellwig 	ib_free_cq(cq);
3149e126ba97SEli Cohen 
3150e126ba97SEli Cohen error_2:
3151e126ba97SEli Cohen 	ib_dealloc_pd(pd);
3152e126ba97SEli Cohen 
3153e126ba97SEli Cohen error_0:
3154e126ba97SEli Cohen 	kfree(attr);
3155e126ba97SEli Cohen 	kfree(init_attr);
3156e126ba97SEli Cohen 	return ret;
3157e126ba97SEli Cohen }
3158e126ba97SEli Cohen 
31596e8484c5SMax Gurtovoy static u8 mlx5_get_umr_fence(u8 umr_fence_cap)
31606e8484c5SMax Gurtovoy {
31616e8484c5SMax Gurtovoy 	switch (umr_fence_cap) {
31626e8484c5SMax Gurtovoy 	case MLX5_CAP_UMR_FENCE_NONE:
31636e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_NONE;
31646e8484c5SMax Gurtovoy 	case MLX5_CAP_UMR_FENCE_SMALL:
31656e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_INITIATOR_SMALL;
31666e8484c5SMax Gurtovoy 	default:
31676e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_STRONG_ORDERING;
31686e8484c5SMax Gurtovoy 	}
31696e8484c5SMax Gurtovoy }
31706e8484c5SMax Gurtovoy 
3171e126ba97SEli Cohen static int create_dev_resources(struct mlx5_ib_resources *devr)
3172e126ba97SEli Cohen {
3173e126ba97SEli Cohen 	struct ib_srq_init_attr attr;
3174e126ba97SEli Cohen 	struct mlx5_ib_dev *dev;
3175bcf4c1eaSMatan Barak 	struct ib_cq_init_attr cq_attr = {.cqe = 1};
31767722f47eSHaggai Eran 	int port;
3177e126ba97SEli Cohen 	int ret = 0;
3178e126ba97SEli Cohen 
3179e126ba97SEli Cohen 	dev = container_of(devr, struct mlx5_ib_dev, devr);
3180e126ba97SEli Cohen 
3181d16e91daSHaggai Eran 	mutex_init(&devr->mutex);
3182d16e91daSHaggai Eran 
3183e126ba97SEli Cohen 	devr->p0 = mlx5_ib_alloc_pd(&dev->ib_dev, NULL, NULL);
3184e126ba97SEli Cohen 	if (IS_ERR(devr->p0)) {
3185e126ba97SEli Cohen 		ret = PTR_ERR(devr->p0);
3186e126ba97SEli Cohen 		goto error0;
3187e126ba97SEli Cohen 	}
3188e126ba97SEli Cohen 	devr->p0->device  = &dev->ib_dev;
3189e126ba97SEli Cohen 	devr->p0->uobject = NULL;
3190e126ba97SEli Cohen 	atomic_set(&devr->p0->usecnt, 0);
3191e126ba97SEli Cohen 
3192bcf4c1eaSMatan Barak 	devr->c0 = mlx5_ib_create_cq(&dev->ib_dev, &cq_attr, NULL, NULL);
3193e126ba97SEli Cohen 	if (IS_ERR(devr->c0)) {
3194e126ba97SEli Cohen 		ret = PTR_ERR(devr->c0);
3195e126ba97SEli Cohen 		goto error1;
3196e126ba97SEli Cohen 	}
3197e126ba97SEli Cohen 	devr->c0->device        = &dev->ib_dev;
3198e126ba97SEli Cohen 	devr->c0->uobject       = NULL;
3199e126ba97SEli Cohen 	devr->c0->comp_handler  = NULL;
3200e126ba97SEli Cohen 	devr->c0->event_handler = NULL;
3201e126ba97SEli Cohen 	devr->c0->cq_context    = NULL;
3202e126ba97SEli Cohen 	atomic_set(&devr->c0->usecnt, 0);
3203e126ba97SEli Cohen 
3204e126ba97SEli Cohen 	devr->x0 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
3205e126ba97SEli Cohen 	if (IS_ERR(devr->x0)) {
3206e126ba97SEli Cohen 		ret = PTR_ERR(devr->x0);
3207e126ba97SEli Cohen 		goto error2;
3208e126ba97SEli Cohen 	}
3209e126ba97SEli Cohen 	devr->x0->device = &dev->ib_dev;
3210e126ba97SEli Cohen 	devr->x0->inode = NULL;
3211e126ba97SEli Cohen 	atomic_set(&devr->x0->usecnt, 0);
3212e126ba97SEli Cohen 	mutex_init(&devr->x0->tgt_qp_mutex);
3213e126ba97SEli Cohen 	INIT_LIST_HEAD(&devr->x0->tgt_qp_list);
3214e126ba97SEli Cohen 
3215e126ba97SEli Cohen 	devr->x1 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
3216e126ba97SEli Cohen 	if (IS_ERR(devr->x1)) {
3217e126ba97SEli Cohen 		ret = PTR_ERR(devr->x1);
3218e126ba97SEli Cohen 		goto error3;
3219e126ba97SEli Cohen 	}
3220e126ba97SEli Cohen 	devr->x1->device = &dev->ib_dev;
3221e126ba97SEli Cohen 	devr->x1->inode = NULL;
3222e126ba97SEli Cohen 	atomic_set(&devr->x1->usecnt, 0);
3223e126ba97SEli Cohen 	mutex_init(&devr->x1->tgt_qp_mutex);
3224e126ba97SEli Cohen 	INIT_LIST_HEAD(&devr->x1->tgt_qp_list);
3225e126ba97SEli Cohen 
3226e126ba97SEli Cohen 	memset(&attr, 0, sizeof(attr));
3227e126ba97SEli Cohen 	attr.attr.max_sge = 1;
3228e126ba97SEli Cohen 	attr.attr.max_wr = 1;
3229e126ba97SEli Cohen 	attr.srq_type = IB_SRQT_XRC;
32301a56ff6dSArtemy Kovalyov 	attr.ext.cq = devr->c0;
3231e126ba97SEli Cohen 	attr.ext.xrc.xrcd = devr->x0;
3232e126ba97SEli Cohen 
3233e126ba97SEli Cohen 	devr->s0 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
3234e126ba97SEli Cohen 	if (IS_ERR(devr->s0)) {
3235e126ba97SEli Cohen 		ret = PTR_ERR(devr->s0);
3236e126ba97SEli Cohen 		goto error4;
3237e126ba97SEli Cohen 	}
3238e126ba97SEli Cohen 	devr->s0->device	= &dev->ib_dev;
3239e126ba97SEli Cohen 	devr->s0->pd		= devr->p0;
3240e126ba97SEli Cohen 	devr->s0->uobject       = NULL;
3241e126ba97SEli Cohen 	devr->s0->event_handler = NULL;
3242e126ba97SEli Cohen 	devr->s0->srq_context   = NULL;
3243e126ba97SEli Cohen 	devr->s0->srq_type      = IB_SRQT_XRC;
3244e126ba97SEli Cohen 	devr->s0->ext.xrc.xrcd	= devr->x0;
32451a56ff6dSArtemy Kovalyov 	devr->s0->ext.cq	= devr->c0;
3246e126ba97SEli Cohen 	atomic_inc(&devr->s0->ext.xrc.xrcd->usecnt);
32471a56ff6dSArtemy Kovalyov 	atomic_inc(&devr->s0->ext.cq->usecnt);
3248e126ba97SEli Cohen 	atomic_inc(&devr->p0->usecnt);
3249e126ba97SEli Cohen 	atomic_set(&devr->s0->usecnt, 0);
3250e126ba97SEli Cohen 
32514aa17b28SHaggai Abramonvsky 	memset(&attr, 0, sizeof(attr));
32524aa17b28SHaggai Abramonvsky 	attr.attr.max_sge = 1;
32534aa17b28SHaggai Abramonvsky 	attr.attr.max_wr = 1;
32544aa17b28SHaggai Abramonvsky 	attr.srq_type = IB_SRQT_BASIC;
32554aa17b28SHaggai Abramonvsky 	devr->s1 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
32564aa17b28SHaggai Abramonvsky 	if (IS_ERR(devr->s1)) {
32574aa17b28SHaggai Abramonvsky 		ret = PTR_ERR(devr->s1);
32584aa17b28SHaggai Abramonvsky 		goto error5;
32594aa17b28SHaggai Abramonvsky 	}
32604aa17b28SHaggai Abramonvsky 	devr->s1->device	= &dev->ib_dev;
32614aa17b28SHaggai Abramonvsky 	devr->s1->pd		= devr->p0;
32624aa17b28SHaggai Abramonvsky 	devr->s1->uobject       = NULL;
32634aa17b28SHaggai Abramonvsky 	devr->s1->event_handler = NULL;
32644aa17b28SHaggai Abramonvsky 	devr->s1->srq_context   = NULL;
32654aa17b28SHaggai Abramonvsky 	devr->s1->srq_type      = IB_SRQT_BASIC;
32661a56ff6dSArtemy Kovalyov 	devr->s1->ext.cq	= devr->c0;
32674aa17b28SHaggai Abramonvsky 	atomic_inc(&devr->p0->usecnt);
32681a56ff6dSArtemy Kovalyov 	atomic_set(&devr->s1->usecnt, 0);
32694aa17b28SHaggai Abramonvsky 
32707722f47eSHaggai Eran 	for (port = 0; port < ARRAY_SIZE(devr->ports); ++port) {
32717722f47eSHaggai Eran 		INIT_WORK(&devr->ports[port].pkey_change_work,
32727722f47eSHaggai Eran 			  pkey_change_handler);
32737722f47eSHaggai Eran 		devr->ports[port].devr = devr;
32747722f47eSHaggai Eran 	}
32757722f47eSHaggai Eran 
3276e126ba97SEli Cohen 	return 0;
3277e126ba97SEli Cohen 
32784aa17b28SHaggai Abramonvsky error5:
32794aa17b28SHaggai Abramonvsky 	mlx5_ib_destroy_srq(devr->s0);
3280e126ba97SEli Cohen error4:
3281e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x1);
3282e126ba97SEli Cohen error3:
3283e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x0);
3284e126ba97SEli Cohen error2:
3285e126ba97SEli Cohen 	mlx5_ib_destroy_cq(devr->c0);
3286e126ba97SEli Cohen error1:
3287e126ba97SEli Cohen 	mlx5_ib_dealloc_pd(devr->p0);
3288e126ba97SEli Cohen error0:
3289e126ba97SEli Cohen 	return ret;
3290e126ba97SEli Cohen }
3291e126ba97SEli Cohen 
3292e126ba97SEli Cohen static void destroy_dev_resources(struct mlx5_ib_resources *devr)
3293e126ba97SEli Cohen {
32947722f47eSHaggai Eran 	struct mlx5_ib_dev *dev =
32957722f47eSHaggai Eran 		container_of(devr, struct mlx5_ib_dev, devr);
32967722f47eSHaggai Eran 	int port;
32977722f47eSHaggai Eran 
32984aa17b28SHaggai Abramonvsky 	mlx5_ib_destroy_srq(devr->s1);
3299e126ba97SEli Cohen 	mlx5_ib_destroy_srq(devr->s0);
3300e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x0);
3301e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x1);
3302e126ba97SEli Cohen 	mlx5_ib_destroy_cq(devr->c0);
3303e126ba97SEli Cohen 	mlx5_ib_dealloc_pd(devr->p0);
33047722f47eSHaggai Eran 
33057722f47eSHaggai Eran 	/* Make sure no change P_Key work items are still executing */
33067722f47eSHaggai Eran 	for (port = 0; port < dev->num_ports; ++port)
33077722f47eSHaggai Eran 		cancel_work_sync(&devr->ports[port].pkey_change_work);
3308e126ba97SEli Cohen }
3309e126ba97SEli Cohen 
3310e53505a8SAchiad Shochat static u32 get_core_cap_flags(struct ib_device *ibdev)
3311e53505a8SAchiad Shochat {
3312e53505a8SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
3313e53505a8SAchiad Shochat 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, 1);
3314e53505a8SAchiad Shochat 	u8 l3_type_cap = MLX5_CAP_ROCE(dev->mdev, l3_type);
3315e53505a8SAchiad Shochat 	u8 roce_version_cap = MLX5_CAP_ROCE(dev->mdev, roce_version);
3316e53505a8SAchiad Shochat 	u32 ret = 0;
3317e53505a8SAchiad Shochat 
3318e53505a8SAchiad Shochat 	if (ll == IB_LINK_LAYER_INFINIBAND)
3319e53505a8SAchiad Shochat 		return RDMA_CORE_PORT_IBA_IB;
3320e53505a8SAchiad Shochat 
332172cd5717SOr Gerlitz 	ret = RDMA_CORE_PORT_RAW_PACKET;
332272cd5717SOr Gerlitz 
3323e53505a8SAchiad Shochat 	if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV4_CAP))
332472cd5717SOr Gerlitz 		return ret;
3325e53505a8SAchiad Shochat 
3326e53505a8SAchiad Shochat 	if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV6_CAP))
332772cd5717SOr Gerlitz 		return ret;
3328e53505a8SAchiad Shochat 
3329e53505a8SAchiad Shochat 	if (roce_version_cap & MLX5_ROCE_VERSION_1_CAP)
3330e53505a8SAchiad Shochat 		ret |= RDMA_CORE_PORT_IBA_ROCE;
3331e53505a8SAchiad Shochat 
3332e53505a8SAchiad Shochat 	if (roce_version_cap & MLX5_ROCE_VERSION_2_CAP)
3333e53505a8SAchiad Shochat 		ret |= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
3334e53505a8SAchiad Shochat 
3335e53505a8SAchiad Shochat 	return ret;
3336e53505a8SAchiad Shochat }
3337e53505a8SAchiad Shochat 
33387738613eSIra Weiny static int mlx5_port_immutable(struct ib_device *ibdev, u8 port_num,
33397738613eSIra Weiny 			       struct ib_port_immutable *immutable)
33407738613eSIra Weiny {
33417738613eSIra Weiny 	struct ib_port_attr attr;
3342ca5b91d6SOr Gerlitz 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
3343ca5b91d6SOr Gerlitz 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, port_num);
33447738613eSIra Weiny 	int err;
33457738613eSIra Weiny 
3346c4550c63SOr Gerlitz 	immutable->core_cap_flags = get_core_cap_flags(ibdev);
3347c4550c63SOr Gerlitz 
3348c4550c63SOr Gerlitz 	err = ib_query_port(ibdev, port_num, &attr);
33497738613eSIra Weiny 	if (err)
33507738613eSIra Weiny 		return err;
33517738613eSIra Weiny 
33527738613eSIra Weiny 	immutable->pkey_tbl_len = attr.pkey_tbl_len;
33537738613eSIra Weiny 	immutable->gid_tbl_len = attr.gid_tbl_len;
3354e53505a8SAchiad Shochat 	immutable->core_cap_flags = get_core_cap_flags(ibdev);
3355ca5b91d6SOr Gerlitz 	if ((ll == IB_LINK_LAYER_INFINIBAND) || MLX5_CAP_GEN(dev->mdev, roce))
3356337877a4SIra Weiny 		immutable->max_mad_size = IB_MGMT_MAD_SIZE;
33577738613eSIra Weiny 
33587738613eSIra Weiny 	return 0;
33597738613eSIra Weiny }
33607738613eSIra Weiny 
33619abb0d1bSLeon Romanovsky static void get_dev_fw_str(struct ib_device *ibdev, char *str)
3362c7342823SIra Weiny {
3363c7342823SIra Weiny 	struct mlx5_ib_dev *dev =
3364c7342823SIra Weiny 		container_of(ibdev, struct mlx5_ib_dev, ib_dev);
33659abb0d1bSLeon Romanovsky 	snprintf(str, IB_FW_VERSION_NAME_MAX, "%d.%d.%04d",
33669abb0d1bSLeon Romanovsky 		 fw_rev_maj(dev->mdev), fw_rev_min(dev->mdev),
33679abb0d1bSLeon Romanovsky 		 fw_rev_sub(dev->mdev));
3368c7342823SIra Weiny }
3369c7342823SIra Weiny 
337045f95acdSOr Gerlitz static int mlx5_eth_lag_init(struct mlx5_ib_dev *dev)
33719ef9c640SAviv Heller {
33729ef9c640SAviv Heller 	struct mlx5_core_dev *mdev = dev->mdev;
33739ef9c640SAviv Heller 	struct mlx5_flow_namespace *ns = mlx5_get_flow_namespace(mdev,
33749ef9c640SAviv Heller 								 MLX5_FLOW_NAMESPACE_LAG);
33759ef9c640SAviv Heller 	struct mlx5_flow_table *ft;
33769ef9c640SAviv Heller 	int err;
33779ef9c640SAviv Heller 
33789ef9c640SAviv Heller 	if (!ns || !mlx5_lag_is_active(mdev))
33799ef9c640SAviv Heller 		return 0;
33809ef9c640SAviv Heller 
33819ef9c640SAviv Heller 	err = mlx5_cmd_create_vport_lag(mdev);
33829ef9c640SAviv Heller 	if (err)
33839ef9c640SAviv Heller 		return err;
33849ef9c640SAviv Heller 
33859ef9c640SAviv Heller 	ft = mlx5_create_lag_demux_flow_table(ns, 0, 0);
33869ef9c640SAviv Heller 	if (IS_ERR(ft)) {
33879ef9c640SAviv Heller 		err = PTR_ERR(ft);
33889ef9c640SAviv Heller 		goto err_destroy_vport_lag;
33899ef9c640SAviv Heller 	}
33909ef9c640SAviv Heller 
33919ef9c640SAviv Heller 	dev->flow_db.lag_demux_ft = ft;
33929ef9c640SAviv Heller 	return 0;
33939ef9c640SAviv Heller 
33949ef9c640SAviv Heller err_destroy_vport_lag:
33959ef9c640SAviv Heller 	mlx5_cmd_destroy_vport_lag(mdev);
33969ef9c640SAviv Heller 	return err;
33979ef9c640SAviv Heller }
33989ef9c640SAviv Heller 
339945f95acdSOr Gerlitz static void mlx5_eth_lag_cleanup(struct mlx5_ib_dev *dev)
34009ef9c640SAviv Heller {
34019ef9c640SAviv Heller 	struct mlx5_core_dev *mdev = dev->mdev;
34029ef9c640SAviv Heller 
34039ef9c640SAviv Heller 	if (dev->flow_db.lag_demux_ft) {
34049ef9c640SAviv Heller 		mlx5_destroy_flow_table(dev->flow_db.lag_demux_ft);
34059ef9c640SAviv Heller 		dev->flow_db.lag_demux_ft = NULL;
34069ef9c640SAviv Heller 
34079ef9c640SAviv Heller 		mlx5_cmd_destroy_vport_lag(mdev);
34089ef9c640SAviv Heller 	}
34099ef9c640SAviv Heller }
34109ef9c640SAviv Heller 
3411d012f5d6SOr Gerlitz static int mlx5_add_netdev_notifier(struct mlx5_ib_dev *dev)
3412fc24fc5eSAchiad Shochat {
3413e53505a8SAchiad Shochat 	int err;
3414e53505a8SAchiad Shochat 
3415fc24fc5eSAchiad Shochat 	dev->roce.nb.notifier_call = mlx5_netdev_event;
3416e53505a8SAchiad Shochat 	err = register_netdevice_notifier(&dev->roce.nb);
34175ec8c83eSAviv Heller 	if (err) {
34185ec8c83eSAviv Heller 		dev->roce.nb.notifier_call = NULL;
3419e53505a8SAchiad Shochat 		return err;
34205ec8c83eSAviv Heller 	}
3421e53505a8SAchiad Shochat 
3422d012f5d6SOr Gerlitz 	return 0;
3423d012f5d6SOr Gerlitz }
3424d012f5d6SOr Gerlitz 
3425d012f5d6SOr Gerlitz static void mlx5_remove_netdev_notifier(struct mlx5_ib_dev *dev)
3426e126ba97SEli Cohen {
3427e126ba97SEli Cohen 	if (dev->roce.nb.notifier_call) {
3428e126ba97SEli Cohen 		unregister_netdevice_notifier(&dev->roce.nb);
3429e126ba97SEli Cohen 		dev->roce.nb.notifier_call = NULL;
3430e126ba97SEli Cohen 	}
3431e126ba97SEli Cohen }
3432e126ba97SEli Cohen 
343345f95acdSOr Gerlitz static int mlx5_enable_eth(struct mlx5_ib_dev *dev)
3434e126ba97SEli Cohen {
3435e126ba97SEli Cohen 	int err;
3436e126ba97SEli Cohen 
3437d012f5d6SOr Gerlitz 	err = mlx5_add_netdev_notifier(dev);
3438d012f5d6SOr Gerlitz 	if (err)
3439e53505a8SAchiad Shochat 		return err;
3440e53505a8SAchiad Shochat 
3441ca5b91d6SOr Gerlitz 	if (MLX5_CAP_GEN(dev->mdev, roce)) {
3442e53505a8SAchiad Shochat 		err = mlx5_nic_vport_enable_roce(dev->mdev);
3443e53505a8SAchiad Shochat 		if (err)
3444e53505a8SAchiad Shochat 			goto err_unregister_netdevice_notifier;
3445ca5b91d6SOr Gerlitz 	}
3446e53505a8SAchiad Shochat 
344745f95acdSOr Gerlitz 	err = mlx5_eth_lag_init(dev);
34489ef9c640SAviv Heller 	if (err)
34499ef9c640SAviv Heller 		goto err_disable_roce;
34509ef9c640SAviv Heller 
3451e53505a8SAchiad Shochat 	return 0;
3452e53505a8SAchiad Shochat 
34539ef9c640SAviv Heller err_disable_roce:
3454ca5b91d6SOr Gerlitz 	if (MLX5_CAP_GEN(dev->mdev, roce))
34559ef9c640SAviv Heller 		mlx5_nic_vport_disable_roce(dev->mdev);
34569ef9c640SAviv Heller 
3457e53505a8SAchiad Shochat err_unregister_netdevice_notifier:
3458d012f5d6SOr Gerlitz 	mlx5_remove_netdev_notifier(dev);
3459e53505a8SAchiad Shochat 	return err;
3460fc24fc5eSAchiad Shochat }
3461fc24fc5eSAchiad Shochat 
346245f95acdSOr Gerlitz static void mlx5_disable_eth(struct mlx5_ib_dev *dev)
3463fc24fc5eSAchiad Shochat {
346445f95acdSOr Gerlitz 	mlx5_eth_lag_cleanup(dev);
3465ca5b91d6SOr Gerlitz 	if (MLX5_CAP_GEN(dev->mdev, roce))
3466e53505a8SAchiad Shochat 		mlx5_nic_vport_disable_roce(dev->mdev);
3467fc24fc5eSAchiad Shochat }
3468fc24fc5eSAchiad Shochat 
3469e1f24a79SParav Pandit struct mlx5_ib_counter {
34707c16f477SKamal Heib 	const char *name;
34717c16f477SKamal Heib 	size_t offset;
34727c16f477SKamal Heib };
34737c16f477SKamal Heib 
34747c16f477SKamal Heib #define INIT_Q_COUNTER(_name)		\
34757c16f477SKamal Heib 	{ .name = #_name, .offset = MLX5_BYTE_OFF(query_q_counter_out, _name)}
34767c16f477SKamal Heib 
3477e1f24a79SParav Pandit static const struct mlx5_ib_counter basic_q_cnts[] = {
34787c16f477SKamal Heib 	INIT_Q_COUNTER(rx_write_requests),
34797c16f477SKamal Heib 	INIT_Q_COUNTER(rx_read_requests),
34807c16f477SKamal Heib 	INIT_Q_COUNTER(rx_atomic_requests),
34817c16f477SKamal Heib 	INIT_Q_COUNTER(out_of_buffer),
34827c16f477SKamal Heib };
34837c16f477SKamal Heib 
3484e1f24a79SParav Pandit static const struct mlx5_ib_counter out_of_seq_q_cnts[] = {
34857c16f477SKamal Heib 	INIT_Q_COUNTER(out_of_sequence),
34867c16f477SKamal Heib };
34877c16f477SKamal Heib 
3488e1f24a79SParav Pandit static const struct mlx5_ib_counter retrans_q_cnts[] = {
34897c16f477SKamal Heib 	INIT_Q_COUNTER(duplicate_request),
34907c16f477SKamal Heib 	INIT_Q_COUNTER(rnr_nak_retry_err),
34917c16f477SKamal Heib 	INIT_Q_COUNTER(packet_seq_err),
34927c16f477SKamal Heib 	INIT_Q_COUNTER(implied_nak_seq_err),
34937c16f477SKamal Heib 	INIT_Q_COUNTER(local_ack_timeout_err),
34947c16f477SKamal Heib };
34957c16f477SKamal Heib 
3496e1f24a79SParav Pandit #define INIT_CONG_COUNTER(_name)		\
3497e1f24a79SParav Pandit 	{ .name = #_name, .offset =	\
3498e1f24a79SParav Pandit 		MLX5_BYTE_OFF(query_cong_statistics_out, _name ## _high)}
3499e1f24a79SParav Pandit 
3500e1f24a79SParav Pandit static const struct mlx5_ib_counter cong_cnts[] = {
3501e1f24a79SParav Pandit 	INIT_CONG_COUNTER(rp_cnp_ignored),
3502e1f24a79SParav Pandit 	INIT_CONG_COUNTER(rp_cnp_handled),
3503e1f24a79SParav Pandit 	INIT_CONG_COUNTER(np_ecn_marked_roce_packets),
3504e1f24a79SParav Pandit 	INIT_CONG_COUNTER(np_cnp_sent),
3505e1f24a79SParav Pandit };
3506e1f24a79SParav Pandit 
350758dcb60aSParav Pandit static const struct mlx5_ib_counter extended_err_cnts[] = {
350858dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_local_length_error),
350958dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_cqe_error),
351058dcb60aSParav Pandit 	INIT_Q_COUNTER(req_cqe_error),
351158dcb60aSParav Pandit 	INIT_Q_COUNTER(req_remote_invalid_request),
351258dcb60aSParav Pandit 	INIT_Q_COUNTER(req_remote_access_errors),
351358dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_remote_access_errors),
351458dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_cqe_flush_error),
351558dcb60aSParav Pandit 	INIT_Q_COUNTER(req_cqe_flush_error),
351658dcb60aSParav Pandit };
351758dcb60aSParav Pandit 
3518e1f24a79SParav Pandit static void mlx5_ib_dealloc_counters(struct mlx5_ib_dev *dev)
35190837e86aSMark Bloch {
35200837e86aSMark Bloch 	unsigned int i;
35210837e86aSMark Bloch 
35227c16f477SKamal Heib 	for (i = 0; i < dev->num_ports; i++) {
35230837e86aSMark Bloch 		mlx5_core_dealloc_q_counter(dev->mdev,
3524e1f24a79SParav Pandit 					    dev->port[i].cnts.set_id);
3525e1f24a79SParav Pandit 		kfree(dev->port[i].cnts.names);
3526e1f24a79SParav Pandit 		kfree(dev->port[i].cnts.offsets);
35277c16f477SKamal Heib 	}
35287c16f477SKamal Heib }
35297c16f477SKamal Heib 
3530e1f24a79SParav Pandit static int __mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev,
3531e1f24a79SParav Pandit 				    struct mlx5_ib_counters *cnts)
35327c16f477SKamal Heib {
35337c16f477SKamal Heib 	u32 num_counters;
35347c16f477SKamal Heib 
35357c16f477SKamal Heib 	num_counters = ARRAY_SIZE(basic_q_cnts);
35367c16f477SKamal Heib 
35377c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt))
35387c16f477SKamal Heib 		num_counters += ARRAY_SIZE(out_of_seq_q_cnts);
35397c16f477SKamal Heib 
35407c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters))
35417c16f477SKamal Heib 		num_counters += ARRAY_SIZE(retrans_q_cnts);
354258dcb60aSParav Pandit 
354358dcb60aSParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters))
354458dcb60aSParav Pandit 		num_counters += ARRAY_SIZE(extended_err_cnts);
354558dcb60aSParav Pandit 
3546e1f24a79SParav Pandit 	cnts->num_q_counters = num_counters;
35477c16f477SKamal Heib 
3548e1f24a79SParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
3549e1f24a79SParav Pandit 		cnts->num_cong_counters = ARRAY_SIZE(cong_cnts);
3550e1f24a79SParav Pandit 		num_counters += ARRAY_SIZE(cong_cnts);
3551e1f24a79SParav Pandit 	}
3552e1f24a79SParav Pandit 
3553e1f24a79SParav Pandit 	cnts->names = kcalloc(num_counters, sizeof(cnts->names), GFP_KERNEL);
3554e1f24a79SParav Pandit 	if (!cnts->names)
35557c16f477SKamal Heib 		return -ENOMEM;
35567c16f477SKamal Heib 
3557e1f24a79SParav Pandit 	cnts->offsets = kcalloc(num_counters,
3558e1f24a79SParav Pandit 				sizeof(cnts->offsets), GFP_KERNEL);
3559e1f24a79SParav Pandit 	if (!cnts->offsets)
35607c16f477SKamal Heib 		goto err_names;
35617c16f477SKamal Heib 
35627c16f477SKamal Heib 	return 0;
35637c16f477SKamal Heib 
35647c16f477SKamal Heib err_names:
3565e1f24a79SParav Pandit 	kfree(cnts->names);
35667c16f477SKamal Heib 	return -ENOMEM;
35677c16f477SKamal Heib }
35687c16f477SKamal Heib 
3569e1f24a79SParav Pandit static void mlx5_ib_fill_counters(struct mlx5_ib_dev *dev,
35707c16f477SKamal Heib 				  const char **names,
35717c16f477SKamal Heib 				  size_t *offsets)
35727c16f477SKamal Heib {
35737c16f477SKamal Heib 	int i;
35747c16f477SKamal Heib 	int j = 0;
35757c16f477SKamal Heib 
35767c16f477SKamal Heib 	for (i = 0; i < ARRAY_SIZE(basic_q_cnts); i++, j++) {
35777c16f477SKamal Heib 		names[j] = basic_q_cnts[i].name;
35787c16f477SKamal Heib 		offsets[j] = basic_q_cnts[i].offset;
35797c16f477SKamal Heib 	}
35807c16f477SKamal Heib 
35817c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt)) {
35827c16f477SKamal Heib 		for (i = 0; i < ARRAY_SIZE(out_of_seq_q_cnts); i++, j++) {
35837c16f477SKamal Heib 			names[j] = out_of_seq_q_cnts[i].name;
35847c16f477SKamal Heib 			offsets[j] = out_of_seq_q_cnts[i].offset;
35857c16f477SKamal Heib 		}
35867c16f477SKamal Heib 	}
35877c16f477SKamal Heib 
35887c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters)) {
35897c16f477SKamal Heib 		for (i = 0; i < ARRAY_SIZE(retrans_q_cnts); i++, j++) {
35907c16f477SKamal Heib 			names[j] = retrans_q_cnts[i].name;
35917c16f477SKamal Heib 			offsets[j] = retrans_q_cnts[i].offset;
35927c16f477SKamal Heib 		}
35937c16f477SKamal Heib 	}
3594e1f24a79SParav Pandit 
359558dcb60aSParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters)) {
359658dcb60aSParav Pandit 		for (i = 0; i < ARRAY_SIZE(extended_err_cnts); i++, j++) {
359758dcb60aSParav Pandit 			names[j] = extended_err_cnts[i].name;
359858dcb60aSParav Pandit 			offsets[j] = extended_err_cnts[i].offset;
359958dcb60aSParav Pandit 		}
360058dcb60aSParav Pandit 	}
360158dcb60aSParav Pandit 
3602e1f24a79SParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
3603e1f24a79SParav Pandit 		for (i = 0; i < ARRAY_SIZE(cong_cnts); i++, j++) {
3604e1f24a79SParav Pandit 			names[j] = cong_cnts[i].name;
3605e1f24a79SParav Pandit 			offsets[j] = cong_cnts[i].offset;
3606e1f24a79SParav Pandit 		}
3607e1f24a79SParav Pandit 	}
36080837e86aSMark Bloch }
36090837e86aSMark Bloch 
3610e1f24a79SParav Pandit static int mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev)
36110837e86aSMark Bloch {
36120837e86aSMark Bloch 	int i;
36130837e86aSMark Bloch 	int ret;
36140837e86aSMark Bloch 
36150837e86aSMark Bloch 	for (i = 0; i < dev->num_ports; i++) {
36167c16f477SKamal Heib 		struct mlx5_ib_port *port = &dev->port[i];
36177c16f477SKamal Heib 
36180837e86aSMark Bloch 		ret = mlx5_core_alloc_q_counter(dev->mdev,
3619e1f24a79SParav Pandit 						&port->cnts.set_id);
36200837e86aSMark Bloch 		if (ret) {
36210837e86aSMark Bloch 			mlx5_ib_warn(dev,
36220837e86aSMark Bloch 				     "couldn't allocate queue counter for port %d, err %d\n",
36230837e86aSMark Bloch 				     i + 1, ret);
36240837e86aSMark Bloch 			goto dealloc_counters;
36250837e86aSMark Bloch 		}
36267c16f477SKamal Heib 
3627e1f24a79SParav Pandit 		ret = __mlx5_ib_alloc_counters(dev, &port->cnts);
36287c16f477SKamal Heib 		if (ret)
36297c16f477SKamal Heib 			goto dealloc_counters;
36307c16f477SKamal Heib 
3631e1f24a79SParav Pandit 		mlx5_ib_fill_counters(dev, port->cnts.names,
3632e1f24a79SParav Pandit 				      port->cnts.offsets);
36330837e86aSMark Bloch 	}
36340837e86aSMark Bloch 
36350837e86aSMark Bloch 	return 0;
36360837e86aSMark Bloch 
36370837e86aSMark Bloch dealloc_counters:
36380837e86aSMark Bloch 	while (--i >= 0)
36390837e86aSMark Bloch 		mlx5_core_dealloc_q_counter(dev->mdev,
3640e1f24a79SParav Pandit 					    dev->port[i].cnts.set_id);
36410837e86aSMark Bloch 
36420837e86aSMark Bloch 	return ret;
36430837e86aSMark Bloch }
36440837e86aSMark Bloch 
36450ad17a8fSMark Bloch static struct rdma_hw_stats *mlx5_ib_alloc_hw_stats(struct ib_device *ibdev,
36460ad17a8fSMark Bloch 						    u8 port_num)
36470ad17a8fSMark Bloch {
36487c16f477SKamal Heib 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
36497c16f477SKamal Heib 	struct mlx5_ib_port *port = &dev->port[port_num - 1];
36500ad17a8fSMark Bloch 
36510ad17a8fSMark Bloch 	/* We support only per port stats */
36520ad17a8fSMark Bloch 	if (port_num == 0)
36530ad17a8fSMark Bloch 		return NULL;
36540ad17a8fSMark Bloch 
3655e1f24a79SParav Pandit 	return rdma_alloc_hw_stats_struct(port->cnts.names,
3656e1f24a79SParav Pandit 					  port->cnts.num_q_counters +
3657e1f24a79SParav Pandit 					  port->cnts.num_cong_counters,
36580ad17a8fSMark Bloch 					  RDMA_HW_STATS_DEFAULT_LIFESPAN);
36590ad17a8fSMark Bloch }
36600ad17a8fSMark Bloch 
3661e1f24a79SParav Pandit static int mlx5_ib_query_q_counters(struct mlx5_ib_dev *dev,
3662e1f24a79SParav Pandit 				    struct mlx5_ib_port *port,
3663e1f24a79SParav Pandit 				    struct rdma_hw_stats *stats)
3664e1f24a79SParav Pandit {
3665e1f24a79SParav Pandit 	int outlen = MLX5_ST_SZ_BYTES(query_q_counter_out);
3666e1f24a79SParav Pandit 	void *out;
3667e1f24a79SParav Pandit 	__be32 val;
3668e1f24a79SParav Pandit 	int ret, i;
3669e1f24a79SParav Pandit 
36701b9a07eeSLeon Romanovsky 	out = kvzalloc(outlen, GFP_KERNEL);
3671e1f24a79SParav Pandit 	if (!out)
3672e1f24a79SParav Pandit 		return -ENOMEM;
3673e1f24a79SParav Pandit 
3674e1f24a79SParav Pandit 	ret = mlx5_core_query_q_counter(dev->mdev,
3675e1f24a79SParav Pandit 					port->cnts.set_id, 0,
3676e1f24a79SParav Pandit 					out, outlen);
3677e1f24a79SParav Pandit 	if (ret)
3678e1f24a79SParav Pandit 		goto free;
3679e1f24a79SParav Pandit 
3680e1f24a79SParav Pandit 	for (i = 0; i < port->cnts.num_q_counters; i++) {
3681e1f24a79SParav Pandit 		val = *(__be32 *)(out + port->cnts.offsets[i]);
3682e1f24a79SParav Pandit 		stats->value[i] = (u64)be32_to_cpu(val);
3683e1f24a79SParav Pandit 	}
3684e1f24a79SParav Pandit 
3685e1f24a79SParav Pandit free:
3686e1f24a79SParav Pandit 	kvfree(out);
3687e1f24a79SParav Pandit 	return ret;
3688e1f24a79SParav Pandit }
3689e1f24a79SParav Pandit 
3690e1f24a79SParav Pandit static int mlx5_ib_query_cong_counters(struct mlx5_ib_dev *dev,
3691e1f24a79SParav Pandit 				       struct mlx5_ib_port *port,
3692e1f24a79SParav Pandit 				       struct rdma_hw_stats *stats)
3693e1f24a79SParav Pandit {
3694e1f24a79SParav Pandit 	int outlen = MLX5_ST_SZ_BYTES(query_cong_statistics_out);
3695e1f24a79SParav Pandit 	void *out;
3696e1f24a79SParav Pandit 	int ret, i;
3697e1f24a79SParav Pandit 	int offset = port->cnts.num_q_counters;
3698e1f24a79SParav Pandit 
36991b9a07eeSLeon Romanovsky 	out = kvzalloc(outlen, GFP_KERNEL);
3700e1f24a79SParav Pandit 	if (!out)
3701e1f24a79SParav Pandit 		return -ENOMEM;
3702e1f24a79SParav Pandit 
3703e1f24a79SParav Pandit 	ret = mlx5_cmd_query_cong_counter(dev->mdev, false, out, outlen);
3704e1f24a79SParav Pandit 	if (ret)
3705e1f24a79SParav Pandit 		goto free;
3706e1f24a79SParav Pandit 
3707e1f24a79SParav Pandit 	for (i = 0; i < port->cnts.num_cong_counters; i++) {
3708e1f24a79SParav Pandit 		stats->value[i + offset] =
3709e1f24a79SParav Pandit 			be64_to_cpup((__be64 *)(out +
3710e1f24a79SParav Pandit 				     port->cnts.offsets[i + offset]));
3711e1f24a79SParav Pandit 	}
3712e1f24a79SParav Pandit 
3713e1f24a79SParav Pandit free:
3714e1f24a79SParav Pandit 	kvfree(out);
3715e1f24a79SParav Pandit 	return ret;
3716e1f24a79SParav Pandit }
3717e1f24a79SParav Pandit 
37180ad17a8fSMark Bloch static int mlx5_ib_get_hw_stats(struct ib_device *ibdev,
37190ad17a8fSMark Bloch 				struct rdma_hw_stats *stats,
37207c16f477SKamal Heib 				u8 port_num, int index)
37210ad17a8fSMark Bloch {
37220ad17a8fSMark Bloch 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
37237c16f477SKamal Heib 	struct mlx5_ib_port *port = &dev->port[port_num - 1];
3724e1f24a79SParav Pandit 	int ret, num_counters;
37250ad17a8fSMark Bloch 
37267c16f477SKamal Heib 	if (!stats)
3727e1f24a79SParav Pandit 		return -EINVAL;
37280ad17a8fSMark Bloch 
3729e1f24a79SParav Pandit 	ret = mlx5_ib_query_q_counters(dev, port, stats);
37300ad17a8fSMark Bloch 	if (ret)
3731e1f24a79SParav Pandit 		return ret;
3732e1f24a79SParav Pandit 	num_counters = port->cnts.num_q_counters;
37330ad17a8fSMark Bloch 
3734e1f24a79SParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
3735e1f24a79SParav Pandit 		ret = mlx5_ib_query_cong_counters(dev, port, stats);
3736e1f24a79SParav Pandit 		if (ret)
3737e1f24a79SParav Pandit 			return ret;
3738e1f24a79SParav Pandit 		num_counters += port->cnts.num_cong_counters;
37390ad17a8fSMark Bloch 	}
37407c16f477SKamal Heib 
3741e1f24a79SParav Pandit 	return num_counters;
37420ad17a8fSMark Bloch }
37430ad17a8fSMark Bloch 
37448e959601SNiranjana Vishwanathapura static void mlx5_ib_free_rdma_netdev(struct net_device *netdev)
37458e959601SNiranjana Vishwanathapura {
37468e959601SNiranjana Vishwanathapura 	return mlx5_rdma_netdev_free(netdev);
37478e959601SNiranjana Vishwanathapura }
37488e959601SNiranjana Vishwanathapura 
3749693dfd5aSErez Shitrit static struct net_device*
3750693dfd5aSErez Shitrit mlx5_ib_alloc_rdma_netdev(struct ib_device *hca,
3751693dfd5aSErez Shitrit 			  u8 port_num,
3752693dfd5aSErez Shitrit 			  enum rdma_netdev_t type,
3753693dfd5aSErez Shitrit 			  const char *name,
3754693dfd5aSErez Shitrit 			  unsigned char name_assign_type,
3755693dfd5aSErez Shitrit 			  void (*setup)(struct net_device *))
3756693dfd5aSErez Shitrit {
37578e959601SNiranjana Vishwanathapura 	struct net_device *netdev;
37588e959601SNiranjana Vishwanathapura 	struct rdma_netdev *rn;
37598e959601SNiranjana Vishwanathapura 
3760693dfd5aSErez Shitrit 	if (type != RDMA_NETDEV_IPOIB)
3761693dfd5aSErez Shitrit 		return ERR_PTR(-EOPNOTSUPP);
3762693dfd5aSErez Shitrit 
37638e959601SNiranjana Vishwanathapura 	netdev = mlx5_rdma_netdev_alloc(to_mdev(hca)->mdev, hca,
3764693dfd5aSErez Shitrit 					name, setup);
37658e959601SNiranjana Vishwanathapura 	if (likely(!IS_ERR_OR_NULL(netdev))) {
37668e959601SNiranjana Vishwanathapura 		rn = netdev_priv(netdev);
37678e959601SNiranjana Vishwanathapura 		rn->free_rdma_netdev = mlx5_ib_free_rdma_netdev;
3768693dfd5aSErez Shitrit 	}
37698e959601SNiranjana Vishwanathapura 	return netdev;
3770693dfd5aSErez Shitrit }
3771693dfd5aSErez Shitrit 
3772fe248c3aSMaor Gottlieb static void delay_drop_debugfs_cleanup(struct mlx5_ib_dev *dev)
3773fe248c3aSMaor Gottlieb {
3774fe248c3aSMaor Gottlieb 	if (!dev->delay_drop.dbg)
3775fe248c3aSMaor Gottlieb 		return;
3776fe248c3aSMaor Gottlieb 	debugfs_remove_recursive(dev->delay_drop.dbg->dir_debugfs);
3777fe248c3aSMaor Gottlieb 	kfree(dev->delay_drop.dbg);
3778fe248c3aSMaor Gottlieb 	dev->delay_drop.dbg = NULL;
3779fe248c3aSMaor Gottlieb }
3780fe248c3aSMaor Gottlieb 
378103404e8aSMaor Gottlieb static void cancel_delay_drop(struct mlx5_ib_dev *dev)
378203404e8aSMaor Gottlieb {
378303404e8aSMaor Gottlieb 	if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
378403404e8aSMaor Gottlieb 		return;
378503404e8aSMaor Gottlieb 
378603404e8aSMaor Gottlieb 	cancel_work_sync(&dev->delay_drop.delay_drop_work);
3787fe248c3aSMaor Gottlieb 	delay_drop_debugfs_cleanup(dev);
3788fe248c3aSMaor Gottlieb }
3789fe248c3aSMaor Gottlieb 
3790fe248c3aSMaor Gottlieb static ssize_t delay_drop_timeout_read(struct file *filp, char __user *buf,
3791fe248c3aSMaor Gottlieb 				       size_t count, loff_t *pos)
3792fe248c3aSMaor Gottlieb {
3793fe248c3aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
3794fe248c3aSMaor Gottlieb 	char lbuf[20];
3795fe248c3aSMaor Gottlieb 	int len;
3796fe248c3aSMaor Gottlieb 
3797fe248c3aSMaor Gottlieb 	len = snprintf(lbuf, sizeof(lbuf), "%u\n", delay_drop->timeout);
3798fe248c3aSMaor Gottlieb 	return simple_read_from_buffer(buf, count, pos, lbuf, len);
3799fe248c3aSMaor Gottlieb }
3800fe248c3aSMaor Gottlieb 
3801fe248c3aSMaor Gottlieb static ssize_t delay_drop_timeout_write(struct file *filp, const char __user *buf,
3802fe248c3aSMaor Gottlieb 					size_t count, loff_t *pos)
3803fe248c3aSMaor Gottlieb {
3804fe248c3aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
3805fe248c3aSMaor Gottlieb 	u32 timeout;
3806fe248c3aSMaor Gottlieb 	u32 var;
3807fe248c3aSMaor Gottlieb 
3808fe248c3aSMaor Gottlieb 	if (kstrtouint_from_user(buf, count, 0, &var))
3809fe248c3aSMaor Gottlieb 		return -EFAULT;
3810fe248c3aSMaor Gottlieb 
3811fe248c3aSMaor Gottlieb 	timeout = min_t(u32, roundup(var, 100), MLX5_MAX_DELAY_DROP_TIMEOUT_MS *
3812fe248c3aSMaor Gottlieb 			1000);
3813fe248c3aSMaor Gottlieb 	if (timeout != var)
3814fe248c3aSMaor Gottlieb 		mlx5_ib_dbg(delay_drop->dev, "Round delay drop timeout to %u usec\n",
3815fe248c3aSMaor Gottlieb 			    timeout);
3816fe248c3aSMaor Gottlieb 
3817fe248c3aSMaor Gottlieb 	delay_drop->timeout = timeout;
3818fe248c3aSMaor Gottlieb 
3819fe248c3aSMaor Gottlieb 	return count;
3820fe248c3aSMaor Gottlieb }
3821fe248c3aSMaor Gottlieb 
3822fe248c3aSMaor Gottlieb static const struct file_operations fops_delay_drop_timeout = {
3823fe248c3aSMaor Gottlieb 	.owner	= THIS_MODULE,
3824fe248c3aSMaor Gottlieb 	.open	= simple_open,
3825fe248c3aSMaor Gottlieb 	.write	= delay_drop_timeout_write,
3826fe248c3aSMaor Gottlieb 	.read	= delay_drop_timeout_read,
3827fe248c3aSMaor Gottlieb };
3828fe248c3aSMaor Gottlieb 
3829fe248c3aSMaor Gottlieb static int delay_drop_debugfs_init(struct mlx5_ib_dev *dev)
3830fe248c3aSMaor Gottlieb {
3831fe248c3aSMaor Gottlieb 	struct mlx5_ib_dbg_delay_drop *dbg;
3832fe248c3aSMaor Gottlieb 
3833fe248c3aSMaor Gottlieb 	if (!mlx5_debugfs_root)
3834fe248c3aSMaor Gottlieb 		return 0;
3835fe248c3aSMaor Gottlieb 
3836fe248c3aSMaor Gottlieb 	dbg = kzalloc(sizeof(*dbg), GFP_KERNEL);
3837fe248c3aSMaor Gottlieb 	if (!dbg)
3838fe248c3aSMaor Gottlieb 		return -ENOMEM;
3839fe248c3aSMaor Gottlieb 
3840fe248c3aSMaor Gottlieb 	dbg->dir_debugfs =
3841fe248c3aSMaor Gottlieb 		debugfs_create_dir("delay_drop",
3842fe248c3aSMaor Gottlieb 				   dev->mdev->priv.dbg_root);
3843fe248c3aSMaor Gottlieb 	if (!dbg->dir_debugfs)
3844fe248c3aSMaor Gottlieb 		return -ENOMEM;
3845fe248c3aSMaor Gottlieb 
3846fe248c3aSMaor Gottlieb 	dbg->events_cnt_debugfs =
3847fe248c3aSMaor Gottlieb 		debugfs_create_atomic_t("num_timeout_events", 0400,
3848fe248c3aSMaor Gottlieb 					dbg->dir_debugfs,
3849fe248c3aSMaor Gottlieb 					&dev->delay_drop.events_cnt);
3850fe248c3aSMaor Gottlieb 	if (!dbg->events_cnt_debugfs)
3851fe248c3aSMaor Gottlieb 		goto out_debugfs;
3852fe248c3aSMaor Gottlieb 
3853fe248c3aSMaor Gottlieb 	dbg->rqs_cnt_debugfs =
3854fe248c3aSMaor Gottlieb 		debugfs_create_atomic_t("num_rqs", 0400,
3855fe248c3aSMaor Gottlieb 					dbg->dir_debugfs,
3856fe248c3aSMaor Gottlieb 					&dev->delay_drop.rqs_cnt);
3857fe248c3aSMaor Gottlieb 	if (!dbg->rqs_cnt_debugfs)
3858fe248c3aSMaor Gottlieb 		goto out_debugfs;
3859fe248c3aSMaor Gottlieb 
3860fe248c3aSMaor Gottlieb 	dbg->timeout_debugfs =
3861fe248c3aSMaor Gottlieb 		debugfs_create_file("timeout", 0600,
3862fe248c3aSMaor Gottlieb 				    dbg->dir_debugfs,
3863fe248c3aSMaor Gottlieb 				    &dev->delay_drop,
3864fe248c3aSMaor Gottlieb 				    &fops_delay_drop_timeout);
3865fe248c3aSMaor Gottlieb 	if (!dbg->timeout_debugfs)
3866fe248c3aSMaor Gottlieb 		goto out_debugfs;
3867fe248c3aSMaor Gottlieb 
38684a5fd5d2SMaor Gottlieb 	dev->delay_drop.dbg = dbg;
38694a5fd5d2SMaor Gottlieb 
3870fe248c3aSMaor Gottlieb 	return 0;
3871fe248c3aSMaor Gottlieb 
3872fe248c3aSMaor Gottlieb out_debugfs:
3873fe248c3aSMaor Gottlieb 	delay_drop_debugfs_cleanup(dev);
3874fe248c3aSMaor Gottlieb 	return -ENOMEM;
387503404e8aSMaor Gottlieb }
387603404e8aSMaor Gottlieb 
387703404e8aSMaor Gottlieb static void init_delay_drop(struct mlx5_ib_dev *dev)
387803404e8aSMaor Gottlieb {
387903404e8aSMaor Gottlieb 	if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
388003404e8aSMaor Gottlieb 		return;
388103404e8aSMaor Gottlieb 
388203404e8aSMaor Gottlieb 	mutex_init(&dev->delay_drop.lock);
388303404e8aSMaor Gottlieb 	dev->delay_drop.dev = dev;
388403404e8aSMaor Gottlieb 	dev->delay_drop.activate = false;
388503404e8aSMaor Gottlieb 	dev->delay_drop.timeout = MLX5_MAX_DELAY_DROP_TIMEOUT_MS * 1000;
388603404e8aSMaor Gottlieb 	INIT_WORK(&dev->delay_drop.delay_drop_work, delay_drop_handler);
3887fe248c3aSMaor Gottlieb 	atomic_set(&dev->delay_drop.rqs_cnt, 0);
3888fe248c3aSMaor Gottlieb 	atomic_set(&dev->delay_drop.events_cnt, 0);
3889fe248c3aSMaor Gottlieb 
3890fe248c3aSMaor Gottlieb 	if (delay_drop_debugfs_init(dev))
3891fe248c3aSMaor Gottlieb 		mlx5_ib_warn(dev, "Failed to init delay drop debugfs\n");
389203404e8aSMaor Gottlieb }
389303404e8aSMaor Gottlieb 
389484305d71SLeon Romanovsky static const struct cpumask *
389584305d71SLeon Romanovsky mlx5_ib_get_vector_affinity(struct ib_device *ibdev, int comp_vector)
389640b24403SSagi Grimberg {
389740b24403SSagi Grimberg 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
389840b24403SSagi Grimberg 
389940b24403SSagi Grimberg 	return mlx5_get_vector_affinity(dev->mdev, comp_vector);
390040b24403SSagi Grimberg }
390140b24403SSagi Grimberg 
39029603b61dSJack Morgenstein static void *mlx5_ib_add(struct mlx5_core_dev *mdev)
3903e126ba97SEli Cohen {
3904e126ba97SEli Cohen 	struct mlx5_ib_dev *dev;
3905ebd61f68SAchiad Shochat 	enum rdma_link_layer ll;
3906ebd61f68SAchiad Shochat 	int port_type_cap;
39074babcf97SAviv Heller 	const char *name;
3908e126ba97SEli Cohen 	int err;
3909e126ba97SEli Cohen 	int i;
3910e126ba97SEli Cohen 
3911ebd61f68SAchiad Shochat 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
3912ebd61f68SAchiad Shochat 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
3913ebd61f68SAchiad Shochat 
3914e126ba97SEli Cohen 	printk_once(KERN_INFO "%s", mlx5_version);
3915e126ba97SEli Cohen 
3916e126ba97SEli Cohen 	dev = (struct mlx5_ib_dev *)ib_alloc_device(sizeof(*dev));
3917e126ba97SEli Cohen 	if (!dev)
39189603b61dSJack Morgenstein 		return NULL;
3919e126ba97SEli Cohen 
39209603b61dSJack Morgenstein 	dev->mdev = mdev;
3921e126ba97SEli Cohen 
39220837e86aSMark Bloch 	dev->port = kcalloc(MLX5_CAP_GEN(mdev, num_ports), sizeof(*dev->port),
39230837e86aSMark Bloch 			    GFP_KERNEL);
39240837e86aSMark Bloch 	if (!dev->port)
39250837e86aSMark Bloch 		goto err_dealloc;
39260837e86aSMark Bloch 
3927fc24fc5eSAchiad Shochat 	rwlock_init(&dev->roce.netdev_lock);
3928e126ba97SEli Cohen 	err = get_port_caps(dev);
3929e126ba97SEli Cohen 	if (err)
39300837e86aSMark Bloch 		goto err_free_port;
3931e126ba97SEli Cohen 
39321b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(dev))
3933e126ba97SEli Cohen 		get_ext_port_caps(dev);
3934e126ba97SEli Cohen 
39354babcf97SAviv Heller 	if (!mlx5_lag_is_active(mdev))
39364babcf97SAviv Heller 		name = "mlx5_%d";
39374babcf97SAviv Heller 	else
39384babcf97SAviv Heller 		name = "mlx5_bond_%d";
39394babcf97SAviv Heller 
39404babcf97SAviv Heller 	strlcpy(dev->ib_dev.name, name, IB_DEVICE_NAME_MAX);
3941e126ba97SEli Cohen 	dev->ib_dev.owner		= THIS_MODULE;
3942e126ba97SEli Cohen 	dev->ib_dev.node_type		= RDMA_NODE_IB_CA;
3943c6790aa9SSagi Grimberg 	dev->ib_dev.local_dma_lkey	= 0 /* not supported for now */;
3944938fe83cSSaeed Mahameed 	dev->num_ports		= MLX5_CAP_GEN(mdev, num_ports);
3945e126ba97SEli Cohen 	dev->ib_dev.phys_port_cnt     = dev->num_ports;
3946233d05d2SSaeed Mahameed 	dev->ib_dev.num_comp_vectors    =
3947233d05d2SSaeed Mahameed 		dev->mdev->priv.eq_table.num_comp_vectors;
39489b0c289eSBart Van Assche 	dev->ib_dev.dev.parent		= &mdev->pdev->dev;
3949e126ba97SEli Cohen 
3950e126ba97SEli Cohen 	dev->ib_dev.uverbs_abi_ver	= MLX5_IB_UVERBS_ABI_VERSION;
3951e126ba97SEli Cohen 	dev->ib_dev.uverbs_cmd_mask	=
3952e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_GET_CONTEXT)		|
3953e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_DEVICE)	|
3954e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_PORT)		|
3955e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_ALLOC_PD)		|
3956e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DEALLOC_PD)		|
395741c450fdSMoni Shoua 		(1ull << IB_USER_VERBS_CMD_CREATE_AH)		|
395841c450fdSMoni Shoua 		(1ull << IB_USER_VERBS_CMD_DESTROY_AH)		|
3959e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_REG_MR)		|
396056e11d62SNoa Osherovich 		(1ull << IB_USER_VERBS_CMD_REREG_MR)		|
3961e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DEREG_MR)		|
3962e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL)	|
3963e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_CQ)		|
3964e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_RESIZE_CQ)		|
3965e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_CQ)		|
3966e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_QP)		|
3967e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_MODIFY_QP)		|
3968e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_QP)		|
3969e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_QP)		|
3970e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_ATTACH_MCAST)	|
3971e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DETACH_MCAST)	|
3972e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_SRQ)		|
3973e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_MODIFY_SRQ)		|
3974e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_SRQ)		|
3975e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_SRQ)		|
3976e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_XSRQ)		|
3977e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_OPEN_QP);
39781707cb4aSHaggai Eran 	dev->ib_dev.uverbs_ex_cmd_mask =
3979d4584ddfSMatan Barak 		(1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE)	|
3980d4584ddfSMatan Barak 		(1ull << IB_USER_VERBS_EX_CMD_CREATE_CQ)	|
39817d29f349SBodong Wang 		(1ull << IB_USER_VERBS_EX_CMD_CREATE_QP)	|
39827d29f349SBodong Wang 		(1ull << IB_USER_VERBS_EX_CMD_MODIFY_QP);
3983e126ba97SEli Cohen 
3984e126ba97SEli Cohen 	dev->ib_dev.query_device	= mlx5_ib_query_device;
3985e126ba97SEli Cohen 	dev->ib_dev.query_port		= mlx5_ib_query_port;
3986ebd61f68SAchiad Shochat 	dev->ib_dev.get_link_layer	= mlx5_ib_port_link_layer;
3987fc24fc5eSAchiad Shochat 	if (ll == IB_LINK_LAYER_ETHERNET)
3988fc24fc5eSAchiad Shochat 		dev->ib_dev.get_netdev	= mlx5_ib_get_netdev;
3989e126ba97SEli Cohen 	dev->ib_dev.query_gid		= mlx5_ib_query_gid;
39903cca2606SAchiad Shochat 	dev->ib_dev.add_gid		= mlx5_ib_add_gid;
39913cca2606SAchiad Shochat 	dev->ib_dev.del_gid		= mlx5_ib_del_gid;
3992e126ba97SEli Cohen 	dev->ib_dev.query_pkey		= mlx5_ib_query_pkey;
3993e126ba97SEli Cohen 	dev->ib_dev.modify_device	= mlx5_ib_modify_device;
3994e126ba97SEli Cohen 	dev->ib_dev.modify_port		= mlx5_ib_modify_port;
3995e126ba97SEli Cohen 	dev->ib_dev.alloc_ucontext	= mlx5_ib_alloc_ucontext;
3996e126ba97SEli Cohen 	dev->ib_dev.dealloc_ucontext	= mlx5_ib_dealloc_ucontext;
3997e126ba97SEli Cohen 	dev->ib_dev.mmap		= mlx5_ib_mmap;
3998e126ba97SEli Cohen 	dev->ib_dev.alloc_pd		= mlx5_ib_alloc_pd;
3999e126ba97SEli Cohen 	dev->ib_dev.dealloc_pd		= mlx5_ib_dealloc_pd;
4000e126ba97SEli Cohen 	dev->ib_dev.create_ah		= mlx5_ib_create_ah;
4001e126ba97SEli Cohen 	dev->ib_dev.query_ah		= mlx5_ib_query_ah;
4002e126ba97SEli Cohen 	dev->ib_dev.destroy_ah		= mlx5_ib_destroy_ah;
4003e126ba97SEli Cohen 	dev->ib_dev.create_srq		= mlx5_ib_create_srq;
4004e126ba97SEli Cohen 	dev->ib_dev.modify_srq		= mlx5_ib_modify_srq;
4005e126ba97SEli Cohen 	dev->ib_dev.query_srq		= mlx5_ib_query_srq;
4006e126ba97SEli Cohen 	dev->ib_dev.destroy_srq		= mlx5_ib_destroy_srq;
4007e126ba97SEli Cohen 	dev->ib_dev.post_srq_recv	= mlx5_ib_post_srq_recv;
4008e126ba97SEli Cohen 	dev->ib_dev.create_qp		= mlx5_ib_create_qp;
4009e126ba97SEli Cohen 	dev->ib_dev.modify_qp		= mlx5_ib_modify_qp;
4010e126ba97SEli Cohen 	dev->ib_dev.query_qp		= mlx5_ib_query_qp;
4011e126ba97SEli Cohen 	dev->ib_dev.destroy_qp		= mlx5_ib_destroy_qp;
4012e126ba97SEli Cohen 	dev->ib_dev.post_send		= mlx5_ib_post_send;
4013e126ba97SEli Cohen 	dev->ib_dev.post_recv		= mlx5_ib_post_recv;
4014e126ba97SEli Cohen 	dev->ib_dev.create_cq		= mlx5_ib_create_cq;
4015e126ba97SEli Cohen 	dev->ib_dev.modify_cq		= mlx5_ib_modify_cq;
4016e126ba97SEli Cohen 	dev->ib_dev.resize_cq		= mlx5_ib_resize_cq;
4017e126ba97SEli Cohen 	dev->ib_dev.destroy_cq		= mlx5_ib_destroy_cq;
4018e126ba97SEli Cohen 	dev->ib_dev.poll_cq		= mlx5_ib_poll_cq;
4019e126ba97SEli Cohen 	dev->ib_dev.req_notify_cq	= mlx5_ib_arm_cq;
4020e126ba97SEli Cohen 	dev->ib_dev.get_dma_mr		= mlx5_ib_get_dma_mr;
4021e126ba97SEli Cohen 	dev->ib_dev.reg_user_mr		= mlx5_ib_reg_user_mr;
402256e11d62SNoa Osherovich 	dev->ib_dev.rereg_user_mr	= mlx5_ib_rereg_user_mr;
4023e126ba97SEli Cohen 	dev->ib_dev.dereg_mr		= mlx5_ib_dereg_mr;
4024e126ba97SEli Cohen 	dev->ib_dev.attach_mcast	= mlx5_ib_mcg_attach;
4025e126ba97SEli Cohen 	dev->ib_dev.detach_mcast	= mlx5_ib_mcg_detach;
4026e126ba97SEli Cohen 	dev->ib_dev.process_mad		= mlx5_ib_process_mad;
40279bee178bSSagi Grimberg 	dev->ib_dev.alloc_mr		= mlx5_ib_alloc_mr;
40288a187ee5SSagi Grimberg 	dev->ib_dev.map_mr_sg		= mlx5_ib_map_mr_sg;
4029d5436ba0SSagi Grimberg 	dev->ib_dev.check_mr_status	= mlx5_ib_check_mr_status;
40307738613eSIra Weiny 	dev->ib_dev.get_port_immutable  = mlx5_port_immutable;
4031c7342823SIra Weiny 	dev->ib_dev.get_dev_fw_str      = get_dev_fw_str;
403240b24403SSagi Grimberg 	dev->ib_dev.get_vector_affinity	= mlx5_ib_get_vector_affinity;
40338e959601SNiranjana Vishwanathapura 	if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads))
4034693dfd5aSErez Shitrit 		dev->ib_dev.alloc_rdma_netdev	= mlx5_ib_alloc_rdma_netdev;
40358e959601SNiranjana Vishwanathapura 
4036eff901d3SEli Cohen 	if (mlx5_core_is_pf(mdev)) {
4037eff901d3SEli Cohen 		dev->ib_dev.get_vf_config	= mlx5_ib_get_vf_config;
4038eff901d3SEli Cohen 		dev->ib_dev.set_vf_link_state	= mlx5_ib_set_vf_link_state;
4039eff901d3SEli Cohen 		dev->ib_dev.get_vf_stats	= mlx5_ib_get_vf_stats;
4040eff901d3SEli Cohen 		dev->ib_dev.set_vf_guid		= mlx5_ib_set_vf_guid;
4041eff901d3SEli Cohen 	}
4042e126ba97SEli Cohen 
40437c2344c3SMaor Gottlieb 	dev->ib_dev.disassociate_ucontext = mlx5_ib_disassociate_ucontext;
40447c2344c3SMaor Gottlieb 
4045938fe83cSSaeed Mahameed 	mlx5_ib_internal_fill_odp_caps(dev);
40468cdd312cSHaggai Eran 
40476e8484c5SMax Gurtovoy 	dev->umr_fence = mlx5_get_umr_fence(MLX5_CAP_GEN(mdev, umr_fence));
40486e8484c5SMax Gurtovoy 
4049d2370e0aSMatan Barak 	if (MLX5_CAP_GEN(mdev, imaicl)) {
4050d2370e0aSMatan Barak 		dev->ib_dev.alloc_mw		= mlx5_ib_alloc_mw;
4051d2370e0aSMatan Barak 		dev->ib_dev.dealloc_mw		= mlx5_ib_dealloc_mw;
4052d2370e0aSMatan Barak 		dev->ib_dev.uverbs_cmd_mask |=
4053d2370e0aSMatan Barak 			(1ull << IB_USER_VERBS_CMD_ALLOC_MW)	|
4054d2370e0aSMatan Barak 			(1ull << IB_USER_VERBS_CMD_DEALLOC_MW);
4055d2370e0aSMatan Barak 	}
4056d2370e0aSMatan Barak 
40577c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt)) {
40580ad17a8fSMark Bloch 		dev->ib_dev.get_hw_stats	= mlx5_ib_get_hw_stats;
40590ad17a8fSMark Bloch 		dev->ib_dev.alloc_hw_stats	= mlx5_ib_alloc_hw_stats;
40600ad17a8fSMark Bloch 	}
40610ad17a8fSMark Bloch 
4062938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, xrc)) {
4063e126ba97SEli Cohen 		dev->ib_dev.alloc_xrcd = mlx5_ib_alloc_xrcd;
4064e126ba97SEli Cohen 		dev->ib_dev.dealloc_xrcd = mlx5_ib_dealloc_xrcd;
4065e126ba97SEli Cohen 		dev->ib_dev.uverbs_cmd_mask |=
4066e126ba97SEli Cohen 			(1ull << IB_USER_VERBS_CMD_OPEN_XRCD) |
4067e126ba97SEli Cohen 			(1ull << IB_USER_VERBS_CMD_CLOSE_XRCD);
4068e126ba97SEli Cohen 	}
4069e126ba97SEli Cohen 
4070038d2ef8SMaor Gottlieb 	dev->ib_dev.create_flow	= mlx5_ib_create_flow;
4071038d2ef8SMaor Gottlieb 	dev->ib_dev.destroy_flow = mlx5_ib_destroy_flow;
407281e30880SYishai Hadas 	dev->ib_dev.uverbs_ex_cmd_mask |=
407381e30880SYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_CREATE_FLOW) |
407481e30880SYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_DESTROY_FLOW);
407581e30880SYishai Hadas 
407681e30880SYishai Hadas 	if (mlx5_ib_port_link_layer(&dev->ib_dev, 1) ==
407781e30880SYishai Hadas 	    IB_LINK_LAYER_ETHERNET) {
407879b20a6cSYishai Hadas 		dev->ib_dev.create_wq	 = mlx5_ib_create_wq;
407979b20a6cSYishai Hadas 		dev->ib_dev.modify_wq	 = mlx5_ib_modify_wq;
408079b20a6cSYishai Hadas 		dev->ib_dev.destroy_wq	 = mlx5_ib_destroy_wq;
4081c5f90929SYishai Hadas 		dev->ib_dev.create_rwq_ind_table = mlx5_ib_create_rwq_ind_table;
4082c5f90929SYishai Hadas 		dev->ib_dev.destroy_rwq_ind_table = mlx5_ib_destroy_rwq_ind_table;
4083038d2ef8SMaor Gottlieb 		dev->ib_dev.uverbs_ex_cmd_mask |=
408479b20a6cSYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_CREATE_WQ) |
408579b20a6cSYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_MODIFY_WQ) |
4086c5f90929SYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_DESTROY_WQ) |
4087c5f90929SYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_CREATE_RWQ_IND_TBL) |
4088c5f90929SYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_DESTROY_RWQ_IND_TBL);
4089038d2ef8SMaor Gottlieb 	}
4090e126ba97SEli Cohen 	err = init_node_data(dev);
4091e126ba97SEli Cohen 	if (err)
409290be7c8aSMajd Dibbiny 		goto err_free_port;
4093e126ba97SEli Cohen 
4094038d2ef8SMaor Gottlieb 	mutex_init(&dev->flow_db.lock);
4095e126ba97SEli Cohen 	mutex_init(&dev->cap_mask_mutex);
409689ea94a7SMaor Gottlieb 	INIT_LIST_HEAD(&dev->qp_list);
409789ea94a7SMaor Gottlieb 	spin_lock_init(&dev->reset_flow_resource_lock);
4098e126ba97SEli Cohen 
4099fc24fc5eSAchiad Shochat 	if (ll == IB_LINK_LAYER_ETHERNET) {
410045f95acdSOr Gerlitz 		err = mlx5_enable_eth(dev);
4101e126ba97SEli Cohen 		if (err)
410290be7c8aSMajd Dibbiny 			goto err_free_port;
4103fd65f1b8SMoni Shoua 		dev->roce.last_port_state = IB_PORT_DOWN;
4104fc24fc5eSAchiad Shochat 	}
4105fc24fc5eSAchiad Shochat 
4106fc24fc5eSAchiad Shochat 	err = create_dev_resources(&dev->devr);
4107fc24fc5eSAchiad Shochat 	if (err)
410845f95acdSOr Gerlitz 		goto err_disable_eth;
4109e126ba97SEli Cohen 
41106aec21f6SHaggai Eran 	err = mlx5_ib_odp_init_one(dev);
4111281d1a92SWei Yongjun 	if (err)
4112e126ba97SEli Cohen 		goto err_rsrc;
4113e126ba97SEli Cohen 
411445bded2cSKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt)) {
4115e1f24a79SParav Pandit 		err = mlx5_ib_alloc_counters(dev);
41166aec21f6SHaggai Eran 		if (err)
41176aec21f6SHaggai Eran 			goto err_odp;
411845bded2cSKamal Heib 	}
41196aec21f6SHaggai Eran 
41204a2da0b8SParav Pandit 	err = mlx5_ib_init_cong_debugfs(dev);
41214a2da0b8SParav Pandit 	if (err)
41224a2da0b8SParav Pandit 		goto err_cnt;
41234a2da0b8SParav Pandit 
41245fe9dec0SEli Cohen 	dev->mdev->priv.uar = mlx5_get_uars_page(dev->mdev);
41255fe9dec0SEli Cohen 	if (!dev->mdev->priv.uar)
41264a2da0b8SParav Pandit 		goto err_cong;
41275fe9dec0SEli Cohen 
41285fe9dec0SEli Cohen 	err = mlx5_alloc_bfreg(dev->mdev, &dev->bfreg, false, false);
41295fe9dec0SEli Cohen 	if (err)
41305fe9dec0SEli Cohen 		goto err_uar_page;
41315fe9dec0SEli Cohen 
41325fe9dec0SEli Cohen 	err = mlx5_alloc_bfreg(dev->mdev, &dev->fp_bfreg, false, true);
41335fe9dec0SEli Cohen 	if (err)
41345fe9dec0SEli Cohen 		goto err_bfreg;
41355fe9dec0SEli Cohen 
41360837e86aSMark Bloch 	err = ib_register_device(&dev->ib_dev, NULL);
41370837e86aSMark Bloch 	if (err)
41385fe9dec0SEli Cohen 		goto err_fp_bfreg;
41390837e86aSMark Bloch 
4140e126ba97SEli Cohen 	err = create_umr_res(dev);
4141e126ba97SEli Cohen 	if (err)
4142e126ba97SEli Cohen 		goto err_dev;
4143e126ba97SEli Cohen 
414403404e8aSMaor Gottlieb 	init_delay_drop(dev);
414503404e8aSMaor Gottlieb 
4146e126ba97SEli Cohen 	for (i = 0; i < ARRAY_SIZE(mlx5_class_attributes); i++) {
4147281d1a92SWei Yongjun 		err = device_create_file(&dev->ib_dev.dev,
4148281d1a92SWei Yongjun 					 mlx5_class_attributes[i]);
4149281d1a92SWei Yongjun 		if (err)
415003404e8aSMaor Gottlieb 			goto err_delay_drop;
4151e126ba97SEli Cohen 	}
4152e126ba97SEli Cohen 
4153c85023e1SHuy Nguyen 	if ((MLX5_CAP_GEN(mdev, port_type) == MLX5_CAP_PORT_TYPE_ETH) &&
4154c85023e1SHuy Nguyen 	    MLX5_CAP_GEN(mdev, disable_local_lb))
4155c85023e1SHuy Nguyen 		mutex_init(&dev->lb_mutex);
4156c85023e1SHuy Nguyen 
4157e126ba97SEli Cohen 	dev->ib_active = true;
4158e126ba97SEli Cohen 
41599603b61dSJack Morgenstein 	return dev;
4160e126ba97SEli Cohen 
416103404e8aSMaor Gottlieb err_delay_drop:
416203404e8aSMaor Gottlieb 	cancel_delay_drop(dev);
4163e126ba97SEli Cohen 	destroy_umrc_res(dev);
4164e126ba97SEli Cohen 
4165e126ba97SEli Cohen err_dev:
4166e126ba97SEli Cohen 	ib_unregister_device(&dev->ib_dev);
4167e126ba97SEli Cohen 
41685fe9dec0SEli Cohen err_fp_bfreg:
41695fe9dec0SEli Cohen 	mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
41705fe9dec0SEli Cohen 
41715fe9dec0SEli Cohen err_bfreg:
41725fe9dec0SEli Cohen 	mlx5_free_bfreg(dev->mdev, &dev->bfreg);
41735fe9dec0SEli Cohen 
41745fe9dec0SEli Cohen err_uar_page:
41755fe9dec0SEli Cohen 	mlx5_put_uars_page(dev->mdev, dev->mdev->priv.uar);
41765fe9dec0SEli Cohen 
4177e1f24a79SParav Pandit err_cnt:
41784a2da0b8SParav Pandit 	mlx5_ib_cleanup_cong_debugfs(dev);
41794a2da0b8SParav Pandit err_cong:
418045bded2cSKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt))
4181e1f24a79SParav Pandit 		mlx5_ib_dealloc_counters(dev);
41820837e86aSMark Bloch 
41836aec21f6SHaggai Eran err_odp:
41846aec21f6SHaggai Eran 	mlx5_ib_odp_remove_one(dev);
41856aec21f6SHaggai Eran 
4186e126ba97SEli Cohen err_rsrc:
4187e126ba97SEli Cohen 	destroy_dev_resources(&dev->devr);
4188e126ba97SEli Cohen 
418945f95acdSOr Gerlitz err_disable_eth:
41905ec8c83eSAviv Heller 	if (ll == IB_LINK_LAYER_ETHERNET) {
419145f95acdSOr Gerlitz 		mlx5_disable_eth(dev);
4192d012f5d6SOr Gerlitz 		mlx5_remove_netdev_notifier(dev);
41935ec8c83eSAviv Heller 	}
4194fc24fc5eSAchiad Shochat 
41950837e86aSMark Bloch err_free_port:
41960837e86aSMark Bloch 	kfree(dev->port);
41970837e86aSMark Bloch 
41989603b61dSJack Morgenstein err_dealloc:
4199e126ba97SEli Cohen 	ib_dealloc_device((struct ib_device *)dev);
4200e126ba97SEli Cohen 
42019603b61dSJack Morgenstein 	return NULL;
4202e126ba97SEli Cohen }
4203e126ba97SEli Cohen 
42049603b61dSJack Morgenstein static void mlx5_ib_remove(struct mlx5_core_dev *mdev, void *context)
4205e126ba97SEli Cohen {
42069603b61dSJack Morgenstein 	struct mlx5_ib_dev *dev = context;
4207fc24fc5eSAchiad Shochat 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev, 1);
42086aec21f6SHaggai Eran 
420903404e8aSMaor Gottlieb 	cancel_delay_drop(dev);
4210d012f5d6SOr Gerlitz 	mlx5_remove_netdev_notifier(dev);
4211e126ba97SEli Cohen 	ib_unregister_device(&dev->ib_dev);
42125fe9dec0SEli Cohen 	mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
42135fe9dec0SEli Cohen 	mlx5_free_bfreg(dev->mdev, &dev->bfreg);
42145fe9dec0SEli Cohen 	mlx5_put_uars_page(dev->mdev, mdev->priv.uar);
42154a2da0b8SParav Pandit 	mlx5_ib_cleanup_cong_debugfs(dev);
421645bded2cSKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt))
4217e1f24a79SParav Pandit 		mlx5_ib_dealloc_counters(dev);
4218eefd56e5SEli Cohen 	destroy_umrc_res(dev);
42196aec21f6SHaggai Eran 	mlx5_ib_odp_remove_one(dev);
4220e126ba97SEli Cohen 	destroy_dev_resources(&dev->devr);
4221fc24fc5eSAchiad Shochat 	if (ll == IB_LINK_LAYER_ETHERNET)
422245f95acdSOr Gerlitz 		mlx5_disable_eth(dev);
42230837e86aSMark Bloch 	kfree(dev->port);
4224e126ba97SEli Cohen 	ib_dealloc_device(&dev->ib_dev);
4225e126ba97SEli Cohen }
4226e126ba97SEli Cohen 
42279603b61dSJack Morgenstein static struct mlx5_interface mlx5_ib_interface = {
42289603b61dSJack Morgenstein 	.add            = mlx5_ib_add,
42299603b61dSJack Morgenstein 	.remove         = mlx5_ib_remove,
42309603b61dSJack Morgenstein 	.event          = mlx5_ib_event,
4231d9aaed83SArtemy Kovalyov #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
4232d9aaed83SArtemy Kovalyov 	.pfault		= mlx5_ib_pfault,
4233d9aaed83SArtemy Kovalyov #endif
423464613d94SSaeed Mahameed 	.protocol	= MLX5_INTERFACE_PROTOCOL_IB,
4235e126ba97SEli Cohen };
4236e126ba97SEli Cohen 
4237e126ba97SEli Cohen static int __init mlx5_ib_init(void)
4238e126ba97SEli Cohen {
42396aec21f6SHaggai Eran 	int err;
42406aec21f6SHaggai Eran 
424181713d37SArtemy Kovalyov 	mlx5_ib_odp_init();
42429603b61dSJack Morgenstein 
42436aec21f6SHaggai Eran 	err = mlx5_register_interface(&mlx5_ib_interface);
42446aec21f6SHaggai Eran 
42456aec21f6SHaggai Eran 	return err;
4246e126ba97SEli Cohen }
4247e126ba97SEli Cohen 
4248e126ba97SEli Cohen static void __exit mlx5_ib_cleanup(void)
4249e126ba97SEli Cohen {
42509603b61dSJack Morgenstein 	mlx5_unregister_interface(&mlx5_ib_interface);
4251e126ba97SEli Cohen }
4252e126ba97SEli Cohen 
4253e126ba97SEli Cohen module_init(mlx5_ib_init);
4254e126ba97SEli Cohen module_exit(mlx5_ib_cleanup);
4255