xref: /openbmc/linux/drivers/infiniband/hw/mlx5/main.c (revision 7e43a2a5bae39fedaa7cce21d637e0c8d96d8e54)
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>
447c2344c3SMaor Gottlieb #include <linux/delay.h>
45e126ba97SEli Cohen #include <rdma/ib_user_verbs.h>
463f89a643SAchiad Shochat #include <rdma/ib_addr.h>
472811ba51SAchiad Shochat #include <rdma/ib_cache.h>
48ada68c31SAchiad Shochat #include <linux/mlx5/port.h>
491b5daf11SMajd Dibbiny #include <linux/mlx5/vport.h>
507c2344c3SMaor Gottlieb #include <linux/list.h>
51e126ba97SEli Cohen #include <rdma/ib_smi.h>
52e126ba97SEli Cohen #include <rdma/ib_umem.h>
53038d2ef8SMaor Gottlieb #include <linux/in.h>
54038d2ef8SMaor Gottlieb #include <linux/etherdevice.h>
55038d2ef8SMaor Gottlieb #include <linux/mlx5/fs.h>
56e126ba97SEli Cohen #include "mlx5_ib.h"
57e126ba97SEli Cohen 
58e126ba97SEli Cohen #define DRIVER_NAME "mlx5_ib"
59169a1d85SAmir Vadai #define DRIVER_VERSION "2.2-1"
60169a1d85SAmir Vadai #define DRIVER_RELDATE	"Feb 2014"
61e126ba97SEli Cohen 
62e126ba97SEli Cohen MODULE_AUTHOR("Eli Cohen <eli@mellanox.com>");
63e126ba97SEli Cohen MODULE_DESCRIPTION("Mellanox Connect-IB HCA IB driver");
64e126ba97SEli Cohen MODULE_LICENSE("Dual BSD/GPL");
65e126ba97SEli Cohen MODULE_VERSION(DRIVER_VERSION);
66e126ba97SEli Cohen 
679603b61dSJack Morgenstein static int deprecated_prof_sel = 2;
689603b61dSJack Morgenstein module_param_named(prof_sel, deprecated_prof_sel, int, 0444);
699603b61dSJack Morgenstein MODULE_PARM_DESC(prof_sel, "profile selector. Deprecated here. Moved to module mlx5_core");
70e126ba97SEli Cohen 
71e126ba97SEli Cohen static char mlx5_version[] =
72e126ba97SEli Cohen 	DRIVER_NAME ": Mellanox Connect-IB Infiniband driver v"
73e126ba97SEli Cohen 	DRIVER_VERSION " (" DRIVER_RELDATE ")\n";
74e126ba97SEli Cohen 
75da7525d2SEran Ben Elisha enum {
76da7525d2SEran Ben Elisha 	MLX5_ATOMIC_SIZE_QP_8BYTES = 1 << 3,
77da7525d2SEran Ben Elisha };
781b5daf11SMajd Dibbiny 
791b5daf11SMajd Dibbiny static enum rdma_link_layer
80ebd61f68SAchiad Shochat mlx5_port_type_cap_to_rdma_ll(int port_type_cap)
811b5daf11SMajd Dibbiny {
82ebd61f68SAchiad Shochat 	switch (port_type_cap) {
831b5daf11SMajd Dibbiny 	case MLX5_CAP_PORT_TYPE_IB:
841b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_INFINIBAND;
851b5daf11SMajd Dibbiny 	case MLX5_CAP_PORT_TYPE_ETH:
861b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_ETHERNET;
871b5daf11SMajd Dibbiny 	default:
881b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_UNSPECIFIED;
891b5daf11SMajd Dibbiny 	}
901b5daf11SMajd Dibbiny }
911b5daf11SMajd Dibbiny 
92ebd61f68SAchiad Shochat static enum rdma_link_layer
93ebd61f68SAchiad Shochat mlx5_ib_port_link_layer(struct ib_device *device, u8 port_num)
94ebd61f68SAchiad Shochat {
95ebd61f68SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(device);
96ebd61f68SAchiad Shochat 	int port_type_cap = MLX5_CAP_GEN(dev->mdev, port_type);
97ebd61f68SAchiad Shochat 
98ebd61f68SAchiad Shochat 	return mlx5_port_type_cap_to_rdma_ll(port_type_cap);
99ebd61f68SAchiad Shochat }
100ebd61f68SAchiad Shochat 
101fc24fc5eSAchiad Shochat static int mlx5_netdev_event(struct notifier_block *this,
102fc24fc5eSAchiad Shochat 			     unsigned long event, void *ptr)
103fc24fc5eSAchiad Shochat {
104fc24fc5eSAchiad Shochat 	struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
105fc24fc5eSAchiad Shochat 	struct mlx5_ib_dev *ibdev = container_of(this, struct mlx5_ib_dev,
106fc24fc5eSAchiad Shochat 						 roce.nb);
107fc24fc5eSAchiad Shochat 
1085ec8c83eSAviv Heller 	switch (event) {
1095ec8c83eSAviv Heller 	case NETDEV_REGISTER:
1105ec8c83eSAviv Heller 	case NETDEV_UNREGISTER:
111fc24fc5eSAchiad Shochat 		write_lock(&ibdev->roce.netdev_lock);
112fc24fc5eSAchiad Shochat 		if (ndev->dev.parent == &ibdev->mdev->pdev->dev)
1135ec8c83eSAviv Heller 			ibdev->roce.netdev = (event == NETDEV_UNREGISTER) ?
1145ec8c83eSAviv Heller 					     NULL : ndev;
115fc24fc5eSAchiad Shochat 		write_unlock(&ibdev->roce.netdev_lock);
1165ec8c83eSAviv Heller 		break;
1175ec8c83eSAviv Heller 
1185ec8c83eSAviv Heller 	case NETDEV_UP:
11988621dfeSAviv Heller 	case NETDEV_DOWN: {
12088621dfeSAviv Heller 		struct net_device *lag_ndev = mlx5_lag_get_roce_netdev(ibdev->mdev);
12188621dfeSAviv Heller 		struct net_device *upper = NULL;
12288621dfeSAviv Heller 
12388621dfeSAviv Heller 		if (lag_ndev) {
12488621dfeSAviv Heller 			upper = netdev_master_upper_dev_get(lag_ndev);
12588621dfeSAviv Heller 			dev_put(lag_ndev);
12688621dfeSAviv Heller 		}
12788621dfeSAviv Heller 
12888621dfeSAviv Heller 		if ((upper == ndev || (!upper && ndev == ibdev->roce.netdev))
12988621dfeSAviv Heller 		    && ibdev->ib_active) {
1305ec8c83eSAviv Heller 			struct ib_event ibev = {0};
1315ec8c83eSAviv Heller 
1325ec8c83eSAviv Heller 			ibev.device = &ibdev->ib_dev;
1335ec8c83eSAviv Heller 			ibev.event = (event == NETDEV_UP) ?
1345ec8c83eSAviv Heller 				     IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
1355ec8c83eSAviv Heller 			ibev.element.port_num = 1;
1365ec8c83eSAviv Heller 			ib_dispatch_event(&ibev);
1375ec8c83eSAviv Heller 		}
1385ec8c83eSAviv Heller 		break;
13988621dfeSAviv Heller 	}
1405ec8c83eSAviv Heller 
1415ec8c83eSAviv Heller 	default:
1425ec8c83eSAviv Heller 		break;
1435ec8c83eSAviv Heller 	}
144fc24fc5eSAchiad Shochat 
145fc24fc5eSAchiad Shochat 	return NOTIFY_DONE;
146fc24fc5eSAchiad Shochat }
147fc24fc5eSAchiad Shochat 
148fc24fc5eSAchiad Shochat static struct net_device *mlx5_ib_get_netdev(struct ib_device *device,
149fc24fc5eSAchiad Shochat 					     u8 port_num)
150fc24fc5eSAchiad Shochat {
151fc24fc5eSAchiad Shochat 	struct mlx5_ib_dev *ibdev = to_mdev(device);
152fc24fc5eSAchiad Shochat 	struct net_device *ndev;
153fc24fc5eSAchiad Shochat 
15488621dfeSAviv Heller 	ndev = mlx5_lag_get_roce_netdev(ibdev->mdev);
15588621dfeSAviv Heller 	if (ndev)
15688621dfeSAviv Heller 		return ndev;
15788621dfeSAviv Heller 
158fc24fc5eSAchiad Shochat 	/* Ensure ndev does not disappear before we invoke dev_hold()
159fc24fc5eSAchiad Shochat 	 */
160fc24fc5eSAchiad Shochat 	read_lock(&ibdev->roce.netdev_lock);
161fc24fc5eSAchiad Shochat 	ndev = ibdev->roce.netdev;
162fc24fc5eSAchiad Shochat 	if (ndev)
163fc24fc5eSAchiad Shochat 		dev_hold(ndev);
164fc24fc5eSAchiad Shochat 	read_unlock(&ibdev->roce.netdev_lock);
165fc24fc5eSAchiad Shochat 
166fc24fc5eSAchiad Shochat 	return ndev;
167fc24fc5eSAchiad Shochat }
168fc24fc5eSAchiad Shochat 
1693f89a643SAchiad Shochat static int mlx5_query_port_roce(struct ib_device *device, u8 port_num,
1703f89a643SAchiad Shochat 				struct ib_port_attr *props)
1713f89a643SAchiad Shochat {
1723f89a643SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(device);
17388621dfeSAviv Heller 	struct net_device *ndev, *upper;
1743f89a643SAchiad Shochat 	enum ib_mtu ndev_ib_mtu;
175c876a1b7SLeon Romanovsky 	u16 qkey_viol_cntr;
1763f89a643SAchiad Shochat 
1773f89a643SAchiad Shochat 	memset(props, 0, sizeof(*props));
1783f89a643SAchiad Shochat 
1793f89a643SAchiad Shochat 	props->port_cap_flags  |= IB_PORT_CM_SUP;
1803f89a643SAchiad Shochat 	props->port_cap_flags  |= IB_PORT_IP_BASED_GIDS;
1813f89a643SAchiad Shochat 
1823f89a643SAchiad Shochat 	props->gid_tbl_len      = MLX5_CAP_ROCE(dev->mdev,
1833f89a643SAchiad Shochat 						roce_address_table_size);
1843f89a643SAchiad Shochat 	props->max_mtu          = IB_MTU_4096;
1853f89a643SAchiad Shochat 	props->max_msg_sz       = 1 << MLX5_CAP_GEN(dev->mdev, log_max_msg);
1863f89a643SAchiad Shochat 	props->pkey_tbl_len     = 1;
1873f89a643SAchiad Shochat 	props->state            = IB_PORT_DOWN;
1883f89a643SAchiad Shochat 	props->phys_state       = 3;
1893f89a643SAchiad Shochat 
190c876a1b7SLeon Romanovsky 	mlx5_query_nic_vport_qkey_viol_cntr(dev->mdev, &qkey_viol_cntr);
191c876a1b7SLeon Romanovsky 	props->qkey_viol_cntr = qkey_viol_cntr;
1923f89a643SAchiad Shochat 
1933f89a643SAchiad Shochat 	ndev = mlx5_ib_get_netdev(device, port_num);
1943f89a643SAchiad Shochat 	if (!ndev)
1953f89a643SAchiad Shochat 		return 0;
1963f89a643SAchiad Shochat 
19788621dfeSAviv Heller 	if (mlx5_lag_is_active(dev->mdev)) {
19888621dfeSAviv Heller 		rcu_read_lock();
19988621dfeSAviv Heller 		upper = netdev_master_upper_dev_get_rcu(ndev);
20088621dfeSAviv Heller 		if (upper) {
20188621dfeSAviv Heller 			dev_put(ndev);
20288621dfeSAviv Heller 			ndev = upper;
20388621dfeSAviv Heller 			dev_hold(ndev);
20488621dfeSAviv Heller 		}
20588621dfeSAviv Heller 		rcu_read_unlock();
20688621dfeSAviv Heller 	}
20788621dfeSAviv Heller 
2083f89a643SAchiad Shochat 	if (netif_running(ndev) && netif_carrier_ok(ndev)) {
2093f89a643SAchiad Shochat 		props->state      = IB_PORT_ACTIVE;
2103f89a643SAchiad Shochat 		props->phys_state = 5;
2113f89a643SAchiad Shochat 	}
2123f89a643SAchiad Shochat 
2133f89a643SAchiad Shochat 	ndev_ib_mtu = iboe_get_mtu(ndev->mtu);
2143f89a643SAchiad Shochat 
2153f89a643SAchiad Shochat 	dev_put(ndev);
2163f89a643SAchiad Shochat 
2173f89a643SAchiad Shochat 	props->active_mtu	= min(props->max_mtu, ndev_ib_mtu);
2183f89a643SAchiad Shochat 
2193f89a643SAchiad Shochat 	props->active_width	= IB_WIDTH_4X;  /* TODO */
2203f89a643SAchiad Shochat 	props->active_speed	= IB_SPEED_QDR; /* TODO */
2213f89a643SAchiad Shochat 
2223f89a643SAchiad Shochat 	return 0;
2233f89a643SAchiad Shochat }
2243f89a643SAchiad Shochat 
2253cca2606SAchiad Shochat static void ib_gid_to_mlx5_roce_addr(const union ib_gid *gid,
2263cca2606SAchiad Shochat 				     const struct ib_gid_attr *attr,
2273cca2606SAchiad Shochat 				     void *mlx5_addr)
2283cca2606SAchiad Shochat {
2293cca2606SAchiad Shochat #define MLX5_SET_RA(p, f, v) MLX5_SET(roce_addr_layout, p, f, v)
2303cca2606SAchiad Shochat 	char *mlx5_addr_l3_addr	= MLX5_ADDR_OF(roce_addr_layout, mlx5_addr,
2313cca2606SAchiad Shochat 					       source_l3_address);
2323cca2606SAchiad Shochat 	void *mlx5_addr_mac	= MLX5_ADDR_OF(roce_addr_layout, mlx5_addr,
2333cca2606SAchiad Shochat 					       source_mac_47_32);
2343cca2606SAchiad Shochat 
2353cca2606SAchiad Shochat 	if (!gid)
2363cca2606SAchiad Shochat 		return;
2373cca2606SAchiad Shochat 
2383cca2606SAchiad Shochat 	ether_addr_copy(mlx5_addr_mac, attr->ndev->dev_addr);
2393cca2606SAchiad Shochat 
2403cca2606SAchiad Shochat 	if (is_vlan_dev(attr->ndev)) {
2413cca2606SAchiad Shochat 		MLX5_SET_RA(mlx5_addr, vlan_valid, 1);
2423cca2606SAchiad Shochat 		MLX5_SET_RA(mlx5_addr, vlan_id, vlan_dev_vlan_id(attr->ndev));
2433cca2606SAchiad Shochat 	}
2443cca2606SAchiad Shochat 
2453cca2606SAchiad Shochat 	switch (attr->gid_type) {
2463cca2606SAchiad Shochat 	case IB_GID_TYPE_IB:
2473cca2606SAchiad Shochat 		MLX5_SET_RA(mlx5_addr, roce_version, MLX5_ROCE_VERSION_1);
2483cca2606SAchiad Shochat 		break;
2493cca2606SAchiad Shochat 	case IB_GID_TYPE_ROCE_UDP_ENCAP:
2503cca2606SAchiad Shochat 		MLX5_SET_RA(mlx5_addr, roce_version, MLX5_ROCE_VERSION_2);
2513cca2606SAchiad Shochat 		break;
2523cca2606SAchiad Shochat 
2533cca2606SAchiad Shochat 	default:
2543cca2606SAchiad Shochat 		WARN_ON(true);
2553cca2606SAchiad Shochat 	}
2563cca2606SAchiad Shochat 
2573cca2606SAchiad Shochat 	if (attr->gid_type != IB_GID_TYPE_IB) {
2583cca2606SAchiad Shochat 		if (ipv6_addr_v4mapped((void *)gid))
2593cca2606SAchiad Shochat 			MLX5_SET_RA(mlx5_addr, roce_l3_type,
2603cca2606SAchiad Shochat 				    MLX5_ROCE_L3_TYPE_IPV4);
2613cca2606SAchiad Shochat 		else
2623cca2606SAchiad Shochat 			MLX5_SET_RA(mlx5_addr, roce_l3_type,
2633cca2606SAchiad Shochat 				    MLX5_ROCE_L3_TYPE_IPV6);
2643cca2606SAchiad Shochat 	}
2653cca2606SAchiad Shochat 
2663cca2606SAchiad Shochat 	if ((attr->gid_type == IB_GID_TYPE_IB) ||
2673cca2606SAchiad Shochat 	    !ipv6_addr_v4mapped((void *)gid))
2683cca2606SAchiad Shochat 		memcpy(mlx5_addr_l3_addr, gid, sizeof(*gid));
2693cca2606SAchiad Shochat 	else
2703cca2606SAchiad Shochat 		memcpy(&mlx5_addr_l3_addr[12], &gid->raw[12], 4);
2713cca2606SAchiad Shochat }
2723cca2606SAchiad Shochat 
2733cca2606SAchiad Shochat static int set_roce_addr(struct ib_device *device, u8 port_num,
2743cca2606SAchiad Shochat 			 unsigned int index,
2753cca2606SAchiad Shochat 			 const union ib_gid *gid,
2763cca2606SAchiad Shochat 			 const struct ib_gid_attr *attr)
2773cca2606SAchiad Shochat {
2783cca2606SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(device);
279c4f287c4SSaeed Mahameed 	u32  in[MLX5_ST_SZ_DW(set_roce_address_in)]  = {0};
280c4f287c4SSaeed Mahameed 	u32 out[MLX5_ST_SZ_DW(set_roce_address_out)] = {0};
2813cca2606SAchiad Shochat 	void *in_addr = MLX5_ADDR_OF(set_roce_address_in, in, roce_address);
2823cca2606SAchiad Shochat 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(device, port_num);
2833cca2606SAchiad Shochat 
2843cca2606SAchiad Shochat 	if (ll != IB_LINK_LAYER_ETHERNET)
2853cca2606SAchiad Shochat 		return -EINVAL;
2863cca2606SAchiad Shochat 
2873cca2606SAchiad Shochat 	ib_gid_to_mlx5_roce_addr(gid, attr, in_addr);
2883cca2606SAchiad Shochat 
2893cca2606SAchiad Shochat 	MLX5_SET(set_roce_address_in, in, roce_address_index, index);
2903cca2606SAchiad Shochat 	MLX5_SET(set_roce_address_in, in, opcode, MLX5_CMD_OP_SET_ROCE_ADDRESS);
2913cca2606SAchiad Shochat 	return mlx5_cmd_exec(dev->mdev, in, sizeof(in), out, sizeof(out));
2923cca2606SAchiad Shochat }
2933cca2606SAchiad Shochat 
2943cca2606SAchiad Shochat static int mlx5_ib_add_gid(struct ib_device *device, u8 port_num,
2953cca2606SAchiad Shochat 			   unsigned int index, const union ib_gid *gid,
2963cca2606SAchiad Shochat 			   const struct ib_gid_attr *attr,
2973cca2606SAchiad Shochat 			   __always_unused void **context)
2983cca2606SAchiad Shochat {
2993cca2606SAchiad Shochat 	return set_roce_addr(device, port_num, index, gid, attr);
3003cca2606SAchiad Shochat }
3013cca2606SAchiad Shochat 
3023cca2606SAchiad Shochat static int mlx5_ib_del_gid(struct ib_device *device, u8 port_num,
3033cca2606SAchiad Shochat 			   unsigned int index, __always_unused void **context)
3043cca2606SAchiad Shochat {
3053cca2606SAchiad Shochat 	return set_roce_addr(device, port_num, index, NULL, NULL);
3063cca2606SAchiad Shochat }
3073cca2606SAchiad Shochat 
3082811ba51SAchiad Shochat __be16 mlx5_get_roce_udp_sport(struct mlx5_ib_dev *dev, u8 port_num,
3092811ba51SAchiad Shochat 			       int index)
3102811ba51SAchiad Shochat {
3112811ba51SAchiad Shochat 	struct ib_gid_attr attr;
3122811ba51SAchiad Shochat 	union ib_gid gid;
3132811ba51SAchiad Shochat 
3142811ba51SAchiad Shochat 	if (ib_get_cached_gid(&dev->ib_dev, port_num, index, &gid, &attr))
3152811ba51SAchiad Shochat 		return 0;
3162811ba51SAchiad Shochat 
3172811ba51SAchiad Shochat 	if (!attr.ndev)
3182811ba51SAchiad Shochat 		return 0;
3192811ba51SAchiad Shochat 
3202811ba51SAchiad Shochat 	dev_put(attr.ndev);
3212811ba51SAchiad Shochat 
3222811ba51SAchiad Shochat 	if (attr.gid_type != IB_GID_TYPE_ROCE_UDP_ENCAP)
3232811ba51SAchiad Shochat 		return 0;
3242811ba51SAchiad Shochat 
3252811ba51SAchiad Shochat 	return cpu_to_be16(MLX5_CAP_ROCE(dev->mdev, r_roce_min_src_udp_port));
3262811ba51SAchiad Shochat }
3272811ba51SAchiad Shochat 
3281b5daf11SMajd Dibbiny static int mlx5_use_mad_ifc(struct mlx5_ib_dev *dev)
3291b5daf11SMajd Dibbiny {
3307fae6655SNoa Osherovich 	if (MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_IB)
331d603c809SEli Cohen 		return !MLX5_CAP_GEN(dev->mdev, ib_virt);
3327fae6655SNoa Osherovich 	return 0;
3331b5daf11SMajd Dibbiny }
3341b5daf11SMajd Dibbiny 
3351b5daf11SMajd Dibbiny enum {
3361b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_MAD,
3371b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_HCA,
3381b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_NIC,
3391b5daf11SMajd Dibbiny };
3401b5daf11SMajd Dibbiny 
3411b5daf11SMajd Dibbiny static int mlx5_get_vport_access_method(struct ib_device *ibdev)
3421b5daf11SMajd Dibbiny {
3431b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(to_mdev(ibdev)))
3441b5daf11SMajd Dibbiny 		return MLX5_VPORT_ACCESS_METHOD_MAD;
3451b5daf11SMajd Dibbiny 
346ebd61f68SAchiad Shochat 	if (mlx5_ib_port_link_layer(ibdev, 1) ==
3471b5daf11SMajd Dibbiny 	    IB_LINK_LAYER_ETHERNET)
3481b5daf11SMajd Dibbiny 		return MLX5_VPORT_ACCESS_METHOD_NIC;
3491b5daf11SMajd Dibbiny 
3501b5daf11SMajd Dibbiny 	return MLX5_VPORT_ACCESS_METHOD_HCA;
3511b5daf11SMajd Dibbiny }
3521b5daf11SMajd Dibbiny 
353da7525d2SEran Ben Elisha static void get_atomic_caps(struct mlx5_ib_dev *dev,
354da7525d2SEran Ben Elisha 			    struct ib_device_attr *props)
355da7525d2SEran Ben Elisha {
356da7525d2SEran Ben Elisha 	u8 tmp;
357da7525d2SEran Ben Elisha 	u8 atomic_operations = MLX5_CAP_ATOMIC(dev->mdev, atomic_operations);
358da7525d2SEran Ben Elisha 	u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_qp);
359da7525d2SEran Ben Elisha 	u8 atomic_req_8B_endianness_mode =
360da7525d2SEran Ben Elisha 		MLX5_CAP_ATOMIC(dev->mdev, atomic_req_8B_endianess_mode);
361da7525d2SEran Ben Elisha 
362da7525d2SEran Ben Elisha 	/* Check if HW supports 8 bytes standard atomic operations and capable
363da7525d2SEran Ben Elisha 	 * of host endianness respond
364da7525d2SEran Ben Elisha 	 */
365da7525d2SEran Ben Elisha 	tmp = MLX5_ATOMIC_OPS_CMP_SWAP | MLX5_ATOMIC_OPS_FETCH_ADD;
366da7525d2SEran Ben Elisha 	if (((atomic_operations & tmp) == tmp) &&
367da7525d2SEran Ben Elisha 	    (atomic_size_qp & MLX5_ATOMIC_SIZE_QP_8BYTES) &&
368da7525d2SEran Ben Elisha 	    (atomic_req_8B_endianness_mode)) {
369da7525d2SEran Ben Elisha 		props->atomic_cap = IB_ATOMIC_HCA;
370da7525d2SEran Ben Elisha 	} else {
371da7525d2SEran Ben Elisha 		props->atomic_cap = IB_ATOMIC_NONE;
372da7525d2SEran Ben Elisha 	}
373da7525d2SEran Ben Elisha }
374da7525d2SEran Ben Elisha 
3751b5daf11SMajd Dibbiny static int mlx5_query_system_image_guid(struct ib_device *ibdev,
3761b5daf11SMajd Dibbiny 					__be64 *sys_image_guid)
3771b5daf11SMajd Dibbiny {
3781b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
3791b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
3801b5daf11SMajd Dibbiny 	u64 tmp;
3811b5daf11SMajd Dibbiny 	int err;
3821b5daf11SMajd Dibbiny 
3831b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
3841b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
3851b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_system_image_guid(ibdev,
3861b5daf11SMajd Dibbiny 							    sys_image_guid);
3871b5daf11SMajd Dibbiny 
3881b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
3891b5daf11SMajd Dibbiny 		err = mlx5_query_hca_vport_system_image_guid(mdev, &tmp);
3903f89a643SAchiad Shochat 		break;
3913f89a643SAchiad Shochat 
3923f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
3933f89a643SAchiad Shochat 		err = mlx5_query_nic_vport_system_image_guid(mdev, &tmp);
3943f89a643SAchiad Shochat 		break;
3951b5daf11SMajd Dibbiny 
3961b5daf11SMajd Dibbiny 	default:
3971b5daf11SMajd Dibbiny 		return -EINVAL;
3981b5daf11SMajd Dibbiny 	}
3993f89a643SAchiad Shochat 
4003f89a643SAchiad Shochat 	if (!err)
4013f89a643SAchiad Shochat 		*sys_image_guid = cpu_to_be64(tmp);
4023f89a643SAchiad Shochat 
4033f89a643SAchiad Shochat 	return err;
4043f89a643SAchiad Shochat 
4051b5daf11SMajd Dibbiny }
4061b5daf11SMajd Dibbiny 
4071b5daf11SMajd Dibbiny static int mlx5_query_max_pkeys(struct ib_device *ibdev,
4081b5daf11SMajd Dibbiny 				u16 *max_pkeys)
4091b5daf11SMajd Dibbiny {
4101b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
4111b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
4121b5daf11SMajd Dibbiny 
4131b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
4141b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
4151b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_max_pkeys(ibdev, max_pkeys);
4161b5daf11SMajd Dibbiny 
4171b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
4181b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
4191b5daf11SMajd Dibbiny 		*max_pkeys = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev,
4201b5daf11SMajd Dibbiny 						pkey_table_size));
4211b5daf11SMajd Dibbiny 		return 0;
4221b5daf11SMajd Dibbiny 
4231b5daf11SMajd Dibbiny 	default:
4241b5daf11SMajd Dibbiny 		return -EINVAL;
4251b5daf11SMajd Dibbiny 	}
4261b5daf11SMajd Dibbiny }
4271b5daf11SMajd Dibbiny 
4281b5daf11SMajd Dibbiny static int mlx5_query_vendor_id(struct ib_device *ibdev,
4291b5daf11SMajd Dibbiny 				u32 *vendor_id)
4301b5daf11SMajd Dibbiny {
4311b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
4321b5daf11SMajd Dibbiny 
4331b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
4341b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
4351b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_vendor_id(ibdev, vendor_id);
4361b5daf11SMajd Dibbiny 
4371b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
4381b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
4391b5daf11SMajd Dibbiny 		return mlx5_core_query_vendor_id(dev->mdev, vendor_id);
4401b5daf11SMajd Dibbiny 
4411b5daf11SMajd Dibbiny 	default:
4421b5daf11SMajd Dibbiny 		return -EINVAL;
4431b5daf11SMajd Dibbiny 	}
4441b5daf11SMajd Dibbiny }
4451b5daf11SMajd Dibbiny 
4461b5daf11SMajd Dibbiny static int mlx5_query_node_guid(struct mlx5_ib_dev *dev,
4471b5daf11SMajd Dibbiny 				__be64 *node_guid)
4481b5daf11SMajd Dibbiny {
4491b5daf11SMajd Dibbiny 	u64 tmp;
4501b5daf11SMajd Dibbiny 	int err;
4511b5daf11SMajd Dibbiny 
4521b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(&dev->ib_dev)) {
4531b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
4541b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_node_guid(dev, node_guid);
4551b5daf11SMajd Dibbiny 
4561b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
4571b5daf11SMajd Dibbiny 		err = mlx5_query_hca_vport_node_guid(dev->mdev, &tmp);
4583f89a643SAchiad Shochat 		break;
4593f89a643SAchiad Shochat 
4603f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
4613f89a643SAchiad Shochat 		err = mlx5_query_nic_vport_node_guid(dev->mdev, &tmp);
4623f89a643SAchiad Shochat 		break;
4631b5daf11SMajd Dibbiny 
4641b5daf11SMajd Dibbiny 	default:
4651b5daf11SMajd Dibbiny 		return -EINVAL;
4661b5daf11SMajd Dibbiny 	}
4673f89a643SAchiad Shochat 
4683f89a643SAchiad Shochat 	if (!err)
4693f89a643SAchiad Shochat 		*node_guid = cpu_to_be64(tmp);
4703f89a643SAchiad Shochat 
4713f89a643SAchiad Shochat 	return err;
4721b5daf11SMajd Dibbiny }
4731b5daf11SMajd Dibbiny 
4741b5daf11SMajd Dibbiny struct mlx5_reg_node_desc {
475bd99fdeaSYuval Shaia 	u8	desc[IB_DEVICE_NODE_DESC_MAX];
4761b5daf11SMajd Dibbiny };
4771b5daf11SMajd Dibbiny 
4781b5daf11SMajd Dibbiny static int mlx5_query_node_desc(struct mlx5_ib_dev *dev, char *node_desc)
4791b5daf11SMajd Dibbiny {
4801b5daf11SMajd Dibbiny 	struct mlx5_reg_node_desc in;
4811b5daf11SMajd Dibbiny 
4821b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(dev))
4831b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_node_desc(dev, node_desc);
4841b5daf11SMajd Dibbiny 
4851b5daf11SMajd Dibbiny 	memset(&in, 0, sizeof(in));
4861b5daf11SMajd Dibbiny 
4871b5daf11SMajd Dibbiny 	return mlx5_core_access_reg(dev->mdev, &in, sizeof(in), node_desc,
4881b5daf11SMajd Dibbiny 				    sizeof(struct mlx5_reg_node_desc),
4891b5daf11SMajd Dibbiny 				    MLX5_REG_NODE_DESC, 0, 0);
4901b5daf11SMajd Dibbiny }
4911b5daf11SMajd Dibbiny 
492e126ba97SEli Cohen static int mlx5_ib_query_device(struct ib_device *ibdev,
4932528e33eSMatan Barak 				struct ib_device_attr *props,
4942528e33eSMatan Barak 				struct ib_udata *uhw)
495e126ba97SEli Cohen {
496e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
497938fe83cSSaeed Mahameed 	struct mlx5_core_dev *mdev = dev->mdev;
498e126ba97SEli Cohen 	int err = -ENOMEM;
499288c01b7SEli Cohen 	int max_sq_desc;
500e126ba97SEli Cohen 	int max_rq_sg;
501e126ba97SEli Cohen 	int max_sq_sg;
502e0238a6aSSagi Grimberg 	u64 min_page_size = 1ull << MLX5_CAP_GEN(mdev, log_pg_sz);
503402ca536SBodong Wang 	struct mlx5_ib_query_device_resp resp = {};
504402ca536SBodong Wang 	size_t resp_len;
505402ca536SBodong Wang 	u64 max_tso;
506e126ba97SEli Cohen 
507402ca536SBodong Wang 	resp_len = sizeof(resp.comp_mask) + sizeof(resp.response_length);
508402ca536SBodong Wang 	if (uhw->outlen && uhw->outlen < resp_len)
509402ca536SBodong Wang 		return -EINVAL;
510402ca536SBodong Wang 	else
511402ca536SBodong Wang 		resp.response_length = resp_len;
512402ca536SBodong Wang 
513402ca536SBodong Wang 	if (uhw->inlen && !ib_is_udata_cleared(uhw, 0, uhw->inlen))
5142528e33eSMatan Barak 		return -EINVAL;
5152528e33eSMatan Barak 
516e126ba97SEli Cohen 	memset(props, 0, sizeof(*props));
5171b5daf11SMajd Dibbiny 	err = mlx5_query_system_image_guid(ibdev,
5181b5daf11SMajd Dibbiny 					   &props->sys_image_guid);
5191b5daf11SMajd Dibbiny 	if (err)
5201b5daf11SMajd Dibbiny 		return err;
5211b5daf11SMajd Dibbiny 
5221b5daf11SMajd Dibbiny 	err = mlx5_query_max_pkeys(ibdev, &props->max_pkeys);
5231b5daf11SMajd Dibbiny 	if (err)
5241b5daf11SMajd Dibbiny 		return err;
5251b5daf11SMajd Dibbiny 
5261b5daf11SMajd Dibbiny 	err = mlx5_query_vendor_id(ibdev, &props->vendor_id);
5271b5daf11SMajd Dibbiny 	if (err)
5281b5daf11SMajd Dibbiny 		return err;
529e126ba97SEli Cohen 
5309603b61dSJack Morgenstein 	props->fw_ver = ((u64)fw_rev_maj(dev->mdev) << 32) |
5319603b61dSJack Morgenstein 		(fw_rev_min(dev->mdev) << 16) |
5329603b61dSJack Morgenstein 		fw_rev_sub(dev->mdev);
533e126ba97SEli Cohen 	props->device_cap_flags    = IB_DEVICE_CHANGE_PHY_PORT |
534e126ba97SEli Cohen 		IB_DEVICE_PORT_ACTIVE_EVENT		|
535e126ba97SEli Cohen 		IB_DEVICE_SYS_IMAGE_GUID		|
5361a4c3a3dSEli Cohen 		IB_DEVICE_RC_RNR_NAK_GEN;
537938fe83cSSaeed Mahameed 
538938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, pkv))
539e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
540938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, qkv))
541e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
542938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, apm))
543e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
544938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, xrc))
545e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_XRC;
546d2370e0aSMatan Barak 	if (MLX5_CAP_GEN(mdev, imaicl)) {
547d2370e0aSMatan Barak 		props->device_cap_flags |= IB_DEVICE_MEM_WINDOW |
548d2370e0aSMatan Barak 					   IB_DEVICE_MEM_WINDOW_TYPE_2B;
549d2370e0aSMatan Barak 		props->max_mw = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
550b005d316SSagi Grimberg 		/* We support 'Gappy' memory registration too */
551b005d316SSagi Grimberg 		props->device_cap_flags |= IB_DEVICE_SG_GAPS_REG;
552d2370e0aSMatan Barak 	}
553e126ba97SEli Cohen 	props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
554938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, sho)) {
5552dea9094SSagi Grimberg 		props->device_cap_flags |= IB_DEVICE_SIGNATURE_HANDOVER;
5562dea9094SSagi Grimberg 		/* At this stage no support for signature handover */
5572dea9094SSagi Grimberg 		props->sig_prot_cap = IB_PROT_T10DIF_TYPE_1 |
5582dea9094SSagi Grimberg 				      IB_PROT_T10DIF_TYPE_2 |
5592dea9094SSagi Grimberg 				      IB_PROT_T10DIF_TYPE_3;
5602dea9094SSagi Grimberg 		props->sig_guard_cap = IB_GUARD_T10DIF_CRC |
5612dea9094SSagi Grimberg 				       IB_GUARD_T10DIF_CSUM;
5622dea9094SSagi Grimberg 	}
563938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, block_lb_mc))
564f360d88aSEli Cohen 		props->device_cap_flags |= IB_DEVICE_BLOCK_MULTICAST_LOOPBACK;
565e126ba97SEli Cohen 
566402ca536SBodong Wang 	if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads)) {
567402ca536SBodong Wang 		if (MLX5_CAP_ETH(mdev, csum_cap))
56888115fe7SBodong Wang 			props->device_cap_flags |= IB_DEVICE_RAW_IP_CSUM;
56988115fe7SBodong Wang 
570402ca536SBodong Wang 		if (field_avail(typeof(resp), tso_caps, uhw->outlen)) {
571402ca536SBodong Wang 			max_tso = MLX5_CAP_ETH(mdev, max_lso_cap);
572402ca536SBodong Wang 			if (max_tso) {
573402ca536SBodong Wang 				resp.tso_caps.max_tso = 1 << max_tso;
574402ca536SBodong Wang 				resp.tso_caps.supported_qpts |=
575402ca536SBodong Wang 					1 << IB_QPT_RAW_PACKET;
576402ca536SBodong Wang 				resp.response_length += sizeof(resp.tso_caps);
577402ca536SBodong Wang 			}
578402ca536SBodong Wang 		}
57931f69a82SYishai Hadas 
58031f69a82SYishai Hadas 		if (field_avail(typeof(resp), rss_caps, uhw->outlen)) {
58131f69a82SYishai Hadas 			resp.rss_caps.rx_hash_function =
58231f69a82SYishai Hadas 						MLX5_RX_HASH_FUNC_TOEPLITZ;
58331f69a82SYishai Hadas 			resp.rss_caps.rx_hash_fields_mask =
58431f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_IPV4 |
58531f69a82SYishai Hadas 						MLX5_RX_HASH_DST_IPV4 |
58631f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_IPV6 |
58731f69a82SYishai Hadas 						MLX5_RX_HASH_DST_IPV6 |
58831f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_PORT_TCP |
58931f69a82SYishai Hadas 						MLX5_RX_HASH_DST_PORT_TCP |
59031f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_PORT_UDP |
59131f69a82SYishai Hadas 						MLX5_RX_HASH_DST_PORT_UDP;
59231f69a82SYishai Hadas 			resp.response_length += sizeof(resp.rss_caps);
59331f69a82SYishai Hadas 		}
59431f69a82SYishai Hadas 	} else {
59531f69a82SYishai Hadas 		if (field_avail(typeof(resp), tso_caps, uhw->outlen))
59631f69a82SYishai Hadas 			resp.response_length += sizeof(resp.tso_caps);
59731f69a82SYishai Hadas 		if (field_avail(typeof(resp), rss_caps, uhw->outlen))
59831f69a82SYishai Hadas 			resp.response_length += sizeof(resp.rss_caps);
599402ca536SBodong Wang 	}
600402ca536SBodong Wang 
601f0313965SErez Shitrit 	if (MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) {
602f0313965SErez Shitrit 		props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
603f0313965SErez Shitrit 		props->device_cap_flags |= IB_DEVICE_UD_TSO;
604f0313965SErez Shitrit 	}
605f0313965SErez Shitrit 
606cff5a0f3SMajd Dibbiny 	if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
607cff5a0f3SMajd Dibbiny 	    MLX5_CAP_ETH(dev->mdev, scatter_fcs))
608cff5a0f3SMajd Dibbiny 		props->device_cap_flags |= IB_DEVICE_RAW_SCATTER_FCS;
609cff5a0f3SMajd Dibbiny 
610da6d6ba3SMaor Gottlieb 	if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS))
611da6d6ba3SMaor Gottlieb 		props->device_cap_flags |= IB_DEVICE_MANAGED_FLOW_STEERING;
612da6d6ba3SMaor Gottlieb 
6131b5daf11SMajd Dibbiny 	props->vendor_part_id	   = mdev->pdev->device;
6141b5daf11SMajd Dibbiny 	props->hw_ver		   = mdev->pdev->revision;
615e126ba97SEli Cohen 
616e126ba97SEli Cohen 	props->max_mr_size	   = ~0ull;
617e0238a6aSSagi Grimberg 	props->page_size_cap	   = ~(min_page_size - 1);
618938fe83cSSaeed Mahameed 	props->max_qp		   = 1 << MLX5_CAP_GEN(mdev, log_max_qp);
619938fe83cSSaeed Mahameed 	props->max_qp_wr	   = 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
620938fe83cSSaeed Mahameed 	max_rq_sg =  MLX5_CAP_GEN(mdev, max_wqe_sz_rq) /
621938fe83cSSaeed Mahameed 		     sizeof(struct mlx5_wqe_data_seg);
622288c01b7SEli Cohen 	max_sq_desc = min_t(int, MLX5_CAP_GEN(mdev, max_wqe_sz_sq), 512);
623288c01b7SEli Cohen 	max_sq_sg = (max_sq_desc - sizeof(struct mlx5_wqe_ctrl_seg) -
624288c01b7SEli Cohen 		     sizeof(struct mlx5_wqe_raddr_seg)) /
625e126ba97SEli Cohen 		sizeof(struct mlx5_wqe_data_seg);
626e126ba97SEli Cohen 	props->max_sge = min(max_rq_sg, max_sq_sg);
627986ef95eSSagi Grimberg 	props->max_sge_rd	   = MLX5_MAX_SGE_RD;
628938fe83cSSaeed Mahameed 	props->max_cq		   = 1 << MLX5_CAP_GEN(mdev, log_max_cq);
6299f177686SLeon Romanovsky 	props->max_cqe = (1 << MLX5_CAP_GEN(mdev, log_max_cq_sz)) - 1;
630938fe83cSSaeed Mahameed 	props->max_mr		   = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
631938fe83cSSaeed Mahameed 	props->max_pd		   = 1 << MLX5_CAP_GEN(mdev, log_max_pd);
632938fe83cSSaeed Mahameed 	props->max_qp_rd_atom	   = 1 << MLX5_CAP_GEN(mdev, log_max_ra_req_qp);
633938fe83cSSaeed Mahameed 	props->max_qp_init_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_res_qp);
634938fe83cSSaeed Mahameed 	props->max_srq		   = 1 << MLX5_CAP_GEN(mdev, log_max_srq);
635938fe83cSSaeed Mahameed 	props->max_srq_wr = (1 << MLX5_CAP_GEN(mdev, log_max_srq_sz)) - 1;
636938fe83cSSaeed Mahameed 	props->local_ca_ack_delay  = MLX5_CAP_GEN(mdev, local_ca_ack_delay);
637e126ba97SEli Cohen 	props->max_res_rd_atom	   = props->max_qp_rd_atom * props->max_qp;
638e126ba97SEli Cohen 	props->max_srq_sge	   = max_rq_sg - 1;
639911f4331SSagi Grimberg 	props->max_fast_reg_page_list_len =
640911f4331SSagi Grimberg 		1 << MLX5_CAP_GEN(mdev, log_max_klm_list_size);
641da7525d2SEran Ben Elisha 	get_atomic_caps(dev, props);
64281bea28fSEli Cohen 	props->masked_atomic_cap   = IB_ATOMIC_NONE;
643938fe83cSSaeed Mahameed 	props->max_mcast_grp	   = 1 << MLX5_CAP_GEN(mdev, log_max_mcg);
644938fe83cSSaeed Mahameed 	props->max_mcast_qp_attach = MLX5_CAP_GEN(mdev, max_qp_mcg);
645e126ba97SEli Cohen 	props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
646e126ba97SEli Cohen 					   props->max_mcast_grp;
647e126ba97SEli Cohen 	props->max_map_per_fmr = INT_MAX; /* no limit in ConnectIB */
64886695a65SMaor Gottlieb 	props->max_ah = INT_MAX;
6497c60bcbbSMatan Barak 	props->hca_core_clock = MLX5_CAP_GEN(mdev, device_frequency_khz);
6507c60bcbbSMatan Barak 	props->timestamp_mask = 0x7FFFFFFFFFFFFFFFULL;
651e126ba97SEli Cohen 
6528cdd312cSHaggai Eran #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
653938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, pg))
6548cdd312cSHaggai Eran 		props->device_cap_flags |= IB_DEVICE_ON_DEMAND_PAGING;
6558cdd312cSHaggai Eran 	props->odp_caps = dev->odp_caps;
6568cdd312cSHaggai Eran #endif
6578cdd312cSHaggai Eran 
658051f2630SLeon Romanovsky 	if (MLX5_CAP_GEN(mdev, cd))
659051f2630SLeon Romanovsky 		props->device_cap_flags |= IB_DEVICE_CROSS_CHANNEL;
660051f2630SLeon Romanovsky 
661eff901d3SEli Cohen 	if (!mlx5_core_is_pf(mdev))
662eff901d3SEli Cohen 		props->device_cap_flags |= IB_DEVICE_VIRTUAL_FUNCTION;
663eff901d3SEli Cohen 
66431f69a82SYishai Hadas 	if (mlx5_ib_port_link_layer(ibdev, 1) ==
66531f69a82SYishai Hadas 	    IB_LINK_LAYER_ETHERNET) {
66631f69a82SYishai Hadas 		props->rss_caps.max_rwq_indirection_tables =
66731f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt);
66831f69a82SYishai Hadas 		props->rss_caps.max_rwq_indirection_table_size =
66931f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt_size);
67031f69a82SYishai Hadas 		props->rss_caps.supported_qpts = 1 << IB_QPT_RAW_PACKET;
67131f69a82SYishai Hadas 		props->max_wq_type_rq =
67231f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rq);
67331f69a82SYishai Hadas 	}
67431f69a82SYishai Hadas 
675191ded4aSBodong Wang 	if (field_avail(typeof(resp), mlx5_ib_support_multi_pkt_send_wqes,
676191ded4aSBodong Wang 			uhw->outlen)) {
677191ded4aSBodong Wang 		resp.mlx5_ib_support_multi_pkt_send_wqes =
678191ded4aSBodong Wang 			MLX5_CAP_ETH(mdev, multi_pkt_send_wqe);
679191ded4aSBodong Wang 		resp.response_length +=
680191ded4aSBodong Wang 			sizeof(resp.mlx5_ib_support_multi_pkt_send_wqes);
681191ded4aSBodong Wang 	}
682191ded4aSBodong Wang 
683191ded4aSBodong Wang 	if (field_avail(typeof(resp), reserved, uhw->outlen))
684191ded4aSBodong Wang 		resp.response_length += sizeof(resp.reserved);
685191ded4aSBodong Wang 
686*7e43a2a5SBodong Wang 	if (field_avail(typeof(resp), cqe_comp_caps, uhw->outlen)) {
687*7e43a2a5SBodong Wang 		resp.cqe_comp_caps.max_num =
688*7e43a2a5SBodong Wang 			MLX5_CAP_GEN(dev->mdev, cqe_compression) ?
689*7e43a2a5SBodong Wang 			MLX5_CAP_GEN(dev->mdev, cqe_compression_max_num) : 0;
690*7e43a2a5SBodong Wang 		resp.cqe_comp_caps.supported_format =
691*7e43a2a5SBodong Wang 			MLX5_IB_CQE_RES_FORMAT_HASH |
692*7e43a2a5SBodong Wang 			MLX5_IB_CQE_RES_FORMAT_CSUM;
693*7e43a2a5SBodong Wang 		resp.response_length += sizeof(resp.cqe_comp_caps);
694*7e43a2a5SBodong Wang 	}
695*7e43a2a5SBodong Wang 
696402ca536SBodong Wang 	if (uhw->outlen) {
697402ca536SBodong Wang 		err = ib_copy_to_udata(uhw, &resp, resp.response_length);
698402ca536SBodong Wang 
699402ca536SBodong Wang 		if (err)
700402ca536SBodong Wang 			return err;
701402ca536SBodong Wang 	}
702402ca536SBodong Wang 
7031b5daf11SMajd Dibbiny 	return 0;
7041b5daf11SMajd Dibbiny }
705e126ba97SEli Cohen 
7061b5daf11SMajd Dibbiny enum mlx5_ib_width {
7071b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_1X	= 1 << 0,
7081b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_2X	= 1 << 1,
7091b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_4X	= 1 << 2,
7101b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_8X	= 1 << 3,
7111b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_12X	= 1 << 4
7121b5daf11SMajd Dibbiny };
7131b5daf11SMajd Dibbiny 
7141b5daf11SMajd Dibbiny static int translate_active_width(struct ib_device *ibdev, u8 active_width,
7151b5daf11SMajd Dibbiny 				  u8 *ib_width)
7161b5daf11SMajd Dibbiny {
7171b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
7181b5daf11SMajd Dibbiny 	int err = 0;
7191b5daf11SMajd Dibbiny 
7201b5daf11SMajd Dibbiny 	if (active_width & MLX5_IB_WIDTH_1X) {
7211b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_1X;
7221b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_2X) {
7231b5daf11SMajd Dibbiny 		mlx5_ib_dbg(dev, "active_width %d is not supported by IB spec\n",
7241b5daf11SMajd Dibbiny 			    (int)active_width);
7251b5daf11SMajd Dibbiny 		err = -EINVAL;
7261b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_4X) {
7271b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_4X;
7281b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_8X) {
7291b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_8X;
7301b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_12X) {
7311b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_12X;
7321b5daf11SMajd Dibbiny 	} else {
7331b5daf11SMajd Dibbiny 		mlx5_ib_dbg(dev, "Invalid active_width %d\n",
7341b5daf11SMajd Dibbiny 			    (int)active_width);
7351b5daf11SMajd Dibbiny 		err = -EINVAL;
7361b5daf11SMajd Dibbiny 	}
7371b5daf11SMajd Dibbiny 
7381b5daf11SMajd Dibbiny 	return err;
7391b5daf11SMajd Dibbiny }
7401b5daf11SMajd Dibbiny 
7411b5daf11SMajd Dibbiny static int mlx5_mtu_to_ib_mtu(int mtu)
7421b5daf11SMajd Dibbiny {
7431b5daf11SMajd Dibbiny 	switch (mtu) {
7441b5daf11SMajd Dibbiny 	case 256: return 1;
7451b5daf11SMajd Dibbiny 	case 512: return 2;
7461b5daf11SMajd Dibbiny 	case 1024: return 3;
7471b5daf11SMajd Dibbiny 	case 2048: return 4;
7481b5daf11SMajd Dibbiny 	case 4096: return 5;
7491b5daf11SMajd Dibbiny 	default:
7501b5daf11SMajd Dibbiny 		pr_warn("invalid mtu\n");
7511b5daf11SMajd Dibbiny 		return -1;
7521b5daf11SMajd Dibbiny 	}
7531b5daf11SMajd Dibbiny }
7541b5daf11SMajd Dibbiny 
7551b5daf11SMajd Dibbiny enum ib_max_vl_num {
7561b5daf11SMajd Dibbiny 	__IB_MAX_VL_0		= 1,
7571b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_1		= 2,
7581b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_3		= 3,
7591b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_7		= 4,
7601b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_14	= 5,
7611b5daf11SMajd Dibbiny };
7621b5daf11SMajd Dibbiny 
7631b5daf11SMajd Dibbiny enum mlx5_vl_hw_cap {
7641b5daf11SMajd Dibbiny 	MLX5_VL_HW_0	= 1,
7651b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_1	= 2,
7661b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_2	= 3,
7671b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_3	= 4,
7681b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_4	= 5,
7691b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_5	= 6,
7701b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_6	= 7,
7711b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_7	= 8,
7721b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_14	= 15
7731b5daf11SMajd Dibbiny };
7741b5daf11SMajd Dibbiny 
7751b5daf11SMajd Dibbiny static int translate_max_vl_num(struct ib_device *ibdev, u8 vl_hw_cap,
7761b5daf11SMajd Dibbiny 				u8 *max_vl_num)
7771b5daf11SMajd Dibbiny {
7781b5daf11SMajd Dibbiny 	switch (vl_hw_cap) {
7791b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0:
7801b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0;
7811b5daf11SMajd Dibbiny 		break;
7821b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_1:
7831b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_1;
7841b5daf11SMajd Dibbiny 		break;
7851b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_3:
7861b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_3;
7871b5daf11SMajd Dibbiny 		break;
7881b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_7:
7891b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_7;
7901b5daf11SMajd Dibbiny 		break;
7911b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_14:
7921b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_14;
7931b5daf11SMajd Dibbiny 		break;
7941b5daf11SMajd Dibbiny 
7951b5daf11SMajd Dibbiny 	default:
7961b5daf11SMajd Dibbiny 		return -EINVAL;
7971b5daf11SMajd Dibbiny 	}
7981b5daf11SMajd Dibbiny 
7991b5daf11SMajd Dibbiny 	return 0;
8001b5daf11SMajd Dibbiny }
8011b5daf11SMajd Dibbiny 
8021b5daf11SMajd Dibbiny static int mlx5_query_hca_port(struct ib_device *ibdev, u8 port,
8031b5daf11SMajd Dibbiny 			       struct ib_port_attr *props)
8041b5daf11SMajd Dibbiny {
8051b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
8061b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
8071b5daf11SMajd Dibbiny 	struct mlx5_hca_vport_context *rep;
808046339eaSSaeed Mahameed 	u16 max_mtu;
809046339eaSSaeed Mahameed 	u16 oper_mtu;
8101b5daf11SMajd Dibbiny 	int err;
8111b5daf11SMajd Dibbiny 	u8 ib_link_width_oper;
8121b5daf11SMajd Dibbiny 	u8 vl_hw_cap;
8131b5daf11SMajd Dibbiny 
8141b5daf11SMajd Dibbiny 	rep = kzalloc(sizeof(*rep), GFP_KERNEL);
8151b5daf11SMajd Dibbiny 	if (!rep) {
8161b5daf11SMajd Dibbiny 		err = -ENOMEM;
8171b5daf11SMajd Dibbiny 		goto out;
8181b5daf11SMajd Dibbiny 	}
8191b5daf11SMajd Dibbiny 
8201b5daf11SMajd Dibbiny 	memset(props, 0, sizeof(*props));
8211b5daf11SMajd Dibbiny 
8221b5daf11SMajd Dibbiny 	err = mlx5_query_hca_vport_context(mdev, 0, port, 0, rep);
8231b5daf11SMajd Dibbiny 	if (err)
8241b5daf11SMajd Dibbiny 		goto out;
8251b5daf11SMajd Dibbiny 
8261b5daf11SMajd Dibbiny 	props->lid		= rep->lid;
8271b5daf11SMajd Dibbiny 	props->lmc		= rep->lmc;
8281b5daf11SMajd Dibbiny 	props->sm_lid		= rep->sm_lid;
8291b5daf11SMajd Dibbiny 	props->sm_sl		= rep->sm_sl;
8301b5daf11SMajd Dibbiny 	props->state		= rep->vport_state;
8311b5daf11SMajd Dibbiny 	props->phys_state	= rep->port_physical_state;
8321b5daf11SMajd Dibbiny 	props->port_cap_flags	= rep->cap_mask1;
8331b5daf11SMajd Dibbiny 	props->gid_tbl_len	= mlx5_get_gid_table_len(MLX5_CAP_GEN(mdev, gid_table_size));
8341b5daf11SMajd Dibbiny 	props->max_msg_sz	= 1 << MLX5_CAP_GEN(mdev, log_max_msg);
8351b5daf11SMajd Dibbiny 	props->pkey_tbl_len	= mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, pkey_table_size));
8361b5daf11SMajd Dibbiny 	props->bad_pkey_cntr	= rep->pkey_violation_counter;
8371b5daf11SMajd Dibbiny 	props->qkey_viol_cntr	= rep->qkey_violation_counter;
8381b5daf11SMajd Dibbiny 	props->subnet_timeout	= rep->subnet_timeout;
8391b5daf11SMajd Dibbiny 	props->init_type_reply	= rep->init_type_reply;
840eff901d3SEli Cohen 	props->grh_required	= rep->grh_required;
8411b5daf11SMajd Dibbiny 
8421b5daf11SMajd Dibbiny 	err = mlx5_query_port_link_width_oper(mdev, &ib_link_width_oper, port);
8431b5daf11SMajd Dibbiny 	if (err)
8441b5daf11SMajd Dibbiny 		goto out;
8451b5daf11SMajd Dibbiny 
8461b5daf11SMajd Dibbiny 	err = translate_active_width(ibdev, ib_link_width_oper,
8471b5daf11SMajd Dibbiny 				     &props->active_width);
8481b5daf11SMajd Dibbiny 	if (err)
8491b5daf11SMajd Dibbiny 		goto out;
850d5beb7f2SNoa Osherovich 	err = mlx5_query_port_ib_proto_oper(mdev, &props->active_speed, port);
8511b5daf11SMajd Dibbiny 	if (err)
8521b5daf11SMajd Dibbiny 		goto out;
8531b5daf11SMajd Dibbiny 
854facc9699SSaeed Mahameed 	mlx5_query_port_max_mtu(mdev, &max_mtu, port);
8551b5daf11SMajd Dibbiny 
8561b5daf11SMajd Dibbiny 	props->max_mtu = mlx5_mtu_to_ib_mtu(max_mtu);
8571b5daf11SMajd Dibbiny 
858facc9699SSaeed Mahameed 	mlx5_query_port_oper_mtu(mdev, &oper_mtu, port);
8591b5daf11SMajd Dibbiny 
8601b5daf11SMajd Dibbiny 	props->active_mtu = mlx5_mtu_to_ib_mtu(oper_mtu);
8611b5daf11SMajd Dibbiny 
8621b5daf11SMajd Dibbiny 	err = mlx5_query_port_vl_hw_cap(mdev, &vl_hw_cap, port);
8631b5daf11SMajd Dibbiny 	if (err)
8641b5daf11SMajd Dibbiny 		goto out;
8651b5daf11SMajd Dibbiny 
8661b5daf11SMajd Dibbiny 	err = translate_max_vl_num(ibdev, vl_hw_cap,
8671b5daf11SMajd Dibbiny 				   &props->max_vl_num);
8681b5daf11SMajd Dibbiny out:
8691b5daf11SMajd Dibbiny 	kfree(rep);
870e126ba97SEli Cohen 	return err;
871e126ba97SEli Cohen }
872e126ba97SEli Cohen 
873e126ba97SEli Cohen int mlx5_ib_query_port(struct ib_device *ibdev, u8 port,
874e126ba97SEli Cohen 		       struct ib_port_attr *props)
875e126ba97SEli Cohen {
8761b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
8771b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
8781b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_port(ibdev, port, props);
879e126ba97SEli Cohen 
8801b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
8811b5daf11SMajd Dibbiny 		return mlx5_query_hca_port(ibdev, port, props);
8821b5daf11SMajd Dibbiny 
8833f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
8843f89a643SAchiad Shochat 		return mlx5_query_port_roce(ibdev, port, props);
8853f89a643SAchiad Shochat 
8861b5daf11SMajd Dibbiny 	default:
887e126ba97SEli Cohen 		return -EINVAL;
888e126ba97SEli Cohen 	}
889e126ba97SEli Cohen }
890e126ba97SEli Cohen 
891e126ba97SEli Cohen static int mlx5_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
892e126ba97SEli Cohen 			     union ib_gid *gid)
893e126ba97SEli Cohen {
8941b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
8951b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
896e126ba97SEli Cohen 
8971b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
8981b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
8991b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_gids(ibdev, port, index, gid);
900e126ba97SEli Cohen 
9011b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
9021b5daf11SMajd Dibbiny 		return mlx5_query_hca_vport_gid(mdev, 0, port, 0, index, gid);
903e126ba97SEli Cohen 
9041b5daf11SMajd Dibbiny 	default:
9051b5daf11SMajd Dibbiny 		return -EINVAL;
9061b5daf11SMajd Dibbiny 	}
907e126ba97SEli Cohen 
908e126ba97SEli Cohen }
909e126ba97SEli Cohen 
910e126ba97SEli Cohen static int mlx5_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
911e126ba97SEli Cohen 			      u16 *pkey)
912e126ba97SEli Cohen {
9131b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
9141b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
915e126ba97SEli Cohen 
9161b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
9171b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
9181b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_pkey(ibdev, port, index, pkey);
919e126ba97SEli Cohen 
9201b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
9211b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
9221b5daf11SMajd Dibbiny 		return mlx5_query_hca_vport_pkey(mdev, 0, port,  0, index,
9231b5daf11SMajd Dibbiny 						 pkey);
9241b5daf11SMajd Dibbiny 	default:
9251b5daf11SMajd Dibbiny 		return -EINVAL;
926e126ba97SEli Cohen 	}
9271b5daf11SMajd Dibbiny }
928e126ba97SEli Cohen 
929e126ba97SEli Cohen static int mlx5_ib_modify_device(struct ib_device *ibdev, int mask,
930e126ba97SEli Cohen 				 struct ib_device_modify *props)
931e126ba97SEli Cohen {
932e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
933e126ba97SEli Cohen 	struct mlx5_reg_node_desc in;
934e126ba97SEli Cohen 	struct mlx5_reg_node_desc out;
935e126ba97SEli Cohen 	int err;
936e126ba97SEli Cohen 
937e126ba97SEli Cohen 	if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
938e126ba97SEli Cohen 		return -EOPNOTSUPP;
939e126ba97SEli Cohen 
940e126ba97SEli Cohen 	if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
941e126ba97SEli Cohen 		return 0;
942e126ba97SEli Cohen 
943e126ba97SEli Cohen 	/*
944e126ba97SEli Cohen 	 * If possible, pass node desc to FW, so it can generate
945e126ba97SEli Cohen 	 * a 144 trap.  If cmd fails, just ignore.
946e126ba97SEli Cohen 	 */
947bd99fdeaSYuval Shaia 	memcpy(&in, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
9489603b61dSJack Morgenstein 	err = mlx5_core_access_reg(dev->mdev, &in, sizeof(in), &out,
949e126ba97SEli Cohen 				   sizeof(out), MLX5_REG_NODE_DESC, 0, 1);
950e126ba97SEli Cohen 	if (err)
951e126ba97SEli Cohen 		return err;
952e126ba97SEli Cohen 
953bd99fdeaSYuval Shaia 	memcpy(ibdev->node_desc, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
954e126ba97SEli Cohen 
955e126ba97SEli Cohen 	return err;
956e126ba97SEli Cohen }
957e126ba97SEli Cohen 
958e126ba97SEli Cohen static int mlx5_ib_modify_port(struct ib_device *ibdev, u8 port, int mask,
959e126ba97SEli Cohen 			       struct ib_port_modify *props)
960e126ba97SEli Cohen {
961e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
962e126ba97SEli Cohen 	struct ib_port_attr attr;
963e126ba97SEli Cohen 	u32 tmp;
964e126ba97SEli Cohen 	int err;
965e126ba97SEli Cohen 
966e126ba97SEli Cohen 	mutex_lock(&dev->cap_mask_mutex);
967e126ba97SEli Cohen 
968e126ba97SEli Cohen 	err = mlx5_ib_query_port(ibdev, port, &attr);
969e126ba97SEli Cohen 	if (err)
970e126ba97SEli Cohen 		goto out;
971e126ba97SEli Cohen 
972e126ba97SEli Cohen 	tmp = (attr.port_cap_flags | props->set_port_cap_mask) &
973e126ba97SEli Cohen 		~props->clr_port_cap_mask;
974e126ba97SEli Cohen 
9759603b61dSJack Morgenstein 	err = mlx5_set_port_caps(dev->mdev, port, tmp);
976e126ba97SEli Cohen 
977e126ba97SEli Cohen out:
978e126ba97SEli Cohen 	mutex_unlock(&dev->cap_mask_mutex);
979e126ba97SEli Cohen 	return err;
980e126ba97SEli Cohen }
981e126ba97SEli Cohen 
982e126ba97SEli Cohen static struct ib_ucontext *mlx5_ib_alloc_ucontext(struct ib_device *ibdev,
983e126ba97SEli Cohen 						  struct ib_udata *udata)
984e126ba97SEli Cohen {
985e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
986b368d7cbSMatan Barak 	struct mlx5_ib_alloc_ucontext_req_v2 req = {};
987b368d7cbSMatan Barak 	struct mlx5_ib_alloc_ucontext_resp resp = {};
988e126ba97SEli Cohen 	struct mlx5_ib_ucontext *context;
989e126ba97SEli Cohen 	struct mlx5_uuar_info *uuari;
990e126ba97SEli Cohen 	struct mlx5_uar *uars;
991c1be5232SEli Cohen 	int gross_uuars;
992e126ba97SEli Cohen 	int num_uars;
99378c0f98cSEli Cohen 	int ver;
994e126ba97SEli Cohen 	int uuarn;
995e126ba97SEli Cohen 	int err;
996e126ba97SEli Cohen 	int i;
997f241e749SJack Morgenstein 	size_t reqlen;
998a168a41cSMajd Dibbiny 	size_t min_req_v2 = offsetof(struct mlx5_ib_alloc_ucontext_req_v2,
999a168a41cSMajd Dibbiny 				     max_cqe_version);
1000e126ba97SEli Cohen 
1001e126ba97SEli Cohen 	if (!dev->ib_active)
1002e126ba97SEli Cohen 		return ERR_PTR(-EAGAIN);
1003e126ba97SEli Cohen 
1004dfbee859SHaggai Abramovsky 	if (udata->inlen < sizeof(struct ib_uverbs_cmd_hdr))
1005dfbee859SHaggai Abramovsky 		return ERR_PTR(-EINVAL);
1006dfbee859SHaggai Abramovsky 
100778c0f98cSEli Cohen 	reqlen = udata->inlen - sizeof(struct ib_uverbs_cmd_hdr);
100878c0f98cSEli Cohen 	if (reqlen == sizeof(struct mlx5_ib_alloc_ucontext_req))
100978c0f98cSEli Cohen 		ver = 0;
1010a168a41cSMajd Dibbiny 	else if (reqlen >= min_req_v2)
101178c0f98cSEli Cohen 		ver = 2;
101278c0f98cSEli Cohen 	else
101378c0f98cSEli Cohen 		return ERR_PTR(-EINVAL);
101478c0f98cSEli Cohen 
1015b368d7cbSMatan Barak 	err = ib_copy_from_udata(&req, udata, min(reqlen, sizeof(req)));
1016e126ba97SEli Cohen 	if (err)
1017e126ba97SEli Cohen 		return ERR_PTR(err);
1018e126ba97SEli Cohen 
1019b368d7cbSMatan Barak 	if (req.flags)
102078c0f98cSEli Cohen 		return ERR_PTR(-EINVAL);
102178c0f98cSEli Cohen 
1022e126ba97SEli Cohen 	if (req.total_num_uuars > MLX5_MAX_UUARS)
1023e126ba97SEli Cohen 		return ERR_PTR(-ENOMEM);
1024e126ba97SEli Cohen 
1025e126ba97SEli Cohen 	if (req.total_num_uuars == 0)
1026e126ba97SEli Cohen 		return ERR_PTR(-EINVAL);
1027e126ba97SEli Cohen 
1028f72300c5SHaggai Abramovsky 	if (req.comp_mask || req.reserved0 || req.reserved1 || req.reserved2)
1029b368d7cbSMatan Barak 		return ERR_PTR(-EOPNOTSUPP);
1030b368d7cbSMatan Barak 
1031b368d7cbSMatan Barak 	if (reqlen > sizeof(req) &&
1032b368d7cbSMatan Barak 	    !ib_is_udata_cleared(udata, sizeof(req),
1033dfbee859SHaggai Abramovsky 				 reqlen - sizeof(req)))
1034b368d7cbSMatan Barak 		return ERR_PTR(-EOPNOTSUPP);
1035b368d7cbSMatan Barak 
1036c1be5232SEli Cohen 	req.total_num_uuars = ALIGN(req.total_num_uuars,
1037c1be5232SEli Cohen 				    MLX5_NON_FP_BF_REGS_PER_PAGE);
1038e126ba97SEli Cohen 	if (req.num_low_latency_uuars > req.total_num_uuars - 1)
1039e126ba97SEli Cohen 		return ERR_PTR(-EINVAL);
1040e126ba97SEli Cohen 
1041c1be5232SEli Cohen 	num_uars = req.total_num_uuars / MLX5_NON_FP_BF_REGS_PER_PAGE;
1042c1be5232SEli Cohen 	gross_uuars = num_uars * MLX5_BF_REGS_PER_PAGE;
1043938fe83cSSaeed Mahameed 	resp.qp_tab_size = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp);
10442cc6ad5fSNoa Osherovich 	if (mlx5_core_is_pf(dev->mdev) && MLX5_CAP_GEN(dev->mdev, bf))
1045938fe83cSSaeed Mahameed 		resp.bf_reg_size = 1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size);
1046e126ba97SEli Cohen 	resp.cache_line_size = L1_CACHE_BYTES;
1047938fe83cSSaeed Mahameed 	resp.max_sq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq);
1048938fe83cSSaeed Mahameed 	resp.max_rq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq);
1049938fe83cSSaeed Mahameed 	resp.max_send_wqebb = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1050938fe83cSSaeed Mahameed 	resp.max_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1051938fe83cSSaeed Mahameed 	resp.max_srq_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz);
1052f72300c5SHaggai Abramovsky 	resp.cqe_version = min_t(__u8,
1053f72300c5SHaggai Abramovsky 				 (__u8)MLX5_CAP_GEN(dev->mdev, cqe_version),
1054f72300c5SHaggai Abramovsky 				 req.max_cqe_version);
1055b368d7cbSMatan Barak 	resp.response_length = min(offsetof(typeof(resp), response_length) +
1056b368d7cbSMatan Barak 				   sizeof(resp.response_length), udata->outlen);
1057e126ba97SEli Cohen 
1058e126ba97SEli Cohen 	context = kzalloc(sizeof(*context), GFP_KERNEL);
1059e126ba97SEli Cohen 	if (!context)
1060e126ba97SEli Cohen 		return ERR_PTR(-ENOMEM);
1061e126ba97SEli Cohen 
1062e126ba97SEli Cohen 	uuari = &context->uuari;
1063e126ba97SEli Cohen 	mutex_init(&uuari->lock);
1064e126ba97SEli Cohen 	uars = kcalloc(num_uars, sizeof(*uars), GFP_KERNEL);
1065e126ba97SEli Cohen 	if (!uars) {
1066e126ba97SEli Cohen 		err = -ENOMEM;
1067e126ba97SEli Cohen 		goto out_ctx;
1068e126ba97SEli Cohen 	}
1069e126ba97SEli Cohen 
1070c1be5232SEli Cohen 	uuari->bitmap = kcalloc(BITS_TO_LONGS(gross_uuars),
1071e126ba97SEli Cohen 				sizeof(*uuari->bitmap),
1072e126ba97SEli Cohen 				GFP_KERNEL);
1073e126ba97SEli Cohen 	if (!uuari->bitmap) {
1074e126ba97SEli Cohen 		err = -ENOMEM;
1075e126ba97SEli Cohen 		goto out_uar_ctx;
1076e126ba97SEli Cohen 	}
1077e126ba97SEli Cohen 	/*
1078e126ba97SEli Cohen 	 * clear all fast path uuars
1079e126ba97SEli Cohen 	 */
1080c1be5232SEli Cohen 	for (i = 0; i < gross_uuars; i++) {
1081e126ba97SEli Cohen 		uuarn = i & 3;
1082e126ba97SEli Cohen 		if (uuarn == 2 || uuarn == 3)
1083e126ba97SEli Cohen 			set_bit(i, uuari->bitmap);
1084e126ba97SEli Cohen 	}
1085e126ba97SEli Cohen 
1086c1be5232SEli Cohen 	uuari->count = kcalloc(gross_uuars, sizeof(*uuari->count), GFP_KERNEL);
1087e126ba97SEli Cohen 	if (!uuari->count) {
1088e126ba97SEli Cohen 		err = -ENOMEM;
1089e126ba97SEli Cohen 		goto out_bitmap;
1090e126ba97SEli Cohen 	}
1091e126ba97SEli Cohen 
1092e126ba97SEli Cohen 	for (i = 0; i < num_uars; i++) {
10939603b61dSJack Morgenstein 		err = mlx5_cmd_alloc_uar(dev->mdev, &uars[i].index);
1094e126ba97SEli Cohen 		if (err)
1095e126ba97SEli Cohen 			goto out_count;
1096e126ba97SEli Cohen 	}
1097e126ba97SEli Cohen 
1098b4cfe447SHaggai Eran #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
1099b4cfe447SHaggai Eran 	context->ibucontext.invalidate_range = &mlx5_ib_invalidate_range;
1100b4cfe447SHaggai Eran #endif
1101b4cfe447SHaggai Eran 
1102146d2f1aSmajd@mellanox.com 	if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain)) {
1103146d2f1aSmajd@mellanox.com 		err = mlx5_core_alloc_transport_domain(dev->mdev,
1104146d2f1aSmajd@mellanox.com 						       &context->tdn);
1105146d2f1aSmajd@mellanox.com 		if (err)
1106146d2f1aSmajd@mellanox.com 			goto out_uars;
1107146d2f1aSmajd@mellanox.com 	}
1108146d2f1aSmajd@mellanox.com 
11097c2344c3SMaor Gottlieb 	INIT_LIST_HEAD(&context->vma_private_list);
1110e126ba97SEli Cohen 	INIT_LIST_HEAD(&context->db_page_list);
1111e126ba97SEli Cohen 	mutex_init(&context->db_page_mutex);
1112e126ba97SEli Cohen 
1113e126ba97SEli Cohen 	resp.tot_uuars = req.total_num_uuars;
1114938fe83cSSaeed Mahameed 	resp.num_ports = MLX5_CAP_GEN(dev->mdev, num_ports);
1115b368d7cbSMatan Barak 
1116f72300c5SHaggai Abramovsky 	if (field_avail(typeof(resp), cqe_version, udata->outlen))
1117f72300c5SHaggai Abramovsky 		resp.response_length += sizeof(resp.cqe_version);
1118b368d7cbSMatan Barak 
1119402ca536SBodong Wang 	if (field_avail(typeof(resp), cmds_supp_uhw, udata->outlen)) {
1120402ca536SBodong Wang 		resp.cmds_supp_uhw |= MLX5_USER_CMDS_SUPP_UHW_QUERY_DEVICE;
1121402ca536SBodong Wang 		resp.response_length += sizeof(resp.cmds_supp_uhw);
1122402ca536SBodong Wang 	}
1123402ca536SBodong Wang 
1124bc5c6eedSNoa Osherovich 	/*
1125bc5c6eedSNoa Osherovich 	 * We don't want to expose information from the PCI bar that is located
1126bc5c6eedSNoa Osherovich 	 * after 4096 bytes, so if the arch only supports larger pages, let's
1127bc5c6eedSNoa Osherovich 	 * pretend we don't support reading the HCA's core clock. This is also
1128bc5c6eedSNoa Osherovich 	 * forced by mmap function.
1129bc5c6eedSNoa Osherovich 	 */
1130bc5c6eedSNoa Osherovich 	if (PAGE_SIZE <= 4096 &&
1131bc5c6eedSNoa Osherovich 	    field_avail(typeof(resp), hca_core_clock_offset, udata->outlen)) {
1132b368d7cbSMatan Barak 		resp.comp_mask |=
1133b368d7cbSMatan Barak 			MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_CORE_CLOCK_OFFSET;
1134b368d7cbSMatan Barak 		resp.hca_core_clock_offset =
1135b368d7cbSMatan Barak 			offsetof(struct mlx5_init_seg, internal_timer_h) %
1136b368d7cbSMatan Barak 			PAGE_SIZE;
1137f72300c5SHaggai Abramovsky 		resp.response_length += sizeof(resp.hca_core_clock_offset) +
1138402ca536SBodong Wang 					sizeof(resp.reserved2);
1139b368d7cbSMatan Barak 	}
1140b368d7cbSMatan Barak 
1141b368d7cbSMatan Barak 	err = ib_copy_to_udata(udata, &resp, resp.response_length);
1142e126ba97SEli Cohen 	if (err)
1143146d2f1aSmajd@mellanox.com 		goto out_td;
1144e126ba97SEli Cohen 
114578c0f98cSEli Cohen 	uuari->ver = ver;
1146e126ba97SEli Cohen 	uuari->num_low_latency_uuars = req.num_low_latency_uuars;
1147e126ba97SEli Cohen 	uuari->uars = uars;
1148e126ba97SEli Cohen 	uuari->num_uars = num_uars;
1149f72300c5SHaggai Abramovsky 	context->cqe_version = resp.cqe_version;
1150f72300c5SHaggai Abramovsky 
1151e126ba97SEli Cohen 	return &context->ibucontext;
1152e126ba97SEli Cohen 
1153146d2f1aSmajd@mellanox.com out_td:
1154146d2f1aSmajd@mellanox.com 	if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1155146d2f1aSmajd@mellanox.com 		mlx5_core_dealloc_transport_domain(dev->mdev, context->tdn);
1156146d2f1aSmajd@mellanox.com 
1157e126ba97SEli Cohen out_uars:
1158e126ba97SEli Cohen 	for (i--; i >= 0; i--)
11599603b61dSJack Morgenstein 		mlx5_cmd_free_uar(dev->mdev, uars[i].index);
1160e126ba97SEli Cohen out_count:
1161e126ba97SEli Cohen 	kfree(uuari->count);
1162e126ba97SEli Cohen 
1163e126ba97SEli Cohen out_bitmap:
1164e126ba97SEli Cohen 	kfree(uuari->bitmap);
1165e126ba97SEli Cohen 
1166e126ba97SEli Cohen out_uar_ctx:
1167e126ba97SEli Cohen 	kfree(uars);
1168e126ba97SEli Cohen 
1169e126ba97SEli Cohen out_ctx:
1170e126ba97SEli Cohen 	kfree(context);
1171e126ba97SEli Cohen 	return ERR_PTR(err);
1172e126ba97SEli Cohen }
1173e126ba97SEli Cohen 
1174e126ba97SEli Cohen static int mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
1175e126ba97SEli Cohen {
1176e126ba97SEli Cohen 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1177e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
1178e126ba97SEli Cohen 	struct mlx5_uuar_info *uuari = &context->uuari;
1179e126ba97SEli Cohen 	int i;
1180e126ba97SEli Cohen 
1181146d2f1aSmajd@mellanox.com 	if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1182146d2f1aSmajd@mellanox.com 		mlx5_core_dealloc_transport_domain(dev->mdev, context->tdn);
1183146d2f1aSmajd@mellanox.com 
1184e126ba97SEli Cohen 	for (i = 0; i < uuari->num_uars; i++) {
11859603b61dSJack Morgenstein 		if (mlx5_cmd_free_uar(dev->mdev, uuari->uars[i].index))
1186e126ba97SEli Cohen 			mlx5_ib_warn(dev, "failed to free UAR 0x%x\n", uuari->uars[i].index);
1187e126ba97SEli Cohen 	}
1188e126ba97SEli Cohen 
1189e126ba97SEli Cohen 	kfree(uuari->count);
1190e126ba97SEli Cohen 	kfree(uuari->bitmap);
1191e126ba97SEli Cohen 	kfree(uuari->uars);
1192e126ba97SEli Cohen 	kfree(context);
1193e126ba97SEli Cohen 
1194e126ba97SEli Cohen 	return 0;
1195e126ba97SEli Cohen }
1196e126ba97SEli Cohen 
1197e126ba97SEli Cohen static phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev, int index)
1198e126ba97SEli Cohen {
11999603b61dSJack Morgenstein 	return (pci_resource_start(dev->mdev->pdev, 0) >> PAGE_SHIFT) + index;
1200e126ba97SEli Cohen }
1201e126ba97SEli Cohen 
1202e126ba97SEli Cohen static int get_command(unsigned long offset)
1203e126ba97SEli Cohen {
1204e126ba97SEli Cohen 	return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK;
1205e126ba97SEli Cohen }
1206e126ba97SEli Cohen 
1207e126ba97SEli Cohen static int get_arg(unsigned long offset)
1208e126ba97SEli Cohen {
1209e126ba97SEli Cohen 	return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1);
1210e126ba97SEli Cohen }
1211e126ba97SEli Cohen 
1212e126ba97SEli Cohen static int get_index(unsigned long offset)
1213e126ba97SEli Cohen {
1214e126ba97SEli Cohen 	return get_arg(offset);
1215e126ba97SEli Cohen }
1216e126ba97SEli Cohen 
12177c2344c3SMaor Gottlieb static void  mlx5_ib_vma_open(struct vm_area_struct *area)
12187c2344c3SMaor Gottlieb {
12197c2344c3SMaor Gottlieb 	/* vma_open is called when a new VMA is created on top of our VMA.  This
12207c2344c3SMaor Gottlieb 	 * is done through either mremap flow or split_vma (usually due to
12217c2344c3SMaor Gottlieb 	 * mlock, madvise, munmap, etc.) We do not support a clone of the VMA,
12227c2344c3SMaor Gottlieb 	 * as this VMA is strongly hardware related.  Therefore we set the
12237c2344c3SMaor Gottlieb 	 * vm_ops of the newly created/cloned VMA to NULL, to prevent it from
12247c2344c3SMaor Gottlieb 	 * calling us again and trying to do incorrect actions.  We assume that
12257c2344c3SMaor Gottlieb 	 * the original VMA size is exactly a single page, and therefore all
12267c2344c3SMaor Gottlieb 	 * "splitting" operation will not happen to it.
12277c2344c3SMaor Gottlieb 	 */
12287c2344c3SMaor Gottlieb 	area->vm_ops = NULL;
12297c2344c3SMaor Gottlieb }
12307c2344c3SMaor Gottlieb 
12317c2344c3SMaor Gottlieb static void  mlx5_ib_vma_close(struct vm_area_struct *area)
12327c2344c3SMaor Gottlieb {
12337c2344c3SMaor Gottlieb 	struct mlx5_ib_vma_private_data *mlx5_ib_vma_priv_data;
12347c2344c3SMaor Gottlieb 
12357c2344c3SMaor Gottlieb 	/* It's guaranteed that all VMAs opened on a FD are closed before the
12367c2344c3SMaor Gottlieb 	 * file itself is closed, therefore no sync is needed with the regular
12377c2344c3SMaor Gottlieb 	 * closing flow. (e.g. mlx5 ib_dealloc_ucontext)
12387c2344c3SMaor Gottlieb 	 * However need a sync with accessing the vma as part of
12397c2344c3SMaor Gottlieb 	 * mlx5_ib_disassociate_ucontext.
12407c2344c3SMaor Gottlieb 	 * The close operation is usually called under mm->mmap_sem except when
12417c2344c3SMaor Gottlieb 	 * process is exiting.
12427c2344c3SMaor Gottlieb 	 * The exiting case is handled explicitly as part of
12437c2344c3SMaor Gottlieb 	 * mlx5_ib_disassociate_ucontext.
12447c2344c3SMaor Gottlieb 	 */
12457c2344c3SMaor Gottlieb 	mlx5_ib_vma_priv_data = (struct mlx5_ib_vma_private_data *)area->vm_private_data;
12467c2344c3SMaor Gottlieb 
12477c2344c3SMaor Gottlieb 	/* setting the vma context pointer to null in the mlx5_ib driver's
12487c2344c3SMaor Gottlieb 	 * private data, to protect a race condition in
12497c2344c3SMaor Gottlieb 	 * mlx5_ib_disassociate_ucontext().
12507c2344c3SMaor Gottlieb 	 */
12517c2344c3SMaor Gottlieb 	mlx5_ib_vma_priv_data->vma = NULL;
12527c2344c3SMaor Gottlieb 	list_del(&mlx5_ib_vma_priv_data->list);
12537c2344c3SMaor Gottlieb 	kfree(mlx5_ib_vma_priv_data);
12547c2344c3SMaor Gottlieb }
12557c2344c3SMaor Gottlieb 
12567c2344c3SMaor Gottlieb static const struct vm_operations_struct mlx5_ib_vm_ops = {
12577c2344c3SMaor Gottlieb 	.open = mlx5_ib_vma_open,
12587c2344c3SMaor Gottlieb 	.close = mlx5_ib_vma_close
12597c2344c3SMaor Gottlieb };
12607c2344c3SMaor Gottlieb 
12617c2344c3SMaor Gottlieb static int mlx5_ib_set_vma_data(struct vm_area_struct *vma,
12627c2344c3SMaor Gottlieb 				struct mlx5_ib_ucontext *ctx)
12637c2344c3SMaor Gottlieb {
12647c2344c3SMaor Gottlieb 	struct mlx5_ib_vma_private_data *vma_prv;
12657c2344c3SMaor Gottlieb 	struct list_head *vma_head = &ctx->vma_private_list;
12667c2344c3SMaor Gottlieb 
12677c2344c3SMaor Gottlieb 	vma_prv = kzalloc(sizeof(*vma_prv), GFP_KERNEL);
12687c2344c3SMaor Gottlieb 	if (!vma_prv)
12697c2344c3SMaor Gottlieb 		return -ENOMEM;
12707c2344c3SMaor Gottlieb 
12717c2344c3SMaor Gottlieb 	vma_prv->vma = vma;
12727c2344c3SMaor Gottlieb 	vma->vm_private_data = vma_prv;
12737c2344c3SMaor Gottlieb 	vma->vm_ops =  &mlx5_ib_vm_ops;
12747c2344c3SMaor Gottlieb 
12757c2344c3SMaor Gottlieb 	list_add(&vma_prv->list, vma_head);
12767c2344c3SMaor Gottlieb 
12777c2344c3SMaor Gottlieb 	return 0;
12787c2344c3SMaor Gottlieb }
12797c2344c3SMaor Gottlieb 
12807c2344c3SMaor Gottlieb static void mlx5_ib_disassociate_ucontext(struct ib_ucontext *ibcontext)
12817c2344c3SMaor Gottlieb {
12827c2344c3SMaor Gottlieb 	int ret;
12837c2344c3SMaor Gottlieb 	struct vm_area_struct *vma;
12847c2344c3SMaor Gottlieb 	struct mlx5_ib_vma_private_data *vma_private, *n;
12857c2344c3SMaor Gottlieb 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
12867c2344c3SMaor Gottlieb 	struct task_struct *owning_process  = NULL;
12877c2344c3SMaor Gottlieb 	struct mm_struct   *owning_mm       = NULL;
12887c2344c3SMaor Gottlieb 
12897c2344c3SMaor Gottlieb 	owning_process = get_pid_task(ibcontext->tgid, PIDTYPE_PID);
12907c2344c3SMaor Gottlieb 	if (!owning_process)
12917c2344c3SMaor Gottlieb 		return;
12927c2344c3SMaor Gottlieb 
12937c2344c3SMaor Gottlieb 	owning_mm = get_task_mm(owning_process);
12947c2344c3SMaor Gottlieb 	if (!owning_mm) {
12957c2344c3SMaor Gottlieb 		pr_info("no mm, disassociate ucontext is pending task termination\n");
12967c2344c3SMaor Gottlieb 		while (1) {
12977c2344c3SMaor Gottlieb 			put_task_struct(owning_process);
12987c2344c3SMaor Gottlieb 			usleep_range(1000, 2000);
12997c2344c3SMaor Gottlieb 			owning_process = get_pid_task(ibcontext->tgid,
13007c2344c3SMaor Gottlieb 						      PIDTYPE_PID);
13017c2344c3SMaor Gottlieb 			if (!owning_process ||
13027c2344c3SMaor Gottlieb 			    owning_process->state == TASK_DEAD) {
13037c2344c3SMaor Gottlieb 				pr_info("disassociate ucontext done, task was terminated\n");
13047c2344c3SMaor Gottlieb 				/* in case task was dead need to release the
13057c2344c3SMaor Gottlieb 				 * task struct.
13067c2344c3SMaor Gottlieb 				 */
13077c2344c3SMaor Gottlieb 				if (owning_process)
13087c2344c3SMaor Gottlieb 					put_task_struct(owning_process);
13097c2344c3SMaor Gottlieb 				return;
13107c2344c3SMaor Gottlieb 			}
13117c2344c3SMaor Gottlieb 		}
13127c2344c3SMaor Gottlieb 	}
13137c2344c3SMaor Gottlieb 
13147c2344c3SMaor Gottlieb 	/* need to protect from a race on closing the vma as part of
13157c2344c3SMaor Gottlieb 	 * mlx5_ib_vma_close.
13167c2344c3SMaor Gottlieb 	 */
13177c2344c3SMaor Gottlieb 	down_read(&owning_mm->mmap_sem);
13187c2344c3SMaor Gottlieb 	list_for_each_entry_safe(vma_private, n, &context->vma_private_list,
13197c2344c3SMaor Gottlieb 				 list) {
13207c2344c3SMaor Gottlieb 		vma = vma_private->vma;
13217c2344c3SMaor Gottlieb 		ret = zap_vma_ptes(vma, vma->vm_start,
13227c2344c3SMaor Gottlieb 				   PAGE_SIZE);
13237c2344c3SMaor Gottlieb 		WARN_ONCE(ret, "%s: zap_vma_ptes failed", __func__);
13247c2344c3SMaor Gottlieb 		/* context going to be destroyed, should
13257c2344c3SMaor Gottlieb 		 * not access ops any more.
13267c2344c3SMaor Gottlieb 		 */
13277c2344c3SMaor Gottlieb 		vma->vm_ops = NULL;
13287c2344c3SMaor Gottlieb 		list_del(&vma_private->list);
13297c2344c3SMaor Gottlieb 		kfree(vma_private);
13307c2344c3SMaor Gottlieb 	}
13317c2344c3SMaor Gottlieb 	up_read(&owning_mm->mmap_sem);
13327c2344c3SMaor Gottlieb 	mmput(owning_mm);
13337c2344c3SMaor Gottlieb 	put_task_struct(owning_process);
13347c2344c3SMaor Gottlieb }
13357c2344c3SMaor Gottlieb 
133637aa5c36SGuy Levi static inline char *mmap_cmd2str(enum mlx5_ib_mmap_cmd cmd)
1337e126ba97SEli Cohen {
133837aa5c36SGuy Levi 	switch (cmd) {
133937aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
134037aa5c36SGuy Levi 		return "WC";
1341e126ba97SEli Cohen 	case MLX5_IB_MMAP_REGULAR_PAGE:
134237aa5c36SGuy Levi 		return "best effort WC";
134337aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
134437aa5c36SGuy Levi 		return "NC";
134537aa5c36SGuy Levi 	default:
134637aa5c36SGuy Levi 		return NULL;
134737aa5c36SGuy Levi 	}
134837aa5c36SGuy Levi }
134937aa5c36SGuy Levi 
135037aa5c36SGuy Levi static int uar_mmap(struct mlx5_ib_dev *dev, enum mlx5_ib_mmap_cmd cmd,
13517c2344c3SMaor Gottlieb 		    struct vm_area_struct *vma,
13527c2344c3SMaor Gottlieb 		    struct mlx5_ib_ucontext *context)
135337aa5c36SGuy Levi {
13547c2344c3SMaor Gottlieb 	struct mlx5_uuar_info *uuari = &context->uuari;
135537aa5c36SGuy Levi 	int err;
135637aa5c36SGuy Levi 	unsigned long idx;
135737aa5c36SGuy Levi 	phys_addr_t pfn, pa;
135837aa5c36SGuy Levi 	pgprot_t prot;
135937aa5c36SGuy Levi 
136037aa5c36SGuy Levi 	switch (cmd) {
136137aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
136237aa5c36SGuy Levi /* Some architectures don't support WC memory */
136337aa5c36SGuy Levi #if defined(CONFIG_X86)
136437aa5c36SGuy Levi 		if (!pat_enabled())
136537aa5c36SGuy Levi 			return -EPERM;
136637aa5c36SGuy Levi #elif !(defined(CONFIG_PPC) || (defined(CONFIG_ARM) && defined(CONFIG_MMU)))
136737aa5c36SGuy Levi 			return -EPERM;
136837aa5c36SGuy Levi #endif
136937aa5c36SGuy Levi 	/* fall through */
137037aa5c36SGuy Levi 	case MLX5_IB_MMAP_REGULAR_PAGE:
137137aa5c36SGuy Levi 		/* For MLX5_IB_MMAP_REGULAR_PAGE do the best effort to get WC */
137237aa5c36SGuy Levi 		prot = pgprot_writecombine(vma->vm_page_prot);
137337aa5c36SGuy Levi 		break;
137437aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
137537aa5c36SGuy Levi 		prot = pgprot_noncached(vma->vm_page_prot);
137637aa5c36SGuy Levi 		break;
137737aa5c36SGuy Levi 	default:
137837aa5c36SGuy Levi 		return -EINVAL;
137937aa5c36SGuy Levi 	}
138037aa5c36SGuy Levi 
1381e126ba97SEli Cohen 	if (vma->vm_end - vma->vm_start != PAGE_SIZE)
1382e126ba97SEli Cohen 		return -EINVAL;
1383e126ba97SEli Cohen 
1384e126ba97SEli Cohen 	idx = get_index(vma->vm_pgoff);
13851c3ce90dSEli Cohen 	if (idx >= uuari->num_uars)
13861c3ce90dSEli Cohen 		return -EINVAL;
13871c3ce90dSEli Cohen 
1388e126ba97SEli Cohen 	pfn = uar_index2pfn(dev, uuari->uars[idx].index);
138937aa5c36SGuy Levi 	mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn %pa\n", idx, &pfn);
1390e126ba97SEli Cohen 
139137aa5c36SGuy Levi 	vma->vm_page_prot = prot;
139237aa5c36SGuy Levi 	err = io_remap_pfn_range(vma, vma->vm_start, pfn,
139337aa5c36SGuy Levi 				 PAGE_SIZE, vma->vm_page_prot);
139437aa5c36SGuy Levi 	if (err) {
139537aa5c36SGuy Levi 		mlx5_ib_err(dev, "io_remap_pfn_range failed with error=%d, vm_start=0x%lx, pfn=%pa, mmap_cmd=%s\n",
139637aa5c36SGuy Levi 			    err, vma->vm_start, &pfn, mmap_cmd2str(cmd));
1397e126ba97SEli Cohen 		return -EAGAIN;
139837aa5c36SGuy Levi 	}
1399e126ba97SEli Cohen 
140037aa5c36SGuy Levi 	pa = pfn << PAGE_SHIFT;
140137aa5c36SGuy Levi 	mlx5_ib_dbg(dev, "mapped %s at 0x%lx, PA %pa\n", mmap_cmd2str(cmd),
140237aa5c36SGuy Levi 		    vma->vm_start, &pa);
140337aa5c36SGuy Levi 
14047c2344c3SMaor Gottlieb 	return mlx5_ib_set_vma_data(vma, context);
140537aa5c36SGuy Levi }
140637aa5c36SGuy Levi 
140737aa5c36SGuy Levi static int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma)
140837aa5c36SGuy Levi {
140937aa5c36SGuy Levi 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
141037aa5c36SGuy Levi 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
141137aa5c36SGuy Levi 	unsigned long command;
141237aa5c36SGuy Levi 	phys_addr_t pfn;
141337aa5c36SGuy Levi 
141437aa5c36SGuy Levi 	command = get_command(vma->vm_pgoff);
141537aa5c36SGuy Levi 	switch (command) {
141637aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
141737aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
141837aa5c36SGuy Levi 	case MLX5_IB_MMAP_REGULAR_PAGE:
14197c2344c3SMaor Gottlieb 		return uar_mmap(dev, command, vma, context);
1420e126ba97SEli Cohen 
1421e126ba97SEli Cohen 	case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES:
1422e126ba97SEli Cohen 		return -ENOSYS;
1423e126ba97SEli Cohen 
1424d69e3bcfSMatan Barak 	case MLX5_IB_MMAP_CORE_CLOCK:
1425d69e3bcfSMatan Barak 		if (vma->vm_end - vma->vm_start != PAGE_SIZE)
1426d69e3bcfSMatan Barak 			return -EINVAL;
1427d69e3bcfSMatan Barak 
14286cbac1e4SMatan Barak 		if (vma->vm_flags & VM_WRITE)
1429d69e3bcfSMatan Barak 			return -EPERM;
1430d69e3bcfSMatan Barak 
1431d69e3bcfSMatan Barak 		/* Don't expose to user-space information it shouldn't have */
1432d69e3bcfSMatan Barak 		if (PAGE_SIZE > 4096)
1433d69e3bcfSMatan Barak 			return -EOPNOTSUPP;
1434d69e3bcfSMatan Barak 
1435d69e3bcfSMatan Barak 		vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1436d69e3bcfSMatan Barak 		pfn = (dev->mdev->iseg_base +
1437d69e3bcfSMatan Barak 		       offsetof(struct mlx5_init_seg, internal_timer_h)) >>
1438d69e3bcfSMatan Barak 			PAGE_SHIFT;
1439d69e3bcfSMatan Barak 		if (io_remap_pfn_range(vma, vma->vm_start, pfn,
1440d69e3bcfSMatan Barak 				       PAGE_SIZE, vma->vm_page_prot))
1441d69e3bcfSMatan Barak 			return -EAGAIN;
1442d69e3bcfSMatan Barak 
1443d69e3bcfSMatan Barak 		mlx5_ib_dbg(dev, "mapped internal timer at 0x%lx, PA 0x%llx\n",
1444d69e3bcfSMatan Barak 			    vma->vm_start,
1445d69e3bcfSMatan Barak 			    (unsigned long long)pfn << PAGE_SHIFT);
1446d69e3bcfSMatan Barak 		break;
1447d69e3bcfSMatan Barak 
1448e126ba97SEli Cohen 	default:
1449e126ba97SEli Cohen 		return -EINVAL;
1450e126ba97SEli Cohen 	}
1451e126ba97SEli Cohen 
1452e126ba97SEli Cohen 	return 0;
1453e126ba97SEli Cohen }
1454e126ba97SEli Cohen 
1455e126ba97SEli Cohen static struct ib_pd *mlx5_ib_alloc_pd(struct ib_device *ibdev,
1456e126ba97SEli Cohen 				      struct ib_ucontext *context,
1457e126ba97SEli Cohen 				      struct ib_udata *udata)
1458e126ba97SEli Cohen {
1459e126ba97SEli Cohen 	struct mlx5_ib_alloc_pd_resp resp;
1460e126ba97SEli Cohen 	struct mlx5_ib_pd *pd;
1461e126ba97SEli Cohen 	int err;
1462e126ba97SEli Cohen 
1463e126ba97SEli Cohen 	pd = kmalloc(sizeof(*pd), GFP_KERNEL);
1464e126ba97SEli Cohen 	if (!pd)
1465e126ba97SEli Cohen 		return ERR_PTR(-ENOMEM);
1466e126ba97SEli Cohen 
14679603b61dSJack Morgenstein 	err = mlx5_core_alloc_pd(to_mdev(ibdev)->mdev, &pd->pdn);
1468e126ba97SEli Cohen 	if (err) {
1469e126ba97SEli Cohen 		kfree(pd);
1470e126ba97SEli Cohen 		return ERR_PTR(err);
1471e126ba97SEli Cohen 	}
1472e126ba97SEli Cohen 
1473e126ba97SEli Cohen 	if (context) {
1474e126ba97SEli Cohen 		resp.pdn = pd->pdn;
1475e126ba97SEli Cohen 		if (ib_copy_to_udata(udata, &resp, sizeof(resp))) {
14769603b61dSJack Morgenstein 			mlx5_core_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn);
1477e126ba97SEli Cohen 			kfree(pd);
1478e126ba97SEli Cohen 			return ERR_PTR(-EFAULT);
1479e126ba97SEli Cohen 		}
1480e126ba97SEli Cohen 	}
1481e126ba97SEli Cohen 
1482e126ba97SEli Cohen 	return &pd->ibpd;
1483e126ba97SEli Cohen }
1484e126ba97SEli Cohen 
1485e126ba97SEli Cohen static int mlx5_ib_dealloc_pd(struct ib_pd *pd)
1486e126ba97SEli Cohen {
1487e126ba97SEli Cohen 	struct mlx5_ib_dev *mdev = to_mdev(pd->device);
1488e126ba97SEli Cohen 	struct mlx5_ib_pd *mpd = to_mpd(pd);
1489e126ba97SEli Cohen 
14909603b61dSJack Morgenstein 	mlx5_core_dealloc_pd(mdev->mdev, mpd->pdn);
1491e126ba97SEli Cohen 	kfree(mpd);
1492e126ba97SEli Cohen 
1493e126ba97SEli Cohen 	return 0;
1494e126ba97SEli Cohen }
1495e126ba97SEli Cohen 
1496466fa6d2SMaor Gottlieb enum {
1497466fa6d2SMaor Gottlieb 	MATCH_CRITERIA_ENABLE_OUTER_BIT,
1498466fa6d2SMaor Gottlieb 	MATCH_CRITERIA_ENABLE_MISC_BIT,
1499466fa6d2SMaor Gottlieb 	MATCH_CRITERIA_ENABLE_INNER_BIT
1500466fa6d2SMaor Gottlieb };
1501038d2ef8SMaor Gottlieb 
1502466fa6d2SMaor Gottlieb #define HEADER_IS_ZERO(match_criteria, headers)			           \
1503466fa6d2SMaor Gottlieb 	!(memchr_inv(MLX5_ADDR_OF(fte_match_param, match_criteria, headers), \
1504466fa6d2SMaor Gottlieb 		    0, MLX5_FLD_SZ_BYTES(fte_match_param, headers)))       \
1505466fa6d2SMaor Gottlieb 
1506466fa6d2SMaor Gottlieb static u8 get_match_criteria_enable(u32 *match_criteria)
1507466fa6d2SMaor Gottlieb {
1508466fa6d2SMaor Gottlieb 	u8 match_criteria_enable;
1509466fa6d2SMaor Gottlieb 
1510466fa6d2SMaor Gottlieb 	match_criteria_enable =
1511466fa6d2SMaor Gottlieb 		(!HEADER_IS_ZERO(match_criteria, outer_headers)) <<
1512466fa6d2SMaor Gottlieb 		MATCH_CRITERIA_ENABLE_OUTER_BIT;
1513466fa6d2SMaor Gottlieb 	match_criteria_enable |=
1514466fa6d2SMaor Gottlieb 		(!HEADER_IS_ZERO(match_criteria, misc_parameters)) <<
1515466fa6d2SMaor Gottlieb 		MATCH_CRITERIA_ENABLE_MISC_BIT;
1516466fa6d2SMaor Gottlieb 	match_criteria_enable |=
1517466fa6d2SMaor Gottlieb 		(!HEADER_IS_ZERO(match_criteria, inner_headers)) <<
1518466fa6d2SMaor Gottlieb 		MATCH_CRITERIA_ENABLE_INNER_BIT;
1519466fa6d2SMaor Gottlieb 
1520466fa6d2SMaor Gottlieb 	return match_criteria_enable;
1521038d2ef8SMaor Gottlieb }
1522038d2ef8SMaor Gottlieb 
1523ca0d4753SMaor Gottlieb static void set_proto(void *outer_c, void *outer_v, u8 mask, u8 val)
1524ca0d4753SMaor Gottlieb {
1525ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_protocol, mask);
1526ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_protocol, val);
1527ca0d4753SMaor Gottlieb }
1528ca0d4753SMaor Gottlieb 
1529ca0d4753SMaor Gottlieb static void set_tos(void *outer_c, void *outer_v, u8 mask, u8 val)
1530ca0d4753SMaor Gottlieb {
1531ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_ecn, mask);
1532ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_ecn, val);
1533ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_dscp, mask >> 2);
1534ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_dscp, val >> 2);
1535ca0d4753SMaor Gottlieb }
1536ca0d4753SMaor Gottlieb 
1537c47ac6aeSMaor Gottlieb #define LAST_ETH_FIELD vlan_tag
1538c47ac6aeSMaor Gottlieb #define LAST_IB_FIELD sl
1539ca0d4753SMaor Gottlieb #define LAST_IPV4_FIELD tos
1540466fa6d2SMaor Gottlieb #define LAST_IPV6_FIELD traffic_class
1541c47ac6aeSMaor Gottlieb #define LAST_TCP_UDP_FIELD src_port
1542c47ac6aeSMaor Gottlieb 
1543c47ac6aeSMaor Gottlieb /* Field is the last supported field */
1544c47ac6aeSMaor Gottlieb #define FIELDS_NOT_SUPPORTED(filter, field)\
1545c47ac6aeSMaor Gottlieb 	memchr_inv((void *)&filter.field  +\
1546c47ac6aeSMaor Gottlieb 		   sizeof(filter.field), 0,\
1547c47ac6aeSMaor Gottlieb 		   sizeof(filter) -\
1548c47ac6aeSMaor Gottlieb 		   offsetof(typeof(filter), field) -\
1549c47ac6aeSMaor Gottlieb 		   sizeof(filter.field))
1550c47ac6aeSMaor Gottlieb 
1551038d2ef8SMaor Gottlieb static int parse_flow_attr(u32 *match_c, u32 *match_v,
1552dd063d0eSMaor Gottlieb 			   const union ib_flow_spec *ib_spec)
1553038d2ef8SMaor Gottlieb {
1554038d2ef8SMaor Gottlieb 	void *outer_headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
1555038d2ef8SMaor Gottlieb 					     outer_headers);
1556038d2ef8SMaor Gottlieb 	void *outer_headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
1557038d2ef8SMaor Gottlieb 					     outer_headers);
1558466fa6d2SMaor Gottlieb 	void *misc_params_c = MLX5_ADDR_OF(fte_match_param, match_c,
1559466fa6d2SMaor Gottlieb 					   misc_parameters);
1560466fa6d2SMaor Gottlieb 	void *misc_params_v = MLX5_ADDR_OF(fte_match_param, match_v,
1561466fa6d2SMaor Gottlieb 					   misc_parameters);
1562466fa6d2SMaor Gottlieb 
1563038d2ef8SMaor Gottlieb 	switch (ib_spec->type) {
1564038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_ETH:
1565c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->eth.mask, LAST_ETH_FIELD))
1566c47ac6aeSMaor Gottlieb 			return -ENOTSUPP;
1567038d2ef8SMaor Gottlieb 
1568038d2ef8SMaor Gottlieb 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_c,
1569038d2ef8SMaor Gottlieb 					     dmac_47_16),
1570038d2ef8SMaor Gottlieb 				ib_spec->eth.mask.dst_mac);
1571038d2ef8SMaor Gottlieb 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_v,
1572038d2ef8SMaor Gottlieb 					     dmac_47_16),
1573038d2ef8SMaor Gottlieb 				ib_spec->eth.val.dst_mac);
1574038d2ef8SMaor Gottlieb 
1575ee3da804SMaor Gottlieb 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_c,
1576ee3da804SMaor Gottlieb 					     smac_47_16),
1577ee3da804SMaor Gottlieb 				ib_spec->eth.mask.src_mac);
1578ee3da804SMaor Gottlieb 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_v,
1579ee3da804SMaor Gottlieb 					     smac_47_16),
1580ee3da804SMaor Gottlieb 				ib_spec->eth.val.src_mac);
1581ee3da804SMaor Gottlieb 
1582038d2ef8SMaor Gottlieb 		if (ib_spec->eth.mask.vlan_tag) {
1583038d2ef8SMaor Gottlieb 			MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c,
1584038d2ef8SMaor Gottlieb 				 vlan_tag, 1);
1585038d2ef8SMaor Gottlieb 			MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v,
1586038d2ef8SMaor Gottlieb 				 vlan_tag, 1);
1587038d2ef8SMaor Gottlieb 
1588038d2ef8SMaor Gottlieb 			MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c,
1589038d2ef8SMaor Gottlieb 				 first_vid, ntohs(ib_spec->eth.mask.vlan_tag));
1590038d2ef8SMaor Gottlieb 			MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v,
1591038d2ef8SMaor Gottlieb 				 first_vid, ntohs(ib_spec->eth.val.vlan_tag));
1592038d2ef8SMaor Gottlieb 
1593038d2ef8SMaor Gottlieb 			MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c,
1594038d2ef8SMaor Gottlieb 				 first_cfi,
1595038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.mask.vlan_tag) >> 12);
1596038d2ef8SMaor Gottlieb 			MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v,
1597038d2ef8SMaor Gottlieb 				 first_cfi,
1598038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.val.vlan_tag) >> 12);
1599038d2ef8SMaor Gottlieb 
1600038d2ef8SMaor Gottlieb 			MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c,
1601038d2ef8SMaor Gottlieb 				 first_prio,
1602038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.mask.vlan_tag) >> 13);
1603038d2ef8SMaor Gottlieb 			MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v,
1604038d2ef8SMaor Gottlieb 				 first_prio,
1605038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.val.vlan_tag) >> 13);
1606038d2ef8SMaor Gottlieb 		}
1607038d2ef8SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c,
1608038d2ef8SMaor Gottlieb 			 ethertype, ntohs(ib_spec->eth.mask.ether_type));
1609038d2ef8SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v,
1610038d2ef8SMaor Gottlieb 			 ethertype, ntohs(ib_spec->eth.val.ether_type));
1611038d2ef8SMaor Gottlieb 		break;
1612038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_IPV4:
1613c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->ipv4.mask, LAST_IPV4_FIELD))
1614c47ac6aeSMaor Gottlieb 			return -ENOTSUPP;
1615038d2ef8SMaor Gottlieb 
1616038d2ef8SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c,
1617038d2ef8SMaor Gottlieb 			 ethertype, 0xffff);
1618038d2ef8SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v,
1619038d2ef8SMaor Gottlieb 			 ethertype, ETH_P_IP);
1620038d2ef8SMaor Gottlieb 
1621038d2ef8SMaor Gottlieb 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_c,
1622038d2ef8SMaor Gottlieb 				    src_ipv4_src_ipv6.ipv4_layout.ipv4),
1623038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.mask.src_ip,
1624038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.mask.src_ip));
1625038d2ef8SMaor Gottlieb 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_v,
1626038d2ef8SMaor Gottlieb 				    src_ipv4_src_ipv6.ipv4_layout.ipv4),
1627038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.val.src_ip,
1628038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.val.src_ip));
1629038d2ef8SMaor Gottlieb 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_c,
1630038d2ef8SMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
1631038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.mask.dst_ip,
1632038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.mask.dst_ip));
1633038d2ef8SMaor Gottlieb 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_v,
1634038d2ef8SMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
1635038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.val.dst_ip,
1636038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.val.dst_ip));
1637ca0d4753SMaor Gottlieb 
1638ca0d4753SMaor Gottlieb 		set_tos(outer_headers_c, outer_headers_v,
1639ca0d4753SMaor Gottlieb 			ib_spec->ipv4.mask.tos, ib_spec->ipv4.val.tos);
1640ca0d4753SMaor Gottlieb 
1641ca0d4753SMaor Gottlieb 		set_proto(outer_headers_c, outer_headers_v,
1642ca0d4753SMaor Gottlieb 			  ib_spec->ipv4.mask.proto, ib_spec->ipv4.val.proto);
1643038d2ef8SMaor Gottlieb 		break;
1644026bae0cSMaor Gottlieb 	case IB_FLOW_SPEC_IPV6:
1645c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->ipv6.mask, LAST_IPV6_FIELD))
1646c47ac6aeSMaor Gottlieb 			return -ENOTSUPP;
1647026bae0cSMaor Gottlieb 
1648026bae0cSMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c,
1649026bae0cSMaor Gottlieb 			 ethertype, 0xffff);
1650026bae0cSMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v,
1651026bae0cSMaor Gottlieb 			 ethertype, ETH_P_IPV6);
1652026bae0cSMaor Gottlieb 
1653026bae0cSMaor Gottlieb 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_c,
1654026bae0cSMaor Gottlieb 				    src_ipv4_src_ipv6.ipv6_layout.ipv6),
1655026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.mask.src_ip,
1656026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.mask.src_ip));
1657026bae0cSMaor Gottlieb 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_v,
1658026bae0cSMaor Gottlieb 				    src_ipv4_src_ipv6.ipv6_layout.ipv6),
1659026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.val.src_ip,
1660026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.val.src_ip));
1661026bae0cSMaor Gottlieb 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_c,
1662026bae0cSMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
1663026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.mask.dst_ip,
1664026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.mask.dst_ip));
1665026bae0cSMaor Gottlieb 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_v,
1666026bae0cSMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
1667026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.val.dst_ip,
1668026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.val.dst_ip));
1669466fa6d2SMaor Gottlieb 
1670466fa6d2SMaor Gottlieb 		set_tos(outer_headers_c, outer_headers_v,
1671466fa6d2SMaor Gottlieb 			ib_spec->ipv6.mask.traffic_class,
1672466fa6d2SMaor Gottlieb 			ib_spec->ipv6.val.traffic_class);
1673466fa6d2SMaor Gottlieb 
1674466fa6d2SMaor Gottlieb 		set_proto(outer_headers_c, outer_headers_v,
1675466fa6d2SMaor Gottlieb 			  ib_spec->ipv6.mask.next_hdr,
1676466fa6d2SMaor Gottlieb 			  ib_spec->ipv6.val.next_hdr);
1677466fa6d2SMaor Gottlieb 
1678466fa6d2SMaor Gottlieb 		MLX5_SET(fte_match_set_misc, misc_params_c,
1679466fa6d2SMaor Gottlieb 			 outer_ipv6_flow_label,
1680466fa6d2SMaor Gottlieb 			 ntohl(ib_spec->ipv6.mask.flow_label));
1681466fa6d2SMaor Gottlieb 		MLX5_SET(fte_match_set_misc, misc_params_v,
1682466fa6d2SMaor Gottlieb 			 outer_ipv6_flow_label,
1683466fa6d2SMaor Gottlieb 			 ntohl(ib_spec->ipv6.val.flow_label));
1684026bae0cSMaor Gottlieb 		break;
1685038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_TCP:
1686c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
1687c47ac6aeSMaor Gottlieb 					 LAST_TCP_UDP_FIELD))
1688c47ac6aeSMaor Gottlieb 			return -ENOTSUPP;
1689038d2ef8SMaor Gottlieb 
1690038d2ef8SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, ip_protocol,
1691038d2ef8SMaor Gottlieb 			 0xff);
1692038d2ef8SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, ip_protocol,
1693038d2ef8SMaor Gottlieb 			 IPPROTO_TCP);
1694038d2ef8SMaor Gottlieb 
1695038d2ef8SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, tcp_sport,
1696038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.src_port));
1697038d2ef8SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, tcp_sport,
1698038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.src_port));
1699038d2ef8SMaor Gottlieb 
1700038d2ef8SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, tcp_dport,
1701038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.dst_port));
1702038d2ef8SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, tcp_dport,
1703038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.dst_port));
1704038d2ef8SMaor Gottlieb 		break;
1705038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_UDP:
1706c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
1707c47ac6aeSMaor Gottlieb 					 LAST_TCP_UDP_FIELD))
1708c47ac6aeSMaor Gottlieb 			return -ENOTSUPP;
1709038d2ef8SMaor Gottlieb 
1710038d2ef8SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, ip_protocol,
1711038d2ef8SMaor Gottlieb 			 0xff);
1712038d2ef8SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, ip_protocol,
1713038d2ef8SMaor Gottlieb 			 IPPROTO_UDP);
1714038d2ef8SMaor Gottlieb 
1715038d2ef8SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, udp_sport,
1716038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.src_port));
1717038d2ef8SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, udp_sport,
1718038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.src_port));
1719038d2ef8SMaor Gottlieb 
1720038d2ef8SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, udp_dport,
1721038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.dst_port));
1722038d2ef8SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, udp_dport,
1723038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.dst_port));
1724038d2ef8SMaor Gottlieb 		break;
1725038d2ef8SMaor Gottlieb 	default:
1726038d2ef8SMaor Gottlieb 		return -EINVAL;
1727038d2ef8SMaor Gottlieb 	}
1728038d2ef8SMaor Gottlieb 
1729038d2ef8SMaor Gottlieb 	return 0;
1730038d2ef8SMaor Gottlieb }
1731038d2ef8SMaor Gottlieb 
1732038d2ef8SMaor Gottlieb /* If a flow could catch both multicast and unicast packets,
1733038d2ef8SMaor Gottlieb  * it won't fall into the multicast flow steering table and this rule
1734038d2ef8SMaor Gottlieb  * could steal other multicast packets.
1735038d2ef8SMaor Gottlieb  */
1736038d2ef8SMaor Gottlieb static bool flow_is_multicast_only(struct ib_flow_attr *ib_attr)
1737038d2ef8SMaor Gottlieb {
1738038d2ef8SMaor Gottlieb 	struct ib_flow_spec_eth *eth_spec;
1739038d2ef8SMaor Gottlieb 
1740038d2ef8SMaor Gottlieb 	if (ib_attr->type != IB_FLOW_ATTR_NORMAL ||
1741038d2ef8SMaor Gottlieb 	    ib_attr->size < sizeof(struct ib_flow_attr) +
1742038d2ef8SMaor Gottlieb 	    sizeof(struct ib_flow_spec_eth) ||
1743038d2ef8SMaor Gottlieb 	    ib_attr->num_of_specs < 1)
1744038d2ef8SMaor Gottlieb 		return false;
1745038d2ef8SMaor Gottlieb 
1746038d2ef8SMaor Gottlieb 	eth_spec = (struct ib_flow_spec_eth *)(ib_attr + 1);
1747038d2ef8SMaor Gottlieb 	if (eth_spec->type != IB_FLOW_SPEC_ETH ||
1748038d2ef8SMaor Gottlieb 	    eth_spec->size != sizeof(*eth_spec))
1749038d2ef8SMaor Gottlieb 		return false;
1750038d2ef8SMaor Gottlieb 
1751038d2ef8SMaor Gottlieb 	return is_multicast_ether_addr(eth_spec->mask.dst_mac) &&
1752038d2ef8SMaor Gottlieb 	       is_multicast_ether_addr(eth_spec->val.dst_mac);
1753038d2ef8SMaor Gottlieb }
1754038d2ef8SMaor Gottlieb 
1755dd063d0eSMaor Gottlieb static bool is_valid_attr(const struct ib_flow_attr *flow_attr)
1756038d2ef8SMaor Gottlieb {
1757038d2ef8SMaor Gottlieb 	union ib_flow_spec *ib_spec = (union ib_flow_spec *)(flow_attr + 1);
1758038d2ef8SMaor Gottlieb 	bool has_ipv4_spec = false;
1759038d2ef8SMaor Gottlieb 	bool eth_type_ipv4 = true;
1760038d2ef8SMaor Gottlieb 	unsigned int spec_index;
1761038d2ef8SMaor Gottlieb 
1762038d2ef8SMaor Gottlieb 	/* Validate that ethertype is correct */
1763038d2ef8SMaor Gottlieb 	for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
1764038d2ef8SMaor Gottlieb 		if (ib_spec->type == IB_FLOW_SPEC_ETH &&
1765038d2ef8SMaor Gottlieb 		    ib_spec->eth.mask.ether_type) {
1766038d2ef8SMaor Gottlieb 			if (!((ib_spec->eth.mask.ether_type == htons(0xffff)) &&
1767038d2ef8SMaor Gottlieb 			      ib_spec->eth.val.ether_type == htons(ETH_P_IP)))
1768038d2ef8SMaor Gottlieb 				eth_type_ipv4 = false;
1769038d2ef8SMaor Gottlieb 		} else if (ib_spec->type == IB_FLOW_SPEC_IPV4) {
1770038d2ef8SMaor Gottlieb 			has_ipv4_spec = true;
1771038d2ef8SMaor Gottlieb 		}
1772038d2ef8SMaor Gottlieb 		ib_spec = (void *)ib_spec + ib_spec->size;
1773038d2ef8SMaor Gottlieb 	}
1774038d2ef8SMaor Gottlieb 	return !has_ipv4_spec || eth_type_ipv4;
1775038d2ef8SMaor Gottlieb }
1776038d2ef8SMaor Gottlieb 
1777038d2ef8SMaor Gottlieb static void put_flow_table(struct mlx5_ib_dev *dev,
1778038d2ef8SMaor Gottlieb 			   struct mlx5_ib_flow_prio *prio, bool ft_added)
1779038d2ef8SMaor Gottlieb {
1780038d2ef8SMaor Gottlieb 	prio->refcount -= !!ft_added;
1781038d2ef8SMaor Gottlieb 	if (!prio->refcount) {
1782038d2ef8SMaor Gottlieb 		mlx5_destroy_flow_table(prio->flow_table);
1783038d2ef8SMaor Gottlieb 		prio->flow_table = NULL;
1784038d2ef8SMaor Gottlieb 	}
1785038d2ef8SMaor Gottlieb }
1786038d2ef8SMaor Gottlieb 
1787038d2ef8SMaor Gottlieb static int mlx5_ib_destroy_flow(struct ib_flow *flow_id)
1788038d2ef8SMaor Gottlieb {
1789038d2ef8SMaor Gottlieb 	struct mlx5_ib_dev *dev = to_mdev(flow_id->qp->device);
1790038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = container_of(flow_id,
1791038d2ef8SMaor Gottlieb 							  struct mlx5_ib_flow_handler,
1792038d2ef8SMaor Gottlieb 							  ibflow);
1793038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *iter, *tmp;
1794038d2ef8SMaor Gottlieb 
1795038d2ef8SMaor Gottlieb 	mutex_lock(&dev->flow_db.lock);
1796038d2ef8SMaor Gottlieb 
1797038d2ef8SMaor Gottlieb 	list_for_each_entry_safe(iter, tmp, &handler->list, list) {
179874491de9SMark Bloch 		mlx5_del_flow_rules(iter->rule);
1799cc0e5d42SMaor Gottlieb 		put_flow_table(dev, iter->prio, true);
1800038d2ef8SMaor Gottlieb 		list_del(&iter->list);
1801038d2ef8SMaor Gottlieb 		kfree(iter);
1802038d2ef8SMaor Gottlieb 	}
1803038d2ef8SMaor Gottlieb 
180474491de9SMark Bloch 	mlx5_del_flow_rules(handler->rule);
18055497adc6SMaor Gottlieb 	put_flow_table(dev, handler->prio, true);
1806038d2ef8SMaor Gottlieb 	mutex_unlock(&dev->flow_db.lock);
1807038d2ef8SMaor Gottlieb 
1808038d2ef8SMaor Gottlieb 	kfree(handler);
1809038d2ef8SMaor Gottlieb 
1810038d2ef8SMaor Gottlieb 	return 0;
1811038d2ef8SMaor Gottlieb }
1812038d2ef8SMaor Gottlieb 
181335d19011SMaor Gottlieb static int ib_prio_to_core_prio(unsigned int priority, bool dont_trap)
181435d19011SMaor Gottlieb {
181535d19011SMaor Gottlieb 	priority *= 2;
181635d19011SMaor Gottlieb 	if (!dont_trap)
181735d19011SMaor Gottlieb 		priority++;
181835d19011SMaor Gottlieb 	return priority;
181935d19011SMaor Gottlieb }
182035d19011SMaor Gottlieb 
1821cc0e5d42SMaor Gottlieb enum flow_table_type {
1822cc0e5d42SMaor Gottlieb 	MLX5_IB_FT_RX,
1823cc0e5d42SMaor Gottlieb 	MLX5_IB_FT_TX
1824cc0e5d42SMaor Gottlieb };
1825cc0e5d42SMaor Gottlieb 
1826038d2ef8SMaor Gottlieb #define MLX5_FS_MAX_TYPES	 10
1827038d2ef8SMaor Gottlieb #define MLX5_FS_MAX_ENTRIES	 32000UL
1828038d2ef8SMaor Gottlieb static struct mlx5_ib_flow_prio *get_flow_table(struct mlx5_ib_dev *dev,
1829cc0e5d42SMaor Gottlieb 						struct ib_flow_attr *flow_attr,
1830cc0e5d42SMaor Gottlieb 						enum flow_table_type ft_type)
1831038d2ef8SMaor Gottlieb {
183235d19011SMaor Gottlieb 	bool dont_trap = flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP;
1833038d2ef8SMaor Gottlieb 	struct mlx5_flow_namespace *ns = NULL;
1834038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_prio *prio;
1835038d2ef8SMaor Gottlieb 	struct mlx5_flow_table *ft;
1836038d2ef8SMaor Gottlieb 	int num_entries;
1837038d2ef8SMaor Gottlieb 	int num_groups;
1838038d2ef8SMaor Gottlieb 	int priority;
1839038d2ef8SMaor Gottlieb 	int err = 0;
1840038d2ef8SMaor Gottlieb 
1841038d2ef8SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
184235d19011SMaor Gottlieb 		if (flow_is_multicast_only(flow_attr) &&
184335d19011SMaor Gottlieb 		    !dont_trap)
1844038d2ef8SMaor Gottlieb 			priority = MLX5_IB_FLOW_MCAST_PRIO;
1845038d2ef8SMaor Gottlieb 		else
184635d19011SMaor Gottlieb 			priority = ib_prio_to_core_prio(flow_attr->priority,
184735d19011SMaor Gottlieb 							dont_trap);
1848038d2ef8SMaor Gottlieb 		ns = mlx5_get_flow_namespace(dev->mdev,
1849038d2ef8SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_BYPASS);
1850038d2ef8SMaor Gottlieb 		num_entries = MLX5_FS_MAX_ENTRIES;
1851038d2ef8SMaor Gottlieb 		num_groups = MLX5_FS_MAX_TYPES;
1852038d2ef8SMaor Gottlieb 		prio = &dev->flow_db.prios[priority];
1853038d2ef8SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
1854038d2ef8SMaor Gottlieb 		   flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
1855038d2ef8SMaor Gottlieb 		ns = mlx5_get_flow_namespace(dev->mdev,
1856038d2ef8SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_LEFTOVERS);
1857038d2ef8SMaor Gottlieb 		build_leftovers_ft_param(&priority,
1858038d2ef8SMaor Gottlieb 					 &num_entries,
1859038d2ef8SMaor Gottlieb 					 &num_groups);
1860038d2ef8SMaor Gottlieb 		prio = &dev->flow_db.prios[MLX5_IB_FLOW_LEFTOVERS_PRIO];
1861cc0e5d42SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
1862cc0e5d42SMaor Gottlieb 		if (!MLX5_CAP_FLOWTABLE(dev->mdev,
1863cc0e5d42SMaor Gottlieb 					allow_sniffer_and_nic_rx_shared_tir))
1864cc0e5d42SMaor Gottlieb 			return ERR_PTR(-ENOTSUPP);
1865cc0e5d42SMaor Gottlieb 
1866cc0e5d42SMaor Gottlieb 		ns = mlx5_get_flow_namespace(dev->mdev, ft_type == MLX5_IB_FT_RX ?
1867cc0e5d42SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_SNIFFER_RX :
1868cc0e5d42SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_SNIFFER_TX);
1869cc0e5d42SMaor Gottlieb 
1870cc0e5d42SMaor Gottlieb 		prio = &dev->flow_db.sniffer[ft_type];
1871cc0e5d42SMaor Gottlieb 		priority = 0;
1872cc0e5d42SMaor Gottlieb 		num_entries = 1;
1873cc0e5d42SMaor Gottlieb 		num_groups = 1;
1874038d2ef8SMaor Gottlieb 	}
1875038d2ef8SMaor Gottlieb 
1876038d2ef8SMaor Gottlieb 	if (!ns)
1877038d2ef8SMaor Gottlieb 		return ERR_PTR(-ENOTSUPP);
1878038d2ef8SMaor Gottlieb 
1879038d2ef8SMaor Gottlieb 	ft = prio->flow_table;
1880038d2ef8SMaor Gottlieb 	if (!ft) {
1881038d2ef8SMaor Gottlieb 		ft = mlx5_create_auto_grouped_flow_table(ns, priority,
1882038d2ef8SMaor Gottlieb 							 num_entries,
1883d63cd286SMaor Gottlieb 							 num_groups,
1884d63cd286SMaor Gottlieb 							 0);
1885038d2ef8SMaor Gottlieb 
1886038d2ef8SMaor Gottlieb 		if (!IS_ERR(ft)) {
1887038d2ef8SMaor Gottlieb 			prio->refcount = 0;
1888038d2ef8SMaor Gottlieb 			prio->flow_table = ft;
1889038d2ef8SMaor Gottlieb 		} else {
1890038d2ef8SMaor Gottlieb 			err = PTR_ERR(ft);
1891038d2ef8SMaor Gottlieb 		}
1892038d2ef8SMaor Gottlieb 	}
1893038d2ef8SMaor Gottlieb 
1894038d2ef8SMaor Gottlieb 	return err ? ERR_PTR(err) : prio;
1895038d2ef8SMaor Gottlieb }
1896038d2ef8SMaor Gottlieb 
1897038d2ef8SMaor Gottlieb static struct mlx5_ib_flow_handler *create_flow_rule(struct mlx5_ib_dev *dev,
1898038d2ef8SMaor Gottlieb 						     struct mlx5_ib_flow_prio *ft_prio,
1899dd063d0eSMaor Gottlieb 						     const struct ib_flow_attr *flow_attr,
1900038d2ef8SMaor Gottlieb 						     struct mlx5_flow_destination *dst)
1901038d2ef8SMaor Gottlieb {
1902038d2ef8SMaor Gottlieb 	struct mlx5_flow_table	*ft = ft_prio->flow_table;
1903038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler;
1904c5bb1730SMaor Gottlieb 	struct mlx5_flow_spec *spec;
1905dd063d0eSMaor Gottlieb 	const void *ib_flow = (const void *)flow_attr + sizeof(*flow_attr);
1906038d2ef8SMaor Gottlieb 	unsigned int spec_index;
190735d19011SMaor Gottlieb 	u32 action;
1908038d2ef8SMaor Gottlieb 	int err = 0;
1909038d2ef8SMaor Gottlieb 
1910038d2ef8SMaor Gottlieb 	if (!is_valid_attr(flow_attr))
1911038d2ef8SMaor Gottlieb 		return ERR_PTR(-EINVAL);
1912038d2ef8SMaor Gottlieb 
1913c5bb1730SMaor Gottlieb 	spec = mlx5_vzalloc(sizeof(*spec));
1914038d2ef8SMaor Gottlieb 	handler = kzalloc(sizeof(*handler), GFP_KERNEL);
1915c5bb1730SMaor Gottlieb 	if (!handler || !spec) {
1916038d2ef8SMaor Gottlieb 		err = -ENOMEM;
1917038d2ef8SMaor Gottlieb 		goto free;
1918038d2ef8SMaor Gottlieb 	}
1919038d2ef8SMaor Gottlieb 
1920038d2ef8SMaor Gottlieb 	INIT_LIST_HEAD(&handler->list);
1921038d2ef8SMaor Gottlieb 
1922038d2ef8SMaor Gottlieb 	for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
1923c5bb1730SMaor Gottlieb 		err = parse_flow_attr(spec->match_criteria,
1924c5bb1730SMaor Gottlieb 				      spec->match_value, ib_flow);
1925038d2ef8SMaor Gottlieb 		if (err < 0)
1926038d2ef8SMaor Gottlieb 			goto free;
1927038d2ef8SMaor Gottlieb 
1928038d2ef8SMaor Gottlieb 		ib_flow += ((union ib_flow_spec *)ib_flow)->size;
1929038d2ef8SMaor Gottlieb 	}
1930038d2ef8SMaor Gottlieb 
1931466fa6d2SMaor Gottlieb 	spec->match_criteria_enable = get_match_criteria_enable(spec->match_criteria);
193235d19011SMaor Gottlieb 	action = dst ? MLX5_FLOW_CONTEXT_ACTION_FWD_DEST :
193335d19011SMaor Gottlieb 		MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO;
193474491de9SMark Bloch 	handler->rule = mlx5_add_flow_rules(ft, spec,
193535d19011SMaor Gottlieb 					   action,
1936038d2ef8SMaor Gottlieb 					   MLX5_FS_DEFAULT_FLOW_TAG,
193774491de9SMark Bloch 					   dst, 1);
1938038d2ef8SMaor Gottlieb 
1939038d2ef8SMaor Gottlieb 	if (IS_ERR(handler->rule)) {
1940038d2ef8SMaor Gottlieb 		err = PTR_ERR(handler->rule);
1941038d2ef8SMaor Gottlieb 		goto free;
1942038d2ef8SMaor Gottlieb 	}
1943038d2ef8SMaor Gottlieb 
1944d9d4980aSMaor Gottlieb 	ft_prio->refcount++;
19455497adc6SMaor Gottlieb 	handler->prio = ft_prio;
1946038d2ef8SMaor Gottlieb 
1947038d2ef8SMaor Gottlieb 	ft_prio->flow_table = ft;
1948038d2ef8SMaor Gottlieb free:
1949038d2ef8SMaor Gottlieb 	if (err)
1950038d2ef8SMaor Gottlieb 		kfree(handler);
1951c5bb1730SMaor Gottlieb 	kvfree(spec);
1952038d2ef8SMaor Gottlieb 	return err ? ERR_PTR(err) : handler;
1953038d2ef8SMaor Gottlieb }
1954038d2ef8SMaor Gottlieb 
195535d19011SMaor Gottlieb static struct mlx5_ib_flow_handler *create_dont_trap_rule(struct mlx5_ib_dev *dev,
195635d19011SMaor Gottlieb 							  struct mlx5_ib_flow_prio *ft_prio,
195735d19011SMaor Gottlieb 							  struct ib_flow_attr *flow_attr,
195835d19011SMaor Gottlieb 							  struct mlx5_flow_destination *dst)
195935d19011SMaor Gottlieb {
196035d19011SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_dst = NULL;
196135d19011SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
196235d19011SMaor Gottlieb 
196335d19011SMaor Gottlieb 	handler = create_flow_rule(dev, ft_prio, flow_attr, NULL);
196435d19011SMaor Gottlieb 	if (!IS_ERR(handler)) {
196535d19011SMaor Gottlieb 		handler_dst = create_flow_rule(dev, ft_prio,
196635d19011SMaor Gottlieb 					       flow_attr, dst);
196735d19011SMaor Gottlieb 		if (IS_ERR(handler_dst)) {
196874491de9SMark Bloch 			mlx5_del_flow_rules(handler->rule);
1969d9d4980aSMaor Gottlieb 			ft_prio->refcount--;
197035d19011SMaor Gottlieb 			kfree(handler);
197135d19011SMaor Gottlieb 			handler = handler_dst;
197235d19011SMaor Gottlieb 		} else {
197335d19011SMaor Gottlieb 			list_add(&handler_dst->list, &handler->list);
197435d19011SMaor Gottlieb 		}
197535d19011SMaor Gottlieb 	}
197635d19011SMaor Gottlieb 
197735d19011SMaor Gottlieb 	return handler;
197835d19011SMaor Gottlieb }
1979038d2ef8SMaor Gottlieb enum {
1980038d2ef8SMaor Gottlieb 	LEFTOVERS_MC,
1981038d2ef8SMaor Gottlieb 	LEFTOVERS_UC,
1982038d2ef8SMaor Gottlieb };
1983038d2ef8SMaor Gottlieb 
1984038d2ef8SMaor Gottlieb static struct mlx5_ib_flow_handler *create_leftovers_rule(struct mlx5_ib_dev *dev,
1985038d2ef8SMaor Gottlieb 							  struct mlx5_ib_flow_prio *ft_prio,
1986038d2ef8SMaor Gottlieb 							  struct ib_flow_attr *flow_attr,
1987038d2ef8SMaor Gottlieb 							  struct mlx5_flow_destination *dst)
1988038d2ef8SMaor Gottlieb {
1989038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_ucast = NULL;
1990038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
1991038d2ef8SMaor Gottlieb 
1992038d2ef8SMaor Gottlieb 	static struct {
1993038d2ef8SMaor Gottlieb 		struct ib_flow_attr	flow_attr;
1994038d2ef8SMaor Gottlieb 		struct ib_flow_spec_eth eth_flow;
1995038d2ef8SMaor Gottlieb 	} leftovers_specs[] = {
1996038d2ef8SMaor Gottlieb 		[LEFTOVERS_MC] = {
1997038d2ef8SMaor Gottlieb 			.flow_attr = {
1998038d2ef8SMaor Gottlieb 				.num_of_specs = 1,
1999038d2ef8SMaor Gottlieb 				.size = sizeof(leftovers_specs[0])
2000038d2ef8SMaor Gottlieb 			},
2001038d2ef8SMaor Gottlieb 			.eth_flow = {
2002038d2ef8SMaor Gottlieb 				.type = IB_FLOW_SPEC_ETH,
2003038d2ef8SMaor Gottlieb 				.size = sizeof(struct ib_flow_spec_eth),
2004038d2ef8SMaor Gottlieb 				.mask = {.dst_mac = {0x1} },
2005038d2ef8SMaor Gottlieb 				.val =  {.dst_mac = {0x1} }
2006038d2ef8SMaor Gottlieb 			}
2007038d2ef8SMaor Gottlieb 		},
2008038d2ef8SMaor Gottlieb 		[LEFTOVERS_UC] = {
2009038d2ef8SMaor Gottlieb 			.flow_attr = {
2010038d2ef8SMaor Gottlieb 				.num_of_specs = 1,
2011038d2ef8SMaor Gottlieb 				.size = sizeof(leftovers_specs[0])
2012038d2ef8SMaor Gottlieb 			},
2013038d2ef8SMaor Gottlieb 			.eth_flow = {
2014038d2ef8SMaor Gottlieb 				.type = IB_FLOW_SPEC_ETH,
2015038d2ef8SMaor Gottlieb 				.size = sizeof(struct ib_flow_spec_eth),
2016038d2ef8SMaor Gottlieb 				.mask = {.dst_mac = {0x1} },
2017038d2ef8SMaor Gottlieb 				.val = {.dst_mac = {} }
2018038d2ef8SMaor Gottlieb 			}
2019038d2ef8SMaor Gottlieb 		}
2020038d2ef8SMaor Gottlieb 	};
2021038d2ef8SMaor Gottlieb 
2022038d2ef8SMaor Gottlieb 	handler = create_flow_rule(dev, ft_prio,
2023038d2ef8SMaor Gottlieb 				   &leftovers_specs[LEFTOVERS_MC].flow_attr,
2024038d2ef8SMaor Gottlieb 				   dst);
2025038d2ef8SMaor Gottlieb 	if (!IS_ERR(handler) &&
2026038d2ef8SMaor Gottlieb 	    flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT) {
2027038d2ef8SMaor Gottlieb 		handler_ucast = create_flow_rule(dev, ft_prio,
2028038d2ef8SMaor Gottlieb 						 &leftovers_specs[LEFTOVERS_UC].flow_attr,
2029038d2ef8SMaor Gottlieb 						 dst);
2030038d2ef8SMaor Gottlieb 		if (IS_ERR(handler_ucast)) {
203174491de9SMark Bloch 			mlx5_del_flow_rules(handler->rule);
2032d9d4980aSMaor Gottlieb 			ft_prio->refcount--;
2033038d2ef8SMaor Gottlieb 			kfree(handler);
2034038d2ef8SMaor Gottlieb 			handler = handler_ucast;
2035038d2ef8SMaor Gottlieb 		} else {
2036038d2ef8SMaor Gottlieb 			list_add(&handler_ucast->list, &handler->list);
2037038d2ef8SMaor Gottlieb 		}
2038038d2ef8SMaor Gottlieb 	}
2039038d2ef8SMaor Gottlieb 
2040038d2ef8SMaor Gottlieb 	return handler;
2041038d2ef8SMaor Gottlieb }
2042038d2ef8SMaor Gottlieb 
2043cc0e5d42SMaor Gottlieb static struct mlx5_ib_flow_handler *create_sniffer_rule(struct mlx5_ib_dev *dev,
2044cc0e5d42SMaor Gottlieb 							struct mlx5_ib_flow_prio *ft_rx,
2045cc0e5d42SMaor Gottlieb 							struct mlx5_ib_flow_prio *ft_tx,
2046cc0e5d42SMaor Gottlieb 							struct mlx5_flow_destination *dst)
2047cc0e5d42SMaor Gottlieb {
2048cc0e5d42SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_rx;
2049cc0e5d42SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_tx;
2050cc0e5d42SMaor Gottlieb 	int err;
2051cc0e5d42SMaor Gottlieb 	static const struct ib_flow_attr flow_attr  = {
2052cc0e5d42SMaor Gottlieb 		.num_of_specs = 0,
2053cc0e5d42SMaor Gottlieb 		.size = sizeof(flow_attr)
2054cc0e5d42SMaor Gottlieb 	};
2055cc0e5d42SMaor Gottlieb 
2056cc0e5d42SMaor Gottlieb 	handler_rx = create_flow_rule(dev, ft_rx, &flow_attr, dst);
2057cc0e5d42SMaor Gottlieb 	if (IS_ERR(handler_rx)) {
2058cc0e5d42SMaor Gottlieb 		err = PTR_ERR(handler_rx);
2059cc0e5d42SMaor Gottlieb 		goto err;
2060cc0e5d42SMaor Gottlieb 	}
2061cc0e5d42SMaor Gottlieb 
2062cc0e5d42SMaor Gottlieb 	handler_tx = create_flow_rule(dev, ft_tx, &flow_attr, dst);
2063cc0e5d42SMaor Gottlieb 	if (IS_ERR(handler_tx)) {
2064cc0e5d42SMaor Gottlieb 		err = PTR_ERR(handler_tx);
2065cc0e5d42SMaor Gottlieb 		goto err_tx;
2066cc0e5d42SMaor Gottlieb 	}
2067cc0e5d42SMaor Gottlieb 
2068cc0e5d42SMaor Gottlieb 	list_add(&handler_tx->list, &handler_rx->list);
2069cc0e5d42SMaor Gottlieb 
2070cc0e5d42SMaor Gottlieb 	return handler_rx;
2071cc0e5d42SMaor Gottlieb 
2072cc0e5d42SMaor Gottlieb err_tx:
207374491de9SMark Bloch 	mlx5_del_flow_rules(handler_rx->rule);
2074cc0e5d42SMaor Gottlieb 	ft_rx->refcount--;
2075cc0e5d42SMaor Gottlieb 	kfree(handler_rx);
2076cc0e5d42SMaor Gottlieb err:
2077cc0e5d42SMaor Gottlieb 	return ERR_PTR(err);
2078cc0e5d42SMaor Gottlieb }
2079cc0e5d42SMaor Gottlieb 
2080038d2ef8SMaor Gottlieb static struct ib_flow *mlx5_ib_create_flow(struct ib_qp *qp,
2081038d2ef8SMaor Gottlieb 					   struct ib_flow_attr *flow_attr,
2082038d2ef8SMaor Gottlieb 					   int domain)
2083038d2ef8SMaor Gottlieb {
2084038d2ef8SMaor Gottlieb 	struct mlx5_ib_dev *dev = to_mdev(qp->device);
2085d9f88e5aSYishai Hadas 	struct mlx5_ib_qp *mqp = to_mqp(qp);
2086038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
2087038d2ef8SMaor Gottlieb 	struct mlx5_flow_destination *dst = NULL;
2088cc0e5d42SMaor Gottlieb 	struct mlx5_ib_flow_prio *ft_prio_tx = NULL;
2089038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_prio *ft_prio;
2090038d2ef8SMaor Gottlieb 	int err;
2091038d2ef8SMaor Gottlieb 
2092038d2ef8SMaor Gottlieb 	if (flow_attr->priority > MLX5_IB_FLOW_LAST_PRIO)
2093038d2ef8SMaor Gottlieb 		return ERR_PTR(-ENOSPC);
2094038d2ef8SMaor Gottlieb 
2095038d2ef8SMaor Gottlieb 	if (domain != IB_FLOW_DOMAIN_USER ||
2096038d2ef8SMaor Gottlieb 	    flow_attr->port > MLX5_CAP_GEN(dev->mdev, num_ports) ||
209735d19011SMaor Gottlieb 	    (flow_attr->flags & ~IB_FLOW_ATTR_FLAGS_DONT_TRAP))
2098038d2ef8SMaor Gottlieb 		return ERR_PTR(-EINVAL);
2099038d2ef8SMaor Gottlieb 
2100038d2ef8SMaor Gottlieb 	dst = kzalloc(sizeof(*dst), GFP_KERNEL);
2101038d2ef8SMaor Gottlieb 	if (!dst)
2102038d2ef8SMaor Gottlieb 		return ERR_PTR(-ENOMEM);
2103038d2ef8SMaor Gottlieb 
2104038d2ef8SMaor Gottlieb 	mutex_lock(&dev->flow_db.lock);
2105038d2ef8SMaor Gottlieb 
2106cc0e5d42SMaor Gottlieb 	ft_prio = get_flow_table(dev, flow_attr, MLX5_IB_FT_RX);
2107038d2ef8SMaor Gottlieb 	if (IS_ERR(ft_prio)) {
2108038d2ef8SMaor Gottlieb 		err = PTR_ERR(ft_prio);
2109038d2ef8SMaor Gottlieb 		goto unlock;
2110038d2ef8SMaor Gottlieb 	}
2111cc0e5d42SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
2112cc0e5d42SMaor Gottlieb 		ft_prio_tx = get_flow_table(dev, flow_attr, MLX5_IB_FT_TX);
2113cc0e5d42SMaor Gottlieb 		if (IS_ERR(ft_prio_tx)) {
2114cc0e5d42SMaor Gottlieb 			err = PTR_ERR(ft_prio_tx);
2115cc0e5d42SMaor Gottlieb 			ft_prio_tx = NULL;
2116cc0e5d42SMaor Gottlieb 			goto destroy_ft;
2117cc0e5d42SMaor Gottlieb 		}
2118cc0e5d42SMaor Gottlieb 	}
2119038d2ef8SMaor Gottlieb 
2120038d2ef8SMaor Gottlieb 	dst->type = MLX5_FLOW_DESTINATION_TYPE_TIR;
2121d9f88e5aSYishai Hadas 	if (mqp->flags & MLX5_IB_QP_RSS)
2122d9f88e5aSYishai Hadas 		dst->tir_num = mqp->rss_qp.tirn;
2123d9f88e5aSYishai Hadas 	else
2124d9f88e5aSYishai Hadas 		dst->tir_num = mqp->raw_packet_qp.rq.tirn;
2125038d2ef8SMaor Gottlieb 
2126038d2ef8SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
212735d19011SMaor Gottlieb 		if (flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP)  {
212835d19011SMaor Gottlieb 			handler = create_dont_trap_rule(dev, ft_prio,
212935d19011SMaor Gottlieb 							flow_attr, dst);
213035d19011SMaor Gottlieb 		} else {
2131038d2ef8SMaor Gottlieb 			handler = create_flow_rule(dev, ft_prio, flow_attr,
2132038d2ef8SMaor Gottlieb 						   dst);
213335d19011SMaor Gottlieb 		}
2134038d2ef8SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
2135038d2ef8SMaor Gottlieb 		   flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
2136038d2ef8SMaor Gottlieb 		handler = create_leftovers_rule(dev, ft_prio, flow_attr,
2137038d2ef8SMaor Gottlieb 						dst);
2138cc0e5d42SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
2139cc0e5d42SMaor Gottlieb 		handler = create_sniffer_rule(dev, ft_prio, ft_prio_tx, dst);
2140038d2ef8SMaor Gottlieb 	} else {
2141038d2ef8SMaor Gottlieb 		err = -EINVAL;
2142038d2ef8SMaor Gottlieb 		goto destroy_ft;
2143038d2ef8SMaor Gottlieb 	}
2144038d2ef8SMaor Gottlieb 
2145038d2ef8SMaor Gottlieb 	if (IS_ERR(handler)) {
2146038d2ef8SMaor Gottlieb 		err = PTR_ERR(handler);
2147038d2ef8SMaor Gottlieb 		handler = NULL;
2148038d2ef8SMaor Gottlieb 		goto destroy_ft;
2149038d2ef8SMaor Gottlieb 	}
2150038d2ef8SMaor Gottlieb 
2151038d2ef8SMaor Gottlieb 	mutex_unlock(&dev->flow_db.lock);
2152038d2ef8SMaor Gottlieb 	kfree(dst);
2153038d2ef8SMaor Gottlieb 
2154038d2ef8SMaor Gottlieb 	return &handler->ibflow;
2155038d2ef8SMaor Gottlieb 
2156038d2ef8SMaor Gottlieb destroy_ft:
2157038d2ef8SMaor Gottlieb 	put_flow_table(dev, ft_prio, false);
2158cc0e5d42SMaor Gottlieb 	if (ft_prio_tx)
2159cc0e5d42SMaor Gottlieb 		put_flow_table(dev, ft_prio_tx, false);
2160038d2ef8SMaor Gottlieb unlock:
2161038d2ef8SMaor Gottlieb 	mutex_unlock(&dev->flow_db.lock);
2162038d2ef8SMaor Gottlieb 	kfree(dst);
2163038d2ef8SMaor Gottlieb 	kfree(handler);
2164038d2ef8SMaor Gottlieb 	return ERR_PTR(err);
2165038d2ef8SMaor Gottlieb }
2166038d2ef8SMaor Gottlieb 
2167e126ba97SEli Cohen static int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
2168e126ba97SEli Cohen {
2169e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
2170e126ba97SEli Cohen 	int err;
2171e126ba97SEli Cohen 
21729603b61dSJack Morgenstein 	err = mlx5_core_attach_mcg(dev->mdev, gid, ibqp->qp_num);
2173e126ba97SEli Cohen 	if (err)
2174e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n",
2175e126ba97SEli Cohen 			     ibqp->qp_num, gid->raw);
2176e126ba97SEli Cohen 
2177e126ba97SEli Cohen 	return err;
2178e126ba97SEli Cohen }
2179e126ba97SEli Cohen 
2180e126ba97SEli Cohen static int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
2181e126ba97SEli Cohen {
2182e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
2183e126ba97SEli Cohen 	int err;
2184e126ba97SEli Cohen 
21859603b61dSJack Morgenstein 	err = mlx5_core_detach_mcg(dev->mdev, gid, ibqp->qp_num);
2186e126ba97SEli Cohen 	if (err)
2187e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n",
2188e126ba97SEli Cohen 			     ibqp->qp_num, gid->raw);
2189e126ba97SEli Cohen 
2190e126ba97SEli Cohen 	return err;
2191e126ba97SEli Cohen }
2192e126ba97SEli Cohen 
2193e126ba97SEli Cohen static int init_node_data(struct mlx5_ib_dev *dev)
2194e126ba97SEli Cohen {
21951b5daf11SMajd Dibbiny 	int err;
2196e126ba97SEli Cohen 
21971b5daf11SMajd Dibbiny 	err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc);
2198e126ba97SEli Cohen 	if (err)
2199e126ba97SEli Cohen 		return err;
22001b5daf11SMajd Dibbiny 
22011b5daf11SMajd Dibbiny 	dev->mdev->rev_id = dev->mdev->pdev->revision;
22021b5daf11SMajd Dibbiny 
22031b5daf11SMajd Dibbiny 	return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid);
2204e126ba97SEli Cohen }
2205e126ba97SEli Cohen 
2206e126ba97SEli Cohen static ssize_t show_fw_pages(struct device *device, struct device_attribute *attr,
2207e126ba97SEli Cohen 			     char *buf)
2208e126ba97SEli Cohen {
2209e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
2210e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
2211e126ba97SEli Cohen 
22129603b61dSJack Morgenstein 	return sprintf(buf, "%d\n", dev->mdev->priv.fw_pages);
2213e126ba97SEli Cohen }
2214e126ba97SEli Cohen 
2215e126ba97SEli Cohen static ssize_t show_reg_pages(struct device *device,
2216e126ba97SEli Cohen 			      struct device_attribute *attr, char *buf)
2217e126ba97SEli Cohen {
2218e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
2219e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
2220e126ba97SEli Cohen 
22216aec21f6SHaggai Eran 	return sprintf(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages));
2222e126ba97SEli Cohen }
2223e126ba97SEli Cohen 
2224e126ba97SEli Cohen static ssize_t show_hca(struct device *device, struct device_attribute *attr,
2225e126ba97SEli Cohen 			char *buf)
2226e126ba97SEli Cohen {
2227e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
2228e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
22299603b61dSJack Morgenstein 	return sprintf(buf, "MT%d\n", dev->mdev->pdev->device);
2230e126ba97SEli Cohen }
2231e126ba97SEli Cohen 
2232e126ba97SEli Cohen static ssize_t show_rev(struct device *device, struct device_attribute *attr,
2233e126ba97SEli Cohen 			char *buf)
2234e126ba97SEli Cohen {
2235e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
2236e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
22379603b61dSJack Morgenstein 	return sprintf(buf, "%x\n", dev->mdev->rev_id);
2238e126ba97SEli Cohen }
2239e126ba97SEli Cohen 
2240e126ba97SEli Cohen static ssize_t show_board(struct device *device, struct device_attribute *attr,
2241e126ba97SEli Cohen 			  char *buf)
2242e126ba97SEli Cohen {
2243e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
2244e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
2245e126ba97SEli Cohen 	return sprintf(buf, "%.*s\n", MLX5_BOARD_ID_LEN,
22469603b61dSJack Morgenstein 		       dev->mdev->board_id);
2247e126ba97SEli Cohen }
2248e126ba97SEli Cohen 
2249e126ba97SEli Cohen static DEVICE_ATTR(hw_rev,   S_IRUGO, show_rev,    NULL);
2250e126ba97SEli Cohen static DEVICE_ATTR(hca_type, S_IRUGO, show_hca,    NULL);
2251e126ba97SEli Cohen static DEVICE_ATTR(board_id, S_IRUGO, show_board,  NULL);
2252e126ba97SEli Cohen static DEVICE_ATTR(fw_pages, S_IRUGO, show_fw_pages, NULL);
2253e126ba97SEli Cohen static DEVICE_ATTR(reg_pages, S_IRUGO, show_reg_pages, NULL);
2254e126ba97SEli Cohen 
2255e126ba97SEli Cohen static struct device_attribute *mlx5_class_attributes[] = {
2256e126ba97SEli Cohen 	&dev_attr_hw_rev,
2257e126ba97SEli Cohen 	&dev_attr_hca_type,
2258e126ba97SEli Cohen 	&dev_attr_board_id,
2259e126ba97SEli Cohen 	&dev_attr_fw_pages,
2260e126ba97SEli Cohen 	&dev_attr_reg_pages,
2261e126ba97SEli Cohen };
2262e126ba97SEli Cohen 
22637722f47eSHaggai Eran static void pkey_change_handler(struct work_struct *work)
22647722f47eSHaggai Eran {
22657722f47eSHaggai Eran 	struct mlx5_ib_port_resources *ports =
22667722f47eSHaggai Eran 		container_of(work, struct mlx5_ib_port_resources,
22677722f47eSHaggai Eran 			     pkey_change_work);
22687722f47eSHaggai Eran 
22697722f47eSHaggai Eran 	mutex_lock(&ports->devr->mutex);
22707722f47eSHaggai Eran 	mlx5_ib_gsi_pkey_change(ports->gsi);
22717722f47eSHaggai Eran 	mutex_unlock(&ports->devr->mutex);
22727722f47eSHaggai Eran }
22737722f47eSHaggai Eran 
227489ea94a7SMaor Gottlieb static void mlx5_ib_handle_internal_error(struct mlx5_ib_dev *ibdev)
227589ea94a7SMaor Gottlieb {
227689ea94a7SMaor Gottlieb 	struct mlx5_ib_qp *mqp;
227789ea94a7SMaor Gottlieb 	struct mlx5_ib_cq *send_mcq, *recv_mcq;
227889ea94a7SMaor Gottlieb 	struct mlx5_core_cq *mcq;
227989ea94a7SMaor Gottlieb 	struct list_head cq_armed_list;
228089ea94a7SMaor Gottlieb 	unsigned long flags_qp;
228189ea94a7SMaor Gottlieb 	unsigned long flags_cq;
228289ea94a7SMaor Gottlieb 	unsigned long flags;
228389ea94a7SMaor Gottlieb 
228489ea94a7SMaor Gottlieb 	INIT_LIST_HEAD(&cq_armed_list);
228589ea94a7SMaor Gottlieb 
228689ea94a7SMaor Gottlieb 	/* Go over qp list reside on that ibdev, sync with create/destroy qp.*/
228789ea94a7SMaor Gottlieb 	spin_lock_irqsave(&ibdev->reset_flow_resource_lock, flags);
228889ea94a7SMaor Gottlieb 	list_for_each_entry(mqp, &ibdev->qp_list, qps_list) {
228989ea94a7SMaor Gottlieb 		spin_lock_irqsave(&mqp->sq.lock, flags_qp);
229089ea94a7SMaor Gottlieb 		if (mqp->sq.tail != mqp->sq.head) {
229189ea94a7SMaor Gottlieb 			send_mcq = to_mcq(mqp->ibqp.send_cq);
229289ea94a7SMaor Gottlieb 			spin_lock_irqsave(&send_mcq->lock, flags_cq);
229389ea94a7SMaor Gottlieb 			if (send_mcq->mcq.comp &&
229489ea94a7SMaor Gottlieb 			    mqp->ibqp.send_cq->comp_handler) {
229589ea94a7SMaor Gottlieb 				if (!send_mcq->mcq.reset_notify_added) {
229689ea94a7SMaor Gottlieb 					send_mcq->mcq.reset_notify_added = 1;
229789ea94a7SMaor Gottlieb 					list_add_tail(&send_mcq->mcq.reset_notify,
229889ea94a7SMaor Gottlieb 						      &cq_armed_list);
229989ea94a7SMaor Gottlieb 				}
230089ea94a7SMaor Gottlieb 			}
230189ea94a7SMaor Gottlieb 			spin_unlock_irqrestore(&send_mcq->lock, flags_cq);
230289ea94a7SMaor Gottlieb 		}
230389ea94a7SMaor Gottlieb 		spin_unlock_irqrestore(&mqp->sq.lock, flags_qp);
230489ea94a7SMaor Gottlieb 		spin_lock_irqsave(&mqp->rq.lock, flags_qp);
230589ea94a7SMaor Gottlieb 		/* no handling is needed for SRQ */
230689ea94a7SMaor Gottlieb 		if (!mqp->ibqp.srq) {
230789ea94a7SMaor Gottlieb 			if (mqp->rq.tail != mqp->rq.head) {
230889ea94a7SMaor Gottlieb 				recv_mcq = to_mcq(mqp->ibqp.recv_cq);
230989ea94a7SMaor Gottlieb 				spin_lock_irqsave(&recv_mcq->lock, flags_cq);
231089ea94a7SMaor Gottlieb 				if (recv_mcq->mcq.comp &&
231189ea94a7SMaor Gottlieb 				    mqp->ibqp.recv_cq->comp_handler) {
231289ea94a7SMaor Gottlieb 					if (!recv_mcq->mcq.reset_notify_added) {
231389ea94a7SMaor Gottlieb 						recv_mcq->mcq.reset_notify_added = 1;
231489ea94a7SMaor Gottlieb 						list_add_tail(&recv_mcq->mcq.reset_notify,
231589ea94a7SMaor Gottlieb 							      &cq_armed_list);
231689ea94a7SMaor Gottlieb 					}
231789ea94a7SMaor Gottlieb 				}
231889ea94a7SMaor Gottlieb 				spin_unlock_irqrestore(&recv_mcq->lock,
231989ea94a7SMaor Gottlieb 						       flags_cq);
232089ea94a7SMaor Gottlieb 			}
232189ea94a7SMaor Gottlieb 		}
232289ea94a7SMaor Gottlieb 		spin_unlock_irqrestore(&mqp->rq.lock, flags_qp);
232389ea94a7SMaor Gottlieb 	}
232489ea94a7SMaor Gottlieb 	/*At that point all inflight post send were put to be executed as of we
232589ea94a7SMaor Gottlieb 	 * lock/unlock above locks Now need to arm all involved CQs.
232689ea94a7SMaor Gottlieb 	 */
232789ea94a7SMaor Gottlieb 	list_for_each_entry(mcq, &cq_armed_list, reset_notify) {
232889ea94a7SMaor Gottlieb 		mcq->comp(mcq);
232989ea94a7SMaor Gottlieb 	}
233089ea94a7SMaor Gottlieb 	spin_unlock_irqrestore(&ibdev->reset_flow_resource_lock, flags);
233189ea94a7SMaor Gottlieb }
233289ea94a7SMaor Gottlieb 
23339603b61dSJack Morgenstein static void mlx5_ib_event(struct mlx5_core_dev *dev, void *context,
23344d2f9bbbSJack Morgenstein 			  enum mlx5_dev_event event, unsigned long param)
2335e126ba97SEli Cohen {
23369603b61dSJack Morgenstein 	struct mlx5_ib_dev *ibdev = (struct mlx5_ib_dev *)context;
2337e126ba97SEli Cohen 	struct ib_event ibev;
23389603b61dSJack Morgenstein 
2339e126ba97SEli Cohen 	u8 port = 0;
2340e126ba97SEli Cohen 
2341e126ba97SEli Cohen 	switch (event) {
2342e126ba97SEli Cohen 	case MLX5_DEV_EVENT_SYS_ERROR:
2343e126ba97SEli Cohen 		ibdev->ib_active = false;
2344e126ba97SEli Cohen 		ibev.event = IB_EVENT_DEVICE_FATAL;
234589ea94a7SMaor Gottlieb 		mlx5_ib_handle_internal_error(ibdev);
2346e126ba97SEli Cohen 		break;
2347e126ba97SEli Cohen 
2348e126ba97SEli Cohen 	case MLX5_DEV_EVENT_PORT_UP:
2349e126ba97SEli Cohen 	case MLX5_DEV_EVENT_PORT_DOWN:
23502788cf3bSNoa Osherovich 	case MLX5_DEV_EVENT_PORT_INITIALIZED:
23514d2f9bbbSJack Morgenstein 		port = (u8)param;
23525ec8c83eSAviv Heller 
23535ec8c83eSAviv Heller 		/* In RoCE, port up/down events are handled in
23545ec8c83eSAviv Heller 		 * mlx5_netdev_event().
23555ec8c83eSAviv Heller 		 */
23565ec8c83eSAviv Heller 		if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) ==
23575ec8c83eSAviv Heller 			IB_LINK_LAYER_ETHERNET)
23585ec8c83eSAviv Heller 			return;
23595ec8c83eSAviv Heller 
23605ec8c83eSAviv Heller 		ibev.event = (event == MLX5_DEV_EVENT_PORT_UP) ?
23615ec8c83eSAviv Heller 			     IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
2362e126ba97SEli Cohen 		break;
2363e126ba97SEli Cohen 
2364e126ba97SEli Cohen 	case MLX5_DEV_EVENT_LID_CHANGE:
2365e126ba97SEli Cohen 		ibev.event = IB_EVENT_LID_CHANGE;
23664d2f9bbbSJack Morgenstein 		port = (u8)param;
2367e126ba97SEli Cohen 		break;
2368e126ba97SEli Cohen 
2369e126ba97SEli Cohen 	case MLX5_DEV_EVENT_PKEY_CHANGE:
2370e126ba97SEli Cohen 		ibev.event = IB_EVENT_PKEY_CHANGE;
23714d2f9bbbSJack Morgenstein 		port = (u8)param;
23727722f47eSHaggai Eran 
23737722f47eSHaggai Eran 		schedule_work(&ibdev->devr.ports[port - 1].pkey_change_work);
2374e126ba97SEli Cohen 		break;
2375e126ba97SEli Cohen 
2376e126ba97SEli Cohen 	case MLX5_DEV_EVENT_GUID_CHANGE:
2377e126ba97SEli Cohen 		ibev.event = IB_EVENT_GID_CHANGE;
23784d2f9bbbSJack Morgenstein 		port = (u8)param;
2379e126ba97SEli Cohen 		break;
2380e126ba97SEli Cohen 
2381e126ba97SEli Cohen 	case MLX5_DEV_EVENT_CLIENT_REREG:
2382e126ba97SEli Cohen 		ibev.event = IB_EVENT_CLIENT_REREGISTER;
23834d2f9bbbSJack Morgenstein 		port = (u8)param;
2384e126ba97SEli Cohen 		break;
2385bdc37924SSaeed Mahameed 	default:
2386bdc37924SSaeed Mahameed 		return;
2387e126ba97SEli Cohen 	}
2388e126ba97SEli Cohen 
2389e126ba97SEli Cohen 	ibev.device	      = &ibdev->ib_dev;
2390e126ba97SEli Cohen 	ibev.element.port_num = port;
2391e126ba97SEli Cohen 
2392a0c84c32SEli Cohen 	if (port < 1 || port > ibdev->num_ports) {
2393a0c84c32SEli Cohen 		mlx5_ib_warn(ibdev, "warning: event on port %d\n", port);
2394a0c84c32SEli Cohen 		return;
2395a0c84c32SEli Cohen 	}
2396a0c84c32SEli Cohen 
2397e126ba97SEli Cohen 	if (ibdev->ib_active)
2398e126ba97SEli Cohen 		ib_dispatch_event(&ibev);
2399e126ba97SEli Cohen }
2400e126ba97SEli Cohen 
2401e126ba97SEli Cohen static void get_ext_port_caps(struct mlx5_ib_dev *dev)
2402e126ba97SEli Cohen {
2403e126ba97SEli Cohen 	int port;
2404e126ba97SEli Cohen 
2405938fe83cSSaeed Mahameed 	for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++)
2406e126ba97SEli Cohen 		mlx5_query_ext_port_caps(dev, port);
2407e126ba97SEli Cohen }
2408e126ba97SEli Cohen 
2409e126ba97SEli Cohen static int get_port_caps(struct mlx5_ib_dev *dev)
2410e126ba97SEli Cohen {
2411e126ba97SEli Cohen 	struct ib_device_attr *dprops = NULL;
2412e126ba97SEli Cohen 	struct ib_port_attr *pprops = NULL;
2413f614fc15SDan Carpenter 	int err = -ENOMEM;
2414e126ba97SEli Cohen 	int port;
24152528e33eSMatan Barak 	struct ib_udata uhw = {.inlen = 0, .outlen = 0};
2416e126ba97SEli Cohen 
2417e126ba97SEli Cohen 	pprops = kmalloc(sizeof(*pprops), GFP_KERNEL);
2418e126ba97SEli Cohen 	if (!pprops)
2419e126ba97SEli Cohen 		goto out;
2420e126ba97SEli Cohen 
2421e126ba97SEli Cohen 	dprops = kmalloc(sizeof(*dprops), GFP_KERNEL);
2422e126ba97SEli Cohen 	if (!dprops)
2423e126ba97SEli Cohen 		goto out;
2424e126ba97SEli Cohen 
24252528e33eSMatan Barak 	err = mlx5_ib_query_device(&dev->ib_dev, dprops, &uhw);
2426e126ba97SEli Cohen 	if (err) {
2427e126ba97SEli Cohen 		mlx5_ib_warn(dev, "query_device failed %d\n", err);
2428e126ba97SEli Cohen 		goto out;
2429e126ba97SEli Cohen 	}
2430e126ba97SEli Cohen 
2431938fe83cSSaeed Mahameed 	for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++) {
2432e126ba97SEli Cohen 		err = mlx5_ib_query_port(&dev->ib_dev, port, pprops);
2433e126ba97SEli Cohen 		if (err) {
2434938fe83cSSaeed Mahameed 			mlx5_ib_warn(dev, "query_port %d failed %d\n",
2435938fe83cSSaeed Mahameed 				     port, err);
2436e126ba97SEli Cohen 			break;
2437e126ba97SEli Cohen 		}
2438938fe83cSSaeed Mahameed 		dev->mdev->port_caps[port - 1].pkey_table_len =
2439938fe83cSSaeed Mahameed 						dprops->max_pkeys;
2440938fe83cSSaeed Mahameed 		dev->mdev->port_caps[port - 1].gid_table_len =
2441938fe83cSSaeed Mahameed 						pprops->gid_tbl_len;
2442e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "pkey_table_len %d, gid_table_len %d\n",
2443e126ba97SEli Cohen 			    dprops->max_pkeys, pprops->gid_tbl_len);
2444e126ba97SEli Cohen 	}
2445e126ba97SEli Cohen 
2446e126ba97SEli Cohen out:
2447e126ba97SEli Cohen 	kfree(pprops);
2448e126ba97SEli Cohen 	kfree(dprops);
2449e126ba97SEli Cohen 
2450e126ba97SEli Cohen 	return err;
2451e126ba97SEli Cohen }
2452e126ba97SEli Cohen 
2453e126ba97SEli Cohen static void destroy_umrc_res(struct mlx5_ib_dev *dev)
2454e126ba97SEli Cohen {
2455e126ba97SEli Cohen 	int err;
2456e126ba97SEli Cohen 
2457e126ba97SEli Cohen 	err = mlx5_mr_cache_cleanup(dev);
2458e126ba97SEli Cohen 	if (err)
2459e126ba97SEli Cohen 		mlx5_ib_warn(dev, "mr cache cleanup failed\n");
2460e126ba97SEli Cohen 
2461e126ba97SEli Cohen 	mlx5_ib_destroy_qp(dev->umrc.qp);
2462add08d76SChristoph Hellwig 	ib_free_cq(dev->umrc.cq);
2463e126ba97SEli Cohen 	ib_dealloc_pd(dev->umrc.pd);
2464e126ba97SEli Cohen }
2465e126ba97SEli Cohen 
2466e126ba97SEli Cohen enum {
2467e126ba97SEli Cohen 	MAX_UMR_WR = 128,
2468e126ba97SEli Cohen };
2469e126ba97SEli Cohen 
2470e126ba97SEli Cohen static int create_umr_res(struct mlx5_ib_dev *dev)
2471e126ba97SEli Cohen {
2472e126ba97SEli Cohen 	struct ib_qp_init_attr *init_attr = NULL;
2473e126ba97SEli Cohen 	struct ib_qp_attr *attr = NULL;
2474e126ba97SEli Cohen 	struct ib_pd *pd;
2475e126ba97SEli Cohen 	struct ib_cq *cq;
2476e126ba97SEli Cohen 	struct ib_qp *qp;
2477e126ba97SEli Cohen 	int ret;
2478e126ba97SEli Cohen 
2479e126ba97SEli Cohen 	attr = kzalloc(sizeof(*attr), GFP_KERNEL);
2480e126ba97SEli Cohen 	init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL);
2481e126ba97SEli Cohen 	if (!attr || !init_attr) {
2482e126ba97SEli Cohen 		ret = -ENOMEM;
2483e126ba97SEli Cohen 		goto error_0;
2484e126ba97SEli Cohen 	}
2485e126ba97SEli Cohen 
2486ed082d36SChristoph Hellwig 	pd = ib_alloc_pd(&dev->ib_dev, 0);
2487e126ba97SEli Cohen 	if (IS_ERR(pd)) {
2488e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create PD for sync UMR QP\n");
2489e126ba97SEli Cohen 		ret = PTR_ERR(pd);
2490e126ba97SEli Cohen 		goto error_0;
2491e126ba97SEli Cohen 	}
2492e126ba97SEli Cohen 
2493add08d76SChristoph Hellwig 	cq = ib_alloc_cq(&dev->ib_dev, NULL, 128, 0, IB_POLL_SOFTIRQ);
2494e126ba97SEli Cohen 	if (IS_ERR(cq)) {
2495e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create CQ for sync UMR QP\n");
2496e126ba97SEli Cohen 		ret = PTR_ERR(cq);
2497e126ba97SEli Cohen 		goto error_2;
2498e126ba97SEli Cohen 	}
2499e126ba97SEli Cohen 
2500e126ba97SEli Cohen 	init_attr->send_cq = cq;
2501e126ba97SEli Cohen 	init_attr->recv_cq = cq;
2502e126ba97SEli Cohen 	init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
2503e126ba97SEli Cohen 	init_attr->cap.max_send_wr = MAX_UMR_WR;
2504e126ba97SEli Cohen 	init_attr->cap.max_send_sge = 1;
2505e126ba97SEli Cohen 	init_attr->qp_type = MLX5_IB_QPT_REG_UMR;
2506e126ba97SEli Cohen 	init_attr->port_num = 1;
2507e126ba97SEli Cohen 	qp = mlx5_ib_create_qp(pd, init_attr, NULL);
2508e126ba97SEli Cohen 	if (IS_ERR(qp)) {
2509e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create sync UMR QP\n");
2510e126ba97SEli Cohen 		ret = PTR_ERR(qp);
2511e126ba97SEli Cohen 		goto error_3;
2512e126ba97SEli Cohen 	}
2513e126ba97SEli Cohen 	qp->device     = &dev->ib_dev;
2514e126ba97SEli Cohen 	qp->real_qp    = qp;
2515e126ba97SEli Cohen 	qp->uobject    = NULL;
2516e126ba97SEli Cohen 	qp->qp_type    = MLX5_IB_QPT_REG_UMR;
2517e126ba97SEli Cohen 
2518e126ba97SEli Cohen 	attr->qp_state = IB_QPS_INIT;
2519e126ba97SEli Cohen 	attr->port_num = 1;
2520e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_PKEY_INDEX |
2521e126ba97SEli Cohen 				IB_QP_PORT, NULL);
2522e126ba97SEli Cohen 	if (ret) {
2523e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify UMR QP\n");
2524e126ba97SEli Cohen 		goto error_4;
2525e126ba97SEli Cohen 	}
2526e126ba97SEli Cohen 
2527e126ba97SEli Cohen 	memset(attr, 0, sizeof(*attr));
2528e126ba97SEli Cohen 	attr->qp_state = IB_QPS_RTR;
2529e126ba97SEli Cohen 	attr->path_mtu = IB_MTU_256;
2530e126ba97SEli Cohen 
2531e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
2532e126ba97SEli Cohen 	if (ret) {
2533e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify umr QP to rtr\n");
2534e126ba97SEli Cohen 		goto error_4;
2535e126ba97SEli Cohen 	}
2536e126ba97SEli Cohen 
2537e126ba97SEli Cohen 	memset(attr, 0, sizeof(*attr));
2538e126ba97SEli Cohen 	attr->qp_state = IB_QPS_RTS;
2539e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
2540e126ba97SEli Cohen 	if (ret) {
2541e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify umr QP to rts\n");
2542e126ba97SEli Cohen 		goto error_4;
2543e126ba97SEli Cohen 	}
2544e126ba97SEli Cohen 
2545e126ba97SEli Cohen 	dev->umrc.qp = qp;
2546e126ba97SEli Cohen 	dev->umrc.cq = cq;
2547e126ba97SEli Cohen 	dev->umrc.pd = pd;
2548e126ba97SEli Cohen 
2549e126ba97SEli Cohen 	sema_init(&dev->umrc.sem, MAX_UMR_WR);
2550e126ba97SEli Cohen 	ret = mlx5_mr_cache_init(dev);
2551e126ba97SEli Cohen 	if (ret) {
2552e126ba97SEli Cohen 		mlx5_ib_warn(dev, "mr cache init failed %d\n", ret);
2553e126ba97SEli Cohen 		goto error_4;
2554e126ba97SEli Cohen 	}
2555e126ba97SEli Cohen 
2556e126ba97SEli Cohen 	kfree(attr);
2557e126ba97SEli Cohen 	kfree(init_attr);
2558e126ba97SEli Cohen 
2559e126ba97SEli Cohen 	return 0;
2560e126ba97SEli Cohen 
2561e126ba97SEli Cohen error_4:
2562e126ba97SEli Cohen 	mlx5_ib_destroy_qp(qp);
2563e126ba97SEli Cohen 
2564e126ba97SEli Cohen error_3:
2565add08d76SChristoph Hellwig 	ib_free_cq(cq);
2566e126ba97SEli Cohen 
2567e126ba97SEli Cohen error_2:
2568e126ba97SEli Cohen 	ib_dealloc_pd(pd);
2569e126ba97SEli Cohen 
2570e126ba97SEli Cohen error_0:
2571e126ba97SEli Cohen 	kfree(attr);
2572e126ba97SEli Cohen 	kfree(init_attr);
2573e126ba97SEli Cohen 	return ret;
2574e126ba97SEli Cohen }
2575e126ba97SEli Cohen 
2576e126ba97SEli Cohen static int create_dev_resources(struct mlx5_ib_resources *devr)
2577e126ba97SEli Cohen {
2578e126ba97SEli Cohen 	struct ib_srq_init_attr attr;
2579e126ba97SEli Cohen 	struct mlx5_ib_dev *dev;
2580bcf4c1eaSMatan Barak 	struct ib_cq_init_attr cq_attr = {.cqe = 1};
25817722f47eSHaggai Eran 	int port;
2582e126ba97SEli Cohen 	int ret = 0;
2583e126ba97SEli Cohen 
2584e126ba97SEli Cohen 	dev = container_of(devr, struct mlx5_ib_dev, devr);
2585e126ba97SEli Cohen 
2586d16e91daSHaggai Eran 	mutex_init(&devr->mutex);
2587d16e91daSHaggai Eran 
2588e126ba97SEli Cohen 	devr->p0 = mlx5_ib_alloc_pd(&dev->ib_dev, NULL, NULL);
2589e126ba97SEli Cohen 	if (IS_ERR(devr->p0)) {
2590e126ba97SEli Cohen 		ret = PTR_ERR(devr->p0);
2591e126ba97SEli Cohen 		goto error0;
2592e126ba97SEli Cohen 	}
2593e126ba97SEli Cohen 	devr->p0->device  = &dev->ib_dev;
2594e126ba97SEli Cohen 	devr->p0->uobject = NULL;
2595e126ba97SEli Cohen 	atomic_set(&devr->p0->usecnt, 0);
2596e126ba97SEli Cohen 
2597bcf4c1eaSMatan Barak 	devr->c0 = mlx5_ib_create_cq(&dev->ib_dev, &cq_attr, NULL, NULL);
2598e126ba97SEli Cohen 	if (IS_ERR(devr->c0)) {
2599e126ba97SEli Cohen 		ret = PTR_ERR(devr->c0);
2600e126ba97SEli Cohen 		goto error1;
2601e126ba97SEli Cohen 	}
2602e126ba97SEli Cohen 	devr->c0->device        = &dev->ib_dev;
2603e126ba97SEli Cohen 	devr->c0->uobject       = NULL;
2604e126ba97SEli Cohen 	devr->c0->comp_handler  = NULL;
2605e126ba97SEli Cohen 	devr->c0->event_handler = NULL;
2606e126ba97SEli Cohen 	devr->c0->cq_context    = NULL;
2607e126ba97SEli Cohen 	atomic_set(&devr->c0->usecnt, 0);
2608e126ba97SEli Cohen 
2609e126ba97SEli Cohen 	devr->x0 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
2610e126ba97SEli Cohen 	if (IS_ERR(devr->x0)) {
2611e126ba97SEli Cohen 		ret = PTR_ERR(devr->x0);
2612e126ba97SEli Cohen 		goto error2;
2613e126ba97SEli Cohen 	}
2614e126ba97SEli Cohen 	devr->x0->device = &dev->ib_dev;
2615e126ba97SEli Cohen 	devr->x0->inode = NULL;
2616e126ba97SEli Cohen 	atomic_set(&devr->x0->usecnt, 0);
2617e126ba97SEli Cohen 	mutex_init(&devr->x0->tgt_qp_mutex);
2618e126ba97SEli Cohen 	INIT_LIST_HEAD(&devr->x0->tgt_qp_list);
2619e126ba97SEli Cohen 
2620e126ba97SEli Cohen 	devr->x1 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
2621e126ba97SEli Cohen 	if (IS_ERR(devr->x1)) {
2622e126ba97SEli Cohen 		ret = PTR_ERR(devr->x1);
2623e126ba97SEli Cohen 		goto error3;
2624e126ba97SEli Cohen 	}
2625e126ba97SEli Cohen 	devr->x1->device = &dev->ib_dev;
2626e126ba97SEli Cohen 	devr->x1->inode = NULL;
2627e126ba97SEli Cohen 	atomic_set(&devr->x1->usecnt, 0);
2628e126ba97SEli Cohen 	mutex_init(&devr->x1->tgt_qp_mutex);
2629e126ba97SEli Cohen 	INIT_LIST_HEAD(&devr->x1->tgt_qp_list);
2630e126ba97SEli Cohen 
2631e126ba97SEli Cohen 	memset(&attr, 0, sizeof(attr));
2632e126ba97SEli Cohen 	attr.attr.max_sge = 1;
2633e126ba97SEli Cohen 	attr.attr.max_wr = 1;
2634e126ba97SEli Cohen 	attr.srq_type = IB_SRQT_XRC;
2635e126ba97SEli Cohen 	attr.ext.xrc.cq = devr->c0;
2636e126ba97SEli Cohen 	attr.ext.xrc.xrcd = devr->x0;
2637e126ba97SEli Cohen 
2638e126ba97SEli Cohen 	devr->s0 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
2639e126ba97SEli Cohen 	if (IS_ERR(devr->s0)) {
2640e126ba97SEli Cohen 		ret = PTR_ERR(devr->s0);
2641e126ba97SEli Cohen 		goto error4;
2642e126ba97SEli Cohen 	}
2643e126ba97SEli Cohen 	devr->s0->device	= &dev->ib_dev;
2644e126ba97SEli Cohen 	devr->s0->pd		= devr->p0;
2645e126ba97SEli Cohen 	devr->s0->uobject       = NULL;
2646e126ba97SEli Cohen 	devr->s0->event_handler = NULL;
2647e126ba97SEli Cohen 	devr->s0->srq_context   = NULL;
2648e126ba97SEli Cohen 	devr->s0->srq_type      = IB_SRQT_XRC;
2649e126ba97SEli Cohen 	devr->s0->ext.xrc.xrcd	= devr->x0;
2650e126ba97SEli Cohen 	devr->s0->ext.xrc.cq	= devr->c0;
2651e126ba97SEli Cohen 	atomic_inc(&devr->s0->ext.xrc.xrcd->usecnt);
2652e126ba97SEli Cohen 	atomic_inc(&devr->s0->ext.xrc.cq->usecnt);
2653e126ba97SEli Cohen 	atomic_inc(&devr->p0->usecnt);
2654e126ba97SEli Cohen 	atomic_set(&devr->s0->usecnt, 0);
2655e126ba97SEli Cohen 
26564aa17b28SHaggai Abramonvsky 	memset(&attr, 0, sizeof(attr));
26574aa17b28SHaggai Abramonvsky 	attr.attr.max_sge = 1;
26584aa17b28SHaggai Abramonvsky 	attr.attr.max_wr = 1;
26594aa17b28SHaggai Abramonvsky 	attr.srq_type = IB_SRQT_BASIC;
26604aa17b28SHaggai Abramonvsky 	devr->s1 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
26614aa17b28SHaggai Abramonvsky 	if (IS_ERR(devr->s1)) {
26624aa17b28SHaggai Abramonvsky 		ret = PTR_ERR(devr->s1);
26634aa17b28SHaggai Abramonvsky 		goto error5;
26644aa17b28SHaggai Abramonvsky 	}
26654aa17b28SHaggai Abramonvsky 	devr->s1->device	= &dev->ib_dev;
26664aa17b28SHaggai Abramonvsky 	devr->s1->pd		= devr->p0;
26674aa17b28SHaggai Abramonvsky 	devr->s1->uobject       = NULL;
26684aa17b28SHaggai Abramonvsky 	devr->s1->event_handler = NULL;
26694aa17b28SHaggai Abramonvsky 	devr->s1->srq_context   = NULL;
26704aa17b28SHaggai Abramonvsky 	devr->s1->srq_type      = IB_SRQT_BASIC;
26714aa17b28SHaggai Abramonvsky 	devr->s1->ext.xrc.cq	= devr->c0;
26724aa17b28SHaggai Abramonvsky 	atomic_inc(&devr->p0->usecnt);
26734aa17b28SHaggai Abramonvsky 	atomic_set(&devr->s0->usecnt, 0);
26744aa17b28SHaggai Abramonvsky 
26757722f47eSHaggai Eran 	for (port = 0; port < ARRAY_SIZE(devr->ports); ++port) {
26767722f47eSHaggai Eran 		INIT_WORK(&devr->ports[port].pkey_change_work,
26777722f47eSHaggai Eran 			  pkey_change_handler);
26787722f47eSHaggai Eran 		devr->ports[port].devr = devr;
26797722f47eSHaggai Eran 	}
26807722f47eSHaggai Eran 
2681e126ba97SEli Cohen 	return 0;
2682e126ba97SEli Cohen 
26834aa17b28SHaggai Abramonvsky error5:
26844aa17b28SHaggai Abramonvsky 	mlx5_ib_destroy_srq(devr->s0);
2685e126ba97SEli Cohen error4:
2686e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x1);
2687e126ba97SEli Cohen error3:
2688e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x0);
2689e126ba97SEli Cohen error2:
2690e126ba97SEli Cohen 	mlx5_ib_destroy_cq(devr->c0);
2691e126ba97SEli Cohen error1:
2692e126ba97SEli Cohen 	mlx5_ib_dealloc_pd(devr->p0);
2693e126ba97SEli Cohen error0:
2694e126ba97SEli Cohen 	return ret;
2695e126ba97SEli Cohen }
2696e126ba97SEli Cohen 
2697e126ba97SEli Cohen static void destroy_dev_resources(struct mlx5_ib_resources *devr)
2698e126ba97SEli Cohen {
26997722f47eSHaggai Eran 	struct mlx5_ib_dev *dev =
27007722f47eSHaggai Eran 		container_of(devr, struct mlx5_ib_dev, devr);
27017722f47eSHaggai Eran 	int port;
27027722f47eSHaggai Eran 
27034aa17b28SHaggai Abramonvsky 	mlx5_ib_destroy_srq(devr->s1);
2704e126ba97SEli Cohen 	mlx5_ib_destroy_srq(devr->s0);
2705e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x0);
2706e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x1);
2707e126ba97SEli Cohen 	mlx5_ib_destroy_cq(devr->c0);
2708e126ba97SEli Cohen 	mlx5_ib_dealloc_pd(devr->p0);
27097722f47eSHaggai Eran 
27107722f47eSHaggai Eran 	/* Make sure no change P_Key work items are still executing */
27117722f47eSHaggai Eran 	for (port = 0; port < dev->num_ports; ++port)
27127722f47eSHaggai Eran 		cancel_work_sync(&devr->ports[port].pkey_change_work);
2713e126ba97SEli Cohen }
2714e126ba97SEli Cohen 
2715e53505a8SAchiad Shochat static u32 get_core_cap_flags(struct ib_device *ibdev)
2716e53505a8SAchiad Shochat {
2717e53505a8SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
2718e53505a8SAchiad Shochat 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, 1);
2719e53505a8SAchiad Shochat 	u8 l3_type_cap = MLX5_CAP_ROCE(dev->mdev, l3_type);
2720e53505a8SAchiad Shochat 	u8 roce_version_cap = MLX5_CAP_ROCE(dev->mdev, roce_version);
2721e53505a8SAchiad Shochat 	u32 ret = 0;
2722e53505a8SAchiad Shochat 
2723e53505a8SAchiad Shochat 	if (ll == IB_LINK_LAYER_INFINIBAND)
2724e53505a8SAchiad Shochat 		return RDMA_CORE_PORT_IBA_IB;
2725e53505a8SAchiad Shochat 
2726e53505a8SAchiad Shochat 	if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV4_CAP))
2727e53505a8SAchiad Shochat 		return 0;
2728e53505a8SAchiad Shochat 
2729e53505a8SAchiad Shochat 	if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV6_CAP))
2730e53505a8SAchiad Shochat 		return 0;
2731e53505a8SAchiad Shochat 
2732e53505a8SAchiad Shochat 	if (roce_version_cap & MLX5_ROCE_VERSION_1_CAP)
2733e53505a8SAchiad Shochat 		ret |= RDMA_CORE_PORT_IBA_ROCE;
2734e53505a8SAchiad Shochat 
2735e53505a8SAchiad Shochat 	if (roce_version_cap & MLX5_ROCE_VERSION_2_CAP)
2736e53505a8SAchiad Shochat 		ret |= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
2737e53505a8SAchiad Shochat 
2738e53505a8SAchiad Shochat 	return ret;
2739e53505a8SAchiad Shochat }
2740e53505a8SAchiad Shochat 
27417738613eSIra Weiny static int mlx5_port_immutable(struct ib_device *ibdev, u8 port_num,
27427738613eSIra Weiny 			       struct ib_port_immutable *immutable)
27437738613eSIra Weiny {
27447738613eSIra Weiny 	struct ib_port_attr attr;
27457738613eSIra Weiny 	int err;
27467738613eSIra Weiny 
27477738613eSIra Weiny 	err = mlx5_ib_query_port(ibdev, port_num, &attr);
27487738613eSIra Weiny 	if (err)
27497738613eSIra Weiny 		return err;
27507738613eSIra Weiny 
27517738613eSIra Weiny 	immutable->pkey_tbl_len = attr.pkey_tbl_len;
27527738613eSIra Weiny 	immutable->gid_tbl_len = attr.gid_tbl_len;
2753e53505a8SAchiad Shochat 	immutable->core_cap_flags = get_core_cap_flags(ibdev);
2754337877a4SIra Weiny 	immutable->max_mad_size = IB_MGMT_MAD_SIZE;
27557738613eSIra Weiny 
27567738613eSIra Weiny 	return 0;
27577738613eSIra Weiny }
27587738613eSIra Weiny 
2759c7342823SIra Weiny static void get_dev_fw_str(struct ib_device *ibdev, char *str,
2760c7342823SIra Weiny 			   size_t str_len)
2761c7342823SIra Weiny {
2762c7342823SIra Weiny 	struct mlx5_ib_dev *dev =
2763c7342823SIra Weiny 		container_of(ibdev, struct mlx5_ib_dev, ib_dev);
2764c7342823SIra Weiny 	snprintf(str, str_len, "%d.%d.%04d", fw_rev_maj(dev->mdev),
2765c7342823SIra Weiny 		       fw_rev_min(dev->mdev), fw_rev_sub(dev->mdev));
2766c7342823SIra Weiny }
2767c7342823SIra Weiny 
27689ef9c640SAviv Heller static int mlx5_roce_lag_init(struct mlx5_ib_dev *dev)
27699ef9c640SAviv Heller {
27709ef9c640SAviv Heller 	struct mlx5_core_dev *mdev = dev->mdev;
27719ef9c640SAviv Heller 	struct mlx5_flow_namespace *ns = mlx5_get_flow_namespace(mdev,
27729ef9c640SAviv Heller 								 MLX5_FLOW_NAMESPACE_LAG);
27739ef9c640SAviv Heller 	struct mlx5_flow_table *ft;
27749ef9c640SAviv Heller 	int err;
27759ef9c640SAviv Heller 
27769ef9c640SAviv Heller 	if (!ns || !mlx5_lag_is_active(mdev))
27779ef9c640SAviv Heller 		return 0;
27789ef9c640SAviv Heller 
27799ef9c640SAviv Heller 	err = mlx5_cmd_create_vport_lag(mdev);
27809ef9c640SAviv Heller 	if (err)
27819ef9c640SAviv Heller 		return err;
27829ef9c640SAviv Heller 
27839ef9c640SAviv Heller 	ft = mlx5_create_lag_demux_flow_table(ns, 0, 0);
27849ef9c640SAviv Heller 	if (IS_ERR(ft)) {
27859ef9c640SAviv Heller 		err = PTR_ERR(ft);
27869ef9c640SAviv Heller 		goto err_destroy_vport_lag;
27879ef9c640SAviv Heller 	}
27889ef9c640SAviv Heller 
27899ef9c640SAviv Heller 	dev->flow_db.lag_demux_ft = ft;
27909ef9c640SAviv Heller 	return 0;
27919ef9c640SAviv Heller 
27929ef9c640SAviv Heller err_destroy_vport_lag:
27939ef9c640SAviv Heller 	mlx5_cmd_destroy_vport_lag(mdev);
27949ef9c640SAviv Heller 	return err;
27959ef9c640SAviv Heller }
27969ef9c640SAviv Heller 
27979ef9c640SAviv Heller static void mlx5_roce_lag_cleanup(struct mlx5_ib_dev *dev)
27989ef9c640SAviv Heller {
27999ef9c640SAviv Heller 	struct mlx5_core_dev *mdev = dev->mdev;
28009ef9c640SAviv Heller 
28019ef9c640SAviv Heller 	if (dev->flow_db.lag_demux_ft) {
28029ef9c640SAviv Heller 		mlx5_destroy_flow_table(dev->flow_db.lag_demux_ft);
28039ef9c640SAviv Heller 		dev->flow_db.lag_demux_ft = NULL;
28049ef9c640SAviv Heller 
28059ef9c640SAviv Heller 		mlx5_cmd_destroy_vport_lag(mdev);
28069ef9c640SAviv Heller 	}
28079ef9c640SAviv Heller }
28089ef9c640SAviv Heller 
28095ec8c83eSAviv Heller static void mlx5_remove_roce_notifier(struct mlx5_ib_dev *dev)
28105ec8c83eSAviv Heller {
28115ec8c83eSAviv Heller 	if (dev->roce.nb.notifier_call) {
28125ec8c83eSAviv Heller 		unregister_netdevice_notifier(&dev->roce.nb);
28135ec8c83eSAviv Heller 		dev->roce.nb.notifier_call = NULL;
28145ec8c83eSAviv Heller 	}
28155ec8c83eSAviv Heller }
28165ec8c83eSAviv Heller 
2817fc24fc5eSAchiad Shochat static int mlx5_enable_roce(struct mlx5_ib_dev *dev)
2818fc24fc5eSAchiad Shochat {
2819e53505a8SAchiad Shochat 	int err;
2820e53505a8SAchiad Shochat 
2821fc24fc5eSAchiad Shochat 	dev->roce.nb.notifier_call = mlx5_netdev_event;
2822e53505a8SAchiad Shochat 	err = register_netdevice_notifier(&dev->roce.nb);
28235ec8c83eSAviv Heller 	if (err) {
28245ec8c83eSAviv Heller 		dev->roce.nb.notifier_call = NULL;
2825e53505a8SAchiad Shochat 		return err;
28265ec8c83eSAviv Heller 	}
2827e53505a8SAchiad Shochat 
2828e53505a8SAchiad Shochat 	err = mlx5_nic_vport_enable_roce(dev->mdev);
2829e53505a8SAchiad Shochat 	if (err)
2830e53505a8SAchiad Shochat 		goto err_unregister_netdevice_notifier;
2831e53505a8SAchiad Shochat 
28329ef9c640SAviv Heller 	err = mlx5_roce_lag_init(dev);
28339ef9c640SAviv Heller 	if (err)
28349ef9c640SAviv Heller 		goto err_disable_roce;
28359ef9c640SAviv Heller 
2836e53505a8SAchiad Shochat 	return 0;
2837e53505a8SAchiad Shochat 
28389ef9c640SAviv Heller err_disable_roce:
28399ef9c640SAviv Heller 	mlx5_nic_vport_disable_roce(dev->mdev);
28409ef9c640SAviv Heller 
2841e53505a8SAchiad Shochat err_unregister_netdevice_notifier:
28425ec8c83eSAviv Heller 	mlx5_remove_roce_notifier(dev);
2843e53505a8SAchiad Shochat 	return err;
2844fc24fc5eSAchiad Shochat }
2845fc24fc5eSAchiad Shochat 
2846fc24fc5eSAchiad Shochat static void mlx5_disable_roce(struct mlx5_ib_dev *dev)
2847fc24fc5eSAchiad Shochat {
28489ef9c640SAviv Heller 	mlx5_roce_lag_cleanup(dev);
2849e53505a8SAchiad Shochat 	mlx5_nic_vport_disable_roce(dev->mdev);
2850fc24fc5eSAchiad Shochat }
2851fc24fc5eSAchiad Shochat 
28520837e86aSMark Bloch static void mlx5_ib_dealloc_q_counters(struct mlx5_ib_dev *dev)
28530837e86aSMark Bloch {
28540837e86aSMark Bloch 	unsigned int i;
28550837e86aSMark Bloch 
28560837e86aSMark Bloch 	for (i = 0; i < dev->num_ports; i++)
28570837e86aSMark Bloch 		mlx5_core_dealloc_q_counter(dev->mdev,
28580837e86aSMark Bloch 					    dev->port[i].q_cnt_id);
28590837e86aSMark Bloch }
28600837e86aSMark Bloch 
28610837e86aSMark Bloch static int mlx5_ib_alloc_q_counters(struct mlx5_ib_dev *dev)
28620837e86aSMark Bloch {
28630837e86aSMark Bloch 	int i;
28640837e86aSMark Bloch 	int ret;
28650837e86aSMark Bloch 
28660837e86aSMark Bloch 	for (i = 0; i < dev->num_ports; i++) {
28670837e86aSMark Bloch 		ret = mlx5_core_alloc_q_counter(dev->mdev,
28680837e86aSMark Bloch 						&dev->port[i].q_cnt_id);
28690837e86aSMark Bloch 		if (ret) {
28700837e86aSMark Bloch 			mlx5_ib_warn(dev,
28710837e86aSMark Bloch 				     "couldn't allocate queue counter for port %d, err %d\n",
28720837e86aSMark Bloch 				     i + 1, ret);
28730837e86aSMark Bloch 			goto dealloc_counters;
28740837e86aSMark Bloch 		}
28750837e86aSMark Bloch 	}
28760837e86aSMark Bloch 
28770837e86aSMark Bloch 	return 0;
28780837e86aSMark Bloch 
28790837e86aSMark Bloch dealloc_counters:
28800837e86aSMark Bloch 	while (--i >= 0)
28810837e86aSMark Bloch 		mlx5_core_dealloc_q_counter(dev->mdev,
28820837e86aSMark Bloch 					    dev->port[i].q_cnt_id);
28830837e86aSMark Bloch 
28840837e86aSMark Bloch 	return ret;
28850837e86aSMark Bloch }
28860837e86aSMark Bloch 
288761961500SWei Yongjun static const char * const names[] = {
28880ad17a8fSMark Bloch 	"rx_write_requests",
28890ad17a8fSMark Bloch 	"rx_read_requests",
28900ad17a8fSMark Bloch 	"rx_atomic_requests",
28910ad17a8fSMark Bloch 	"out_of_buffer",
28920ad17a8fSMark Bloch 	"out_of_sequence",
28930ad17a8fSMark Bloch 	"duplicate_request",
28940ad17a8fSMark Bloch 	"rnr_nak_retry_err",
28950ad17a8fSMark Bloch 	"packet_seq_err",
28960ad17a8fSMark Bloch 	"implied_nak_seq_err",
28970ad17a8fSMark Bloch 	"local_ack_timeout_err",
28980ad17a8fSMark Bloch };
28990ad17a8fSMark Bloch 
29000ad17a8fSMark Bloch static const size_t stats_offsets[] = {
29010ad17a8fSMark Bloch 	MLX5_BYTE_OFF(query_q_counter_out, rx_write_requests),
29020ad17a8fSMark Bloch 	MLX5_BYTE_OFF(query_q_counter_out, rx_read_requests),
29030ad17a8fSMark Bloch 	MLX5_BYTE_OFF(query_q_counter_out, rx_atomic_requests),
29040ad17a8fSMark Bloch 	MLX5_BYTE_OFF(query_q_counter_out, out_of_buffer),
29050ad17a8fSMark Bloch 	MLX5_BYTE_OFF(query_q_counter_out, out_of_sequence),
29060ad17a8fSMark Bloch 	MLX5_BYTE_OFF(query_q_counter_out, duplicate_request),
29070ad17a8fSMark Bloch 	MLX5_BYTE_OFF(query_q_counter_out, rnr_nak_retry_err),
29080ad17a8fSMark Bloch 	MLX5_BYTE_OFF(query_q_counter_out, packet_seq_err),
29090ad17a8fSMark Bloch 	MLX5_BYTE_OFF(query_q_counter_out, implied_nak_seq_err),
29100ad17a8fSMark Bloch 	MLX5_BYTE_OFF(query_q_counter_out, local_ack_timeout_err),
29110ad17a8fSMark Bloch };
29120ad17a8fSMark Bloch 
29130ad17a8fSMark Bloch static struct rdma_hw_stats *mlx5_ib_alloc_hw_stats(struct ib_device *ibdev,
29140ad17a8fSMark Bloch 						    u8 port_num)
29150ad17a8fSMark Bloch {
29160ad17a8fSMark Bloch 	BUILD_BUG_ON(ARRAY_SIZE(names) != ARRAY_SIZE(stats_offsets));
29170ad17a8fSMark Bloch 
29180ad17a8fSMark Bloch 	/* We support only per port stats */
29190ad17a8fSMark Bloch 	if (port_num == 0)
29200ad17a8fSMark Bloch 		return NULL;
29210ad17a8fSMark Bloch 
29220ad17a8fSMark Bloch 	return rdma_alloc_hw_stats_struct(names, ARRAY_SIZE(names),
29230ad17a8fSMark Bloch 					  RDMA_HW_STATS_DEFAULT_LIFESPAN);
29240ad17a8fSMark Bloch }
29250ad17a8fSMark Bloch 
29260ad17a8fSMark Bloch static int mlx5_ib_get_hw_stats(struct ib_device *ibdev,
29270ad17a8fSMark Bloch 				struct rdma_hw_stats *stats,
29280ad17a8fSMark Bloch 				u8 port, int index)
29290ad17a8fSMark Bloch {
29300ad17a8fSMark Bloch 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
29310ad17a8fSMark Bloch 	int outlen = MLX5_ST_SZ_BYTES(query_q_counter_out);
29320ad17a8fSMark Bloch 	void *out;
29330ad17a8fSMark Bloch 	__be32 val;
29340ad17a8fSMark Bloch 	int ret;
29350ad17a8fSMark Bloch 	int i;
29360ad17a8fSMark Bloch 
29370ad17a8fSMark Bloch 	if (!port || !stats)
29380ad17a8fSMark Bloch 		return -ENOSYS;
29390ad17a8fSMark Bloch 
29400ad17a8fSMark Bloch 	out = mlx5_vzalloc(outlen);
29410ad17a8fSMark Bloch 	if (!out)
29420ad17a8fSMark Bloch 		return -ENOMEM;
29430ad17a8fSMark Bloch 
29440ad17a8fSMark Bloch 	ret = mlx5_core_query_q_counter(dev->mdev,
29450ad17a8fSMark Bloch 					dev->port[port - 1].q_cnt_id, 0,
29460ad17a8fSMark Bloch 					out, outlen);
29470ad17a8fSMark Bloch 	if (ret)
29480ad17a8fSMark Bloch 		goto free;
29490ad17a8fSMark Bloch 
29500ad17a8fSMark Bloch 	for (i = 0; i < ARRAY_SIZE(names); i++) {
29510ad17a8fSMark Bloch 		val = *(__be32 *)(out + stats_offsets[i]);
29520ad17a8fSMark Bloch 		stats->value[i] = (u64)be32_to_cpu(val);
29530ad17a8fSMark Bloch 	}
29540ad17a8fSMark Bloch free:
29550ad17a8fSMark Bloch 	kvfree(out);
29560ad17a8fSMark Bloch 	return ARRAY_SIZE(names);
29570ad17a8fSMark Bloch }
29580ad17a8fSMark Bloch 
29599603b61dSJack Morgenstein static void *mlx5_ib_add(struct mlx5_core_dev *mdev)
2960e126ba97SEli Cohen {
2961e126ba97SEli Cohen 	struct mlx5_ib_dev *dev;
2962ebd61f68SAchiad Shochat 	enum rdma_link_layer ll;
2963ebd61f68SAchiad Shochat 	int port_type_cap;
29644babcf97SAviv Heller 	const char *name;
2965e126ba97SEli Cohen 	int err;
2966e126ba97SEli Cohen 	int i;
2967e126ba97SEli Cohen 
2968ebd61f68SAchiad Shochat 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
2969ebd61f68SAchiad Shochat 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
2970ebd61f68SAchiad Shochat 
2971e53505a8SAchiad Shochat 	if ((ll == IB_LINK_LAYER_ETHERNET) && !MLX5_CAP_GEN(mdev, roce))
2972647241eaSMajd Dibbiny 		return NULL;
2973647241eaSMajd Dibbiny 
2974e126ba97SEli Cohen 	printk_once(KERN_INFO "%s", mlx5_version);
2975e126ba97SEli Cohen 
2976e126ba97SEli Cohen 	dev = (struct mlx5_ib_dev *)ib_alloc_device(sizeof(*dev));
2977e126ba97SEli Cohen 	if (!dev)
29789603b61dSJack Morgenstein 		return NULL;
2979e126ba97SEli Cohen 
29809603b61dSJack Morgenstein 	dev->mdev = mdev;
2981e126ba97SEli Cohen 
29820837e86aSMark Bloch 	dev->port = kcalloc(MLX5_CAP_GEN(mdev, num_ports), sizeof(*dev->port),
29830837e86aSMark Bloch 			    GFP_KERNEL);
29840837e86aSMark Bloch 	if (!dev->port)
29850837e86aSMark Bloch 		goto err_dealloc;
29860837e86aSMark Bloch 
2987fc24fc5eSAchiad Shochat 	rwlock_init(&dev->roce.netdev_lock);
2988e126ba97SEli Cohen 	err = get_port_caps(dev);
2989e126ba97SEli Cohen 	if (err)
29900837e86aSMark Bloch 		goto err_free_port;
2991e126ba97SEli Cohen 
29921b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(dev))
2993e126ba97SEli Cohen 		get_ext_port_caps(dev);
2994e126ba97SEli Cohen 
2995e126ba97SEli Cohen 	MLX5_INIT_DOORBELL_LOCK(&dev->uar_lock);
2996e126ba97SEli Cohen 
29974babcf97SAviv Heller 	if (!mlx5_lag_is_active(mdev))
29984babcf97SAviv Heller 		name = "mlx5_%d";
29994babcf97SAviv Heller 	else
30004babcf97SAviv Heller 		name = "mlx5_bond_%d";
30014babcf97SAviv Heller 
30024babcf97SAviv Heller 	strlcpy(dev->ib_dev.name, name, IB_DEVICE_NAME_MAX);
3003e126ba97SEli Cohen 	dev->ib_dev.owner		= THIS_MODULE;
3004e126ba97SEli Cohen 	dev->ib_dev.node_type		= RDMA_NODE_IB_CA;
3005c6790aa9SSagi Grimberg 	dev->ib_dev.local_dma_lkey	= 0 /* not supported for now */;
3006938fe83cSSaeed Mahameed 	dev->num_ports		= MLX5_CAP_GEN(mdev, num_ports);
3007e126ba97SEli Cohen 	dev->ib_dev.phys_port_cnt     = dev->num_ports;
3008233d05d2SSaeed Mahameed 	dev->ib_dev.num_comp_vectors    =
3009233d05d2SSaeed Mahameed 		dev->mdev->priv.eq_table.num_comp_vectors;
3010e126ba97SEli Cohen 	dev->ib_dev.dma_device	= &mdev->pdev->dev;
3011e126ba97SEli Cohen 
3012e126ba97SEli Cohen 	dev->ib_dev.uverbs_abi_ver	= MLX5_IB_UVERBS_ABI_VERSION;
3013e126ba97SEli Cohen 	dev->ib_dev.uverbs_cmd_mask	=
3014e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_GET_CONTEXT)		|
3015e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_DEVICE)	|
3016e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_PORT)		|
3017e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_ALLOC_PD)		|
3018e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DEALLOC_PD)		|
3019e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_REG_MR)		|
302056e11d62SNoa Osherovich 		(1ull << IB_USER_VERBS_CMD_REREG_MR)		|
3021e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DEREG_MR)		|
3022e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL)	|
3023e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_CQ)		|
3024e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_RESIZE_CQ)		|
3025e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_CQ)		|
3026e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_QP)		|
3027e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_MODIFY_QP)		|
3028e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_QP)		|
3029e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_QP)		|
3030e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_ATTACH_MCAST)	|
3031e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DETACH_MCAST)	|
3032e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_SRQ)		|
3033e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_MODIFY_SRQ)		|
3034e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_SRQ)		|
3035e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_SRQ)		|
3036e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_XSRQ)		|
3037e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_OPEN_QP);
30381707cb4aSHaggai Eran 	dev->ib_dev.uverbs_ex_cmd_mask =
3039d4584ddfSMatan Barak 		(1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE)	|
3040d4584ddfSMatan Barak 		(1ull << IB_USER_VERBS_EX_CMD_CREATE_CQ)	|
3041d4584ddfSMatan Barak 		(1ull << IB_USER_VERBS_EX_CMD_CREATE_QP);
3042e126ba97SEli Cohen 
3043e126ba97SEli Cohen 	dev->ib_dev.query_device	= mlx5_ib_query_device;
3044e126ba97SEli Cohen 	dev->ib_dev.query_port		= mlx5_ib_query_port;
3045ebd61f68SAchiad Shochat 	dev->ib_dev.get_link_layer	= mlx5_ib_port_link_layer;
3046fc24fc5eSAchiad Shochat 	if (ll == IB_LINK_LAYER_ETHERNET)
3047fc24fc5eSAchiad Shochat 		dev->ib_dev.get_netdev	= mlx5_ib_get_netdev;
3048e126ba97SEli Cohen 	dev->ib_dev.query_gid		= mlx5_ib_query_gid;
30493cca2606SAchiad Shochat 	dev->ib_dev.add_gid		= mlx5_ib_add_gid;
30503cca2606SAchiad Shochat 	dev->ib_dev.del_gid		= mlx5_ib_del_gid;
3051e126ba97SEli Cohen 	dev->ib_dev.query_pkey		= mlx5_ib_query_pkey;
3052e126ba97SEli Cohen 	dev->ib_dev.modify_device	= mlx5_ib_modify_device;
3053e126ba97SEli Cohen 	dev->ib_dev.modify_port		= mlx5_ib_modify_port;
3054e126ba97SEli Cohen 	dev->ib_dev.alloc_ucontext	= mlx5_ib_alloc_ucontext;
3055e126ba97SEli Cohen 	dev->ib_dev.dealloc_ucontext	= mlx5_ib_dealloc_ucontext;
3056e126ba97SEli Cohen 	dev->ib_dev.mmap		= mlx5_ib_mmap;
3057e126ba97SEli Cohen 	dev->ib_dev.alloc_pd		= mlx5_ib_alloc_pd;
3058e126ba97SEli Cohen 	dev->ib_dev.dealloc_pd		= mlx5_ib_dealloc_pd;
3059e126ba97SEli Cohen 	dev->ib_dev.create_ah		= mlx5_ib_create_ah;
3060e126ba97SEli Cohen 	dev->ib_dev.query_ah		= mlx5_ib_query_ah;
3061e126ba97SEli Cohen 	dev->ib_dev.destroy_ah		= mlx5_ib_destroy_ah;
3062e126ba97SEli Cohen 	dev->ib_dev.create_srq		= mlx5_ib_create_srq;
3063e126ba97SEli Cohen 	dev->ib_dev.modify_srq		= mlx5_ib_modify_srq;
3064e126ba97SEli Cohen 	dev->ib_dev.query_srq		= mlx5_ib_query_srq;
3065e126ba97SEli Cohen 	dev->ib_dev.destroy_srq		= mlx5_ib_destroy_srq;
3066e126ba97SEli Cohen 	dev->ib_dev.post_srq_recv	= mlx5_ib_post_srq_recv;
3067e126ba97SEli Cohen 	dev->ib_dev.create_qp		= mlx5_ib_create_qp;
3068e126ba97SEli Cohen 	dev->ib_dev.modify_qp		= mlx5_ib_modify_qp;
3069e126ba97SEli Cohen 	dev->ib_dev.query_qp		= mlx5_ib_query_qp;
3070e126ba97SEli Cohen 	dev->ib_dev.destroy_qp		= mlx5_ib_destroy_qp;
3071e126ba97SEli Cohen 	dev->ib_dev.post_send		= mlx5_ib_post_send;
3072e126ba97SEli Cohen 	dev->ib_dev.post_recv		= mlx5_ib_post_recv;
3073e126ba97SEli Cohen 	dev->ib_dev.create_cq		= mlx5_ib_create_cq;
3074e126ba97SEli Cohen 	dev->ib_dev.modify_cq		= mlx5_ib_modify_cq;
3075e126ba97SEli Cohen 	dev->ib_dev.resize_cq		= mlx5_ib_resize_cq;
3076e126ba97SEli Cohen 	dev->ib_dev.destroy_cq		= mlx5_ib_destroy_cq;
3077e126ba97SEli Cohen 	dev->ib_dev.poll_cq		= mlx5_ib_poll_cq;
3078e126ba97SEli Cohen 	dev->ib_dev.req_notify_cq	= mlx5_ib_arm_cq;
3079e126ba97SEli Cohen 	dev->ib_dev.get_dma_mr		= mlx5_ib_get_dma_mr;
3080e126ba97SEli Cohen 	dev->ib_dev.reg_user_mr		= mlx5_ib_reg_user_mr;
308156e11d62SNoa Osherovich 	dev->ib_dev.rereg_user_mr	= mlx5_ib_rereg_user_mr;
3082e126ba97SEli Cohen 	dev->ib_dev.dereg_mr		= mlx5_ib_dereg_mr;
3083e126ba97SEli Cohen 	dev->ib_dev.attach_mcast	= mlx5_ib_mcg_attach;
3084e126ba97SEli Cohen 	dev->ib_dev.detach_mcast	= mlx5_ib_mcg_detach;
3085e126ba97SEli Cohen 	dev->ib_dev.process_mad		= mlx5_ib_process_mad;
30869bee178bSSagi Grimberg 	dev->ib_dev.alloc_mr		= mlx5_ib_alloc_mr;
30878a187ee5SSagi Grimberg 	dev->ib_dev.map_mr_sg		= mlx5_ib_map_mr_sg;
3088d5436ba0SSagi Grimberg 	dev->ib_dev.check_mr_status	= mlx5_ib_check_mr_status;
30897738613eSIra Weiny 	dev->ib_dev.get_port_immutable  = mlx5_port_immutable;
3090c7342823SIra Weiny 	dev->ib_dev.get_dev_fw_str      = get_dev_fw_str;
3091eff901d3SEli Cohen 	if (mlx5_core_is_pf(mdev)) {
3092eff901d3SEli Cohen 		dev->ib_dev.get_vf_config	= mlx5_ib_get_vf_config;
3093eff901d3SEli Cohen 		dev->ib_dev.set_vf_link_state	= mlx5_ib_set_vf_link_state;
3094eff901d3SEli Cohen 		dev->ib_dev.get_vf_stats	= mlx5_ib_get_vf_stats;
3095eff901d3SEli Cohen 		dev->ib_dev.set_vf_guid		= mlx5_ib_set_vf_guid;
3096eff901d3SEli Cohen 	}
3097e126ba97SEli Cohen 
30987c2344c3SMaor Gottlieb 	dev->ib_dev.disassociate_ucontext = mlx5_ib_disassociate_ucontext;
30997c2344c3SMaor Gottlieb 
3100938fe83cSSaeed Mahameed 	mlx5_ib_internal_fill_odp_caps(dev);
31018cdd312cSHaggai Eran 
3102d2370e0aSMatan Barak 	if (MLX5_CAP_GEN(mdev, imaicl)) {
3103d2370e0aSMatan Barak 		dev->ib_dev.alloc_mw		= mlx5_ib_alloc_mw;
3104d2370e0aSMatan Barak 		dev->ib_dev.dealloc_mw		= mlx5_ib_dealloc_mw;
3105d2370e0aSMatan Barak 		dev->ib_dev.uverbs_cmd_mask |=
3106d2370e0aSMatan Barak 			(1ull << IB_USER_VERBS_CMD_ALLOC_MW)	|
3107d2370e0aSMatan Barak 			(1ull << IB_USER_VERBS_CMD_DEALLOC_MW);
3108d2370e0aSMatan Barak 	}
3109d2370e0aSMatan Barak 
31100ad17a8fSMark Bloch 	if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt) &&
31110ad17a8fSMark Bloch 	    MLX5_CAP_GEN(dev->mdev, retransmission_q_counters)) {
31120ad17a8fSMark Bloch 		dev->ib_dev.get_hw_stats	= mlx5_ib_get_hw_stats;
31130ad17a8fSMark Bloch 		dev->ib_dev.alloc_hw_stats	= mlx5_ib_alloc_hw_stats;
31140ad17a8fSMark Bloch 	}
31150ad17a8fSMark Bloch 
3116938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, xrc)) {
3117e126ba97SEli Cohen 		dev->ib_dev.alloc_xrcd = mlx5_ib_alloc_xrcd;
3118e126ba97SEli Cohen 		dev->ib_dev.dealloc_xrcd = mlx5_ib_dealloc_xrcd;
3119e126ba97SEli Cohen 		dev->ib_dev.uverbs_cmd_mask |=
3120e126ba97SEli Cohen 			(1ull << IB_USER_VERBS_CMD_OPEN_XRCD) |
3121e126ba97SEli Cohen 			(1ull << IB_USER_VERBS_CMD_CLOSE_XRCD);
3122e126ba97SEli Cohen 	}
3123e126ba97SEli Cohen 
3124048ccca8SLinus Torvalds 	if (mlx5_ib_port_link_layer(&dev->ib_dev, 1) ==
3125038d2ef8SMaor Gottlieb 	    IB_LINK_LAYER_ETHERNET) {
3126038d2ef8SMaor Gottlieb 		dev->ib_dev.create_flow	= mlx5_ib_create_flow;
3127038d2ef8SMaor Gottlieb 		dev->ib_dev.destroy_flow = mlx5_ib_destroy_flow;
312879b20a6cSYishai Hadas 		dev->ib_dev.create_wq	 = mlx5_ib_create_wq;
312979b20a6cSYishai Hadas 		dev->ib_dev.modify_wq	 = mlx5_ib_modify_wq;
313079b20a6cSYishai Hadas 		dev->ib_dev.destroy_wq	 = mlx5_ib_destroy_wq;
3131c5f90929SYishai Hadas 		dev->ib_dev.create_rwq_ind_table = mlx5_ib_create_rwq_ind_table;
3132c5f90929SYishai Hadas 		dev->ib_dev.destroy_rwq_ind_table = mlx5_ib_destroy_rwq_ind_table;
3133038d2ef8SMaor Gottlieb 		dev->ib_dev.uverbs_ex_cmd_mask |=
3134038d2ef8SMaor Gottlieb 			(1ull << IB_USER_VERBS_EX_CMD_CREATE_FLOW) |
313579b20a6cSYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_DESTROY_FLOW) |
313679b20a6cSYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_CREATE_WQ) |
313779b20a6cSYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_MODIFY_WQ) |
3138c5f90929SYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_DESTROY_WQ) |
3139c5f90929SYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_CREATE_RWQ_IND_TBL) |
3140c5f90929SYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_DESTROY_RWQ_IND_TBL);
3141038d2ef8SMaor Gottlieb 	}
3142e126ba97SEli Cohen 	err = init_node_data(dev);
3143e126ba97SEli Cohen 	if (err)
3144233d05d2SSaeed Mahameed 		goto err_dealloc;
3145e126ba97SEli Cohen 
3146038d2ef8SMaor Gottlieb 	mutex_init(&dev->flow_db.lock);
3147e126ba97SEli Cohen 	mutex_init(&dev->cap_mask_mutex);
314889ea94a7SMaor Gottlieb 	INIT_LIST_HEAD(&dev->qp_list);
314989ea94a7SMaor Gottlieb 	spin_lock_init(&dev->reset_flow_resource_lock);
3150e126ba97SEli Cohen 
3151fc24fc5eSAchiad Shochat 	if (ll == IB_LINK_LAYER_ETHERNET) {
3152fc24fc5eSAchiad Shochat 		err = mlx5_enable_roce(dev);
3153e126ba97SEli Cohen 		if (err)
3154233d05d2SSaeed Mahameed 			goto err_dealloc;
3155fc24fc5eSAchiad Shochat 	}
3156fc24fc5eSAchiad Shochat 
3157fc24fc5eSAchiad Shochat 	err = create_dev_resources(&dev->devr);
3158fc24fc5eSAchiad Shochat 	if (err)
3159fc24fc5eSAchiad Shochat 		goto err_disable_roce;
3160e126ba97SEli Cohen 
31616aec21f6SHaggai Eran 	err = mlx5_ib_odp_init_one(dev);
3162281d1a92SWei Yongjun 	if (err)
3163e126ba97SEli Cohen 		goto err_rsrc;
3164e126ba97SEli Cohen 
31650837e86aSMark Bloch 	err = mlx5_ib_alloc_q_counters(dev);
31666aec21f6SHaggai Eran 	if (err)
31676aec21f6SHaggai Eran 		goto err_odp;
31686aec21f6SHaggai Eran 
31690837e86aSMark Bloch 	err = ib_register_device(&dev->ib_dev, NULL);
31700837e86aSMark Bloch 	if (err)
31710837e86aSMark Bloch 		goto err_q_cnt;
31720837e86aSMark Bloch 
3173e126ba97SEli Cohen 	err = create_umr_res(dev);
3174e126ba97SEli Cohen 	if (err)
3175e126ba97SEli Cohen 		goto err_dev;
3176e126ba97SEli Cohen 
3177e126ba97SEli Cohen 	for (i = 0; i < ARRAY_SIZE(mlx5_class_attributes); i++) {
3178281d1a92SWei Yongjun 		err = device_create_file(&dev->ib_dev.dev,
3179281d1a92SWei Yongjun 					 mlx5_class_attributes[i]);
3180281d1a92SWei Yongjun 		if (err)
3181e126ba97SEli Cohen 			goto err_umrc;
3182e126ba97SEli Cohen 	}
3183e126ba97SEli Cohen 
3184e126ba97SEli Cohen 	dev->ib_active = true;
3185e126ba97SEli Cohen 
31869603b61dSJack Morgenstein 	return dev;
3187e126ba97SEli Cohen 
3188e126ba97SEli Cohen err_umrc:
3189e126ba97SEli Cohen 	destroy_umrc_res(dev);
3190e126ba97SEli Cohen 
3191e126ba97SEli Cohen err_dev:
3192e126ba97SEli Cohen 	ib_unregister_device(&dev->ib_dev);
3193e126ba97SEli Cohen 
31940837e86aSMark Bloch err_q_cnt:
31950837e86aSMark Bloch 	mlx5_ib_dealloc_q_counters(dev);
31960837e86aSMark Bloch 
31976aec21f6SHaggai Eran err_odp:
31986aec21f6SHaggai Eran 	mlx5_ib_odp_remove_one(dev);
31996aec21f6SHaggai Eran 
3200e126ba97SEli Cohen err_rsrc:
3201e126ba97SEli Cohen 	destroy_dev_resources(&dev->devr);
3202e126ba97SEli Cohen 
3203fc24fc5eSAchiad Shochat err_disable_roce:
32045ec8c83eSAviv Heller 	if (ll == IB_LINK_LAYER_ETHERNET) {
3205fc24fc5eSAchiad Shochat 		mlx5_disable_roce(dev);
32065ec8c83eSAviv Heller 		mlx5_remove_roce_notifier(dev);
32075ec8c83eSAviv Heller 	}
3208fc24fc5eSAchiad Shochat 
32090837e86aSMark Bloch err_free_port:
32100837e86aSMark Bloch 	kfree(dev->port);
32110837e86aSMark Bloch 
32129603b61dSJack Morgenstein err_dealloc:
3213e126ba97SEli Cohen 	ib_dealloc_device((struct ib_device *)dev);
3214e126ba97SEli Cohen 
32159603b61dSJack Morgenstein 	return NULL;
3216e126ba97SEli Cohen }
3217e126ba97SEli Cohen 
32189603b61dSJack Morgenstein static void mlx5_ib_remove(struct mlx5_core_dev *mdev, void *context)
3219e126ba97SEli Cohen {
32209603b61dSJack Morgenstein 	struct mlx5_ib_dev *dev = context;
3221fc24fc5eSAchiad Shochat 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev, 1);
32226aec21f6SHaggai Eran 
32235ec8c83eSAviv Heller 	mlx5_remove_roce_notifier(dev);
3224e126ba97SEli Cohen 	ib_unregister_device(&dev->ib_dev);
32250837e86aSMark Bloch 	mlx5_ib_dealloc_q_counters(dev);
3226eefd56e5SEli Cohen 	destroy_umrc_res(dev);
32276aec21f6SHaggai Eran 	mlx5_ib_odp_remove_one(dev);
3228e126ba97SEli Cohen 	destroy_dev_resources(&dev->devr);
3229fc24fc5eSAchiad Shochat 	if (ll == IB_LINK_LAYER_ETHERNET)
3230fc24fc5eSAchiad Shochat 		mlx5_disable_roce(dev);
32310837e86aSMark Bloch 	kfree(dev->port);
3232e126ba97SEli Cohen 	ib_dealloc_device(&dev->ib_dev);
3233e126ba97SEli Cohen }
3234e126ba97SEli Cohen 
32359603b61dSJack Morgenstein static struct mlx5_interface mlx5_ib_interface = {
32369603b61dSJack Morgenstein 	.add            = mlx5_ib_add,
32379603b61dSJack Morgenstein 	.remove         = mlx5_ib_remove,
32389603b61dSJack Morgenstein 	.event          = mlx5_ib_event,
323964613d94SSaeed Mahameed 	.protocol	= MLX5_INTERFACE_PROTOCOL_IB,
3240e126ba97SEli Cohen };
3241e126ba97SEli Cohen 
3242e126ba97SEli Cohen static int __init mlx5_ib_init(void)
3243e126ba97SEli Cohen {
32446aec21f6SHaggai Eran 	int err;
32456aec21f6SHaggai Eran 
32469603b61dSJack Morgenstein 	if (deprecated_prof_sel != 2)
32479603b61dSJack Morgenstein 		pr_warn("prof_sel is deprecated for mlx5_ib, set it for mlx5_core\n");
32489603b61dSJack Morgenstein 
32496aec21f6SHaggai Eran 	err = mlx5_ib_odp_init();
32506aec21f6SHaggai Eran 	if (err)
32516aec21f6SHaggai Eran 		return err;
32526aec21f6SHaggai Eran 
32536aec21f6SHaggai Eran 	err = mlx5_register_interface(&mlx5_ib_interface);
32546aec21f6SHaggai Eran 	if (err)
32556aec21f6SHaggai Eran 		goto clean_odp;
32566aec21f6SHaggai Eran 
32576aec21f6SHaggai Eran 	return err;
32586aec21f6SHaggai Eran 
32596aec21f6SHaggai Eran clean_odp:
32606aec21f6SHaggai Eran 	mlx5_ib_odp_cleanup();
32616aec21f6SHaggai Eran 	return err;
3262e126ba97SEli Cohen }
3263e126ba97SEli Cohen 
3264e126ba97SEli Cohen static void __exit mlx5_ib_cleanup(void)
3265e126ba97SEli Cohen {
32669603b61dSJack Morgenstein 	mlx5_unregister_interface(&mlx5_ib_interface);
32676aec21f6SHaggai Eran 	mlx5_ib_odp_cleanup();
3268e126ba97SEli Cohen }
3269e126ba97SEli Cohen 
3270e126ba97SEli Cohen module_init(mlx5_ib_init);
3271e126ba97SEli Cohen module_exit(mlx5_ib_cleanup);
3272