xref: /openbmc/linux/drivers/infiniband/hw/mlx5/main.c (revision 095b0927f0ce74f7211b9046f8493dbba26ca930)
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 
33adec640eSChristoph Hellwig #include <linux/highmem.h>
34e126ba97SEli Cohen #include <linux/module.h>
35e126ba97SEli Cohen #include <linux/init.h>
36e126ba97SEli Cohen #include <linux/errno.h>
37e126ba97SEli Cohen #include <linux/pci.h>
38e126ba97SEli Cohen #include <linux/dma-mapping.h>
39e126ba97SEli Cohen #include <linux/slab.h>
4037aa5c36SGuy Levi #if defined(CONFIG_X86)
4137aa5c36SGuy Levi #include <asm/pat.h>
4237aa5c36SGuy Levi #endif
43e126ba97SEli Cohen #include <linux/sched.h>
446e84f315SIngo Molnar #include <linux/sched/mm.h>
450881e7bdSIngo Molnar #include <linux/sched/task.h>
467c2344c3SMaor Gottlieb #include <linux/delay.h>
47e126ba97SEli Cohen #include <rdma/ib_user_verbs.h>
483f89a643SAchiad Shochat #include <rdma/ib_addr.h>
492811ba51SAchiad Shochat #include <rdma/ib_cache.h>
50ada68c31SAchiad Shochat #include <linux/mlx5/port.h>
511b5daf11SMajd Dibbiny #include <linux/mlx5/vport.h>
527c2344c3SMaor Gottlieb #include <linux/list.h>
53e126ba97SEli Cohen #include <rdma/ib_smi.h>
54e126ba97SEli Cohen #include <rdma/ib_umem.h>
55038d2ef8SMaor Gottlieb #include <linux/in.h>
56038d2ef8SMaor Gottlieb #include <linux/etherdevice.h>
57038d2ef8SMaor Gottlieb #include <linux/mlx5/fs.h>
5878984898SOr Gerlitz #include <linux/mlx5/vport.h>
59e126ba97SEli Cohen #include "mlx5_ib.h"
60e1f24a79SParav Pandit #include "cmd.h"
61e126ba97SEli Cohen 
62e126ba97SEli Cohen #define DRIVER_NAME "mlx5_ib"
63b359911dSTariq Toukan #define DRIVER_VERSION "5.0-0"
64e126ba97SEli Cohen 
65e126ba97SEli Cohen MODULE_AUTHOR("Eli Cohen <eli@mellanox.com>");
66e126ba97SEli Cohen MODULE_DESCRIPTION("Mellanox Connect-IB HCA IB driver");
67e126ba97SEli Cohen MODULE_LICENSE("Dual BSD/GPL");
68e126ba97SEli Cohen MODULE_VERSION(DRIVER_VERSION);
69e126ba97SEli Cohen 
70e126ba97SEli Cohen static char mlx5_version[] =
71e126ba97SEli Cohen 	DRIVER_NAME ": Mellanox Connect-IB Infiniband driver v"
72b359911dSTariq Toukan 	DRIVER_VERSION "\n";
73e126ba97SEli Cohen 
74da7525d2SEran Ben Elisha enum {
75da7525d2SEran Ben Elisha 	MLX5_ATOMIC_SIZE_QP_8BYTES = 1 << 3,
76da7525d2SEran Ben Elisha };
771b5daf11SMajd Dibbiny 
781b5daf11SMajd Dibbiny static enum rdma_link_layer
79ebd61f68SAchiad Shochat mlx5_port_type_cap_to_rdma_ll(int port_type_cap)
801b5daf11SMajd Dibbiny {
81ebd61f68SAchiad Shochat 	switch (port_type_cap) {
821b5daf11SMajd Dibbiny 	case MLX5_CAP_PORT_TYPE_IB:
831b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_INFINIBAND;
841b5daf11SMajd Dibbiny 	case MLX5_CAP_PORT_TYPE_ETH:
851b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_ETHERNET;
861b5daf11SMajd Dibbiny 	default:
871b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_UNSPECIFIED;
881b5daf11SMajd Dibbiny 	}
891b5daf11SMajd Dibbiny }
901b5daf11SMajd Dibbiny 
91ebd61f68SAchiad Shochat static enum rdma_link_layer
92ebd61f68SAchiad Shochat mlx5_ib_port_link_layer(struct ib_device *device, u8 port_num)
93ebd61f68SAchiad Shochat {
94ebd61f68SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(device);
95ebd61f68SAchiad Shochat 	int port_type_cap = MLX5_CAP_GEN(dev->mdev, port_type);
96ebd61f68SAchiad Shochat 
97ebd61f68SAchiad Shochat 	return mlx5_port_type_cap_to_rdma_ll(port_type_cap);
98ebd61f68SAchiad Shochat }
99ebd61f68SAchiad Shochat 
100fc24fc5eSAchiad Shochat static int mlx5_netdev_event(struct notifier_block *this,
101fc24fc5eSAchiad Shochat 			     unsigned long event, void *ptr)
102fc24fc5eSAchiad Shochat {
103fc24fc5eSAchiad Shochat 	struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
104fc24fc5eSAchiad Shochat 	struct mlx5_ib_dev *ibdev = container_of(this, struct mlx5_ib_dev,
105fc24fc5eSAchiad Shochat 						 roce.nb);
106fc24fc5eSAchiad Shochat 
1075ec8c83eSAviv Heller 	switch (event) {
1085ec8c83eSAviv Heller 	case NETDEV_REGISTER:
1095ec8c83eSAviv Heller 	case NETDEV_UNREGISTER:
110fc24fc5eSAchiad Shochat 		write_lock(&ibdev->roce.netdev_lock);
111fc24fc5eSAchiad Shochat 		if (ndev->dev.parent == &ibdev->mdev->pdev->dev)
1125ec8c83eSAviv Heller 			ibdev->roce.netdev = (event == NETDEV_UNREGISTER) ?
1135ec8c83eSAviv Heller 					     NULL : ndev;
114fc24fc5eSAchiad Shochat 		write_unlock(&ibdev->roce.netdev_lock);
1155ec8c83eSAviv Heller 		break;
1165ec8c83eSAviv Heller 
1175ec8c83eSAviv Heller 	case NETDEV_UP:
11888621dfeSAviv Heller 	case NETDEV_DOWN: {
11988621dfeSAviv Heller 		struct net_device *lag_ndev = mlx5_lag_get_roce_netdev(ibdev->mdev);
12088621dfeSAviv Heller 		struct net_device *upper = NULL;
12188621dfeSAviv Heller 
12288621dfeSAviv Heller 		if (lag_ndev) {
12388621dfeSAviv Heller 			upper = netdev_master_upper_dev_get(lag_ndev);
12488621dfeSAviv Heller 			dev_put(lag_ndev);
12588621dfeSAviv Heller 		}
12688621dfeSAviv Heller 
12788621dfeSAviv Heller 		if ((upper == ndev || (!upper && ndev == ibdev->roce.netdev))
12888621dfeSAviv Heller 		    && ibdev->ib_active) {
129626bc02dSBart Van Assche 			struct ib_event ibev = { };
1305ec8c83eSAviv Heller 
1315ec8c83eSAviv Heller 			ibev.device = &ibdev->ib_dev;
1325ec8c83eSAviv Heller 			ibev.event = (event == NETDEV_UP) ?
1335ec8c83eSAviv Heller 				     IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
1345ec8c83eSAviv Heller 			ibev.element.port_num = 1;
1355ec8c83eSAviv Heller 			ib_dispatch_event(&ibev);
1365ec8c83eSAviv Heller 		}
1375ec8c83eSAviv Heller 		break;
13888621dfeSAviv Heller 	}
1395ec8c83eSAviv Heller 
1405ec8c83eSAviv Heller 	default:
1415ec8c83eSAviv Heller 		break;
1425ec8c83eSAviv Heller 	}
143fc24fc5eSAchiad Shochat 
144fc24fc5eSAchiad Shochat 	return NOTIFY_DONE;
145fc24fc5eSAchiad Shochat }
146fc24fc5eSAchiad Shochat 
147fc24fc5eSAchiad Shochat static struct net_device *mlx5_ib_get_netdev(struct ib_device *device,
148fc24fc5eSAchiad Shochat 					     u8 port_num)
149fc24fc5eSAchiad Shochat {
150fc24fc5eSAchiad Shochat 	struct mlx5_ib_dev *ibdev = to_mdev(device);
151fc24fc5eSAchiad Shochat 	struct net_device *ndev;
152fc24fc5eSAchiad Shochat 
15388621dfeSAviv Heller 	ndev = mlx5_lag_get_roce_netdev(ibdev->mdev);
15488621dfeSAviv Heller 	if (ndev)
15588621dfeSAviv Heller 		return ndev;
15688621dfeSAviv Heller 
157fc24fc5eSAchiad Shochat 	/* Ensure ndev does not disappear before we invoke dev_hold()
158fc24fc5eSAchiad Shochat 	 */
159fc24fc5eSAchiad Shochat 	read_lock(&ibdev->roce.netdev_lock);
160fc24fc5eSAchiad Shochat 	ndev = ibdev->roce.netdev;
161fc24fc5eSAchiad Shochat 	if (ndev)
162fc24fc5eSAchiad Shochat 		dev_hold(ndev);
163fc24fc5eSAchiad Shochat 	read_unlock(&ibdev->roce.netdev_lock);
164fc24fc5eSAchiad Shochat 
165fc24fc5eSAchiad Shochat 	return ndev;
166fc24fc5eSAchiad Shochat }
167fc24fc5eSAchiad Shochat 
168f1b65df5SNoa Osherovich static int translate_eth_proto_oper(u32 eth_proto_oper, u8 *active_speed,
169f1b65df5SNoa Osherovich 				    u8 *active_width)
170f1b65df5SNoa Osherovich {
171f1b65df5SNoa Osherovich 	switch (eth_proto_oper) {
172f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_1000BASE_CX_SGMII):
173f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_1000BASE_KX):
174f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100BASE_TX):
175f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_1000BASE_T):
176f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
177f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_SDR;
178f1b65df5SNoa Osherovich 		break;
179f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_T):
180f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_CX4):
181f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_KX4):
182f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_KR):
183f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_CR):
184f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_SR):
185f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_ER):
186f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
187f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_QDR;
188f1b65df5SNoa Osherovich 		break;
189f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_25GBASE_CR):
190f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_25GBASE_KR):
191f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_25GBASE_SR):
192f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
193f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_EDR;
194f1b65df5SNoa Osherovich 		break;
195f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_CR4):
196f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_KR4):
197f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_SR4):
198f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_LR4):
199f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_4X;
200f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_QDR;
201f1b65df5SNoa Osherovich 		break;
202f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_50GBASE_CR2):
203f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_50GBASE_KR2):
204f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_50GBASE_SR2):
205f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
206f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_HDR;
207f1b65df5SNoa Osherovich 		break;
208f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_56GBASE_R4):
209f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_4X;
210f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_FDR;
211f1b65df5SNoa Osherovich 		break;
212f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_CR4):
213f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_SR4):
214f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_KR4):
215f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_LR4):
216f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_4X;
217f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_EDR;
218f1b65df5SNoa Osherovich 		break;
219f1b65df5SNoa Osherovich 	default:
220f1b65df5SNoa Osherovich 		return -EINVAL;
221f1b65df5SNoa Osherovich 	}
222f1b65df5SNoa Osherovich 
223f1b65df5SNoa Osherovich 	return 0;
224f1b65df5SNoa Osherovich }
225f1b65df5SNoa Osherovich 
226*095b0927SIlan Tayari static int mlx5_query_port_roce(struct ib_device *device, u8 port_num,
2273f89a643SAchiad Shochat 				struct ib_port_attr *props)
2283f89a643SAchiad Shochat {
2293f89a643SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(device);
230f1b65df5SNoa Osherovich 	struct mlx5_core_dev *mdev = dev->mdev;
23188621dfeSAviv Heller 	struct net_device *ndev, *upper;
2323f89a643SAchiad Shochat 	enum ib_mtu ndev_ib_mtu;
233c876a1b7SLeon Romanovsky 	u16 qkey_viol_cntr;
234f1b65df5SNoa Osherovich 	u32 eth_prot_oper;
235*095b0927SIlan Tayari 	int err;
2363f89a643SAchiad Shochat 
237f1b65df5SNoa Osherovich 	/* Possible bad flows are checked before filling out props so in case
238f1b65df5SNoa Osherovich 	 * of an error it will still be zeroed out.
23950f22fd8SNoa Osherovich 	 */
240*095b0927SIlan Tayari 	err = mlx5_query_port_eth_proto_oper(mdev, &eth_prot_oper, port_num);
241*095b0927SIlan Tayari 	if (err)
242*095b0927SIlan Tayari 		return err;
243f1b65df5SNoa Osherovich 
244f1b65df5SNoa Osherovich 	translate_eth_proto_oper(eth_prot_oper, &props->active_speed,
245f1b65df5SNoa Osherovich 				 &props->active_width);
2463f89a643SAchiad Shochat 
2473f89a643SAchiad Shochat 	props->port_cap_flags  |= IB_PORT_CM_SUP;
2483f89a643SAchiad Shochat 	props->port_cap_flags  |= IB_PORT_IP_BASED_GIDS;
2493f89a643SAchiad Shochat 
2503f89a643SAchiad Shochat 	props->gid_tbl_len      = MLX5_CAP_ROCE(dev->mdev,
2513f89a643SAchiad Shochat 						roce_address_table_size);
2523f89a643SAchiad Shochat 	props->max_mtu          = IB_MTU_4096;
2533f89a643SAchiad Shochat 	props->max_msg_sz       = 1 << MLX5_CAP_GEN(dev->mdev, log_max_msg);
2543f89a643SAchiad Shochat 	props->pkey_tbl_len     = 1;
2553f89a643SAchiad Shochat 	props->state            = IB_PORT_DOWN;
2563f89a643SAchiad Shochat 	props->phys_state       = 3;
2573f89a643SAchiad Shochat 
258c876a1b7SLeon Romanovsky 	mlx5_query_nic_vport_qkey_viol_cntr(dev->mdev, &qkey_viol_cntr);
259c876a1b7SLeon Romanovsky 	props->qkey_viol_cntr = qkey_viol_cntr;
2603f89a643SAchiad Shochat 
2613f89a643SAchiad Shochat 	ndev = mlx5_ib_get_netdev(device, port_num);
2623f89a643SAchiad Shochat 	if (!ndev)
263*095b0927SIlan Tayari 		return 0;
2643f89a643SAchiad Shochat 
26588621dfeSAviv Heller 	if (mlx5_lag_is_active(dev->mdev)) {
26688621dfeSAviv Heller 		rcu_read_lock();
26788621dfeSAviv Heller 		upper = netdev_master_upper_dev_get_rcu(ndev);
26888621dfeSAviv Heller 		if (upper) {
26988621dfeSAviv Heller 			dev_put(ndev);
27088621dfeSAviv Heller 			ndev = upper;
27188621dfeSAviv Heller 			dev_hold(ndev);
27288621dfeSAviv Heller 		}
27388621dfeSAviv Heller 		rcu_read_unlock();
27488621dfeSAviv Heller 	}
27588621dfeSAviv Heller 
2763f89a643SAchiad Shochat 	if (netif_running(ndev) && netif_carrier_ok(ndev)) {
2773f89a643SAchiad Shochat 		props->state      = IB_PORT_ACTIVE;
2783f89a643SAchiad Shochat 		props->phys_state = 5;
2793f89a643SAchiad Shochat 	}
2803f89a643SAchiad Shochat 
2813f89a643SAchiad Shochat 	ndev_ib_mtu = iboe_get_mtu(ndev->mtu);
2823f89a643SAchiad Shochat 
2833f89a643SAchiad Shochat 	dev_put(ndev);
2843f89a643SAchiad Shochat 
2853f89a643SAchiad Shochat 	props->active_mtu	= min(props->max_mtu, ndev_ib_mtu);
286*095b0927SIlan Tayari 	return 0;
2873f89a643SAchiad Shochat }
2883f89a643SAchiad Shochat 
289*095b0927SIlan Tayari static int set_roce_addr(struct mlx5_ib_dev *dev, u8 port_num,
290*095b0927SIlan Tayari 			 unsigned int index, const union ib_gid *gid,
291*095b0927SIlan Tayari 			 const struct ib_gid_attr *attr)
2923cca2606SAchiad Shochat {
293*095b0927SIlan Tayari 	enum ib_gid_type gid_type = IB_GID_TYPE_IB;
294*095b0927SIlan Tayari 	u8 roce_version = 0;
295*095b0927SIlan Tayari 	u8 roce_l3_type = 0;
296*095b0927SIlan Tayari 	bool vlan = false;
297*095b0927SIlan Tayari 	u8 mac[ETH_ALEN];
298*095b0927SIlan Tayari 	u16 vlan_id = 0;
2993cca2606SAchiad Shochat 
300*095b0927SIlan Tayari 	if (gid) {
301*095b0927SIlan Tayari 		gid_type = attr->gid_type;
302*095b0927SIlan Tayari 		ether_addr_copy(mac, attr->ndev->dev_addr);
3033cca2606SAchiad Shochat 
3043cca2606SAchiad Shochat 		if (is_vlan_dev(attr->ndev)) {
305*095b0927SIlan Tayari 			vlan = true;
306*095b0927SIlan Tayari 			vlan_id = vlan_dev_vlan_id(attr->ndev);
307*095b0927SIlan Tayari 		}
3083cca2606SAchiad Shochat 	}
3093cca2606SAchiad Shochat 
310*095b0927SIlan Tayari 	switch (gid_type) {
3113cca2606SAchiad Shochat 	case IB_GID_TYPE_IB:
312*095b0927SIlan Tayari 		roce_version = MLX5_ROCE_VERSION_1;
3133cca2606SAchiad Shochat 		break;
3143cca2606SAchiad Shochat 	case IB_GID_TYPE_ROCE_UDP_ENCAP:
315*095b0927SIlan Tayari 		roce_version = MLX5_ROCE_VERSION_2;
316*095b0927SIlan Tayari 		if (ipv6_addr_v4mapped((void *)gid))
317*095b0927SIlan Tayari 			roce_l3_type = MLX5_ROCE_L3_TYPE_IPV4;
318*095b0927SIlan Tayari 		else
319*095b0927SIlan Tayari 			roce_l3_type = MLX5_ROCE_L3_TYPE_IPV6;
3203cca2606SAchiad Shochat 		break;
3213cca2606SAchiad Shochat 
3223cca2606SAchiad Shochat 	default:
323*095b0927SIlan Tayari 		mlx5_ib_warn(dev, "Unexpected GID type %u\n", gid_type);
3243cca2606SAchiad Shochat 	}
3253cca2606SAchiad Shochat 
326*095b0927SIlan Tayari 	return mlx5_core_roce_gid_set(dev->mdev, index, roce_version,
327*095b0927SIlan Tayari 				      roce_l3_type, gid->raw, mac, vlan,
328*095b0927SIlan Tayari 				      vlan_id);
3293cca2606SAchiad Shochat }
3303cca2606SAchiad Shochat 
3313cca2606SAchiad Shochat static int mlx5_ib_add_gid(struct ib_device *device, u8 port_num,
3323cca2606SAchiad Shochat 			   unsigned int index, const union ib_gid *gid,
3333cca2606SAchiad Shochat 			   const struct ib_gid_attr *attr,
3343cca2606SAchiad Shochat 			   __always_unused void **context)
3353cca2606SAchiad Shochat {
336*095b0927SIlan Tayari 	return set_roce_addr(to_mdev(device), port_num, index, gid, attr);
3373cca2606SAchiad Shochat }
3383cca2606SAchiad Shochat 
3393cca2606SAchiad Shochat static int mlx5_ib_del_gid(struct ib_device *device, u8 port_num,
3403cca2606SAchiad Shochat 			   unsigned int index, __always_unused void **context)
3413cca2606SAchiad Shochat {
342*095b0927SIlan Tayari 	return set_roce_addr(to_mdev(device), port_num, index, NULL, NULL);
3433cca2606SAchiad Shochat }
3443cca2606SAchiad Shochat 
3452811ba51SAchiad Shochat __be16 mlx5_get_roce_udp_sport(struct mlx5_ib_dev *dev, u8 port_num,
3462811ba51SAchiad Shochat 			       int index)
3472811ba51SAchiad Shochat {
3482811ba51SAchiad Shochat 	struct ib_gid_attr attr;
3492811ba51SAchiad Shochat 	union ib_gid gid;
3502811ba51SAchiad Shochat 
3512811ba51SAchiad Shochat 	if (ib_get_cached_gid(&dev->ib_dev, port_num, index, &gid, &attr))
3522811ba51SAchiad Shochat 		return 0;
3532811ba51SAchiad Shochat 
3542811ba51SAchiad Shochat 	if (!attr.ndev)
3552811ba51SAchiad Shochat 		return 0;
3562811ba51SAchiad Shochat 
3572811ba51SAchiad Shochat 	dev_put(attr.ndev);
3582811ba51SAchiad Shochat 
3592811ba51SAchiad Shochat 	if (attr.gid_type != IB_GID_TYPE_ROCE_UDP_ENCAP)
3602811ba51SAchiad Shochat 		return 0;
3612811ba51SAchiad Shochat 
3622811ba51SAchiad Shochat 	return cpu_to_be16(MLX5_CAP_ROCE(dev->mdev, r_roce_min_src_udp_port));
3632811ba51SAchiad Shochat }
3642811ba51SAchiad Shochat 
365ed88451eSMajd Dibbiny int mlx5_get_roce_gid_type(struct mlx5_ib_dev *dev, u8 port_num,
366ed88451eSMajd Dibbiny 			   int index, enum ib_gid_type *gid_type)
367ed88451eSMajd Dibbiny {
368ed88451eSMajd Dibbiny 	struct ib_gid_attr attr;
369ed88451eSMajd Dibbiny 	union ib_gid gid;
370ed88451eSMajd Dibbiny 	int ret;
371ed88451eSMajd Dibbiny 
372ed88451eSMajd Dibbiny 	ret = ib_get_cached_gid(&dev->ib_dev, port_num, index, &gid, &attr);
373ed88451eSMajd Dibbiny 	if (ret)
374ed88451eSMajd Dibbiny 		return ret;
375ed88451eSMajd Dibbiny 
376ed88451eSMajd Dibbiny 	if (!attr.ndev)
377ed88451eSMajd Dibbiny 		return -ENODEV;
378ed88451eSMajd Dibbiny 
379ed88451eSMajd Dibbiny 	dev_put(attr.ndev);
380ed88451eSMajd Dibbiny 
381ed88451eSMajd Dibbiny 	*gid_type = attr.gid_type;
382ed88451eSMajd Dibbiny 
383ed88451eSMajd Dibbiny 	return 0;
384ed88451eSMajd Dibbiny }
385ed88451eSMajd Dibbiny 
3861b5daf11SMajd Dibbiny static int mlx5_use_mad_ifc(struct mlx5_ib_dev *dev)
3871b5daf11SMajd Dibbiny {
3887fae6655SNoa Osherovich 	if (MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_IB)
389d603c809SEli Cohen 		return !MLX5_CAP_GEN(dev->mdev, ib_virt);
3907fae6655SNoa Osherovich 	return 0;
3911b5daf11SMajd Dibbiny }
3921b5daf11SMajd Dibbiny 
3931b5daf11SMajd Dibbiny enum {
3941b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_MAD,
3951b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_HCA,
3961b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_NIC,
3971b5daf11SMajd Dibbiny };
3981b5daf11SMajd Dibbiny 
3991b5daf11SMajd Dibbiny static int mlx5_get_vport_access_method(struct ib_device *ibdev)
4001b5daf11SMajd Dibbiny {
4011b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(to_mdev(ibdev)))
4021b5daf11SMajd Dibbiny 		return MLX5_VPORT_ACCESS_METHOD_MAD;
4031b5daf11SMajd Dibbiny 
404ebd61f68SAchiad Shochat 	if (mlx5_ib_port_link_layer(ibdev, 1) ==
4051b5daf11SMajd Dibbiny 	    IB_LINK_LAYER_ETHERNET)
4061b5daf11SMajd Dibbiny 		return MLX5_VPORT_ACCESS_METHOD_NIC;
4071b5daf11SMajd Dibbiny 
4081b5daf11SMajd Dibbiny 	return MLX5_VPORT_ACCESS_METHOD_HCA;
4091b5daf11SMajd Dibbiny }
4101b5daf11SMajd Dibbiny 
411da7525d2SEran Ben Elisha static void get_atomic_caps(struct mlx5_ib_dev *dev,
412da7525d2SEran Ben Elisha 			    struct ib_device_attr *props)
413da7525d2SEran Ben Elisha {
414da7525d2SEran Ben Elisha 	u8 tmp;
415da7525d2SEran Ben Elisha 	u8 atomic_operations = MLX5_CAP_ATOMIC(dev->mdev, atomic_operations);
416da7525d2SEran Ben Elisha 	u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_qp);
417da7525d2SEran Ben Elisha 	u8 atomic_req_8B_endianness_mode =
418bd10838aSOr Gerlitz 		MLX5_CAP_ATOMIC(dev->mdev, atomic_req_8B_endianness_mode);
419da7525d2SEran Ben Elisha 
420da7525d2SEran Ben Elisha 	/* Check if HW supports 8 bytes standard atomic operations and capable
421da7525d2SEran Ben Elisha 	 * of host endianness respond
422da7525d2SEran Ben Elisha 	 */
423da7525d2SEran Ben Elisha 	tmp = MLX5_ATOMIC_OPS_CMP_SWAP | MLX5_ATOMIC_OPS_FETCH_ADD;
424da7525d2SEran Ben Elisha 	if (((atomic_operations & tmp) == tmp) &&
425da7525d2SEran Ben Elisha 	    (atomic_size_qp & MLX5_ATOMIC_SIZE_QP_8BYTES) &&
426da7525d2SEran Ben Elisha 	    (atomic_req_8B_endianness_mode)) {
427da7525d2SEran Ben Elisha 		props->atomic_cap = IB_ATOMIC_HCA;
428da7525d2SEran Ben Elisha 	} else {
429da7525d2SEran Ben Elisha 		props->atomic_cap = IB_ATOMIC_NONE;
430da7525d2SEran Ben Elisha 	}
431da7525d2SEran Ben Elisha }
432da7525d2SEran Ben Elisha 
4331b5daf11SMajd Dibbiny static int mlx5_query_system_image_guid(struct ib_device *ibdev,
4341b5daf11SMajd Dibbiny 					__be64 *sys_image_guid)
4351b5daf11SMajd Dibbiny {
4361b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
4371b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
4381b5daf11SMajd Dibbiny 	u64 tmp;
4391b5daf11SMajd Dibbiny 	int err;
4401b5daf11SMajd Dibbiny 
4411b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
4421b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
4431b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_system_image_guid(ibdev,
4441b5daf11SMajd Dibbiny 							    sys_image_guid);
4451b5daf11SMajd Dibbiny 
4461b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
4471b5daf11SMajd Dibbiny 		err = mlx5_query_hca_vport_system_image_guid(mdev, &tmp);
4483f89a643SAchiad Shochat 		break;
4493f89a643SAchiad Shochat 
4503f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
4513f89a643SAchiad Shochat 		err = mlx5_query_nic_vport_system_image_guid(mdev, &tmp);
4523f89a643SAchiad Shochat 		break;
4531b5daf11SMajd Dibbiny 
4541b5daf11SMajd Dibbiny 	default:
4551b5daf11SMajd Dibbiny 		return -EINVAL;
4561b5daf11SMajd Dibbiny 	}
4573f89a643SAchiad Shochat 
4583f89a643SAchiad Shochat 	if (!err)
4593f89a643SAchiad Shochat 		*sys_image_guid = cpu_to_be64(tmp);
4603f89a643SAchiad Shochat 
4613f89a643SAchiad Shochat 	return err;
4623f89a643SAchiad Shochat 
4631b5daf11SMajd Dibbiny }
4641b5daf11SMajd Dibbiny 
4651b5daf11SMajd Dibbiny static int mlx5_query_max_pkeys(struct ib_device *ibdev,
4661b5daf11SMajd Dibbiny 				u16 *max_pkeys)
4671b5daf11SMajd Dibbiny {
4681b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
4691b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
4701b5daf11SMajd Dibbiny 
4711b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
4721b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
4731b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_max_pkeys(ibdev, max_pkeys);
4741b5daf11SMajd Dibbiny 
4751b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
4761b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
4771b5daf11SMajd Dibbiny 		*max_pkeys = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev,
4781b5daf11SMajd Dibbiny 						pkey_table_size));
4791b5daf11SMajd Dibbiny 		return 0;
4801b5daf11SMajd Dibbiny 
4811b5daf11SMajd Dibbiny 	default:
4821b5daf11SMajd Dibbiny 		return -EINVAL;
4831b5daf11SMajd Dibbiny 	}
4841b5daf11SMajd Dibbiny }
4851b5daf11SMajd Dibbiny 
4861b5daf11SMajd Dibbiny static int mlx5_query_vendor_id(struct ib_device *ibdev,
4871b5daf11SMajd Dibbiny 				u32 *vendor_id)
4881b5daf11SMajd Dibbiny {
4891b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
4901b5daf11SMajd Dibbiny 
4911b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
4921b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
4931b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_vendor_id(ibdev, vendor_id);
4941b5daf11SMajd Dibbiny 
4951b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
4961b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
4971b5daf11SMajd Dibbiny 		return mlx5_core_query_vendor_id(dev->mdev, vendor_id);
4981b5daf11SMajd Dibbiny 
4991b5daf11SMajd Dibbiny 	default:
5001b5daf11SMajd Dibbiny 		return -EINVAL;
5011b5daf11SMajd Dibbiny 	}
5021b5daf11SMajd Dibbiny }
5031b5daf11SMajd Dibbiny 
5041b5daf11SMajd Dibbiny static int mlx5_query_node_guid(struct mlx5_ib_dev *dev,
5051b5daf11SMajd Dibbiny 				__be64 *node_guid)
5061b5daf11SMajd Dibbiny {
5071b5daf11SMajd Dibbiny 	u64 tmp;
5081b5daf11SMajd Dibbiny 	int err;
5091b5daf11SMajd Dibbiny 
5101b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(&dev->ib_dev)) {
5111b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
5121b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_node_guid(dev, node_guid);
5131b5daf11SMajd Dibbiny 
5141b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
5151b5daf11SMajd Dibbiny 		err = mlx5_query_hca_vport_node_guid(dev->mdev, &tmp);
5163f89a643SAchiad Shochat 		break;
5173f89a643SAchiad Shochat 
5183f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
5193f89a643SAchiad Shochat 		err = mlx5_query_nic_vport_node_guid(dev->mdev, &tmp);
5203f89a643SAchiad Shochat 		break;
5211b5daf11SMajd Dibbiny 
5221b5daf11SMajd Dibbiny 	default:
5231b5daf11SMajd Dibbiny 		return -EINVAL;
5241b5daf11SMajd Dibbiny 	}
5253f89a643SAchiad Shochat 
5263f89a643SAchiad Shochat 	if (!err)
5273f89a643SAchiad Shochat 		*node_guid = cpu_to_be64(tmp);
5283f89a643SAchiad Shochat 
5293f89a643SAchiad Shochat 	return err;
5301b5daf11SMajd Dibbiny }
5311b5daf11SMajd Dibbiny 
5321b5daf11SMajd Dibbiny struct mlx5_reg_node_desc {
533bd99fdeaSYuval Shaia 	u8	desc[IB_DEVICE_NODE_DESC_MAX];
5341b5daf11SMajd Dibbiny };
5351b5daf11SMajd Dibbiny 
5361b5daf11SMajd Dibbiny static int mlx5_query_node_desc(struct mlx5_ib_dev *dev, char *node_desc)
5371b5daf11SMajd Dibbiny {
5381b5daf11SMajd Dibbiny 	struct mlx5_reg_node_desc in;
5391b5daf11SMajd Dibbiny 
5401b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(dev))
5411b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_node_desc(dev, node_desc);
5421b5daf11SMajd Dibbiny 
5431b5daf11SMajd Dibbiny 	memset(&in, 0, sizeof(in));
5441b5daf11SMajd Dibbiny 
5451b5daf11SMajd Dibbiny 	return mlx5_core_access_reg(dev->mdev, &in, sizeof(in), node_desc,
5461b5daf11SMajd Dibbiny 				    sizeof(struct mlx5_reg_node_desc),
5471b5daf11SMajd Dibbiny 				    MLX5_REG_NODE_DESC, 0, 0);
5481b5daf11SMajd Dibbiny }
5491b5daf11SMajd Dibbiny 
550e126ba97SEli Cohen static int mlx5_ib_query_device(struct ib_device *ibdev,
5512528e33eSMatan Barak 				struct ib_device_attr *props,
5522528e33eSMatan Barak 				struct ib_udata *uhw)
553e126ba97SEli Cohen {
554e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
555938fe83cSSaeed Mahameed 	struct mlx5_core_dev *mdev = dev->mdev;
556e126ba97SEli Cohen 	int err = -ENOMEM;
557288c01b7SEli Cohen 	int max_sq_desc;
558e126ba97SEli Cohen 	int max_rq_sg;
559e126ba97SEli Cohen 	int max_sq_sg;
560e0238a6aSSagi Grimberg 	u64 min_page_size = 1ull << MLX5_CAP_GEN(mdev, log_pg_sz);
561402ca536SBodong Wang 	struct mlx5_ib_query_device_resp resp = {};
562402ca536SBodong Wang 	size_t resp_len;
563402ca536SBodong Wang 	u64 max_tso;
564e126ba97SEli Cohen 
565402ca536SBodong Wang 	resp_len = sizeof(resp.comp_mask) + sizeof(resp.response_length);
566402ca536SBodong Wang 	if (uhw->outlen && uhw->outlen < resp_len)
567402ca536SBodong Wang 		return -EINVAL;
568402ca536SBodong Wang 	else
569402ca536SBodong Wang 		resp.response_length = resp_len;
570402ca536SBodong Wang 
571402ca536SBodong Wang 	if (uhw->inlen && !ib_is_udata_cleared(uhw, 0, uhw->inlen))
5722528e33eSMatan Barak 		return -EINVAL;
5732528e33eSMatan Barak 
574e126ba97SEli Cohen 	memset(props, 0, sizeof(*props));
5751b5daf11SMajd Dibbiny 	err = mlx5_query_system_image_guid(ibdev,
5761b5daf11SMajd Dibbiny 					   &props->sys_image_guid);
5771b5daf11SMajd Dibbiny 	if (err)
5781b5daf11SMajd Dibbiny 		return err;
5791b5daf11SMajd Dibbiny 
5801b5daf11SMajd Dibbiny 	err = mlx5_query_max_pkeys(ibdev, &props->max_pkeys);
5811b5daf11SMajd Dibbiny 	if (err)
5821b5daf11SMajd Dibbiny 		return err;
5831b5daf11SMajd Dibbiny 
5841b5daf11SMajd Dibbiny 	err = mlx5_query_vendor_id(ibdev, &props->vendor_id);
5851b5daf11SMajd Dibbiny 	if (err)
5861b5daf11SMajd Dibbiny 		return err;
587e126ba97SEli Cohen 
5889603b61dSJack Morgenstein 	props->fw_ver = ((u64)fw_rev_maj(dev->mdev) << 32) |
5899603b61dSJack Morgenstein 		(fw_rev_min(dev->mdev) << 16) |
5909603b61dSJack Morgenstein 		fw_rev_sub(dev->mdev);
591e126ba97SEli Cohen 	props->device_cap_flags    = IB_DEVICE_CHANGE_PHY_PORT |
592e126ba97SEli Cohen 		IB_DEVICE_PORT_ACTIVE_EVENT		|
593e126ba97SEli Cohen 		IB_DEVICE_SYS_IMAGE_GUID		|
5941a4c3a3dSEli Cohen 		IB_DEVICE_RC_RNR_NAK_GEN;
595938fe83cSSaeed Mahameed 
596938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, pkv))
597e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
598938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, qkv))
599e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
600938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, apm))
601e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
602938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, xrc))
603e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_XRC;
604d2370e0aSMatan Barak 	if (MLX5_CAP_GEN(mdev, imaicl)) {
605d2370e0aSMatan Barak 		props->device_cap_flags |= IB_DEVICE_MEM_WINDOW |
606d2370e0aSMatan Barak 					   IB_DEVICE_MEM_WINDOW_TYPE_2B;
607d2370e0aSMatan Barak 		props->max_mw = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
608b005d316SSagi Grimberg 		/* We support 'Gappy' memory registration too */
609b005d316SSagi Grimberg 		props->device_cap_flags |= IB_DEVICE_SG_GAPS_REG;
610d2370e0aSMatan Barak 	}
611e126ba97SEli Cohen 	props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
612938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, sho)) {
6132dea9094SSagi Grimberg 		props->device_cap_flags |= IB_DEVICE_SIGNATURE_HANDOVER;
6142dea9094SSagi Grimberg 		/* At this stage no support for signature handover */
6152dea9094SSagi Grimberg 		props->sig_prot_cap = IB_PROT_T10DIF_TYPE_1 |
6162dea9094SSagi Grimberg 				      IB_PROT_T10DIF_TYPE_2 |
6172dea9094SSagi Grimberg 				      IB_PROT_T10DIF_TYPE_3;
6182dea9094SSagi Grimberg 		props->sig_guard_cap = IB_GUARD_T10DIF_CRC |
6192dea9094SSagi Grimberg 				       IB_GUARD_T10DIF_CSUM;
6202dea9094SSagi Grimberg 	}
621938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, block_lb_mc))
622f360d88aSEli Cohen 		props->device_cap_flags |= IB_DEVICE_BLOCK_MULTICAST_LOOPBACK;
623e126ba97SEli Cohen 
624402ca536SBodong Wang 	if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads)) {
625e8161334SNoa Osherovich 		if (MLX5_CAP_ETH(mdev, csum_cap)) {
626e8161334SNoa Osherovich 			/* Legacy bit to support old userspace libraries */
62788115fe7SBodong Wang 			props->device_cap_flags |= IB_DEVICE_RAW_IP_CSUM;
628e8161334SNoa Osherovich 			props->raw_packet_caps |= IB_RAW_PACKET_CAP_IP_CSUM;
629e8161334SNoa Osherovich 		}
630e8161334SNoa Osherovich 
631e8161334SNoa Osherovich 		if (MLX5_CAP_ETH(dev->mdev, vlan_cap))
632e8161334SNoa Osherovich 			props->raw_packet_caps |=
633e8161334SNoa Osherovich 				IB_RAW_PACKET_CAP_CVLAN_STRIPPING;
63488115fe7SBodong Wang 
635402ca536SBodong Wang 		if (field_avail(typeof(resp), tso_caps, uhw->outlen)) {
636402ca536SBodong Wang 			max_tso = MLX5_CAP_ETH(mdev, max_lso_cap);
637402ca536SBodong Wang 			if (max_tso) {
638402ca536SBodong Wang 				resp.tso_caps.max_tso = 1 << max_tso;
639402ca536SBodong Wang 				resp.tso_caps.supported_qpts |=
640402ca536SBodong Wang 					1 << IB_QPT_RAW_PACKET;
641402ca536SBodong Wang 				resp.response_length += sizeof(resp.tso_caps);
642402ca536SBodong Wang 			}
643402ca536SBodong Wang 		}
64431f69a82SYishai Hadas 
64531f69a82SYishai Hadas 		if (field_avail(typeof(resp), rss_caps, uhw->outlen)) {
64631f69a82SYishai Hadas 			resp.rss_caps.rx_hash_function =
64731f69a82SYishai Hadas 						MLX5_RX_HASH_FUNC_TOEPLITZ;
64831f69a82SYishai Hadas 			resp.rss_caps.rx_hash_fields_mask =
64931f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_IPV4 |
65031f69a82SYishai Hadas 						MLX5_RX_HASH_DST_IPV4 |
65131f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_IPV6 |
65231f69a82SYishai Hadas 						MLX5_RX_HASH_DST_IPV6 |
65331f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_PORT_TCP |
65431f69a82SYishai Hadas 						MLX5_RX_HASH_DST_PORT_TCP |
65531f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_PORT_UDP |
65631f69a82SYishai Hadas 						MLX5_RX_HASH_DST_PORT_UDP;
65731f69a82SYishai Hadas 			resp.response_length += sizeof(resp.rss_caps);
65831f69a82SYishai Hadas 		}
65931f69a82SYishai Hadas 	} else {
66031f69a82SYishai Hadas 		if (field_avail(typeof(resp), tso_caps, uhw->outlen))
66131f69a82SYishai Hadas 			resp.response_length += sizeof(resp.tso_caps);
66231f69a82SYishai Hadas 		if (field_avail(typeof(resp), rss_caps, uhw->outlen))
66331f69a82SYishai Hadas 			resp.response_length += sizeof(resp.rss_caps);
664402ca536SBodong Wang 	}
665402ca536SBodong Wang 
666f0313965SErez Shitrit 	if (MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) {
667f0313965SErez Shitrit 		props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
668f0313965SErez Shitrit 		props->device_cap_flags |= IB_DEVICE_UD_TSO;
669f0313965SErez Shitrit 	}
670f0313965SErez Shitrit 
671cff5a0f3SMajd Dibbiny 	if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
672e8161334SNoa Osherovich 	    MLX5_CAP_ETH(dev->mdev, scatter_fcs)) {
673e8161334SNoa Osherovich 		/* Legacy bit to support old userspace libraries */
674cff5a0f3SMajd Dibbiny 		props->device_cap_flags |= IB_DEVICE_RAW_SCATTER_FCS;
675e8161334SNoa Osherovich 		props->raw_packet_caps |= IB_RAW_PACKET_CAP_SCATTER_FCS;
676e8161334SNoa Osherovich 	}
677cff5a0f3SMajd Dibbiny 
678da6d6ba3SMaor Gottlieb 	if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS))
679da6d6ba3SMaor Gottlieb 		props->device_cap_flags |= IB_DEVICE_MANAGED_FLOW_STEERING;
680da6d6ba3SMaor Gottlieb 
6811b5daf11SMajd Dibbiny 	props->vendor_part_id	   = mdev->pdev->device;
6821b5daf11SMajd Dibbiny 	props->hw_ver		   = mdev->pdev->revision;
683e126ba97SEli Cohen 
684e126ba97SEli Cohen 	props->max_mr_size	   = ~0ull;
685e0238a6aSSagi Grimberg 	props->page_size_cap	   = ~(min_page_size - 1);
686938fe83cSSaeed Mahameed 	props->max_qp		   = 1 << MLX5_CAP_GEN(mdev, log_max_qp);
687938fe83cSSaeed Mahameed 	props->max_qp_wr	   = 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
688938fe83cSSaeed Mahameed 	max_rq_sg =  MLX5_CAP_GEN(mdev, max_wqe_sz_rq) /
689938fe83cSSaeed Mahameed 		     sizeof(struct mlx5_wqe_data_seg);
690288c01b7SEli Cohen 	max_sq_desc = min_t(int, MLX5_CAP_GEN(mdev, max_wqe_sz_sq), 512);
691288c01b7SEli Cohen 	max_sq_sg = (max_sq_desc - sizeof(struct mlx5_wqe_ctrl_seg) -
692288c01b7SEli Cohen 		     sizeof(struct mlx5_wqe_raddr_seg)) /
693e126ba97SEli Cohen 		sizeof(struct mlx5_wqe_data_seg);
694e126ba97SEli Cohen 	props->max_sge = min(max_rq_sg, max_sq_sg);
695986ef95eSSagi Grimberg 	props->max_sge_rd	   = MLX5_MAX_SGE_RD;
696938fe83cSSaeed Mahameed 	props->max_cq		   = 1 << MLX5_CAP_GEN(mdev, log_max_cq);
6979f177686SLeon Romanovsky 	props->max_cqe = (1 << MLX5_CAP_GEN(mdev, log_max_cq_sz)) - 1;
698938fe83cSSaeed Mahameed 	props->max_mr		   = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
699938fe83cSSaeed Mahameed 	props->max_pd		   = 1 << MLX5_CAP_GEN(mdev, log_max_pd);
700938fe83cSSaeed Mahameed 	props->max_qp_rd_atom	   = 1 << MLX5_CAP_GEN(mdev, log_max_ra_req_qp);
701938fe83cSSaeed Mahameed 	props->max_qp_init_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_res_qp);
702938fe83cSSaeed Mahameed 	props->max_srq		   = 1 << MLX5_CAP_GEN(mdev, log_max_srq);
703938fe83cSSaeed Mahameed 	props->max_srq_wr = (1 << MLX5_CAP_GEN(mdev, log_max_srq_sz)) - 1;
704938fe83cSSaeed Mahameed 	props->local_ca_ack_delay  = MLX5_CAP_GEN(mdev, local_ca_ack_delay);
705e126ba97SEli Cohen 	props->max_res_rd_atom	   = props->max_qp_rd_atom * props->max_qp;
706e126ba97SEli Cohen 	props->max_srq_sge	   = max_rq_sg - 1;
707911f4331SSagi Grimberg 	props->max_fast_reg_page_list_len =
708911f4331SSagi Grimberg 		1 << MLX5_CAP_GEN(mdev, log_max_klm_list_size);
709da7525d2SEran Ben Elisha 	get_atomic_caps(dev, props);
71081bea28fSEli Cohen 	props->masked_atomic_cap   = IB_ATOMIC_NONE;
711938fe83cSSaeed Mahameed 	props->max_mcast_grp	   = 1 << MLX5_CAP_GEN(mdev, log_max_mcg);
712938fe83cSSaeed Mahameed 	props->max_mcast_qp_attach = MLX5_CAP_GEN(mdev, max_qp_mcg);
713e126ba97SEli Cohen 	props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
714e126ba97SEli Cohen 					   props->max_mcast_grp;
715e126ba97SEli Cohen 	props->max_map_per_fmr = INT_MAX; /* no limit in ConnectIB */
71686695a65SMaor Gottlieb 	props->max_ah = INT_MAX;
7177c60bcbbSMatan Barak 	props->hca_core_clock = MLX5_CAP_GEN(mdev, device_frequency_khz);
7187c60bcbbSMatan Barak 	props->timestamp_mask = 0x7FFFFFFFFFFFFFFFULL;
719e126ba97SEli Cohen 
7208cdd312cSHaggai Eran #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
721938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, pg))
7228cdd312cSHaggai Eran 		props->device_cap_flags |= IB_DEVICE_ON_DEMAND_PAGING;
7238cdd312cSHaggai Eran 	props->odp_caps = dev->odp_caps;
7248cdd312cSHaggai Eran #endif
7258cdd312cSHaggai Eran 
726051f2630SLeon Romanovsky 	if (MLX5_CAP_GEN(mdev, cd))
727051f2630SLeon Romanovsky 		props->device_cap_flags |= IB_DEVICE_CROSS_CHANNEL;
728051f2630SLeon Romanovsky 
729eff901d3SEli Cohen 	if (!mlx5_core_is_pf(mdev))
730eff901d3SEli Cohen 		props->device_cap_flags |= IB_DEVICE_VIRTUAL_FUNCTION;
731eff901d3SEli Cohen 
73231f69a82SYishai Hadas 	if (mlx5_ib_port_link_layer(ibdev, 1) ==
73331f69a82SYishai Hadas 	    IB_LINK_LAYER_ETHERNET) {
73431f69a82SYishai Hadas 		props->rss_caps.max_rwq_indirection_tables =
73531f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt);
73631f69a82SYishai Hadas 		props->rss_caps.max_rwq_indirection_table_size =
73731f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt_size);
73831f69a82SYishai Hadas 		props->rss_caps.supported_qpts = 1 << IB_QPT_RAW_PACKET;
73931f69a82SYishai Hadas 		props->max_wq_type_rq =
74031f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rq);
74131f69a82SYishai Hadas 	}
74231f69a82SYishai Hadas 
7437e43a2a5SBodong Wang 	if (field_avail(typeof(resp), cqe_comp_caps, uhw->outlen)) {
7447e43a2a5SBodong Wang 		resp.cqe_comp_caps.max_num =
7457e43a2a5SBodong Wang 			MLX5_CAP_GEN(dev->mdev, cqe_compression) ?
7467e43a2a5SBodong Wang 			MLX5_CAP_GEN(dev->mdev, cqe_compression_max_num) : 0;
7477e43a2a5SBodong Wang 		resp.cqe_comp_caps.supported_format =
7487e43a2a5SBodong Wang 			MLX5_IB_CQE_RES_FORMAT_HASH |
7497e43a2a5SBodong Wang 			MLX5_IB_CQE_RES_FORMAT_CSUM;
7507e43a2a5SBodong Wang 		resp.response_length += sizeof(resp.cqe_comp_caps);
7517e43a2a5SBodong Wang 	}
7527e43a2a5SBodong Wang 
753d949167dSBodong Wang 	if (field_avail(typeof(resp), packet_pacing_caps, uhw->outlen)) {
754d949167dSBodong Wang 		if (MLX5_CAP_QOS(mdev, packet_pacing) &&
755d949167dSBodong Wang 		    MLX5_CAP_GEN(mdev, qos)) {
756d949167dSBodong Wang 			resp.packet_pacing_caps.qp_rate_limit_max =
757d949167dSBodong Wang 				MLX5_CAP_QOS(mdev, packet_pacing_max_rate);
758d949167dSBodong Wang 			resp.packet_pacing_caps.qp_rate_limit_min =
759d949167dSBodong Wang 				MLX5_CAP_QOS(mdev, packet_pacing_min_rate);
760d949167dSBodong Wang 			resp.packet_pacing_caps.supported_qpts |=
761d949167dSBodong Wang 				1 << IB_QPT_RAW_PACKET;
762d949167dSBodong Wang 		}
763d949167dSBodong Wang 		resp.response_length += sizeof(resp.packet_pacing_caps);
764d949167dSBodong Wang 	}
765d949167dSBodong Wang 
7669f885201SLeon Romanovsky 	if (field_avail(typeof(resp), mlx5_ib_support_multi_pkt_send_wqes,
7679f885201SLeon Romanovsky 			uhw->outlen)) {
7689f885201SLeon Romanovsky 		resp.mlx5_ib_support_multi_pkt_send_wqes =
7699f885201SLeon Romanovsky 			MLX5_CAP_ETH(mdev, multi_pkt_send_wqe);
7709f885201SLeon Romanovsky 		resp.response_length +=
7719f885201SLeon Romanovsky 			sizeof(resp.mlx5_ib_support_multi_pkt_send_wqes);
7729f885201SLeon Romanovsky 	}
7739f885201SLeon Romanovsky 
7749f885201SLeon Romanovsky 	if (field_avail(typeof(resp), reserved, uhw->outlen))
7759f885201SLeon Romanovsky 		resp.response_length += sizeof(resp.reserved);
7769f885201SLeon Romanovsky 
777402ca536SBodong Wang 	if (uhw->outlen) {
778402ca536SBodong Wang 		err = ib_copy_to_udata(uhw, &resp, resp.response_length);
779402ca536SBodong Wang 
780402ca536SBodong Wang 		if (err)
781402ca536SBodong Wang 			return err;
782402ca536SBodong Wang 	}
783402ca536SBodong Wang 
7841b5daf11SMajd Dibbiny 	return 0;
7851b5daf11SMajd Dibbiny }
786e126ba97SEli Cohen 
7871b5daf11SMajd Dibbiny enum mlx5_ib_width {
7881b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_1X	= 1 << 0,
7891b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_2X	= 1 << 1,
7901b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_4X	= 1 << 2,
7911b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_8X	= 1 << 3,
7921b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_12X	= 1 << 4
7931b5daf11SMajd Dibbiny };
7941b5daf11SMajd Dibbiny 
7951b5daf11SMajd Dibbiny static int translate_active_width(struct ib_device *ibdev, u8 active_width,
7961b5daf11SMajd Dibbiny 				  u8 *ib_width)
7971b5daf11SMajd Dibbiny {
7981b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
7991b5daf11SMajd Dibbiny 	int err = 0;
8001b5daf11SMajd Dibbiny 
8011b5daf11SMajd Dibbiny 	if (active_width & MLX5_IB_WIDTH_1X) {
8021b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_1X;
8031b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_2X) {
8041b5daf11SMajd Dibbiny 		mlx5_ib_dbg(dev, "active_width %d is not supported by IB spec\n",
8051b5daf11SMajd Dibbiny 			    (int)active_width);
8061b5daf11SMajd Dibbiny 		err = -EINVAL;
8071b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_4X) {
8081b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_4X;
8091b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_8X) {
8101b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_8X;
8111b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_12X) {
8121b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_12X;
8131b5daf11SMajd Dibbiny 	} else {
8141b5daf11SMajd Dibbiny 		mlx5_ib_dbg(dev, "Invalid active_width %d\n",
8151b5daf11SMajd Dibbiny 			    (int)active_width);
8161b5daf11SMajd Dibbiny 		err = -EINVAL;
8171b5daf11SMajd Dibbiny 	}
8181b5daf11SMajd Dibbiny 
8191b5daf11SMajd Dibbiny 	return err;
8201b5daf11SMajd Dibbiny }
8211b5daf11SMajd Dibbiny 
8221b5daf11SMajd Dibbiny static int mlx5_mtu_to_ib_mtu(int mtu)
8231b5daf11SMajd Dibbiny {
8241b5daf11SMajd Dibbiny 	switch (mtu) {
8251b5daf11SMajd Dibbiny 	case 256: return 1;
8261b5daf11SMajd Dibbiny 	case 512: return 2;
8271b5daf11SMajd Dibbiny 	case 1024: return 3;
8281b5daf11SMajd Dibbiny 	case 2048: return 4;
8291b5daf11SMajd Dibbiny 	case 4096: return 5;
8301b5daf11SMajd Dibbiny 	default:
8311b5daf11SMajd Dibbiny 		pr_warn("invalid mtu\n");
8321b5daf11SMajd Dibbiny 		return -1;
8331b5daf11SMajd Dibbiny 	}
8341b5daf11SMajd Dibbiny }
8351b5daf11SMajd Dibbiny 
8361b5daf11SMajd Dibbiny enum ib_max_vl_num {
8371b5daf11SMajd Dibbiny 	__IB_MAX_VL_0		= 1,
8381b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_1		= 2,
8391b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_3		= 3,
8401b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_7		= 4,
8411b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_14	= 5,
8421b5daf11SMajd Dibbiny };
8431b5daf11SMajd Dibbiny 
8441b5daf11SMajd Dibbiny enum mlx5_vl_hw_cap {
8451b5daf11SMajd Dibbiny 	MLX5_VL_HW_0	= 1,
8461b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_1	= 2,
8471b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_2	= 3,
8481b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_3	= 4,
8491b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_4	= 5,
8501b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_5	= 6,
8511b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_6	= 7,
8521b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_7	= 8,
8531b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_14	= 15
8541b5daf11SMajd Dibbiny };
8551b5daf11SMajd Dibbiny 
8561b5daf11SMajd Dibbiny static int translate_max_vl_num(struct ib_device *ibdev, u8 vl_hw_cap,
8571b5daf11SMajd Dibbiny 				u8 *max_vl_num)
8581b5daf11SMajd Dibbiny {
8591b5daf11SMajd Dibbiny 	switch (vl_hw_cap) {
8601b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0:
8611b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0;
8621b5daf11SMajd Dibbiny 		break;
8631b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_1:
8641b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_1;
8651b5daf11SMajd Dibbiny 		break;
8661b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_3:
8671b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_3;
8681b5daf11SMajd Dibbiny 		break;
8691b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_7:
8701b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_7;
8711b5daf11SMajd Dibbiny 		break;
8721b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_14:
8731b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_14;
8741b5daf11SMajd Dibbiny 		break;
8751b5daf11SMajd Dibbiny 
8761b5daf11SMajd Dibbiny 	default:
8771b5daf11SMajd Dibbiny 		return -EINVAL;
8781b5daf11SMajd Dibbiny 	}
8791b5daf11SMajd Dibbiny 
8801b5daf11SMajd Dibbiny 	return 0;
8811b5daf11SMajd Dibbiny }
8821b5daf11SMajd Dibbiny 
8831b5daf11SMajd Dibbiny static int mlx5_query_hca_port(struct ib_device *ibdev, u8 port,
8841b5daf11SMajd Dibbiny 			       struct ib_port_attr *props)
8851b5daf11SMajd Dibbiny {
8861b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
8871b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
8881b5daf11SMajd Dibbiny 	struct mlx5_hca_vport_context *rep;
889046339eaSSaeed Mahameed 	u16 max_mtu;
890046339eaSSaeed Mahameed 	u16 oper_mtu;
8911b5daf11SMajd Dibbiny 	int err;
8921b5daf11SMajd Dibbiny 	u8 ib_link_width_oper;
8931b5daf11SMajd Dibbiny 	u8 vl_hw_cap;
8941b5daf11SMajd Dibbiny 
8951b5daf11SMajd Dibbiny 	rep = kzalloc(sizeof(*rep), GFP_KERNEL);
8961b5daf11SMajd Dibbiny 	if (!rep) {
8971b5daf11SMajd Dibbiny 		err = -ENOMEM;
8981b5daf11SMajd Dibbiny 		goto out;
8991b5daf11SMajd Dibbiny 	}
9001b5daf11SMajd Dibbiny 
901c4550c63SOr Gerlitz 	/* props being zeroed by the caller, avoid zeroing it here */
9021b5daf11SMajd Dibbiny 
9031b5daf11SMajd Dibbiny 	err = mlx5_query_hca_vport_context(mdev, 0, port, 0, rep);
9041b5daf11SMajd Dibbiny 	if (err)
9051b5daf11SMajd Dibbiny 		goto out;
9061b5daf11SMajd Dibbiny 
9071b5daf11SMajd Dibbiny 	props->lid		= rep->lid;
9081b5daf11SMajd Dibbiny 	props->lmc		= rep->lmc;
9091b5daf11SMajd Dibbiny 	props->sm_lid		= rep->sm_lid;
9101b5daf11SMajd Dibbiny 	props->sm_sl		= rep->sm_sl;
9111b5daf11SMajd Dibbiny 	props->state		= rep->vport_state;
9121b5daf11SMajd Dibbiny 	props->phys_state	= rep->port_physical_state;
9131b5daf11SMajd Dibbiny 	props->port_cap_flags	= rep->cap_mask1;
9141b5daf11SMajd Dibbiny 	props->gid_tbl_len	= mlx5_get_gid_table_len(MLX5_CAP_GEN(mdev, gid_table_size));
9151b5daf11SMajd Dibbiny 	props->max_msg_sz	= 1 << MLX5_CAP_GEN(mdev, log_max_msg);
9161b5daf11SMajd Dibbiny 	props->pkey_tbl_len	= mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, pkey_table_size));
9171b5daf11SMajd Dibbiny 	props->bad_pkey_cntr	= rep->pkey_violation_counter;
9181b5daf11SMajd Dibbiny 	props->qkey_viol_cntr	= rep->qkey_violation_counter;
9191b5daf11SMajd Dibbiny 	props->subnet_timeout	= rep->subnet_timeout;
9201b5daf11SMajd Dibbiny 	props->init_type_reply	= rep->init_type_reply;
921eff901d3SEli Cohen 	props->grh_required	= rep->grh_required;
9221b5daf11SMajd Dibbiny 
9231b5daf11SMajd Dibbiny 	err = mlx5_query_port_link_width_oper(mdev, &ib_link_width_oper, port);
9241b5daf11SMajd Dibbiny 	if (err)
9251b5daf11SMajd Dibbiny 		goto out;
9261b5daf11SMajd Dibbiny 
9271b5daf11SMajd Dibbiny 	err = translate_active_width(ibdev, ib_link_width_oper,
9281b5daf11SMajd Dibbiny 				     &props->active_width);
9291b5daf11SMajd Dibbiny 	if (err)
9301b5daf11SMajd Dibbiny 		goto out;
931d5beb7f2SNoa Osherovich 	err = mlx5_query_port_ib_proto_oper(mdev, &props->active_speed, port);
9321b5daf11SMajd Dibbiny 	if (err)
9331b5daf11SMajd Dibbiny 		goto out;
9341b5daf11SMajd Dibbiny 
935facc9699SSaeed Mahameed 	mlx5_query_port_max_mtu(mdev, &max_mtu, port);
9361b5daf11SMajd Dibbiny 
9371b5daf11SMajd Dibbiny 	props->max_mtu = mlx5_mtu_to_ib_mtu(max_mtu);
9381b5daf11SMajd Dibbiny 
939facc9699SSaeed Mahameed 	mlx5_query_port_oper_mtu(mdev, &oper_mtu, port);
9401b5daf11SMajd Dibbiny 
9411b5daf11SMajd Dibbiny 	props->active_mtu = mlx5_mtu_to_ib_mtu(oper_mtu);
9421b5daf11SMajd Dibbiny 
9431b5daf11SMajd Dibbiny 	err = mlx5_query_port_vl_hw_cap(mdev, &vl_hw_cap, port);
9441b5daf11SMajd Dibbiny 	if (err)
9451b5daf11SMajd Dibbiny 		goto out;
9461b5daf11SMajd Dibbiny 
9471b5daf11SMajd Dibbiny 	err = translate_max_vl_num(ibdev, vl_hw_cap,
9481b5daf11SMajd Dibbiny 				   &props->max_vl_num);
9491b5daf11SMajd Dibbiny out:
9501b5daf11SMajd Dibbiny 	kfree(rep);
951e126ba97SEli Cohen 	return err;
952e126ba97SEli Cohen }
953e126ba97SEli Cohen 
954e126ba97SEli Cohen int mlx5_ib_query_port(struct ib_device *ibdev, u8 port,
955e126ba97SEli Cohen 		       struct ib_port_attr *props)
956e126ba97SEli Cohen {
957*095b0927SIlan Tayari 	unsigned int count;
958*095b0927SIlan Tayari 	int ret;
959*095b0927SIlan Tayari 
9601b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
9611b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
962*095b0927SIlan Tayari 		ret = mlx5_query_mad_ifc_port(ibdev, port, props);
963*095b0927SIlan Tayari 		break;
964e126ba97SEli Cohen 
9651b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
966*095b0927SIlan Tayari 		ret = mlx5_query_hca_port(ibdev, port, props);
967*095b0927SIlan Tayari 		break;
9681b5daf11SMajd Dibbiny 
9693f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
970*095b0927SIlan Tayari 		ret = mlx5_query_port_roce(ibdev, port, props);
971*095b0927SIlan Tayari 		break;
9723f89a643SAchiad Shochat 
9731b5daf11SMajd Dibbiny 	default:
974*095b0927SIlan Tayari 		ret = -EINVAL;
975e126ba97SEli Cohen 	}
976*095b0927SIlan Tayari 
977*095b0927SIlan Tayari 	if (!ret && props) {
978*095b0927SIlan Tayari 		count = mlx5_core_reserved_gids_count(to_mdev(ibdev)->mdev);
979*095b0927SIlan Tayari 		props->gid_tbl_len -= count;
980*095b0927SIlan Tayari 	}
981*095b0927SIlan Tayari 	return ret;
982e126ba97SEli Cohen }
983e126ba97SEli Cohen 
984e126ba97SEli Cohen static int mlx5_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
985e126ba97SEli Cohen 			     union ib_gid *gid)
986e126ba97SEli Cohen {
9871b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
9881b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
989e126ba97SEli Cohen 
9901b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
9911b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
9921b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_gids(ibdev, port, index, gid);
993e126ba97SEli Cohen 
9941b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
9951b5daf11SMajd Dibbiny 		return mlx5_query_hca_vport_gid(mdev, 0, port, 0, index, gid);
996e126ba97SEli Cohen 
9971b5daf11SMajd Dibbiny 	default:
9981b5daf11SMajd Dibbiny 		return -EINVAL;
9991b5daf11SMajd Dibbiny 	}
1000e126ba97SEli Cohen 
1001e126ba97SEli Cohen }
1002e126ba97SEli Cohen 
1003e126ba97SEli Cohen static int mlx5_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
1004e126ba97SEli Cohen 			      u16 *pkey)
1005e126ba97SEli Cohen {
10061b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
10071b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
1008e126ba97SEli Cohen 
10091b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
10101b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
10111b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_pkey(ibdev, port, index, pkey);
1012e126ba97SEli Cohen 
10131b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
10141b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
10151b5daf11SMajd Dibbiny 		return mlx5_query_hca_vport_pkey(mdev, 0, port,  0, index,
10161b5daf11SMajd Dibbiny 						 pkey);
10171b5daf11SMajd Dibbiny 	default:
10181b5daf11SMajd Dibbiny 		return -EINVAL;
1019e126ba97SEli Cohen 	}
10201b5daf11SMajd Dibbiny }
1021e126ba97SEli Cohen 
1022e126ba97SEli Cohen static int mlx5_ib_modify_device(struct ib_device *ibdev, int mask,
1023e126ba97SEli Cohen 				 struct ib_device_modify *props)
1024e126ba97SEli Cohen {
1025e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1026e126ba97SEli Cohen 	struct mlx5_reg_node_desc in;
1027e126ba97SEli Cohen 	struct mlx5_reg_node_desc out;
1028e126ba97SEli Cohen 	int err;
1029e126ba97SEli Cohen 
1030e126ba97SEli Cohen 	if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
1031e126ba97SEli Cohen 		return -EOPNOTSUPP;
1032e126ba97SEli Cohen 
1033e126ba97SEli Cohen 	if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
1034e126ba97SEli Cohen 		return 0;
1035e126ba97SEli Cohen 
1036e126ba97SEli Cohen 	/*
1037e126ba97SEli Cohen 	 * If possible, pass node desc to FW, so it can generate
1038e126ba97SEli Cohen 	 * a 144 trap.  If cmd fails, just ignore.
1039e126ba97SEli Cohen 	 */
1040bd99fdeaSYuval Shaia 	memcpy(&in, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
10419603b61dSJack Morgenstein 	err = mlx5_core_access_reg(dev->mdev, &in, sizeof(in), &out,
1042e126ba97SEli Cohen 				   sizeof(out), MLX5_REG_NODE_DESC, 0, 1);
1043e126ba97SEli Cohen 	if (err)
1044e126ba97SEli Cohen 		return err;
1045e126ba97SEli Cohen 
1046bd99fdeaSYuval Shaia 	memcpy(ibdev->node_desc, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
1047e126ba97SEli Cohen 
1048e126ba97SEli Cohen 	return err;
1049e126ba97SEli Cohen }
1050e126ba97SEli Cohen 
1051cdbe33d0SEli Cohen static int set_port_caps_atomic(struct mlx5_ib_dev *dev, u8 port_num, u32 mask,
1052cdbe33d0SEli Cohen 				u32 value)
1053cdbe33d0SEli Cohen {
1054cdbe33d0SEli Cohen 	struct mlx5_hca_vport_context ctx = {};
1055cdbe33d0SEli Cohen 	int err;
1056cdbe33d0SEli Cohen 
1057cdbe33d0SEli Cohen 	err = mlx5_query_hca_vport_context(dev->mdev, 0,
1058cdbe33d0SEli Cohen 					   port_num, 0, &ctx);
1059cdbe33d0SEli Cohen 	if (err)
1060cdbe33d0SEli Cohen 		return err;
1061cdbe33d0SEli Cohen 
1062cdbe33d0SEli Cohen 	if (~ctx.cap_mask1_perm & mask) {
1063cdbe33d0SEli Cohen 		mlx5_ib_warn(dev, "trying to change bitmask 0x%X but change supported 0x%X\n",
1064cdbe33d0SEli Cohen 			     mask, ctx.cap_mask1_perm);
1065cdbe33d0SEli Cohen 		return -EINVAL;
1066cdbe33d0SEli Cohen 	}
1067cdbe33d0SEli Cohen 
1068cdbe33d0SEli Cohen 	ctx.cap_mask1 = value;
1069cdbe33d0SEli Cohen 	ctx.cap_mask1_perm = mask;
1070cdbe33d0SEli Cohen 	err = mlx5_core_modify_hca_vport_context(dev->mdev, 0,
1071cdbe33d0SEli Cohen 						 port_num, 0, &ctx);
1072cdbe33d0SEli Cohen 
1073cdbe33d0SEli Cohen 	return err;
1074cdbe33d0SEli Cohen }
1075cdbe33d0SEli Cohen 
1076e126ba97SEli Cohen static int mlx5_ib_modify_port(struct ib_device *ibdev, u8 port, int mask,
1077e126ba97SEli Cohen 			       struct ib_port_modify *props)
1078e126ba97SEli Cohen {
1079e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1080e126ba97SEli Cohen 	struct ib_port_attr attr;
1081e126ba97SEli Cohen 	u32 tmp;
1082e126ba97SEli Cohen 	int err;
1083cdbe33d0SEli Cohen 	u32 change_mask;
1084cdbe33d0SEli Cohen 	u32 value;
1085cdbe33d0SEli Cohen 	bool is_ib = (mlx5_ib_port_link_layer(ibdev, port) ==
1086cdbe33d0SEli Cohen 		      IB_LINK_LAYER_INFINIBAND);
1087cdbe33d0SEli Cohen 
1088cdbe33d0SEli Cohen 	if (MLX5_CAP_GEN(dev->mdev, ib_virt) && is_ib) {
1089cdbe33d0SEli Cohen 		change_mask = props->clr_port_cap_mask | props->set_port_cap_mask;
1090cdbe33d0SEli Cohen 		value = ~props->clr_port_cap_mask | props->set_port_cap_mask;
1091cdbe33d0SEli Cohen 		return set_port_caps_atomic(dev, port, change_mask, value);
1092cdbe33d0SEli Cohen 	}
1093e126ba97SEli Cohen 
1094e126ba97SEli Cohen 	mutex_lock(&dev->cap_mask_mutex);
1095e126ba97SEli Cohen 
1096c4550c63SOr Gerlitz 	err = ib_query_port(ibdev, port, &attr);
1097e126ba97SEli Cohen 	if (err)
1098e126ba97SEli Cohen 		goto out;
1099e126ba97SEli Cohen 
1100e126ba97SEli Cohen 	tmp = (attr.port_cap_flags | props->set_port_cap_mask) &
1101e126ba97SEli Cohen 		~props->clr_port_cap_mask;
1102e126ba97SEli Cohen 
11039603b61dSJack Morgenstein 	err = mlx5_set_port_caps(dev->mdev, port, tmp);
1104e126ba97SEli Cohen 
1105e126ba97SEli Cohen out:
1106e126ba97SEli Cohen 	mutex_unlock(&dev->cap_mask_mutex);
1107e126ba97SEli Cohen 	return err;
1108e126ba97SEli Cohen }
1109e126ba97SEli Cohen 
111030aa60b3SEli Cohen static void print_lib_caps(struct mlx5_ib_dev *dev, u64 caps)
111130aa60b3SEli Cohen {
111230aa60b3SEli Cohen 	mlx5_ib_dbg(dev, "MLX5_LIB_CAP_4K_UAR = %s\n",
111330aa60b3SEli Cohen 		    caps & MLX5_LIB_CAP_4K_UAR ? "y" : "n");
111430aa60b3SEli Cohen }
111530aa60b3SEli Cohen 
1116b037c29aSEli Cohen static int calc_total_bfregs(struct mlx5_ib_dev *dev, bool lib_uar_4k,
1117b037c29aSEli Cohen 			     struct mlx5_ib_alloc_ucontext_req_v2 *req,
1118b037c29aSEli Cohen 			     u32 *num_sys_pages)
1119b037c29aSEli Cohen {
1120b037c29aSEli Cohen 	int uars_per_sys_page;
1121b037c29aSEli Cohen 	int bfregs_per_sys_page;
1122b037c29aSEli Cohen 	int ref_bfregs = req->total_num_bfregs;
1123b037c29aSEli Cohen 
1124b037c29aSEli Cohen 	if (req->total_num_bfregs == 0)
1125b037c29aSEli Cohen 		return -EINVAL;
1126b037c29aSEli Cohen 
1127b037c29aSEli Cohen 	BUILD_BUG_ON(MLX5_MAX_BFREGS % MLX5_NON_FP_BFREGS_IN_PAGE);
1128b037c29aSEli Cohen 	BUILD_BUG_ON(MLX5_MAX_BFREGS < MLX5_NON_FP_BFREGS_IN_PAGE);
1129b037c29aSEli Cohen 
1130b037c29aSEli Cohen 	if (req->total_num_bfregs > MLX5_MAX_BFREGS)
1131b037c29aSEli Cohen 		return -ENOMEM;
1132b037c29aSEli Cohen 
1133b037c29aSEli Cohen 	uars_per_sys_page = get_uars_per_sys_page(dev, lib_uar_4k);
1134b037c29aSEli Cohen 	bfregs_per_sys_page = uars_per_sys_page * MLX5_NON_FP_BFREGS_PER_UAR;
1135b037c29aSEli Cohen 	req->total_num_bfregs = ALIGN(req->total_num_bfregs, bfregs_per_sys_page);
1136b037c29aSEli Cohen 	*num_sys_pages = req->total_num_bfregs / bfregs_per_sys_page;
1137b037c29aSEli Cohen 
1138b037c29aSEli Cohen 	if (req->num_low_latency_bfregs > req->total_num_bfregs - 1)
1139b037c29aSEli Cohen 		return -EINVAL;
1140b037c29aSEli Cohen 
1141b037c29aSEli Cohen 	mlx5_ib_dbg(dev, "uar_4k: fw support %s, lib support %s, user requested %d bfregs, alloated %d, using %d sys pages\n",
1142b037c29aSEli Cohen 		    MLX5_CAP_GEN(dev->mdev, uar_4k) ? "yes" : "no",
1143b037c29aSEli Cohen 		    lib_uar_4k ? "yes" : "no", ref_bfregs,
1144b037c29aSEli Cohen 		    req->total_num_bfregs, *num_sys_pages);
1145b037c29aSEli Cohen 
1146b037c29aSEli Cohen 	return 0;
1147b037c29aSEli Cohen }
1148b037c29aSEli Cohen 
1149b037c29aSEli Cohen static int allocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
1150b037c29aSEli Cohen {
1151b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1152b037c29aSEli Cohen 	int err;
1153b037c29aSEli Cohen 	int i;
1154b037c29aSEli Cohen 
1155b037c29aSEli Cohen 	bfregi = &context->bfregi;
1156b037c29aSEli Cohen 	for (i = 0; i < bfregi->num_sys_pages; i++) {
1157b037c29aSEli Cohen 		err = mlx5_cmd_alloc_uar(dev->mdev, &bfregi->sys_pages[i]);
1158b037c29aSEli Cohen 		if (err)
1159b037c29aSEli Cohen 			goto error;
1160b037c29aSEli Cohen 
1161b037c29aSEli Cohen 		mlx5_ib_dbg(dev, "allocated uar %d\n", bfregi->sys_pages[i]);
1162b037c29aSEli Cohen 	}
1163b037c29aSEli Cohen 	return 0;
1164b037c29aSEli Cohen 
1165b037c29aSEli Cohen error:
1166b037c29aSEli Cohen 	for (--i; i >= 0; i--)
1167b037c29aSEli Cohen 		if (mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]))
1168b037c29aSEli Cohen 			mlx5_ib_warn(dev, "failed to free uar %d\n", i);
1169b037c29aSEli Cohen 
1170b037c29aSEli Cohen 	return err;
1171b037c29aSEli Cohen }
1172b037c29aSEli Cohen 
1173b037c29aSEli Cohen static int deallocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
1174b037c29aSEli Cohen {
1175b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1176b037c29aSEli Cohen 	int err;
1177b037c29aSEli Cohen 	int i;
1178b037c29aSEli Cohen 
1179b037c29aSEli Cohen 	bfregi = &context->bfregi;
1180b037c29aSEli Cohen 	for (i = 0; i < bfregi->num_sys_pages; i++) {
1181b037c29aSEli Cohen 		err = mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]);
1182b037c29aSEli Cohen 		if (err) {
1183b037c29aSEli Cohen 			mlx5_ib_warn(dev, "failed to free uar %d\n", i);
1184b037c29aSEli Cohen 			return err;
1185b037c29aSEli Cohen 		}
1186b037c29aSEli Cohen 	}
1187b037c29aSEli Cohen 	return 0;
1188b037c29aSEli Cohen }
1189b037c29aSEli Cohen 
1190e126ba97SEli Cohen static struct ib_ucontext *mlx5_ib_alloc_ucontext(struct ib_device *ibdev,
1191e126ba97SEli Cohen 						  struct ib_udata *udata)
1192e126ba97SEli Cohen {
1193e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1194b368d7cbSMatan Barak 	struct mlx5_ib_alloc_ucontext_req_v2 req = {};
1195b368d7cbSMatan Barak 	struct mlx5_ib_alloc_ucontext_resp resp = {};
1196e126ba97SEli Cohen 	struct mlx5_ib_ucontext *context;
11972f5ff264SEli Cohen 	struct mlx5_bfreg_info *bfregi;
119878c0f98cSEli Cohen 	int ver;
1199e126ba97SEli Cohen 	int err;
1200f241e749SJack Morgenstein 	size_t reqlen;
1201a168a41cSMajd Dibbiny 	size_t min_req_v2 = offsetof(struct mlx5_ib_alloc_ucontext_req_v2,
1202a168a41cSMajd Dibbiny 				     max_cqe_version);
1203b037c29aSEli Cohen 	bool lib_uar_4k;
1204e126ba97SEli Cohen 
1205e126ba97SEli Cohen 	if (!dev->ib_active)
1206e126ba97SEli Cohen 		return ERR_PTR(-EAGAIN);
1207e126ba97SEli Cohen 
1208dfbee859SHaggai Abramovsky 	if (udata->inlen < sizeof(struct ib_uverbs_cmd_hdr))
1209dfbee859SHaggai Abramovsky 		return ERR_PTR(-EINVAL);
1210dfbee859SHaggai Abramovsky 
121178c0f98cSEli Cohen 	reqlen = udata->inlen - sizeof(struct ib_uverbs_cmd_hdr);
121278c0f98cSEli Cohen 	if (reqlen == sizeof(struct mlx5_ib_alloc_ucontext_req))
121378c0f98cSEli Cohen 		ver = 0;
1214a168a41cSMajd Dibbiny 	else if (reqlen >= min_req_v2)
121578c0f98cSEli Cohen 		ver = 2;
121678c0f98cSEli Cohen 	else
121778c0f98cSEli Cohen 		return ERR_PTR(-EINVAL);
121878c0f98cSEli Cohen 
1219b368d7cbSMatan Barak 	err = ib_copy_from_udata(&req, udata, min(reqlen, sizeof(req)));
1220e126ba97SEli Cohen 	if (err)
1221e126ba97SEli Cohen 		return ERR_PTR(err);
1222e126ba97SEli Cohen 
1223b368d7cbSMatan Barak 	if (req.flags)
122478c0f98cSEli Cohen 		return ERR_PTR(-EINVAL);
122578c0f98cSEli Cohen 
1226f72300c5SHaggai Abramovsky 	if (req.comp_mask || req.reserved0 || req.reserved1 || req.reserved2)
1227b368d7cbSMatan Barak 		return ERR_PTR(-EOPNOTSUPP);
1228b368d7cbSMatan Barak 
12292f5ff264SEli Cohen 	req.total_num_bfregs = ALIGN(req.total_num_bfregs,
12302f5ff264SEli Cohen 				    MLX5_NON_FP_BFREGS_PER_UAR);
12312f5ff264SEli Cohen 	if (req.num_low_latency_bfregs > req.total_num_bfregs - 1)
1232e126ba97SEli Cohen 		return ERR_PTR(-EINVAL);
1233e126ba97SEli Cohen 
1234938fe83cSSaeed Mahameed 	resp.qp_tab_size = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp);
12352cc6ad5fSNoa Osherovich 	if (mlx5_core_is_pf(dev->mdev) && MLX5_CAP_GEN(dev->mdev, bf))
1236938fe83cSSaeed Mahameed 		resp.bf_reg_size = 1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size);
1237b47bd6eaSDaniel Jurgens 	resp.cache_line_size = cache_line_size();
1238938fe83cSSaeed Mahameed 	resp.max_sq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq);
1239938fe83cSSaeed Mahameed 	resp.max_rq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq);
1240938fe83cSSaeed Mahameed 	resp.max_send_wqebb = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1241938fe83cSSaeed Mahameed 	resp.max_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1242938fe83cSSaeed Mahameed 	resp.max_srq_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz);
1243f72300c5SHaggai Abramovsky 	resp.cqe_version = min_t(__u8,
1244f72300c5SHaggai Abramovsky 				 (__u8)MLX5_CAP_GEN(dev->mdev, cqe_version),
1245f72300c5SHaggai Abramovsky 				 req.max_cqe_version);
124630aa60b3SEli Cohen 	resp.log_uar_size = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
124730aa60b3SEli Cohen 				MLX5_ADAPTER_PAGE_SHIFT : PAGE_SHIFT;
124830aa60b3SEli Cohen 	resp.num_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
124930aa60b3SEli Cohen 					MLX5_CAP_GEN(dev->mdev, num_of_uars_per_page) : 1;
1250b368d7cbSMatan Barak 	resp.response_length = min(offsetof(typeof(resp), response_length) +
1251b368d7cbSMatan Barak 				   sizeof(resp.response_length), udata->outlen);
1252e126ba97SEli Cohen 
1253e126ba97SEli Cohen 	context = kzalloc(sizeof(*context), GFP_KERNEL);
1254e126ba97SEli Cohen 	if (!context)
1255e126ba97SEli Cohen 		return ERR_PTR(-ENOMEM);
1256e126ba97SEli Cohen 
125730aa60b3SEli Cohen 	lib_uar_4k = req.lib_caps & MLX5_LIB_CAP_4K_UAR;
12582f5ff264SEli Cohen 	bfregi = &context->bfregi;
1259b037c29aSEli Cohen 
1260b037c29aSEli Cohen 	/* updates req->total_num_bfregs */
1261b037c29aSEli Cohen 	err = calc_total_bfregs(dev, lib_uar_4k, &req, &bfregi->num_sys_pages);
1262b037c29aSEli Cohen 	if (err)
1263b037c29aSEli Cohen 		goto out_ctx;
1264b037c29aSEli Cohen 
12652f5ff264SEli Cohen 	mutex_init(&bfregi->lock);
1266b037c29aSEli Cohen 	bfregi->lib_uar_4k = lib_uar_4k;
1267b037c29aSEli Cohen 	bfregi->count = kcalloc(req.total_num_bfregs, sizeof(*bfregi->count),
1268b037c29aSEli Cohen 				GFP_KERNEL);
1269b037c29aSEli Cohen 	if (!bfregi->count) {
1270e126ba97SEli Cohen 		err = -ENOMEM;
1271e126ba97SEli Cohen 		goto out_ctx;
1272e126ba97SEli Cohen 	}
1273e126ba97SEli Cohen 
1274b037c29aSEli Cohen 	bfregi->sys_pages = kcalloc(bfregi->num_sys_pages,
1275b037c29aSEli Cohen 				    sizeof(*bfregi->sys_pages),
1276e126ba97SEli Cohen 				    GFP_KERNEL);
1277b037c29aSEli Cohen 	if (!bfregi->sys_pages) {
1278e126ba97SEli Cohen 		err = -ENOMEM;
1279e126ba97SEli Cohen 		goto out_count;
1280e126ba97SEli Cohen 	}
1281e126ba97SEli Cohen 
1282b037c29aSEli Cohen 	err = allocate_uars(dev, context);
1283b037c29aSEli Cohen 	if (err)
1284b037c29aSEli Cohen 		goto out_sys_pages;
1285b037c29aSEli Cohen 
1286b4cfe447SHaggai Eran #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
1287b4cfe447SHaggai Eran 	context->ibucontext.invalidate_range = &mlx5_ib_invalidate_range;
1288b4cfe447SHaggai Eran #endif
1289b4cfe447SHaggai Eran 
12907d0cc6edSArtemy Kovalyov 	context->upd_xlt_page = __get_free_page(GFP_KERNEL);
12917d0cc6edSArtemy Kovalyov 	if (!context->upd_xlt_page) {
12927d0cc6edSArtemy Kovalyov 		err = -ENOMEM;
12937d0cc6edSArtemy Kovalyov 		goto out_uars;
12947d0cc6edSArtemy Kovalyov 	}
12957d0cc6edSArtemy Kovalyov 	mutex_init(&context->upd_xlt_page_mutex);
12967d0cc6edSArtemy Kovalyov 
1297146d2f1aSmajd@mellanox.com 	if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain)) {
1298146d2f1aSmajd@mellanox.com 		err = mlx5_core_alloc_transport_domain(dev->mdev,
1299146d2f1aSmajd@mellanox.com 						       &context->tdn);
1300146d2f1aSmajd@mellanox.com 		if (err)
13017d0cc6edSArtemy Kovalyov 			goto out_page;
1302146d2f1aSmajd@mellanox.com 	}
1303146d2f1aSmajd@mellanox.com 
13047c2344c3SMaor Gottlieb 	INIT_LIST_HEAD(&context->vma_private_list);
1305e126ba97SEli Cohen 	INIT_LIST_HEAD(&context->db_page_list);
1306e126ba97SEli Cohen 	mutex_init(&context->db_page_mutex);
1307e126ba97SEli Cohen 
13082f5ff264SEli Cohen 	resp.tot_bfregs = req.total_num_bfregs;
1309938fe83cSSaeed Mahameed 	resp.num_ports = MLX5_CAP_GEN(dev->mdev, num_ports);
1310b368d7cbSMatan Barak 
1311f72300c5SHaggai Abramovsky 	if (field_avail(typeof(resp), cqe_version, udata->outlen))
1312f72300c5SHaggai Abramovsky 		resp.response_length += sizeof(resp.cqe_version);
1313b368d7cbSMatan Barak 
1314402ca536SBodong Wang 	if (field_avail(typeof(resp), cmds_supp_uhw, udata->outlen)) {
13156ad279c5SMoni Shoua 		resp.cmds_supp_uhw |= MLX5_USER_CMDS_SUPP_UHW_QUERY_DEVICE |
13166ad279c5SMoni Shoua 				      MLX5_USER_CMDS_SUPP_UHW_CREATE_AH;
1317402ca536SBodong Wang 		resp.response_length += sizeof(resp.cmds_supp_uhw);
1318402ca536SBodong Wang 	}
1319402ca536SBodong Wang 
132078984898SOr Gerlitz 	if (field_avail(typeof(resp), eth_min_inline, udata->outlen)) {
132178984898SOr Gerlitz 		if (mlx5_ib_port_link_layer(ibdev, 1) == IB_LINK_LAYER_ETHERNET) {
132278984898SOr Gerlitz 			mlx5_query_min_inline(dev->mdev, &resp.eth_min_inline);
132378984898SOr Gerlitz 			resp.eth_min_inline++;
132478984898SOr Gerlitz 		}
132578984898SOr Gerlitz 		resp.response_length += sizeof(resp.eth_min_inline);
132678984898SOr Gerlitz 	}
132778984898SOr Gerlitz 
1328bc5c6eedSNoa Osherovich 	/*
1329bc5c6eedSNoa Osherovich 	 * We don't want to expose information from the PCI bar that is located
1330bc5c6eedSNoa Osherovich 	 * after 4096 bytes, so if the arch only supports larger pages, let's
1331bc5c6eedSNoa Osherovich 	 * pretend we don't support reading the HCA's core clock. This is also
1332bc5c6eedSNoa Osherovich 	 * forced by mmap function.
1333bc5c6eedSNoa Osherovich 	 */
1334de8d6e02SEli Cohen 	if (field_avail(typeof(resp), hca_core_clock_offset, udata->outlen)) {
1335de8d6e02SEli Cohen 		if (PAGE_SIZE <= 4096) {
1336b368d7cbSMatan Barak 			resp.comp_mask |=
1337b368d7cbSMatan Barak 				MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_CORE_CLOCK_OFFSET;
1338b368d7cbSMatan Barak 			resp.hca_core_clock_offset =
1339de8d6e02SEli Cohen 				offsetof(struct mlx5_init_seg, internal_timer_h) % PAGE_SIZE;
1340de8d6e02SEli Cohen 		}
1341f72300c5SHaggai Abramovsky 		resp.response_length += sizeof(resp.hca_core_clock_offset) +
1342402ca536SBodong Wang 					sizeof(resp.reserved2);
1343b368d7cbSMatan Barak 	}
1344b368d7cbSMatan Barak 
134530aa60b3SEli Cohen 	if (field_avail(typeof(resp), log_uar_size, udata->outlen))
134630aa60b3SEli Cohen 		resp.response_length += sizeof(resp.log_uar_size);
134730aa60b3SEli Cohen 
134830aa60b3SEli Cohen 	if (field_avail(typeof(resp), num_uars_per_page, udata->outlen))
134930aa60b3SEli Cohen 		resp.response_length += sizeof(resp.num_uars_per_page);
135030aa60b3SEli Cohen 
1351b368d7cbSMatan Barak 	err = ib_copy_to_udata(udata, &resp, resp.response_length);
1352e126ba97SEli Cohen 	if (err)
1353146d2f1aSmajd@mellanox.com 		goto out_td;
1354e126ba97SEli Cohen 
13552f5ff264SEli Cohen 	bfregi->ver = ver;
13562f5ff264SEli Cohen 	bfregi->num_low_latency_bfregs = req.num_low_latency_bfregs;
1357f72300c5SHaggai Abramovsky 	context->cqe_version = resp.cqe_version;
135830aa60b3SEli Cohen 	context->lib_caps = req.lib_caps;
135930aa60b3SEli Cohen 	print_lib_caps(dev, context->lib_caps);
1360f72300c5SHaggai Abramovsky 
1361e126ba97SEli Cohen 	return &context->ibucontext;
1362e126ba97SEli Cohen 
1363146d2f1aSmajd@mellanox.com out_td:
1364146d2f1aSmajd@mellanox.com 	if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1365146d2f1aSmajd@mellanox.com 		mlx5_core_dealloc_transport_domain(dev->mdev, context->tdn);
1366146d2f1aSmajd@mellanox.com 
13677d0cc6edSArtemy Kovalyov out_page:
13687d0cc6edSArtemy Kovalyov 	free_page(context->upd_xlt_page);
13697d0cc6edSArtemy Kovalyov 
1370e126ba97SEli Cohen out_uars:
1371b037c29aSEli Cohen 	deallocate_uars(dev, context);
1372b037c29aSEli Cohen 
1373b037c29aSEli Cohen out_sys_pages:
1374b037c29aSEli Cohen 	kfree(bfregi->sys_pages);
1375b037c29aSEli Cohen 
1376e126ba97SEli Cohen out_count:
13772f5ff264SEli Cohen 	kfree(bfregi->count);
1378e126ba97SEli Cohen 
1379e126ba97SEli Cohen out_ctx:
1380e126ba97SEli Cohen 	kfree(context);
1381b037c29aSEli Cohen 
1382e126ba97SEli Cohen 	return ERR_PTR(err);
1383e126ba97SEli Cohen }
1384e126ba97SEli Cohen 
1385e126ba97SEli Cohen static int mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
1386e126ba97SEli Cohen {
1387e126ba97SEli Cohen 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1388e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
1389b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1390e126ba97SEli Cohen 
1391b037c29aSEli Cohen 	bfregi = &context->bfregi;
1392146d2f1aSmajd@mellanox.com 	if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1393146d2f1aSmajd@mellanox.com 		mlx5_core_dealloc_transport_domain(dev->mdev, context->tdn);
1394146d2f1aSmajd@mellanox.com 
13957d0cc6edSArtemy Kovalyov 	free_page(context->upd_xlt_page);
1396b037c29aSEli Cohen 	deallocate_uars(dev, context);
1397b037c29aSEli Cohen 	kfree(bfregi->sys_pages);
13982f5ff264SEli Cohen 	kfree(bfregi->count);
1399e126ba97SEli Cohen 	kfree(context);
1400e126ba97SEli Cohen 
1401e126ba97SEli Cohen 	return 0;
1402e126ba97SEli Cohen }
1403e126ba97SEli Cohen 
1404b037c29aSEli Cohen static phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev,
1405b037c29aSEli Cohen 				 struct mlx5_bfreg_info *bfregi,
1406b037c29aSEli Cohen 				 int idx)
1407e126ba97SEli Cohen {
1408b037c29aSEli Cohen 	int fw_uars_per_page;
1409b037c29aSEli Cohen 
1410b037c29aSEli Cohen 	fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ? MLX5_UARS_IN_PAGE : 1;
1411b037c29aSEli Cohen 
1412b037c29aSEli Cohen 	return (pci_resource_start(dev->mdev->pdev, 0) >> PAGE_SHIFT) +
1413b037c29aSEli Cohen 			bfregi->sys_pages[idx] / fw_uars_per_page;
1414e126ba97SEli Cohen }
1415e126ba97SEli Cohen 
1416e126ba97SEli Cohen static int get_command(unsigned long offset)
1417e126ba97SEli Cohen {
1418e126ba97SEli Cohen 	return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK;
1419e126ba97SEli Cohen }
1420e126ba97SEli Cohen 
1421e126ba97SEli Cohen static int get_arg(unsigned long offset)
1422e126ba97SEli Cohen {
1423e126ba97SEli Cohen 	return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1);
1424e126ba97SEli Cohen }
1425e126ba97SEli Cohen 
1426e126ba97SEli Cohen static int get_index(unsigned long offset)
1427e126ba97SEli Cohen {
1428e126ba97SEli Cohen 	return get_arg(offset);
1429e126ba97SEli Cohen }
1430e126ba97SEli Cohen 
14317c2344c3SMaor Gottlieb static void  mlx5_ib_vma_open(struct vm_area_struct *area)
14327c2344c3SMaor Gottlieb {
14337c2344c3SMaor Gottlieb 	/* vma_open is called when a new VMA is created on top of our VMA.  This
14347c2344c3SMaor Gottlieb 	 * is done through either mremap flow or split_vma (usually due to
14357c2344c3SMaor Gottlieb 	 * mlock, madvise, munmap, etc.) We do not support a clone of the VMA,
14367c2344c3SMaor Gottlieb 	 * as this VMA is strongly hardware related.  Therefore we set the
14377c2344c3SMaor Gottlieb 	 * vm_ops of the newly created/cloned VMA to NULL, to prevent it from
14387c2344c3SMaor Gottlieb 	 * calling us again and trying to do incorrect actions.  We assume that
14397c2344c3SMaor Gottlieb 	 * the original VMA size is exactly a single page, and therefore all
14407c2344c3SMaor Gottlieb 	 * "splitting" operation will not happen to it.
14417c2344c3SMaor Gottlieb 	 */
14427c2344c3SMaor Gottlieb 	area->vm_ops = NULL;
14437c2344c3SMaor Gottlieb }
14447c2344c3SMaor Gottlieb 
14457c2344c3SMaor Gottlieb static void  mlx5_ib_vma_close(struct vm_area_struct *area)
14467c2344c3SMaor Gottlieb {
14477c2344c3SMaor Gottlieb 	struct mlx5_ib_vma_private_data *mlx5_ib_vma_priv_data;
14487c2344c3SMaor Gottlieb 
14497c2344c3SMaor Gottlieb 	/* It's guaranteed that all VMAs opened on a FD are closed before the
14507c2344c3SMaor Gottlieb 	 * file itself is closed, therefore no sync is needed with the regular
14517c2344c3SMaor Gottlieb 	 * closing flow. (e.g. mlx5 ib_dealloc_ucontext)
14527c2344c3SMaor Gottlieb 	 * However need a sync with accessing the vma as part of
14537c2344c3SMaor Gottlieb 	 * mlx5_ib_disassociate_ucontext.
14547c2344c3SMaor Gottlieb 	 * The close operation is usually called under mm->mmap_sem except when
14557c2344c3SMaor Gottlieb 	 * process is exiting.
14567c2344c3SMaor Gottlieb 	 * The exiting case is handled explicitly as part of
14577c2344c3SMaor Gottlieb 	 * mlx5_ib_disassociate_ucontext.
14587c2344c3SMaor Gottlieb 	 */
14597c2344c3SMaor Gottlieb 	mlx5_ib_vma_priv_data = (struct mlx5_ib_vma_private_data *)area->vm_private_data;
14607c2344c3SMaor Gottlieb 
14617c2344c3SMaor Gottlieb 	/* setting the vma context pointer to null in the mlx5_ib driver's
14627c2344c3SMaor Gottlieb 	 * private data, to protect a race condition in
14637c2344c3SMaor Gottlieb 	 * mlx5_ib_disassociate_ucontext().
14647c2344c3SMaor Gottlieb 	 */
14657c2344c3SMaor Gottlieb 	mlx5_ib_vma_priv_data->vma = NULL;
14667c2344c3SMaor Gottlieb 	list_del(&mlx5_ib_vma_priv_data->list);
14677c2344c3SMaor Gottlieb 	kfree(mlx5_ib_vma_priv_data);
14687c2344c3SMaor Gottlieb }
14697c2344c3SMaor Gottlieb 
14707c2344c3SMaor Gottlieb static const struct vm_operations_struct mlx5_ib_vm_ops = {
14717c2344c3SMaor Gottlieb 	.open = mlx5_ib_vma_open,
14727c2344c3SMaor Gottlieb 	.close = mlx5_ib_vma_close
14737c2344c3SMaor Gottlieb };
14747c2344c3SMaor Gottlieb 
14757c2344c3SMaor Gottlieb static int mlx5_ib_set_vma_data(struct vm_area_struct *vma,
14767c2344c3SMaor Gottlieb 				struct mlx5_ib_ucontext *ctx)
14777c2344c3SMaor Gottlieb {
14787c2344c3SMaor Gottlieb 	struct mlx5_ib_vma_private_data *vma_prv;
14797c2344c3SMaor Gottlieb 	struct list_head *vma_head = &ctx->vma_private_list;
14807c2344c3SMaor Gottlieb 
14817c2344c3SMaor Gottlieb 	vma_prv = kzalloc(sizeof(*vma_prv), GFP_KERNEL);
14827c2344c3SMaor Gottlieb 	if (!vma_prv)
14837c2344c3SMaor Gottlieb 		return -ENOMEM;
14847c2344c3SMaor Gottlieb 
14857c2344c3SMaor Gottlieb 	vma_prv->vma = vma;
14867c2344c3SMaor Gottlieb 	vma->vm_private_data = vma_prv;
14877c2344c3SMaor Gottlieb 	vma->vm_ops =  &mlx5_ib_vm_ops;
14887c2344c3SMaor Gottlieb 
14897c2344c3SMaor Gottlieb 	list_add(&vma_prv->list, vma_head);
14907c2344c3SMaor Gottlieb 
14917c2344c3SMaor Gottlieb 	return 0;
14927c2344c3SMaor Gottlieb }
14937c2344c3SMaor Gottlieb 
14947c2344c3SMaor Gottlieb static void mlx5_ib_disassociate_ucontext(struct ib_ucontext *ibcontext)
14957c2344c3SMaor Gottlieb {
14967c2344c3SMaor Gottlieb 	int ret;
14977c2344c3SMaor Gottlieb 	struct vm_area_struct *vma;
14987c2344c3SMaor Gottlieb 	struct mlx5_ib_vma_private_data *vma_private, *n;
14997c2344c3SMaor Gottlieb 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
15007c2344c3SMaor Gottlieb 	struct task_struct *owning_process  = NULL;
15017c2344c3SMaor Gottlieb 	struct mm_struct   *owning_mm       = NULL;
15027c2344c3SMaor Gottlieb 
15037c2344c3SMaor Gottlieb 	owning_process = get_pid_task(ibcontext->tgid, PIDTYPE_PID);
15047c2344c3SMaor Gottlieb 	if (!owning_process)
15057c2344c3SMaor Gottlieb 		return;
15067c2344c3SMaor Gottlieb 
15077c2344c3SMaor Gottlieb 	owning_mm = get_task_mm(owning_process);
15087c2344c3SMaor Gottlieb 	if (!owning_mm) {
15097c2344c3SMaor Gottlieb 		pr_info("no mm, disassociate ucontext is pending task termination\n");
15107c2344c3SMaor Gottlieb 		while (1) {
15117c2344c3SMaor Gottlieb 			put_task_struct(owning_process);
15127c2344c3SMaor Gottlieb 			usleep_range(1000, 2000);
15137c2344c3SMaor Gottlieb 			owning_process = get_pid_task(ibcontext->tgid,
15147c2344c3SMaor Gottlieb 						      PIDTYPE_PID);
15157c2344c3SMaor Gottlieb 			if (!owning_process ||
15167c2344c3SMaor Gottlieb 			    owning_process->state == TASK_DEAD) {
15177c2344c3SMaor Gottlieb 				pr_info("disassociate ucontext done, task was terminated\n");
15187c2344c3SMaor Gottlieb 				/* in case task was dead need to release the
15197c2344c3SMaor Gottlieb 				 * task struct.
15207c2344c3SMaor Gottlieb 				 */
15217c2344c3SMaor Gottlieb 				if (owning_process)
15227c2344c3SMaor Gottlieb 					put_task_struct(owning_process);
15237c2344c3SMaor Gottlieb 				return;
15247c2344c3SMaor Gottlieb 			}
15257c2344c3SMaor Gottlieb 		}
15267c2344c3SMaor Gottlieb 	}
15277c2344c3SMaor Gottlieb 
15287c2344c3SMaor Gottlieb 	/* need to protect from a race on closing the vma as part of
15297c2344c3SMaor Gottlieb 	 * mlx5_ib_vma_close.
15307c2344c3SMaor Gottlieb 	 */
1531ecc7d83bSMaor Gottlieb 	down_write(&owning_mm->mmap_sem);
15327c2344c3SMaor Gottlieb 	list_for_each_entry_safe(vma_private, n, &context->vma_private_list,
15337c2344c3SMaor Gottlieb 				 list) {
15347c2344c3SMaor Gottlieb 		vma = vma_private->vma;
15357c2344c3SMaor Gottlieb 		ret = zap_vma_ptes(vma, vma->vm_start,
15367c2344c3SMaor Gottlieb 				   PAGE_SIZE);
15377c2344c3SMaor Gottlieb 		WARN_ONCE(ret, "%s: zap_vma_ptes failed", __func__);
15387c2344c3SMaor Gottlieb 		/* context going to be destroyed, should
15397c2344c3SMaor Gottlieb 		 * not access ops any more.
15407c2344c3SMaor Gottlieb 		 */
154113776612SMaor Gottlieb 		vma->vm_flags &= ~(VM_SHARED | VM_MAYSHARE);
15427c2344c3SMaor Gottlieb 		vma->vm_ops = NULL;
15437c2344c3SMaor Gottlieb 		list_del(&vma_private->list);
15447c2344c3SMaor Gottlieb 		kfree(vma_private);
15457c2344c3SMaor Gottlieb 	}
1546ecc7d83bSMaor Gottlieb 	up_write(&owning_mm->mmap_sem);
15477c2344c3SMaor Gottlieb 	mmput(owning_mm);
15487c2344c3SMaor Gottlieb 	put_task_struct(owning_process);
15497c2344c3SMaor Gottlieb }
15507c2344c3SMaor Gottlieb 
155137aa5c36SGuy Levi static inline char *mmap_cmd2str(enum mlx5_ib_mmap_cmd cmd)
1552e126ba97SEli Cohen {
155337aa5c36SGuy Levi 	switch (cmd) {
155437aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
155537aa5c36SGuy Levi 		return "WC";
1556e126ba97SEli Cohen 	case MLX5_IB_MMAP_REGULAR_PAGE:
155737aa5c36SGuy Levi 		return "best effort WC";
155837aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
155937aa5c36SGuy Levi 		return "NC";
156037aa5c36SGuy Levi 	default:
156137aa5c36SGuy Levi 		return NULL;
156237aa5c36SGuy Levi 	}
156337aa5c36SGuy Levi }
156437aa5c36SGuy Levi 
156537aa5c36SGuy Levi static int uar_mmap(struct mlx5_ib_dev *dev, enum mlx5_ib_mmap_cmd cmd,
15667c2344c3SMaor Gottlieb 		    struct vm_area_struct *vma,
15677c2344c3SMaor Gottlieb 		    struct mlx5_ib_ucontext *context)
156837aa5c36SGuy Levi {
15692f5ff264SEli Cohen 	struct mlx5_bfreg_info *bfregi = &context->bfregi;
157037aa5c36SGuy Levi 	int err;
157137aa5c36SGuy Levi 	unsigned long idx;
157237aa5c36SGuy Levi 	phys_addr_t pfn, pa;
157337aa5c36SGuy Levi 	pgprot_t prot;
1574b037c29aSEli Cohen 	int uars_per_page;
1575b037c29aSEli Cohen 
1576b037c29aSEli Cohen 	if (vma->vm_end - vma->vm_start != PAGE_SIZE)
1577b037c29aSEli Cohen 		return -EINVAL;
1578b037c29aSEli Cohen 
1579b037c29aSEli Cohen 	uars_per_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k);
1580b037c29aSEli Cohen 	idx = get_index(vma->vm_pgoff);
1581b037c29aSEli Cohen 	if (idx % uars_per_page ||
1582b037c29aSEli Cohen 	    idx * uars_per_page >= bfregi->num_sys_pages) {
1583b037c29aSEli Cohen 		mlx5_ib_warn(dev, "invalid uar index %lu\n", idx);
1584b037c29aSEli Cohen 		return -EINVAL;
1585b037c29aSEli Cohen 	}
158637aa5c36SGuy Levi 
158737aa5c36SGuy Levi 	switch (cmd) {
158837aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
158937aa5c36SGuy Levi /* Some architectures don't support WC memory */
159037aa5c36SGuy Levi #if defined(CONFIG_X86)
159137aa5c36SGuy Levi 		if (!pat_enabled())
159237aa5c36SGuy Levi 			return -EPERM;
159337aa5c36SGuy Levi #elif !(defined(CONFIG_PPC) || (defined(CONFIG_ARM) && defined(CONFIG_MMU)))
159437aa5c36SGuy Levi 			return -EPERM;
159537aa5c36SGuy Levi #endif
159637aa5c36SGuy Levi 	/* fall through */
159737aa5c36SGuy Levi 	case MLX5_IB_MMAP_REGULAR_PAGE:
159837aa5c36SGuy Levi 		/* For MLX5_IB_MMAP_REGULAR_PAGE do the best effort to get WC */
159937aa5c36SGuy Levi 		prot = pgprot_writecombine(vma->vm_page_prot);
160037aa5c36SGuy Levi 		break;
160137aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
160237aa5c36SGuy Levi 		prot = pgprot_noncached(vma->vm_page_prot);
160337aa5c36SGuy Levi 		break;
160437aa5c36SGuy Levi 	default:
160537aa5c36SGuy Levi 		return -EINVAL;
160637aa5c36SGuy Levi 	}
160737aa5c36SGuy Levi 
1608b037c29aSEli Cohen 	pfn = uar_index2pfn(dev, bfregi, idx);
160937aa5c36SGuy Levi 	mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn %pa\n", idx, &pfn);
1610e126ba97SEli Cohen 
161137aa5c36SGuy Levi 	vma->vm_page_prot = prot;
161237aa5c36SGuy Levi 	err = io_remap_pfn_range(vma, vma->vm_start, pfn,
161337aa5c36SGuy Levi 				 PAGE_SIZE, vma->vm_page_prot);
161437aa5c36SGuy Levi 	if (err) {
161537aa5c36SGuy Levi 		mlx5_ib_err(dev, "io_remap_pfn_range failed with error=%d, vm_start=0x%lx, pfn=%pa, mmap_cmd=%s\n",
161637aa5c36SGuy Levi 			    err, vma->vm_start, &pfn, mmap_cmd2str(cmd));
1617e126ba97SEli Cohen 		return -EAGAIN;
161837aa5c36SGuy Levi 	}
1619e126ba97SEli Cohen 
162037aa5c36SGuy Levi 	pa = pfn << PAGE_SHIFT;
162137aa5c36SGuy Levi 	mlx5_ib_dbg(dev, "mapped %s at 0x%lx, PA %pa\n", mmap_cmd2str(cmd),
162237aa5c36SGuy Levi 		    vma->vm_start, &pa);
162337aa5c36SGuy Levi 
16247c2344c3SMaor Gottlieb 	return mlx5_ib_set_vma_data(vma, context);
162537aa5c36SGuy Levi }
162637aa5c36SGuy Levi 
162737aa5c36SGuy Levi static int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma)
162837aa5c36SGuy Levi {
162937aa5c36SGuy Levi 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
163037aa5c36SGuy Levi 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
163137aa5c36SGuy Levi 	unsigned long command;
163237aa5c36SGuy Levi 	phys_addr_t pfn;
163337aa5c36SGuy Levi 
163437aa5c36SGuy Levi 	command = get_command(vma->vm_pgoff);
163537aa5c36SGuy Levi 	switch (command) {
163637aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
163737aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
163837aa5c36SGuy Levi 	case MLX5_IB_MMAP_REGULAR_PAGE:
16397c2344c3SMaor Gottlieb 		return uar_mmap(dev, command, vma, context);
1640e126ba97SEli Cohen 
1641e126ba97SEli Cohen 	case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES:
1642e126ba97SEli Cohen 		return -ENOSYS;
1643e126ba97SEli Cohen 
1644d69e3bcfSMatan Barak 	case MLX5_IB_MMAP_CORE_CLOCK:
1645d69e3bcfSMatan Barak 		if (vma->vm_end - vma->vm_start != PAGE_SIZE)
1646d69e3bcfSMatan Barak 			return -EINVAL;
1647d69e3bcfSMatan Barak 
16486cbac1e4SMatan Barak 		if (vma->vm_flags & VM_WRITE)
1649d69e3bcfSMatan Barak 			return -EPERM;
1650d69e3bcfSMatan Barak 
1651d69e3bcfSMatan Barak 		/* Don't expose to user-space information it shouldn't have */
1652d69e3bcfSMatan Barak 		if (PAGE_SIZE > 4096)
1653d69e3bcfSMatan Barak 			return -EOPNOTSUPP;
1654d69e3bcfSMatan Barak 
1655d69e3bcfSMatan Barak 		vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1656d69e3bcfSMatan Barak 		pfn = (dev->mdev->iseg_base +
1657d69e3bcfSMatan Barak 		       offsetof(struct mlx5_init_seg, internal_timer_h)) >>
1658d69e3bcfSMatan Barak 			PAGE_SHIFT;
1659d69e3bcfSMatan Barak 		if (io_remap_pfn_range(vma, vma->vm_start, pfn,
1660d69e3bcfSMatan Barak 				       PAGE_SIZE, vma->vm_page_prot))
1661d69e3bcfSMatan Barak 			return -EAGAIN;
1662d69e3bcfSMatan Barak 
1663d69e3bcfSMatan Barak 		mlx5_ib_dbg(dev, "mapped internal timer at 0x%lx, PA 0x%llx\n",
1664d69e3bcfSMatan Barak 			    vma->vm_start,
1665d69e3bcfSMatan Barak 			    (unsigned long long)pfn << PAGE_SHIFT);
1666d69e3bcfSMatan Barak 		break;
1667d69e3bcfSMatan Barak 
1668e126ba97SEli Cohen 	default:
1669e126ba97SEli Cohen 		return -EINVAL;
1670e126ba97SEli Cohen 	}
1671e126ba97SEli Cohen 
1672e126ba97SEli Cohen 	return 0;
1673e126ba97SEli Cohen }
1674e126ba97SEli Cohen 
1675e126ba97SEli Cohen static struct ib_pd *mlx5_ib_alloc_pd(struct ib_device *ibdev,
1676e126ba97SEli Cohen 				      struct ib_ucontext *context,
1677e126ba97SEli Cohen 				      struct ib_udata *udata)
1678e126ba97SEli Cohen {
1679e126ba97SEli Cohen 	struct mlx5_ib_alloc_pd_resp resp;
1680e126ba97SEli Cohen 	struct mlx5_ib_pd *pd;
1681e126ba97SEli Cohen 	int err;
1682e126ba97SEli Cohen 
1683e126ba97SEli Cohen 	pd = kmalloc(sizeof(*pd), GFP_KERNEL);
1684e126ba97SEli Cohen 	if (!pd)
1685e126ba97SEli Cohen 		return ERR_PTR(-ENOMEM);
1686e126ba97SEli Cohen 
16879603b61dSJack Morgenstein 	err = mlx5_core_alloc_pd(to_mdev(ibdev)->mdev, &pd->pdn);
1688e126ba97SEli Cohen 	if (err) {
1689e126ba97SEli Cohen 		kfree(pd);
1690e126ba97SEli Cohen 		return ERR_PTR(err);
1691e126ba97SEli Cohen 	}
1692e126ba97SEli Cohen 
1693e126ba97SEli Cohen 	if (context) {
1694e126ba97SEli Cohen 		resp.pdn = pd->pdn;
1695e126ba97SEli Cohen 		if (ib_copy_to_udata(udata, &resp, sizeof(resp))) {
16969603b61dSJack Morgenstein 			mlx5_core_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn);
1697e126ba97SEli Cohen 			kfree(pd);
1698e126ba97SEli Cohen 			return ERR_PTR(-EFAULT);
1699e126ba97SEli Cohen 		}
1700e126ba97SEli Cohen 	}
1701e126ba97SEli Cohen 
1702e126ba97SEli Cohen 	return &pd->ibpd;
1703e126ba97SEli Cohen }
1704e126ba97SEli Cohen 
1705e126ba97SEli Cohen static int mlx5_ib_dealloc_pd(struct ib_pd *pd)
1706e126ba97SEli Cohen {
1707e126ba97SEli Cohen 	struct mlx5_ib_dev *mdev = to_mdev(pd->device);
1708e126ba97SEli Cohen 	struct mlx5_ib_pd *mpd = to_mpd(pd);
1709e126ba97SEli Cohen 
17109603b61dSJack Morgenstein 	mlx5_core_dealloc_pd(mdev->mdev, mpd->pdn);
1711e126ba97SEli Cohen 	kfree(mpd);
1712e126ba97SEli Cohen 
1713e126ba97SEli Cohen 	return 0;
1714e126ba97SEli Cohen }
1715e126ba97SEli Cohen 
1716466fa6d2SMaor Gottlieb enum {
1717466fa6d2SMaor Gottlieb 	MATCH_CRITERIA_ENABLE_OUTER_BIT,
1718466fa6d2SMaor Gottlieb 	MATCH_CRITERIA_ENABLE_MISC_BIT,
1719466fa6d2SMaor Gottlieb 	MATCH_CRITERIA_ENABLE_INNER_BIT
1720466fa6d2SMaor Gottlieb };
1721038d2ef8SMaor Gottlieb 
1722466fa6d2SMaor Gottlieb #define HEADER_IS_ZERO(match_criteria, headers)			           \
1723466fa6d2SMaor Gottlieb 	!(memchr_inv(MLX5_ADDR_OF(fte_match_param, match_criteria, headers), \
1724466fa6d2SMaor Gottlieb 		    0, MLX5_FLD_SZ_BYTES(fte_match_param, headers)))       \
1725466fa6d2SMaor Gottlieb 
1726466fa6d2SMaor Gottlieb static u8 get_match_criteria_enable(u32 *match_criteria)
1727466fa6d2SMaor Gottlieb {
1728466fa6d2SMaor Gottlieb 	u8 match_criteria_enable;
1729466fa6d2SMaor Gottlieb 
1730466fa6d2SMaor Gottlieb 	match_criteria_enable =
1731466fa6d2SMaor Gottlieb 		(!HEADER_IS_ZERO(match_criteria, outer_headers)) <<
1732466fa6d2SMaor Gottlieb 		MATCH_CRITERIA_ENABLE_OUTER_BIT;
1733466fa6d2SMaor Gottlieb 	match_criteria_enable |=
1734466fa6d2SMaor Gottlieb 		(!HEADER_IS_ZERO(match_criteria, misc_parameters)) <<
1735466fa6d2SMaor Gottlieb 		MATCH_CRITERIA_ENABLE_MISC_BIT;
1736466fa6d2SMaor Gottlieb 	match_criteria_enable |=
1737466fa6d2SMaor Gottlieb 		(!HEADER_IS_ZERO(match_criteria, inner_headers)) <<
1738466fa6d2SMaor Gottlieb 		MATCH_CRITERIA_ENABLE_INNER_BIT;
1739466fa6d2SMaor Gottlieb 
1740466fa6d2SMaor Gottlieb 	return match_criteria_enable;
1741038d2ef8SMaor Gottlieb }
1742038d2ef8SMaor Gottlieb 
1743ca0d4753SMaor Gottlieb static void set_proto(void *outer_c, void *outer_v, u8 mask, u8 val)
1744ca0d4753SMaor Gottlieb {
1745ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_protocol, mask);
1746ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_protocol, val);
1747ca0d4753SMaor Gottlieb }
1748ca0d4753SMaor Gottlieb 
17492d1e697eSMoses Reuben static void set_flow_label(void *misc_c, void *misc_v, u8 mask, u8 val,
17502d1e697eSMoses Reuben 			   bool inner)
17512d1e697eSMoses Reuben {
17522d1e697eSMoses Reuben 	if (inner) {
17532d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
17542d1e697eSMoses Reuben 			 misc_c, inner_ipv6_flow_label, mask);
17552d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
17562d1e697eSMoses Reuben 			 misc_v, inner_ipv6_flow_label, val);
17572d1e697eSMoses Reuben 	} else {
17582d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
17592d1e697eSMoses Reuben 			 misc_c, outer_ipv6_flow_label, mask);
17602d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
17612d1e697eSMoses Reuben 			 misc_v, outer_ipv6_flow_label, val);
17622d1e697eSMoses Reuben 	}
17632d1e697eSMoses Reuben }
17642d1e697eSMoses Reuben 
1765ca0d4753SMaor Gottlieb static void set_tos(void *outer_c, void *outer_v, u8 mask, u8 val)
1766ca0d4753SMaor Gottlieb {
1767ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_ecn, mask);
1768ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_ecn, val);
1769ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_dscp, mask >> 2);
1770ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_dscp, val >> 2);
1771ca0d4753SMaor Gottlieb }
1772ca0d4753SMaor Gottlieb 
1773c47ac6aeSMaor Gottlieb #define LAST_ETH_FIELD vlan_tag
1774c47ac6aeSMaor Gottlieb #define LAST_IB_FIELD sl
1775ca0d4753SMaor Gottlieb #define LAST_IPV4_FIELD tos
1776466fa6d2SMaor Gottlieb #define LAST_IPV6_FIELD traffic_class
1777c47ac6aeSMaor Gottlieb #define LAST_TCP_UDP_FIELD src_port
1778ffb30d8fSMoses Reuben #define LAST_TUNNEL_FIELD tunnel_id
17792ac693f9SMoses Reuben #define LAST_FLOW_TAG_FIELD tag_id
1780a22ed86cSSlava Shwartsman #define LAST_DROP_FIELD size
1781c47ac6aeSMaor Gottlieb 
1782c47ac6aeSMaor Gottlieb /* Field is the last supported field */
1783c47ac6aeSMaor Gottlieb #define FIELDS_NOT_SUPPORTED(filter, field)\
1784c47ac6aeSMaor Gottlieb 	memchr_inv((void *)&filter.field  +\
1785c47ac6aeSMaor Gottlieb 		   sizeof(filter.field), 0,\
1786c47ac6aeSMaor Gottlieb 		   sizeof(filter) -\
1787c47ac6aeSMaor Gottlieb 		   offsetof(typeof(filter), field) -\
1788c47ac6aeSMaor Gottlieb 		   sizeof(filter.field))
1789c47ac6aeSMaor Gottlieb 
179019cc7524SAriel Levkovich #define IPV4_VERSION 4
179119cc7524SAriel Levkovich #define IPV6_VERSION 6
179219cc7524SAriel Levkovich static int parse_flow_attr(struct mlx5_core_dev *mdev, u32 *match_c,
179319cc7524SAriel Levkovich 			   u32 *match_v, const union ib_flow_spec *ib_spec,
1794a22ed86cSSlava Shwartsman 			   u32 *tag_id, bool *is_drop)
1795038d2ef8SMaor Gottlieb {
1796466fa6d2SMaor Gottlieb 	void *misc_params_c = MLX5_ADDR_OF(fte_match_param, match_c,
1797466fa6d2SMaor Gottlieb 					   misc_parameters);
1798466fa6d2SMaor Gottlieb 	void *misc_params_v = MLX5_ADDR_OF(fte_match_param, match_v,
1799466fa6d2SMaor Gottlieb 					   misc_parameters);
18002d1e697eSMoses Reuben 	void *headers_c;
18012d1e697eSMoses Reuben 	void *headers_v;
180219cc7524SAriel Levkovich 	int match_ipv;
1803466fa6d2SMaor Gottlieb 
18042d1e697eSMoses Reuben 	if (ib_spec->type & IB_FLOW_SPEC_INNER) {
18052d1e697eSMoses Reuben 		headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
18062d1e697eSMoses Reuben 					 inner_headers);
18072d1e697eSMoses Reuben 		headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
18082d1e697eSMoses Reuben 					 inner_headers);
180919cc7524SAriel Levkovich 		match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
181019cc7524SAriel Levkovich 					ft_field_support.inner_ip_version);
18112d1e697eSMoses Reuben 	} else {
18122d1e697eSMoses Reuben 		headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
18132d1e697eSMoses Reuben 					 outer_headers);
18142d1e697eSMoses Reuben 		headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
18152d1e697eSMoses Reuben 					 outer_headers);
181619cc7524SAriel Levkovich 		match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
181719cc7524SAriel Levkovich 					ft_field_support.outer_ip_version);
18182d1e697eSMoses Reuben 	}
18192d1e697eSMoses Reuben 
18202d1e697eSMoses Reuben 	switch (ib_spec->type & ~IB_FLOW_SPEC_INNER) {
1821038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_ETH:
1822c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->eth.mask, LAST_ETH_FIELD))
18231ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
1824038d2ef8SMaor Gottlieb 
18252d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
1826038d2ef8SMaor Gottlieb 					     dmac_47_16),
1827038d2ef8SMaor Gottlieb 				ib_spec->eth.mask.dst_mac);
18282d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
1829038d2ef8SMaor Gottlieb 					     dmac_47_16),
1830038d2ef8SMaor Gottlieb 				ib_spec->eth.val.dst_mac);
1831038d2ef8SMaor Gottlieb 
18322d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
1833ee3da804SMaor Gottlieb 					     smac_47_16),
1834ee3da804SMaor Gottlieb 				ib_spec->eth.mask.src_mac);
18352d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
1836ee3da804SMaor Gottlieb 					     smac_47_16),
1837ee3da804SMaor Gottlieb 				ib_spec->eth.val.src_mac);
1838ee3da804SMaor Gottlieb 
1839038d2ef8SMaor Gottlieb 		if (ib_spec->eth.mask.vlan_tag) {
18402d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
184110543365SMohamad Haj Yahia 				 cvlan_tag, 1);
18422d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
184310543365SMohamad Haj Yahia 				 cvlan_tag, 1);
1844038d2ef8SMaor Gottlieb 
18452d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1846038d2ef8SMaor Gottlieb 				 first_vid, ntohs(ib_spec->eth.mask.vlan_tag));
18472d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1848038d2ef8SMaor Gottlieb 				 first_vid, ntohs(ib_spec->eth.val.vlan_tag));
1849038d2ef8SMaor Gottlieb 
18502d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1851038d2ef8SMaor Gottlieb 				 first_cfi,
1852038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.mask.vlan_tag) >> 12);
18532d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1854038d2ef8SMaor Gottlieb 				 first_cfi,
1855038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.val.vlan_tag) >> 12);
1856038d2ef8SMaor Gottlieb 
18572d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1858038d2ef8SMaor Gottlieb 				 first_prio,
1859038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.mask.vlan_tag) >> 13);
18602d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1861038d2ef8SMaor Gottlieb 				 first_prio,
1862038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.val.vlan_tag) >> 13);
1863038d2ef8SMaor Gottlieb 		}
18642d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1865038d2ef8SMaor Gottlieb 			 ethertype, ntohs(ib_spec->eth.mask.ether_type));
18662d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1867038d2ef8SMaor Gottlieb 			 ethertype, ntohs(ib_spec->eth.val.ether_type));
1868038d2ef8SMaor Gottlieb 		break;
1869038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_IPV4:
1870c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->ipv4.mask, LAST_IPV4_FIELD))
18711ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
1872038d2ef8SMaor Gottlieb 
187319cc7524SAriel Levkovich 		if (match_ipv) {
187419cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
187519cc7524SAriel Levkovich 				 ip_version, 0xf);
187619cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
187719cc7524SAriel Levkovich 				 ip_version, IPV4_VERSION);
187819cc7524SAriel Levkovich 		} else {
18792d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1880038d2ef8SMaor Gottlieb 				 ethertype, 0xffff);
18812d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1882038d2ef8SMaor Gottlieb 				 ethertype, ETH_P_IP);
188319cc7524SAriel Levkovich 		}
1884038d2ef8SMaor Gottlieb 
18852d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
1886038d2ef8SMaor Gottlieb 				    src_ipv4_src_ipv6.ipv4_layout.ipv4),
1887038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.mask.src_ip,
1888038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.mask.src_ip));
18892d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
1890038d2ef8SMaor Gottlieb 				    src_ipv4_src_ipv6.ipv4_layout.ipv4),
1891038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.val.src_ip,
1892038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.val.src_ip));
18932d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
1894038d2ef8SMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
1895038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.mask.dst_ip,
1896038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.mask.dst_ip));
18972d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
1898038d2ef8SMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
1899038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.val.dst_ip,
1900038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.val.dst_ip));
1901ca0d4753SMaor Gottlieb 
19022d1e697eSMoses Reuben 		set_tos(headers_c, headers_v,
1903ca0d4753SMaor Gottlieb 			ib_spec->ipv4.mask.tos, ib_spec->ipv4.val.tos);
1904ca0d4753SMaor Gottlieb 
19052d1e697eSMoses Reuben 		set_proto(headers_c, headers_v,
1906ca0d4753SMaor Gottlieb 			  ib_spec->ipv4.mask.proto, ib_spec->ipv4.val.proto);
1907038d2ef8SMaor Gottlieb 		break;
1908026bae0cSMaor Gottlieb 	case IB_FLOW_SPEC_IPV6:
1909c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->ipv6.mask, LAST_IPV6_FIELD))
19101ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
1911026bae0cSMaor Gottlieb 
191219cc7524SAriel Levkovich 		if (match_ipv) {
191319cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
191419cc7524SAriel Levkovich 				 ip_version, 0xf);
191519cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
191619cc7524SAriel Levkovich 				 ip_version, IPV6_VERSION);
191719cc7524SAriel Levkovich 		} else {
19182d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1919026bae0cSMaor Gottlieb 				 ethertype, 0xffff);
19202d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1921026bae0cSMaor Gottlieb 				 ethertype, ETH_P_IPV6);
192219cc7524SAriel Levkovich 		}
1923026bae0cSMaor Gottlieb 
19242d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
1925026bae0cSMaor Gottlieb 				    src_ipv4_src_ipv6.ipv6_layout.ipv6),
1926026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.mask.src_ip,
1927026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.mask.src_ip));
19282d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
1929026bae0cSMaor Gottlieb 				    src_ipv4_src_ipv6.ipv6_layout.ipv6),
1930026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.val.src_ip,
1931026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.val.src_ip));
19322d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
1933026bae0cSMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
1934026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.mask.dst_ip,
1935026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.mask.dst_ip));
19362d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
1937026bae0cSMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
1938026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.val.dst_ip,
1939026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.val.dst_ip));
1940466fa6d2SMaor Gottlieb 
19412d1e697eSMoses Reuben 		set_tos(headers_c, headers_v,
1942466fa6d2SMaor Gottlieb 			ib_spec->ipv6.mask.traffic_class,
1943466fa6d2SMaor Gottlieb 			ib_spec->ipv6.val.traffic_class);
1944466fa6d2SMaor Gottlieb 
19452d1e697eSMoses Reuben 		set_proto(headers_c, headers_v,
1946466fa6d2SMaor Gottlieb 			  ib_spec->ipv6.mask.next_hdr,
1947466fa6d2SMaor Gottlieb 			  ib_spec->ipv6.val.next_hdr);
1948466fa6d2SMaor Gottlieb 
19492d1e697eSMoses Reuben 		set_flow_label(misc_params_c, misc_params_v,
19502d1e697eSMoses Reuben 			       ntohl(ib_spec->ipv6.mask.flow_label),
19512d1e697eSMoses Reuben 			       ntohl(ib_spec->ipv6.val.flow_label),
19522d1e697eSMoses Reuben 			       ib_spec->type & IB_FLOW_SPEC_INNER);
19532d1e697eSMoses Reuben 
1954026bae0cSMaor Gottlieb 		break;
1955038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_TCP:
1956c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
1957c47ac6aeSMaor Gottlieb 					 LAST_TCP_UDP_FIELD))
19581ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
1959038d2ef8SMaor Gottlieb 
19602d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
1961038d2ef8SMaor Gottlieb 			 0xff);
19622d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
1963038d2ef8SMaor Gottlieb 			 IPPROTO_TCP);
1964038d2ef8SMaor Gottlieb 
19652d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_sport,
1966038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.src_port));
19672d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_sport,
1968038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.src_port));
1969038d2ef8SMaor Gottlieb 
19702d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_dport,
1971038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.dst_port));
19722d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_dport,
1973038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.dst_port));
1974038d2ef8SMaor Gottlieb 		break;
1975038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_UDP:
1976c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
1977c47ac6aeSMaor Gottlieb 					 LAST_TCP_UDP_FIELD))
19781ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
1979038d2ef8SMaor Gottlieb 
19802d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
1981038d2ef8SMaor Gottlieb 			 0xff);
19822d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
1983038d2ef8SMaor Gottlieb 			 IPPROTO_UDP);
1984038d2ef8SMaor Gottlieb 
19852d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_sport,
1986038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.src_port));
19872d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_sport,
1988038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.src_port));
1989038d2ef8SMaor Gottlieb 
19902d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_dport,
1991038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.dst_port));
19922d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport,
1993038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.dst_port));
1994038d2ef8SMaor Gottlieb 		break;
1995ffb30d8fSMoses Reuben 	case IB_FLOW_SPEC_VXLAN_TUNNEL:
1996ffb30d8fSMoses Reuben 		if (FIELDS_NOT_SUPPORTED(ib_spec->tunnel.mask,
1997ffb30d8fSMoses Reuben 					 LAST_TUNNEL_FIELD))
19981ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
1999ffb30d8fSMoses Reuben 
2000ffb30d8fSMoses Reuben 		MLX5_SET(fte_match_set_misc, misc_params_c, vxlan_vni,
2001ffb30d8fSMoses Reuben 			 ntohl(ib_spec->tunnel.mask.tunnel_id));
2002ffb30d8fSMoses Reuben 		MLX5_SET(fte_match_set_misc, misc_params_v, vxlan_vni,
2003ffb30d8fSMoses Reuben 			 ntohl(ib_spec->tunnel.val.tunnel_id));
2004ffb30d8fSMoses Reuben 		break;
20052ac693f9SMoses Reuben 	case IB_FLOW_SPEC_ACTION_TAG:
20062ac693f9SMoses Reuben 		if (FIELDS_NOT_SUPPORTED(ib_spec->flow_tag,
20072ac693f9SMoses Reuben 					 LAST_FLOW_TAG_FIELD))
20082ac693f9SMoses Reuben 			return -EOPNOTSUPP;
20092ac693f9SMoses Reuben 		if (ib_spec->flow_tag.tag_id >= BIT(24))
20102ac693f9SMoses Reuben 			return -EINVAL;
20112ac693f9SMoses Reuben 
20122ac693f9SMoses Reuben 		*tag_id = ib_spec->flow_tag.tag_id;
20132ac693f9SMoses Reuben 		break;
2014a22ed86cSSlava Shwartsman 	case IB_FLOW_SPEC_ACTION_DROP:
2015a22ed86cSSlava Shwartsman 		if (FIELDS_NOT_SUPPORTED(ib_spec->drop,
2016a22ed86cSSlava Shwartsman 					 LAST_DROP_FIELD))
2017a22ed86cSSlava Shwartsman 			return -EOPNOTSUPP;
2018a22ed86cSSlava Shwartsman 		*is_drop = true;
2019a22ed86cSSlava Shwartsman 		break;
2020038d2ef8SMaor Gottlieb 	default:
2021038d2ef8SMaor Gottlieb 		return -EINVAL;
2022038d2ef8SMaor Gottlieb 	}
2023038d2ef8SMaor Gottlieb 
2024038d2ef8SMaor Gottlieb 	return 0;
2025038d2ef8SMaor Gottlieb }
2026038d2ef8SMaor Gottlieb 
2027038d2ef8SMaor Gottlieb /* If a flow could catch both multicast and unicast packets,
2028038d2ef8SMaor Gottlieb  * it won't fall into the multicast flow steering table and this rule
2029038d2ef8SMaor Gottlieb  * could steal other multicast packets.
2030038d2ef8SMaor Gottlieb  */
2031038d2ef8SMaor Gottlieb static bool flow_is_multicast_only(struct ib_flow_attr *ib_attr)
2032038d2ef8SMaor Gottlieb {
2033038d2ef8SMaor Gottlieb 	struct ib_flow_spec_eth *eth_spec;
2034038d2ef8SMaor Gottlieb 
2035038d2ef8SMaor Gottlieb 	if (ib_attr->type != IB_FLOW_ATTR_NORMAL ||
2036038d2ef8SMaor Gottlieb 	    ib_attr->size < sizeof(struct ib_flow_attr) +
2037038d2ef8SMaor Gottlieb 	    sizeof(struct ib_flow_spec_eth) ||
2038038d2ef8SMaor Gottlieb 	    ib_attr->num_of_specs < 1)
2039038d2ef8SMaor Gottlieb 		return false;
2040038d2ef8SMaor Gottlieb 
2041038d2ef8SMaor Gottlieb 	eth_spec = (struct ib_flow_spec_eth *)(ib_attr + 1);
2042038d2ef8SMaor Gottlieb 	if (eth_spec->type != IB_FLOW_SPEC_ETH ||
2043038d2ef8SMaor Gottlieb 	    eth_spec->size != sizeof(*eth_spec))
2044038d2ef8SMaor Gottlieb 		return false;
2045038d2ef8SMaor Gottlieb 
2046038d2ef8SMaor Gottlieb 	return is_multicast_ether_addr(eth_spec->mask.dst_mac) &&
2047038d2ef8SMaor Gottlieb 	       is_multicast_ether_addr(eth_spec->val.dst_mac);
2048038d2ef8SMaor Gottlieb }
2049038d2ef8SMaor Gottlieb 
205019cc7524SAriel Levkovich static bool is_valid_ethertype(struct mlx5_core_dev *mdev,
205119cc7524SAriel Levkovich 			       const struct ib_flow_attr *flow_attr,
20520f750966SAriel Levkovich 			       bool check_inner)
2053038d2ef8SMaor Gottlieb {
2054038d2ef8SMaor Gottlieb 	union ib_flow_spec *ib_spec = (union ib_flow_spec *)(flow_attr + 1);
205519cc7524SAriel Levkovich 	int match_ipv = check_inner ?
205619cc7524SAriel Levkovich 			MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
205719cc7524SAriel Levkovich 					ft_field_support.inner_ip_version) :
205819cc7524SAriel Levkovich 			MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
205919cc7524SAriel Levkovich 					ft_field_support.outer_ip_version);
20600f750966SAriel Levkovich 	int inner_bit = check_inner ? IB_FLOW_SPEC_INNER : 0;
20610f750966SAriel Levkovich 	bool ipv4_spec_valid, ipv6_spec_valid;
20620f750966SAriel Levkovich 	unsigned int ip_spec_type = 0;
20630f750966SAriel Levkovich 	bool has_ethertype = false;
2064038d2ef8SMaor Gottlieb 	unsigned int spec_index;
20650f750966SAriel Levkovich 	bool mask_valid = true;
20660f750966SAriel Levkovich 	u16 eth_type = 0;
20670f750966SAriel Levkovich 	bool type_valid;
2068038d2ef8SMaor Gottlieb 
2069038d2ef8SMaor Gottlieb 	/* Validate that ethertype is correct */
2070038d2ef8SMaor Gottlieb 	for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
20710f750966SAriel Levkovich 		if ((ib_spec->type == (IB_FLOW_SPEC_ETH | inner_bit)) &&
2072038d2ef8SMaor Gottlieb 		    ib_spec->eth.mask.ether_type) {
20730f750966SAriel Levkovich 			mask_valid = (ib_spec->eth.mask.ether_type ==
20740f750966SAriel Levkovich 				      htons(0xffff));
20750f750966SAriel Levkovich 			has_ethertype = true;
20760f750966SAriel Levkovich 			eth_type = ntohs(ib_spec->eth.val.ether_type);
20770f750966SAriel Levkovich 		} else if ((ib_spec->type == (IB_FLOW_SPEC_IPV4 | inner_bit)) ||
20780f750966SAriel Levkovich 			   (ib_spec->type == (IB_FLOW_SPEC_IPV6 | inner_bit))) {
20790f750966SAriel Levkovich 			ip_spec_type = ib_spec->type;
2080038d2ef8SMaor Gottlieb 		}
2081038d2ef8SMaor Gottlieb 		ib_spec = (void *)ib_spec + ib_spec->size;
2082038d2ef8SMaor Gottlieb 	}
20830f750966SAriel Levkovich 
20840f750966SAriel Levkovich 	type_valid = (!has_ethertype) || (!ip_spec_type);
20850f750966SAriel Levkovich 	if (!type_valid && mask_valid) {
20860f750966SAriel Levkovich 		ipv4_spec_valid = (eth_type == ETH_P_IP) &&
20870f750966SAriel Levkovich 			(ip_spec_type == (IB_FLOW_SPEC_IPV4 | inner_bit));
20880f750966SAriel Levkovich 		ipv6_spec_valid = (eth_type == ETH_P_IPV6) &&
20890f750966SAriel Levkovich 			(ip_spec_type == (IB_FLOW_SPEC_IPV6 | inner_bit));
209019cc7524SAriel Levkovich 
209119cc7524SAriel Levkovich 		type_valid = (ipv4_spec_valid) || (ipv6_spec_valid) ||
209219cc7524SAriel Levkovich 			     (((eth_type == ETH_P_MPLS_UC) ||
209319cc7524SAriel Levkovich 			       (eth_type == ETH_P_MPLS_MC)) && match_ipv);
20940f750966SAriel Levkovich 	}
20950f750966SAriel Levkovich 
20960f750966SAriel Levkovich 	return type_valid;
20970f750966SAriel Levkovich }
20980f750966SAriel Levkovich 
209919cc7524SAriel Levkovich static bool is_valid_attr(struct mlx5_core_dev *mdev,
210019cc7524SAriel Levkovich 			  const struct ib_flow_attr *flow_attr)
21010f750966SAriel Levkovich {
210219cc7524SAriel Levkovich 	return is_valid_ethertype(mdev, flow_attr, false) &&
210319cc7524SAriel Levkovich 	       is_valid_ethertype(mdev, flow_attr, true);
2104038d2ef8SMaor Gottlieb }
2105038d2ef8SMaor Gottlieb 
2106038d2ef8SMaor Gottlieb static void put_flow_table(struct mlx5_ib_dev *dev,
2107038d2ef8SMaor Gottlieb 			   struct mlx5_ib_flow_prio *prio, bool ft_added)
2108038d2ef8SMaor Gottlieb {
2109038d2ef8SMaor Gottlieb 	prio->refcount -= !!ft_added;
2110038d2ef8SMaor Gottlieb 	if (!prio->refcount) {
2111038d2ef8SMaor Gottlieb 		mlx5_destroy_flow_table(prio->flow_table);
2112038d2ef8SMaor Gottlieb 		prio->flow_table = NULL;
2113038d2ef8SMaor Gottlieb 	}
2114038d2ef8SMaor Gottlieb }
2115038d2ef8SMaor Gottlieb 
2116038d2ef8SMaor Gottlieb static int mlx5_ib_destroy_flow(struct ib_flow *flow_id)
2117038d2ef8SMaor Gottlieb {
2118038d2ef8SMaor Gottlieb 	struct mlx5_ib_dev *dev = to_mdev(flow_id->qp->device);
2119038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = container_of(flow_id,
2120038d2ef8SMaor Gottlieb 							  struct mlx5_ib_flow_handler,
2121038d2ef8SMaor Gottlieb 							  ibflow);
2122038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *iter, *tmp;
2123038d2ef8SMaor Gottlieb 
2124038d2ef8SMaor Gottlieb 	mutex_lock(&dev->flow_db.lock);
2125038d2ef8SMaor Gottlieb 
2126038d2ef8SMaor Gottlieb 	list_for_each_entry_safe(iter, tmp, &handler->list, list) {
212774491de9SMark Bloch 		mlx5_del_flow_rules(iter->rule);
2128cc0e5d42SMaor Gottlieb 		put_flow_table(dev, iter->prio, true);
2129038d2ef8SMaor Gottlieb 		list_del(&iter->list);
2130038d2ef8SMaor Gottlieb 		kfree(iter);
2131038d2ef8SMaor Gottlieb 	}
2132038d2ef8SMaor Gottlieb 
213374491de9SMark Bloch 	mlx5_del_flow_rules(handler->rule);
21345497adc6SMaor Gottlieb 	put_flow_table(dev, handler->prio, true);
2135038d2ef8SMaor Gottlieb 	mutex_unlock(&dev->flow_db.lock);
2136038d2ef8SMaor Gottlieb 
2137038d2ef8SMaor Gottlieb 	kfree(handler);
2138038d2ef8SMaor Gottlieb 
2139038d2ef8SMaor Gottlieb 	return 0;
2140038d2ef8SMaor Gottlieb }
2141038d2ef8SMaor Gottlieb 
214235d19011SMaor Gottlieb static int ib_prio_to_core_prio(unsigned int priority, bool dont_trap)
214335d19011SMaor Gottlieb {
214435d19011SMaor Gottlieb 	priority *= 2;
214535d19011SMaor Gottlieb 	if (!dont_trap)
214635d19011SMaor Gottlieb 		priority++;
214735d19011SMaor Gottlieb 	return priority;
214835d19011SMaor Gottlieb }
214935d19011SMaor Gottlieb 
2150cc0e5d42SMaor Gottlieb enum flow_table_type {
2151cc0e5d42SMaor Gottlieb 	MLX5_IB_FT_RX,
2152cc0e5d42SMaor Gottlieb 	MLX5_IB_FT_TX
2153cc0e5d42SMaor Gottlieb };
2154cc0e5d42SMaor Gottlieb 
215500b7c2abSMaor Gottlieb #define MLX5_FS_MAX_TYPES	 6
215600b7c2abSMaor Gottlieb #define MLX5_FS_MAX_ENTRIES	 BIT(16)
2157038d2ef8SMaor Gottlieb static struct mlx5_ib_flow_prio *get_flow_table(struct mlx5_ib_dev *dev,
2158cc0e5d42SMaor Gottlieb 						struct ib_flow_attr *flow_attr,
2159cc0e5d42SMaor Gottlieb 						enum flow_table_type ft_type)
2160038d2ef8SMaor Gottlieb {
216135d19011SMaor Gottlieb 	bool dont_trap = flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP;
2162038d2ef8SMaor Gottlieb 	struct mlx5_flow_namespace *ns = NULL;
2163038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_prio *prio;
2164038d2ef8SMaor Gottlieb 	struct mlx5_flow_table *ft;
2165dac388efSMaor Gottlieb 	int max_table_size;
2166038d2ef8SMaor Gottlieb 	int num_entries;
2167038d2ef8SMaor Gottlieb 	int num_groups;
2168038d2ef8SMaor Gottlieb 	int priority;
2169038d2ef8SMaor Gottlieb 	int err = 0;
2170038d2ef8SMaor Gottlieb 
2171dac388efSMaor Gottlieb 	max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
2172dac388efSMaor Gottlieb 						       log_max_ft_size));
2173038d2ef8SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
217435d19011SMaor Gottlieb 		if (flow_is_multicast_only(flow_attr) &&
217535d19011SMaor Gottlieb 		    !dont_trap)
2176038d2ef8SMaor Gottlieb 			priority = MLX5_IB_FLOW_MCAST_PRIO;
2177038d2ef8SMaor Gottlieb 		else
217835d19011SMaor Gottlieb 			priority = ib_prio_to_core_prio(flow_attr->priority,
217935d19011SMaor Gottlieb 							dont_trap);
2180038d2ef8SMaor Gottlieb 		ns = mlx5_get_flow_namespace(dev->mdev,
2181038d2ef8SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_BYPASS);
2182038d2ef8SMaor Gottlieb 		num_entries = MLX5_FS_MAX_ENTRIES;
2183038d2ef8SMaor Gottlieb 		num_groups = MLX5_FS_MAX_TYPES;
2184038d2ef8SMaor Gottlieb 		prio = &dev->flow_db.prios[priority];
2185038d2ef8SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
2186038d2ef8SMaor Gottlieb 		   flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
2187038d2ef8SMaor Gottlieb 		ns = mlx5_get_flow_namespace(dev->mdev,
2188038d2ef8SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_LEFTOVERS);
2189038d2ef8SMaor Gottlieb 		build_leftovers_ft_param(&priority,
2190038d2ef8SMaor Gottlieb 					 &num_entries,
2191038d2ef8SMaor Gottlieb 					 &num_groups);
2192038d2ef8SMaor Gottlieb 		prio = &dev->flow_db.prios[MLX5_IB_FLOW_LEFTOVERS_PRIO];
2193cc0e5d42SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
2194cc0e5d42SMaor Gottlieb 		if (!MLX5_CAP_FLOWTABLE(dev->mdev,
2195cc0e5d42SMaor Gottlieb 					allow_sniffer_and_nic_rx_shared_tir))
2196cc0e5d42SMaor Gottlieb 			return ERR_PTR(-ENOTSUPP);
2197cc0e5d42SMaor Gottlieb 
2198cc0e5d42SMaor Gottlieb 		ns = mlx5_get_flow_namespace(dev->mdev, ft_type == MLX5_IB_FT_RX ?
2199cc0e5d42SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_SNIFFER_RX :
2200cc0e5d42SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_SNIFFER_TX);
2201cc0e5d42SMaor Gottlieb 
2202cc0e5d42SMaor Gottlieb 		prio = &dev->flow_db.sniffer[ft_type];
2203cc0e5d42SMaor Gottlieb 		priority = 0;
2204cc0e5d42SMaor Gottlieb 		num_entries = 1;
2205cc0e5d42SMaor Gottlieb 		num_groups = 1;
2206038d2ef8SMaor Gottlieb 	}
2207038d2ef8SMaor Gottlieb 
2208038d2ef8SMaor Gottlieb 	if (!ns)
2209038d2ef8SMaor Gottlieb 		return ERR_PTR(-ENOTSUPP);
2210038d2ef8SMaor Gottlieb 
2211dac388efSMaor Gottlieb 	if (num_entries > max_table_size)
2212dac388efSMaor Gottlieb 		return ERR_PTR(-ENOMEM);
2213dac388efSMaor Gottlieb 
2214038d2ef8SMaor Gottlieb 	ft = prio->flow_table;
2215038d2ef8SMaor Gottlieb 	if (!ft) {
2216038d2ef8SMaor Gottlieb 		ft = mlx5_create_auto_grouped_flow_table(ns, priority,
2217038d2ef8SMaor Gottlieb 							 num_entries,
2218d63cd286SMaor Gottlieb 							 num_groups,
2219c9f1b073SHadar Hen Zion 							 0, 0);
2220038d2ef8SMaor Gottlieb 
2221038d2ef8SMaor Gottlieb 		if (!IS_ERR(ft)) {
2222038d2ef8SMaor Gottlieb 			prio->refcount = 0;
2223038d2ef8SMaor Gottlieb 			prio->flow_table = ft;
2224038d2ef8SMaor Gottlieb 		} else {
2225038d2ef8SMaor Gottlieb 			err = PTR_ERR(ft);
2226038d2ef8SMaor Gottlieb 		}
2227038d2ef8SMaor Gottlieb 	}
2228038d2ef8SMaor Gottlieb 
2229038d2ef8SMaor Gottlieb 	return err ? ERR_PTR(err) : prio;
2230038d2ef8SMaor Gottlieb }
2231038d2ef8SMaor Gottlieb 
2232038d2ef8SMaor Gottlieb static struct mlx5_ib_flow_handler *create_flow_rule(struct mlx5_ib_dev *dev,
2233038d2ef8SMaor Gottlieb 						     struct mlx5_ib_flow_prio *ft_prio,
2234dd063d0eSMaor Gottlieb 						     const struct ib_flow_attr *flow_attr,
2235038d2ef8SMaor Gottlieb 						     struct mlx5_flow_destination *dst)
2236038d2ef8SMaor Gottlieb {
2237038d2ef8SMaor Gottlieb 	struct mlx5_flow_table	*ft = ft_prio->flow_table;
2238038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler;
223966958ed9SHadar Hen Zion 	struct mlx5_flow_act flow_act = {0};
2240c5bb1730SMaor Gottlieb 	struct mlx5_flow_spec *spec;
2241a22ed86cSSlava Shwartsman 	struct mlx5_flow_destination *rule_dst = dst;
2242dd063d0eSMaor Gottlieb 	const void *ib_flow = (const void *)flow_attr + sizeof(*flow_attr);
2243038d2ef8SMaor Gottlieb 	unsigned int spec_index;
22442ac693f9SMoses Reuben 	u32 flow_tag = MLX5_FS_DEFAULT_FLOW_TAG;
2245a22ed86cSSlava Shwartsman 	bool is_drop = false;
2246038d2ef8SMaor Gottlieb 	int err = 0;
2247a22ed86cSSlava Shwartsman 	int dest_num = 1;
2248038d2ef8SMaor Gottlieb 
224919cc7524SAriel Levkovich 	if (!is_valid_attr(dev->mdev, flow_attr))
2250038d2ef8SMaor Gottlieb 		return ERR_PTR(-EINVAL);
2251038d2ef8SMaor Gottlieb 
22521b9a07eeSLeon Romanovsky 	spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
2253038d2ef8SMaor Gottlieb 	handler = kzalloc(sizeof(*handler), GFP_KERNEL);
2254c5bb1730SMaor Gottlieb 	if (!handler || !spec) {
2255038d2ef8SMaor Gottlieb 		err = -ENOMEM;
2256038d2ef8SMaor Gottlieb 		goto free;
2257038d2ef8SMaor Gottlieb 	}
2258038d2ef8SMaor Gottlieb 
2259038d2ef8SMaor Gottlieb 	INIT_LIST_HEAD(&handler->list);
2260038d2ef8SMaor Gottlieb 
2261038d2ef8SMaor Gottlieb 	for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
226219cc7524SAriel Levkovich 		err = parse_flow_attr(dev->mdev, spec->match_criteria,
2263a22ed86cSSlava Shwartsman 				      spec->match_value,
2264a22ed86cSSlava Shwartsman 				      ib_flow, &flow_tag, &is_drop);
2265038d2ef8SMaor Gottlieb 		if (err < 0)
2266038d2ef8SMaor Gottlieb 			goto free;
2267038d2ef8SMaor Gottlieb 
2268038d2ef8SMaor Gottlieb 		ib_flow += ((union ib_flow_spec *)ib_flow)->size;
2269038d2ef8SMaor Gottlieb 	}
2270038d2ef8SMaor Gottlieb 
2271466fa6d2SMaor Gottlieb 	spec->match_criteria_enable = get_match_criteria_enable(spec->match_criteria);
2272a22ed86cSSlava Shwartsman 	if (is_drop) {
2273a22ed86cSSlava Shwartsman 		flow_act.action = MLX5_FLOW_CONTEXT_ACTION_DROP;
2274a22ed86cSSlava Shwartsman 		rule_dst = NULL;
2275a22ed86cSSlava Shwartsman 		dest_num = 0;
2276a22ed86cSSlava Shwartsman 	} else {
227766958ed9SHadar Hen Zion 		flow_act.action = dst ? MLX5_FLOW_CONTEXT_ACTION_FWD_DEST :
227835d19011SMaor Gottlieb 		    MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO;
2279a22ed86cSSlava Shwartsman 	}
22802ac693f9SMoses Reuben 
22812ac693f9SMoses Reuben 	if (flow_tag != MLX5_FS_DEFAULT_FLOW_TAG &&
22822ac693f9SMoses Reuben 	    (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
22832ac693f9SMoses Reuben 	     flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT)) {
22842ac693f9SMoses Reuben 		mlx5_ib_warn(dev, "Flow tag %u and attribute type %x isn't allowed in leftovers\n",
22852ac693f9SMoses Reuben 			     flow_tag, flow_attr->type);
22862ac693f9SMoses Reuben 		err = -EINVAL;
22872ac693f9SMoses Reuben 		goto free;
22882ac693f9SMoses Reuben 	}
22892ac693f9SMoses Reuben 	flow_act.flow_tag = flow_tag;
229074491de9SMark Bloch 	handler->rule = mlx5_add_flow_rules(ft, spec,
229166958ed9SHadar Hen Zion 					    &flow_act,
2292a22ed86cSSlava Shwartsman 					    rule_dst, dest_num);
2293038d2ef8SMaor Gottlieb 
2294038d2ef8SMaor Gottlieb 	if (IS_ERR(handler->rule)) {
2295038d2ef8SMaor Gottlieb 		err = PTR_ERR(handler->rule);
2296038d2ef8SMaor Gottlieb 		goto free;
2297038d2ef8SMaor Gottlieb 	}
2298038d2ef8SMaor Gottlieb 
2299d9d4980aSMaor Gottlieb 	ft_prio->refcount++;
23005497adc6SMaor Gottlieb 	handler->prio = ft_prio;
2301038d2ef8SMaor Gottlieb 
2302038d2ef8SMaor Gottlieb 	ft_prio->flow_table = ft;
2303038d2ef8SMaor Gottlieb free:
2304038d2ef8SMaor Gottlieb 	if (err)
2305038d2ef8SMaor Gottlieb 		kfree(handler);
2306c5bb1730SMaor Gottlieb 	kvfree(spec);
2307038d2ef8SMaor Gottlieb 	return err ? ERR_PTR(err) : handler;
2308038d2ef8SMaor Gottlieb }
2309038d2ef8SMaor Gottlieb 
231035d19011SMaor Gottlieb static struct mlx5_ib_flow_handler *create_dont_trap_rule(struct mlx5_ib_dev *dev,
231135d19011SMaor Gottlieb 							  struct mlx5_ib_flow_prio *ft_prio,
231235d19011SMaor Gottlieb 							  struct ib_flow_attr *flow_attr,
231335d19011SMaor Gottlieb 							  struct mlx5_flow_destination *dst)
231435d19011SMaor Gottlieb {
231535d19011SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_dst = NULL;
231635d19011SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
231735d19011SMaor Gottlieb 
231835d19011SMaor Gottlieb 	handler = create_flow_rule(dev, ft_prio, flow_attr, NULL);
231935d19011SMaor Gottlieb 	if (!IS_ERR(handler)) {
232035d19011SMaor Gottlieb 		handler_dst = create_flow_rule(dev, ft_prio,
232135d19011SMaor Gottlieb 					       flow_attr, dst);
232235d19011SMaor Gottlieb 		if (IS_ERR(handler_dst)) {
232374491de9SMark Bloch 			mlx5_del_flow_rules(handler->rule);
2324d9d4980aSMaor Gottlieb 			ft_prio->refcount--;
232535d19011SMaor Gottlieb 			kfree(handler);
232635d19011SMaor Gottlieb 			handler = handler_dst;
232735d19011SMaor Gottlieb 		} else {
232835d19011SMaor Gottlieb 			list_add(&handler_dst->list, &handler->list);
232935d19011SMaor Gottlieb 		}
233035d19011SMaor Gottlieb 	}
233135d19011SMaor Gottlieb 
233235d19011SMaor Gottlieb 	return handler;
233335d19011SMaor Gottlieb }
2334038d2ef8SMaor Gottlieb enum {
2335038d2ef8SMaor Gottlieb 	LEFTOVERS_MC,
2336038d2ef8SMaor Gottlieb 	LEFTOVERS_UC,
2337038d2ef8SMaor Gottlieb };
2338038d2ef8SMaor Gottlieb 
2339038d2ef8SMaor Gottlieb static struct mlx5_ib_flow_handler *create_leftovers_rule(struct mlx5_ib_dev *dev,
2340038d2ef8SMaor Gottlieb 							  struct mlx5_ib_flow_prio *ft_prio,
2341038d2ef8SMaor Gottlieb 							  struct ib_flow_attr *flow_attr,
2342038d2ef8SMaor Gottlieb 							  struct mlx5_flow_destination *dst)
2343038d2ef8SMaor Gottlieb {
2344038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_ucast = NULL;
2345038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
2346038d2ef8SMaor Gottlieb 
2347038d2ef8SMaor Gottlieb 	static struct {
2348038d2ef8SMaor Gottlieb 		struct ib_flow_attr	flow_attr;
2349038d2ef8SMaor Gottlieb 		struct ib_flow_spec_eth eth_flow;
2350038d2ef8SMaor Gottlieb 	} leftovers_specs[] = {
2351038d2ef8SMaor Gottlieb 		[LEFTOVERS_MC] = {
2352038d2ef8SMaor Gottlieb 			.flow_attr = {
2353038d2ef8SMaor Gottlieb 				.num_of_specs = 1,
2354038d2ef8SMaor Gottlieb 				.size = sizeof(leftovers_specs[0])
2355038d2ef8SMaor Gottlieb 			},
2356038d2ef8SMaor Gottlieb 			.eth_flow = {
2357038d2ef8SMaor Gottlieb 				.type = IB_FLOW_SPEC_ETH,
2358038d2ef8SMaor Gottlieb 				.size = sizeof(struct ib_flow_spec_eth),
2359038d2ef8SMaor Gottlieb 				.mask = {.dst_mac = {0x1} },
2360038d2ef8SMaor Gottlieb 				.val =  {.dst_mac = {0x1} }
2361038d2ef8SMaor Gottlieb 			}
2362038d2ef8SMaor Gottlieb 		},
2363038d2ef8SMaor Gottlieb 		[LEFTOVERS_UC] = {
2364038d2ef8SMaor Gottlieb 			.flow_attr = {
2365038d2ef8SMaor Gottlieb 				.num_of_specs = 1,
2366038d2ef8SMaor Gottlieb 				.size = sizeof(leftovers_specs[0])
2367038d2ef8SMaor Gottlieb 			},
2368038d2ef8SMaor Gottlieb 			.eth_flow = {
2369038d2ef8SMaor Gottlieb 				.type = IB_FLOW_SPEC_ETH,
2370038d2ef8SMaor Gottlieb 				.size = sizeof(struct ib_flow_spec_eth),
2371038d2ef8SMaor Gottlieb 				.mask = {.dst_mac = {0x1} },
2372038d2ef8SMaor Gottlieb 				.val = {.dst_mac = {} }
2373038d2ef8SMaor Gottlieb 			}
2374038d2ef8SMaor Gottlieb 		}
2375038d2ef8SMaor Gottlieb 	};
2376038d2ef8SMaor Gottlieb 
2377038d2ef8SMaor Gottlieb 	handler = create_flow_rule(dev, ft_prio,
2378038d2ef8SMaor Gottlieb 				   &leftovers_specs[LEFTOVERS_MC].flow_attr,
2379038d2ef8SMaor Gottlieb 				   dst);
2380038d2ef8SMaor Gottlieb 	if (!IS_ERR(handler) &&
2381038d2ef8SMaor Gottlieb 	    flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT) {
2382038d2ef8SMaor Gottlieb 		handler_ucast = create_flow_rule(dev, ft_prio,
2383038d2ef8SMaor Gottlieb 						 &leftovers_specs[LEFTOVERS_UC].flow_attr,
2384038d2ef8SMaor Gottlieb 						 dst);
2385038d2ef8SMaor Gottlieb 		if (IS_ERR(handler_ucast)) {
238674491de9SMark Bloch 			mlx5_del_flow_rules(handler->rule);
2387d9d4980aSMaor Gottlieb 			ft_prio->refcount--;
2388038d2ef8SMaor Gottlieb 			kfree(handler);
2389038d2ef8SMaor Gottlieb 			handler = handler_ucast;
2390038d2ef8SMaor Gottlieb 		} else {
2391038d2ef8SMaor Gottlieb 			list_add(&handler_ucast->list, &handler->list);
2392038d2ef8SMaor Gottlieb 		}
2393038d2ef8SMaor Gottlieb 	}
2394038d2ef8SMaor Gottlieb 
2395038d2ef8SMaor Gottlieb 	return handler;
2396038d2ef8SMaor Gottlieb }
2397038d2ef8SMaor Gottlieb 
2398cc0e5d42SMaor Gottlieb static struct mlx5_ib_flow_handler *create_sniffer_rule(struct mlx5_ib_dev *dev,
2399cc0e5d42SMaor Gottlieb 							struct mlx5_ib_flow_prio *ft_rx,
2400cc0e5d42SMaor Gottlieb 							struct mlx5_ib_flow_prio *ft_tx,
2401cc0e5d42SMaor Gottlieb 							struct mlx5_flow_destination *dst)
2402cc0e5d42SMaor Gottlieb {
2403cc0e5d42SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_rx;
2404cc0e5d42SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_tx;
2405cc0e5d42SMaor Gottlieb 	int err;
2406cc0e5d42SMaor Gottlieb 	static const struct ib_flow_attr flow_attr  = {
2407cc0e5d42SMaor Gottlieb 		.num_of_specs = 0,
2408cc0e5d42SMaor Gottlieb 		.size = sizeof(flow_attr)
2409cc0e5d42SMaor Gottlieb 	};
2410cc0e5d42SMaor Gottlieb 
2411cc0e5d42SMaor Gottlieb 	handler_rx = create_flow_rule(dev, ft_rx, &flow_attr, dst);
2412cc0e5d42SMaor Gottlieb 	if (IS_ERR(handler_rx)) {
2413cc0e5d42SMaor Gottlieb 		err = PTR_ERR(handler_rx);
2414cc0e5d42SMaor Gottlieb 		goto err;
2415cc0e5d42SMaor Gottlieb 	}
2416cc0e5d42SMaor Gottlieb 
2417cc0e5d42SMaor Gottlieb 	handler_tx = create_flow_rule(dev, ft_tx, &flow_attr, dst);
2418cc0e5d42SMaor Gottlieb 	if (IS_ERR(handler_tx)) {
2419cc0e5d42SMaor Gottlieb 		err = PTR_ERR(handler_tx);
2420cc0e5d42SMaor Gottlieb 		goto err_tx;
2421cc0e5d42SMaor Gottlieb 	}
2422cc0e5d42SMaor Gottlieb 
2423cc0e5d42SMaor Gottlieb 	list_add(&handler_tx->list, &handler_rx->list);
2424cc0e5d42SMaor Gottlieb 
2425cc0e5d42SMaor Gottlieb 	return handler_rx;
2426cc0e5d42SMaor Gottlieb 
2427cc0e5d42SMaor Gottlieb err_tx:
242874491de9SMark Bloch 	mlx5_del_flow_rules(handler_rx->rule);
2429cc0e5d42SMaor Gottlieb 	ft_rx->refcount--;
2430cc0e5d42SMaor Gottlieb 	kfree(handler_rx);
2431cc0e5d42SMaor Gottlieb err:
2432cc0e5d42SMaor Gottlieb 	return ERR_PTR(err);
2433cc0e5d42SMaor Gottlieb }
2434cc0e5d42SMaor Gottlieb 
2435038d2ef8SMaor Gottlieb static struct ib_flow *mlx5_ib_create_flow(struct ib_qp *qp,
2436038d2ef8SMaor Gottlieb 					   struct ib_flow_attr *flow_attr,
2437038d2ef8SMaor Gottlieb 					   int domain)
2438038d2ef8SMaor Gottlieb {
2439038d2ef8SMaor Gottlieb 	struct mlx5_ib_dev *dev = to_mdev(qp->device);
2440d9f88e5aSYishai Hadas 	struct mlx5_ib_qp *mqp = to_mqp(qp);
2441038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
2442038d2ef8SMaor Gottlieb 	struct mlx5_flow_destination *dst = NULL;
2443cc0e5d42SMaor Gottlieb 	struct mlx5_ib_flow_prio *ft_prio_tx = NULL;
2444038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_prio *ft_prio;
2445038d2ef8SMaor Gottlieb 	int err;
2446038d2ef8SMaor Gottlieb 
2447038d2ef8SMaor Gottlieb 	if (flow_attr->priority > MLX5_IB_FLOW_LAST_PRIO)
2448dac388efSMaor Gottlieb 		return ERR_PTR(-ENOMEM);
2449038d2ef8SMaor Gottlieb 
2450038d2ef8SMaor Gottlieb 	if (domain != IB_FLOW_DOMAIN_USER ||
2451038d2ef8SMaor Gottlieb 	    flow_attr->port > MLX5_CAP_GEN(dev->mdev, num_ports) ||
245235d19011SMaor Gottlieb 	    (flow_attr->flags & ~IB_FLOW_ATTR_FLAGS_DONT_TRAP))
2453038d2ef8SMaor Gottlieb 		return ERR_PTR(-EINVAL);
2454038d2ef8SMaor Gottlieb 
2455038d2ef8SMaor Gottlieb 	dst = kzalloc(sizeof(*dst), GFP_KERNEL);
2456038d2ef8SMaor Gottlieb 	if (!dst)
2457038d2ef8SMaor Gottlieb 		return ERR_PTR(-ENOMEM);
2458038d2ef8SMaor Gottlieb 
2459038d2ef8SMaor Gottlieb 	mutex_lock(&dev->flow_db.lock);
2460038d2ef8SMaor Gottlieb 
2461cc0e5d42SMaor Gottlieb 	ft_prio = get_flow_table(dev, flow_attr, MLX5_IB_FT_RX);
2462038d2ef8SMaor Gottlieb 	if (IS_ERR(ft_prio)) {
2463038d2ef8SMaor Gottlieb 		err = PTR_ERR(ft_prio);
2464038d2ef8SMaor Gottlieb 		goto unlock;
2465038d2ef8SMaor Gottlieb 	}
2466cc0e5d42SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
2467cc0e5d42SMaor Gottlieb 		ft_prio_tx = get_flow_table(dev, flow_attr, MLX5_IB_FT_TX);
2468cc0e5d42SMaor Gottlieb 		if (IS_ERR(ft_prio_tx)) {
2469cc0e5d42SMaor Gottlieb 			err = PTR_ERR(ft_prio_tx);
2470cc0e5d42SMaor Gottlieb 			ft_prio_tx = NULL;
2471cc0e5d42SMaor Gottlieb 			goto destroy_ft;
2472cc0e5d42SMaor Gottlieb 		}
2473cc0e5d42SMaor Gottlieb 	}
2474038d2ef8SMaor Gottlieb 
2475038d2ef8SMaor Gottlieb 	dst->type = MLX5_FLOW_DESTINATION_TYPE_TIR;
2476d9f88e5aSYishai Hadas 	if (mqp->flags & MLX5_IB_QP_RSS)
2477d9f88e5aSYishai Hadas 		dst->tir_num = mqp->rss_qp.tirn;
2478d9f88e5aSYishai Hadas 	else
2479d9f88e5aSYishai Hadas 		dst->tir_num = mqp->raw_packet_qp.rq.tirn;
2480038d2ef8SMaor Gottlieb 
2481038d2ef8SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
248235d19011SMaor Gottlieb 		if (flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP)  {
248335d19011SMaor Gottlieb 			handler = create_dont_trap_rule(dev, ft_prio,
248435d19011SMaor Gottlieb 							flow_attr, dst);
248535d19011SMaor Gottlieb 		} else {
2486038d2ef8SMaor Gottlieb 			handler = create_flow_rule(dev, ft_prio, flow_attr,
2487038d2ef8SMaor Gottlieb 						   dst);
248835d19011SMaor Gottlieb 		}
2489038d2ef8SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
2490038d2ef8SMaor Gottlieb 		   flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
2491038d2ef8SMaor Gottlieb 		handler = create_leftovers_rule(dev, ft_prio, flow_attr,
2492038d2ef8SMaor Gottlieb 						dst);
2493cc0e5d42SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
2494cc0e5d42SMaor Gottlieb 		handler = create_sniffer_rule(dev, ft_prio, ft_prio_tx, dst);
2495038d2ef8SMaor Gottlieb 	} else {
2496038d2ef8SMaor Gottlieb 		err = -EINVAL;
2497038d2ef8SMaor Gottlieb 		goto destroy_ft;
2498038d2ef8SMaor Gottlieb 	}
2499038d2ef8SMaor Gottlieb 
2500038d2ef8SMaor Gottlieb 	if (IS_ERR(handler)) {
2501038d2ef8SMaor Gottlieb 		err = PTR_ERR(handler);
2502038d2ef8SMaor Gottlieb 		handler = NULL;
2503038d2ef8SMaor Gottlieb 		goto destroy_ft;
2504038d2ef8SMaor Gottlieb 	}
2505038d2ef8SMaor Gottlieb 
2506038d2ef8SMaor Gottlieb 	mutex_unlock(&dev->flow_db.lock);
2507038d2ef8SMaor Gottlieb 	kfree(dst);
2508038d2ef8SMaor Gottlieb 
2509038d2ef8SMaor Gottlieb 	return &handler->ibflow;
2510038d2ef8SMaor Gottlieb 
2511038d2ef8SMaor Gottlieb destroy_ft:
2512038d2ef8SMaor Gottlieb 	put_flow_table(dev, ft_prio, false);
2513cc0e5d42SMaor Gottlieb 	if (ft_prio_tx)
2514cc0e5d42SMaor Gottlieb 		put_flow_table(dev, ft_prio_tx, false);
2515038d2ef8SMaor Gottlieb unlock:
2516038d2ef8SMaor Gottlieb 	mutex_unlock(&dev->flow_db.lock);
2517038d2ef8SMaor Gottlieb 	kfree(dst);
2518038d2ef8SMaor Gottlieb 	kfree(handler);
2519038d2ef8SMaor Gottlieb 	return ERR_PTR(err);
2520038d2ef8SMaor Gottlieb }
2521038d2ef8SMaor Gottlieb 
2522e126ba97SEli Cohen static int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
2523e126ba97SEli Cohen {
2524e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
2525e126ba97SEli Cohen 	int err;
2526e126ba97SEli Cohen 
25279603b61dSJack Morgenstein 	err = mlx5_core_attach_mcg(dev->mdev, gid, ibqp->qp_num);
2528e126ba97SEli Cohen 	if (err)
2529e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n",
2530e126ba97SEli Cohen 			     ibqp->qp_num, gid->raw);
2531e126ba97SEli Cohen 
2532e126ba97SEli Cohen 	return err;
2533e126ba97SEli Cohen }
2534e126ba97SEli Cohen 
2535e126ba97SEli Cohen static int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
2536e126ba97SEli Cohen {
2537e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
2538e126ba97SEli Cohen 	int err;
2539e126ba97SEli Cohen 
25409603b61dSJack Morgenstein 	err = mlx5_core_detach_mcg(dev->mdev, gid, ibqp->qp_num);
2541e126ba97SEli Cohen 	if (err)
2542e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n",
2543e126ba97SEli Cohen 			     ibqp->qp_num, gid->raw);
2544e126ba97SEli Cohen 
2545e126ba97SEli Cohen 	return err;
2546e126ba97SEli Cohen }
2547e126ba97SEli Cohen 
2548e126ba97SEli Cohen static int init_node_data(struct mlx5_ib_dev *dev)
2549e126ba97SEli Cohen {
25501b5daf11SMajd Dibbiny 	int err;
2551e126ba97SEli Cohen 
25521b5daf11SMajd Dibbiny 	err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc);
2553e126ba97SEli Cohen 	if (err)
2554e126ba97SEli Cohen 		return err;
25551b5daf11SMajd Dibbiny 
25561b5daf11SMajd Dibbiny 	dev->mdev->rev_id = dev->mdev->pdev->revision;
25571b5daf11SMajd Dibbiny 
25581b5daf11SMajd Dibbiny 	return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid);
2559e126ba97SEli Cohen }
2560e126ba97SEli Cohen 
2561e126ba97SEli Cohen static ssize_t show_fw_pages(struct device *device, struct device_attribute *attr,
2562e126ba97SEli Cohen 			     char *buf)
2563e126ba97SEli Cohen {
2564e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
2565e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
2566e126ba97SEli Cohen 
25679603b61dSJack Morgenstein 	return sprintf(buf, "%d\n", dev->mdev->priv.fw_pages);
2568e126ba97SEli Cohen }
2569e126ba97SEli Cohen 
2570e126ba97SEli Cohen static ssize_t show_reg_pages(struct device *device,
2571e126ba97SEli Cohen 			      struct device_attribute *attr, char *buf)
2572e126ba97SEli Cohen {
2573e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
2574e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
2575e126ba97SEli Cohen 
25766aec21f6SHaggai Eran 	return sprintf(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages));
2577e126ba97SEli Cohen }
2578e126ba97SEli Cohen 
2579e126ba97SEli Cohen static ssize_t show_hca(struct device *device, struct device_attribute *attr,
2580e126ba97SEli Cohen 			char *buf)
2581e126ba97SEli Cohen {
2582e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
2583e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
25849603b61dSJack Morgenstein 	return sprintf(buf, "MT%d\n", dev->mdev->pdev->device);
2585e126ba97SEli Cohen }
2586e126ba97SEli Cohen 
2587e126ba97SEli Cohen static ssize_t show_rev(struct device *device, struct device_attribute *attr,
2588e126ba97SEli Cohen 			char *buf)
2589e126ba97SEli Cohen {
2590e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
2591e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
25929603b61dSJack Morgenstein 	return sprintf(buf, "%x\n", dev->mdev->rev_id);
2593e126ba97SEli Cohen }
2594e126ba97SEli Cohen 
2595e126ba97SEli Cohen static ssize_t show_board(struct device *device, struct device_attribute *attr,
2596e126ba97SEli Cohen 			  char *buf)
2597e126ba97SEli Cohen {
2598e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
2599e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
2600e126ba97SEli Cohen 	return sprintf(buf, "%.*s\n", MLX5_BOARD_ID_LEN,
26019603b61dSJack Morgenstein 		       dev->mdev->board_id);
2602e126ba97SEli Cohen }
2603e126ba97SEli Cohen 
2604e126ba97SEli Cohen static DEVICE_ATTR(hw_rev,   S_IRUGO, show_rev,    NULL);
2605e126ba97SEli Cohen static DEVICE_ATTR(hca_type, S_IRUGO, show_hca,    NULL);
2606e126ba97SEli Cohen static DEVICE_ATTR(board_id, S_IRUGO, show_board,  NULL);
2607e126ba97SEli Cohen static DEVICE_ATTR(fw_pages, S_IRUGO, show_fw_pages, NULL);
2608e126ba97SEli Cohen static DEVICE_ATTR(reg_pages, S_IRUGO, show_reg_pages, NULL);
2609e126ba97SEli Cohen 
2610e126ba97SEli Cohen static struct device_attribute *mlx5_class_attributes[] = {
2611e126ba97SEli Cohen 	&dev_attr_hw_rev,
2612e126ba97SEli Cohen 	&dev_attr_hca_type,
2613e126ba97SEli Cohen 	&dev_attr_board_id,
2614e126ba97SEli Cohen 	&dev_attr_fw_pages,
2615e126ba97SEli Cohen 	&dev_attr_reg_pages,
2616e126ba97SEli Cohen };
2617e126ba97SEli Cohen 
26187722f47eSHaggai Eran static void pkey_change_handler(struct work_struct *work)
26197722f47eSHaggai Eran {
26207722f47eSHaggai Eran 	struct mlx5_ib_port_resources *ports =
26217722f47eSHaggai Eran 		container_of(work, struct mlx5_ib_port_resources,
26227722f47eSHaggai Eran 			     pkey_change_work);
26237722f47eSHaggai Eran 
26247722f47eSHaggai Eran 	mutex_lock(&ports->devr->mutex);
26257722f47eSHaggai Eran 	mlx5_ib_gsi_pkey_change(ports->gsi);
26267722f47eSHaggai Eran 	mutex_unlock(&ports->devr->mutex);
26277722f47eSHaggai Eran }
26287722f47eSHaggai Eran 
262989ea94a7SMaor Gottlieb static void mlx5_ib_handle_internal_error(struct mlx5_ib_dev *ibdev)
263089ea94a7SMaor Gottlieb {
263189ea94a7SMaor Gottlieb 	struct mlx5_ib_qp *mqp;
263289ea94a7SMaor Gottlieb 	struct mlx5_ib_cq *send_mcq, *recv_mcq;
263389ea94a7SMaor Gottlieb 	struct mlx5_core_cq *mcq;
263489ea94a7SMaor Gottlieb 	struct list_head cq_armed_list;
263589ea94a7SMaor Gottlieb 	unsigned long flags_qp;
263689ea94a7SMaor Gottlieb 	unsigned long flags_cq;
263789ea94a7SMaor Gottlieb 	unsigned long flags;
263889ea94a7SMaor Gottlieb 
263989ea94a7SMaor Gottlieb 	INIT_LIST_HEAD(&cq_armed_list);
264089ea94a7SMaor Gottlieb 
264189ea94a7SMaor Gottlieb 	/* Go over qp list reside on that ibdev, sync with create/destroy qp.*/
264289ea94a7SMaor Gottlieb 	spin_lock_irqsave(&ibdev->reset_flow_resource_lock, flags);
264389ea94a7SMaor Gottlieb 	list_for_each_entry(mqp, &ibdev->qp_list, qps_list) {
264489ea94a7SMaor Gottlieb 		spin_lock_irqsave(&mqp->sq.lock, flags_qp);
264589ea94a7SMaor Gottlieb 		if (mqp->sq.tail != mqp->sq.head) {
264689ea94a7SMaor Gottlieb 			send_mcq = to_mcq(mqp->ibqp.send_cq);
264789ea94a7SMaor Gottlieb 			spin_lock_irqsave(&send_mcq->lock, flags_cq);
264889ea94a7SMaor Gottlieb 			if (send_mcq->mcq.comp &&
264989ea94a7SMaor Gottlieb 			    mqp->ibqp.send_cq->comp_handler) {
265089ea94a7SMaor Gottlieb 				if (!send_mcq->mcq.reset_notify_added) {
265189ea94a7SMaor Gottlieb 					send_mcq->mcq.reset_notify_added = 1;
265289ea94a7SMaor Gottlieb 					list_add_tail(&send_mcq->mcq.reset_notify,
265389ea94a7SMaor Gottlieb 						      &cq_armed_list);
265489ea94a7SMaor Gottlieb 				}
265589ea94a7SMaor Gottlieb 			}
265689ea94a7SMaor Gottlieb 			spin_unlock_irqrestore(&send_mcq->lock, flags_cq);
265789ea94a7SMaor Gottlieb 		}
265889ea94a7SMaor Gottlieb 		spin_unlock_irqrestore(&mqp->sq.lock, flags_qp);
265989ea94a7SMaor Gottlieb 		spin_lock_irqsave(&mqp->rq.lock, flags_qp);
266089ea94a7SMaor Gottlieb 		/* no handling is needed for SRQ */
266189ea94a7SMaor Gottlieb 		if (!mqp->ibqp.srq) {
266289ea94a7SMaor Gottlieb 			if (mqp->rq.tail != mqp->rq.head) {
266389ea94a7SMaor Gottlieb 				recv_mcq = to_mcq(mqp->ibqp.recv_cq);
266489ea94a7SMaor Gottlieb 				spin_lock_irqsave(&recv_mcq->lock, flags_cq);
266589ea94a7SMaor Gottlieb 				if (recv_mcq->mcq.comp &&
266689ea94a7SMaor Gottlieb 				    mqp->ibqp.recv_cq->comp_handler) {
266789ea94a7SMaor Gottlieb 					if (!recv_mcq->mcq.reset_notify_added) {
266889ea94a7SMaor Gottlieb 						recv_mcq->mcq.reset_notify_added = 1;
266989ea94a7SMaor Gottlieb 						list_add_tail(&recv_mcq->mcq.reset_notify,
267089ea94a7SMaor Gottlieb 							      &cq_armed_list);
267189ea94a7SMaor Gottlieb 					}
267289ea94a7SMaor Gottlieb 				}
267389ea94a7SMaor Gottlieb 				spin_unlock_irqrestore(&recv_mcq->lock,
267489ea94a7SMaor Gottlieb 						       flags_cq);
267589ea94a7SMaor Gottlieb 			}
267689ea94a7SMaor Gottlieb 		}
267789ea94a7SMaor Gottlieb 		spin_unlock_irqrestore(&mqp->rq.lock, flags_qp);
267889ea94a7SMaor Gottlieb 	}
267989ea94a7SMaor Gottlieb 	/*At that point all inflight post send were put to be executed as of we
268089ea94a7SMaor Gottlieb 	 * lock/unlock above locks Now need to arm all involved CQs.
268189ea94a7SMaor Gottlieb 	 */
268289ea94a7SMaor Gottlieb 	list_for_each_entry(mcq, &cq_armed_list, reset_notify) {
268389ea94a7SMaor Gottlieb 		mcq->comp(mcq);
268489ea94a7SMaor Gottlieb 	}
268589ea94a7SMaor Gottlieb 	spin_unlock_irqrestore(&ibdev->reset_flow_resource_lock, flags);
268689ea94a7SMaor Gottlieb }
268789ea94a7SMaor Gottlieb 
26889603b61dSJack Morgenstein static void mlx5_ib_event(struct mlx5_core_dev *dev, void *context,
26894d2f9bbbSJack Morgenstein 			  enum mlx5_dev_event event, unsigned long param)
2690e126ba97SEli Cohen {
26919603b61dSJack Morgenstein 	struct mlx5_ib_dev *ibdev = (struct mlx5_ib_dev *)context;
2692e126ba97SEli Cohen 	struct ib_event ibev;
2693dbaaff2aSEli Cohen 	bool fatal = false;
2694e126ba97SEli Cohen 	u8 port = 0;
2695e126ba97SEli Cohen 
2696e126ba97SEli Cohen 	switch (event) {
2697e126ba97SEli Cohen 	case MLX5_DEV_EVENT_SYS_ERROR:
2698e126ba97SEli Cohen 		ibev.event = IB_EVENT_DEVICE_FATAL;
269989ea94a7SMaor Gottlieb 		mlx5_ib_handle_internal_error(ibdev);
2700dbaaff2aSEli Cohen 		fatal = true;
2701e126ba97SEli Cohen 		break;
2702e126ba97SEli Cohen 
2703e126ba97SEli Cohen 	case MLX5_DEV_EVENT_PORT_UP:
2704e126ba97SEli Cohen 	case MLX5_DEV_EVENT_PORT_DOWN:
27052788cf3bSNoa Osherovich 	case MLX5_DEV_EVENT_PORT_INITIALIZED:
27064d2f9bbbSJack Morgenstein 		port = (u8)param;
27075ec8c83eSAviv Heller 
27085ec8c83eSAviv Heller 		/* In RoCE, port up/down events are handled in
27095ec8c83eSAviv Heller 		 * mlx5_netdev_event().
27105ec8c83eSAviv Heller 		 */
27115ec8c83eSAviv Heller 		if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) ==
27125ec8c83eSAviv Heller 			IB_LINK_LAYER_ETHERNET)
27135ec8c83eSAviv Heller 			return;
27145ec8c83eSAviv Heller 
27155ec8c83eSAviv Heller 		ibev.event = (event == MLX5_DEV_EVENT_PORT_UP) ?
27165ec8c83eSAviv Heller 			     IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
2717e126ba97SEli Cohen 		break;
2718e126ba97SEli Cohen 
2719e126ba97SEli Cohen 	case MLX5_DEV_EVENT_LID_CHANGE:
2720e126ba97SEli Cohen 		ibev.event = IB_EVENT_LID_CHANGE;
27214d2f9bbbSJack Morgenstein 		port = (u8)param;
2722e126ba97SEli Cohen 		break;
2723e126ba97SEli Cohen 
2724e126ba97SEli Cohen 	case MLX5_DEV_EVENT_PKEY_CHANGE:
2725e126ba97SEli Cohen 		ibev.event = IB_EVENT_PKEY_CHANGE;
27264d2f9bbbSJack Morgenstein 		port = (u8)param;
27277722f47eSHaggai Eran 
27287722f47eSHaggai Eran 		schedule_work(&ibdev->devr.ports[port - 1].pkey_change_work);
2729e126ba97SEli Cohen 		break;
2730e126ba97SEli Cohen 
2731e126ba97SEli Cohen 	case MLX5_DEV_EVENT_GUID_CHANGE:
2732e126ba97SEli Cohen 		ibev.event = IB_EVENT_GID_CHANGE;
27334d2f9bbbSJack Morgenstein 		port = (u8)param;
2734e126ba97SEli Cohen 		break;
2735e126ba97SEli Cohen 
2736e126ba97SEli Cohen 	case MLX5_DEV_EVENT_CLIENT_REREG:
2737e126ba97SEli Cohen 		ibev.event = IB_EVENT_CLIENT_REREGISTER;
27384d2f9bbbSJack Morgenstein 		port = (u8)param;
2739e126ba97SEli Cohen 		break;
2740bdc37924SSaeed Mahameed 	default:
2741bdc37924SSaeed Mahameed 		return;
2742e126ba97SEli Cohen 	}
2743e126ba97SEli Cohen 
2744e126ba97SEli Cohen 	ibev.device	      = &ibdev->ib_dev;
2745e126ba97SEli Cohen 	ibev.element.port_num = port;
2746e126ba97SEli Cohen 
2747a0c84c32SEli Cohen 	if (port < 1 || port > ibdev->num_ports) {
2748a0c84c32SEli Cohen 		mlx5_ib_warn(ibdev, "warning: event on port %d\n", port);
2749a0c84c32SEli Cohen 		return;
2750a0c84c32SEli Cohen 	}
2751a0c84c32SEli Cohen 
2752e126ba97SEli Cohen 	if (ibdev->ib_active)
2753e126ba97SEli Cohen 		ib_dispatch_event(&ibev);
2754dbaaff2aSEli Cohen 
2755dbaaff2aSEli Cohen 	if (fatal)
2756dbaaff2aSEli Cohen 		ibdev->ib_active = false;
2757e126ba97SEli Cohen }
2758e126ba97SEli Cohen 
2759c43f1112SMaor Gottlieb static int set_has_smi_cap(struct mlx5_ib_dev *dev)
2760c43f1112SMaor Gottlieb {
2761c43f1112SMaor Gottlieb 	struct mlx5_hca_vport_context vport_ctx;
2762c43f1112SMaor Gottlieb 	int err;
2763c43f1112SMaor Gottlieb 	int port;
2764c43f1112SMaor Gottlieb 
2765c43f1112SMaor Gottlieb 	for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++) {
2766c43f1112SMaor Gottlieb 		dev->mdev->port_caps[port - 1].has_smi = false;
2767c43f1112SMaor Gottlieb 		if (MLX5_CAP_GEN(dev->mdev, port_type) ==
2768c43f1112SMaor Gottlieb 		    MLX5_CAP_PORT_TYPE_IB) {
2769c43f1112SMaor Gottlieb 			if (MLX5_CAP_GEN(dev->mdev, ib_virt)) {
2770c43f1112SMaor Gottlieb 				err = mlx5_query_hca_vport_context(dev->mdev, 0,
2771c43f1112SMaor Gottlieb 								   port, 0,
2772c43f1112SMaor Gottlieb 								   &vport_ctx);
2773c43f1112SMaor Gottlieb 				if (err) {
2774c43f1112SMaor Gottlieb 					mlx5_ib_err(dev, "query_hca_vport_context for port=%d failed %d\n",
2775c43f1112SMaor Gottlieb 						    port, err);
2776c43f1112SMaor Gottlieb 					return err;
2777c43f1112SMaor Gottlieb 				}
2778c43f1112SMaor Gottlieb 				dev->mdev->port_caps[port - 1].has_smi =
2779c43f1112SMaor Gottlieb 					vport_ctx.has_smi;
2780c43f1112SMaor Gottlieb 			} else {
2781c43f1112SMaor Gottlieb 				dev->mdev->port_caps[port - 1].has_smi = true;
2782c43f1112SMaor Gottlieb 			}
2783c43f1112SMaor Gottlieb 		}
2784c43f1112SMaor Gottlieb 	}
2785c43f1112SMaor Gottlieb 	return 0;
2786c43f1112SMaor Gottlieb }
2787c43f1112SMaor Gottlieb 
2788e126ba97SEli Cohen static void get_ext_port_caps(struct mlx5_ib_dev *dev)
2789e126ba97SEli Cohen {
2790e126ba97SEli Cohen 	int port;
2791e126ba97SEli Cohen 
2792938fe83cSSaeed Mahameed 	for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++)
2793e126ba97SEli Cohen 		mlx5_query_ext_port_caps(dev, port);
2794e126ba97SEli Cohen }
2795e126ba97SEli Cohen 
2796e126ba97SEli Cohen static int get_port_caps(struct mlx5_ib_dev *dev)
2797e126ba97SEli Cohen {
2798e126ba97SEli Cohen 	struct ib_device_attr *dprops = NULL;
2799e126ba97SEli Cohen 	struct ib_port_attr *pprops = NULL;
2800f614fc15SDan Carpenter 	int err = -ENOMEM;
2801e126ba97SEli Cohen 	int port;
28022528e33eSMatan Barak 	struct ib_udata uhw = {.inlen = 0, .outlen = 0};
2803e126ba97SEli Cohen 
2804e126ba97SEli Cohen 	pprops = kmalloc(sizeof(*pprops), GFP_KERNEL);
2805e126ba97SEli Cohen 	if (!pprops)
2806e126ba97SEli Cohen 		goto out;
2807e126ba97SEli Cohen 
2808e126ba97SEli Cohen 	dprops = kmalloc(sizeof(*dprops), GFP_KERNEL);
2809e126ba97SEli Cohen 	if (!dprops)
2810e126ba97SEli Cohen 		goto out;
2811e126ba97SEli Cohen 
2812c43f1112SMaor Gottlieb 	err = set_has_smi_cap(dev);
2813c43f1112SMaor Gottlieb 	if (err)
2814c43f1112SMaor Gottlieb 		goto out;
2815c43f1112SMaor Gottlieb 
28162528e33eSMatan Barak 	err = mlx5_ib_query_device(&dev->ib_dev, dprops, &uhw);
2817e126ba97SEli Cohen 	if (err) {
2818e126ba97SEli Cohen 		mlx5_ib_warn(dev, "query_device failed %d\n", err);
2819e126ba97SEli Cohen 		goto out;
2820e126ba97SEli Cohen 	}
2821e126ba97SEli Cohen 
2822938fe83cSSaeed Mahameed 	for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++) {
2823c4550c63SOr Gerlitz 		memset(pprops, 0, sizeof(*pprops));
2824e126ba97SEli Cohen 		err = mlx5_ib_query_port(&dev->ib_dev, port, pprops);
2825e126ba97SEli Cohen 		if (err) {
2826938fe83cSSaeed Mahameed 			mlx5_ib_warn(dev, "query_port %d failed %d\n",
2827938fe83cSSaeed Mahameed 				     port, err);
2828e126ba97SEli Cohen 			break;
2829e126ba97SEli Cohen 		}
2830938fe83cSSaeed Mahameed 		dev->mdev->port_caps[port - 1].pkey_table_len =
2831938fe83cSSaeed Mahameed 						dprops->max_pkeys;
2832938fe83cSSaeed Mahameed 		dev->mdev->port_caps[port - 1].gid_table_len =
2833938fe83cSSaeed Mahameed 						pprops->gid_tbl_len;
2834e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "pkey_table_len %d, gid_table_len %d\n",
2835e126ba97SEli Cohen 			    dprops->max_pkeys, pprops->gid_tbl_len);
2836e126ba97SEli Cohen 	}
2837e126ba97SEli Cohen 
2838e126ba97SEli Cohen out:
2839e126ba97SEli Cohen 	kfree(pprops);
2840e126ba97SEli Cohen 	kfree(dprops);
2841e126ba97SEli Cohen 
2842e126ba97SEli Cohen 	return err;
2843e126ba97SEli Cohen }
2844e126ba97SEli Cohen 
2845e126ba97SEli Cohen static void destroy_umrc_res(struct mlx5_ib_dev *dev)
2846e126ba97SEli Cohen {
2847e126ba97SEli Cohen 	int err;
2848e126ba97SEli Cohen 
2849e126ba97SEli Cohen 	err = mlx5_mr_cache_cleanup(dev);
2850e126ba97SEli Cohen 	if (err)
2851e126ba97SEli Cohen 		mlx5_ib_warn(dev, "mr cache cleanup failed\n");
2852e126ba97SEli Cohen 
2853e126ba97SEli Cohen 	mlx5_ib_destroy_qp(dev->umrc.qp);
2854add08d76SChristoph Hellwig 	ib_free_cq(dev->umrc.cq);
2855e126ba97SEli Cohen 	ib_dealloc_pd(dev->umrc.pd);
2856e126ba97SEli Cohen }
2857e126ba97SEli Cohen 
2858e126ba97SEli Cohen enum {
2859e126ba97SEli Cohen 	MAX_UMR_WR = 128,
2860e126ba97SEli Cohen };
2861e126ba97SEli Cohen 
2862e126ba97SEli Cohen static int create_umr_res(struct mlx5_ib_dev *dev)
2863e126ba97SEli Cohen {
2864e126ba97SEli Cohen 	struct ib_qp_init_attr *init_attr = NULL;
2865e126ba97SEli Cohen 	struct ib_qp_attr *attr = NULL;
2866e126ba97SEli Cohen 	struct ib_pd *pd;
2867e126ba97SEli Cohen 	struct ib_cq *cq;
2868e126ba97SEli Cohen 	struct ib_qp *qp;
2869e126ba97SEli Cohen 	int ret;
2870e126ba97SEli Cohen 
2871e126ba97SEli Cohen 	attr = kzalloc(sizeof(*attr), GFP_KERNEL);
2872e126ba97SEli Cohen 	init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL);
2873e126ba97SEli Cohen 	if (!attr || !init_attr) {
2874e126ba97SEli Cohen 		ret = -ENOMEM;
2875e126ba97SEli Cohen 		goto error_0;
2876e126ba97SEli Cohen 	}
2877e126ba97SEli Cohen 
2878ed082d36SChristoph Hellwig 	pd = ib_alloc_pd(&dev->ib_dev, 0);
2879e126ba97SEli Cohen 	if (IS_ERR(pd)) {
2880e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create PD for sync UMR QP\n");
2881e126ba97SEli Cohen 		ret = PTR_ERR(pd);
2882e126ba97SEli Cohen 		goto error_0;
2883e126ba97SEli Cohen 	}
2884e126ba97SEli Cohen 
2885add08d76SChristoph Hellwig 	cq = ib_alloc_cq(&dev->ib_dev, NULL, 128, 0, IB_POLL_SOFTIRQ);
2886e126ba97SEli Cohen 	if (IS_ERR(cq)) {
2887e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create CQ for sync UMR QP\n");
2888e126ba97SEli Cohen 		ret = PTR_ERR(cq);
2889e126ba97SEli Cohen 		goto error_2;
2890e126ba97SEli Cohen 	}
2891e126ba97SEli Cohen 
2892e126ba97SEli Cohen 	init_attr->send_cq = cq;
2893e126ba97SEli Cohen 	init_attr->recv_cq = cq;
2894e126ba97SEli Cohen 	init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
2895e126ba97SEli Cohen 	init_attr->cap.max_send_wr = MAX_UMR_WR;
2896e126ba97SEli Cohen 	init_attr->cap.max_send_sge = 1;
2897e126ba97SEli Cohen 	init_attr->qp_type = MLX5_IB_QPT_REG_UMR;
2898e126ba97SEli Cohen 	init_attr->port_num = 1;
2899e126ba97SEli Cohen 	qp = mlx5_ib_create_qp(pd, init_attr, NULL);
2900e126ba97SEli Cohen 	if (IS_ERR(qp)) {
2901e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create sync UMR QP\n");
2902e126ba97SEli Cohen 		ret = PTR_ERR(qp);
2903e126ba97SEli Cohen 		goto error_3;
2904e126ba97SEli Cohen 	}
2905e126ba97SEli Cohen 	qp->device     = &dev->ib_dev;
2906e126ba97SEli Cohen 	qp->real_qp    = qp;
2907e126ba97SEli Cohen 	qp->uobject    = NULL;
2908e126ba97SEli Cohen 	qp->qp_type    = MLX5_IB_QPT_REG_UMR;
2909e126ba97SEli Cohen 
2910e126ba97SEli Cohen 	attr->qp_state = IB_QPS_INIT;
2911e126ba97SEli Cohen 	attr->port_num = 1;
2912e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_PKEY_INDEX |
2913e126ba97SEli Cohen 				IB_QP_PORT, NULL);
2914e126ba97SEli Cohen 	if (ret) {
2915e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify UMR QP\n");
2916e126ba97SEli Cohen 		goto error_4;
2917e126ba97SEli Cohen 	}
2918e126ba97SEli Cohen 
2919e126ba97SEli Cohen 	memset(attr, 0, sizeof(*attr));
2920e126ba97SEli Cohen 	attr->qp_state = IB_QPS_RTR;
2921e126ba97SEli Cohen 	attr->path_mtu = IB_MTU_256;
2922e126ba97SEli Cohen 
2923e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
2924e126ba97SEli Cohen 	if (ret) {
2925e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify umr QP to rtr\n");
2926e126ba97SEli Cohen 		goto error_4;
2927e126ba97SEli Cohen 	}
2928e126ba97SEli Cohen 
2929e126ba97SEli Cohen 	memset(attr, 0, sizeof(*attr));
2930e126ba97SEli Cohen 	attr->qp_state = IB_QPS_RTS;
2931e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
2932e126ba97SEli Cohen 	if (ret) {
2933e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify umr QP to rts\n");
2934e126ba97SEli Cohen 		goto error_4;
2935e126ba97SEli Cohen 	}
2936e126ba97SEli Cohen 
2937e126ba97SEli Cohen 	dev->umrc.qp = qp;
2938e126ba97SEli Cohen 	dev->umrc.cq = cq;
2939e126ba97SEli Cohen 	dev->umrc.pd = pd;
2940e126ba97SEli Cohen 
2941e126ba97SEli Cohen 	sema_init(&dev->umrc.sem, MAX_UMR_WR);
2942e126ba97SEli Cohen 	ret = mlx5_mr_cache_init(dev);
2943e126ba97SEli Cohen 	if (ret) {
2944e126ba97SEli Cohen 		mlx5_ib_warn(dev, "mr cache init failed %d\n", ret);
2945e126ba97SEli Cohen 		goto error_4;
2946e126ba97SEli Cohen 	}
2947e126ba97SEli Cohen 
2948e126ba97SEli Cohen 	kfree(attr);
2949e126ba97SEli Cohen 	kfree(init_attr);
2950e126ba97SEli Cohen 
2951e126ba97SEli Cohen 	return 0;
2952e126ba97SEli Cohen 
2953e126ba97SEli Cohen error_4:
2954e126ba97SEli Cohen 	mlx5_ib_destroy_qp(qp);
2955e126ba97SEli Cohen 
2956e126ba97SEli Cohen error_3:
2957add08d76SChristoph Hellwig 	ib_free_cq(cq);
2958e126ba97SEli Cohen 
2959e126ba97SEli Cohen error_2:
2960e126ba97SEli Cohen 	ib_dealloc_pd(pd);
2961e126ba97SEli Cohen 
2962e126ba97SEli Cohen error_0:
2963e126ba97SEli Cohen 	kfree(attr);
2964e126ba97SEli Cohen 	kfree(init_attr);
2965e126ba97SEli Cohen 	return ret;
2966e126ba97SEli Cohen }
2967e126ba97SEli Cohen 
29686e8484c5SMax Gurtovoy static u8 mlx5_get_umr_fence(u8 umr_fence_cap)
29696e8484c5SMax Gurtovoy {
29706e8484c5SMax Gurtovoy 	switch (umr_fence_cap) {
29716e8484c5SMax Gurtovoy 	case MLX5_CAP_UMR_FENCE_NONE:
29726e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_NONE;
29736e8484c5SMax Gurtovoy 	case MLX5_CAP_UMR_FENCE_SMALL:
29746e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_INITIATOR_SMALL;
29756e8484c5SMax Gurtovoy 	default:
29766e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_STRONG_ORDERING;
29776e8484c5SMax Gurtovoy 	}
29786e8484c5SMax Gurtovoy }
29796e8484c5SMax Gurtovoy 
2980e126ba97SEli Cohen static int create_dev_resources(struct mlx5_ib_resources *devr)
2981e126ba97SEli Cohen {
2982e126ba97SEli Cohen 	struct ib_srq_init_attr attr;
2983e126ba97SEli Cohen 	struct mlx5_ib_dev *dev;
2984bcf4c1eaSMatan Barak 	struct ib_cq_init_attr cq_attr = {.cqe = 1};
29857722f47eSHaggai Eran 	int port;
2986e126ba97SEli Cohen 	int ret = 0;
2987e126ba97SEli Cohen 
2988e126ba97SEli Cohen 	dev = container_of(devr, struct mlx5_ib_dev, devr);
2989e126ba97SEli Cohen 
2990d16e91daSHaggai Eran 	mutex_init(&devr->mutex);
2991d16e91daSHaggai Eran 
2992e126ba97SEli Cohen 	devr->p0 = mlx5_ib_alloc_pd(&dev->ib_dev, NULL, NULL);
2993e126ba97SEli Cohen 	if (IS_ERR(devr->p0)) {
2994e126ba97SEli Cohen 		ret = PTR_ERR(devr->p0);
2995e126ba97SEli Cohen 		goto error0;
2996e126ba97SEli Cohen 	}
2997e126ba97SEli Cohen 	devr->p0->device  = &dev->ib_dev;
2998e126ba97SEli Cohen 	devr->p0->uobject = NULL;
2999e126ba97SEli Cohen 	atomic_set(&devr->p0->usecnt, 0);
3000e126ba97SEli Cohen 
3001bcf4c1eaSMatan Barak 	devr->c0 = mlx5_ib_create_cq(&dev->ib_dev, &cq_attr, NULL, NULL);
3002e126ba97SEli Cohen 	if (IS_ERR(devr->c0)) {
3003e126ba97SEli Cohen 		ret = PTR_ERR(devr->c0);
3004e126ba97SEli Cohen 		goto error1;
3005e126ba97SEli Cohen 	}
3006e126ba97SEli Cohen 	devr->c0->device        = &dev->ib_dev;
3007e126ba97SEli Cohen 	devr->c0->uobject       = NULL;
3008e126ba97SEli Cohen 	devr->c0->comp_handler  = NULL;
3009e126ba97SEli Cohen 	devr->c0->event_handler = NULL;
3010e126ba97SEli Cohen 	devr->c0->cq_context    = NULL;
3011e126ba97SEli Cohen 	atomic_set(&devr->c0->usecnt, 0);
3012e126ba97SEli Cohen 
3013e126ba97SEli Cohen 	devr->x0 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
3014e126ba97SEli Cohen 	if (IS_ERR(devr->x0)) {
3015e126ba97SEli Cohen 		ret = PTR_ERR(devr->x0);
3016e126ba97SEli Cohen 		goto error2;
3017e126ba97SEli Cohen 	}
3018e126ba97SEli Cohen 	devr->x0->device = &dev->ib_dev;
3019e126ba97SEli Cohen 	devr->x0->inode = NULL;
3020e126ba97SEli Cohen 	atomic_set(&devr->x0->usecnt, 0);
3021e126ba97SEli Cohen 	mutex_init(&devr->x0->tgt_qp_mutex);
3022e126ba97SEli Cohen 	INIT_LIST_HEAD(&devr->x0->tgt_qp_list);
3023e126ba97SEli Cohen 
3024e126ba97SEli Cohen 	devr->x1 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
3025e126ba97SEli Cohen 	if (IS_ERR(devr->x1)) {
3026e126ba97SEli Cohen 		ret = PTR_ERR(devr->x1);
3027e126ba97SEli Cohen 		goto error3;
3028e126ba97SEli Cohen 	}
3029e126ba97SEli Cohen 	devr->x1->device = &dev->ib_dev;
3030e126ba97SEli Cohen 	devr->x1->inode = NULL;
3031e126ba97SEli Cohen 	atomic_set(&devr->x1->usecnt, 0);
3032e126ba97SEli Cohen 	mutex_init(&devr->x1->tgt_qp_mutex);
3033e126ba97SEli Cohen 	INIT_LIST_HEAD(&devr->x1->tgt_qp_list);
3034e126ba97SEli Cohen 
3035e126ba97SEli Cohen 	memset(&attr, 0, sizeof(attr));
3036e126ba97SEli Cohen 	attr.attr.max_sge = 1;
3037e126ba97SEli Cohen 	attr.attr.max_wr = 1;
3038e126ba97SEli Cohen 	attr.srq_type = IB_SRQT_XRC;
3039e126ba97SEli Cohen 	attr.ext.xrc.cq = devr->c0;
3040e126ba97SEli Cohen 	attr.ext.xrc.xrcd = devr->x0;
3041e126ba97SEli Cohen 
3042e126ba97SEli Cohen 	devr->s0 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
3043e126ba97SEli Cohen 	if (IS_ERR(devr->s0)) {
3044e126ba97SEli Cohen 		ret = PTR_ERR(devr->s0);
3045e126ba97SEli Cohen 		goto error4;
3046e126ba97SEli Cohen 	}
3047e126ba97SEli Cohen 	devr->s0->device	= &dev->ib_dev;
3048e126ba97SEli Cohen 	devr->s0->pd		= devr->p0;
3049e126ba97SEli Cohen 	devr->s0->uobject       = NULL;
3050e126ba97SEli Cohen 	devr->s0->event_handler = NULL;
3051e126ba97SEli Cohen 	devr->s0->srq_context   = NULL;
3052e126ba97SEli Cohen 	devr->s0->srq_type      = IB_SRQT_XRC;
3053e126ba97SEli Cohen 	devr->s0->ext.xrc.xrcd	= devr->x0;
3054e126ba97SEli Cohen 	devr->s0->ext.xrc.cq	= devr->c0;
3055e126ba97SEli Cohen 	atomic_inc(&devr->s0->ext.xrc.xrcd->usecnt);
3056e126ba97SEli Cohen 	atomic_inc(&devr->s0->ext.xrc.cq->usecnt);
3057e126ba97SEli Cohen 	atomic_inc(&devr->p0->usecnt);
3058e126ba97SEli Cohen 	atomic_set(&devr->s0->usecnt, 0);
3059e126ba97SEli Cohen 
30604aa17b28SHaggai Abramonvsky 	memset(&attr, 0, sizeof(attr));
30614aa17b28SHaggai Abramonvsky 	attr.attr.max_sge = 1;
30624aa17b28SHaggai Abramonvsky 	attr.attr.max_wr = 1;
30634aa17b28SHaggai Abramonvsky 	attr.srq_type = IB_SRQT_BASIC;
30644aa17b28SHaggai Abramonvsky 	devr->s1 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
30654aa17b28SHaggai Abramonvsky 	if (IS_ERR(devr->s1)) {
30664aa17b28SHaggai Abramonvsky 		ret = PTR_ERR(devr->s1);
30674aa17b28SHaggai Abramonvsky 		goto error5;
30684aa17b28SHaggai Abramonvsky 	}
30694aa17b28SHaggai Abramonvsky 	devr->s1->device	= &dev->ib_dev;
30704aa17b28SHaggai Abramonvsky 	devr->s1->pd		= devr->p0;
30714aa17b28SHaggai Abramonvsky 	devr->s1->uobject       = NULL;
30724aa17b28SHaggai Abramonvsky 	devr->s1->event_handler = NULL;
30734aa17b28SHaggai Abramonvsky 	devr->s1->srq_context   = NULL;
30744aa17b28SHaggai Abramonvsky 	devr->s1->srq_type      = IB_SRQT_BASIC;
30754aa17b28SHaggai Abramonvsky 	devr->s1->ext.xrc.cq	= devr->c0;
30764aa17b28SHaggai Abramonvsky 	atomic_inc(&devr->p0->usecnt);
30774aa17b28SHaggai Abramonvsky 	atomic_set(&devr->s0->usecnt, 0);
30784aa17b28SHaggai Abramonvsky 
30797722f47eSHaggai Eran 	for (port = 0; port < ARRAY_SIZE(devr->ports); ++port) {
30807722f47eSHaggai Eran 		INIT_WORK(&devr->ports[port].pkey_change_work,
30817722f47eSHaggai Eran 			  pkey_change_handler);
30827722f47eSHaggai Eran 		devr->ports[port].devr = devr;
30837722f47eSHaggai Eran 	}
30847722f47eSHaggai Eran 
3085e126ba97SEli Cohen 	return 0;
3086e126ba97SEli Cohen 
30874aa17b28SHaggai Abramonvsky error5:
30884aa17b28SHaggai Abramonvsky 	mlx5_ib_destroy_srq(devr->s0);
3089e126ba97SEli Cohen error4:
3090e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x1);
3091e126ba97SEli Cohen error3:
3092e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x0);
3093e126ba97SEli Cohen error2:
3094e126ba97SEli Cohen 	mlx5_ib_destroy_cq(devr->c0);
3095e126ba97SEli Cohen error1:
3096e126ba97SEli Cohen 	mlx5_ib_dealloc_pd(devr->p0);
3097e126ba97SEli Cohen error0:
3098e126ba97SEli Cohen 	return ret;
3099e126ba97SEli Cohen }
3100e126ba97SEli Cohen 
3101e126ba97SEli Cohen static void destroy_dev_resources(struct mlx5_ib_resources *devr)
3102e126ba97SEli Cohen {
31037722f47eSHaggai Eran 	struct mlx5_ib_dev *dev =
31047722f47eSHaggai Eran 		container_of(devr, struct mlx5_ib_dev, devr);
31057722f47eSHaggai Eran 	int port;
31067722f47eSHaggai Eran 
31074aa17b28SHaggai Abramonvsky 	mlx5_ib_destroy_srq(devr->s1);
3108e126ba97SEli Cohen 	mlx5_ib_destroy_srq(devr->s0);
3109e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x0);
3110e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x1);
3111e126ba97SEli Cohen 	mlx5_ib_destroy_cq(devr->c0);
3112e126ba97SEli Cohen 	mlx5_ib_dealloc_pd(devr->p0);
31137722f47eSHaggai Eran 
31147722f47eSHaggai Eran 	/* Make sure no change P_Key work items are still executing */
31157722f47eSHaggai Eran 	for (port = 0; port < dev->num_ports; ++port)
31167722f47eSHaggai Eran 		cancel_work_sync(&devr->ports[port].pkey_change_work);
3117e126ba97SEli Cohen }
3118e126ba97SEli Cohen 
3119e53505a8SAchiad Shochat static u32 get_core_cap_flags(struct ib_device *ibdev)
3120e53505a8SAchiad Shochat {
3121e53505a8SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
3122e53505a8SAchiad Shochat 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, 1);
3123e53505a8SAchiad Shochat 	u8 l3_type_cap = MLX5_CAP_ROCE(dev->mdev, l3_type);
3124e53505a8SAchiad Shochat 	u8 roce_version_cap = MLX5_CAP_ROCE(dev->mdev, roce_version);
3125e53505a8SAchiad Shochat 	u32 ret = 0;
3126e53505a8SAchiad Shochat 
3127e53505a8SAchiad Shochat 	if (ll == IB_LINK_LAYER_INFINIBAND)
3128e53505a8SAchiad Shochat 		return RDMA_CORE_PORT_IBA_IB;
3129e53505a8SAchiad Shochat 
313072cd5717SOr Gerlitz 	ret = RDMA_CORE_PORT_RAW_PACKET;
313172cd5717SOr Gerlitz 
3132e53505a8SAchiad Shochat 	if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV4_CAP))
313372cd5717SOr Gerlitz 		return ret;
3134e53505a8SAchiad Shochat 
3135e53505a8SAchiad Shochat 	if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV6_CAP))
313672cd5717SOr Gerlitz 		return ret;
3137e53505a8SAchiad Shochat 
3138e53505a8SAchiad Shochat 	if (roce_version_cap & MLX5_ROCE_VERSION_1_CAP)
3139e53505a8SAchiad Shochat 		ret |= RDMA_CORE_PORT_IBA_ROCE;
3140e53505a8SAchiad Shochat 
3141e53505a8SAchiad Shochat 	if (roce_version_cap & MLX5_ROCE_VERSION_2_CAP)
3142e53505a8SAchiad Shochat 		ret |= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
3143e53505a8SAchiad Shochat 
3144e53505a8SAchiad Shochat 	return ret;
3145e53505a8SAchiad Shochat }
3146e53505a8SAchiad Shochat 
31477738613eSIra Weiny static int mlx5_port_immutable(struct ib_device *ibdev, u8 port_num,
31487738613eSIra Weiny 			       struct ib_port_immutable *immutable)
31497738613eSIra Weiny {
31507738613eSIra Weiny 	struct ib_port_attr attr;
3151ca5b91d6SOr Gerlitz 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
3152ca5b91d6SOr Gerlitz 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, port_num);
31537738613eSIra Weiny 	int err;
31547738613eSIra Weiny 
3155c4550c63SOr Gerlitz 	immutable->core_cap_flags = get_core_cap_flags(ibdev);
3156c4550c63SOr Gerlitz 
3157c4550c63SOr Gerlitz 	err = ib_query_port(ibdev, port_num, &attr);
31587738613eSIra Weiny 	if (err)
31597738613eSIra Weiny 		return err;
31607738613eSIra Weiny 
31617738613eSIra Weiny 	immutable->pkey_tbl_len = attr.pkey_tbl_len;
31627738613eSIra Weiny 	immutable->gid_tbl_len = attr.gid_tbl_len;
3163e53505a8SAchiad Shochat 	immutable->core_cap_flags = get_core_cap_flags(ibdev);
3164ca5b91d6SOr Gerlitz 	if ((ll == IB_LINK_LAYER_INFINIBAND) || MLX5_CAP_GEN(dev->mdev, roce))
3165337877a4SIra Weiny 		immutable->max_mad_size = IB_MGMT_MAD_SIZE;
31667738613eSIra Weiny 
31677738613eSIra Weiny 	return 0;
31687738613eSIra Weiny }
31697738613eSIra Weiny 
3170c7342823SIra Weiny static void get_dev_fw_str(struct ib_device *ibdev, char *str,
3171c7342823SIra Weiny 			   size_t str_len)
3172c7342823SIra Weiny {
3173c7342823SIra Weiny 	struct mlx5_ib_dev *dev =
3174c7342823SIra Weiny 		container_of(ibdev, struct mlx5_ib_dev, ib_dev);
3175c7342823SIra Weiny 	snprintf(str, str_len, "%d.%d.%04d", fw_rev_maj(dev->mdev),
3176c7342823SIra Weiny 		       fw_rev_min(dev->mdev), fw_rev_sub(dev->mdev));
3177c7342823SIra Weiny }
3178c7342823SIra Weiny 
317945f95acdSOr Gerlitz static int mlx5_eth_lag_init(struct mlx5_ib_dev *dev)
31809ef9c640SAviv Heller {
31819ef9c640SAviv Heller 	struct mlx5_core_dev *mdev = dev->mdev;
31829ef9c640SAviv Heller 	struct mlx5_flow_namespace *ns = mlx5_get_flow_namespace(mdev,
31839ef9c640SAviv Heller 								 MLX5_FLOW_NAMESPACE_LAG);
31849ef9c640SAviv Heller 	struct mlx5_flow_table *ft;
31859ef9c640SAviv Heller 	int err;
31869ef9c640SAviv Heller 
31879ef9c640SAviv Heller 	if (!ns || !mlx5_lag_is_active(mdev))
31889ef9c640SAviv Heller 		return 0;
31899ef9c640SAviv Heller 
31909ef9c640SAviv Heller 	err = mlx5_cmd_create_vport_lag(mdev);
31919ef9c640SAviv Heller 	if (err)
31929ef9c640SAviv Heller 		return err;
31939ef9c640SAviv Heller 
31949ef9c640SAviv Heller 	ft = mlx5_create_lag_demux_flow_table(ns, 0, 0);
31959ef9c640SAviv Heller 	if (IS_ERR(ft)) {
31969ef9c640SAviv Heller 		err = PTR_ERR(ft);
31979ef9c640SAviv Heller 		goto err_destroy_vport_lag;
31989ef9c640SAviv Heller 	}
31999ef9c640SAviv Heller 
32009ef9c640SAviv Heller 	dev->flow_db.lag_demux_ft = ft;
32019ef9c640SAviv Heller 	return 0;
32029ef9c640SAviv Heller 
32039ef9c640SAviv Heller err_destroy_vport_lag:
32049ef9c640SAviv Heller 	mlx5_cmd_destroy_vport_lag(mdev);
32059ef9c640SAviv Heller 	return err;
32069ef9c640SAviv Heller }
32079ef9c640SAviv Heller 
320845f95acdSOr Gerlitz static void mlx5_eth_lag_cleanup(struct mlx5_ib_dev *dev)
32099ef9c640SAviv Heller {
32109ef9c640SAviv Heller 	struct mlx5_core_dev *mdev = dev->mdev;
32119ef9c640SAviv Heller 
32129ef9c640SAviv Heller 	if (dev->flow_db.lag_demux_ft) {
32139ef9c640SAviv Heller 		mlx5_destroy_flow_table(dev->flow_db.lag_demux_ft);
32149ef9c640SAviv Heller 		dev->flow_db.lag_demux_ft = NULL;
32159ef9c640SAviv Heller 
32169ef9c640SAviv Heller 		mlx5_cmd_destroy_vport_lag(mdev);
32179ef9c640SAviv Heller 	}
32189ef9c640SAviv Heller }
32199ef9c640SAviv Heller 
3220d012f5d6SOr Gerlitz static int mlx5_add_netdev_notifier(struct mlx5_ib_dev *dev)
3221fc24fc5eSAchiad Shochat {
3222e53505a8SAchiad Shochat 	int err;
3223e53505a8SAchiad Shochat 
3224fc24fc5eSAchiad Shochat 	dev->roce.nb.notifier_call = mlx5_netdev_event;
3225e53505a8SAchiad Shochat 	err = register_netdevice_notifier(&dev->roce.nb);
32265ec8c83eSAviv Heller 	if (err) {
32275ec8c83eSAviv Heller 		dev->roce.nb.notifier_call = NULL;
3228e53505a8SAchiad Shochat 		return err;
32295ec8c83eSAviv Heller 	}
3230e53505a8SAchiad Shochat 
3231d012f5d6SOr Gerlitz 	return 0;
3232d012f5d6SOr Gerlitz }
3233d012f5d6SOr Gerlitz 
3234d012f5d6SOr Gerlitz static void mlx5_remove_netdev_notifier(struct mlx5_ib_dev *dev)
3235e126ba97SEli Cohen {
3236e126ba97SEli Cohen 	if (dev->roce.nb.notifier_call) {
3237e126ba97SEli Cohen 		unregister_netdevice_notifier(&dev->roce.nb);
3238e126ba97SEli Cohen 		dev->roce.nb.notifier_call = NULL;
3239e126ba97SEli Cohen 	}
3240e126ba97SEli Cohen }
3241e126ba97SEli Cohen 
324245f95acdSOr Gerlitz static int mlx5_enable_eth(struct mlx5_ib_dev *dev)
3243e126ba97SEli Cohen {
3244e126ba97SEli Cohen 	int err;
3245e126ba97SEli Cohen 
3246d012f5d6SOr Gerlitz 	err = mlx5_add_netdev_notifier(dev);
3247d012f5d6SOr Gerlitz 	if (err)
3248e53505a8SAchiad Shochat 		return err;
3249e53505a8SAchiad Shochat 
3250ca5b91d6SOr Gerlitz 	if (MLX5_CAP_GEN(dev->mdev, roce)) {
3251e53505a8SAchiad Shochat 		err = mlx5_nic_vport_enable_roce(dev->mdev);
3252e53505a8SAchiad Shochat 		if (err)
3253e53505a8SAchiad Shochat 			goto err_unregister_netdevice_notifier;
3254ca5b91d6SOr Gerlitz 	}
3255e53505a8SAchiad Shochat 
325645f95acdSOr Gerlitz 	err = mlx5_eth_lag_init(dev);
32579ef9c640SAviv Heller 	if (err)
32589ef9c640SAviv Heller 		goto err_disable_roce;
32599ef9c640SAviv Heller 
3260e53505a8SAchiad Shochat 	return 0;
3261e53505a8SAchiad Shochat 
32629ef9c640SAviv Heller err_disable_roce:
3263ca5b91d6SOr Gerlitz 	if (MLX5_CAP_GEN(dev->mdev, roce))
32649ef9c640SAviv Heller 		mlx5_nic_vport_disable_roce(dev->mdev);
32659ef9c640SAviv Heller 
3266e53505a8SAchiad Shochat err_unregister_netdevice_notifier:
3267d012f5d6SOr Gerlitz 	mlx5_remove_netdev_notifier(dev);
3268e53505a8SAchiad Shochat 	return err;
3269fc24fc5eSAchiad Shochat }
3270fc24fc5eSAchiad Shochat 
327145f95acdSOr Gerlitz static void mlx5_disable_eth(struct mlx5_ib_dev *dev)
3272fc24fc5eSAchiad Shochat {
327345f95acdSOr Gerlitz 	mlx5_eth_lag_cleanup(dev);
3274ca5b91d6SOr Gerlitz 	if (MLX5_CAP_GEN(dev->mdev, roce))
3275e53505a8SAchiad Shochat 		mlx5_nic_vport_disable_roce(dev->mdev);
3276fc24fc5eSAchiad Shochat }
3277fc24fc5eSAchiad Shochat 
3278e1f24a79SParav Pandit struct mlx5_ib_counter {
32797c16f477SKamal Heib 	const char *name;
32807c16f477SKamal Heib 	size_t offset;
32817c16f477SKamal Heib };
32827c16f477SKamal Heib 
32837c16f477SKamal Heib #define INIT_Q_COUNTER(_name)		\
32847c16f477SKamal Heib 	{ .name = #_name, .offset = MLX5_BYTE_OFF(query_q_counter_out, _name)}
32857c16f477SKamal Heib 
3286e1f24a79SParav Pandit static const struct mlx5_ib_counter basic_q_cnts[] = {
32877c16f477SKamal Heib 	INIT_Q_COUNTER(rx_write_requests),
32887c16f477SKamal Heib 	INIT_Q_COUNTER(rx_read_requests),
32897c16f477SKamal Heib 	INIT_Q_COUNTER(rx_atomic_requests),
32907c16f477SKamal Heib 	INIT_Q_COUNTER(out_of_buffer),
32917c16f477SKamal Heib };
32927c16f477SKamal Heib 
3293e1f24a79SParav Pandit static const struct mlx5_ib_counter out_of_seq_q_cnts[] = {
32947c16f477SKamal Heib 	INIT_Q_COUNTER(out_of_sequence),
32957c16f477SKamal Heib };
32967c16f477SKamal Heib 
3297e1f24a79SParav Pandit static const struct mlx5_ib_counter retrans_q_cnts[] = {
32987c16f477SKamal Heib 	INIT_Q_COUNTER(duplicate_request),
32997c16f477SKamal Heib 	INIT_Q_COUNTER(rnr_nak_retry_err),
33007c16f477SKamal Heib 	INIT_Q_COUNTER(packet_seq_err),
33017c16f477SKamal Heib 	INIT_Q_COUNTER(implied_nak_seq_err),
33027c16f477SKamal Heib 	INIT_Q_COUNTER(local_ack_timeout_err),
33037c16f477SKamal Heib };
33047c16f477SKamal Heib 
3305e1f24a79SParav Pandit #define INIT_CONG_COUNTER(_name)		\
3306e1f24a79SParav Pandit 	{ .name = #_name, .offset =	\
3307e1f24a79SParav Pandit 		MLX5_BYTE_OFF(query_cong_statistics_out, _name ## _high)}
3308e1f24a79SParav Pandit 
3309e1f24a79SParav Pandit static const struct mlx5_ib_counter cong_cnts[] = {
3310e1f24a79SParav Pandit 	INIT_CONG_COUNTER(rp_cnp_ignored),
3311e1f24a79SParav Pandit 	INIT_CONG_COUNTER(rp_cnp_handled),
3312e1f24a79SParav Pandit 	INIT_CONG_COUNTER(np_ecn_marked_roce_packets),
3313e1f24a79SParav Pandit 	INIT_CONG_COUNTER(np_cnp_sent),
3314e1f24a79SParav Pandit };
3315e1f24a79SParav Pandit 
3316e1f24a79SParav Pandit static void mlx5_ib_dealloc_counters(struct mlx5_ib_dev *dev)
33170837e86aSMark Bloch {
33180837e86aSMark Bloch 	unsigned int i;
33190837e86aSMark Bloch 
33207c16f477SKamal Heib 	for (i = 0; i < dev->num_ports; i++) {
33210837e86aSMark Bloch 		mlx5_core_dealloc_q_counter(dev->mdev,
3322e1f24a79SParav Pandit 					    dev->port[i].cnts.set_id);
3323e1f24a79SParav Pandit 		kfree(dev->port[i].cnts.names);
3324e1f24a79SParav Pandit 		kfree(dev->port[i].cnts.offsets);
33257c16f477SKamal Heib 	}
33267c16f477SKamal Heib }
33277c16f477SKamal Heib 
3328e1f24a79SParav Pandit static int __mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev,
3329e1f24a79SParav Pandit 				    struct mlx5_ib_counters *cnts)
33307c16f477SKamal Heib {
33317c16f477SKamal Heib 	u32 num_counters;
33327c16f477SKamal Heib 
33337c16f477SKamal Heib 	num_counters = ARRAY_SIZE(basic_q_cnts);
33347c16f477SKamal Heib 
33357c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt))
33367c16f477SKamal Heib 		num_counters += ARRAY_SIZE(out_of_seq_q_cnts);
33377c16f477SKamal Heib 
33387c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters))
33397c16f477SKamal Heib 		num_counters += ARRAY_SIZE(retrans_q_cnts);
3340e1f24a79SParav Pandit 	cnts->num_q_counters = num_counters;
33417c16f477SKamal Heib 
3342e1f24a79SParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
3343e1f24a79SParav Pandit 		cnts->num_cong_counters = ARRAY_SIZE(cong_cnts);
3344e1f24a79SParav Pandit 		num_counters += ARRAY_SIZE(cong_cnts);
3345e1f24a79SParav Pandit 	}
3346e1f24a79SParav Pandit 
3347e1f24a79SParav Pandit 	cnts->names = kcalloc(num_counters, sizeof(cnts->names), GFP_KERNEL);
3348e1f24a79SParav Pandit 	if (!cnts->names)
33497c16f477SKamal Heib 		return -ENOMEM;
33507c16f477SKamal Heib 
3351e1f24a79SParav Pandit 	cnts->offsets = kcalloc(num_counters,
3352e1f24a79SParav Pandit 				sizeof(cnts->offsets), GFP_KERNEL);
3353e1f24a79SParav Pandit 	if (!cnts->offsets)
33547c16f477SKamal Heib 		goto err_names;
33557c16f477SKamal Heib 
33567c16f477SKamal Heib 	return 0;
33577c16f477SKamal Heib 
33587c16f477SKamal Heib err_names:
3359e1f24a79SParav Pandit 	kfree(cnts->names);
33607c16f477SKamal Heib 	return -ENOMEM;
33617c16f477SKamal Heib }
33627c16f477SKamal Heib 
3363e1f24a79SParav Pandit static void mlx5_ib_fill_counters(struct mlx5_ib_dev *dev,
33647c16f477SKamal Heib 				  const char **names,
33657c16f477SKamal Heib 				  size_t *offsets)
33667c16f477SKamal Heib {
33677c16f477SKamal Heib 	int i;
33687c16f477SKamal Heib 	int j = 0;
33697c16f477SKamal Heib 
33707c16f477SKamal Heib 	for (i = 0; i < ARRAY_SIZE(basic_q_cnts); i++, j++) {
33717c16f477SKamal Heib 		names[j] = basic_q_cnts[i].name;
33727c16f477SKamal Heib 		offsets[j] = basic_q_cnts[i].offset;
33737c16f477SKamal Heib 	}
33747c16f477SKamal Heib 
33757c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt)) {
33767c16f477SKamal Heib 		for (i = 0; i < ARRAY_SIZE(out_of_seq_q_cnts); i++, j++) {
33777c16f477SKamal Heib 			names[j] = out_of_seq_q_cnts[i].name;
33787c16f477SKamal Heib 			offsets[j] = out_of_seq_q_cnts[i].offset;
33797c16f477SKamal Heib 		}
33807c16f477SKamal Heib 	}
33817c16f477SKamal Heib 
33827c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters)) {
33837c16f477SKamal Heib 		for (i = 0; i < ARRAY_SIZE(retrans_q_cnts); i++, j++) {
33847c16f477SKamal Heib 			names[j] = retrans_q_cnts[i].name;
33857c16f477SKamal Heib 			offsets[j] = retrans_q_cnts[i].offset;
33867c16f477SKamal Heib 		}
33877c16f477SKamal Heib 	}
3388e1f24a79SParav Pandit 
3389e1f24a79SParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
3390e1f24a79SParav Pandit 		for (i = 0; i < ARRAY_SIZE(cong_cnts); i++, j++) {
3391e1f24a79SParav Pandit 			names[j] = cong_cnts[i].name;
3392e1f24a79SParav Pandit 			offsets[j] = cong_cnts[i].offset;
3393e1f24a79SParav Pandit 		}
3394e1f24a79SParav Pandit 	}
33950837e86aSMark Bloch }
33960837e86aSMark Bloch 
3397e1f24a79SParav Pandit static int mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev)
33980837e86aSMark Bloch {
33990837e86aSMark Bloch 	int i;
34000837e86aSMark Bloch 	int ret;
34010837e86aSMark Bloch 
34020837e86aSMark Bloch 	for (i = 0; i < dev->num_ports; i++) {
34037c16f477SKamal Heib 		struct mlx5_ib_port *port = &dev->port[i];
34047c16f477SKamal Heib 
34050837e86aSMark Bloch 		ret = mlx5_core_alloc_q_counter(dev->mdev,
3406e1f24a79SParav Pandit 						&port->cnts.set_id);
34070837e86aSMark Bloch 		if (ret) {
34080837e86aSMark Bloch 			mlx5_ib_warn(dev,
34090837e86aSMark Bloch 				     "couldn't allocate queue counter for port %d, err %d\n",
34100837e86aSMark Bloch 				     i + 1, ret);
34110837e86aSMark Bloch 			goto dealloc_counters;
34120837e86aSMark Bloch 		}
34137c16f477SKamal Heib 
3414e1f24a79SParav Pandit 		ret = __mlx5_ib_alloc_counters(dev, &port->cnts);
34157c16f477SKamal Heib 		if (ret)
34167c16f477SKamal Heib 			goto dealloc_counters;
34177c16f477SKamal Heib 
3418e1f24a79SParav Pandit 		mlx5_ib_fill_counters(dev, port->cnts.names,
3419e1f24a79SParav Pandit 				      port->cnts.offsets);
34200837e86aSMark Bloch 	}
34210837e86aSMark Bloch 
34220837e86aSMark Bloch 	return 0;
34230837e86aSMark Bloch 
34240837e86aSMark Bloch dealloc_counters:
34250837e86aSMark Bloch 	while (--i >= 0)
34260837e86aSMark Bloch 		mlx5_core_dealloc_q_counter(dev->mdev,
3427e1f24a79SParav Pandit 					    dev->port[i].cnts.set_id);
34280837e86aSMark Bloch 
34290837e86aSMark Bloch 	return ret;
34300837e86aSMark Bloch }
34310837e86aSMark Bloch 
34320ad17a8fSMark Bloch static struct rdma_hw_stats *mlx5_ib_alloc_hw_stats(struct ib_device *ibdev,
34330ad17a8fSMark Bloch 						    u8 port_num)
34340ad17a8fSMark Bloch {
34357c16f477SKamal Heib 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
34367c16f477SKamal Heib 	struct mlx5_ib_port *port = &dev->port[port_num - 1];
34370ad17a8fSMark Bloch 
34380ad17a8fSMark Bloch 	/* We support only per port stats */
34390ad17a8fSMark Bloch 	if (port_num == 0)
34400ad17a8fSMark Bloch 		return NULL;
34410ad17a8fSMark Bloch 
3442e1f24a79SParav Pandit 	return rdma_alloc_hw_stats_struct(port->cnts.names,
3443e1f24a79SParav Pandit 					  port->cnts.num_q_counters +
3444e1f24a79SParav Pandit 					  port->cnts.num_cong_counters,
34450ad17a8fSMark Bloch 					  RDMA_HW_STATS_DEFAULT_LIFESPAN);
34460ad17a8fSMark Bloch }
34470ad17a8fSMark Bloch 
3448e1f24a79SParav Pandit static int mlx5_ib_query_q_counters(struct mlx5_ib_dev *dev,
3449e1f24a79SParav Pandit 				    struct mlx5_ib_port *port,
3450e1f24a79SParav Pandit 				    struct rdma_hw_stats *stats)
3451e1f24a79SParav Pandit {
3452e1f24a79SParav Pandit 	int outlen = MLX5_ST_SZ_BYTES(query_q_counter_out);
3453e1f24a79SParav Pandit 	void *out;
3454e1f24a79SParav Pandit 	__be32 val;
3455e1f24a79SParav Pandit 	int ret, i;
3456e1f24a79SParav Pandit 
34571b9a07eeSLeon Romanovsky 	out = kvzalloc(outlen, GFP_KERNEL);
3458e1f24a79SParav Pandit 	if (!out)
3459e1f24a79SParav Pandit 		return -ENOMEM;
3460e1f24a79SParav Pandit 
3461e1f24a79SParav Pandit 	ret = mlx5_core_query_q_counter(dev->mdev,
3462e1f24a79SParav Pandit 					port->cnts.set_id, 0,
3463e1f24a79SParav Pandit 					out, outlen);
3464e1f24a79SParav Pandit 	if (ret)
3465e1f24a79SParav Pandit 		goto free;
3466e1f24a79SParav Pandit 
3467e1f24a79SParav Pandit 	for (i = 0; i < port->cnts.num_q_counters; i++) {
3468e1f24a79SParav Pandit 		val = *(__be32 *)(out + port->cnts.offsets[i]);
3469e1f24a79SParav Pandit 		stats->value[i] = (u64)be32_to_cpu(val);
3470e1f24a79SParav Pandit 	}
3471e1f24a79SParav Pandit 
3472e1f24a79SParav Pandit free:
3473e1f24a79SParav Pandit 	kvfree(out);
3474e1f24a79SParav Pandit 	return ret;
3475e1f24a79SParav Pandit }
3476e1f24a79SParav Pandit 
3477e1f24a79SParav Pandit static int mlx5_ib_query_cong_counters(struct mlx5_ib_dev *dev,
3478e1f24a79SParav Pandit 				       struct mlx5_ib_port *port,
3479e1f24a79SParav Pandit 				       struct rdma_hw_stats *stats)
3480e1f24a79SParav Pandit {
3481e1f24a79SParav Pandit 	int outlen = MLX5_ST_SZ_BYTES(query_cong_statistics_out);
3482e1f24a79SParav Pandit 	void *out;
3483e1f24a79SParav Pandit 	int ret, i;
3484e1f24a79SParav Pandit 	int offset = port->cnts.num_q_counters;
3485e1f24a79SParav Pandit 
34861b9a07eeSLeon Romanovsky 	out = kvzalloc(outlen, GFP_KERNEL);
3487e1f24a79SParav Pandit 	if (!out)
3488e1f24a79SParav Pandit 		return -ENOMEM;
3489e1f24a79SParav Pandit 
3490e1f24a79SParav Pandit 	ret = mlx5_cmd_query_cong_counter(dev->mdev, false, out, outlen);
3491e1f24a79SParav Pandit 	if (ret)
3492e1f24a79SParav Pandit 		goto free;
3493e1f24a79SParav Pandit 
3494e1f24a79SParav Pandit 	for (i = 0; i < port->cnts.num_cong_counters; i++) {
3495e1f24a79SParav Pandit 		stats->value[i + offset] =
3496e1f24a79SParav Pandit 			be64_to_cpup((__be64 *)(out +
3497e1f24a79SParav Pandit 				     port->cnts.offsets[i + offset]));
3498e1f24a79SParav Pandit 	}
3499e1f24a79SParav Pandit 
3500e1f24a79SParav Pandit free:
3501e1f24a79SParav Pandit 	kvfree(out);
3502e1f24a79SParav Pandit 	return ret;
3503e1f24a79SParav Pandit }
3504e1f24a79SParav Pandit 
35050ad17a8fSMark Bloch static int mlx5_ib_get_hw_stats(struct ib_device *ibdev,
35060ad17a8fSMark Bloch 				struct rdma_hw_stats *stats,
35077c16f477SKamal Heib 				u8 port_num, int index)
35080ad17a8fSMark Bloch {
35090ad17a8fSMark Bloch 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
35107c16f477SKamal Heib 	struct mlx5_ib_port *port = &dev->port[port_num - 1];
3511e1f24a79SParav Pandit 	int ret, num_counters;
35120ad17a8fSMark Bloch 
35137c16f477SKamal Heib 	if (!stats)
3514e1f24a79SParav Pandit 		return -EINVAL;
35150ad17a8fSMark Bloch 
3516e1f24a79SParav Pandit 	ret = mlx5_ib_query_q_counters(dev, port, stats);
35170ad17a8fSMark Bloch 	if (ret)
3518e1f24a79SParav Pandit 		return ret;
3519e1f24a79SParav Pandit 	num_counters = port->cnts.num_q_counters;
35200ad17a8fSMark Bloch 
3521e1f24a79SParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
3522e1f24a79SParav Pandit 		ret = mlx5_ib_query_cong_counters(dev, port, stats);
3523e1f24a79SParav Pandit 		if (ret)
3524e1f24a79SParav Pandit 			return ret;
3525e1f24a79SParav Pandit 		num_counters += port->cnts.num_cong_counters;
35260ad17a8fSMark Bloch 	}
35277c16f477SKamal Heib 
3528e1f24a79SParav Pandit 	return num_counters;
35290ad17a8fSMark Bloch }
35300ad17a8fSMark Bloch 
3531693dfd5aSErez Shitrit static struct net_device*
3532693dfd5aSErez Shitrit mlx5_ib_alloc_rdma_netdev(struct ib_device *hca,
3533693dfd5aSErez Shitrit 			  u8 port_num,
3534693dfd5aSErez Shitrit 			  enum rdma_netdev_t type,
3535693dfd5aSErez Shitrit 			  const char *name,
3536693dfd5aSErez Shitrit 			  unsigned char name_assign_type,
3537693dfd5aSErez Shitrit 			  void (*setup)(struct net_device *))
3538693dfd5aSErez Shitrit {
3539693dfd5aSErez Shitrit 	if (type != RDMA_NETDEV_IPOIB)
3540693dfd5aSErez Shitrit 		return ERR_PTR(-EOPNOTSUPP);
3541693dfd5aSErez Shitrit 
3542693dfd5aSErez Shitrit 	return mlx5_rdma_netdev_alloc(to_mdev(hca)->mdev, hca,
3543693dfd5aSErez Shitrit 				      name, setup);
3544693dfd5aSErez Shitrit }
3545693dfd5aSErez Shitrit 
3546693dfd5aSErez Shitrit static void mlx5_ib_free_rdma_netdev(struct net_device *netdev)
3547693dfd5aSErez Shitrit {
3548693dfd5aSErez Shitrit 	return mlx5_rdma_netdev_free(netdev);
3549693dfd5aSErez Shitrit }
3550693dfd5aSErez Shitrit 
35519603b61dSJack Morgenstein static void *mlx5_ib_add(struct mlx5_core_dev *mdev)
3552e126ba97SEli Cohen {
3553e126ba97SEli Cohen 	struct mlx5_ib_dev *dev;
3554ebd61f68SAchiad Shochat 	enum rdma_link_layer ll;
3555ebd61f68SAchiad Shochat 	int port_type_cap;
35564babcf97SAviv Heller 	const char *name;
3557e126ba97SEli Cohen 	int err;
3558e126ba97SEli Cohen 	int i;
3559e126ba97SEli Cohen 
3560ebd61f68SAchiad Shochat 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
3561ebd61f68SAchiad Shochat 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
3562ebd61f68SAchiad Shochat 
3563e126ba97SEli Cohen 	printk_once(KERN_INFO "%s", mlx5_version);
3564e126ba97SEli Cohen 
3565e126ba97SEli Cohen 	dev = (struct mlx5_ib_dev *)ib_alloc_device(sizeof(*dev));
3566e126ba97SEli Cohen 	if (!dev)
35679603b61dSJack Morgenstein 		return NULL;
3568e126ba97SEli Cohen 
35699603b61dSJack Morgenstein 	dev->mdev = mdev;
3570e126ba97SEli Cohen 
35710837e86aSMark Bloch 	dev->port = kcalloc(MLX5_CAP_GEN(mdev, num_ports), sizeof(*dev->port),
35720837e86aSMark Bloch 			    GFP_KERNEL);
35730837e86aSMark Bloch 	if (!dev->port)
35740837e86aSMark Bloch 		goto err_dealloc;
35750837e86aSMark Bloch 
3576fc24fc5eSAchiad Shochat 	rwlock_init(&dev->roce.netdev_lock);
3577e126ba97SEli Cohen 	err = get_port_caps(dev);
3578e126ba97SEli Cohen 	if (err)
35790837e86aSMark Bloch 		goto err_free_port;
3580e126ba97SEli Cohen 
35811b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(dev))
3582e126ba97SEli Cohen 		get_ext_port_caps(dev);
3583e126ba97SEli Cohen 
35844babcf97SAviv Heller 	if (!mlx5_lag_is_active(mdev))
35854babcf97SAviv Heller 		name = "mlx5_%d";
35864babcf97SAviv Heller 	else
35874babcf97SAviv Heller 		name = "mlx5_bond_%d";
35884babcf97SAviv Heller 
35894babcf97SAviv Heller 	strlcpy(dev->ib_dev.name, name, IB_DEVICE_NAME_MAX);
3590e126ba97SEli Cohen 	dev->ib_dev.owner		= THIS_MODULE;
3591e126ba97SEli Cohen 	dev->ib_dev.node_type		= RDMA_NODE_IB_CA;
3592c6790aa9SSagi Grimberg 	dev->ib_dev.local_dma_lkey	= 0 /* not supported for now */;
3593938fe83cSSaeed Mahameed 	dev->num_ports		= MLX5_CAP_GEN(mdev, num_ports);
3594e126ba97SEli Cohen 	dev->ib_dev.phys_port_cnt     = dev->num_ports;
3595233d05d2SSaeed Mahameed 	dev->ib_dev.num_comp_vectors    =
3596233d05d2SSaeed Mahameed 		dev->mdev->priv.eq_table.num_comp_vectors;
35979b0c289eSBart Van Assche 	dev->ib_dev.dev.parent		= &mdev->pdev->dev;
3598e126ba97SEli Cohen 
3599e126ba97SEli Cohen 	dev->ib_dev.uverbs_abi_ver	= MLX5_IB_UVERBS_ABI_VERSION;
3600e126ba97SEli Cohen 	dev->ib_dev.uverbs_cmd_mask	=
3601e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_GET_CONTEXT)		|
3602e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_DEVICE)	|
3603e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_PORT)		|
3604e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_ALLOC_PD)		|
3605e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DEALLOC_PD)		|
360641c450fdSMoni Shoua 		(1ull << IB_USER_VERBS_CMD_CREATE_AH)		|
360741c450fdSMoni Shoua 		(1ull << IB_USER_VERBS_CMD_DESTROY_AH)		|
3608e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_REG_MR)		|
360956e11d62SNoa Osherovich 		(1ull << IB_USER_VERBS_CMD_REREG_MR)		|
3610e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DEREG_MR)		|
3611e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL)	|
3612e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_CQ)		|
3613e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_RESIZE_CQ)		|
3614e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_CQ)		|
3615e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_QP)		|
3616e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_MODIFY_QP)		|
3617e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_QP)		|
3618e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_QP)		|
3619e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_ATTACH_MCAST)	|
3620e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DETACH_MCAST)	|
3621e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_SRQ)		|
3622e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_MODIFY_SRQ)		|
3623e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_SRQ)		|
3624e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_SRQ)		|
3625e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_XSRQ)		|
3626e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_OPEN_QP);
36271707cb4aSHaggai Eran 	dev->ib_dev.uverbs_ex_cmd_mask =
3628d4584ddfSMatan Barak 		(1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE)	|
3629d4584ddfSMatan Barak 		(1ull << IB_USER_VERBS_EX_CMD_CREATE_CQ)	|
36307d29f349SBodong Wang 		(1ull << IB_USER_VERBS_EX_CMD_CREATE_QP)	|
36317d29f349SBodong Wang 		(1ull << IB_USER_VERBS_EX_CMD_MODIFY_QP);
3632e126ba97SEli Cohen 
3633e126ba97SEli Cohen 	dev->ib_dev.query_device	= mlx5_ib_query_device;
3634e126ba97SEli Cohen 	dev->ib_dev.query_port		= mlx5_ib_query_port;
3635ebd61f68SAchiad Shochat 	dev->ib_dev.get_link_layer	= mlx5_ib_port_link_layer;
3636fc24fc5eSAchiad Shochat 	if (ll == IB_LINK_LAYER_ETHERNET)
3637fc24fc5eSAchiad Shochat 		dev->ib_dev.get_netdev	= mlx5_ib_get_netdev;
3638e126ba97SEli Cohen 	dev->ib_dev.query_gid		= mlx5_ib_query_gid;
36393cca2606SAchiad Shochat 	dev->ib_dev.add_gid		= mlx5_ib_add_gid;
36403cca2606SAchiad Shochat 	dev->ib_dev.del_gid		= mlx5_ib_del_gid;
3641e126ba97SEli Cohen 	dev->ib_dev.query_pkey		= mlx5_ib_query_pkey;
3642e126ba97SEli Cohen 	dev->ib_dev.modify_device	= mlx5_ib_modify_device;
3643e126ba97SEli Cohen 	dev->ib_dev.modify_port		= mlx5_ib_modify_port;
3644e126ba97SEli Cohen 	dev->ib_dev.alloc_ucontext	= mlx5_ib_alloc_ucontext;
3645e126ba97SEli Cohen 	dev->ib_dev.dealloc_ucontext	= mlx5_ib_dealloc_ucontext;
3646e126ba97SEli Cohen 	dev->ib_dev.mmap		= mlx5_ib_mmap;
3647e126ba97SEli Cohen 	dev->ib_dev.alloc_pd		= mlx5_ib_alloc_pd;
3648e126ba97SEli Cohen 	dev->ib_dev.dealloc_pd		= mlx5_ib_dealloc_pd;
3649e126ba97SEli Cohen 	dev->ib_dev.create_ah		= mlx5_ib_create_ah;
3650e126ba97SEli Cohen 	dev->ib_dev.query_ah		= mlx5_ib_query_ah;
3651e126ba97SEli Cohen 	dev->ib_dev.destroy_ah		= mlx5_ib_destroy_ah;
3652e126ba97SEli Cohen 	dev->ib_dev.create_srq		= mlx5_ib_create_srq;
3653e126ba97SEli Cohen 	dev->ib_dev.modify_srq		= mlx5_ib_modify_srq;
3654e126ba97SEli Cohen 	dev->ib_dev.query_srq		= mlx5_ib_query_srq;
3655e126ba97SEli Cohen 	dev->ib_dev.destroy_srq		= mlx5_ib_destroy_srq;
3656e126ba97SEli Cohen 	dev->ib_dev.post_srq_recv	= mlx5_ib_post_srq_recv;
3657e126ba97SEli Cohen 	dev->ib_dev.create_qp		= mlx5_ib_create_qp;
3658e126ba97SEli Cohen 	dev->ib_dev.modify_qp		= mlx5_ib_modify_qp;
3659e126ba97SEli Cohen 	dev->ib_dev.query_qp		= mlx5_ib_query_qp;
3660e126ba97SEli Cohen 	dev->ib_dev.destroy_qp		= mlx5_ib_destroy_qp;
3661e126ba97SEli Cohen 	dev->ib_dev.post_send		= mlx5_ib_post_send;
3662e126ba97SEli Cohen 	dev->ib_dev.post_recv		= mlx5_ib_post_recv;
3663e126ba97SEli Cohen 	dev->ib_dev.create_cq		= mlx5_ib_create_cq;
3664e126ba97SEli Cohen 	dev->ib_dev.modify_cq		= mlx5_ib_modify_cq;
3665e126ba97SEli Cohen 	dev->ib_dev.resize_cq		= mlx5_ib_resize_cq;
3666e126ba97SEli Cohen 	dev->ib_dev.destroy_cq		= mlx5_ib_destroy_cq;
3667e126ba97SEli Cohen 	dev->ib_dev.poll_cq		= mlx5_ib_poll_cq;
3668e126ba97SEli Cohen 	dev->ib_dev.req_notify_cq	= mlx5_ib_arm_cq;
3669e126ba97SEli Cohen 	dev->ib_dev.get_dma_mr		= mlx5_ib_get_dma_mr;
3670e126ba97SEli Cohen 	dev->ib_dev.reg_user_mr		= mlx5_ib_reg_user_mr;
367156e11d62SNoa Osherovich 	dev->ib_dev.rereg_user_mr	= mlx5_ib_rereg_user_mr;
3672e126ba97SEli Cohen 	dev->ib_dev.dereg_mr		= mlx5_ib_dereg_mr;
3673e126ba97SEli Cohen 	dev->ib_dev.attach_mcast	= mlx5_ib_mcg_attach;
3674e126ba97SEli Cohen 	dev->ib_dev.detach_mcast	= mlx5_ib_mcg_detach;
3675e126ba97SEli Cohen 	dev->ib_dev.process_mad		= mlx5_ib_process_mad;
36769bee178bSSagi Grimberg 	dev->ib_dev.alloc_mr		= mlx5_ib_alloc_mr;
36778a187ee5SSagi Grimberg 	dev->ib_dev.map_mr_sg		= mlx5_ib_map_mr_sg;
3678d5436ba0SSagi Grimberg 	dev->ib_dev.check_mr_status	= mlx5_ib_check_mr_status;
36797738613eSIra Weiny 	dev->ib_dev.get_port_immutable  = mlx5_port_immutable;
3680c7342823SIra Weiny 	dev->ib_dev.get_dev_fw_str      = get_dev_fw_str;
3681022d038aSAlex Vesker 	if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads)) {
3682693dfd5aSErez Shitrit 		dev->ib_dev.alloc_rdma_netdev	= mlx5_ib_alloc_rdma_netdev;
3683693dfd5aSErez Shitrit 		dev->ib_dev.free_rdma_netdev	= mlx5_ib_free_rdma_netdev;
3684022d038aSAlex Vesker 	}
3685eff901d3SEli Cohen 	if (mlx5_core_is_pf(mdev)) {
3686eff901d3SEli Cohen 		dev->ib_dev.get_vf_config	= mlx5_ib_get_vf_config;
3687eff901d3SEli Cohen 		dev->ib_dev.set_vf_link_state	= mlx5_ib_set_vf_link_state;
3688eff901d3SEli Cohen 		dev->ib_dev.get_vf_stats	= mlx5_ib_get_vf_stats;
3689eff901d3SEli Cohen 		dev->ib_dev.set_vf_guid		= mlx5_ib_set_vf_guid;
3690eff901d3SEli Cohen 	}
3691e126ba97SEli Cohen 
36927c2344c3SMaor Gottlieb 	dev->ib_dev.disassociate_ucontext = mlx5_ib_disassociate_ucontext;
36937c2344c3SMaor Gottlieb 
3694938fe83cSSaeed Mahameed 	mlx5_ib_internal_fill_odp_caps(dev);
36958cdd312cSHaggai Eran 
36966e8484c5SMax Gurtovoy 	dev->umr_fence = mlx5_get_umr_fence(MLX5_CAP_GEN(mdev, umr_fence));
36976e8484c5SMax Gurtovoy 
3698d2370e0aSMatan Barak 	if (MLX5_CAP_GEN(mdev, imaicl)) {
3699d2370e0aSMatan Barak 		dev->ib_dev.alloc_mw		= mlx5_ib_alloc_mw;
3700d2370e0aSMatan Barak 		dev->ib_dev.dealloc_mw		= mlx5_ib_dealloc_mw;
3701d2370e0aSMatan Barak 		dev->ib_dev.uverbs_cmd_mask |=
3702d2370e0aSMatan Barak 			(1ull << IB_USER_VERBS_CMD_ALLOC_MW)	|
3703d2370e0aSMatan Barak 			(1ull << IB_USER_VERBS_CMD_DEALLOC_MW);
3704d2370e0aSMatan Barak 	}
3705d2370e0aSMatan Barak 
37067c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt)) {
37070ad17a8fSMark Bloch 		dev->ib_dev.get_hw_stats	= mlx5_ib_get_hw_stats;
37080ad17a8fSMark Bloch 		dev->ib_dev.alloc_hw_stats	= mlx5_ib_alloc_hw_stats;
37090ad17a8fSMark Bloch 	}
37100ad17a8fSMark Bloch 
3711938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, xrc)) {
3712e126ba97SEli Cohen 		dev->ib_dev.alloc_xrcd = mlx5_ib_alloc_xrcd;
3713e126ba97SEli Cohen 		dev->ib_dev.dealloc_xrcd = mlx5_ib_dealloc_xrcd;
3714e126ba97SEli Cohen 		dev->ib_dev.uverbs_cmd_mask |=
3715e126ba97SEli Cohen 			(1ull << IB_USER_VERBS_CMD_OPEN_XRCD) |
3716e126ba97SEli Cohen 			(1ull << IB_USER_VERBS_CMD_CLOSE_XRCD);
3717e126ba97SEli Cohen 	}
3718e126ba97SEli Cohen 
3719048ccca8SLinus Torvalds 	if (mlx5_ib_port_link_layer(&dev->ib_dev, 1) ==
3720038d2ef8SMaor Gottlieb 	    IB_LINK_LAYER_ETHERNET) {
3721038d2ef8SMaor Gottlieb 		dev->ib_dev.create_flow	= mlx5_ib_create_flow;
3722038d2ef8SMaor Gottlieb 		dev->ib_dev.destroy_flow = mlx5_ib_destroy_flow;
372379b20a6cSYishai Hadas 		dev->ib_dev.create_wq	 = mlx5_ib_create_wq;
372479b20a6cSYishai Hadas 		dev->ib_dev.modify_wq	 = mlx5_ib_modify_wq;
372579b20a6cSYishai Hadas 		dev->ib_dev.destroy_wq	 = mlx5_ib_destroy_wq;
3726c5f90929SYishai Hadas 		dev->ib_dev.create_rwq_ind_table = mlx5_ib_create_rwq_ind_table;
3727c5f90929SYishai Hadas 		dev->ib_dev.destroy_rwq_ind_table = mlx5_ib_destroy_rwq_ind_table;
3728038d2ef8SMaor Gottlieb 		dev->ib_dev.uverbs_ex_cmd_mask |=
3729038d2ef8SMaor Gottlieb 			(1ull << IB_USER_VERBS_EX_CMD_CREATE_FLOW) |
373079b20a6cSYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_DESTROY_FLOW) |
373179b20a6cSYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_CREATE_WQ) |
373279b20a6cSYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_MODIFY_WQ) |
3733c5f90929SYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_DESTROY_WQ) |
3734c5f90929SYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_CREATE_RWQ_IND_TBL) |
3735c5f90929SYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_DESTROY_RWQ_IND_TBL);
3736038d2ef8SMaor Gottlieb 	}
3737e126ba97SEli Cohen 	err = init_node_data(dev);
3738e126ba97SEli Cohen 	if (err)
373990be7c8aSMajd Dibbiny 		goto err_free_port;
3740e126ba97SEli Cohen 
3741038d2ef8SMaor Gottlieb 	mutex_init(&dev->flow_db.lock);
3742e126ba97SEli Cohen 	mutex_init(&dev->cap_mask_mutex);
374389ea94a7SMaor Gottlieb 	INIT_LIST_HEAD(&dev->qp_list);
374489ea94a7SMaor Gottlieb 	spin_lock_init(&dev->reset_flow_resource_lock);
3745e126ba97SEli Cohen 
3746fc24fc5eSAchiad Shochat 	if (ll == IB_LINK_LAYER_ETHERNET) {
374745f95acdSOr Gerlitz 		err = mlx5_enable_eth(dev);
3748e126ba97SEli Cohen 		if (err)
374990be7c8aSMajd Dibbiny 			goto err_free_port;
3750fc24fc5eSAchiad Shochat 	}
3751fc24fc5eSAchiad Shochat 
3752fc24fc5eSAchiad Shochat 	err = create_dev_resources(&dev->devr);
3753fc24fc5eSAchiad Shochat 	if (err)
375445f95acdSOr Gerlitz 		goto err_disable_eth;
3755e126ba97SEli Cohen 
37566aec21f6SHaggai Eran 	err = mlx5_ib_odp_init_one(dev);
3757281d1a92SWei Yongjun 	if (err)
3758e126ba97SEli Cohen 		goto err_rsrc;
3759e126ba97SEli Cohen 
376045bded2cSKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt)) {
3761e1f24a79SParav Pandit 		err = mlx5_ib_alloc_counters(dev);
37626aec21f6SHaggai Eran 		if (err)
37636aec21f6SHaggai Eran 			goto err_odp;
376445bded2cSKamal Heib 	}
37656aec21f6SHaggai Eran 
37665fe9dec0SEli Cohen 	dev->mdev->priv.uar = mlx5_get_uars_page(dev->mdev);
37675fe9dec0SEli Cohen 	if (!dev->mdev->priv.uar)
3768e1f24a79SParav Pandit 		goto err_cnt;
37695fe9dec0SEli Cohen 
37705fe9dec0SEli Cohen 	err = mlx5_alloc_bfreg(dev->mdev, &dev->bfreg, false, false);
37715fe9dec0SEli Cohen 	if (err)
37725fe9dec0SEli Cohen 		goto err_uar_page;
37735fe9dec0SEli Cohen 
37745fe9dec0SEli Cohen 	err = mlx5_alloc_bfreg(dev->mdev, &dev->fp_bfreg, false, true);
37755fe9dec0SEli Cohen 	if (err)
37765fe9dec0SEli Cohen 		goto err_bfreg;
37775fe9dec0SEli Cohen 
37780837e86aSMark Bloch 	err = ib_register_device(&dev->ib_dev, NULL);
37790837e86aSMark Bloch 	if (err)
37805fe9dec0SEli Cohen 		goto err_fp_bfreg;
37810837e86aSMark Bloch 
3782e126ba97SEli Cohen 	err = create_umr_res(dev);
3783e126ba97SEli Cohen 	if (err)
3784e126ba97SEli Cohen 		goto err_dev;
3785e126ba97SEli Cohen 
3786e126ba97SEli Cohen 	for (i = 0; i < ARRAY_SIZE(mlx5_class_attributes); i++) {
3787281d1a92SWei Yongjun 		err = device_create_file(&dev->ib_dev.dev,
3788281d1a92SWei Yongjun 					 mlx5_class_attributes[i]);
3789281d1a92SWei Yongjun 		if (err)
3790e126ba97SEli Cohen 			goto err_umrc;
3791e126ba97SEli Cohen 	}
3792e126ba97SEli Cohen 
3793e126ba97SEli Cohen 	dev->ib_active = true;
3794e126ba97SEli Cohen 
37959603b61dSJack Morgenstein 	return dev;
3796e126ba97SEli Cohen 
3797e126ba97SEli Cohen err_umrc:
3798e126ba97SEli Cohen 	destroy_umrc_res(dev);
3799e126ba97SEli Cohen 
3800e126ba97SEli Cohen err_dev:
3801e126ba97SEli Cohen 	ib_unregister_device(&dev->ib_dev);
3802e126ba97SEli Cohen 
38035fe9dec0SEli Cohen err_fp_bfreg:
38045fe9dec0SEli Cohen 	mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
38055fe9dec0SEli Cohen 
38065fe9dec0SEli Cohen err_bfreg:
38075fe9dec0SEli Cohen 	mlx5_free_bfreg(dev->mdev, &dev->bfreg);
38085fe9dec0SEli Cohen 
38095fe9dec0SEli Cohen err_uar_page:
38105fe9dec0SEli Cohen 	mlx5_put_uars_page(dev->mdev, dev->mdev->priv.uar);
38115fe9dec0SEli Cohen 
3812e1f24a79SParav Pandit err_cnt:
381345bded2cSKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt))
3814e1f24a79SParav Pandit 		mlx5_ib_dealloc_counters(dev);
38150837e86aSMark Bloch 
38166aec21f6SHaggai Eran err_odp:
38176aec21f6SHaggai Eran 	mlx5_ib_odp_remove_one(dev);
38186aec21f6SHaggai Eran 
3819e126ba97SEli Cohen err_rsrc:
3820e126ba97SEli Cohen 	destroy_dev_resources(&dev->devr);
3821e126ba97SEli Cohen 
382245f95acdSOr Gerlitz err_disable_eth:
38235ec8c83eSAviv Heller 	if (ll == IB_LINK_LAYER_ETHERNET) {
382445f95acdSOr Gerlitz 		mlx5_disable_eth(dev);
3825d012f5d6SOr Gerlitz 		mlx5_remove_netdev_notifier(dev);
38265ec8c83eSAviv Heller 	}
3827fc24fc5eSAchiad Shochat 
38280837e86aSMark Bloch err_free_port:
38290837e86aSMark Bloch 	kfree(dev->port);
38300837e86aSMark Bloch 
38319603b61dSJack Morgenstein err_dealloc:
3832e126ba97SEli Cohen 	ib_dealloc_device((struct ib_device *)dev);
3833e126ba97SEli Cohen 
38349603b61dSJack Morgenstein 	return NULL;
3835e126ba97SEli Cohen }
3836e126ba97SEli Cohen 
38379603b61dSJack Morgenstein static void mlx5_ib_remove(struct mlx5_core_dev *mdev, void *context)
3838e126ba97SEli Cohen {
38399603b61dSJack Morgenstein 	struct mlx5_ib_dev *dev = context;
3840fc24fc5eSAchiad Shochat 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev, 1);
38416aec21f6SHaggai Eran 
3842d012f5d6SOr Gerlitz 	mlx5_remove_netdev_notifier(dev);
3843e126ba97SEli Cohen 	ib_unregister_device(&dev->ib_dev);
38445fe9dec0SEli Cohen 	mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
38455fe9dec0SEli Cohen 	mlx5_free_bfreg(dev->mdev, &dev->bfreg);
38465fe9dec0SEli Cohen 	mlx5_put_uars_page(dev->mdev, mdev->priv.uar);
384745bded2cSKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt))
3848e1f24a79SParav Pandit 		mlx5_ib_dealloc_counters(dev);
3849eefd56e5SEli Cohen 	destroy_umrc_res(dev);
38506aec21f6SHaggai Eran 	mlx5_ib_odp_remove_one(dev);
3851e126ba97SEli Cohen 	destroy_dev_resources(&dev->devr);
3852fc24fc5eSAchiad Shochat 	if (ll == IB_LINK_LAYER_ETHERNET)
385345f95acdSOr Gerlitz 		mlx5_disable_eth(dev);
38540837e86aSMark Bloch 	kfree(dev->port);
3855e126ba97SEli Cohen 	ib_dealloc_device(&dev->ib_dev);
3856e126ba97SEli Cohen }
3857e126ba97SEli Cohen 
38589603b61dSJack Morgenstein static struct mlx5_interface mlx5_ib_interface = {
38599603b61dSJack Morgenstein 	.add            = mlx5_ib_add,
38609603b61dSJack Morgenstein 	.remove         = mlx5_ib_remove,
38619603b61dSJack Morgenstein 	.event          = mlx5_ib_event,
3862d9aaed83SArtemy Kovalyov #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
3863d9aaed83SArtemy Kovalyov 	.pfault		= mlx5_ib_pfault,
3864d9aaed83SArtemy Kovalyov #endif
386564613d94SSaeed Mahameed 	.protocol	= MLX5_INTERFACE_PROTOCOL_IB,
3866e126ba97SEli Cohen };
3867e126ba97SEli Cohen 
3868e126ba97SEli Cohen static int __init mlx5_ib_init(void)
3869e126ba97SEli Cohen {
38706aec21f6SHaggai Eran 	int err;
38716aec21f6SHaggai Eran 
387281713d37SArtemy Kovalyov 	mlx5_ib_odp_init();
38739603b61dSJack Morgenstein 
38746aec21f6SHaggai Eran 	err = mlx5_register_interface(&mlx5_ib_interface);
38756aec21f6SHaggai Eran 
38766aec21f6SHaggai Eran 	return err;
3877e126ba97SEli Cohen }
3878e126ba97SEli Cohen 
3879e126ba97SEli Cohen static void __exit mlx5_ib_cleanup(void)
3880e126ba97SEli Cohen {
38819603b61dSJack Morgenstein 	mlx5_unregister_interface(&mlx5_ib_interface);
3882e126ba97SEli Cohen }
3883e126ba97SEli Cohen 
3884e126ba97SEli Cohen module_init(mlx5_ib_init);
3885e126ba97SEli Cohen module_exit(mlx5_ib_cleanup);
3886