xref: /openbmc/linux/drivers/infiniband/hw/mlx5/main.c (revision 146d2f1af3245a10b13eef263687e54f4e253d1d)
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>
41e126ba97SEli Cohen #include <linux/sched.h>
42e126ba97SEli Cohen #include <rdma/ib_user_verbs.h>
433f89a643SAchiad Shochat #include <rdma/ib_addr.h>
442811ba51SAchiad Shochat #include <rdma/ib_cache.h>
451b5daf11SMajd Dibbiny #include <linux/mlx5/vport.h>
46e126ba97SEli Cohen #include <rdma/ib_smi.h>
47e126ba97SEli Cohen #include <rdma/ib_umem.h>
48e126ba97SEli Cohen #include "user.h"
49e126ba97SEli Cohen #include "mlx5_ib.h"
50e126ba97SEli Cohen 
51e126ba97SEli Cohen #define DRIVER_NAME "mlx5_ib"
52169a1d85SAmir Vadai #define DRIVER_VERSION "2.2-1"
53169a1d85SAmir Vadai #define DRIVER_RELDATE	"Feb 2014"
54e126ba97SEli Cohen 
55e126ba97SEli Cohen MODULE_AUTHOR("Eli Cohen <eli@mellanox.com>");
56e126ba97SEli Cohen MODULE_DESCRIPTION("Mellanox Connect-IB HCA IB driver");
57e126ba97SEli Cohen MODULE_LICENSE("Dual BSD/GPL");
58e126ba97SEli Cohen MODULE_VERSION(DRIVER_VERSION);
59e126ba97SEli Cohen 
609603b61dSJack Morgenstein static int deprecated_prof_sel = 2;
619603b61dSJack Morgenstein module_param_named(prof_sel, deprecated_prof_sel, int, 0444);
629603b61dSJack Morgenstein MODULE_PARM_DESC(prof_sel, "profile selector. Deprecated here. Moved to module mlx5_core");
63e126ba97SEli Cohen 
64e126ba97SEli Cohen static char mlx5_version[] =
65e126ba97SEli Cohen 	DRIVER_NAME ": Mellanox Connect-IB Infiniband driver v"
66e126ba97SEli Cohen 	DRIVER_VERSION " (" DRIVER_RELDATE ")\n";
67e126ba97SEli Cohen 
68da7525d2SEran Ben Elisha enum {
69da7525d2SEran Ben Elisha 	MLX5_ATOMIC_SIZE_QP_8BYTES = 1 << 3,
70da7525d2SEran Ben Elisha };
71da7525d2SEran Ben Elisha 
721b5daf11SMajd Dibbiny static enum rdma_link_layer
73ebd61f68SAchiad Shochat mlx5_port_type_cap_to_rdma_ll(int port_type_cap)
741b5daf11SMajd Dibbiny {
75ebd61f68SAchiad Shochat 	switch (port_type_cap) {
761b5daf11SMajd Dibbiny 	case MLX5_CAP_PORT_TYPE_IB:
771b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_INFINIBAND;
781b5daf11SMajd Dibbiny 	case MLX5_CAP_PORT_TYPE_ETH:
791b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_ETHERNET;
801b5daf11SMajd Dibbiny 	default:
811b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_UNSPECIFIED;
821b5daf11SMajd Dibbiny 	}
831b5daf11SMajd Dibbiny }
841b5daf11SMajd Dibbiny 
85ebd61f68SAchiad Shochat static enum rdma_link_layer
86ebd61f68SAchiad Shochat mlx5_ib_port_link_layer(struct ib_device *device, u8 port_num)
87ebd61f68SAchiad Shochat {
88ebd61f68SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(device);
89ebd61f68SAchiad Shochat 	int port_type_cap = MLX5_CAP_GEN(dev->mdev, port_type);
90ebd61f68SAchiad Shochat 
91ebd61f68SAchiad Shochat 	return mlx5_port_type_cap_to_rdma_ll(port_type_cap);
92ebd61f68SAchiad Shochat }
93ebd61f68SAchiad Shochat 
94fc24fc5eSAchiad Shochat static int mlx5_netdev_event(struct notifier_block *this,
95fc24fc5eSAchiad Shochat 			     unsigned long event, void *ptr)
96fc24fc5eSAchiad Shochat {
97fc24fc5eSAchiad Shochat 	struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
98fc24fc5eSAchiad Shochat 	struct mlx5_ib_dev *ibdev = container_of(this, struct mlx5_ib_dev,
99fc24fc5eSAchiad Shochat 						 roce.nb);
100fc24fc5eSAchiad Shochat 
101fc24fc5eSAchiad Shochat 	if ((event != NETDEV_UNREGISTER) && (event != NETDEV_REGISTER))
102fc24fc5eSAchiad Shochat 		return NOTIFY_DONE;
103fc24fc5eSAchiad Shochat 
104fc24fc5eSAchiad Shochat 	write_lock(&ibdev->roce.netdev_lock);
105fc24fc5eSAchiad Shochat 	if (ndev->dev.parent == &ibdev->mdev->pdev->dev)
106fc24fc5eSAchiad Shochat 		ibdev->roce.netdev = (event == NETDEV_UNREGISTER) ? NULL : ndev;
107fc24fc5eSAchiad Shochat 	write_unlock(&ibdev->roce.netdev_lock);
108fc24fc5eSAchiad Shochat 
109fc24fc5eSAchiad Shochat 	return NOTIFY_DONE;
110fc24fc5eSAchiad Shochat }
111fc24fc5eSAchiad Shochat 
112fc24fc5eSAchiad Shochat static struct net_device *mlx5_ib_get_netdev(struct ib_device *device,
113fc24fc5eSAchiad Shochat 					     u8 port_num)
114fc24fc5eSAchiad Shochat {
115fc24fc5eSAchiad Shochat 	struct mlx5_ib_dev *ibdev = to_mdev(device);
116fc24fc5eSAchiad Shochat 	struct net_device *ndev;
117fc24fc5eSAchiad Shochat 
118fc24fc5eSAchiad Shochat 	/* Ensure ndev does not disappear before we invoke dev_hold()
119fc24fc5eSAchiad Shochat 	 */
120fc24fc5eSAchiad Shochat 	read_lock(&ibdev->roce.netdev_lock);
121fc24fc5eSAchiad Shochat 	ndev = ibdev->roce.netdev;
122fc24fc5eSAchiad Shochat 	if (ndev)
123fc24fc5eSAchiad Shochat 		dev_hold(ndev);
124fc24fc5eSAchiad Shochat 	read_unlock(&ibdev->roce.netdev_lock);
125fc24fc5eSAchiad Shochat 
126fc24fc5eSAchiad Shochat 	return ndev;
127fc24fc5eSAchiad Shochat }
128fc24fc5eSAchiad Shochat 
1293f89a643SAchiad Shochat static int mlx5_query_port_roce(struct ib_device *device, u8 port_num,
1303f89a643SAchiad Shochat 				struct ib_port_attr *props)
1313f89a643SAchiad Shochat {
1323f89a643SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(device);
1333f89a643SAchiad Shochat 	struct net_device *ndev;
1343f89a643SAchiad Shochat 	enum ib_mtu ndev_ib_mtu;
135c876a1b7SLeon Romanovsky 	u16 qkey_viol_cntr;
1363f89a643SAchiad Shochat 
1373f89a643SAchiad Shochat 	memset(props, 0, sizeof(*props));
1383f89a643SAchiad Shochat 
1393f89a643SAchiad Shochat 	props->port_cap_flags  |= IB_PORT_CM_SUP;
1403f89a643SAchiad Shochat 	props->port_cap_flags  |= IB_PORT_IP_BASED_GIDS;
1413f89a643SAchiad Shochat 
1423f89a643SAchiad Shochat 	props->gid_tbl_len      = MLX5_CAP_ROCE(dev->mdev,
1433f89a643SAchiad Shochat 						roce_address_table_size);
1443f89a643SAchiad Shochat 	props->max_mtu          = IB_MTU_4096;
1453f89a643SAchiad Shochat 	props->max_msg_sz       = 1 << MLX5_CAP_GEN(dev->mdev, log_max_msg);
1463f89a643SAchiad Shochat 	props->pkey_tbl_len     = 1;
1473f89a643SAchiad Shochat 	props->state            = IB_PORT_DOWN;
1483f89a643SAchiad Shochat 	props->phys_state       = 3;
1493f89a643SAchiad Shochat 
150c876a1b7SLeon Romanovsky 	mlx5_query_nic_vport_qkey_viol_cntr(dev->mdev, &qkey_viol_cntr);
151c876a1b7SLeon Romanovsky 	props->qkey_viol_cntr = qkey_viol_cntr;
1523f89a643SAchiad Shochat 
1533f89a643SAchiad Shochat 	ndev = mlx5_ib_get_netdev(device, port_num);
1543f89a643SAchiad Shochat 	if (!ndev)
1553f89a643SAchiad Shochat 		return 0;
1563f89a643SAchiad Shochat 
1573f89a643SAchiad Shochat 	if (netif_running(ndev) && netif_carrier_ok(ndev)) {
1583f89a643SAchiad Shochat 		props->state      = IB_PORT_ACTIVE;
1593f89a643SAchiad Shochat 		props->phys_state = 5;
1603f89a643SAchiad Shochat 	}
1613f89a643SAchiad Shochat 
1623f89a643SAchiad Shochat 	ndev_ib_mtu = iboe_get_mtu(ndev->mtu);
1633f89a643SAchiad Shochat 
1643f89a643SAchiad Shochat 	dev_put(ndev);
1653f89a643SAchiad Shochat 
1663f89a643SAchiad Shochat 	props->active_mtu	= min(props->max_mtu, ndev_ib_mtu);
1673f89a643SAchiad Shochat 
1683f89a643SAchiad Shochat 	props->active_width	= IB_WIDTH_4X;  /* TODO */
1693f89a643SAchiad Shochat 	props->active_speed	= IB_SPEED_QDR; /* TODO */
1703f89a643SAchiad Shochat 
1713f89a643SAchiad Shochat 	return 0;
1723f89a643SAchiad Shochat }
1733f89a643SAchiad Shochat 
1743cca2606SAchiad Shochat static void ib_gid_to_mlx5_roce_addr(const union ib_gid *gid,
1753cca2606SAchiad Shochat 				     const struct ib_gid_attr *attr,
1763cca2606SAchiad Shochat 				     void *mlx5_addr)
1773cca2606SAchiad Shochat {
1783cca2606SAchiad Shochat #define MLX5_SET_RA(p, f, v) MLX5_SET(roce_addr_layout, p, f, v)
1793cca2606SAchiad Shochat 	char *mlx5_addr_l3_addr	= MLX5_ADDR_OF(roce_addr_layout, mlx5_addr,
1803cca2606SAchiad Shochat 					       source_l3_address);
1813cca2606SAchiad Shochat 	void *mlx5_addr_mac	= MLX5_ADDR_OF(roce_addr_layout, mlx5_addr,
1823cca2606SAchiad Shochat 					       source_mac_47_32);
1833cca2606SAchiad Shochat 
1843cca2606SAchiad Shochat 	if (!gid)
1853cca2606SAchiad Shochat 		return;
1863cca2606SAchiad Shochat 
1873cca2606SAchiad Shochat 	ether_addr_copy(mlx5_addr_mac, attr->ndev->dev_addr);
1883cca2606SAchiad Shochat 
1893cca2606SAchiad Shochat 	if (is_vlan_dev(attr->ndev)) {
1903cca2606SAchiad Shochat 		MLX5_SET_RA(mlx5_addr, vlan_valid, 1);
1913cca2606SAchiad Shochat 		MLX5_SET_RA(mlx5_addr, vlan_id, vlan_dev_vlan_id(attr->ndev));
1923cca2606SAchiad Shochat 	}
1933cca2606SAchiad Shochat 
1943cca2606SAchiad Shochat 	switch (attr->gid_type) {
1953cca2606SAchiad Shochat 	case IB_GID_TYPE_IB:
1963cca2606SAchiad Shochat 		MLX5_SET_RA(mlx5_addr, roce_version, MLX5_ROCE_VERSION_1);
1973cca2606SAchiad Shochat 		break;
1983cca2606SAchiad Shochat 	case IB_GID_TYPE_ROCE_UDP_ENCAP:
1993cca2606SAchiad Shochat 		MLX5_SET_RA(mlx5_addr, roce_version, MLX5_ROCE_VERSION_2);
2003cca2606SAchiad Shochat 		break;
2013cca2606SAchiad Shochat 
2023cca2606SAchiad Shochat 	default:
2033cca2606SAchiad Shochat 		WARN_ON(true);
2043cca2606SAchiad Shochat 	}
2053cca2606SAchiad Shochat 
2063cca2606SAchiad Shochat 	if (attr->gid_type != IB_GID_TYPE_IB) {
2073cca2606SAchiad Shochat 		if (ipv6_addr_v4mapped((void *)gid))
2083cca2606SAchiad Shochat 			MLX5_SET_RA(mlx5_addr, roce_l3_type,
2093cca2606SAchiad Shochat 				    MLX5_ROCE_L3_TYPE_IPV4);
2103cca2606SAchiad Shochat 		else
2113cca2606SAchiad Shochat 			MLX5_SET_RA(mlx5_addr, roce_l3_type,
2123cca2606SAchiad Shochat 				    MLX5_ROCE_L3_TYPE_IPV6);
2133cca2606SAchiad Shochat 	}
2143cca2606SAchiad Shochat 
2153cca2606SAchiad Shochat 	if ((attr->gid_type == IB_GID_TYPE_IB) ||
2163cca2606SAchiad Shochat 	    !ipv6_addr_v4mapped((void *)gid))
2173cca2606SAchiad Shochat 		memcpy(mlx5_addr_l3_addr, gid, sizeof(*gid));
2183cca2606SAchiad Shochat 	else
2193cca2606SAchiad Shochat 		memcpy(&mlx5_addr_l3_addr[12], &gid->raw[12], 4);
2203cca2606SAchiad Shochat }
2213cca2606SAchiad Shochat 
2223cca2606SAchiad Shochat static int set_roce_addr(struct ib_device *device, u8 port_num,
2233cca2606SAchiad Shochat 			 unsigned int index,
2243cca2606SAchiad Shochat 			 const union ib_gid *gid,
2253cca2606SAchiad Shochat 			 const struct ib_gid_attr *attr)
2263cca2606SAchiad Shochat {
2273cca2606SAchiad Shochat 	struct mlx5_ib_dev *dev	= to_mdev(device);
2283cca2606SAchiad Shochat 	u32  in[MLX5_ST_SZ_DW(set_roce_address_in)];
2293cca2606SAchiad Shochat 	u32 out[MLX5_ST_SZ_DW(set_roce_address_out)];
2303cca2606SAchiad Shochat 	void *in_addr = MLX5_ADDR_OF(set_roce_address_in, in, roce_address);
2313cca2606SAchiad Shochat 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(device, port_num);
2323cca2606SAchiad Shochat 
2333cca2606SAchiad Shochat 	if (ll != IB_LINK_LAYER_ETHERNET)
2343cca2606SAchiad Shochat 		return -EINVAL;
2353cca2606SAchiad Shochat 
2363cca2606SAchiad Shochat 	memset(in, 0, sizeof(in));
2373cca2606SAchiad Shochat 
2383cca2606SAchiad Shochat 	ib_gid_to_mlx5_roce_addr(gid, attr, in_addr);
2393cca2606SAchiad Shochat 
2403cca2606SAchiad Shochat 	MLX5_SET(set_roce_address_in, in, roce_address_index, index);
2413cca2606SAchiad Shochat 	MLX5_SET(set_roce_address_in, in, opcode, MLX5_CMD_OP_SET_ROCE_ADDRESS);
2423cca2606SAchiad Shochat 
2433cca2606SAchiad Shochat 	memset(out, 0, sizeof(out));
2443cca2606SAchiad Shochat 	return mlx5_cmd_exec(dev->mdev, in, sizeof(in), out, sizeof(out));
2453cca2606SAchiad Shochat }
2463cca2606SAchiad Shochat 
2473cca2606SAchiad Shochat static int mlx5_ib_add_gid(struct ib_device *device, u8 port_num,
2483cca2606SAchiad Shochat 			   unsigned int index, const union ib_gid *gid,
2493cca2606SAchiad Shochat 			   const struct ib_gid_attr *attr,
2503cca2606SAchiad Shochat 			   __always_unused void **context)
2513cca2606SAchiad Shochat {
2523cca2606SAchiad Shochat 	return set_roce_addr(device, port_num, index, gid, attr);
2533cca2606SAchiad Shochat }
2543cca2606SAchiad Shochat 
2553cca2606SAchiad Shochat static int mlx5_ib_del_gid(struct ib_device *device, u8 port_num,
2563cca2606SAchiad Shochat 			   unsigned int index, __always_unused void **context)
2573cca2606SAchiad Shochat {
2583cca2606SAchiad Shochat 	return set_roce_addr(device, port_num, index, NULL, NULL);
2593cca2606SAchiad Shochat }
2603cca2606SAchiad Shochat 
2612811ba51SAchiad Shochat __be16 mlx5_get_roce_udp_sport(struct mlx5_ib_dev *dev, u8 port_num,
2622811ba51SAchiad Shochat 			       int index)
2632811ba51SAchiad Shochat {
2642811ba51SAchiad Shochat 	struct ib_gid_attr attr;
2652811ba51SAchiad Shochat 	union ib_gid gid;
2662811ba51SAchiad Shochat 
2672811ba51SAchiad Shochat 	if (ib_get_cached_gid(&dev->ib_dev, port_num, index, &gid, &attr))
2682811ba51SAchiad Shochat 		return 0;
2692811ba51SAchiad Shochat 
2702811ba51SAchiad Shochat 	if (!attr.ndev)
2712811ba51SAchiad Shochat 		return 0;
2722811ba51SAchiad Shochat 
2732811ba51SAchiad Shochat 	dev_put(attr.ndev);
2742811ba51SAchiad Shochat 
2752811ba51SAchiad Shochat 	if (attr.gid_type != IB_GID_TYPE_ROCE_UDP_ENCAP)
2762811ba51SAchiad Shochat 		return 0;
2772811ba51SAchiad Shochat 
2782811ba51SAchiad Shochat 	return cpu_to_be16(MLX5_CAP_ROCE(dev->mdev, r_roce_min_src_udp_port));
2792811ba51SAchiad Shochat }
2802811ba51SAchiad Shochat 
2811b5daf11SMajd Dibbiny static int mlx5_use_mad_ifc(struct mlx5_ib_dev *dev)
2821b5daf11SMajd Dibbiny {
2831b5daf11SMajd Dibbiny 	return !dev->mdev->issi;
2841b5daf11SMajd Dibbiny }
2851b5daf11SMajd Dibbiny 
2861b5daf11SMajd Dibbiny enum {
2871b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_MAD,
2881b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_HCA,
2891b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_NIC,
2901b5daf11SMajd Dibbiny };
2911b5daf11SMajd Dibbiny 
2921b5daf11SMajd Dibbiny static int mlx5_get_vport_access_method(struct ib_device *ibdev)
2931b5daf11SMajd Dibbiny {
2941b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(to_mdev(ibdev)))
2951b5daf11SMajd Dibbiny 		return MLX5_VPORT_ACCESS_METHOD_MAD;
2961b5daf11SMajd Dibbiny 
297ebd61f68SAchiad Shochat 	if (mlx5_ib_port_link_layer(ibdev, 1) ==
2981b5daf11SMajd Dibbiny 	    IB_LINK_LAYER_ETHERNET)
2991b5daf11SMajd Dibbiny 		return MLX5_VPORT_ACCESS_METHOD_NIC;
3001b5daf11SMajd Dibbiny 
3011b5daf11SMajd Dibbiny 	return MLX5_VPORT_ACCESS_METHOD_HCA;
3021b5daf11SMajd Dibbiny }
3031b5daf11SMajd Dibbiny 
304da7525d2SEran Ben Elisha static void get_atomic_caps(struct mlx5_ib_dev *dev,
305da7525d2SEran Ben Elisha 			    struct ib_device_attr *props)
306da7525d2SEran Ben Elisha {
307da7525d2SEran Ben Elisha 	u8 tmp;
308da7525d2SEran Ben Elisha 	u8 atomic_operations = MLX5_CAP_ATOMIC(dev->mdev, atomic_operations);
309da7525d2SEran Ben Elisha 	u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_qp);
310da7525d2SEran Ben Elisha 	u8 atomic_req_8B_endianness_mode =
311da7525d2SEran Ben Elisha 		MLX5_CAP_ATOMIC(dev->mdev, atomic_req_8B_endianess_mode);
312da7525d2SEran Ben Elisha 
313da7525d2SEran Ben Elisha 	/* Check if HW supports 8 bytes standard atomic operations and capable
314da7525d2SEran Ben Elisha 	 * of host endianness respond
315da7525d2SEran Ben Elisha 	 */
316da7525d2SEran Ben Elisha 	tmp = MLX5_ATOMIC_OPS_CMP_SWAP | MLX5_ATOMIC_OPS_FETCH_ADD;
317da7525d2SEran Ben Elisha 	if (((atomic_operations & tmp) == tmp) &&
318da7525d2SEran Ben Elisha 	    (atomic_size_qp & MLX5_ATOMIC_SIZE_QP_8BYTES) &&
319da7525d2SEran Ben Elisha 	    (atomic_req_8B_endianness_mode)) {
320da7525d2SEran Ben Elisha 		props->atomic_cap = IB_ATOMIC_HCA;
321da7525d2SEran Ben Elisha 	} else {
322da7525d2SEran Ben Elisha 		props->atomic_cap = IB_ATOMIC_NONE;
323da7525d2SEran Ben Elisha 	}
324da7525d2SEran Ben Elisha }
325da7525d2SEran Ben Elisha 
3261b5daf11SMajd Dibbiny static int mlx5_query_system_image_guid(struct ib_device *ibdev,
3271b5daf11SMajd Dibbiny 					__be64 *sys_image_guid)
3281b5daf11SMajd Dibbiny {
3291b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
3301b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
3311b5daf11SMajd Dibbiny 	u64 tmp;
3321b5daf11SMajd Dibbiny 	int err;
3331b5daf11SMajd Dibbiny 
3341b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
3351b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
3361b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_system_image_guid(ibdev,
3371b5daf11SMajd Dibbiny 							    sys_image_guid);
3381b5daf11SMajd Dibbiny 
3391b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
3401b5daf11SMajd Dibbiny 		err = mlx5_query_hca_vport_system_image_guid(mdev, &tmp);
3413f89a643SAchiad Shochat 		break;
3423f89a643SAchiad Shochat 
3433f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
3443f89a643SAchiad Shochat 		err = mlx5_query_nic_vport_system_image_guid(mdev, &tmp);
3453f89a643SAchiad Shochat 		break;
3461b5daf11SMajd Dibbiny 
3471b5daf11SMajd Dibbiny 	default:
3481b5daf11SMajd Dibbiny 		return -EINVAL;
3491b5daf11SMajd Dibbiny 	}
3503f89a643SAchiad Shochat 
3513f89a643SAchiad Shochat 	if (!err)
3523f89a643SAchiad Shochat 		*sys_image_guid = cpu_to_be64(tmp);
3533f89a643SAchiad Shochat 
3543f89a643SAchiad Shochat 	return err;
3553f89a643SAchiad Shochat 
3561b5daf11SMajd Dibbiny }
3571b5daf11SMajd Dibbiny 
3581b5daf11SMajd Dibbiny static int mlx5_query_max_pkeys(struct ib_device *ibdev,
3591b5daf11SMajd Dibbiny 				u16 *max_pkeys)
3601b5daf11SMajd Dibbiny {
3611b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
3621b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
3631b5daf11SMajd Dibbiny 
3641b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
3651b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
3661b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_max_pkeys(ibdev, max_pkeys);
3671b5daf11SMajd Dibbiny 
3681b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
3691b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
3701b5daf11SMajd Dibbiny 		*max_pkeys = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev,
3711b5daf11SMajd Dibbiny 						pkey_table_size));
3721b5daf11SMajd Dibbiny 		return 0;
3731b5daf11SMajd Dibbiny 
3741b5daf11SMajd Dibbiny 	default:
3751b5daf11SMajd Dibbiny 		return -EINVAL;
3761b5daf11SMajd Dibbiny 	}
3771b5daf11SMajd Dibbiny }
3781b5daf11SMajd Dibbiny 
3791b5daf11SMajd Dibbiny static int mlx5_query_vendor_id(struct ib_device *ibdev,
3801b5daf11SMajd Dibbiny 				u32 *vendor_id)
3811b5daf11SMajd Dibbiny {
3821b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
3831b5daf11SMajd Dibbiny 
3841b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
3851b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
3861b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_vendor_id(ibdev, vendor_id);
3871b5daf11SMajd Dibbiny 
3881b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
3891b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
3901b5daf11SMajd Dibbiny 		return mlx5_core_query_vendor_id(dev->mdev, vendor_id);
3911b5daf11SMajd Dibbiny 
3921b5daf11SMajd Dibbiny 	default:
3931b5daf11SMajd Dibbiny 		return -EINVAL;
3941b5daf11SMajd Dibbiny 	}
3951b5daf11SMajd Dibbiny }
3961b5daf11SMajd Dibbiny 
3971b5daf11SMajd Dibbiny static int mlx5_query_node_guid(struct mlx5_ib_dev *dev,
3981b5daf11SMajd Dibbiny 				__be64 *node_guid)
3991b5daf11SMajd Dibbiny {
4001b5daf11SMajd Dibbiny 	u64 tmp;
4011b5daf11SMajd Dibbiny 	int err;
4021b5daf11SMajd Dibbiny 
4031b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(&dev->ib_dev)) {
4041b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
4051b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_node_guid(dev, node_guid);
4061b5daf11SMajd Dibbiny 
4071b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
4081b5daf11SMajd Dibbiny 		err = mlx5_query_hca_vport_node_guid(dev->mdev, &tmp);
4093f89a643SAchiad Shochat 		break;
4103f89a643SAchiad Shochat 
4113f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
4123f89a643SAchiad Shochat 		err = mlx5_query_nic_vport_node_guid(dev->mdev, &tmp);
4133f89a643SAchiad Shochat 		break;
4141b5daf11SMajd Dibbiny 
4151b5daf11SMajd Dibbiny 	default:
4161b5daf11SMajd Dibbiny 		return -EINVAL;
4171b5daf11SMajd Dibbiny 	}
4183f89a643SAchiad Shochat 
4193f89a643SAchiad Shochat 	if (!err)
4203f89a643SAchiad Shochat 		*node_guid = cpu_to_be64(tmp);
4213f89a643SAchiad Shochat 
4223f89a643SAchiad Shochat 	return err;
4231b5daf11SMajd Dibbiny }
4241b5daf11SMajd Dibbiny 
4251b5daf11SMajd Dibbiny struct mlx5_reg_node_desc {
4261b5daf11SMajd Dibbiny 	u8	desc[64];
4271b5daf11SMajd Dibbiny };
4281b5daf11SMajd Dibbiny 
4291b5daf11SMajd Dibbiny static int mlx5_query_node_desc(struct mlx5_ib_dev *dev, char *node_desc)
4301b5daf11SMajd Dibbiny {
4311b5daf11SMajd Dibbiny 	struct mlx5_reg_node_desc in;
4321b5daf11SMajd Dibbiny 
4331b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(dev))
4341b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_node_desc(dev, node_desc);
4351b5daf11SMajd Dibbiny 
4361b5daf11SMajd Dibbiny 	memset(&in, 0, sizeof(in));
4371b5daf11SMajd Dibbiny 
4381b5daf11SMajd Dibbiny 	return mlx5_core_access_reg(dev->mdev, &in, sizeof(in), node_desc,
4391b5daf11SMajd Dibbiny 				    sizeof(struct mlx5_reg_node_desc),
4401b5daf11SMajd Dibbiny 				    MLX5_REG_NODE_DESC, 0, 0);
4411b5daf11SMajd Dibbiny }
4421b5daf11SMajd Dibbiny 
443e126ba97SEli Cohen static int mlx5_ib_query_device(struct ib_device *ibdev,
4442528e33eSMatan Barak 				struct ib_device_attr *props,
4452528e33eSMatan Barak 				struct ib_udata *uhw)
446e126ba97SEli Cohen {
447e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
448938fe83cSSaeed Mahameed 	struct mlx5_core_dev *mdev = dev->mdev;
449e126ba97SEli Cohen 	int err = -ENOMEM;
450e126ba97SEli Cohen 	int max_rq_sg;
451e126ba97SEli Cohen 	int max_sq_sg;
452e0238a6aSSagi Grimberg 	u64 min_page_size = 1ull << MLX5_CAP_GEN(mdev, log_pg_sz);
453e126ba97SEli Cohen 
4542528e33eSMatan Barak 	if (uhw->inlen || uhw->outlen)
4552528e33eSMatan Barak 		return -EINVAL;
4562528e33eSMatan Barak 
457e126ba97SEli Cohen 	memset(props, 0, sizeof(*props));
4581b5daf11SMajd Dibbiny 	err = mlx5_query_system_image_guid(ibdev,
4591b5daf11SMajd Dibbiny 					   &props->sys_image_guid);
4601b5daf11SMajd Dibbiny 	if (err)
4611b5daf11SMajd Dibbiny 		return err;
4621b5daf11SMajd Dibbiny 
4631b5daf11SMajd Dibbiny 	err = mlx5_query_max_pkeys(ibdev, &props->max_pkeys);
4641b5daf11SMajd Dibbiny 	if (err)
4651b5daf11SMajd Dibbiny 		return err;
4661b5daf11SMajd Dibbiny 
4671b5daf11SMajd Dibbiny 	err = mlx5_query_vendor_id(ibdev, &props->vendor_id);
4681b5daf11SMajd Dibbiny 	if (err)
4691b5daf11SMajd Dibbiny 		return err;
470e126ba97SEli Cohen 
4719603b61dSJack Morgenstein 	props->fw_ver = ((u64)fw_rev_maj(dev->mdev) << 32) |
4729603b61dSJack Morgenstein 		(fw_rev_min(dev->mdev) << 16) |
4739603b61dSJack Morgenstein 		fw_rev_sub(dev->mdev);
474e126ba97SEli Cohen 	props->device_cap_flags    = IB_DEVICE_CHANGE_PHY_PORT |
475e126ba97SEli Cohen 		IB_DEVICE_PORT_ACTIVE_EVENT		|
476e126ba97SEli Cohen 		IB_DEVICE_SYS_IMAGE_GUID		|
4771a4c3a3dSEli Cohen 		IB_DEVICE_RC_RNR_NAK_GEN;
478938fe83cSSaeed Mahameed 
479938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, pkv))
480e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
481938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, qkv))
482e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
483938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, apm))
484e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
485938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, xrc))
486e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_XRC;
487e126ba97SEli Cohen 	props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
488938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, sho)) {
4892dea9094SSagi Grimberg 		props->device_cap_flags |= IB_DEVICE_SIGNATURE_HANDOVER;
4902dea9094SSagi Grimberg 		/* At this stage no support for signature handover */
4912dea9094SSagi Grimberg 		props->sig_prot_cap = IB_PROT_T10DIF_TYPE_1 |
4922dea9094SSagi Grimberg 				      IB_PROT_T10DIF_TYPE_2 |
4932dea9094SSagi Grimberg 				      IB_PROT_T10DIF_TYPE_3;
4942dea9094SSagi Grimberg 		props->sig_guard_cap = IB_GUARD_T10DIF_CRC |
4952dea9094SSagi Grimberg 				       IB_GUARD_T10DIF_CSUM;
4962dea9094SSagi Grimberg 	}
497938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, block_lb_mc))
498f360d88aSEli Cohen 		props->device_cap_flags |= IB_DEVICE_BLOCK_MULTICAST_LOOPBACK;
499e126ba97SEli Cohen 
50088115fe7SBodong Wang 	if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
50188115fe7SBodong Wang 	    (MLX5_CAP_ETH(dev->mdev, csum_cap)))
50288115fe7SBodong Wang 			props->device_cap_flags |= IB_DEVICE_RAW_IP_CSUM;
50388115fe7SBodong Wang 
5041b5daf11SMajd Dibbiny 	props->vendor_part_id	   = mdev->pdev->device;
5051b5daf11SMajd Dibbiny 	props->hw_ver		   = mdev->pdev->revision;
506e126ba97SEli Cohen 
507e126ba97SEli Cohen 	props->max_mr_size	   = ~0ull;
508e0238a6aSSagi Grimberg 	props->page_size_cap	   = ~(min_page_size - 1);
509938fe83cSSaeed Mahameed 	props->max_qp		   = 1 << MLX5_CAP_GEN(mdev, log_max_qp);
510938fe83cSSaeed Mahameed 	props->max_qp_wr	   = 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
511938fe83cSSaeed Mahameed 	max_rq_sg =  MLX5_CAP_GEN(mdev, max_wqe_sz_rq) /
512938fe83cSSaeed Mahameed 		     sizeof(struct mlx5_wqe_data_seg);
513938fe83cSSaeed Mahameed 	max_sq_sg = (MLX5_CAP_GEN(mdev, max_wqe_sz_sq) -
514938fe83cSSaeed Mahameed 		     sizeof(struct mlx5_wqe_ctrl_seg)) /
515e126ba97SEli Cohen 		     sizeof(struct mlx5_wqe_data_seg);
516e126ba97SEli Cohen 	props->max_sge = min(max_rq_sg, max_sq_sg);
51718ebd407SSagi Grimberg 	props->max_sge_rd = props->max_sge;
518938fe83cSSaeed Mahameed 	props->max_cq		   = 1 << MLX5_CAP_GEN(mdev, log_max_cq);
5199f177686SLeon Romanovsky 	props->max_cqe = (1 << MLX5_CAP_GEN(mdev, log_max_cq_sz)) - 1;
520938fe83cSSaeed Mahameed 	props->max_mr		   = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
521938fe83cSSaeed Mahameed 	props->max_pd		   = 1 << MLX5_CAP_GEN(mdev, log_max_pd);
522938fe83cSSaeed Mahameed 	props->max_qp_rd_atom	   = 1 << MLX5_CAP_GEN(mdev, log_max_ra_req_qp);
523938fe83cSSaeed Mahameed 	props->max_qp_init_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_res_qp);
524938fe83cSSaeed Mahameed 	props->max_srq		   = 1 << MLX5_CAP_GEN(mdev, log_max_srq);
525938fe83cSSaeed Mahameed 	props->max_srq_wr = (1 << MLX5_CAP_GEN(mdev, log_max_srq_sz)) - 1;
526938fe83cSSaeed Mahameed 	props->local_ca_ack_delay  = MLX5_CAP_GEN(mdev, local_ca_ack_delay);
527e126ba97SEli Cohen 	props->max_res_rd_atom	   = props->max_qp_rd_atom * props->max_qp;
528e126ba97SEli Cohen 	props->max_srq_sge	   = max_rq_sg - 1;
529e126ba97SEli Cohen 	props->max_fast_reg_page_list_len = (unsigned int)-1;
530da7525d2SEran Ben Elisha 	get_atomic_caps(dev, props);
53181bea28fSEli Cohen 	props->masked_atomic_cap   = IB_ATOMIC_NONE;
532938fe83cSSaeed Mahameed 	props->max_mcast_grp	   = 1 << MLX5_CAP_GEN(mdev, log_max_mcg);
533938fe83cSSaeed Mahameed 	props->max_mcast_qp_attach = MLX5_CAP_GEN(mdev, max_qp_mcg);
534e126ba97SEli Cohen 	props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
535e126ba97SEli Cohen 					   props->max_mcast_grp;
536e126ba97SEli Cohen 	props->max_map_per_fmr = INT_MAX; /* no limit in ConnectIB */
5377c60bcbbSMatan Barak 	props->hca_core_clock = MLX5_CAP_GEN(mdev, device_frequency_khz);
5387c60bcbbSMatan Barak 	props->timestamp_mask = 0x7FFFFFFFFFFFFFFFULL;
539e126ba97SEli Cohen 
5408cdd312cSHaggai Eran #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
541938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, pg))
5428cdd312cSHaggai Eran 		props->device_cap_flags |= IB_DEVICE_ON_DEMAND_PAGING;
5438cdd312cSHaggai Eran 	props->odp_caps = dev->odp_caps;
5448cdd312cSHaggai Eran #endif
5458cdd312cSHaggai Eran 
546051f2630SLeon Romanovsky 	if (MLX5_CAP_GEN(mdev, cd))
547051f2630SLeon Romanovsky 		props->device_cap_flags |= IB_DEVICE_CROSS_CHANNEL;
548051f2630SLeon Romanovsky 
5491b5daf11SMajd Dibbiny 	return 0;
5501b5daf11SMajd Dibbiny }
551e126ba97SEli Cohen 
5521b5daf11SMajd Dibbiny enum mlx5_ib_width {
5531b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_1X	= 1 << 0,
5541b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_2X	= 1 << 1,
5551b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_4X	= 1 << 2,
5561b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_8X	= 1 << 3,
5571b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_12X	= 1 << 4
5581b5daf11SMajd Dibbiny };
5591b5daf11SMajd Dibbiny 
5601b5daf11SMajd Dibbiny static int translate_active_width(struct ib_device *ibdev, u8 active_width,
5611b5daf11SMajd Dibbiny 				  u8 *ib_width)
5621b5daf11SMajd Dibbiny {
5631b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
5641b5daf11SMajd Dibbiny 	int err = 0;
5651b5daf11SMajd Dibbiny 
5661b5daf11SMajd Dibbiny 	if (active_width & MLX5_IB_WIDTH_1X) {
5671b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_1X;
5681b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_2X) {
5691b5daf11SMajd Dibbiny 		mlx5_ib_dbg(dev, "active_width %d is not supported by IB spec\n",
5701b5daf11SMajd Dibbiny 			    (int)active_width);
5711b5daf11SMajd Dibbiny 		err = -EINVAL;
5721b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_4X) {
5731b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_4X;
5741b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_8X) {
5751b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_8X;
5761b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_12X) {
5771b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_12X;
5781b5daf11SMajd Dibbiny 	} else {
5791b5daf11SMajd Dibbiny 		mlx5_ib_dbg(dev, "Invalid active_width %d\n",
5801b5daf11SMajd Dibbiny 			    (int)active_width);
5811b5daf11SMajd Dibbiny 		err = -EINVAL;
5821b5daf11SMajd Dibbiny 	}
5831b5daf11SMajd Dibbiny 
5841b5daf11SMajd Dibbiny 	return err;
5851b5daf11SMajd Dibbiny }
5861b5daf11SMajd Dibbiny 
5871b5daf11SMajd Dibbiny static int mlx5_mtu_to_ib_mtu(int mtu)
5881b5daf11SMajd Dibbiny {
5891b5daf11SMajd Dibbiny 	switch (mtu) {
5901b5daf11SMajd Dibbiny 	case 256: return 1;
5911b5daf11SMajd Dibbiny 	case 512: return 2;
5921b5daf11SMajd Dibbiny 	case 1024: return 3;
5931b5daf11SMajd Dibbiny 	case 2048: return 4;
5941b5daf11SMajd Dibbiny 	case 4096: return 5;
5951b5daf11SMajd Dibbiny 	default:
5961b5daf11SMajd Dibbiny 		pr_warn("invalid mtu\n");
5971b5daf11SMajd Dibbiny 		return -1;
5981b5daf11SMajd Dibbiny 	}
5991b5daf11SMajd Dibbiny }
6001b5daf11SMajd Dibbiny 
6011b5daf11SMajd Dibbiny enum ib_max_vl_num {
6021b5daf11SMajd Dibbiny 	__IB_MAX_VL_0		= 1,
6031b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_1		= 2,
6041b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_3		= 3,
6051b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_7		= 4,
6061b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_14	= 5,
6071b5daf11SMajd Dibbiny };
6081b5daf11SMajd Dibbiny 
6091b5daf11SMajd Dibbiny enum mlx5_vl_hw_cap {
6101b5daf11SMajd Dibbiny 	MLX5_VL_HW_0	= 1,
6111b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_1	= 2,
6121b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_2	= 3,
6131b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_3	= 4,
6141b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_4	= 5,
6151b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_5	= 6,
6161b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_6	= 7,
6171b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_7	= 8,
6181b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_14	= 15
6191b5daf11SMajd Dibbiny };
6201b5daf11SMajd Dibbiny 
6211b5daf11SMajd Dibbiny static int translate_max_vl_num(struct ib_device *ibdev, u8 vl_hw_cap,
6221b5daf11SMajd Dibbiny 				u8 *max_vl_num)
6231b5daf11SMajd Dibbiny {
6241b5daf11SMajd Dibbiny 	switch (vl_hw_cap) {
6251b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0:
6261b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0;
6271b5daf11SMajd Dibbiny 		break;
6281b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_1:
6291b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_1;
6301b5daf11SMajd Dibbiny 		break;
6311b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_3:
6321b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_3;
6331b5daf11SMajd Dibbiny 		break;
6341b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_7:
6351b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_7;
6361b5daf11SMajd Dibbiny 		break;
6371b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_14:
6381b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_14;
6391b5daf11SMajd Dibbiny 		break;
6401b5daf11SMajd Dibbiny 
6411b5daf11SMajd Dibbiny 	default:
6421b5daf11SMajd Dibbiny 		return -EINVAL;
6431b5daf11SMajd Dibbiny 	}
6441b5daf11SMajd Dibbiny 
6451b5daf11SMajd Dibbiny 	return 0;
6461b5daf11SMajd Dibbiny }
6471b5daf11SMajd Dibbiny 
6481b5daf11SMajd Dibbiny static int mlx5_query_hca_port(struct ib_device *ibdev, u8 port,
6491b5daf11SMajd Dibbiny 			       struct ib_port_attr *props)
6501b5daf11SMajd Dibbiny {
6511b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
6521b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
6531b5daf11SMajd Dibbiny 	struct mlx5_hca_vport_context *rep;
6541b5daf11SMajd Dibbiny 	int max_mtu;
6551b5daf11SMajd Dibbiny 	int oper_mtu;
6561b5daf11SMajd Dibbiny 	int err;
6571b5daf11SMajd Dibbiny 	u8 ib_link_width_oper;
6581b5daf11SMajd Dibbiny 	u8 vl_hw_cap;
6591b5daf11SMajd Dibbiny 
6601b5daf11SMajd Dibbiny 	rep = kzalloc(sizeof(*rep), GFP_KERNEL);
6611b5daf11SMajd Dibbiny 	if (!rep) {
6621b5daf11SMajd Dibbiny 		err = -ENOMEM;
6631b5daf11SMajd Dibbiny 		goto out;
6641b5daf11SMajd Dibbiny 	}
6651b5daf11SMajd Dibbiny 
6661b5daf11SMajd Dibbiny 	memset(props, 0, sizeof(*props));
6671b5daf11SMajd Dibbiny 
6681b5daf11SMajd Dibbiny 	err = mlx5_query_hca_vport_context(mdev, 0, port, 0, rep);
6691b5daf11SMajd Dibbiny 	if (err)
6701b5daf11SMajd Dibbiny 		goto out;
6711b5daf11SMajd Dibbiny 
6721b5daf11SMajd Dibbiny 	props->lid		= rep->lid;
6731b5daf11SMajd Dibbiny 	props->lmc		= rep->lmc;
6741b5daf11SMajd Dibbiny 	props->sm_lid		= rep->sm_lid;
6751b5daf11SMajd Dibbiny 	props->sm_sl		= rep->sm_sl;
6761b5daf11SMajd Dibbiny 	props->state		= rep->vport_state;
6771b5daf11SMajd Dibbiny 	props->phys_state	= rep->port_physical_state;
6781b5daf11SMajd Dibbiny 	props->port_cap_flags	= rep->cap_mask1;
6791b5daf11SMajd Dibbiny 	props->gid_tbl_len	= mlx5_get_gid_table_len(MLX5_CAP_GEN(mdev, gid_table_size));
6801b5daf11SMajd Dibbiny 	props->max_msg_sz	= 1 << MLX5_CAP_GEN(mdev, log_max_msg);
6811b5daf11SMajd Dibbiny 	props->pkey_tbl_len	= mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, pkey_table_size));
6821b5daf11SMajd Dibbiny 	props->bad_pkey_cntr	= rep->pkey_violation_counter;
6831b5daf11SMajd Dibbiny 	props->qkey_viol_cntr	= rep->qkey_violation_counter;
6841b5daf11SMajd Dibbiny 	props->subnet_timeout	= rep->subnet_timeout;
6851b5daf11SMajd Dibbiny 	props->init_type_reply	= rep->init_type_reply;
6861b5daf11SMajd Dibbiny 
6871b5daf11SMajd Dibbiny 	err = mlx5_query_port_link_width_oper(mdev, &ib_link_width_oper, port);
6881b5daf11SMajd Dibbiny 	if (err)
6891b5daf11SMajd Dibbiny 		goto out;
6901b5daf11SMajd Dibbiny 
6911b5daf11SMajd Dibbiny 	err = translate_active_width(ibdev, ib_link_width_oper,
6921b5daf11SMajd Dibbiny 				     &props->active_width);
6931b5daf11SMajd Dibbiny 	if (err)
6941b5daf11SMajd Dibbiny 		goto out;
6951b5daf11SMajd Dibbiny 	err = mlx5_query_port_proto_oper(mdev, &props->active_speed, MLX5_PTYS_IB,
6961b5daf11SMajd Dibbiny 					 port);
6971b5daf11SMajd Dibbiny 	if (err)
6981b5daf11SMajd Dibbiny 		goto out;
6991b5daf11SMajd Dibbiny 
700facc9699SSaeed Mahameed 	mlx5_query_port_max_mtu(mdev, &max_mtu, port);
7011b5daf11SMajd Dibbiny 
7021b5daf11SMajd Dibbiny 	props->max_mtu = mlx5_mtu_to_ib_mtu(max_mtu);
7031b5daf11SMajd Dibbiny 
704facc9699SSaeed Mahameed 	mlx5_query_port_oper_mtu(mdev, &oper_mtu, port);
7051b5daf11SMajd Dibbiny 
7061b5daf11SMajd Dibbiny 	props->active_mtu = mlx5_mtu_to_ib_mtu(oper_mtu);
7071b5daf11SMajd Dibbiny 
7081b5daf11SMajd Dibbiny 	err = mlx5_query_port_vl_hw_cap(mdev, &vl_hw_cap, port);
7091b5daf11SMajd Dibbiny 	if (err)
7101b5daf11SMajd Dibbiny 		goto out;
7111b5daf11SMajd Dibbiny 
7121b5daf11SMajd Dibbiny 	err = translate_max_vl_num(ibdev, vl_hw_cap,
7131b5daf11SMajd Dibbiny 				   &props->max_vl_num);
7141b5daf11SMajd Dibbiny out:
7151b5daf11SMajd Dibbiny 	kfree(rep);
716e126ba97SEli Cohen 	return err;
717e126ba97SEli Cohen }
718e126ba97SEli Cohen 
719e126ba97SEli Cohen int mlx5_ib_query_port(struct ib_device *ibdev, u8 port,
720e126ba97SEli Cohen 		       struct ib_port_attr *props)
721e126ba97SEli Cohen {
7221b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
7231b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
7241b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_port(ibdev, port, props);
725e126ba97SEli Cohen 
7261b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
7271b5daf11SMajd Dibbiny 		return mlx5_query_hca_port(ibdev, port, props);
7281b5daf11SMajd Dibbiny 
7293f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
7303f89a643SAchiad Shochat 		return mlx5_query_port_roce(ibdev, port, props);
7313f89a643SAchiad Shochat 
7321b5daf11SMajd Dibbiny 	default:
733e126ba97SEli Cohen 		return -EINVAL;
734e126ba97SEli Cohen 	}
735e126ba97SEli Cohen }
736e126ba97SEli Cohen 
737e126ba97SEli Cohen static int mlx5_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
738e126ba97SEli Cohen 			     union ib_gid *gid)
739e126ba97SEli Cohen {
7401b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
7411b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
742e126ba97SEli Cohen 
7431b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
7441b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
7451b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_gids(ibdev, port, index, gid);
746e126ba97SEli Cohen 
7471b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
7481b5daf11SMajd Dibbiny 		return mlx5_query_hca_vport_gid(mdev, 0, port, 0, index, gid);
749e126ba97SEli Cohen 
7501b5daf11SMajd Dibbiny 	default:
7511b5daf11SMajd Dibbiny 		return -EINVAL;
7521b5daf11SMajd Dibbiny 	}
753e126ba97SEli Cohen 
754e126ba97SEli Cohen }
755e126ba97SEli Cohen 
756e126ba97SEli Cohen static int mlx5_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
757e126ba97SEli Cohen 			      u16 *pkey)
758e126ba97SEli Cohen {
7591b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
7601b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
761e126ba97SEli Cohen 
7621b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
7631b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
7641b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_pkey(ibdev, port, index, pkey);
765e126ba97SEli Cohen 
7661b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
7671b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
7681b5daf11SMajd Dibbiny 		return mlx5_query_hca_vport_pkey(mdev, 0, port,  0, index,
7691b5daf11SMajd Dibbiny 						 pkey);
7701b5daf11SMajd Dibbiny 	default:
7711b5daf11SMajd Dibbiny 		return -EINVAL;
772e126ba97SEli Cohen 	}
7731b5daf11SMajd Dibbiny }
774e126ba97SEli Cohen 
775e126ba97SEli Cohen static int mlx5_ib_modify_device(struct ib_device *ibdev, int mask,
776e126ba97SEli Cohen 				 struct ib_device_modify *props)
777e126ba97SEli Cohen {
778e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
779e126ba97SEli Cohen 	struct mlx5_reg_node_desc in;
780e126ba97SEli Cohen 	struct mlx5_reg_node_desc out;
781e126ba97SEli Cohen 	int err;
782e126ba97SEli Cohen 
783e126ba97SEli Cohen 	if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
784e126ba97SEli Cohen 		return -EOPNOTSUPP;
785e126ba97SEli Cohen 
786e126ba97SEli Cohen 	if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
787e126ba97SEli Cohen 		return 0;
788e126ba97SEli Cohen 
789e126ba97SEli Cohen 	/*
790e126ba97SEli Cohen 	 * If possible, pass node desc to FW, so it can generate
791e126ba97SEli Cohen 	 * a 144 trap.  If cmd fails, just ignore.
792e126ba97SEli Cohen 	 */
793e126ba97SEli Cohen 	memcpy(&in, props->node_desc, 64);
7949603b61dSJack Morgenstein 	err = mlx5_core_access_reg(dev->mdev, &in, sizeof(in), &out,
795e126ba97SEli Cohen 				   sizeof(out), MLX5_REG_NODE_DESC, 0, 1);
796e126ba97SEli Cohen 	if (err)
797e126ba97SEli Cohen 		return err;
798e126ba97SEli Cohen 
799e126ba97SEli Cohen 	memcpy(ibdev->node_desc, props->node_desc, 64);
800e126ba97SEli Cohen 
801e126ba97SEli Cohen 	return err;
802e126ba97SEli Cohen }
803e126ba97SEli Cohen 
804e126ba97SEli Cohen static int mlx5_ib_modify_port(struct ib_device *ibdev, u8 port, int mask,
805e126ba97SEli Cohen 			       struct ib_port_modify *props)
806e126ba97SEli Cohen {
807e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
808e126ba97SEli Cohen 	struct ib_port_attr attr;
809e126ba97SEli Cohen 	u32 tmp;
810e126ba97SEli Cohen 	int err;
811e126ba97SEli Cohen 
812e126ba97SEli Cohen 	mutex_lock(&dev->cap_mask_mutex);
813e126ba97SEli Cohen 
814e126ba97SEli Cohen 	err = mlx5_ib_query_port(ibdev, port, &attr);
815e126ba97SEli Cohen 	if (err)
816e126ba97SEli Cohen 		goto out;
817e126ba97SEli Cohen 
818e126ba97SEli Cohen 	tmp = (attr.port_cap_flags | props->set_port_cap_mask) &
819e126ba97SEli Cohen 		~props->clr_port_cap_mask;
820e126ba97SEli Cohen 
8219603b61dSJack Morgenstein 	err = mlx5_set_port_caps(dev->mdev, port, tmp);
822e126ba97SEli Cohen 
823e126ba97SEli Cohen out:
824e126ba97SEli Cohen 	mutex_unlock(&dev->cap_mask_mutex);
825e126ba97SEli Cohen 	return err;
826e126ba97SEli Cohen }
827e126ba97SEli Cohen 
828e126ba97SEli Cohen static struct ib_ucontext *mlx5_ib_alloc_ucontext(struct ib_device *ibdev,
829e126ba97SEli Cohen 						  struct ib_udata *udata)
830e126ba97SEli Cohen {
831e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
832b368d7cbSMatan Barak 	struct mlx5_ib_alloc_ucontext_req_v2 req = {};
833b368d7cbSMatan Barak 	struct mlx5_ib_alloc_ucontext_resp resp = {};
834e126ba97SEli Cohen 	struct mlx5_ib_ucontext *context;
835e126ba97SEli Cohen 	struct mlx5_uuar_info *uuari;
836e126ba97SEli Cohen 	struct mlx5_uar *uars;
837c1be5232SEli Cohen 	int gross_uuars;
838e126ba97SEli Cohen 	int num_uars;
83978c0f98cSEli Cohen 	int ver;
840e126ba97SEli Cohen 	int uuarn;
841e126ba97SEli Cohen 	int err;
842e126ba97SEli Cohen 	int i;
843f241e749SJack Morgenstein 	size_t reqlen;
844e126ba97SEli Cohen 
845e126ba97SEli Cohen 	if (!dev->ib_active)
846e126ba97SEli Cohen 		return ERR_PTR(-EAGAIN);
847e126ba97SEli Cohen 
848dfbee859SHaggai Abramovsky 	if (udata->inlen < sizeof(struct ib_uverbs_cmd_hdr))
849dfbee859SHaggai Abramovsky 		return ERR_PTR(-EINVAL);
850dfbee859SHaggai Abramovsky 
85178c0f98cSEli Cohen 	reqlen = udata->inlen - sizeof(struct ib_uverbs_cmd_hdr);
85278c0f98cSEli Cohen 	if (reqlen == sizeof(struct mlx5_ib_alloc_ucontext_req))
85378c0f98cSEli Cohen 		ver = 0;
854b368d7cbSMatan Barak 	else if (reqlen >= sizeof(struct mlx5_ib_alloc_ucontext_req_v2))
85578c0f98cSEli Cohen 		ver = 2;
85678c0f98cSEli Cohen 	else
85778c0f98cSEli Cohen 		return ERR_PTR(-EINVAL);
85878c0f98cSEli Cohen 
859b368d7cbSMatan Barak 	err = ib_copy_from_udata(&req, udata, min(reqlen, sizeof(req)));
860e126ba97SEli Cohen 	if (err)
861e126ba97SEli Cohen 		return ERR_PTR(err);
862e126ba97SEli Cohen 
863b368d7cbSMatan Barak 	if (req.flags)
86478c0f98cSEli Cohen 		return ERR_PTR(-EINVAL);
86578c0f98cSEli Cohen 
866e126ba97SEli Cohen 	if (req.total_num_uuars > MLX5_MAX_UUARS)
867e126ba97SEli Cohen 		return ERR_PTR(-ENOMEM);
868e126ba97SEli Cohen 
869e126ba97SEli Cohen 	if (req.total_num_uuars == 0)
870e126ba97SEli Cohen 		return ERR_PTR(-EINVAL);
871e126ba97SEli Cohen 
872f72300c5SHaggai Abramovsky 	if (req.comp_mask || req.reserved0 || req.reserved1 || req.reserved2)
873b368d7cbSMatan Barak 		return ERR_PTR(-EOPNOTSUPP);
874b368d7cbSMatan Barak 
875b368d7cbSMatan Barak 	if (reqlen > sizeof(req) &&
876b368d7cbSMatan Barak 	    !ib_is_udata_cleared(udata, sizeof(req),
877dfbee859SHaggai Abramovsky 				 reqlen - sizeof(req)))
878b368d7cbSMatan Barak 		return ERR_PTR(-EOPNOTSUPP);
879b368d7cbSMatan Barak 
880c1be5232SEli Cohen 	req.total_num_uuars = ALIGN(req.total_num_uuars,
881c1be5232SEli Cohen 				    MLX5_NON_FP_BF_REGS_PER_PAGE);
882e126ba97SEli Cohen 	if (req.num_low_latency_uuars > req.total_num_uuars - 1)
883e126ba97SEli Cohen 		return ERR_PTR(-EINVAL);
884e126ba97SEli Cohen 
885c1be5232SEli Cohen 	num_uars = req.total_num_uuars / MLX5_NON_FP_BF_REGS_PER_PAGE;
886c1be5232SEli Cohen 	gross_uuars = num_uars * MLX5_BF_REGS_PER_PAGE;
887938fe83cSSaeed Mahameed 	resp.qp_tab_size = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp);
888938fe83cSSaeed Mahameed 	resp.bf_reg_size = 1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size);
889e126ba97SEli Cohen 	resp.cache_line_size = L1_CACHE_BYTES;
890938fe83cSSaeed Mahameed 	resp.max_sq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq);
891938fe83cSSaeed Mahameed 	resp.max_rq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq);
892938fe83cSSaeed Mahameed 	resp.max_send_wqebb = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
893938fe83cSSaeed Mahameed 	resp.max_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
894938fe83cSSaeed Mahameed 	resp.max_srq_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz);
895f72300c5SHaggai Abramovsky 	resp.cqe_version = min_t(__u8,
896f72300c5SHaggai Abramovsky 				 (__u8)MLX5_CAP_GEN(dev->mdev, cqe_version),
897f72300c5SHaggai Abramovsky 				 req.max_cqe_version);
898b368d7cbSMatan Barak 	resp.response_length = min(offsetof(typeof(resp), response_length) +
899b368d7cbSMatan Barak 				   sizeof(resp.response_length), udata->outlen);
900e126ba97SEli Cohen 
901e126ba97SEli Cohen 	context = kzalloc(sizeof(*context), GFP_KERNEL);
902e126ba97SEli Cohen 	if (!context)
903e126ba97SEli Cohen 		return ERR_PTR(-ENOMEM);
904e126ba97SEli Cohen 
905e126ba97SEli Cohen 	uuari = &context->uuari;
906e126ba97SEli Cohen 	mutex_init(&uuari->lock);
907e126ba97SEli Cohen 	uars = kcalloc(num_uars, sizeof(*uars), GFP_KERNEL);
908e126ba97SEli Cohen 	if (!uars) {
909e126ba97SEli Cohen 		err = -ENOMEM;
910e126ba97SEli Cohen 		goto out_ctx;
911e126ba97SEli Cohen 	}
912e126ba97SEli Cohen 
913c1be5232SEli Cohen 	uuari->bitmap = kcalloc(BITS_TO_LONGS(gross_uuars),
914e126ba97SEli Cohen 				sizeof(*uuari->bitmap),
915e126ba97SEli Cohen 				GFP_KERNEL);
916e126ba97SEli Cohen 	if (!uuari->bitmap) {
917e126ba97SEli Cohen 		err = -ENOMEM;
918e126ba97SEli Cohen 		goto out_uar_ctx;
919e126ba97SEli Cohen 	}
920e126ba97SEli Cohen 	/*
921e126ba97SEli Cohen 	 * clear all fast path uuars
922e126ba97SEli Cohen 	 */
923c1be5232SEli Cohen 	for (i = 0; i < gross_uuars; i++) {
924e126ba97SEli Cohen 		uuarn = i & 3;
925e126ba97SEli Cohen 		if (uuarn == 2 || uuarn == 3)
926e126ba97SEli Cohen 			set_bit(i, uuari->bitmap);
927e126ba97SEli Cohen 	}
928e126ba97SEli Cohen 
929c1be5232SEli Cohen 	uuari->count = kcalloc(gross_uuars, sizeof(*uuari->count), GFP_KERNEL);
930e126ba97SEli Cohen 	if (!uuari->count) {
931e126ba97SEli Cohen 		err = -ENOMEM;
932e126ba97SEli Cohen 		goto out_bitmap;
933e126ba97SEli Cohen 	}
934e126ba97SEli Cohen 
935e126ba97SEli Cohen 	for (i = 0; i < num_uars; i++) {
9369603b61dSJack Morgenstein 		err = mlx5_cmd_alloc_uar(dev->mdev, &uars[i].index);
937e126ba97SEli Cohen 		if (err)
938e126ba97SEli Cohen 			goto out_count;
939e126ba97SEli Cohen 	}
940e126ba97SEli Cohen 
941b4cfe447SHaggai Eran #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
942b4cfe447SHaggai Eran 	context->ibucontext.invalidate_range = &mlx5_ib_invalidate_range;
943b4cfe447SHaggai Eran #endif
944b4cfe447SHaggai Eran 
945*146d2f1aSmajd@mellanox.com 	if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain)) {
946*146d2f1aSmajd@mellanox.com 		err = mlx5_core_alloc_transport_domain(dev->mdev,
947*146d2f1aSmajd@mellanox.com 						       &context->tdn);
948*146d2f1aSmajd@mellanox.com 		if (err)
949*146d2f1aSmajd@mellanox.com 			goto out_uars;
950*146d2f1aSmajd@mellanox.com 	}
951*146d2f1aSmajd@mellanox.com 
952e126ba97SEli Cohen 	INIT_LIST_HEAD(&context->db_page_list);
953e126ba97SEli Cohen 	mutex_init(&context->db_page_mutex);
954e126ba97SEli Cohen 
955e126ba97SEli Cohen 	resp.tot_uuars = req.total_num_uuars;
956938fe83cSSaeed Mahameed 	resp.num_ports = MLX5_CAP_GEN(dev->mdev, num_ports);
957b368d7cbSMatan Barak 
958f72300c5SHaggai Abramovsky 	if (field_avail(typeof(resp), cqe_version, udata->outlen))
959f72300c5SHaggai Abramovsky 		resp.response_length += sizeof(resp.cqe_version);
960b368d7cbSMatan Barak 
961b368d7cbSMatan Barak 	if (field_avail(typeof(resp), hca_core_clock_offset, udata->outlen)) {
962b368d7cbSMatan Barak 		resp.comp_mask |=
963b368d7cbSMatan Barak 			MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_CORE_CLOCK_OFFSET;
964b368d7cbSMatan Barak 		resp.hca_core_clock_offset =
965b368d7cbSMatan Barak 			offsetof(struct mlx5_init_seg, internal_timer_h) %
966b368d7cbSMatan Barak 			PAGE_SIZE;
967f72300c5SHaggai Abramovsky 		resp.response_length += sizeof(resp.hca_core_clock_offset) +
968f72300c5SHaggai Abramovsky 					sizeof(resp.reserved2) +
969f72300c5SHaggai Abramovsky 					sizeof(resp.reserved3);
970b368d7cbSMatan Barak 	}
971b368d7cbSMatan Barak 
972b368d7cbSMatan Barak 	err = ib_copy_to_udata(udata, &resp, resp.response_length);
973e126ba97SEli Cohen 	if (err)
974*146d2f1aSmajd@mellanox.com 		goto out_td;
975e126ba97SEli Cohen 
97678c0f98cSEli Cohen 	uuari->ver = ver;
977e126ba97SEli Cohen 	uuari->num_low_latency_uuars = req.num_low_latency_uuars;
978e126ba97SEli Cohen 	uuari->uars = uars;
979e126ba97SEli Cohen 	uuari->num_uars = num_uars;
980f72300c5SHaggai Abramovsky 	context->cqe_version = resp.cqe_version;
981f72300c5SHaggai Abramovsky 
982e126ba97SEli Cohen 	return &context->ibucontext;
983e126ba97SEli Cohen 
984*146d2f1aSmajd@mellanox.com out_td:
985*146d2f1aSmajd@mellanox.com 	if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
986*146d2f1aSmajd@mellanox.com 		mlx5_core_dealloc_transport_domain(dev->mdev, context->tdn);
987*146d2f1aSmajd@mellanox.com 
988e126ba97SEli Cohen out_uars:
989e126ba97SEli Cohen 	for (i--; i >= 0; i--)
9909603b61dSJack Morgenstein 		mlx5_cmd_free_uar(dev->mdev, uars[i].index);
991e126ba97SEli Cohen out_count:
992e126ba97SEli Cohen 	kfree(uuari->count);
993e126ba97SEli Cohen 
994e126ba97SEli Cohen out_bitmap:
995e126ba97SEli Cohen 	kfree(uuari->bitmap);
996e126ba97SEli Cohen 
997e126ba97SEli Cohen out_uar_ctx:
998e126ba97SEli Cohen 	kfree(uars);
999e126ba97SEli Cohen 
1000e126ba97SEli Cohen out_ctx:
1001e126ba97SEli Cohen 	kfree(context);
1002e126ba97SEli Cohen 	return ERR_PTR(err);
1003e126ba97SEli Cohen }
1004e126ba97SEli Cohen 
1005e126ba97SEli Cohen static int mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
1006e126ba97SEli Cohen {
1007e126ba97SEli Cohen 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1008e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
1009e126ba97SEli Cohen 	struct mlx5_uuar_info *uuari = &context->uuari;
1010e126ba97SEli Cohen 	int i;
1011e126ba97SEli Cohen 
1012*146d2f1aSmajd@mellanox.com 	if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1013*146d2f1aSmajd@mellanox.com 		mlx5_core_dealloc_transport_domain(dev->mdev, context->tdn);
1014*146d2f1aSmajd@mellanox.com 
1015e126ba97SEli Cohen 	for (i = 0; i < uuari->num_uars; i++) {
10169603b61dSJack Morgenstein 		if (mlx5_cmd_free_uar(dev->mdev, uuari->uars[i].index))
1017e126ba97SEli Cohen 			mlx5_ib_warn(dev, "failed to free UAR 0x%x\n", uuari->uars[i].index);
1018e126ba97SEli Cohen 	}
1019e126ba97SEli Cohen 
1020e126ba97SEli Cohen 	kfree(uuari->count);
1021e126ba97SEli Cohen 	kfree(uuari->bitmap);
1022e126ba97SEli Cohen 	kfree(uuari->uars);
1023e126ba97SEli Cohen 	kfree(context);
1024e126ba97SEli Cohen 
1025e126ba97SEli Cohen 	return 0;
1026e126ba97SEli Cohen }
1027e126ba97SEli Cohen 
1028e126ba97SEli Cohen static phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev, int index)
1029e126ba97SEli Cohen {
10309603b61dSJack Morgenstein 	return (pci_resource_start(dev->mdev->pdev, 0) >> PAGE_SHIFT) + index;
1031e126ba97SEli Cohen }
1032e126ba97SEli Cohen 
1033e126ba97SEli Cohen static int get_command(unsigned long offset)
1034e126ba97SEli Cohen {
1035e126ba97SEli Cohen 	return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK;
1036e126ba97SEli Cohen }
1037e126ba97SEli Cohen 
1038e126ba97SEli Cohen static int get_arg(unsigned long offset)
1039e126ba97SEli Cohen {
1040e126ba97SEli Cohen 	return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1);
1041e126ba97SEli Cohen }
1042e126ba97SEli Cohen 
1043e126ba97SEli Cohen static int get_index(unsigned long offset)
1044e126ba97SEli Cohen {
1045e126ba97SEli Cohen 	return get_arg(offset);
1046e126ba97SEli Cohen }
1047e126ba97SEli Cohen 
1048e126ba97SEli Cohen static int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma)
1049e126ba97SEli Cohen {
1050e126ba97SEli Cohen 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1051e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
1052e126ba97SEli Cohen 	struct mlx5_uuar_info *uuari = &context->uuari;
1053e126ba97SEli Cohen 	unsigned long command;
1054e126ba97SEli Cohen 	unsigned long idx;
1055e126ba97SEli Cohen 	phys_addr_t pfn;
1056e126ba97SEli Cohen 
1057e126ba97SEli Cohen 	command = get_command(vma->vm_pgoff);
1058e126ba97SEli Cohen 	switch (command) {
1059e126ba97SEli Cohen 	case MLX5_IB_MMAP_REGULAR_PAGE:
1060e126ba97SEli Cohen 		if (vma->vm_end - vma->vm_start != PAGE_SIZE)
1061e126ba97SEli Cohen 			return -EINVAL;
1062e126ba97SEli Cohen 
1063e126ba97SEli Cohen 		idx = get_index(vma->vm_pgoff);
10641c3ce90dSEli Cohen 		if (idx >= uuari->num_uars)
10651c3ce90dSEli Cohen 			return -EINVAL;
10661c3ce90dSEli Cohen 
1067e126ba97SEli Cohen 		pfn = uar_index2pfn(dev, uuari->uars[idx].index);
1068e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn 0x%llx\n", idx,
1069e126ba97SEli Cohen 			    (unsigned long long)pfn);
1070e126ba97SEli Cohen 
1071e126ba97SEli Cohen 		vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
1072e126ba97SEli Cohen 		if (io_remap_pfn_range(vma, vma->vm_start, pfn,
1073e126ba97SEli Cohen 				       PAGE_SIZE, vma->vm_page_prot))
1074e126ba97SEli Cohen 			return -EAGAIN;
1075e126ba97SEli Cohen 
1076e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "mapped WC at 0x%lx, PA 0x%llx\n",
1077e126ba97SEli Cohen 			    vma->vm_start,
1078e126ba97SEli Cohen 			    (unsigned long long)pfn << PAGE_SHIFT);
1079e126ba97SEli Cohen 		break;
1080e126ba97SEli Cohen 
1081e126ba97SEli Cohen 	case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES:
1082e126ba97SEli Cohen 		return -ENOSYS;
1083e126ba97SEli Cohen 
1084d69e3bcfSMatan Barak 	case MLX5_IB_MMAP_CORE_CLOCK:
1085d69e3bcfSMatan Barak 		if (vma->vm_end - vma->vm_start != PAGE_SIZE)
1086d69e3bcfSMatan Barak 			return -EINVAL;
1087d69e3bcfSMatan Barak 
1088d69e3bcfSMatan Barak 		if (vma->vm_flags & (VM_WRITE | VM_EXEC))
1089d69e3bcfSMatan Barak 			return -EPERM;
1090d69e3bcfSMatan Barak 
1091d69e3bcfSMatan Barak 		/* Don't expose to user-space information it shouldn't have */
1092d69e3bcfSMatan Barak 		if (PAGE_SIZE > 4096)
1093d69e3bcfSMatan Barak 			return -EOPNOTSUPP;
1094d69e3bcfSMatan Barak 
1095d69e3bcfSMatan Barak 		vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1096d69e3bcfSMatan Barak 		pfn = (dev->mdev->iseg_base +
1097d69e3bcfSMatan Barak 		       offsetof(struct mlx5_init_seg, internal_timer_h)) >>
1098d69e3bcfSMatan Barak 			PAGE_SHIFT;
1099d69e3bcfSMatan Barak 		if (io_remap_pfn_range(vma, vma->vm_start, pfn,
1100d69e3bcfSMatan Barak 				       PAGE_SIZE, vma->vm_page_prot))
1101d69e3bcfSMatan Barak 			return -EAGAIN;
1102d69e3bcfSMatan Barak 
1103d69e3bcfSMatan Barak 		mlx5_ib_dbg(dev, "mapped internal timer at 0x%lx, PA 0x%llx\n",
1104d69e3bcfSMatan Barak 			    vma->vm_start,
1105d69e3bcfSMatan Barak 			    (unsigned long long)pfn << PAGE_SHIFT);
1106d69e3bcfSMatan Barak 		break;
1107d69e3bcfSMatan Barak 
1108e126ba97SEli Cohen 	default:
1109e126ba97SEli Cohen 		return -EINVAL;
1110e126ba97SEli Cohen 	}
1111e126ba97SEli Cohen 
1112e126ba97SEli Cohen 	return 0;
1113e126ba97SEli Cohen }
1114e126ba97SEli Cohen 
1115e126ba97SEli Cohen static struct ib_pd *mlx5_ib_alloc_pd(struct ib_device *ibdev,
1116e126ba97SEli Cohen 				      struct ib_ucontext *context,
1117e126ba97SEli Cohen 				      struct ib_udata *udata)
1118e126ba97SEli Cohen {
1119e126ba97SEli Cohen 	struct mlx5_ib_alloc_pd_resp resp;
1120e126ba97SEli Cohen 	struct mlx5_ib_pd *pd;
1121e126ba97SEli Cohen 	int err;
1122e126ba97SEli Cohen 
1123e126ba97SEli Cohen 	pd = kmalloc(sizeof(*pd), GFP_KERNEL);
1124e126ba97SEli Cohen 	if (!pd)
1125e126ba97SEli Cohen 		return ERR_PTR(-ENOMEM);
1126e126ba97SEli Cohen 
11279603b61dSJack Morgenstein 	err = mlx5_core_alloc_pd(to_mdev(ibdev)->mdev, &pd->pdn);
1128e126ba97SEli Cohen 	if (err) {
1129e126ba97SEli Cohen 		kfree(pd);
1130e126ba97SEli Cohen 		return ERR_PTR(err);
1131e126ba97SEli Cohen 	}
1132e126ba97SEli Cohen 
1133e126ba97SEli Cohen 	if (context) {
1134e126ba97SEli Cohen 		resp.pdn = pd->pdn;
1135e126ba97SEli Cohen 		if (ib_copy_to_udata(udata, &resp, sizeof(resp))) {
11369603b61dSJack Morgenstein 			mlx5_core_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn);
1137e126ba97SEli Cohen 			kfree(pd);
1138e126ba97SEli Cohen 			return ERR_PTR(-EFAULT);
1139e126ba97SEli Cohen 		}
1140e126ba97SEli Cohen 	}
1141e126ba97SEli Cohen 
1142e126ba97SEli Cohen 	return &pd->ibpd;
1143e126ba97SEli Cohen }
1144e126ba97SEli Cohen 
1145e126ba97SEli Cohen static int mlx5_ib_dealloc_pd(struct ib_pd *pd)
1146e126ba97SEli Cohen {
1147e126ba97SEli Cohen 	struct mlx5_ib_dev *mdev = to_mdev(pd->device);
1148e126ba97SEli Cohen 	struct mlx5_ib_pd *mpd = to_mpd(pd);
1149e126ba97SEli Cohen 
11509603b61dSJack Morgenstein 	mlx5_core_dealloc_pd(mdev->mdev, mpd->pdn);
1151e126ba97SEli Cohen 	kfree(mpd);
1152e126ba97SEli Cohen 
1153e126ba97SEli Cohen 	return 0;
1154e126ba97SEli Cohen }
1155e126ba97SEli Cohen 
1156e126ba97SEli Cohen static int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
1157e126ba97SEli Cohen {
1158e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
1159e126ba97SEli Cohen 	int err;
1160e126ba97SEli Cohen 
11619603b61dSJack Morgenstein 	err = mlx5_core_attach_mcg(dev->mdev, gid, ibqp->qp_num);
1162e126ba97SEli Cohen 	if (err)
1163e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n",
1164e126ba97SEli Cohen 			     ibqp->qp_num, gid->raw);
1165e126ba97SEli Cohen 
1166e126ba97SEli Cohen 	return err;
1167e126ba97SEli Cohen }
1168e126ba97SEli Cohen 
1169e126ba97SEli Cohen static int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
1170e126ba97SEli Cohen {
1171e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
1172e126ba97SEli Cohen 	int err;
1173e126ba97SEli Cohen 
11749603b61dSJack Morgenstein 	err = mlx5_core_detach_mcg(dev->mdev, gid, ibqp->qp_num);
1175e126ba97SEli Cohen 	if (err)
1176e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n",
1177e126ba97SEli Cohen 			     ibqp->qp_num, gid->raw);
1178e126ba97SEli Cohen 
1179e126ba97SEli Cohen 	return err;
1180e126ba97SEli Cohen }
1181e126ba97SEli Cohen 
1182e126ba97SEli Cohen static int init_node_data(struct mlx5_ib_dev *dev)
1183e126ba97SEli Cohen {
11841b5daf11SMajd Dibbiny 	int err;
1185e126ba97SEli Cohen 
11861b5daf11SMajd Dibbiny 	err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc);
1187e126ba97SEli Cohen 	if (err)
1188e126ba97SEli Cohen 		return err;
11891b5daf11SMajd Dibbiny 
11901b5daf11SMajd Dibbiny 	dev->mdev->rev_id = dev->mdev->pdev->revision;
11911b5daf11SMajd Dibbiny 
11921b5daf11SMajd Dibbiny 	return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid);
1193e126ba97SEli Cohen }
1194e126ba97SEli Cohen 
1195e126ba97SEli Cohen static ssize_t show_fw_pages(struct device *device, struct device_attribute *attr,
1196e126ba97SEli Cohen 			     char *buf)
1197e126ba97SEli Cohen {
1198e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
1199e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
1200e126ba97SEli Cohen 
12019603b61dSJack Morgenstein 	return sprintf(buf, "%d\n", dev->mdev->priv.fw_pages);
1202e126ba97SEli Cohen }
1203e126ba97SEli Cohen 
1204e126ba97SEli Cohen static ssize_t show_reg_pages(struct device *device,
1205e126ba97SEli Cohen 			      struct device_attribute *attr, char *buf)
1206e126ba97SEli Cohen {
1207e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
1208e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
1209e126ba97SEli Cohen 
12106aec21f6SHaggai Eran 	return sprintf(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages));
1211e126ba97SEli Cohen }
1212e126ba97SEli Cohen 
1213e126ba97SEli Cohen static ssize_t show_hca(struct device *device, struct device_attribute *attr,
1214e126ba97SEli Cohen 			char *buf)
1215e126ba97SEli Cohen {
1216e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
1217e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
12189603b61dSJack Morgenstein 	return sprintf(buf, "MT%d\n", dev->mdev->pdev->device);
1219e126ba97SEli Cohen }
1220e126ba97SEli Cohen 
1221e126ba97SEli Cohen static ssize_t show_fw_ver(struct device *device, struct device_attribute *attr,
1222e126ba97SEli Cohen 			   char *buf)
1223e126ba97SEli Cohen {
1224e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
1225e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
12269603b61dSJack Morgenstein 	return sprintf(buf, "%d.%d.%d\n", fw_rev_maj(dev->mdev),
12279603b61dSJack Morgenstein 		       fw_rev_min(dev->mdev), fw_rev_sub(dev->mdev));
1228e126ba97SEli Cohen }
1229e126ba97SEli Cohen 
1230e126ba97SEli Cohen static ssize_t show_rev(struct device *device, struct device_attribute *attr,
1231e126ba97SEli Cohen 			char *buf)
1232e126ba97SEli Cohen {
1233e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
1234e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
12359603b61dSJack Morgenstein 	return sprintf(buf, "%x\n", dev->mdev->rev_id);
1236e126ba97SEli Cohen }
1237e126ba97SEli Cohen 
1238e126ba97SEli Cohen static ssize_t show_board(struct device *device, struct device_attribute *attr,
1239e126ba97SEli Cohen 			  char *buf)
1240e126ba97SEli Cohen {
1241e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
1242e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
1243e126ba97SEli Cohen 	return sprintf(buf, "%.*s\n", MLX5_BOARD_ID_LEN,
12449603b61dSJack Morgenstein 		       dev->mdev->board_id);
1245e126ba97SEli Cohen }
1246e126ba97SEli Cohen 
1247e126ba97SEli Cohen static DEVICE_ATTR(hw_rev,   S_IRUGO, show_rev,    NULL);
1248e126ba97SEli Cohen static DEVICE_ATTR(fw_ver,   S_IRUGO, show_fw_ver, NULL);
1249e126ba97SEli Cohen static DEVICE_ATTR(hca_type, S_IRUGO, show_hca,    NULL);
1250e126ba97SEli Cohen static DEVICE_ATTR(board_id, S_IRUGO, show_board,  NULL);
1251e126ba97SEli Cohen static DEVICE_ATTR(fw_pages, S_IRUGO, show_fw_pages, NULL);
1252e126ba97SEli Cohen static DEVICE_ATTR(reg_pages, S_IRUGO, show_reg_pages, NULL);
1253e126ba97SEli Cohen 
1254e126ba97SEli Cohen static struct device_attribute *mlx5_class_attributes[] = {
1255e126ba97SEli Cohen 	&dev_attr_hw_rev,
1256e126ba97SEli Cohen 	&dev_attr_fw_ver,
1257e126ba97SEli Cohen 	&dev_attr_hca_type,
1258e126ba97SEli Cohen 	&dev_attr_board_id,
1259e126ba97SEli Cohen 	&dev_attr_fw_pages,
1260e126ba97SEli Cohen 	&dev_attr_reg_pages,
1261e126ba97SEli Cohen };
1262e126ba97SEli Cohen 
12639603b61dSJack Morgenstein static void mlx5_ib_event(struct mlx5_core_dev *dev, void *context,
12644d2f9bbbSJack Morgenstein 			  enum mlx5_dev_event event, unsigned long param)
1265e126ba97SEli Cohen {
12669603b61dSJack Morgenstein 	struct mlx5_ib_dev *ibdev = (struct mlx5_ib_dev *)context;
1267e126ba97SEli Cohen 	struct ib_event ibev;
12689603b61dSJack Morgenstein 
1269e126ba97SEli Cohen 	u8 port = 0;
1270e126ba97SEli Cohen 
1271e126ba97SEli Cohen 	switch (event) {
1272e126ba97SEli Cohen 	case MLX5_DEV_EVENT_SYS_ERROR:
1273e126ba97SEli Cohen 		ibdev->ib_active = false;
1274e126ba97SEli Cohen 		ibev.event = IB_EVENT_DEVICE_FATAL;
1275e126ba97SEli Cohen 		break;
1276e126ba97SEli Cohen 
1277e126ba97SEli Cohen 	case MLX5_DEV_EVENT_PORT_UP:
1278e126ba97SEli Cohen 		ibev.event = IB_EVENT_PORT_ACTIVE;
12794d2f9bbbSJack Morgenstein 		port = (u8)param;
1280e126ba97SEli Cohen 		break;
1281e126ba97SEli Cohen 
1282e126ba97SEli Cohen 	case MLX5_DEV_EVENT_PORT_DOWN:
1283e126ba97SEli Cohen 		ibev.event = IB_EVENT_PORT_ERR;
12844d2f9bbbSJack Morgenstein 		port = (u8)param;
1285e126ba97SEli Cohen 		break;
1286e126ba97SEli Cohen 
1287e126ba97SEli Cohen 	case MLX5_DEV_EVENT_PORT_INITIALIZED:
1288e126ba97SEli Cohen 		/* not used by ULPs */
1289e126ba97SEli Cohen 		return;
1290e126ba97SEli Cohen 
1291e126ba97SEli Cohen 	case MLX5_DEV_EVENT_LID_CHANGE:
1292e126ba97SEli Cohen 		ibev.event = IB_EVENT_LID_CHANGE;
12934d2f9bbbSJack Morgenstein 		port = (u8)param;
1294e126ba97SEli Cohen 		break;
1295e126ba97SEli Cohen 
1296e126ba97SEli Cohen 	case MLX5_DEV_EVENT_PKEY_CHANGE:
1297e126ba97SEli Cohen 		ibev.event = IB_EVENT_PKEY_CHANGE;
12984d2f9bbbSJack Morgenstein 		port = (u8)param;
1299e126ba97SEli Cohen 		break;
1300e126ba97SEli Cohen 
1301e126ba97SEli Cohen 	case MLX5_DEV_EVENT_GUID_CHANGE:
1302e126ba97SEli Cohen 		ibev.event = IB_EVENT_GID_CHANGE;
13034d2f9bbbSJack Morgenstein 		port = (u8)param;
1304e126ba97SEli Cohen 		break;
1305e126ba97SEli Cohen 
1306e126ba97SEli Cohen 	case MLX5_DEV_EVENT_CLIENT_REREG:
1307e126ba97SEli Cohen 		ibev.event = IB_EVENT_CLIENT_REREGISTER;
13084d2f9bbbSJack Morgenstein 		port = (u8)param;
1309e126ba97SEli Cohen 		break;
1310e126ba97SEli Cohen 	}
1311e126ba97SEli Cohen 
1312e126ba97SEli Cohen 	ibev.device	      = &ibdev->ib_dev;
1313e126ba97SEli Cohen 	ibev.element.port_num = port;
1314e126ba97SEli Cohen 
1315a0c84c32SEli Cohen 	if (port < 1 || port > ibdev->num_ports) {
1316a0c84c32SEli Cohen 		mlx5_ib_warn(ibdev, "warning: event on port %d\n", port);
1317a0c84c32SEli Cohen 		return;
1318a0c84c32SEli Cohen 	}
1319a0c84c32SEli Cohen 
1320e126ba97SEli Cohen 	if (ibdev->ib_active)
1321e126ba97SEli Cohen 		ib_dispatch_event(&ibev);
1322e126ba97SEli Cohen }
1323e126ba97SEli Cohen 
1324e126ba97SEli Cohen static void get_ext_port_caps(struct mlx5_ib_dev *dev)
1325e126ba97SEli Cohen {
1326e126ba97SEli Cohen 	int port;
1327e126ba97SEli Cohen 
1328938fe83cSSaeed Mahameed 	for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++)
1329e126ba97SEli Cohen 		mlx5_query_ext_port_caps(dev, port);
1330e126ba97SEli Cohen }
1331e126ba97SEli Cohen 
1332e126ba97SEli Cohen static int get_port_caps(struct mlx5_ib_dev *dev)
1333e126ba97SEli Cohen {
1334e126ba97SEli Cohen 	struct ib_device_attr *dprops = NULL;
1335e126ba97SEli Cohen 	struct ib_port_attr *pprops = NULL;
1336f614fc15SDan Carpenter 	int err = -ENOMEM;
1337e126ba97SEli Cohen 	int port;
13382528e33eSMatan Barak 	struct ib_udata uhw = {.inlen = 0, .outlen = 0};
1339e126ba97SEli Cohen 
1340e126ba97SEli Cohen 	pprops = kmalloc(sizeof(*pprops), GFP_KERNEL);
1341e126ba97SEli Cohen 	if (!pprops)
1342e126ba97SEli Cohen 		goto out;
1343e126ba97SEli Cohen 
1344e126ba97SEli Cohen 	dprops = kmalloc(sizeof(*dprops), GFP_KERNEL);
1345e126ba97SEli Cohen 	if (!dprops)
1346e126ba97SEli Cohen 		goto out;
1347e126ba97SEli Cohen 
13482528e33eSMatan Barak 	err = mlx5_ib_query_device(&dev->ib_dev, dprops, &uhw);
1349e126ba97SEli Cohen 	if (err) {
1350e126ba97SEli Cohen 		mlx5_ib_warn(dev, "query_device failed %d\n", err);
1351e126ba97SEli Cohen 		goto out;
1352e126ba97SEli Cohen 	}
1353e126ba97SEli Cohen 
1354938fe83cSSaeed Mahameed 	for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++) {
1355e126ba97SEli Cohen 		err = mlx5_ib_query_port(&dev->ib_dev, port, pprops);
1356e126ba97SEli Cohen 		if (err) {
1357938fe83cSSaeed Mahameed 			mlx5_ib_warn(dev, "query_port %d failed %d\n",
1358938fe83cSSaeed Mahameed 				     port, err);
1359e126ba97SEli Cohen 			break;
1360e126ba97SEli Cohen 		}
1361938fe83cSSaeed Mahameed 		dev->mdev->port_caps[port - 1].pkey_table_len =
1362938fe83cSSaeed Mahameed 						dprops->max_pkeys;
1363938fe83cSSaeed Mahameed 		dev->mdev->port_caps[port - 1].gid_table_len =
1364938fe83cSSaeed Mahameed 						pprops->gid_tbl_len;
1365e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "pkey_table_len %d, gid_table_len %d\n",
1366e126ba97SEli Cohen 			    dprops->max_pkeys, pprops->gid_tbl_len);
1367e126ba97SEli Cohen 	}
1368e126ba97SEli Cohen 
1369e126ba97SEli Cohen out:
1370e126ba97SEli Cohen 	kfree(pprops);
1371e126ba97SEli Cohen 	kfree(dprops);
1372e126ba97SEli Cohen 
1373e126ba97SEli Cohen 	return err;
1374e126ba97SEli Cohen }
1375e126ba97SEli Cohen 
1376e126ba97SEli Cohen static void destroy_umrc_res(struct mlx5_ib_dev *dev)
1377e126ba97SEli Cohen {
1378e126ba97SEli Cohen 	int err;
1379e126ba97SEli Cohen 
1380e126ba97SEli Cohen 	err = mlx5_mr_cache_cleanup(dev);
1381e126ba97SEli Cohen 	if (err)
1382e126ba97SEli Cohen 		mlx5_ib_warn(dev, "mr cache cleanup failed\n");
1383e126ba97SEli Cohen 
1384e126ba97SEli Cohen 	mlx5_ib_destroy_qp(dev->umrc.qp);
1385e126ba97SEli Cohen 	ib_destroy_cq(dev->umrc.cq);
1386e126ba97SEli Cohen 	ib_dealloc_pd(dev->umrc.pd);
1387e126ba97SEli Cohen }
1388e126ba97SEli Cohen 
1389e126ba97SEli Cohen enum {
1390e126ba97SEli Cohen 	MAX_UMR_WR = 128,
1391e126ba97SEli Cohen };
1392e126ba97SEli Cohen 
1393e126ba97SEli Cohen static int create_umr_res(struct mlx5_ib_dev *dev)
1394e126ba97SEli Cohen {
1395e126ba97SEli Cohen 	struct ib_qp_init_attr *init_attr = NULL;
1396e126ba97SEli Cohen 	struct ib_qp_attr *attr = NULL;
1397e126ba97SEli Cohen 	struct ib_pd *pd;
1398e126ba97SEli Cohen 	struct ib_cq *cq;
1399e126ba97SEli Cohen 	struct ib_qp *qp;
14008e37210bSMatan Barak 	struct ib_cq_init_attr cq_attr = {};
1401e126ba97SEli Cohen 	int ret;
1402e126ba97SEli Cohen 
1403e126ba97SEli Cohen 	attr = kzalloc(sizeof(*attr), GFP_KERNEL);
1404e126ba97SEli Cohen 	init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL);
1405e126ba97SEli Cohen 	if (!attr || !init_attr) {
1406e126ba97SEli Cohen 		ret = -ENOMEM;
1407e126ba97SEli Cohen 		goto error_0;
1408e126ba97SEli Cohen 	}
1409e126ba97SEli Cohen 
1410e126ba97SEli Cohen 	pd = ib_alloc_pd(&dev->ib_dev);
1411e126ba97SEli Cohen 	if (IS_ERR(pd)) {
1412e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create PD for sync UMR QP\n");
1413e126ba97SEli Cohen 		ret = PTR_ERR(pd);
1414e126ba97SEli Cohen 		goto error_0;
1415e126ba97SEli Cohen 	}
1416e126ba97SEli Cohen 
14178e37210bSMatan Barak 	cq_attr.cqe = 128;
14188e37210bSMatan Barak 	cq = ib_create_cq(&dev->ib_dev, mlx5_umr_cq_handler, NULL, NULL,
14198e37210bSMatan Barak 			  &cq_attr);
1420e126ba97SEli Cohen 	if (IS_ERR(cq)) {
1421e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create CQ for sync UMR QP\n");
1422e126ba97SEli Cohen 		ret = PTR_ERR(cq);
1423e126ba97SEli Cohen 		goto error_2;
1424e126ba97SEli Cohen 	}
1425e126ba97SEli Cohen 	ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
1426e126ba97SEli Cohen 
1427e126ba97SEli Cohen 	init_attr->send_cq = cq;
1428e126ba97SEli Cohen 	init_attr->recv_cq = cq;
1429e126ba97SEli Cohen 	init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
1430e126ba97SEli Cohen 	init_attr->cap.max_send_wr = MAX_UMR_WR;
1431e126ba97SEli Cohen 	init_attr->cap.max_send_sge = 1;
1432e126ba97SEli Cohen 	init_attr->qp_type = MLX5_IB_QPT_REG_UMR;
1433e126ba97SEli Cohen 	init_attr->port_num = 1;
1434e126ba97SEli Cohen 	qp = mlx5_ib_create_qp(pd, init_attr, NULL);
1435e126ba97SEli Cohen 	if (IS_ERR(qp)) {
1436e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create sync UMR QP\n");
1437e126ba97SEli Cohen 		ret = PTR_ERR(qp);
1438e126ba97SEli Cohen 		goto error_3;
1439e126ba97SEli Cohen 	}
1440e126ba97SEli Cohen 	qp->device     = &dev->ib_dev;
1441e126ba97SEli Cohen 	qp->real_qp    = qp;
1442e126ba97SEli Cohen 	qp->uobject    = NULL;
1443e126ba97SEli Cohen 	qp->qp_type    = MLX5_IB_QPT_REG_UMR;
1444e126ba97SEli Cohen 
1445e126ba97SEli Cohen 	attr->qp_state = IB_QPS_INIT;
1446e126ba97SEli Cohen 	attr->port_num = 1;
1447e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_PKEY_INDEX |
1448e126ba97SEli Cohen 				IB_QP_PORT, NULL);
1449e126ba97SEli Cohen 	if (ret) {
1450e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify UMR QP\n");
1451e126ba97SEli Cohen 		goto error_4;
1452e126ba97SEli Cohen 	}
1453e126ba97SEli Cohen 
1454e126ba97SEli Cohen 	memset(attr, 0, sizeof(*attr));
1455e126ba97SEli Cohen 	attr->qp_state = IB_QPS_RTR;
1456e126ba97SEli Cohen 	attr->path_mtu = IB_MTU_256;
1457e126ba97SEli Cohen 
1458e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
1459e126ba97SEli Cohen 	if (ret) {
1460e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify umr QP to rtr\n");
1461e126ba97SEli Cohen 		goto error_4;
1462e126ba97SEli Cohen 	}
1463e126ba97SEli Cohen 
1464e126ba97SEli Cohen 	memset(attr, 0, sizeof(*attr));
1465e126ba97SEli Cohen 	attr->qp_state = IB_QPS_RTS;
1466e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
1467e126ba97SEli Cohen 	if (ret) {
1468e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify umr QP to rts\n");
1469e126ba97SEli Cohen 		goto error_4;
1470e126ba97SEli Cohen 	}
1471e126ba97SEli Cohen 
1472e126ba97SEli Cohen 	dev->umrc.qp = qp;
1473e126ba97SEli Cohen 	dev->umrc.cq = cq;
1474e126ba97SEli Cohen 	dev->umrc.pd = pd;
1475e126ba97SEli Cohen 
1476e126ba97SEli Cohen 	sema_init(&dev->umrc.sem, MAX_UMR_WR);
1477e126ba97SEli Cohen 	ret = mlx5_mr_cache_init(dev);
1478e126ba97SEli Cohen 	if (ret) {
1479e126ba97SEli Cohen 		mlx5_ib_warn(dev, "mr cache init failed %d\n", ret);
1480e126ba97SEli Cohen 		goto error_4;
1481e126ba97SEli Cohen 	}
1482e126ba97SEli Cohen 
1483e126ba97SEli Cohen 	kfree(attr);
1484e126ba97SEli Cohen 	kfree(init_attr);
1485e126ba97SEli Cohen 
1486e126ba97SEli Cohen 	return 0;
1487e126ba97SEli Cohen 
1488e126ba97SEli Cohen error_4:
1489e126ba97SEli Cohen 	mlx5_ib_destroy_qp(qp);
1490e126ba97SEli Cohen 
1491e126ba97SEli Cohen error_3:
1492e126ba97SEli Cohen 	ib_destroy_cq(cq);
1493e126ba97SEli Cohen 
1494e126ba97SEli Cohen error_2:
1495e126ba97SEli Cohen 	ib_dealloc_pd(pd);
1496e126ba97SEli Cohen 
1497e126ba97SEli Cohen error_0:
1498e126ba97SEli Cohen 	kfree(attr);
1499e126ba97SEli Cohen 	kfree(init_attr);
1500e126ba97SEli Cohen 	return ret;
1501e126ba97SEli Cohen }
1502e126ba97SEli Cohen 
1503e126ba97SEli Cohen static int create_dev_resources(struct mlx5_ib_resources *devr)
1504e126ba97SEli Cohen {
1505e126ba97SEli Cohen 	struct ib_srq_init_attr attr;
1506e126ba97SEli Cohen 	struct mlx5_ib_dev *dev;
1507bcf4c1eaSMatan Barak 	struct ib_cq_init_attr cq_attr = {.cqe = 1};
1508e126ba97SEli Cohen 	int ret = 0;
1509e126ba97SEli Cohen 
1510e126ba97SEli Cohen 	dev = container_of(devr, struct mlx5_ib_dev, devr);
1511e126ba97SEli Cohen 
1512e126ba97SEli Cohen 	devr->p0 = mlx5_ib_alloc_pd(&dev->ib_dev, NULL, NULL);
1513e126ba97SEli Cohen 	if (IS_ERR(devr->p0)) {
1514e126ba97SEli Cohen 		ret = PTR_ERR(devr->p0);
1515e126ba97SEli Cohen 		goto error0;
1516e126ba97SEli Cohen 	}
1517e126ba97SEli Cohen 	devr->p0->device  = &dev->ib_dev;
1518e126ba97SEli Cohen 	devr->p0->uobject = NULL;
1519e126ba97SEli Cohen 	atomic_set(&devr->p0->usecnt, 0);
1520e126ba97SEli Cohen 
1521bcf4c1eaSMatan Barak 	devr->c0 = mlx5_ib_create_cq(&dev->ib_dev, &cq_attr, NULL, NULL);
1522e126ba97SEli Cohen 	if (IS_ERR(devr->c0)) {
1523e126ba97SEli Cohen 		ret = PTR_ERR(devr->c0);
1524e126ba97SEli Cohen 		goto error1;
1525e126ba97SEli Cohen 	}
1526e126ba97SEli Cohen 	devr->c0->device        = &dev->ib_dev;
1527e126ba97SEli Cohen 	devr->c0->uobject       = NULL;
1528e126ba97SEli Cohen 	devr->c0->comp_handler  = NULL;
1529e126ba97SEli Cohen 	devr->c0->event_handler = NULL;
1530e126ba97SEli Cohen 	devr->c0->cq_context    = NULL;
1531e126ba97SEli Cohen 	atomic_set(&devr->c0->usecnt, 0);
1532e126ba97SEli Cohen 
1533e126ba97SEli Cohen 	devr->x0 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
1534e126ba97SEli Cohen 	if (IS_ERR(devr->x0)) {
1535e126ba97SEli Cohen 		ret = PTR_ERR(devr->x0);
1536e126ba97SEli Cohen 		goto error2;
1537e126ba97SEli Cohen 	}
1538e126ba97SEli Cohen 	devr->x0->device = &dev->ib_dev;
1539e126ba97SEli Cohen 	devr->x0->inode = NULL;
1540e126ba97SEli Cohen 	atomic_set(&devr->x0->usecnt, 0);
1541e126ba97SEli Cohen 	mutex_init(&devr->x0->tgt_qp_mutex);
1542e126ba97SEli Cohen 	INIT_LIST_HEAD(&devr->x0->tgt_qp_list);
1543e126ba97SEli Cohen 
1544e126ba97SEli Cohen 	devr->x1 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
1545e126ba97SEli Cohen 	if (IS_ERR(devr->x1)) {
1546e126ba97SEli Cohen 		ret = PTR_ERR(devr->x1);
1547e126ba97SEli Cohen 		goto error3;
1548e126ba97SEli Cohen 	}
1549e126ba97SEli Cohen 	devr->x1->device = &dev->ib_dev;
1550e126ba97SEli Cohen 	devr->x1->inode = NULL;
1551e126ba97SEli Cohen 	atomic_set(&devr->x1->usecnt, 0);
1552e126ba97SEli Cohen 	mutex_init(&devr->x1->tgt_qp_mutex);
1553e126ba97SEli Cohen 	INIT_LIST_HEAD(&devr->x1->tgt_qp_list);
1554e126ba97SEli Cohen 
1555e126ba97SEli Cohen 	memset(&attr, 0, sizeof(attr));
1556e126ba97SEli Cohen 	attr.attr.max_sge = 1;
1557e126ba97SEli Cohen 	attr.attr.max_wr = 1;
1558e126ba97SEli Cohen 	attr.srq_type = IB_SRQT_XRC;
1559e126ba97SEli Cohen 	attr.ext.xrc.cq = devr->c0;
1560e126ba97SEli Cohen 	attr.ext.xrc.xrcd = devr->x0;
1561e126ba97SEli Cohen 
1562e126ba97SEli Cohen 	devr->s0 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
1563e126ba97SEli Cohen 	if (IS_ERR(devr->s0)) {
1564e126ba97SEli Cohen 		ret = PTR_ERR(devr->s0);
1565e126ba97SEli Cohen 		goto error4;
1566e126ba97SEli Cohen 	}
1567e126ba97SEli Cohen 	devr->s0->device	= &dev->ib_dev;
1568e126ba97SEli Cohen 	devr->s0->pd		= devr->p0;
1569e126ba97SEli Cohen 	devr->s0->uobject       = NULL;
1570e126ba97SEli Cohen 	devr->s0->event_handler = NULL;
1571e126ba97SEli Cohen 	devr->s0->srq_context   = NULL;
1572e126ba97SEli Cohen 	devr->s0->srq_type      = IB_SRQT_XRC;
1573e126ba97SEli Cohen 	devr->s0->ext.xrc.xrcd	= devr->x0;
1574e126ba97SEli Cohen 	devr->s0->ext.xrc.cq	= devr->c0;
1575e126ba97SEli Cohen 	atomic_inc(&devr->s0->ext.xrc.xrcd->usecnt);
1576e126ba97SEli Cohen 	atomic_inc(&devr->s0->ext.xrc.cq->usecnt);
1577e126ba97SEli Cohen 	atomic_inc(&devr->p0->usecnt);
1578e126ba97SEli Cohen 	atomic_set(&devr->s0->usecnt, 0);
1579e126ba97SEli Cohen 
15804aa17b28SHaggai Abramonvsky 	memset(&attr, 0, sizeof(attr));
15814aa17b28SHaggai Abramonvsky 	attr.attr.max_sge = 1;
15824aa17b28SHaggai Abramonvsky 	attr.attr.max_wr = 1;
15834aa17b28SHaggai Abramonvsky 	attr.srq_type = IB_SRQT_BASIC;
15844aa17b28SHaggai Abramonvsky 	devr->s1 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
15854aa17b28SHaggai Abramonvsky 	if (IS_ERR(devr->s1)) {
15864aa17b28SHaggai Abramonvsky 		ret = PTR_ERR(devr->s1);
15874aa17b28SHaggai Abramonvsky 		goto error5;
15884aa17b28SHaggai Abramonvsky 	}
15894aa17b28SHaggai Abramonvsky 	devr->s1->device	= &dev->ib_dev;
15904aa17b28SHaggai Abramonvsky 	devr->s1->pd		= devr->p0;
15914aa17b28SHaggai Abramonvsky 	devr->s1->uobject       = NULL;
15924aa17b28SHaggai Abramonvsky 	devr->s1->event_handler = NULL;
15934aa17b28SHaggai Abramonvsky 	devr->s1->srq_context   = NULL;
15944aa17b28SHaggai Abramonvsky 	devr->s1->srq_type      = IB_SRQT_BASIC;
15954aa17b28SHaggai Abramonvsky 	devr->s1->ext.xrc.cq	= devr->c0;
15964aa17b28SHaggai Abramonvsky 	atomic_inc(&devr->p0->usecnt);
15974aa17b28SHaggai Abramonvsky 	atomic_set(&devr->s0->usecnt, 0);
15984aa17b28SHaggai Abramonvsky 
1599e126ba97SEli Cohen 	return 0;
1600e126ba97SEli Cohen 
16014aa17b28SHaggai Abramonvsky error5:
16024aa17b28SHaggai Abramonvsky 	mlx5_ib_destroy_srq(devr->s0);
1603e126ba97SEli Cohen error4:
1604e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x1);
1605e126ba97SEli Cohen error3:
1606e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x0);
1607e126ba97SEli Cohen error2:
1608e126ba97SEli Cohen 	mlx5_ib_destroy_cq(devr->c0);
1609e126ba97SEli Cohen error1:
1610e126ba97SEli Cohen 	mlx5_ib_dealloc_pd(devr->p0);
1611e126ba97SEli Cohen error0:
1612e126ba97SEli Cohen 	return ret;
1613e126ba97SEli Cohen }
1614e126ba97SEli Cohen 
1615e126ba97SEli Cohen static void destroy_dev_resources(struct mlx5_ib_resources *devr)
1616e126ba97SEli Cohen {
16174aa17b28SHaggai Abramonvsky 	mlx5_ib_destroy_srq(devr->s1);
1618e126ba97SEli Cohen 	mlx5_ib_destroy_srq(devr->s0);
1619e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x0);
1620e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x1);
1621e126ba97SEli Cohen 	mlx5_ib_destroy_cq(devr->c0);
1622e126ba97SEli Cohen 	mlx5_ib_dealloc_pd(devr->p0);
1623e126ba97SEli Cohen }
1624e126ba97SEli Cohen 
1625e53505a8SAchiad Shochat static u32 get_core_cap_flags(struct ib_device *ibdev)
1626e53505a8SAchiad Shochat {
1627e53505a8SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1628e53505a8SAchiad Shochat 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, 1);
1629e53505a8SAchiad Shochat 	u8 l3_type_cap = MLX5_CAP_ROCE(dev->mdev, l3_type);
1630e53505a8SAchiad Shochat 	u8 roce_version_cap = MLX5_CAP_ROCE(dev->mdev, roce_version);
1631e53505a8SAchiad Shochat 	u32 ret = 0;
1632e53505a8SAchiad Shochat 
1633e53505a8SAchiad Shochat 	if (ll == IB_LINK_LAYER_INFINIBAND)
1634e53505a8SAchiad Shochat 		return RDMA_CORE_PORT_IBA_IB;
1635e53505a8SAchiad Shochat 
1636e53505a8SAchiad Shochat 	if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV4_CAP))
1637e53505a8SAchiad Shochat 		return 0;
1638e53505a8SAchiad Shochat 
1639e53505a8SAchiad Shochat 	if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV6_CAP))
1640e53505a8SAchiad Shochat 		return 0;
1641e53505a8SAchiad Shochat 
1642e53505a8SAchiad Shochat 	if (roce_version_cap & MLX5_ROCE_VERSION_1_CAP)
1643e53505a8SAchiad Shochat 		ret |= RDMA_CORE_PORT_IBA_ROCE;
1644e53505a8SAchiad Shochat 
1645e53505a8SAchiad Shochat 	if (roce_version_cap & MLX5_ROCE_VERSION_2_CAP)
1646e53505a8SAchiad Shochat 		ret |= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
1647e53505a8SAchiad Shochat 
1648e53505a8SAchiad Shochat 	return ret;
1649e53505a8SAchiad Shochat }
1650e53505a8SAchiad Shochat 
16517738613eSIra Weiny static int mlx5_port_immutable(struct ib_device *ibdev, u8 port_num,
16527738613eSIra Weiny 			       struct ib_port_immutable *immutable)
16537738613eSIra Weiny {
16547738613eSIra Weiny 	struct ib_port_attr attr;
16557738613eSIra Weiny 	int err;
16567738613eSIra Weiny 
16577738613eSIra Weiny 	err = mlx5_ib_query_port(ibdev, port_num, &attr);
16587738613eSIra Weiny 	if (err)
16597738613eSIra Weiny 		return err;
16607738613eSIra Weiny 
16617738613eSIra Weiny 	immutable->pkey_tbl_len = attr.pkey_tbl_len;
16627738613eSIra Weiny 	immutable->gid_tbl_len = attr.gid_tbl_len;
1663e53505a8SAchiad Shochat 	immutable->core_cap_flags = get_core_cap_flags(ibdev);
1664337877a4SIra Weiny 	immutable->max_mad_size = IB_MGMT_MAD_SIZE;
16657738613eSIra Weiny 
16667738613eSIra Weiny 	return 0;
16677738613eSIra Weiny }
16687738613eSIra Weiny 
1669fc24fc5eSAchiad Shochat static int mlx5_enable_roce(struct mlx5_ib_dev *dev)
1670fc24fc5eSAchiad Shochat {
1671e53505a8SAchiad Shochat 	int err;
1672e53505a8SAchiad Shochat 
1673fc24fc5eSAchiad Shochat 	dev->roce.nb.notifier_call = mlx5_netdev_event;
1674e53505a8SAchiad Shochat 	err = register_netdevice_notifier(&dev->roce.nb);
1675e53505a8SAchiad Shochat 	if (err)
1676e53505a8SAchiad Shochat 		return err;
1677e53505a8SAchiad Shochat 
1678e53505a8SAchiad Shochat 	err = mlx5_nic_vport_enable_roce(dev->mdev);
1679e53505a8SAchiad Shochat 	if (err)
1680e53505a8SAchiad Shochat 		goto err_unregister_netdevice_notifier;
1681e53505a8SAchiad Shochat 
1682e53505a8SAchiad Shochat 	return 0;
1683e53505a8SAchiad Shochat 
1684e53505a8SAchiad Shochat err_unregister_netdevice_notifier:
1685e53505a8SAchiad Shochat 	unregister_netdevice_notifier(&dev->roce.nb);
1686e53505a8SAchiad Shochat 	return err;
1687fc24fc5eSAchiad Shochat }
1688fc24fc5eSAchiad Shochat 
1689fc24fc5eSAchiad Shochat static void mlx5_disable_roce(struct mlx5_ib_dev *dev)
1690fc24fc5eSAchiad Shochat {
1691e53505a8SAchiad Shochat 	mlx5_nic_vport_disable_roce(dev->mdev);
1692fc24fc5eSAchiad Shochat 	unregister_netdevice_notifier(&dev->roce.nb);
1693fc24fc5eSAchiad Shochat }
1694fc24fc5eSAchiad Shochat 
16959603b61dSJack Morgenstein static void *mlx5_ib_add(struct mlx5_core_dev *mdev)
1696e126ba97SEli Cohen {
1697e126ba97SEli Cohen 	struct mlx5_ib_dev *dev;
1698ebd61f68SAchiad Shochat 	enum rdma_link_layer ll;
1699ebd61f68SAchiad Shochat 	int port_type_cap;
1700e126ba97SEli Cohen 	int err;
1701e126ba97SEli Cohen 	int i;
1702e126ba97SEli Cohen 
1703ebd61f68SAchiad Shochat 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
1704ebd61f68SAchiad Shochat 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
1705ebd61f68SAchiad Shochat 
1706e53505a8SAchiad Shochat 	if ((ll == IB_LINK_LAYER_ETHERNET) && !MLX5_CAP_GEN(mdev, roce))
1707647241eaSMajd Dibbiny 		return NULL;
1708647241eaSMajd Dibbiny 
1709e126ba97SEli Cohen 	printk_once(KERN_INFO "%s", mlx5_version);
1710e126ba97SEli Cohen 
1711e126ba97SEli Cohen 	dev = (struct mlx5_ib_dev *)ib_alloc_device(sizeof(*dev));
1712e126ba97SEli Cohen 	if (!dev)
17139603b61dSJack Morgenstein 		return NULL;
1714e126ba97SEli Cohen 
17159603b61dSJack Morgenstein 	dev->mdev = mdev;
1716e126ba97SEli Cohen 
1717fc24fc5eSAchiad Shochat 	rwlock_init(&dev->roce.netdev_lock);
1718e126ba97SEli Cohen 	err = get_port_caps(dev);
1719e126ba97SEli Cohen 	if (err)
17209603b61dSJack Morgenstein 		goto err_dealloc;
1721e126ba97SEli Cohen 
17221b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(dev))
1723e126ba97SEli Cohen 		get_ext_port_caps(dev);
1724e126ba97SEli Cohen 
1725e126ba97SEli Cohen 	MLX5_INIT_DOORBELL_LOCK(&dev->uar_lock);
1726e126ba97SEli Cohen 
1727e126ba97SEli Cohen 	strlcpy(dev->ib_dev.name, "mlx5_%d", IB_DEVICE_NAME_MAX);
1728e126ba97SEli Cohen 	dev->ib_dev.owner		= THIS_MODULE;
1729e126ba97SEli Cohen 	dev->ib_dev.node_type		= RDMA_NODE_IB_CA;
1730c6790aa9SSagi Grimberg 	dev->ib_dev.local_dma_lkey	= 0 /* not supported for now */;
1731938fe83cSSaeed Mahameed 	dev->num_ports		= MLX5_CAP_GEN(mdev, num_ports);
1732e126ba97SEli Cohen 	dev->ib_dev.phys_port_cnt     = dev->num_ports;
1733233d05d2SSaeed Mahameed 	dev->ib_dev.num_comp_vectors    =
1734233d05d2SSaeed Mahameed 		dev->mdev->priv.eq_table.num_comp_vectors;
1735e126ba97SEli Cohen 	dev->ib_dev.dma_device	= &mdev->pdev->dev;
1736e126ba97SEli Cohen 
1737e126ba97SEli Cohen 	dev->ib_dev.uverbs_abi_ver	= MLX5_IB_UVERBS_ABI_VERSION;
1738e126ba97SEli Cohen 	dev->ib_dev.uverbs_cmd_mask	=
1739e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_GET_CONTEXT)		|
1740e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_DEVICE)	|
1741e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_PORT)		|
1742e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_ALLOC_PD)		|
1743e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DEALLOC_PD)		|
1744e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_REG_MR)		|
1745e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DEREG_MR)		|
1746e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL)	|
1747e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_CQ)		|
1748e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_RESIZE_CQ)		|
1749e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_CQ)		|
1750e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_QP)		|
1751e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_MODIFY_QP)		|
1752e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_QP)		|
1753e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_QP)		|
1754e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_ATTACH_MCAST)	|
1755e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DETACH_MCAST)	|
1756e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_SRQ)		|
1757e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_MODIFY_SRQ)		|
1758e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_SRQ)		|
1759e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_SRQ)		|
1760e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_XSRQ)		|
1761e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_OPEN_QP);
17621707cb4aSHaggai Eran 	dev->ib_dev.uverbs_ex_cmd_mask =
17631707cb4aSHaggai Eran 		(1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE);
1764e126ba97SEli Cohen 
1765e126ba97SEli Cohen 	dev->ib_dev.query_device	= mlx5_ib_query_device;
1766e126ba97SEli Cohen 	dev->ib_dev.query_port		= mlx5_ib_query_port;
1767ebd61f68SAchiad Shochat 	dev->ib_dev.get_link_layer	= mlx5_ib_port_link_layer;
1768fc24fc5eSAchiad Shochat 	if (ll == IB_LINK_LAYER_ETHERNET)
1769fc24fc5eSAchiad Shochat 		dev->ib_dev.get_netdev	= mlx5_ib_get_netdev;
1770e126ba97SEli Cohen 	dev->ib_dev.query_gid		= mlx5_ib_query_gid;
17713cca2606SAchiad Shochat 	dev->ib_dev.add_gid		= mlx5_ib_add_gid;
17723cca2606SAchiad Shochat 	dev->ib_dev.del_gid		= mlx5_ib_del_gid;
1773e126ba97SEli Cohen 	dev->ib_dev.query_pkey		= mlx5_ib_query_pkey;
1774e126ba97SEli Cohen 	dev->ib_dev.modify_device	= mlx5_ib_modify_device;
1775e126ba97SEli Cohen 	dev->ib_dev.modify_port		= mlx5_ib_modify_port;
1776e126ba97SEli Cohen 	dev->ib_dev.alloc_ucontext	= mlx5_ib_alloc_ucontext;
1777e126ba97SEli Cohen 	dev->ib_dev.dealloc_ucontext	= mlx5_ib_dealloc_ucontext;
1778e126ba97SEli Cohen 	dev->ib_dev.mmap		= mlx5_ib_mmap;
1779e126ba97SEli Cohen 	dev->ib_dev.alloc_pd		= mlx5_ib_alloc_pd;
1780e126ba97SEli Cohen 	dev->ib_dev.dealloc_pd		= mlx5_ib_dealloc_pd;
1781e126ba97SEli Cohen 	dev->ib_dev.create_ah		= mlx5_ib_create_ah;
1782e126ba97SEli Cohen 	dev->ib_dev.query_ah		= mlx5_ib_query_ah;
1783e126ba97SEli Cohen 	dev->ib_dev.destroy_ah		= mlx5_ib_destroy_ah;
1784e126ba97SEli Cohen 	dev->ib_dev.create_srq		= mlx5_ib_create_srq;
1785e126ba97SEli Cohen 	dev->ib_dev.modify_srq		= mlx5_ib_modify_srq;
1786e126ba97SEli Cohen 	dev->ib_dev.query_srq		= mlx5_ib_query_srq;
1787e126ba97SEli Cohen 	dev->ib_dev.destroy_srq		= mlx5_ib_destroy_srq;
1788e126ba97SEli Cohen 	dev->ib_dev.post_srq_recv	= mlx5_ib_post_srq_recv;
1789e126ba97SEli Cohen 	dev->ib_dev.create_qp		= mlx5_ib_create_qp;
1790e126ba97SEli Cohen 	dev->ib_dev.modify_qp		= mlx5_ib_modify_qp;
1791e126ba97SEli Cohen 	dev->ib_dev.query_qp		= mlx5_ib_query_qp;
1792e126ba97SEli Cohen 	dev->ib_dev.destroy_qp		= mlx5_ib_destroy_qp;
1793e126ba97SEli Cohen 	dev->ib_dev.post_send		= mlx5_ib_post_send;
1794e126ba97SEli Cohen 	dev->ib_dev.post_recv		= mlx5_ib_post_recv;
1795e126ba97SEli Cohen 	dev->ib_dev.create_cq		= mlx5_ib_create_cq;
1796e126ba97SEli Cohen 	dev->ib_dev.modify_cq		= mlx5_ib_modify_cq;
1797e126ba97SEli Cohen 	dev->ib_dev.resize_cq		= mlx5_ib_resize_cq;
1798e126ba97SEli Cohen 	dev->ib_dev.destroy_cq		= mlx5_ib_destroy_cq;
1799e126ba97SEli Cohen 	dev->ib_dev.poll_cq		= mlx5_ib_poll_cq;
1800e126ba97SEli Cohen 	dev->ib_dev.req_notify_cq	= mlx5_ib_arm_cq;
1801e126ba97SEli Cohen 	dev->ib_dev.get_dma_mr		= mlx5_ib_get_dma_mr;
1802e126ba97SEli Cohen 	dev->ib_dev.reg_user_mr		= mlx5_ib_reg_user_mr;
1803e126ba97SEli Cohen 	dev->ib_dev.dereg_mr		= mlx5_ib_dereg_mr;
1804e126ba97SEli Cohen 	dev->ib_dev.attach_mcast	= mlx5_ib_mcg_attach;
1805e126ba97SEli Cohen 	dev->ib_dev.detach_mcast	= mlx5_ib_mcg_detach;
1806e126ba97SEli Cohen 	dev->ib_dev.process_mad		= mlx5_ib_process_mad;
18079bee178bSSagi Grimberg 	dev->ib_dev.alloc_mr		= mlx5_ib_alloc_mr;
18088a187ee5SSagi Grimberg 	dev->ib_dev.map_mr_sg		= mlx5_ib_map_mr_sg;
1809d5436ba0SSagi Grimberg 	dev->ib_dev.check_mr_status	= mlx5_ib_check_mr_status;
18107738613eSIra Weiny 	dev->ib_dev.get_port_immutable  = mlx5_port_immutable;
1811e126ba97SEli Cohen 
1812938fe83cSSaeed Mahameed 	mlx5_ib_internal_fill_odp_caps(dev);
18138cdd312cSHaggai Eran 
1814938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, xrc)) {
1815e126ba97SEli Cohen 		dev->ib_dev.alloc_xrcd = mlx5_ib_alloc_xrcd;
1816e126ba97SEli Cohen 		dev->ib_dev.dealloc_xrcd = mlx5_ib_dealloc_xrcd;
1817e126ba97SEli Cohen 		dev->ib_dev.uverbs_cmd_mask |=
1818e126ba97SEli Cohen 			(1ull << IB_USER_VERBS_CMD_OPEN_XRCD) |
1819e126ba97SEli Cohen 			(1ull << IB_USER_VERBS_CMD_CLOSE_XRCD);
1820e126ba97SEli Cohen 	}
1821e126ba97SEli Cohen 
1822e126ba97SEli Cohen 	err = init_node_data(dev);
1823e126ba97SEli Cohen 	if (err)
1824233d05d2SSaeed Mahameed 		goto err_dealloc;
1825e126ba97SEli Cohen 
1826e126ba97SEli Cohen 	mutex_init(&dev->cap_mask_mutex);
1827e126ba97SEli Cohen 
1828fc24fc5eSAchiad Shochat 	if (ll == IB_LINK_LAYER_ETHERNET) {
1829fc24fc5eSAchiad Shochat 		err = mlx5_enable_roce(dev);
1830e126ba97SEli Cohen 		if (err)
1831233d05d2SSaeed Mahameed 			goto err_dealloc;
1832fc24fc5eSAchiad Shochat 	}
1833fc24fc5eSAchiad Shochat 
1834fc24fc5eSAchiad Shochat 	err = create_dev_resources(&dev->devr);
1835fc24fc5eSAchiad Shochat 	if (err)
1836fc24fc5eSAchiad Shochat 		goto err_disable_roce;
1837e126ba97SEli Cohen 
18386aec21f6SHaggai Eran 	err = mlx5_ib_odp_init_one(dev);
1839281d1a92SWei Yongjun 	if (err)
1840e126ba97SEli Cohen 		goto err_rsrc;
1841e126ba97SEli Cohen 
18426aec21f6SHaggai Eran 	err = ib_register_device(&dev->ib_dev, NULL);
18436aec21f6SHaggai Eran 	if (err)
18446aec21f6SHaggai Eran 		goto err_odp;
18456aec21f6SHaggai Eran 
1846e126ba97SEli Cohen 	err = create_umr_res(dev);
1847e126ba97SEli Cohen 	if (err)
1848e126ba97SEli Cohen 		goto err_dev;
1849e126ba97SEli Cohen 
1850e126ba97SEli Cohen 	for (i = 0; i < ARRAY_SIZE(mlx5_class_attributes); i++) {
1851281d1a92SWei Yongjun 		err = device_create_file(&dev->ib_dev.dev,
1852281d1a92SWei Yongjun 					 mlx5_class_attributes[i]);
1853281d1a92SWei Yongjun 		if (err)
1854e126ba97SEli Cohen 			goto err_umrc;
1855e126ba97SEli Cohen 	}
1856e126ba97SEli Cohen 
1857e126ba97SEli Cohen 	dev->ib_active = true;
1858e126ba97SEli Cohen 
18599603b61dSJack Morgenstein 	return dev;
1860e126ba97SEli Cohen 
1861e126ba97SEli Cohen err_umrc:
1862e126ba97SEli Cohen 	destroy_umrc_res(dev);
1863e126ba97SEli Cohen 
1864e126ba97SEli Cohen err_dev:
1865e126ba97SEli Cohen 	ib_unregister_device(&dev->ib_dev);
1866e126ba97SEli Cohen 
18676aec21f6SHaggai Eran err_odp:
18686aec21f6SHaggai Eran 	mlx5_ib_odp_remove_one(dev);
18696aec21f6SHaggai Eran 
1870e126ba97SEli Cohen err_rsrc:
1871e126ba97SEli Cohen 	destroy_dev_resources(&dev->devr);
1872e126ba97SEli Cohen 
1873fc24fc5eSAchiad Shochat err_disable_roce:
1874fc24fc5eSAchiad Shochat 	if (ll == IB_LINK_LAYER_ETHERNET)
1875fc24fc5eSAchiad Shochat 		mlx5_disable_roce(dev);
1876fc24fc5eSAchiad Shochat 
18779603b61dSJack Morgenstein err_dealloc:
1878e126ba97SEli Cohen 	ib_dealloc_device((struct ib_device *)dev);
1879e126ba97SEli Cohen 
18809603b61dSJack Morgenstein 	return NULL;
1881e126ba97SEli Cohen }
1882e126ba97SEli Cohen 
18839603b61dSJack Morgenstein static void mlx5_ib_remove(struct mlx5_core_dev *mdev, void *context)
1884e126ba97SEli Cohen {
18859603b61dSJack Morgenstein 	struct mlx5_ib_dev *dev = context;
1886fc24fc5eSAchiad Shochat 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev, 1);
18876aec21f6SHaggai Eran 
1888e126ba97SEli Cohen 	ib_unregister_device(&dev->ib_dev);
1889eefd56e5SEli Cohen 	destroy_umrc_res(dev);
18906aec21f6SHaggai Eran 	mlx5_ib_odp_remove_one(dev);
1891e126ba97SEli Cohen 	destroy_dev_resources(&dev->devr);
1892fc24fc5eSAchiad Shochat 	if (ll == IB_LINK_LAYER_ETHERNET)
1893fc24fc5eSAchiad Shochat 		mlx5_disable_roce(dev);
1894e126ba97SEli Cohen 	ib_dealloc_device(&dev->ib_dev);
1895e126ba97SEli Cohen }
1896e126ba97SEli Cohen 
18979603b61dSJack Morgenstein static struct mlx5_interface mlx5_ib_interface = {
18989603b61dSJack Morgenstein 	.add            = mlx5_ib_add,
18999603b61dSJack Morgenstein 	.remove         = mlx5_ib_remove,
19009603b61dSJack Morgenstein 	.event          = mlx5_ib_event,
190164613d94SSaeed Mahameed 	.protocol	= MLX5_INTERFACE_PROTOCOL_IB,
1902e126ba97SEli Cohen };
1903e126ba97SEli Cohen 
1904e126ba97SEli Cohen static int __init mlx5_ib_init(void)
1905e126ba97SEli Cohen {
19066aec21f6SHaggai Eran 	int err;
19076aec21f6SHaggai Eran 
19089603b61dSJack Morgenstein 	if (deprecated_prof_sel != 2)
19099603b61dSJack Morgenstein 		pr_warn("prof_sel is deprecated for mlx5_ib, set it for mlx5_core\n");
19109603b61dSJack Morgenstein 
19116aec21f6SHaggai Eran 	err = mlx5_ib_odp_init();
19126aec21f6SHaggai Eran 	if (err)
19136aec21f6SHaggai Eran 		return err;
19146aec21f6SHaggai Eran 
19156aec21f6SHaggai Eran 	err = mlx5_register_interface(&mlx5_ib_interface);
19166aec21f6SHaggai Eran 	if (err)
19176aec21f6SHaggai Eran 		goto clean_odp;
19186aec21f6SHaggai Eran 
19196aec21f6SHaggai Eran 	return err;
19206aec21f6SHaggai Eran 
19216aec21f6SHaggai Eran clean_odp:
19226aec21f6SHaggai Eran 	mlx5_ib_odp_cleanup();
19236aec21f6SHaggai Eran 	return err;
1924e126ba97SEli Cohen }
1925e126ba97SEli Cohen 
1926e126ba97SEli Cohen static void __exit mlx5_ib_cleanup(void)
1927e126ba97SEli Cohen {
19289603b61dSJack Morgenstein 	mlx5_unregister_interface(&mlx5_ib_interface);
19296aec21f6SHaggai Eran 	mlx5_ib_odp_cleanup();
1930e126ba97SEli Cohen }
1931e126ba97SEli Cohen 
1932e126ba97SEli Cohen module_init(mlx5_ib_init);
1933e126ba97SEli Cohen module_exit(mlx5_ib_cleanup);
1934