xref: /openbmc/linux/drivers/infiniband/hw/mlx5/main.c (revision 038d2ef87572757861a177b19f9d489def2c48b8)
1e126ba97SEli Cohen /*
26cf0a15fSSaeed Mahameed  * Copyright (c) 2013-2015, Mellanox Technologies. All rights reserved.
3e126ba97SEli Cohen  *
4e126ba97SEli Cohen  * This software is available to you under a choice of one of two
5e126ba97SEli Cohen  * licenses.  You may choose to be licensed under the terms of the GNU
6e126ba97SEli Cohen  * General Public License (GPL) Version 2, available from the file
7e126ba97SEli Cohen  * COPYING in the main directory of this source tree, or the
8e126ba97SEli Cohen  * OpenIB.org BSD license below:
9e126ba97SEli Cohen  *
10e126ba97SEli Cohen  *     Redistribution and use in source and binary forms, with or
11e126ba97SEli Cohen  *     without modification, are permitted provided that the following
12e126ba97SEli Cohen  *     conditions are met:
13e126ba97SEli Cohen  *
14e126ba97SEli Cohen  *      - Redistributions of source code must retain the above
15e126ba97SEli Cohen  *        copyright notice, this list of conditions and the following
16e126ba97SEli Cohen  *        disclaimer.
17e126ba97SEli Cohen  *
18e126ba97SEli Cohen  *      - Redistributions in binary form must reproduce the above
19e126ba97SEli Cohen  *        copyright notice, this list of conditions and the following
20e126ba97SEli Cohen  *        disclaimer in the documentation and/or other materials
21e126ba97SEli Cohen  *        provided with the distribution.
22e126ba97SEli Cohen  *
23e126ba97SEli Cohen  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24e126ba97SEli Cohen  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25e126ba97SEli Cohen  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26e126ba97SEli Cohen  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27e126ba97SEli Cohen  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28e126ba97SEli Cohen  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29e126ba97SEli Cohen  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30e126ba97SEli Cohen  * SOFTWARE.
31e126ba97SEli Cohen  */
32e126ba97SEli Cohen 
33adec640eSChristoph Hellwig #include <linux/highmem.h>
34e126ba97SEli Cohen #include <linux/module.h>
35e126ba97SEli Cohen #include <linux/init.h>
36e126ba97SEli Cohen #include <linux/errno.h>
37e126ba97SEli Cohen #include <linux/pci.h>
38e126ba97SEli Cohen #include <linux/dma-mapping.h>
39e126ba97SEli Cohen #include <linux/slab.h>
40e126ba97SEli Cohen #include <linux/io-mapping.h>
41e126ba97SEli Cohen #include <linux/sched.h>
42e126ba97SEli Cohen #include <rdma/ib_user_verbs.h>
431b5daf11SMajd Dibbiny #include <linux/mlx5/vport.h>
44e126ba97SEli Cohen #include <rdma/ib_smi.h>
45e126ba97SEli Cohen #include <rdma/ib_umem.h>
46*038d2ef8SMaor Gottlieb #include <linux/in.h>
47*038d2ef8SMaor Gottlieb #include <linux/etherdevice.h>
48*038d2ef8SMaor Gottlieb #include <linux/mlx5/fs.h>
49e126ba97SEli Cohen #include "user.h"
50e126ba97SEli Cohen #include "mlx5_ib.h"
51e126ba97SEli Cohen 
52e126ba97SEli Cohen #define DRIVER_NAME "mlx5_ib"
53169a1d85SAmir Vadai #define DRIVER_VERSION "2.2-1"
54169a1d85SAmir Vadai #define DRIVER_RELDATE	"Feb 2014"
55e126ba97SEli Cohen 
56e126ba97SEli Cohen MODULE_AUTHOR("Eli Cohen <eli@mellanox.com>");
57e126ba97SEli Cohen MODULE_DESCRIPTION("Mellanox Connect-IB HCA IB driver");
58e126ba97SEli Cohen MODULE_LICENSE("Dual BSD/GPL");
59e126ba97SEli Cohen MODULE_VERSION(DRIVER_VERSION);
60e126ba97SEli Cohen 
619603b61dSJack Morgenstein static int deprecated_prof_sel = 2;
629603b61dSJack Morgenstein module_param_named(prof_sel, deprecated_prof_sel, int, 0444);
639603b61dSJack Morgenstein MODULE_PARM_DESC(prof_sel, "profile selector. Deprecated here. Moved to module mlx5_core");
64e126ba97SEli Cohen 
65e126ba97SEli Cohen static char mlx5_version[] =
66e126ba97SEli Cohen 	DRIVER_NAME ": Mellanox Connect-IB Infiniband driver v"
67e126ba97SEli Cohen 	DRIVER_VERSION " (" DRIVER_RELDATE ")\n";
68e126ba97SEli Cohen 
691b5daf11SMajd Dibbiny static enum rdma_link_layer
701b5daf11SMajd Dibbiny mlx5_ib_port_link_layer(struct ib_device *device)
711b5daf11SMajd Dibbiny {
721b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(device);
731b5daf11SMajd Dibbiny 
741b5daf11SMajd Dibbiny 	switch (MLX5_CAP_GEN(dev->mdev, port_type)) {
751b5daf11SMajd Dibbiny 	case MLX5_CAP_PORT_TYPE_IB:
761b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_INFINIBAND;
771b5daf11SMajd Dibbiny 	case MLX5_CAP_PORT_TYPE_ETH:
781b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_ETHERNET;
791b5daf11SMajd Dibbiny 	default:
801b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_UNSPECIFIED;
811b5daf11SMajd Dibbiny 	}
821b5daf11SMajd Dibbiny }
831b5daf11SMajd Dibbiny 
841b5daf11SMajd Dibbiny static int mlx5_use_mad_ifc(struct mlx5_ib_dev *dev)
851b5daf11SMajd Dibbiny {
861b5daf11SMajd Dibbiny 	return !dev->mdev->issi;
871b5daf11SMajd Dibbiny }
881b5daf11SMajd Dibbiny 
891b5daf11SMajd Dibbiny enum {
901b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_MAD,
911b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_HCA,
921b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_NIC,
931b5daf11SMajd Dibbiny };
941b5daf11SMajd Dibbiny 
951b5daf11SMajd Dibbiny static int mlx5_get_vport_access_method(struct ib_device *ibdev)
961b5daf11SMajd Dibbiny {
971b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(to_mdev(ibdev)))
981b5daf11SMajd Dibbiny 		return MLX5_VPORT_ACCESS_METHOD_MAD;
991b5daf11SMajd Dibbiny 
1001b5daf11SMajd Dibbiny 	if (mlx5_ib_port_link_layer(ibdev) ==
1011b5daf11SMajd Dibbiny 	    IB_LINK_LAYER_ETHERNET)
1021b5daf11SMajd Dibbiny 		return MLX5_VPORT_ACCESS_METHOD_NIC;
1031b5daf11SMajd Dibbiny 
1041b5daf11SMajd Dibbiny 	return MLX5_VPORT_ACCESS_METHOD_HCA;
1051b5daf11SMajd Dibbiny }
1061b5daf11SMajd Dibbiny 
1071b5daf11SMajd Dibbiny static int mlx5_query_system_image_guid(struct ib_device *ibdev,
1081b5daf11SMajd Dibbiny 					__be64 *sys_image_guid)
1091b5daf11SMajd Dibbiny {
1101b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1111b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
1121b5daf11SMajd Dibbiny 	u64 tmp;
1131b5daf11SMajd Dibbiny 	int err;
1141b5daf11SMajd Dibbiny 
1151b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
1161b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
1171b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_system_image_guid(ibdev,
1181b5daf11SMajd Dibbiny 							    sys_image_guid);
1191b5daf11SMajd Dibbiny 
1201b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
1211b5daf11SMajd Dibbiny 		err = mlx5_query_hca_vport_system_image_guid(mdev, &tmp);
1221b5daf11SMajd Dibbiny 		if (!err)
1231b5daf11SMajd Dibbiny 			*sys_image_guid = cpu_to_be64(tmp);
1241b5daf11SMajd Dibbiny 		return err;
1251b5daf11SMajd Dibbiny 
1261b5daf11SMajd Dibbiny 	default:
1271b5daf11SMajd Dibbiny 		return -EINVAL;
1281b5daf11SMajd Dibbiny 	}
1291b5daf11SMajd Dibbiny }
1301b5daf11SMajd Dibbiny 
1311b5daf11SMajd Dibbiny static int mlx5_query_max_pkeys(struct ib_device *ibdev,
1321b5daf11SMajd Dibbiny 				u16 *max_pkeys)
1331b5daf11SMajd Dibbiny {
1341b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1351b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
1361b5daf11SMajd Dibbiny 
1371b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
1381b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
1391b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_max_pkeys(ibdev, max_pkeys);
1401b5daf11SMajd Dibbiny 
1411b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
1421b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
1431b5daf11SMajd Dibbiny 		*max_pkeys = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev,
1441b5daf11SMajd Dibbiny 						pkey_table_size));
1451b5daf11SMajd Dibbiny 		return 0;
1461b5daf11SMajd Dibbiny 
1471b5daf11SMajd Dibbiny 	default:
1481b5daf11SMajd Dibbiny 		return -EINVAL;
1491b5daf11SMajd Dibbiny 	}
1501b5daf11SMajd Dibbiny }
1511b5daf11SMajd Dibbiny 
1521b5daf11SMajd Dibbiny static int mlx5_query_vendor_id(struct ib_device *ibdev,
1531b5daf11SMajd Dibbiny 				u32 *vendor_id)
1541b5daf11SMajd Dibbiny {
1551b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1561b5daf11SMajd Dibbiny 
1571b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
1581b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
1591b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_vendor_id(ibdev, vendor_id);
1601b5daf11SMajd Dibbiny 
1611b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
1621b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
1631b5daf11SMajd Dibbiny 		return mlx5_core_query_vendor_id(dev->mdev, vendor_id);
1641b5daf11SMajd Dibbiny 
1651b5daf11SMajd Dibbiny 	default:
1661b5daf11SMajd Dibbiny 		return -EINVAL;
1671b5daf11SMajd Dibbiny 	}
1681b5daf11SMajd Dibbiny }
1691b5daf11SMajd Dibbiny 
1701b5daf11SMajd Dibbiny static int mlx5_query_node_guid(struct mlx5_ib_dev *dev,
1711b5daf11SMajd Dibbiny 				__be64 *node_guid)
1721b5daf11SMajd Dibbiny {
1731b5daf11SMajd Dibbiny 	u64 tmp;
1741b5daf11SMajd Dibbiny 	int err;
1751b5daf11SMajd Dibbiny 
1761b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(&dev->ib_dev)) {
1771b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
1781b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_node_guid(dev, node_guid);
1791b5daf11SMajd Dibbiny 
1801b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
1811b5daf11SMajd Dibbiny 		err = mlx5_query_hca_vport_node_guid(dev->mdev, &tmp);
1821b5daf11SMajd Dibbiny 		if (!err)
1831b5daf11SMajd Dibbiny 			*node_guid = cpu_to_be64(tmp);
1841b5daf11SMajd Dibbiny 		return err;
1851b5daf11SMajd Dibbiny 
1861b5daf11SMajd Dibbiny 	default:
1871b5daf11SMajd Dibbiny 		return -EINVAL;
1881b5daf11SMajd Dibbiny 	}
1891b5daf11SMajd Dibbiny }
1901b5daf11SMajd Dibbiny 
1911b5daf11SMajd Dibbiny struct mlx5_reg_node_desc {
1921b5daf11SMajd Dibbiny 	u8	desc[64];
1931b5daf11SMajd Dibbiny };
1941b5daf11SMajd Dibbiny 
1951b5daf11SMajd Dibbiny static int mlx5_query_node_desc(struct mlx5_ib_dev *dev, char *node_desc)
1961b5daf11SMajd Dibbiny {
1971b5daf11SMajd Dibbiny 	struct mlx5_reg_node_desc in;
1981b5daf11SMajd Dibbiny 
1991b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(dev))
2001b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_node_desc(dev, node_desc);
2011b5daf11SMajd Dibbiny 
2021b5daf11SMajd Dibbiny 	memset(&in, 0, sizeof(in));
2031b5daf11SMajd Dibbiny 
2041b5daf11SMajd Dibbiny 	return mlx5_core_access_reg(dev->mdev, &in, sizeof(in), node_desc,
2051b5daf11SMajd Dibbiny 				    sizeof(struct mlx5_reg_node_desc),
2061b5daf11SMajd Dibbiny 				    MLX5_REG_NODE_DESC, 0, 0);
2071b5daf11SMajd Dibbiny }
2081b5daf11SMajd Dibbiny 
209e126ba97SEli Cohen static int mlx5_ib_query_device(struct ib_device *ibdev,
2102528e33eSMatan Barak 				struct ib_device_attr *props,
2112528e33eSMatan Barak 				struct ib_udata *uhw)
212e126ba97SEli Cohen {
213e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
214938fe83cSSaeed Mahameed 	struct mlx5_core_dev *mdev = dev->mdev;
215e126ba97SEli Cohen 	int err = -ENOMEM;
216e126ba97SEli Cohen 	int max_rq_sg;
217e126ba97SEli Cohen 	int max_sq_sg;
218e0238a6aSSagi Grimberg 	u64 min_page_size = 1ull << MLX5_CAP_GEN(mdev, log_pg_sz);
219e126ba97SEli Cohen 
2202528e33eSMatan Barak 	if (uhw->inlen || uhw->outlen)
2212528e33eSMatan Barak 		return -EINVAL;
2222528e33eSMatan Barak 
223e126ba97SEli Cohen 	memset(props, 0, sizeof(*props));
2241b5daf11SMajd Dibbiny 	err = mlx5_query_system_image_guid(ibdev,
2251b5daf11SMajd Dibbiny 					   &props->sys_image_guid);
2261b5daf11SMajd Dibbiny 	if (err)
2271b5daf11SMajd Dibbiny 		return err;
2281b5daf11SMajd Dibbiny 
2291b5daf11SMajd Dibbiny 	err = mlx5_query_max_pkeys(ibdev, &props->max_pkeys);
2301b5daf11SMajd Dibbiny 	if (err)
2311b5daf11SMajd Dibbiny 		return err;
2321b5daf11SMajd Dibbiny 
2331b5daf11SMajd Dibbiny 	err = mlx5_query_vendor_id(ibdev, &props->vendor_id);
2341b5daf11SMajd Dibbiny 	if (err)
2351b5daf11SMajd Dibbiny 		return err;
236e126ba97SEli Cohen 
2379603b61dSJack Morgenstein 	props->fw_ver = ((u64)fw_rev_maj(dev->mdev) << 32) |
2389603b61dSJack Morgenstein 		(fw_rev_min(dev->mdev) << 16) |
2399603b61dSJack Morgenstein 		fw_rev_sub(dev->mdev);
240e126ba97SEli Cohen 	props->device_cap_flags    = IB_DEVICE_CHANGE_PHY_PORT |
241e126ba97SEli Cohen 		IB_DEVICE_PORT_ACTIVE_EVENT		|
242e126ba97SEli Cohen 		IB_DEVICE_SYS_IMAGE_GUID		|
2431a4c3a3dSEli Cohen 		IB_DEVICE_RC_RNR_NAK_GEN;
244938fe83cSSaeed Mahameed 
245938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, pkv))
246e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
247938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, qkv))
248e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
249938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, apm))
250e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
251938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, xrc))
252e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_XRC;
253e126ba97SEli Cohen 	props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
254938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, sho)) {
2552dea9094SSagi Grimberg 		props->device_cap_flags |= IB_DEVICE_SIGNATURE_HANDOVER;
2562dea9094SSagi Grimberg 		/* At this stage no support for signature handover */
2572dea9094SSagi Grimberg 		props->sig_prot_cap = IB_PROT_T10DIF_TYPE_1 |
2582dea9094SSagi Grimberg 				      IB_PROT_T10DIF_TYPE_2 |
2592dea9094SSagi Grimberg 				      IB_PROT_T10DIF_TYPE_3;
2602dea9094SSagi Grimberg 		props->sig_guard_cap = IB_GUARD_T10DIF_CRC |
2612dea9094SSagi Grimberg 				       IB_GUARD_T10DIF_CSUM;
2622dea9094SSagi Grimberg 	}
263938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, block_lb_mc))
264f360d88aSEli Cohen 		props->device_cap_flags |= IB_DEVICE_BLOCK_MULTICAST_LOOPBACK;
265e126ba97SEli Cohen 
2661b5daf11SMajd Dibbiny 	props->vendor_part_id	   = mdev->pdev->device;
2671b5daf11SMajd Dibbiny 	props->hw_ver		   = mdev->pdev->revision;
268e126ba97SEli Cohen 
269e126ba97SEli Cohen 	props->max_mr_size	   = ~0ull;
270e0238a6aSSagi Grimberg 	props->page_size_cap	   = ~(min_page_size - 1);
271938fe83cSSaeed Mahameed 	props->max_qp		   = 1 << MLX5_CAP_GEN(mdev, log_max_qp);
272938fe83cSSaeed Mahameed 	props->max_qp_wr	   = 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
273938fe83cSSaeed Mahameed 	max_rq_sg =  MLX5_CAP_GEN(mdev, max_wqe_sz_rq) /
274938fe83cSSaeed Mahameed 		     sizeof(struct mlx5_wqe_data_seg);
275938fe83cSSaeed Mahameed 	max_sq_sg = (MLX5_CAP_GEN(mdev, max_wqe_sz_sq) -
276938fe83cSSaeed Mahameed 		     sizeof(struct mlx5_wqe_ctrl_seg)) /
277e126ba97SEli Cohen 		     sizeof(struct mlx5_wqe_data_seg);
278e126ba97SEli Cohen 	props->max_sge = min(max_rq_sg, max_sq_sg);
27918ebd407SSagi Grimberg 	props->max_sge_rd = props->max_sge;
280938fe83cSSaeed Mahameed 	props->max_cq		   = 1 << MLX5_CAP_GEN(mdev, log_max_cq);
281938fe83cSSaeed Mahameed 	props->max_cqe = (1 << MLX5_CAP_GEN(mdev, log_max_eq_sz)) - 1;
282938fe83cSSaeed Mahameed 	props->max_mr		   = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
283938fe83cSSaeed Mahameed 	props->max_pd		   = 1 << MLX5_CAP_GEN(mdev, log_max_pd);
284938fe83cSSaeed Mahameed 	props->max_qp_rd_atom	   = 1 << MLX5_CAP_GEN(mdev, log_max_ra_req_qp);
285938fe83cSSaeed Mahameed 	props->max_qp_init_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_res_qp);
286938fe83cSSaeed Mahameed 	props->max_srq		   = 1 << MLX5_CAP_GEN(mdev, log_max_srq);
287938fe83cSSaeed Mahameed 	props->max_srq_wr = (1 << MLX5_CAP_GEN(mdev, log_max_srq_sz)) - 1;
288938fe83cSSaeed Mahameed 	props->local_ca_ack_delay  = MLX5_CAP_GEN(mdev, local_ca_ack_delay);
289e126ba97SEli Cohen 	props->max_res_rd_atom	   = props->max_qp_rd_atom * props->max_qp;
290e126ba97SEli Cohen 	props->max_srq_sge	   = max_rq_sg - 1;
291e126ba97SEli Cohen 	props->max_fast_reg_page_list_len = (unsigned int)-1;
29281bea28fSEli Cohen 	props->atomic_cap	   = IB_ATOMIC_NONE;
29381bea28fSEli Cohen 	props->masked_atomic_cap   = IB_ATOMIC_NONE;
294938fe83cSSaeed Mahameed 	props->max_mcast_grp	   = 1 << MLX5_CAP_GEN(mdev, log_max_mcg);
295938fe83cSSaeed Mahameed 	props->max_mcast_qp_attach = MLX5_CAP_GEN(mdev, max_qp_mcg);
296e126ba97SEli Cohen 	props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
297e126ba97SEli Cohen 					   props->max_mcast_grp;
298e126ba97SEli Cohen 	props->max_map_per_fmr = INT_MAX; /* no limit in ConnectIB */
299e126ba97SEli Cohen 
3008cdd312cSHaggai Eran #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
301938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, pg))
3028cdd312cSHaggai Eran 		props->device_cap_flags |= IB_DEVICE_ON_DEMAND_PAGING;
3038cdd312cSHaggai Eran 	props->odp_caps = dev->odp_caps;
3048cdd312cSHaggai Eran #endif
3058cdd312cSHaggai Eran 
3061b5daf11SMajd Dibbiny 	return 0;
3071b5daf11SMajd Dibbiny }
308e126ba97SEli Cohen 
3091b5daf11SMajd Dibbiny enum mlx5_ib_width {
3101b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_1X	= 1 << 0,
3111b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_2X	= 1 << 1,
3121b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_4X	= 1 << 2,
3131b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_8X	= 1 << 3,
3141b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_12X	= 1 << 4
3151b5daf11SMajd Dibbiny };
3161b5daf11SMajd Dibbiny 
3171b5daf11SMajd Dibbiny static int translate_active_width(struct ib_device *ibdev, u8 active_width,
3181b5daf11SMajd Dibbiny 				  u8 *ib_width)
3191b5daf11SMajd Dibbiny {
3201b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
3211b5daf11SMajd Dibbiny 	int err = 0;
3221b5daf11SMajd Dibbiny 
3231b5daf11SMajd Dibbiny 	if (active_width & MLX5_IB_WIDTH_1X) {
3241b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_1X;
3251b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_2X) {
3261b5daf11SMajd Dibbiny 		mlx5_ib_dbg(dev, "active_width %d is not supported by IB spec\n",
3271b5daf11SMajd Dibbiny 			    (int)active_width);
3281b5daf11SMajd Dibbiny 		err = -EINVAL;
3291b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_4X) {
3301b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_4X;
3311b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_8X) {
3321b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_8X;
3331b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_12X) {
3341b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_12X;
3351b5daf11SMajd Dibbiny 	} else {
3361b5daf11SMajd Dibbiny 		mlx5_ib_dbg(dev, "Invalid active_width %d\n",
3371b5daf11SMajd Dibbiny 			    (int)active_width);
3381b5daf11SMajd Dibbiny 		err = -EINVAL;
3391b5daf11SMajd Dibbiny 	}
3401b5daf11SMajd Dibbiny 
3411b5daf11SMajd Dibbiny 	return err;
3421b5daf11SMajd Dibbiny }
3431b5daf11SMajd Dibbiny 
3441b5daf11SMajd Dibbiny static int mlx5_mtu_to_ib_mtu(int mtu)
3451b5daf11SMajd Dibbiny {
3461b5daf11SMajd Dibbiny 	switch (mtu) {
3471b5daf11SMajd Dibbiny 	case 256: return 1;
3481b5daf11SMajd Dibbiny 	case 512: return 2;
3491b5daf11SMajd Dibbiny 	case 1024: return 3;
3501b5daf11SMajd Dibbiny 	case 2048: return 4;
3511b5daf11SMajd Dibbiny 	case 4096: return 5;
3521b5daf11SMajd Dibbiny 	default:
3531b5daf11SMajd Dibbiny 		pr_warn("invalid mtu\n");
3541b5daf11SMajd Dibbiny 		return -1;
3551b5daf11SMajd Dibbiny 	}
3561b5daf11SMajd Dibbiny }
3571b5daf11SMajd Dibbiny 
3581b5daf11SMajd Dibbiny enum ib_max_vl_num {
3591b5daf11SMajd Dibbiny 	__IB_MAX_VL_0		= 1,
3601b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_1		= 2,
3611b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_3		= 3,
3621b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_7		= 4,
3631b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_14	= 5,
3641b5daf11SMajd Dibbiny };
3651b5daf11SMajd Dibbiny 
3661b5daf11SMajd Dibbiny enum mlx5_vl_hw_cap {
3671b5daf11SMajd Dibbiny 	MLX5_VL_HW_0	= 1,
3681b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_1	= 2,
3691b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_2	= 3,
3701b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_3	= 4,
3711b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_4	= 5,
3721b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_5	= 6,
3731b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_6	= 7,
3741b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_7	= 8,
3751b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_14	= 15
3761b5daf11SMajd Dibbiny };
3771b5daf11SMajd Dibbiny 
3781b5daf11SMajd Dibbiny static int translate_max_vl_num(struct ib_device *ibdev, u8 vl_hw_cap,
3791b5daf11SMajd Dibbiny 				u8 *max_vl_num)
3801b5daf11SMajd Dibbiny {
3811b5daf11SMajd Dibbiny 	switch (vl_hw_cap) {
3821b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0:
3831b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0;
3841b5daf11SMajd Dibbiny 		break;
3851b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_1:
3861b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_1;
3871b5daf11SMajd Dibbiny 		break;
3881b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_3:
3891b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_3;
3901b5daf11SMajd Dibbiny 		break;
3911b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_7:
3921b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_7;
3931b5daf11SMajd Dibbiny 		break;
3941b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_14:
3951b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_14;
3961b5daf11SMajd Dibbiny 		break;
3971b5daf11SMajd Dibbiny 
3981b5daf11SMajd Dibbiny 	default:
3991b5daf11SMajd Dibbiny 		return -EINVAL;
4001b5daf11SMajd Dibbiny 	}
4011b5daf11SMajd Dibbiny 
4021b5daf11SMajd Dibbiny 	return 0;
4031b5daf11SMajd Dibbiny }
4041b5daf11SMajd Dibbiny 
4051b5daf11SMajd Dibbiny static int mlx5_query_hca_port(struct ib_device *ibdev, u8 port,
4061b5daf11SMajd Dibbiny 			       struct ib_port_attr *props)
4071b5daf11SMajd Dibbiny {
4081b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
4091b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
4101b5daf11SMajd Dibbiny 	struct mlx5_hca_vport_context *rep;
4111b5daf11SMajd Dibbiny 	int max_mtu;
4121b5daf11SMajd Dibbiny 	int oper_mtu;
4131b5daf11SMajd Dibbiny 	int err;
4141b5daf11SMajd Dibbiny 	u8 ib_link_width_oper;
4151b5daf11SMajd Dibbiny 	u8 vl_hw_cap;
4161b5daf11SMajd Dibbiny 
4171b5daf11SMajd Dibbiny 	rep = kzalloc(sizeof(*rep), GFP_KERNEL);
4181b5daf11SMajd Dibbiny 	if (!rep) {
4191b5daf11SMajd Dibbiny 		err = -ENOMEM;
4201b5daf11SMajd Dibbiny 		goto out;
4211b5daf11SMajd Dibbiny 	}
4221b5daf11SMajd Dibbiny 
4231b5daf11SMajd Dibbiny 	memset(props, 0, sizeof(*props));
4241b5daf11SMajd Dibbiny 
4251b5daf11SMajd Dibbiny 	err = mlx5_query_hca_vport_context(mdev, 0, port, 0, rep);
4261b5daf11SMajd Dibbiny 	if (err)
4271b5daf11SMajd Dibbiny 		goto out;
4281b5daf11SMajd Dibbiny 
4291b5daf11SMajd Dibbiny 	props->lid		= rep->lid;
4301b5daf11SMajd Dibbiny 	props->lmc		= rep->lmc;
4311b5daf11SMajd Dibbiny 	props->sm_lid		= rep->sm_lid;
4321b5daf11SMajd Dibbiny 	props->sm_sl		= rep->sm_sl;
4331b5daf11SMajd Dibbiny 	props->state		= rep->vport_state;
4341b5daf11SMajd Dibbiny 	props->phys_state	= rep->port_physical_state;
4351b5daf11SMajd Dibbiny 	props->port_cap_flags	= rep->cap_mask1;
4361b5daf11SMajd Dibbiny 	props->gid_tbl_len	= mlx5_get_gid_table_len(MLX5_CAP_GEN(mdev, gid_table_size));
4371b5daf11SMajd Dibbiny 	props->max_msg_sz	= 1 << MLX5_CAP_GEN(mdev, log_max_msg);
4381b5daf11SMajd Dibbiny 	props->pkey_tbl_len	= mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, pkey_table_size));
4391b5daf11SMajd Dibbiny 	props->bad_pkey_cntr	= rep->pkey_violation_counter;
4401b5daf11SMajd Dibbiny 	props->qkey_viol_cntr	= rep->qkey_violation_counter;
4411b5daf11SMajd Dibbiny 	props->subnet_timeout	= rep->subnet_timeout;
4421b5daf11SMajd Dibbiny 	props->init_type_reply	= rep->init_type_reply;
4431b5daf11SMajd Dibbiny 
4441b5daf11SMajd Dibbiny 	err = mlx5_query_port_link_width_oper(mdev, &ib_link_width_oper, port);
4451b5daf11SMajd Dibbiny 	if (err)
4461b5daf11SMajd Dibbiny 		goto out;
4471b5daf11SMajd Dibbiny 
4481b5daf11SMajd Dibbiny 	err = translate_active_width(ibdev, ib_link_width_oper,
4491b5daf11SMajd Dibbiny 				     &props->active_width);
4501b5daf11SMajd Dibbiny 	if (err)
4511b5daf11SMajd Dibbiny 		goto out;
4521b5daf11SMajd Dibbiny 	err = mlx5_query_port_proto_oper(mdev, &props->active_speed, MLX5_PTYS_IB,
4531b5daf11SMajd Dibbiny 					 port);
4541b5daf11SMajd Dibbiny 	if (err)
4551b5daf11SMajd Dibbiny 		goto out;
4561b5daf11SMajd Dibbiny 
457facc9699SSaeed Mahameed 	mlx5_query_port_max_mtu(mdev, &max_mtu, port);
4581b5daf11SMajd Dibbiny 
4591b5daf11SMajd Dibbiny 	props->max_mtu = mlx5_mtu_to_ib_mtu(max_mtu);
4601b5daf11SMajd Dibbiny 
461facc9699SSaeed Mahameed 	mlx5_query_port_oper_mtu(mdev, &oper_mtu, port);
4621b5daf11SMajd Dibbiny 
4631b5daf11SMajd Dibbiny 	props->active_mtu = mlx5_mtu_to_ib_mtu(oper_mtu);
4641b5daf11SMajd Dibbiny 
4651b5daf11SMajd Dibbiny 	err = mlx5_query_port_vl_hw_cap(mdev, &vl_hw_cap, port);
4661b5daf11SMajd Dibbiny 	if (err)
4671b5daf11SMajd Dibbiny 		goto out;
4681b5daf11SMajd Dibbiny 
4691b5daf11SMajd Dibbiny 	err = translate_max_vl_num(ibdev, vl_hw_cap,
4701b5daf11SMajd Dibbiny 				   &props->max_vl_num);
4711b5daf11SMajd Dibbiny out:
4721b5daf11SMajd Dibbiny 	kfree(rep);
473e126ba97SEli Cohen 	return err;
474e126ba97SEli Cohen }
475e126ba97SEli Cohen 
476e126ba97SEli Cohen int mlx5_ib_query_port(struct ib_device *ibdev, u8 port,
477e126ba97SEli Cohen 		       struct ib_port_attr *props)
478e126ba97SEli Cohen {
4791b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
4801b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
4811b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_port(ibdev, port, props);
482e126ba97SEli Cohen 
4831b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
4841b5daf11SMajd Dibbiny 		return mlx5_query_hca_port(ibdev, port, props);
4851b5daf11SMajd Dibbiny 
4861b5daf11SMajd Dibbiny 	default:
487e126ba97SEli Cohen 		return -EINVAL;
488e126ba97SEli Cohen 	}
489e126ba97SEli Cohen }
490e126ba97SEli Cohen 
491e126ba97SEli Cohen static int mlx5_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
492e126ba97SEli Cohen 			     union ib_gid *gid)
493e126ba97SEli Cohen {
4941b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
4951b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
496e126ba97SEli Cohen 
4971b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
4981b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
4991b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_gids(ibdev, port, index, gid);
500e126ba97SEli Cohen 
5011b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
5021b5daf11SMajd Dibbiny 		return mlx5_query_hca_vport_gid(mdev, 0, port, 0, index, gid);
503e126ba97SEli Cohen 
5041b5daf11SMajd Dibbiny 	default:
5051b5daf11SMajd Dibbiny 		return -EINVAL;
5061b5daf11SMajd Dibbiny 	}
507e126ba97SEli Cohen 
508e126ba97SEli Cohen }
509e126ba97SEli Cohen 
510e126ba97SEli Cohen static int mlx5_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
511e126ba97SEli Cohen 			      u16 *pkey)
512e126ba97SEli Cohen {
5131b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
5141b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
515e126ba97SEli Cohen 
5161b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
5171b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
5181b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_pkey(ibdev, port, index, pkey);
519e126ba97SEli Cohen 
5201b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
5211b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
5221b5daf11SMajd Dibbiny 		return mlx5_query_hca_vport_pkey(mdev, 0, port,  0, index,
5231b5daf11SMajd Dibbiny 						 pkey);
5241b5daf11SMajd Dibbiny 	default:
5251b5daf11SMajd Dibbiny 		return -EINVAL;
526e126ba97SEli Cohen 	}
5271b5daf11SMajd Dibbiny }
528e126ba97SEli Cohen 
529e126ba97SEli Cohen static int mlx5_ib_modify_device(struct ib_device *ibdev, int mask,
530e126ba97SEli Cohen 				 struct ib_device_modify *props)
531e126ba97SEli Cohen {
532e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
533e126ba97SEli Cohen 	struct mlx5_reg_node_desc in;
534e126ba97SEli Cohen 	struct mlx5_reg_node_desc out;
535e126ba97SEli Cohen 	int err;
536e126ba97SEli Cohen 
537e126ba97SEli Cohen 	if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
538e126ba97SEli Cohen 		return -EOPNOTSUPP;
539e126ba97SEli Cohen 
540e126ba97SEli Cohen 	if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
541e126ba97SEli Cohen 		return 0;
542e126ba97SEli Cohen 
543e126ba97SEli Cohen 	/*
544e126ba97SEli Cohen 	 * If possible, pass node desc to FW, so it can generate
545e126ba97SEli Cohen 	 * a 144 trap.  If cmd fails, just ignore.
546e126ba97SEli Cohen 	 */
547e126ba97SEli Cohen 	memcpy(&in, props->node_desc, 64);
5489603b61dSJack Morgenstein 	err = mlx5_core_access_reg(dev->mdev, &in, sizeof(in), &out,
549e126ba97SEli Cohen 				   sizeof(out), MLX5_REG_NODE_DESC, 0, 1);
550e126ba97SEli Cohen 	if (err)
551e126ba97SEli Cohen 		return err;
552e126ba97SEli Cohen 
553e126ba97SEli Cohen 	memcpy(ibdev->node_desc, props->node_desc, 64);
554e126ba97SEli Cohen 
555e126ba97SEli Cohen 	return err;
556e126ba97SEli Cohen }
557e126ba97SEli Cohen 
558e126ba97SEli Cohen static int mlx5_ib_modify_port(struct ib_device *ibdev, u8 port, int mask,
559e126ba97SEli Cohen 			       struct ib_port_modify *props)
560e126ba97SEli Cohen {
561e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
562e126ba97SEli Cohen 	struct ib_port_attr attr;
563e126ba97SEli Cohen 	u32 tmp;
564e126ba97SEli Cohen 	int err;
565e126ba97SEli Cohen 
566e126ba97SEli Cohen 	mutex_lock(&dev->cap_mask_mutex);
567e126ba97SEli Cohen 
568e126ba97SEli Cohen 	err = mlx5_ib_query_port(ibdev, port, &attr);
569e126ba97SEli Cohen 	if (err)
570e126ba97SEli Cohen 		goto out;
571e126ba97SEli Cohen 
572e126ba97SEli Cohen 	tmp = (attr.port_cap_flags | props->set_port_cap_mask) &
573e126ba97SEli Cohen 		~props->clr_port_cap_mask;
574e126ba97SEli Cohen 
5759603b61dSJack Morgenstein 	err = mlx5_set_port_caps(dev->mdev, port, tmp);
576e126ba97SEli Cohen 
577e126ba97SEli Cohen out:
578e126ba97SEli Cohen 	mutex_unlock(&dev->cap_mask_mutex);
579e126ba97SEli Cohen 	return err;
580e126ba97SEli Cohen }
581e126ba97SEli Cohen 
582e126ba97SEli Cohen static struct ib_ucontext *mlx5_ib_alloc_ucontext(struct ib_device *ibdev,
583e126ba97SEli Cohen 						  struct ib_udata *udata)
584e126ba97SEli Cohen {
585e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
58678c0f98cSEli Cohen 	struct mlx5_ib_alloc_ucontext_req_v2 req;
587e126ba97SEli Cohen 	struct mlx5_ib_alloc_ucontext_resp resp;
588e126ba97SEli Cohen 	struct mlx5_ib_ucontext *context;
589e126ba97SEli Cohen 	struct mlx5_uuar_info *uuari;
590e126ba97SEli Cohen 	struct mlx5_uar *uars;
591c1be5232SEli Cohen 	int gross_uuars;
592e126ba97SEli Cohen 	int num_uars;
59378c0f98cSEli Cohen 	int ver;
594e126ba97SEli Cohen 	int uuarn;
595e126ba97SEli Cohen 	int err;
596e126ba97SEli Cohen 	int i;
597f241e749SJack Morgenstein 	size_t reqlen;
598e126ba97SEli Cohen 
599e126ba97SEli Cohen 	if (!dev->ib_active)
600e126ba97SEli Cohen 		return ERR_PTR(-EAGAIN);
601e126ba97SEli Cohen 
60278c0f98cSEli Cohen 	memset(&req, 0, sizeof(req));
60378c0f98cSEli Cohen 	reqlen = udata->inlen - sizeof(struct ib_uverbs_cmd_hdr);
60478c0f98cSEli Cohen 	if (reqlen == sizeof(struct mlx5_ib_alloc_ucontext_req))
60578c0f98cSEli Cohen 		ver = 0;
60678c0f98cSEli Cohen 	else if (reqlen == sizeof(struct mlx5_ib_alloc_ucontext_req_v2))
60778c0f98cSEli Cohen 		ver = 2;
60878c0f98cSEli Cohen 	else
60978c0f98cSEli Cohen 		return ERR_PTR(-EINVAL);
61078c0f98cSEli Cohen 
61178c0f98cSEli Cohen 	err = ib_copy_from_udata(&req, udata, reqlen);
612e126ba97SEli Cohen 	if (err)
613e126ba97SEli Cohen 		return ERR_PTR(err);
614e126ba97SEli Cohen 
61578c0f98cSEli Cohen 	if (req.flags || req.reserved)
61678c0f98cSEli Cohen 		return ERR_PTR(-EINVAL);
61778c0f98cSEli Cohen 
618e126ba97SEli Cohen 	if (req.total_num_uuars > MLX5_MAX_UUARS)
619e126ba97SEli Cohen 		return ERR_PTR(-ENOMEM);
620e126ba97SEli Cohen 
621e126ba97SEli Cohen 	if (req.total_num_uuars == 0)
622e126ba97SEli Cohen 		return ERR_PTR(-EINVAL);
623e126ba97SEli Cohen 
624c1be5232SEli Cohen 	req.total_num_uuars = ALIGN(req.total_num_uuars,
625c1be5232SEli Cohen 				    MLX5_NON_FP_BF_REGS_PER_PAGE);
626e126ba97SEli Cohen 	if (req.num_low_latency_uuars > req.total_num_uuars - 1)
627e126ba97SEli Cohen 		return ERR_PTR(-EINVAL);
628e126ba97SEli Cohen 
629c1be5232SEli Cohen 	num_uars = req.total_num_uuars / MLX5_NON_FP_BF_REGS_PER_PAGE;
630c1be5232SEli Cohen 	gross_uuars = num_uars * MLX5_BF_REGS_PER_PAGE;
631938fe83cSSaeed Mahameed 	resp.qp_tab_size = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp);
632938fe83cSSaeed Mahameed 	resp.bf_reg_size = 1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size);
633e126ba97SEli Cohen 	resp.cache_line_size = L1_CACHE_BYTES;
634938fe83cSSaeed Mahameed 	resp.max_sq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq);
635938fe83cSSaeed Mahameed 	resp.max_rq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq);
636938fe83cSSaeed Mahameed 	resp.max_send_wqebb = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
637938fe83cSSaeed Mahameed 	resp.max_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
638938fe83cSSaeed Mahameed 	resp.max_srq_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz);
639e126ba97SEli Cohen 
640e126ba97SEli Cohen 	context = kzalloc(sizeof(*context), GFP_KERNEL);
641e126ba97SEli Cohen 	if (!context)
642e126ba97SEli Cohen 		return ERR_PTR(-ENOMEM);
643e126ba97SEli Cohen 
644e126ba97SEli Cohen 	uuari = &context->uuari;
645e126ba97SEli Cohen 	mutex_init(&uuari->lock);
646e126ba97SEli Cohen 	uars = kcalloc(num_uars, sizeof(*uars), GFP_KERNEL);
647e126ba97SEli Cohen 	if (!uars) {
648e126ba97SEli Cohen 		err = -ENOMEM;
649e126ba97SEli Cohen 		goto out_ctx;
650e126ba97SEli Cohen 	}
651e126ba97SEli Cohen 
652c1be5232SEli Cohen 	uuari->bitmap = kcalloc(BITS_TO_LONGS(gross_uuars),
653e126ba97SEli Cohen 				sizeof(*uuari->bitmap),
654e126ba97SEli Cohen 				GFP_KERNEL);
655e126ba97SEli Cohen 	if (!uuari->bitmap) {
656e126ba97SEli Cohen 		err = -ENOMEM;
657e126ba97SEli Cohen 		goto out_uar_ctx;
658e126ba97SEli Cohen 	}
659e126ba97SEli Cohen 	/*
660e126ba97SEli Cohen 	 * clear all fast path uuars
661e126ba97SEli Cohen 	 */
662c1be5232SEli Cohen 	for (i = 0; i < gross_uuars; i++) {
663e126ba97SEli Cohen 		uuarn = i & 3;
664e126ba97SEli Cohen 		if (uuarn == 2 || uuarn == 3)
665e126ba97SEli Cohen 			set_bit(i, uuari->bitmap);
666e126ba97SEli Cohen 	}
667e126ba97SEli Cohen 
668c1be5232SEli Cohen 	uuari->count = kcalloc(gross_uuars, sizeof(*uuari->count), GFP_KERNEL);
669e126ba97SEli Cohen 	if (!uuari->count) {
670e126ba97SEli Cohen 		err = -ENOMEM;
671e126ba97SEli Cohen 		goto out_bitmap;
672e126ba97SEli Cohen 	}
673e126ba97SEli Cohen 
674e126ba97SEli Cohen 	for (i = 0; i < num_uars; i++) {
6759603b61dSJack Morgenstein 		err = mlx5_cmd_alloc_uar(dev->mdev, &uars[i].index);
676e126ba97SEli Cohen 		if (err)
677e126ba97SEli Cohen 			goto out_count;
678e126ba97SEli Cohen 	}
679e126ba97SEli Cohen 
680b4cfe447SHaggai Eran #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
681b4cfe447SHaggai Eran 	context->ibucontext.invalidate_range = &mlx5_ib_invalidate_range;
682b4cfe447SHaggai Eran #endif
683b4cfe447SHaggai Eran 
684e126ba97SEli Cohen 	INIT_LIST_HEAD(&context->db_page_list);
685e126ba97SEli Cohen 	mutex_init(&context->db_page_mutex);
686e126ba97SEli Cohen 
687e126ba97SEli Cohen 	resp.tot_uuars = req.total_num_uuars;
688938fe83cSSaeed Mahameed 	resp.num_ports = MLX5_CAP_GEN(dev->mdev, num_ports);
68992b0ca7cSDan Carpenter 	err = ib_copy_to_udata(udata, &resp,
69092b0ca7cSDan Carpenter 			       sizeof(resp) - sizeof(resp.reserved));
691e126ba97SEli Cohen 	if (err)
692e126ba97SEli Cohen 		goto out_uars;
693e126ba97SEli Cohen 
69478c0f98cSEli Cohen 	uuari->ver = ver;
695e126ba97SEli Cohen 	uuari->num_low_latency_uuars = req.num_low_latency_uuars;
696e126ba97SEli Cohen 	uuari->uars = uars;
697e126ba97SEli Cohen 	uuari->num_uars = num_uars;
698e126ba97SEli Cohen 	return &context->ibucontext;
699e126ba97SEli Cohen 
700e126ba97SEli Cohen out_uars:
701e126ba97SEli Cohen 	for (i--; i >= 0; i--)
7029603b61dSJack Morgenstein 		mlx5_cmd_free_uar(dev->mdev, uars[i].index);
703e126ba97SEli Cohen out_count:
704e126ba97SEli Cohen 	kfree(uuari->count);
705e126ba97SEli Cohen 
706e126ba97SEli Cohen out_bitmap:
707e126ba97SEli Cohen 	kfree(uuari->bitmap);
708e126ba97SEli Cohen 
709e126ba97SEli Cohen out_uar_ctx:
710e126ba97SEli Cohen 	kfree(uars);
711e126ba97SEli Cohen 
712e126ba97SEli Cohen out_ctx:
713e126ba97SEli Cohen 	kfree(context);
714e126ba97SEli Cohen 	return ERR_PTR(err);
715e126ba97SEli Cohen }
716e126ba97SEli Cohen 
717e126ba97SEli Cohen static int mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
718e126ba97SEli Cohen {
719e126ba97SEli Cohen 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
720e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
721e126ba97SEli Cohen 	struct mlx5_uuar_info *uuari = &context->uuari;
722e126ba97SEli Cohen 	int i;
723e126ba97SEli Cohen 
724e126ba97SEli Cohen 	for (i = 0; i < uuari->num_uars; i++) {
7259603b61dSJack Morgenstein 		if (mlx5_cmd_free_uar(dev->mdev, uuari->uars[i].index))
726e126ba97SEli Cohen 			mlx5_ib_warn(dev, "failed to free UAR 0x%x\n", uuari->uars[i].index);
727e126ba97SEli Cohen 	}
728e126ba97SEli Cohen 
729e126ba97SEli Cohen 	kfree(uuari->count);
730e126ba97SEli Cohen 	kfree(uuari->bitmap);
731e126ba97SEli Cohen 	kfree(uuari->uars);
732e126ba97SEli Cohen 	kfree(context);
733e126ba97SEli Cohen 
734e126ba97SEli Cohen 	return 0;
735e126ba97SEli Cohen }
736e126ba97SEli Cohen 
737e126ba97SEli Cohen static phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev, int index)
738e126ba97SEli Cohen {
7399603b61dSJack Morgenstein 	return (pci_resource_start(dev->mdev->pdev, 0) >> PAGE_SHIFT) + index;
740e126ba97SEli Cohen }
741e126ba97SEli Cohen 
742e126ba97SEli Cohen static int get_command(unsigned long offset)
743e126ba97SEli Cohen {
744e126ba97SEli Cohen 	return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK;
745e126ba97SEli Cohen }
746e126ba97SEli Cohen 
747e126ba97SEli Cohen static int get_arg(unsigned long offset)
748e126ba97SEli Cohen {
749e126ba97SEli Cohen 	return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1);
750e126ba97SEli Cohen }
751e126ba97SEli Cohen 
752e126ba97SEli Cohen static int get_index(unsigned long offset)
753e126ba97SEli Cohen {
754e126ba97SEli Cohen 	return get_arg(offset);
755e126ba97SEli Cohen }
756e126ba97SEli Cohen 
757e126ba97SEli Cohen static int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma)
758e126ba97SEli Cohen {
759e126ba97SEli Cohen 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
760e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
761e126ba97SEli Cohen 	struct mlx5_uuar_info *uuari = &context->uuari;
762e126ba97SEli Cohen 	unsigned long command;
763e126ba97SEli Cohen 	unsigned long idx;
764e126ba97SEli Cohen 	phys_addr_t pfn;
765e126ba97SEli Cohen 
766e126ba97SEli Cohen 	command = get_command(vma->vm_pgoff);
767e126ba97SEli Cohen 	switch (command) {
768e126ba97SEli Cohen 	case MLX5_IB_MMAP_REGULAR_PAGE:
769e126ba97SEli Cohen 		if (vma->vm_end - vma->vm_start != PAGE_SIZE)
770e126ba97SEli Cohen 			return -EINVAL;
771e126ba97SEli Cohen 
772e126ba97SEli Cohen 		idx = get_index(vma->vm_pgoff);
7731c3ce90dSEli Cohen 		if (idx >= uuari->num_uars)
7741c3ce90dSEli Cohen 			return -EINVAL;
7751c3ce90dSEli Cohen 
776e126ba97SEli Cohen 		pfn = uar_index2pfn(dev, uuari->uars[idx].index);
777e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn 0x%llx\n", idx,
778e126ba97SEli Cohen 			    (unsigned long long)pfn);
779e126ba97SEli Cohen 
780e126ba97SEli Cohen 		vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
781e126ba97SEli Cohen 		if (io_remap_pfn_range(vma, vma->vm_start, pfn,
782e126ba97SEli Cohen 				       PAGE_SIZE, vma->vm_page_prot))
783e126ba97SEli Cohen 			return -EAGAIN;
784e126ba97SEli Cohen 
785e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "mapped WC at 0x%lx, PA 0x%llx\n",
786e126ba97SEli Cohen 			    vma->vm_start,
787e126ba97SEli Cohen 			    (unsigned long long)pfn << PAGE_SHIFT);
788e126ba97SEli Cohen 		break;
789e126ba97SEli Cohen 
790e126ba97SEli Cohen 	case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES:
791e126ba97SEli Cohen 		return -ENOSYS;
792e126ba97SEli Cohen 
793e126ba97SEli Cohen 	default:
794e126ba97SEli Cohen 		return -EINVAL;
795e126ba97SEli Cohen 	}
796e126ba97SEli Cohen 
797e126ba97SEli Cohen 	return 0;
798e126ba97SEli Cohen }
799e126ba97SEli Cohen 
800e126ba97SEli Cohen static struct ib_pd *mlx5_ib_alloc_pd(struct ib_device *ibdev,
801e126ba97SEli Cohen 				      struct ib_ucontext *context,
802e126ba97SEli Cohen 				      struct ib_udata *udata)
803e126ba97SEli Cohen {
804e126ba97SEli Cohen 	struct mlx5_ib_alloc_pd_resp resp;
805e126ba97SEli Cohen 	struct mlx5_ib_pd *pd;
806e126ba97SEli Cohen 	int err;
807e126ba97SEli Cohen 
808e126ba97SEli Cohen 	pd = kmalloc(sizeof(*pd), GFP_KERNEL);
809e126ba97SEli Cohen 	if (!pd)
810e126ba97SEli Cohen 		return ERR_PTR(-ENOMEM);
811e126ba97SEli Cohen 
8129603b61dSJack Morgenstein 	err = mlx5_core_alloc_pd(to_mdev(ibdev)->mdev, &pd->pdn);
813e126ba97SEli Cohen 	if (err) {
814e126ba97SEli Cohen 		kfree(pd);
815e126ba97SEli Cohen 		return ERR_PTR(err);
816e126ba97SEli Cohen 	}
817e126ba97SEli Cohen 
818e126ba97SEli Cohen 	if (context) {
819e126ba97SEli Cohen 		resp.pdn = pd->pdn;
820e126ba97SEli Cohen 		if (ib_copy_to_udata(udata, &resp, sizeof(resp))) {
8219603b61dSJack Morgenstein 			mlx5_core_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn);
822e126ba97SEli Cohen 			kfree(pd);
823e126ba97SEli Cohen 			return ERR_PTR(-EFAULT);
824e126ba97SEli Cohen 		}
825e126ba97SEli Cohen 	}
826e126ba97SEli Cohen 
827e126ba97SEli Cohen 	return &pd->ibpd;
828e126ba97SEli Cohen }
829e126ba97SEli Cohen 
830e126ba97SEli Cohen static int mlx5_ib_dealloc_pd(struct ib_pd *pd)
831e126ba97SEli Cohen {
832e126ba97SEli Cohen 	struct mlx5_ib_dev *mdev = to_mdev(pd->device);
833e126ba97SEli Cohen 	struct mlx5_ib_pd *mpd = to_mpd(pd);
834e126ba97SEli Cohen 
8359603b61dSJack Morgenstein 	mlx5_core_dealloc_pd(mdev->mdev, mpd->pdn);
836e126ba97SEli Cohen 	kfree(mpd);
837e126ba97SEli Cohen 
838e126ba97SEli Cohen 	return 0;
839e126ba97SEli Cohen }
840e126ba97SEli Cohen 
841*038d2ef8SMaor Gottlieb static bool outer_header_zero(u32 *match_criteria)
842*038d2ef8SMaor Gottlieb {
843*038d2ef8SMaor Gottlieb 	int size = MLX5_ST_SZ_BYTES(fte_match_param);
844*038d2ef8SMaor Gottlieb 	char *outer_headers_c = MLX5_ADDR_OF(fte_match_param, match_criteria,
845*038d2ef8SMaor Gottlieb 					     outer_headers);
846*038d2ef8SMaor Gottlieb 
847*038d2ef8SMaor Gottlieb 	return outer_headers_c[0] == 0 && !memcmp(outer_headers_c,
848*038d2ef8SMaor Gottlieb 						  outer_headers_c + 1,
849*038d2ef8SMaor Gottlieb 						  size - 1);
850*038d2ef8SMaor Gottlieb }
851*038d2ef8SMaor Gottlieb 
852*038d2ef8SMaor Gottlieb static int parse_flow_attr(u32 *match_c, u32 *match_v,
853*038d2ef8SMaor Gottlieb 			   union ib_flow_spec *ib_spec)
854*038d2ef8SMaor Gottlieb {
855*038d2ef8SMaor Gottlieb 	void *outer_headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
856*038d2ef8SMaor Gottlieb 					     outer_headers);
857*038d2ef8SMaor Gottlieb 	void *outer_headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
858*038d2ef8SMaor Gottlieb 					     outer_headers);
859*038d2ef8SMaor Gottlieb 	switch (ib_spec->type) {
860*038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_ETH:
861*038d2ef8SMaor Gottlieb 		if (ib_spec->size != sizeof(ib_spec->eth))
862*038d2ef8SMaor Gottlieb 			return -EINVAL;
863*038d2ef8SMaor Gottlieb 
864*038d2ef8SMaor Gottlieb 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_c,
865*038d2ef8SMaor Gottlieb 					     dmac_47_16),
866*038d2ef8SMaor Gottlieb 				ib_spec->eth.mask.dst_mac);
867*038d2ef8SMaor Gottlieb 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_v,
868*038d2ef8SMaor Gottlieb 					     dmac_47_16),
869*038d2ef8SMaor Gottlieb 				ib_spec->eth.val.dst_mac);
870*038d2ef8SMaor Gottlieb 
871*038d2ef8SMaor Gottlieb 		if (ib_spec->eth.mask.vlan_tag) {
872*038d2ef8SMaor Gottlieb 			MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c,
873*038d2ef8SMaor Gottlieb 				 vlan_tag, 1);
874*038d2ef8SMaor Gottlieb 			MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v,
875*038d2ef8SMaor Gottlieb 				 vlan_tag, 1);
876*038d2ef8SMaor Gottlieb 
877*038d2ef8SMaor Gottlieb 			MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c,
878*038d2ef8SMaor Gottlieb 				 first_vid, ntohs(ib_spec->eth.mask.vlan_tag));
879*038d2ef8SMaor Gottlieb 			MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v,
880*038d2ef8SMaor Gottlieb 				 first_vid, ntohs(ib_spec->eth.val.vlan_tag));
881*038d2ef8SMaor Gottlieb 
882*038d2ef8SMaor Gottlieb 			MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c,
883*038d2ef8SMaor Gottlieb 				 first_cfi,
884*038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.mask.vlan_tag) >> 12);
885*038d2ef8SMaor Gottlieb 			MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v,
886*038d2ef8SMaor Gottlieb 				 first_cfi,
887*038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.val.vlan_tag) >> 12);
888*038d2ef8SMaor Gottlieb 
889*038d2ef8SMaor Gottlieb 			MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c,
890*038d2ef8SMaor Gottlieb 				 first_prio,
891*038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.mask.vlan_tag) >> 13);
892*038d2ef8SMaor Gottlieb 			MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v,
893*038d2ef8SMaor Gottlieb 				 first_prio,
894*038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.val.vlan_tag) >> 13);
895*038d2ef8SMaor Gottlieb 		}
896*038d2ef8SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c,
897*038d2ef8SMaor Gottlieb 			 ethertype, ntohs(ib_spec->eth.mask.ether_type));
898*038d2ef8SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v,
899*038d2ef8SMaor Gottlieb 			 ethertype, ntohs(ib_spec->eth.val.ether_type));
900*038d2ef8SMaor Gottlieb 		break;
901*038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_IPV4:
902*038d2ef8SMaor Gottlieb 		if (ib_spec->size != sizeof(ib_spec->ipv4))
903*038d2ef8SMaor Gottlieb 			return -EINVAL;
904*038d2ef8SMaor Gottlieb 
905*038d2ef8SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c,
906*038d2ef8SMaor Gottlieb 			 ethertype, 0xffff);
907*038d2ef8SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v,
908*038d2ef8SMaor Gottlieb 			 ethertype, ETH_P_IP);
909*038d2ef8SMaor Gottlieb 
910*038d2ef8SMaor Gottlieb 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_c,
911*038d2ef8SMaor Gottlieb 				    src_ipv4_src_ipv6.ipv4_layout.ipv4),
912*038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.mask.src_ip,
913*038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.mask.src_ip));
914*038d2ef8SMaor Gottlieb 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_v,
915*038d2ef8SMaor Gottlieb 				    src_ipv4_src_ipv6.ipv4_layout.ipv4),
916*038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.val.src_ip,
917*038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.val.src_ip));
918*038d2ef8SMaor Gottlieb 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_c,
919*038d2ef8SMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
920*038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.mask.dst_ip,
921*038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.mask.dst_ip));
922*038d2ef8SMaor Gottlieb 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_v,
923*038d2ef8SMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
924*038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.val.dst_ip,
925*038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.val.dst_ip));
926*038d2ef8SMaor Gottlieb 		break;
927*038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_TCP:
928*038d2ef8SMaor Gottlieb 		if (ib_spec->size != sizeof(ib_spec->tcp_udp))
929*038d2ef8SMaor Gottlieb 			return -EINVAL;
930*038d2ef8SMaor Gottlieb 
931*038d2ef8SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, ip_protocol,
932*038d2ef8SMaor Gottlieb 			 0xff);
933*038d2ef8SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, ip_protocol,
934*038d2ef8SMaor Gottlieb 			 IPPROTO_TCP);
935*038d2ef8SMaor Gottlieb 
936*038d2ef8SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, tcp_sport,
937*038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.src_port));
938*038d2ef8SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, tcp_sport,
939*038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.src_port));
940*038d2ef8SMaor Gottlieb 
941*038d2ef8SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, tcp_dport,
942*038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.dst_port));
943*038d2ef8SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, tcp_dport,
944*038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.dst_port));
945*038d2ef8SMaor Gottlieb 		break;
946*038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_UDP:
947*038d2ef8SMaor Gottlieb 		if (ib_spec->size != sizeof(ib_spec->tcp_udp))
948*038d2ef8SMaor Gottlieb 			return -EINVAL;
949*038d2ef8SMaor Gottlieb 
950*038d2ef8SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, ip_protocol,
951*038d2ef8SMaor Gottlieb 			 0xff);
952*038d2ef8SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, ip_protocol,
953*038d2ef8SMaor Gottlieb 			 IPPROTO_UDP);
954*038d2ef8SMaor Gottlieb 
955*038d2ef8SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, udp_sport,
956*038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.src_port));
957*038d2ef8SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, udp_sport,
958*038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.src_port));
959*038d2ef8SMaor Gottlieb 
960*038d2ef8SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, udp_dport,
961*038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.dst_port));
962*038d2ef8SMaor Gottlieb 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, udp_dport,
963*038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.dst_port));
964*038d2ef8SMaor Gottlieb 		break;
965*038d2ef8SMaor Gottlieb 	default:
966*038d2ef8SMaor Gottlieb 		return -EINVAL;
967*038d2ef8SMaor Gottlieb 	}
968*038d2ef8SMaor Gottlieb 
969*038d2ef8SMaor Gottlieb 	return 0;
970*038d2ef8SMaor Gottlieb }
971*038d2ef8SMaor Gottlieb 
972*038d2ef8SMaor Gottlieb /* If a flow could catch both multicast and unicast packets,
973*038d2ef8SMaor Gottlieb  * it won't fall into the multicast flow steering table and this rule
974*038d2ef8SMaor Gottlieb  * could steal other multicast packets.
975*038d2ef8SMaor Gottlieb  */
976*038d2ef8SMaor Gottlieb static bool flow_is_multicast_only(struct ib_flow_attr *ib_attr)
977*038d2ef8SMaor Gottlieb {
978*038d2ef8SMaor Gottlieb 	struct ib_flow_spec_eth *eth_spec;
979*038d2ef8SMaor Gottlieb 
980*038d2ef8SMaor Gottlieb 	if (ib_attr->type != IB_FLOW_ATTR_NORMAL ||
981*038d2ef8SMaor Gottlieb 	    ib_attr->size < sizeof(struct ib_flow_attr) +
982*038d2ef8SMaor Gottlieb 	    sizeof(struct ib_flow_spec_eth) ||
983*038d2ef8SMaor Gottlieb 	    ib_attr->num_of_specs < 1)
984*038d2ef8SMaor Gottlieb 		return false;
985*038d2ef8SMaor Gottlieb 
986*038d2ef8SMaor Gottlieb 	eth_spec = (struct ib_flow_spec_eth *)(ib_attr + 1);
987*038d2ef8SMaor Gottlieb 	if (eth_spec->type != IB_FLOW_SPEC_ETH ||
988*038d2ef8SMaor Gottlieb 	    eth_spec->size != sizeof(*eth_spec))
989*038d2ef8SMaor Gottlieb 		return false;
990*038d2ef8SMaor Gottlieb 
991*038d2ef8SMaor Gottlieb 	return is_multicast_ether_addr(eth_spec->mask.dst_mac) &&
992*038d2ef8SMaor Gottlieb 	       is_multicast_ether_addr(eth_spec->val.dst_mac);
993*038d2ef8SMaor Gottlieb }
994*038d2ef8SMaor Gottlieb 
995*038d2ef8SMaor Gottlieb static bool is_valid_attr(struct ib_flow_attr *flow_attr)
996*038d2ef8SMaor Gottlieb {
997*038d2ef8SMaor Gottlieb 	union ib_flow_spec *ib_spec = (union ib_flow_spec *)(flow_attr + 1);
998*038d2ef8SMaor Gottlieb 	bool has_ipv4_spec = false;
999*038d2ef8SMaor Gottlieb 	bool eth_type_ipv4 = true;
1000*038d2ef8SMaor Gottlieb 	unsigned int spec_index;
1001*038d2ef8SMaor Gottlieb 
1002*038d2ef8SMaor Gottlieb 	/* Validate that ethertype is correct */
1003*038d2ef8SMaor Gottlieb 	for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
1004*038d2ef8SMaor Gottlieb 		if (ib_spec->type == IB_FLOW_SPEC_ETH &&
1005*038d2ef8SMaor Gottlieb 		    ib_spec->eth.mask.ether_type) {
1006*038d2ef8SMaor Gottlieb 			if (!((ib_spec->eth.mask.ether_type == htons(0xffff)) &&
1007*038d2ef8SMaor Gottlieb 			      ib_spec->eth.val.ether_type == htons(ETH_P_IP)))
1008*038d2ef8SMaor Gottlieb 				eth_type_ipv4 = false;
1009*038d2ef8SMaor Gottlieb 		} else if (ib_spec->type == IB_FLOW_SPEC_IPV4) {
1010*038d2ef8SMaor Gottlieb 			has_ipv4_spec = true;
1011*038d2ef8SMaor Gottlieb 		}
1012*038d2ef8SMaor Gottlieb 		ib_spec = (void *)ib_spec + ib_spec->size;
1013*038d2ef8SMaor Gottlieb 	}
1014*038d2ef8SMaor Gottlieb 	return !has_ipv4_spec || eth_type_ipv4;
1015*038d2ef8SMaor Gottlieb }
1016*038d2ef8SMaor Gottlieb 
1017*038d2ef8SMaor Gottlieb static void put_flow_table(struct mlx5_ib_dev *dev,
1018*038d2ef8SMaor Gottlieb 			   struct mlx5_ib_flow_prio *prio, bool ft_added)
1019*038d2ef8SMaor Gottlieb {
1020*038d2ef8SMaor Gottlieb 	prio->refcount -= !!ft_added;
1021*038d2ef8SMaor Gottlieb 	if (!prio->refcount) {
1022*038d2ef8SMaor Gottlieb 		mlx5_destroy_flow_table(prio->flow_table);
1023*038d2ef8SMaor Gottlieb 		prio->flow_table = NULL;
1024*038d2ef8SMaor Gottlieb 	}
1025*038d2ef8SMaor Gottlieb }
1026*038d2ef8SMaor Gottlieb 
1027*038d2ef8SMaor Gottlieb static int mlx5_ib_destroy_flow(struct ib_flow *flow_id)
1028*038d2ef8SMaor Gottlieb {
1029*038d2ef8SMaor Gottlieb 	struct mlx5_ib_dev *dev = to_mdev(flow_id->qp->device);
1030*038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = container_of(flow_id,
1031*038d2ef8SMaor Gottlieb 							  struct mlx5_ib_flow_handler,
1032*038d2ef8SMaor Gottlieb 							  ibflow);
1033*038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *iter, *tmp;
1034*038d2ef8SMaor Gottlieb 
1035*038d2ef8SMaor Gottlieb 	mutex_lock(&dev->flow_db.lock);
1036*038d2ef8SMaor Gottlieb 
1037*038d2ef8SMaor Gottlieb 	list_for_each_entry_safe(iter, tmp, &handler->list, list) {
1038*038d2ef8SMaor Gottlieb 		mlx5_del_flow_rule(iter->rule);
1039*038d2ef8SMaor Gottlieb 		list_del(&iter->list);
1040*038d2ef8SMaor Gottlieb 		kfree(iter);
1041*038d2ef8SMaor Gottlieb 	}
1042*038d2ef8SMaor Gottlieb 
1043*038d2ef8SMaor Gottlieb 	mlx5_del_flow_rule(handler->rule);
1044*038d2ef8SMaor Gottlieb 	put_flow_table(dev, &dev->flow_db.prios[handler->prio], true);
1045*038d2ef8SMaor Gottlieb 	mutex_unlock(&dev->flow_db.lock);
1046*038d2ef8SMaor Gottlieb 
1047*038d2ef8SMaor Gottlieb 	kfree(handler);
1048*038d2ef8SMaor Gottlieb 
1049*038d2ef8SMaor Gottlieb 	return 0;
1050*038d2ef8SMaor Gottlieb }
1051*038d2ef8SMaor Gottlieb 
1052*038d2ef8SMaor Gottlieb #define MLX5_FS_MAX_TYPES	 10
1053*038d2ef8SMaor Gottlieb #define MLX5_FS_MAX_ENTRIES	 32000UL
1054*038d2ef8SMaor Gottlieb static struct mlx5_ib_flow_prio *get_flow_table(struct mlx5_ib_dev *dev,
1055*038d2ef8SMaor Gottlieb 						struct ib_flow_attr *flow_attr)
1056*038d2ef8SMaor Gottlieb {
1057*038d2ef8SMaor Gottlieb 	struct mlx5_flow_namespace *ns = NULL;
1058*038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_prio *prio;
1059*038d2ef8SMaor Gottlieb 	struct mlx5_flow_table *ft;
1060*038d2ef8SMaor Gottlieb 	int num_entries;
1061*038d2ef8SMaor Gottlieb 	int num_groups;
1062*038d2ef8SMaor Gottlieb 	int priority;
1063*038d2ef8SMaor Gottlieb 	int err = 0;
1064*038d2ef8SMaor Gottlieb 
1065*038d2ef8SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
1066*038d2ef8SMaor Gottlieb 		if (flow_is_multicast_only(flow_attr))
1067*038d2ef8SMaor Gottlieb 			priority = MLX5_IB_FLOW_MCAST_PRIO;
1068*038d2ef8SMaor Gottlieb 		else
1069*038d2ef8SMaor Gottlieb 			priority = flow_attr->priority;
1070*038d2ef8SMaor Gottlieb 		ns = mlx5_get_flow_namespace(dev->mdev,
1071*038d2ef8SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_BYPASS);
1072*038d2ef8SMaor Gottlieb 		num_entries = MLX5_FS_MAX_ENTRIES;
1073*038d2ef8SMaor Gottlieb 		num_groups = MLX5_FS_MAX_TYPES;
1074*038d2ef8SMaor Gottlieb 		prio = &dev->flow_db.prios[priority];
1075*038d2ef8SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
1076*038d2ef8SMaor Gottlieb 		   flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
1077*038d2ef8SMaor Gottlieb 		ns = mlx5_get_flow_namespace(dev->mdev,
1078*038d2ef8SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_LEFTOVERS);
1079*038d2ef8SMaor Gottlieb 		build_leftovers_ft_param(&priority,
1080*038d2ef8SMaor Gottlieb 					 &num_entries,
1081*038d2ef8SMaor Gottlieb 					 &num_groups);
1082*038d2ef8SMaor Gottlieb 		prio = &dev->flow_db.prios[MLX5_IB_FLOW_LEFTOVERS_PRIO];
1083*038d2ef8SMaor Gottlieb 	}
1084*038d2ef8SMaor Gottlieb 
1085*038d2ef8SMaor Gottlieb 	if (!ns)
1086*038d2ef8SMaor Gottlieb 		return ERR_PTR(-ENOTSUPP);
1087*038d2ef8SMaor Gottlieb 
1088*038d2ef8SMaor Gottlieb 	ft = prio->flow_table;
1089*038d2ef8SMaor Gottlieb 	if (!ft) {
1090*038d2ef8SMaor Gottlieb 		ft = mlx5_create_auto_grouped_flow_table(ns, priority,
1091*038d2ef8SMaor Gottlieb 							 num_entries,
1092*038d2ef8SMaor Gottlieb 							 num_groups);
1093*038d2ef8SMaor Gottlieb 
1094*038d2ef8SMaor Gottlieb 		if (!IS_ERR(ft)) {
1095*038d2ef8SMaor Gottlieb 			prio->refcount = 0;
1096*038d2ef8SMaor Gottlieb 			prio->flow_table = ft;
1097*038d2ef8SMaor Gottlieb 		} else {
1098*038d2ef8SMaor Gottlieb 			err = PTR_ERR(ft);
1099*038d2ef8SMaor Gottlieb 		}
1100*038d2ef8SMaor Gottlieb 	}
1101*038d2ef8SMaor Gottlieb 
1102*038d2ef8SMaor Gottlieb 	return err ? ERR_PTR(err) : prio;
1103*038d2ef8SMaor Gottlieb }
1104*038d2ef8SMaor Gottlieb 
1105*038d2ef8SMaor Gottlieb static struct mlx5_ib_flow_handler *create_flow_rule(struct mlx5_ib_dev *dev,
1106*038d2ef8SMaor Gottlieb 						     struct mlx5_ib_flow_prio *ft_prio,
1107*038d2ef8SMaor Gottlieb 						     struct ib_flow_attr *flow_attr,
1108*038d2ef8SMaor Gottlieb 						     struct mlx5_flow_destination *dst)
1109*038d2ef8SMaor Gottlieb {
1110*038d2ef8SMaor Gottlieb 	struct mlx5_flow_table	*ft = ft_prio->flow_table;
1111*038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler;
1112*038d2ef8SMaor Gottlieb 	void *ib_flow = flow_attr + 1;
1113*038d2ef8SMaor Gottlieb 	u8 match_criteria_enable = 0;
1114*038d2ef8SMaor Gottlieb 	unsigned int spec_index;
1115*038d2ef8SMaor Gottlieb 	u32 *match_c;
1116*038d2ef8SMaor Gottlieb 	u32 *match_v;
1117*038d2ef8SMaor Gottlieb 	int err = 0;
1118*038d2ef8SMaor Gottlieb 
1119*038d2ef8SMaor Gottlieb 	if (!is_valid_attr(flow_attr))
1120*038d2ef8SMaor Gottlieb 		return ERR_PTR(-EINVAL);
1121*038d2ef8SMaor Gottlieb 
1122*038d2ef8SMaor Gottlieb 	match_c = kzalloc(MLX5_ST_SZ_BYTES(fte_match_param), GFP_KERNEL);
1123*038d2ef8SMaor Gottlieb 	match_v = kzalloc(MLX5_ST_SZ_BYTES(fte_match_param), GFP_KERNEL);
1124*038d2ef8SMaor Gottlieb 	handler = kzalloc(sizeof(*handler), GFP_KERNEL);
1125*038d2ef8SMaor Gottlieb 	if (!handler || !match_c || !match_v) {
1126*038d2ef8SMaor Gottlieb 		err = -ENOMEM;
1127*038d2ef8SMaor Gottlieb 		goto free;
1128*038d2ef8SMaor Gottlieb 	}
1129*038d2ef8SMaor Gottlieb 
1130*038d2ef8SMaor Gottlieb 	INIT_LIST_HEAD(&handler->list);
1131*038d2ef8SMaor Gottlieb 
1132*038d2ef8SMaor Gottlieb 	for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
1133*038d2ef8SMaor Gottlieb 		err = parse_flow_attr(match_c, match_v, ib_flow);
1134*038d2ef8SMaor Gottlieb 		if (err < 0)
1135*038d2ef8SMaor Gottlieb 			goto free;
1136*038d2ef8SMaor Gottlieb 
1137*038d2ef8SMaor Gottlieb 		ib_flow += ((union ib_flow_spec *)ib_flow)->size;
1138*038d2ef8SMaor Gottlieb 	}
1139*038d2ef8SMaor Gottlieb 
1140*038d2ef8SMaor Gottlieb 	/* Outer header support only */
1141*038d2ef8SMaor Gottlieb 	match_criteria_enable = (!outer_header_zero(match_c)) << 0;
1142*038d2ef8SMaor Gottlieb 	handler->rule = mlx5_add_flow_rule(ft, match_criteria_enable,
1143*038d2ef8SMaor Gottlieb 					   match_c, match_v,
1144*038d2ef8SMaor Gottlieb 					   MLX5_FLOW_CONTEXT_ACTION_FWD_DEST,
1145*038d2ef8SMaor Gottlieb 					   MLX5_FS_DEFAULT_FLOW_TAG,
1146*038d2ef8SMaor Gottlieb 					   dst);
1147*038d2ef8SMaor Gottlieb 
1148*038d2ef8SMaor Gottlieb 	if (IS_ERR(handler->rule)) {
1149*038d2ef8SMaor Gottlieb 		err = PTR_ERR(handler->rule);
1150*038d2ef8SMaor Gottlieb 		goto free;
1151*038d2ef8SMaor Gottlieb 	}
1152*038d2ef8SMaor Gottlieb 
1153*038d2ef8SMaor Gottlieb 	handler->prio = ft_prio - dev->flow_db.prios;
1154*038d2ef8SMaor Gottlieb 
1155*038d2ef8SMaor Gottlieb 	ft_prio->flow_table = ft;
1156*038d2ef8SMaor Gottlieb free:
1157*038d2ef8SMaor Gottlieb 	if (err)
1158*038d2ef8SMaor Gottlieb 		kfree(handler);
1159*038d2ef8SMaor Gottlieb 	kfree(match_c);
1160*038d2ef8SMaor Gottlieb 	kfree(match_v);
1161*038d2ef8SMaor Gottlieb 	return err ? ERR_PTR(err) : handler;
1162*038d2ef8SMaor Gottlieb }
1163*038d2ef8SMaor Gottlieb 
1164*038d2ef8SMaor Gottlieb enum {
1165*038d2ef8SMaor Gottlieb 	LEFTOVERS_MC,
1166*038d2ef8SMaor Gottlieb 	LEFTOVERS_UC,
1167*038d2ef8SMaor Gottlieb };
1168*038d2ef8SMaor Gottlieb 
1169*038d2ef8SMaor Gottlieb static struct mlx5_ib_flow_handler *create_leftovers_rule(struct mlx5_ib_dev *dev,
1170*038d2ef8SMaor Gottlieb 							  struct mlx5_ib_flow_prio *ft_prio,
1171*038d2ef8SMaor Gottlieb 							  struct ib_flow_attr *flow_attr,
1172*038d2ef8SMaor Gottlieb 							  struct mlx5_flow_destination *dst)
1173*038d2ef8SMaor Gottlieb {
1174*038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_ucast = NULL;
1175*038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
1176*038d2ef8SMaor Gottlieb 
1177*038d2ef8SMaor Gottlieb 	static struct {
1178*038d2ef8SMaor Gottlieb 		struct ib_flow_attr	flow_attr;
1179*038d2ef8SMaor Gottlieb 		struct ib_flow_spec_eth eth_flow;
1180*038d2ef8SMaor Gottlieb 	} leftovers_specs[] = {
1181*038d2ef8SMaor Gottlieb 		[LEFTOVERS_MC] = {
1182*038d2ef8SMaor Gottlieb 			.flow_attr = {
1183*038d2ef8SMaor Gottlieb 				.num_of_specs = 1,
1184*038d2ef8SMaor Gottlieb 				.size = sizeof(leftovers_specs[0])
1185*038d2ef8SMaor Gottlieb 			},
1186*038d2ef8SMaor Gottlieb 			.eth_flow = {
1187*038d2ef8SMaor Gottlieb 				.type = IB_FLOW_SPEC_ETH,
1188*038d2ef8SMaor Gottlieb 				.size = sizeof(struct ib_flow_spec_eth),
1189*038d2ef8SMaor Gottlieb 				.mask = {.dst_mac = {0x1} },
1190*038d2ef8SMaor Gottlieb 				.val =  {.dst_mac = {0x1} }
1191*038d2ef8SMaor Gottlieb 			}
1192*038d2ef8SMaor Gottlieb 		},
1193*038d2ef8SMaor Gottlieb 		[LEFTOVERS_UC] = {
1194*038d2ef8SMaor Gottlieb 			.flow_attr = {
1195*038d2ef8SMaor Gottlieb 				.num_of_specs = 1,
1196*038d2ef8SMaor Gottlieb 				.size = sizeof(leftovers_specs[0])
1197*038d2ef8SMaor Gottlieb 			},
1198*038d2ef8SMaor Gottlieb 			.eth_flow = {
1199*038d2ef8SMaor Gottlieb 				.type = IB_FLOW_SPEC_ETH,
1200*038d2ef8SMaor Gottlieb 				.size = sizeof(struct ib_flow_spec_eth),
1201*038d2ef8SMaor Gottlieb 				.mask = {.dst_mac = {0x1} },
1202*038d2ef8SMaor Gottlieb 				.val = {.dst_mac = {} }
1203*038d2ef8SMaor Gottlieb 			}
1204*038d2ef8SMaor Gottlieb 		}
1205*038d2ef8SMaor Gottlieb 	};
1206*038d2ef8SMaor Gottlieb 
1207*038d2ef8SMaor Gottlieb 	handler = create_flow_rule(dev, ft_prio,
1208*038d2ef8SMaor Gottlieb 				   &leftovers_specs[LEFTOVERS_MC].flow_attr,
1209*038d2ef8SMaor Gottlieb 				   dst);
1210*038d2ef8SMaor Gottlieb 	if (!IS_ERR(handler) &&
1211*038d2ef8SMaor Gottlieb 	    flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT) {
1212*038d2ef8SMaor Gottlieb 		handler_ucast = create_flow_rule(dev, ft_prio,
1213*038d2ef8SMaor Gottlieb 						 &leftovers_specs[LEFTOVERS_UC].flow_attr,
1214*038d2ef8SMaor Gottlieb 						 dst);
1215*038d2ef8SMaor Gottlieb 		if (IS_ERR(handler_ucast)) {
1216*038d2ef8SMaor Gottlieb 			kfree(handler);
1217*038d2ef8SMaor Gottlieb 			handler = handler_ucast;
1218*038d2ef8SMaor Gottlieb 		} else {
1219*038d2ef8SMaor Gottlieb 			list_add(&handler_ucast->list, &handler->list);
1220*038d2ef8SMaor Gottlieb 		}
1221*038d2ef8SMaor Gottlieb 	}
1222*038d2ef8SMaor Gottlieb 
1223*038d2ef8SMaor Gottlieb 	return handler;
1224*038d2ef8SMaor Gottlieb }
1225*038d2ef8SMaor Gottlieb 
1226*038d2ef8SMaor Gottlieb static struct ib_flow *mlx5_ib_create_flow(struct ib_qp *qp,
1227*038d2ef8SMaor Gottlieb 					   struct ib_flow_attr *flow_attr,
1228*038d2ef8SMaor Gottlieb 					   int domain)
1229*038d2ef8SMaor Gottlieb {
1230*038d2ef8SMaor Gottlieb 	struct mlx5_ib_dev *dev = to_mdev(qp->device);
1231*038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
1232*038d2ef8SMaor Gottlieb 	struct mlx5_flow_destination *dst = NULL;
1233*038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_prio *ft_prio;
1234*038d2ef8SMaor Gottlieb 	int err;
1235*038d2ef8SMaor Gottlieb 
1236*038d2ef8SMaor Gottlieb 	if (flow_attr->priority > MLX5_IB_FLOW_LAST_PRIO)
1237*038d2ef8SMaor Gottlieb 		return ERR_PTR(-ENOSPC);
1238*038d2ef8SMaor Gottlieb 
1239*038d2ef8SMaor Gottlieb 	if (domain != IB_FLOW_DOMAIN_USER ||
1240*038d2ef8SMaor Gottlieb 	    flow_attr->port > MLX5_CAP_GEN(dev->mdev, num_ports) ||
1241*038d2ef8SMaor Gottlieb 	    flow_attr->flags)
1242*038d2ef8SMaor Gottlieb 		return ERR_PTR(-EINVAL);
1243*038d2ef8SMaor Gottlieb 
1244*038d2ef8SMaor Gottlieb 	dst = kzalloc(sizeof(*dst), GFP_KERNEL);
1245*038d2ef8SMaor Gottlieb 	if (!dst)
1246*038d2ef8SMaor Gottlieb 		return ERR_PTR(-ENOMEM);
1247*038d2ef8SMaor Gottlieb 
1248*038d2ef8SMaor Gottlieb 	mutex_lock(&dev->flow_db.lock);
1249*038d2ef8SMaor Gottlieb 
1250*038d2ef8SMaor Gottlieb 	ft_prio = get_flow_table(dev, flow_attr);
1251*038d2ef8SMaor Gottlieb 	if (IS_ERR(ft_prio)) {
1252*038d2ef8SMaor Gottlieb 		err = PTR_ERR(ft_prio);
1253*038d2ef8SMaor Gottlieb 		goto unlock;
1254*038d2ef8SMaor Gottlieb 	}
1255*038d2ef8SMaor Gottlieb 
1256*038d2ef8SMaor Gottlieb 	dst->type = MLX5_FLOW_DESTINATION_TYPE_TIR;
1257*038d2ef8SMaor Gottlieb 	dst->tir_num = to_mqp(qp)->raw_packet_qp.rq.tirn;
1258*038d2ef8SMaor Gottlieb 
1259*038d2ef8SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
1260*038d2ef8SMaor Gottlieb 		handler = create_flow_rule(dev, ft_prio, flow_attr,
1261*038d2ef8SMaor Gottlieb 					   dst);
1262*038d2ef8SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
1263*038d2ef8SMaor Gottlieb 		   flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
1264*038d2ef8SMaor Gottlieb 		handler = create_leftovers_rule(dev, ft_prio, flow_attr,
1265*038d2ef8SMaor Gottlieb 						dst);
1266*038d2ef8SMaor Gottlieb 	} else {
1267*038d2ef8SMaor Gottlieb 		err = -EINVAL;
1268*038d2ef8SMaor Gottlieb 		goto destroy_ft;
1269*038d2ef8SMaor Gottlieb 	}
1270*038d2ef8SMaor Gottlieb 
1271*038d2ef8SMaor Gottlieb 	if (IS_ERR(handler)) {
1272*038d2ef8SMaor Gottlieb 		err = PTR_ERR(handler);
1273*038d2ef8SMaor Gottlieb 		handler = NULL;
1274*038d2ef8SMaor Gottlieb 		goto destroy_ft;
1275*038d2ef8SMaor Gottlieb 	}
1276*038d2ef8SMaor Gottlieb 
1277*038d2ef8SMaor Gottlieb 	ft_prio->refcount++;
1278*038d2ef8SMaor Gottlieb 	mutex_unlock(&dev->flow_db.lock);
1279*038d2ef8SMaor Gottlieb 	kfree(dst);
1280*038d2ef8SMaor Gottlieb 
1281*038d2ef8SMaor Gottlieb 	return &handler->ibflow;
1282*038d2ef8SMaor Gottlieb 
1283*038d2ef8SMaor Gottlieb destroy_ft:
1284*038d2ef8SMaor Gottlieb 	put_flow_table(dev, ft_prio, false);
1285*038d2ef8SMaor Gottlieb unlock:
1286*038d2ef8SMaor Gottlieb 	mutex_unlock(&dev->flow_db.lock);
1287*038d2ef8SMaor Gottlieb 	kfree(dst);
1288*038d2ef8SMaor Gottlieb 	kfree(handler);
1289*038d2ef8SMaor Gottlieb 	return ERR_PTR(err);
1290*038d2ef8SMaor Gottlieb }
1291*038d2ef8SMaor Gottlieb 
1292e126ba97SEli Cohen static int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
1293e126ba97SEli Cohen {
1294e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
1295e126ba97SEli Cohen 	int err;
1296e126ba97SEli Cohen 
12979603b61dSJack Morgenstein 	err = mlx5_core_attach_mcg(dev->mdev, gid, ibqp->qp_num);
1298e126ba97SEli Cohen 	if (err)
1299e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n",
1300e126ba97SEli Cohen 			     ibqp->qp_num, gid->raw);
1301e126ba97SEli Cohen 
1302e126ba97SEli Cohen 	return err;
1303e126ba97SEli Cohen }
1304e126ba97SEli Cohen 
1305e126ba97SEli Cohen static int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
1306e126ba97SEli Cohen {
1307e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
1308e126ba97SEli Cohen 	int err;
1309e126ba97SEli Cohen 
13109603b61dSJack Morgenstein 	err = mlx5_core_detach_mcg(dev->mdev, gid, ibqp->qp_num);
1311e126ba97SEli Cohen 	if (err)
1312e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n",
1313e126ba97SEli Cohen 			     ibqp->qp_num, gid->raw);
1314e126ba97SEli Cohen 
1315e126ba97SEli Cohen 	return err;
1316e126ba97SEli Cohen }
1317e126ba97SEli Cohen 
1318e126ba97SEli Cohen static int init_node_data(struct mlx5_ib_dev *dev)
1319e126ba97SEli Cohen {
13201b5daf11SMajd Dibbiny 	int err;
1321e126ba97SEli Cohen 
13221b5daf11SMajd Dibbiny 	err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc);
1323e126ba97SEli Cohen 	if (err)
1324e126ba97SEli Cohen 		return err;
13251b5daf11SMajd Dibbiny 
13261b5daf11SMajd Dibbiny 	dev->mdev->rev_id = dev->mdev->pdev->revision;
13271b5daf11SMajd Dibbiny 
13281b5daf11SMajd Dibbiny 	return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid);
1329e126ba97SEli Cohen }
1330e126ba97SEli Cohen 
1331e126ba97SEli Cohen static ssize_t show_fw_pages(struct device *device, struct device_attribute *attr,
1332e126ba97SEli Cohen 			     char *buf)
1333e126ba97SEli Cohen {
1334e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
1335e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
1336e126ba97SEli Cohen 
13379603b61dSJack Morgenstein 	return sprintf(buf, "%d\n", dev->mdev->priv.fw_pages);
1338e126ba97SEli Cohen }
1339e126ba97SEli Cohen 
1340e126ba97SEli Cohen static ssize_t show_reg_pages(struct device *device,
1341e126ba97SEli Cohen 			      struct device_attribute *attr, char *buf)
1342e126ba97SEli Cohen {
1343e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
1344e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
1345e126ba97SEli Cohen 
13466aec21f6SHaggai Eran 	return sprintf(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages));
1347e126ba97SEli Cohen }
1348e126ba97SEli Cohen 
1349e126ba97SEli Cohen static ssize_t show_hca(struct device *device, struct device_attribute *attr,
1350e126ba97SEli Cohen 			char *buf)
1351e126ba97SEli Cohen {
1352e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
1353e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
13549603b61dSJack Morgenstein 	return sprintf(buf, "MT%d\n", dev->mdev->pdev->device);
1355e126ba97SEli Cohen }
1356e126ba97SEli Cohen 
1357e126ba97SEli Cohen static ssize_t show_fw_ver(struct device *device, struct device_attribute *attr,
1358e126ba97SEli Cohen 			   char *buf)
1359e126ba97SEli Cohen {
1360e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
1361e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
13629603b61dSJack Morgenstein 	return sprintf(buf, "%d.%d.%d\n", fw_rev_maj(dev->mdev),
13639603b61dSJack Morgenstein 		       fw_rev_min(dev->mdev), fw_rev_sub(dev->mdev));
1364e126ba97SEli Cohen }
1365e126ba97SEli Cohen 
1366e126ba97SEli Cohen static ssize_t show_rev(struct device *device, struct device_attribute *attr,
1367e126ba97SEli Cohen 			char *buf)
1368e126ba97SEli Cohen {
1369e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
1370e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
13719603b61dSJack Morgenstein 	return sprintf(buf, "%x\n", dev->mdev->rev_id);
1372e126ba97SEli Cohen }
1373e126ba97SEli Cohen 
1374e126ba97SEli Cohen static ssize_t show_board(struct device *device, struct device_attribute *attr,
1375e126ba97SEli Cohen 			  char *buf)
1376e126ba97SEli Cohen {
1377e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
1378e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
1379e126ba97SEli Cohen 	return sprintf(buf, "%.*s\n", MLX5_BOARD_ID_LEN,
13809603b61dSJack Morgenstein 		       dev->mdev->board_id);
1381e126ba97SEli Cohen }
1382e126ba97SEli Cohen 
1383e126ba97SEli Cohen static DEVICE_ATTR(hw_rev,   S_IRUGO, show_rev,    NULL);
1384e126ba97SEli Cohen static DEVICE_ATTR(fw_ver,   S_IRUGO, show_fw_ver, NULL);
1385e126ba97SEli Cohen static DEVICE_ATTR(hca_type, S_IRUGO, show_hca,    NULL);
1386e126ba97SEli Cohen static DEVICE_ATTR(board_id, S_IRUGO, show_board,  NULL);
1387e126ba97SEli Cohen static DEVICE_ATTR(fw_pages, S_IRUGO, show_fw_pages, NULL);
1388e126ba97SEli Cohen static DEVICE_ATTR(reg_pages, S_IRUGO, show_reg_pages, NULL);
1389e126ba97SEli Cohen 
1390e126ba97SEli Cohen static struct device_attribute *mlx5_class_attributes[] = {
1391e126ba97SEli Cohen 	&dev_attr_hw_rev,
1392e126ba97SEli Cohen 	&dev_attr_fw_ver,
1393e126ba97SEli Cohen 	&dev_attr_hca_type,
1394e126ba97SEli Cohen 	&dev_attr_board_id,
1395e126ba97SEli Cohen 	&dev_attr_fw_pages,
1396e126ba97SEli Cohen 	&dev_attr_reg_pages,
1397e126ba97SEli Cohen };
1398e126ba97SEli Cohen 
13999603b61dSJack Morgenstein static void mlx5_ib_event(struct mlx5_core_dev *dev, void *context,
14004d2f9bbbSJack Morgenstein 			  enum mlx5_dev_event event, unsigned long param)
1401e126ba97SEli Cohen {
14029603b61dSJack Morgenstein 	struct mlx5_ib_dev *ibdev = (struct mlx5_ib_dev *)context;
1403e126ba97SEli Cohen 	struct ib_event ibev;
14049603b61dSJack Morgenstein 
1405e126ba97SEli Cohen 	u8 port = 0;
1406e126ba97SEli Cohen 
1407e126ba97SEli Cohen 	switch (event) {
1408e126ba97SEli Cohen 	case MLX5_DEV_EVENT_SYS_ERROR:
1409e126ba97SEli Cohen 		ibdev->ib_active = false;
1410e126ba97SEli Cohen 		ibev.event = IB_EVENT_DEVICE_FATAL;
1411e126ba97SEli Cohen 		break;
1412e126ba97SEli Cohen 
1413e126ba97SEli Cohen 	case MLX5_DEV_EVENT_PORT_UP:
1414e126ba97SEli Cohen 		ibev.event = IB_EVENT_PORT_ACTIVE;
14154d2f9bbbSJack Morgenstein 		port = (u8)param;
1416e126ba97SEli Cohen 		break;
1417e126ba97SEli Cohen 
1418e126ba97SEli Cohen 	case MLX5_DEV_EVENT_PORT_DOWN:
1419e126ba97SEli Cohen 		ibev.event = IB_EVENT_PORT_ERR;
14204d2f9bbbSJack Morgenstein 		port = (u8)param;
1421e126ba97SEli Cohen 		break;
1422e126ba97SEli Cohen 
1423e126ba97SEli Cohen 	case MLX5_DEV_EVENT_PORT_INITIALIZED:
1424e126ba97SEli Cohen 		/* not used by ULPs */
1425e126ba97SEli Cohen 		return;
1426e126ba97SEli Cohen 
1427e126ba97SEli Cohen 	case MLX5_DEV_EVENT_LID_CHANGE:
1428e126ba97SEli Cohen 		ibev.event = IB_EVENT_LID_CHANGE;
14294d2f9bbbSJack Morgenstein 		port = (u8)param;
1430e126ba97SEli Cohen 		break;
1431e126ba97SEli Cohen 
1432e126ba97SEli Cohen 	case MLX5_DEV_EVENT_PKEY_CHANGE:
1433e126ba97SEli Cohen 		ibev.event = IB_EVENT_PKEY_CHANGE;
14344d2f9bbbSJack Morgenstein 		port = (u8)param;
1435e126ba97SEli Cohen 		break;
1436e126ba97SEli Cohen 
1437e126ba97SEli Cohen 	case MLX5_DEV_EVENT_GUID_CHANGE:
1438e126ba97SEli Cohen 		ibev.event = IB_EVENT_GID_CHANGE;
14394d2f9bbbSJack Morgenstein 		port = (u8)param;
1440e126ba97SEli Cohen 		break;
1441e126ba97SEli Cohen 
1442e126ba97SEli Cohen 	case MLX5_DEV_EVENT_CLIENT_REREG:
1443e126ba97SEli Cohen 		ibev.event = IB_EVENT_CLIENT_REREGISTER;
14444d2f9bbbSJack Morgenstein 		port = (u8)param;
1445e126ba97SEli Cohen 		break;
1446e126ba97SEli Cohen 	}
1447e126ba97SEli Cohen 
1448e126ba97SEli Cohen 	ibev.device	      = &ibdev->ib_dev;
1449e126ba97SEli Cohen 	ibev.element.port_num = port;
1450e126ba97SEli Cohen 
1451a0c84c32SEli Cohen 	if (port < 1 || port > ibdev->num_ports) {
1452a0c84c32SEli Cohen 		mlx5_ib_warn(ibdev, "warning: event on port %d\n", port);
1453a0c84c32SEli Cohen 		return;
1454a0c84c32SEli Cohen 	}
1455a0c84c32SEli Cohen 
1456e126ba97SEli Cohen 	if (ibdev->ib_active)
1457e126ba97SEli Cohen 		ib_dispatch_event(&ibev);
1458e126ba97SEli Cohen }
1459e126ba97SEli Cohen 
1460e126ba97SEli Cohen static void get_ext_port_caps(struct mlx5_ib_dev *dev)
1461e126ba97SEli Cohen {
1462e126ba97SEli Cohen 	int port;
1463e126ba97SEli Cohen 
1464938fe83cSSaeed Mahameed 	for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++)
1465e126ba97SEli Cohen 		mlx5_query_ext_port_caps(dev, port);
1466e126ba97SEli Cohen }
1467e126ba97SEli Cohen 
1468e126ba97SEli Cohen static int get_port_caps(struct mlx5_ib_dev *dev)
1469e126ba97SEli Cohen {
1470e126ba97SEli Cohen 	struct ib_device_attr *dprops = NULL;
1471e126ba97SEli Cohen 	struct ib_port_attr *pprops = NULL;
1472f614fc15SDan Carpenter 	int err = -ENOMEM;
1473e126ba97SEli Cohen 	int port;
14742528e33eSMatan Barak 	struct ib_udata uhw = {.inlen = 0, .outlen = 0};
1475e126ba97SEli Cohen 
1476e126ba97SEli Cohen 	pprops = kmalloc(sizeof(*pprops), GFP_KERNEL);
1477e126ba97SEli Cohen 	if (!pprops)
1478e126ba97SEli Cohen 		goto out;
1479e126ba97SEli Cohen 
1480e126ba97SEli Cohen 	dprops = kmalloc(sizeof(*dprops), GFP_KERNEL);
1481e126ba97SEli Cohen 	if (!dprops)
1482e126ba97SEli Cohen 		goto out;
1483e126ba97SEli Cohen 
14842528e33eSMatan Barak 	err = mlx5_ib_query_device(&dev->ib_dev, dprops, &uhw);
1485e126ba97SEli Cohen 	if (err) {
1486e126ba97SEli Cohen 		mlx5_ib_warn(dev, "query_device failed %d\n", err);
1487e126ba97SEli Cohen 		goto out;
1488e126ba97SEli Cohen 	}
1489e126ba97SEli Cohen 
1490938fe83cSSaeed Mahameed 	for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++) {
1491e126ba97SEli Cohen 		err = mlx5_ib_query_port(&dev->ib_dev, port, pprops);
1492e126ba97SEli Cohen 		if (err) {
1493938fe83cSSaeed Mahameed 			mlx5_ib_warn(dev, "query_port %d failed %d\n",
1494938fe83cSSaeed Mahameed 				     port, err);
1495e126ba97SEli Cohen 			break;
1496e126ba97SEli Cohen 		}
1497938fe83cSSaeed Mahameed 		dev->mdev->port_caps[port - 1].pkey_table_len =
1498938fe83cSSaeed Mahameed 						dprops->max_pkeys;
1499938fe83cSSaeed Mahameed 		dev->mdev->port_caps[port - 1].gid_table_len =
1500938fe83cSSaeed Mahameed 						pprops->gid_tbl_len;
1501e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "pkey_table_len %d, gid_table_len %d\n",
1502e126ba97SEli Cohen 			    dprops->max_pkeys, pprops->gid_tbl_len);
1503e126ba97SEli Cohen 	}
1504e126ba97SEli Cohen 
1505e126ba97SEli Cohen out:
1506e126ba97SEli Cohen 	kfree(pprops);
1507e126ba97SEli Cohen 	kfree(dprops);
1508e126ba97SEli Cohen 
1509e126ba97SEli Cohen 	return err;
1510e126ba97SEli Cohen }
1511e126ba97SEli Cohen 
1512e126ba97SEli Cohen static void destroy_umrc_res(struct mlx5_ib_dev *dev)
1513e126ba97SEli Cohen {
1514e126ba97SEli Cohen 	int err;
1515e126ba97SEli Cohen 
1516e126ba97SEli Cohen 	err = mlx5_mr_cache_cleanup(dev);
1517e126ba97SEli Cohen 	if (err)
1518e126ba97SEli Cohen 		mlx5_ib_warn(dev, "mr cache cleanup failed\n");
1519e126ba97SEli Cohen 
1520e126ba97SEli Cohen 	mlx5_ib_destroy_qp(dev->umrc.qp);
1521e126ba97SEli Cohen 	ib_destroy_cq(dev->umrc.cq);
1522e126ba97SEli Cohen 	ib_dealloc_pd(dev->umrc.pd);
1523e126ba97SEli Cohen }
1524e126ba97SEli Cohen 
1525e126ba97SEli Cohen enum {
1526e126ba97SEli Cohen 	MAX_UMR_WR = 128,
1527e126ba97SEli Cohen };
1528e126ba97SEli Cohen 
1529e126ba97SEli Cohen static int create_umr_res(struct mlx5_ib_dev *dev)
1530e126ba97SEli Cohen {
1531e126ba97SEli Cohen 	struct ib_qp_init_attr *init_attr = NULL;
1532e126ba97SEli Cohen 	struct ib_qp_attr *attr = NULL;
1533e126ba97SEli Cohen 	struct ib_pd *pd;
1534e126ba97SEli Cohen 	struct ib_cq *cq;
1535e126ba97SEli Cohen 	struct ib_qp *qp;
15368e37210bSMatan Barak 	struct ib_cq_init_attr cq_attr = {};
1537e126ba97SEli Cohen 	int ret;
1538e126ba97SEli Cohen 
1539e126ba97SEli Cohen 	attr = kzalloc(sizeof(*attr), GFP_KERNEL);
1540e126ba97SEli Cohen 	init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL);
1541e126ba97SEli Cohen 	if (!attr || !init_attr) {
1542e126ba97SEli Cohen 		ret = -ENOMEM;
1543e126ba97SEli Cohen 		goto error_0;
1544e126ba97SEli Cohen 	}
1545e126ba97SEli Cohen 
1546e126ba97SEli Cohen 	pd = ib_alloc_pd(&dev->ib_dev);
1547e126ba97SEli Cohen 	if (IS_ERR(pd)) {
1548e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create PD for sync UMR QP\n");
1549e126ba97SEli Cohen 		ret = PTR_ERR(pd);
1550e126ba97SEli Cohen 		goto error_0;
1551e126ba97SEli Cohen 	}
1552e126ba97SEli Cohen 
15538e37210bSMatan Barak 	cq_attr.cqe = 128;
15548e37210bSMatan Barak 	cq = ib_create_cq(&dev->ib_dev, mlx5_umr_cq_handler, NULL, NULL,
15558e37210bSMatan Barak 			  &cq_attr);
1556e126ba97SEli Cohen 	if (IS_ERR(cq)) {
1557e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create CQ for sync UMR QP\n");
1558e126ba97SEli Cohen 		ret = PTR_ERR(cq);
1559e126ba97SEli Cohen 		goto error_2;
1560e126ba97SEli Cohen 	}
1561e126ba97SEli Cohen 	ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
1562e126ba97SEli Cohen 
1563e126ba97SEli Cohen 	init_attr->send_cq = cq;
1564e126ba97SEli Cohen 	init_attr->recv_cq = cq;
1565e126ba97SEli Cohen 	init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
1566e126ba97SEli Cohen 	init_attr->cap.max_send_wr = MAX_UMR_WR;
1567e126ba97SEli Cohen 	init_attr->cap.max_send_sge = 1;
1568e126ba97SEli Cohen 	init_attr->qp_type = MLX5_IB_QPT_REG_UMR;
1569e126ba97SEli Cohen 	init_attr->port_num = 1;
1570e126ba97SEli Cohen 	qp = mlx5_ib_create_qp(pd, init_attr, NULL);
1571e126ba97SEli Cohen 	if (IS_ERR(qp)) {
1572e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create sync UMR QP\n");
1573e126ba97SEli Cohen 		ret = PTR_ERR(qp);
1574e126ba97SEli Cohen 		goto error_3;
1575e126ba97SEli Cohen 	}
1576e126ba97SEli Cohen 	qp->device     = &dev->ib_dev;
1577e126ba97SEli Cohen 	qp->real_qp    = qp;
1578e126ba97SEli Cohen 	qp->uobject    = NULL;
1579e126ba97SEli Cohen 	qp->qp_type    = MLX5_IB_QPT_REG_UMR;
1580e126ba97SEli Cohen 
1581e126ba97SEli Cohen 	attr->qp_state = IB_QPS_INIT;
1582e126ba97SEli Cohen 	attr->port_num = 1;
1583e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_PKEY_INDEX |
1584e126ba97SEli Cohen 				IB_QP_PORT, NULL);
1585e126ba97SEli Cohen 	if (ret) {
1586e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify UMR QP\n");
1587e126ba97SEli Cohen 		goto error_4;
1588e126ba97SEli Cohen 	}
1589e126ba97SEli Cohen 
1590e126ba97SEli Cohen 	memset(attr, 0, sizeof(*attr));
1591e126ba97SEli Cohen 	attr->qp_state = IB_QPS_RTR;
1592e126ba97SEli Cohen 	attr->path_mtu = IB_MTU_256;
1593e126ba97SEli Cohen 
1594e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
1595e126ba97SEli Cohen 	if (ret) {
1596e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify umr QP to rtr\n");
1597e126ba97SEli Cohen 		goto error_4;
1598e126ba97SEli Cohen 	}
1599e126ba97SEli Cohen 
1600e126ba97SEli Cohen 	memset(attr, 0, sizeof(*attr));
1601e126ba97SEli Cohen 	attr->qp_state = IB_QPS_RTS;
1602e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
1603e126ba97SEli Cohen 	if (ret) {
1604e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify umr QP to rts\n");
1605e126ba97SEli Cohen 		goto error_4;
1606e126ba97SEli Cohen 	}
1607e126ba97SEli Cohen 
1608e126ba97SEli Cohen 	dev->umrc.qp = qp;
1609e126ba97SEli Cohen 	dev->umrc.cq = cq;
1610e126ba97SEli Cohen 	dev->umrc.pd = pd;
1611e126ba97SEli Cohen 
1612e126ba97SEli Cohen 	sema_init(&dev->umrc.sem, MAX_UMR_WR);
1613e126ba97SEli Cohen 	ret = mlx5_mr_cache_init(dev);
1614e126ba97SEli Cohen 	if (ret) {
1615e126ba97SEli Cohen 		mlx5_ib_warn(dev, "mr cache init failed %d\n", ret);
1616e126ba97SEli Cohen 		goto error_4;
1617e126ba97SEli Cohen 	}
1618e126ba97SEli Cohen 
1619e126ba97SEli Cohen 	kfree(attr);
1620e126ba97SEli Cohen 	kfree(init_attr);
1621e126ba97SEli Cohen 
1622e126ba97SEli Cohen 	return 0;
1623e126ba97SEli Cohen 
1624e126ba97SEli Cohen error_4:
1625e126ba97SEli Cohen 	mlx5_ib_destroy_qp(qp);
1626e126ba97SEli Cohen 
1627e126ba97SEli Cohen error_3:
1628e126ba97SEli Cohen 	ib_destroy_cq(cq);
1629e126ba97SEli Cohen 
1630e126ba97SEli Cohen error_2:
1631e126ba97SEli Cohen 	ib_dealloc_pd(pd);
1632e126ba97SEli Cohen 
1633e126ba97SEli Cohen error_0:
1634e126ba97SEli Cohen 	kfree(attr);
1635e126ba97SEli Cohen 	kfree(init_attr);
1636e126ba97SEli Cohen 	return ret;
1637e126ba97SEli Cohen }
1638e126ba97SEli Cohen 
1639e126ba97SEli Cohen static int create_dev_resources(struct mlx5_ib_resources *devr)
1640e126ba97SEli Cohen {
1641e126ba97SEli Cohen 	struct ib_srq_init_attr attr;
1642e126ba97SEli Cohen 	struct mlx5_ib_dev *dev;
1643bcf4c1eaSMatan Barak 	struct ib_cq_init_attr cq_attr = {.cqe = 1};
1644e126ba97SEli Cohen 	int ret = 0;
1645e126ba97SEli Cohen 
1646e126ba97SEli Cohen 	dev = container_of(devr, struct mlx5_ib_dev, devr);
1647e126ba97SEli Cohen 
1648e126ba97SEli Cohen 	devr->p0 = mlx5_ib_alloc_pd(&dev->ib_dev, NULL, NULL);
1649e126ba97SEli Cohen 	if (IS_ERR(devr->p0)) {
1650e126ba97SEli Cohen 		ret = PTR_ERR(devr->p0);
1651e126ba97SEli Cohen 		goto error0;
1652e126ba97SEli Cohen 	}
1653e126ba97SEli Cohen 	devr->p0->device  = &dev->ib_dev;
1654e126ba97SEli Cohen 	devr->p0->uobject = NULL;
1655e126ba97SEli Cohen 	atomic_set(&devr->p0->usecnt, 0);
1656e126ba97SEli Cohen 
1657bcf4c1eaSMatan Barak 	devr->c0 = mlx5_ib_create_cq(&dev->ib_dev, &cq_attr, NULL, NULL);
1658e126ba97SEli Cohen 	if (IS_ERR(devr->c0)) {
1659e126ba97SEli Cohen 		ret = PTR_ERR(devr->c0);
1660e126ba97SEli Cohen 		goto error1;
1661e126ba97SEli Cohen 	}
1662e126ba97SEli Cohen 	devr->c0->device        = &dev->ib_dev;
1663e126ba97SEli Cohen 	devr->c0->uobject       = NULL;
1664e126ba97SEli Cohen 	devr->c0->comp_handler  = NULL;
1665e126ba97SEli Cohen 	devr->c0->event_handler = NULL;
1666e126ba97SEli Cohen 	devr->c0->cq_context    = NULL;
1667e126ba97SEli Cohen 	atomic_set(&devr->c0->usecnt, 0);
1668e126ba97SEli Cohen 
1669e126ba97SEli Cohen 	devr->x0 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
1670e126ba97SEli Cohen 	if (IS_ERR(devr->x0)) {
1671e126ba97SEli Cohen 		ret = PTR_ERR(devr->x0);
1672e126ba97SEli Cohen 		goto error2;
1673e126ba97SEli Cohen 	}
1674e126ba97SEli Cohen 	devr->x0->device = &dev->ib_dev;
1675e126ba97SEli Cohen 	devr->x0->inode = NULL;
1676e126ba97SEli Cohen 	atomic_set(&devr->x0->usecnt, 0);
1677e126ba97SEli Cohen 	mutex_init(&devr->x0->tgt_qp_mutex);
1678e126ba97SEli Cohen 	INIT_LIST_HEAD(&devr->x0->tgt_qp_list);
1679e126ba97SEli Cohen 
1680e126ba97SEli Cohen 	devr->x1 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
1681e126ba97SEli Cohen 	if (IS_ERR(devr->x1)) {
1682e126ba97SEli Cohen 		ret = PTR_ERR(devr->x1);
1683e126ba97SEli Cohen 		goto error3;
1684e126ba97SEli Cohen 	}
1685e126ba97SEli Cohen 	devr->x1->device = &dev->ib_dev;
1686e126ba97SEli Cohen 	devr->x1->inode = NULL;
1687e126ba97SEli Cohen 	atomic_set(&devr->x1->usecnt, 0);
1688e126ba97SEli Cohen 	mutex_init(&devr->x1->tgt_qp_mutex);
1689e126ba97SEli Cohen 	INIT_LIST_HEAD(&devr->x1->tgt_qp_list);
1690e126ba97SEli Cohen 
1691e126ba97SEli Cohen 	memset(&attr, 0, sizeof(attr));
1692e126ba97SEli Cohen 	attr.attr.max_sge = 1;
1693e126ba97SEli Cohen 	attr.attr.max_wr = 1;
1694e126ba97SEli Cohen 	attr.srq_type = IB_SRQT_XRC;
1695e126ba97SEli Cohen 	attr.ext.xrc.cq = devr->c0;
1696e126ba97SEli Cohen 	attr.ext.xrc.xrcd = devr->x0;
1697e126ba97SEli Cohen 
1698e126ba97SEli Cohen 	devr->s0 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
1699e126ba97SEli Cohen 	if (IS_ERR(devr->s0)) {
1700e126ba97SEli Cohen 		ret = PTR_ERR(devr->s0);
1701e126ba97SEli Cohen 		goto error4;
1702e126ba97SEli Cohen 	}
1703e126ba97SEli Cohen 	devr->s0->device	= &dev->ib_dev;
1704e126ba97SEli Cohen 	devr->s0->pd		= devr->p0;
1705e126ba97SEli Cohen 	devr->s0->uobject       = NULL;
1706e126ba97SEli Cohen 	devr->s0->event_handler = NULL;
1707e126ba97SEli Cohen 	devr->s0->srq_context   = NULL;
1708e126ba97SEli Cohen 	devr->s0->srq_type      = IB_SRQT_XRC;
1709e126ba97SEli Cohen 	devr->s0->ext.xrc.xrcd	= devr->x0;
1710e126ba97SEli Cohen 	devr->s0->ext.xrc.cq	= devr->c0;
1711e126ba97SEli Cohen 	atomic_inc(&devr->s0->ext.xrc.xrcd->usecnt);
1712e126ba97SEli Cohen 	atomic_inc(&devr->s0->ext.xrc.cq->usecnt);
1713e126ba97SEli Cohen 	atomic_inc(&devr->p0->usecnt);
1714e126ba97SEli Cohen 	atomic_set(&devr->s0->usecnt, 0);
1715e126ba97SEli Cohen 
17164aa17b28SHaggai Abramonvsky 	memset(&attr, 0, sizeof(attr));
17174aa17b28SHaggai Abramonvsky 	attr.attr.max_sge = 1;
17184aa17b28SHaggai Abramonvsky 	attr.attr.max_wr = 1;
17194aa17b28SHaggai Abramonvsky 	attr.srq_type = IB_SRQT_BASIC;
17204aa17b28SHaggai Abramonvsky 	devr->s1 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
17214aa17b28SHaggai Abramonvsky 	if (IS_ERR(devr->s1)) {
17224aa17b28SHaggai Abramonvsky 		ret = PTR_ERR(devr->s1);
17234aa17b28SHaggai Abramonvsky 		goto error5;
17244aa17b28SHaggai Abramonvsky 	}
17254aa17b28SHaggai Abramonvsky 	devr->s1->device	= &dev->ib_dev;
17264aa17b28SHaggai Abramonvsky 	devr->s1->pd		= devr->p0;
17274aa17b28SHaggai Abramonvsky 	devr->s1->uobject       = NULL;
17284aa17b28SHaggai Abramonvsky 	devr->s1->event_handler = NULL;
17294aa17b28SHaggai Abramonvsky 	devr->s1->srq_context   = NULL;
17304aa17b28SHaggai Abramonvsky 	devr->s1->srq_type      = IB_SRQT_BASIC;
17314aa17b28SHaggai Abramonvsky 	devr->s1->ext.xrc.cq	= devr->c0;
17324aa17b28SHaggai Abramonvsky 	atomic_inc(&devr->p0->usecnt);
17334aa17b28SHaggai Abramonvsky 	atomic_set(&devr->s0->usecnt, 0);
17344aa17b28SHaggai Abramonvsky 
1735e126ba97SEli Cohen 	return 0;
1736e126ba97SEli Cohen 
17374aa17b28SHaggai Abramonvsky error5:
17384aa17b28SHaggai Abramonvsky 	mlx5_ib_destroy_srq(devr->s0);
1739e126ba97SEli Cohen error4:
1740e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x1);
1741e126ba97SEli Cohen error3:
1742e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x0);
1743e126ba97SEli Cohen error2:
1744e126ba97SEli Cohen 	mlx5_ib_destroy_cq(devr->c0);
1745e126ba97SEli Cohen error1:
1746e126ba97SEli Cohen 	mlx5_ib_dealloc_pd(devr->p0);
1747e126ba97SEli Cohen error0:
1748e126ba97SEli Cohen 	return ret;
1749e126ba97SEli Cohen }
1750e126ba97SEli Cohen 
1751e126ba97SEli Cohen static void destroy_dev_resources(struct mlx5_ib_resources *devr)
1752e126ba97SEli Cohen {
17534aa17b28SHaggai Abramonvsky 	mlx5_ib_destroy_srq(devr->s1);
1754e126ba97SEli Cohen 	mlx5_ib_destroy_srq(devr->s0);
1755e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x0);
1756e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x1);
1757e126ba97SEli Cohen 	mlx5_ib_destroy_cq(devr->c0);
1758e126ba97SEli Cohen 	mlx5_ib_dealloc_pd(devr->p0);
1759e126ba97SEli Cohen }
1760e126ba97SEli Cohen 
17617738613eSIra Weiny static int mlx5_port_immutable(struct ib_device *ibdev, u8 port_num,
17627738613eSIra Weiny 			       struct ib_port_immutable *immutable)
17637738613eSIra Weiny {
17647738613eSIra Weiny 	struct ib_port_attr attr;
17657738613eSIra Weiny 	int err;
17667738613eSIra Weiny 
17677738613eSIra Weiny 	err = mlx5_ib_query_port(ibdev, port_num, &attr);
17687738613eSIra Weiny 	if (err)
17697738613eSIra Weiny 		return err;
17707738613eSIra Weiny 
17717738613eSIra Weiny 	immutable->pkey_tbl_len = attr.pkey_tbl_len;
17727738613eSIra Weiny 	immutable->gid_tbl_len = attr.gid_tbl_len;
1773f9b22e35SIra Weiny 	immutable->core_cap_flags = RDMA_CORE_PORT_IBA_IB;
1774337877a4SIra Weiny 	immutable->max_mad_size = IB_MGMT_MAD_SIZE;
17757738613eSIra Weiny 
17767738613eSIra Weiny 	return 0;
17777738613eSIra Weiny }
17787738613eSIra Weiny 
17799603b61dSJack Morgenstein static void *mlx5_ib_add(struct mlx5_core_dev *mdev)
1780e126ba97SEli Cohen {
1781e126ba97SEli Cohen 	struct mlx5_ib_dev *dev;
1782e126ba97SEli Cohen 	int err;
1783e126ba97SEli Cohen 	int i;
1784e126ba97SEli Cohen 
1785647241eaSMajd Dibbiny 	/* don't create IB instance over Eth ports, no RoCE yet! */
1786647241eaSMajd Dibbiny 	if (MLX5_CAP_GEN(mdev, port_type) == MLX5_CAP_PORT_TYPE_ETH)
1787647241eaSMajd Dibbiny 		return NULL;
1788647241eaSMajd Dibbiny 
1789e126ba97SEli Cohen 	printk_once(KERN_INFO "%s", mlx5_version);
1790e126ba97SEli Cohen 
1791e126ba97SEli Cohen 	dev = (struct mlx5_ib_dev *)ib_alloc_device(sizeof(*dev));
1792e126ba97SEli Cohen 	if (!dev)
17939603b61dSJack Morgenstein 		return NULL;
1794e126ba97SEli Cohen 
17959603b61dSJack Morgenstein 	dev->mdev = mdev;
1796e126ba97SEli Cohen 
1797e126ba97SEli Cohen 	err = get_port_caps(dev);
1798e126ba97SEli Cohen 	if (err)
17999603b61dSJack Morgenstein 		goto err_dealloc;
1800e126ba97SEli Cohen 
18011b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(dev))
1802e126ba97SEli Cohen 		get_ext_port_caps(dev);
1803e126ba97SEli Cohen 
1804e126ba97SEli Cohen 	MLX5_INIT_DOORBELL_LOCK(&dev->uar_lock);
1805e126ba97SEli Cohen 
1806e126ba97SEli Cohen 	strlcpy(dev->ib_dev.name, "mlx5_%d", IB_DEVICE_NAME_MAX);
1807e126ba97SEli Cohen 	dev->ib_dev.owner		= THIS_MODULE;
1808e126ba97SEli Cohen 	dev->ib_dev.node_type		= RDMA_NODE_IB_CA;
1809c6790aa9SSagi Grimberg 	dev->ib_dev.local_dma_lkey	= 0 /* not supported for now */;
1810938fe83cSSaeed Mahameed 	dev->num_ports		= MLX5_CAP_GEN(mdev, num_ports);
1811e126ba97SEli Cohen 	dev->ib_dev.phys_port_cnt     = dev->num_ports;
1812233d05d2SSaeed Mahameed 	dev->ib_dev.num_comp_vectors    =
1813233d05d2SSaeed Mahameed 		dev->mdev->priv.eq_table.num_comp_vectors;
1814e126ba97SEli Cohen 	dev->ib_dev.dma_device	= &mdev->pdev->dev;
1815e126ba97SEli Cohen 
1816e126ba97SEli Cohen 	dev->ib_dev.uverbs_abi_ver	= MLX5_IB_UVERBS_ABI_VERSION;
1817e126ba97SEli Cohen 	dev->ib_dev.uverbs_cmd_mask	=
1818e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_GET_CONTEXT)		|
1819e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_DEVICE)	|
1820e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_PORT)		|
1821e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_ALLOC_PD)		|
1822e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DEALLOC_PD)		|
1823e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_REG_MR)		|
1824e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DEREG_MR)		|
1825e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL)	|
1826e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_CQ)		|
1827e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_RESIZE_CQ)		|
1828e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_CQ)		|
1829e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_QP)		|
1830e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_MODIFY_QP)		|
1831e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_QP)		|
1832e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_QP)		|
1833e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_ATTACH_MCAST)	|
1834e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DETACH_MCAST)	|
1835e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_SRQ)		|
1836e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_MODIFY_SRQ)		|
1837e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_SRQ)		|
1838e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_SRQ)		|
1839e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_XSRQ)		|
1840e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_OPEN_QP);
18411707cb4aSHaggai Eran 	dev->ib_dev.uverbs_ex_cmd_mask =
18421707cb4aSHaggai Eran 		(1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE);
1843e126ba97SEli Cohen 
1844e126ba97SEli Cohen 	dev->ib_dev.query_device	= mlx5_ib_query_device;
1845e126ba97SEli Cohen 	dev->ib_dev.query_port		= mlx5_ib_query_port;
1846e126ba97SEli Cohen 	dev->ib_dev.query_gid		= mlx5_ib_query_gid;
1847e126ba97SEli Cohen 	dev->ib_dev.query_pkey		= mlx5_ib_query_pkey;
1848e126ba97SEli Cohen 	dev->ib_dev.modify_device	= mlx5_ib_modify_device;
1849e126ba97SEli Cohen 	dev->ib_dev.modify_port		= mlx5_ib_modify_port;
1850e126ba97SEli Cohen 	dev->ib_dev.alloc_ucontext	= mlx5_ib_alloc_ucontext;
1851e126ba97SEli Cohen 	dev->ib_dev.dealloc_ucontext	= mlx5_ib_dealloc_ucontext;
1852e126ba97SEli Cohen 	dev->ib_dev.mmap		= mlx5_ib_mmap;
1853e126ba97SEli Cohen 	dev->ib_dev.alloc_pd		= mlx5_ib_alloc_pd;
1854e126ba97SEli Cohen 	dev->ib_dev.dealloc_pd		= mlx5_ib_dealloc_pd;
1855e126ba97SEli Cohen 	dev->ib_dev.create_ah		= mlx5_ib_create_ah;
1856e126ba97SEli Cohen 	dev->ib_dev.query_ah		= mlx5_ib_query_ah;
1857e126ba97SEli Cohen 	dev->ib_dev.destroy_ah		= mlx5_ib_destroy_ah;
1858e126ba97SEli Cohen 	dev->ib_dev.create_srq		= mlx5_ib_create_srq;
1859e126ba97SEli Cohen 	dev->ib_dev.modify_srq		= mlx5_ib_modify_srq;
1860e126ba97SEli Cohen 	dev->ib_dev.query_srq		= mlx5_ib_query_srq;
1861e126ba97SEli Cohen 	dev->ib_dev.destroy_srq		= mlx5_ib_destroy_srq;
1862e126ba97SEli Cohen 	dev->ib_dev.post_srq_recv	= mlx5_ib_post_srq_recv;
1863e126ba97SEli Cohen 	dev->ib_dev.create_qp		= mlx5_ib_create_qp;
1864e126ba97SEli Cohen 	dev->ib_dev.modify_qp		= mlx5_ib_modify_qp;
1865e126ba97SEli Cohen 	dev->ib_dev.query_qp		= mlx5_ib_query_qp;
1866e126ba97SEli Cohen 	dev->ib_dev.destroy_qp		= mlx5_ib_destroy_qp;
1867e126ba97SEli Cohen 	dev->ib_dev.post_send		= mlx5_ib_post_send;
1868e126ba97SEli Cohen 	dev->ib_dev.post_recv		= mlx5_ib_post_recv;
1869e126ba97SEli Cohen 	dev->ib_dev.create_cq		= mlx5_ib_create_cq;
1870e126ba97SEli Cohen 	dev->ib_dev.modify_cq		= mlx5_ib_modify_cq;
1871e126ba97SEli Cohen 	dev->ib_dev.resize_cq		= mlx5_ib_resize_cq;
1872e126ba97SEli Cohen 	dev->ib_dev.destroy_cq		= mlx5_ib_destroy_cq;
1873e126ba97SEli Cohen 	dev->ib_dev.poll_cq		= mlx5_ib_poll_cq;
1874e126ba97SEli Cohen 	dev->ib_dev.req_notify_cq	= mlx5_ib_arm_cq;
1875e126ba97SEli Cohen 	dev->ib_dev.get_dma_mr		= mlx5_ib_get_dma_mr;
1876e126ba97SEli Cohen 	dev->ib_dev.reg_user_mr		= mlx5_ib_reg_user_mr;
1877e126ba97SEli Cohen 	dev->ib_dev.dereg_mr		= mlx5_ib_dereg_mr;
1878e126ba97SEli Cohen 	dev->ib_dev.attach_mcast	= mlx5_ib_mcg_attach;
1879e126ba97SEli Cohen 	dev->ib_dev.detach_mcast	= mlx5_ib_mcg_detach;
1880e126ba97SEli Cohen 	dev->ib_dev.process_mad		= mlx5_ib_process_mad;
18819bee178bSSagi Grimberg 	dev->ib_dev.alloc_mr		= mlx5_ib_alloc_mr;
18828a187ee5SSagi Grimberg 	dev->ib_dev.map_mr_sg		= mlx5_ib_map_mr_sg;
1883d5436ba0SSagi Grimberg 	dev->ib_dev.check_mr_status	= mlx5_ib_check_mr_status;
18847738613eSIra Weiny 	dev->ib_dev.get_port_immutable  = mlx5_port_immutable;
1885e126ba97SEli Cohen 
1886938fe83cSSaeed Mahameed 	mlx5_ib_internal_fill_odp_caps(dev);
18878cdd312cSHaggai Eran 
1888938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, xrc)) {
1889e126ba97SEli Cohen 		dev->ib_dev.alloc_xrcd = mlx5_ib_alloc_xrcd;
1890e126ba97SEli Cohen 		dev->ib_dev.dealloc_xrcd = mlx5_ib_dealloc_xrcd;
1891e126ba97SEli Cohen 		dev->ib_dev.uverbs_cmd_mask |=
1892e126ba97SEli Cohen 			(1ull << IB_USER_VERBS_CMD_OPEN_XRCD) |
1893e126ba97SEli Cohen 			(1ull << IB_USER_VERBS_CMD_CLOSE_XRCD);
1894e126ba97SEli Cohen 	}
1895e126ba97SEli Cohen 
1896*038d2ef8SMaor Gottlieb 	if (mlx5_ib_port_link_layer(&dev->ib_dev) ==
1897*038d2ef8SMaor Gottlieb 	    IB_LINK_LAYER_ETHERNET) {
1898*038d2ef8SMaor Gottlieb 		dev->ib_dev.create_flow	= mlx5_ib_create_flow;
1899*038d2ef8SMaor Gottlieb 		dev->ib_dev.destroy_flow = mlx5_ib_destroy_flow;
1900*038d2ef8SMaor Gottlieb 		dev->ib_dev.uverbs_ex_cmd_mask |=
1901*038d2ef8SMaor Gottlieb 			(1ull << IB_USER_VERBS_EX_CMD_CREATE_FLOW) |
1902*038d2ef8SMaor Gottlieb 			(1ull << IB_USER_VERBS_EX_CMD_DESTROY_FLOW);
1903*038d2ef8SMaor Gottlieb 	}
1904e126ba97SEli Cohen 	err = init_node_data(dev);
1905e126ba97SEli Cohen 	if (err)
1906233d05d2SSaeed Mahameed 		goto err_dealloc;
1907e126ba97SEli Cohen 
1908*038d2ef8SMaor Gottlieb 	mutex_init(&dev->flow_db.lock);
1909e126ba97SEli Cohen 	mutex_init(&dev->cap_mask_mutex);
1910e126ba97SEli Cohen 
1911e126ba97SEli Cohen 	err = create_dev_resources(&dev->devr);
1912e126ba97SEli Cohen 	if (err)
1913233d05d2SSaeed Mahameed 		goto err_dealloc;
1914e126ba97SEli Cohen 
19156aec21f6SHaggai Eran 	err = mlx5_ib_odp_init_one(dev);
1916281d1a92SWei Yongjun 	if (err)
1917e126ba97SEli Cohen 		goto err_rsrc;
1918e126ba97SEli Cohen 
19196aec21f6SHaggai Eran 	err = ib_register_device(&dev->ib_dev, NULL);
19206aec21f6SHaggai Eran 	if (err)
19216aec21f6SHaggai Eran 		goto err_odp;
19226aec21f6SHaggai Eran 
1923e126ba97SEli Cohen 	err = create_umr_res(dev);
1924e126ba97SEli Cohen 	if (err)
1925e126ba97SEli Cohen 		goto err_dev;
1926e126ba97SEli Cohen 
1927e126ba97SEli Cohen 	for (i = 0; i < ARRAY_SIZE(mlx5_class_attributes); i++) {
1928281d1a92SWei Yongjun 		err = device_create_file(&dev->ib_dev.dev,
1929281d1a92SWei Yongjun 					 mlx5_class_attributes[i]);
1930281d1a92SWei Yongjun 		if (err)
1931e126ba97SEli Cohen 			goto err_umrc;
1932e126ba97SEli Cohen 	}
1933e126ba97SEli Cohen 
1934e126ba97SEli Cohen 	dev->ib_active = true;
1935e126ba97SEli Cohen 
19369603b61dSJack Morgenstein 	return dev;
1937e126ba97SEli Cohen 
1938e126ba97SEli Cohen err_umrc:
1939e126ba97SEli Cohen 	destroy_umrc_res(dev);
1940e126ba97SEli Cohen 
1941e126ba97SEli Cohen err_dev:
1942e126ba97SEli Cohen 	ib_unregister_device(&dev->ib_dev);
1943e126ba97SEli Cohen 
19446aec21f6SHaggai Eran err_odp:
19456aec21f6SHaggai Eran 	mlx5_ib_odp_remove_one(dev);
19466aec21f6SHaggai Eran 
1947e126ba97SEli Cohen err_rsrc:
1948e126ba97SEli Cohen 	destroy_dev_resources(&dev->devr);
1949e126ba97SEli Cohen 
19509603b61dSJack Morgenstein err_dealloc:
1951e126ba97SEli Cohen 	ib_dealloc_device((struct ib_device *)dev);
1952e126ba97SEli Cohen 
19539603b61dSJack Morgenstein 	return NULL;
1954e126ba97SEli Cohen }
1955e126ba97SEli Cohen 
19569603b61dSJack Morgenstein static void mlx5_ib_remove(struct mlx5_core_dev *mdev, void *context)
1957e126ba97SEli Cohen {
19589603b61dSJack Morgenstein 	struct mlx5_ib_dev *dev = context;
19596aec21f6SHaggai Eran 
1960e126ba97SEli Cohen 	ib_unregister_device(&dev->ib_dev);
1961eefd56e5SEli Cohen 	destroy_umrc_res(dev);
19626aec21f6SHaggai Eran 	mlx5_ib_odp_remove_one(dev);
1963e126ba97SEli Cohen 	destroy_dev_resources(&dev->devr);
1964e126ba97SEli Cohen 	ib_dealloc_device(&dev->ib_dev);
1965e126ba97SEli Cohen }
1966e126ba97SEli Cohen 
19679603b61dSJack Morgenstein static struct mlx5_interface mlx5_ib_interface = {
19689603b61dSJack Morgenstein 	.add            = mlx5_ib_add,
19699603b61dSJack Morgenstein 	.remove         = mlx5_ib_remove,
19709603b61dSJack Morgenstein 	.event          = mlx5_ib_event,
197164613d94SSaeed Mahameed 	.protocol	= MLX5_INTERFACE_PROTOCOL_IB,
1972e126ba97SEli Cohen };
1973e126ba97SEli Cohen 
1974e126ba97SEli Cohen static int __init mlx5_ib_init(void)
1975e126ba97SEli Cohen {
19766aec21f6SHaggai Eran 	int err;
19776aec21f6SHaggai Eran 
19789603b61dSJack Morgenstein 	if (deprecated_prof_sel != 2)
19799603b61dSJack Morgenstein 		pr_warn("prof_sel is deprecated for mlx5_ib, set it for mlx5_core\n");
19809603b61dSJack Morgenstein 
19816aec21f6SHaggai Eran 	err = mlx5_ib_odp_init();
19826aec21f6SHaggai Eran 	if (err)
19836aec21f6SHaggai Eran 		return err;
19846aec21f6SHaggai Eran 
19856aec21f6SHaggai Eran 	err = mlx5_register_interface(&mlx5_ib_interface);
19866aec21f6SHaggai Eran 	if (err)
19876aec21f6SHaggai Eran 		goto clean_odp;
19886aec21f6SHaggai Eran 
19896aec21f6SHaggai Eran 	return err;
19906aec21f6SHaggai Eran 
19916aec21f6SHaggai Eran clean_odp:
19926aec21f6SHaggai Eran 	mlx5_ib_odp_cleanup();
19936aec21f6SHaggai Eran 	return err;
1994e126ba97SEli Cohen }
1995e126ba97SEli Cohen 
1996e126ba97SEli Cohen static void __exit mlx5_ib_cleanup(void)
1997e126ba97SEli Cohen {
19989603b61dSJack Morgenstein 	mlx5_unregister_interface(&mlx5_ib_interface);
19996aec21f6SHaggai Eran 	mlx5_ib_odp_cleanup();
2000e126ba97SEli Cohen }
2001e126ba97SEli Cohen 
2002e126ba97SEli Cohen module_init(mlx5_ib_init);
2003e126ba97SEli Cohen module_exit(mlx5_ib_cleanup);
2004