xref: /openbmc/linux/drivers/infiniband/hw/mlx5/main.c (revision 72cd57178f3447b612f136c7a215418b5a20bf3d)
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>
4037aa5c36SGuy Levi #if defined(CONFIG_X86)
4137aa5c36SGuy Levi #include <asm/pat.h>
4237aa5c36SGuy Levi #endif
43e126ba97SEli Cohen #include <linux/sched.h>
447c2344c3SMaor Gottlieb #include <linux/delay.h>
45e126ba97SEli Cohen #include <rdma/ib_user_verbs.h>
463f89a643SAchiad Shochat #include <rdma/ib_addr.h>
472811ba51SAchiad Shochat #include <rdma/ib_cache.h>
48ada68c31SAchiad Shochat #include <linux/mlx5/port.h>
491b5daf11SMajd Dibbiny #include <linux/mlx5/vport.h>
507c2344c3SMaor Gottlieb #include <linux/list.h>
51e126ba97SEli Cohen #include <rdma/ib_smi.h>
52e126ba97SEli Cohen #include <rdma/ib_umem.h>
53038d2ef8SMaor Gottlieb #include <linux/in.h>
54038d2ef8SMaor Gottlieb #include <linux/etherdevice.h>
55038d2ef8SMaor Gottlieb #include <linux/mlx5/fs.h>
56e126ba97SEli Cohen #include "mlx5_ib.h"
57e126ba97SEli Cohen 
58e126ba97SEli Cohen #define DRIVER_NAME "mlx5_ib"
59169a1d85SAmir Vadai #define DRIVER_VERSION "2.2-1"
60169a1d85SAmir Vadai #define DRIVER_RELDATE	"Feb 2014"
61e126ba97SEli Cohen 
62e126ba97SEli Cohen MODULE_AUTHOR("Eli Cohen <eli@mellanox.com>");
63e126ba97SEli Cohen MODULE_DESCRIPTION("Mellanox Connect-IB HCA IB driver");
64e126ba97SEli Cohen MODULE_LICENSE("Dual BSD/GPL");
65e126ba97SEli Cohen MODULE_VERSION(DRIVER_VERSION);
66e126ba97SEli Cohen 
67e126ba97SEli Cohen static char mlx5_version[] =
68e126ba97SEli Cohen 	DRIVER_NAME ": Mellanox Connect-IB Infiniband driver v"
69e126ba97SEli Cohen 	DRIVER_VERSION " (" DRIVER_RELDATE ")\n";
70e126ba97SEli Cohen 
71da7525d2SEran Ben Elisha enum {
72da7525d2SEran Ben Elisha 	MLX5_ATOMIC_SIZE_QP_8BYTES = 1 << 3,
73da7525d2SEran Ben Elisha };
741b5daf11SMajd Dibbiny 
751b5daf11SMajd Dibbiny static enum rdma_link_layer
76ebd61f68SAchiad Shochat mlx5_port_type_cap_to_rdma_ll(int port_type_cap)
771b5daf11SMajd Dibbiny {
78ebd61f68SAchiad Shochat 	switch (port_type_cap) {
791b5daf11SMajd Dibbiny 	case MLX5_CAP_PORT_TYPE_IB:
801b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_INFINIBAND;
811b5daf11SMajd Dibbiny 	case MLX5_CAP_PORT_TYPE_ETH:
821b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_ETHERNET;
831b5daf11SMajd Dibbiny 	default:
841b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_UNSPECIFIED;
851b5daf11SMajd Dibbiny 	}
861b5daf11SMajd Dibbiny }
871b5daf11SMajd Dibbiny 
88ebd61f68SAchiad Shochat static enum rdma_link_layer
89ebd61f68SAchiad Shochat mlx5_ib_port_link_layer(struct ib_device *device, u8 port_num)
90ebd61f68SAchiad Shochat {
91ebd61f68SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(device);
92ebd61f68SAchiad Shochat 	int port_type_cap = MLX5_CAP_GEN(dev->mdev, port_type);
93ebd61f68SAchiad Shochat 
94ebd61f68SAchiad Shochat 	return mlx5_port_type_cap_to_rdma_ll(port_type_cap);
95ebd61f68SAchiad Shochat }
96ebd61f68SAchiad Shochat 
97fc24fc5eSAchiad Shochat static int mlx5_netdev_event(struct notifier_block *this,
98fc24fc5eSAchiad Shochat 			     unsigned long event, void *ptr)
99fc24fc5eSAchiad Shochat {
100fc24fc5eSAchiad Shochat 	struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
101fc24fc5eSAchiad Shochat 	struct mlx5_ib_dev *ibdev = container_of(this, struct mlx5_ib_dev,
102fc24fc5eSAchiad Shochat 						 roce.nb);
103fc24fc5eSAchiad Shochat 
1045ec8c83eSAviv Heller 	switch (event) {
1055ec8c83eSAviv Heller 	case NETDEV_REGISTER:
1065ec8c83eSAviv Heller 	case NETDEV_UNREGISTER:
107fc24fc5eSAchiad Shochat 		write_lock(&ibdev->roce.netdev_lock);
108fc24fc5eSAchiad Shochat 		if (ndev->dev.parent == &ibdev->mdev->pdev->dev)
1095ec8c83eSAviv Heller 			ibdev->roce.netdev = (event == NETDEV_UNREGISTER) ?
1105ec8c83eSAviv Heller 					     NULL : ndev;
111fc24fc5eSAchiad Shochat 		write_unlock(&ibdev->roce.netdev_lock);
1125ec8c83eSAviv Heller 		break;
1135ec8c83eSAviv Heller 
1145ec8c83eSAviv Heller 	case NETDEV_UP:
11588621dfeSAviv Heller 	case NETDEV_DOWN: {
11688621dfeSAviv Heller 		struct net_device *lag_ndev = mlx5_lag_get_roce_netdev(ibdev->mdev);
11788621dfeSAviv Heller 		struct net_device *upper = NULL;
11888621dfeSAviv Heller 
11988621dfeSAviv Heller 		if (lag_ndev) {
12088621dfeSAviv Heller 			upper = netdev_master_upper_dev_get(lag_ndev);
12188621dfeSAviv Heller 			dev_put(lag_ndev);
12288621dfeSAviv Heller 		}
12388621dfeSAviv Heller 
12488621dfeSAviv Heller 		if ((upper == ndev || (!upper && ndev == ibdev->roce.netdev))
12588621dfeSAviv Heller 		    && ibdev->ib_active) {
126626bc02dSBart Van Assche 			struct ib_event ibev = { };
1275ec8c83eSAviv Heller 
1285ec8c83eSAviv Heller 			ibev.device = &ibdev->ib_dev;
1295ec8c83eSAviv Heller 			ibev.event = (event == NETDEV_UP) ?
1305ec8c83eSAviv Heller 				     IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
1315ec8c83eSAviv Heller 			ibev.element.port_num = 1;
1325ec8c83eSAviv Heller 			ib_dispatch_event(&ibev);
1335ec8c83eSAviv Heller 		}
1345ec8c83eSAviv Heller 		break;
13588621dfeSAviv Heller 	}
1365ec8c83eSAviv Heller 
1375ec8c83eSAviv Heller 	default:
1385ec8c83eSAviv Heller 		break;
1395ec8c83eSAviv Heller 	}
140fc24fc5eSAchiad Shochat 
141fc24fc5eSAchiad Shochat 	return NOTIFY_DONE;
142fc24fc5eSAchiad Shochat }
143fc24fc5eSAchiad Shochat 
144fc24fc5eSAchiad Shochat static struct net_device *mlx5_ib_get_netdev(struct ib_device *device,
145fc24fc5eSAchiad Shochat 					     u8 port_num)
146fc24fc5eSAchiad Shochat {
147fc24fc5eSAchiad Shochat 	struct mlx5_ib_dev *ibdev = to_mdev(device);
148fc24fc5eSAchiad Shochat 	struct net_device *ndev;
149fc24fc5eSAchiad Shochat 
15088621dfeSAviv Heller 	ndev = mlx5_lag_get_roce_netdev(ibdev->mdev);
15188621dfeSAviv Heller 	if (ndev)
15288621dfeSAviv Heller 		return ndev;
15388621dfeSAviv Heller 
154fc24fc5eSAchiad Shochat 	/* Ensure ndev does not disappear before we invoke dev_hold()
155fc24fc5eSAchiad Shochat 	 */
156fc24fc5eSAchiad Shochat 	read_lock(&ibdev->roce.netdev_lock);
157fc24fc5eSAchiad Shochat 	ndev = ibdev->roce.netdev;
158fc24fc5eSAchiad Shochat 	if (ndev)
159fc24fc5eSAchiad Shochat 		dev_hold(ndev);
160fc24fc5eSAchiad Shochat 	read_unlock(&ibdev->roce.netdev_lock);
161fc24fc5eSAchiad Shochat 
162fc24fc5eSAchiad Shochat 	return ndev;
163fc24fc5eSAchiad Shochat }
164fc24fc5eSAchiad Shochat 
1653f89a643SAchiad Shochat static int mlx5_query_port_roce(struct ib_device *device, u8 port_num,
1663f89a643SAchiad Shochat 				struct ib_port_attr *props)
1673f89a643SAchiad Shochat {
1683f89a643SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(device);
16988621dfeSAviv Heller 	struct net_device *ndev, *upper;
1703f89a643SAchiad Shochat 	enum ib_mtu ndev_ib_mtu;
171c876a1b7SLeon Romanovsky 	u16 qkey_viol_cntr;
1723f89a643SAchiad Shochat 
1733f89a643SAchiad Shochat 	memset(props, 0, sizeof(*props));
1743f89a643SAchiad Shochat 
1753f89a643SAchiad Shochat 	props->port_cap_flags  |= IB_PORT_CM_SUP;
1763f89a643SAchiad Shochat 	props->port_cap_flags  |= IB_PORT_IP_BASED_GIDS;
1773f89a643SAchiad Shochat 
1783f89a643SAchiad Shochat 	props->gid_tbl_len      = MLX5_CAP_ROCE(dev->mdev,
1793f89a643SAchiad Shochat 						roce_address_table_size);
1803f89a643SAchiad Shochat 	props->max_mtu          = IB_MTU_4096;
1813f89a643SAchiad Shochat 	props->max_msg_sz       = 1 << MLX5_CAP_GEN(dev->mdev, log_max_msg);
1823f89a643SAchiad Shochat 	props->pkey_tbl_len     = 1;
1833f89a643SAchiad Shochat 	props->state            = IB_PORT_DOWN;
1843f89a643SAchiad Shochat 	props->phys_state       = 3;
1853f89a643SAchiad Shochat 
186c876a1b7SLeon Romanovsky 	mlx5_query_nic_vport_qkey_viol_cntr(dev->mdev, &qkey_viol_cntr);
187c876a1b7SLeon Romanovsky 	props->qkey_viol_cntr = qkey_viol_cntr;
1883f89a643SAchiad Shochat 
1893f89a643SAchiad Shochat 	ndev = mlx5_ib_get_netdev(device, port_num);
1903f89a643SAchiad Shochat 	if (!ndev)
1913f89a643SAchiad Shochat 		return 0;
1923f89a643SAchiad Shochat 
19388621dfeSAviv Heller 	if (mlx5_lag_is_active(dev->mdev)) {
19488621dfeSAviv Heller 		rcu_read_lock();
19588621dfeSAviv Heller 		upper = netdev_master_upper_dev_get_rcu(ndev);
19688621dfeSAviv Heller 		if (upper) {
19788621dfeSAviv Heller 			dev_put(ndev);
19888621dfeSAviv Heller 			ndev = upper;
19988621dfeSAviv Heller 			dev_hold(ndev);
20088621dfeSAviv Heller 		}
20188621dfeSAviv Heller 		rcu_read_unlock();
20288621dfeSAviv Heller 	}
20388621dfeSAviv Heller 
2043f89a643SAchiad Shochat 	if (netif_running(ndev) && netif_carrier_ok(ndev)) {
2053f89a643SAchiad Shochat 		props->state      = IB_PORT_ACTIVE;
2063f89a643SAchiad Shochat 		props->phys_state = 5;
2073f89a643SAchiad Shochat 	}
2083f89a643SAchiad Shochat 
2093f89a643SAchiad Shochat 	ndev_ib_mtu = iboe_get_mtu(ndev->mtu);
2103f89a643SAchiad Shochat 
2113f89a643SAchiad Shochat 	dev_put(ndev);
2123f89a643SAchiad Shochat 
2133f89a643SAchiad Shochat 	props->active_mtu	= min(props->max_mtu, ndev_ib_mtu);
2143f89a643SAchiad Shochat 
2153f89a643SAchiad Shochat 	props->active_width	= IB_WIDTH_4X;  /* TODO */
2163f89a643SAchiad Shochat 	props->active_speed	= IB_SPEED_QDR; /* TODO */
2173f89a643SAchiad Shochat 
2183f89a643SAchiad Shochat 	return 0;
2193f89a643SAchiad Shochat }
2203f89a643SAchiad Shochat 
2213cca2606SAchiad Shochat static void ib_gid_to_mlx5_roce_addr(const union ib_gid *gid,
2223cca2606SAchiad Shochat 				     const struct ib_gid_attr *attr,
2233cca2606SAchiad Shochat 				     void *mlx5_addr)
2243cca2606SAchiad Shochat {
2253cca2606SAchiad Shochat #define MLX5_SET_RA(p, f, v) MLX5_SET(roce_addr_layout, p, f, v)
2263cca2606SAchiad Shochat 	char *mlx5_addr_l3_addr	= MLX5_ADDR_OF(roce_addr_layout, mlx5_addr,
2273cca2606SAchiad Shochat 					       source_l3_address);
2283cca2606SAchiad Shochat 	void *mlx5_addr_mac	= MLX5_ADDR_OF(roce_addr_layout, mlx5_addr,
2293cca2606SAchiad Shochat 					       source_mac_47_32);
2303cca2606SAchiad Shochat 
2313cca2606SAchiad Shochat 	if (!gid)
2323cca2606SAchiad Shochat 		return;
2333cca2606SAchiad Shochat 
2343cca2606SAchiad Shochat 	ether_addr_copy(mlx5_addr_mac, attr->ndev->dev_addr);
2353cca2606SAchiad Shochat 
2363cca2606SAchiad Shochat 	if (is_vlan_dev(attr->ndev)) {
2373cca2606SAchiad Shochat 		MLX5_SET_RA(mlx5_addr, vlan_valid, 1);
2383cca2606SAchiad Shochat 		MLX5_SET_RA(mlx5_addr, vlan_id, vlan_dev_vlan_id(attr->ndev));
2393cca2606SAchiad Shochat 	}
2403cca2606SAchiad Shochat 
2413cca2606SAchiad Shochat 	switch (attr->gid_type) {
2423cca2606SAchiad Shochat 	case IB_GID_TYPE_IB:
2433cca2606SAchiad Shochat 		MLX5_SET_RA(mlx5_addr, roce_version, MLX5_ROCE_VERSION_1);
2443cca2606SAchiad Shochat 		break;
2453cca2606SAchiad Shochat 	case IB_GID_TYPE_ROCE_UDP_ENCAP:
2463cca2606SAchiad Shochat 		MLX5_SET_RA(mlx5_addr, roce_version, MLX5_ROCE_VERSION_2);
2473cca2606SAchiad Shochat 		break;
2483cca2606SAchiad Shochat 
2493cca2606SAchiad Shochat 	default:
2503cca2606SAchiad Shochat 		WARN_ON(true);
2513cca2606SAchiad Shochat 	}
2523cca2606SAchiad Shochat 
2533cca2606SAchiad Shochat 	if (attr->gid_type != IB_GID_TYPE_IB) {
2543cca2606SAchiad Shochat 		if (ipv6_addr_v4mapped((void *)gid))
2553cca2606SAchiad Shochat 			MLX5_SET_RA(mlx5_addr, roce_l3_type,
2563cca2606SAchiad Shochat 				    MLX5_ROCE_L3_TYPE_IPV4);
2573cca2606SAchiad Shochat 		else
2583cca2606SAchiad Shochat 			MLX5_SET_RA(mlx5_addr, roce_l3_type,
2593cca2606SAchiad Shochat 				    MLX5_ROCE_L3_TYPE_IPV6);
2603cca2606SAchiad Shochat 	}
2613cca2606SAchiad Shochat 
2623cca2606SAchiad Shochat 	if ((attr->gid_type == IB_GID_TYPE_IB) ||
2633cca2606SAchiad Shochat 	    !ipv6_addr_v4mapped((void *)gid))
2643cca2606SAchiad Shochat 		memcpy(mlx5_addr_l3_addr, gid, sizeof(*gid));
2653cca2606SAchiad Shochat 	else
2663cca2606SAchiad Shochat 		memcpy(&mlx5_addr_l3_addr[12], &gid->raw[12], 4);
2673cca2606SAchiad Shochat }
2683cca2606SAchiad Shochat 
2693cca2606SAchiad Shochat static int set_roce_addr(struct ib_device *device, u8 port_num,
2703cca2606SAchiad Shochat 			 unsigned int index,
2713cca2606SAchiad Shochat 			 const union ib_gid *gid,
2723cca2606SAchiad Shochat 			 const struct ib_gid_attr *attr)
2733cca2606SAchiad Shochat {
2743cca2606SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(device);
275c4f287c4SSaeed Mahameed 	u32  in[MLX5_ST_SZ_DW(set_roce_address_in)]  = {0};
276c4f287c4SSaeed Mahameed 	u32 out[MLX5_ST_SZ_DW(set_roce_address_out)] = {0};
2773cca2606SAchiad Shochat 	void *in_addr = MLX5_ADDR_OF(set_roce_address_in, in, roce_address);
2783cca2606SAchiad Shochat 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(device, port_num);
2793cca2606SAchiad Shochat 
2803cca2606SAchiad Shochat 	if (ll != IB_LINK_LAYER_ETHERNET)
2813cca2606SAchiad Shochat 		return -EINVAL;
2823cca2606SAchiad Shochat 
2833cca2606SAchiad Shochat 	ib_gid_to_mlx5_roce_addr(gid, attr, in_addr);
2843cca2606SAchiad Shochat 
2853cca2606SAchiad Shochat 	MLX5_SET(set_roce_address_in, in, roce_address_index, index);
2863cca2606SAchiad Shochat 	MLX5_SET(set_roce_address_in, in, opcode, MLX5_CMD_OP_SET_ROCE_ADDRESS);
2873cca2606SAchiad Shochat 	return mlx5_cmd_exec(dev->mdev, in, sizeof(in), out, sizeof(out));
2883cca2606SAchiad Shochat }
2893cca2606SAchiad Shochat 
2903cca2606SAchiad Shochat static int mlx5_ib_add_gid(struct ib_device *device, u8 port_num,
2913cca2606SAchiad Shochat 			   unsigned int index, const union ib_gid *gid,
2923cca2606SAchiad Shochat 			   const struct ib_gid_attr *attr,
2933cca2606SAchiad Shochat 			   __always_unused void **context)
2943cca2606SAchiad Shochat {
2953cca2606SAchiad Shochat 	return set_roce_addr(device, port_num, index, gid, attr);
2963cca2606SAchiad Shochat }
2973cca2606SAchiad Shochat 
2983cca2606SAchiad Shochat static int mlx5_ib_del_gid(struct ib_device *device, u8 port_num,
2993cca2606SAchiad Shochat 			   unsigned int index, __always_unused void **context)
3003cca2606SAchiad Shochat {
3013cca2606SAchiad Shochat 	return set_roce_addr(device, port_num, index, NULL, NULL);
3023cca2606SAchiad Shochat }
3033cca2606SAchiad Shochat 
3042811ba51SAchiad Shochat __be16 mlx5_get_roce_udp_sport(struct mlx5_ib_dev *dev, u8 port_num,
3052811ba51SAchiad Shochat 			       int index)
3062811ba51SAchiad Shochat {
3072811ba51SAchiad Shochat 	struct ib_gid_attr attr;
3082811ba51SAchiad Shochat 	union ib_gid gid;
3092811ba51SAchiad Shochat 
3102811ba51SAchiad Shochat 	if (ib_get_cached_gid(&dev->ib_dev, port_num, index, &gid, &attr))
3112811ba51SAchiad Shochat 		return 0;
3122811ba51SAchiad Shochat 
3132811ba51SAchiad Shochat 	if (!attr.ndev)
3142811ba51SAchiad Shochat 		return 0;
3152811ba51SAchiad Shochat 
3162811ba51SAchiad Shochat 	dev_put(attr.ndev);
3172811ba51SAchiad Shochat 
3182811ba51SAchiad Shochat 	if (attr.gid_type != IB_GID_TYPE_ROCE_UDP_ENCAP)
3192811ba51SAchiad Shochat 		return 0;
3202811ba51SAchiad Shochat 
3212811ba51SAchiad Shochat 	return cpu_to_be16(MLX5_CAP_ROCE(dev->mdev, r_roce_min_src_udp_port));
3222811ba51SAchiad Shochat }
3232811ba51SAchiad Shochat 
324ed88451eSMajd Dibbiny int mlx5_get_roce_gid_type(struct mlx5_ib_dev *dev, u8 port_num,
325ed88451eSMajd Dibbiny 			   int index, enum ib_gid_type *gid_type)
326ed88451eSMajd Dibbiny {
327ed88451eSMajd Dibbiny 	struct ib_gid_attr attr;
328ed88451eSMajd Dibbiny 	union ib_gid gid;
329ed88451eSMajd Dibbiny 	int ret;
330ed88451eSMajd Dibbiny 
331ed88451eSMajd Dibbiny 	ret = ib_get_cached_gid(&dev->ib_dev, port_num, index, &gid, &attr);
332ed88451eSMajd Dibbiny 	if (ret)
333ed88451eSMajd Dibbiny 		return ret;
334ed88451eSMajd Dibbiny 
335ed88451eSMajd Dibbiny 	if (!attr.ndev)
336ed88451eSMajd Dibbiny 		return -ENODEV;
337ed88451eSMajd Dibbiny 
338ed88451eSMajd Dibbiny 	dev_put(attr.ndev);
339ed88451eSMajd Dibbiny 
340ed88451eSMajd Dibbiny 	*gid_type = attr.gid_type;
341ed88451eSMajd Dibbiny 
342ed88451eSMajd Dibbiny 	return 0;
343ed88451eSMajd Dibbiny }
344ed88451eSMajd Dibbiny 
3451b5daf11SMajd Dibbiny static int mlx5_use_mad_ifc(struct mlx5_ib_dev *dev)
3461b5daf11SMajd Dibbiny {
3477fae6655SNoa Osherovich 	if (MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_IB)
348d603c809SEli Cohen 		return !MLX5_CAP_GEN(dev->mdev, ib_virt);
3497fae6655SNoa Osherovich 	return 0;
3501b5daf11SMajd Dibbiny }
3511b5daf11SMajd Dibbiny 
3521b5daf11SMajd Dibbiny enum {
3531b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_MAD,
3541b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_HCA,
3551b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_NIC,
3561b5daf11SMajd Dibbiny };
3571b5daf11SMajd Dibbiny 
3581b5daf11SMajd Dibbiny static int mlx5_get_vport_access_method(struct ib_device *ibdev)
3591b5daf11SMajd Dibbiny {
3601b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(to_mdev(ibdev)))
3611b5daf11SMajd Dibbiny 		return MLX5_VPORT_ACCESS_METHOD_MAD;
3621b5daf11SMajd Dibbiny 
363ebd61f68SAchiad Shochat 	if (mlx5_ib_port_link_layer(ibdev, 1) ==
3641b5daf11SMajd Dibbiny 	    IB_LINK_LAYER_ETHERNET)
3651b5daf11SMajd Dibbiny 		return MLX5_VPORT_ACCESS_METHOD_NIC;
3661b5daf11SMajd Dibbiny 
3671b5daf11SMajd Dibbiny 	return MLX5_VPORT_ACCESS_METHOD_HCA;
3681b5daf11SMajd Dibbiny }
3691b5daf11SMajd Dibbiny 
370da7525d2SEran Ben Elisha static void get_atomic_caps(struct mlx5_ib_dev *dev,
371da7525d2SEran Ben Elisha 			    struct ib_device_attr *props)
372da7525d2SEran Ben Elisha {
373da7525d2SEran Ben Elisha 	u8 tmp;
374da7525d2SEran Ben Elisha 	u8 atomic_operations = MLX5_CAP_ATOMIC(dev->mdev, atomic_operations);
375da7525d2SEran Ben Elisha 	u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_qp);
376da7525d2SEran Ben Elisha 	u8 atomic_req_8B_endianness_mode =
377da7525d2SEran Ben Elisha 		MLX5_CAP_ATOMIC(dev->mdev, atomic_req_8B_endianess_mode);
378da7525d2SEran Ben Elisha 
379da7525d2SEran Ben Elisha 	/* Check if HW supports 8 bytes standard atomic operations and capable
380da7525d2SEran Ben Elisha 	 * of host endianness respond
381da7525d2SEran Ben Elisha 	 */
382da7525d2SEran Ben Elisha 	tmp = MLX5_ATOMIC_OPS_CMP_SWAP | MLX5_ATOMIC_OPS_FETCH_ADD;
383da7525d2SEran Ben Elisha 	if (((atomic_operations & tmp) == tmp) &&
384da7525d2SEran Ben Elisha 	    (atomic_size_qp & MLX5_ATOMIC_SIZE_QP_8BYTES) &&
385da7525d2SEran Ben Elisha 	    (atomic_req_8B_endianness_mode)) {
386da7525d2SEran Ben Elisha 		props->atomic_cap = IB_ATOMIC_HCA;
387da7525d2SEran Ben Elisha 	} else {
388da7525d2SEran Ben Elisha 		props->atomic_cap = IB_ATOMIC_NONE;
389da7525d2SEran Ben Elisha 	}
390da7525d2SEran Ben Elisha }
391da7525d2SEran Ben Elisha 
3921b5daf11SMajd Dibbiny static int mlx5_query_system_image_guid(struct ib_device *ibdev,
3931b5daf11SMajd Dibbiny 					__be64 *sys_image_guid)
3941b5daf11SMajd Dibbiny {
3951b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
3961b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
3971b5daf11SMajd Dibbiny 	u64 tmp;
3981b5daf11SMajd Dibbiny 	int err;
3991b5daf11SMajd Dibbiny 
4001b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
4011b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
4021b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_system_image_guid(ibdev,
4031b5daf11SMajd Dibbiny 							    sys_image_guid);
4041b5daf11SMajd Dibbiny 
4051b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
4061b5daf11SMajd Dibbiny 		err = mlx5_query_hca_vport_system_image_guid(mdev, &tmp);
4073f89a643SAchiad Shochat 		break;
4083f89a643SAchiad Shochat 
4093f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
4103f89a643SAchiad Shochat 		err = mlx5_query_nic_vport_system_image_guid(mdev, &tmp);
4113f89a643SAchiad Shochat 		break;
4121b5daf11SMajd Dibbiny 
4131b5daf11SMajd Dibbiny 	default:
4141b5daf11SMajd Dibbiny 		return -EINVAL;
4151b5daf11SMajd Dibbiny 	}
4163f89a643SAchiad Shochat 
4173f89a643SAchiad Shochat 	if (!err)
4183f89a643SAchiad Shochat 		*sys_image_guid = cpu_to_be64(tmp);
4193f89a643SAchiad Shochat 
4203f89a643SAchiad Shochat 	return err;
4213f89a643SAchiad Shochat 
4221b5daf11SMajd Dibbiny }
4231b5daf11SMajd Dibbiny 
4241b5daf11SMajd Dibbiny static int mlx5_query_max_pkeys(struct ib_device *ibdev,
4251b5daf11SMajd Dibbiny 				u16 *max_pkeys)
4261b5daf11SMajd Dibbiny {
4271b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
4281b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
4291b5daf11SMajd Dibbiny 
4301b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
4311b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
4321b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_max_pkeys(ibdev, max_pkeys);
4331b5daf11SMajd Dibbiny 
4341b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
4351b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
4361b5daf11SMajd Dibbiny 		*max_pkeys = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev,
4371b5daf11SMajd Dibbiny 						pkey_table_size));
4381b5daf11SMajd Dibbiny 		return 0;
4391b5daf11SMajd Dibbiny 
4401b5daf11SMajd Dibbiny 	default:
4411b5daf11SMajd Dibbiny 		return -EINVAL;
4421b5daf11SMajd Dibbiny 	}
4431b5daf11SMajd Dibbiny }
4441b5daf11SMajd Dibbiny 
4451b5daf11SMajd Dibbiny static int mlx5_query_vendor_id(struct ib_device *ibdev,
4461b5daf11SMajd Dibbiny 				u32 *vendor_id)
4471b5daf11SMajd Dibbiny {
4481b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
4491b5daf11SMajd Dibbiny 
4501b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
4511b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
4521b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_vendor_id(ibdev, vendor_id);
4531b5daf11SMajd Dibbiny 
4541b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
4551b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
4561b5daf11SMajd Dibbiny 		return mlx5_core_query_vendor_id(dev->mdev, vendor_id);
4571b5daf11SMajd Dibbiny 
4581b5daf11SMajd Dibbiny 	default:
4591b5daf11SMajd Dibbiny 		return -EINVAL;
4601b5daf11SMajd Dibbiny 	}
4611b5daf11SMajd Dibbiny }
4621b5daf11SMajd Dibbiny 
4631b5daf11SMajd Dibbiny static int mlx5_query_node_guid(struct mlx5_ib_dev *dev,
4641b5daf11SMajd Dibbiny 				__be64 *node_guid)
4651b5daf11SMajd Dibbiny {
4661b5daf11SMajd Dibbiny 	u64 tmp;
4671b5daf11SMajd Dibbiny 	int err;
4681b5daf11SMajd Dibbiny 
4691b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(&dev->ib_dev)) {
4701b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
4711b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_node_guid(dev, node_guid);
4721b5daf11SMajd Dibbiny 
4731b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
4741b5daf11SMajd Dibbiny 		err = mlx5_query_hca_vport_node_guid(dev->mdev, &tmp);
4753f89a643SAchiad Shochat 		break;
4763f89a643SAchiad Shochat 
4773f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
4783f89a643SAchiad Shochat 		err = mlx5_query_nic_vport_node_guid(dev->mdev, &tmp);
4793f89a643SAchiad Shochat 		break;
4801b5daf11SMajd Dibbiny 
4811b5daf11SMajd Dibbiny 	default:
4821b5daf11SMajd Dibbiny 		return -EINVAL;
4831b5daf11SMajd Dibbiny 	}
4843f89a643SAchiad Shochat 
4853f89a643SAchiad Shochat 	if (!err)
4863f89a643SAchiad Shochat 		*node_guid = cpu_to_be64(tmp);
4873f89a643SAchiad Shochat 
4883f89a643SAchiad Shochat 	return err;
4891b5daf11SMajd Dibbiny }
4901b5daf11SMajd Dibbiny 
4911b5daf11SMajd Dibbiny struct mlx5_reg_node_desc {
492bd99fdeaSYuval Shaia 	u8	desc[IB_DEVICE_NODE_DESC_MAX];
4931b5daf11SMajd Dibbiny };
4941b5daf11SMajd Dibbiny 
4951b5daf11SMajd Dibbiny static int mlx5_query_node_desc(struct mlx5_ib_dev *dev, char *node_desc)
4961b5daf11SMajd Dibbiny {
4971b5daf11SMajd Dibbiny 	struct mlx5_reg_node_desc in;
4981b5daf11SMajd Dibbiny 
4991b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(dev))
5001b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_node_desc(dev, node_desc);
5011b5daf11SMajd Dibbiny 
5021b5daf11SMajd Dibbiny 	memset(&in, 0, sizeof(in));
5031b5daf11SMajd Dibbiny 
5041b5daf11SMajd Dibbiny 	return mlx5_core_access_reg(dev->mdev, &in, sizeof(in), node_desc,
5051b5daf11SMajd Dibbiny 				    sizeof(struct mlx5_reg_node_desc),
5061b5daf11SMajd Dibbiny 				    MLX5_REG_NODE_DESC, 0, 0);
5071b5daf11SMajd Dibbiny }
5081b5daf11SMajd Dibbiny 
509e126ba97SEli Cohen static int mlx5_ib_query_device(struct ib_device *ibdev,
5102528e33eSMatan Barak 				struct ib_device_attr *props,
5112528e33eSMatan Barak 				struct ib_udata *uhw)
512e126ba97SEli Cohen {
513e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
514938fe83cSSaeed Mahameed 	struct mlx5_core_dev *mdev = dev->mdev;
515e126ba97SEli Cohen 	int err = -ENOMEM;
516288c01b7SEli Cohen 	int max_sq_desc;
517e126ba97SEli Cohen 	int max_rq_sg;
518e126ba97SEli Cohen 	int max_sq_sg;
519e0238a6aSSagi Grimberg 	u64 min_page_size = 1ull << MLX5_CAP_GEN(mdev, log_pg_sz);
520402ca536SBodong Wang 	struct mlx5_ib_query_device_resp resp = {};
521402ca536SBodong Wang 	size_t resp_len;
522402ca536SBodong Wang 	u64 max_tso;
523e126ba97SEli Cohen 
524402ca536SBodong Wang 	resp_len = sizeof(resp.comp_mask) + sizeof(resp.response_length);
525402ca536SBodong Wang 	if (uhw->outlen && uhw->outlen < resp_len)
526402ca536SBodong Wang 		return -EINVAL;
527402ca536SBodong Wang 	else
528402ca536SBodong Wang 		resp.response_length = resp_len;
529402ca536SBodong Wang 
530402ca536SBodong Wang 	if (uhw->inlen && !ib_is_udata_cleared(uhw, 0, uhw->inlen))
5312528e33eSMatan Barak 		return -EINVAL;
5322528e33eSMatan Barak 
533e126ba97SEli Cohen 	memset(props, 0, sizeof(*props));
5341b5daf11SMajd Dibbiny 	err = mlx5_query_system_image_guid(ibdev,
5351b5daf11SMajd Dibbiny 					   &props->sys_image_guid);
5361b5daf11SMajd Dibbiny 	if (err)
5371b5daf11SMajd Dibbiny 		return err;
5381b5daf11SMajd Dibbiny 
5391b5daf11SMajd Dibbiny 	err = mlx5_query_max_pkeys(ibdev, &props->max_pkeys);
5401b5daf11SMajd Dibbiny 	if (err)
5411b5daf11SMajd Dibbiny 		return err;
5421b5daf11SMajd Dibbiny 
5431b5daf11SMajd Dibbiny 	err = mlx5_query_vendor_id(ibdev, &props->vendor_id);
5441b5daf11SMajd Dibbiny 	if (err)
5451b5daf11SMajd Dibbiny 		return err;
546e126ba97SEli Cohen 
5479603b61dSJack Morgenstein 	props->fw_ver = ((u64)fw_rev_maj(dev->mdev) << 32) |
5489603b61dSJack Morgenstein 		(fw_rev_min(dev->mdev) << 16) |
5499603b61dSJack Morgenstein 		fw_rev_sub(dev->mdev);
550e126ba97SEli Cohen 	props->device_cap_flags    = IB_DEVICE_CHANGE_PHY_PORT |
551e126ba97SEli Cohen 		IB_DEVICE_PORT_ACTIVE_EVENT		|
552e126ba97SEli Cohen 		IB_DEVICE_SYS_IMAGE_GUID		|
5531a4c3a3dSEli Cohen 		IB_DEVICE_RC_RNR_NAK_GEN;
554938fe83cSSaeed Mahameed 
555938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, pkv))
556e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
557938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, qkv))
558e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
559938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, apm))
560e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
561938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, xrc))
562e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_XRC;
563d2370e0aSMatan Barak 	if (MLX5_CAP_GEN(mdev, imaicl)) {
564d2370e0aSMatan Barak 		props->device_cap_flags |= IB_DEVICE_MEM_WINDOW |
565d2370e0aSMatan Barak 					   IB_DEVICE_MEM_WINDOW_TYPE_2B;
566d2370e0aSMatan Barak 		props->max_mw = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
567b005d316SSagi Grimberg 		/* We support 'Gappy' memory registration too */
568b005d316SSagi Grimberg 		props->device_cap_flags |= IB_DEVICE_SG_GAPS_REG;
569d2370e0aSMatan Barak 	}
570e126ba97SEli Cohen 	props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
571938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, sho)) {
5722dea9094SSagi Grimberg 		props->device_cap_flags |= IB_DEVICE_SIGNATURE_HANDOVER;
5732dea9094SSagi Grimberg 		/* At this stage no support for signature handover */
5742dea9094SSagi Grimberg 		props->sig_prot_cap = IB_PROT_T10DIF_TYPE_1 |
5752dea9094SSagi Grimberg 				      IB_PROT_T10DIF_TYPE_2 |
5762dea9094SSagi Grimberg 				      IB_PROT_T10DIF_TYPE_3;
5772dea9094SSagi Grimberg 		props->sig_guard_cap = IB_GUARD_T10DIF_CRC |
5782dea9094SSagi Grimberg 				       IB_GUARD_T10DIF_CSUM;
5792dea9094SSagi Grimberg 	}
580938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, block_lb_mc))
581f360d88aSEli Cohen 		props->device_cap_flags |= IB_DEVICE_BLOCK_MULTICAST_LOOPBACK;
582e126ba97SEli Cohen 
583402ca536SBodong Wang 	if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads)) {
584e8161334SNoa Osherovich 		if (MLX5_CAP_ETH(mdev, csum_cap)) {
585e8161334SNoa Osherovich 			/* Legacy bit to support old userspace libraries */
58688115fe7SBodong Wang 			props->device_cap_flags |= IB_DEVICE_RAW_IP_CSUM;
587e8161334SNoa Osherovich 			props->raw_packet_caps |= IB_RAW_PACKET_CAP_IP_CSUM;
588e8161334SNoa Osherovich 		}
589e8161334SNoa Osherovich 
590e8161334SNoa Osherovich 		if (MLX5_CAP_ETH(dev->mdev, vlan_cap))
591e8161334SNoa Osherovich 			props->raw_packet_caps |=
592e8161334SNoa Osherovich 				IB_RAW_PACKET_CAP_CVLAN_STRIPPING;
59388115fe7SBodong Wang 
594402ca536SBodong Wang 		if (field_avail(typeof(resp), tso_caps, uhw->outlen)) {
595402ca536SBodong Wang 			max_tso = MLX5_CAP_ETH(mdev, max_lso_cap);
596402ca536SBodong Wang 			if (max_tso) {
597402ca536SBodong Wang 				resp.tso_caps.max_tso = 1 << max_tso;
598402ca536SBodong Wang 				resp.tso_caps.supported_qpts |=
599402ca536SBodong Wang 					1 << IB_QPT_RAW_PACKET;
600402ca536SBodong Wang 				resp.response_length += sizeof(resp.tso_caps);
601402ca536SBodong Wang 			}
602402ca536SBodong Wang 		}
60331f69a82SYishai Hadas 
60431f69a82SYishai Hadas 		if (field_avail(typeof(resp), rss_caps, uhw->outlen)) {
60531f69a82SYishai Hadas 			resp.rss_caps.rx_hash_function =
60631f69a82SYishai Hadas 						MLX5_RX_HASH_FUNC_TOEPLITZ;
60731f69a82SYishai Hadas 			resp.rss_caps.rx_hash_fields_mask =
60831f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_IPV4 |
60931f69a82SYishai Hadas 						MLX5_RX_HASH_DST_IPV4 |
61031f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_IPV6 |
61131f69a82SYishai Hadas 						MLX5_RX_HASH_DST_IPV6 |
61231f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_PORT_TCP |
61331f69a82SYishai Hadas 						MLX5_RX_HASH_DST_PORT_TCP |
61431f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_PORT_UDP |
61531f69a82SYishai Hadas 						MLX5_RX_HASH_DST_PORT_UDP;
61631f69a82SYishai Hadas 			resp.response_length += sizeof(resp.rss_caps);
61731f69a82SYishai Hadas 		}
61831f69a82SYishai Hadas 	} else {
61931f69a82SYishai Hadas 		if (field_avail(typeof(resp), tso_caps, uhw->outlen))
62031f69a82SYishai Hadas 			resp.response_length += sizeof(resp.tso_caps);
62131f69a82SYishai Hadas 		if (field_avail(typeof(resp), rss_caps, uhw->outlen))
62231f69a82SYishai Hadas 			resp.response_length += sizeof(resp.rss_caps);
623402ca536SBodong Wang 	}
624402ca536SBodong Wang 
625f0313965SErez Shitrit 	if (MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) {
626f0313965SErez Shitrit 		props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
627f0313965SErez Shitrit 		props->device_cap_flags |= IB_DEVICE_UD_TSO;
628f0313965SErez Shitrit 	}
629f0313965SErez Shitrit 
630cff5a0f3SMajd Dibbiny 	if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
631e8161334SNoa Osherovich 	    MLX5_CAP_ETH(dev->mdev, scatter_fcs)) {
632e8161334SNoa Osherovich 		/* Legacy bit to support old userspace libraries */
633cff5a0f3SMajd Dibbiny 		props->device_cap_flags |= IB_DEVICE_RAW_SCATTER_FCS;
634e8161334SNoa Osherovich 		props->raw_packet_caps |= IB_RAW_PACKET_CAP_SCATTER_FCS;
635e8161334SNoa Osherovich 	}
636cff5a0f3SMajd Dibbiny 
637da6d6ba3SMaor Gottlieb 	if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS))
638da6d6ba3SMaor Gottlieb 		props->device_cap_flags |= IB_DEVICE_MANAGED_FLOW_STEERING;
639da6d6ba3SMaor Gottlieb 
6401b5daf11SMajd Dibbiny 	props->vendor_part_id	   = mdev->pdev->device;
6411b5daf11SMajd Dibbiny 	props->hw_ver		   = mdev->pdev->revision;
642e126ba97SEli Cohen 
643e126ba97SEli Cohen 	props->max_mr_size	   = ~0ull;
644e0238a6aSSagi Grimberg 	props->page_size_cap	   = ~(min_page_size - 1);
645938fe83cSSaeed Mahameed 	props->max_qp		   = 1 << MLX5_CAP_GEN(mdev, log_max_qp);
646938fe83cSSaeed Mahameed 	props->max_qp_wr	   = 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
647938fe83cSSaeed Mahameed 	max_rq_sg =  MLX5_CAP_GEN(mdev, max_wqe_sz_rq) /
648938fe83cSSaeed Mahameed 		     sizeof(struct mlx5_wqe_data_seg);
649288c01b7SEli Cohen 	max_sq_desc = min_t(int, MLX5_CAP_GEN(mdev, max_wqe_sz_sq), 512);
650288c01b7SEli Cohen 	max_sq_sg = (max_sq_desc - sizeof(struct mlx5_wqe_ctrl_seg) -
651288c01b7SEli Cohen 		     sizeof(struct mlx5_wqe_raddr_seg)) /
652e126ba97SEli Cohen 		sizeof(struct mlx5_wqe_data_seg);
653e126ba97SEli Cohen 	props->max_sge = min(max_rq_sg, max_sq_sg);
654986ef95eSSagi Grimberg 	props->max_sge_rd	   = MLX5_MAX_SGE_RD;
655938fe83cSSaeed Mahameed 	props->max_cq		   = 1 << MLX5_CAP_GEN(mdev, log_max_cq);
6569f177686SLeon Romanovsky 	props->max_cqe = (1 << MLX5_CAP_GEN(mdev, log_max_cq_sz)) - 1;
657938fe83cSSaeed Mahameed 	props->max_mr		   = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
658938fe83cSSaeed Mahameed 	props->max_pd		   = 1 << MLX5_CAP_GEN(mdev, log_max_pd);
659938fe83cSSaeed Mahameed 	props->max_qp_rd_atom	   = 1 << MLX5_CAP_GEN(mdev, log_max_ra_req_qp);
660938fe83cSSaeed Mahameed 	props->max_qp_init_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_res_qp);
661938fe83cSSaeed Mahameed 	props->max_srq		   = 1 << MLX5_CAP_GEN(mdev, log_max_srq);
662938fe83cSSaeed Mahameed 	props->max_srq_wr = (1 << MLX5_CAP_GEN(mdev, log_max_srq_sz)) - 1;
663938fe83cSSaeed Mahameed 	props->local_ca_ack_delay  = MLX5_CAP_GEN(mdev, local_ca_ack_delay);
664e126ba97SEli Cohen 	props->max_res_rd_atom	   = props->max_qp_rd_atom * props->max_qp;
665e126ba97SEli Cohen 	props->max_srq_sge	   = max_rq_sg - 1;
666911f4331SSagi Grimberg 	props->max_fast_reg_page_list_len =
667911f4331SSagi Grimberg 		1 << MLX5_CAP_GEN(mdev, log_max_klm_list_size);
668da7525d2SEran Ben Elisha 	get_atomic_caps(dev, props);
66981bea28fSEli Cohen 	props->masked_atomic_cap   = IB_ATOMIC_NONE;
670938fe83cSSaeed Mahameed 	props->max_mcast_grp	   = 1 << MLX5_CAP_GEN(mdev, log_max_mcg);
671938fe83cSSaeed Mahameed 	props->max_mcast_qp_attach = MLX5_CAP_GEN(mdev, max_qp_mcg);
672e126ba97SEli Cohen 	props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
673e126ba97SEli Cohen 					   props->max_mcast_grp;
674e126ba97SEli Cohen 	props->max_map_per_fmr = INT_MAX; /* no limit in ConnectIB */
67586695a65SMaor Gottlieb 	props->max_ah = INT_MAX;
6767c60bcbbSMatan Barak 	props->hca_core_clock = MLX5_CAP_GEN(mdev, device_frequency_khz);
6777c60bcbbSMatan Barak 	props->timestamp_mask = 0x7FFFFFFFFFFFFFFFULL;
678e126ba97SEli Cohen 
6798cdd312cSHaggai Eran #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
680938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, pg))
6818cdd312cSHaggai Eran 		props->device_cap_flags |= IB_DEVICE_ON_DEMAND_PAGING;
6828cdd312cSHaggai Eran 	props->odp_caps = dev->odp_caps;
6838cdd312cSHaggai Eran #endif
6848cdd312cSHaggai Eran 
685051f2630SLeon Romanovsky 	if (MLX5_CAP_GEN(mdev, cd))
686051f2630SLeon Romanovsky 		props->device_cap_flags |= IB_DEVICE_CROSS_CHANNEL;
687051f2630SLeon Romanovsky 
688eff901d3SEli Cohen 	if (!mlx5_core_is_pf(mdev))
689eff901d3SEli Cohen 		props->device_cap_flags |= IB_DEVICE_VIRTUAL_FUNCTION;
690eff901d3SEli Cohen 
69131f69a82SYishai Hadas 	if (mlx5_ib_port_link_layer(ibdev, 1) ==
69231f69a82SYishai Hadas 	    IB_LINK_LAYER_ETHERNET) {
69331f69a82SYishai Hadas 		props->rss_caps.max_rwq_indirection_tables =
69431f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt);
69531f69a82SYishai Hadas 		props->rss_caps.max_rwq_indirection_table_size =
69631f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt_size);
69731f69a82SYishai Hadas 		props->rss_caps.supported_qpts = 1 << IB_QPT_RAW_PACKET;
69831f69a82SYishai Hadas 		props->max_wq_type_rq =
69931f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rq);
70031f69a82SYishai Hadas 	}
70131f69a82SYishai Hadas 
7027e43a2a5SBodong Wang 	if (field_avail(typeof(resp), cqe_comp_caps, uhw->outlen)) {
7037e43a2a5SBodong Wang 		resp.cqe_comp_caps.max_num =
7047e43a2a5SBodong Wang 			MLX5_CAP_GEN(dev->mdev, cqe_compression) ?
7057e43a2a5SBodong Wang 			MLX5_CAP_GEN(dev->mdev, cqe_compression_max_num) : 0;
7067e43a2a5SBodong Wang 		resp.cqe_comp_caps.supported_format =
7077e43a2a5SBodong Wang 			MLX5_IB_CQE_RES_FORMAT_HASH |
7087e43a2a5SBodong Wang 			MLX5_IB_CQE_RES_FORMAT_CSUM;
7097e43a2a5SBodong Wang 		resp.response_length += sizeof(resp.cqe_comp_caps);
7107e43a2a5SBodong Wang 	}
7117e43a2a5SBodong Wang 
712d949167dSBodong Wang 	if (field_avail(typeof(resp), packet_pacing_caps, uhw->outlen)) {
713d949167dSBodong Wang 		if (MLX5_CAP_QOS(mdev, packet_pacing) &&
714d949167dSBodong Wang 		    MLX5_CAP_GEN(mdev, qos)) {
715d949167dSBodong Wang 			resp.packet_pacing_caps.qp_rate_limit_max =
716d949167dSBodong Wang 				MLX5_CAP_QOS(mdev, packet_pacing_max_rate);
717d949167dSBodong Wang 			resp.packet_pacing_caps.qp_rate_limit_min =
718d949167dSBodong Wang 				MLX5_CAP_QOS(mdev, packet_pacing_min_rate);
719d949167dSBodong Wang 			resp.packet_pacing_caps.supported_qpts |=
720d949167dSBodong Wang 				1 << IB_QPT_RAW_PACKET;
721d949167dSBodong Wang 		}
722d949167dSBodong Wang 		resp.response_length += sizeof(resp.packet_pacing_caps);
723d949167dSBodong Wang 	}
724d949167dSBodong Wang 
7259f885201SLeon Romanovsky 	if (field_avail(typeof(resp), mlx5_ib_support_multi_pkt_send_wqes,
7269f885201SLeon Romanovsky 			uhw->outlen)) {
7279f885201SLeon Romanovsky 		resp.mlx5_ib_support_multi_pkt_send_wqes =
7289f885201SLeon Romanovsky 			MLX5_CAP_ETH(mdev, multi_pkt_send_wqe);
7299f885201SLeon Romanovsky 		resp.response_length +=
7309f885201SLeon Romanovsky 			sizeof(resp.mlx5_ib_support_multi_pkt_send_wqes);
7319f885201SLeon Romanovsky 	}
7329f885201SLeon Romanovsky 
7339f885201SLeon Romanovsky 	if (field_avail(typeof(resp), reserved, uhw->outlen))
7349f885201SLeon Romanovsky 		resp.response_length += sizeof(resp.reserved);
7359f885201SLeon Romanovsky 
736402ca536SBodong Wang 	if (uhw->outlen) {
737402ca536SBodong Wang 		err = ib_copy_to_udata(uhw, &resp, resp.response_length);
738402ca536SBodong Wang 
739402ca536SBodong Wang 		if (err)
740402ca536SBodong Wang 			return err;
741402ca536SBodong Wang 	}
742402ca536SBodong Wang 
7431b5daf11SMajd Dibbiny 	return 0;
7441b5daf11SMajd Dibbiny }
745e126ba97SEli Cohen 
7461b5daf11SMajd Dibbiny enum mlx5_ib_width {
7471b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_1X	= 1 << 0,
7481b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_2X	= 1 << 1,
7491b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_4X	= 1 << 2,
7501b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_8X	= 1 << 3,
7511b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_12X	= 1 << 4
7521b5daf11SMajd Dibbiny };
7531b5daf11SMajd Dibbiny 
7541b5daf11SMajd Dibbiny static int translate_active_width(struct ib_device *ibdev, u8 active_width,
7551b5daf11SMajd Dibbiny 				  u8 *ib_width)
7561b5daf11SMajd Dibbiny {
7571b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
7581b5daf11SMajd Dibbiny 	int err = 0;
7591b5daf11SMajd Dibbiny 
7601b5daf11SMajd Dibbiny 	if (active_width & MLX5_IB_WIDTH_1X) {
7611b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_1X;
7621b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_2X) {
7631b5daf11SMajd Dibbiny 		mlx5_ib_dbg(dev, "active_width %d is not supported by IB spec\n",
7641b5daf11SMajd Dibbiny 			    (int)active_width);
7651b5daf11SMajd Dibbiny 		err = -EINVAL;
7661b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_4X) {
7671b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_4X;
7681b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_8X) {
7691b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_8X;
7701b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_12X) {
7711b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_12X;
7721b5daf11SMajd Dibbiny 	} else {
7731b5daf11SMajd Dibbiny 		mlx5_ib_dbg(dev, "Invalid active_width %d\n",
7741b5daf11SMajd Dibbiny 			    (int)active_width);
7751b5daf11SMajd Dibbiny 		err = -EINVAL;
7761b5daf11SMajd Dibbiny 	}
7771b5daf11SMajd Dibbiny 
7781b5daf11SMajd Dibbiny 	return err;
7791b5daf11SMajd Dibbiny }
7801b5daf11SMajd Dibbiny 
7811b5daf11SMajd Dibbiny static int mlx5_mtu_to_ib_mtu(int mtu)
7821b5daf11SMajd Dibbiny {
7831b5daf11SMajd Dibbiny 	switch (mtu) {
7841b5daf11SMajd Dibbiny 	case 256: return 1;
7851b5daf11SMajd Dibbiny 	case 512: return 2;
7861b5daf11SMajd Dibbiny 	case 1024: return 3;
7871b5daf11SMajd Dibbiny 	case 2048: return 4;
7881b5daf11SMajd Dibbiny 	case 4096: return 5;
7891b5daf11SMajd Dibbiny 	default:
7901b5daf11SMajd Dibbiny 		pr_warn("invalid mtu\n");
7911b5daf11SMajd Dibbiny 		return -1;
7921b5daf11SMajd Dibbiny 	}
7931b5daf11SMajd Dibbiny }
7941b5daf11SMajd Dibbiny 
7951b5daf11SMajd Dibbiny enum ib_max_vl_num {
7961b5daf11SMajd Dibbiny 	__IB_MAX_VL_0		= 1,
7971b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_1		= 2,
7981b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_3		= 3,
7991b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_7		= 4,
8001b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_14	= 5,
8011b5daf11SMajd Dibbiny };
8021b5daf11SMajd Dibbiny 
8031b5daf11SMajd Dibbiny enum mlx5_vl_hw_cap {
8041b5daf11SMajd Dibbiny 	MLX5_VL_HW_0	= 1,
8051b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_1	= 2,
8061b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_2	= 3,
8071b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_3	= 4,
8081b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_4	= 5,
8091b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_5	= 6,
8101b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_6	= 7,
8111b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_7	= 8,
8121b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_14	= 15
8131b5daf11SMajd Dibbiny };
8141b5daf11SMajd Dibbiny 
8151b5daf11SMajd Dibbiny static int translate_max_vl_num(struct ib_device *ibdev, u8 vl_hw_cap,
8161b5daf11SMajd Dibbiny 				u8 *max_vl_num)
8171b5daf11SMajd Dibbiny {
8181b5daf11SMajd Dibbiny 	switch (vl_hw_cap) {
8191b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0:
8201b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0;
8211b5daf11SMajd Dibbiny 		break;
8221b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_1:
8231b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_1;
8241b5daf11SMajd Dibbiny 		break;
8251b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_3:
8261b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_3;
8271b5daf11SMajd Dibbiny 		break;
8281b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_7:
8291b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_7;
8301b5daf11SMajd Dibbiny 		break;
8311b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_14:
8321b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_14;
8331b5daf11SMajd Dibbiny 		break;
8341b5daf11SMajd Dibbiny 
8351b5daf11SMajd Dibbiny 	default:
8361b5daf11SMajd Dibbiny 		return -EINVAL;
8371b5daf11SMajd Dibbiny 	}
8381b5daf11SMajd Dibbiny 
8391b5daf11SMajd Dibbiny 	return 0;
8401b5daf11SMajd Dibbiny }
8411b5daf11SMajd Dibbiny 
8421b5daf11SMajd Dibbiny static int mlx5_query_hca_port(struct ib_device *ibdev, u8 port,
8431b5daf11SMajd Dibbiny 			       struct ib_port_attr *props)
8441b5daf11SMajd Dibbiny {
8451b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
8461b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
8471b5daf11SMajd Dibbiny 	struct mlx5_hca_vport_context *rep;
848046339eaSSaeed Mahameed 	u16 max_mtu;
849046339eaSSaeed Mahameed 	u16 oper_mtu;
8501b5daf11SMajd Dibbiny 	int err;
8511b5daf11SMajd Dibbiny 	u8 ib_link_width_oper;
8521b5daf11SMajd Dibbiny 	u8 vl_hw_cap;
8531b5daf11SMajd Dibbiny 
8541b5daf11SMajd Dibbiny 	rep = kzalloc(sizeof(*rep), GFP_KERNEL);
8551b5daf11SMajd Dibbiny 	if (!rep) {
8561b5daf11SMajd Dibbiny 		err = -ENOMEM;
8571b5daf11SMajd Dibbiny 		goto out;
8581b5daf11SMajd Dibbiny 	}
8591b5daf11SMajd Dibbiny 
8601b5daf11SMajd Dibbiny 	memset(props, 0, sizeof(*props));
8611b5daf11SMajd Dibbiny 
8621b5daf11SMajd Dibbiny 	err = mlx5_query_hca_vport_context(mdev, 0, port, 0, rep);
8631b5daf11SMajd Dibbiny 	if (err)
8641b5daf11SMajd Dibbiny 		goto out;
8651b5daf11SMajd Dibbiny 
8661b5daf11SMajd Dibbiny 	props->lid		= rep->lid;
8671b5daf11SMajd Dibbiny 	props->lmc		= rep->lmc;
8681b5daf11SMajd Dibbiny 	props->sm_lid		= rep->sm_lid;
8691b5daf11SMajd Dibbiny 	props->sm_sl		= rep->sm_sl;
8701b5daf11SMajd Dibbiny 	props->state		= rep->vport_state;
8711b5daf11SMajd Dibbiny 	props->phys_state	= rep->port_physical_state;
8721b5daf11SMajd Dibbiny 	props->port_cap_flags	= rep->cap_mask1;
8731b5daf11SMajd Dibbiny 	props->gid_tbl_len	= mlx5_get_gid_table_len(MLX5_CAP_GEN(mdev, gid_table_size));
8741b5daf11SMajd Dibbiny 	props->max_msg_sz	= 1 << MLX5_CAP_GEN(mdev, log_max_msg);
8751b5daf11SMajd Dibbiny 	props->pkey_tbl_len	= mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, pkey_table_size));
8761b5daf11SMajd Dibbiny 	props->bad_pkey_cntr	= rep->pkey_violation_counter;
8771b5daf11SMajd Dibbiny 	props->qkey_viol_cntr	= rep->qkey_violation_counter;
8781b5daf11SMajd Dibbiny 	props->subnet_timeout	= rep->subnet_timeout;
8791b5daf11SMajd Dibbiny 	props->init_type_reply	= rep->init_type_reply;
880eff901d3SEli Cohen 	props->grh_required	= rep->grh_required;
8811b5daf11SMajd Dibbiny 
8821b5daf11SMajd Dibbiny 	err = mlx5_query_port_link_width_oper(mdev, &ib_link_width_oper, port);
8831b5daf11SMajd Dibbiny 	if (err)
8841b5daf11SMajd Dibbiny 		goto out;
8851b5daf11SMajd Dibbiny 
8861b5daf11SMajd Dibbiny 	err = translate_active_width(ibdev, ib_link_width_oper,
8871b5daf11SMajd Dibbiny 				     &props->active_width);
8881b5daf11SMajd Dibbiny 	if (err)
8891b5daf11SMajd Dibbiny 		goto out;
890d5beb7f2SNoa Osherovich 	err = mlx5_query_port_ib_proto_oper(mdev, &props->active_speed, port);
8911b5daf11SMajd Dibbiny 	if (err)
8921b5daf11SMajd Dibbiny 		goto out;
8931b5daf11SMajd Dibbiny 
894facc9699SSaeed Mahameed 	mlx5_query_port_max_mtu(mdev, &max_mtu, port);
8951b5daf11SMajd Dibbiny 
8961b5daf11SMajd Dibbiny 	props->max_mtu = mlx5_mtu_to_ib_mtu(max_mtu);
8971b5daf11SMajd Dibbiny 
898facc9699SSaeed Mahameed 	mlx5_query_port_oper_mtu(mdev, &oper_mtu, port);
8991b5daf11SMajd Dibbiny 
9001b5daf11SMajd Dibbiny 	props->active_mtu = mlx5_mtu_to_ib_mtu(oper_mtu);
9011b5daf11SMajd Dibbiny 
9021b5daf11SMajd Dibbiny 	err = mlx5_query_port_vl_hw_cap(mdev, &vl_hw_cap, port);
9031b5daf11SMajd Dibbiny 	if (err)
9041b5daf11SMajd Dibbiny 		goto out;
9051b5daf11SMajd Dibbiny 
9061b5daf11SMajd Dibbiny 	err = translate_max_vl_num(ibdev, vl_hw_cap,
9071b5daf11SMajd Dibbiny 				   &props->max_vl_num);
9081b5daf11SMajd Dibbiny out:
9091b5daf11SMajd Dibbiny 	kfree(rep);
910e126ba97SEli Cohen 	return err;
911e126ba97SEli Cohen }
912e126ba97SEli Cohen 
913e126ba97SEli Cohen int mlx5_ib_query_port(struct ib_device *ibdev, u8 port,
914e126ba97SEli Cohen 		       struct ib_port_attr *props)
915e126ba97SEli Cohen {
9161b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
9171b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
9181b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_port(ibdev, port, props);
919e126ba97SEli Cohen 
9201b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
9211b5daf11SMajd Dibbiny 		return mlx5_query_hca_port(ibdev, port, props);
9221b5daf11SMajd Dibbiny 
9233f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
9243f89a643SAchiad Shochat 		return mlx5_query_port_roce(ibdev, port, props);
9253f89a643SAchiad Shochat 
9261b5daf11SMajd Dibbiny 	default:
927e126ba97SEli Cohen 		return -EINVAL;
928e126ba97SEli Cohen 	}
929e126ba97SEli Cohen }
930e126ba97SEli Cohen 
931e126ba97SEli Cohen static int mlx5_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
932e126ba97SEli Cohen 			     union ib_gid *gid)
933e126ba97SEli Cohen {
9341b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
9351b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
936e126ba97SEli Cohen 
9371b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
9381b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
9391b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_gids(ibdev, port, index, gid);
940e126ba97SEli Cohen 
9411b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
9421b5daf11SMajd Dibbiny 		return mlx5_query_hca_vport_gid(mdev, 0, port, 0, index, gid);
943e126ba97SEli Cohen 
9441b5daf11SMajd Dibbiny 	default:
9451b5daf11SMajd Dibbiny 		return -EINVAL;
9461b5daf11SMajd Dibbiny 	}
947e126ba97SEli Cohen 
948e126ba97SEli Cohen }
949e126ba97SEli Cohen 
950e126ba97SEli Cohen static int mlx5_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
951e126ba97SEli Cohen 			      u16 *pkey)
952e126ba97SEli Cohen {
9531b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
9541b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
955e126ba97SEli Cohen 
9561b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
9571b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
9581b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_pkey(ibdev, port, index, pkey);
959e126ba97SEli Cohen 
9601b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
9611b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
9621b5daf11SMajd Dibbiny 		return mlx5_query_hca_vport_pkey(mdev, 0, port,  0, index,
9631b5daf11SMajd Dibbiny 						 pkey);
9641b5daf11SMajd Dibbiny 	default:
9651b5daf11SMajd Dibbiny 		return -EINVAL;
966e126ba97SEli Cohen 	}
9671b5daf11SMajd Dibbiny }
968e126ba97SEli Cohen 
969e126ba97SEli Cohen static int mlx5_ib_modify_device(struct ib_device *ibdev, int mask,
970e126ba97SEli Cohen 				 struct ib_device_modify *props)
971e126ba97SEli Cohen {
972e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
973e126ba97SEli Cohen 	struct mlx5_reg_node_desc in;
974e126ba97SEli Cohen 	struct mlx5_reg_node_desc out;
975e126ba97SEli Cohen 	int err;
976e126ba97SEli Cohen 
977e126ba97SEli Cohen 	if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
978e126ba97SEli Cohen 		return -EOPNOTSUPP;
979e126ba97SEli Cohen 
980e126ba97SEli Cohen 	if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
981e126ba97SEli Cohen 		return 0;
982e126ba97SEli Cohen 
983e126ba97SEli Cohen 	/*
984e126ba97SEli Cohen 	 * If possible, pass node desc to FW, so it can generate
985e126ba97SEli Cohen 	 * a 144 trap.  If cmd fails, just ignore.
986e126ba97SEli Cohen 	 */
987bd99fdeaSYuval Shaia 	memcpy(&in, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
9889603b61dSJack Morgenstein 	err = mlx5_core_access_reg(dev->mdev, &in, sizeof(in), &out,
989e126ba97SEli Cohen 				   sizeof(out), MLX5_REG_NODE_DESC, 0, 1);
990e126ba97SEli Cohen 	if (err)
991e126ba97SEli Cohen 		return err;
992e126ba97SEli Cohen 
993bd99fdeaSYuval Shaia 	memcpy(ibdev->node_desc, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
994e126ba97SEli Cohen 
995e126ba97SEli Cohen 	return err;
996e126ba97SEli Cohen }
997e126ba97SEli Cohen 
998e126ba97SEli Cohen static int mlx5_ib_modify_port(struct ib_device *ibdev, u8 port, int mask,
999e126ba97SEli Cohen 			       struct ib_port_modify *props)
1000e126ba97SEli Cohen {
1001e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1002e126ba97SEli Cohen 	struct ib_port_attr attr;
1003e126ba97SEli Cohen 	u32 tmp;
1004e126ba97SEli Cohen 	int err;
1005e126ba97SEli Cohen 
1006e126ba97SEli Cohen 	mutex_lock(&dev->cap_mask_mutex);
1007e126ba97SEli Cohen 
1008e126ba97SEli Cohen 	err = mlx5_ib_query_port(ibdev, port, &attr);
1009e126ba97SEli Cohen 	if (err)
1010e126ba97SEli Cohen 		goto out;
1011e126ba97SEli Cohen 
1012e126ba97SEli Cohen 	tmp = (attr.port_cap_flags | props->set_port_cap_mask) &
1013e126ba97SEli Cohen 		~props->clr_port_cap_mask;
1014e126ba97SEli Cohen 
10159603b61dSJack Morgenstein 	err = mlx5_set_port_caps(dev->mdev, port, tmp);
1016e126ba97SEli Cohen 
1017e126ba97SEli Cohen out:
1018e126ba97SEli Cohen 	mutex_unlock(&dev->cap_mask_mutex);
1019e126ba97SEli Cohen 	return err;
1020e126ba97SEli Cohen }
1021e126ba97SEli Cohen 
102230aa60b3SEli Cohen static void print_lib_caps(struct mlx5_ib_dev *dev, u64 caps)
102330aa60b3SEli Cohen {
102430aa60b3SEli Cohen 	mlx5_ib_dbg(dev, "MLX5_LIB_CAP_4K_UAR = %s\n",
102530aa60b3SEli Cohen 		    caps & MLX5_LIB_CAP_4K_UAR ? "y" : "n");
102630aa60b3SEli Cohen }
102730aa60b3SEli Cohen 
1028b037c29aSEli Cohen static int calc_total_bfregs(struct mlx5_ib_dev *dev, bool lib_uar_4k,
1029b037c29aSEli Cohen 			     struct mlx5_ib_alloc_ucontext_req_v2 *req,
1030b037c29aSEli Cohen 			     u32 *num_sys_pages)
1031b037c29aSEli Cohen {
1032b037c29aSEli Cohen 	int uars_per_sys_page;
1033b037c29aSEli Cohen 	int bfregs_per_sys_page;
1034b037c29aSEli Cohen 	int ref_bfregs = req->total_num_bfregs;
1035b037c29aSEli Cohen 
1036b037c29aSEli Cohen 	if (req->total_num_bfregs == 0)
1037b037c29aSEli Cohen 		return -EINVAL;
1038b037c29aSEli Cohen 
1039b037c29aSEli Cohen 	BUILD_BUG_ON(MLX5_MAX_BFREGS % MLX5_NON_FP_BFREGS_IN_PAGE);
1040b037c29aSEli Cohen 	BUILD_BUG_ON(MLX5_MAX_BFREGS < MLX5_NON_FP_BFREGS_IN_PAGE);
1041b037c29aSEli Cohen 
1042b037c29aSEli Cohen 	if (req->total_num_bfregs > MLX5_MAX_BFREGS)
1043b037c29aSEli Cohen 		return -ENOMEM;
1044b037c29aSEli Cohen 
1045b037c29aSEli Cohen 	uars_per_sys_page = get_uars_per_sys_page(dev, lib_uar_4k);
1046b037c29aSEli Cohen 	bfregs_per_sys_page = uars_per_sys_page * MLX5_NON_FP_BFREGS_PER_UAR;
1047b037c29aSEli Cohen 	req->total_num_bfregs = ALIGN(req->total_num_bfregs, bfregs_per_sys_page);
1048b037c29aSEli Cohen 	*num_sys_pages = req->total_num_bfregs / bfregs_per_sys_page;
1049b037c29aSEli Cohen 
1050b037c29aSEli Cohen 	if (req->num_low_latency_bfregs > req->total_num_bfregs - 1)
1051b037c29aSEli Cohen 		return -EINVAL;
1052b037c29aSEli Cohen 
1053b037c29aSEli Cohen 	mlx5_ib_dbg(dev, "uar_4k: fw support %s, lib support %s, user requested %d bfregs, alloated %d, using %d sys pages\n",
1054b037c29aSEli Cohen 		    MLX5_CAP_GEN(dev->mdev, uar_4k) ? "yes" : "no",
1055b037c29aSEli Cohen 		    lib_uar_4k ? "yes" : "no", ref_bfregs,
1056b037c29aSEli Cohen 		    req->total_num_bfregs, *num_sys_pages);
1057b037c29aSEli Cohen 
1058b037c29aSEli Cohen 	return 0;
1059b037c29aSEli Cohen }
1060b037c29aSEli Cohen 
1061b037c29aSEli Cohen static int allocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
1062b037c29aSEli Cohen {
1063b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1064b037c29aSEli Cohen 	int err;
1065b037c29aSEli Cohen 	int i;
1066b037c29aSEli Cohen 
1067b037c29aSEli Cohen 	bfregi = &context->bfregi;
1068b037c29aSEli Cohen 	for (i = 0; i < bfregi->num_sys_pages; i++) {
1069b037c29aSEli Cohen 		err = mlx5_cmd_alloc_uar(dev->mdev, &bfregi->sys_pages[i]);
1070b037c29aSEli Cohen 		if (err)
1071b037c29aSEli Cohen 			goto error;
1072b037c29aSEli Cohen 
1073b037c29aSEli Cohen 		mlx5_ib_dbg(dev, "allocated uar %d\n", bfregi->sys_pages[i]);
1074b037c29aSEli Cohen 	}
1075b037c29aSEli Cohen 	return 0;
1076b037c29aSEli Cohen 
1077b037c29aSEli Cohen error:
1078b037c29aSEli Cohen 	for (--i; i >= 0; i--)
1079b037c29aSEli Cohen 		if (mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]))
1080b037c29aSEli Cohen 			mlx5_ib_warn(dev, "failed to free uar %d\n", i);
1081b037c29aSEli Cohen 
1082b037c29aSEli Cohen 	return err;
1083b037c29aSEli Cohen }
1084b037c29aSEli Cohen 
1085b037c29aSEli Cohen static int deallocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
1086b037c29aSEli Cohen {
1087b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1088b037c29aSEli Cohen 	int err;
1089b037c29aSEli Cohen 	int i;
1090b037c29aSEli Cohen 
1091b037c29aSEli Cohen 	bfregi = &context->bfregi;
1092b037c29aSEli Cohen 	for (i = 0; i < bfregi->num_sys_pages; i++) {
1093b037c29aSEli Cohen 		err = mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]);
1094b037c29aSEli Cohen 		if (err) {
1095b037c29aSEli Cohen 			mlx5_ib_warn(dev, "failed to free uar %d\n", i);
1096b037c29aSEli Cohen 			return err;
1097b037c29aSEli Cohen 		}
1098b037c29aSEli Cohen 	}
1099b037c29aSEli Cohen 	return 0;
1100b037c29aSEli Cohen }
1101b037c29aSEli Cohen 
1102e126ba97SEli Cohen static struct ib_ucontext *mlx5_ib_alloc_ucontext(struct ib_device *ibdev,
1103e126ba97SEli Cohen 						  struct ib_udata *udata)
1104e126ba97SEli Cohen {
1105e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1106b368d7cbSMatan Barak 	struct mlx5_ib_alloc_ucontext_req_v2 req = {};
1107b368d7cbSMatan Barak 	struct mlx5_ib_alloc_ucontext_resp resp = {};
1108e126ba97SEli Cohen 	struct mlx5_ib_ucontext *context;
11092f5ff264SEli Cohen 	struct mlx5_bfreg_info *bfregi;
111078c0f98cSEli Cohen 	int ver;
1111e126ba97SEli Cohen 	int err;
1112f241e749SJack Morgenstein 	size_t reqlen;
1113a168a41cSMajd Dibbiny 	size_t min_req_v2 = offsetof(struct mlx5_ib_alloc_ucontext_req_v2,
1114a168a41cSMajd Dibbiny 				     max_cqe_version);
1115b037c29aSEli Cohen 	bool lib_uar_4k;
1116e126ba97SEli Cohen 
1117e126ba97SEli Cohen 	if (!dev->ib_active)
1118e126ba97SEli Cohen 		return ERR_PTR(-EAGAIN);
1119e126ba97SEli Cohen 
1120dfbee859SHaggai Abramovsky 	if (udata->inlen < sizeof(struct ib_uverbs_cmd_hdr))
1121dfbee859SHaggai Abramovsky 		return ERR_PTR(-EINVAL);
1122dfbee859SHaggai Abramovsky 
112378c0f98cSEli Cohen 	reqlen = udata->inlen - sizeof(struct ib_uverbs_cmd_hdr);
112478c0f98cSEli Cohen 	if (reqlen == sizeof(struct mlx5_ib_alloc_ucontext_req))
112578c0f98cSEli Cohen 		ver = 0;
1126a168a41cSMajd Dibbiny 	else if (reqlen >= min_req_v2)
112778c0f98cSEli Cohen 		ver = 2;
112878c0f98cSEli Cohen 	else
112978c0f98cSEli Cohen 		return ERR_PTR(-EINVAL);
113078c0f98cSEli Cohen 
1131b368d7cbSMatan Barak 	err = ib_copy_from_udata(&req, udata, min(reqlen, sizeof(req)));
1132e126ba97SEli Cohen 	if (err)
1133e126ba97SEli Cohen 		return ERR_PTR(err);
1134e126ba97SEli Cohen 
1135b368d7cbSMatan Barak 	if (req.flags)
113678c0f98cSEli Cohen 		return ERR_PTR(-EINVAL);
113778c0f98cSEli Cohen 
1138f72300c5SHaggai Abramovsky 	if (req.comp_mask || req.reserved0 || req.reserved1 || req.reserved2)
1139b368d7cbSMatan Barak 		return ERR_PTR(-EOPNOTSUPP);
1140b368d7cbSMatan Barak 
11412f5ff264SEli Cohen 	req.total_num_bfregs = ALIGN(req.total_num_bfregs,
11422f5ff264SEli Cohen 				    MLX5_NON_FP_BFREGS_PER_UAR);
11432f5ff264SEli Cohen 	if (req.num_low_latency_bfregs > req.total_num_bfregs - 1)
1144e126ba97SEli Cohen 		return ERR_PTR(-EINVAL);
1145e126ba97SEli Cohen 
1146938fe83cSSaeed Mahameed 	resp.qp_tab_size = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp);
11472cc6ad5fSNoa Osherovich 	if (mlx5_core_is_pf(dev->mdev) && MLX5_CAP_GEN(dev->mdev, bf))
1148938fe83cSSaeed Mahameed 		resp.bf_reg_size = 1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size);
1149b47bd6eaSDaniel Jurgens 	resp.cache_line_size = cache_line_size();
1150938fe83cSSaeed Mahameed 	resp.max_sq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq);
1151938fe83cSSaeed Mahameed 	resp.max_rq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq);
1152938fe83cSSaeed Mahameed 	resp.max_send_wqebb = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1153938fe83cSSaeed Mahameed 	resp.max_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1154938fe83cSSaeed Mahameed 	resp.max_srq_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz);
1155f72300c5SHaggai Abramovsky 	resp.cqe_version = min_t(__u8,
1156f72300c5SHaggai Abramovsky 				 (__u8)MLX5_CAP_GEN(dev->mdev, cqe_version),
1157f72300c5SHaggai Abramovsky 				 req.max_cqe_version);
115830aa60b3SEli Cohen 	resp.log_uar_size = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
115930aa60b3SEli Cohen 				MLX5_ADAPTER_PAGE_SHIFT : PAGE_SHIFT;
116030aa60b3SEli Cohen 	resp.num_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
116130aa60b3SEli Cohen 					MLX5_CAP_GEN(dev->mdev, num_of_uars_per_page) : 1;
1162b368d7cbSMatan Barak 	resp.response_length = min(offsetof(typeof(resp), response_length) +
1163b368d7cbSMatan Barak 				   sizeof(resp.response_length), udata->outlen);
1164e126ba97SEli Cohen 
1165e126ba97SEli Cohen 	context = kzalloc(sizeof(*context), GFP_KERNEL);
1166e126ba97SEli Cohen 	if (!context)
1167e126ba97SEli Cohen 		return ERR_PTR(-ENOMEM);
1168e126ba97SEli Cohen 
116930aa60b3SEli Cohen 	lib_uar_4k = req.lib_caps & MLX5_LIB_CAP_4K_UAR;
11702f5ff264SEli Cohen 	bfregi = &context->bfregi;
1171b037c29aSEli Cohen 
1172b037c29aSEli Cohen 	/* updates req->total_num_bfregs */
1173b037c29aSEli Cohen 	err = calc_total_bfregs(dev, lib_uar_4k, &req, &bfregi->num_sys_pages);
1174b037c29aSEli Cohen 	if (err)
1175b037c29aSEli Cohen 		goto out_ctx;
1176b037c29aSEli Cohen 
11772f5ff264SEli Cohen 	mutex_init(&bfregi->lock);
1178b037c29aSEli Cohen 	bfregi->lib_uar_4k = lib_uar_4k;
1179b037c29aSEli Cohen 	bfregi->count = kcalloc(req.total_num_bfregs, sizeof(*bfregi->count),
1180b037c29aSEli Cohen 				GFP_KERNEL);
1181b037c29aSEli Cohen 	if (!bfregi->count) {
1182e126ba97SEli Cohen 		err = -ENOMEM;
1183e126ba97SEli Cohen 		goto out_ctx;
1184e126ba97SEli Cohen 	}
1185e126ba97SEli Cohen 
1186b037c29aSEli Cohen 	bfregi->sys_pages = kcalloc(bfregi->num_sys_pages,
1187b037c29aSEli Cohen 				    sizeof(*bfregi->sys_pages),
1188e126ba97SEli Cohen 				    GFP_KERNEL);
1189b037c29aSEli Cohen 	if (!bfregi->sys_pages) {
1190e126ba97SEli Cohen 		err = -ENOMEM;
1191e126ba97SEli Cohen 		goto out_count;
1192e126ba97SEli Cohen 	}
1193e126ba97SEli Cohen 
1194b037c29aSEli Cohen 	err = allocate_uars(dev, context);
1195b037c29aSEli Cohen 	if (err)
1196b037c29aSEli Cohen 		goto out_sys_pages;
1197b037c29aSEli Cohen 
1198b4cfe447SHaggai Eran #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
1199b4cfe447SHaggai Eran 	context->ibucontext.invalidate_range = &mlx5_ib_invalidate_range;
1200b4cfe447SHaggai Eran #endif
1201b4cfe447SHaggai Eran 
12027d0cc6edSArtemy Kovalyov 	context->upd_xlt_page = __get_free_page(GFP_KERNEL);
12037d0cc6edSArtemy Kovalyov 	if (!context->upd_xlt_page) {
12047d0cc6edSArtemy Kovalyov 		err = -ENOMEM;
12057d0cc6edSArtemy Kovalyov 		goto out_uars;
12067d0cc6edSArtemy Kovalyov 	}
12077d0cc6edSArtemy Kovalyov 	mutex_init(&context->upd_xlt_page_mutex);
12087d0cc6edSArtemy Kovalyov 
1209146d2f1aSmajd@mellanox.com 	if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain)) {
1210146d2f1aSmajd@mellanox.com 		err = mlx5_core_alloc_transport_domain(dev->mdev,
1211146d2f1aSmajd@mellanox.com 						       &context->tdn);
1212146d2f1aSmajd@mellanox.com 		if (err)
12137d0cc6edSArtemy Kovalyov 			goto out_page;
1214146d2f1aSmajd@mellanox.com 	}
1215146d2f1aSmajd@mellanox.com 
12167c2344c3SMaor Gottlieb 	INIT_LIST_HEAD(&context->vma_private_list);
1217e126ba97SEli Cohen 	INIT_LIST_HEAD(&context->db_page_list);
1218e126ba97SEli Cohen 	mutex_init(&context->db_page_mutex);
1219e126ba97SEli Cohen 
12202f5ff264SEli Cohen 	resp.tot_bfregs = req.total_num_bfregs;
1221938fe83cSSaeed Mahameed 	resp.num_ports = MLX5_CAP_GEN(dev->mdev, num_ports);
1222b368d7cbSMatan Barak 
1223f72300c5SHaggai Abramovsky 	if (field_avail(typeof(resp), cqe_version, udata->outlen))
1224f72300c5SHaggai Abramovsky 		resp.response_length += sizeof(resp.cqe_version);
1225b368d7cbSMatan Barak 
1226402ca536SBodong Wang 	if (field_avail(typeof(resp), cmds_supp_uhw, udata->outlen)) {
12276ad279c5SMoni Shoua 		resp.cmds_supp_uhw |= MLX5_USER_CMDS_SUPP_UHW_QUERY_DEVICE |
12286ad279c5SMoni Shoua 				      MLX5_USER_CMDS_SUPP_UHW_CREATE_AH;
1229402ca536SBodong Wang 		resp.response_length += sizeof(resp.cmds_supp_uhw);
1230402ca536SBodong Wang 	}
1231402ca536SBodong Wang 
1232bc5c6eedSNoa Osherovich 	/*
1233bc5c6eedSNoa Osherovich 	 * We don't want to expose information from the PCI bar that is located
1234bc5c6eedSNoa Osherovich 	 * after 4096 bytes, so if the arch only supports larger pages, let's
1235bc5c6eedSNoa Osherovich 	 * pretend we don't support reading the HCA's core clock. This is also
1236bc5c6eedSNoa Osherovich 	 * forced by mmap function.
1237bc5c6eedSNoa Osherovich 	 */
1238de8d6e02SEli Cohen 	if (field_avail(typeof(resp), hca_core_clock_offset, udata->outlen)) {
1239de8d6e02SEli Cohen 		if (PAGE_SIZE <= 4096) {
1240b368d7cbSMatan Barak 			resp.comp_mask |=
1241b368d7cbSMatan Barak 				MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_CORE_CLOCK_OFFSET;
1242b368d7cbSMatan Barak 			resp.hca_core_clock_offset =
1243de8d6e02SEli Cohen 				offsetof(struct mlx5_init_seg, internal_timer_h) % PAGE_SIZE;
1244de8d6e02SEli Cohen 		}
1245f72300c5SHaggai Abramovsky 		resp.response_length += sizeof(resp.hca_core_clock_offset) +
1246402ca536SBodong Wang 					sizeof(resp.reserved2);
1247b368d7cbSMatan Barak 	}
1248b368d7cbSMatan Barak 
124930aa60b3SEli Cohen 	if (field_avail(typeof(resp), log_uar_size, udata->outlen))
125030aa60b3SEli Cohen 		resp.response_length += sizeof(resp.log_uar_size);
125130aa60b3SEli Cohen 
125230aa60b3SEli Cohen 	if (field_avail(typeof(resp), num_uars_per_page, udata->outlen))
125330aa60b3SEli Cohen 		resp.response_length += sizeof(resp.num_uars_per_page);
125430aa60b3SEli Cohen 
1255b368d7cbSMatan Barak 	err = ib_copy_to_udata(udata, &resp, resp.response_length);
1256e126ba97SEli Cohen 	if (err)
1257146d2f1aSmajd@mellanox.com 		goto out_td;
1258e126ba97SEli Cohen 
12592f5ff264SEli Cohen 	bfregi->ver = ver;
12602f5ff264SEli Cohen 	bfregi->num_low_latency_bfregs = req.num_low_latency_bfregs;
1261f72300c5SHaggai Abramovsky 	context->cqe_version = resp.cqe_version;
126230aa60b3SEli Cohen 	context->lib_caps = req.lib_caps;
126330aa60b3SEli Cohen 	print_lib_caps(dev, context->lib_caps);
1264f72300c5SHaggai Abramovsky 
1265e126ba97SEli Cohen 	return &context->ibucontext;
1266e126ba97SEli Cohen 
1267146d2f1aSmajd@mellanox.com out_td:
1268146d2f1aSmajd@mellanox.com 	if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1269146d2f1aSmajd@mellanox.com 		mlx5_core_dealloc_transport_domain(dev->mdev, context->tdn);
1270146d2f1aSmajd@mellanox.com 
12717d0cc6edSArtemy Kovalyov out_page:
12727d0cc6edSArtemy Kovalyov 	free_page(context->upd_xlt_page);
12737d0cc6edSArtemy Kovalyov 
1274e126ba97SEli Cohen out_uars:
1275b037c29aSEli Cohen 	deallocate_uars(dev, context);
1276b037c29aSEli Cohen 
1277b037c29aSEli Cohen out_sys_pages:
1278b037c29aSEli Cohen 	kfree(bfregi->sys_pages);
1279b037c29aSEli Cohen 
1280e126ba97SEli Cohen out_count:
12812f5ff264SEli Cohen 	kfree(bfregi->count);
1282e126ba97SEli Cohen 
1283e126ba97SEli Cohen out_ctx:
1284e126ba97SEli Cohen 	kfree(context);
1285b037c29aSEli Cohen 
1286e126ba97SEli Cohen 	return ERR_PTR(err);
1287e126ba97SEli Cohen }
1288e126ba97SEli Cohen 
1289e126ba97SEli Cohen static int mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
1290e126ba97SEli Cohen {
1291e126ba97SEli Cohen 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1292e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
1293b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1294e126ba97SEli Cohen 
1295b037c29aSEli Cohen 	bfregi = &context->bfregi;
1296146d2f1aSmajd@mellanox.com 	if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1297146d2f1aSmajd@mellanox.com 		mlx5_core_dealloc_transport_domain(dev->mdev, context->tdn);
1298146d2f1aSmajd@mellanox.com 
12997d0cc6edSArtemy Kovalyov 	free_page(context->upd_xlt_page);
1300b037c29aSEli Cohen 	deallocate_uars(dev, context);
1301b037c29aSEli Cohen 	kfree(bfregi->sys_pages);
13022f5ff264SEli Cohen 	kfree(bfregi->count);
1303e126ba97SEli Cohen 	kfree(context);
1304e126ba97SEli Cohen 
1305e126ba97SEli Cohen 	return 0;
1306e126ba97SEli Cohen }
1307e126ba97SEli Cohen 
1308b037c29aSEli Cohen static phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev,
1309b037c29aSEli Cohen 				 struct mlx5_bfreg_info *bfregi,
1310b037c29aSEli Cohen 				 int idx)
1311e126ba97SEli Cohen {
1312b037c29aSEli Cohen 	int fw_uars_per_page;
1313b037c29aSEli Cohen 
1314b037c29aSEli Cohen 	fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ? MLX5_UARS_IN_PAGE : 1;
1315b037c29aSEli Cohen 
1316b037c29aSEli Cohen 	return (pci_resource_start(dev->mdev->pdev, 0) >> PAGE_SHIFT) +
1317b037c29aSEli Cohen 			bfregi->sys_pages[idx] / fw_uars_per_page;
1318e126ba97SEli Cohen }
1319e126ba97SEli Cohen 
1320e126ba97SEli Cohen static int get_command(unsigned long offset)
1321e126ba97SEli Cohen {
1322e126ba97SEli Cohen 	return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK;
1323e126ba97SEli Cohen }
1324e126ba97SEli Cohen 
1325e126ba97SEli Cohen static int get_arg(unsigned long offset)
1326e126ba97SEli Cohen {
1327e126ba97SEli Cohen 	return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1);
1328e126ba97SEli Cohen }
1329e126ba97SEli Cohen 
1330e126ba97SEli Cohen static int get_index(unsigned long offset)
1331e126ba97SEli Cohen {
1332e126ba97SEli Cohen 	return get_arg(offset);
1333e126ba97SEli Cohen }
1334e126ba97SEli Cohen 
13357c2344c3SMaor Gottlieb static void  mlx5_ib_vma_open(struct vm_area_struct *area)
13367c2344c3SMaor Gottlieb {
13377c2344c3SMaor Gottlieb 	/* vma_open is called when a new VMA is created on top of our VMA.  This
13387c2344c3SMaor Gottlieb 	 * is done through either mremap flow or split_vma (usually due to
13397c2344c3SMaor Gottlieb 	 * mlock, madvise, munmap, etc.) We do not support a clone of the VMA,
13407c2344c3SMaor Gottlieb 	 * as this VMA is strongly hardware related.  Therefore we set the
13417c2344c3SMaor Gottlieb 	 * vm_ops of the newly created/cloned VMA to NULL, to prevent it from
13427c2344c3SMaor Gottlieb 	 * calling us again and trying to do incorrect actions.  We assume that
13437c2344c3SMaor Gottlieb 	 * the original VMA size is exactly a single page, and therefore all
13447c2344c3SMaor Gottlieb 	 * "splitting" operation will not happen to it.
13457c2344c3SMaor Gottlieb 	 */
13467c2344c3SMaor Gottlieb 	area->vm_ops = NULL;
13477c2344c3SMaor Gottlieb }
13487c2344c3SMaor Gottlieb 
13497c2344c3SMaor Gottlieb static void  mlx5_ib_vma_close(struct vm_area_struct *area)
13507c2344c3SMaor Gottlieb {
13517c2344c3SMaor Gottlieb 	struct mlx5_ib_vma_private_data *mlx5_ib_vma_priv_data;
13527c2344c3SMaor Gottlieb 
13537c2344c3SMaor Gottlieb 	/* It's guaranteed that all VMAs opened on a FD are closed before the
13547c2344c3SMaor Gottlieb 	 * file itself is closed, therefore no sync is needed with the regular
13557c2344c3SMaor Gottlieb 	 * closing flow. (e.g. mlx5 ib_dealloc_ucontext)
13567c2344c3SMaor Gottlieb 	 * However need a sync with accessing the vma as part of
13577c2344c3SMaor Gottlieb 	 * mlx5_ib_disassociate_ucontext.
13587c2344c3SMaor Gottlieb 	 * The close operation is usually called under mm->mmap_sem except when
13597c2344c3SMaor Gottlieb 	 * process is exiting.
13607c2344c3SMaor Gottlieb 	 * The exiting case is handled explicitly as part of
13617c2344c3SMaor Gottlieb 	 * mlx5_ib_disassociate_ucontext.
13627c2344c3SMaor Gottlieb 	 */
13637c2344c3SMaor Gottlieb 	mlx5_ib_vma_priv_data = (struct mlx5_ib_vma_private_data *)area->vm_private_data;
13647c2344c3SMaor Gottlieb 
13657c2344c3SMaor Gottlieb 	/* setting the vma context pointer to null in the mlx5_ib driver's
13667c2344c3SMaor Gottlieb 	 * private data, to protect a race condition in
13677c2344c3SMaor Gottlieb 	 * mlx5_ib_disassociate_ucontext().
13687c2344c3SMaor Gottlieb 	 */
13697c2344c3SMaor Gottlieb 	mlx5_ib_vma_priv_data->vma = NULL;
13707c2344c3SMaor Gottlieb 	list_del(&mlx5_ib_vma_priv_data->list);
13717c2344c3SMaor Gottlieb 	kfree(mlx5_ib_vma_priv_data);
13727c2344c3SMaor Gottlieb }
13737c2344c3SMaor Gottlieb 
13747c2344c3SMaor Gottlieb static const struct vm_operations_struct mlx5_ib_vm_ops = {
13757c2344c3SMaor Gottlieb 	.open = mlx5_ib_vma_open,
13767c2344c3SMaor Gottlieb 	.close = mlx5_ib_vma_close
13777c2344c3SMaor Gottlieb };
13787c2344c3SMaor Gottlieb 
13797c2344c3SMaor Gottlieb static int mlx5_ib_set_vma_data(struct vm_area_struct *vma,
13807c2344c3SMaor Gottlieb 				struct mlx5_ib_ucontext *ctx)
13817c2344c3SMaor Gottlieb {
13827c2344c3SMaor Gottlieb 	struct mlx5_ib_vma_private_data *vma_prv;
13837c2344c3SMaor Gottlieb 	struct list_head *vma_head = &ctx->vma_private_list;
13847c2344c3SMaor Gottlieb 
13857c2344c3SMaor Gottlieb 	vma_prv = kzalloc(sizeof(*vma_prv), GFP_KERNEL);
13867c2344c3SMaor Gottlieb 	if (!vma_prv)
13877c2344c3SMaor Gottlieb 		return -ENOMEM;
13887c2344c3SMaor Gottlieb 
13897c2344c3SMaor Gottlieb 	vma_prv->vma = vma;
13907c2344c3SMaor Gottlieb 	vma->vm_private_data = vma_prv;
13917c2344c3SMaor Gottlieb 	vma->vm_ops =  &mlx5_ib_vm_ops;
13927c2344c3SMaor Gottlieb 
13937c2344c3SMaor Gottlieb 	list_add(&vma_prv->list, vma_head);
13947c2344c3SMaor Gottlieb 
13957c2344c3SMaor Gottlieb 	return 0;
13967c2344c3SMaor Gottlieb }
13977c2344c3SMaor Gottlieb 
13987c2344c3SMaor Gottlieb static void mlx5_ib_disassociate_ucontext(struct ib_ucontext *ibcontext)
13997c2344c3SMaor Gottlieb {
14007c2344c3SMaor Gottlieb 	int ret;
14017c2344c3SMaor Gottlieb 	struct vm_area_struct *vma;
14027c2344c3SMaor Gottlieb 	struct mlx5_ib_vma_private_data *vma_private, *n;
14037c2344c3SMaor Gottlieb 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
14047c2344c3SMaor Gottlieb 	struct task_struct *owning_process  = NULL;
14057c2344c3SMaor Gottlieb 	struct mm_struct   *owning_mm       = NULL;
14067c2344c3SMaor Gottlieb 
14077c2344c3SMaor Gottlieb 	owning_process = get_pid_task(ibcontext->tgid, PIDTYPE_PID);
14087c2344c3SMaor Gottlieb 	if (!owning_process)
14097c2344c3SMaor Gottlieb 		return;
14107c2344c3SMaor Gottlieb 
14117c2344c3SMaor Gottlieb 	owning_mm = get_task_mm(owning_process);
14127c2344c3SMaor Gottlieb 	if (!owning_mm) {
14137c2344c3SMaor Gottlieb 		pr_info("no mm, disassociate ucontext is pending task termination\n");
14147c2344c3SMaor Gottlieb 		while (1) {
14157c2344c3SMaor Gottlieb 			put_task_struct(owning_process);
14167c2344c3SMaor Gottlieb 			usleep_range(1000, 2000);
14177c2344c3SMaor Gottlieb 			owning_process = get_pid_task(ibcontext->tgid,
14187c2344c3SMaor Gottlieb 						      PIDTYPE_PID);
14197c2344c3SMaor Gottlieb 			if (!owning_process ||
14207c2344c3SMaor Gottlieb 			    owning_process->state == TASK_DEAD) {
14217c2344c3SMaor Gottlieb 				pr_info("disassociate ucontext done, task was terminated\n");
14227c2344c3SMaor Gottlieb 				/* in case task was dead need to release the
14237c2344c3SMaor Gottlieb 				 * task struct.
14247c2344c3SMaor Gottlieb 				 */
14257c2344c3SMaor Gottlieb 				if (owning_process)
14267c2344c3SMaor Gottlieb 					put_task_struct(owning_process);
14277c2344c3SMaor Gottlieb 				return;
14287c2344c3SMaor Gottlieb 			}
14297c2344c3SMaor Gottlieb 		}
14307c2344c3SMaor Gottlieb 	}
14317c2344c3SMaor Gottlieb 
14327c2344c3SMaor Gottlieb 	/* need to protect from a race on closing the vma as part of
14337c2344c3SMaor Gottlieb 	 * mlx5_ib_vma_close.
14347c2344c3SMaor Gottlieb 	 */
14357c2344c3SMaor Gottlieb 	down_read(&owning_mm->mmap_sem);
14367c2344c3SMaor Gottlieb 	list_for_each_entry_safe(vma_private, n, &context->vma_private_list,
14377c2344c3SMaor Gottlieb 				 list) {
14387c2344c3SMaor Gottlieb 		vma = vma_private->vma;
14397c2344c3SMaor Gottlieb 		ret = zap_vma_ptes(vma, vma->vm_start,
14407c2344c3SMaor Gottlieb 				   PAGE_SIZE);
14417c2344c3SMaor Gottlieb 		WARN_ONCE(ret, "%s: zap_vma_ptes failed", __func__);
14427c2344c3SMaor Gottlieb 		/* context going to be destroyed, should
14437c2344c3SMaor Gottlieb 		 * not access ops any more.
14447c2344c3SMaor Gottlieb 		 */
14457c2344c3SMaor Gottlieb 		vma->vm_ops = NULL;
14467c2344c3SMaor Gottlieb 		list_del(&vma_private->list);
14477c2344c3SMaor Gottlieb 		kfree(vma_private);
14487c2344c3SMaor Gottlieb 	}
14497c2344c3SMaor Gottlieb 	up_read(&owning_mm->mmap_sem);
14507c2344c3SMaor Gottlieb 	mmput(owning_mm);
14517c2344c3SMaor Gottlieb 	put_task_struct(owning_process);
14527c2344c3SMaor Gottlieb }
14537c2344c3SMaor Gottlieb 
145437aa5c36SGuy Levi static inline char *mmap_cmd2str(enum mlx5_ib_mmap_cmd cmd)
1455e126ba97SEli Cohen {
145637aa5c36SGuy Levi 	switch (cmd) {
145737aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
145837aa5c36SGuy Levi 		return "WC";
1459e126ba97SEli Cohen 	case MLX5_IB_MMAP_REGULAR_PAGE:
146037aa5c36SGuy Levi 		return "best effort WC";
146137aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
146237aa5c36SGuy Levi 		return "NC";
146337aa5c36SGuy Levi 	default:
146437aa5c36SGuy Levi 		return NULL;
146537aa5c36SGuy Levi 	}
146637aa5c36SGuy Levi }
146737aa5c36SGuy Levi 
146837aa5c36SGuy Levi static int uar_mmap(struct mlx5_ib_dev *dev, enum mlx5_ib_mmap_cmd cmd,
14697c2344c3SMaor Gottlieb 		    struct vm_area_struct *vma,
14707c2344c3SMaor Gottlieb 		    struct mlx5_ib_ucontext *context)
147137aa5c36SGuy Levi {
14722f5ff264SEli Cohen 	struct mlx5_bfreg_info *bfregi = &context->bfregi;
147337aa5c36SGuy Levi 	int err;
147437aa5c36SGuy Levi 	unsigned long idx;
147537aa5c36SGuy Levi 	phys_addr_t pfn, pa;
147637aa5c36SGuy Levi 	pgprot_t prot;
1477b037c29aSEli Cohen 	int uars_per_page;
1478b037c29aSEli Cohen 
1479b037c29aSEli Cohen 	if (vma->vm_end - vma->vm_start != PAGE_SIZE)
1480b037c29aSEli Cohen 		return -EINVAL;
1481b037c29aSEli Cohen 
1482b037c29aSEli Cohen 	uars_per_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k);
1483b037c29aSEli Cohen 	idx = get_index(vma->vm_pgoff);
1484b037c29aSEli Cohen 	if (idx % uars_per_page ||
1485b037c29aSEli Cohen 	    idx * uars_per_page >= bfregi->num_sys_pages) {
1486b037c29aSEli Cohen 		mlx5_ib_warn(dev, "invalid uar index %lu\n", idx);
1487b037c29aSEli Cohen 		return -EINVAL;
1488b037c29aSEli Cohen 	}
148937aa5c36SGuy Levi 
149037aa5c36SGuy Levi 	switch (cmd) {
149137aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
149237aa5c36SGuy Levi /* Some architectures don't support WC memory */
149337aa5c36SGuy Levi #if defined(CONFIG_X86)
149437aa5c36SGuy Levi 		if (!pat_enabled())
149537aa5c36SGuy Levi 			return -EPERM;
149637aa5c36SGuy Levi #elif !(defined(CONFIG_PPC) || (defined(CONFIG_ARM) && defined(CONFIG_MMU)))
149737aa5c36SGuy Levi 			return -EPERM;
149837aa5c36SGuy Levi #endif
149937aa5c36SGuy Levi 	/* fall through */
150037aa5c36SGuy Levi 	case MLX5_IB_MMAP_REGULAR_PAGE:
150137aa5c36SGuy Levi 		/* For MLX5_IB_MMAP_REGULAR_PAGE do the best effort to get WC */
150237aa5c36SGuy Levi 		prot = pgprot_writecombine(vma->vm_page_prot);
150337aa5c36SGuy Levi 		break;
150437aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
150537aa5c36SGuy Levi 		prot = pgprot_noncached(vma->vm_page_prot);
150637aa5c36SGuy Levi 		break;
150737aa5c36SGuy Levi 	default:
150837aa5c36SGuy Levi 		return -EINVAL;
150937aa5c36SGuy Levi 	}
151037aa5c36SGuy Levi 
1511b037c29aSEli Cohen 	pfn = uar_index2pfn(dev, bfregi, idx);
151237aa5c36SGuy Levi 	mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn %pa\n", idx, &pfn);
1513e126ba97SEli Cohen 
151437aa5c36SGuy Levi 	vma->vm_page_prot = prot;
151537aa5c36SGuy Levi 	err = io_remap_pfn_range(vma, vma->vm_start, pfn,
151637aa5c36SGuy Levi 				 PAGE_SIZE, vma->vm_page_prot);
151737aa5c36SGuy Levi 	if (err) {
151837aa5c36SGuy Levi 		mlx5_ib_err(dev, "io_remap_pfn_range failed with error=%d, vm_start=0x%lx, pfn=%pa, mmap_cmd=%s\n",
151937aa5c36SGuy Levi 			    err, vma->vm_start, &pfn, mmap_cmd2str(cmd));
1520e126ba97SEli Cohen 		return -EAGAIN;
152137aa5c36SGuy Levi 	}
1522e126ba97SEli Cohen 
152337aa5c36SGuy Levi 	pa = pfn << PAGE_SHIFT;
152437aa5c36SGuy Levi 	mlx5_ib_dbg(dev, "mapped %s at 0x%lx, PA %pa\n", mmap_cmd2str(cmd),
152537aa5c36SGuy Levi 		    vma->vm_start, &pa);
152637aa5c36SGuy Levi 
15277c2344c3SMaor Gottlieb 	return mlx5_ib_set_vma_data(vma, context);
152837aa5c36SGuy Levi }
152937aa5c36SGuy Levi 
153037aa5c36SGuy Levi static int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma)
153137aa5c36SGuy Levi {
153237aa5c36SGuy Levi 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
153337aa5c36SGuy Levi 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
153437aa5c36SGuy Levi 	unsigned long command;
153537aa5c36SGuy Levi 	phys_addr_t pfn;
153637aa5c36SGuy Levi 
153737aa5c36SGuy Levi 	command = get_command(vma->vm_pgoff);
153837aa5c36SGuy Levi 	switch (command) {
153937aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
154037aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
154137aa5c36SGuy Levi 	case MLX5_IB_MMAP_REGULAR_PAGE:
15427c2344c3SMaor Gottlieb 		return uar_mmap(dev, command, vma, context);
1543e126ba97SEli Cohen 
1544e126ba97SEli Cohen 	case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES:
1545e126ba97SEli Cohen 		return -ENOSYS;
1546e126ba97SEli Cohen 
1547d69e3bcfSMatan Barak 	case MLX5_IB_MMAP_CORE_CLOCK:
1548d69e3bcfSMatan Barak 		if (vma->vm_end - vma->vm_start != PAGE_SIZE)
1549d69e3bcfSMatan Barak 			return -EINVAL;
1550d69e3bcfSMatan Barak 
15516cbac1e4SMatan Barak 		if (vma->vm_flags & VM_WRITE)
1552d69e3bcfSMatan Barak 			return -EPERM;
1553d69e3bcfSMatan Barak 
1554d69e3bcfSMatan Barak 		/* Don't expose to user-space information it shouldn't have */
1555d69e3bcfSMatan Barak 		if (PAGE_SIZE > 4096)
1556d69e3bcfSMatan Barak 			return -EOPNOTSUPP;
1557d69e3bcfSMatan Barak 
1558d69e3bcfSMatan Barak 		vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1559d69e3bcfSMatan Barak 		pfn = (dev->mdev->iseg_base +
1560d69e3bcfSMatan Barak 		       offsetof(struct mlx5_init_seg, internal_timer_h)) >>
1561d69e3bcfSMatan Barak 			PAGE_SHIFT;
1562d69e3bcfSMatan Barak 		if (io_remap_pfn_range(vma, vma->vm_start, pfn,
1563d69e3bcfSMatan Barak 				       PAGE_SIZE, vma->vm_page_prot))
1564d69e3bcfSMatan Barak 			return -EAGAIN;
1565d69e3bcfSMatan Barak 
1566d69e3bcfSMatan Barak 		mlx5_ib_dbg(dev, "mapped internal timer at 0x%lx, PA 0x%llx\n",
1567d69e3bcfSMatan Barak 			    vma->vm_start,
1568d69e3bcfSMatan Barak 			    (unsigned long long)pfn << PAGE_SHIFT);
1569d69e3bcfSMatan Barak 		break;
1570d69e3bcfSMatan Barak 
1571e126ba97SEli Cohen 	default:
1572e126ba97SEli Cohen 		return -EINVAL;
1573e126ba97SEli Cohen 	}
1574e126ba97SEli Cohen 
1575e126ba97SEli Cohen 	return 0;
1576e126ba97SEli Cohen }
1577e126ba97SEli Cohen 
1578e126ba97SEli Cohen static struct ib_pd *mlx5_ib_alloc_pd(struct ib_device *ibdev,
1579e126ba97SEli Cohen 				      struct ib_ucontext *context,
1580e126ba97SEli Cohen 				      struct ib_udata *udata)
1581e126ba97SEli Cohen {
1582e126ba97SEli Cohen 	struct mlx5_ib_alloc_pd_resp resp;
1583e126ba97SEli Cohen 	struct mlx5_ib_pd *pd;
1584e126ba97SEli Cohen 	int err;
1585e126ba97SEli Cohen 
1586e126ba97SEli Cohen 	pd = kmalloc(sizeof(*pd), GFP_KERNEL);
1587e126ba97SEli Cohen 	if (!pd)
1588e126ba97SEli Cohen 		return ERR_PTR(-ENOMEM);
1589e126ba97SEli Cohen 
15909603b61dSJack Morgenstein 	err = mlx5_core_alloc_pd(to_mdev(ibdev)->mdev, &pd->pdn);
1591e126ba97SEli Cohen 	if (err) {
1592e126ba97SEli Cohen 		kfree(pd);
1593e126ba97SEli Cohen 		return ERR_PTR(err);
1594e126ba97SEli Cohen 	}
1595e126ba97SEli Cohen 
1596e126ba97SEli Cohen 	if (context) {
1597e126ba97SEli Cohen 		resp.pdn = pd->pdn;
1598e126ba97SEli Cohen 		if (ib_copy_to_udata(udata, &resp, sizeof(resp))) {
15999603b61dSJack Morgenstein 			mlx5_core_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn);
1600e126ba97SEli Cohen 			kfree(pd);
1601e126ba97SEli Cohen 			return ERR_PTR(-EFAULT);
1602e126ba97SEli Cohen 		}
1603e126ba97SEli Cohen 	}
1604e126ba97SEli Cohen 
1605e126ba97SEli Cohen 	return &pd->ibpd;
1606e126ba97SEli Cohen }
1607e126ba97SEli Cohen 
1608e126ba97SEli Cohen static int mlx5_ib_dealloc_pd(struct ib_pd *pd)
1609e126ba97SEli Cohen {
1610e126ba97SEli Cohen 	struct mlx5_ib_dev *mdev = to_mdev(pd->device);
1611e126ba97SEli Cohen 	struct mlx5_ib_pd *mpd = to_mpd(pd);
1612e126ba97SEli Cohen 
16139603b61dSJack Morgenstein 	mlx5_core_dealloc_pd(mdev->mdev, mpd->pdn);
1614e126ba97SEli Cohen 	kfree(mpd);
1615e126ba97SEli Cohen 
1616e126ba97SEli Cohen 	return 0;
1617e126ba97SEli Cohen }
1618e126ba97SEli Cohen 
1619466fa6d2SMaor Gottlieb enum {
1620466fa6d2SMaor Gottlieb 	MATCH_CRITERIA_ENABLE_OUTER_BIT,
1621466fa6d2SMaor Gottlieb 	MATCH_CRITERIA_ENABLE_MISC_BIT,
1622466fa6d2SMaor Gottlieb 	MATCH_CRITERIA_ENABLE_INNER_BIT
1623466fa6d2SMaor Gottlieb };
1624038d2ef8SMaor Gottlieb 
1625466fa6d2SMaor Gottlieb #define HEADER_IS_ZERO(match_criteria, headers)			           \
1626466fa6d2SMaor Gottlieb 	!(memchr_inv(MLX5_ADDR_OF(fte_match_param, match_criteria, headers), \
1627466fa6d2SMaor Gottlieb 		    0, MLX5_FLD_SZ_BYTES(fte_match_param, headers)))       \
1628466fa6d2SMaor Gottlieb 
1629466fa6d2SMaor Gottlieb static u8 get_match_criteria_enable(u32 *match_criteria)
1630466fa6d2SMaor Gottlieb {
1631466fa6d2SMaor Gottlieb 	u8 match_criteria_enable;
1632466fa6d2SMaor Gottlieb 
1633466fa6d2SMaor Gottlieb 	match_criteria_enable =
1634466fa6d2SMaor Gottlieb 		(!HEADER_IS_ZERO(match_criteria, outer_headers)) <<
1635466fa6d2SMaor Gottlieb 		MATCH_CRITERIA_ENABLE_OUTER_BIT;
1636466fa6d2SMaor Gottlieb 	match_criteria_enable |=
1637466fa6d2SMaor Gottlieb 		(!HEADER_IS_ZERO(match_criteria, misc_parameters)) <<
1638466fa6d2SMaor Gottlieb 		MATCH_CRITERIA_ENABLE_MISC_BIT;
1639466fa6d2SMaor Gottlieb 	match_criteria_enable |=
1640466fa6d2SMaor Gottlieb 		(!HEADER_IS_ZERO(match_criteria, inner_headers)) <<
1641466fa6d2SMaor Gottlieb 		MATCH_CRITERIA_ENABLE_INNER_BIT;
1642466fa6d2SMaor Gottlieb 
1643466fa6d2SMaor Gottlieb 	return match_criteria_enable;
1644038d2ef8SMaor Gottlieb }
1645038d2ef8SMaor Gottlieb 
1646ca0d4753SMaor Gottlieb static void set_proto(void *outer_c, void *outer_v, u8 mask, u8 val)
1647ca0d4753SMaor Gottlieb {
1648ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_protocol, mask);
1649ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_protocol, val);
1650ca0d4753SMaor Gottlieb }
1651ca0d4753SMaor Gottlieb 
16522d1e697eSMoses Reuben static void set_flow_label(void *misc_c, void *misc_v, u8 mask, u8 val,
16532d1e697eSMoses Reuben 			   bool inner)
16542d1e697eSMoses Reuben {
16552d1e697eSMoses Reuben 	if (inner) {
16562d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
16572d1e697eSMoses Reuben 			 misc_c, inner_ipv6_flow_label, mask);
16582d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
16592d1e697eSMoses Reuben 			 misc_v, inner_ipv6_flow_label, val);
16602d1e697eSMoses Reuben 	} else {
16612d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
16622d1e697eSMoses Reuben 			 misc_c, outer_ipv6_flow_label, mask);
16632d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
16642d1e697eSMoses Reuben 			 misc_v, outer_ipv6_flow_label, val);
16652d1e697eSMoses Reuben 	}
16662d1e697eSMoses Reuben }
16672d1e697eSMoses Reuben 
1668ca0d4753SMaor Gottlieb static void set_tos(void *outer_c, void *outer_v, u8 mask, u8 val)
1669ca0d4753SMaor Gottlieb {
1670ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_ecn, mask);
1671ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_ecn, val);
1672ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_dscp, mask >> 2);
1673ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_dscp, val >> 2);
1674ca0d4753SMaor Gottlieb }
1675ca0d4753SMaor Gottlieb 
1676c47ac6aeSMaor Gottlieb #define LAST_ETH_FIELD vlan_tag
1677c47ac6aeSMaor Gottlieb #define LAST_IB_FIELD sl
1678ca0d4753SMaor Gottlieb #define LAST_IPV4_FIELD tos
1679466fa6d2SMaor Gottlieb #define LAST_IPV6_FIELD traffic_class
1680c47ac6aeSMaor Gottlieb #define LAST_TCP_UDP_FIELD src_port
1681ffb30d8fSMoses Reuben #define LAST_TUNNEL_FIELD tunnel_id
16822ac693f9SMoses Reuben #define LAST_FLOW_TAG_FIELD tag_id
1683c47ac6aeSMaor Gottlieb 
1684c47ac6aeSMaor Gottlieb /* Field is the last supported field */
1685c47ac6aeSMaor Gottlieb #define FIELDS_NOT_SUPPORTED(filter, field)\
1686c47ac6aeSMaor Gottlieb 	memchr_inv((void *)&filter.field  +\
1687c47ac6aeSMaor Gottlieb 		   sizeof(filter.field), 0,\
1688c47ac6aeSMaor Gottlieb 		   sizeof(filter) -\
1689c47ac6aeSMaor Gottlieb 		   offsetof(typeof(filter), field) -\
1690c47ac6aeSMaor Gottlieb 		   sizeof(filter.field))
1691c47ac6aeSMaor Gottlieb 
1692038d2ef8SMaor Gottlieb static int parse_flow_attr(u32 *match_c, u32 *match_v,
16932ac693f9SMoses Reuben 			   const union ib_flow_spec *ib_spec, u32 *tag_id)
1694038d2ef8SMaor Gottlieb {
1695466fa6d2SMaor Gottlieb 	void *misc_params_c = MLX5_ADDR_OF(fte_match_param, match_c,
1696466fa6d2SMaor Gottlieb 					   misc_parameters);
1697466fa6d2SMaor Gottlieb 	void *misc_params_v = MLX5_ADDR_OF(fte_match_param, match_v,
1698466fa6d2SMaor Gottlieb 					   misc_parameters);
16992d1e697eSMoses Reuben 	void *headers_c;
17002d1e697eSMoses Reuben 	void *headers_v;
1701466fa6d2SMaor Gottlieb 
17022d1e697eSMoses Reuben 	if (ib_spec->type & IB_FLOW_SPEC_INNER) {
17032d1e697eSMoses Reuben 		headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
17042d1e697eSMoses Reuben 					 inner_headers);
17052d1e697eSMoses Reuben 		headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
17062d1e697eSMoses Reuben 					 inner_headers);
17072d1e697eSMoses Reuben 	} else {
17082d1e697eSMoses Reuben 		headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
17092d1e697eSMoses Reuben 					 outer_headers);
17102d1e697eSMoses Reuben 		headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
17112d1e697eSMoses Reuben 					 outer_headers);
17122d1e697eSMoses Reuben 	}
17132d1e697eSMoses Reuben 
17142d1e697eSMoses Reuben 	switch (ib_spec->type & ~IB_FLOW_SPEC_INNER) {
1715038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_ETH:
1716c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->eth.mask, LAST_ETH_FIELD))
17171ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
1718038d2ef8SMaor Gottlieb 
17192d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
1720038d2ef8SMaor Gottlieb 					     dmac_47_16),
1721038d2ef8SMaor Gottlieb 				ib_spec->eth.mask.dst_mac);
17222d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
1723038d2ef8SMaor Gottlieb 					     dmac_47_16),
1724038d2ef8SMaor Gottlieb 				ib_spec->eth.val.dst_mac);
1725038d2ef8SMaor Gottlieb 
17262d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
1727ee3da804SMaor Gottlieb 					     smac_47_16),
1728ee3da804SMaor Gottlieb 				ib_spec->eth.mask.src_mac);
17292d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
1730ee3da804SMaor Gottlieb 					     smac_47_16),
1731ee3da804SMaor Gottlieb 				ib_spec->eth.val.src_mac);
1732ee3da804SMaor Gottlieb 
1733038d2ef8SMaor Gottlieb 		if (ib_spec->eth.mask.vlan_tag) {
17342d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1735038d2ef8SMaor Gottlieb 				 vlan_tag, 1);
17362d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1737038d2ef8SMaor Gottlieb 				 vlan_tag, 1);
1738038d2ef8SMaor Gottlieb 
17392d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1740038d2ef8SMaor Gottlieb 				 first_vid, ntohs(ib_spec->eth.mask.vlan_tag));
17412d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1742038d2ef8SMaor Gottlieb 				 first_vid, ntohs(ib_spec->eth.val.vlan_tag));
1743038d2ef8SMaor Gottlieb 
17442d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1745038d2ef8SMaor Gottlieb 				 first_cfi,
1746038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.mask.vlan_tag) >> 12);
17472d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1748038d2ef8SMaor Gottlieb 				 first_cfi,
1749038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.val.vlan_tag) >> 12);
1750038d2ef8SMaor Gottlieb 
17512d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1752038d2ef8SMaor Gottlieb 				 first_prio,
1753038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.mask.vlan_tag) >> 13);
17542d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1755038d2ef8SMaor Gottlieb 				 first_prio,
1756038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.val.vlan_tag) >> 13);
1757038d2ef8SMaor Gottlieb 		}
17582d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1759038d2ef8SMaor Gottlieb 			 ethertype, ntohs(ib_spec->eth.mask.ether_type));
17602d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1761038d2ef8SMaor Gottlieb 			 ethertype, ntohs(ib_spec->eth.val.ether_type));
1762038d2ef8SMaor Gottlieb 		break;
1763038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_IPV4:
1764c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->ipv4.mask, LAST_IPV4_FIELD))
17651ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
1766038d2ef8SMaor Gottlieb 
17672d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1768038d2ef8SMaor Gottlieb 			 ethertype, 0xffff);
17692d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1770038d2ef8SMaor Gottlieb 			 ethertype, ETH_P_IP);
1771038d2ef8SMaor Gottlieb 
17722d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
1773038d2ef8SMaor Gottlieb 				    src_ipv4_src_ipv6.ipv4_layout.ipv4),
1774038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.mask.src_ip,
1775038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.mask.src_ip));
17762d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
1777038d2ef8SMaor Gottlieb 				    src_ipv4_src_ipv6.ipv4_layout.ipv4),
1778038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.val.src_ip,
1779038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.val.src_ip));
17802d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
1781038d2ef8SMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
1782038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.mask.dst_ip,
1783038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.mask.dst_ip));
17842d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
1785038d2ef8SMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
1786038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.val.dst_ip,
1787038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.val.dst_ip));
1788ca0d4753SMaor Gottlieb 
17892d1e697eSMoses Reuben 		set_tos(headers_c, headers_v,
1790ca0d4753SMaor Gottlieb 			ib_spec->ipv4.mask.tos, ib_spec->ipv4.val.tos);
1791ca0d4753SMaor Gottlieb 
17922d1e697eSMoses Reuben 		set_proto(headers_c, headers_v,
1793ca0d4753SMaor Gottlieb 			  ib_spec->ipv4.mask.proto, ib_spec->ipv4.val.proto);
1794038d2ef8SMaor Gottlieb 		break;
1795026bae0cSMaor Gottlieb 	case IB_FLOW_SPEC_IPV6:
1796c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->ipv6.mask, LAST_IPV6_FIELD))
17971ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
1798026bae0cSMaor Gottlieb 
17992d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1800026bae0cSMaor Gottlieb 			 ethertype, 0xffff);
18012d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1802026bae0cSMaor Gottlieb 			 ethertype, ETH_P_IPV6);
1803026bae0cSMaor Gottlieb 
18042d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
1805026bae0cSMaor Gottlieb 				    src_ipv4_src_ipv6.ipv6_layout.ipv6),
1806026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.mask.src_ip,
1807026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.mask.src_ip));
18082d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
1809026bae0cSMaor Gottlieb 				    src_ipv4_src_ipv6.ipv6_layout.ipv6),
1810026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.val.src_ip,
1811026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.val.src_ip));
18122d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
1813026bae0cSMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
1814026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.mask.dst_ip,
1815026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.mask.dst_ip));
18162d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
1817026bae0cSMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
1818026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.val.dst_ip,
1819026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.val.dst_ip));
1820466fa6d2SMaor Gottlieb 
18212d1e697eSMoses Reuben 		set_tos(headers_c, headers_v,
1822466fa6d2SMaor Gottlieb 			ib_spec->ipv6.mask.traffic_class,
1823466fa6d2SMaor Gottlieb 			ib_spec->ipv6.val.traffic_class);
1824466fa6d2SMaor Gottlieb 
18252d1e697eSMoses Reuben 		set_proto(headers_c, headers_v,
1826466fa6d2SMaor Gottlieb 			  ib_spec->ipv6.mask.next_hdr,
1827466fa6d2SMaor Gottlieb 			  ib_spec->ipv6.val.next_hdr);
1828466fa6d2SMaor Gottlieb 
18292d1e697eSMoses Reuben 		set_flow_label(misc_params_c, misc_params_v,
18302d1e697eSMoses Reuben 			       ntohl(ib_spec->ipv6.mask.flow_label),
18312d1e697eSMoses Reuben 			       ntohl(ib_spec->ipv6.val.flow_label),
18322d1e697eSMoses Reuben 			       ib_spec->type & IB_FLOW_SPEC_INNER);
18332d1e697eSMoses Reuben 
1834026bae0cSMaor Gottlieb 		break;
1835038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_TCP:
1836c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
1837c47ac6aeSMaor Gottlieb 					 LAST_TCP_UDP_FIELD))
18381ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
1839038d2ef8SMaor Gottlieb 
18402d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
1841038d2ef8SMaor Gottlieb 			 0xff);
18422d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
1843038d2ef8SMaor Gottlieb 			 IPPROTO_TCP);
1844038d2ef8SMaor Gottlieb 
18452d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_sport,
1846038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.src_port));
18472d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_sport,
1848038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.src_port));
1849038d2ef8SMaor Gottlieb 
18502d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_dport,
1851038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.dst_port));
18522d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_dport,
1853038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.dst_port));
1854038d2ef8SMaor Gottlieb 		break;
1855038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_UDP:
1856c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
1857c47ac6aeSMaor Gottlieb 					 LAST_TCP_UDP_FIELD))
18581ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
1859038d2ef8SMaor Gottlieb 
18602d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
1861038d2ef8SMaor Gottlieb 			 0xff);
18622d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
1863038d2ef8SMaor Gottlieb 			 IPPROTO_UDP);
1864038d2ef8SMaor Gottlieb 
18652d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_sport,
1866038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.src_port));
18672d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_sport,
1868038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.src_port));
1869038d2ef8SMaor Gottlieb 
18702d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_dport,
1871038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.dst_port));
18722d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport,
1873038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.dst_port));
1874038d2ef8SMaor Gottlieb 		break;
1875ffb30d8fSMoses Reuben 	case IB_FLOW_SPEC_VXLAN_TUNNEL:
1876ffb30d8fSMoses Reuben 		if (FIELDS_NOT_SUPPORTED(ib_spec->tunnel.mask,
1877ffb30d8fSMoses Reuben 					 LAST_TUNNEL_FIELD))
18781ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
1879ffb30d8fSMoses Reuben 
1880ffb30d8fSMoses Reuben 		MLX5_SET(fte_match_set_misc, misc_params_c, vxlan_vni,
1881ffb30d8fSMoses Reuben 			 ntohl(ib_spec->tunnel.mask.tunnel_id));
1882ffb30d8fSMoses Reuben 		MLX5_SET(fte_match_set_misc, misc_params_v, vxlan_vni,
1883ffb30d8fSMoses Reuben 			 ntohl(ib_spec->tunnel.val.tunnel_id));
1884ffb30d8fSMoses Reuben 		break;
18852ac693f9SMoses Reuben 	case IB_FLOW_SPEC_ACTION_TAG:
18862ac693f9SMoses Reuben 		if (FIELDS_NOT_SUPPORTED(ib_spec->flow_tag,
18872ac693f9SMoses Reuben 					 LAST_FLOW_TAG_FIELD))
18882ac693f9SMoses Reuben 			return -EOPNOTSUPP;
18892ac693f9SMoses Reuben 		if (ib_spec->flow_tag.tag_id >= BIT(24))
18902ac693f9SMoses Reuben 			return -EINVAL;
18912ac693f9SMoses Reuben 
18922ac693f9SMoses Reuben 		*tag_id = ib_spec->flow_tag.tag_id;
18932ac693f9SMoses Reuben 		break;
1894038d2ef8SMaor Gottlieb 	default:
1895038d2ef8SMaor Gottlieb 		return -EINVAL;
1896038d2ef8SMaor Gottlieb 	}
1897038d2ef8SMaor Gottlieb 
1898038d2ef8SMaor Gottlieb 	return 0;
1899038d2ef8SMaor Gottlieb }
1900038d2ef8SMaor Gottlieb 
1901038d2ef8SMaor Gottlieb /* If a flow could catch both multicast and unicast packets,
1902038d2ef8SMaor Gottlieb  * it won't fall into the multicast flow steering table and this rule
1903038d2ef8SMaor Gottlieb  * could steal other multicast packets.
1904038d2ef8SMaor Gottlieb  */
1905038d2ef8SMaor Gottlieb static bool flow_is_multicast_only(struct ib_flow_attr *ib_attr)
1906038d2ef8SMaor Gottlieb {
1907038d2ef8SMaor Gottlieb 	struct ib_flow_spec_eth *eth_spec;
1908038d2ef8SMaor Gottlieb 
1909038d2ef8SMaor Gottlieb 	if (ib_attr->type != IB_FLOW_ATTR_NORMAL ||
1910038d2ef8SMaor Gottlieb 	    ib_attr->size < sizeof(struct ib_flow_attr) +
1911038d2ef8SMaor Gottlieb 	    sizeof(struct ib_flow_spec_eth) ||
1912038d2ef8SMaor Gottlieb 	    ib_attr->num_of_specs < 1)
1913038d2ef8SMaor Gottlieb 		return false;
1914038d2ef8SMaor Gottlieb 
1915038d2ef8SMaor Gottlieb 	eth_spec = (struct ib_flow_spec_eth *)(ib_attr + 1);
1916038d2ef8SMaor Gottlieb 	if (eth_spec->type != IB_FLOW_SPEC_ETH ||
1917038d2ef8SMaor Gottlieb 	    eth_spec->size != sizeof(*eth_spec))
1918038d2ef8SMaor Gottlieb 		return false;
1919038d2ef8SMaor Gottlieb 
1920038d2ef8SMaor Gottlieb 	return is_multicast_ether_addr(eth_spec->mask.dst_mac) &&
1921038d2ef8SMaor Gottlieb 	       is_multicast_ether_addr(eth_spec->val.dst_mac);
1922038d2ef8SMaor Gottlieb }
1923038d2ef8SMaor Gottlieb 
1924dd063d0eSMaor Gottlieb static bool is_valid_attr(const struct ib_flow_attr *flow_attr)
1925038d2ef8SMaor Gottlieb {
1926038d2ef8SMaor Gottlieb 	union ib_flow_spec *ib_spec = (union ib_flow_spec *)(flow_attr + 1);
1927038d2ef8SMaor Gottlieb 	bool has_ipv4_spec = false;
1928038d2ef8SMaor Gottlieb 	bool eth_type_ipv4 = true;
1929038d2ef8SMaor Gottlieb 	unsigned int spec_index;
1930038d2ef8SMaor Gottlieb 
1931038d2ef8SMaor Gottlieb 	/* Validate that ethertype is correct */
1932038d2ef8SMaor Gottlieb 	for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
1933038d2ef8SMaor Gottlieb 		if (ib_spec->type == IB_FLOW_SPEC_ETH &&
1934038d2ef8SMaor Gottlieb 		    ib_spec->eth.mask.ether_type) {
1935038d2ef8SMaor Gottlieb 			if (!((ib_spec->eth.mask.ether_type == htons(0xffff)) &&
1936038d2ef8SMaor Gottlieb 			      ib_spec->eth.val.ether_type == htons(ETH_P_IP)))
1937038d2ef8SMaor Gottlieb 				eth_type_ipv4 = false;
1938038d2ef8SMaor Gottlieb 		} else if (ib_spec->type == IB_FLOW_SPEC_IPV4) {
1939038d2ef8SMaor Gottlieb 			has_ipv4_spec = true;
1940038d2ef8SMaor Gottlieb 		}
1941038d2ef8SMaor Gottlieb 		ib_spec = (void *)ib_spec + ib_spec->size;
1942038d2ef8SMaor Gottlieb 	}
1943038d2ef8SMaor Gottlieb 	return !has_ipv4_spec || eth_type_ipv4;
1944038d2ef8SMaor Gottlieb }
1945038d2ef8SMaor Gottlieb 
1946038d2ef8SMaor Gottlieb static void put_flow_table(struct mlx5_ib_dev *dev,
1947038d2ef8SMaor Gottlieb 			   struct mlx5_ib_flow_prio *prio, bool ft_added)
1948038d2ef8SMaor Gottlieb {
1949038d2ef8SMaor Gottlieb 	prio->refcount -= !!ft_added;
1950038d2ef8SMaor Gottlieb 	if (!prio->refcount) {
1951038d2ef8SMaor Gottlieb 		mlx5_destroy_flow_table(prio->flow_table);
1952038d2ef8SMaor Gottlieb 		prio->flow_table = NULL;
1953038d2ef8SMaor Gottlieb 	}
1954038d2ef8SMaor Gottlieb }
1955038d2ef8SMaor Gottlieb 
1956038d2ef8SMaor Gottlieb static int mlx5_ib_destroy_flow(struct ib_flow *flow_id)
1957038d2ef8SMaor Gottlieb {
1958038d2ef8SMaor Gottlieb 	struct mlx5_ib_dev *dev = to_mdev(flow_id->qp->device);
1959038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = container_of(flow_id,
1960038d2ef8SMaor Gottlieb 							  struct mlx5_ib_flow_handler,
1961038d2ef8SMaor Gottlieb 							  ibflow);
1962038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *iter, *tmp;
1963038d2ef8SMaor Gottlieb 
1964038d2ef8SMaor Gottlieb 	mutex_lock(&dev->flow_db.lock);
1965038d2ef8SMaor Gottlieb 
1966038d2ef8SMaor Gottlieb 	list_for_each_entry_safe(iter, tmp, &handler->list, list) {
196774491de9SMark Bloch 		mlx5_del_flow_rules(iter->rule);
1968cc0e5d42SMaor Gottlieb 		put_flow_table(dev, iter->prio, true);
1969038d2ef8SMaor Gottlieb 		list_del(&iter->list);
1970038d2ef8SMaor Gottlieb 		kfree(iter);
1971038d2ef8SMaor Gottlieb 	}
1972038d2ef8SMaor Gottlieb 
197374491de9SMark Bloch 	mlx5_del_flow_rules(handler->rule);
19745497adc6SMaor Gottlieb 	put_flow_table(dev, handler->prio, true);
1975038d2ef8SMaor Gottlieb 	mutex_unlock(&dev->flow_db.lock);
1976038d2ef8SMaor Gottlieb 
1977038d2ef8SMaor Gottlieb 	kfree(handler);
1978038d2ef8SMaor Gottlieb 
1979038d2ef8SMaor Gottlieb 	return 0;
1980038d2ef8SMaor Gottlieb }
1981038d2ef8SMaor Gottlieb 
198235d19011SMaor Gottlieb static int ib_prio_to_core_prio(unsigned int priority, bool dont_trap)
198335d19011SMaor Gottlieb {
198435d19011SMaor Gottlieb 	priority *= 2;
198535d19011SMaor Gottlieb 	if (!dont_trap)
198635d19011SMaor Gottlieb 		priority++;
198735d19011SMaor Gottlieb 	return priority;
198835d19011SMaor Gottlieb }
198935d19011SMaor Gottlieb 
1990cc0e5d42SMaor Gottlieb enum flow_table_type {
1991cc0e5d42SMaor Gottlieb 	MLX5_IB_FT_RX,
1992cc0e5d42SMaor Gottlieb 	MLX5_IB_FT_TX
1993cc0e5d42SMaor Gottlieb };
1994cc0e5d42SMaor Gottlieb 
1995038d2ef8SMaor Gottlieb #define MLX5_FS_MAX_TYPES	 10
1996038d2ef8SMaor Gottlieb #define MLX5_FS_MAX_ENTRIES	 32000UL
1997038d2ef8SMaor Gottlieb static struct mlx5_ib_flow_prio *get_flow_table(struct mlx5_ib_dev *dev,
1998cc0e5d42SMaor Gottlieb 						struct ib_flow_attr *flow_attr,
1999cc0e5d42SMaor Gottlieb 						enum flow_table_type ft_type)
2000038d2ef8SMaor Gottlieb {
200135d19011SMaor Gottlieb 	bool dont_trap = flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP;
2002038d2ef8SMaor Gottlieb 	struct mlx5_flow_namespace *ns = NULL;
2003038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_prio *prio;
2004038d2ef8SMaor Gottlieb 	struct mlx5_flow_table *ft;
2005038d2ef8SMaor Gottlieb 	int num_entries;
2006038d2ef8SMaor Gottlieb 	int num_groups;
2007038d2ef8SMaor Gottlieb 	int priority;
2008038d2ef8SMaor Gottlieb 	int err = 0;
2009038d2ef8SMaor Gottlieb 
2010038d2ef8SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
201135d19011SMaor Gottlieb 		if (flow_is_multicast_only(flow_attr) &&
201235d19011SMaor Gottlieb 		    !dont_trap)
2013038d2ef8SMaor Gottlieb 			priority = MLX5_IB_FLOW_MCAST_PRIO;
2014038d2ef8SMaor Gottlieb 		else
201535d19011SMaor Gottlieb 			priority = ib_prio_to_core_prio(flow_attr->priority,
201635d19011SMaor Gottlieb 							dont_trap);
2017038d2ef8SMaor Gottlieb 		ns = mlx5_get_flow_namespace(dev->mdev,
2018038d2ef8SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_BYPASS);
2019038d2ef8SMaor Gottlieb 		num_entries = MLX5_FS_MAX_ENTRIES;
2020038d2ef8SMaor Gottlieb 		num_groups = MLX5_FS_MAX_TYPES;
2021038d2ef8SMaor Gottlieb 		prio = &dev->flow_db.prios[priority];
2022038d2ef8SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
2023038d2ef8SMaor Gottlieb 		   flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
2024038d2ef8SMaor Gottlieb 		ns = mlx5_get_flow_namespace(dev->mdev,
2025038d2ef8SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_LEFTOVERS);
2026038d2ef8SMaor Gottlieb 		build_leftovers_ft_param(&priority,
2027038d2ef8SMaor Gottlieb 					 &num_entries,
2028038d2ef8SMaor Gottlieb 					 &num_groups);
2029038d2ef8SMaor Gottlieb 		prio = &dev->flow_db.prios[MLX5_IB_FLOW_LEFTOVERS_PRIO];
2030cc0e5d42SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
2031cc0e5d42SMaor Gottlieb 		if (!MLX5_CAP_FLOWTABLE(dev->mdev,
2032cc0e5d42SMaor Gottlieb 					allow_sniffer_and_nic_rx_shared_tir))
2033cc0e5d42SMaor Gottlieb 			return ERR_PTR(-ENOTSUPP);
2034cc0e5d42SMaor Gottlieb 
2035cc0e5d42SMaor Gottlieb 		ns = mlx5_get_flow_namespace(dev->mdev, ft_type == MLX5_IB_FT_RX ?
2036cc0e5d42SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_SNIFFER_RX :
2037cc0e5d42SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_SNIFFER_TX);
2038cc0e5d42SMaor Gottlieb 
2039cc0e5d42SMaor Gottlieb 		prio = &dev->flow_db.sniffer[ft_type];
2040cc0e5d42SMaor Gottlieb 		priority = 0;
2041cc0e5d42SMaor Gottlieb 		num_entries = 1;
2042cc0e5d42SMaor Gottlieb 		num_groups = 1;
2043038d2ef8SMaor Gottlieb 	}
2044038d2ef8SMaor Gottlieb 
2045038d2ef8SMaor Gottlieb 	if (!ns)
2046038d2ef8SMaor Gottlieb 		return ERR_PTR(-ENOTSUPP);
2047038d2ef8SMaor Gottlieb 
2048038d2ef8SMaor Gottlieb 	ft = prio->flow_table;
2049038d2ef8SMaor Gottlieb 	if (!ft) {
2050038d2ef8SMaor Gottlieb 		ft = mlx5_create_auto_grouped_flow_table(ns, priority,
2051038d2ef8SMaor Gottlieb 							 num_entries,
2052d63cd286SMaor Gottlieb 							 num_groups,
2053c9f1b073SHadar Hen Zion 							 0, 0);
2054038d2ef8SMaor Gottlieb 
2055038d2ef8SMaor Gottlieb 		if (!IS_ERR(ft)) {
2056038d2ef8SMaor Gottlieb 			prio->refcount = 0;
2057038d2ef8SMaor Gottlieb 			prio->flow_table = ft;
2058038d2ef8SMaor Gottlieb 		} else {
2059038d2ef8SMaor Gottlieb 			err = PTR_ERR(ft);
2060038d2ef8SMaor Gottlieb 		}
2061038d2ef8SMaor Gottlieb 	}
2062038d2ef8SMaor Gottlieb 
2063038d2ef8SMaor Gottlieb 	return err ? ERR_PTR(err) : prio;
2064038d2ef8SMaor Gottlieb }
2065038d2ef8SMaor Gottlieb 
2066038d2ef8SMaor Gottlieb static struct mlx5_ib_flow_handler *create_flow_rule(struct mlx5_ib_dev *dev,
2067038d2ef8SMaor Gottlieb 						     struct mlx5_ib_flow_prio *ft_prio,
2068dd063d0eSMaor Gottlieb 						     const struct ib_flow_attr *flow_attr,
2069038d2ef8SMaor Gottlieb 						     struct mlx5_flow_destination *dst)
2070038d2ef8SMaor Gottlieb {
2071038d2ef8SMaor Gottlieb 	struct mlx5_flow_table	*ft = ft_prio->flow_table;
2072038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler;
207366958ed9SHadar Hen Zion 	struct mlx5_flow_act flow_act = {0};
2074c5bb1730SMaor Gottlieb 	struct mlx5_flow_spec *spec;
2075dd063d0eSMaor Gottlieb 	const void *ib_flow = (const void *)flow_attr + sizeof(*flow_attr);
2076038d2ef8SMaor Gottlieb 	unsigned int spec_index;
20772ac693f9SMoses Reuben 	u32 flow_tag = MLX5_FS_DEFAULT_FLOW_TAG;
2078038d2ef8SMaor Gottlieb 	int err = 0;
2079038d2ef8SMaor Gottlieb 
2080038d2ef8SMaor Gottlieb 	if (!is_valid_attr(flow_attr))
2081038d2ef8SMaor Gottlieb 		return ERR_PTR(-EINVAL);
2082038d2ef8SMaor Gottlieb 
2083c5bb1730SMaor Gottlieb 	spec = mlx5_vzalloc(sizeof(*spec));
2084038d2ef8SMaor Gottlieb 	handler = kzalloc(sizeof(*handler), GFP_KERNEL);
2085c5bb1730SMaor Gottlieb 	if (!handler || !spec) {
2086038d2ef8SMaor Gottlieb 		err = -ENOMEM;
2087038d2ef8SMaor Gottlieb 		goto free;
2088038d2ef8SMaor Gottlieb 	}
2089038d2ef8SMaor Gottlieb 
2090038d2ef8SMaor Gottlieb 	INIT_LIST_HEAD(&handler->list);
2091038d2ef8SMaor Gottlieb 
2092038d2ef8SMaor Gottlieb 	for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
2093c5bb1730SMaor Gottlieb 		err = parse_flow_attr(spec->match_criteria,
20942ac693f9SMoses Reuben 				      spec->match_value, ib_flow, &flow_tag);
2095038d2ef8SMaor Gottlieb 		if (err < 0)
2096038d2ef8SMaor Gottlieb 			goto free;
2097038d2ef8SMaor Gottlieb 
2098038d2ef8SMaor Gottlieb 		ib_flow += ((union ib_flow_spec *)ib_flow)->size;
2099038d2ef8SMaor Gottlieb 	}
2100038d2ef8SMaor Gottlieb 
2101466fa6d2SMaor Gottlieb 	spec->match_criteria_enable = get_match_criteria_enable(spec->match_criteria);
210266958ed9SHadar Hen Zion 	flow_act.action = dst ? MLX5_FLOW_CONTEXT_ACTION_FWD_DEST :
210335d19011SMaor Gottlieb 		MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO;
21042ac693f9SMoses Reuben 
21052ac693f9SMoses Reuben 	if (flow_tag != MLX5_FS_DEFAULT_FLOW_TAG &&
21062ac693f9SMoses Reuben 	    (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
21072ac693f9SMoses Reuben 	     flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT)) {
21082ac693f9SMoses Reuben 		mlx5_ib_warn(dev, "Flow tag %u and attribute type %x isn't allowed in leftovers\n",
21092ac693f9SMoses Reuben 			     flow_tag, flow_attr->type);
21102ac693f9SMoses Reuben 		err = -EINVAL;
21112ac693f9SMoses Reuben 		goto free;
21122ac693f9SMoses Reuben 	}
21132ac693f9SMoses Reuben 	flow_act.flow_tag = flow_tag;
211474491de9SMark Bloch 	handler->rule = mlx5_add_flow_rules(ft, spec,
211566958ed9SHadar Hen Zion 					    &flow_act,
211674491de9SMark Bloch 					    dst, 1);
2117038d2ef8SMaor Gottlieb 
2118038d2ef8SMaor Gottlieb 	if (IS_ERR(handler->rule)) {
2119038d2ef8SMaor Gottlieb 		err = PTR_ERR(handler->rule);
2120038d2ef8SMaor Gottlieb 		goto free;
2121038d2ef8SMaor Gottlieb 	}
2122038d2ef8SMaor Gottlieb 
2123d9d4980aSMaor Gottlieb 	ft_prio->refcount++;
21245497adc6SMaor Gottlieb 	handler->prio = ft_prio;
2125038d2ef8SMaor Gottlieb 
2126038d2ef8SMaor Gottlieb 	ft_prio->flow_table = ft;
2127038d2ef8SMaor Gottlieb free:
2128038d2ef8SMaor Gottlieb 	if (err)
2129038d2ef8SMaor Gottlieb 		kfree(handler);
2130c5bb1730SMaor Gottlieb 	kvfree(spec);
2131038d2ef8SMaor Gottlieb 	return err ? ERR_PTR(err) : handler;
2132038d2ef8SMaor Gottlieb }
2133038d2ef8SMaor Gottlieb 
213435d19011SMaor Gottlieb static struct mlx5_ib_flow_handler *create_dont_trap_rule(struct mlx5_ib_dev *dev,
213535d19011SMaor Gottlieb 							  struct mlx5_ib_flow_prio *ft_prio,
213635d19011SMaor Gottlieb 							  struct ib_flow_attr *flow_attr,
213735d19011SMaor Gottlieb 							  struct mlx5_flow_destination *dst)
213835d19011SMaor Gottlieb {
213935d19011SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_dst = NULL;
214035d19011SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
214135d19011SMaor Gottlieb 
214235d19011SMaor Gottlieb 	handler = create_flow_rule(dev, ft_prio, flow_attr, NULL);
214335d19011SMaor Gottlieb 	if (!IS_ERR(handler)) {
214435d19011SMaor Gottlieb 		handler_dst = create_flow_rule(dev, ft_prio,
214535d19011SMaor Gottlieb 					       flow_attr, dst);
214635d19011SMaor Gottlieb 		if (IS_ERR(handler_dst)) {
214774491de9SMark Bloch 			mlx5_del_flow_rules(handler->rule);
2148d9d4980aSMaor Gottlieb 			ft_prio->refcount--;
214935d19011SMaor Gottlieb 			kfree(handler);
215035d19011SMaor Gottlieb 			handler = handler_dst;
215135d19011SMaor Gottlieb 		} else {
215235d19011SMaor Gottlieb 			list_add(&handler_dst->list, &handler->list);
215335d19011SMaor Gottlieb 		}
215435d19011SMaor Gottlieb 	}
215535d19011SMaor Gottlieb 
215635d19011SMaor Gottlieb 	return handler;
215735d19011SMaor Gottlieb }
2158038d2ef8SMaor Gottlieb enum {
2159038d2ef8SMaor Gottlieb 	LEFTOVERS_MC,
2160038d2ef8SMaor Gottlieb 	LEFTOVERS_UC,
2161038d2ef8SMaor Gottlieb };
2162038d2ef8SMaor Gottlieb 
2163038d2ef8SMaor Gottlieb static struct mlx5_ib_flow_handler *create_leftovers_rule(struct mlx5_ib_dev *dev,
2164038d2ef8SMaor Gottlieb 							  struct mlx5_ib_flow_prio *ft_prio,
2165038d2ef8SMaor Gottlieb 							  struct ib_flow_attr *flow_attr,
2166038d2ef8SMaor Gottlieb 							  struct mlx5_flow_destination *dst)
2167038d2ef8SMaor Gottlieb {
2168038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_ucast = NULL;
2169038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
2170038d2ef8SMaor Gottlieb 
2171038d2ef8SMaor Gottlieb 	static struct {
2172038d2ef8SMaor Gottlieb 		struct ib_flow_attr	flow_attr;
2173038d2ef8SMaor Gottlieb 		struct ib_flow_spec_eth eth_flow;
2174038d2ef8SMaor Gottlieb 	} leftovers_specs[] = {
2175038d2ef8SMaor Gottlieb 		[LEFTOVERS_MC] = {
2176038d2ef8SMaor Gottlieb 			.flow_attr = {
2177038d2ef8SMaor Gottlieb 				.num_of_specs = 1,
2178038d2ef8SMaor Gottlieb 				.size = sizeof(leftovers_specs[0])
2179038d2ef8SMaor Gottlieb 			},
2180038d2ef8SMaor Gottlieb 			.eth_flow = {
2181038d2ef8SMaor Gottlieb 				.type = IB_FLOW_SPEC_ETH,
2182038d2ef8SMaor Gottlieb 				.size = sizeof(struct ib_flow_spec_eth),
2183038d2ef8SMaor Gottlieb 				.mask = {.dst_mac = {0x1} },
2184038d2ef8SMaor Gottlieb 				.val =  {.dst_mac = {0x1} }
2185038d2ef8SMaor Gottlieb 			}
2186038d2ef8SMaor Gottlieb 		},
2187038d2ef8SMaor Gottlieb 		[LEFTOVERS_UC] = {
2188038d2ef8SMaor Gottlieb 			.flow_attr = {
2189038d2ef8SMaor Gottlieb 				.num_of_specs = 1,
2190038d2ef8SMaor Gottlieb 				.size = sizeof(leftovers_specs[0])
2191038d2ef8SMaor Gottlieb 			},
2192038d2ef8SMaor Gottlieb 			.eth_flow = {
2193038d2ef8SMaor Gottlieb 				.type = IB_FLOW_SPEC_ETH,
2194038d2ef8SMaor Gottlieb 				.size = sizeof(struct ib_flow_spec_eth),
2195038d2ef8SMaor Gottlieb 				.mask = {.dst_mac = {0x1} },
2196038d2ef8SMaor Gottlieb 				.val = {.dst_mac = {} }
2197038d2ef8SMaor Gottlieb 			}
2198038d2ef8SMaor Gottlieb 		}
2199038d2ef8SMaor Gottlieb 	};
2200038d2ef8SMaor Gottlieb 
2201038d2ef8SMaor Gottlieb 	handler = create_flow_rule(dev, ft_prio,
2202038d2ef8SMaor Gottlieb 				   &leftovers_specs[LEFTOVERS_MC].flow_attr,
2203038d2ef8SMaor Gottlieb 				   dst);
2204038d2ef8SMaor Gottlieb 	if (!IS_ERR(handler) &&
2205038d2ef8SMaor Gottlieb 	    flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT) {
2206038d2ef8SMaor Gottlieb 		handler_ucast = create_flow_rule(dev, ft_prio,
2207038d2ef8SMaor Gottlieb 						 &leftovers_specs[LEFTOVERS_UC].flow_attr,
2208038d2ef8SMaor Gottlieb 						 dst);
2209038d2ef8SMaor Gottlieb 		if (IS_ERR(handler_ucast)) {
221074491de9SMark Bloch 			mlx5_del_flow_rules(handler->rule);
2211d9d4980aSMaor Gottlieb 			ft_prio->refcount--;
2212038d2ef8SMaor Gottlieb 			kfree(handler);
2213038d2ef8SMaor Gottlieb 			handler = handler_ucast;
2214038d2ef8SMaor Gottlieb 		} else {
2215038d2ef8SMaor Gottlieb 			list_add(&handler_ucast->list, &handler->list);
2216038d2ef8SMaor Gottlieb 		}
2217038d2ef8SMaor Gottlieb 	}
2218038d2ef8SMaor Gottlieb 
2219038d2ef8SMaor Gottlieb 	return handler;
2220038d2ef8SMaor Gottlieb }
2221038d2ef8SMaor Gottlieb 
2222cc0e5d42SMaor Gottlieb static struct mlx5_ib_flow_handler *create_sniffer_rule(struct mlx5_ib_dev *dev,
2223cc0e5d42SMaor Gottlieb 							struct mlx5_ib_flow_prio *ft_rx,
2224cc0e5d42SMaor Gottlieb 							struct mlx5_ib_flow_prio *ft_tx,
2225cc0e5d42SMaor Gottlieb 							struct mlx5_flow_destination *dst)
2226cc0e5d42SMaor Gottlieb {
2227cc0e5d42SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_rx;
2228cc0e5d42SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_tx;
2229cc0e5d42SMaor Gottlieb 	int err;
2230cc0e5d42SMaor Gottlieb 	static const struct ib_flow_attr flow_attr  = {
2231cc0e5d42SMaor Gottlieb 		.num_of_specs = 0,
2232cc0e5d42SMaor Gottlieb 		.size = sizeof(flow_attr)
2233cc0e5d42SMaor Gottlieb 	};
2234cc0e5d42SMaor Gottlieb 
2235cc0e5d42SMaor Gottlieb 	handler_rx = create_flow_rule(dev, ft_rx, &flow_attr, dst);
2236cc0e5d42SMaor Gottlieb 	if (IS_ERR(handler_rx)) {
2237cc0e5d42SMaor Gottlieb 		err = PTR_ERR(handler_rx);
2238cc0e5d42SMaor Gottlieb 		goto err;
2239cc0e5d42SMaor Gottlieb 	}
2240cc0e5d42SMaor Gottlieb 
2241cc0e5d42SMaor Gottlieb 	handler_tx = create_flow_rule(dev, ft_tx, &flow_attr, dst);
2242cc0e5d42SMaor Gottlieb 	if (IS_ERR(handler_tx)) {
2243cc0e5d42SMaor Gottlieb 		err = PTR_ERR(handler_tx);
2244cc0e5d42SMaor Gottlieb 		goto err_tx;
2245cc0e5d42SMaor Gottlieb 	}
2246cc0e5d42SMaor Gottlieb 
2247cc0e5d42SMaor Gottlieb 	list_add(&handler_tx->list, &handler_rx->list);
2248cc0e5d42SMaor Gottlieb 
2249cc0e5d42SMaor Gottlieb 	return handler_rx;
2250cc0e5d42SMaor Gottlieb 
2251cc0e5d42SMaor Gottlieb err_tx:
225274491de9SMark Bloch 	mlx5_del_flow_rules(handler_rx->rule);
2253cc0e5d42SMaor Gottlieb 	ft_rx->refcount--;
2254cc0e5d42SMaor Gottlieb 	kfree(handler_rx);
2255cc0e5d42SMaor Gottlieb err:
2256cc0e5d42SMaor Gottlieb 	return ERR_PTR(err);
2257cc0e5d42SMaor Gottlieb }
2258cc0e5d42SMaor Gottlieb 
2259038d2ef8SMaor Gottlieb static struct ib_flow *mlx5_ib_create_flow(struct ib_qp *qp,
2260038d2ef8SMaor Gottlieb 					   struct ib_flow_attr *flow_attr,
2261038d2ef8SMaor Gottlieb 					   int domain)
2262038d2ef8SMaor Gottlieb {
2263038d2ef8SMaor Gottlieb 	struct mlx5_ib_dev *dev = to_mdev(qp->device);
2264d9f88e5aSYishai Hadas 	struct mlx5_ib_qp *mqp = to_mqp(qp);
2265038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
2266038d2ef8SMaor Gottlieb 	struct mlx5_flow_destination *dst = NULL;
2267cc0e5d42SMaor Gottlieb 	struct mlx5_ib_flow_prio *ft_prio_tx = NULL;
2268038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_prio *ft_prio;
2269038d2ef8SMaor Gottlieb 	int err;
2270038d2ef8SMaor Gottlieb 
2271038d2ef8SMaor Gottlieb 	if (flow_attr->priority > MLX5_IB_FLOW_LAST_PRIO)
2272038d2ef8SMaor Gottlieb 		return ERR_PTR(-ENOSPC);
2273038d2ef8SMaor Gottlieb 
2274038d2ef8SMaor Gottlieb 	if (domain != IB_FLOW_DOMAIN_USER ||
2275038d2ef8SMaor Gottlieb 	    flow_attr->port > MLX5_CAP_GEN(dev->mdev, num_ports) ||
227635d19011SMaor Gottlieb 	    (flow_attr->flags & ~IB_FLOW_ATTR_FLAGS_DONT_TRAP))
2277038d2ef8SMaor Gottlieb 		return ERR_PTR(-EINVAL);
2278038d2ef8SMaor Gottlieb 
2279038d2ef8SMaor Gottlieb 	dst = kzalloc(sizeof(*dst), GFP_KERNEL);
2280038d2ef8SMaor Gottlieb 	if (!dst)
2281038d2ef8SMaor Gottlieb 		return ERR_PTR(-ENOMEM);
2282038d2ef8SMaor Gottlieb 
2283038d2ef8SMaor Gottlieb 	mutex_lock(&dev->flow_db.lock);
2284038d2ef8SMaor Gottlieb 
2285cc0e5d42SMaor Gottlieb 	ft_prio = get_flow_table(dev, flow_attr, MLX5_IB_FT_RX);
2286038d2ef8SMaor Gottlieb 	if (IS_ERR(ft_prio)) {
2287038d2ef8SMaor Gottlieb 		err = PTR_ERR(ft_prio);
2288038d2ef8SMaor Gottlieb 		goto unlock;
2289038d2ef8SMaor Gottlieb 	}
2290cc0e5d42SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
2291cc0e5d42SMaor Gottlieb 		ft_prio_tx = get_flow_table(dev, flow_attr, MLX5_IB_FT_TX);
2292cc0e5d42SMaor Gottlieb 		if (IS_ERR(ft_prio_tx)) {
2293cc0e5d42SMaor Gottlieb 			err = PTR_ERR(ft_prio_tx);
2294cc0e5d42SMaor Gottlieb 			ft_prio_tx = NULL;
2295cc0e5d42SMaor Gottlieb 			goto destroy_ft;
2296cc0e5d42SMaor Gottlieb 		}
2297cc0e5d42SMaor Gottlieb 	}
2298038d2ef8SMaor Gottlieb 
2299038d2ef8SMaor Gottlieb 	dst->type = MLX5_FLOW_DESTINATION_TYPE_TIR;
2300d9f88e5aSYishai Hadas 	if (mqp->flags & MLX5_IB_QP_RSS)
2301d9f88e5aSYishai Hadas 		dst->tir_num = mqp->rss_qp.tirn;
2302d9f88e5aSYishai Hadas 	else
2303d9f88e5aSYishai Hadas 		dst->tir_num = mqp->raw_packet_qp.rq.tirn;
2304038d2ef8SMaor Gottlieb 
2305038d2ef8SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
230635d19011SMaor Gottlieb 		if (flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP)  {
230735d19011SMaor Gottlieb 			handler = create_dont_trap_rule(dev, ft_prio,
230835d19011SMaor Gottlieb 							flow_attr, dst);
230935d19011SMaor Gottlieb 		} else {
2310038d2ef8SMaor Gottlieb 			handler = create_flow_rule(dev, ft_prio, flow_attr,
2311038d2ef8SMaor Gottlieb 						   dst);
231235d19011SMaor Gottlieb 		}
2313038d2ef8SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
2314038d2ef8SMaor Gottlieb 		   flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
2315038d2ef8SMaor Gottlieb 		handler = create_leftovers_rule(dev, ft_prio, flow_attr,
2316038d2ef8SMaor Gottlieb 						dst);
2317cc0e5d42SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
2318cc0e5d42SMaor Gottlieb 		handler = create_sniffer_rule(dev, ft_prio, ft_prio_tx, dst);
2319038d2ef8SMaor Gottlieb 	} else {
2320038d2ef8SMaor Gottlieb 		err = -EINVAL;
2321038d2ef8SMaor Gottlieb 		goto destroy_ft;
2322038d2ef8SMaor Gottlieb 	}
2323038d2ef8SMaor Gottlieb 
2324038d2ef8SMaor Gottlieb 	if (IS_ERR(handler)) {
2325038d2ef8SMaor Gottlieb 		err = PTR_ERR(handler);
2326038d2ef8SMaor Gottlieb 		handler = NULL;
2327038d2ef8SMaor Gottlieb 		goto destroy_ft;
2328038d2ef8SMaor Gottlieb 	}
2329038d2ef8SMaor Gottlieb 
2330038d2ef8SMaor Gottlieb 	mutex_unlock(&dev->flow_db.lock);
2331038d2ef8SMaor Gottlieb 	kfree(dst);
2332038d2ef8SMaor Gottlieb 
2333038d2ef8SMaor Gottlieb 	return &handler->ibflow;
2334038d2ef8SMaor Gottlieb 
2335038d2ef8SMaor Gottlieb destroy_ft:
2336038d2ef8SMaor Gottlieb 	put_flow_table(dev, ft_prio, false);
2337cc0e5d42SMaor Gottlieb 	if (ft_prio_tx)
2338cc0e5d42SMaor Gottlieb 		put_flow_table(dev, ft_prio_tx, false);
2339038d2ef8SMaor Gottlieb unlock:
2340038d2ef8SMaor Gottlieb 	mutex_unlock(&dev->flow_db.lock);
2341038d2ef8SMaor Gottlieb 	kfree(dst);
2342038d2ef8SMaor Gottlieb 	kfree(handler);
2343038d2ef8SMaor Gottlieb 	return ERR_PTR(err);
2344038d2ef8SMaor Gottlieb }
2345038d2ef8SMaor Gottlieb 
2346e126ba97SEli Cohen static int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
2347e126ba97SEli Cohen {
2348e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
2349e126ba97SEli Cohen 	int err;
2350e126ba97SEli Cohen 
23519603b61dSJack Morgenstein 	err = mlx5_core_attach_mcg(dev->mdev, gid, ibqp->qp_num);
2352e126ba97SEli Cohen 	if (err)
2353e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n",
2354e126ba97SEli Cohen 			     ibqp->qp_num, gid->raw);
2355e126ba97SEli Cohen 
2356e126ba97SEli Cohen 	return err;
2357e126ba97SEli Cohen }
2358e126ba97SEli Cohen 
2359e126ba97SEli Cohen static int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
2360e126ba97SEli Cohen {
2361e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
2362e126ba97SEli Cohen 	int err;
2363e126ba97SEli Cohen 
23649603b61dSJack Morgenstein 	err = mlx5_core_detach_mcg(dev->mdev, gid, ibqp->qp_num);
2365e126ba97SEli Cohen 	if (err)
2366e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n",
2367e126ba97SEli Cohen 			     ibqp->qp_num, gid->raw);
2368e126ba97SEli Cohen 
2369e126ba97SEli Cohen 	return err;
2370e126ba97SEli Cohen }
2371e126ba97SEli Cohen 
2372e126ba97SEli Cohen static int init_node_data(struct mlx5_ib_dev *dev)
2373e126ba97SEli Cohen {
23741b5daf11SMajd Dibbiny 	int err;
2375e126ba97SEli Cohen 
23761b5daf11SMajd Dibbiny 	err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc);
2377e126ba97SEli Cohen 	if (err)
2378e126ba97SEli Cohen 		return err;
23791b5daf11SMajd Dibbiny 
23801b5daf11SMajd Dibbiny 	dev->mdev->rev_id = dev->mdev->pdev->revision;
23811b5daf11SMajd Dibbiny 
23821b5daf11SMajd Dibbiny 	return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid);
2383e126ba97SEli Cohen }
2384e126ba97SEli Cohen 
2385e126ba97SEli Cohen static ssize_t show_fw_pages(struct device *device, struct device_attribute *attr,
2386e126ba97SEli Cohen 			     char *buf)
2387e126ba97SEli Cohen {
2388e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
2389e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
2390e126ba97SEli Cohen 
23919603b61dSJack Morgenstein 	return sprintf(buf, "%d\n", dev->mdev->priv.fw_pages);
2392e126ba97SEli Cohen }
2393e126ba97SEli Cohen 
2394e126ba97SEli Cohen static ssize_t show_reg_pages(struct device *device,
2395e126ba97SEli Cohen 			      struct device_attribute *attr, char *buf)
2396e126ba97SEli Cohen {
2397e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
2398e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
2399e126ba97SEli Cohen 
24006aec21f6SHaggai Eran 	return sprintf(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages));
2401e126ba97SEli Cohen }
2402e126ba97SEli Cohen 
2403e126ba97SEli Cohen static ssize_t show_hca(struct device *device, struct device_attribute *attr,
2404e126ba97SEli Cohen 			char *buf)
2405e126ba97SEli Cohen {
2406e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
2407e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
24089603b61dSJack Morgenstein 	return sprintf(buf, "MT%d\n", dev->mdev->pdev->device);
2409e126ba97SEli Cohen }
2410e126ba97SEli Cohen 
2411e126ba97SEli Cohen static ssize_t show_rev(struct device *device, struct device_attribute *attr,
2412e126ba97SEli Cohen 			char *buf)
2413e126ba97SEli Cohen {
2414e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
2415e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
24169603b61dSJack Morgenstein 	return sprintf(buf, "%x\n", dev->mdev->rev_id);
2417e126ba97SEli Cohen }
2418e126ba97SEli Cohen 
2419e126ba97SEli Cohen static ssize_t show_board(struct device *device, struct device_attribute *attr,
2420e126ba97SEli Cohen 			  char *buf)
2421e126ba97SEli Cohen {
2422e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
2423e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
2424e126ba97SEli Cohen 	return sprintf(buf, "%.*s\n", MLX5_BOARD_ID_LEN,
24259603b61dSJack Morgenstein 		       dev->mdev->board_id);
2426e126ba97SEli Cohen }
2427e126ba97SEli Cohen 
2428e126ba97SEli Cohen static DEVICE_ATTR(hw_rev,   S_IRUGO, show_rev,    NULL);
2429e126ba97SEli Cohen static DEVICE_ATTR(hca_type, S_IRUGO, show_hca,    NULL);
2430e126ba97SEli Cohen static DEVICE_ATTR(board_id, S_IRUGO, show_board,  NULL);
2431e126ba97SEli Cohen static DEVICE_ATTR(fw_pages, S_IRUGO, show_fw_pages, NULL);
2432e126ba97SEli Cohen static DEVICE_ATTR(reg_pages, S_IRUGO, show_reg_pages, NULL);
2433e126ba97SEli Cohen 
2434e126ba97SEli Cohen static struct device_attribute *mlx5_class_attributes[] = {
2435e126ba97SEli Cohen 	&dev_attr_hw_rev,
2436e126ba97SEli Cohen 	&dev_attr_hca_type,
2437e126ba97SEli Cohen 	&dev_attr_board_id,
2438e126ba97SEli Cohen 	&dev_attr_fw_pages,
2439e126ba97SEli Cohen 	&dev_attr_reg_pages,
2440e126ba97SEli Cohen };
2441e126ba97SEli Cohen 
24427722f47eSHaggai Eran static void pkey_change_handler(struct work_struct *work)
24437722f47eSHaggai Eran {
24447722f47eSHaggai Eran 	struct mlx5_ib_port_resources *ports =
24457722f47eSHaggai Eran 		container_of(work, struct mlx5_ib_port_resources,
24467722f47eSHaggai Eran 			     pkey_change_work);
24477722f47eSHaggai Eran 
24487722f47eSHaggai Eran 	mutex_lock(&ports->devr->mutex);
24497722f47eSHaggai Eran 	mlx5_ib_gsi_pkey_change(ports->gsi);
24507722f47eSHaggai Eran 	mutex_unlock(&ports->devr->mutex);
24517722f47eSHaggai Eran }
24527722f47eSHaggai Eran 
245389ea94a7SMaor Gottlieb static void mlx5_ib_handle_internal_error(struct mlx5_ib_dev *ibdev)
245489ea94a7SMaor Gottlieb {
245589ea94a7SMaor Gottlieb 	struct mlx5_ib_qp *mqp;
245689ea94a7SMaor Gottlieb 	struct mlx5_ib_cq *send_mcq, *recv_mcq;
245789ea94a7SMaor Gottlieb 	struct mlx5_core_cq *mcq;
245889ea94a7SMaor Gottlieb 	struct list_head cq_armed_list;
245989ea94a7SMaor Gottlieb 	unsigned long flags_qp;
246089ea94a7SMaor Gottlieb 	unsigned long flags_cq;
246189ea94a7SMaor Gottlieb 	unsigned long flags;
246289ea94a7SMaor Gottlieb 
246389ea94a7SMaor Gottlieb 	INIT_LIST_HEAD(&cq_armed_list);
246489ea94a7SMaor Gottlieb 
246589ea94a7SMaor Gottlieb 	/* Go over qp list reside on that ibdev, sync with create/destroy qp.*/
246689ea94a7SMaor Gottlieb 	spin_lock_irqsave(&ibdev->reset_flow_resource_lock, flags);
246789ea94a7SMaor Gottlieb 	list_for_each_entry(mqp, &ibdev->qp_list, qps_list) {
246889ea94a7SMaor Gottlieb 		spin_lock_irqsave(&mqp->sq.lock, flags_qp);
246989ea94a7SMaor Gottlieb 		if (mqp->sq.tail != mqp->sq.head) {
247089ea94a7SMaor Gottlieb 			send_mcq = to_mcq(mqp->ibqp.send_cq);
247189ea94a7SMaor Gottlieb 			spin_lock_irqsave(&send_mcq->lock, flags_cq);
247289ea94a7SMaor Gottlieb 			if (send_mcq->mcq.comp &&
247389ea94a7SMaor Gottlieb 			    mqp->ibqp.send_cq->comp_handler) {
247489ea94a7SMaor Gottlieb 				if (!send_mcq->mcq.reset_notify_added) {
247589ea94a7SMaor Gottlieb 					send_mcq->mcq.reset_notify_added = 1;
247689ea94a7SMaor Gottlieb 					list_add_tail(&send_mcq->mcq.reset_notify,
247789ea94a7SMaor Gottlieb 						      &cq_armed_list);
247889ea94a7SMaor Gottlieb 				}
247989ea94a7SMaor Gottlieb 			}
248089ea94a7SMaor Gottlieb 			spin_unlock_irqrestore(&send_mcq->lock, flags_cq);
248189ea94a7SMaor Gottlieb 		}
248289ea94a7SMaor Gottlieb 		spin_unlock_irqrestore(&mqp->sq.lock, flags_qp);
248389ea94a7SMaor Gottlieb 		spin_lock_irqsave(&mqp->rq.lock, flags_qp);
248489ea94a7SMaor Gottlieb 		/* no handling is needed for SRQ */
248589ea94a7SMaor Gottlieb 		if (!mqp->ibqp.srq) {
248689ea94a7SMaor Gottlieb 			if (mqp->rq.tail != mqp->rq.head) {
248789ea94a7SMaor Gottlieb 				recv_mcq = to_mcq(mqp->ibqp.recv_cq);
248889ea94a7SMaor Gottlieb 				spin_lock_irqsave(&recv_mcq->lock, flags_cq);
248989ea94a7SMaor Gottlieb 				if (recv_mcq->mcq.comp &&
249089ea94a7SMaor Gottlieb 				    mqp->ibqp.recv_cq->comp_handler) {
249189ea94a7SMaor Gottlieb 					if (!recv_mcq->mcq.reset_notify_added) {
249289ea94a7SMaor Gottlieb 						recv_mcq->mcq.reset_notify_added = 1;
249389ea94a7SMaor Gottlieb 						list_add_tail(&recv_mcq->mcq.reset_notify,
249489ea94a7SMaor Gottlieb 							      &cq_armed_list);
249589ea94a7SMaor Gottlieb 					}
249689ea94a7SMaor Gottlieb 				}
249789ea94a7SMaor Gottlieb 				spin_unlock_irqrestore(&recv_mcq->lock,
249889ea94a7SMaor Gottlieb 						       flags_cq);
249989ea94a7SMaor Gottlieb 			}
250089ea94a7SMaor Gottlieb 		}
250189ea94a7SMaor Gottlieb 		spin_unlock_irqrestore(&mqp->rq.lock, flags_qp);
250289ea94a7SMaor Gottlieb 	}
250389ea94a7SMaor Gottlieb 	/*At that point all inflight post send were put to be executed as of we
250489ea94a7SMaor Gottlieb 	 * lock/unlock above locks Now need to arm all involved CQs.
250589ea94a7SMaor Gottlieb 	 */
250689ea94a7SMaor Gottlieb 	list_for_each_entry(mcq, &cq_armed_list, reset_notify) {
250789ea94a7SMaor Gottlieb 		mcq->comp(mcq);
250889ea94a7SMaor Gottlieb 	}
250989ea94a7SMaor Gottlieb 	spin_unlock_irqrestore(&ibdev->reset_flow_resource_lock, flags);
251089ea94a7SMaor Gottlieb }
251189ea94a7SMaor Gottlieb 
25129603b61dSJack Morgenstein static void mlx5_ib_event(struct mlx5_core_dev *dev, void *context,
25134d2f9bbbSJack Morgenstein 			  enum mlx5_dev_event event, unsigned long param)
2514e126ba97SEli Cohen {
25159603b61dSJack Morgenstein 	struct mlx5_ib_dev *ibdev = (struct mlx5_ib_dev *)context;
2516e126ba97SEli Cohen 	struct ib_event ibev;
2517dbaaff2aSEli Cohen 	bool fatal = false;
2518e126ba97SEli Cohen 	u8 port = 0;
2519e126ba97SEli Cohen 
2520e126ba97SEli Cohen 	switch (event) {
2521e126ba97SEli Cohen 	case MLX5_DEV_EVENT_SYS_ERROR:
2522e126ba97SEli Cohen 		ibev.event = IB_EVENT_DEVICE_FATAL;
252389ea94a7SMaor Gottlieb 		mlx5_ib_handle_internal_error(ibdev);
2524dbaaff2aSEli Cohen 		fatal = true;
2525e126ba97SEli Cohen 		break;
2526e126ba97SEli Cohen 
2527e126ba97SEli Cohen 	case MLX5_DEV_EVENT_PORT_UP:
2528e126ba97SEli Cohen 	case MLX5_DEV_EVENT_PORT_DOWN:
25292788cf3bSNoa Osherovich 	case MLX5_DEV_EVENT_PORT_INITIALIZED:
25304d2f9bbbSJack Morgenstein 		port = (u8)param;
25315ec8c83eSAviv Heller 
25325ec8c83eSAviv Heller 		/* In RoCE, port up/down events are handled in
25335ec8c83eSAviv Heller 		 * mlx5_netdev_event().
25345ec8c83eSAviv Heller 		 */
25355ec8c83eSAviv Heller 		if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) ==
25365ec8c83eSAviv Heller 			IB_LINK_LAYER_ETHERNET)
25375ec8c83eSAviv Heller 			return;
25385ec8c83eSAviv Heller 
25395ec8c83eSAviv Heller 		ibev.event = (event == MLX5_DEV_EVENT_PORT_UP) ?
25405ec8c83eSAviv Heller 			     IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
2541e126ba97SEli Cohen 		break;
2542e126ba97SEli Cohen 
2543e126ba97SEli Cohen 	case MLX5_DEV_EVENT_LID_CHANGE:
2544e126ba97SEli Cohen 		ibev.event = IB_EVENT_LID_CHANGE;
25454d2f9bbbSJack Morgenstein 		port = (u8)param;
2546e126ba97SEli Cohen 		break;
2547e126ba97SEli Cohen 
2548e126ba97SEli Cohen 	case MLX5_DEV_EVENT_PKEY_CHANGE:
2549e126ba97SEli Cohen 		ibev.event = IB_EVENT_PKEY_CHANGE;
25504d2f9bbbSJack Morgenstein 		port = (u8)param;
25517722f47eSHaggai Eran 
25527722f47eSHaggai Eran 		schedule_work(&ibdev->devr.ports[port - 1].pkey_change_work);
2553e126ba97SEli Cohen 		break;
2554e126ba97SEli Cohen 
2555e126ba97SEli Cohen 	case MLX5_DEV_EVENT_GUID_CHANGE:
2556e126ba97SEli Cohen 		ibev.event = IB_EVENT_GID_CHANGE;
25574d2f9bbbSJack Morgenstein 		port = (u8)param;
2558e126ba97SEli Cohen 		break;
2559e126ba97SEli Cohen 
2560e126ba97SEli Cohen 	case MLX5_DEV_EVENT_CLIENT_REREG:
2561e126ba97SEli Cohen 		ibev.event = IB_EVENT_CLIENT_REREGISTER;
25624d2f9bbbSJack Morgenstein 		port = (u8)param;
2563e126ba97SEli Cohen 		break;
2564bdc37924SSaeed Mahameed 	default:
2565bdc37924SSaeed Mahameed 		return;
2566e126ba97SEli Cohen 	}
2567e126ba97SEli Cohen 
2568e126ba97SEli Cohen 	ibev.device	      = &ibdev->ib_dev;
2569e126ba97SEli Cohen 	ibev.element.port_num = port;
2570e126ba97SEli Cohen 
2571a0c84c32SEli Cohen 	if (port < 1 || port > ibdev->num_ports) {
2572a0c84c32SEli Cohen 		mlx5_ib_warn(ibdev, "warning: event on port %d\n", port);
2573a0c84c32SEli Cohen 		return;
2574a0c84c32SEli Cohen 	}
2575a0c84c32SEli Cohen 
2576e126ba97SEli Cohen 	if (ibdev->ib_active)
2577e126ba97SEli Cohen 		ib_dispatch_event(&ibev);
2578dbaaff2aSEli Cohen 
2579dbaaff2aSEli Cohen 	if (fatal)
2580dbaaff2aSEli Cohen 		ibdev->ib_active = false;
2581e126ba97SEli Cohen }
2582e126ba97SEli Cohen 
2583c43f1112SMaor Gottlieb static int set_has_smi_cap(struct mlx5_ib_dev *dev)
2584c43f1112SMaor Gottlieb {
2585c43f1112SMaor Gottlieb 	struct mlx5_hca_vport_context vport_ctx;
2586c43f1112SMaor Gottlieb 	int err;
2587c43f1112SMaor Gottlieb 	int port;
2588c43f1112SMaor Gottlieb 
2589c43f1112SMaor Gottlieb 	for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++) {
2590c43f1112SMaor Gottlieb 		dev->mdev->port_caps[port - 1].has_smi = false;
2591c43f1112SMaor Gottlieb 		if (MLX5_CAP_GEN(dev->mdev, port_type) ==
2592c43f1112SMaor Gottlieb 		    MLX5_CAP_PORT_TYPE_IB) {
2593c43f1112SMaor Gottlieb 			if (MLX5_CAP_GEN(dev->mdev, ib_virt)) {
2594c43f1112SMaor Gottlieb 				err = mlx5_query_hca_vport_context(dev->mdev, 0,
2595c43f1112SMaor Gottlieb 								   port, 0,
2596c43f1112SMaor Gottlieb 								   &vport_ctx);
2597c43f1112SMaor Gottlieb 				if (err) {
2598c43f1112SMaor Gottlieb 					mlx5_ib_err(dev, "query_hca_vport_context for port=%d failed %d\n",
2599c43f1112SMaor Gottlieb 						    port, err);
2600c43f1112SMaor Gottlieb 					return err;
2601c43f1112SMaor Gottlieb 				}
2602c43f1112SMaor Gottlieb 				dev->mdev->port_caps[port - 1].has_smi =
2603c43f1112SMaor Gottlieb 					vport_ctx.has_smi;
2604c43f1112SMaor Gottlieb 			} else {
2605c43f1112SMaor Gottlieb 				dev->mdev->port_caps[port - 1].has_smi = true;
2606c43f1112SMaor Gottlieb 			}
2607c43f1112SMaor Gottlieb 		}
2608c43f1112SMaor Gottlieb 	}
2609c43f1112SMaor Gottlieb 	return 0;
2610c43f1112SMaor Gottlieb }
2611c43f1112SMaor Gottlieb 
2612e126ba97SEli Cohen static void get_ext_port_caps(struct mlx5_ib_dev *dev)
2613e126ba97SEli Cohen {
2614e126ba97SEli Cohen 	int port;
2615e126ba97SEli Cohen 
2616938fe83cSSaeed Mahameed 	for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++)
2617e126ba97SEli Cohen 		mlx5_query_ext_port_caps(dev, port);
2618e126ba97SEli Cohen }
2619e126ba97SEli Cohen 
2620e126ba97SEli Cohen static int get_port_caps(struct mlx5_ib_dev *dev)
2621e126ba97SEli Cohen {
2622e126ba97SEli Cohen 	struct ib_device_attr *dprops = NULL;
2623e126ba97SEli Cohen 	struct ib_port_attr *pprops = NULL;
2624f614fc15SDan Carpenter 	int err = -ENOMEM;
2625e126ba97SEli Cohen 	int port;
26262528e33eSMatan Barak 	struct ib_udata uhw = {.inlen = 0, .outlen = 0};
2627e126ba97SEli Cohen 
2628e126ba97SEli Cohen 	pprops = kmalloc(sizeof(*pprops), GFP_KERNEL);
2629e126ba97SEli Cohen 	if (!pprops)
2630e126ba97SEli Cohen 		goto out;
2631e126ba97SEli Cohen 
2632e126ba97SEli Cohen 	dprops = kmalloc(sizeof(*dprops), GFP_KERNEL);
2633e126ba97SEli Cohen 	if (!dprops)
2634e126ba97SEli Cohen 		goto out;
2635e126ba97SEli Cohen 
2636c43f1112SMaor Gottlieb 	err = set_has_smi_cap(dev);
2637c43f1112SMaor Gottlieb 	if (err)
2638c43f1112SMaor Gottlieb 		goto out;
2639c43f1112SMaor Gottlieb 
26402528e33eSMatan Barak 	err = mlx5_ib_query_device(&dev->ib_dev, dprops, &uhw);
2641e126ba97SEli Cohen 	if (err) {
2642e126ba97SEli Cohen 		mlx5_ib_warn(dev, "query_device failed %d\n", err);
2643e126ba97SEli Cohen 		goto out;
2644e126ba97SEli Cohen 	}
2645e126ba97SEli Cohen 
2646938fe83cSSaeed Mahameed 	for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++) {
2647e126ba97SEli Cohen 		err = mlx5_ib_query_port(&dev->ib_dev, port, pprops);
2648e126ba97SEli Cohen 		if (err) {
2649938fe83cSSaeed Mahameed 			mlx5_ib_warn(dev, "query_port %d failed %d\n",
2650938fe83cSSaeed Mahameed 				     port, err);
2651e126ba97SEli Cohen 			break;
2652e126ba97SEli Cohen 		}
2653938fe83cSSaeed Mahameed 		dev->mdev->port_caps[port - 1].pkey_table_len =
2654938fe83cSSaeed Mahameed 						dprops->max_pkeys;
2655938fe83cSSaeed Mahameed 		dev->mdev->port_caps[port - 1].gid_table_len =
2656938fe83cSSaeed Mahameed 						pprops->gid_tbl_len;
2657e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "pkey_table_len %d, gid_table_len %d\n",
2658e126ba97SEli Cohen 			    dprops->max_pkeys, pprops->gid_tbl_len);
2659e126ba97SEli Cohen 	}
2660e126ba97SEli Cohen 
2661e126ba97SEli Cohen out:
2662e126ba97SEli Cohen 	kfree(pprops);
2663e126ba97SEli Cohen 	kfree(dprops);
2664e126ba97SEli Cohen 
2665e126ba97SEli Cohen 	return err;
2666e126ba97SEli Cohen }
2667e126ba97SEli Cohen 
2668e126ba97SEli Cohen static void destroy_umrc_res(struct mlx5_ib_dev *dev)
2669e126ba97SEli Cohen {
2670e126ba97SEli Cohen 	int err;
2671e126ba97SEli Cohen 
2672e126ba97SEli Cohen 	err = mlx5_mr_cache_cleanup(dev);
2673e126ba97SEli Cohen 	if (err)
2674e126ba97SEli Cohen 		mlx5_ib_warn(dev, "mr cache cleanup failed\n");
2675e126ba97SEli Cohen 
2676e126ba97SEli Cohen 	mlx5_ib_destroy_qp(dev->umrc.qp);
2677add08d76SChristoph Hellwig 	ib_free_cq(dev->umrc.cq);
2678e126ba97SEli Cohen 	ib_dealloc_pd(dev->umrc.pd);
2679e126ba97SEli Cohen }
2680e126ba97SEli Cohen 
2681e126ba97SEli Cohen enum {
2682e126ba97SEli Cohen 	MAX_UMR_WR = 128,
2683e126ba97SEli Cohen };
2684e126ba97SEli Cohen 
2685e126ba97SEli Cohen static int create_umr_res(struct mlx5_ib_dev *dev)
2686e126ba97SEli Cohen {
2687e126ba97SEli Cohen 	struct ib_qp_init_attr *init_attr = NULL;
2688e126ba97SEli Cohen 	struct ib_qp_attr *attr = NULL;
2689e126ba97SEli Cohen 	struct ib_pd *pd;
2690e126ba97SEli Cohen 	struct ib_cq *cq;
2691e126ba97SEli Cohen 	struct ib_qp *qp;
2692e126ba97SEli Cohen 	int ret;
2693e126ba97SEli Cohen 
2694e126ba97SEli Cohen 	attr = kzalloc(sizeof(*attr), GFP_KERNEL);
2695e126ba97SEli Cohen 	init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL);
2696e126ba97SEli Cohen 	if (!attr || !init_attr) {
2697e126ba97SEli Cohen 		ret = -ENOMEM;
2698e126ba97SEli Cohen 		goto error_0;
2699e126ba97SEli Cohen 	}
2700e126ba97SEli Cohen 
2701ed082d36SChristoph Hellwig 	pd = ib_alloc_pd(&dev->ib_dev, 0);
2702e126ba97SEli Cohen 	if (IS_ERR(pd)) {
2703e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create PD for sync UMR QP\n");
2704e126ba97SEli Cohen 		ret = PTR_ERR(pd);
2705e126ba97SEli Cohen 		goto error_0;
2706e126ba97SEli Cohen 	}
2707e126ba97SEli Cohen 
2708add08d76SChristoph Hellwig 	cq = ib_alloc_cq(&dev->ib_dev, NULL, 128, 0, IB_POLL_SOFTIRQ);
2709e126ba97SEli Cohen 	if (IS_ERR(cq)) {
2710e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create CQ for sync UMR QP\n");
2711e126ba97SEli Cohen 		ret = PTR_ERR(cq);
2712e126ba97SEli Cohen 		goto error_2;
2713e126ba97SEli Cohen 	}
2714e126ba97SEli Cohen 
2715e126ba97SEli Cohen 	init_attr->send_cq = cq;
2716e126ba97SEli Cohen 	init_attr->recv_cq = cq;
2717e126ba97SEli Cohen 	init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
2718e126ba97SEli Cohen 	init_attr->cap.max_send_wr = MAX_UMR_WR;
2719e126ba97SEli Cohen 	init_attr->cap.max_send_sge = 1;
2720e126ba97SEli Cohen 	init_attr->qp_type = MLX5_IB_QPT_REG_UMR;
2721e126ba97SEli Cohen 	init_attr->port_num = 1;
2722e126ba97SEli Cohen 	qp = mlx5_ib_create_qp(pd, init_attr, NULL);
2723e126ba97SEli Cohen 	if (IS_ERR(qp)) {
2724e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create sync UMR QP\n");
2725e126ba97SEli Cohen 		ret = PTR_ERR(qp);
2726e126ba97SEli Cohen 		goto error_3;
2727e126ba97SEli Cohen 	}
2728e126ba97SEli Cohen 	qp->device     = &dev->ib_dev;
2729e126ba97SEli Cohen 	qp->real_qp    = qp;
2730e126ba97SEli Cohen 	qp->uobject    = NULL;
2731e126ba97SEli Cohen 	qp->qp_type    = MLX5_IB_QPT_REG_UMR;
2732e126ba97SEli Cohen 
2733e126ba97SEli Cohen 	attr->qp_state = IB_QPS_INIT;
2734e126ba97SEli Cohen 	attr->port_num = 1;
2735e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_PKEY_INDEX |
2736e126ba97SEli Cohen 				IB_QP_PORT, NULL);
2737e126ba97SEli Cohen 	if (ret) {
2738e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify UMR QP\n");
2739e126ba97SEli Cohen 		goto error_4;
2740e126ba97SEli Cohen 	}
2741e126ba97SEli Cohen 
2742e126ba97SEli Cohen 	memset(attr, 0, sizeof(*attr));
2743e126ba97SEli Cohen 	attr->qp_state = IB_QPS_RTR;
2744e126ba97SEli Cohen 	attr->path_mtu = IB_MTU_256;
2745e126ba97SEli Cohen 
2746e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
2747e126ba97SEli Cohen 	if (ret) {
2748e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify umr QP to rtr\n");
2749e126ba97SEli Cohen 		goto error_4;
2750e126ba97SEli Cohen 	}
2751e126ba97SEli Cohen 
2752e126ba97SEli Cohen 	memset(attr, 0, sizeof(*attr));
2753e126ba97SEli Cohen 	attr->qp_state = IB_QPS_RTS;
2754e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
2755e126ba97SEli Cohen 	if (ret) {
2756e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify umr QP to rts\n");
2757e126ba97SEli Cohen 		goto error_4;
2758e126ba97SEli Cohen 	}
2759e126ba97SEli Cohen 
2760e126ba97SEli Cohen 	dev->umrc.qp = qp;
2761e126ba97SEli Cohen 	dev->umrc.cq = cq;
2762e126ba97SEli Cohen 	dev->umrc.pd = pd;
2763e126ba97SEli Cohen 
2764e126ba97SEli Cohen 	sema_init(&dev->umrc.sem, MAX_UMR_WR);
2765e126ba97SEli Cohen 	ret = mlx5_mr_cache_init(dev);
2766e126ba97SEli Cohen 	if (ret) {
2767e126ba97SEli Cohen 		mlx5_ib_warn(dev, "mr cache init failed %d\n", ret);
2768e126ba97SEli Cohen 		goto error_4;
2769e126ba97SEli Cohen 	}
2770e126ba97SEli Cohen 
2771e126ba97SEli Cohen 	kfree(attr);
2772e126ba97SEli Cohen 	kfree(init_attr);
2773e126ba97SEli Cohen 
2774e126ba97SEli Cohen 	return 0;
2775e126ba97SEli Cohen 
2776e126ba97SEli Cohen error_4:
2777e126ba97SEli Cohen 	mlx5_ib_destroy_qp(qp);
2778e126ba97SEli Cohen 
2779e126ba97SEli Cohen error_3:
2780add08d76SChristoph Hellwig 	ib_free_cq(cq);
2781e126ba97SEli Cohen 
2782e126ba97SEli Cohen error_2:
2783e126ba97SEli Cohen 	ib_dealloc_pd(pd);
2784e126ba97SEli Cohen 
2785e126ba97SEli Cohen error_0:
2786e126ba97SEli Cohen 	kfree(attr);
2787e126ba97SEli Cohen 	kfree(init_attr);
2788e126ba97SEli Cohen 	return ret;
2789e126ba97SEli Cohen }
2790e126ba97SEli Cohen 
2791e126ba97SEli Cohen static int create_dev_resources(struct mlx5_ib_resources *devr)
2792e126ba97SEli Cohen {
2793e126ba97SEli Cohen 	struct ib_srq_init_attr attr;
2794e126ba97SEli Cohen 	struct mlx5_ib_dev *dev;
2795bcf4c1eaSMatan Barak 	struct ib_cq_init_attr cq_attr = {.cqe = 1};
27967722f47eSHaggai Eran 	int port;
2797e126ba97SEli Cohen 	int ret = 0;
2798e126ba97SEli Cohen 
2799e126ba97SEli Cohen 	dev = container_of(devr, struct mlx5_ib_dev, devr);
2800e126ba97SEli Cohen 
2801d16e91daSHaggai Eran 	mutex_init(&devr->mutex);
2802d16e91daSHaggai Eran 
2803e126ba97SEli Cohen 	devr->p0 = mlx5_ib_alloc_pd(&dev->ib_dev, NULL, NULL);
2804e126ba97SEli Cohen 	if (IS_ERR(devr->p0)) {
2805e126ba97SEli Cohen 		ret = PTR_ERR(devr->p0);
2806e126ba97SEli Cohen 		goto error0;
2807e126ba97SEli Cohen 	}
2808e126ba97SEli Cohen 	devr->p0->device  = &dev->ib_dev;
2809e126ba97SEli Cohen 	devr->p0->uobject = NULL;
2810e126ba97SEli Cohen 	atomic_set(&devr->p0->usecnt, 0);
2811e126ba97SEli Cohen 
2812bcf4c1eaSMatan Barak 	devr->c0 = mlx5_ib_create_cq(&dev->ib_dev, &cq_attr, NULL, NULL);
2813e126ba97SEli Cohen 	if (IS_ERR(devr->c0)) {
2814e126ba97SEli Cohen 		ret = PTR_ERR(devr->c0);
2815e126ba97SEli Cohen 		goto error1;
2816e126ba97SEli Cohen 	}
2817e126ba97SEli Cohen 	devr->c0->device        = &dev->ib_dev;
2818e126ba97SEli Cohen 	devr->c0->uobject       = NULL;
2819e126ba97SEli Cohen 	devr->c0->comp_handler  = NULL;
2820e126ba97SEli Cohen 	devr->c0->event_handler = NULL;
2821e126ba97SEli Cohen 	devr->c0->cq_context    = NULL;
2822e126ba97SEli Cohen 	atomic_set(&devr->c0->usecnt, 0);
2823e126ba97SEli Cohen 
2824e126ba97SEli Cohen 	devr->x0 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
2825e126ba97SEli Cohen 	if (IS_ERR(devr->x0)) {
2826e126ba97SEli Cohen 		ret = PTR_ERR(devr->x0);
2827e126ba97SEli Cohen 		goto error2;
2828e126ba97SEli Cohen 	}
2829e126ba97SEli Cohen 	devr->x0->device = &dev->ib_dev;
2830e126ba97SEli Cohen 	devr->x0->inode = NULL;
2831e126ba97SEli Cohen 	atomic_set(&devr->x0->usecnt, 0);
2832e126ba97SEli Cohen 	mutex_init(&devr->x0->tgt_qp_mutex);
2833e126ba97SEli Cohen 	INIT_LIST_HEAD(&devr->x0->tgt_qp_list);
2834e126ba97SEli Cohen 
2835e126ba97SEli Cohen 	devr->x1 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
2836e126ba97SEli Cohen 	if (IS_ERR(devr->x1)) {
2837e126ba97SEli Cohen 		ret = PTR_ERR(devr->x1);
2838e126ba97SEli Cohen 		goto error3;
2839e126ba97SEli Cohen 	}
2840e126ba97SEli Cohen 	devr->x1->device = &dev->ib_dev;
2841e126ba97SEli Cohen 	devr->x1->inode = NULL;
2842e126ba97SEli Cohen 	atomic_set(&devr->x1->usecnt, 0);
2843e126ba97SEli Cohen 	mutex_init(&devr->x1->tgt_qp_mutex);
2844e126ba97SEli Cohen 	INIT_LIST_HEAD(&devr->x1->tgt_qp_list);
2845e126ba97SEli Cohen 
2846e126ba97SEli Cohen 	memset(&attr, 0, sizeof(attr));
2847e126ba97SEli Cohen 	attr.attr.max_sge = 1;
2848e126ba97SEli Cohen 	attr.attr.max_wr = 1;
2849e126ba97SEli Cohen 	attr.srq_type = IB_SRQT_XRC;
2850e126ba97SEli Cohen 	attr.ext.xrc.cq = devr->c0;
2851e126ba97SEli Cohen 	attr.ext.xrc.xrcd = devr->x0;
2852e126ba97SEli Cohen 
2853e126ba97SEli Cohen 	devr->s0 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
2854e126ba97SEli Cohen 	if (IS_ERR(devr->s0)) {
2855e126ba97SEli Cohen 		ret = PTR_ERR(devr->s0);
2856e126ba97SEli Cohen 		goto error4;
2857e126ba97SEli Cohen 	}
2858e126ba97SEli Cohen 	devr->s0->device	= &dev->ib_dev;
2859e126ba97SEli Cohen 	devr->s0->pd		= devr->p0;
2860e126ba97SEli Cohen 	devr->s0->uobject       = NULL;
2861e126ba97SEli Cohen 	devr->s0->event_handler = NULL;
2862e126ba97SEli Cohen 	devr->s0->srq_context   = NULL;
2863e126ba97SEli Cohen 	devr->s0->srq_type      = IB_SRQT_XRC;
2864e126ba97SEli Cohen 	devr->s0->ext.xrc.xrcd	= devr->x0;
2865e126ba97SEli Cohen 	devr->s0->ext.xrc.cq	= devr->c0;
2866e126ba97SEli Cohen 	atomic_inc(&devr->s0->ext.xrc.xrcd->usecnt);
2867e126ba97SEli Cohen 	atomic_inc(&devr->s0->ext.xrc.cq->usecnt);
2868e126ba97SEli Cohen 	atomic_inc(&devr->p0->usecnt);
2869e126ba97SEli Cohen 	atomic_set(&devr->s0->usecnt, 0);
2870e126ba97SEli Cohen 
28714aa17b28SHaggai Abramonvsky 	memset(&attr, 0, sizeof(attr));
28724aa17b28SHaggai Abramonvsky 	attr.attr.max_sge = 1;
28734aa17b28SHaggai Abramonvsky 	attr.attr.max_wr = 1;
28744aa17b28SHaggai Abramonvsky 	attr.srq_type = IB_SRQT_BASIC;
28754aa17b28SHaggai Abramonvsky 	devr->s1 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
28764aa17b28SHaggai Abramonvsky 	if (IS_ERR(devr->s1)) {
28774aa17b28SHaggai Abramonvsky 		ret = PTR_ERR(devr->s1);
28784aa17b28SHaggai Abramonvsky 		goto error5;
28794aa17b28SHaggai Abramonvsky 	}
28804aa17b28SHaggai Abramonvsky 	devr->s1->device	= &dev->ib_dev;
28814aa17b28SHaggai Abramonvsky 	devr->s1->pd		= devr->p0;
28824aa17b28SHaggai Abramonvsky 	devr->s1->uobject       = NULL;
28834aa17b28SHaggai Abramonvsky 	devr->s1->event_handler = NULL;
28844aa17b28SHaggai Abramonvsky 	devr->s1->srq_context   = NULL;
28854aa17b28SHaggai Abramonvsky 	devr->s1->srq_type      = IB_SRQT_BASIC;
28864aa17b28SHaggai Abramonvsky 	devr->s1->ext.xrc.cq	= devr->c0;
28874aa17b28SHaggai Abramonvsky 	atomic_inc(&devr->p0->usecnt);
28884aa17b28SHaggai Abramonvsky 	atomic_set(&devr->s0->usecnt, 0);
28894aa17b28SHaggai Abramonvsky 
28907722f47eSHaggai Eran 	for (port = 0; port < ARRAY_SIZE(devr->ports); ++port) {
28917722f47eSHaggai Eran 		INIT_WORK(&devr->ports[port].pkey_change_work,
28927722f47eSHaggai Eran 			  pkey_change_handler);
28937722f47eSHaggai Eran 		devr->ports[port].devr = devr;
28947722f47eSHaggai Eran 	}
28957722f47eSHaggai Eran 
2896e126ba97SEli Cohen 	return 0;
2897e126ba97SEli Cohen 
28984aa17b28SHaggai Abramonvsky error5:
28994aa17b28SHaggai Abramonvsky 	mlx5_ib_destroy_srq(devr->s0);
2900e126ba97SEli Cohen error4:
2901e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x1);
2902e126ba97SEli Cohen error3:
2903e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x0);
2904e126ba97SEli Cohen error2:
2905e126ba97SEli Cohen 	mlx5_ib_destroy_cq(devr->c0);
2906e126ba97SEli Cohen error1:
2907e126ba97SEli Cohen 	mlx5_ib_dealloc_pd(devr->p0);
2908e126ba97SEli Cohen error0:
2909e126ba97SEli Cohen 	return ret;
2910e126ba97SEli Cohen }
2911e126ba97SEli Cohen 
2912e126ba97SEli Cohen static void destroy_dev_resources(struct mlx5_ib_resources *devr)
2913e126ba97SEli Cohen {
29147722f47eSHaggai Eran 	struct mlx5_ib_dev *dev =
29157722f47eSHaggai Eran 		container_of(devr, struct mlx5_ib_dev, devr);
29167722f47eSHaggai Eran 	int port;
29177722f47eSHaggai Eran 
29184aa17b28SHaggai Abramonvsky 	mlx5_ib_destroy_srq(devr->s1);
2919e126ba97SEli Cohen 	mlx5_ib_destroy_srq(devr->s0);
2920e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x0);
2921e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x1);
2922e126ba97SEli Cohen 	mlx5_ib_destroy_cq(devr->c0);
2923e126ba97SEli Cohen 	mlx5_ib_dealloc_pd(devr->p0);
29247722f47eSHaggai Eran 
29257722f47eSHaggai Eran 	/* Make sure no change P_Key work items are still executing */
29267722f47eSHaggai Eran 	for (port = 0; port < dev->num_ports; ++port)
29277722f47eSHaggai Eran 		cancel_work_sync(&devr->ports[port].pkey_change_work);
2928e126ba97SEli Cohen }
2929e126ba97SEli Cohen 
2930e53505a8SAchiad Shochat static u32 get_core_cap_flags(struct ib_device *ibdev)
2931e53505a8SAchiad Shochat {
2932e53505a8SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
2933e53505a8SAchiad Shochat 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, 1);
2934e53505a8SAchiad Shochat 	u8 l3_type_cap = MLX5_CAP_ROCE(dev->mdev, l3_type);
2935e53505a8SAchiad Shochat 	u8 roce_version_cap = MLX5_CAP_ROCE(dev->mdev, roce_version);
2936e53505a8SAchiad Shochat 	u32 ret = 0;
2937e53505a8SAchiad Shochat 
2938e53505a8SAchiad Shochat 	if (ll == IB_LINK_LAYER_INFINIBAND)
2939e53505a8SAchiad Shochat 		return RDMA_CORE_PORT_IBA_IB;
2940e53505a8SAchiad Shochat 
2941*72cd5717SOr Gerlitz 	ret = RDMA_CORE_PORT_RAW_PACKET;
2942*72cd5717SOr Gerlitz 
2943e53505a8SAchiad Shochat 	if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV4_CAP))
2944*72cd5717SOr Gerlitz 		return ret;
2945e53505a8SAchiad Shochat 
2946e53505a8SAchiad Shochat 	if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV6_CAP))
2947*72cd5717SOr Gerlitz 		return ret;
2948e53505a8SAchiad Shochat 
2949e53505a8SAchiad Shochat 	if (roce_version_cap & MLX5_ROCE_VERSION_1_CAP)
2950e53505a8SAchiad Shochat 		ret |= RDMA_CORE_PORT_IBA_ROCE;
2951e53505a8SAchiad Shochat 
2952e53505a8SAchiad Shochat 	if (roce_version_cap & MLX5_ROCE_VERSION_2_CAP)
2953e53505a8SAchiad Shochat 		ret |= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
2954e53505a8SAchiad Shochat 
2955e53505a8SAchiad Shochat 	return ret;
2956e53505a8SAchiad Shochat }
2957e53505a8SAchiad Shochat 
29587738613eSIra Weiny static int mlx5_port_immutable(struct ib_device *ibdev, u8 port_num,
29597738613eSIra Weiny 			       struct ib_port_immutable *immutable)
29607738613eSIra Weiny {
29617738613eSIra Weiny 	struct ib_port_attr attr;
2962ca5b91d6SOr Gerlitz 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
2963ca5b91d6SOr Gerlitz 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, port_num);
29647738613eSIra Weiny 	int err;
29657738613eSIra Weiny 
29667738613eSIra Weiny 	err = mlx5_ib_query_port(ibdev, port_num, &attr);
29677738613eSIra Weiny 	if (err)
29687738613eSIra Weiny 		return err;
29697738613eSIra Weiny 
29707738613eSIra Weiny 	immutable->pkey_tbl_len = attr.pkey_tbl_len;
29717738613eSIra Weiny 	immutable->gid_tbl_len = attr.gid_tbl_len;
2972e53505a8SAchiad Shochat 	immutable->core_cap_flags = get_core_cap_flags(ibdev);
2973ca5b91d6SOr Gerlitz 	if ((ll == IB_LINK_LAYER_INFINIBAND) || MLX5_CAP_GEN(dev->mdev, roce))
2974337877a4SIra Weiny 		immutable->max_mad_size = IB_MGMT_MAD_SIZE;
29757738613eSIra Weiny 
29767738613eSIra Weiny 	return 0;
29777738613eSIra Weiny }
29787738613eSIra Weiny 
2979c7342823SIra Weiny static void get_dev_fw_str(struct ib_device *ibdev, char *str,
2980c7342823SIra Weiny 			   size_t str_len)
2981c7342823SIra Weiny {
2982c7342823SIra Weiny 	struct mlx5_ib_dev *dev =
2983c7342823SIra Weiny 		container_of(ibdev, struct mlx5_ib_dev, ib_dev);
2984c7342823SIra Weiny 	snprintf(str, str_len, "%d.%d.%04d", fw_rev_maj(dev->mdev),
2985c7342823SIra Weiny 		       fw_rev_min(dev->mdev), fw_rev_sub(dev->mdev));
2986c7342823SIra Weiny }
2987c7342823SIra Weiny 
298845f95acdSOr Gerlitz static int mlx5_eth_lag_init(struct mlx5_ib_dev *dev)
29899ef9c640SAviv Heller {
29909ef9c640SAviv Heller 	struct mlx5_core_dev *mdev = dev->mdev;
29919ef9c640SAviv Heller 	struct mlx5_flow_namespace *ns = mlx5_get_flow_namespace(mdev,
29929ef9c640SAviv Heller 								 MLX5_FLOW_NAMESPACE_LAG);
29939ef9c640SAviv Heller 	struct mlx5_flow_table *ft;
29949ef9c640SAviv Heller 	int err;
29959ef9c640SAviv Heller 
29969ef9c640SAviv Heller 	if (!ns || !mlx5_lag_is_active(mdev))
29979ef9c640SAviv Heller 		return 0;
29989ef9c640SAviv Heller 
29999ef9c640SAviv Heller 	err = mlx5_cmd_create_vport_lag(mdev);
30009ef9c640SAviv Heller 	if (err)
30019ef9c640SAviv Heller 		return err;
30029ef9c640SAviv Heller 
30039ef9c640SAviv Heller 	ft = mlx5_create_lag_demux_flow_table(ns, 0, 0);
30049ef9c640SAviv Heller 	if (IS_ERR(ft)) {
30059ef9c640SAviv Heller 		err = PTR_ERR(ft);
30069ef9c640SAviv Heller 		goto err_destroy_vport_lag;
30079ef9c640SAviv Heller 	}
30089ef9c640SAviv Heller 
30099ef9c640SAviv Heller 	dev->flow_db.lag_demux_ft = ft;
30109ef9c640SAviv Heller 	return 0;
30119ef9c640SAviv Heller 
30129ef9c640SAviv Heller err_destroy_vport_lag:
30139ef9c640SAviv Heller 	mlx5_cmd_destroy_vport_lag(mdev);
30149ef9c640SAviv Heller 	return err;
30159ef9c640SAviv Heller }
30169ef9c640SAviv Heller 
301745f95acdSOr Gerlitz static void mlx5_eth_lag_cleanup(struct mlx5_ib_dev *dev)
30189ef9c640SAviv Heller {
30199ef9c640SAviv Heller 	struct mlx5_core_dev *mdev = dev->mdev;
30209ef9c640SAviv Heller 
30219ef9c640SAviv Heller 	if (dev->flow_db.lag_demux_ft) {
30229ef9c640SAviv Heller 		mlx5_destroy_flow_table(dev->flow_db.lag_demux_ft);
30239ef9c640SAviv Heller 		dev->flow_db.lag_demux_ft = NULL;
30249ef9c640SAviv Heller 
30259ef9c640SAviv Heller 		mlx5_cmd_destroy_vport_lag(mdev);
30269ef9c640SAviv Heller 	}
30279ef9c640SAviv Heller }
30289ef9c640SAviv Heller 
3029d012f5d6SOr Gerlitz static int mlx5_add_netdev_notifier(struct mlx5_ib_dev *dev)
3030fc24fc5eSAchiad Shochat {
3031e53505a8SAchiad Shochat 	int err;
3032e53505a8SAchiad Shochat 
3033fc24fc5eSAchiad Shochat 	dev->roce.nb.notifier_call = mlx5_netdev_event;
3034e53505a8SAchiad Shochat 	err = register_netdevice_notifier(&dev->roce.nb);
30355ec8c83eSAviv Heller 	if (err) {
30365ec8c83eSAviv Heller 		dev->roce.nb.notifier_call = NULL;
3037e53505a8SAchiad Shochat 		return err;
30385ec8c83eSAviv Heller 	}
3039e53505a8SAchiad Shochat 
3040d012f5d6SOr Gerlitz 	return 0;
3041d012f5d6SOr Gerlitz }
3042d012f5d6SOr Gerlitz 
3043d012f5d6SOr Gerlitz static void mlx5_remove_netdev_notifier(struct mlx5_ib_dev *dev)
3044e126ba97SEli Cohen {
3045e126ba97SEli Cohen 	if (dev->roce.nb.notifier_call) {
3046e126ba97SEli Cohen 		unregister_netdevice_notifier(&dev->roce.nb);
3047e126ba97SEli Cohen 		dev->roce.nb.notifier_call = NULL;
3048e126ba97SEli Cohen 	}
3049e126ba97SEli Cohen }
3050e126ba97SEli Cohen 
305145f95acdSOr Gerlitz static int mlx5_enable_eth(struct mlx5_ib_dev *dev)
3052e126ba97SEli Cohen {
3053e126ba97SEli Cohen 	int err;
3054e126ba97SEli Cohen 
3055d012f5d6SOr Gerlitz 	err = mlx5_add_netdev_notifier(dev);
3056d012f5d6SOr Gerlitz 	if (err)
3057e53505a8SAchiad Shochat 		return err;
3058e53505a8SAchiad Shochat 
3059ca5b91d6SOr Gerlitz 	if (MLX5_CAP_GEN(dev->mdev, roce)) {
3060e53505a8SAchiad Shochat 		err = mlx5_nic_vport_enable_roce(dev->mdev);
3061e53505a8SAchiad Shochat 		if (err)
3062e53505a8SAchiad Shochat 			goto err_unregister_netdevice_notifier;
3063ca5b91d6SOr Gerlitz 	}
3064e53505a8SAchiad Shochat 
306545f95acdSOr Gerlitz 	err = mlx5_eth_lag_init(dev);
30669ef9c640SAviv Heller 	if (err)
30679ef9c640SAviv Heller 		goto err_disable_roce;
30689ef9c640SAviv Heller 
3069e53505a8SAchiad Shochat 	return 0;
3070e53505a8SAchiad Shochat 
30719ef9c640SAviv Heller err_disable_roce:
3072ca5b91d6SOr Gerlitz 	if (MLX5_CAP_GEN(dev->mdev, roce))
30739ef9c640SAviv Heller 		mlx5_nic_vport_disable_roce(dev->mdev);
30749ef9c640SAviv Heller 
3075e53505a8SAchiad Shochat err_unregister_netdevice_notifier:
3076d012f5d6SOr Gerlitz 	mlx5_remove_netdev_notifier(dev);
3077e53505a8SAchiad Shochat 	return err;
3078fc24fc5eSAchiad Shochat }
3079fc24fc5eSAchiad Shochat 
308045f95acdSOr Gerlitz static void mlx5_disable_eth(struct mlx5_ib_dev *dev)
3081fc24fc5eSAchiad Shochat {
308245f95acdSOr Gerlitz 	mlx5_eth_lag_cleanup(dev);
3083ca5b91d6SOr Gerlitz 	if (MLX5_CAP_GEN(dev->mdev, roce))
3084e53505a8SAchiad Shochat 		mlx5_nic_vport_disable_roce(dev->mdev);
3085fc24fc5eSAchiad Shochat }
3086fc24fc5eSAchiad Shochat 
30877c16f477SKamal Heib struct mlx5_ib_q_counter {
30887c16f477SKamal Heib 	const char *name;
30897c16f477SKamal Heib 	size_t offset;
30907c16f477SKamal Heib };
30917c16f477SKamal Heib 
30927c16f477SKamal Heib #define INIT_Q_COUNTER(_name)		\
30937c16f477SKamal Heib 	{ .name = #_name, .offset = MLX5_BYTE_OFF(query_q_counter_out, _name)}
30947c16f477SKamal Heib 
30957c16f477SKamal Heib static const struct mlx5_ib_q_counter basic_q_cnts[] = {
30967c16f477SKamal Heib 	INIT_Q_COUNTER(rx_write_requests),
30977c16f477SKamal Heib 	INIT_Q_COUNTER(rx_read_requests),
30987c16f477SKamal Heib 	INIT_Q_COUNTER(rx_atomic_requests),
30997c16f477SKamal Heib 	INIT_Q_COUNTER(out_of_buffer),
31007c16f477SKamal Heib };
31017c16f477SKamal Heib 
31027c16f477SKamal Heib static const struct mlx5_ib_q_counter out_of_seq_q_cnts[] = {
31037c16f477SKamal Heib 	INIT_Q_COUNTER(out_of_sequence),
31047c16f477SKamal Heib };
31057c16f477SKamal Heib 
31067c16f477SKamal Heib static const struct mlx5_ib_q_counter retrans_q_cnts[] = {
31077c16f477SKamal Heib 	INIT_Q_COUNTER(duplicate_request),
31087c16f477SKamal Heib 	INIT_Q_COUNTER(rnr_nak_retry_err),
31097c16f477SKamal Heib 	INIT_Q_COUNTER(packet_seq_err),
31107c16f477SKamal Heib 	INIT_Q_COUNTER(implied_nak_seq_err),
31117c16f477SKamal Heib 	INIT_Q_COUNTER(local_ack_timeout_err),
31127c16f477SKamal Heib };
31137c16f477SKamal Heib 
31140837e86aSMark Bloch static void mlx5_ib_dealloc_q_counters(struct mlx5_ib_dev *dev)
31150837e86aSMark Bloch {
31160837e86aSMark Bloch 	unsigned int i;
31170837e86aSMark Bloch 
31187c16f477SKamal Heib 	for (i = 0; i < dev->num_ports; i++) {
31190837e86aSMark Bloch 		mlx5_core_dealloc_q_counter(dev->mdev,
31207c16f477SKamal Heib 					    dev->port[i].q_cnts.set_id);
31217c16f477SKamal Heib 		kfree(dev->port[i].q_cnts.names);
31227c16f477SKamal Heib 		kfree(dev->port[i].q_cnts.offsets);
31237c16f477SKamal Heib 	}
31247c16f477SKamal Heib }
31257c16f477SKamal Heib 
31267c16f477SKamal Heib static int __mlx5_ib_alloc_q_counters(struct mlx5_ib_dev *dev,
31277c16f477SKamal Heib 				      const char ***names,
31287c16f477SKamal Heib 				      size_t **offsets,
31297c16f477SKamal Heib 				      u32 *num)
31307c16f477SKamal Heib {
31317c16f477SKamal Heib 	u32 num_counters;
31327c16f477SKamal Heib 
31337c16f477SKamal Heib 	num_counters = ARRAY_SIZE(basic_q_cnts);
31347c16f477SKamal Heib 
31357c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt))
31367c16f477SKamal Heib 		num_counters += ARRAY_SIZE(out_of_seq_q_cnts);
31377c16f477SKamal Heib 
31387c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters))
31397c16f477SKamal Heib 		num_counters += ARRAY_SIZE(retrans_q_cnts);
31407c16f477SKamal Heib 
31417c16f477SKamal Heib 	*names = kcalloc(num_counters, sizeof(**names), GFP_KERNEL);
31427c16f477SKamal Heib 	if (!*names)
31437c16f477SKamal Heib 		return -ENOMEM;
31447c16f477SKamal Heib 
31457c16f477SKamal Heib 	*offsets = kcalloc(num_counters, sizeof(**offsets), GFP_KERNEL);
31467c16f477SKamal Heib 	if (!*offsets)
31477c16f477SKamal Heib 		goto err_names;
31487c16f477SKamal Heib 
31497c16f477SKamal Heib 	*num = num_counters;
31507c16f477SKamal Heib 
31517c16f477SKamal Heib 	return 0;
31527c16f477SKamal Heib 
31537c16f477SKamal Heib err_names:
31547c16f477SKamal Heib 	kfree(*names);
31557c16f477SKamal Heib 	return -ENOMEM;
31567c16f477SKamal Heib }
31577c16f477SKamal Heib 
31587c16f477SKamal Heib static void mlx5_ib_fill_q_counters(struct mlx5_ib_dev *dev,
31597c16f477SKamal Heib 				    const char **names,
31607c16f477SKamal Heib 				    size_t *offsets)
31617c16f477SKamal Heib {
31627c16f477SKamal Heib 	int i;
31637c16f477SKamal Heib 	int j = 0;
31647c16f477SKamal Heib 
31657c16f477SKamal Heib 	for (i = 0; i < ARRAY_SIZE(basic_q_cnts); i++, j++) {
31667c16f477SKamal Heib 		names[j] = basic_q_cnts[i].name;
31677c16f477SKamal Heib 		offsets[j] = basic_q_cnts[i].offset;
31687c16f477SKamal Heib 	}
31697c16f477SKamal Heib 
31707c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt)) {
31717c16f477SKamal Heib 		for (i = 0; i < ARRAY_SIZE(out_of_seq_q_cnts); i++, j++) {
31727c16f477SKamal Heib 			names[j] = out_of_seq_q_cnts[i].name;
31737c16f477SKamal Heib 			offsets[j] = out_of_seq_q_cnts[i].offset;
31747c16f477SKamal Heib 		}
31757c16f477SKamal Heib 	}
31767c16f477SKamal Heib 
31777c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters)) {
31787c16f477SKamal Heib 		for (i = 0; i < ARRAY_SIZE(retrans_q_cnts); i++, j++) {
31797c16f477SKamal Heib 			names[j] = retrans_q_cnts[i].name;
31807c16f477SKamal Heib 			offsets[j] = retrans_q_cnts[i].offset;
31817c16f477SKamal Heib 		}
31827c16f477SKamal Heib 	}
31830837e86aSMark Bloch }
31840837e86aSMark Bloch 
31850837e86aSMark Bloch static int mlx5_ib_alloc_q_counters(struct mlx5_ib_dev *dev)
31860837e86aSMark Bloch {
31870837e86aSMark Bloch 	int i;
31880837e86aSMark Bloch 	int ret;
31890837e86aSMark Bloch 
31900837e86aSMark Bloch 	for (i = 0; i < dev->num_ports; i++) {
31917c16f477SKamal Heib 		struct mlx5_ib_port *port = &dev->port[i];
31927c16f477SKamal Heib 
31930837e86aSMark Bloch 		ret = mlx5_core_alloc_q_counter(dev->mdev,
31947c16f477SKamal Heib 						&port->q_cnts.set_id);
31950837e86aSMark Bloch 		if (ret) {
31960837e86aSMark Bloch 			mlx5_ib_warn(dev,
31970837e86aSMark Bloch 				     "couldn't allocate queue counter for port %d, err %d\n",
31980837e86aSMark Bloch 				     i + 1, ret);
31990837e86aSMark Bloch 			goto dealloc_counters;
32000837e86aSMark Bloch 		}
32017c16f477SKamal Heib 
32027c16f477SKamal Heib 		ret = __mlx5_ib_alloc_q_counters(dev,
32037c16f477SKamal Heib 						 &port->q_cnts.names,
32047c16f477SKamal Heib 						 &port->q_cnts.offsets,
32057c16f477SKamal Heib 						 &port->q_cnts.num_counters);
32067c16f477SKamal Heib 		if (ret)
32077c16f477SKamal Heib 			goto dealloc_counters;
32087c16f477SKamal Heib 
32097c16f477SKamal Heib 		mlx5_ib_fill_q_counters(dev, port->q_cnts.names,
32107c16f477SKamal Heib 					port->q_cnts.offsets);
32110837e86aSMark Bloch 	}
32120837e86aSMark Bloch 
32130837e86aSMark Bloch 	return 0;
32140837e86aSMark Bloch 
32150837e86aSMark Bloch dealloc_counters:
32160837e86aSMark Bloch 	while (--i >= 0)
32170837e86aSMark Bloch 		mlx5_core_dealloc_q_counter(dev->mdev,
32187c16f477SKamal Heib 					    dev->port[i].q_cnts.set_id);
32190837e86aSMark Bloch 
32200837e86aSMark Bloch 	return ret;
32210837e86aSMark Bloch }
32220837e86aSMark Bloch 
32230ad17a8fSMark Bloch static struct rdma_hw_stats *mlx5_ib_alloc_hw_stats(struct ib_device *ibdev,
32240ad17a8fSMark Bloch 						    u8 port_num)
32250ad17a8fSMark Bloch {
32267c16f477SKamal Heib 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
32277c16f477SKamal Heib 	struct mlx5_ib_port *port = &dev->port[port_num - 1];
32280ad17a8fSMark Bloch 
32290ad17a8fSMark Bloch 	/* We support only per port stats */
32300ad17a8fSMark Bloch 	if (port_num == 0)
32310ad17a8fSMark Bloch 		return NULL;
32320ad17a8fSMark Bloch 
32337c16f477SKamal Heib 	return rdma_alloc_hw_stats_struct(port->q_cnts.names,
32347c16f477SKamal Heib 					  port->q_cnts.num_counters,
32350ad17a8fSMark Bloch 					  RDMA_HW_STATS_DEFAULT_LIFESPAN);
32360ad17a8fSMark Bloch }
32370ad17a8fSMark Bloch 
32380ad17a8fSMark Bloch static int mlx5_ib_get_hw_stats(struct ib_device *ibdev,
32390ad17a8fSMark Bloch 				struct rdma_hw_stats *stats,
32407c16f477SKamal Heib 				u8 port_num, int index)
32410ad17a8fSMark Bloch {
32420ad17a8fSMark Bloch 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
32437c16f477SKamal Heib 	struct mlx5_ib_port *port = &dev->port[port_num - 1];
32440ad17a8fSMark Bloch 	int outlen = MLX5_ST_SZ_BYTES(query_q_counter_out);
32450ad17a8fSMark Bloch 	void *out;
32460ad17a8fSMark Bloch 	__be32 val;
32470ad17a8fSMark Bloch 	int ret;
32480ad17a8fSMark Bloch 	int i;
32490ad17a8fSMark Bloch 
32507c16f477SKamal Heib 	if (!stats)
32510ad17a8fSMark Bloch 		return -ENOSYS;
32520ad17a8fSMark Bloch 
32530ad17a8fSMark Bloch 	out = mlx5_vzalloc(outlen);
32540ad17a8fSMark Bloch 	if (!out)
32550ad17a8fSMark Bloch 		return -ENOMEM;
32560ad17a8fSMark Bloch 
32570ad17a8fSMark Bloch 	ret = mlx5_core_query_q_counter(dev->mdev,
32587c16f477SKamal Heib 					port->q_cnts.set_id, 0,
32590ad17a8fSMark Bloch 					out, outlen);
32600ad17a8fSMark Bloch 	if (ret)
32610ad17a8fSMark Bloch 		goto free;
32620ad17a8fSMark Bloch 
32637c16f477SKamal Heib 	for (i = 0; i < port->q_cnts.num_counters; i++) {
32647c16f477SKamal Heib 		val = *(__be32 *)(out + port->q_cnts.offsets[i]);
32650ad17a8fSMark Bloch 		stats->value[i] = (u64)be32_to_cpu(val);
32660ad17a8fSMark Bloch 	}
32677c16f477SKamal Heib 
32680ad17a8fSMark Bloch free:
32690ad17a8fSMark Bloch 	kvfree(out);
32707c16f477SKamal Heib 	return port->q_cnts.num_counters;
32710ad17a8fSMark Bloch }
32720ad17a8fSMark Bloch 
32739603b61dSJack Morgenstein static void *mlx5_ib_add(struct mlx5_core_dev *mdev)
3274e126ba97SEli Cohen {
3275e126ba97SEli Cohen 	struct mlx5_ib_dev *dev;
3276ebd61f68SAchiad Shochat 	enum rdma_link_layer ll;
3277ebd61f68SAchiad Shochat 	int port_type_cap;
32784babcf97SAviv Heller 	const char *name;
3279e126ba97SEli Cohen 	int err;
3280e126ba97SEli Cohen 	int i;
3281e126ba97SEli Cohen 
3282ebd61f68SAchiad Shochat 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
3283ebd61f68SAchiad Shochat 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
3284ebd61f68SAchiad Shochat 
3285e126ba97SEli Cohen 	printk_once(KERN_INFO "%s", mlx5_version);
3286e126ba97SEli Cohen 
3287e126ba97SEli Cohen 	dev = (struct mlx5_ib_dev *)ib_alloc_device(sizeof(*dev));
3288e126ba97SEli Cohen 	if (!dev)
32899603b61dSJack Morgenstein 		return NULL;
3290e126ba97SEli Cohen 
32919603b61dSJack Morgenstein 	dev->mdev = mdev;
3292e126ba97SEli Cohen 
32930837e86aSMark Bloch 	dev->port = kcalloc(MLX5_CAP_GEN(mdev, num_ports), sizeof(*dev->port),
32940837e86aSMark Bloch 			    GFP_KERNEL);
32950837e86aSMark Bloch 	if (!dev->port)
32960837e86aSMark Bloch 		goto err_dealloc;
32970837e86aSMark Bloch 
3298fc24fc5eSAchiad Shochat 	rwlock_init(&dev->roce.netdev_lock);
3299e126ba97SEli Cohen 	err = get_port_caps(dev);
3300e126ba97SEli Cohen 	if (err)
33010837e86aSMark Bloch 		goto err_free_port;
3302e126ba97SEli Cohen 
33031b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(dev))
3304e126ba97SEli Cohen 		get_ext_port_caps(dev);
3305e126ba97SEli Cohen 
33064babcf97SAviv Heller 	if (!mlx5_lag_is_active(mdev))
33074babcf97SAviv Heller 		name = "mlx5_%d";
33084babcf97SAviv Heller 	else
33094babcf97SAviv Heller 		name = "mlx5_bond_%d";
33104babcf97SAviv Heller 
33114babcf97SAviv Heller 	strlcpy(dev->ib_dev.name, name, IB_DEVICE_NAME_MAX);
3312e126ba97SEli Cohen 	dev->ib_dev.owner		= THIS_MODULE;
3313e126ba97SEli Cohen 	dev->ib_dev.node_type		= RDMA_NODE_IB_CA;
3314c6790aa9SSagi Grimberg 	dev->ib_dev.local_dma_lkey	= 0 /* not supported for now */;
3315938fe83cSSaeed Mahameed 	dev->num_ports		= MLX5_CAP_GEN(mdev, num_ports);
3316e126ba97SEli Cohen 	dev->ib_dev.phys_port_cnt     = dev->num_ports;
3317233d05d2SSaeed Mahameed 	dev->ib_dev.num_comp_vectors    =
3318233d05d2SSaeed Mahameed 		dev->mdev->priv.eq_table.num_comp_vectors;
3319e126ba97SEli Cohen 	dev->ib_dev.dma_device	= &mdev->pdev->dev;
3320e126ba97SEli Cohen 
3321e126ba97SEli Cohen 	dev->ib_dev.uverbs_abi_ver	= MLX5_IB_UVERBS_ABI_VERSION;
3322e126ba97SEli Cohen 	dev->ib_dev.uverbs_cmd_mask	=
3323e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_GET_CONTEXT)		|
3324e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_DEVICE)	|
3325e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_PORT)		|
3326e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_ALLOC_PD)		|
3327e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DEALLOC_PD)		|
332841c450fdSMoni Shoua 		(1ull << IB_USER_VERBS_CMD_CREATE_AH)		|
332941c450fdSMoni Shoua 		(1ull << IB_USER_VERBS_CMD_DESTROY_AH)		|
3330e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_REG_MR)		|
333156e11d62SNoa Osherovich 		(1ull << IB_USER_VERBS_CMD_REREG_MR)		|
3332e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DEREG_MR)		|
3333e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL)	|
3334e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_CQ)		|
3335e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_RESIZE_CQ)		|
3336e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_CQ)		|
3337e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_QP)		|
3338e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_MODIFY_QP)		|
3339e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_QP)		|
3340e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_QP)		|
3341e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_ATTACH_MCAST)	|
3342e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DETACH_MCAST)	|
3343e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_SRQ)		|
3344e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_MODIFY_SRQ)		|
3345e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_SRQ)		|
3346e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_SRQ)		|
3347e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_XSRQ)		|
3348e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_OPEN_QP);
33491707cb4aSHaggai Eran 	dev->ib_dev.uverbs_ex_cmd_mask =
3350d4584ddfSMatan Barak 		(1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE)	|
3351d4584ddfSMatan Barak 		(1ull << IB_USER_VERBS_EX_CMD_CREATE_CQ)	|
33527d29f349SBodong Wang 		(1ull << IB_USER_VERBS_EX_CMD_CREATE_QP)	|
33537d29f349SBodong Wang 		(1ull << IB_USER_VERBS_EX_CMD_MODIFY_QP);
3354e126ba97SEli Cohen 
3355e126ba97SEli Cohen 	dev->ib_dev.query_device	= mlx5_ib_query_device;
3356e126ba97SEli Cohen 	dev->ib_dev.query_port		= mlx5_ib_query_port;
3357ebd61f68SAchiad Shochat 	dev->ib_dev.get_link_layer	= mlx5_ib_port_link_layer;
3358fc24fc5eSAchiad Shochat 	if (ll == IB_LINK_LAYER_ETHERNET)
3359fc24fc5eSAchiad Shochat 		dev->ib_dev.get_netdev	= mlx5_ib_get_netdev;
3360e126ba97SEli Cohen 	dev->ib_dev.query_gid		= mlx5_ib_query_gid;
33613cca2606SAchiad Shochat 	dev->ib_dev.add_gid		= mlx5_ib_add_gid;
33623cca2606SAchiad Shochat 	dev->ib_dev.del_gid		= mlx5_ib_del_gid;
3363e126ba97SEli Cohen 	dev->ib_dev.query_pkey		= mlx5_ib_query_pkey;
3364e126ba97SEli Cohen 	dev->ib_dev.modify_device	= mlx5_ib_modify_device;
3365e126ba97SEli Cohen 	dev->ib_dev.modify_port		= mlx5_ib_modify_port;
3366e126ba97SEli Cohen 	dev->ib_dev.alloc_ucontext	= mlx5_ib_alloc_ucontext;
3367e126ba97SEli Cohen 	dev->ib_dev.dealloc_ucontext	= mlx5_ib_dealloc_ucontext;
3368e126ba97SEli Cohen 	dev->ib_dev.mmap		= mlx5_ib_mmap;
3369e126ba97SEli Cohen 	dev->ib_dev.alloc_pd		= mlx5_ib_alloc_pd;
3370e126ba97SEli Cohen 	dev->ib_dev.dealloc_pd		= mlx5_ib_dealloc_pd;
3371e126ba97SEli Cohen 	dev->ib_dev.create_ah		= mlx5_ib_create_ah;
3372e126ba97SEli Cohen 	dev->ib_dev.query_ah		= mlx5_ib_query_ah;
3373e126ba97SEli Cohen 	dev->ib_dev.destroy_ah		= mlx5_ib_destroy_ah;
3374e126ba97SEli Cohen 	dev->ib_dev.create_srq		= mlx5_ib_create_srq;
3375e126ba97SEli Cohen 	dev->ib_dev.modify_srq		= mlx5_ib_modify_srq;
3376e126ba97SEli Cohen 	dev->ib_dev.query_srq		= mlx5_ib_query_srq;
3377e126ba97SEli Cohen 	dev->ib_dev.destroy_srq		= mlx5_ib_destroy_srq;
3378e126ba97SEli Cohen 	dev->ib_dev.post_srq_recv	= mlx5_ib_post_srq_recv;
3379e126ba97SEli Cohen 	dev->ib_dev.create_qp		= mlx5_ib_create_qp;
3380e126ba97SEli Cohen 	dev->ib_dev.modify_qp		= mlx5_ib_modify_qp;
3381e126ba97SEli Cohen 	dev->ib_dev.query_qp		= mlx5_ib_query_qp;
3382e126ba97SEli Cohen 	dev->ib_dev.destroy_qp		= mlx5_ib_destroy_qp;
3383e126ba97SEli Cohen 	dev->ib_dev.post_send		= mlx5_ib_post_send;
3384e126ba97SEli Cohen 	dev->ib_dev.post_recv		= mlx5_ib_post_recv;
3385e126ba97SEli Cohen 	dev->ib_dev.create_cq		= mlx5_ib_create_cq;
3386e126ba97SEli Cohen 	dev->ib_dev.modify_cq		= mlx5_ib_modify_cq;
3387e126ba97SEli Cohen 	dev->ib_dev.resize_cq		= mlx5_ib_resize_cq;
3388e126ba97SEli Cohen 	dev->ib_dev.destroy_cq		= mlx5_ib_destroy_cq;
3389e126ba97SEli Cohen 	dev->ib_dev.poll_cq		= mlx5_ib_poll_cq;
3390e126ba97SEli Cohen 	dev->ib_dev.req_notify_cq	= mlx5_ib_arm_cq;
3391e126ba97SEli Cohen 	dev->ib_dev.get_dma_mr		= mlx5_ib_get_dma_mr;
3392e126ba97SEli Cohen 	dev->ib_dev.reg_user_mr		= mlx5_ib_reg_user_mr;
339356e11d62SNoa Osherovich 	dev->ib_dev.rereg_user_mr	= mlx5_ib_rereg_user_mr;
3394e126ba97SEli Cohen 	dev->ib_dev.dereg_mr		= mlx5_ib_dereg_mr;
3395e126ba97SEli Cohen 	dev->ib_dev.attach_mcast	= mlx5_ib_mcg_attach;
3396e126ba97SEli Cohen 	dev->ib_dev.detach_mcast	= mlx5_ib_mcg_detach;
3397e126ba97SEli Cohen 	dev->ib_dev.process_mad		= mlx5_ib_process_mad;
33989bee178bSSagi Grimberg 	dev->ib_dev.alloc_mr		= mlx5_ib_alloc_mr;
33998a187ee5SSagi Grimberg 	dev->ib_dev.map_mr_sg		= mlx5_ib_map_mr_sg;
3400d5436ba0SSagi Grimberg 	dev->ib_dev.check_mr_status	= mlx5_ib_check_mr_status;
34017738613eSIra Weiny 	dev->ib_dev.get_port_immutable  = mlx5_port_immutable;
3402c7342823SIra Weiny 	dev->ib_dev.get_dev_fw_str      = get_dev_fw_str;
3403eff901d3SEli Cohen 	if (mlx5_core_is_pf(mdev)) {
3404eff901d3SEli Cohen 		dev->ib_dev.get_vf_config	= mlx5_ib_get_vf_config;
3405eff901d3SEli Cohen 		dev->ib_dev.set_vf_link_state	= mlx5_ib_set_vf_link_state;
3406eff901d3SEli Cohen 		dev->ib_dev.get_vf_stats	= mlx5_ib_get_vf_stats;
3407eff901d3SEli Cohen 		dev->ib_dev.set_vf_guid		= mlx5_ib_set_vf_guid;
3408eff901d3SEli Cohen 	}
3409e126ba97SEli Cohen 
34107c2344c3SMaor Gottlieb 	dev->ib_dev.disassociate_ucontext = mlx5_ib_disassociate_ucontext;
34117c2344c3SMaor Gottlieb 
3412938fe83cSSaeed Mahameed 	mlx5_ib_internal_fill_odp_caps(dev);
34138cdd312cSHaggai Eran 
3414d2370e0aSMatan Barak 	if (MLX5_CAP_GEN(mdev, imaicl)) {
3415d2370e0aSMatan Barak 		dev->ib_dev.alloc_mw		= mlx5_ib_alloc_mw;
3416d2370e0aSMatan Barak 		dev->ib_dev.dealloc_mw		= mlx5_ib_dealloc_mw;
3417d2370e0aSMatan Barak 		dev->ib_dev.uverbs_cmd_mask |=
3418d2370e0aSMatan Barak 			(1ull << IB_USER_VERBS_CMD_ALLOC_MW)	|
3419d2370e0aSMatan Barak 			(1ull << IB_USER_VERBS_CMD_DEALLOC_MW);
3420d2370e0aSMatan Barak 	}
3421d2370e0aSMatan Barak 
34227c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt)) {
34230ad17a8fSMark Bloch 		dev->ib_dev.get_hw_stats	= mlx5_ib_get_hw_stats;
34240ad17a8fSMark Bloch 		dev->ib_dev.alloc_hw_stats	= mlx5_ib_alloc_hw_stats;
34250ad17a8fSMark Bloch 	}
34260ad17a8fSMark Bloch 
3427938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, xrc)) {
3428e126ba97SEli Cohen 		dev->ib_dev.alloc_xrcd = mlx5_ib_alloc_xrcd;
3429e126ba97SEli Cohen 		dev->ib_dev.dealloc_xrcd = mlx5_ib_dealloc_xrcd;
3430e126ba97SEli Cohen 		dev->ib_dev.uverbs_cmd_mask |=
3431e126ba97SEli Cohen 			(1ull << IB_USER_VERBS_CMD_OPEN_XRCD) |
3432e126ba97SEli Cohen 			(1ull << IB_USER_VERBS_CMD_CLOSE_XRCD);
3433e126ba97SEli Cohen 	}
3434e126ba97SEli Cohen 
3435048ccca8SLinus Torvalds 	if (mlx5_ib_port_link_layer(&dev->ib_dev, 1) ==
3436038d2ef8SMaor Gottlieb 	    IB_LINK_LAYER_ETHERNET) {
3437038d2ef8SMaor Gottlieb 		dev->ib_dev.create_flow	= mlx5_ib_create_flow;
3438038d2ef8SMaor Gottlieb 		dev->ib_dev.destroy_flow = mlx5_ib_destroy_flow;
343979b20a6cSYishai Hadas 		dev->ib_dev.create_wq	 = mlx5_ib_create_wq;
344079b20a6cSYishai Hadas 		dev->ib_dev.modify_wq	 = mlx5_ib_modify_wq;
344179b20a6cSYishai Hadas 		dev->ib_dev.destroy_wq	 = mlx5_ib_destroy_wq;
3442c5f90929SYishai Hadas 		dev->ib_dev.create_rwq_ind_table = mlx5_ib_create_rwq_ind_table;
3443c5f90929SYishai Hadas 		dev->ib_dev.destroy_rwq_ind_table = mlx5_ib_destroy_rwq_ind_table;
3444038d2ef8SMaor Gottlieb 		dev->ib_dev.uverbs_ex_cmd_mask |=
3445038d2ef8SMaor Gottlieb 			(1ull << IB_USER_VERBS_EX_CMD_CREATE_FLOW) |
344679b20a6cSYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_DESTROY_FLOW) |
344779b20a6cSYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_CREATE_WQ) |
344879b20a6cSYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_MODIFY_WQ) |
3449c5f90929SYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_DESTROY_WQ) |
3450c5f90929SYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_CREATE_RWQ_IND_TBL) |
3451c5f90929SYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_DESTROY_RWQ_IND_TBL);
3452038d2ef8SMaor Gottlieb 	}
3453e126ba97SEli Cohen 	err = init_node_data(dev);
3454e126ba97SEli Cohen 	if (err)
345590be7c8aSMajd Dibbiny 		goto err_free_port;
3456e126ba97SEli Cohen 
3457038d2ef8SMaor Gottlieb 	mutex_init(&dev->flow_db.lock);
3458e126ba97SEli Cohen 	mutex_init(&dev->cap_mask_mutex);
345989ea94a7SMaor Gottlieb 	INIT_LIST_HEAD(&dev->qp_list);
346089ea94a7SMaor Gottlieb 	spin_lock_init(&dev->reset_flow_resource_lock);
3461e126ba97SEli Cohen 
3462fc24fc5eSAchiad Shochat 	if (ll == IB_LINK_LAYER_ETHERNET) {
346345f95acdSOr Gerlitz 		err = mlx5_enable_eth(dev);
3464e126ba97SEli Cohen 		if (err)
346590be7c8aSMajd Dibbiny 			goto err_free_port;
3466fc24fc5eSAchiad Shochat 	}
3467fc24fc5eSAchiad Shochat 
3468fc24fc5eSAchiad Shochat 	err = create_dev_resources(&dev->devr);
3469fc24fc5eSAchiad Shochat 	if (err)
347045f95acdSOr Gerlitz 		goto err_disable_eth;
3471e126ba97SEli Cohen 
34726aec21f6SHaggai Eran 	err = mlx5_ib_odp_init_one(dev);
3473281d1a92SWei Yongjun 	if (err)
3474e126ba97SEli Cohen 		goto err_rsrc;
3475e126ba97SEli Cohen 
347645bded2cSKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt)) {
34770837e86aSMark Bloch 		err = mlx5_ib_alloc_q_counters(dev);
34786aec21f6SHaggai Eran 		if (err)
34796aec21f6SHaggai Eran 			goto err_odp;
348045bded2cSKamal Heib 	}
34816aec21f6SHaggai Eran 
34825fe9dec0SEli Cohen 	dev->mdev->priv.uar = mlx5_get_uars_page(dev->mdev);
34835fe9dec0SEli Cohen 	if (!dev->mdev->priv.uar)
34845fe9dec0SEli Cohen 		goto err_q_cnt;
34855fe9dec0SEli Cohen 
34865fe9dec0SEli Cohen 	err = mlx5_alloc_bfreg(dev->mdev, &dev->bfreg, false, false);
34875fe9dec0SEli Cohen 	if (err)
34885fe9dec0SEli Cohen 		goto err_uar_page;
34895fe9dec0SEli Cohen 
34905fe9dec0SEli Cohen 	err = mlx5_alloc_bfreg(dev->mdev, &dev->fp_bfreg, false, true);
34915fe9dec0SEli Cohen 	if (err)
34925fe9dec0SEli Cohen 		goto err_bfreg;
34935fe9dec0SEli Cohen 
34940837e86aSMark Bloch 	err = ib_register_device(&dev->ib_dev, NULL);
34950837e86aSMark Bloch 	if (err)
34965fe9dec0SEli Cohen 		goto err_fp_bfreg;
34970837e86aSMark Bloch 
3498e126ba97SEli Cohen 	err = create_umr_res(dev);
3499e126ba97SEli Cohen 	if (err)
3500e126ba97SEli Cohen 		goto err_dev;
3501e126ba97SEli Cohen 
3502e126ba97SEli Cohen 	for (i = 0; i < ARRAY_SIZE(mlx5_class_attributes); i++) {
3503281d1a92SWei Yongjun 		err = device_create_file(&dev->ib_dev.dev,
3504281d1a92SWei Yongjun 					 mlx5_class_attributes[i]);
3505281d1a92SWei Yongjun 		if (err)
3506e126ba97SEli Cohen 			goto err_umrc;
3507e126ba97SEli Cohen 	}
3508e126ba97SEli Cohen 
3509e126ba97SEli Cohen 	dev->ib_active = true;
3510e126ba97SEli Cohen 
35119603b61dSJack Morgenstein 	return dev;
3512e126ba97SEli Cohen 
3513e126ba97SEli Cohen err_umrc:
3514e126ba97SEli Cohen 	destroy_umrc_res(dev);
3515e126ba97SEli Cohen 
3516e126ba97SEli Cohen err_dev:
3517e126ba97SEli Cohen 	ib_unregister_device(&dev->ib_dev);
3518e126ba97SEli Cohen 
35195fe9dec0SEli Cohen err_fp_bfreg:
35205fe9dec0SEli Cohen 	mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
35215fe9dec0SEli Cohen 
35225fe9dec0SEli Cohen err_bfreg:
35235fe9dec0SEli Cohen 	mlx5_free_bfreg(dev->mdev, &dev->bfreg);
35245fe9dec0SEli Cohen 
35255fe9dec0SEli Cohen err_uar_page:
35265fe9dec0SEli Cohen 	mlx5_put_uars_page(dev->mdev, dev->mdev->priv.uar);
35275fe9dec0SEli Cohen 
35280837e86aSMark Bloch err_q_cnt:
352945bded2cSKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt))
35300837e86aSMark Bloch 		mlx5_ib_dealloc_q_counters(dev);
35310837e86aSMark Bloch 
35326aec21f6SHaggai Eran err_odp:
35336aec21f6SHaggai Eran 	mlx5_ib_odp_remove_one(dev);
35346aec21f6SHaggai Eran 
3535e126ba97SEli Cohen err_rsrc:
3536e126ba97SEli Cohen 	destroy_dev_resources(&dev->devr);
3537e126ba97SEli Cohen 
353845f95acdSOr Gerlitz err_disable_eth:
35395ec8c83eSAviv Heller 	if (ll == IB_LINK_LAYER_ETHERNET) {
354045f95acdSOr Gerlitz 		mlx5_disable_eth(dev);
3541d012f5d6SOr Gerlitz 		mlx5_remove_netdev_notifier(dev);
35425ec8c83eSAviv Heller 	}
3543fc24fc5eSAchiad Shochat 
35440837e86aSMark Bloch err_free_port:
35450837e86aSMark Bloch 	kfree(dev->port);
35460837e86aSMark Bloch 
35479603b61dSJack Morgenstein err_dealloc:
3548e126ba97SEli Cohen 	ib_dealloc_device((struct ib_device *)dev);
3549e126ba97SEli Cohen 
35509603b61dSJack Morgenstein 	return NULL;
3551e126ba97SEli Cohen }
3552e126ba97SEli Cohen 
35539603b61dSJack Morgenstein static void mlx5_ib_remove(struct mlx5_core_dev *mdev, void *context)
3554e126ba97SEli Cohen {
35559603b61dSJack Morgenstein 	struct mlx5_ib_dev *dev = context;
3556fc24fc5eSAchiad Shochat 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev, 1);
35576aec21f6SHaggai Eran 
3558d012f5d6SOr Gerlitz 	mlx5_remove_netdev_notifier(dev);
3559e126ba97SEli Cohen 	ib_unregister_device(&dev->ib_dev);
35605fe9dec0SEli Cohen 	mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
35615fe9dec0SEli Cohen 	mlx5_free_bfreg(dev->mdev, &dev->bfreg);
35625fe9dec0SEli Cohen 	mlx5_put_uars_page(dev->mdev, mdev->priv.uar);
356345bded2cSKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt))
35640837e86aSMark Bloch 		mlx5_ib_dealloc_q_counters(dev);
3565eefd56e5SEli Cohen 	destroy_umrc_res(dev);
35666aec21f6SHaggai Eran 	mlx5_ib_odp_remove_one(dev);
3567e126ba97SEli Cohen 	destroy_dev_resources(&dev->devr);
3568fc24fc5eSAchiad Shochat 	if (ll == IB_LINK_LAYER_ETHERNET)
356945f95acdSOr Gerlitz 		mlx5_disable_eth(dev);
35700837e86aSMark Bloch 	kfree(dev->port);
3571e126ba97SEli Cohen 	ib_dealloc_device(&dev->ib_dev);
3572e126ba97SEli Cohen }
3573e126ba97SEli Cohen 
35749603b61dSJack Morgenstein static struct mlx5_interface mlx5_ib_interface = {
35759603b61dSJack Morgenstein 	.add            = mlx5_ib_add,
35769603b61dSJack Morgenstein 	.remove         = mlx5_ib_remove,
35779603b61dSJack Morgenstein 	.event          = mlx5_ib_event,
3578d9aaed83SArtemy Kovalyov #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
3579d9aaed83SArtemy Kovalyov 	.pfault		= mlx5_ib_pfault,
3580d9aaed83SArtemy Kovalyov #endif
358164613d94SSaeed Mahameed 	.protocol	= MLX5_INTERFACE_PROTOCOL_IB,
3582e126ba97SEli Cohen };
3583e126ba97SEli Cohen 
3584e126ba97SEli Cohen static int __init mlx5_ib_init(void)
3585e126ba97SEli Cohen {
35866aec21f6SHaggai Eran 	int err;
35876aec21f6SHaggai Eran 
358881713d37SArtemy Kovalyov 	mlx5_ib_odp_init();
358981713d37SArtemy Kovalyov 
35906aec21f6SHaggai Eran 	err = mlx5_register_interface(&mlx5_ib_interface);
35916aec21f6SHaggai Eran 
35926aec21f6SHaggai Eran 	return err;
3593e126ba97SEli Cohen }
3594e126ba97SEli Cohen 
3595e126ba97SEli Cohen static void __exit mlx5_ib_cleanup(void)
3596e126ba97SEli Cohen {
35979603b61dSJack Morgenstein 	mlx5_unregister_interface(&mlx5_ib_interface);
3598e126ba97SEli Cohen }
3599e126ba97SEli Cohen 
3600e126ba97SEli Cohen module_init(mlx5_ib_init);
3601e126ba97SEli Cohen module_exit(mlx5_ib_cleanup);
3602