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> 4137aa5c36SGuy Levi #if defined(CONFIG_X86) 4237aa5c36SGuy Levi #include <asm/pat.h> 4337aa5c36SGuy Levi #endif 44e126ba97SEli Cohen #include <linux/sched.h> 457c2344c3SMaor Gottlieb #include <linux/delay.h> 46e126ba97SEli Cohen #include <rdma/ib_user_verbs.h> 473f89a643SAchiad Shochat #include <rdma/ib_addr.h> 482811ba51SAchiad Shochat #include <rdma/ib_cache.h> 49ada68c31SAchiad Shochat #include <linux/mlx5/port.h> 501b5daf11SMajd Dibbiny #include <linux/mlx5/vport.h> 517c2344c3SMaor Gottlieb #include <linux/list.h> 52e126ba97SEli Cohen #include <rdma/ib_smi.h> 53e126ba97SEli Cohen #include <rdma/ib_umem.h> 54038d2ef8SMaor Gottlieb #include <linux/in.h> 55038d2ef8SMaor Gottlieb #include <linux/etherdevice.h> 56038d2ef8SMaor Gottlieb #include <linux/mlx5/fs.h> 57e126ba97SEli Cohen #include "user.h" 58e126ba97SEli Cohen #include "mlx5_ib.h" 59e126ba97SEli Cohen 60e126ba97SEli Cohen #define DRIVER_NAME "mlx5_ib" 61169a1d85SAmir Vadai #define DRIVER_VERSION "2.2-1" 62169a1d85SAmir Vadai #define DRIVER_RELDATE "Feb 2014" 63e126ba97SEli Cohen 64e126ba97SEli Cohen MODULE_AUTHOR("Eli Cohen <eli@mellanox.com>"); 65e126ba97SEli Cohen MODULE_DESCRIPTION("Mellanox Connect-IB HCA IB driver"); 66e126ba97SEli Cohen MODULE_LICENSE("Dual BSD/GPL"); 67e126ba97SEli Cohen MODULE_VERSION(DRIVER_VERSION); 68e126ba97SEli Cohen 699603b61dSJack Morgenstein static int deprecated_prof_sel = 2; 709603b61dSJack Morgenstein module_param_named(prof_sel, deprecated_prof_sel, int, 0444); 719603b61dSJack Morgenstein MODULE_PARM_DESC(prof_sel, "profile selector. Deprecated here. Moved to module mlx5_core"); 72e126ba97SEli Cohen 73e126ba97SEli Cohen static char mlx5_version[] = 74e126ba97SEli Cohen DRIVER_NAME ": Mellanox Connect-IB Infiniband driver v" 75e126ba97SEli Cohen DRIVER_VERSION " (" DRIVER_RELDATE ")\n"; 76e126ba97SEli Cohen 77da7525d2SEran Ben Elisha enum { 78da7525d2SEran Ben Elisha MLX5_ATOMIC_SIZE_QP_8BYTES = 1 << 3, 79da7525d2SEran Ben Elisha }; 801b5daf11SMajd Dibbiny 811b5daf11SMajd Dibbiny static enum rdma_link_layer 82ebd61f68SAchiad Shochat mlx5_port_type_cap_to_rdma_ll(int port_type_cap) 831b5daf11SMajd Dibbiny { 84ebd61f68SAchiad Shochat switch (port_type_cap) { 851b5daf11SMajd Dibbiny case MLX5_CAP_PORT_TYPE_IB: 861b5daf11SMajd Dibbiny return IB_LINK_LAYER_INFINIBAND; 871b5daf11SMajd Dibbiny case MLX5_CAP_PORT_TYPE_ETH: 881b5daf11SMajd Dibbiny return IB_LINK_LAYER_ETHERNET; 891b5daf11SMajd Dibbiny default: 901b5daf11SMajd Dibbiny return IB_LINK_LAYER_UNSPECIFIED; 911b5daf11SMajd Dibbiny } 921b5daf11SMajd Dibbiny } 931b5daf11SMajd Dibbiny 94ebd61f68SAchiad Shochat static enum rdma_link_layer 95ebd61f68SAchiad Shochat mlx5_ib_port_link_layer(struct ib_device *device, u8 port_num) 96ebd61f68SAchiad Shochat { 97ebd61f68SAchiad Shochat struct mlx5_ib_dev *dev = to_mdev(device); 98ebd61f68SAchiad Shochat int port_type_cap = MLX5_CAP_GEN(dev->mdev, port_type); 99ebd61f68SAchiad Shochat 100ebd61f68SAchiad Shochat return mlx5_port_type_cap_to_rdma_ll(port_type_cap); 101ebd61f68SAchiad Shochat } 102ebd61f68SAchiad Shochat 103fc24fc5eSAchiad Shochat static int mlx5_netdev_event(struct notifier_block *this, 104fc24fc5eSAchiad Shochat unsigned long event, void *ptr) 105fc24fc5eSAchiad Shochat { 106fc24fc5eSAchiad Shochat struct net_device *ndev = netdev_notifier_info_to_dev(ptr); 107fc24fc5eSAchiad Shochat struct mlx5_ib_dev *ibdev = container_of(this, struct mlx5_ib_dev, 108fc24fc5eSAchiad Shochat roce.nb); 109fc24fc5eSAchiad Shochat 110*5ec8c83eSAviv Heller switch (event) { 111*5ec8c83eSAviv Heller case NETDEV_REGISTER: 112*5ec8c83eSAviv Heller case NETDEV_UNREGISTER: 113fc24fc5eSAchiad Shochat write_lock(&ibdev->roce.netdev_lock); 114fc24fc5eSAchiad Shochat if (ndev->dev.parent == &ibdev->mdev->pdev->dev) 115*5ec8c83eSAviv Heller ibdev->roce.netdev = (event == NETDEV_UNREGISTER) ? 116*5ec8c83eSAviv Heller NULL : ndev; 117fc24fc5eSAchiad Shochat write_unlock(&ibdev->roce.netdev_lock); 118*5ec8c83eSAviv Heller break; 119*5ec8c83eSAviv Heller 120*5ec8c83eSAviv Heller case NETDEV_UP: 121*5ec8c83eSAviv Heller case NETDEV_DOWN: 122*5ec8c83eSAviv Heller if (ndev == ibdev->roce.netdev && ibdev->ib_active) { 123*5ec8c83eSAviv Heller struct ib_event ibev = {0}; 124*5ec8c83eSAviv Heller 125*5ec8c83eSAviv Heller ibev.device = &ibdev->ib_dev; 126*5ec8c83eSAviv Heller ibev.event = (event == NETDEV_UP) ? 127*5ec8c83eSAviv Heller IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR; 128*5ec8c83eSAviv Heller ibev.element.port_num = 1; 129*5ec8c83eSAviv Heller ib_dispatch_event(&ibev); 130*5ec8c83eSAviv Heller } 131*5ec8c83eSAviv Heller break; 132*5ec8c83eSAviv Heller 133*5ec8c83eSAviv Heller default: 134*5ec8c83eSAviv Heller break; 135*5ec8c83eSAviv Heller } 136fc24fc5eSAchiad Shochat 137fc24fc5eSAchiad Shochat return NOTIFY_DONE; 138fc24fc5eSAchiad Shochat } 139fc24fc5eSAchiad Shochat 140fc24fc5eSAchiad Shochat static struct net_device *mlx5_ib_get_netdev(struct ib_device *device, 141fc24fc5eSAchiad Shochat u8 port_num) 142fc24fc5eSAchiad Shochat { 143fc24fc5eSAchiad Shochat struct mlx5_ib_dev *ibdev = to_mdev(device); 144fc24fc5eSAchiad Shochat struct net_device *ndev; 145fc24fc5eSAchiad Shochat 146fc24fc5eSAchiad Shochat /* Ensure ndev does not disappear before we invoke dev_hold() 147fc24fc5eSAchiad Shochat */ 148fc24fc5eSAchiad Shochat read_lock(&ibdev->roce.netdev_lock); 149fc24fc5eSAchiad Shochat ndev = ibdev->roce.netdev; 150fc24fc5eSAchiad Shochat if (ndev) 151fc24fc5eSAchiad Shochat dev_hold(ndev); 152fc24fc5eSAchiad Shochat read_unlock(&ibdev->roce.netdev_lock); 153fc24fc5eSAchiad Shochat 154fc24fc5eSAchiad Shochat return ndev; 155fc24fc5eSAchiad Shochat } 156fc24fc5eSAchiad Shochat 1573f89a643SAchiad Shochat static int mlx5_query_port_roce(struct ib_device *device, u8 port_num, 1583f89a643SAchiad Shochat struct ib_port_attr *props) 1593f89a643SAchiad Shochat { 1603f89a643SAchiad Shochat struct mlx5_ib_dev *dev = to_mdev(device); 1613f89a643SAchiad Shochat struct net_device *ndev; 1623f89a643SAchiad Shochat enum ib_mtu ndev_ib_mtu; 163c876a1b7SLeon Romanovsky u16 qkey_viol_cntr; 1643f89a643SAchiad Shochat 1653f89a643SAchiad Shochat memset(props, 0, sizeof(*props)); 1663f89a643SAchiad Shochat 1673f89a643SAchiad Shochat props->port_cap_flags |= IB_PORT_CM_SUP; 1683f89a643SAchiad Shochat props->port_cap_flags |= IB_PORT_IP_BASED_GIDS; 1693f89a643SAchiad Shochat 1703f89a643SAchiad Shochat props->gid_tbl_len = MLX5_CAP_ROCE(dev->mdev, 1713f89a643SAchiad Shochat roce_address_table_size); 1723f89a643SAchiad Shochat props->max_mtu = IB_MTU_4096; 1733f89a643SAchiad Shochat props->max_msg_sz = 1 << MLX5_CAP_GEN(dev->mdev, log_max_msg); 1743f89a643SAchiad Shochat props->pkey_tbl_len = 1; 1753f89a643SAchiad Shochat props->state = IB_PORT_DOWN; 1763f89a643SAchiad Shochat props->phys_state = 3; 1773f89a643SAchiad Shochat 178c876a1b7SLeon Romanovsky mlx5_query_nic_vport_qkey_viol_cntr(dev->mdev, &qkey_viol_cntr); 179c876a1b7SLeon Romanovsky props->qkey_viol_cntr = qkey_viol_cntr; 1803f89a643SAchiad Shochat 1813f89a643SAchiad Shochat ndev = mlx5_ib_get_netdev(device, port_num); 1823f89a643SAchiad Shochat if (!ndev) 1833f89a643SAchiad Shochat return 0; 1843f89a643SAchiad Shochat 1853f89a643SAchiad Shochat if (netif_running(ndev) && netif_carrier_ok(ndev)) { 1863f89a643SAchiad Shochat props->state = IB_PORT_ACTIVE; 1873f89a643SAchiad Shochat props->phys_state = 5; 1883f89a643SAchiad Shochat } 1893f89a643SAchiad Shochat 1903f89a643SAchiad Shochat ndev_ib_mtu = iboe_get_mtu(ndev->mtu); 1913f89a643SAchiad Shochat 1923f89a643SAchiad Shochat dev_put(ndev); 1933f89a643SAchiad Shochat 1943f89a643SAchiad Shochat props->active_mtu = min(props->max_mtu, ndev_ib_mtu); 1953f89a643SAchiad Shochat 1963f89a643SAchiad Shochat props->active_width = IB_WIDTH_4X; /* TODO */ 1973f89a643SAchiad Shochat props->active_speed = IB_SPEED_QDR; /* TODO */ 1983f89a643SAchiad Shochat 1993f89a643SAchiad Shochat return 0; 2003f89a643SAchiad Shochat } 2013f89a643SAchiad Shochat 2023cca2606SAchiad Shochat static void ib_gid_to_mlx5_roce_addr(const union ib_gid *gid, 2033cca2606SAchiad Shochat const struct ib_gid_attr *attr, 2043cca2606SAchiad Shochat void *mlx5_addr) 2053cca2606SAchiad Shochat { 2063cca2606SAchiad Shochat #define MLX5_SET_RA(p, f, v) MLX5_SET(roce_addr_layout, p, f, v) 2073cca2606SAchiad Shochat char *mlx5_addr_l3_addr = MLX5_ADDR_OF(roce_addr_layout, mlx5_addr, 2083cca2606SAchiad Shochat source_l3_address); 2093cca2606SAchiad Shochat void *mlx5_addr_mac = MLX5_ADDR_OF(roce_addr_layout, mlx5_addr, 2103cca2606SAchiad Shochat source_mac_47_32); 2113cca2606SAchiad Shochat 2123cca2606SAchiad Shochat if (!gid) 2133cca2606SAchiad Shochat return; 2143cca2606SAchiad Shochat 2153cca2606SAchiad Shochat ether_addr_copy(mlx5_addr_mac, attr->ndev->dev_addr); 2163cca2606SAchiad Shochat 2173cca2606SAchiad Shochat if (is_vlan_dev(attr->ndev)) { 2183cca2606SAchiad Shochat MLX5_SET_RA(mlx5_addr, vlan_valid, 1); 2193cca2606SAchiad Shochat MLX5_SET_RA(mlx5_addr, vlan_id, vlan_dev_vlan_id(attr->ndev)); 2203cca2606SAchiad Shochat } 2213cca2606SAchiad Shochat 2223cca2606SAchiad Shochat switch (attr->gid_type) { 2233cca2606SAchiad Shochat case IB_GID_TYPE_IB: 2243cca2606SAchiad Shochat MLX5_SET_RA(mlx5_addr, roce_version, MLX5_ROCE_VERSION_1); 2253cca2606SAchiad Shochat break; 2263cca2606SAchiad Shochat case IB_GID_TYPE_ROCE_UDP_ENCAP: 2273cca2606SAchiad Shochat MLX5_SET_RA(mlx5_addr, roce_version, MLX5_ROCE_VERSION_2); 2283cca2606SAchiad Shochat break; 2293cca2606SAchiad Shochat 2303cca2606SAchiad Shochat default: 2313cca2606SAchiad Shochat WARN_ON(true); 2323cca2606SAchiad Shochat } 2333cca2606SAchiad Shochat 2343cca2606SAchiad Shochat if (attr->gid_type != IB_GID_TYPE_IB) { 2353cca2606SAchiad Shochat if (ipv6_addr_v4mapped((void *)gid)) 2363cca2606SAchiad Shochat MLX5_SET_RA(mlx5_addr, roce_l3_type, 2373cca2606SAchiad Shochat MLX5_ROCE_L3_TYPE_IPV4); 2383cca2606SAchiad Shochat else 2393cca2606SAchiad Shochat MLX5_SET_RA(mlx5_addr, roce_l3_type, 2403cca2606SAchiad Shochat MLX5_ROCE_L3_TYPE_IPV6); 2413cca2606SAchiad Shochat } 2423cca2606SAchiad Shochat 2433cca2606SAchiad Shochat if ((attr->gid_type == IB_GID_TYPE_IB) || 2443cca2606SAchiad Shochat !ipv6_addr_v4mapped((void *)gid)) 2453cca2606SAchiad Shochat memcpy(mlx5_addr_l3_addr, gid, sizeof(*gid)); 2463cca2606SAchiad Shochat else 2473cca2606SAchiad Shochat memcpy(&mlx5_addr_l3_addr[12], &gid->raw[12], 4); 2483cca2606SAchiad Shochat } 2493cca2606SAchiad Shochat 2503cca2606SAchiad Shochat static int set_roce_addr(struct ib_device *device, u8 port_num, 2513cca2606SAchiad Shochat unsigned int index, 2523cca2606SAchiad Shochat const union ib_gid *gid, 2533cca2606SAchiad Shochat const struct ib_gid_attr *attr) 2543cca2606SAchiad Shochat { 2553cca2606SAchiad Shochat struct mlx5_ib_dev *dev = to_mdev(device); 256c4f287c4SSaeed Mahameed u32 in[MLX5_ST_SZ_DW(set_roce_address_in)] = {0}; 257c4f287c4SSaeed Mahameed u32 out[MLX5_ST_SZ_DW(set_roce_address_out)] = {0}; 2583cca2606SAchiad Shochat void *in_addr = MLX5_ADDR_OF(set_roce_address_in, in, roce_address); 2593cca2606SAchiad Shochat enum rdma_link_layer ll = mlx5_ib_port_link_layer(device, port_num); 2603cca2606SAchiad Shochat 2613cca2606SAchiad Shochat if (ll != IB_LINK_LAYER_ETHERNET) 2623cca2606SAchiad Shochat return -EINVAL; 2633cca2606SAchiad Shochat 2643cca2606SAchiad Shochat ib_gid_to_mlx5_roce_addr(gid, attr, in_addr); 2653cca2606SAchiad Shochat 2663cca2606SAchiad Shochat MLX5_SET(set_roce_address_in, in, roce_address_index, index); 2673cca2606SAchiad Shochat MLX5_SET(set_roce_address_in, in, opcode, MLX5_CMD_OP_SET_ROCE_ADDRESS); 2683cca2606SAchiad Shochat return mlx5_cmd_exec(dev->mdev, in, sizeof(in), out, sizeof(out)); 2693cca2606SAchiad Shochat } 2703cca2606SAchiad Shochat 2713cca2606SAchiad Shochat static int mlx5_ib_add_gid(struct ib_device *device, u8 port_num, 2723cca2606SAchiad Shochat unsigned int index, const union ib_gid *gid, 2733cca2606SAchiad Shochat const struct ib_gid_attr *attr, 2743cca2606SAchiad Shochat __always_unused void **context) 2753cca2606SAchiad Shochat { 2763cca2606SAchiad Shochat return set_roce_addr(device, port_num, index, gid, attr); 2773cca2606SAchiad Shochat } 2783cca2606SAchiad Shochat 2793cca2606SAchiad Shochat static int mlx5_ib_del_gid(struct ib_device *device, u8 port_num, 2803cca2606SAchiad Shochat unsigned int index, __always_unused void **context) 2813cca2606SAchiad Shochat { 2823cca2606SAchiad Shochat return set_roce_addr(device, port_num, index, NULL, NULL); 2833cca2606SAchiad Shochat } 2843cca2606SAchiad Shochat 2852811ba51SAchiad Shochat __be16 mlx5_get_roce_udp_sport(struct mlx5_ib_dev *dev, u8 port_num, 2862811ba51SAchiad Shochat int index) 2872811ba51SAchiad Shochat { 2882811ba51SAchiad Shochat struct ib_gid_attr attr; 2892811ba51SAchiad Shochat union ib_gid gid; 2902811ba51SAchiad Shochat 2912811ba51SAchiad Shochat if (ib_get_cached_gid(&dev->ib_dev, port_num, index, &gid, &attr)) 2922811ba51SAchiad Shochat return 0; 2932811ba51SAchiad Shochat 2942811ba51SAchiad Shochat if (!attr.ndev) 2952811ba51SAchiad Shochat return 0; 2962811ba51SAchiad Shochat 2972811ba51SAchiad Shochat dev_put(attr.ndev); 2982811ba51SAchiad Shochat 2992811ba51SAchiad Shochat if (attr.gid_type != IB_GID_TYPE_ROCE_UDP_ENCAP) 3002811ba51SAchiad Shochat return 0; 3012811ba51SAchiad Shochat 3022811ba51SAchiad Shochat return cpu_to_be16(MLX5_CAP_ROCE(dev->mdev, r_roce_min_src_udp_port)); 3032811ba51SAchiad Shochat } 3042811ba51SAchiad Shochat 3051b5daf11SMajd Dibbiny static int mlx5_use_mad_ifc(struct mlx5_ib_dev *dev) 3061b5daf11SMajd Dibbiny { 307d603c809SEli Cohen return !MLX5_CAP_GEN(dev->mdev, ib_virt); 3081b5daf11SMajd Dibbiny } 3091b5daf11SMajd Dibbiny 3101b5daf11SMajd Dibbiny enum { 3111b5daf11SMajd Dibbiny MLX5_VPORT_ACCESS_METHOD_MAD, 3121b5daf11SMajd Dibbiny MLX5_VPORT_ACCESS_METHOD_HCA, 3131b5daf11SMajd Dibbiny MLX5_VPORT_ACCESS_METHOD_NIC, 3141b5daf11SMajd Dibbiny }; 3151b5daf11SMajd Dibbiny 3161b5daf11SMajd Dibbiny static int mlx5_get_vport_access_method(struct ib_device *ibdev) 3171b5daf11SMajd Dibbiny { 3181b5daf11SMajd Dibbiny if (mlx5_use_mad_ifc(to_mdev(ibdev))) 3191b5daf11SMajd Dibbiny return MLX5_VPORT_ACCESS_METHOD_MAD; 3201b5daf11SMajd Dibbiny 321ebd61f68SAchiad Shochat if (mlx5_ib_port_link_layer(ibdev, 1) == 3221b5daf11SMajd Dibbiny IB_LINK_LAYER_ETHERNET) 3231b5daf11SMajd Dibbiny return MLX5_VPORT_ACCESS_METHOD_NIC; 3241b5daf11SMajd Dibbiny 3251b5daf11SMajd Dibbiny return MLX5_VPORT_ACCESS_METHOD_HCA; 3261b5daf11SMajd Dibbiny } 3271b5daf11SMajd Dibbiny 328da7525d2SEran Ben Elisha static void get_atomic_caps(struct mlx5_ib_dev *dev, 329da7525d2SEran Ben Elisha struct ib_device_attr *props) 330da7525d2SEran Ben Elisha { 331da7525d2SEran Ben Elisha u8 tmp; 332da7525d2SEran Ben Elisha u8 atomic_operations = MLX5_CAP_ATOMIC(dev->mdev, atomic_operations); 333da7525d2SEran Ben Elisha u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_qp); 334da7525d2SEran Ben Elisha u8 atomic_req_8B_endianness_mode = 335da7525d2SEran Ben Elisha MLX5_CAP_ATOMIC(dev->mdev, atomic_req_8B_endianess_mode); 336da7525d2SEran Ben Elisha 337da7525d2SEran Ben Elisha /* Check if HW supports 8 bytes standard atomic operations and capable 338da7525d2SEran Ben Elisha * of host endianness respond 339da7525d2SEran Ben Elisha */ 340da7525d2SEran Ben Elisha tmp = MLX5_ATOMIC_OPS_CMP_SWAP | MLX5_ATOMIC_OPS_FETCH_ADD; 341da7525d2SEran Ben Elisha if (((atomic_operations & tmp) == tmp) && 342da7525d2SEran Ben Elisha (atomic_size_qp & MLX5_ATOMIC_SIZE_QP_8BYTES) && 343da7525d2SEran Ben Elisha (atomic_req_8B_endianness_mode)) { 344da7525d2SEran Ben Elisha props->atomic_cap = IB_ATOMIC_HCA; 345da7525d2SEran Ben Elisha } else { 346da7525d2SEran Ben Elisha props->atomic_cap = IB_ATOMIC_NONE; 347da7525d2SEran Ben Elisha } 348da7525d2SEran Ben Elisha } 349da7525d2SEran Ben Elisha 3501b5daf11SMajd Dibbiny static int mlx5_query_system_image_guid(struct ib_device *ibdev, 3511b5daf11SMajd Dibbiny __be64 *sys_image_guid) 3521b5daf11SMajd Dibbiny { 3531b5daf11SMajd Dibbiny struct mlx5_ib_dev *dev = to_mdev(ibdev); 3541b5daf11SMajd Dibbiny struct mlx5_core_dev *mdev = dev->mdev; 3551b5daf11SMajd Dibbiny u64 tmp; 3561b5daf11SMajd Dibbiny int err; 3571b5daf11SMajd Dibbiny 3581b5daf11SMajd Dibbiny switch (mlx5_get_vport_access_method(ibdev)) { 3591b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_MAD: 3601b5daf11SMajd Dibbiny return mlx5_query_mad_ifc_system_image_guid(ibdev, 3611b5daf11SMajd Dibbiny sys_image_guid); 3621b5daf11SMajd Dibbiny 3631b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_HCA: 3641b5daf11SMajd Dibbiny err = mlx5_query_hca_vport_system_image_guid(mdev, &tmp); 3653f89a643SAchiad Shochat break; 3663f89a643SAchiad Shochat 3673f89a643SAchiad Shochat case MLX5_VPORT_ACCESS_METHOD_NIC: 3683f89a643SAchiad Shochat err = mlx5_query_nic_vport_system_image_guid(mdev, &tmp); 3693f89a643SAchiad Shochat break; 3701b5daf11SMajd Dibbiny 3711b5daf11SMajd Dibbiny default: 3721b5daf11SMajd Dibbiny return -EINVAL; 3731b5daf11SMajd Dibbiny } 3743f89a643SAchiad Shochat 3753f89a643SAchiad Shochat if (!err) 3763f89a643SAchiad Shochat *sys_image_guid = cpu_to_be64(tmp); 3773f89a643SAchiad Shochat 3783f89a643SAchiad Shochat return err; 3793f89a643SAchiad Shochat 3801b5daf11SMajd Dibbiny } 3811b5daf11SMajd Dibbiny 3821b5daf11SMajd Dibbiny static int mlx5_query_max_pkeys(struct ib_device *ibdev, 3831b5daf11SMajd Dibbiny u16 *max_pkeys) 3841b5daf11SMajd Dibbiny { 3851b5daf11SMajd Dibbiny struct mlx5_ib_dev *dev = to_mdev(ibdev); 3861b5daf11SMajd Dibbiny struct mlx5_core_dev *mdev = dev->mdev; 3871b5daf11SMajd Dibbiny 3881b5daf11SMajd Dibbiny switch (mlx5_get_vport_access_method(ibdev)) { 3891b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_MAD: 3901b5daf11SMajd Dibbiny return mlx5_query_mad_ifc_max_pkeys(ibdev, max_pkeys); 3911b5daf11SMajd Dibbiny 3921b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_HCA: 3931b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_NIC: 3941b5daf11SMajd Dibbiny *max_pkeys = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, 3951b5daf11SMajd Dibbiny pkey_table_size)); 3961b5daf11SMajd Dibbiny return 0; 3971b5daf11SMajd Dibbiny 3981b5daf11SMajd Dibbiny default: 3991b5daf11SMajd Dibbiny return -EINVAL; 4001b5daf11SMajd Dibbiny } 4011b5daf11SMajd Dibbiny } 4021b5daf11SMajd Dibbiny 4031b5daf11SMajd Dibbiny static int mlx5_query_vendor_id(struct ib_device *ibdev, 4041b5daf11SMajd Dibbiny u32 *vendor_id) 4051b5daf11SMajd Dibbiny { 4061b5daf11SMajd Dibbiny struct mlx5_ib_dev *dev = to_mdev(ibdev); 4071b5daf11SMajd Dibbiny 4081b5daf11SMajd Dibbiny switch (mlx5_get_vport_access_method(ibdev)) { 4091b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_MAD: 4101b5daf11SMajd Dibbiny return mlx5_query_mad_ifc_vendor_id(ibdev, vendor_id); 4111b5daf11SMajd Dibbiny 4121b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_HCA: 4131b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_NIC: 4141b5daf11SMajd Dibbiny return mlx5_core_query_vendor_id(dev->mdev, vendor_id); 4151b5daf11SMajd Dibbiny 4161b5daf11SMajd Dibbiny default: 4171b5daf11SMajd Dibbiny return -EINVAL; 4181b5daf11SMajd Dibbiny } 4191b5daf11SMajd Dibbiny } 4201b5daf11SMajd Dibbiny 4211b5daf11SMajd Dibbiny static int mlx5_query_node_guid(struct mlx5_ib_dev *dev, 4221b5daf11SMajd Dibbiny __be64 *node_guid) 4231b5daf11SMajd Dibbiny { 4241b5daf11SMajd Dibbiny u64 tmp; 4251b5daf11SMajd Dibbiny int err; 4261b5daf11SMajd Dibbiny 4271b5daf11SMajd Dibbiny switch (mlx5_get_vport_access_method(&dev->ib_dev)) { 4281b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_MAD: 4291b5daf11SMajd Dibbiny return mlx5_query_mad_ifc_node_guid(dev, node_guid); 4301b5daf11SMajd Dibbiny 4311b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_HCA: 4321b5daf11SMajd Dibbiny err = mlx5_query_hca_vport_node_guid(dev->mdev, &tmp); 4333f89a643SAchiad Shochat break; 4343f89a643SAchiad Shochat 4353f89a643SAchiad Shochat case MLX5_VPORT_ACCESS_METHOD_NIC: 4363f89a643SAchiad Shochat err = mlx5_query_nic_vport_node_guid(dev->mdev, &tmp); 4373f89a643SAchiad Shochat break; 4381b5daf11SMajd Dibbiny 4391b5daf11SMajd Dibbiny default: 4401b5daf11SMajd Dibbiny return -EINVAL; 4411b5daf11SMajd Dibbiny } 4423f89a643SAchiad Shochat 4433f89a643SAchiad Shochat if (!err) 4443f89a643SAchiad Shochat *node_guid = cpu_to_be64(tmp); 4453f89a643SAchiad Shochat 4463f89a643SAchiad Shochat return err; 4471b5daf11SMajd Dibbiny } 4481b5daf11SMajd Dibbiny 4491b5daf11SMajd Dibbiny struct mlx5_reg_node_desc { 4501b5daf11SMajd Dibbiny u8 desc[64]; 4511b5daf11SMajd Dibbiny }; 4521b5daf11SMajd Dibbiny 4531b5daf11SMajd Dibbiny static int mlx5_query_node_desc(struct mlx5_ib_dev *dev, char *node_desc) 4541b5daf11SMajd Dibbiny { 4551b5daf11SMajd Dibbiny struct mlx5_reg_node_desc in; 4561b5daf11SMajd Dibbiny 4571b5daf11SMajd Dibbiny if (mlx5_use_mad_ifc(dev)) 4581b5daf11SMajd Dibbiny return mlx5_query_mad_ifc_node_desc(dev, node_desc); 4591b5daf11SMajd Dibbiny 4601b5daf11SMajd Dibbiny memset(&in, 0, sizeof(in)); 4611b5daf11SMajd Dibbiny 4621b5daf11SMajd Dibbiny return mlx5_core_access_reg(dev->mdev, &in, sizeof(in), node_desc, 4631b5daf11SMajd Dibbiny sizeof(struct mlx5_reg_node_desc), 4641b5daf11SMajd Dibbiny MLX5_REG_NODE_DESC, 0, 0); 4651b5daf11SMajd Dibbiny } 4661b5daf11SMajd Dibbiny 467e126ba97SEli Cohen static int mlx5_ib_query_device(struct ib_device *ibdev, 4682528e33eSMatan Barak struct ib_device_attr *props, 4692528e33eSMatan Barak struct ib_udata *uhw) 470e126ba97SEli Cohen { 471e126ba97SEli Cohen struct mlx5_ib_dev *dev = to_mdev(ibdev); 472938fe83cSSaeed Mahameed struct mlx5_core_dev *mdev = dev->mdev; 473e126ba97SEli Cohen int err = -ENOMEM; 474e126ba97SEli Cohen int max_rq_sg; 475e126ba97SEli Cohen int max_sq_sg; 476e0238a6aSSagi Grimberg u64 min_page_size = 1ull << MLX5_CAP_GEN(mdev, log_pg_sz); 477402ca536SBodong Wang struct mlx5_ib_query_device_resp resp = {}; 478402ca536SBodong Wang size_t resp_len; 479402ca536SBodong Wang u64 max_tso; 480e126ba97SEli Cohen 481402ca536SBodong Wang resp_len = sizeof(resp.comp_mask) + sizeof(resp.response_length); 482402ca536SBodong Wang if (uhw->outlen && uhw->outlen < resp_len) 483402ca536SBodong Wang return -EINVAL; 484402ca536SBodong Wang else 485402ca536SBodong Wang resp.response_length = resp_len; 486402ca536SBodong Wang 487402ca536SBodong Wang if (uhw->inlen && !ib_is_udata_cleared(uhw, 0, uhw->inlen)) 4882528e33eSMatan Barak return -EINVAL; 4892528e33eSMatan Barak 490e126ba97SEli Cohen memset(props, 0, sizeof(*props)); 4911b5daf11SMajd Dibbiny err = mlx5_query_system_image_guid(ibdev, 4921b5daf11SMajd Dibbiny &props->sys_image_guid); 4931b5daf11SMajd Dibbiny if (err) 4941b5daf11SMajd Dibbiny return err; 4951b5daf11SMajd Dibbiny 4961b5daf11SMajd Dibbiny err = mlx5_query_max_pkeys(ibdev, &props->max_pkeys); 4971b5daf11SMajd Dibbiny if (err) 4981b5daf11SMajd Dibbiny return err; 4991b5daf11SMajd Dibbiny 5001b5daf11SMajd Dibbiny err = mlx5_query_vendor_id(ibdev, &props->vendor_id); 5011b5daf11SMajd Dibbiny if (err) 5021b5daf11SMajd Dibbiny return err; 503e126ba97SEli Cohen 5049603b61dSJack Morgenstein props->fw_ver = ((u64)fw_rev_maj(dev->mdev) << 32) | 5059603b61dSJack Morgenstein (fw_rev_min(dev->mdev) << 16) | 5069603b61dSJack Morgenstein fw_rev_sub(dev->mdev); 507e126ba97SEli Cohen props->device_cap_flags = IB_DEVICE_CHANGE_PHY_PORT | 508e126ba97SEli Cohen IB_DEVICE_PORT_ACTIVE_EVENT | 509e126ba97SEli Cohen IB_DEVICE_SYS_IMAGE_GUID | 5101a4c3a3dSEli Cohen IB_DEVICE_RC_RNR_NAK_GEN; 511938fe83cSSaeed Mahameed 512938fe83cSSaeed Mahameed if (MLX5_CAP_GEN(mdev, pkv)) 513e126ba97SEli Cohen props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR; 514938fe83cSSaeed Mahameed if (MLX5_CAP_GEN(mdev, qkv)) 515e126ba97SEli Cohen props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR; 516938fe83cSSaeed Mahameed if (MLX5_CAP_GEN(mdev, apm)) 517e126ba97SEli Cohen props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG; 518938fe83cSSaeed Mahameed if (MLX5_CAP_GEN(mdev, xrc)) 519e126ba97SEli Cohen props->device_cap_flags |= IB_DEVICE_XRC; 520d2370e0aSMatan Barak if (MLX5_CAP_GEN(mdev, imaicl)) { 521d2370e0aSMatan Barak props->device_cap_flags |= IB_DEVICE_MEM_WINDOW | 522d2370e0aSMatan Barak IB_DEVICE_MEM_WINDOW_TYPE_2B; 523d2370e0aSMatan Barak props->max_mw = 1 << MLX5_CAP_GEN(mdev, log_max_mkey); 524b005d316SSagi Grimberg /* We support 'Gappy' memory registration too */ 525b005d316SSagi Grimberg props->device_cap_flags |= IB_DEVICE_SG_GAPS_REG; 526d2370e0aSMatan Barak } 527e126ba97SEli Cohen props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS; 528938fe83cSSaeed Mahameed if (MLX5_CAP_GEN(mdev, sho)) { 5292dea9094SSagi Grimberg props->device_cap_flags |= IB_DEVICE_SIGNATURE_HANDOVER; 5302dea9094SSagi Grimberg /* At this stage no support for signature handover */ 5312dea9094SSagi Grimberg props->sig_prot_cap = IB_PROT_T10DIF_TYPE_1 | 5322dea9094SSagi Grimberg IB_PROT_T10DIF_TYPE_2 | 5332dea9094SSagi Grimberg IB_PROT_T10DIF_TYPE_3; 5342dea9094SSagi Grimberg props->sig_guard_cap = IB_GUARD_T10DIF_CRC | 5352dea9094SSagi Grimberg IB_GUARD_T10DIF_CSUM; 5362dea9094SSagi Grimberg } 537938fe83cSSaeed Mahameed if (MLX5_CAP_GEN(mdev, block_lb_mc)) 538f360d88aSEli Cohen props->device_cap_flags |= IB_DEVICE_BLOCK_MULTICAST_LOOPBACK; 539e126ba97SEli Cohen 540402ca536SBodong Wang if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads)) { 541402ca536SBodong Wang if (MLX5_CAP_ETH(mdev, csum_cap)) 54288115fe7SBodong Wang props->device_cap_flags |= IB_DEVICE_RAW_IP_CSUM; 54388115fe7SBodong Wang 544402ca536SBodong Wang if (field_avail(typeof(resp), tso_caps, uhw->outlen)) { 545402ca536SBodong Wang max_tso = MLX5_CAP_ETH(mdev, max_lso_cap); 546402ca536SBodong Wang if (max_tso) { 547402ca536SBodong Wang resp.tso_caps.max_tso = 1 << max_tso; 548402ca536SBodong Wang resp.tso_caps.supported_qpts |= 549402ca536SBodong Wang 1 << IB_QPT_RAW_PACKET; 550402ca536SBodong Wang resp.response_length += sizeof(resp.tso_caps); 551402ca536SBodong Wang } 552402ca536SBodong Wang } 55331f69a82SYishai Hadas 55431f69a82SYishai Hadas if (field_avail(typeof(resp), rss_caps, uhw->outlen)) { 55531f69a82SYishai Hadas resp.rss_caps.rx_hash_function = 55631f69a82SYishai Hadas MLX5_RX_HASH_FUNC_TOEPLITZ; 55731f69a82SYishai Hadas resp.rss_caps.rx_hash_fields_mask = 55831f69a82SYishai Hadas MLX5_RX_HASH_SRC_IPV4 | 55931f69a82SYishai Hadas MLX5_RX_HASH_DST_IPV4 | 56031f69a82SYishai Hadas MLX5_RX_HASH_SRC_IPV6 | 56131f69a82SYishai Hadas MLX5_RX_HASH_DST_IPV6 | 56231f69a82SYishai Hadas MLX5_RX_HASH_SRC_PORT_TCP | 56331f69a82SYishai Hadas MLX5_RX_HASH_DST_PORT_TCP | 56431f69a82SYishai Hadas MLX5_RX_HASH_SRC_PORT_UDP | 56531f69a82SYishai Hadas MLX5_RX_HASH_DST_PORT_UDP; 56631f69a82SYishai Hadas resp.response_length += sizeof(resp.rss_caps); 56731f69a82SYishai Hadas } 56831f69a82SYishai Hadas } else { 56931f69a82SYishai Hadas if (field_avail(typeof(resp), tso_caps, uhw->outlen)) 57031f69a82SYishai Hadas resp.response_length += sizeof(resp.tso_caps); 57131f69a82SYishai Hadas if (field_avail(typeof(resp), rss_caps, uhw->outlen)) 57231f69a82SYishai Hadas resp.response_length += sizeof(resp.rss_caps); 573402ca536SBodong Wang } 574402ca536SBodong Wang 575f0313965SErez Shitrit if (MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) { 576f0313965SErez Shitrit props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM; 577f0313965SErez Shitrit props->device_cap_flags |= IB_DEVICE_UD_TSO; 578f0313965SErez Shitrit } 579f0313965SErez Shitrit 580cff5a0f3SMajd Dibbiny if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) && 581cff5a0f3SMajd Dibbiny MLX5_CAP_ETH(dev->mdev, scatter_fcs)) 582cff5a0f3SMajd Dibbiny props->device_cap_flags |= IB_DEVICE_RAW_SCATTER_FCS; 583cff5a0f3SMajd Dibbiny 584da6d6ba3SMaor Gottlieb if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS)) 585da6d6ba3SMaor Gottlieb props->device_cap_flags |= IB_DEVICE_MANAGED_FLOW_STEERING; 586da6d6ba3SMaor Gottlieb 5871b5daf11SMajd Dibbiny props->vendor_part_id = mdev->pdev->device; 5881b5daf11SMajd Dibbiny props->hw_ver = mdev->pdev->revision; 589e126ba97SEli Cohen 590e126ba97SEli Cohen props->max_mr_size = ~0ull; 591e0238a6aSSagi Grimberg props->page_size_cap = ~(min_page_size - 1); 592938fe83cSSaeed Mahameed props->max_qp = 1 << MLX5_CAP_GEN(mdev, log_max_qp); 593938fe83cSSaeed Mahameed props->max_qp_wr = 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz); 594938fe83cSSaeed Mahameed max_rq_sg = MLX5_CAP_GEN(mdev, max_wqe_sz_rq) / 595938fe83cSSaeed Mahameed sizeof(struct mlx5_wqe_data_seg); 596938fe83cSSaeed Mahameed max_sq_sg = (MLX5_CAP_GEN(mdev, max_wqe_sz_sq) - 597938fe83cSSaeed Mahameed sizeof(struct mlx5_wqe_ctrl_seg)) / 598e126ba97SEli Cohen sizeof(struct mlx5_wqe_data_seg); 599e126ba97SEli Cohen props->max_sge = min(max_rq_sg, max_sq_sg); 600986ef95eSSagi Grimberg props->max_sge_rd = MLX5_MAX_SGE_RD; 601938fe83cSSaeed Mahameed props->max_cq = 1 << MLX5_CAP_GEN(mdev, log_max_cq); 6029f177686SLeon Romanovsky props->max_cqe = (1 << MLX5_CAP_GEN(mdev, log_max_cq_sz)) - 1; 603938fe83cSSaeed Mahameed props->max_mr = 1 << MLX5_CAP_GEN(mdev, log_max_mkey); 604938fe83cSSaeed Mahameed props->max_pd = 1 << MLX5_CAP_GEN(mdev, log_max_pd); 605938fe83cSSaeed Mahameed props->max_qp_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_req_qp); 606938fe83cSSaeed Mahameed props->max_qp_init_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_res_qp); 607938fe83cSSaeed Mahameed props->max_srq = 1 << MLX5_CAP_GEN(mdev, log_max_srq); 608938fe83cSSaeed Mahameed props->max_srq_wr = (1 << MLX5_CAP_GEN(mdev, log_max_srq_sz)) - 1; 609938fe83cSSaeed Mahameed props->local_ca_ack_delay = MLX5_CAP_GEN(mdev, local_ca_ack_delay); 610e126ba97SEli Cohen props->max_res_rd_atom = props->max_qp_rd_atom * props->max_qp; 611e126ba97SEli Cohen props->max_srq_sge = max_rq_sg - 1; 612911f4331SSagi Grimberg props->max_fast_reg_page_list_len = 613911f4331SSagi Grimberg 1 << MLX5_CAP_GEN(mdev, log_max_klm_list_size); 614da7525d2SEran Ben Elisha get_atomic_caps(dev, props); 61581bea28fSEli Cohen props->masked_atomic_cap = IB_ATOMIC_NONE; 616938fe83cSSaeed Mahameed props->max_mcast_grp = 1 << MLX5_CAP_GEN(mdev, log_max_mcg); 617938fe83cSSaeed Mahameed props->max_mcast_qp_attach = MLX5_CAP_GEN(mdev, max_qp_mcg); 618e126ba97SEli Cohen props->max_total_mcast_qp_attach = props->max_mcast_qp_attach * 619e126ba97SEli Cohen props->max_mcast_grp; 620e126ba97SEli Cohen props->max_map_per_fmr = INT_MAX; /* no limit in ConnectIB */ 6217c60bcbbSMatan Barak props->hca_core_clock = MLX5_CAP_GEN(mdev, device_frequency_khz); 6227c60bcbbSMatan Barak props->timestamp_mask = 0x7FFFFFFFFFFFFFFFULL; 623e126ba97SEli Cohen 6248cdd312cSHaggai Eran #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING 625938fe83cSSaeed Mahameed if (MLX5_CAP_GEN(mdev, pg)) 6268cdd312cSHaggai Eran props->device_cap_flags |= IB_DEVICE_ON_DEMAND_PAGING; 6278cdd312cSHaggai Eran props->odp_caps = dev->odp_caps; 6288cdd312cSHaggai Eran #endif 6298cdd312cSHaggai Eran 630051f2630SLeon Romanovsky if (MLX5_CAP_GEN(mdev, cd)) 631051f2630SLeon Romanovsky props->device_cap_flags |= IB_DEVICE_CROSS_CHANNEL; 632051f2630SLeon Romanovsky 633eff901d3SEli Cohen if (!mlx5_core_is_pf(mdev)) 634eff901d3SEli Cohen props->device_cap_flags |= IB_DEVICE_VIRTUAL_FUNCTION; 635eff901d3SEli Cohen 63631f69a82SYishai Hadas if (mlx5_ib_port_link_layer(ibdev, 1) == 63731f69a82SYishai Hadas IB_LINK_LAYER_ETHERNET) { 63831f69a82SYishai Hadas props->rss_caps.max_rwq_indirection_tables = 63931f69a82SYishai Hadas 1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt); 64031f69a82SYishai Hadas props->rss_caps.max_rwq_indirection_table_size = 64131f69a82SYishai Hadas 1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt_size); 64231f69a82SYishai Hadas props->rss_caps.supported_qpts = 1 << IB_QPT_RAW_PACKET; 64331f69a82SYishai Hadas props->max_wq_type_rq = 64431f69a82SYishai Hadas 1 << MLX5_CAP_GEN(dev->mdev, log_max_rq); 64531f69a82SYishai Hadas } 64631f69a82SYishai Hadas 647402ca536SBodong Wang if (uhw->outlen) { 648402ca536SBodong Wang err = ib_copy_to_udata(uhw, &resp, resp.response_length); 649402ca536SBodong Wang 650402ca536SBodong Wang if (err) 651402ca536SBodong Wang return err; 652402ca536SBodong Wang } 653402ca536SBodong Wang 6541b5daf11SMajd Dibbiny return 0; 6551b5daf11SMajd Dibbiny } 656e126ba97SEli Cohen 6571b5daf11SMajd Dibbiny enum mlx5_ib_width { 6581b5daf11SMajd Dibbiny MLX5_IB_WIDTH_1X = 1 << 0, 6591b5daf11SMajd Dibbiny MLX5_IB_WIDTH_2X = 1 << 1, 6601b5daf11SMajd Dibbiny MLX5_IB_WIDTH_4X = 1 << 2, 6611b5daf11SMajd Dibbiny MLX5_IB_WIDTH_8X = 1 << 3, 6621b5daf11SMajd Dibbiny MLX5_IB_WIDTH_12X = 1 << 4 6631b5daf11SMajd Dibbiny }; 6641b5daf11SMajd Dibbiny 6651b5daf11SMajd Dibbiny static int translate_active_width(struct ib_device *ibdev, u8 active_width, 6661b5daf11SMajd Dibbiny u8 *ib_width) 6671b5daf11SMajd Dibbiny { 6681b5daf11SMajd Dibbiny struct mlx5_ib_dev *dev = to_mdev(ibdev); 6691b5daf11SMajd Dibbiny int err = 0; 6701b5daf11SMajd Dibbiny 6711b5daf11SMajd Dibbiny if (active_width & MLX5_IB_WIDTH_1X) { 6721b5daf11SMajd Dibbiny *ib_width = IB_WIDTH_1X; 6731b5daf11SMajd Dibbiny } else if (active_width & MLX5_IB_WIDTH_2X) { 6741b5daf11SMajd Dibbiny mlx5_ib_dbg(dev, "active_width %d is not supported by IB spec\n", 6751b5daf11SMajd Dibbiny (int)active_width); 6761b5daf11SMajd Dibbiny err = -EINVAL; 6771b5daf11SMajd Dibbiny } else if (active_width & MLX5_IB_WIDTH_4X) { 6781b5daf11SMajd Dibbiny *ib_width = IB_WIDTH_4X; 6791b5daf11SMajd Dibbiny } else if (active_width & MLX5_IB_WIDTH_8X) { 6801b5daf11SMajd Dibbiny *ib_width = IB_WIDTH_8X; 6811b5daf11SMajd Dibbiny } else if (active_width & MLX5_IB_WIDTH_12X) { 6821b5daf11SMajd Dibbiny *ib_width = IB_WIDTH_12X; 6831b5daf11SMajd Dibbiny } else { 6841b5daf11SMajd Dibbiny mlx5_ib_dbg(dev, "Invalid active_width %d\n", 6851b5daf11SMajd Dibbiny (int)active_width); 6861b5daf11SMajd Dibbiny err = -EINVAL; 6871b5daf11SMajd Dibbiny } 6881b5daf11SMajd Dibbiny 6891b5daf11SMajd Dibbiny return err; 6901b5daf11SMajd Dibbiny } 6911b5daf11SMajd Dibbiny 6921b5daf11SMajd Dibbiny static int mlx5_mtu_to_ib_mtu(int mtu) 6931b5daf11SMajd Dibbiny { 6941b5daf11SMajd Dibbiny switch (mtu) { 6951b5daf11SMajd Dibbiny case 256: return 1; 6961b5daf11SMajd Dibbiny case 512: return 2; 6971b5daf11SMajd Dibbiny case 1024: return 3; 6981b5daf11SMajd Dibbiny case 2048: return 4; 6991b5daf11SMajd Dibbiny case 4096: return 5; 7001b5daf11SMajd Dibbiny default: 7011b5daf11SMajd Dibbiny pr_warn("invalid mtu\n"); 7021b5daf11SMajd Dibbiny return -1; 7031b5daf11SMajd Dibbiny } 7041b5daf11SMajd Dibbiny } 7051b5daf11SMajd Dibbiny 7061b5daf11SMajd Dibbiny enum ib_max_vl_num { 7071b5daf11SMajd Dibbiny __IB_MAX_VL_0 = 1, 7081b5daf11SMajd Dibbiny __IB_MAX_VL_0_1 = 2, 7091b5daf11SMajd Dibbiny __IB_MAX_VL_0_3 = 3, 7101b5daf11SMajd Dibbiny __IB_MAX_VL_0_7 = 4, 7111b5daf11SMajd Dibbiny __IB_MAX_VL_0_14 = 5, 7121b5daf11SMajd Dibbiny }; 7131b5daf11SMajd Dibbiny 7141b5daf11SMajd Dibbiny enum mlx5_vl_hw_cap { 7151b5daf11SMajd Dibbiny MLX5_VL_HW_0 = 1, 7161b5daf11SMajd Dibbiny MLX5_VL_HW_0_1 = 2, 7171b5daf11SMajd Dibbiny MLX5_VL_HW_0_2 = 3, 7181b5daf11SMajd Dibbiny MLX5_VL_HW_0_3 = 4, 7191b5daf11SMajd Dibbiny MLX5_VL_HW_0_4 = 5, 7201b5daf11SMajd Dibbiny MLX5_VL_HW_0_5 = 6, 7211b5daf11SMajd Dibbiny MLX5_VL_HW_0_6 = 7, 7221b5daf11SMajd Dibbiny MLX5_VL_HW_0_7 = 8, 7231b5daf11SMajd Dibbiny MLX5_VL_HW_0_14 = 15 7241b5daf11SMajd Dibbiny }; 7251b5daf11SMajd Dibbiny 7261b5daf11SMajd Dibbiny static int translate_max_vl_num(struct ib_device *ibdev, u8 vl_hw_cap, 7271b5daf11SMajd Dibbiny u8 *max_vl_num) 7281b5daf11SMajd Dibbiny { 7291b5daf11SMajd Dibbiny switch (vl_hw_cap) { 7301b5daf11SMajd Dibbiny case MLX5_VL_HW_0: 7311b5daf11SMajd Dibbiny *max_vl_num = __IB_MAX_VL_0; 7321b5daf11SMajd Dibbiny break; 7331b5daf11SMajd Dibbiny case MLX5_VL_HW_0_1: 7341b5daf11SMajd Dibbiny *max_vl_num = __IB_MAX_VL_0_1; 7351b5daf11SMajd Dibbiny break; 7361b5daf11SMajd Dibbiny case MLX5_VL_HW_0_3: 7371b5daf11SMajd Dibbiny *max_vl_num = __IB_MAX_VL_0_3; 7381b5daf11SMajd Dibbiny break; 7391b5daf11SMajd Dibbiny case MLX5_VL_HW_0_7: 7401b5daf11SMajd Dibbiny *max_vl_num = __IB_MAX_VL_0_7; 7411b5daf11SMajd Dibbiny break; 7421b5daf11SMajd Dibbiny case MLX5_VL_HW_0_14: 7431b5daf11SMajd Dibbiny *max_vl_num = __IB_MAX_VL_0_14; 7441b5daf11SMajd Dibbiny break; 7451b5daf11SMajd Dibbiny 7461b5daf11SMajd Dibbiny default: 7471b5daf11SMajd Dibbiny return -EINVAL; 7481b5daf11SMajd Dibbiny } 7491b5daf11SMajd Dibbiny 7501b5daf11SMajd Dibbiny return 0; 7511b5daf11SMajd Dibbiny } 7521b5daf11SMajd Dibbiny 7531b5daf11SMajd Dibbiny static int mlx5_query_hca_port(struct ib_device *ibdev, u8 port, 7541b5daf11SMajd Dibbiny struct ib_port_attr *props) 7551b5daf11SMajd Dibbiny { 7561b5daf11SMajd Dibbiny struct mlx5_ib_dev *dev = to_mdev(ibdev); 7571b5daf11SMajd Dibbiny struct mlx5_core_dev *mdev = dev->mdev; 7581b5daf11SMajd Dibbiny struct mlx5_hca_vport_context *rep; 759046339eaSSaeed Mahameed u16 max_mtu; 760046339eaSSaeed Mahameed u16 oper_mtu; 7611b5daf11SMajd Dibbiny int err; 7621b5daf11SMajd Dibbiny u8 ib_link_width_oper; 7631b5daf11SMajd Dibbiny u8 vl_hw_cap; 7641b5daf11SMajd Dibbiny 7651b5daf11SMajd Dibbiny rep = kzalloc(sizeof(*rep), GFP_KERNEL); 7661b5daf11SMajd Dibbiny if (!rep) { 7671b5daf11SMajd Dibbiny err = -ENOMEM; 7681b5daf11SMajd Dibbiny goto out; 7691b5daf11SMajd Dibbiny } 7701b5daf11SMajd Dibbiny 7711b5daf11SMajd Dibbiny memset(props, 0, sizeof(*props)); 7721b5daf11SMajd Dibbiny 7731b5daf11SMajd Dibbiny err = mlx5_query_hca_vport_context(mdev, 0, port, 0, rep); 7741b5daf11SMajd Dibbiny if (err) 7751b5daf11SMajd Dibbiny goto out; 7761b5daf11SMajd Dibbiny 7771b5daf11SMajd Dibbiny props->lid = rep->lid; 7781b5daf11SMajd Dibbiny props->lmc = rep->lmc; 7791b5daf11SMajd Dibbiny props->sm_lid = rep->sm_lid; 7801b5daf11SMajd Dibbiny props->sm_sl = rep->sm_sl; 7811b5daf11SMajd Dibbiny props->state = rep->vport_state; 7821b5daf11SMajd Dibbiny props->phys_state = rep->port_physical_state; 7831b5daf11SMajd Dibbiny props->port_cap_flags = rep->cap_mask1; 7841b5daf11SMajd Dibbiny props->gid_tbl_len = mlx5_get_gid_table_len(MLX5_CAP_GEN(mdev, gid_table_size)); 7851b5daf11SMajd Dibbiny props->max_msg_sz = 1 << MLX5_CAP_GEN(mdev, log_max_msg); 7861b5daf11SMajd Dibbiny props->pkey_tbl_len = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, pkey_table_size)); 7871b5daf11SMajd Dibbiny props->bad_pkey_cntr = rep->pkey_violation_counter; 7881b5daf11SMajd Dibbiny props->qkey_viol_cntr = rep->qkey_violation_counter; 7891b5daf11SMajd Dibbiny props->subnet_timeout = rep->subnet_timeout; 7901b5daf11SMajd Dibbiny props->init_type_reply = rep->init_type_reply; 791eff901d3SEli Cohen props->grh_required = rep->grh_required; 7921b5daf11SMajd Dibbiny 7931b5daf11SMajd Dibbiny err = mlx5_query_port_link_width_oper(mdev, &ib_link_width_oper, port); 7941b5daf11SMajd Dibbiny if (err) 7951b5daf11SMajd Dibbiny goto out; 7961b5daf11SMajd Dibbiny 7971b5daf11SMajd Dibbiny err = translate_active_width(ibdev, ib_link_width_oper, 7981b5daf11SMajd Dibbiny &props->active_width); 7991b5daf11SMajd Dibbiny if (err) 8001b5daf11SMajd Dibbiny goto out; 801d5beb7f2SNoa Osherovich err = mlx5_query_port_ib_proto_oper(mdev, &props->active_speed, port); 8021b5daf11SMajd Dibbiny if (err) 8031b5daf11SMajd Dibbiny goto out; 8041b5daf11SMajd Dibbiny 805facc9699SSaeed Mahameed mlx5_query_port_max_mtu(mdev, &max_mtu, port); 8061b5daf11SMajd Dibbiny 8071b5daf11SMajd Dibbiny props->max_mtu = mlx5_mtu_to_ib_mtu(max_mtu); 8081b5daf11SMajd Dibbiny 809facc9699SSaeed Mahameed mlx5_query_port_oper_mtu(mdev, &oper_mtu, port); 8101b5daf11SMajd Dibbiny 8111b5daf11SMajd Dibbiny props->active_mtu = mlx5_mtu_to_ib_mtu(oper_mtu); 8121b5daf11SMajd Dibbiny 8131b5daf11SMajd Dibbiny err = mlx5_query_port_vl_hw_cap(mdev, &vl_hw_cap, port); 8141b5daf11SMajd Dibbiny if (err) 8151b5daf11SMajd Dibbiny goto out; 8161b5daf11SMajd Dibbiny 8171b5daf11SMajd Dibbiny err = translate_max_vl_num(ibdev, vl_hw_cap, 8181b5daf11SMajd Dibbiny &props->max_vl_num); 8191b5daf11SMajd Dibbiny out: 8201b5daf11SMajd Dibbiny kfree(rep); 821e126ba97SEli Cohen return err; 822e126ba97SEli Cohen } 823e126ba97SEli Cohen 824e126ba97SEli Cohen int mlx5_ib_query_port(struct ib_device *ibdev, u8 port, 825e126ba97SEli Cohen struct ib_port_attr *props) 826e126ba97SEli Cohen { 8271b5daf11SMajd Dibbiny switch (mlx5_get_vport_access_method(ibdev)) { 8281b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_MAD: 8291b5daf11SMajd Dibbiny return mlx5_query_mad_ifc_port(ibdev, port, props); 830e126ba97SEli Cohen 8311b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_HCA: 8321b5daf11SMajd Dibbiny return mlx5_query_hca_port(ibdev, port, props); 8331b5daf11SMajd Dibbiny 8343f89a643SAchiad Shochat case MLX5_VPORT_ACCESS_METHOD_NIC: 8353f89a643SAchiad Shochat return mlx5_query_port_roce(ibdev, port, props); 8363f89a643SAchiad Shochat 8371b5daf11SMajd Dibbiny default: 838e126ba97SEli Cohen return -EINVAL; 839e126ba97SEli Cohen } 840e126ba97SEli Cohen } 841e126ba97SEli Cohen 842e126ba97SEli Cohen static int mlx5_ib_query_gid(struct ib_device *ibdev, u8 port, int index, 843e126ba97SEli Cohen union ib_gid *gid) 844e126ba97SEli Cohen { 8451b5daf11SMajd Dibbiny struct mlx5_ib_dev *dev = to_mdev(ibdev); 8461b5daf11SMajd Dibbiny struct mlx5_core_dev *mdev = dev->mdev; 847e126ba97SEli Cohen 8481b5daf11SMajd Dibbiny switch (mlx5_get_vport_access_method(ibdev)) { 8491b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_MAD: 8501b5daf11SMajd Dibbiny return mlx5_query_mad_ifc_gids(ibdev, port, index, gid); 851e126ba97SEli Cohen 8521b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_HCA: 8531b5daf11SMajd Dibbiny return mlx5_query_hca_vport_gid(mdev, 0, port, 0, index, gid); 854e126ba97SEli Cohen 8551b5daf11SMajd Dibbiny default: 8561b5daf11SMajd Dibbiny return -EINVAL; 8571b5daf11SMajd Dibbiny } 858e126ba97SEli Cohen 859e126ba97SEli Cohen } 860e126ba97SEli Cohen 861e126ba97SEli Cohen static int mlx5_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index, 862e126ba97SEli Cohen u16 *pkey) 863e126ba97SEli Cohen { 8641b5daf11SMajd Dibbiny struct mlx5_ib_dev *dev = to_mdev(ibdev); 8651b5daf11SMajd Dibbiny struct mlx5_core_dev *mdev = dev->mdev; 866e126ba97SEli Cohen 8671b5daf11SMajd Dibbiny switch (mlx5_get_vport_access_method(ibdev)) { 8681b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_MAD: 8691b5daf11SMajd Dibbiny return mlx5_query_mad_ifc_pkey(ibdev, port, index, pkey); 870e126ba97SEli Cohen 8711b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_HCA: 8721b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_NIC: 8731b5daf11SMajd Dibbiny return mlx5_query_hca_vport_pkey(mdev, 0, port, 0, index, 8741b5daf11SMajd Dibbiny pkey); 8751b5daf11SMajd Dibbiny default: 8761b5daf11SMajd Dibbiny return -EINVAL; 877e126ba97SEli Cohen } 8781b5daf11SMajd Dibbiny } 879e126ba97SEli Cohen 880e126ba97SEli Cohen static int mlx5_ib_modify_device(struct ib_device *ibdev, int mask, 881e126ba97SEli Cohen struct ib_device_modify *props) 882e126ba97SEli Cohen { 883e126ba97SEli Cohen struct mlx5_ib_dev *dev = to_mdev(ibdev); 884e126ba97SEli Cohen struct mlx5_reg_node_desc in; 885e126ba97SEli Cohen struct mlx5_reg_node_desc out; 886e126ba97SEli Cohen int err; 887e126ba97SEli Cohen 888e126ba97SEli Cohen if (mask & ~IB_DEVICE_MODIFY_NODE_DESC) 889e126ba97SEli Cohen return -EOPNOTSUPP; 890e126ba97SEli Cohen 891e126ba97SEli Cohen if (!(mask & IB_DEVICE_MODIFY_NODE_DESC)) 892e126ba97SEli Cohen return 0; 893e126ba97SEli Cohen 894e126ba97SEli Cohen /* 895e126ba97SEli Cohen * If possible, pass node desc to FW, so it can generate 896e126ba97SEli Cohen * a 144 trap. If cmd fails, just ignore. 897e126ba97SEli Cohen */ 898e126ba97SEli Cohen memcpy(&in, props->node_desc, 64); 8999603b61dSJack Morgenstein err = mlx5_core_access_reg(dev->mdev, &in, sizeof(in), &out, 900e126ba97SEli Cohen sizeof(out), MLX5_REG_NODE_DESC, 0, 1); 901e126ba97SEli Cohen if (err) 902e126ba97SEli Cohen return err; 903e126ba97SEli Cohen 904e126ba97SEli Cohen memcpy(ibdev->node_desc, props->node_desc, 64); 905e126ba97SEli Cohen 906e126ba97SEli Cohen return err; 907e126ba97SEli Cohen } 908e126ba97SEli Cohen 909e126ba97SEli Cohen static int mlx5_ib_modify_port(struct ib_device *ibdev, u8 port, int mask, 910e126ba97SEli Cohen struct ib_port_modify *props) 911e126ba97SEli Cohen { 912e126ba97SEli Cohen struct mlx5_ib_dev *dev = to_mdev(ibdev); 913e126ba97SEli Cohen struct ib_port_attr attr; 914e126ba97SEli Cohen u32 tmp; 915e126ba97SEli Cohen int err; 916e126ba97SEli Cohen 917e126ba97SEli Cohen mutex_lock(&dev->cap_mask_mutex); 918e126ba97SEli Cohen 919e126ba97SEli Cohen err = mlx5_ib_query_port(ibdev, port, &attr); 920e126ba97SEli Cohen if (err) 921e126ba97SEli Cohen goto out; 922e126ba97SEli Cohen 923e126ba97SEli Cohen tmp = (attr.port_cap_flags | props->set_port_cap_mask) & 924e126ba97SEli Cohen ~props->clr_port_cap_mask; 925e126ba97SEli Cohen 9269603b61dSJack Morgenstein err = mlx5_set_port_caps(dev->mdev, port, tmp); 927e126ba97SEli Cohen 928e126ba97SEli Cohen out: 929e126ba97SEli Cohen mutex_unlock(&dev->cap_mask_mutex); 930e126ba97SEli Cohen return err; 931e126ba97SEli Cohen } 932e126ba97SEli Cohen 933e126ba97SEli Cohen static struct ib_ucontext *mlx5_ib_alloc_ucontext(struct ib_device *ibdev, 934e126ba97SEli Cohen struct ib_udata *udata) 935e126ba97SEli Cohen { 936e126ba97SEli Cohen struct mlx5_ib_dev *dev = to_mdev(ibdev); 937b368d7cbSMatan Barak struct mlx5_ib_alloc_ucontext_req_v2 req = {}; 938b368d7cbSMatan Barak struct mlx5_ib_alloc_ucontext_resp resp = {}; 939e126ba97SEli Cohen struct mlx5_ib_ucontext *context; 940e126ba97SEli Cohen struct mlx5_uuar_info *uuari; 941e126ba97SEli Cohen struct mlx5_uar *uars; 942c1be5232SEli Cohen int gross_uuars; 943e126ba97SEli Cohen int num_uars; 94478c0f98cSEli Cohen int ver; 945e126ba97SEli Cohen int uuarn; 946e126ba97SEli Cohen int err; 947e126ba97SEli Cohen int i; 948f241e749SJack Morgenstein size_t reqlen; 949a168a41cSMajd Dibbiny size_t min_req_v2 = offsetof(struct mlx5_ib_alloc_ucontext_req_v2, 950a168a41cSMajd Dibbiny max_cqe_version); 951e126ba97SEli Cohen 952e126ba97SEli Cohen if (!dev->ib_active) 953e126ba97SEli Cohen return ERR_PTR(-EAGAIN); 954e126ba97SEli Cohen 955dfbee859SHaggai Abramovsky if (udata->inlen < sizeof(struct ib_uverbs_cmd_hdr)) 956dfbee859SHaggai Abramovsky return ERR_PTR(-EINVAL); 957dfbee859SHaggai Abramovsky 95878c0f98cSEli Cohen reqlen = udata->inlen - sizeof(struct ib_uverbs_cmd_hdr); 95978c0f98cSEli Cohen if (reqlen == sizeof(struct mlx5_ib_alloc_ucontext_req)) 96078c0f98cSEli Cohen ver = 0; 961a168a41cSMajd Dibbiny else if (reqlen >= min_req_v2) 96278c0f98cSEli Cohen ver = 2; 96378c0f98cSEli Cohen else 96478c0f98cSEli Cohen return ERR_PTR(-EINVAL); 96578c0f98cSEli Cohen 966b368d7cbSMatan Barak err = ib_copy_from_udata(&req, udata, min(reqlen, sizeof(req))); 967e126ba97SEli Cohen if (err) 968e126ba97SEli Cohen return ERR_PTR(err); 969e126ba97SEli Cohen 970b368d7cbSMatan Barak if (req.flags) 97178c0f98cSEli Cohen return ERR_PTR(-EINVAL); 97278c0f98cSEli Cohen 973e126ba97SEli Cohen if (req.total_num_uuars > MLX5_MAX_UUARS) 974e126ba97SEli Cohen return ERR_PTR(-ENOMEM); 975e126ba97SEli Cohen 976e126ba97SEli Cohen if (req.total_num_uuars == 0) 977e126ba97SEli Cohen return ERR_PTR(-EINVAL); 978e126ba97SEli Cohen 979f72300c5SHaggai Abramovsky if (req.comp_mask || req.reserved0 || req.reserved1 || req.reserved2) 980b368d7cbSMatan Barak return ERR_PTR(-EOPNOTSUPP); 981b368d7cbSMatan Barak 982b368d7cbSMatan Barak if (reqlen > sizeof(req) && 983b368d7cbSMatan Barak !ib_is_udata_cleared(udata, sizeof(req), 984dfbee859SHaggai Abramovsky reqlen - sizeof(req))) 985b368d7cbSMatan Barak return ERR_PTR(-EOPNOTSUPP); 986b368d7cbSMatan Barak 987c1be5232SEli Cohen req.total_num_uuars = ALIGN(req.total_num_uuars, 988c1be5232SEli Cohen MLX5_NON_FP_BF_REGS_PER_PAGE); 989e126ba97SEli Cohen if (req.num_low_latency_uuars > req.total_num_uuars - 1) 990e126ba97SEli Cohen return ERR_PTR(-EINVAL); 991e126ba97SEli Cohen 992c1be5232SEli Cohen num_uars = req.total_num_uuars / MLX5_NON_FP_BF_REGS_PER_PAGE; 993c1be5232SEli Cohen gross_uuars = num_uars * MLX5_BF_REGS_PER_PAGE; 994938fe83cSSaeed Mahameed resp.qp_tab_size = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp); 9952cc6ad5fSNoa Osherovich if (mlx5_core_is_pf(dev->mdev) && MLX5_CAP_GEN(dev->mdev, bf)) 996938fe83cSSaeed Mahameed resp.bf_reg_size = 1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size); 997e126ba97SEli Cohen resp.cache_line_size = L1_CACHE_BYTES; 998938fe83cSSaeed Mahameed resp.max_sq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq); 999938fe83cSSaeed Mahameed resp.max_rq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq); 1000938fe83cSSaeed Mahameed resp.max_send_wqebb = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz); 1001938fe83cSSaeed Mahameed resp.max_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz); 1002938fe83cSSaeed Mahameed resp.max_srq_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz); 1003f72300c5SHaggai Abramovsky resp.cqe_version = min_t(__u8, 1004f72300c5SHaggai Abramovsky (__u8)MLX5_CAP_GEN(dev->mdev, cqe_version), 1005f72300c5SHaggai Abramovsky req.max_cqe_version); 1006b368d7cbSMatan Barak resp.response_length = min(offsetof(typeof(resp), response_length) + 1007b368d7cbSMatan Barak sizeof(resp.response_length), udata->outlen); 1008e126ba97SEli Cohen 1009e126ba97SEli Cohen context = kzalloc(sizeof(*context), GFP_KERNEL); 1010e126ba97SEli Cohen if (!context) 1011e126ba97SEli Cohen return ERR_PTR(-ENOMEM); 1012e126ba97SEli Cohen 1013e126ba97SEli Cohen uuari = &context->uuari; 1014e126ba97SEli Cohen mutex_init(&uuari->lock); 1015e126ba97SEli Cohen uars = kcalloc(num_uars, sizeof(*uars), GFP_KERNEL); 1016e126ba97SEli Cohen if (!uars) { 1017e126ba97SEli Cohen err = -ENOMEM; 1018e126ba97SEli Cohen goto out_ctx; 1019e126ba97SEli Cohen } 1020e126ba97SEli Cohen 1021c1be5232SEli Cohen uuari->bitmap = kcalloc(BITS_TO_LONGS(gross_uuars), 1022e126ba97SEli Cohen sizeof(*uuari->bitmap), 1023e126ba97SEli Cohen GFP_KERNEL); 1024e126ba97SEli Cohen if (!uuari->bitmap) { 1025e126ba97SEli Cohen err = -ENOMEM; 1026e126ba97SEli Cohen goto out_uar_ctx; 1027e126ba97SEli Cohen } 1028e126ba97SEli Cohen /* 1029e126ba97SEli Cohen * clear all fast path uuars 1030e126ba97SEli Cohen */ 1031c1be5232SEli Cohen for (i = 0; i < gross_uuars; i++) { 1032e126ba97SEli Cohen uuarn = i & 3; 1033e126ba97SEli Cohen if (uuarn == 2 || uuarn == 3) 1034e126ba97SEli Cohen set_bit(i, uuari->bitmap); 1035e126ba97SEli Cohen } 1036e126ba97SEli Cohen 1037c1be5232SEli Cohen uuari->count = kcalloc(gross_uuars, sizeof(*uuari->count), GFP_KERNEL); 1038e126ba97SEli Cohen if (!uuari->count) { 1039e126ba97SEli Cohen err = -ENOMEM; 1040e126ba97SEli Cohen goto out_bitmap; 1041e126ba97SEli Cohen } 1042e126ba97SEli Cohen 1043e126ba97SEli Cohen for (i = 0; i < num_uars; i++) { 10449603b61dSJack Morgenstein err = mlx5_cmd_alloc_uar(dev->mdev, &uars[i].index); 1045e126ba97SEli Cohen if (err) 1046e126ba97SEli Cohen goto out_count; 1047e126ba97SEli Cohen } 1048e126ba97SEli Cohen 1049b4cfe447SHaggai Eran #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING 1050b4cfe447SHaggai Eran context->ibucontext.invalidate_range = &mlx5_ib_invalidate_range; 1051b4cfe447SHaggai Eran #endif 1052b4cfe447SHaggai Eran 1053146d2f1aSmajd@mellanox.com if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain)) { 1054146d2f1aSmajd@mellanox.com err = mlx5_core_alloc_transport_domain(dev->mdev, 1055146d2f1aSmajd@mellanox.com &context->tdn); 1056146d2f1aSmajd@mellanox.com if (err) 1057146d2f1aSmajd@mellanox.com goto out_uars; 1058146d2f1aSmajd@mellanox.com } 1059146d2f1aSmajd@mellanox.com 10607c2344c3SMaor Gottlieb INIT_LIST_HEAD(&context->vma_private_list); 1061e126ba97SEli Cohen INIT_LIST_HEAD(&context->db_page_list); 1062e126ba97SEli Cohen mutex_init(&context->db_page_mutex); 1063e126ba97SEli Cohen 1064e126ba97SEli Cohen resp.tot_uuars = req.total_num_uuars; 1065938fe83cSSaeed Mahameed resp.num_ports = MLX5_CAP_GEN(dev->mdev, num_ports); 1066b368d7cbSMatan Barak 1067f72300c5SHaggai Abramovsky if (field_avail(typeof(resp), cqe_version, udata->outlen)) 1068f72300c5SHaggai Abramovsky resp.response_length += sizeof(resp.cqe_version); 1069b368d7cbSMatan Barak 1070402ca536SBodong Wang if (field_avail(typeof(resp), cmds_supp_uhw, udata->outlen)) { 1071402ca536SBodong Wang resp.cmds_supp_uhw |= MLX5_USER_CMDS_SUPP_UHW_QUERY_DEVICE; 1072402ca536SBodong Wang resp.response_length += sizeof(resp.cmds_supp_uhw); 1073402ca536SBodong Wang } 1074402ca536SBodong Wang 1075bc5c6eedSNoa Osherovich /* 1076bc5c6eedSNoa Osherovich * We don't want to expose information from the PCI bar that is located 1077bc5c6eedSNoa Osherovich * after 4096 bytes, so if the arch only supports larger pages, let's 1078bc5c6eedSNoa Osherovich * pretend we don't support reading the HCA's core clock. This is also 1079bc5c6eedSNoa Osherovich * forced by mmap function. 1080bc5c6eedSNoa Osherovich */ 1081bc5c6eedSNoa Osherovich if (PAGE_SIZE <= 4096 && 1082bc5c6eedSNoa Osherovich field_avail(typeof(resp), hca_core_clock_offset, udata->outlen)) { 1083b368d7cbSMatan Barak resp.comp_mask |= 1084b368d7cbSMatan Barak MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_CORE_CLOCK_OFFSET; 1085b368d7cbSMatan Barak resp.hca_core_clock_offset = 1086b368d7cbSMatan Barak offsetof(struct mlx5_init_seg, internal_timer_h) % 1087b368d7cbSMatan Barak PAGE_SIZE; 1088f72300c5SHaggai Abramovsky resp.response_length += sizeof(resp.hca_core_clock_offset) + 1089402ca536SBodong Wang sizeof(resp.reserved2); 1090b368d7cbSMatan Barak } 1091b368d7cbSMatan Barak 1092b368d7cbSMatan Barak err = ib_copy_to_udata(udata, &resp, resp.response_length); 1093e126ba97SEli Cohen if (err) 1094146d2f1aSmajd@mellanox.com goto out_td; 1095e126ba97SEli Cohen 109678c0f98cSEli Cohen uuari->ver = ver; 1097e126ba97SEli Cohen uuari->num_low_latency_uuars = req.num_low_latency_uuars; 1098e126ba97SEli Cohen uuari->uars = uars; 1099e126ba97SEli Cohen uuari->num_uars = num_uars; 1100f72300c5SHaggai Abramovsky context->cqe_version = resp.cqe_version; 1101f72300c5SHaggai Abramovsky 1102e126ba97SEli Cohen return &context->ibucontext; 1103e126ba97SEli Cohen 1104146d2f1aSmajd@mellanox.com out_td: 1105146d2f1aSmajd@mellanox.com if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain)) 1106146d2f1aSmajd@mellanox.com mlx5_core_dealloc_transport_domain(dev->mdev, context->tdn); 1107146d2f1aSmajd@mellanox.com 1108e126ba97SEli Cohen out_uars: 1109e126ba97SEli Cohen for (i--; i >= 0; i--) 11109603b61dSJack Morgenstein mlx5_cmd_free_uar(dev->mdev, uars[i].index); 1111e126ba97SEli Cohen out_count: 1112e126ba97SEli Cohen kfree(uuari->count); 1113e126ba97SEli Cohen 1114e126ba97SEli Cohen out_bitmap: 1115e126ba97SEli Cohen kfree(uuari->bitmap); 1116e126ba97SEli Cohen 1117e126ba97SEli Cohen out_uar_ctx: 1118e126ba97SEli Cohen kfree(uars); 1119e126ba97SEli Cohen 1120e126ba97SEli Cohen out_ctx: 1121e126ba97SEli Cohen kfree(context); 1122e126ba97SEli Cohen return ERR_PTR(err); 1123e126ba97SEli Cohen } 1124e126ba97SEli Cohen 1125e126ba97SEli Cohen static int mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext) 1126e126ba97SEli Cohen { 1127e126ba97SEli Cohen struct mlx5_ib_ucontext *context = to_mucontext(ibcontext); 1128e126ba97SEli Cohen struct mlx5_ib_dev *dev = to_mdev(ibcontext->device); 1129e126ba97SEli Cohen struct mlx5_uuar_info *uuari = &context->uuari; 1130e126ba97SEli Cohen int i; 1131e126ba97SEli Cohen 1132146d2f1aSmajd@mellanox.com if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain)) 1133146d2f1aSmajd@mellanox.com mlx5_core_dealloc_transport_domain(dev->mdev, context->tdn); 1134146d2f1aSmajd@mellanox.com 1135e126ba97SEli Cohen for (i = 0; i < uuari->num_uars; i++) { 11369603b61dSJack Morgenstein if (mlx5_cmd_free_uar(dev->mdev, uuari->uars[i].index)) 1137e126ba97SEli Cohen mlx5_ib_warn(dev, "failed to free UAR 0x%x\n", uuari->uars[i].index); 1138e126ba97SEli Cohen } 1139e126ba97SEli Cohen 1140e126ba97SEli Cohen kfree(uuari->count); 1141e126ba97SEli Cohen kfree(uuari->bitmap); 1142e126ba97SEli Cohen kfree(uuari->uars); 1143e126ba97SEli Cohen kfree(context); 1144e126ba97SEli Cohen 1145e126ba97SEli Cohen return 0; 1146e126ba97SEli Cohen } 1147e126ba97SEli Cohen 1148e126ba97SEli Cohen static phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev, int index) 1149e126ba97SEli Cohen { 11509603b61dSJack Morgenstein return (pci_resource_start(dev->mdev->pdev, 0) >> PAGE_SHIFT) + index; 1151e126ba97SEli Cohen } 1152e126ba97SEli Cohen 1153e126ba97SEli Cohen static int get_command(unsigned long offset) 1154e126ba97SEli Cohen { 1155e126ba97SEli Cohen return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK; 1156e126ba97SEli Cohen } 1157e126ba97SEli Cohen 1158e126ba97SEli Cohen static int get_arg(unsigned long offset) 1159e126ba97SEli Cohen { 1160e126ba97SEli Cohen return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1); 1161e126ba97SEli Cohen } 1162e126ba97SEli Cohen 1163e126ba97SEli Cohen static int get_index(unsigned long offset) 1164e126ba97SEli Cohen { 1165e126ba97SEli Cohen return get_arg(offset); 1166e126ba97SEli Cohen } 1167e126ba97SEli Cohen 11687c2344c3SMaor Gottlieb static void mlx5_ib_vma_open(struct vm_area_struct *area) 11697c2344c3SMaor Gottlieb { 11707c2344c3SMaor Gottlieb /* vma_open is called when a new VMA is created on top of our VMA. This 11717c2344c3SMaor Gottlieb * is done through either mremap flow or split_vma (usually due to 11727c2344c3SMaor Gottlieb * mlock, madvise, munmap, etc.) We do not support a clone of the VMA, 11737c2344c3SMaor Gottlieb * as this VMA is strongly hardware related. Therefore we set the 11747c2344c3SMaor Gottlieb * vm_ops of the newly created/cloned VMA to NULL, to prevent it from 11757c2344c3SMaor Gottlieb * calling us again and trying to do incorrect actions. We assume that 11767c2344c3SMaor Gottlieb * the original VMA size is exactly a single page, and therefore all 11777c2344c3SMaor Gottlieb * "splitting" operation will not happen to it. 11787c2344c3SMaor Gottlieb */ 11797c2344c3SMaor Gottlieb area->vm_ops = NULL; 11807c2344c3SMaor Gottlieb } 11817c2344c3SMaor Gottlieb 11827c2344c3SMaor Gottlieb static void mlx5_ib_vma_close(struct vm_area_struct *area) 11837c2344c3SMaor Gottlieb { 11847c2344c3SMaor Gottlieb struct mlx5_ib_vma_private_data *mlx5_ib_vma_priv_data; 11857c2344c3SMaor Gottlieb 11867c2344c3SMaor Gottlieb /* It's guaranteed that all VMAs opened on a FD are closed before the 11877c2344c3SMaor Gottlieb * file itself is closed, therefore no sync is needed with the regular 11887c2344c3SMaor Gottlieb * closing flow. (e.g. mlx5 ib_dealloc_ucontext) 11897c2344c3SMaor Gottlieb * However need a sync with accessing the vma as part of 11907c2344c3SMaor Gottlieb * mlx5_ib_disassociate_ucontext. 11917c2344c3SMaor Gottlieb * The close operation is usually called under mm->mmap_sem except when 11927c2344c3SMaor Gottlieb * process is exiting. 11937c2344c3SMaor Gottlieb * The exiting case is handled explicitly as part of 11947c2344c3SMaor Gottlieb * mlx5_ib_disassociate_ucontext. 11957c2344c3SMaor Gottlieb */ 11967c2344c3SMaor Gottlieb mlx5_ib_vma_priv_data = (struct mlx5_ib_vma_private_data *)area->vm_private_data; 11977c2344c3SMaor Gottlieb 11987c2344c3SMaor Gottlieb /* setting the vma context pointer to null in the mlx5_ib driver's 11997c2344c3SMaor Gottlieb * private data, to protect a race condition in 12007c2344c3SMaor Gottlieb * mlx5_ib_disassociate_ucontext(). 12017c2344c3SMaor Gottlieb */ 12027c2344c3SMaor Gottlieb mlx5_ib_vma_priv_data->vma = NULL; 12037c2344c3SMaor Gottlieb list_del(&mlx5_ib_vma_priv_data->list); 12047c2344c3SMaor Gottlieb kfree(mlx5_ib_vma_priv_data); 12057c2344c3SMaor Gottlieb } 12067c2344c3SMaor Gottlieb 12077c2344c3SMaor Gottlieb static const struct vm_operations_struct mlx5_ib_vm_ops = { 12087c2344c3SMaor Gottlieb .open = mlx5_ib_vma_open, 12097c2344c3SMaor Gottlieb .close = mlx5_ib_vma_close 12107c2344c3SMaor Gottlieb }; 12117c2344c3SMaor Gottlieb 12127c2344c3SMaor Gottlieb static int mlx5_ib_set_vma_data(struct vm_area_struct *vma, 12137c2344c3SMaor Gottlieb struct mlx5_ib_ucontext *ctx) 12147c2344c3SMaor Gottlieb { 12157c2344c3SMaor Gottlieb struct mlx5_ib_vma_private_data *vma_prv; 12167c2344c3SMaor Gottlieb struct list_head *vma_head = &ctx->vma_private_list; 12177c2344c3SMaor Gottlieb 12187c2344c3SMaor Gottlieb vma_prv = kzalloc(sizeof(*vma_prv), GFP_KERNEL); 12197c2344c3SMaor Gottlieb if (!vma_prv) 12207c2344c3SMaor Gottlieb return -ENOMEM; 12217c2344c3SMaor Gottlieb 12227c2344c3SMaor Gottlieb vma_prv->vma = vma; 12237c2344c3SMaor Gottlieb vma->vm_private_data = vma_prv; 12247c2344c3SMaor Gottlieb vma->vm_ops = &mlx5_ib_vm_ops; 12257c2344c3SMaor Gottlieb 12267c2344c3SMaor Gottlieb list_add(&vma_prv->list, vma_head); 12277c2344c3SMaor Gottlieb 12287c2344c3SMaor Gottlieb return 0; 12297c2344c3SMaor Gottlieb } 12307c2344c3SMaor Gottlieb 12317c2344c3SMaor Gottlieb static void mlx5_ib_disassociate_ucontext(struct ib_ucontext *ibcontext) 12327c2344c3SMaor Gottlieb { 12337c2344c3SMaor Gottlieb int ret; 12347c2344c3SMaor Gottlieb struct vm_area_struct *vma; 12357c2344c3SMaor Gottlieb struct mlx5_ib_vma_private_data *vma_private, *n; 12367c2344c3SMaor Gottlieb struct mlx5_ib_ucontext *context = to_mucontext(ibcontext); 12377c2344c3SMaor Gottlieb struct task_struct *owning_process = NULL; 12387c2344c3SMaor Gottlieb struct mm_struct *owning_mm = NULL; 12397c2344c3SMaor Gottlieb 12407c2344c3SMaor Gottlieb owning_process = get_pid_task(ibcontext->tgid, PIDTYPE_PID); 12417c2344c3SMaor Gottlieb if (!owning_process) 12427c2344c3SMaor Gottlieb return; 12437c2344c3SMaor Gottlieb 12447c2344c3SMaor Gottlieb owning_mm = get_task_mm(owning_process); 12457c2344c3SMaor Gottlieb if (!owning_mm) { 12467c2344c3SMaor Gottlieb pr_info("no mm, disassociate ucontext is pending task termination\n"); 12477c2344c3SMaor Gottlieb while (1) { 12487c2344c3SMaor Gottlieb put_task_struct(owning_process); 12497c2344c3SMaor Gottlieb usleep_range(1000, 2000); 12507c2344c3SMaor Gottlieb owning_process = get_pid_task(ibcontext->tgid, 12517c2344c3SMaor Gottlieb PIDTYPE_PID); 12527c2344c3SMaor Gottlieb if (!owning_process || 12537c2344c3SMaor Gottlieb owning_process->state == TASK_DEAD) { 12547c2344c3SMaor Gottlieb pr_info("disassociate ucontext done, task was terminated\n"); 12557c2344c3SMaor Gottlieb /* in case task was dead need to release the 12567c2344c3SMaor Gottlieb * task struct. 12577c2344c3SMaor Gottlieb */ 12587c2344c3SMaor Gottlieb if (owning_process) 12597c2344c3SMaor Gottlieb put_task_struct(owning_process); 12607c2344c3SMaor Gottlieb return; 12617c2344c3SMaor Gottlieb } 12627c2344c3SMaor Gottlieb } 12637c2344c3SMaor Gottlieb } 12647c2344c3SMaor Gottlieb 12657c2344c3SMaor Gottlieb /* need to protect from a race on closing the vma as part of 12667c2344c3SMaor Gottlieb * mlx5_ib_vma_close. 12677c2344c3SMaor Gottlieb */ 12687c2344c3SMaor Gottlieb down_read(&owning_mm->mmap_sem); 12697c2344c3SMaor Gottlieb list_for_each_entry_safe(vma_private, n, &context->vma_private_list, 12707c2344c3SMaor Gottlieb list) { 12717c2344c3SMaor Gottlieb vma = vma_private->vma; 12727c2344c3SMaor Gottlieb ret = zap_vma_ptes(vma, vma->vm_start, 12737c2344c3SMaor Gottlieb PAGE_SIZE); 12747c2344c3SMaor Gottlieb WARN_ONCE(ret, "%s: zap_vma_ptes failed", __func__); 12757c2344c3SMaor Gottlieb /* context going to be destroyed, should 12767c2344c3SMaor Gottlieb * not access ops any more. 12777c2344c3SMaor Gottlieb */ 12787c2344c3SMaor Gottlieb vma->vm_ops = NULL; 12797c2344c3SMaor Gottlieb list_del(&vma_private->list); 12807c2344c3SMaor Gottlieb kfree(vma_private); 12817c2344c3SMaor Gottlieb } 12827c2344c3SMaor Gottlieb up_read(&owning_mm->mmap_sem); 12837c2344c3SMaor Gottlieb mmput(owning_mm); 12847c2344c3SMaor Gottlieb put_task_struct(owning_process); 12857c2344c3SMaor Gottlieb } 12867c2344c3SMaor Gottlieb 128737aa5c36SGuy Levi static inline char *mmap_cmd2str(enum mlx5_ib_mmap_cmd cmd) 1288e126ba97SEli Cohen { 128937aa5c36SGuy Levi switch (cmd) { 129037aa5c36SGuy Levi case MLX5_IB_MMAP_WC_PAGE: 129137aa5c36SGuy Levi return "WC"; 1292e126ba97SEli Cohen case MLX5_IB_MMAP_REGULAR_PAGE: 129337aa5c36SGuy Levi return "best effort WC"; 129437aa5c36SGuy Levi case MLX5_IB_MMAP_NC_PAGE: 129537aa5c36SGuy Levi return "NC"; 129637aa5c36SGuy Levi default: 129737aa5c36SGuy Levi return NULL; 129837aa5c36SGuy Levi } 129937aa5c36SGuy Levi } 130037aa5c36SGuy Levi 130137aa5c36SGuy Levi static int uar_mmap(struct mlx5_ib_dev *dev, enum mlx5_ib_mmap_cmd cmd, 13027c2344c3SMaor Gottlieb struct vm_area_struct *vma, 13037c2344c3SMaor Gottlieb struct mlx5_ib_ucontext *context) 130437aa5c36SGuy Levi { 13057c2344c3SMaor Gottlieb struct mlx5_uuar_info *uuari = &context->uuari; 130637aa5c36SGuy Levi int err; 130737aa5c36SGuy Levi unsigned long idx; 130837aa5c36SGuy Levi phys_addr_t pfn, pa; 130937aa5c36SGuy Levi pgprot_t prot; 131037aa5c36SGuy Levi 131137aa5c36SGuy Levi switch (cmd) { 131237aa5c36SGuy Levi case MLX5_IB_MMAP_WC_PAGE: 131337aa5c36SGuy Levi /* Some architectures don't support WC memory */ 131437aa5c36SGuy Levi #if defined(CONFIG_X86) 131537aa5c36SGuy Levi if (!pat_enabled()) 131637aa5c36SGuy Levi return -EPERM; 131737aa5c36SGuy Levi #elif !(defined(CONFIG_PPC) || (defined(CONFIG_ARM) && defined(CONFIG_MMU))) 131837aa5c36SGuy Levi return -EPERM; 131937aa5c36SGuy Levi #endif 132037aa5c36SGuy Levi /* fall through */ 132137aa5c36SGuy Levi case MLX5_IB_MMAP_REGULAR_PAGE: 132237aa5c36SGuy Levi /* For MLX5_IB_MMAP_REGULAR_PAGE do the best effort to get WC */ 132337aa5c36SGuy Levi prot = pgprot_writecombine(vma->vm_page_prot); 132437aa5c36SGuy Levi break; 132537aa5c36SGuy Levi case MLX5_IB_MMAP_NC_PAGE: 132637aa5c36SGuy Levi prot = pgprot_noncached(vma->vm_page_prot); 132737aa5c36SGuy Levi break; 132837aa5c36SGuy Levi default: 132937aa5c36SGuy Levi return -EINVAL; 133037aa5c36SGuy Levi } 133137aa5c36SGuy Levi 1332e126ba97SEli Cohen if (vma->vm_end - vma->vm_start != PAGE_SIZE) 1333e126ba97SEli Cohen return -EINVAL; 1334e126ba97SEli Cohen 1335e126ba97SEli Cohen idx = get_index(vma->vm_pgoff); 13361c3ce90dSEli Cohen if (idx >= uuari->num_uars) 13371c3ce90dSEli Cohen return -EINVAL; 13381c3ce90dSEli Cohen 1339e126ba97SEli Cohen pfn = uar_index2pfn(dev, uuari->uars[idx].index); 134037aa5c36SGuy Levi mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn %pa\n", idx, &pfn); 1341e126ba97SEli Cohen 134237aa5c36SGuy Levi vma->vm_page_prot = prot; 134337aa5c36SGuy Levi err = io_remap_pfn_range(vma, vma->vm_start, pfn, 134437aa5c36SGuy Levi PAGE_SIZE, vma->vm_page_prot); 134537aa5c36SGuy Levi if (err) { 134637aa5c36SGuy Levi mlx5_ib_err(dev, "io_remap_pfn_range failed with error=%d, vm_start=0x%lx, pfn=%pa, mmap_cmd=%s\n", 134737aa5c36SGuy Levi err, vma->vm_start, &pfn, mmap_cmd2str(cmd)); 1348e126ba97SEli Cohen return -EAGAIN; 134937aa5c36SGuy Levi } 1350e126ba97SEli Cohen 135137aa5c36SGuy Levi pa = pfn << PAGE_SHIFT; 135237aa5c36SGuy Levi mlx5_ib_dbg(dev, "mapped %s at 0x%lx, PA %pa\n", mmap_cmd2str(cmd), 135337aa5c36SGuy Levi vma->vm_start, &pa); 135437aa5c36SGuy Levi 13557c2344c3SMaor Gottlieb return mlx5_ib_set_vma_data(vma, context); 135637aa5c36SGuy Levi } 135737aa5c36SGuy Levi 135837aa5c36SGuy Levi static int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma) 135937aa5c36SGuy Levi { 136037aa5c36SGuy Levi struct mlx5_ib_ucontext *context = to_mucontext(ibcontext); 136137aa5c36SGuy Levi struct mlx5_ib_dev *dev = to_mdev(ibcontext->device); 136237aa5c36SGuy Levi unsigned long command; 136337aa5c36SGuy Levi phys_addr_t pfn; 136437aa5c36SGuy Levi 136537aa5c36SGuy Levi command = get_command(vma->vm_pgoff); 136637aa5c36SGuy Levi switch (command) { 136737aa5c36SGuy Levi case MLX5_IB_MMAP_WC_PAGE: 136837aa5c36SGuy Levi case MLX5_IB_MMAP_NC_PAGE: 136937aa5c36SGuy Levi case MLX5_IB_MMAP_REGULAR_PAGE: 13707c2344c3SMaor Gottlieb return uar_mmap(dev, command, vma, context); 1371e126ba97SEli Cohen 1372e126ba97SEli Cohen case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES: 1373e126ba97SEli Cohen return -ENOSYS; 1374e126ba97SEli Cohen 1375d69e3bcfSMatan Barak case MLX5_IB_MMAP_CORE_CLOCK: 1376d69e3bcfSMatan Barak if (vma->vm_end - vma->vm_start != PAGE_SIZE) 1377d69e3bcfSMatan Barak return -EINVAL; 1378d69e3bcfSMatan Barak 13796cbac1e4SMatan Barak if (vma->vm_flags & VM_WRITE) 1380d69e3bcfSMatan Barak return -EPERM; 1381d69e3bcfSMatan Barak 1382d69e3bcfSMatan Barak /* Don't expose to user-space information it shouldn't have */ 1383d69e3bcfSMatan Barak if (PAGE_SIZE > 4096) 1384d69e3bcfSMatan Barak return -EOPNOTSUPP; 1385d69e3bcfSMatan Barak 1386d69e3bcfSMatan Barak vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); 1387d69e3bcfSMatan Barak pfn = (dev->mdev->iseg_base + 1388d69e3bcfSMatan Barak offsetof(struct mlx5_init_seg, internal_timer_h)) >> 1389d69e3bcfSMatan Barak PAGE_SHIFT; 1390d69e3bcfSMatan Barak if (io_remap_pfn_range(vma, vma->vm_start, pfn, 1391d69e3bcfSMatan Barak PAGE_SIZE, vma->vm_page_prot)) 1392d69e3bcfSMatan Barak return -EAGAIN; 1393d69e3bcfSMatan Barak 1394d69e3bcfSMatan Barak mlx5_ib_dbg(dev, "mapped internal timer at 0x%lx, PA 0x%llx\n", 1395d69e3bcfSMatan Barak vma->vm_start, 1396d69e3bcfSMatan Barak (unsigned long long)pfn << PAGE_SHIFT); 1397d69e3bcfSMatan Barak break; 1398d69e3bcfSMatan Barak 1399e126ba97SEli Cohen default: 1400e126ba97SEli Cohen return -EINVAL; 1401e126ba97SEli Cohen } 1402e126ba97SEli Cohen 1403e126ba97SEli Cohen return 0; 1404e126ba97SEli Cohen } 1405e126ba97SEli Cohen 1406e126ba97SEli Cohen static struct ib_pd *mlx5_ib_alloc_pd(struct ib_device *ibdev, 1407e126ba97SEli Cohen struct ib_ucontext *context, 1408e126ba97SEli Cohen struct ib_udata *udata) 1409e126ba97SEli Cohen { 1410e126ba97SEli Cohen struct mlx5_ib_alloc_pd_resp resp; 1411e126ba97SEli Cohen struct mlx5_ib_pd *pd; 1412e126ba97SEli Cohen int err; 1413e126ba97SEli Cohen 1414e126ba97SEli Cohen pd = kmalloc(sizeof(*pd), GFP_KERNEL); 1415e126ba97SEli Cohen if (!pd) 1416e126ba97SEli Cohen return ERR_PTR(-ENOMEM); 1417e126ba97SEli Cohen 14189603b61dSJack Morgenstein err = mlx5_core_alloc_pd(to_mdev(ibdev)->mdev, &pd->pdn); 1419e126ba97SEli Cohen if (err) { 1420e126ba97SEli Cohen kfree(pd); 1421e126ba97SEli Cohen return ERR_PTR(err); 1422e126ba97SEli Cohen } 1423e126ba97SEli Cohen 1424e126ba97SEli Cohen if (context) { 1425e126ba97SEli Cohen resp.pdn = pd->pdn; 1426e126ba97SEli Cohen if (ib_copy_to_udata(udata, &resp, sizeof(resp))) { 14279603b61dSJack Morgenstein mlx5_core_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn); 1428e126ba97SEli Cohen kfree(pd); 1429e126ba97SEli Cohen return ERR_PTR(-EFAULT); 1430e126ba97SEli Cohen } 1431e126ba97SEli Cohen } 1432e126ba97SEli Cohen 1433e126ba97SEli Cohen return &pd->ibpd; 1434e126ba97SEli Cohen } 1435e126ba97SEli Cohen 1436e126ba97SEli Cohen static int mlx5_ib_dealloc_pd(struct ib_pd *pd) 1437e126ba97SEli Cohen { 1438e126ba97SEli Cohen struct mlx5_ib_dev *mdev = to_mdev(pd->device); 1439e126ba97SEli Cohen struct mlx5_ib_pd *mpd = to_mpd(pd); 1440e126ba97SEli Cohen 14419603b61dSJack Morgenstein mlx5_core_dealloc_pd(mdev->mdev, mpd->pdn); 1442e126ba97SEli Cohen kfree(mpd); 1443e126ba97SEli Cohen 1444e126ba97SEli Cohen return 0; 1445e126ba97SEli Cohen } 1446e126ba97SEli Cohen 1447466fa6d2SMaor Gottlieb enum { 1448466fa6d2SMaor Gottlieb MATCH_CRITERIA_ENABLE_OUTER_BIT, 1449466fa6d2SMaor Gottlieb MATCH_CRITERIA_ENABLE_MISC_BIT, 1450466fa6d2SMaor Gottlieb MATCH_CRITERIA_ENABLE_INNER_BIT 1451466fa6d2SMaor Gottlieb }; 1452038d2ef8SMaor Gottlieb 1453466fa6d2SMaor Gottlieb #define HEADER_IS_ZERO(match_criteria, headers) \ 1454466fa6d2SMaor Gottlieb !(memchr_inv(MLX5_ADDR_OF(fte_match_param, match_criteria, headers), \ 1455466fa6d2SMaor Gottlieb 0, MLX5_FLD_SZ_BYTES(fte_match_param, headers))) \ 1456466fa6d2SMaor Gottlieb 1457466fa6d2SMaor Gottlieb static u8 get_match_criteria_enable(u32 *match_criteria) 1458466fa6d2SMaor Gottlieb { 1459466fa6d2SMaor Gottlieb u8 match_criteria_enable; 1460466fa6d2SMaor Gottlieb 1461466fa6d2SMaor Gottlieb match_criteria_enable = 1462466fa6d2SMaor Gottlieb (!HEADER_IS_ZERO(match_criteria, outer_headers)) << 1463466fa6d2SMaor Gottlieb MATCH_CRITERIA_ENABLE_OUTER_BIT; 1464466fa6d2SMaor Gottlieb match_criteria_enable |= 1465466fa6d2SMaor Gottlieb (!HEADER_IS_ZERO(match_criteria, misc_parameters)) << 1466466fa6d2SMaor Gottlieb MATCH_CRITERIA_ENABLE_MISC_BIT; 1467466fa6d2SMaor Gottlieb match_criteria_enable |= 1468466fa6d2SMaor Gottlieb (!HEADER_IS_ZERO(match_criteria, inner_headers)) << 1469466fa6d2SMaor Gottlieb MATCH_CRITERIA_ENABLE_INNER_BIT; 1470466fa6d2SMaor Gottlieb 1471466fa6d2SMaor Gottlieb return match_criteria_enable; 1472038d2ef8SMaor Gottlieb } 1473038d2ef8SMaor Gottlieb 1474ca0d4753SMaor Gottlieb static void set_proto(void *outer_c, void *outer_v, u8 mask, u8 val) 1475ca0d4753SMaor Gottlieb { 1476ca0d4753SMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_protocol, mask); 1477ca0d4753SMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_protocol, val); 1478ca0d4753SMaor Gottlieb } 1479ca0d4753SMaor Gottlieb 1480ca0d4753SMaor Gottlieb static void set_tos(void *outer_c, void *outer_v, u8 mask, u8 val) 1481ca0d4753SMaor Gottlieb { 1482ca0d4753SMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_ecn, mask); 1483ca0d4753SMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_ecn, val); 1484ca0d4753SMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_dscp, mask >> 2); 1485ca0d4753SMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_dscp, val >> 2); 1486ca0d4753SMaor Gottlieb } 1487ca0d4753SMaor Gottlieb 1488c47ac6aeSMaor Gottlieb #define LAST_ETH_FIELD vlan_tag 1489c47ac6aeSMaor Gottlieb #define LAST_IB_FIELD sl 1490ca0d4753SMaor Gottlieb #define LAST_IPV4_FIELD tos 1491466fa6d2SMaor Gottlieb #define LAST_IPV6_FIELD traffic_class 1492c47ac6aeSMaor Gottlieb #define LAST_TCP_UDP_FIELD src_port 1493c47ac6aeSMaor Gottlieb 1494c47ac6aeSMaor Gottlieb /* Field is the last supported field */ 1495c47ac6aeSMaor Gottlieb #define FIELDS_NOT_SUPPORTED(filter, field)\ 1496c47ac6aeSMaor Gottlieb memchr_inv((void *)&filter.field +\ 1497c47ac6aeSMaor Gottlieb sizeof(filter.field), 0,\ 1498c47ac6aeSMaor Gottlieb sizeof(filter) -\ 1499c47ac6aeSMaor Gottlieb offsetof(typeof(filter), field) -\ 1500c47ac6aeSMaor Gottlieb sizeof(filter.field)) 1501c47ac6aeSMaor Gottlieb 1502038d2ef8SMaor Gottlieb static int parse_flow_attr(u32 *match_c, u32 *match_v, 1503dd063d0eSMaor Gottlieb const union ib_flow_spec *ib_spec) 1504038d2ef8SMaor Gottlieb { 1505038d2ef8SMaor Gottlieb void *outer_headers_c = MLX5_ADDR_OF(fte_match_param, match_c, 1506038d2ef8SMaor Gottlieb outer_headers); 1507038d2ef8SMaor Gottlieb void *outer_headers_v = MLX5_ADDR_OF(fte_match_param, match_v, 1508038d2ef8SMaor Gottlieb outer_headers); 1509466fa6d2SMaor Gottlieb void *misc_params_c = MLX5_ADDR_OF(fte_match_param, match_c, 1510466fa6d2SMaor Gottlieb misc_parameters); 1511466fa6d2SMaor Gottlieb void *misc_params_v = MLX5_ADDR_OF(fte_match_param, match_v, 1512466fa6d2SMaor Gottlieb misc_parameters); 1513466fa6d2SMaor Gottlieb 1514038d2ef8SMaor Gottlieb switch (ib_spec->type) { 1515038d2ef8SMaor Gottlieb case IB_FLOW_SPEC_ETH: 1516c47ac6aeSMaor Gottlieb if (FIELDS_NOT_SUPPORTED(ib_spec->eth.mask, LAST_ETH_FIELD)) 1517c47ac6aeSMaor Gottlieb return -ENOTSUPP; 1518038d2ef8SMaor Gottlieb 1519038d2ef8SMaor Gottlieb ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_c, 1520038d2ef8SMaor Gottlieb dmac_47_16), 1521038d2ef8SMaor Gottlieb ib_spec->eth.mask.dst_mac); 1522038d2ef8SMaor Gottlieb ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_v, 1523038d2ef8SMaor Gottlieb dmac_47_16), 1524038d2ef8SMaor Gottlieb ib_spec->eth.val.dst_mac); 1525038d2ef8SMaor Gottlieb 1526038d2ef8SMaor Gottlieb if (ib_spec->eth.mask.vlan_tag) { 1527038d2ef8SMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, 1528038d2ef8SMaor Gottlieb vlan_tag, 1); 1529038d2ef8SMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, 1530038d2ef8SMaor Gottlieb vlan_tag, 1); 1531038d2ef8SMaor Gottlieb 1532038d2ef8SMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, 1533038d2ef8SMaor Gottlieb first_vid, ntohs(ib_spec->eth.mask.vlan_tag)); 1534038d2ef8SMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, 1535038d2ef8SMaor Gottlieb first_vid, ntohs(ib_spec->eth.val.vlan_tag)); 1536038d2ef8SMaor Gottlieb 1537038d2ef8SMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, 1538038d2ef8SMaor Gottlieb first_cfi, 1539038d2ef8SMaor Gottlieb ntohs(ib_spec->eth.mask.vlan_tag) >> 12); 1540038d2ef8SMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, 1541038d2ef8SMaor Gottlieb first_cfi, 1542038d2ef8SMaor Gottlieb ntohs(ib_spec->eth.val.vlan_tag) >> 12); 1543038d2ef8SMaor Gottlieb 1544038d2ef8SMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, 1545038d2ef8SMaor Gottlieb first_prio, 1546038d2ef8SMaor Gottlieb ntohs(ib_spec->eth.mask.vlan_tag) >> 13); 1547038d2ef8SMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, 1548038d2ef8SMaor Gottlieb first_prio, 1549038d2ef8SMaor Gottlieb ntohs(ib_spec->eth.val.vlan_tag) >> 13); 1550038d2ef8SMaor Gottlieb } 1551038d2ef8SMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, 1552038d2ef8SMaor Gottlieb ethertype, ntohs(ib_spec->eth.mask.ether_type)); 1553038d2ef8SMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, 1554038d2ef8SMaor Gottlieb ethertype, ntohs(ib_spec->eth.val.ether_type)); 1555038d2ef8SMaor Gottlieb break; 1556038d2ef8SMaor Gottlieb case IB_FLOW_SPEC_IPV4: 1557c47ac6aeSMaor Gottlieb if (FIELDS_NOT_SUPPORTED(ib_spec->ipv4.mask, LAST_IPV4_FIELD)) 1558c47ac6aeSMaor Gottlieb return -ENOTSUPP; 1559038d2ef8SMaor Gottlieb 1560038d2ef8SMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, 1561038d2ef8SMaor Gottlieb ethertype, 0xffff); 1562038d2ef8SMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, 1563038d2ef8SMaor Gottlieb ethertype, ETH_P_IP); 1564038d2ef8SMaor Gottlieb 1565038d2ef8SMaor Gottlieb memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_c, 1566038d2ef8SMaor Gottlieb src_ipv4_src_ipv6.ipv4_layout.ipv4), 1567038d2ef8SMaor Gottlieb &ib_spec->ipv4.mask.src_ip, 1568038d2ef8SMaor Gottlieb sizeof(ib_spec->ipv4.mask.src_ip)); 1569038d2ef8SMaor Gottlieb memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_v, 1570038d2ef8SMaor Gottlieb src_ipv4_src_ipv6.ipv4_layout.ipv4), 1571038d2ef8SMaor Gottlieb &ib_spec->ipv4.val.src_ip, 1572038d2ef8SMaor Gottlieb sizeof(ib_spec->ipv4.val.src_ip)); 1573038d2ef8SMaor Gottlieb memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_c, 1574038d2ef8SMaor Gottlieb dst_ipv4_dst_ipv6.ipv4_layout.ipv4), 1575038d2ef8SMaor Gottlieb &ib_spec->ipv4.mask.dst_ip, 1576038d2ef8SMaor Gottlieb sizeof(ib_spec->ipv4.mask.dst_ip)); 1577038d2ef8SMaor Gottlieb memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_v, 1578038d2ef8SMaor Gottlieb dst_ipv4_dst_ipv6.ipv4_layout.ipv4), 1579038d2ef8SMaor Gottlieb &ib_spec->ipv4.val.dst_ip, 1580038d2ef8SMaor Gottlieb sizeof(ib_spec->ipv4.val.dst_ip)); 1581ca0d4753SMaor Gottlieb 1582ca0d4753SMaor Gottlieb set_tos(outer_headers_c, outer_headers_v, 1583ca0d4753SMaor Gottlieb ib_spec->ipv4.mask.tos, ib_spec->ipv4.val.tos); 1584ca0d4753SMaor Gottlieb 1585ca0d4753SMaor Gottlieb set_proto(outer_headers_c, outer_headers_v, 1586ca0d4753SMaor Gottlieb ib_spec->ipv4.mask.proto, ib_spec->ipv4.val.proto); 1587038d2ef8SMaor Gottlieb break; 1588026bae0cSMaor Gottlieb case IB_FLOW_SPEC_IPV6: 1589c47ac6aeSMaor Gottlieb if (FIELDS_NOT_SUPPORTED(ib_spec->ipv6.mask, LAST_IPV6_FIELD)) 1590c47ac6aeSMaor Gottlieb return -ENOTSUPP; 1591026bae0cSMaor Gottlieb 1592026bae0cSMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, 1593026bae0cSMaor Gottlieb ethertype, 0xffff); 1594026bae0cSMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, 1595026bae0cSMaor Gottlieb ethertype, ETH_P_IPV6); 1596026bae0cSMaor Gottlieb 1597026bae0cSMaor Gottlieb memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_c, 1598026bae0cSMaor Gottlieb src_ipv4_src_ipv6.ipv6_layout.ipv6), 1599026bae0cSMaor Gottlieb &ib_spec->ipv6.mask.src_ip, 1600026bae0cSMaor Gottlieb sizeof(ib_spec->ipv6.mask.src_ip)); 1601026bae0cSMaor Gottlieb memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_v, 1602026bae0cSMaor Gottlieb src_ipv4_src_ipv6.ipv6_layout.ipv6), 1603026bae0cSMaor Gottlieb &ib_spec->ipv6.val.src_ip, 1604026bae0cSMaor Gottlieb sizeof(ib_spec->ipv6.val.src_ip)); 1605026bae0cSMaor Gottlieb memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_c, 1606026bae0cSMaor Gottlieb dst_ipv4_dst_ipv6.ipv6_layout.ipv6), 1607026bae0cSMaor Gottlieb &ib_spec->ipv6.mask.dst_ip, 1608026bae0cSMaor Gottlieb sizeof(ib_spec->ipv6.mask.dst_ip)); 1609026bae0cSMaor Gottlieb memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_v, 1610026bae0cSMaor Gottlieb dst_ipv4_dst_ipv6.ipv6_layout.ipv6), 1611026bae0cSMaor Gottlieb &ib_spec->ipv6.val.dst_ip, 1612026bae0cSMaor Gottlieb sizeof(ib_spec->ipv6.val.dst_ip)); 1613466fa6d2SMaor Gottlieb 1614466fa6d2SMaor Gottlieb set_tos(outer_headers_c, outer_headers_v, 1615466fa6d2SMaor Gottlieb ib_spec->ipv6.mask.traffic_class, 1616466fa6d2SMaor Gottlieb ib_spec->ipv6.val.traffic_class); 1617466fa6d2SMaor Gottlieb 1618466fa6d2SMaor Gottlieb set_proto(outer_headers_c, outer_headers_v, 1619466fa6d2SMaor Gottlieb ib_spec->ipv6.mask.next_hdr, 1620466fa6d2SMaor Gottlieb ib_spec->ipv6.val.next_hdr); 1621466fa6d2SMaor Gottlieb 1622466fa6d2SMaor Gottlieb MLX5_SET(fte_match_set_misc, misc_params_c, 1623466fa6d2SMaor Gottlieb outer_ipv6_flow_label, 1624466fa6d2SMaor Gottlieb ntohl(ib_spec->ipv6.mask.flow_label)); 1625466fa6d2SMaor Gottlieb MLX5_SET(fte_match_set_misc, misc_params_v, 1626466fa6d2SMaor Gottlieb outer_ipv6_flow_label, 1627466fa6d2SMaor Gottlieb ntohl(ib_spec->ipv6.val.flow_label)); 1628026bae0cSMaor Gottlieb break; 1629038d2ef8SMaor Gottlieb case IB_FLOW_SPEC_TCP: 1630c47ac6aeSMaor Gottlieb if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask, 1631c47ac6aeSMaor Gottlieb LAST_TCP_UDP_FIELD)) 1632c47ac6aeSMaor Gottlieb return -ENOTSUPP; 1633038d2ef8SMaor Gottlieb 1634038d2ef8SMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, ip_protocol, 1635038d2ef8SMaor Gottlieb 0xff); 1636038d2ef8SMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, ip_protocol, 1637038d2ef8SMaor Gottlieb IPPROTO_TCP); 1638038d2ef8SMaor Gottlieb 1639038d2ef8SMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, tcp_sport, 1640038d2ef8SMaor Gottlieb ntohs(ib_spec->tcp_udp.mask.src_port)); 1641038d2ef8SMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, tcp_sport, 1642038d2ef8SMaor Gottlieb ntohs(ib_spec->tcp_udp.val.src_port)); 1643038d2ef8SMaor Gottlieb 1644038d2ef8SMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, tcp_dport, 1645038d2ef8SMaor Gottlieb ntohs(ib_spec->tcp_udp.mask.dst_port)); 1646038d2ef8SMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, tcp_dport, 1647038d2ef8SMaor Gottlieb ntohs(ib_spec->tcp_udp.val.dst_port)); 1648038d2ef8SMaor Gottlieb break; 1649038d2ef8SMaor Gottlieb case IB_FLOW_SPEC_UDP: 1650c47ac6aeSMaor Gottlieb if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask, 1651c47ac6aeSMaor Gottlieb LAST_TCP_UDP_FIELD)) 1652c47ac6aeSMaor Gottlieb return -ENOTSUPP; 1653038d2ef8SMaor Gottlieb 1654038d2ef8SMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, ip_protocol, 1655038d2ef8SMaor Gottlieb 0xff); 1656038d2ef8SMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, ip_protocol, 1657038d2ef8SMaor Gottlieb IPPROTO_UDP); 1658038d2ef8SMaor Gottlieb 1659038d2ef8SMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, udp_sport, 1660038d2ef8SMaor Gottlieb ntohs(ib_spec->tcp_udp.mask.src_port)); 1661038d2ef8SMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, udp_sport, 1662038d2ef8SMaor Gottlieb ntohs(ib_spec->tcp_udp.val.src_port)); 1663038d2ef8SMaor Gottlieb 1664038d2ef8SMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, udp_dport, 1665038d2ef8SMaor Gottlieb ntohs(ib_spec->tcp_udp.mask.dst_port)); 1666038d2ef8SMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, udp_dport, 1667038d2ef8SMaor Gottlieb ntohs(ib_spec->tcp_udp.val.dst_port)); 1668038d2ef8SMaor Gottlieb break; 1669038d2ef8SMaor Gottlieb default: 1670038d2ef8SMaor Gottlieb return -EINVAL; 1671038d2ef8SMaor Gottlieb } 1672038d2ef8SMaor Gottlieb 1673038d2ef8SMaor Gottlieb return 0; 1674038d2ef8SMaor Gottlieb } 1675038d2ef8SMaor Gottlieb 1676038d2ef8SMaor Gottlieb /* If a flow could catch both multicast and unicast packets, 1677038d2ef8SMaor Gottlieb * it won't fall into the multicast flow steering table and this rule 1678038d2ef8SMaor Gottlieb * could steal other multicast packets. 1679038d2ef8SMaor Gottlieb */ 1680038d2ef8SMaor Gottlieb static bool flow_is_multicast_only(struct ib_flow_attr *ib_attr) 1681038d2ef8SMaor Gottlieb { 1682038d2ef8SMaor Gottlieb struct ib_flow_spec_eth *eth_spec; 1683038d2ef8SMaor Gottlieb 1684038d2ef8SMaor Gottlieb if (ib_attr->type != IB_FLOW_ATTR_NORMAL || 1685038d2ef8SMaor Gottlieb ib_attr->size < sizeof(struct ib_flow_attr) + 1686038d2ef8SMaor Gottlieb sizeof(struct ib_flow_spec_eth) || 1687038d2ef8SMaor Gottlieb ib_attr->num_of_specs < 1) 1688038d2ef8SMaor Gottlieb return false; 1689038d2ef8SMaor Gottlieb 1690038d2ef8SMaor Gottlieb eth_spec = (struct ib_flow_spec_eth *)(ib_attr + 1); 1691038d2ef8SMaor Gottlieb if (eth_spec->type != IB_FLOW_SPEC_ETH || 1692038d2ef8SMaor Gottlieb eth_spec->size != sizeof(*eth_spec)) 1693038d2ef8SMaor Gottlieb return false; 1694038d2ef8SMaor Gottlieb 1695038d2ef8SMaor Gottlieb return is_multicast_ether_addr(eth_spec->mask.dst_mac) && 1696038d2ef8SMaor Gottlieb is_multicast_ether_addr(eth_spec->val.dst_mac); 1697038d2ef8SMaor Gottlieb } 1698038d2ef8SMaor Gottlieb 1699dd063d0eSMaor Gottlieb static bool is_valid_attr(const struct ib_flow_attr *flow_attr) 1700038d2ef8SMaor Gottlieb { 1701038d2ef8SMaor Gottlieb union ib_flow_spec *ib_spec = (union ib_flow_spec *)(flow_attr + 1); 1702038d2ef8SMaor Gottlieb bool has_ipv4_spec = false; 1703038d2ef8SMaor Gottlieb bool eth_type_ipv4 = true; 1704038d2ef8SMaor Gottlieb unsigned int spec_index; 1705038d2ef8SMaor Gottlieb 1706038d2ef8SMaor Gottlieb /* Validate that ethertype is correct */ 1707038d2ef8SMaor Gottlieb for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) { 1708038d2ef8SMaor Gottlieb if (ib_spec->type == IB_FLOW_SPEC_ETH && 1709038d2ef8SMaor Gottlieb ib_spec->eth.mask.ether_type) { 1710038d2ef8SMaor Gottlieb if (!((ib_spec->eth.mask.ether_type == htons(0xffff)) && 1711038d2ef8SMaor Gottlieb ib_spec->eth.val.ether_type == htons(ETH_P_IP))) 1712038d2ef8SMaor Gottlieb eth_type_ipv4 = false; 1713038d2ef8SMaor Gottlieb } else if (ib_spec->type == IB_FLOW_SPEC_IPV4) { 1714038d2ef8SMaor Gottlieb has_ipv4_spec = true; 1715038d2ef8SMaor Gottlieb } 1716038d2ef8SMaor Gottlieb ib_spec = (void *)ib_spec + ib_spec->size; 1717038d2ef8SMaor Gottlieb } 1718038d2ef8SMaor Gottlieb return !has_ipv4_spec || eth_type_ipv4; 1719038d2ef8SMaor Gottlieb } 1720038d2ef8SMaor Gottlieb 1721038d2ef8SMaor Gottlieb static void put_flow_table(struct mlx5_ib_dev *dev, 1722038d2ef8SMaor Gottlieb struct mlx5_ib_flow_prio *prio, bool ft_added) 1723038d2ef8SMaor Gottlieb { 1724038d2ef8SMaor Gottlieb prio->refcount -= !!ft_added; 1725038d2ef8SMaor Gottlieb if (!prio->refcount) { 1726038d2ef8SMaor Gottlieb mlx5_destroy_flow_table(prio->flow_table); 1727038d2ef8SMaor Gottlieb prio->flow_table = NULL; 1728038d2ef8SMaor Gottlieb } 1729038d2ef8SMaor Gottlieb } 1730038d2ef8SMaor Gottlieb 1731038d2ef8SMaor Gottlieb static int mlx5_ib_destroy_flow(struct ib_flow *flow_id) 1732038d2ef8SMaor Gottlieb { 1733038d2ef8SMaor Gottlieb struct mlx5_ib_dev *dev = to_mdev(flow_id->qp->device); 1734038d2ef8SMaor Gottlieb struct mlx5_ib_flow_handler *handler = container_of(flow_id, 1735038d2ef8SMaor Gottlieb struct mlx5_ib_flow_handler, 1736038d2ef8SMaor Gottlieb ibflow); 1737038d2ef8SMaor Gottlieb struct mlx5_ib_flow_handler *iter, *tmp; 1738038d2ef8SMaor Gottlieb 1739038d2ef8SMaor Gottlieb mutex_lock(&dev->flow_db.lock); 1740038d2ef8SMaor Gottlieb 1741038d2ef8SMaor Gottlieb list_for_each_entry_safe(iter, tmp, &handler->list, list) { 1742038d2ef8SMaor Gottlieb mlx5_del_flow_rule(iter->rule); 1743cc0e5d42SMaor Gottlieb put_flow_table(dev, iter->prio, true); 1744038d2ef8SMaor Gottlieb list_del(&iter->list); 1745038d2ef8SMaor Gottlieb kfree(iter); 1746038d2ef8SMaor Gottlieb } 1747038d2ef8SMaor Gottlieb 1748038d2ef8SMaor Gottlieb mlx5_del_flow_rule(handler->rule); 17495497adc6SMaor Gottlieb put_flow_table(dev, handler->prio, true); 1750038d2ef8SMaor Gottlieb mutex_unlock(&dev->flow_db.lock); 1751038d2ef8SMaor Gottlieb 1752038d2ef8SMaor Gottlieb kfree(handler); 1753038d2ef8SMaor Gottlieb 1754038d2ef8SMaor Gottlieb return 0; 1755038d2ef8SMaor Gottlieb } 1756038d2ef8SMaor Gottlieb 175735d19011SMaor Gottlieb static int ib_prio_to_core_prio(unsigned int priority, bool dont_trap) 175835d19011SMaor Gottlieb { 175935d19011SMaor Gottlieb priority *= 2; 176035d19011SMaor Gottlieb if (!dont_trap) 176135d19011SMaor Gottlieb priority++; 176235d19011SMaor Gottlieb return priority; 176335d19011SMaor Gottlieb } 176435d19011SMaor Gottlieb 1765cc0e5d42SMaor Gottlieb enum flow_table_type { 1766cc0e5d42SMaor Gottlieb MLX5_IB_FT_RX, 1767cc0e5d42SMaor Gottlieb MLX5_IB_FT_TX 1768cc0e5d42SMaor Gottlieb }; 1769cc0e5d42SMaor Gottlieb 1770038d2ef8SMaor Gottlieb #define MLX5_FS_MAX_TYPES 10 1771038d2ef8SMaor Gottlieb #define MLX5_FS_MAX_ENTRIES 32000UL 1772038d2ef8SMaor Gottlieb static struct mlx5_ib_flow_prio *get_flow_table(struct mlx5_ib_dev *dev, 1773cc0e5d42SMaor Gottlieb struct ib_flow_attr *flow_attr, 1774cc0e5d42SMaor Gottlieb enum flow_table_type ft_type) 1775038d2ef8SMaor Gottlieb { 177635d19011SMaor Gottlieb bool dont_trap = flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP; 1777038d2ef8SMaor Gottlieb struct mlx5_flow_namespace *ns = NULL; 1778038d2ef8SMaor Gottlieb struct mlx5_ib_flow_prio *prio; 1779038d2ef8SMaor Gottlieb struct mlx5_flow_table *ft; 1780038d2ef8SMaor Gottlieb int num_entries; 1781038d2ef8SMaor Gottlieb int num_groups; 1782038d2ef8SMaor Gottlieb int priority; 1783038d2ef8SMaor Gottlieb int err = 0; 1784038d2ef8SMaor Gottlieb 1785038d2ef8SMaor Gottlieb if (flow_attr->type == IB_FLOW_ATTR_NORMAL) { 178635d19011SMaor Gottlieb if (flow_is_multicast_only(flow_attr) && 178735d19011SMaor Gottlieb !dont_trap) 1788038d2ef8SMaor Gottlieb priority = MLX5_IB_FLOW_MCAST_PRIO; 1789038d2ef8SMaor Gottlieb else 179035d19011SMaor Gottlieb priority = ib_prio_to_core_prio(flow_attr->priority, 179135d19011SMaor Gottlieb dont_trap); 1792038d2ef8SMaor Gottlieb ns = mlx5_get_flow_namespace(dev->mdev, 1793038d2ef8SMaor Gottlieb MLX5_FLOW_NAMESPACE_BYPASS); 1794038d2ef8SMaor Gottlieb num_entries = MLX5_FS_MAX_ENTRIES; 1795038d2ef8SMaor Gottlieb num_groups = MLX5_FS_MAX_TYPES; 1796038d2ef8SMaor Gottlieb prio = &dev->flow_db.prios[priority]; 1797038d2ef8SMaor Gottlieb } else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT || 1798038d2ef8SMaor Gottlieb flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) { 1799038d2ef8SMaor Gottlieb ns = mlx5_get_flow_namespace(dev->mdev, 1800038d2ef8SMaor Gottlieb MLX5_FLOW_NAMESPACE_LEFTOVERS); 1801038d2ef8SMaor Gottlieb build_leftovers_ft_param(&priority, 1802038d2ef8SMaor Gottlieb &num_entries, 1803038d2ef8SMaor Gottlieb &num_groups); 1804038d2ef8SMaor Gottlieb prio = &dev->flow_db.prios[MLX5_IB_FLOW_LEFTOVERS_PRIO]; 1805cc0e5d42SMaor Gottlieb } else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) { 1806cc0e5d42SMaor Gottlieb if (!MLX5_CAP_FLOWTABLE(dev->mdev, 1807cc0e5d42SMaor Gottlieb allow_sniffer_and_nic_rx_shared_tir)) 1808cc0e5d42SMaor Gottlieb return ERR_PTR(-ENOTSUPP); 1809cc0e5d42SMaor Gottlieb 1810cc0e5d42SMaor Gottlieb ns = mlx5_get_flow_namespace(dev->mdev, ft_type == MLX5_IB_FT_RX ? 1811cc0e5d42SMaor Gottlieb MLX5_FLOW_NAMESPACE_SNIFFER_RX : 1812cc0e5d42SMaor Gottlieb MLX5_FLOW_NAMESPACE_SNIFFER_TX); 1813cc0e5d42SMaor Gottlieb 1814cc0e5d42SMaor Gottlieb prio = &dev->flow_db.sniffer[ft_type]; 1815cc0e5d42SMaor Gottlieb priority = 0; 1816cc0e5d42SMaor Gottlieb num_entries = 1; 1817cc0e5d42SMaor Gottlieb num_groups = 1; 1818038d2ef8SMaor Gottlieb } 1819038d2ef8SMaor Gottlieb 1820038d2ef8SMaor Gottlieb if (!ns) 1821038d2ef8SMaor Gottlieb return ERR_PTR(-ENOTSUPP); 1822038d2ef8SMaor Gottlieb 1823038d2ef8SMaor Gottlieb ft = prio->flow_table; 1824038d2ef8SMaor Gottlieb if (!ft) { 1825038d2ef8SMaor Gottlieb ft = mlx5_create_auto_grouped_flow_table(ns, priority, 1826038d2ef8SMaor Gottlieb num_entries, 1827d63cd286SMaor Gottlieb num_groups, 1828d63cd286SMaor Gottlieb 0); 1829038d2ef8SMaor Gottlieb 1830038d2ef8SMaor Gottlieb if (!IS_ERR(ft)) { 1831038d2ef8SMaor Gottlieb prio->refcount = 0; 1832038d2ef8SMaor Gottlieb prio->flow_table = ft; 1833038d2ef8SMaor Gottlieb } else { 1834038d2ef8SMaor Gottlieb err = PTR_ERR(ft); 1835038d2ef8SMaor Gottlieb } 1836038d2ef8SMaor Gottlieb } 1837038d2ef8SMaor Gottlieb 1838038d2ef8SMaor Gottlieb return err ? ERR_PTR(err) : prio; 1839038d2ef8SMaor Gottlieb } 1840038d2ef8SMaor Gottlieb 1841038d2ef8SMaor Gottlieb static struct mlx5_ib_flow_handler *create_flow_rule(struct mlx5_ib_dev *dev, 1842038d2ef8SMaor Gottlieb struct mlx5_ib_flow_prio *ft_prio, 1843dd063d0eSMaor Gottlieb const struct ib_flow_attr *flow_attr, 1844038d2ef8SMaor Gottlieb struct mlx5_flow_destination *dst) 1845038d2ef8SMaor Gottlieb { 1846038d2ef8SMaor Gottlieb struct mlx5_flow_table *ft = ft_prio->flow_table; 1847038d2ef8SMaor Gottlieb struct mlx5_ib_flow_handler *handler; 1848c5bb1730SMaor Gottlieb struct mlx5_flow_spec *spec; 1849dd063d0eSMaor Gottlieb const void *ib_flow = (const void *)flow_attr + sizeof(*flow_attr); 1850038d2ef8SMaor Gottlieb unsigned int spec_index; 185135d19011SMaor Gottlieb u32 action; 1852038d2ef8SMaor Gottlieb int err = 0; 1853038d2ef8SMaor Gottlieb 1854038d2ef8SMaor Gottlieb if (!is_valid_attr(flow_attr)) 1855038d2ef8SMaor Gottlieb return ERR_PTR(-EINVAL); 1856038d2ef8SMaor Gottlieb 1857c5bb1730SMaor Gottlieb spec = mlx5_vzalloc(sizeof(*spec)); 1858038d2ef8SMaor Gottlieb handler = kzalloc(sizeof(*handler), GFP_KERNEL); 1859c5bb1730SMaor Gottlieb if (!handler || !spec) { 1860038d2ef8SMaor Gottlieb err = -ENOMEM; 1861038d2ef8SMaor Gottlieb goto free; 1862038d2ef8SMaor Gottlieb } 1863038d2ef8SMaor Gottlieb 1864038d2ef8SMaor Gottlieb INIT_LIST_HEAD(&handler->list); 1865038d2ef8SMaor Gottlieb 1866038d2ef8SMaor Gottlieb for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) { 1867c5bb1730SMaor Gottlieb err = parse_flow_attr(spec->match_criteria, 1868c5bb1730SMaor Gottlieb spec->match_value, ib_flow); 1869038d2ef8SMaor Gottlieb if (err < 0) 1870038d2ef8SMaor Gottlieb goto free; 1871038d2ef8SMaor Gottlieb 1872038d2ef8SMaor Gottlieb ib_flow += ((union ib_flow_spec *)ib_flow)->size; 1873038d2ef8SMaor Gottlieb } 1874038d2ef8SMaor Gottlieb 1875466fa6d2SMaor Gottlieb spec->match_criteria_enable = get_match_criteria_enable(spec->match_criteria); 187635d19011SMaor Gottlieb action = dst ? MLX5_FLOW_CONTEXT_ACTION_FWD_DEST : 187735d19011SMaor Gottlieb MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO; 1878c5bb1730SMaor Gottlieb handler->rule = mlx5_add_flow_rule(ft, spec, 187935d19011SMaor Gottlieb action, 1880038d2ef8SMaor Gottlieb MLX5_FS_DEFAULT_FLOW_TAG, 1881038d2ef8SMaor Gottlieb dst); 1882038d2ef8SMaor Gottlieb 1883038d2ef8SMaor Gottlieb if (IS_ERR(handler->rule)) { 1884038d2ef8SMaor Gottlieb err = PTR_ERR(handler->rule); 1885038d2ef8SMaor Gottlieb goto free; 1886038d2ef8SMaor Gottlieb } 1887038d2ef8SMaor Gottlieb 1888d9d4980aSMaor Gottlieb ft_prio->refcount++; 18895497adc6SMaor Gottlieb handler->prio = ft_prio; 1890038d2ef8SMaor Gottlieb 1891038d2ef8SMaor Gottlieb ft_prio->flow_table = ft; 1892038d2ef8SMaor Gottlieb free: 1893038d2ef8SMaor Gottlieb if (err) 1894038d2ef8SMaor Gottlieb kfree(handler); 1895c5bb1730SMaor Gottlieb kvfree(spec); 1896038d2ef8SMaor Gottlieb return err ? ERR_PTR(err) : handler; 1897038d2ef8SMaor Gottlieb } 1898038d2ef8SMaor Gottlieb 189935d19011SMaor Gottlieb static struct mlx5_ib_flow_handler *create_dont_trap_rule(struct mlx5_ib_dev *dev, 190035d19011SMaor Gottlieb struct mlx5_ib_flow_prio *ft_prio, 190135d19011SMaor Gottlieb struct ib_flow_attr *flow_attr, 190235d19011SMaor Gottlieb struct mlx5_flow_destination *dst) 190335d19011SMaor Gottlieb { 190435d19011SMaor Gottlieb struct mlx5_ib_flow_handler *handler_dst = NULL; 190535d19011SMaor Gottlieb struct mlx5_ib_flow_handler *handler = NULL; 190635d19011SMaor Gottlieb 190735d19011SMaor Gottlieb handler = create_flow_rule(dev, ft_prio, flow_attr, NULL); 190835d19011SMaor Gottlieb if (!IS_ERR(handler)) { 190935d19011SMaor Gottlieb handler_dst = create_flow_rule(dev, ft_prio, 191035d19011SMaor Gottlieb flow_attr, dst); 191135d19011SMaor Gottlieb if (IS_ERR(handler_dst)) { 191235d19011SMaor Gottlieb mlx5_del_flow_rule(handler->rule); 1913d9d4980aSMaor Gottlieb ft_prio->refcount--; 191435d19011SMaor Gottlieb kfree(handler); 191535d19011SMaor Gottlieb handler = handler_dst; 191635d19011SMaor Gottlieb } else { 191735d19011SMaor Gottlieb list_add(&handler_dst->list, &handler->list); 191835d19011SMaor Gottlieb } 191935d19011SMaor Gottlieb } 192035d19011SMaor Gottlieb 192135d19011SMaor Gottlieb return handler; 192235d19011SMaor Gottlieb } 1923038d2ef8SMaor Gottlieb enum { 1924038d2ef8SMaor Gottlieb LEFTOVERS_MC, 1925038d2ef8SMaor Gottlieb LEFTOVERS_UC, 1926038d2ef8SMaor Gottlieb }; 1927038d2ef8SMaor Gottlieb 1928038d2ef8SMaor Gottlieb static struct mlx5_ib_flow_handler *create_leftovers_rule(struct mlx5_ib_dev *dev, 1929038d2ef8SMaor Gottlieb struct mlx5_ib_flow_prio *ft_prio, 1930038d2ef8SMaor Gottlieb struct ib_flow_attr *flow_attr, 1931038d2ef8SMaor Gottlieb struct mlx5_flow_destination *dst) 1932038d2ef8SMaor Gottlieb { 1933038d2ef8SMaor Gottlieb struct mlx5_ib_flow_handler *handler_ucast = NULL; 1934038d2ef8SMaor Gottlieb struct mlx5_ib_flow_handler *handler = NULL; 1935038d2ef8SMaor Gottlieb 1936038d2ef8SMaor Gottlieb static struct { 1937038d2ef8SMaor Gottlieb struct ib_flow_attr flow_attr; 1938038d2ef8SMaor Gottlieb struct ib_flow_spec_eth eth_flow; 1939038d2ef8SMaor Gottlieb } leftovers_specs[] = { 1940038d2ef8SMaor Gottlieb [LEFTOVERS_MC] = { 1941038d2ef8SMaor Gottlieb .flow_attr = { 1942038d2ef8SMaor Gottlieb .num_of_specs = 1, 1943038d2ef8SMaor Gottlieb .size = sizeof(leftovers_specs[0]) 1944038d2ef8SMaor Gottlieb }, 1945038d2ef8SMaor Gottlieb .eth_flow = { 1946038d2ef8SMaor Gottlieb .type = IB_FLOW_SPEC_ETH, 1947038d2ef8SMaor Gottlieb .size = sizeof(struct ib_flow_spec_eth), 1948038d2ef8SMaor Gottlieb .mask = {.dst_mac = {0x1} }, 1949038d2ef8SMaor Gottlieb .val = {.dst_mac = {0x1} } 1950038d2ef8SMaor Gottlieb } 1951038d2ef8SMaor Gottlieb }, 1952038d2ef8SMaor Gottlieb [LEFTOVERS_UC] = { 1953038d2ef8SMaor Gottlieb .flow_attr = { 1954038d2ef8SMaor Gottlieb .num_of_specs = 1, 1955038d2ef8SMaor Gottlieb .size = sizeof(leftovers_specs[0]) 1956038d2ef8SMaor Gottlieb }, 1957038d2ef8SMaor Gottlieb .eth_flow = { 1958038d2ef8SMaor Gottlieb .type = IB_FLOW_SPEC_ETH, 1959038d2ef8SMaor Gottlieb .size = sizeof(struct ib_flow_spec_eth), 1960038d2ef8SMaor Gottlieb .mask = {.dst_mac = {0x1} }, 1961038d2ef8SMaor Gottlieb .val = {.dst_mac = {} } 1962038d2ef8SMaor Gottlieb } 1963038d2ef8SMaor Gottlieb } 1964038d2ef8SMaor Gottlieb }; 1965038d2ef8SMaor Gottlieb 1966038d2ef8SMaor Gottlieb handler = create_flow_rule(dev, ft_prio, 1967038d2ef8SMaor Gottlieb &leftovers_specs[LEFTOVERS_MC].flow_attr, 1968038d2ef8SMaor Gottlieb dst); 1969038d2ef8SMaor Gottlieb if (!IS_ERR(handler) && 1970038d2ef8SMaor Gottlieb flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT) { 1971038d2ef8SMaor Gottlieb handler_ucast = create_flow_rule(dev, ft_prio, 1972038d2ef8SMaor Gottlieb &leftovers_specs[LEFTOVERS_UC].flow_attr, 1973038d2ef8SMaor Gottlieb dst); 1974038d2ef8SMaor Gottlieb if (IS_ERR(handler_ucast)) { 19757055a294SMaor Gottlieb mlx5_del_flow_rule(handler->rule); 1976d9d4980aSMaor Gottlieb ft_prio->refcount--; 1977038d2ef8SMaor Gottlieb kfree(handler); 1978038d2ef8SMaor Gottlieb handler = handler_ucast; 1979038d2ef8SMaor Gottlieb } else { 1980038d2ef8SMaor Gottlieb list_add(&handler_ucast->list, &handler->list); 1981038d2ef8SMaor Gottlieb } 1982038d2ef8SMaor Gottlieb } 1983038d2ef8SMaor Gottlieb 1984038d2ef8SMaor Gottlieb return handler; 1985038d2ef8SMaor Gottlieb } 1986038d2ef8SMaor Gottlieb 1987cc0e5d42SMaor Gottlieb static struct mlx5_ib_flow_handler *create_sniffer_rule(struct mlx5_ib_dev *dev, 1988cc0e5d42SMaor Gottlieb struct mlx5_ib_flow_prio *ft_rx, 1989cc0e5d42SMaor Gottlieb struct mlx5_ib_flow_prio *ft_tx, 1990cc0e5d42SMaor Gottlieb struct mlx5_flow_destination *dst) 1991cc0e5d42SMaor Gottlieb { 1992cc0e5d42SMaor Gottlieb struct mlx5_ib_flow_handler *handler_rx; 1993cc0e5d42SMaor Gottlieb struct mlx5_ib_flow_handler *handler_tx; 1994cc0e5d42SMaor Gottlieb int err; 1995cc0e5d42SMaor Gottlieb static const struct ib_flow_attr flow_attr = { 1996cc0e5d42SMaor Gottlieb .num_of_specs = 0, 1997cc0e5d42SMaor Gottlieb .size = sizeof(flow_attr) 1998cc0e5d42SMaor Gottlieb }; 1999cc0e5d42SMaor Gottlieb 2000cc0e5d42SMaor Gottlieb handler_rx = create_flow_rule(dev, ft_rx, &flow_attr, dst); 2001cc0e5d42SMaor Gottlieb if (IS_ERR(handler_rx)) { 2002cc0e5d42SMaor Gottlieb err = PTR_ERR(handler_rx); 2003cc0e5d42SMaor Gottlieb goto err; 2004cc0e5d42SMaor Gottlieb } 2005cc0e5d42SMaor Gottlieb 2006cc0e5d42SMaor Gottlieb handler_tx = create_flow_rule(dev, ft_tx, &flow_attr, dst); 2007cc0e5d42SMaor Gottlieb if (IS_ERR(handler_tx)) { 2008cc0e5d42SMaor Gottlieb err = PTR_ERR(handler_tx); 2009cc0e5d42SMaor Gottlieb goto err_tx; 2010cc0e5d42SMaor Gottlieb } 2011cc0e5d42SMaor Gottlieb 2012cc0e5d42SMaor Gottlieb list_add(&handler_tx->list, &handler_rx->list); 2013cc0e5d42SMaor Gottlieb 2014cc0e5d42SMaor Gottlieb return handler_rx; 2015cc0e5d42SMaor Gottlieb 2016cc0e5d42SMaor Gottlieb err_tx: 2017cc0e5d42SMaor Gottlieb mlx5_del_flow_rule(handler_rx->rule); 2018cc0e5d42SMaor Gottlieb ft_rx->refcount--; 2019cc0e5d42SMaor Gottlieb kfree(handler_rx); 2020cc0e5d42SMaor Gottlieb err: 2021cc0e5d42SMaor Gottlieb return ERR_PTR(err); 2022cc0e5d42SMaor Gottlieb } 2023cc0e5d42SMaor Gottlieb 2024038d2ef8SMaor Gottlieb static struct ib_flow *mlx5_ib_create_flow(struct ib_qp *qp, 2025038d2ef8SMaor Gottlieb struct ib_flow_attr *flow_attr, 2026038d2ef8SMaor Gottlieb int domain) 2027038d2ef8SMaor Gottlieb { 2028038d2ef8SMaor Gottlieb struct mlx5_ib_dev *dev = to_mdev(qp->device); 2029038d2ef8SMaor Gottlieb struct mlx5_ib_flow_handler *handler = NULL; 2030038d2ef8SMaor Gottlieb struct mlx5_flow_destination *dst = NULL; 2031cc0e5d42SMaor Gottlieb struct mlx5_ib_flow_prio *ft_prio_tx = NULL; 2032038d2ef8SMaor Gottlieb struct mlx5_ib_flow_prio *ft_prio; 2033038d2ef8SMaor Gottlieb int err; 2034038d2ef8SMaor Gottlieb 2035038d2ef8SMaor Gottlieb if (flow_attr->priority > MLX5_IB_FLOW_LAST_PRIO) 2036038d2ef8SMaor Gottlieb return ERR_PTR(-ENOSPC); 2037038d2ef8SMaor Gottlieb 2038038d2ef8SMaor Gottlieb if (domain != IB_FLOW_DOMAIN_USER || 2039038d2ef8SMaor Gottlieb flow_attr->port > MLX5_CAP_GEN(dev->mdev, num_ports) || 204035d19011SMaor Gottlieb (flow_attr->flags & ~IB_FLOW_ATTR_FLAGS_DONT_TRAP)) 2041038d2ef8SMaor Gottlieb return ERR_PTR(-EINVAL); 2042038d2ef8SMaor Gottlieb 2043038d2ef8SMaor Gottlieb dst = kzalloc(sizeof(*dst), GFP_KERNEL); 2044038d2ef8SMaor Gottlieb if (!dst) 2045038d2ef8SMaor Gottlieb return ERR_PTR(-ENOMEM); 2046038d2ef8SMaor Gottlieb 2047038d2ef8SMaor Gottlieb mutex_lock(&dev->flow_db.lock); 2048038d2ef8SMaor Gottlieb 2049cc0e5d42SMaor Gottlieb ft_prio = get_flow_table(dev, flow_attr, MLX5_IB_FT_RX); 2050038d2ef8SMaor Gottlieb if (IS_ERR(ft_prio)) { 2051038d2ef8SMaor Gottlieb err = PTR_ERR(ft_prio); 2052038d2ef8SMaor Gottlieb goto unlock; 2053038d2ef8SMaor Gottlieb } 2054cc0e5d42SMaor Gottlieb if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) { 2055cc0e5d42SMaor Gottlieb ft_prio_tx = get_flow_table(dev, flow_attr, MLX5_IB_FT_TX); 2056cc0e5d42SMaor Gottlieb if (IS_ERR(ft_prio_tx)) { 2057cc0e5d42SMaor Gottlieb err = PTR_ERR(ft_prio_tx); 2058cc0e5d42SMaor Gottlieb ft_prio_tx = NULL; 2059cc0e5d42SMaor Gottlieb goto destroy_ft; 2060cc0e5d42SMaor Gottlieb } 2061cc0e5d42SMaor Gottlieb } 2062038d2ef8SMaor Gottlieb 2063038d2ef8SMaor Gottlieb dst->type = MLX5_FLOW_DESTINATION_TYPE_TIR; 2064038d2ef8SMaor Gottlieb dst->tir_num = to_mqp(qp)->raw_packet_qp.rq.tirn; 2065038d2ef8SMaor Gottlieb 2066038d2ef8SMaor Gottlieb if (flow_attr->type == IB_FLOW_ATTR_NORMAL) { 206735d19011SMaor Gottlieb if (flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP) { 206835d19011SMaor Gottlieb handler = create_dont_trap_rule(dev, ft_prio, 206935d19011SMaor Gottlieb flow_attr, dst); 207035d19011SMaor Gottlieb } else { 2071038d2ef8SMaor Gottlieb handler = create_flow_rule(dev, ft_prio, flow_attr, 2072038d2ef8SMaor Gottlieb dst); 207335d19011SMaor Gottlieb } 2074038d2ef8SMaor Gottlieb } else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT || 2075038d2ef8SMaor Gottlieb flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) { 2076038d2ef8SMaor Gottlieb handler = create_leftovers_rule(dev, ft_prio, flow_attr, 2077038d2ef8SMaor Gottlieb dst); 2078cc0e5d42SMaor Gottlieb } else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) { 2079cc0e5d42SMaor Gottlieb handler = create_sniffer_rule(dev, ft_prio, ft_prio_tx, dst); 2080038d2ef8SMaor Gottlieb } else { 2081038d2ef8SMaor Gottlieb err = -EINVAL; 2082038d2ef8SMaor Gottlieb goto destroy_ft; 2083038d2ef8SMaor Gottlieb } 2084038d2ef8SMaor Gottlieb 2085038d2ef8SMaor Gottlieb if (IS_ERR(handler)) { 2086038d2ef8SMaor Gottlieb err = PTR_ERR(handler); 2087038d2ef8SMaor Gottlieb handler = NULL; 2088038d2ef8SMaor Gottlieb goto destroy_ft; 2089038d2ef8SMaor Gottlieb } 2090038d2ef8SMaor Gottlieb 2091038d2ef8SMaor Gottlieb mutex_unlock(&dev->flow_db.lock); 2092038d2ef8SMaor Gottlieb kfree(dst); 2093038d2ef8SMaor Gottlieb 2094038d2ef8SMaor Gottlieb return &handler->ibflow; 2095038d2ef8SMaor Gottlieb 2096038d2ef8SMaor Gottlieb destroy_ft: 2097038d2ef8SMaor Gottlieb put_flow_table(dev, ft_prio, false); 2098cc0e5d42SMaor Gottlieb if (ft_prio_tx) 2099cc0e5d42SMaor Gottlieb put_flow_table(dev, ft_prio_tx, false); 2100038d2ef8SMaor Gottlieb unlock: 2101038d2ef8SMaor Gottlieb mutex_unlock(&dev->flow_db.lock); 2102038d2ef8SMaor Gottlieb kfree(dst); 2103038d2ef8SMaor Gottlieb kfree(handler); 2104038d2ef8SMaor Gottlieb return ERR_PTR(err); 2105038d2ef8SMaor Gottlieb } 2106038d2ef8SMaor Gottlieb 2107e126ba97SEli Cohen static int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid) 2108e126ba97SEli Cohen { 2109e126ba97SEli Cohen struct mlx5_ib_dev *dev = to_mdev(ibqp->device); 2110e126ba97SEli Cohen int err; 2111e126ba97SEli Cohen 21129603b61dSJack Morgenstein err = mlx5_core_attach_mcg(dev->mdev, gid, ibqp->qp_num); 2113e126ba97SEli Cohen if (err) 2114e126ba97SEli Cohen mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n", 2115e126ba97SEli Cohen ibqp->qp_num, gid->raw); 2116e126ba97SEli Cohen 2117e126ba97SEli Cohen return err; 2118e126ba97SEli Cohen } 2119e126ba97SEli Cohen 2120e126ba97SEli Cohen static int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid) 2121e126ba97SEli Cohen { 2122e126ba97SEli Cohen struct mlx5_ib_dev *dev = to_mdev(ibqp->device); 2123e126ba97SEli Cohen int err; 2124e126ba97SEli Cohen 21259603b61dSJack Morgenstein err = mlx5_core_detach_mcg(dev->mdev, gid, ibqp->qp_num); 2126e126ba97SEli Cohen if (err) 2127e126ba97SEli Cohen mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n", 2128e126ba97SEli Cohen ibqp->qp_num, gid->raw); 2129e126ba97SEli Cohen 2130e126ba97SEli Cohen return err; 2131e126ba97SEli Cohen } 2132e126ba97SEli Cohen 2133e126ba97SEli Cohen static int init_node_data(struct mlx5_ib_dev *dev) 2134e126ba97SEli Cohen { 21351b5daf11SMajd Dibbiny int err; 2136e126ba97SEli Cohen 21371b5daf11SMajd Dibbiny err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc); 2138e126ba97SEli Cohen if (err) 2139e126ba97SEli Cohen return err; 21401b5daf11SMajd Dibbiny 21411b5daf11SMajd Dibbiny dev->mdev->rev_id = dev->mdev->pdev->revision; 21421b5daf11SMajd Dibbiny 21431b5daf11SMajd Dibbiny return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid); 2144e126ba97SEli Cohen } 2145e126ba97SEli Cohen 2146e126ba97SEli Cohen static ssize_t show_fw_pages(struct device *device, struct device_attribute *attr, 2147e126ba97SEli Cohen char *buf) 2148e126ba97SEli Cohen { 2149e126ba97SEli Cohen struct mlx5_ib_dev *dev = 2150e126ba97SEli Cohen container_of(device, struct mlx5_ib_dev, ib_dev.dev); 2151e126ba97SEli Cohen 21529603b61dSJack Morgenstein return sprintf(buf, "%d\n", dev->mdev->priv.fw_pages); 2153e126ba97SEli Cohen } 2154e126ba97SEli Cohen 2155e126ba97SEli Cohen static ssize_t show_reg_pages(struct device *device, 2156e126ba97SEli Cohen struct device_attribute *attr, char *buf) 2157e126ba97SEli Cohen { 2158e126ba97SEli Cohen struct mlx5_ib_dev *dev = 2159e126ba97SEli Cohen container_of(device, struct mlx5_ib_dev, ib_dev.dev); 2160e126ba97SEli Cohen 21616aec21f6SHaggai Eran return sprintf(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages)); 2162e126ba97SEli Cohen } 2163e126ba97SEli Cohen 2164e126ba97SEli Cohen static ssize_t show_hca(struct device *device, struct device_attribute *attr, 2165e126ba97SEli Cohen char *buf) 2166e126ba97SEli Cohen { 2167e126ba97SEli Cohen struct mlx5_ib_dev *dev = 2168e126ba97SEli Cohen container_of(device, struct mlx5_ib_dev, ib_dev.dev); 21699603b61dSJack Morgenstein return sprintf(buf, "MT%d\n", dev->mdev->pdev->device); 2170e126ba97SEli Cohen } 2171e126ba97SEli Cohen 2172e126ba97SEli Cohen static ssize_t show_rev(struct device *device, struct device_attribute *attr, 2173e126ba97SEli Cohen char *buf) 2174e126ba97SEli Cohen { 2175e126ba97SEli Cohen struct mlx5_ib_dev *dev = 2176e126ba97SEli Cohen container_of(device, struct mlx5_ib_dev, ib_dev.dev); 21779603b61dSJack Morgenstein return sprintf(buf, "%x\n", dev->mdev->rev_id); 2178e126ba97SEli Cohen } 2179e126ba97SEli Cohen 2180e126ba97SEli Cohen static ssize_t show_board(struct device *device, struct device_attribute *attr, 2181e126ba97SEli Cohen char *buf) 2182e126ba97SEli Cohen { 2183e126ba97SEli Cohen struct mlx5_ib_dev *dev = 2184e126ba97SEli Cohen container_of(device, struct mlx5_ib_dev, ib_dev.dev); 2185e126ba97SEli Cohen return sprintf(buf, "%.*s\n", MLX5_BOARD_ID_LEN, 21869603b61dSJack Morgenstein dev->mdev->board_id); 2187e126ba97SEli Cohen } 2188e126ba97SEli Cohen 2189e126ba97SEli Cohen static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL); 2190e126ba97SEli Cohen static DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL); 2191e126ba97SEli Cohen static DEVICE_ATTR(board_id, S_IRUGO, show_board, NULL); 2192e126ba97SEli Cohen static DEVICE_ATTR(fw_pages, S_IRUGO, show_fw_pages, NULL); 2193e126ba97SEli Cohen static DEVICE_ATTR(reg_pages, S_IRUGO, show_reg_pages, NULL); 2194e126ba97SEli Cohen 2195e126ba97SEli Cohen static struct device_attribute *mlx5_class_attributes[] = { 2196e126ba97SEli Cohen &dev_attr_hw_rev, 2197e126ba97SEli Cohen &dev_attr_hca_type, 2198e126ba97SEli Cohen &dev_attr_board_id, 2199e126ba97SEli Cohen &dev_attr_fw_pages, 2200e126ba97SEli Cohen &dev_attr_reg_pages, 2201e126ba97SEli Cohen }; 2202e126ba97SEli Cohen 22037722f47eSHaggai Eran static void pkey_change_handler(struct work_struct *work) 22047722f47eSHaggai Eran { 22057722f47eSHaggai Eran struct mlx5_ib_port_resources *ports = 22067722f47eSHaggai Eran container_of(work, struct mlx5_ib_port_resources, 22077722f47eSHaggai Eran pkey_change_work); 22087722f47eSHaggai Eran 22097722f47eSHaggai Eran mutex_lock(&ports->devr->mutex); 22107722f47eSHaggai Eran mlx5_ib_gsi_pkey_change(ports->gsi); 22117722f47eSHaggai Eran mutex_unlock(&ports->devr->mutex); 22127722f47eSHaggai Eran } 22137722f47eSHaggai Eran 221489ea94a7SMaor Gottlieb static void mlx5_ib_handle_internal_error(struct mlx5_ib_dev *ibdev) 221589ea94a7SMaor Gottlieb { 221689ea94a7SMaor Gottlieb struct mlx5_ib_qp *mqp; 221789ea94a7SMaor Gottlieb struct mlx5_ib_cq *send_mcq, *recv_mcq; 221889ea94a7SMaor Gottlieb struct mlx5_core_cq *mcq; 221989ea94a7SMaor Gottlieb struct list_head cq_armed_list; 222089ea94a7SMaor Gottlieb unsigned long flags_qp; 222189ea94a7SMaor Gottlieb unsigned long flags_cq; 222289ea94a7SMaor Gottlieb unsigned long flags; 222389ea94a7SMaor Gottlieb 222489ea94a7SMaor Gottlieb INIT_LIST_HEAD(&cq_armed_list); 222589ea94a7SMaor Gottlieb 222689ea94a7SMaor Gottlieb /* Go over qp list reside on that ibdev, sync with create/destroy qp.*/ 222789ea94a7SMaor Gottlieb spin_lock_irqsave(&ibdev->reset_flow_resource_lock, flags); 222889ea94a7SMaor Gottlieb list_for_each_entry(mqp, &ibdev->qp_list, qps_list) { 222989ea94a7SMaor Gottlieb spin_lock_irqsave(&mqp->sq.lock, flags_qp); 223089ea94a7SMaor Gottlieb if (mqp->sq.tail != mqp->sq.head) { 223189ea94a7SMaor Gottlieb send_mcq = to_mcq(mqp->ibqp.send_cq); 223289ea94a7SMaor Gottlieb spin_lock_irqsave(&send_mcq->lock, flags_cq); 223389ea94a7SMaor Gottlieb if (send_mcq->mcq.comp && 223489ea94a7SMaor Gottlieb mqp->ibqp.send_cq->comp_handler) { 223589ea94a7SMaor Gottlieb if (!send_mcq->mcq.reset_notify_added) { 223689ea94a7SMaor Gottlieb send_mcq->mcq.reset_notify_added = 1; 223789ea94a7SMaor Gottlieb list_add_tail(&send_mcq->mcq.reset_notify, 223889ea94a7SMaor Gottlieb &cq_armed_list); 223989ea94a7SMaor Gottlieb } 224089ea94a7SMaor Gottlieb } 224189ea94a7SMaor Gottlieb spin_unlock_irqrestore(&send_mcq->lock, flags_cq); 224289ea94a7SMaor Gottlieb } 224389ea94a7SMaor Gottlieb spin_unlock_irqrestore(&mqp->sq.lock, flags_qp); 224489ea94a7SMaor Gottlieb spin_lock_irqsave(&mqp->rq.lock, flags_qp); 224589ea94a7SMaor Gottlieb /* no handling is needed for SRQ */ 224689ea94a7SMaor Gottlieb if (!mqp->ibqp.srq) { 224789ea94a7SMaor Gottlieb if (mqp->rq.tail != mqp->rq.head) { 224889ea94a7SMaor Gottlieb recv_mcq = to_mcq(mqp->ibqp.recv_cq); 224989ea94a7SMaor Gottlieb spin_lock_irqsave(&recv_mcq->lock, flags_cq); 225089ea94a7SMaor Gottlieb if (recv_mcq->mcq.comp && 225189ea94a7SMaor Gottlieb mqp->ibqp.recv_cq->comp_handler) { 225289ea94a7SMaor Gottlieb if (!recv_mcq->mcq.reset_notify_added) { 225389ea94a7SMaor Gottlieb recv_mcq->mcq.reset_notify_added = 1; 225489ea94a7SMaor Gottlieb list_add_tail(&recv_mcq->mcq.reset_notify, 225589ea94a7SMaor Gottlieb &cq_armed_list); 225689ea94a7SMaor Gottlieb } 225789ea94a7SMaor Gottlieb } 225889ea94a7SMaor Gottlieb spin_unlock_irqrestore(&recv_mcq->lock, 225989ea94a7SMaor Gottlieb flags_cq); 226089ea94a7SMaor Gottlieb } 226189ea94a7SMaor Gottlieb } 226289ea94a7SMaor Gottlieb spin_unlock_irqrestore(&mqp->rq.lock, flags_qp); 226389ea94a7SMaor Gottlieb } 226489ea94a7SMaor Gottlieb /*At that point all inflight post send were put to be executed as of we 226589ea94a7SMaor Gottlieb * lock/unlock above locks Now need to arm all involved CQs. 226689ea94a7SMaor Gottlieb */ 226789ea94a7SMaor Gottlieb list_for_each_entry(mcq, &cq_armed_list, reset_notify) { 226889ea94a7SMaor Gottlieb mcq->comp(mcq); 226989ea94a7SMaor Gottlieb } 227089ea94a7SMaor Gottlieb spin_unlock_irqrestore(&ibdev->reset_flow_resource_lock, flags); 227189ea94a7SMaor Gottlieb } 227289ea94a7SMaor Gottlieb 22739603b61dSJack Morgenstein static void mlx5_ib_event(struct mlx5_core_dev *dev, void *context, 22744d2f9bbbSJack Morgenstein enum mlx5_dev_event event, unsigned long param) 2275e126ba97SEli Cohen { 22769603b61dSJack Morgenstein struct mlx5_ib_dev *ibdev = (struct mlx5_ib_dev *)context; 2277e126ba97SEli Cohen struct ib_event ibev; 22789603b61dSJack Morgenstein 2279e126ba97SEli Cohen u8 port = 0; 2280e126ba97SEli Cohen 2281e126ba97SEli Cohen switch (event) { 2282e126ba97SEli Cohen case MLX5_DEV_EVENT_SYS_ERROR: 2283e126ba97SEli Cohen ibdev->ib_active = false; 2284e126ba97SEli Cohen ibev.event = IB_EVENT_DEVICE_FATAL; 228589ea94a7SMaor Gottlieb mlx5_ib_handle_internal_error(ibdev); 2286e126ba97SEli Cohen break; 2287e126ba97SEli Cohen 2288e126ba97SEli Cohen case MLX5_DEV_EVENT_PORT_UP: 2289e126ba97SEli Cohen case MLX5_DEV_EVENT_PORT_DOWN: 22902788cf3bSNoa Osherovich case MLX5_DEV_EVENT_PORT_INITIALIZED: 22914d2f9bbbSJack Morgenstein port = (u8)param; 2292*5ec8c83eSAviv Heller 2293*5ec8c83eSAviv Heller /* In RoCE, port up/down events are handled in 2294*5ec8c83eSAviv Heller * mlx5_netdev_event(). 2295*5ec8c83eSAviv Heller */ 2296*5ec8c83eSAviv Heller if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) == 2297*5ec8c83eSAviv Heller IB_LINK_LAYER_ETHERNET) 2298*5ec8c83eSAviv Heller return; 2299*5ec8c83eSAviv Heller 2300*5ec8c83eSAviv Heller ibev.event = (event == MLX5_DEV_EVENT_PORT_UP) ? 2301*5ec8c83eSAviv Heller IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR; 2302e126ba97SEli Cohen break; 2303e126ba97SEli Cohen 2304e126ba97SEli Cohen case MLX5_DEV_EVENT_LID_CHANGE: 2305e126ba97SEli Cohen ibev.event = IB_EVENT_LID_CHANGE; 23064d2f9bbbSJack Morgenstein port = (u8)param; 2307e126ba97SEli Cohen break; 2308e126ba97SEli Cohen 2309e126ba97SEli Cohen case MLX5_DEV_EVENT_PKEY_CHANGE: 2310e126ba97SEli Cohen ibev.event = IB_EVENT_PKEY_CHANGE; 23114d2f9bbbSJack Morgenstein port = (u8)param; 23127722f47eSHaggai Eran 23137722f47eSHaggai Eran schedule_work(&ibdev->devr.ports[port - 1].pkey_change_work); 2314e126ba97SEli Cohen break; 2315e126ba97SEli Cohen 2316e126ba97SEli Cohen case MLX5_DEV_EVENT_GUID_CHANGE: 2317e126ba97SEli Cohen ibev.event = IB_EVENT_GID_CHANGE; 23184d2f9bbbSJack Morgenstein port = (u8)param; 2319e126ba97SEli Cohen break; 2320e126ba97SEli Cohen 2321e126ba97SEli Cohen case MLX5_DEV_EVENT_CLIENT_REREG: 2322e126ba97SEli Cohen ibev.event = IB_EVENT_CLIENT_REREGISTER; 23234d2f9bbbSJack Morgenstein port = (u8)param; 2324e126ba97SEli Cohen break; 2325e126ba97SEli Cohen } 2326e126ba97SEli Cohen 2327e126ba97SEli Cohen ibev.device = &ibdev->ib_dev; 2328e126ba97SEli Cohen ibev.element.port_num = port; 2329e126ba97SEli Cohen 2330a0c84c32SEli Cohen if (port < 1 || port > ibdev->num_ports) { 2331a0c84c32SEli Cohen mlx5_ib_warn(ibdev, "warning: event on port %d\n", port); 2332a0c84c32SEli Cohen return; 2333a0c84c32SEli Cohen } 2334a0c84c32SEli Cohen 2335e126ba97SEli Cohen if (ibdev->ib_active) 2336e126ba97SEli Cohen ib_dispatch_event(&ibev); 2337e126ba97SEli Cohen } 2338e126ba97SEli Cohen 2339e126ba97SEli Cohen static void get_ext_port_caps(struct mlx5_ib_dev *dev) 2340e126ba97SEli Cohen { 2341e126ba97SEli Cohen int port; 2342e126ba97SEli Cohen 2343938fe83cSSaeed Mahameed for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++) 2344e126ba97SEli Cohen mlx5_query_ext_port_caps(dev, port); 2345e126ba97SEli Cohen } 2346e126ba97SEli Cohen 2347e126ba97SEli Cohen static int get_port_caps(struct mlx5_ib_dev *dev) 2348e126ba97SEli Cohen { 2349e126ba97SEli Cohen struct ib_device_attr *dprops = NULL; 2350e126ba97SEli Cohen struct ib_port_attr *pprops = NULL; 2351f614fc15SDan Carpenter int err = -ENOMEM; 2352e126ba97SEli Cohen int port; 23532528e33eSMatan Barak struct ib_udata uhw = {.inlen = 0, .outlen = 0}; 2354e126ba97SEli Cohen 2355e126ba97SEli Cohen pprops = kmalloc(sizeof(*pprops), GFP_KERNEL); 2356e126ba97SEli Cohen if (!pprops) 2357e126ba97SEli Cohen goto out; 2358e126ba97SEli Cohen 2359e126ba97SEli Cohen dprops = kmalloc(sizeof(*dprops), GFP_KERNEL); 2360e126ba97SEli Cohen if (!dprops) 2361e126ba97SEli Cohen goto out; 2362e126ba97SEli Cohen 23632528e33eSMatan Barak err = mlx5_ib_query_device(&dev->ib_dev, dprops, &uhw); 2364e126ba97SEli Cohen if (err) { 2365e126ba97SEli Cohen mlx5_ib_warn(dev, "query_device failed %d\n", err); 2366e126ba97SEli Cohen goto out; 2367e126ba97SEli Cohen } 2368e126ba97SEli Cohen 2369938fe83cSSaeed Mahameed for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++) { 2370e126ba97SEli Cohen err = mlx5_ib_query_port(&dev->ib_dev, port, pprops); 2371e126ba97SEli Cohen if (err) { 2372938fe83cSSaeed Mahameed mlx5_ib_warn(dev, "query_port %d failed %d\n", 2373938fe83cSSaeed Mahameed port, err); 2374e126ba97SEli Cohen break; 2375e126ba97SEli Cohen } 2376938fe83cSSaeed Mahameed dev->mdev->port_caps[port - 1].pkey_table_len = 2377938fe83cSSaeed Mahameed dprops->max_pkeys; 2378938fe83cSSaeed Mahameed dev->mdev->port_caps[port - 1].gid_table_len = 2379938fe83cSSaeed Mahameed pprops->gid_tbl_len; 2380e126ba97SEli Cohen mlx5_ib_dbg(dev, "pkey_table_len %d, gid_table_len %d\n", 2381e126ba97SEli Cohen dprops->max_pkeys, pprops->gid_tbl_len); 2382e126ba97SEli Cohen } 2383e126ba97SEli Cohen 2384e126ba97SEli Cohen out: 2385e126ba97SEli Cohen kfree(pprops); 2386e126ba97SEli Cohen kfree(dprops); 2387e126ba97SEli Cohen 2388e126ba97SEli Cohen return err; 2389e126ba97SEli Cohen } 2390e126ba97SEli Cohen 2391e126ba97SEli Cohen static void destroy_umrc_res(struct mlx5_ib_dev *dev) 2392e126ba97SEli Cohen { 2393e126ba97SEli Cohen int err; 2394e126ba97SEli Cohen 2395e126ba97SEli Cohen err = mlx5_mr_cache_cleanup(dev); 2396e126ba97SEli Cohen if (err) 2397e126ba97SEli Cohen mlx5_ib_warn(dev, "mr cache cleanup failed\n"); 2398e126ba97SEli Cohen 2399e126ba97SEli Cohen mlx5_ib_destroy_qp(dev->umrc.qp); 2400add08d76SChristoph Hellwig ib_free_cq(dev->umrc.cq); 2401e126ba97SEli Cohen ib_dealloc_pd(dev->umrc.pd); 2402e126ba97SEli Cohen } 2403e126ba97SEli Cohen 2404e126ba97SEli Cohen enum { 2405e126ba97SEli Cohen MAX_UMR_WR = 128, 2406e126ba97SEli Cohen }; 2407e126ba97SEli Cohen 2408e126ba97SEli Cohen static int create_umr_res(struct mlx5_ib_dev *dev) 2409e126ba97SEli Cohen { 2410e126ba97SEli Cohen struct ib_qp_init_attr *init_attr = NULL; 2411e126ba97SEli Cohen struct ib_qp_attr *attr = NULL; 2412e126ba97SEli Cohen struct ib_pd *pd; 2413e126ba97SEli Cohen struct ib_cq *cq; 2414e126ba97SEli Cohen struct ib_qp *qp; 2415e126ba97SEli Cohen int ret; 2416e126ba97SEli Cohen 2417e126ba97SEli Cohen attr = kzalloc(sizeof(*attr), GFP_KERNEL); 2418e126ba97SEli Cohen init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL); 2419e126ba97SEli Cohen if (!attr || !init_attr) { 2420e126ba97SEli Cohen ret = -ENOMEM; 2421e126ba97SEli Cohen goto error_0; 2422e126ba97SEli Cohen } 2423e126ba97SEli Cohen 2424ed082d36SChristoph Hellwig pd = ib_alloc_pd(&dev->ib_dev, 0); 2425e126ba97SEli Cohen if (IS_ERR(pd)) { 2426e126ba97SEli Cohen mlx5_ib_dbg(dev, "Couldn't create PD for sync UMR QP\n"); 2427e126ba97SEli Cohen ret = PTR_ERR(pd); 2428e126ba97SEli Cohen goto error_0; 2429e126ba97SEli Cohen } 2430e126ba97SEli Cohen 2431add08d76SChristoph Hellwig cq = ib_alloc_cq(&dev->ib_dev, NULL, 128, 0, IB_POLL_SOFTIRQ); 2432e126ba97SEli Cohen if (IS_ERR(cq)) { 2433e126ba97SEli Cohen mlx5_ib_dbg(dev, "Couldn't create CQ for sync UMR QP\n"); 2434e126ba97SEli Cohen ret = PTR_ERR(cq); 2435e126ba97SEli Cohen goto error_2; 2436e126ba97SEli Cohen } 2437e126ba97SEli Cohen 2438e126ba97SEli Cohen init_attr->send_cq = cq; 2439e126ba97SEli Cohen init_attr->recv_cq = cq; 2440e126ba97SEli Cohen init_attr->sq_sig_type = IB_SIGNAL_ALL_WR; 2441e126ba97SEli Cohen init_attr->cap.max_send_wr = MAX_UMR_WR; 2442e126ba97SEli Cohen init_attr->cap.max_send_sge = 1; 2443e126ba97SEli Cohen init_attr->qp_type = MLX5_IB_QPT_REG_UMR; 2444e126ba97SEli Cohen init_attr->port_num = 1; 2445e126ba97SEli Cohen qp = mlx5_ib_create_qp(pd, init_attr, NULL); 2446e126ba97SEli Cohen if (IS_ERR(qp)) { 2447e126ba97SEli Cohen mlx5_ib_dbg(dev, "Couldn't create sync UMR QP\n"); 2448e126ba97SEli Cohen ret = PTR_ERR(qp); 2449e126ba97SEli Cohen goto error_3; 2450e126ba97SEli Cohen } 2451e126ba97SEli Cohen qp->device = &dev->ib_dev; 2452e126ba97SEli Cohen qp->real_qp = qp; 2453e126ba97SEli Cohen qp->uobject = NULL; 2454e126ba97SEli Cohen qp->qp_type = MLX5_IB_QPT_REG_UMR; 2455e126ba97SEli Cohen 2456e126ba97SEli Cohen attr->qp_state = IB_QPS_INIT; 2457e126ba97SEli Cohen attr->port_num = 1; 2458e126ba97SEli Cohen ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_PKEY_INDEX | 2459e126ba97SEli Cohen IB_QP_PORT, NULL); 2460e126ba97SEli Cohen if (ret) { 2461e126ba97SEli Cohen mlx5_ib_dbg(dev, "Couldn't modify UMR QP\n"); 2462e126ba97SEli Cohen goto error_4; 2463e126ba97SEli Cohen } 2464e126ba97SEli Cohen 2465e126ba97SEli Cohen memset(attr, 0, sizeof(*attr)); 2466e126ba97SEli Cohen attr->qp_state = IB_QPS_RTR; 2467e126ba97SEli Cohen attr->path_mtu = IB_MTU_256; 2468e126ba97SEli Cohen 2469e126ba97SEli Cohen ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL); 2470e126ba97SEli Cohen if (ret) { 2471e126ba97SEli Cohen mlx5_ib_dbg(dev, "Couldn't modify umr QP to rtr\n"); 2472e126ba97SEli Cohen goto error_4; 2473e126ba97SEli Cohen } 2474e126ba97SEli Cohen 2475e126ba97SEli Cohen memset(attr, 0, sizeof(*attr)); 2476e126ba97SEli Cohen attr->qp_state = IB_QPS_RTS; 2477e126ba97SEli Cohen ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL); 2478e126ba97SEli Cohen if (ret) { 2479e126ba97SEli Cohen mlx5_ib_dbg(dev, "Couldn't modify umr QP to rts\n"); 2480e126ba97SEli Cohen goto error_4; 2481e126ba97SEli Cohen } 2482e126ba97SEli Cohen 2483e126ba97SEli Cohen dev->umrc.qp = qp; 2484e126ba97SEli Cohen dev->umrc.cq = cq; 2485e126ba97SEli Cohen dev->umrc.pd = pd; 2486e126ba97SEli Cohen 2487e126ba97SEli Cohen sema_init(&dev->umrc.sem, MAX_UMR_WR); 2488e126ba97SEli Cohen ret = mlx5_mr_cache_init(dev); 2489e126ba97SEli Cohen if (ret) { 2490e126ba97SEli Cohen mlx5_ib_warn(dev, "mr cache init failed %d\n", ret); 2491e126ba97SEli Cohen goto error_4; 2492e126ba97SEli Cohen } 2493e126ba97SEli Cohen 2494e126ba97SEli Cohen kfree(attr); 2495e126ba97SEli Cohen kfree(init_attr); 2496e126ba97SEli Cohen 2497e126ba97SEli Cohen return 0; 2498e126ba97SEli Cohen 2499e126ba97SEli Cohen error_4: 2500e126ba97SEli Cohen mlx5_ib_destroy_qp(qp); 2501e126ba97SEli Cohen 2502e126ba97SEli Cohen error_3: 2503add08d76SChristoph Hellwig ib_free_cq(cq); 2504e126ba97SEli Cohen 2505e126ba97SEli Cohen error_2: 2506e126ba97SEli Cohen ib_dealloc_pd(pd); 2507e126ba97SEli Cohen 2508e126ba97SEli Cohen error_0: 2509e126ba97SEli Cohen kfree(attr); 2510e126ba97SEli Cohen kfree(init_attr); 2511e126ba97SEli Cohen return ret; 2512e126ba97SEli Cohen } 2513e126ba97SEli Cohen 2514e126ba97SEli Cohen static int create_dev_resources(struct mlx5_ib_resources *devr) 2515e126ba97SEli Cohen { 2516e126ba97SEli Cohen struct ib_srq_init_attr attr; 2517e126ba97SEli Cohen struct mlx5_ib_dev *dev; 2518bcf4c1eaSMatan Barak struct ib_cq_init_attr cq_attr = {.cqe = 1}; 25197722f47eSHaggai Eran int port; 2520e126ba97SEli Cohen int ret = 0; 2521e126ba97SEli Cohen 2522e126ba97SEli Cohen dev = container_of(devr, struct mlx5_ib_dev, devr); 2523e126ba97SEli Cohen 2524d16e91daSHaggai Eran mutex_init(&devr->mutex); 2525d16e91daSHaggai Eran 2526e126ba97SEli Cohen devr->p0 = mlx5_ib_alloc_pd(&dev->ib_dev, NULL, NULL); 2527e126ba97SEli Cohen if (IS_ERR(devr->p0)) { 2528e126ba97SEli Cohen ret = PTR_ERR(devr->p0); 2529e126ba97SEli Cohen goto error0; 2530e126ba97SEli Cohen } 2531e126ba97SEli Cohen devr->p0->device = &dev->ib_dev; 2532e126ba97SEli Cohen devr->p0->uobject = NULL; 2533e126ba97SEli Cohen atomic_set(&devr->p0->usecnt, 0); 2534e126ba97SEli Cohen 2535bcf4c1eaSMatan Barak devr->c0 = mlx5_ib_create_cq(&dev->ib_dev, &cq_attr, NULL, NULL); 2536e126ba97SEli Cohen if (IS_ERR(devr->c0)) { 2537e126ba97SEli Cohen ret = PTR_ERR(devr->c0); 2538e126ba97SEli Cohen goto error1; 2539e126ba97SEli Cohen } 2540e126ba97SEli Cohen devr->c0->device = &dev->ib_dev; 2541e126ba97SEli Cohen devr->c0->uobject = NULL; 2542e126ba97SEli Cohen devr->c0->comp_handler = NULL; 2543e126ba97SEli Cohen devr->c0->event_handler = NULL; 2544e126ba97SEli Cohen devr->c0->cq_context = NULL; 2545e126ba97SEli Cohen atomic_set(&devr->c0->usecnt, 0); 2546e126ba97SEli Cohen 2547e126ba97SEli Cohen devr->x0 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL); 2548e126ba97SEli Cohen if (IS_ERR(devr->x0)) { 2549e126ba97SEli Cohen ret = PTR_ERR(devr->x0); 2550e126ba97SEli Cohen goto error2; 2551e126ba97SEli Cohen } 2552e126ba97SEli Cohen devr->x0->device = &dev->ib_dev; 2553e126ba97SEli Cohen devr->x0->inode = NULL; 2554e126ba97SEli Cohen atomic_set(&devr->x0->usecnt, 0); 2555e126ba97SEli Cohen mutex_init(&devr->x0->tgt_qp_mutex); 2556e126ba97SEli Cohen INIT_LIST_HEAD(&devr->x0->tgt_qp_list); 2557e126ba97SEli Cohen 2558e126ba97SEli Cohen devr->x1 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL); 2559e126ba97SEli Cohen if (IS_ERR(devr->x1)) { 2560e126ba97SEli Cohen ret = PTR_ERR(devr->x1); 2561e126ba97SEli Cohen goto error3; 2562e126ba97SEli Cohen } 2563e126ba97SEli Cohen devr->x1->device = &dev->ib_dev; 2564e126ba97SEli Cohen devr->x1->inode = NULL; 2565e126ba97SEli Cohen atomic_set(&devr->x1->usecnt, 0); 2566e126ba97SEli Cohen mutex_init(&devr->x1->tgt_qp_mutex); 2567e126ba97SEli Cohen INIT_LIST_HEAD(&devr->x1->tgt_qp_list); 2568e126ba97SEli Cohen 2569e126ba97SEli Cohen memset(&attr, 0, sizeof(attr)); 2570e126ba97SEli Cohen attr.attr.max_sge = 1; 2571e126ba97SEli Cohen attr.attr.max_wr = 1; 2572e126ba97SEli Cohen attr.srq_type = IB_SRQT_XRC; 2573e126ba97SEli Cohen attr.ext.xrc.cq = devr->c0; 2574e126ba97SEli Cohen attr.ext.xrc.xrcd = devr->x0; 2575e126ba97SEli Cohen 2576e126ba97SEli Cohen devr->s0 = mlx5_ib_create_srq(devr->p0, &attr, NULL); 2577e126ba97SEli Cohen if (IS_ERR(devr->s0)) { 2578e126ba97SEli Cohen ret = PTR_ERR(devr->s0); 2579e126ba97SEli Cohen goto error4; 2580e126ba97SEli Cohen } 2581e126ba97SEli Cohen devr->s0->device = &dev->ib_dev; 2582e126ba97SEli Cohen devr->s0->pd = devr->p0; 2583e126ba97SEli Cohen devr->s0->uobject = NULL; 2584e126ba97SEli Cohen devr->s0->event_handler = NULL; 2585e126ba97SEli Cohen devr->s0->srq_context = NULL; 2586e126ba97SEli Cohen devr->s0->srq_type = IB_SRQT_XRC; 2587e126ba97SEli Cohen devr->s0->ext.xrc.xrcd = devr->x0; 2588e126ba97SEli Cohen devr->s0->ext.xrc.cq = devr->c0; 2589e126ba97SEli Cohen atomic_inc(&devr->s0->ext.xrc.xrcd->usecnt); 2590e126ba97SEli Cohen atomic_inc(&devr->s0->ext.xrc.cq->usecnt); 2591e126ba97SEli Cohen atomic_inc(&devr->p0->usecnt); 2592e126ba97SEli Cohen atomic_set(&devr->s0->usecnt, 0); 2593e126ba97SEli Cohen 25944aa17b28SHaggai Abramonvsky memset(&attr, 0, sizeof(attr)); 25954aa17b28SHaggai Abramonvsky attr.attr.max_sge = 1; 25964aa17b28SHaggai Abramonvsky attr.attr.max_wr = 1; 25974aa17b28SHaggai Abramonvsky attr.srq_type = IB_SRQT_BASIC; 25984aa17b28SHaggai Abramonvsky devr->s1 = mlx5_ib_create_srq(devr->p0, &attr, NULL); 25994aa17b28SHaggai Abramonvsky if (IS_ERR(devr->s1)) { 26004aa17b28SHaggai Abramonvsky ret = PTR_ERR(devr->s1); 26014aa17b28SHaggai Abramonvsky goto error5; 26024aa17b28SHaggai Abramonvsky } 26034aa17b28SHaggai Abramonvsky devr->s1->device = &dev->ib_dev; 26044aa17b28SHaggai Abramonvsky devr->s1->pd = devr->p0; 26054aa17b28SHaggai Abramonvsky devr->s1->uobject = NULL; 26064aa17b28SHaggai Abramonvsky devr->s1->event_handler = NULL; 26074aa17b28SHaggai Abramonvsky devr->s1->srq_context = NULL; 26084aa17b28SHaggai Abramonvsky devr->s1->srq_type = IB_SRQT_BASIC; 26094aa17b28SHaggai Abramonvsky devr->s1->ext.xrc.cq = devr->c0; 26104aa17b28SHaggai Abramonvsky atomic_inc(&devr->p0->usecnt); 26114aa17b28SHaggai Abramonvsky atomic_set(&devr->s0->usecnt, 0); 26124aa17b28SHaggai Abramonvsky 26137722f47eSHaggai Eran for (port = 0; port < ARRAY_SIZE(devr->ports); ++port) { 26147722f47eSHaggai Eran INIT_WORK(&devr->ports[port].pkey_change_work, 26157722f47eSHaggai Eran pkey_change_handler); 26167722f47eSHaggai Eran devr->ports[port].devr = devr; 26177722f47eSHaggai Eran } 26187722f47eSHaggai Eran 2619e126ba97SEli Cohen return 0; 2620e126ba97SEli Cohen 26214aa17b28SHaggai Abramonvsky error5: 26224aa17b28SHaggai Abramonvsky mlx5_ib_destroy_srq(devr->s0); 2623e126ba97SEli Cohen error4: 2624e126ba97SEli Cohen mlx5_ib_dealloc_xrcd(devr->x1); 2625e126ba97SEli Cohen error3: 2626e126ba97SEli Cohen mlx5_ib_dealloc_xrcd(devr->x0); 2627e126ba97SEli Cohen error2: 2628e126ba97SEli Cohen mlx5_ib_destroy_cq(devr->c0); 2629e126ba97SEli Cohen error1: 2630e126ba97SEli Cohen mlx5_ib_dealloc_pd(devr->p0); 2631e126ba97SEli Cohen error0: 2632e126ba97SEli Cohen return ret; 2633e126ba97SEli Cohen } 2634e126ba97SEli Cohen 2635e126ba97SEli Cohen static void destroy_dev_resources(struct mlx5_ib_resources *devr) 2636e126ba97SEli Cohen { 26377722f47eSHaggai Eran struct mlx5_ib_dev *dev = 26387722f47eSHaggai Eran container_of(devr, struct mlx5_ib_dev, devr); 26397722f47eSHaggai Eran int port; 26407722f47eSHaggai Eran 26414aa17b28SHaggai Abramonvsky mlx5_ib_destroy_srq(devr->s1); 2642e126ba97SEli Cohen mlx5_ib_destroy_srq(devr->s0); 2643e126ba97SEli Cohen mlx5_ib_dealloc_xrcd(devr->x0); 2644e126ba97SEli Cohen mlx5_ib_dealloc_xrcd(devr->x1); 2645e126ba97SEli Cohen mlx5_ib_destroy_cq(devr->c0); 2646e126ba97SEli Cohen mlx5_ib_dealloc_pd(devr->p0); 26477722f47eSHaggai Eran 26487722f47eSHaggai Eran /* Make sure no change P_Key work items are still executing */ 26497722f47eSHaggai Eran for (port = 0; port < dev->num_ports; ++port) 26507722f47eSHaggai Eran cancel_work_sync(&devr->ports[port].pkey_change_work); 2651e126ba97SEli Cohen } 2652e126ba97SEli Cohen 2653e53505a8SAchiad Shochat static u32 get_core_cap_flags(struct ib_device *ibdev) 2654e53505a8SAchiad Shochat { 2655e53505a8SAchiad Shochat struct mlx5_ib_dev *dev = to_mdev(ibdev); 2656e53505a8SAchiad Shochat enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, 1); 2657e53505a8SAchiad Shochat u8 l3_type_cap = MLX5_CAP_ROCE(dev->mdev, l3_type); 2658e53505a8SAchiad Shochat u8 roce_version_cap = MLX5_CAP_ROCE(dev->mdev, roce_version); 2659e53505a8SAchiad Shochat u32 ret = 0; 2660e53505a8SAchiad Shochat 2661e53505a8SAchiad Shochat if (ll == IB_LINK_LAYER_INFINIBAND) 2662e53505a8SAchiad Shochat return RDMA_CORE_PORT_IBA_IB; 2663e53505a8SAchiad Shochat 2664e53505a8SAchiad Shochat if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV4_CAP)) 2665e53505a8SAchiad Shochat return 0; 2666e53505a8SAchiad Shochat 2667e53505a8SAchiad Shochat if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV6_CAP)) 2668e53505a8SAchiad Shochat return 0; 2669e53505a8SAchiad Shochat 2670e53505a8SAchiad Shochat if (roce_version_cap & MLX5_ROCE_VERSION_1_CAP) 2671e53505a8SAchiad Shochat ret |= RDMA_CORE_PORT_IBA_ROCE; 2672e53505a8SAchiad Shochat 2673e53505a8SAchiad Shochat if (roce_version_cap & MLX5_ROCE_VERSION_2_CAP) 2674e53505a8SAchiad Shochat ret |= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP; 2675e53505a8SAchiad Shochat 2676e53505a8SAchiad Shochat return ret; 2677e53505a8SAchiad Shochat } 2678e53505a8SAchiad Shochat 26797738613eSIra Weiny static int mlx5_port_immutable(struct ib_device *ibdev, u8 port_num, 26807738613eSIra Weiny struct ib_port_immutable *immutable) 26817738613eSIra Weiny { 26827738613eSIra Weiny struct ib_port_attr attr; 26837738613eSIra Weiny int err; 26847738613eSIra Weiny 26857738613eSIra Weiny err = mlx5_ib_query_port(ibdev, port_num, &attr); 26867738613eSIra Weiny if (err) 26877738613eSIra Weiny return err; 26887738613eSIra Weiny 26897738613eSIra Weiny immutable->pkey_tbl_len = attr.pkey_tbl_len; 26907738613eSIra Weiny immutable->gid_tbl_len = attr.gid_tbl_len; 2691e53505a8SAchiad Shochat immutable->core_cap_flags = get_core_cap_flags(ibdev); 2692337877a4SIra Weiny immutable->max_mad_size = IB_MGMT_MAD_SIZE; 26937738613eSIra Weiny 26947738613eSIra Weiny return 0; 26957738613eSIra Weiny } 26967738613eSIra Weiny 2697c7342823SIra Weiny static void get_dev_fw_str(struct ib_device *ibdev, char *str, 2698c7342823SIra Weiny size_t str_len) 2699c7342823SIra Weiny { 2700c7342823SIra Weiny struct mlx5_ib_dev *dev = 2701c7342823SIra Weiny container_of(ibdev, struct mlx5_ib_dev, ib_dev); 2702c7342823SIra Weiny snprintf(str, str_len, "%d.%d.%04d", fw_rev_maj(dev->mdev), 2703c7342823SIra Weiny fw_rev_min(dev->mdev), fw_rev_sub(dev->mdev)); 2704c7342823SIra Weiny } 2705c7342823SIra Weiny 2706*5ec8c83eSAviv Heller static void mlx5_remove_roce_notifier(struct mlx5_ib_dev *dev) 2707*5ec8c83eSAviv Heller { 2708*5ec8c83eSAviv Heller if (dev->roce.nb.notifier_call) { 2709*5ec8c83eSAviv Heller unregister_netdevice_notifier(&dev->roce.nb); 2710*5ec8c83eSAviv Heller dev->roce.nb.notifier_call = NULL; 2711*5ec8c83eSAviv Heller } 2712*5ec8c83eSAviv Heller } 2713*5ec8c83eSAviv Heller 2714fc24fc5eSAchiad Shochat static int mlx5_enable_roce(struct mlx5_ib_dev *dev) 2715fc24fc5eSAchiad Shochat { 2716e53505a8SAchiad Shochat int err; 2717e53505a8SAchiad Shochat 2718fc24fc5eSAchiad Shochat dev->roce.nb.notifier_call = mlx5_netdev_event; 2719e53505a8SAchiad Shochat err = register_netdevice_notifier(&dev->roce.nb); 2720*5ec8c83eSAviv Heller if (err) { 2721*5ec8c83eSAviv Heller dev->roce.nb.notifier_call = NULL; 2722e53505a8SAchiad Shochat return err; 2723*5ec8c83eSAviv Heller } 2724e53505a8SAchiad Shochat 2725e53505a8SAchiad Shochat err = mlx5_nic_vport_enable_roce(dev->mdev); 2726e53505a8SAchiad Shochat if (err) 2727e53505a8SAchiad Shochat goto err_unregister_netdevice_notifier; 2728e53505a8SAchiad Shochat 2729e53505a8SAchiad Shochat return 0; 2730e53505a8SAchiad Shochat 2731e53505a8SAchiad Shochat err_unregister_netdevice_notifier: 2732*5ec8c83eSAviv Heller mlx5_remove_roce_notifier(dev); 2733e53505a8SAchiad Shochat return err; 2734fc24fc5eSAchiad Shochat } 2735fc24fc5eSAchiad Shochat 2736fc24fc5eSAchiad Shochat static void mlx5_disable_roce(struct mlx5_ib_dev *dev) 2737fc24fc5eSAchiad Shochat { 2738e53505a8SAchiad Shochat mlx5_nic_vport_disable_roce(dev->mdev); 2739fc24fc5eSAchiad Shochat } 2740fc24fc5eSAchiad Shochat 27410837e86aSMark Bloch static void mlx5_ib_dealloc_q_counters(struct mlx5_ib_dev *dev) 27420837e86aSMark Bloch { 27430837e86aSMark Bloch unsigned int i; 27440837e86aSMark Bloch 27450837e86aSMark Bloch for (i = 0; i < dev->num_ports; i++) 27460837e86aSMark Bloch mlx5_core_dealloc_q_counter(dev->mdev, 27470837e86aSMark Bloch dev->port[i].q_cnt_id); 27480837e86aSMark Bloch } 27490837e86aSMark Bloch 27500837e86aSMark Bloch static int mlx5_ib_alloc_q_counters(struct mlx5_ib_dev *dev) 27510837e86aSMark Bloch { 27520837e86aSMark Bloch int i; 27530837e86aSMark Bloch int ret; 27540837e86aSMark Bloch 27550837e86aSMark Bloch for (i = 0; i < dev->num_ports; i++) { 27560837e86aSMark Bloch ret = mlx5_core_alloc_q_counter(dev->mdev, 27570837e86aSMark Bloch &dev->port[i].q_cnt_id); 27580837e86aSMark Bloch if (ret) { 27590837e86aSMark Bloch mlx5_ib_warn(dev, 27600837e86aSMark Bloch "couldn't allocate queue counter for port %d, err %d\n", 27610837e86aSMark Bloch i + 1, ret); 27620837e86aSMark Bloch goto dealloc_counters; 27630837e86aSMark Bloch } 27640837e86aSMark Bloch } 27650837e86aSMark Bloch 27660837e86aSMark Bloch return 0; 27670837e86aSMark Bloch 27680837e86aSMark Bloch dealloc_counters: 27690837e86aSMark Bloch while (--i >= 0) 27700837e86aSMark Bloch mlx5_core_dealloc_q_counter(dev->mdev, 27710837e86aSMark Bloch dev->port[i].q_cnt_id); 27720837e86aSMark Bloch 27730837e86aSMark Bloch return ret; 27740837e86aSMark Bloch } 27750837e86aSMark Bloch 277661961500SWei Yongjun static const char * const names[] = { 27770ad17a8fSMark Bloch "rx_write_requests", 27780ad17a8fSMark Bloch "rx_read_requests", 27790ad17a8fSMark Bloch "rx_atomic_requests", 27800ad17a8fSMark Bloch "out_of_buffer", 27810ad17a8fSMark Bloch "out_of_sequence", 27820ad17a8fSMark Bloch "duplicate_request", 27830ad17a8fSMark Bloch "rnr_nak_retry_err", 27840ad17a8fSMark Bloch "packet_seq_err", 27850ad17a8fSMark Bloch "implied_nak_seq_err", 27860ad17a8fSMark Bloch "local_ack_timeout_err", 27870ad17a8fSMark Bloch }; 27880ad17a8fSMark Bloch 27890ad17a8fSMark Bloch static const size_t stats_offsets[] = { 27900ad17a8fSMark Bloch MLX5_BYTE_OFF(query_q_counter_out, rx_write_requests), 27910ad17a8fSMark Bloch MLX5_BYTE_OFF(query_q_counter_out, rx_read_requests), 27920ad17a8fSMark Bloch MLX5_BYTE_OFF(query_q_counter_out, rx_atomic_requests), 27930ad17a8fSMark Bloch MLX5_BYTE_OFF(query_q_counter_out, out_of_buffer), 27940ad17a8fSMark Bloch MLX5_BYTE_OFF(query_q_counter_out, out_of_sequence), 27950ad17a8fSMark Bloch MLX5_BYTE_OFF(query_q_counter_out, duplicate_request), 27960ad17a8fSMark Bloch MLX5_BYTE_OFF(query_q_counter_out, rnr_nak_retry_err), 27970ad17a8fSMark Bloch MLX5_BYTE_OFF(query_q_counter_out, packet_seq_err), 27980ad17a8fSMark Bloch MLX5_BYTE_OFF(query_q_counter_out, implied_nak_seq_err), 27990ad17a8fSMark Bloch MLX5_BYTE_OFF(query_q_counter_out, local_ack_timeout_err), 28000ad17a8fSMark Bloch }; 28010ad17a8fSMark Bloch 28020ad17a8fSMark Bloch static struct rdma_hw_stats *mlx5_ib_alloc_hw_stats(struct ib_device *ibdev, 28030ad17a8fSMark Bloch u8 port_num) 28040ad17a8fSMark Bloch { 28050ad17a8fSMark Bloch BUILD_BUG_ON(ARRAY_SIZE(names) != ARRAY_SIZE(stats_offsets)); 28060ad17a8fSMark Bloch 28070ad17a8fSMark Bloch /* We support only per port stats */ 28080ad17a8fSMark Bloch if (port_num == 0) 28090ad17a8fSMark Bloch return NULL; 28100ad17a8fSMark Bloch 28110ad17a8fSMark Bloch return rdma_alloc_hw_stats_struct(names, ARRAY_SIZE(names), 28120ad17a8fSMark Bloch RDMA_HW_STATS_DEFAULT_LIFESPAN); 28130ad17a8fSMark Bloch } 28140ad17a8fSMark Bloch 28150ad17a8fSMark Bloch static int mlx5_ib_get_hw_stats(struct ib_device *ibdev, 28160ad17a8fSMark Bloch struct rdma_hw_stats *stats, 28170ad17a8fSMark Bloch u8 port, int index) 28180ad17a8fSMark Bloch { 28190ad17a8fSMark Bloch struct mlx5_ib_dev *dev = to_mdev(ibdev); 28200ad17a8fSMark Bloch int outlen = MLX5_ST_SZ_BYTES(query_q_counter_out); 28210ad17a8fSMark Bloch void *out; 28220ad17a8fSMark Bloch __be32 val; 28230ad17a8fSMark Bloch int ret; 28240ad17a8fSMark Bloch int i; 28250ad17a8fSMark Bloch 28260ad17a8fSMark Bloch if (!port || !stats) 28270ad17a8fSMark Bloch return -ENOSYS; 28280ad17a8fSMark Bloch 28290ad17a8fSMark Bloch out = mlx5_vzalloc(outlen); 28300ad17a8fSMark Bloch if (!out) 28310ad17a8fSMark Bloch return -ENOMEM; 28320ad17a8fSMark Bloch 28330ad17a8fSMark Bloch ret = mlx5_core_query_q_counter(dev->mdev, 28340ad17a8fSMark Bloch dev->port[port - 1].q_cnt_id, 0, 28350ad17a8fSMark Bloch out, outlen); 28360ad17a8fSMark Bloch if (ret) 28370ad17a8fSMark Bloch goto free; 28380ad17a8fSMark Bloch 28390ad17a8fSMark Bloch for (i = 0; i < ARRAY_SIZE(names); i++) { 28400ad17a8fSMark Bloch val = *(__be32 *)(out + stats_offsets[i]); 28410ad17a8fSMark Bloch stats->value[i] = (u64)be32_to_cpu(val); 28420ad17a8fSMark Bloch } 28430ad17a8fSMark Bloch free: 28440ad17a8fSMark Bloch kvfree(out); 28450ad17a8fSMark Bloch return ARRAY_SIZE(names); 28460ad17a8fSMark Bloch } 28470ad17a8fSMark Bloch 28489603b61dSJack Morgenstein static void *mlx5_ib_add(struct mlx5_core_dev *mdev) 2849e126ba97SEli Cohen { 2850e126ba97SEli Cohen struct mlx5_ib_dev *dev; 2851ebd61f68SAchiad Shochat enum rdma_link_layer ll; 2852ebd61f68SAchiad Shochat int port_type_cap; 2853e126ba97SEli Cohen int err; 2854e126ba97SEli Cohen int i; 2855e126ba97SEli Cohen 2856ebd61f68SAchiad Shochat port_type_cap = MLX5_CAP_GEN(mdev, port_type); 2857ebd61f68SAchiad Shochat ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap); 2858ebd61f68SAchiad Shochat 2859e53505a8SAchiad Shochat if ((ll == IB_LINK_LAYER_ETHERNET) && !MLX5_CAP_GEN(mdev, roce)) 2860647241eaSMajd Dibbiny return NULL; 2861647241eaSMajd Dibbiny 2862e126ba97SEli Cohen printk_once(KERN_INFO "%s", mlx5_version); 2863e126ba97SEli Cohen 2864e126ba97SEli Cohen dev = (struct mlx5_ib_dev *)ib_alloc_device(sizeof(*dev)); 2865e126ba97SEli Cohen if (!dev) 28669603b61dSJack Morgenstein return NULL; 2867e126ba97SEli Cohen 28689603b61dSJack Morgenstein dev->mdev = mdev; 2869e126ba97SEli Cohen 28700837e86aSMark Bloch dev->port = kcalloc(MLX5_CAP_GEN(mdev, num_ports), sizeof(*dev->port), 28710837e86aSMark Bloch GFP_KERNEL); 28720837e86aSMark Bloch if (!dev->port) 28730837e86aSMark Bloch goto err_dealloc; 28740837e86aSMark Bloch 2875fc24fc5eSAchiad Shochat rwlock_init(&dev->roce.netdev_lock); 2876e126ba97SEli Cohen err = get_port_caps(dev); 2877e126ba97SEli Cohen if (err) 28780837e86aSMark Bloch goto err_free_port; 2879e126ba97SEli Cohen 28801b5daf11SMajd Dibbiny if (mlx5_use_mad_ifc(dev)) 2881e126ba97SEli Cohen get_ext_port_caps(dev); 2882e126ba97SEli Cohen 2883e126ba97SEli Cohen MLX5_INIT_DOORBELL_LOCK(&dev->uar_lock); 2884e126ba97SEli Cohen 2885e126ba97SEli Cohen strlcpy(dev->ib_dev.name, "mlx5_%d", IB_DEVICE_NAME_MAX); 2886e126ba97SEli Cohen dev->ib_dev.owner = THIS_MODULE; 2887e126ba97SEli Cohen dev->ib_dev.node_type = RDMA_NODE_IB_CA; 2888c6790aa9SSagi Grimberg dev->ib_dev.local_dma_lkey = 0 /* not supported for now */; 2889938fe83cSSaeed Mahameed dev->num_ports = MLX5_CAP_GEN(mdev, num_ports); 2890e126ba97SEli Cohen dev->ib_dev.phys_port_cnt = dev->num_ports; 2891233d05d2SSaeed Mahameed dev->ib_dev.num_comp_vectors = 2892233d05d2SSaeed Mahameed dev->mdev->priv.eq_table.num_comp_vectors; 2893e126ba97SEli Cohen dev->ib_dev.dma_device = &mdev->pdev->dev; 2894e126ba97SEli Cohen 2895e126ba97SEli Cohen dev->ib_dev.uverbs_abi_ver = MLX5_IB_UVERBS_ABI_VERSION; 2896e126ba97SEli Cohen dev->ib_dev.uverbs_cmd_mask = 2897e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) | 2898e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) | 2899e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_QUERY_PORT) | 2900e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_ALLOC_PD) | 2901e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) | 2902e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_REG_MR) | 290356e11d62SNoa Osherovich (1ull << IB_USER_VERBS_CMD_REREG_MR) | 2904e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_DEREG_MR) | 2905e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) | 2906e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_CREATE_CQ) | 2907e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_RESIZE_CQ) | 2908e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) | 2909e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_CREATE_QP) | 2910e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_MODIFY_QP) | 2911e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_QUERY_QP) | 2912e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_DESTROY_QP) | 2913e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST) | 2914e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_DETACH_MCAST) | 2915e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_CREATE_SRQ) | 2916e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ) | 2917e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_QUERY_SRQ) | 2918e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ) | 2919e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_CREATE_XSRQ) | 2920e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_OPEN_QP); 29211707cb4aSHaggai Eran dev->ib_dev.uverbs_ex_cmd_mask = 2922d4584ddfSMatan Barak (1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE) | 2923d4584ddfSMatan Barak (1ull << IB_USER_VERBS_EX_CMD_CREATE_CQ) | 2924d4584ddfSMatan Barak (1ull << IB_USER_VERBS_EX_CMD_CREATE_QP); 2925e126ba97SEli Cohen 2926e126ba97SEli Cohen dev->ib_dev.query_device = mlx5_ib_query_device; 2927e126ba97SEli Cohen dev->ib_dev.query_port = mlx5_ib_query_port; 2928ebd61f68SAchiad Shochat dev->ib_dev.get_link_layer = mlx5_ib_port_link_layer; 2929fc24fc5eSAchiad Shochat if (ll == IB_LINK_LAYER_ETHERNET) 2930fc24fc5eSAchiad Shochat dev->ib_dev.get_netdev = mlx5_ib_get_netdev; 2931e126ba97SEli Cohen dev->ib_dev.query_gid = mlx5_ib_query_gid; 29323cca2606SAchiad Shochat dev->ib_dev.add_gid = mlx5_ib_add_gid; 29333cca2606SAchiad Shochat dev->ib_dev.del_gid = mlx5_ib_del_gid; 2934e126ba97SEli Cohen dev->ib_dev.query_pkey = mlx5_ib_query_pkey; 2935e126ba97SEli Cohen dev->ib_dev.modify_device = mlx5_ib_modify_device; 2936e126ba97SEli Cohen dev->ib_dev.modify_port = mlx5_ib_modify_port; 2937e126ba97SEli Cohen dev->ib_dev.alloc_ucontext = mlx5_ib_alloc_ucontext; 2938e126ba97SEli Cohen dev->ib_dev.dealloc_ucontext = mlx5_ib_dealloc_ucontext; 2939e126ba97SEli Cohen dev->ib_dev.mmap = mlx5_ib_mmap; 2940e126ba97SEli Cohen dev->ib_dev.alloc_pd = mlx5_ib_alloc_pd; 2941e126ba97SEli Cohen dev->ib_dev.dealloc_pd = mlx5_ib_dealloc_pd; 2942e126ba97SEli Cohen dev->ib_dev.create_ah = mlx5_ib_create_ah; 2943e126ba97SEli Cohen dev->ib_dev.query_ah = mlx5_ib_query_ah; 2944e126ba97SEli Cohen dev->ib_dev.destroy_ah = mlx5_ib_destroy_ah; 2945e126ba97SEli Cohen dev->ib_dev.create_srq = mlx5_ib_create_srq; 2946e126ba97SEli Cohen dev->ib_dev.modify_srq = mlx5_ib_modify_srq; 2947e126ba97SEli Cohen dev->ib_dev.query_srq = mlx5_ib_query_srq; 2948e126ba97SEli Cohen dev->ib_dev.destroy_srq = mlx5_ib_destroy_srq; 2949e126ba97SEli Cohen dev->ib_dev.post_srq_recv = mlx5_ib_post_srq_recv; 2950e126ba97SEli Cohen dev->ib_dev.create_qp = mlx5_ib_create_qp; 2951e126ba97SEli Cohen dev->ib_dev.modify_qp = mlx5_ib_modify_qp; 2952e126ba97SEli Cohen dev->ib_dev.query_qp = mlx5_ib_query_qp; 2953e126ba97SEli Cohen dev->ib_dev.destroy_qp = mlx5_ib_destroy_qp; 2954e126ba97SEli Cohen dev->ib_dev.post_send = mlx5_ib_post_send; 2955e126ba97SEli Cohen dev->ib_dev.post_recv = mlx5_ib_post_recv; 2956e126ba97SEli Cohen dev->ib_dev.create_cq = mlx5_ib_create_cq; 2957e126ba97SEli Cohen dev->ib_dev.modify_cq = mlx5_ib_modify_cq; 2958e126ba97SEli Cohen dev->ib_dev.resize_cq = mlx5_ib_resize_cq; 2959e126ba97SEli Cohen dev->ib_dev.destroy_cq = mlx5_ib_destroy_cq; 2960e126ba97SEli Cohen dev->ib_dev.poll_cq = mlx5_ib_poll_cq; 2961e126ba97SEli Cohen dev->ib_dev.req_notify_cq = mlx5_ib_arm_cq; 2962e126ba97SEli Cohen dev->ib_dev.get_dma_mr = mlx5_ib_get_dma_mr; 2963e126ba97SEli Cohen dev->ib_dev.reg_user_mr = mlx5_ib_reg_user_mr; 296456e11d62SNoa Osherovich dev->ib_dev.rereg_user_mr = mlx5_ib_rereg_user_mr; 2965e126ba97SEli Cohen dev->ib_dev.dereg_mr = mlx5_ib_dereg_mr; 2966e126ba97SEli Cohen dev->ib_dev.attach_mcast = mlx5_ib_mcg_attach; 2967e126ba97SEli Cohen dev->ib_dev.detach_mcast = mlx5_ib_mcg_detach; 2968e126ba97SEli Cohen dev->ib_dev.process_mad = mlx5_ib_process_mad; 29699bee178bSSagi Grimberg dev->ib_dev.alloc_mr = mlx5_ib_alloc_mr; 29708a187ee5SSagi Grimberg dev->ib_dev.map_mr_sg = mlx5_ib_map_mr_sg; 2971d5436ba0SSagi Grimberg dev->ib_dev.check_mr_status = mlx5_ib_check_mr_status; 29727738613eSIra Weiny dev->ib_dev.get_port_immutable = mlx5_port_immutable; 2973c7342823SIra Weiny dev->ib_dev.get_dev_fw_str = get_dev_fw_str; 2974eff901d3SEli Cohen if (mlx5_core_is_pf(mdev)) { 2975eff901d3SEli Cohen dev->ib_dev.get_vf_config = mlx5_ib_get_vf_config; 2976eff901d3SEli Cohen dev->ib_dev.set_vf_link_state = mlx5_ib_set_vf_link_state; 2977eff901d3SEli Cohen dev->ib_dev.get_vf_stats = mlx5_ib_get_vf_stats; 2978eff901d3SEli Cohen dev->ib_dev.set_vf_guid = mlx5_ib_set_vf_guid; 2979eff901d3SEli Cohen } 2980e126ba97SEli Cohen 29817c2344c3SMaor Gottlieb dev->ib_dev.disassociate_ucontext = mlx5_ib_disassociate_ucontext; 29827c2344c3SMaor Gottlieb 2983938fe83cSSaeed Mahameed mlx5_ib_internal_fill_odp_caps(dev); 29848cdd312cSHaggai Eran 2985d2370e0aSMatan Barak if (MLX5_CAP_GEN(mdev, imaicl)) { 2986d2370e0aSMatan Barak dev->ib_dev.alloc_mw = mlx5_ib_alloc_mw; 2987d2370e0aSMatan Barak dev->ib_dev.dealloc_mw = mlx5_ib_dealloc_mw; 2988d2370e0aSMatan Barak dev->ib_dev.uverbs_cmd_mask |= 2989d2370e0aSMatan Barak (1ull << IB_USER_VERBS_CMD_ALLOC_MW) | 2990d2370e0aSMatan Barak (1ull << IB_USER_VERBS_CMD_DEALLOC_MW); 2991d2370e0aSMatan Barak } 2992d2370e0aSMatan Barak 29930ad17a8fSMark Bloch if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt) && 29940ad17a8fSMark Bloch MLX5_CAP_GEN(dev->mdev, retransmission_q_counters)) { 29950ad17a8fSMark Bloch dev->ib_dev.get_hw_stats = mlx5_ib_get_hw_stats; 29960ad17a8fSMark Bloch dev->ib_dev.alloc_hw_stats = mlx5_ib_alloc_hw_stats; 29970ad17a8fSMark Bloch } 29980ad17a8fSMark Bloch 2999938fe83cSSaeed Mahameed if (MLX5_CAP_GEN(mdev, xrc)) { 3000e126ba97SEli Cohen dev->ib_dev.alloc_xrcd = mlx5_ib_alloc_xrcd; 3001e126ba97SEli Cohen dev->ib_dev.dealloc_xrcd = mlx5_ib_dealloc_xrcd; 3002e126ba97SEli Cohen dev->ib_dev.uverbs_cmd_mask |= 3003e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_OPEN_XRCD) | 3004e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_CLOSE_XRCD); 3005e126ba97SEli Cohen } 3006e126ba97SEli Cohen 3007048ccca8SLinus Torvalds if (mlx5_ib_port_link_layer(&dev->ib_dev, 1) == 3008038d2ef8SMaor Gottlieb IB_LINK_LAYER_ETHERNET) { 3009038d2ef8SMaor Gottlieb dev->ib_dev.create_flow = mlx5_ib_create_flow; 3010038d2ef8SMaor Gottlieb dev->ib_dev.destroy_flow = mlx5_ib_destroy_flow; 301179b20a6cSYishai Hadas dev->ib_dev.create_wq = mlx5_ib_create_wq; 301279b20a6cSYishai Hadas dev->ib_dev.modify_wq = mlx5_ib_modify_wq; 301379b20a6cSYishai Hadas dev->ib_dev.destroy_wq = mlx5_ib_destroy_wq; 3014c5f90929SYishai Hadas dev->ib_dev.create_rwq_ind_table = mlx5_ib_create_rwq_ind_table; 3015c5f90929SYishai Hadas dev->ib_dev.destroy_rwq_ind_table = mlx5_ib_destroy_rwq_ind_table; 3016038d2ef8SMaor Gottlieb dev->ib_dev.uverbs_ex_cmd_mask |= 3017038d2ef8SMaor Gottlieb (1ull << IB_USER_VERBS_EX_CMD_CREATE_FLOW) | 301879b20a6cSYishai Hadas (1ull << IB_USER_VERBS_EX_CMD_DESTROY_FLOW) | 301979b20a6cSYishai Hadas (1ull << IB_USER_VERBS_EX_CMD_CREATE_WQ) | 302079b20a6cSYishai Hadas (1ull << IB_USER_VERBS_EX_CMD_MODIFY_WQ) | 3021c5f90929SYishai Hadas (1ull << IB_USER_VERBS_EX_CMD_DESTROY_WQ) | 3022c5f90929SYishai Hadas (1ull << IB_USER_VERBS_EX_CMD_CREATE_RWQ_IND_TBL) | 3023c5f90929SYishai Hadas (1ull << IB_USER_VERBS_EX_CMD_DESTROY_RWQ_IND_TBL); 3024038d2ef8SMaor Gottlieb } 3025e126ba97SEli Cohen err = init_node_data(dev); 3026e126ba97SEli Cohen if (err) 3027233d05d2SSaeed Mahameed goto err_dealloc; 3028e126ba97SEli Cohen 3029038d2ef8SMaor Gottlieb mutex_init(&dev->flow_db.lock); 3030e126ba97SEli Cohen mutex_init(&dev->cap_mask_mutex); 303189ea94a7SMaor Gottlieb INIT_LIST_HEAD(&dev->qp_list); 303289ea94a7SMaor Gottlieb spin_lock_init(&dev->reset_flow_resource_lock); 3033e126ba97SEli Cohen 3034fc24fc5eSAchiad Shochat if (ll == IB_LINK_LAYER_ETHERNET) { 3035fc24fc5eSAchiad Shochat err = mlx5_enable_roce(dev); 3036e126ba97SEli Cohen if (err) 3037233d05d2SSaeed Mahameed goto err_dealloc; 3038fc24fc5eSAchiad Shochat } 3039fc24fc5eSAchiad Shochat 3040fc24fc5eSAchiad Shochat err = create_dev_resources(&dev->devr); 3041fc24fc5eSAchiad Shochat if (err) 3042fc24fc5eSAchiad Shochat goto err_disable_roce; 3043e126ba97SEli Cohen 30446aec21f6SHaggai Eran err = mlx5_ib_odp_init_one(dev); 3045281d1a92SWei Yongjun if (err) 3046e126ba97SEli Cohen goto err_rsrc; 3047e126ba97SEli Cohen 30480837e86aSMark Bloch err = mlx5_ib_alloc_q_counters(dev); 30496aec21f6SHaggai Eran if (err) 30506aec21f6SHaggai Eran goto err_odp; 30516aec21f6SHaggai Eran 30520837e86aSMark Bloch err = ib_register_device(&dev->ib_dev, NULL); 30530837e86aSMark Bloch if (err) 30540837e86aSMark Bloch goto err_q_cnt; 30550837e86aSMark Bloch 3056e126ba97SEli Cohen err = create_umr_res(dev); 3057e126ba97SEli Cohen if (err) 3058e126ba97SEli Cohen goto err_dev; 3059e126ba97SEli Cohen 3060e126ba97SEli Cohen for (i = 0; i < ARRAY_SIZE(mlx5_class_attributes); i++) { 3061281d1a92SWei Yongjun err = device_create_file(&dev->ib_dev.dev, 3062281d1a92SWei Yongjun mlx5_class_attributes[i]); 3063281d1a92SWei Yongjun if (err) 3064e126ba97SEli Cohen goto err_umrc; 3065e126ba97SEli Cohen } 3066e126ba97SEli Cohen 3067e126ba97SEli Cohen dev->ib_active = true; 3068e126ba97SEli Cohen 30699603b61dSJack Morgenstein return dev; 3070e126ba97SEli Cohen 3071e126ba97SEli Cohen err_umrc: 3072e126ba97SEli Cohen destroy_umrc_res(dev); 3073e126ba97SEli Cohen 3074e126ba97SEli Cohen err_dev: 3075e126ba97SEli Cohen ib_unregister_device(&dev->ib_dev); 3076e126ba97SEli Cohen 30770837e86aSMark Bloch err_q_cnt: 30780837e86aSMark Bloch mlx5_ib_dealloc_q_counters(dev); 30790837e86aSMark Bloch 30806aec21f6SHaggai Eran err_odp: 30816aec21f6SHaggai Eran mlx5_ib_odp_remove_one(dev); 30826aec21f6SHaggai Eran 3083e126ba97SEli Cohen err_rsrc: 3084e126ba97SEli Cohen destroy_dev_resources(&dev->devr); 3085e126ba97SEli Cohen 3086fc24fc5eSAchiad Shochat err_disable_roce: 3087*5ec8c83eSAviv Heller if (ll == IB_LINK_LAYER_ETHERNET) { 3088fc24fc5eSAchiad Shochat mlx5_disable_roce(dev); 3089*5ec8c83eSAviv Heller mlx5_remove_roce_notifier(dev); 3090*5ec8c83eSAviv Heller } 3091fc24fc5eSAchiad Shochat 30920837e86aSMark Bloch err_free_port: 30930837e86aSMark Bloch kfree(dev->port); 30940837e86aSMark Bloch 30959603b61dSJack Morgenstein err_dealloc: 3096e126ba97SEli Cohen ib_dealloc_device((struct ib_device *)dev); 3097e126ba97SEli Cohen 30989603b61dSJack Morgenstein return NULL; 3099e126ba97SEli Cohen } 3100e126ba97SEli Cohen 31019603b61dSJack Morgenstein static void mlx5_ib_remove(struct mlx5_core_dev *mdev, void *context) 3102e126ba97SEli Cohen { 31039603b61dSJack Morgenstein struct mlx5_ib_dev *dev = context; 3104fc24fc5eSAchiad Shochat enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev, 1); 31056aec21f6SHaggai Eran 3106*5ec8c83eSAviv Heller mlx5_remove_roce_notifier(dev); 3107e126ba97SEli Cohen ib_unregister_device(&dev->ib_dev); 31080837e86aSMark Bloch mlx5_ib_dealloc_q_counters(dev); 3109eefd56e5SEli Cohen destroy_umrc_res(dev); 31106aec21f6SHaggai Eran mlx5_ib_odp_remove_one(dev); 3111e126ba97SEli Cohen destroy_dev_resources(&dev->devr); 3112fc24fc5eSAchiad Shochat if (ll == IB_LINK_LAYER_ETHERNET) 3113fc24fc5eSAchiad Shochat mlx5_disable_roce(dev); 31140837e86aSMark Bloch kfree(dev->port); 3115e126ba97SEli Cohen ib_dealloc_device(&dev->ib_dev); 3116e126ba97SEli Cohen } 3117e126ba97SEli Cohen 31189603b61dSJack Morgenstein static struct mlx5_interface mlx5_ib_interface = { 31199603b61dSJack Morgenstein .add = mlx5_ib_add, 31209603b61dSJack Morgenstein .remove = mlx5_ib_remove, 31219603b61dSJack Morgenstein .event = mlx5_ib_event, 312264613d94SSaeed Mahameed .protocol = MLX5_INTERFACE_PROTOCOL_IB, 3123e126ba97SEli Cohen }; 3124e126ba97SEli Cohen 3125e126ba97SEli Cohen static int __init mlx5_ib_init(void) 3126e126ba97SEli Cohen { 31276aec21f6SHaggai Eran int err; 31286aec21f6SHaggai Eran 31299603b61dSJack Morgenstein if (deprecated_prof_sel != 2) 31309603b61dSJack Morgenstein pr_warn("prof_sel is deprecated for mlx5_ib, set it for mlx5_core\n"); 31319603b61dSJack Morgenstein 31326aec21f6SHaggai Eran err = mlx5_ib_odp_init(); 31336aec21f6SHaggai Eran if (err) 31346aec21f6SHaggai Eran return err; 31356aec21f6SHaggai Eran 31366aec21f6SHaggai Eran err = mlx5_register_interface(&mlx5_ib_interface); 31376aec21f6SHaggai Eran if (err) 31386aec21f6SHaggai Eran goto clean_odp; 31396aec21f6SHaggai Eran 31406aec21f6SHaggai Eran return err; 31416aec21f6SHaggai Eran 31426aec21f6SHaggai Eran clean_odp: 31436aec21f6SHaggai Eran mlx5_ib_odp_cleanup(); 31446aec21f6SHaggai Eran return err; 3145e126ba97SEli Cohen } 3146e126ba97SEli Cohen 3147e126ba97SEli Cohen static void __exit mlx5_ib_cleanup(void) 3148e126ba97SEli Cohen { 31499603b61dSJack Morgenstein mlx5_unregister_interface(&mlx5_ib_interface); 31506aec21f6SHaggai Eran mlx5_ib_odp_cleanup(); 3151e126ba97SEli Cohen } 3152e126ba97SEli Cohen 3153e126ba97SEli Cohen module_init(mlx5_ib_init); 3154e126ba97SEli Cohen module_exit(mlx5_ib_cleanup); 3155