xref: /openbmc/linux/drivers/infiniband/hw/mlx5/main.c (revision 6e84f31522f931027bf695752087ece278c10d3f)
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>
44*6e84f315SIngo Molnar #include <linux/sched/mm.h>
457c2344c3SMaor Gottlieb #include <linux/delay.h>
46e126ba97SEli Cohen #include <rdma/ib_user_verbs.h>
473f89a643SAchiad Shochat #include <rdma/ib_addr.h>
482811ba51SAchiad Shochat #include <rdma/ib_cache.h>
49ada68c31SAchiad Shochat #include <linux/mlx5/port.h>
501b5daf11SMajd Dibbiny #include <linux/mlx5/vport.h>
517c2344c3SMaor Gottlieb #include <linux/list.h>
52e126ba97SEli Cohen #include <rdma/ib_smi.h>
53e126ba97SEli Cohen #include <rdma/ib_umem.h>
54038d2ef8SMaor Gottlieb #include <linux/in.h>
55038d2ef8SMaor Gottlieb #include <linux/etherdevice.h>
56038d2ef8SMaor Gottlieb #include <linux/mlx5/fs.h>
5778984898SOr Gerlitz #include <linux/mlx5/vport.h>
58e126ba97SEli Cohen #include "mlx5_ib.h"
59e126ba97SEli Cohen 
60e126ba97SEli Cohen #define DRIVER_NAME "mlx5_ib"
61169a1d85SAmir Vadai #define DRIVER_VERSION "2.2-1"
62169a1d85SAmir Vadai #define DRIVER_RELDATE	"Feb 2014"
63e126ba97SEli Cohen 
64e126ba97SEli Cohen MODULE_AUTHOR("Eli Cohen <eli@mellanox.com>");
65e126ba97SEli Cohen MODULE_DESCRIPTION("Mellanox Connect-IB HCA IB driver");
66e126ba97SEli Cohen MODULE_LICENSE("Dual BSD/GPL");
67e126ba97SEli Cohen MODULE_VERSION(DRIVER_VERSION);
68e126ba97SEli Cohen 
69e126ba97SEli Cohen static char mlx5_version[] =
70e126ba97SEli Cohen 	DRIVER_NAME ": Mellanox Connect-IB Infiniband driver v"
71e126ba97SEli Cohen 	DRIVER_VERSION " (" DRIVER_RELDATE ")\n";
72e126ba97SEli Cohen 
73da7525d2SEran Ben Elisha enum {
74da7525d2SEran Ben Elisha 	MLX5_ATOMIC_SIZE_QP_8BYTES = 1 << 3,
75da7525d2SEran Ben Elisha };
761b5daf11SMajd Dibbiny 
771b5daf11SMajd Dibbiny static enum rdma_link_layer
78ebd61f68SAchiad Shochat mlx5_port_type_cap_to_rdma_ll(int port_type_cap)
791b5daf11SMajd Dibbiny {
80ebd61f68SAchiad Shochat 	switch (port_type_cap) {
811b5daf11SMajd Dibbiny 	case MLX5_CAP_PORT_TYPE_IB:
821b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_INFINIBAND;
831b5daf11SMajd Dibbiny 	case MLX5_CAP_PORT_TYPE_ETH:
841b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_ETHERNET;
851b5daf11SMajd Dibbiny 	default:
861b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_UNSPECIFIED;
871b5daf11SMajd Dibbiny 	}
881b5daf11SMajd Dibbiny }
891b5daf11SMajd Dibbiny 
90ebd61f68SAchiad Shochat static enum rdma_link_layer
91ebd61f68SAchiad Shochat mlx5_ib_port_link_layer(struct ib_device *device, u8 port_num)
92ebd61f68SAchiad Shochat {
93ebd61f68SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(device);
94ebd61f68SAchiad Shochat 	int port_type_cap = MLX5_CAP_GEN(dev->mdev, port_type);
95ebd61f68SAchiad Shochat 
96ebd61f68SAchiad Shochat 	return mlx5_port_type_cap_to_rdma_ll(port_type_cap);
97ebd61f68SAchiad Shochat }
98ebd61f68SAchiad Shochat 
99fc24fc5eSAchiad Shochat static int mlx5_netdev_event(struct notifier_block *this,
100fc24fc5eSAchiad Shochat 			     unsigned long event, void *ptr)
101fc24fc5eSAchiad Shochat {
102fc24fc5eSAchiad Shochat 	struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
103fc24fc5eSAchiad Shochat 	struct mlx5_ib_dev *ibdev = container_of(this, struct mlx5_ib_dev,
104fc24fc5eSAchiad Shochat 						 roce.nb);
105fc24fc5eSAchiad Shochat 
1065ec8c83eSAviv Heller 	switch (event) {
1075ec8c83eSAviv Heller 	case NETDEV_REGISTER:
1085ec8c83eSAviv Heller 	case NETDEV_UNREGISTER:
109fc24fc5eSAchiad Shochat 		write_lock(&ibdev->roce.netdev_lock);
110fc24fc5eSAchiad Shochat 		if (ndev->dev.parent == &ibdev->mdev->pdev->dev)
1115ec8c83eSAviv Heller 			ibdev->roce.netdev = (event == NETDEV_UNREGISTER) ?
1125ec8c83eSAviv Heller 					     NULL : ndev;
113fc24fc5eSAchiad Shochat 		write_unlock(&ibdev->roce.netdev_lock);
1145ec8c83eSAviv Heller 		break;
1155ec8c83eSAviv Heller 
1165ec8c83eSAviv Heller 	case NETDEV_UP:
11788621dfeSAviv Heller 	case NETDEV_DOWN: {
11888621dfeSAviv Heller 		struct net_device *lag_ndev = mlx5_lag_get_roce_netdev(ibdev->mdev);
11988621dfeSAviv Heller 		struct net_device *upper = NULL;
12088621dfeSAviv Heller 
12188621dfeSAviv Heller 		if (lag_ndev) {
12288621dfeSAviv Heller 			upper = netdev_master_upper_dev_get(lag_ndev);
12388621dfeSAviv Heller 			dev_put(lag_ndev);
12488621dfeSAviv Heller 		}
12588621dfeSAviv Heller 
12688621dfeSAviv Heller 		if ((upper == ndev || (!upper && ndev == ibdev->roce.netdev))
12788621dfeSAviv Heller 		    && ibdev->ib_active) {
128626bc02dSBart Van Assche 			struct ib_event ibev = { };
1295ec8c83eSAviv Heller 
1305ec8c83eSAviv Heller 			ibev.device = &ibdev->ib_dev;
1315ec8c83eSAviv Heller 			ibev.event = (event == NETDEV_UP) ?
1325ec8c83eSAviv Heller 				     IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
1335ec8c83eSAviv Heller 			ibev.element.port_num = 1;
1345ec8c83eSAviv Heller 			ib_dispatch_event(&ibev);
1355ec8c83eSAviv Heller 		}
1365ec8c83eSAviv Heller 		break;
13788621dfeSAviv Heller 	}
1385ec8c83eSAviv Heller 
1395ec8c83eSAviv Heller 	default:
1405ec8c83eSAviv Heller 		break;
1415ec8c83eSAviv Heller 	}
142fc24fc5eSAchiad Shochat 
143fc24fc5eSAchiad Shochat 	return NOTIFY_DONE;
144fc24fc5eSAchiad Shochat }
145fc24fc5eSAchiad Shochat 
146fc24fc5eSAchiad Shochat static struct net_device *mlx5_ib_get_netdev(struct ib_device *device,
147fc24fc5eSAchiad Shochat 					     u8 port_num)
148fc24fc5eSAchiad Shochat {
149fc24fc5eSAchiad Shochat 	struct mlx5_ib_dev *ibdev = to_mdev(device);
150fc24fc5eSAchiad Shochat 	struct net_device *ndev;
151fc24fc5eSAchiad Shochat 
15288621dfeSAviv Heller 	ndev = mlx5_lag_get_roce_netdev(ibdev->mdev);
15388621dfeSAviv Heller 	if (ndev)
15488621dfeSAviv Heller 		return ndev;
15588621dfeSAviv Heller 
156fc24fc5eSAchiad Shochat 	/* Ensure ndev does not disappear before we invoke dev_hold()
157fc24fc5eSAchiad Shochat 	 */
158fc24fc5eSAchiad Shochat 	read_lock(&ibdev->roce.netdev_lock);
159fc24fc5eSAchiad Shochat 	ndev = ibdev->roce.netdev;
160fc24fc5eSAchiad Shochat 	if (ndev)
161fc24fc5eSAchiad Shochat 		dev_hold(ndev);
162fc24fc5eSAchiad Shochat 	read_unlock(&ibdev->roce.netdev_lock);
163fc24fc5eSAchiad Shochat 
164fc24fc5eSAchiad Shochat 	return ndev;
165fc24fc5eSAchiad Shochat }
166fc24fc5eSAchiad Shochat 
1673f89a643SAchiad Shochat static int mlx5_query_port_roce(struct ib_device *device, u8 port_num,
1683f89a643SAchiad Shochat 				struct ib_port_attr *props)
1693f89a643SAchiad Shochat {
1703f89a643SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(device);
17188621dfeSAviv Heller 	struct net_device *ndev, *upper;
1723f89a643SAchiad Shochat 	enum ib_mtu ndev_ib_mtu;
173c876a1b7SLeon Romanovsky 	u16 qkey_viol_cntr;
1743f89a643SAchiad Shochat 
175c4550c63SOr Gerlitz 	/* props being zeroed by the caller, avoid zeroing it here */
1763f89a643SAchiad Shochat 
1773f89a643SAchiad Shochat 	props->port_cap_flags  |= IB_PORT_CM_SUP;
1783f89a643SAchiad Shochat 	props->port_cap_flags  |= IB_PORT_IP_BASED_GIDS;
1793f89a643SAchiad Shochat 
1803f89a643SAchiad Shochat 	props->gid_tbl_len      = MLX5_CAP_ROCE(dev->mdev,
1813f89a643SAchiad Shochat 						roce_address_table_size);
1823f89a643SAchiad Shochat 	props->max_mtu          = IB_MTU_4096;
1833f89a643SAchiad Shochat 	props->max_msg_sz       = 1 << MLX5_CAP_GEN(dev->mdev, log_max_msg);
1843f89a643SAchiad Shochat 	props->pkey_tbl_len     = 1;
1853f89a643SAchiad Shochat 	props->state            = IB_PORT_DOWN;
1863f89a643SAchiad Shochat 	props->phys_state       = 3;
1873f89a643SAchiad Shochat 
188c876a1b7SLeon Romanovsky 	mlx5_query_nic_vport_qkey_viol_cntr(dev->mdev, &qkey_viol_cntr);
189c876a1b7SLeon Romanovsky 	props->qkey_viol_cntr = qkey_viol_cntr;
1903f89a643SAchiad Shochat 
1913f89a643SAchiad Shochat 	ndev = mlx5_ib_get_netdev(device, port_num);
1923f89a643SAchiad Shochat 	if (!ndev)
1933f89a643SAchiad Shochat 		return 0;
1943f89a643SAchiad Shochat 
19588621dfeSAviv Heller 	if (mlx5_lag_is_active(dev->mdev)) {
19688621dfeSAviv Heller 		rcu_read_lock();
19788621dfeSAviv Heller 		upper = netdev_master_upper_dev_get_rcu(ndev);
19888621dfeSAviv Heller 		if (upper) {
19988621dfeSAviv Heller 			dev_put(ndev);
20088621dfeSAviv Heller 			ndev = upper;
20188621dfeSAviv Heller 			dev_hold(ndev);
20288621dfeSAviv Heller 		}
20388621dfeSAviv Heller 		rcu_read_unlock();
20488621dfeSAviv Heller 	}
20588621dfeSAviv Heller 
2063f89a643SAchiad Shochat 	if (netif_running(ndev) && netif_carrier_ok(ndev)) {
2073f89a643SAchiad Shochat 		props->state      = IB_PORT_ACTIVE;
2083f89a643SAchiad Shochat 		props->phys_state = 5;
2093f89a643SAchiad Shochat 	}
2103f89a643SAchiad Shochat 
2113f89a643SAchiad Shochat 	ndev_ib_mtu = iboe_get_mtu(ndev->mtu);
2123f89a643SAchiad Shochat 
2133f89a643SAchiad Shochat 	dev_put(ndev);
2143f89a643SAchiad Shochat 
2153f89a643SAchiad Shochat 	props->active_mtu	= min(props->max_mtu, ndev_ib_mtu);
2163f89a643SAchiad Shochat 
2173f89a643SAchiad Shochat 	props->active_width	= IB_WIDTH_4X;  /* TODO */
2183f89a643SAchiad Shochat 	props->active_speed	= IB_SPEED_QDR; /* TODO */
2193f89a643SAchiad Shochat 
2203f89a643SAchiad Shochat 	return 0;
2213f89a643SAchiad Shochat }
2223f89a643SAchiad Shochat 
2233cca2606SAchiad Shochat static void ib_gid_to_mlx5_roce_addr(const union ib_gid *gid,
2243cca2606SAchiad Shochat 				     const struct ib_gid_attr *attr,
2253cca2606SAchiad Shochat 				     void *mlx5_addr)
2263cca2606SAchiad Shochat {
2273cca2606SAchiad Shochat #define MLX5_SET_RA(p, f, v) MLX5_SET(roce_addr_layout, p, f, v)
2283cca2606SAchiad Shochat 	char *mlx5_addr_l3_addr	= MLX5_ADDR_OF(roce_addr_layout, mlx5_addr,
2293cca2606SAchiad Shochat 					       source_l3_address);
2303cca2606SAchiad Shochat 	void *mlx5_addr_mac	= MLX5_ADDR_OF(roce_addr_layout, mlx5_addr,
2313cca2606SAchiad Shochat 					       source_mac_47_32);
2323cca2606SAchiad Shochat 
2333cca2606SAchiad Shochat 	if (!gid)
2343cca2606SAchiad Shochat 		return;
2353cca2606SAchiad Shochat 
2363cca2606SAchiad Shochat 	ether_addr_copy(mlx5_addr_mac, attr->ndev->dev_addr);
2373cca2606SAchiad Shochat 
2383cca2606SAchiad Shochat 	if (is_vlan_dev(attr->ndev)) {
2393cca2606SAchiad Shochat 		MLX5_SET_RA(mlx5_addr, vlan_valid, 1);
2403cca2606SAchiad Shochat 		MLX5_SET_RA(mlx5_addr, vlan_id, vlan_dev_vlan_id(attr->ndev));
2413cca2606SAchiad Shochat 	}
2423cca2606SAchiad Shochat 
2433cca2606SAchiad Shochat 	switch (attr->gid_type) {
2443cca2606SAchiad Shochat 	case IB_GID_TYPE_IB:
2453cca2606SAchiad Shochat 		MLX5_SET_RA(mlx5_addr, roce_version, MLX5_ROCE_VERSION_1);
2463cca2606SAchiad Shochat 		break;
2473cca2606SAchiad Shochat 	case IB_GID_TYPE_ROCE_UDP_ENCAP:
2483cca2606SAchiad Shochat 		MLX5_SET_RA(mlx5_addr, roce_version, MLX5_ROCE_VERSION_2);
2493cca2606SAchiad Shochat 		break;
2503cca2606SAchiad Shochat 
2513cca2606SAchiad Shochat 	default:
2523cca2606SAchiad Shochat 		WARN_ON(true);
2533cca2606SAchiad Shochat 	}
2543cca2606SAchiad Shochat 
2553cca2606SAchiad Shochat 	if (attr->gid_type != IB_GID_TYPE_IB) {
2563cca2606SAchiad Shochat 		if (ipv6_addr_v4mapped((void *)gid))
2573cca2606SAchiad Shochat 			MLX5_SET_RA(mlx5_addr, roce_l3_type,
2583cca2606SAchiad Shochat 				    MLX5_ROCE_L3_TYPE_IPV4);
2593cca2606SAchiad Shochat 		else
2603cca2606SAchiad Shochat 			MLX5_SET_RA(mlx5_addr, roce_l3_type,
2613cca2606SAchiad Shochat 				    MLX5_ROCE_L3_TYPE_IPV6);
2623cca2606SAchiad Shochat 	}
2633cca2606SAchiad Shochat 
2643cca2606SAchiad Shochat 	if ((attr->gid_type == IB_GID_TYPE_IB) ||
2653cca2606SAchiad Shochat 	    !ipv6_addr_v4mapped((void *)gid))
2663cca2606SAchiad Shochat 		memcpy(mlx5_addr_l3_addr, gid, sizeof(*gid));
2673cca2606SAchiad Shochat 	else
2683cca2606SAchiad Shochat 		memcpy(&mlx5_addr_l3_addr[12], &gid->raw[12], 4);
2693cca2606SAchiad Shochat }
2703cca2606SAchiad Shochat 
2713cca2606SAchiad Shochat static int set_roce_addr(struct ib_device *device, u8 port_num,
2723cca2606SAchiad Shochat 			 unsigned int index,
2733cca2606SAchiad Shochat 			 const union ib_gid *gid,
2743cca2606SAchiad Shochat 			 const struct ib_gid_attr *attr)
2753cca2606SAchiad Shochat {
2763cca2606SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(device);
277c4f287c4SSaeed Mahameed 	u32  in[MLX5_ST_SZ_DW(set_roce_address_in)]  = {0};
278c4f287c4SSaeed Mahameed 	u32 out[MLX5_ST_SZ_DW(set_roce_address_out)] = {0};
2793cca2606SAchiad Shochat 	void *in_addr = MLX5_ADDR_OF(set_roce_address_in, in, roce_address);
2803cca2606SAchiad Shochat 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(device, port_num);
2813cca2606SAchiad Shochat 
2823cca2606SAchiad Shochat 	if (ll != IB_LINK_LAYER_ETHERNET)
2833cca2606SAchiad Shochat 		return -EINVAL;
2843cca2606SAchiad Shochat 
2853cca2606SAchiad Shochat 	ib_gid_to_mlx5_roce_addr(gid, attr, in_addr);
2863cca2606SAchiad Shochat 
2873cca2606SAchiad Shochat 	MLX5_SET(set_roce_address_in, in, roce_address_index, index);
2883cca2606SAchiad Shochat 	MLX5_SET(set_roce_address_in, in, opcode, MLX5_CMD_OP_SET_ROCE_ADDRESS);
2893cca2606SAchiad Shochat 	return mlx5_cmd_exec(dev->mdev, in, sizeof(in), out, sizeof(out));
2903cca2606SAchiad Shochat }
2913cca2606SAchiad Shochat 
2923cca2606SAchiad Shochat static int mlx5_ib_add_gid(struct ib_device *device, u8 port_num,
2933cca2606SAchiad Shochat 			   unsigned int index, const union ib_gid *gid,
2943cca2606SAchiad Shochat 			   const struct ib_gid_attr *attr,
2953cca2606SAchiad Shochat 			   __always_unused void **context)
2963cca2606SAchiad Shochat {
2973cca2606SAchiad Shochat 	return set_roce_addr(device, port_num, index, gid, attr);
2983cca2606SAchiad Shochat }
2993cca2606SAchiad Shochat 
3003cca2606SAchiad Shochat static int mlx5_ib_del_gid(struct ib_device *device, u8 port_num,
3013cca2606SAchiad Shochat 			   unsigned int index, __always_unused void **context)
3023cca2606SAchiad Shochat {
3033cca2606SAchiad Shochat 	return set_roce_addr(device, port_num, index, NULL, NULL);
3043cca2606SAchiad Shochat }
3053cca2606SAchiad Shochat 
3062811ba51SAchiad Shochat __be16 mlx5_get_roce_udp_sport(struct mlx5_ib_dev *dev, u8 port_num,
3072811ba51SAchiad Shochat 			       int index)
3082811ba51SAchiad Shochat {
3092811ba51SAchiad Shochat 	struct ib_gid_attr attr;
3102811ba51SAchiad Shochat 	union ib_gid gid;
3112811ba51SAchiad Shochat 
3122811ba51SAchiad Shochat 	if (ib_get_cached_gid(&dev->ib_dev, port_num, index, &gid, &attr))
3132811ba51SAchiad Shochat 		return 0;
3142811ba51SAchiad Shochat 
3152811ba51SAchiad Shochat 	if (!attr.ndev)
3162811ba51SAchiad Shochat 		return 0;
3172811ba51SAchiad Shochat 
3182811ba51SAchiad Shochat 	dev_put(attr.ndev);
3192811ba51SAchiad Shochat 
3202811ba51SAchiad Shochat 	if (attr.gid_type != IB_GID_TYPE_ROCE_UDP_ENCAP)
3212811ba51SAchiad Shochat 		return 0;
3222811ba51SAchiad Shochat 
3232811ba51SAchiad Shochat 	return cpu_to_be16(MLX5_CAP_ROCE(dev->mdev, r_roce_min_src_udp_port));
3242811ba51SAchiad Shochat }
3252811ba51SAchiad Shochat 
326ed88451eSMajd Dibbiny int mlx5_get_roce_gid_type(struct mlx5_ib_dev *dev, u8 port_num,
327ed88451eSMajd Dibbiny 			   int index, enum ib_gid_type *gid_type)
328ed88451eSMajd Dibbiny {
329ed88451eSMajd Dibbiny 	struct ib_gid_attr attr;
330ed88451eSMajd Dibbiny 	union ib_gid gid;
331ed88451eSMajd Dibbiny 	int ret;
332ed88451eSMajd Dibbiny 
333ed88451eSMajd Dibbiny 	ret = ib_get_cached_gid(&dev->ib_dev, port_num, index, &gid, &attr);
334ed88451eSMajd Dibbiny 	if (ret)
335ed88451eSMajd Dibbiny 		return ret;
336ed88451eSMajd Dibbiny 
337ed88451eSMajd Dibbiny 	if (!attr.ndev)
338ed88451eSMajd Dibbiny 		return -ENODEV;
339ed88451eSMajd Dibbiny 
340ed88451eSMajd Dibbiny 	dev_put(attr.ndev);
341ed88451eSMajd Dibbiny 
342ed88451eSMajd Dibbiny 	*gid_type = attr.gid_type;
343ed88451eSMajd Dibbiny 
344ed88451eSMajd Dibbiny 	return 0;
345ed88451eSMajd Dibbiny }
346ed88451eSMajd Dibbiny 
3471b5daf11SMajd Dibbiny static int mlx5_use_mad_ifc(struct mlx5_ib_dev *dev)
3481b5daf11SMajd Dibbiny {
3497fae6655SNoa Osherovich 	if (MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_IB)
350d603c809SEli Cohen 		return !MLX5_CAP_GEN(dev->mdev, ib_virt);
3517fae6655SNoa Osherovich 	return 0;
3521b5daf11SMajd Dibbiny }
3531b5daf11SMajd Dibbiny 
3541b5daf11SMajd Dibbiny enum {
3551b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_MAD,
3561b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_HCA,
3571b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_NIC,
3581b5daf11SMajd Dibbiny };
3591b5daf11SMajd Dibbiny 
3601b5daf11SMajd Dibbiny static int mlx5_get_vport_access_method(struct ib_device *ibdev)
3611b5daf11SMajd Dibbiny {
3621b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(to_mdev(ibdev)))
3631b5daf11SMajd Dibbiny 		return MLX5_VPORT_ACCESS_METHOD_MAD;
3641b5daf11SMajd Dibbiny 
365ebd61f68SAchiad Shochat 	if (mlx5_ib_port_link_layer(ibdev, 1) ==
3661b5daf11SMajd Dibbiny 	    IB_LINK_LAYER_ETHERNET)
3671b5daf11SMajd Dibbiny 		return MLX5_VPORT_ACCESS_METHOD_NIC;
3681b5daf11SMajd Dibbiny 
3691b5daf11SMajd Dibbiny 	return MLX5_VPORT_ACCESS_METHOD_HCA;
3701b5daf11SMajd Dibbiny }
3711b5daf11SMajd Dibbiny 
372da7525d2SEran Ben Elisha static void get_atomic_caps(struct mlx5_ib_dev *dev,
373da7525d2SEran Ben Elisha 			    struct ib_device_attr *props)
374da7525d2SEran Ben Elisha {
375da7525d2SEran Ben Elisha 	u8 tmp;
376da7525d2SEran Ben Elisha 	u8 atomic_operations = MLX5_CAP_ATOMIC(dev->mdev, atomic_operations);
377da7525d2SEran Ben Elisha 	u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_qp);
378da7525d2SEran Ben Elisha 	u8 atomic_req_8B_endianness_mode =
379da7525d2SEran Ben Elisha 		MLX5_CAP_ATOMIC(dev->mdev, atomic_req_8B_endianess_mode);
380da7525d2SEran Ben Elisha 
381da7525d2SEran Ben Elisha 	/* Check if HW supports 8 bytes standard atomic operations and capable
382da7525d2SEran Ben Elisha 	 * of host endianness respond
383da7525d2SEran Ben Elisha 	 */
384da7525d2SEran Ben Elisha 	tmp = MLX5_ATOMIC_OPS_CMP_SWAP | MLX5_ATOMIC_OPS_FETCH_ADD;
385da7525d2SEran Ben Elisha 	if (((atomic_operations & tmp) == tmp) &&
386da7525d2SEran Ben Elisha 	    (atomic_size_qp & MLX5_ATOMIC_SIZE_QP_8BYTES) &&
387da7525d2SEran Ben Elisha 	    (atomic_req_8B_endianness_mode)) {
388da7525d2SEran Ben Elisha 		props->atomic_cap = IB_ATOMIC_HCA;
389da7525d2SEran Ben Elisha 	} else {
390da7525d2SEran Ben Elisha 		props->atomic_cap = IB_ATOMIC_NONE;
391da7525d2SEran Ben Elisha 	}
392da7525d2SEran Ben Elisha }
393da7525d2SEran Ben Elisha 
3941b5daf11SMajd Dibbiny static int mlx5_query_system_image_guid(struct ib_device *ibdev,
3951b5daf11SMajd Dibbiny 					__be64 *sys_image_guid)
3961b5daf11SMajd Dibbiny {
3971b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
3981b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
3991b5daf11SMajd Dibbiny 	u64 tmp;
4001b5daf11SMajd Dibbiny 	int err;
4011b5daf11SMajd Dibbiny 
4021b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
4031b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
4041b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_system_image_guid(ibdev,
4051b5daf11SMajd Dibbiny 							    sys_image_guid);
4061b5daf11SMajd Dibbiny 
4071b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
4081b5daf11SMajd Dibbiny 		err = mlx5_query_hca_vport_system_image_guid(mdev, &tmp);
4093f89a643SAchiad Shochat 		break;
4103f89a643SAchiad Shochat 
4113f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
4123f89a643SAchiad Shochat 		err = mlx5_query_nic_vport_system_image_guid(mdev, &tmp);
4133f89a643SAchiad Shochat 		break;
4141b5daf11SMajd Dibbiny 
4151b5daf11SMajd Dibbiny 	default:
4161b5daf11SMajd Dibbiny 		return -EINVAL;
4171b5daf11SMajd Dibbiny 	}
4183f89a643SAchiad Shochat 
4193f89a643SAchiad Shochat 	if (!err)
4203f89a643SAchiad Shochat 		*sys_image_guid = cpu_to_be64(tmp);
4213f89a643SAchiad Shochat 
4223f89a643SAchiad Shochat 	return err;
4233f89a643SAchiad Shochat 
4241b5daf11SMajd Dibbiny }
4251b5daf11SMajd Dibbiny 
4261b5daf11SMajd Dibbiny static int mlx5_query_max_pkeys(struct ib_device *ibdev,
4271b5daf11SMajd Dibbiny 				u16 *max_pkeys)
4281b5daf11SMajd Dibbiny {
4291b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
4301b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
4311b5daf11SMajd Dibbiny 
4321b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
4331b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
4341b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_max_pkeys(ibdev, max_pkeys);
4351b5daf11SMajd Dibbiny 
4361b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
4371b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
4381b5daf11SMajd Dibbiny 		*max_pkeys = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev,
4391b5daf11SMajd Dibbiny 						pkey_table_size));
4401b5daf11SMajd Dibbiny 		return 0;
4411b5daf11SMajd Dibbiny 
4421b5daf11SMajd Dibbiny 	default:
4431b5daf11SMajd Dibbiny 		return -EINVAL;
4441b5daf11SMajd Dibbiny 	}
4451b5daf11SMajd Dibbiny }
4461b5daf11SMajd Dibbiny 
4471b5daf11SMajd Dibbiny static int mlx5_query_vendor_id(struct ib_device *ibdev,
4481b5daf11SMajd Dibbiny 				u32 *vendor_id)
4491b5daf11SMajd Dibbiny {
4501b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
4511b5daf11SMajd Dibbiny 
4521b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
4531b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
4541b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_vendor_id(ibdev, vendor_id);
4551b5daf11SMajd Dibbiny 
4561b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
4571b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
4581b5daf11SMajd Dibbiny 		return mlx5_core_query_vendor_id(dev->mdev, vendor_id);
4591b5daf11SMajd Dibbiny 
4601b5daf11SMajd Dibbiny 	default:
4611b5daf11SMajd Dibbiny 		return -EINVAL;
4621b5daf11SMajd Dibbiny 	}
4631b5daf11SMajd Dibbiny }
4641b5daf11SMajd Dibbiny 
4651b5daf11SMajd Dibbiny static int mlx5_query_node_guid(struct mlx5_ib_dev *dev,
4661b5daf11SMajd Dibbiny 				__be64 *node_guid)
4671b5daf11SMajd Dibbiny {
4681b5daf11SMajd Dibbiny 	u64 tmp;
4691b5daf11SMajd Dibbiny 	int err;
4701b5daf11SMajd Dibbiny 
4711b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(&dev->ib_dev)) {
4721b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
4731b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_node_guid(dev, node_guid);
4741b5daf11SMajd Dibbiny 
4751b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
4761b5daf11SMajd Dibbiny 		err = mlx5_query_hca_vport_node_guid(dev->mdev, &tmp);
4773f89a643SAchiad Shochat 		break;
4783f89a643SAchiad Shochat 
4793f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
4803f89a643SAchiad Shochat 		err = mlx5_query_nic_vport_node_guid(dev->mdev, &tmp);
4813f89a643SAchiad Shochat 		break;
4821b5daf11SMajd Dibbiny 
4831b5daf11SMajd Dibbiny 	default:
4841b5daf11SMajd Dibbiny 		return -EINVAL;
4851b5daf11SMajd Dibbiny 	}
4863f89a643SAchiad Shochat 
4873f89a643SAchiad Shochat 	if (!err)
4883f89a643SAchiad Shochat 		*node_guid = cpu_to_be64(tmp);
4893f89a643SAchiad Shochat 
4903f89a643SAchiad Shochat 	return err;
4911b5daf11SMajd Dibbiny }
4921b5daf11SMajd Dibbiny 
4931b5daf11SMajd Dibbiny struct mlx5_reg_node_desc {
494bd99fdeaSYuval Shaia 	u8	desc[IB_DEVICE_NODE_DESC_MAX];
4951b5daf11SMajd Dibbiny };
4961b5daf11SMajd Dibbiny 
4971b5daf11SMajd Dibbiny static int mlx5_query_node_desc(struct mlx5_ib_dev *dev, char *node_desc)
4981b5daf11SMajd Dibbiny {
4991b5daf11SMajd Dibbiny 	struct mlx5_reg_node_desc in;
5001b5daf11SMajd Dibbiny 
5011b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(dev))
5021b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_node_desc(dev, node_desc);
5031b5daf11SMajd Dibbiny 
5041b5daf11SMajd Dibbiny 	memset(&in, 0, sizeof(in));
5051b5daf11SMajd Dibbiny 
5061b5daf11SMajd Dibbiny 	return mlx5_core_access_reg(dev->mdev, &in, sizeof(in), node_desc,
5071b5daf11SMajd Dibbiny 				    sizeof(struct mlx5_reg_node_desc),
5081b5daf11SMajd Dibbiny 				    MLX5_REG_NODE_DESC, 0, 0);
5091b5daf11SMajd Dibbiny }
5101b5daf11SMajd Dibbiny 
511e126ba97SEli Cohen static int mlx5_ib_query_device(struct ib_device *ibdev,
5122528e33eSMatan Barak 				struct ib_device_attr *props,
5132528e33eSMatan Barak 				struct ib_udata *uhw)
514e126ba97SEli Cohen {
515e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
516938fe83cSSaeed Mahameed 	struct mlx5_core_dev *mdev = dev->mdev;
517e126ba97SEli Cohen 	int err = -ENOMEM;
518288c01b7SEli Cohen 	int max_sq_desc;
519e126ba97SEli Cohen 	int max_rq_sg;
520e126ba97SEli Cohen 	int max_sq_sg;
521e0238a6aSSagi Grimberg 	u64 min_page_size = 1ull << MLX5_CAP_GEN(mdev, log_pg_sz);
522402ca536SBodong Wang 	struct mlx5_ib_query_device_resp resp = {};
523402ca536SBodong Wang 	size_t resp_len;
524402ca536SBodong Wang 	u64 max_tso;
525e126ba97SEli Cohen 
526402ca536SBodong Wang 	resp_len = sizeof(resp.comp_mask) + sizeof(resp.response_length);
527402ca536SBodong Wang 	if (uhw->outlen && uhw->outlen < resp_len)
528402ca536SBodong Wang 		return -EINVAL;
529402ca536SBodong Wang 	else
530402ca536SBodong Wang 		resp.response_length = resp_len;
531402ca536SBodong Wang 
532402ca536SBodong Wang 	if (uhw->inlen && !ib_is_udata_cleared(uhw, 0, uhw->inlen))
5332528e33eSMatan Barak 		return -EINVAL;
5342528e33eSMatan Barak 
535e126ba97SEli Cohen 	memset(props, 0, sizeof(*props));
5361b5daf11SMajd Dibbiny 	err = mlx5_query_system_image_guid(ibdev,
5371b5daf11SMajd Dibbiny 					   &props->sys_image_guid);
5381b5daf11SMajd Dibbiny 	if (err)
5391b5daf11SMajd Dibbiny 		return err;
5401b5daf11SMajd Dibbiny 
5411b5daf11SMajd Dibbiny 	err = mlx5_query_max_pkeys(ibdev, &props->max_pkeys);
5421b5daf11SMajd Dibbiny 	if (err)
5431b5daf11SMajd Dibbiny 		return err;
5441b5daf11SMajd Dibbiny 
5451b5daf11SMajd Dibbiny 	err = mlx5_query_vendor_id(ibdev, &props->vendor_id);
5461b5daf11SMajd Dibbiny 	if (err)
5471b5daf11SMajd Dibbiny 		return err;
548e126ba97SEli Cohen 
5499603b61dSJack Morgenstein 	props->fw_ver = ((u64)fw_rev_maj(dev->mdev) << 32) |
5509603b61dSJack Morgenstein 		(fw_rev_min(dev->mdev) << 16) |
5519603b61dSJack Morgenstein 		fw_rev_sub(dev->mdev);
552e126ba97SEli Cohen 	props->device_cap_flags    = IB_DEVICE_CHANGE_PHY_PORT |
553e126ba97SEli Cohen 		IB_DEVICE_PORT_ACTIVE_EVENT		|
554e126ba97SEli Cohen 		IB_DEVICE_SYS_IMAGE_GUID		|
5551a4c3a3dSEli Cohen 		IB_DEVICE_RC_RNR_NAK_GEN;
556938fe83cSSaeed Mahameed 
557938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, pkv))
558e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
559938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, qkv))
560e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
561938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, apm))
562e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
563938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, xrc))
564e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_XRC;
565d2370e0aSMatan Barak 	if (MLX5_CAP_GEN(mdev, imaicl)) {
566d2370e0aSMatan Barak 		props->device_cap_flags |= IB_DEVICE_MEM_WINDOW |
567d2370e0aSMatan Barak 					   IB_DEVICE_MEM_WINDOW_TYPE_2B;
568d2370e0aSMatan Barak 		props->max_mw = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
569b005d316SSagi Grimberg 		/* We support 'Gappy' memory registration too */
570b005d316SSagi Grimberg 		props->device_cap_flags |= IB_DEVICE_SG_GAPS_REG;
571d2370e0aSMatan Barak 	}
572e126ba97SEli Cohen 	props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
573938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, sho)) {
5742dea9094SSagi Grimberg 		props->device_cap_flags |= IB_DEVICE_SIGNATURE_HANDOVER;
5752dea9094SSagi Grimberg 		/* At this stage no support for signature handover */
5762dea9094SSagi Grimberg 		props->sig_prot_cap = IB_PROT_T10DIF_TYPE_1 |
5772dea9094SSagi Grimberg 				      IB_PROT_T10DIF_TYPE_2 |
5782dea9094SSagi Grimberg 				      IB_PROT_T10DIF_TYPE_3;
5792dea9094SSagi Grimberg 		props->sig_guard_cap = IB_GUARD_T10DIF_CRC |
5802dea9094SSagi Grimberg 				       IB_GUARD_T10DIF_CSUM;
5812dea9094SSagi Grimberg 	}
582938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, block_lb_mc))
583f360d88aSEli Cohen 		props->device_cap_flags |= IB_DEVICE_BLOCK_MULTICAST_LOOPBACK;
584e126ba97SEli Cohen 
585402ca536SBodong Wang 	if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads)) {
586e8161334SNoa Osherovich 		if (MLX5_CAP_ETH(mdev, csum_cap)) {
587e8161334SNoa Osherovich 			/* Legacy bit to support old userspace libraries */
58888115fe7SBodong Wang 			props->device_cap_flags |= IB_DEVICE_RAW_IP_CSUM;
589e8161334SNoa Osherovich 			props->raw_packet_caps |= IB_RAW_PACKET_CAP_IP_CSUM;
590e8161334SNoa Osherovich 		}
591e8161334SNoa Osherovich 
592e8161334SNoa Osherovich 		if (MLX5_CAP_ETH(dev->mdev, vlan_cap))
593e8161334SNoa Osherovich 			props->raw_packet_caps |=
594e8161334SNoa Osherovich 				IB_RAW_PACKET_CAP_CVLAN_STRIPPING;
59588115fe7SBodong Wang 
596402ca536SBodong Wang 		if (field_avail(typeof(resp), tso_caps, uhw->outlen)) {
597402ca536SBodong Wang 			max_tso = MLX5_CAP_ETH(mdev, max_lso_cap);
598402ca536SBodong Wang 			if (max_tso) {
599402ca536SBodong Wang 				resp.tso_caps.max_tso = 1 << max_tso;
600402ca536SBodong Wang 				resp.tso_caps.supported_qpts |=
601402ca536SBodong Wang 					1 << IB_QPT_RAW_PACKET;
602402ca536SBodong Wang 				resp.response_length += sizeof(resp.tso_caps);
603402ca536SBodong Wang 			}
604402ca536SBodong Wang 		}
60531f69a82SYishai Hadas 
60631f69a82SYishai Hadas 		if (field_avail(typeof(resp), rss_caps, uhw->outlen)) {
60731f69a82SYishai Hadas 			resp.rss_caps.rx_hash_function =
60831f69a82SYishai Hadas 						MLX5_RX_HASH_FUNC_TOEPLITZ;
60931f69a82SYishai Hadas 			resp.rss_caps.rx_hash_fields_mask =
61031f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_IPV4 |
61131f69a82SYishai Hadas 						MLX5_RX_HASH_DST_IPV4 |
61231f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_IPV6 |
61331f69a82SYishai Hadas 						MLX5_RX_HASH_DST_IPV6 |
61431f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_PORT_TCP |
61531f69a82SYishai Hadas 						MLX5_RX_HASH_DST_PORT_TCP |
61631f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_PORT_UDP |
61731f69a82SYishai Hadas 						MLX5_RX_HASH_DST_PORT_UDP;
61831f69a82SYishai Hadas 			resp.response_length += sizeof(resp.rss_caps);
61931f69a82SYishai Hadas 		}
62031f69a82SYishai Hadas 	} else {
62131f69a82SYishai Hadas 		if (field_avail(typeof(resp), tso_caps, uhw->outlen))
62231f69a82SYishai Hadas 			resp.response_length += sizeof(resp.tso_caps);
62331f69a82SYishai Hadas 		if (field_avail(typeof(resp), rss_caps, uhw->outlen))
62431f69a82SYishai Hadas 			resp.response_length += sizeof(resp.rss_caps);
625402ca536SBodong Wang 	}
626402ca536SBodong Wang 
627f0313965SErez Shitrit 	if (MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) {
628f0313965SErez Shitrit 		props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
629f0313965SErez Shitrit 		props->device_cap_flags |= IB_DEVICE_UD_TSO;
630f0313965SErez Shitrit 	}
631f0313965SErez Shitrit 
632cff5a0f3SMajd Dibbiny 	if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
633e8161334SNoa Osherovich 	    MLX5_CAP_ETH(dev->mdev, scatter_fcs)) {
634e8161334SNoa Osherovich 		/* Legacy bit to support old userspace libraries */
635cff5a0f3SMajd Dibbiny 		props->device_cap_flags |= IB_DEVICE_RAW_SCATTER_FCS;
636e8161334SNoa Osherovich 		props->raw_packet_caps |= IB_RAW_PACKET_CAP_SCATTER_FCS;
637e8161334SNoa Osherovich 	}
638cff5a0f3SMajd Dibbiny 
639da6d6ba3SMaor Gottlieb 	if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS))
640da6d6ba3SMaor Gottlieb 		props->device_cap_flags |= IB_DEVICE_MANAGED_FLOW_STEERING;
641da6d6ba3SMaor Gottlieb 
6421b5daf11SMajd Dibbiny 	props->vendor_part_id	   = mdev->pdev->device;
6431b5daf11SMajd Dibbiny 	props->hw_ver		   = mdev->pdev->revision;
644e126ba97SEli Cohen 
645e126ba97SEli Cohen 	props->max_mr_size	   = ~0ull;
646e0238a6aSSagi Grimberg 	props->page_size_cap	   = ~(min_page_size - 1);
647938fe83cSSaeed Mahameed 	props->max_qp		   = 1 << MLX5_CAP_GEN(mdev, log_max_qp);
648938fe83cSSaeed Mahameed 	props->max_qp_wr	   = 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
649938fe83cSSaeed Mahameed 	max_rq_sg =  MLX5_CAP_GEN(mdev, max_wqe_sz_rq) /
650938fe83cSSaeed Mahameed 		     sizeof(struct mlx5_wqe_data_seg);
651288c01b7SEli Cohen 	max_sq_desc = min_t(int, MLX5_CAP_GEN(mdev, max_wqe_sz_sq), 512);
652288c01b7SEli Cohen 	max_sq_sg = (max_sq_desc - sizeof(struct mlx5_wqe_ctrl_seg) -
653288c01b7SEli Cohen 		     sizeof(struct mlx5_wqe_raddr_seg)) /
654e126ba97SEli Cohen 		sizeof(struct mlx5_wqe_data_seg);
655e126ba97SEli Cohen 	props->max_sge = min(max_rq_sg, max_sq_sg);
656986ef95eSSagi Grimberg 	props->max_sge_rd	   = MLX5_MAX_SGE_RD;
657938fe83cSSaeed Mahameed 	props->max_cq		   = 1 << MLX5_CAP_GEN(mdev, log_max_cq);
6589f177686SLeon Romanovsky 	props->max_cqe = (1 << MLX5_CAP_GEN(mdev, log_max_cq_sz)) - 1;
659938fe83cSSaeed Mahameed 	props->max_mr		   = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
660938fe83cSSaeed Mahameed 	props->max_pd		   = 1 << MLX5_CAP_GEN(mdev, log_max_pd);
661938fe83cSSaeed Mahameed 	props->max_qp_rd_atom	   = 1 << MLX5_CAP_GEN(mdev, log_max_ra_req_qp);
662938fe83cSSaeed Mahameed 	props->max_qp_init_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_res_qp);
663938fe83cSSaeed Mahameed 	props->max_srq		   = 1 << MLX5_CAP_GEN(mdev, log_max_srq);
664938fe83cSSaeed Mahameed 	props->max_srq_wr = (1 << MLX5_CAP_GEN(mdev, log_max_srq_sz)) - 1;
665938fe83cSSaeed Mahameed 	props->local_ca_ack_delay  = MLX5_CAP_GEN(mdev, local_ca_ack_delay);
666e126ba97SEli Cohen 	props->max_res_rd_atom	   = props->max_qp_rd_atom * props->max_qp;
667e126ba97SEli Cohen 	props->max_srq_sge	   = max_rq_sg - 1;
668911f4331SSagi Grimberg 	props->max_fast_reg_page_list_len =
669911f4331SSagi Grimberg 		1 << MLX5_CAP_GEN(mdev, log_max_klm_list_size);
670da7525d2SEran Ben Elisha 	get_atomic_caps(dev, props);
67181bea28fSEli Cohen 	props->masked_atomic_cap   = IB_ATOMIC_NONE;
672938fe83cSSaeed Mahameed 	props->max_mcast_grp	   = 1 << MLX5_CAP_GEN(mdev, log_max_mcg);
673938fe83cSSaeed Mahameed 	props->max_mcast_qp_attach = MLX5_CAP_GEN(mdev, max_qp_mcg);
674e126ba97SEli Cohen 	props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
675e126ba97SEli Cohen 					   props->max_mcast_grp;
676e126ba97SEli Cohen 	props->max_map_per_fmr = INT_MAX; /* no limit in ConnectIB */
67786695a65SMaor Gottlieb 	props->max_ah = INT_MAX;
6787c60bcbbSMatan Barak 	props->hca_core_clock = MLX5_CAP_GEN(mdev, device_frequency_khz);
6797c60bcbbSMatan Barak 	props->timestamp_mask = 0x7FFFFFFFFFFFFFFFULL;
680e126ba97SEli Cohen 
6818cdd312cSHaggai Eran #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
682938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, pg))
6838cdd312cSHaggai Eran 		props->device_cap_flags |= IB_DEVICE_ON_DEMAND_PAGING;
6848cdd312cSHaggai Eran 	props->odp_caps = dev->odp_caps;
6858cdd312cSHaggai Eran #endif
6868cdd312cSHaggai Eran 
687051f2630SLeon Romanovsky 	if (MLX5_CAP_GEN(mdev, cd))
688051f2630SLeon Romanovsky 		props->device_cap_flags |= IB_DEVICE_CROSS_CHANNEL;
689051f2630SLeon Romanovsky 
690eff901d3SEli Cohen 	if (!mlx5_core_is_pf(mdev))
691eff901d3SEli Cohen 		props->device_cap_flags |= IB_DEVICE_VIRTUAL_FUNCTION;
692eff901d3SEli Cohen 
69331f69a82SYishai Hadas 	if (mlx5_ib_port_link_layer(ibdev, 1) ==
69431f69a82SYishai Hadas 	    IB_LINK_LAYER_ETHERNET) {
69531f69a82SYishai Hadas 		props->rss_caps.max_rwq_indirection_tables =
69631f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt);
69731f69a82SYishai Hadas 		props->rss_caps.max_rwq_indirection_table_size =
69831f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt_size);
69931f69a82SYishai Hadas 		props->rss_caps.supported_qpts = 1 << IB_QPT_RAW_PACKET;
70031f69a82SYishai Hadas 		props->max_wq_type_rq =
70131f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rq);
70231f69a82SYishai Hadas 	}
70331f69a82SYishai Hadas 
7047e43a2a5SBodong Wang 	if (field_avail(typeof(resp), cqe_comp_caps, uhw->outlen)) {
7057e43a2a5SBodong Wang 		resp.cqe_comp_caps.max_num =
7067e43a2a5SBodong Wang 			MLX5_CAP_GEN(dev->mdev, cqe_compression) ?
7077e43a2a5SBodong Wang 			MLX5_CAP_GEN(dev->mdev, cqe_compression_max_num) : 0;
7087e43a2a5SBodong Wang 		resp.cqe_comp_caps.supported_format =
7097e43a2a5SBodong Wang 			MLX5_IB_CQE_RES_FORMAT_HASH |
7107e43a2a5SBodong Wang 			MLX5_IB_CQE_RES_FORMAT_CSUM;
7117e43a2a5SBodong Wang 		resp.response_length += sizeof(resp.cqe_comp_caps);
7127e43a2a5SBodong Wang 	}
7137e43a2a5SBodong Wang 
714d949167dSBodong Wang 	if (field_avail(typeof(resp), packet_pacing_caps, uhw->outlen)) {
715d949167dSBodong Wang 		if (MLX5_CAP_QOS(mdev, packet_pacing) &&
716d949167dSBodong Wang 		    MLX5_CAP_GEN(mdev, qos)) {
717d949167dSBodong Wang 			resp.packet_pacing_caps.qp_rate_limit_max =
718d949167dSBodong Wang 				MLX5_CAP_QOS(mdev, packet_pacing_max_rate);
719d949167dSBodong Wang 			resp.packet_pacing_caps.qp_rate_limit_min =
720d949167dSBodong Wang 				MLX5_CAP_QOS(mdev, packet_pacing_min_rate);
721d949167dSBodong Wang 			resp.packet_pacing_caps.supported_qpts |=
722d949167dSBodong Wang 				1 << IB_QPT_RAW_PACKET;
723d949167dSBodong Wang 		}
724d949167dSBodong Wang 		resp.response_length += sizeof(resp.packet_pacing_caps);
725d949167dSBodong Wang 	}
726d949167dSBodong Wang 
7279f885201SLeon Romanovsky 	if (field_avail(typeof(resp), mlx5_ib_support_multi_pkt_send_wqes,
7289f885201SLeon Romanovsky 			uhw->outlen)) {
7299f885201SLeon Romanovsky 		resp.mlx5_ib_support_multi_pkt_send_wqes =
7309f885201SLeon Romanovsky 			MLX5_CAP_ETH(mdev, multi_pkt_send_wqe);
7319f885201SLeon Romanovsky 		resp.response_length +=
7329f885201SLeon Romanovsky 			sizeof(resp.mlx5_ib_support_multi_pkt_send_wqes);
7339f885201SLeon Romanovsky 	}
7349f885201SLeon Romanovsky 
7359f885201SLeon Romanovsky 	if (field_avail(typeof(resp), reserved, uhw->outlen))
7369f885201SLeon Romanovsky 		resp.response_length += sizeof(resp.reserved);
7379f885201SLeon Romanovsky 
738402ca536SBodong Wang 	if (uhw->outlen) {
739402ca536SBodong Wang 		err = ib_copy_to_udata(uhw, &resp, resp.response_length);
740402ca536SBodong Wang 
741402ca536SBodong Wang 		if (err)
742402ca536SBodong Wang 			return err;
743402ca536SBodong Wang 	}
744402ca536SBodong Wang 
7451b5daf11SMajd Dibbiny 	return 0;
7461b5daf11SMajd Dibbiny }
747e126ba97SEli Cohen 
7481b5daf11SMajd Dibbiny enum mlx5_ib_width {
7491b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_1X	= 1 << 0,
7501b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_2X	= 1 << 1,
7511b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_4X	= 1 << 2,
7521b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_8X	= 1 << 3,
7531b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_12X	= 1 << 4
7541b5daf11SMajd Dibbiny };
7551b5daf11SMajd Dibbiny 
7561b5daf11SMajd Dibbiny static int translate_active_width(struct ib_device *ibdev, u8 active_width,
7571b5daf11SMajd Dibbiny 				  u8 *ib_width)
7581b5daf11SMajd Dibbiny {
7591b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
7601b5daf11SMajd Dibbiny 	int err = 0;
7611b5daf11SMajd Dibbiny 
7621b5daf11SMajd Dibbiny 	if (active_width & MLX5_IB_WIDTH_1X) {
7631b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_1X;
7641b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_2X) {
7651b5daf11SMajd Dibbiny 		mlx5_ib_dbg(dev, "active_width %d is not supported by IB spec\n",
7661b5daf11SMajd Dibbiny 			    (int)active_width);
7671b5daf11SMajd Dibbiny 		err = -EINVAL;
7681b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_4X) {
7691b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_4X;
7701b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_8X) {
7711b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_8X;
7721b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_12X) {
7731b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_12X;
7741b5daf11SMajd Dibbiny 	} else {
7751b5daf11SMajd Dibbiny 		mlx5_ib_dbg(dev, "Invalid active_width %d\n",
7761b5daf11SMajd Dibbiny 			    (int)active_width);
7771b5daf11SMajd Dibbiny 		err = -EINVAL;
7781b5daf11SMajd Dibbiny 	}
7791b5daf11SMajd Dibbiny 
7801b5daf11SMajd Dibbiny 	return err;
7811b5daf11SMajd Dibbiny }
7821b5daf11SMajd Dibbiny 
7831b5daf11SMajd Dibbiny static int mlx5_mtu_to_ib_mtu(int mtu)
7841b5daf11SMajd Dibbiny {
7851b5daf11SMajd Dibbiny 	switch (mtu) {
7861b5daf11SMajd Dibbiny 	case 256: return 1;
7871b5daf11SMajd Dibbiny 	case 512: return 2;
7881b5daf11SMajd Dibbiny 	case 1024: return 3;
7891b5daf11SMajd Dibbiny 	case 2048: return 4;
7901b5daf11SMajd Dibbiny 	case 4096: return 5;
7911b5daf11SMajd Dibbiny 	default:
7921b5daf11SMajd Dibbiny 		pr_warn("invalid mtu\n");
7931b5daf11SMajd Dibbiny 		return -1;
7941b5daf11SMajd Dibbiny 	}
7951b5daf11SMajd Dibbiny }
7961b5daf11SMajd Dibbiny 
7971b5daf11SMajd Dibbiny enum ib_max_vl_num {
7981b5daf11SMajd Dibbiny 	__IB_MAX_VL_0		= 1,
7991b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_1		= 2,
8001b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_3		= 3,
8011b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_7		= 4,
8021b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_14	= 5,
8031b5daf11SMajd Dibbiny };
8041b5daf11SMajd Dibbiny 
8051b5daf11SMajd Dibbiny enum mlx5_vl_hw_cap {
8061b5daf11SMajd Dibbiny 	MLX5_VL_HW_0	= 1,
8071b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_1	= 2,
8081b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_2	= 3,
8091b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_3	= 4,
8101b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_4	= 5,
8111b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_5	= 6,
8121b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_6	= 7,
8131b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_7	= 8,
8141b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_14	= 15
8151b5daf11SMajd Dibbiny };
8161b5daf11SMajd Dibbiny 
8171b5daf11SMajd Dibbiny static int translate_max_vl_num(struct ib_device *ibdev, u8 vl_hw_cap,
8181b5daf11SMajd Dibbiny 				u8 *max_vl_num)
8191b5daf11SMajd Dibbiny {
8201b5daf11SMajd Dibbiny 	switch (vl_hw_cap) {
8211b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0:
8221b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0;
8231b5daf11SMajd Dibbiny 		break;
8241b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_1:
8251b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_1;
8261b5daf11SMajd Dibbiny 		break;
8271b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_3:
8281b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_3;
8291b5daf11SMajd Dibbiny 		break;
8301b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_7:
8311b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_7;
8321b5daf11SMajd Dibbiny 		break;
8331b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_14:
8341b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_14;
8351b5daf11SMajd Dibbiny 		break;
8361b5daf11SMajd Dibbiny 
8371b5daf11SMajd Dibbiny 	default:
8381b5daf11SMajd Dibbiny 		return -EINVAL;
8391b5daf11SMajd Dibbiny 	}
8401b5daf11SMajd Dibbiny 
8411b5daf11SMajd Dibbiny 	return 0;
8421b5daf11SMajd Dibbiny }
8431b5daf11SMajd Dibbiny 
8441b5daf11SMajd Dibbiny static int mlx5_query_hca_port(struct ib_device *ibdev, u8 port,
8451b5daf11SMajd Dibbiny 			       struct ib_port_attr *props)
8461b5daf11SMajd Dibbiny {
8471b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
8481b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
8491b5daf11SMajd Dibbiny 	struct mlx5_hca_vport_context *rep;
850046339eaSSaeed Mahameed 	u16 max_mtu;
851046339eaSSaeed Mahameed 	u16 oper_mtu;
8521b5daf11SMajd Dibbiny 	int err;
8531b5daf11SMajd Dibbiny 	u8 ib_link_width_oper;
8541b5daf11SMajd Dibbiny 	u8 vl_hw_cap;
8551b5daf11SMajd Dibbiny 
8561b5daf11SMajd Dibbiny 	rep = kzalloc(sizeof(*rep), GFP_KERNEL);
8571b5daf11SMajd Dibbiny 	if (!rep) {
8581b5daf11SMajd Dibbiny 		err = -ENOMEM;
8591b5daf11SMajd Dibbiny 		goto out;
8601b5daf11SMajd Dibbiny 	}
8611b5daf11SMajd Dibbiny 
862c4550c63SOr Gerlitz 	/* props being zeroed by the caller, avoid zeroing it here */
8631b5daf11SMajd Dibbiny 
8641b5daf11SMajd Dibbiny 	err = mlx5_query_hca_vport_context(mdev, 0, port, 0, rep);
8651b5daf11SMajd Dibbiny 	if (err)
8661b5daf11SMajd Dibbiny 		goto out;
8671b5daf11SMajd Dibbiny 
8681b5daf11SMajd Dibbiny 	props->lid		= rep->lid;
8691b5daf11SMajd Dibbiny 	props->lmc		= rep->lmc;
8701b5daf11SMajd Dibbiny 	props->sm_lid		= rep->sm_lid;
8711b5daf11SMajd Dibbiny 	props->sm_sl		= rep->sm_sl;
8721b5daf11SMajd Dibbiny 	props->state		= rep->vport_state;
8731b5daf11SMajd Dibbiny 	props->phys_state	= rep->port_physical_state;
8741b5daf11SMajd Dibbiny 	props->port_cap_flags	= rep->cap_mask1;
8751b5daf11SMajd Dibbiny 	props->gid_tbl_len	= mlx5_get_gid_table_len(MLX5_CAP_GEN(mdev, gid_table_size));
8761b5daf11SMajd Dibbiny 	props->max_msg_sz	= 1 << MLX5_CAP_GEN(mdev, log_max_msg);
8771b5daf11SMajd Dibbiny 	props->pkey_tbl_len	= mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, pkey_table_size));
8781b5daf11SMajd Dibbiny 	props->bad_pkey_cntr	= rep->pkey_violation_counter;
8791b5daf11SMajd Dibbiny 	props->qkey_viol_cntr	= rep->qkey_violation_counter;
8801b5daf11SMajd Dibbiny 	props->subnet_timeout	= rep->subnet_timeout;
8811b5daf11SMajd Dibbiny 	props->init_type_reply	= rep->init_type_reply;
882eff901d3SEli Cohen 	props->grh_required	= rep->grh_required;
8831b5daf11SMajd Dibbiny 
8841b5daf11SMajd Dibbiny 	err = mlx5_query_port_link_width_oper(mdev, &ib_link_width_oper, port);
8851b5daf11SMajd Dibbiny 	if (err)
8861b5daf11SMajd Dibbiny 		goto out;
8871b5daf11SMajd Dibbiny 
8881b5daf11SMajd Dibbiny 	err = translate_active_width(ibdev, ib_link_width_oper,
8891b5daf11SMajd Dibbiny 				     &props->active_width);
8901b5daf11SMajd Dibbiny 	if (err)
8911b5daf11SMajd Dibbiny 		goto out;
892d5beb7f2SNoa Osherovich 	err = mlx5_query_port_ib_proto_oper(mdev, &props->active_speed, port);
8931b5daf11SMajd Dibbiny 	if (err)
8941b5daf11SMajd Dibbiny 		goto out;
8951b5daf11SMajd Dibbiny 
896facc9699SSaeed Mahameed 	mlx5_query_port_max_mtu(mdev, &max_mtu, port);
8971b5daf11SMajd Dibbiny 
8981b5daf11SMajd Dibbiny 	props->max_mtu = mlx5_mtu_to_ib_mtu(max_mtu);
8991b5daf11SMajd Dibbiny 
900facc9699SSaeed Mahameed 	mlx5_query_port_oper_mtu(mdev, &oper_mtu, port);
9011b5daf11SMajd Dibbiny 
9021b5daf11SMajd Dibbiny 	props->active_mtu = mlx5_mtu_to_ib_mtu(oper_mtu);
9031b5daf11SMajd Dibbiny 
9041b5daf11SMajd Dibbiny 	err = mlx5_query_port_vl_hw_cap(mdev, &vl_hw_cap, port);
9051b5daf11SMajd Dibbiny 	if (err)
9061b5daf11SMajd Dibbiny 		goto out;
9071b5daf11SMajd Dibbiny 
9081b5daf11SMajd Dibbiny 	err = translate_max_vl_num(ibdev, vl_hw_cap,
9091b5daf11SMajd Dibbiny 				   &props->max_vl_num);
9101b5daf11SMajd Dibbiny out:
9111b5daf11SMajd Dibbiny 	kfree(rep);
912e126ba97SEli Cohen 	return err;
913e126ba97SEli Cohen }
914e126ba97SEli Cohen 
915e126ba97SEli Cohen int mlx5_ib_query_port(struct ib_device *ibdev, u8 port,
916e126ba97SEli Cohen 		       struct ib_port_attr *props)
917e126ba97SEli Cohen {
9181b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
9191b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
9201b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_port(ibdev, port, props);
921e126ba97SEli Cohen 
9221b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
9231b5daf11SMajd Dibbiny 		return mlx5_query_hca_port(ibdev, port, props);
9241b5daf11SMajd Dibbiny 
9253f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
9263f89a643SAchiad Shochat 		return mlx5_query_port_roce(ibdev, port, props);
9273f89a643SAchiad Shochat 
9281b5daf11SMajd Dibbiny 	default:
929e126ba97SEli Cohen 		return -EINVAL;
930e126ba97SEli Cohen 	}
931e126ba97SEli Cohen }
932e126ba97SEli Cohen 
933e126ba97SEli Cohen static int mlx5_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
934e126ba97SEli Cohen 			     union ib_gid *gid)
935e126ba97SEli Cohen {
9361b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
9371b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
938e126ba97SEli Cohen 
9391b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
9401b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
9411b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_gids(ibdev, port, index, gid);
942e126ba97SEli Cohen 
9431b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
9441b5daf11SMajd Dibbiny 		return mlx5_query_hca_vport_gid(mdev, 0, port, 0, index, gid);
945e126ba97SEli Cohen 
9461b5daf11SMajd Dibbiny 	default:
9471b5daf11SMajd Dibbiny 		return -EINVAL;
9481b5daf11SMajd Dibbiny 	}
949e126ba97SEli Cohen 
950e126ba97SEli Cohen }
951e126ba97SEli Cohen 
952e126ba97SEli Cohen static int mlx5_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
953e126ba97SEli Cohen 			      u16 *pkey)
954e126ba97SEli Cohen {
9551b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
9561b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
957e126ba97SEli Cohen 
9581b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
9591b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
9601b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_pkey(ibdev, port, index, pkey);
961e126ba97SEli Cohen 
9621b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
9631b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
9641b5daf11SMajd Dibbiny 		return mlx5_query_hca_vport_pkey(mdev, 0, port,  0, index,
9651b5daf11SMajd Dibbiny 						 pkey);
9661b5daf11SMajd Dibbiny 	default:
9671b5daf11SMajd Dibbiny 		return -EINVAL;
968e126ba97SEli Cohen 	}
9691b5daf11SMajd Dibbiny }
970e126ba97SEli Cohen 
971e126ba97SEli Cohen static int mlx5_ib_modify_device(struct ib_device *ibdev, int mask,
972e126ba97SEli Cohen 				 struct ib_device_modify *props)
973e126ba97SEli Cohen {
974e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
975e126ba97SEli Cohen 	struct mlx5_reg_node_desc in;
976e126ba97SEli Cohen 	struct mlx5_reg_node_desc out;
977e126ba97SEli Cohen 	int err;
978e126ba97SEli Cohen 
979e126ba97SEli Cohen 	if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
980e126ba97SEli Cohen 		return -EOPNOTSUPP;
981e126ba97SEli Cohen 
982e126ba97SEli Cohen 	if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
983e126ba97SEli Cohen 		return 0;
984e126ba97SEli Cohen 
985e126ba97SEli Cohen 	/*
986e126ba97SEli Cohen 	 * If possible, pass node desc to FW, so it can generate
987e126ba97SEli Cohen 	 * a 144 trap.  If cmd fails, just ignore.
988e126ba97SEli Cohen 	 */
989bd99fdeaSYuval Shaia 	memcpy(&in, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
9909603b61dSJack Morgenstein 	err = mlx5_core_access_reg(dev->mdev, &in, sizeof(in), &out,
991e126ba97SEli Cohen 				   sizeof(out), MLX5_REG_NODE_DESC, 0, 1);
992e126ba97SEli Cohen 	if (err)
993e126ba97SEli Cohen 		return err;
994e126ba97SEli Cohen 
995bd99fdeaSYuval Shaia 	memcpy(ibdev->node_desc, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
996e126ba97SEli Cohen 
997e126ba97SEli Cohen 	return err;
998e126ba97SEli Cohen }
999e126ba97SEli Cohen 
1000cdbe33d0SEli Cohen static int set_port_caps_atomic(struct mlx5_ib_dev *dev, u8 port_num, u32 mask,
1001cdbe33d0SEli Cohen 				u32 value)
1002cdbe33d0SEli Cohen {
1003cdbe33d0SEli Cohen 	struct mlx5_hca_vport_context ctx = {};
1004cdbe33d0SEli Cohen 	int err;
1005cdbe33d0SEli Cohen 
1006cdbe33d0SEli Cohen 	err = mlx5_query_hca_vport_context(dev->mdev, 0,
1007cdbe33d0SEli Cohen 					   port_num, 0, &ctx);
1008cdbe33d0SEli Cohen 	if (err)
1009cdbe33d0SEli Cohen 		return err;
1010cdbe33d0SEli Cohen 
1011cdbe33d0SEli Cohen 	if (~ctx.cap_mask1_perm & mask) {
1012cdbe33d0SEli Cohen 		mlx5_ib_warn(dev, "trying to change bitmask 0x%X but change supported 0x%X\n",
1013cdbe33d0SEli Cohen 			     mask, ctx.cap_mask1_perm);
1014cdbe33d0SEli Cohen 		return -EINVAL;
1015cdbe33d0SEli Cohen 	}
1016cdbe33d0SEli Cohen 
1017cdbe33d0SEli Cohen 	ctx.cap_mask1 = value;
1018cdbe33d0SEli Cohen 	ctx.cap_mask1_perm = mask;
1019cdbe33d0SEli Cohen 	err = mlx5_core_modify_hca_vport_context(dev->mdev, 0,
1020cdbe33d0SEli Cohen 						 port_num, 0, &ctx);
1021cdbe33d0SEli Cohen 
1022cdbe33d0SEli Cohen 	return err;
1023cdbe33d0SEli Cohen }
1024cdbe33d0SEli Cohen 
1025e126ba97SEli Cohen static int mlx5_ib_modify_port(struct ib_device *ibdev, u8 port, int mask,
1026e126ba97SEli Cohen 			       struct ib_port_modify *props)
1027e126ba97SEli Cohen {
1028e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1029e126ba97SEli Cohen 	struct ib_port_attr attr;
1030e126ba97SEli Cohen 	u32 tmp;
1031e126ba97SEli Cohen 	int err;
1032cdbe33d0SEli Cohen 	u32 change_mask;
1033cdbe33d0SEli Cohen 	u32 value;
1034cdbe33d0SEli Cohen 	bool is_ib = (mlx5_ib_port_link_layer(ibdev, port) ==
1035cdbe33d0SEli Cohen 		      IB_LINK_LAYER_INFINIBAND);
1036cdbe33d0SEli Cohen 
1037cdbe33d0SEli Cohen 	if (MLX5_CAP_GEN(dev->mdev, ib_virt) && is_ib) {
1038cdbe33d0SEli Cohen 		change_mask = props->clr_port_cap_mask | props->set_port_cap_mask;
1039cdbe33d0SEli Cohen 		value = ~props->clr_port_cap_mask | props->set_port_cap_mask;
1040cdbe33d0SEli Cohen 		return set_port_caps_atomic(dev, port, change_mask, value);
1041cdbe33d0SEli Cohen 	}
1042e126ba97SEli Cohen 
1043e126ba97SEli Cohen 	mutex_lock(&dev->cap_mask_mutex);
1044e126ba97SEli Cohen 
1045c4550c63SOr Gerlitz 	err = ib_query_port(ibdev, port, &attr);
1046e126ba97SEli Cohen 	if (err)
1047e126ba97SEli Cohen 		goto out;
1048e126ba97SEli Cohen 
1049e126ba97SEli Cohen 	tmp = (attr.port_cap_flags | props->set_port_cap_mask) &
1050e126ba97SEli Cohen 		~props->clr_port_cap_mask;
1051e126ba97SEli Cohen 
10529603b61dSJack Morgenstein 	err = mlx5_set_port_caps(dev->mdev, port, tmp);
1053e126ba97SEli Cohen 
1054e126ba97SEli Cohen out:
1055e126ba97SEli Cohen 	mutex_unlock(&dev->cap_mask_mutex);
1056e126ba97SEli Cohen 	return err;
1057e126ba97SEli Cohen }
1058e126ba97SEli Cohen 
105930aa60b3SEli Cohen static void print_lib_caps(struct mlx5_ib_dev *dev, u64 caps)
106030aa60b3SEli Cohen {
106130aa60b3SEli Cohen 	mlx5_ib_dbg(dev, "MLX5_LIB_CAP_4K_UAR = %s\n",
106230aa60b3SEli Cohen 		    caps & MLX5_LIB_CAP_4K_UAR ? "y" : "n");
106330aa60b3SEli Cohen }
106430aa60b3SEli Cohen 
1065b037c29aSEli Cohen static int calc_total_bfregs(struct mlx5_ib_dev *dev, bool lib_uar_4k,
1066b037c29aSEli Cohen 			     struct mlx5_ib_alloc_ucontext_req_v2 *req,
1067b037c29aSEli Cohen 			     u32 *num_sys_pages)
1068b037c29aSEli Cohen {
1069b037c29aSEli Cohen 	int uars_per_sys_page;
1070b037c29aSEli Cohen 	int bfregs_per_sys_page;
1071b037c29aSEli Cohen 	int ref_bfregs = req->total_num_bfregs;
1072b037c29aSEli Cohen 
1073b037c29aSEli Cohen 	if (req->total_num_bfregs == 0)
1074b037c29aSEli Cohen 		return -EINVAL;
1075b037c29aSEli Cohen 
1076b037c29aSEli Cohen 	BUILD_BUG_ON(MLX5_MAX_BFREGS % MLX5_NON_FP_BFREGS_IN_PAGE);
1077b037c29aSEli Cohen 	BUILD_BUG_ON(MLX5_MAX_BFREGS < MLX5_NON_FP_BFREGS_IN_PAGE);
1078b037c29aSEli Cohen 
1079b037c29aSEli Cohen 	if (req->total_num_bfregs > MLX5_MAX_BFREGS)
1080b037c29aSEli Cohen 		return -ENOMEM;
1081b037c29aSEli Cohen 
1082b037c29aSEli Cohen 	uars_per_sys_page = get_uars_per_sys_page(dev, lib_uar_4k);
1083b037c29aSEli Cohen 	bfregs_per_sys_page = uars_per_sys_page * MLX5_NON_FP_BFREGS_PER_UAR;
1084b037c29aSEli Cohen 	req->total_num_bfregs = ALIGN(req->total_num_bfregs, bfregs_per_sys_page);
1085b037c29aSEli Cohen 	*num_sys_pages = req->total_num_bfregs / bfregs_per_sys_page;
1086b037c29aSEli Cohen 
1087b037c29aSEli Cohen 	if (req->num_low_latency_bfregs > req->total_num_bfregs - 1)
1088b037c29aSEli Cohen 		return -EINVAL;
1089b037c29aSEli Cohen 
1090b037c29aSEli Cohen 	mlx5_ib_dbg(dev, "uar_4k: fw support %s, lib support %s, user requested %d bfregs, alloated %d, using %d sys pages\n",
1091b037c29aSEli Cohen 		    MLX5_CAP_GEN(dev->mdev, uar_4k) ? "yes" : "no",
1092b037c29aSEli Cohen 		    lib_uar_4k ? "yes" : "no", ref_bfregs,
1093b037c29aSEli Cohen 		    req->total_num_bfregs, *num_sys_pages);
1094b037c29aSEli Cohen 
1095b037c29aSEli Cohen 	return 0;
1096b037c29aSEli Cohen }
1097b037c29aSEli Cohen 
1098b037c29aSEli Cohen static int allocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
1099b037c29aSEli Cohen {
1100b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1101b037c29aSEli Cohen 	int err;
1102b037c29aSEli Cohen 	int i;
1103b037c29aSEli Cohen 
1104b037c29aSEli Cohen 	bfregi = &context->bfregi;
1105b037c29aSEli Cohen 	for (i = 0; i < bfregi->num_sys_pages; i++) {
1106b037c29aSEli Cohen 		err = mlx5_cmd_alloc_uar(dev->mdev, &bfregi->sys_pages[i]);
1107b037c29aSEli Cohen 		if (err)
1108b037c29aSEli Cohen 			goto error;
1109b037c29aSEli Cohen 
1110b037c29aSEli Cohen 		mlx5_ib_dbg(dev, "allocated uar %d\n", bfregi->sys_pages[i]);
1111b037c29aSEli Cohen 	}
1112b037c29aSEli Cohen 	return 0;
1113b037c29aSEli Cohen 
1114b037c29aSEli Cohen error:
1115b037c29aSEli Cohen 	for (--i; i >= 0; i--)
1116b037c29aSEli Cohen 		if (mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]))
1117b037c29aSEli Cohen 			mlx5_ib_warn(dev, "failed to free uar %d\n", i);
1118b037c29aSEli Cohen 
1119b037c29aSEli Cohen 	return err;
1120b037c29aSEli Cohen }
1121b037c29aSEli Cohen 
1122b037c29aSEli Cohen static int deallocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
1123b037c29aSEli Cohen {
1124b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1125b037c29aSEli Cohen 	int err;
1126b037c29aSEli Cohen 	int i;
1127b037c29aSEli Cohen 
1128b037c29aSEli Cohen 	bfregi = &context->bfregi;
1129b037c29aSEli Cohen 	for (i = 0; i < bfregi->num_sys_pages; i++) {
1130b037c29aSEli Cohen 		err = mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]);
1131b037c29aSEli Cohen 		if (err) {
1132b037c29aSEli Cohen 			mlx5_ib_warn(dev, "failed to free uar %d\n", i);
1133b037c29aSEli Cohen 			return err;
1134b037c29aSEli Cohen 		}
1135b037c29aSEli Cohen 	}
1136b037c29aSEli Cohen 	return 0;
1137b037c29aSEli Cohen }
1138b037c29aSEli Cohen 
1139e126ba97SEli Cohen static struct ib_ucontext *mlx5_ib_alloc_ucontext(struct ib_device *ibdev,
1140e126ba97SEli Cohen 						  struct ib_udata *udata)
1141e126ba97SEli Cohen {
1142e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1143b368d7cbSMatan Barak 	struct mlx5_ib_alloc_ucontext_req_v2 req = {};
1144b368d7cbSMatan Barak 	struct mlx5_ib_alloc_ucontext_resp resp = {};
1145e126ba97SEli Cohen 	struct mlx5_ib_ucontext *context;
11462f5ff264SEli Cohen 	struct mlx5_bfreg_info *bfregi;
114778c0f98cSEli Cohen 	int ver;
1148e126ba97SEli Cohen 	int err;
1149f241e749SJack Morgenstein 	size_t reqlen;
1150a168a41cSMajd Dibbiny 	size_t min_req_v2 = offsetof(struct mlx5_ib_alloc_ucontext_req_v2,
1151a168a41cSMajd Dibbiny 				     max_cqe_version);
1152b037c29aSEli Cohen 	bool lib_uar_4k;
1153e126ba97SEli Cohen 
1154e126ba97SEli Cohen 	if (!dev->ib_active)
1155e126ba97SEli Cohen 		return ERR_PTR(-EAGAIN);
1156e126ba97SEli Cohen 
1157dfbee859SHaggai Abramovsky 	if (udata->inlen < sizeof(struct ib_uverbs_cmd_hdr))
1158dfbee859SHaggai Abramovsky 		return ERR_PTR(-EINVAL);
1159dfbee859SHaggai Abramovsky 
116078c0f98cSEli Cohen 	reqlen = udata->inlen - sizeof(struct ib_uverbs_cmd_hdr);
116178c0f98cSEli Cohen 	if (reqlen == sizeof(struct mlx5_ib_alloc_ucontext_req))
116278c0f98cSEli Cohen 		ver = 0;
1163a168a41cSMajd Dibbiny 	else if (reqlen >= min_req_v2)
116478c0f98cSEli Cohen 		ver = 2;
116578c0f98cSEli Cohen 	else
116678c0f98cSEli Cohen 		return ERR_PTR(-EINVAL);
116778c0f98cSEli Cohen 
1168b368d7cbSMatan Barak 	err = ib_copy_from_udata(&req, udata, min(reqlen, sizeof(req)));
1169e126ba97SEli Cohen 	if (err)
1170e126ba97SEli Cohen 		return ERR_PTR(err);
1171e126ba97SEli Cohen 
1172b368d7cbSMatan Barak 	if (req.flags)
117378c0f98cSEli Cohen 		return ERR_PTR(-EINVAL);
117478c0f98cSEli Cohen 
1175f72300c5SHaggai Abramovsky 	if (req.comp_mask || req.reserved0 || req.reserved1 || req.reserved2)
1176b368d7cbSMatan Barak 		return ERR_PTR(-EOPNOTSUPP);
1177b368d7cbSMatan Barak 
11782f5ff264SEli Cohen 	req.total_num_bfregs = ALIGN(req.total_num_bfregs,
11792f5ff264SEli Cohen 				    MLX5_NON_FP_BFREGS_PER_UAR);
11802f5ff264SEli Cohen 	if (req.num_low_latency_bfregs > req.total_num_bfregs - 1)
1181e126ba97SEli Cohen 		return ERR_PTR(-EINVAL);
1182e126ba97SEli Cohen 
1183938fe83cSSaeed Mahameed 	resp.qp_tab_size = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp);
11842cc6ad5fSNoa Osherovich 	if (mlx5_core_is_pf(dev->mdev) && MLX5_CAP_GEN(dev->mdev, bf))
1185938fe83cSSaeed Mahameed 		resp.bf_reg_size = 1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size);
1186b47bd6eaSDaniel Jurgens 	resp.cache_line_size = cache_line_size();
1187938fe83cSSaeed Mahameed 	resp.max_sq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq);
1188938fe83cSSaeed Mahameed 	resp.max_rq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq);
1189938fe83cSSaeed Mahameed 	resp.max_send_wqebb = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1190938fe83cSSaeed Mahameed 	resp.max_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1191938fe83cSSaeed Mahameed 	resp.max_srq_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz);
1192f72300c5SHaggai Abramovsky 	resp.cqe_version = min_t(__u8,
1193f72300c5SHaggai Abramovsky 				 (__u8)MLX5_CAP_GEN(dev->mdev, cqe_version),
1194f72300c5SHaggai Abramovsky 				 req.max_cqe_version);
119530aa60b3SEli Cohen 	resp.log_uar_size = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
119630aa60b3SEli Cohen 				MLX5_ADAPTER_PAGE_SHIFT : PAGE_SHIFT;
119730aa60b3SEli Cohen 	resp.num_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
119830aa60b3SEli Cohen 					MLX5_CAP_GEN(dev->mdev, num_of_uars_per_page) : 1;
1199b368d7cbSMatan Barak 	resp.response_length = min(offsetof(typeof(resp), response_length) +
1200b368d7cbSMatan Barak 				   sizeof(resp.response_length), udata->outlen);
1201e126ba97SEli Cohen 
1202e126ba97SEli Cohen 	context = kzalloc(sizeof(*context), GFP_KERNEL);
1203e126ba97SEli Cohen 	if (!context)
1204e126ba97SEli Cohen 		return ERR_PTR(-ENOMEM);
1205e126ba97SEli Cohen 
120630aa60b3SEli Cohen 	lib_uar_4k = req.lib_caps & MLX5_LIB_CAP_4K_UAR;
12072f5ff264SEli Cohen 	bfregi = &context->bfregi;
1208b037c29aSEli Cohen 
1209b037c29aSEli Cohen 	/* updates req->total_num_bfregs */
1210b037c29aSEli Cohen 	err = calc_total_bfregs(dev, lib_uar_4k, &req, &bfregi->num_sys_pages);
1211b037c29aSEli Cohen 	if (err)
1212b037c29aSEli Cohen 		goto out_ctx;
1213b037c29aSEli Cohen 
12142f5ff264SEli Cohen 	mutex_init(&bfregi->lock);
1215b037c29aSEli Cohen 	bfregi->lib_uar_4k = lib_uar_4k;
1216b037c29aSEli Cohen 	bfregi->count = kcalloc(req.total_num_bfregs, sizeof(*bfregi->count),
1217b037c29aSEli Cohen 				GFP_KERNEL);
1218b037c29aSEli Cohen 	if (!bfregi->count) {
1219e126ba97SEli Cohen 		err = -ENOMEM;
1220e126ba97SEli Cohen 		goto out_ctx;
1221e126ba97SEli Cohen 	}
1222e126ba97SEli Cohen 
1223b037c29aSEli Cohen 	bfregi->sys_pages = kcalloc(bfregi->num_sys_pages,
1224b037c29aSEli Cohen 				    sizeof(*bfregi->sys_pages),
1225e126ba97SEli Cohen 				    GFP_KERNEL);
1226b037c29aSEli Cohen 	if (!bfregi->sys_pages) {
1227e126ba97SEli Cohen 		err = -ENOMEM;
1228e126ba97SEli Cohen 		goto out_count;
1229e126ba97SEli Cohen 	}
1230e126ba97SEli Cohen 
1231b037c29aSEli Cohen 	err = allocate_uars(dev, context);
1232b037c29aSEli Cohen 	if (err)
1233b037c29aSEli Cohen 		goto out_sys_pages;
1234b037c29aSEli Cohen 
1235b4cfe447SHaggai Eran #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
1236b4cfe447SHaggai Eran 	context->ibucontext.invalidate_range = &mlx5_ib_invalidate_range;
1237b4cfe447SHaggai Eran #endif
1238b4cfe447SHaggai Eran 
12397d0cc6edSArtemy Kovalyov 	context->upd_xlt_page = __get_free_page(GFP_KERNEL);
12407d0cc6edSArtemy Kovalyov 	if (!context->upd_xlt_page) {
12417d0cc6edSArtemy Kovalyov 		err = -ENOMEM;
12427d0cc6edSArtemy Kovalyov 		goto out_uars;
12437d0cc6edSArtemy Kovalyov 	}
12447d0cc6edSArtemy Kovalyov 	mutex_init(&context->upd_xlt_page_mutex);
12457d0cc6edSArtemy Kovalyov 
1246146d2f1aSmajd@mellanox.com 	if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain)) {
1247146d2f1aSmajd@mellanox.com 		err = mlx5_core_alloc_transport_domain(dev->mdev,
1248146d2f1aSmajd@mellanox.com 						       &context->tdn);
1249146d2f1aSmajd@mellanox.com 		if (err)
12507d0cc6edSArtemy Kovalyov 			goto out_page;
1251146d2f1aSmajd@mellanox.com 	}
1252146d2f1aSmajd@mellanox.com 
12537c2344c3SMaor Gottlieb 	INIT_LIST_HEAD(&context->vma_private_list);
1254e126ba97SEli Cohen 	INIT_LIST_HEAD(&context->db_page_list);
1255e126ba97SEli Cohen 	mutex_init(&context->db_page_mutex);
1256e126ba97SEli Cohen 
12572f5ff264SEli Cohen 	resp.tot_bfregs = req.total_num_bfregs;
1258938fe83cSSaeed Mahameed 	resp.num_ports = MLX5_CAP_GEN(dev->mdev, num_ports);
1259b368d7cbSMatan Barak 
1260f72300c5SHaggai Abramovsky 	if (field_avail(typeof(resp), cqe_version, udata->outlen))
1261f72300c5SHaggai Abramovsky 		resp.response_length += sizeof(resp.cqe_version);
1262b368d7cbSMatan Barak 
1263402ca536SBodong Wang 	if (field_avail(typeof(resp), cmds_supp_uhw, udata->outlen)) {
12646ad279c5SMoni Shoua 		resp.cmds_supp_uhw |= MLX5_USER_CMDS_SUPP_UHW_QUERY_DEVICE |
12656ad279c5SMoni Shoua 				      MLX5_USER_CMDS_SUPP_UHW_CREATE_AH;
1266402ca536SBodong Wang 		resp.response_length += sizeof(resp.cmds_supp_uhw);
1267402ca536SBodong Wang 	}
1268402ca536SBodong Wang 
126978984898SOr Gerlitz 	if (field_avail(typeof(resp), eth_min_inline, udata->outlen)) {
127078984898SOr Gerlitz 		if (mlx5_ib_port_link_layer(ibdev, 1) == IB_LINK_LAYER_ETHERNET) {
127178984898SOr Gerlitz 			mlx5_query_min_inline(dev->mdev, &resp.eth_min_inline);
127278984898SOr Gerlitz 			resp.eth_min_inline++;
127378984898SOr Gerlitz 		}
127478984898SOr Gerlitz 		resp.response_length += sizeof(resp.eth_min_inline);
127578984898SOr Gerlitz 	}
127678984898SOr Gerlitz 
1277bc5c6eedSNoa Osherovich 	/*
1278bc5c6eedSNoa Osherovich 	 * We don't want to expose information from the PCI bar that is located
1279bc5c6eedSNoa Osherovich 	 * after 4096 bytes, so if the arch only supports larger pages, let's
1280bc5c6eedSNoa Osherovich 	 * pretend we don't support reading the HCA's core clock. This is also
1281bc5c6eedSNoa Osherovich 	 * forced by mmap function.
1282bc5c6eedSNoa Osherovich 	 */
1283de8d6e02SEli Cohen 	if (field_avail(typeof(resp), hca_core_clock_offset, udata->outlen)) {
1284de8d6e02SEli Cohen 		if (PAGE_SIZE <= 4096) {
1285b368d7cbSMatan Barak 			resp.comp_mask |=
1286b368d7cbSMatan Barak 				MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_CORE_CLOCK_OFFSET;
1287b368d7cbSMatan Barak 			resp.hca_core_clock_offset =
1288de8d6e02SEli Cohen 				offsetof(struct mlx5_init_seg, internal_timer_h) % PAGE_SIZE;
1289de8d6e02SEli Cohen 		}
1290f72300c5SHaggai Abramovsky 		resp.response_length += sizeof(resp.hca_core_clock_offset) +
1291402ca536SBodong Wang 					sizeof(resp.reserved2);
1292b368d7cbSMatan Barak 	}
1293b368d7cbSMatan Barak 
129430aa60b3SEli Cohen 	if (field_avail(typeof(resp), log_uar_size, udata->outlen))
129530aa60b3SEli Cohen 		resp.response_length += sizeof(resp.log_uar_size);
129630aa60b3SEli Cohen 
129730aa60b3SEli Cohen 	if (field_avail(typeof(resp), num_uars_per_page, udata->outlen))
129830aa60b3SEli Cohen 		resp.response_length += sizeof(resp.num_uars_per_page);
129930aa60b3SEli Cohen 
1300b368d7cbSMatan Barak 	err = ib_copy_to_udata(udata, &resp, resp.response_length);
1301e126ba97SEli Cohen 	if (err)
1302146d2f1aSmajd@mellanox.com 		goto out_td;
1303e126ba97SEli Cohen 
13042f5ff264SEli Cohen 	bfregi->ver = ver;
13052f5ff264SEli Cohen 	bfregi->num_low_latency_bfregs = req.num_low_latency_bfregs;
1306f72300c5SHaggai Abramovsky 	context->cqe_version = resp.cqe_version;
130730aa60b3SEli Cohen 	context->lib_caps = req.lib_caps;
130830aa60b3SEli Cohen 	print_lib_caps(dev, context->lib_caps);
1309f72300c5SHaggai Abramovsky 
1310e126ba97SEli Cohen 	return &context->ibucontext;
1311e126ba97SEli Cohen 
1312146d2f1aSmajd@mellanox.com out_td:
1313146d2f1aSmajd@mellanox.com 	if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1314146d2f1aSmajd@mellanox.com 		mlx5_core_dealloc_transport_domain(dev->mdev, context->tdn);
1315146d2f1aSmajd@mellanox.com 
13167d0cc6edSArtemy Kovalyov out_page:
13177d0cc6edSArtemy Kovalyov 	free_page(context->upd_xlt_page);
13187d0cc6edSArtemy Kovalyov 
1319e126ba97SEli Cohen out_uars:
1320b037c29aSEli Cohen 	deallocate_uars(dev, context);
1321b037c29aSEli Cohen 
1322b037c29aSEli Cohen out_sys_pages:
1323b037c29aSEli Cohen 	kfree(bfregi->sys_pages);
1324b037c29aSEli Cohen 
1325e126ba97SEli Cohen out_count:
13262f5ff264SEli Cohen 	kfree(bfregi->count);
1327e126ba97SEli Cohen 
1328e126ba97SEli Cohen out_ctx:
1329e126ba97SEli Cohen 	kfree(context);
1330b037c29aSEli Cohen 
1331e126ba97SEli Cohen 	return ERR_PTR(err);
1332e126ba97SEli Cohen }
1333e126ba97SEli Cohen 
1334e126ba97SEli Cohen static int mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
1335e126ba97SEli Cohen {
1336e126ba97SEli Cohen 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1337e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
1338b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1339e126ba97SEli Cohen 
1340b037c29aSEli Cohen 	bfregi = &context->bfregi;
1341146d2f1aSmajd@mellanox.com 	if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1342146d2f1aSmajd@mellanox.com 		mlx5_core_dealloc_transport_domain(dev->mdev, context->tdn);
1343146d2f1aSmajd@mellanox.com 
13447d0cc6edSArtemy Kovalyov 	free_page(context->upd_xlt_page);
1345b037c29aSEli Cohen 	deallocate_uars(dev, context);
1346b037c29aSEli Cohen 	kfree(bfregi->sys_pages);
13472f5ff264SEli Cohen 	kfree(bfregi->count);
1348e126ba97SEli Cohen 	kfree(context);
1349e126ba97SEli Cohen 
1350e126ba97SEli Cohen 	return 0;
1351e126ba97SEli Cohen }
1352e126ba97SEli Cohen 
1353b037c29aSEli Cohen static phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev,
1354b037c29aSEli Cohen 				 struct mlx5_bfreg_info *bfregi,
1355b037c29aSEli Cohen 				 int idx)
1356e126ba97SEli Cohen {
1357b037c29aSEli Cohen 	int fw_uars_per_page;
1358b037c29aSEli Cohen 
1359b037c29aSEli Cohen 	fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ? MLX5_UARS_IN_PAGE : 1;
1360b037c29aSEli Cohen 
1361b037c29aSEli Cohen 	return (pci_resource_start(dev->mdev->pdev, 0) >> PAGE_SHIFT) +
1362b037c29aSEli Cohen 			bfregi->sys_pages[idx] / fw_uars_per_page;
1363e126ba97SEli Cohen }
1364e126ba97SEli Cohen 
1365e126ba97SEli Cohen static int get_command(unsigned long offset)
1366e126ba97SEli Cohen {
1367e126ba97SEli Cohen 	return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK;
1368e126ba97SEli Cohen }
1369e126ba97SEli Cohen 
1370e126ba97SEli Cohen static int get_arg(unsigned long offset)
1371e126ba97SEli Cohen {
1372e126ba97SEli Cohen 	return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1);
1373e126ba97SEli Cohen }
1374e126ba97SEli Cohen 
1375e126ba97SEli Cohen static int get_index(unsigned long offset)
1376e126ba97SEli Cohen {
1377e126ba97SEli Cohen 	return get_arg(offset);
1378e126ba97SEli Cohen }
1379e126ba97SEli Cohen 
13807c2344c3SMaor Gottlieb static void  mlx5_ib_vma_open(struct vm_area_struct *area)
13817c2344c3SMaor Gottlieb {
13827c2344c3SMaor Gottlieb 	/* vma_open is called when a new VMA is created on top of our VMA.  This
13837c2344c3SMaor Gottlieb 	 * is done through either mremap flow or split_vma (usually due to
13847c2344c3SMaor Gottlieb 	 * mlock, madvise, munmap, etc.) We do not support a clone of the VMA,
13857c2344c3SMaor Gottlieb 	 * as this VMA is strongly hardware related.  Therefore we set the
13867c2344c3SMaor Gottlieb 	 * vm_ops of the newly created/cloned VMA to NULL, to prevent it from
13877c2344c3SMaor Gottlieb 	 * calling us again and trying to do incorrect actions.  We assume that
13887c2344c3SMaor Gottlieb 	 * the original VMA size is exactly a single page, and therefore all
13897c2344c3SMaor Gottlieb 	 * "splitting" operation will not happen to it.
13907c2344c3SMaor Gottlieb 	 */
13917c2344c3SMaor Gottlieb 	area->vm_ops = NULL;
13927c2344c3SMaor Gottlieb }
13937c2344c3SMaor Gottlieb 
13947c2344c3SMaor Gottlieb static void  mlx5_ib_vma_close(struct vm_area_struct *area)
13957c2344c3SMaor Gottlieb {
13967c2344c3SMaor Gottlieb 	struct mlx5_ib_vma_private_data *mlx5_ib_vma_priv_data;
13977c2344c3SMaor Gottlieb 
13987c2344c3SMaor Gottlieb 	/* It's guaranteed that all VMAs opened on a FD are closed before the
13997c2344c3SMaor Gottlieb 	 * file itself is closed, therefore no sync is needed with the regular
14007c2344c3SMaor Gottlieb 	 * closing flow. (e.g. mlx5 ib_dealloc_ucontext)
14017c2344c3SMaor Gottlieb 	 * However need a sync with accessing the vma as part of
14027c2344c3SMaor Gottlieb 	 * mlx5_ib_disassociate_ucontext.
14037c2344c3SMaor Gottlieb 	 * The close operation is usually called under mm->mmap_sem except when
14047c2344c3SMaor Gottlieb 	 * process is exiting.
14057c2344c3SMaor Gottlieb 	 * The exiting case is handled explicitly as part of
14067c2344c3SMaor Gottlieb 	 * mlx5_ib_disassociate_ucontext.
14077c2344c3SMaor Gottlieb 	 */
14087c2344c3SMaor Gottlieb 	mlx5_ib_vma_priv_data = (struct mlx5_ib_vma_private_data *)area->vm_private_data;
14097c2344c3SMaor Gottlieb 
14107c2344c3SMaor Gottlieb 	/* setting the vma context pointer to null in the mlx5_ib driver's
14117c2344c3SMaor Gottlieb 	 * private data, to protect a race condition in
14127c2344c3SMaor Gottlieb 	 * mlx5_ib_disassociate_ucontext().
14137c2344c3SMaor Gottlieb 	 */
14147c2344c3SMaor Gottlieb 	mlx5_ib_vma_priv_data->vma = NULL;
14157c2344c3SMaor Gottlieb 	list_del(&mlx5_ib_vma_priv_data->list);
14167c2344c3SMaor Gottlieb 	kfree(mlx5_ib_vma_priv_data);
14177c2344c3SMaor Gottlieb }
14187c2344c3SMaor Gottlieb 
14197c2344c3SMaor Gottlieb static const struct vm_operations_struct mlx5_ib_vm_ops = {
14207c2344c3SMaor Gottlieb 	.open = mlx5_ib_vma_open,
14217c2344c3SMaor Gottlieb 	.close = mlx5_ib_vma_close
14227c2344c3SMaor Gottlieb };
14237c2344c3SMaor Gottlieb 
14247c2344c3SMaor Gottlieb static int mlx5_ib_set_vma_data(struct vm_area_struct *vma,
14257c2344c3SMaor Gottlieb 				struct mlx5_ib_ucontext *ctx)
14267c2344c3SMaor Gottlieb {
14277c2344c3SMaor Gottlieb 	struct mlx5_ib_vma_private_data *vma_prv;
14287c2344c3SMaor Gottlieb 	struct list_head *vma_head = &ctx->vma_private_list;
14297c2344c3SMaor Gottlieb 
14307c2344c3SMaor Gottlieb 	vma_prv = kzalloc(sizeof(*vma_prv), GFP_KERNEL);
14317c2344c3SMaor Gottlieb 	if (!vma_prv)
14327c2344c3SMaor Gottlieb 		return -ENOMEM;
14337c2344c3SMaor Gottlieb 
14347c2344c3SMaor Gottlieb 	vma_prv->vma = vma;
14357c2344c3SMaor Gottlieb 	vma->vm_private_data = vma_prv;
14367c2344c3SMaor Gottlieb 	vma->vm_ops =  &mlx5_ib_vm_ops;
14377c2344c3SMaor Gottlieb 
14387c2344c3SMaor Gottlieb 	list_add(&vma_prv->list, vma_head);
14397c2344c3SMaor Gottlieb 
14407c2344c3SMaor Gottlieb 	return 0;
14417c2344c3SMaor Gottlieb }
14427c2344c3SMaor Gottlieb 
14437c2344c3SMaor Gottlieb static void mlx5_ib_disassociate_ucontext(struct ib_ucontext *ibcontext)
14447c2344c3SMaor Gottlieb {
14457c2344c3SMaor Gottlieb 	int ret;
14467c2344c3SMaor Gottlieb 	struct vm_area_struct *vma;
14477c2344c3SMaor Gottlieb 	struct mlx5_ib_vma_private_data *vma_private, *n;
14487c2344c3SMaor Gottlieb 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
14497c2344c3SMaor Gottlieb 	struct task_struct *owning_process  = NULL;
14507c2344c3SMaor Gottlieb 	struct mm_struct   *owning_mm       = NULL;
14517c2344c3SMaor Gottlieb 
14527c2344c3SMaor Gottlieb 	owning_process = get_pid_task(ibcontext->tgid, PIDTYPE_PID);
14537c2344c3SMaor Gottlieb 	if (!owning_process)
14547c2344c3SMaor Gottlieb 		return;
14557c2344c3SMaor Gottlieb 
14567c2344c3SMaor Gottlieb 	owning_mm = get_task_mm(owning_process);
14577c2344c3SMaor Gottlieb 	if (!owning_mm) {
14587c2344c3SMaor Gottlieb 		pr_info("no mm, disassociate ucontext is pending task termination\n");
14597c2344c3SMaor Gottlieb 		while (1) {
14607c2344c3SMaor Gottlieb 			put_task_struct(owning_process);
14617c2344c3SMaor Gottlieb 			usleep_range(1000, 2000);
14627c2344c3SMaor Gottlieb 			owning_process = get_pid_task(ibcontext->tgid,
14637c2344c3SMaor Gottlieb 						      PIDTYPE_PID);
14647c2344c3SMaor Gottlieb 			if (!owning_process ||
14657c2344c3SMaor Gottlieb 			    owning_process->state == TASK_DEAD) {
14667c2344c3SMaor Gottlieb 				pr_info("disassociate ucontext done, task was terminated\n");
14677c2344c3SMaor Gottlieb 				/* in case task was dead need to release the
14687c2344c3SMaor Gottlieb 				 * task struct.
14697c2344c3SMaor Gottlieb 				 */
14707c2344c3SMaor Gottlieb 				if (owning_process)
14717c2344c3SMaor Gottlieb 					put_task_struct(owning_process);
14727c2344c3SMaor Gottlieb 				return;
14737c2344c3SMaor Gottlieb 			}
14747c2344c3SMaor Gottlieb 		}
14757c2344c3SMaor Gottlieb 	}
14767c2344c3SMaor Gottlieb 
14777c2344c3SMaor Gottlieb 	/* need to protect from a race on closing the vma as part of
14787c2344c3SMaor Gottlieb 	 * mlx5_ib_vma_close.
14797c2344c3SMaor Gottlieb 	 */
14807c2344c3SMaor Gottlieb 	down_read(&owning_mm->mmap_sem);
14817c2344c3SMaor Gottlieb 	list_for_each_entry_safe(vma_private, n, &context->vma_private_list,
14827c2344c3SMaor Gottlieb 				 list) {
14837c2344c3SMaor Gottlieb 		vma = vma_private->vma;
14847c2344c3SMaor Gottlieb 		ret = zap_vma_ptes(vma, vma->vm_start,
14857c2344c3SMaor Gottlieb 				   PAGE_SIZE);
14867c2344c3SMaor Gottlieb 		WARN_ONCE(ret, "%s: zap_vma_ptes failed", __func__);
14877c2344c3SMaor Gottlieb 		/* context going to be destroyed, should
14887c2344c3SMaor Gottlieb 		 * not access ops any more.
14897c2344c3SMaor Gottlieb 		 */
14907c2344c3SMaor Gottlieb 		vma->vm_ops = NULL;
14917c2344c3SMaor Gottlieb 		list_del(&vma_private->list);
14927c2344c3SMaor Gottlieb 		kfree(vma_private);
14937c2344c3SMaor Gottlieb 	}
14947c2344c3SMaor Gottlieb 	up_read(&owning_mm->mmap_sem);
14957c2344c3SMaor Gottlieb 	mmput(owning_mm);
14967c2344c3SMaor Gottlieb 	put_task_struct(owning_process);
14977c2344c3SMaor Gottlieb }
14987c2344c3SMaor Gottlieb 
149937aa5c36SGuy Levi static inline char *mmap_cmd2str(enum mlx5_ib_mmap_cmd cmd)
1500e126ba97SEli Cohen {
150137aa5c36SGuy Levi 	switch (cmd) {
150237aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
150337aa5c36SGuy Levi 		return "WC";
1504e126ba97SEli Cohen 	case MLX5_IB_MMAP_REGULAR_PAGE:
150537aa5c36SGuy Levi 		return "best effort WC";
150637aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
150737aa5c36SGuy Levi 		return "NC";
150837aa5c36SGuy Levi 	default:
150937aa5c36SGuy Levi 		return NULL;
151037aa5c36SGuy Levi 	}
151137aa5c36SGuy Levi }
151237aa5c36SGuy Levi 
151337aa5c36SGuy Levi static int uar_mmap(struct mlx5_ib_dev *dev, enum mlx5_ib_mmap_cmd cmd,
15147c2344c3SMaor Gottlieb 		    struct vm_area_struct *vma,
15157c2344c3SMaor Gottlieb 		    struct mlx5_ib_ucontext *context)
151637aa5c36SGuy Levi {
15172f5ff264SEli Cohen 	struct mlx5_bfreg_info *bfregi = &context->bfregi;
151837aa5c36SGuy Levi 	int err;
151937aa5c36SGuy Levi 	unsigned long idx;
152037aa5c36SGuy Levi 	phys_addr_t pfn, pa;
152137aa5c36SGuy Levi 	pgprot_t prot;
1522b037c29aSEli Cohen 	int uars_per_page;
1523b037c29aSEli Cohen 
1524b037c29aSEli Cohen 	if (vma->vm_end - vma->vm_start != PAGE_SIZE)
1525b037c29aSEli Cohen 		return -EINVAL;
1526b037c29aSEli Cohen 
1527b037c29aSEli Cohen 	uars_per_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k);
1528b037c29aSEli Cohen 	idx = get_index(vma->vm_pgoff);
1529b037c29aSEli Cohen 	if (idx % uars_per_page ||
1530b037c29aSEli Cohen 	    idx * uars_per_page >= bfregi->num_sys_pages) {
1531b037c29aSEli Cohen 		mlx5_ib_warn(dev, "invalid uar index %lu\n", idx);
1532b037c29aSEli Cohen 		return -EINVAL;
1533b037c29aSEli Cohen 	}
153437aa5c36SGuy Levi 
153537aa5c36SGuy Levi 	switch (cmd) {
153637aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
153737aa5c36SGuy Levi /* Some architectures don't support WC memory */
153837aa5c36SGuy Levi #if defined(CONFIG_X86)
153937aa5c36SGuy Levi 		if (!pat_enabled())
154037aa5c36SGuy Levi 			return -EPERM;
154137aa5c36SGuy Levi #elif !(defined(CONFIG_PPC) || (defined(CONFIG_ARM) && defined(CONFIG_MMU)))
154237aa5c36SGuy Levi 			return -EPERM;
154337aa5c36SGuy Levi #endif
154437aa5c36SGuy Levi 	/* fall through */
154537aa5c36SGuy Levi 	case MLX5_IB_MMAP_REGULAR_PAGE:
154637aa5c36SGuy Levi 		/* For MLX5_IB_MMAP_REGULAR_PAGE do the best effort to get WC */
154737aa5c36SGuy Levi 		prot = pgprot_writecombine(vma->vm_page_prot);
154837aa5c36SGuy Levi 		break;
154937aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
155037aa5c36SGuy Levi 		prot = pgprot_noncached(vma->vm_page_prot);
155137aa5c36SGuy Levi 		break;
155237aa5c36SGuy Levi 	default:
155337aa5c36SGuy Levi 		return -EINVAL;
155437aa5c36SGuy Levi 	}
155537aa5c36SGuy Levi 
1556b037c29aSEli Cohen 	pfn = uar_index2pfn(dev, bfregi, idx);
155737aa5c36SGuy Levi 	mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn %pa\n", idx, &pfn);
1558e126ba97SEli Cohen 
155937aa5c36SGuy Levi 	vma->vm_page_prot = prot;
156037aa5c36SGuy Levi 	err = io_remap_pfn_range(vma, vma->vm_start, pfn,
156137aa5c36SGuy Levi 				 PAGE_SIZE, vma->vm_page_prot);
156237aa5c36SGuy Levi 	if (err) {
156337aa5c36SGuy Levi 		mlx5_ib_err(dev, "io_remap_pfn_range failed with error=%d, vm_start=0x%lx, pfn=%pa, mmap_cmd=%s\n",
156437aa5c36SGuy Levi 			    err, vma->vm_start, &pfn, mmap_cmd2str(cmd));
1565e126ba97SEli Cohen 		return -EAGAIN;
156637aa5c36SGuy Levi 	}
1567e126ba97SEli Cohen 
156837aa5c36SGuy Levi 	pa = pfn << PAGE_SHIFT;
156937aa5c36SGuy Levi 	mlx5_ib_dbg(dev, "mapped %s at 0x%lx, PA %pa\n", mmap_cmd2str(cmd),
157037aa5c36SGuy Levi 		    vma->vm_start, &pa);
157137aa5c36SGuy Levi 
15727c2344c3SMaor Gottlieb 	return mlx5_ib_set_vma_data(vma, context);
157337aa5c36SGuy Levi }
157437aa5c36SGuy Levi 
157537aa5c36SGuy Levi static int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma)
157637aa5c36SGuy Levi {
157737aa5c36SGuy Levi 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
157837aa5c36SGuy Levi 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
157937aa5c36SGuy Levi 	unsigned long command;
158037aa5c36SGuy Levi 	phys_addr_t pfn;
158137aa5c36SGuy Levi 
158237aa5c36SGuy Levi 	command = get_command(vma->vm_pgoff);
158337aa5c36SGuy Levi 	switch (command) {
158437aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
158537aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
158637aa5c36SGuy Levi 	case MLX5_IB_MMAP_REGULAR_PAGE:
15877c2344c3SMaor Gottlieb 		return uar_mmap(dev, command, vma, context);
1588e126ba97SEli Cohen 
1589e126ba97SEli Cohen 	case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES:
1590e126ba97SEli Cohen 		return -ENOSYS;
1591e126ba97SEli Cohen 
1592d69e3bcfSMatan Barak 	case MLX5_IB_MMAP_CORE_CLOCK:
1593d69e3bcfSMatan Barak 		if (vma->vm_end - vma->vm_start != PAGE_SIZE)
1594d69e3bcfSMatan Barak 			return -EINVAL;
1595d69e3bcfSMatan Barak 
15966cbac1e4SMatan Barak 		if (vma->vm_flags & VM_WRITE)
1597d69e3bcfSMatan Barak 			return -EPERM;
1598d69e3bcfSMatan Barak 
1599d69e3bcfSMatan Barak 		/* Don't expose to user-space information it shouldn't have */
1600d69e3bcfSMatan Barak 		if (PAGE_SIZE > 4096)
1601d69e3bcfSMatan Barak 			return -EOPNOTSUPP;
1602d69e3bcfSMatan Barak 
1603d69e3bcfSMatan Barak 		vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1604d69e3bcfSMatan Barak 		pfn = (dev->mdev->iseg_base +
1605d69e3bcfSMatan Barak 		       offsetof(struct mlx5_init_seg, internal_timer_h)) >>
1606d69e3bcfSMatan Barak 			PAGE_SHIFT;
1607d69e3bcfSMatan Barak 		if (io_remap_pfn_range(vma, vma->vm_start, pfn,
1608d69e3bcfSMatan Barak 				       PAGE_SIZE, vma->vm_page_prot))
1609d69e3bcfSMatan Barak 			return -EAGAIN;
1610d69e3bcfSMatan Barak 
1611d69e3bcfSMatan Barak 		mlx5_ib_dbg(dev, "mapped internal timer at 0x%lx, PA 0x%llx\n",
1612d69e3bcfSMatan Barak 			    vma->vm_start,
1613d69e3bcfSMatan Barak 			    (unsigned long long)pfn << PAGE_SHIFT);
1614d69e3bcfSMatan Barak 		break;
1615d69e3bcfSMatan Barak 
1616e126ba97SEli Cohen 	default:
1617e126ba97SEli Cohen 		return -EINVAL;
1618e126ba97SEli Cohen 	}
1619e126ba97SEli Cohen 
1620e126ba97SEli Cohen 	return 0;
1621e126ba97SEli Cohen }
1622e126ba97SEli Cohen 
1623e126ba97SEli Cohen static struct ib_pd *mlx5_ib_alloc_pd(struct ib_device *ibdev,
1624e126ba97SEli Cohen 				      struct ib_ucontext *context,
1625e126ba97SEli Cohen 				      struct ib_udata *udata)
1626e126ba97SEli Cohen {
1627e126ba97SEli Cohen 	struct mlx5_ib_alloc_pd_resp resp;
1628e126ba97SEli Cohen 	struct mlx5_ib_pd *pd;
1629e126ba97SEli Cohen 	int err;
1630e126ba97SEli Cohen 
1631e126ba97SEli Cohen 	pd = kmalloc(sizeof(*pd), GFP_KERNEL);
1632e126ba97SEli Cohen 	if (!pd)
1633e126ba97SEli Cohen 		return ERR_PTR(-ENOMEM);
1634e126ba97SEli Cohen 
16359603b61dSJack Morgenstein 	err = mlx5_core_alloc_pd(to_mdev(ibdev)->mdev, &pd->pdn);
1636e126ba97SEli Cohen 	if (err) {
1637e126ba97SEli Cohen 		kfree(pd);
1638e126ba97SEli Cohen 		return ERR_PTR(err);
1639e126ba97SEli Cohen 	}
1640e126ba97SEli Cohen 
1641e126ba97SEli Cohen 	if (context) {
1642e126ba97SEli Cohen 		resp.pdn = pd->pdn;
1643e126ba97SEli Cohen 		if (ib_copy_to_udata(udata, &resp, sizeof(resp))) {
16449603b61dSJack Morgenstein 			mlx5_core_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn);
1645e126ba97SEli Cohen 			kfree(pd);
1646e126ba97SEli Cohen 			return ERR_PTR(-EFAULT);
1647e126ba97SEli Cohen 		}
1648e126ba97SEli Cohen 	}
1649e126ba97SEli Cohen 
1650e126ba97SEli Cohen 	return &pd->ibpd;
1651e126ba97SEli Cohen }
1652e126ba97SEli Cohen 
1653e126ba97SEli Cohen static int mlx5_ib_dealloc_pd(struct ib_pd *pd)
1654e126ba97SEli Cohen {
1655e126ba97SEli Cohen 	struct mlx5_ib_dev *mdev = to_mdev(pd->device);
1656e126ba97SEli Cohen 	struct mlx5_ib_pd *mpd = to_mpd(pd);
1657e126ba97SEli Cohen 
16589603b61dSJack Morgenstein 	mlx5_core_dealloc_pd(mdev->mdev, mpd->pdn);
1659e126ba97SEli Cohen 	kfree(mpd);
1660e126ba97SEli Cohen 
1661e126ba97SEli Cohen 	return 0;
1662e126ba97SEli Cohen }
1663e126ba97SEli Cohen 
1664466fa6d2SMaor Gottlieb enum {
1665466fa6d2SMaor Gottlieb 	MATCH_CRITERIA_ENABLE_OUTER_BIT,
1666466fa6d2SMaor Gottlieb 	MATCH_CRITERIA_ENABLE_MISC_BIT,
1667466fa6d2SMaor Gottlieb 	MATCH_CRITERIA_ENABLE_INNER_BIT
1668466fa6d2SMaor Gottlieb };
1669038d2ef8SMaor Gottlieb 
1670466fa6d2SMaor Gottlieb #define HEADER_IS_ZERO(match_criteria, headers)			           \
1671466fa6d2SMaor Gottlieb 	!(memchr_inv(MLX5_ADDR_OF(fte_match_param, match_criteria, headers), \
1672466fa6d2SMaor Gottlieb 		    0, MLX5_FLD_SZ_BYTES(fte_match_param, headers)))       \
1673466fa6d2SMaor Gottlieb 
1674466fa6d2SMaor Gottlieb static u8 get_match_criteria_enable(u32 *match_criteria)
1675466fa6d2SMaor Gottlieb {
1676466fa6d2SMaor Gottlieb 	u8 match_criteria_enable;
1677466fa6d2SMaor Gottlieb 
1678466fa6d2SMaor Gottlieb 	match_criteria_enable =
1679466fa6d2SMaor Gottlieb 		(!HEADER_IS_ZERO(match_criteria, outer_headers)) <<
1680466fa6d2SMaor Gottlieb 		MATCH_CRITERIA_ENABLE_OUTER_BIT;
1681466fa6d2SMaor Gottlieb 	match_criteria_enable |=
1682466fa6d2SMaor Gottlieb 		(!HEADER_IS_ZERO(match_criteria, misc_parameters)) <<
1683466fa6d2SMaor Gottlieb 		MATCH_CRITERIA_ENABLE_MISC_BIT;
1684466fa6d2SMaor Gottlieb 	match_criteria_enable |=
1685466fa6d2SMaor Gottlieb 		(!HEADER_IS_ZERO(match_criteria, inner_headers)) <<
1686466fa6d2SMaor Gottlieb 		MATCH_CRITERIA_ENABLE_INNER_BIT;
1687466fa6d2SMaor Gottlieb 
1688466fa6d2SMaor Gottlieb 	return match_criteria_enable;
1689038d2ef8SMaor Gottlieb }
1690038d2ef8SMaor Gottlieb 
1691ca0d4753SMaor Gottlieb static void set_proto(void *outer_c, void *outer_v, u8 mask, u8 val)
1692ca0d4753SMaor Gottlieb {
1693ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_protocol, mask);
1694ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_protocol, val);
1695ca0d4753SMaor Gottlieb }
1696ca0d4753SMaor Gottlieb 
16972d1e697eSMoses Reuben static void set_flow_label(void *misc_c, void *misc_v, u8 mask, u8 val,
16982d1e697eSMoses Reuben 			   bool inner)
16992d1e697eSMoses Reuben {
17002d1e697eSMoses Reuben 	if (inner) {
17012d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
17022d1e697eSMoses Reuben 			 misc_c, inner_ipv6_flow_label, mask);
17032d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
17042d1e697eSMoses Reuben 			 misc_v, inner_ipv6_flow_label, val);
17052d1e697eSMoses Reuben 	} else {
17062d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
17072d1e697eSMoses Reuben 			 misc_c, outer_ipv6_flow_label, mask);
17082d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
17092d1e697eSMoses Reuben 			 misc_v, outer_ipv6_flow_label, val);
17102d1e697eSMoses Reuben 	}
17112d1e697eSMoses Reuben }
17122d1e697eSMoses Reuben 
1713ca0d4753SMaor Gottlieb static void set_tos(void *outer_c, void *outer_v, u8 mask, u8 val)
1714ca0d4753SMaor Gottlieb {
1715ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_ecn, mask);
1716ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_ecn, val);
1717ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_dscp, mask >> 2);
1718ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_dscp, val >> 2);
1719ca0d4753SMaor Gottlieb }
1720ca0d4753SMaor Gottlieb 
1721c47ac6aeSMaor Gottlieb #define LAST_ETH_FIELD vlan_tag
1722c47ac6aeSMaor Gottlieb #define LAST_IB_FIELD sl
1723ca0d4753SMaor Gottlieb #define LAST_IPV4_FIELD tos
1724466fa6d2SMaor Gottlieb #define LAST_IPV6_FIELD traffic_class
1725c47ac6aeSMaor Gottlieb #define LAST_TCP_UDP_FIELD src_port
1726ffb30d8fSMoses Reuben #define LAST_TUNNEL_FIELD tunnel_id
17272ac693f9SMoses Reuben #define LAST_FLOW_TAG_FIELD tag_id
1728c47ac6aeSMaor Gottlieb 
1729c47ac6aeSMaor Gottlieb /* Field is the last supported field */
1730c47ac6aeSMaor Gottlieb #define FIELDS_NOT_SUPPORTED(filter, field)\
1731c47ac6aeSMaor Gottlieb 	memchr_inv((void *)&filter.field  +\
1732c47ac6aeSMaor Gottlieb 		   sizeof(filter.field), 0,\
1733c47ac6aeSMaor Gottlieb 		   sizeof(filter) -\
1734c47ac6aeSMaor Gottlieb 		   offsetof(typeof(filter), field) -\
1735c47ac6aeSMaor Gottlieb 		   sizeof(filter.field))
1736c47ac6aeSMaor Gottlieb 
1737038d2ef8SMaor Gottlieb static int parse_flow_attr(u32 *match_c, u32 *match_v,
17382ac693f9SMoses Reuben 			   const union ib_flow_spec *ib_spec, u32 *tag_id)
1739038d2ef8SMaor Gottlieb {
1740466fa6d2SMaor Gottlieb 	void *misc_params_c = MLX5_ADDR_OF(fte_match_param, match_c,
1741466fa6d2SMaor Gottlieb 					   misc_parameters);
1742466fa6d2SMaor Gottlieb 	void *misc_params_v = MLX5_ADDR_OF(fte_match_param, match_v,
1743466fa6d2SMaor Gottlieb 					   misc_parameters);
17442d1e697eSMoses Reuben 	void *headers_c;
17452d1e697eSMoses Reuben 	void *headers_v;
1746466fa6d2SMaor Gottlieb 
17472d1e697eSMoses Reuben 	if (ib_spec->type & IB_FLOW_SPEC_INNER) {
17482d1e697eSMoses Reuben 		headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
17492d1e697eSMoses Reuben 					 inner_headers);
17502d1e697eSMoses Reuben 		headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
17512d1e697eSMoses Reuben 					 inner_headers);
17522d1e697eSMoses Reuben 	} else {
17532d1e697eSMoses Reuben 		headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
17542d1e697eSMoses Reuben 					 outer_headers);
17552d1e697eSMoses Reuben 		headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
17562d1e697eSMoses Reuben 					 outer_headers);
17572d1e697eSMoses Reuben 	}
17582d1e697eSMoses Reuben 
17592d1e697eSMoses Reuben 	switch (ib_spec->type & ~IB_FLOW_SPEC_INNER) {
1760038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_ETH:
1761c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->eth.mask, LAST_ETH_FIELD))
17621ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
1763038d2ef8SMaor Gottlieb 
17642d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
1765038d2ef8SMaor Gottlieb 					     dmac_47_16),
1766038d2ef8SMaor Gottlieb 				ib_spec->eth.mask.dst_mac);
17672d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
1768038d2ef8SMaor Gottlieb 					     dmac_47_16),
1769038d2ef8SMaor Gottlieb 				ib_spec->eth.val.dst_mac);
1770038d2ef8SMaor Gottlieb 
17712d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
1772ee3da804SMaor Gottlieb 					     smac_47_16),
1773ee3da804SMaor Gottlieb 				ib_spec->eth.mask.src_mac);
17742d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
1775ee3da804SMaor Gottlieb 					     smac_47_16),
1776ee3da804SMaor Gottlieb 				ib_spec->eth.val.src_mac);
1777ee3da804SMaor Gottlieb 
1778038d2ef8SMaor Gottlieb 		if (ib_spec->eth.mask.vlan_tag) {
17792d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
178010543365SMohamad Haj Yahia 				 cvlan_tag, 1);
17812d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
178210543365SMohamad Haj Yahia 				 cvlan_tag, 1);
1783038d2ef8SMaor Gottlieb 
17842d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1785038d2ef8SMaor Gottlieb 				 first_vid, ntohs(ib_spec->eth.mask.vlan_tag));
17862d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1787038d2ef8SMaor Gottlieb 				 first_vid, ntohs(ib_spec->eth.val.vlan_tag));
1788038d2ef8SMaor Gottlieb 
17892d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1790038d2ef8SMaor Gottlieb 				 first_cfi,
1791038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.mask.vlan_tag) >> 12);
17922d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1793038d2ef8SMaor Gottlieb 				 first_cfi,
1794038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.val.vlan_tag) >> 12);
1795038d2ef8SMaor Gottlieb 
17962d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1797038d2ef8SMaor Gottlieb 				 first_prio,
1798038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.mask.vlan_tag) >> 13);
17992d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1800038d2ef8SMaor Gottlieb 				 first_prio,
1801038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.val.vlan_tag) >> 13);
1802038d2ef8SMaor Gottlieb 		}
18032d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1804038d2ef8SMaor Gottlieb 			 ethertype, ntohs(ib_spec->eth.mask.ether_type));
18052d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1806038d2ef8SMaor Gottlieb 			 ethertype, ntohs(ib_spec->eth.val.ether_type));
1807038d2ef8SMaor Gottlieb 		break;
1808038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_IPV4:
1809c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->ipv4.mask, LAST_IPV4_FIELD))
18101ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
1811038d2ef8SMaor Gottlieb 
18122d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1813038d2ef8SMaor Gottlieb 			 ethertype, 0xffff);
18142d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1815038d2ef8SMaor Gottlieb 			 ethertype, ETH_P_IP);
1816038d2ef8SMaor Gottlieb 
18172d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
1818038d2ef8SMaor Gottlieb 				    src_ipv4_src_ipv6.ipv4_layout.ipv4),
1819038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.mask.src_ip,
1820038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.mask.src_ip));
18212d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
1822038d2ef8SMaor Gottlieb 				    src_ipv4_src_ipv6.ipv4_layout.ipv4),
1823038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.val.src_ip,
1824038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.val.src_ip));
18252d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
1826038d2ef8SMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
1827038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.mask.dst_ip,
1828038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.mask.dst_ip));
18292d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
1830038d2ef8SMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
1831038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.val.dst_ip,
1832038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.val.dst_ip));
1833ca0d4753SMaor Gottlieb 
18342d1e697eSMoses Reuben 		set_tos(headers_c, headers_v,
1835ca0d4753SMaor Gottlieb 			ib_spec->ipv4.mask.tos, ib_spec->ipv4.val.tos);
1836ca0d4753SMaor Gottlieb 
18372d1e697eSMoses Reuben 		set_proto(headers_c, headers_v,
1838ca0d4753SMaor Gottlieb 			  ib_spec->ipv4.mask.proto, ib_spec->ipv4.val.proto);
1839038d2ef8SMaor Gottlieb 		break;
1840026bae0cSMaor Gottlieb 	case IB_FLOW_SPEC_IPV6:
1841c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->ipv6.mask, LAST_IPV6_FIELD))
18421ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
1843026bae0cSMaor Gottlieb 
18442d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1845026bae0cSMaor Gottlieb 			 ethertype, 0xffff);
18462d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1847026bae0cSMaor Gottlieb 			 ethertype, ETH_P_IPV6);
1848026bae0cSMaor Gottlieb 
18492d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
1850026bae0cSMaor Gottlieb 				    src_ipv4_src_ipv6.ipv6_layout.ipv6),
1851026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.mask.src_ip,
1852026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.mask.src_ip));
18532d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
1854026bae0cSMaor Gottlieb 				    src_ipv4_src_ipv6.ipv6_layout.ipv6),
1855026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.val.src_ip,
1856026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.val.src_ip));
18572d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
1858026bae0cSMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
1859026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.mask.dst_ip,
1860026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.mask.dst_ip));
18612d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
1862026bae0cSMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
1863026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.val.dst_ip,
1864026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.val.dst_ip));
1865466fa6d2SMaor Gottlieb 
18662d1e697eSMoses Reuben 		set_tos(headers_c, headers_v,
1867466fa6d2SMaor Gottlieb 			ib_spec->ipv6.mask.traffic_class,
1868466fa6d2SMaor Gottlieb 			ib_spec->ipv6.val.traffic_class);
1869466fa6d2SMaor Gottlieb 
18702d1e697eSMoses Reuben 		set_proto(headers_c, headers_v,
1871466fa6d2SMaor Gottlieb 			  ib_spec->ipv6.mask.next_hdr,
1872466fa6d2SMaor Gottlieb 			  ib_spec->ipv6.val.next_hdr);
1873466fa6d2SMaor Gottlieb 
18742d1e697eSMoses Reuben 		set_flow_label(misc_params_c, misc_params_v,
18752d1e697eSMoses Reuben 			       ntohl(ib_spec->ipv6.mask.flow_label),
18762d1e697eSMoses Reuben 			       ntohl(ib_spec->ipv6.val.flow_label),
18772d1e697eSMoses Reuben 			       ib_spec->type & IB_FLOW_SPEC_INNER);
18782d1e697eSMoses Reuben 
1879026bae0cSMaor Gottlieb 		break;
1880038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_TCP:
1881c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
1882c47ac6aeSMaor Gottlieb 					 LAST_TCP_UDP_FIELD))
18831ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
1884038d2ef8SMaor Gottlieb 
18852d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
1886038d2ef8SMaor Gottlieb 			 0xff);
18872d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
1888038d2ef8SMaor Gottlieb 			 IPPROTO_TCP);
1889038d2ef8SMaor Gottlieb 
18902d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_sport,
1891038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.src_port));
18922d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_sport,
1893038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.src_port));
1894038d2ef8SMaor Gottlieb 
18952d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_dport,
1896038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.dst_port));
18972d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_dport,
1898038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.dst_port));
1899038d2ef8SMaor Gottlieb 		break;
1900038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_UDP:
1901c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
1902c47ac6aeSMaor Gottlieb 					 LAST_TCP_UDP_FIELD))
19031ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
1904038d2ef8SMaor Gottlieb 
19052d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
1906038d2ef8SMaor Gottlieb 			 0xff);
19072d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
1908038d2ef8SMaor Gottlieb 			 IPPROTO_UDP);
1909038d2ef8SMaor Gottlieb 
19102d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_sport,
1911038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.src_port));
19122d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_sport,
1913038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.src_port));
1914038d2ef8SMaor Gottlieb 
19152d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_dport,
1916038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.dst_port));
19172d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport,
1918038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.dst_port));
1919038d2ef8SMaor Gottlieb 		break;
1920ffb30d8fSMoses Reuben 	case IB_FLOW_SPEC_VXLAN_TUNNEL:
1921ffb30d8fSMoses Reuben 		if (FIELDS_NOT_SUPPORTED(ib_spec->tunnel.mask,
1922ffb30d8fSMoses Reuben 					 LAST_TUNNEL_FIELD))
19231ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
1924ffb30d8fSMoses Reuben 
1925ffb30d8fSMoses Reuben 		MLX5_SET(fte_match_set_misc, misc_params_c, vxlan_vni,
1926ffb30d8fSMoses Reuben 			 ntohl(ib_spec->tunnel.mask.tunnel_id));
1927ffb30d8fSMoses Reuben 		MLX5_SET(fte_match_set_misc, misc_params_v, vxlan_vni,
1928ffb30d8fSMoses Reuben 			 ntohl(ib_spec->tunnel.val.tunnel_id));
1929ffb30d8fSMoses Reuben 		break;
19302ac693f9SMoses Reuben 	case IB_FLOW_SPEC_ACTION_TAG:
19312ac693f9SMoses Reuben 		if (FIELDS_NOT_SUPPORTED(ib_spec->flow_tag,
19322ac693f9SMoses Reuben 					 LAST_FLOW_TAG_FIELD))
19332ac693f9SMoses Reuben 			return -EOPNOTSUPP;
19342ac693f9SMoses Reuben 		if (ib_spec->flow_tag.tag_id >= BIT(24))
19352ac693f9SMoses Reuben 			return -EINVAL;
19362ac693f9SMoses Reuben 
19372ac693f9SMoses Reuben 		*tag_id = ib_spec->flow_tag.tag_id;
19382ac693f9SMoses Reuben 		break;
1939038d2ef8SMaor Gottlieb 	default:
1940038d2ef8SMaor Gottlieb 		return -EINVAL;
1941038d2ef8SMaor Gottlieb 	}
1942038d2ef8SMaor Gottlieb 
1943038d2ef8SMaor Gottlieb 	return 0;
1944038d2ef8SMaor Gottlieb }
1945038d2ef8SMaor Gottlieb 
1946038d2ef8SMaor Gottlieb /* If a flow could catch both multicast and unicast packets,
1947038d2ef8SMaor Gottlieb  * it won't fall into the multicast flow steering table and this rule
1948038d2ef8SMaor Gottlieb  * could steal other multicast packets.
1949038d2ef8SMaor Gottlieb  */
1950038d2ef8SMaor Gottlieb static bool flow_is_multicast_only(struct ib_flow_attr *ib_attr)
1951038d2ef8SMaor Gottlieb {
1952038d2ef8SMaor Gottlieb 	struct ib_flow_spec_eth *eth_spec;
1953038d2ef8SMaor Gottlieb 
1954038d2ef8SMaor Gottlieb 	if (ib_attr->type != IB_FLOW_ATTR_NORMAL ||
1955038d2ef8SMaor Gottlieb 	    ib_attr->size < sizeof(struct ib_flow_attr) +
1956038d2ef8SMaor Gottlieb 	    sizeof(struct ib_flow_spec_eth) ||
1957038d2ef8SMaor Gottlieb 	    ib_attr->num_of_specs < 1)
1958038d2ef8SMaor Gottlieb 		return false;
1959038d2ef8SMaor Gottlieb 
1960038d2ef8SMaor Gottlieb 	eth_spec = (struct ib_flow_spec_eth *)(ib_attr + 1);
1961038d2ef8SMaor Gottlieb 	if (eth_spec->type != IB_FLOW_SPEC_ETH ||
1962038d2ef8SMaor Gottlieb 	    eth_spec->size != sizeof(*eth_spec))
1963038d2ef8SMaor Gottlieb 		return false;
1964038d2ef8SMaor Gottlieb 
1965038d2ef8SMaor Gottlieb 	return is_multicast_ether_addr(eth_spec->mask.dst_mac) &&
1966038d2ef8SMaor Gottlieb 	       is_multicast_ether_addr(eth_spec->val.dst_mac);
1967038d2ef8SMaor Gottlieb }
1968038d2ef8SMaor Gottlieb 
1969dd063d0eSMaor Gottlieb static bool is_valid_attr(const struct ib_flow_attr *flow_attr)
1970038d2ef8SMaor Gottlieb {
1971038d2ef8SMaor Gottlieb 	union ib_flow_spec *ib_spec = (union ib_flow_spec *)(flow_attr + 1);
1972038d2ef8SMaor Gottlieb 	bool has_ipv4_spec = false;
1973038d2ef8SMaor Gottlieb 	bool eth_type_ipv4 = true;
1974038d2ef8SMaor Gottlieb 	unsigned int spec_index;
1975038d2ef8SMaor Gottlieb 
1976038d2ef8SMaor Gottlieb 	/* Validate that ethertype is correct */
1977038d2ef8SMaor Gottlieb 	for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
1978038d2ef8SMaor Gottlieb 		if (ib_spec->type == IB_FLOW_SPEC_ETH &&
1979038d2ef8SMaor Gottlieb 		    ib_spec->eth.mask.ether_type) {
1980038d2ef8SMaor Gottlieb 			if (!((ib_spec->eth.mask.ether_type == htons(0xffff)) &&
1981038d2ef8SMaor Gottlieb 			      ib_spec->eth.val.ether_type == htons(ETH_P_IP)))
1982038d2ef8SMaor Gottlieb 				eth_type_ipv4 = false;
1983038d2ef8SMaor Gottlieb 		} else if (ib_spec->type == IB_FLOW_SPEC_IPV4) {
1984038d2ef8SMaor Gottlieb 			has_ipv4_spec = true;
1985038d2ef8SMaor Gottlieb 		}
1986038d2ef8SMaor Gottlieb 		ib_spec = (void *)ib_spec + ib_spec->size;
1987038d2ef8SMaor Gottlieb 	}
1988038d2ef8SMaor Gottlieb 	return !has_ipv4_spec || eth_type_ipv4;
1989038d2ef8SMaor Gottlieb }
1990038d2ef8SMaor Gottlieb 
1991038d2ef8SMaor Gottlieb static void put_flow_table(struct mlx5_ib_dev *dev,
1992038d2ef8SMaor Gottlieb 			   struct mlx5_ib_flow_prio *prio, bool ft_added)
1993038d2ef8SMaor Gottlieb {
1994038d2ef8SMaor Gottlieb 	prio->refcount -= !!ft_added;
1995038d2ef8SMaor Gottlieb 	if (!prio->refcount) {
1996038d2ef8SMaor Gottlieb 		mlx5_destroy_flow_table(prio->flow_table);
1997038d2ef8SMaor Gottlieb 		prio->flow_table = NULL;
1998038d2ef8SMaor Gottlieb 	}
1999038d2ef8SMaor Gottlieb }
2000038d2ef8SMaor Gottlieb 
2001038d2ef8SMaor Gottlieb static int mlx5_ib_destroy_flow(struct ib_flow *flow_id)
2002038d2ef8SMaor Gottlieb {
2003038d2ef8SMaor Gottlieb 	struct mlx5_ib_dev *dev = to_mdev(flow_id->qp->device);
2004038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = container_of(flow_id,
2005038d2ef8SMaor Gottlieb 							  struct mlx5_ib_flow_handler,
2006038d2ef8SMaor Gottlieb 							  ibflow);
2007038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *iter, *tmp;
2008038d2ef8SMaor Gottlieb 
2009038d2ef8SMaor Gottlieb 	mutex_lock(&dev->flow_db.lock);
2010038d2ef8SMaor Gottlieb 
2011038d2ef8SMaor Gottlieb 	list_for_each_entry_safe(iter, tmp, &handler->list, list) {
201274491de9SMark Bloch 		mlx5_del_flow_rules(iter->rule);
2013cc0e5d42SMaor Gottlieb 		put_flow_table(dev, iter->prio, true);
2014038d2ef8SMaor Gottlieb 		list_del(&iter->list);
2015038d2ef8SMaor Gottlieb 		kfree(iter);
2016038d2ef8SMaor Gottlieb 	}
2017038d2ef8SMaor Gottlieb 
201874491de9SMark Bloch 	mlx5_del_flow_rules(handler->rule);
20195497adc6SMaor Gottlieb 	put_flow_table(dev, handler->prio, true);
2020038d2ef8SMaor Gottlieb 	mutex_unlock(&dev->flow_db.lock);
2021038d2ef8SMaor Gottlieb 
2022038d2ef8SMaor Gottlieb 	kfree(handler);
2023038d2ef8SMaor Gottlieb 
2024038d2ef8SMaor Gottlieb 	return 0;
2025038d2ef8SMaor Gottlieb }
2026038d2ef8SMaor Gottlieb 
202735d19011SMaor Gottlieb static int ib_prio_to_core_prio(unsigned int priority, bool dont_trap)
202835d19011SMaor Gottlieb {
202935d19011SMaor Gottlieb 	priority *= 2;
203035d19011SMaor Gottlieb 	if (!dont_trap)
203135d19011SMaor Gottlieb 		priority++;
203235d19011SMaor Gottlieb 	return priority;
203335d19011SMaor Gottlieb }
203435d19011SMaor Gottlieb 
2035cc0e5d42SMaor Gottlieb enum flow_table_type {
2036cc0e5d42SMaor Gottlieb 	MLX5_IB_FT_RX,
2037cc0e5d42SMaor Gottlieb 	MLX5_IB_FT_TX
2038cc0e5d42SMaor Gottlieb };
2039cc0e5d42SMaor Gottlieb 
2040038d2ef8SMaor Gottlieb #define MLX5_FS_MAX_TYPES	 10
2041038d2ef8SMaor Gottlieb #define MLX5_FS_MAX_ENTRIES	 32000UL
2042038d2ef8SMaor Gottlieb static struct mlx5_ib_flow_prio *get_flow_table(struct mlx5_ib_dev *dev,
2043cc0e5d42SMaor Gottlieb 						struct ib_flow_attr *flow_attr,
2044cc0e5d42SMaor Gottlieb 						enum flow_table_type ft_type)
2045038d2ef8SMaor Gottlieb {
204635d19011SMaor Gottlieb 	bool dont_trap = flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP;
2047038d2ef8SMaor Gottlieb 	struct mlx5_flow_namespace *ns = NULL;
2048038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_prio *prio;
2049038d2ef8SMaor Gottlieb 	struct mlx5_flow_table *ft;
2050038d2ef8SMaor Gottlieb 	int num_entries;
2051038d2ef8SMaor Gottlieb 	int num_groups;
2052038d2ef8SMaor Gottlieb 	int priority;
2053038d2ef8SMaor Gottlieb 	int err = 0;
2054038d2ef8SMaor Gottlieb 
2055038d2ef8SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
205635d19011SMaor Gottlieb 		if (flow_is_multicast_only(flow_attr) &&
205735d19011SMaor Gottlieb 		    !dont_trap)
2058038d2ef8SMaor Gottlieb 			priority = MLX5_IB_FLOW_MCAST_PRIO;
2059038d2ef8SMaor Gottlieb 		else
206035d19011SMaor Gottlieb 			priority = ib_prio_to_core_prio(flow_attr->priority,
206135d19011SMaor Gottlieb 							dont_trap);
2062038d2ef8SMaor Gottlieb 		ns = mlx5_get_flow_namespace(dev->mdev,
2063038d2ef8SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_BYPASS);
2064038d2ef8SMaor Gottlieb 		num_entries = MLX5_FS_MAX_ENTRIES;
2065038d2ef8SMaor Gottlieb 		num_groups = MLX5_FS_MAX_TYPES;
2066038d2ef8SMaor Gottlieb 		prio = &dev->flow_db.prios[priority];
2067038d2ef8SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
2068038d2ef8SMaor Gottlieb 		   flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
2069038d2ef8SMaor Gottlieb 		ns = mlx5_get_flow_namespace(dev->mdev,
2070038d2ef8SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_LEFTOVERS);
2071038d2ef8SMaor Gottlieb 		build_leftovers_ft_param(&priority,
2072038d2ef8SMaor Gottlieb 					 &num_entries,
2073038d2ef8SMaor Gottlieb 					 &num_groups);
2074038d2ef8SMaor Gottlieb 		prio = &dev->flow_db.prios[MLX5_IB_FLOW_LEFTOVERS_PRIO];
2075cc0e5d42SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
2076cc0e5d42SMaor Gottlieb 		if (!MLX5_CAP_FLOWTABLE(dev->mdev,
2077cc0e5d42SMaor Gottlieb 					allow_sniffer_and_nic_rx_shared_tir))
2078cc0e5d42SMaor Gottlieb 			return ERR_PTR(-ENOTSUPP);
2079cc0e5d42SMaor Gottlieb 
2080cc0e5d42SMaor Gottlieb 		ns = mlx5_get_flow_namespace(dev->mdev, ft_type == MLX5_IB_FT_RX ?
2081cc0e5d42SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_SNIFFER_RX :
2082cc0e5d42SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_SNIFFER_TX);
2083cc0e5d42SMaor Gottlieb 
2084cc0e5d42SMaor Gottlieb 		prio = &dev->flow_db.sniffer[ft_type];
2085cc0e5d42SMaor Gottlieb 		priority = 0;
2086cc0e5d42SMaor Gottlieb 		num_entries = 1;
2087cc0e5d42SMaor Gottlieb 		num_groups = 1;
2088038d2ef8SMaor Gottlieb 	}
2089038d2ef8SMaor Gottlieb 
2090038d2ef8SMaor Gottlieb 	if (!ns)
2091038d2ef8SMaor Gottlieb 		return ERR_PTR(-ENOTSUPP);
2092038d2ef8SMaor Gottlieb 
2093038d2ef8SMaor Gottlieb 	ft = prio->flow_table;
2094038d2ef8SMaor Gottlieb 	if (!ft) {
2095038d2ef8SMaor Gottlieb 		ft = mlx5_create_auto_grouped_flow_table(ns, priority,
2096038d2ef8SMaor Gottlieb 							 num_entries,
2097d63cd286SMaor Gottlieb 							 num_groups,
2098c9f1b073SHadar Hen Zion 							 0, 0);
2099038d2ef8SMaor Gottlieb 
2100038d2ef8SMaor Gottlieb 		if (!IS_ERR(ft)) {
2101038d2ef8SMaor Gottlieb 			prio->refcount = 0;
2102038d2ef8SMaor Gottlieb 			prio->flow_table = ft;
2103038d2ef8SMaor Gottlieb 		} else {
2104038d2ef8SMaor Gottlieb 			err = PTR_ERR(ft);
2105038d2ef8SMaor Gottlieb 		}
2106038d2ef8SMaor Gottlieb 	}
2107038d2ef8SMaor Gottlieb 
2108038d2ef8SMaor Gottlieb 	return err ? ERR_PTR(err) : prio;
2109038d2ef8SMaor Gottlieb }
2110038d2ef8SMaor Gottlieb 
2111038d2ef8SMaor Gottlieb static struct mlx5_ib_flow_handler *create_flow_rule(struct mlx5_ib_dev *dev,
2112038d2ef8SMaor Gottlieb 						     struct mlx5_ib_flow_prio *ft_prio,
2113dd063d0eSMaor Gottlieb 						     const struct ib_flow_attr *flow_attr,
2114038d2ef8SMaor Gottlieb 						     struct mlx5_flow_destination *dst)
2115038d2ef8SMaor Gottlieb {
2116038d2ef8SMaor Gottlieb 	struct mlx5_flow_table	*ft = ft_prio->flow_table;
2117038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler;
211866958ed9SHadar Hen Zion 	struct mlx5_flow_act flow_act = {0};
2119c5bb1730SMaor Gottlieb 	struct mlx5_flow_spec *spec;
2120dd063d0eSMaor Gottlieb 	const void *ib_flow = (const void *)flow_attr + sizeof(*flow_attr);
2121038d2ef8SMaor Gottlieb 	unsigned int spec_index;
21222ac693f9SMoses Reuben 	u32 flow_tag = MLX5_FS_DEFAULT_FLOW_TAG;
2123038d2ef8SMaor Gottlieb 	int err = 0;
2124038d2ef8SMaor Gottlieb 
2125038d2ef8SMaor Gottlieb 	if (!is_valid_attr(flow_attr))
2126038d2ef8SMaor Gottlieb 		return ERR_PTR(-EINVAL);
2127038d2ef8SMaor Gottlieb 
2128c5bb1730SMaor Gottlieb 	spec = mlx5_vzalloc(sizeof(*spec));
2129038d2ef8SMaor Gottlieb 	handler = kzalloc(sizeof(*handler), GFP_KERNEL);
2130c5bb1730SMaor Gottlieb 	if (!handler || !spec) {
2131038d2ef8SMaor Gottlieb 		err = -ENOMEM;
2132038d2ef8SMaor Gottlieb 		goto free;
2133038d2ef8SMaor Gottlieb 	}
2134038d2ef8SMaor Gottlieb 
2135038d2ef8SMaor Gottlieb 	INIT_LIST_HEAD(&handler->list);
2136038d2ef8SMaor Gottlieb 
2137038d2ef8SMaor Gottlieb 	for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
2138c5bb1730SMaor Gottlieb 		err = parse_flow_attr(spec->match_criteria,
21392ac693f9SMoses Reuben 				      spec->match_value, ib_flow, &flow_tag);
2140038d2ef8SMaor Gottlieb 		if (err < 0)
2141038d2ef8SMaor Gottlieb 			goto free;
2142038d2ef8SMaor Gottlieb 
2143038d2ef8SMaor Gottlieb 		ib_flow += ((union ib_flow_spec *)ib_flow)->size;
2144038d2ef8SMaor Gottlieb 	}
2145038d2ef8SMaor Gottlieb 
2146466fa6d2SMaor Gottlieb 	spec->match_criteria_enable = get_match_criteria_enable(spec->match_criteria);
214766958ed9SHadar Hen Zion 	flow_act.action = dst ? MLX5_FLOW_CONTEXT_ACTION_FWD_DEST :
214835d19011SMaor Gottlieb 		MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO;
21492ac693f9SMoses Reuben 
21502ac693f9SMoses Reuben 	if (flow_tag != MLX5_FS_DEFAULT_FLOW_TAG &&
21512ac693f9SMoses Reuben 	    (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
21522ac693f9SMoses Reuben 	     flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT)) {
21532ac693f9SMoses Reuben 		mlx5_ib_warn(dev, "Flow tag %u and attribute type %x isn't allowed in leftovers\n",
21542ac693f9SMoses Reuben 			     flow_tag, flow_attr->type);
21552ac693f9SMoses Reuben 		err = -EINVAL;
21562ac693f9SMoses Reuben 		goto free;
21572ac693f9SMoses Reuben 	}
21582ac693f9SMoses Reuben 	flow_act.flow_tag = flow_tag;
215974491de9SMark Bloch 	handler->rule = mlx5_add_flow_rules(ft, spec,
216066958ed9SHadar Hen Zion 					    &flow_act,
216174491de9SMark Bloch 					    dst, 1);
2162038d2ef8SMaor Gottlieb 
2163038d2ef8SMaor Gottlieb 	if (IS_ERR(handler->rule)) {
2164038d2ef8SMaor Gottlieb 		err = PTR_ERR(handler->rule);
2165038d2ef8SMaor Gottlieb 		goto free;
2166038d2ef8SMaor Gottlieb 	}
2167038d2ef8SMaor Gottlieb 
2168d9d4980aSMaor Gottlieb 	ft_prio->refcount++;
21695497adc6SMaor Gottlieb 	handler->prio = ft_prio;
2170038d2ef8SMaor Gottlieb 
2171038d2ef8SMaor Gottlieb 	ft_prio->flow_table = ft;
2172038d2ef8SMaor Gottlieb free:
2173038d2ef8SMaor Gottlieb 	if (err)
2174038d2ef8SMaor Gottlieb 		kfree(handler);
2175c5bb1730SMaor Gottlieb 	kvfree(spec);
2176038d2ef8SMaor Gottlieb 	return err ? ERR_PTR(err) : handler;
2177038d2ef8SMaor Gottlieb }
2178038d2ef8SMaor Gottlieb 
217935d19011SMaor Gottlieb static struct mlx5_ib_flow_handler *create_dont_trap_rule(struct mlx5_ib_dev *dev,
218035d19011SMaor Gottlieb 							  struct mlx5_ib_flow_prio *ft_prio,
218135d19011SMaor Gottlieb 							  struct ib_flow_attr *flow_attr,
218235d19011SMaor Gottlieb 							  struct mlx5_flow_destination *dst)
218335d19011SMaor Gottlieb {
218435d19011SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_dst = NULL;
218535d19011SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
218635d19011SMaor Gottlieb 
218735d19011SMaor Gottlieb 	handler = create_flow_rule(dev, ft_prio, flow_attr, NULL);
218835d19011SMaor Gottlieb 	if (!IS_ERR(handler)) {
218935d19011SMaor Gottlieb 		handler_dst = create_flow_rule(dev, ft_prio,
219035d19011SMaor Gottlieb 					       flow_attr, dst);
219135d19011SMaor Gottlieb 		if (IS_ERR(handler_dst)) {
219274491de9SMark Bloch 			mlx5_del_flow_rules(handler->rule);
2193d9d4980aSMaor Gottlieb 			ft_prio->refcount--;
219435d19011SMaor Gottlieb 			kfree(handler);
219535d19011SMaor Gottlieb 			handler = handler_dst;
219635d19011SMaor Gottlieb 		} else {
219735d19011SMaor Gottlieb 			list_add(&handler_dst->list, &handler->list);
219835d19011SMaor Gottlieb 		}
219935d19011SMaor Gottlieb 	}
220035d19011SMaor Gottlieb 
220135d19011SMaor Gottlieb 	return handler;
220235d19011SMaor Gottlieb }
2203038d2ef8SMaor Gottlieb enum {
2204038d2ef8SMaor Gottlieb 	LEFTOVERS_MC,
2205038d2ef8SMaor Gottlieb 	LEFTOVERS_UC,
2206038d2ef8SMaor Gottlieb };
2207038d2ef8SMaor Gottlieb 
2208038d2ef8SMaor Gottlieb static struct mlx5_ib_flow_handler *create_leftovers_rule(struct mlx5_ib_dev *dev,
2209038d2ef8SMaor Gottlieb 							  struct mlx5_ib_flow_prio *ft_prio,
2210038d2ef8SMaor Gottlieb 							  struct ib_flow_attr *flow_attr,
2211038d2ef8SMaor Gottlieb 							  struct mlx5_flow_destination *dst)
2212038d2ef8SMaor Gottlieb {
2213038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_ucast = NULL;
2214038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
2215038d2ef8SMaor Gottlieb 
2216038d2ef8SMaor Gottlieb 	static struct {
2217038d2ef8SMaor Gottlieb 		struct ib_flow_attr	flow_attr;
2218038d2ef8SMaor Gottlieb 		struct ib_flow_spec_eth eth_flow;
2219038d2ef8SMaor Gottlieb 	} leftovers_specs[] = {
2220038d2ef8SMaor Gottlieb 		[LEFTOVERS_MC] = {
2221038d2ef8SMaor Gottlieb 			.flow_attr = {
2222038d2ef8SMaor Gottlieb 				.num_of_specs = 1,
2223038d2ef8SMaor Gottlieb 				.size = sizeof(leftovers_specs[0])
2224038d2ef8SMaor Gottlieb 			},
2225038d2ef8SMaor Gottlieb 			.eth_flow = {
2226038d2ef8SMaor Gottlieb 				.type = IB_FLOW_SPEC_ETH,
2227038d2ef8SMaor Gottlieb 				.size = sizeof(struct ib_flow_spec_eth),
2228038d2ef8SMaor Gottlieb 				.mask = {.dst_mac = {0x1} },
2229038d2ef8SMaor Gottlieb 				.val =  {.dst_mac = {0x1} }
2230038d2ef8SMaor Gottlieb 			}
2231038d2ef8SMaor Gottlieb 		},
2232038d2ef8SMaor Gottlieb 		[LEFTOVERS_UC] = {
2233038d2ef8SMaor Gottlieb 			.flow_attr = {
2234038d2ef8SMaor Gottlieb 				.num_of_specs = 1,
2235038d2ef8SMaor Gottlieb 				.size = sizeof(leftovers_specs[0])
2236038d2ef8SMaor Gottlieb 			},
2237038d2ef8SMaor Gottlieb 			.eth_flow = {
2238038d2ef8SMaor Gottlieb 				.type = IB_FLOW_SPEC_ETH,
2239038d2ef8SMaor Gottlieb 				.size = sizeof(struct ib_flow_spec_eth),
2240038d2ef8SMaor Gottlieb 				.mask = {.dst_mac = {0x1} },
2241038d2ef8SMaor Gottlieb 				.val = {.dst_mac = {} }
2242038d2ef8SMaor Gottlieb 			}
2243038d2ef8SMaor Gottlieb 		}
2244038d2ef8SMaor Gottlieb 	};
2245038d2ef8SMaor Gottlieb 
2246038d2ef8SMaor Gottlieb 	handler = create_flow_rule(dev, ft_prio,
2247038d2ef8SMaor Gottlieb 				   &leftovers_specs[LEFTOVERS_MC].flow_attr,
2248038d2ef8SMaor Gottlieb 				   dst);
2249038d2ef8SMaor Gottlieb 	if (!IS_ERR(handler) &&
2250038d2ef8SMaor Gottlieb 	    flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT) {
2251038d2ef8SMaor Gottlieb 		handler_ucast = create_flow_rule(dev, ft_prio,
2252038d2ef8SMaor Gottlieb 						 &leftovers_specs[LEFTOVERS_UC].flow_attr,
2253038d2ef8SMaor Gottlieb 						 dst);
2254038d2ef8SMaor Gottlieb 		if (IS_ERR(handler_ucast)) {
225574491de9SMark Bloch 			mlx5_del_flow_rules(handler->rule);
2256d9d4980aSMaor Gottlieb 			ft_prio->refcount--;
2257038d2ef8SMaor Gottlieb 			kfree(handler);
2258038d2ef8SMaor Gottlieb 			handler = handler_ucast;
2259038d2ef8SMaor Gottlieb 		} else {
2260038d2ef8SMaor Gottlieb 			list_add(&handler_ucast->list, &handler->list);
2261038d2ef8SMaor Gottlieb 		}
2262038d2ef8SMaor Gottlieb 	}
2263038d2ef8SMaor Gottlieb 
2264038d2ef8SMaor Gottlieb 	return handler;
2265038d2ef8SMaor Gottlieb }
2266038d2ef8SMaor Gottlieb 
2267cc0e5d42SMaor Gottlieb static struct mlx5_ib_flow_handler *create_sniffer_rule(struct mlx5_ib_dev *dev,
2268cc0e5d42SMaor Gottlieb 							struct mlx5_ib_flow_prio *ft_rx,
2269cc0e5d42SMaor Gottlieb 							struct mlx5_ib_flow_prio *ft_tx,
2270cc0e5d42SMaor Gottlieb 							struct mlx5_flow_destination *dst)
2271cc0e5d42SMaor Gottlieb {
2272cc0e5d42SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_rx;
2273cc0e5d42SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_tx;
2274cc0e5d42SMaor Gottlieb 	int err;
2275cc0e5d42SMaor Gottlieb 	static const struct ib_flow_attr flow_attr  = {
2276cc0e5d42SMaor Gottlieb 		.num_of_specs = 0,
2277cc0e5d42SMaor Gottlieb 		.size = sizeof(flow_attr)
2278cc0e5d42SMaor Gottlieb 	};
2279cc0e5d42SMaor Gottlieb 
2280cc0e5d42SMaor Gottlieb 	handler_rx = create_flow_rule(dev, ft_rx, &flow_attr, dst);
2281cc0e5d42SMaor Gottlieb 	if (IS_ERR(handler_rx)) {
2282cc0e5d42SMaor Gottlieb 		err = PTR_ERR(handler_rx);
2283cc0e5d42SMaor Gottlieb 		goto err;
2284cc0e5d42SMaor Gottlieb 	}
2285cc0e5d42SMaor Gottlieb 
2286cc0e5d42SMaor Gottlieb 	handler_tx = create_flow_rule(dev, ft_tx, &flow_attr, dst);
2287cc0e5d42SMaor Gottlieb 	if (IS_ERR(handler_tx)) {
2288cc0e5d42SMaor Gottlieb 		err = PTR_ERR(handler_tx);
2289cc0e5d42SMaor Gottlieb 		goto err_tx;
2290cc0e5d42SMaor Gottlieb 	}
2291cc0e5d42SMaor Gottlieb 
2292cc0e5d42SMaor Gottlieb 	list_add(&handler_tx->list, &handler_rx->list);
2293cc0e5d42SMaor Gottlieb 
2294cc0e5d42SMaor Gottlieb 	return handler_rx;
2295cc0e5d42SMaor Gottlieb 
2296cc0e5d42SMaor Gottlieb err_tx:
229774491de9SMark Bloch 	mlx5_del_flow_rules(handler_rx->rule);
2298cc0e5d42SMaor Gottlieb 	ft_rx->refcount--;
2299cc0e5d42SMaor Gottlieb 	kfree(handler_rx);
2300cc0e5d42SMaor Gottlieb err:
2301cc0e5d42SMaor Gottlieb 	return ERR_PTR(err);
2302cc0e5d42SMaor Gottlieb }
2303cc0e5d42SMaor Gottlieb 
2304038d2ef8SMaor Gottlieb static struct ib_flow *mlx5_ib_create_flow(struct ib_qp *qp,
2305038d2ef8SMaor Gottlieb 					   struct ib_flow_attr *flow_attr,
2306038d2ef8SMaor Gottlieb 					   int domain)
2307038d2ef8SMaor Gottlieb {
2308038d2ef8SMaor Gottlieb 	struct mlx5_ib_dev *dev = to_mdev(qp->device);
2309d9f88e5aSYishai Hadas 	struct mlx5_ib_qp *mqp = to_mqp(qp);
2310038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
2311038d2ef8SMaor Gottlieb 	struct mlx5_flow_destination *dst = NULL;
2312cc0e5d42SMaor Gottlieb 	struct mlx5_ib_flow_prio *ft_prio_tx = NULL;
2313038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_prio *ft_prio;
2314038d2ef8SMaor Gottlieb 	int err;
2315038d2ef8SMaor Gottlieb 
2316038d2ef8SMaor Gottlieb 	if (flow_attr->priority > MLX5_IB_FLOW_LAST_PRIO)
2317038d2ef8SMaor Gottlieb 		return ERR_PTR(-ENOSPC);
2318038d2ef8SMaor Gottlieb 
2319038d2ef8SMaor Gottlieb 	if (domain != IB_FLOW_DOMAIN_USER ||
2320038d2ef8SMaor Gottlieb 	    flow_attr->port > MLX5_CAP_GEN(dev->mdev, num_ports) ||
232135d19011SMaor Gottlieb 	    (flow_attr->flags & ~IB_FLOW_ATTR_FLAGS_DONT_TRAP))
2322038d2ef8SMaor Gottlieb 		return ERR_PTR(-EINVAL);
2323038d2ef8SMaor Gottlieb 
2324038d2ef8SMaor Gottlieb 	dst = kzalloc(sizeof(*dst), GFP_KERNEL);
2325038d2ef8SMaor Gottlieb 	if (!dst)
2326038d2ef8SMaor Gottlieb 		return ERR_PTR(-ENOMEM);
2327038d2ef8SMaor Gottlieb 
2328038d2ef8SMaor Gottlieb 	mutex_lock(&dev->flow_db.lock);
2329038d2ef8SMaor Gottlieb 
2330cc0e5d42SMaor Gottlieb 	ft_prio = get_flow_table(dev, flow_attr, MLX5_IB_FT_RX);
2331038d2ef8SMaor Gottlieb 	if (IS_ERR(ft_prio)) {
2332038d2ef8SMaor Gottlieb 		err = PTR_ERR(ft_prio);
2333038d2ef8SMaor Gottlieb 		goto unlock;
2334038d2ef8SMaor Gottlieb 	}
2335cc0e5d42SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
2336cc0e5d42SMaor Gottlieb 		ft_prio_tx = get_flow_table(dev, flow_attr, MLX5_IB_FT_TX);
2337cc0e5d42SMaor Gottlieb 		if (IS_ERR(ft_prio_tx)) {
2338cc0e5d42SMaor Gottlieb 			err = PTR_ERR(ft_prio_tx);
2339cc0e5d42SMaor Gottlieb 			ft_prio_tx = NULL;
2340cc0e5d42SMaor Gottlieb 			goto destroy_ft;
2341cc0e5d42SMaor Gottlieb 		}
2342cc0e5d42SMaor Gottlieb 	}
2343038d2ef8SMaor Gottlieb 
2344038d2ef8SMaor Gottlieb 	dst->type = MLX5_FLOW_DESTINATION_TYPE_TIR;
2345d9f88e5aSYishai Hadas 	if (mqp->flags & MLX5_IB_QP_RSS)
2346d9f88e5aSYishai Hadas 		dst->tir_num = mqp->rss_qp.tirn;
2347d9f88e5aSYishai Hadas 	else
2348d9f88e5aSYishai Hadas 		dst->tir_num = mqp->raw_packet_qp.rq.tirn;
2349038d2ef8SMaor Gottlieb 
2350038d2ef8SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
235135d19011SMaor Gottlieb 		if (flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP)  {
235235d19011SMaor Gottlieb 			handler = create_dont_trap_rule(dev, ft_prio,
235335d19011SMaor Gottlieb 							flow_attr, dst);
235435d19011SMaor Gottlieb 		} else {
2355038d2ef8SMaor Gottlieb 			handler = create_flow_rule(dev, ft_prio, flow_attr,
2356038d2ef8SMaor Gottlieb 						   dst);
235735d19011SMaor Gottlieb 		}
2358038d2ef8SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
2359038d2ef8SMaor Gottlieb 		   flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
2360038d2ef8SMaor Gottlieb 		handler = create_leftovers_rule(dev, ft_prio, flow_attr,
2361038d2ef8SMaor Gottlieb 						dst);
2362cc0e5d42SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
2363cc0e5d42SMaor Gottlieb 		handler = create_sniffer_rule(dev, ft_prio, ft_prio_tx, dst);
2364038d2ef8SMaor Gottlieb 	} else {
2365038d2ef8SMaor Gottlieb 		err = -EINVAL;
2366038d2ef8SMaor Gottlieb 		goto destroy_ft;
2367038d2ef8SMaor Gottlieb 	}
2368038d2ef8SMaor Gottlieb 
2369038d2ef8SMaor Gottlieb 	if (IS_ERR(handler)) {
2370038d2ef8SMaor Gottlieb 		err = PTR_ERR(handler);
2371038d2ef8SMaor Gottlieb 		handler = NULL;
2372038d2ef8SMaor Gottlieb 		goto destroy_ft;
2373038d2ef8SMaor Gottlieb 	}
2374038d2ef8SMaor Gottlieb 
2375038d2ef8SMaor Gottlieb 	mutex_unlock(&dev->flow_db.lock);
2376038d2ef8SMaor Gottlieb 	kfree(dst);
2377038d2ef8SMaor Gottlieb 
2378038d2ef8SMaor Gottlieb 	return &handler->ibflow;
2379038d2ef8SMaor Gottlieb 
2380038d2ef8SMaor Gottlieb destroy_ft:
2381038d2ef8SMaor Gottlieb 	put_flow_table(dev, ft_prio, false);
2382cc0e5d42SMaor Gottlieb 	if (ft_prio_tx)
2383cc0e5d42SMaor Gottlieb 		put_flow_table(dev, ft_prio_tx, false);
2384038d2ef8SMaor Gottlieb unlock:
2385038d2ef8SMaor Gottlieb 	mutex_unlock(&dev->flow_db.lock);
2386038d2ef8SMaor Gottlieb 	kfree(dst);
2387038d2ef8SMaor Gottlieb 	kfree(handler);
2388038d2ef8SMaor Gottlieb 	return ERR_PTR(err);
2389038d2ef8SMaor Gottlieb }
2390038d2ef8SMaor Gottlieb 
2391e126ba97SEli Cohen static int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
2392e126ba97SEli Cohen {
2393e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
2394e126ba97SEli Cohen 	int err;
2395e126ba97SEli Cohen 
23969603b61dSJack Morgenstein 	err = mlx5_core_attach_mcg(dev->mdev, gid, ibqp->qp_num);
2397e126ba97SEli Cohen 	if (err)
2398e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n",
2399e126ba97SEli Cohen 			     ibqp->qp_num, gid->raw);
2400e126ba97SEli Cohen 
2401e126ba97SEli Cohen 	return err;
2402e126ba97SEli Cohen }
2403e126ba97SEli Cohen 
2404e126ba97SEli Cohen static int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
2405e126ba97SEli Cohen {
2406e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
2407e126ba97SEli Cohen 	int err;
2408e126ba97SEli Cohen 
24099603b61dSJack Morgenstein 	err = mlx5_core_detach_mcg(dev->mdev, gid, ibqp->qp_num);
2410e126ba97SEli Cohen 	if (err)
2411e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n",
2412e126ba97SEli Cohen 			     ibqp->qp_num, gid->raw);
2413e126ba97SEli Cohen 
2414e126ba97SEli Cohen 	return err;
2415e126ba97SEli Cohen }
2416e126ba97SEli Cohen 
2417e126ba97SEli Cohen static int init_node_data(struct mlx5_ib_dev *dev)
2418e126ba97SEli Cohen {
24191b5daf11SMajd Dibbiny 	int err;
2420e126ba97SEli Cohen 
24211b5daf11SMajd Dibbiny 	err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc);
2422e126ba97SEli Cohen 	if (err)
2423e126ba97SEli Cohen 		return err;
24241b5daf11SMajd Dibbiny 
24251b5daf11SMajd Dibbiny 	dev->mdev->rev_id = dev->mdev->pdev->revision;
24261b5daf11SMajd Dibbiny 
24271b5daf11SMajd Dibbiny 	return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid);
2428e126ba97SEli Cohen }
2429e126ba97SEli Cohen 
2430e126ba97SEli Cohen static ssize_t show_fw_pages(struct device *device, struct device_attribute *attr,
2431e126ba97SEli Cohen 			     char *buf)
2432e126ba97SEli Cohen {
2433e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
2434e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
2435e126ba97SEli Cohen 
24369603b61dSJack Morgenstein 	return sprintf(buf, "%d\n", dev->mdev->priv.fw_pages);
2437e126ba97SEli Cohen }
2438e126ba97SEli Cohen 
2439e126ba97SEli Cohen static ssize_t show_reg_pages(struct device *device,
2440e126ba97SEli Cohen 			      struct device_attribute *attr, char *buf)
2441e126ba97SEli Cohen {
2442e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
2443e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
2444e126ba97SEli Cohen 
24456aec21f6SHaggai Eran 	return sprintf(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages));
2446e126ba97SEli Cohen }
2447e126ba97SEli Cohen 
2448e126ba97SEli Cohen static ssize_t show_hca(struct device *device, struct device_attribute *attr,
2449e126ba97SEli Cohen 			char *buf)
2450e126ba97SEli Cohen {
2451e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
2452e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
24539603b61dSJack Morgenstein 	return sprintf(buf, "MT%d\n", dev->mdev->pdev->device);
2454e126ba97SEli Cohen }
2455e126ba97SEli Cohen 
2456e126ba97SEli Cohen static ssize_t show_rev(struct device *device, struct device_attribute *attr,
2457e126ba97SEli Cohen 			char *buf)
2458e126ba97SEli Cohen {
2459e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
2460e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
24619603b61dSJack Morgenstein 	return sprintf(buf, "%x\n", dev->mdev->rev_id);
2462e126ba97SEli Cohen }
2463e126ba97SEli Cohen 
2464e126ba97SEli Cohen static ssize_t show_board(struct device *device, struct device_attribute *attr,
2465e126ba97SEli Cohen 			  char *buf)
2466e126ba97SEli Cohen {
2467e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
2468e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
2469e126ba97SEli Cohen 	return sprintf(buf, "%.*s\n", MLX5_BOARD_ID_LEN,
24709603b61dSJack Morgenstein 		       dev->mdev->board_id);
2471e126ba97SEli Cohen }
2472e126ba97SEli Cohen 
2473e126ba97SEli Cohen static DEVICE_ATTR(hw_rev,   S_IRUGO, show_rev,    NULL);
2474e126ba97SEli Cohen static DEVICE_ATTR(hca_type, S_IRUGO, show_hca,    NULL);
2475e126ba97SEli Cohen static DEVICE_ATTR(board_id, S_IRUGO, show_board,  NULL);
2476e126ba97SEli Cohen static DEVICE_ATTR(fw_pages, S_IRUGO, show_fw_pages, NULL);
2477e126ba97SEli Cohen static DEVICE_ATTR(reg_pages, S_IRUGO, show_reg_pages, NULL);
2478e126ba97SEli Cohen 
2479e126ba97SEli Cohen static struct device_attribute *mlx5_class_attributes[] = {
2480e126ba97SEli Cohen 	&dev_attr_hw_rev,
2481e126ba97SEli Cohen 	&dev_attr_hca_type,
2482e126ba97SEli Cohen 	&dev_attr_board_id,
2483e126ba97SEli Cohen 	&dev_attr_fw_pages,
2484e126ba97SEli Cohen 	&dev_attr_reg_pages,
2485e126ba97SEli Cohen };
2486e126ba97SEli Cohen 
24877722f47eSHaggai Eran static void pkey_change_handler(struct work_struct *work)
24887722f47eSHaggai Eran {
24897722f47eSHaggai Eran 	struct mlx5_ib_port_resources *ports =
24907722f47eSHaggai Eran 		container_of(work, struct mlx5_ib_port_resources,
24917722f47eSHaggai Eran 			     pkey_change_work);
24927722f47eSHaggai Eran 
24937722f47eSHaggai Eran 	mutex_lock(&ports->devr->mutex);
24947722f47eSHaggai Eran 	mlx5_ib_gsi_pkey_change(ports->gsi);
24957722f47eSHaggai Eran 	mutex_unlock(&ports->devr->mutex);
24967722f47eSHaggai Eran }
24977722f47eSHaggai Eran 
249889ea94a7SMaor Gottlieb static void mlx5_ib_handle_internal_error(struct mlx5_ib_dev *ibdev)
249989ea94a7SMaor Gottlieb {
250089ea94a7SMaor Gottlieb 	struct mlx5_ib_qp *mqp;
250189ea94a7SMaor Gottlieb 	struct mlx5_ib_cq *send_mcq, *recv_mcq;
250289ea94a7SMaor Gottlieb 	struct mlx5_core_cq *mcq;
250389ea94a7SMaor Gottlieb 	struct list_head cq_armed_list;
250489ea94a7SMaor Gottlieb 	unsigned long flags_qp;
250589ea94a7SMaor Gottlieb 	unsigned long flags_cq;
250689ea94a7SMaor Gottlieb 	unsigned long flags;
250789ea94a7SMaor Gottlieb 
250889ea94a7SMaor Gottlieb 	INIT_LIST_HEAD(&cq_armed_list);
250989ea94a7SMaor Gottlieb 
251089ea94a7SMaor Gottlieb 	/* Go over qp list reside on that ibdev, sync with create/destroy qp.*/
251189ea94a7SMaor Gottlieb 	spin_lock_irqsave(&ibdev->reset_flow_resource_lock, flags);
251289ea94a7SMaor Gottlieb 	list_for_each_entry(mqp, &ibdev->qp_list, qps_list) {
251389ea94a7SMaor Gottlieb 		spin_lock_irqsave(&mqp->sq.lock, flags_qp);
251489ea94a7SMaor Gottlieb 		if (mqp->sq.tail != mqp->sq.head) {
251589ea94a7SMaor Gottlieb 			send_mcq = to_mcq(mqp->ibqp.send_cq);
251689ea94a7SMaor Gottlieb 			spin_lock_irqsave(&send_mcq->lock, flags_cq);
251789ea94a7SMaor Gottlieb 			if (send_mcq->mcq.comp &&
251889ea94a7SMaor Gottlieb 			    mqp->ibqp.send_cq->comp_handler) {
251989ea94a7SMaor Gottlieb 				if (!send_mcq->mcq.reset_notify_added) {
252089ea94a7SMaor Gottlieb 					send_mcq->mcq.reset_notify_added = 1;
252189ea94a7SMaor Gottlieb 					list_add_tail(&send_mcq->mcq.reset_notify,
252289ea94a7SMaor Gottlieb 						      &cq_armed_list);
252389ea94a7SMaor Gottlieb 				}
252489ea94a7SMaor Gottlieb 			}
252589ea94a7SMaor Gottlieb 			spin_unlock_irqrestore(&send_mcq->lock, flags_cq);
252689ea94a7SMaor Gottlieb 		}
252789ea94a7SMaor Gottlieb 		spin_unlock_irqrestore(&mqp->sq.lock, flags_qp);
252889ea94a7SMaor Gottlieb 		spin_lock_irqsave(&mqp->rq.lock, flags_qp);
252989ea94a7SMaor Gottlieb 		/* no handling is needed for SRQ */
253089ea94a7SMaor Gottlieb 		if (!mqp->ibqp.srq) {
253189ea94a7SMaor Gottlieb 			if (mqp->rq.tail != mqp->rq.head) {
253289ea94a7SMaor Gottlieb 				recv_mcq = to_mcq(mqp->ibqp.recv_cq);
253389ea94a7SMaor Gottlieb 				spin_lock_irqsave(&recv_mcq->lock, flags_cq);
253489ea94a7SMaor Gottlieb 				if (recv_mcq->mcq.comp &&
253589ea94a7SMaor Gottlieb 				    mqp->ibqp.recv_cq->comp_handler) {
253689ea94a7SMaor Gottlieb 					if (!recv_mcq->mcq.reset_notify_added) {
253789ea94a7SMaor Gottlieb 						recv_mcq->mcq.reset_notify_added = 1;
253889ea94a7SMaor Gottlieb 						list_add_tail(&recv_mcq->mcq.reset_notify,
253989ea94a7SMaor Gottlieb 							      &cq_armed_list);
254089ea94a7SMaor Gottlieb 					}
254189ea94a7SMaor Gottlieb 				}
254289ea94a7SMaor Gottlieb 				spin_unlock_irqrestore(&recv_mcq->lock,
254389ea94a7SMaor Gottlieb 						       flags_cq);
254489ea94a7SMaor Gottlieb 			}
254589ea94a7SMaor Gottlieb 		}
254689ea94a7SMaor Gottlieb 		spin_unlock_irqrestore(&mqp->rq.lock, flags_qp);
254789ea94a7SMaor Gottlieb 	}
254889ea94a7SMaor Gottlieb 	/*At that point all inflight post send were put to be executed as of we
254989ea94a7SMaor Gottlieb 	 * lock/unlock above locks Now need to arm all involved CQs.
255089ea94a7SMaor Gottlieb 	 */
255189ea94a7SMaor Gottlieb 	list_for_each_entry(mcq, &cq_armed_list, reset_notify) {
255289ea94a7SMaor Gottlieb 		mcq->comp(mcq);
255389ea94a7SMaor Gottlieb 	}
255489ea94a7SMaor Gottlieb 	spin_unlock_irqrestore(&ibdev->reset_flow_resource_lock, flags);
255589ea94a7SMaor Gottlieb }
255689ea94a7SMaor Gottlieb 
25579603b61dSJack Morgenstein static void mlx5_ib_event(struct mlx5_core_dev *dev, void *context,
25584d2f9bbbSJack Morgenstein 			  enum mlx5_dev_event event, unsigned long param)
2559e126ba97SEli Cohen {
25609603b61dSJack Morgenstein 	struct mlx5_ib_dev *ibdev = (struct mlx5_ib_dev *)context;
2561e126ba97SEli Cohen 	struct ib_event ibev;
2562dbaaff2aSEli Cohen 	bool fatal = false;
2563e126ba97SEli Cohen 	u8 port = 0;
2564e126ba97SEli Cohen 
2565e126ba97SEli Cohen 	switch (event) {
2566e126ba97SEli Cohen 	case MLX5_DEV_EVENT_SYS_ERROR:
2567e126ba97SEli Cohen 		ibev.event = IB_EVENT_DEVICE_FATAL;
256889ea94a7SMaor Gottlieb 		mlx5_ib_handle_internal_error(ibdev);
2569dbaaff2aSEli Cohen 		fatal = true;
2570e126ba97SEli Cohen 		break;
2571e126ba97SEli Cohen 
2572e126ba97SEli Cohen 	case MLX5_DEV_EVENT_PORT_UP:
2573e126ba97SEli Cohen 	case MLX5_DEV_EVENT_PORT_DOWN:
25742788cf3bSNoa Osherovich 	case MLX5_DEV_EVENT_PORT_INITIALIZED:
25754d2f9bbbSJack Morgenstein 		port = (u8)param;
25765ec8c83eSAviv Heller 
25775ec8c83eSAviv Heller 		/* In RoCE, port up/down events are handled in
25785ec8c83eSAviv Heller 		 * mlx5_netdev_event().
25795ec8c83eSAviv Heller 		 */
25805ec8c83eSAviv Heller 		if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) ==
25815ec8c83eSAviv Heller 			IB_LINK_LAYER_ETHERNET)
25825ec8c83eSAviv Heller 			return;
25835ec8c83eSAviv Heller 
25845ec8c83eSAviv Heller 		ibev.event = (event == MLX5_DEV_EVENT_PORT_UP) ?
25855ec8c83eSAviv Heller 			     IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
2586e126ba97SEli Cohen 		break;
2587e126ba97SEli Cohen 
2588e126ba97SEli Cohen 	case MLX5_DEV_EVENT_LID_CHANGE:
2589e126ba97SEli Cohen 		ibev.event = IB_EVENT_LID_CHANGE;
25904d2f9bbbSJack Morgenstein 		port = (u8)param;
2591e126ba97SEli Cohen 		break;
2592e126ba97SEli Cohen 
2593e126ba97SEli Cohen 	case MLX5_DEV_EVENT_PKEY_CHANGE:
2594e126ba97SEli Cohen 		ibev.event = IB_EVENT_PKEY_CHANGE;
25954d2f9bbbSJack Morgenstein 		port = (u8)param;
25967722f47eSHaggai Eran 
25977722f47eSHaggai Eran 		schedule_work(&ibdev->devr.ports[port - 1].pkey_change_work);
2598e126ba97SEli Cohen 		break;
2599e126ba97SEli Cohen 
2600e126ba97SEli Cohen 	case MLX5_DEV_EVENT_GUID_CHANGE:
2601e126ba97SEli Cohen 		ibev.event = IB_EVENT_GID_CHANGE;
26024d2f9bbbSJack Morgenstein 		port = (u8)param;
2603e126ba97SEli Cohen 		break;
2604e126ba97SEli Cohen 
2605e126ba97SEli Cohen 	case MLX5_DEV_EVENT_CLIENT_REREG:
2606e126ba97SEli Cohen 		ibev.event = IB_EVENT_CLIENT_REREGISTER;
26074d2f9bbbSJack Morgenstein 		port = (u8)param;
2608e126ba97SEli Cohen 		break;
2609bdc37924SSaeed Mahameed 	default:
2610bdc37924SSaeed Mahameed 		return;
2611e126ba97SEli Cohen 	}
2612e126ba97SEli Cohen 
2613e126ba97SEli Cohen 	ibev.device	      = &ibdev->ib_dev;
2614e126ba97SEli Cohen 	ibev.element.port_num = port;
2615e126ba97SEli Cohen 
2616a0c84c32SEli Cohen 	if (port < 1 || port > ibdev->num_ports) {
2617a0c84c32SEli Cohen 		mlx5_ib_warn(ibdev, "warning: event on port %d\n", port);
2618a0c84c32SEli Cohen 		return;
2619a0c84c32SEli Cohen 	}
2620a0c84c32SEli Cohen 
2621e126ba97SEli Cohen 	if (ibdev->ib_active)
2622e126ba97SEli Cohen 		ib_dispatch_event(&ibev);
2623dbaaff2aSEli Cohen 
2624dbaaff2aSEli Cohen 	if (fatal)
2625dbaaff2aSEli Cohen 		ibdev->ib_active = false;
2626e126ba97SEli Cohen }
2627e126ba97SEli Cohen 
2628c43f1112SMaor Gottlieb static int set_has_smi_cap(struct mlx5_ib_dev *dev)
2629c43f1112SMaor Gottlieb {
2630c43f1112SMaor Gottlieb 	struct mlx5_hca_vport_context vport_ctx;
2631c43f1112SMaor Gottlieb 	int err;
2632c43f1112SMaor Gottlieb 	int port;
2633c43f1112SMaor Gottlieb 
2634c43f1112SMaor Gottlieb 	for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++) {
2635c43f1112SMaor Gottlieb 		dev->mdev->port_caps[port - 1].has_smi = false;
2636c43f1112SMaor Gottlieb 		if (MLX5_CAP_GEN(dev->mdev, port_type) ==
2637c43f1112SMaor Gottlieb 		    MLX5_CAP_PORT_TYPE_IB) {
2638c43f1112SMaor Gottlieb 			if (MLX5_CAP_GEN(dev->mdev, ib_virt)) {
2639c43f1112SMaor Gottlieb 				err = mlx5_query_hca_vport_context(dev->mdev, 0,
2640c43f1112SMaor Gottlieb 								   port, 0,
2641c43f1112SMaor Gottlieb 								   &vport_ctx);
2642c43f1112SMaor Gottlieb 				if (err) {
2643c43f1112SMaor Gottlieb 					mlx5_ib_err(dev, "query_hca_vport_context for port=%d failed %d\n",
2644c43f1112SMaor Gottlieb 						    port, err);
2645c43f1112SMaor Gottlieb 					return err;
2646c43f1112SMaor Gottlieb 				}
2647c43f1112SMaor Gottlieb 				dev->mdev->port_caps[port - 1].has_smi =
2648c43f1112SMaor Gottlieb 					vport_ctx.has_smi;
2649c43f1112SMaor Gottlieb 			} else {
2650c43f1112SMaor Gottlieb 				dev->mdev->port_caps[port - 1].has_smi = true;
2651c43f1112SMaor Gottlieb 			}
2652c43f1112SMaor Gottlieb 		}
2653c43f1112SMaor Gottlieb 	}
2654c43f1112SMaor Gottlieb 	return 0;
2655c43f1112SMaor Gottlieb }
2656c43f1112SMaor Gottlieb 
2657e126ba97SEli Cohen static void get_ext_port_caps(struct mlx5_ib_dev *dev)
2658e126ba97SEli Cohen {
2659e126ba97SEli Cohen 	int port;
2660e126ba97SEli Cohen 
2661938fe83cSSaeed Mahameed 	for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++)
2662e126ba97SEli Cohen 		mlx5_query_ext_port_caps(dev, port);
2663e126ba97SEli Cohen }
2664e126ba97SEli Cohen 
2665e126ba97SEli Cohen static int get_port_caps(struct mlx5_ib_dev *dev)
2666e126ba97SEli Cohen {
2667e126ba97SEli Cohen 	struct ib_device_attr *dprops = NULL;
2668e126ba97SEli Cohen 	struct ib_port_attr *pprops = NULL;
2669f614fc15SDan Carpenter 	int err = -ENOMEM;
2670e126ba97SEli Cohen 	int port;
26712528e33eSMatan Barak 	struct ib_udata uhw = {.inlen = 0, .outlen = 0};
2672e126ba97SEli Cohen 
2673e126ba97SEli Cohen 	pprops = kmalloc(sizeof(*pprops), GFP_KERNEL);
2674e126ba97SEli Cohen 	if (!pprops)
2675e126ba97SEli Cohen 		goto out;
2676e126ba97SEli Cohen 
2677e126ba97SEli Cohen 	dprops = kmalloc(sizeof(*dprops), GFP_KERNEL);
2678e126ba97SEli Cohen 	if (!dprops)
2679e126ba97SEli Cohen 		goto out;
2680e126ba97SEli Cohen 
2681c43f1112SMaor Gottlieb 	err = set_has_smi_cap(dev);
2682c43f1112SMaor Gottlieb 	if (err)
2683c43f1112SMaor Gottlieb 		goto out;
2684c43f1112SMaor Gottlieb 
26852528e33eSMatan Barak 	err = mlx5_ib_query_device(&dev->ib_dev, dprops, &uhw);
2686e126ba97SEli Cohen 	if (err) {
2687e126ba97SEli Cohen 		mlx5_ib_warn(dev, "query_device failed %d\n", err);
2688e126ba97SEli Cohen 		goto out;
2689e126ba97SEli Cohen 	}
2690e126ba97SEli Cohen 
2691938fe83cSSaeed Mahameed 	for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++) {
2692c4550c63SOr Gerlitz 		memset(pprops, 0, sizeof(*pprops));
2693e126ba97SEli Cohen 		err = mlx5_ib_query_port(&dev->ib_dev, port, pprops);
2694e126ba97SEli Cohen 		if (err) {
2695938fe83cSSaeed Mahameed 			mlx5_ib_warn(dev, "query_port %d failed %d\n",
2696938fe83cSSaeed Mahameed 				     port, err);
2697e126ba97SEli Cohen 			break;
2698e126ba97SEli Cohen 		}
2699938fe83cSSaeed Mahameed 		dev->mdev->port_caps[port - 1].pkey_table_len =
2700938fe83cSSaeed Mahameed 						dprops->max_pkeys;
2701938fe83cSSaeed Mahameed 		dev->mdev->port_caps[port - 1].gid_table_len =
2702938fe83cSSaeed Mahameed 						pprops->gid_tbl_len;
2703e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "pkey_table_len %d, gid_table_len %d\n",
2704e126ba97SEli Cohen 			    dprops->max_pkeys, pprops->gid_tbl_len);
2705e126ba97SEli Cohen 	}
2706e126ba97SEli Cohen 
2707e126ba97SEli Cohen out:
2708e126ba97SEli Cohen 	kfree(pprops);
2709e126ba97SEli Cohen 	kfree(dprops);
2710e126ba97SEli Cohen 
2711e126ba97SEli Cohen 	return err;
2712e126ba97SEli Cohen }
2713e126ba97SEli Cohen 
2714e126ba97SEli Cohen static void destroy_umrc_res(struct mlx5_ib_dev *dev)
2715e126ba97SEli Cohen {
2716e126ba97SEli Cohen 	int err;
2717e126ba97SEli Cohen 
2718e126ba97SEli Cohen 	err = mlx5_mr_cache_cleanup(dev);
2719e126ba97SEli Cohen 	if (err)
2720e126ba97SEli Cohen 		mlx5_ib_warn(dev, "mr cache cleanup failed\n");
2721e126ba97SEli Cohen 
2722e126ba97SEli Cohen 	mlx5_ib_destroy_qp(dev->umrc.qp);
2723add08d76SChristoph Hellwig 	ib_free_cq(dev->umrc.cq);
2724e126ba97SEli Cohen 	ib_dealloc_pd(dev->umrc.pd);
2725e126ba97SEli Cohen }
2726e126ba97SEli Cohen 
2727e126ba97SEli Cohen enum {
2728e126ba97SEli Cohen 	MAX_UMR_WR = 128,
2729e126ba97SEli Cohen };
2730e126ba97SEli Cohen 
2731e126ba97SEli Cohen static int create_umr_res(struct mlx5_ib_dev *dev)
2732e126ba97SEli Cohen {
2733e126ba97SEli Cohen 	struct ib_qp_init_attr *init_attr = NULL;
2734e126ba97SEli Cohen 	struct ib_qp_attr *attr = NULL;
2735e126ba97SEli Cohen 	struct ib_pd *pd;
2736e126ba97SEli Cohen 	struct ib_cq *cq;
2737e126ba97SEli Cohen 	struct ib_qp *qp;
2738e126ba97SEli Cohen 	int ret;
2739e126ba97SEli Cohen 
2740e126ba97SEli Cohen 	attr = kzalloc(sizeof(*attr), GFP_KERNEL);
2741e126ba97SEli Cohen 	init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL);
2742e126ba97SEli Cohen 	if (!attr || !init_attr) {
2743e126ba97SEli Cohen 		ret = -ENOMEM;
2744e126ba97SEli Cohen 		goto error_0;
2745e126ba97SEli Cohen 	}
2746e126ba97SEli Cohen 
2747ed082d36SChristoph Hellwig 	pd = ib_alloc_pd(&dev->ib_dev, 0);
2748e126ba97SEli Cohen 	if (IS_ERR(pd)) {
2749e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create PD for sync UMR QP\n");
2750e126ba97SEli Cohen 		ret = PTR_ERR(pd);
2751e126ba97SEli Cohen 		goto error_0;
2752e126ba97SEli Cohen 	}
2753e126ba97SEli Cohen 
2754add08d76SChristoph Hellwig 	cq = ib_alloc_cq(&dev->ib_dev, NULL, 128, 0, IB_POLL_SOFTIRQ);
2755e126ba97SEli Cohen 	if (IS_ERR(cq)) {
2756e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create CQ for sync UMR QP\n");
2757e126ba97SEli Cohen 		ret = PTR_ERR(cq);
2758e126ba97SEli Cohen 		goto error_2;
2759e126ba97SEli Cohen 	}
2760e126ba97SEli Cohen 
2761e126ba97SEli Cohen 	init_attr->send_cq = cq;
2762e126ba97SEli Cohen 	init_attr->recv_cq = cq;
2763e126ba97SEli Cohen 	init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
2764e126ba97SEli Cohen 	init_attr->cap.max_send_wr = MAX_UMR_WR;
2765e126ba97SEli Cohen 	init_attr->cap.max_send_sge = 1;
2766e126ba97SEli Cohen 	init_attr->qp_type = MLX5_IB_QPT_REG_UMR;
2767e126ba97SEli Cohen 	init_attr->port_num = 1;
2768e126ba97SEli Cohen 	qp = mlx5_ib_create_qp(pd, init_attr, NULL);
2769e126ba97SEli Cohen 	if (IS_ERR(qp)) {
2770e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create sync UMR QP\n");
2771e126ba97SEli Cohen 		ret = PTR_ERR(qp);
2772e126ba97SEli Cohen 		goto error_3;
2773e126ba97SEli Cohen 	}
2774e126ba97SEli Cohen 	qp->device     = &dev->ib_dev;
2775e126ba97SEli Cohen 	qp->real_qp    = qp;
2776e126ba97SEli Cohen 	qp->uobject    = NULL;
2777e126ba97SEli Cohen 	qp->qp_type    = MLX5_IB_QPT_REG_UMR;
2778e126ba97SEli Cohen 
2779e126ba97SEli Cohen 	attr->qp_state = IB_QPS_INIT;
2780e126ba97SEli Cohen 	attr->port_num = 1;
2781e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_PKEY_INDEX |
2782e126ba97SEli Cohen 				IB_QP_PORT, NULL);
2783e126ba97SEli Cohen 	if (ret) {
2784e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify UMR QP\n");
2785e126ba97SEli Cohen 		goto error_4;
2786e126ba97SEli Cohen 	}
2787e126ba97SEli Cohen 
2788e126ba97SEli Cohen 	memset(attr, 0, sizeof(*attr));
2789e126ba97SEli Cohen 	attr->qp_state = IB_QPS_RTR;
2790e126ba97SEli Cohen 	attr->path_mtu = IB_MTU_256;
2791e126ba97SEli Cohen 
2792e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
2793e126ba97SEli Cohen 	if (ret) {
2794e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify umr QP to rtr\n");
2795e126ba97SEli Cohen 		goto error_4;
2796e126ba97SEli Cohen 	}
2797e126ba97SEli Cohen 
2798e126ba97SEli Cohen 	memset(attr, 0, sizeof(*attr));
2799e126ba97SEli Cohen 	attr->qp_state = IB_QPS_RTS;
2800e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
2801e126ba97SEli Cohen 	if (ret) {
2802e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify umr QP to rts\n");
2803e126ba97SEli Cohen 		goto error_4;
2804e126ba97SEli Cohen 	}
2805e126ba97SEli Cohen 
2806e126ba97SEli Cohen 	dev->umrc.qp = qp;
2807e126ba97SEli Cohen 	dev->umrc.cq = cq;
2808e126ba97SEli Cohen 	dev->umrc.pd = pd;
2809e126ba97SEli Cohen 
2810e126ba97SEli Cohen 	sema_init(&dev->umrc.sem, MAX_UMR_WR);
2811e126ba97SEli Cohen 	ret = mlx5_mr_cache_init(dev);
2812e126ba97SEli Cohen 	if (ret) {
2813e126ba97SEli Cohen 		mlx5_ib_warn(dev, "mr cache init failed %d\n", ret);
2814e126ba97SEli Cohen 		goto error_4;
2815e126ba97SEli Cohen 	}
2816e126ba97SEli Cohen 
2817e126ba97SEli Cohen 	kfree(attr);
2818e126ba97SEli Cohen 	kfree(init_attr);
2819e126ba97SEli Cohen 
2820e126ba97SEli Cohen 	return 0;
2821e126ba97SEli Cohen 
2822e126ba97SEli Cohen error_4:
2823e126ba97SEli Cohen 	mlx5_ib_destroy_qp(qp);
2824e126ba97SEli Cohen 
2825e126ba97SEli Cohen error_3:
2826add08d76SChristoph Hellwig 	ib_free_cq(cq);
2827e126ba97SEli Cohen 
2828e126ba97SEli Cohen error_2:
2829e126ba97SEli Cohen 	ib_dealloc_pd(pd);
2830e126ba97SEli Cohen 
2831e126ba97SEli Cohen error_0:
2832e126ba97SEli Cohen 	kfree(attr);
2833e126ba97SEli Cohen 	kfree(init_attr);
2834e126ba97SEli Cohen 	return ret;
2835e126ba97SEli Cohen }
2836e126ba97SEli Cohen 
2837e126ba97SEli Cohen static int create_dev_resources(struct mlx5_ib_resources *devr)
2838e126ba97SEli Cohen {
2839e126ba97SEli Cohen 	struct ib_srq_init_attr attr;
2840e126ba97SEli Cohen 	struct mlx5_ib_dev *dev;
2841bcf4c1eaSMatan Barak 	struct ib_cq_init_attr cq_attr = {.cqe = 1};
28427722f47eSHaggai Eran 	int port;
2843e126ba97SEli Cohen 	int ret = 0;
2844e126ba97SEli Cohen 
2845e126ba97SEli Cohen 	dev = container_of(devr, struct mlx5_ib_dev, devr);
2846e126ba97SEli Cohen 
2847d16e91daSHaggai Eran 	mutex_init(&devr->mutex);
2848d16e91daSHaggai Eran 
2849e126ba97SEli Cohen 	devr->p0 = mlx5_ib_alloc_pd(&dev->ib_dev, NULL, NULL);
2850e126ba97SEli Cohen 	if (IS_ERR(devr->p0)) {
2851e126ba97SEli Cohen 		ret = PTR_ERR(devr->p0);
2852e126ba97SEli Cohen 		goto error0;
2853e126ba97SEli Cohen 	}
2854e126ba97SEli Cohen 	devr->p0->device  = &dev->ib_dev;
2855e126ba97SEli Cohen 	devr->p0->uobject = NULL;
2856e126ba97SEli Cohen 	atomic_set(&devr->p0->usecnt, 0);
2857e126ba97SEli Cohen 
2858bcf4c1eaSMatan Barak 	devr->c0 = mlx5_ib_create_cq(&dev->ib_dev, &cq_attr, NULL, NULL);
2859e126ba97SEli Cohen 	if (IS_ERR(devr->c0)) {
2860e126ba97SEli Cohen 		ret = PTR_ERR(devr->c0);
2861e126ba97SEli Cohen 		goto error1;
2862e126ba97SEli Cohen 	}
2863e126ba97SEli Cohen 	devr->c0->device        = &dev->ib_dev;
2864e126ba97SEli Cohen 	devr->c0->uobject       = NULL;
2865e126ba97SEli Cohen 	devr->c0->comp_handler  = NULL;
2866e126ba97SEli Cohen 	devr->c0->event_handler = NULL;
2867e126ba97SEli Cohen 	devr->c0->cq_context    = NULL;
2868e126ba97SEli Cohen 	atomic_set(&devr->c0->usecnt, 0);
2869e126ba97SEli Cohen 
2870e126ba97SEli Cohen 	devr->x0 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
2871e126ba97SEli Cohen 	if (IS_ERR(devr->x0)) {
2872e126ba97SEli Cohen 		ret = PTR_ERR(devr->x0);
2873e126ba97SEli Cohen 		goto error2;
2874e126ba97SEli Cohen 	}
2875e126ba97SEli Cohen 	devr->x0->device = &dev->ib_dev;
2876e126ba97SEli Cohen 	devr->x0->inode = NULL;
2877e126ba97SEli Cohen 	atomic_set(&devr->x0->usecnt, 0);
2878e126ba97SEli Cohen 	mutex_init(&devr->x0->tgt_qp_mutex);
2879e126ba97SEli Cohen 	INIT_LIST_HEAD(&devr->x0->tgt_qp_list);
2880e126ba97SEli Cohen 
2881e126ba97SEli Cohen 	devr->x1 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
2882e126ba97SEli Cohen 	if (IS_ERR(devr->x1)) {
2883e126ba97SEli Cohen 		ret = PTR_ERR(devr->x1);
2884e126ba97SEli Cohen 		goto error3;
2885e126ba97SEli Cohen 	}
2886e126ba97SEli Cohen 	devr->x1->device = &dev->ib_dev;
2887e126ba97SEli Cohen 	devr->x1->inode = NULL;
2888e126ba97SEli Cohen 	atomic_set(&devr->x1->usecnt, 0);
2889e126ba97SEli Cohen 	mutex_init(&devr->x1->tgt_qp_mutex);
2890e126ba97SEli Cohen 	INIT_LIST_HEAD(&devr->x1->tgt_qp_list);
2891e126ba97SEli Cohen 
2892e126ba97SEli Cohen 	memset(&attr, 0, sizeof(attr));
2893e126ba97SEli Cohen 	attr.attr.max_sge = 1;
2894e126ba97SEli Cohen 	attr.attr.max_wr = 1;
2895e126ba97SEli Cohen 	attr.srq_type = IB_SRQT_XRC;
2896e126ba97SEli Cohen 	attr.ext.xrc.cq = devr->c0;
2897e126ba97SEli Cohen 	attr.ext.xrc.xrcd = devr->x0;
2898e126ba97SEli Cohen 
2899e126ba97SEli Cohen 	devr->s0 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
2900e126ba97SEli Cohen 	if (IS_ERR(devr->s0)) {
2901e126ba97SEli Cohen 		ret = PTR_ERR(devr->s0);
2902e126ba97SEli Cohen 		goto error4;
2903e126ba97SEli Cohen 	}
2904e126ba97SEli Cohen 	devr->s0->device	= &dev->ib_dev;
2905e126ba97SEli Cohen 	devr->s0->pd		= devr->p0;
2906e126ba97SEli Cohen 	devr->s0->uobject       = NULL;
2907e126ba97SEli Cohen 	devr->s0->event_handler = NULL;
2908e126ba97SEli Cohen 	devr->s0->srq_context   = NULL;
2909e126ba97SEli Cohen 	devr->s0->srq_type      = IB_SRQT_XRC;
2910e126ba97SEli Cohen 	devr->s0->ext.xrc.xrcd	= devr->x0;
2911e126ba97SEli Cohen 	devr->s0->ext.xrc.cq	= devr->c0;
2912e126ba97SEli Cohen 	atomic_inc(&devr->s0->ext.xrc.xrcd->usecnt);
2913e126ba97SEli Cohen 	atomic_inc(&devr->s0->ext.xrc.cq->usecnt);
2914e126ba97SEli Cohen 	atomic_inc(&devr->p0->usecnt);
2915e126ba97SEli Cohen 	atomic_set(&devr->s0->usecnt, 0);
2916e126ba97SEli Cohen 
29174aa17b28SHaggai Abramonvsky 	memset(&attr, 0, sizeof(attr));
29184aa17b28SHaggai Abramonvsky 	attr.attr.max_sge = 1;
29194aa17b28SHaggai Abramonvsky 	attr.attr.max_wr = 1;
29204aa17b28SHaggai Abramonvsky 	attr.srq_type = IB_SRQT_BASIC;
29214aa17b28SHaggai Abramonvsky 	devr->s1 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
29224aa17b28SHaggai Abramonvsky 	if (IS_ERR(devr->s1)) {
29234aa17b28SHaggai Abramonvsky 		ret = PTR_ERR(devr->s1);
29244aa17b28SHaggai Abramonvsky 		goto error5;
29254aa17b28SHaggai Abramonvsky 	}
29264aa17b28SHaggai Abramonvsky 	devr->s1->device	= &dev->ib_dev;
29274aa17b28SHaggai Abramonvsky 	devr->s1->pd		= devr->p0;
29284aa17b28SHaggai Abramonvsky 	devr->s1->uobject       = NULL;
29294aa17b28SHaggai Abramonvsky 	devr->s1->event_handler = NULL;
29304aa17b28SHaggai Abramonvsky 	devr->s1->srq_context   = NULL;
29314aa17b28SHaggai Abramonvsky 	devr->s1->srq_type      = IB_SRQT_BASIC;
29324aa17b28SHaggai Abramonvsky 	devr->s1->ext.xrc.cq	= devr->c0;
29334aa17b28SHaggai Abramonvsky 	atomic_inc(&devr->p0->usecnt);
29344aa17b28SHaggai Abramonvsky 	atomic_set(&devr->s0->usecnt, 0);
29354aa17b28SHaggai Abramonvsky 
29367722f47eSHaggai Eran 	for (port = 0; port < ARRAY_SIZE(devr->ports); ++port) {
29377722f47eSHaggai Eran 		INIT_WORK(&devr->ports[port].pkey_change_work,
29387722f47eSHaggai Eran 			  pkey_change_handler);
29397722f47eSHaggai Eran 		devr->ports[port].devr = devr;
29407722f47eSHaggai Eran 	}
29417722f47eSHaggai Eran 
2942e126ba97SEli Cohen 	return 0;
2943e126ba97SEli Cohen 
29444aa17b28SHaggai Abramonvsky error5:
29454aa17b28SHaggai Abramonvsky 	mlx5_ib_destroy_srq(devr->s0);
2946e126ba97SEli Cohen error4:
2947e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x1);
2948e126ba97SEli Cohen error3:
2949e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x0);
2950e126ba97SEli Cohen error2:
2951e126ba97SEli Cohen 	mlx5_ib_destroy_cq(devr->c0);
2952e126ba97SEli Cohen error1:
2953e126ba97SEli Cohen 	mlx5_ib_dealloc_pd(devr->p0);
2954e126ba97SEli Cohen error0:
2955e126ba97SEli Cohen 	return ret;
2956e126ba97SEli Cohen }
2957e126ba97SEli Cohen 
2958e126ba97SEli Cohen static void destroy_dev_resources(struct mlx5_ib_resources *devr)
2959e126ba97SEli Cohen {
29607722f47eSHaggai Eran 	struct mlx5_ib_dev *dev =
29617722f47eSHaggai Eran 		container_of(devr, struct mlx5_ib_dev, devr);
29627722f47eSHaggai Eran 	int port;
29637722f47eSHaggai Eran 
29644aa17b28SHaggai Abramonvsky 	mlx5_ib_destroy_srq(devr->s1);
2965e126ba97SEli Cohen 	mlx5_ib_destroy_srq(devr->s0);
2966e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x0);
2967e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x1);
2968e126ba97SEli Cohen 	mlx5_ib_destroy_cq(devr->c0);
2969e126ba97SEli Cohen 	mlx5_ib_dealloc_pd(devr->p0);
29707722f47eSHaggai Eran 
29717722f47eSHaggai Eran 	/* Make sure no change P_Key work items are still executing */
29727722f47eSHaggai Eran 	for (port = 0; port < dev->num_ports; ++port)
29737722f47eSHaggai Eran 		cancel_work_sync(&devr->ports[port].pkey_change_work);
2974e126ba97SEli Cohen }
2975e126ba97SEli Cohen 
2976e53505a8SAchiad Shochat static u32 get_core_cap_flags(struct ib_device *ibdev)
2977e53505a8SAchiad Shochat {
2978e53505a8SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
2979e53505a8SAchiad Shochat 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, 1);
2980e53505a8SAchiad Shochat 	u8 l3_type_cap = MLX5_CAP_ROCE(dev->mdev, l3_type);
2981e53505a8SAchiad Shochat 	u8 roce_version_cap = MLX5_CAP_ROCE(dev->mdev, roce_version);
2982e53505a8SAchiad Shochat 	u32 ret = 0;
2983e53505a8SAchiad Shochat 
2984e53505a8SAchiad Shochat 	if (ll == IB_LINK_LAYER_INFINIBAND)
2985e53505a8SAchiad Shochat 		return RDMA_CORE_PORT_IBA_IB;
2986e53505a8SAchiad Shochat 
298772cd5717SOr Gerlitz 	ret = RDMA_CORE_PORT_RAW_PACKET;
298872cd5717SOr Gerlitz 
2989e53505a8SAchiad Shochat 	if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV4_CAP))
299072cd5717SOr Gerlitz 		return ret;
2991e53505a8SAchiad Shochat 
2992e53505a8SAchiad Shochat 	if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV6_CAP))
299372cd5717SOr Gerlitz 		return ret;
2994e53505a8SAchiad Shochat 
2995e53505a8SAchiad Shochat 	if (roce_version_cap & MLX5_ROCE_VERSION_1_CAP)
2996e53505a8SAchiad Shochat 		ret |= RDMA_CORE_PORT_IBA_ROCE;
2997e53505a8SAchiad Shochat 
2998e53505a8SAchiad Shochat 	if (roce_version_cap & MLX5_ROCE_VERSION_2_CAP)
2999e53505a8SAchiad Shochat 		ret |= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
3000e53505a8SAchiad Shochat 
3001e53505a8SAchiad Shochat 	return ret;
3002e53505a8SAchiad Shochat }
3003e53505a8SAchiad Shochat 
30047738613eSIra Weiny static int mlx5_port_immutable(struct ib_device *ibdev, u8 port_num,
30057738613eSIra Weiny 			       struct ib_port_immutable *immutable)
30067738613eSIra Weiny {
30077738613eSIra Weiny 	struct ib_port_attr attr;
3008ca5b91d6SOr Gerlitz 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
3009ca5b91d6SOr Gerlitz 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, port_num);
30107738613eSIra Weiny 	int err;
30117738613eSIra Weiny 
3012c4550c63SOr Gerlitz 	immutable->core_cap_flags = get_core_cap_flags(ibdev);
3013c4550c63SOr Gerlitz 
3014c4550c63SOr Gerlitz 	err = ib_query_port(ibdev, port_num, &attr);
30157738613eSIra Weiny 	if (err)
30167738613eSIra Weiny 		return err;
30177738613eSIra Weiny 
30187738613eSIra Weiny 	immutable->pkey_tbl_len = attr.pkey_tbl_len;
30197738613eSIra Weiny 	immutable->gid_tbl_len = attr.gid_tbl_len;
3020e53505a8SAchiad Shochat 	immutable->core_cap_flags = get_core_cap_flags(ibdev);
3021ca5b91d6SOr Gerlitz 	if ((ll == IB_LINK_LAYER_INFINIBAND) || MLX5_CAP_GEN(dev->mdev, roce))
3022337877a4SIra Weiny 		immutable->max_mad_size = IB_MGMT_MAD_SIZE;
30237738613eSIra Weiny 
30247738613eSIra Weiny 	return 0;
30257738613eSIra Weiny }
30267738613eSIra Weiny 
3027c7342823SIra Weiny static void get_dev_fw_str(struct ib_device *ibdev, char *str,
3028c7342823SIra Weiny 			   size_t str_len)
3029c7342823SIra Weiny {
3030c7342823SIra Weiny 	struct mlx5_ib_dev *dev =
3031c7342823SIra Weiny 		container_of(ibdev, struct mlx5_ib_dev, ib_dev);
3032c7342823SIra Weiny 	snprintf(str, str_len, "%d.%d.%04d", fw_rev_maj(dev->mdev),
3033c7342823SIra Weiny 		       fw_rev_min(dev->mdev), fw_rev_sub(dev->mdev));
3034c7342823SIra Weiny }
3035c7342823SIra Weiny 
303645f95acdSOr Gerlitz static int mlx5_eth_lag_init(struct mlx5_ib_dev *dev)
30379ef9c640SAviv Heller {
30389ef9c640SAviv Heller 	struct mlx5_core_dev *mdev = dev->mdev;
30399ef9c640SAviv Heller 	struct mlx5_flow_namespace *ns = mlx5_get_flow_namespace(mdev,
30409ef9c640SAviv Heller 								 MLX5_FLOW_NAMESPACE_LAG);
30419ef9c640SAviv Heller 	struct mlx5_flow_table *ft;
30429ef9c640SAviv Heller 	int err;
30439ef9c640SAviv Heller 
30449ef9c640SAviv Heller 	if (!ns || !mlx5_lag_is_active(mdev))
30459ef9c640SAviv Heller 		return 0;
30469ef9c640SAviv Heller 
30479ef9c640SAviv Heller 	err = mlx5_cmd_create_vport_lag(mdev);
30489ef9c640SAviv Heller 	if (err)
30499ef9c640SAviv Heller 		return err;
30509ef9c640SAviv Heller 
30519ef9c640SAviv Heller 	ft = mlx5_create_lag_demux_flow_table(ns, 0, 0);
30529ef9c640SAviv Heller 	if (IS_ERR(ft)) {
30539ef9c640SAviv Heller 		err = PTR_ERR(ft);
30549ef9c640SAviv Heller 		goto err_destroy_vport_lag;
30559ef9c640SAviv Heller 	}
30569ef9c640SAviv Heller 
30579ef9c640SAviv Heller 	dev->flow_db.lag_demux_ft = ft;
30589ef9c640SAviv Heller 	return 0;
30599ef9c640SAviv Heller 
30609ef9c640SAviv Heller err_destroy_vport_lag:
30619ef9c640SAviv Heller 	mlx5_cmd_destroy_vport_lag(mdev);
30629ef9c640SAviv Heller 	return err;
30639ef9c640SAviv Heller }
30649ef9c640SAviv Heller 
306545f95acdSOr Gerlitz static void mlx5_eth_lag_cleanup(struct mlx5_ib_dev *dev)
30669ef9c640SAviv Heller {
30679ef9c640SAviv Heller 	struct mlx5_core_dev *mdev = dev->mdev;
30689ef9c640SAviv Heller 
30699ef9c640SAviv Heller 	if (dev->flow_db.lag_demux_ft) {
30709ef9c640SAviv Heller 		mlx5_destroy_flow_table(dev->flow_db.lag_demux_ft);
30719ef9c640SAviv Heller 		dev->flow_db.lag_demux_ft = NULL;
30729ef9c640SAviv Heller 
30739ef9c640SAviv Heller 		mlx5_cmd_destroy_vport_lag(mdev);
30749ef9c640SAviv Heller 	}
30759ef9c640SAviv Heller }
30769ef9c640SAviv Heller 
3077d012f5d6SOr Gerlitz static int mlx5_add_netdev_notifier(struct mlx5_ib_dev *dev)
3078fc24fc5eSAchiad Shochat {
3079e53505a8SAchiad Shochat 	int err;
3080e53505a8SAchiad Shochat 
3081fc24fc5eSAchiad Shochat 	dev->roce.nb.notifier_call = mlx5_netdev_event;
3082e53505a8SAchiad Shochat 	err = register_netdevice_notifier(&dev->roce.nb);
30835ec8c83eSAviv Heller 	if (err) {
30845ec8c83eSAviv Heller 		dev->roce.nb.notifier_call = NULL;
3085e53505a8SAchiad Shochat 		return err;
30865ec8c83eSAviv Heller 	}
3087e53505a8SAchiad Shochat 
3088d012f5d6SOr Gerlitz 	return 0;
3089d012f5d6SOr Gerlitz }
3090d012f5d6SOr Gerlitz 
3091d012f5d6SOr Gerlitz static void mlx5_remove_netdev_notifier(struct mlx5_ib_dev *dev)
3092e126ba97SEli Cohen {
3093e126ba97SEli Cohen 	if (dev->roce.nb.notifier_call) {
3094e126ba97SEli Cohen 		unregister_netdevice_notifier(&dev->roce.nb);
3095e126ba97SEli Cohen 		dev->roce.nb.notifier_call = NULL;
3096e126ba97SEli Cohen 	}
3097e126ba97SEli Cohen }
3098e126ba97SEli Cohen 
309945f95acdSOr Gerlitz static int mlx5_enable_eth(struct mlx5_ib_dev *dev)
3100e126ba97SEli Cohen {
3101e126ba97SEli Cohen 	int err;
3102e126ba97SEli Cohen 
3103d012f5d6SOr Gerlitz 	err = mlx5_add_netdev_notifier(dev);
3104d012f5d6SOr Gerlitz 	if (err)
3105e53505a8SAchiad Shochat 		return err;
3106e53505a8SAchiad Shochat 
3107ca5b91d6SOr Gerlitz 	if (MLX5_CAP_GEN(dev->mdev, roce)) {
3108e53505a8SAchiad Shochat 		err = mlx5_nic_vport_enable_roce(dev->mdev);
3109e53505a8SAchiad Shochat 		if (err)
3110e53505a8SAchiad Shochat 			goto err_unregister_netdevice_notifier;
3111ca5b91d6SOr Gerlitz 	}
3112e53505a8SAchiad Shochat 
311345f95acdSOr Gerlitz 	err = mlx5_eth_lag_init(dev);
31149ef9c640SAviv Heller 	if (err)
31159ef9c640SAviv Heller 		goto err_disable_roce;
31169ef9c640SAviv Heller 
3117e53505a8SAchiad Shochat 	return 0;
3118e53505a8SAchiad Shochat 
31199ef9c640SAviv Heller err_disable_roce:
3120ca5b91d6SOr Gerlitz 	if (MLX5_CAP_GEN(dev->mdev, roce))
31219ef9c640SAviv Heller 		mlx5_nic_vport_disable_roce(dev->mdev);
31229ef9c640SAviv Heller 
3123e53505a8SAchiad Shochat err_unregister_netdevice_notifier:
3124d012f5d6SOr Gerlitz 	mlx5_remove_netdev_notifier(dev);
3125e53505a8SAchiad Shochat 	return err;
3126fc24fc5eSAchiad Shochat }
3127fc24fc5eSAchiad Shochat 
312845f95acdSOr Gerlitz static void mlx5_disable_eth(struct mlx5_ib_dev *dev)
3129fc24fc5eSAchiad Shochat {
313045f95acdSOr Gerlitz 	mlx5_eth_lag_cleanup(dev);
3131ca5b91d6SOr Gerlitz 	if (MLX5_CAP_GEN(dev->mdev, roce))
3132e53505a8SAchiad Shochat 		mlx5_nic_vport_disable_roce(dev->mdev);
3133fc24fc5eSAchiad Shochat }
3134fc24fc5eSAchiad Shochat 
31357c16f477SKamal Heib struct mlx5_ib_q_counter {
31367c16f477SKamal Heib 	const char *name;
31377c16f477SKamal Heib 	size_t offset;
31387c16f477SKamal Heib };
31397c16f477SKamal Heib 
31407c16f477SKamal Heib #define INIT_Q_COUNTER(_name)		\
31417c16f477SKamal Heib 	{ .name = #_name, .offset = MLX5_BYTE_OFF(query_q_counter_out, _name)}
31427c16f477SKamal Heib 
31437c16f477SKamal Heib static const struct mlx5_ib_q_counter basic_q_cnts[] = {
31447c16f477SKamal Heib 	INIT_Q_COUNTER(rx_write_requests),
31457c16f477SKamal Heib 	INIT_Q_COUNTER(rx_read_requests),
31467c16f477SKamal Heib 	INIT_Q_COUNTER(rx_atomic_requests),
31477c16f477SKamal Heib 	INIT_Q_COUNTER(out_of_buffer),
31487c16f477SKamal Heib };
31497c16f477SKamal Heib 
31507c16f477SKamal Heib static const struct mlx5_ib_q_counter out_of_seq_q_cnts[] = {
31517c16f477SKamal Heib 	INIT_Q_COUNTER(out_of_sequence),
31527c16f477SKamal Heib };
31537c16f477SKamal Heib 
31547c16f477SKamal Heib static const struct mlx5_ib_q_counter retrans_q_cnts[] = {
31557c16f477SKamal Heib 	INIT_Q_COUNTER(duplicate_request),
31567c16f477SKamal Heib 	INIT_Q_COUNTER(rnr_nak_retry_err),
31577c16f477SKamal Heib 	INIT_Q_COUNTER(packet_seq_err),
31587c16f477SKamal Heib 	INIT_Q_COUNTER(implied_nak_seq_err),
31597c16f477SKamal Heib 	INIT_Q_COUNTER(local_ack_timeout_err),
31607c16f477SKamal Heib };
31617c16f477SKamal Heib 
31620837e86aSMark Bloch static void mlx5_ib_dealloc_q_counters(struct mlx5_ib_dev *dev)
31630837e86aSMark Bloch {
31640837e86aSMark Bloch 	unsigned int i;
31650837e86aSMark Bloch 
31667c16f477SKamal Heib 	for (i = 0; i < dev->num_ports; i++) {
31670837e86aSMark Bloch 		mlx5_core_dealloc_q_counter(dev->mdev,
31687c16f477SKamal Heib 					    dev->port[i].q_cnts.set_id);
31697c16f477SKamal Heib 		kfree(dev->port[i].q_cnts.names);
31707c16f477SKamal Heib 		kfree(dev->port[i].q_cnts.offsets);
31717c16f477SKamal Heib 	}
31727c16f477SKamal Heib }
31737c16f477SKamal Heib 
31747c16f477SKamal Heib static int __mlx5_ib_alloc_q_counters(struct mlx5_ib_dev *dev,
31757c16f477SKamal Heib 				      const char ***names,
31767c16f477SKamal Heib 				      size_t **offsets,
31777c16f477SKamal Heib 				      u32 *num)
31787c16f477SKamal Heib {
31797c16f477SKamal Heib 	u32 num_counters;
31807c16f477SKamal Heib 
31817c16f477SKamal Heib 	num_counters = ARRAY_SIZE(basic_q_cnts);
31827c16f477SKamal Heib 
31837c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt))
31847c16f477SKamal Heib 		num_counters += ARRAY_SIZE(out_of_seq_q_cnts);
31857c16f477SKamal Heib 
31867c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters))
31877c16f477SKamal Heib 		num_counters += ARRAY_SIZE(retrans_q_cnts);
31887c16f477SKamal Heib 
31897c16f477SKamal Heib 	*names = kcalloc(num_counters, sizeof(**names), GFP_KERNEL);
31907c16f477SKamal Heib 	if (!*names)
31917c16f477SKamal Heib 		return -ENOMEM;
31927c16f477SKamal Heib 
31937c16f477SKamal Heib 	*offsets = kcalloc(num_counters, sizeof(**offsets), GFP_KERNEL);
31947c16f477SKamal Heib 	if (!*offsets)
31957c16f477SKamal Heib 		goto err_names;
31967c16f477SKamal Heib 
31977c16f477SKamal Heib 	*num = num_counters;
31987c16f477SKamal Heib 
31997c16f477SKamal Heib 	return 0;
32007c16f477SKamal Heib 
32017c16f477SKamal Heib err_names:
32027c16f477SKamal Heib 	kfree(*names);
32037c16f477SKamal Heib 	return -ENOMEM;
32047c16f477SKamal Heib }
32057c16f477SKamal Heib 
32067c16f477SKamal Heib static void mlx5_ib_fill_q_counters(struct mlx5_ib_dev *dev,
32077c16f477SKamal Heib 				    const char **names,
32087c16f477SKamal Heib 				    size_t *offsets)
32097c16f477SKamal Heib {
32107c16f477SKamal Heib 	int i;
32117c16f477SKamal Heib 	int j = 0;
32127c16f477SKamal Heib 
32137c16f477SKamal Heib 	for (i = 0; i < ARRAY_SIZE(basic_q_cnts); i++, j++) {
32147c16f477SKamal Heib 		names[j] = basic_q_cnts[i].name;
32157c16f477SKamal Heib 		offsets[j] = basic_q_cnts[i].offset;
32167c16f477SKamal Heib 	}
32177c16f477SKamal Heib 
32187c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt)) {
32197c16f477SKamal Heib 		for (i = 0; i < ARRAY_SIZE(out_of_seq_q_cnts); i++, j++) {
32207c16f477SKamal Heib 			names[j] = out_of_seq_q_cnts[i].name;
32217c16f477SKamal Heib 			offsets[j] = out_of_seq_q_cnts[i].offset;
32227c16f477SKamal Heib 		}
32237c16f477SKamal Heib 	}
32247c16f477SKamal Heib 
32257c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters)) {
32267c16f477SKamal Heib 		for (i = 0; i < ARRAY_SIZE(retrans_q_cnts); i++, j++) {
32277c16f477SKamal Heib 			names[j] = retrans_q_cnts[i].name;
32287c16f477SKamal Heib 			offsets[j] = retrans_q_cnts[i].offset;
32297c16f477SKamal Heib 		}
32307c16f477SKamal Heib 	}
32310837e86aSMark Bloch }
32320837e86aSMark Bloch 
32330837e86aSMark Bloch static int mlx5_ib_alloc_q_counters(struct mlx5_ib_dev *dev)
32340837e86aSMark Bloch {
32350837e86aSMark Bloch 	int i;
32360837e86aSMark Bloch 	int ret;
32370837e86aSMark Bloch 
32380837e86aSMark Bloch 	for (i = 0; i < dev->num_ports; i++) {
32397c16f477SKamal Heib 		struct mlx5_ib_port *port = &dev->port[i];
32407c16f477SKamal Heib 
32410837e86aSMark Bloch 		ret = mlx5_core_alloc_q_counter(dev->mdev,
32427c16f477SKamal Heib 						&port->q_cnts.set_id);
32430837e86aSMark Bloch 		if (ret) {
32440837e86aSMark Bloch 			mlx5_ib_warn(dev,
32450837e86aSMark Bloch 				     "couldn't allocate queue counter for port %d, err %d\n",
32460837e86aSMark Bloch 				     i + 1, ret);
32470837e86aSMark Bloch 			goto dealloc_counters;
32480837e86aSMark Bloch 		}
32497c16f477SKamal Heib 
32507c16f477SKamal Heib 		ret = __mlx5_ib_alloc_q_counters(dev,
32517c16f477SKamal Heib 						 &port->q_cnts.names,
32527c16f477SKamal Heib 						 &port->q_cnts.offsets,
32537c16f477SKamal Heib 						 &port->q_cnts.num_counters);
32547c16f477SKamal Heib 		if (ret)
32557c16f477SKamal Heib 			goto dealloc_counters;
32567c16f477SKamal Heib 
32577c16f477SKamal Heib 		mlx5_ib_fill_q_counters(dev, port->q_cnts.names,
32587c16f477SKamal Heib 					port->q_cnts.offsets);
32590837e86aSMark Bloch 	}
32600837e86aSMark Bloch 
32610837e86aSMark Bloch 	return 0;
32620837e86aSMark Bloch 
32630837e86aSMark Bloch dealloc_counters:
32640837e86aSMark Bloch 	while (--i >= 0)
32650837e86aSMark Bloch 		mlx5_core_dealloc_q_counter(dev->mdev,
32667c16f477SKamal Heib 					    dev->port[i].q_cnts.set_id);
32670837e86aSMark Bloch 
32680837e86aSMark Bloch 	return ret;
32690837e86aSMark Bloch }
32700837e86aSMark Bloch 
32710ad17a8fSMark Bloch static struct rdma_hw_stats *mlx5_ib_alloc_hw_stats(struct ib_device *ibdev,
32720ad17a8fSMark Bloch 						    u8 port_num)
32730ad17a8fSMark Bloch {
32747c16f477SKamal Heib 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
32757c16f477SKamal Heib 	struct mlx5_ib_port *port = &dev->port[port_num - 1];
32760ad17a8fSMark Bloch 
32770ad17a8fSMark Bloch 	/* We support only per port stats */
32780ad17a8fSMark Bloch 	if (port_num == 0)
32790ad17a8fSMark Bloch 		return NULL;
32800ad17a8fSMark Bloch 
32817c16f477SKamal Heib 	return rdma_alloc_hw_stats_struct(port->q_cnts.names,
32827c16f477SKamal Heib 					  port->q_cnts.num_counters,
32830ad17a8fSMark Bloch 					  RDMA_HW_STATS_DEFAULT_LIFESPAN);
32840ad17a8fSMark Bloch }
32850ad17a8fSMark Bloch 
32860ad17a8fSMark Bloch static int mlx5_ib_get_hw_stats(struct ib_device *ibdev,
32870ad17a8fSMark Bloch 				struct rdma_hw_stats *stats,
32887c16f477SKamal Heib 				u8 port_num, int index)
32890ad17a8fSMark Bloch {
32900ad17a8fSMark Bloch 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
32917c16f477SKamal Heib 	struct mlx5_ib_port *port = &dev->port[port_num - 1];
32920ad17a8fSMark Bloch 	int outlen = MLX5_ST_SZ_BYTES(query_q_counter_out);
32930ad17a8fSMark Bloch 	void *out;
32940ad17a8fSMark Bloch 	__be32 val;
32950ad17a8fSMark Bloch 	int ret;
32960ad17a8fSMark Bloch 	int i;
32970ad17a8fSMark Bloch 
32987c16f477SKamal Heib 	if (!stats)
32990ad17a8fSMark Bloch 		return -ENOSYS;
33000ad17a8fSMark Bloch 
33010ad17a8fSMark Bloch 	out = mlx5_vzalloc(outlen);
33020ad17a8fSMark Bloch 	if (!out)
33030ad17a8fSMark Bloch 		return -ENOMEM;
33040ad17a8fSMark Bloch 
33050ad17a8fSMark Bloch 	ret = mlx5_core_query_q_counter(dev->mdev,
33067c16f477SKamal Heib 					port->q_cnts.set_id, 0,
33070ad17a8fSMark Bloch 					out, outlen);
33080ad17a8fSMark Bloch 	if (ret)
33090ad17a8fSMark Bloch 		goto free;
33100ad17a8fSMark Bloch 
33117c16f477SKamal Heib 	for (i = 0; i < port->q_cnts.num_counters; i++) {
33127c16f477SKamal Heib 		val = *(__be32 *)(out + port->q_cnts.offsets[i]);
33130ad17a8fSMark Bloch 		stats->value[i] = (u64)be32_to_cpu(val);
33140ad17a8fSMark Bloch 	}
33157c16f477SKamal Heib 
33160ad17a8fSMark Bloch free:
33170ad17a8fSMark Bloch 	kvfree(out);
33187c16f477SKamal Heib 	return port->q_cnts.num_counters;
33190ad17a8fSMark Bloch }
33200ad17a8fSMark Bloch 
33219603b61dSJack Morgenstein static void *mlx5_ib_add(struct mlx5_core_dev *mdev)
3322e126ba97SEli Cohen {
3323e126ba97SEli Cohen 	struct mlx5_ib_dev *dev;
3324ebd61f68SAchiad Shochat 	enum rdma_link_layer ll;
3325ebd61f68SAchiad Shochat 	int port_type_cap;
33264babcf97SAviv Heller 	const char *name;
3327e126ba97SEli Cohen 	int err;
3328e126ba97SEli Cohen 	int i;
3329e126ba97SEli Cohen 
3330ebd61f68SAchiad Shochat 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
3331ebd61f68SAchiad Shochat 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
3332ebd61f68SAchiad Shochat 
3333e126ba97SEli Cohen 	printk_once(KERN_INFO "%s", mlx5_version);
3334e126ba97SEli Cohen 
3335e126ba97SEli Cohen 	dev = (struct mlx5_ib_dev *)ib_alloc_device(sizeof(*dev));
3336e126ba97SEli Cohen 	if (!dev)
33379603b61dSJack Morgenstein 		return NULL;
3338e126ba97SEli Cohen 
33399603b61dSJack Morgenstein 	dev->mdev = mdev;
3340e126ba97SEli Cohen 
33410837e86aSMark Bloch 	dev->port = kcalloc(MLX5_CAP_GEN(mdev, num_ports), sizeof(*dev->port),
33420837e86aSMark Bloch 			    GFP_KERNEL);
33430837e86aSMark Bloch 	if (!dev->port)
33440837e86aSMark Bloch 		goto err_dealloc;
33450837e86aSMark Bloch 
3346fc24fc5eSAchiad Shochat 	rwlock_init(&dev->roce.netdev_lock);
3347e126ba97SEli Cohen 	err = get_port_caps(dev);
3348e126ba97SEli Cohen 	if (err)
33490837e86aSMark Bloch 		goto err_free_port;
3350e126ba97SEli Cohen 
33511b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(dev))
3352e126ba97SEli Cohen 		get_ext_port_caps(dev);
3353e126ba97SEli Cohen 
33544babcf97SAviv Heller 	if (!mlx5_lag_is_active(mdev))
33554babcf97SAviv Heller 		name = "mlx5_%d";
33564babcf97SAviv Heller 	else
33574babcf97SAviv Heller 		name = "mlx5_bond_%d";
33584babcf97SAviv Heller 
33594babcf97SAviv Heller 	strlcpy(dev->ib_dev.name, name, IB_DEVICE_NAME_MAX);
3360e126ba97SEli Cohen 	dev->ib_dev.owner		= THIS_MODULE;
3361e126ba97SEli Cohen 	dev->ib_dev.node_type		= RDMA_NODE_IB_CA;
3362c6790aa9SSagi Grimberg 	dev->ib_dev.local_dma_lkey	= 0 /* not supported for now */;
3363938fe83cSSaeed Mahameed 	dev->num_ports		= MLX5_CAP_GEN(mdev, num_ports);
3364e126ba97SEli Cohen 	dev->ib_dev.phys_port_cnt     = dev->num_ports;
3365233d05d2SSaeed Mahameed 	dev->ib_dev.num_comp_vectors    =
3366233d05d2SSaeed Mahameed 		dev->mdev->priv.eq_table.num_comp_vectors;
33679b0c289eSBart Van Assche 	dev->ib_dev.dev.parent		= &mdev->pdev->dev;
3368e126ba97SEli Cohen 
3369e126ba97SEli Cohen 	dev->ib_dev.uverbs_abi_ver	= MLX5_IB_UVERBS_ABI_VERSION;
3370e126ba97SEli Cohen 	dev->ib_dev.uverbs_cmd_mask	=
3371e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_GET_CONTEXT)		|
3372e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_DEVICE)	|
3373e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_PORT)		|
3374e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_ALLOC_PD)		|
3375e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DEALLOC_PD)		|
337641c450fdSMoni Shoua 		(1ull << IB_USER_VERBS_CMD_CREATE_AH)		|
337741c450fdSMoni Shoua 		(1ull << IB_USER_VERBS_CMD_DESTROY_AH)		|
3378e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_REG_MR)		|
337956e11d62SNoa Osherovich 		(1ull << IB_USER_VERBS_CMD_REREG_MR)		|
3380e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DEREG_MR)		|
3381e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL)	|
3382e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_CQ)		|
3383e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_RESIZE_CQ)		|
3384e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_CQ)		|
3385e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_QP)		|
3386e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_MODIFY_QP)		|
3387e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_QP)		|
3388e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_QP)		|
3389e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_ATTACH_MCAST)	|
3390e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DETACH_MCAST)	|
3391e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_SRQ)		|
3392e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_MODIFY_SRQ)		|
3393e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_SRQ)		|
3394e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_SRQ)		|
3395e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_XSRQ)		|
3396e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_OPEN_QP);
33971707cb4aSHaggai Eran 	dev->ib_dev.uverbs_ex_cmd_mask =
3398d4584ddfSMatan Barak 		(1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE)	|
3399d4584ddfSMatan Barak 		(1ull << IB_USER_VERBS_EX_CMD_CREATE_CQ)	|
34007d29f349SBodong Wang 		(1ull << IB_USER_VERBS_EX_CMD_CREATE_QP)	|
34017d29f349SBodong Wang 		(1ull << IB_USER_VERBS_EX_CMD_MODIFY_QP);
3402e126ba97SEli Cohen 
3403e126ba97SEli Cohen 	dev->ib_dev.query_device	= mlx5_ib_query_device;
3404e126ba97SEli Cohen 	dev->ib_dev.query_port		= mlx5_ib_query_port;
3405ebd61f68SAchiad Shochat 	dev->ib_dev.get_link_layer	= mlx5_ib_port_link_layer;
3406fc24fc5eSAchiad Shochat 	if (ll == IB_LINK_LAYER_ETHERNET)
3407fc24fc5eSAchiad Shochat 		dev->ib_dev.get_netdev	= mlx5_ib_get_netdev;
3408e126ba97SEli Cohen 	dev->ib_dev.query_gid		= mlx5_ib_query_gid;
34093cca2606SAchiad Shochat 	dev->ib_dev.add_gid		= mlx5_ib_add_gid;
34103cca2606SAchiad Shochat 	dev->ib_dev.del_gid		= mlx5_ib_del_gid;
3411e126ba97SEli Cohen 	dev->ib_dev.query_pkey		= mlx5_ib_query_pkey;
3412e126ba97SEli Cohen 	dev->ib_dev.modify_device	= mlx5_ib_modify_device;
3413e126ba97SEli Cohen 	dev->ib_dev.modify_port		= mlx5_ib_modify_port;
3414e126ba97SEli Cohen 	dev->ib_dev.alloc_ucontext	= mlx5_ib_alloc_ucontext;
3415e126ba97SEli Cohen 	dev->ib_dev.dealloc_ucontext	= mlx5_ib_dealloc_ucontext;
3416e126ba97SEli Cohen 	dev->ib_dev.mmap		= mlx5_ib_mmap;
3417e126ba97SEli Cohen 	dev->ib_dev.alloc_pd		= mlx5_ib_alloc_pd;
3418e126ba97SEli Cohen 	dev->ib_dev.dealloc_pd		= mlx5_ib_dealloc_pd;
3419e126ba97SEli Cohen 	dev->ib_dev.create_ah		= mlx5_ib_create_ah;
3420e126ba97SEli Cohen 	dev->ib_dev.query_ah		= mlx5_ib_query_ah;
3421e126ba97SEli Cohen 	dev->ib_dev.destroy_ah		= mlx5_ib_destroy_ah;
3422e126ba97SEli Cohen 	dev->ib_dev.create_srq		= mlx5_ib_create_srq;
3423e126ba97SEli Cohen 	dev->ib_dev.modify_srq		= mlx5_ib_modify_srq;
3424e126ba97SEli Cohen 	dev->ib_dev.query_srq		= mlx5_ib_query_srq;
3425e126ba97SEli Cohen 	dev->ib_dev.destroy_srq		= mlx5_ib_destroy_srq;
3426e126ba97SEli Cohen 	dev->ib_dev.post_srq_recv	= mlx5_ib_post_srq_recv;
3427e126ba97SEli Cohen 	dev->ib_dev.create_qp		= mlx5_ib_create_qp;
3428e126ba97SEli Cohen 	dev->ib_dev.modify_qp		= mlx5_ib_modify_qp;
3429e126ba97SEli Cohen 	dev->ib_dev.query_qp		= mlx5_ib_query_qp;
3430e126ba97SEli Cohen 	dev->ib_dev.destroy_qp		= mlx5_ib_destroy_qp;
3431e126ba97SEli Cohen 	dev->ib_dev.post_send		= mlx5_ib_post_send;
3432e126ba97SEli Cohen 	dev->ib_dev.post_recv		= mlx5_ib_post_recv;
3433e126ba97SEli Cohen 	dev->ib_dev.create_cq		= mlx5_ib_create_cq;
3434e126ba97SEli Cohen 	dev->ib_dev.modify_cq		= mlx5_ib_modify_cq;
3435e126ba97SEli Cohen 	dev->ib_dev.resize_cq		= mlx5_ib_resize_cq;
3436e126ba97SEli Cohen 	dev->ib_dev.destroy_cq		= mlx5_ib_destroy_cq;
3437e126ba97SEli Cohen 	dev->ib_dev.poll_cq		= mlx5_ib_poll_cq;
3438e126ba97SEli Cohen 	dev->ib_dev.req_notify_cq	= mlx5_ib_arm_cq;
3439e126ba97SEli Cohen 	dev->ib_dev.get_dma_mr		= mlx5_ib_get_dma_mr;
3440e126ba97SEli Cohen 	dev->ib_dev.reg_user_mr		= mlx5_ib_reg_user_mr;
344156e11d62SNoa Osherovich 	dev->ib_dev.rereg_user_mr	= mlx5_ib_rereg_user_mr;
3442e126ba97SEli Cohen 	dev->ib_dev.dereg_mr		= mlx5_ib_dereg_mr;
3443e126ba97SEli Cohen 	dev->ib_dev.attach_mcast	= mlx5_ib_mcg_attach;
3444e126ba97SEli Cohen 	dev->ib_dev.detach_mcast	= mlx5_ib_mcg_detach;
3445e126ba97SEli Cohen 	dev->ib_dev.process_mad		= mlx5_ib_process_mad;
34469bee178bSSagi Grimberg 	dev->ib_dev.alloc_mr		= mlx5_ib_alloc_mr;
34478a187ee5SSagi Grimberg 	dev->ib_dev.map_mr_sg		= mlx5_ib_map_mr_sg;
3448d5436ba0SSagi Grimberg 	dev->ib_dev.check_mr_status	= mlx5_ib_check_mr_status;
34497738613eSIra Weiny 	dev->ib_dev.get_port_immutable  = mlx5_port_immutable;
3450c7342823SIra Weiny 	dev->ib_dev.get_dev_fw_str      = get_dev_fw_str;
3451eff901d3SEli Cohen 	if (mlx5_core_is_pf(mdev)) {
3452eff901d3SEli Cohen 		dev->ib_dev.get_vf_config	= mlx5_ib_get_vf_config;
3453eff901d3SEli Cohen 		dev->ib_dev.set_vf_link_state	= mlx5_ib_set_vf_link_state;
3454eff901d3SEli Cohen 		dev->ib_dev.get_vf_stats	= mlx5_ib_get_vf_stats;
3455eff901d3SEli Cohen 		dev->ib_dev.set_vf_guid		= mlx5_ib_set_vf_guid;
3456eff901d3SEli Cohen 	}
3457e126ba97SEli Cohen 
34587c2344c3SMaor Gottlieb 	dev->ib_dev.disassociate_ucontext = mlx5_ib_disassociate_ucontext;
34597c2344c3SMaor Gottlieb 
3460938fe83cSSaeed Mahameed 	mlx5_ib_internal_fill_odp_caps(dev);
34618cdd312cSHaggai Eran 
3462d2370e0aSMatan Barak 	if (MLX5_CAP_GEN(mdev, imaicl)) {
3463d2370e0aSMatan Barak 		dev->ib_dev.alloc_mw		= mlx5_ib_alloc_mw;
3464d2370e0aSMatan Barak 		dev->ib_dev.dealloc_mw		= mlx5_ib_dealloc_mw;
3465d2370e0aSMatan Barak 		dev->ib_dev.uverbs_cmd_mask |=
3466d2370e0aSMatan Barak 			(1ull << IB_USER_VERBS_CMD_ALLOC_MW)	|
3467d2370e0aSMatan Barak 			(1ull << IB_USER_VERBS_CMD_DEALLOC_MW);
3468d2370e0aSMatan Barak 	}
3469d2370e0aSMatan Barak 
34707c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt)) {
34710ad17a8fSMark Bloch 		dev->ib_dev.get_hw_stats	= mlx5_ib_get_hw_stats;
34720ad17a8fSMark Bloch 		dev->ib_dev.alloc_hw_stats	= mlx5_ib_alloc_hw_stats;
34730ad17a8fSMark Bloch 	}
34740ad17a8fSMark Bloch 
3475938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, xrc)) {
3476e126ba97SEli Cohen 		dev->ib_dev.alloc_xrcd = mlx5_ib_alloc_xrcd;
3477e126ba97SEli Cohen 		dev->ib_dev.dealloc_xrcd = mlx5_ib_dealloc_xrcd;
3478e126ba97SEli Cohen 		dev->ib_dev.uverbs_cmd_mask |=
3479e126ba97SEli Cohen 			(1ull << IB_USER_VERBS_CMD_OPEN_XRCD) |
3480e126ba97SEli Cohen 			(1ull << IB_USER_VERBS_CMD_CLOSE_XRCD);
3481e126ba97SEli Cohen 	}
3482e126ba97SEli Cohen 
3483048ccca8SLinus Torvalds 	if (mlx5_ib_port_link_layer(&dev->ib_dev, 1) ==
3484038d2ef8SMaor Gottlieb 	    IB_LINK_LAYER_ETHERNET) {
3485038d2ef8SMaor Gottlieb 		dev->ib_dev.create_flow	= mlx5_ib_create_flow;
3486038d2ef8SMaor Gottlieb 		dev->ib_dev.destroy_flow = mlx5_ib_destroy_flow;
348779b20a6cSYishai Hadas 		dev->ib_dev.create_wq	 = mlx5_ib_create_wq;
348879b20a6cSYishai Hadas 		dev->ib_dev.modify_wq	 = mlx5_ib_modify_wq;
348979b20a6cSYishai Hadas 		dev->ib_dev.destroy_wq	 = mlx5_ib_destroy_wq;
3490c5f90929SYishai Hadas 		dev->ib_dev.create_rwq_ind_table = mlx5_ib_create_rwq_ind_table;
3491c5f90929SYishai Hadas 		dev->ib_dev.destroy_rwq_ind_table = mlx5_ib_destroy_rwq_ind_table;
3492038d2ef8SMaor Gottlieb 		dev->ib_dev.uverbs_ex_cmd_mask |=
3493038d2ef8SMaor Gottlieb 			(1ull << IB_USER_VERBS_EX_CMD_CREATE_FLOW) |
349479b20a6cSYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_DESTROY_FLOW) |
349579b20a6cSYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_CREATE_WQ) |
349679b20a6cSYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_MODIFY_WQ) |
3497c5f90929SYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_DESTROY_WQ) |
3498c5f90929SYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_CREATE_RWQ_IND_TBL) |
3499c5f90929SYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_DESTROY_RWQ_IND_TBL);
3500038d2ef8SMaor Gottlieb 	}
3501e126ba97SEli Cohen 	err = init_node_data(dev);
3502e126ba97SEli Cohen 	if (err)
350390be7c8aSMajd Dibbiny 		goto err_free_port;
3504e126ba97SEli Cohen 
3505038d2ef8SMaor Gottlieb 	mutex_init(&dev->flow_db.lock);
3506e126ba97SEli Cohen 	mutex_init(&dev->cap_mask_mutex);
350789ea94a7SMaor Gottlieb 	INIT_LIST_HEAD(&dev->qp_list);
350889ea94a7SMaor Gottlieb 	spin_lock_init(&dev->reset_flow_resource_lock);
3509e126ba97SEli Cohen 
3510fc24fc5eSAchiad Shochat 	if (ll == IB_LINK_LAYER_ETHERNET) {
351145f95acdSOr Gerlitz 		err = mlx5_enable_eth(dev);
3512e126ba97SEli Cohen 		if (err)
351390be7c8aSMajd Dibbiny 			goto err_free_port;
3514fc24fc5eSAchiad Shochat 	}
3515fc24fc5eSAchiad Shochat 
3516fc24fc5eSAchiad Shochat 	err = create_dev_resources(&dev->devr);
3517fc24fc5eSAchiad Shochat 	if (err)
351845f95acdSOr Gerlitz 		goto err_disable_eth;
3519e126ba97SEli Cohen 
35206aec21f6SHaggai Eran 	err = mlx5_ib_odp_init_one(dev);
3521281d1a92SWei Yongjun 	if (err)
3522e126ba97SEli Cohen 		goto err_rsrc;
3523e126ba97SEli Cohen 
352445bded2cSKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt)) {
35250837e86aSMark Bloch 		err = mlx5_ib_alloc_q_counters(dev);
35266aec21f6SHaggai Eran 		if (err)
35276aec21f6SHaggai Eran 			goto err_odp;
352845bded2cSKamal Heib 	}
35296aec21f6SHaggai Eran 
35305fe9dec0SEli Cohen 	dev->mdev->priv.uar = mlx5_get_uars_page(dev->mdev);
35315fe9dec0SEli Cohen 	if (!dev->mdev->priv.uar)
35325fe9dec0SEli Cohen 		goto err_q_cnt;
35335fe9dec0SEli Cohen 
35345fe9dec0SEli Cohen 	err = mlx5_alloc_bfreg(dev->mdev, &dev->bfreg, false, false);
35355fe9dec0SEli Cohen 	if (err)
35365fe9dec0SEli Cohen 		goto err_uar_page;
35375fe9dec0SEli Cohen 
35385fe9dec0SEli Cohen 	err = mlx5_alloc_bfreg(dev->mdev, &dev->fp_bfreg, false, true);
35395fe9dec0SEli Cohen 	if (err)
35405fe9dec0SEli Cohen 		goto err_bfreg;
35415fe9dec0SEli Cohen 
35420837e86aSMark Bloch 	err = ib_register_device(&dev->ib_dev, NULL);
35430837e86aSMark Bloch 	if (err)
35445fe9dec0SEli Cohen 		goto err_fp_bfreg;
35450837e86aSMark Bloch 
3546e126ba97SEli Cohen 	err = create_umr_res(dev);
3547e126ba97SEli Cohen 	if (err)
3548e126ba97SEli Cohen 		goto err_dev;
3549e126ba97SEli Cohen 
3550e126ba97SEli Cohen 	for (i = 0; i < ARRAY_SIZE(mlx5_class_attributes); i++) {
3551281d1a92SWei Yongjun 		err = device_create_file(&dev->ib_dev.dev,
3552281d1a92SWei Yongjun 					 mlx5_class_attributes[i]);
3553281d1a92SWei Yongjun 		if (err)
3554e126ba97SEli Cohen 			goto err_umrc;
3555e126ba97SEli Cohen 	}
3556e126ba97SEli Cohen 
3557e126ba97SEli Cohen 	dev->ib_active = true;
3558e126ba97SEli Cohen 
35599603b61dSJack Morgenstein 	return dev;
3560e126ba97SEli Cohen 
3561e126ba97SEli Cohen err_umrc:
3562e126ba97SEli Cohen 	destroy_umrc_res(dev);
3563e126ba97SEli Cohen 
3564e126ba97SEli Cohen err_dev:
3565e126ba97SEli Cohen 	ib_unregister_device(&dev->ib_dev);
3566e126ba97SEli Cohen 
35675fe9dec0SEli Cohen err_fp_bfreg:
35685fe9dec0SEli Cohen 	mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
35695fe9dec0SEli Cohen 
35705fe9dec0SEli Cohen err_bfreg:
35715fe9dec0SEli Cohen 	mlx5_free_bfreg(dev->mdev, &dev->bfreg);
35725fe9dec0SEli Cohen 
35735fe9dec0SEli Cohen err_uar_page:
35745fe9dec0SEli Cohen 	mlx5_put_uars_page(dev->mdev, dev->mdev->priv.uar);
35755fe9dec0SEli Cohen 
35760837e86aSMark Bloch err_q_cnt:
357745bded2cSKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt))
35780837e86aSMark Bloch 		mlx5_ib_dealloc_q_counters(dev);
35790837e86aSMark Bloch 
35806aec21f6SHaggai Eran err_odp:
35816aec21f6SHaggai Eran 	mlx5_ib_odp_remove_one(dev);
35826aec21f6SHaggai Eran 
3583e126ba97SEli Cohen err_rsrc:
3584e126ba97SEli Cohen 	destroy_dev_resources(&dev->devr);
3585e126ba97SEli Cohen 
358645f95acdSOr Gerlitz err_disable_eth:
35875ec8c83eSAviv Heller 	if (ll == IB_LINK_LAYER_ETHERNET) {
358845f95acdSOr Gerlitz 		mlx5_disable_eth(dev);
3589d012f5d6SOr Gerlitz 		mlx5_remove_netdev_notifier(dev);
35905ec8c83eSAviv Heller 	}
3591fc24fc5eSAchiad Shochat 
35920837e86aSMark Bloch err_free_port:
35930837e86aSMark Bloch 	kfree(dev->port);
35940837e86aSMark Bloch 
35959603b61dSJack Morgenstein err_dealloc:
3596e126ba97SEli Cohen 	ib_dealloc_device((struct ib_device *)dev);
3597e126ba97SEli Cohen 
35989603b61dSJack Morgenstein 	return NULL;
3599e126ba97SEli Cohen }
3600e126ba97SEli Cohen 
36019603b61dSJack Morgenstein static void mlx5_ib_remove(struct mlx5_core_dev *mdev, void *context)
3602e126ba97SEli Cohen {
36039603b61dSJack Morgenstein 	struct mlx5_ib_dev *dev = context;
3604fc24fc5eSAchiad Shochat 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev, 1);
36056aec21f6SHaggai Eran 
3606d012f5d6SOr Gerlitz 	mlx5_remove_netdev_notifier(dev);
3607e126ba97SEli Cohen 	ib_unregister_device(&dev->ib_dev);
36085fe9dec0SEli Cohen 	mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
36095fe9dec0SEli Cohen 	mlx5_free_bfreg(dev->mdev, &dev->bfreg);
36105fe9dec0SEli Cohen 	mlx5_put_uars_page(dev->mdev, mdev->priv.uar);
361145bded2cSKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt))
36120837e86aSMark Bloch 		mlx5_ib_dealloc_q_counters(dev);
3613eefd56e5SEli Cohen 	destroy_umrc_res(dev);
36146aec21f6SHaggai Eran 	mlx5_ib_odp_remove_one(dev);
3615e126ba97SEli Cohen 	destroy_dev_resources(&dev->devr);
3616fc24fc5eSAchiad Shochat 	if (ll == IB_LINK_LAYER_ETHERNET)
361745f95acdSOr Gerlitz 		mlx5_disable_eth(dev);
36180837e86aSMark Bloch 	kfree(dev->port);
3619e126ba97SEli Cohen 	ib_dealloc_device(&dev->ib_dev);
3620e126ba97SEli Cohen }
3621e126ba97SEli Cohen 
36229603b61dSJack Morgenstein static struct mlx5_interface mlx5_ib_interface = {
36239603b61dSJack Morgenstein 	.add            = mlx5_ib_add,
36249603b61dSJack Morgenstein 	.remove         = mlx5_ib_remove,
36259603b61dSJack Morgenstein 	.event          = mlx5_ib_event,
3626d9aaed83SArtemy Kovalyov #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
3627d9aaed83SArtemy Kovalyov 	.pfault		= mlx5_ib_pfault,
3628d9aaed83SArtemy Kovalyov #endif
362964613d94SSaeed Mahameed 	.protocol	= MLX5_INTERFACE_PROTOCOL_IB,
3630e126ba97SEli Cohen };
3631e126ba97SEli Cohen 
3632e126ba97SEli Cohen static int __init mlx5_ib_init(void)
3633e126ba97SEli Cohen {
36346aec21f6SHaggai Eran 	int err;
36356aec21f6SHaggai Eran 
363681713d37SArtemy Kovalyov 	mlx5_ib_odp_init();
36379603b61dSJack Morgenstein 
36386aec21f6SHaggai Eran 	err = mlx5_register_interface(&mlx5_ib_interface);
36396aec21f6SHaggai Eran 
36406aec21f6SHaggai Eran 	return err;
3641e126ba97SEli Cohen }
3642e126ba97SEli Cohen 
3643e126ba97SEli Cohen static void __exit mlx5_ib_cleanup(void)
3644e126ba97SEli Cohen {
36459603b61dSJack Morgenstein 	mlx5_unregister_interface(&mlx5_ib_interface);
3646e126ba97SEli Cohen }
3647e126ba97SEli Cohen 
3648e126ba97SEli Cohen module_init(mlx5_ib_init);
3649e126ba97SEli Cohen module_exit(mlx5_ib_cleanup);
3650