xref: /openbmc/linux/drivers/infiniband/hw/mlx5/main.c (revision 2788cf3bd90af3791c3195c52391bcf34fa67b40)
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>
40e126ba97SEli Cohen #include <linux/io-mapping.h>
4137aa5c36SGuy Levi #if defined(CONFIG_X86)
4237aa5c36SGuy Levi #include <asm/pat.h>
4337aa5c36SGuy Levi #endif
44e126ba97SEli Cohen #include <linux/sched.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>
50e126ba97SEli Cohen #include <rdma/ib_smi.h>
51e126ba97SEli Cohen #include <rdma/ib_umem.h>
52038d2ef8SMaor Gottlieb #include <linux/in.h>
53038d2ef8SMaor Gottlieb #include <linux/etherdevice.h>
54038d2ef8SMaor Gottlieb #include <linux/mlx5/fs.h>
55e126ba97SEli Cohen #include "user.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 
108fc24fc5eSAchiad Shochat 	if ((event != NETDEV_UNREGISTER) && (event != NETDEV_REGISTER))
109fc24fc5eSAchiad Shochat 		return NOTIFY_DONE;
110fc24fc5eSAchiad Shochat 
111fc24fc5eSAchiad Shochat 	write_lock(&ibdev->roce.netdev_lock);
112fc24fc5eSAchiad Shochat 	if (ndev->dev.parent == &ibdev->mdev->pdev->dev)
113fc24fc5eSAchiad Shochat 		ibdev->roce.netdev = (event == NETDEV_UNREGISTER) ? NULL : ndev;
114fc24fc5eSAchiad Shochat 	write_unlock(&ibdev->roce.netdev_lock);
115fc24fc5eSAchiad Shochat 
116fc24fc5eSAchiad Shochat 	return NOTIFY_DONE;
117fc24fc5eSAchiad Shochat }
118fc24fc5eSAchiad Shochat 
119fc24fc5eSAchiad Shochat static struct net_device *mlx5_ib_get_netdev(struct ib_device *device,
120fc24fc5eSAchiad Shochat 					     u8 port_num)
121fc24fc5eSAchiad Shochat {
122fc24fc5eSAchiad Shochat 	struct mlx5_ib_dev *ibdev = to_mdev(device);
123fc24fc5eSAchiad Shochat 	struct net_device *ndev;
124fc24fc5eSAchiad Shochat 
125fc24fc5eSAchiad Shochat 	/* Ensure ndev does not disappear before we invoke dev_hold()
126fc24fc5eSAchiad Shochat 	 */
127fc24fc5eSAchiad Shochat 	read_lock(&ibdev->roce.netdev_lock);
128fc24fc5eSAchiad Shochat 	ndev = ibdev->roce.netdev;
129fc24fc5eSAchiad Shochat 	if (ndev)
130fc24fc5eSAchiad Shochat 		dev_hold(ndev);
131fc24fc5eSAchiad Shochat 	read_unlock(&ibdev->roce.netdev_lock);
132fc24fc5eSAchiad Shochat 
133fc24fc5eSAchiad Shochat 	return ndev;
134fc24fc5eSAchiad Shochat }
135fc24fc5eSAchiad Shochat 
1363f89a643SAchiad Shochat static int mlx5_query_port_roce(struct ib_device *device, u8 port_num,
1373f89a643SAchiad Shochat 				struct ib_port_attr *props)
1383f89a643SAchiad Shochat {
1393f89a643SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(device);
1403f89a643SAchiad Shochat 	struct net_device *ndev;
1413f89a643SAchiad Shochat 	enum ib_mtu ndev_ib_mtu;
142c876a1b7SLeon Romanovsky 	u16 qkey_viol_cntr;
1433f89a643SAchiad Shochat 
1443f89a643SAchiad Shochat 	memset(props, 0, sizeof(*props));
1453f89a643SAchiad Shochat 
1463f89a643SAchiad Shochat 	props->port_cap_flags  |= IB_PORT_CM_SUP;
1473f89a643SAchiad Shochat 	props->port_cap_flags  |= IB_PORT_IP_BASED_GIDS;
1483f89a643SAchiad Shochat 
1493f89a643SAchiad Shochat 	props->gid_tbl_len      = MLX5_CAP_ROCE(dev->mdev,
1503f89a643SAchiad Shochat 						roce_address_table_size);
1513f89a643SAchiad Shochat 	props->max_mtu          = IB_MTU_4096;
1523f89a643SAchiad Shochat 	props->max_msg_sz       = 1 << MLX5_CAP_GEN(dev->mdev, log_max_msg);
1533f89a643SAchiad Shochat 	props->pkey_tbl_len     = 1;
1543f89a643SAchiad Shochat 	props->state            = IB_PORT_DOWN;
1553f89a643SAchiad Shochat 	props->phys_state       = 3;
1563f89a643SAchiad Shochat 
157c876a1b7SLeon Romanovsky 	mlx5_query_nic_vport_qkey_viol_cntr(dev->mdev, &qkey_viol_cntr);
158c876a1b7SLeon Romanovsky 	props->qkey_viol_cntr = qkey_viol_cntr;
1593f89a643SAchiad Shochat 
1603f89a643SAchiad Shochat 	ndev = mlx5_ib_get_netdev(device, port_num);
1613f89a643SAchiad Shochat 	if (!ndev)
1623f89a643SAchiad Shochat 		return 0;
1633f89a643SAchiad Shochat 
1643f89a643SAchiad Shochat 	if (netif_running(ndev) && netif_carrier_ok(ndev)) {
1653f89a643SAchiad Shochat 		props->state      = IB_PORT_ACTIVE;
1663f89a643SAchiad Shochat 		props->phys_state = 5;
1673f89a643SAchiad Shochat 	}
1683f89a643SAchiad Shochat 
1693f89a643SAchiad Shochat 	ndev_ib_mtu = iboe_get_mtu(ndev->mtu);
1703f89a643SAchiad Shochat 
1713f89a643SAchiad Shochat 	dev_put(ndev);
1723f89a643SAchiad Shochat 
1733f89a643SAchiad Shochat 	props->active_mtu	= min(props->max_mtu, ndev_ib_mtu);
1743f89a643SAchiad Shochat 
1753f89a643SAchiad Shochat 	props->active_width	= IB_WIDTH_4X;  /* TODO */
1763f89a643SAchiad Shochat 	props->active_speed	= IB_SPEED_QDR; /* TODO */
1773f89a643SAchiad Shochat 
1783f89a643SAchiad Shochat 	return 0;
1793f89a643SAchiad Shochat }
1803f89a643SAchiad Shochat 
1813cca2606SAchiad Shochat static void ib_gid_to_mlx5_roce_addr(const union ib_gid *gid,
1823cca2606SAchiad Shochat 				     const struct ib_gid_attr *attr,
1833cca2606SAchiad Shochat 				     void *mlx5_addr)
1843cca2606SAchiad Shochat {
1853cca2606SAchiad Shochat #define MLX5_SET_RA(p, f, v) MLX5_SET(roce_addr_layout, p, f, v)
1863cca2606SAchiad Shochat 	char *mlx5_addr_l3_addr	= MLX5_ADDR_OF(roce_addr_layout, mlx5_addr,
1873cca2606SAchiad Shochat 					       source_l3_address);
1883cca2606SAchiad Shochat 	void *mlx5_addr_mac	= MLX5_ADDR_OF(roce_addr_layout, mlx5_addr,
1893cca2606SAchiad Shochat 					       source_mac_47_32);
1903cca2606SAchiad Shochat 
1913cca2606SAchiad Shochat 	if (!gid)
1923cca2606SAchiad Shochat 		return;
1933cca2606SAchiad Shochat 
1943cca2606SAchiad Shochat 	ether_addr_copy(mlx5_addr_mac, attr->ndev->dev_addr);
1953cca2606SAchiad Shochat 
1963cca2606SAchiad Shochat 	if (is_vlan_dev(attr->ndev)) {
1973cca2606SAchiad Shochat 		MLX5_SET_RA(mlx5_addr, vlan_valid, 1);
1983cca2606SAchiad Shochat 		MLX5_SET_RA(mlx5_addr, vlan_id, vlan_dev_vlan_id(attr->ndev));
1993cca2606SAchiad Shochat 	}
2003cca2606SAchiad Shochat 
2013cca2606SAchiad Shochat 	switch (attr->gid_type) {
2023cca2606SAchiad Shochat 	case IB_GID_TYPE_IB:
2033cca2606SAchiad Shochat 		MLX5_SET_RA(mlx5_addr, roce_version, MLX5_ROCE_VERSION_1);
2043cca2606SAchiad Shochat 		break;
2053cca2606SAchiad Shochat 	case IB_GID_TYPE_ROCE_UDP_ENCAP:
2063cca2606SAchiad Shochat 		MLX5_SET_RA(mlx5_addr, roce_version, MLX5_ROCE_VERSION_2);
2073cca2606SAchiad Shochat 		break;
2083cca2606SAchiad Shochat 
2093cca2606SAchiad Shochat 	default:
2103cca2606SAchiad Shochat 		WARN_ON(true);
2113cca2606SAchiad Shochat 	}
2123cca2606SAchiad Shochat 
2133cca2606SAchiad Shochat 	if (attr->gid_type != IB_GID_TYPE_IB) {
2143cca2606SAchiad Shochat 		if (ipv6_addr_v4mapped((void *)gid))
2153cca2606SAchiad Shochat 			MLX5_SET_RA(mlx5_addr, roce_l3_type,
2163cca2606SAchiad Shochat 				    MLX5_ROCE_L3_TYPE_IPV4);
2173cca2606SAchiad Shochat 		else
2183cca2606SAchiad Shochat 			MLX5_SET_RA(mlx5_addr, roce_l3_type,
2193cca2606SAchiad Shochat 				    MLX5_ROCE_L3_TYPE_IPV6);
2203cca2606SAchiad Shochat 	}
2213cca2606SAchiad Shochat 
2223cca2606SAchiad Shochat 	if ((attr->gid_type == IB_GID_TYPE_IB) ||
2233cca2606SAchiad Shochat 	    !ipv6_addr_v4mapped((void *)gid))
2243cca2606SAchiad Shochat 		memcpy(mlx5_addr_l3_addr, gid, sizeof(*gid));
2253cca2606SAchiad Shochat 	else
2263cca2606SAchiad Shochat 		memcpy(&mlx5_addr_l3_addr[12], &gid->raw[12], 4);
2273cca2606SAchiad Shochat }
2283cca2606SAchiad Shochat 
2293cca2606SAchiad Shochat static int set_roce_addr(struct ib_device *device, u8 port_num,
2303cca2606SAchiad Shochat 			 unsigned int index,
2313cca2606SAchiad Shochat 			 const union ib_gid *gid,
2323cca2606SAchiad Shochat 			 const struct ib_gid_attr *attr)
2333cca2606SAchiad Shochat {
2343cca2606SAchiad Shochat 	struct mlx5_ib_dev *dev	= to_mdev(device);
2353cca2606SAchiad Shochat 	u32  in[MLX5_ST_SZ_DW(set_roce_address_in)];
2363cca2606SAchiad Shochat 	u32 out[MLX5_ST_SZ_DW(set_roce_address_out)];
2373cca2606SAchiad Shochat 	void *in_addr = MLX5_ADDR_OF(set_roce_address_in, in, roce_address);
2383cca2606SAchiad Shochat 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(device, port_num);
2393cca2606SAchiad Shochat 
2403cca2606SAchiad Shochat 	if (ll != IB_LINK_LAYER_ETHERNET)
2413cca2606SAchiad Shochat 		return -EINVAL;
2423cca2606SAchiad Shochat 
2433cca2606SAchiad Shochat 	memset(in, 0, sizeof(in));
2443cca2606SAchiad Shochat 
2453cca2606SAchiad Shochat 	ib_gid_to_mlx5_roce_addr(gid, attr, in_addr);
2463cca2606SAchiad Shochat 
2473cca2606SAchiad Shochat 	MLX5_SET(set_roce_address_in, in, roce_address_index, index);
2483cca2606SAchiad Shochat 	MLX5_SET(set_roce_address_in, in, opcode, MLX5_CMD_OP_SET_ROCE_ADDRESS);
2493cca2606SAchiad Shochat 
2503cca2606SAchiad Shochat 	memset(out, 0, sizeof(out));
2513cca2606SAchiad Shochat 	return mlx5_cmd_exec(dev->mdev, in, sizeof(in), out, sizeof(out));
2523cca2606SAchiad Shochat }
2533cca2606SAchiad Shochat 
2543cca2606SAchiad Shochat static int mlx5_ib_add_gid(struct ib_device *device, u8 port_num,
2553cca2606SAchiad Shochat 			   unsigned int index, const union ib_gid *gid,
2563cca2606SAchiad Shochat 			   const struct ib_gid_attr *attr,
2573cca2606SAchiad Shochat 			   __always_unused void **context)
2583cca2606SAchiad Shochat {
2593cca2606SAchiad Shochat 	return set_roce_addr(device, port_num, index, gid, attr);
2603cca2606SAchiad Shochat }
2613cca2606SAchiad Shochat 
2623cca2606SAchiad Shochat static int mlx5_ib_del_gid(struct ib_device *device, u8 port_num,
2633cca2606SAchiad Shochat 			   unsigned int index, __always_unused void **context)
2643cca2606SAchiad Shochat {
2653cca2606SAchiad Shochat 	return set_roce_addr(device, port_num, index, NULL, NULL);
2663cca2606SAchiad Shochat }
2673cca2606SAchiad Shochat 
2682811ba51SAchiad Shochat __be16 mlx5_get_roce_udp_sport(struct mlx5_ib_dev *dev, u8 port_num,
2692811ba51SAchiad Shochat 			       int index)
2702811ba51SAchiad Shochat {
2712811ba51SAchiad Shochat 	struct ib_gid_attr attr;
2722811ba51SAchiad Shochat 	union ib_gid gid;
2732811ba51SAchiad Shochat 
2742811ba51SAchiad Shochat 	if (ib_get_cached_gid(&dev->ib_dev, port_num, index, &gid, &attr))
2752811ba51SAchiad Shochat 		return 0;
2762811ba51SAchiad Shochat 
2772811ba51SAchiad Shochat 	if (!attr.ndev)
2782811ba51SAchiad Shochat 		return 0;
2792811ba51SAchiad Shochat 
2802811ba51SAchiad Shochat 	dev_put(attr.ndev);
2812811ba51SAchiad Shochat 
2822811ba51SAchiad Shochat 	if (attr.gid_type != IB_GID_TYPE_ROCE_UDP_ENCAP)
2832811ba51SAchiad Shochat 		return 0;
2842811ba51SAchiad Shochat 
2852811ba51SAchiad Shochat 	return cpu_to_be16(MLX5_CAP_ROCE(dev->mdev, r_roce_min_src_udp_port));
2862811ba51SAchiad Shochat }
2872811ba51SAchiad Shochat 
2881b5daf11SMajd Dibbiny static int mlx5_use_mad_ifc(struct mlx5_ib_dev *dev)
2891b5daf11SMajd Dibbiny {
290d603c809SEli Cohen 	return !MLX5_CAP_GEN(dev->mdev, ib_virt);
2911b5daf11SMajd Dibbiny }
2921b5daf11SMajd Dibbiny 
2931b5daf11SMajd Dibbiny enum {
2941b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_MAD,
2951b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_HCA,
2961b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_NIC,
2971b5daf11SMajd Dibbiny };
2981b5daf11SMajd Dibbiny 
2991b5daf11SMajd Dibbiny static int mlx5_get_vport_access_method(struct ib_device *ibdev)
3001b5daf11SMajd Dibbiny {
3011b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(to_mdev(ibdev)))
3021b5daf11SMajd Dibbiny 		return MLX5_VPORT_ACCESS_METHOD_MAD;
3031b5daf11SMajd Dibbiny 
304ebd61f68SAchiad Shochat 	if (mlx5_ib_port_link_layer(ibdev, 1) ==
3051b5daf11SMajd Dibbiny 	    IB_LINK_LAYER_ETHERNET)
3061b5daf11SMajd Dibbiny 		return MLX5_VPORT_ACCESS_METHOD_NIC;
3071b5daf11SMajd Dibbiny 
3081b5daf11SMajd Dibbiny 	return MLX5_VPORT_ACCESS_METHOD_HCA;
3091b5daf11SMajd Dibbiny }
3101b5daf11SMajd Dibbiny 
311da7525d2SEran Ben Elisha static void get_atomic_caps(struct mlx5_ib_dev *dev,
312da7525d2SEran Ben Elisha 			    struct ib_device_attr *props)
313da7525d2SEran Ben Elisha {
314da7525d2SEran Ben Elisha 	u8 tmp;
315da7525d2SEran Ben Elisha 	u8 atomic_operations = MLX5_CAP_ATOMIC(dev->mdev, atomic_operations);
316da7525d2SEran Ben Elisha 	u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_qp);
317da7525d2SEran Ben Elisha 	u8 atomic_req_8B_endianness_mode =
318da7525d2SEran Ben Elisha 		MLX5_CAP_ATOMIC(dev->mdev, atomic_req_8B_endianess_mode);
319da7525d2SEran Ben Elisha 
320da7525d2SEran Ben Elisha 	/* Check if HW supports 8 bytes standard atomic operations and capable
321da7525d2SEran Ben Elisha 	 * of host endianness respond
322da7525d2SEran Ben Elisha 	 */
323da7525d2SEran Ben Elisha 	tmp = MLX5_ATOMIC_OPS_CMP_SWAP | MLX5_ATOMIC_OPS_FETCH_ADD;
324da7525d2SEran Ben Elisha 	if (((atomic_operations & tmp) == tmp) &&
325da7525d2SEran Ben Elisha 	    (atomic_size_qp & MLX5_ATOMIC_SIZE_QP_8BYTES) &&
326da7525d2SEran Ben Elisha 	    (atomic_req_8B_endianness_mode)) {
327da7525d2SEran Ben Elisha 		props->atomic_cap = IB_ATOMIC_HCA;
328da7525d2SEran Ben Elisha 	} else {
329da7525d2SEran Ben Elisha 		props->atomic_cap = IB_ATOMIC_NONE;
330da7525d2SEran Ben Elisha 	}
331da7525d2SEran Ben Elisha }
332da7525d2SEran Ben Elisha 
3331b5daf11SMajd Dibbiny static int mlx5_query_system_image_guid(struct ib_device *ibdev,
3341b5daf11SMajd Dibbiny 					__be64 *sys_image_guid)
3351b5daf11SMajd Dibbiny {
3361b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
3371b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
3381b5daf11SMajd Dibbiny 	u64 tmp;
3391b5daf11SMajd Dibbiny 	int err;
3401b5daf11SMajd Dibbiny 
3411b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
3421b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
3431b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_system_image_guid(ibdev,
3441b5daf11SMajd Dibbiny 							    sys_image_guid);
3451b5daf11SMajd Dibbiny 
3461b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
3471b5daf11SMajd Dibbiny 		err = mlx5_query_hca_vport_system_image_guid(mdev, &tmp);
3483f89a643SAchiad Shochat 		break;
3493f89a643SAchiad Shochat 
3503f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
3513f89a643SAchiad Shochat 		err = mlx5_query_nic_vport_system_image_guid(mdev, &tmp);
3523f89a643SAchiad Shochat 		break;
3531b5daf11SMajd Dibbiny 
3541b5daf11SMajd Dibbiny 	default:
3551b5daf11SMajd Dibbiny 		return -EINVAL;
3561b5daf11SMajd Dibbiny 	}
3573f89a643SAchiad Shochat 
3583f89a643SAchiad Shochat 	if (!err)
3593f89a643SAchiad Shochat 		*sys_image_guid = cpu_to_be64(tmp);
3603f89a643SAchiad Shochat 
3613f89a643SAchiad Shochat 	return err;
3623f89a643SAchiad Shochat 
3631b5daf11SMajd Dibbiny }
3641b5daf11SMajd Dibbiny 
3651b5daf11SMajd Dibbiny static int mlx5_query_max_pkeys(struct ib_device *ibdev,
3661b5daf11SMajd Dibbiny 				u16 *max_pkeys)
3671b5daf11SMajd Dibbiny {
3681b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
3691b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
3701b5daf11SMajd Dibbiny 
3711b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
3721b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
3731b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_max_pkeys(ibdev, max_pkeys);
3741b5daf11SMajd Dibbiny 
3751b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
3761b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
3771b5daf11SMajd Dibbiny 		*max_pkeys = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev,
3781b5daf11SMajd Dibbiny 						pkey_table_size));
3791b5daf11SMajd Dibbiny 		return 0;
3801b5daf11SMajd Dibbiny 
3811b5daf11SMajd Dibbiny 	default:
3821b5daf11SMajd Dibbiny 		return -EINVAL;
3831b5daf11SMajd Dibbiny 	}
3841b5daf11SMajd Dibbiny }
3851b5daf11SMajd Dibbiny 
3861b5daf11SMajd Dibbiny static int mlx5_query_vendor_id(struct ib_device *ibdev,
3871b5daf11SMajd Dibbiny 				u32 *vendor_id)
3881b5daf11SMajd Dibbiny {
3891b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
3901b5daf11SMajd Dibbiny 
3911b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
3921b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
3931b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_vendor_id(ibdev, vendor_id);
3941b5daf11SMajd Dibbiny 
3951b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
3961b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
3971b5daf11SMajd Dibbiny 		return mlx5_core_query_vendor_id(dev->mdev, vendor_id);
3981b5daf11SMajd Dibbiny 
3991b5daf11SMajd Dibbiny 	default:
4001b5daf11SMajd Dibbiny 		return -EINVAL;
4011b5daf11SMajd Dibbiny 	}
4021b5daf11SMajd Dibbiny }
4031b5daf11SMajd Dibbiny 
4041b5daf11SMajd Dibbiny static int mlx5_query_node_guid(struct mlx5_ib_dev *dev,
4051b5daf11SMajd Dibbiny 				__be64 *node_guid)
4061b5daf11SMajd Dibbiny {
4071b5daf11SMajd Dibbiny 	u64 tmp;
4081b5daf11SMajd Dibbiny 	int err;
4091b5daf11SMajd Dibbiny 
4101b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(&dev->ib_dev)) {
4111b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
4121b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_node_guid(dev, node_guid);
4131b5daf11SMajd Dibbiny 
4141b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
4151b5daf11SMajd Dibbiny 		err = mlx5_query_hca_vport_node_guid(dev->mdev, &tmp);
4163f89a643SAchiad Shochat 		break;
4173f89a643SAchiad Shochat 
4183f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
4193f89a643SAchiad Shochat 		err = mlx5_query_nic_vport_node_guid(dev->mdev, &tmp);
4203f89a643SAchiad Shochat 		break;
4211b5daf11SMajd Dibbiny 
4221b5daf11SMajd Dibbiny 	default:
4231b5daf11SMajd Dibbiny 		return -EINVAL;
4241b5daf11SMajd Dibbiny 	}
4253f89a643SAchiad Shochat 
4263f89a643SAchiad Shochat 	if (!err)
4273f89a643SAchiad Shochat 		*node_guid = cpu_to_be64(tmp);
4283f89a643SAchiad Shochat 
4293f89a643SAchiad Shochat 	return err;
4301b5daf11SMajd Dibbiny }
4311b5daf11SMajd Dibbiny 
4321b5daf11SMajd Dibbiny struct mlx5_reg_node_desc {
4331b5daf11SMajd Dibbiny 	u8	desc[64];
4341b5daf11SMajd Dibbiny };
4351b5daf11SMajd Dibbiny 
4361b5daf11SMajd Dibbiny static int mlx5_query_node_desc(struct mlx5_ib_dev *dev, char *node_desc)
4371b5daf11SMajd Dibbiny {
4381b5daf11SMajd Dibbiny 	struct mlx5_reg_node_desc in;
4391b5daf11SMajd Dibbiny 
4401b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(dev))
4411b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_node_desc(dev, node_desc);
4421b5daf11SMajd Dibbiny 
4431b5daf11SMajd Dibbiny 	memset(&in, 0, sizeof(in));
4441b5daf11SMajd Dibbiny 
4451b5daf11SMajd Dibbiny 	return mlx5_core_access_reg(dev->mdev, &in, sizeof(in), node_desc,
4461b5daf11SMajd Dibbiny 				    sizeof(struct mlx5_reg_node_desc),
4471b5daf11SMajd Dibbiny 				    MLX5_REG_NODE_DESC, 0, 0);
4481b5daf11SMajd Dibbiny }
4491b5daf11SMajd Dibbiny 
450e126ba97SEli Cohen static int mlx5_ib_query_device(struct ib_device *ibdev,
4512528e33eSMatan Barak 				struct ib_device_attr *props,
4522528e33eSMatan Barak 				struct ib_udata *uhw)
453e126ba97SEli Cohen {
454e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
455938fe83cSSaeed Mahameed 	struct mlx5_core_dev *mdev = dev->mdev;
456e126ba97SEli Cohen 	int err = -ENOMEM;
457e126ba97SEli Cohen 	int max_rq_sg;
458e126ba97SEli Cohen 	int max_sq_sg;
459e0238a6aSSagi Grimberg 	u64 min_page_size = 1ull << MLX5_CAP_GEN(mdev, log_pg_sz);
460e126ba97SEli Cohen 
4612528e33eSMatan Barak 	if (uhw->inlen || uhw->outlen)
4622528e33eSMatan Barak 		return -EINVAL;
4632528e33eSMatan Barak 
464e126ba97SEli Cohen 	memset(props, 0, sizeof(*props));
4651b5daf11SMajd Dibbiny 	err = mlx5_query_system_image_guid(ibdev,
4661b5daf11SMajd Dibbiny 					   &props->sys_image_guid);
4671b5daf11SMajd Dibbiny 	if (err)
4681b5daf11SMajd Dibbiny 		return err;
4691b5daf11SMajd Dibbiny 
4701b5daf11SMajd Dibbiny 	err = mlx5_query_max_pkeys(ibdev, &props->max_pkeys);
4711b5daf11SMajd Dibbiny 	if (err)
4721b5daf11SMajd Dibbiny 		return err;
4731b5daf11SMajd Dibbiny 
4741b5daf11SMajd Dibbiny 	err = mlx5_query_vendor_id(ibdev, &props->vendor_id);
4751b5daf11SMajd Dibbiny 	if (err)
4761b5daf11SMajd Dibbiny 		return err;
477e126ba97SEli Cohen 
4789603b61dSJack Morgenstein 	props->fw_ver = ((u64)fw_rev_maj(dev->mdev) << 32) |
4799603b61dSJack Morgenstein 		(fw_rev_min(dev->mdev) << 16) |
4809603b61dSJack Morgenstein 		fw_rev_sub(dev->mdev);
481e126ba97SEli Cohen 	props->device_cap_flags    = IB_DEVICE_CHANGE_PHY_PORT |
482e126ba97SEli Cohen 		IB_DEVICE_PORT_ACTIVE_EVENT		|
483e126ba97SEli Cohen 		IB_DEVICE_SYS_IMAGE_GUID		|
4841a4c3a3dSEli Cohen 		IB_DEVICE_RC_RNR_NAK_GEN;
485938fe83cSSaeed Mahameed 
486938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, pkv))
487e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
488938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, qkv))
489e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
490938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, apm))
491e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
492938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, xrc))
493e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_XRC;
494d2370e0aSMatan Barak 	if (MLX5_CAP_GEN(mdev, imaicl)) {
495d2370e0aSMatan Barak 		props->device_cap_flags |= IB_DEVICE_MEM_WINDOW |
496d2370e0aSMatan Barak 					   IB_DEVICE_MEM_WINDOW_TYPE_2B;
497d2370e0aSMatan Barak 		props->max_mw = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
498b005d316SSagi Grimberg 		/* We support 'Gappy' memory registration too */
499b005d316SSagi Grimberg 		props->device_cap_flags |= IB_DEVICE_SG_GAPS_REG;
500d2370e0aSMatan Barak 	}
501e126ba97SEli Cohen 	props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
502938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, sho)) {
5032dea9094SSagi Grimberg 		props->device_cap_flags |= IB_DEVICE_SIGNATURE_HANDOVER;
5042dea9094SSagi Grimberg 		/* At this stage no support for signature handover */
5052dea9094SSagi Grimberg 		props->sig_prot_cap = IB_PROT_T10DIF_TYPE_1 |
5062dea9094SSagi Grimberg 				      IB_PROT_T10DIF_TYPE_2 |
5072dea9094SSagi Grimberg 				      IB_PROT_T10DIF_TYPE_3;
5082dea9094SSagi Grimberg 		props->sig_guard_cap = IB_GUARD_T10DIF_CRC |
5092dea9094SSagi Grimberg 				       IB_GUARD_T10DIF_CSUM;
5102dea9094SSagi Grimberg 	}
511938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, block_lb_mc))
512f360d88aSEli Cohen 		props->device_cap_flags |= IB_DEVICE_BLOCK_MULTICAST_LOOPBACK;
513e126ba97SEli Cohen 
51488115fe7SBodong Wang 	if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
51588115fe7SBodong Wang 	    (MLX5_CAP_ETH(dev->mdev, csum_cap)))
51688115fe7SBodong Wang 			props->device_cap_flags |= IB_DEVICE_RAW_IP_CSUM;
51788115fe7SBodong Wang 
518f0313965SErez Shitrit 	if (MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) {
519f0313965SErez Shitrit 		props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
520f0313965SErez Shitrit 		props->device_cap_flags |= IB_DEVICE_UD_TSO;
521f0313965SErez Shitrit 	}
522f0313965SErez Shitrit 
523cff5a0f3SMajd Dibbiny 	if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
524cff5a0f3SMajd Dibbiny 	    MLX5_CAP_ETH(dev->mdev, scatter_fcs))
525cff5a0f3SMajd Dibbiny 		props->device_cap_flags |= IB_DEVICE_RAW_SCATTER_FCS;
526cff5a0f3SMajd Dibbiny 
527da6d6ba3SMaor Gottlieb 	if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS))
528da6d6ba3SMaor Gottlieb 		props->device_cap_flags |= IB_DEVICE_MANAGED_FLOW_STEERING;
529da6d6ba3SMaor Gottlieb 
5301b5daf11SMajd Dibbiny 	props->vendor_part_id	   = mdev->pdev->device;
5311b5daf11SMajd Dibbiny 	props->hw_ver		   = mdev->pdev->revision;
532e126ba97SEli Cohen 
533e126ba97SEli Cohen 	props->max_mr_size	   = ~0ull;
534e0238a6aSSagi Grimberg 	props->page_size_cap	   = ~(min_page_size - 1);
535938fe83cSSaeed Mahameed 	props->max_qp		   = 1 << MLX5_CAP_GEN(mdev, log_max_qp);
536938fe83cSSaeed Mahameed 	props->max_qp_wr	   = 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
537938fe83cSSaeed Mahameed 	max_rq_sg =  MLX5_CAP_GEN(mdev, max_wqe_sz_rq) /
538938fe83cSSaeed Mahameed 		     sizeof(struct mlx5_wqe_data_seg);
539938fe83cSSaeed Mahameed 	max_sq_sg = (MLX5_CAP_GEN(mdev, max_wqe_sz_sq) -
540938fe83cSSaeed Mahameed 		     sizeof(struct mlx5_wqe_ctrl_seg)) /
541e126ba97SEli Cohen 		     sizeof(struct mlx5_wqe_data_seg);
542e126ba97SEli Cohen 	props->max_sge = min(max_rq_sg, max_sq_sg);
543986ef95eSSagi Grimberg 	props->max_sge_rd	   = MLX5_MAX_SGE_RD;
544938fe83cSSaeed Mahameed 	props->max_cq		   = 1 << MLX5_CAP_GEN(mdev, log_max_cq);
5459f177686SLeon Romanovsky 	props->max_cqe = (1 << MLX5_CAP_GEN(mdev, log_max_cq_sz)) - 1;
546938fe83cSSaeed Mahameed 	props->max_mr		   = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
547938fe83cSSaeed Mahameed 	props->max_pd		   = 1 << MLX5_CAP_GEN(mdev, log_max_pd);
548938fe83cSSaeed Mahameed 	props->max_qp_rd_atom	   = 1 << MLX5_CAP_GEN(mdev, log_max_ra_req_qp);
549938fe83cSSaeed Mahameed 	props->max_qp_init_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_res_qp);
550938fe83cSSaeed Mahameed 	props->max_srq		   = 1 << MLX5_CAP_GEN(mdev, log_max_srq);
551938fe83cSSaeed Mahameed 	props->max_srq_wr = (1 << MLX5_CAP_GEN(mdev, log_max_srq_sz)) - 1;
552938fe83cSSaeed Mahameed 	props->local_ca_ack_delay  = MLX5_CAP_GEN(mdev, local_ca_ack_delay);
553e126ba97SEli Cohen 	props->max_res_rd_atom	   = props->max_qp_rd_atom * props->max_qp;
554e126ba97SEli Cohen 	props->max_srq_sge	   = max_rq_sg - 1;
555911f4331SSagi Grimberg 	props->max_fast_reg_page_list_len =
556911f4331SSagi Grimberg 		1 << MLX5_CAP_GEN(mdev, log_max_klm_list_size);
557da7525d2SEran Ben Elisha 	get_atomic_caps(dev, props);
55881bea28fSEli Cohen 	props->masked_atomic_cap   = IB_ATOMIC_NONE;
559938fe83cSSaeed Mahameed 	props->max_mcast_grp	   = 1 << MLX5_CAP_GEN(mdev, log_max_mcg);
560938fe83cSSaeed Mahameed 	props->max_mcast_qp_attach = MLX5_CAP_GEN(mdev, max_qp_mcg);
561e126ba97SEli Cohen 	props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
562e126ba97SEli Cohen 					   props->max_mcast_grp;
563e126ba97SEli Cohen 	props->max_map_per_fmr = INT_MAX; /* no limit in ConnectIB */
5647c60bcbbSMatan Barak 	props->hca_core_clock = MLX5_CAP_GEN(mdev, device_frequency_khz);
5657c60bcbbSMatan Barak 	props->timestamp_mask = 0x7FFFFFFFFFFFFFFFULL;
566e126ba97SEli Cohen 
5678cdd312cSHaggai Eran #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
568938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, pg))
5698cdd312cSHaggai Eran 		props->device_cap_flags |= IB_DEVICE_ON_DEMAND_PAGING;
5708cdd312cSHaggai Eran 	props->odp_caps = dev->odp_caps;
5718cdd312cSHaggai Eran #endif
5728cdd312cSHaggai Eran 
573051f2630SLeon Romanovsky 	if (MLX5_CAP_GEN(mdev, cd))
574051f2630SLeon Romanovsky 		props->device_cap_flags |= IB_DEVICE_CROSS_CHANNEL;
575051f2630SLeon Romanovsky 
576eff901d3SEli Cohen 	if (!mlx5_core_is_pf(mdev))
577eff901d3SEli Cohen 		props->device_cap_flags |= IB_DEVICE_VIRTUAL_FUNCTION;
578eff901d3SEli Cohen 
5791b5daf11SMajd Dibbiny 	return 0;
5801b5daf11SMajd Dibbiny }
581e126ba97SEli Cohen 
5821b5daf11SMajd Dibbiny enum mlx5_ib_width {
5831b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_1X	= 1 << 0,
5841b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_2X	= 1 << 1,
5851b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_4X	= 1 << 2,
5861b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_8X	= 1 << 3,
5871b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_12X	= 1 << 4
5881b5daf11SMajd Dibbiny };
5891b5daf11SMajd Dibbiny 
5901b5daf11SMajd Dibbiny static int translate_active_width(struct ib_device *ibdev, u8 active_width,
5911b5daf11SMajd Dibbiny 				  u8 *ib_width)
5921b5daf11SMajd Dibbiny {
5931b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
5941b5daf11SMajd Dibbiny 	int err = 0;
5951b5daf11SMajd Dibbiny 
5961b5daf11SMajd Dibbiny 	if (active_width & MLX5_IB_WIDTH_1X) {
5971b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_1X;
5981b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_2X) {
5991b5daf11SMajd Dibbiny 		mlx5_ib_dbg(dev, "active_width %d is not supported by IB spec\n",
6001b5daf11SMajd Dibbiny 			    (int)active_width);
6011b5daf11SMajd Dibbiny 		err = -EINVAL;
6021b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_4X) {
6031b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_4X;
6041b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_8X) {
6051b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_8X;
6061b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_12X) {
6071b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_12X;
6081b5daf11SMajd Dibbiny 	} else {
6091b5daf11SMajd Dibbiny 		mlx5_ib_dbg(dev, "Invalid active_width %d\n",
6101b5daf11SMajd Dibbiny 			    (int)active_width);
6111b5daf11SMajd Dibbiny 		err = -EINVAL;
6121b5daf11SMajd Dibbiny 	}
6131b5daf11SMajd Dibbiny 
6141b5daf11SMajd Dibbiny 	return err;
6151b5daf11SMajd Dibbiny }
6161b5daf11SMajd Dibbiny 
6171b5daf11SMajd Dibbiny static int mlx5_mtu_to_ib_mtu(int mtu)
6181b5daf11SMajd Dibbiny {
6191b5daf11SMajd Dibbiny 	switch (mtu) {
6201b5daf11SMajd Dibbiny 	case 256: return 1;
6211b5daf11SMajd Dibbiny 	case 512: return 2;
6221b5daf11SMajd Dibbiny 	case 1024: return 3;
6231b5daf11SMajd Dibbiny 	case 2048: return 4;
6241b5daf11SMajd Dibbiny 	case 4096: return 5;
6251b5daf11SMajd Dibbiny 	default:
6261b5daf11SMajd Dibbiny 		pr_warn("invalid mtu\n");
6271b5daf11SMajd Dibbiny 		return -1;
6281b5daf11SMajd Dibbiny 	}
6291b5daf11SMajd Dibbiny }
6301b5daf11SMajd Dibbiny 
6311b5daf11SMajd Dibbiny enum ib_max_vl_num {
6321b5daf11SMajd Dibbiny 	__IB_MAX_VL_0		= 1,
6331b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_1		= 2,
6341b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_3		= 3,
6351b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_7		= 4,
6361b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_14	= 5,
6371b5daf11SMajd Dibbiny };
6381b5daf11SMajd Dibbiny 
6391b5daf11SMajd Dibbiny enum mlx5_vl_hw_cap {
6401b5daf11SMajd Dibbiny 	MLX5_VL_HW_0	= 1,
6411b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_1	= 2,
6421b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_2	= 3,
6431b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_3	= 4,
6441b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_4	= 5,
6451b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_5	= 6,
6461b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_6	= 7,
6471b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_7	= 8,
6481b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_14	= 15
6491b5daf11SMajd Dibbiny };
6501b5daf11SMajd Dibbiny 
6511b5daf11SMajd Dibbiny static int translate_max_vl_num(struct ib_device *ibdev, u8 vl_hw_cap,
6521b5daf11SMajd Dibbiny 				u8 *max_vl_num)
6531b5daf11SMajd Dibbiny {
6541b5daf11SMajd Dibbiny 	switch (vl_hw_cap) {
6551b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0:
6561b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0;
6571b5daf11SMajd Dibbiny 		break;
6581b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_1:
6591b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_1;
6601b5daf11SMajd Dibbiny 		break;
6611b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_3:
6621b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_3;
6631b5daf11SMajd Dibbiny 		break;
6641b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_7:
6651b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_7;
6661b5daf11SMajd Dibbiny 		break;
6671b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_14:
6681b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_14;
6691b5daf11SMajd Dibbiny 		break;
6701b5daf11SMajd Dibbiny 
6711b5daf11SMajd Dibbiny 	default:
6721b5daf11SMajd Dibbiny 		return -EINVAL;
6731b5daf11SMajd Dibbiny 	}
6741b5daf11SMajd Dibbiny 
6751b5daf11SMajd Dibbiny 	return 0;
6761b5daf11SMajd Dibbiny }
6771b5daf11SMajd Dibbiny 
6781b5daf11SMajd Dibbiny static int mlx5_query_hca_port(struct ib_device *ibdev, u8 port,
6791b5daf11SMajd Dibbiny 			       struct ib_port_attr *props)
6801b5daf11SMajd Dibbiny {
6811b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
6821b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
6831b5daf11SMajd Dibbiny 	struct mlx5_hca_vport_context *rep;
684046339eaSSaeed Mahameed 	u16 max_mtu;
685046339eaSSaeed Mahameed 	u16 oper_mtu;
6861b5daf11SMajd Dibbiny 	int err;
6871b5daf11SMajd Dibbiny 	u8 ib_link_width_oper;
6881b5daf11SMajd Dibbiny 	u8 vl_hw_cap;
6891b5daf11SMajd Dibbiny 
6901b5daf11SMajd Dibbiny 	rep = kzalloc(sizeof(*rep), GFP_KERNEL);
6911b5daf11SMajd Dibbiny 	if (!rep) {
6921b5daf11SMajd Dibbiny 		err = -ENOMEM;
6931b5daf11SMajd Dibbiny 		goto out;
6941b5daf11SMajd Dibbiny 	}
6951b5daf11SMajd Dibbiny 
6961b5daf11SMajd Dibbiny 	memset(props, 0, sizeof(*props));
6971b5daf11SMajd Dibbiny 
6981b5daf11SMajd Dibbiny 	err = mlx5_query_hca_vport_context(mdev, 0, port, 0, rep);
6991b5daf11SMajd Dibbiny 	if (err)
7001b5daf11SMajd Dibbiny 		goto out;
7011b5daf11SMajd Dibbiny 
7021b5daf11SMajd Dibbiny 	props->lid		= rep->lid;
7031b5daf11SMajd Dibbiny 	props->lmc		= rep->lmc;
7041b5daf11SMajd Dibbiny 	props->sm_lid		= rep->sm_lid;
7051b5daf11SMajd Dibbiny 	props->sm_sl		= rep->sm_sl;
7061b5daf11SMajd Dibbiny 	props->state		= rep->vport_state;
7071b5daf11SMajd Dibbiny 	props->phys_state	= rep->port_physical_state;
7081b5daf11SMajd Dibbiny 	props->port_cap_flags	= rep->cap_mask1;
7091b5daf11SMajd Dibbiny 	props->gid_tbl_len	= mlx5_get_gid_table_len(MLX5_CAP_GEN(mdev, gid_table_size));
7101b5daf11SMajd Dibbiny 	props->max_msg_sz	= 1 << MLX5_CAP_GEN(mdev, log_max_msg);
7111b5daf11SMajd Dibbiny 	props->pkey_tbl_len	= mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, pkey_table_size));
7121b5daf11SMajd Dibbiny 	props->bad_pkey_cntr	= rep->pkey_violation_counter;
7131b5daf11SMajd Dibbiny 	props->qkey_viol_cntr	= rep->qkey_violation_counter;
7141b5daf11SMajd Dibbiny 	props->subnet_timeout	= rep->subnet_timeout;
7151b5daf11SMajd Dibbiny 	props->init_type_reply	= rep->init_type_reply;
716eff901d3SEli Cohen 	props->grh_required	= rep->grh_required;
7171b5daf11SMajd Dibbiny 
7181b5daf11SMajd Dibbiny 	err = mlx5_query_port_link_width_oper(mdev, &ib_link_width_oper, port);
7191b5daf11SMajd Dibbiny 	if (err)
7201b5daf11SMajd Dibbiny 		goto out;
7211b5daf11SMajd Dibbiny 
7221b5daf11SMajd Dibbiny 	err = translate_active_width(ibdev, ib_link_width_oper,
7231b5daf11SMajd Dibbiny 				     &props->active_width);
7241b5daf11SMajd Dibbiny 	if (err)
7251b5daf11SMajd Dibbiny 		goto out;
7261b5daf11SMajd Dibbiny 	err = mlx5_query_port_proto_oper(mdev, &props->active_speed, MLX5_PTYS_IB,
7271b5daf11SMajd Dibbiny 					 port);
7281b5daf11SMajd Dibbiny 	if (err)
7291b5daf11SMajd Dibbiny 		goto out;
7301b5daf11SMajd Dibbiny 
731facc9699SSaeed Mahameed 	mlx5_query_port_max_mtu(mdev, &max_mtu, port);
7321b5daf11SMajd Dibbiny 
7331b5daf11SMajd Dibbiny 	props->max_mtu = mlx5_mtu_to_ib_mtu(max_mtu);
7341b5daf11SMajd Dibbiny 
735facc9699SSaeed Mahameed 	mlx5_query_port_oper_mtu(mdev, &oper_mtu, port);
7361b5daf11SMajd Dibbiny 
7371b5daf11SMajd Dibbiny 	props->active_mtu = mlx5_mtu_to_ib_mtu(oper_mtu);
7381b5daf11SMajd Dibbiny 
7391b5daf11SMajd Dibbiny 	err = mlx5_query_port_vl_hw_cap(mdev, &vl_hw_cap, port);
7401b5daf11SMajd Dibbiny 	if (err)
7411b5daf11SMajd Dibbiny 		goto out;
7421b5daf11SMajd Dibbiny 
7431b5daf11SMajd Dibbiny 	err = translate_max_vl_num(ibdev, vl_hw_cap,
7441b5daf11SMajd Dibbiny 				   &props->max_vl_num);
7451b5daf11SMajd Dibbiny out:
7461b5daf11SMajd Dibbiny 	kfree(rep);
747e126ba97SEli Cohen 	return err;
748e126ba97SEli Cohen }
749e126ba97SEli Cohen 
750e126ba97SEli Cohen int mlx5_ib_query_port(struct ib_device *ibdev, u8 port,
751e126ba97SEli Cohen 		       struct ib_port_attr *props)
752e126ba97SEli Cohen {
7531b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
7541b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
7551b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_port(ibdev, port, props);
756e126ba97SEli Cohen 
7571b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
7581b5daf11SMajd Dibbiny 		return mlx5_query_hca_port(ibdev, port, props);
7591b5daf11SMajd Dibbiny 
7603f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
7613f89a643SAchiad Shochat 		return mlx5_query_port_roce(ibdev, port, props);
7623f89a643SAchiad Shochat 
7631b5daf11SMajd Dibbiny 	default:
764e126ba97SEli Cohen 		return -EINVAL;
765e126ba97SEli Cohen 	}
766e126ba97SEli Cohen }
767e126ba97SEli Cohen 
768e126ba97SEli Cohen static int mlx5_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
769e126ba97SEli Cohen 			     union ib_gid *gid)
770e126ba97SEli Cohen {
7711b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
7721b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
773e126ba97SEli Cohen 
7741b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
7751b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
7761b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_gids(ibdev, port, index, gid);
777e126ba97SEli Cohen 
7781b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
7791b5daf11SMajd Dibbiny 		return mlx5_query_hca_vport_gid(mdev, 0, port, 0, index, gid);
780e126ba97SEli Cohen 
7811b5daf11SMajd Dibbiny 	default:
7821b5daf11SMajd Dibbiny 		return -EINVAL;
7831b5daf11SMajd Dibbiny 	}
784e126ba97SEli Cohen 
785e126ba97SEli Cohen }
786e126ba97SEli Cohen 
787e126ba97SEli Cohen static int mlx5_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
788e126ba97SEli Cohen 			      u16 *pkey)
789e126ba97SEli Cohen {
7901b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
7911b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
792e126ba97SEli Cohen 
7931b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
7941b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
7951b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_pkey(ibdev, port, index, pkey);
796e126ba97SEli Cohen 
7971b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
7981b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
7991b5daf11SMajd Dibbiny 		return mlx5_query_hca_vport_pkey(mdev, 0, port,  0, index,
8001b5daf11SMajd Dibbiny 						 pkey);
8011b5daf11SMajd Dibbiny 	default:
8021b5daf11SMajd Dibbiny 		return -EINVAL;
803e126ba97SEli Cohen 	}
8041b5daf11SMajd Dibbiny }
805e126ba97SEli Cohen 
806e126ba97SEli Cohen static int mlx5_ib_modify_device(struct ib_device *ibdev, int mask,
807e126ba97SEli Cohen 				 struct ib_device_modify *props)
808e126ba97SEli Cohen {
809e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
810e126ba97SEli Cohen 	struct mlx5_reg_node_desc in;
811e126ba97SEli Cohen 	struct mlx5_reg_node_desc out;
812e126ba97SEli Cohen 	int err;
813e126ba97SEli Cohen 
814e126ba97SEli Cohen 	if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
815e126ba97SEli Cohen 		return -EOPNOTSUPP;
816e126ba97SEli Cohen 
817e126ba97SEli Cohen 	if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
818e126ba97SEli Cohen 		return 0;
819e126ba97SEli Cohen 
820e126ba97SEli Cohen 	/*
821e126ba97SEli Cohen 	 * If possible, pass node desc to FW, so it can generate
822e126ba97SEli Cohen 	 * a 144 trap.  If cmd fails, just ignore.
823e126ba97SEli Cohen 	 */
824e126ba97SEli Cohen 	memcpy(&in, props->node_desc, 64);
8259603b61dSJack Morgenstein 	err = mlx5_core_access_reg(dev->mdev, &in, sizeof(in), &out,
826e126ba97SEli Cohen 				   sizeof(out), MLX5_REG_NODE_DESC, 0, 1);
827e126ba97SEli Cohen 	if (err)
828e126ba97SEli Cohen 		return err;
829e126ba97SEli Cohen 
830e126ba97SEli Cohen 	memcpy(ibdev->node_desc, props->node_desc, 64);
831e126ba97SEli Cohen 
832e126ba97SEli Cohen 	return err;
833e126ba97SEli Cohen }
834e126ba97SEli Cohen 
835e126ba97SEli Cohen static int mlx5_ib_modify_port(struct ib_device *ibdev, u8 port, int mask,
836e126ba97SEli Cohen 			       struct ib_port_modify *props)
837e126ba97SEli Cohen {
838e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
839e126ba97SEli Cohen 	struct ib_port_attr attr;
840e126ba97SEli Cohen 	u32 tmp;
841e126ba97SEli Cohen 	int err;
842e126ba97SEli Cohen 
843e126ba97SEli Cohen 	mutex_lock(&dev->cap_mask_mutex);
844e126ba97SEli Cohen 
845e126ba97SEli Cohen 	err = mlx5_ib_query_port(ibdev, port, &attr);
846e126ba97SEli Cohen 	if (err)
847e126ba97SEli Cohen 		goto out;
848e126ba97SEli Cohen 
849e126ba97SEli Cohen 	tmp = (attr.port_cap_flags | props->set_port_cap_mask) &
850e126ba97SEli Cohen 		~props->clr_port_cap_mask;
851e126ba97SEli Cohen 
8529603b61dSJack Morgenstein 	err = mlx5_set_port_caps(dev->mdev, port, tmp);
853e126ba97SEli Cohen 
854e126ba97SEli Cohen out:
855e126ba97SEli Cohen 	mutex_unlock(&dev->cap_mask_mutex);
856e126ba97SEli Cohen 	return err;
857e126ba97SEli Cohen }
858e126ba97SEli Cohen 
859e126ba97SEli Cohen static struct ib_ucontext *mlx5_ib_alloc_ucontext(struct ib_device *ibdev,
860e126ba97SEli Cohen 						  struct ib_udata *udata)
861e126ba97SEli Cohen {
862e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
863b368d7cbSMatan Barak 	struct mlx5_ib_alloc_ucontext_req_v2 req = {};
864b368d7cbSMatan Barak 	struct mlx5_ib_alloc_ucontext_resp resp = {};
865e126ba97SEli Cohen 	struct mlx5_ib_ucontext *context;
866e126ba97SEli Cohen 	struct mlx5_uuar_info *uuari;
867e126ba97SEli Cohen 	struct mlx5_uar *uars;
868c1be5232SEli Cohen 	int gross_uuars;
869e126ba97SEli Cohen 	int num_uars;
87078c0f98cSEli Cohen 	int ver;
871e126ba97SEli Cohen 	int uuarn;
872e126ba97SEli Cohen 	int err;
873e126ba97SEli Cohen 	int i;
874f241e749SJack Morgenstein 	size_t reqlen;
875a168a41cSMajd Dibbiny 	size_t min_req_v2 = offsetof(struct mlx5_ib_alloc_ucontext_req_v2,
876a168a41cSMajd Dibbiny 				     max_cqe_version);
877e126ba97SEli Cohen 
878e126ba97SEli Cohen 	if (!dev->ib_active)
879e126ba97SEli Cohen 		return ERR_PTR(-EAGAIN);
880e126ba97SEli Cohen 
881dfbee859SHaggai Abramovsky 	if (udata->inlen < sizeof(struct ib_uverbs_cmd_hdr))
882dfbee859SHaggai Abramovsky 		return ERR_PTR(-EINVAL);
883dfbee859SHaggai Abramovsky 
88478c0f98cSEli Cohen 	reqlen = udata->inlen - sizeof(struct ib_uverbs_cmd_hdr);
88578c0f98cSEli Cohen 	if (reqlen == sizeof(struct mlx5_ib_alloc_ucontext_req))
88678c0f98cSEli Cohen 		ver = 0;
887a168a41cSMajd Dibbiny 	else if (reqlen >= min_req_v2)
88878c0f98cSEli Cohen 		ver = 2;
88978c0f98cSEli Cohen 	else
89078c0f98cSEli Cohen 		return ERR_PTR(-EINVAL);
89178c0f98cSEli Cohen 
892b368d7cbSMatan Barak 	err = ib_copy_from_udata(&req, udata, min(reqlen, sizeof(req)));
893e126ba97SEli Cohen 	if (err)
894e126ba97SEli Cohen 		return ERR_PTR(err);
895e126ba97SEli Cohen 
896b368d7cbSMatan Barak 	if (req.flags)
89778c0f98cSEli Cohen 		return ERR_PTR(-EINVAL);
89878c0f98cSEli Cohen 
899e126ba97SEli Cohen 	if (req.total_num_uuars > MLX5_MAX_UUARS)
900e126ba97SEli Cohen 		return ERR_PTR(-ENOMEM);
901e126ba97SEli Cohen 
902e126ba97SEli Cohen 	if (req.total_num_uuars == 0)
903e126ba97SEli Cohen 		return ERR_PTR(-EINVAL);
904e126ba97SEli Cohen 
905f72300c5SHaggai Abramovsky 	if (req.comp_mask || req.reserved0 || req.reserved1 || req.reserved2)
906b368d7cbSMatan Barak 		return ERR_PTR(-EOPNOTSUPP);
907b368d7cbSMatan Barak 
908b368d7cbSMatan Barak 	if (reqlen > sizeof(req) &&
909b368d7cbSMatan Barak 	    !ib_is_udata_cleared(udata, sizeof(req),
910dfbee859SHaggai Abramovsky 				 reqlen - sizeof(req)))
911b368d7cbSMatan Barak 		return ERR_PTR(-EOPNOTSUPP);
912b368d7cbSMatan Barak 
913c1be5232SEli Cohen 	req.total_num_uuars = ALIGN(req.total_num_uuars,
914c1be5232SEli Cohen 				    MLX5_NON_FP_BF_REGS_PER_PAGE);
915e126ba97SEli Cohen 	if (req.num_low_latency_uuars > req.total_num_uuars - 1)
916e126ba97SEli Cohen 		return ERR_PTR(-EINVAL);
917e126ba97SEli Cohen 
918c1be5232SEli Cohen 	num_uars = req.total_num_uuars / MLX5_NON_FP_BF_REGS_PER_PAGE;
919c1be5232SEli Cohen 	gross_uuars = num_uars * MLX5_BF_REGS_PER_PAGE;
920938fe83cSSaeed Mahameed 	resp.qp_tab_size = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp);
921938fe83cSSaeed Mahameed 	resp.bf_reg_size = 1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size);
922e126ba97SEli Cohen 	resp.cache_line_size = L1_CACHE_BYTES;
923938fe83cSSaeed Mahameed 	resp.max_sq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq);
924938fe83cSSaeed Mahameed 	resp.max_rq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq);
925938fe83cSSaeed Mahameed 	resp.max_send_wqebb = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
926938fe83cSSaeed Mahameed 	resp.max_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
927938fe83cSSaeed Mahameed 	resp.max_srq_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz);
928f72300c5SHaggai Abramovsky 	resp.cqe_version = min_t(__u8,
929f72300c5SHaggai Abramovsky 				 (__u8)MLX5_CAP_GEN(dev->mdev, cqe_version),
930f72300c5SHaggai Abramovsky 				 req.max_cqe_version);
931b368d7cbSMatan Barak 	resp.response_length = min(offsetof(typeof(resp), response_length) +
932b368d7cbSMatan Barak 				   sizeof(resp.response_length), udata->outlen);
933e126ba97SEli Cohen 
934e126ba97SEli Cohen 	context = kzalloc(sizeof(*context), GFP_KERNEL);
935e126ba97SEli Cohen 	if (!context)
936e126ba97SEli Cohen 		return ERR_PTR(-ENOMEM);
937e126ba97SEli Cohen 
938e126ba97SEli Cohen 	uuari = &context->uuari;
939e126ba97SEli Cohen 	mutex_init(&uuari->lock);
940e126ba97SEli Cohen 	uars = kcalloc(num_uars, sizeof(*uars), GFP_KERNEL);
941e126ba97SEli Cohen 	if (!uars) {
942e126ba97SEli Cohen 		err = -ENOMEM;
943e126ba97SEli Cohen 		goto out_ctx;
944e126ba97SEli Cohen 	}
945e126ba97SEli Cohen 
946c1be5232SEli Cohen 	uuari->bitmap = kcalloc(BITS_TO_LONGS(gross_uuars),
947e126ba97SEli Cohen 				sizeof(*uuari->bitmap),
948e126ba97SEli Cohen 				GFP_KERNEL);
949e126ba97SEli Cohen 	if (!uuari->bitmap) {
950e126ba97SEli Cohen 		err = -ENOMEM;
951e126ba97SEli Cohen 		goto out_uar_ctx;
952e126ba97SEli Cohen 	}
953e126ba97SEli Cohen 	/*
954e126ba97SEli Cohen 	 * clear all fast path uuars
955e126ba97SEli Cohen 	 */
956c1be5232SEli Cohen 	for (i = 0; i < gross_uuars; i++) {
957e126ba97SEli Cohen 		uuarn = i & 3;
958e126ba97SEli Cohen 		if (uuarn == 2 || uuarn == 3)
959e126ba97SEli Cohen 			set_bit(i, uuari->bitmap);
960e126ba97SEli Cohen 	}
961e126ba97SEli Cohen 
962c1be5232SEli Cohen 	uuari->count = kcalloc(gross_uuars, sizeof(*uuari->count), GFP_KERNEL);
963e126ba97SEli Cohen 	if (!uuari->count) {
964e126ba97SEli Cohen 		err = -ENOMEM;
965e126ba97SEli Cohen 		goto out_bitmap;
966e126ba97SEli Cohen 	}
967e126ba97SEli Cohen 
968e126ba97SEli Cohen 	for (i = 0; i < num_uars; i++) {
9699603b61dSJack Morgenstein 		err = mlx5_cmd_alloc_uar(dev->mdev, &uars[i].index);
970e126ba97SEli Cohen 		if (err)
971e126ba97SEli Cohen 			goto out_count;
972e126ba97SEli Cohen 	}
973e126ba97SEli Cohen 
974b4cfe447SHaggai Eran #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
975b4cfe447SHaggai Eran 	context->ibucontext.invalidate_range = &mlx5_ib_invalidate_range;
976b4cfe447SHaggai Eran #endif
977b4cfe447SHaggai Eran 
978146d2f1aSmajd@mellanox.com 	if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain)) {
979146d2f1aSmajd@mellanox.com 		err = mlx5_core_alloc_transport_domain(dev->mdev,
980146d2f1aSmajd@mellanox.com 						       &context->tdn);
981146d2f1aSmajd@mellanox.com 		if (err)
982146d2f1aSmajd@mellanox.com 			goto out_uars;
983146d2f1aSmajd@mellanox.com 	}
984146d2f1aSmajd@mellanox.com 
985e126ba97SEli Cohen 	INIT_LIST_HEAD(&context->db_page_list);
986e126ba97SEli Cohen 	mutex_init(&context->db_page_mutex);
987e126ba97SEli Cohen 
988e126ba97SEli Cohen 	resp.tot_uuars = req.total_num_uuars;
989938fe83cSSaeed Mahameed 	resp.num_ports = MLX5_CAP_GEN(dev->mdev, num_ports);
990b368d7cbSMatan Barak 
991f72300c5SHaggai Abramovsky 	if (field_avail(typeof(resp), cqe_version, udata->outlen))
992f72300c5SHaggai Abramovsky 		resp.response_length += sizeof(resp.cqe_version);
993b368d7cbSMatan Barak 
994b368d7cbSMatan Barak 	if (field_avail(typeof(resp), hca_core_clock_offset, udata->outlen)) {
995b368d7cbSMatan Barak 		resp.comp_mask |=
996b368d7cbSMatan Barak 			MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_CORE_CLOCK_OFFSET;
997b368d7cbSMatan Barak 		resp.hca_core_clock_offset =
998b368d7cbSMatan Barak 			offsetof(struct mlx5_init_seg, internal_timer_h) %
999b368d7cbSMatan Barak 			PAGE_SIZE;
1000f72300c5SHaggai Abramovsky 		resp.response_length += sizeof(resp.hca_core_clock_offset) +
1001f72300c5SHaggai Abramovsky 					sizeof(resp.reserved2) +
1002f72300c5SHaggai Abramovsky 					sizeof(resp.reserved3);
1003b368d7cbSMatan Barak 	}
1004b368d7cbSMatan Barak 
1005b368d7cbSMatan Barak 	err = ib_copy_to_udata(udata, &resp, resp.response_length);
1006e126ba97SEli Cohen 	if (err)
1007146d2f1aSmajd@mellanox.com 		goto out_td;
1008e126ba97SEli Cohen 
100978c0f98cSEli Cohen 	uuari->ver = ver;
1010e126ba97SEli Cohen 	uuari->num_low_latency_uuars = req.num_low_latency_uuars;
1011e126ba97SEli Cohen 	uuari->uars = uars;
1012e126ba97SEli Cohen 	uuari->num_uars = num_uars;
1013f72300c5SHaggai Abramovsky 	context->cqe_version = resp.cqe_version;
1014f72300c5SHaggai Abramovsky 
1015e126ba97SEli Cohen 	return &context->ibucontext;
1016e126ba97SEli Cohen 
1017146d2f1aSmajd@mellanox.com out_td:
1018146d2f1aSmajd@mellanox.com 	if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1019146d2f1aSmajd@mellanox.com 		mlx5_core_dealloc_transport_domain(dev->mdev, context->tdn);
1020146d2f1aSmajd@mellanox.com 
1021e126ba97SEli Cohen out_uars:
1022e126ba97SEli Cohen 	for (i--; i >= 0; i--)
10239603b61dSJack Morgenstein 		mlx5_cmd_free_uar(dev->mdev, uars[i].index);
1024e126ba97SEli Cohen out_count:
1025e126ba97SEli Cohen 	kfree(uuari->count);
1026e126ba97SEli Cohen 
1027e126ba97SEli Cohen out_bitmap:
1028e126ba97SEli Cohen 	kfree(uuari->bitmap);
1029e126ba97SEli Cohen 
1030e126ba97SEli Cohen out_uar_ctx:
1031e126ba97SEli Cohen 	kfree(uars);
1032e126ba97SEli Cohen 
1033e126ba97SEli Cohen out_ctx:
1034e126ba97SEli Cohen 	kfree(context);
1035e126ba97SEli Cohen 	return ERR_PTR(err);
1036e126ba97SEli Cohen }
1037e126ba97SEli Cohen 
1038e126ba97SEli Cohen static int mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
1039e126ba97SEli Cohen {
1040e126ba97SEli Cohen 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1041e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
1042e126ba97SEli Cohen 	struct mlx5_uuar_info *uuari = &context->uuari;
1043e126ba97SEli Cohen 	int i;
1044e126ba97SEli Cohen 
1045146d2f1aSmajd@mellanox.com 	if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1046146d2f1aSmajd@mellanox.com 		mlx5_core_dealloc_transport_domain(dev->mdev, context->tdn);
1047146d2f1aSmajd@mellanox.com 
1048e126ba97SEli Cohen 	for (i = 0; i < uuari->num_uars; i++) {
10499603b61dSJack Morgenstein 		if (mlx5_cmd_free_uar(dev->mdev, uuari->uars[i].index))
1050e126ba97SEli Cohen 			mlx5_ib_warn(dev, "failed to free UAR 0x%x\n", uuari->uars[i].index);
1051e126ba97SEli Cohen 	}
1052e126ba97SEli Cohen 
1053e126ba97SEli Cohen 	kfree(uuari->count);
1054e126ba97SEli Cohen 	kfree(uuari->bitmap);
1055e126ba97SEli Cohen 	kfree(uuari->uars);
1056e126ba97SEli Cohen 	kfree(context);
1057e126ba97SEli Cohen 
1058e126ba97SEli Cohen 	return 0;
1059e126ba97SEli Cohen }
1060e126ba97SEli Cohen 
1061e126ba97SEli Cohen static phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev, int index)
1062e126ba97SEli Cohen {
10639603b61dSJack Morgenstein 	return (pci_resource_start(dev->mdev->pdev, 0) >> PAGE_SHIFT) + index;
1064e126ba97SEli Cohen }
1065e126ba97SEli Cohen 
1066e126ba97SEli Cohen static int get_command(unsigned long offset)
1067e126ba97SEli Cohen {
1068e126ba97SEli Cohen 	return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK;
1069e126ba97SEli Cohen }
1070e126ba97SEli Cohen 
1071e126ba97SEli Cohen static int get_arg(unsigned long offset)
1072e126ba97SEli Cohen {
1073e126ba97SEli Cohen 	return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1);
1074e126ba97SEli Cohen }
1075e126ba97SEli Cohen 
1076e126ba97SEli Cohen static int get_index(unsigned long offset)
1077e126ba97SEli Cohen {
1078e126ba97SEli Cohen 	return get_arg(offset);
1079e126ba97SEli Cohen }
1080e126ba97SEli Cohen 
108137aa5c36SGuy Levi static inline char *mmap_cmd2str(enum mlx5_ib_mmap_cmd cmd)
1082e126ba97SEli Cohen {
108337aa5c36SGuy Levi 	switch (cmd) {
108437aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
108537aa5c36SGuy Levi 		return "WC";
1086e126ba97SEli Cohen 	case MLX5_IB_MMAP_REGULAR_PAGE:
108737aa5c36SGuy Levi 		return "best effort WC";
108837aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
108937aa5c36SGuy Levi 		return "NC";
109037aa5c36SGuy Levi 	default:
109137aa5c36SGuy Levi 		return NULL;
109237aa5c36SGuy Levi 	}
109337aa5c36SGuy Levi }
109437aa5c36SGuy Levi 
109537aa5c36SGuy Levi static int uar_mmap(struct mlx5_ib_dev *dev, enum mlx5_ib_mmap_cmd cmd,
109637aa5c36SGuy Levi 		    struct vm_area_struct *vma, struct mlx5_uuar_info *uuari)
109737aa5c36SGuy Levi {
109837aa5c36SGuy Levi 	int err;
109937aa5c36SGuy Levi 	unsigned long idx;
110037aa5c36SGuy Levi 	phys_addr_t pfn, pa;
110137aa5c36SGuy Levi 	pgprot_t prot;
110237aa5c36SGuy Levi 
110337aa5c36SGuy Levi 	switch (cmd) {
110437aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
110537aa5c36SGuy Levi /* Some architectures don't support WC memory */
110637aa5c36SGuy Levi #if defined(CONFIG_X86)
110737aa5c36SGuy Levi 		if (!pat_enabled())
110837aa5c36SGuy Levi 			return -EPERM;
110937aa5c36SGuy Levi #elif !(defined(CONFIG_PPC) || (defined(CONFIG_ARM) && defined(CONFIG_MMU)))
111037aa5c36SGuy Levi 			return -EPERM;
111137aa5c36SGuy Levi #endif
111237aa5c36SGuy Levi 	/* fall through */
111337aa5c36SGuy Levi 	case MLX5_IB_MMAP_REGULAR_PAGE:
111437aa5c36SGuy Levi 		/* For MLX5_IB_MMAP_REGULAR_PAGE do the best effort to get WC */
111537aa5c36SGuy Levi 		prot = pgprot_writecombine(vma->vm_page_prot);
111637aa5c36SGuy Levi 		break;
111737aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
111837aa5c36SGuy Levi 		prot = pgprot_noncached(vma->vm_page_prot);
111937aa5c36SGuy Levi 		break;
112037aa5c36SGuy Levi 	default:
112137aa5c36SGuy Levi 		return -EINVAL;
112237aa5c36SGuy Levi 	}
112337aa5c36SGuy Levi 
1124e126ba97SEli Cohen 	if (vma->vm_end - vma->vm_start != PAGE_SIZE)
1125e126ba97SEli Cohen 		return -EINVAL;
1126e126ba97SEli Cohen 
1127e126ba97SEli Cohen 	idx = get_index(vma->vm_pgoff);
11281c3ce90dSEli Cohen 	if (idx >= uuari->num_uars)
11291c3ce90dSEli Cohen 		return -EINVAL;
11301c3ce90dSEli Cohen 
1131e126ba97SEli Cohen 	pfn = uar_index2pfn(dev, uuari->uars[idx].index);
113237aa5c36SGuy Levi 	mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn %pa\n", idx, &pfn);
1133e126ba97SEli Cohen 
113437aa5c36SGuy Levi 	vma->vm_page_prot = prot;
113537aa5c36SGuy Levi 	err = io_remap_pfn_range(vma, vma->vm_start, pfn,
113637aa5c36SGuy Levi 				 PAGE_SIZE, vma->vm_page_prot);
113737aa5c36SGuy Levi 	if (err) {
113837aa5c36SGuy Levi 		mlx5_ib_err(dev, "io_remap_pfn_range failed with error=%d, vm_start=0x%lx, pfn=%pa, mmap_cmd=%s\n",
113937aa5c36SGuy Levi 			    err, vma->vm_start, &pfn, mmap_cmd2str(cmd));
1140e126ba97SEli Cohen 		return -EAGAIN;
114137aa5c36SGuy Levi 	}
1142e126ba97SEli Cohen 
114337aa5c36SGuy Levi 	pa = pfn << PAGE_SHIFT;
114437aa5c36SGuy Levi 	mlx5_ib_dbg(dev, "mapped %s at 0x%lx, PA %pa\n", mmap_cmd2str(cmd),
114537aa5c36SGuy Levi 		    vma->vm_start, &pa);
114637aa5c36SGuy Levi 
114737aa5c36SGuy Levi 	return 0;
114837aa5c36SGuy Levi }
114937aa5c36SGuy Levi 
115037aa5c36SGuy Levi static int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma)
115137aa5c36SGuy Levi {
115237aa5c36SGuy Levi 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
115337aa5c36SGuy Levi 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
115437aa5c36SGuy Levi 	struct mlx5_uuar_info *uuari = &context->uuari;
115537aa5c36SGuy Levi 	unsigned long command;
115637aa5c36SGuy Levi 	phys_addr_t pfn;
115737aa5c36SGuy Levi 
115837aa5c36SGuy Levi 	command = get_command(vma->vm_pgoff);
115937aa5c36SGuy Levi 	switch (command) {
116037aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
116137aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
116237aa5c36SGuy Levi 	case MLX5_IB_MMAP_REGULAR_PAGE:
116337aa5c36SGuy Levi 		return uar_mmap(dev, command, vma, uuari);
1164e126ba97SEli Cohen 
1165e126ba97SEli Cohen 	case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES:
1166e126ba97SEli Cohen 		return -ENOSYS;
1167e126ba97SEli Cohen 
1168d69e3bcfSMatan Barak 	case MLX5_IB_MMAP_CORE_CLOCK:
1169d69e3bcfSMatan Barak 		if (vma->vm_end - vma->vm_start != PAGE_SIZE)
1170d69e3bcfSMatan Barak 			return -EINVAL;
1171d69e3bcfSMatan Barak 
11726cbac1e4SMatan Barak 		if (vma->vm_flags & VM_WRITE)
1173d69e3bcfSMatan Barak 			return -EPERM;
1174d69e3bcfSMatan Barak 
1175d69e3bcfSMatan Barak 		/* Don't expose to user-space information it shouldn't have */
1176d69e3bcfSMatan Barak 		if (PAGE_SIZE > 4096)
1177d69e3bcfSMatan Barak 			return -EOPNOTSUPP;
1178d69e3bcfSMatan Barak 
1179d69e3bcfSMatan Barak 		vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1180d69e3bcfSMatan Barak 		pfn = (dev->mdev->iseg_base +
1181d69e3bcfSMatan Barak 		       offsetof(struct mlx5_init_seg, internal_timer_h)) >>
1182d69e3bcfSMatan Barak 			PAGE_SHIFT;
1183d69e3bcfSMatan Barak 		if (io_remap_pfn_range(vma, vma->vm_start, pfn,
1184d69e3bcfSMatan Barak 				       PAGE_SIZE, vma->vm_page_prot))
1185d69e3bcfSMatan Barak 			return -EAGAIN;
1186d69e3bcfSMatan Barak 
1187d69e3bcfSMatan Barak 		mlx5_ib_dbg(dev, "mapped internal timer at 0x%lx, PA 0x%llx\n",
1188d69e3bcfSMatan Barak 			    vma->vm_start,
1189d69e3bcfSMatan Barak 			    (unsigned long long)pfn << PAGE_SHIFT);
1190d69e3bcfSMatan Barak 		break;
1191d69e3bcfSMatan Barak 
1192e126ba97SEli Cohen 	default:
1193e126ba97SEli Cohen 		return -EINVAL;
1194e126ba97SEli Cohen 	}
1195e126ba97SEli Cohen 
1196e126ba97SEli Cohen 	return 0;
1197e126ba97SEli Cohen }
1198e126ba97SEli Cohen 
1199e126ba97SEli Cohen static struct ib_pd *mlx5_ib_alloc_pd(struct ib_device *ibdev,
1200e126ba97SEli Cohen 				      struct ib_ucontext *context,
1201e126ba97SEli Cohen 				      struct ib_udata *udata)
1202e126ba97SEli Cohen {
1203e126ba97SEli Cohen 	struct mlx5_ib_alloc_pd_resp resp;
1204e126ba97SEli Cohen 	struct mlx5_ib_pd *pd;
1205e126ba97SEli Cohen 	int err;
1206e126ba97SEli Cohen 
1207e126ba97SEli Cohen 	pd = kmalloc(sizeof(*pd), GFP_KERNEL);
1208e126ba97SEli Cohen 	if (!pd)
1209e126ba97SEli Cohen 		return ERR_PTR(-ENOMEM);
1210e126ba97SEli Cohen 
12119603b61dSJack Morgenstein 	err = mlx5_core_alloc_pd(to_mdev(ibdev)->mdev, &pd->pdn);
1212e126ba97SEli Cohen 	if (err) {
1213e126ba97SEli Cohen 		kfree(pd);
1214e126ba97SEli Cohen 		return ERR_PTR(err);
1215e126ba97SEli Cohen 	}
1216e126ba97SEli Cohen 
1217e126ba97SEli Cohen 	if (context) {
1218e126ba97SEli Cohen 		resp.pdn = pd->pdn;
1219e126ba97SEli Cohen 		if (ib_copy_to_udata(udata, &resp, sizeof(resp))) {
12209603b61dSJack Morgenstein 			mlx5_core_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn);
1221e126ba97SEli Cohen 			kfree(pd);
1222e126ba97SEli Cohen 			return ERR_PTR(-EFAULT);
1223e126ba97SEli Cohen 		}
1224e126ba97SEli Cohen 	}
1225e126ba97SEli Cohen 
1226e126ba97SEli Cohen 	return &pd->ibpd;
1227e126ba97SEli Cohen }
1228e126ba97SEli Cohen 
1229e126ba97SEli Cohen static int mlx5_ib_dealloc_pd(struct ib_pd *pd)
1230e126ba97SEli Cohen {
1231e126ba97SEli Cohen 	struct mlx5_ib_dev *mdev = to_mdev(pd->device);
1232e126ba97SEli Cohen 	struct mlx5_ib_pd *mpd = to_mpd(pd);
1233e126ba97SEli Cohen 
12349603b61dSJack Morgenstein 	mlx5_core_dealloc_pd(mdev->mdev, mpd->pdn);
1235e126ba97SEli Cohen 	kfree(mpd);
1236e126ba97SEli Cohen 
1237e126ba97SEli Cohen 	return 0;
1238e126ba97SEli Cohen }
1239e126ba97SEli Cohen 
1240038d2ef8SMaor Gottlieb static bool outer_header_zero(u32 *match_criteria)
1241038d2ef8SMaor Gottlieb {
1242038d2ef8SMaor Gottlieb 	int size = MLX5_ST_SZ_BYTES(fte_match_param);
1243038d2ef8SMaor Gottlieb 	char *outer_headers_c = MLX5_ADDR_OF(fte_match_param, match_criteria,
1244038d2ef8SMaor Gottlieb 					     outer_headers);
1245038d2ef8SMaor Gottlieb 
1246038d2ef8SMaor Gottlieb 	return outer_headers_c[0] == 0 && !memcmp(outer_headers_c,
1247038d2ef8SMaor Gottlieb 						  outer_headers_c + 1,
1248038d2ef8SMaor Gottlieb 						  size - 1);
1249038d2ef8SMaor Gottlieb }
1250038d2ef8SMaor Gottlieb 
1251038d2ef8SMaor Gottlieb static int parse_flow_attr(u32 *match_c, u32 *match_v,
1252038d2ef8SMaor Gottlieb 			   union ib_flow_spec *ib_spec)
1253038d2ef8SMaor Gottlieb {
1254038d2ef8SMaor Gottlieb 	void *outer_headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
1255038d2ef8SMaor Gottlieb 					     outer_headers);
1256038d2ef8SMaor Gottlieb 	void *outer_headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
1257038d2ef8SMaor Gottlieb 					     outer_headers);
1258038d2ef8SMaor Gottlieb 	switch (ib_spec->type) {
1259038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_ETH:
1260038d2ef8SMaor Gottlieb 		if (ib_spec->size != sizeof(ib_spec->eth))
1261038d2ef8SMaor Gottlieb 			return -EINVAL;
1262038d2ef8SMaor Gottlieb 
1263038d2ef8SMaor Gottlieb 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_c,
1264038d2ef8SMaor Gottlieb 					     dmac_47_16),
1265038d2ef8SMaor Gottlieb 				ib_spec->eth.mask.dst_mac);
1266038d2ef8SMaor Gottlieb 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_v,
1267038d2ef8SMaor Gottlieb 					     dmac_47_16),
1268038d2ef8SMaor Gottlieb 				ib_spec->eth.val.dst_mac);
1269038d2ef8SMaor Gottlieb 
1270038d2ef8SMaor Gottlieb 		if (ib_spec->eth.mask.vlan_tag) {
1271038d2ef8SMaor Gottlieb 			MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c,
1272038d2ef8SMaor Gottlieb 				 vlan_tag, 1);
1273038d2ef8SMaor Gottlieb 			MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v,
1274038d2ef8SMaor Gottlieb 				 vlan_tag, 1);
1275038d2ef8SMaor Gottlieb 
1276038d2ef8SMaor Gottlieb 			MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c,
1277038d2ef8SMaor Gottlieb 				 first_vid, ntohs(ib_spec->eth.mask.vlan_tag));
1278038d2ef8SMaor Gottlieb 			MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v,
1279038d2ef8SMaor Gottlieb 				 first_vid, ntohs(ib_spec->eth.val.vlan_tag));
1280038d2ef8SMaor Gottlieb 
1281038d2ef8SMaor Gottlieb 			MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c,
1282038d2ef8SMaor Gottlieb 				 first_cfi,
1283038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.mask.vlan_tag) >> 12);
1284038d2ef8SMaor Gottlieb 			MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v,
1285038d2ef8SMaor Gottlieb 				 first_cfi,
1286038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.val.vlan_tag) >> 12);
1287038d2ef8SMaor Gottlieb 
1288038d2ef8SMaor Gottlieb 			MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c,
1289038d2ef8SMaor Gottlieb 				 first_prio,
1290038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.mask.vlan_tag) >> 13);
1291038d2ef8SMaor Gottlieb 			MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v,
1292038d2ef8SMaor Gottlieb 				 first_prio,
1293038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.val.vlan_tag) >> 13);
1294038d2ef8SMaor Gottlieb 		}
1295038d2ef8SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c,
1296038d2ef8SMaor Gottlieb 			 ethertype, ntohs(ib_spec->eth.mask.ether_type));
1297038d2ef8SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v,
1298038d2ef8SMaor Gottlieb 			 ethertype, ntohs(ib_spec->eth.val.ether_type));
1299038d2ef8SMaor Gottlieb 		break;
1300038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_IPV4:
1301038d2ef8SMaor Gottlieb 		if (ib_spec->size != sizeof(ib_spec->ipv4))
1302038d2ef8SMaor Gottlieb 			return -EINVAL;
1303038d2ef8SMaor Gottlieb 
1304038d2ef8SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c,
1305038d2ef8SMaor Gottlieb 			 ethertype, 0xffff);
1306038d2ef8SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v,
1307038d2ef8SMaor Gottlieb 			 ethertype, ETH_P_IP);
1308038d2ef8SMaor Gottlieb 
1309038d2ef8SMaor Gottlieb 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_c,
1310038d2ef8SMaor Gottlieb 				    src_ipv4_src_ipv6.ipv4_layout.ipv4),
1311038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.mask.src_ip,
1312038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.mask.src_ip));
1313038d2ef8SMaor Gottlieb 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_v,
1314038d2ef8SMaor Gottlieb 				    src_ipv4_src_ipv6.ipv4_layout.ipv4),
1315038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.val.src_ip,
1316038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.val.src_ip));
1317038d2ef8SMaor Gottlieb 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_c,
1318038d2ef8SMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
1319038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.mask.dst_ip,
1320038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.mask.dst_ip));
1321038d2ef8SMaor Gottlieb 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_v,
1322038d2ef8SMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
1323038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.val.dst_ip,
1324038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.val.dst_ip));
1325038d2ef8SMaor Gottlieb 		break;
1326038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_TCP:
1327038d2ef8SMaor Gottlieb 		if (ib_spec->size != sizeof(ib_spec->tcp_udp))
1328038d2ef8SMaor Gottlieb 			return -EINVAL;
1329038d2ef8SMaor Gottlieb 
1330038d2ef8SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, ip_protocol,
1331038d2ef8SMaor Gottlieb 			 0xff);
1332038d2ef8SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, ip_protocol,
1333038d2ef8SMaor Gottlieb 			 IPPROTO_TCP);
1334038d2ef8SMaor Gottlieb 
1335038d2ef8SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, tcp_sport,
1336038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.src_port));
1337038d2ef8SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, tcp_sport,
1338038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.src_port));
1339038d2ef8SMaor Gottlieb 
1340038d2ef8SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, tcp_dport,
1341038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.dst_port));
1342038d2ef8SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, tcp_dport,
1343038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.dst_port));
1344038d2ef8SMaor Gottlieb 		break;
1345038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_UDP:
1346038d2ef8SMaor Gottlieb 		if (ib_spec->size != sizeof(ib_spec->tcp_udp))
1347038d2ef8SMaor Gottlieb 			return -EINVAL;
1348038d2ef8SMaor Gottlieb 
1349038d2ef8SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, ip_protocol,
1350038d2ef8SMaor Gottlieb 			 0xff);
1351038d2ef8SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, ip_protocol,
1352038d2ef8SMaor Gottlieb 			 IPPROTO_UDP);
1353038d2ef8SMaor Gottlieb 
1354038d2ef8SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, udp_sport,
1355038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.src_port));
1356038d2ef8SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, udp_sport,
1357038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.src_port));
1358038d2ef8SMaor Gottlieb 
1359038d2ef8SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, udp_dport,
1360038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.dst_port));
1361038d2ef8SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, udp_dport,
1362038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.dst_port));
1363038d2ef8SMaor Gottlieb 		break;
1364038d2ef8SMaor Gottlieb 	default:
1365038d2ef8SMaor Gottlieb 		return -EINVAL;
1366038d2ef8SMaor Gottlieb 	}
1367038d2ef8SMaor Gottlieb 
1368038d2ef8SMaor Gottlieb 	return 0;
1369038d2ef8SMaor Gottlieb }
1370038d2ef8SMaor Gottlieb 
1371038d2ef8SMaor Gottlieb /* If a flow could catch both multicast and unicast packets,
1372038d2ef8SMaor Gottlieb  * it won't fall into the multicast flow steering table and this rule
1373038d2ef8SMaor Gottlieb  * could steal other multicast packets.
1374038d2ef8SMaor Gottlieb  */
1375038d2ef8SMaor Gottlieb static bool flow_is_multicast_only(struct ib_flow_attr *ib_attr)
1376038d2ef8SMaor Gottlieb {
1377038d2ef8SMaor Gottlieb 	struct ib_flow_spec_eth *eth_spec;
1378038d2ef8SMaor Gottlieb 
1379038d2ef8SMaor Gottlieb 	if (ib_attr->type != IB_FLOW_ATTR_NORMAL ||
1380038d2ef8SMaor Gottlieb 	    ib_attr->size < sizeof(struct ib_flow_attr) +
1381038d2ef8SMaor Gottlieb 	    sizeof(struct ib_flow_spec_eth) ||
1382038d2ef8SMaor Gottlieb 	    ib_attr->num_of_specs < 1)
1383038d2ef8SMaor Gottlieb 		return false;
1384038d2ef8SMaor Gottlieb 
1385038d2ef8SMaor Gottlieb 	eth_spec = (struct ib_flow_spec_eth *)(ib_attr + 1);
1386038d2ef8SMaor Gottlieb 	if (eth_spec->type != IB_FLOW_SPEC_ETH ||
1387038d2ef8SMaor Gottlieb 	    eth_spec->size != sizeof(*eth_spec))
1388038d2ef8SMaor Gottlieb 		return false;
1389038d2ef8SMaor Gottlieb 
1390038d2ef8SMaor Gottlieb 	return is_multicast_ether_addr(eth_spec->mask.dst_mac) &&
1391038d2ef8SMaor Gottlieb 	       is_multicast_ether_addr(eth_spec->val.dst_mac);
1392038d2ef8SMaor Gottlieb }
1393038d2ef8SMaor Gottlieb 
1394038d2ef8SMaor Gottlieb static bool is_valid_attr(struct ib_flow_attr *flow_attr)
1395038d2ef8SMaor Gottlieb {
1396038d2ef8SMaor Gottlieb 	union ib_flow_spec *ib_spec = (union ib_flow_spec *)(flow_attr + 1);
1397038d2ef8SMaor Gottlieb 	bool has_ipv4_spec = false;
1398038d2ef8SMaor Gottlieb 	bool eth_type_ipv4 = true;
1399038d2ef8SMaor Gottlieb 	unsigned int spec_index;
1400038d2ef8SMaor Gottlieb 
1401038d2ef8SMaor Gottlieb 	/* Validate that ethertype is correct */
1402038d2ef8SMaor Gottlieb 	for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
1403038d2ef8SMaor Gottlieb 		if (ib_spec->type == IB_FLOW_SPEC_ETH &&
1404038d2ef8SMaor Gottlieb 		    ib_spec->eth.mask.ether_type) {
1405038d2ef8SMaor Gottlieb 			if (!((ib_spec->eth.mask.ether_type == htons(0xffff)) &&
1406038d2ef8SMaor Gottlieb 			      ib_spec->eth.val.ether_type == htons(ETH_P_IP)))
1407038d2ef8SMaor Gottlieb 				eth_type_ipv4 = false;
1408038d2ef8SMaor Gottlieb 		} else if (ib_spec->type == IB_FLOW_SPEC_IPV4) {
1409038d2ef8SMaor Gottlieb 			has_ipv4_spec = true;
1410038d2ef8SMaor Gottlieb 		}
1411038d2ef8SMaor Gottlieb 		ib_spec = (void *)ib_spec + ib_spec->size;
1412038d2ef8SMaor Gottlieb 	}
1413038d2ef8SMaor Gottlieb 	return !has_ipv4_spec || eth_type_ipv4;
1414038d2ef8SMaor Gottlieb }
1415038d2ef8SMaor Gottlieb 
1416038d2ef8SMaor Gottlieb static void put_flow_table(struct mlx5_ib_dev *dev,
1417038d2ef8SMaor Gottlieb 			   struct mlx5_ib_flow_prio *prio, bool ft_added)
1418038d2ef8SMaor Gottlieb {
1419038d2ef8SMaor Gottlieb 	prio->refcount -= !!ft_added;
1420038d2ef8SMaor Gottlieb 	if (!prio->refcount) {
1421038d2ef8SMaor Gottlieb 		mlx5_destroy_flow_table(prio->flow_table);
1422038d2ef8SMaor Gottlieb 		prio->flow_table = NULL;
1423038d2ef8SMaor Gottlieb 	}
1424038d2ef8SMaor Gottlieb }
1425038d2ef8SMaor Gottlieb 
1426038d2ef8SMaor Gottlieb static int mlx5_ib_destroy_flow(struct ib_flow *flow_id)
1427038d2ef8SMaor Gottlieb {
1428038d2ef8SMaor Gottlieb 	struct mlx5_ib_dev *dev = to_mdev(flow_id->qp->device);
1429038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = container_of(flow_id,
1430038d2ef8SMaor Gottlieb 							  struct mlx5_ib_flow_handler,
1431038d2ef8SMaor Gottlieb 							  ibflow);
1432038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *iter, *tmp;
1433038d2ef8SMaor Gottlieb 
1434038d2ef8SMaor Gottlieb 	mutex_lock(&dev->flow_db.lock);
1435038d2ef8SMaor Gottlieb 
1436038d2ef8SMaor Gottlieb 	list_for_each_entry_safe(iter, tmp, &handler->list, list) {
1437038d2ef8SMaor Gottlieb 		mlx5_del_flow_rule(iter->rule);
1438038d2ef8SMaor Gottlieb 		list_del(&iter->list);
1439038d2ef8SMaor Gottlieb 		kfree(iter);
1440038d2ef8SMaor Gottlieb 	}
1441038d2ef8SMaor Gottlieb 
1442038d2ef8SMaor Gottlieb 	mlx5_del_flow_rule(handler->rule);
1443038d2ef8SMaor Gottlieb 	put_flow_table(dev, &dev->flow_db.prios[handler->prio], true);
1444038d2ef8SMaor Gottlieb 	mutex_unlock(&dev->flow_db.lock);
1445038d2ef8SMaor Gottlieb 
1446038d2ef8SMaor Gottlieb 	kfree(handler);
1447038d2ef8SMaor Gottlieb 
1448038d2ef8SMaor Gottlieb 	return 0;
1449038d2ef8SMaor Gottlieb }
1450038d2ef8SMaor Gottlieb 
145135d19011SMaor Gottlieb static int ib_prio_to_core_prio(unsigned int priority, bool dont_trap)
145235d19011SMaor Gottlieb {
145335d19011SMaor Gottlieb 	priority *= 2;
145435d19011SMaor Gottlieb 	if (!dont_trap)
145535d19011SMaor Gottlieb 		priority++;
145635d19011SMaor Gottlieb 	return priority;
145735d19011SMaor Gottlieb }
145835d19011SMaor Gottlieb 
1459038d2ef8SMaor Gottlieb #define MLX5_FS_MAX_TYPES	 10
1460038d2ef8SMaor Gottlieb #define MLX5_FS_MAX_ENTRIES	 32000UL
1461038d2ef8SMaor Gottlieb static struct mlx5_ib_flow_prio *get_flow_table(struct mlx5_ib_dev *dev,
1462038d2ef8SMaor Gottlieb 						struct ib_flow_attr *flow_attr)
1463038d2ef8SMaor Gottlieb {
146435d19011SMaor Gottlieb 	bool dont_trap = flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP;
1465038d2ef8SMaor Gottlieb 	struct mlx5_flow_namespace *ns = NULL;
1466038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_prio *prio;
1467038d2ef8SMaor Gottlieb 	struct mlx5_flow_table *ft;
1468038d2ef8SMaor Gottlieb 	int num_entries;
1469038d2ef8SMaor Gottlieb 	int num_groups;
1470038d2ef8SMaor Gottlieb 	int priority;
1471038d2ef8SMaor Gottlieb 	int err = 0;
1472038d2ef8SMaor Gottlieb 
1473038d2ef8SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
147435d19011SMaor Gottlieb 		if (flow_is_multicast_only(flow_attr) &&
147535d19011SMaor Gottlieb 		    !dont_trap)
1476038d2ef8SMaor Gottlieb 			priority = MLX5_IB_FLOW_MCAST_PRIO;
1477038d2ef8SMaor Gottlieb 		else
147835d19011SMaor Gottlieb 			priority = ib_prio_to_core_prio(flow_attr->priority,
147935d19011SMaor Gottlieb 							dont_trap);
1480038d2ef8SMaor Gottlieb 		ns = mlx5_get_flow_namespace(dev->mdev,
1481038d2ef8SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_BYPASS);
1482038d2ef8SMaor Gottlieb 		num_entries = MLX5_FS_MAX_ENTRIES;
1483038d2ef8SMaor Gottlieb 		num_groups = MLX5_FS_MAX_TYPES;
1484038d2ef8SMaor Gottlieb 		prio = &dev->flow_db.prios[priority];
1485038d2ef8SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
1486038d2ef8SMaor Gottlieb 		   flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
1487038d2ef8SMaor Gottlieb 		ns = mlx5_get_flow_namespace(dev->mdev,
1488038d2ef8SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_LEFTOVERS);
1489038d2ef8SMaor Gottlieb 		build_leftovers_ft_param(&priority,
1490038d2ef8SMaor Gottlieb 					 &num_entries,
1491038d2ef8SMaor Gottlieb 					 &num_groups);
1492038d2ef8SMaor Gottlieb 		prio = &dev->flow_db.prios[MLX5_IB_FLOW_LEFTOVERS_PRIO];
1493038d2ef8SMaor Gottlieb 	}
1494038d2ef8SMaor Gottlieb 
1495038d2ef8SMaor Gottlieb 	if (!ns)
1496038d2ef8SMaor Gottlieb 		return ERR_PTR(-ENOTSUPP);
1497038d2ef8SMaor Gottlieb 
1498038d2ef8SMaor Gottlieb 	ft = prio->flow_table;
1499038d2ef8SMaor Gottlieb 	if (!ft) {
1500038d2ef8SMaor Gottlieb 		ft = mlx5_create_auto_grouped_flow_table(ns, priority,
1501038d2ef8SMaor Gottlieb 							 num_entries,
1502d63cd286SMaor Gottlieb 							 num_groups,
1503d63cd286SMaor Gottlieb 							 0);
1504038d2ef8SMaor Gottlieb 
1505038d2ef8SMaor Gottlieb 		if (!IS_ERR(ft)) {
1506038d2ef8SMaor Gottlieb 			prio->refcount = 0;
1507038d2ef8SMaor Gottlieb 			prio->flow_table = ft;
1508038d2ef8SMaor Gottlieb 		} else {
1509038d2ef8SMaor Gottlieb 			err = PTR_ERR(ft);
1510038d2ef8SMaor Gottlieb 		}
1511038d2ef8SMaor Gottlieb 	}
1512038d2ef8SMaor Gottlieb 
1513038d2ef8SMaor Gottlieb 	return err ? ERR_PTR(err) : prio;
1514038d2ef8SMaor Gottlieb }
1515038d2ef8SMaor Gottlieb 
1516038d2ef8SMaor Gottlieb static struct mlx5_ib_flow_handler *create_flow_rule(struct mlx5_ib_dev *dev,
1517038d2ef8SMaor Gottlieb 						     struct mlx5_ib_flow_prio *ft_prio,
1518038d2ef8SMaor Gottlieb 						     struct ib_flow_attr *flow_attr,
1519038d2ef8SMaor Gottlieb 						     struct mlx5_flow_destination *dst)
1520038d2ef8SMaor Gottlieb {
1521038d2ef8SMaor Gottlieb 	struct mlx5_flow_table	*ft = ft_prio->flow_table;
1522038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler;
1523038d2ef8SMaor Gottlieb 	void *ib_flow = flow_attr + 1;
1524038d2ef8SMaor Gottlieb 	u8 match_criteria_enable = 0;
1525038d2ef8SMaor Gottlieb 	unsigned int spec_index;
1526038d2ef8SMaor Gottlieb 	u32 *match_c;
1527038d2ef8SMaor Gottlieb 	u32 *match_v;
152835d19011SMaor Gottlieb 	u32 action;
1529038d2ef8SMaor Gottlieb 	int err = 0;
1530038d2ef8SMaor Gottlieb 
1531038d2ef8SMaor Gottlieb 	if (!is_valid_attr(flow_attr))
1532038d2ef8SMaor Gottlieb 		return ERR_PTR(-EINVAL);
1533038d2ef8SMaor Gottlieb 
1534038d2ef8SMaor Gottlieb 	match_c = kzalloc(MLX5_ST_SZ_BYTES(fte_match_param), GFP_KERNEL);
1535038d2ef8SMaor Gottlieb 	match_v = kzalloc(MLX5_ST_SZ_BYTES(fte_match_param), GFP_KERNEL);
1536038d2ef8SMaor Gottlieb 	handler = kzalloc(sizeof(*handler), GFP_KERNEL);
1537038d2ef8SMaor Gottlieb 	if (!handler || !match_c || !match_v) {
1538038d2ef8SMaor Gottlieb 		err = -ENOMEM;
1539038d2ef8SMaor Gottlieb 		goto free;
1540038d2ef8SMaor Gottlieb 	}
1541038d2ef8SMaor Gottlieb 
1542038d2ef8SMaor Gottlieb 	INIT_LIST_HEAD(&handler->list);
1543038d2ef8SMaor Gottlieb 
1544038d2ef8SMaor Gottlieb 	for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
1545038d2ef8SMaor Gottlieb 		err = parse_flow_attr(match_c, match_v, ib_flow);
1546038d2ef8SMaor Gottlieb 		if (err < 0)
1547038d2ef8SMaor Gottlieb 			goto free;
1548038d2ef8SMaor Gottlieb 
1549038d2ef8SMaor Gottlieb 		ib_flow += ((union ib_flow_spec *)ib_flow)->size;
1550038d2ef8SMaor Gottlieb 	}
1551038d2ef8SMaor Gottlieb 
1552038d2ef8SMaor Gottlieb 	/* Outer header support only */
1553038d2ef8SMaor Gottlieb 	match_criteria_enable = (!outer_header_zero(match_c)) << 0;
155435d19011SMaor Gottlieb 	action = dst ? MLX5_FLOW_CONTEXT_ACTION_FWD_DEST :
155535d19011SMaor Gottlieb 		MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO;
1556038d2ef8SMaor Gottlieb 	handler->rule = mlx5_add_flow_rule(ft, match_criteria_enable,
1557038d2ef8SMaor Gottlieb 					   match_c, match_v,
155835d19011SMaor Gottlieb 					   action,
1559038d2ef8SMaor Gottlieb 					   MLX5_FS_DEFAULT_FLOW_TAG,
1560038d2ef8SMaor Gottlieb 					   dst);
1561038d2ef8SMaor Gottlieb 
1562038d2ef8SMaor Gottlieb 	if (IS_ERR(handler->rule)) {
1563038d2ef8SMaor Gottlieb 		err = PTR_ERR(handler->rule);
1564038d2ef8SMaor Gottlieb 		goto free;
1565038d2ef8SMaor Gottlieb 	}
1566038d2ef8SMaor Gottlieb 
1567038d2ef8SMaor Gottlieb 	handler->prio = ft_prio - dev->flow_db.prios;
1568038d2ef8SMaor Gottlieb 
1569038d2ef8SMaor Gottlieb 	ft_prio->flow_table = ft;
1570038d2ef8SMaor Gottlieb free:
1571038d2ef8SMaor Gottlieb 	if (err)
1572038d2ef8SMaor Gottlieb 		kfree(handler);
1573038d2ef8SMaor Gottlieb 	kfree(match_c);
1574038d2ef8SMaor Gottlieb 	kfree(match_v);
1575038d2ef8SMaor Gottlieb 	return err ? ERR_PTR(err) : handler;
1576038d2ef8SMaor Gottlieb }
1577038d2ef8SMaor Gottlieb 
157835d19011SMaor Gottlieb static struct mlx5_ib_flow_handler *create_dont_trap_rule(struct mlx5_ib_dev *dev,
157935d19011SMaor Gottlieb 							  struct mlx5_ib_flow_prio *ft_prio,
158035d19011SMaor Gottlieb 							  struct ib_flow_attr *flow_attr,
158135d19011SMaor Gottlieb 							  struct mlx5_flow_destination *dst)
158235d19011SMaor Gottlieb {
158335d19011SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_dst = NULL;
158435d19011SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
158535d19011SMaor Gottlieb 
158635d19011SMaor Gottlieb 	handler = create_flow_rule(dev, ft_prio, flow_attr, NULL);
158735d19011SMaor Gottlieb 	if (!IS_ERR(handler)) {
158835d19011SMaor Gottlieb 		handler_dst = create_flow_rule(dev, ft_prio,
158935d19011SMaor Gottlieb 					       flow_attr, dst);
159035d19011SMaor Gottlieb 		if (IS_ERR(handler_dst)) {
159135d19011SMaor Gottlieb 			mlx5_del_flow_rule(handler->rule);
159235d19011SMaor Gottlieb 			kfree(handler);
159335d19011SMaor Gottlieb 			handler = handler_dst;
159435d19011SMaor Gottlieb 		} else {
159535d19011SMaor Gottlieb 			list_add(&handler_dst->list, &handler->list);
159635d19011SMaor Gottlieb 		}
159735d19011SMaor Gottlieb 	}
159835d19011SMaor Gottlieb 
159935d19011SMaor Gottlieb 	return handler;
160035d19011SMaor Gottlieb }
1601038d2ef8SMaor Gottlieb enum {
1602038d2ef8SMaor Gottlieb 	LEFTOVERS_MC,
1603038d2ef8SMaor Gottlieb 	LEFTOVERS_UC,
1604038d2ef8SMaor Gottlieb };
1605038d2ef8SMaor Gottlieb 
1606038d2ef8SMaor Gottlieb static struct mlx5_ib_flow_handler *create_leftovers_rule(struct mlx5_ib_dev *dev,
1607038d2ef8SMaor Gottlieb 							  struct mlx5_ib_flow_prio *ft_prio,
1608038d2ef8SMaor Gottlieb 							  struct ib_flow_attr *flow_attr,
1609038d2ef8SMaor Gottlieb 							  struct mlx5_flow_destination *dst)
1610038d2ef8SMaor Gottlieb {
1611038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_ucast = NULL;
1612038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
1613038d2ef8SMaor Gottlieb 
1614038d2ef8SMaor Gottlieb 	static struct {
1615038d2ef8SMaor Gottlieb 		struct ib_flow_attr	flow_attr;
1616038d2ef8SMaor Gottlieb 		struct ib_flow_spec_eth eth_flow;
1617038d2ef8SMaor Gottlieb 	} leftovers_specs[] = {
1618038d2ef8SMaor Gottlieb 		[LEFTOVERS_MC] = {
1619038d2ef8SMaor Gottlieb 			.flow_attr = {
1620038d2ef8SMaor Gottlieb 				.num_of_specs = 1,
1621038d2ef8SMaor Gottlieb 				.size = sizeof(leftovers_specs[0])
1622038d2ef8SMaor Gottlieb 			},
1623038d2ef8SMaor Gottlieb 			.eth_flow = {
1624038d2ef8SMaor Gottlieb 				.type = IB_FLOW_SPEC_ETH,
1625038d2ef8SMaor Gottlieb 				.size = sizeof(struct ib_flow_spec_eth),
1626038d2ef8SMaor Gottlieb 				.mask = {.dst_mac = {0x1} },
1627038d2ef8SMaor Gottlieb 				.val =  {.dst_mac = {0x1} }
1628038d2ef8SMaor Gottlieb 			}
1629038d2ef8SMaor Gottlieb 		},
1630038d2ef8SMaor Gottlieb 		[LEFTOVERS_UC] = {
1631038d2ef8SMaor Gottlieb 			.flow_attr = {
1632038d2ef8SMaor Gottlieb 				.num_of_specs = 1,
1633038d2ef8SMaor Gottlieb 				.size = sizeof(leftovers_specs[0])
1634038d2ef8SMaor Gottlieb 			},
1635038d2ef8SMaor Gottlieb 			.eth_flow = {
1636038d2ef8SMaor Gottlieb 				.type = IB_FLOW_SPEC_ETH,
1637038d2ef8SMaor Gottlieb 				.size = sizeof(struct ib_flow_spec_eth),
1638038d2ef8SMaor Gottlieb 				.mask = {.dst_mac = {0x1} },
1639038d2ef8SMaor Gottlieb 				.val = {.dst_mac = {} }
1640038d2ef8SMaor Gottlieb 			}
1641038d2ef8SMaor Gottlieb 		}
1642038d2ef8SMaor Gottlieb 	};
1643038d2ef8SMaor Gottlieb 
1644038d2ef8SMaor Gottlieb 	handler = create_flow_rule(dev, ft_prio,
1645038d2ef8SMaor Gottlieb 				   &leftovers_specs[LEFTOVERS_MC].flow_attr,
1646038d2ef8SMaor Gottlieb 				   dst);
1647038d2ef8SMaor Gottlieb 	if (!IS_ERR(handler) &&
1648038d2ef8SMaor Gottlieb 	    flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT) {
1649038d2ef8SMaor Gottlieb 		handler_ucast = create_flow_rule(dev, ft_prio,
1650038d2ef8SMaor Gottlieb 						 &leftovers_specs[LEFTOVERS_UC].flow_attr,
1651038d2ef8SMaor Gottlieb 						 dst);
1652038d2ef8SMaor Gottlieb 		if (IS_ERR(handler_ucast)) {
1653038d2ef8SMaor Gottlieb 			kfree(handler);
1654038d2ef8SMaor Gottlieb 			handler = handler_ucast;
1655038d2ef8SMaor Gottlieb 		} else {
1656038d2ef8SMaor Gottlieb 			list_add(&handler_ucast->list, &handler->list);
1657038d2ef8SMaor Gottlieb 		}
1658038d2ef8SMaor Gottlieb 	}
1659038d2ef8SMaor Gottlieb 
1660038d2ef8SMaor Gottlieb 	return handler;
1661038d2ef8SMaor Gottlieb }
1662038d2ef8SMaor Gottlieb 
1663038d2ef8SMaor Gottlieb static struct ib_flow *mlx5_ib_create_flow(struct ib_qp *qp,
1664038d2ef8SMaor Gottlieb 					   struct ib_flow_attr *flow_attr,
1665038d2ef8SMaor Gottlieb 					   int domain)
1666038d2ef8SMaor Gottlieb {
1667038d2ef8SMaor Gottlieb 	struct mlx5_ib_dev *dev = to_mdev(qp->device);
1668038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
1669038d2ef8SMaor Gottlieb 	struct mlx5_flow_destination *dst = NULL;
1670038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_prio *ft_prio;
1671038d2ef8SMaor Gottlieb 	int err;
1672038d2ef8SMaor Gottlieb 
1673038d2ef8SMaor Gottlieb 	if (flow_attr->priority > MLX5_IB_FLOW_LAST_PRIO)
1674038d2ef8SMaor Gottlieb 		return ERR_PTR(-ENOSPC);
1675038d2ef8SMaor Gottlieb 
1676038d2ef8SMaor Gottlieb 	if (domain != IB_FLOW_DOMAIN_USER ||
1677038d2ef8SMaor Gottlieb 	    flow_attr->port > MLX5_CAP_GEN(dev->mdev, num_ports) ||
167835d19011SMaor Gottlieb 	    (flow_attr->flags & ~IB_FLOW_ATTR_FLAGS_DONT_TRAP))
1679038d2ef8SMaor Gottlieb 		return ERR_PTR(-EINVAL);
1680038d2ef8SMaor Gottlieb 
1681038d2ef8SMaor Gottlieb 	dst = kzalloc(sizeof(*dst), GFP_KERNEL);
1682038d2ef8SMaor Gottlieb 	if (!dst)
1683038d2ef8SMaor Gottlieb 		return ERR_PTR(-ENOMEM);
1684038d2ef8SMaor Gottlieb 
1685038d2ef8SMaor Gottlieb 	mutex_lock(&dev->flow_db.lock);
1686038d2ef8SMaor Gottlieb 
1687038d2ef8SMaor Gottlieb 	ft_prio = get_flow_table(dev, flow_attr);
1688038d2ef8SMaor Gottlieb 	if (IS_ERR(ft_prio)) {
1689038d2ef8SMaor Gottlieb 		err = PTR_ERR(ft_prio);
1690038d2ef8SMaor Gottlieb 		goto unlock;
1691038d2ef8SMaor Gottlieb 	}
1692038d2ef8SMaor Gottlieb 
1693038d2ef8SMaor Gottlieb 	dst->type = MLX5_FLOW_DESTINATION_TYPE_TIR;
1694038d2ef8SMaor Gottlieb 	dst->tir_num = to_mqp(qp)->raw_packet_qp.rq.tirn;
1695038d2ef8SMaor Gottlieb 
1696038d2ef8SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
169735d19011SMaor Gottlieb 		if (flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP)  {
169835d19011SMaor Gottlieb 			handler = create_dont_trap_rule(dev, ft_prio,
169935d19011SMaor Gottlieb 							flow_attr, dst);
170035d19011SMaor Gottlieb 		} else {
1701038d2ef8SMaor Gottlieb 			handler = create_flow_rule(dev, ft_prio, flow_attr,
1702038d2ef8SMaor Gottlieb 						   dst);
170335d19011SMaor Gottlieb 		}
1704038d2ef8SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
1705038d2ef8SMaor Gottlieb 		   flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
1706038d2ef8SMaor Gottlieb 		handler = create_leftovers_rule(dev, ft_prio, flow_attr,
1707038d2ef8SMaor Gottlieb 						dst);
1708038d2ef8SMaor Gottlieb 	} else {
1709038d2ef8SMaor Gottlieb 		err = -EINVAL;
1710038d2ef8SMaor Gottlieb 		goto destroy_ft;
1711038d2ef8SMaor Gottlieb 	}
1712038d2ef8SMaor Gottlieb 
1713038d2ef8SMaor Gottlieb 	if (IS_ERR(handler)) {
1714038d2ef8SMaor Gottlieb 		err = PTR_ERR(handler);
1715038d2ef8SMaor Gottlieb 		handler = NULL;
1716038d2ef8SMaor Gottlieb 		goto destroy_ft;
1717038d2ef8SMaor Gottlieb 	}
1718038d2ef8SMaor Gottlieb 
1719038d2ef8SMaor Gottlieb 	ft_prio->refcount++;
1720038d2ef8SMaor Gottlieb 	mutex_unlock(&dev->flow_db.lock);
1721038d2ef8SMaor Gottlieb 	kfree(dst);
1722038d2ef8SMaor Gottlieb 
1723038d2ef8SMaor Gottlieb 	return &handler->ibflow;
1724038d2ef8SMaor Gottlieb 
1725038d2ef8SMaor Gottlieb destroy_ft:
1726038d2ef8SMaor Gottlieb 	put_flow_table(dev, ft_prio, false);
1727038d2ef8SMaor Gottlieb unlock:
1728038d2ef8SMaor Gottlieb 	mutex_unlock(&dev->flow_db.lock);
1729038d2ef8SMaor Gottlieb 	kfree(dst);
1730038d2ef8SMaor Gottlieb 	kfree(handler);
1731038d2ef8SMaor Gottlieb 	return ERR_PTR(err);
1732038d2ef8SMaor Gottlieb }
1733038d2ef8SMaor Gottlieb 
1734e126ba97SEli Cohen static int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
1735e126ba97SEli Cohen {
1736e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
1737e126ba97SEli Cohen 	int err;
1738e126ba97SEli Cohen 
17399603b61dSJack Morgenstein 	err = mlx5_core_attach_mcg(dev->mdev, gid, ibqp->qp_num);
1740e126ba97SEli Cohen 	if (err)
1741e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n",
1742e126ba97SEli Cohen 			     ibqp->qp_num, gid->raw);
1743e126ba97SEli Cohen 
1744e126ba97SEli Cohen 	return err;
1745e126ba97SEli Cohen }
1746e126ba97SEli Cohen 
1747e126ba97SEli Cohen static int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
1748e126ba97SEli Cohen {
1749e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
1750e126ba97SEli Cohen 	int err;
1751e126ba97SEli Cohen 
17529603b61dSJack Morgenstein 	err = mlx5_core_detach_mcg(dev->mdev, gid, ibqp->qp_num);
1753e126ba97SEli Cohen 	if (err)
1754e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n",
1755e126ba97SEli Cohen 			     ibqp->qp_num, gid->raw);
1756e126ba97SEli Cohen 
1757e126ba97SEli Cohen 	return err;
1758e126ba97SEli Cohen }
1759e126ba97SEli Cohen 
1760e126ba97SEli Cohen static int init_node_data(struct mlx5_ib_dev *dev)
1761e126ba97SEli Cohen {
17621b5daf11SMajd Dibbiny 	int err;
1763e126ba97SEli Cohen 
17641b5daf11SMajd Dibbiny 	err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc);
1765e126ba97SEli Cohen 	if (err)
1766e126ba97SEli Cohen 		return err;
17671b5daf11SMajd Dibbiny 
17681b5daf11SMajd Dibbiny 	dev->mdev->rev_id = dev->mdev->pdev->revision;
17691b5daf11SMajd Dibbiny 
17701b5daf11SMajd Dibbiny 	return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid);
1771e126ba97SEli Cohen }
1772e126ba97SEli Cohen 
1773e126ba97SEli Cohen static ssize_t show_fw_pages(struct device *device, struct device_attribute *attr,
1774e126ba97SEli Cohen 			     char *buf)
1775e126ba97SEli Cohen {
1776e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
1777e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
1778e126ba97SEli Cohen 
17799603b61dSJack Morgenstein 	return sprintf(buf, "%d\n", dev->mdev->priv.fw_pages);
1780e126ba97SEli Cohen }
1781e126ba97SEli Cohen 
1782e126ba97SEli Cohen static ssize_t show_reg_pages(struct device *device,
1783e126ba97SEli Cohen 			      struct device_attribute *attr, char *buf)
1784e126ba97SEli Cohen {
1785e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
1786e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
1787e126ba97SEli Cohen 
17886aec21f6SHaggai Eran 	return sprintf(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages));
1789e126ba97SEli Cohen }
1790e126ba97SEli Cohen 
1791e126ba97SEli Cohen static ssize_t show_hca(struct device *device, struct device_attribute *attr,
1792e126ba97SEli Cohen 			char *buf)
1793e126ba97SEli Cohen {
1794e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
1795e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
17969603b61dSJack Morgenstein 	return sprintf(buf, "MT%d\n", dev->mdev->pdev->device);
1797e126ba97SEli Cohen }
1798e126ba97SEli Cohen 
1799e126ba97SEli Cohen static ssize_t show_fw_ver(struct device *device, struct device_attribute *attr,
1800e126ba97SEli Cohen 			   char *buf)
1801e126ba97SEli Cohen {
1802e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
1803e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
18049603b61dSJack Morgenstein 	return sprintf(buf, "%d.%d.%d\n", fw_rev_maj(dev->mdev),
18059603b61dSJack Morgenstein 		       fw_rev_min(dev->mdev), fw_rev_sub(dev->mdev));
1806e126ba97SEli Cohen }
1807e126ba97SEli Cohen 
1808e126ba97SEli Cohen static ssize_t show_rev(struct device *device, struct device_attribute *attr,
1809e126ba97SEli Cohen 			char *buf)
1810e126ba97SEli Cohen {
1811e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
1812e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
18139603b61dSJack Morgenstein 	return sprintf(buf, "%x\n", dev->mdev->rev_id);
1814e126ba97SEli Cohen }
1815e126ba97SEli Cohen 
1816e126ba97SEli Cohen static ssize_t show_board(struct device *device, struct device_attribute *attr,
1817e126ba97SEli Cohen 			  char *buf)
1818e126ba97SEli Cohen {
1819e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
1820e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
1821e126ba97SEli Cohen 	return sprintf(buf, "%.*s\n", MLX5_BOARD_ID_LEN,
18229603b61dSJack Morgenstein 		       dev->mdev->board_id);
1823e126ba97SEli Cohen }
1824e126ba97SEli Cohen 
1825e126ba97SEli Cohen static DEVICE_ATTR(hw_rev,   S_IRUGO, show_rev,    NULL);
1826e126ba97SEli Cohen static DEVICE_ATTR(fw_ver,   S_IRUGO, show_fw_ver, NULL);
1827e126ba97SEli Cohen static DEVICE_ATTR(hca_type, S_IRUGO, show_hca,    NULL);
1828e126ba97SEli Cohen static DEVICE_ATTR(board_id, S_IRUGO, show_board,  NULL);
1829e126ba97SEli Cohen static DEVICE_ATTR(fw_pages, S_IRUGO, show_fw_pages, NULL);
1830e126ba97SEli Cohen static DEVICE_ATTR(reg_pages, S_IRUGO, show_reg_pages, NULL);
1831e126ba97SEli Cohen 
1832e126ba97SEli Cohen static struct device_attribute *mlx5_class_attributes[] = {
1833e126ba97SEli Cohen 	&dev_attr_hw_rev,
1834e126ba97SEli Cohen 	&dev_attr_fw_ver,
1835e126ba97SEli Cohen 	&dev_attr_hca_type,
1836e126ba97SEli Cohen 	&dev_attr_board_id,
1837e126ba97SEli Cohen 	&dev_attr_fw_pages,
1838e126ba97SEli Cohen 	&dev_attr_reg_pages,
1839e126ba97SEli Cohen };
1840e126ba97SEli Cohen 
18417722f47eSHaggai Eran static void pkey_change_handler(struct work_struct *work)
18427722f47eSHaggai Eran {
18437722f47eSHaggai Eran 	struct mlx5_ib_port_resources *ports =
18447722f47eSHaggai Eran 		container_of(work, struct mlx5_ib_port_resources,
18457722f47eSHaggai Eran 			     pkey_change_work);
18467722f47eSHaggai Eran 
18477722f47eSHaggai Eran 	mutex_lock(&ports->devr->mutex);
18487722f47eSHaggai Eran 	mlx5_ib_gsi_pkey_change(ports->gsi);
18497722f47eSHaggai Eran 	mutex_unlock(&ports->devr->mutex);
18507722f47eSHaggai Eran }
18517722f47eSHaggai Eran 
18529603b61dSJack Morgenstein static void mlx5_ib_event(struct mlx5_core_dev *dev, void *context,
18534d2f9bbbSJack Morgenstein 			  enum mlx5_dev_event event, unsigned long param)
1854e126ba97SEli Cohen {
18559603b61dSJack Morgenstein 	struct mlx5_ib_dev *ibdev = (struct mlx5_ib_dev *)context;
1856e126ba97SEli Cohen 	struct ib_event ibev;
18579603b61dSJack Morgenstein 
1858e126ba97SEli Cohen 	u8 port = 0;
1859e126ba97SEli Cohen 
1860e126ba97SEli Cohen 	switch (event) {
1861e126ba97SEli Cohen 	case MLX5_DEV_EVENT_SYS_ERROR:
1862e126ba97SEli Cohen 		ibdev->ib_active = false;
1863e126ba97SEli Cohen 		ibev.event = IB_EVENT_DEVICE_FATAL;
1864e126ba97SEli Cohen 		break;
1865e126ba97SEli Cohen 
1866e126ba97SEli Cohen 	case MLX5_DEV_EVENT_PORT_UP:
1867e126ba97SEli Cohen 		ibev.event = IB_EVENT_PORT_ACTIVE;
18684d2f9bbbSJack Morgenstein 		port = (u8)param;
1869e126ba97SEli Cohen 		break;
1870e126ba97SEli Cohen 
1871e126ba97SEli Cohen 	case MLX5_DEV_EVENT_PORT_DOWN:
1872*2788cf3bSNoa Osherovich 	case MLX5_DEV_EVENT_PORT_INITIALIZED:
1873e126ba97SEli Cohen 		ibev.event = IB_EVENT_PORT_ERR;
18744d2f9bbbSJack Morgenstein 		port = (u8)param;
1875e126ba97SEli Cohen 		break;
1876e126ba97SEli Cohen 
1877e126ba97SEli Cohen 	case MLX5_DEV_EVENT_LID_CHANGE:
1878e126ba97SEli Cohen 		ibev.event = IB_EVENT_LID_CHANGE;
18794d2f9bbbSJack Morgenstein 		port = (u8)param;
1880e126ba97SEli Cohen 		break;
1881e126ba97SEli Cohen 
1882e126ba97SEli Cohen 	case MLX5_DEV_EVENT_PKEY_CHANGE:
1883e126ba97SEli Cohen 		ibev.event = IB_EVENT_PKEY_CHANGE;
18844d2f9bbbSJack Morgenstein 		port = (u8)param;
18857722f47eSHaggai Eran 
18867722f47eSHaggai Eran 		schedule_work(&ibdev->devr.ports[port - 1].pkey_change_work);
1887e126ba97SEli Cohen 		break;
1888e126ba97SEli Cohen 
1889e126ba97SEli Cohen 	case MLX5_DEV_EVENT_GUID_CHANGE:
1890e126ba97SEli Cohen 		ibev.event = IB_EVENT_GID_CHANGE;
18914d2f9bbbSJack Morgenstein 		port = (u8)param;
1892e126ba97SEli Cohen 		break;
1893e126ba97SEli Cohen 
1894e126ba97SEli Cohen 	case MLX5_DEV_EVENT_CLIENT_REREG:
1895e126ba97SEli Cohen 		ibev.event = IB_EVENT_CLIENT_REREGISTER;
18964d2f9bbbSJack Morgenstein 		port = (u8)param;
1897e126ba97SEli Cohen 		break;
1898e126ba97SEli Cohen 	}
1899e126ba97SEli Cohen 
1900e126ba97SEli Cohen 	ibev.device	      = &ibdev->ib_dev;
1901e126ba97SEli Cohen 	ibev.element.port_num = port;
1902e126ba97SEli Cohen 
1903a0c84c32SEli Cohen 	if (port < 1 || port > ibdev->num_ports) {
1904a0c84c32SEli Cohen 		mlx5_ib_warn(ibdev, "warning: event on port %d\n", port);
1905a0c84c32SEli Cohen 		return;
1906a0c84c32SEli Cohen 	}
1907a0c84c32SEli Cohen 
1908e126ba97SEli Cohen 	if (ibdev->ib_active)
1909e126ba97SEli Cohen 		ib_dispatch_event(&ibev);
1910e126ba97SEli Cohen }
1911e126ba97SEli Cohen 
1912e126ba97SEli Cohen static void get_ext_port_caps(struct mlx5_ib_dev *dev)
1913e126ba97SEli Cohen {
1914e126ba97SEli Cohen 	int port;
1915e126ba97SEli Cohen 
1916938fe83cSSaeed Mahameed 	for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++)
1917e126ba97SEli Cohen 		mlx5_query_ext_port_caps(dev, port);
1918e126ba97SEli Cohen }
1919e126ba97SEli Cohen 
1920e126ba97SEli Cohen static int get_port_caps(struct mlx5_ib_dev *dev)
1921e126ba97SEli Cohen {
1922e126ba97SEli Cohen 	struct ib_device_attr *dprops = NULL;
1923e126ba97SEli Cohen 	struct ib_port_attr *pprops = NULL;
1924f614fc15SDan Carpenter 	int err = -ENOMEM;
1925e126ba97SEli Cohen 	int port;
19262528e33eSMatan Barak 	struct ib_udata uhw = {.inlen = 0, .outlen = 0};
1927e126ba97SEli Cohen 
1928e126ba97SEli Cohen 	pprops = kmalloc(sizeof(*pprops), GFP_KERNEL);
1929e126ba97SEli Cohen 	if (!pprops)
1930e126ba97SEli Cohen 		goto out;
1931e126ba97SEli Cohen 
1932e126ba97SEli Cohen 	dprops = kmalloc(sizeof(*dprops), GFP_KERNEL);
1933e126ba97SEli Cohen 	if (!dprops)
1934e126ba97SEli Cohen 		goto out;
1935e126ba97SEli Cohen 
19362528e33eSMatan Barak 	err = mlx5_ib_query_device(&dev->ib_dev, dprops, &uhw);
1937e126ba97SEli Cohen 	if (err) {
1938e126ba97SEli Cohen 		mlx5_ib_warn(dev, "query_device failed %d\n", err);
1939e126ba97SEli Cohen 		goto out;
1940e126ba97SEli Cohen 	}
1941e126ba97SEli Cohen 
1942938fe83cSSaeed Mahameed 	for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++) {
1943e126ba97SEli Cohen 		err = mlx5_ib_query_port(&dev->ib_dev, port, pprops);
1944e126ba97SEli Cohen 		if (err) {
1945938fe83cSSaeed Mahameed 			mlx5_ib_warn(dev, "query_port %d failed %d\n",
1946938fe83cSSaeed Mahameed 				     port, err);
1947e126ba97SEli Cohen 			break;
1948e126ba97SEli Cohen 		}
1949938fe83cSSaeed Mahameed 		dev->mdev->port_caps[port - 1].pkey_table_len =
1950938fe83cSSaeed Mahameed 						dprops->max_pkeys;
1951938fe83cSSaeed Mahameed 		dev->mdev->port_caps[port - 1].gid_table_len =
1952938fe83cSSaeed Mahameed 						pprops->gid_tbl_len;
1953e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "pkey_table_len %d, gid_table_len %d\n",
1954e126ba97SEli Cohen 			    dprops->max_pkeys, pprops->gid_tbl_len);
1955e126ba97SEli Cohen 	}
1956e126ba97SEli Cohen 
1957e126ba97SEli Cohen out:
1958e126ba97SEli Cohen 	kfree(pprops);
1959e126ba97SEli Cohen 	kfree(dprops);
1960e126ba97SEli Cohen 
1961e126ba97SEli Cohen 	return err;
1962e126ba97SEli Cohen }
1963e126ba97SEli Cohen 
1964e126ba97SEli Cohen static void destroy_umrc_res(struct mlx5_ib_dev *dev)
1965e126ba97SEli Cohen {
1966e126ba97SEli Cohen 	int err;
1967e126ba97SEli Cohen 
1968e126ba97SEli Cohen 	err = mlx5_mr_cache_cleanup(dev);
1969e126ba97SEli Cohen 	if (err)
1970e126ba97SEli Cohen 		mlx5_ib_warn(dev, "mr cache cleanup failed\n");
1971e126ba97SEli Cohen 
1972e126ba97SEli Cohen 	mlx5_ib_destroy_qp(dev->umrc.qp);
1973add08d76SChristoph Hellwig 	ib_free_cq(dev->umrc.cq);
1974e126ba97SEli Cohen 	ib_dealloc_pd(dev->umrc.pd);
1975e126ba97SEli Cohen }
1976e126ba97SEli Cohen 
1977e126ba97SEli Cohen enum {
1978e126ba97SEli Cohen 	MAX_UMR_WR = 128,
1979e126ba97SEli Cohen };
1980e126ba97SEli Cohen 
1981e126ba97SEli Cohen static int create_umr_res(struct mlx5_ib_dev *dev)
1982e126ba97SEli Cohen {
1983e126ba97SEli Cohen 	struct ib_qp_init_attr *init_attr = NULL;
1984e126ba97SEli Cohen 	struct ib_qp_attr *attr = NULL;
1985e126ba97SEli Cohen 	struct ib_pd *pd;
1986e126ba97SEli Cohen 	struct ib_cq *cq;
1987e126ba97SEli Cohen 	struct ib_qp *qp;
1988e126ba97SEli Cohen 	int ret;
1989e126ba97SEli Cohen 
1990e126ba97SEli Cohen 	attr = kzalloc(sizeof(*attr), GFP_KERNEL);
1991e126ba97SEli Cohen 	init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL);
1992e126ba97SEli Cohen 	if (!attr || !init_attr) {
1993e126ba97SEli Cohen 		ret = -ENOMEM;
1994e126ba97SEli Cohen 		goto error_0;
1995e126ba97SEli Cohen 	}
1996e126ba97SEli Cohen 
1997e126ba97SEli Cohen 	pd = ib_alloc_pd(&dev->ib_dev);
1998e126ba97SEli Cohen 	if (IS_ERR(pd)) {
1999e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create PD for sync UMR QP\n");
2000e126ba97SEli Cohen 		ret = PTR_ERR(pd);
2001e126ba97SEli Cohen 		goto error_0;
2002e126ba97SEli Cohen 	}
2003e126ba97SEli Cohen 
2004add08d76SChristoph Hellwig 	cq = ib_alloc_cq(&dev->ib_dev, NULL, 128, 0, IB_POLL_SOFTIRQ);
2005e126ba97SEli Cohen 	if (IS_ERR(cq)) {
2006e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create CQ for sync UMR QP\n");
2007e126ba97SEli Cohen 		ret = PTR_ERR(cq);
2008e126ba97SEli Cohen 		goto error_2;
2009e126ba97SEli Cohen 	}
2010e126ba97SEli Cohen 
2011e126ba97SEli Cohen 	init_attr->send_cq = cq;
2012e126ba97SEli Cohen 	init_attr->recv_cq = cq;
2013e126ba97SEli Cohen 	init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
2014e126ba97SEli Cohen 	init_attr->cap.max_send_wr = MAX_UMR_WR;
2015e126ba97SEli Cohen 	init_attr->cap.max_send_sge = 1;
2016e126ba97SEli Cohen 	init_attr->qp_type = MLX5_IB_QPT_REG_UMR;
2017e126ba97SEli Cohen 	init_attr->port_num = 1;
2018e126ba97SEli Cohen 	qp = mlx5_ib_create_qp(pd, init_attr, NULL);
2019e126ba97SEli Cohen 	if (IS_ERR(qp)) {
2020e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create sync UMR QP\n");
2021e126ba97SEli Cohen 		ret = PTR_ERR(qp);
2022e126ba97SEli Cohen 		goto error_3;
2023e126ba97SEli Cohen 	}
2024e126ba97SEli Cohen 	qp->device     = &dev->ib_dev;
2025e126ba97SEli Cohen 	qp->real_qp    = qp;
2026e126ba97SEli Cohen 	qp->uobject    = NULL;
2027e126ba97SEli Cohen 	qp->qp_type    = MLX5_IB_QPT_REG_UMR;
2028e126ba97SEli Cohen 
2029e126ba97SEli Cohen 	attr->qp_state = IB_QPS_INIT;
2030e126ba97SEli Cohen 	attr->port_num = 1;
2031e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_PKEY_INDEX |
2032e126ba97SEli Cohen 				IB_QP_PORT, NULL);
2033e126ba97SEli Cohen 	if (ret) {
2034e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify UMR QP\n");
2035e126ba97SEli Cohen 		goto error_4;
2036e126ba97SEli Cohen 	}
2037e126ba97SEli Cohen 
2038e126ba97SEli Cohen 	memset(attr, 0, sizeof(*attr));
2039e126ba97SEli Cohen 	attr->qp_state = IB_QPS_RTR;
2040e126ba97SEli Cohen 	attr->path_mtu = IB_MTU_256;
2041e126ba97SEli Cohen 
2042e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
2043e126ba97SEli Cohen 	if (ret) {
2044e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify umr QP to rtr\n");
2045e126ba97SEli Cohen 		goto error_4;
2046e126ba97SEli Cohen 	}
2047e126ba97SEli Cohen 
2048e126ba97SEli Cohen 	memset(attr, 0, sizeof(*attr));
2049e126ba97SEli Cohen 	attr->qp_state = IB_QPS_RTS;
2050e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
2051e126ba97SEli Cohen 	if (ret) {
2052e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify umr QP to rts\n");
2053e126ba97SEli Cohen 		goto error_4;
2054e126ba97SEli Cohen 	}
2055e126ba97SEli Cohen 
2056e126ba97SEli Cohen 	dev->umrc.qp = qp;
2057e126ba97SEli Cohen 	dev->umrc.cq = cq;
2058e126ba97SEli Cohen 	dev->umrc.pd = pd;
2059e126ba97SEli Cohen 
2060e126ba97SEli Cohen 	sema_init(&dev->umrc.sem, MAX_UMR_WR);
2061e126ba97SEli Cohen 	ret = mlx5_mr_cache_init(dev);
2062e126ba97SEli Cohen 	if (ret) {
2063e126ba97SEli Cohen 		mlx5_ib_warn(dev, "mr cache init failed %d\n", ret);
2064e126ba97SEli Cohen 		goto error_4;
2065e126ba97SEli Cohen 	}
2066e126ba97SEli Cohen 
2067e126ba97SEli Cohen 	kfree(attr);
2068e126ba97SEli Cohen 	kfree(init_attr);
2069e126ba97SEli Cohen 
2070e126ba97SEli Cohen 	return 0;
2071e126ba97SEli Cohen 
2072e126ba97SEli Cohen error_4:
2073e126ba97SEli Cohen 	mlx5_ib_destroy_qp(qp);
2074e126ba97SEli Cohen 
2075e126ba97SEli Cohen error_3:
2076add08d76SChristoph Hellwig 	ib_free_cq(cq);
2077e126ba97SEli Cohen 
2078e126ba97SEli Cohen error_2:
2079e126ba97SEli Cohen 	ib_dealloc_pd(pd);
2080e126ba97SEli Cohen 
2081e126ba97SEli Cohen error_0:
2082e126ba97SEli Cohen 	kfree(attr);
2083e126ba97SEli Cohen 	kfree(init_attr);
2084e126ba97SEli Cohen 	return ret;
2085e126ba97SEli Cohen }
2086e126ba97SEli Cohen 
2087e126ba97SEli Cohen static int create_dev_resources(struct mlx5_ib_resources *devr)
2088e126ba97SEli Cohen {
2089e126ba97SEli Cohen 	struct ib_srq_init_attr attr;
2090e126ba97SEli Cohen 	struct mlx5_ib_dev *dev;
2091bcf4c1eaSMatan Barak 	struct ib_cq_init_attr cq_attr = {.cqe = 1};
20927722f47eSHaggai Eran 	int port;
2093e126ba97SEli Cohen 	int ret = 0;
2094e126ba97SEli Cohen 
2095e126ba97SEli Cohen 	dev = container_of(devr, struct mlx5_ib_dev, devr);
2096e126ba97SEli Cohen 
2097d16e91daSHaggai Eran 	mutex_init(&devr->mutex);
2098d16e91daSHaggai Eran 
2099e126ba97SEli Cohen 	devr->p0 = mlx5_ib_alloc_pd(&dev->ib_dev, NULL, NULL);
2100e126ba97SEli Cohen 	if (IS_ERR(devr->p0)) {
2101e126ba97SEli Cohen 		ret = PTR_ERR(devr->p0);
2102e126ba97SEli Cohen 		goto error0;
2103e126ba97SEli Cohen 	}
2104e126ba97SEli Cohen 	devr->p0->device  = &dev->ib_dev;
2105e126ba97SEli Cohen 	devr->p0->uobject = NULL;
2106e126ba97SEli Cohen 	atomic_set(&devr->p0->usecnt, 0);
2107e126ba97SEli Cohen 
2108bcf4c1eaSMatan Barak 	devr->c0 = mlx5_ib_create_cq(&dev->ib_dev, &cq_attr, NULL, NULL);
2109e126ba97SEli Cohen 	if (IS_ERR(devr->c0)) {
2110e126ba97SEli Cohen 		ret = PTR_ERR(devr->c0);
2111e126ba97SEli Cohen 		goto error1;
2112e126ba97SEli Cohen 	}
2113e126ba97SEli Cohen 	devr->c0->device        = &dev->ib_dev;
2114e126ba97SEli Cohen 	devr->c0->uobject       = NULL;
2115e126ba97SEli Cohen 	devr->c0->comp_handler  = NULL;
2116e126ba97SEli Cohen 	devr->c0->event_handler = NULL;
2117e126ba97SEli Cohen 	devr->c0->cq_context    = NULL;
2118e126ba97SEli Cohen 	atomic_set(&devr->c0->usecnt, 0);
2119e126ba97SEli Cohen 
2120e126ba97SEli Cohen 	devr->x0 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
2121e126ba97SEli Cohen 	if (IS_ERR(devr->x0)) {
2122e126ba97SEli Cohen 		ret = PTR_ERR(devr->x0);
2123e126ba97SEli Cohen 		goto error2;
2124e126ba97SEli Cohen 	}
2125e126ba97SEli Cohen 	devr->x0->device = &dev->ib_dev;
2126e126ba97SEli Cohen 	devr->x0->inode = NULL;
2127e126ba97SEli Cohen 	atomic_set(&devr->x0->usecnt, 0);
2128e126ba97SEli Cohen 	mutex_init(&devr->x0->tgt_qp_mutex);
2129e126ba97SEli Cohen 	INIT_LIST_HEAD(&devr->x0->tgt_qp_list);
2130e126ba97SEli Cohen 
2131e126ba97SEli Cohen 	devr->x1 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
2132e126ba97SEli Cohen 	if (IS_ERR(devr->x1)) {
2133e126ba97SEli Cohen 		ret = PTR_ERR(devr->x1);
2134e126ba97SEli Cohen 		goto error3;
2135e126ba97SEli Cohen 	}
2136e126ba97SEli Cohen 	devr->x1->device = &dev->ib_dev;
2137e126ba97SEli Cohen 	devr->x1->inode = NULL;
2138e126ba97SEli Cohen 	atomic_set(&devr->x1->usecnt, 0);
2139e126ba97SEli Cohen 	mutex_init(&devr->x1->tgt_qp_mutex);
2140e126ba97SEli Cohen 	INIT_LIST_HEAD(&devr->x1->tgt_qp_list);
2141e126ba97SEli Cohen 
2142e126ba97SEli Cohen 	memset(&attr, 0, sizeof(attr));
2143e126ba97SEli Cohen 	attr.attr.max_sge = 1;
2144e126ba97SEli Cohen 	attr.attr.max_wr = 1;
2145e126ba97SEli Cohen 	attr.srq_type = IB_SRQT_XRC;
2146e126ba97SEli Cohen 	attr.ext.xrc.cq = devr->c0;
2147e126ba97SEli Cohen 	attr.ext.xrc.xrcd = devr->x0;
2148e126ba97SEli Cohen 
2149e126ba97SEli Cohen 	devr->s0 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
2150e126ba97SEli Cohen 	if (IS_ERR(devr->s0)) {
2151e126ba97SEli Cohen 		ret = PTR_ERR(devr->s0);
2152e126ba97SEli Cohen 		goto error4;
2153e126ba97SEli Cohen 	}
2154e126ba97SEli Cohen 	devr->s0->device	= &dev->ib_dev;
2155e126ba97SEli Cohen 	devr->s0->pd		= devr->p0;
2156e126ba97SEli Cohen 	devr->s0->uobject       = NULL;
2157e126ba97SEli Cohen 	devr->s0->event_handler = NULL;
2158e126ba97SEli Cohen 	devr->s0->srq_context   = NULL;
2159e126ba97SEli Cohen 	devr->s0->srq_type      = IB_SRQT_XRC;
2160e126ba97SEli Cohen 	devr->s0->ext.xrc.xrcd	= devr->x0;
2161e126ba97SEli Cohen 	devr->s0->ext.xrc.cq	= devr->c0;
2162e126ba97SEli Cohen 	atomic_inc(&devr->s0->ext.xrc.xrcd->usecnt);
2163e126ba97SEli Cohen 	atomic_inc(&devr->s0->ext.xrc.cq->usecnt);
2164e126ba97SEli Cohen 	atomic_inc(&devr->p0->usecnt);
2165e126ba97SEli Cohen 	atomic_set(&devr->s0->usecnt, 0);
2166e126ba97SEli Cohen 
21674aa17b28SHaggai Abramonvsky 	memset(&attr, 0, sizeof(attr));
21684aa17b28SHaggai Abramonvsky 	attr.attr.max_sge = 1;
21694aa17b28SHaggai Abramonvsky 	attr.attr.max_wr = 1;
21704aa17b28SHaggai Abramonvsky 	attr.srq_type = IB_SRQT_BASIC;
21714aa17b28SHaggai Abramonvsky 	devr->s1 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
21724aa17b28SHaggai Abramonvsky 	if (IS_ERR(devr->s1)) {
21734aa17b28SHaggai Abramonvsky 		ret = PTR_ERR(devr->s1);
21744aa17b28SHaggai Abramonvsky 		goto error5;
21754aa17b28SHaggai Abramonvsky 	}
21764aa17b28SHaggai Abramonvsky 	devr->s1->device	= &dev->ib_dev;
21774aa17b28SHaggai Abramonvsky 	devr->s1->pd		= devr->p0;
21784aa17b28SHaggai Abramonvsky 	devr->s1->uobject       = NULL;
21794aa17b28SHaggai Abramonvsky 	devr->s1->event_handler = NULL;
21804aa17b28SHaggai Abramonvsky 	devr->s1->srq_context   = NULL;
21814aa17b28SHaggai Abramonvsky 	devr->s1->srq_type      = IB_SRQT_BASIC;
21824aa17b28SHaggai Abramonvsky 	devr->s1->ext.xrc.cq	= devr->c0;
21834aa17b28SHaggai Abramonvsky 	atomic_inc(&devr->p0->usecnt);
21844aa17b28SHaggai Abramonvsky 	atomic_set(&devr->s0->usecnt, 0);
21854aa17b28SHaggai Abramonvsky 
21867722f47eSHaggai Eran 	for (port = 0; port < ARRAY_SIZE(devr->ports); ++port) {
21877722f47eSHaggai Eran 		INIT_WORK(&devr->ports[port].pkey_change_work,
21887722f47eSHaggai Eran 			  pkey_change_handler);
21897722f47eSHaggai Eran 		devr->ports[port].devr = devr;
21907722f47eSHaggai Eran 	}
21917722f47eSHaggai Eran 
2192e126ba97SEli Cohen 	return 0;
2193e126ba97SEli Cohen 
21944aa17b28SHaggai Abramonvsky error5:
21954aa17b28SHaggai Abramonvsky 	mlx5_ib_destroy_srq(devr->s0);
2196e126ba97SEli Cohen error4:
2197e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x1);
2198e126ba97SEli Cohen error3:
2199e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x0);
2200e126ba97SEli Cohen error2:
2201e126ba97SEli Cohen 	mlx5_ib_destroy_cq(devr->c0);
2202e126ba97SEli Cohen error1:
2203e126ba97SEli Cohen 	mlx5_ib_dealloc_pd(devr->p0);
2204e126ba97SEli Cohen error0:
2205e126ba97SEli Cohen 	return ret;
2206e126ba97SEli Cohen }
2207e126ba97SEli Cohen 
2208e126ba97SEli Cohen static void destroy_dev_resources(struct mlx5_ib_resources *devr)
2209e126ba97SEli Cohen {
22107722f47eSHaggai Eran 	struct mlx5_ib_dev *dev =
22117722f47eSHaggai Eran 		container_of(devr, struct mlx5_ib_dev, devr);
22127722f47eSHaggai Eran 	int port;
22137722f47eSHaggai Eran 
22144aa17b28SHaggai Abramonvsky 	mlx5_ib_destroy_srq(devr->s1);
2215e126ba97SEli Cohen 	mlx5_ib_destroy_srq(devr->s0);
2216e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x0);
2217e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x1);
2218e126ba97SEli Cohen 	mlx5_ib_destroy_cq(devr->c0);
2219e126ba97SEli Cohen 	mlx5_ib_dealloc_pd(devr->p0);
22207722f47eSHaggai Eran 
22217722f47eSHaggai Eran 	/* Make sure no change P_Key work items are still executing */
22227722f47eSHaggai Eran 	for (port = 0; port < dev->num_ports; ++port)
22237722f47eSHaggai Eran 		cancel_work_sync(&devr->ports[port].pkey_change_work);
2224e126ba97SEli Cohen }
2225e126ba97SEli Cohen 
2226e53505a8SAchiad Shochat static u32 get_core_cap_flags(struct ib_device *ibdev)
2227e53505a8SAchiad Shochat {
2228e53505a8SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
2229e53505a8SAchiad Shochat 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, 1);
2230e53505a8SAchiad Shochat 	u8 l3_type_cap = MLX5_CAP_ROCE(dev->mdev, l3_type);
2231e53505a8SAchiad Shochat 	u8 roce_version_cap = MLX5_CAP_ROCE(dev->mdev, roce_version);
2232e53505a8SAchiad Shochat 	u32 ret = 0;
2233e53505a8SAchiad Shochat 
2234e53505a8SAchiad Shochat 	if (ll == IB_LINK_LAYER_INFINIBAND)
2235e53505a8SAchiad Shochat 		return RDMA_CORE_PORT_IBA_IB;
2236e53505a8SAchiad Shochat 
2237e53505a8SAchiad Shochat 	if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV4_CAP))
2238e53505a8SAchiad Shochat 		return 0;
2239e53505a8SAchiad Shochat 
2240e53505a8SAchiad Shochat 	if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV6_CAP))
2241e53505a8SAchiad Shochat 		return 0;
2242e53505a8SAchiad Shochat 
2243e53505a8SAchiad Shochat 	if (roce_version_cap & MLX5_ROCE_VERSION_1_CAP)
2244e53505a8SAchiad Shochat 		ret |= RDMA_CORE_PORT_IBA_ROCE;
2245e53505a8SAchiad Shochat 
2246e53505a8SAchiad Shochat 	if (roce_version_cap & MLX5_ROCE_VERSION_2_CAP)
2247e53505a8SAchiad Shochat 		ret |= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
2248e53505a8SAchiad Shochat 
2249e53505a8SAchiad Shochat 	return ret;
2250e53505a8SAchiad Shochat }
2251e53505a8SAchiad Shochat 
22527738613eSIra Weiny static int mlx5_port_immutable(struct ib_device *ibdev, u8 port_num,
22537738613eSIra Weiny 			       struct ib_port_immutable *immutable)
22547738613eSIra Weiny {
22557738613eSIra Weiny 	struct ib_port_attr attr;
22567738613eSIra Weiny 	int err;
22577738613eSIra Weiny 
22587738613eSIra Weiny 	err = mlx5_ib_query_port(ibdev, port_num, &attr);
22597738613eSIra Weiny 	if (err)
22607738613eSIra Weiny 		return err;
22617738613eSIra Weiny 
22627738613eSIra Weiny 	immutable->pkey_tbl_len = attr.pkey_tbl_len;
22637738613eSIra Weiny 	immutable->gid_tbl_len = attr.gid_tbl_len;
2264e53505a8SAchiad Shochat 	immutable->core_cap_flags = get_core_cap_flags(ibdev);
2265337877a4SIra Weiny 	immutable->max_mad_size = IB_MGMT_MAD_SIZE;
22667738613eSIra Weiny 
22677738613eSIra Weiny 	return 0;
22687738613eSIra Weiny }
22697738613eSIra Weiny 
2270fc24fc5eSAchiad Shochat static int mlx5_enable_roce(struct mlx5_ib_dev *dev)
2271fc24fc5eSAchiad Shochat {
2272e53505a8SAchiad Shochat 	int err;
2273e53505a8SAchiad Shochat 
2274fc24fc5eSAchiad Shochat 	dev->roce.nb.notifier_call = mlx5_netdev_event;
2275e53505a8SAchiad Shochat 	err = register_netdevice_notifier(&dev->roce.nb);
2276e53505a8SAchiad Shochat 	if (err)
2277e53505a8SAchiad Shochat 		return err;
2278e53505a8SAchiad Shochat 
2279e53505a8SAchiad Shochat 	err = mlx5_nic_vport_enable_roce(dev->mdev);
2280e53505a8SAchiad Shochat 	if (err)
2281e53505a8SAchiad Shochat 		goto err_unregister_netdevice_notifier;
2282e53505a8SAchiad Shochat 
2283e53505a8SAchiad Shochat 	return 0;
2284e53505a8SAchiad Shochat 
2285e53505a8SAchiad Shochat err_unregister_netdevice_notifier:
2286e53505a8SAchiad Shochat 	unregister_netdevice_notifier(&dev->roce.nb);
2287e53505a8SAchiad Shochat 	return err;
2288fc24fc5eSAchiad Shochat }
2289fc24fc5eSAchiad Shochat 
2290fc24fc5eSAchiad Shochat static void mlx5_disable_roce(struct mlx5_ib_dev *dev)
2291fc24fc5eSAchiad Shochat {
2292e53505a8SAchiad Shochat 	mlx5_nic_vport_disable_roce(dev->mdev);
2293fc24fc5eSAchiad Shochat 	unregister_netdevice_notifier(&dev->roce.nb);
2294fc24fc5eSAchiad Shochat }
2295fc24fc5eSAchiad Shochat 
22969603b61dSJack Morgenstein static void *mlx5_ib_add(struct mlx5_core_dev *mdev)
2297e126ba97SEli Cohen {
2298e126ba97SEli Cohen 	struct mlx5_ib_dev *dev;
2299ebd61f68SAchiad Shochat 	enum rdma_link_layer ll;
2300ebd61f68SAchiad Shochat 	int port_type_cap;
2301e126ba97SEli Cohen 	int err;
2302e126ba97SEli Cohen 	int i;
2303e126ba97SEli Cohen 
2304ebd61f68SAchiad Shochat 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
2305ebd61f68SAchiad Shochat 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
2306ebd61f68SAchiad Shochat 
2307e53505a8SAchiad Shochat 	if ((ll == IB_LINK_LAYER_ETHERNET) && !MLX5_CAP_GEN(mdev, roce))
2308647241eaSMajd Dibbiny 		return NULL;
2309647241eaSMajd Dibbiny 
2310e126ba97SEli Cohen 	printk_once(KERN_INFO "%s", mlx5_version);
2311e126ba97SEli Cohen 
2312e126ba97SEli Cohen 	dev = (struct mlx5_ib_dev *)ib_alloc_device(sizeof(*dev));
2313e126ba97SEli Cohen 	if (!dev)
23149603b61dSJack Morgenstein 		return NULL;
2315e126ba97SEli Cohen 
23169603b61dSJack Morgenstein 	dev->mdev = mdev;
2317e126ba97SEli Cohen 
2318fc24fc5eSAchiad Shochat 	rwlock_init(&dev->roce.netdev_lock);
2319e126ba97SEli Cohen 	err = get_port_caps(dev);
2320e126ba97SEli Cohen 	if (err)
23219603b61dSJack Morgenstein 		goto err_dealloc;
2322e126ba97SEli Cohen 
23231b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(dev))
2324e126ba97SEli Cohen 		get_ext_port_caps(dev);
2325e126ba97SEli Cohen 
2326e126ba97SEli Cohen 	MLX5_INIT_DOORBELL_LOCK(&dev->uar_lock);
2327e126ba97SEli Cohen 
2328e126ba97SEli Cohen 	strlcpy(dev->ib_dev.name, "mlx5_%d", IB_DEVICE_NAME_MAX);
2329e126ba97SEli Cohen 	dev->ib_dev.owner		= THIS_MODULE;
2330e126ba97SEli Cohen 	dev->ib_dev.node_type		= RDMA_NODE_IB_CA;
2331c6790aa9SSagi Grimberg 	dev->ib_dev.local_dma_lkey	= 0 /* not supported for now */;
2332938fe83cSSaeed Mahameed 	dev->num_ports		= MLX5_CAP_GEN(mdev, num_ports);
2333e126ba97SEli Cohen 	dev->ib_dev.phys_port_cnt     = dev->num_ports;
2334233d05d2SSaeed Mahameed 	dev->ib_dev.num_comp_vectors    =
2335233d05d2SSaeed Mahameed 		dev->mdev->priv.eq_table.num_comp_vectors;
2336e126ba97SEli Cohen 	dev->ib_dev.dma_device	= &mdev->pdev->dev;
2337e126ba97SEli Cohen 
2338e126ba97SEli Cohen 	dev->ib_dev.uverbs_abi_ver	= MLX5_IB_UVERBS_ABI_VERSION;
2339e126ba97SEli Cohen 	dev->ib_dev.uverbs_cmd_mask	=
2340e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_GET_CONTEXT)		|
2341e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_DEVICE)	|
2342e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_PORT)		|
2343e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_ALLOC_PD)		|
2344e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DEALLOC_PD)		|
2345e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_REG_MR)		|
234656e11d62SNoa Osherovich 		(1ull << IB_USER_VERBS_CMD_REREG_MR)		|
2347e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DEREG_MR)		|
2348e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL)	|
2349e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_CQ)		|
2350e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_RESIZE_CQ)		|
2351e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_CQ)		|
2352e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_QP)		|
2353e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_MODIFY_QP)		|
2354e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_QP)		|
2355e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_QP)		|
2356e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_ATTACH_MCAST)	|
2357e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DETACH_MCAST)	|
2358e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_SRQ)		|
2359e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_MODIFY_SRQ)		|
2360e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_SRQ)		|
2361e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_SRQ)		|
2362e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_XSRQ)		|
2363e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_OPEN_QP);
23641707cb4aSHaggai Eran 	dev->ib_dev.uverbs_ex_cmd_mask =
2365d4584ddfSMatan Barak 		(1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE)	|
2366d4584ddfSMatan Barak 		(1ull << IB_USER_VERBS_EX_CMD_CREATE_CQ)	|
2367d4584ddfSMatan Barak 		(1ull << IB_USER_VERBS_EX_CMD_CREATE_QP);
2368e126ba97SEli Cohen 
2369e126ba97SEli Cohen 	dev->ib_dev.query_device	= mlx5_ib_query_device;
2370e126ba97SEli Cohen 	dev->ib_dev.query_port		= mlx5_ib_query_port;
2371ebd61f68SAchiad Shochat 	dev->ib_dev.get_link_layer	= mlx5_ib_port_link_layer;
2372fc24fc5eSAchiad Shochat 	if (ll == IB_LINK_LAYER_ETHERNET)
2373fc24fc5eSAchiad Shochat 		dev->ib_dev.get_netdev	= mlx5_ib_get_netdev;
2374e126ba97SEli Cohen 	dev->ib_dev.query_gid		= mlx5_ib_query_gid;
23753cca2606SAchiad Shochat 	dev->ib_dev.add_gid		= mlx5_ib_add_gid;
23763cca2606SAchiad Shochat 	dev->ib_dev.del_gid		= mlx5_ib_del_gid;
2377e126ba97SEli Cohen 	dev->ib_dev.query_pkey		= mlx5_ib_query_pkey;
2378e126ba97SEli Cohen 	dev->ib_dev.modify_device	= mlx5_ib_modify_device;
2379e126ba97SEli Cohen 	dev->ib_dev.modify_port		= mlx5_ib_modify_port;
2380e126ba97SEli Cohen 	dev->ib_dev.alloc_ucontext	= mlx5_ib_alloc_ucontext;
2381e126ba97SEli Cohen 	dev->ib_dev.dealloc_ucontext	= mlx5_ib_dealloc_ucontext;
2382e126ba97SEli Cohen 	dev->ib_dev.mmap		= mlx5_ib_mmap;
2383e126ba97SEli Cohen 	dev->ib_dev.alloc_pd		= mlx5_ib_alloc_pd;
2384e126ba97SEli Cohen 	dev->ib_dev.dealloc_pd		= mlx5_ib_dealloc_pd;
2385e126ba97SEli Cohen 	dev->ib_dev.create_ah		= mlx5_ib_create_ah;
2386e126ba97SEli Cohen 	dev->ib_dev.query_ah		= mlx5_ib_query_ah;
2387e126ba97SEli Cohen 	dev->ib_dev.destroy_ah		= mlx5_ib_destroy_ah;
2388e126ba97SEli Cohen 	dev->ib_dev.create_srq		= mlx5_ib_create_srq;
2389e126ba97SEli Cohen 	dev->ib_dev.modify_srq		= mlx5_ib_modify_srq;
2390e126ba97SEli Cohen 	dev->ib_dev.query_srq		= mlx5_ib_query_srq;
2391e126ba97SEli Cohen 	dev->ib_dev.destroy_srq		= mlx5_ib_destroy_srq;
2392e126ba97SEli Cohen 	dev->ib_dev.post_srq_recv	= mlx5_ib_post_srq_recv;
2393e126ba97SEli Cohen 	dev->ib_dev.create_qp		= mlx5_ib_create_qp;
2394e126ba97SEli Cohen 	dev->ib_dev.modify_qp		= mlx5_ib_modify_qp;
2395e126ba97SEli Cohen 	dev->ib_dev.query_qp		= mlx5_ib_query_qp;
2396e126ba97SEli Cohen 	dev->ib_dev.destroy_qp		= mlx5_ib_destroy_qp;
2397e126ba97SEli Cohen 	dev->ib_dev.post_send		= mlx5_ib_post_send;
2398e126ba97SEli Cohen 	dev->ib_dev.post_recv		= mlx5_ib_post_recv;
2399e126ba97SEli Cohen 	dev->ib_dev.create_cq		= mlx5_ib_create_cq;
2400e126ba97SEli Cohen 	dev->ib_dev.modify_cq		= mlx5_ib_modify_cq;
2401e126ba97SEli Cohen 	dev->ib_dev.resize_cq		= mlx5_ib_resize_cq;
2402e126ba97SEli Cohen 	dev->ib_dev.destroy_cq		= mlx5_ib_destroy_cq;
2403e126ba97SEli Cohen 	dev->ib_dev.poll_cq		= mlx5_ib_poll_cq;
2404e126ba97SEli Cohen 	dev->ib_dev.req_notify_cq	= mlx5_ib_arm_cq;
2405e126ba97SEli Cohen 	dev->ib_dev.get_dma_mr		= mlx5_ib_get_dma_mr;
2406e126ba97SEli Cohen 	dev->ib_dev.reg_user_mr		= mlx5_ib_reg_user_mr;
240756e11d62SNoa Osherovich 	dev->ib_dev.rereg_user_mr	= mlx5_ib_rereg_user_mr;
2408e126ba97SEli Cohen 	dev->ib_dev.dereg_mr		= mlx5_ib_dereg_mr;
2409e126ba97SEli Cohen 	dev->ib_dev.attach_mcast	= mlx5_ib_mcg_attach;
2410e126ba97SEli Cohen 	dev->ib_dev.detach_mcast	= mlx5_ib_mcg_detach;
2411e126ba97SEli Cohen 	dev->ib_dev.process_mad		= mlx5_ib_process_mad;
24129bee178bSSagi Grimberg 	dev->ib_dev.alloc_mr		= mlx5_ib_alloc_mr;
24138a187ee5SSagi Grimberg 	dev->ib_dev.map_mr_sg		= mlx5_ib_map_mr_sg;
2414d5436ba0SSagi Grimberg 	dev->ib_dev.check_mr_status	= mlx5_ib_check_mr_status;
24157738613eSIra Weiny 	dev->ib_dev.get_port_immutable  = mlx5_port_immutable;
2416eff901d3SEli Cohen 	if (mlx5_core_is_pf(mdev)) {
2417eff901d3SEli Cohen 		dev->ib_dev.get_vf_config	= mlx5_ib_get_vf_config;
2418eff901d3SEli Cohen 		dev->ib_dev.set_vf_link_state	= mlx5_ib_set_vf_link_state;
2419eff901d3SEli Cohen 		dev->ib_dev.get_vf_stats	= mlx5_ib_get_vf_stats;
2420eff901d3SEli Cohen 		dev->ib_dev.set_vf_guid		= mlx5_ib_set_vf_guid;
2421eff901d3SEli Cohen 	}
2422e126ba97SEli Cohen 
2423938fe83cSSaeed Mahameed 	mlx5_ib_internal_fill_odp_caps(dev);
24248cdd312cSHaggai Eran 
2425d2370e0aSMatan Barak 	if (MLX5_CAP_GEN(mdev, imaicl)) {
2426d2370e0aSMatan Barak 		dev->ib_dev.alloc_mw		= mlx5_ib_alloc_mw;
2427d2370e0aSMatan Barak 		dev->ib_dev.dealloc_mw		= mlx5_ib_dealloc_mw;
2428d2370e0aSMatan Barak 		dev->ib_dev.uverbs_cmd_mask |=
2429d2370e0aSMatan Barak 			(1ull << IB_USER_VERBS_CMD_ALLOC_MW)	|
2430d2370e0aSMatan Barak 			(1ull << IB_USER_VERBS_CMD_DEALLOC_MW);
2431d2370e0aSMatan Barak 	}
2432d2370e0aSMatan Barak 
2433938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, xrc)) {
2434e126ba97SEli Cohen 		dev->ib_dev.alloc_xrcd = mlx5_ib_alloc_xrcd;
2435e126ba97SEli Cohen 		dev->ib_dev.dealloc_xrcd = mlx5_ib_dealloc_xrcd;
2436e126ba97SEli Cohen 		dev->ib_dev.uverbs_cmd_mask |=
2437e126ba97SEli Cohen 			(1ull << IB_USER_VERBS_CMD_OPEN_XRCD) |
2438e126ba97SEli Cohen 			(1ull << IB_USER_VERBS_CMD_CLOSE_XRCD);
2439e126ba97SEli Cohen 	}
2440e126ba97SEli Cohen 
2441048ccca8SLinus Torvalds 	if (mlx5_ib_port_link_layer(&dev->ib_dev, 1) ==
2442038d2ef8SMaor Gottlieb 	    IB_LINK_LAYER_ETHERNET) {
2443038d2ef8SMaor Gottlieb 		dev->ib_dev.create_flow	= mlx5_ib_create_flow;
2444038d2ef8SMaor Gottlieb 		dev->ib_dev.destroy_flow = mlx5_ib_destroy_flow;
2445038d2ef8SMaor Gottlieb 		dev->ib_dev.uverbs_ex_cmd_mask |=
2446038d2ef8SMaor Gottlieb 			(1ull << IB_USER_VERBS_EX_CMD_CREATE_FLOW) |
2447038d2ef8SMaor Gottlieb 			(1ull << IB_USER_VERBS_EX_CMD_DESTROY_FLOW);
2448038d2ef8SMaor Gottlieb 	}
2449e126ba97SEli Cohen 	err = init_node_data(dev);
2450e126ba97SEli Cohen 	if (err)
2451233d05d2SSaeed Mahameed 		goto err_dealloc;
2452e126ba97SEli Cohen 
2453038d2ef8SMaor Gottlieb 	mutex_init(&dev->flow_db.lock);
2454e126ba97SEli Cohen 	mutex_init(&dev->cap_mask_mutex);
2455e126ba97SEli Cohen 
2456fc24fc5eSAchiad Shochat 	if (ll == IB_LINK_LAYER_ETHERNET) {
2457fc24fc5eSAchiad Shochat 		err = mlx5_enable_roce(dev);
2458e126ba97SEli Cohen 		if (err)
2459233d05d2SSaeed Mahameed 			goto err_dealloc;
2460fc24fc5eSAchiad Shochat 	}
2461fc24fc5eSAchiad Shochat 
2462fc24fc5eSAchiad Shochat 	err = create_dev_resources(&dev->devr);
2463fc24fc5eSAchiad Shochat 	if (err)
2464fc24fc5eSAchiad Shochat 		goto err_disable_roce;
2465e126ba97SEli Cohen 
24666aec21f6SHaggai Eran 	err = mlx5_ib_odp_init_one(dev);
2467281d1a92SWei Yongjun 	if (err)
2468e126ba97SEli Cohen 		goto err_rsrc;
2469e126ba97SEli Cohen 
24706aec21f6SHaggai Eran 	err = ib_register_device(&dev->ib_dev, NULL);
24716aec21f6SHaggai Eran 	if (err)
24726aec21f6SHaggai Eran 		goto err_odp;
24736aec21f6SHaggai Eran 
2474e126ba97SEli Cohen 	err = create_umr_res(dev);
2475e126ba97SEli Cohen 	if (err)
2476e126ba97SEli Cohen 		goto err_dev;
2477e126ba97SEli Cohen 
2478e126ba97SEli Cohen 	for (i = 0; i < ARRAY_SIZE(mlx5_class_attributes); i++) {
2479281d1a92SWei Yongjun 		err = device_create_file(&dev->ib_dev.dev,
2480281d1a92SWei Yongjun 					 mlx5_class_attributes[i]);
2481281d1a92SWei Yongjun 		if (err)
2482e126ba97SEli Cohen 			goto err_umrc;
2483e126ba97SEli Cohen 	}
2484e126ba97SEli Cohen 
2485e126ba97SEli Cohen 	dev->ib_active = true;
2486e126ba97SEli Cohen 
24879603b61dSJack Morgenstein 	return dev;
2488e126ba97SEli Cohen 
2489e126ba97SEli Cohen err_umrc:
2490e126ba97SEli Cohen 	destroy_umrc_res(dev);
2491e126ba97SEli Cohen 
2492e126ba97SEli Cohen err_dev:
2493e126ba97SEli Cohen 	ib_unregister_device(&dev->ib_dev);
2494e126ba97SEli Cohen 
24956aec21f6SHaggai Eran err_odp:
24966aec21f6SHaggai Eran 	mlx5_ib_odp_remove_one(dev);
24976aec21f6SHaggai Eran 
2498e126ba97SEli Cohen err_rsrc:
2499e126ba97SEli Cohen 	destroy_dev_resources(&dev->devr);
2500e126ba97SEli Cohen 
2501fc24fc5eSAchiad Shochat err_disable_roce:
2502fc24fc5eSAchiad Shochat 	if (ll == IB_LINK_LAYER_ETHERNET)
2503fc24fc5eSAchiad Shochat 		mlx5_disable_roce(dev);
2504fc24fc5eSAchiad Shochat 
25059603b61dSJack Morgenstein err_dealloc:
2506e126ba97SEli Cohen 	ib_dealloc_device((struct ib_device *)dev);
2507e126ba97SEli Cohen 
25089603b61dSJack Morgenstein 	return NULL;
2509e126ba97SEli Cohen }
2510e126ba97SEli Cohen 
25119603b61dSJack Morgenstein static void mlx5_ib_remove(struct mlx5_core_dev *mdev, void *context)
2512e126ba97SEli Cohen {
25139603b61dSJack Morgenstein 	struct mlx5_ib_dev *dev = context;
2514fc24fc5eSAchiad Shochat 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev, 1);
25156aec21f6SHaggai Eran 
2516e126ba97SEli Cohen 	ib_unregister_device(&dev->ib_dev);
2517eefd56e5SEli Cohen 	destroy_umrc_res(dev);
25186aec21f6SHaggai Eran 	mlx5_ib_odp_remove_one(dev);
2519e126ba97SEli Cohen 	destroy_dev_resources(&dev->devr);
2520fc24fc5eSAchiad Shochat 	if (ll == IB_LINK_LAYER_ETHERNET)
2521fc24fc5eSAchiad Shochat 		mlx5_disable_roce(dev);
2522e126ba97SEli Cohen 	ib_dealloc_device(&dev->ib_dev);
2523e126ba97SEli Cohen }
2524e126ba97SEli Cohen 
25259603b61dSJack Morgenstein static struct mlx5_interface mlx5_ib_interface = {
25269603b61dSJack Morgenstein 	.add            = mlx5_ib_add,
25279603b61dSJack Morgenstein 	.remove         = mlx5_ib_remove,
25289603b61dSJack Morgenstein 	.event          = mlx5_ib_event,
252964613d94SSaeed Mahameed 	.protocol	= MLX5_INTERFACE_PROTOCOL_IB,
2530e126ba97SEli Cohen };
2531e126ba97SEli Cohen 
2532e126ba97SEli Cohen static int __init mlx5_ib_init(void)
2533e126ba97SEli Cohen {
25346aec21f6SHaggai Eran 	int err;
25356aec21f6SHaggai Eran 
25369603b61dSJack Morgenstein 	if (deprecated_prof_sel != 2)
25379603b61dSJack Morgenstein 		pr_warn("prof_sel is deprecated for mlx5_ib, set it for mlx5_core\n");
25389603b61dSJack Morgenstein 
25396aec21f6SHaggai Eran 	err = mlx5_ib_odp_init();
25406aec21f6SHaggai Eran 	if (err)
25416aec21f6SHaggai Eran 		return err;
25426aec21f6SHaggai Eran 
25436aec21f6SHaggai Eran 	err = mlx5_register_interface(&mlx5_ib_interface);
25446aec21f6SHaggai Eran 	if (err)
25456aec21f6SHaggai Eran 		goto clean_odp;
25466aec21f6SHaggai Eran 
25476aec21f6SHaggai Eran 	return err;
25486aec21f6SHaggai Eran 
25496aec21f6SHaggai Eran clean_odp:
25506aec21f6SHaggai Eran 	mlx5_ib_odp_cleanup();
25516aec21f6SHaggai Eran 	return err;
2552e126ba97SEli Cohen }
2553e126ba97SEli Cohen 
2554e126ba97SEli Cohen static void __exit mlx5_ib_cleanup(void)
2555e126ba97SEli Cohen {
25569603b61dSJack Morgenstein 	mlx5_unregister_interface(&mlx5_ib_interface);
25576aec21f6SHaggai Eran 	mlx5_ib_odp_cleanup();
2558e126ba97SEli Cohen }
2559e126ba97SEli Cohen 
2560e126ba97SEli Cohen module_init(mlx5_ib_init);
2561e126ba97SEli Cohen module_exit(mlx5_ib_cleanup);
2562