xref: /openbmc/linux/drivers/infiniband/hw/mlx5/main.c (revision 026bae0cb428102228d110780d90e6ae44bbe4c7)
1e126ba97SEli Cohen /*
26cf0a15fSSaeed Mahameed  * Copyright (c) 2013-2015, Mellanox Technologies. All rights reserved.
3e126ba97SEli Cohen  *
4e126ba97SEli Cohen  * This software is available to you under a choice of one of two
5e126ba97SEli Cohen  * licenses.  You may choose to be licensed under the terms of the GNU
6e126ba97SEli Cohen  * General Public License (GPL) Version 2, available from the file
7e126ba97SEli Cohen  * COPYING in the main directory of this source tree, or the
8e126ba97SEli Cohen  * OpenIB.org BSD license below:
9e126ba97SEli Cohen  *
10e126ba97SEli Cohen  *     Redistribution and use in source and binary forms, with or
11e126ba97SEli Cohen  *     without modification, are permitted provided that the following
12e126ba97SEli Cohen  *     conditions are met:
13e126ba97SEli Cohen  *
14e126ba97SEli Cohen  *      - Redistributions of source code must retain the above
15e126ba97SEli Cohen  *        copyright notice, this list of conditions and the following
16e126ba97SEli Cohen  *        disclaimer.
17e126ba97SEli Cohen  *
18e126ba97SEli Cohen  *      - Redistributions in binary form must reproduce the above
19e126ba97SEli Cohen  *        copyright notice, this list of conditions and the following
20e126ba97SEli Cohen  *        disclaimer in the documentation and/or other materials
21e126ba97SEli Cohen  *        provided with the distribution.
22e126ba97SEli Cohen  *
23e126ba97SEli Cohen  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24e126ba97SEli Cohen  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25e126ba97SEli Cohen  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26e126ba97SEli Cohen  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27e126ba97SEli Cohen  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28e126ba97SEli Cohen  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29e126ba97SEli Cohen  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30e126ba97SEli Cohen  * SOFTWARE.
31e126ba97SEli Cohen  */
32e126ba97SEli Cohen 
33adec640eSChristoph Hellwig #include <linux/highmem.h>
34e126ba97SEli Cohen #include <linux/module.h>
35e126ba97SEli Cohen #include <linux/init.h>
36e126ba97SEli Cohen #include <linux/errno.h>
37e126ba97SEli Cohen #include <linux/pci.h>
38e126ba97SEli Cohen #include <linux/dma-mapping.h>
39e126ba97SEli Cohen #include <linux/slab.h>
40e126ba97SEli Cohen #include <linux/io-mapping.h>
4137aa5c36SGuy Levi #if defined(CONFIG_X86)
4237aa5c36SGuy Levi #include <asm/pat.h>
4337aa5c36SGuy Levi #endif
44e126ba97SEli Cohen #include <linux/sched.h>
457c2344c3SMaor Gottlieb #include <linux/delay.h>
46e126ba97SEli Cohen #include <rdma/ib_user_verbs.h>
473f89a643SAchiad Shochat #include <rdma/ib_addr.h>
482811ba51SAchiad Shochat #include <rdma/ib_cache.h>
49ada68c31SAchiad Shochat #include <linux/mlx5/port.h>
501b5daf11SMajd Dibbiny #include <linux/mlx5/vport.h>
517c2344c3SMaor Gottlieb #include <linux/list.h>
52e126ba97SEli Cohen #include <rdma/ib_smi.h>
53e126ba97SEli Cohen #include <rdma/ib_umem.h>
54038d2ef8SMaor Gottlieb #include <linux/in.h>
55038d2ef8SMaor Gottlieb #include <linux/etherdevice.h>
56038d2ef8SMaor Gottlieb #include <linux/mlx5/fs.h>
57e126ba97SEli Cohen #include "user.h"
58e126ba97SEli Cohen #include "mlx5_ib.h"
59e126ba97SEli Cohen 
60e126ba97SEli Cohen #define DRIVER_NAME "mlx5_ib"
61169a1d85SAmir Vadai #define DRIVER_VERSION "2.2-1"
62169a1d85SAmir Vadai #define DRIVER_RELDATE	"Feb 2014"
63e126ba97SEli Cohen 
64e126ba97SEli Cohen MODULE_AUTHOR("Eli Cohen <eli@mellanox.com>");
65e126ba97SEli Cohen MODULE_DESCRIPTION("Mellanox Connect-IB HCA IB driver");
66e126ba97SEli Cohen MODULE_LICENSE("Dual BSD/GPL");
67e126ba97SEli Cohen MODULE_VERSION(DRIVER_VERSION);
68e126ba97SEli Cohen 
699603b61dSJack Morgenstein static int deprecated_prof_sel = 2;
709603b61dSJack Morgenstein module_param_named(prof_sel, deprecated_prof_sel, int, 0444);
719603b61dSJack Morgenstein MODULE_PARM_DESC(prof_sel, "profile selector. Deprecated here. Moved to module mlx5_core");
72e126ba97SEli Cohen 
73e126ba97SEli Cohen static char mlx5_version[] =
74e126ba97SEli Cohen 	DRIVER_NAME ": Mellanox Connect-IB Infiniband driver v"
75e126ba97SEli Cohen 	DRIVER_VERSION " (" DRIVER_RELDATE ")\n";
76e126ba97SEli Cohen 
77da7525d2SEran Ben Elisha enum {
78da7525d2SEran Ben Elisha 	MLX5_ATOMIC_SIZE_QP_8BYTES = 1 << 3,
79da7525d2SEran Ben Elisha };
801b5daf11SMajd Dibbiny 
811b5daf11SMajd Dibbiny static enum rdma_link_layer
82ebd61f68SAchiad Shochat mlx5_port_type_cap_to_rdma_ll(int port_type_cap)
831b5daf11SMajd Dibbiny {
84ebd61f68SAchiad Shochat 	switch (port_type_cap) {
851b5daf11SMajd Dibbiny 	case MLX5_CAP_PORT_TYPE_IB:
861b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_INFINIBAND;
871b5daf11SMajd Dibbiny 	case MLX5_CAP_PORT_TYPE_ETH:
881b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_ETHERNET;
891b5daf11SMajd Dibbiny 	default:
901b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_UNSPECIFIED;
911b5daf11SMajd Dibbiny 	}
921b5daf11SMajd Dibbiny }
931b5daf11SMajd Dibbiny 
94ebd61f68SAchiad Shochat static enum rdma_link_layer
95ebd61f68SAchiad Shochat mlx5_ib_port_link_layer(struct ib_device *device, u8 port_num)
96ebd61f68SAchiad Shochat {
97ebd61f68SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(device);
98ebd61f68SAchiad Shochat 	int port_type_cap = MLX5_CAP_GEN(dev->mdev, port_type);
99ebd61f68SAchiad Shochat 
100ebd61f68SAchiad Shochat 	return mlx5_port_type_cap_to_rdma_ll(port_type_cap);
101ebd61f68SAchiad Shochat }
102ebd61f68SAchiad Shochat 
103fc24fc5eSAchiad Shochat static int mlx5_netdev_event(struct notifier_block *this,
104fc24fc5eSAchiad Shochat 			     unsigned long event, void *ptr)
105fc24fc5eSAchiad Shochat {
106fc24fc5eSAchiad Shochat 	struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
107fc24fc5eSAchiad Shochat 	struct mlx5_ib_dev *ibdev = container_of(this, struct mlx5_ib_dev,
108fc24fc5eSAchiad Shochat 						 roce.nb);
109fc24fc5eSAchiad Shochat 
110fc24fc5eSAchiad Shochat 	if ((event != NETDEV_UNREGISTER) && (event != NETDEV_REGISTER))
111fc24fc5eSAchiad Shochat 		return NOTIFY_DONE;
112fc24fc5eSAchiad Shochat 
113fc24fc5eSAchiad Shochat 	write_lock(&ibdev->roce.netdev_lock);
114fc24fc5eSAchiad Shochat 	if (ndev->dev.parent == &ibdev->mdev->pdev->dev)
115fc24fc5eSAchiad Shochat 		ibdev->roce.netdev = (event == NETDEV_UNREGISTER) ? NULL : ndev;
116fc24fc5eSAchiad Shochat 	write_unlock(&ibdev->roce.netdev_lock);
117fc24fc5eSAchiad Shochat 
118fc24fc5eSAchiad Shochat 	return NOTIFY_DONE;
119fc24fc5eSAchiad Shochat }
120fc24fc5eSAchiad Shochat 
121fc24fc5eSAchiad Shochat static struct net_device *mlx5_ib_get_netdev(struct ib_device *device,
122fc24fc5eSAchiad Shochat 					     u8 port_num)
123fc24fc5eSAchiad Shochat {
124fc24fc5eSAchiad Shochat 	struct mlx5_ib_dev *ibdev = to_mdev(device);
125fc24fc5eSAchiad Shochat 	struct net_device *ndev;
126fc24fc5eSAchiad Shochat 
127fc24fc5eSAchiad Shochat 	/* Ensure ndev does not disappear before we invoke dev_hold()
128fc24fc5eSAchiad Shochat 	 */
129fc24fc5eSAchiad Shochat 	read_lock(&ibdev->roce.netdev_lock);
130fc24fc5eSAchiad Shochat 	ndev = ibdev->roce.netdev;
131fc24fc5eSAchiad Shochat 	if (ndev)
132fc24fc5eSAchiad Shochat 		dev_hold(ndev);
133fc24fc5eSAchiad Shochat 	read_unlock(&ibdev->roce.netdev_lock);
134fc24fc5eSAchiad Shochat 
135fc24fc5eSAchiad Shochat 	return ndev;
136fc24fc5eSAchiad Shochat }
137fc24fc5eSAchiad Shochat 
1383f89a643SAchiad Shochat static int mlx5_query_port_roce(struct ib_device *device, u8 port_num,
1393f89a643SAchiad Shochat 				struct ib_port_attr *props)
1403f89a643SAchiad Shochat {
1413f89a643SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(device);
1423f89a643SAchiad Shochat 	struct net_device *ndev;
1433f89a643SAchiad Shochat 	enum ib_mtu ndev_ib_mtu;
144c876a1b7SLeon Romanovsky 	u16 qkey_viol_cntr;
1453f89a643SAchiad Shochat 
1463f89a643SAchiad Shochat 	memset(props, 0, sizeof(*props));
1473f89a643SAchiad Shochat 
1483f89a643SAchiad Shochat 	props->port_cap_flags  |= IB_PORT_CM_SUP;
1493f89a643SAchiad Shochat 	props->port_cap_flags  |= IB_PORT_IP_BASED_GIDS;
1503f89a643SAchiad Shochat 
1513f89a643SAchiad Shochat 	props->gid_tbl_len      = MLX5_CAP_ROCE(dev->mdev,
1523f89a643SAchiad Shochat 						roce_address_table_size);
1533f89a643SAchiad Shochat 	props->max_mtu          = IB_MTU_4096;
1543f89a643SAchiad Shochat 	props->max_msg_sz       = 1 << MLX5_CAP_GEN(dev->mdev, log_max_msg);
1553f89a643SAchiad Shochat 	props->pkey_tbl_len     = 1;
1563f89a643SAchiad Shochat 	props->state            = IB_PORT_DOWN;
1573f89a643SAchiad Shochat 	props->phys_state       = 3;
1583f89a643SAchiad Shochat 
159c876a1b7SLeon Romanovsky 	mlx5_query_nic_vport_qkey_viol_cntr(dev->mdev, &qkey_viol_cntr);
160c876a1b7SLeon Romanovsky 	props->qkey_viol_cntr = qkey_viol_cntr;
1613f89a643SAchiad Shochat 
1623f89a643SAchiad Shochat 	ndev = mlx5_ib_get_netdev(device, port_num);
1633f89a643SAchiad Shochat 	if (!ndev)
1643f89a643SAchiad Shochat 		return 0;
1653f89a643SAchiad Shochat 
1663f89a643SAchiad Shochat 	if (netif_running(ndev) && netif_carrier_ok(ndev)) {
1673f89a643SAchiad Shochat 		props->state      = IB_PORT_ACTIVE;
1683f89a643SAchiad Shochat 		props->phys_state = 5;
1693f89a643SAchiad Shochat 	}
1703f89a643SAchiad Shochat 
1713f89a643SAchiad Shochat 	ndev_ib_mtu = iboe_get_mtu(ndev->mtu);
1723f89a643SAchiad Shochat 
1733f89a643SAchiad Shochat 	dev_put(ndev);
1743f89a643SAchiad Shochat 
1753f89a643SAchiad Shochat 	props->active_mtu	= min(props->max_mtu, ndev_ib_mtu);
1763f89a643SAchiad Shochat 
1773f89a643SAchiad Shochat 	props->active_width	= IB_WIDTH_4X;  /* TODO */
1783f89a643SAchiad Shochat 	props->active_speed	= IB_SPEED_QDR; /* TODO */
1793f89a643SAchiad Shochat 
1803f89a643SAchiad Shochat 	return 0;
1813f89a643SAchiad Shochat }
1823f89a643SAchiad Shochat 
1833cca2606SAchiad Shochat static void ib_gid_to_mlx5_roce_addr(const union ib_gid *gid,
1843cca2606SAchiad Shochat 				     const struct ib_gid_attr *attr,
1853cca2606SAchiad Shochat 				     void *mlx5_addr)
1863cca2606SAchiad Shochat {
1873cca2606SAchiad Shochat #define MLX5_SET_RA(p, f, v) MLX5_SET(roce_addr_layout, p, f, v)
1883cca2606SAchiad Shochat 	char *mlx5_addr_l3_addr	= MLX5_ADDR_OF(roce_addr_layout, mlx5_addr,
1893cca2606SAchiad Shochat 					       source_l3_address);
1903cca2606SAchiad Shochat 	void *mlx5_addr_mac	= MLX5_ADDR_OF(roce_addr_layout, mlx5_addr,
1913cca2606SAchiad Shochat 					       source_mac_47_32);
1923cca2606SAchiad Shochat 
1933cca2606SAchiad Shochat 	if (!gid)
1943cca2606SAchiad Shochat 		return;
1953cca2606SAchiad Shochat 
1963cca2606SAchiad Shochat 	ether_addr_copy(mlx5_addr_mac, attr->ndev->dev_addr);
1973cca2606SAchiad Shochat 
1983cca2606SAchiad Shochat 	if (is_vlan_dev(attr->ndev)) {
1993cca2606SAchiad Shochat 		MLX5_SET_RA(mlx5_addr, vlan_valid, 1);
2003cca2606SAchiad Shochat 		MLX5_SET_RA(mlx5_addr, vlan_id, vlan_dev_vlan_id(attr->ndev));
2013cca2606SAchiad Shochat 	}
2023cca2606SAchiad Shochat 
2033cca2606SAchiad Shochat 	switch (attr->gid_type) {
2043cca2606SAchiad Shochat 	case IB_GID_TYPE_IB:
2053cca2606SAchiad Shochat 		MLX5_SET_RA(mlx5_addr, roce_version, MLX5_ROCE_VERSION_1);
2063cca2606SAchiad Shochat 		break;
2073cca2606SAchiad Shochat 	case IB_GID_TYPE_ROCE_UDP_ENCAP:
2083cca2606SAchiad Shochat 		MLX5_SET_RA(mlx5_addr, roce_version, MLX5_ROCE_VERSION_2);
2093cca2606SAchiad Shochat 		break;
2103cca2606SAchiad Shochat 
2113cca2606SAchiad Shochat 	default:
2123cca2606SAchiad Shochat 		WARN_ON(true);
2133cca2606SAchiad Shochat 	}
2143cca2606SAchiad Shochat 
2153cca2606SAchiad Shochat 	if (attr->gid_type != IB_GID_TYPE_IB) {
2163cca2606SAchiad Shochat 		if (ipv6_addr_v4mapped((void *)gid))
2173cca2606SAchiad Shochat 			MLX5_SET_RA(mlx5_addr, roce_l3_type,
2183cca2606SAchiad Shochat 				    MLX5_ROCE_L3_TYPE_IPV4);
2193cca2606SAchiad Shochat 		else
2203cca2606SAchiad Shochat 			MLX5_SET_RA(mlx5_addr, roce_l3_type,
2213cca2606SAchiad Shochat 				    MLX5_ROCE_L3_TYPE_IPV6);
2223cca2606SAchiad Shochat 	}
2233cca2606SAchiad Shochat 
2243cca2606SAchiad Shochat 	if ((attr->gid_type == IB_GID_TYPE_IB) ||
2253cca2606SAchiad Shochat 	    !ipv6_addr_v4mapped((void *)gid))
2263cca2606SAchiad Shochat 		memcpy(mlx5_addr_l3_addr, gid, sizeof(*gid));
2273cca2606SAchiad Shochat 	else
2283cca2606SAchiad Shochat 		memcpy(&mlx5_addr_l3_addr[12], &gid->raw[12], 4);
2293cca2606SAchiad Shochat }
2303cca2606SAchiad Shochat 
2313cca2606SAchiad Shochat static int set_roce_addr(struct ib_device *device, u8 port_num,
2323cca2606SAchiad Shochat 			 unsigned int index,
2333cca2606SAchiad Shochat 			 const union ib_gid *gid,
2343cca2606SAchiad Shochat 			 const struct ib_gid_attr *attr)
2353cca2606SAchiad Shochat {
2363cca2606SAchiad Shochat 	struct mlx5_ib_dev *dev	= to_mdev(device);
2373cca2606SAchiad Shochat 	u32  in[MLX5_ST_SZ_DW(set_roce_address_in)];
2383cca2606SAchiad Shochat 	u32 out[MLX5_ST_SZ_DW(set_roce_address_out)];
2393cca2606SAchiad Shochat 	void *in_addr = MLX5_ADDR_OF(set_roce_address_in, in, roce_address);
2403cca2606SAchiad Shochat 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(device, port_num);
2413cca2606SAchiad Shochat 
2423cca2606SAchiad Shochat 	if (ll != IB_LINK_LAYER_ETHERNET)
2433cca2606SAchiad Shochat 		return -EINVAL;
2443cca2606SAchiad Shochat 
2453cca2606SAchiad Shochat 	memset(in, 0, sizeof(in));
2463cca2606SAchiad Shochat 
2473cca2606SAchiad Shochat 	ib_gid_to_mlx5_roce_addr(gid, attr, in_addr);
2483cca2606SAchiad Shochat 
2493cca2606SAchiad Shochat 	MLX5_SET(set_roce_address_in, in, roce_address_index, index);
2503cca2606SAchiad Shochat 	MLX5_SET(set_roce_address_in, in, opcode, MLX5_CMD_OP_SET_ROCE_ADDRESS);
2513cca2606SAchiad Shochat 
2523cca2606SAchiad Shochat 	memset(out, 0, sizeof(out));
2533cca2606SAchiad Shochat 	return mlx5_cmd_exec(dev->mdev, in, sizeof(in), out, sizeof(out));
2543cca2606SAchiad Shochat }
2553cca2606SAchiad Shochat 
2563cca2606SAchiad Shochat static int mlx5_ib_add_gid(struct ib_device *device, u8 port_num,
2573cca2606SAchiad Shochat 			   unsigned int index, const union ib_gid *gid,
2583cca2606SAchiad Shochat 			   const struct ib_gid_attr *attr,
2593cca2606SAchiad Shochat 			   __always_unused void **context)
2603cca2606SAchiad Shochat {
2613cca2606SAchiad Shochat 	return set_roce_addr(device, port_num, index, gid, attr);
2623cca2606SAchiad Shochat }
2633cca2606SAchiad Shochat 
2643cca2606SAchiad Shochat static int mlx5_ib_del_gid(struct ib_device *device, u8 port_num,
2653cca2606SAchiad Shochat 			   unsigned int index, __always_unused void **context)
2663cca2606SAchiad Shochat {
2673cca2606SAchiad Shochat 	return set_roce_addr(device, port_num, index, NULL, NULL);
2683cca2606SAchiad Shochat }
2693cca2606SAchiad Shochat 
2702811ba51SAchiad Shochat __be16 mlx5_get_roce_udp_sport(struct mlx5_ib_dev *dev, u8 port_num,
2712811ba51SAchiad Shochat 			       int index)
2722811ba51SAchiad Shochat {
2732811ba51SAchiad Shochat 	struct ib_gid_attr attr;
2742811ba51SAchiad Shochat 	union ib_gid gid;
2752811ba51SAchiad Shochat 
2762811ba51SAchiad Shochat 	if (ib_get_cached_gid(&dev->ib_dev, port_num, index, &gid, &attr))
2772811ba51SAchiad Shochat 		return 0;
2782811ba51SAchiad Shochat 
2792811ba51SAchiad Shochat 	if (!attr.ndev)
2802811ba51SAchiad Shochat 		return 0;
2812811ba51SAchiad Shochat 
2822811ba51SAchiad Shochat 	dev_put(attr.ndev);
2832811ba51SAchiad Shochat 
2842811ba51SAchiad Shochat 	if (attr.gid_type != IB_GID_TYPE_ROCE_UDP_ENCAP)
2852811ba51SAchiad Shochat 		return 0;
2862811ba51SAchiad Shochat 
2872811ba51SAchiad Shochat 	return cpu_to_be16(MLX5_CAP_ROCE(dev->mdev, r_roce_min_src_udp_port));
2882811ba51SAchiad Shochat }
2892811ba51SAchiad Shochat 
2901b5daf11SMajd Dibbiny static int mlx5_use_mad_ifc(struct mlx5_ib_dev *dev)
2911b5daf11SMajd Dibbiny {
292d603c809SEli Cohen 	return !MLX5_CAP_GEN(dev->mdev, ib_virt);
2931b5daf11SMajd Dibbiny }
2941b5daf11SMajd Dibbiny 
2951b5daf11SMajd Dibbiny enum {
2961b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_MAD,
2971b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_HCA,
2981b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_NIC,
2991b5daf11SMajd Dibbiny };
3001b5daf11SMajd Dibbiny 
3011b5daf11SMajd Dibbiny static int mlx5_get_vport_access_method(struct ib_device *ibdev)
3021b5daf11SMajd Dibbiny {
3031b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(to_mdev(ibdev)))
3041b5daf11SMajd Dibbiny 		return MLX5_VPORT_ACCESS_METHOD_MAD;
3051b5daf11SMajd Dibbiny 
306ebd61f68SAchiad Shochat 	if (mlx5_ib_port_link_layer(ibdev, 1) ==
3071b5daf11SMajd Dibbiny 	    IB_LINK_LAYER_ETHERNET)
3081b5daf11SMajd Dibbiny 		return MLX5_VPORT_ACCESS_METHOD_NIC;
3091b5daf11SMajd Dibbiny 
3101b5daf11SMajd Dibbiny 	return MLX5_VPORT_ACCESS_METHOD_HCA;
3111b5daf11SMajd Dibbiny }
3121b5daf11SMajd Dibbiny 
313da7525d2SEran Ben Elisha static void get_atomic_caps(struct mlx5_ib_dev *dev,
314da7525d2SEran Ben Elisha 			    struct ib_device_attr *props)
315da7525d2SEran Ben Elisha {
316da7525d2SEran Ben Elisha 	u8 tmp;
317da7525d2SEran Ben Elisha 	u8 atomic_operations = MLX5_CAP_ATOMIC(dev->mdev, atomic_operations);
318da7525d2SEran Ben Elisha 	u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_qp);
319da7525d2SEran Ben Elisha 	u8 atomic_req_8B_endianness_mode =
320da7525d2SEran Ben Elisha 		MLX5_CAP_ATOMIC(dev->mdev, atomic_req_8B_endianess_mode);
321da7525d2SEran Ben Elisha 
322da7525d2SEran Ben Elisha 	/* Check if HW supports 8 bytes standard atomic operations and capable
323da7525d2SEran Ben Elisha 	 * of host endianness respond
324da7525d2SEran Ben Elisha 	 */
325da7525d2SEran Ben Elisha 	tmp = MLX5_ATOMIC_OPS_CMP_SWAP | MLX5_ATOMIC_OPS_FETCH_ADD;
326da7525d2SEran Ben Elisha 	if (((atomic_operations & tmp) == tmp) &&
327da7525d2SEran Ben Elisha 	    (atomic_size_qp & MLX5_ATOMIC_SIZE_QP_8BYTES) &&
328da7525d2SEran Ben Elisha 	    (atomic_req_8B_endianness_mode)) {
329da7525d2SEran Ben Elisha 		props->atomic_cap = IB_ATOMIC_HCA;
330da7525d2SEran Ben Elisha 	} else {
331da7525d2SEran Ben Elisha 		props->atomic_cap = IB_ATOMIC_NONE;
332da7525d2SEran Ben Elisha 	}
333da7525d2SEran Ben Elisha }
334da7525d2SEran Ben Elisha 
3351b5daf11SMajd Dibbiny static int mlx5_query_system_image_guid(struct ib_device *ibdev,
3361b5daf11SMajd Dibbiny 					__be64 *sys_image_guid)
3371b5daf11SMajd Dibbiny {
3381b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
3391b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
3401b5daf11SMajd Dibbiny 	u64 tmp;
3411b5daf11SMajd Dibbiny 	int err;
3421b5daf11SMajd Dibbiny 
3431b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
3441b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
3451b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_system_image_guid(ibdev,
3461b5daf11SMajd Dibbiny 							    sys_image_guid);
3471b5daf11SMajd Dibbiny 
3481b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
3491b5daf11SMajd Dibbiny 		err = mlx5_query_hca_vport_system_image_guid(mdev, &tmp);
3503f89a643SAchiad Shochat 		break;
3513f89a643SAchiad Shochat 
3523f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
3533f89a643SAchiad Shochat 		err = mlx5_query_nic_vport_system_image_guid(mdev, &tmp);
3543f89a643SAchiad Shochat 		break;
3551b5daf11SMajd Dibbiny 
3561b5daf11SMajd Dibbiny 	default:
3571b5daf11SMajd Dibbiny 		return -EINVAL;
3581b5daf11SMajd Dibbiny 	}
3593f89a643SAchiad Shochat 
3603f89a643SAchiad Shochat 	if (!err)
3613f89a643SAchiad Shochat 		*sys_image_guid = cpu_to_be64(tmp);
3623f89a643SAchiad Shochat 
3633f89a643SAchiad Shochat 	return err;
3643f89a643SAchiad Shochat 
3651b5daf11SMajd Dibbiny }
3661b5daf11SMajd Dibbiny 
3671b5daf11SMajd Dibbiny static int mlx5_query_max_pkeys(struct ib_device *ibdev,
3681b5daf11SMajd Dibbiny 				u16 *max_pkeys)
3691b5daf11SMajd Dibbiny {
3701b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
3711b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
3721b5daf11SMajd Dibbiny 
3731b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
3741b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
3751b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_max_pkeys(ibdev, max_pkeys);
3761b5daf11SMajd Dibbiny 
3771b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
3781b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
3791b5daf11SMajd Dibbiny 		*max_pkeys = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev,
3801b5daf11SMajd Dibbiny 						pkey_table_size));
3811b5daf11SMajd Dibbiny 		return 0;
3821b5daf11SMajd Dibbiny 
3831b5daf11SMajd Dibbiny 	default:
3841b5daf11SMajd Dibbiny 		return -EINVAL;
3851b5daf11SMajd Dibbiny 	}
3861b5daf11SMajd Dibbiny }
3871b5daf11SMajd Dibbiny 
3881b5daf11SMajd Dibbiny static int mlx5_query_vendor_id(struct ib_device *ibdev,
3891b5daf11SMajd Dibbiny 				u32 *vendor_id)
3901b5daf11SMajd Dibbiny {
3911b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
3921b5daf11SMajd Dibbiny 
3931b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
3941b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
3951b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_vendor_id(ibdev, vendor_id);
3961b5daf11SMajd Dibbiny 
3971b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
3981b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
3991b5daf11SMajd Dibbiny 		return mlx5_core_query_vendor_id(dev->mdev, vendor_id);
4001b5daf11SMajd Dibbiny 
4011b5daf11SMajd Dibbiny 	default:
4021b5daf11SMajd Dibbiny 		return -EINVAL;
4031b5daf11SMajd Dibbiny 	}
4041b5daf11SMajd Dibbiny }
4051b5daf11SMajd Dibbiny 
4061b5daf11SMajd Dibbiny static int mlx5_query_node_guid(struct mlx5_ib_dev *dev,
4071b5daf11SMajd Dibbiny 				__be64 *node_guid)
4081b5daf11SMajd Dibbiny {
4091b5daf11SMajd Dibbiny 	u64 tmp;
4101b5daf11SMajd Dibbiny 	int err;
4111b5daf11SMajd Dibbiny 
4121b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(&dev->ib_dev)) {
4131b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
4141b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_node_guid(dev, node_guid);
4151b5daf11SMajd Dibbiny 
4161b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
4171b5daf11SMajd Dibbiny 		err = mlx5_query_hca_vport_node_guid(dev->mdev, &tmp);
4183f89a643SAchiad Shochat 		break;
4193f89a643SAchiad Shochat 
4203f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
4213f89a643SAchiad Shochat 		err = mlx5_query_nic_vport_node_guid(dev->mdev, &tmp);
4223f89a643SAchiad Shochat 		break;
4231b5daf11SMajd Dibbiny 
4241b5daf11SMajd Dibbiny 	default:
4251b5daf11SMajd Dibbiny 		return -EINVAL;
4261b5daf11SMajd Dibbiny 	}
4273f89a643SAchiad Shochat 
4283f89a643SAchiad Shochat 	if (!err)
4293f89a643SAchiad Shochat 		*node_guid = cpu_to_be64(tmp);
4303f89a643SAchiad Shochat 
4313f89a643SAchiad Shochat 	return err;
4321b5daf11SMajd Dibbiny }
4331b5daf11SMajd Dibbiny 
4341b5daf11SMajd Dibbiny struct mlx5_reg_node_desc {
4351b5daf11SMajd Dibbiny 	u8	desc[64];
4361b5daf11SMajd Dibbiny };
4371b5daf11SMajd Dibbiny 
4381b5daf11SMajd Dibbiny static int mlx5_query_node_desc(struct mlx5_ib_dev *dev, char *node_desc)
4391b5daf11SMajd Dibbiny {
4401b5daf11SMajd Dibbiny 	struct mlx5_reg_node_desc in;
4411b5daf11SMajd Dibbiny 
4421b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(dev))
4431b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_node_desc(dev, node_desc);
4441b5daf11SMajd Dibbiny 
4451b5daf11SMajd Dibbiny 	memset(&in, 0, sizeof(in));
4461b5daf11SMajd Dibbiny 
4471b5daf11SMajd Dibbiny 	return mlx5_core_access_reg(dev->mdev, &in, sizeof(in), node_desc,
4481b5daf11SMajd Dibbiny 				    sizeof(struct mlx5_reg_node_desc),
4491b5daf11SMajd Dibbiny 				    MLX5_REG_NODE_DESC, 0, 0);
4501b5daf11SMajd Dibbiny }
4511b5daf11SMajd Dibbiny 
452e126ba97SEli Cohen static int mlx5_ib_query_device(struct ib_device *ibdev,
4532528e33eSMatan Barak 				struct ib_device_attr *props,
4542528e33eSMatan Barak 				struct ib_udata *uhw)
455e126ba97SEli Cohen {
456e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
457938fe83cSSaeed Mahameed 	struct mlx5_core_dev *mdev = dev->mdev;
458e126ba97SEli Cohen 	int err = -ENOMEM;
459e126ba97SEli Cohen 	int max_rq_sg;
460e126ba97SEli Cohen 	int max_sq_sg;
461e0238a6aSSagi Grimberg 	u64 min_page_size = 1ull << MLX5_CAP_GEN(mdev, log_pg_sz);
462e126ba97SEli Cohen 
4632528e33eSMatan Barak 	if (uhw->inlen || uhw->outlen)
4642528e33eSMatan Barak 		return -EINVAL;
4652528e33eSMatan Barak 
466e126ba97SEli Cohen 	memset(props, 0, sizeof(*props));
4671b5daf11SMajd Dibbiny 	err = mlx5_query_system_image_guid(ibdev,
4681b5daf11SMajd Dibbiny 					   &props->sys_image_guid);
4691b5daf11SMajd Dibbiny 	if (err)
4701b5daf11SMajd Dibbiny 		return err;
4711b5daf11SMajd Dibbiny 
4721b5daf11SMajd Dibbiny 	err = mlx5_query_max_pkeys(ibdev, &props->max_pkeys);
4731b5daf11SMajd Dibbiny 	if (err)
4741b5daf11SMajd Dibbiny 		return err;
4751b5daf11SMajd Dibbiny 
4761b5daf11SMajd Dibbiny 	err = mlx5_query_vendor_id(ibdev, &props->vendor_id);
4771b5daf11SMajd Dibbiny 	if (err)
4781b5daf11SMajd Dibbiny 		return err;
479e126ba97SEli Cohen 
4809603b61dSJack Morgenstein 	props->fw_ver = ((u64)fw_rev_maj(dev->mdev) << 32) |
4819603b61dSJack Morgenstein 		(fw_rev_min(dev->mdev) << 16) |
4829603b61dSJack Morgenstein 		fw_rev_sub(dev->mdev);
483e126ba97SEli Cohen 	props->device_cap_flags    = IB_DEVICE_CHANGE_PHY_PORT |
484e126ba97SEli Cohen 		IB_DEVICE_PORT_ACTIVE_EVENT		|
485e126ba97SEli Cohen 		IB_DEVICE_SYS_IMAGE_GUID		|
4861a4c3a3dSEli Cohen 		IB_DEVICE_RC_RNR_NAK_GEN;
487938fe83cSSaeed Mahameed 
488938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, pkv))
489e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
490938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, qkv))
491e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
492938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, apm))
493e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
494938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, xrc))
495e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_XRC;
496d2370e0aSMatan Barak 	if (MLX5_CAP_GEN(mdev, imaicl)) {
497d2370e0aSMatan Barak 		props->device_cap_flags |= IB_DEVICE_MEM_WINDOW |
498d2370e0aSMatan Barak 					   IB_DEVICE_MEM_WINDOW_TYPE_2B;
499d2370e0aSMatan Barak 		props->max_mw = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
500b005d316SSagi Grimberg 		/* We support 'Gappy' memory registration too */
501b005d316SSagi Grimberg 		props->device_cap_flags |= IB_DEVICE_SG_GAPS_REG;
502d2370e0aSMatan Barak 	}
503e126ba97SEli Cohen 	props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
504938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, sho)) {
5052dea9094SSagi Grimberg 		props->device_cap_flags |= IB_DEVICE_SIGNATURE_HANDOVER;
5062dea9094SSagi Grimberg 		/* At this stage no support for signature handover */
5072dea9094SSagi Grimberg 		props->sig_prot_cap = IB_PROT_T10DIF_TYPE_1 |
5082dea9094SSagi Grimberg 				      IB_PROT_T10DIF_TYPE_2 |
5092dea9094SSagi Grimberg 				      IB_PROT_T10DIF_TYPE_3;
5102dea9094SSagi Grimberg 		props->sig_guard_cap = IB_GUARD_T10DIF_CRC |
5112dea9094SSagi Grimberg 				       IB_GUARD_T10DIF_CSUM;
5122dea9094SSagi Grimberg 	}
513938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, block_lb_mc))
514f360d88aSEli Cohen 		props->device_cap_flags |= IB_DEVICE_BLOCK_MULTICAST_LOOPBACK;
515e126ba97SEli Cohen 
51688115fe7SBodong Wang 	if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
51788115fe7SBodong Wang 	    (MLX5_CAP_ETH(dev->mdev, csum_cap)))
51888115fe7SBodong Wang 			props->device_cap_flags |= IB_DEVICE_RAW_IP_CSUM;
51988115fe7SBodong Wang 
520f0313965SErez Shitrit 	if (MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) {
521f0313965SErez Shitrit 		props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
522f0313965SErez Shitrit 		props->device_cap_flags |= IB_DEVICE_UD_TSO;
523f0313965SErez Shitrit 	}
524f0313965SErez Shitrit 
525cff5a0f3SMajd Dibbiny 	if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
526cff5a0f3SMajd Dibbiny 	    MLX5_CAP_ETH(dev->mdev, scatter_fcs))
527cff5a0f3SMajd Dibbiny 		props->device_cap_flags |= IB_DEVICE_RAW_SCATTER_FCS;
528cff5a0f3SMajd Dibbiny 
529da6d6ba3SMaor Gottlieb 	if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS))
530da6d6ba3SMaor Gottlieb 		props->device_cap_flags |= IB_DEVICE_MANAGED_FLOW_STEERING;
531da6d6ba3SMaor Gottlieb 
5321b5daf11SMajd Dibbiny 	props->vendor_part_id	   = mdev->pdev->device;
5331b5daf11SMajd Dibbiny 	props->hw_ver		   = mdev->pdev->revision;
534e126ba97SEli Cohen 
535e126ba97SEli Cohen 	props->max_mr_size	   = ~0ull;
536e0238a6aSSagi Grimberg 	props->page_size_cap	   = ~(min_page_size - 1);
537938fe83cSSaeed Mahameed 	props->max_qp		   = 1 << MLX5_CAP_GEN(mdev, log_max_qp);
538938fe83cSSaeed Mahameed 	props->max_qp_wr	   = 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
539938fe83cSSaeed Mahameed 	max_rq_sg =  MLX5_CAP_GEN(mdev, max_wqe_sz_rq) /
540938fe83cSSaeed Mahameed 		     sizeof(struct mlx5_wqe_data_seg);
541938fe83cSSaeed Mahameed 	max_sq_sg = (MLX5_CAP_GEN(mdev, max_wqe_sz_sq) -
542938fe83cSSaeed Mahameed 		     sizeof(struct mlx5_wqe_ctrl_seg)) /
543e126ba97SEli Cohen 		     sizeof(struct mlx5_wqe_data_seg);
544e126ba97SEli Cohen 	props->max_sge = min(max_rq_sg, max_sq_sg);
545986ef95eSSagi Grimberg 	props->max_sge_rd	   = MLX5_MAX_SGE_RD;
546938fe83cSSaeed Mahameed 	props->max_cq		   = 1 << MLX5_CAP_GEN(mdev, log_max_cq);
5479f177686SLeon Romanovsky 	props->max_cqe = (1 << MLX5_CAP_GEN(mdev, log_max_cq_sz)) - 1;
548938fe83cSSaeed Mahameed 	props->max_mr		   = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
549938fe83cSSaeed Mahameed 	props->max_pd		   = 1 << MLX5_CAP_GEN(mdev, log_max_pd);
550938fe83cSSaeed Mahameed 	props->max_qp_rd_atom	   = 1 << MLX5_CAP_GEN(mdev, log_max_ra_req_qp);
551938fe83cSSaeed Mahameed 	props->max_qp_init_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_res_qp);
552938fe83cSSaeed Mahameed 	props->max_srq		   = 1 << MLX5_CAP_GEN(mdev, log_max_srq);
553938fe83cSSaeed Mahameed 	props->max_srq_wr = (1 << MLX5_CAP_GEN(mdev, log_max_srq_sz)) - 1;
554938fe83cSSaeed Mahameed 	props->local_ca_ack_delay  = MLX5_CAP_GEN(mdev, local_ca_ack_delay);
555e126ba97SEli Cohen 	props->max_res_rd_atom	   = props->max_qp_rd_atom * props->max_qp;
556e126ba97SEli Cohen 	props->max_srq_sge	   = max_rq_sg - 1;
557911f4331SSagi Grimberg 	props->max_fast_reg_page_list_len =
558911f4331SSagi Grimberg 		1 << MLX5_CAP_GEN(mdev, log_max_klm_list_size);
559da7525d2SEran Ben Elisha 	get_atomic_caps(dev, props);
56081bea28fSEli Cohen 	props->masked_atomic_cap   = IB_ATOMIC_NONE;
561938fe83cSSaeed Mahameed 	props->max_mcast_grp	   = 1 << MLX5_CAP_GEN(mdev, log_max_mcg);
562938fe83cSSaeed Mahameed 	props->max_mcast_qp_attach = MLX5_CAP_GEN(mdev, max_qp_mcg);
563e126ba97SEli Cohen 	props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
564e126ba97SEli Cohen 					   props->max_mcast_grp;
565e126ba97SEli Cohen 	props->max_map_per_fmr = INT_MAX; /* no limit in ConnectIB */
5667c60bcbbSMatan Barak 	props->hca_core_clock = MLX5_CAP_GEN(mdev, device_frequency_khz);
5677c60bcbbSMatan Barak 	props->timestamp_mask = 0x7FFFFFFFFFFFFFFFULL;
568e126ba97SEli Cohen 
5698cdd312cSHaggai Eran #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
570938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, pg))
5718cdd312cSHaggai Eran 		props->device_cap_flags |= IB_DEVICE_ON_DEMAND_PAGING;
5728cdd312cSHaggai Eran 	props->odp_caps = dev->odp_caps;
5738cdd312cSHaggai Eran #endif
5748cdd312cSHaggai Eran 
575051f2630SLeon Romanovsky 	if (MLX5_CAP_GEN(mdev, cd))
576051f2630SLeon Romanovsky 		props->device_cap_flags |= IB_DEVICE_CROSS_CHANNEL;
577051f2630SLeon Romanovsky 
578eff901d3SEli Cohen 	if (!mlx5_core_is_pf(mdev))
579eff901d3SEli Cohen 		props->device_cap_flags |= IB_DEVICE_VIRTUAL_FUNCTION;
580eff901d3SEli Cohen 
5811b5daf11SMajd Dibbiny 	return 0;
5821b5daf11SMajd Dibbiny }
583e126ba97SEli Cohen 
5841b5daf11SMajd Dibbiny enum mlx5_ib_width {
5851b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_1X	= 1 << 0,
5861b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_2X	= 1 << 1,
5871b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_4X	= 1 << 2,
5881b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_8X	= 1 << 3,
5891b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_12X	= 1 << 4
5901b5daf11SMajd Dibbiny };
5911b5daf11SMajd Dibbiny 
5921b5daf11SMajd Dibbiny static int translate_active_width(struct ib_device *ibdev, u8 active_width,
5931b5daf11SMajd Dibbiny 				  u8 *ib_width)
5941b5daf11SMajd Dibbiny {
5951b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
5961b5daf11SMajd Dibbiny 	int err = 0;
5971b5daf11SMajd Dibbiny 
5981b5daf11SMajd Dibbiny 	if (active_width & MLX5_IB_WIDTH_1X) {
5991b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_1X;
6001b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_2X) {
6011b5daf11SMajd Dibbiny 		mlx5_ib_dbg(dev, "active_width %d is not supported by IB spec\n",
6021b5daf11SMajd Dibbiny 			    (int)active_width);
6031b5daf11SMajd Dibbiny 		err = -EINVAL;
6041b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_4X) {
6051b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_4X;
6061b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_8X) {
6071b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_8X;
6081b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_12X) {
6091b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_12X;
6101b5daf11SMajd Dibbiny 	} else {
6111b5daf11SMajd Dibbiny 		mlx5_ib_dbg(dev, "Invalid active_width %d\n",
6121b5daf11SMajd Dibbiny 			    (int)active_width);
6131b5daf11SMajd Dibbiny 		err = -EINVAL;
6141b5daf11SMajd Dibbiny 	}
6151b5daf11SMajd Dibbiny 
6161b5daf11SMajd Dibbiny 	return err;
6171b5daf11SMajd Dibbiny }
6181b5daf11SMajd Dibbiny 
6191b5daf11SMajd Dibbiny static int mlx5_mtu_to_ib_mtu(int mtu)
6201b5daf11SMajd Dibbiny {
6211b5daf11SMajd Dibbiny 	switch (mtu) {
6221b5daf11SMajd Dibbiny 	case 256: return 1;
6231b5daf11SMajd Dibbiny 	case 512: return 2;
6241b5daf11SMajd Dibbiny 	case 1024: return 3;
6251b5daf11SMajd Dibbiny 	case 2048: return 4;
6261b5daf11SMajd Dibbiny 	case 4096: return 5;
6271b5daf11SMajd Dibbiny 	default:
6281b5daf11SMajd Dibbiny 		pr_warn("invalid mtu\n");
6291b5daf11SMajd Dibbiny 		return -1;
6301b5daf11SMajd Dibbiny 	}
6311b5daf11SMajd Dibbiny }
6321b5daf11SMajd Dibbiny 
6331b5daf11SMajd Dibbiny enum ib_max_vl_num {
6341b5daf11SMajd Dibbiny 	__IB_MAX_VL_0		= 1,
6351b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_1		= 2,
6361b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_3		= 3,
6371b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_7		= 4,
6381b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_14	= 5,
6391b5daf11SMajd Dibbiny };
6401b5daf11SMajd Dibbiny 
6411b5daf11SMajd Dibbiny enum mlx5_vl_hw_cap {
6421b5daf11SMajd Dibbiny 	MLX5_VL_HW_0	= 1,
6431b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_1	= 2,
6441b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_2	= 3,
6451b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_3	= 4,
6461b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_4	= 5,
6471b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_5	= 6,
6481b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_6	= 7,
6491b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_7	= 8,
6501b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_14	= 15
6511b5daf11SMajd Dibbiny };
6521b5daf11SMajd Dibbiny 
6531b5daf11SMajd Dibbiny static int translate_max_vl_num(struct ib_device *ibdev, u8 vl_hw_cap,
6541b5daf11SMajd Dibbiny 				u8 *max_vl_num)
6551b5daf11SMajd Dibbiny {
6561b5daf11SMajd Dibbiny 	switch (vl_hw_cap) {
6571b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0:
6581b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0;
6591b5daf11SMajd Dibbiny 		break;
6601b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_1:
6611b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_1;
6621b5daf11SMajd Dibbiny 		break;
6631b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_3:
6641b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_3;
6651b5daf11SMajd Dibbiny 		break;
6661b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_7:
6671b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_7;
6681b5daf11SMajd Dibbiny 		break;
6691b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_14:
6701b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_14;
6711b5daf11SMajd Dibbiny 		break;
6721b5daf11SMajd Dibbiny 
6731b5daf11SMajd Dibbiny 	default:
6741b5daf11SMajd Dibbiny 		return -EINVAL;
6751b5daf11SMajd Dibbiny 	}
6761b5daf11SMajd Dibbiny 
6771b5daf11SMajd Dibbiny 	return 0;
6781b5daf11SMajd Dibbiny }
6791b5daf11SMajd Dibbiny 
6801b5daf11SMajd Dibbiny static int mlx5_query_hca_port(struct ib_device *ibdev, u8 port,
6811b5daf11SMajd Dibbiny 			       struct ib_port_attr *props)
6821b5daf11SMajd Dibbiny {
6831b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
6841b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
6851b5daf11SMajd Dibbiny 	struct mlx5_hca_vport_context *rep;
686046339eaSSaeed Mahameed 	u16 max_mtu;
687046339eaSSaeed Mahameed 	u16 oper_mtu;
6881b5daf11SMajd Dibbiny 	int err;
6891b5daf11SMajd Dibbiny 	u8 ib_link_width_oper;
6901b5daf11SMajd Dibbiny 	u8 vl_hw_cap;
6911b5daf11SMajd Dibbiny 
6921b5daf11SMajd Dibbiny 	rep = kzalloc(sizeof(*rep), GFP_KERNEL);
6931b5daf11SMajd Dibbiny 	if (!rep) {
6941b5daf11SMajd Dibbiny 		err = -ENOMEM;
6951b5daf11SMajd Dibbiny 		goto out;
6961b5daf11SMajd Dibbiny 	}
6971b5daf11SMajd Dibbiny 
6981b5daf11SMajd Dibbiny 	memset(props, 0, sizeof(*props));
6991b5daf11SMajd Dibbiny 
7001b5daf11SMajd Dibbiny 	err = mlx5_query_hca_vport_context(mdev, 0, port, 0, rep);
7011b5daf11SMajd Dibbiny 	if (err)
7021b5daf11SMajd Dibbiny 		goto out;
7031b5daf11SMajd Dibbiny 
7041b5daf11SMajd Dibbiny 	props->lid		= rep->lid;
7051b5daf11SMajd Dibbiny 	props->lmc		= rep->lmc;
7061b5daf11SMajd Dibbiny 	props->sm_lid		= rep->sm_lid;
7071b5daf11SMajd Dibbiny 	props->sm_sl		= rep->sm_sl;
7081b5daf11SMajd Dibbiny 	props->state		= rep->vport_state;
7091b5daf11SMajd Dibbiny 	props->phys_state	= rep->port_physical_state;
7101b5daf11SMajd Dibbiny 	props->port_cap_flags	= rep->cap_mask1;
7111b5daf11SMajd Dibbiny 	props->gid_tbl_len	= mlx5_get_gid_table_len(MLX5_CAP_GEN(mdev, gid_table_size));
7121b5daf11SMajd Dibbiny 	props->max_msg_sz	= 1 << MLX5_CAP_GEN(mdev, log_max_msg);
7131b5daf11SMajd Dibbiny 	props->pkey_tbl_len	= mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, pkey_table_size));
7141b5daf11SMajd Dibbiny 	props->bad_pkey_cntr	= rep->pkey_violation_counter;
7151b5daf11SMajd Dibbiny 	props->qkey_viol_cntr	= rep->qkey_violation_counter;
7161b5daf11SMajd Dibbiny 	props->subnet_timeout	= rep->subnet_timeout;
7171b5daf11SMajd Dibbiny 	props->init_type_reply	= rep->init_type_reply;
718eff901d3SEli Cohen 	props->grh_required	= rep->grh_required;
7191b5daf11SMajd Dibbiny 
7201b5daf11SMajd Dibbiny 	err = mlx5_query_port_link_width_oper(mdev, &ib_link_width_oper, port);
7211b5daf11SMajd Dibbiny 	if (err)
7221b5daf11SMajd Dibbiny 		goto out;
7231b5daf11SMajd Dibbiny 
7241b5daf11SMajd Dibbiny 	err = translate_active_width(ibdev, ib_link_width_oper,
7251b5daf11SMajd Dibbiny 				     &props->active_width);
7261b5daf11SMajd Dibbiny 	if (err)
7271b5daf11SMajd Dibbiny 		goto out;
7281b5daf11SMajd Dibbiny 	err = mlx5_query_port_proto_oper(mdev, &props->active_speed, MLX5_PTYS_IB,
7291b5daf11SMajd Dibbiny 					 port);
7301b5daf11SMajd Dibbiny 	if (err)
7311b5daf11SMajd Dibbiny 		goto out;
7321b5daf11SMajd Dibbiny 
733facc9699SSaeed Mahameed 	mlx5_query_port_max_mtu(mdev, &max_mtu, port);
7341b5daf11SMajd Dibbiny 
7351b5daf11SMajd Dibbiny 	props->max_mtu = mlx5_mtu_to_ib_mtu(max_mtu);
7361b5daf11SMajd Dibbiny 
737facc9699SSaeed Mahameed 	mlx5_query_port_oper_mtu(mdev, &oper_mtu, port);
7381b5daf11SMajd Dibbiny 
7391b5daf11SMajd Dibbiny 	props->active_mtu = mlx5_mtu_to_ib_mtu(oper_mtu);
7401b5daf11SMajd Dibbiny 
7411b5daf11SMajd Dibbiny 	err = mlx5_query_port_vl_hw_cap(mdev, &vl_hw_cap, port);
7421b5daf11SMajd Dibbiny 	if (err)
7431b5daf11SMajd Dibbiny 		goto out;
7441b5daf11SMajd Dibbiny 
7451b5daf11SMajd Dibbiny 	err = translate_max_vl_num(ibdev, vl_hw_cap,
7461b5daf11SMajd Dibbiny 				   &props->max_vl_num);
7471b5daf11SMajd Dibbiny out:
7481b5daf11SMajd Dibbiny 	kfree(rep);
749e126ba97SEli Cohen 	return err;
750e126ba97SEli Cohen }
751e126ba97SEli Cohen 
752e126ba97SEli Cohen int mlx5_ib_query_port(struct ib_device *ibdev, u8 port,
753e126ba97SEli Cohen 		       struct ib_port_attr *props)
754e126ba97SEli Cohen {
7551b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
7561b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
7571b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_port(ibdev, port, props);
758e126ba97SEli Cohen 
7591b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
7601b5daf11SMajd Dibbiny 		return mlx5_query_hca_port(ibdev, port, props);
7611b5daf11SMajd Dibbiny 
7623f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
7633f89a643SAchiad Shochat 		return mlx5_query_port_roce(ibdev, port, props);
7643f89a643SAchiad Shochat 
7651b5daf11SMajd Dibbiny 	default:
766e126ba97SEli Cohen 		return -EINVAL;
767e126ba97SEli Cohen 	}
768e126ba97SEli Cohen }
769e126ba97SEli Cohen 
770e126ba97SEli Cohen static int mlx5_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
771e126ba97SEli Cohen 			     union ib_gid *gid)
772e126ba97SEli Cohen {
7731b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
7741b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
775e126ba97SEli Cohen 
7761b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
7771b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
7781b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_gids(ibdev, port, index, gid);
779e126ba97SEli Cohen 
7801b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
7811b5daf11SMajd Dibbiny 		return mlx5_query_hca_vport_gid(mdev, 0, port, 0, index, gid);
782e126ba97SEli Cohen 
7831b5daf11SMajd Dibbiny 	default:
7841b5daf11SMajd Dibbiny 		return -EINVAL;
7851b5daf11SMajd Dibbiny 	}
786e126ba97SEli Cohen 
787e126ba97SEli Cohen }
788e126ba97SEli Cohen 
789e126ba97SEli Cohen static int mlx5_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
790e126ba97SEli Cohen 			      u16 *pkey)
791e126ba97SEli Cohen {
7921b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
7931b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
794e126ba97SEli Cohen 
7951b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
7961b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
7971b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_pkey(ibdev, port, index, pkey);
798e126ba97SEli Cohen 
7991b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
8001b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
8011b5daf11SMajd Dibbiny 		return mlx5_query_hca_vport_pkey(mdev, 0, port,  0, index,
8021b5daf11SMajd Dibbiny 						 pkey);
8031b5daf11SMajd Dibbiny 	default:
8041b5daf11SMajd Dibbiny 		return -EINVAL;
805e126ba97SEli Cohen 	}
8061b5daf11SMajd Dibbiny }
807e126ba97SEli Cohen 
808e126ba97SEli Cohen static int mlx5_ib_modify_device(struct ib_device *ibdev, int mask,
809e126ba97SEli Cohen 				 struct ib_device_modify *props)
810e126ba97SEli Cohen {
811e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
812e126ba97SEli Cohen 	struct mlx5_reg_node_desc in;
813e126ba97SEli Cohen 	struct mlx5_reg_node_desc out;
814e126ba97SEli Cohen 	int err;
815e126ba97SEli Cohen 
816e126ba97SEli Cohen 	if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
817e126ba97SEli Cohen 		return -EOPNOTSUPP;
818e126ba97SEli Cohen 
819e126ba97SEli Cohen 	if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
820e126ba97SEli Cohen 		return 0;
821e126ba97SEli Cohen 
822e126ba97SEli Cohen 	/*
823e126ba97SEli Cohen 	 * If possible, pass node desc to FW, so it can generate
824e126ba97SEli Cohen 	 * a 144 trap.  If cmd fails, just ignore.
825e126ba97SEli Cohen 	 */
826e126ba97SEli Cohen 	memcpy(&in, props->node_desc, 64);
8279603b61dSJack Morgenstein 	err = mlx5_core_access_reg(dev->mdev, &in, sizeof(in), &out,
828e126ba97SEli Cohen 				   sizeof(out), MLX5_REG_NODE_DESC, 0, 1);
829e126ba97SEli Cohen 	if (err)
830e126ba97SEli Cohen 		return err;
831e126ba97SEli Cohen 
832e126ba97SEli Cohen 	memcpy(ibdev->node_desc, props->node_desc, 64);
833e126ba97SEli Cohen 
834e126ba97SEli Cohen 	return err;
835e126ba97SEli Cohen }
836e126ba97SEli Cohen 
837e126ba97SEli Cohen static int mlx5_ib_modify_port(struct ib_device *ibdev, u8 port, int mask,
838e126ba97SEli Cohen 			       struct ib_port_modify *props)
839e126ba97SEli Cohen {
840e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
841e126ba97SEli Cohen 	struct ib_port_attr attr;
842e126ba97SEli Cohen 	u32 tmp;
843e126ba97SEli Cohen 	int err;
844e126ba97SEli Cohen 
845e126ba97SEli Cohen 	mutex_lock(&dev->cap_mask_mutex);
846e126ba97SEli Cohen 
847e126ba97SEli Cohen 	err = mlx5_ib_query_port(ibdev, port, &attr);
848e126ba97SEli Cohen 	if (err)
849e126ba97SEli Cohen 		goto out;
850e126ba97SEli Cohen 
851e126ba97SEli Cohen 	tmp = (attr.port_cap_flags | props->set_port_cap_mask) &
852e126ba97SEli Cohen 		~props->clr_port_cap_mask;
853e126ba97SEli Cohen 
8549603b61dSJack Morgenstein 	err = mlx5_set_port_caps(dev->mdev, port, tmp);
855e126ba97SEli Cohen 
856e126ba97SEli Cohen out:
857e126ba97SEli Cohen 	mutex_unlock(&dev->cap_mask_mutex);
858e126ba97SEli Cohen 	return err;
859e126ba97SEli Cohen }
860e126ba97SEli Cohen 
861e126ba97SEli Cohen static struct ib_ucontext *mlx5_ib_alloc_ucontext(struct ib_device *ibdev,
862e126ba97SEli Cohen 						  struct ib_udata *udata)
863e126ba97SEli Cohen {
864e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
865b368d7cbSMatan Barak 	struct mlx5_ib_alloc_ucontext_req_v2 req = {};
866b368d7cbSMatan Barak 	struct mlx5_ib_alloc_ucontext_resp resp = {};
867e126ba97SEli Cohen 	struct mlx5_ib_ucontext *context;
868e126ba97SEli Cohen 	struct mlx5_uuar_info *uuari;
869e126ba97SEli Cohen 	struct mlx5_uar *uars;
870c1be5232SEli Cohen 	int gross_uuars;
871e126ba97SEli Cohen 	int num_uars;
87278c0f98cSEli Cohen 	int ver;
873e126ba97SEli Cohen 	int uuarn;
874e126ba97SEli Cohen 	int err;
875e126ba97SEli Cohen 	int i;
876f241e749SJack Morgenstein 	size_t reqlen;
877a168a41cSMajd Dibbiny 	size_t min_req_v2 = offsetof(struct mlx5_ib_alloc_ucontext_req_v2,
878a168a41cSMajd Dibbiny 				     max_cqe_version);
879e126ba97SEli Cohen 
880e126ba97SEli Cohen 	if (!dev->ib_active)
881e126ba97SEli Cohen 		return ERR_PTR(-EAGAIN);
882e126ba97SEli Cohen 
883dfbee859SHaggai Abramovsky 	if (udata->inlen < sizeof(struct ib_uverbs_cmd_hdr))
884dfbee859SHaggai Abramovsky 		return ERR_PTR(-EINVAL);
885dfbee859SHaggai Abramovsky 
88678c0f98cSEli Cohen 	reqlen = udata->inlen - sizeof(struct ib_uverbs_cmd_hdr);
88778c0f98cSEli Cohen 	if (reqlen == sizeof(struct mlx5_ib_alloc_ucontext_req))
88878c0f98cSEli Cohen 		ver = 0;
889a168a41cSMajd Dibbiny 	else if (reqlen >= min_req_v2)
89078c0f98cSEli Cohen 		ver = 2;
89178c0f98cSEli Cohen 	else
89278c0f98cSEli Cohen 		return ERR_PTR(-EINVAL);
89378c0f98cSEli Cohen 
894b368d7cbSMatan Barak 	err = ib_copy_from_udata(&req, udata, min(reqlen, sizeof(req)));
895e126ba97SEli Cohen 	if (err)
896e126ba97SEli Cohen 		return ERR_PTR(err);
897e126ba97SEli Cohen 
898b368d7cbSMatan Barak 	if (req.flags)
89978c0f98cSEli Cohen 		return ERR_PTR(-EINVAL);
90078c0f98cSEli Cohen 
901e126ba97SEli Cohen 	if (req.total_num_uuars > MLX5_MAX_UUARS)
902e126ba97SEli Cohen 		return ERR_PTR(-ENOMEM);
903e126ba97SEli Cohen 
904e126ba97SEli Cohen 	if (req.total_num_uuars == 0)
905e126ba97SEli Cohen 		return ERR_PTR(-EINVAL);
906e126ba97SEli Cohen 
907f72300c5SHaggai Abramovsky 	if (req.comp_mask || req.reserved0 || req.reserved1 || req.reserved2)
908b368d7cbSMatan Barak 		return ERR_PTR(-EOPNOTSUPP);
909b368d7cbSMatan Barak 
910b368d7cbSMatan Barak 	if (reqlen > sizeof(req) &&
911b368d7cbSMatan Barak 	    !ib_is_udata_cleared(udata, sizeof(req),
912dfbee859SHaggai Abramovsky 				 reqlen - sizeof(req)))
913b368d7cbSMatan Barak 		return ERR_PTR(-EOPNOTSUPP);
914b368d7cbSMatan Barak 
915c1be5232SEli Cohen 	req.total_num_uuars = ALIGN(req.total_num_uuars,
916c1be5232SEli Cohen 				    MLX5_NON_FP_BF_REGS_PER_PAGE);
917e126ba97SEli Cohen 	if (req.num_low_latency_uuars > req.total_num_uuars - 1)
918e126ba97SEli Cohen 		return ERR_PTR(-EINVAL);
919e126ba97SEli Cohen 
920c1be5232SEli Cohen 	num_uars = req.total_num_uuars / MLX5_NON_FP_BF_REGS_PER_PAGE;
921c1be5232SEli Cohen 	gross_uuars = num_uars * MLX5_BF_REGS_PER_PAGE;
922938fe83cSSaeed Mahameed 	resp.qp_tab_size = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp);
9232cc6ad5fSNoa Osherovich 	if (mlx5_core_is_pf(dev->mdev) && MLX5_CAP_GEN(dev->mdev, bf))
924938fe83cSSaeed Mahameed 		resp.bf_reg_size = 1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size);
925e126ba97SEli Cohen 	resp.cache_line_size = L1_CACHE_BYTES;
926938fe83cSSaeed Mahameed 	resp.max_sq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq);
927938fe83cSSaeed Mahameed 	resp.max_rq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq);
928938fe83cSSaeed Mahameed 	resp.max_send_wqebb = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
929938fe83cSSaeed Mahameed 	resp.max_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
930938fe83cSSaeed Mahameed 	resp.max_srq_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz);
931f72300c5SHaggai Abramovsky 	resp.cqe_version = min_t(__u8,
932f72300c5SHaggai Abramovsky 				 (__u8)MLX5_CAP_GEN(dev->mdev, cqe_version),
933f72300c5SHaggai Abramovsky 				 req.max_cqe_version);
934b368d7cbSMatan Barak 	resp.response_length = min(offsetof(typeof(resp), response_length) +
935b368d7cbSMatan Barak 				   sizeof(resp.response_length), udata->outlen);
936e126ba97SEli Cohen 
937e126ba97SEli Cohen 	context = kzalloc(sizeof(*context), GFP_KERNEL);
938e126ba97SEli Cohen 	if (!context)
939e126ba97SEli Cohen 		return ERR_PTR(-ENOMEM);
940e126ba97SEli Cohen 
941e126ba97SEli Cohen 	uuari = &context->uuari;
942e126ba97SEli Cohen 	mutex_init(&uuari->lock);
943e126ba97SEli Cohen 	uars = kcalloc(num_uars, sizeof(*uars), GFP_KERNEL);
944e126ba97SEli Cohen 	if (!uars) {
945e126ba97SEli Cohen 		err = -ENOMEM;
946e126ba97SEli Cohen 		goto out_ctx;
947e126ba97SEli Cohen 	}
948e126ba97SEli Cohen 
949c1be5232SEli Cohen 	uuari->bitmap = kcalloc(BITS_TO_LONGS(gross_uuars),
950e126ba97SEli Cohen 				sizeof(*uuari->bitmap),
951e126ba97SEli Cohen 				GFP_KERNEL);
952e126ba97SEli Cohen 	if (!uuari->bitmap) {
953e126ba97SEli Cohen 		err = -ENOMEM;
954e126ba97SEli Cohen 		goto out_uar_ctx;
955e126ba97SEli Cohen 	}
956e126ba97SEli Cohen 	/*
957e126ba97SEli Cohen 	 * clear all fast path uuars
958e126ba97SEli Cohen 	 */
959c1be5232SEli Cohen 	for (i = 0; i < gross_uuars; i++) {
960e126ba97SEli Cohen 		uuarn = i & 3;
961e126ba97SEli Cohen 		if (uuarn == 2 || uuarn == 3)
962e126ba97SEli Cohen 			set_bit(i, uuari->bitmap);
963e126ba97SEli Cohen 	}
964e126ba97SEli Cohen 
965c1be5232SEli Cohen 	uuari->count = kcalloc(gross_uuars, sizeof(*uuari->count), GFP_KERNEL);
966e126ba97SEli Cohen 	if (!uuari->count) {
967e126ba97SEli Cohen 		err = -ENOMEM;
968e126ba97SEli Cohen 		goto out_bitmap;
969e126ba97SEli Cohen 	}
970e126ba97SEli Cohen 
971e126ba97SEli Cohen 	for (i = 0; i < num_uars; i++) {
9729603b61dSJack Morgenstein 		err = mlx5_cmd_alloc_uar(dev->mdev, &uars[i].index);
973e126ba97SEli Cohen 		if (err)
974e126ba97SEli Cohen 			goto out_count;
975e126ba97SEli Cohen 	}
976e126ba97SEli Cohen 
977b4cfe447SHaggai Eran #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
978b4cfe447SHaggai Eran 	context->ibucontext.invalidate_range = &mlx5_ib_invalidate_range;
979b4cfe447SHaggai Eran #endif
980b4cfe447SHaggai Eran 
981146d2f1aSmajd@mellanox.com 	if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain)) {
982146d2f1aSmajd@mellanox.com 		err = mlx5_core_alloc_transport_domain(dev->mdev,
983146d2f1aSmajd@mellanox.com 						       &context->tdn);
984146d2f1aSmajd@mellanox.com 		if (err)
985146d2f1aSmajd@mellanox.com 			goto out_uars;
986146d2f1aSmajd@mellanox.com 	}
987146d2f1aSmajd@mellanox.com 
9887c2344c3SMaor Gottlieb 	INIT_LIST_HEAD(&context->vma_private_list);
989e126ba97SEli Cohen 	INIT_LIST_HEAD(&context->db_page_list);
990e126ba97SEli Cohen 	mutex_init(&context->db_page_mutex);
991e126ba97SEli Cohen 
992e126ba97SEli Cohen 	resp.tot_uuars = req.total_num_uuars;
993938fe83cSSaeed Mahameed 	resp.num_ports = MLX5_CAP_GEN(dev->mdev, num_ports);
994b368d7cbSMatan Barak 
995f72300c5SHaggai Abramovsky 	if (field_avail(typeof(resp), cqe_version, udata->outlen))
996f72300c5SHaggai Abramovsky 		resp.response_length += sizeof(resp.cqe_version);
997b368d7cbSMatan Barak 
998bc5c6eedSNoa Osherovich 	/*
999bc5c6eedSNoa Osherovich 	 * We don't want to expose information from the PCI bar that is located
1000bc5c6eedSNoa Osherovich 	 * after 4096 bytes, so if the arch only supports larger pages, let's
1001bc5c6eedSNoa Osherovich 	 * pretend we don't support reading the HCA's core clock. This is also
1002bc5c6eedSNoa Osherovich 	 * forced by mmap function.
1003bc5c6eedSNoa Osherovich 	 */
1004bc5c6eedSNoa Osherovich 	if (PAGE_SIZE <= 4096 &&
1005bc5c6eedSNoa Osherovich 	    field_avail(typeof(resp), hca_core_clock_offset, udata->outlen)) {
1006b368d7cbSMatan Barak 		resp.comp_mask |=
1007b368d7cbSMatan Barak 			MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_CORE_CLOCK_OFFSET;
1008b368d7cbSMatan Barak 		resp.hca_core_clock_offset =
1009b368d7cbSMatan Barak 			offsetof(struct mlx5_init_seg, internal_timer_h) %
1010b368d7cbSMatan Barak 			PAGE_SIZE;
1011f72300c5SHaggai Abramovsky 		resp.response_length += sizeof(resp.hca_core_clock_offset) +
1012f72300c5SHaggai Abramovsky 					sizeof(resp.reserved2) +
1013f72300c5SHaggai Abramovsky 					sizeof(resp.reserved3);
1014b368d7cbSMatan Barak 	}
1015b368d7cbSMatan Barak 
1016b368d7cbSMatan Barak 	err = ib_copy_to_udata(udata, &resp, resp.response_length);
1017e126ba97SEli Cohen 	if (err)
1018146d2f1aSmajd@mellanox.com 		goto out_td;
1019e126ba97SEli Cohen 
102078c0f98cSEli Cohen 	uuari->ver = ver;
1021e126ba97SEli Cohen 	uuari->num_low_latency_uuars = req.num_low_latency_uuars;
1022e126ba97SEli Cohen 	uuari->uars = uars;
1023e126ba97SEli Cohen 	uuari->num_uars = num_uars;
1024f72300c5SHaggai Abramovsky 	context->cqe_version = resp.cqe_version;
1025f72300c5SHaggai Abramovsky 
1026e126ba97SEli Cohen 	return &context->ibucontext;
1027e126ba97SEli Cohen 
1028146d2f1aSmajd@mellanox.com out_td:
1029146d2f1aSmajd@mellanox.com 	if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1030146d2f1aSmajd@mellanox.com 		mlx5_core_dealloc_transport_domain(dev->mdev, context->tdn);
1031146d2f1aSmajd@mellanox.com 
1032e126ba97SEli Cohen out_uars:
1033e126ba97SEli Cohen 	for (i--; i >= 0; i--)
10349603b61dSJack Morgenstein 		mlx5_cmd_free_uar(dev->mdev, uars[i].index);
1035e126ba97SEli Cohen out_count:
1036e126ba97SEli Cohen 	kfree(uuari->count);
1037e126ba97SEli Cohen 
1038e126ba97SEli Cohen out_bitmap:
1039e126ba97SEli Cohen 	kfree(uuari->bitmap);
1040e126ba97SEli Cohen 
1041e126ba97SEli Cohen out_uar_ctx:
1042e126ba97SEli Cohen 	kfree(uars);
1043e126ba97SEli Cohen 
1044e126ba97SEli Cohen out_ctx:
1045e126ba97SEli Cohen 	kfree(context);
1046e126ba97SEli Cohen 	return ERR_PTR(err);
1047e126ba97SEli Cohen }
1048e126ba97SEli Cohen 
1049e126ba97SEli Cohen static int mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
1050e126ba97SEli Cohen {
1051e126ba97SEli Cohen 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1052e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
1053e126ba97SEli Cohen 	struct mlx5_uuar_info *uuari = &context->uuari;
1054e126ba97SEli Cohen 	int i;
1055e126ba97SEli Cohen 
1056146d2f1aSmajd@mellanox.com 	if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1057146d2f1aSmajd@mellanox.com 		mlx5_core_dealloc_transport_domain(dev->mdev, context->tdn);
1058146d2f1aSmajd@mellanox.com 
1059e126ba97SEli Cohen 	for (i = 0; i < uuari->num_uars; i++) {
10609603b61dSJack Morgenstein 		if (mlx5_cmd_free_uar(dev->mdev, uuari->uars[i].index))
1061e126ba97SEli Cohen 			mlx5_ib_warn(dev, "failed to free UAR 0x%x\n", uuari->uars[i].index);
1062e126ba97SEli Cohen 	}
1063e126ba97SEli Cohen 
1064e126ba97SEli Cohen 	kfree(uuari->count);
1065e126ba97SEli Cohen 	kfree(uuari->bitmap);
1066e126ba97SEli Cohen 	kfree(uuari->uars);
1067e126ba97SEli Cohen 	kfree(context);
1068e126ba97SEli Cohen 
1069e126ba97SEli Cohen 	return 0;
1070e126ba97SEli Cohen }
1071e126ba97SEli Cohen 
1072e126ba97SEli Cohen static phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev, int index)
1073e126ba97SEli Cohen {
10749603b61dSJack Morgenstein 	return (pci_resource_start(dev->mdev->pdev, 0) >> PAGE_SHIFT) + index;
1075e126ba97SEli Cohen }
1076e126ba97SEli Cohen 
1077e126ba97SEli Cohen static int get_command(unsigned long offset)
1078e126ba97SEli Cohen {
1079e126ba97SEli Cohen 	return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK;
1080e126ba97SEli Cohen }
1081e126ba97SEli Cohen 
1082e126ba97SEli Cohen static int get_arg(unsigned long offset)
1083e126ba97SEli Cohen {
1084e126ba97SEli Cohen 	return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1);
1085e126ba97SEli Cohen }
1086e126ba97SEli Cohen 
1087e126ba97SEli Cohen static int get_index(unsigned long offset)
1088e126ba97SEli Cohen {
1089e126ba97SEli Cohen 	return get_arg(offset);
1090e126ba97SEli Cohen }
1091e126ba97SEli Cohen 
10927c2344c3SMaor Gottlieb static void  mlx5_ib_vma_open(struct vm_area_struct *area)
10937c2344c3SMaor Gottlieb {
10947c2344c3SMaor Gottlieb 	/* vma_open is called when a new VMA is created on top of our VMA.  This
10957c2344c3SMaor Gottlieb 	 * is done through either mremap flow or split_vma (usually due to
10967c2344c3SMaor Gottlieb 	 * mlock, madvise, munmap, etc.) We do not support a clone of the VMA,
10977c2344c3SMaor Gottlieb 	 * as this VMA is strongly hardware related.  Therefore we set the
10987c2344c3SMaor Gottlieb 	 * vm_ops of the newly created/cloned VMA to NULL, to prevent it from
10997c2344c3SMaor Gottlieb 	 * calling us again and trying to do incorrect actions.  We assume that
11007c2344c3SMaor Gottlieb 	 * the original VMA size is exactly a single page, and therefore all
11017c2344c3SMaor Gottlieb 	 * "splitting" operation will not happen to it.
11027c2344c3SMaor Gottlieb 	 */
11037c2344c3SMaor Gottlieb 	area->vm_ops = NULL;
11047c2344c3SMaor Gottlieb }
11057c2344c3SMaor Gottlieb 
11067c2344c3SMaor Gottlieb static void  mlx5_ib_vma_close(struct vm_area_struct *area)
11077c2344c3SMaor Gottlieb {
11087c2344c3SMaor Gottlieb 	struct mlx5_ib_vma_private_data *mlx5_ib_vma_priv_data;
11097c2344c3SMaor Gottlieb 
11107c2344c3SMaor Gottlieb 	/* It's guaranteed that all VMAs opened on a FD are closed before the
11117c2344c3SMaor Gottlieb 	 * file itself is closed, therefore no sync is needed with the regular
11127c2344c3SMaor Gottlieb 	 * closing flow. (e.g. mlx5 ib_dealloc_ucontext)
11137c2344c3SMaor Gottlieb 	 * However need a sync with accessing the vma as part of
11147c2344c3SMaor Gottlieb 	 * mlx5_ib_disassociate_ucontext.
11157c2344c3SMaor Gottlieb 	 * The close operation is usually called under mm->mmap_sem except when
11167c2344c3SMaor Gottlieb 	 * process is exiting.
11177c2344c3SMaor Gottlieb 	 * The exiting case is handled explicitly as part of
11187c2344c3SMaor Gottlieb 	 * mlx5_ib_disassociate_ucontext.
11197c2344c3SMaor Gottlieb 	 */
11207c2344c3SMaor Gottlieb 	mlx5_ib_vma_priv_data = (struct mlx5_ib_vma_private_data *)area->vm_private_data;
11217c2344c3SMaor Gottlieb 
11227c2344c3SMaor Gottlieb 	/* setting the vma context pointer to null in the mlx5_ib driver's
11237c2344c3SMaor Gottlieb 	 * private data, to protect a race condition in
11247c2344c3SMaor Gottlieb 	 * mlx5_ib_disassociate_ucontext().
11257c2344c3SMaor Gottlieb 	 */
11267c2344c3SMaor Gottlieb 	mlx5_ib_vma_priv_data->vma = NULL;
11277c2344c3SMaor Gottlieb 	list_del(&mlx5_ib_vma_priv_data->list);
11287c2344c3SMaor Gottlieb 	kfree(mlx5_ib_vma_priv_data);
11297c2344c3SMaor Gottlieb }
11307c2344c3SMaor Gottlieb 
11317c2344c3SMaor Gottlieb static const struct vm_operations_struct mlx5_ib_vm_ops = {
11327c2344c3SMaor Gottlieb 	.open = mlx5_ib_vma_open,
11337c2344c3SMaor Gottlieb 	.close = mlx5_ib_vma_close
11347c2344c3SMaor Gottlieb };
11357c2344c3SMaor Gottlieb 
11367c2344c3SMaor Gottlieb static int mlx5_ib_set_vma_data(struct vm_area_struct *vma,
11377c2344c3SMaor Gottlieb 				struct mlx5_ib_ucontext *ctx)
11387c2344c3SMaor Gottlieb {
11397c2344c3SMaor Gottlieb 	struct mlx5_ib_vma_private_data *vma_prv;
11407c2344c3SMaor Gottlieb 	struct list_head *vma_head = &ctx->vma_private_list;
11417c2344c3SMaor Gottlieb 
11427c2344c3SMaor Gottlieb 	vma_prv = kzalloc(sizeof(*vma_prv), GFP_KERNEL);
11437c2344c3SMaor Gottlieb 	if (!vma_prv)
11447c2344c3SMaor Gottlieb 		return -ENOMEM;
11457c2344c3SMaor Gottlieb 
11467c2344c3SMaor Gottlieb 	vma_prv->vma = vma;
11477c2344c3SMaor Gottlieb 	vma->vm_private_data = vma_prv;
11487c2344c3SMaor Gottlieb 	vma->vm_ops =  &mlx5_ib_vm_ops;
11497c2344c3SMaor Gottlieb 
11507c2344c3SMaor Gottlieb 	list_add(&vma_prv->list, vma_head);
11517c2344c3SMaor Gottlieb 
11527c2344c3SMaor Gottlieb 	return 0;
11537c2344c3SMaor Gottlieb }
11547c2344c3SMaor Gottlieb 
11557c2344c3SMaor Gottlieb static void mlx5_ib_disassociate_ucontext(struct ib_ucontext *ibcontext)
11567c2344c3SMaor Gottlieb {
11577c2344c3SMaor Gottlieb 	int ret;
11587c2344c3SMaor Gottlieb 	struct vm_area_struct *vma;
11597c2344c3SMaor Gottlieb 	struct mlx5_ib_vma_private_data *vma_private, *n;
11607c2344c3SMaor Gottlieb 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
11617c2344c3SMaor Gottlieb 	struct task_struct *owning_process  = NULL;
11627c2344c3SMaor Gottlieb 	struct mm_struct   *owning_mm       = NULL;
11637c2344c3SMaor Gottlieb 
11647c2344c3SMaor Gottlieb 	owning_process = get_pid_task(ibcontext->tgid, PIDTYPE_PID);
11657c2344c3SMaor Gottlieb 	if (!owning_process)
11667c2344c3SMaor Gottlieb 		return;
11677c2344c3SMaor Gottlieb 
11687c2344c3SMaor Gottlieb 	owning_mm = get_task_mm(owning_process);
11697c2344c3SMaor Gottlieb 	if (!owning_mm) {
11707c2344c3SMaor Gottlieb 		pr_info("no mm, disassociate ucontext is pending task termination\n");
11717c2344c3SMaor Gottlieb 		while (1) {
11727c2344c3SMaor Gottlieb 			put_task_struct(owning_process);
11737c2344c3SMaor Gottlieb 			usleep_range(1000, 2000);
11747c2344c3SMaor Gottlieb 			owning_process = get_pid_task(ibcontext->tgid,
11757c2344c3SMaor Gottlieb 						      PIDTYPE_PID);
11767c2344c3SMaor Gottlieb 			if (!owning_process ||
11777c2344c3SMaor Gottlieb 			    owning_process->state == TASK_DEAD) {
11787c2344c3SMaor Gottlieb 				pr_info("disassociate ucontext done, task was terminated\n");
11797c2344c3SMaor Gottlieb 				/* in case task was dead need to release the
11807c2344c3SMaor Gottlieb 				 * task struct.
11817c2344c3SMaor Gottlieb 				 */
11827c2344c3SMaor Gottlieb 				if (owning_process)
11837c2344c3SMaor Gottlieb 					put_task_struct(owning_process);
11847c2344c3SMaor Gottlieb 				return;
11857c2344c3SMaor Gottlieb 			}
11867c2344c3SMaor Gottlieb 		}
11877c2344c3SMaor Gottlieb 	}
11887c2344c3SMaor Gottlieb 
11897c2344c3SMaor Gottlieb 	/* need to protect from a race on closing the vma as part of
11907c2344c3SMaor Gottlieb 	 * mlx5_ib_vma_close.
11917c2344c3SMaor Gottlieb 	 */
11927c2344c3SMaor Gottlieb 	down_read(&owning_mm->mmap_sem);
11937c2344c3SMaor Gottlieb 	list_for_each_entry_safe(vma_private, n, &context->vma_private_list,
11947c2344c3SMaor Gottlieb 				 list) {
11957c2344c3SMaor Gottlieb 		vma = vma_private->vma;
11967c2344c3SMaor Gottlieb 		ret = zap_vma_ptes(vma, vma->vm_start,
11977c2344c3SMaor Gottlieb 				   PAGE_SIZE);
11987c2344c3SMaor Gottlieb 		WARN_ONCE(ret, "%s: zap_vma_ptes failed", __func__);
11997c2344c3SMaor Gottlieb 		/* context going to be destroyed, should
12007c2344c3SMaor Gottlieb 		 * not access ops any more.
12017c2344c3SMaor Gottlieb 		 */
12027c2344c3SMaor Gottlieb 		vma->vm_ops = NULL;
12037c2344c3SMaor Gottlieb 		list_del(&vma_private->list);
12047c2344c3SMaor Gottlieb 		kfree(vma_private);
12057c2344c3SMaor Gottlieb 	}
12067c2344c3SMaor Gottlieb 	up_read(&owning_mm->mmap_sem);
12077c2344c3SMaor Gottlieb 	mmput(owning_mm);
12087c2344c3SMaor Gottlieb 	put_task_struct(owning_process);
12097c2344c3SMaor Gottlieb }
12107c2344c3SMaor Gottlieb 
121137aa5c36SGuy Levi static inline char *mmap_cmd2str(enum mlx5_ib_mmap_cmd cmd)
1212e126ba97SEli Cohen {
121337aa5c36SGuy Levi 	switch (cmd) {
121437aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
121537aa5c36SGuy Levi 		return "WC";
1216e126ba97SEli Cohen 	case MLX5_IB_MMAP_REGULAR_PAGE:
121737aa5c36SGuy Levi 		return "best effort WC";
121837aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
121937aa5c36SGuy Levi 		return "NC";
122037aa5c36SGuy Levi 	default:
122137aa5c36SGuy Levi 		return NULL;
122237aa5c36SGuy Levi 	}
122337aa5c36SGuy Levi }
122437aa5c36SGuy Levi 
122537aa5c36SGuy Levi static int uar_mmap(struct mlx5_ib_dev *dev, enum mlx5_ib_mmap_cmd cmd,
12267c2344c3SMaor Gottlieb 		    struct vm_area_struct *vma,
12277c2344c3SMaor Gottlieb 		    struct mlx5_ib_ucontext *context)
122837aa5c36SGuy Levi {
12297c2344c3SMaor Gottlieb 	struct mlx5_uuar_info *uuari = &context->uuari;
123037aa5c36SGuy Levi 	int err;
123137aa5c36SGuy Levi 	unsigned long idx;
123237aa5c36SGuy Levi 	phys_addr_t pfn, pa;
123337aa5c36SGuy Levi 	pgprot_t prot;
123437aa5c36SGuy Levi 
123537aa5c36SGuy Levi 	switch (cmd) {
123637aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
123737aa5c36SGuy Levi /* Some architectures don't support WC memory */
123837aa5c36SGuy Levi #if defined(CONFIG_X86)
123937aa5c36SGuy Levi 		if (!pat_enabled())
124037aa5c36SGuy Levi 			return -EPERM;
124137aa5c36SGuy Levi #elif !(defined(CONFIG_PPC) || (defined(CONFIG_ARM) && defined(CONFIG_MMU)))
124237aa5c36SGuy Levi 			return -EPERM;
124337aa5c36SGuy Levi #endif
124437aa5c36SGuy Levi 	/* fall through */
124537aa5c36SGuy Levi 	case MLX5_IB_MMAP_REGULAR_PAGE:
124637aa5c36SGuy Levi 		/* For MLX5_IB_MMAP_REGULAR_PAGE do the best effort to get WC */
124737aa5c36SGuy Levi 		prot = pgprot_writecombine(vma->vm_page_prot);
124837aa5c36SGuy Levi 		break;
124937aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
125037aa5c36SGuy Levi 		prot = pgprot_noncached(vma->vm_page_prot);
125137aa5c36SGuy Levi 		break;
125237aa5c36SGuy Levi 	default:
125337aa5c36SGuy Levi 		return -EINVAL;
125437aa5c36SGuy Levi 	}
125537aa5c36SGuy Levi 
1256e126ba97SEli Cohen 	if (vma->vm_end - vma->vm_start != PAGE_SIZE)
1257e126ba97SEli Cohen 		return -EINVAL;
1258e126ba97SEli Cohen 
1259e126ba97SEli Cohen 	idx = get_index(vma->vm_pgoff);
12601c3ce90dSEli Cohen 	if (idx >= uuari->num_uars)
12611c3ce90dSEli Cohen 		return -EINVAL;
12621c3ce90dSEli Cohen 
1263e126ba97SEli Cohen 	pfn = uar_index2pfn(dev, uuari->uars[idx].index);
126437aa5c36SGuy Levi 	mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn %pa\n", idx, &pfn);
1265e126ba97SEli Cohen 
126637aa5c36SGuy Levi 	vma->vm_page_prot = prot;
126737aa5c36SGuy Levi 	err = io_remap_pfn_range(vma, vma->vm_start, pfn,
126837aa5c36SGuy Levi 				 PAGE_SIZE, vma->vm_page_prot);
126937aa5c36SGuy Levi 	if (err) {
127037aa5c36SGuy Levi 		mlx5_ib_err(dev, "io_remap_pfn_range failed with error=%d, vm_start=0x%lx, pfn=%pa, mmap_cmd=%s\n",
127137aa5c36SGuy Levi 			    err, vma->vm_start, &pfn, mmap_cmd2str(cmd));
1272e126ba97SEli Cohen 		return -EAGAIN;
127337aa5c36SGuy Levi 	}
1274e126ba97SEli Cohen 
127537aa5c36SGuy Levi 	pa = pfn << PAGE_SHIFT;
127637aa5c36SGuy Levi 	mlx5_ib_dbg(dev, "mapped %s at 0x%lx, PA %pa\n", mmap_cmd2str(cmd),
127737aa5c36SGuy Levi 		    vma->vm_start, &pa);
127837aa5c36SGuy Levi 
12797c2344c3SMaor Gottlieb 	return mlx5_ib_set_vma_data(vma, context);
128037aa5c36SGuy Levi }
128137aa5c36SGuy Levi 
128237aa5c36SGuy Levi static int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma)
128337aa5c36SGuy Levi {
128437aa5c36SGuy Levi 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
128537aa5c36SGuy Levi 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
128637aa5c36SGuy Levi 	unsigned long command;
128737aa5c36SGuy Levi 	phys_addr_t pfn;
128837aa5c36SGuy Levi 
128937aa5c36SGuy Levi 	command = get_command(vma->vm_pgoff);
129037aa5c36SGuy Levi 	switch (command) {
129137aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
129237aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
129337aa5c36SGuy Levi 	case MLX5_IB_MMAP_REGULAR_PAGE:
12947c2344c3SMaor Gottlieb 		return uar_mmap(dev, command, vma, context);
1295e126ba97SEli Cohen 
1296e126ba97SEli Cohen 	case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES:
1297e126ba97SEli Cohen 		return -ENOSYS;
1298e126ba97SEli Cohen 
1299d69e3bcfSMatan Barak 	case MLX5_IB_MMAP_CORE_CLOCK:
1300d69e3bcfSMatan Barak 		if (vma->vm_end - vma->vm_start != PAGE_SIZE)
1301d69e3bcfSMatan Barak 			return -EINVAL;
1302d69e3bcfSMatan Barak 
13036cbac1e4SMatan Barak 		if (vma->vm_flags & VM_WRITE)
1304d69e3bcfSMatan Barak 			return -EPERM;
1305d69e3bcfSMatan Barak 
1306d69e3bcfSMatan Barak 		/* Don't expose to user-space information it shouldn't have */
1307d69e3bcfSMatan Barak 		if (PAGE_SIZE > 4096)
1308d69e3bcfSMatan Barak 			return -EOPNOTSUPP;
1309d69e3bcfSMatan Barak 
1310d69e3bcfSMatan Barak 		vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1311d69e3bcfSMatan Barak 		pfn = (dev->mdev->iseg_base +
1312d69e3bcfSMatan Barak 		       offsetof(struct mlx5_init_seg, internal_timer_h)) >>
1313d69e3bcfSMatan Barak 			PAGE_SHIFT;
1314d69e3bcfSMatan Barak 		if (io_remap_pfn_range(vma, vma->vm_start, pfn,
1315d69e3bcfSMatan Barak 				       PAGE_SIZE, vma->vm_page_prot))
1316d69e3bcfSMatan Barak 			return -EAGAIN;
1317d69e3bcfSMatan Barak 
1318d69e3bcfSMatan Barak 		mlx5_ib_dbg(dev, "mapped internal timer at 0x%lx, PA 0x%llx\n",
1319d69e3bcfSMatan Barak 			    vma->vm_start,
1320d69e3bcfSMatan Barak 			    (unsigned long long)pfn << PAGE_SHIFT);
1321d69e3bcfSMatan Barak 		break;
1322d69e3bcfSMatan Barak 
1323e126ba97SEli Cohen 	default:
1324e126ba97SEli Cohen 		return -EINVAL;
1325e126ba97SEli Cohen 	}
1326e126ba97SEli Cohen 
1327e126ba97SEli Cohen 	return 0;
1328e126ba97SEli Cohen }
1329e126ba97SEli Cohen 
1330e126ba97SEli Cohen static struct ib_pd *mlx5_ib_alloc_pd(struct ib_device *ibdev,
1331e126ba97SEli Cohen 				      struct ib_ucontext *context,
1332e126ba97SEli Cohen 				      struct ib_udata *udata)
1333e126ba97SEli Cohen {
1334e126ba97SEli Cohen 	struct mlx5_ib_alloc_pd_resp resp;
1335e126ba97SEli Cohen 	struct mlx5_ib_pd *pd;
1336e126ba97SEli Cohen 	int err;
1337e126ba97SEli Cohen 
1338e126ba97SEli Cohen 	pd = kmalloc(sizeof(*pd), GFP_KERNEL);
1339e126ba97SEli Cohen 	if (!pd)
1340e126ba97SEli Cohen 		return ERR_PTR(-ENOMEM);
1341e126ba97SEli Cohen 
13429603b61dSJack Morgenstein 	err = mlx5_core_alloc_pd(to_mdev(ibdev)->mdev, &pd->pdn);
1343e126ba97SEli Cohen 	if (err) {
1344e126ba97SEli Cohen 		kfree(pd);
1345e126ba97SEli Cohen 		return ERR_PTR(err);
1346e126ba97SEli Cohen 	}
1347e126ba97SEli Cohen 
1348e126ba97SEli Cohen 	if (context) {
1349e126ba97SEli Cohen 		resp.pdn = pd->pdn;
1350e126ba97SEli Cohen 		if (ib_copy_to_udata(udata, &resp, sizeof(resp))) {
13519603b61dSJack Morgenstein 			mlx5_core_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn);
1352e126ba97SEli Cohen 			kfree(pd);
1353e126ba97SEli Cohen 			return ERR_PTR(-EFAULT);
1354e126ba97SEli Cohen 		}
1355e126ba97SEli Cohen 	}
1356e126ba97SEli Cohen 
1357e126ba97SEli Cohen 	return &pd->ibpd;
1358e126ba97SEli Cohen }
1359e126ba97SEli Cohen 
1360e126ba97SEli Cohen static int mlx5_ib_dealloc_pd(struct ib_pd *pd)
1361e126ba97SEli Cohen {
1362e126ba97SEli Cohen 	struct mlx5_ib_dev *mdev = to_mdev(pd->device);
1363e126ba97SEli Cohen 	struct mlx5_ib_pd *mpd = to_mpd(pd);
1364e126ba97SEli Cohen 
13659603b61dSJack Morgenstein 	mlx5_core_dealloc_pd(mdev->mdev, mpd->pdn);
1366e126ba97SEli Cohen 	kfree(mpd);
1367e126ba97SEli Cohen 
1368e126ba97SEli Cohen 	return 0;
1369e126ba97SEli Cohen }
1370e126ba97SEli Cohen 
1371038d2ef8SMaor Gottlieb static bool outer_header_zero(u32 *match_criteria)
1372038d2ef8SMaor Gottlieb {
1373038d2ef8SMaor Gottlieb 	int size = MLX5_ST_SZ_BYTES(fte_match_param);
1374038d2ef8SMaor Gottlieb 	char *outer_headers_c = MLX5_ADDR_OF(fte_match_param, match_criteria,
1375038d2ef8SMaor Gottlieb 					     outer_headers);
1376038d2ef8SMaor Gottlieb 
1377038d2ef8SMaor Gottlieb 	return outer_headers_c[0] == 0 && !memcmp(outer_headers_c,
1378038d2ef8SMaor Gottlieb 						  outer_headers_c + 1,
1379038d2ef8SMaor Gottlieb 						  size - 1);
1380038d2ef8SMaor Gottlieb }
1381038d2ef8SMaor Gottlieb 
1382038d2ef8SMaor Gottlieb static int parse_flow_attr(u32 *match_c, u32 *match_v,
1383038d2ef8SMaor Gottlieb 			   union ib_flow_spec *ib_spec)
1384038d2ef8SMaor Gottlieb {
1385038d2ef8SMaor Gottlieb 	void *outer_headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
1386038d2ef8SMaor Gottlieb 					     outer_headers);
1387038d2ef8SMaor Gottlieb 	void *outer_headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
1388038d2ef8SMaor Gottlieb 					     outer_headers);
1389038d2ef8SMaor Gottlieb 	switch (ib_spec->type) {
1390038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_ETH:
1391038d2ef8SMaor Gottlieb 		if (ib_spec->size != sizeof(ib_spec->eth))
1392038d2ef8SMaor Gottlieb 			return -EINVAL;
1393038d2ef8SMaor Gottlieb 
1394038d2ef8SMaor Gottlieb 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_c,
1395038d2ef8SMaor Gottlieb 					     dmac_47_16),
1396038d2ef8SMaor Gottlieb 				ib_spec->eth.mask.dst_mac);
1397038d2ef8SMaor Gottlieb 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_v,
1398038d2ef8SMaor Gottlieb 					     dmac_47_16),
1399038d2ef8SMaor Gottlieb 				ib_spec->eth.val.dst_mac);
1400038d2ef8SMaor Gottlieb 
1401038d2ef8SMaor Gottlieb 		if (ib_spec->eth.mask.vlan_tag) {
1402038d2ef8SMaor Gottlieb 			MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c,
1403038d2ef8SMaor Gottlieb 				 vlan_tag, 1);
1404038d2ef8SMaor Gottlieb 			MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v,
1405038d2ef8SMaor Gottlieb 				 vlan_tag, 1);
1406038d2ef8SMaor Gottlieb 
1407038d2ef8SMaor Gottlieb 			MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c,
1408038d2ef8SMaor Gottlieb 				 first_vid, ntohs(ib_spec->eth.mask.vlan_tag));
1409038d2ef8SMaor Gottlieb 			MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v,
1410038d2ef8SMaor Gottlieb 				 first_vid, ntohs(ib_spec->eth.val.vlan_tag));
1411038d2ef8SMaor Gottlieb 
1412038d2ef8SMaor Gottlieb 			MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c,
1413038d2ef8SMaor Gottlieb 				 first_cfi,
1414038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.mask.vlan_tag) >> 12);
1415038d2ef8SMaor Gottlieb 			MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v,
1416038d2ef8SMaor Gottlieb 				 first_cfi,
1417038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.val.vlan_tag) >> 12);
1418038d2ef8SMaor Gottlieb 
1419038d2ef8SMaor Gottlieb 			MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c,
1420038d2ef8SMaor Gottlieb 				 first_prio,
1421038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.mask.vlan_tag) >> 13);
1422038d2ef8SMaor Gottlieb 			MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v,
1423038d2ef8SMaor Gottlieb 				 first_prio,
1424038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.val.vlan_tag) >> 13);
1425038d2ef8SMaor Gottlieb 		}
1426038d2ef8SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c,
1427038d2ef8SMaor Gottlieb 			 ethertype, ntohs(ib_spec->eth.mask.ether_type));
1428038d2ef8SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v,
1429038d2ef8SMaor Gottlieb 			 ethertype, ntohs(ib_spec->eth.val.ether_type));
1430038d2ef8SMaor Gottlieb 		break;
1431038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_IPV4:
1432038d2ef8SMaor Gottlieb 		if (ib_spec->size != sizeof(ib_spec->ipv4))
1433038d2ef8SMaor Gottlieb 			return -EINVAL;
1434038d2ef8SMaor Gottlieb 
1435038d2ef8SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c,
1436038d2ef8SMaor Gottlieb 			 ethertype, 0xffff);
1437038d2ef8SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v,
1438038d2ef8SMaor Gottlieb 			 ethertype, ETH_P_IP);
1439038d2ef8SMaor Gottlieb 
1440038d2ef8SMaor Gottlieb 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_c,
1441038d2ef8SMaor Gottlieb 				    src_ipv4_src_ipv6.ipv4_layout.ipv4),
1442038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.mask.src_ip,
1443038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.mask.src_ip));
1444038d2ef8SMaor Gottlieb 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_v,
1445038d2ef8SMaor Gottlieb 				    src_ipv4_src_ipv6.ipv4_layout.ipv4),
1446038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.val.src_ip,
1447038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.val.src_ip));
1448038d2ef8SMaor Gottlieb 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_c,
1449038d2ef8SMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
1450038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.mask.dst_ip,
1451038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.mask.dst_ip));
1452038d2ef8SMaor Gottlieb 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_v,
1453038d2ef8SMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
1454038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.val.dst_ip,
1455038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.val.dst_ip));
1456038d2ef8SMaor Gottlieb 		break;
1457*026bae0cSMaor Gottlieb 	case IB_FLOW_SPEC_IPV6:
1458*026bae0cSMaor Gottlieb 		if (ib_spec->size != sizeof(ib_spec->ipv6))
1459*026bae0cSMaor Gottlieb 			return -EINVAL;
1460*026bae0cSMaor Gottlieb 
1461*026bae0cSMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c,
1462*026bae0cSMaor Gottlieb 			 ethertype, 0xffff);
1463*026bae0cSMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v,
1464*026bae0cSMaor Gottlieb 			 ethertype, ETH_P_IPV6);
1465*026bae0cSMaor Gottlieb 
1466*026bae0cSMaor Gottlieb 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_c,
1467*026bae0cSMaor Gottlieb 				    src_ipv4_src_ipv6.ipv6_layout.ipv6),
1468*026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.mask.src_ip,
1469*026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.mask.src_ip));
1470*026bae0cSMaor Gottlieb 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_v,
1471*026bae0cSMaor Gottlieb 				    src_ipv4_src_ipv6.ipv6_layout.ipv6),
1472*026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.val.src_ip,
1473*026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.val.src_ip));
1474*026bae0cSMaor Gottlieb 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_c,
1475*026bae0cSMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
1476*026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.mask.dst_ip,
1477*026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.mask.dst_ip));
1478*026bae0cSMaor Gottlieb 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_v,
1479*026bae0cSMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
1480*026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.val.dst_ip,
1481*026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.val.dst_ip));
1482*026bae0cSMaor Gottlieb 		break;
1483038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_TCP:
1484038d2ef8SMaor Gottlieb 		if (ib_spec->size != sizeof(ib_spec->tcp_udp))
1485038d2ef8SMaor Gottlieb 			return -EINVAL;
1486038d2ef8SMaor Gottlieb 
1487038d2ef8SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, ip_protocol,
1488038d2ef8SMaor Gottlieb 			 0xff);
1489038d2ef8SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, ip_protocol,
1490038d2ef8SMaor Gottlieb 			 IPPROTO_TCP);
1491038d2ef8SMaor Gottlieb 
1492038d2ef8SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, tcp_sport,
1493038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.src_port));
1494038d2ef8SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, tcp_sport,
1495038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.src_port));
1496038d2ef8SMaor Gottlieb 
1497038d2ef8SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, tcp_dport,
1498038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.dst_port));
1499038d2ef8SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, tcp_dport,
1500038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.dst_port));
1501038d2ef8SMaor Gottlieb 		break;
1502038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_UDP:
1503038d2ef8SMaor Gottlieb 		if (ib_spec->size != sizeof(ib_spec->tcp_udp))
1504038d2ef8SMaor Gottlieb 			return -EINVAL;
1505038d2ef8SMaor Gottlieb 
1506038d2ef8SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, ip_protocol,
1507038d2ef8SMaor Gottlieb 			 0xff);
1508038d2ef8SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, ip_protocol,
1509038d2ef8SMaor Gottlieb 			 IPPROTO_UDP);
1510038d2ef8SMaor Gottlieb 
1511038d2ef8SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, udp_sport,
1512038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.src_port));
1513038d2ef8SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, udp_sport,
1514038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.src_port));
1515038d2ef8SMaor Gottlieb 
1516038d2ef8SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, udp_dport,
1517038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.dst_port));
1518038d2ef8SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, udp_dport,
1519038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.dst_port));
1520038d2ef8SMaor Gottlieb 		break;
1521038d2ef8SMaor Gottlieb 	default:
1522038d2ef8SMaor Gottlieb 		return -EINVAL;
1523038d2ef8SMaor Gottlieb 	}
1524038d2ef8SMaor Gottlieb 
1525038d2ef8SMaor Gottlieb 	return 0;
1526038d2ef8SMaor Gottlieb }
1527038d2ef8SMaor Gottlieb 
1528038d2ef8SMaor Gottlieb /* If a flow could catch both multicast and unicast packets,
1529038d2ef8SMaor Gottlieb  * it won't fall into the multicast flow steering table and this rule
1530038d2ef8SMaor Gottlieb  * could steal other multicast packets.
1531038d2ef8SMaor Gottlieb  */
1532038d2ef8SMaor Gottlieb static bool flow_is_multicast_only(struct ib_flow_attr *ib_attr)
1533038d2ef8SMaor Gottlieb {
1534038d2ef8SMaor Gottlieb 	struct ib_flow_spec_eth *eth_spec;
1535038d2ef8SMaor Gottlieb 
1536038d2ef8SMaor Gottlieb 	if (ib_attr->type != IB_FLOW_ATTR_NORMAL ||
1537038d2ef8SMaor Gottlieb 	    ib_attr->size < sizeof(struct ib_flow_attr) +
1538038d2ef8SMaor Gottlieb 	    sizeof(struct ib_flow_spec_eth) ||
1539038d2ef8SMaor Gottlieb 	    ib_attr->num_of_specs < 1)
1540038d2ef8SMaor Gottlieb 		return false;
1541038d2ef8SMaor Gottlieb 
1542038d2ef8SMaor Gottlieb 	eth_spec = (struct ib_flow_spec_eth *)(ib_attr + 1);
1543038d2ef8SMaor Gottlieb 	if (eth_spec->type != IB_FLOW_SPEC_ETH ||
1544038d2ef8SMaor Gottlieb 	    eth_spec->size != sizeof(*eth_spec))
1545038d2ef8SMaor Gottlieb 		return false;
1546038d2ef8SMaor Gottlieb 
1547038d2ef8SMaor Gottlieb 	return is_multicast_ether_addr(eth_spec->mask.dst_mac) &&
1548038d2ef8SMaor Gottlieb 	       is_multicast_ether_addr(eth_spec->val.dst_mac);
1549038d2ef8SMaor Gottlieb }
1550038d2ef8SMaor Gottlieb 
1551038d2ef8SMaor Gottlieb static bool is_valid_attr(struct ib_flow_attr *flow_attr)
1552038d2ef8SMaor Gottlieb {
1553038d2ef8SMaor Gottlieb 	union ib_flow_spec *ib_spec = (union ib_flow_spec *)(flow_attr + 1);
1554038d2ef8SMaor Gottlieb 	bool has_ipv4_spec = false;
1555038d2ef8SMaor Gottlieb 	bool eth_type_ipv4 = true;
1556038d2ef8SMaor Gottlieb 	unsigned int spec_index;
1557038d2ef8SMaor Gottlieb 
1558038d2ef8SMaor Gottlieb 	/* Validate that ethertype is correct */
1559038d2ef8SMaor Gottlieb 	for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
1560038d2ef8SMaor Gottlieb 		if (ib_spec->type == IB_FLOW_SPEC_ETH &&
1561038d2ef8SMaor Gottlieb 		    ib_spec->eth.mask.ether_type) {
1562038d2ef8SMaor Gottlieb 			if (!((ib_spec->eth.mask.ether_type == htons(0xffff)) &&
1563038d2ef8SMaor Gottlieb 			      ib_spec->eth.val.ether_type == htons(ETH_P_IP)))
1564038d2ef8SMaor Gottlieb 				eth_type_ipv4 = false;
1565038d2ef8SMaor Gottlieb 		} else if (ib_spec->type == IB_FLOW_SPEC_IPV4) {
1566038d2ef8SMaor Gottlieb 			has_ipv4_spec = true;
1567038d2ef8SMaor Gottlieb 		}
1568038d2ef8SMaor Gottlieb 		ib_spec = (void *)ib_spec + ib_spec->size;
1569038d2ef8SMaor Gottlieb 	}
1570038d2ef8SMaor Gottlieb 	return !has_ipv4_spec || eth_type_ipv4;
1571038d2ef8SMaor Gottlieb }
1572038d2ef8SMaor Gottlieb 
1573038d2ef8SMaor Gottlieb static void put_flow_table(struct mlx5_ib_dev *dev,
1574038d2ef8SMaor Gottlieb 			   struct mlx5_ib_flow_prio *prio, bool ft_added)
1575038d2ef8SMaor Gottlieb {
1576038d2ef8SMaor Gottlieb 	prio->refcount -= !!ft_added;
1577038d2ef8SMaor Gottlieb 	if (!prio->refcount) {
1578038d2ef8SMaor Gottlieb 		mlx5_destroy_flow_table(prio->flow_table);
1579038d2ef8SMaor Gottlieb 		prio->flow_table = NULL;
1580038d2ef8SMaor Gottlieb 	}
1581038d2ef8SMaor Gottlieb }
1582038d2ef8SMaor Gottlieb 
1583038d2ef8SMaor Gottlieb static int mlx5_ib_destroy_flow(struct ib_flow *flow_id)
1584038d2ef8SMaor Gottlieb {
1585038d2ef8SMaor Gottlieb 	struct mlx5_ib_dev *dev = to_mdev(flow_id->qp->device);
1586038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = container_of(flow_id,
1587038d2ef8SMaor Gottlieb 							  struct mlx5_ib_flow_handler,
1588038d2ef8SMaor Gottlieb 							  ibflow);
1589038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *iter, *tmp;
1590038d2ef8SMaor Gottlieb 
1591038d2ef8SMaor Gottlieb 	mutex_lock(&dev->flow_db.lock);
1592038d2ef8SMaor Gottlieb 
1593038d2ef8SMaor Gottlieb 	list_for_each_entry_safe(iter, tmp, &handler->list, list) {
1594038d2ef8SMaor Gottlieb 		mlx5_del_flow_rule(iter->rule);
1595038d2ef8SMaor Gottlieb 		list_del(&iter->list);
1596038d2ef8SMaor Gottlieb 		kfree(iter);
1597038d2ef8SMaor Gottlieb 	}
1598038d2ef8SMaor Gottlieb 
1599038d2ef8SMaor Gottlieb 	mlx5_del_flow_rule(handler->rule);
1600038d2ef8SMaor Gottlieb 	put_flow_table(dev, &dev->flow_db.prios[handler->prio], true);
1601038d2ef8SMaor Gottlieb 	mutex_unlock(&dev->flow_db.lock);
1602038d2ef8SMaor Gottlieb 
1603038d2ef8SMaor Gottlieb 	kfree(handler);
1604038d2ef8SMaor Gottlieb 
1605038d2ef8SMaor Gottlieb 	return 0;
1606038d2ef8SMaor Gottlieb }
1607038d2ef8SMaor Gottlieb 
160835d19011SMaor Gottlieb static int ib_prio_to_core_prio(unsigned int priority, bool dont_trap)
160935d19011SMaor Gottlieb {
161035d19011SMaor Gottlieb 	priority *= 2;
161135d19011SMaor Gottlieb 	if (!dont_trap)
161235d19011SMaor Gottlieb 		priority++;
161335d19011SMaor Gottlieb 	return priority;
161435d19011SMaor Gottlieb }
161535d19011SMaor Gottlieb 
1616038d2ef8SMaor Gottlieb #define MLX5_FS_MAX_TYPES	 10
1617038d2ef8SMaor Gottlieb #define MLX5_FS_MAX_ENTRIES	 32000UL
1618038d2ef8SMaor Gottlieb static struct mlx5_ib_flow_prio *get_flow_table(struct mlx5_ib_dev *dev,
1619038d2ef8SMaor Gottlieb 						struct ib_flow_attr *flow_attr)
1620038d2ef8SMaor Gottlieb {
162135d19011SMaor Gottlieb 	bool dont_trap = flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP;
1622038d2ef8SMaor Gottlieb 	struct mlx5_flow_namespace *ns = NULL;
1623038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_prio *prio;
1624038d2ef8SMaor Gottlieb 	struct mlx5_flow_table *ft;
1625038d2ef8SMaor Gottlieb 	int num_entries;
1626038d2ef8SMaor Gottlieb 	int num_groups;
1627038d2ef8SMaor Gottlieb 	int priority;
1628038d2ef8SMaor Gottlieb 	int err = 0;
1629038d2ef8SMaor Gottlieb 
1630038d2ef8SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
163135d19011SMaor Gottlieb 		if (flow_is_multicast_only(flow_attr) &&
163235d19011SMaor Gottlieb 		    !dont_trap)
1633038d2ef8SMaor Gottlieb 			priority = MLX5_IB_FLOW_MCAST_PRIO;
1634038d2ef8SMaor Gottlieb 		else
163535d19011SMaor Gottlieb 			priority = ib_prio_to_core_prio(flow_attr->priority,
163635d19011SMaor Gottlieb 							dont_trap);
1637038d2ef8SMaor Gottlieb 		ns = mlx5_get_flow_namespace(dev->mdev,
1638038d2ef8SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_BYPASS);
1639038d2ef8SMaor Gottlieb 		num_entries = MLX5_FS_MAX_ENTRIES;
1640038d2ef8SMaor Gottlieb 		num_groups = MLX5_FS_MAX_TYPES;
1641038d2ef8SMaor Gottlieb 		prio = &dev->flow_db.prios[priority];
1642038d2ef8SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
1643038d2ef8SMaor Gottlieb 		   flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
1644038d2ef8SMaor Gottlieb 		ns = mlx5_get_flow_namespace(dev->mdev,
1645038d2ef8SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_LEFTOVERS);
1646038d2ef8SMaor Gottlieb 		build_leftovers_ft_param(&priority,
1647038d2ef8SMaor Gottlieb 					 &num_entries,
1648038d2ef8SMaor Gottlieb 					 &num_groups);
1649038d2ef8SMaor Gottlieb 		prio = &dev->flow_db.prios[MLX5_IB_FLOW_LEFTOVERS_PRIO];
1650038d2ef8SMaor Gottlieb 	}
1651038d2ef8SMaor Gottlieb 
1652038d2ef8SMaor Gottlieb 	if (!ns)
1653038d2ef8SMaor Gottlieb 		return ERR_PTR(-ENOTSUPP);
1654038d2ef8SMaor Gottlieb 
1655038d2ef8SMaor Gottlieb 	ft = prio->flow_table;
1656038d2ef8SMaor Gottlieb 	if (!ft) {
1657038d2ef8SMaor Gottlieb 		ft = mlx5_create_auto_grouped_flow_table(ns, priority,
1658038d2ef8SMaor Gottlieb 							 num_entries,
1659d63cd286SMaor Gottlieb 							 num_groups,
1660d63cd286SMaor Gottlieb 							 0);
1661038d2ef8SMaor Gottlieb 
1662038d2ef8SMaor Gottlieb 		if (!IS_ERR(ft)) {
1663038d2ef8SMaor Gottlieb 			prio->refcount = 0;
1664038d2ef8SMaor Gottlieb 			prio->flow_table = ft;
1665038d2ef8SMaor Gottlieb 		} else {
1666038d2ef8SMaor Gottlieb 			err = PTR_ERR(ft);
1667038d2ef8SMaor Gottlieb 		}
1668038d2ef8SMaor Gottlieb 	}
1669038d2ef8SMaor Gottlieb 
1670038d2ef8SMaor Gottlieb 	return err ? ERR_PTR(err) : prio;
1671038d2ef8SMaor Gottlieb }
1672038d2ef8SMaor Gottlieb 
1673038d2ef8SMaor Gottlieb static struct mlx5_ib_flow_handler *create_flow_rule(struct mlx5_ib_dev *dev,
1674038d2ef8SMaor Gottlieb 						     struct mlx5_ib_flow_prio *ft_prio,
1675038d2ef8SMaor Gottlieb 						     struct ib_flow_attr *flow_attr,
1676038d2ef8SMaor Gottlieb 						     struct mlx5_flow_destination *dst)
1677038d2ef8SMaor Gottlieb {
1678038d2ef8SMaor Gottlieb 	struct mlx5_flow_table	*ft = ft_prio->flow_table;
1679038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler;
1680038d2ef8SMaor Gottlieb 	void *ib_flow = flow_attr + 1;
1681038d2ef8SMaor Gottlieb 	u8 match_criteria_enable = 0;
1682038d2ef8SMaor Gottlieb 	unsigned int spec_index;
1683038d2ef8SMaor Gottlieb 	u32 *match_c;
1684038d2ef8SMaor Gottlieb 	u32 *match_v;
168535d19011SMaor Gottlieb 	u32 action;
1686038d2ef8SMaor Gottlieb 	int err = 0;
1687038d2ef8SMaor Gottlieb 
1688038d2ef8SMaor Gottlieb 	if (!is_valid_attr(flow_attr))
1689038d2ef8SMaor Gottlieb 		return ERR_PTR(-EINVAL);
1690038d2ef8SMaor Gottlieb 
1691038d2ef8SMaor Gottlieb 	match_c = kzalloc(MLX5_ST_SZ_BYTES(fte_match_param), GFP_KERNEL);
1692038d2ef8SMaor Gottlieb 	match_v = kzalloc(MLX5_ST_SZ_BYTES(fte_match_param), GFP_KERNEL);
1693038d2ef8SMaor Gottlieb 	handler = kzalloc(sizeof(*handler), GFP_KERNEL);
1694038d2ef8SMaor Gottlieb 	if (!handler || !match_c || !match_v) {
1695038d2ef8SMaor Gottlieb 		err = -ENOMEM;
1696038d2ef8SMaor Gottlieb 		goto free;
1697038d2ef8SMaor Gottlieb 	}
1698038d2ef8SMaor Gottlieb 
1699038d2ef8SMaor Gottlieb 	INIT_LIST_HEAD(&handler->list);
1700038d2ef8SMaor Gottlieb 
1701038d2ef8SMaor Gottlieb 	for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
1702038d2ef8SMaor Gottlieb 		err = parse_flow_attr(match_c, match_v, ib_flow);
1703038d2ef8SMaor Gottlieb 		if (err < 0)
1704038d2ef8SMaor Gottlieb 			goto free;
1705038d2ef8SMaor Gottlieb 
1706038d2ef8SMaor Gottlieb 		ib_flow += ((union ib_flow_spec *)ib_flow)->size;
1707038d2ef8SMaor Gottlieb 	}
1708038d2ef8SMaor Gottlieb 
1709038d2ef8SMaor Gottlieb 	/* Outer header support only */
1710038d2ef8SMaor Gottlieb 	match_criteria_enable = (!outer_header_zero(match_c)) << 0;
171135d19011SMaor Gottlieb 	action = dst ? MLX5_FLOW_CONTEXT_ACTION_FWD_DEST :
171235d19011SMaor Gottlieb 		MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO;
1713038d2ef8SMaor Gottlieb 	handler->rule = mlx5_add_flow_rule(ft, match_criteria_enable,
1714038d2ef8SMaor Gottlieb 					   match_c, match_v,
171535d19011SMaor Gottlieb 					   action,
1716038d2ef8SMaor Gottlieb 					   MLX5_FS_DEFAULT_FLOW_TAG,
1717038d2ef8SMaor Gottlieb 					   dst);
1718038d2ef8SMaor Gottlieb 
1719038d2ef8SMaor Gottlieb 	if (IS_ERR(handler->rule)) {
1720038d2ef8SMaor Gottlieb 		err = PTR_ERR(handler->rule);
1721038d2ef8SMaor Gottlieb 		goto free;
1722038d2ef8SMaor Gottlieb 	}
1723038d2ef8SMaor Gottlieb 
1724038d2ef8SMaor Gottlieb 	handler->prio = ft_prio - dev->flow_db.prios;
1725038d2ef8SMaor Gottlieb 
1726038d2ef8SMaor Gottlieb 	ft_prio->flow_table = ft;
1727038d2ef8SMaor Gottlieb free:
1728038d2ef8SMaor Gottlieb 	if (err)
1729038d2ef8SMaor Gottlieb 		kfree(handler);
1730038d2ef8SMaor Gottlieb 	kfree(match_c);
1731038d2ef8SMaor Gottlieb 	kfree(match_v);
1732038d2ef8SMaor Gottlieb 	return err ? ERR_PTR(err) : handler;
1733038d2ef8SMaor Gottlieb }
1734038d2ef8SMaor Gottlieb 
173535d19011SMaor Gottlieb static struct mlx5_ib_flow_handler *create_dont_trap_rule(struct mlx5_ib_dev *dev,
173635d19011SMaor Gottlieb 							  struct mlx5_ib_flow_prio *ft_prio,
173735d19011SMaor Gottlieb 							  struct ib_flow_attr *flow_attr,
173835d19011SMaor Gottlieb 							  struct mlx5_flow_destination *dst)
173935d19011SMaor Gottlieb {
174035d19011SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_dst = NULL;
174135d19011SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
174235d19011SMaor Gottlieb 
174335d19011SMaor Gottlieb 	handler = create_flow_rule(dev, ft_prio, flow_attr, NULL);
174435d19011SMaor Gottlieb 	if (!IS_ERR(handler)) {
174535d19011SMaor Gottlieb 		handler_dst = create_flow_rule(dev, ft_prio,
174635d19011SMaor Gottlieb 					       flow_attr, dst);
174735d19011SMaor Gottlieb 		if (IS_ERR(handler_dst)) {
174835d19011SMaor Gottlieb 			mlx5_del_flow_rule(handler->rule);
174935d19011SMaor Gottlieb 			kfree(handler);
175035d19011SMaor Gottlieb 			handler = handler_dst;
175135d19011SMaor Gottlieb 		} else {
175235d19011SMaor Gottlieb 			list_add(&handler_dst->list, &handler->list);
175335d19011SMaor Gottlieb 		}
175435d19011SMaor Gottlieb 	}
175535d19011SMaor Gottlieb 
175635d19011SMaor Gottlieb 	return handler;
175735d19011SMaor Gottlieb }
1758038d2ef8SMaor Gottlieb enum {
1759038d2ef8SMaor Gottlieb 	LEFTOVERS_MC,
1760038d2ef8SMaor Gottlieb 	LEFTOVERS_UC,
1761038d2ef8SMaor Gottlieb };
1762038d2ef8SMaor Gottlieb 
1763038d2ef8SMaor Gottlieb static struct mlx5_ib_flow_handler *create_leftovers_rule(struct mlx5_ib_dev *dev,
1764038d2ef8SMaor Gottlieb 							  struct mlx5_ib_flow_prio *ft_prio,
1765038d2ef8SMaor Gottlieb 							  struct ib_flow_attr *flow_attr,
1766038d2ef8SMaor Gottlieb 							  struct mlx5_flow_destination *dst)
1767038d2ef8SMaor Gottlieb {
1768038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_ucast = NULL;
1769038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
1770038d2ef8SMaor Gottlieb 
1771038d2ef8SMaor Gottlieb 	static struct {
1772038d2ef8SMaor Gottlieb 		struct ib_flow_attr	flow_attr;
1773038d2ef8SMaor Gottlieb 		struct ib_flow_spec_eth eth_flow;
1774038d2ef8SMaor Gottlieb 	} leftovers_specs[] = {
1775038d2ef8SMaor Gottlieb 		[LEFTOVERS_MC] = {
1776038d2ef8SMaor Gottlieb 			.flow_attr = {
1777038d2ef8SMaor Gottlieb 				.num_of_specs = 1,
1778038d2ef8SMaor Gottlieb 				.size = sizeof(leftovers_specs[0])
1779038d2ef8SMaor Gottlieb 			},
1780038d2ef8SMaor Gottlieb 			.eth_flow = {
1781038d2ef8SMaor Gottlieb 				.type = IB_FLOW_SPEC_ETH,
1782038d2ef8SMaor Gottlieb 				.size = sizeof(struct ib_flow_spec_eth),
1783038d2ef8SMaor Gottlieb 				.mask = {.dst_mac = {0x1} },
1784038d2ef8SMaor Gottlieb 				.val =  {.dst_mac = {0x1} }
1785038d2ef8SMaor Gottlieb 			}
1786038d2ef8SMaor Gottlieb 		},
1787038d2ef8SMaor Gottlieb 		[LEFTOVERS_UC] = {
1788038d2ef8SMaor Gottlieb 			.flow_attr = {
1789038d2ef8SMaor Gottlieb 				.num_of_specs = 1,
1790038d2ef8SMaor Gottlieb 				.size = sizeof(leftovers_specs[0])
1791038d2ef8SMaor Gottlieb 			},
1792038d2ef8SMaor Gottlieb 			.eth_flow = {
1793038d2ef8SMaor Gottlieb 				.type = IB_FLOW_SPEC_ETH,
1794038d2ef8SMaor Gottlieb 				.size = sizeof(struct ib_flow_spec_eth),
1795038d2ef8SMaor Gottlieb 				.mask = {.dst_mac = {0x1} },
1796038d2ef8SMaor Gottlieb 				.val = {.dst_mac = {} }
1797038d2ef8SMaor Gottlieb 			}
1798038d2ef8SMaor Gottlieb 		}
1799038d2ef8SMaor Gottlieb 	};
1800038d2ef8SMaor Gottlieb 
1801038d2ef8SMaor Gottlieb 	handler = create_flow_rule(dev, ft_prio,
1802038d2ef8SMaor Gottlieb 				   &leftovers_specs[LEFTOVERS_MC].flow_attr,
1803038d2ef8SMaor Gottlieb 				   dst);
1804038d2ef8SMaor Gottlieb 	if (!IS_ERR(handler) &&
1805038d2ef8SMaor Gottlieb 	    flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT) {
1806038d2ef8SMaor Gottlieb 		handler_ucast = create_flow_rule(dev, ft_prio,
1807038d2ef8SMaor Gottlieb 						 &leftovers_specs[LEFTOVERS_UC].flow_attr,
1808038d2ef8SMaor Gottlieb 						 dst);
1809038d2ef8SMaor Gottlieb 		if (IS_ERR(handler_ucast)) {
1810038d2ef8SMaor Gottlieb 			kfree(handler);
1811038d2ef8SMaor Gottlieb 			handler = handler_ucast;
1812038d2ef8SMaor Gottlieb 		} else {
1813038d2ef8SMaor Gottlieb 			list_add(&handler_ucast->list, &handler->list);
1814038d2ef8SMaor Gottlieb 		}
1815038d2ef8SMaor Gottlieb 	}
1816038d2ef8SMaor Gottlieb 
1817038d2ef8SMaor Gottlieb 	return handler;
1818038d2ef8SMaor Gottlieb }
1819038d2ef8SMaor Gottlieb 
1820038d2ef8SMaor Gottlieb static struct ib_flow *mlx5_ib_create_flow(struct ib_qp *qp,
1821038d2ef8SMaor Gottlieb 					   struct ib_flow_attr *flow_attr,
1822038d2ef8SMaor Gottlieb 					   int domain)
1823038d2ef8SMaor Gottlieb {
1824038d2ef8SMaor Gottlieb 	struct mlx5_ib_dev *dev = to_mdev(qp->device);
1825038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
1826038d2ef8SMaor Gottlieb 	struct mlx5_flow_destination *dst = NULL;
1827038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_prio *ft_prio;
1828038d2ef8SMaor Gottlieb 	int err;
1829038d2ef8SMaor Gottlieb 
1830038d2ef8SMaor Gottlieb 	if (flow_attr->priority > MLX5_IB_FLOW_LAST_PRIO)
1831038d2ef8SMaor Gottlieb 		return ERR_PTR(-ENOSPC);
1832038d2ef8SMaor Gottlieb 
1833038d2ef8SMaor Gottlieb 	if (domain != IB_FLOW_DOMAIN_USER ||
1834038d2ef8SMaor Gottlieb 	    flow_attr->port > MLX5_CAP_GEN(dev->mdev, num_ports) ||
183535d19011SMaor Gottlieb 	    (flow_attr->flags & ~IB_FLOW_ATTR_FLAGS_DONT_TRAP))
1836038d2ef8SMaor Gottlieb 		return ERR_PTR(-EINVAL);
1837038d2ef8SMaor Gottlieb 
1838038d2ef8SMaor Gottlieb 	dst = kzalloc(sizeof(*dst), GFP_KERNEL);
1839038d2ef8SMaor Gottlieb 	if (!dst)
1840038d2ef8SMaor Gottlieb 		return ERR_PTR(-ENOMEM);
1841038d2ef8SMaor Gottlieb 
1842038d2ef8SMaor Gottlieb 	mutex_lock(&dev->flow_db.lock);
1843038d2ef8SMaor Gottlieb 
1844038d2ef8SMaor Gottlieb 	ft_prio = get_flow_table(dev, flow_attr);
1845038d2ef8SMaor Gottlieb 	if (IS_ERR(ft_prio)) {
1846038d2ef8SMaor Gottlieb 		err = PTR_ERR(ft_prio);
1847038d2ef8SMaor Gottlieb 		goto unlock;
1848038d2ef8SMaor Gottlieb 	}
1849038d2ef8SMaor Gottlieb 
1850038d2ef8SMaor Gottlieb 	dst->type = MLX5_FLOW_DESTINATION_TYPE_TIR;
1851038d2ef8SMaor Gottlieb 	dst->tir_num = to_mqp(qp)->raw_packet_qp.rq.tirn;
1852038d2ef8SMaor Gottlieb 
1853038d2ef8SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
185435d19011SMaor Gottlieb 		if (flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP)  {
185535d19011SMaor Gottlieb 			handler = create_dont_trap_rule(dev, ft_prio,
185635d19011SMaor Gottlieb 							flow_attr, dst);
185735d19011SMaor Gottlieb 		} else {
1858038d2ef8SMaor Gottlieb 			handler = create_flow_rule(dev, ft_prio, flow_attr,
1859038d2ef8SMaor Gottlieb 						   dst);
186035d19011SMaor Gottlieb 		}
1861038d2ef8SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
1862038d2ef8SMaor Gottlieb 		   flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
1863038d2ef8SMaor Gottlieb 		handler = create_leftovers_rule(dev, ft_prio, flow_attr,
1864038d2ef8SMaor Gottlieb 						dst);
1865038d2ef8SMaor Gottlieb 	} else {
1866038d2ef8SMaor Gottlieb 		err = -EINVAL;
1867038d2ef8SMaor Gottlieb 		goto destroy_ft;
1868038d2ef8SMaor Gottlieb 	}
1869038d2ef8SMaor Gottlieb 
1870038d2ef8SMaor Gottlieb 	if (IS_ERR(handler)) {
1871038d2ef8SMaor Gottlieb 		err = PTR_ERR(handler);
1872038d2ef8SMaor Gottlieb 		handler = NULL;
1873038d2ef8SMaor Gottlieb 		goto destroy_ft;
1874038d2ef8SMaor Gottlieb 	}
1875038d2ef8SMaor Gottlieb 
1876038d2ef8SMaor Gottlieb 	ft_prio->refcount++;
1877038d2ef8SMaor Gottlieb 	mutex_unlock(&dev->flow_db.lock);
1878038d2ef8SMaor Gottlieb 	kfree(dst);
1879038d2ef8SMaor Gottlieb 
1880038d2ef8SMaor Gottlieb 	return &handler->ibflow;
1881038d2ef8SMaor Gottlieb 
1882038d2ef8SMaor Gottlieb destroy_ft:
1883038d2ef8SMaor Gottlieb 	put_flow_table(dev, ft_prio, false);
1884038d2ef8SMaor Gottlieb unlock:
1885038d2ef8SMaor Gottlieb 	mutex_unlock(&dev->flow_db.lock);
1886038d2ef8SMaor Gottlieb 	kfree(dst);
1887038d2ef8SMaor Gottlieb 	kfree(handler);
1888038d2ef8SMaor Gottlieb 	return ERR_PTR(err);
1889038d2ef8SMaor Gottlieb }
1890038d2ef8SMaor Gottlieb 
1891e126ba97SEli Cohen static int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
1892e126ba97SEli Cohen {
1893e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
1894e126ba97SEli Cohen 	int err;
1895e126ba97SEli Cohen 
18969603b61dSJack Morgenstein 	err = mlx5_core_attach_mcg(dev->mdev, gid, ibqp->qp_num);
1897e126ba97SEli Cohen 	if (err)
1898e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n",
1899e126ba97SEli Cohen 			     ibqp->qp_num, gid->raw);
1900e126ba97SEli Cohen 
1901e126ba97SEli Cohen 	return err;
1902e126ba97SEli Cohen }
1903e126ba97SEli Cohen 
1904e126ba97SEli Cohen static int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
1905e126ba97SEli Cohen {
1906e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
1907e126ba97SEli Cohen 	int err;
1908e126ba97SEli Cohen 
19099603b61dSJack Morgenstein 	err = mlx5_core_detach_mcg(dev->mdev, gid, ibqp->qp_num);
1910e126ba97SEli Cohen 	if (err)
1911e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n",
1912e126ba97SEli Cohen 			     ibqp->qp_num, gid->raw);
1913e126ba97SEli Cohen 
1914e126ba97SEli Cohen 	return err;
1915e126ba97SEli Cohen }
1916e126ba97SEli Cohen 
1917e126ba97SEli Cohen static int init_node_data(struct mlx5_ib_dev *dev)
1918e126ba97SEli Cohen {
19191b5daf11SMajd Dibbiny 	int err;
1920e126ba97SEli Cohen 
19211b5daf11SMajd Dibbiny 	err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc);
1922e126ba97SEli Cohen 	if (err)
1923e126ba97SEli Cohen 		return err;
19241b5daf11SMajd Dibbiny 
19251b5daf11SMajd Dibbiny 	dev->mdev->rev_id = dev->mdev->pdev->revision;
19261b5daf11SMajd Dibbiny 
19271b5daf11SMajd Dibbiny 	return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid);
1928e126ba97SEli Cohen }
1929e126ba97SEli Cohen 
1930e126ba97SEli Cohen static ssize_t show_fw_pages(struct device *device, struct device_attribute *attr,
1931e126ba97SEli Cohen 			     char *buf)
1932e126ba97SEli Cohen {
1933e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
1934e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
1935e126ba97SEli Cohen 
19369603b61dSJack Morgenstein 	return sprintf(buf, "%d\n", dev->mdev->priv.fw_pages);
1937e126ba97SEli Cohen }
1938e126ba97SEli Cohen 
1939e126ba97SEli Cohen static ssize_t show_reg_pages(struct device *device,
1940e126ba97SEli Cohen 			      struct device_attribute *attr, char *buf)
1941e126ba97SEli Cohen {
1942e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
1943e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
1944e126ba97SEli Cohen 
19456aec21f6SHaggai Eran 	return sprintf(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages));
1946e126ba97SEli Cohen }
1947e126ba97SEli Cohen 
1948e126ba97SEli Cohen static ssize_t show_hca(struct device *device, struct device_attribute *attr,
1949e126ba97SEli Cohen 			char *buf)
1950e126ba97SEli Cohen {
1951e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
1952e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
19539603b61dSJack Morgenstein 	return sprintf(buf, "MT%d\n", dev->mdev->pdev->device);
1954e126ba97SEli Cohen }
1955e126ba97SEli Cohen 
1956e126ba97SEli Cohen static ssize_t show_fw_ver(struct device *device, struct device_attribute *attr,
1957e126ba97SEli Cohen 			   char *buf)
1958e126ba97SEli Cohen {
1959e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
1960e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
1961c0fcebf5SEran Ben Elisha 	return sprintf(buf, "%d.%d.%04d\n", fw_rev_maj(dev->mdev),
19629603b61dSJack Morgenstein 		       fw_rev_min(dev->mdev), fw_rev_sub(dev->mdev));
1963e126ba97SEli Cohen }
1964e126ba97SEli Cohen 
1965e126ba97SEli Cohen static ssize_t show_rev(struct device *device, struct device_attribute *attr,
1966e126ba97SEli Cohen 			char *buf)
1967e126ba97SEli Cohen {
1968e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
1969e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
19709603b61dSJack Morgenstein 	return sprintf(buf, "%x\n", dev->mdev->rev_id);
1971e126ba97SEli Cohen }
1972e126ba97SEli Cohen 
1973e126ba97SEli Cohen static ssize_t show_board(struct device *device, struct device_attribute *attr,
1974e126ba97SEli Cohen 			  char *buf)
1975e126ba97SEli Cohen {
1976e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
1977e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
1978e126ba97SEli Cohen 	return sprintf(buf, "%.*s\n", MLX5_BOARD_ID_LEN,
19799603b61dSJack Morgenstein 		       dev->mdev->board_id);
1980e126ba97SEli Cohen }
1981e126ba97SEli Cohen 
1982e126ba97SEli Cohen static DEVICE_ATTR(hw_rev,   S_IRUGO, show_rev,    NULL);
1983e126ba97SEli Cohen static DEVICE_ATTR(fw_ver,   S_IRUGO, show_fw_ver, NULL);
1984e126ba97SEli Cohen static DEVICE_ATTR(hca_type, S_IRUGO, show_hca,    NULL);
1985e126ba97SEli Cohen static DEVICE_ATTR(board_id, S_IRUGO, show_board,  NULL);
1986e126ba97SEli Cohen static DEVICE_ATTR(fw_pages, S_IRUGO, show_fw_pages, NULL);
1987e126ba97SEli Cohen static DEVICE_ATTR(reg_pages, S_IRUGO, show_reg_pages, NULL);
1988e126ba97SEli Cohen 
1989e126ba97SEli Cohen static struct device_attribute *mlx5_class_attributes[] = {
1990e126ba97SEli Cohen 	&dev_attr_hw_rev,
1991e126ba97SEli Cohen 	&dev_attr_fw_ver,
1992e126ba97SEli Cohen 	&dev_attr_hca_type,
1993e126ba97SEli Cohen 	&dev_attr_board_id,
1994e126ba97SEli Cohen 	&dev_attr_fw_pages,
1995e126ba97SEli Cohen 	&dev_attr_reg_pages,
1996e126ba97SEli Cohen };
1997e126ba97SEli Cohen 
19987722f47eSHaggai Eran static void pkey_change_handler(struct work_struct *work)
19997722f47eSHaggai Eran {
20007722f47eSHaggai Eran 	struct mlx5_ib_port_resources *ports =
20017722f47eSHaggai Eran 		container_of(work, struct mlx5_ib_port_resources,
20027722f47eSHaggai Eran 			     pkey_change_work);
20037722f47eSHaggai Eran 
20047722f47eSHaggai Eran 	mutex_lock(&ports->devr->mutex);
20057722f47eSHaggai Eran 	mlx5_ib_gsi_pkey_change(ports->gsi);
20067722f47eSHaggai Eran 	mutex_unlock(&ports->devr->mutex);
20077722f47eSHaggai Eran }
20087722f47eSHaggai Eran 
200989ea94a7SMaor Gottlieb static void mlx5_ib_handle_internal_error(struct mlx5_ib_dev *ibdev)
201089ea94a7SMaor Gottlieb {
201189ea94a7SMaor Gottlieb 	struct mlx5_ib_qp *mqp;
201289ea94a7SMaor Gottlieb 	struct mlx5_ib_cq *send_mcq, *recv_mcq;
201389ea94a7SMaor Gottlieb 	struct mlx5_core_cq *mcq;
201489ea94a7SMaor Gottlieb 	struct list_head cq_armed_list;
201589ea94a7SMaor Gottlieb 	unsigned long flags_qp;
201689ea94a7SMaor Gottlieb 	unsigned long flags_cq;
201789ea94a7SMaor Gottlieb 	unsigned long flags;
201889ea94a7SMaor Gottlieb 
201989ea94a7SMaor Gottlieb 	INIT_LIST_HEAD(&cq_armed_list);
202089ea94a7SMaor Gottlieb 
202189ea94a7SMaor Gottlieb 	/* Go over qp list reside on that ibdev, sync with create/destroy qp.*/
202289ea94a7SMaor Gottlieb 	spin_lock_irqsave(&ibdev->reset_flow_resource_lock, flags);
202389ea94a7SMaor Gottlieb 	list_for_each_entry(mqp, &ibdev->qp_list, qps_list) {
202489ea94a7SMaor Gottlieb 		spin_lock_irqsave(&mqp->sq.lock, flags_qp);
202589ea94a7SMaor Gottlieb 		if (mqp->sq.tail != mqp->sq.head) {
202689ea94a7SMaor Gottlieb 			send_mcq = to_mcq(mqp->ibqp.send_cq);
202789ea94a7SMaor Gottlieb 			spin_lock_irqsave(&send_mcq->lock, flags_cq);
202889ea94a7SMaor Gottlieb 			if (send_mcq->mcq.comp &&
202989ea94a7SMaor Gottlieb 			    mqp->ibqp.send_cq->comp_handler) {
203089ea94a7SMaor Gottlieb 				if (!send_mcq->mcq.reset_notify_added) {
203189ea94a7SMaor Gottlieb 					send_mcq->mcq.reset_notify_added = 1;
203289ea94a7SMaor Gottlieb 					list_add_tail(&send_mcq->mcq.reset_notify,
203389ea94a7SMaor Gottlieb 						      &cq_armed_list);
203489ea94a7SMaor Gottlieb 				}
203589ea94a7SMaor Gottlieb 			}
203689ea94a7SMaor Gottlieb 			spin_unlock_irqrestore(&send_mcq->lock, flags_cq);
203789ea94a7SMaor Gottlieb 		}
203889ea94a7SMaor Gottlieb 		spin_unlock_irqrestore(&mqp->sq.lock, flags_qp);
203989ea94a7SMaor Gottlieb 		spin_lock_irqsave(&mqp->rq.lock, flags_qp);
204089ea94a7SMaor Gottlieb 		/* no handling is needed for SRQ */
204189ea94a7SMaor Gottlieb 		if (!mqp->ibqp.srq) {
204289ea94a7SMaor Gottlieb 			if (mqp->rq.tail != mqp->rq.head) {
204389ea94a7SMaor Gottlieb 				recv_mcq = to_mcq(mqp->ibqp.recv_cq);
204489ea94a7SMaor Gottlieb 				spin_lock_irqsave(&recv_mcq->lock, flags_cq);
204589ea94a7SMaor Gottlieb 				if (recv_mcq->mcq.comp &&
204689ea94a7SMaor Gottlieb 				    mqp->ibqp.recv_cq->comp_handler) {
204789ea94a7SMaor Gottlieb 					if (!recv_mcq->mcq.reset_notify_added) {
204889ea94a7SMaor Gottlieb 						recv_mcq->mcq.reset_notify_added = 1;
204989ea94a7SMaor Gottlieb 						list_add_tail(&recv_mcq->mcq.reset_notify,
205089ea94a7SMaor Gottlieb 							      &cq_armed_list);
205189ea94a7SMaor Gottlieb 					}
205289ea94a7SMaor Gottlieb 				}
205389ea94a7SMaor Gottlieb 				spin_unlock_irqrestore(&recv_mcq->lock,
205489ea94a7SMaor Gottlieb 						       flags_cq);
205589ea94a7SMaor Gottlieb 			}
205689ea94a7SMaor Gottlieb 		}
205789ea94a7SMaor Gottlieb 		spin_unlock_irqrestore(&mqp->rq.lock, flags_qp);
205889ea94a7SMaor Gottlieb 	}
205989ea94a7SMaor Gottlieb 	/*At that point all inflight post send were put to be executed as of we
206089ea94a7SMaor Gottlieb 	 * lock/unlock above locks Now need to arm all involved CQs.
206189ea94a7SMaor Gottlieb 	 */
206289ea94a7SMaor Gottlieb 	list_for_each_entry(mcq, &cq_armed_list, reset_notify) {
206389ea94a7SMaor Gottlieb 		mcq->comp(mcq);
206489ea94a7SMaor Gottlieb 	}
206589ea94a7SMaor Gottlieb 	spin_unlock_irqrestore(&ibdev->reset_flow_resource_lock, flags);
206689ea94a7SMaor Gottlieb }
206789ea94a7SMaor Gottlieb 
20689603b61dSJack Morgenstein static void mlx5_ib_event(struct mlx5_core_dev *dev, void *context,
20694d2f9bbbSJack Morgenstein 			  enum mlx5_dev_event event, unsigned long param)
2070e126ba97SEli Cohen {
20719603b61dSJack Morgenstein 	struct mlx5_ib_dev *ibdev = (struct mlx5_ib_dev *)context;
2072e126ba97SEli Cohen 	struct ib_event ibev;
20739603b61dSJack Morgenstein 
2074e126ba97SEli Cohen 	u8 port = 0;
2075e126ba97SEli Cohen 
2076e126ba97SEli Cohen 	switch (event) {
2077e126ba97SEli Cohen 	case MLX5_DEV_EVENT_SYS_ERROR:
2078e126ba97SEli Cohen 		ibdev->ib_active = false;
2079e126ba97SEli Cohen 		ibev.event = IB_EVENT_DEVICE_FATAL;
208089ea94a7SMaor Gottlieb 		mlx5_ib_handle_internal_error(ibdev);
2081e126ba97SEli Cohen 		break;
2082e126ba97SEli Cohen 
2083e126ba97SEli Cohen 	case MLX5_DEV_EVENT_PORT_UP:
2084e126ba97SEli Cohen 		ibev.event = IB_EVENT_PORT_ACTIVE;
20854d2f9bbbSJack Morgenstein 		port = (u8)param;
2086e126ba97SEli Cohen 		break;
2087e126ba97SEli Cohen 
2088e126ba97SEli Cohen 	case MLX5_DEV_EVENT_PORT_DOWN:
20892788cf3bSNoa Osherovich 	case MLX5_DEV_EVENT_PORT_INITIALIZED:
2090e126ba97SEli Cohen 		ibev.event = IB_EVENT_PORT_ERR;
20914d2f9bbbSJack Morgenstein 		port = (u8)param;
2092e126ba97SEli Cohen 		break;
2093e126ba97SEli Cohen 
2094e126ba97SEli Cohen 	case MLX5_DEV_EVENT_LID_CHANGE:
2095e126ba97SEli Cohen 		ibev.event = IB_EVENT_LID_CHANGE;
20964d2f9bbbSJack Morgenstein 		port = (u8)param;
2097e126ba97SEli Cohen 		break;
2098e126ba97SEli Cohen 
2099e126ba97SEli Cohen 	case MLX5_DEV_EVENT_PKEY_CHANGE:
2100e126ba97SEli Cohen 		ibev.event = IB_EVENT_PKEY_CHANGE;
21014d2f9bbbSJack Morgenstein 		port = (u8)param;
21027722f47eSHaggai Eran 
21037722f47eSHaggai Eran 		schedule_work(&ibdev->devr.ports[port - 1].pkey_change_work);
2104e126ba97SEli Cohen 		break;
2105e126ba97SEli Cohen 
2106e126ba97SEli Cohen 	case MLX5_DEV_EVENT_GUID_CHANGE:
2107e126ba97SEli Cohen 		ibev.event = IB_EVENT_GID_CHANGE;
21084d2f9bbbSJack Morgenstein 		port = (u8)param;
2109e126ba97SEli Cohen 		break;
2110e126ba97SEli Cohen 
2111e126ba97SEli Cohen 	case MLX5_DEV_EVENT_CLIENT_REREG:
2112e126ba97SEli Cohen 		ibev.event = IB_EVENT_CLIENT_REREGISTER;
21134d2f9bbbSJack Morgenstein 		port = (u8)param;
2114e126ba97SEli Cohen 		break;
2115e126ba97SEli Cohen 	}
2116e126ba97SEli Cohen 
2117e126ba97SEli Cohen 	ibev.device	      = &ibdev->ib_dev;
2118e126ba97SEli Cohen 	ibev.element.port_num = port;
2119e126ba97SEli Cohen 
2120a0c84c32SEli Cohen 	if (port < 1 || port > ibdev->num_ports) {
2121a0c84c32SEli Cohen 		mlx5_ib_warn(ibdev, "warning: event on port %d\n", port);
2122a0c84c32SEli Cohen 		return;
2123a0c84c32SEli Cohen 	}
2124a0c84c32SEli Cohen 
2125e126ba97SEli Cohen 	if (ibdev->ib_active)
2126e126ba97SEli Cohen 		ib_dispatch_event(&ibev);
2127e126ba97SEli Cohen }
2128e126ba97SEli Cohen 
2129e126ba97SEli Cohen static void get_ext_port_caps(struct mlx5_ib_dev *dev)
2130e126ba97SEli Cohen {
2131e126ba97SEli Cohen 	int port;
2132e126ba97SEli Cohen 
2133938fe83cSSaeed Mahameed 	for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++)
2134e126ba97SEli Cohen 		mlx5_query_ext_port_caps(dev, port);
2135e126ba97SEli Cohen }
2136e126ba97SEli Cohen 
2137e126ba97SEli Cohen static int get_port_caps(struct mlx5_ib_dev *dev)
2138e126ba97SEli Cohen {
2139e126ba97SEli Cohen 	struct ib_device_attr *dprops = NULL;
2140e126ba97SEli Cohen 	struct ib_port_attr *pprops = NULL;
2141f614fc15SDan Carpenter 	int err = -ENOMEM;
2142e126ba97SEli Cohen 	int port;
21432528e33eSMatan Barak 	struct ib_udata uhw = {.inlen = 0, .outlen = 0};
2144e126ba97SEli Cohen 
2145e126ba97SEli Cohen 	pprops = kmalloc(sizeof(*pprops), GFP_KERNEL);
2146e126ba97SEli Cohen 	if (!pprops)
2147e126ba97SEli Cohen 		goto out;
2148e126ba97SEli Cohen 
2149e126ba97SEli Cohen 	dprops = kmalloc(sizeof(*dprops), GFP_KERNEL);
2150e126ba97SEli Cohen 	if (!dprops)
2151e126ba97SEli Cohen 		goto out;
2152e126ba97SEli Cohen 
21532528e33eSMatan Barak 	err = mlx5_ib_query_device(&dev->ib_dev, dprops, &uhw);
2154e126ba97SEli Cohen 	if (err) {
2155e126ba97SEli Cohen 		mlx5_ib_warn(dev, "query_device failed %d\n", err);
2156e126ba97SEli Cohen 		goto out;
2157e126ba97SEli Cohen 	}
2158e126ba97SEli Cohen 
2159938fe83cSSaeed Mahameed 	for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++) {
2160e126ba97SEli Cohen 		err = mlx5_ib_query_port(&dev->ib_dev, port, pprops);
2161e126ba97SEli Cohen 		if (err) {
2162938fe83cSSaeed Mahameed 			mlx5_ib_warn(dev, "query_port %d failed %d\n",
2163938fe83cSSaeed Mahameed 				     port, err);
2164e126ba97SEli Cohen 			break;
2165e126ba97SEli Cohen 		}
2166938fe83cSSaeed Mahameed 		dev->mdev->port_caps[port - 1].pkey_table_len =
2167938fe83cSSaeed Mahameed 						dprops->max_pkeys;
2168938fe83cSSaeed Mahameed 		dev->mdev->port_caps[port - 1].gid_table_len =
2169938fe83cSSaeed Mahameed 						pprops->gid_tbl_len;
2170e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "pkey_table_len %d, gid_table_len %d\n",
2171e126ba97SEli Cohen 			    dprops->max_pkeys, pprops->gid_tbl_len);
2172e126ba97SEli Cohen 	}
2173e126ba97SEli Cohen 
2174e126ba97SEli Cohen out:
2175e126ba97SEli Cohen 	kfree(pprops);
2176e126ba97SEli Cohen 	kfree(dprops);
2177e126ba97SEli Cohen 
2178e126ba97SEli Cohen 	return err;
2179e126ba97SEli Cohen }
2180e126ba97SEli Cohen 
2181e126ba97SEli Cohen static void destroy_umrc_res(struct mlx5_ib_dev *dev)
2182e126ba97SEli Cohen {
2183e126ba97SEli Cohen 	int err;
2184e126ba97SEli Cohen 
2185e126ba97SEli Cohen 	err = mlx5_mr_cache_cleanup(dev);
2186e126ba97SEli Cohen 	if (err)
2187e126ba97SEli Cohen 		mlx5_ib_warn(dev, "mr cache cleanup failed\n");
2188e126ba97SEli Cohen 
2189e126ba97SEli Cohen 	mlx5_ib_destroy_qp(dev->umrc.qp);
2190add08d76SChristoph Hellwig 	ib_free_cq(dev->umrc.cq);
2191e126ba97SEli Cohen 	ib_dealloc_pd(dev->umrc.pd);
2192e126ba97SEli Cohen }
2193e126ba97SEli Cohen 
2194e126ba97SEli Cohen enum {
2195e126ba97SEli Cohen 	MAX_UMR_WR = 128,
2196e126ba97SEli Cohen };
2197e126ba97SEli Cohen 
2198e126ba97SEli Cohen static int create_umr_res(struct mlx5_ib_dev *dev)
2199e126ba97SEli Cohen {
2200e126ba97SEli Cohen 	struct ib_qp_init_attr *init_attr = NULL;
2201e126ba97SEli Cohen 	struct ib_qp_attr *attr = NULL;
2202e126ba97SEli Cohen 	struct ib_pd *pd;
2203e126ba97SEli Cohen 	struct ib_cq *cq;
2204e126ba97SEli Cohen 	struct ib_qp *qp;
2205e126ba97SEli Cohen 	int ret;
2206e126ba97SEli Cohen 
2207e126ba97SEli Cohen 	attr = kzalloc(sizeof(*attr), GFP_KERNEL);
2208e126ba97SEli Cohen 	init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL);
2209e126ba97SEli Cohen 	if (!attr || !init_attr) {
2210e126ba97SEli Cohen 		ret = -ENOMEM;
2211e126ba97SEli Cohen 		goto error_0;
2212e126ba97SEli Cohen 	}
2213e126ba97SEli Cohen 
2214e126ba97SEli Cohen 	pd = ib_alloc_pd(&dev->ib_dev);
2215e126ba97SEli Cohen 	if (IS_ERR(pd)) {
2216e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create PD for sync UMR QP\n");
2217e126ba97SEli Cohen 		ret = PTR_ERR(pd);
2218e126ba97SEli Cohen 		goto error_0;
2219e126ba97SEli Cohen 	}
2220e126ba97SEli Cohen 
2221add08d76SChristoph Hellwig 	cq = ib_alloc_cq(&dev->ib_dev, NULL, 128, 0, IB_POLL_SOFTIRQ);
2222e126ba97SEli Cohen 	if (IS_ERR(cq)) {
2223e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create CQ for sync UMR QP\n");
2224e126ba97SEli Cohen 		ret = PTR_ERR(cq);
2225e126ba97SEli Cohen 		goto error_2;
2226e126ba97SEli Cohen 	}
2227e126ba97SEli Cohen 
2228e126ba97SEli Cohen 	init_attr->send_cq = cq;
2229e126ba97SEli Cohen 	init_attr->recv_cq = cq;
2230e126ba97SEli Cohen 	init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
2231e126ba97SEli Cohen 	init_attr->cap.max_send_wr = MAX_UMR_WR;
2232e126ba97SEli Cohen 	init_attr->cap.max_send_sge = 1;
2233e126ba97SEli Cohen 	init_attr->qp_type = MLX5_IB_QPT_REG_UMR;
2234e126ba97SEli Cohen 	init_attr->port_num = 1;
2235e126ba97SEli Cohen 	qp = mlx5_ib_create_qp(pd, init_attr, NULL);
2236e126ba97SEli Cohen 	if (IS_ERR(qp)) {
2237e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create sync UMR QP\n");
2238e126ba97SEli Cohen 		ret = PTR_ERR(qp);
2239e126ba97SEli Cohen 		goto error_3;
2240e126ba97SEli Cohen 	}
2241e126ba97SEli Cohen 	qp->device     = &dev->ib_dev;
2242e126ba97SEli Cohen 	qp->real_qp    = qp;
2243e126ba97SEli Cohen 	qp->uobject    = NULL;
2244e126ba97SEli Cohen 	qp->qp_type    = MLX5_IB_QPT_REG_UMR;
2245e126ba97SEli Cohen 
2246e126ba97SEli Cohen 	attr->qp_state = IB_QPS_INIT;
2247e126ba97SEli Cohen 	attr->port_num = 1;
2248e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_PKEY_INDEX |
2249e126ba97SEli Cohen 				IB_QP_PORT, NULL);
2250e126ba97SEli Cohen 	if (ret) {
2251e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify UMR QP\n");
2252e126ba97SEli Cohen 		goto error_4;
2253e126ba97SEli Cohen 	}
2254e126ba97SEli Cohen 
2255e126ba97SEli Cohen 	memset(attr, 0, sizeof(*attr));
2256e126ba97SEli Cohen 	attr->qp_state = IB_QPS_RTR;
2257e126ba97SEli Cohen 	attr->path_mtu = IB_MTU_256;
2258e126ba97SEli Cohen 
2259e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
2260e126ba97SEli Cohen 	if (ret) {
2261e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify umr QP to rtr\n");
2262e126ba97SEli Cohen 		goto error_4;
2263e126ba97SEli Cohen 	}
2264e126ba97SEli Cohen 
2265e126ba97SEli Cohen 	memset(attr, 0, sizeof(*attr));
2266e126ba97SEli Cohen 	attr->qp_state = IB_QPS_RTS;
2267e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
2268e126ba97SEli Cohen 	if (ret) {
2269e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify umr QP to rts\n");
2270e126ba97SEli Cohen 		goto error_4;
2271e126ba97SEli Cohen 	}
2272e126ba97SEli Cohen 
2273e126ba97SEli Cohen 	dev->umrc.qp = qp;
2274e126ba97SEli Cohen 	dev->umrc.cq = cq;
2275e126ba97SEli Cohen 	dev->umrc.pd = pd;
2276e126ba97SEli Cohen 
2277e126ba97SEli Cohen 	sema_init(&dev->umrc.sem, MAX_UMR_WR);
2278e126ba97SEli Cohen 	ret = mlx5_mr_cache_init(dev);
2279e126ba97SEli Cohen 	if (ret) {
2280e126ba97SEli Cohen 		mlx5_ib_warn(dev, "mr cache init failed %d\n", ret);
2281e126ba97SEli Cohen 		goto error_4;
2282e126ba97SEli Cohen 	}
2283e126ba97SEli Cohen 
2284e126ba97SEli Cohen 	kfree(attr);
2285e126ba97SEli Cohen 	kfree(init_attr);
2286e126ba97SEli Cohen 
2287e126ba97SEli Cohen 	return 0;
2288e126ba97SEli Cohen 
2289e126ba97SEli Cohen error_4:
2290e126ba97SEli Cohen 	mlx5_ib_destroy_qp(qp);
2291e126ba97SEli Cohen 
2292e126ba97SEli Cohen error_3:
2293add08d76SChristoph Hellwig 	ib_free_cq(cq);
2294e126ba97SEli Cohen 
2295e126ba97SEli Cohen error_2:
2296e126ba97SEli Cohen 	ib_dealloc_pd(pd);
2297e126ba97SEli Cohen 
2298e126ba97SEli Cohen error_0:
2299e126ba97SEli Cohen 	kfree(attr);
2300e126ba97SEli Cohen 	kfree(init_attr);
2301e126ba97SEli Cohen 	return ret;
2302e126ba97SEli Cohen }
2303e126ba97SEli Cohen 
2304e126ba97SEli Cohen static int create_dev_resources(struct mlx5_ib_resources *devr)
2305e126ba97SEli Cohen {
2306e126ba97SEli Cohen 	struct ib_srq_init_attr attr;
2307e126ba97SEli Cohen 	struct mlx5_ib_dev *dev;
2308bcf4c1eaSMatan Barak 	struct ib_cq_init_attr cq_attr = {.cqe = 1};
23097722f47eSHaggai Eran 	int port;
2310e126ba97SEli Cohen 	int ret = 0;
2311e126ba97SEli Cohen 
2312e126ba97SEli Cohen 	dev = container_of(devr, struct mlx5_ib_dev, devr);
2313e126ba97SEli Cohen 
2314d16e91daSHaggai Eran 	mutex_init(&devr->mutex);
2315d16e91daSHaggai Eran 
2316e126ba97SEli Cohen 	devr->p0 = mlx5_ib_alloc_pd(&dev->ib_dev, NULL, NULL);
2317e126ba97SEli Cohen 	if (IS_ERR(devr->p0)) {
2318e126ba97SEli Cohen 		ret = PTR_ERR(devr->p0);
2319e126ba97SEli Cohen 		goto error0;
2320e126ba97SEli Cohen 	}
2321e126ba97SEli Cohen 	devr->p0->device  = &dev->ib_dev;
2322e126ba97SEli Cohen 	devr->p0->uobject = NULL;
2323e126ba97SEli Cohen 	atomic_set(&devr->p0->usecnt, 0);
2324e126ba97SEli Cohen 
2325bcf4c1eaSMatan Barak 	devr->c0 = mlx5_ib_create_cq(&dev->ib_dev, &cq_attr, NULL, NULL);
2326e126ba97SEli Cohen 	if (IS_ERR(devr->c0)) {
2327e126ba97SEli Cohen 		ret = PTR_ERR(devr->c0);
2328e126ba97SEli Cohen 		goto error1;
2329e126ba97SEli Cohen 	}
2330e126ba97SEli Cohen 	devr->c0->device        = &dev->ib_dev;
2331e126ba97SEli Cohen 	devr->c0->uobject       = NULL;
2332e126ba97SEli Cohen 	devr->c0->comp_handler  = NULL;
2333e126ba97SEli Cohen 	devr->c0->event_handler = NULL;
2334e126ba97SEli Cohen 	devr->c0->cq_context    = NULL;
2335e126ba97SEli Cohen 	atomic_set(&devr->c0->usecnt, 0);
2336e126ba97SEli Cohen 
2337e126ba97SEli Cohen 	devr->x0 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
2338e126ba97SEli Cohen 	if (IS_ERR(devr->x0)) {
2339e126ba97SEli Cohen 		ret = PTR_ERR(devr->x0);
2340e126ba97SEli Cohen 		goto error2;
2341e126ba97SEli Cohen 	}
2342e126ba97SEli Cohen 	devr->x0->device = &dev->ib_dev;
2343e126ba97SEli Cohen 	devr->x0->inode = NULL;
2344e126ba97SEli Cohen 	atomic_set(&devr->x0->usecnt, 0);
2345e126ba97SEli Cohen 	mutex_init(&devr->x0->tgt_qp_mutex);
2346e126ba97SEli Cohen 	INIT_LIST_HEAD(&devr->x0->tgt_qp_list);
2347e126ba97SEli Cohen 
2348e126ba97SEli Cohen 	devr->x1 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
2349e126ba97SEli Cohen 	if (IS_ERR(devr->x1)) {
2350e126ba97SEli Cohen 		ret = PTR_ERR(devr->x1);
2351e126ba97SEli Cohen 		goto error3;
2352e126ba97SEli Cohen 	}
2353e126ba97SEli Cohen 	devr->x1->device = &dev->ib_dev;
2354e126ba97SEli Cohen 	devr->x1->inode = NULL;
2355e126ba97SEli Cohen 	atomic_set(&devr->x1->usecnt, 0);
2356e126ba97SEli Cohen 	mutex_init(&devr->x1->tgt_qp_mutex);
2357e126ba97SEli Cohen 	INIT_LIST_HEAD(&devr->x1->tgt_qp_list);
2358e126ba97SEli Cohen 
2359e126ba97SEli Cohen 	memset(&attr, 0, sizeof(attr));
2360e126ba97SEli Cohen 	attr.attr.max_sge = 1;
2361e126ba97SEli Cohen 	attr.attr.max_wr = 1;
2362e126ba97SEli Cohen 	attr.srq_type = IB_SRQT_XRC;
2363e126ba97SEli Cohen 	attr.ext.xrc.cq = devr->c0;
2364e126ba97SEli Cohen 	attr.ext.xrc.xrcd = devr->x0;
2365e126ba97SEli Cohen 
2366e126ba97SEli Cohen 	devr->s0 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
2367e126ba97SEli Cohen 	if (IS_ERR(devr->s0)) {
2368e126ba97SEli Cohen 		ret = PTR_ERR(devr->s0);
2369e126ba97SEli Cohen 		goto error4;
2370e126ba97SEli Cohen 	}
2371e126ba97SEli Cohen 	devr->s0->device	= &dev->ib_dev;
2372e126ba97SEli Cohen 	devr->s0->pd		= devr->p0;
2373e126ba97SEli Cohen 	devr->s0->uobject       = NULL;
2374e126ba97SEli Cohen 	devr->s0->event_handler = NULL;
2375e126ba97SEli Cohen 	devr->s0->srq_context   = NULL;
2376e126ba97SEli Cohen 	devr->s0->srq_type      = IB_SRQT_XRC;
2377e126ba97SEli Cohen 	devr->s0->ext.xrc.xrcd	= devr->x0;
2378e126ba97SEli Cohen 	devr->s0->ext.xrc.cq	= devr->c0;
2379e126ba97SEli Cohen 	atomic_inc(&devr->s0->ext.xrc.xrcd->usecnt);
2380e126ba97SEli Cohen 	atomic_inc(&devr->s0->ext.xrc.cq->usecnt);
2381e126ba97SEli Cohen 	atomic_inc(&devr->p0->usecnt);
2382e126ba97SEli Cohen 	atomic_set(&devr->s0->usecnt, 0);
2383e126ba97SEli Cohen 
23844aa17b28SHaggai Abramonvsky 	memset(&attr, 0, sizeof(attr));
23854aa17b28SHaggai Abramonvsky 	attr.attr.max_sge = 1;
23864aa17b28SHaggai Abramonvsky 	attr.attr.max_wr = 1;
23874aa17b28SHaggai Abramonvsky 	attr.srq_type = IB_SRQT_BASIC;
23884aa17b28SHaggai Abramonvsky 	devr->s1 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
23894aa17b28SHaggai Abramonvsky 	if (IS_ERR(devr->s1)) {
23904aa17b28SHaggai Abramonvsky 		ret = PTR_ERR(devr->s1);
23914aa17b28SHaggai Abramonvsky 		goto error5;
23924aa17b28SHaggai Abramonvsky 	}
23934aa17b28SHaggai Abramonvsky 	devr->s1->device	= &dev->ib_dev;
23944aa17b28SHaggai Abramonvsky 	devr->s1->pd		= devr->p0;
23954aa17b28SHaggai Abramonvsky 	devr->s1->uobject       = NULL;
23964aa17b28SHaggai Abramonvsky 	devr->s1->event_handler = NULL;
23974aa17b28SHaggai Abramonvsky 	devr->s1->srq_context   = NULL;
23984aa17b28SHaggai Abramonvsky 	devr->s1->srq_type      = IB_SRQT_BASIC;
23994aa17b28SHaggai Abramonvsky 	devr->s1->ext.xrc.cq	= devr->c0;
24004aa17b28SHaggai Abramonvsky 	atomic_inc(&devr->p0->usecnt);
24014aa17b28SHaggai Abramonvsky 	atomic_set(&devr->s0->usecnt, 0);
24024aa17b28SHaggai Abramonvsky 
24037722f47eSHaggai Eran 	for (port = 0; port < ARRAY_SIZE(devr->ports); ++port) {
24047722f47eSHaggai Eran 		INIT_WORK(&devr->ports[port].pkey_change_work,
24057722f47eSHaggai Eran 			  pkey_change_handler);
24067722f47eSHaggai Eran 		devr->ports[port].devr = devr;
24077722f47eSHaggai Eran 	}
24087722f47eSHaggai Eran 
2409e126ba97SEli Cohen 	return 0;
2410e126ba97SEli Cohen 
24114aa17b28SHaggai Abramonvsky error5:
24124aa17b28SHaggai Abramonvsky 	mlx5_ib_destroy_srq(devr->s0);
2413e126ba97SEli Cohen error4:
2414e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x1);
2415e126ba97SEli Cohen error3:
2416e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x0);
2417e126ba97SEli Cohen error2:
2418e126ba97SEli Cohen 	mlx5_ib_destroy_cq(devr->c0);
2419e126ba97SEli Cohen error1:
2420e126ba97SEli Cohen 	mlx5_ib_dealloc_pd(devr->p0);
2421e126ba97SEli Cohen error0:
2422e126ba97SEli Cohen 	return ret;
2423e126ba97SEli Cohen }
2424e126ba97SEli Cohen 
2425e126ba97SEli Cohen static void destroy_dev_resources(struct mlx5_ib_resources *devr)
2426e126ba97SEli Cohen {
24277722f47eSHaggai Eran 	struct mlx5_ib_dev *dev =
24287722f47eSHaggai Eran 		container_of(devr, struct mlx5_ib_dev, devr);
24297722f47eSHaggai Eran 	int port;
24307722f47eSHaggai Eran 
24314aa17b28SHaggai Abramonvsky 	mlx5_ib_destroy_srq(devr->s1);
2432e126ba97SEli Cohen 	mlx5_ib_destroy_srq(devr->s0);
2433e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x0);
2434e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x1);
2435e126ba97SEli Cohen 	mlx5_ib_destroy_cq(devr->c0);
2436e126ba97SEli Cohen 	mlx5_ib_dealloc_pd(devr->p0);
24377722f47eSHaggai Eran 
24387722f47eSHaggai Eran 	/* Make sure no change P_Key work items are still executing */
24397722f47eSHaggai Eran 	for (port = 0; port < dev->num_ports; ++port)
24407722f47eSHaggai Eran 		cancel_work_sync(&devr->ports[port].pkey_change_work);
2441e126ba97SEli Cohen }
2442e126ba97SEli Cohen 
2443e53505a8SAchiad Shochat static u32 get_core_cap_flags(struct ib_device *ibdev)
2444e53505a8SAchiad Shochat {
2445e53505a8SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
2446e53505a8SAchiad Shochat 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, 1);
2447e53505a8SAchiad Shochat 	u8 l3_type_cap = MLX5_CAP_ROCE(dev->mdev, l3_type);
2448e53505a8SAchiad Shochat 	u8 roce_version_cap = MLX5_CAP_ROCE(dev->mdev, roce_version);
2449e53505a8SAchiad Shochat 	u32 ret = 0;
2450e53505a8SAchiad Shochat 
2451e53505a8SAchiad Shochat 	if (ll == IB_LINK_LAYER_INFINIBAND)
2452e53505a8SAchiad Shochat 		return RDMA_CORE_PORT_IBA_IB;
2453e53505a8SAchiad Shochat 
2454e53505a8SAchiad Shochat 	if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV4_CAP))
2455e53505a8SAchiad Shochat 		return 0;
2456e53505a8SAchiad Shochat 
2457e53505a8SAchiad Shochat 	if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV6_CAP))
2458e53505a8SAchiad Shochat 		return 0;
2459e53505a8SAchiad Shochat 
2460e53505a8SAchiad Shochat 	if (roce_version_cap & MLX5_ROCE_VERSION_1_CAP)
2461e53505a8SAchiad Shochat 		ret |= RDMA_CORE_PORT_IBA_ROCE;
2462e53505a8SAchiad Shochat 
2463e53505a8SAchiad Shochat 	if (roce_version_cap & MLX5_ROCE_VERSION_2_CAP)
2464e53505a8SAchiad Shochat 		ret |= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
2465e53505a8SAchiad Shochat 
2466e53505a8SAchiad Shochat 	return ret;
2467e53505a8SAchiad Shochat }
2468e53505a8SAchiad Shochat 
24697738613eSIra Weiny static int mlx5_port_immutable(struct ib_device *ibdev, u8 port_num,
24707738613eSIra Weiny 			       struct ib_port_immutable *immutable)
24717738613eSIra Weiny {
24727738613eSIra Weiny 	struct ib_port_attr attr;
24737738613eSIra Weiny 	int err;
24747738613eSIra Weiny 
24757738613eSIra Weiny 	err = mlx5_ib_query_port(ibdev, port_num, &attr);
24767738613eSIra Weiny 	if (err)
24777738613eSIra Weiny 		return err;
24787738613eSIra Weiny 
24797738613eSIra Weiny 	immutable->pkey_tbl_len = attr.pkey_tbl_len;
24807738613eSIra Weiny 	immutable->gid_tbl_len = attr.gid_tbl_len;
2481e53505a8SAchiad Shochat 	immutable->core_cap_flags = get_core_cap_flags(ibdev);
2482337877a4SIra Weiny 	immutable->max_mad_size = IB_MGMT_MAD_SIZE;
24837738613eSIra Weiny 
24847738613eSIra Weiny 	return 0;
24857738613eSIra Weiny }
24867738613eSIra Weiny 
2487fc24fc5eSAchiad Shochat static int mlx5_enable_roce(struct mlx5_ib_dev *dev)
2488fc24fc5eSAchiad Shochat {
2489e53505a8SAchiad Shochat 	int err;
2490e53505a8SAchiad Shochat 
2491fc24fc5eSAchiad Shochat 	dev->roce.nb.notifier_call = mlx5_netdev_event;
2492e53505a8SAchiad Shochat 	err = register_netdevice_notifier(&dev->roce.nb);
2493e53505a8SAchiad Shochat 	if (err)
2494e53505a8SAchiad Shochat 		return err;
2495e53505a8SAchiad Shochat 
2496e53505a8SAchiad Shochat 	err = mlx5_nic_vport_enable_roce(dev->mdev);
2497e53505a8SAchiad Shochat 	if (err)
2498e53505a8SAchiad Shochat 		goto err_unregister_netdevice_notifier;
2499e53505a8SAchiad Shochat 
2500e53505a8SAchiad Shochat 	return 0;
2501e53505a8SAchiad Shochat 
2502e53505a8SAchiad Shochat err_unregister_netdevice_notifier:
2503e53505a8SAchiad Shochat 	unregister_netdevice_notifier(&dev->roce.nb);
2504e53505a8SAchiad Shochat 	return err;
2505fc24fc5eSAchiad Shochat }
2506fc24fc5eSAchiad Shochat 
2507fc24fc5eSAchiad Shochat static void mlx5_disable_roce(struct mlx5_ib_dev *dev)
2508fc24fc5eSAchiad Shochat {
2509e53505a8SAchiad Shochat 	mlx5_nic_vport_disable_roce(dev->mdev);
2510fc24fc5eSAchiad Shochat 	unregister_netdevice_notifier(&dev->roce.nb);
2511fc24fc5eSAchiad Shochat }
2512fc24fc5eSAchiad Shochat 
25139603b61dSJack Morgenstein static void *mlx5_ib_add(struct mlx5_core_dev *mdev)
2514e126ba97SEli Cohen {
2515e126ba97SEli Cohen 	struct mlx5_ib_dev *dev;
2516ebd61f68SAchiad Shochat 	enum rdma_link_layer ll;
2517ebd61f68SAchiad Shochat 	int port_type_cap;
2518e126ba97SEli Cohen 	int err;
2519e126ba97SEli Cohen 	int i;
2520e126ba97SEli Cohen 
2521ebd61f68SAchiad Shochat 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
2522ebd61f68SAchiad Shochat 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
2523ebd61f68SAchiad Shochat 
2524e53505a8SAchiad Shochat 	if ((ll == IB_LINK_LAYER_ETHERNET) && !MLX5_CAP_GEN(mdev, roce))
2525647241eaSMajd Dibbiny 		return NULL;
2526647241eaSMajd Dibbiny 
2527e126ba97SEli Cohen 	printk_once(KERN_INFO "%s", mlx5_version);
2528e126ba97SEli Cohen 
2529e126ba97SEli Cohen 	dev = (struct mlx5_ib_dev *)ib_alloc_device(sizeof(*dev));
2530e126ba97SEli Cohen 	if (!dev)
25319603b61dSJack Morgenstein 		return NULL;
2532e126ba97SEli Cohen 
25339603b61dSJack Morgenstein 	dev->mdev = mdev;
2534e126ba97SEli Cohen 
2535fc24fc5eSAchiad Shochat 	rwlock_init(&dev->roce.netdev_lock);
2536e126ba97SEli Cohen 	err = get_port_caps(dev);
2537e126ba97SEli Cohen 	if (err)
25389603b61dSJack Morgenstein 		goto err_dealloc;
2539e126ba97SEli Cohen 
25401b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(dev))
2541e126ba97SEli Cohen 		get_ext_port_caps(dev);
2542e126ba97SEli Cohen 
2543e126ba97SEli Cohen 	MLX5_INIT_DOORBELL_LOCK(&dev->uar_lock);
2544e126ba97SEli Cohen 
2545e126ba97SEli Cohen 	strlcpy(dev->ib_dev.name, "mlx5_%d", IB_DEVICE_NAME_MAX);
2546e126ba97SEli Cohen 	dev->ib_dev.owner		= THIS_MODULE;
2547e126ba97SEli Cohen 	dev->ib_dev.node_type		= RDMA_NODE_IB_CA;
2548c6790aa9SSagi Grimberg 	dev->ib_dev.local_dma_lkey	= 0 /* not supported for now */;
2549938fe83cSSaeed Mahameed 	dev->num_ports		= MLX5_CAP_GEN(mdev, num_ports);
2550e126ba97SEli Cohen 	dev->ib_dev.phys_port_cnt     = dev->num_ports;
2551233d05d2SSaeed Mahameed 	dev->ib_dev.num_comp_vectors    =
2552233d05d2SSaeed Mahameed 		dev->mdev->priv.eq_table.num_comp_vectors;
2553e126ba97SEli Cohen 	dev->ib_dev.dma_device	= &mdev->pdev->dev;
2554e126ba97SEli Cohen 
2555e126ba97SEli Cohen 	dev->ib_dev.uverbs_abi_ver	= MLX5_IB_UVERBS_ABI_VERSION;
2556e126ba97SEli Cohen 	dev->ib_dev.uverbs_cmd_mask	=
2557e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_GET_CONTEXT)		|
2558e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_DEVICE)	|
2559e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_PORT)		|
2560e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_ALLOC_PD)		|
2561e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DEALLOC_PD)		|
2562e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_REG_MR)		|
256356e11d62SNoa Osherovich 		(1ull << IB_USER_VERBS_CMD_REREG_MR)		|
2564e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DEREG_MR)		|
2565e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL)	|
2566e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_CQ)		|
2567e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_RESIZE_CQ)		|
2568e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_CQ)		|
2569e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_QP)		|
2570e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_MODIFY_QP)		|
2571e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_QP)		|
2572e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_QP)		|
2573e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_ATTACH_MCAST)	|
2574e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DETACH_MCAST)	|
2575e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_SRQ)		|
2576e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_MODIFY_SRQ)		|
2577e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_SRQ)		|
2578e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_SRQ)		|
2579e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_XSRQ)		|
2580e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_OPEN_QP);
25811707cb4aSHaggai Eran 	dev->ib_dev.uverbs_ex_cmd_mask =
2582d4584ddfSMatan Barak 		(1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE)	|
2583d4584ddfSMatan Barak 		(1ull << IB_USER_VERBS_EX_CMD_CREATE_CQ)	|
2584d4584ddfSMatan Barak 		(1ull << IB_USER_VERBS_EX_CMD_CREATE_QP);
2585e126ba97SEli Cohen 
2586e126ba97SEli Cohen 	dev->ib_dev.query_device	= mlx5_ib_query_device;
2587e126ba97SEli Cohen 	dev->ib_dev.query_port		= mlx5_ib_query_port;
2588ebd61f68SAchiad Shochat 	dev->ib_dev.get_link_layer	= mlx5_ib_port_link_layer;
2589fc24fc5eSAchiad Shochat 	if (ll == IB_LINK_LAYER_ETHERNET)
2590fc24fc5eSAchiad Shochat 		dev->ib_dev.get_netdev	= mlx5_ib_get_netdev;
2591e126ba97SEli Cohen 	dev->ib_dev.query_gid		= mlx5_ib_query_gid;
25923cca2606SAchiad Shochat 	dev->ib_dev.add_gid		= mlx5_ib_add_gid;
25933cca2606SAchiad Shochat 	dev->ib_dev.del_gid		= mlx5_ib_del_gid;
2594e126ba97SEli Cohen 	dev->ib_dev.query_pkey		= mlx5_ib_query_pkey;
2595e126ba97SEli Cohen 	dev->ib_dev.modify_device	= mlx5_ib_modify_device;
2596e126ba97SEli Cohen 	dev->ib_dev.modify_port		= mlx5_ib_modify_port;
2597e126ba97SEli Cohen 	dev->ib_dev.alloc_ucontext	= mlx5_ib_alloc_ucontext;
2598e126ba97SEli Cohen 	dev->ib_dev.dealloc_ucontext	= mlx5_ib_dealloc_ucontext;
2599e126ba97SEli Cohen 	dev->ib_dev.mmap		= mlx5_ib_mmap;
2600e126ba97SEli Cohen 	dev->ib_dev.alloc_pd		= mlx5_ib_alloc_pd;
2601e126ba97SEli Cohen 	dev->ib_dev.dealloc_pd		= mlx5_ib_dealloc_pd;
2602e126ba97SEli Cohen 	dev->ib_dev.create_ah		= mlx5_ib_create_ah;
2603e126ba97SEli Cohen 	dev->ib_dev.query_ah		= mlx5_ib_query_ah;
2604e126ba97SEli Cohen 	dev->ib_dev.destroy_ah		= mlx5_ib_destroy_ah;
2605e126ba97SEli Cohen 	dev->ib_dev.create_srq		= mlx5_ib_create_srq;
2606e126ba97SEli Cohen 	dev->ib_dev.modify_srq		= mlx5_ib_modify_srq;
2607e126ba97SEli Cohen 	dev->ib_dev.query_srq		= mlx5_ib_query_srq;
2608e126ba97SEli Cohen 	dev->ib_dev.destroy_srq		= mlx5_ib_destroy_srq;
2609e126ba97SEli Cohen 	dev->ib_dev.post_srq_recv	= mlx5_ib_post_srq_recv;
2610e126ba97SEli Cohen 	dev->ib_dev.create_qp		= mlx5_ib_create_qp;
2611e126ba97SEli Cohen 	dev->ib_dev.modify_qp		= mlx5_ib_modify_qp;
2612e126ba97SEli Cohen 	dev->ib_dev.query_qp		= mlx5_ib_query_qp;
2613e126ba97SEli Cohen 	dev->ib_dev.destroy_qp		= mlx5_ib_destroy_qp;
2614e126ba97SEli Cohen 	dev->ib_dev.post_send		= mlx5_ib_post_send;
2615e126ba97SEli Cohen 	dev->ib_dev.post_recv		= mlx5_ib_post_recv;
2616e126ba97SEli Cohen 	dev->ib_dev.create_cq		= mlx5_ib_create_cq;
2617e126ba97SEli Cohen 	dev->ib_dev.modify_cq		= mlx5_ib_modify_cq;
2618e126ba97SEli Cohen 	dev->ib_dev.resize_cq		= mlx5_ib_resize_cq;
2619e126ba97SEli Cohen 	dev->ib_dev.destroy_cq		= mlx5_ib_destroy_cq;
2620e126ba97SEli Cohen 	dev->ib_dev.poll_cq		= mlx5_ib_poll_cq;
2621e126ba97SEli Cohen 	dev->ib_dev.req_notify_cq	= mlx5_ib_arm_cq;
2622e126ba97SEli Cohen 	dev->ib_dev.get_dma_mr		= mlx5_ib_get_dma_mr;
2623e126ba97SEli Cohen 	dev->ib_dev.reg_user_mr		= mlx5_ib_reg_user_mr;
262456e11d62SNoa Osherovich 	dev->ib_dev.rereg_user_mr	= mlx5_ib_rereg_user_mr;
2625e126ba97SEli Cohen 	dev->ib_dev.dereg_mr		= mlx5_ib_dereg_mr;
2626e126ba97SEli Cohen 	dev->ib_dev.attach_mcast	= mlx5_ib_mcg_attach;
2627e126ba97SEli Cohen 	dev->ib_dev.detach_mcast	= mlx5_ib_mcg_detach;
2628e126ba97SEli Cohen 	dev->ib_dev.process_mad		= mlx5_ib_process_mad;
26299bee178bSSagi Grimberg 	dev->ib_dev.alloc_mr		= mlx5_ib_alloc_mr;
26308a187ee5SSagi Grimberg 	dev->ib_dev.map_mr_sg		= mlx5_ib_map_mr_sg;
2631d5436ba0SSagi Grimberg 	dev->ib_dev.check_mr_status	= mlx5_ib_check_mr_status;
26327738613eSIra Weiny 	dev->ib_dev.get_port_immutable  = mlx5_port_immutable;
2633eff901d3SEli Cohen 	if (mlx5_core_is_pf(mdev)) {
2634eff901d3SEli Cohen 		dev->ib_dev.get_vf_config	= mlx5_ib_get_vf_config;
2635eff901d3SEli Cohen 		dev->ib_dev.set_vf_link_state	= mlx5_ib_set_vf_link_state;
2636eff901d3SEli Cohen 		dev->ib_dev.get_vf_stats	= mlx5_ib_get_vf_stats;
2637eff901d3SEli Cohen 		dev->ib_dev.set_vf_guid		= mlx5_ib_set_vf_guid;
2638eff901d3SEli Cohen 	}
2639e126ba97SEli Cohen 
26407c2344c3SMaor Gottlieb 	dev->ib_dev.disassociate_ucontext = mlx5_ib_disassociate_ucontext;
26417c2344c3SMaor Gottlieb 
2642938fe83cSSaeed Mahameed 	mlx5_ib_internal_fill_odp_caps(dev);
26438cdd312cSHaggai Eran 
2644d2370e0aSMatan Barak 	if (MLX5_CAP_GEN(mdev, imaicl)) {
2645d2370e0aSMatan Barak 		dev->ib_dev.alloc_mw		= mlx5_ib_alloc_mw;
2646d2370e0aSMatan Barak 		dev->ib_dev.dealloc_mw		= mlx5_ib_dealloc_mw;
2647d2370e0aSMatan Barak 		dev->ib_dev.uverbs_cmd_mask |=
2648d2370e0aSMatan Barak 			(1ull << IB_USER_VERBS_CMD_ALLOC_MW)	|
2649d2370e0aSMatan Barak 			(1ull << IB_USER_VERBS_CMD_DEALLOC_MW);
2650d2370e0aSMatan Barak 	}
2651d2370e0aSMatan Barak 
2652938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, xrc)) {
2653e126ba97SEli Cohen 		dev->ib_dev.alloc_xrcd = mlx5_ib_alloc_xrcd;
2654e126ba97SEli Cohen 		dev->ib_dev.dealloc_xrcd = mlx5_ib_dealloc_xrcd;
2655e126ba97SEli Cohen 		dev->ib_dev.uverbs_cmd_mask |=
2656e126ba97SEli Cohen 			(1ull << IB_USER_VERBS_CMD_OPEN_XRCD) |
2657e126ba97SEli Cohen 			(1ull << IB_USER_VERBS_CMD_CLOSE_XRCD);
2658e126ba97SEli Cohen 	}
2659e126ba97SEli Cohen 
2660048ccca8SLinus Torvalds 	if (mlx5_ib_port_link_layer(&dev->ib_dev, 1) ==
2661038d2ef8SMaor Gottlieb 	    IB_LINK_LAYER_ETHERNET) {
2662038d2ef8SMaor Gottlieb 		dev->ib_dev.create_flow	= mlx5_ib_create_flow;
2663038d2ef8SMaor Gottlieb 		dev->ib_dev.destroy_flow = mlx5_ib_destroy_flow;
266479b20a6cSYishai Hadas 		dev->ib_dev.create_wq	 = mlx5_ib_create_wq;
266579b20a6cSYishai Hadas 		dev->ib_dev.modify_wq	 = mlx5_ib_modify_wq;
266679b20a6cSYishai Hadas 		dev->ib_dev.destroy_wq	 = mlx5_ib_destroy_wq;
2667c5f90929SYishai Hadas 		dev->ib_dev.create_rwq_ind_table = mlx5_ib_create_rwq_ind_table;
2668c5f90929SYishai Hadas 		dev->ib_dev.destroy_rwq_ind_table = mlx5_ib_destroy_rwq_ind_table;
2669038d2ef8SMaor Gottlieb 		dev->ib_dev.uverbs_ex_cmd_mask |=
2670038d2ef8SMaor Gottlieb 			(1ull << IB_USER_VERBS_EX_CMD_CREATE_FLOW) |
267179b20a6cSYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_DESTROY_FLOW) |
267279b20a6cSYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_CREATE_WQ) |
267379b20a6cSYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_MODIFY_WQ) |
2674c5f90929SYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_DESTROY_WQ) |
2675c5f90929SYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_CREATE_RWQ_IND_TBL) |
2676c5f90929SYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_DESTROY_RWQ_IND_TBL);
2677038d2ef8SMaor Gottlieb 	}
2678e126ba97SEli Cohen 	err = init_node_data(dev);
2679e126ba97SEli Cohen 	if (err)
2680233d05d2SSaeed Mahameed 		goto err_dealloc;
2681e126ba97SEli Cohen 
2682038d2ef8SMaor Gottlieb 	mutex_init(&dev->flow_db.lock);
2683e126ba97SEli Cohen 	mutex_init(&dev->cap_mask_mutex);
268489ea94a7SMaor Gottlieb 	INIT_LIST_HEAD(&dev->qp_list);
268589ea94a7SMaor Gottlieb 	spin_lock_init(&dev->reset_flow_resource_lock);
2686e126ba97SEli Cohen 
2687fc24fc5eSAchiad Shochat 	if (ll == IB_LINK_LAYER_ETHERNET) {
2688fc24fc5eSAchiad Shochat 		err = mlx5_enable_roce(dev);
2689e126ba97SEli Cohen 		if (err)
2690233d05d2SSaeed Mahameed 			goto err_dealloc;
2691fc24fc5eSAchiad Shochat 	}
2692fc24fc5eSAchiad Shochat 
2693fc24fc5eSAchiad Shochat 	err = create_dev_resources(&dev->devr);
2694fc24fc5eSAchiad Shochat 	if (err)
2695fc24fc5eSAchiad Shochat 		goto err_disable_roce;
2696e126ba97SEli Cohen 
26976aec21f6SHaggai Eran 	err = mlx5_ib_odp_init_one(dev);
2698281d1a92SWei Yongjun 	if (err)
2699e126ba97SEli Cohen 		goto err_rsrc;
2700e126ba97SEli Cohen 
27016aec21f6SHaggai Eran 	err = ib_register_device(&dev->ib_dev, NULL);
27026aec21f6SHaggai Eran 	if (err)
27036aec21f6SHaggai Eran 		goto err_odp;
27046aec21f6SHaggai Eran 
2705e126ba97SEli Cohen 	err = create_umr_res(dev);
2706e126ba97SEli Cohen 	if (err)
2707e126ba97SEli Cohen 		goto err_dev;
2708e126ba97SEli Cohen 
2709e126ba97SEli Cohen 	for (i = 0; i < ARRAY_SIZE(mlx5_class_attributes); i++) {
2710281d1a92SWei Yongjun 		err = device_create_file(&dev->ib_dev.dev,
2711281d1a92SWei Yongjun 					 mlx5_class_attributes[i]);
2712281d1a92SWei Yongjun 		if (err)
2713e126ba97SEli Cohen 			goto err_umrc;
2714e126ba97SEli Cohen 	}
2715e126ba97SEli Cohen 
2716e126ba97SEli Cohen 	dev->ib_active = true;
2717e126ba97SEli Cohen 
27189603b61dSJack Morgenstein 	return dev;
2719e126ba97SEli Cohen 
2720e126ba97SEli Cohen err_umrc:
2721e126ba97SEli Cohen 	destroy_umrc_res(dev);
2722e126ba97SEli Cohen 
2723e126ba97SEli Cohen err_dev:
2724e126ba97SEli Cohen 	ib_unregister_device(&dev->ib_dev);
2725e126ba97SEli Cohen 
27266aec21f6SHaggai Eran err_odp:
27276aec21f6SHaggai Eran 	mlx5_ib_odp_remove_one(dev);
27286aec21f6SHaggai Eran 
2729e126ba97SEli Cohen err_rsrc:
2730e126ba97SEli Cohen 	destroy_dev_resources(&dev->devr);
2731e126ba97SEli Cohen 
2732fc24fc5eSAchiad Shochat err_disable_roce:
2733fc24fc5eSAchiad Shochat 	if (ll == IB_LINK_LAYER_ETHERNET)
2734fc24fc5eSAchiad Shochat 		mlx5_disable_roce(dev);
2735fc24fc5eSAchiad Shochat 
27369603b61dSJack Morgenstein err_dealloc:
2737e126ba97SEli Cohen 	ib_dealloc_device((struct ib_device *)dev);
2738e126ba97SEli Cohen 
27399603b61dSJack Morgenstein 	return NULL;
2740e126ba97SEli Cohen }
2741e126ba97SEli Cohen 
27429603b61dSJack Morgenstein static void mlx5_ib_remove(struct mlx5_core_dev *mdev, void *context)
2743e126ba97SEli Cohen {
27449603b61dSJack Morgenstein 	struct mlx5_ib_dev *dev = context;
2745fc24fc5eSAchiad Shochat 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev, 1);
27466aec21f6SHaggai Eran 
2747e126ba97SEli Cohen 	ib_unregister_device(&dev->ib_dev);
2748eefd56e5SEli Cohen 	destroy_umrc_res(dev);
27496aec21f6SHaggai Eran 	mlx5_ib_odp_remove_one(dev);
2750e126ba97SEli Cohen 	destroy_dev_resources(&dev->devr);
2751fc24fc5eSAchiad Shochat 	if (ll == IB_LINK_LAYER_ETHERNET)
2752fc24fc5eSAchiad Shochat 		mlx5_disable_roce(dev);
2753e126ba97SEli Cohen 	ib_dealloc_device(&dev->ib_dev);
2754e126ba97SEli Cohen }
2755e126ba97SEli Cohen 
27569603b61dSJack Morgenstein static struct mlx5_interface mlx5_ib_interface = {
27579603b61dSJack Morgenstein 	.add            = mlx5_ib_add,
27589603b61dSJack Morgenstein 	.remove         = mlx5_ib_remove,
27599603b61dSJack Morgenstein 	.event          = mlx5_ib_event,
276064613d94SSaeed Mahameed 	.protocol	= MLX5_INTERFACE_PROTOCOL_IB,
2761e126ba97SEli Cohen };
2762e126ba97SEli Cohen 
2763e126ba97SEli Cohen static int __init mlx5_ib_init(void)
2764e126ba97SEli Cohen {
27656aec21f6SHaggai Eran 	int err;
27666aec21f6SHaggai Eran 
27679603b61dSJack Morgenstein 	if (deprecated_prof_sel != 2)
27689603b61dSJack Morgenstein 		pr_warn("prof_sel is deprecated for mlx5_ib, set it for mlx5_core\n");
27699603b61dSJack Morgenstein 
27706aec21f6SHaggai Eran 	err = mlx5_ib_odp_init();
27716aec21f6SHaggai Eran 	if (err)
27726aec21f6SHaggai Eran 		return err;
27736aec21f6SHaggai Eran 
27746aec21f6SHaggai Eran 	err = mlx5_register_interface(&mlx5_ib_interface);
27756aec21f6SHaggai Eran 	if (err)
27766aec21f6SHaggai Eran 		goto clean_odp;
27776aec21f6SHaggai Eran 
27786aec21f6SHaggai Eran 	return err;
27796aec21f6SHaggai Eran 
27806aec21f6SHaggai Eran clean_odp:
27816aec21f6SHaggai Eran 	mlx5_ib_odp_cleanup();
27826aec21f6SHaggai Eran 	return err;
2783e126ba97SEli Cohen }
2784e126ba97SEli Cohen 
2785e126ba97SEli Cohen static void __exit mlx5_ib_cleanup(void)
2786e126ba97SEli Cohen {
27879603b61dSJack Morgenstein 	mlx5_unregister_interface(&mlx5_ib_interface);
27886aec21f6SHaggai Eran 	mlx5_ib_odp_cleanup();
2789e126ba97SEli Cohen }
2790e126ba97SEli Cohen 
2791e126ba97SEli Cohen module_init(mlx5_ib_init);
2792e126ba97SEli Cohen module_exit(mlx5_ib_cleanup);
2793