xref: /openbmc/linux/drivers/infiniband/hw/mlx5/main.c (revision 1377661298d2820d675553d186c31b6f46c140d0)
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>
446e84f315SIngo Molnar #include <linux/sched/mm.h>
450881e7bdSIngo Molnar #include <linux/sched/task.h>
467c2344c3SMaor Gottlieb #include <linux/delay.h>
47e126ba97SEli Cohen #include <rdma/ib_user_verbs.h>
483f89a643SAchiad Shochat #include <rdma/ib_addr.h>
492811ba51SAchiad Shochat #include <rdma/ib_cache.h>
50ada68c31SAchiad Shochat #include <linux/mlx5/port.h>
511b5daf11SMajd Dibbiny #include <linux/mlx5/vport.h>
527c2344c3SMaor Gottlieb #include <linux/list.h>
53e126ba97SEli Cohen #include <rdma/ib_smi.h>
54e126ba97SEli Cohen #include <rdma/ib_umem.h>
55038d2ef8SMaor Gottlieb #include <linux/in.h>
56038d2ef8SMaor Gottlieb #include <linux/etherdevice.h>
57038d2ef8SMaor Gottlieb #include <linux/mlx5/fs.h>
5878984898SOr Gerlitz #include <linux/mlx5/vport.h>
59e126ba97SEli Cohen #include "mlx5_ib.h"
60e126ba97SEli Cohen 
61e126ba97SEli Cohen #define DRIVER_NAME "mlx5_ib"
62169a1d85SAmir Vadai #define DRIVER_VERSION "2.2-1"
63169a1d85SAmir Vadai #define DRIVER_RELDATE	"Feb 2014"
64e126ba97SEli Cohen 
65e126ba97SEli Cohen MODULE_AUTHOR("Eli Cohen <eli@mellanox.com>");
66e126ba97SEli Cohen MODULE_DESCRIPTION("Mellanox Connect-IB HCA IB driver");
67e126ba97SEli Cohen MODULE_LICENSE("Dual BSD/GPL");
68e126ba97SEli Cohen MODULE_VERSION(DRIVER_VERSION);
69e126ba97SEli Cohen 
70e126ba97SEli Cohen static char mlx5_version[] =
71e126ba97SEli Cohen 	DRIVER_NAME ": Mellanox Connect-IB Infiniband driver v"
72e126ba97SEli Cohen 	DRIVER_VERSION " (" DRIVER_RELDATE ")\n";
73e126ba97SEli Cohen 
74da7525d2SEran Ben Elisha enum {
75da7525d2SEran Ben Elisha 	MLX5_ATOMIC_SIZE_QP_8BYTES = 1 << 3,
76da7525d2SEran Ben Elisha };
771b5daf11SMajd Dibbiny 
781b5daf11SMajd Dibbiny static enum rdma_link_layer
79ebd61f68SAchiad Shochat mlx5_port_type_cap_to_rdma_ll(int port_type_cap)
801b5daf11SMajd Dibbiny {
81ebd61f68SAchiad Shochat 	switch (port_type_cap) {
821b5daf11SMajd Dibbiny 	case MLX5_CAP_PORT_TYPE_IB:
831b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_INFINIBAND;
841b5daf11SMajd Dibbiny 	case MLX5_CAP_PORT_TYPE_ETH:
851b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_ETHERNET;
861b5daf11SMajd Dibbiny 	default:
871b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_UNSPECIFIED;
881b5daf11SMajd Dibbiny 	}
891b5daf11SMajd Dibbiny }
901b5daf11SMajd Dibbiny 
91ebd61f68SAchiad Shochat static enum rdma_link_layer
92ebd61f68SAchiad Shochat mlx5_ib_port_link_layer(struct ib_device *device, u8 port_num)
93ebd61f68SAchiad Shochat {
94ebd61f68SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(device);
95ebd61f68SAchiad Shochat 	int port_type_cap = MLX5_CAP_GEN(dev->mdev, port_type);
96ebd61f68SAchiad Shochat 
97ebd61f68SAchiad Shochat 	return mlx5_port_type_cap_to_rdma_ll(port_type_cap);
98ebd61f68SAchiad Shochat }
99ebd61f68SAchiad Shochat 
100fc24fc5eSAchiad Shochat static int mlx5_netdev_event(struct notifier_block *this,
101fc24fc5eSAchiad Shochat 			     unsigned long event, void *ptr)
102fc24fc5eSAchiad Shochat {
103fc24fc5eSAchiad Shochat 	struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
104fc24fc5eSAchiad Shochat 	struct mlx5_ib_dev *ibdev = container_of(this, struct mlx5_ib_dev,
105fc24fc5eSAchiad Shochat 						 roce.nb);
106fc24fc5eSAchiad Shochat 
1075ec8c83eSAviv Heller 	switch (event) {
1085ec8c83eSAviv Heller 	case NETDEV_REGISTER:
1095ec8c83eSAviv Heller 	case NETDEV_UNREGISTER:
110fc24fc5eSAchiad Shochat 		write_lock(&ibdev->roce.netdev_lock);
111fc24fc5eSAchiad Shochat 		if (ndev->dev.parent == &ibdev->mdev->pdev->dev)
1125ec8c83eSAviv Heller 			ibdev->roce.netdev = (event == NETDEV_UNREGISTER) ?
1135ec8c83eSAviv Heller 					     NULL : ndev;
114fc24fc5eSAchiad Shochat 		write_unlock(&ibdev->roce.netdev_lock);
1155ec8c83eSAviv Heller 		break;
1165ec8c83eSAviv Heller 
1175ec8c83eSAviv Heller 	case NETDEV_UP:
11888621dfeSAviv Heller 	case NETDEV_DOWN: {
11988621dfeSAviv Heller 		struct net_device *lag_ndev = mlx5_lag_get_roce_netdev(ibdev->mdev);
12088621dfeSAviv Heller 		struct net_device *upper = NULL;
12188621dfeSAviv Heller 
12288621dfeSAviv Heller 		if (lag_ndev) {
12388621dfeSAviv Heller 			upper = netdev_master_upper_dev_get(lag_ndev);
12488621dfeSAviv Heller 			dev_put(lag_ndev);
12588621dfeSAviv Heller 		}
12688621dfeSAviv Heller 
12788621dfeSAviv Heller 		if ((upper == ndev || (!upper && ndev == ibdev->roce.netdev))
12888621dfeSAviv Heller 		    && ibdev->ib_active) {
129626bc02dSBart Van Assche 			struct ib_event ibev = { };
1305ec8c83eSAviv Heller 
1315ec8c83eSAviv Heller 			ibev.device = &ibdev->ib_dev;
1325ec8c83eSAviv Heller 			ibev.event = (event == NETDEV_UP) ?
1335ec8c83eSAviv Heller 				     IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
1345ec8c83eSAviv Heller 			ibev.element.port_num = 1;
1355ec8c83eSAviv Heller 			ib_dispatch_event(&ibev);
1365ec8c83eSAviv Heller 		}
1375ec8c83eSAviv Heller 		break;
13888621dfeSAviv Heller 	}
1395ec8c83eSAviv Heller 
1405ec8c83eSAviv Heller 	default:
1415ec8c83eSAviv Heller 		break;
1425ec8c83eSAviv Heller 	}
143fc24fc5eSAchiad Shochat 
144fc24fc5eSAchiad Shochat 	return NOTIFY_DONE;
145fc24fc5eSAchiad Shochat }
146fc24fc5eSAchiad Shochat 
147fc24fc5eSAchiad Shochat static struct net_device *mlx5_ib_get_netdev(struct ib_device *device,
148fc24fc5eSAchiad Shochat 					     u8 port_num)
149fc24fc5eSAchiad Shochat {
150fc24fc5eSAchiad Shochat 	struct mlx5_ib_dev *ibdev = to_mdev(device);
151fc24fc5eSAchiad Shochat 	struct net_device *ndev;
152fc24fc5eSAchiad Shochat 
15388621dfeSAviv Heller 	ndev = mlx5_lag_get_roce_netdev(ibdev->mdev);
15488621dfeSAviv Heller 	if (ndev)
15588621dfeSAviv Heller 		return ndev;
15688621dfeSAviv Heller 
157fc24fc5eSAchiad Shochat 	/* Ensure ndev does not disappear before we invoke dev_hold()
158fc24fc5eSAchiad Shochat 	 */
159fc24fc5eSAchiad Shochat 	read_lock(&ibdev->roce.netdev_lock);
160fc24fc5eSAchiad Shochat 	ndev = ibdev->roce.netdev;
161fc24fc5eSAchiad Shochat 	if (ndev)
162fc24fc5eSAchiad Shochat 		dev_hold(ndev);
163fc24fc5eSAchiad Shochat 	read_unlock(&ibdev->roce.netdev_lock);
164fc24fc5eSAchiad Shochat 
165fc24fc5eSAchiad Shochat 	return ndev;
166fc24fc5eSAchiad Shochat }
167fc24fc5eSAchiad Shochat 
1683f89a643SAchiad Shochat static int mlx5_query_port_roce(struct ib_device *device, u8 port_num,
1693f89a643SAchiad Shochat 				struct ib_port_attr *props)
1703f89a643SAchiad Shochat {
1713f89a643SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(device);
17288621dfeSAviv Heller 	struct net_device *ndev, *upper;
1733f89a643SAchiad Shochat 	enum ib_mtu ndev_ib_mtu;
174c876a1b7SLeon Romanovsky 	u16 qkey_viol_cntr;
1753f89a643SAchiad Shochat 
176c4550c63SOr Gerlitz 	/* props being zeroed by the caller, avoid zeroing it here */
1773f89a643SAchiad Shochat 
1783f89a643SAchiad Shochat 	props->port_cap_flags  |= IB_PORT_CM_SUP;
1793f89a643SAchiad Shochat 	props->port_cap_flags  |= IB_PORT_IP_BASED_GIDS;
1803f89a643SAchiad Shochat 
1813f89a643SAchiad Shochat 	props->gid_tbl_len      = MLX5_CAP_ROCE(dev->mdev,
1823f89a643SAchiad Shochat 						roce_address_table_size);
1833f89a643SAchiad Shochat 	props->max_mtu          = IB_MTU_4096;
1843f89a643SAchiad Shochat 	props->max_msg_sz       = 1 << MLX5_CAP_GEN(dev->mdev, log_max_msg);
1853f89a643SAchiad Shochat 	props->pkey_tbl_len     = 1;
1863f89a643SAchiad Shochat 	props->state            = IB_PORT_DOWN;
1873f89a643SAchiad Shochat 	props->phys_state       = 3;
1883f89a643SAchiad Shochat 
189c876a1b7SLeon Romanovsky 	mlx5_query_nic_vport_qkey_viol_cntr(dev->mdev, &qkey_viol_cntr);
190c876a1b7SLeon Romanovsky 	props->qkey_viol_cntr = qkey_viol_cntr;
1913f89a643SAchiad Shochat 
1923f89a643SAchiad Shochat 	ndev = mlx5_ib_get_netdev(device, port_num);
1933f89a643SAchiad Shochat 	if (!ndev)
1943f89a643SAchiad Shochat 		return 0;
1953f89a643SAchiad Shochat 
19688621dfeSAviv Heller 	if (mlx5_lag_is_active(dev->mdev)) {
19788621dfeSAviv Heller 		rcu_read_lock();
19888621dfeSAviv Heller 		upper = netdev_master_upper_dev_get_rcu(ndev);
19988621dfeSAviv Heller 		if (upper) {
20088621dfeSAviv Heller 			dev_put(ndev);
20188621dfeSAviv Heller 			ndev = upper;
20288621dfeSAviv Heller 			dev_hold(ndev);
20388621dfeSAviv Heller 		}
20488621dfeSAviv Heller 		rcu_read_unlock();
20588621dfeSAviv Heller 	}
20688621dfeSAviv Heller 
2073f89a643SAchiad Shochat 	if (netif_running(ndev) && netif_carrier_ok(ndev)) {
2083f89a643SAchiad Shochat 		props->state      = IB_PORT_ACTIVE;
2093f89a643SAchiad Shochat 		props->phys_state = 5;
2103f89a643SAchiad Shochat 	}
2113f89a643SAchiad Shochat 
2123f89a643SAchiad Shochat 	ndev_ib_mtu = iboe_get_mtu(ndev->mtu);
2133f89a643SAchiad Shochat 
2143f89a643SAchiad Shochat 	dev_put(ndev);
2153f89a643SAchiad Shochat 
2163f89a643SAchiad Shochat 	props->active_mtu	= min(props->max_mtu, ndev_ib_mtu);
2173f89a643SAchiad Shochat 
2183f89a643SAchiad Shochat 	props->active_width	= IB_WIDTH_4X;  /* TODO */
2193f89a643SAchiad Shochat 	props->active_speed	= IB_SPEED_QDR; /* TODO */
2203f89a643SAchiad Shochat 
2213f89a643SAchiad Shochat 	return 0;
2223f89a643SAchiad Shochat }
2233f89a643SAchiad Shochat 
2243cca2606SAchiad Shochat static void ib_gid_to_mlx5_roce_addr(const union ib_gid *gid,
2253cca2606SAchiad Shochat 				     const struct ib_gid_attr *attr,
2263cca2606SAchiad Shochat 				     void *mlx5_addr)
2273cca2606SAchiad Shochat {
2283cca2606SAchiad Shochat #define MLX5_SET_RA(p, f, v) MLX5_SET(roce_addr_layout, p, f, v)
2293cca2606SAchiad Shochat 	char *mlx5_addr_l3_addr	= MLX5_ADDR_OF(roce_addr_layout, mlx5_addr,
2303cca2606SAchiad Shochat 					       source_l3_address);
2313cca2606SAchiad Shochat 	void *mlx5_addr_mac	= MLX5_ADDR_OF(roce_addr_layout, mlx5_addr,
2323cca2606SAchiad Shochat 					       source_mac_47_32);
2333cca2606SAchiad Shochat 
2343cca2606SAchiad Shochat 	if (!gid)
2353cca2606SAchiad Shochat 		return;
2363cca2606SAchiad Shochat 
2373cca2606SAchiad Shochat 	ether_addr_copy(mlx5_addr_mac, attr->ndev->dev_addr);
2383cca2606SAchiad Shochat 
2393cca2606SAchiad Shochat 	if (is_vlan_dev(attr->ndev)) {
2403cca2606SAchiad Shochat 		MLX5_SET_RA(mlx5_addr, vlan_valid, 1);
2413cca2606SAchiad Shochat 		MLX5_SET_RA(mlx5_addr, vlan_id, vlan_dev_vlan_id(attr->ndev));
2423cca2606SAchiad Shochat 	}
2433cca2606SAchiad Shochat 
2443cca2606SAchiad Shochat 	switch (attr->gid_type) {
2453cca2606SAchiad Shochat 	case IB_GID_TYPE_IB:
2463cca2606SAchiad Shochat 		MLX5_SET_RA(mlx5_addr, roce_version, MLX5_ROCE_VERSION_1);
2473cca2606SAchiad Shochat 		break;
2483cca2606SAchiad Shochat 	case IB_GID_TYPE_ROCE_UDP_ENCAP:
2493cca2606SAchiad Shochat 		MLX5_SET_RA(mlx5_addr, roce_version, MLX5_ROCE_VERSION_2);
2503cca2606SAchiad Shochat 		break;
2513cca2606SAchiad Shochat 
2523cca2606SAchiad Shochat 	default:
2533cca2606SAchiad Shochat 		WARN_ON(true);
2543cca2606SAchiad Shochat 	}
2553cca2606SAchiad Shochat 
2563cca2606SAchiad Shochat 	if (attr->gid_type != IB_GID_TYPE_IB) {
2573cca2606SAchiad Shochat 		if (ipv6_addr_v4mapped((void *)gid))
2583cca2606SAchiad Shochat 			MLX5_SET_RA(mlx5_addr, roce_l3_type,
2593cca2606SAchiad Shochat 				    MLX5_ROCE_L3_TYPE_IPV4);
2603cca2606SAchiad Shochat 		else
2613cca2606SAchiad Shochat 			MLX5_SET_RA(mlx5_addr, roce_l3_type,
2623cca2606SAchiad Shochat 				    MLX5_ROCE_L3_TYPE_IPV6);
2633cca2606SAchiad Shochat 	}
2643cca2606SAchiad Shochat 
2653cca2606SAchiad Shochat 	if ((attr->gid_type == IB_GID_TYPE_IB) ||
2663cca2606SAchiad Shochat 	    !ipv6_addr_v4mapped((void *)gid))
2673cca2606SAchiad Shochat 		memcpy(mlx5_addr_l3_addr, gid, sizeof(*gid));
2683cca2606SAchiad Shochat 	else
2693cca2606SAchiad Shochat 		memcpy(&mlx5_addr_l3_addr[12], &gid->raw[12], 4);
2703cca2606SAchiad Shochat }
2713cca2606SAchiad Shochat 
2723cca2606SAchiad Shochat static int set_roce_addr(struct ib_device *device, u8 port_num,
2733cca2606SAchiad Shochat 			 unsigned int index,
2743cca2606SAchiad Shochat 			 const union ib_gid *gid,
2753cca2606SAchiad Shochat 			 const struct ib_gid_attr *attr)
2763cca2606SAchiad Shochat {
2773cca2606SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(device);
278c4f287c4SSaeed Mahameed 	u32  in[MLX5_ST_SZ_DW(set_roce_address_in)]  = {0};
279c4f287c4SSaeed Mahameed 	u32 out[MLX5_ST_SZ_DW(set_roce_address_out)] = {0};
2803cca2606SAchiad Shochat 	void *in_addr = MLX5_ADDR_OF(set_roce_address_in, in, roce_address);
2813cca2606SAchiad Shochat 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(device, port_num);
2823cca2606SAchiad Shochat 
2833cca2606SAchiad Shochat 	if (ll != IB_LINK_LAYER_ETHERNET)
2843cca2606SAchiad Shochat 		return -EINVAL;
2853cca2606SAchiad Shochat 
2863cca2606SAchiad Shochat 	ib_gid_to_mlx5_roce_addr(gid, attr, in_addr);
2873cca2606SAchiad Shochat 
2883cca2606SAchiad Shochat 	MLX5_SET(set_roce_address_in, in, roce_address_index, index);
2893cca2606SAchiad Shochat 	MLX5_SET(set_roce_address_in, in, opcode, MLX5_CMD_OP_SET_ROCE_ADDRESS);
2903cca2606SAchiad Shochat 	return mlx5_cmd_exec(dev->mdev, in, sizeof(in), out, sizeof(out));
2913cca2606SAchiad Shochat }
2923cca2606SAchiad Shochat 
2933cca2606SAchiad Shochat static int mlx5_ib_add_gid(struct ib_device *device, u8 port_num,
2943cca2606SAchiad Shochat 			   unsigned int index, const union ib_gid *gid,
2953cca2606SAchiad Shochat 			   const struct ib_gid_attr *attr,
2963cca2606SAchiad Shochat 			   __always_unused void **context)
2973cca2606SAchiad Shochat {
2983cca2606SAchiad Shochat 	return set_roce_addr(device, port_num, index, gid, attr);
2993cca2606SAchiad Shochat }
3003cca2606SAchiad Shochat 
3013cca2606SAchiad Shochat static int mlx5_ib_del_gid(struct ib_device *device, u8 port_num,
3023cca2606SAchiad Shochat 			   unsigned int index, __always_unused void **context)
3033cca2606SAchiad Shochat {
3043cca2606SAchiad Shochat 	return set_roce_addr(device, port_num, index, NULL, NULL);
3053cca2606SAchiad Shochat }
3063cca2606SAchiad Shochat 
3072811ba51SAchiad Shochat __be16 mlx5_get_roce_udp_sport(struct mlx5_ib_dev *dev, u8 port_num,
3082811ba51SAchiad Shochat 			       int index)
3092811ba51SAchiad Shochat {
3102811ba51SAchiad Shochat 	struct ib_gid_attr attr;
3112811ba51SAchiad Shochat 	union ib_gid gid;
3122811ba51SAchiad Shochat 
3132811ba51SAchiad Shochat 	if (ib_get_cached_gid(&dev->ib_dev, port_num, index, &gid, &attr))
3142811ba51SAchiad Shochat 		return 0;
3152811ba51SAchiad Shochat 
3162811ba51SAchiad Shochat 	if (!attr.ndev)
3172811ba51SAchiad Shochat 		return 0;
3182811ba51SAchiad Shochat 
3192811ba51SAchiad Shochat 	dev_put(attr.ndev);
3202811ba51SAchiad Shochat 
3212811ba51SAchiad Shochat 	if (attr.gid_type != IB_GID_TYPE_ROCE_UDP_ENCAP)
3222811ba51SAchiad Shochat 		return 0;
3232811ba51SAchiad Shochat 
3242811ba51SAchiad Shochat 	return cpu_to_be16(MLX5_CAP_ROCE(dev->mdev, r_roce_min_src_udp_port));
3252811ba51SAchiad Shochat }
3262811ba51SAchiad Shochat 
327ed88451eSMajd Dibbiny int mlx5_get_roce_gid_type(struct mlx5_ib_dev *dev, u8 port_num,
328ed88451eSMajd Dibbiny 			   int index, enum ib_gid_type *gid_type)
329ed88451eSMajd Dibbiny {
330ed88451eSMajd Dibbiny 	struct ib_gid_attr attr;
331ed88451eSMajd Dibbiny 	union ib_gid gid;
332ed88451eSMajd Dibbiny 	int ret;
333ed88451eSMajd Dibbiny 
334ed88451eSMajd Dibbiny 	ret = ib_get_cached_gid(&dev->ib_dev, port_num, index, &gid, &attr);
335ed88451eSMajd Dibbiny 	if (ret)
336ed88451eSMajd Dibbiny 		return ret;
337ed88451eSMajd Dibbiny 
338ed88451eSMajd Dibbiny 	if (!attr.ndev)
339ed88451eSMajd Dibbiny 		return -ENODEV;
340ed88451eSMajd Dibbiny 
341ed88451eSMajd Dibbiny 	dev_put(attr.ndev);
342ed88451eSMajd Dibbiny 
343ed88451eSMajd Dibbiny 	*gid_type = attr.gid_type;
344ed88451eSMajd Dibbiny 
345ed88451eSMajd Dibbiny 	return 0;
346ed88451eSMajd Dibbiny }
347ed88451eSMajd Dibbiny 
3481b5daf11SMajd Dibbiny static int mlx5_use_mad_ifc(struct mlx5_ib_dev *dev)
3491b5daf11SMajd Dibbiny {
3507fae6655SNoa Osherovich 	if (MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_IB)
351d603c809SEli Cohen 		return !MLX5_CAP_GEN(dev->mdev, ib_virt);
3527fae6655SNoa Osherovich 	return 0;
3531b5daf11SMajd Dibbiny }
3541b5daf11SMajd Dibbiny 
3551b5daf11SMajd Dibbiny enum {
3561b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_MAD,
3571b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_HCA,
3581b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_NIC,
3591b5daf11SMajd Dibbiny };
3601b5daf11SMajd Dibbiny 
3611b5daf11SMajd Dibbiny static int mlx5_get_vport_access_method(struct ib_device *ibdev)
3621b5daf11SMajd Dibbiny {
3631b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(to_mdev(ibdev)))
3641b5daf11SMajd Dibbiny 		return MLX5_VPORT_ACCESS_METHOD_MAD;
3651b5daf11SMajd Dibbiny 
366ebd61f68SAchiad Shochat 	if (mlx5_ib_port_link_layer(ibdev, 1) ==
3671b5daf11SMajd Dibbiny 	    IB_LINK_LAYER_ETHERNET)
3681b5daf11SMajd Dibbiny 		return MLX5_VPORT_ACCESS_METHOD_NIC;
3691b5daf11SMajd Dibbiny 
3701b5daf11SMajd Dibbiny 	return MLX5_VPORT_ACCESS_METHOD_HCA;
3711b5daf11SMajd Dibbiny }
3721b5daf11SMajd Dibbiny 
373da7525d2SEran Ben Elisha static void get_atomic_caps(struct mlx5_ib_dev *dev,
374da7525d2SEran Ben Elisha 			    struct ib_device_attr *props)
375da7525d2SEran Ben Elisha {
376da7525d2SEran Ben Elisha 	u8 tmp;
377da7525d2SEran Ben Elisha 	u8 atomic_operations = MLX5_CAP_ATOMIC(dev->mdev, atomic_operations);
378da7525d2SEran Ben Elisha 	u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_qp);
379da7525d2SEran Ben Elisha 	u8 atomic_req_8B_endianness_mode =
380da7525d2SEran Ben Elisha 		MLX5_CAP_ATOMIC(dev->mdev, atomic_req_8B_endianess_mode);
381da7525d2SEran Ben Elisha 
382da7525d2SEran Ben Elisha 	/* Check if HW supports 8 bytes standard atomic operations and capable
383da7525d2SEran Ben Elisha 	 * of host endianness respond
384da7525d2SEran Ben Elisha 	 */
385da7525d2SEran Ben Elisha 	tmp = MLX5_ATOMIC_OPS_CMP_SWAP | MLX5_ATOMIC_OPS_FETCH_ADD;
386da7525d2SEran Ben Elisha 	if (((atomic_operations & tmp) == tmp) &&
387da7525d2SEran Ben Elisha 	    (atomic_size_qp & MLX5_ATOMIC_SIZE_QP_8BYTES) &&
388da7525d2SEran Ben Elisha 	    (atomic_req_8B_endianness_mode)) {
389da7525d2SEran Ben Elisha 		props->atomic_cap = IB_ATOMIC_HCA;
390da7525d2SEran Ben Elisha 	} else {
391da7525d2SEran Ben Elisha 		props->atomic_cap = IB_ATOMIC_NONE;
392da7525d2SEran Ben Elisha 	}
393da7525d2SEran Ben Elisha }
394da7525d2SEran Ben Elisha 
3951b5daf11SMajd Dibbiny static int mlx5_query_system_image_guid(struct ib_device *ibdev,
3961b5daf11SMajd Dibbiny 					__be64 *sys_image_guid)
3971b5daf11SMajd Dibbiny {
3981b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
3991b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
4001b5daf11SMajd Dibbiny 	u64 tmp;
4011b5daf11SMajd Dibbiny 	int err;
4021b5daf11SMajd Dibbiny 
4031b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
4041b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
4051b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_system_image_guid(ibdev,
4061b5daf11SMajd Dibbiny 							    sys_image_guid);
4071b5daf11SMajd Dibbiny 
4081b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
4091b5daf11SMajd Dibbiny 		err = mlx5_query_hca_vport_system_image_guid(mdev, &tmp);
4103f89a643SAchiad Shochat 		break;
4113f89a643SAchiad Shochat 
4123f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
4133f89a643SAchiad Shochat 		err = mlx5_query_nic_vport_system_image_guid(mdev, &tmp);
4143f89a643SAchiad Shochat 		break;
4151b5daf11SMajd Dibbiny 
4161b5daf11SMajd Dibbiny 	default:
4171b5daf11SMajd Dibbiny 		return -EINVAL;
4181b5daf11SMajd Dibbiny 	}
4193f89a643SAchiad Shochat 
4203f89a643SAchiad Shochat 	if (!err)
4213f89a643SAchiad Shochat 		*sys_image_guid = cpu_to_be64(tmp);
4223f89a643SAchiad Shochat 
4233f89a643SAchiad Shochat 	return err;
4243f89a643SAchiad Shochat 
4251b5daf11SMajd Dibbiny }
4261b5daf11SMajd Dibbiny 
4271b5daf11SMajd Dibbiny static int mlx5_query_max_pkeys(struct ib_device *ibdev,
4281b5daf11SMajd Dibbiny 				u16 *max_pkeys)
4291b5daf11SMajd Dibbiny {
4301b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
4311b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
4321b5daf11SMajd Dibbiny 
4331b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
4341b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
4351b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_max_pkeys(ibdev, max_pkeys);
4361b5daf11SMajd Dibbiny 
4371b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
4381b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
4391b5daf11SMajd Dibbiny 		*max_pkeys = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev,
4401b5daf11SMajd Dibbiny 						pkey_table_size));
4411b5daf11SMajd Dibbiny 		return 0;
4421b5daf11SMajd Dibbiny 
4431b5daf11SMajd Dibbiny 	default:
4441b5daf11SMajd Dibbiny 		return -EINVAL;
4451b5daf11SMajd Dibbiny 	}
4461b5daf11SMajd Dibbiny }
4471b5daf11SMajd Dibbiny 
4481b5daf11SMajd Dibbiny static int mlx5_query_vendor_id(struct ib_device *ibdev,
4491b5daf11SMajd Dibbiny 				u32 *vendor_id)
4501b5daf11SMajd Dibbiny {
4511b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
4521b5daf11SMajd Dibbiny 
4531b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
4541b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
4551b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_vendor_id(ibdev, vendor_id);
4561b5daf11SMajd Dibbiny 
4571b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
4581b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
4591b5daf11SMajd Dibbiny 		return mlx5_core_query_vendor_id(dev->mdev, vendor_id);
4601b5daf11SMajd Dibbiny 
4611b5daf11SMajd Dibbiny 	default:
4621b5daf11SMajd Dibbiny 		return -EINVAL;
4631b5daf11SMajd Dibbiny 	}
4641b5daf11SMajd Dibbiny }
4651b5daf11SMajd Dibbiny 
4661b5daf11SMajd Dibbiny static int mlx5_query_node_guid(struct mlx5_ib_dev *dev,
4671b5daf11SMajd Dibbiny 				__be64 *node_guid)
4681b5daf11SMajd Dibbiny {
4691b5daf11SMajd Dibbiny 	u64 tmp;
4701b5daf11SMajd Dibbiny 	int err;
4711b5daf11SMajd Dibbiny 
4721b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(&dev->ib_dev)) {
4731b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
4741b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_node_guid(dev, node_guid);
4751b5daf11SMajd Dibbiny 
4761b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
4771b5daf11SMajd Dibbiny 		err = mlx5_query_hca_vport_node_guid(dev->mdev, &tmp);
4783f89a643SAchiad Shochat 		break;
4793f89a643SAchiad Shochat 
4803f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
4813f89a643SAchiad Shochat 		err = mlx5_query_nic_vport_node_guid(dev->mdev, &tmp);
4823f89a643SAchiad Shochat 		break;
4831b5daf11SMajd Dibbiny 
4841b5daf11SMajd Dibbiny 	default:
4851b5daf11SMajd Dibbiny 		return -EINVAL;
4861b5daf11SMajd Dibbiny 	}
4873f89a643SAchiad Shochat 
4883f89a643SAchiad Shochat 	if (!err)
4893f89a643SAchiad Shochat 		*node_guid = cpu_to_be64(tmp);
4903f89a643SAchiad Shochat 
4913f89a643SAchiad Shochat 	return err;
4921b5daf11SMajd Dibbiny }
4931b5daf11SMajd Dibbiny 
4941b5daf11SMajd Dibbiny struct mlx5_reg_node_desc {
495bd99fdeaSYuval Shaia 	u8	desc[IB_DEVICE_NODE_DESC_MAX];
4961b5daf11SMajd Dibbiny };
4971b5daf11SMajd Dibbiny 
4981b5daf11SMajd Dibbiny static int mlx5_query_node_desc(struct mlx5_ib_dev *dev, char *node_desc)
4991b5daf11SMajd Dibbiny {
5001b5daf11SMajd Dibbiny 	struct mlx5_reg_node_desc in;
5011b5daf11SMajd Dibbiny 
5021b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(dev))
5031b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_node_desc(dev, node_desc);
5041b5daf11SMajd Dibbiny 
5051b5daf11SMajd Dibbiny 	memset(&in, 0, sizeof(in));
5061b5daf11SMajd Dibbiny 
5071b5daf11SMajd Dibbiny 	return mlx5_core_access_reg(dev->mdev, &in, sizeof(in), node_desc,
5081b5daf11SMajd Dibbiny 				    sizeof(struct mlx5_reg_node_desc),
5091b5daf11SMajd Dibbiny 				    MLX5_REG_NODE_DESC, 0, 0);
5101b5daf11SMajd Dibbiny }
5111b5daf11SMajd Dibbiny 
512e126ba97SEli Cohen static int mlx5_ib_query_device(struct ib_device *ibdev,
5132528e33eSMatan Barak 				struct ib_device_attr *props,
5142528e33eSMatan Barak 				struct ib_udata *uhw)
515e126ba97SEli Cohen {
516e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
517938fe83cSSaeed Mahameed 	struct mlx5_core_dev *mdev = dev->mdev;
518e126ba97SEli Cohen 	int err = -ENOMEM;
519288c01b7SEli Cohen 	int max_sq_desc;
520e126ba97SEli Cohen 	int max_rq_sg;
521e126ba97SEli Cohen 	int max_sq_sg;
522e0238a6aSSagi Grimberg 	u64 min_page_size = 1ull << MLX5_CAP_GEN(mdev, log_pg_sz);
523402ca536SBodong Wang 	struct mlx5_ib_query_device_resp resp = {};
524402ca536SBodong Wang 	size_t resp_len;
525402ca536SBodong Wang 	u64 max_tso;
526e126ba97SEli Cohen 
527402ca536SBodong Wang 	resp_len = sizeof(resp.comp_mask) + sizeof(resp.response_length);
528402ca536SBodong Wang 	if (uhw->outlen && uhw->outlen < resp_len)
529402ca536SBodong Wang 		return -EINVAL;
530402ca536SBodong Wang 	else
531402ca536SBodong Wang 		resp.response_length = resp_len;
532402ca536SBodong Wang 
533402ca536SBodong Wang 	if (uhw->inlen && !ib_is_udata_cleared(uhw, 0, uhw->inlen))
5342528e33eSMatan Barak 		return -EINVAL;
5352528e33eSMatan Barak 
536e126ba97SEli Cohen 	memset(props, 0, sizeof(*props));
5371b5daf11SMajd Dibbiny 	err = mlx5_query_system_image_guid(ibdev,
5381b5daf11SMajd Dibbiny 					   &props->sys_image_guid);
5391b5daf11SMajd Dibbiny 	if (err)
5401b5daf11SMajd Dibbiny 		return err;
5411b5daf11SMajd Dibbiny 
5421b5daf11SMajd Dibbiny 	err = mlx5_query_max_pkeys(ibdev, &props->max_pkeys);
5431b5daf11SMajd Dibbiny 	if (err)
5441b5daf11SMajd Dibbiny 		return err;
5451b5daf11SMajd Dibbiny 
5461b5daf11SMajd Dibbiny 	err = mlx5_query_vendor_id(ibdev, &props->vendor_id);
5471b5daf11SMajd Dibbiny 	if (err)
5481b5daf11SMajd Dibbiny 		return err;
549e126ba97SEli Cohen 
5509603b61dSJack Morgenstein 	props->fw_ver = ((u64)fw_rev_maj(dev->mdev) << 32) |
5519603b61dSJack Morgenstein 		(fw_rev_min(dev->mdev) << 16) |
5529603b61dSJack Morgenstein 		fw_rev_sub(dev->mdev);
553e126ba97SEli Cohen 	props->device_cap_flags    = IB_DEVICE_CHANGE_PHY_PORT |
554e126ba97SEli Cohen 		IB_DEVICE_PORT_ACTIVE_EVENT		|
555e126ba97SEli Cohen 		IB_DEVICE_SYS_IMAGE_GUID		|
5561a4c3a3dSEli Cohen 		IB_DEVICE_RC_RNR_NAK_GEN;
557938fe83cSSaeed Mahameed 
558938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, pkv))
559e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
560938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, qkv))
561e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
562938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, apm))
563e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
564938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, xrc))
565e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_XRC;
566d2370e0aSMatan Barak 	if (MLX5_CAP_GEN(mdev, imaicl)) {
567d2370e0aSMatan Barak 		props->device_cap_flags |= IB_DEVICE_MEM_WINDOW |
568d2370e0aSMatan Barak 					   IB_DEVICE_MEM_WINDOW_TYPE_2B;
569d2370e0aSMatan Barak 		props->max_mw = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
570b005d316SSagi Grimberg 		/* We support 'Gappy' memory registration too */
571b005d316SSagi Grimberg 		props->device_cap_flags |= IB_DEVICE_SG_GAPS_REG;
572d2370e0aSMatan Barak 	}
573e126ba97SEli Cohen 	props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
574938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, sho)) {
5752dea9094SSagi Grimberg 		props->device_cap_flags |= IB_DEVICE_SIGNATURE_HANDOVER;
5762dea9094SSagi Grimberg 		/* At this stage no support for signature handover */
5772dea9094SSagi Grimberg 		props->sig_prot_cap = IB_PROT_T10DIF_TYPE_1 |
5782dea9094SSagi Grimberg 				      IB_PROT_T10DIF_TYPE_2 |
5792dea9094SSagi Grimberg 				      IB_PROT_T10DIF_TYPE_3;
5802dea9094SSagi Grimberg 		props->sig_guard_cap = IB_GUARD_T10DIF_CRC |
5812dea9094SSagi Grimberg 				       IB_GUARD_T10DIF_CSUM;
5822dea9094SSagi Grimberg 	}
583938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, block_lb_mc))
584f360d88aSEli Cohen 		props->device_cap_flags |= IB_DEVICE_BLOCK_MULTICAST_LOOPBACK;
585e126ba97SEli Cohen 
586402ca536SBodong Wang 	if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads)) {
587e8161334SNoa Osherovich 		if (MLX5_CAP_ETH(mdev, csum_cap)) {
588e8161334SNoa Osherovich 			/* Legacy bit to support old userspace libraries */
58988115fe7SBodong Wang 			props->device_cap_flags |= IB_DEVICE_RAW_IP_CSUM;
590e8161334SNoa Osherovich 			props->raw_packet_caps |= IB_RAW_PACKET_CAP_IP_CSUM;
591e8161334SNoa Osherovich 		}
592e8161334SNoa Osherovich 
593e8161334SNoa Osherovich 		if (MLX5_CAP_ETH(dev->mdev, vlan_cap))
594e8161334SNoa Osherovich 			props->raw_packet_caps |=
595e8161334SNoa Osherovich 				IB_RAW_PACKET_CAP_CVLAN_STRIPPING;
59688115fe7SBodong Wang 
597402ca536SBodong Wang 		if (field_avail(typeof(resp), tso_caps, uhw->outlen)) {
598402ca536SBodong Wang 			max_tso = MLX5_CAP_ETH(mdev, max_lso_cap);
599402ca536SBodong Wang 			if (max_tso) {
600402ca536SBodong Wang 				resp.tso_caps.max_tso = 1 << max_tso;
601402ca536SBodong Wang 				resp.tso_caps.supported_qpts |=
602402ca536SBodong Wang 					1 << IB_QPT_RAW_PACKET;
603402ca536SBodong Wang 				resp.response_length += sizeof(resp.tso_caps);
604402ca536SBodong Wang 			}
605402ca536SBodong Wang 		}
60631f69a82SYishai Hadas 
60731f69a82SYishai Hadas 		if (field_avail(typeof(resp), rss_caps, uhw->outlen)) {
60831f69a82SYishai Hadas 			resp.rss_caps.rx_hash_function =
60931f69a82SYishai Hadas 						MLX5_RX_HASH_FUNC_TOEPLITZ;
61031f69a82SYishai Hadas 			resp.rss_caps.rx_hash_fields_mask =
61131f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_IPV4 |
61231f69a82SYishai Hadas 						MLX5_RX_HASH_DST_IPV4 |
61331f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_IPV6 |
61431f69a82SYishai Hadas 						MLX5_RX_HASH_DST_IPV6 |
61531f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_PORT_TCP |
61631f69a82SYishai Hadas 						MLX5_RX_HASH_DST_PORT_TCP |
61731f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_PORT_UDP |
61831f69a82SYishai Hadas 						MLX5_RX_HASH_DST_PORT_UDP;
61931f69a82SYishai Hadas 			resp.response_length += sizeof(resp.rss_caps);
62031f69a82SYishai Hadas 		}
62131f69a82SYishai Hadas 	} else {
62231f69a82SYishai Hadas 		if (field_avail(typeof(resp), tso_caps, uhw->outlen))
62331f69a82SYishai Hadas 			resp.response_length += sizeof(resp.tso_caps);
62431f69a82SYishai Hadas 		if (field_avail(typeof(resp), rss_caps, uhw->outlen))
62531f69a82SYishai Hadas 			resp.response_length += sizeof(resp.rss_caps);
626402ca536SBodong Wang 	}
627402ca536SBodong Wang 
628f0313965SErez Shitrit 	if (MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) {
629f0313965SErez Shitrit 		props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
630f0313965SErez Shitrit 		props->device_cap_flags |= IB_DEVICE_UD_TSO;
631f0313965SErez Shitrit 	}
632f0313965SErez Shitrit 
633cff5a0f3SMajd Dibbiny 	if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
634e8161334SNoa Osherovich 	    MLX5_CAP_ETH(dev->mdev, scatter_fcs)) {
635e8161334SNoa Osherovich 		/* Legacy bit to support old userspace libraries */
636cff5a0f3SMajd Dibbiny 		props->device_cap_flags |= IB_DEVICE_RAW_SCATTER_FCS;
637e8161334SNoa Osherovich 		props->raw_packet_caps |= IB_RAW_PACKET_CAP_SCATTER_FCS;
638e8161334SNoa Osherovich 	}
639cff5a0f3SMajd Dibbiny 
640da6d6ba3SMaor Gottlieb 	if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS))
641da6d6ba3SMaor Gottlieb 		props->device_cap_flags |= IB_DEVICE_MANAGED_FLOW_STEERING;
642da6d6ba3SMaor Gottlieb 
6431b5daf11SMajd Dibbiny 	props->vendor_part_id	   = mdev->pdev->device;
6441b5daf11SMajd Dibbiny 	props->hw_ver		   = mdev->pdev->revision;
645e126ba97SEli Cohen 
646e126ba97SEli Cohen 	props->max_mr_size	   = ~0ull;
647e0238a6aSSagi Grimberg 	props->page_size_cap	   = ~(min_page_size - 1);
648938fe83cSSaeed Mahameed 	props->max_qp		   = 1 << MLX5_CAP_GEN(mdev, log_max_qp);
649938fe83cSSaeed Mahameed 	props->max_qp_wr	   = 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
650938fe83cSSaeed Mahameed 	max_rq_sg =  MLX5_CAP_GEN(mdev, max_wqe_sz_rq) /
651938fe83cSSaeed Mahameed 		     sizeof(struct mlx5_wqe_data_seg);
652288c01b7SEli Cohen 	max_sq_desc = min_t(int, MLX5_CAP_GEN(mdev, max_wqe_sz_sq), 512);
653288c01b7SEli Cohen 	max_sq_sg = (max_sq_desc - sizeof(struct mlx5_wqe_ctrl_seg) -
654288c01b7SEli Cohen 		     sizeof(struct mlx5_wqe_raddr_seg)) /
655e126ba97SEli Cohen 		sizeof(struct mlx5_wqe_data_seg);
656e126ba97SEli Cohen 	props->max_sge = min(max_rq_sg, max_sq_sg);
657986ef95eSSagi Grimberg 	props->max_sge_rd	   = MLX5_MAX_SGE_RD;
658938fe83cSSaeed Mahameed 	props->max_cq		   = 1 << MLX5_CAP_GEN(mdev, log_max_cq);
6599f177686SLeon Romanovsky 	props->max_cqe = (1 << MLX5_CAP_GEN(mdev, log_max_cq_sz)) - 1;
660938fe83cSSaeed Mahameed 	props->max_mr		   = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
661938fe83cSSaeed Mahameed 	props->max_pd		   = 1 << MLX5_CAP_GEN(mdev, log_max_pd);
662938fe83cSSaeed Mahameed 	props->max_qp_rd_atom	   = 1 << MLX5_CAP_GEN(mdev, log_max_ra_req_qp);
663938fe83cSSaeed Mahameed 	props->max_qp_init_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_res_qp);
664938fe83cSSaeed Mahameed 	props->max_srq		   = 1 << MLX5_CAP_GEN(mdev, log_max_srq);
665938fe83cSSaeed Mahameed 	props->max_srq_wr = (1 << MLX5_CAP_GEN(mdev, log_max_srq_sz)) - 1;
666938fe83cSSaeed Mahameed 	props->local_ca_ack_delay  = MLX5_CAP_GEN(mdev, local_ca_ack_delay);
667e126ba97SEli Cohen 	props->max_res_rd_atom	   = props->max_qp_rd_atom * props->max_qp;
668e126ba97SEli Cohen 	props->max_srq_sge	   = max_rq_sg - 1;
669911f4331SSagi Grimberg 	props->max_fast_reg_page_list_len =
670911f4331SSagi Grimberg 		1 << MLX5_CAP_GEN(mdev, log_max_klm_list_size);
671da7525d2SEran Ben Elisha 	get_atomic_caps(dev, props);
67281bea28fSEli Cohen 	props->masked_atomic_cap   = IB_ATOMIC_NONE;
673938fe83cSSaeed Mahameed 	props->max_mcast_grp	   = 1 << MLX5_CAP_GEN(mdev, log_max_mcg);
674938fe83cSSaeed Mahameed 	props->max_mcast_qp_attach = MLX5_CAP_GEN(mdev, max_qp_mcg);
675e126ba97SEli Cohen 	props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
676e126ba97SEli Cohen 					   props->max_mcast_grp;
677e126ba97SEli Cohen 	props->max_map_per_fmr = INT_MAX; /* no limit in ConnectIB */
67886695a65SMaor Gottlieb 	props->max_ah = INT_MAX;
6797c60bcbbSMatan Barak 	props->hca_core_clock = MLX5_CAP_GEN(mdev, device_frequency_khz);
6807c60bcbbSMatan Barak 	props->timestamp_mask = 0x7FFFFFFFFFFFFFFFULL;
681e126ba97SEli Cohen 
6828cdd312cSHaggai Eran #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
683938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, pg))
6848cdd312cSHaggai Eran 		props->device_cap_flags |= IB_DEVICE_ON_DEMAND_PAGING;
6858cdd312cSHaggai Eran 	props->odp_caps = dev->odp_caps;
6868cdd312cSHaggai Eran #endif
6878cdd312cSHaggai Eran 
688051f2630SLeon Romanovsky 	if (MLX5_CAP_GEN(mdev, cd))
689051f2630SLeon Romanovsky 		props->device_cap_flags |= IB_DEVICE_CROSS_CHANNEL;
690051f2630SLeon Romanovsky 
691eff901d3SEli Cohen 	if (!mlx5_core_is_pf(mdev))
692eff901d3SEli Cohen 		props->device_cap_flags |= IB_DEVICE_VIRTUAL_FUNCTION;
693eff901d3SEli Cohen 
69431f69a82SYishai Hadas 	if (mlx5_ib_port_link_layer(ibdev, 1) ==
69531f69a82SYishai Hadas 	    IB_LINK_LAYER_ETHERNET) {
69631f69a82SYishai Hadas 		props->rss_caps.max_rwq_indirection_tables =
69731f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt);
69831f69a82SYishai Hadas 		props->rss_caps.max_rwq_indirection_table_size =
69931f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt_size);
70031f69a82SYishai Hadas 		props->rss_caps.supported_qpts = 1 << IB_QPT_RAW_PACKET;
70131f69a82SYishai Hadas 		props->max_wq_type_rq =
70231f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rq);
70331f69a82SYishai Hadas 	}
70431f69a82SYishai Hadas 
7057e43a2a5SBodong Wang 	if (field_avail(typeof(resp), cqe_comp_caps, uhw->outlen)) {
7067e43a2a5SBodong Wang 		resp.cqe_comp_caps.max_num =
7077e43a2a5SBodong Wang 			MLX5_CAP_GEN(dev->mdev, cqe_compression) ?
7087e43a2a5SBodong Wang 			MLX5_CAP_GEN(dev->mdev, cqe_compression_max_num) : 0;
7097e43a2a5SBodong Wang 		resp.cqe_comp_caps.supported_format =
7107e43a2a5SBodong Wang 			MLX5_IB_CQE_RES_FORMAT_HASH |
7117e43a2a5SBodong Wang 			MLX5_IB_CQE_RES_FORMAT_CSUM;
7127e43a2a5SBodong Wang 		resp.response_length += sizeof(resp.cqe_comp_caps);
7137e43a2a5SBodong Wang 	}
7147e43a2a5SBodong Wang 
715d949167dSBodong Wang 	if (field_avail(typeof(resp), packet_pacing_caps, uhw->outlen)) {
716d949167dSBodong Wang 		if (MLX5_CAP_QOS(mdev, packet_pacing) &&
717d949167dSBodong Wang 		    MLX5_CAP_GEN(mdev, qos)) {
718d949167dSBodong Wang 			resp.packet_pacing_caps.qp_rate_limit_max =
719d949167dSBodong Wang 				MLX5_CAP_QOS(mdev, packet_pacing_max_rate);
720d949167dSBodong Wang 			resp.packet_pacing_caps.qp_rate_limit_min =
721d949167dSBodong Wang 				MLX5_CAP_QOS(mdev, packet_pacing_min_rate);
722d949167dSBodong Wang 			resp.packet_pacing_caps.supported_qpts |=
723d949167dSBodong Wang 				1 << IB_QPT_RAW_PACKET;
724d949167dSBodong Wang 		}
725d949167dSBodong Wang 		resp.response_length += sizeof(resp.packet_pacing_caps);
726d949167dSBodong Wang 	}
727d949167dSBodong Wang 
7289f885201SLeon Romanovsky 	if (field_avail(typeof(resp), mlx5_ib_support_multi_pkt_send_wqes,
7299f885201SLeon Romanovsky 			uhw->outlen)) {
7309f885201SLeon Romanovsky 		resp.mlx5_ib_support_multi_pkt_send_wqes =
7319f885201SLeon Romanovsky 			MLX5_CAP_ETH(mdev, multi_pkt_send_wqe);
7329f885201SLeon Romanovsky 		resp.response_length +=
7339f885201SLeon Romanovsky 			sizeof(resp.mlx5_ib_support_multi_pkt_send_wqes);
7349f885201SLeon Romanovsky 	}
7359f885201SLeon Romanovsky 
7369f885201SLeon Romanovsky 	if (field_avail(typeof(resp), reserved, uhw->outlen))
7379f885201SLeon Romanovsky 		resp.response_length += sizeof(resp.reserved);
7389f885201SLeon Romanovsky 
739402ca536SBodong Wang 	if (uhw->outlen) {
740402ca536SBodong Wang 		err = ib_copy_to_udata(uhw, &resp, resp.response_length);
741402ca536SBodong Wang 
742402ca536SBodong Wang 		if (err)
743402ca536SBodong Wang 			return err;
744402ca536SBodong Wang 	}
745402ca536SBodong Wang 
7461b5daf11SMajd Dibbiny 	return 0;
7471b5daf11SMajd Dibbiny }
748e126ba97SEli Cohen 
7491b5daf11SMajd Dibbiny enum mlx5_ib_width {
7501b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_1X	= 1 << 0,
7511b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_2X	= 1 << 1,
7521b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_4X	= 1 << 2,
7531b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_8X	= 1 << 3,
7541b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_12X	= 1 << 4
7551b5daf11SMajd Dibbiny };
7561b5daf11SMajd Dibbiny 
7571b5daf11SMajd Dibbiny static int translate_active_width(struct ib_device *ibdev, u8 active_width,
7581b5daf11SMajd Dibbiny 				  u8 *ib_width)
7591b5daf11SMajd Dibbiny {
7601b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
7611b5daf11SMajd Dibbiny 	int err = 0;
7621b5daf11SMajd Dibbiny 
7631b5daf11SMajd Dibbiny 	if (active_width & MLX5_IB_WIDTH_1X) {
7641b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_1X;
7651b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_2X) {
7661b5daf11SMajd Dibbiny 		mlx5_ib_dbg(dev, "active_width %d is not supported by IB spec\n",
7671b5daf11SMajd Dibbiny 			    (int)active_width);
7681b5daf11SMajd Dibbiny 		err = -EINVAL;
7691b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_4X) {
7701b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_4X;
7711b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_8X) {
7721b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_8X;
7731b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_12X) {
7741b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_12X;
7751b5daf11SMajd Dibbiny 	} else {
7761b5daf11SMajd Dibbiny 		mlx5_ib_dbg(dev, "Invalid active_width %d\n",
7771b5daf11SMajd Dibbiny 			    (int)active_width);
7781b5daf11SMajd Dibbiny 		err = -EINVAL;
7791b5daf11SMajd Dibbiny 	}
7801b5daf11SMajd Dibbiny 
7811b5daf11SMajd Dibbiny 	return err;
7821b5daf11SMajd Dibbiny }
7831b5daf11SMajd Dibbiny 
7841b5daf11SMajd Dibbiny static int mlx5_mtu_to_ib_mtu(int mtu)
7851b5daf11SMajd Dibbiny {
7861b5daf11SMajd Dibbiny 	switch (mtu) {
7871b5daf11SMajd Dibbiny 	case 256: return 1;
7881b5daf11SMajd Dibbiny 	case 512: return 2;
7891b5daf11SMajd Dibbiny 	case 1024: return 3;
7901b5daf11SMajd Dibbiny 	case 2048: return 4;
7911b5daf11SMajd Dibbiny 	case 4096: return 5;
7921b5daf11SMajd Dibbiny 	default:
7931b5daf11SMajd Dibbiny 		pr_warn("invalid mtu\n");
7941b5daf11SMajd Dibbiny 		return -1;
7951b5daf11SMajd Dibbiny 	}
7961b5daf11SMajd Dibbiny }
7971b5daf11SMajd Dibbiny 
7981b5daf11SMajd Dibbiny enum ib_max_vl_num {
7991b5daf11SMajd Dibbiny 	__IB_MAX_VL_0		= 1,
8001b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_1		= 2,
8011b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_3		= 3,
8021b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_7		= 4,
8031b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_14	= 5,
8041b5daf11SMajd Dibbiny };
8051b5daf11SMajd Dibbiny 
8061b5daf11SMajd Dibbiny enum mlx5_vl_hw_cap {
8071b5daf11SMajd Dibbiny 	MLX5_VL_HW_0	= 1,
8081b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_1	= 2,
8091b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_2	= 3,
8101b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_3	= 4,
8111b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_4	= 5,
8121b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_5	= 6,
8131b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_6	= 7,
8141b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_7	= 8,
8151b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_14	= 15
8161b5daf11SMajd Dibbiny };
8171b5daf11SMajd Dibbiny 
8181b5daf11SMajd Dibbiny static int translate_max_vl_num(struct ib_device *ibdev, u8 vl_hw_cap,
8191b5daf11SMajd Dibbiny 				u8 *max_vl_num)
8201b5daf11SMajd Dibbiny {
8211b5daf11SMajd Dibbiny 	switch (vl_hw_cap) {
8221b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0:
8231b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0;
8241b5daf11SMajd Dibbiny 		break;
8251b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_1:
8261b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_1;
8271b5daf11SMajd Dibbiny 		break;
8281b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_3:
8291b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_3;
8301b5daf11SMajd Dibbiny 		break;
8311b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_7:
8321b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_7;
8331b5daf11SMajd Dibbiny 		break;
8341b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_14:
8351b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_14;
8361b5daf11SMajd Dibbiny 		break;
8371b5daf11SMajd Dibbiny 
8381b5daf11SMajd Dibbiny 	default:
8391b5daf11SMajd Dibbiny 		return -EINVAL;
8401b5daf11SMajd Dibbiny 	}
8411b5daf11SMajd Dibbiny 
8421b5daf11SMajd Dibbiny 	return 0;
8431b5daf11SMajd Dibbiny }
8441b5daf11SMajd Dibbiny 
8451b5daf11SMajd Dibbiny static int mlx5_query_hca_port(struct ib_device *ibdev, u8 port,
8461b5daf11SMajd Dibbiny 			       struct ib_port_attr *props)
8471b5daf11SMajd Dibbiny {
8481b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
8491b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
8501b5daf11SMajd Dibbiny 	struct mlx5_hca_vport_context *rep;
851046339eaSSaeed Mahameed 	u16 max_mtu;
852046339eaSSaeed Mahameed 	u16 oper_mtu;
8531b5daf11SMajd Dibbiny 	int err;
8541b5daf11SMajd Dibbiny 	u8 ib_link_width_oper;
8551b5daf11SMajd Dibbiny 	u8 vl_hw_cap;
8561b5daf11SMajd Dibbiny 
8571b5daf11SMajd Dibbiny 	rep = kzalloc(sizeof(*rep), GFP_KERNEL);
8581b5daf11SMajd Dibbiny 	if (!rep) {
8591b5daf11SMajd Dibbiny 		err = -ENOMEM;
8601b5daf11SMajd Dibbiny 		goto out;
8611b5daf11SMajd Dibbiny 	}
8621b5daf11SMajd Dibbiny 
863c4550c63SOr Gerlitz 	/* props being zeroed by the caller, avoid zeroing it here */
8641b5daf11SMajd Dibbiny 
8651b5daf11SMajd Dibbiny 	err = mlx5_query_hca_vport_context(mdev, 0, port, 0, rep);
8661b5daf11SMajd Dibbiny 	if (err)
8671b5daf11SMajd Dibbiny 		goto out;
8681b5daf11SMajd Dibbiny 
8691b5daf11SMajd Dibbiny 	props->lid		= rep->lid;
8701b5daf11SMajd Dibbiny 	props->lmc		= rep->lmc;
8711b5daf11SMajd Dibbiny 	props->sm_lid		= rep->sm_lid;
8721b5daf11SMajd Dibbiny 	props->sm_sl		= rep->sm_sl;
8731b5daf11SMajd Dibbiny 	props->state		= rep->vport_state;
8741b5daf11SMajd Dibbiny 	props->phys_state	= rep->port_physical_state;
8751b5daf11SMajd Dibbiny 	props->port_cap_flags	= rep->cap_mask1;
8761b5daf11SMajd Dibbiny 	props->gid_tbl_len	= mlx5_get_gid_table_len(MLX5_CAP_GEN(mdev, gid_table_size));
8771b5daf11SMajd Dibbiny 	props->max_msg_sz	= 1 << MLX5_CAP_GEN(mdev, log_max_msg);
8781b5daf11SMajd Dibbiny 	props->pkey_tbl_len	= mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, pkey_table_size));
8791b5daf11SMajd Dibbiny 	props->bad_pkey_cntr	= rep->pkey_violation_counter;
8801b5daf11SMajd Dibbiny 	props->qkey_viol_cntr	= rep->qkey_violation_counter;
8811b5daf11SMajd Dibbiny 	props->subnet_timeout	= rep->subnet_timeout;
8821b5daf11SMajd Dibbiny 	props->init_type_reply	= rep->init_type_reply;
883eff901d3SEli Cohen 	props->grh_required	= rep->grh_required;
8841b5daf11SMajd Dibbiny 
8851b5daf11SMajd Dibbiny 	err = mlx5_query_port_link_width_oper(mdev, &ib_link_width_oper, port);
8861b5daf11SMajd Dibbiny 	if (err)
8871b5daf11SMajd Dibbiny 		goto out;
8881b5daf11SMajd Dibbiny 
8891b5daf11SMajd Dibbiny 	err = translate_active_width(ibdev, ib_link_width_oper,
8901b5daf11SMajd Dibbiny 				     &props->active_width);
8911b5daf11SMajd Dibbiny 	if (err)
8921b5daf11SMajd Dibbiny 		goto out;
893d5beb7f2SNoa Osherovich 	err = mlx5_query_port_ib_proto_oper(mdev, &props->active_speed, port);
8941b5daf11SMajd Dibbiny 	if (err)
8951b5daf11SMajd Dibbiny 		goto out;
8961b5daf11SMajd Dibbiny 
897facc9699SSaeed Mahameed 	mlx5_query_port_max_mtu(mdev, &max_mtu, port);
8981b5daf11SMajd Dibbiny 
8991b5daf11SMajd Dibbiny 	props->max_mtu = mlx5_mtu_to_ib_mtu(max_mtu);
9001b5daf11SMajd Dibbiny 
901facc9699SSaeed Mahameed 	mlx5_query_port_oper_mtu(mdev, &oper_mtu, port);
9021b5daf11SMajd Dibbiny 
9031b5daf11SMajd Dibbiny 	props->active_mtu = mlx5_mtu_to_ib_mtu(oper_mtu);
9041b5daf11SMajd Dibbiny 
9051b5daf11SMajd Dibbiny 	err = mlx5_query_port_vl_hw_cap(mdev, &vl_hw_cap, port);
9061b5daf11SMajd Dibbiny 	if (err)
9071b5daf11SMajd Dibbiny 		goto out;
9081b5daf11SMajd Dibbiny 
9091b5daf11SMajd Dibbiny 	err = translate_max_vl_num(ibdev, vl_hw_cap,
9101b5daf11SMajd Dibbiny 				   &props->max_vl_num);
9111b5daf11SMajd Dibbiny out:
9121b5daf11SMajd Dibbiny 	kfree(rep);
913e126ba97SEli Cohen 	return err;
914e126ba97SEli Cohen }
915e126ba97SEli Cohen 
916e126ba97SEli Cohen int mlx5_ib_query_port(struct ib_device *ibdev, u8 port,
917e126ba97SEli Cohen 		       struct ib_port_attr *props)
918e126ba97SEli Cohen {
9191b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
9201b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
9211b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_port(ibdev, port, props);
922e126ba97SEli Cohen 
9231b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
9241b5daf11SMajd Dibbiny 		return mlx5_query_hca_port(ibdev, port, props);
9251b5daf11SMajd Dibbiny 
9263f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
9273f89a643SAchiad Shochat 		return mlx5_query_port_roce(ibdev, port, props);
9283f89a643SAchiad Shochat 
9291b5daf11SMajd Dibbiny 	default:
930e126ba97SEli Cohen 		return -EINVAL;
931e126ba97SEli Cohen 	}
932e126ba97SEli Cohen }
933e126ba97SEli Cohen 
934e126ba97SEli Cohen static int mlx5_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
935e126ba97SEli Cohen 			     union ib_gid *gid)
936e126ba97SEli Cohen {
9371b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
9381b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
939e126ba97SEli Cohen 
9401b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
9411b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
9421b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_gids(ibdev, port, index, gid);
943e126ba97SEli Cohen 
9441b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
9451b5daf11SMajd Dibbiny 		return mlx5_query_hca_vport_gid(mdev, 0, port, 0, index, gid);
946e126ba97SEli Cohen 
9471b5daf11SMajd Dibbiny 	default:
9481b5daf11SMajd Dibbiny 		return -EINVAL;
9491b5daf11SMajd Dibbiny 	}
950e126ba97SEli Cohen 
951e126ba97SEli Cohen }
952e126ba97SEli Cohen 
953e126ba97SEli Cohen static int mlx5_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
954e126ba97SEli Cohen 			      u16 *pkey)
955e126ba97SEli Cohen {
9561b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
9571b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
958e126ba97SEli Cohen 
9591b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
9601b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
9611b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_pkey(ibdev, port, index, pkey);
962e126ba97SEli Cohen 
9631b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
9641b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
9651b5daf11SMajd Dibbiny 		return mlx5_query_hca_vport_pkey(mdev, 0, port,  0, index,
9661b5daf11SMajd Dibbiny 						 pkey);
9671b5daf11SMajd Dibbiny 	default:
9681b5daf11SMajd Dibbiny 		return -EINVAL;
969e126ba97SEli Cohen 	}
9701b5daf11SMajd Dibbiny }
971e126ba97SEli Cohen 
972e126ba97SEli Cohen static int mlx5_ib_modify_device(struct ib_device *ibdev, int mask,
973e126ba97SEli Cohen 				 struct ib_device_modify *props)
974e126ba97SEli Cohen {
975e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
976e126ba97SEli Cohen 	struct mlx5_reg_node_desc in;
977e126ba97SEli Cohen 	struct mlx5_reg_node_desc out;
978e126ba97SEli Cohen 	int err;
979e126ba97SEli Cohen 
980e126ba97SEli Cohen 	if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
981e126ba97SEli Cohen 		return -EOPNOTSUPP;
982e126ba97SEli Cohen 
983e126ba97SEli Cohen 	if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
984e126ba97SEli Cohen 		return 0;
985e126ba97SEli Cohen 
986e126ba97SEli Cohen 	/*
987e126ba97SEli Cohen 	 * If possible, pass node desc to FW, so it can generate
988e126ba97SEli Cohen 	 * a 144 trap.  If cmd fails, just ignore.
989e126ba97SEli Cohen 	 */
990bd99fdeaSYuval Shaia 	memcpy(&in, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
9919603b61dSJack Morgenstein 	err = mlx5_core_access_reg(dev->mdev, &in, sizeof(in), &out,
992e126ba97SEli Cohen 				   sizeof(out), MLX5_REG_NODE_DESC, 0, 1);
993e126ba97SEli Cohen 	if (err)
994e126ba97SEli Cohen 		return err;
995e126ba97SEli Cohen 
996bd99fdeaSYuval Shaia 	memcpy(ibdev->node_desc, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
997e126ba97SEli Cohen 
998e126ba97SEli Cohen 	return err;
999e126ba97SEli Cohen }
1000e126ba97SEli Cohen 
1001cdbe33d0SEli Cohen static int set_port_caps_atomic(struct mlx5_ib_dev *dev, u8 port_num, u32 mask,
1002cdbe33d0SEli Cohen 				u32 value)
1003cdbe33d0SEli Cohen {
1004cdbe33d0SEli Cohen 	struct mlx5_hca_vport_context ctx = {};
1005cdbe33d0SEli Cohen 	int err;
1006cdbe33d0SEli Cohen 
1007cdbe33d0SEli Cohen 	err = mlx5_query_hca_vport_context(dev->mdev, 0,
1008cdbe33d0SEli Cohen 					   port_num, 0, &ctx);
1009cdbe33d0SEli Cohen 	if (err)
1010cdbe33d0SEli Cohen 		return err;
1011cdbe33d0SEli Cohen 
1012cdbe33d0SEli Cohen 	if (~ctx.cap_mask1_perm & mask) {
1013cdbe33d0SEli Cohen 		mlx5_ib_warn(dev, "trying to change bitmask 0x%X but change supported 0x%X\n",
1014cdbe33d0SEli Cohen 			     mask, ctx.cap_mask1_perm);
1015cdbe33d0SEli Cohen 		return -EINVAL;
1016cdbe33d0SEli Cohen 	}
1017cdbe33d0SEli Cohen 
1018cdbe33d0SEli Cohen 	ctx.cap_mask1 = value;
1019cdbe33d0SEli Cohen 	ctx.cap_mask1_perm = mask;
1020cdbe33d0SEli Cohen 	err = mlx5_core_modify_hca_vport_context(dev->mdev, 0,
1021cdbe33d0SEli Cohen 						 port_num, 0, &ctx);
1022cdbe33d0SEli Cohen 
1023cdbe33d0SEli Cohen 	return err;
1024cdbe33d0SEli Cohen }
1025cdbe33d0SEli Cohen 
1026e126ba97SEli Cohen static int mlx5_ib_modify_port(struct ib_device *ibdev, u8 port, int mask,
1027e126ba97SEli Cohen 			       struct ib_port_modify *props)
1028e126ba97SEli Cohen {
1029e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1030e126ba97SEli Cohen 	struct ib_port_attr attr;
1031e126ba97SEli Cohen 	u32 tmp;
1032e126ba97SEli Cohen 	int err;
1033cdbe33d0SEli Cohen 	u32 change_mask;
1034cdbe33d0SEli Cohen 	u32 value;
1035cdbe33d0SEli Cohen 	bool is_ib = (mlx5_ib_port_link_layer(ibdev, port) ==
1036cdbe33d0SEli Cohen 		      IB_LINK_LAYER_INFINIBAND);
1037cdbe33d0SEli Cohen 
1038cdbe33d0SEli Cohen 	if (MLX5_CAP_GEN(dev->mdev, ib_virt) && is_ib) {
1039cdbe33d0SEli Cohen 		change_mask = props->clr_port_cap_mask | props->set_port_cap_mask;
1040cdbe33d0SEli Cohen 		value = ~props->clr_port_cap_mask | props->set_port_cap_mask;
1041cdbe33d0SEli Cohen 		return set_port_caps_atomic(dev, port, change_mask, value);
1042cdbe33d0SEli Cohen 	}
1043e126ba97SEli Cohen 
1044e126ba97SEli Cohen 	mutex_lock(&dev->cap_mask_mutex);
1045e126ba97SEli Cohen 
1046c4550c63SOr Gerlitz 	err = ib_query_port(ibdev, port, &attr);
1047e126ba97SEli Cohen 	if (err)
1048e126ba97SEli Cohen 		goto out;
1049e126ba97SEli Cohen 
1050e126ba97SEli Cohen 	tmp = (attr.port_cap_flags | props->set_port_cap_mask) &
1051e126ba97SEli Cohen 		~props->clr_port_cap_mask;
1052e126ba97SEli Cohen 
10539603b61dSJack Morgenstein 	err = mlx5_set_port_caps(dev->mdev, port, tmp);
1054e126ba97SEli Cohen 
1055e126ba97SEli Cohen out:
1056e126ba97SEli Cohen 	mutex_unlock(&dev->cap_mask_mutex);
1057e126ba97SEli Cohen 	return err;
1058e126ba97SEli Cohen }
1059e126ba97SEli Cohen 
106030aa60b3SEli Cohen static void print_lib_caps(struct mlx5_ib_dev *dev, u64 caps)
106130aa60b3SEli Cohen {
106230aa60b3SEli Cohen 	mlx5_ib_dbg(dev, "MLX5_LIB_CAP_4K_UAR = %s\n",
106330aa60b3SEli Cohen 		    caps & MLX5_LIB_CAP_4K_UAR ? "y" : "n");
106430aa60b3SEli Cohen }
106530aa60b3SEli Cohen 
1066b037c29aSEli Cohen static int calc_total_bfregs(struct mlx5_ib_dev *dev, bool lib_uar_4k,
1067b037c29aSEli Cohen 			     struct mlx5_ib_alloc_ucontext_req_v2 *req,
1068b037c29aSEli Cohen 			     u32 *num_sys_pages)
1069b037c29aSEli Cohen {
1070b037c29aSEli Cohen 	int uars_per_sys_page;
1071b037c29aSEli Cohen 	int bfregs_per_sys_page;
1072b037c29aSEli Cohen 	int ref_bfregs = req->total_num_bfregs;
1073b037c29aSEli Cohen 
1074b037c29aSEli Cohen 	if (req->total_num_bfregs == 0)
1075b037c29aSEli Cohen 		return -EINVAL;
1076b037c29aSEli Cohen 
1077b037c29aSEli Cohen 	BUILD_BUG_ON(MLX5_MAX_BFREGS % MLX5_NON_FP_BFREGS_IN_PAGE);
1078b037c29aSEli Cohen 	BUILD_BUG_ON(MLX5_MAX_BFREGS < MLX5_NON_FP_BFREGS_IN_PAGE);
1079b037c29aSEli Cohen 
1080b037c29aSEli Cohen 	if (req->total_num_bfregs > MLX5_MAX_BFREGS)
1081b037c29aSEli Cohen 		return -ENOMEM;
1082b037c29aSEli Cohen 
1083b037c29aSEli Cohen 	uars_per_sys_page = get_uars_per_sys_page(dev, lib_uar_4k);
1084b037c29aSEli Cohen 	bfregs_per_sys_page = uars_per_sys_page * MLX5_NON_FP_BFREGS_PER_UAR;
1085b037c29aSEli Cohen 	req->total_num_bfregs = ALIGN(req->total_num_bfregs, bfregs_per_sys_page);
1086b037c29aSEli Cohen 	*num_sys_pages = req->total_num_bfregs / bfregs_per_sys_page;
1087b037c29aSEli Cohen 
1088b037c29aSEli Cohen 	if (req->num_low_latency_bfregs > req->total_num_bfregs - 1)
1089b037c29aSEli Cohen 		return -EINVAL;
1090b037c29aSEli Cohen 
1091b037c29aSEli Cohen 	mlx5_ib_dbg(dev, "uar_4k: fw support %s, lib support %s, user requested %d bfregs, alloated %d, using %d sys pages\n",
1092b037c29aSEli Cohen 		    MLX5_CAP_GEN(dev->mdev, uar_4k) ? "yes" : "no",
1093b037c29aSEli Cohen 		    lib_uar_4k ? "yes" : "no", ref_bfregs,
1094b037c29aSEli Cohen 		    req->total_num_bfregs, *num_sys_pages);
1095b037c29aSEli Cohen 
1096b037c29aSEli Cohen 	return 0;
1097b037c29aSEli Cohen }
1098b037c29aSEli Cohen 
1099b037c29aSEli Cohen static int allocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
1100b037c29aSEli Cohen {
1101b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1102b037c29aSEli Cohen 	int err;
1103b037c29aSEli Cohen 	int i;
1104b037c29aSEli Cohen 
1105b037c29aSEli Cohen 	bfregi = &context->bfregi;
1106b037c29aSEli Cohen 	for (i = 0; i < bfregi->num_sys_pages; i++) {
1107b037c29aSEli Cohen 		err = mlx5_cmd_alloc_uar(dev->mdev, &bfregi->sys_pages[i]);
1108b037c29aSEli Cohen 		if (err)
1109b037c29aSEli Cohen 			goto error;
1110b037c29aSEli Cohen 
1111b037c29aSEli Cohen 		mlx5_ib_dbg(dev, "allocated uar %d\n", bfregi->sys_pages[i]);
1112b037c29aSEli Cohen 	}
1113b037c29aSEli Cohen 	return 0;
1114b037c29aSEli Cohen 
1115b037c29aSEli Cohen error:
1116b037c29aSEli Cohen 	for (--i; i >= 0; i--)
1117b037c29aSEli Cohen 		if (mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]))
1118b037c29aSEli Cohen 			mlx5_ib_warn(dev, "failed to free uar %d\n", i);
1119b037c29aSEli Cohen 
1120b037c29aSEli Cohen 	return err;
1121b037c29aSEli Cohen }
1122b037c29aSEli Cohen 
1123b037c29aSEli Cohen static int deallocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
1124b037c29aSEli Cohen {
1125b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1126b037c29aSEli Cohen 	int err;
1127b037c29aSEli Cohen 	int i;
1128b037c29aSEli Cohen 
1129b037c29aSEli Cohen 	bfregi = &context->bfregi;
1130b037c29aSEli Cohen 	for (i = 0; i < bfregi->num_sys_pages; i++) {
1131b037c29aSEli Cohen 		err = mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]);
1132b037c29aSEli Cohen 		if (err) {
1133b037c29aSEli Cohen 			mlx5_ib_warn(dev, "failed to free uar %d\n", i);
1134b037c29aSEli Cohen 			return err;
1135b037c29aSEli Cohen 		}
1136b037c29aSEli Cohen 	}
1137b037c29aSEli Cohen 	return 0;
1138b037c29aSEli Cohen }
1139b037c29aSEli Cohen 
1140e126ba97SEli Cohen static struct ib_ucontext *mlx5_ib_alloc_ucontext(struct ib_device *ibdev,
1141e126ba97SEli Cohen 						  struct ib_udata *udata)
1142e126ba97SEli Cohen {
1143e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1144b368d7cbSMatan Barak 	struct mlx5_ib_alloc_ucontext_req_v2 req = {};
1145b368d7cbSMatan Barak 	struct mlx5_ib_alloc_ucontext_resp resp = {};
1146e126ba97SEli Cohen 	struct mlx5_ib_ucontext *context;
11472f5ff264SEli Cohen 	struct mlx5_bfreg_info *bfregi;
114878c0f98cSEli Cohen 	int ver;
1149e126ba97SEli Cohen 	int err;
1150f241e749SJack Morgenstein 	size_t reqlen;
1151a168a41cSMajd Dibbiny 	size_t min_req_v2 = offsetof(struct mlx5_ib_alloc_ucontext_req_v2,
1152a168a41cSMajd Dibbiny 				     max_cqe_version);
1153b037c29aSEli Cohen 	bool lib_uar_4k;
1154e126ba97SEli Cohen 
1155e126ba97SEli Cohen 	if (!dev->ib_active)
1156e126ba97SEli Cohen 		return ERR_PTR(-EAGAIN);
1157e126ba97SEli Cohen 
1158dfbee859SHaggai Abramovsky 	if (udata->inlen < sizeof(struct ib_uverbs_cmd_hdr))
1159dfbee859SHaggai Abramovsky 		return ERR_PTR(-EINVAL);
1160dfbee859SHaggai Abramovsky 
116178c0f98cSEli Cohen 	reqlen = udata->inlen - sizeof(struct ib_uverbs_cmd_hdr);
116278c0f98cSEli Cohen 	if (reqlen == sizeof(struct mlx5_ib_alloc_ucontext_req))
116378c0f98cSEli Cohen 		ver = 0;
1164a168a41cSMajd Dibbiny 	else if (reqlen >= min_req_v2)
116578c0f98cSEli Cohen 		ver = 2;
116678c0f98cSEli Cohen 	else
116778c0f98cSEli Cohen 		return ERR_PTR(-EINVAL);
116878c0f98cSEli Cohen 
1169b368d7cbSMatan Barak 	err = ib_copy_from_udata(&req, udata, min(reqlen, sizeof(req)));
1170e126ba97SEli Cohen 	if (err)
1171e126ba97SEli Cohen 		return ERR_PTR(err);
1172e126ba97SEli Cohen 
1173b368d7cbSMatan Barak 	if (req.flags)
117478c0f98cSEli Cohen 		return ERR_PTR(-EINVAL);
117578c0f98cSEli Cohen 
1176f72300c5SHaggai Abramovsky 	if (req.comp_mask || req.reserved0 || req.reserved1 || req.reserved2)
1177b368d7cbSMatan Barak 		return ERR_PTR(-EOPNOTSUPP);
1178b368d7cbSMatan Barak 
11792f5ff264SEli Cohen 	req.total_num_bfregs = ALIGN(req.total_num_bfregs,
11802f5ff264SEli Cohen 				    MLX5_NON_FP_BFREGS_PER_UAR);
11812f5ff264SEli Cohen 	if (req.num_low_latency_bfregs > req.total_num_bfregs - 1)
1182e126ba97SEli Cohen 		return ERR_PTR(-EINVAL);
1183e126ba97SEli Cohen 
1184938fe83cSSaeed Mahameed 	resp.qp_tab_size = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp);
11852cc6ad5fSNoa Osherovich 	if (mlx5_core_is_pf(dev->mdev) && MLX5_CAP_GEN(dev->mdev, bf))
1186938fe83cSSaeed Mahameed 		resp.bf_reg_size = 1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size);
1187b47bd6eaSDaniel Jurgens 	resp.cache_line_size = cache_line_size();
1188938fe83cSSaeed Mahameed 	resp.max_sq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq);
1189938fe83cSSaeed Mahameed 	resp.max_rq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq);
1190938fe83cSSaeed Mahameed 	resp.max_send_wqebb = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1191938fe83cSSaeed Mahameed 	resp.max_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1192938fe83cSSaeed Mahameed 	resp.max_srq_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz);
1193f72300c5SHaggai Abramovsky 	resp.cqe_version = min_t(__u8,
1194f72300c5SHaggai Abramovsky 				 (__u8)MLX5_CAP_GEN(dev->mdev, cqe_version),
1195f72300c5SHaggai Abramovsky 				 req.max_cqe_version);
119630aa60b3SEli Cohen 	resp.log_uar_size = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
119730aa60b3SEli Cohen 				MLX5_ADAPTER_PAGE_SHIFT : PAGE_SHIFT;
119830aa60b3SEli Cohen 	resp.num_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
119930aa60b3SEli Cohen 					MLX5_CAP_GEN(dev->mdev, num_of_uars_per_page) : 1;
1200b368d7cbSMatan Barak 	resp.response_length = min(offsetof(typeof(resp), response_length) +
1201b368d7cbSMatan Barak 				   sizeof(resp.response_length), udata->outlen);
1202e126ba97SEli Cohen 
1203e126ba97SEli Cohen 	context = kzalloc(sizeof(*context), GFP_KERNEL);
1204e126ba97SEli Cohen 	if (!context)
1205e126ba97SEli Cohen 		return ERR_PTR(-ENOMEM);
1206e126ba97SEli Cohen 
120730aa60b3SEli Cohen 	lib_uar_4k = req.lib_caps & MLX5_LIB_CAP_4K_UAR;
12082f5ff264SEli Cohen 	bfregi = &context->bfregi;
1209b037c29aSEli Cohen 
1210b037c29aSEli Cohen 	/* updates req->total_num_bfregs */
1211b037c29aSEli Cohen 	err = calc_total_bfregs(dev, lib_uar_4k, &req, &bfregi->num_sys_pages);
1212b037c29aSEli Cohen 	if (err)
1213b037c29aSEli Cohen 		goto out_ctx;
1214b037c29aSEli Cohen 
12152f5ff264SEli Cohen 	mutex_init(&bfregi->lock);
1216b037c29aSEli Cohen 	bfregi->lib_uar_4k = lib_uar_4k;
1217b037c29aSEli Cohen 	bfregi->count = kcalloc(req.total_num_bfregs, sizeof(*bfregi->count),
1218b037c29aSEli Cohen 				GFP_KERNEL);
1219b037c29aSEli Cohen 	if (!bfregi->count) {
1220e126ba97SEli Cohen 		err = -ENOMEM;
1221e126ba97SEli Cohen 		goto out_ctx;
1222e126ba97SEli Cohen 	}
1223e126ba97SEli Cohen 
1224b037c29aSEli Cohen 	bfregi->sys_pages = kcalloc(bfregi->num_sys_pages,
1225b037c29aSEli Cohen 				    sizeof(*bfregi->sys_pages),
1226e126ba97SEli Cohen 				    GFP_KERNEL);
1227b037c29aSEli Cohen 	if (!bfregi->sys_pages) {
1228e126ba97SEli Cohen 		err = -ENOMEM;
1229e126ba97SEli Cohen 		goto out_count;
1230e126ba97SEli Cohen 	}
1231e126ba97SEli Cohen 
1232b037c29aSEli Cohen 	err = allocate_uars(dev, context);
1233b037c29aSEli Cohen 	if (err)
1234b037c29aSEli Cohen 		goto out_sys_pages;
1235b037c29aSEli Cohen 
1236b4cfe447SHaggai Eran #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
1237b4cfe447SHaggai Eran 	context->ibucontext.invalidate_range = &mlx5_ib_invalidate_range;
1238b4cfe447SHaggai Eran #endif
1239b4cfe447SHaggai Eran 
12407d0cc6edSArtemy Kovalyov 	context->upd_xlt_page = __get_free_page(GFP_KERNEL);
12417d0cc6edSArtemy Kovalyov 	if (!context->upd_xlt_page) {
12427d0cc6edSArtemy Kovalyov 		err = -ENOMEM;
12437d0cc6edSArtemy Kovalyov 		goto out_uars;
12447d0cc6edSArtemy Kovalyov 	}
12457d0cc6edSArtemy Kovalyov 	mutex_init(&context->upd_xlt_page_mutex);
12467d0cc6edSArtemy Kovalyov 
1247146d2f1aSmajd@mellanox.com 	if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain)) {
1248146d2f1aSmajd@mellanox.com 		err = mlx5_core_alloc_transport_domain(dev->mdev,
1249146d2f1aSmajd@mellanox.com 						       &context->tdn);
1250146d2f1aSmajd@mellanox.com 		if (err)
12517d0cc6edSArtemy Kovalyov 			goto out_page;
1252146d2f1aSmajd@mellanox.com 	}
1253146d2f1aSmajd@mellanox.com 
12547c2344c3SMaor Gottlieb 	INIT_LIST_HEAD(&context->vma_private_list);
1255e126ba97SEli Cohen 	INIT_LIST_HEAD(&context->db_page_list);
1256e126ba97SEli Cohen 	mutex_init(&context->db_page_mutex);
1257e126ba97SEli Cohen 
12582f5ff264SEli Cohen 	resp.tot_bfregs = req.total_num_bfregs;
1259938fe83cSSaeed Mahameed 	resp.num_ports = MLX5_CAP_GEN(dev->mdev, num_ports);
1260b368d7cbSMatan Barak 
1261f72300c5SHaggai Abramovsky 	if (field_avail(typeof(resp), cqe_version, udata->outlen))
1262f72300c5SHaggai Abramovsky 		resp.response_length += sizeof(resp.cqe_version);
1263b368d7cbSMatan Barak 
1264402ca536SBodong Wang 	if (field_avail(typeof(resp), cmds_supp_uhw, udata->outlen)) {
12656ad279c5SMoni Shoua 		resp.cmds_supp_uhw |= MLX5_USER_CMDS_SUPP_UHW_QUERY_DEVICE |
12666ad279c5SMoni Shoua 				      MLX5_USER_CMDS_SUPP_UHW_CREATE_AH;
1267402ca536SBodong Wang 		resp.response_length += sizeof(resp.cmds_supp_uhw);
1268402ca536SBodong Wang 	}
1269402ca536SBodong Wang 
127078984898SOr Gerlitz 	if (field_avail(typeof(resp), eth_min_inline, udata->outlen)) {
127178984898SOr Gerlitz 		if (mlx5_ib_port_link_layer(ibdev, 1) == IB_LINK_LAYER_ETHERNET) {
127278984898SOr Gerlitz 			mlx5_query_min_inline(dev->mdev, &resp.eth_min_inline);
127378984898SOr Gerlitz 			resp.eth_min_inline++;
127478984898SOr Gerlitz 		}
127578984898SOr Gerlitz 		resp.response_length += sizeof(resp.eth_min_inline);
127678984898SOr Gerlitz 	}
127778984898SOr Gerlitz 
1278bc5c6eedSNoa Osherovich 	/*
1279bc5c6eedSNoa Osherovich 	 * We don't want to expose information from the PCI bar that is located
1280bc5c6eedSNoa Osherovich 	 * after 4096 bytes, so if the arch only supports larger pages, let's
1281bc5c6eedSNoa Osherovich 	 * pretend we don't support reading the HCA's core clock. This is also
1282bc5c6eedSNoa Osherovich 	 * forced by mmap function.
1283bc5c6eedSNoa Osherovich 	 */
1284de8d6e02SEli Cohen 	if (field_avail(typeof(resp), hca_core_clock_offset, udata->outlen)) {
1285de8d6e02SEli Cohen 		if (PAGE_SIZE <= 4096) {
1286b368d7cbSMatan Barak 			resp.comp_mask |=
1287b368d7cbSMatan Barak 				MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_CORE_CLOCK_OFFSET;
1288b368d7cbSMatan Barak 			resp.hca_core_clock_offset =
1289de8d6e02SEli Cohen 				offsetof(struct mlx5_init_seg, internal_timer_h) % PAGE_SIZE;
1290de8d6e02SEli Cohen 		}
1291f72300c5SHaggai Abramovsky 		resp.response_length += sizeof(resp.hca_core_clock_offset) +
1292402ca536SBodong Wang 					sizeof(resp.reserved2);
1293b368d7cbSMatan Barak 	}
1294b368d7cbSMatan Barak 
129530aa60b3SEli Cohen 	if (field_avail(typeof(resp), log_uar_size, udata->outlen))
129630aa60b3SEli Cohen 		resp.response_length += sizeof(resp.log_uar_size);
129730aa60b3SEli Cohen 
129830aa60b3SEli Cohen 	if (field_avail(typeof(resp), num_uars_per_page, udata->outlen))
129930aa60b3SEli Cohen 		resp.response_length += sizeof(resp.num_uars_per_page);
130030aa60b3SEli Cohen 
1301b368d7cbSMatan Barak 	err = ib_copy_to_udata(udata, &resp, resp.response_length);
1302e126ba97SEli Cohen 	if (err)
1303146d2f1aSmajd@mellanox.com 		goto out_td;
1304e126ba97SEli Cohen 
13052f5ff264SEli Cohen 	bfregi->ver = ver;
13062f5ff264SEli Cohen 	bfregi->num_low_latency_bfregs = req.num_low_latency_bfregs;
1307f72300c5SHaggai Abramovsky 	context->cqe_version = resp.cqe_version;
130830aa60b3SEli Cohen 	context->lib_caps = req.lib_caps;
130930aa60b3SEli Cohen 	print_lib_caps(dev, context->lib_caps);
1310f72300c5SHaggai Abramovsky 
1311e126ba97SEli Cohen 	return &context->ibucontext;
1312e126ba97SEli Cohen 
1313146d2f1aSmajd@mellanox.com out_td:
1314146d2f1aSmajd@mellanox.com 	if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1315146d2f1aSmajd@mellanox.com 		mlx5_core_dealloc_transport_domain(dev->mdev, context->tdn);
1316146d2f1aSmajd@mellanox.com 
13177d0cc6edSArtemy Kovalyov out_page:
13187d0cc6edSArtemy Kovalyov 	free_page(context->upd_xlt_page);
13197d0cc6edSArtemy Kovalyov 
1320e126ba97SEli Cohen out_uars:
1321b037c29aSEli Cohen 	deallocate_uars(dev, context);
1322b037c29aSEli Cohen 
1323b037c29aSEli Cohen out_sys_pages:
1324b037c29aSEli Cohen 	kfree(bfregi->sys_pages);
1325b037c29aSEli Cohen 
1326e126ba97SEli Cohen out_count:
13272f5ff264SEli Cohen 	kfree(bfregi->count);
1328e126ba97SEli Cohen 
1329e126ba97SEli Cohen out_ctx:
1330e126ba97SEli Cohen 	kfree(context);
1331b037c29aSEli Cohen 
1332e126ba97SEli Cohen 	return ERR_PTR(err);
1333e126ba97SEli Cohen }
1334e126ba97SEli Cohen 
1335e126ba97SEli Cohen static int mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
1336e126ba97SEli Cohen {
1337e126ba97SEli Cohen 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1338e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
1339b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1340e126ba97SEli Cohen 
1341b037c29aSEli Cohen 	bfregi = &context->bfregi;
1342146d2f1aSmajd@mellanox.com 	if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1343146d2f1aSmajd@mellanox.com 		mlx5_core_dealloc_transport_domain(dev->mdev, context->tdn);
1344146d2f1aSmajd@mellanox.com 
13457d0cc6edSArtemy Kovalyov 	free_page(context->upd_xlt_page);
1346b037c29aSEli Cohen 	deallocate_uars(dev, context);
1347b037c29aSEli Cohen 	kfree(bfregi->sys_pages);
13482f5ff264SEli Cohen 	kfree(bfregi->count);
1349e126ba97SEli Cohen 	kfree(context);
1350e126ba97SEli Cohen 
1351e126ba97SEli Cohen 	return 0;
1352e126ba97SEli Cohen }
1353e126ba97SEli Cohen 
1354b037c29aSEli Cohen static phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev,
1355b037c29aSEli Cohen 				 struct mlx5_bfreg_info *bfregi,
1356b037c29aSEli Cohen 				 int idx)
1357e126ba97SEli Cohen {
1358b037c29aSEli Cohen 	int fw_uars_per_page;
1359b037c29aSEli Cohen 
1360b037c29aSEli Cohen 	fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ? MLX5_UARS_IN_PAGE : 1;
1361b037c29aSEli Cohen 
1362b037c29aSEli Cohen 	return (pci_resource_start(dev->mdev->pdev, 0) >> PAGE_SHIFT) +
1363b037c29aSEli Cohen 			bfregi->sys_pages[idx] / fw_uars_per_page;
1364e126ba97SEli Cohen }
1365e126ba97SEli Cohen 
1366e126ba97SEli Cohen static int get_command(unsigned long offset)
1367e126ba97SEli Cohen {
1368e126ba97SEli Cohen 	return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK;
1369e126ba97SEli Cohen }
1370e126ba97SEli Cohen 
1371e126ba97SEli Cohen static int get_arg(unsigned long offset)
1372e126ba97SEli Cohen {
1373e126ba97SEli Cohen 	return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1);
1374e126ba97SEli Cohen }
1375e126ba97SEli Cohen 
1376e126ba97SEli Cohen static int get_index(unsigned long offset)
1377e126ba97SEli Cohen {
1378e126ba97SEli Cohen 	return get_arg(offset);
1379e126ba97SEli Cohen }
1380e126ba97SEli Cohen 
13817c2344c3SMaor Gottlieb static void  mlx5_ib_vma_open(struct vm_area_struct *area)
13827c2344c3SMaor Gottlieb {
13837c2344c3SMaor Gottlieb 	/* vma_open is called when a new VMA is created on top of our VMA.  This
13847c2344c3SMaor Gottlieb 	 * is done through either mremap flow or split_vma (usually due to
13857c2344c3SMaor Gottlieb 	 * mlock, madvise, munmap, etc.) We do not support a clone of the VMA,
13867c2344c3SMaor Gottlieb 	 * as this VMA is strongly hardware related.  Therefore we set the
13877c2344c3SMaor Gottlieb 	 * vm_ops of the newly created/cloned VMA to NULL, to prevent it from
13887c2344c3SMaor Gottlieb 	 * calling us again and trying to do incorrect actions.  We assume that
13897c2344c3SMaor Gottlieb 	 * the original VMA size is exactly a single page, and therefore all
13907c2344c3SMaor Gottlieb 	 * "splitting" operation will not happen to it.
13917c2344c3SMaor Gottlieb 	 */
13927c2344c3SMaor Gottlieb 	area->vm_ops = NULL;
13937c2344c3SMaor Gottlieb }
13947c2344c3SMaor Gottlieb 
13957c2344c3SMaor Gottlieb static void  mlx5_ib_vma_close(struct vm_area_struct *area)
13967c2344c3SMaor Gottlieb {
13977c2344c3SMaor Gottlieb 	struct mlx5_ib_vma_private_data *mlx5_ib_vma_priv_data;
13987c2344c3SMaor Gottlieb 
13997c2344c3SMaor Gottlieb 	/* It's guaranteed that all VMAs opened on a FD are closed before the
14007c2344c3SMaor Gottlieb 	 * file itself is closed, therefore no sync is needed with the regular
14017c2344c3SMaor Gottlieb 	 * closing flow. (e.g. mlx5 ib_dealloc_ucontext)
14027c2344c3SMaor Gottlieb 	 * However need a sync with accessing the vma as part of
14037c2344c3SMaor Gottlieb 	 * mlx5_ib_disassociate_ucontext.
14047c2344c3SMaor Gottlieb 	 * The close operation is usually called under mm->mmap_sem except when
14057c2344c3SMaor Gottlieb 	 * process is exiting.
14067c2344c3SMaor Gottlieb 	 * The exiting case is handled explicitly as part of
14077c2344c3SMaor Gottlieb 	 * mlx5_ib_disassociate_ucontext.
14087c2344c3SMaor Gottlieb 	 */
14097c2344c3SMaor Gottlieb 	mlx5_ib_vma_priv_data = (struct mlx5_ib_vma_private_data *)area->vm_private_data;
14107c2344c3SMaor Gottlieb 
14117c2344c3SMaor Gottlieb 	/* setting the vma context pointer to null in the mlx5_ib driver's
14127c2344c3SMaor Gottlieb 	 * private data, to protect a race condition in
14137c2344c3SMaor Gottlieb 	 * mlx5_ib_disassociate_ucontext().
14147c2344c3SMaor Gottlieb 	 */
14157c2344c3SMaor Gottlieb 	mlx5_ib_vma_priv_data->vma = NULL;
14167c2344c3SMaor Gottlieb 	list_del(&mlx5_ib_vma_priv_data->list);
14177c2344c3SMaor Gottlieb 	kfree(mlx5_ib_vma_priv_data);
14187c2344c3SMaor Gottlieb }
14197c2344c3SMaor Gottlieb 
14207c2344c3SMaor Gottlieb static const struct vm_operations_struct mlx5_ib_vm_ops = {
14217c2344c3SMaor Gottlieb 	.open = mlx5_ib_vma_open,
14227c2344c3SMaor Gottlieb 	.close = mlx5_ib_vma_close
14237c2344c3SMaor Gottlieb };
14247c2344c3SMaor Gottlieb 
14257c2344c3SMaor Gottlieb static int mlx5_ib_set_vma_data(struct vm_area_struct *vma,
14267c2344c3SMaor Gottlieb 				struct mlx5_ib_ucontext *ctx)
14277c2344c3SMaor Gottlieb {
14287c2344c3SMaor Gottlieb 	struct mlx5_ib_vma_private_data *vma_prv;
14297c2344c3SMaor Gottlieb 	struct list_head *vma_head = &ctx->vma_private_list;
14307c2344c3SMaor Gottlieb 
14317c2344c3SMaor Gottlieb 	vma_prv = kzalloc(sizeof(*vma_prv), GFP_KERNEL);
14327c2344c3SMaor Gottlieb 	if (!vma_prv)
14337c2344c3SMaor Gottlieb 		return -ENOMEM;
14347c2344c3SMaor Gottlieb 
14357c2344c3SMaor Gottlieb 	vma_prv->vma = vma;
14367c2344c3SMaor Gottlieb 	vma->vm_private_data = vma_prv;
14377c2344c3SMaor Gottlieb 	vma->vm_ops =  &mlx5_ib_vm_ops;
14387c2344c3SMaor Gottlieb 
14397c2344c3SMaor Gottlieb 	list_add(&vma_prv->list, vma_head);
14407c2344c3SMaor Gottlieb 
14417c2344c3SMaor Gottlieb 	return 0;
14427c2344c3SMaor Gottlieb }
14437c2344c3SMaor Gottlieb 
14447c2344c3SMaor Gottlieb static void mlx5_ib_disassociate_ucontext(struct ib_ucontext *ibcontext)
14457c2344c3SMaor Gottlieb {
14467c2344c3SMaor Gottlieb 	int ret;
14477c2344c3SMaor Gottlieb 	struct vm_area_struct *vma;
14487c2344c3SMaor Gottlieb 	struct mlx5_ib_vma_private_data *vma_private, *n;
14497c2344c3SMaor Gottlieb 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
14507c2344c3SMaor Gottlieb 	struct task_struct *owning_process  = NULL;
14517c2344c3SMaor Gottlieb 	struct mm_struct   *owning_mm       = NULL;
14527c2344c3SMaor Gottlieb 
14537c2344c3SMaor Gottlieb 	owning_process = get_pid_task(ibcontext->tgid, PIDTYPE_PID);
14547c2344c3SMaor Gottlieb 	if (!owning_process)
14557c2344c3SMaor Gottlieb 		return;
14567c2344c3SMaor Gottlieb 
14577c2344c3SMaor Gottlieb 	owning_mm = get_task_mm(owning_process);
14587c2344c3SMaor Gottlieb 	if (!owning_mm) {
14597c2344c3SMaor Gottlieb 		pr_info("no mm, disassociate ucontext is pending task termination\n");
14607c2344c3SMaor Gottlieb 		while (1) {
14617c2344c3SMaor Gottlieb 			put_task_struct(owning_process);
14627c2344c3SMaor Gottlieb 			usleep_range(1000, 2000);
14637c2344c3SMaor Gottlieb 			owning_process = get_pid_task(ibcontext->tgid,
14647c2344c3SMaor Gottlieb 						      PIDTYPE_PID);
14657c2344c3SMaor Gottlieb 			if (!owning_process ||
14667c2344c3SMaor Gottlieb 			    owning_process->state == TASK_DEAD) {
14677c2344c3SMaor Gottlieb 				pr_info("disassociate ucontext done, task was terminated\n");
14687c2344c3SMaor Gottlieb 				/* in case task was dead need to release the
14697c2344c3SMaor Gottlieb 				 * task struct.
14707c2344c3SMaor Gottlieb 				 */
14717c2344c3SMaor Gottlieb 				if (owning_process)
14727c2344c3SMaor Gottlieb 					put_task_struct(owning_process);
14737c2344c3SMaor Gottlieb 				return;
14747c2344c3SMaor Gottlieb 			}
14757c2344c3SMaor Gottlieb 		}
14767c2344c3SMaor Gottlieb 	}
14777c2344c3SMaor Gottlieb 
14787c2344c3SMaor Gottlieb 	/* need to protect from a race on closing the vma as part of
14797c2344c3SMaor Gottlieb 	 * mlx5_ib_vma_close.
14807c2344c3SMaor Gottlieb 	 */
1481ecc7d83bSMaor Gottlieb 	down_write(&owning_mm->mmap_sem);
14827c2344c3SMaor Gottlieb 	list_for_each_entry_safe(vma_private, n, &context->vma_private_list,
14837c2344c3SMaor Gottlieb 				 list) {
14847c2344c3SMaor Gottlieb 		vma = vma_private->vma;
14857c2344c3SMaor Gottlieb 		ret = zap_vma_ptes(vma, vma->vm_start,
14867c2344c3SMaor Gottlieb 				   PAGE_SIZE);
14877c2344c3SMaor Gottlieb 		WARN_ONCE(ret, "%s: zap_vma_ptes failed", __func__);
14887c2344c3SMaor Gottlieb 		/* context going to be destroyed, should
14897c2344c3SMaor Gottlieb 		 * not access ops any more.
14907c2344c3SMaor Gottlieb 		 */
1491*13776612SMaor Gottlieb 		vma->vm_flags &= ~(VM_SHARED | VM_MAYSHARE);
14927c2344c3SMaor Gottlieb 		vma->vm_ops = NULL;
14937c2344c3SMaor Gottlieb 		list_del(&vma_private->list);
14947c2344c3SMaor Gottlieb 		kfree(vma_private);
14957c2344c3SMaor Gottlieb 	}
1496ecc7d83bSMaor Gottlieb 	up_write(&owning_mm->mmap_sem);
14977c2344c3SMaor Gottlieb 	mmput(owning_mm);
14987c2344c3SMaor Gottlieb 	put_task_struct(owning_process);
14997c2344c3SMaor Gottlieb }
15007c2344c3SMaor Gottlieb 
150137aa5c36SGuy Levi static inline char *mmap_cmd2str(enum mlx5_ib_mmap_cmd cmd)
1502e126ba97SEli Cohen {
150337aa5c36SGuy Levi 	switch (cmd) {
150437aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
150537aa5c36SGuy Levi 		return "WC";
1506e126ba97SEli Cohen 	case MLX5_IB_MMAP_REGULAR_PAGE:
150737aa5c36SGuy Levi 		return "best effort WC";
150837aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
150937aa5c36SGuy Levi 		return "NC";
151037aa5c36SGuy Levi 	default:
151137aa5c36SGuy Levi 		return NULL;
151237aa5c36SGuy Levi 	}
151337aa5c36SGuy Levi }
151437aa5c36SGuy Levi 
151537aa5c36SGuy Levi static int uar_mmap(struct mlx5_ib_dev *dev, enum mlx5_ib_mmap_cmd cmd,
15167c2344c3SMaor Gottlieb 		    struct vm_area_struct *vma,
15177c2344c3SMaor Gottlieb 		    struct mlx5_ib_ucontext *context)
151837aa5c36SGuy Levi {
15192f5ff264SEli Cohen 	struct mlx5_bfreg_info *bfregi = &context->bfregi;
152037aa5c36SGuy Levi 	int err;
152137aa5c36SGuy Levi 	unsigned long idx;
152237aa5c36SGuy Levi 	phys_addr_t pfn, pa;
152337aa5c36SGuy Levi 	pgprot_t prot;
1524b037c29aSEli Cohen 	int uars_per_page;
1525b037c29aSEli Cohen 
1526b037c29aSEli Cohen 	if (vma->vm_end - vma->vm_start != PAGE_SIZE)
1527b037c29aSEli Cohen 		return -EINVAL;
1528b037c29aSEli Cohen 
1529b037c29aSEli Cohen 	uars_per_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k);
1530b037c29aSEli Cohen 	idx = get_index(vma->vm_pgoff);
1531b037c29aSEli Cohen 	if (idx % uars_per_page ||
1532b037c29aSEli Cohen 	    idx * uars_per_page >= bfregi->num_sys_pages) {
1533b037c29aSEli Cohen 		mlx5_ib_warn(dev, "invalid uar index %lu\n", idx);
1534b037c29aSEli Cohen 		return -EINVAL;
1535b037c29aSEli Cohen 	}
153637aa5c36SGuy Levi 
153737aa5c36SGuy Levi 	switch (cmd) {
153837aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
153937aa5c36SGuy Levi /* Some architectures don't support WC memory */
154037aa5c36SGuy Levi #if defined(CONFIG_X86)
154137aa5c36SGuy Levi 		if (!pat_enabled())
154237aa5c36SGuy Levi 			return -EPERM;
154337aa5c36SGuy Levi #elif !(defined(CONFIG_PPC) || (defined(CONFIG_ARM) && defined(CONFIG_MMU)))
154437aa5c36SGuy Levi 			return -EPERM;
154537aa5c36SGuy Levi #endif
154637aa5c36SGuy Levi 	/* fall through */
154737aa5c36SGuy Levi 	case MLX5_IB_MMAP_REGULAR_PAGE:
154837aa5c36SGuy Levi 		/* For MLX5_IB_MMAP_REGULAR_PAGE do the best effort to get WC */
154937aa5c36SGuy Levi 		prot = pgprot_writecombine(vma->vm_page_prot);
155037aa5c36SGuy Levi 		break;
155137aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
155237aa5c36SGuy Levi 		prot = pgprot_noncached(vma->vm_page_prot);
155337aa5c36SGuy Levi 		break;
155437aa5c36SGuy Levi 	default:
155537aa5c36SGuy Levi 		return -EINVAL;
155637aa5c36SGuy Levi 	}
155737aa5c36SGuy Levi 
1558b037c29aSEli Cohen 	pfn = uar_index2pfn(dev, bfregi, idx);
155937aa5c36SGuy Levi 	mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn %pa\n", idx, &pfn);
1560e126ba97SEli Cohen 
156137aa5c36SGuy Levi 	vma->vm_page_prot = prot;
156237aa5c36SGuy Levi 	err = io_remap_pfn_range(vma, vma->vm_start, pfn,
156337aa5c36SGuy Levi 				 PAGE_SIZE, vma->vm_page_prot);
156437aa5c36SGuy Levi 	if (err) {
156537aa5c36SGuy Levi 		mlx5_ib_err(dev, "io_remap_pfn_range failed with error=%d, vm_start=0x%lx, pfn=%pa, mmap_cmd=%s\n",
156637aa5c36SGuy Levi 			    err, vma->vm_start, &pfn, mmap_cmd2str(cmd));
1567e126ba97SEli Cohen 		return -EAGAIN;
156837aa5c36SGuy Levi 	}
1569e126ba97SEli Cohen 
157037aa5c36SGuy Levi 	pa = pfn << PAGE_SHIFT;
157137aa5c36SGuy Levi 	mlx5_ib_dbg(dev, "mapped %s at 0x%lx, PA %pa\n", mmap_cmd2str(cmd),
157237aa5c36SGuy Levi 		    vma->vm_start, &pa);
157337aa5c36SGuy Levi 
15747c2344c3SMaor Gottlieb 	return mlx5_ib_set_vma_data(vma, context);
157537aa5c36SGuy Levi }
157637aa5c36SGuy Levi 
157737aa5c36SGuy Levi static int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma)
157837aa5c36SGuy Levi {
157937aa5c36SGuy Levi 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
158037aa5c36SGuy Levi 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
158137aa5c36SGuy Levi 	unsigned long command;
158237aa5c36SGuy Levi 	phys_addr_t pfn;
158337aa5c36SGuy Levi 
158437aa5c36SGuy Levi 	command = get_command(vma->vm_pgoff);
158537aa5c36SGuy Levi 	switch (command) {
158637aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
158737aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
158837aa5c36SGuy Levi 	case MLX5_IB_MMAP_REGULAR_PAGE:
15897c2344c3SMaor Gottlieb 		return uar_mmap(dev, command, vma, context);
1590e126ba97SEli Cohen 
1591e126ba97SEli Cohen 	case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES:
1592e126ba97SEli Cohen 		return -ENOSYS;
1593e126ba97SEli Cohen 
1594d69e3bcfSMatan Barak 	case MLX5_IB_MMAP_CORE_CLOCK:
1595d69e3bcfSMatan Barak 		if (vma->vm_end - vma->vm_start != PAGE_SIZE)
1596d69e3bcfSMatan Barak 			return -EINVAL;
1597d69e3bcfSMatan Barak 
15986cbac1e4SMatan Barak 		if (vma->vm_flags & VM_WRITE)
1599d69e3bcfSMatan Barak 			return -EPERM;
1600d69e3bcfSMatan Barak 
1601d69e3bcfSMatan Barak 		/* Don't expose to user-space information it shouldn't have */
1602d69e3bcfSMatan Barak 		if (PAGE_SIZE > 4096)
1603d69e3bcfSMatan Barak 			return -EOPNOTSUPP;
1604d69e3bcfSMatan Barak 
1605d69e3bcfSMatan Barak 		vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1606d69e3bcfSMatan Barak 		pfn = (dev->mdev->iseg_base +
1607d69e3bcfSMatan Barak 		       offsetof(struct mlx5_init_seg, internal_timer_h)) >>
1608d69e3bcfSMatan Barak 			PAGE_SHIFT;
1609d69e3bcfSMatan Barak 		if (io_remap_pfn_range(vma, vma->vm_start, pfn,
1610d69e3bcfSMatan Barak 				       PAGE_SIZE, vma->vm_page_prot))
1611d69e3bcfSMatan Barak 			return -EAGAIN;
1612d69e3bcfSMatan Barak 
1613d69e3bcfSMatan Barak 		mlx5_ib_dbg(dev, "mapped internal timer at 0x%lx, PA 0x%llx\n",
1614d69e3bcfSMatan Barak 			    vma->vm_start,
1615d69e3bcfSMatan Barak 			    (unsigned long long)pfn << PAGE_SHIFT);
1616d69e3bcfSMatan Barak 		break;
1617d69e3bcfSMatan Barak 
1618e126ba97SEli Cohen 	default:
1619e126ba97SEli Cohen 		return -EINVAL;
1620e126ba97SEli Cohen 	}
1621e126ba97SEli Cohen 
1622e126ba97SEli Cohen 	return 0;
1623e126ba97SEli Cohen }
1624e126ba97SEli Cohen 
1625e126ba97SEli Cohen static struct ib_pd *mlx5_ib_alloc_pd(struct ib_device *ibdev,
1626e126ba97SEli Cohen 				      struct ib_ucontext *context,
1627e126ba97SEli Cohen 				      struct ib_udata *udata)
1628e126ba97SEli Cohen {
1629e126ba97SEli Cohen 	struct mlx5_ib_alloc_pd_resp resp;
1630e126ba97SEli Cohen 	struct mlx5_ib_pd *pd;
1631e126ba97SEli Cohen 	int err;
1632e126ba97SEli Cohen 
1633e126ba97SEli Cohen 	pd = kmalloc(sizeof(*pd), GFP_KERNEL);
1634e126ba97SEli Cohen 	if (!pd)
1635e126ba97SEli Cohen 		return ERR_PTR(-ENOMEM);
1636e126ba97SEli Cohen 
16379603b61dSJack Morgenstein 	err = mlx5_core_alloc_pd(to_mdev(ibdev)->mdev, &pd->pdn);
1638e126ba97SEli Cohen 	if (err) {
1639e126ba97SEli Cohen 		kfree(pd);
1640e126ba97SEli Cohen 		return ERR_PTR(err);
1641e126ba97SEli Cohen 	}
1642e126ba97SEli Cohen 
1643e126ba97SEli Cohen 	if (context) {
1644e126ba97SEli Cohen 		resp.pdn = pd->pdn;
1645e126ba97SEli Cohen 		if (ib_copy_to_udata(udata, &resp, sizeof(resp))) {
16469603b61dSJack Morgenstein 			mlx5_core_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn);
1647e126ba97SEli Cohen 			kfree(pd);
1648e126ba97SEli Cohen 			return ERR_PTR(-EFAULT);
1649e126ba97SEli Cohen 		}
1650e126ba97SEli Cohen 	}
1651e126ba97SEli Cohen 
1652e126ba97SEli Cohen 	return &pd->ibpd;
1653e126ba97SEli Cohen }
1654e126ba97SEli Cohen 
1655e126ba97SEli Cohen static int mlx5_ib_dealloc_pd(struct ib_pd *pd)
1656e126ba97SEli Cohen {
1657e126ba97SEli Cohen 	struct mlx5_ib_dev *mdev = to_mdev(pd->device);
1658e126ba97SEli Cohen 	struct mlx5_ib_pd *mpd = to_mpd(pd);
1659e126ba97SEli Cohen 
16609603b61dSJack Morgenstein 	mlx5_core_dealloc_pd(mdev->mdev, mpd->pdn);
1661e126ba97SEli Cohen 	kfree(mpd);
1662e126ba97SEli Cohen 
1663e126ba97SEli Cohen 	return 0;
1664e126ba97SEli Cohen }
1665e126ba97SEli Cohen 
1666466fa6d2SMaor Gottlieb enum {
1667466fa6d2SMaor Gottlieb 	MATCH_CRITERIA_ENABLE_OUTER_BIT,
1668466fa6d2SMaor Gottlieb 	MATCH_CRITERIA_ENABLE_MISC_BIT,
1669466fa6d2SMaor Gottlieb 	MATCH_CRITERIA_ENABLE_INNER_BIT
1670466fa6d2SMaor Gottlieb };
1671038d2ef8SMaor Gottlieb 
1672466fa6d2SMaor Gottlieb #define HEADER_IS_ZERO(match_criteria, headers)			           \
1673466fa6d2SMaor Gottlieb 	!(memchr_inv(MLX5_ADDR_OF(fte_match_param, match_criteria, headers), \
1674466fa6d2SMaor Gottlieb 		    0, MLX5_FLD_SZ_BYTES(fte_match_param, headers)))       \
1675466fa6d2SMaor Gottlieb 
1676466fa6d2SMaor Gottlieb static u8 get_match_criteria_enable(u32 *match_criteria)
1677466fa6d2SMaor Gottlieb {
1678466fa6d2SMaor Gottlieb 	u8 match_criteria_enable;
1679466fa6d2SMaor Gottlieb 
1680466fa6d2SMaor Gottlieb 	match_criteria_enable =
1681466fa6d2SMaor Gottlieb 		(!HEADER_IS_ZERO(match_criteria, outer_headers)) <<
1682466fa6d2SMaor Gottlieb 		MATCH_CRITERIA_ENABLE_OUTER_BIT;
1683466fa6d2SMaor Gottlieb 	match_criteria_enable |=
1684466fa6d2SMaor Gottlieb 		(!HEADER_IS_ZERO(match_criteria, misc_parameters)) <<
1685466fa6d2SMaor Gottlieb 		MATCH_CRITERIA_ENABLE_MISC_BIT;
1686466fa6d2SMaor Gottlieb 	match_criteria_enable |=
1687466fa6d2SMaor Gottlieb 		(!HEADER_IS_ZERO(match_criteria, inner_headers)) <<
1688466fa6d2SMaor Gottlieb 		MATCH_CRITERIA_ENABLE_INNER_BIT;
1689466fa6d2SMaor Gottlieb 
1690466fa6d2SMaor Gottlieb 	return match_criteria_enable;
1691038d2ef8SMaor Gottlieb }
1692038d2ef8SMaor Gottlieb 
1693ca0d4753SMaor Gottlieb static void set_proto(void *outer_c, void *outer_v, u8 mask, u8 val)
1694ca0d4753SMaor Gottlieb {
1695ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_protocol, mask);
1696ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_protocol, val);
1697ca0d4753SMaor Gottlieb }
1698ca0d4753SMaor Gottlieb 
16992d1e697eSMoses Reuben static void set_flow_label(void *misc_c, void *misc_v, u8 mask, u8 val,
17002d1e697eSMoses Reuben 			   bool inner)
17012d1e697eSMoses Reuben {
17022d1e697eSMoses Reuben 	if (inner) {
17032d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
17042d1e697eSMoses Reuben 			 misc_c, inner_ipv6_flow_label, mask);
17052d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
17062d1e697eSMoses Reuben 			 misc_v, inner_ipv6_flow_label, val);
17072d1e697eSMoses Reuben 	} else {
17082d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
17092d1e697eSMoses Reuben 			 misc_c, outer_ipv6_flow_label, mask);
17102d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
17112d1e697eSMoses Reuben 			 misc_v, outer_ipv6_flow_label, val);
17122d1e697eSMoses Reuben 	}
17132d1e697eSMoses Reuben }
17142d1e697eSMoses Reuben 
1715ca0d4753SMaor Gottlieb static void set_tos(void *outer_c, void *outer_v, u8 mask, u8 val)
1716ca0d4753SMaor Gottlieb {
1717ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_ecn, mask);
1718ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_ecn, val);
1719ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_dscp, mask >> 2);
1720ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_dscp, val >> 2);
1721ca0d4753SMaor Gottlieb }
1722ca0d4753SMaor Gottlieb 
1723c47ac6aeSMaor Gottlieb #define LAST_ETH_FIELD vlan_tag
1724c47ac6aeSMaor Gottlieb #define LAST_IB_FIELD sl
1725ca0d4753SMaor Gottlieb #define LAST_IPV4_FIELD tos
1726466fa6d2SMaor Gottlieb #define LAST_IPV6_FIELD traffic_class
1727c47ac6aeSMaor Gottlieb #define LAST_TCP_UDP_FIELD src_port
1728ffb30d8fSMoses Reuben #define LAST_TUNNEL_FIELD tunnel_id
17292ac693f9SMoses Reuben #define LAST_FLOW_TAG_FIELD tag_id
1730c47ac6aeSMaor Gottlieb 
1731c47ac6aeSMaor Gottlieb /* Field is the last supported field */
1732c47ac6aeSMaor Gottlieb #define FIELDS_NOT_SUPPORTED(filter, field)\
1733c47ac6aeSMaor Gottlieb 	memchr_inv((void *)&filter.field  +\
1734c47ac6aeSMaor Gottlieb 		   sizeof(filter.field), 0,\
1735c47ac6aeSMaor Gottlieb 		   sizeof(filter) -\
1736c47ac6aeSMaor Gottlieb 		   offsetof(typeof(filter), field) -\
1737c47ac6aeSMaor Gottlieb 		   sizeof(filter.field))
1738c47ac6aeSMaor Gottlieb 
1739038d2ef8SMaor Gottlieb static int parse_flow_attr(u32 *match_c, u32 *match_v,
17402ac693f9SMoses Reuben 			   const union ib_flow_spec *ib_spec, u32 *tag_id)
1741038d2ef8SMaor Gottlieb {
1742466fa6d2SMaor Gottlieb 	void *misc_params_c = MLX5_ADDR_OF(fte_match_param, match_c,
1743466fa6d2SMaor Gottlieb 					   misc_parameters);
1744466fa6d2SMaor Gottlieb 	void *misc_params_v = MLX5_ADDR_OF(fte_match_param, match_v,
1745466fa6d2SMaor Gottlieb 					   misc_parameters);
17462d1e697eSMoses Reuben 	void *headers_c;
17472d1e697eSMoses Reuben 	void *headers_v;
1748466fa6d2SMaor Gottlieb 
17492d1e697eSMoses Reuben 	if (ib_spec->type & IB_FLOW_SPEC_INNER) {
17502d1e697eSMoses Reuben 		headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
17512d1e697eSMoses Reuben 					 inner_headers);
17522d1e697eSMoses Reuben 		headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
17532d1e697eSMoses Reuben 					 inner_headers);
17542d1e697eSMoses Reuben 	} else {
17552d1e697eSMoses Reuben 		headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
17562d1e697eSMoses Reuben 					 outer_headers);
17572d1e697eSMoses Reuben 		headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
17582d1e697eSMoses Reuben 					 outer_headers);
17592d1e697eSMoses Reuben 	}
17602d1e697eSMoses Reuben 
17612d1e697eSMoses Reuben 	switch (ib_spec->type & ~IB_FLOW_SPEC_INNER) {
1762038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_ETH:
1763c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->eth.mask, LAST_ETH_FIELD))
17641ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
1765038d2ef8SMaor Gottlieb 
17662d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
1767038d2ef8SMaor Gottlieb 					     dmac_47_16),
1768038d2ef8SMaor Gottlieb 				ib_spec->eth.mask.dst_mac);
17692d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
1770038d2ef8SMaor Gottlieb 					     dmac_47_16),
1771038d2ef8SMaor Gottlieb 				ib_spec->eth.val.dst_mac);
1772038d2ef8SMaor Gottlieb 
17732d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
1774ee3da804SMaor Gottlieb 					     smac_47_16),
1775ee3da804SMaor Gottlieb 				ib_spec->eth.mask.src_mac);
17762d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
1777ee3da804SMaor Gottlieb 					     smac_47_16),
1778ee3da804SMaor Gottlieb 				ib_spec->eth.val.src_mac);
1779ee3da804SMaor Gottlieb 
1780038d2ef8SMaor Gottlieb 		if (ib_spec->eth.mask.vlan_tag) {
17812d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
178210543365SMohamad Haj Yahia 				 cvlan_tag, 1);
17832d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
178410543365SMohamad Haj Yahia 				 cvlan_tag, 1);
1785038d2ef8SMaor Gottlieb 
17862d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1787038d2ef8SMaor Gottlieb 				 first_vid, ntohs(ib_spec->eth.mask.vlan_tag));
17882d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1789038d2ef8SMaor Gottlieb 				 first_vid, ntohs(ib_spec->eth.val.vlan_tag));
1790038d2ef8SMaor Gottlieb 
17912d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1792038d2ef8SMaor Gottlieb 				 first_cfi,
1793038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.mask.vlan_tag) >> 12);
17942d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1795038d2ef8SMaor Gottlieb 				 first_cfi,
1796038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.val.vlan_tag) >> 12);
1797038d2ef8SMaor Gottlieb 
17982d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1799038d2ef8SMaor Gottlieb 				 first_prio,
1800038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.mask.vlan_tag) >> 13);
18012d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1802038d2ef8SMaor Gottlieb 				 first_prio,
1803038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.val.vlan_tag) >> 13);
1804038d2ef8SMaor Gottlieb 		}
18052d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1806038d2ef8SMaor Gottlieb 			 ethertype, ntohs(ib_spec->eth.mask.ether_type));
18072d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1808038d2ef8SMaor Gottlieb 			 ethertype, ntohs(ib_spec->eth.val.ether_type));
1809038d2ef8SMaor Gottlieb 		break;
1810038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_IPV4:
1811c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->ipv4.mask, LAST_IPV4_FIELD))
18121ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
1813038d2ef8SMaor Gottlieb 
18142d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1815038d2ef8SMaor Gottlieb 			 ethertype, 0xffff);
18162d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1817038d2ef8SMaor Gottlieb 			 ethertype, ETH_P_IP);
1818038d2ef8SMaor Gottlieb 
18192d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
1820038d2ef8SMaor Gottlieb 				    src_ipv4_src_ipv6.ipv4_layout.ipv4),
1821038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.mask.src_ip,
1822038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.mask.src_ip));
18232d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
1824038d2ef8SMaor Gottlieb 				    src_ipv4_src_ipv6.ipv4_layout.ipv4),
1825038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.val.src_ip,
1826038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.val.src_ip));
18272d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
1828038d2ef8SMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
1829038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.mask.dst_ip,
1830038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.mask.dst_ip));
18312d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
1832038d2ef8SMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
1833038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.val.dst_ip,
1834038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.val.dst_ip));
1835ca0d4753SMaor Gottlieb 
18362d1e697eSMoses Reuben 		set_tos(headers_c, headers_v,
1837ca0d4753SMaor Gottlieb 			ib_spec->ipv4.mask.tos, ib_spec->ipv4.val.tos);
1838ca0d4753SMaor Gottlieb 
18392d1e697eSMoses Reuben 		set_proto(headers_c, headers_v,
1840ca0d4753SMaor Gottlieb 			  ib_spec->ipv4.mask.proto, ib_spec->ipv4.val.proto);
1841038d2ef8SMaor Gottlieb 		break;
1842026bae0cSMaor Gottlieb 	case IB_FLOW_SPEC_IPV6:
1843c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->ipv6.mask, LAST_IPV6_FIELD))
18441ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
1845026bae0cSMaor Gottlieb 
18462d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1847026bae0cSMaor Gottlieb 			 ethertype, 0xffff);
18482d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1849026bae0cSMaor Gottlieb 			 ethertype, ETH_P_IPV6);
1850026bae0cSMaor Gottlieb 
18512d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
1852026bae0cSMaor Gottlieb 				    src_ipv4_src_ipv6.ipv6_layout.ipv6),
1853026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.mask.src_ip,
1854026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.mask.src_ip));
18552d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
1856026bae0cSMaor Gottlieb 				    src_ipv4_src_ipv6.ipv6_layout.ipv6),
1857026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.val.src_ip,
1858026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.val.src_ip));
18592d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
1860026bae0cSMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
1861026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.mask.dst_ip,
1862026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.mask.dst_ip));
18632d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
1864026bae0cSMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
1865026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.val.dst_ip,
1866026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.val.dst_ip));
1867466fa6d2SMaor Gottlieb 
18682d1e697eSMoses Reuben 		set_tos(headers_c, headers_v,
1869466fa6d2SMaor Gottlieb 			ib_spec->ipv6.mask.traffic_class,
1870466fa6d2SMaor Gottlieb 			ib_spec->ipv6.val.traffic_class);
1871466fa6d2SMaor Gottlieb 
18722d1e697eSMoses Reuben 		set_proto(headers_c, headers_v,
1873466fa6d2SMaor Gottlieb 			  ib_spec->ipv6.mask.next_hdr,
1874466fa6d2SMaor Gottlieb 			  ib_spec->ipv6.val.next_hdr);
1875466fa6d2SMaor Gottlieb 
18762d1e697eSMoses Reuben 		set_flow_label(misc_params_c, misc_params_v,
18772d1e697eSMoses Reuben 			       ntohl(ib_spec->ipv6.mask.flow_label),
18782d1e697eSMoses Reuben 			       ntohl(ib_spec->ipv6.val.flow_label),
18792d1e697eSMoses Reuben 			       ib_spec->type & IB_FLOW_SPEC_INNER);
18802d1e697eSMoses Reuben 
1881026bae0cSMaor Gottlieb 		break;
1882038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_TCP:
1883c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
1884c47ac6aeSMaor Gottlieb 					 LAST_TCP_UDP_FIELD))
18851ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
1886038d2ef8SMaor Gottlieb 
18872d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
1888038d2ef8SMaor Gottlieb 			 0xff);
18892d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
1890038d2ef8SMaor Gottlieb 			 IPPROTO_TCP);
1891038d2ef8SMaor Gottlieb 
18922d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_sport,
1893038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.src_port));
18942d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_sport,
1895038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.src_port));
1896038d2ef8SMaor Gottlieb 
18972d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_dport,
1898038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.dst_port));
18992d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_dport,
1900038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.dst_port));
1901038d2ef8SMaor Gottlieb 		break;
1902038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_UDP:
1903c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
1904c47ac6aeSMaor Gottlieb 					 LAST_TCP_UDP_FIELD))
19051ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
1906038d2ef8SMaor Gottlieb 
19072d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
1908038d2ef8SMaor Gottlieb 			 0xff);
19092d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
1910038d2ef8SMaor Gottlieb 			 IPPROTO_UDP);
1911038d2ef8SMaor Gottlieb 
19122d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_sport,
1913038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.src_port));
19142d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_sport,
1915038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.src_port));
1916038d2ef8SMaor Gottlieb 
19172d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_dport,
1918038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.dst_port));
19192d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport,
1920038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.dst_port));
1921038d2ef8SMaor Gottlieb 		break;
1922ffb30d8fSMoses Reuben 	case IB_FLOW_SPEC_VXLAN_TUNNEL:
1923ffb30d8fSMoses Reuben 		if (FIELDS_NOT_SUPPORTED(ib_spec->tunnel.mask,
1924ffb30d8fSMoses Reuben 					 LAST_TUNNEL_FIELD))
19251ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
1926ffb30d8fSMoses Reuben 
1927ffb30d8fSMoses Reuben 		MLX5_SET(fte_match_set_misc, misc_params_c, vxlan_vni,
1928ffb30d8fSMoses Reuben 			 ntohl(ib_spec->tunnel.mask.tunnel_id));
1929ffb30d8fSMoses Reuben 		MLX5_SET(fte_match_set_misc, misc_params_v, vxlan_vni,
1930ffb30d8fSMoses Reuben 			 ntohl(ib_spec->tunnel.val.tunnel_id));
1931ffb30d8fSMoses Reuben 		break;
19322ac693f9SMoses Reuben 	case IB_FLOW_SPEC_ACTION_TAG:
19332ac693f9SMoses Reuben 		if (FIELDS_NOT_SUPPORTED(ib_spec->flow_tag,
19342ac693f9SMoses Reuben 					 LAST_FLOW_TAG_FIELD))
19352ac693f9SMoses Reuben 			return -EOPNOTSUPP;
19362ac693f9SMoses Reuben 		if (ib_spec->flow_tag.tag_id >= BIT(24))
19372ac693f9SMoses Reuben 			return -EINVAL;
19382ac693f9SMoses Reuben 
19392ac693f9SMoses Reuben 		*tag_id = ib_spec->flow_tag.tag_id;
19402ac693f9SMoses Reuben 		break;
1941038d2ef8SMaor Gottlieb 	default:
1942038d2ef8SMaor Gottlieb 		return -EINVAL;
1943038d2ef8SMaor Gottlieb 	}
1944038d2ef8SMaor Gottlieb 
1945038d2ef8SMaor Gottlieb 	return 0;
1946038d2ef8SMaor Gottlieb }
1947038d2ef8SMaor Gottlieb 
1948038d2ef8SMaor Gottlieb /* If a flow could catch both multicast and unicast packets,
1949038d2ef8SMaor Gottlieb  * it won't fall into the multicast flow steering table and this rule
1950038d2ef8SMaor Gottlieb  * could steal other multicast packets.
1951038d2ef8SMaor Gottlieb  */
1952038d2ef8SMaor Gottlieb static bool flow_is_multicast_only(struct ib_flow_attr *ib_attr)
1953038d2ef8SMaor Gottlieb {
1954038d2ef8SMaor Gottlieb 	struct ib_flow_spec_eth *eth_spec;
1955038d2ef8SMaor Gottlieb 
1956038d2ef8SMaor Gottlieb 	if (ib_attr->type != IB_FLOW_ATTR_NORMAL ||
1957038d2ef8SMaor Gottlieb 	    ib_attr->size < sizeof(struct ib_flow_attr) +
1958038d2ef8SMaor Gottlieb 	    sizeof(struct ib_flow_spec_eth) ||
1959038d2ef8SMaor Gottlieb 	    ib_attr->num_of_specs < 1)
1960038d2ef8SMaor Gottlieb 		return false;
1961038d2ef8SMaor Gottlieb 
1962038d2ef8SMaor Gottlieb 	eth_spec = (struct ib_flow_spec_eth *)(ib_attr + 1);
1963038d2ef8SMaor Gottlieb 	if (eth_spec->type != IB_FLOW_SPEC_ETH ||
1964038d2ef8SMaor Gottlieb 	    eth_spec->size != sizeof(*eth_spec))
1965038d2ef8SMaor Gottlieb 		return false;
1966038d2ef8SMaor Gottlieb 
1967038d2ef8SMaor Gottlieb 	return is_multicast_ether_addr(eth_spec->mask.dst_mac) &&
1968038d2ef8SMaor Gottlieb 	       is_multicast_ether_addr(eth_spec->val.dst_mac);
1969038d2ef8SMaor Gottlieb }
1970038d2ef8SMaor Gottlieb 
1971dd063d0eSMaor Gottlieb static bool is_valid_attr(const struct ib_flow_attr *flow_attr)
1972038d2ef8SMaor Gottlieb {
1973038d2ef8SMaor Gottlieb 	union ib_flow_spec *ib_spec = (union ib_flow_spec *)(flow_attr + 1);
1974038d2ef8SMaor Gottlieb 	bool has_ipv4_spec = false;
1975038d2ef8SMaor Gottlieb 	bool eth_type_ipv4 = true;
1976038d2ef8SMaor Gottlieb 	unsigned int spec_index;
1977038d2ef8SMaor Gottlieb 
1978038d2ef8SMaor Gottlieb 	/* Validate that ethertype is correct */
1979038d2ef8SMaor Gottlieb 	for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
1980038d2ef8SMaor Gottlieb 		if (ib_spec->type == IB_FLOW_SPEC_ETH &&
1981038d2ef8SMaor Gottlieb 		    ib_spec->eth.mask.ether_type) {
1982038d2ef8SMaor Gottlieb 			if (!((ib_spec->eth.mask.ether_type == htons(0xffff)) &&
1983038d2ef8SMaor Gottlieb 			      ib_spec->eth.val.ether_type == htons(ETH_P_IP)))
1984038d2ef8SMaor Gottlieb 				eth_type_ipv4 = false;
1985038d2ef8SMaor Gottlieb 		} else if (ib_spec->type == IB_FLOW_SPEC_IPV4) {
1986038d2ef8SMaor Gottlieb 			has_ipv4_spec = true;
1987038d2ef8SMaor Gottlieb 		}
1988038d2ef8SMaor Gottlieb 		ib_spec = (void *)ib_spec + ib_spec->size;
1989038d2ef8SMaor Gottlieb 	}
1990038d2ef8SMaor Gottlieb 	return !has_ipv4_spec || eth_type_ipv4;
1991038d2ef8SMaor Gottlieb }
1992038d2ef8SMaor Gottlieb 
1993038d2ef8SMaor Gottlieb static void put_flow_table(struct mlx5_ib_dev *dev,
1994038d2ef8SMaor Gottlieb 			   struct mlx5_ib_flow_prio *prio, bool ft_added)
1995038d2ef8SMaor Gottlieb {
1996038d2ef8SMaor Gottlieb 	prio->refcount -= !!ft_added;
1997038d2ef8SMaor Gottlieb 	if (!prio->refcount) {
1998038d2ef8SMaor Gottlieb 		mlx5_destroy_flow_table(prio->flow_table);
1999038d2ef8SMaor Gottlieb 		prio->flow_table = NULL;
2000038d2ef8SMaor Gottlieb 	}
2001038d2ef8SMaor Gottlieb }
2002038d2ef8SMaor Gottlieb 
2003038d2ef8SMaor Gottlieb static int mlx5_ib_destroy_flow(struct ib_flow *flow_id)
2004038d2ef8SMaor Gottlieb {
2005038d2ef8SMaor Gottlieb 	struct mlx5_ib_dev *dev = to_mdev(flow_id->qp->device);
2006038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = container_of(flow_id,
2007038d2ef8SMaor Gottlieb 							  struct mlx5_ib_flow_handler,
2008038d2ef8SMaor Gottlieb 							  ibflow);
2009038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *iter, *tmp;
2010038d2ef8SMaor Gottlieb 
2011038d2ef8SMaor Gottlieb 	mutex_lock(&dev->flow_db.lock);
2012038d2ef8SMaor Gottlieb 
2013038d2ef8SMaor Gottlieb 	list_for_each_entry_safe(iter, tmp, &handler->list, list) {
201474491de9SMark Bloch 		mlx5_del_flow_rules(iter->rule);
2015cc0e5d42SMaor Gottlieb 		put_flow_table(dev, iter->prio, true);
2016038d2ef8SMaor Gottlieb 		list_del(&iter->list);
2017038d2ef8SMaor Gottlieb 		kfree(iter);
2018038d2ef8SMaor Gottlieb 	}
2019038d2ef8SMaor Gottlieb 
202074491de9SMark Bloch 	mlx5_del_flow_rules(handler->rule);
20215497adc6SMaor Gottlieb 	put_flow_table(dev, handler->prio, true);
2022038d2ef8SMaor Gottlieb 	mutex_unlock(&dev->flow_db.lock);
2023038d2ef8SMaor Gottlieb 
2024038d2ef8SMaor Gottlieb 	kfree(handler);
2025038d2ef8SMaor Gottlieb 
2026038d2ef8SMaor Gottlieb 	return 0;
2027038d2ef8SMaor Gottlieb }
2028038d2ef8SMaor Gottlieb 
202935d19011SMaor Gottlieb static int ib_prio_to_core_prio(unsigned int priority, bool dont_trap)
203035d19011SMaor Gottlieb {
203135d19011SMaor Gottlieb 	priority *= 2;
203235d19011SMaor Gottlieb 	if (!dont_trap)
203335d19011SMaor Gottlieb 		priority++;
203435d19011SMaor Gottlieb 	return priority;
203535d19011SMaor Gottlieb }
203635d19011SMaor Gottlieb 
2037cc0e5d42SMaor Gottlieb enum flow_table_type {
2038cc0e5d42SMaor Gottlieb 	MLX5_IB_FT_RX,
2039cc0e5d42SMaor Gottlieb 	MLX5_IB_FT_TX
2040cc0e5d42SMaor Gottlieb };
2041cc0e5d42SMaor Gottlieb 
2042038d2ef8SMaor Gottlieb #define MLX5_FS_MAX_TYPES	 10
2043038d2ef8SMaor Gottlieb #define MLX5_FS_MAX_ENTRIES	 32000UL
2044038d2ef8SMaor Gottlieb static struct mlx5_ib_flow_prio *get_flow_table(struct mlx5_ib_dev *dev,
2045cc0e5d42SMaor Gottlieb 						struct ib_flow_attr *flow_attr,
2046cc0e5d42SMaor Gottlieb 						enum flow_table_type ft_type)
2047038d2ef8SMaor Gottlieb {
204835d19011SMaor Gottlieb 	bool dont_trap = flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP;
2049038d2ef8SMaor Gottlieb 	struct mlx5_flow_namespace *ns = NULL;
2050038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_prio *prio;
2051038d2ef8SMaor Gottlieb 	struct mlx5_flow_table *ft;
2052038d2ef8SMaor Gottlieb 	int num_entries;
2053038d2ef8SMaor Gottlieb 	int num_groups;
2054038d2ef8SMaor Gottlieb 	int priority;
2055038d2ef8SMaor Gottlieb 	int err = 0;
2056038d2ef8SMaor Gottlieb 
2057038d2ef8SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
205835d19011SMaor Gottlieb 		if (flow_is_multicast_only(flow_attr) &&
205935d19011SMaor Gottlieb 		    !dont_trap)
2060038d2ef8SMaor Gottlieb 			priority = MLX5_IB_FLOW_MCAST_PRIO;
2061038d2ef8SMaor Gottlieb 		else
206235d19011SMaor Gottlieb 			priority = ib_prio_to_core_prio(flow_attr->priority,
206335d19011SMaor Gottlieb 							dont_trap);
2064038d2ef8SMaor Gottlieb 		ns = mlx5_get_flow_namespace(dev->mdev,
2065038d2ef8SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_BYPASS);
2066038d2ef8SMaor Gottlieb 		num_entries = MLX5_FS_MAX_ENTRIES;
2067038d2ef8SMaor Gottlieb 		num_groups = MLX5_FS_MAX_TYPES;
2068038d2ef8SMaor Gottlieb 		prio = &dev->flow_db.prios[priority];
2069038d2ef8SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
2070038d2ef8SMaor Gottlieb 		   flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
2071038d2ef8SMaor Gottlieb 		ns = mlx5_get_flow_namespace(dev->mdev,
2072038d2ef8SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_LEFTOVERS);
2073038d2ef8SMaor Gottlieb 		build_leftovers_ft_param(&priority,
2074038d2ef8SMaor Gottlieb 					 &num_entries,
2075038d2ef8SMaor Gottlieb 					 &num_groups);
2076038d2ef8SMaor Gottlieb 		prio = &dev->flow_db.prios[MLX5_IB_FLOW_LEFTOVERS_PRIO];
2077cc0e5d42SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
2078cc0e5d42SMaor Gottlieb 		if (!MLX5_CAP_FLOWTABLE(dev->mdev,
2079cc0e5d42SMaor Gottlieb 					allow_sniffer_and_nic_rx_shared_tir))
2080cc0e5d42SMaor Gottlieb 			return ERR_PTR(-ENOTSUPP);
2081cc0e5d42SMaor Gottlieb 
2082cc0e5d42SMaor Gottlieb 		ns = mlx5_get_flow_namespace(dev->mdev, ft_type == MLX5_IB_FT_RX ?
2083cc0e5d42SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_SNIFFER_RX :
2084cc0e5d42SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_SNIFFER_TX);
2085cc0e5d42SMaor Gottlieb 
2086cc0e5d42SMaor Gottlieb 		prio = &dev->flow_db.sniffer[ft_type];
2087cc0e5d42SMaor Gottlieb 		priority = 0;
2088cc0e5d42SMaor Gottlieb 		num_entries = 1;
2089cc0e5d42SMaor Gottlieb 		num_groups = 1;
2090038d2ef8SMaor Gottlieb 	}
2091038d2ef8SMaor Gottlieb 
2092038d2ef8SMaor Gottlieb 	if (!ns)
2093038d2ef8SMaor Gottlieb 		return ERR_PTR(-ENOTSUPP);
2094038d2ef8SMaor Gottlieb 
2095038d2ef8SMaor Gottlieb 	ft = prio->flow_table;
2096038d2ef8SMaor Gottlieb 	if (!ft) {
2097038d2ef8SMaor Gottlieb 		ft = mlx5_create_auto_grouped_flow_table(ns, priority,
2098038d2ef8SMaor Gottlieb 							 num_entries,
2099d63cd286SMaor Gottlieb 							 num_groups,
2100c9f1b073SHadar Hen Zion 							 0, 0);
2101038d2ef8SMaor Gottlieb 
2102038d2ef8SMaor Gottlieb 		if (!IS_ERR(ft)) {
2103038d2ef8SMaor Gottlieb 			prio->refcount = 0;
2104038d2ef8SMaor Gottlieb 			prio->flow_table = ft;
2105038d2ef8SMaor Gottlieb 		} else {
2106038d2ef8SMaor Gottlieb 			err = PTR_ERR(ft);
2107038d2ef8SMaor Gottlieb 		}
2108038d2ef8SMaor Gottlieb 	}
2109038d2ef8SMaor Gottlieb 
2110038d2ef8SMaor Gottlieb 	return err ? ERR_PTR(err) : prio;
2111038d2ef8SMaor Gottlieb }
2112038d2ef8SMaor Gottlieb 
2113038d2ef8SMaor Gottlieb static struct mlx5_ib_flow_handler *create_flow_rule(struct mlx5_ib_dev *dev,
2114038d2ef8SMaor Gottlieb 						     struct mlx5_ib_flow_prio *ft_prio,
2115dd063d0eSMaor Gottlieb 						     const struct ib_flow_attr *flow_attr,
2116038d2ef8SMaor Gottlieb 						     struct mlx5_flow_destination *dst)
2117038d2ef8SMaor Gottlieb {
2118038d2ef8SMaor Gottlieb 	struct mlx5_flow_table	*ft = ft_prio->flow_table;
2119038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler;
212066958ed9SHadar Hen Zion 	struct mlx5_flow_act flow_act = {0};
2121c5bb1730SMaor Gottlieb 	struct mlx5_flow_spec *spec;
2122dd063d0eSMaor Gottlieb 	const void *ib_flow = (const void *)flow_attr + sizeof(*flow_attr);
2123038d2ef8SMaor Gottlieb 	unsigned int spec_index;
21242ac693f9SMoses Reuben 	u32 flow_tag = MLX5_FS_DEFAULT_FLOW_TAG;
2125038d2ef8SMaor Gottlieb 	int err = 0;
2126038d2ef8SMaor Gottlieb 
2127038d2ef8SMaor Gottlieb 	if (!is_valid_attr(flow_attr))
2128038d2ef8SMaor Gottlieb 		return ERR_PTR(-EINVAL);
2129038d2ef8SMaor Gottlieb 
2130c5bb1730SMaor Gottlieb 	spec = mlx5_vzalloc(sizeof(*spec));
2131038d2ef8SMaor Gottlieb 	handler = kzalloc(sizeof(*handler), GFP_KERNEL);
2132c5bb1730SMaor Gottlieb 	if (!handler || !spec) {
2133038d2ef8SMaor Gottlieb 		err = -ENOMEM;
2134038d2ef8SMaor Gottlieb 		goto free;
2135038d2ef8SMaor Gottlieb 	}
2136038d2ef8SMaor Gottlieb 
2137038d2ef8SMaor Gottlieb 	INIT_LIST_HEAD(&handler->list);
2138038d2ef8SMaor Gottlieb 
2139038d2ef8SMaor Gottlieb 	for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
2140c5bb1730SMaor Gottlieb 		err = parse_flow_attr(spec->match_criteria,
21412ac693f9SMoses Reuben 				      spec->match_value, ib_flow, &flow_tag);
2142038d2ef8SMaor Gottlieb 		if (err < 0)
2143038d2ef8SMaor Gottlieb 			goto free;
2144038d2ef8SMaor Gottlieb 
2145038d2ef8SMaor Gottlieb 		ib_flow += ((union ib_flow_spec *)ib_flow)->size;
2146038d2ef8SMaor Gottlieb 	}
2147038d2ef8SMaor Gottlieb 
2148466fa6d2SMaor Gottlieb 	spec->match_criteria_enable = get_match_criteria_enable(spec->match_criteria);
214966958ed9SHadar Hen Zion 	flow_act.action = dst ? MLX5_FLOW_CONTEXT_ACTION_FWD_DEST :
215035d19011SMaor Gottlieb 		MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO;
21512ac693f9SMoses Reuben 
21522ac693f9SMoses Reuben 	if (flow_tag != MLX5_FS_DEFAULT_FLOW_TAG &&
21532ac693f9SMoses Reuben 	    (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
21542ac693f9SMoses Reuben 	     flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT)) {
21552ac693f9SMoses Reuben 		mlx5_ib_warn(dev, "Flow tag %u and attribute type %x isn't allowed in leftovers\n",
21562ac693f9SMoses Reuben 			     flow_tag, flow_attr->type);
21572ac693f9SMoses Reuben 		err = -EINVAL;
21582ac693f9SMoses Reuben 		goto free;
21592ac693f9SMoses Reuben 	}
21602ac693f9SMoses Reuben 	flow_act.flow_tag = flow_tag;
216174491de9SMark Bloch 	handler->rule = mlx5_add_flow_rules(ft, spec,
216266958ed9SHadar Hen Zion 					    &flow_act,
216374491de9SMark Bloch 					    dst, 1);
2164038d2ef8SMaor Gottlieb 
2165038d2ef8SMaor Gottlieb 	if (IS_ERR(handler->rule)) {
2166038d2ef8SMaor Gottlieb 		err = PTR_ERR(handler->rule);
2167038d2ef8SMaor Gottlieb 		goto free;
2168038d2ef8SMaor Gottlieb 	}
2169038d2ef8SMaor Gottlieb 
2170d9d4980aSMaor Gottlieb 	ft_prio->refcount++;
21715497adc6SMaor Gottlieb 	handler->prio = ft_prio;
2172038d2ef8SMaor Gottlieb 
2173038d2ef8SMaor Gottlieb 	ft_prio->flow_table = ft;
2174038d2ef8SMaor Gottlieb free:
2175038d2ef8SMaor Gottlieb 	if (err)
2176038d2ef8SMaor Gottlieb 		kfree(handler);
2177c5bb1730SMaor Gottlieb 	kvfree(spec);
2178038d2ef8SMaor Gottlieb 	return err ? ERR_PTR(err) : handler;
2179038d2ef8SMaor Gottlieb }
2180038d2ef8SMaor Gottlieb 
218135d19011SMaor Gottlieb static struct mlx5_ib_flow_handler *create_dont_trap_rule(struct mlx5_ib_dev *dev,
218235d19011SMaor Gottlieb 							  struct mlx5_ib_flow_prio *ft_prio,
218335d19011SMaor Gottlieb 							  struct ib_flow_attr *flow_attr,
218435d19011SMaor Gottlieb 							  struct mlx5_flow_destination *dst)
218535d19011SMaor Gottlieb {
218635d19011SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_dst = NULL;
218735d19011SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
218835d19011SMaor Gottlieb 
218935d19011SMaor Gottlieb 	handler = create_flow_rule(dev, ft_prio, flow_attr, NULL);
219035d19011SMaor Gottlieb 	if (!IS_ERR(handler)) {
219135d19011SMaor Gottlieb 		handler_dst = create_flow_rule(dev, ft_prio,
219235d19011SMaor Gottlieb 					       flow_attr, dst);
219335d19011SMaor Gottlieb 		if (IS_ERR(handler_dst)) {
219474491de9SMark Bloch 			mlx5_del_flow_rules(handler->rule);
2195d9d4980aSMaor Gottlieb 			ft_prio->refcount--;
219635d19011SMaor Gottlieb 			kfree(handler);
219735d19011SMaor Gottlieb 			handler = handler_dst;
219835d19011SMaor Gottlieb 		} else {
219935d19011SMaor Gottlieb 			list_add(&handler_dst->list, &handler->list);
220035d19011SMaor Gottlieb 		}
220135d19011SMaor Gottlieb 	}
220235d19011SMaor Gottlieb 
220335d19011SMaor Gottlieb 	return handler;
220435d19011SMaor Gottlieb }
2205038d2ef8SMaor Gottlieb enum {
2206038d2ef8SMaor Gottlieb 	LEFTOVERS_MC,
2207038d2ef8SMaor Gottlieb 	LEFTOVERS_UC,
2208038d2ef8SMaor Gottlieb };
2209038d2ef8SMaor Gottlieb 
2210038d2ef8SMaor Gottlieb static struct mlx5_ib_flow_handler *create_leftovers_rule(struct mlx5_ib_dev *dev,
2211038d2ef8SMaor Gottlieb 							  struct mlx5_ib_flow_prio *ft_prio,
2212038d2ef8SMaor Gottlieb 							  struct ib_flow_attr *flow_attr,
2213038d2ef8SMaor Gottlieb 							  struct mlx5_flow_destination *dst)
2214038d2ef8SMaor Gottlieb {
2215038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_ucast = NULL;
2216038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
2217038d2ef8SMaor Gottlieb 
2218038d2ef8SMaor Gottlieb 	static struct {
2219038d2ef8SMaor Gottlieb 		struct ib_flow_attr	flow_attr;
2220038d2ef8SMaor Gottlieb 		struct ib_flow_spec_eth eth_flow;
2221038d2ef8SMaor Gottlieb 	} leftovers_specs[] = {
2222038d2ef8SMaor Gottlieb 		[LEFTOVERS_MC] = {
2223038d2ef8SMaor Gottlieb 			.flow_attr = {
2224038d2ef8SMaor Gottlieb 				.num_of_specs = 1,
2225038d2ef8SMaor Gottlieb 				.size = sizeof(leftovers_specs[0])
2226038d2ef8SMaor Gottlieb 			},
2227038d2ef8SMaor Gottlieb 			.eth_flow = {
2228038d2ef8SMaor Gottlieb 				.type = IB_FLOW_SPEC_ETH,
2229038d2ef8SMaor Gottlieb 				.size = sizeof(struct ib_flow_spec_eth),
2230038d2ef8SMaor Gottlieb 				.mask = {.dst_mac = {0x1} },
2231038d2ef8SMaor Gottlieb 				.val =  {.dst_mac = {0x1} }
2232038d2ef8SMaor Gottlieb 			}
2233038d2ef8SMaor Gottlieb 		},
2234038d2ef8SMaor Gottlieb 		[LEFTOVERS_UC] = {
2235038d2ef8SMaor Gottlieb 			.flow_attr = {
2236038d2ef8SMaor Gottlieb 				.num_of_specs = 1,
2237038d2ef8SMaor Gottlieb 				.size = sizeof(leftovers_specs[0])
2238038d2ef8SMaor Gottlieb 			},
2239038d2ef8SMaor Gottlieb 			.eth_flow = {
2240038d2ef8SMaor Gottlieb 				.type = IB_FLOW_SPEC_ETH,
2241038d2ef8SMaor Gottlieb 				.size = sizeof(struct ib_flow_spec_eth),
2242038d2ef8SMaor Gottlieb 				.mask = {.dst_mac = {0x1} },
2243038d2ef8SMaor Gottlieb 				.val = {.dst_mac = {} }
2244038d2ef8SMaor Gottlieb 			}
2245038d2ef8SMaor Gottlieb 		}
2246038d2ef8SMaor Gottlieb 	};
2247038d2ef8SMaor Gottlieb 
2248038d2ef8SMaor Gottlieb 	handler = create_flow_rule(dev, ft_prio,
2249038d2ef8SMaor Gottlieb 				   &leftovers_specs[LEFTOVERS_MC].flow_attr,
2250038d2ef8SMaor Gottlieb 				   dst);
2251038d2ef8SMaor Gottlieb 	if (!IS_ERR(handler) &&
2252038d2ef8SMaor Gottlieb 	    flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT) {
2253038d2ef8SMaor Gottlieb 		handler_ucast = create_flow_rule(dev, ft_prio,
2254038d2ef8SMaor Gottlieb 						 &leftovers_specs[LEFTOVERS_UC].flow_attr,
2255038d2ef8SMaor Gottlieb 						 dst);
2256038d2ef8SMaor Gottlieb 		if (IS_ERR(handler_ucast)) {
225774491de9SMark Bloch 			mlx5_del_flow_rules(handler->rule);
2258d9d4980aSMaor Gottlieb 			ft_prio->refcount--;
2259038d2ef8SMaor Gottlieb 			kfree(handler);
2260038d2ef8SMaor Gottlieb 			handler = handler_ucast;
2261038d2ef8SMaor Gottlieb 		} else {
2262038d2ef8SMaor Gottlieb 			list_add(&handler_ucast->list, &handler->list);
2263038d2ef8SMaor Gottlieb 		}
2264038d2ef8SMaor Gottlieb 	}
2265038d2ef8SMaor Gottlieb 
2266038d2ef8SMaor Gottlieb 	return handler;
2267038d2ef8SMaor Gottlieb }
2268038d2ef8SMaor Gottlieb 
2269cc0e5d42SMaor Gottlieb static struct mlx5_ib_flow_handler *create_sniffer_rule(struct mlx5_ib_dev *dev,
2270cc0e5d42SMaor Gottlieb 							struct mlx5_ib_flow_prio *ft_rx,
2271cc0e5d42SMaor Gottlieb 							struct mlx5_ib_flow_prio *ft_tx,
2272cc0e5d42SMaor Gottlieb 							struct mlx5_flow_destination *dst)
2273cc0e5d42SMaor Gottlieb {
2274cc0e5d42SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_rx;
2275cc0e5d42SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_tx;
2276cc0e5d42SMaor Gottlieb 	int err;
2277cc0e5d42SMaor Gottlieb 	static const struct ib_flow_attr flow_attr  = {
2278cc0e5d42SMaor Gottlieb 		.num_of_specs = 0,
2279cc0e5d42SMaor Gottlieb 		.size = sizeof(flow_attr)
2280cc0e5d42SMaor Gottlieb 	};
2281cc0e5d42SMaor Gottlieb 
2282cc0e5d42SMaor Gottlieb 	handler_rx = create_flow_rule(dev, ft_rx, &flow_attr, dst);
2283cc0e5d42SMaor Gottlieb 	if (IS_ERR(handler_rx)) {
2284cc0e5d42SMaor Gottlieb 		err = PTR_ERR(handler_rx);
2285cc0e5d42SMaor Gottlieb 		goto err;
2286cc0e5d42SMaor Gottlieb 	}
2287cc0e5d42SMaor Gottlieb 
2288cc0e5d42SMaor Gottlieb 	handler_tx = create_flow_rule(dev, ft_tx, &flow_attr, dst);
2289cc0e5d42SMaor Gottlieb 	if (IS_ERR(handler_tx)) {
2290cc0e5d42SMaor Gottlieb 		err = PTR_ERR(handler_tx);
2291cc0e5d42SMaor Gottlieb 		goto err_tx;
2292cc0e5d42SMaor Gottlieb 	}
2293cc0e5d42SMaor Gottlieb 
2294cc0e5d42SMaor Gottlieb 	list_add(&handler_tx->list, &handler_rx->list);
2295cc0e5d42SMaor Gottlieb 
2296cc0e5d42SMaor Gottlieb 	return handler_rx;
2297cc0e5d42SMaor Gottlieb 
2298cc0e5d42SMaor Gottlieb err_tx:
229974491de9SMark Bloch 	mlx5_del_flow_rules(handler_rx->rule);
2300cc0e5d42SMaor Gottlieb 	ft_rx->refcount--;
2301cc0e5d42SMaor Gottlieb 	kfree(handler_rx);
2302cc0e5d42SMaor Gottlieb err:
2303cc0e5d42SMaor Gottlieb 	return ERR_PTR(err);
2304cc0e5d42SMaor Gottlieb }
2305cc0e5d42SMaor Gottlieb 
2306038d2ef8SMaor Gottlieb static struct ib_flow *mlx5_ib_create_flow(struct ib_qp *qp,
2307038d2ef8SMaor Gottlieb 					   struct ib_flow_attr *flow_attr,
2308038d2ef8SMaor Gottlieb 					   int domain)
2309038d2ef8SMaor Gottlieb {
2310038d2ef8SMaor Gottlieb 	struct mlx5_ib_dev *dev = to_mdev(qp->device);
2311d9f88e5aSYishai Hadas 	struct mlx5_ib_qp *mqp = to_mqp(qp);
2312038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
2313038d2ef8SMaor Gottlieb 	struct mlx5_flow_destination *dst = NULL;
2314cc0e5d42SMaor Gottlieb 	struct mlx5_ib_flow_prio *ft_prio_tx = NULL;
2315038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_prio *ft_prio;
2316038d2ef8SMaor Gottlieb 	int err;
2317038d2ef8SMaor Gottlieb 
2318038d2ef8SMaor Gottlieb 	if (flow_attr->priority > MLX5_IB_FLOW_LAST_PRIO)
2319038d2ef8SMaor Gottlieb 		return ERR_PTR(-ENOSPC);
2320038d2ef8SMaor Gottlieb 
2321038d2ef8SMaor Gottlieb 	if (domain != IB_FLOW_DOMAIN_USER ||
2322038d2ef8SMaor Gottlieb 	    flow_attr->port > MLX5_CAP_GEN(dev->mdev, num_ports) ||
232335d19011SMaor Gottlieb 	    (flow_attr->flags & ~IB_FLOW_ATTR_FLAGS_DONT_TRAP))
2324038d2ef8SMaor Gottlieb 		return ERR_PTR(-EINVAL);
2325038d2ef8SMaor Gottlieb 
2326038d2ef8SMaor Gottlieb 	dst = kzalloc(sizeof(*dst), GFP_KERNEL);
2327038d2ef8SMaor Gottlieb 	if (!dst)
2328038d2ef8SMaor Gottlieb 		return ERR_PTR(-ENOMEM);
2329038d2ef8SMaor Gottlieb 
2330038d2ef8SMaor Gottlieb 	mutex_lock(&dev->flow_db.lock);
2331038d2ef8SMaor Gottlieb 
2332cc0e5d42SMaor Gottlieb 	ft_prio = get_flow_table(dev, flow_attr, MLX5_IB_FT_RX);
2333038d2ef8SMaor Gottlieb 	if (IS_ERR(ft_prio)) {
2334038d2ef8SMaor Gottlieb 		err = PTR_ERR(ft_prio);
2335038d2ef8SMaor Gottlieb 		goto unlock;
2336038d2ef8SMaor Gottlieb 	}
2337cc0e5d42SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
2338cc0e5d42SMaor Gottlieb 		ft_prio_tx = get_flow_table(dev, flow_attr, MLX5_IB_FT_TX);
2339cc0e5d42SMaor Gottlieb 		if (IS_ERR(ft_prio_tx)) {
2340cc0e5d42SMaor Gottlieb 			err = PTR_ERR(ft_prio_tx);
2341cc0e5d42SMaor Gottlieb 			ft_prio_tx = NULL;
2342cc0e5d42SMaor Gottlieb 			goto destroy_ft;
2343cc0e5d42SMaor Gottlieb 		}
2344cc0e5d42SMaor Gottlieb 	}
2345038d2ef8SMaor Gottlieb 
2346038d2ef8SMaor Gottlieb 	dst->type = MLX5_FLOW_DESTINATION_TYPE_TIR;
2347d9f88e5aSYishai Hadas 	if (mqp->flags & MLX5_IB_QP_RSS)
2348d9f88e5aSYishai Hadas 		dst->tir_num = mqp->rss_qp.tirn;
2349d9f88e5aSYishai Hadas 	else
2350d9f88e5aSYishai Hadas 		dst->tir_num = mqp->raw_packet_qp.rq.tirn;
2351038d2ef8SMaor Gottlieb 
2352038d2ef8SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
235335d19011SMaor Gottlieb 		if (flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP)  {
235435d19011SMaor Gottlieb 			handler = create_dont_trap_rule(dev, ft_prio,
235535d19011SMaor Gottlieb 							flow_attr, dst);
235635d19011SMaor Gottlieb 		} else {
2357038d2ef8SMaor Gottlieb 			handler = create_flow_rule(dev, ft_prio, flow_attr,
2358038d2ef8SMaor Gottlieb 						   dst);
235935d19011SMaor Gottlieb 		}
2360038d2ef8SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
2361038d2ef8SMaor Gottlieb 		   flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
2362038d2ef8SMaor Gottlieb 		handler = create_leftovers_rule(dev, ft_prio, flow_attr,
2363038d2ef8SMaor Gottlieb 						dst);
2364cc0e5d42SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
2365cc0e5d42SMaor Gottlieb 		handler = create_sniffer_rule(dev, ft_prio, ft_prio_tx, dst);
2366038d2ef8SMaor Gottlieb 	} else {
2367038d2ef8SMaor Gottlieb 		err = -EINVAL;
2368038d2ef8SMaor Gottlieb 		goto destroy_ft;
2369038d2ef8SMaor Gottlieb 	}
2370038d2ef8SMaor Gottlieb 
2371038d2ef8SMaor Gottlieb 	if (IS_ERR(handler)) {
2372038d2ef8SMaor Gottlieb 		err = PTR_ERR(handler);
2373038d2ef8SMaor Gottlieb 		handler = NULL;
2374038d2ef8SMaor Gottlieb 		goto destroy_ft;
2375038d2ef8SMaor Gottlieb 	}
2376038d2ef8SMaor Gottlieb 
2377038d2ef8SMaor Gottlieb 	mutex_unlock(&dev->flow_db.lock);
2378038d2ef8SMaor Gottlieb 	kfree(dst);
2379038d2ef8SMaor Gottlieb 
2380038d2ef8SMaor Gottlieb 	return &handler->ibflow;
2381038d2ef8SMaor Gottlieb 
2382038d2ef8SMaor Gottlieb destroy_ft:
2383038d2ef8SMaor Gottlieb 	put_flow_table(dev, ft_prio, false);
2384cc0e5d42SMaor Gottlieb 	if (ft_prio_tx)
2385cc0e5d42SMaor Gottlieb 		put_flow_table(dev, ft_prio_tx, false);
2386038d2ef8SMaor Gottlieb unlock:
2387038d2ef8SMaor Gottlieb 	mutex_unlock(&dev->flow_db.lock);
2388038d2ef8SMaor Gottlieb 	kfree(dst);
2389038d2ef8SMaor Gottlieb 	kfree(handler);
2390038d2ef8SMaor Gottlieb 	return ERR_PTR(err);
2391038d2ef8SMaor Gottlieb }
2392038d2ef8SMaor Gottlieb 
2393e126ba97SEli Cohen static int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
2394e126ba97SEli Cohen {
2395e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
2396e126ba97SEli Cohen 	int err;
2397e126ba97SEli Cohen 
23989603b61dSJack Morgenstein 	err = mlx5_core_attach_mcg(dev->mdev, gid, ibqp->qp_num);
2399e126ba97SEli Cohen 	if (err)
2400e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n",
2401e126ba97SEli Cohen 			     ibqp->qp_num, gid->raw);
2402e126ba97SEli Cohen 
2403e126ba97SEli Cohen 	return err;
2404e126ba97SEli Cohen }
2405e126ba97SEli Cohen 
2406e126ba97SEli Cohen static int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
2407e126ba97SEli Cohen {
2408e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
2409e126ba97SEli Cohen 	int err;
2410e126ba97SEli Cohen 
24119603b61dSJack Morgenstein 	err = mlx5_core_detach_mcg(dev->mdev, gid, ibqp->qp_num);
2412e126ba97SEli Cohen 	if (err)
2413e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n",
2414e126ba97SEli Cohen 			     ibqp->qp_num, gid->raw);
2415e126ba97SEli Cohen 
2416e126ba97SEli Cohen 	return err;
2417e126ba97SEli Cohen }
2418e126ba97SEli Cohen 
2419e126ba97SEli Cohen static int init_node_data(struct mlx5_ib_dev *dev)
2420e126ba97SEli Cohen {
24211b5daf11SMajd Dibbiny 	int err;
2422e126ba97SEli Cohen 
24231b5daf11SMajd Dibbiny 	err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc);
2424e126ba97SEli Cohen 	if (err)
2425e126ba97SEli Cohen 		return err;
24261b5daf11SMajd Dibbiny 
24271b5daf11SMajd Dibbiny 	dev->mdev->rev_id = dev->mdev->pdev->revision;
24281b5daf11SMajd Dibbiny 
24291b5daf11SMajd Dibbiny 	return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid);
2430e126ba97SEli Cohen }
2431e126ba97SEli Cohen 
2432e126ba97SEli Cohen static ssize_t show_fw_pages(struct device *device, struct device_attribute *attr,
2433e126ba97SEli Cohen 			     char *buf)
2434e126ba97SEli Cohen {
2435e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
2436e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
2437e126ba97SEli Cohen 
24389603b61dSJack Morgenstein 	return sprintf(buf, "%d\n", dev->mdev->priv.fw_pages);
2439e126ba97SEli Cohen }
2440e126ba97SEli Cohen 
2441e126ba97SEli Cohen static ssize_t show_reg_pages(struct device *device,
2442e126ba97SEli Cohen 			      struct device_attribute *attr, char *buf)
2443e126ba97SEli Cohen {
2444e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
2445e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
2446e126ba97SEli Cohen 
24476aec21f6SHaggai Eran 	return sprintf(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages));
2448e126ba97SEli Cohen }
2449e126ba97SEli Cohen 
2450e126ba97SEli Cohen static ssize_t show_hca(struct device *device, struct device_attribute *attr,
2451e126ba97SEli Cohen 			char *buf)
2452e126ba97SEli Cohen {
2453e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
2454e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
24559603b61dSJack Morgenstein 	return sprintf(buf, "MT%d\n", dev->mdev->pdev->device);
2456e126ba97SEli Cohen }
2457e126ba97SEli Cohen 
2458e126ba97SEli Cohen static ssize_t show_rev(struct device *device, struct device_attribute *attr,
2459e126ba97SEli Cohen 			char *buf)
2460e126ba97SEli Cohen {
2461e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
2462e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
24639603b61dSJack Morgenstein 	return sprintf(buf, "%x\n", dev->mdev->rev_id);
2464e126ba97SEli Cohen }
2465e126ba97SEli Cohen 
2466e126ba97SEli Cohen static ssize_t show_board(struct device *device, struct device_attribute *attr,
2467e126ba97SEli Cohen 			  char *buf)
2468e126ba97SEli Cohen {
2469e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
2470e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
2471e126ba97SEli Cohen 	return sprintf(buf, "%.*s\n", MLX5_BOARD_ID_LEN,
24729603b61dSJack Morgenstein 		       dev->mdev->board_id);
2473e126ba97SEli Cohen }
2474e126ba97SEli Cohen 
2475e126ba97SEli Cohen static DEVICE_ATTR(hw_rev,   S_IRUGO, show_rev,    NULL);
2476e126ba97SEli Cohen static DEVICE_ATTR(hca_type, S_IRUGO, show_hca,    NULL);
2477e126ba97SEli Cohen static DEVICE_ATTR(board_id, S_IRUGO, show_board,  NULL);
2478e126ba97SEli Cohen static DEVICE_ATTR(fw_pages, S_IRUGO, show_fw_pages, NULL);
2479e126ba97SEli Cohen static DEVICE_ATTR(reg_pages, S_IRUGO, show_reg_pages, NULL);
2480e126ba97SEli Cohen 
2481e126ba97SEli Cohen static struct device_attribute *mlx5_class_attributes[] = {
2482e126ba97SEli Cohen 	&dev_attr_hw_rev,
2483e126ba97SEli Cohen 	&dev_attr_hca_type,
2484e126ba97SEli Cohen 	&dev_attr_board_id,
2485e126ba97SEli Cohen 	&dev_attr_fw_pages,
2486e126ba97SEli Cohen 	&dev_attr_reg_pages,
2487e126ba97SEli Cohen };
2488e126ba97SEli Cohen 
24897722f47eSHaggai Eran static void pkey_change_handler(struct work_struct *work)
24907722f47eSHaggai Eran {
24917722f47eSHaggai Eran 	struct mlx5_ib_port_resources *ports =
24927722f47eSHaggai Eran 		container_of(work, struct mlx5_ib_port_resources,
24937722f47eSHaggai Eran 			     pkey_change_work);
24947722f47eSHaggai Eran 
24957722f47eSHaggai Eran 	mutex_lock(&ports->devr->mutex);
24967722f47eSHaggai Eran 	mlx5_ib_gsi_pkey_change(ports->gsi);
24977722f47eSHaggai Eran 	mutex_unlock(&ports->devr->mutex);
24987722f47eSHaggai Eran }
24997722f47eSHaggai Eran 
250089ea94a7SMaor Gottlieb static void mlx5_ib_handle_internal_error(struct mlx5_ib_dev *ibdev)
250189ea94a7SMaor Gottlieb {
250289ea94a7SMaor Gottlieb 	struct mlx5_ib_qp *mqp;
250389ea94a7SMaor Gottlieb 	struct mlx5_ib_cq *send_mcq, *recv_mcq;
250489ea94a7SMaor Gottlieb 	struct mlx5_core_cq *mcq;
250589ea94a7SMaor Gottlieb 	struct list_head cq_armed_list;
250689ea94a7SMaor Gottlieb 	unsigned long flags_qp;
250789ea94a7SMaor Gottlieb 	unsigned long flags_cq;
250889ea94a7SMaor Gottlieb 	unsigned long flags;
250989ea94a7SMaor Gottlieb 
251089ea94a7SMaor Gottlieb 	INIT_LIST_HEAD(&cq_armed_list);
251189ea94a7SMaor Gottlieb 
251289ea94a7SMaor Gottlieb 	/* Go over qp list reside on that ibdev, sync with create/destroy qp.*/
251389ea94a7SMaor Gottlieb 	spin_lock_irqsave(&ibdev->reset_flow_resource_lock, flags);
251489ea94a7SMaor Gottlieb 	list_for_each_entry(mqp, &ibdev->qp_list, qps_list) {
251589ea94a7SMaor Gottlieb 		spin_lock_irqsave(&mqp->sq.lock, flags_qp);
251689ea94a7SMaor Gottlieb 		if (mqp->sq.tail != mqp->sq.head) {
251789ea94a7SMaor Gottlieb 			send_mcq = to_mcq(mqp->ibqp.send_cq);
251889ea94a7SMaor Gottlieb 			spin_lock_irqsave(&send_mcq->lock, flags_cq);
251989ea94a7SMaor Gottlieb 			if (send_mcq->mcq.comp &&
252089ea94a7SMaor Gottlieb 			    mqp->ibqp.send_cq->comp_handler) {
252189ea94a7SMaor Gottlieb 				if (!send_mcq->mcq.reset_notify_added) {
252289ea94a7SMaor Gottlieb 					send_mcq->mcq.reset_notify_added = 1;
252389ea94a7SMaor Gottlieb 					list_add_tail(&send_mcq->mcq.reset_notify,
252489ea94a7SMaor Gottlieb 						      &cq_armed_list);
252589ea94a7SMaor Gottlieb 				}
252689ea94a7SMaor Gottlieb 			}
252789ea94a7SMaor Gottlieb 			spin_unlock_irqrestore(&send_mcq->lock, flags_cq);
252889ea94a7SMaor Gottlieb 		}
252989ea94a7SMaor Gottlieb 		spin_unlock_irqrestore(&mqp->sq.lock, flags_qp);
253089ea94a7SMaor Gottlieb 		spin_lock_irqsave(&mqp->rq.lock, flags_qp);
253189ea94a7SMaor Gottlieb 		/* no handling is needed for SRQ */
253289ea94a7SMaor Gottlieb 		if (!mqp->ibqp.srq) {
253389ea94a7SMaor Gottlieb 			if (mqp->rq.tail != mqp->rq.head) {
253489ea94a7SMaor Gottlieb 				recv_mcq = to_mcq(mqp->ibqp.recv_cq);
253589ea94a7SMaor Gottlieb 				spin_lock_irqsave(&recv_mcq->lock, flags_cq);
253689ea94a7SMaor Gottlieb 				if (recv_mcq->mcq.comp &&
253789ea94a7SMaor Gottlieb 				    mqp->ibqp.recv_cq->comp_handler) {
253889ea94a7SMaor Gottlieb 					if (!recv_mcq->mcq.reset_notify_added) {
253989ea94a7SMaor Gottlieb 						recv_mcq->mcq.reset_notify_added = 1;
254089ea94a7SMaor Gottlieb 						list_add_tail(&recv_mcq->mcq.reset_notify,
254189ea94a7SMaor Gottlieb 							      &cq_armed_list);
254289ea94a7SMaor Gottlieb 					}
254389ea94a7SMaor Gottlieb 				}
254489ea94a7SMaor Gottlieb 				spin_unlock_irqrestore(&recv_mcq->lock,
254589ea94a7SMaor Gottlieb 						       flags_cq);
254689ea94a7SMaor Gottlieb 			}
254789ea94a7SMaor Gottlieb 		}
254889ea94a7SMaor Gottlieb 		spin_unlock_irqrestore(&mqp->rq.lock, flags_qp);
254989ea94a7SMaor Gottlieb 	}
255089ea94a7SMaor Gottlieb 	/*At that point all inflight post send were put to be executed as of we
255189ea94a7SMaor Gottlieb 	 * lock/unlock above locks Now need to arm all involved CQs.
255289ea94a7SMaor Gottlieb 	 */
255389ea94a7SMaor Gottlieb 	list_for_each_entry(mcq, &cq_armed_list, reset_notify) {
255489ea94a7SMaor Gottlieb 		mcq->comp(mcq);
255589ea94a7SMaor Gottlieb 	}
255689ea94a7SMaor Gottlieb 	spin_unlock_irqrestore(&ibdev->reset_flow_resource_lock, flags);
255789ea94a7SMaor Gottlieb }
255889ea94a7SMaor Gottlieb 
25599603b61dSJack Morgenstein static void mlx5_ib_event(struct mlx5_core_dev *dev, void *context,
25604d2f9bbbSJack Morgenstein 			  enum mlx5_dev_event event, unsigned long param)
2561e126ba97SEli Cohen {
25629603b61dSJack Morgenstein 	struct mlx5_ib_dev *ibdev = (struct mlx5_ib_dev *)context;
2563e126ba97SEli Cohen 	struct ib_event ibev;
2564dbaaff2aSEli Cohen 	bool fatal = false;
2565e126ba97SEli Cohen 	u8 port = 0;
2566e126ba97SEli Cohen 
2567e126ba97SEli Cohen 	switch (event) {
2568e126ba97SEli Cohen 	case MLX5_DEV_EVENT_SYS_ERROR:
2569e126ba97SEli Cohen 		ibev.event = IB_EVENT_DEVICE_FATAL;
257089ea94a7SMaor Gottlieb 		mlx5_ib_handle_internal_error(ibdev);
2571dbaaff2aSEli Cohen 		fatal = true;
2572e126ba97SEli Cohen 		break;
2573e126ba97SEli Cohen 
2574e126ba97SEli Cohen 	case MLX5_DEV_EVENT_PORT_UP:
2575e126ba97SEli Cohen 	case MLX5_DEV_EVENT_PORT_DOWN:
25762788cf3bSNoa Osherovich 	case MLX5_DEV_EVENT_PORT_INITIALIZED:
25774d2f9bbbSJack Morgenstein 		port = (u8)param;
25785ec8c83eSAviv Heller 
25795ec8c83eSAviv Heller 		/* In RoCE, port up/down events are handled in
25805ec8c83eSAviv Heller 		 * mlx5_netdev_event().
25815ec8c83eSAviv Heller 		 */
25825ec8c83eSAviv Heller 		if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) ==
25835ec8c83eSAviv Heller 			IB_LINK_LAYER_ETHERNET)
25845ec8c83eSAviv Heller 			return;
25855ec8c83eSAviv Heller 
25865ec8c83eSAviv Heller 		ibev.event = (event == MLX5_DEV_EVENT_PORT_UP) ?
25875ec8c83eSAviv Heller 			     IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
2588e126ba97SEli Cohen 		break;
2589e126ba97SEli Cohen 
2590e126ba97SEli Cohen 	case MLX5_DEV_EVENT_LID_CHANGE:
2591e126ba97SEli Cohen 		ibev.event = IB_EVENT_LID_CHANGE;
25924d2f9bbbSJack Morgenstein 		port = (u8)param;
2593e126ba97SEli Cohen 		break;
2594e126ba97SEli Cohen 
2595e126ba97SEli Cohen 	case MLX5_DEV_EVENT_PKEY_CHANGE:
2596e126ba97SEli Cohen 		ibev.event = IB_EVENT_PKEY_CHANGE;
25974d2f9bbbSJack Morgenstein 		port = (u8)param;
25987722f47eSHaggai Eran 
25997722f47eSHaggai Eran 		schedule_work(&ibdev->devr.ports[port - 1].pkey_change_work);
2600e126ba97SEli Cohen 		break;
2601e126ba97SEli Cohen 
2602e126ba97SEli Cohen 	case MLX5_DEV_EVENT_GUID_CHANGE:
2603e126ba97SEli Cohen 		ibev.event = IB_EVENT_GID_CHANGE;
26044d2f9bbbSJack Morgenstein 		port = (u8)param;
2605e126ba97SEli Cohen 		break;
2606e126ba97SEli Cohen 
2607e126ba97SEli Cohen 	case MLX5_DEV_EVENT_CLIENT_REREG:
2608e126ba97SEli Cohen 		ibev.event = IB_EVENT_CLIENT_REREGISTER;
26094d2f9bbbSJack Morgenstein 		port = (u8)param;
2610e126ba97SEli Cohen 		break;
2611bdc37924SSaeed Mahameed 	default:
2612bdc37924SSaeed Mahameed 		return;
2613e126ba97SEli Cohen 	}
2614e126ba97SEli Cohen 
2615e126ba97SEli Cohen 	ibev.device	      = &ibdev->ib_dev;
2616e126ba97SEli Cohen 	ibev.element.port_num = port;
2617e126ba97SEli Cohen 
2618a0c84c32SEli Cohen 	if (port < 1 || port > ibdev->num_ports) {
2619a0c84c32SEli Cohen 		mlx5_ib_warn(ibdev, "warning: event on port %d\n", port);
2620a0c84c32SEli Cohen 		return;
2621a0c84c32SEli Cohen 	}
2622a0c84c32SEli Cohen 
2623e126ba97SEli Cohen 	if (ibdev->ib_active)
2624e126ba97SEli Cohen 		ib_dispatch_event(&ibev);
2625dbaaff2aSEli Cohen 
2626dbaaff2aSEli Cohen 	if (fatal)
2627dbaaff2aSEli Cohen 		ibdev->ib_active = false;
2628e126ba97SEli Cohen }
2629e126ba97SEli Cohen 
2630c43f1112SMaor Gottlieb static int set_has_smi_cap(struct mlx5_ib_dev *dev)
2631c43f1112SMaor Gottlieb {
2632c43f1112SMaor Gottlieb 	struct mlx5_hca_vport_context vport_ctx;
2633c43f1112SMaor Gottlieb 	int err;
2634c43f1112SMaor Gottlieb 	int port;
2635c43f1112SMaor Gottlieb 
2636c43f1112SMaor Gottlieb 	for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++) {
2637c43f1112SMaor Gottlieb 		dev->mdev->port_caps[port - 1].has_smi = false;
2638c43f1112SMaor Gottlieb 		if (MLX5_CAP_GEN(dev->mdev, port_type) ==
2639c43f1112SMaor Gottlieb 		    MLX5_CAP_PORT_TYPE_IB) {
2640c43f1112SMaor Gottlieb 			if (MLX5_CAP_GEN(dev->mdev, ib_virt)) {
2641c43f1112SMaor Gottlieb 				err = mlx5_query_hca_vport_context(dev->mdev, 0,
2642c43f1112SMaor Gottlieb 								   port, 0,
2643c43f1112SMaor Gottlieb 								   &vport_ctx);
2644c43f1112SMaor Gottlieb 				if (err) {
2645c43f1112SMaor Gottlieb 					mlx5_ib_err(dev, "query_hca_vport_context for port=%d failed %d\n",
2646c43f1112SMaor Gottlieb 						    port, err);
2647c43f1112SMaor Gottlieb 					return err;
2648c43f1112SMaor Gottlieb 				}
2649c43f1112SMaor Gottlieb 				dev->mdev->port_caps[port - 1].has_smi =
2650c43f1112SMaor Gottlieb 					vport_ctx.has_smi;
2651c43f1112SMaor Gottlieb 			} else {
2652c43f1112SMaor Gottlieb 				dev->mdev->port_caps[port - 1].has_smi = true;
2653c43f1112SMaor Gottlieb 			}
2654c43f1112SMaor Gottlieb 		}
2655c43f1112SMaor Gottlieb 	}
2656c43f1112SMaor Gottlieb 	return 0;
2657c43f1112SMaor Gottlieb }
2658c43f1112SMaor Gottlieb 
2659e126ba97SEli Cohen static void get_ext_port_caps(struct mlx5_ib_dev *dev)
2660e126ba97SEli Cohen {
2661e126ba97SEli Cohen 	int port;
2662e126ba97SEli Cohen 
2663938fe83cSSaeed Mahameed 	for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++)
2664e126ba97SEli Cohen 		mlx5_query_ext_port_caps(dev, port);
2665e126ba97SEli Cohen }
2666e126ba97SEli Cohen 
2667e126ba97SEli Cohen static int get_port_caps(struct mlx5_ib_dev *dev)
2668e126ba97SEli Cohen {
2669e126ba97SEli Cohen 	struct ib_device_attr *dprops = NULL;
2670e126ba97SEli Cohen 	struct ib_port_attr *pprops = NULL;
2671f614fc15SDan Carpenter 	int err = -ENOMEM;
2672e126ba97SEli Cohen 	int port;
26732528e33eSMatan Barak 	struct ib_udata uhw = {.inlen = 0, .outlen = 0};
2674e126ba97SEli Cohen 
2675e126ba97SEli Cohen 	pprops = kmalloc(sizeof(*pprops), GFP_KERNEL);
2676e126ba97SEli Cohen 	if (!pprops)
2677e126ba97SEli Cohen 		goto out;
2678e126ba97SEli Cohen 
2679e126ba97SEli Cohen 	dprops = kmalloc(sizeof(*dprops), GFP_KERNEL);
2680e126ba97SEli Cohen 	if (!dprops)
2681e126ba97SEli Cohen 		goto out;
2682e126ba97SEli Cohen 
2683c43f1112SMaor Gottlieb 	err = set_has_smi_cap(dev);
2684c43f1112SMaor Gottlieb 	if (err)
2685c43f1112SMaor Gottlieb 		goto out;
2686c43f1112SMaor Gottlieb 
26872528e33eSMatan Barak 	err = mlx5_ib_query_device(&dev->ib_dev, dprops, &uhw);
2688e126ba97SEli Cohen 	if (err) {
2689e126ba97SEli Cohen 		mlx5_ib_warn(dev, "query_device failed %d\n", err);
2690e126ba97SEli Cohen 		goto out;
2691e126ba97SEli Cohen 	}
2692e126ba97SEli Cohen 
2693938fe83cSSaeed Mahameed 	for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++) {
2694c4550c63SOr Gerlitz 		memset(pprops, 0, sizeof(*pprops));
2695e126ba97SEli Cohen 		err = mlx5_ib_query_port(&dev->ib_dev, port, pprops);
2696e126ba97SEli Cohen 		if (err) {
2697938fe83cSSaeed Mahameed 			mlx5_ib_warn(dev, "query_port %d failed %d\n",
2698938fe83cSSaeed Mahameed 				     port, err);
2699e126ba97SEli Cohen 			break;
2700e126ba97SEli Cohen 		}
2701938fe83cSSaeed Mahameed 		dev->mdev->port_caps[port - 1].pkey_table_len =
2702938fe83cSSaeed Mahameed 						dprops->max_pkeys;
2703938fe83cSSaeed Mahameed 		dev->mdev->port_caps[port - 1].gid_table_len =
2704938fe83cSSaeed Mahameed 						pprops->gid_tbl_len;
2705e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "pkey_table_len %d, gid_table_len %d\n",
2706e126ba97SEli Cohen 			    dprops->max_pkeys, pprops->gid_tbl_len);
2707e126ba97SEli Cohen 	}
2708e126ba97SEli Cohen 
2709e126ba97SEli Cohen out:
2710e126ba97SEli Cohen 	kfree(pprops);
2711e126ba97SEli Cohen 	kfree(dprops);
2712e126ba97SEli Cohen 
2713e126ba97SEli Cohen 	return err;
2714e126ba97SEli Cohen }
2715e126ba97SEli Cohen 
2716e126ba97SEli Cohen static void destroy_umrc_res(struct mlx5_ib_dev *dev)
2717e126ba97SEli Cohen {
2718e126ba97SEli Cohen 	int err;
2719e126ba97SEli Cohen 
2720e126ba97SEli Cohen 	err = mlx5_mr_cache_cleanup(dev);
2721e126ba97SEli Cohen 	if (err)
2722e126ba97SEli Cohen 		mlx5_ib_warn(dev, "mr cache cleanup failed\n");
2723e126ba97SEli Cohen 
2724e126ba97SEli Cohen 	mlx5_ib_destroy_qp(dev->umrc.qp);
2725add08d76SChristoph Hellwig 	ib_free_cq(dev->umrc.cq);
2726e126ba97SEli Cohen 	ib_dealloc_pd(dev->umrc.pd);
2727e126ba97SEli Cohen }
2728e126ba97SEli Cohen 
2729e126ba97SEli Cohen enum {
2730e126ba97SEli Cohen 	MAX_UMR_WR = 128,
2731e126ba97SEli Cohen };
2732e126ba97SEli Cohen 
2733e126ba97SEli Cohen static int create_umr_res(struct mlx5_ib_dev *dev)
2734e126ba97SEli Cohen {
2735e126ba97SEli Cohen 	struct ib_qp_init_attr *init_attr = NULL;
2736e126ba97SEli Cohen 	struct ib_qp_attr *attr = NULL;
2737e126ba97SEli Cohen 	struct ib_pd *pd;
2738e126ba97SEli Cohen 	struct ib_cq *cq;
2739e126ba97SEli Cohen 	struct ib_qp *qp;
2740e126ba97SEli Cohen 	int ret;
2741e126ba97SEli Cohen 
2742e126ba97SEli Cohen 	attr = kzalloc(sizeof(*attr), GFP_KERNEL);
2743e126ba97SEli Cohen 	init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL);
2744e126ba97SEli Cohen 	if (!attr || !init_attr) {
2745e126ba97SEli Cohen 		ret = -ENOMEM;
2746e126ba97SEli Cohen 		goto error_0;
2747e126ba97SEli Cohen 	}
2748e126ba97SEli Cohen 
2749ed082d36SChristoph Hellwig 	pd = ib_alloc_pd(&dev->ib_dev, 0);
2750e126ba97SEli Cohen 	if (IS_ERR(pd)) {
2751e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create PD for sync UMR QP\n");
2752e126ba97SEli Cohen 		ret = PTR_ERR(pd);
2753e126ba97SEli Cohen 		goto error_0;
2754e126ba97SEli Cohen 	}
2755e126ba97SEli Cohen 
2756add08d76SChristoph Hellwig 	cq = ib_alloc_cq(&dev->ib_dev, NULL, 128, 0, IB_POLL_SOFTIRQ);
2757e126ba97SEli Cohen 	if (IS_ERR(cq)) {
2758e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create CQ for sync UMR QP\n");
2759e126ba97SEli Cohen 		ret = PTR_ERR(cq);
2760e126ba97SEli Cohen 		goto error_2;
2761e126ba97SEli Cohen 	}
2762e126ba97SEli Cohen 
2763e126ba97SEli Cohen 	init_attr->send_cq = cq;
2764e126ba97SEli Cohen 	init_attr->recv_cq = cq;
2765e126ba97SEli Cohen 	init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
2766e126ba97SEli Cohen 	init_attr->cap.max_send_wr = MAX_UMR_WR;
2767e126ba97SEli Cohen 	init_attr->cap.max_send_sge = 1;
2768e126ba97SEli Cohen 	init_attr->qp_type = MLX5_IB_QPT_REG_UMR;
2769e126ba97SEli Cohen 	init_attr->port_num = 1;
2770e126ba97SEli Cohen 	qp = mlx5_ib_create_qp(pd, init_attr, NULL);
2771e126ba97SEli Cohen 	if (IS_ERR(qp)) {
2772e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create sync UMR QP\n");
2773e126ba97SEli Cohen 		ret = PTR_ERR(qp);
2774e126ba97SEli Cohen 		goto error_3;
2775e126ba97SEli Cohen 	}
2776e126ba97SEli Cohen 	qp->device     = &dev->ib_dev;
2777e126ba97SEli Cohen 	qp->real_qp    = qp;
2778e126ba97SEli Cohen 	qp->uobject    = NULL;
2779e126ba97SEli Cohen 	qp->qp_type    = MLX5_IB_QPT_REG_UMR;
2780e126ba97SEli Cohen 
2781e126ba97SEli Cohen 	attr->qp_state = IB_QPS_INIT;
2782e126ba97SEli Cohen 	attr->port_num = 1;
2783e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_PKEY_INDEX |
2784e126ba97SEli Cohen 				IB_QP_PORT, NULL);
2785e126ba97SEli Cohen 	if (ret) {
2786e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify UMR QP\n");
2787e126ba97SEli Cohen 		goto error_4;
2788e126ba97SEli Cohen 	}
2789e126ba97SEli Cohen 
2790e126ba97SEli Cohen 	memset(attr, 0, sizeof(*attr));
2791e126ba97SEli Cohen 	attr->qp_state = IB_QPS_RTR;
2792e126ba97SEli Cohen 	attr->path_mtu = IB_MTU_256;
2793e126ba97SEli Cohen 
2794e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
2795e126ba97SEli Cohen 	if (ret) {
2796e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify umr QP to rtr\n");
2797e126ba97SEli Cohen 		goto error_4;
2798e126ba97SEli Cohen 	}
2799e126ba97SEli Cohen 
2800e126ba97SEli Cohen 	memset(attr, 0, sizeof(*attr));
2801e126ba97SEli Cohen 	attr->qp_state = IB_QPS_RTS;
2802e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
2803e126ba97SEli Cohen 	if (ret) {
2804e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify umr QP to rts\n");
2805e126ba97SEli Cohen 		goto error_4;
2806e126ba97SEli Cohen 	}
2807e126ba97SEli Cohen 
2808e126ba97SEli Cohen 	dev->umrc.qp = qp;
2809e126ba97SEli Cohen 	dev->umrc.cq = cq;
2810e126ba97SEli Cohen 	dev->umrc.pd = pd;
2811e126ba97SEli Cohen 
2812e126ba97SEli Cohen 	sema_init(&dev->umrc.sem, MAX_UMR_WR);
2813e126ba97SEli Cohen 	ret = mlx5_mr_cache_init(dev);
2814e126ba97SEli Cohen 	if (ret) {
2815e126ba97SEli Cohen 		mlx5_ib_warn(dev, "mr cache init failed %d\n", ret);
2816e126ba97SEli Cohen 		goto error_4;
2817e126ba97SEli Cohen 	}
2818e126ba97SEli Cohen 
2819e126ba97SEli Cohen 	kfree(attr);
2820e126ba97SEli Cohen 	kfree(init_attr);
2821e126ba97SEli Cohen 
2822e126ba97SEli Cohen 	return 0;
2823e126ba97SEli Cohen 
2824e126ba97SEli Cohen error_4:
2825e126ba97SEli Cohen 	mlx5_ib_destroy_qp(qp);
2826e126ba97SEli Cohen 
2827e126ba97SEli Cohen error_3:
2828add08d76SChristoph Hellwig 	ib_free_cq(cq);
2829e126ba97SEli Cohen 
2830e126ba97SEli Cohen error_2:
2831e126ba97SEli Cohen 	ib_dealloc_pd(pd);
2832e126ba97SEli Cohen 
2833e126ba97SEli Cohen error_0:
2834e126ba97SEli Cohen 	kfree(attr);
2835e126ba97SEli Cohen 	kfree(init_attr);
2836e126ba97SEli Cohen 	return ret;
2837e126ba97SEli Cohen }
2838e126ba97SEli Cohen 
2839e126ba97SEli Cohen static int create_dev_resources(struct mlx5_ib_resources *devr)
2840e126ba97SEli Cohen {
2841e126ba97SEli Cohen 	struct ib_srq_init_attr attr;
2842e126ba97SEli Cohen 	struct mlx5_ib_dev *dev;
2843bcf4c1eaSMatan Barak 	struct ib_cq_init_attr cq_attr = {.cqe = 1};
28447722f47eSHaggai Eran 	int port;
2845e126ba97SEli Cohen 	int ret = 0;
2846e126ba97SEli Cohen 
2847e126ba97SEli Cohen 	dev = container_of(devr, struct mlx5_ib_dev, devr);
2848e126ba97SEli Cohen 
2849d16e91daSHaggai Eran 	mutex_init(&devr->mutex);
2850d16e91daSHaggai Eran 
2851e126ba97SEli Cohen 	devr->p0 = mlx5_ib_alloc_pd(&dev->ib_dev, NULL, NULL);
2852e126ba97SEli Cohen 	if (IS_ERR(devr->p0)) {
2853e126ba97SEli Cohen 		ret = PTR_ERR(devr->p0);
2854e126ba97SEli Cohen 		goto error0;
2855e126ba97SEli Cohen 	}
2856e126ba97SEli Cohen 	devr->p0->device  = &dev->ib_dev;
2857e126ba97SEli Cohen 	devr->p0->uobject = NULL;
2858e126ba97SEli Cohen 	atomic_set(&devr->p0->usecnt, 0);
2859e126ba97SEli Cohen 
2860bcf4c1eaSMatan Barak 	devr->c0 = mlx5_ib_create_cq(&dev->ib_dev, &cq_attr, NULL, NULL);
2861e126ba97SEli Cohen 	if (IS_ERR(devr->c0)) {
2862e126ba97SEli Cohen 		ret = PTR_ERR(devr->c0);
2863e126ba97SEli Cohen 		goto error1;
2864e126ba97SEli Cohen 	}
2865e126ba97SEli Cohen 	devr->c0->device        = &dev->ib_dev;
2866e126ba97SEli Cohen 	devr->c0->uobject       = NULL;
2867e126ba97SEli Cohen 	devr->c0->comp_handler  = NULL;
2868e126ba97SEli Cohen 	devr->c0->event_handler = NULL;
2869e126ba97SEli Cohen 	devr->c0->cq_context    = NULL;
2870e126ba97SEli Cohen 	atomic_set(&devr->c0->usecnt, 0);
2871e126ba97SEli Cohen 
2872e126ba97SEli Cohen 	devr->x0 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
2873e126ba97SEli Cohen 	if (IS_ERR(devr->x0)) {
2874e126ba97SEli Cohen 		ret = PTR_ERR(devr->x0);
2875e126ba97SEli Cohen 		goto error2;
2876e126ba97SEli Cohen 	}
2877e126ba97SEli Cohen 	devr->x0->device = &dev->ib_dev;
2878e126ba97SEli Cohen 	devr->x0->inode = NULL;
2879e126ba97SEli Cohen 	atomic_set(&devr->x0->usecnt, 0);
2880e126ba97SEli Cohen 	mutex_init(&devr->x0->tgt_qp_mutex);
2881e126ba97SEli Cohen 	INIT_LIST_HEAD(&devr->x0->tgt_qp_list);
2882e126ba97SEli Cohen 
2883e126ba97SEli Cohen 	devr->x1 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
2884e126ba97SEli Cohen 	if (IS_ERR(devr->x1)) {
2885e126ba97SEli Cohen 		ret = PTR_ERR(devr->x1);
2886e126ba97SEli Cohen 		goto error3;
2887e126ba97SEli Cohen 	}
2888e126ba97SEli Cohen 	devr->x1->device = &dev->ib_dev;
2889e126ba97SEli Cohen 	devr->x1->inode = NULL;
2890e126ba97SEli Cohen 	atomic_set(&devr->x1->usecnt, 0);
2891e126ba97SEli Cohen 	mutex_init(&devr->x1->tgt_qp_mutex);
2892e126ba97SEli Cohen 	INIT_LIST_HEAD(&devr->x1->tgt_qp_list);
2893e126ba97SEli Cohen 
2894e126ba97SEli Cohen 	memset(&attr, 0, sizeof(attr));
2895e126ba97SEli Cohen 	attr.attr.max_sge = 1;
2896e126ba97SEli Cohen 	attr.attr.max_wr = 1;
2897e126ba97SEli Cohen 	attr.srq_type = IB_SRQT_XRC;
2898e126ba97SEli Cohen 	attr.ext.xrc.cq = devr->c0;
2899e126ba97SEli Cohen 	attr.ext.xrc.xrcd = devr->x0;
2900e126ba97SEli Cohen 
2901e126ba97SEli Cohen 	devr->s0 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
2902e126ba97SEli Cohen 	if (IS_ERR(devr->s0)) {
2903e126ba97SEli Cohen 		ret = PTR_ERR(devr->s0);
2904e126ba97SEli Cohen 		goto error4;
2905e126ba97SEli Cohen 	}
2906e126ba97SEli Cohen 	devr->s0->device	= &dev->ib_dev;
2907e126ba97SEli Cohen 	devr->s0->pd		= devr->p0;
2908e126ba97SEli Cohen 	devr->s0->uobject       = NULL;
2909e126ba97SEli Cohen 	devr->s0->event_handler = NULL;
2910e126ba97SEli Cohen 	devr->s0->srq_context   = NULL;
2911e126ba97SEli Cohen 	devr->s0->srq_type      = IB_SRQT_XRC;
2912e126ba97SEli Cohen 	devr->s0->ext.xrc.xrcd	= devr->x0;
2913e126ba97SEli Cohen 	devr->s0->ext.xrc.cq	= devr->c0;
2914e126ba97SEli Cohen 	atomic_inc(&devr->s0->ext.xrc.xrcd->usecnt);
2915e126ba97SEli Cohen 	atomic_inc(&devr->s0->ext.xrc.cq->usecnt);
2916e126ba97SEli Cohen 	atomic_inc(&devr->p0->usecnt);
2917e126ba97SEli Cohen 	atomic_set(&devr->s0->usecnt, 0);
2918e126ba97SEli Cohen 
29194aa17b28SHaggai Abramonvsky 	memset(&attr, 0, sizeof(attr));
29204aa17b28SHaggai Abramonvsky 	attr.attr.max_sge = 1;
29214aa17b28SHaggai Abramonvsky 	attr.attr.max_wr = 1;
29224aa17b28SHaggai Abramonvsky 	attr.srq_type = IB_SRQT_BASIC;
29234aa17b28SHaggai Abramonvsky 	devr->s1 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
29244aa17b28SHaggai Abramonvsky 	if (IS_ERR(devr->s1)) {
29254aa17b28SHaggai Abramonvsky 		ret = PTR_ERR(devr->s1);
29264aa17b28SHaggai Abramonvsky 		goto error5;
29274aa17b28SHaggai Abramonvsky 	}
29284aa17b28SHaggai Abramonvsky 	devr->s1->device	= &dev->ib_dev;
29294aa17b28SHaggai Abramonvsky 	devr->s1->pd		= devr->p0;
29304aa17b28SHaggai Abramonvsky 	devr->s1->uobject       = NULL;
29314aa17b28SHaggai Abramonvsky 	devr->s1->event_handler = NULL;
29324aa17b28SHaggai Abramonvsky 	devr->s1->srq_context   = NULL;
29334aa17b28SHaggai Abramonvsky 	devr->s1->srq_type      = IB_SRQT_BASIC;
29344aa17b28SHaggai Abramonvsky 	devr->s1->ext.xrc.cq	= devr->c0;
29354aa17b28SHaggai Abramonvsky 	atomic_inc(&devr->p0->usecnt);
29364aa17b28SHaggai Abramonvsky 	atomic_set(&devr->s0->usecnt, 0);
29374aa17b28SHaggai Abramonvsky 
29387722f47eSHaggai Eran 	for (port = 0; port < ARRAY_SIZE(devr->ports); ++port) {
29397722f47eSHaggai Eran 		INIT_WORK(&devr->ports[port].pkey_change_work,
29407722f47eSHaggai Eran 			  pkey_change_handler);
29417722f47eSHaggai Eran 		devr->ports[port].devr = devr;
29427722f47eSHaggai Eran 	}
29437722f47eSHaggai Eran 
2944e126ba97SEli Cohen 	return 0;
2945e126ba97SEli Cohen 
29464aa17b28SHaggai Abramonvsky error5:
29474aa17b28SHaggai Abramonvsky 	mlx5_ib_destroy_srq(devr->s0);
2948e126ba97SEli Cohen error4:
2949e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x1);
2950e126ba97SEli Cohen error3:
2951e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x0);
2952e126ba97SEli Cohen error2:
2953e126ba97SEli Cohen 	mlx5_ib_destroy_cq(devr->c0);
2954e126ba97SEli Cohen error1:
2955e126ba97SEli Cohen 	mlx5_ib_dealloc_pd(devr->p0);
2956e126ba97SEli Cohen error0:
2957e126ba97SEli Cohen 	return ret;
2958e126ba97SEli Cohen }
2959e126ba97SEli Cohen 
2960e126ba97SEli Cohen static void destroy_dev_resources(struct mlx5_ib_resources *devr)
2961e126ba97SEli Cohen {
29627722f47eSHaggai Eran 	struct mlx5_ib_dev *dev =
29637722f47eSHaggai Eran 		container_of(devr, struct mlx5_ib_dev, devr);
29647722f47eSHaggai Eran 	int port;
29657722f47eSHaggai Eran 
29664aa17b28SHaggai Abramonvsky 	mlx5_ib_destroy_srq(devr->s1);
2967e126ba97SEli Cohen 	mlx5_ib_destroy_srq(devr->s0);
2968e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x0);
2969e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x1);
2970e126ba97SEli Cohen 	mlx5_ib_destroy_cq(devr->c0);
2971e126ba97SEli Cohen 	mlx5_ib_dealloc_pd(devr->p0);
29727722f47eSHaggai Eran 
29737722f47eSHaggai Eran 	/* Make sure no change P_Key work items are still executing */
29747722f47eSHaggai Eran 	for (port = 0; port < dev->num_ports; ++port)
29757722f47eSHaggai Eran 		cancel_work_sync(&devr->ports[port].pkey_change_work);
2976e126ba97SEli Cohen }
2977e126ba97SEli Cohen 
2978e53505a8SAchiad Shochat static u32 get_core_cap_flags(struct ib_device *ibdev)
2979e53505a8SAchiad Shochat {
2980e53505a8SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
2981e53505a8SAchiad Shochat 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, 1);
2982e53505a8SAchiad Shochat 	u8 l3_type_cap = MLX5_CAP_ROCE(dev->mdev, l3_type);
2983e53505a8SAchiad Shochat 	u8 roce_version_cap = MLX5_CAP_ROCE(dev->mdev, roce_version);
2984e53505a8SAchiad Shochat 	u32 ret = 0;
2985e53505a8SAchiad Shochat 
2986e53505a8SAchiad Shochat 	if (ll == IB_LINK_LAYER_INFINIBAND)
2987e53505a8SAchiad Shochat 		return RDMA_CORE_PORT_IBA_IB;
2988e53505a8SAchiad Shochat 
298972cd5717SOr Gerlitz 	ret = RDMA_CORE_PORT_RAW_PACKET;
299072cd5717SOr Gerlitz 
2991e53505a8SAchiad Shochat 	if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV4_CAP))
299272cd5717SOr Gerlitz 		return ret;
2993e53505a8SAchiad Shochat 
2994e53505a8SAchiad Shochat 	if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV6_CAP))
299572cd5717SOr Gerlitz 		return ret;
2996e53505a8SAchiad Shochat 
2997e53505a8SAchiad Shochat 	if (roce_version_cap & MLX5_ROCE_VERSION_1_CAP)
2998e53505a8SAchiad Shochat 		ret |= RDMA_CORE_PORT_IBA_ROCE;
2999e53505a8SAchiad Shochat 
3000e53505a8SAchiad Shochat 	if (roce_version_cap & MLX5_ROCE_VERSION_2_CAP)
3001e53505a8SAchiad Shochat 		ret |= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
3002e53505a8SAchiad Shochat 
3003e53505a8SAchiad Shochat 	return ret;
3004e53505a8SAchiad Shochat }
3005e53505a8SAchiad Shochat 
30067738613eSIra Weiny static int mlx5_port_immutable(struct ib_device *ibdev, u8 port_num,
30077738613eSIra Weiny 			       struct ib_port_immutable *immutable)
30087738613eSIra Weiny {
30097738613eSIra Weiny 	struct ib_port_attr attr;
3010ca5b91d6SOr Gerlitz 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
3011ca5b91d6SOr Gerlitz 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, port_num);
30127738613eSIra Weiny 	int err;
30137738613eSIra Weiny 
3014c4550c63SOr Gerlitz 	immutable->core_cap_flags = get_core_cap_flags(ibdev);
3015c4550c63SOr Gerlitz 
3016c4550c63SOr Gerlitz 	err = ib_query_port(ibdev, port_num, &attr);
30177738613eSIra Weiny 	if (err)
30187738613eSIra Weiny 		return err;
30197738613eSIra Weiny 
30207738613eSIra Weiny 	immutable->pkey_tbl_len = attr.pkey_tbl_len;
30217738613eSIra Weiny 	immutable->gid_tbl_len = attr.gid_tbl_len;
3022e53505a8SAchiad Shochat 	immutable->core_cap_flags = get_core_cap_flags(ibdev);
3023ca5b91d6SOr Gerlitz 	if ((ll == IB_LINK_LAYER_INFINIBAND) || MLX5_CAP_GEN(dev->mdev, roce))
3024337877a4SIra Weiny 		immutable->max_mad_size = IB_MGMT_MAD_SIZE;
30257738613eSIra Weiny 
30267738613eSIra Weiny 	return 0;
30277738613eSIra Weiny }
30287738613eSIra Weiny 
3029c7342823SIra Weiny static void get_dev_fw_str(struct ib_device *ibdev, char *str,
3030c7342823SIra Weiny 			   size_t str_len)
3031c7342823SIra Weiny {
3032c7342823SIra Weiny 	struct mlx5_ib_dev *dev =
3033c7342823SIra Weiny 		container_of(ibdev, struct mlx5_ib_dev, ib_dev);
3034c7342823SIra Weiny 	snprintf(str, str_len, "%d.%d.%04d", fw_rev_maj(dev->mdev),
3035c7342823SIra Weiny 		       fw_rev_min(dev->mdev), fw_rev_sub(dev->mdev));
3036c7342823SIra Weiny }
3037c7342823SIra Weiny 
303845f95acdSOr Gerlitz static int mlx5_eth_lag_init(struct mlx5_ib_dev *dev)
30399ef9c640SAviv Heller {
30409ef9c640SAviv Heller 	struct mlx5_core_dev *mdev = dev->mdev;
30419ef9c640SAviv Heller 	struct mlx5_flow_namespace *ns = mlx5_get_flow_namespace(mdev,
30429ef9c640SAviv Heller 								 MLX5_FLOW_NAMESPACE_LAG);
30439ef9c640SAviv Heller 	struct mlx5_flow_table *ft;
30449ef9c640SAviv Heller 	int err;
30459ef9c640SAviv Heller 
30469ef9c640SAviv Heller 	if (!ns || !mlx5_lag_is_active(mdev))
30479ef9c640SAviv Heller 		return 0;
30489ef9c640SAviv Heller 
30499ef9c640SAviv Heller 	err = mlx5_cmd_create_vport_lag(mdev);
30509ef9c640SAviv Heller 	if (err)
30519ef9c640SAviv Heller 		return err;
30529ef9c640SAviv Heller 
30539ef9c640SAviv Heller 	ft = mlx5_create_lag_demux_flow_table(ns, 0, 0);
30549ef9c640SAviv Heller 	if (IS_ERR(ft)) {
30559ef9c640SAviv Heller 		err = PTR_ERR(ft);
30569ef9c640SAviv Heller 		goto err_destroy_vport_lag;
30579ef9c640SAviv Heller 	}
30589ef9c640SAviv Heller 
30599ef9c640SAviv Heller 	dev->flow_db.lag_demux_ft = ft;
30609ef9c640SAviv Heller 	return 0;
30619ef9c640SAviv Heller 
30629ef9c640SAviv Heller err_destroy_vport_lag:
30639ef9c640SAviv Heller 	mlx5_cmd_destroy_vport_lag(mdev);
30649ef9c640SAviv Heller 	return err;
30659ef9c640SAviv Heller }
30669ef9c640SAviv Heller 
306745f95acdSOr Gerlitz static void mlx5_eth_lag_cleanup(struct mlx5_ib_dev *dev)
30689ef9c640SAviv Heller {
30699ef9c640SAviv Heller 	struct mlx5_core_dev *mdev = dev->mdev;
30709ef9c640SAviv Heller 
30719ef9c640SAviv Heller 	if (dev->flow_db.lag_demux_ft) {
30729ef9c640SAviv Heller 		mlx5_destroy_flow_table(dev->flow_db.lag_demux_ft);
30739ef9c640SAviv Heller 		dev->flow_db.lag_demux_ft = NULL;
30749ef9c640SAviv Heller 
30759ef9c640SAviv Heller 		mlx5_cmd_destroy_vport_lag(mdev);
30769ef9c640SAviv Heller 	}
30779ef9c640SAviv Heller }
30789ef9c640SAviv Heller 
3079d012f5d6SOr Gerlitz static int mlx5_add_netdev_notifier(struct mlx5_ib_dev *dev)
3080fc24fc5eSAchiad Shochat {
3081e53505a8SAchiad Shochat 	int err;
3082e53505a8SAchiad Shochat 
3083fc24fc5eSAchiad Shochat 	dev->roce.nb.notifier_call = mlx5_netdev_event;
3084e53505a8SAchiad Shochat 	err = register_netdevice_notifier(&dev->roce.nb);
30855ec8c83eSAviv Heller 	if (err) {
30865ec8c83eSAviv Heller 		dev->roce.nb.notifier_call = NULL;
3087e53505a8SAchiad Shochat 		return err;
30885ec8c83eSAviv Heller 	}
3089e53505a8SAchiad Shochat 
3090d012f5d6SOr Gerlitz 	return 0;
3091d012f5d6SOr Gerlitz }
3092d012f5d6SOr Gerlitz 
3093d012f5d6SOr Gerlitz static void mlx5_remove_netdev_notifier(struct mlx5_ib_dev *dev)
3094e126ba97SEli Cohen {
3095e126ba97SEli Cohen 	if (dev->roce.nb.notifier_call) {
3096e126ba97SEli Cohen 		unregister_netdevice_notifier(&dev->roce.nb);
3097e126ba97SEli Cohen 		dev->roce.nb.notifier_call = NULL;
3098e126ba97SEli Cohen 	}
3099e126ba97SEli Cohen }
3100e126ba97SEli Cohen 
310145f95acdSOr Gerlitz static int mlx5_enable_eth(struct mlx5_ib_dev *dev)
3102e126ba97SEli Cohen {
3103e126ba97SEli Cohen 	int err;
3104e126ba97SEli Cohen 
3105d012f5d6SOr Gerlitz 	err = mlx5_add_netdev_notifier(dev);
3106d012f5d6SOr Gerlitz 	if (err)
3107e53505a8SAchiad Shochat 		return err;
3108e53505a8SAchiad Shochat 
3109ca5b91d6SOr Gerlitz 	if (MLX5_CAP_GEN(dev->mdev, roce)) {
3110e53505a8SAchiad Shochat 		err = mlx5_nic_vport_enable_roce(dev->mdev);
3111e53505a8SAchiad Shochat 		if (err)
3112e53505a8SAchiad Shochat 			goto err_unregister_netdevice_notifier;
3113ca5b91d6SOr Gerlitz 	}
3114e53505a8SAchiad Shochat 
311545f95acdSOr Gerlitz 	err = mlx5_eth_lag_init(dev);
31169ef9c640SAviv Heller 	if (err)
31179ef9c640SAviv Heller 		goto err_disable_roce;
31189ef9c640SAviv Heller 
3119e53505a8SAchiad Shochat 	return 0;
3120e53505a8SAchiad Shochat 
31219ef9c640SAviv Heller err_disable_roce:
3122ca5b91d6SOr Gerlitz 	if (MLX5_CAP_GEN(dev->mdev, roce))
31239ef9c640SAviv Heller 		mlx5_nic_vport_disable_roce(dev->mdev);
31249ef9c640SAviv Heller 
3125e53505a8SAchiad Shochat err_unregister_netdevice_notifier:
3126d012f5d6SOr Gerlitz 	mlx5_remove_netdev_notifier(dev);
3127e53505a8SAchiad Shochat 	return err;
3128fc24fc5eSAchiad Shochat }
3129fc24fc5eSAchiad Shochat 
313045f95acdSOr Gerlitz static void mlx5_disable_eth(struct mlx5_ib_dev *dev)
3131fc24fc5eSAchiad Shochat {
313245f95acdSOr Gerlitz 	mlx5_eth_lag_cleanup(dev);
3133ca5b91d6SOr Gerlitz 	if (MLX5_CAP_GEN(dev->mdev, roce))
3134e53505a8SAchiad Shochat 		mlx5_nic_vport_disable_roce(dev->mdev);
3135fc24fc5eSAchiad Shochat }
3136fc24fc5eSAchiad Shochat 
31377c16f477SKamal Heib struct mlx5_ib_q_counter {
31387c16f477SKamal Heib 	const char *name;
31397c16f477SKamal Heib 	size_t offset;
31407c16f477SKamal Heib };
31417c16f477SKamal Heib 
31427c16f477SKamal Heib #define INIT_Q_COUNTER(_name)		\
31437c16f477SKamal Heib 	{ .name = #_name, .offset = MLX5_BYTE_OFF(query_q_counter_out, _name)}
31447c16f477SKamal Heib 
31457c16f477SKamal Heib static const struct mlx5_ib_q_counter basic_q_cnts[] = {
31467c16f477SKamal Heib 	INIT_Q_COUNTER(rx_write_requests),
31477c16f477SKamal Heib 	INIT_Q_COUNTER(rx_read_requests),
31487c16f477SKamal Heib 	INIT_Q_COUNTER(rx_atomic_requests),
31497c16f477SKamal Heib 	INIT_Q_COUNTER(out_of_buffer),
31507c16f477SKamal Heib };
31517c16f477SKamal Heib 
31527c16f477SKamal Heib static const struct mlx5_ib_q_counter out_of_seq_q_cnts[] = {
31537c16f477SKamal Heib 	INIT_Q_COUNTER(out_of_sequence),
31547c16f477SKamal Heib };
31557c16f477SKamal Heib 
31567c16f477SKamal Heib static const struct mlx5_ib_q_counter retrans_q_cnts[] = {
31577c16f477SKamal Heib 	INIT_Q_COUNTER(duplicate_request),
31587c16f477SKamal Heib 	INIT_Q_COUNTER(rnr_nak_retry_err),
31597c16f477SKamal Heib 	INIT_Q_COUNTER(packet_seq_err),
31607c16f477SKamal Heib 	INIT_Q_COUNTER(implied_nak_seq_err),
31617c16f477SKamal Heib 	INIT_Q_COUNTER(local_ack_timeout_err),
31627c16f477SKamal Heib };
31637c16f477SKamal Heib 
31640837e86aSMark Bloch static void mlx5_ib_dealloc_q_counters(struct mlx5_ib_dev *dev)
31650837e86aSMark Bloch {
31660837e86aSMark Bloch 	unsigned int i;
31670837e86aSMark Bloch 
31687c16f477SKamal Heib 	for (i = 0; i < dev->num_ports; i++) {
31690837e86aSMark Bloch 		mlx5_core_dealloc_q_counter(dev->mdev,
31707c16f477SKamal Heib 					    dev->port[i].q_cnts.set_id);
31717c16f477SKamal Heib 		kfree(dev->port[i].q_cnts.names);
31727c16f477SKamal Heib 		kfree(dev->port[i].q_cnts.offsets);
31737c16f477SKamal Heib 	}
31747c16f477SKamal Heib }
31757c16f477SKamal Heib 
31767c16f477SKamal Heib static int __mlx5_ib_alloc_q_counters(struct mlx5_ib_dev *dev,
31777c16f477SKamal Heib 				      const char ***names,
31787c16f477SKamal Heib 				      size_t **offsets,
31797c16f477SKamal Heib 				      u32 *num)
31807c16f477SKamal Heib {
31817c16f477SKamal Heib 	u32 num_counters;
31827c16f477SKamal Heib 
31837c16f477SKamal Heib 	num_counters = ARRAY_SIZE(basic_q_cnts);
31847c16f477SKamal Heib 
31857c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt))
31867c16f477SKamal Heib 		num_counters += ARRAY_SIZE(out_of_seq_q_cnts);
31877c16f477SKamal Heib 
31887c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters))
31897c16f477SKamal Heib 		num_counters += ARRAY_SIZE(retrans_q_cnts);
31907c16f477SKamal Heib 
31917c16f477SKamal Heib 	*names = kcalloc(num_counters, sizeof(**names), GFP_KERNEL);
31927c16f477SKamal Heib 	if (!*names)
31937c16f477SKamal Heib 		return -ENOMEM;
31947c16f477SKamal Heib 
31957c16f477SKamal Heib 	*offsets = kcalloc(num_counters, sizeof(**offsets), GFP_KERNEL);
31967c16f477SKamal Heib 	if (!*offsets)
31977c16f477SKamal Heib 		goto err_names;
31987c16f477SKamal Heib 
31997c16f477SKamal Heib 	*num = num_counters;
32007c16f477SKamal Heib 
32017c16f477SKamal Heib 	return 0;
32027c16f477SKamal Heib 
32037c16f477SKamal Heib err_names:
32047c16f477SKamal Heib 	kfree(*names);
32057c16f477SKamal Heib 	return -ENOMEM;
32067c16f477SKamal Heib }
32077c16f477SKamal Heib 
32087c16f477SKamal Heib static void mlx5_ib_fill_q_counters(struct mlx5_ib_dev *dev,
32097c16f477SKamal Heib 				    const char **names,
32107c16f477SKamal Heib 				    size_t *offsets)
32117c16f477SKamal Heib {
32127c16f477SKamal Heib 	int i;
32137c16f477SKamal Heib 	int j = 0;
32147c16f477SKamal Heib 
32157c16f477SKamal Heib 	for (i = 0; i < ARRAY_SIZE(basic_q_cnts); i++, j++) {
32167c16f477SKamal Heib 		names[j] = basic_q_cnts[i].name;
32177c16f477SKamal Heib 		offsets[j] = basic_q_cnts[i].offset;
32187c16f477SKamal Heib 	}
32197c16f477SKamal Heib 
32207c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt)) {
32217c16f477SKamal Heib 		for (i = 0; i < ARRAY_SIZE(out_of_seq_q_cnts); i++, j++) {
32227c16f477SKamal Heib 			names[j] = out_of_seq_q_cnts[i].name;
32237c16f477SKamal Heib 			offsets[j] = out_of_seq_q_cnts[i].offset;
32247c16f477SKamal Heib 		}
32257c16f477SKamal Heib 	}
32267c16f477SKamal Heib 
32277c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters)) {
32287c16f477SKamal Heib 		for (i = 0; i < ARRAY_SIZE(retrans_q_cnts); i++, j++) {
32297c16f477SKamal Heib 			names[j] = retrans_q_cnts[i].name;
32307c16f477SKamal Heib 			offsets[j] = retrans_q_cnts[i].offset;
32317c16f477SKamal Heib 		}
32327c16f477SKamal Heib 	}
32330837e86aSMark Bloch }
32340837e86aSMark Bloch 
32350837e86aSMark Bloch static int mlx5_ib_alloc_q_counters(struct mlx5_ib_dev *dev)
32360837e86aSMark Bloch {
32370837e86aSMark Bloch 	int i;
32380837e86aSMark Bloch 	int ret;
32390837e86aSMark Bloch 
32400837e86aSMark Bloch 	for (i = 0; i < dev->num_ports; i++) {
32417c16f477SKamal Heib 		struct mlx5_ib_port *port = &dev->port[i];
32427c16f477SKamal Heib 
32430837e86aSMark Bloch 		ret = mlx5_core_alloc_q_counter(dev->mdev,
32447c16f477SKamal Heib 						&port->q_cnts.set_id);
32450837e86aSMark Bloch 		if (ret) {
32460837e86aSMark Bloch 			mlx5_ib_warn(dev,
32470837e86aSMark Bloch 				     "couldn't allocate queue counter for port %d, err %d\n",
32480837e86aSMark Bloch 				     i + 1, ret);
32490837e86aSMark Bloch 			goto dealloc_counters;
32500837e86aSMark Bloch 		}
32517c16f477SKamal Heib 
32527c16f477SKamal Heib 		ret = __mlx5_ib_alloc_q_counters(dev,
32537c16f477SKamal Heib 						 &port->q_cnts.names,
32547c16f477SKamal Heib 						 &port->q_cnts.offsets,
32557c16f477SKamal Heib 						 &port->q_cnts.num_counters);
32567c16f477SKamal Heib 		if (ret)
32577c16f477SKamal Heib 			goto dealloc_counters;
32587c16f477SKamal Heib 
32597c16f477SKamal Heib 		mlx5_ib_fill_q_counters(dev, port->q_cnts.names,
32607c16f477SKamal Heib 					port->q_cnts.offsets);
32610837e86aSMark Bloch 	}
32620837e86aSMark Bloch 
32630837e86aSMark Bloch 	return 0;
32640837e86aSMark Bloch 
32650837e86aSMark Bloch dealloc_counters:
32660837e86aSMark Bloch 	while (--i >= 0)
32670837e86aSMark Bloch 		mlx5_core_dealloc_q_counter(dev->mdev,
32687c16f477SKamal Heib 					    dev->port[i].q_cnts.set_id);
32690837e86aSMark Bloch 
32700837e86aSMark Bloch 	return ret;
32710837e86aSMark Bloch }
32720837e86aSMark Bloch 
32730ad17a8fSMark Bloch static struct rdma_hw_stats *mlx5_ib_alloc_hw_stats(struct ib_device *ibdev,
32740ad17a8fSMark Bloch 						    u8 port_num)
32750ad17a8fSMark Bloch {
32767c16f477SKamal Heib 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
32777c16f477SKamal Heib 	struct mlx5_ib_port *port = &dev->port[port_num - 1];
32780ad17a8fSMark Bloch 
32790ad17a8fSMark Bloch 	/* We support only per port stats */
32800ad17a8fSMark Bloch 	if (port_num == 0)
32810ad17a8fSMark Bloch 		return NULL;
32820ad17a8fSMark Bloch 
32837c16f477SKamal Heib 	return rdma_alloc_hw_stats_struct(port->q_cnts.names,
32847c16f477SKamal Heib 					  port->q_cnts.num_counters,
32850ad17a8fSMark Bloch 					  RDMA_HW_STATS_DEFAULT_LIFESPAN);
32860ad17a8fSMark Bloch }
32870ad17a8fSMark Bloch 
32880ad17a8fSMark Bloch static int mlx5_ib_get_hw_stats(struct ib_device *ibdev,
32890ad17a8fSMark Bloch 				struct rdma_hw_stats *stats,
32907c16f477SKamal Heib 				u8 port_num, int index)
32910ad17a8fSMark Bloch {
32920ad17a8fSMark Bloch 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
32937c16f477SKamal Heib 	struct mlx5_ib_port *port = &dev->port[port_num - 1];
32940ad17a8fSMark Bloch 	int outlen = MLX5_ST_SZ_BYTES(query_q_counter_out);
32950ad17a8fSMark Bloch 	void *out;
32960ad17a8fSMark Bloch 	__be32 val;
32970ad17a8fSMark Bloch 	int ret;
32980ad17a8fSMark Bloch 	int i;
32990ad17a8fSMark Bloch 
33007c16f477SKamal Heib 	if (!stats)
33010ad17a8fSMark Bloch 		return -ENOSYS;
33020ad17a8fSMark Bloch 
33030ad17a8fSMark Bloch 	out = mlx5_vzalloc(outlen);
33040ad17a8fSMark Bloch 	if (!out)
33050ad17a8fSMark Bloch 		return -ENOMEM;
33060ad17a8fSMark Bloch 
33070ad17a8fSMark Bloch 	ret = mlx5_core_query_q_counter(dev->mdev,
33087c16f477SKamal Heib 					port->q_cnts.set_id, 0,
33090ad17a8fSMark Bloch 					out, outlen);
33100ad17a8fSMark Bloch 	if (ret)
33110ad17a8fSMark Bloch 		goto free;
33120ad17a8fSMark Bloch 
33137c16f477SKamal Heib 	for (i = 0; i < port->q_cnts.num_counters; i++) {
33147c16f477SKamal Heib 		val = *(__be32 *)(out + port->q_cnts.offsets[i]);
33150ad17a8fSMark Bloch 		stats->value[i] = (u64)be32_to_cpu(val);
33160ad17a8fSMark Bloch 	}
33177c16f477SKamal Heib 
33180ad17a8fSMark Bloch free:
33190ad17a8fSMark Bloch 	kvfree(out);
33207c16f477SKamal Heib 	return port->q_cnts.num_counters;
33210ad17a8fSMark Bloch }
33220ad17a8fSMark Bloch 
33239603b61dSJack Morgenstein static void *mlx5_ib_add(struct mlx5_core_dev *mdev)
3324e126ba97SEli Cohen {
3325e126ba97SEli Cohen 	struct mlx5_ib_dev *dev;
3326ebd61f68SAchiad Shochat 	enum rdma_link_layer ll;
3327ebd61f68SAchiad Shochat 	int port_type_cap;
33284babcf97SAviv Heller 	const char *name;
3329e126ba97SEli Cohen 	int err;
3330e126ba97SEli Cohen 	int i;
3331e126ba97SEli Cohen 
3332ebd61f68SAchiad Shochat 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
3333ebd61f68SAchiad Shochat 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
3334ebd61f68SAchiad Shochat 
3335e126ba97SEli Cohen 	printk_once(KERN_INFO "%s", mlx5_version);
3336e126ba97SEli Cohen 
3337e126ba97SEli Cohen 	dev = (struct mlx5_ib_dev *)ib_alloc_device(sizeof(*dev));
3338e126ba97SEli Cohen 	if (!dev)
33399603b61dSJack Morgenstein 		return NULL;
3340e126ba97SEli Cohen 
33419603b61dSJack Morgenstein 	dev->mdev = mdev;
3342e126ba97SEli Cohen 
33430837e86aSMark Bloch 	dev->port = kcalloc(MLX5_CAP_GEN(mdev, num_ports), sizeof(*dev->port),
33440837e86aSMark Bloch 			    GFP_KERNEL);
33450837e86aSMark Bloch 	if (!dev->port)
33460837e86aSMark Bloch 		goto err_dealloc;
33470837e86aSMark Bloch 
3348fc24fc5eSAchiad Shochat 	rwlock_init(&dev->roce.netdev_lock);
3349e126ba97SEli Cohen 	err = get_port_caps(dev);
3350e126ba97SEli Cohen 	if (err)
33510837e86aSMark Bloch 		goto err_free_port;
3352e126ba97SEli Cohen 
33531b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(dev))
3354e126ba97SEli Cohen 		get_ext_port_caps(dev);
3355e126ba97SEli Cohen 
33564babcf97SAviv Heller 	if (!mlx5_lag_is_active(mdev))
33574babcf97SAviv Heller 		name = "mlx5_%d";
33584babcf97SAviv Heller 	else
33594babcf97SAviv Heller 		name = "mlx5_bond_%d";
33604babcf97SAviv Heller 
33614babcf97SAviv Heller 	strlcpy(dev->ib_dev.name, name, IB_DEVICE_NAME_MAX);
3362e126ba97SEli Cohen 	dev->ib_dev.owner		= THIS_MODULE;
3363e126ba97SEli Cohen 	dev->ib_dev.node_type		= RDMA_NODE_IB_CA;
3364c6790aa9SSagi Grimberg 	dev->ib_dev.local_dma_lkey	= 0 /* not supported for now */;
3365938fe83cSSaeed Mahameed 	dev->num_ports		= MLX5_CAP_GEN(mdev, num_ports);
3366e126ba97SEli Cohen 	dev->ib_dev.phys_port_cnt     = dev->num_ports;
3367233d05d2SSaeed Mahameed 	dev->ib_dev.num_comp_vectors    =
3368233d05d2SSaeed Mahameed 		dev->mdev->priv.eq_table.num_comp_vectors;
33699b0c289eSBart Van Assche 	dev->ib_dev.dev.parent		= &mdev->pdev->dev;
3370e126ba97SEli Cohen 
3371e126ba97SEli Cohen 	dev->ib_dev.uverbs_abi_ver	= MLX5_IB_UVERBS_ABI_VERSION;
3372e126ba97SEli Cohen 	dev->ib_dev.uverbs_cmd_mask	=
3373e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_GET_CONTEXT)		|
3374e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_DEVICE)	|
3375e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_PORT)		|
3376e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_ALLOC_PD)		|
3377e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DEALLOC_PD)		|
337841c450fdSMoni Shoua 		(1ull << IB_USER_VERBS_CMD_CREATE_AH)		|
337941c450fdSMoni Shoua 		(1ull << IB_USER_VERBS_CMD_DESTROY_AH)		|
3380e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_REG_MR)		|
338156e11d62SNoa Osherovich 		(1ull << IB_USER_VERBS_CMD_REREG_MR)		|
3382e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DEREG_MR)		|
3383e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL)	|
3384e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_CQ)		|
3385e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_RESIZE_CQ)		|
3386e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_CQ)		|
3387e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_QP)		|
3388e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_MODIFY_QP)		|
3389e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_QP)		|
3390e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_QP)		|
3391e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_ATTACH_MCAST)	|
3392e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DETACH_MCAST)	|
3393e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_SRQ)		|
3394e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_MODIFY_SRQ)		|
3395e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_SRQ)		|
3396e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_SRQ)		|
3397e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_XSRQ)		|
3398e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_OPEN_QP);
33991707cb4aSHaggai Eran 	dev->ib_dev.uverbs_ex_cmd_mask =
3400d4584ddfSMatan Barak 		(1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE)	|
3401d4584ddfSMatan Barak 		(1ull << IB_USER_VERBS_EX_CMD_CREATE_CQ)	|
34027d29f349SBodong Wang 		(1ull << IB_USER_VERBS_EX_CMD_CREATE_QP)	|
34037d29f349SBodong Wang 		(1ull << IB_USER_VERBS_EX_CMD_MODIFY_QP);
3404e126ba97SEli Cohen 
3405e126ba97SEli Cohen 	dev->ib_dev.query_device	= mlx5_ib_query_device;
3406e126ba97SEli Cohen 	dev->ib_dev.query_port		= mlx5_ib_query_port;
3407ebd61f68SAchiad Shochat 	dev->ib_dev.get_link_layer	= mlx5_ib_port_link_layer;
3408fc24fc5eSAchiad Shochat 	if (ll == IB_LINK_LAYER_ETHERNET)
3409fc24fc5eSAchiad Shochat 		dev->ib_dev.get_netdev	= mlx5_ib_get_netdev;
3410e126ba97SEli Cohen 	dev->ib_dev.query_gid		= mlx5_ib_query_gid;
34113cca2606SAchiad Shochat 	dev->ib_dev.add_gid		= mlx5_ib_add_gid;
34123cca2606SAchiad Shochat 	dev->ib_dev.del_gid		= mlx5_ib_del_gid;
3413e126ba97SEli Cohen 	dev->ib_dev.query_pkey		= mlx5_ib_query_pkey;
3414e126ba97SEli Cohen 	dev->ib_dev.modify_device	= mlx5_ib_modify_device;
3415e126ba97SEli Cohen 	dev->ib_dev.modify_port		= mlx5_ib_modify_port;
3416e126ba97SEli Cohen 	dev->ib_dev.alloc_ucontext	= mlx5_ib_alloc_ucontext;
3417e126ba97SEli Cohen 	dev->ib_dev.dealloc_ucontext	= mlx5_ib_dealloc_ucontext;
3418e126ba97SEli Cohen 	dev->ib_dev.mmap		= mlx5_ib_mmap;
3419e126ba97SEli Cohen 	dev->ib_dev.alloc_pd		= mlx5_ib_alloc_pd;
3420e126ba97SEli Cohen 	dev->ib_dev.dealloc_pd		= mlx5_ib_dealloc_pd;
3421e126ba97SEli Cohen 	dev->ib_dev.create_ah		= mlx5_ib_create_ah;
3422e126ba97SEli Cohen 	dev->ib_dev.query_ah		= mlx5_ib_query_ah;
3423e126ba97SEli Cohen 	dev->ib_dev.destroy_ah		= mlx5_ib_destroy_ah;
3424e126ba97SEli Cohen 	dev->ib_dev.create_srq		= mlx5_ib_create_srq;
3425e126ba97SEli Cohen 	dev->ib_dev.modify_srq		= mlx5_ib_modify_srq;
3426e126ba97SEli Cohen 	dev->ib_dev.query_srq		= mlx5_ib_query_srq;
3427e126ba97SEli Cohen 	dev->ib_dev.destroy_srq		= mlx5_ib_destroy_srq;
3428e126ba97SEli Cohen 	dev->ib_dev.post_srq_recv	= mlx5_ib_post_srq_recv;
3429e126ba97SEli Cohen 	dev->ib_dev.create_qp		= mlx5_ib_create_qp;
3430e126ba97SEli Cohen 	dev->ib_dev.modify_qp		= mlx5_ib_modify_qp;
3431e126ba97SEli Cohen 	dev->ib_dev.query_qp		= mlx5_ib_query_qp;
3432e126ba97SEli Cohen 	dev->ib_dev.destroy_qp		= mlx5_ib_destroy_qp;
3433e126ba97SEli Cohen 	dev->ib_dev.post_send		= mlx5_ib_post_send;
3434e126ba97SEli Cohen 	dev->ib_dev.post_recv		= mlx5_ib_post_recv;
3435e126ba97SEli Cohen 	dev->ib_dev.create_cq		= mlx5_ib_create_cq;
3436e126ba97SEli Cohen 	dev->ib_dev.modify_cq		= mlx5_ib_modify_cq;
3437e126ba97SEli Cohen 	dev->ib_dev.resize_cq		= mlx5_ib_resize_cq;
3438e126ba97SEli Cohen 	dev->ib_dev.destroy_cq		= mlx5_ib_destroy_cq;
3439e126ba97SEli Cohen 	dev->ib_dev.poll_cq		= mlx5_ib_poll_cq;
3440e126ba97SEli Cohen 	dev->ib_dev.req_notify_cq	= mlx5_ib_arm_cq;
3441e126ba97SEli Cohen 	dev->ib_dev.get_dma_mr		= mlx5_ib_get_dma_mr;
3442e126ba97SEli Cohen 	dev->ib_dev.reg_user_mr		= mlx5_ib_reg_user_mr;
344356e11d62SNoa Osherovich 	dev->ib_dev.rereg_user_mr	= mlx5_ib_rereg_user_mr;
3444e126ba97SEli Cohen 	dev->ib_dev.dereg_mr		= mlx5_ib_dereg_mr;
3445e126ba97SEli Cohen 	dev->ib_dev.attach_mcast	= mlx5_ib_mcg_attach;
3446e126ba97SEli Cohen 	dev->ib_dev.detach_mcast	= mlx5_ib_mcg_detach;
3447e126ba97SEli Cohen 	dev->ib_dev.process_mad		= mlx5_ib_process_mad;
34489bee178bSSagi Grimberg 	dev->ib_dev.alloc_mr		= mlx5_ib_alloc_mr;
34498a187ee5SSagi Grimberg 	dev->ib_dev.map_mr_sg		= mlx5_ib_map_mr_sg;
3450d5436ba0SSagi Grimberg 	dev->ib_dev.check_mr_status	= mlx5_ib_check_mr_status;
34517738613eSIra Weiny 	dev->ib_dev.get_port_immutable  = mlx5_port_immutable;
3452c7342823SIra Weiny 	dev->ib_dev.get_dev_fw_str      = get_dev_fw_str;
3453eff901d3SEli Cohen 	if (mlx5_core_is_pf(mdev)) {
3454eff901d3SEli Cohen 		dev->ib_dev.get_vf_config	= mlx5_ib_get_vf_config;
3455eff901d3SEli Cohen 		dev->ib_dev.set_vf_link_state	= mlx5_ib_set_vf_link_state;
3456eff901d3SEli Cohen 		dev->ib_dev.get_vf_stats	= mlx5_ib_get_vf_stats;
3457eff901d3SEli Cohen 		dev->ib_dev.set_vf_guid		= mlx5_ib_set_vf_guid;
3458eff901d3SEli Cohen 	}
3459e126ba97SEli Cohen 
34607c2344c3SMaor Gottlieb 	dev->ib_dev.disassociate_ucontext = mlx5_ib_disassociate_ucontext;
34617c2344c3SMaor Gottlieb 
3462938fe83cSSaeed Mahameed 	mlx5_ib_internal_fill_odp_caps(dev);
34638cdd312cSHaggai Eran 
3464d2370e0aSMatan Barak 	if (MLX5_CAP_GEN(mdev, imaicl)) {
3465d2370e0aSMatan Barak 		dev->ib_dev.alloc_mw		= mlx5_ib_alloc_mw;
3466d2370e0aSMatan Barak 		dev->ib_dev.dealloc_mw		= mlx5_ib_dealloc_mw;
3467d2370e0aSMatan Barak 		dev->ib_dev.uverbs_cmd_mask |=
3468d2370e0aSMatan Barak 			(1ull << IB_USER_VERBS_CMD_ALLOC_MW)	|
3469d2370e0aSMatan Barak 			(1ull << IB_USER_VERBS_CMD_DEALLOC_MW);
3470d2370e0aSMatan Barak 	}
3471d2370e0aSMatan Barak 
34727c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt)) {
34730ad17a8fSMark Bloch 		dev->ib_dev.get_hw_stats	= mlx5_ib_get_hw_stats;
34740ad17a8fSMark Bloch 		dev->ib_dev.alloc_hw_stats	= mlx5_ib_alloc_hw_stats;
34750ad17a8fSMark Bloch 	}
34760ad17a8fSMark Bloch 
3477938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, xrc)) {
3478e126ba97SEli Cohen 		dev->ib_dev.alloc_xrcd = mlx5_ib_alloc_xrcd;
3479e126ba97SEli Cohen 		dev->ib_dev.dealloc_xrcd = mlx5_ib_dealloc_xrcd;
3480e126ba97SEli Cohen 		dev->ib_dev.uverbs_cmd_mask |=
3481e126ba97SEli Cohen 			(1ull << IB_USER_VERBS_CMD_OPEN_XRCD) |
3482e126ba97SEli Cohen 			(1ull << IB_USER_VERBS_CMD_CLOSE_XRCD);
3483e126ba97SEli Cohen 	}
3484e126ba97SEli Cohen 
3485048ccca8SLinus Torvalds 	if (mlx5_ib_port_link_layer(&dev->ib_dev, 1) ==
3486038d2ef8SMaor Gottlieb 	    IB_LINK_LAYER_ETHERNET) {
3487038d2ef8SMaor Gottlieb 		dev->ib_dev.create_flow	= mlx5_ib_create_flow;
3488038d2ef8SMaor Gottlieb 		dev->ib_dev.destroy_flow = mlx5_ib_destroy_flow;
348979b20a6cSYishai Hadas 		dev->ib_dev.create_wq	 = mlx5_ib_create_wq;
349079b20a6cSYishai Hadas 		dev->ib_dev.modify_wq	 = mlx5_ib_modify_wq;
349179b20a6cSYishai Hadas 		dev->ib_dev.destroy_wq	 = mlx5_ib_destroy_wq;
3492c5f90929SYishai Hadas 		dev->ib_dev.create_rwq_ind_table = mlx5_ib_create_rwq_ind_table;
3493c5f90929SYishai Hadas 		dev->ib_dev.destroy_rwq_ind_table = mlx5_ib_destroy_rwq_ind_table;
3494038d2ef8SMaor Gottlieb 		dev->ib_dev.uverbs_ex_cmd_mask |=
3495038d2ef8SMaor Gottlieb 			(1ull << IB_USER_VERBS_EX_CMD_CREATE_FLOW) |
349679b20a6cSYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_DESTROY_FLOW) |
349779b20a6cSYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_CREATE_WQ) |
349879b20a6cSYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_MODIFY_WQ) |
3499c5f90929SYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_DESTROY_WQ) |
3500c5f90929SYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_CREATE_RWQ_IND_TBL) |
3501c5f90929SYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_DESTROY_RWQ_IND_TBL);
3502038d2ef8SMaor Gottlieb 	}
3503e126ba97SEli Cohen 	err = init_node_data(dev);
3504e126ba97SEli Cohen 	if (err)
350590be7c8aSMajd Dibbiny 		goto err_free_port;
3506e126ba97SEli Cohen 
3507038d2ef8SMaor Gottlieb 	mutex_init(&dev->flow_db.lock);
3508e126ba97SEli Cohen 	mutex_init(&dev->cap_mask_mutex);
350989ea94a7SMaor Gottlieb 	INIT_LIST_HEAD(&dev->qp_list);
351089ea94a7SMaor Gottlieb 	spin_lock_init(&dev->reset_flow_resource_lock);
3511e126ba97SEli Cohen 
3512fc24fc5eSAchiad Shochat 	if (ll == IB_LINK_LAYER_ETHERNET) {
351345f95acdSOr Gerlitz 		err = mlx5_enable_eth(dev);
3514e126ba97SEli Cohen 		if (err)
351590be7c8aSMajd Dibbiny 			goto err_free_port;
3516fc24fc5eSAchiad Shochat 	}
3517fc24fc5eSAchiad Shochat 
3518fc24fc5eSAchiad Shochat 	err = create_dev_resources(&dev->devr);
3519fc24fc5eSAchiad Shochat 	if (err)
352045f95acdSOr Gerlitz 		goto err_disable_eth;
3521e126ba97SEli Cohen 
35226aec21f6SHaggai Eran 	err = mlx5_ib_odp_init_one(dev);
3523281d1a92SWei Yongjun 	if (err)
3524e126ba97SEli Cohen 		goto err_rsrc;
3525e126ba97SEli Cohen 
352645bded2cSKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt)) {
35270837e86aSMark Bloch 		err = mlx5_ib_alloc_q_counters(dev);
35286aec21f6SHaggai Eran 		if (err)
35296aec21f6SHaggai Eran 			goto err_odp;
353045bded2cSKamal Heib 	}
35316aec21f6SHaggai Eran 
35325fe9dec0SEli Cohen 	dev->mdev->priv.uar = mlx5_get_uars_page(dev->mdev);
35335fe9dec0SEli Cohen 	if (!dev->mdev->priv.uar)
35345fe9dec0SEli Cohen 		goto err_q_cnt;
35355fe9dec0SEli Cohen 
35365fe9dec0SEli Cohen 	err = mlx5_alloc_bfreg(dev->mdev, &dev->bfreg, false, false);
35375fe9dec0SEli Cohen 	if (err)
35385fe9dec0SEli Cohen 		goto err_uar_page;
35395fe9dec0SEli Cohen 
35405fe9dec0SEli Cohen 	err = mlx5_alloc_bfreg(dev->mdev, &dev->fp_bfreg, false, true);
35415fe9dec0SEli Cohen 	if (err)
35425fe9dec0SEli Cohen 		goto err_bfreg;
35435fe9dec0SEli Cohen 
35440837e86aSMark Bloch 	err = ib_register_device(&dev->ib_dev, NULL);
35450837e86aSMark Bloch 	if (err)
35465fe9dec0SEli Cohen 		goto err_fp_bfreg;
35470837e86aSMark Bloch 
3548e126ba97SEli Cohen 	err = create_umr_res(dev);
3549e126ba97SEli Cohen 	if (err)
3550e126ba97SEli Cohen 		goto err_dev;
3551e126ba97SEli Cohen 
3552e126ba97SEli Cohen 	for (i = 0; i < ARRAY_SIZE(mlx5_class_attributes); i++) {
3553281d1a92SWei Yongjun 		err = device_create_file(&dev->ib_dev.dev,
3554281d1a92SWei Yongjun 					 mlx5_class_attributes[i]);
3555281d1a92SWei Yongjun 		if (err)
3556e126ba97SEli Cohen 			goto err_umrc;
3557e126ba97SEli Cohen 	}
3558e126ba97SEli Cohen 
3559e126ba97SEli Cohen 	dev->ib_active = true;
3560e126ba97SEli Cohen 
35619603b61dSJack Morgenstein 	return dev;
3562e126ba97SEli Cohen 
3563e126ba97SEli Cohen err_umrc:
3564e126ba97SEli Cohen 	destroy_umrc_res(dev);
3565e126ba97SEli Cohen 
3566e126ba97SEli Cohen err_dev:
3567e126ba97SEli Cohen 	ib_unregister_device(&dev->ib_dev);
3568e126ba97SEli Cohen 
35695fe9dec0SEli Cohen err_fp_bfreg:
35705fe9dec0SEli Cohen 	mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
35715fe9dec0SEli Cohen 
35725fe9dec0SEli Cohen err_bfreg:
35735fe9dec0SEli Cohen 	mlx5_free_bfreg(dev->mdev, &dev->bfreg);
35745fe9dec0SEli Cohen 
35755fe9dec0SEli Cohen err_uar_page:
35765fe9dec0SEli Cohen 	mlx5_put_uars_page(dev->mdev, dev->mdev->priv.uar);
35775fe9dec0SEli Cohen 
35780837e86aSMark Bloch err_q_cnt:
357945bded2cSKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt))
35800837e86aSMark Bloch 		mlx5_ib_dealloc_q_counters(dev);
35810837e86aSMark Bloch 
35826aec21f6SHaggai Eran err_odp:
35836aec21f6SHaggai Eran 	mlx5_ib_odp_remove_one(dev);
35846aec21f6SHaggai Eran 
3585e126ba97SEli Cohen err_rsrc:
3586e126ba97SEli Cohen 	destroy_dev_resources(&dev->devr);
3587e126ba97SEli Cohen 
358845f95acdSOr Gerlitz err_disable_eth:
35895ec8c83eSAviv Heller 	if (ll == IB_LINK_LAYER_ETHERNET) {
359045f95acdSOr Gerlitz 		mlx5_disable_eth(dev);
3591d012f5d6SOr Gerlitz 		mlx5_remove_netdev_notifier(dev);
35925ec8c83eSAviv Heller 	}
3593fc24fc5eSAchiad Shochat 
35940837e86aSMark Bloch err_free_port:
35950837e86aSMark Bloch 	kfree(dev->port);
35960837e86aSMark Bloch 
35979603b61dSJack Morgenstein err_dealloc:
3598e126ba97SEli Cohen 	ib_dealloc_device((struct ib_device *)dev);
3599e126ba97SEli Cohen 
36009603b61dSJack Morgenstein 	return NULL;
3601e126ba97SEli Cohen }
3602e126ba97SEli Cohen 
36039603b61dSJack Morgenstein static void mlx5_ib_remove(struct mlx5_core_dev *mdev, void *context)
3604e126ba97SEli Cohen {
36059603b61dSJack Morgenstein 	struct mlx5_ib_dev *dev = context;
3606fc24fc5eSAchiad Shochat 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev, 1);
36076aec21f6SHaggai Eran 
3608d012f5d6SOr Gerlitz 	mlx5_remove_netdev_notifier(dev);
3609e126ba97SEli Cohen 	ib_unregister_device(&dev->ib_dev);
36105fe9dec0SEli Cohen 	mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
36115fe9dec0SEli Cohen 	mlx5_free_bfreg(dev->mdev, &dev->bfreg);
36125fe9dec0SEli Cohen 	mlx5_put_uars_page(dev->mdev, mdev->priv.uar);
361345bded2cSKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt))
36140837e86aSMark Bloch 		mlx5_ib_dealloc_q_counters(dev);
3615eefd56e5SEli Cohen 	destroy_umrc_res(dev);
36166aec21f6SHaggai Eran 	mlx5_ib_odp_remove_one(dev);
3617e126ba97SEli Cohen 	destroy_dev_resources(&dev->devr);
3618fc24fc5eSAchiad Shochat 	if (ll == IB_LINK_LAYER_ETHERNET)
361945f95acdSOr Gerlitz 		mlx5_disable_eth(dev);
36200837e86aSMark Bloch 	kfree(dev->port);
3621e126ba97SEli Cohen 	ib_dealloc_device(&dev->ib_dev);
3622e126ba97SEli Cohen }
3623e126ba97SEli Cohen 
36249603b61dSJack Morgenstein static struct mlx5_interface mlx5_ib_interface = {
36259603b61dSJack Morgenstein 	.add            = mlx5_ib_add,
36269603b61dSJack Morgenstein 	.remove         = mlx5_ib_remove,
36279603b61dSJack Morgenstein 	.event          = mlx5_ib_event,
3628d9aaed83SArtemy Kovalyov #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
3629d9aaed83SArtemy Kovalyov 	.pfault		= mlx5_ib_pfault,
3630d9aaed83SArtemy Kovalyov #endif
363164613d94SSaeed Mahameed 	.protocol	= MLX5_INTERFACE_PROTOCOL_IB,
3632e126ba97SEli Cohen };
3633e126ba97SEli Cohen 
3634e126ba97SEli Cohen static int __init mlx5_ib_init(void)
3635e126ba97SEli Cohen {
36366aec21f6SHaggai Eran 	int err;
36376aec21f6SHaggai Eran 
363881713d37SArtemy Kovalyov 	mlx5_ib_odp_init();
36399603b61dSJack Morgenstein 
36406aec21f6SHaggai Eran 	err = mlx5_register_interface(&mlx5_ib_interface);
36416aec21f6SHaggai Eran 
36426aec21f6SHaggai Eran 	return err;
3643e126ba97SEli Cohen }
3644e126ba97SEli Cohen 
3645e126ba97SEli Cohen static void __exit mlx5_ib_cleanup(void)
3646e126ba97SEli Cohen {
36479603b61dSJack Morgenstein 	mlx5_unregister_interface(&mlx5_ib_interface);
3648e126ba97SEli Cohen }
3649e126ba97SEli Cohen 
3650e126ba97SEli Cohen module_init(mlx5_ib_init);
3651e126ba97SEli Cohen module_exit(mlx5_ib_cleanup);
3652