xref: /openbmc/linux/drivers/infiniband/hw/mlx5/main.c (revision 3f89a643eb29543af0838d37604bbc29a4e1eb60)
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>
43*3f89a643SAchiad Shochat #include <rdma/ib_addr.h>
441b5daf11SMajd Dibbiny #include <linux/mlx5/vport.h>
45e126ba97SEli Cohen #include <rdma/ib_smi.h>
46e126ba97SEli Cohen #include <rdma/ib_umem.h>
47e126ba97SEli Cohen #include "user.h"
48e126ba97SEli Cohen #include "mlx5_ib.h"
49e126ba97SEli Cohen 
50e126ba97SEli Cohen #define DRIVER_NAME "mlx5_ib"
51169a1d85SAmir Vadai #define DRIVER_VERSION "2.2-1"
52169a1d85SAmir Vadai #define DRIVER_RELDATE	"Feb 2014"
53e126ba97SEli Cohen 
54e126ba97SEli Cohen MODULE_AUTHOR("Eli Cohen <eli@mellanox.com>");
55e126ba97SEli Cohen MODULE_DESCRIPTION("Mellanox Connect-IB HCA IB driver");
56e126ba97SEli Cohen MODULE_LICENSE("Dual BSD/GPL");
57e126ba97SEli Cohen MODULE_VERSION(DRIVER_VERSION);
58e126ba97SEli Cohen 
599603b61dSJack Morgenstein static int deprecated_prof_sel = 2;
609603b61dSJack Morgenstein module_param_named(prof_sel, deprecated_prof_sel, int, 0444);
619603b61dSJack Morgenstein MODULE_PARM_DESC(prof_sel, "profile selector. Deprecated here. Moved to module mlx5_core");
62e126ba97SEli Cohen 
63e126ba97SEli Cohen static char mlx5_version[] =
64e126ba97SEli Cohen 	DRIVER_NAME ": Mellanox Connect-IB Infiniband driver v"
65e126ba97SEli Cohen 	DRIVER_VERSION " (" DRIVER_RELDATE ")\n";
66e126ba97SEli Cohen 
671b5daf11SMajd Dibbiny static enum rdma_link_layer
68ebd61f68SAchiad Shochat mlx5_port_type_cap_to_rdma_ll(int port_type_cap)
691b5daf11SMajd Dibbiny {
70ebd61f68SAchiad Shochat 	switch (port_type_cap) {
711b5daf11SMajd Dibbiny 	case MLX5_CAP_PORT_TYPE_IB:
721b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_INFINIBAND;
731b5daf11SMajd Dibbiny 	case MLX5_CAP_PORT_TYPE_ETH:
741b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_ETHERNET;
751b5daf11SMajd Dibbiny 	default:
761b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_UNSPECIFIED;
771b5daf11SMajd Dibbiny 	}
781b5daf11SMajd Dibbiny }
791b5daf11SMajd Dibbiny 
80ebd61f68SAchiad Shochat static enum rdma_link_layer
81ebd61f68SAchiad Shochat mlx5_ib_port_link_layer(struct ib_device *device, u8 port_num)
82ebd61f68SAchiad Shochat {
83ebd61f68SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(device);
84ebd61f68SAchiad Shochat 	int port_type_cap = MLX5_CAP_GEN(dev->mdev, port_type);
85ebd61f68SAchiad Shochat 
86ebd61f68SAchiad Shochat 	return mlx5_port_type_cap_to_rdma_ll(port_type_cap);
87ebd61f68SAchiad Shochat }
88ebd61f68SAchiad Shochat 
89fc24fc5eSAchiad Shochat static int mlx5_netdev_event(struct notifier_block *this,
90fc24fc5eSAchiad Shochat 			     unsigned long event, void *ptr)
91fc24fc5eSAchiad Shochat {
92fc24fc5eSAchiad Shochat 	struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
93fc24fc5eSAchiad Shochat 	struct mlx5_ib_dev *ibdev = container_of(this, struct mlx5_ib_dev,
94fc24fc5eSAchiad Shochat 						 roce.nb);
95fc24fc5eSAchiad Shochat 
96fc24fc5eSAchiad Shochat 	if ((event != NETDEV_UNREGISTER) && (event != NETDEV_REGISTER))
97fc24fc5eSAchiad Shochat 		return NOTIFY_DONE;
98fc24fc5eSAchiad Shochat 
99fc24fc5eSAchiad Shochat 	write_lock(&ibdev->roce.netdev_lock);
100fc24fc5eSAchiad Shochat 	if (ndev->dev.parent == &ibdev->mdev->pdev->dev)
101fc24fc5eSAchiad Shochat 		ibdev->roce.netdev = (event == NETDEV_UNREGISTER) ? NULL : ndev;
102fc24fc5eSAchiad Shochat 	write_unlock(&ibdev->roce.netdev_lock);
103fc24fc5eSAchiad Shochat 
104fc24fc5eSAchiad Shochat 	return NOTIFY_DONE;
105fc24fc5eSAchiad Shochat }
106fc24fc5eSAchiad Shochat 
107fc24fc5eSAchiad Shochat static struct net_device *mlx5_ib_get_netdev(struct ib_device *device,
108fc24fc5eSAchiad Shochat 					     u8 port_num)
109fc24fc5eSAchiad Shochat {
110fc24fc5eSAchiad Shochat 	struct mlx5_ib_dev *ibdev = to_mdev(device);
111fc24fc5eSAchiad Shochat 	struct net_device *ndev;
112fc24fc5eSAchiad Shochat 
113fc24fc5eSAchiad Shochat 	/* Ensure ndev does not disappear before we invoke dev_hold()
114fc24fc5eSAchiad Shochat 	 */
115fc24fc5eSAchiad Shochat 	read_lock(&ibdev->roce.netdev_lock);
116fc24fc5eSAchiad Shochat 	ndev = ibdev->roce.netdev;
117fc24fc5eSAchiad Shochat 	if (ndev)
118fc24fc5eSAchiad Shochat 		dev_hold(ndev);
119fc24fc5eSAchiad Shochat 	read_unlock(&ibdev->roce.netdev_lock);
120fc24fc5eSAchiad Shochat 
121fc24fc5eSAchiad Shochat 	return ndev;
122fc24fc5eSAchiad Shochat }
123fc24fc5eSAchiad Shochat 
124*3f89a643SAchiad Shochat static int mlx5_query_port_roce(struct ib_device *device, u8 port_num,
125*3f89a643SAchiad Shochat 				struct ib_port_attr *props)
126*3f89a643SAchiad Shochat {
127*3f89a643SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(device);
128*3f89a643SAchiad Shochat 	struct net_device *ndev;
129*3f89a643SAchiad Shochat 	enum ib_mtu ndev_ib_mtu;
130*3f89a643SAchiad Shochat 
131*3f89a643SAchiad Shochat 	memset(props, 0, sizeof(*props));
132*3f89a643SAchiad Shochat 
133*3f89a643SAchiad Shochat 	props->port_cap_flags  |= IB_PORT_CM_SUP;
134*3f89a643SAchiad Shochat 	props->port_cap_flags  |= IB_PORT_IP_BASED_GIDS;
135*3f89a643SAchiad Shochat 
136*3f89a643SAchiad Shochat 	props->gid_tbl_len      = MLX5_CAP_ROCE(dev->mdev,
137*3f89a643SAchiad Shochat 						roce_address_table_size);
138*3f89a643SAchiad Shochat 	props->max_mtu          = IB_MTU_4096;
139*3f89a643SAchiad Shochat 	props->max_msg_sz       = 1 << MLX5_CAP_GEN(dev->mdev, log_max_msg);
140*3f89a643SAchiad Shochat 	props->pkey_tbl_len     = 1;
141*3f89a643SAchiad Shochat 	props->state            = IB_PORT_DOWN;
142*3f89a643SAchiad Shochat 	props->phys_state       = 3;
143*3f89a643SAchiad Shochat 
144*3f89a643SAchiad Shochat 	mlx5_query_nic_vport_qkey_viol_cntr(dev->mdev,
145*3f89a643SAchiad Shochat 					    (u16 *)&props->qkey_viol_cntr);
146*3f89a643SAchiad Shochat 
147*3f89a643SAchiad Shochat 	ndev = mlx5_ib_get_netdev(device, port_num);
148*3f89a643SAchiad Shochat 	if (!ndev)
149*3f89a643SAchiad Shochat 		return 0;
150*3f89a643SAchiad Shochat 
151*3f89a643SAchiad Shochat 	if (netif_running(ndev) && netif_carrier_ok(ndev)) {
152*3f89a643SAchiad Shochat 		props->state      = IB_PORT_ACTIVE;
153*3f89a643SAchiad Shochat 		props->phys_state = 5;
154*3f89a643SAchiad Shochat 	}
155*3f89a643SAchiad Shochat 
156*3f89a643SAchiad Shochat 	ndev_ib_mtu = iboe_get_mtu(ndev->mtu);
157*3f89a643SAchiad Shochat 
158*3f89a643SAchiad Shochat 	dev_put(ndev);
159*3f89a643SAchiad Shochat 
160*3f89a643SAchiad Shochat 	props->active_mtu	= min(props->max_mtu, ndev_ib_mtu);
161*3f89a643SAchiad Shochat 
162*3f89a643SAchiad Shochat 	props->active_width	= IB_WIDTH_4X;  /* TODO */
163*3f89a643SAchiad Shochat 	props->active_speed	= IB_SPEED_QDR; /* TODO */
164*3f89a643SAchiad Shochat 
165*3f89a643SAchiad Shochat 	return 0;
166*3f89a643SAchiad Shochat }
167*3f89a643SAchiad Shochat 
1681b5daf11SMajd Dibbiny static int mlx5_use_mad_ifc(struct mlx5_ib_dev *dev)
1691b5daf11SMajd Dibbiny {
1701b5daf11SMajd Dibbiny 	return !dev->mdev->issi;
1711b5daf11SMajd Dibbiny }
1721b5daf11SMajd Dibbiny 
1731b5daf11SMajd Dibbiny enum {
1741b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_MAD,
1751b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_HCA,
1761b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_NIC,
1771b5daf11SMajd Dibbiny };
1781b5daf11SMajd Dibbiny 
1791b5daf11SMajd Dibbiny static int mlx5_get_vport_access_method(struct ib_device *ibdev)
1801b5daf11SMajd Dibbiny {
1811b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(to_mdev(ibdev)))
1821b5daf11SMajd Dibbiny 		return MLX5_VPORT_ACCESS_METHOD_MAD;
1831b5daf11SMajd Dibbiny 
184ebd61f68SAchiad Shochat 	if (mlx5_ib_port_link_layer(ibdev, 1) ==
1851b5daf11SMajd Dibbiny 	    IB_LINK_LAYER_ETHERNET)
1861b5daf11SMajd Dibbiny 		return MLX5_VPORT_ACCESS_METHOD_NIC;
1871b5daf11SMajd Dibbiny 
1881b5daf11SMajd Dibbiny 	return MLX5_VPORT_ACCESS_METHOD_HCA;
1891b5daf11SMajd Dibbiny }
1901b5daf11SMajd Dibbiny 
1911b5daf11SMajd Dibbiny static int mlx5_query_system_image_guid(struct ib_device *ibdev,
1921b5daf11SMajd Dibbiny 					__be64 *sys_image_guid)
1931b5daf11SMajd Dibbiny {
1941b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1951b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
1961b5daf11SMajd Dibbiny 	u64 tmp;
1971b5daf11SMajd Dibbiny 	int err;
1981b5daf11SMajd Dibbiny 
1991b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
2001b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
2011b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_system_image_guid(ibdev,
2021b5daf11SMajd Dibbiny 							    sys_image_guid);
2031b5daf11SMajd Dibbiny 
2041b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
2051b5daf11SMajd Dibbiny 		err = mlx5_query_hca_vport_system_image_guid(mdev, &tmp);
206*3f89a643SAchiad Shochat 		break;
207*3f89a643SAchiad Shochat 
208*3f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
209*3f89a643SAchiad Shochat 		err = mlx5_query_nic_vport_system_image_guid(mdev, &tmp);
210*3f89a643SAchiad Shochat 		break;
2111b5daf11SMajd Dibbiny 
2121b5daf11SMajd Dibbiny 	default:
2131b5daf11SMajd Dibbiny 		return -EINVAL;
2141b5daf11SMajd Dibbiny 	}
215*3f89a643SAchiad Shochat 
216*3f89a643SAchiad Shochat 	if (!err)
217*3f89a643SAchiad Shochat 		*sys_image_guid = cpu_to_be64(tmp);
218*3f89a643SAchiad Shochat 
219*3f89a643SAchiad Shochat 	return err;
220*3f89a643SAchiad Shochat 
2211b5daf11SMajd Dibbiny }
2221b5daf11SMajd Dibbiny 
2231b5daf11SMajd Dibbiny static int mlx5_query_max_pkeys(struct ib_device *ibdev,
2241b5daf11SMajd Dibbiny 				u16 *max_pkeys)
2251b5daf11SMajd Dibbiny {
2261b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
2271b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
2281b5daf11SMajd Dibbiny 
2291b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
2301b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
2311b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_max_pkeys(ibdev, max_pkeys);
2321b5daf11SMajd Dibbiny 
2331b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
2341b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
2351b5daf11SMajd Dibbiny 		*max_pkeys = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev,
2361b5daf11SMajd Dibbiny 						pkey_table_size));
2371b5daf11SMajd Dibbiny 		return 0;
2381b5daf11SMajd Dibbiny 
2391b5daf11SMajd Dibbiny 	default:
2401b5daf11SMajd Dibbiny 		return -EINVAL;
2411b5daf11SMajd Dibbiny 	}
2421b5daf11SMajd Dibbiny }
2431b5daf11SMajd Dibbiny 
2441b5daf11SMajd Dibbiny static int mlx5_query_vendor_id(struct ib_device *ibdev,
2451b5daf11SMajd Dibbiny 				u32 *vendor_id)
2461b5daf11SMajd Dibbiny {
2471b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
2481b5daf11SMajd Dibbiny 
2491b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
2501b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
2511b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_vendor_id(ibdev, vendor_id);
2521b5daf11SMajd Dibbiny 
2531b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
2541b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
2551b5daf11SMajd Dibbiny 		return mlx5_core_query_vendor_id(dev->mdev, vendor_id);
2561b5daf11SMajd Dibbiny 
2571b5daf11SMajd Dibbiny 	default:
2581b5daf11SMajd Dibbiny 		return -EINVAL;
2591b5daf11SMajd Dibbiny 	}
2601b5daf11SMajd Dibbiny }
2611b5daf11SMajd Dibbiny 
2621b5daf11SMajd Dibbiny static int mlx5_query_node_guid(struct mlx5_ib_dev *dev,
2631b5daf11SMajd Dibbiny 				__be64 *node_guid)
2641b5daf11SMajd Dibbiny {
2651b5daf11SMajd Dibbiny 	u64 tmp;
2661b5daf11SMajd Dibbiny 	int err;
2671b5daf11SMajd Dibbiny 
2681b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(&dev->ib_dev)) {
2691b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
2701b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_node_guid(dev, node_guid);
2711b5daf11SMajd Dibbiny 
2721b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
2731b5daf11SMajd Dibbiny 		err = mlx5_query_hca_vport_node_guid(dev->mdev, &tmp);
274*3f89a643SAchiad Shochat 		break;
275*3f89a643SAchiad Shochat 
276*3f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
277*3f89a643SAchiad Shochat 		err = mlx5_query_nic_vport_node_guid(dev->mdev, &tmp);
278*3f89a643SAchiad Shochat 		break;
2791b5daf11SMajd Dibbiny 
2801b5daf11SMajd Dibbiny 	default:
2811b5daf11SMajd Dibbiny 		return -EINVAL;
2821b5daf11SMajd Dibbiny 	}
283*3f89a643SAchiad Shochat 
284*3f89a643SAchiad Shochat 	if (!err)
285*3f89a643SAchiad Shochat 		*node_guid = cpu_to_be64(tmp);
286*3f89a643SAchiad Shochat 
287*3f89a643SAchiad Shochat 	return err;
2881b5daf11SMajd Dibbiny }
2891b5daf11SMajd Dibbiny 
2901b5daf11SMajd Dibbiny struct mlx5_reg_node_desc {
2911b5daf11SMajd Dibbiny 	u8	desc[64];
2921b5daf11SMajd Dibbiny };
2931b5daf11SMajd Dibbiny 
2941b5daf11SMajd Dibbiny static int mlx5_query_node_desc(struct mlx5_ib_dev *dev, char *node_desc)
2951b5daf11SMajd Dibbiny {
2961b5daf11SMajd Dibbiny 	struct mlx5_reg_node_desc in;
2971b5daf11SMajd Dibbiny 
2981b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(dev))
2991b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_node_desc(dev, node_desc);
3001b5daf11SMajd Dibbiny 
3011b5daf11SMajd Dibbiny 	memset(&in, 0, sizeof(in));
3021b5daf11SMajd Dibbiny 
3031b5daf11SMajd Dibbiny 	return mlx5_core_access_reg(dev->mdev, &in, sizeof(in), node_desc,
3041b5daf11SMajd Dibbiny 				    sizeof(struct mlx5_reg_node_desc),
3051b5daf11SMajd Dibbiny 				    MLX5_REG_NODE_DESC, 0, 0);
3061b5daf11SMajd Dibbiny }
3071b5daf11SMajd Dibbiny 
308e126ba97SEli Cohen static int mlx5_ib_query_device(struct ib_device *ibdev,
3092528e33eSMatan Barak 				struct ib_device_attr *props,
3102528e33eSMatan Barak 				struct ib_udata *uhw)
311e126ba97SEli Cohen {
312e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
313938fe83cSSaeed Mahameed 	struct mlx5_core_dev *mdev = dev->mdev;
314e126ba97SEli Cohen 	int err = -ENOMEM;
315e126ba97SEli Cohen 	int max_rq_sg;
316e126ba97SEli Cohen 	int max_sq_sg;
317e0238a6aSSagi Grimberg 	u64 min_page_size = 1ull << MLX5_CAP_GEN(mdev, log_pg_sz);
318e126ba97SEli Cohen 
3192528e33eSMatan Barak 	if (uhw->inlen || uhw->outlen)
3202528e33eSMatan Barak 		return -EINVAL;
3212528e33eSMatan Barak 
322e126ba97SEli Cohen 	memset(props, 0, sizeof(*props));
3231b5daf11SMajd Dibbiny 	err = mlx5_query_system_image_guid(ibdev,
3241b5daf11SMajd Dibbiny 					   &props->sys_image_guid);
3251b5daf11SMajd Dibbiny 	if (err)
3261b5daf11SMajd Dibbiny 		return err;
3271b5daf11SMajd Dibbiny 
3281b5daf11SMajd Dibbiny 	err = mlx5_query_max_pkeys(ibdev, &props->max_pkeys);
3291b5daf11SMajd Dibbiny 	if (err)
3301b5daf11SMajd Dibbiny 		return err;
3311b5daf11SMajd Dibbiny 
3321b5daf11SMajd Dibbiny 	err = mlx5_query_vendor_id(ibdev, &props->vendor_id);
3331b5daf11SMajd Dibbiny 	if (err)
3341b5daf11SMajd Dibbiny 		return err;
335e126ba97SEli Cohen 
3369603b61dSJack Morgenstein 	props->fw_ver = ((u64)fw_rev_maj(dev->mdev) << 32) |
3379603b61dSJack Morgenstein 		(fw_rev_min(dev->mdev) << 16) |
3389603b61dSJack Morgenstein 		fw_rev_sub(dev->mdev);
339e126ba97SEli Cohen 	props->device_cap_flags    = IB_DEVICE_CHANGE_PHY_PORT |
340e126ba97SEli Cohen 		IB_DEVICE_PORT_ACTIVE_EVENT		|
341e126ba97SEli Cohen 		IB_DEVICE_SYS_IMAGE_GUID		|
3421a4c3a3dSEli Cohen 		IB_DEVICE_RC_RNR_NAK_GEN;
343938fe83cSSaeed Mahameed 
344938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, pkv))
345e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
346938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, qkv))
347e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
348938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, apm))
349e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
350938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, xrc))
351e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_XRC;
352e126ba97SEli Cohen 	props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
353938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, sho)) {
3542dea9094SSagi Grimberg 		props->device_cap_flags |= IB_DEVICE_SIGNATURE_HANDOVER;
3552dea9094SSagi Grimberg 		/* At this stage no support for signature handover */
3562dea9094SSagi Grimberg 		props->sig_prot_cap = IB_PROT_T10DIF_TYPE_1 |
3572dea9094SSagi Grimberg 				      IB_PROT_T10DIF_TYPE_2 |
3582dea9094SSagi Grimberg 				      IB_PROT_T10DIF_TYPE_3;
3592dea9094SSagi Grimberg 		props->sig_guard_cap = IB_GUARD_T10DIF_CRC |
3602dea9094SSagi Grimberg 				       IB_GUARD_T10DIF_CSUM;
3612dea9094SSagi Grimberg 	}
362938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, block_lb_mc))
363f360d88aSEli Cohen 		props->device_cap_flags |= IB_DEVICE_BLOCK_MULTICAST_LOOPBACK;
364e126ba97SEli Cohen 
3651b5daf11SMajd Dibbiny 	props->vendor_part_id	   = mdev->pdev->device;
3661b5daf11SMajd Dibbiny 	props->hw_ver		   = mdev->pdev->revision;
367e126ba97SEli Cohen 
368e126ba97SEli Cohen 	props->max_mr_size	   = ~0ull;
369e0238a6aSSagi Grimberg 	props->page_size_cap	   = ~(min_page_size - 1);
370938fe83cSSaeed Mahameed 	props->max_qp		   = 1 << MLX5_CAP_GEN(mdev, log_max_qp);
371938fe83cSSaeed Mahameed 	props->max_qp_wr	   = 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
372938fe83cSSaeed Mahameed 	max_rq_sg =  MLX5_CAP_GEN(mdev, max_wqe_sz_rq) /
373938fe83cSSaeed Mahameed 		     sizeof(struct mlx5_wqe_data_seg);
374938fe83cSSaeed Mahameed 	max_sq_sg = (MLX5_CAP_GEN(mdev, max_wqe_sz_sq) -
375938fe83cSSaeed Mahameed 		     sizeof(struct mlx5_wqe_ctrl_seg)) /
376e126ba97SEli Cohen 		     sizeof(struct mlx5_wqe_data_seg);
377e126ba97SEli Cohen 	props->max_sge = min(max_rq_sg, max_sq_sg);
37818ebd407SSagi Grimberg 	props->max_sge_rd = props->max_sge;
379938fe83cSSaeed Mahameed 	props->max_cq		   = 1 << MLX5_CAP_GEN(mdev, log_max_cq);
380938fe83cSSaeed Mahameed 	props->max_cqe = (1 << MLX5_CAP_GEN(mdev, log_max_eq_sz)) - 1;
381938fe83cSSaeed Mahameed 	props->max_mr		   = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
382938fe83cSSaeed Mahameed 	props->max_pd		   = 1 << MLX5_CAP_GEN(mdev, log_max_pd);
383938fe83cSSaeed Mahameed 	props->max_qp_rd_atom	   = 1 << MLX5_CAP_GEN(mdev, log_max_ra_req_qp);
384938fe83cSSaeed Mahameed 	props->max_qp_init_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_res_qp);
385938fe83cSSaeed Mahameed 	props->max_srq		   = 1 << MLX5_CAP_GEN(mdev, log_max_srq);
386938fe83cSSaeed Mahameed 	props->max_srq_wr = (1 << MLX5_CAP_GEN(mdev, log_max_srq_sz)) - 1;
387938fe83cSSaeed Mahameed 	props->local_ca_ack_delay  = MLX5_CAP_GEN(mdev, local_ca_ack_delay);
388e126ba97SEli Cohen 	props->max_res_rd_atom	   = props->max_qp_rd_atom * props->max_qp;
389e126ba97SEli Cohen 	props->max_srq_sge	   = max_rq_sg - 1;
390e126ba97SEli Cohen 	props->max_fast_reg_page_list_len = (unsigned int)-1;
39181bea28fSEli Cohen 	props->atomic_cap	   = IB_ATOMIC_NONE;
39281bea28fSEli Cohen 	props->masked_atomic_cap   = IB_ATOMIC_NONE;
393938fe83cSSaeed Mahameed 	props->max_mcast_grp	   = 1 << MLX5_CAP_GEN(mdev, log_max_mcg);
394938fe83cSSaeed Mahameed 	props->max_mcast_qp_attach = MLX5_CAP_GEN(mdev, max_qp_mcg);
395e126ba97SEli Cohen 	props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
396e126ba97SEli Cohen 					   props->max_mcast_grp;
397e126ba97SEli Cohen 	props->max_map_per_fmr = INT_MAX; /* no limit in ConnectIB */
398e126ba97SEli Cohen 
3998cdd312cSHaggai Eran #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
400938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, pg))
4018cdd312cSHaggai Eran 		props->device_cap_flags |= IB_DEVICE_ON_DEMAND_PAGING;
4028cdd312cSHaggai Eran 	props->odp_caps = dev->odp_caps;
4038cdd312cSHaggai Eran #endif
4048cdd312cSHaggai Eran 
4051b5daf11SMajd Dibbiny 	return 0;
4061b5daf11SMajd Dibbiny }
407e126ba97SEli Cohen 
4081b5daf11SMajd Dibbiny enum mlx5_ib_width {
4091b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_1X	= 1 << 0,
4101b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_2X	= 1 << 1,
4111b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_4X	= 1 << 2,
4121b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_8X	= 1 << 3,
4131b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_12X	= 1 << 4
4141b5daf11SMajd Dibbiny };
4151b5daf11SMajd Dibbiny 
4161b5daf11SMajd Dibbiny static int translate_active_width(struct ib_device *ibdev, u8 active_width,
4171b5daf11SMajd Dibbiny 				  u8 *ib_width)
4181b5daf11SMajd Dibbiny {
4191b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
4201b5daf11SMajd Dibbiny 	int err = 0;
4211b5daf11SMajd Dibbiny 
4221b5daf11SMajd Dibbiny 	if (active_width & MLX5_IB_WIDTH_1X) {
4231b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_1X;
4241b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_2X) {
4251b5daf11SMajd Dibbiny 		mlx5_ib_dbg(dev, "active_width %d is not supported by IB spec\n",
4261b5daf11SMajd Dibbiny 			    (int)active_width);
4271b5daf11SMajd Dibbiny 		err = -EINVAL;
4281b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_4X) {
4291b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_4X;
4301b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_8X) {
4311b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_8X;
4321b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_12X) {
4331b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_12X;
4341b5daf11SMajd Dibbiny 	} else {
4351b5daf11SMajd Dibbiny 		mlx5_ib_dbg(dev, "Invalid active_width %d\n",
4361b5daf11SMajd Dibbiny 			    (int)active_width);
4371b5daf11SMajd Dibbiny 		err = -EINVAL;
4381b5daf11SMajd Dibbiny 	}
4391b5daf11SMajd Dibbiny 
4401b5daf11SMajd Dibbiny 	return err;
4411b5daf11SMajd Dibbiny }
4421b5daf11SMajd Dibbiny 
4431b5daf11SMajd Dibbiny static int mlx5_mtu_to_ib_mtu(int mtu)
4441b5daf11SMajd Dibbiny {
4451b5daf11SMajd Dibbiny 	switch (mtu) {
4461b5daf11SMajd Dibbiny 	case 256: return 1;
4471b5daf11SMajd Dibbiny 	case 512: return 2;
4481b5daf11SMajd Dibbiny 	case 1024: return 3;
4491b5daf11SMajd Dibbiny 	case 2048: return 4;
4501b5daf11SMajd Dibbiny 	case 4096: return 5;
4511b5daf11SMajd Dibbiny 	default:
4521b5daf11SMajd Dibbiny 		pr_warn("invalid mtu\n");
4531b5daf11SMajd Dibbiny 		return -1;
4541b5daf11SMajd Dibbiny 	}
4551b5daf11SMajd Dibbiny }
4561b5daf11SMajd Dibbiny 
4571b5daf11SMajd Dibbiny enum ib_max_vl_num {
4581b5daf11SMajd Dibbiny 	__IB_MAX_VL_0		= 1,
4591b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_1		= 2,
4601b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_3		= 3,
4611b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_7		= 4,
4621b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_14	= 5,
4631b5daf11SMajd Dibbiny };
4641b5daf11SMajd Dibbiny 
4651b5daf11SMajd Dibbiny enum mlx5_vl_hw_cap {
4661b5daf11SMajd Dibbiny 	MLX5_VL_HW_0	= 1,
4671b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_1	= 2,
4681b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_2	= 3,
4691b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_3	= 4,
4701b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_4	= 5,
4711b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_5	= 6,
4721b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_6	= 7,
4731b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_7	= 8,
4741b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_14	= 15
4751b5daf11SMajd Dibbiny };
4761b5daf11SMajd Dibbiny 
4771b5daf11SMajd Dibbiny static int translate_max_vl_num(struct ib_device *ibdev, u8 vl_hw_cap,
4781b5daf11SMajd Dibbiny 				u8 *max_vl_num)
4791b5daf11SMajd Dibbiny {
4801b5daf11SMajd Dibbiny 	switch (vl_hw_cap) {
4811b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0:
4821b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0;
4831b5daf11SMajd Dibbiny 		break;
4841b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_1:
4851b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_1;
4861b5daf11SMajd Dibbiny 		break;
4871b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_3:
4881b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_3;
4891b5daf11SMajd Dibbiny 		break;
4901b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_7:
4911b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_7;
4921b5daf11SMajd Dibbiny 		break;
4931b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_14:
4941b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_14;
4951b5daf11SMajd Dibbiny 		break;
4961b5daf11SMajd Dibbiny 
4971b5daf11SMajd Dibbiny 	default:
4981b5daf11SMajd Dibbiny 		return -EINVAL;
4991b5daf11SMajd Dibbiny 	}
5001b5daf11SMajd Dibbiny 
5011b5daf11SMajd Dibbiny 	return 0;
5021b5daf11SMajd Dibbiny }
5031b5daf11SMajd Dibbiny 
5041b5daf11SMajd Dibbiny static int mlx5_query_hca_port(struct ib_device *ibdev, u8 port,
5051b5daf11SMajd Dibbiny 			       struct ib_port_attr *props)
5061b5daf11SMajd Dibbiny {
5071b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
5081b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
5091b5daf11SMajd Dibbiny 	struct mlx5_hca_vport_context *rep;
5101b5daf11SMajd Dibbiny 	int max_mtu;
5111b5daf11SMajd Dibbiny 	int oper_mtu;
5121b5daf11SMajd Dibbiny 	int err;
5131b5daf11SMajd Dibbiny 	u8 ib_link_width_oper;
5141b5daf11SMajd Dibbiny 	u8 vl_hw_cap;
5151b5daf11SMajd Dibbiny 
5161b5daf11SMajd Dibbiny 	rep = kzalloc(sizeof(*rep), GFP_KERNEL);
5171b5daf11SMajd Dibbiny 	if (!rep) {
5181b5daf11SMajd Dibbiny 		err = -ENOMEM;
5191b5daf11SMajd Dibbiny 		goto out;
5201b5daf11SMajd Dibbiny 	}
5211b5daf11SMajd Dibbiny 
5221b5daf11SMajd Dibbiny 	memset(props, 0, sizeof(*props));
5231b5daf11SMajd Dibbiny 
5241b5daf11SMajd Dibbiny 	err = mlx5_query_hca_vport_context(mdev, 0, port, 0, rep);
5251b5daf11SMajd Dibbiny 	if (err)
5261b5daf11SMajd Dibbiny 		goto out;
5271b5daf11SMajd Dibbiny 
5281b5daf11SMajd Dibbiny 	props->lid		= rep->lid;
5291b5daf11SMajd Dibbiny 	props->lmc		= rep->lmc;
5301b5daf11SMajd Dibbiny 	props->sm_lid		= rep->sm_lid;
5311b5daf11SMajd Dibbiny 	props->sm_sl		= rep->sm_sl;
5321b5daf11SMajd Dibbiny 	props->state		= rep->vport_state;
5331b5daf11SMajd Dibbiny 	props->phys_state	= rep->port_physical_state;
5341b5daf11SMajd Dibbiny 	props->port_cap_flags	= rep->cap_mask1;
5351b5daf11SMajd Dibbiny 	props->gid_tbl_len	= mlx5_get_gid_table_len(MLX5_CAP_GEN(mdev, gid_table_size));
5361b5daf11SMajd Dibbiny 	props->max_msg_sz	= 1 << MLX5_CAP_GEN(mdev, log_max_msg);
5371b5daf11SMajd Dibbiny 	props->pkey_tbl_len	= mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, pkey_table_size));
5381b5daf11SMajd Dibbiny 	props->bad_pkey_cntr	= rep->pkey_violation_counter;
5391b5daf11SMajd Dibbiny 	props->qkey_viol_cntr	= rep->qkey_violation_counter;
5401b5daf11SMajd Dibbiny 	props->subnet_timeout	= rep->subnet_timeout;
5411b5daf11SMajd Dibbiny 	props->init_type_reply	= rep->init_type_reply;
5421b5daf11SMajd Dibbiny 
5431b5daf11SMajd Dibbiny 	err = mlx5_query_port_link_width_oper(mdev, &ib_link_width_oper, port);
5441b5daf11SMajd Dibbiny 	if (err)
5451b5daf11SMajd Dibbiny 		goto out;
5461b5daf11SMajd Dibbiny 
5471b5daf11SMajd Dibbiny 	err = translate_active_width(ibdev, ib_link_width_oper,
5481b5daf11SMajd Dibbiny 				     &props->active_width);
5491b5daf11SMajd Dibbiny 	if (err)
5501b5daf11SMajd Dibbiny 		goto out;
5511b5daf11SMajd Dibbiny 	err = mlx5_query_port_proto_oper(mdev, &props->active_speed, MLX5_PTYS_IB,
5521b5daf11SMajd Dibbiny 					 port);
5531b5daf11SMajd Dibbiny 	if (err)
5541b5daf11SMajd Dibbiny 		goto out;
5551b5daf11SMajd Dibbiny 
556facc9699SSaeed Mahameed 	mlx5_query_port_max_mtu(mdev, &max_mtu, port);
5571b5daf11SMajd Dibbiny 
5581b5daf11SMajd Dibbiny 	props->max_mtu = mlx5_mtu_to_ib_mtu(max_mtu);
5591b5daf11SMajd Dibbiny 
560facc9699SSaeed Mahameed 	mlx5_query_port_oper_mtu(mdev, &oper_mtu, port);
5611b5daf11SMajd Dibbiny 
5621b5daf11SMajd Dibbiny 	props->active_mtu = mlx5_mtu_to_ib_mtu(oper_mtu);
5631b5daf11SMajd Dibbiny 
5641b5daf11SMajd Dibbiny 	err = mlx5_query_port_vl_hw_cap(mdev, &vl_hw_cap, port);
5651b5daf11SMajd Dibbiny 	if (err)
5661b5daf11SMajd Dibbiny 		goto out;
5671b5daf11SMajd Dibbiny 
5681b5daf11SMajd Dibbiny 	err = translate_max_vl_num(ibdev, vl_hw_cap,
5691b5daf11SMajd Dibbiny 				   &props->max_vl_num);
5701b5daf11SMajd Dibbiny out:
5711b5daf11SMajd Dibbiny 	kfree(rep);
572e126ba97SEli Cohen 	return err;
573e126ba97SEli Cohen }
574e126ba97SEli Cohen 
575e126ba97SEli Cohen int mlx5_ib_query_port(struct ib_device *ibdev, u8 port,
576e126ba97SEli Cohen 		       struct ib_port_attr *props)
577e126ba97SEli Cohen {
5781b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
5791b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
5801b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_port(ibdev, port, props);
581e126ba97SEli Cohen 
5821b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
5831b5daf11SMajd Dibbiny 		return mlx5_query_hca_port(ibdev, port, props);
5841b5daf11SMajd Dibbiny 
585*3f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
586*3f89a643SAchiad Shochat 		return mlx5_query_port_roce(ibdev, port, props);
587*3f89a643SAchiad Shochat 
5881b5daf11SMajd Dibbiny 	default:
589e126ba97SEli Cohen 		return -EINVAL;
590e126ba97SEli Cohen 	}
591e126ba97SEli Cohen }
592e126ba97SEli Cohen 
593e126ba97SEli Cohen static int mlx5_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
594e126ba97SEli Cohen 			     union ib_gid *gid)
595e126ba97SEli Cohen {
5961b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
5971b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
598e126ba97SEli Cohen 
5991b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
6001b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
6011b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_gids(ibdev, port, index, gid);
602e126ba97SEli Cohen 
6031b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
6041b5daf11SMajd Dibbiny 		return mlx5_query_hca_vport_gid(mdev, 0, port, 0, index, gid);
605e126ba97SEli Cohen 
6061b5daf11SMajd Dibbiny 	default:
6071b5daf11SMajd Dibbiny 		return -EINVAL;
6081b5daf11SMajd Dibbiny 	}
609e126ba97SEli Cohen 
610e126ba97SEli Cohen }
611e126ba97SEli Cohen 
612e126ba97SEli Cohen static int mlx5_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
613e126ba97SEli Cohen 			      u16 *pkey)
614e126ba97SEli Cohen {
6151b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
6161b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
617e126ba97SEli Cohen 
6181b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
6191b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
6201b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_pkey(ibdev, port, index, pkey);
621e126ba97SEli Cohen 
6221b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
6231b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
6241b5daf11SMajd Dibbiny 		return mlx5_query_hca_vport_pkey(mdev, 0, port,  0, index,
6251b5daf11SMajd Dibbiny 						 pkey);
6261b5daf11SMajd Dibbiny 	default:
6271b5daf11SMajd Dibbiny 		return -EINVAL;
628e126ba97SEli Cohen 	}
6291b5daf11SMajd Dibbiny }
630e126ba97SEli Cohen 
631e126ba97SEli Cohen static int mlx5_ib_modify_device(struct ib_device *ibdev, int mask,
632e126ba97SEli Cohen 				 struct ib_device_modify *props)
633e126ba97SEli Cohen {
634e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
635e126ba97SEli Cohen 	struct mlx5_reg_node_desc in;
636e126ba97SEli Cohen 	struct mlx5_reg_node_desc out;
637e126ba97SEli Cohen 	int err;
638e126ba97SEli Cohen 
639e126ba97SEli Cohen 	if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
640e126ba97SEli Cohen 		return -EOPNOTSUPP;
641e126ba97SEli Cohen 
642e126ba97SEli Cohen 	if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
643e126ba97SEli Cohen 		return 0;
644e126ba97SEli Cohen 
645e126ba97SEli Cohen 	/*
646e126ba97SEli Cohen 	 * If possible, pass node desc to FW, so it can generate
647e126ba97SEli Cohen 	 * a 144 trap.  If cmd fails, just ignore.
648e126ba97SEli Cohen 	 */
649e126ba97SEli Cohen 	memcpy(&in, props->node_desc, 64);
6509603b61dSJack Morgenstein 	err = mlx5_core_access_reg(dev->mdev, &in, sizeof(in), &out,
651e126ba97SEli Cohen 				   sizeof(out), MLX5_REG_NODE_DESC, 0, 1);
652e126ba97SEli Cohen 	if (err)
653e126ba97SEli Cohen 		return err;
654e126ba97SEli Cohen 
655e126ba97SEli Cohen 	memcpy(ibdev->node_desc, props->node_desc, 64);
656e126ba97SEli Cohen 
657e126ba97SEli Cohen 	return err;
658e126ba97SEli Cohen }
659e126ba97SEli Cohen 
660e126ba97SEli Cohen static int mlx5_ib_modify_port(struct ib_device *ibdev, u8 port, int mask,
661e126ba97SEli Cohen 			       struct ib_port_modify *props)
662e126ba97SEli Cohen {
663e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
664e126ba97SEli Cohen 	struct ib_port_attr attr;
665e126ba97SEli Cohen 	u32 tmp;
666e126ba97SEli Cohen 	int err;
667e126ba97SEli Cohen 
668e126ba97SEli Cohen 	mutex_lock(&dev->cap_mask_mutex);
669e126ba97SEli Cohen 
670e126ba97SEli Cohen 	err = mlx5_ib_query_port(ibdev, port, &attr);
671e126ba97SEli Cohen 	if (err)
672e126ba97SEli Cohen 		goto out;
673e126ba97SEli Cohen 
674e126ba97SEli Cohen 	tmp = (attr.port_cap_flags | props->set_port_cap_mask) &
675e126ba97SEli Cohen 		~props->clr_port_cap_mask;
676e126ba97SEli Cohen 
6779603b61dSJack Morgenstein 	err = mlx5_set_port_caps(dev->mdev, port, tmp);
678e126ba97SEli Cohen 
679e126ba97SEli Cohen out:
680e126ba97SEli Cohen 	mutex_unlock(&dev->cap_mask_mutex);
681e126ba97SEli Cohen 	return err;
682e126ba97SEli Cohen }
683e126ba97SEli Cohen 
684e126ba97SEli Cohen static struct ib_ucontext *mlx5_ib_alloc_ucontext(struct ib_device *ibdev,
685e126ba97SEli Cohen 						  struct ib_udata *udata)
686e126ba97SEli Cohen {
687e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
68878c0f98cSEli Cohen 	struct mlx5_ib_alloc_ucontext_req_v2 req;
689e126ba97SEli Cohen 	struct mlx5_ib_alloc_ucontext_resp resp;
690e126ba97SEli Cohen 	struct mlx5_ib_ucontext *context;
691e126ba97SEli Cohen 	struct mlx5_uuar_info *uuari;
692e126ba97SEli Cohen 	struct mlx5_uar *uars;
693c1be5232SEli Cohen 	int gross_uuars;
694e126ba97SEli Cohen 	int num_uars;
69578c0f98cSEli Cohen 	int ver;
696e126ba97SEli Cohen 	int uuarn;
697e126ba97SEli Cohen 	int err;
698e126ba97SEli Cohen 	int i;
699f241e749SJack Morgenstein 	size_t reqlen;
700e126ba97SEli Cohen 
701e126ba97SEli Cohen 	if (!dev->ib_active)
702e126ba97SEli Cohen 		return ERR_PTR(-EAGAIN);
703e126ba97SEli Cohen 
70478c0f98cSEli Cohen 	memset(&req, 0, sizeof(req));
70578c0f98cSEli Cohen 	reqlen = udata->inlen - sizeof(struct ib_uverbs_cmd_hdr);
70678c0f98cSEli Cohen 	if (reqlen == sizeof(struct mlx5_ib_alloc_ucontext_req))
70778c0f98cSEli Cohen 		ver = 0;
70878c0f98cSEli Cohen 	else if (reqlen == sizeof(struct mlx5_ib_alloc_ucontext_req_v2))
70978c0f98cSEli Cohen 		ver = 2;
71078c0f98cSEli Cohen 	else
71178c0f98cSEli Cohen 		return ERR_PTR(-EINVAL);
71278c0f98cSEli Cohen 
71378c0f98cSEli Cohen 	err = ib_copy_from_udata(&req, udata, reqlen);
714e126ba97SEli Cohen 	if (err)
715e126ba97SEli Cohen 		return ERR_PTR(err);
716e126ba97SEli Cohen 
71778c0f98cSEli Cohen 	if (req.flags || req.reserved)
71878c0f98cSEli Cohen 		return ERR_PTR(-EINVAL);
71978c0f98cSEli Cohen 
720e126ba97SEli Cohen 	if (req.total_num_uuars > MLX5_MAX_UUARS)
721e126ba97SEli Cohen 		return ERR_PTR(-ENOMEM);
722e126ba97SEli Cohen 
723e126ba97SEli Cohen 	if (req.total_num_uuars == 0)
724e126ba97SEli Cohen 		return ERR_PTR(-EINVAL);
725e126ba97SEli Cohen 
726c1be5232SEli Cohen 	req.total_num_uuars = ALIGN(req.total_num_uuars,
727c1be5232SEli Cohen 				    MLX5_NON_FP_BF_REGS_PER_PAGE);
728e126ba97SEli Cohen 	if (req.num_low_latency_uuars > req.total_num_uuars - 1)
729e126ba97SEli Cohen 		return ERR_PTR(-EINVAL);
730e126ba97SEli Cohen 
731c1be5232SEli Cohen 	num_uars = req.total_num_uuars / MLX5_NON_FP_BF_REGS_PER_PAGE;
732c1be5232SEli Cohen 	gross_uuars = num_uars * MLX5_BF_REGS_PER_PAGE;
733938fe83cSSaeed Mahameed 	resp.qp_tab_size = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp);
734938fe83cSSaeed Mahameed 	resp.bf_reg_size = 1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size);
735e126ba97SEli Cohen 	resp.cache_line_size = L1_CACHE_BYTES;
736938fe83cSSaeed Mahameed 	resp.max_sq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq);
737938fe83cSSaeed Mahameed 	resp.max_rq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq);
738938fe83cSSaeed Mahameed 	resp.max_send_wqebb = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
739938fe83cSSaeed Mahameed 	resp.max_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
740938fe83cSSaeed Mahameed 	resp.max_srq_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz);
741e126ba97SEli Cohen 
742e126ba97SEli Cohen 	context = kzalloc(sizeof(*context), GFP_KERNEL);
743e126ba97SEli Cohen 	if (!context)
744e126ba97SEli Cohen 		return ERR_PTR(-ENOMEM);
745e126ba97SEli Cohen 
746e126ba97SEli Cohen 	uuari = &context->uuari;
747e126ba97SEli Cohen 	mutex_init(&uuari->lock);
748e126ba97SEli Cohen 	uars = kcalloc(num_uars, sizeof(*uars), GFP_KERNEL);
749e126ba97SEli Cohen 	if (!uars) {
750e126ba97SEli Cohen 		err = -ENOMEM;
751e126ba97SEli Cohen 		goto out_ctx;
752e126ba97SEli Cohen 	}
753e126ba97SEli Cohen 
754c1be5232SEli Cohen 	uuari->bitmap = kcalloc(BITS_TO_LONGS(gross_uuars),
755e126ba97SEli Cohen 				sizeof(*uuari->bitmap),
756e126ba97SEli Cohen 				GFP_KERNEL);
757e126ba97SEli Cohen 	if (!uuari->bitmap) {
758e126ba97SEli Cohen 		err = -ENOMEM;
759e126ba97SEli Cohen 		goto out_uar_ctx;
760e126ba97SEli Cohen 	}
761e126ba97SEli Cohen 	/*
762e126ba97SEli Cohen 	 * clear all fast path uuars
763e126ba97SEli Cohen 	 */
764c1be5232SEli Cohen 	for (i = 0; i < gross_uuars; i++) {
765e126ba97SEli Cohen 		uuarn = i & 3;
766e126ba97SEli Cohen 		if (uuarn == 2 || uuarn == 3)
767e126ba97SEli Cohen 			set_bit(i, uuari->bitmap);
768e126ba97SEli Cohen 	}
769e126ba97SEli Cohen 
770c1be5232SEli Cohen 	uuari->count = kcalloc(gross_uuars, sizeof(*uuari->count), GFP_KERNEL);
771e126ba97SEli Cohen 	if (!uuari->count) {
772e126ba97SEli Cohen 		err = -ENOMEM;
773e126ba97SEli Cohen 		goto out_bitmap;
774e126ba97SEli Cohen 	}
775e126ba97SEli Cohen 
776e126ba97SEli Cohen 	for (i = 0; i < num_uars; i++) {
7779603b61dSJack Morgenstein 		err = mlx5_cmd_alloc_uar(dev->mdev, &uars[i].index);
778e126ba97SEli Cohen 		if (err)
779e126ba97SEli Cohen 			goto out_count;
780e126ba97SEli Cohen 	}
781e126ba97SEli Cohen 
782b4cfe447SHaggai Eran #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
783b4cfe447SHaggai Eran 	context->ibucontext.invalidate_range = &mlx5_ib_invalidate_range;
784b4cfe447SHaggai Eran #endif
785b4cfe447SHaggai Eran 
786e126ba97SEli Cohen 	INIT_LIST_HEAD(&context->db_page_list);
787e126ba97SEli Cohen 	mutex_init(&context->db_page_mutex);
788e126ba97SEli Cohen 
789e126ba97SEli Cohen 	resp.tot_uuars = req.total_num_uuars;
790938fe83cSSaeed Mahameed 	resp.num_ports = MLX5_CAP_GEN(dev->mdev, num_ports);
79192b0ca7cSDan Carpenter 	err = ib_copy_to_udata(udata, &resp,
79292b0ca7cSDan Carpenter 			       sizeof(resp) - sizeof(resp.reserved));
793e126ba97SEli Cohen 	if (err)
794e126ba97SEli Cohen 		goto out_uars;
795e126ba97SEli Cohen 
79678c0f98cSEli Cohen 	uuari->ver = ver;
797e126ba97SEli Cohen 	uuari->num_low_latency_uuars = req.num_low_latency_uuars;
798e126ba97SEli Cohen 	uuari->uars = uars;
799e126ba97SEli Cohen 	uuari->num_uars = num_uars;
800e126ba97SEli Cohen 	return &context->ibucontext;
801e126ba97SEli Cohen 
802e126ba97SEli Cohen out_uars:
803e126ba97SEli Cohen 	for (i--; i >= 0; i--)
8049603b61dSJack Morgenstein 		mlx5_cmd_free_uar(dev->mdev, uars[i].index);
805e126ba97SEli Cohen out_count:
806e126ba97SEli Cohen 	kfree(uuari->count);
807e126ba97SEli Cohen 
808e126ba97SEli Cohen out_bitmap:
809e126ba97SEli Cohen 	kfree(uuari->bitmap);
810e126ba97SEli Cohen 
811e126ba97SEli Cohen out_uar_ctx:
812e126ba97SEli Cohen 	kfree(uars);
813e126ba97SEli Cohen 
814e126ba97SEli Cohen out_ctx:
815e126ba97SEli Cohen 	kfree(context);
816e126ba97SEli Cohen 	return ERR_PTR(err);
817e126ba97SEli Cohen }
818e126ba97SEli Cohen 
819e126ba97SEli Cohen static int mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
820e126ba97SEli Cohen {
821e126ba97SEli Cohen 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
822e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
823e126ba97SEli Cohen 	struct mlx5_uuar_info *uuari = &context->uuari;
824e126ba97SEli Cohen 	int i;
825e126ba97SEli Cohen 
826e126ba97SEli Cohen 	for (i = 0; i < uuari->num_uars; i++) {
8279603b61dSJack Morgenstein 		if (mlx5_cmd_free_uar(dev->mdev, uuari->uars[i].index))
828e126ba97SEli Cohen 			mlx5_ib_warn(dev, "failed to free UAR 0x%x\n", uuari->uars[i].index);
829e126ba97SEli Cohen 	}
830e126ba97SEli Cohen 
831e126ba97SEli Cohen 	kfree(uuari->count);
832e126ba97SEli Cohen 	kfree(uuari->bitmap);
833e126ba97SEli Cohen 	kfree(uuari->uars);
834e126ba97SEli Cohen 	kfree(context);
835e126ba97SEli Cohen 
836e126ba97SEli Cohen 	return 0;
837e126ba97SEli Cohen }
838e126ba97SEli Cohen 
839e126ba97SEli Cohen static phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev, int index)
840e126ba97SEli Cohen {
8419603b61dSJack Morgenstein 	return (pci_resource_start(dev->mdev->pdev, 0) >> PAGE_SHIFT) + index;
842e126ba97SEli Cohen }
843e126ba97SEli Cohen 
844e126ba97SEli Cohen static int get_command(unsigned long offset)
845e126ba97SEli Cohen {
846e126ba97SEli Cohen 	return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK;
847e126ba97SEli Cohen }
848e126ba97SEli Cohen 
849e126ba97SEli Cohen static int get_arg(unsigned long offset)
850e126ba97SEli Cohen {
851e126ba97SEli Cohen 	return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1);
852e126ba97SEli Cohen }
853e126ba97SEli Cohen 
854e126ba97SEli Cohen static int get_index(unsigned long offset)
855e126ba97SEli Cohen {
856e126ba97SEli Cohen 	return get_arg(offset);
857e126ba97SEli Cohen }
858e126ba97SEli Cohen 
859e126ba97SEli Cohen static int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma)
860e126ba97SEli Cohen {
861e126ba97SEli Cohen 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
862e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
863e126ba97SEli Cohen 	struct mlx5_uuar_info *uuari = &context->uuari;
864e126ba97SEli Cohen 	unsigned long command;
865e126ba97SEli Cohen 	unsigned long idx;
866e126ba97SEli Cohen 	phys_addr_t pfn;
867e126ba97SEli Cohen 
868e126ba97SEli Cohen 	command = get_command(vma->vm_pgoff);
869e126ba97SEli Cohen 	switch (command) {
870e126ba97SEli Cohen 	case MLX5_IB_MMAP_REGULAR_PAGE:
871e126ba97SEli Cohen 		if (vma->vm_end - vma->vm_start != PAGE_SIZE)
872e126ba97SEli Cohen 			return -EINVAL;
873e126ba97SEli Cohen 
874e126ba97SEli Cohen 		idx = get_index(vma->vm_pgoff);
8751c3ce90dSEli Cohen 		if (idx >= uuari->num_uars)
8761c3ce90dSEli Cohen 			return -EINVAL;
8771c3ce90dSEli Cohen 
878e126ba97SEli Cohen 		pfn = uar_index2pfn(dev, uuari->uars[idx].index);
879e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn 0x%llx\n", idx,
880e126ba97SEli Cohen 			    (unsigned long long)pfn);
881e126ba97SEli Cohen 
882e126ba97SEli Cohen 		vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
883e126ba97SEli Cohen 		if (io_remap_pfn_range(vma, vma->vm_start, pfn,
884e126ba97SEli Cohen 				       PAGE_SIZE, vma->vm_page_prot))
885e126ba97SEli Cohen 			return -EAGAIN;
886e126ba97SEli Cohen 
887e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "mapped WC at 0x%lx, PA 0x%llx\n",
888e126ba97SEli Cohen 			    vma->vm_start,
889e126ba97SEli Cohen 			    (unsigned long long)pfn << PAGE_SHIFT);
890e126ba97SEli Cohen 		break;
891e126ba97SEli Cohen 
892e126ba97SEli Cohen 	case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES:
893e126ba97SEli Cohen 		return -ENOSYS;
894e126ba97SEli Cohen 
895e126ba97SEli Cohen 	default:
896e126ba97SEli Cohen 		return -EINVAL;
897e126ba97SEli Cohen 	}
898e126ba97SEli Cohen 
899e126ba97SEli Cohen 	return 0;
900e126ba97SEli Cohen }
901e126ba97SEli Cohen 
902e126ba97SEli Cohen static struct ib_pd *mlx5_ib_alloc_pd(struct ib_device *ibdev,
903e126ba97SEli Cohen 				      struct ib_ucontext *context,
904e126ba97SEli Cohen 				      struct ib_udata *udata)
905e126ba97SEli Cohen {
906e126ba97SEli Cohen 	struct mlx5_ib_alloc_pd_resp resp;
907e126ba97SEli Cohen 	struct mlx5_ib_pd *pd;
908e126ba97SEli Cohen 	int err;
909e126ba97SEli Cohen 
910e126ba97SEli Cohen 	pd = kmalloc(sizeof(*pd), GFP_KERNEL);
911e126ba97SEli Cohen 	if (!pd)
912e126ba97SEli Cohen 		return ERR_PTR(-ENOMEM);
913e126ba97SEli Cohen 
9149603b61dSJack Morgenstein 	err = mlx5_core_alloc_pd(to_mdev(ibdev)->mdev, &pd->pdn);
915e126ba97SEli Cohen 	if (err) {
916e126ba97SEli Cohen 		kfree(pd);
917e126ba97SEli Cohen 		return ERR_PTR(err);
918e126ba97SEli Cohen 	}
919e126ba97SEli Cohen 
920e126ba97SEli Cohen 	if (context) {
921e126ba97SEli Cohen 		resp.pdn = pd->pdn;
922e126ba97SEli Cohen 		if (ib_copy_to_udata(udata, &resp, sizeof(resp))) {
9239603b61dSJack Morgenstein 			mlx5_core_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn);
924e126ba97SEli Cohen 			kfree(pd);
925e126ba97SEli Cohen 			return ERR_PTR(-EFAULT);
926e126ba97SEli Cohen 		}
927e126ba97SEli Cohen 	}
928e126ba97SEli Cohen 
929e126ba97SEli Cohen 	return &pd->ibpd;
930e126ba97SEli Cohen }
931e126ba97SEli Cohen 
932e126ba97SEli Cohen static int mlx5_ib_dealloc_pd(struct ib_pd *pd)
933e126ba97SEli Cohen {
934e126ba97SEli Cohen 	struct mlx5_ib_dev *mdev = to_mdev(pd->device);
935e126ba97SEli Cohen 	struct mlx5_ib_pd *mpd = to_mpd(pd);
936e126ba97SEli Cohen 
9379603b61dSJack Morgenstein 	mlx5_core_dealloc_pd(mdev->mdev, mpd->pdn);
938e126ba97SEli Cohen 	kfree(mpd);
939e126ba97SEli Cohen 
940e126ba97SEli Cohen 	return 0;
941e126ba97SEli Cohen }
942e126ba97SEli Cohen 
943e126ba97SEli Cohen static int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
944e126ba97SEli Cohen {
945e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
946e126ba97SEli Cohen 	int err;
947e126ba97SEli Cohen 
9489603b61dSJack Morgenstein 	err = mlx5_core_attach_mcg(dev->mdev, gid, ibqp->qp_num);
949e126ba97SEli Cohen 	if (err)
950e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n",
951e126ba97SEli Cohen 			     ibqp->qp_num, gid->raw);
952e126ba97SEli Cohen 
953e126ba97SEli Cohen 	return err;
954e126ba97SEli Cohen }
955e126ba97SEli Cohen 
956e126ba97SEli Cohen static int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
957e126ba97SEli Cohen {
958e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
959e126ba97SEli Cohen 	int err;
960e126ba97SEli Cohen 
9619603b61dSJack Morgenstein 	err = mlx5_core_detach_mcg(dev->mdev, gid, ibqp->qp_num);
962e126ba97SEli Cohen 	if (err)
963e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n",
964e126ba97SEli Cohen 			     ibqp->qp_num, gid->raw);
965e126ba97SEli Cohen 
966e126ba97SEli Cohen 	return err;
967e126ba97SEli Cohen }
968e126ba97SEli Cohen 
969e126ba97SEli Cohen static int init_node_data(struct mlx5_ib_dev *dev)
970e126ba97SEli Cohen {
9711b5daf11SMajd Dibbiny 	int err;
972e126ba97SEli Cohen 
9731b5daf11SMajd Dibbiny 	err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc);
974e126ba97SEli Cohen 	if (err)
975e126ba97SEli Cohen 		return err;
9761b5daf11SMajd Dibbiny 
9771b5daf11SMajd Dibbiny 	dev->mdev->rev_id = dev->mdev->pdev->revision;
9781b5daf11SMajd Dibbiny 
9791b5daf11SMajd Dibbiny 	return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid);
980e126ba97SEli Cohen }
981e126ba97SEli Cohen 
982e126ba97SEli Cohen static ssize_t show_fw_pages(struct device *device, struct device_attribute *attr,
983e126ba97SEli Cohen 			     char *buf)
984e126ba97SEli Cohen {
985e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
986e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
987e126ba97SEli Cohen 
9889603b61dSJack Morgenstein 	return sprintf(buf, "%d\n", dev->mdev->priv.fw_pages);
989e126ba97SEli Cohen }
990e126ba97SEli Cohen 
991e126ba97SEli Cohen static ssize_t show_reg_pages(struct device *device,
992e126ba97SEli Cohen 			      struct device_attribute *attr, char *buf)
993e126ba97SEli Cohen {
994e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
995e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
996e126ba97SEli Cohen 
9976aec21f6SHaggai Eran 	return sprintf(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages));
998e126ba97SEli Cohen }
999e126ba97SEli Cohen 
1000e126ba97SEli Cohen static ssize_t show_hca(struct device *device, struct device_attribute *attr,
1001e126ba97SEli Cohen 			char *buf)
1002e126ba97SEli Cohen {
1003e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
1004e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
10059603b61dSJack Morgenstein 	return sprintf(buf, "MT%d\n", dev->mdev->pdev->device);
1006e126ba97SEli Cohen }
1007e126ba97SEli Cohen 
1008e126ba97SEli Cohen static ssize_t show_fw_ver(struct device *device, struct device_attribute *attr,
1009e126ba97SEli Cohen 			   char *buf)
1010e126ba97SEli Cohen {
1011e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
1012e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
10139603b61dSJack Morgenstein 	return sprintf(buf, "%d.%d.%d\n", fw_rev_maj(dev->mdev),
10149603b61dSJack Morgenstein 		       fw_rev_min(dev->mdev), fw_rev_sub(dev->mdev));
1015e126ba97SEli Cohen }
1016e126ba97SEli Cohen 
1017e126ba97SEli Cohen static ssize_t show_rev(struct device *device, struct device_attribute *attr,
1018e126ba97SEli Cohen 			char *buf)
1019e126ba97SEli Cohen {
1020e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
1021e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
10229603b61dSJack Morgenstein 	return sprintf(buf, "%x\n", dev->mdev->rev_id);
1023e126ba97SEli Cohen }
1024e126ba97SEli Cohen 
1025e126ba97SEli Cohen static ssize_t show_board(struct device *device, struct device_attribute *attr,
1026e126ba97SEli Cohen 			  char *buf)
1027e126ba97SEli Cohen {
1028e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
1029e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
1030e126ba97SEli Cohen 	return sprintf(buf, "%.*s\n", MLX5_BOARD_ID_LEN,
10319603b61dSJack Morgenstein 		       dev->mdev->board_id);
1032e126ba97SEli Cohen }
1033e126ba97SEli Cohen 
1034e126ba97SEli Cohen static DEVICE_ATTR(hw_rev,   S_IRUGO, show_rev,    NULL);
1035e126ba97SEli Cohen static DEVICE_ATTR(fw_ver,   S_IRUGO, show_fw_ver, NULL);
1036e126ba97SEli Cohen static DEVICE_ATTR(hca_type, S_IRUGO, show_hca,    NULL);
1037e126ba97SEli Cohen static DEVICE_ATTR(board_id, S_IRUGO, show_board,  NULL);
1038e126ba97SEli Cohen static DEVICE_ATTR(fw_pages, S_IRUGO, show_fw_pages, NULL);
1039e126ba97SEli Cohen static DEVICE_ATTR(reg_pages, S_IRUGO, show_reg_pages, NULL);
1040e126ba97SEli Cohen 
1041e126ba97SEli Cohen static struct device_attribute *mlx5_class_attributes[] = {
1042e126ba97SEli Cohen 	&dev_attr_hw_rev,
1043e126ba97SEli Cohen 	&dev_attr_fw_ver,
1044e126ba97SEli Cohen 	&dev_attr_hca_type,
1045e126ba97SEli Cohen 	&dev_attr_board_id,
1046e126ba97SEli Cohen 	&dev_attr_fw_pages,
1047e126ba97SEli Cohen 	&dev_attr_reg_pages,
1048e126ba97SEli Cohen };
1049e126ba97SEli Cohen 
10509603b61dSJack Morgenstein static void mlx5_ib_event(struct mlx5_core_dev *dev, void *context,
10514d2f9bbbSJack Morgenstein 			  enum mlx5_dev_event event, unsigned long param)
1052e126ba97SEli Cohen {
10539603b61dSJack Morgenstein 	struct mlx5_ib_dev *ibdev = (struct mlx5_ib_dev *)context;
1054e126ba97SEli Cohen 	struct ib_event ibev;
10559603b61dSJack Morgenstein 
1056e126ba97SEli Cohen 	u8 port = 0;
1057e126ba97SEli Cohen 
1058e126ba97SEli Cohen 	switch (event) {
1059e126ba97SEli Cohen 	case MLX5_DEV_EVENT_SYS_ERROR:
1060e126ba97SEli Cohen 		ibdev->ib_active = false;
1061e126ba97SEli Cohen 		ibev.event = IB_EVENT_DEVICE_FATAL;
1062e126ba97SEli Cohen 		break;
1063e126ba97SEli Cohen 
1064e126ba97SEli Cohen 	case MLX5_DEV_EVENT_PORT_UP:
1065e126ba97SEli Cohen 		ibev.event = IB_EVENT_PORT_ACTIVE;
10664d2f9bbbSJack Morgenstein 		port = (u8)param;
1067e126ba97SEli Cohen 		break;
1068e126ba97SEli Cohen 
1069e126ba97SEli Cohen 	case MLX5_DEV_EVENT_PORT_DOWN:
1070e126ba97SEli Cohen 		ibev.event = IB_EVENT_PORT_ERR;
10714d2f9bbbSJack Morgenstein 		port = (u8)param;
1072e126ba97SEli Cohen 		break;
1073e126ba97SEli Cohen 
1074e126ba97SEli Cohen 	case MLX5_DEV_EVENT_PORT_INITIALIZED:
1075e126ba97SEli Cohen 		/* not used by ULPs */
1076e126ba97SEli Cohen 		return;
1077e126ba97SEli Cohen 
1078e126ba97SEli Cohen 	case MLX5_DEV_EVENT_LID_CHANGE:
1079e126ba97SEli Cohen 		ibev.event = IB_EVENT_LID_CHANGE;
10804d2f9bbbSJack Morgenstein 		port = (u8)param;
1081e126ba97SEli Cohen 		break;
1082e126ba97SEli Cohen 
1083e126ba97SEli Cohen 	case MLX5_DEV_EVENT_PKEY_CHANGE:
1084e126ba97SEli Cohen 		ibev.event = IB_EVENT_PKEY_CHANGE;
10854d2f9bbbSJack Morgenstein 		port = (u8)param;
1086e126ba97SEli Cohen 		break;
1087e126ba97SEli Cohen 
1088e126ba97SEli Cohen 	case MLX5_DEV_EVENT_GUID_CHANGE:
1089e126ba97SEli Cohen 		ibev.event = IB_EVENT_GID_CHANGE;
10904d2f9bbbSJack Morgenstein 		port = (u8)param;
1091e126ba97SEli Cohen 		break;
1092e126ba97SEli Cohen 
1093e126ba97SEli Cohen 	case MLX5_DEV_EVENT_CLIENT_REREG:
1094e126ba97SEli Cohen 		ibev.event = IB_EVENT_CLIENT_REREGISTER;
10954d2f9bbbSJack Morgenstein 		port = (u8)param;
1096e126ba97SEli Cohen 		break;
1097e126ba97SEli Cohen 	}
1098e126ba97SEli Cohen 
1099e126ba97SEli Cohen 	ibev.device	      = &ibdev->ib_dev;
1100e126ba97SEli Cohen 	ibev.element.port_num = port;
1101e126ba97SEli Cohen 
1102a0c84c32SEli Cohen 	if (port < 1 || port > ibdev->num_ports) {
1103a0c84c32SEli Cohen 		mlx5_ib_warn(ibdev, "warning: event on port %d\n", port);
1104a0c84c32SEli Cohen 		return;
1105a0c84c32SEli Cohen 	}
1106a0c84c32SEli Cohen 
1107e126ba97SEli Cohen 	if (ibdev->ib_active)
1108e126ba97SEli Cohen 		ib_dispatch_event(&ibev);
1109e126ba97SEli Cohen }
1110e126ba97SEli Cohen 
1111e126ba97SEli Cohen static void get_ext_port_caps(struct mlx5_ib_dev *dev)
1112e126ba97SEli Cohen {
1113e126ba97SEli Cohen 	int port;
1114e126ba97SEli Cohen 
1115938fe83cSSaeed Mahameed 	for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++)
1116e126ba97SEli Cohen 		mlx5_query_ext_port_caps(dev, port);
1117e126ba97SEli Cohen }
1118e126ba97SEli Cohen 
1119e126ba97SEli Cohen static int get_port_caps(struct mlx5_ib_dev *dev)
1120e126ba97SEli Cohen {
1121e126ba97SEli Cohen 	struct ib_device_attr *dprops = NULL;
1122e126ba97SEli Cohen 	struct ib_port_attr *pprops = NULL;
1123f614fc15SDan Carpenter 	int err = -ENOMEM;
1124e126ba97SEli Cohen 	int port;
11252528e33eSMatan Barak 	struct ib_udata uhw = {.inlen = 0, .outlen = 0};
1126e126ba97SEli Cohen 
1127e126ba97SEli Cohen 	pprops = kmalloc(sizeof(*pprops), GFP_KERNEL);
1128e126ba97SEli Cohen 	if (!pprops)
1129e126ba97SEli Cohen 		goto out;
1130e126ba97SEli Cohen 
1131e126ba97SEli Cohen 	dprops = kmalloc(sizeof(*dprops), GFP_KERNEL);
1132e126ba97SEli Cohen 	if (!dprops)
1133e126ba97SEli Cohen 		goto out;
1134e126ba97SEli Cohen 
11352528e33eSMatan Barak 	err = mlx5_ib_query_device(&dev->ib_dev, dprops, &uhw);
1136e126ba97SEli Cohen 	if (err) {
1137e126ba97SEli Cohen 		mlx5_ib_warn(dev, "query_device failed %d\n", err);
1138e126ba97SEli Cohen 		goto out;
1139e126ba97SEli Cohen 	}
1140e126ba97SEli Cohen 
1141938fe83cSSaeed Mahameed 	for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++) {
1142e126ba97SEli Cohen 		err = mlx5_ib_query_port(&dev->ib_dev, port, pprops);
1143e126ba97SEli Cohen 		if (err) {
1144938fe83cSSaeed Mahameed 			mlx5_ib_warn(dev, "query_port %d failed %d\n",
1145938fe83cSSaeed Mahameed 				     port, err);
1146e126ba97SEli Cohen 			break;
1147e126ba97SEli Cohen 		}
1148938fe83cSSaeed Mahameed 		dev->mdev->port_caps[port - 1].pkey_table_len =
1149938fe83cSSaeed Mahameed 						dprops->max_pkeys;
1150938fe83cSSaeed Mahameed 		dev->mdev->port_caps[port - 1].gid_table_len =
1151938fe83cSSaeed Mahameed 						pprops->gid_tbl_len;
1152e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "pkey_table_len %d, gid_table_len %d\n",
1153e126ba97SEli Cohen 			    dprops->max_pkeys, pprops->gid_tbl_len);
1154e126ba97SEli Cohen 	}
1155e126ba97SEli Cohen 
1156e126ba97SEli Cohen out:
1157e126ba97SEli Cohen 	kfree(pprops);
1158e126ba97SEli Cohen 	kfree(dprops);
1159e126ba97SEli Cohen 
1160e126ba97SEli Cohen 	return err;
1161e126ba97SEli Cohen }
1162e126ba97SEli Cohen 
1163e126ba97SEli Cohen static void destroy_umrc_res(struct mlx5_ib_dev *dev)
1164e126ba97SEli Cohen {
1165e126ba97SEli Cohen 	int err;
1166e126ba97SEli Cohen 
1167e126ba97SEli Cohen 	err = mlx5_mr_cache_cleanup(dev);
1168e126ba97SEli Cohen 	if (err)
1169e126ba97SEli Cohen 		mlx5_ib_warn(dev, "mr cache cleanup failed\n");
1170e126ba97SEli Cohen 
1171e126ba97SEli Cohen 	mlx5_ib_destroy_qp(dev->umrc.qp);
1172e126ba97SEli Cohen 	ib_destroy_cq(dev->umrc.cq);
1173e126ba97SEli Cohen 	ib_dealloc_pd(dev->umrc.pd);
1174e126ba97SEli Cohen }
1175e126ba97SEli Cohen 
1176e126ba97SEli Cohen enum {
1177e126ba97SEli Cohen 	MAX_UMR_WR = 128,
1178e126ba97SEli Cohen };
1179e126ba97SEli Cohen 
1180e126ba97SEli Cohen static int create_umr_res(struct mlx5_ib_dev *dev)
1181e126ba97SEli Cohen {
1182e126ba97SEli Cohen 	struct ib_qp_init_attr *init_attr = NULL;
1183e126ba97SEli Cohen 	struct ib_qp_attr *attr = NULL;
1184e126ba97SEli Cohen 	struct ib_pd *pd;
1185e126ba97SEli Cohen 	struct ib_cq *cq;
1186e126ba97SEli Cohen 	struct ib_qp *qp;
11878e37210bSMatan Barak 	struct ib_cq_init_attr cq_attr = {};
1188e126ba97SEli Cohen 	int ret;
1189e126ba97SEli Cohen 
1190e126ba97SEli Cohen 	attr = kzalloc(sizeof(*attr), GFP_KERNEL);
1191e126ba97SEli Cohen 	init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL);
1192e126ba97SEli Cohen 	if (!attr || !init_attr) {
1193e126ba97SEli Cohen 		ret = -ENOMEM;
1194e126ba97SEli Cohen 		goto error_0;
1195e126ba97SEli Cohen 	}
1196e126ba97SEli Cohen 
1197e126ba97SEli Cohen 	pd = ib_alloc_pd(&dev->ib_dev);
1198e126ba97SEli Cohen 	if (IS_ERR(pd)) {
1199e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create PD for sync UMR QP\n");
1200e126ba97SEli Cohen 		ret = PTR_ERR(pd);
1201e126ba97SEli Cohen 		goto error_0;
1202e126ba97SEli Cohen 	}
1203e126ba97SEli Cohen 
12048e37210bSMatan Barak 	cq_attr.cqe = 128;
12058e37210bSMatan Barak 	cq = ib_create_cq(&dev->ib_dev, mlx5_umr_cq_handler, NULL, NULL,
12068e37210bSMatan Barak 			  &cq_attr);
1207e126ba97SEli Cohen 	if (IS_ERR(cq)) {
1208e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create CQ for sync UMR QP\n");
1209e126ba97SEli Cohen 		ret = PTR_ERR(cq);
1210e126ba97SEli Cohen 		goto error_2;
1211e126ba97SEli Cohen 	}
1212e126ba97SEli Cohen 	ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
1213e126ba97SEli Cohen 
1214e126ba97SEli Cohen 	init_attr->send_cq = cq;
1215e126ba97SEli Cohen 	init_attr->recv_cq = cq;
1216e126ba97SEli Cohen 	init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
1217e126ba97SEli Cohen 	init_attr->cap.max_send_wr = MAX_UMR_WR;
1218e126ba97SEli Cohen 	init_attr->cap.max_send_sge = 1;
1219e126ba97SEli Cohen 	init_attr->qp_type = MLX5_IB_QPT_REG_UMR;
1220e126ba97SEli Cohen 	init_attr->port_num = 1;
1221e126ba97SEli Cohen 	qp = mlx5_ib_create_qp(pd, init_attr, NULL);
1222e126ba97SEli Cohen 	if (IS_ERR(qp)) {
1223e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create sync UMR QP\n");
1224e126ba97SEli Cohen 		ret = PTR_ERR(qp);
1225e126ba97SEli Cohen 		goto error_3;
1226e126ba97SEli Cohen 	}
1227e126ba97SEli Cohen 	qp->device     = &dev->ib_dev;
1228e126ba97SEli Cohen 	qp->real_qp    = qp;
1229e126ba97SEli Cohen 	qp->uobject    = NULL;
1230e126ba97SEli Cohen 	qp->qp_type    = MLX5_IB_QPT_REG_UMR;
1231e126ba97SEli Cohen 
1232e126ba97SEli Cohen 	attr->qp_state = IB_QPS_INIT;
1233e126ba97SEli Cohen 	attr->port_num = 1;
1234e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_PKEY_INDEX |
1235e126ba97SEli Cohen 				IB_QP_PORT, NULL);
1236e126ba97SEli Cohen 	if (ret) {
1237e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify UMR QP\n");
1238e126ba97SEli Cohen 		goto error_4;
1239e126ba97SEli Cohen 	}
1240e126ba97SEli Cohen 
1241e126ba97SEli Cohen 	memset(attr, 0, sizeof(*attr));
1242e126ba97SEli Cohen 	attr->qp_state = IB_QPS_RTR;
1243e126ba97SEli Cohen 	attr->path_mtu = IB_MTU_256;
1244e126ba97SEli Cohen 
1245e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
1246e126ba97SEli Cohen 	if (ret) {
1247e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify umr QP to rtr\n");
1248e126ba97SEli Cohen 		goto error_4;
1249e126ba97SEli Cohen 	}
1250e126ba97SEli Cohen 
1251e126ba97SEli Cohen 	memset(attr, 0, sizeof(*attr));
1252e126ba97SEli Cohen 	attr->qp_state = IB_QPS_RTS;
1253e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
1254e126ba97SEli Cohen 	if (ret) {
1255e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify umr QP to rts\n");
1256e126ba97SEli Cohen 		goto error_4;
1257e126ba97SEli Cohen 	}
1258e126ba97SEli Cohen 
1259e126ba97SEli Cohen 	dev->umrc.qp = qp;
1260e126ba97SEli Cohen 	dev->umrc.cq = cq;
1261e126ba97SEli Cohen 	dev->umrc.pd = pd;
1262e126ba97SEli Cohen 
1263e126ba97SEli Cohen 	sema_init(&dev->umrc.sem, MAX_UMR_WR);
1264e126ba97SEli Cohen 	ret = mlx5_mr_cache_init(dev);
1265e126ba97SEli Cohen 	if (ret) {
1266e126ba97SEli Cohen 		mlx5_ib_warn(dev, "mr cache init failed %d\n", ret);
1267e126ba97SEli Cohen 		goto error_4;
1268e126ba97SEli Cohen 	}
1269e126ba97SEli Cohen 
1270e126ba97SEli Cohen 	kfree(attr);
1271e126ba97SEli Cohen 	kfree(init_attr);
1272e126ba97SEli Cohen 
1273e126ba97SEli Cohen 	return 0;
1274e126ba97SEli Cohen 
1275e126ba97SEli Cohen error_4:
1276e126ba97SEli Cohen 	mlx5_ib_destroy_qp(qp);
1277e126ba97SEli Cohen 
1278e126ba97SEli Cohen error_3:
1279e126ba97SEli Cohen 	ib_destroy_cq(cq);
1280e126ba97SEli Cohen 
1281e126ba97SEli Cohen error_2:
1282e126ba97SEli Cohen 	ib_dealloc_pd(pd);
1283e126ba97SEli Cohen 
1284e126ba97SEli Cohen error_0:
1285e126ba97SEli Cohen 	kfree(attr);
1286e126ba97SEli Cohen 	kfree(init_attr);
1287e126ba97SEli Cohen 	return ret;
1288e126ba97SEli Cohen }
1289e126ba97SEli Cohen 
1290e126ba97SEli Cohen static int create_dev_resources(struct mlx5_ib_resources *devr)
1291e126ba97SEli Cohen {
1292e126ba97SEli Cohen 	struct ib_srq_init_attr attr;
1293e126ba97SEli Cohen 	struct mlx5_ib_dev *dev;
1294bcf4c1eaSMatan Barak 	struct ib_cq_init_attr cq_attr = {.cqe = 1};
1295e126ba97SEli Cohen 	int ret = 0;
1296e126ba97SEli Cohen 
1297e126ba97SEli Cohen 	dev = container_of(devr, struct mlx5_ib_dev, devr);
1298e126ba97SEli Cohen 
1299e126ba97SEli Cohen 	devr->p0 = mlx5_ib_alloc_pd(&dev->ib_dev, NULL, NULL);
1300e126ba97SEli Cohen 	if (IS_ERR(devr->p0)) {
1301e126ba97SEli Cohen 		ret = PTR_ERR(devr->p0);
1302e126ba97SEli Cohen 		goto error0;
1303e126ba97SEli Cohen 	}
1304e126ba97SEli Cohen 	devr->p0->device  = &dev->ib_dev;
1305e126ba97SEli Cohen 	devr->p0->uobject = NULL;
1306e126ba97SEli Cohen 	atomic_set(&devr->p0->usecnt, 0);
1307e126ba97SEli Cohen 
1308bcf4c1eaSMatan Barak 	devr->c0 = mlx5_ib_create_cq(&dev->ib_dev, &cq_attr, NULL, NULL);
1309e126ba97SEli Cohen 	if (IS_ERR(devr->c0)) {
1310e126ba97SEli Cohen 		ret = PTR_ERR(devr->c0);
1311e126ba97SEli Cohen 		goto error1;
1312e126ba97SEli Cohen 	}
1313e126ba97SEli Cohen 	devr->c0->device        = &dev->ib_dev;
1314e126ba97SEli Cohen 	devr->c0->uobject       = NULL;
1315e126ba97SEli Cohen 	devr->c0->comp_handler  = NULL;
1316e126ba97SEli Cohen 	devr->c0->event_handler = NULL;
1317e126ba97SEli Cohen 	devr->c0->cq_context    = NULL;
1318e126ba97SEli Cohen 	atomic_set(&devr->c0->usecnt, 0);
1319e126ba97SEli Cohen 
1320e126ba97SEli Cohen 	devr->x0 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
1321e126ba97SEli Cohen 	if (IS_ERR(devr->x0)) {
1322e126ba97SEli Cohen 		ret = PTR_ERR(devr->x0);
1323e126ba97SEli Cohen 		goto error2;
1324e126ba97SEli Cohen 	}
1325e126ba97SEli Cohen 	devr->x0->device = &dev->ib_dev;
1326e126ba97SEli Cohen 	devr->x0->inode = NULL;
1327e126ba97SEli Cohen 	atomic_set(&devr->x0->usecnt, 0);
1328e126ba97SEli Cohen 	mutex_init(&devr->x0->tgt_qp_mutex);
1329e126ba97SEli Cohen 	INIT_LIST_HEAD(&devr->x0->tgt_qp_list);
1330e126ba97SEli Cohen 
1331e126ba97SEli Cohen 	devr->x1 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
1332e126ba97SEli Cohen 	if (IS_ERR(devr->x1)) {
1333e126ba97SEli Cohen 		ret = PTR_ERR(devr->x1);
1334e126ba97SEli Cohen 		goto error3;
1335e126ba97SEli Cohen 	}
1336e126ba97SEli Cohen 	devr->x1->device = &dev->ib_dev;
1337e126ba97SEli Cohen 	devr->x1->inode = NULL;
1338e126ba97SEli Cohen 	atomic_set(&devr->x1->usecnt, 0);
1339e126ba97SEli Cohen 	mutex_init(&devr->x1->tgt_qp_mutex);
1340e126ba97SEli Cohen 	INIT_LIST_HEAD(&devr->x1->tgt_qp_list);
1341e126ba97SEli Cohen 
1342e126ba97SEli Cohen 	memset(&attr, 0, sizeof(attr));
1343e126ba97SEli Cohen 	attr.attr.max_sge = 1;
1344e126ba97SEli Cohen 	attr.attr.max_wr = 1;
1345e126ba97SEli Cohen 	attr.srq_type = IB_SRQT_XRC;
1346e126ba97SEli Cohen 	attr.ext.xrc.cq = devr->c0;
1347e126ba97SEli Cohen 	attr.ext.xrc.xrcd = devr->x0;
1348e126ba97SEli Cohen 
1349e126ba97SEli Cohen 	devr->s0 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
1350e126ba97SEli Cohen 	if (IS_ERR(devr->s0)) {
1351e126ba97SEli Cohen 		ret = PTR_ERR(devr->s0);
1352e126ba97SEli Cohen 		goto error4;
1353e126ba97SEli Cohen 	}
1354e126ba97SEli Cohen 	devr->s0->device	= &dev->ib_dev;
1355e126ba97SEli Cohen 	devr->s0->pd		= devr->p0;
1356e126ba97SEli Cohen 	devr->s0->uobject       = NULL;
1357e126ba97SEli Cohen 	devr->s0->event_handler = NULL;
1358e126ba97SEli Cohen 	devr->s0->srq_context   = NULL;
1359e126ba97SEli Cohen 	devr->s0->srq_type      = IB_SRQT_XRC;
1360e126ba97SEli Cohen 	devr->s0->ext.xrc.xrcd	= devr->x0;
1361e126ba97SEli Cohen 	devr->s0->ext.xrc.cq	= devr->c0;
1362e126ba97SEli Cohen 	atomic_inc(&devr->s0->ext.xrc.xrcd->usecnt);
1363e126ba97SEli Cohen 	atomic_inc(&devr->s0->ext.xrc.cq->usecnt);
1364e126ba97SEli Cohen 	atomic_inc(&devr->p0->usecnt);
1365e126ba97SEli Cohen 	atomic_set(&devr->s0->usecnt, 0);
1366e126ba97SEli Cohen 
13674aa17b28SHaggai Abramonvsky 	memset(&attr, 0, sizeof(attr));
13684aa17b28SHaggai Abramonvsky 	attr.attr.max_sge = 1;
13694aa17b28SHaggai Abramonvsky 	attr.attr.max_wr = 1;
13704aa17b28SHaggai Abramonvsky 	attr.srq_type = IB_SRQT_BASIC;
13714aa17b28SHaggai Abramonvsky 	devr->s1 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
13724aa17b28SHaggai Abramonvsky 	if (IS_ERR(devr->s1)) {
13734aa17b28SHaggai Abramonvsky 		ret = PTR_ERR(devr->s1);
13744aa17b28SHaggai Abramonvsky 		goto error5;
13754aa17b28SHaggai Abramonvsky 	}
13764aa17b28SHaggai Abramonvsky 	devr->s1->device	= &dev->ib_dev;
13774aa17b28SHaggai Abramonvsky 	devr->s1->pd		= devr->p0;
13784aa17b28SHaggai Abramonvsky 	devr->s1->uobject       = NULL;
13794aa17b28SHaggai Abramonvsky 	devr->s1->event_handler = NULL;
13804aa17b28SHaggai Abramonvsky 	devr->s1->srq_context   = NULL;
13814aa17b28SHaggai Abramonvsky 	devr->s1->srq_type      = IB_SRQT_BASIC;
13824aa17b28SHaggai Abramonvsky 	devr->s1->ext.xrc.cq	= devr->c0;
13834aa17b28SHaggai Abramonvsky 	atomic_inc(&devr->p0->usecnt);
13844aa17b28SHaggai Abramonvsky 	atomic_set(&devr->s0->usecnt, 0);
13854aa17b28SHaggai Abramonvsky 
1386e126ba97SEli Cohen 	return 0;
1387e126ba97SEli Cohen 
13884aa17b28SHaggai Abramonvsky error5:
13894aa17b28SHaggai Abramonvsky 	mlx5_ib_destroy_srq(devr->s0);
1390e126ba97SEli Cohen error4:
1391e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x1);
1392e126ba97SEli Cohen error3:
1393e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x0);
1394e126ba97SEli Cohen error2:
1395e126ba97SEli Cohen 	mlx5_ib_destroy_cq(devr->c0);
1396e126ba97SEli Cohen error1:
1397e126ba97SEli Cohen 	mlx5_ib_dealloc_pd(devr->p0);
1398e126ba97SEli Cohen error0:
1399e126ba97SEli Cohen 	return ret;
1400e126ba97SEli Cohen }
1401e126ba97SEli Cohen 
1402e126ba97SEli Cohen static void destroy_dev_resources(struct mlx5_ib_resources *devr)
1403e126ba97SEli Cohen {
14044aa17b28SHaggai Abramonvsky 	mlx5_ib_destroy_srq(devr->s1);
1405e126ba97SEli Cohen 	mlx5_ib_destroy_srq(devr->s0);
1406e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x0);
1407e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x1);
1408e126ba97SEli Cohen 	mlx5_ib_destroy_cq(devr->c0);
1409e126ba97SEli Cohen 	mlx5_ib_dealloc_pd(devr->p0);
1410e126ba97SEli Cohen }
1411e126ba97SEli Cohen 
14127738613eSIra Weiny static int mlx5_port_immutable(struct ib_device *ibdev, u8 port_num,
14137738613eSIra Weiny 			       struct ib_port_immutable *immutable)
14147738613eSIra Weiny {
14157738613eSIra Weiny 	struct ib_port_attr attr;
14167738613eSIra Weiny 	int err;
14177738613eSIra Weiny 
14187738613eSIra Weiny 	err = mlx5_ib_query_port(ibdev, port_num, &attr);
14197738613eSIra Weiny 	if (err)
14207738613eSIra Weiny 		return err;
14217738613eSIra Weiny 
14227738613eSIra Weiny 	immutable->pkey_tbl_len = attr.pkey_tbl_len;
14237738613eSIra Weiny 	immutable->gid_tbl_len = attr.gid_tbl_len;
1424f9b22e35SIra Weiny 	immutable->core_cap_flags = RDMA_CORE_PORT_IBA_IB;
1425337877a4SIra Weiny 	immutable->max_mad_size = IB_MGMT_MAD_SIZE;
14267738613eSIra Weiny 
14277738613eSIra Weiny 	return 0;
14287738613eSIra Weiny }
14297738613eSIra Weiny 
1430fc24fc5eSAchiad Shochat static int mlx5_enable_roce(struct mlx5_ib_dev *dev)
1431fc24fc5eSAchiad Shochat {
1432fc24fc5eSAchiad Shochat 	dev->roce.nb.notifier_call = mlx5_netdev_event;
1433fc24fc5eSAchiad Shochat 	return register_netdevice_notifier(&dev->roce.nb);
1434fc24fc5eSAchiad Shochat }
1435fc24fc5eSAchiad Shochat 
1436fc24fc5eSAchiad Shochat static void mlx5_disable_roce(struct mlx5_ib_dev *dev)
1437fc24fc5eSAchiad Shochat {
1438fc24fc5eSAchiad Shochat 	unregister_netdevice_notifier(&dev->roce.nb);
1439fc24fc5eSAchiad Shochat }
1440fc24fc5eSAchiad Shochat 
14419603b61dSJack Morgenstein static void *mlx5_ib_add(struct mlx5_core_dev *mdev)
1442e126ba97SEli Cohen {
1443e126ba97SEli Cohen 	struct mlx5_ib_dev *dev;
1444ebd61f68SAchiad Shochat 	enum rdma_link_layer ll;
1445ebd61f68SAchiad Shochat 	int port_type_cap;
1446e126ba97SEli Cohen 	int err;
1447e126ba97SEli Cohen 	int i;
1448e126ba97SEli Cohen 
1449ebd61f68SAchiad Shochat 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
1450ebd61f68SAchiad Shochat 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
1451ebd61f68SAchiad Shochat 
1452647241eaSMajd Dibbiny 	/* don't create IB instance over Eth ports, no RoCE yet! */
1453ebd61f68SAchiad Shochat 	if (ll == IB_LINK_LAYER_ETHERNET)
1454647241eaSMajd Dibbiny 		return NULL;
1455647241eaSMajd Dibbiny 
1456e126ba97SEli Cohen 	printk_once(KERN_INFO "%s", mlx5_version);
1457e126ba97SEli Cohen 
1458e126ba97SEli Cohen 	dev = (struct mlx5_ib_dev *)ib_alloc_device(sizeof(*dev));
1459e126ba97SEli Cohen 	if (!dev)
14609603b61dSJack Morgenstein 		return NULL;
1461e126ba97SEli Cohen 
14629603b61dSJack Morgenstein 	dev->mdev = mdev;
1463e126ba97SEli Cohen 
1464fc24fc5eSAchiad Shochat 	rwlock_init(&dev->roce.netdev_lock);
1465e126ba97SEli Cohen 	err = get_port_caps(dev);
1466e126ba97SEli Cohen 	if (err)
14679603b61dSJack Morgenstein 		goto err_dealloc;
1468e126ba97SEli Cohen 
14691b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(dev))
1470e126ba97SEli Cohen 		get_ext_port_caps(dev);
1471e126ba97SEli Cohen 
1472e126ba97SEli Cohen 	MLX5_INIT_DOORBELL_LOCK(&dev->uar_lock);
1473e126ba97SEli Cohen 
1474e126ba97SEli Cohen 	strlcpy(dev->ib_dev.name, "mlx5_%d", IB_DEVICE_NAME_MAX);
1475e126ba97SEli Cohen 	dev->ib_dev.owner		= THIS_MODULE;
1476e126ba97SEli Cohen 	dev->ib_dev.node_type		= RDMA_NODE_IB_CA;
1477c6790aa9SSagi Grimberg 	dev->ib_dev.local_dma_lkey	= 0 /* not supported for now */;
1478938fe83cSSaeed Mahameed 	dev->num_ports		= MLX5_CAP_GEN(mdev, num_ports);
1479e126ba97SEli Cohen 	dev->ib_dev.phys_port_cnt     = dev->num_ports;
1480233d05d2SSaeed Mahameed 	dev->ib_dev.num_comp_vectors    =
1481233d05d2SSaeed Mahameed 		dev->mdev->priv.eq_table.num_comp_vectors;
1482e126ba97SEli Cohen 	dev->ib_dev.dma_device	= &mdev->pdev->dev;
1483e126ba97SEli Cohen 
1484e126ba97SEli Cohen 	dev->ib_dev.uverbs_abi_ver	= MLX5_IB_UVERBS_ABI_VERSION;
1485e126ba97SEli Cohen 	dev->ib_dev.uverbs_cmd_mask	=
1486e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_GET_CONTEXT)		|
1487e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_DEVICE)	|
1488e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_PORT)		|
1489e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_ALLOC_PD)		|
1490e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DEALLOC_PD)		|
1491e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_REG_MR)		|
1492e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DEREG_MR)		|
1493e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL)	|
1494e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_CQ)		|
1495e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_RESIZE_CQ)		|
1496e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_CQ)		|
1497e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_QP)		|
1498e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_MODIFY_QP)		|
1499e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_QP)		|
1500e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_QP)		|
1501e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_ATTACH_MCAST)	|
1502e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DETACH_MCAST)	|
1503e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_SRQ)		|
1504e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_MODIFY_SRQ)		|
1505e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_SRQ)		|
1506e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_SRQ)		|
1507e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_XSRQ)		|
1508e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_OPEN_QP);
15091707cb4aSHaggai Eran 	dev->ib_dev.uverbs_ex_cmd_mask =
15101707cb4aSHaggai Eran 		(1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE);
1511e126ba97SEli Cohen 
1512e126ba97SEli Cohen 	dev->ib_dev.query_device	= mlx5_ib_query_device;
1513e126ba97SEli Cohen 	dev->ib_dev.query_port		= mlx5_ib_query_port;
1514ebd61f68SAchiad Shochat 	dev->ib_dev.get_link_layer	= mlx5_ib_port_link_layer;
1515fc24fc5eSAchiad Shochat 	if (ll == IB_LINK_LAYER_ETHERNET)
1516fc24fc5eSAchiad Shochat 		dev->ib_dev.get_netdev	= mlx5_ib_get_netdev;
1517e126ba97SEli Cohen 	dev->ib_dev.query_gid		= mlx5_ib_query_gid;
1518e126ba97SEli Cohen 	dev->ib_dev.query_pkey		= mlx5_ib_query_pkey;
1519e126ba97SEli Cohen 	dev->ib_dev.modify_device	= mlx5_ib_modify_device;
1520e126ba97SEli Cohen 	dev->ib_dev.modify_port		= mlx5_ib_modify_port;
1521e126ba97SEli Cohen 	dev->ib_dev.alloc_ucontext	= mlx5_ib_alloc_ucontext;
1522e126ba97SEli Cohen 	dev->ib_dev.dealloc_ucontext	= mlx5_ib_dealloc_ucontext;
1523e126ba97SEli Cohen 	dev->ib_dev.mmap		= mlx5_ib_mmap;
1524e126ba97SEli Cohen 	dev->ib_dev.alloc_pd		= mlx5_ib_alloc_pd;
1525e126ba97SEli Cohen 	dev->ib_dev.dealloc_pd		= mlx5_ib_dealloc_pd;
1526e126ba97SEli Cohen 	dev->ib_dev.create_ah		= mlx5_ib_create_ah;
1527e126ba97SEli Cohen 	dev->ib_dev.query_ah		= mlx5_ib_query_ah;
1528e126ba97SEli Cohen 	dev->ib_dev.destroy_ah		= mlx5_ib_destroy_ah;
1529e126ba97SEli Cohen 	dev->ib_dev.create_srq		= mlx5_ib_create_srq;
1530e126ba97SEli Cohen 	dev->ib_dev.modify_srq		= mlx5_ib_modify_srq;
1531e126ba97SEli Cohen 	dev->ib_dev.query_srq		= mlx5_ib_query_srq;
1532e126ba97SEli Cohen 	dev->ib_dev.destroy_srq		= mlx5_ib_destroy_srq;
1533e126ba97SEli Cohen 	dev->ib_dev.post_srq_recv	= mlx5_ib_post_srq_recv;
1534e126ba97SEli Cohen 	dev->ib_dev.create_qp		= mlx5_ib_create_qp;
1535e126ba97SEli Cohen 	dev->ib_dev.modify_qp		= mlx5_ib_modify_qp;
1536e126ba97SEli Cohen 	dev->ib_dev.query_qp		= mlx5_ib_query_qp;
1537e126ba97SEli Cohen 	dev->ib_dev.destroy_qp		= mlx5_ib_destroy_qp;
1538e126ba97SEli Cohen 	dev->ib_dev.post_send		= mlx5_ib_post_send;
1539e126ba97SEli Cohen 	dev->ib_dev.post_recv		= mlx5_ib_post_recv;
1540e126ba97SEli Cohen 	dev->ib_dev.create_cq		= mlx5_ib_create_cq;
1541e126ba97SEli Cohen 	dev->ib_dev.modify_cq		= mlx5_ib_modify_cq;
1542e126ba97SEli Cohen 	dev->ib_dev.resize_cq		= mlx5_ib_resize_cq;
1543e126ba97SEli Cohen 	dev->ib_dev.destroy_cq		= mlx5_ib_destroy_cq;
1544e126ba97SEli Cohen 	dev->ib_dev.poll_cq		= mlx5_ib_poll_cq;
1545e126ba97SEli Cohen 	dev->ib_dev.req_notify_cq	= mlx5_ib_arm_cq;
1546e126ba97SEli Cohen 	dev->ib_dev.get_dma_mr		= mlx5_ib_get_dma_mr;
1547e126ba97SEli Cohen 	dev->ib_dev.reg_user_mr		= mlx5_ib_reg_user_mr;
1548e126ba97SEli Cohen 	dev->ib_dev.dereg_mr		= mlx5_ib_dereg_mr;
1549e126ba97SEli Cohen 	dev->ib_dev.attach_mcast	= mlx5_ib_mcg_attach;
1550e126ba97SEli Cohen 	dev->ib_dev.detach_mcast	= mlx5_ib_mcg_detach;
1551e126ba97SEli Cohen 	dev->ib_dev.process_mad		= mlx5_ib_process_mad;
15529bee178bSSagi Grimberg 	dev->ib_dev.alloc_mr		= mlx5_ib_alloc_mr;
15538a187ee5SSagi Grimberg 	dev->ib_dev.map_mr_sg		= mlx5_ib_map_mr_sg;
1554d5436ba0SSagi Grimberg 	dev->ib_dev.check_mr_status	= mlx5_ib_check_mr_status;
15557738613eSIra Weiny 	dev->ib_dev.get_port_immutable  = mlx5_port_immutable;
1556e126ba97SEli Cohen 
1557938fe83cSSaeed Mahameed 	mlx5_ib_internal_fill_odp_caps(dev);
15588cdd312cSHaggai Eran 
1559938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, xrc)) {
1560e126ba97SEli Cohen 		dev->ib_dev.alloc_xrcd = mlx5_ib_alloc_xrcd;
1561e126ba97SEli Cohen 		dev->ib_dev.dealloc_xrcd = mlx5_ib_dealloc_xrcd;
1562e126ba97SEli Cohen 		dev->ib_dev.uverbs_cmd_mask |=
1563e126ba97SEli Cohen 			(1ull << IB_USER_VERBS_CMD_OPEN_XRCD) |
1564e126ba97SEli Cohen 			(1ull << IB_USER_VERBS_CMD_CLOSE_XRCD);
1565e126ba97SEli Cohen 	}
1566e126ba97SEli Cohen 
1567e126ba97SEli Cohen 	err = init_node_data(dev);
1568e126ba97SEli Cohen 	if (err)
1569233d05d2SSaeed Mahameed 		goto err_dealloc;
1570e126ba97SEli Cohen 
1571e126ba97SEli Cohen 	mutex_init(&dev->cap_mask_mutex);
1572e126ba97SEli Cohen 
1573fc24fc5eSAchiad Shochat 	if (ll == IB_LINK_LAYER_ETHERNET) {
1574fc24fc5eSAchiad Shochat 		err = mlx5_enable_roce(dev);
1575e126ba97SEli Cohen 		if (err)
1576233d05d2SSaeed Mahameed 			goto err_dealloc;
1577fc24fc5eSAchiad Shochat 	}
1578fc24fc5eSAchiad Shochat 
1579fc24fc5eSAchiad Shochat 	err = create_dev_resources(&dev->devr);
1580fc24fc5eSAchiad Shochat 	if (err)
1581fc24fc5eSAchiad Shochat 		goto err_disable_roce;
1582e126ba97SEli Cohen 
15836aec21f6SHaggai Eran 	err = mlx5_ib_odp_init_one(dev);
1584281d1a92SWei Yongjun 	if (err)
1585e126ba97SEli Cohen 		goto err_rsrc;
1586e126ba97SEli Cohen 
15876aec21f6SHaggai Eran 	err = ib_register_device(&dev->ib_dev, NULL);
15886aec21f6SHaggai Eran 	if (err)
15896aec21f6SHaggai Eran 		goto err_odp;
15906aec21f6SHaggai Eran 
1591e126ba97SEli Cohen 	err = create_umr_res(dev);
1592e126ba97SEli Cohen 	if (err)
1593e126ba97SEli Cohen 		goto err_dev;
1594e126ba97SEli Cohen 
1595e126ba97SEli Cohen 	for (i = 0; i < ARRAY_SIZE(mlx5_class_attributes); i++) {
1596281d1a92SWei Yongjun 		err = device_create_file(&dev->ib_dev.dev,
1597281d1a92SWei Yongjun 					 mlx5_class_attributes[i]);
1598281d1a92SWei Yongjun 		if (err)
1599e126ba97SEli Cohen 			goto err_umrc;
1600e126ba97SEli Cohen 	}
1601e126ba97SEli Cohen 
1602e126ba97SEli Cohen 	dev->ib_active = true;
1603e126ba97SEli Cohen 
16049603b61dSJack Morgenstein 	return dev;
1605e126ba97SEli Cohen 
1606e126ba97SEli Cohen err_umrc:
1607e126ba97SEli Cohen 	destroy_umrc_res(dev);
1608e126ba97SEli Cohen 
1609e126ba97SEli Cohen err_dev:
1610e126ba97SEli Cohen 	ib_unregister_device(&dev->ib_dev);
1611e126ba97SEli Cohen 
16126aec21f6SHaggai Eran err_odp:
16136aec21f6SHaggai Eran 	mlx5_ib_odp_remove_one(dev);
16146aec21f6SHaggai Eran 
1615e126ba97SEli Cohen err_rsrc:
1616e126ba97SEli Cohen 	destroy_dev_resources(&dev->devr);
1617e126ba97SEli Cohen 
1618fc24fc5eSAchiad Shochat err_disable_roce:
1619fc24fc5eSAchiad Shochat 	if (ll == IB_LINK_LAYER_ETHERNET)
1620fc24fc5eSAchiad Shochat 		mlx5_disable_roce(dev);
1621fc24fc5eSAchiad Shochat 
16229603b61dSJack Morgenstein err_dealloc:
1623e126ba97SEli Cohen 	ib_dealloc_device((struct ib_device *)dev);
1624e126ba97SEli Cohen 
16259603b61dSJack Morgenstein 	return NULL;
1626e126ba97SEli Cohen }
1627e126ba97SEli Cohen 
16289603b61dSJack Morgenstein static void mlx5_ib_remove(struct mlx5_core_dev *mdev, void *context)
1629e126ba97SEli Cohen {
16309603b61dSJack Morgenstein 	struct mlx5_ib_dev *dev = context;
1631fc24fc5eSAchiad Shochat 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev, 1);
16326aec21f6SHaggai Eran 
1633e126ba97SEli Cohen 	ib_unregister_device(&dev->ib_dev);
1634eefd56e5SEli Cohen 	destroy_umrc_res(dev);
16356aec21f6SHaggai Eran 	mlx5_ib_odp_remove_one(dev);
1636e126ba97SEli Cohen 	destroy_dev_resources(&dev->devr);
1637fc24fc5eSAchiad Shochat 	if (ll == IB_LINK_LAYER_ETHERNET)
1638fc24fc5eSAchiad Shochat 		mlx5_disable_roce(dev);
1639e126ba97SEli Cohen 	ib_dealloc_device(&dev->ib_dev);
1640e126ba97SEli Cohen }
1641e126ba97SEli Cohen 
16429603b61dSJack Morgenstein static struct mlx5_interface mlx5_ib_interface = {
16439603b61dSJack Morgenstein 	.add            = mlx5_ib_add,
16449603b61dSJack Morgenstein 	.remove         = mlx5_ib_remove,
16459603b61dSJack Morgenstein 	.event          = mlx5_ib_event,
164664613d94SSaeed Mahameed 	.protocol	= MLX5_INTERFACE_PROTOCOL_IB,
1647e126ba97SEli Cohen };
1648e126ba97SEli Cohen 
1649e126ba97SEli Cohen static int __init mlx5_ib_init(void)
1650e126ba97SEli Cohen {
16516aec21f6SHaggai Eran 	int err;
16526aec21f6SHaggai Eran 
16539603b61dSJack Morgenstein 	if (deprecated_prof_sel != 2)
16549603b61dSJack Morgenstein 		pr_warn("prof_sel is deprecated for mlx5_ib, set it for mlx5_core\n");
16559603b61dSJack Morgenstein 
16566aec21f6SHaggai Eran 	err = mlx5_ib_odp_init();
16576aec21f6SHaggai Eran 	if (err)
16586aec21f6SHaggai Eran 		return err;
16596aec21f6SHaggai Eran 
16606aec21f6SHaggai Eran 	err = mlx5_register_interface(&mlx5_ib_interface);
16616aec21f6SHaggai Eran 	if (err)
16626aec21f6SHaggai Eran 		goto clean_odp;
16636aec21f6SHaggai Eran 
16646aec21f6SHaggai Eran 	return err;
16656aec21f6SHaggai Eran 
16666aec21f6SHaggai Eran clean_odp:
16676aec21f6SHaggai Eran 	mlx5_ib_odp_cleanup();
16686aec21f6SHaggai Eran 	return err;
1669e126ba97SEli Cohen }
1670e126ba97SEli Cohen 
1671e126ba97SEli Cohen static void __exit mlx5_ib_cleanup(void)
1672e126ba97SEli Cohen {
16739603b61dSJack Morgenstein 	mlx5_unregister_interface(&mlx5_ib_interface);
16746aec21f6SHaggai Eran 	mlx5_ib_odp_cleanup();
1675e126ba97SEli Cohen }
1676e126ba97SEli Cohen 
1677e126ba97SEli Cohen module_init(mlx5_ib_init);
1678e126ba97SEli Cohen module_exit(mlx5_ib_cleanup);
1679