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> 433f89a643SAchiad Shochat #include <rdma/ib_addr.h> 442811ba51SAchiad Shochat #include <rdma/ib_cache.h> 451b5daf11SMajd Dibbiny #include <linux/mlx5/vport.h> 46e126ba97SEli Cohen #include <rdma/ib_smi.h> 47e126ba97SEli Cohen #include <rdma/ib_umem.h> 48038d2ef8SMaor Gottlieb #include <linux/in.h> 49038d2ef8SMaor Gottlieb #include <linux/etherdevice.h> 50038d2ef8SMaor Gottlieb #include <linux/mlx5/fs.h> 51e126ba97SEli Cohen #include "user.h" 52e126ba97SEli Cohen #include "mlx5_ib.h" 53e126ba97SEli Cohen 54e126ba97SEli Cohen #define DRIVER_NAME "mlx5_ib" 55169a1d85SAmir Vadai #define DRIVER_VERSION "2.2-1" 56169a1d85SAmir Vadai #define DRIVER_RELDATE "Feb 2014" 57e126ba97SEli Cohen 58e126ba97SEli Cohen MODULE_AUTHOR("Eli Cohen <eli@mellanox.com>"); 59e126ba97SEli Cohen MODULE_DESCRIPTION("Mellanox Connect-IB HCA IB driver"); 60e126ba97SEli Cohen MODULE_LICENSE("Dual BSD/GPL"); 61e126ba97SEli Cohen MODULE_VERSION(DRIVER_VERSION); 62e126ba97SEli Cohen 639603b61dSJack Morgenstein static int deprecated_prof_sel = 2; 649603b61dSJack Morgenstein module_param_named(prof_sel, deprecated_prof_sel, int, 0444); 659603b61dSJack Morgenstein MODULE_PARM_DESC(prof_sel, "profile selector. Deprecated here. Moved to module mlx5_core"); 66e126ba97SEli Cohen 67e126ba97SEli Cohen static char mlx5_version[] = 68e126ba97SEli Cohen DRIVER_NAME ": Mellanox Connect-IB Infiniband driver v" 69e126ba97SEli Cohen DRIVER_VERSION " (" DRIVER_RELDATE ")\n"; 70e126ba97SEli Cohen 71da7525d2SEran Ben Elisha enum { 72da7525d2SEran Ben Elisha MLX5_ATOMIC_SIZE_QP_8BYTES = 1 << 3, 73da7525d2SEran Ben Elisha }; 741b5daf11SMajd Dibbiny 751b5daf11SMajd Dibbiny static enum rdma_link_layer 76ebd61f68SAchiad Shochat mlx5_port_type_cap_to_rdma_ll(int port_type_cap) 771b5daf11SMajd Dibbiny { 78ebd61f68SAchiad Shochat switch (port_type_cap) { 791b5daf11SMajd Dibbiny case MLX5_CAP_PORT_TYPE_IB: 801b5daf11SMajd Dibbiny return IB_LINK_LAYER_INFINIBAND; 811b5daf11SMajd Dibbiny case MLX5_CAP_PORT_TYPE_ETH: 821b5daf11SMajd Dibbiny return IB_LINK_LAYER_ETHERNET; 831b5daf11SMajd Dibbiny default: 841b5daf11SMajd Dibbiny return IB_LINK_LAYER_UNSPECIFIED; 851b5daf11SMajd Dibbiny } 861b5daf11SMajd Dibbiny } 871b5daf11SMajd Dibbiny 88ebd61f68SAchiad Shochat static enum rdma_link_layer 89ebd61f68SAchiad Shochat mlx5_ib_port_link_layer(struct ib_device *device, u8 port_num) 90ebd61f68SAchiad Shochat { 91ebd61f68SAchiad Shochat struct mlx5_ib_dev *dev = to_mdev(device); 92ebd61f68SAchiad Shochat int port_type_cap = MLX5_CAP_GEN(dev->mdev, port_type); 93ebd61f68SAchiad Shochat 94ebd61f68SAchiad Shochat return mlx5_port_type_cap_to_rdma_ll(port_type_cap); 95ebd61f68SAchiad Shochat } 96ebd61f68SAchiad Shochat 97fc24fc5eSAchiad Shochat static int mlx5_netdev_event(struct notifier_block *this, 98fc24fc5eSAchiad Shochat unsigned long event, void *ptr) 99fc24fc5eSAchiad Shochat { 100fc24fc5eSAchiad Shochat struct net_device *ndev = netdev_notifier_info_to_dev(ptr); 101fc24fc5eSAchiad Shochat struct mlx5_ib_dev *ibdev = container_of(this, struct mlx5_ib_dev, 102fc24fc5eSAchiad Shochat roce.nb); 103fc24fc5eSAchiad Shochat 104fc24fc5eSAchiad Shochat if ((event != NETDEV_UNREGISTER) && (event != NETDEV_REGISTER)) 105fc24fc5eSAchiad Shochat return NOTIFY_DONE; 106fc24fc5eSAchiad Shochat 107fc24fc5eSAchiad Shochat write_lock(&ibdev->roce.netdev_lock); 108fc24fc5eSAchiad Shochat if (ndev->dev.parent == &ibdev->mdev->pdev->dev) 109fc24fc5eSAchiad Shochat ibdev->roce.netdev = (event == NETDEV_UNREGISTER) ? NULL : ndev; 110fc24fc5eSAchiad Shochat write_unlock(&ibdev->roce.netdev_lock); 111fc24fc5eSAchiad Shochat 112fc24fc5eSAchiad Shochat return NOTIFY_DONE; 113fc24fc5eSAchiad Shochat } 114fc24fc5eSAchiad Shochat 115fc24fc5eSAchiad Shochat static struct net_device *mlx5_ib_get_netdev(struct ib_device *device, 116fc24fc5eSAchiad Shochat u8 port_num) 117fc24fc5eSAchiad Shochat { 118fc24fc5eSAchiad Shochat struct mlx5_ib_dev *ibdev = to_mdev(device); 119fc24fc5eSAchiad Shochat struct net_device *ndev; 120fc24fc5eSAchiad Shochat 121fc24fc5eSAchiad Shochat /* Ensure ndev does not disappear before we invoke dev_hold() 122fc24fc5eSAchiad Shochat */ 123fc24fc5eSAchiad Shochat read_lock(&ibdev->roce.netdev_lock); 124fc24fc5eSAchiad Shochat ndev = ibdev->roce.netdev; 125fc24fc5eSAchiad Shochat if (ndev) 126fc24fc5eSAchiad Shochat dev_hold(ndev); 127fc24fc5eSAchiad Shochat read_unlock(&ibdev->roce.netdev_lock); 128fc24fc5eSAchiad Shochat 129fc24fc5eSAchiad Shochat return ndev; 130fc24fc5eSAchiad Shochat } 131fc24fc5eSAchiad Shochat 1323f89a643SAchiad Shochat static int mlx5_query_port_roce(struct ib_device *device, u8 port_num, 1333f89a643SAchiad Shochat struct ib_port_attr *props) 1343f89a643SAchiad Shochat { 1353f89a643SAchiad Shochat struct mlx5_ib_dev *dev = to_mdev(device); 1363f89a643SAchiad Shochat struct net_device *ndev; 1373f89a643SAchiad Shochat enum ib_mtu ndev_ib_mtu; 138c876a1b7SLeon Romanovsky u16 qkey_viol_cntr; 1393f89a643SAchiad Shochat 1403f89a643SAchiad Shochat memset(props, 0, sizeof(*props)); 1413f89a643SAchiad Shochat 1423f89a643SAchiad Shochat props->port_cap_flags |= IB_PORT_CM_SUP; 1433f89a643SAchiad Shochat props->port_cap_flags |= IB_PORT_IP_BASED_GIDS; 1443f89a643SAchiad Shochat 1453f89a643SAchiad Shochat props->gid_tbl_len = MLX5_CAP_ROCE(dev->mdev, 1463f89a643SAchiad Shochat roce_address_table_size); 1473f89a643SAchiad Shochat props->max_mtu = IB_MTU_4096; 1483f89a643SAchiad Shochat props->max_msg_sz = 1 << MLX5_CAP_GEN(dev->mdev, log_max_msg); 1493f89a643SAchiad Shochat props->pkey_tbl_len = 1; 1503f89a643SAchiad Shochat props->state = IB_PORT_DOWN; 1513f89a643SAchiad Shochat props->phys_state = 3; 1523f89a643SAchiad Shochat 153c876a1b7SLeon Romanovsky mlx5_query_nic_vport_qkey_viol_cntr(dev->mdev, &qkey_viol_cntr); 154c876a1b7SLeon Romanovsky props->qkey_viol_cntr = qkey_viol_cntr; 1553f89a643SAchiad Shochat 1563f89a643SAchiad Shochat ndev = mlx5_ib_get_netdev(device, port_num); 1573f89a643SAchiad Shochat if (!ndev) 1583f89a643SAchiad Shochat return 0; 1593f89a643SAchiad Shochat 1603f89a643SAchiad Shochat if (netif_running(ndev) && netif_carrier_ok(ndev)) { 1613f89a643SAchiad Shochat props->state = IB_PORT_ACTIVE; 1623f89a643SAchiad Shochat props->phys_state = 5; 1633f89a643SAchiad Shochat } 1643f89a643SAchiad Shochat 1653f89a643SAchiad Shochat ndev_ib_mtu = iboe_get_mtu(ndev->mtu); 1663f89a643SAchiad Shochat 1673f89a643SAchiad Shochat dev_put(ndev); 1683f89a643SAchiad Shochat 1693f89a643SAchiad Shochat props->active_mtu = min(props->max_mtu, ndev_ib_mtu); 1703f89a643SAchiad Shochat 1713f89a643SAchiad Shochat props->active_width = IB_WIDTH_4X; /* TODO */ 1723f89a643SAchiad Shochat props->active_speed = IB_SPEED_QDR; /* TODO */ 1733f89a643SAchiad Shochat 1743f89a643SAchiad Shochat return 0; 1753f89a643SAchiad Shochat } 1763f89a643SAchiad Shochat 1773cca2606SAchiad Shochat static void ib_gid_to_mlx5_roce_addr(const union ib_gid *gid, 1783cca2606SAchiad Shochat const struct ib_gid_attr *attr, 1793cca2606SAchiad Shochat void *mlx5_addr) 1803cca2606SAchiad Shochat { 1813cca2606SAchiad Shochat #define MLX5_SET_RA(p, f, v) MLX5_SET(roce_addr_layout, p, f, v) 1823cca2606SAchiad Shochat char *mlx5_addr_l3_addr = MLX5_ADDR_OF(roce_addr_layout, mlx5_addr, 1833cca2606SAchiad Shochat source_l3_address); 1843cca2606SAchiad Shochat void *mlx5_addr_mac = MLX5_ADDR_OF(roce_addr_layout, mlx5_addr, 1853cca2606SAchiad Shochat source_mac_47_32); 1863cca2606SAchiad Shochat 1873cca2606SAchiad Shochat if (!gid) 1883cca2606SAchiad Shochat return; 1893cca2606SAchiad Shochat 1903cca2606SAchiad Shochat ether_addr_copy(mlx5_addr_mac, attr->ndev->dev_addr); 1913cca2606SAchiad Shochat 1923cca2606SAchiad Shochat if (is_vlan_dev(attr->ndev)) { 1933cca2606SAchiad Shochat MLX5_SET_RA(mlx5_addr, vlan_valid, 1); 1943cca2606SAchiad Shochat MLX5_SET_RA(mlx5_addr, vlan_id, vlan_dev_vlan_id(attr->ndev)); 1953cca2606SAchiad Shochat } 1963cca2606SAchiad Shochat 1973cca2606SAchiad Shochat switch (attr->gid_type) { 1983cca2606SAchiad Shochat case IB_GID_TYPE_IB: 1993cca2606SAchiad Shochat MLX5_SET_RA(mlx5_addr, roce_version, MLX5_ROCE_VERSION_1); 2003cca2606SAchiad Shochat break; 2013cca2606SAchiad Shochat case IB_GID_TYPE_ROCE_UDP_ENCAP: 2023cca2606SAchiad Shochat MLX5_SET_RA(mlx5_addr, roce_version, MLX5_ROCE_VERSION_2); 2033cca2606SAchiad Shochat break; 2043cca2606SAchiad Shochat 2053cca2606SAchiad Shochat default: 2063cca2606SAchiad Shochat WARN_ON(true); 2073cca2606SAchiad Shochat } 2083cca2606SAchiad Shochat 2093cca2606SAchiad Shochat if (attr->gid_type != IB_GID_TYPE_IB) { 2103cca2606SAchiad Shochat if (ipv6_addr_v4mapped((void *)gid)) 2113cca2606SAchiad Shochat MLX5_SET_RA(mlx5_addr, roce_l3_type, 2123cca2606SAchiad Shochat MLX5_ROCE_L3_TYPE_IPV4); 2133cca2606SAchiad Shochat else 2143cca2606SAchiad Shochat MLX5_SET_RA(mlx5_addr, roce_l3_type, 2153cca2606SAchiad Shochat MLX5_ROCE_L3_TYPE_IPV6); 2163cca2606SAchiad Shochat } 2173cca2606SAchiad Shochat 2183cca2606SAchiad Shochat if ((attr->gid_type == IB_GID_TYPE_IB) || 2193cca2606SAchiad Shochat !ipv6_addr_v4mapped((void *)gid)) 2203cca2606SAchiad Shochat memcpy(mlx5_addr_l3_addr, gid, sizeof(*gid)); 2213cca2606SAchiad Shochat else 2223cca2606SAchiad Shochat memcpy(&mlx5_addr_l3_addr[12], &gid->raw[12], 4); 2233cca2606SAchiad Shochat } 2243cca2606SAchiad Shochat 2253cca2606SAchiad Shochat static int set_roce_addr(struct ib_device *device, u8 port_num, 2263cca2606SAchiad Shochat unsigned int index, 2273cca2606SAchiad Shochat const union ib_gid *gid, 2283cca2606SAchiad Shochat const struct ib_gid_attr *attr) 2293cca2606SAchiad Shochat { 2303cca2606SAchiad Shochat struct mlx5_ib_dev *dev = to_mdev(device); 2313cca2606SAchiad Shochat u32 in[MLX5_ST_SZ_DW(set_roce_address_in)]; 2323cca2606SAchiad Shochat u32 out[MLX5_ST_SZ_DW(set_roce_address_out)]; 2333cca2606SAchiad Shochat void *in_addr = MLX5_ADDR_OF(set_roce_address_in, in, roce_address); 2343cca2606SAchiad Shochat enum rdma_link_layer ll = mlx5_ib_port_link_layer(device, port_num); 2353cca2606SAchiad Shochat 2363cca2606SAchiad Shochat if (ll != IB_LINK_LAYER_ETHERNET) 2373cca2606SAchiad Shochat return -EINVAL; 2383cca2606SAchiad Shochat 2393cca2606SAchiad Shochat memset(in, 0, sizeof(in)); 2403cca2606SAchiad Shochat 2413cca2606SAchiad Shochat ib_gid_to_mlx5_roce_addr(gid, attr, in_addr); 2423cca2606SAchiad Shochat 2433cca2606SAchiad Shochat MLX5_SET(set_roce_address_in, in, roce_address_index, index); 2443cca2606SAchiad Shochat MLX5_SET(set_roce_address_in, in, opcode, MLX5_CMD_OP_SET_ROCE_ADDRESS); 2453cca2606SAchiad Shochat 2463cca2606SAchiad Shochat memset(out, 0, sizeof(out)); 2473cca2606SAchiad Shochat return mlx5_cmd_exec(dev->mdev, in, sizeof(in), out, sizeof(out)); 2483cca2606SAchiad Shochat } 2493cca2606SAchiad Shochat 2503cca2606SAchiad Shochat static int mlx5_ib_add_gid(struct ib_device *device, u8 port_num, 2513cca2606SAchiad Shochat unsigned int index, const union ib_gid *gid, 2523cca2606SAchiad Shochat const struct ib_gid_attr *attr, 2533cca2606SAchiad Shochat __always_unused void **context) 2543cca2606SAchiad Shochat { 2553cca2606SAchiad Shochat return set_roce_addr(device, port_num, index, gid, attr); 2563cca2606SAchiad Shochat } 2573cca2606SAchiad Shochat 2583cca2606SAchiad Shochat static int mlx5_ib_del_gid(struct ib_device *device, u8 port_num, 2593cca2606SAchiad Shochat unsigned int index, __always_unused void **context) 2603cca2606SAchiad Shochat { 2613cca2606SAchiad Shochat return set_roce_addr(device, port_num, index, NULL, NULL); 2623cca2606SAchiad Shochat } 2633cca2606SAchiad Shochat 2642811ba51SAchiad Shochat __be16 mlx5_get_roce_udp_sport(struct mlx5_ib_dev *dev, u8 port_num, 2652811ba51SAchiad Shochat int index) 2662811ba51SAchiad Shochat { 2672811ba51SAchiad Shochat struct ib_gid_attr attr; 2682811ba51SAchiad Shochat union ib_gid gid; 2692811ba51SAchiad Shochat 2702811ba51SAchiad Shochat if (ib_get_cached_gid(&dev->ib_dev, port_num, index, &gid, &attr)) 2712811ba51SAchiad Shochat return 0; 2722811ba51SAchiad Shochat 2732811ba51SAchiad Shochat if (!attr.ndev) 2742811ba51SAchiad Shochat return 0; 2752811ba51SAchiad Shochat 2762811ba51SAchiad Shochat dev_put(attr.ndev); 2772811ba51SAchiad Shochat 2782811ba51SAchiad Shochat if (attr.gid_type != IB_GID_TYPE_ROCE_UDP_ENCAP) 2792811ba51SAchiad Shochat return 0; 2802811ba51SAchiad Shochat 2812811ba51SAchiad Shochat return cpu_to_be16(MLX5_CAP_ROCE(dev->mdev, r_roce_min_src_udp_port)); 2822811ba51SAchiad Shochat } 2832811ba51SAchiad Shochat 2841b5daf11SMajd Dibbiny static int mlx5_use_mad_ifc(struct mlx5_ib_dev *dev) 2851b5daf11SMajd Dibbiny { 2861b5daf11SMajd Dibbiny return !dev->mdev->issi; 2871b5daf11SMajd Dibbiny } 2881b5daf11SMajd Dibbiny 2891b5daf11SMajd Dibbiny enum { 2901b5daf11SMajd Dibbiny MLX5_VPORT_ACCESS_METHOD_MAD, 2911b5daf11SMajd Dibbiny MLX5_VPORT_ACCESS_METHOD_HCA, 2921b5daf11SMajd Dibbiny MLX5_VPORT_ACCESS_METHOD_NIC, 2931b5daf11SMajd Dibbiny }; 2941b5daf11SMajd Dibbiny 2951b5daf11SMajd Dibbiny static int mlx5_get_vport_access_method(struct ib_device *ibdev) 2961b5daf11SMajd Dibbiny { 2971b5daf11SMajd Dibbiny if (mlx5_use_mad_ifc(to_mdev(ibdev))) 2981b5daf11SMajd Dibbiny return MLX5_VPORT_ACCESS_METHOD_MAD; 2991b5daf11SMajd Dibbiny 300ebd61f68SAchiad Shochat if (mlx5_ib_port_link_layer(ibdev, 1) == 3011b5daf11SMajd Dibbiny IB_LINK_LAYER_ETHERNET) 3021b5daf11SMajd Dibbiny return MLX5_VPORT_ACCESS_METHOD_NIC; 3031b5daf11SMajd Dibbiny 3041b5daf11SMajd Dibbiny return MLX5_VPORT_ACCESS_METHOD_HCA; 3051b5daf11SMajd Dibbiny } 3061b5daf11SMajd Dibbiny 307da7525d2SEran Ben Elisha static void get_atomic_caps(struct mlx5_ib_dev *dev, 308da7525d2SEran Ben Elisha struct ib_device_attr *props) 309da7525d2SEran Ben Elisha { 310da7525d2SEran Ben Elisha u8 tmp; 311da7525d2SEran Ben Elisha u8 atomic_operations = MLX5_CAP_ATOMIC(dev->mdev, atomic_operations); 312da7525d2SEran Ben Elisha u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_qp); 313da7525d2SEran Ben Elisha u8 atomic_req_8B_endianness_mode = 314da7525d2SEran Ben Elisha MLX5_CAP_ATOMIC(dev->mdev, atomic_req_8B_endianess_mode); 315da7525d2SEran Ben Elisha 316da7525d2SEran Ben Elisha /* Check if HW supports 8 bytes standard atomic operations and capable 317da7525d2SEran Ben Elisha * of host endianness respond 318da7525d2SEran Ben Elisha */ 319da7525d2SEran Ben Elisha tmp = MLX5_ATOMIC_OPS_CMP_SWAP | MLX5_ATOMIC_OPS_FETCH_ADD; 320da7525d2SEran Ben Elisha if (((atomic_operations & tmp) == tmp) && 321da7525d2SEran Ben Elisha (atomic_size_qp & MLX5_ATOMIC_SIZE_QP_8BYTES) && 322da7525d2SEran Ben Elisha (atomic_req_8B_endianness_mode)) { 323da7525d2SEran Ben Elisha props->atomic_cap = IB_ATOMIC_HCA; 324da7525d2SEran Ben Elisha } else { 325da7525d2SEran Ben Elisha props->atomic_cap = IB_ATOMIC_NONE; 326da7525d2SEran Ben Elisha } 327da7525d2SEran Ben Elisha } 328da7525d2SEran Ben Elisha 3291b5daf11SMajd Dibbiny static int mlx5_query_system_image_guid(struct ib_device *ibdev, 3301b5daf11SMajd Dibbiny __be64 *sys_image_guid) 3311b5daf11SMajd Dibbiny { 3321b5daf11SMajd Dibbiny struct mlx5_ib_dev *dev = to_mdev(ibdev); 3331b5daf11SMajd Dibbiny struct mlx5_core_dev *mdev = dev->mdev; 3341b5daf11SMajd Dibbiny u64 tmp; 3351b5daf11SMajd Dibbiny int err; 3361b5daf11SMajd Dibbiny 3371b5daf11SMajd Dibbiny switch (mlx5_get_vport_access_method(ibdev)) { 3381b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_MAD: 3391b5daf11SMajd Dibbiny return mlx5_query_mad_ifc_system_image_guid(ibdev, 3401b5daf11SMajd Dibbiny sys_image_guid); 3411b5daf11SMajd Dibbiny 3421b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_HCA: 3431b5daf11SMajd Dibbiny err = mlx5_query_hca_vport_system_image_guid(mdev, &tmp); 3443f89a643SAchiad Shochat break; 3453f89a643SAchiad Shochat 3463f89a643SAchiad Shochat case MLX5_VPORT_ACCESS_METHOD_NIC: 3473f89a643SAchiad Shochat err = mlx5_query_nic_vport_system_image_guid(mdev, &tmp); 3483f89a643SAchiad Shochat break; 3491b5daf11SMajd Dibbiny 3501b5daf11SMajd Dibbiny default: 3511b5daf11SMajd Dibbiny return -EINVAL; 3521b5daf11SMajd Dibbiny } 3533f89a643SAchiad Shochat 3543f89a643SAchiad Shochat if (!err) 3553f89a643SAchiad Shochat *sys_image_guid = cpu_to_be64(tmp); 3563f89a643SAchiad Shochat 3573f89a643SAchiad Shochat return err; 3583f89a643SAchiad Shochat 3591b5daf11SMajd Dibbiny } 3601b5daf11SMajd Dibbiny 3611b5daf11SMajd Dibbiny static int mlx5_query_max_pkeys(struct ib_device *ibdev, 3621b5daf11SMajd Dibbiny u16 *max_pkeys) 3631b5daf11SMajd Dibbiny { 3641b5daf11SMajd Dibbiny struct mlx5_ib_dev *dev = to_mdev(ibdev); 3651b5daf11SMajd Dibbiny struct mlx5_core_dev *mdev = dev->mdev; 3661b5daf11SMajd Dibbiny 3671b5daf11SMajd Dibbiny switch (mlx5_get_vport_access_method(ibdev)) { 3681b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_MAD: 3691b5daf11SMajd Dibbiny return mlx5_query_mad_ifc_max_pkeys(ibdev, max_pkeys); 3701b5daf11SMajd Dibbiny 3711b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_HCA: 3721b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_NIC: 3731b5daf11SMajd Dibbiny *max_pkeys = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, 3741b5daf11SMajd Dibbiny pkey_table_size)); 3751b5daf11SMajd Dibbiny return 0; 3761b5daf11SMajd Dibbiny 3771b5daf11SMajd Dibbiny default: 3781b5daf11SMajd Dibbiny return -EINVAL; 3791b5daf11SMajd Dibbiny } 3801b5daf11SMajd Dibbiny } 3811b5daf11SMajd Dibbiny 3821b5daf11SMajd Dibbiny static int mlx5_query_vendor_id(struct ib_device *ibdev, 3831b5daf11SMajd Dibbiny u32 *vendor_id) 3841b5daf11SMajd Dibbiny { 3851b5daf11SMajd Dibbiny struct mlx5_ib_dev *dev = to_mdev(ibdev); 3861b5daf11SMajd Dibbiny 3871b5daf11SMajd Dibbiny switch (mlx5_get_vport_access_method(ibdev)) { 3881b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_MAD: 3891b5daf11SMajd Dibbiny return mlx5_query_mad_ifc_vendor_id(ibdev, vendor_id); 3901b5daf11SMajd Dibbiny 3911b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_HCA: 3921b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_NIC: 3931b5daf11SMajd Dibbiny return mlx5_core_query_vendor_id(dev->mdev, vendor_id); 3941b5daf11SMajd Dibbiny 3951b5daf11SMajd Dibbiny default: 3961b5daf11SMajd Dibbiny return -EINVAL; 3971b5daf11SMajd Dibbiny } 3981b5daf11SMajd Dibbiny } 3991b5daf11SMajd Dibbiny 4001b5daf11SMajd Dibbiny static int mlx5_query_node_guid(struct mlx5_ib_dev *dev, 4011b5daf11SMajd Dibbiny __be64 *node_guid) 4021b5daf11SMajd Dibbiny { 4031b5daf11SMajd Dibbiny u64 tmp; 4041b5daf11SMajd Dibbiny int err; 4051b5daf11SMajd Dibbiny 4061b5daf11SMajd Dibbiny switch (mlx5_get_vport_access_method(&dev->ib_dev)) { 4071b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_MAD: 4081b5daf11SMajd Dibbiny return mlx5_query_mad_ifc_node_guid(dev, node_guid); 4091b5daf11SMajd Dibbiny 4101b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_HCA: 4111b5daf11SMajd Dibbiny err = mlx5_query_hca_vport_node_guid(dev->mdev, &tmp); 4123f89a643SAchiad Shochat break; 4133f89a643SAchiad Shochat 4143f89a643SAchiad Shochat case MLX5_VPORT_ACCESS_METHOD_NIC: 4153f89a643SAchiad Shochat err = mlx5_query_nic_vport_node_guid(dev->mdev, &tmp); 4163f89a643SAchiad Shochat break; 4171b5daf11SMajd Dibbiny 4181b5daf11SMajd Dibbiny default: 4191b5daf11SMajd Dibbiny return -EINVAL; 4201b5daf11SMajd Dibbiny } 4213f89a643SAchiad Shochat 4223f89a643SAchiad Shochat if (!err) 4233f89a643SAchiad Shochat *node_guid = cpu_to_be64(tmp); 4243f89a643SAchiad Shochat 4253f89a643SAchiad Shochat return err; 4261b5daf11SMajd Dibbiny } 4271b5daf11SMajd Dibbiny 4281b5daf11SMajd Dibbiny struct mlx5_reg_node_desc { 4291b5daf11SMajd Dibbiny u8 desc[64]; 4301b5daf11SMajd Dibbiny }; 4311b5daf11SMajd Dibbiny 4321b5daf11SMajd Dibbiny static int mlx5_query_node_desc(struct mlx5_ib_dev *dev, char *node_desc) 4331b5daf11SMajd Dibbiny { 4341b5daf11SMajd Dibbiny struct mlx5_reg_node_desc in; 4351b5daf11SMajd Dibbiny 4361b5daf11SMajd Dibbiny if (mlx5_use_mad_ifc(dev)) 4371b5daf11SMajd Dibbiny return mlx5_query_mad_ifc_node_desc(dev, node_desc); 4381b5daf11SMajd Dibbiny 4391b5daf11SMajd Dibbiny memset(&in, 0, sizeof(in)); 4401b5daf11SMajd Dibbiny 4411b5daf11SMajd Dibbiny return mlx5_core_access_reg(dev->mdev, &in, sizeof(in), node_desc, 4421b5daf11SMajd Dibbiny sizeof(struct mlx5_reg_node_desc), 4431b5daf11SMajd Dibbiny MLX5_REG_NODE_DESC, 0, 0); 4441b5daf11SMajd Dibbiny } 4451b5daf11SMajd Dibbiny 446e126ba97SEli Cohen static int mlx5_ib_query_device(struct ib_device *ibdev, 4472528e33eSMatan Barak struct ib_device_attr *props, 4482528e33eSMatan Barak struct ib_udata *uhw) 449e126ba97SEli Cohen { 450e126ba97SEli Cohen struct mlx5_ib_dev *dev = to_mdev(ibdev); 451938fe83cSSaeed Mahameed struct mlx5_core_dev *mdev = dev->mdev; 452e126ba97SEli Cohen int err = -ENOMEM; 453e126ba97SEli Cohen int max_rq_sg; 454e126ba97SEli Cohen int max_sq_sg; 455e0238a6aSSagi Grimberg u64 min_page_size = 1ull << MLX5_CAP_GEN(mdev, log_pg_sz); 456e126ba97SEli Cohen 4572528e33eSMatan Barak if (uhw->inlen || uhw->outlen) 4582528e33eSMatan Barak return -EINVAL; 4592528e33eSMatan Barak 460e126ba97SEli Cohen memset(props, 0, sizeof(*props)); 4611b5daf11SMajd Dibbiny err = mlx5_query_system_image_guid(ibdev, 4621b5daf11SMajd Dibbiny &props->sys_image_guid); 4631b5daf11SMajd Dibbiny if (err) 4641b5daf11SMajd Dibbiny return err; 4651b5daf11SMajd Dibbiny 4661b5daf11SMajd Dibbiny err = mlx5_query_max_pkeys(ibdev, &props->max_pkeys); 4671b5daf11SMajd Dibbiny if (err) 4681b5daf11SMajd Dibbiny return err; 4691b5daf11SMajd Dibbiny 4701b5daf11SMajd Dibbiny err = mlx5_query_vendor_id(ibdev, &props->vendor_id); 4711b5daf11SMajd Dibbiny if (err) 4721b5daf11SMajd Dibbiny return err; 473e126ba97SEli Cohen 4749603b61dSJack Morgenstein props->fw_ver = ((u64)fw_rev_maj(dev->mdev) << 32) | 4759603b61dSJack Morgenstein (fw_rev_min(dev->mdev) << 16) | 4769603b61dSJack Morgenstein fw_rev_sub(dev->mdev); 477e126ba97SEli Cohen props->device_cap_flags = IB_DEVICE_CHANGE_PHY_PORT | 478e126ba97SEli Cohen IB_DEVICE_PORT_ACTIVE_EVENT | 479e126ba97SEli Cohen IB_DEVICE_SYS_IMAGE_GUID | 4801a4c3a3dSEli Cohen IB_DEVICE_RC_RNR_NAK_GEN; 481938fe83cSSaeed Mahameed 482938fe83cSSaeed Mahameed if (MLX5_CAP_GEN(mdev, pkv)) 483e126ba97SEli Cohen props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR; 484938fe83cSSaeed Mahameed if (MLX5_CAP_GEN(mdev, qkv)) 485e126ba97SEli Cohen props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR; 486938fe83cSSaeed Mahameed if (MLX5_CAP_GEN(mdev, apm)) 487e126ba97SEli Cohen props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG; 488938fe83cSSaeed Mahameed if (MLX5_CAP_GEN(mdev, xrc)) 489e126ba97SEli Cohen props->device_cap_flags |= IB_DEVICE_XRC; 490e126ba97SEli Cohen props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS; 491938fe83cSSaeed Mahameed if (MLX5_CAP_GEN(mdev, sho)) { 4922dea9094SSagi Grimberg props->device_cap_flags |= IB_DEVICE_SIGNATURE_HANDOVER; 4932dea9094SSagi Grimberg /* At this stage no support for signature handover */ 4942dea9094SSagi Grimberg props->sig_prot_cap = IB_PROT_T10DIF_TYPE_1 | 4952dea9094SSagi Grimberg IB_PROT_T10DIF_TYPE_2 | 4962dea9094SSagi Grimberg IB_PROT_T10DIF_TYPE_3; 4972dea9094SSagi Grimberg props->sig_guard_cap = IB_GUARD_T10DIF_CRC | 4982dea9094SSagi Grimberg IB_GUARD_T10DIF_CSUM; 4992dea9094SSagi Grimberg } 500938fe83cSSaeed Mahameed if (MLX5_CAP_GEN(mdev, block_lb_mc)) 501f360d88aSEli Cohen props->device_cap_flags |= IB_DEVICE_BLOCK_MULTICAST_LOOPBACK; 502e126ba97SEli Cohen 50388115fe7SBodong Wang if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) && 50488115fe7SBodong Wang (MLX5_CAP_ETH(dev->mdev, csum_cap))) 50588115fe7SBodong Wang props->device_cap_flags |= IB_DEVICE_RAW_IP_CSUM; 50688115fe7SBodong Wang 507f0313965SErez Shitrit if (MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) { 508f0313965SErez Shitrit props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM; 509f0313965SErez Shitrit props->device_cap_flags |= IB_DEVICE_UD_TSO; 510f0313965SErez Shitrit } 511f0313965SErez Shitrit 5121b5daf11SMajd Dibbiny props->vendor_part_id = mdev->pdev->device; 5131b5daf11SMajd Dibbiny props->hw_ver = mdev->pdev->revision; 514e126ba97SEli Cohen 515e126ba97SEli Cohen props->max_mr_size = ~0ull; 516e0238a6aSSagi Grimberg props->page_size_cap = ~(min_page_size - 1); 517938fe83cSSaeed Mahameed props->max_qp = 1 << MLX5_CAP_GEN(mdev, log_max_qp); 518938fe83cSSaeed Mahameed props->max_qp_wr = 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz); 519938fe83cSSaeed Mahameed max_rq_sg = MLX5_CAP_GEN(mdev, max_wqe_sz_rq) / 520938fe83cSSaeed Mahameed sizeof(struct mlx5_wqe_data_seg); 521938fe83cSSaeed Mahameed max_sq_sg = (MLX5_CAP_GEN(mdev, max_wqe_sz_sq) - 522938fe83cSSaeed Mahameed sizeof(struct mlx5_wqe_ctrl_seg)) / 523e126ba97SEli Cohen sizeof(struct mlx5_wqe_data_seg); 524e126ba97SEli Cohen props->max_sge = min(max_rq_sg, max_sq_sg); 52518ebd407SSagi Grimberg props->max_sge_rd = props->max_sge; 526938fe83cSSaeed Mahameed props->max_cq = 1 << MLX5_CAP_GEN(mdev, log_max_cq); 5279f177686SLeon Romanovsky props->max_cqe = (1 << MLX5_CAP_GEN(mdev, log_max_cq_sz)) - 1; 528938fe83cSSaeed Mahameed props->max_mr = 1 << MLX5_CAP_GEN(mdev, log_max_mkey); 529938fe83cSSaeed Mahameed props->max_pd = 1 << MLX5_CAP_GEN(mdev, log_max_pd); 530938fe83cSSaeed Mahameed props->max_qp_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_req_qp); 531938fe83cSSaeed Mahameed props->max_qp_init_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_res_qp); 532938fe83cSSaeed Mahameed props->max_srq = 1 << MLX5_CAP_GEN(mdev, log_max_srq); 533938fe83cSSaeed Mahameed props->max_srq_wr = (1 << MLX5_CAP_GEN(mdev, log_max_srq_sz)) - 1; 534938fe83cSSaeed Mahameed props->local_ca_ack_delay = MLX5_CAP_GEN(mdev, local_ca_ack_delay); 535e126ba97SEli Cohen props->max_res_rd_atom = props->max_qp_rd_atom * props->max_qp; 536e126ba97SEli Cohen props->max_srq_sge = max_rq_sg - 1; 537e126ba97SEli Cohen props->max_fast_reg_page_list_len = (unsigned int)-1; 538da7525d2SEran Ben Elisha get_atomic_caps(dev, props); 53981bea28fSEli Cohen props->masked_atomic_cap = IB_ATOMIC_NONE; 540938fe83cSSaeed Mahameed props->max_mcast_grp = 1 << MLX5_CAP_GEN(mdev, log_max_mcg); 541938fe83cSSaeed Mahameed props->max_mcast_qp_attach = MLX5_CAP_GEN(mdev, max_qp_mcg); 542e126ba97SEli Cohen props->max_total_mcast_qp_attach = props->max_mcast_qp_attach * 543e126ba97SEli Cohen props->max_mcast_grp; 544e126ba97SEli Cohen props->max_map_per_fmr = INT_MAX; /* no limit in ConnectIB */ 5457c60bcbbSMatan Barak props->hca_core_clock = MLX5_CAP_GEN(mdev, device_frequency_khz); 5467c60bcbbSMatan Barak props->timestamp_mask = 0x7FFFFFFFFFFFFFFFULL; 547e126ba97SEli Cohen 5488cdd312cSHaggai Eran #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING 549938fe83cSSaeed Mahameed if (MLX5_CAP_GEN(mdev, pg)) 5508cdd312cSHaggai Eran props->device_cap_flags |= IB_DEVICE_ON_DEMAND_PAGING; 5518cdd312cSHaggai Eran props->odp_caps = dev->odp_caps; 5528cdd312cSHaggai Eran #endif 5538cdd312cSHaggai Eran 554051f2630SLeon Romanovsky if (MLX5_CAP_GEN(mdev, cd)) 555051f2630SLeon Romanovsky props->device_cap_flags |= IB_DEVICE_CROSS_CHANNEL; 556051f2630SLeon Romanovsky 5571b5daf11SMajd Dibbiny return 0; 5581b5daf11SMajd Dibbiny } 559e126ba97SEli Cohen 5601b5daf11SMajd Dibbiny enum mlx5_ib_width { 5611b5daf11SMajd Dibbiny MLX5_IB_WIDTH_1X = 1 << 0, 5621b5daf11SMajd Dibbiny MLX5_IB_WIDTH_2X = 1 << 1, 5631b5daf11SMajd Dibbiny MLX5_IB_WIDTH_4X = 1 << 2, 5641b5daf11SMajd Dibbiny MLX5_IB_WIDTH_8X = 1 << 3, 5651b5daf11SMajd Dibbiny MLX5_IB_WIDTH_12X = 1 << 4 5661b5daf11SMajd Dibbiny }; 5671b5daf11SMajd Dibbiny 5681b5daf11SMajd Dibbiny static int translate_active_width(struct ib_device *ibdev, u8 active_width, 5691b5daf11SMajd Dibbiny u8 *ib_width) 5701b5daf11SMajd Dibbiny { 5711b5daf11SMajd Dibbiny struct mlx5_ib_dev *dev = to_mdev(ibdev); 5721b5daf11SMajd Dibbiny int err = 0; 5731b5daf11SMajd Dibbiny 5741b5daf11SMajd Dibbiny if (active_width & MLX5_IB_WIDTH_1X) { 5751b5daf11SMajd Dibbiny *ib_width = IB_WIDTH_1X; 5761b5daf11SMajd Dibbiny } else if (active_width & MLX5_IB_WIDTH_2X) { 5771b5daf11SMajd Dibbiny mlx5_ib_dbg(dev, "active_width %d is not supported by IB spec\n", 5781b5daf11SMajd Dibbiny (int)active_width); 5791b5daf11SMajd Dibbiny err = -EINVAL; 5801b5daf11SMajd Dibbiny } else if (active_width & MLX5_IB_WIDTH_4X) { 5811b5daf11SMajd Dibbiny *ib_width = IB_WIDTH_4X; 5821b5daf11SMajd Dibbiny } else if (active_width & MLX5_IB_WIDTH_8X) { 5831b5daf11SMajd Dibbiny *ib_width = IB_WIDTH_8X; 5841b5daf11SMajd Dibbiny } else if (active_width & MLX5_IB_WIDTH_12X) { 5851b5daf11SMajd Dibbiny *ib_width = IB_WIDTH_12X; 5861b5daf11SMajd Dibbiny } else { 5871b5daf11SMajd Dibbiny mlx5_ib_dbg(dev, "Invalid active_width %d\n", 5881b5daf11SMajd Dibbiny (int)active_width); 5891b5daf11SMajd Dibbiny err = -EINVAL; 5901b5daf11SMajd Dibbiny } 5911b5daf11SMajd Dibbiny 5921b5daf11SMajd Dibbiny return err; 5931b5daf11SMajd Dibbiny } 5941b5daf11SMajd Dibbiny 5951b5daf11SMajd Dibbiny static int mlx5_mtu_to_ib_mtu(int mtu) 5961b5daf11SMajd Dibbiny { 5971b5daf11SMajd Dibbiny switch (mtu) { 5981b5daf11SMajd Dibbiny case 256: return 1; 5991b5daf11SMajd Dibbiny case 512: return 2; 6001b5daf11SMajd Dibbiny case 1024: return 3; 6011b5daf11SMajd Dibbiny case 2048: return 4; 6021b5daf11SMajd Dibbiny case 4096: return 5; 6031b5daf11SMajd Dibbiny default: 6041b5daf11SMajd Dibbiny pr_warn("invalid mtu\n"); 6051b5daf11SMajd Dibbiny return -1; 6061b5daf11SMajd Dibbiny } 6071b5daf11SMajd Dibbiny } 6081b5daf11SMajd Dibbiny 6091b5daf11SMajd Dibbiny enum ib_max_vl_num { 6101b5daf11SMajd Dibbiny __IB_MAX_VL_0 = 1, 6111b5daf11SMajd Dibbiny __IB_MAX_VL_0_1 = 2, 6121b5daf11SMajd Dibbiny __IB_MAX_VL_0_3 = 3, 6131b5daf11SMajd Dibbiny __IB_MAX_VL_0_7 = 4, 6141b5daf11SMajd Dibbiny __IB_MAX_VL_0_14 = 5, 6151b5daf11SMajd Dibbiny }; 6161b5daf11SMajd Dibbiny 6171b5daf11SMajd Dibbiny enum mlx5_vl_hw_cap { 6181b5daf11SMajd Dibbiny MLX5_VL_HW_0 = 1, 6191b5daf11SMajd Dibbiny MLX5_VL_HW_0_1 = 2, 6201b5daf11SMajd Dibbiny MLX5_VL_HW_0_2 = 3, 6211b5daf11SMajd Dibbiny MLX5_VL_HW_0_3 = 4, 6221b5daf11SMajd Dibbiny MLX5_VL_HW_0_4 = 5, 6231b5daf11SMajd Dibbiny MLX5_VL_HW_0_5 = 6, 6241b5daf11SMajd Dibbiny MLX5_VL_HW_0_6 = 7, 6251b5daf11SMajd Dibbiny MLX5_VL_HW_0_7 = 8, 6261b5daf11SMajd Dibbiny MLX5_VL_HW_0_14 = 15 6271b5daf11SMajd Dibbiny }; 6281b5daf11SMajd Dibbiny 6291b5daf11SMajd Dibbiny static int translate_max_vl_num(struct ib_device *ibdev, u8 vl_hw_cap, 6301b5daf11SMajd Dibbiny u8 *max_vl_num) 6311b5daf11SMajd Dibbiny { 6321b5daf11SMajd Dibbiny switch (vl_hw_cap) { 6331b5daf11SMajd Dibbiny case MLX5_VL_HW_0: 6341b5daf11SMajd Dibbiny *max_vl_num = __IB_MAX_VL_0; 6351b5daf11SMajd Dibbiny break; 6361b5daf11SMajd Dibbiny case MLX5_VL_HW_0_1: 6371b5daf11SMajd Dibbiny *max_vl_num = __IB_MAX_VL_0_1; 6381b5daf11SMajd Dibbiny break; 6391b5daf11SMajd Dibbiny case MLX5_VL_HW_0_3: 6401b5daf11SMajd Dibbiny *max_vl_num = __IB_MAX_VL_0_3; 6411b5daf11SMajd Dibbiny break; 6421b5daf11SMajd Dibbiny case MLX5_VL_HW_0_7: 6431b5daf11SMajd Dibbiny *max_vl_num = __IB_MAX_VL_0_7; 6441b5daf11SMajd Dibbiny break; 6451b5daf11SMajd Dibbiny case MLX5_VL_HW_0_14: 6461b5daf11SMajd Dibbiny *max_vl_num = __IB_MAX_VL_0_14; 6471b5daf11SMajd Dibbiny break; 6481b5daf11SMajd Dibbiny 6491b5daf11SMajd Dibbiny default: 6501b5daf11SMajd Dibbiny return -EINVAL; 6511b5daf11SMajd Dibbiny } 6521b5daf11SMajd Dibbiny 6531b5daf11SMajd Dibbiny return 0; 6541b5daf11SMajd Dibbiny } 6551b5daf11SMajd Dibbiny 6561b5daf11SMajd Dibbiny static int mlx5_query_hca_port(struct ib_device *ibdev, u8 port, 6571b5daf11SMajd Dibbiny struct ib_port_attr *props) 6581b5daf11SMajd Dibbiny { 6591b5daf11SMajd Dibbiny struct mlx5_ib_dev *dev = to_mdev(ibdev); 6601b5daf11SMajd Dibbiny struct mlx5_core_dev *mdev = dev->mdev; 6611b5daf11SMajd Dibbiny struct mlx5_hca_vport_context *rep; 6621b5daf11SMajd Dibbiny int max_mtu; 6631b5daf11SMajd Dibbiny int oper_mtu; 6641b5daf11SMajd Dibbiny int err; 6651b5daf11SMajd Dibbiny u8 ib_link_width_oper; 6661b5daf11SMajd Dibbiny u8 vl_hw_cap; 6671b5daf11SMajd Dibbiny 6681b5daf11SMajd Dibbiny rep = kzalloc(sizeof(*rep), GFP_KERNEL); 6691b5daf11SMajd Dibbiny if (!rep) { 6701b5daf11SMajd Dibbiny err = -ENOMEM; 6711b5daf11SMajd Dibbiny goto out; 6721b5daf11SMajd Dibbiny } 6731b5daf11SMajd Dibbiny 6741b5daf11SMajd Dibbiny memset(props, 0, sizeof(*props)); 6751b5daf11SMajd Dibbiny 6761b5daf11SMajd Dibbiny err = mlx5_query_hca_vport_context(mdev, 0, port, 0, rep); 6771b5daf11SMajd Dibbiny if (err) 6781b5daf11SMajd Dibbiny goto out; 6791b5daf11SMajd Dibbiny 6801b5daf11SMajd Dibbiny props->lid = rep->lid; 6811b5daf11SMajd Dibbiny props->lmc = rep->lmc; 6821b5daf11SMajd Dibbiny props->sm_lid = rep->sm_lid; 6831b5daf11SMajd Dibbiny props->sm_sl = rep->sm_sl; 6841b5daf11SMajd Dibbiny props->state = rep->vport_state; 6851b5daf11SMajd Dibbiny props->phys_state = rep->port_physical_state; 6861b5daf11SMajd Dibbiny props->port_cap_flags = rep->cap_mask1; 6871b5daf11SMajd Dibbiny props->gid_tbl_len = mlx5_get_gid_table_len(MLX5_CAP_GEN(mdev, gid_table_size)); 6881b5daf11SMajd Dibbiny props->max_msg_sz = 1 << MLX5_CAP_GEN(mdev, log_max_msg); 6891b5daf11SMajd Dibbiny props->pkey_tbl_len = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, pkey_table_size)); 6901b5daf11SMajd Dibbiny props->bad_pkey_cntr = rep->pkey_violation_counter; 6911b5daf11SMajd Dibbiny props->qkey_viol_cntr = rep->qkey_violation_counter; 6921b5daf11SMajd Dibbiny props->subnet_timeout = rep->subnet_timeout; 6931b5daf11SMajd Dibbiny props->init_type_reply = rep->init_type_reply; 6941b5daf11SMajd Dibbiny 6951b5daf11SMajd Dibbiny err = mlx5_query_port_link_width_oper(mdev, &ib_link_width_oper, port); 6961b5daf11SMajd Dibbiny if (err) 6971b5daf11SMajd Dibbiny goto out; 6981b5daf11SMajd Dibbiny 6991b5daf11SMajd Dibbiny err = translate_active_width(ibdev, ib_link_width_oper, 7001b5daf11SMajd Dibbiny &props->active_width); 7011b5daf11SMajd Dibbiny if (err) 7021b5daf11SMajd Dibbiny goto out; 7031b5daf11SMajd Dibbiny err = mlx5_query_port_proto_oper(mdev, &props->active_speed, MLX5_PTYS_IB, 7041b5daf11SMajd Dibbiny port); 7051b5daf11SMajd Dibbiny if (err) 7061b5daf11SMajd Dibbiny goto out; 7071b5daf11SMajd Dibbiny 708facc9699SSaeed Mahameed mlx5_query_port_max_mtu(mdev, &max_mtu, port); 7091b5daf11SMajd Dibbiny 7101b5daf11SMajd Dibbiny props->max_mtu = mlx5_mtu_to_ib_mtu(max_mtu); 7111b5daf11SMajd Dibbiny 712facc9699SSaeed Mahameed mlx5_query_port_oper_mtu(mdev, &oper_mtu, port); 7131b5daf11SMajd Dibbiny 7141b5daf11SMajd Dibbiny props->active_mtu = mlx5_mtu_to_ib_mtu(oper_mtu); 7151b5daf11SMajd Dibbiny 7161b5daf11SMajd Dibbiny err = mlx5_query_port_vl_hw_cap(mdev, &vl_hw_cap, port); 7171b5daf11SMajd Dibbiny if (err) 7181b5daf11SMajd Dibbiny goto out; 7191b5daf11SMajd Dibbiny 7201b5daf11SMajd Dibbiny err = translate_max_vl_num(ibdev, vl_hw_cap, 7211b5daf11SMajd Dibbiny &props->max_vl_num); 7221b5daf11SMajd Dibbiny out: 7231b5daf11SMajd Dibbiny kfree(rep); 724e126ba97SEli Cohen return err; 725e126ba97SEli Cohen } 726e126ba97SEli Cohen 727e126ba97SEli Cohen int mlx5_ib_query_port(struct ib_device *ibdev, u8 port, 728e126ba97SEli Cohen struct ib_port_attr *props) 729e126ba97SEli Cohen { 7301b5daf11SMajd Dibbiny switch (mlx5_get_vport_access_method(ibdev)) { 7311b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_MAD: 7321b5daf11SMajd Dibbiny return mlx5_query_mad_ifc_port(ibdev, port, props); 733e126ba97SEli Cohen 7341b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_HCA: 7351b5daf11SMajd Dibbiny return mlx5_query_hca_port(ibdev, port, props); 7361b5daf11SMajd Dibbiny 7373f89a643SAchiad Shochat case MLX5_VPORT_ACCESS_METHOD_NIC: 7383f89a643SAchiad Shochat return mlx5_query_port_roce(ibdev, port, props); 7393f89a643SAchiad Shochat 7401b5daf11SMajd Dibbiny default: 741e126ba97SEli Cohen return -EINVAL; 742e126ba97SEli Cohen } 743e126ba97SEli Cohen } 744e126ba97SEli Cohen 745e126ba97SEli Cohen static int mlx5_ib_query_gid(struct ib_device *ibdev, u8 port, int index, 746e126ba97SEli Cohen union ib_gid *gid) 747e126ba97SEli Cohen { 7481b5daf11SMajd Dibbiny struct mlx5_ib_dev *dev = to_mdev(ibdev); 7491b5daf11SMajd Dibbiny struct mlx5_core_dev *mdev = dev->mdev; 750e126ba97SEli Cohen 7511b5daf11SMajd Dibbiny switch (mlx5_get_vport_access_method(ibdev)) { 7521b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_MAD: 7531b5daf11SMajd Dibbiny return mlx5_query_mad_ifc_gids(ibdev, port, index, gid); 754e126ba97SEli Cohen 7551b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_HCA: 7561b5daf11SMajd Dibbiny return mlx5_query_hca_vport_gid(mdev, 0, port, 0, index, gid); 757e126ba97SEli Cohen 7581b5daf11SMajd Dibbiny default: 7591b5daf11SMajd Dibbiny return -EINVAL; 7601b5daf11SMajd Dibbiny } 761e126ba97SEli Cohen 762e126ba97SEli Cohen } 763e126ba97SEli Cohen 764e126ba97SEli Cohen static int mlx5_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index, 765e126ba97SEli Cohen u16 *pkey) 766e126ba97SEli Cohen { 7671b5daf11SMajd Dibbiny struct mlx5_ib_dev *dev = to_mdev(ibdev); 7681b5daf11SMajd Dibbiny struct mlx5_core_dev *mdev = dev->mdev; 769e126ba97SEli Cohen 7701b5daf11SMajd Dibbiny switch (mlx5_get_vport_access_method(ibdev)) { 7711b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_MAD: 7721b5daf11SMajd Dibbiny return mlx5_query_mad_ifc_pkey(ibdev, port, index, pkey); 773e126ba97SEli Cohen 7741b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_HCA: 7751b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_NIC: 7761b5daf11SMajd Dibbiny return mlx5_query_hca_vport_pkey(mdev, 0, port, 0, index, 7771b5daf11SMajd Dibbiny pkey); 7781b5daf11SMajd Dibbiny default: 7791b5daf11SMajd Dibbiny return -EINVAL; 780e126ba97SEli Cohen } 7811b5daf11SMajd Dibbiny } 782e126ba97SEli Cohen 783e126ba97SEli Cohen static int mlx5_ib_modify_device(struct ib_device *ibdev, int mask, 784e126ba97SEli Cohen struct ib_device_modify *props) 785e126ba97SEli Cohen { 786e126ba97SEli Cohen struct mlx5_ib_dev *dev = to_mdev(ibdev); 787e126ba97SEli Cohen struct mlx5_reg_node_desc in; 788e126ba97SEli Cohen struct mlx5_reg_node_desc out; 789e126ba97SEli Cohen int err; 790e126ba97SEli Cohen 791e126ba97SEli Cohen if (mask & ~IB_DEVICE_MODIFY_NODE_DESC) 792e126ba97SEli Cohen return -EOPNOTSUPP; 793e126ba97SEli Cohen 794e126ba97SEli Cohen if (!(mask & IB_DEVICE_MODIFY_NODE_DESC)) 795e126ba97SEli Cohen return 0; 796e126ba97SEli Cohen 797e126ba97SEli Cohen /* 798e126ba97SEli Cohen * If possible, pass node desc to FW, so it can generate 799e126ba97SEli Cohen * a 144 trap. If cmd fails, just ignore. 800e126ba97SEli Cohen */ 801e126ba97SEli Cohen memcpy(&in, props->node_desc, 64); 8029603b61dSJack Morgenstein err = mlx5_core_access_reg(dev->mdev, &in, sizeof(in), &out, 803e126ba97SEli Cohen sizeof(out), MLX5_REG_NODE_DESC, 0, 1); 804e126ba97SEli Cohen if (err) 805e126ba97SEli Cohen return err; 806e126ba97SEli Cohen 807e126ba97SEli Cohen memcpy(ibdev->node_desc, props->node_desc, 64); 808e126ba97SEli Cohen 809e126ba97SEli Cohen return err; 810e126ba97SEli Cohen } 811e126ba97SEli Cohen 812e126ba97SEli Cohen static int mlx5_ib_modify_port(struct ib_device *ibdev, u8 port, int mask, 813e126ba97SEli Cohen struct ib_port_modify *props) 814e126ba97SEli Cohen { 815e126ba97SEli Cohen struct mlx5_ib_dev *dev = to_mdev(ibdev); 816e126ba97SEli Cohen struct ib_port_attr attr; 817e126ba97SEli Cohen u32 tmp; 818e126ba97SEli Cohen int err; 819e126ba97SEli Cohen 820e126ba97SEli Cohen mutex_lock(&dev->cap_mask_mutex); 821e126ba97SEli Cohen 822e126ba97SEli Cohen err = mlx5_ib_query_port(ibdev, port, &attr); 823e126ba97SEli Cohen if (err) 824e126ba97SEli Cohen goto out; 825e126ba97SEli Cohen 826e126ba97SEli Cohen tmp = (attr.port_cap_flags | props->set_port_cap_mask) & 827e126ba97SEli Cohen ~props->clr_port_cap_mask; 828e126ba97SEli Cohen 8299603b61dSJack Morgenstein err = mlx5_set_port_caps(dev->mdev, port, tmp); 830e126ba97SEli Cohen 831e126ba97SEli Cohen out: 832e126ba97SEli Cohen mutex_unlock(&dev->cap_mask_mutex); 833e126ba97SEli Cohen return err; 834e126ba97SEli Cohen } 835e126ba97SEli Cohen 836e126ba97SEli Cohen static struct ib_ucontext *mlx5_ib_alloc_ucontext(struct ib_device *ibdev, 837e126ba97SEli Cohen struct ib_udata *udata) 838e126ba97SEli Cohen { 839e126ba97SEli Cohen struct mlx5_ib_dev *dev = to_mdev(ibdev); 840b368d7cbSMatan Barak struct mlx5_ib_alloc_ucontext_req_v2 req = {}; 841b368d7cbSMatan Barak struct mlx5_ib_alloc_ucontext_resp resp = {}; 842e126ba97SEli Cohen struct mlx5_ib_ucontext *context; 843e126ba97SEli Cohen struct mlx5_uuar_info *uuari; 844e126ba97SEli Cohen struct mlx5_uar *uars; 845c1be5232SEli Cohen int gross_uuars; 846e126ba97SEli Cohen int num_uars; 84778c0f98cSEli Cohen int ver; 848e126ba97SEli Cohen int uuarn; 849e126ba97SEli Cohen int err; 850e126ba97SEli Cohen int i; 851f241e749SJack Morgenstein size_t reqlen; 852a168a41cSMajd Dibbiny size_t min_req_v2 = offsetof(struct mlx5_ib_alloc_ucontext_req_v2, 853a168a41cSMajd Dibbiny max_cqe_version); 854e126ba97SEli Cohen 855e126ba97SEli Cohen if (!dev->ib_active) 856e126ba97SEli Cohen return ERR_PTR(-EAGAIN); 857e126ba97SEli Cohen 858dfbee859SHaggai Abramovsky if (udata->inlen < sizeof(struct ib_uverbs_cmd_hdr)) 859dfbee859SHaggai Abramovsky return ERR_PTR(-EINVAL); 860dfbee859SHaggai Abramovsky 86178c0f98cSEli Cohen reqlen = udata->inlen - sizeof(struct ib_uverbs_cmd_hdr); 86278c0f98cSEli Cohen if (reqlen == sizeof(struct mlx5_ib_alloc_ucontext_req)) 86378c0f98cSEli Cohen ver = 0; 864a168a41cSMajd Dibbiny else if (reqlen >= min_req_v2) 86578c0f98cSEli Cohen ver = 2; 86678c0f98cSEli Cohen else 86778c0f98cSEli Cohen return ERR_PTR(-EINVAL); 86878c0f98cSEli Cohen 869b368d7cbSMatan Barak err = ib_copy_from_udata(&req, udata, min(reqlen, sizeof(req))); 870e126ba97SEli Cohen if (err) 871e126ba97SEli Cohen return ERR_PTR(err); 872e126ba97SEli Cohen 873b368d7cbSMatan Barak if (req.flags) 87478c0f98cSEli Cohen return ERR_PTR(-EINVAL); 87578c0f98cSEli Cohen 876e126ba97SEli Cohen if (req.total_num_uuars > MLX5_MAX_UUARS) 877e126ba97SEli Cohen return ERR_PTR(-ENOMEM); 878e126ba97SEli Cohen 879e126ba97SEli Cohen if (req.total_num_uuars == 0) 880e126ba97SEli Cohen return ERR_PTR(-EINVAL); 881e126ba97SEli Cohen 882f72300c5SHaggai Abramovsky if (req.comp_mask || req.reserved0 || req.reserved1 || req.reserved2) 883b368d7cbSMatan Barak return ERR_PTR(-EOPNOTSUPP); 884b368d7cbSMatan Barak 885b368d7cbSMatan Barak if (reqlen > sizeof(req) && 886b368d7cbSMatan Barak !ib_is_udata_cleared(udata, sizeof(req), 887dfbee859SHaggai Abramovsky reqlen - sizeof(req))) 888b368d7cbSMatan Barak return ERR_PTR(-EOPNOTSUPP); 889b368d7cbSMatan Barak 890c1be5232SEli Cohen req.total_num_uuars = ALIGN(req.total_num_uuars, 891c1be5232SEli Cohen MLX5_NON_FP_BF_REGS_PER_PAGE); 892e126ba97SEli Cohen if (req.num_low_latency_uuars > req.total_num_uuars - 1) 893e126ba97SEli Cohen return ERR_PTR(-EINVAL); 894e126ba97SEli Cohen 895c1be5232SEli Cohen num_uars = req.total_num_uuars / MLX5_NON_FP_BF_REGS_PER_PAGE; 896c1be5232SEli Cohen gross_uuars = num_uars * MLX5_BF_REGS_PER_PAGE; 897938fe83cSSaeed Mahameed resp.qp_tab_size = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp); 898938fe83cSSaeed Mahameed resp.bf_reg_size = 1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size); 899e126ba97SEli Cohen resp.cache_line_size = L1_CACHE_BYTES; 900938fe83cSSaeed Mahameed resp.max_sq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq); 901938fe83cSSaeed Mahameed resp.max_rq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq); 902938fe83cSSaeed Mahameed resp.max_send_wqebb = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz); 903938fe83cSSaeed Mahameed resp.max_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz); 904938fe83cSSaeed Mahameed resp.max_srq_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz); 905f72300c5SHaggai Abramovsky resp.cqe_version = min_t(__u8, 906f72300c5SHaggai Abramovsky (__u8)MLX5_CAP_GEN(dev->mdev, cqe_version), 907f72300c5SHaggai Abramovsky req.max_cqe_version); 908b368d7cbSMatan Barak resp.response_length = min(offsetof(typeof(resp), response_length) + 909b368d7cbSMatan Barak sizeof(resp.response_length), udata->outlen); 910e126ba97SEli Cohen 911e126ba97SEli Cohen context = kzalloc(sizeof(*context), GFP_KERNEL); 912e126ba97SEli Cohen if (!context) 913e126ba97SEli Cohen return ERR_PTR(-ENOMEM); 914e126ba97SEli Cohen 915e126ba97SEli Cohen uuari = &context->uuari; 916e126ba97SEli Cohen mutex_init(&uuari->lock); 917e126ba97SEli Cohen uars = kcalloc(num_uars, sizeof(*uars), GFP_KERNEL); 918e126ba97SEli Cohen if (!uars) { 919e126ba97SEli Cohen err = -ENOMEM; 920e126ba97SEli Cohen goto out_ctx; 921e126ba97SEli Cohen } 922e126ba97SEli Cohen 923c1be5232SEli Cohen uuari->bitmap = kcalloc(BITS_TO_LONGS(gross_uuars), 924e126ba97SEli Cohen sizeof(*uuari->bitmap), 925e126ba97SEli Cohen GFP_KERNEL); 926e126ba97SEli Cohen if (!uuari->bitmap) { 927e126ba97SEli Cohen err = -ENOMEM; 928e126ba97SEli Cohen goto out_uar_ctx; 929e126ba97SEli Cohen } 930e126ba97SEli Cohen /* 931e126ba97SEli Cohen * clear all fast path uuars 932e126ba97SEli Cohen */ 933c1be5232SEli Cohen for (i = 0; i < gross_uuars; i++) { 934e126ba97SEli Cohen uuarn = i & 3; 935e126ba97SEli Cohen if (uuarn == 2 || uuarn == 3) 936e126ba97SEli Cohen set_bit(i, uuari->bitmap); 937e126ba97SEli Cohen } 938e126ba97SEli Cohen 939c1be5232SEli Cohen uuari->count = kcalloc(gross_uuars, sizeof(*uuari->count), GFP_KERNEL); 940e126ba97SEli Cohen if (!uuari->count) { 941e126ba97SEli Cohen err = -ENOMEM; 942e126ba97SEli Cohen goto out_bitmap; 943e126ba97SEli Cohen } 944e126ba97SEli Cohen 945e126ba97SEli Cohen for (i = 0; i < num_uars; i++) { 9469603b61dSJack Morgenstein err = mlx5_cmd_alloc_uar(dev->mdev, &uars[i].index); 947e126ba97SEli Cohen if (err) 948e126ba97SEli Cohen goto out_count; 949e126ba97SEli Cohen } 950e126ba97SEli Cohen 951b4cfe447SHaggai Eran #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING 952b4cfe447SHaggai Eran context->ibucontext.invalidate_range = &mlx5_ib_invalidate_range; 953b4cfe447SHaggai Eran #endif 954b4cfe447SHaggai Eran 955146d2f1aSmajd@mellanox.com if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain)) { 956146d2f1aSmajd@mellanox.com err = mlx5_core_alloc_transport_domain(dev->mdev, 957146d2f1aSmajd@mellanox.com &context->tdn); 958146d2f1aSmajd@mellanox.com if (err) 959146d2f1aSmajd@mellanox.com goto out_uars; 960146d2f1aSmajd@mellanox.com } 961146d2f1aSmajd@mellanox.com 962e126ba97SEli Cohen INIT_LIST_HEAD(&context->db_page_list); 963e126ba97SEli Cohen mutex_init(&context->db_page_mutex); 964e126ba97SEli Cohen 965e126ba97SEli Cohen resp.tot_uuars = req.total_num_uuars; 966938fe83cSSaeed Mahameed resp.num_ports = MLX5_CAP_GEN(dev->mdev, num_ports); 967b368d7cbSMatan Barak 968f72300c5SHaggai Abramovsky if (field_avail(typeof(resp), cqe_version, udata->outlen)) 969f72300c5SHaggai Abramovsky resp.response_length += sizeof(resp.cqe_version); 970b368d7cbSMatan Barak 971b368d7cbSMatan Barak if (field_avail(typeof(resp), hca_core_clock_offset, udata->outlen)) { 972b368d7cbSMatan Barak resp.comp_mask |= 973b368d7cbSMatan Barak MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_CORE_CLOCK_OFFSET; 974b368d7cbSMatan Barak resp.hca_core_clock_offset = 975b368d7cbSMatan Barak offsetof(struct mlx5_init_seg, internal_timer_h) % 976b368d7cbSMatan Barak PAGE_SIZE; 977f72300c5SHaggai Abramovsky resp.response_length += sizeof(resp.hca_core_clock_offset) + 978f72300c5SHaggai Abramovsky sizeof(resp.reserved2) + 979f72300c5SHaggai Abramovsky sizeof(resp.reserved3); 980b368d7cbSMatan Barak } 981b368d7cbSMatan Barak 982b368d7cbSMatan Barak err = ib_copy_to_udata(udata, &resp, resp.response_length); 983e126ba97SEli Cohen if (err) 984146d2f1aSmajd@mellanox.com goto out_td; 985e126ba97SEli Cohen 98678c0f98cSEli Cohen uuari->ver = ver; 987e126ba97SEli Cohen uuari->num_low_latency_uuars = req.num_low_latency_uuars; 988e126ba97SEli Cohen uuari->uars = uars; 989e126ba97SEli Cohen uuari->num_uars = num_uars; 990f72300c5SHaggai Abramovsky context->cqe_version = resp.cqe_version; 991f72300c5SHaggai Abramovsky 992e126ba97SEli Cohen return &context->ibucontext; 993e126ba97SEli Cohen 994146d2f1aSmajd@mellanox.com out_td: 995146d2f1aSmajd@mellanox.com if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain)) 996146d2f1aSmajd@mellanox.com mlx5_core_dealloc_transport_domain(dev->mdev, context->tdn); 997146d2f1aSmajd@mellanox.com 998e126ba97SEli Cohen out_uars: 999e126ba97SEli Cohen for (i--; i >= 0; i--) 10009603b61dSJack Morgenstein mlx5_cmd_free_uar(dev->mdev, uars[i].index); 1001e126ba97SEli Cohen out_count: 1002e126ba97SEli Cohen kfree(uuari->count); 1003e126ba97SEli Cohen 1004e126ba97SEli Cohen out_bitmap: 1005e126ba97SEli Cohen kfree(uuari->bitmap); 1006e126ba97SEli Cohen 1007e126ba97SEli Cohen out_uar_ctx: 1008e126ba97SEli Cohen kfree(uars); 1009e126ba97SEli Cohen 1010e126ba97SEli Cohen out_ctx: 1011e126ba97SEli Cohen kfree(context); 1012e126ba97SEli Cohen return ERR_PTR(err); 1013e126ba97SEli Cohen } 1014e126ba97SEli Cohen 1015e126ba97SEli Cohen static int mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext) 1016e126ba97SEli Cohen { 1017e126ba97SEli Cohen struct mlx5_ib_ucontext *context = to_mucontext(ibcontext); 1018e126ba97SEli Cohen struct mlx5_ib_dev *dev = to_mdev(ibcontext->device); 1019e126ba97SEli Cohen struct mlx5_uuar_info *uuari = &context->uuari; 1020e126ba97SEli Cohen int i; 1021e126ba97SEli Cohen 1022146d2f1aSmajd@mellanox.com if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain)) 1023146d2f1aSmajd@mellanox.com mlx5_core_dealloc_transport_domain(dev->mdev, context->tdn); 1024146d2f1aSmajd@mellanox.com 1025e126ba97SEli Cohen for (i = 0; i < uuari->num_uars; i++) { 10269603b61dSJack Morgenstein if (mlx5_cmd_free_uar(dev->mdev, uuari->uars[i].index)) 1027e126ba97SEli Cohen mlx5_ib_warn(dev, "failed to free UAR 0x%x\n", uuari->uars[i].index); 1028e126ba97SEli Cohen } 1029e126ba97SEli Cohen 1030e126ba97SEli Cohen kfree(uuari->count); 1031e126ba97SEli Cohen kfree(uuari->bitmap); 1032e126ba97SEli Cohen kfree(uuari->uars); 1033e126ba97SEli Cohen kfree(context); 1034e126ba97SEli Cohen 1035e126ba97SEli Cohen return 0; 1036e126ba97SEli Cohen } 1037e126ba97SEli Cohen 1038e126ba97SEli Cohen static phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev, int index) 1039e126ba97SEli Cohen { 10409603b61dSJack Morgenstein return (pci_resource_start(dev->mdev->pdev, 0) >> PAGE_SHIFT) + index; 1041e126ba97SEli Cohen } 1042e126ba97SEli Cohen 1043e126ba97SEli Cohen static int get_command(unsigned long offset) 1044e126ba97SEli Cohen { 1045e126ba97SEli Cohen return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK; 1046e126ba97SEli Cohen } 1047e126ba97SEli Cohen 1048e126ba97SEli Cohen static int get_arg(unsigned long offset) 1049e126ba97SEli Cohen { 1050e126ba97SEli Cohen return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1); 1051e126ba97SEli Cohen } 1052e126ba97SEli Cohen 1053e126ba97SEli Cohen static int get_index(unsigned long offset) 1054e126ba97SEli Cohen { 1055e126ba97SEli Cohen return get_arg(offset); 1056e126ba97SEli Cohen } 1057e126ba97SEli Cohen 1058e126ba97SEli Cohen static int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma) 1059e126ba97SEli Cohen { 1060e126ba97SEli Cohen struct mlx5_ib_ucontext *context = to_mucontext(ibcontext); 1061e126ba97SEli Cohen struct mlx5_ib_dev *dev = to_mdev(ibcontext->device); 1062e126ba97SEli Cohen struct mlx5_uuar_info *uuari = &context->uuari; 1063e126ba97SEli Cohen unsigned long command; 1064e126ba97SEli Cohen unsigned long idx; 1065e126ba97SEli Cohen phys_addr_t pfn; 1066e126ba97SEli Cohen 1067e126ba97SEli Cohen command = get_command(vma->vm_pgoff); 1068e126ba97SEli Cohen switch (command) { 1069e126ba97SEli Cohen case MLX5_IB_MMAP_REGULAR_PAGE: 1070e126ba97SEli Cohen if (vma->vm_end - vma->vm_start != PAGE_SIZE) 1071e126ba97SEli Cohen return -EINVAL; 1072e126ba97SEli Cohen 1073e126ba97SEli Cohen idx = get_index(vma->vm_pgoff); 10741c3ce90dSEli Cohen if (idx >= uuari->num_uars) 10751c3ce90dSEli Cohen return -EINVAL; 10761c3ce90dSEli Cohen 1077e126ba97SEli Cohen pfn = uar_index2pfn(dev, uuari->uars[idx].index); 1078e126ba97SEli Cohen mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn 0x%llx\n", idx, 1079e126ba97SEli Cohen (unsigned long long)pfn); 1080e126ba97SEli Cohen 1081e126ba97SEli Cohen vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot); 1082e126ba97SEli Cohen if (io_remap_pfn_range(vma, vma->vm_start, pfn, 1083e126ba97SEli Cohen PAGE_SIZE, vma->vm_page_prot)) 1084e126ba97SEli Cohen return -EAGAIN; 1085e126ba97SEli Cohen 1086e126ba97SEli Cohen mlx5_ib_dbg(dev, "mapped WC at 0x%lx, PA 0x%llx\n", 1087e126ba97SEli Cohen vma->vm_start, 1088e126ba97SEli Cohen (unsigned long long)pfn << PAGE_SHIFT); 1089e126ba97SEli Cohen break; 1090e126ba97SEli Cohen 1091e126ba97SEli Cohen case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES: 1092e126ba97SEli Cohen return -ENOSYS; 1093e126ba97SEli Cohen 1094d69e3bcfSMatan Barak case MLX5_IB_MMAP_CORE_CLOCK: 1095d69e3bcfSMatan Barak if (vma->vm_end - vma->vm_start != PAGE_SIZE) 1096d69e3bcfSMatan Barak return -EINVAL; 1097d69e3bcfSMatan Barak 1098d69e3bcfSMatan Barak if (vma->vm_flags & (VM_WRITE | VM_EXEC)) 1099d69e3bcfSMatan Barak return -EPERM; 1100d69e3bcfSMatan Barak 1101d69e3bcfSMatan Barak /* Don't expose to user-space information it shouldn't have */ 1102d69e3bcfSMatan Barak if (PAGE_SIZE > 4096) 1103d69e3bcfSMatan Barak return -EOPNOTSUPP; 1104d69e3bcfSMatan Barak 1105d69e3bcfSMatan Barak vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); 1106d69e3bcfSMatan Barak pfn = (dev->mdev->iseg_base + 1107d69e3bcfSMatan Barak offsetof(struct mlx5_init_seg, internal_timer_h)) >> 1108d69e3bcfSMatan Barak PAGE_SHIFT; 1109d69e3bcfSMatan Barak if (io_remap_pfn_range(vma, vma->vm_start, pfn, 1110d69e3bcfSMatan Barak PAGE_SIZE, vma->vm_page_prot)) 1111d69e3bcfSMatan Barak return -EAGAIN; 1112d69e3bcfSMatan Barak 1113d69e3bcfSMatan Barak mlx5_ib_dbg(dev, "mapped internal timer at 0x%lx, PA 0x%llx\n", 1114d69e3bcfSMatan Barak vma->vm_start, 1115d69e3bcfSMatan Barak (unsigned long long)pfn << PAGE_SHIFT); 1116d69e3bcfSMatan Barak break; 1117d69e3bcfSMatan Barak 1118e126ba97SEli Cohen default: 1119e126ba97SEli Cohen return -EINVAL; 1120e126ba97SEli Cohen } 1121e126ba97SEli Cohen 1122e126ba97SEli Cohen return 0; 1123e126ba97SEli Cohen } 1124e126ba97SEli Cohen 1125e126ba97SEli Cohen static struct ib_pd *mlx5_ib_alloc_pd(struct ib_device *ibdev, 1126e126ba97SEli Cohen struct ib_ucontext *context, 1127e126ba97SEli Cohen struct ib_udata *udata) 1128e126ba97SEli Cohen { 1129e126ba97SEli Cohen struct mlx5_ib_alloc_pd_resp resp; 1130e126ba97SEli Cohen struct mlx5_ib_pd *pd; 1131e126ba97SEli Cohen int err; 1132e126ba97SEli Cohen 1133e126ba97SEli Cohen pd = kmalloc(sizeof(*pd), GFP_KERNEL); 1134e126ba97SEli Cohen if (!pd) 1135e126ba97SEli Cohen return ERR_PTR(-ENOMEM); 1136e126ba97SEli Cohen 11379603b61dSJack Morgenstein err = mlx5_core_alloc_pd(to_mdev(ibdev)->mdev, &pd->pdn); 1138e126ba97SEli Cohen if (err) { 1139e126ba97SEli Cohen kfree(pd); 1140e126ba97SEli Cohen return ERR_PTR(err); 1141e126ba97SEli Cohen } 1142e126ba97SEli Cohen 1143e126ba97SEli Cohen if (context) { 1144e126ba97SEli Cohen resp.pdn = pd->pdn; 1145e126ba97SEli Cohen if (ib_copy_to_udata(udata, &resp, sizeof(resp))) { 11469603b61dSJack Morgenstein mlx5_core_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn); 1147e126ba97SEli Cohen kfree(pd); 1148e126ba97SEli Cohen return ERR_PTR(-EFAULT); 1149e126ba97SEli Cohen } 1150e126ba97SEli Cohen } 1151e126ba97SEli Cohen 1152e126ba97SEli Cohen return &pd->ibpd; 1153e126ba97SEli Cohen } 1154e126ba97SEli Cohen 1155e126ba97SEli Cohen static int mlx5_ib_dealloc_pd(struct ib_pd *pd) 1156e126ba97SEli Cohen { 1157e126ba97SEli Cohen struct mlx5_ib_dev *mdev = to_mdev(pd->device); 1158e126ba97SEli Cohen struct mlx5_ib_pd *mpd = to_mpd(pd); 1159e126ba97SEli Cohen 11609603b61dSJack Morgenstein mlx5_core_dealloc_pd(mdev->mdev, mpd->pdn); 1161e126ba97SEli Cohen kfree(mpd); 1162e126ba97SEli Cohen 1163e126ba97SEli Cohen return 0; 1164e126ba97SEli Cohen } 1165e126ba97SEli Cohen 1166038d2ef8SMaor Gottlieb static bool outer_header_zero(u32 *match_criteria) 1167038d2ef8SMaor Gottlieb { 1168038d2ef8SMaor Gottlieb int size = MLX5_ST_SZ_BYTES(fte_match_param); 1169038d2ef8SMaor Gottlieb char *outer_headers_c = MLX5_ADDR_OF(fte_match_param, match_criteria, 1170038d2ef8SMaor Gottlieb outer_headers); 1171038d2ef8SMaor Gottlieb 1172038d2ef8SMaor Gottlieb return outer_headers_c[0] == 0 && !memcmp(outer_headers_c, 1173038d2ef8SMaor Gottlieb outer_headers_c + 1, 1174038d2ef8SMaor Gottlieb size - 1); 1175038d2ef8SMaor Gottlieb } 1176038d2ef8SMaor Gottlieb 1177038d2ef8SMaor Gottlieb static int parse_flow_attr(u32 *match_c, u32 *match_v, 1178038d2ef8SMaor Gottlieb union ib_flow_spec *ib_spec) 1179038d2ef8SMaor Gottlieb { 1180038d2ef8SMaor Gottlieb void *outer_headers_c = MLX5_ADDR_OF(fte_match_param, match_c, 1181038d2ef8SMaor Gottlieb outer_headers); 1182038d2ef8SMaor Gottlieb void *outer_headers_v = MLX5_ADDR_OF(fte_match_param, match_v, 1183038d2ef8SMaor Gottlieb outer_headers); 1184038d2ef8SMaor Gottlieb switch (ib_spec->type) { 1185038d2ef8SMaor Gottlieb case IB_FLOW_SPEC_ETH: 1186038d2ef8SMaor Gottlieb if (ib_spec->size != sizeof(ib_spec->eth)) 1187038d2ef8SMaor Gottlieb return -EINVAL; 1188038d2ef8SMaor Gottlieb 1189038d2ef8SMaor Gottlieb ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_c, 1190038d2ef8SMaor Gottlieb dmac_47_16), 1191038d2ef8SMaor Gottlieb ib_spec->eth.mask.dst_mac); 1192038d2ef8SMaor Gottlieb ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_v, 1193038d2ef8SMaor Gottlieb dmac_47_16), 1194038d2ef8SMaor Gottlieb ib_spec->eth.val.dst_mac); 1195038d2ef8SMaor Gottlieb 1196038d2ef8SMaor Gottlieb if (ib_spec->eth.mask.vlan_tag) { 1197038d2ef8SMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, 1198038d2ef8SMaor Gottlieb vlan_tag, 1); 1199038d2ef8SMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, 1200038d2ef8SMaor Gottlieb vlan_tag, 1); 1201038d2ef8SMaor Gottlieb 1202038d2ef8SMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, 1203038d2ef8SMaor Gottlieb first_vid, ntohs(ib_spec->eth.mask.vlan_tag)); 1204038d2ef8SMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, 1205038d2ef8SMaor Gottlieb first_vid, ntohs(ib_spec->eth.val.vlan_tag)); 1206038d2ef8SMaor Gottlieb 1207038d2ef8SMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, 1208038d2ef8SMaor Gottlieb first_cfi, 1209038d2ef8SMaor Gottlieb ntohs(ib_spec->eth.mask.vlan_tag) >> 12); 1210038d2ef8SMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, 1211038d2ef8SMaor Gottlieb first_cfi, 1212038d2ef8SMaor Gottlieb ntohs(ib_spec->eth.val.vlan_tag) >> 12); 1213038d2ef8SMaor Gottlieb 1214038d2ef8SMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, 1215038d2ef8SMaor Gottlieb first_prio, 1216038d2ef8SMaor Gottlieb ntohs(ib_spec->eth.mask.vlan_tag) >> 13); 1217038d2ef8SMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, 1218038d2ef8SMaor Gottlieb first_prio, 1219038d2ef8SMaor Gottlieb ntohs(ib_spec->eth.val.vlan_tag) >> 13); 1220038d2ef8SMaor Gottlieb } 1221038d2ef8SMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, 1222038d2ef8SMaor Gottlieb ethertype, ntohs(ib_spec->eth.mask.ether_type)); 1223038d2ef8SMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, 1224038d2ef8SMaor Gottlieb ethertype, ntohs(ib_spec->eth.val.ether_type)); 1225038d2ef8SMaor Gottlieb break; 1226038d2ef8SMaor Gottlieb case IB_FLOW_SPEC_IPV4: 1227038d2ef8SMaor Gottlieb if (ib_spec->size != sizeof(ib_spec->ipv4)) 1228038d2ef8SMaor Gottlieb return -EINVAL; 1229038d2ef8SMaor Gottlieb 1230038d2ef8SMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, 1231038d2ef8SMaor Gottlieb ethertype, 0xffff); 1232038d2ef8SMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, 1233038d2ef8SMaor Gottlieb ethertype, ETH_P_IP); 1234038d2ef8SMaor Gottlieb 1235038d2ef8SMaor Gottlieb memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_c, 1236038d2ef8SMaor Gottlieb src_ipv4_src_ipv6.ipv4_layout.ipv4), 1237038d2ef8SMaor Gottlieb &ib_spec->ipv4.mask.src_ip, 1238038d2ef8SMaor Gottlieb sizeof(ib_spec->ipv4.mask.src_ip)); 1239038d2ef8SMaor Gottlieb memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_v, 1240038d2ef8SMaor Gottlieb src_ipv4_src_ipv6.ipv4_layout.ipv4), 1241038d2ef8SMaor Gottlieb &ib_spec->ipv4.val.src_ip, 1242038d2ef8SMaor Gottlieb sizeof(ib_spec->ipv4.val.src_ip)); 1243038d2ef8SMaor Gottlieb memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_c, 1244038d2ef8SMaor Gottlieb dst_ipv4_dst_ipv6.ipv4_layout.ipv4), 1245038d2ef8SMaor Gottlieb &ib_spec->ipv4.mask.dst_ip, 1246038d2ef8SMaor Gottlieb sizeof(ib_spec->ipv4.mask.dst_ip)); 1247038d2ef8SMaor Gottlieb memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_v, 1248038d2ef8SMaor Gottlieb dst_ipv4_dst_ipv6.ipv4_layout.ipv4), 1249038d2ef8SMaor Gottlieb &ib_spec->ipv4.val.dst_ip, 1250038d2ef8SMaor Gottlieb sizeof(ib_spec->ipv4.val.dst_ip)); 1251038d2ef8SMaor Gottlieb break; 1252038d2ef8SMaor Gottlieb case IB_FLOW_SPEC_TCP: 1253038d2ef8SMaor Gottlieb if (ib_spec->size != sizeof(ib_spec->tcp_udp)) 1254038d2ef8SMaor Gottlieb return -EINVAL; 1255038d2ef8SMaor Gottlieb 1256038d2ef8SMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, ip_protocol, 1257038d2ef8SMaor Gottlieb 0xff); 1258038d2ef8SMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, ip_protocol, 1259038d2ef8SMaor Gottlieb IPPROTO_TCP); 1260038d2ef8SMaor Gottlieb 1261038d2ef8SMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, tcp_sport, 1262038d2ef8SMaor Gottlieb ntohs(ib_spec->tcp_udp.mask.src_port)); 1263038d2ef8SMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, tcp_sport, 1264038d2ef8SMaor Gottlieb ntohs(ib_spec->tcp_udp.val.src_port)); 1265038d2ef8SMaor Gottlieb 1266038d2ef8SMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, tcp_dport, 1267038d2ef8SMaor Gottlieb ntohs(ib_spec->tcp_udp.mask.dst_port)); 1268038d2ef8SMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, tcp_dport, 1269038d2ef8SMaor Gottlieb ntohs(ib_spec->tcp_udp.val.dst_port)); 1270038d2ef8SMaor Gottlieb break; 1271038d2ef8SMaor Gottlieb case IB_FLOW_SPEC_UDP: 1272038d2ef8SMaor Gottlieb if (ib_spec->size != sizeof(ib_spec->tcp_udp)) 1273038d2ef8SMaor Gottlieb return -EINVAL; 1274038d2ef8SMaor Gottlieb 1275038d2ef8SMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, ip_protocol, 1276038d2ef8SMaor Gottlieb 0xff); 1277038d2ef8SMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, ip_protocol, 1278038d2ef8SMaor Gottlieb IPPROTO_UDP); 1279038d2ef8SMaor Gottlieb 1280038d2ef8SMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, udp_sport, 1281038d2ef8SMaor Gottlieb ntohs(ib_spec->tcp_udp.mask.src_port)); 1282038d2ef8SMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, udp_sport, 1283038d2ef8SMaor Gottlieb ntohs(ib_spec->tcp_udp.val.src_port)); 1284038d2ef8SMaor Gottlieb 1285038d2ef8SMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, udp_dport, 1286038d2ef8SMaor Gottlieb ntohs(ib_spec->tcp_udp.mask.dst_port)); 1287038d2ef8SMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, udp_dport, 1288038d2ef8SMaor Gottlieb ntohs(ib_spec->tcp_udp.val.dst_port)); 1289038d2ef8SMaor Gottlieb break; 1290038d2ef8SMaor Gottlieb default: 1291038d2ef8SMaor Gottlieb return -EINVAL; 1292038d2ef8SMaor Gottlieb } 1293038d2ef8SMaor Gottlieb 1294038d2ef8SMaor Gottlieb return 0; 1295038d2ef8SMaor Gottlieb } 1296038d2ef8SMaor Gottlieb 1297038d2ef8SMaor Gottlieb /* If a flow could catch both multicast and unicast packets, 1298038d2ef8SMaor Gottlieb * it won't fall into the multicast flow steering table and this rule 1299038d2ef8SMaor Gottlieb * could steal other multicast packets. 1300038d2ef8SMaor Gottlieb */ 1301038d2ef8SMaor Gottlieb static bool flow_is_multicast_only(struct ib_flow_attr *ib_attr) 1302038d2ef8SMaor Gottlieb { 1303038d2ef8SMaor Gottlieb struct ib_flow_spec_eth *eth_spec; 1304038d2ef8SMaor Gottlieb 1305038d2ef8SMaor Gottlieb if (ib_attr->type != IB_FLOW_ATTR_NORMAL || 1306038d2ef8SMaor Gottlieb ib_attr->size < sizeof(struct ib_flow_attr) + 1307038d2ef8SMaor Gottlieb sizeof(struct ib_flow_spec_eth) || 1308038d2ef8SMaor Gottlieb ib_attr->num_of_specs < 1) 1309038d2ef8SMaor Gottlieb return false; 1310038d2ef8SMaor Gottlieb 1311038d2ef8SMaor Gottlieb eth_spec = (struct ib_flow_spec_eth *)(ib_attr + 1); 1312038d2ef8SMaor Gottlieb if (eth_spec->type != IB_FLOW_SPEC_ETH || 1313038d2ef8SMaor Gottlieb eth_spec->size != sizeof(*eth_spec)) 1314038d2ef8SMaor Gottlieb return false; 1315038d2ef8SMaor Gottlieb 1316038d2ef8SMaor Gottlieb return is_multicast_ether_addr(eth_spec->mask.dst_mac) && 1317038d2ef8SMaor Gottlieb is_multicast_ether_addr(eth_spec->val.dst_mac); 1318038d2ef8SMaor Gottlieb } 1319038d2ef8SMaor Gottlieb 1320038d2ef8SMaor Gottlieb static bool is_valid_attr(struct ib_flow_attr *flow_attr) 1321038d2ef8SMaor Gottlieb { 1322038d2ef8SMaor Gottlieb union ib_flow_spec *ib_spec = (union ib_flow_spec *)(flow_attr + 1); 1323038d2ef8SMaor Gottlieb bool has_ipv4_spec = false; 1324038d2ef8SMaor Gottlieb bool eth_type_ipv4 = true; 1325038d2ef8SMaor Gottlieb unsigned int spec_index; 1326038d2ef8SMaor Gottlieb 1327038d2ef8SMaor Gottlieb /* Validate that ethertype is correct */ 1328038d2ef8SMaor Gottlieb for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) { 1329038d2ef8SMaor Gottlieb if (ib_spec->type == IB_FLOW_SPEC_ETH && 1330038d2ef8SMaor Gottlieb ib_spec->eth.mask.ether_type) { 1331038d2ef8SMaor Gottlieb if (!((ib_spec->eth.mask.ether_type == htons(0xffff)) && 1332038d2ef8SMaor Gottlieb ib_spec->eth.val.ether_type == htons(ETH_P_IP))) 1333038d2ef8SMaor Gottlieb eth_type_ipv4 = false; 1334038d2ef8SMaor Gottlieb } else if (ib_spec->type == IB_FLOW_SPEC_IPV4) { 1335038d2ef8SMaor Gottlieb has_ipv4_spec = true; 1336038d2ef8SMaor Gottlieb } 1337038d2ef8SMaor Gottlieb ib_spec = (void *)ib_spec + ib_spec->size; 1338038d2ef8SMaor Gottlieb } 1339038d2ef8SMaor Gottlieb return !has_ipv4_spec || eth_type_ipv4; 1340038d2ef8SMaor Gottlieb } 1341038d2ef8SMaor Gottlieb 1342038d2ef8SMaor Gottlieb static void put_flow_table(struct mlx5_ib_dev *dev, 1343038d2ef8SMaor Gottlieb struct mlx5_ib_flow_prio *prio, bool ft_added) 1344038d2ef8SMaor Gottlieb { 1345038d2ef8SMaor Gottlieb prio->refcount -= !!ft_added; 1346038d2ef8SMaor Gottlieb if (!prio->refcount) { 1347038d2ef8SMaor Gottlieb mlx5_destroy_flow_table(prio->flow_table); 1348038d2ef8SMaor Gottlieb prio->flow_table = NULL; 1349038d2ef8SMaor Gottlieb } 1350038d2ef8SMaor Gottlieb } 1351038d2ef8SMaor Gottlieb 1352038d2ef8SMaor Gottlieb static int mlx5_ib_destroy_flow(struct ib_flow *flow_id) 1353038d2ef8SMaor Gottlieb { 1354038d2ef8SMaor Gottlieb struct mlx5_ib_dev *dev = to_mdev(flow_id->qp->device); 1355038d2ef8SMaor Gottlieb struct mlx5_ib_flow_handler *handler = container_of(flow_id, 1356038d2ef8SMaor Gottlieb struct mlx5_ib_flow_handler, 1357038d2ef8SMaor Gottlieb ibflow); 1358038d2ef8SMaor Gottlieb struct mlx5_ib_flow_handler *iter, *tmp; 1359038d2ef8SMaor Gottlieb 1360038d2ef8SMaor Gottlieb mutex_lock(&dev->flow_db.lock); 1361038d2ef8SMaor Gottlieb 1362038d2ef8SMaor Gottlieb list_for_each_entry_safe(iter, tmp, &handler->list, list) { 1363038d2ef8SMaor Gottlieb mlx5_del_flow_rule(iter->rule); 1364038d2ef8SMaor Gottlieb list_del(&iter->list); 1365038d2ef8SMaor Gottlieb kfree(iter); 1366038d2ef8SMaor Gottlieb } 1367038d2ef8SMaor Gottlieb 1368038d2ef8SMaor Gottlieb mlx5_del_flow_rule(handler->rule); 1369038d2ef8SMaor Gottlieb put_flow_table(dev, &dev->flow_db.prios[handler->prio], true); 1370038d2ef8SMaor Gottlieb mutex_unlock(&dev->flow_db.lock); 1371038d2ef8SMaor Gottlieb 1372038d2ef8SMaor Gottlieb kfree(handler); 1373038d2ef8SMaor Gottlieb 1374038d2ef8SMaor Gottlieb return 0; 1375038d2ef8SMaor Gottlieb } 1376038d2ef8SMaor Gottlieb 1377038d2ef8SMaor Gottlieb #define MLX5_FS_MAX_TYPES 10 1378038d2ef8SMaor Gottlieb #define MLX5_FS_MAX_ENTRIES 32000UL 1379038d2ef8SMaor Gottlieb static struct mlx5_ib_flow_prio *get_flow_table(struct mlx5_ib_dev *dev, 1380038d2ef8SMaor Gottlieb struct ib_flow_attr *flow_attr) 1381038d2ef8SMaor Gottlieb { 1382038d2ef8SMaor Gottlieb struct mlx5_flow_namespace *ns = NULL; 1383038d2ef8SMaor Gottlieb struct mlx5_ib_flow_prio *prio; 1384038d2ef8SMaor Gottlieb struct mlx5_flow_table *ft; 1385038d2ef8SMaor Gottlieb int num_entries; 1386038d2ef8SMaor Gottlieb int num_groups; 1387038d2ef8SMaor Gottlieb int priority; 1388038d2ef8SMaor Gottlieb int err = 0; 1389038d2ef8SMaor Gottlieb 1390038d2ef8SMaor Gottlieb if (flow_attr->type == IB_FLOW_ATTR_NORMAL) { 1391038d2ef8SMaor Gottlieb if (flow_is_multicast_only(flow_attr)) 1392038d2ef8SMaor Gottlieb priority = MLX5_IB_FLOW_MCAST_PRIO; 1393038d2ef8SMaor Gottlieb else 1394038d2ef8SMaor Gottlieb priority = flow_attr->priority; 1395038d2ef8SMaor Gottlieb ns = mlx5_get_flow_namespace(dev->mdev, 1396038d2ef8SMaor Gottlieb MLX5_FLOW_NAMESPACE_BYPASS); 1397038d2ef8SMaor Gottlieb num_entries = MLX5_FS_MAX_ENTRIES; 1398038d2ef8SMaor Gottlieb num_groups = MLX5_FS_MAX_TYPES; 1399038d2ef8SMaor Gottlieb prio = &dev->flow_db.prios[priority]; 1400038d2ef8SMaor Gottlieb } else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT || 1401038d2ef8SMaor Gottlieb flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) { 1402038d2ef8SMaor Gottlieb ns = mlx5_get_flow_namespace(dev->mdev, 1403038d2ef8SMaor Gottlieb MLX5_FLOW_NAMESPACE_LEFTOVERS); 1404038d2ef8SMaor Gottlieb build_leftovers_ft_param(&priority, 1405038d2ef8SMaor Gottlieb &num_entries, 1406038d2ef8SMaor Gottlieb &num_groups); 1407038d2ef8SMaor Gottlieb prio = &dev->flow_db.prios[MLX5_IB_FLOW_LEFTOVERS_PRIO]; 1408038d2ef8SMaor Gottlieb } 1409038d2ef8SMaor Gottlieb 1410038d2ef8SMaor Gottlieb if (!ns) 1411038d2ef8SMaor Gottlieb return ERR_PTR(-ENOTSUPP); 1412038d2ef8SMaor Gottlieb 1413038d2ef8SMaor Gottlieb ft = prio->flow_table; 1414038d2ef8SMaor Gottlieb if (!ft) { 1415038d2ef8SMaor Gottlieb ft = mlx5_create_auto_grouped_flow_table(ns, priority, 1416038d2ef8SMaor Gottlieb num_entries, 1417038d2ef8SMaor Gottlieb num_groups); 1418038d2ef8SMaor Gottlieb 1419038d2ef8SMaor Gottlieb if (!IS_ERR(ft)) { 1420038d2ef8SMaor Gottlieb prio->refcount = 0; 1421038d2ef8SMaor Gottlieb prio->flow_table = ft; 1422038d2ef8SMaor Gottlieb } else { 1423038d2ef8SMaor Gottlieb err = PTR_ERR(ft); 1424038d2ef8SMaor Gottlieb } 1425038d2ef8SMaor Gottlieb } 1426038d2ef8SMaor Gottlieb 1427038d2ef8SMaor Gottlieb return err ? ERR_PTR(err) : prio; 1428038d2ef8SMaor Gottlieb } 1429038d2ef8SMaor Gottlieb 1430038d2ef8SMaor Gottlieb static struct mlx5_ib_flow_handler *create_flow_rule(struct mlx5_ib_dev *dev, 1431038d2ef8SMaor Gottlieb struct mlx5_ib_flow_prio *ft_prio, 1432038d2ef8SMaor Gottlieb struct ib_flow_attr *flow_attr, 1433038d2ef8SMaor Gottlieb struct mlx5_flow_destination *dst) 1434038d2ef8SMaor Gottlieb { 1435038d2ef8SMaor Gottlieb struct mlx5_flow_table *ft = ft_prio->flow_table; 1436038d2ef8SMaor Gottlieb struct mlx5_ib_flow_handler *handler; 1437038d2ef8SMaor Gottlieb void *ib_flow = flow_attr + 1; 1438038d2ef8SMaor Gottlieb u8 match_criteria_enable = 0; 1439038d2ef8SMaor Gottlieb unsigned int spec_index; 1440038d2ef8SMaor Gottlieb u32 *match_c; 1441038d2ef8SMaor Gottlieb u32 *match_v; 1442038d2ef8SMaor Gottlieb int err = 0; 1443038d2ef8SMaor Gottlieb 1444038d2ef8SMaor Gottlieb if (!is_valid_attr(flow_attr)) 1445038d2ef8SMaor Gottlieb return ERR_PTR(-EINVAL); 1446038d2ef8SMaor Gottlieb 1447038d2ef8SMaor Gottlieb match_c = kzalloc(MLX5_ST_SZ_BYTES(fte_match_param), GFP_KERNEL); 1448038d2ef8SMaor Gottlieb match_v = kzalloc(MLX5_ST_SZ_BYTES(fte_match_param), GFP_KERNEL); 1449038d2ef8SMaor Gottlieb handler = kzalloc(sizeof(*handler), GFP_KERNEL); 1450038d2ef8SMaor Gottlieb if (!handler || !match_c || !match_v) { 1451038d2ef8SMaor Gottlieb err = -ENOMEM; 1452038d2ef8SMaor Gottlieb goto free; 1453038d2ef8SMaor Gottlieb } 1454038d2ef8SMaor Gottlieb 1455038d2ef8SMaor Gottlieb INIT_LIST_HEAD(&handler->list); 1456038d2ef8SMaor Gottlieb 1457038d2ef8SMaor Gottlieb for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) { 1458038d2ef8SMaor Gottlieb err = parse_flow_attr(match_c, match_v, ib_flow); 1459038d2ef8SMaor Gottlieb if (err < 0) 1460038d2ef8SMaor Gottlieb goto free; 1461038d2ef8SMaor Gottlieb 1462038d2ef8SMaor Gottlieb ib_flow += ((union ib_flow_spec *)ib_flow)->size; 1463038d2ef8SMaor Gottlieb } 1464038d2ef8SMaor Gottlieb 1465038d2ef8SMaor Gottlieb /* Outer header support only */ 1466038d2ef8SMaor Gottlieb match_criteria_enable = (!outer_header_zero(match_c)) << 0; 1467038d2ef8SMaor Gottlieb handler->rule = mlx5_add_flow_rule(ft, match_criteria_enable, 1468038d2ef8SMaor Gottlieb match_c, match_v, 1469038d2ef8SMaor Gottlieb MLX5_FLOW_CONTEXT_ACTION_FWD_DEST, 1470038d2ef8SMaor Gottlieb MLX5_FS_DEFAULT_FLOW_TAG, 1471038d2ef8SMaor Gottlieb dst); 1472038d2ef8SMaor Gottlieb 1473038d2ef8SMaor Gottlieb if (IS_ERR(handler->rule)) { 1474038d2ef8SMaor Gottlieb err = PTR_ERR(handler->rule); 1475038d2ef8SMaor Gottlieb goto free; 1476038d2ef8SMaor Gottlieb } 1477038d2ef8SMaor Gottlieb 1478038d2ef8SMaor Gottlieb handler->prio = ft_prio - dev->flow_db.prios; 1479038d2ef8SMaor Gottlieb 1480038d2ef8SMaor Gottlieb ft_prio->flow_table = ft; 1481038d2ef8SMaor Gottlieb free: 1482038d2ef8SMaor Gottlieb if (err) 1483038d2ef8SMaor Gottlieb kfree(handler); 1484038d2ef8SMaor Gottlieb kfree(match_c); 1485038d2ef8SMaor Gottlieb kfree(match_v); 1486038d2ef8SMaor Gottlieb return err ? ERR_PTR(err) : handler; 1487038d2ef8SMaor Gottlieb } 1488038d2ef8SMaor Gottlieb 1489038d2ef8SMaor Gottlieb enum { 1490038d2ef8SMaor Gottlieb LEFTOVERS_MC, 1491038d2ef8SMaor Gottlieb LEFTOVERS_UC, 1492038d2ef8SMaor Gottlieb }; 1493038d2ef8SMaor Gottlieb 1494038d2ef8SMaor Gottlieb static struct mlx5_ib_flow_handler *create_leftovers_rule(struct mlx5_ib_dev *dev, 1495038d2ef8SMaor Gottlieb struct mlx5_ib_flow_prio *ft_prio, 1496038d2ef8SMaor Gottlieb struct ib_flow_attr *flow_attr, 1497038d2ef8SMaor Gottlieb struct mlx5_flow_destination *dst) 1498038d2ef8SMaor Gottlieb { 1499038d2ef8SMaor Gottlieb struct mlx5_ib_flow_handler *handler_ucast = NULL; 1500038d2ef8SMaor Gottlieb struct mlx5_ib_flow_handler *handler = NULL; 1501038d2ef8SMaor Gottlieb 1502038d2ef8SMaor Gottlieb static struct { 1503038d2ef8SMaor Gottlieb struct ib_flow_attr flow_attr; 1504038d2ef8SMaor Gottlieb struct ib_flow_spec_eth eth_flow; 1505038d2ef8SMaor Gottlieb } leftovers_specs[] = { 1506038d2ef8SMaor Gottlieb [LEFTOVERS_MC] = { 1507038d2ef8SMaor Gottlieb .flow_attr = { 1508038d2ef8SMaor Gottlieb .num_of_specs = 1, 1509038d2ef8SMaor Gottlieb .size = sizeof(leftovers_specs[0]) 1510038d2ef8SMaor Gottlieb }, 1511038d2ef8SMaor Gottlieb .eth_flow = { 1512038d2ef8SMaor Gottlieb .type = IB_FLOW_SPEC_ETH, 1513038d2ef8SMaor Gottlieb .size = sizeof(struct ib_flow_spec_eth), 1514038d2ef8SMaor Gottlieb .mask = {.dst_mac = {0x1} }, 1515038d2ef8SMaor Gottlieb .val = {.dst_mac = {0x1} } 1516038d2ef8SMaor Gottlieb } 1517038d2ef8SMaor Gottlieb }, 1518038d2ef8SMaor Gottlieb [LEFTOVERS_UC] = { 1519038d2ef8SMaor Gottlieb .flow_attr = { 1520038d2ef8SMaor Gottlieb .num_of_specs = 1, 1521038d2ef8SMaor Gottlieb .size = sizeof(leftovers_specs[0]) 1522038d2ef8SMaor Gottlieb }, 1523038d2ef8SMaor Gottlieb .eth_flow = { 1524038d2ef8SMaor Gottlieb .type = IB_FLOW_SPEC_ETH, 1525038d2ef8SMaor Gottlieb .size = sizeof(struct ib_flow_spec_eth), 1526038d2ef8SMaor Gottlieb .mask = {.dst_mac = {0x1} }, 1527038d2ef8SMaor Gottlieb .val = {.dst_mac = {} } 1528038d2ef8SMaor Gottlieb } 1529038d2ef8SMaor Gottlieb } 1530038d2ef8SMaor Gottlieb }; 1531038d2ef8SMaor Gottlieb 1532038d2ef8SMaor Gottlieb handler = create_flow_rule(dev, ft_prio, 1533038d2ef8SMaor Gottlieb &leftovers_specs[LEFTOVERS_MC].flow_attr, 1534038d2ef8SMaor Gottlieb dst); 1535038d2ef8SMaor Gottlieb if (!IS_ERR(handler) && 1536038d2ef8SMaor Gottlieb flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT) { 1537038d2ef8SMaor Gottlieb handler_ucast = create_flow_rule(dev, ft_prio, 1538038d2ef8SMaor Gottlieb &leftovers_specs[LEFTOVERS_UC].flow_attr, 1539038d2ef8SMaor Gottlieb dst); 1540038d2ef8SMaor Gottlieb if (IS_ERR(handler_ucast)) { 1541038d2ef8SMaor Gottlieb kfree(handler); 1542038d2ef8SMaor Gottlieb handler = handler_ucast; 1543038d2ef8SMaor Gottlieb } else { 1544038d2ef8SMaor Gottlieb list_add(&handler_ucast->list, &handler->list); 1545038d2ef8SMaor Gottlieb } 1546038d2ef8SMaor Gottlieb } 1547038d2ef8SMaor Gottlieb 1548038d2ef8SMaor Gottlieb return handler; 1549038d2ef8SMaor Gottlieb } 1550038d2ef8SMaor Gottlieb 1551038d2ef8SMaor Gottlieb static struct ib_flow *mlx5_ib_create_flow(struct ib_qp *qp, 1552038d2ef8SMaor Gottlieb struct ib_flow_attr *flow_attr, 1553038d2ef8SMaor Gottlieb int domain) 1554038d2ef8SMaor Gottlieb { 1555038d2ef8SMaor Gottlieb struct mlx5_ib_dev *dev = to_mdev(qp->device); 1556038d2ef8SMaor Gottlieb struct mlx5_ib_flow_handler *handler = NULL; 1557038d2ef8SMaor Gottlieb struct mlx5_flow_destination *dst = NULL; 1558038d2ef8SMaor Gottlieb struct mlx5_ib_flow_prio *ft_prio; 1559038d2ef8SMaor Gottlieb int err; 1560038d2ef8SMaor Gottlieb 1561038d2ef8SMaor Gottlieb if (flow_attr->priority > MLX5_IB_FLOW_LAST_PRIO) 1562038d2ef8SMaor Gottlieb return ERR_PTR(-ENOSPC); 1563038d2ef8SMaor Gottlieb 1564038d2ef8SMaor Gottlieb if (domain != IB_FLOW_DOMAIN_USER || 1565038d2ef8SMaor Gottlieb flow_attr->port > MLX5_CAP_GEN(dev->mdev, num_ports) || 1566038d2ef8SMaor Gottlieb flow_attr->flags) 1567038d2ef8SMaor Gottlieb return ERR_PTR(-EINVAL); 1568038d2ef8SMaor Gottlieb 1569038d2ef8SMaor Gottlieb dst = kzalloc(sizeof(*dst), GFP_KERNEL); 1570038d2ef8SMaor Gottlieb if (!dst) 1571038d2ef8SMaor Gottlieb return ERR_PTR(-ENOMEM); 1572038d2ef8SMaor Gottlieb 1573038d2ef8SMaor Gottlieb mutex_lock(&dev->flow_db.lock); 1574038d2ef8SMaor Gottlieb 1575038d2ef8SMaor Gottlieb ft_prio = get_flow_table(dev, flow_attr); 1576038d2ef8SMaor Gottlieb if (IS_ERR(ft_prio)) { 1577038d2ef8SMaor Gottlieb err = PTR_ERR(ft_prio); 1578038d2ef8SMaor Gottlieb goto unlock; 1579038d2ef8SMaor Gottlieb } 1580038d2ef8SMaor Gottlieb 1581038d2ef8SMaor Gottlieb dst->type = MLX5_FLOW_DESTINATION_TYPE_TIR; 1582038d2ef8SMaor Gottlieb dst->tir_num = to_mqp(qp)->raw_packet_qp.rq.tirn; 1583038d2ef8SMaor Gottlieb 1584038d2ef8SMaor Gottlieb if (flow_attr->type == IB_FLOW_ATTR_NORMAL) { 1585038d2ef8SMaor Gottlieb handler = create_flow_rule(dev, ft_prio, flow_attr, 1586038d2ef8SMaor Gottlieb dst); 1587038d2ef8SMaor Gottlieb } else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT || 1588038d2ef8SMaor Gottlieb flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) { 1589038d2ef8SMaor Gottlieb handler = create_leftovers_rule(dev, ft_prio, flow_attr, 1590038d2ef8SMaor Gottlieb dst); 1591038d2ef8SMaor Gottlieb } else { 1592038d2ef8SMaor Gottlieb err = -EINVAL; 1593038d2ef8SMaor Gottlieb goto destroy_ft; 1594038d2ef8SMaor Gottlieb } 1595038d2ef8SMaor Gottlieb 1596038d2ef8SMaor Gottlieb if (IS_ERR(handler)) { 1597038d2ef8SMaor Gottlieb err = PTR_ERR(handler); 1598038d2ef8SMaor Gottlieb handler = NULL; 1599038d2ef8SMaor Gottlieb goto destroy_ft; 1600038d2ef8SMaor Gottlieb } 1601038d2ef8SMaor Gottlieb 1602038d2ef8SMaor Gottlieb ft_prio->refcount++; 1603038d2ef8SMaor Gottlieb mutex_unlock(&dev->flow_db.lock); 1604038d2ef8SMaor Gottlieb kfree(dst); 1605038d2ef8SMaor Gottlieb 1606038d2ef8SMaor Gottlieb return &handler->ibflow; 1607038d2ef8SMaor Gottlieb 1608038d2ef8SMaor Gottlieb destroy_ft: 1609038d2ef8SMaor Gottlieb put_flow_table(dev, ft_prio, false); 1610038d2ef8SMaor Gottlieb unlock: 1611038d2ef8SMaor Gottlieb mutex_unlock(&dev->flow_db.lock); 1612038d2ef8SMaor Gottlieb kfree(dst); 1613038d2ef8SMaor Gottlieb kfree(handler); 1614038d2ef8SMaor Gottlieb return ERR_PTR(err); 1615038d2ef8SMaor Gottlieb } 1616038d2ef8SMaor Gottlieb 1617e126ba97SEli Cohen static int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid) 1618e126ba97SEli Cohen { 1619e126ba97SEli Cohen struct mlx5_ib_dev *dev = to_mdev(ibqp->device); 1620e126ba97SEli Cohen int err; 1621e126ba97SEli Cohen 16229603b61dSJack Morgenstein err = mlx5_core_attach_mcg(dev->mdev, gid, ibqp->qp_num); 1623e126ba97SEli Cohen if (err) 1624e126ba97SEli Cohen mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n", 1625e126ba97SEli Cohen ibqp->qp_num, gid->raw); 1626e126ba97SEli Cohen 1627e126ba97SEli Cohen return err; 1628e126ba97SEli Cohen } 1629e126ba97SEli Cohen 1630e126ba97SEli Cohen static int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid) 1631e126ba97SEli Cohen { 1632e126ba97SEli Cohen struct mlx5_ib_dev *dev = to_mdev(ibqp->device); 1633e126ba97SEli Cohen int err; 1634e126ba97SEli Cohen 16359603b61dSJack Morgenstein err = mlx5_core_detach_mcg(dev->mdev, gid, ibqp->qp_num); 1636e126ba97SEli Cohen if (err) 1637e126ba97SEli Cohen mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n", 1638e126ba97SEli Cohen ibqp->qp_num, gid->raw); 1639e126ba97SEli Cohen 1640e126ba97SEli Cohen return err; 1641e126ba97SEli Cohen } 1642e126ba97SEli Cohen 1643e126ba97SEli Cohen static int init_node_data(struct mlx5_ib_dev *dev) 1644e126ba97SEli Cohen { 16451b5daf11SMajd Dibbiny int err; 1646e126ba97SEli Cohen 16471b5daf11SMajd Dibbiny err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc); 1648e126ba97SEli Cohen if (err) 1649e126ba97SEli Cohen return err; 16501b5daf11SMajd Dibbiny 16511b5daf11SMajd Dibbiny dev->mdev->rev_id = dev->mdev->pdev->revision; 16521b5daf11SMajd Dibbiny 16531b5daf11SMajd Dibbiny return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid); 1654e126ba97SEli Cohen } 1655e126ba97SEli Cohen 1656e126ba97SEli Cohen static ssize_t show_fw_pages(struct device *device, struct device_attribute *attr, 1657e126ba97SEli Cohen char *buf) 1658e126ba97SEli Cohen { 1659e126ba97SEli Cohen struct mlx5_ib_dev *dev = 1660e126ba97SEli Cohen container_of(device, struct mlx5_ib_dev, ib_dev.dev); 1661e126ba97SEli Cohen 16629603b61dSJack Morgenstein return sprintf(buf, "%d\n", dev->mdev->priv.fw_pages); 1663e126ba97SEli Cohen } 1664e126ba97SEli Cohen 1665e126ba97SEli Cohen static ssize_t show_reg_pages(struct device *device, 1666e126ba97SEli Cohen struct device_attribute *attr, char *buf) 1667e126ba97SEli Cohen { 1668e126ba97SEli Cohen struct mlx5_ib_dev *dev = 1669e126ba97SEli Cohen container_of(device, struct mlx5_ib_dev, ib_dev.dev); 1670e126ba97SEli Cohen 16716aec21f6SHaggai Eran return sprintf(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages)); 1672e126ba97SEli Cohen } 1673e126ba97SEli Cohen 1674e126ba97SEli Cohen static ssize_t show_hca(struct device *device, struct device_attribute *attr, 1675e126ba97SEli Cohen char *buf) 1676e126ba97SEli Cohen { 1677e126ba97SEli Cohen struct mlx5_ib_dev *dev = 1678e126ba97SEli Cohen container_of(device, struct mlx5_ib_dev, ib_dev.dev); 16799603b61dSJack Morgenstein return sprintf(buf, "MT%d\n", dev->mdev->pdev->device); 1680e126ba97SEli Cohen } 1681e126ba97SEli Cohen 1682e126ba97SEli Cohen static ssize_t show_fw_ver(struct device *device, struct device_attribute *attr, 1683e126ba97SEli Cohen char *buf) 1684e126ba97SEli Cohen { 1685e126ba97SEli Cohen struct mlx5_ib_dev *dev = 1686e126ba97SEli Cohen container_of(device, struct mlx5_ib_dev, ib_dev.dev); 16879603b61dSJack Morgenstein return sprintf(buf, "%d.%d.%d\n", fw_rev_maj(dev->mdev), 16889603b61dSJack Morgenstein fw_rev_min(dev->mdev), fw_rev_sub(dev->mdev)); 1689e126ba97SEli Cohen } 1690e126ba97SEli Cohen 1691e126ba97SEli Cohen static ssize_t show_rev(struct device *device, struct device_attribute *attr, 1692e126ba97SEli Cohen char *buf) 1693e126ba97SEli Cohen { 1694e126ba97SEli Cohen struct mlx5_ib_dev *dev = 1695e126ba97SEli Cohen container_of(device, struct mlx5_ib_dev, ib_dev.dev); 16969603b61dSJack Morgenstein return sprintf(buf, "%x\n", dev->mdev->rev_id); 1697e126ba97SEli Cohen } 1698e126ba97SEli Cohen 1699e126ba97SEli Cohen static ssize_t show_board(struct device *device, struct device_attribute *attr, 1700e126ba97SEli Cohen char *buf) 1701e126ba97SEli Cohen { 1702e126ba97SEli Cohen struct mlx5_ib_dev *dev = 1703e126ba97SEli Cohen container_of(device, struct mlx5_ib_dev, ib_dev.dev); 1704e126ba97SEli Cohen return sprintf(buf, "%.*s\n", MLX5_BOARD_ID_LEN, 17059603b61dSJack Morgenstein dev->mdev->board_id); 1706e126ba97SEli Cohen } 1707e126ba97SEli Cohen 1708e126ba97SEli Cohen static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL); 1709e126ba97SEli Cohen static DEVICE_ATTR(fw_ver, S_IRUGO, show_fw_ver, NULL); 1710e126ba97SEli Cohen static DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL); 1711e126ba97SEli Cohen static DEVICE_ATTR(board_id, S_IRUGO, show_board, NULL); 1712e126ba97SEli Cohen static DEVICE_ATTR(fw_pages, S_IRUGO, show_fw_pages, NULL); 1713e126ba97SEli Cohen static DEVICE_ATTR(reg_pages, S_IRUGO, show_reg_pages, NULL); 1714e126ba97SEli Cohen 1715e126ba97SEli Cohen static struct device_attribute *mlx5_class_attributes[] = { 1716e126ba97SEli Cohen &dev_attr_hw_rev, 1717e126ba97SEli Cohen &dev_attr_fw_ver, 1718e126ba97SEli Cohen &dev_attr_hca_type, 1719e126ba97SEli Cohen &dev_attr_board_id, 1720e126ba97SEli Cohen &dev_attr_fw_pages, 1721e126ba97SEli Cohen &dev_attr_reg_pages, 1722e126ba97SEli Cohen }; 1723e126ba97SEli Cohen 17247722f47eSHaggai Eran static void pkey_change_handler(struct work_struct *work) 17257722f47eSHaggai Eran { 17267722f47eSHaggai Eran struct mlx5_ib_port_resources *ports = 17277722f47eSHaggai Eran container_of(work, struct mlx5_ib_port_resources, 17287722f47eSHaggai Eran pkey_change_work); 17297722f47eSHaggai Eran 17307722f47eSHaggai Eran mutex_lock(&ports->devr->mutex); 17317722f47eSHaggai Eran mlx5_ib_gsi_pkey_change(ports->gsi); 17327722f47eSHaggai Eran mutex_unlock(&ports->devr->mutex); 17337722f47eSHaggai Eran } 17347722f47eSHaggai Eran 17359603b61dSJack Morgenstein static void mlx5_ib_event(struct mlx5_core_dev *dev, void *context, 17364d2f9bbbSJack Morgenstein enum mlx5_dev_event event, unsigned long param) 1737e126ba97SEli Cohen { 17389603b61dSJack Morgenstein struct mlx5_ib_dev *ibdev = (struct mlx5_ib_dev *)context; 1739e126ba97SEli Cohen struct ib_event ibev; 17409603b61dSJack Morgenstein 1741e126ba97SEli Cohen u8 port = 0; 1742e126ba97SEli Cohen 1743e126ba97SEli Cohen switch (event) { 1744e126ba97SEli Cohen case MLX5_DEV_EVENT_SYS_ERROR: 1745e126ba97SEli Cohen ibdev->ib_active = false; 1746e126ba97SEli Cohen ibev.event = IB_EVENT_DEVICE_FATAL; 1747e126ba97SEli Cohen break; 1748e126ba97SEli Cohen 1749e126ba97SEli Cohen case MLX5_DEV_EVENT_PORT_UP: 1750e126ba97SEli Cohen ibev.event = IB_EVENT_PORT_ACTIVE; 17514d2f9bbbSJack Morgenstein port = (u8)param; 1752e126ba97SEli Cohen break; 1753e126ba97SEli Cohen 1754e126ba97SEli Cohen case MLX5_DEV_EVENT_PORT_DOWN: 1755e126ba97SEli Cohen ibev.event = IB_EVENT_PORT_ERR; 17564d2f9bbbSJack Morgenstein port = (u8)param; 1757e126ba97SEli Cohen break; 1758e126ba97SEli Cohen 1759e126ba97SEli Cohen case MLX5_DEV_EVENT_PORT_INITIALIZED: 1760e126ba97SEli Cohen /* not used by ULPs */ 1761e126ba97SEli Cohen return; 1762e126ba97SEli Cohen 1763e126ba97SEli Cohen case MLX5_DEV_EVENT_LID_CHANGE: 1764e126ba97SEli Cohen ibev.event = IB_EVENT_LID_CHANGE; 17654d2f9bbbSJack Morgenstein port = (u8)param; 1766e126ba97SEli Cohen break; 1767e126ba97SEli Cohen 1768e126ba97SEli Cohen case MLX5_DEV_EVENT_PKEY_CHANGE: 1769e126ba97SEli Cohen ibev.event = IB_EVENT_PKEY_CHANGE; 17704d2f9bbbSJack Morgenstein port = (u8)param; 17717722f47eSHaggai Eran 17727722f47eSHaggai Eran schedule_work(&ibdev->devr.ports[port - 1].pkey_change_work); 1773e126ba97SEli Cohen break; 1774e126ba97SEli Cohen 1775e126ba97SEli Cohen case MLX5_DEV_EVENT_GUID_CHANGE: 1776e126ba97SEli Cohen ibev.event = IB_EVENT_GID_CHANGE; 17774d2f9bbbSJack Morgenstein port = (u8)param; 1778e126ba97SEli Cohen break; 1779e126ba97SEli Cohen 1780e126ba97SEli Cohen case MLX5_DEV_EVENT_CLIENT_REREG: 1781e126ba97SEli Cohen ibev.event = IB_EVENT_CLIENT_REREGISTER; 17824d2f9bbbSJack Morgenstein port = (u8)param; 1783e126ba97SEli Cohen break; 1784e126ba97SEli Cohen } 1785e126ba97SEli Cohen 1786e126ba97SEli Cohen ibev.device = &ibdev->ib_dev; 1787e126ba97SEli Cohen ibev.element.port_num = port; 1788e126ba97SEli Cohen 1789a0c84c32SEli Cohen if (port < 1 || port > ibdev->num_ports) { 1790a0c84c32SEli Cohen mlx5_ib_warn(ibdev, "warning: event on port %d\n", port); 1791a0c84c32SEli Cohen return; 1792a0c84c32SEli Cohen } 1793a0c84c32SEli Cohen 1794e126ba97SEli Cohen if (ibdev->ib_active) 1795e126ba97SEli Cohen ib_dispatch_event(&ibev); 1796e126ba97SEli Cohen } 1797e126ba97SEli Cohen 1798e126ba97SEli Cohen static void get_ext_port_caps(struct mlx5_ib_dev *dev) 1799e126ba97SEli Cohen { 1800e126ba97SEli Cohen int port; 1801e126ba97SEli Cohen 1802938fe83cSSaeed Mahameed for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++) 1803e126ba97SEli Cohen mlx5_query_ext_port_caps(dev, port); 1804e126ba97SEli Cohen } 1805e126ba97SEli Cohen 1806e126ba97SEli Cohen static int get_port_caps(struct mlx5_ib_dev *dev) 1807e126ba97SEli Cohen { 1808e126ba97SEli Cohen struct ib_device_attr *dprops = NULL; 1809e126ba97SEli Cohen struct ib_port_attr *pprops = NULL; 1810f614fc15SDan Carpenter int err = -ENOMEM; 1811e126ba97SEli Cohen int port; 18122528e33eSMatan Barak struct ib_udata uhw = {.inlen = 0, .outlen = 0}; 1813e126ba97SEli Cohen 1814e126ba97SEli Cohen pprops = kmalloc(sizeof(*pprops), GFP_KERNEL); 1815e126ba97SEli Cohen if (!pprops) 1816e126ba97SEli Cohen goto out; 1817e126ba97SEli Cohen 1818e126ba97SEli Cohen dprops = kmalloc(sizeof(*dprops), GFP_KERNEL); 1819e126ba97SEli Cohen if (!dprops) 1820e126ba97SEli Cohen goto out; 1821e126ba97SEli Cohen 18222528e33eSMatan Barak err = mlx5_ib_query_device(&dev->ib_dev, dprops, &uhw); 1823e126ba97SEli Cohen if (err) { 1824e126ba97SEli Cohen mlx5_ib_warn(dev, "query_device failed %d\n", err); 1825e126ba97SEli Cohen goto out; 1826e126ba97SEli Cohen } 1827e126ba97SEli Cohen 1828938fe83cSSaeed Mahameed for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++) { 1829e126ba97SEli Cohen err = mlx5_ib_query_port(&dev->ib_dev, port, pprops); 1830e126ba97SEli Cohen if (err) { 1831938fe83cSSaeed Mahameed mlx5_ib_warn(dev, "query_port %d failed %d\n", 1832938fe83cSSaeed Mahameed port, err); 1833e126ba97SEli Cohen break; 1834e126ba97SEli Cohen } 1835938fe83cSSaeed Mahameed dev->mdev->port_caps[port - 1].pkey_table_len = 1836938fe83cSSaeed Mahameed dprops->max_pkeys; 1837938fe83cSSaeed Mahameed dev->mdev->port_caps[port - 1].gid_table_len = 1838938fe83cSSaeed Mahameed pprops->gid_tbl_len; 1839e126ba97SEli Cohen mlx5_ib_dbg(dev, "pkey_table_len %d, gid_table_len %d\n", 1840e126ba97SEli Cohen dprops->max_pkeys, pprops->gid_tbl_len); 1841e126ba97SEli Cohen } 1842e126ba97SEli Cohen 1843e126ba97SEli Cohen out: 1844e126ba97SEli Cohen kfree(pprops); 1845e126ba97SEli Cohen kfree(dprops); 1846e126ba97SEli Cohen 1847e126ba97SEli Cohen return err; 1848e126ba97SEli Cohen } 1849e126ba97SEli Cohen 1850e126ba97SEli Cohen static void destroy_umrc_res(struct mlx5_ib_dev *dev) 1851e126ba97SEli Cohen { 1852e126ba97SEli Cohen int err; 1853e126ba97SEli Cohen 1854e126ba97SEli Cohen err = mlx5_mr_cache_cleanup(dev); 1855e126ba97SEli Cohen if (err) 1856e126ba97SEli Cohen mlx5_ib_warn(dev, "mr cache cleanup failed\n"); 1857e126ba97SEli Cohen 1858e126ba97SEli Cohen mlx5_ib_destroy_qp(dev->umrc.qp); 1859e126ba97SEli Cohen ib_destroy_cq(dev->umrc.cq); 1860e126ba97SEli Cohen ib_dealloc_pd(dev->umrc.pd); 1861e126ba97SEli Cohen } 1862e126ba97SEli Cohen 1863e126ba97SEli Cohen enum { 1864e126ba97SEli Cohen MAX_UMR_WR = 128, 1865e126ba97SEli Cohen }; 1866e126ba97SEli Cohen 1867e126ba97SEli Cohen static int create_umr_res(struct mlx5_ib_dev *dev) 1868e126ba97SEli Cohen { 1869e126ba97SEli Cohen struct ib_qp_init_attr *init_attr = NULL; 1870e126ba97SEli Cohen struct ib_qp_attr *attr = NULL; 1871e126ba97SEli Cohen struct ib_pd *pd; 1872e126ba97SEli Cohen struct ib_cq *cq; 1873e126ba97SEli Cohen struct ib_qp *qp; 18748e37210bSMatan Barak struct ib_cq_init_attr cq_attr = {}; 1875e126ba97SEli Cohen int ret; 1876e126ba97SEli Cohen 1877e126ba97SEli Cohen attr = kzalloc(sizeof(*attr), GFP_KERNEL); 1878e126ba97SEli Cohen init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL); 1879e126ba97SEli Cohen if (!attr || !init_attr) { 1880e126ba97SEli Cohen ret = -ENOMEM; 1881e126ba97SEli Cohen goto error_0; 1882e126ba97SEli Cohen } 1883e126ba97SEli Cohen 1884e126ba97SEli Cohen pd = ib_alloc_pd(&dev->ib_dev); 1885e126ba97SEli Cohen if (IS_ERR(pd)) { 1886e126ba97SEli Cohen mlx5_ib_dbg(dev, "Couldn't create PD for sync UMR QP\n"); 1887e126ba97SEli Cohen ret = PTR_ERR(pd); 1888e126ba97SEli Cohen goto error_0; 1889e126ba97SEli Cohen } 1890e126ba97SEli Cohen 18918e37210bSMatan Barak cq_attr.cqe = 128; 18928e37210bSMatan Barak cq = ib_create_cq(&dev->ib_dev, mlx5_umr_cq_handler, NULL, NULL, 18938e37210bSMatan Barak &cq_attr); 1894e126ba97SEli Cohen if (IS_ERR(cq)) { 1895e126ba97SEli Cohen mlx5_ib_dbg(dev, "Couldn't create CQ for sync UMR QP\n"); 1896e126ba97SEli Cohen ret = PTR_ERR(cq); 1897e126ba97SEli Cohen goto error_2; 1898e126ba97SEli Cohen } 1899e126ba97SEli Cohen ib_req_notify_cq(cq, IB_CQ_NEXT_COMP); 1900e126ba97SEli Cohen 1901e126ba97SEli Cohen init_attr->send_cq = cq; 1902e126ba97SEli Cohen init_attr->recv_cq = cq; 1903e126ba97SEli Cohen init_attr->sq_sig_type = IB_SIGNAL_ALL_WR; 1904e126ba97SEli Cohen init_attr->cap.max_send_wr = MAX_UMR_WR; 1905e126ba97SEli Cohen init_attr->cap.max_send_sge = 1; 1906e126ba97SEli Cohen init_attr->qp_type = MLX5_IB_QPT_REG_UMR; 1907e126ba97SEli Cohen init_attr->port_num = 1; 1908e126ba97SEli Cohen qp = mlx5_ib_create_qp(pd, init_attr, NULL); 1909e126ba97SEli Cohen if (IS_ERR(qp)) { 1910e126ba97SEli Cohen mlx5_ib_dbg(dev, "Couldn't create sync UMR QP\n"); 1911e126ba97SEli Cohen ret = PTR_ERR(qp); 1912e126ba97SEli Cohen goto error_3; 1913e126ba97SEli Cohen } 1914e126ba97SEli Cohen qp->device = &dev->ib_dev; 1915e126ba97SEli Cohen qp->real_qp = qp; 1916e126ba97SEli Cohen qp->uobject = NULL; 1917e126ba97SEli Cohen qp->qp_type = MLX5_IB_QPT_REG_UMR; 1918e126ba97SEli Cohen 1919e126ba97SEli Cohen attr->qp_state = IB_QPS_INIT; 1920e126ba97SEli Cohen attr->port_num = 1; 1921e126ba97SEli Cohen ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_PKEY_INDEX | 1922e126ba97SEli Cohen IB_QP_PORT, NULL); 1923e126ba97SEli Cohen if (ret) { 1924e126ba97SEli Cohen mlx5_ib_dbg(dev, "Couldn't modify UMR QP\n"); 1925e126ba97SEli Cohen goto error_4; 1926e126ba97SEli Cohen } 1927e126ba97SEli Cohen 1928e126ba97SEli Cohen memset(attr, 0, sizeof(*attr)); 1929e126ba97SEli Cohen attr->qp_state = IB_QPS_RTR; 1930e126ba97SEli Cohen attr->path_mtu = IB_MTU_256; 1931e126ba97SEli Cohen 1932e126ba97SEli Cohen ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL); 1933e126ba97SEli Cohen if (ret) { 1934e126ba97SEli Cohen mlx5_ib_dbg(dev, "Couldn't modify umr QP to rtr\n"); 1935e126ba97SEli Cohen goto error_4; 1936e126ba97SEli Cohen } 1937e126ba97SEli Cohen 1938e126ba97SEli Cohen memset(attr, 0, sizeof(*attr)); 1939e126ba97SEli Cohen attr->qp_state = IB_QPS_RTS; 1940e126ba97SEli Cohen ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL); 1941e126ba97SEli Cohen if (ret) { 1942e126ba97SEli Cohen mlx5_ib_dbg(dev, "Couldn't modify umr QP to rts\n"); 1943e126ba97SEli Cohen goto error_4; 1944e126ba97SEli Cohen } 1945e126ba97SEli Cohen 1946e126ba97SEli Cohen dev->umrc.qp = qp; 1947e126ba97SEli Cohen dev->umrc.cq = cq; 1948e126ba97SEli Cohen dev->umrc.pd = pd; 1949e126ba97SEli Cohen 1950e126ba97SEli Cohen sema_init(&dev->umrc.sem, MAX_UMR_WR); 1951e126ba97SEli Cohen ret = mlx5_mr_cache_init(dev); 1952e126ba97SEli Cohen if (ret) { 1953e126ba97SEli Cohen mlx5_ib_warn(dev, "mr cache init failed %d\n", ret); 1954e126ba97SEli Cohen goto error_4; 1955e126ba97SEli Cohen } 1956e126ba97SEli Cohen 1957e126ba97SEli Cohen kfree(attr); 1958e126ba97SEli Cohen kfree(init_attr); 1959e126ba97SEli Cohen 1960e126ba97SEli Cohen return 0; 1961e126ba97SEli Cohen 1962e126ba97SEli Cohen error_4: 1963e126ba97SEli Cohen mlx5_ib_destroy_qp(qp); 1964e126ba97SEli Cohen 1965e126ba97SEli Cohen error_3: 1966e126ba97SEli Cohen ib_destroy_cq(cq); 1967e126ba97SEli Cohen 1968e126ba97SEli Cohen error_2: 1969e126ba97SEli Cohen ib_dealloc_pd(pd); 1970e126ba97SEli Cohen 1971e126ba97SEli Cohen error_0: 1972e126ba97SEli Cohen kfree(attr); 1973e126ba97SEli Cohen kfree(init_attr); 1974e126ba97SEli Cohen return ret; 1975e126ba97SEli Cohen } 1976e126ba97SEli Cohen 1977e126ba97SEli Cohen static int create_dev_resources(struct mlx5_ib_resources *devr) 1978e126ba97SEli Cohen { 1979e126ba97SEli Cohen struct ib_srq_init_attr attr; 1980e126ba97SEli Cohen struct mlx5_ib_dev *dev; 1981bcf4c1eaSMatan Barak struct ib_cq_init_attr cq_attr = {.cqe = 1}; 19827722f47eSHaggai Eran int port; 1983e126ba97SEli Cohen int ret = 0; 1984e126ba97SEli Cohen 1985e126ba97SEli Cohen dev = container_of(devr, struct mlx5_ib_dev, devr); 1986e126ba97SEli Cohen 1987d16e91daSHaggai Eran mutex_init(&devr->mutex); 1988d16e91daSHaggai Eran 1989e126ba97SEli Cohen devr->p0 = mlx5_ib_alloc_pd(&dev->ib_dev, NULL, NULL); 1990e126ba97SEli Cohen if (IS_ERR(devr->p0)) { 1991e126ba97SEli Cohen ret = PTR_ERR(devr->p0); 1992e126ba97SEli Cohen goto error0; 1993e126ba97SEli Cohen } 1994e126ba97SEli Cohen devr->p0->device = &dev->ib_dev; 1995e126ba97SEli Cohen devr->p0->uobject = NULL; 1996e126ba97SEli Cohen atomic_set(&devr->p0->usecnt, 0); 1997e126ba97SEli Cohen 1998bcf4c1eaSMatan Barak devr->c0 = mlx5_ib_create_cq(&dev->ib_dev, &cq_attr, NULL, NULL); 1999e126ba97SEli Cohen if (IS_ERR(devr->c0)) { 2000e126ba97SEli Cohen ret = PTR_ERR(devr->c0); 2001e126ba97SEli Cohen goto error1; 2002e126ba97SEli Cohen } 2003e126ba97SEli Cohen devr->c0->device = &dev->ib_dev; 2004e126ba97SEli Cohen devr->c0->uobject = NULL; 2005e126ba97SEli Cohen devr->c0->comp_handler = NULL; 2006e126ba97SEli Cohen devr->c0->event_handler = NULL; 2007e126ba97SEli Cohen devr->c0->cq_context = NULL; 2008e126ba97SEli Cohen atomic_set(&devr->c0->usecnt, 0); 2009e126ba97SEli Cohen 2010e126ba97SEli Cohen devr->x0 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL); 2011e126ba97SEli Cohen if (IS_ERR(devr->x0)) { 2012e126ba97SEli Cohen ret = PTR_ERR(devr->x0); 2013e126ba97SEli Cohen goto error2; 2014e126ba97SEli Cohen } 2015e126ba97SEli Cohen devr->x0->device = &dev->ib_dev; 2016e126ba97SEli Cohen devr->x0->inode = NULL; 2017e126ba97SEli Cohen atomic_set(&devr->x0->usecnt, 0); 2018e126ba97SEli Cohen mutex_init(&devr->x0->tgt_qp_mutex); 2019e126ba97SEli Cohen INIT_LIST_HEAD(&devr->x0->tgt_qp_list); 2020e126ba97SEli Cohen 2021e126ba97SEli Cohen devr->x1 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL); 2022e126ba97SEli Cohen if (IS_ERR(devr->x1)) { 2023e126ba97SEli Cohen ret = PTR_ERR(devr->x1); 2024e126ba97SEli Cohen goto error3; 2025e126ba97SEli Cohen } 2026e126ba97SEli Cohen devr->x1->device = &dev->ib_dev; 2027e126ba97SEli Cohen devr->x1->inode = NULL; 2028e126ba97SEli Cohen atomic_set(&devr->x1->usecnt, 0); 2029e126ba97SEli Cohen mutex_init(&devr->x1->tgt_qp_mutex); 2030e126ba97SEli Cohen INIT_LIST_HEAD(&devr->x1->tgt_qp_list); 2031e126ba97SEli Cohen 2032e126ba97SEli Cohen memset(&attr, 0, sizeof(attr)); 2033e126ba97SEli Cohen attr.attr.max_sge = 1; 2034e126ba97SEli Cohen attr.attr.max_wr = 1; 2035e126ba97SEli Cohen attr.srq_type = IB_SRQT_XRC; 2036e126ba97SEli Cohen attr.ext.xrc.cq = devr->c0; 2037e126ba97SEli Cohen attr.ext.xrc.xrcd = devr->x0; 2038e126ba97SEli Cohen 2039e126ba97SEli Cohen devr->s0 = mlx5_ib_create_srq(devr->p0, &attr, NULL); 2040e126ba97SEli Cohen if (IS_ERR(devr->s0)) { 2041e126ba97SEli Cohen ret = PTR_ERR(devr->s0); 2042e126ba97SEli Cohen goto error4; 2043e126ba97SEli Cohen } 2044e126ba97SEli Cohen devr->s0->device = &dev->ib_dev; 2045e126ba97SEli Cohen devr->s0->pd = devr->p0; 2046e126ba97SEli Cohen devr->s0->uobject = NULL; 2047e126ba97SEli Cohen devr->s0->event_handler = NULL; 2048e126ba97SEli Cohen devr->s0->srq_context = NULL; 2049e126ba97SEli Cohen devr->s0->srq_type = IB_SRQT_XRC; 2050e126ba97SEli Cohen devr->s0->ext.xrc.xrcd = devr->x0; 2051e126ba97SEli Cohen devr->s0->ext.xrc.cq = devr->c0; 2052e126ba97SEli Cohen atomic_inc(&devr->s0->ext.xrc.xrcd->usecnt); 2053e126ba97SEli Cohen atomic_inc(&devr->s0->ext.xrc.cq->usecnt); 2054e126ba97SEli Cohen atomic_inc(&devr->p0->usecnt); 2055e126ba97SEli Cohen atomic_set(&devr->s0->usecnt, 0); 2056e126ba97SEli Cohen 20574aa17b28SHaggai Abramonvsky memset(&attr, 0, sizeof(attr)); 20584aa17b28SHaggai Abramonvsky attr.attr.max_sge = 1; 20594aa17b28SHaggai Abramonvsky attr.attr.max_wr = 1; 20604aa17b28SHaggai Abramonvsky attr.srq_type = IB_SRQT_BASIC; 20614aa17b28SHaggai Abramonvsky devr->s1 = mlx5_ib_create_srq(devr->p0, &attr, NULL); 20624aa17b28SHaggai Abramonvsky if (IS_ERR(devr->s1)) { 20634aa17b28SHaggai Abramonvsky ret = PTR_ERR(devr->s1); 20644aa17b28SHaggai Abramonvsky goto error5; 20654aa17b28SHaggai Abramonvsky } 20664aa17b28SHaggai Abramonvsky devr->s1->device = &dev->ib_dev; 20674aa17b28SHaggai Abramonvsky devr->s1->pd = devr->p0; 20684aa17b28SHaggai Abramonvsky devr->s1->uobject = NULL; 20694aa17b28SHaggai Abramonvsky devr->s1->event_handler = NULL; 20704aa17b28SHaggai Abramonvsky devr->s1->srq_context = NULL; 20714aa17b28SHaggai Abramonvsky devr->s1->srq_type = IB_SRQT_BASIC; 20724aa17b28SHaggai Abramonvsky devr->s1->ext.xrc.cq = devr->c0; 20734aa17b28SHaggai Abramonvsky atomic_inc(&devr->p0->usecnt); 20744aa17b28SHaggai Abramonvsky atomic_set(&devr->s0->usecnt, 0); 20754aa17b28SHaggai Abramonvsky 20767722f47eSHaggai Eran for (port = 0; port < ARRAY_SIZE(devr->ports); ++port) { 20777722f47eSHaggai Eran INIT_WORK(&devr->ports[port].pkey_change_work, 20787722f47eSHaggai Eran pkey_change_handler); 20797722f47eSHaggai Eran devr->ports[port].devr = devr; 20807722f47eSHaggai Eran } 20817722f47eSHaggai Eran 2082e126ba97SEli Cohen return 0; 2083e126ba97SEli Cohen 20844aa17b28SHaggai Abramonvsky error5: 20854aa17b28SHaggai Abramonvsky mlx5_ib_destroy_srq(devr->s0); 2086e126ba97SEli Cohen error4: 2087e126ba97SEli Cohen mlx5_ib_dealloc_xrcd(devr->x1); 2088e126ba97SEli Cohen error3: 2089e126ba97SEli Cohen mlx5_ib_dealloc_xrcd(devr->x0); 2090e126ba97SEli Cohen error2: 2091e126ba97SEli Cohen mlx5_ib_destroy_cq(devr->c0); 2092e126ba97SEli Cohen error1: 2093e126ba97SEli Cohen mlx5_ib_dealloc_pd(devr->p0); 2094e126ba97SEli Cohen error0: 2095e126ba97SEli Cohen return ret; 2096e126ba97SEli Cohen } 2097e126ba97SEli Cohen 2098e126ba97SEli Cohen static void destroy_dev_resources(struct mlx5_ib_resources *devr) 2099e126ba97SEli Cohen { 21007722f47eSHaggai Eran struct mlx5_ib_dev *dev = 21017722f47eSHaggai Eran container_of(devr, struct mlx5_ib_dev, devr); 21027722f47eSHaggai Eran int port; 21037722f47eSHaggai Eran 21044aa17b28SHaggai Abramonvsky mlx5_ib_destroy_srq(devr->s1); 2105e126ba97SEli Cohen mlx5_ib_destroy_srq(devr->s0); 2106e126ba97SEli Cohen mlx5_ib_dealloc_xrcd(devr->x0); 2107e126ba97SEli Cohen mlx5_ib_dealloc_xrcd(devr->x1); 2108e126ba97SEli Cohen mlx5_ib_destroy_cq(devr->c0); 2109e126ba97SEli Cohen mlx5_ib_dealloc_pd(devr->p0); 21107722f47eSHaggai Eran 21117722f47eSHaggai Eran /* Make sure no change P_Key work items are still executing */ 21127722f47eSHaggai Eran for (port = 0; port < dev->num_ports; ++port) 21137722f47eSHaggai Eran cancel_work_sync(&devr->ports[port].pkey_change_work); 2114e126ba97SEli Cohen } 2115e126ba97SEli Cohen 2116e53505a8SAchiad Shochat static u32 get_core_cap_flags(struct ib_device *ibdev) 2117e53505a8SAchiad Shochat { 2118e53505a8SAchiad Shochat struct mlx5_ib_dev *dev = to_mdev(ibdev); 2119e53505a8SAchiad Shochat enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, 1); 2120e53505a8SAchiad Shochat u8 l3_type_cap = MLX5_CAP_ROCE(dev->mdev, l3_type); 2121e53505a8SAchiad Shochat u8 roce_version_cap = MLX5_CAP_ROCE(dev->mdev, roce_version); 2122e53505a8SAchiad Shochat u32 ret = 0; 2123e53505a8SAchiad Shochat 2124e53505a8SAchiad Shochat if (ll == IB_LINK_LAYER_INFINIBAND) 2125e53505a8SAchiad Shochat return RDMA_CORE_PORT_IBA_IB; 2126e53505a8SAchiad Shochat 2127e53505a8SAchiad Shochat if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV4_CAP)) 2128e53505a8SAchiad Shochat return 0; 2129e53505a8SAchiad Shochat 2130e53505a8SAchiad Shochat if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV6_CAP)) 2131e53505a8SAchiad Shochat return 0; 2132e53505a8SAchiad Shochat 2133e53505a8SAchiad Shochat if (roce_version_cap & MLX5_ROCE_VERSION_1_CAP) 2134e53505a8SAchiad Shochat ret |= RDMA_CORE_PORT_IBA_ROCE; 2135e53505a8SAchiad Shochat 2136e53505a8SAchiad Shochat if (roce_version_cap & MLX5_ROCE_VERSION_2_CAP) 2137e53505a8SAchiad Shochat ret |= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP; 2138e53505a8SAchiad Shochat 2139e53505a8SAchiad Shochat return ret; 2140e53505a8SAchiad Shochat } 2141e53505a8SAchiad Shochat 21427738613eSIra Weiny static int mlx5_port_immutable(struct ib_device *ibdev, u8 port_num, 21437738613eSIra Weiny struct ib_port_immutable *immutable) 21447738613eSIra Weiny { 21457738613eSIra Weiny struct ib_port_attr attr; 21467738613eSIra Weiny int err; 21477738613eSIra Weiny 21487738613eSIra Weiny err = mlx5_ib_query_port(ibdev, port_num, &attr); 21497738613eSIra Weiny if (err) 21507738613eSIra Weiny return err; 21517738613eSIra Weiny 21527738613eSIra Weiny immutable->pkey_tbl_len = attr.pkey_tbl_len; 21537738613eSIra Weiny immutable->gid_tbl_len = attr.gid_tbl_len; 2154e53505a8SAchiad Shochat immutable->core_cap_flags = get_core_cap_flags(ibdev); 2155337877a4SIra Weiny immutable->max_mad_size = IB_MGMT_MAD_SIZE; 21567738613eSIra Weiny 21577738613eSIra Weiny return 0; 21587738613eSIra Weiny } 21597738613eSIra Weiny 2160fc24fc5eSAchiad Shochat static int mlx5_enable_roce(struct mlx5_ib_dev *dev) 2161fc24fc5eSAchiad Shochat { 2162e53505a8SAchiad Shochat int err; 2163e53505a8SAchiad Shochat 2164fc24fc5eSAchiad Shochat dev->roce.nb.notifier_call = mlx5_netdev_event; 2165e53505a8SAchiad Shochat err = register_netdevice_notifier(&dev->roce.nb); 2166e53505a8SAchiad Shochat if (err) 2167e53505a8SAchiad Shochat return err; 2168e53505a8SAchiad Shochat 2169e53505a8SAchiad Shochat err = mlx5_nic_vport_enable_roce(dev->mdev); 2170e53505a8SAchiad Shochat if (err) 2171e53505a8SAchiad Shochat goto err_unregister_netdevice_notifier; 2172e53505a8SAchiad Shochat 2173e53505a8SAchiad Shochat return 0; 2174e53505a8SAchiad Shochat 2175e53505a8SAchiad Shochat err_unregister_netdevice_notifier: 2176e53505a8SAchiad Shochat unregister_netdevice_notifier(&dev->roce.nb); 2177e53505a8SAchiad Shochat return err; 2178fc24fc5eSAchiad Shochat } 2179fc24fc5eSAchiad Shochat 2180fc24fc5eSAchiad Shochat static void mlx5_disable_roce(struct mlx5_ib_dev *dev) 2181fc24fc5eSAchiad Shochat { 2182e53505a8SAchiad Shochat mlx5_nic_vport_disable_roce(dev->mdev); 2183fc24fc5eSAchiad Shochat unregister_netdevice_notifier(&dev->roce.nb); 2184fc24fc5eSAchiad Shochat } 2185fc24fc5eSAchiad Shochat 21869603b61dSJack Morgenstein static void *mlx5_ib_add(struct mlx5_core_dev *mdev) 2187e126ba97SEli Cohen { 2188e126ba97SEli Cohen struct mlx5_ib_dev *dev; 2189ebd61f68SAchiad Shochat enum rdma_link_layer ll; 2190ebd61f68SAchiad Shochat int port_type_cap; 2191e126ba97SEli Cohen int err; 2192e126ba97SEli Cohen int i; 2193e126ba97SEli Cohen 2194ebd61f68SAchiad Shochat port_type_cap = MLX5_CAP_GEN(mdev, port_type); 2195ebd61f68SAchiad Shochat ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap); 2196ebd61f68SAchiad Shochat 2197e53505a8SAchiad Shochat if ((ll == IB_LINK_LAYER_ETHERNET) && !MLX5_CAP_GEN(mdev, roce)) 2198647241eaSMajd Dibbiny return NULL; 2199647241eaSMajd Dibbiny 2200e126ba97SEli Cohen printk_once(KERN_INFO "%s", mlx5_version); 2201e126ba97SEli Cohen 2202e126ba97SEli Cohen dev = (struct mlx5_ib_dev *)ib_alloc_device(sizeof(*dev)); 2203e126ba97SEli Cohen if (!dev) 22049603b61dSJack Morgenstein return NULL; 2205e126ba97SEli Cohen 22069603b61dSJack Morgenstein dev->mdev = mdev; 2207e126ba97SEli Cohen 2208fc24fc5eSAchiad Shochat rwlock_init(&dev->roce.netdev_lock); 2209e126ba97SEli Cohen err = get_port_caps(dev); 2210e126ba97SEli Cohen if (err) 22119603b61dSJack Morgenstein goto err_dealloc; 2212e126ba97SEli Cohen 22131b5daf11SMajd Dibbiny if (mlx5_use_mad_ifc(dev)) 2214e126ba97SEli Cohen get_ext_port_caps(dev); 2215e126ba97SEli Cohen 2216e126ba97SEli Cohen MLX5_INIT_DOORBELL_LOCK(&dev->uar_lock); 2217e126ba97SEli Cohen 2218e126ba97SEli Cohen strlcpy(dev->ib_dev.name, "mlx5_%d", IB_DEVICE_NAME_MAX); 2219e126ba97SEli Cohen dev->ib_dev.owner = THIS_MODULE; 2220e126ba97SEli Cohen dev->ib_dev.node_type = RDMA_NODE_IB_CA; 2221c6790aa9SSagi Grimberg dev->ib_dev.local_dma_lkey = 0 /* not supported for now */; 2222938fe83cSSaeed Mahameed dev->num_ports = MLX5_CAP_GEN(mdev, num_ports); 2223e126ba97SEli Cohen dev->ib_dev.phys_port_cnt = dev->num_ports; 2224233d05d2SSaeed Mahameed dev->ib_dev.num_comp_vectors = 2225233d05d2SSaeed Mahameed dev->mdev->priv.eq_table.num_comp_vectors; 2226e126ba97SEli Cohen dev->ib_dev.dma_device = &mdev->pdev->dev; 2227e126ba97SEli Cohen 2228e126ba97SEli Cohen dev->ib_dev.uverbs_abi_ver = MLX5_IB_UVERBS_ABI_VERSION; 2229e126ba97SEli Cohen dev->ib_dev.uverbs_cmd_mask = 2230e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) | 2231e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) | 2232e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_QUERY_PORT) | 2233e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_ALLOC_PD) | 2234e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) | 2235e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_REG_MR) | 2236*56e11d62SNoa Osherovich (1ull << IB_USER_VERBS_CMD_REREG_MR) | 2237e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_DEREG_MR) | 2238e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) | 2239e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_CREATE_CQ) | 2240e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_RESIZE_CQ) | 2241e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) | 2242e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_CREATE_QP) | 2243e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_MODIFY_QP) | 2244e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_QUERY_QP) | 2245e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_DESTROY_QP) | 2246e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST) | 2247e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_DETACH_MCAST) | 2248e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_CREATE_SRQ) | 2249e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ) | 2250e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_QUERY_SRQ) | 2251e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ) | 2252e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_CREATE_XSRQ) | 2253e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_OPEN_QP); 22541707cb4aSHaggai Eran dev->ib_dev.uverbs_ex_cmd_mask = 2255d4584ddfSMatan Barak (1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE) | 2256d4584ddfSMatan Barak (1ull << IB_USER_VERBS_EX_CMD_CREATE_CQ) | 2257d4584ddfSMatan Barak (1ull << IB_USER_VERBS_EX_CMD_CREATE_QP); 2258e126ba97SEli Cohen 2259e126ba97SEli Cohen dev->ib_dev.query_device = mlx5_ib_query_device; 2260e126ba97SEli Cohen dev->ib_dev.query_port = mlx5_ib_query_port; 2261ebd61f68SAchiad Shochat dev->ib_dev.get_link_layer = mlx5_ib_port_link_layer; 2262fc24fc5eSAchiad Shochat if (ll == IB_LINK_LAYER_ETHERNET) 2263fc24fc5eSAchiad Shochat dev->ib_dev.get_netdev = mlx5_ib_get_netdev; 2264e126ba97SEli Cohen dev->ib_dev.query_gid = mlx5_ib_query_gid; 22653cca2606SAchiad Shochat dev->ib_dev.add_gid = mlx5_ib_add_gid; 22663cca2606SAchiad Shochat dev->ib_dev.del_gid = mlx5_ib_del_gid; 2267e126ba97SEli Cohen dev->ib_dev.query_pkey = mlx5_ib_query_pkey; 2268e126ba97SEli Cohen dev->ib_dev.modify_device = mlx5_ib_modify_device; 2269e126ba97SEli Cohen dev->ib_dev.modify_port = mlx5_ib_modify_port; 2270e126ba97SEli Cohen dev->ib_dev.alloc_ucontext = mlx5_ib_alloc_ucontext; 2271e126ba97SEli Cohen dev->ib_dev.dealloc_ucontext = mlx5_ib_dealloc_ucontext; 2272e126ba97SEli Cohen dev->ib_dev.mmap = mlx5_ib_mmap; 2273e126ba97SEli Cohen dev->ib_dev.alloc_pd = mlx5_ib_alloc_pd; 2274e126ba97SEli Cohen dev->ib_dev.dealloc_pd = mlx5_ib_dealloc_pd; 2275e126ba97SEli Cohen dev->ib_dev.create_ah = mlx5_ib_create_ah; 2276e126ba97SEli Cohen dev->ib_dev.query_ah = mlx5_ib_query_ah; 2277e126ba97SEli Cohen dev->ib_dev.destroy_ah = mlx5_ib_destroy_ah; 2278e126ba97SEli Cohen dev->ib_dev.create_srq = mlx5_ib_create_srq; 2279e126ba97SEli Cohen dev->ib_dev.modify_srq = mlx5_ib_modify_srq; 2280e126ba97SEli Cohen dev->ib_dev.query_srq = mlx5_ib_query_srq; 2281e126ba97SEli Cohen dev->ib_dev.destroy_srq = mlx5_ib_destroy_srq; 2282e126ba97SEli Cohen dev->ib_dev.post_srq_recv = mlx5_ib_post_srq_recv; 2283e126ba97SEli Cohen dev->ib_dev.create_qp = mlx5_ib_create_qp; 2284e126ba97SEli Cohen dev->ib_dev.modify_qp = mlx5_ib_modify_qp; 2285e126ba97SEli Cohen dev->ib_dev.query_qp = mlx5_ib_query_qp; 2286e126ba97SEli Cohen dev->ib_dev.destroy_qp = mlx5_ib_destroy_qp; 2287e126ba97SEli Cohen dev->ib_dev.post_send = mlx5_ib_post_send; 2288e126ba97SEli Cohen dev->ib_dev.post_recv = mlx5_ib_post_recv; 2289e126ba97SEli Cohen dev->ib_dev.create_cq = mlx5_ib_create_cq; 2290e126ba97SEli Cohen dev->ib_dev.modify_cq = mlx5_ib_modify_cq; 2291e126ba97SEli Cohen dev->ib_dev.resize_cq = mlx5_ib_resize_cq; 2292e126ba97SEli Cohen dev->ib_dev.destroy_cq = mlx5_ib_destroy_cq; 2293e126ba97SEli Cohen dev->ib_dev.poll_cq = mlx5_ib_poll_cq; 2294e126ba97SEli Cohen dev->ib_dev.req_notify_cq = mlx5_ib_arm_cq; 2295e126ba97SEli Cohen dev->ib_dev.get_dma_mr = mlx5_ib_get_dma_mr; 2296e126ba97SEli Cohen dev->ib_dev.reg_user_mr = mlx5_ib_reg_user_mr; 2297*56e11d62SNoa Osherovich dev->ib_dev.rereg_user_mr = mlx5_ib_rereg_user_mr; 2298e126ba97SEli Cohen dev->ib_dev.dereg_mr = mlx5_ib_dereg_mr; 2299e126ba97SEli Cohen dev->ib_dev.attach_mcast = mlx5_ib_mcg_attach; 2300e126ba97SEli Cohen dev->ib_dev.detach_mcast = mlx5_ib_mcg_detach; 2301e126ba97SEli Cohen dev->ib_dev.process_mad = mlx5_ib_process_mad; 23029bee178bSSagi Grimberg dev->ib_dev.alloc_mr = mlx5_ib_alloc_mr; 23038a187ee5SSagi Grimberg dev->ib_dev.map_mr_sg = mlx5_ib_map_mr_sg; 2304d5436ba0SSagi Grimberg dev->ib_dev.check_mr_status = mlx5_ib_check_mr_status; 23057738613eSIra Weiny dev->ib_dev.get_port_immutable = mlx5_port_immutable; 2306e126ba97SEli Cohen 2307938fe83cSSaeed Mahameed mlx5_ib_internal_fill_odp_caps(dev); 23088cdd312cSHaggai Eran 2309938fe83cSSaeed Mahameed if (MLX5_CAP_GEN(mdev, xrc)) { 2310e126ba97SEli Cohen dev->ib_dev.alloc_xrcd = mlx5_ib_alloc_xrcd; 2311e126ba97SEli Cohen dev->ib_dev.dealloc_xrcd = mlx5_ib_dealloc_xrcd; 2312e126ba97SEli Cohen dev->ib_dev.uverbs_cmd_mask |= 2313e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_OPEN_XRCD) | 2314e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_CLOSE_XRCD); 2315e126ba97SEli Cohen } 2316e126ba97SEli Cohen 2317048ccca8SLinus Torvalds if (mlx5_ib_port_link_layer(&dev->ib_dev, 1) == 2318038d2ef8SMaor Gottlieb IB_LINK_LAYER_ETHERNET) { 2319038d2ef8SMaor Gottlieb dev->ib_dev.create_flow = mlx5_ib_create_flow; 2320038d2ef8SMaor Gottlieb dev->ib_dev.destroy_flow = mlx5_ib_destroy_flow; 2321038d2ef8SMaor Gottlieb dev->ib_dev.uverbs_ex_cmd_mask |= 2322038d2ef8SMaor Gottlieb (1ull << IB_USER_VERBS_EX_CMD_CREATE_FLOW) | 2323038d2ef8SMaor Gottlieb (1ull << IB_USER_VERBS_EX_CMD_DESTROY_FLOW); 2324038d2ef8SMaor Gottlieb } 2325e126ba97SEli Cohen err = init_node_data(dev); 2326e126ba97SEli Cohen if (err) 2327233d05d2SSaeed Mahameed goto err_dealloc; 2328e126ba97SEli Cohen 2329038d2ef8SMaor Gottlieb mutex_init(&dev->flow_db.lock); 2330e126ba97SEli Cohen mutex_init(&dev->cap_mask_mutex); 2331e126ba97SEli Cohen 2332fc24fc5eSAchiad Shochat if (ll == IB_LINK_LAYER_ETHERNET) { 2333fc24fc5eSAchiad Shochat err = mlx5_enable_roce(dev); 2334e126ba97SEli Cohen if (err) 2335233d05d2SSaeed Mahameed goto err_dealloc; 2336fc24fc5eSAchiad Shochat } 2337fc24fc5eSAchiad Shochat 2338fc24fc5eSAchiad Shochat err = create_dev_resources(&dev->devr); 2339fc24fc5eSAchiad Shochat if (err) 2340fc24fc5eSAchiad Shochat goto err_disable_roce; 2341e126ba97SEli Cohen 23426aec21f6SHaggai Eran err = mlx5_ib_odp_init_one(dev); 2343281d1a92SWei Yongjun if (err) 2344e126ba97SEli Cohen goto err_rsrc; 2345e126ba97SEli Cohen 23466aec21f6SHaggai Eran err = ib_register_device(&dev->ib_dev, NULL); 23476aec21f6SHaggai Eran if (err) 23486aec21f6SHaggai Eran goto err_odp; 23496aec21f6SHaggai Eran 2350e126ba97SEli Cohen err = create_umr_res(dev); 2351e126ba97SEli Cohen if (err) 2352e126ba97SEli Cohen goto err_dev; 2353e126ba97SEli Cohen 2354e126ba97SEli Cohen for (i = 0; i < ARRAY_SIZE(mlx5_class_attributes); i++) { 2355281d1a92SWei Yongjun err = device_create_file(&dev->ib_dev.dev, 2356281d1a92SWei Yongjun mlx5_class_attributes[i]); 2357281d1a92SWei Yongjun if (err) 2358e126ba97SEli Cohen goto err_umrc; 2359e126ba97SEli Cohen } 2360e126ba97SEli Cohen 2361e126ba97SEli Cohen dev->ib_active = true; 2362e126ba97SEli Cohen 23639603b61dSJack Morgenstein return dev; 2364e126ba97SEli Cohen 2365e126ba97SEli Cohen err_umrc: 2366e126ba97SEli Cohen destroy_umrc_res(dev); 2367e126ba97SEli Cohen 2368e126ba97SEli Cohen err_dev: 2369e126ba97SEli Cohen ib_unregister_device(&dev->ib_dev); 2370e126ba97SEli Cohen 23716aec21f6SHaggai Eran err_odp: 23726aec21f6SHaggai Eran mlx5_ib_odp_remove_one(dev); 23736aec21f6SHaggai Eran 2374e126ba97SEli Cohen err_rsrc: 2375e126ba97SEli Cohen destroy_dev_resources(&dev->devr); 2376e126ba97SEli Cohen 2377fc24fc5eSAchiad Shochat err_disable_roce: 2378fc24fc5eSAchiad Shochat if (ll == IB_LINK_LAYER_ETHERNET) 2379fc24fc5eSAchiad Shochat mlx5_disable_roce(dev); 2380fc24fc5eSAchiad Shochat 23819603b61dSJack Morgenstein err_dealloc: 2382e126ba97SEli Cohen ib_dealloc_device((struct ib_device *)dev); 2383e126ba97SEli Cohen 23849603b61dSJack Morgenstein return NULL; 2385e126ba97SEli Cohen } 2386e126ba97SEli Cohen 23879603b61dSJack Morgenstein static void mlx5_ib_remove(struct mlx5_core_dev *mdev, void *context) 2388e126ba97SEli Cohen { 23899603b61dSJack Morgenstein struct mlx5_ib_dev *dev = context; 2390fc24fc5eSAchiad Shochat enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev, 1); 23916aec21f6SHaggai Eran 2392e126ba97SEli Cohen ib_unregister_device(&dev->ib_dev); 2393eefd56e5SEli Cohen destroy_umrc_res(dev); 23946aec21f6SHaggai Eran mlx5_ib_odp_remove_one(dev); 2395e126ba97SEli Cohen destroy_dev_resources(&dev->devr); 2396fc24fc5eSAchiad Shochat if (ll == IB_LINK_LAYER_ETHERNET) 2397fc24fc5eSAchiad Shochat mlx5_disable_roce(dev); 2398e126ba97SEli Cohen ib_dealloc_device(&dev->ib_dev); 2399e126ba97SEli Cohen } 2400e126ba97SEli Cohen 24019603b61dSJack Morgenstein static struct mlx5_interface mlx5_ib_interface = { 24029603b61dSJack Morgenstein .add = mlx5_ib_add, 24039603b61dSJack Morgenstein .remove = mlx5_ib_remove, 24049603b61dSJack Morgenstein .event = mlx5_ib_event, 240564613d94SSaeed Mahameed .protocol = MLX5_INTERFACE_PROTOCOL_IB, 2406e126ba97SEli Cohen }; 2407e126ba97SEli Cohen 2408e126ba97SEli Cohen static int __init mlx5_ib_init(void) 2409e126ba97SEli Cohen { 24106aec21f6SHaggai Eran int err; 24116aec21f6SHaggai Eran 24129603b61dSJack Morgenstein if (deprecated_prof_sel != 2) 24139603b61dSJack Morgenstein pr_warn("prof_sel is deprecated for mlx5_ib, set it for mlx5_core\n"); 24149603b61dSJack Morgenstein 24156aec21f6SHaggai Eran err = mlx5_ib_odp_init(); 24166aec21f6SHaggai Eran if (err) 24176aec21f6SHaggai Eran return err; 24186aec21f6SHaggai Eran 24196aec21f6SHaggai Eran err = mlx5_register_interface(&mlx5_ib_interface); 24206aec21f6SHaggai Eran if (err) 24216aec21f6SHaggai Eran goto clean_odp; 24226aec21f6SHaggai Eran 24236aec21f6SHaggai Eran return err; 24246aec21f6SHaggai Eran 24256aec21f6SHaggai Eran clean_odp: 24266aec21f6SHaggai Eran mlx5_ib_odp_cleanup(); 24276aec21f6SHaggai Eran return err; 2428e126ba97SEli Cohen } 2429e126ba97SEli Cohen 2430e126ba97SEli Cohen static void __exit mlx5_ib_cleanup(void) 2431e126ba97SEli Cohen { 24329603b61dSJack Morgenstein mlx5_unregister_interface(&mlx5_ib_interface); 24336aec21f6SHaggai Eran mlx5_ib_odp_cleanup(); 2434e126ba97SEli Cohen } 2435e126ba97SEli Cohen 2436e126ba97SEli Cohen module_init(mlx5_ib_init); 2437e126ba97SEli Cohen module_exit(mlx5_ib_cleanup); 2438