xref: /openbmc/linux/drivers/infiniband/hw/mlx5/main.c (revision 3cca26069a4b7f6d8fd3dc0ed707e795c22712e2)
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>
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 
1243f89a643SAchiad Shochat static int mlx5_query_port_roce(struct ib_device *device, u8 port_num,
1253f89a643SAchiad Shochat 				struct ib_port_attr *props)
1263f89a643SAchiad Shochat {
1273f89a643SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(device);
1283f89a643SAchiad Shochat 	struct net_device *ndev;
1293f89a643SAchiad Shochat 	enum ib_mtu ndev_ib_mtu;
1303f89a643SAchiad Shochat 
1313f89a643SAchiad Shochat 	memset(props, 0, sizeof(*props));
1323f89a643SAchiad Shochat 
1333f89a643SAchiad Shochat 	props->port_cap_flags  |= IB_PORT_CM_SUP;
1343f89a643SAchiad Shochat 	props->port_cap_flags  |= IB_PORT_IP_BASED_GIDS;
1353f89a643SAchiad Shochat 
1363f89a643SAchiad Shochat 	props->gid_tbl_len      = MLX5_CAP_ROCE(dev->mdev,
1373f89a643SAchiad Shochat 						roce_address_table_size);
1383f89a643SAchiad Shochat 	props->max_mtu          = IB_MTU_4096;
1393f89a643SAchiad Shochat 	props->max_msg_sz       = 1 << MLX5_CAP_GEN(dev->mdev, log_max_msg);
1403f89a643SAchiad Shochat 	props->pkey_tbl_len     = 1;
1413f89a643SAchiad Shochat 	props->state            = IB_PORT_DOWN;
1423f89a643SAchiad Shochat 	props->phys_state       = 3;
1433f89a643SAchiad Shochat 
1443f89a643SAchiad Shochat 	mlx5_query_nic_vport_qkey_viol_cntr(dev->mdev,
1453f89a643SAchiad Shochat 					    (u16 *)&props->qkey_viol_cntr);
1463f89a643SAchiad Shochat 
1473f89a643SAchiad Shochat 	ndev = mlx5_ib_get_netdev(device, port_num);
1483f89a643SAchiad Shochat 	if (!ndev)
1493f89a643SAchiad Shochat 		return 0;
1503f89a643SAchiad Shochat 
1513f89a643SAchiad Shochat 	if (netif_running(ndev) && netif_carrier_ok(ndev)) {
1523f89a643SAchiad Shochat 		props->state      = IB_PORT_ACTIVE;
1533f89a643SAchiad Shochat 		props->phys_state = 5;
1543f89a643SAchiad Shochat 	}
1553f89a643SAchiad Shochat 
1563f89a643SAchiad Shochat 	ndev_ib_mtu = iboe_get_mtu(ndev->mtu);
1573f89a643SAchiad Shochat 
1583f89a643SAchiad Shochat 	dev_put(ndev);
1593f89a643SAchiad Shochat 
1603f89a643SAchiad Shochat 	props->active_mtu	= min(props->max_mtu, ndev_ib_mtu);
1613f89a643SAchiad Shochat 
1623f89a643SAchiad Shochat 	props->active_width	= IB_WIDTH_4X;  /* TODO */
1633f89a643SAchiad Shochat 	props->active_speed	= IB_SPEED_QDR; /* TODO */
1643f89a643SAchiad Shochat 
1653f89a643SAchiad Shochat 	return 0;
1663f89a643SAchiad Shochat }
1673f89a643SAchiad Shochat 
168*3cca2606SAchiad Shochat static void ib_gid_to_mlx5_roce_addr(const union ib_gid *gid,
169*3cca2606SAchiad Shochat 				     const struct ib_gid_attr *attr,
170*3cca2606SAchiad Shochat 				     void *mlx5_addr)
171*3cca2606SAchiad Shochat {
172*3cca2606SAchiad Shochat #define MLX5_SET_RA(p, f, v) MLX5_SET(roce_addr_layout, p, f, v)
173*3cca2606SAchiad Shochat 	char *mlx5_addr_l3_addr	= MLX5_ADDR_OF(roce_addr_layout, mlx5_addr,
174*3cca2606SAchiad Shochat 					       source_l3_address);
175*3cca2606SAchiad Shochat 	void *mlx5_addr_mac	= MLX5_ADDR_OF(roce_addr_layout, mlx5_addr,
176*3cca2606SAchiad Shochat 					       source_mac_47_32);
177*3cca2606SAchiad Shochat 
178*3cca2606SAchiad Shochat 	if (!gid)
179*3cca2606SAchiad Shochat 		return;
180*3cca2606SAchiad Shochat 
181*3cca2606SAchiad Shochat 	ether_addr_copy(mlx5_addr_mac, attr->ndev->dev_addr);
182*3cca2606SAchiad Shochat 
183*3cca2606SAchiad Shochat 	if (is_vlan_dev(attr->ndev)) {
184*3cca2606SAchiad Shochat 		MLX5_SET_RA(mlx5_addr, vlan_valid, 1);
185*3cca2606SAchiad Shochat 		MLX5_SET_RA(mlx5_addr, vlan_id, vlan_dev_vlan_id(attr->ndev));
186*3cca2606SAchiad Shochat 	}
187*3cca2606SAchiad Shochat 
188*3cca2606SAchiad Shochat 	switch (attr->gid_type) {
189*3cca2606SAchiad Shochat 	case IB_GID_TYPE_IB:
190*3cca2606SAchiad Shochat 		MLX5_SET_RA(mlx5_addr, roce_version, MLX5_ROCE_VERSION_1);
191*3cca2606SAchiad Shochat 		break;
192*3cca2606SAchiad Shochat 	case IB_GID_TYPE_ROCE_UDP_ENCAP:
193*3cca2606SAchiad Shochat 		MLX5_SET_RA(mlx5_addr, roce_version, MLX5_ROCE_VERSION_2);
194*3cca2606SAchiad Shochat 		break;
195*3cca2606SAchiad Shochat 
196*3cca2606SAchiad Shochat 	default:
197*3cca2606SAchiad Shochat 		WARN_ON(true);
198*3cca2606SAchiad Shochat 	}
199*3cca2606SAchiad Shochat 
200*3cca2606SAchiad Shochat 	if (attr->gid_type != IB_GID_TYPE_IB) {
201*3cca2606SAchiad Shochat 		if (ipv6_addr_v4mapped((void *)gid))
202*3cca2606SAchiad Shochat 			MLX5_SET_RA(mlx5_addr, roce_l3_type,
203*3cca2606SAchiad Shochat 				    MLX5_ROCE_L3_TYPE_IPV4);
204*3cca2606SAchiad Shochat 		else
205*3cca2606SAchiad Shochat 			MLX5_SET_RA(mlx5_addr, roce_l3_type,
206*3cca2606SAchiad Shochat 				    MLX5_ROCE_L3_TYPE_IPV6);
207*3cca2606SAchiad Shochat 	}
208*3cca2606SAchiad Shochat 
209*3cca2606SAchiad Shochat 	if ((attr->gid_type == IB_GID_TYPE_IB) ||
210*3cca2606SAchiad Shochat 	    !ipv6_addr_v4mapped((void *)gid))
211*3cca2606SAchiad Shochat 		memcpy(mlx5_addr_l3_addr, gid, sizeof(*gid));
212*3cca2606SAchiad Shochat 	else
213*3cca2606SAchiad Shochat 		memcpy(&mlx5_addr_l3_addr[12], &gid->raw[12], 4);
214*3cca2606SAchiad Shochat }
215*3cca2606SAchiad Shochat 
216*3cca2606SAchiad Shochat static int set_roce_addr(struct ib_device *device, u8 port_num,
217*3cca2606SAchiad Shochat 			 unsigned int index,
218*3cca2606SAchiad Shochat 			 const union ib_gid *gid,
219*3cca2606SAchiad Shochat 			 const struct ib_gid_attr *attr)
220*3cca2606SAchiad Shochat {
221*3cca2606SAchiad Shochat 	struct mlx5_ib_dev *dev	= to_mdev(device);
222*3cca2606SAchiad Shochat 	u32  in[MLX5_ST_SZ_DW(set_roce_address_in)];
223*3cca2606SAchiad Shochat 	u32 out[MLX5_ST_SZ_DW(set_roce_address_out)];
224*3cca2606SAchiad Shochat 	void *in_addr = MLX5_ADDR_OF(set_roce_address_in, in, roce_address);
225*3cca2606SAchiad Shochat 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(device, port_num);
226*3cca2606SAchiad Shochat 
227*3cca2606SAchiad Shochat 	if (ll != IB_LINK_LAYER_ETHERNET)
228*3cca2606SAchiad Shochat 		return -EINVAL;
229*3cca2606SAchiad Shochat 
230*3cca2606SAchiad Shochat 	memset(in, 0, sizeof(in));
231*3cca2606SAchiad Shochat 
232*3cca2606SAchiad Shochat 	ib_gid_to_mlx5_roce_addr(gid, attr, in_addr);
233*3cca2606SAchiad Shochat 
234*3cca2606SAchiad Shochat 	MLX5_SET(set_roce_address_in, in, roce_address_index, index);
235*3cca2606SAchiad Shochat 	MLX5_SET(set_roce_address_in, in, opcode, MLX5_CMD_OP_SET_ROCE_ADDRESS);
236*3cca2606SAchiad Shochat 
237*3cca2606SAchiad Shochat 	memset(out, 0, sizeof(out));
238*3cca2606SAchiad Shochat 	return mlx5_cmd_exec(dev->mdev, in, sizeof(in), out, sizeof(out));
239*3cca2606SAchiad Shochat }
240*3cca2606SAchiad Shochat 
241*3cca2606SAchiad Shochat static int mlx5_ib_add_gid(struct ib_device *device, u8 port_num,
242*3cca2606SAchiad Shochat 			   unsigned int index, const union ib_gid *gid,
243*3cca2606SAchiad Shochat 			   const struct ib_gid_attr *attr,
244*3cca2606SAchiad Shochat 			   __always_unused void **context)
245*3cca2606SAchiad Shochat {
246*3cca2606SAchiad Shochat 	return set_roce_addr(device, port_num, index, gid, attr);
247*3cca2606SAchiad Shochat }
248*3cca2606SAchiad Shochat 
249*3cca2606SAchiad Shochat static int mlx5_ib_del_gid(struct ib_device *device, u8 port_num,
250*3cca2606SAchiad Shochat 			   unsigned int index, __always_unused void **context)
251*3cca2606SAchiad Shochat {
252*3cca2606SAchiad Shochat 	return set_roce_addr(device, port_num, index, NULL, NULL);
253*3cca2606SAchiad Shochat }
254*3cca2606SAchiad Shochat 
2551b5daf11SMajd Dibbiny static int mlx5_use_mad_ifc(struct mlx5_ib_dev *dev)
2561b5daf11SMajd Dibbiny {
2571b5daf11SMajd Dibbiny 	return !dev->mdev->issi;
2581b5daf11SMajd Dibbiny }
2591b5daf11SMajd Dibbiny 
2601b5daf11SMajd Dibbiny enum {
2611b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_MAD,
2621b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_HCA,
2631b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_NIC,
2641b5daf11SMajd Dibbiny };
2651b5daf11SMajd Dibbiny 
2661b5daf11SMajd Dibbiny static int mlx5_get_vport_access_method(struct ib_device *ibdev)
2671b5daf11SMajd Dibbiny {
2681b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(to_mdev(ibdev)))
2691b5daf11SMajd Dibbiny 		return MLX5_VPORT_ACCESS_METHOD_MAD;
2701b5daf11SMajd Dibbiny 
271ebd61f68SAchiad Shochat 	if (mlx5_ib_port_link_layer(ibdev, 1) ==
2721b5daf11SMajd Dibbiny 	    IB_LINK_LAYER_ETHERNET)
2731b5daf11SMajd Dibbiny 		return MLX5_VPORT_ACCESS_METHOD_NIC;
2741b5daf11SMajd Dibbiny 
2751b5daf11SMajd Dibbiny 	return MLX5_VPORT_ACCESS_METHOD_HCA;
2761b5daf11SMajd Dibbiny }
2771b5daf11SMajd Dibbiny 
2781b5daf11SMajd Dibbiny static int mlx5_query_system_image_guid(struct ib_device *ibdev,
2791b5daf11SMajd Dibbiny 					__be64 *sys_image_guid)
2801b5daf11SMajd Dibbiny {
2811b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
2821b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
2831b5daf11SMajd Dibbiny 	u64 tmp;
2841b5daf11SMajd Dibbiny 	int err;
2851b5daf11SMajd Dibbiny 
2861b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
2871b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
2881b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_system_image_guid(ibdev,
2891b5daf11SMajd Dibbiny 							    sys_image_guid);
2901b5daf11SMajd Dibbiny 
2911b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
2921b5daf11SMajd Dibbiny 		err = mlx5_query_hca_vport_system_image_guid(mdev, &tmp);
2933f89a643SAchiad Shochat 		break;
2943f89a643SAchiad Shochat 
2953f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
2963f89a643SAchiad Shochat 		err = mlx5_query_nic_vport_system_image_guid(mdev, &tmp);
2973f89a643SAchiad Shochat 		break;
2981b5daf11SMajd Dibbiny 
2991b5daf11SMajd Dibbiny 	default:
3001b5daf11SMajd Dibbiny 		return -EINVAL;
3011b5daf11SMajd Dibbiny 	}
3023f89a643SAchiad Shochat 
3033f89a643SAchiad Shochat 	if (!err)
3043f89a643SAchiad Shochat 		*sys_image_guid = cpu_to_be64(tmp);
3053f89a643SAchiad Shochat 
3063f89a643SAchiad Shochat 	return err;
3073f89a643SAchiad Shochat 
3081b5daf11SMajd Dibbiny }
3091b5daf11SMajd Dibbiny 
3101b5daf11SMajd Dibbiny static int mlx5_query_max_pkeys(struct ib_device *ibdev,
3111b5daf11SMajd Dibbiny 				u16 *max_pkeys)
3121b5daf11SMajd Dibbiny {
3131b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
3141b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
3151b5daf11SMajd Dibbiny 
3161b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
3171b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
3181b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_max_pkeys(ibdev, max_pkeys);
3191b5daf11SMajd Dibbiny 
3201b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
3211b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
3221b5daf11SMajd Dibbiny 		*max_pkeys = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev,
3231b5daf11SMajd Dibbiny 						pkey_table_size));
3241b5daf11SMajd Dibbiny 		return 0;
3251b5daf11SMajd Dibbiny 
3261b5daf11SMajd Dibbiny 	default:
3271b5daf11SMajd Dibbiny 		return -EINVAL;
3281b5daf11SMajd Dibbiny 	}
3291b5daf11SMajd Dibbiny }
3301b5daf11SMajd Dibbiny 
3311b5daf11SMajd Dibbiny static int mlx5_query_vendor_id(struct ib_device *ibdev,
3321b5daf11SMajd Dibbiny 				u32 *vendor_id)
3331b5daf11SMajd Dibbiny {
3341b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
3351b5daf11SMajd Dibbiny 
3361b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
3371b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
3381b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_vendor_id(ibdev, vendor_id);
3391b5daf11SMajd Dibbiny 
3401b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
3411b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
3421b5daf11SMajd Dibbiny 		return mlx5_core_query_vendor_id(dev->mdev, vendor_id);
3431b5daf11SMajd Dibbiny 
3441b5daf11SMajd Dibbiny 	default:
3451b5daf11SMajd Dibbiny 		return -EINVAL;
3461b5daf11SMajd Dibbiny 	}
3471b5daf11SMajd Dibbiny }
3481b5daf11SMajd Dibbiny 
3491b5daf11SMajd Dibbiny static int mlx5_query_node_guid(struct mlx5_ib_dev *dev,
3501b5daf11SMajd Dibbiny 				__be64 *node_guid)
3511b5daf11SMajd Dibbiny {
3521b5daf11SMajd Dibbiny 	u64 tmp;
3531b5daf11SMajd Dibbiny 	int err;
3541b5daf11SMajd Dibbiny 
3551b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(&dev->ib_dev)) {
3561b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
3571b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_node_guid(dev, node_guid);
3581b5daf11SMajd Dibbiny 
3591b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
3601b5daf11SMajd Dibbiny 		err = mlx5_query_hca_vport_node_guid(dev->mdev, &tmp);
3613f89a643SAchiad Shochat 		break;
3623f89a643SAchiad Shochat 
3633f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
3643f89a643SAchiad Shochat 		err = mlx5_query_nic_vport_node_guid(dev->mdev, &tmp);
3653f89a643SAchiad Shochat 		break;
3661b5daf11SMajd Dibbiny 
3671b5daf11SMajd Dibbiny 	default:
3681b5daf11SMajd Dibbiny 		return -EINVAL;
3691b5daf11SMajd Dibbiny 	}
3703f89a643SAchiad Shochat 
3713f89a643SAchiad Shochat 	if (!err)
3723f89a643SAchiad Shochat 		*node_guid = cpu_to_be64(tmp);
3733f89a643SAchiad Shochat 
3743f89a643SAchiad Shochat 	return err;
3751b5daf11SMajd Dibbiny }
3761b5daf11SMajd Dibbiny 
3771b5daf11SMajd Dibbiny struct mlx5_reg_node_desc {
3781b5daf11SMajd Dibbiny 	u8	desc[64];
3791b5daf11SMajd Dibbiny };
3801b5daf11SMajd Dibbiny 
3811b5daf11SMajd Dibbiny static int mlx5_query_node_desc(struct mlx5_ib_dev *dev, char *node_desc)
3821b5daf11SMajd Dibbiny {
3831b5daf11SMajd Dibbiny 	struct mlx5_reg_node_desc in;
3841b5daf11SMajd Dibbiny 
3851b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(dev))
3861b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_node_desc(dev, node_desc);
3871b5daf11SMajd Dibbiny 
3881b5daf11SMajd Dibbiny 	memset(&in, 0, sizeof(in));
3891b5daf11SMajd Dibbiny 
3901b5daf11SMajd Dibbiny 	return mlx5_core_access_reg(dev->mdev, &in, sizeof(in), node_desc,
3911b5daf11SMajd Dibbiny 				    sizeof(struct mlx5_reg_node_desc),
3921b5daf11SMajd Dibbiny 				    MLX5_REG_NODE_DESC, 0, 0);
3931b5daf11SMajd Dibbiny }
3941b5daf11SMajd Dibbiny 
395e126ba97SEli Cohen static int mlx5_ib_query_device(struct ib_device *ibdev,
3962528e33eSMatan Barak 				struct ib_device_attr *props,
3972528e33eSMatan Barak 				struct ib_udata *uhw)
398e126ba97SEli Cohen {
399e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
400938fe83cSSaeed Mahameed 	struct mlx5_core_dev *mdev = dev->mdev;
401e126ba97SEli Cohen 	int err = -ENOMEM;
402e126ba97SEli Cohen 	int max_rq_sg;
403e126ba97SEli Cohen 	int max_sq_sg;
404e0238a6aSSagi Grimberg 	u64 min_page_size = 1ull << MLX5_CAP_GEN(mdev, log_pg_sz);
405e126ba97SEli Cohen 
4062528e33eSMatan Barak 	if (uhw->inlen || uhw->outlen)
4072528e33eSMatan Barak 		return -EINVAL;
4082528e33eSMatan Barak 
409e126ba97SEli Cohen 	memset(props, 0, sizeof(*props));
4101b5daf11SMajd Dibbiny 	err = mlx5_query_system_image_guid(ibdev,
4111b5daf11SMajd Dibbiny 					   &props->sys_image_guid);
4121b5daf11SMajd Dibbiny 	if (err)
4131b5daf11SMajd Dibbiny 		return err;
4141b5daf11SMajd Dibbiny 
4151b5daf11SMajd Dibbiny 	err = mlx5_query_max_pkeys(ibdev, &props->max_pkeys);
4161b5daf11SMajd Dibbiny 	if (err)
4171b5daf11SMajd Dibbiny 		return err;
4181b5daf11SMajd Dibbiny 
4191b5daf11SMajd Dibbiny 	err = mlx5_query_vendor_id(ibdev, &props->vendor_id);
4201b5daf11SMajd Dibbiny 	if (err)
4211b5daf11SMajd Dibbiny 		return err;
422e126ba97SEli Cohen 
4239603b61dSJack Morgenstein 	props->fw_ver = ((u64)fw_rev_maj(dev->mdev) << 32) |
4249603b61dSJack Morgenstein 		(fw_rev_min(dev->mdev) << 16) |
4259603b61dSJack Morgenstein 		fw_rev_sub(dev->mdev);
426e126ba97SEli Cohen 	props->device_cap_flags    = IB_DEVICE_CHANGE_PHY_PORT |
427e126ba97SEli Cohen 		IB_DEVICE_PORT_ACTIVE_EVENT		|
428e126ba97SEli Cohen 		IB_DEVICE_SYS_IMAGE_GUID		|
4291a4c3a3dSEli Cohen 		IB_DEVICE_RC_RNR_NAK_GEN;
430938fe83cSSaeed Mahameed 
431938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, pkv))
432e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
433938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, qkv))
434e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
435938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, apm))
436e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
437938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, xrc))
438e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_XRC;
439e126ba97SEli Cohen 	props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
440938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, sho)) {
4412dea9094SSagi Grimberg 		props->device_cap_flags |= IB_DEVICE_SIGNATURE_HANDOVER;
4422dea9094SSagi Grimberg 		/* At this stage no support for signature handover */
4432dea9094SSagi Grimberg 		props->sig_prot_cap = IB_PROT_T10DIF_TYPE_1 |
4442dea9094SSagi Grimberg 				      IB_PROT_T10DIF_TYPE_2 |
4452dea9094SSagi Grimberg 				      IB_PROT_T10DIF_TYPE_3;
4462dea9094SSagi Grimberg 		props->sig_guard_cap = IB_GUARD_T10DIF_CRC |
4472dea9094SSagi Grimberg 				       IB_GUARD_T10DIF_CSUM;
4482dea9094SSagi Grimberg 	}
449938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, block_lb_mc))
450f360d88aSEli Cohen 		props->device_cap_flags |= IB_DEVICE_BLOCK_MULTICAST_LOOPBACK;
451e126ba97SEli Cohen 
4521b5daf11SMajd Dibbiny 	props->vendor_part_id	   = mdev->pdev->device;
4531b5daf11SMajd Dibbiny 	props->hw_ver		   = mdev->pdev->revision;
454e126ba97SEli Cohen 
455e126ba97SEli Cohen 	props->max_mr_size	   = ~0ull;
456e0238a6aSSagi Grimberg 	props->page_size_cap	   = ~(min_page_size - 1);
457938fe83cSSaeed Mahameed 	props->max_qp		   = 1 << MLX5_CAP_GEN(mdev, log_max_qp);
458938fe83cSSaeed Mahameed 	props->max_qp_wr	   = 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
459938fe83cSSaeed Mahameed 	max_rq_sg =  MLX5_CAP_GEN(mdev, max_wqe_sz_rq) /
460938fe83cSSaeed Mahameed 		     sizeof(struct mlx5_wqe_data_seg);
461938fe83cSSaeed Mahameed 	max_sq_sg = (MLX5_CAP_GEN(mdev, max_wqe_sz_sq) -
462938fe83cSSaeed Mahameed 		     sizeof(struct mlx5_wqe_ctrl_seg)) /
463e126ba97SEli Cohen 		     sizeof(struct mlx5_wqe_data_seg);
464e126ba97SEli Cohen 	props->max_sge = min(max_rq_sg, max_sq_sg);
46518ebd407SSagi Grimberg 	props->max_sge_rd = props->max_sge;
466938fe83cSSaeed Mahameed 	props->max_cq		   = 1 << MLX5_CAP_GEN(mdev, log_max_cq);
467938fe83cSSaeed Mahameed 	props->max_cqe = (1 << MLX5_CAP_GEN(mdev, log_max_eq_sz)) - 1;
468938fe83cSSaeed Mahameed 	props->max_mr		   = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
469938fe83cSSaeed Mahameed 	props->max_pd		   = 1 << MLX5_CAP_GEN(mdev, log_max_pd);
470938fe83cSSaeed Mahameed 	props->max_qp_rd_atom	   = 1 << MLX5_CAP_GEN(mdev, log_max_ra_req_qp);
471938fe83cSSaeed Mahameed 	props->max_qp_init_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_res_qp);
472938fe83cSSaeed Mahameed 	props->max_srq		   = 1 << MLX5_CAP_GEN(mdev, log_max_srq);
473938fe83cSSaeed Mahameed 	props->max_srq_wr = (1 << MLX5_CAP_GEN(mdev, log_max_srq_sz)) - 1;
474938fe83cSSaeed Mahameed 	props->local_ca_ack_delay  = MLX5_CAP_GEN(mdev, local_ca_ack_delay);
475e126ba97SEli Cohen 	props->max_res_rd_atom	   = props->max_qp_rd_atom * props->max_qp;
476e126ba97SEli Cohen 	props->max_srq_sge	   = max_rq_sg - 1;
477e126ba97SEli Cohen 	props->max_fast_reg_page_list_len = (unsigned int)-1;
47881bea28fSEli Cohen 	props->atomic_cap	   = IB_ATOMIC_NONE;
47981bea28fSEli Cohen 	props->masked_atomic_cap   = IB_ATOMIC_NONE;
480938fe83cSSaeed Mahameed 	props->max_mcast_grp	   = 1 << MLX5_CAP_GEN(mdev, log_max_mcg);
481938fe83cSSaeed Mahameed 	props->max_mcast_qp_attach = MLX5_CAP_GEN(mdev, max_qp_mcg);
482e126ba97SEli Cohen 	props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
483e126ba97SEli Cohen 					   props->max_mcast_grp;
484e126ba97SEli Cohen 	props->max_map_per_fmr = INT_MAX; /* no limit in ConnectIB */
485e126ba97SEli Cohen 
4868cdd312cSHaggai Eran #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
487938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, pg))
4888cdd312cSHaggai Eran 		props->device_cap_flags |= IB_DEVICE_ON_DEMAND_PAGING;
4898cdd312cSHaggai Eran 	props->odp_caps = dev->odp_caps;
4908cdd312cSHaggai Eran #endif
4918cdd312cSHaggai Eran 
4921b5daf11SMajd Dibbiny 	return 0;
4931b5daf11SMajd Dibbiny }
494e126ba97SEli Cohen 
4951b5daf11SMajd Dibbiny enum mlx5_ib_width {
4961b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_1X	= 1 << 0,
4971b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_2X	= 1 << 1,
4981b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_4X	= 1 << 2,
4991b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_8X	= 1 << 3,
5001b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_12X	= 1 << 4
5011b5daf11SMajd Dibbiny };
5021b5daf11SMajd Dibbiny 
5031b5daf11SMajd Dibbiny static int translate_active_width(struct ib_device *ibdev, u8 active_width,
5041b5daf11SMajd Dibbiny 				  u8 *ib_width)
5051b5daf11SMajd Dibbiny {
5061b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
5071b5daf11SMajd Dibbiny 	int err = 0;
5081b5daf11SMajd Dibbiny 
5091b5daf11SMajd Dibbiny 	if (active_width & MLX5_IB_WIDTH_1X) {
5101b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_1X;
5111b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_2X) {
5121b5daf11SMajd Dibbiny 		mlx5_ib_dbg(dev, "active_width %d is not supported by IB spec\n",
5131b5daf11SMajd Dibbiny 			    (int)active_width);
5141b5daf11SMajd Dibbiny 		err = -EINVAL;
5151b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_4X) {
5161b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_4X;
5171b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_8X) {
5181b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_8X;
5191b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_12X) {
5201b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_12X;
5211b5daf11SMajd Dibbiny 	} else {
5221b5daf11SMajd Dibbiny 		mlx5_ib_dbg(dev, "Invalid active_width %d\n",
5231b5daf11SMajd Dibbiny 			    (int)active_width);
5241b5daf11SMajd Dibbiny 		err = -EINVAL;
5251b5daf11SMajd Dibbiny 	}
5261b5daf11SMajd Dibbiny 
5271b5daf11SMajd Dibbiny 	return err;
5281b5daf11SMajd Dibbiny }
5291b5daf11SMajd Dibbiny 
5301b5daf11SMajd Dibbiny static int mlx5_mtu_to_ib_mtu(int mtu)
5311b5daf11SMajd Dibbiny {
5321b5daf11SMajd Dibbiny 	switch (mtu) {
5331b5daf11SMajd Dibbiny 	case 256: return 1;
5341b5daf11SMajd Dibbiny 	case 512: return 2;
5351b5daf11SMajd Dibbiny 	case 1024: return 3;
5361b5daf11SMajd Dibbiny 	case 2048: return 4;
5371b5daf11SMajd Dibbiny 	case 4096: return 5;
5381b5daf11SMajd Dibbiny 	default:
5391b5daf11SMajd Dibbiny 		pr_warn("invalid mtu\n");
5401b5daf11SMajd Dibbiny 		return -1;
5411b5daf11SMajd Dibbiny 	}
5421b5daf11SMajd Dibbiny }
5431b5daf11SMajd Dibbiny 
5441b5daf11SMajd Dibbiny enum ib_max_vl_num {
5451b5daf11SMajd Dibbiny 	__IB_MAX_VL_0		= 1,
5461b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_1		= 2,
5471b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_3		= 3,
5481b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_7		= 4,
5491b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_14	= 5,
5501b5daf11SMajd Dibbiny };
5511b5daf11SMajd Dibbiny 
5521b5daf11SMajd Dibbiny enum mlx5_vl_hw_cap {
5531b5daf11SMajd Dibbiny 	MLX5_VL_HW_0	= 1,
5541b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_1	= 2,
5551b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_2	= 3,
5561b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_3	= 4,
5571b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_4	= 5,
5581b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_5	= 6,
5591b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_6	= 7,
5601b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_7	= 8,
5611b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_14	= 15
5621b5daf11SMajd Dibbiny };
5631b5daf11SMajd Dibbiny 
5641b5daf11SMajd Dibbiny static int translate_max_vl_num(struct ib_device *ibdev, u8 vl_hw_cap,
5651b5daf11SMajd Dibbiny 				u8 *max_vl_num)
5661b5daf11SMajd Dibbiny {
5671b5daf11SMajd Dibbiny 	switch (vl_hw_cap) {
5681b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0:
5691b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0;
5701b5daf11SMajd Dibbiny 		break;
5711b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_1:
5721b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_1;
5731b5daf11SMajd Dibbiny 		break;
5741b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_3:
5751b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_3;
5761b5daf11SMajd Dibbiny 		break;
5771b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_7:
5781b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_7;
5791b5daf11SMajd Dibbiny 		break;
5801b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_14:
5811b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_14;
5821b5daf11SMajd Dibbiny 		break;
5831b5daf11SMajd Dibbiny 
5841b5daf11SMajd Dibbiny 	default:
5851b5daf11SMajd Dibbiny 		return -EINVAL;
5861b5daf11SMajd Dibbiny 	}
5871b5daf11SMajd Dibbiny 
5881b5daf11SMajd Dibbiny 	return 0;
5891b5daf11SMajd Dibbiny }
5901b5daf11SMajd Dibbiny 
5911b5daf11SMajd Dibbiny static int mlx5_query_hca_port(struct ib_device *ibdev, u8 port,
5921b5daf11SMajd Dibbiny 			       struct ib_port_attr *props)
5931b5daf11SMajd Dibbiny {
5941b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
5951b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
5961b5daf11SMajd Dibbiny 	struct mlx5_hca_vport_context *rep;
5971b5daf11SMajd Dibbiny 	int max_mtu;
5981b5daf11SMajd Dibbiny 	int oper_mtu;
5991b5daf11SMajd Dibbiny 	int err;
6001b5daf11SMajd Dibbiny 	u8 ib_link_width_oper;
6011b5daf11SMajd Dibbiny 	u8 vl_hw_cap;
6021b5daf11SMajd Dibbiny 
6031b5daf11SMajd Dibbiny 	rep = kzalloc(sizeof(*rep), GFP_KERNEL);
6041b5daf11SMajd Dibbiny 	if (!rep) {
6051b5daf11SMajd Dibbiny 		err = -ENOMEM;
6061b5daf11SMajd Dibbiny 		goto out;
6071b5daf11SMajd Dibbiny 	}
6081b5daf11SMajd Dibbiny 
6091b5daf11SMajd Dibbiny 	memset(props, 0, sizeof(*props));
6101b5daf11SMajd Dibbiny 
6111b5daf11SMajd Dibbiny 	err = mlx5_query_hca_vport_context(mdev, 0, port, 0, rep);
6121b5daf11SMajd Dibbiny 	if (err)
6131b5daf11SMajd Dibbiny 		goto out;
6141b5daf11SMajd Dibbiny 
6151b5daf11SMajd Dibbiny 	props->lid		= rep->lid;
6161b5daf11SMajd Dibbiny 	props->lmc		= rep->lmc;
6171b5daf11SMajd Dibbiny 	props->sm_lid		= rep->sm_lid;
6181b5daf11SMajd Dibbiny 	props->sm_sl		= rep->sm_sl;
6191b5daf11SMajd Dibbiny 	props->state		= rep->vport_state;
6201b5daf11SMajd Dibbiny 	props->phys_state	= rep->port_physical_state;
6211b5daf11SMajd Dibbiny 	props->port_cap_flags	= rep->cap_mask1;
6221b5daf11SMajd Dibbiny 	props->gid_tbl_len	= mlx5_get_gid_table_len(MLX5_CAP_GEN(mdev, gid_table_size));
6231b5daf11SMajd Dibbiny 	props->max_msg_sz	= 1 << MLX5_CAP_GEN(mdev, log_max_msg);
6241b5daf11SMajd Dibbiny 	props->pkey_tbl_len	= mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, pkey_table_size));
6251b5daf11SMajd Dibbiny 	props->bad_pkey_cntr	= rep->pkey_violation_counter;
6261b5daf11SMajd Dibbiny 	props->qkey_viol_cntr	= rep->qkey_violation_counter;
6271b5daf11SMajd Dibbiny 	props->subnet_timeout	= rep->subnet_timeout;
6281b5daf11SMajd Dibbiny 	props->init_type_reply	= rep->init_type_reply;
6291b5daf11SMajd Dibbiny 
6301b5daf11SMajd Dibbiny 	err = mlx5_query_port_link_width_oper(mdev, &ib_link_width_oper, port);
6311b5daf11SMajd Dibbiny 	if (err)
6321b5daf11SMajd Dibbiny 		goto out;
6331b5daf11SMajd Dibbiny 
6341b5daf11SMajd Dibbiny 	err = translate_active_width(ibdev, ib_link_width_oper,
6351b5daf11SMajd Dibbiny 				     &props->active_width);
6361b5daf11SMajd Dibbiny 	if (err)
6371b5daf11SMajd Dibbiny 		goto out;
6381b5daf11SMajd Dibbiny 	err = mlx5_query_port_proto_oper(mdev, &props->active_speed, MLX5_PTYS_IB,
6391b5daf11SMajd Dibbiny 					 port);
6401b5daf11SMajd Dibbiny 	if (err)
6411b5daf11SMajd Dibbiny 		goto out;
6421b5daf11SMajd Dibbiny 
643facc9699SSaeed Mahameed 	mlx5_query_port_max_mtu(mdev, &max_mtu, port);
6441b5daf11SMajd Dibbiny 
6451b5daf11SMajd Dibbiny 	props->max_mtu = mlx5_mtu_to_ib_mtu(max_mtu);
6461b5daf11SMajd Dibbiny 
647facc9699SSaeed Mahameed 	mlx5_query_port_oper_mtu(mdev, &oper_mtu, port);
6481b5daf11SMajd Dibbiny 
6491b5daf11SMajd Dibbiny 	props->active_mtu = mlx5_mtu_to_ib_mtu(oper_mtu);
6501b5daf11SMajd Dibbiny 
6511b5daf11SMajd Dibbiny 	err = mlx5_query_port_vl_hw_cap(mdev, &vl_hw_cap, port);
6521b5daf11SMajd Dibbiny 	if (err)
6531b5daf11SMajd Dibbiny 		goto out;
6541b5daf11SMajd Dibbiny 
6551b5daf11SMajd Dibbiny 	err = translate_max_vl_num(ibdev, vl_hw_cap,
6561b5daf11SMajd Dibbiny 				   &props->max_vl_num);
6571b5daf11SMajd Dibbiny out:
6581b5daf11SMajd Dibbiny 	kfree(rep);
659e126ba97SEli Cohen 	return err;
660e126ba97SEli Cohen }
661e126ba97SEli Cohen 
662e126ba97SEli Cohen int mlx5_ib_query_port(struct ib_device *ibdev, u8 port,
663e126ba97SEli Cohen 		       struct ib_port_attr *props)
664e126ba97SEli Cohen {
6651b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
6661b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
6671b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_port(ibdev, port, props);
668e126ba97SEli Cohen 
6691b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
6701b5daf11SMajd Dibbiny 		return mlx5_query_hca_port(ibdev, port, props);
6711b5daf11SMajd Dibbiny 
6723f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
6733f89a643SAchiad Shochat 		return mlx5_query_port_roce(ibdev, port, props);
6743f89a643SAchiad Shochat 
6751b5daf11SMajd Dibbiny 	default:
676e126ba97SEli Cohen 		return -EINVAL;
677e126ba97SEli Cohen 	}
678e126ba97SEli Cohen }
679e126ba97SEli Cohen 
680e126ba97SEli Cohen static int mlx5_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
681e126ba97SEli Cohen 			     union ib_gid *gid)
682e126ba97SEli Cohen {
6831b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
6841b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
685e126ba97SEli Cohen 
6861b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
6871b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
6881b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_gids(ibdev, port, index, gid);
689e126ba97SEli Cohen 
6901b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
6911b5daf11SMajd Dibbiny 		return mlx5_query_hca_vport_gid(mdev, 0, port, 0, index, gid);
692e126ba97SEli Cohen 
6931b5daf11SMajd Dibbiny 	default:
6941b5daf11SMajd Dibbiny 		return -EINVAL;
6951b5daf11SMajd Dibbiny 	}
696e126ba97SEli Cohen 
697e126ba97SEli Cohen }
698e126ba97SEli Cohen 
699e126ba97SEli Cohen static int mlx5_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
700e126ba97SEli Cohen 			      u16 *pkey)
701e126ba97SEli Cohen {
7021b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
7031b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
704e126ba97SEli Cohen 
7051b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
7061b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
7071b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_pkey(ibdev, port, index, pkey);
708e126ba97SEli Cohen 
7091b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
7101b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
7111b5daf11SMajd Dibbiny 		return mlx5_query_hca_vport_pkey(mdev, 0, port,  0, index,
7121b5daf11SMajd Dibbiny 						 pkey);
7131b5daf11SMajd Dibbiny 	default:
7141b5daf11SMajd Dibbiny 		return -EINVAL;
715e126ba97SEli Cohen 	}
7161b5daf11SMajd Dibbiny }
717e126ba97SEli Cohen 
718e126ba97SEli Cohen static int mlx5_ib_modify_device(struct ib_device *ibdev, int mask,
719e126ba97SEli Cohen 				 struct ib_device_modify *props)
720e126ba97SEli Cohen {
721e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
722e126ba97SEli Cohen 	struct mlx5_reg_node_desc in;
723e126ba97SEli Cohen 	struct mlx5_reg_node_desc out;
724e126ba97SEli Cohen 	int err;
725e126ba97SEli Cohen 
726e126ba97SEli Cohen 	if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
727e126ba97SEli Cohen 		return -EOPNOTSUPP;
728e126ba97SEli Cohen 
729e126ba97SEli Cohen 	if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
730e126ba97SEli Cohen 		return 0;
731e126ba97SEli Cohen 
732e126ba97SEli Cohen 	/*
733e126ba97SEli Cohen 	 * If possible, pass node desc to FW, so it can generate
734e126ba97SEli Cohen 	 * a 144 trap.  If cmd fails, just ignore.
735e126ba97SEli Cohen 	 */
736e126ba97SEli Cohen 	memcpy(&in, props->node_desc, 64);
7379603b61dSJack Morgenstein 	err = mlx5_core_access_reg(dev->mdev, &in, sizeof(in), &out,
738e126ba97SEli Cohen 				   sizeof(out), MLX5_REG_NODE_DESC, 0, 1);
739e126ba97SEli Cohen 	if (err)
740e126ba97SEli Cohen 		return err;
741e126ba97SEli Cohen 
742e126ba97SEli Cohen 	memcpy(ibdev->node_desc, props->node_desc, 64);
743e126ba97SEli Cohen 
744e126ba97SEli Cohen 	return err;
745e126ba97SEli Cohen }
746e126ba97SEli Cohen 
747e126ba97SEli Cohen static int mlx5_ib_modify_port(struct ib_device *ibdev, u8 port, int mask,
748e126ba97SEli Cohen 			       struct ib_port_modify *props)
749e126ba97SEli Cohen {
750e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
751e126ba97SEli Cohen 	struct ib_port_attr attr;
752e126ba97SEli Cohen 	u32 tmp;
753e126ba97SEli Cohen 	int err;
754e126ba97SEli Cohen 
755e126ba97SEli Cohen 	mutex_lock(&dev->cap_mask_mutex);
756e126ba97SEli Cohen 
757e126ba97SEli Cohen 	err = mlx5_ib_query_port(ibdev, port, &attr);
758e126ba97SEli Cohen 	if (err)
759e126ba97SEli Cohen 		goto out;
760e126ba97SEli Cohen 
761e126ba97SEli Cohen 	tmp = (attr.port_cap_flags | props->set_port_cap_mask) &
762e126ba97SEli Cohen 		~props->clr_port_cap_mask;
763e126ba97SEli Cohen 
7649603b61dSJack Morgenstein 	err = mlx5_set_port_caps(dev->mdev, port, tmp);
765e126ba97SEli Cohen 
766e126ba97SEli Cohen out:
767e126ba97SEli Cohen 	mutex_unlock(&dev->cap_mask_mutex);
768e126ba97SEli Cohen 	return err;
769e126ba97SEli Cohen }
770e126ba97SEli Cohen 
771e126ba97SEli Cohen static struct ib_ucontext *mlx5_ib_alloc_ucontext(struct ib_device *ibdev,
772e126ba97SEli Cohen 						  struct ib_udata *udata)
773e126ba97SEli Cohen {
774e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
77578c0f98cSEli Cohen 	struct mlx5_ib_alloc_ucontext_req_v2 req;
776e126ba97SEli Cohen 	struct mlx5_ib_alloc_ucontext_resp resp;
777e126ba97SEli Cohen 	struct mlx5_ib_ucontext *context;
778e126ba97SEli Cohen 	struct mlx5_uuar_info *uuari;
779e126ba97SEli Cohen 	struct mlx5_uar *uars;
780c1be5232SEli Cohen 	int gross_uuars;
781e126ba97SEli Cohen 	int num_uars;
78278c0f98cSEli Cohen 	int ver;
783e126ba97SEli Cohen 	int uuarn;
784e126ba97SEli Cohen 	int err;
785e126ba97SEli Cohen 	int i;
786f241e749SJack Morgenstein 	size_t reqlen;
787e126ba97SEli Cohen 
788e126ba97SEli Cohen 	if (!dev->ib_active)
789e126ba97SEli Cohen 		return ERR_PTR(-EAGAIN);
790e126ba97SEli Cohen 
79178c0f98cSEli Cohen 	memset(&req, 0, sizeof(req));
79278c0f98cSEli Cohen 	reqlen = udata->inlen - sizeof(struct ib_uverbs_cmd_hdr);
79378c0f98cSEli Cohen 	if (reqlen == sizeof(struct mlx5_ib_alloc_ucontext_req))
79478c0f98cSEli Cohen 		ver = 0;
79578c0f98cSEli Cohen 	else if (reqlen == sizeof(struct mlx5_ib_alloc_ucontext_req_v2))
79678c0f98cSEli Cohen 		ver = 2;
79778c0f98cSEli Cohen 	else
79878c0f98cSEli Cohen 		return ERR_PTR(-EINVAL);
79978c0f98cSEli Cohen 
80078c0f98cSEli Cohen 	err = ib_copy_from_udata(&req, udata, reqlen);
801e126ba97SEli Cohen 	if (err)
802e126ba97SEli Cohen 		return ERR_PTR(err);
803e126ba97SEli Cohen 
80478c0f98cSEli Cohen 	if (req.flags || req.reserved)
80578c0f98cSEli Cohen 		return ERR_PTR(-EINVAL);
80678c0f98cSEli Cohen 
807e126ba97SEli Cohen 	if (req.total_num_uuars > MLX5_MAX_UUARS)
808e126ba97SEli Cohen 		return ERR_PTR(-ENOMEM);
809e126ba97SEli Cohen 
810e126ba97SEli Cohen 	if (req.total_num_uuars == 0)
811e126ba97SEli Cohen 		return ERR_PTR(-EINVAL);
812e126ba97SEli Cohen 
813c1be5232SEli Cohen 	req.total_num_uuars = ALIGN(req.total_num_uuars,
814c1be5232SEli Cohen 				    MLX5_NON_FP_BF_REGS_PER_PAGE);
815e126ba97SEli Cohen 	if (req.num_low_latency_uuars > req.total_num_uuars - 1)
816e126ba97SEli Cohen 		return ERR_PTR(-EINVAL);
817e126ba97SEli Cohen 
818c1be5232SEli Cohen 	num_uars = req.total_num_uuars / MLX5_NON_FP_BF_REGS_PER_PAGE;
819c1be5232SEli Cohen 	gross_uuars = num_uars * MLX5_BF_REGS_PER_PAGE;
820938fe83cSSaeed Mahameed 	resp.qp_tab_size = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp);
821938fe83cSSaeed Mahameed 	resp.bf_reg_size = 1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size);
822e126ba97SEli Cohen 	resp.cache_line_size = L1_CACHE_BYTES;
823938fe83cSSaeed Mahameed 	resp.max_sq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq);
824938fe83cSSaeed Mahameed 	resp.max_rq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq);
825938fe83cSSaeed Mahameed 	resp.max_send_wqebb = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
826938fe83cSSaeed Mahameed 	resp.max_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
827938fe83cSSaeed Mahameed 	resp.max_srq_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz);
828e126ba97SEli Cohen 
829e126ba97SEli Cohen 	context = kzalloc(sizeof(*context), GFP_KERNEL);
830e126ba97SEli Cohen 	if (!context)
831e126ba97SEli Cohen 		return ERR_PTR(-ENOMEM);
832e126ba97SEli Cohen 
833e126ba97SEli Cohen 	uuari = &context->uuari;
834e126ba97SEli Cohen 	mutex_init(&uuari->lock);
835e126ba97SEli Cohen 	uars = kcalloc(num_uars, sizeof(*uars), GFP_KERNEL);
836e126ba97SEli Cohen 	if (!uars) {
837e126ba97SEli Cohen 		err = -ENOMEM;
838e126ba97SEli Cohen 		goto out_ctx;
839e126ba97SEli Cohen 	}
840e126ba97SEli Cohen 
841c1be5232SEli Cohen 	uuari->bitmap = kcalloc(BITS_TO_LONGS(gross_uuars),
842e126ba97SEli Cohen 				sizeof(*uuari->bitmap),
843e126ba97SEli Cohen 				GFP_KERNEL);
844e126ba97SEli Cohen 	if (!uuari->bitmap) {
845e126ba97SEli Cohen 		err = -ENOMEM;
846e126ba97SEli Cohen 		goto out_uar_ctx;
847e126ba97SEli Cohen 	}
848e126ba97SEli Cohen 	/*
849e126ba97SEli Cohen 	 * clear all fast path uuars
850e126ba97SEli Cohen 	 */
851c1be5232SEli Cohen 	for (i = 0; i < gross_uuars; i++) {
852e126ba97SEli Cohen 		uuarn = i & 3;
853e126ba97SEli Cohen 		if (uuarn == 2 || uuarn == 3)
854e126ba97SEli Cohen 			set_bit(i, uuari->bitmap);
855e126ba97SEli Cohen 	}
856e126ba97SEli Cohen 
857c1be5232SEli Cohen 	uuari->count = kcalloc(gross_uuars, sizeof(*uuari->count), GFP_KERNEL);
858e126ba97SEli Cohen 	if (!uuari->count) {
859e126ba97SEli Cohen 		err = -ENOMEM;
860e126ba97SEli Cohen 		goto out_bitmap;
861e126ba97SEli Cohen 	}
862e126ba97SEli Cohen 
863e126ba97SEli Cohen 	for (i = 0; i < num_uars; i++) {
8649603b61dSJack Morgenstein 		err = mlx5_cmd_alloc_uar(dev->mdev, &uars[i].index);
865e126ba97SEli Cohen 		if (err)
866e126ba97SEli Cohen 			goto out_count;
867e126ba97SEli Cohen 	}
868e126ba97SEli Cohen 
869b4cfe447SHaggai Eran #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
870b4cfe447SHaggai Eran 	context->ibucontext.invalidate_range = &mlx5_ib_invalidate_range;
871b4cfe447SHaggai Eran #endif
872b4cfe447SHaggai Eran 
873e126ba97SEli Cohen 	INIT_LIST_HEAD(&context->db_page_list);
874e126ba97SEli Cohen 	mutex_init(&context->db_page_mutex);
875e126ba97SEli Cohen 
876e126ba97SEli Cohen 	resp.tot_uuars = req.total_num_uuars;
877938fe83cSSaeed Mahameed 	resp.num_ports = MLX5_CAP_GEN(dev->mdev, num_ports);
87892b0ca7cSDan Carpenter 	err = ib_copy_to_udata(udata, &resp,
87992b0ca7cSDan Carpenter 			       sizeof(resp) - sizeof(resp.reserved));
880e126ba97SEli Cohen 	if (err)
881e126ba97SEli Cohen 		goto out_uars;
882e126ba97SEli Cohen 
88378c0f98cSEli Cohen 	uuari->ver = ver;
884e126ba97SEli Cohen 	uuari->num_low_latency_uuars = req.num_low_latency_uuars;
885e126ba97SEli Cohen 	uuari->uars = uars;
886e126ba97SEli Cohen 	uuari->num_uars = num_uars;
887e126ba97SEli Cohen 	return &context->ibucontext;
888e126ba97SEli Cohen 
889e126ba97SEli Cohen out_uars:
890e126ba97SEli Cohen 	for (i--; i >= 0; i--)
8919603b61dSJack Morgenstein 		mlx5_cmd_free_uar(dev->mdev, uars[i].index);
892e126ba97SEli Cohen out_count:
893e126ba97SEli Cohen 	kfree(uuari->count);
894e126ba97SEli Cohen 
895e126ba97SEli Cohen out_bitmap:
896e126ba97SEli Cohen 	kfree(uuari->bitmap);
897e126ba97SEli Cohen 
898e126ba97SEli Cohen out_uar_ctx:
899e126ba97SEli Cohen 	kfree(uars);
900e126ba97SEli Cohen 
901e126ba97SEli Cohen out_ctx:
902e126ba97SEli Cohen 	kfree(context);
903e126ba97SEli Cohen 	return ERR_PTR(err);
904e126ba97SEli Cohen }
905e126ba97SEli Cohen 
906e126ba97SEli Cohen static int mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
907e126ba97SEli Cohen {
908e126ba97SEli Cohen 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
909e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
910e126ba97SEli Cohen 	struct mlx5_uuar_info *uuari = &context->uuari;
911e126ba97SEli Cohen 	int i;
912e126ba97SEli Cohen 
913e126ba97SEli Cohen 	for (i = 0; i < uuari->num_uars; i++) {
9149603b61dSJack Morgenstein 		if (mlx5_cmd_free_uar(dev->mdev, uuari->uars[i].index))
915e126ba97SEli Cohen 			mlx5_ib_warn(dev, "failed to free UAR 0x%x\n", uuari->uars[i].index);
916e126ba97SEli Cohen 	}
917e126ba97SEli Cohen 
918e126ba97SEli Cohen 	kfree(uuari->count);
919e126ba97SEli Cohen 	kfree(uuari->bitmap);
920e126ba97SEli Cohen 	kfree(uuari->uars);
921e126ba97SEli Cohen 	kfree(context);
922e126ba97SEli Cohen 
923e126ba97SEli Cohen 	return 0;
924e126ba97SEli Cohen }
925e126ba97SEli Cohen 
926e126ba97SEli Cohen static phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev, int index)
927e126ba97SEli Cohen {
9289603b61dSJack Morgenstein 	return (pci_resource_start(dev->mdev->pdev, 0) >> PAGE_SHIFT) + index;
929e126ba97SEli Cohen }
930e126ba97SEli Cohen 
931e126ba97SEli Cohen static int get_command(unsigned long offset)
932e126ba97SEli Cohen {
933e126ba97SEli Cohen 	return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK;
934e126ba97SEli Cohen }
935e126ba97SEli Cohen 
936e126ba97SEli Cohen static int get_arg(unsigned long offset)
937e126ba97SEli Cohen {
938e126ba97SEli Cohen 	return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1);
939e126ba97SEli Cohen }
940e126ba97SEli Cohen 
941e126ba97SEli Cohen static int get_index(unsigned long offset)
942e126ba97SEli Cohen {
943e126ba97SEli Cohen 	return get_arg(offset);
944e126ba97SEli Cohen }
945e126ba97SEli Cohen 
946e126ba97SEli Cohen static int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma)
947e126ba97SEli Cohen {
948e126ba97SEli Cohen 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
949e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
950e126ba97SEli Cohen 	struct mlx5_uuar_info *uuari = &context->uuari;
951e126ba97SEli Cohen 	unsigned long command;
952e126ba97SEli Cohen 	unsigned long idx;
953e126ba97SEli Cohen 	phys_addr_t pfn;
954e126ba97SEli Cohen 
955e126ba97SEli Cohen 	command = get_command(vma->vm_pgoff);
956e126ba97SEli Cohen 	switch (command) {
957e126ba97SEli Cohen 	case MLX5_IB_MMAP_REGULAR_PAGE:
958e126ba97SEli Cohen 		if (vma->vm_end - vma->vm_start != PAGE_SIZE)
959e126ba97SEli Cohen 			return -EINVAL;
960e126ba97SEli Cohen 
961e126ba97SEli Cohen 		idx = get_index(vma->vm_pgoff);
9621c3ce90dSEli Cohen 		if (idx >= uuari->num_uars)
9631c3ce90dSEli Cohen 			return -EINVAL;
9641c3ce90dSEli Cohen 
965e126ba97SEli Cohen 		pfn = uar_index2pfn(dev, uuari->uars[idx].index);
966e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn 0x%llx\n", idx,
967e126ba97SEli Cohen 			    (unsigned long long)pfn);
968e126ba97SEli Cohen 
969e126ba97SEli Cohen 		vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
970e126ba97SEli Cohen 		if (io_remap_pfn_range(vma, vma->vm_start, pfn,
971e126ba97SEli Cohen 				       PAGE_SIZE, vma->vm_page_prot))
972e126ba97SEli Cohen 			return -EAGAIN;
973e126ba97SEli Cohen 
974e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "mapped WC at 0x%lx, PA 0x%llx\n",
975e126ba97SEli Cohen 			    vma->vm_start,
976e126ba97SEli Cohen 			    (unsigned long long)pfn << PAGE_SHIFT);
977e126ba97SEli Cohen 		break;
978e126ba97SEli Cohen 
979e126ba97SEli Cohen 	case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES:
980e126ba97SEli Cohen 		return -ENOSYS;
981e126ba97SEli Cohen 
982e126ba97SEli Cohen 	default:
983e126ba97SEli Cohen 		return -EINVAL;
984e126ba97SEli Cohen 	}
985e126ba97SEli Cohen 
986e126ba97SEli Cohen 	return 0;
987e126ba97SEli Cohen }
988e126ba97SEli Cohen 
989e126ba97SEli Cohen static struct ib_pd *mlx5_ib_alloc_pd(struct ib_device *ibdev,
990e126ba97SEli Cohen 				      struct ib_ucontext *context,
991e126ba97SEli Cohen 				      struct ib_udata *udata)
992e126ba97SEli Cohen {
993e126ba97SEli Cohen 	struct mlx5_ib_alloc_pd_resp resp;
994e126ba97SEli Cohen 	struct mlx5_ib_pd *pd;
995e126ba97SEli Cohen 	int err;
996e126ba97SEli Cohen 
997e126ba97SEli Cohen 	pd = kmalloc(sizeof(*pd), GFP_KERNEL);
998e126ba97SEli Cohen 	if (!pd)
999e126ba97SEli Cohen 		return ERR_PTR(-ENOMEM);
1000e126ba97SEli Cohen 
10019603b61dSJack Morgenstein 	err = mlx5_core_alloc_pd(to_mdev(ibdev)->mdev, &pd->pdn);
1002e126ba97SEli Cohen 	if (err) {
1003e126ba97SEli Cohen 		kfree(pd);
1004e126ba97SEli Cohen 		return ERR_PTR(err);
1005e126ba97SEli Cohen 	}
1006e126ba97SEli Cohen 
1007e126ba97SEli Cohen 	if (context) {
1008e126ba97SEli Cohen 		resp.pdn = pd->pdn;
1009e126ba97SEli Cohen 		if (ib_copy_to_udata(udata, &resp, sizeof(resp))) {
10109603b61dSJack Morgenstein 			mlx5_core_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn);
1011e126ba97SEli Cohen 			kfree(pd);
1012e126ba97SEli Cohen 			return ERR_PTR(-EFAULT);
1013e126ba97SEli Cohen 		}
1014e126ba97SEli Cohen 	}
1015e126ba97SEli Cohen 
1016e126ba97SEli Cohen 	return &pd->ibpd;
1017e126ba97SEli Cohen }
1018e126ba97SEli Cohen 
1019e126ba97SEli Cohen static int mlx5_ib_dealloc_pd(struct ib_pd *pd)
1020e126ba97SEli Cohen {
1021e126ba97SEli Cohen 	struct mlx5_ib_dev *mdev = to_mdev(pd->device);
1022e126ba97SEli Cohen 	struct mlx5_ib_pd *mpd = to_mpd(pd);
1023e126ba97SEli Cohen 
10249603b61dSJack Morgenstein 	mlx5_core_dealloc_pd(mdev->mdev, mpd->pdn);
1025e126ba97SEli Cohen 	kfree(mpd);
1026e126ba97SEli Cohen 
1027e126ba97SEli Cohen 	return 0;
1028e126ba97SEli Cohen }
1029e126ba97SEli Cohen 
1030e126ba97SEli Cohen static int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
1031e126ba97SEli Cohen {
1032e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
1033e126ba97SEli Cohen 	int err;
1034e126ba97SEli Cohen 
10359603b61dSJack Morgenstein 	err = mlx5_core_attach_mcg(dev->mdev, gid, ibqp->qp_num);
1036e126ba97SEli Cohen 	if (err)
1037e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n",
1038e126ba97SEli Cohen 			     ibqp->qp_num, gid->raw);
1039e126ba97SEli Cohen 
1040e126ba97SEli Cohen 	return err;
1041e126ba97SEli Cohen }
1042e126ba97SEli Cohen 
1043e126ba97SEli Cohen static int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
1044e126ba97SEli Cohen {
1045e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
1046e126ba97SEli Cohen 	int err;
1047e126ba97SEli Cohen 
10489603b61dSJack Morgenstein 	err = mlx5_core_detach_mcg(dev->mdev, gid, ibqp->qp_num);
1049e126ba97SEli Cohen 	if (err)
1050e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n",
1051e126ba97SEli Cohen 			     ibqp->qp_num, gid->raw);
1052e126ba97SEli Cohen 
1053e126ba97SEli Cohen 	return err;
1054e126ba97SEli Cohen }
1055e126ba97SEli Cohen 
1056e126ba97SEli Cohen static int init_node_data(struct mlx5_ib_dev *dev)
1057e126ba97SEli Cohen {
10581b5daf11SMajd Dibbiny 	int err;
1059e126ba97SEli Cohen 
10601b5daf11SMajd Dibbiny 	err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc);
1061e126ba97SEli Cohen 	if (err)
1062e126ba97SEli Cohen 		return err;
10631b5daf11SMajd Dibbiny 
10641b5daf11SMajd Dibbiny 	dev->mdev->rev_id = dev->mdev->pdev->revision;
10651b5daf11SMajd Dibbiny 
10661b5daf11SMajd Dibbiny 	return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid);
1067e126ba97SEli Cohen }
1068e126ba97SEli Cohen 
1069e126ba97SEli Cohen static ssize_t show_fw_pages(struct device *device, struct device_attribute *attr,
1070e126ba97SEli Cohen 			     char *buf)
1071e126ba97SEli Cohen {
1072e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
1073e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
1074e126ba97SEli Cohen 
10759603b61dSJack Morgenstein 	return sprintf(buf, "%d\n", dev->mdev->priv.fw_pages);
1076e126ba97SEli Cohen }
1077e126ba97SEli Cohen 
1078e126ba97SEli Cohen static ssize_t show_reg_pages(struct device *device,
1079e126ba97SEli Cohen 			      struct device_attribute *attr, char *buf)
1080e126ba97SEli Cohen {
1081e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
1082e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
1083e126ba97SEli Cohen 
10846aec21f6SHaggai Eran 	return sprintf(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages));
1085e126ba97SEli Cohen }
1086e126ba97SEli Cohen 
1087e126ba97SEli Cohen static ssize_t show_hca(struct device *device, struct device_attribute *attr,
1088e126ba97SEli Cohen 			char *buf)
1089e126ba97SEli Cohen {
1090e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
1091e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
10929603b61dSJack Morgenstein 	return sprintf(buf, "MT%d\n", dev->mdev->pdev->device);
1093e126ba97SEli Cohen }
1094e126ba97SEli Cohen 
1095e126ba97SEli Cohen static ssize_t show_fw_ver(struct device *device, struct device_attribute *attr,
1096e126ba97SEli Cohen 			   char *buf)
1097e126ba97SEli Cohen {
1098e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
1099e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
11009603b61dSJack Morgenstein 	return sprintf(buf, "%d.%d.%d\n", fw_rev_maj(dev->mdev),
11019603b61dSJack Morgenstein 		       fw_rev_min(dev->mdev), fw_rev_sub(dev->mdev));
1102e126ba97SEli Cohen }
1103e126ba97SEli Cohen 
1104e126ba97SEli Cohen static ssize_t show_rev(struct device *device, struct device_attribute *attr,
1105e126ba97SEli Cohen 			char *buf)
1106e126ba97SEli Cohen {
1107e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
1108e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
11099603b61dSJack Morgenstein 	return sprintf(buf, "%x\n", dev->mdev->rev_id);
1110e126ba97SEli Cohen }
1111e126ba97SEli Cohen 
1112e126ba97SEli Cohen static ssize_t show_board(struct device *device, struct device_attribute *attr,
1113e126ba97SEli Cohen 			  char *buf)
1114e126ba97SEli Cohen {
1115e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
1116e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
1117e126ba97SEli Cohen 	return sprintf(buf, "%.*s\n", MLX5_BOARD_ID_LEN,
11189603b61dSJack Morgenstein 		       dev->mdev->board_id);
1119e126ba97SEli Cohen }
1120e126ba97SEli Cohen 
1121e126ba97SEli Cohen static DEVICE_ATTR(hw_rev,   S_IRUGO, show_rev,    NULL);
1122e126ba97SEli Cohen static DEVICE_ATTR(fw_ver,   S_IRUGO, show_fw_ver, NULL);
1123e126ba97SEli Cohen static DEVICE_ATTR(hca_type, S_IRUGO, show_hca,    NULL);
1124e126ba97SEli Cohen static DEVICE_ATTR(board_id, S_IRUGO, show_board,  NULL);
1125e126ba97SEli Cohen static DEVICE_ATTR(fw_pages, S_IRUGO, show_fw_pages, NULL);
1126e126ba97SEli Cohen static DEVICE_ATTR(reg_pages, S_IRUGO, show_reg_pages, NULL);
1127e126ba97SEli Cohen 
1128e126ba97SEli Cohen static struct device_attribute *mlx5_class_attributes[] = {
1129e126ba97SEli Cohen 	&dev_attr_hw_rev,
1130e126ba97SEli Cohen 	&dev_attr_fw_ver,
1131e126ba97SEli Cohen 	&dev_attr_hca_type,
1132e126ba97SEli Cohen 	&dev_attr_board_id,
1133e126ba97SEli Cohen 	&dev_attr_fw_pages,
1134e126ba97SEli Cohen 	&dev_attr_reg_pages,
1135e126ba97SEli Cohen };
1136e126ba97SEli Cohen 
11379603b61dSJack Morgenstein static void mlx5_ib_event(struct mlx5_core_dev *dev, void *context,
11384d2f9bbbSJack Morgenstein 			  enum mlx5_dev_event event, unsigned long param)
1139e126ba97SEli Cohen {
11409603b61dSJack Morgenstein 	struct mlx5_ib_dev *ibdev = (struct mlx5_ib_dev *)context;
1141e126ba97SEli Cohen 	struct ib_event ibev;
11429603b61dSJack Morgenstein 
1143e126ba97SEli Cohen 	u8 port = 0;
1144e126ba97SEli Cohen 
1145e126ba97SEli Cohen 	switch (event) {
1146e126ba97SEli Cohen 	case MLX5_DEV_EVENT_SYS_ERROR:
1147e126ba97SEli Cohen 		ibdev->ib_active = false;
1148e126ba97SEli Cohen 		ibev.event = IB_EVENT_DEVICE_FATAL;
1149e126ba97SEli Cohen 		break;
1150e126ba97SEli Cohen 
1151e126ba97SEli Cohen 	case MLX5_DEV_EVENT_PORT_UP:
1152e126ba97SEli Cohen 		ibev.event = IB_EVENT_PORT_ACTIVE;
11534d2f9bbbSJack Morgenstein 		port = (u8)param;
1154e126ba97SEli Cohen 		break;
1155e126ba97SEli Cohen 
1156e126ba97SEli Cohen 	case MLX5_DEV_EVENT_PORT_DOWN:
1157e126ba97SEli Cohen 		ibev.event = IB_EVENT_PORT_ERR;
11584d2f9bbbSJack Morgenstein 		port = (u8)param;
1159e126ba97SEli Cohen 		break;
1160e126ba97SEli Cohen 
1161e126ba97SEli Cohen 	case MLX5_DEV_EVENT_PORT_INITIALIZED:
1162e126ba97SEli Cohen 		/* not used by ULPs */
1163e126ba97SEli Cohen 		return;
1164e126ba97SEli Cohen 
1165e126ba97SEli Cohen 	case MLX5_DEV_EVENT_LID_CHANGE:
1166e126ba97SEli Cohen 		ibev.event = IB_EVENT_LID_CHANGE;
11674d2f9bbbSJack Morgenstein 		port = (u8)param;
1168e126ba97SEli Cohen 		break;
1169e126ba97SEli Cohen 
1170e126ba97SEli Cohen 	case MLX5_DEV_EVENT_PKEY_CHANGE:
1171e126ba97SEli Cohen 		ibev.event = IB_EVENT_PKEY_CHANGE;
11724d2f9bbbSJack Morgenstein 		port = (u8)param;
1173e126ba97SEli Cohen 		break;
1174e126ba97SEli Cohen 
1175e126ba97SEli Cohen 	case MLX5_DEV_EVENT_GUID_CHANGE:
1176e126ba97SEli Cohen 		ibev.event = IB_EVENT_GID_CHANGE;
11774d2f9bbbSJack Morgenstein 		port = (u8)param;
1178e126ba97SEli Cohen 		break;
1179e126ba97SEli Cohen 
1180e126ba97SEli Cohen 	case MLX5_DEV_EVENT_CLIENT_REREG:
1181e126ba97SEli Cohen 		ibev.event = IB_EVENT_CLIENT_REREGISTER;
11824d2f9bbbSJack Morgenstein 		port = (u8)param;
1183e126ba97SEli Cohen 		break;
1184e126ba97SEli Cohen 	}
1185e126ba97SEli Cohen 
1186e126ba97SEli Cohen 	ibev.device	      = &ibdev->ib_dev;
1187e126ba97SEli Cohen 	ibev.element.port_num = port;
1188e126ba97SEli Cohen 
1189a0c84c32SEli Cohen 	if (port < 1 || port > ibdev->num_ports) {
1190a0c84c32SEli Cohen 		mlx5_ib_warn(ibdev, "warning: event on port %d\n", port);
1191a0c84c32SEli Cohen 		return;
1192a0c84c32SEli Cohen 	}
1193a0c84c32SEli Cohen 
1194e126ba97SEli Cohen 	if (ibdev->ib_active)
1195e126ba97SEli Cohen 		ib_dispatch_event(&ibev);
1196e126ba97SEli Cohen }
1197e126ba97SEli Cohen 
1198e126ba97SEli Cohen static void get_ext_port_caps(struct mlx5_ib_dev *dev)
1199e126ba97SEli Cohen {
1200e126ba97SEli Cohen 	int port;
1201e126ba97SEli Cohen 
1202938fe83cSSaeed Mahameed 	for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++)
1203e126ba97SEli Cohen 		mlx5_query_ext_port_caps(dev, port);
1204e126ba97SEli Cohen }
1205e126ba97SEli Cohen 
1206e126ba97SEli Cohen static int get_port_caps(struct mlx5_ib_dev *dev)
1207e126ba97SEli Cohen {
1208e126ba97SEli Cohen 	struct ib_device_attr *dprops = NULL;
1209e126ba97SEli Cohen 	struct ib_port_attr *pprops = NULL;
1210f614fc15SDan Carpenter 	int err = -ENOMEM;
1211e126ba97SEli Cohen 	int port;
12122528e33eSMatan Barak 	struct ib_udata uhw = {.inlen = 0, .outlen = 0};
1213e126ba97SEli Cohen 
1214e126ba97SEli Cohen 	pprops = kmalloc(sizeof(*pprops), GFP_KERNEL);
1215e126ba97SEli Cohen 	if (!pprops)
1216e126ba97SEli Cohen 		goto out;
1217e126ba97SEli Cohen 
1218e126ba97SEli Cohen 	dprops = kmalloc(sizeof(*dprops), GFP_KERNEL);
1219e126ba97SEli Cohen 	if (!dprops)
1220e126ba97SEli Cohen 		goto out;
1221e126ba97SEli Cohen 
12222528e33eSMatan Barak 	err = mlx5_ib_query_device(&dev->ib_dev, dprops, &uhw);
1223e126ba97SEli Cohen 	if (err) {
1224e126ba97SEli Cohen 		mlx5_ib_warn(dev, "query_device failed %d\n", err);
1225e126ba97SEli Cohen 		goto out;
1226e126ba97SEli Cohen 	}
1227e126ba97SEli Cohen 
1228938fe83cSSaeed Mahameed 	for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++) {
1229e126ba97SEli Cohen 		err = mlx5_ib_query_port(&dev->ib_dev, port, pprops);
1230e126ba97SEli Cohen 		if (err) {
1231938fe83cSSaeed Mahameed 			mlx5_ib_warn(dev, "query_port %d failed %d\n",
1232938fe83cSSaeed Mahameed 				     port, err);
1233e126ba97SEli Cohen 			break;
1234e126ba97SEli Cohen 		}
1235938fe83cSSaeed Mahameed 		dev->mdev->port_caps[port - 1].pkey_table_len =
1236938fe83cSSaeed Mahameed 						dprops->max_pkeys;
1237938fe83cSSaeed Mahameed 		dev->mdev->port_caps[port - 1].gid_table_len =
1238938fe83cSSaeed Mahameed 						pprops->gid_tbl_len;
1239e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "pkey_table_len %d, gid_table_len %d\n",
1240e126ba97SEli Cohen 			    dprops->max_pkeys, pprops->gid_tbl_len);
1241e126ba97SEli Cohen 	}
1242e126ba97SEli Cohen 
1243e126ba97SEli Cohen out:
1244e126ba97SEli Cohen 	kfree(pprops);
1245e126ba97SEli Cohen 	kfree(dprops);
1246e126ba97SEli Cohen 
1247e126ba97SEli Cohen 	return err;
1248e126ba97SEli Cohen }
1249e126ba97SEli Cohen 
1250e126ba97SEli Cohen static void destroy_umrc_res(struct mlx5_ib_dev *dev)
1251e126ba97SEli Cohen {
1252e126ba97SEli Cohen 	int err;
1253e126ba97SEli Cohen 
1254e126ba97SEli Cohen 	err = mlx5_mr_cache_cleanup(dev);
1255e126ba97SEli Cohen 	if (err)
1256e126ba97SEli Cohen 		mlx5_ib_warn(dev, "mr cache cleanup failed\n");
1257e126ba97SEli Cohen 
1258e126ba97SEli Cohen 	mlx5_ib_destroy_qp(dev->umrc.qp);
1259e126ba97SEli Cohen 	ib_destroy_cq(dev->umrc.cq);
1260e126ba97SEli Cohen 	ib_dealloc_pd(dev->umrc.pd);
1261e126ba97SEli Cohen }
1262e126ba97SEli Cohen 
1263e126ba97SEli Cohen enum {
1264e126ba97SEli Cohen 	MAX_UMR_WR = 128,
1265e126ba97SEli Cohen };
1266e126ba97SEli Cohen 
1267e126ba97SEli Cohen static int create_umr_res(struct mlx5_ib_dev *dev)
1268e126ba97SEli Cohen {
1269e126ba97SEli Cohen 	struct ib_qp_init_attr *init_attr = NULL;
1270e126ba97SEli Cohen 	struct ib_qp_attr *attr = NULL;
1271e126ba97SEli Cohen 	struct ib_pd *pd;
1272e126ba97SEli Cohen 	struct ib_cq *cq;
1273e126ba97SEli Cohen 	struct ib_qp *qp;
12748e37210bSMatan Barak 	struct ib_cq_init_attr cq_attr = {};
1275e126ba97SEli Cohen 	int ret;
1276e126ba97SEli Cohen 
1277e126ba97SEli Cohen 	attr = kzalloc(sizeof(*attr), GFP_KERNEL);
1278e126ba97SEli Cohen 	init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL);
1279e126ba97SEli Cohen 	if (!attr || !init_attr) {
1280e126ba97SEli Cohen 		ret = -ENOMEM;
1281e126ba97SEli Cohen 		goto error_0;
1282e126ba97SEli Cohen 	}
1283e126ba97SEli Cohen 
1284e126ba97SEli Cohen 	pd = ib_alloc_pd(&dev->ib_dev);
1285e126ba97SEli Cohen 	if (IS_ERR(pd)) {
1286e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create PD for sync UMR QP\n");
1287e126ba97SEli Cohen 		ret = PTR_ERR(pd);
1288e126ba97SEli Cohen 		goto error_0;
1289e126ba97SEli Cohen 	}
1290e126ba97SEli Cohen 
12918e37210bSMatan Barak 	cq_attr.cqe = 128;
12928e37210bSMatan Barak 	cq = ib_create_cq(&dev->ib_dev, mlx5_umr_cq_handler, NULL, NULL,
12938e37210bSMatan Barak 			  &cq_attr);
1294e126ba97SEli Cohen 	if (IS_ERR(cq)) {
1295e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create CQ for sync UMR QP\n");
1296e126ba97SEli Cohen 		ret = PTR_ERR(cq);
1297e126ba97SEli Cohen 		goto error_2;
1298e126ba97SEli Cohen 	}
1299e126ba97SEli Cohen 	ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
1300e126ba97SEli Cohen 
1301e126ba97SEli Cohen 	init_attr->send_cq = cq;
1302e126ba97SEli Cohen 	init_attr->recv_cq = cq;
1303e126ba97SEli Cohen 	init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
1304e126ba97SEli Cohen 	init_attr->cap.max_send_wr = MAX_UMR_WR;
1305e126ba97SEli Cohen 	init_attr->cap.max_send_sge = 1;
1306e126ba97SEli Cohen 	init_attr->qp_type = MLX5_IB_QPT_REG_UMR;
1307e126ba97SEli Cohen 	init_attr->port_num = 1;
1308e126ba97SEli Cohen 	qp = mlx5_ib_create_qp(pd, init_attr, NULL);
1309e126ba97SEli Cohen 	if (IS_ERR(qp)) {
1310e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create sync UMR QP\n");
1311e126ba97SEli Cohen 		ret = PTR_ERR(qp);
1312e126ba97SEli Cohen 		goto error_3;
1313e126ba97SEli Cohen 	}
1314e126ba97SEli Cohen 	qp->device     = &dev->ib_dev;
1315e126ba97SEli Cohen 	qp->real_qp    = qp;
1316e126ba97SEli Cohen 	qp->uobject    = NULL;
1317e126ba97SEli Cohen 	qp->qp_type    = MLX5_IB_QPT_REG_UMR;
1318e126ba97SEli Cohen 
1319e126ba97SEli Cohen 	attr->qp_state = IB_QPS_INIT;
1320e126ba97SEli Cohen 	attr->port_num = 1;
1321e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_PKEY_INDEX |
1322e126ba97SEli Cohen 				IB_QP_PORT, NULL);
1323e126ba97SEli Cohen 	if (ret) {
1324e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify UMR QP\n");
1325e126ba97SEli Cohen 		goto error_4;
1326e126ba97SEli Cohen 	}
1327e126ba97SEli Cohen 
1328e126ba97SEli Cohen 	memset(attr, 0, sizeof(*attr));
1329e126ba97SEli Cohen 	attr->qp_state = IB_QPS_RTR;
1330e126ba97SEli Cohen 	attr->path_mtu = IB_MTU_256;
1331e126ba97SEli Cohen 
1332e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
1333e126ba97SEli Cohen 	if (ret) {
1334e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify umr QP to rtr\n");
1335e126ba97SEli Cohen 		goto error_4;
1336e126ba97SEli Cohen 	}
1337e126ba97SEli Cohen 
1338e126ba97SEli Cohen 	memset(attr, 0, sizeof(*attr));
1339e126ba97SEli Cohen 	attr->qp_state = IB_QPS_RTS;
1340e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
1341e126ba97SEli Cohen 	if (ret) {
1342e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify umr QP to rts\n");
1343e126ba97SEli Cohen 		goto error_4;
1344e126ba97SEli Cohen 	}
1345e126ba97SEli Cohen 
1346e126ba97SEli Cohen 	dev->umrc.qp = qp;
1347e126ba97SEli Cohen 	dev->umrc.cq = cq;
1348e126ba97SEli Cohen 	dev->umrc.pd = pd;
1349e126ba97SEli Cohen 
1350e126ba97SEli Cohen 	sema_init(&dev->umrc.sem, MAX_UMR_WR);
1351e126ba97SEli Cohen 	ret = mlx5_mr_cache_init(dev);
1352e126ba97SEli Cohen 	if (ret) {
1353e126ba97SEli Cohen 		mlx5_ib_warn(dev, "mr cache init failed %d\n", ret);
1354e126ba97SEli Cohen 		goto error_4;
1355e126ba97SEli Cohen 	}
1356e126ba97SEli Cohen 
1357e126ba97SEli Cohen 	kfree(attr);
1358e126ba97SEli Cohen 	kfree(init_attr);
1359e126ba97SEli Cohen 
1360e126ba97SEli Cohen 	return 0;
1361e126ba97SEli Cohen 
1362e126ba97SEli Cohen error_4:
1363e126ba97SEli Cohen 	mlx5_ib_destroy_qp(qp);
1364e126ba97SEli Cohen 
1365e126ba97SEli Cohen error_3:
1366e126ba97SEli Cohen 	ib_destroy_cq(cq);
1367e126ba97SEli Cohen 
1368e126ba97SEli Cohen error_2:
1369e126ba97SEli Cohen 	ib_dealloc_pd(pd);
1370e126ba97SEli Cohen 
1371e126ba97SEli Cohen error_0:
1372e126ba97SEli Cohen 	kfree(attr);
1373e126ba97SEli Cohen 	kfree(init_attr);
1374e126ba97SEli Cohen 	return ret;
1375e126ba97SEli Cohen }
1376e126ba97SEli Cohen 
1377e126ba97SEli Cohen static int create_dev_resources(struct mlx5_ib_resources *devr)
1378e126ba97SEli Cohen {
1379e126ba97SEli Cohen 	struct ib_srq_init_attr attr;
1380e126ba97SEli Cohen 	struct mlx5_ib_dev *dev;
1381bcf4c1eaSMatan Barak 	struct ib_cq_init_attr cq_attr = {.cqe = 1};
1382e126ba97SEli Cohen 	int ret = 0;
1383e126ba97SEli Cohen 
1384e126ba97SEli Cohen 	dev = container_of(devr, struct mlx5_ib_dev, devr);
1385e126ba97SEli Cohen 
1386e126ba97SEli Cohen 	devr->p0 = mlx5_ib_alloc_pd(&dev->ib_dev, NULL, NULL);
1387e126ba97SEli Cohen 	if (IS_ERR(devr->p0)) {
1388e126ba97SEli Cohen 		ret = PTR_ERR(devr->p0);
1389e126ba97SEli Cohen 		goto error0;
1390e126ba97SEli Cohen 	}
1391e126ba97SEli Cohen 	devr->p0->device  = &dev->ib_dev;
1392e126ba97SEli Cohen 	devr->p0->uobject = NULL;
1393e126ba97SEli Cohen 	atomic_set(&devr->p0->usecnt, 0);
1394e126ba97SEli Cohen 
1395bcf4c1eaSMatan Barak 	devr->c0 = mlx5_ib_create_cq(&dev->ib_dev, &cq_attr, NULL, NULL);
1396e126ba97SEli Cohen 	if (IS_ERR(devr->c0)) {
1397e126ba97SEli Cohen 		ret = PTR_ERR(devr->c0);
1398e126ba97SEli Cohen 		goto error1;
1399e126ba97SEli Cohen 	}
1400e126ba97SEli Cohen 	devr->c0->device        = &dev->ib_dev;
1401e126ba97SEli Cohen 	devr->c0->uobject       = NULL;
1402e126ba97SEli Cohen 	devr->c0->comp_handler  = NULL;
1403e126ba97SEli Cohen 	devr->c0->event_handler = NULL;
1404e126ba97SEli Cohen 	devr->c0->cq_context    = NULL;
1405e126ba97SEli Cohen 	atomic_set(&devr->c0->usecnt, 0);
1406e126ba97SEli Cohen 
1407e126ba97SEli Cohen 	devr->x0 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
1408e126ba97SEli Cohen 	if (IS_ERR(devr->x0)) {
1409e126ba97SEli Cohen 		ret = PTR_ERR(devr->x0);
1410e126ba97SEli Cohen 		goto error2;
1411e126ba97SEli Cohen 	}
1412e126ba97SEli Cohen 	devr->x0->device = &dev->ib_dev;
1413e126ba97SEli Cohen 	devr->x0->inode = NULL;
1414e126ba97SEli Cohen 	atomic_set(&devr->x0->usecnt, 0);
1415e126ba97SEli Cohen 	mutex_init(&devr->x0->tgt_qp_mutex);
1416e126ba97SEli Cohen 	INIT_LIST_HEAD(&devr->x0->tgt_qp_list);
1417e126ba97SEli Cohen 
1418e126ba97SEli Cohen 	devr->x1 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
1419e126ba97SEli Cohen 	if (IS_ERR(devr->x1)) {
1420e126ba97SEli Cohen 		ret = PTR_ERR(devr->x1);
1421e126ba97SEli Cohen 		goto error3;
1422e126ba97SEli Cohen 	}
1423e126ba97SEli Cohen 	devr->x1->device = &dev->ib_dev;
1424e126ba97SEli Cohen 	devr->x1->inode = NULL;
1425e126ba97SEli Cohen 	atomic_set(&devr->x1->usecnt, 0);
1426e126ba97SEli Cohen 	mutex_init(&devr->x1->tgt_qp_mutex);
1427e126ba97SEli Cohen 	INIT_LIST_HEAD(&devr->x1->tgt_qp_list);
1428e126ba97SEli Cohen 
1429e126ba97SEli Cohen 	memset(&attr, 0, sizeof(attr));
1430e126ba97SEli Cohen 	attr.attr.max_sge = 1;
1431e126ba97SEli Cohen 	attr.attr.max_wr = 1;
1432e126ba97SEli Cohen 	attr.srq_type = IB_SRQT_XRC;
1433e126ba97SEli Cohen 	attr.ext.xrc.cq = devr->c0;
1434e126ba97SEli Cohen 	attr.ext.xrc.xrcd = devr->x0;
1435e126ba97SEli Cohen 
1436e126ba97SEli Cohen 	devr->s0 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
1437e126ba97SEli Cohen 	if (IS_ERR(devr->s0)) {
1438e126ba97SEli Cohen 		ret = PTR_ERR(devr->s0);
1439e126ba97SEli Cohen 		goto error4;
1440e126ba97SEli Cohen 	}
1441e126ba97SEli Cohen 	devr->s0->device	= &dev->ib_dev;
1442e126ba97SEli Cohen 	devr->s0->pd		= devr->p0;
1443e126ba97SEli Cohen 	devr->s0->uobject       = NULL;
1444e126ba97SEli Cohen 	devr->s0->event_handler = NULL;
1445e126ba97SEli Cohen 	devr->s0->srq_context   = NULL;
1446e126ba97SEli Cohen 	devr->s0->srq_type      = IB_SRQT_XRC;
1447e126ba97SEli Cohen 	devr->s0->ext.xrc.xrcd	= devr->x0;
1448e126ba97SEli Cohen 	devr->s0->ext.xrc.cq	= devr->c0;
1449e126ba97SEli Cohen 	atomic_inc(&devr->s0->ext.xrc.xrcd->usecnt);
1450e126ba97SEli Cohen 	atomic_inc(&devr->s0->ext.xrc.cq->usecnt);
1451e126ba97SEli Cohen 	atomic_inc(&devr->p0->usecnt);
1452e126ba97SEli Cohen 	atomic_set(&devr->s0->usecnt, 0);
1453e126ba97SEli Cohen 
14544aa17b28SHaggai Abramonvsky 	memset(&attr, 0, sizeof(attr));
14554aa17b28SHaggai Abramonvsky 	attr.attr.max_sge = 1;
14564aa17b28SHaggai Abramonvsky 	attr.attr.max_wr = 1;
14574aa17b28SHaggai Abramonvsky 	attr.srq_type = IB_SRQT_BASIC;
14584aa17b28SHaggai Abramonvsky 	devr->s1 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
14594aa17b28SHaggai Abramonvsky 	if (IS_ERR(devr->s1)) {
14604aa17b28SHaggai Abramonvsky 		ret = PTR_ERR(devr->s1);
14614aa17b28SHaggai Abramonvsky 		goto error5;
14624aa17b28SHaggai Abramonvsky 	}
14634aa17b28SHaggai Abramonvsky 	devr->s1->device	= &dev->ib_dev;
14644aa17b28SHaggai Abramonvsky 	devr->s1->pd		= devr->p0;
14654aa17b28SHaggai Abramonvsky 	devr->s1->uobject       = NULL;
14664aa17b28SHaggai Abramonvsky 	devr->s1->event_handler = NULL;
14674aa17b28SHaggai Abramonvsky 	devr->s1->srq_context   = NULL;
14684aa17b28SHaggai Abramonvsky 	devr->s1->srq_type      = IB_SRQT_BASIC;
14694aa17b28SHaggai Abramonvsky 	devr->s1->ext.xrc.cq	= devr->c0;
14704aa17b28SHaggai Abramonvsky 	atomic_inc(&devr->p0->usecnt);
14714aa17b28SHaggai Abramonvsky 	atomic_set(&devr->s0->usecnt, 0);
14724aa17b28SHaggai Abramonvsky 
1473e126ba97SEli Cohen 	return 0;
1474e126ba97SEli Cohen 
14754aa17b28SHaggai Abramonvsky error5:
14764aa17b28SHaggai Abramonvsky 	mlx5_ib_destroy_srq(devr->s0);
1477e126ba97SEli Cohen error4:
1478e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x1);
1479e126ba97SEli Cohen error3:
1480e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x0);
1481e126ba97SEli Cohen error2:
1482e126ba97SEli Cohen 	mlx5_ib_destroy_cq(devr->c0);
1483e126ba97SEli Cohen error1:
1484e126ba97SEli Cohen 	mlx5_ib_dealloc_pd(devr->p0);
1485e126ba97SEli Cohen error0:
1486e126ba97SEli Cohen 	return ret;
1487e126ba97SEli Cohen }
1488e126ba97SEli Cohen 
1489e126ba97SEli Cohen static void destroy_dev_resources(struct mlx5_ib_resources *devr)
1490e126ba97SEli Cohen {
14914aa17b28SHaggai Abramonvsky 	mlx5_ib_destroy_srq(devr->s1);
1492e126ba97SEli Cohen 	mlx5_ib_destroy_srq(devr->s0);
1493e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x0);
1494e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x1);
1495e126ba97SEli Cohen 	mlx5_ib_destroy_cq(devr->c0);
1496e126ba97SEli Cohen 	mlx5_ib_dealloc_pd(devr->p0);
1497e126ba97SEli Cohen }
1498e126ba97SEli Cohen 
14997738613eSIra Weiny static int mlx5_port_immutable(struct ib_device *ibdev, u8 port_num,
15007738613eSIra Weiny 			       struct ib_port_immutable *immutable)
15017738613eSIra Weiny {
15027738613eSIra Weiny 	struct ib_port_attr attr;
15037738613eSIra Weiny 	int err;
15047738613eSIra Weiny 
15057738613eSIra Weiny 	err = mlx5_ib_query_port(ibdev, port_num, &attr);
15067738613eSIra Weiny 	if (err)
15077738613eSIra Weiny 		return err;
15087738613eSIra Weiny 
15097738613eSIra Weiny 	immutable->pkey_tbl_len = attr.pkey_tbl_len;
15107738613eSIra Weiny 	immutable->gid_tbl_len = attr.gid_tbl_len;
1511f9b22e35SIra Weiny 	immutable->core_cap_flags = RDMA_CORE_PORT_IBA_IB;
1512337877a4SIra Weiny 	immutable->max_mad_size = IB_MGMT_MAD_SIZE;
15137738613eSIra Weiny 
15147738613eSIra Weiny 	return 0;
15157738613eSIra Weiny }
15167738613eSIra Weiny 
1517fc24fc5eSAchiad Shochat static int mlx5_enable_roce(struct mlx5_ib_dev *dev)
1518fc24fc5eSAchiad Shochat {
1519fc24fc5eSAchiad Shochat 	dev->roce.nb.notifier_call = mlx5_netdev_event;
1520fc24fc5eSAchiad Shochat 	return register_netdevice_notifier(&dev->roce.nb);
1521fc24fc5eSAchiad Shochat }
1522fc24fc5eSAchiad Shochat 
1523fc24fc5eSAchiad Shochat static void mlx5_disable_roce(struct mlx5_ib_dev *dev)
1524fc24fc5eSAchiad Shochat {
1525fc24fc5eSAchiad Shochat 	unregister_netdevice_notifier(&dev->roce.nb);
1526fc24fc5eSAchiad Shochat }
1527fc24fc5eSAchiad Shochat 
15289603b61dSJack Morgenstein static void *mlx5_ib_add(struct mlx5_core_dev *mdev)
1529e126ba97SEli Cohen {
1530e126ba97SEli Cohen 	struct mlx5_ib_dev *dev;
1531ebd61f68SAchiad Shochat 	enum rdma_link_layer ll;
1532ebd61f68SAchiad Shochat 	int port_type_cap;
1533e126ba97SEli Cohen 	int err;
1534e126ba97SEli Cohen 	int i;
1535e126ba97SEli Cohen 
1536ebd61f68SAchiad Shochat 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
1537ebd61f68SAchiad Shochat 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
1538ebd61f68SAchiad Shochat 
1539647241eaSMajd Dibbiny 	/* don't create IB instance over Eth ports, no RoCE yet! */
1540ebd61f68SAchiad Shochat 	if (ll == IB_LINK_LAYER_ETHERNET)
1541647241eaSMajd Dibbiny 		return NULL;
1542647241eaSMajd Dibbiny 
1543e126ba97SEli Cohen 	printk_once(KERN_INFO "%s", mlx5_version);
1544e126ba97SEli Cohen 
1545e126ba97SEli Cohen 	dev = (struct mlx5_ib_dev *)ib_alloc_device(sizeof(*dev));
1546e126ba97SEli Cohen 	if (!dev)
15479603b61dSJack Morgenstein 		return NULL;
1548e126ba97SEli Cohen 
15499603b61dSJack Morgenstein 	dev->mdev = mdev;
1550e126ba97SEli Cohen 
1551fc24fc5eSAchiad Shochat 	rwlock_init(&dev->roce.netdev_lock);
1552e126ba97SEli Cohen 	err = get_port_caps(dev);
1553e126ba97SEli Cohen 	if (err)
15549603b61dSJack Morgenstein 		goto err_dealloc;
1555e126ba97SEli Cohen 
15561b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(dev))
1557e126ba97SEli Cohen 		get_ext_port_caps(dev);
1558e126ba97SEli Cohen 
1559e126ba97SEli Cohen 	MLX5_INIT_DOORBELL_LOCK(&dev->uar_lock);
1560e126ba97SEli Cohen 
1561e126ba97SEli Cohen 	strlcpy(dev->ib_dev.name, "mlx5_%d", IB_DEVICE_NAME_MAX);
1562e126ba97SEli Cohen 	dev->ib_dev.owner		= THIS_MODULE;
1563e126ba97SEli Cohen 	dev->ib_dev.node_type		= RDMA_NODE_IB_CA;
1564c6790aa9SSagi Grimberg 	dev->ib_dev.local_dma_lkey	= 0 /* not supported for now */;
1565938fe83cSSaeed Mahameed 	dev->num_ports		= MLX5_CAP_GEN(mdev, num_ports);
1566e126ba97SEli Cohen 	dev->ib_dev.phys_port_cnt     = dev->num_ports;
1567233d05d2SSaeed Mahameed 	dev->ib_dev.num_comp_vectors    =
1568233d05d2SSaeed Mahameed 		dev->mdev->priv.eq_table.num_comp_vectors;
1569e126ba97SEli Cohen 	dev->ib_dev.dma_device	= &mdev->pdev->dev;
1570e126ba97SEli Cohen 
1571e126ba97SEli Cohen 	dev->ib_dev.uverbs_abi_ver	= MLX5_IB_UVERBS_ABI_VERSION;
1572e126ba97SEli Cohen 	dev->ib_dev.uverbs_cmd_mask	=
1573e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_GET_CONTEXT)		|
1574e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_DEVICE)	|
1575e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_PORT)		|
1576e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_ALLOC_PD)		|
1577e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DEALLOC_PD)		|
1578e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_REG_MR)		|
1579e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DEREG_MR)		|
1580e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL)	|
1581e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_CQ)		|
1582e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_RESIZE_CQ)		|
1583e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_CQ)		|
1584e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_QP)		|
1585e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_MODIFY_QP)		|
1586e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_QP)		|
1587e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_QP)		|
1588e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_ATTACH_MCAST)	|
1589e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DETACH_MCAST)	|
1590e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_SRQ)		|
1591e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_MODIFY_SRQ)		|
1592e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_SRQ)		|
1593e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_SRQ)		|
1594e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_XSRQ)		|
1595e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_OPEN_QP);
15961707cb4aSHaggai Eran 	dev->ib_dev.uverbs_ex_cmd_mask =
15971707cb4aSHaggai Eran 		(1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE);
1598e126ba97SEli Cohen 
1599e126ba97SEli Cohen 	dev->ib_dev.query_device	= mlx5_ib_query_device;
1600e126ba97SEli Cohen 	dev->ib_dev.query_port		= mlx5_ib_query_port;
1601ebd61f68SAchiad Shochat 	dev->ib_dev.get_link_layer	= mlx5_ib_port_link_layer;
1602fc24fc5eSAchiad Shochat 	if (ll == IB_LINK_LAYER_ETHERNET)
1603fc24fc5eSAchiad Shochat 		dev->ib_dev.get_netdev	= mlx5_ib_get_netdev;
1604e126ba97SEli Cohen 	dev->ib_dev.query_gid		= mlx5_ib_query_gid;
1605*3cca2606SAchiad Shochat 	dev->ib_dev.add_gid		= mlx5_ib_add_gid;
1606*3cca2606SAchiad Shochat 	dev->ib_dev.del_gid		= mlx5_ib_del_gid;
1607e126ba97SEli Cohen 	dev->ib_dev.query_pkey		= mlx5_ib_query_pkey;
1608e126ba97SEli Cohen 	dev->ib_dev.modify_device	= mlx5_ib_modify_device;
1609e126ba97SEli Cohen 	dev->ib_dev.modify_port		= mlx5_ib_modify_port;
1610e126ba97SEli Cohen 	dev->ib_dev.alloc_ucontext	= mlx5_ib_alloc_ucontext;
1611e126ba97SEli Cohen 	dev->ib_dev.dealloc_ucontext	= mlx5_ib_dealloc_ucontext;
1612e126ba97SEli Cohen 	dev->ib_dev.mmap		= mlx5_ib_mmap;
1613e126ba97SEli Cohen 	dev->ib_dev.alloc_pd		= mlx5_ib_alloc_pd;
1614e126ba97SEli Cohen 	dev->ib_dev.dealloc_pd		= mlx5_ib_dealloc_pd;
1615e126ba97SEli Cohen 	dev->ib_dev.create_ah		= mlx5_ib_create_ah;
1616e126ba97SEli Cohen 	dev->ib_dev.query_ah		= mlx5_ib_query_ah;
1617e126ba97SEli Cohen 	dev->ib_dev.destroy_ah		= mlx5_ib_destroy_ah;
1618e126ba97SEli Cohen 	dev->ib_dev.create_srq		= mlx5_ib_create_srq;
1619e126ba97SEli Cohen 	dev->ib_dev.modify_srq		= mlx5_ib_modify_srq;
1620e126ba97SEli Cohen 	dev->ib_dev.query_srq		= mlx5_ib_query_srq;
1621e126ba97SEli Cohen 	dev->ib_dev.destroy_srq		= mlx5_ib_destroy_srq;
1622e126ba97SEli Cohen 	dev->ib_dev.post_srq_recv	= mlx5_ib_post_srq_recv;
1623e126ba97SEli Cohen 	dev->ib_dev.create_qp		= mlx5_ib_create_qp;
1624e126ba97SEli Cohen 	dev->ib_dev.modify_qp		= mlx5_ib_modify_qp;
1625e126ba97SEli Cohen 	dev->ib_dev.query_qp		= mlx5_ib_query_qp;
1626e126ba97SEli Cohen 	dev->ib_dev.destroy_qp		= mlx5_ib_destroy_qp;
1627e126ba97SEli Cohen 	dev->ib_dev.post_send		= mlx5_ib_post_send;
1628e126ba97SEli Cohen 	dev->ib_dev.post_recv		= mlx5_ib_post_recv;
1629e126ba97SEli Cohen 	dev->ib_dev.create_cq		= mlx5_ib_create_cq;
1630e126ba97SEli Cohen 	dev->ib_dev.modify_cq		= mlx5_ib_modify_cq;
1631e126ba97SEli Cohen 	dev->ib_dev.resize_cq		= mlx5_ib_resize_cq;
1632e126ba97SEli Cohen 	dev->ib_dev.destroy_cq		= mlx5_ib_destroy_cq;
1633e126ba97SEli Cohen 	dev->ib_dev.poll_cq		= mlx5_ib_poll_cq;
1634e126ba97SEli Cohen 	dev->ib_dev.req_notify_cq	= mlx5_ib_arm_cq;
1635e126ba97SEli Cohen 	dev->ib_dev.get_dma_mr		= mlx5_ib_get_dma_mr;
1636e126ba97SEli Cohen 	dev->ib_dev.reg_user_mr		= mlx5_ib_reg_user_mr;
1637e126ba97SEli Cohen 	dev->ib_dev.dereg_mr		= mlx5_ib_dereg_mr;
1638e126ba97SEli Cohen 	dev->ib_dev.attach_mcast	= mlx5_ib_mcg_attach;
1639e126ba97SEli Cohen 	dev->ib_dev.detach_mcast	= mlx5_ib_mcg_detach;
1640e126ba97SEli Cohen 	dev->ib_dev.process_mad		= mlx5_ib_process_mad;
16419bee178bSSagi Grimberg 	dev->ib_dev.alloc_mr		= mlx5_ib_alloc_mr;
16428a187ee5SSagi Grimberg 	dev->ib_dev.map_mr_sg		= mlx5_ib_map_mr_sg;
1643d5436ba0SSagi Grimberg 	dev->ib_dev.check_mr_status	= mlx5_ib_check_mr_status;
16447738613eSIra Weiny 	dev->ib_dev.get_port_immutable  = mlx5_port_immutable;
1645e126ba97SEli Cohen 
1646938fe83cSSaeed Mahameed 	mlx5_ib_internal_fill_odp_caps(dev);
16478cdd312cSHaggai Eran 
1648938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, xrc)) {
1649e126ba97SEli Cohen 		dev->ib_dev.alloc_xrcd = mlx5_ib_alloc_xrcd;
1650e126ba97SEli Cohen 		dev->ib_dev.dealloc_xrcd = mlx5_ib_dealloc_xrcd;
1651e126ba97SEli Cohen 		dev->ib_dev.uverbs_cmd_mask |=
1652e126ba97SEli Cohen 			(1ull << IB_USER_VERBS_CMD_OPEN_XRCD) |
1653e126ba97SEli Cohen 			(1ull << IB_USER_VERBS_CMD_CLOSE_XRCD);
1654e126ba97SEli Cohen 	}
1655e126ba97SEli Cohen 
1656e126ba97SEli Cohen 	err = init_node_data(dev);
1657e126ba97SEli Cohen 	if (err)
1658233d05d2SSaeed Mahameed 		goto err_dealloc;
1659e126ba97SEli Cohen 
1660e126ba97SEli Cohen 	mutex_init(&dev->cap_mask_mutex);
1661e126ba97SEli Cohen 
1662fc24fc5eSAchiad Shochat 	if (ll == IB_LINK_LAYER_ETHERNET) {
1663fc24fc5eSAchiad Shochat 		err = mlx5_enable_roce(dev);
1664e126ba97SEli Cohen 		if (err)
1665233d05d2SSaeed Mahameed 			goto err_dealloc;
1666fc24fc5eSAchiad Shochat 	}
1667fc24fc5eSAchiad Shochat 
1668fc24fc5eSAchiad Shochat 	err = create_dev_resources(&dev->devr);
1669fc24fc5eSAchiad Shochat 	if (err)
1670fc24fc5eSAchiad Shochat 		goto err_disable_roce;
1671e126ba97SEli Cohen 
16726aec21f6SHaggai Eran 	err = mlx5_ib_odp_init_one(dev);
1673281d1a92SWei Yongjun 	if (err)
1674e126ba97SEli Cohen 		goto err_rsrc;
1675e126ba97SEli Cohen 
16766aec21f6SHaggai Eran 	err = ib_register_device(&dev->ib_dev, NULL);
16776aec21f6SHaggai Eran 	if (err)
16786aec21f6SHaggai Eran 		goto err_odp;
16796aec21f6SHaggai Eran 
1680e126ba97SEli Cohen 	err = create_umr_res(dev);
1681e126ba97SEli Cohen 	if (err)
1682e126ba97SEli Cohen 		goto err_dev;
1683e126ba97SEli Cohen 
1684e126ba97SEli Cohen 	for (i = 0; i < ARRAY_SIZE(mlx5_class_attributes); i++) {
1685281d1a92SWei Yongjun 		err = device_create_file(&dev->ib_dev.dev,
1686281d1a92SWei Yongjun 					 mlx5_class_attributes[i]);
1687281d1a92SWei Yongjun 		if (err)
1688e126ba97SEli Cohen 			goto err_umrc;
1689e126ba97SEli Cohen 	}
1690e126ba97SEli Cohen 
1691e126ba97SEli Cohen 	dev->ib_active = true;
1692e126ba97SEli Cohen 
16939603b61dSJack Morgenstein 	return dev;
1694e126ba97SEli Cohen 
1695e126ba97SEli Cohen err_umrc:
1696e126ba97SEli Cohen 	destroy_umrc_res(dev);
1697e126ba97SEli Cohen 
1698e126ba97SEli Cohen err_dev:
1699e126ba97SEli Cohen 	ib_unregister_device(&dev->ib_dev);
1700e126ba97SEli Cohen 
17016aec21f6SHaggai Eran err_odp:
17026aec21f6SHaggai Eran 	mlx5_ib_odp_remove_one(dev);
17036aec21f6SHaggai Eran 
1704e126ba97SEli Cohen err_rsrc:
1705e126ba97SEli Cohen 	destroy_dev_resources(&dev->devr);
1706e126ba97SEli Cohen 
1707fc24fc5eSAchiad Shochat err_disable_roce:
1708fc24fc5eSAchiad Shochat 	if (ll == IB_LINK_LAYER_ETHERNET)
1709fc24fc5eSAchiad Shochat 		mlx5_disable_roce(dev);
1710fc24fc5eSAchiad Shochat 
17119603b61dSJack Morgenstein err_dealloc:
1712e126ba97SEli Cohen 	ib_dealloc_device((struct ib_device *)dev);
1713e126ba97SEli Cohen 
17149603b61dSJack Morgenstein 	return NULL;
1715e126ba97SEli Cohen }
1716e126ba97SEli Cohen 
17179603b61dSJack Morgenstein static void mlx5_ib_remove(struct mlx5_core_dev *mdev, void *context)
1718e126ba97SEli Cohen {
17199603b61dSJack Morgenstein 	struct mlx5_ib_dev *dev = context;
1720fc24fc5eSAchiad Shochat 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev, 1);
17216aec21f6SHaggai Eran 
1722e126ba97SEli Cohen 	ib_unregister_device(&dev->ib_dev);
1723eefd56e5SEli Cohen 	destroy_umrc_res(dev);
17246aec21f6SHaggai Eran 	mlx5_ib_odp_remove_one(dev);
1725e126ba97SEli Cohen 	destroy_dev_resources(&dev->devr);
1726fc24fc5eSAchiad Shochat 	if (ll == IB_LINK_LAYER_ETHERNET)
1727fc24fc5eSAchiad Shochat 		mlx5_disable_roce(dev);
1728e126ba97SEli Cohen 	ib_dealloc_device(&dev->ib_dev);
1729e126ba97SEli Cohen }
1730e126ba97SEli Cohen 
17319603b61dSJack Morgenstein static struct mlx5_interface mlx5_ib_interface = {
17329603b61dSJack Morgenstein 	.add            = mlx5_ib_add,
17339603b61dSJack Morgenstein 	.remove         = mlx5_ib_remove,
17349603b61dSJack Morgenstein 	.event          = mlx5_ib_event,
173564613d94SSaeed Mahameed 	.protocol	= MLX5_INTERFACE_PROTOCOL_IB,
1736e126ba97SEli Cohen };
1737e126ba97SEli Cohen 
1738e126ba97SEli Cohen static int __init mlx5_ib_init(void)
1739e126ba97SEli Cohen {
17406aec21f6SHaggai Eran 	int err;
17416aec21f6SHaggai Eran 
17429603b61dSJack Morgenstein 	if (deprecated_prof_sel != 2)
17439603b61dSJack Morgenstein 		pr_warn("prof_sel is deprecated for mlx5_ib, set it for mlx5_core\n");
17449603b61dSJack Morgenstein 
17456aec21f6SHaggai Eran 	err = mlx5_ib_odp_init();
17466aec21f6SHaggai Eran 	if (err)
17476aec21f6SHaggai Eran 		return err;
17486aec21f6SHaggai Eran 
17496aec21f6SHaggai Eran 	err = mlx5_register_interface(&mlx5_ib_interface);
17506aec21f6SHaggai Eran 	if (err)
17516aec21f6SHaggai Eran 		goto clean_odp;
17526aec21f6SHaggai Eran 
17536aec21f6SHaggai Eran 	return err;
17546aec21f6SHaggai Eran 
17556aec21f6SHaggai Eran clean_odp:
17566aec21f6SHaggai Eran 	mlx5_ib_odp_cleanup();
17576aec21f6SHaggai Eran 	return err;
1758e126ba97SEli Cohen }
1759e126ba97SEli Cohen 
1760e126ba97SEli Cohen static void __exit mlx5_ib_cleanup(void)
1761e126ba97SEli Cohen {
17629603b61dSJack Morgenstein 	mlx5_unregister_interface(&mlx5_ib_interface);
17636aec21f6SHaggai Eran 	mlx5_ib_odp_cleanup();
1764e126ba97SEli Cohen }
1765e126ba97SEli Cohen 
1766e126ba97SEli Cohen module_init(mlx5_ib_init);
1767e126ba97SEli Cohen module_exit(mlx5_ib_cleanup);
1768