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 33e126ba97SEli Cohen #include <asm-generic/kmap_types.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> 46e126ba97SEli Cohen #include "user.h" 47e126ba97SEli Cohen #include "mlx5_ib.h" 48e126ba97SEli Cohen 49e126ba97SEli Cohen #define DRIVER_NAME "mlx5_ib" 50169a1d85SAmir Vadai #define DRIVER_VERSION "2.2-1" 51169a1d85SAmir Vadai #define DRIVER_RELDATE "Feb 2014" 52e126ba97SEli Cohen 53e126ba97SEli Cohen MODULE_AUTHOR("Eli Cohen <eli@mellanox.com>"); 54e126ba97SEli Cohen MODULE_DESCRIPTION("Mellanox Connect-IB HCA IB driver"); 55e126ba97SEli Cohen MODULE_LICENSE("Dual BSD/GPL"); 56e126ba97SEli Cohen MODULE_VERSION(DRIVER_VERSION); 57e126ba97SEli Cohen 589603b61dSJack Morgenstein static int deprecated_prof_sel = 2; 599603b61dSJack Morgenstein module_param_named(prof_sel, deprecated_prof_sel, int, 0444); 609603b61dSJack Morgenstein MODULE_PARM_DESC(prof_sel, "profile selector. Deprecated here. Moved to module mlx5_core"); 61e126ba97SEli Cohen 62e126ba97SEli Cohen static char mlx5_version[] = 63e126ba97SEli Cohen DRIVER_NAME ": Mellanox Connect-IB Infiniband driver v" 64e126ba97SEli Cohen DRIVER_VERSION " (" DRIVER_RELDATE ")\n"; 65e126ba97SEli Cohen 661b5daf11SMajd Dibbiny static enum rdma_link_layer 671b5daf11SMajd Dibbiny mlx5_ib_port_link_layer(struct ib_device *device) 681b5daf11SMajd Dibbiny { 691b5daf11SMajd Dibbiny struct mlx5_ib_dev *dev = to_mdev(device); 701b5daf11SMajd Dibbiny 711b5daf11SMajd Dibbiny switch (MLX5_CAP_GEN(dev->mdev, port_type)) { 721b5daf11SMajd Dibbiny case MLX5_CAP_PORT_TYPE_IB: 731b5daf11SMajd Dibbiny return IB_LINK_LAYER_INFINIBAND; 741b5daf11SMajd Dibbiny case MLX5_CAP_PORT_TYPE_ETH: 751b5daf11SMajd Dibbiny return IB_LINK_LAYER_ETHERNET; 761b5daf11SMajd Dibbiny default: 771b5daf11SMajd Dibbiny return IB_LINK_LAYER_UNSPECIFIED; 781b5daf11SMajd Dibbiny } 791b5daf11SMajd Dibbiny } 801b5daf11SMajd Dibbiny 811b5daf11SMajd Dibbiny static int mlx5_use_mad_ifc(struct mlx5_ib_dev *dev) 821b5daf11SMajd Dibbiny { 831b5daf11SMajd Dibbiny return !dev->mdev->issi; 841b5daf11SMajd Dibbiny } 851b5daf11SMajd Dibbiny 861b5daf11SMajd Dibbiny enum { 871b5daf11SMajd Dibbiny MLX5_VPORT_ACCESS_METHOD_MAD, 881b5daf11SMajd Dibbiny MLX5_VPORT_ACCESS_METHOD_HCA, 891b5daf11SMajd Dibbiny MLX5_VPORT_ACCESS_METHOD_NIC, 901b5daf11SMajd Dibbiny }; 911b5daf11SMajd Dibbiny 921b5daf11SMajd Dibbiny static int mlx5_get_vport_access_method(struct ib_device *ibdev) 931b5daf11SMajd Dibbiny { 941b5daf11SMajd Dibbiny if (mlx5_use_mad_ifc(to_mdev(ibdev))) 951b5daf11SMajd Dibbiny return MLX5_VPORT_ACCESS_METHOD_MAD; 961b5daf11SMajd Dibbiny 971b5daf11SMajd Dibbiny if (mlx5_ib_port_link_layer(ibdev) == 981b5daf11SMajd Dibbiny IB_LINK_LAYER_ETHERNET) 991b5daf11SMajd Dibbiny return MLX5_VPORT_ACCESS_METHOD_NIC; 1001b5daf11SMajd Dibbiny 1011b5daf11SMajd Dibbiny return MLX5_VPORT_ACCESS_METHOD_HCA; 1021b5daf11SMajd Dibbiny } 1031b5daf11SMajd Dibbiny 1041b5daf11SMajd Dibbiny static int mlx5_query_system_image_guid(struct ib_device *ibdev, 1051b5daf11SMajd Dibbiny __be64 *sys_image_guid) 1061b5daf11SMajd Dibbiny { 1071b5daf11SMajd Dibbiny struct mlx5_ib_dev *dev = to_mdev(ibdev); 1081b5daf11SMajd Dibbiny struct mlx5_core_dev *mdev = dev->mdev; 1091b5daf11SMajd Dibbiny u64 tmp; 1101b5daf11SMajd Dibbiny int err; 1111b5daf11SMajd Dibbiny 1121b5daf11SMajd Dibbiny switch (mlx5_get_vport_access_method(ibdev)) { 1131b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_MAD: 1141b5daf11SMajd Dibbiny return mlx5_query_mad_ifc_system_image_guid(ibdev, 1151b5daf11SMajd Dibbiny sys_image_guid); 1161b5daf11SMajd Dibbiny 1171b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_HCA: 1181b5daf11SMajd Dibbiny err = mlx5_query_hca_vport_system_image_guid(mdev, &tmp); 1191b5daf11SMajd Dibbiny if (!err) 1201b5daf11SMajd Dibbiny *sys_image_guid = cpu_to_be64(tmp); 1211b5daf11SMajd Dibbiny return err; 1221b5daf11SMajd Dibbiny 1231b5daf11SMajd Dibbiny default: 1241b5daf11SMajd Dibbiny return -EINVAL; 1251b5daf11SMajd Dibbiny } 1261b5daf11SMajd Dibbiny } 1271b5daf11SMajd Dibbiny 1281b5daf11SMajd Dibbiny static int mlx5_query_max_pkeys(struct ib_device *ibdev, 1291b5daf11SMajd Dibbiny u16 *max_pkeys) 1301b5daf11SMajd Dibbiny { 1311b5daf11SMajd Dibbiny struct mlx5_ib_dev *dev = to_mdev(ibdev); 1321b5daf11SMajd Dibbiny struct mlx5_core_dev *mdev = dev->mdev; 1331b5daf11SMajd Dibbiny 1341b5daf11SMajd Dibbiny switch (mlx5_get_vport_access_method(ibdev)) { 1351b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_MAD: 1361b5daf11SMajd Dibbiny return mlx5_query_mad_ifc_max_pkeys(ibdev, max_pkeys); 1371b5daf11SMajd Dibbiny 1381b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_HCA: 1391b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_NIC: 1401b5daf11SMajd Dibbiny *max_pkeys = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, 1411b5daf11SMajd Dibbiny pkey_table_size)); 1421b5daf11SMajd Dibbiny return 0; 1431b5daf11SMajd Dibbiny 1441b5daf11SMajd Dibbiny default: 1451b5daf11SMajd Dibbiny return -EINVAL; 1461b5daf11SMajd Dibbiny } 1471b5daf11SMajd Dibbiny } 1481b5daf11SMajd Dibbiny 1491b5daf11SMajd Dibbiny static int mlx5_query_vendor_id(struct ib_device *ibdev, 1501b5daf11SMajd Dibbiny u32 *vendor_id) 1511b5daf11SMajd Dibbiny { 1521b5daf11SMajd Dibbiny struct mlx5_ib_dev *dev = to_mdev(ibdev); 1531b5daf11SMajd Dibbiny 1541b5daf11SMajd Dibbiny switch (mlx5_get_vport_access_method(ibdev)) { 1551b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_MAD: 1561b5daf11SMajd Dibbiny return mlx5_query_mad_ifc_vendor_id(ibdev, vendor_id); 1571b5daf11SMajd Dibbiny 1581b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_HCA: 1591b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_NIC: 1601b5daf11SMajd Dibbiny return mlx5_core_query_vendor_id(dev->mdev, vendor_id); 1611b5daf11SMajd Dibbiny 1621b5daf11SMajd Dibbiny default: 1631b5daf11SMajd Dibbiny return -EINVAL; 1641b5daf11SMajd Dibbiny } 1651b5daf11SMajd Dibbiny } 1661b5daf11SMajd Dibbiny 1671b5daf11SMajd Dibbiny static int mlx5_query_node_guid(struct mlx5_ib_dev *dev, 1681b5daf11SMajd Dibbiny __be64 *node_guid) 1691b5daf11SMajd Dibbiny { 1701b5daf11SMajd Dibbiny u64 tmp; 1711b5daf11SMajd Dibbiny int err; 1721b5daf11SMajd Dibbiny 1731b5daf11SMajd Dibbiny switch (mlx5_get_vport_access_method(&dev->ib_dev)) { 1741b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_MAD: 1751b5daf11SMajd Dibbiny return mlx5_query_mad_ifc_node_guid(dev, node_guid); 1761b5daf11SMajd Dibbiny 1771b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_HCA: 1781b5daf11SMajd Dibbiny err = mlx5_query_hca_vport_node_guid(dev->mdev, &tmp); 1791b5daf11SMajd Dibbiny if (!err) 1801b5daf11SMajd Dibbiny *node_guid = cpu_to_be64(tmp); 1811b5daf11SMajd Dibbiny return err; 1821b5daf11SMajd Dibbiny 1831b5daf11SMajd Dibbiny default: 1841b5daf11SMajd Dibbiny return -EINVAL; 1851b5daf11SMajd Dibbiny } 1861b5daf11SMajd Dibbiny } 1871b5daf11SMajd Dibbiny 1881b5daf11SMajd Dibbiny struct mlx5_reg_node_desc { 1891b5daf11SMajd Dibbiny u8 desc[64]; 1901b5daf11SMajd Dibbiny }; 1911b5daf11SMajd Dibbiny 1921b5daf11SMajd Dibbiny static int mlx5_query_node_desc(struct mlx5_ib_dev *dev, char *node_desc) 1931b5daf11SMajd Dibbiny { 1941b5daf11SMajd Dibbiny struct mlx5_reg_node_desc in; 1951b5daf11SMajd Dibbiny 1961b5daf11SMajd Dibbiny if (mlx5_use_mad_ifc(dev)) 1971b5daf11SMajd Dibbiny return mlx5_query_mad_ifc_node_desc(dev, node_desc); 1981b5daf11SMajd Dibbiny 1991b5daf11SMajd Dibbiny memset(&in, 0, sizeof(in)); 2001b5daf11SMajd Dibbiny 2011b5daf11SMajd Dibbiny return mlx5_core_access_reg(dev->mdev, &in, sizeof(in), node_desc, 2021b5daf11SMajd Dibbiny sizeof(struct mlx5_reg_node_desc), 2031b5daf11SMajd Dibbiny MLX5_REG_NODE_DESC, 0, 0); 2041b5daf11SMajd Dibbiny } 2051b5daf11SMajd Dibbiny 206e126ba97SEli Cohen static int mlx5_ib_query_device(struct ib_device *ibdev, 207e126ba97SEli Cohen struct ib_device_attr *props) 208e126ba97SEli Cohen { 209e126ba97SEli Cohen struct mlx5_ib_dev *dev = to_mdev(ibdev); 210938fe83cSSaeed Mahameed struct mlx5_core_dev *mdev = dev->mdev; 211e126ba97SEli Cohen int err = -ENOMEM; 212e126ba97SEli Cohen int max_rq_sg; 213e126ba97SEli Cohen int max_sq_sg; 214e126ba97SEli Cohen 215e126ba97SEli Cohen memset(props, 0, sizeof(*props)); 2161b5daf11SMajd Dibbiny err = mlx5_query_system_image_guid(ibdev, 2171b5daf11SMajd Dibbiny &props->sys_image_guid); 2181b5daf11SMajd Dibbiny if (err) 2191b5daf11SMajd Dibbiny return err; 2201b5daf11SMajd Dibbiny 2211b5daf11SMajd Dibbiny err = mlx5_query_max_pkeys(ibdev, &props->max_pkeys); 2221b5daf11SMajd Dibbiny if (err) 2231b5daf11SMajd Dibbiny return err; 2241b5daf11SMajd Dibbiny 2251b5daf11SMajd Dibbiny err = mlx5_query_vendor_id(ibdev, &props->vendor_id); 2261b5daf11SMajd Dibbiny if (err) 2271b5daf11SMajd Dibbiny return err; 228e126ba97SEli Cohen 2299603b61dSJack Morgenstein props->fw_ver = ((u64)fw_rev_maj(dev->mdev) << 32) | 2309603b61dSJack Morgenstein (fw_rev_min(dev->mdev) << 16) | 2319603b61dSJack Morgenstein fw_rev_sub(dev->mdev); 232e126ba97SEli Cohen props->device_cap_flags = IB_DEVICE_CHANGE_PHY_PORT | 233e126ba97SEli Cohen IB_DEVICE_PORT_ACTIVE_EVENT | 234e126ba97SEli Cohen IB_DEVICE_SYS_IMAGE_GUID | 2351a4c3a3dSEli Cohen IB_DEVICE_RC_RNR_NAK_GEN; 236938fe83cSSaeed Mahameed 237938fe83cSSaeed Mahameed if (MLX5_CAP_GEN(mdev, pkv)) 238e126ba97SEli Cohen props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR; 239938fe83cSSaeed Mahameed if (MLX5_CAP_GEN(mdev, qkv)) 240e126ba97SEli Cohen props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR; 241938fe83cSSaeed Mahameed if (MLX5_CAP_GEN(mdev, apm)) 242e126ba97SEli Cohen props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG; 243e126ba97SEli Cohen props->device_cap_flags |= IB_DEVICE_LOCAL_DMA_LKEY; 244938fe83cSSaeed Mahameed if (MLX5_CAP_GEN(mdev, xrc)) 245e126ba97SEli Cohen props->device_cap_flags |= IB_DEVICE_XRC; 246e126ba97SEli Cohen props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS; 247938fe83cSSaeed Mahameed if (MLX5_CAP_GEN(mdev, sho)) { 2482dea9094SSagi Grimberg props->device_cap_flags |= IB_DEVICE_SIGNATURE_HANDOVER; 2492dea9094SSagi Grimberg /* At this stage no support for signature handover */ 2502dea9094SSagi Grimberg props->sig_prot_cap = IB_PROT_T10DIF_TYPE_1 | 2512dea9094SSagi Grimberg IB_PROT_T10DIF_TYPE_2 | 2522dea9094SSagi Grimberg IB_PROT_T10DIF_TYPE_3; 2532dea9094SSagi Grimberg props->sig_guard_cap = IB_GUARD_T10DIF_CRC | 2542dea9094SSagi Grimberg IB_GUARD_T10DIF_CSUM; 2552dea9094SSagi Grimberg } 256938fe83cSSaeed Mahameed if (MLX5_CAP_GEN(mdev, block_lb_mc)) 257f360d88aSEli Cohen props->device_cap_flags |= IB_DEVICE_BLOCK_MULTICAST_LOOPBACK; 258e126ba97SEli Cohen 2591b5daf11SMajd Dibbiny props->vendor_part_id = mdev->pdev->device; 2601b5daf11SMajd Dibbiny props->hw_ver = mdev->pdev->revision; 261e126ba97SEli Cohen 262e126ba97SEli Cohen props->max_mr_size = ~0ull; 263938fe83cSSaeed Mahameed props->page_size_cap = 1ull << MLX5_CAP_GEN(mdev, log_pg_sz); 264938fe83cSSaeed Mahameed props->max_qp = 1 << MLX5_CAP_GEN(mdev, log_max_qp); 265938fe83cSSaeed Mahameed props->max_qp_wr = 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz); 266938fe83cSSaeed Mahameed max_rq_sg = MLX5_CAP_GEN(mdev, max_wqe_sz_rq) / 267938fe83cSSaeed Mahameed sizeof(struct mlx5_wqe_data_seg); 268938fe83cSSaeed Mahameed max_sq_sg = (MLX5_CAP_GEN(mdev, max_wqe_sz_sq) - 269938fe83cSSaeed Mahameed sizeof(struct mlx5_wqe_ctrl_seg)) / 270e126ba97SEli Cohen sizeof(struct mlx5_wqe_data_seg); 271e126ba97SEli Cohen props->max_sge = min(max_rq_sg, max_sq_sg); 272938fe83cSSaeed Mahameed props->max_cq = 1 << MLX5_CAP_GEN(mdev, log_max_cq); 273938fe83cSSaeed Mahameed props->max_cqe = (1 << MLX5_CAP_GEN(mdev, log_max_eq_sz)) - 1; 274938fe83cSSaeed Mahameed props->max_mr = 1 << MLX5_CAP_GEN(mdev, log_max_mkey); 275938fe83cSSaeed Mahameed props->max_pd = 1 << MLX5_CAP_GEN(mdev, log_max_pd); 276938fe83cSSaeed Mahameed props->max_qp_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_req_qp); 277938fe83cSSaeed Mahameed props->max_qp_init_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_res_qp); 278938fe83cSSaeed Mahameed props->max_srq = 1 << MLX5_CAP_GEN(mdev, log_max_srq); 279938fe83cSSaeed Mahameed props->max_srq_wr = (1 << MLX5_CAP_GEN(mdev, log_max_srq_sz)) - 1; 280938fe83cSSaeed Mahameed props->local_ca_ack_delay = MLX5_CAP_GEN(mdev, local_ca_ack_delay); 281e126ba97SEli Cohen props->max_res_rd_atom = props->max_qp_rd_atom * props->max_qp; 282e126ba97SEli Cohen props->max_srq_sge = max_rq_sg - 1; 283e126ba97SEli Cohen props->max_fast_reg_page_list_len = (unsigned int)-1; 28481bea28fSEli Cohen props->atomic_cap = IB_ATOMIC_NONE; 28581bea28fSEli Cohen props->masked_atomic_cap = IB_ATOMIC_NONE; 286938fe83cSSaeed Mahameed props->max_mcast_grp = 1 << MLX5_CAP_GEN(mdev, log_max_mcg); 287938fe83cSSaeed Mahameed props->max_mcast_qp_attach = MLX5_CAP_GEN(mdev, max_qp_mcg); 288e126ba97SEli Cohen props->max_total_mcast_qp_attach = props->max_mcast_qp_attach * 289e126ba97SEli Cohen props->max_mcast_grp; 290e126ba97SEli Cohen props->max_map_per_fmr = INT_MAX; /* no limit in ConnectIB */ 291e126ba97SEli Cohen 2928cdd312cSHaggai Eran #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING 293938fe83cSSaeed Mahameed if (MLX5_CAP_GEN(mdev, pg)) 2948cdd312cSHaggai Eran props->device_cap_flags |= IB_DEVICE_ON_DEMAND_PAGING; 2958cdd312cSHaggai Eran props->odp_caps = dev->odp_caps; 2968cdd312cSHaggai Eran #endif 2978cdd312cSHaggai Eran 2981b5daf11SMajd Dibbiny return 0; 2991b5daf11SMajd Dibbiny } 300e126ba97SEli Cohen 3011b5daf11SMajd Dibbiny enum mlx5_ib_width { 3021b5daf11SMajd Dibbiny MLX5_IB_WIDTH_1X = 1 << 0, 3031b5daf11SMajd Dibbiny MLX5_IB_WIDTH_2X = 1 << 1, 3041b5daf11SMajd Dibbiny MLX5_IB_WIDTH_4X = 1 << 2, 3051b5daf11SMajd Dibbiny MLX5_IB_WIDTH_8X = 1 << 3, 3061b5daf11SMajd Dibbiny MLX5_IB_WIDTH_12X = 1 << 4 3071b5daf11SMajd Dibbiny }; 3081b5daf11SMajd Dibbiny 3091b5daf11SMajd Dibbiny static int translate_active_width(struct ib_device *ibdev, u8 active_width, 3101b5daf11SMajd Dibbiny u8 *ib_width) 3111b5daf11SMajd Dibbiny { 3121b5daf11SMajd Dibbiny struct mlx5_ib_dev *dev = to_mdev(ibdev); 3131b5daf11SMajd Dibbiny int err = 0; 3141b5daf11SMajd Dibbiny 3151b5daf11SMajd Dibbiny if (active_width & MLX5_IB_WIDTH_1X) { 3161b5daf11SMajd Dibbiny *ib_width = IB_WIDTH_1X; 3171b5daf11SMajd Dibbiny } else if (active_width & MLX5_IB_WIDTH_2X) { 3181b5daf11SMajd Dibbiny mlx5_ib_dbg(dev, "active_width %d is not supported by IB spec\n", 3191b5daf11SMajd Dibbiny (int)active_width); 3201b5daf11SMajd Dibbiny err = -EINVAL; 3211b5daf11SMajd Dibbiny } else if (active_width & MLX5_IB_WIDTH_4X) { 3221b5daf11SMajd Dibbiny *ib_width = IB_WIDTH_4X; 3231b5daf11SMajd Dibbiny } else if (active_width & MLX5_IB_WIDTH_8X) { 3241b5daf11SMajd Dibbiny *ib_width = IB_WIDTH_8X; 3251b5daf11SMajd Dibbiny } else if (active_width & MLX5_IB_WIDTH_12X) { 3261b5daf11SMajd Dibbiny *ib_width = IB_WIDTH_12X; 3271b5daf11SMajd Dibbiny } else { 3281b5daf11SMajd Dibbiny mlx5_ib_dbg(dev, "Invalid active_width %d\n", 3291b5daf11SMajd Dibbiny (int)active_width); 3301b5daf11SMajd Dibbiny err = -EINVAL; 3311b5daf11SMajd Dibbiny } 3321b5daf11SMajd Dibbiny 3331b5daf11SMajd Dibbiny return err; 3341b5daf11SMajd Dibbiny } 3351b5daf11SMajd Dibbiny 3361b5daf11SMajd Dibbiny static int mlx5_mtu_to_ib_mtu(int mtu) 3371b5daf11SMajd Dibbiny { 3381b5daf11SMajd Dibbiny switch (mtu) { 3391b5daf11SMajd Dibbiny case 256: return 1; 3401b5daf11SMajd Dibbiny case 512: return 2; 3411b5daf11SMajd Dibbiny case 1024: return 3; 3421b5daf11SMajd Dibbiny case 2048: return 4; 3431b5daf11SMajd Dibbiny case 4096: return 5; 3441b5daf11SMajd Dibbiny default: 3451b5daf11SMajd Dibbiny pr_warn("invalid mtu\n"); 3461b5daf11SMajd Dibbiny return -1; 3471b5daf11SMajd Dibbiny } 3481b5daf11SMajd Dibbiny } 3491b5daf11SMajd Dibbiny 3501b5daf11SMajd Dibbiny enum ib_max_vl_num { 3511b5daf11SMajd Dibbiny __IB_MAX_VL_0 = 1, 3521b5daf11SMajd Dibbiny __IB_MAX_VL_0_1 = 2, 3531b5daf11SMajd Dibbiny __IB_MAX_VL_0_3 = 3, 3541b5daf11SMajd Dibbiny __IB_MAX_VL_0_7 = 4, 3551b5daf11SMajd Dibbiny __IB_MAX_VL_0_14 = 5, 3561b5daf11SMajd Dibbiny }; 3571b5daf11SMajd Dibbiny 3581b5daf11SMajd Dibbiny enum mlx5_vl_hw_cap { 3591b5daf11SMajd Dibbiny MLX5_VL_HW_0 = 1, 3601b5daf11SMajd Dibbiny MLX5_VL_HW_0_1 = 2, 3611b5daf11SMajd Dibbiny MLX5_VL_HW_0_2 = 3, 3621b5daf11SMajd Dibbiny MLX5_VL_HW_0_3 = 4, 3631b5daf11SMajd Dibbiny MLX5_VL_HW_0_4 = 5, 3641b5daf11SMajd Dibbiny MLX5_VL_HW_0_5 = 6, 3651b5daf11SMajd Dibbiny MLX5_VL_HW_0_6 = 7, 3661b5daf11SMajd Dibbiny MLX5_VL_HW_0_7 = 8, 3671b5daf11SMajd Dibbiny MLX5_VL_HW_0_14 = 15 3681b5daf11SMajd Dibbiny }; 3691b5daf11SMajd Dibbiny 3701b5daf11SMajd Dibbiny static int translate_max_vl_num(struct ib_device *ibdev, u8 vl_hw_cap, 3711b5daf11SMajd Dibbiny u8 *max_vl_num) 3721b5daf11SMajd Dibbiny { 3731b5daf11SMajd Dibbiny switch (vl_hw_cap) { 3741b5daf11SMajd Dibbiny case MLX5_VL_HW_0: 3751b5daf11SMajd Dibbiny *max_vl_num = __IB_MAX_VL_0; 3761b5daf11SMajd Dibbiny break; 3771b5daf11SMajd Dibbiny case MLX5_VL_HW_0_1: 3781b5daf11SMajd Dibbiny *max_vl_num = __IB_MAX_VL_0_1; 3791b5daf11SMajd Dibbiny break; 3801b5daf11SMajd Dibbiny case MLX5_VL_HW_0_3: 3811b5daf11SMajd Dibbiny *max_vl_num = __IB_MAX_VL_0_3; 3821b5daf11SMajd Dibbiny break; 3831b5daf11SMajd Dibbiny case MLX5_VL_HW_0_7: 3841b5daf11SMajd Dibbiny *max_vl_num = __IB_MAX_VL_0_7; 3851b5daf11SMajd Dibbiny break; 3861b5daf11SMajd Dibbiny case MLX5_VL_HW_0_14: 3871b5daf11SMajd Dibbiny *max_vl_num = __IB_MAX_VL_0_14; 3881b5daf11SMajd Dibbiny break; 3891b5daf11SMajd Dibbiny 3901b5daf11SMajd Dibbiny default: 3911b5daf11SMajd Dibbiny return -EINVAL; 3921b5daf11SMajd Dibbiny } 3931b5daf11SMajd Dibbiny 3941b5daf11SMajd Dibbiny return 0; 3951b5daf11SMajd Dibbiny } 3961b5daf11SMajd Dibbiny 3971b5daf11SMajd Dibbiny static int mlx5_query_hca_port(struct ib_device *ibdev, u8 port, 3981b5daf11SMajd Dibbiny struct ib_port_attr *props) 3991b5daf11SMajd Dibbiny { 4001b5daf11SMajd Dibbiny struct mlx5_ib_dev *dev = to_mdev(ibdev); 4011b5daf11SMajd Dibbiny struct mlx5_core_dev *mdev = dev->mdev; 4021b5daf11SMajd Dibbiny struct mlx5_hca_vport_context *rep; 4031b5daf11SMajd Dibbiny int max_mtu; 4041b5daf11SMajd Dibbiny int oper_mtu; 4051b5daf11SMajd Dibbiny int err; 4061b5daf11SMajd Dibbiny u8 ib_link_width_oper; 4071b5daf11SMajd Dibbiny u8 vl_hw_cap; 4081b5daf11SMajd Dibbiny 4091b5daf11SMajd Dibbiny rep = kzalloc(sizeof(*rep), GFP_KERNEL); 4101b5daf11SMajd Dibbiny if (!rep) { 4111b5daf11SMajd Dibbiny err = -ENOMEM; 4121b5daf11SMajd Dibbiny goto out; 4131b5daf11SMajd Dibbiny } 4141b5daf11SMajd Dibbiny 4151b5daf11SMajd Dibbiny memset(props, 0, sizeof(*props)); 4161b5daf11SMajd Dibbiny 4171b5daf11SMajd Dibbiny err = mlx5_query_hca_vport_context(mdev, 0, port, 0, rep); 4181b5daf11SMajd Dibbiny if (err) 4191b5daf11SMajd Dibbiny goto out; 4201b5daf11SMajd Dibbiny 4211b5daf11SMajd Dibbiny props->lid = rep->lid; 4221b5daf11SMajd Dibbiny props->lmc = rep->lmc; 4231b5daf11SMajd Dibbiny props->sm_lid = rep->sm_lid; 4241b5daf11SMajd Dibbiny props->sm_sl = rep->sm_sl; 4251b5daf11SMajd Dibbiny props->state = rep->vport_state; 4261b5daf11SMajd Dibbiny props->phys_state = rep->port_physical_state; 4271b5daf11SMajd Dibbiny props->port_cap_flags = rep->cap_mask1; 4281b5daf11SMajd Dibbiny props->gid_tbl_len = mlx5_get_gid_table_len(MLX5_CAP_GEN(mdev, gid_table_size)); 4291b5daf11SMajd Dibbiny props->max_msg_sz = 1 << MLX5_CAP_GEN(mdev, log_max_msg); 4301b5daf11SMajd Dibbiny props->pkey_tbl_len = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, pkey_table_size)); 4311b5daf11SMajd Dibbiny props->bad_pkey_cntr = rep->pkey_violation_counter; 4321b5daf11SMajd Dibbiny props->qkey_viol_cntr = rep->qkey_violation_counter; 4331b5daf11SMajd Dibbiny props->subnet_timeout = rep->subnet_timeout; 4341b5daf11SMajd Dibbiny props->init_type_reply = rep->init_type_reply; 4351b5daf11SMajd Dibbiny 4361b5daf11SMajd Dibbiny err = mlx5_query_port_link_width_oper(mdev, &ib_link_width_oper, port); 4371b5daf11SMajd Dibbiny if (err) 4381b5daf11SMajd Dibbiny goto out; 4391b5daf11SMajd Dibbiny 4401b5daf11SMajd Dibbiny err = translate_active_width(ibdev, ib_link_width_oper, 4411b5daf11SMajd Dibbiny &props->active_width); 4421b5daf11SMajd Dibbiny if (err) 4431b5daf11SMajd Dibbiny goto out; 4441b5daf11SMajd Dibbiny err = mlx5_query_port_proto_oper(mdev, &props->active_speed, MLX5_PTYS_IB, 4451b5daf11SMajd Dibbiny port); 4461b5daf11SMajd Dibbiny if (err) 4471b5daf11SMajd Dibbiny goto out; 4481b5daf11SMajd Dibbiny 449*facc9699SSaeed Mahameed mlx5_query_port_max_mtu(mdev, &max_mtu, port); 4501b5daf11SMajd Dibbiny 4511b5daf11SMajd Dibbiny props->max_mtu = mlx5_mtu_to_ib_mtu(max_mtu); 4521b5daf11SMajd Dibbiny 453*facc9699SSaeed Mahameed mlx5_query_port_oper_mtu(mdev, &oper_mtu, port); 4541b5daf11SMajd Dibbiny 4551b5daf11SMajd Dibbiny props->active_mtu = mlx5_mtu_to_ib_mtu(oper_mtu); 4561b5daf11SMajd Dibbiny 4571b5daf11SMajd Dibbiny err = mlx5_query_port_vl_hw_cap(mdev, &vl_hw_cap, port); 4581b5daf11SMajd Dibbiny if (err) 4591b5daf11SMajd Dibbiny goto out; 4601b5daf11SMajd Dibbiny 4611b5daf11SMajd Dibbiny err = translate_max_vl_num(ibdev, vl_hw_cap, 4621b5daf11SMajd Dibbiny &props->max_vl_num); 4631b5daf11SMajd Dibbiny out: 4641b5daf11SMajd Dibbiny kfree(rep); 465e126ba97SEli Cohen return err; 466e126ba97SEli Cohen } 467e126ba97SEli Cohen 468e126ba97SEli Cohen int mlx5_ib_query_port(struct ib_device *ibdev, u8 port, 469e126ba97SEli Cohen struct ib_port_attr *props) 470e126ba97SEli Cohen { 4711b5daf11SMajd Dibbiny switch (mlx5_get_vport_access_method(ibdev)) { 4721b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_MAD: 4731b5daf11SMajd Dibbiny return mlx5_query_mad_ifc_port(ibdev, port, props); 474e126ba97SEli Cohen 4751b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_HCA: 4761b5daf11SMajd Dibbiny return mlx5_query_hca_port(ibdev, port, props); 4771b5daf11SMajd Dibbiny 4781b5daf11SMajd Dibbiny default: 479e126ba97SEli Cohen return -EINVAL; 480e126ba97SEli Cohen } 481e126ba97SEli Cohen } 482e126ba97SEli Cohen 483e126ba97SEli Cohen static int mlx5_ib_query_gid(struct ib_device *ibdev, u8 port, int index, 484e126ba97SEli Cohen union ib_gid *gid) 485e126ba97SEli Cohen { 4861b5daf11SMajd Dibbiny struct mlx5_ib_dev *dev = to_mdev(ibdev); 4871b5daf11SMajd Dibbiny struct mlx5_core_dev *mdev = dev->mdev; 488e126ba97SEli Cohen 4891b5daf11SMajd Dibbiny switch (mlx5_get_vport_access_method(ibdev)) { 4901b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_MAD: 4911b5daf11SMajd Dibbiny return mlx5_query_mad_ifc_gids(ibdev, port, index, gid); 492e126ba97SEli Cohen 4931b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_HCA: 4941b5daf11SMajd Dibbiny return mlx5_query_hca_vport_gid(mdev, 0, port, 0, index, gid); 495e126ba97SEli Cohen 4961b5daf11SMajd Dibbiny default: 4971b5daf11SMajd Dibbiny return -EINVAL; 4981b5daf11SMajd Dibbiny } 499e126ba97SEli Cohen 500e126ba97SEli Cohen } 501e126ba97SEli Cohen 502e126ba97SEli Cohen static int mlx5_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index, 503e126ba97SEli Cohen u16 *pkey) 504e126ba97SEli Cohen { 5051b5daf11SMajd Dibbiny struct mlx5_ib_dev *dev = to_mdev(ibdev); 5061b5daf11SMajd Dibbiny struct mlx5_core_dev *mdev = dev->mdev; 507e126ba97SEli Cohen 5081b5daf11SMajd Dibbiny switch (mlx5_get_vport_access_method(ibdev)) { 5091b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_MAD: 5101b5daf11SMajd Dibbiny return mlx5_query_mad_ifc_pkey(ibdev, port, index, pkey); 511e126ba97SEli Cohen 5121b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_HCA: 5131b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_NIC: 5141b5daf11SMajd Dibbiny return mlx5_query_hca_vport_pkey(mdev, 0, port, 0, index, 5151b5daf11SMajd Dibbiny pkey); 5161b5daf11SMajd Dibbiny default: 5171b5daf11SMajd Dibbiny return -EINVAL; 518e126ba97SEli Cohen } 5191b5daf11SMajd Dibbiny } 520e126ba97SEli Cohen 521e126ba97SEli Cohen static int mlx5_ib_modify_device(struct ib_device *ibdev, int mask, 522e126ba97SEli Cohen struct ib_device_modify *props) 523e126ba97SEli Cohen { 524e126ba97SEli Cohen struct mlx5_ib_dev *dev = to_mdev(ibdev); 525e126ba97SEli Cohen struct mlx5_reg_node_desc in; 526e126ba97SEli Cohen struct mlx5_reg_node_desc out; 527e126ba97SEli Cohen int err; 528e126ba97SEli Cohen 529e126ba97SEli Cohen if (mask & ~IB_DEVICE_MODIFY_NODE_DESC) 530e126ba97SEli Cohen return -EOPNOTSUPP; 531e126ba97SEli Cohen 532e126ba97SEli Cohen if (!(mask & IB_DEVICE_MODIFY_NODE_DESC)) 533e126ba97SEli Cohen return 0; 534e126ba97SEli Cohen 535e126ba97SEli Cohen /* 536e126ba97SEli Cohen * If possible, pass node desc to FW, so it can generate 537e126ba97SEli Cohen * a 144 trap. If cmd fails, just ignore. 538e126ba97SEli Cohen */ 539e126ba97SEli Cohen memcpy(&in, props->node_desc, 64); 5409603b61dSJack Morgenstein err = mlx5_core_access_reg(dev->mdev, &in, sizeof(in), &out, 541e126ba97SEli Cohen sizeof(out), MLX5_REG_NODE_DESC, 0, 1); 542e126ba97SEli Cohen if (err) 543e126ba97SEli Cohen return err; 544e126ba97SEli Cohen 545e126ba97SEli Cohen memcpy(ibdev->node_desc, props->node_desc, 64); 546e126ba97SEli Cohen 547e126ba97SEli Cohen return err; 548e126ba97SEli Cohen } 549e126ba97SEli Cohen 550e126ba97SEli Cohen static int mlx5_ib_modify_port(struct ib_device *ibdev, u8 port, int mask, 551e126ba97SEli Cohen struct ib_port_modify *props) 552e126ba97SEli Cohen { 553e126ba97SEli Cohen struct mlx5_ib_dev *dev = to_mdev(ibdev); 554e126ba97SEli Cohen struct ib_port_attr attr; 555e126ba97SEli Cohen u32 tmp; 556e126ba97SEli Cohen int err; 557e126ba97SEli Cohen 558e126ba97SEli Cohen mutex_lock(&dev->cap_mask_mutex); 559e126ba97SEli Cohen 560e126ba97SEli Cohen err = mlx5_ib_query_port(ibdev, port, &attr); 561e126ba97SEli Cohen if (err) 562e126ba97SEli Cohen goto out; 563e126ba97SEli Cohen 564e126ba97SEli Cohen tmp = (attr.port_cap_flags | props->set_port_cap_mask) & 565e126ba97SEli Cohen ~props->clr_port_cap_mask; 566e126ba97SEli Cohen 5679603b61dSJack Morgenstein err = mlx5_set_port_caps(dev->mdev, port, tmp); 568e126ba97SEli Cohen 569e126ba97SEli Cohen out: 570e126ba97SEli Cohen mutex_unlock(&dev->cap_mask_mutex); 571e126ba97SEli Cohen return err; 572e126ba97SEli Cohen } 573e126ba97SEli Cohen 574e126ba97SEli Cohen static struct ib_ucontext *mlx5_ib_alloc_ucontext(struct ib_device *ibdev, 575e126ba97SEli Cohen struct ib_udata *udata) 576e126ba97SEli Cohen { 577e126ba97SEli Cohen struct mlx5_ib_dev *dev = to_mdev(ibdev); 57878c0f98cSEli Cohen struct mlx5_ib_alloc_ucontext_req_v2 req; 579e126ba97SEli Cohen struct mlx5_ib_alloc_ucontext_resp resp; 580e126ba97SEli Cohen struct mlx5_ib_ucontext *context; 581e126ba97SEli Cohen struct mlx5_uuar_info *uuari; 582e126ba97SEli Cohen struct mlx5_uar *uars; 583c1be5232SEli Cohen int gross_uuars; 584e126ba97SEli Cohen int num_uars; 58578c0f98cSEli Cohen int ver; 586e126ba97SEli Cohen int uuarn; 587e126ba97SEli Cohen int err; 588e126ba97SEli Cohen int i; 589f241e749SJack Morgenstein size_t reqlen; 590e126ba97SEli Cohen 591e126ba97SEli Cohen if (!dev->ib_active) 592e126ba97SEli Cohen return ERR_PTR(-EAGAIN); 593e126ba97SEli Cohen 59478c0f98cSEli Cohen memset(&req, 0, sizeof(req)); 59578c0f98cSEli Cohen reqlen = udata->inlen - sizeof(struct ib_uverbs_cmd_hdr); 59678c0f98cSEli Cohen if (reqlen == sizeof(struct mlx5_ib_alloc_ucontext_req)) 59778c0f98cSEli Cohen ver = 0; 59878c0f98cSEli Cohen else if (reqlen == sizeof(struct mlx5_ib_alloc_ucontext_req_v2)) 59978c0f98cSEli Cohen ver = 2; 60078c0f98cSEli Cohen else 60178c0f98cSEli Cohen return ERR_PTR(-EINVAL); 60278c0f98cSEli Cohen 60378c0f98cSEli Cohen err = ib_copy_from_udata(&req, udata, reqlen); 604e126ba97SEli Cohen if (err) 605e126ba97SEli Cohen return ERR_PTR(err); 606e126ba97SEli Cohen 60778c0f98cSEli Cohen if (req.flags || req.reserved) 60878c0f98cSEli Cohen return ERR_PTR(-EINVAL); 60978c0f98cSEli Cohen 610e126ba97SEli Cohen if (req.total_num_uuars > MLX5_MAX_UUARS) 611e126ba97SEli Cohen return ERR_PTR(-ENOMEM); 612e126ba97SEli Cohen 613e126ba97SEli Cohen if (req.total_num_uuars == 0) 614e126ba97SEli Cohen return ERR_PTR(-EINVAL); 615e126ba97SEli Cohen 616c1be5232SEli Cohen req.total_num_uuars = ALIGN(req.total_num_uuars, 617c1be5232SEli Cohen MLX5_NON_FP_BF_REGS_PER_PAGE); 618e126ba97SEli Cohen if (req.num_low_latency_uuars > req.total_num_uuars - 1) 619e126ba97SEli Cohen return ERR_PTR(-EINVAL); 620e126ba97SEli Cohen 621c1be5232SEli Cohen num_uars = req.total_num_uuars / MLX5_NON_FP_BF_REGS_PER_PAGE; 622c1be5232SEli Cohen gross_uuars = num_uars * MLX5_BF_REGS_PER_PAGE; 623938fe83cSSaeed Mahameed resp.qp_tab_size = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp); 624938fe83cSSaeed Mahameed resp.bf_reg_size = 1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size); 625e126ba97SEli Cohen resp.cache_line_size = L1_CACHE_BYTES; 626938fe83cSSaeed Mahameed resp.max_sq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq); 627938fe83cSSaeed Mahameed resp.max_rq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq); 628938fe83cSSaeed Mahameed resp.max_send_wqebb = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz); 629938fe83cSSaeed Mahameed resp.max_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz); 630938fe83cSSaeed Mahameed resp.max_srq_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz); 631e126ba97SEli Cohen 632e126ba97SEli Cohen context = kzalloc(sizeof(*context), GFP_KERNEL); 633e126ba97SEli Cohen if (!context) 634e126ba97SEli Cohen return ERR_PTR(-ENOMEM); 635e126ba97SEli Cohen 636e126ba97SEli Cohen uuari = &context->uuari; 637e126ba97SEli Cohen mutex_init(&uuari->lock); 638e126ba97SEli Cohen uars = kcalloc(num_uars, sizeof(*uars), GFP_KERNEL); 639e126ba97SEli Cohen if (!uars) { 640e126ba97SEli Cohen err = -ENOMEM; 641e126ba97SEli Cohen goto out_ctx; 642e126ba97SEli Cohen } 643e126ba97SEli Cohen 644c1be5232SEli Cohen uuari->bitmap = kcalloc(BITS_TO_LONGS(gross_uuars), 645e126ba97SEli Cohen sizeof(*uuari->bitmap), 646e126ba97SEli Cohen GFP_KERNEL); 647e126ba97SEli Cohen if (!uuari->bitmap) { 648e126ba97SEli Cohen err = -ENOMEM; 649e126ba97SEli Cohen goto out_uar_ctx; 650e126ba97SEli Cohen } 651e126ba97SEli Cohen /* 652e126ba97SEli Cohen * clear all fast path uuars 653e126ba97SEli Cohen */ 654c1be5232SEli Cohen for (i = 0; i < gross_uuars; i++) { 655e126ba97SEli Cohen uuarn = i & 3; 656e126ba97SEli Cohen if (uuarn == 2 || uuarn == 3) 657e126ba97SEli Cohen set_bit(i, uuari->bitmap); 658e126ba97SEli Cohen } 659e126ba97SEli Cohen 660c1be5232SEli Cohen uuari->count = kcalloc(gross_uuars, sizeof(*uuari->count), GFP_KERNEL); 661e126ba97SEli Cohen if (!uuari->count) { 662e126ba97SEli Cohen err = -ENOMEM; 663e126ba97SEli Cohen goto out_bitmap; 664e126ba97SEli Cohen } 665e126ba97SEli Cohen 666e126ba97SEli Cohen for (i = 0; i < num_uars; i++) { 6679603b61dSJack Morgenstein err = mlx5_cmd_alloc_uar(dev->mdev, &uars[i].index); 668e126ba97SEli Cohen if (err) 669e126ba97SEli Cohen goto out_count; 670e126ba97SEli Cohen } 671e126ba97SEli Cohen 672b4cfe447SHaggai Eran #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING 673b4cfe447SHaggai Eran context->ibucontext.invalidate_range = &mlx5_ib_invalidate_range; 674b4cfe447SHaggai Eran #endif 675b4cfe447SHaggai Eran 676e126ba97SEli Cohen INIT_LIST_HEAD(&context->db_page_list); 677e126ba97SEli Cohen mutex_init(&context->db_page_mutex); 678e126ba97SEli Cohen 679e126ba97SEli Cohen resp.tot_uuars = req.total_num_uuars; 680938fe83cSSaeed Mahameed resp.num_ports = MLX5_CAP_GEN(dev->mdev, num_ports); 68192b0ca7cSDan Carpenter err = ib_copy_to_udata(udata, &resp, 68292b0ca7cSDan Carpenter sizeof(resp) - sizeof(resp.reserved)); 683e126ba97SEli Cohen if (err) 684e126ba97SEli Cohen goto out_uars; 685e126ba97SEli Cohen 68678c0f98cSEli Cohen uuari->ver = ver; 687e126ba97SEli Cohen uuari->num_low_latency_uuars = req.num_low_latency_uuars; 688e126ba97SEli Cohen uuari->uars = uars; 689e126ba97SEli Cohen uuari->num_uars = num_uars; 690e126ba97SEli Cohen return &context->ibucontext; 691e126ba97SEli Cohen 692e126ba97SEli Cohen out_uars: 693e126ba97SEli Cohen for (i--; i >= 0; i--) 6949603b61dSJack Morgenstein mlx5_cmd_free_uar(dev->mdev, uars[i].index); 695e126ba97SEli Cohen out_count: 696e126ba97SEli Cohen kfree(uuari->count); 697e126ba97SEli Cohen 698e126ba97SEli Cohen out_bitmap: 699e126ba97SEli Cohen kfree(uuari->bitmap); 700e126ba97SEli Cohen 701e126ba97SEli Cohen out_uar_ctx: 702e126ba97SEli Cohen kfree(uars); 703e126ba97SEli Cohen 704e126ba97SEli Cohen out_ctx: 705e126ba97SEli Cohen kfree(context); 706e126ba97SEli Cohen return ERR_PTR(err); 707e126ba97SEli Cohen } 708e126ba97SEli Cohen 709e126ba97SEli Cohen static int mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext) 710e126ba97SEli Cohen { 711e126ba97SEli Cohen struct mlx5_ib_ucontext *context = to_mucontext(ibcontext); 712e126ba97SEli Cohen struct mlx5_ib_dev *dev = to_mdev(ibcontext->device); 713e126ba97SEli Cohen struct mlx5_uuar_info *uuari = &context->uuari; 714e126ba97SEli Cohen int i; 715e126ba97SEli Cohen 716e126ba97SEli Cohen for (i = 0; i < uuari->num_uars; i++) { 7179603b61dSJack Morgenstein if (mlx5_cmd_free_uar(dev->mdev, uuari->uars[i].index)) 718e126ba97SEli Cohen mlx5_ib_warn(dev, "failed to free UAR 0x%x\n", uuari->uars[i].index); 719e126ba97SEli Cohen } 720e126ba97SEli Cohen 721e126ba97SEli Cohen kfree(uuari->count); 722e126ba97SEli Cohen kfree(uuari->bitmap); 723e126ba97SEli Cohen kfree(uuari->uars); 724e126ba97SEli Cohen kfree(context); 725e126ba97SEli Cohen 726e126ba97SEli Cohen return 0; 727e126ba97SEli Cohen } 728e126ba97SEli Cohen 729e126ba97SEli Cohen static phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev, int index) 730e126ba97SEli Cohen { 7319603b61dSJack Morgenstein return (pci_resource_start(dev->mdev->pdev, 0) >> PAGE_SHIFT) + index; 732e126ba97SEli Cohen } 733e126ba97SEli Cohen 734e126ba97SEli Cohen static int get_command(unsigned long offset) 735e126ba97SEli Cohen { 736e126ba97SEli Cohen return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK; 737e126ba97SEli Cohen } 738e126ba97SEli Cohen 739e126ba97SEli Cohen static int get_arg(unsigned long offset) 740e126ba97SEli Cohen { 741e126ba97SEli Cohen return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1); 742e126ba97SEli Cohen } 743e126ba97SEli Cohen 744e126ba97SEli Cohen static int get_index(unsigned long offset) 745e126ba97SEli Cohen { 746e126ba97SEli Cohen return get_arg(offset); 747e126ba97SEli Cohen } 748e126ba97SEli Cohen 749e126ba97SEli Cohen static int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma) 750e126ba97SEli Cohen { 751e126ba97SEli Cohen struct mlx5_ib_ucontext *context = to_mucontext(ibcontext); 752e126ba97SEli Cohen struct mlx5_ib_dev *dev = to_mdev(ibcontext->device); 753e126ba97SEli Cohen struct mlx5_uuar_info *uuari = &context->uuari; 754e126ba97SEli Cohen unsigned long command; 755e126ba97SEli Cohen unsigned long idx; 756e126ba97SEli Cohen phys_addr_t pfn; 757e126ba97SEli Cohen 758e126ba97SEli Cohen command = get_command(vma->vm_pgoff); 759e126ba97SEli Cohen switch (command) { 760e126ba97SEli Cohen case MLX5_IB_MMAP_REGULAR_PAGE: 761e126ba97SEli Cohen if (vma->vm_end - vma->vm_start != PAGE_SIZE) 762e126ba97SEli Cohen return -EINVAL; 763e126ba97SEli Cohen 764e126ba97SEli Cohen idx = get_index(vma->vm_pgoff); 7651c3ce90dSEli Cohen if (idx >= uuari->num_uars) 7661c3ce90dSEli Cohen return -EINVAL; 7671c3ce90dSEli Cohen 768e126ba97SEli Cohen pfn = uar_index2pfn(dev, uuari->uars[idx].index); 769e126ba97SEli Cohen mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn 0x%llx\n", idx, 770e126ba97SEli Cohen (unsigned long long)pfn); 771e126ba97SEli Cohen 772e126ba97SEli Cohen vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot); 773e126ba97SEli Cohen if (io_remap_pfn_range(vma, vma->vm_start, pfn, 774e126ba97SEli Cohen PAGE_SIZE, vma->vm_page_prot)) 775e126ba97SEli Cohen return -EAGAIN; 776e126ba97SEli Cohen 777e126ba97SEli Cohen mlx5_ib_dbg(dev, "mapped WC at 0x%lx, PA 0x%llx\n", 778e126ba97SEli Cohen vma->vm_start, 779e126ba97SEli Cohen (unsigned long long)pfn << PAGE_SHIFT); 780e126ba97SEli Cohen break; 781e126ba97SEli Cohen 782e126ba97SEli Cohen case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES: 783e126ba97SEli Cohen return -ENOSYS; 784e126ba97SEli Cohen 785e126ba97SEli Cohen default: 786e126ba97SEli Cohen return -EINVAL; 787e126ba97SEli Cohen } 788e126ba97SEli Cohen 789e126ba97SEli Cohen return 0; 790e126ba97SEli Cohen } 791e126ba97SEli Cohen 792e126ba97SEli Cohen static int alloc_pa_mkey(struct mlx5_ib_dev *dev, u32 *key, u32 pdn) 793e126ba97SEli Cohen { 794e126ba97SEli Cohen struct mlx5_create_mkey_mbox_in *in; 795e126ba97SEli Cohen struct mlx5_mkey_seg *seg; 796e126ba97SEli Cohen struct mlx5_core_mr mr; 797e126ba97SEli Cohen int err; 798e126ba97SEli Cohen 799e126ba97SEli Cohen in = kzalloc(sizeof(*in), GFP_KERNEL); 800e126ba97SEli Cohen if (!in) 801e126ba97SEli Cohen return -ENOMEM; 802e126ba97SEli Cohen 803e126ba97SEli Cohen seg = &in->seg; 804e126ba97SEli Cohen seg->flags = MLX5_PERM_LOCAL_READ | MLX5_ACCESS_MODE_PA; 805e126ba97SEli Cohen seg->flags_pd = cpu_to_be32(pdn | MLX5_MKEY_LEN64); 806e126ba97SEli Cohen seg->qpn_mkey7_0 = cpu_to_be32(0xffffff << 8); 807e126ba97SEli Cohen seg->start_addr = 0; 808e126ba97SEli Cohen 8099603b61dSJack Morgenstein err = mlx5_core_create_mkey(dev->mdev, &mr, in, sizeof(*in), 810746b5583SEli Cohen NULL, NULL, NULL); 811e126ba97SEli Cohen if (err) { 812e126ba97SEli Cohen mlx5_ib_warn(dev, "failed to create mkey, %d\n", err); 813e126ba97SEli Cohen goto err_in; 814e126ba97SEli Cohen } 815e126ba97SEli Cohen 816e126ba97SEli Cohen kfree(in); 817e126ba97SEli Cohen *key = mr.key; 818e126ba97SEli Cohen 819e126ba97SEli Cohen return 0; 820e126ba97SEli Cohen 821e126ba97SEli Cohen err_in: 822e126ba97SEli Cohen kfree(in); 823e126ba97SEli Cohen 824e126ba97SEli Cohen return err; 825e126ba97SEli Cohen } 826e126ba97SEli Cohen 827e126ba97SEli Cohen static void free_pa_mkey(struct mlx5_ib_dev *dev, u32 key) 828e126ba97SEli Cohen { 829e126ba97SEli Cohen struct mlx5_core_mr mr; 830e126ba97SEli Cohen int err; 831e126ba97SEli Cohen 832e126ba97SEli Cohen memset(&mr, 0, sizeof(mr)); 833e126ba97SEli Cohen mr.key = key; 8349603b61dSJack Morgenstein err = mlx5_core_destroy_mkey(dev->mdev, &mr); 835e126ba97SEli Cohen if (err) 836e126ba97SEli Cohen mlx5_ib_warn(dev, "failed to destroy mkey 0x%x\n", key); 837e126ba97SEli Cohen } 838e126ba97SEli Cohen 839e126ba97SEli Cohen static struct ib_pd *mlx5_ib_alloc_pd(struct ib_device *ibdev, 840e126ba97SEli Cohen struct ib_ucontext *context, 841e126ba97SEli Cohen struct ib_udata *udata) 842e126ba97SEli Cohen { 843e126ba97SEli Cohen struct mlx5_ib_alloc_pd_resp resp; 844e126ba97SEli Cohen struct mlx5_ib_pd *pd; 845e126ba97SEli Cohen int err; 846e126ba97SEli Cohen 847e126ba97SEli Cohen pd = kmalloc(sizeof(*pd), GFP_KERNEL); 848e126ba97SEli Cohen if (!pd) 849e126ba97SEli Cohen return ERR_PTR(-ENOMEM); 850e126ba97SEli Cohen 8519603b61dSJack Morgenstein err = mlx5_core_alloc_pd(to_mdev(ibdev)->mdev, &pd->pdn); 852e126ba97SEli Cohen if (err) { 853e126ba97SEli Cohen kfree(pd); 854e126ba97SEli Cohen return ERR_PTR(err); 855e126ba97SEli Cohen } 856e126ba97SEli Cohen 857e126ba97SEli Cohen if (context) { 858e126ba97SEli Cohen resp.pdn = pd->pdn; 859e126ba97SEli Cohen if (ib_copy_to_udata(udata, &resp, sizeof(resp))) { 8609603b61dSJack Morgenstein mlx5_core_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn); 861e126ba97SEli Cohen kfree(pd); 862e126ba97SEli Cohen return ERR_PTR(-EFAULT); 863e126ba97SEli Cohen } 864e126ba97SEli Cohen } else { 865e126ba97SEli Cohen err = alloc_pa_mkey(to_mdev(ibdev), &pd->pa_lkey, pd->pdn); 866e126ba97SEli Cohen if (err) { 8679603b61dSJack Morgenstein mlx5_core_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn); 868e126ba97SEli Cohen kfree(pd); 869e126ba97SEli Cohen return ERR_PTR(err); 870e126ba97SEli Cohen } 871e126ba97SEli Cohen } 872e126ba97SEli Cohen 873e126ba97SEli Cohen return &pd->ibpd; 874e126ba97SEli Cohen } 875e126ba97SEli Cohen 876e126ba97SEli Cohen static int mlx5_ib_dealloc_pd(struct ib_pd *pd) 877e126ba97SEli Cohen { 878e126ba97SEli Cohen struct mlx5_ib_dev *mdev = to_mdev(pd->device); 879e126ba97SEli Cohen struct mlx5_ib_pd *mpd = to_mpd(pd); 880e126ba97SEli Cohen 881e126ba97SEli Cohen if (!pd->uobject) 882e126ba97SEli Cohen free_pa_mkey(mdev, mpd->pa_lkey); 883e126ba97SEli Cohen 8849603b61dSJack Morgenstein mlx5_core_dealloc_pd(mdev->mdev, mpd->pdn); 885e126ba97SEli Cohen kfree(mpd); 886e126ba97SEli Cohen 887e126ba97SEli Cohen return 0; 888e126ba97SEli Cohen } 889e126ba97SEli Cohen 890e126ba97SEli Cohen static int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid) 891e126ba97SEli Cohen { 892e126ba97SEli Cohen struct mlx5_ib_dev *dev = to_mdev(ibqp->device); 893e126ba97SEli Cohen int err; 894e126ba97SEli Cohen 8959603b61dSJack Morgenstein err = mlx5_core_attach_mcg(dev->mdev, gid, ibqp->qp_num); 896e126ba97SEli Cohen if (err) 897e126ba97SEli Cohen mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n", 898e126ba97SEli Cohen ibqp->qp_num, gid->raw); 899e126ba97SEli Cohen 900e126ba97SEli Cohen return err; 901e126ba97SEli Cohen } 902e126ba97SEli Cohen 903e126ba97SEli Cohen static int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid) 904e126ba97SEli Cohen { 905e126ba97SEli Cohen struct mlx5_ib_dev *dev = to_mdev(ibqp->device); 906e126ba97SEli Cohen int err; 907e126ba97SEli Cohen 9089603b61dSJack Morgenstein err = mlx5_core_detach_mcg(dev->mdev, gid, ibqp->qp_num); 909e126ba97SEli Cohen if (err) 910e126ba97SEli Cohen mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n", 911e126ba97SEli Cohen ibqp->qp_num, gid->raw); 912e126ba97SEli Cohen 913e126ba97SEli Cohen return err; 914e126ba97SEli Cohen } 915e126ba97SEli Cohen 916e126ba97SEli Cohen static int init_node_data(struct mlx5_ib_dev *dev) 917e126ba97SEli Cohen { 9181b5daf11SMajd Dibbiny int err; 919e126ba97SEli Cohen 9201b5daf11SMajd Dibbiny err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc); 921e126ba97SEli Cohen if (err) 922e126ba97SEli Cohen return err; 9231b5daf11SMajd Dibbiny 9241b5daf11SMajd Dibbiny dev->mdev->rev_id = dev->mdev->pdev->revision; 9251b5daf11SMajd Dibbiny 9261b5daf11SMajd Dibbiny return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid); 927e126ba97SEli Cohen } 928e126ba97SEli Cohen 929e126ba97SEli Cohen static ssize_t show_fw_pages(struct device *device, struct device_attribute *attr, 930e126ba97SEli Cohen char *buf) 931e126ba97SEli Cohen { 932e126ba97SEli Cohen struct mlx5_ib_dev *dev = 933e126ba97SEli Cohen container_of(device, struct mlx5_ib_dev, ib_dev.dev); 934e126ba97SEli Cohen 9359603b61dSJack Morgenstein return sprintf(buf, "%d\n", dev->mdev->priv.fw_pages); 936e126ba97SEli Cohen } 937e126ba97SEli Cohen 938e126ba97SEli Cohen static ssize_t show_reg_pages(struct device *device, 939e126ba97SEli Cohen struct device_attribute *attr, char *buf) 940e126ba97SEli Cohen { 941e126ba97SEli Cohen struct mlx5_ib_dev *dev = 942e126ba97SEli Cohen container_of(device, struct mlx5_ib_dev, ib_dev.dev); 943e126ba97SEli Cohen 9446aec21f6SHaggai Eran return sprintf(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages)); 945e126ba97SEli Cohen } 946e126ba97SEli Cohen 947e126ba97SEli Cohen static ssize_t show_hca(struct device *device, struct device_attribute *attr, 948e126ba97SEli Cohen char *buf) 949e126ba97SEli Cohen { 950e126ba97SEli Cohen struct mlx5_ib_dev *dev = 951e126ba97SEli Cohen container_of(device, struct mlx5_ib_dev, ib_dev.dev); 9529603b61dSJack Morgenstein return sprintf(buf, "MT%d\n", dev->mdev->pdev->device); 953e126ba97SEli Cohen } 954e126ba97SEli Cohen 955e126ba97SEli Cohen static ssize_t show_fw_ver(struct device *device, struct device_attribute *attr, 956e126ba97SEli Cohen char *buf) 957e126ba97SEli Cohen { 958e126ba97SEli Cohen struct mlx5_ib_dev *dev = 959e126ba97SEli Cohen container_of(device, struct mlx5_ib_dev, ib_dev.dev); 9609603b61dSJack Morgenstein return sprintf(buf, "%d.%d.%d\n", fw_rev_maj(dev->mdev), 9619603b61dSJack Morgenstein fw_rev_min(dev->mdev), fw_rev_sub(dev->mdev)); 962e126ba97SEli Cohen } 963e126ba97SEli Cohen 964e126ba97SEli Cohen static ssize_t show_rev(struct device *device, struct device_attribute *attr, 965e126ba97SEli Cohen char *buf) 966e126ba97SEli Cohen { 967e126ba97SEli Cohen struct mlx5_ib_dev *dev = 968e126ba97SEli Cohen container_of(device, struct mlx5_ib_dev, ib_dev.dev); 9699603b61dSJack Morgenstein return sprintf(buf, "%x\n", dev->mdev->rev_id); 970e126ba97SEli Cohen } 971e126ba97SEli Cohen 972e126ba97SEli Cohen static ssize_t show_board(struct device *device, struct device_attribute *attr, 973e126ba97SEli Cohen char *buf) 974e126ba97SEli Cohen { 975e126ba97SEli Cohen struct mlx5_ib_dev *dev = 976e126ba97SEli Cohen container_of(device, struct mlx5_ib_dev, ib_dev.dev); 977e126ba97SEli Cohen return sprintf(buf, "%.*s\n", MLX5_BOARD_ID_LEN, 9789603b61dSJack Morgenstein dev->mdev->board_id); 979e126ba97SEli Cohen } 980e126ba97SEli Cohen 981e126ba97SEli Cohen static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL); 982e126ba97SEli Cohen static DEVICE_ATTR(fw_ver, S_IRUGO, show_fw_ver, NULL); 983e126ba97SEli Cohen static DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL); 984e126ba97SEli Cohen static DEVICE_ATTR(board_id, S_IRUGO, show_board, NULL); 985e126ba97SEli Cohen static DEVICE_ATTR(fw_pages, S_IRUGO, show_fw_pages, NULL); 986e126ba97SEli Cohen static DEVICE_ATTR(reg_pages, S_IRUGO, show_reg_pages, NULL); 987e126ba97SEli Cohen 988e126ba97SEli Cohen static struct device_attribute *mlx5_class_attributes[] = { 989e126ba97SEli Cohen &dev_attr_hw_rev, 990e126ba97SEli Cohen &dev_attr_fw_ver, 991e126ba97SEli Cohen &dev_attr_hca_type, 992e126ba97SEli Cohen &dev_attr_board_id, 993e126ba97SEli Cohen &dev_attr_fw_pages, 994e126ba97SEli Cohen &dev_attr_reg_pages, 995e126ba97SEli Cohen }; 996e126ba97SEli Cohen 9979603b61dSJack Morgenstein static void mlx5_ib_event(struct mlx5_core_dev *dev, void *context, 9984d2f9bbbSJack Morgenstein enum mlx5_dev_event event, unsigned long param) 999e126ba97SEli Cohen { 10009603b61dSJack Morgenstein struct mlx5_ib_dev *ibdev = (struct mlx5_ib_dev *)context; 1001e126ba97SEli Cohen struct ib_event ibev; 10029603b61dSJack Morgenstein 1003e126ba97SEli Cohen u8 port = 0; 1004e126ba97SEli Cohen 1005e126ba97SEli Cohen switch (event) { 1006e126ba97SEli Cohen case MLX5_DEV_EVENT_SYS_ERROR: 1007e126ba97SEli Cohen ibdev->ib_active = false; 1008e126ba97SEli Cohen ibev.event = IB_EVENT_DEVICE_FATAL; 1009e126ba97SEli Cohen break; 1010e126ba97SEli Cohen 1011e126ba97SEli Cohen case MLX5_DEV_EVENT_PORT_UP: 1012e126ba97SEli Cohen ibev.event = IB_EVENT_PORT_ACTIVE; 10134d2f9bbbSJack Morgenstein port = (u8)param; 1014e126ba97SEli Cohen break; 1015e126ba97SEli Cohen 1016e126ba97SEli Cohen case MLX5_DEV_EVENT_PORT_DOWN: 1017e126ba97SEli Cohen ibev.event = IB_EVENT_PORT_ERR; 10184d2f9bbbSJack Morgenstein port = (u8)param; 1019e126ba97SEli Cohen break; 1020e126ba97SEli Cohen 1021e126ba97SEli Cohen case MLX5_DEV_EVENT_PORT_INITIALIZED: 1022e126ba97SEli Cohen /* not used by ULPs */ 1023e126ba97SEli Cohen return; 1024e126ba97SEli Cohen 1025e126ba97SEli Cohen case MLX5_DEV_EVENT_LID_CHANGE: 1026e126ba97SEli Cohen ibev.event = IB_EVENT_LID_CHANGE; 10274d2f9bbbSJack Morgenstein port = (u8)param; 1028e126ba97SEli Cohen break; 1029e126ba97SEli Cohen 1030e126ba97SEli Cohen case MLX5_DEV_EVENT_PKEY_CHANGE: 1031e126ba97SEli Cohen ibev.event = IB_EVENT_PKEY_CHANGE; 10324d2f9bbbSJack Morgenstein port = (u8)param; 1033e126ba97SEli Cohen break; 1034e126ba97SEli Cohen 1035e126ba97SEli Cohen case MLX5_DEV_EVENT_GUID_CHANGE: 1036e126ba97SEli Cohen ibev.event = IB_EVENT_GID_CHANGE; 10374d2f9bbbSJack Morgenstein port = (u8)param; 1038e126ba97SEli Cohen break; 1039e126ba97SEli Cohen 1040e126ba97SEli Cohen case MLX5_DEV_EVENT_CLIENT_REREG: 1041e126ba97SEli Cohen ibev.event = IB_EVENT_CLIENT_REREGISTER; 10424d2f9bbbSJack Morgenstein port = (u8)param; 1043e126ba97SEli Cohen break; 1044e126ba97SEli Cohen } 1045e126ba97SEli Cohen 1046e126ba97SEli Cohen ibev.device = &ibdev->ib_dev; 1047e126ba97SEli Cohen ibev.element.port_num = port; 1048e126ba97SEli Cohen 1049a0c84c32SEli Cohen if (port < 1 || port > ibdev->num_ports) { 1050a0c84c32SEli Cohen mlx5_ib_warn(ibdev, "warning: event on port %d\n", port); 1051a0c84c32SEli Cohen return; 1052a0c84c32SEli Cohen } 1053a0c84c32SEli Cohen 1054e126ba97SEli Cohen if (ibdev->ib_active) 1055e126ba97SEli Cohen ib_dispatch_event(&ibev); 1056e126ba97SEli Cohen } 1057e126ba97SEli Cohen 1058e126ba97SEli Cohen static void get_ext_port_caps(struct mlx5_ib_dev *dev) 1059e126ba97SEli Cohen { 1060e126ba97SEli Cohen int port; 1061e126ba97SEli Cohen 1062938fe83cSSaeed Mahameed for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++) 1063e126ba97SEli Cohen mlx5_query_ext_port_caps(dev, port); 1064e126ba97SEli Cohen } 1065e126ba97SEli Cohen 1066e126ba97SEli Cohen static int get_port_caps(struct mlx5_ib_dev *dev) 1067e126ba97SEli Cohen { 1068e126ba97SEli Cohen struct ib_device_attr *dprops = NULL; 1069e126ba97SEli Cohen struct ib_port_attr *pprops = NULL; 1070f614fc15SDan Carpenter int err = -ENOMEM; 1071e126ba97SEli Cohen int port; 1072e126ba97SEli Cohen 1073e126ba97SEli Cohen pprops = kmalloc(sizeof(*pprops), GFP_KERNEL); 1074e126ba97SEli Cohen if (!pprops) 1075e126ba97SEli Cohen goto out; 1076e126ba97SEli Cohen 1077e126ba97SEli Cohen dprops = kmalloc(sizeof(*dprops), GFP_KERNEL); 1078e126ba97SEli Cohen if (!dprops) 1079e126ba97SEli Cohen goto out; 1080e126ba97SEli Cohen 1081e126ba97SEli Cohen err = mlx5_ib_query_device(&dev->ib_dev, dprops); 1082e126ba97SEli Cohen if (err) { 1083e126ba97SEli Cohen mlx5_ib_warn(dev, "query_device failed %d\n", err); 1084e126ba97SEli Cohen goto out; 1085e126ba97SEli Cohen } 1086e126ba97SEli Cohen 1087938fe83cSSaeed Mahameed for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++) { 1088e126ba97SEli Cohen err = mlx5_ib_query_port(&dev->ib_dev, port, pprops); 1089e126ba97SEli Cohen if (err) { 1090938fe83cSSaeed Mahameed mlx5_ib_warn(dev, "query_port %d failed %d\n", 1091938fe83cSSaeed Mahameed port, err); 1092e126ba97SEli Cohen break; 1093e126ba97SEli Cohen } 1094938fe83cSSaeed Mahameed dev->mdev->port_caps[port - 1].pkey_table_len = 1095938fe83cSSaeed Mahameed dprops->max_pkeys; 1096938fe83cSSaeed Mahameed dev->mdev->port_caps[port - 1].gid_table_len = 1097938fe83cSSaeed Mahameed pprops->gid_tbl_len; 1098e126ba97SEli Cohen mlx5_ib_dbg(dev, "pkey_table_len %d, gid_table_len %d\n", 1099e126ba97SEli Cohen dprops->max_pkeys, pprops->gid_tbl_len); 1100e126ba97SEli Cohen } 1101e126ba97SEli Cohen 1102e126ba97SEli Cohen out: 1103e126ba97SEli Cohen kfree(pprops); 1104e126ba97SEli Cohen kfree(dprops); 1105e126ba97SEli Cohen 1106e126ba97SEli Cohen return err; 1107e126ba97SEli Cohen } 1108e126ba97SEli Cohen 1109e126ba97SEli Cohen static void destroy_umrc_res(struct mlx5_ib_dev *dev) 1110e126ba97SEli Cohen { 1111e126ba97SEli Cohen int err; 1112e126ba97SEli Cohen 1113e126ba97SEli Cohen err = mlx5_mr_cache_cleanup(dev); 1114e126ba97SEli Cohen if (err) 1115e126ba97SEli Cohen mlx5_ib_warn(dev, "mr cache cleanup failed\n"); 1116e126ba97SEli Cohen 1117e126ba97SEli Cohen mlx5_ib_destroy_qp(dev->umrc.qp); 1118e126ba97SEli Cohen ib_destroy_cq(dev->umrc.cq); 1119e126ba97SEli Cohen ib_dereg_mr(dev->umrc.mr); 1120e126ba97SEli Cohen ib_dealloc_pd(dev->umrc.pd); 1121e126ba97SEli Cohen } 1122e126ba97SEli Cohen 1123e126ba97SEli Cohen enum { 1124e126ba97SEli Cohen MAX_UMR_WR = 128, 1125e126ba97SEli Cohen }; 1126e126ba97SEli Cohen 1127e126ba97SEli Cohen static int create_umr_res(struct mlx5_ib_dev *dev) 1128e126ba97SEli Cohen { 1129e126ba97SEli Cohen struct ib_qp_init_attr *init_attr = NULL; 1130e126ba97SEli Cohen struct ib_qp_attr *attr = NULL; 1131e126ba97SEli Cohen struct ib_pd *pd; 1132e126ba97SEli Cohen struct ib_cq *cq; 1133e126ba97SEli Cohen struct ib_qp *qp; 1134e126ba97SEli Cohen struct ib_mr *mr; 1135e126ba97SEli Cohen int ret; 1136e126ba97SEli Cohen 1137e126ba97SEli Cohen attr = kzalloc(sizeof(*attr), GFP_KERNEL); 1138e126ba97SEli Cohen init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL); 1139e126ba97SEli Cohen if (!attr || !init_attr) { 1140e126ba97SEli Cohen ret = -ENOMEM; 1141e126ba97SEli Cohen goto error_0; 1142e126ba97SEli Cohen } 1143e126ba97SEli Cohen 1144e126ba97SEli Cohen pd = ib_alloc_pd(&dev->ib_dev); 1145e126ba97SEli Cohen if (IS_ERR(pd)) { 1146e126ba97SEli Cohen mlx5_ib_dbg(dev, "Couldn't create PD for sync UMR QP\n"); 1147e126ba97SEli Cohen ret = PTR_ERR(pd); 1148e126ba97SEli Cohen goto error_0; 1149e126ba97SEli Cohen } 1150e126ba97SEli Cohen 1151e126ba97SEli Cohen mr = ib_get_dma_mr(pd, IB_ACCESS_LOCAL_WRITE); 1152e126ba97SEli Cohen if (IS_ERR(mr)) { 1153e126ba97SEli Cohen mlx5_ib_dbg(dev, "Couldn't create DMA MR for sync UMR QP\n"); 1154e126ba97SEli Cohen ret = PTR_ERR(mr); 1155e126ba97SEli Cohen goto error_1; 1156e126ba97SEli Cohen } 1157e126ba97SEli Cohen 1158e126ba97SEli Cohen cq = ib_create_cq(&dev->ib_dev, mlx5_umr_cq_handler, NULL, NULL, 128, 1159e126ba97SEli Cohen 0); 1160e126ba97SEli Cohen if (IS_ERR(cq)) { 1161e126ba97SEli Cohen mlx5_ib_dbg(dev, "Couldn't create CQ for sync UMR QP\n"); 1162e126ba97SEli Cohen ret = PTR_ERR(cq); 1163e126ba97SEli Cohen goto error_2; 1164e126ba97SEli Cohen } 1165e126ba97SEli Cohen ib_req_notify_cq(cq, IB_CQ_NEXT_COMP); 1166e126ba97SEli Cohen 1167e126ba97SEli Cohen init_attr->send_cq = cq; 1168e126ba97SEli Cohen init_attr->recv_cq = cq; 1169e126ba97SEli Cohen init_attr->sq_sig_type = IB_SIGNAL_ALL_WR; 1170e126ba97SEli Cohen init_attr->cap.max_send_wr = MAX_UMR_WR; 1171e126ba97SEli Cohen init_attr->cap.max_send_sge = 1; 1172e126ba97SEli Cohen init_attr->qp_type = MLX5_IB_QPT_REG_UMR; 1173e126ba97SEli Cohen init_attr->port_num = 1; 1174e126ba97SEli Cohen qp = mlx5_ib_create_qp(pd, init_attr, NULL); 1175e126ba97SEli Cohen if (IS_ERR(qp)) { 1176e126ba97SEli Cohen mlx5_ib_dbg(dev, "Couldn't create sync UMR QP\n"); 1177e126ba97SEli Cohen ret = PTR_ERR(qp); 1178e126ba97SEli Cohen goto error_3; 1179e126ba97SEli Cohen } 1180e126ba97SEli Cohen qp->device = &dev->ib_dev; 1181e126ba97SEli Cohen qp->real_qp = qp; 1182e126ba97SEli Cohen qp->uobject = NULL; 1183e126ba97SEli Cohen qp->qp_type = MLX5_IB_QPT_REG_UMR; 1184e126ba97SEli Cohen 1185e126ba97SEli Cohen attr->qp_state = IB_QPS_INIT; 1186e126ba97SEli Cohen attr->port_num = 1; 1187e126ba97SEli Cohen ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_PKEY_INDEX | 1188e126ba97SEli Cohen IB_QP_PORT, NULL); 1189e126ba97SEli Cohen if (ret) { 1190e126ba97SEli Cohen mlx5_ib_dbg(dev, "Couldn't modify UMR QP\n"); 1191e126ba97SEli Cohen goto error_4; 1192e126ba97SEli Cohen } 1193e126ba97SEli Cohen 1194e126ba97SEli Cohen memset(attr, 0, sizeof(*attr)); 1195e126ba97SEli Cohen attr->qp_state = IB_QPS_RTR; 1196e126ba97SEli Cohen attr->path_mtu = IB_MTU_256; 1197e126ba97SEli Cohen 1198e126ba97SEli Cohen ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL); 1199e126ba97SEli Cohen if (ret) { 1200e126ba97SEli Cohen mlx5_ib_dbg(dev, "Couldn't modify umr QP to rtr\n"); 1201e126ba97SEli Cohen goto error_4; 1202e126ba97SEli Cohen } 1203e126ba97SEli Cohen 1204e126ba97SEli Cohen memset(attr, 0, sizeof(*attr)); 1205e126ba97SEli Cohen attr->qp_state = IB_QPS_RTS; 1206e126ba97SEli Cohen ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL); 1207e126ba97SEli Cohen if (ret) { 1208e126ba97SEli Cohen mlx5_ib_dbg(dev, "Couldn't modify umr QP to rts\n"); 1209e126ba97SEli Cohen goto error_4; 1210e126ba97SEli Cohen } 1211e126ba97SEli Cohen 1212e126ba97SEli Cohen dev->umrc.qp = qp; 1213e126ba97SEli Cohen dev->umrc.cq = cq; 1214e126ba97SEli Cohen dev->umrc.mr = mr; 1215e126ba97SEli Cohen dev->umrc.pd = pd; 1216e126ba97SEli Cohen 1217e126ba97SEli Cohen sema_init(&dev->umrc.sem, MAX_UMR_WR); 1218e126ba97SEli Cohen ret = mlx5_mr_cache_init(dev); 1219e126ba97SEli Cohen if (ret) { 1220e126ba97SEli Cohen mlx5_ib_warn(dev, "mr cache init failed %d\n", ret); 1221e126ba97SEli Cohen goto error_4; 1222e126ba97SEli Cohen } 1223e126ba97SEli Cohen 1224e126ba97SEli Cohen kfree(attr); 1225e126ba97SEli Cohen kfree(init_attr); 1226e126ba97SEli Cohen 1227e126ba97SEli Cohen return 0; 1228e126ba97SEli Cohen 1229e126ba97SEli Cohen error_4: 1230e126ba97SEli Cohen mlx5_ib_destroy_qp(qp); 1231e126ba97SEli Cohen 1232e126ba97SEli Cohen error_3: 1233e126ba97SEli Cohen ib_destroy_cq(cq); 1234e126ba97SEli Cohen 1235e126ba97SEli Cohen error_2: 1236e126ba97SEli Cohen ib_dereg_mr(mr); 1237e126ba97SEli Cohen 1238e126ba97SEli Cohen error_1: 1239e126ba97SEli Cohen ib_dealloc_pd(pd); 1240e126ba97SEli Cohen 1241e126ba97SEli Cohen error_0: 1242e126ba97SEli Cohen kfree(attr); 1243e126ba97SEli Cohen kfree(init_attr); 1244e126ba97SEli Cohen return ret; 1245e126ba97SEli Cohen } 1246e126ba97SEli Cohen 1247e126ba97SEli Cohen static int create_dev_resources(struct mlx5_ib_resources *devr) 1248e126ba97SEli Cohen { 1249e126ba97SEli Cohen struct ib_srq_init_attr attr; 1250e126ba97SEli Cohen struct mlx5_ib_dev *dev; 1251e126ba97SEli Cohen int ret = 0; 1252e126ba97SEli Cohen 1253e126ba97SEli Cohen dev = container_of(devr, struct mlx5_ib_dev, devr); 1254e126ba97SEli Cohen 1255e126ba97SEli Cohen devr->p0 = mlx5_ib_alloc_pd(&dev->ib_dev, NULL, NULL); 1256e126ba97SEli Cohen if (IS_ERR(devr->p0)) { 1257e126ba97SEli Cohen ret = PTR_ERR(devr->p0); 1258e126ba97SEli Cohen goto error0; 1259e126ba97SEli Cohen } 1260e126ba97SEli Cohen devr->p0->device = &dev->ib_dev; 1261e126ba97SEli Cohen devr->p0->uobject = NULL; 1262e126ba97SEli Cohen atomic_set(&devr->p0->usecnt, 0); 1263e126ba97SEli Cohen 1264e126ba97SEli Cohen devr->c0 = mlx5_ib_create_cq(&dev->ib_dev, 1, 0, NULL, NULL); 1265e126ba97SEli Cohen if (IS_ERR(devr->c0)) { 1266e126ba97SEli Cohen ret = PTR_ERR(devr->c0); 1267e126ba97SEli Cohen goto error1; 1268e126ba97SEli Cohen } 1269e126ba97SEli Cohen devr->c0->device = &dev->ib_dev; 1270e126ba97SEli Cohen devr->c0->uobject = NULL; 1271e126ba97SEli Cohen devr->c0->comp_handler = NULL; 1272e126ba97SEli Cohen devr->c0->event_handler = NULL; 1273e126ba97SEli Cohen devr->c0->cq_context = NULL; 1274e126ba97SEli Cohen atomic_set(&devr->c0->usecnt, 0); 1275e126ba97SEli Cohen 1276e126ba97SEli Cohen devr->x0 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL); 1277e126ba97SEli Cohen if (IS_ERR(devr->x0)) { 1278e126ba97SEli Cohen ret = PTR_ERR(devr->x0); 1279e126ba97SEli Cohen goto error2; 1280e126ba97SEli Cohen } 1281e126ba97SEli Cohen devr->x0->device = &dev->ib_dev; 1282e126ba97SEli Cohen devr->x0->inode = NULL; 1283e126ba97SEli Cohen atomic_set(&devr->x0->usecnt, 0); 1284e126ba97SEli Cohen mutex_init(&devr->x0->tgt_qp_mutex); 1285e126ba97SEli Cohen INIT_LIST_HEAD(&devr->x0->tgt_qp_list); 1286e126ba97SEli Cohen 1287e126ba97SEli Cohen devr->x1 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL); 1288e126ba97SEli Cohen if (IS_ERR(devr->x1)) { 1289e126ba97SEli Cohen ret = PTR_ERR(devr->x1); 1290e126ba97SEli Cohen goto error3; 1291e126ba97SEli Cohen } 1292e126ba97SEli Cohen devr->x1->device = &dev->ib_dev; 1293e126ba97SEli Cohen devr->x1->inode = NULL; 1294e126ba97SEli Cohen atomic_set(&devr->x1->usecnt, 0); 1295e126ba97SEli Cohen mutex_init(&devr->x1->tgt_qp_mutex); 1296e126ba97SEli Cohen INIT_LIST_HEAD(&devr->x1->tgt_qp_list); 1297e126ba97SEli Cohen 1298e126ba97SEli Cohen memset(&attr, 0, sizeof(attr)); 1299e126ba97SEli Cohen attr.attr.max_sge = 1; 1300e126ba97SEli Cohen attr.attr.max_wr = 1; 1301e126ba97SEli Cohen attr.srq_type = IB_SRQT_XRC; 1302e126ba97SEli Cohen attr.ext.xrc.cq = devr->c0; 1303e126ba97SEli Cohen attr.ext.xrc.xrcd = devr->x0; 1304e126ba97SEli Cohen 1305e126ba97SEli Cohen devr->s0 = mlx5_ib_create_srq(devr->p0, &attr, NULL); 1306e126ba97SEli Cohen if (IS_ERR(devr->s0)) { 1307e126ba97SEli Cohen ret = PTR_ERR(devr->s0); 1308e126ba97SEli Cohen goto error4; 1309e126ba97SEli Cohen } 1310e126ba97SEli Cohen devr->s0->device = &dev->ib_dev; 1311e126ba97SEli Cohen devr->s0->pd = devr->p0; 1312e126ba97SEli Cohen devr->s0->uobject = NULL; 1313e126ba97SEli Cohen devr->s0->event_handler = NULL; 1314e126ba97SEli Cohen devr->s0->srq_context = NULL; 1315e126ba97SEli Cohen devr->s0->srq_type = IB_SRQT_XRC; 1316e126ba97SEli Cohen devr->s0->ext.xrc.xrcd = devr->x0; 1317e126ba97SEli Cohen devr->s0->ext.xrc.cq = devr->c0; 1318e126ba97SEli Cohen atomic_inc(&devr->s0->ext.xrc.xrcd->usecnt); 1319e126ba97SEli Cohen atomic_inc(&devr->s0->ext.xrc.cq->usecnt); 1320e126ba97SEli Cohen atomic_inc(&devr->p0->usecnt); 1321e126ba97SEli Cohen atomic_set(&devr->s0->usecnt, 0); 1322e126ba97SEli Cohen 13234aa17b28SHaggai Abramonvsky memset(&attr, 0, sizeof(attr)); 13244aa17b28SHaggai Abramonvsky attr.attr.max_sge = 1; 13254aa17b28SHaggai Abramonvsky attr.attr.max_wr = 1; 13264aa17b28SHaggai Abramonvsky attr.srq_type = IB_SRQT_BASIC; 13274aa17b28SHaggai Abramonvsky devr->s1 = mlx5_ib_create_srq(devr->p0, &attr, NULL); 13284aa17b28SHaggai Abramonvsky if (IS_ERR(devr->s1)) { 13294aa17b28SHaggai Abramonvsky ret = PTR_ERR(devr->s1); 13304aa17b28SHaggai Abramonvsky goto error5; 13314aa17b28SHaggai Abramonvsky } 13324aa17b28SHaggai Abramonvsky devr->s1->device = &dev->ib_dev; 13334aa17b28SHaggai Abramonvsky devr->s1->pd = devr->p0; 13344aa17b28SHaggai Abramonvsky devr->s1->uobject = NULL; 13354aa17b28SHaggai Abramonvsky devr->s1->event_handler = NULL; 13364aa17b28SHaggai Abramonvsky devr->s1->srq_context = NULL; 13374aa17b28SHaggai Abramonvsky devr->s1->srq_type = IB_SRQT_BASIC; 13384aa17b28SHaggai Abramonvsky devr->s1->ext.xrc.cq = devr->c0; 13394aa17b28SHaggai Abramonvsky atomic_inc(&devr->p0->usecnt); 13404aa17b28SHaggai Abramonvsky atomic_set(&devr->s0->usecnt, 0); 13414aa17b28SHaggai Abramonvsky 1342e126ba97SEli Cohen return 0; 1343e126ba97SEli Cohen 13444aa17b28SHaggai Abramonvsky error5: 13454aa17b28SHaggai Abramonvsky mlx5_ib_destroy_srq(devr->s0); 1346e126ba97SEli Cohen error4: 1347e126ba97SEli Cohen mlx5_ib_dealloc_xrcd(devr->x1); 1348e126ba97SEli Cohen error3: 1349e126ba97SEli Cohen mlx5_ib_dealloc_xrcd(devr->x0); 1350e126ba97SEli Cohen error2: 1351e126ba97SEli Cohen mlx5_ib_destroy_cq(devr->c0); 1352e126ba97SEli Cohen error1: 1353e126ba97SEli Cohen mlx5_ib_dealloc_pd(devr->p0); 1354e126ba97SEli Cohen error0: 1355e126ba97SEli Cohen return ret; 1356e126ba97SEli Cohen } 1357e126ba97SEli Cohen 1358e126ba97SEli Cohen static void destroy_dev_resources(struct mlx5_ib_resources *devr) 1359e126ba97SEli Cohen { 13604aa17b28SHaggai Abramonvsky mlx5_ib_destroy_srq(devr->s1); 1361e126ba97SEli Cohen mlx5_ib_destroy_srq(devr->s0); 1362e126ba97SEli Cohen mlx5_ib_dealloc_xrcd(devr->x0); 1363e126ba97SEli Cohen mlx5_ib_dealloc_xrcd(devr->x1); 1364e126ba97SEli Cohen mlx5_ib_destroy_cq(devr->c0); 1365e126ba97SEli Cohen mlx5_ib_dealloc_pd(devr->p0); 1366e126ba97SEli Cohen } 1367e126ba97SEli Cohen 13689603b61dSJack Morgenstein static void *mlx5_ib_add(struct mlx5_core_dev *mdev) 1369e126ba97SEli Cohen { 1370e126ba97SEli Cohen struct mlx5_ib_dev *dev; 1371e126ba97SEli Cohen int err; 1372e126ba97SEli Cohen int i; 1373e126ba97SEli Cohen 1374647241eaSMajd Dibbiny /* don't create IB instance over Eth ports, no RoCE yet! */ 1375647241eaSMajd Dibbiny if (MLX5_CAP_GEN(mdev, port_type) == MLX5_CAP_PORT_TYPE_ETH) 1376647241eaSMajd Dibbiny return NULL; 1377647241eaSMajd Dibbiny 1378e126ba97SEli Cohen printk_once(KERN_INFO "%s", mlx5_version); 1379e126ba97SEli Cohen 1380e126ba97SEli Cohen dev = (struct mlx5_ib_dev *)ib_alloc_device(sizeof(*dev)); 1381e126ba97SEli Cohen if (!dev) 13829603b61dSJack Morgenstein return NULL; 1383e126ba97SEli Cohen 13849603b61dSJack Morgenstein dev->mdev = mdev; 1385e126ba97SEli Cohen 1386e126ba97SEli Cohen err = get_port_caps(dev); 1387e126ba97SEli Cohen if (err) 13889603b61dSJack Morgenstein goto err_dealloc; 1389e126ba97SEli Cohen 13901b5daf11SMajd Dibbiny if (mlx5_use_mad_ifc(dev)) 1391e126ba97SEli Cohen get_ext_port_caps(dev); 1392e126ba97SEli Cohen 1393e126ba97SEli Cohen MLX5_INIT_DOORBELL_LOCK(&dev->uar_lock); 1394e126ba97SEli Cohen 1395e126ba97SEli Cohen strlcpy(dev->ib_dev.name, "mlx5_%d", IB_DEVICE_NAME_MAX); 1396e126ba97SEli Cohen dev->ib_dev.owner = THIS_MODULE; 1397e126ba97SEli Cohen dev->ib_dev.node_type = RDMA_NODE_IB_CA; 1398938fe83cSSaeed Mahameed dev->ib_dev.local_dma_lkey = 0 /* not supported for now */; 1399938fe83cSSaeed Mahameed dev->num_ports = MLX5_CAP_GEN(mdev, num_ports); 1400e126ba97SEli Cohen dev->ib_dev.phys_port_cnt = dev->num_ports; 1401233d05d2SSaeed Mahameed dev->ib_dev.num_comp_vectors = 1402233d05d2SSaeed Mahameed dev->mdev->priv.eq_table.num_comp_vectors; 1403e126ba97SEli Cohen dev->ib_dev.dma_device = &mdev->pdev->dev; 1404e126ba97SEli Cohen 1405e126ba97SEli Cohen dev->ib_dev.uverbs_abi_ver = MLX5_IB_UVERBS_ABI_VERSION; 1406e126ba97SEli Cohen dev->ib_dev.uverbs_cmd_mask = 1407e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) | 1408e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) | 1409e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_QUERY_PORT) | 1410e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_ALLOC_PD) | 1411e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) | 1412e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_REG_MR) | 1413e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_DEREG_MR) | 1414e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) | 1415e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_CREATE_CQ) | 1416e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_RESIZE_CQ) | 1417e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) | 1418e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_CREATE_QP) | 1419e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_MODIFY_QP) | 1420e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_QUERY_QP) | 1421e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_DESTROY_QP) | 1422e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST) | 1423e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_DETACH_MCAST) | 1424e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_CREATE_SRQ) | 1425e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ) | 1426e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_QUERY_SRQ) | 1427e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ) | 1428e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_CREATE_XSRQ) | 1429e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_OPEN_QP); 14301707cb4aSHaggai Eran dev->ib_dev.uverbs_ex_cmd_mask = 14311707cb4aSHaggai Eran (1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE); 1432e126ba97SEli Cohen 1433e126ba97SEli Cohen dev->ib_dev.query_device = mlx5_ib_query_device; 1434e126ba97SEli Cohen dev->ib_dev.query_port = mlx5_ib_query_port; 1435e126ba97SEli Cohen dev->ib_dev.query_gid = mlx5_ib_query_gid; 1436e126ba97SEli Cohen dev->ib_dev.query_pkey = mlx5_ib_query_pkey; 1437e126ba97SEli Cohen dev->ib_dev.modify_device = mlx5_ib_modify_device; 1438e126ba97SEli Cohen dev->ib_dev.modify_port = mlx5_ib_modify_port; 1439e126ba97SEli Cohen dev->ib_dev.alloc_ucontext = mlx5_ib_alloc_ucontext; 1440e126ba97SEli Cohen dev->ib_dev.dealloc_ucontext = mlx5_ib_dealloc_ucontext; 1441e126ba97SEli Cohen dev->ib_dev.mmap = mlx5_ib_mmap; 1442e126ba97SEli Cohen dev->ib_dev.alloc_pd = mlx5_ib_alloc_pd; 1443e126ba97SEli Cohen dev->ib_dev.dealloc_pd = mlx5_ib_dealloc_pd; 1444e126ba97SEli Cohen dev->ib_dev.create_ah = mlx5_ib_create_ah; 1445e126ba97SEli Cohen dev->ib_dev.query_ah = mlx5_ib_query_ah; 1446e126ba97SEli Cohen dev->ib_dev.destroy_ah = mlx5_ib_destroy_ah; 1447e126ba97SEli Cohen dev->ib_dev.create_srq = mlx5_ib_create_srq; 1448e126ba97SEli Cohen dev->ib_dev.modify_srq = mlx5_ib_modify_srq; 1449e126ba97SEli Cohen dev->ib_dev.query_srq = mlx5_ib_query_srq; 1450e126ba97SEli Cohen dev->ib_dev.destroy_srq = mlx5_ib_destroy_srq; 1451e126ba97SEli Cohen dev->ib_dev.post_srq_recv = mlx5_ib_post_srq_recv; 1452e126ba97SEli Cohen dev->ib_dev.create_qp = mlx5_ib_create_qp; 1453e126ba97SEli Cohen dev->ib_dev.modify_qp = mlx5_ib_modify_qp; 1454e126ba97SEli Cohen dev->ib_dev.query_qp = mlx5_ib_query_qp; 1455e126ba97SEli Cohen dev->ib_dev.destroy_qp = mlx5_ib_destroy_qp; 1456e126ba97SEli Cohen dev->ib_dev.post_send = mlx5_ib_post_send; 1457e126ba97SEli Cohen dev->ib_dev.post_recv = mlx5_ib_post_recv; 1458e126ba97SEli Cohen dev->ib_dev.create_cq = mlx5_ib_create_cq; 1459e126ba97SEli Cohen dev->ib_dev.modify_cq = mlx5_ib_modify_cq; 1460e126ba97SEli Cohen dev->ib_dev.resize_cq = mlx5_ib_resize_cq; 1461e126ba97SEli Cohen dev->ib_dev.destroy_cq = mlx5_ib_destroy_cq; 1462e126ba97SEli Cohen dev->ib_dev.poll_cq = mlx5_ib_poll_cq; 1463e126ba97SEli Cohen dev->ib_dev.req_notify_cq = mlx5_ib_arm_cq; 1464e126ba97SEli Cohen dev->ib_dev.get_dma_mr = mlx5_ib_get_dma_mr; 1465e126ba97SEli Cohen dev->ib_dev.reg_user_mr = mlx5_ib_reg_user_mr; 1466e126ba97SEli Cohen dev->ib_dev.dereg_mr = mlx5_ib_dereg_mr; 14673121e3c4SSagi Grimberg dev->ib_dev.destroy_mr = mlx5_ib_destroy_mr; 1468e126ba97SEli Cohen dev->ib_dev.attach_mcast = mlx5_ib_mcg_attach; 1469e126ba97SEli Cohen dev->ib_dev.detach_mcast = mlx5_ib_mcg_detach; 1470e126ba97SEli Cohen dev->ib_dev.process_mad = mlx5_ib_process_mad; 14713121e3c4SSagi Grimberg dev->ib_dev.create_mr = mlx5_ib_create_mr; 1472e126ba97SEli Cohen dev->ib_dev.alloc_fast_reg_mr = mlx5_ib_alloc_fast_reg_mr; 1473e126ba97SEli Cohen dev->ib_dev.alloc_fast_reg_page_list = mlx5_ib_alloc_fast_reg_page_list; 1474e126ba97SEli Cohen dev->ib_dev.free_fast_reg_page_list = mlx5_ib_free_fast_reg_page_list; 1475d5436ba0SSagi Grimberg dev->ib_dev.check_mr_status = mlx5_ib_check_mr_status; 1476e126ba97SEli Cohen 1477938fe83cSSaeed Mahameed mlx5_ib_internal_fill_odp_caps(dev); 14788cdd312cSHaggai Eran 1479938fe83cSSaeed Mahameed if (MLX5_CAP_GEN(mdev, xrc)) { 1480e126ba97SEli Cohen dev->ib_dev.alloc_xrcd = mlx5_ib_alloc_xrcd; 1481e126ba97SEli Cohen dev->ib_dev.dealloc_xrcd = mlx5_ib_dealloc_xrcd; 1482e126ba97SEli Cohen dev->ib_dev.uverbs_cmd_mask |= 1483e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_OPEN_XRCD) | 1484e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_CLOSE_XRCD); 1485e126ba97SEli Cohen } 1486e126ba97SEli Cohen 1487e126ba97SEli Cohen err = init_node_data(dev); 1488e126ba97SEli Cohen if (err) 1489233d05d2SSaeed Mahameed goto err_dealloc; 1490e126ba97SEli Cohen 1491e126ba97SEli Cohen mutex_init(&dev->cap_mask_mutex); 1492e126ba97SEli Cohen 1493e126ba97SEli Cohen err = create_dev_resources(&dev->devr); 1494e126ba97SEli Cohen if (err) 1495233d05d2SSaeed Mahameed goto err_dealloc; 1496e126ba97SEli Cohen 14976aec21f6SHaggai Eran err = mlx5_ib_odp_init_one(dev); 1498281d1a92SWei Yongjun if (err) 1499e126ba97SEli Cohen goto err_rsrc; 1500e126ba97SEli Cohen 15016aec21f6SHaggai Eran err = ib_register_device(&dev->ib_dev, NULL); 15026aec21f6SHaggai Eran if (err) 15036aec21f6SHaggai Eran goto err_odp; 15046aec21f6SHaggai Eran 1505e126ba97SEli Cohen err = create_umr_res(dev); 1506e126ba97SEli Cohen if (err) 1507e126ba97SEli Cohen goto err_dev; 1508e126ba97SEli Cohen 1509e126ba97SEli Cohen for (i = 0; i < ARRAY_SIZE(mlx5_class_attributes); i++) { 1510281d1a92SWei Yongjun err = device_create_file(&dev->ib_dev.dev, 1511281d1a92SWei Yongjun mlx5_class_attributes[i]); 1512281d1a92SWei Yongjun if (err) 1513e126ba97SEli Cohen goto err_umrc; 1514e126ba97SEli Cohen } 1515e126ba97SEli Cohen 1516e126ba97SEli Cohen dev->ib_active = true; 1517e126ba97SEli Cohen 15189603b61dSJack Morgenstein return dev; 1519e126ba97SEli Cohen 1520e126ba97SEli Cohen err_umrc: 1521e126ba97SEli Cohen destroy_umrc_res(dev); 1522e126ba97SEli Cohen 1523e126ba97SEli Cohen err_dev: 1524e126ba97SEli Cohen ib_unregister_device(&dev->ib_dev); 1525e126ba97SEli Cohen 15266aec21f6SHaggai Eran err_odp: 15276aec21f6SHaggai Eran mlx5_ib_odp_remove_one(dev); 15286aec21f6SHaggai Eran 1529e126ba97SEli Cohen err_rsrc: 1530e126ba97SEli Cohen destroy_dev_resources(&dev->devr); 1531e126ba97SEli Cohen 15329603b61dSJack Morgenstein err_dealloc: 1533e126ba97SEli Cohen ib_dealloc_device((struct ib_device *)dev); 1534e126ba97SEli Cohen 15359603b61dSJack Morgenstein return NULL; 1536e126ba97SEli Cohen } 1537e126ba97SEli Cohen 15389603b61dSJack Morgenstein static void mlx5_ib_remove(struct mlx5_core_dev *mdev, void *context) 1539e126ba97SEli Cohen { 15409603b61dSJack Morgenstein struct mlx5_ib_dev *dev = context; 15416aec21f6SHaggai Eran 1542e126ba97SEli Cohen ib_unregister_device(&dev->ib_dev); 1543eefd56e5SEli Cohen destroy_umrc_res(dev); 15446aec21f6SHaggai Eran mlx5_ib_odp_remove_one(dev); 1545e126ba97SEli Cohen destroy_dev_resources(&dev->devr); 1546e126ba97SEli Cohen ib_dealloc_device(&dev->ib_dev); 1547e126ba97SEli Cohen } 1548e126ba97SEli Cohen 15499603b61dSJack Morgenstein static struct mlx5_interface mlx5_ib_interface = { 15509603b61dSJack Morgenstein .add = mlx5_ib_add, 15519603b61dSJack Morgenstein .remove = mlx5_ib_remove, 15529603b61dSJack Morgenstein .event = mlx5_ib_event, 155364613d94SSaeed Mahameed .protocol = MLX5_INTERFACE_PROTOCOL_IB, 1554e126ba97SEli Cohen }; 1555e126ba97SEli Cohen 1556e126ba97SEli Cohen static int __init mlx5_ib_init(void) 1557e126ba97SEli Cohen { 15586aec21f6SHaggai Eran int err; 15596aec21f6SHaggai Eran 15609603b61dSJack Morgenstein if (deprecated_prof_sel != 2) 15619603b61dSJack Morgenstein pr_warn("prof_sel is deprecated for mlx5_ib, set it for mlx5_core\n"); 15629603b61dSJack Morgenstein 15636aec21f6SHaggai Eran err = mlx5_ib_odp_init(); 15646aec21f6SHaggai Eran if (err) 15656aec21f6SHaggai Eran return err; 15666aec21f6SHaggai Eran 15676aec21f6SHaggai Eran err = mlx5_register_interface(&mlx5_ib_interface); 15686aec21f6SHaggai Eran if (err) 15696aec21f6SHaggai Eran goto clean_odp; 15706aec21f6SHaggai Eran 15716aec21f6SHaggai Eran return err; 15726aec21f6SHaggai Eran 15736aec21f6SHaggai Eran clean_odp: 15746aec21f6SHaggai Eran mlx5_ib_odp_cleanup(); 15756aec21f6SHaggai Eran return err; 1576e126ba97SEli Cohen } 1577e126ba97SEli Cohen 1578e126ba97SEli Cohen static void __exit mlx5_ib_cleanup(void) 1579e126ba97SEli Cohen { 15809603b61dSJack Morgenstein mlx5_unregister_interface(&mlx5_ib_interface); 15816aec21f6SHaggai Eran mlx5_ib_odp_cleanup(); 1582e126ba97SEli Cohen } 1583e126ba97SEli Cohen 1584e126ba97SEli Cohen module_init(mlx5_ib_init); 1585e126ba97SEli Cohen module_exit(mlx5_ib_cleanup); 1586