1e126ba97SEli Cohen /* 26cf0a15fSSaeed Mahameed * Copyright (c) 2013-2015, Mellanox Technologies. All rights reserved. 3e126ba97SEli Cohen * 4e126ba97SEli Cohen * This software is available to you under a choice of one of two 5e126ba97SEli Cohen * licenses. You may choose to be licensed under the terms of the GNU 6e126ba97SEli Cohen * General Public License (GPL) Version 2, available from the file 7e126ba97SEli Cohen * COPYING in the main directory of this source tree, or the 8e126ba97SEli Cohen * OpenIB.org BSD license below: 9e126ba97SEli Cohen * 10e126ba97SEli Cohen * Redistribution and use in source and binary forms, with or 11e126ba97SEli Cohen * without modification, are permitted provided that the following 12e126ba97SEli Cohen * conditions are met: 13e126ba97SEli Cohen * 14e126ba97SEli Cohen * - Redistributions of source code must retain the above 15e126ba97SEli Cohen * copyright notice, this list of conditions and the following 16e126ba97SEli Cohen * disclaimer. 17e126ba97SEli Cohen * 18e126ba97SEli Cohen * - Redistributions in binary form must reproduce the above 19e126ba97SEli Cohen * copyright notice, this list of conditions and the following 20e126ba97SEli Cohen * disclaimer in the documentation and/or other materials 21e126ba97SEli Cohen * provided with the distribution. 22e126ba97SEli Cohen * 23e126ba97SEli Cohen * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 24e126ba97SEli Cohen * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 25e126ba97SEli Cohen * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 26e126ba97SEli Cohen * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 27e126ba97SEli Cohen * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 28e126ba97SEli Cohen * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 29e126ba97SEli Cohen * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 30e126ba97SEli Cohen * SOFTWARE. 31e126ba97SEli Cohen */ 32e126ba97SEli Cohen 33adec640eSChristoph Hellwig #include <linux/highmem.h> 34e126ba97SEli Cohen #include <linux/module.h> 35e126ba97SEli Cohen #include <linux/init.h> 36e126ba97SEli Cohen #include <linux/errno.h> 37e126ba97SEli Cohen #include <linux/pci.h> 38e126ba97SEli Cohen #include <linux/dma-mapping.h> 39e126ba97SEli Cohen #include <linux/slab.h> 4037aa5c36SGuy Levi #if defined(CONFIG_X86) 4137aa5c36SGuy Levi #include <asm/pat.h> 4237aa5c36SGuy Levi #endif 43e126ba97SEli Cohen #include <linux/sched.h> 446e84f315SIngo Molnar #include <linux/sched/mm.h> 450881e7bdSIngo Molnar #include <linux/sched/task.h> 467c2344c3SMaor Gottlieb #include <linux/delay.h> 47e126ba97SEli Cohen #include <rdma/ib_user_verbs.h> 483f89a643SAchiad Shochat #include <rdma/ib_addr.h> 492811ba51SAchiad Shochat #include <rdma/ib_cache.h> 50ada68c31SAchiad Shochat #include <linux/mlx5/port.h> 511b5daf11SMajd Dibbiny #include <linux/mlx5/vport.h> 527c2344c3SMaor Gottlieb #include <linux/list.h> 53e126ba97SEli Cohen #include <rdma/ib_smi.h> 54e126ba97SEli Cohen #include <rdma/ib_umem.h> 55038d2ef8SMaor Gottlieb #include <linux/in.h> 56038d2ef8SMaor Gottlieb #include <linux/etherdevice.h> 57038d2ef8SMaor Gottlieb #include <linux/mlx5/fs.h> 5878984898SOr Gerlitz #include <linux/mlx5/vport.h> 59e126ba97SEli Cohen #include "mlx5_ib.h" 60e1f24a79SParav Pandit #include "cmd.h" 61e126ba97SEli Cohen 62e126ba97SEli Cohen #define DRIVER_NAME "mlx5_ib" 63169a1d85SAmir Vadai #define DRIVER_VERSION "2.2-1" 64169a1d85SAmir Vadai #define DRIVER_RELDATE "Feb 2014" 65e126ba97SEli Cohen 66e126ba97SEli Cohen MODULE_AUTHOR("Eli Cohen <eli@mellanox.com>"); 67e126ba97SEli Cohen MODULE_DESCRIPTION("Mellanox Connect-IB HCA IB driver"); 68e126ba97SEli Cohen MODULE_LICENSE("Dual BSD/GPL"); 69e126ba97SEli Cohen MODULE_VERSION(DRIVER_VERSION); 70e126ba97SEli Cohen 71e126ba97SEli Cohen static char mlx5_version[] = 72e126ba97SEli Cohen DRIVER_NAME ": Mellanox Connect-IB Infiniband driver v" 73e126ba97SEli Cohen DRIVER_VERSION " (" DRIVER_RELDATE ")\n"; 74e126ba97SEli Cohen 75da7525d2SEran Ben Elisha enum { 76da7525d2SEran Ben Elisha MLX5_ATOMIC_SIZE_QP_8BYTES = 1 << 3, 77da7525d2SEran Ben Elisha }; 781b5daf11SMajd Dibbiny 791b5daf11SMajd Dibbiny static enum rdma_link_layer 80ebd61f68SAchiad Shochat mlx5_port_type_cap_to_rdma_ll(int port_type_cap) 811b5daf11SMajd Dibbiny { 82ebd61f68SAchiad Shochat switch (port_type_cap) { 831b5daf11SMajd Dibbiny case MLX5_CAP_PORT_TYPE_IB: 841b5daf11SMajd Dibbiny return IB_LINK_LAYER_INFINIBAND; 851b5daf11SMajd Dibbiny case MLX5_CAP_PORT_TYPE_ETH: 861b5daf11SMajd Dibbiny return IB_LINK_LAYER_ETHERNET; 871b5daf11SMajd Dibbiny default: 881b5daf11SMajd Dibbiny return IB_LINK_LAYER_UNSPECIFIED; 891b5daf11SMajd Dibbiny } 901b5daf11SMajd Dibbiny } 911b5daf11SMajd Dibbiny 92ebd61f68SAchiad Shochat static enum rdma_link_layer 93ebd61f68SAchiad Shochat mlx5_ib_port_link_layer(struct ib_device *device, u8 port_num) 94ebd61f68SAchiad Shochat { 95ebd61f68SAchiad Shochat struct mlx5_ib_dev *dev = to_mdev(device); 96ebd61f68SAchiad Shochat int port_type_cap = MLX5_CAP_GEN(dev->mdev, port_type); 97ebd61f68SAchiad Shochat 98ebd61f68SAchiad Shochat return mlx5_port_type_cap_to_rdma_ll(port_type_cap); 99ebd61f68SAchiad Shochat } 100ebd61f68SAchiad Shochat 101fc24fc5eSAchiad Shochat static int mlx5_netdev_event(struct notifier_block *this, 102fc24fc5eSAchiad Shochat unsigned long event, void *ptr) 103fc24fc5eSAchiad Shochat { 104fc24fc5eSAchiad Shochat struct net_device *ndev = netdev_notifier_info_to_dev(ptr); 105fc24fc5eSAchiad Shochat struct mlx5_ib_dev *ibdev = container_of(this, struct mlx5_ib_dev, 106fc24fc5eSAchiad Shochat roce.nb); 107fc24fc5eSAchiad Shochat 1085ec8c83eSAviv Heller switch (event) { 1095ec8c83eSAviv Heller case NETDEV_REGISTER: 1105ec8c83eSAviv Heller case NETDEV_UNREGISTER: 111fc24fc5eSAchiad Shochat write_lock(&ibdev->roce.netdev_lock); 112fc24fc5eSAchiad Shochat if (ndev->dev.parent == &ibdev->mdev->pdev->dev) 1135ec8c83eSAviv Heller ibdev->roce.netdev = (event == NETDEV_UNREGISTER) ? 1145ec8c83eSAviv Heller NULL : ndev; 115fc24fc5eSAchiad Shochat write_unlock(&ibdev->roce.netdev_lock); 1165ec8c83eSAviv Heller break; 1175ec8c83eSAviv Heller 1185ec8c83eSAviv Heller case NETDEV_UP: 11988621dfeSAviv Heller case NETDEV_DOWN: { 12088621dfeSAviv Heller struct net_device *lag_ndev = mlx5_lag_get_roce_netdev(ibdev->mdev); 12188621dfeSAviv Heller struct net_device *upper = NULL; 12288621dfeSAviv Heller 12388621dfeSAviv Heller if (lag_ndev) { 12488621dfeSAviv Heller upper = netdev_master_upper_dev_get(lag_ndev); 12588621dfeSAviv Heller dev_put(lag_ndev); 12688621dfeSAviv Heller } 12788621dfeSAviv Heller 12888621dfeSAviv Heller if ((upper == ndev || (!upper && ndev == ibdev->roce.netdev)) 12988621dfeSAviv Heller && ibdev->ib_active) { 130626bc02dSBart Van Assche struct ib_event ibev = { }; 1315ec8c83eSAviv Heller 1325ec8c83eSAviv Heller ibev.device = &ibdev->ib_dev; 1335ec8c83eSAviv Heller ibev.event = (event == NETDEV_UP) ? 1345ec8c83eSAviv Heller IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR; 1355ec8c83eSAviv Heller ibev.element.port_num = 1; 1365ec8c83eSAviv Heller ib_dispatch_event(&ibev); 1375ec8c83eSAviv Heller } 1385ec8c83eSAviv Heller break; 13988621dfeSAviv Heller } 1405ec8c83eSAviv Heller 1415ec8c83eSAviv Heller default: 1425ec8c83eSAviv Heller break; 1435ec8c83eSAviv Heller } 144fc24fc5eSAchiad Shochat 145fc24fc5eSAchiad Shochat return NOTIFY_DONE; 146fc24fc5eSAchiad Shochat } 147fc24fc5eSAchiad Shochat 148fc24fc5eSAchiad Shochat static struct net_device *mlx5_ib_get_netdev(struct ib_device *device, 149fc24fc5eSAchiad Shochat u8 port_num) 150fc24fc5eSAchiad Shochat { 151fc24fc5eSAchiad Shochat struct mlx5_ib_dev *ibdev = to_mdev(device); 152fc24fc5eSAchiad Shochat struct net_device *ndev; 153fc24fc5eSAchiad Shochat 15488621dfeSAviv Heller ndev = mlx5_lag_get_roce_netdev(ibdev->mdev); 15588621dfeSAviv Heller if (ndev) 15688621dfeSAviv Heller return ndev; 15788621dfeSAviv Heller 158fc24fc5eSAchiad Shochat /* Ensure ndev does not disappear before we invoke dev_hold() 159fc24fc5eSAchiad Shochat */ 160fc24fc5eSAchiad Shochat read_lock(&ibdev->roce.netdev_lock); 161fc24fc5eSAchiad Shochat ndev = ibdev->roce.netdev; 162fc24fc5eSAchiad Shochat if (ndev) 163fc24fc5eSAchiad Shochat dev_hold(ndev); 164fc24fc5eSAchiad Shochat read_unlock(&ibdev->roce.netdev_lock); 165fc24fc5eSAchiad Shochat 166fc24fc5eSAchiad Shochat return ndev; 167fc24fc5eSAchiad Shochat } 168fc24fc5eSAchiad Shochat 169f1b65df5SNoa Osherovich static int translate_eth_proto_oper(u32 eth_proto_oper, u8 *active_speed, 170f1b65df5SNoa Osherovich u8 *active_width) 171f1b65df5SNoa Osherovich { 172f1b65df5SNoa Osherovich switch (eth_proto_oper) { 173f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_1000BASE_CX_SGMII): 174f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_1000BASE_KX): 175f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_100BASE_TX): 176f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_1000BASE_T): 177f1b65df5SNoa Osherovich *active_width = IB_WIDTH_1X; 178f1b65df5SNoa Osherovich *active_speed = IB_SPEED_SDR; 179f1b65df5SNoa Osherovich break; 180f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_10GBASE_T): 181f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_10GBASE_CX4): 182f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_10GBASE_KX4): 183f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_10GBASE_KR): 184f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_10GBASE_CR): 185f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_10GBASE_SR): 186f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_10GBASE_ER): 187f1b65df5SNoa Osherovich *active_width = IB_WIDTH_1X; 188f1b65df5SNoa Osherovich *active_speed = IB_SPEED_QDR; 189f1b65df5SNoa Osherovich break; 190f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_25GBASE_CR): 191f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_25GBASE_KR): 192f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_25GBASE_SR): 193f1b65df5SNoa Osherovich *active_width = IB_WIDTH_1X; 194f1b65df5SNoa Osherovich *active_speed = IB_SPEED_EDR; 195f1b65df5SNoa Osherovich break; 196f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_40GBASE_CR4): 197f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_40GBASE_KR4): 198f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_40GBASE_SR4): 199f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_40GBASE_LR4): 200f1b65df5SNoa Osherovich *active_width = IB_WIDTH_4X; 201f1b65df5SNoa Osherovich *active_speed = IB_SPEED_QDR; 202f1b65df5SNoa Osherovich break; 203f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_50GBASE_CR2): 204f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_50GBASE_KR2): 205f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_50GBASE_SR2): 206f1b65df5SNoa Osherovich *active_width = IB_WIDTH_1X; 207f1b65df5SNoa Osherovich *active_speed = IB_SPEED_HDR; 208f1b65df5SNoa Osherovich break; 209f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_56GBASE_R4): 210f1b65df5SNoa Osherovich *active_width = IB_WIDTH_4X; 211f1b65df5SNoa Osherovich *active_speed = IB_SPEED_FDR; 212f1b65df5SNoa Osherovich break; 213f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_100GBASE_CR4): 214f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_100GBASE_SR4): 215f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_100GBASE_KR4): 216f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_100GBASE_LR4): 217f1b65df5SNoa Osherovich *active_width = IB_WIDTH_4X; 218f1b65df5SNoa Osherovich *active_speed = IB_SPEED_EDR; 219f1b65df5SNoa Osherovich break; 220f1b65df5SNoa Osherovich default: 221f1b65df5SNoa Osherovich return -EINVAL; 222f1b65df5SNoa Osherovich } 223f1b65df5SNoa Osherovich 224f1b65df5SNoa Osherovich return 0; 225f1b65df5SNoa Osherovich } 226f1b65df5SNoa Osherovich 22750f22fd8SNoa Osherovich static void mlx5_query_port_roce(struct ib_device *device, u8 port_num, 2283f89a643SAchiad Shochat struct ib_port_attr *props) 2293f89a643SAchiad Shochat { 2303f89a643SAchiad Shochat struct mlx5_ib_dev *dev = to_mdev(device); 231f1b65df5SNoa Osherovich struct mlx5_core_dev *mdev = dev->mdev; 23288621dfeSAviv Heller struct net_device *ndev, *upper; 2333f89a643SAchiad Shochat enum ib_mtu ndev_ib_mtu; 234c876a1b7SLeon Romanovsky u16 qkey_viol_cntr; 235f1b65df5SNoa Osherovich u32 eth_prot_oper; 2363f89a643SAchiad Shochat 237f1b65df5SNoa Osherovich /* Possible bad flows are checked before filling out props so in case 238f1b65df5SNoa Osherovich * of an error it will still be zeroed out. 23950f22fd8SNoa Osherovich */ 240f1b65df5SNoa Osherovich if (mlx5_query_port_eth_proto_oper(mdev, ð_prot_oper, port_num)) 241f1b65df5SNoa Osherovich return; 242f1b65df5SNoa Osherovich 243f1b65df5SNoa Osherovich translate_eth_proto_oper(eth_prot_oper, &props->active_speed, 244f1b65df5SNoa Osherovich &props->active_width); 2453f89a643SAchiad Shochat 2463f89a643SAchiad Shochat props->port_cap_flags |= IB_PORT_CM_SUP; 2473f89a643SAchiad Shochat props->port_cap_flags |= IB_PORT_IP_BASED_GIDS; 2483f89a643SAchiad Shochat 2493f89a643SAchiad Shochat props->gid_tbl_len = MLX5_CAP_ROCE(dev->mdev, 2503f89a643SAchiad Shochat roce_address_table_size); 2513f89a643SAchiad Shochat props->max_mtu = IB_MTU_4096; 2523f89a643SAchiad Shochat props->max_msg_sz = 1 << MLX5_CAP_GEN(dev->mdev, log_max_msg); 2533f89a643SAchiad Shochat props->pkey_tbl_len = 1; 2543f89a643SAchiad Shochat props->state = IB_PORT_DOWN; 2553f89a643SAchiad Shochat props->phys_state = 3; 2563f89a643SAchiad Shochat 257c876a1b7SLeon Romanovsky mlx5_query_nic_vport_qkey_viol_cntr(dev->mdev, &qkey_viol_cntr); 258c876a1b7SLeon Romanovsky props->qkey_viol_cntr = qkey_viol_cntr; 2593f89a643SAchiad Shochat 2603f89a643SAchiad Shochat ndev = mlx5_ib_get_netdev(device, port_num); 2613f89a643SAchiad Shochat if (!ndev) 26250f22fd8SNoa Osherovich return; 2633f89a643SAchiad Shochat 26488621dfeSAviv Heller if (mlx5_lag_is_active(dev->mdev)) { 26588621dfeSAviv Heller rcu_read_lock(); 26688621dfeSAviv Heller upper = netdev_master_upper_dev_get_rcu(ndev); 26788621dfeSAviv Heller if (upper) { 26888621dfeSAviv Heller dev_put(ndev); 26988621dfeSAviv Heller ndev = upper; 27088621dfeSAviv Heller dev_hold(ndev); 27188621dfeSAviv Heller } 27288621dfeSAviv Heller rcu_read_unlock(); 27388621dfeSAviv Heller } 27488621dfeSAviv Heller 2753f89a643SAchiad Shochat if (netif_running(ndev) && netif_carrier_ok(ndev)) { 2763f89a643SAchiad Shochat props->state = IB_PORT_ACTIVE; 2773f89a643SAchiad Shochat props->phys_state = 5; 2783f89a643SAchiad Shochat } 2793f89a643SAchiad Shochat 2803f89a643SAchiad Shochat ndev_ib_mtu = iboe_get_mtu(ndev->mtu); 2813f89a643SAchiad Shochat 2823f89a643SAchiad Shochat dev_put(ndev); 2833f89a643SAchiad Shochat 2843f89a643SAchiad Shochat props->active_mtu = min(props->max_mtu, ndev_ib_mtu); 2853f89a643SAchiad Shochat } 2863f89a643SAchiad Shochat 2873cca2606SAchiad Shochat static void ib_gid_to_mlx5_roce_addr(const union ib_gid *gid, 2883cca2606SAchiad Shochat const struct ib_gid_attr *attr, 2893cca2606SAchiad Shochat void *mlx5_addr) 2903cca2606SAchiad Shochat { 2913cca2606SAchiad Shochat #define MLX5_SET_RA(p, f, v) MLX5_SET(roce_addr_layout, p, f, v) 2923cca2606SAchiad Shochat char *mlx5_addr_l3_addr = MLX5_ADDR_OF(roce_addr_layout, mlx5_addr, 2933cca2606SAchiad Shochat source_l3_address); 2943cca2606SAchiad Shochat void *mlx5_addr_mac = MLX5_ADDR_OF(roce_addr_layout, mlx5_addr, 2953cca2606SAchiad Shochat source_mac_47_32); 2963cca2606SAchiad Shochat 2973cca2606SAchiad Shochat if (!gid) 2983cca2606SAchiad Shochat return; 2993cca2606SAchiad Shochat 3003cca2606SAchiad Shochat ether_addr_copy(mlx5_addr_mac, attr->ndev->dev_addr); 3013cca2606SAchiad Shochat 3023cca2606SAchiad Shochat if (is_vlan_dev(attr->ndev)) { 3033cca2606SAchiad Shochat MLX5_SET_RA(mlx5_addr, vlan_valid, 1); 3043cca2606SAchiad Shochat MLX5_SET_RA(mlx5_addr, vlan_id, vlan_dev_vlan_id(attr->ndev)); 3053cca2606SAchiad Shochat } 3063cca2606SAchiad Shochat 3073cca2606SAchiad Shochat switch (attr->gid_type) { 3083cca2606SAchiad Shochat case IB_GID_TYPE_IB: 3093cca2606SAchiad Shochat MLX5_SET_RA(mlx5_addr, roce_version, MLX5_ROCE_VERSION_1); 3103cca2606SAchiad Shochat break; 3113cca2606SAchiad Shochat case IB_GID_TYPE_ROCE_UDP_ENCAP: 3123cca2606SAchiad Shochat MLX5_SET_RA(mlx5_addr, roce_version, MLX5_ROCE_VERSION_2); 3133cca2606SAchiad Shochat break; 3143cca2606SAchiad Shochat 3153cca2606SAchiad Shochat default: 3163cca2606SAchiad Shochat WARN_ON(true); 3173cca2606SAchiad Shochat } 3183cca2606SAchiad Shochat 3193cca2606SAchiad Shochat if (attr->gid_type != IB_GID_TYPE_IB) { 3203cca2606SAchiad Shochat if (ipv6_addr_v4mapped((void *)gid)) 3213cca2606SAchiad Shochat MLX5_SET_RA(mlx5_addr, roce_l3_type, 3223cca2606SAchiad Shochat MLX5_ROCE_L3_TYPE_IPV4); 3233cca2606SAchiad Shochat else 3243cca2606SAchiad Shochat MLX5_SET_RA(mlx5_addr, roce_l3_type, 3253cca2606SAchiad Shochat MLX5_ROCE_L3_TYPE_IPV6); 3263cca2606SAchiad Shochat } 3273cca2606SAchiad Shochat 3283cca2606SAchiad Shochat if ((attr->gid_type == IB_GID_TYPE_IB) || 3293cca2606SAchiad Shochat !ipv6_addr_v4mapped((void *)gid)) 3303cca2606SAchiad Shochat memcpy(mlx5_addr_l3_addr, gid, sizeof(*gid)); 3313cca2606SAchiad Shochat else 3323cca2606SAchiad Shochat memcpy(&mlx5_addr_l3_addr[12], &gid->raw[12], 4); 3333cca2606SAchiad Shochat } 3343cca2606SAchiad Shochat 3353cca2606SAchiad Shochat static int set_roce_addr(struct ib_device *device, u8 port_num, 3363cca2606SAchiad Shochat unsigned int index, 3373cca2606SAchiad Shochat const union ib_gid *gid, 3383cca2606SAchiad Shochat const struct ib_gid_attr *attr) 3393cca2606SAchiad Shochat { 3403cca2606SAchiad Shochat struct mlx5_ib_dev *dev = to_mdev(device); 341c4f287c4SSaeed Mahameed u32 in[MLX5_ST_SZ_DW(set_roce_address_in)] = {0}; 342c4f287c4SSaeed Mahameed u32 out[MLX5_ST_SZ_DW(set_roce_address_out)] = {0}; 3433cca2606SAchiad Shochat void *in_addr = MLX5_ADDR_OF(set_roce_address_in, in, roce_address); 3443cca2606SAchiad Shochat enum rdma_link_layer ll = mlx5_ib_port_link_layer(device, port_num); 3453cca2606SAchiad Shochat 3463cca2606SAchiad Shochat if (ll != IB_LINK_LAYER_ETHERNET) 3473cca2606SAchiad Shochat return -EINVAL; 3483cca2606SAchiad Shochat 3493cca2606SAchiad Shochat ib_gid_to_mlx5_roce_addr(gid, attr, in_addr); 3503cca2606SAchiad Shochat 3513cca2606SAchiad Shochat MLX5_SET(set_roce_address_in, in, roce_address_index, index); 3523cca2606SAchiad Shochat MLX5_SET(set_roce_address_in, in, opcode, MLX5_CMD_OP_SET_ROCE_ADDRESS); 3533cca2606SAchiad Shochat return mlx5_cmd_exec(dev->mdev, in, sizeof(in), out, sizeof(out)); 3543cca2606SAchiad Shochat } 3553cca2606SAchiad Shochat 3563cca2606SAchiad Shochat static int mlx5_ib_add_gid(struct ib_device *device, u8 port_num, 3573cca2606SAchiad Shochat unsigned int index, const union ib_gid *gid, 3583cca2606SAchiad Shochat const struct ib_gid_attr *attr, 3593cca2606SAchiad Shochat __always_unused void **context) 3603cca2606SAchiad Shochat { 3613cca2606SAchiad Shochat return set_roce_addr(device, port_num, index, gid, attr); 3623cca2606SAchiad Shochat } 3633cca2606SAchiad Shochat 3643cca2606SAchiad Shochat static int mlx5_ib_del_gid(struct ib_device *device, u8 port_num, 3653cca2606SAchiad Shochat unsigned int index, __always_unused void **context) 3663cca2606SAchiad Shochat { 3673cca2606SAchiad Shochat return set_roce_addr(device, port_num, index, NULL, NULL); 3683cca2606SAchiad Shochat } 3693cca2606SAchiad Shochat 3702811ba51SAchiad Shochat __be16 mlx5_get_roce_udp_sport(struct mlx5_ib_dev *dev, u8 port_num, 3712811ba51SAchiad Shochat int index) 3722811ba51SAchiad Shochat { 3732811ba51SAchiad Shochat struct ib_gid_attr attr; 3742811ba51SAchiad Shochat union ib_gid gid; 3752811ba51SAchiad Shochat 3762811ba51SAchiad Shochat if (ib_get_cached_gid(&dev->ib_dev, port_num, index, &gid, &attr)) 3772811ba51SAchiad Shochat return 0; 3782811ba51SAchiad Shochat 3792811ba51SAchiad Shochat if (!attr.ndev) 3802811ba51SAchiad Shochat return 0; 3812811ba51SAchiad Shochat 3822811ba51SAchiad Shochat dev_put(attr.ndev); 3832811ba51SAchiad Shochat 3842811ba51SAchiad Shochat if (attr.gid_type != IB_GID_TYPE_ROCE_UDP_ENCAP) 3852811ba51SAchiad Shochat return 0; 3862811ba51SAchiad Shochat 3872811ba51SAchiad Shochat return cpu_to_be16(MLX5_CAP_ROCE(dev->mdev, r_roce_min_src_udp_port)); 3882811ba51SAchiad Shochat } 3892811ba51SAchiad Shochat 390ed88451eSMajd Dibbiny int mlx5_get_roce_gid_type(struct mlx5_ib_dev *dev, u8 port_num, 391ed88451eSMajd Dibbiny int index, enum ib_gid_type *gid_type) 392ed88451eSMajd Dibbiny { 393ed88451eSMajd Dibbiny struct ib_gid_attr attr; 394ed88451eSMajd Dibbiny union ib_gid gid; 395ed88451eSMajd Dibbiny int ret; 396ed88451eSMajd Dibbiny 397ed88451eSMajd Dibbiny ret = ib_get_cached_gid(&dev->ib_dev, port_num, index, &gid, &attr); 398ed88451eSMajd Dibbiny if (ret) 399ed88451eSMajd Dibbiny return ret; 400ed88451eSMajd Dibbiny 401ed88451eSMajd Dibbiny if (!attr.ndev) 402ed88451eSMajd Dibbiny return -ENODEV; 403ed88451eSMajd Dibbiny 404ed88451eSMajd Dibbiny dev_put(attr.ndev); 405ed88451eSMajd Dibbiny 406ed88451eSMajd Dibbiny *gid_type = attr.gid_type; 407ed88451eSMajd Dibbiny 408ed88451eSMajd Dibbiny return 0; 409ed88451eSMajd Dibbiny } 410ed88451eSMajd Dibbiny 4111b5daf11SMajd Dibbiny static int mlx5_use_mad_ifc(struct mlx5_ib_dev *dev) 4121b5daf11SMajd Dibbiny { 4137fae6655SNoa Osherovich if (MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_IB) 414d603c809SEli Cohen return !MLX5_CAP_GEN(dev->mdev, ib_virt); 4157fae6655SNoa Osherovich return 0; 4161b5daf11SMajd Dibbiny } 4171b5daf11SMajd Dibbiny 4181b5daf11SMajd Dibbiny enum { 4191b5daf11SMajd Dibbiny MLX5_VPORT_ACCESS_METHOD_MAD, 4201b5daf11SMajd Dibbiny MLX5_VPORT_ACCESS_METHOD_HCA, 4211b5daf11SMajd Dibbiny MLX5_VPORT_ACCESS_METHOD_NIC, 4221b5daf11SMajd Dibbiny }; 4231b5daf11SMajd Dibbiny 4241b5daf11SMajd Dibbiny static int mlx5_get_vport_access_method(struct ib_device *ibdev) 4251b5daf11SMajd Dibbiny { 4261b5daf11SMajd Dibbiny if (mlx5_use_mad_ifc(to_mdev(ibdev))) 4271b5daf11SMajd Dibbiny return MLX5_VPORT_ACCESS_METHOD_MAD; 4281b5daf11SMajd Dibbiny 429ebd61f68SAchiad Shochat if (mlx5_ib_port_link_layer(ibdev, 1) == 4301b5daf11SMajd Dibbiny IB_LINK_LAYER_ETHERNET) 4311b5daf11SMajd Dibbiny return MLX5_VPORT_ACCESS_METHOD_NIC; 4321b5daf11SMajd Dibbiny 4331b5daf11SMajd Dibbiny return MLX5_VPORT_ACCESS_METHOD_HCA; 4341b5daf11SMajd Dibbiny } 4351b5daf11SMajd Dibbiny 436da7525d2SEran Ben Elisha static void get_atomic_caps(struct mlx5_ib_dev *dev, 437da7525d2SEran Ben Elisha struct ib_device_attr *props) 438da7525d2SEran Ben Elisha { 439da7525d2SEran Ben Elisha u8 tmp; 440da7525d2SEran Ben Elisha u8 atomic_operations = MLX5_CAP_ATOMIC(dev->mdev, atomic_operations); 441da7525d2SEran Ben Elisha u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_qp); 442da7525d2SEran Ben Elisha u8 atomic_req_8B_endianness_mode = 443da7525d2SEran Ben Elisha MLX5_CAP_ATOMIC(dev->mdev, atomic_req_8B_endianess_mode); 444da7525d2SEran Ben Elisha 445da7525d2SEran Ben Elisha /* Check if HW supports 8 bytes standard atomic operations and capable 446da7525d2SEran Ben Elisha * of host endianness respond 447da7525d2SEran Ben Elisha */ 448da7525d2SEran Ben Elisha tmp = MLX5_ATOMIC_OPS_CMP_SWAP | MLX5_ATOMIC_OPS_FETCH_ADD; 449da7525d2SEran Ben Elisha if (((atomic_operations & tmp) == tmp) && 450da7525d2SEran Ben Elisha (atomic_size_qp & MLX5_ATOMIC_SIZE_QP_8BYTES) && 451da7525d2SEran Ben Elisha (atomic_req_8B_endianness_mode)) { 452da7525d2SEran Ben Elisha props->atomic_cap = IB_ATOMIC_HCA; 453da7525d2SEran Ben Elisha } else { 454da7525d2SEran Ben Elisha props->atomic_cap = IB_ATOMIC_NONE; 455da7525d2SEran Ben Elisha } 456da7525d2SEran Ben Elisha } 457da7525d2SEran Ben Elisha 4581b5daf11SMajd Dibbiny static int mlx5_query_system_image_guid(struct ib_device *ibdev, 4591b5daf11SMajd Dibbiny __be64 *sys_image_guid) 4601b5daf11SMajd Dibbiny { 4611b5daf11SMajd Dibbiny struct mlx5_ib_dev *dev = to_mdev(ibdev); 4621b5daf11SMajd Dibbiny struct mlx5_core_dev *mdev = dev->mdev; 4631b5daf11SMajd Dibbiny u64 tmp; 4641b5daf11SMajd Dibbiny int err; 4651b5daf11SMajd Dibbiny 4661b5daf11SMajd Dibbiny switch (mlx5_get_vport_access_method(ibdev)) { 4671b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_MAD: 4681b5daf11SMajd Dibbiny return mlx5_query_mad_ifc_system_image_guid(ibdev, 4691b5daf11SMajd Dibbiny sys_image_guid); 4701b5daf11SMajd Dibbiny 4711b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_HCA: 4721b5daf11SMajd Dibbiny err = mlx5_query_hca_vport_system_image_guid(mdev, &tmp); 4733f89a643SAchiad Shochat break; 4743f89a643SAchiad Shochat 4753f89a643SAchiad Shochat case MLX5_VPORT_ACCESS_METHOD_NIC: 4763f89a643SAchiad Shochat err = mlx5_query_nic_vport_system_image_guid(mdev, &tmp); 4773f89a643SAchiad Shochat break; 4781b5daf11SMajd Dibbiny 4791b5daf11SMajd Dibbiny default: 4801b5daf11SMajd Dibbiny return -EINVAL; 4811b5daf11SMajd Dibbiny } 4823f89a643SAchiad Shochat 4833f89a643SAchiad Shochat if (!err) 4843f89a643SAchiad Shochat *sys_image_guid = cpu_to_be64(tmp); 4853f89a643SAchiad Shochat 4863f89a643SAchiad Shochat return err; 4873f89a643SAchiad Shochat 4881b5daf11SMajd Dibbiny } 4891b5daf11SMajd Dibbiny 4901b5daf11SMajd Dibbiny static int mlx5_query_max_pkeys(struct ib_device *ibdev, 4911b5daf11SMajd Dibbiny u16 *max_pkeys) 4921b5daf11SMajd Dibbiny { 4931b5daf11SMajd Dibbiny struct mlx5_ib_dev *dev = to_mdev(ibdev); 4941b5daf11SMajd Dibbiny struct mlx5_core_dev *mdev = dev->mdev; 4951b5daf11SMajd Dibbiny 4961b5daf11SMajd Dibbiny switch (mlx5_get_vport_access_method(ibdev)) { 4971b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_MAD: 4981b5daf11SMajd Dibbiny return mlx5_query_mad_ifc_max_pkeys(ibdev, max_pkeys); 4991b5daf11SMajd Dibbiny 5001b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_HCA: 5011b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_NIC: 5021b5daf11SMajd Dibbiny *max_pkeys = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, 5031b5daf11SMajd Dibbiny pkey_table_size)); 5041b5daf11SMajd Dibbiny return 0; 5051b5daf11SMajd Dibbiny 5061b5daf11SMajd Dibbiny default: 5071b5daf11SMajd Dibbiny return -EINVAL; 5081b5daf11SMajd Dibbiny } 5091b5daf11SMajd Dibbiny } 5101b5daf11SMajd Dibbiny 5111b5daf11SMajd Dibbiny static int mlx5_query_vendor_id(struct ib_device *ibdev, 5121b5daf11SMajd Dibbiny u32 *vendor_id) 5131b5daf11SMajd Dibbiny { 5141b5daf11SMajd Dibbiny struct mlx5_ib_dev *dev = to_mdev(ibdev); 5151b5daf11SMajd Dibbiny 5161b5daf11SMajd Dibbiny switch (mlx5_get_vport_access_method(ibdev)) { 5171b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_MAD: 5181b5daf11SMajd Dibbiny return mlx5_query_mad_ifc_vendor_id(ibdev, vendor_id); 5191b5daf11SMajd Dibbiny 5201b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_HCA: 5211b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_NIC: 5221b5daf11SMajd Dibbiny return mlx5_core_query_vendor_id(dev->mdev, vendor_id); 5231b5daf11SMajd Dibbiny 5241b5daf11SMajd Dibbiny default: 5251b5daf11SMajd Dibbiny return -EINVAL; 5261b5daf11SMajd Dibbiny } 5271b5daf11SMajd Dibbiny } 5281b5daf11SMajd Dibbiny 5291b5daf11SMajd Dibbiny static int mlx5_query_node_guid(struct mlx5_ib_dev *dev, 5301b5daf11SMajd Dibbiny __be64 *node_guid) 5311b5daf11SMajd Dibbiny { 5321b5daf11SMajd Dibbiny u64 tmp; 5331b5daf11SMajd Dibbiny int err; 5341b5daf11SMajd Dibbiny 5351b5daf11SMajd Dibbiny switch (mlx5_get_vport_access_method(&dev->ib_dev)) { 5361b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_MAD: 5371b5daf11SMajd Dibbiny return mlx5_query_mad_ifc_node_guid(dev, node_guid); 5381b5daf11SMajd Dibbiny 5391b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_HCA: 5401b5daf11SMajd Dibbiny err = mlx5_query_hca_vport_node_guid(dev->mdev, &tmp); 5413f89a643SAchiad Shochat break; 5423f89a643SAchiad Shochat 5433f89a643SAchiad Shochat case MLX5_VPORT_ACCESS_METHOD_NIC: 5443f89a643SAchiad Shochat err = mlx5_query_nic_vport_node_guid(dev->mdev, &tmp); 5453f89a643SAchiad Shochat break; 5461b5daf11SMajd Dibbiny 5471b5daf11SMajd Dibbiny default: 5481b5daf11SMajd Dibbiny return -EINVAL; 5491b5daf11SMajd Dibbiny } 5503f89a643SAchiad Shochat 5513f89a643SAchiad Shochat if (!err) 5523f89a643SAchiad Shochat *node_guid = cpu_to_be64(tmp); 5533f89a643SAchiad Shochat 5543f89a643SAchiad Shochat return err; 5551b5daf11SMajd Dibbiny } 5561b5daf11SMajd Dibbiny 5571b5daf11SMajd Dibbiny struct mlx5_reg_node_desc { 558bd99fdeaSYuval Shaia u8 desc[IB_DEVICE_NODE_DESC_MAX]; 5591b5daf11SMajd Dibbiny }; 5601b5daf11SMajd Dibbiny 5611b5daf11SMajd Dibbiny static int mlx5_query_node_desc(struct mlx5_ib_dev *dev, char *node_desc) 5621b5daf11SMajd Dibbiny { 5631b5daf11SMajd Dibbiny struct mlx5_reg_node_desc in; 5641b5daf11SMajd Dibbiny 5651b5daf11SMajd Dibbiny if (mlx5_use_mad_ifc(dev)) 5661b5daf11SMajd Dibbiny return mlx5_query_mad_ifc_node_desc(dev, node_desc); 5671b5daf11SMajd Dibbiny 5681b5daf11SMajd Dibbiny memset(&in, 0, sizeof(in)); 5691b5daf11SMajd Dibbiny 5701b5daf11SMajd Dibbiny return mlx5_core_access_reg(dev->mdev, &in, sizeof(in), node_desc, 5711b5daf11SMajd Dibbiny sizeof(struct mlx5_reg_node_desc), 5721b5daf11SMajd Dibbiny MLX5_REG_NODE_DESC, 0, 0); 5731b5daf11SMajd Dibbiny } 5741b5daf11SMajd Dibbiny 575e126ba97SEli Cohen static int mlx5_ib_query_device(struct ib_device *ibdev, 5762528e33eSMatan Barak struct ib_device_attr *props, 5772528e33eSMatan Barak struct ib_udata *uhw) 578e126ba97SEli Cohen { 579e126ba97SEli Cohen struct mlx5_ib_dev *dev = to_mdev(ibdev); 580938fe83cSSaeed Mahameed struct mlx5_core_dev *mdev = dev->mdev; 581e126ba97SEli Cohen int err = -ENOMEM; 582288c01b7SEli Cohen int max_sq_desc; 583e126ba97SEli Cohen int max_rq_sg; 584e126ba97SEli Cohen int max_sq_sg; 585e0238a6aSSagi Grimberg u64 min_page_size = 1ull << MLX5_CAP_GEN(mdev, log_pg_sz); 586402ca536SBodong Wang struct mlx5_ib_query_device_resp resp = {}; 587402ca536SBodong Wang size_t resp_len; 588402ca536SBodong Wang u64 max_tso; 589e126ba97SEli Cohen 590402ca536SBodong Wang resp_len = sizeof(resp.comp_mask) + sizeof(resp.response_length); 591402ca536SBodong Wang if (uhw->outlen && uhw->outlen < resp_len) 592402ca536SBodong Wang return -EINVAL; 593402ca536SBodong Wang else 594402ca536SBodong Wang resp.response_length = resp_len; 595402ca536SBodong Wang 596402ca536SBodong Wang if (uhw->inlen && !ib_is_udata_cleared(uhw, 0, uhw->inlen)) 5972528e33eSMatan Barak return -EINVAL; 5982528e33eSMatan Barak 599e126ba97SEli Cohen memset(props, 0, sizeof(*props)); 6001b5daf11SMajd Dibbiny err = mlx5_query_system_image_guid(ibdev, 6011b5daf11SMajd Dibbiny &props->sys_image_guid); 6021b5daf11SMajd Dibbiny if (err) 6031b5daf11SMajd Dibbiny return err; 6041b5daf11SMajd Dibbiny 6051b5daf11SMajd Dibbiny err = mlx5_query_max_pkeys(ibdev, &props->max_pkeys); 6061b5daf11SMajd Dibbiny if (err) 6071b5daf11SMajd Dibbiny return err; 6081b5daf11SMajd Dibbiny 6091b5daf11SMajd Dibbiny err = mlx5_query_vendor_id(ibdev, &props->vendor_id); 6101b5daf11SMajd Dibbiny if (err) 6111b5daf11SMajd Dibbiny return err; 612e126ba97SEli Cohen 6139603b61dSJack Morgenstein props->fw_ver = ((u64)fw_rev_maj(dev->mdev) << 32) | 6149603b61dSJack Morgenstein (fw_rev_min(dev->mdev) << 16) | 6159603b61dSJack Morgenstein fw_rev_sub(dev->mdev); 616e126ba97SEli Cohen props->device_cap_flags = IB_DEVICE_CHANGE_PHY_PORT | 617e126ba97SEli Cohen IB_DEVICE_PORT_ACTIVE_EVENT | 618e126ba97SEli Cohen IB_DEVICE_SYS_IMAGE_GUID | 6191a4c3a3dSEli Cohen IB_DEVICE_RC_RNR_NAK_GEN; 620938fe83cSSaeed Mahameed 621938fe83cSSaeed Mahameed if (MLX5_CAP_GEN(mdev, pkv)) 622e126ba97SEli Cohen props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR; 623938fe83cSSaeed Mahameed if (MLX5_CAP_GEN(mdev, qkv)) 624e126ba97SEli Cohen props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR; 625938fe83cSSaeed Mahameed if (MLX5_CAP_GEN(mdev, apm)) 626e126ba97SEli Cohen props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG; 627938fe83cSSaeed Mahameed if (MLX5_CAP_GEN(mdev, xrc)) 628e126ba97SEli Cohen props->device_cap_flags |= IB_DEVICE_XRC; 629d2370e0aSMatan Barak if (MLX5_CAP_GEN(mdev, imaicl)) { 630d2370e0aSMatan Barak props->device_cap_flags |= IB_DEVICE_MEM_WINDOW | 631d2370e0aSMatan Barak IB_DEVICE_MEM_WINDOW_TYPE_2B; 632d2370e0aSMatan Barak props->max_mw = 1 << MLX5_CAP_GEN(mdev, log_max_mkey); 633b005d316SSagi Grimberg /* We support 'Gappy' memory registration too */ 634b005d316SSagi Grimberg props->device_cap_flags |= IB_DEVICE_SG_GAPS_REG; 635d2370e0aSMatan Barak } 636e126ba97SEli Cohen props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS; 637938fe83cSSaeed Mahameed if (MLX5_CAP_GEN(mdev, sho)) { 6382dea9094SSagi Grimberg props->device_cap_flags |= IB_DEVICE_SIGNATURE_HANDOVER; 6392dea9094SSagi Grimberg /* At this stage no support for signature handover */ 6402dea9094SSagi Grimberg props->sig_prot_cap = IB_PROT_T10DIF_TYPE_1 | 6412dea9094SSagi Grimberg IB_PROT_T10DIF_TYPE_2 | 6422dea9094SSagi Grimberg IB_PROT_T10DIF_TYPE_3; 6432dea9094SSagi Grimberg props->sig_guard_cap = IB_GUARD_T10DIF_CRC | 6442dea9094SSagi Grimberg IB_GUARD_T10DIF_CSUM; 6452dea9094SSagi Grimberg } 646938fe83cSSaeed Mahameed if (MLX5_CAP_GEN(mdev, block_lb_mc)) 647f360d88aSEli Cohen props->device_cap_flags |= IB_DEVICE_BLOCK_MULTICAST_LOOPBACK; 648e126ba97SEli Cohen 649402ca536SBodong Wang if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads)) { 650e8161334SNoa Osherovich if (MLX5_CAP_ETH(mdev, csum_cap)) { 651e8161334SNoa Osherovich /* Legacy bit to support old userspace libraries */ 65288115fe7SBodong Wang props->device_cap_flags |= IB_DEVICE_RAW_IP_CSUM; 653e8161334SNoa Osherovich props->raw_packet_caps |= IB_RAW_PACKET_CAP_IP_CSUM; 654e8161334SNoa Osherovich } 655e8161334SNoa Osherovich 656e8161334SNoa Osherovich if (MLX5_CAP_ETH(dev->mdev, vlan_cap)) 657e8161334SNoa Osherovich props->raw_packet_caps |= 658e8161334SNoa Osherovich IB_RAW_PACKET_CAP_CVLAN_STRIPPING; 65988115fe7SBodong Wang 660402ca536SBodong Wang if (field_avail(typeof(resp), tso_caps, uhw->outlen)) { 661402ca536SBodong Wang max_tso = MLX5_CAP_ETH(mdev, max_lso_cap); 662402ca536SBodong Wang if (max_tso) { 663402ca536SBodong Wang resp.tso_caps.max_tso = 1 << max_tso; 664402ca536SBodong Wang resp.tso_caps.supported_qpts |= 665402ca536SBodong Wang 1 << IB_QPT_RAW_PACKET; 666402ca536SBodong Wang resp.response_length += sizeof(resp.tso_caps); 667402ca536SBodong Wang } 668402ca536SBodong Wang } 66931f69a82SYishai Hadas 67031f69a82SYishai Hadas if (field_avail(typeof(resp), rss_caps, uhw->outlen)) { 67131f69a82SYishai Hadas resp.rss_caps.rx_hash_function = 67231f69a82SYishai Hadas MLX5_RX_HASH_FUNC_TOEPLITZ; 67331f69a82SYishai Hadas resp.rss_caps.rx_hash_fields_mask = 67431f69a82SYishai Hadas MLX5_RX_HASH_SRC_IPV4 | 67531f69a82SYishai Hadas MLX5_RX_HASH_DST_IPV4 | 67631f69a82SYishai Hadas MLX5_RX_HASH_SRC_IPV6 | 67731f69a82SYishai Hadas MLX5_RX_HASH_DST_IPV6 | 67831f69a82SYishai Hadas MLX5_RX_HASH_SRC_PORT_TCP | 67931f69a82SYishai Hadas MLX5_RX_HASH_DST_PORT_TCP | 68031f69a82SYishai Hadas MLX5_RX_HASH_SRC_PORT_UDP | 68131f69a82SYishai Hadas MLX5_RX_HASH_DST_PORT_UDP; 68231f69a82SYishai Hadas resp.response_length += sizeof(resp.rss_caps); 68331f69a82SYishai Hadas } 68431f69a82SYishai Hadas } else { 68531f69a82SYishai Hadas if (field_avail(typeof(resp), tso_caps, uhw->outlen)) 68631f69a82SYishai Hadas resp.response_length += sizeof(resp.tso_caps); 68731f69a82SYishai Hadas if (field_avail(typeof(resp), rss_caps, uhw->outlen)) 68831f69a82SYishai Hadas resp.response_length += sizeof(resp.rss_caps); 689402ca536SBodong Wang } 690402ca536SBodong Wang 691f0313965SErez Shitrit if (MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) { 692f0313965SErez Shitrit props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM; 693f0313965SErez Shitrit props->device_cap_flags |= IB_DEVICE_UD_TSO; 694f0313965SErez Shitrit } 695f0313965SErez Shitrit 696cff5a0f3SMajd Dibbiny if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) && 697e8161334SNoa Osherovich MLX5_CAP_ETH(dev->mdev, scatter_fcs)) { 698e8161334SNoa Osherovich /* Legacy bit to support old userspace libraries */ 699cff5a0f3SMajd Dibbiny props->device_cap_flags |= IB_DEVICE_RAW_SCATTER_FCS; 700e8161334SNoa Osherovich props->raw_packet_caps |= IB_RAW_PACKET_CAP_SCATTER_FCS; 701e8161334SNoa Osherovich } 702cff5a0f3SMajd Dibbiny 703da6d6ba3SMaor Gottlieb if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS)) 704da6d6ba3SMaor Gottlieb props->device_cap_flags |= IB_DEVICE_MANAGED_FLOW_STEERING; 705da6d6ba3SMaor Gottlieb 7061b5daf11SMajd Dibbiny props->vendor_part_id = mdev->pdev->device; 7071b5daf11SMajd Dibbiny props->hw_ver = mdev->pdev->revision; 708e126ba97SEli Cohen 709e126ba97SEli Cohen props->max_mr_size = ~0ull; 710e0238a6aSSagi Grimberg props->page_size_cap = ~(min_page_size - 1); 711938fe83cSSaeed Mahameed props->max_qp = 1 << MLX5_CAP_GEN(mdev, log_max_qp); 712938fe83cSSaeed Mahameed props->max_qp_wr = 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz); 713938fe83cSSaeed Mahameed max_rq_sg = MLX5_CAP_GEN(mdev, max_wqe_sz_rq) / 714938fe83cSSaeed Mahameed sizeof(struct mlx5_wqe_data_seg); 715288c01b7SEli Cohen max_sq_desc = min_t(int, MLX5_CAP_GEN(mdev, max_wqe_sz_sq), 512); 716288c01b7SEli Cohen max_sq_sg = (max_sq_desc - sizeof(struct mlx5_wqe_ctrl_seg) - 717288c01b7SEli Cohen sizeof(struct mlx5_wqe_raddr_seg)) / 718e126ba97SEli Cohen sizeof(struct mlx5_wqe_data_seg); 719e126ba97SEli Cohen props->max_sge = min(max_rq_sg, max_sq_sg); 720986ef95eSSagi Grimberg props->max_sge_rd = MLX5_MAX_SGE_RD; 721938fe83cSSaeed Mahameed props->max_cq = 1 << MLX5_CAP_GEN(mdev, log_max_cq); 7229f177686SLeon Romanovsky props->max_cqe = (1 << MLX5_CAP_GEN(mdev, log_max_cq_sz)) - 1; 723938fe83cSSaeed Mahameed props->max_mr = 1 << MLX5_CAP_GEN(mdev, log_max_mkey); 724938fe83cSSaeed Mahameed props->max_pd = 1 << MLX5_CAP_GEN(mdev, log_max_pd); 725938fe83cSSaeed Mahameed props->max_qp_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_req_qp); 726938fe83cSSaeed Mahameed props->max_qp_init_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_res_qp); 727938fe83cSSaeed Mahameed props->max_srq = 1 << MLX5_CAP_GEN(mdev, log_max_srq); 728938fe83cSSaeed Mahameed props->max_srq_wr = (1 << MLX5_CAP_GEN(mdev, log_max_srq_sz)) - 1; 729938fe83cSSaeed Mahameed props->local_ca_ack_delay = MLX5_CAP_GEN(mdev, local_ca_ack_delay); 730e126ba97SEli Cohen props->max_res_rd_atom = props->max_qp_rd_atom * props->max_qp; 731e126ba97SEli Cohen props->max_srq_sge = max_rq_sg - 1; 732911f4331SSagi Grimberg props->max_fast_reg_page_list_len = 733911f4331SSagi Grimberg 1 << MLX5_CAP_GEN(mdev, log_max_klm_list_size); 734da7525d2SEran Ben Elisha get_atomic_caps(dev, props); 73581bea28fSEli Cohen props->masked_atomic_cap = IB_ATOMIC_NONE; 736938fe83cSSaeed Mahameed props->max_mcast_grp = 1 << MLX5_CAP_GEN(mdev, log_max_mcg); 737938fe83cSSaeed Mahameed props->max_mcast_qp_attach = MLX5_CAP_GEN(mdev, max_qp_mcg); 738e126ba97SEli Cohen props->max_total_mcast_qp_attach = props->max_mcast_qp_attach * 739e126ba97SEli Cohen props->max_mcast_grp; 740e126ba97SEli Cohen props->max_map_per_fmr = INT_MAX; /* no limit in ConnectIB */ 74186695a65SMaor Gottlieb props->max_ah = INT_MAX; 7427c60bcbbSMatan Barak props->hca_core_clock = MLX5_CAP_GEN(mdev, device_frequency_khz); 7437c60bcbbSMatan Barak props->timestamp_mask = 0x7FFFFFFFFFFFFFFFULL; 744e126ba97SEli Cohen 7458cdd312cSHaggai Eran #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING 746938fe83cSSaeed Mahameed if (MLX5_CAP_GEN(mdev, pg)) 7478cdd312cSHaggai Eran props->device_cap_flags |= IB_DEVICE_ON_DEMAND_PAGING; 7488cdd312cSHaggai Eran props->odp_caps = dev->odp_caps; 7498cdd312cSHaggai Eran #endif 7508cdd312cSHaggai Eran 751051f2630SLeon Romanovsky if (MLX5_CAP_GEN(mdev, cd)) 752051f2630SLeon Romanovsky props->device_cap_flags |= IB_DEVICE_CROSS_CHANNEL; 753051f2630SLeon Romanovsky 754eff901d3SEli Cohen if (!mlx5_core_is_pf(mdev)) 755eff901d3SEli Cohen props->device_cap_flags |= IB_DEVICE_VIRTUAL_FUNCTION; 756eff901d3SEli Cohen 75731f69a82SYishai Hadas if (mlx5_ib_port_link_layer(ibdev, 1) == 75831f69a82SYishai Hadas IB_LINK_LAYER_ETHERNET) { 75931f69a82SYishai Hadas props->rss_caps.max_rwq_indirection_tables = 76031f69a82SYishai Hadas 1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt); 76131f69a82SYishai Hadas props->rss_caps.max_rwq_indirection_table_size = 76231f69a82SYishai Hadas 1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt_size); 76331f69a82SYishai Hadas props->rss_caps.supported_qpts = 1 << IB_QPT_RAW_PACKET; 76431f69a82SYishai Hadas props->max_wq_type_rq = 76531f69a82SYishai Hadas 1 << MLX5_CAP_GEN(dev->mdev, log_max_rq); 76631f69a82SYishai Hadas } 76731f69a82SYishai Hadas 7687e43a2a5SBodong Wang if (field_avail(typeof(resp), cqe_comp_caps, uhw->outlen)) { 7697e43a2a5SBodong Wang resp.cqe_comp_caps.max_num = 7707e43a2a5SBodong Wang MLX5_CAP_GEN(dev->mdev, cqe_compression) ? 7717e43a2a5SBodong Wang MLX5_CAP_GEN(dev->mdev, cqe_compression_max_num) : 0; 7727e43a2a5SBodong Wang resp.cqe_comp_caps.supported_format = 7737e43a2a5SBodong Wang MLX5_IB_CQE_RES_FORMAT_HASH | 7747e43a2a5SBodong Wang MLX5_IB_CQE_RES_FORMAT_CSUM; 7757e43a2a5SBodong Wang resp.response_length += sizeof(resp.cqe_comp_caps); 7767e43a2a5SBodong Wang } 7777e43a2a5SBodong Wang 778d949167dSBodong Wang if (field_avail(typeof(resp), packet_pacing_caps, uhw->outlen)) { 779d949167dSBodong Wang if (MLX5_CAP_QOS(mdev, packet_pacing) && 780d949167dSBodong Wang MLX5_CAP_GEN(mdev, qos)) { 781d949167dSBodong Wang resp.packet_pacing_caps.qp_rate_limit_max = 782d949167dSBodong Wang MLX5_CAP_QOS(mdev, packet_pacing_max_rate); 783d949167dSBodong Wang resp.packet_pacing_caps.qp_rate_limit_min = 784d949167dSBodong Wang MLX5_CAP_QOS(mdev, packet_pacing_min_rate); 785d949167dSBodong Wang resp.packet_pacing_caps.supported_qpts |= 786d949167dSBodong Wang 1 << IB_QPT_RAW_PACKET; 787d949167dSBodong Wang } 788d949167dSBodong Wang resp.response_length += sizeof(resp.packet_pacing_caps); 789d949167dSBodong Wang } 790d949167dSBodong Wang 7919f885201SLeon Romanovsky if (field_avail(typeof(resp), mlx5_ib_support_multi_pkt_send_wqes, 7929f885201SLeon Romanovsky uhw->outlen)) { 7939f885201SLeon Romanovsky resp.mlx5_ib_support_multi_pkt_send_wqes = 7949f885201SLeon Romanovsky MLX5_CAP_ETH(mdev, multi_pkt_send_wqe); 7959f885201SLeon Romanovsky resp.response_length += 7969f885201SLeon Romanovsky sizeof(resp.mlx5_ib_support_multi_pkt_send_wqes); 7979f885201SLeon Romanovsky } 7989f885201SLeon Romanovsky 7999f885201SLeon Romanovsky if (field_avail(typeof(resp), reserved, uhw->outlen)) 8009f885201SLeon Romanovsky resp.response_length += sizeof(resp.reserved); 8019f885201SLeon Romanovsky 802402ca536SBodong Wang if (uhw->outlen) { 803402ca536SBodong Wang err = ib_copy_to_udata(uhw, &resp, resp.response_length); 804402ca536SBodong Wang 805402ca536SBodong Wang if (err) 806402ca536SBodong Wang return err; 807402ca536SBodong Wang } 808402ca536SBodong Wang 8091b5daf11SMajd Dibbiny return 0; 8101b5daf11SMajd Dibbiny } 811e126ba97SEli Cohen 8121b5daf11SMajd Dibbiny enum mlx5_ib_width { 8131b5daf11SMajd Dibbiny MLX5_IB_WIDTH_1X = 1 << 0, 8141b5daf11SMajd Dibbiny MLX5_IB_WIDTH_2X = 1 << 1, 8151b5daf11SMajd Dibbiny MLX5_IB_WIDTH_4X = 1 << 2, 8161b5daf11SMajd Dibbiny MLX5_IB_WIDTH_8X = 1 << 3, 8171b5daf11SMajd Dibbiny MLX5_IB_WIDTH_12X = 1 << 4 8181b5daf11SMajd Dibbiny }; 8191b5daf11SMajd Dibbiny 8201b5daf11SMajd Dibbiny static int translate_active_width(struct ib_device *ibdev, u8 active_width, 8211b5daf11SMajd Dibbiny u8 *ib_width) 8221b5daf11SMajd Dibbiny { 8231b5daf11SMajd Dibbiny struct mlx5_ib_dev *dev = to_mdev(ibdev); 8241b5daf11SMajd Dibbiny int err = 0; 8251b5daf11SMajd Dibbiny 8261b5daf11SMajd Dibbiny if (active_width & MLX5_IB_WIDTH_1X) { 8271b5daf11SMajd Dibbiny *ib_width = IB_WIDTH_1X; 8281b5daf11SMajd Dibbiny } else if (active_width & MLX5_IB_WIDTH_2X) { 8291b5daf11SMajd Dibbiny mlx5_ib_dbg(dev, "active_width %d is not supported by IB spec\n", 8301b5daf11SMajd Dibbiny (int)active_width); 8311b5daf11SMajd Dibbiny err = -EINVAL; 8321b5daf11SMajd Dibbiny } else if (active_width & MLX5_IB_WIDTH_4X) { 8331b5daf11SMajd Dibbiny *ib_width = IB_WIDTH_4X; 8341b5daf11SMajd Dibbiny } else if (active_width & MLX5_IB_WIDTH_8X) { 8351b5daf11SMajd Dibbiny *ib_width = IB_WIDTH_8X; 8361b5daf11SMajd Dibbiny } else if (active_width & MLX5_IB_WIDTH_12X) { 8371b5daf11SMajd Dibbiny *ib_width = IB_WIDTH_12X; 8381b5daf11SMajd Dibbiny } else { 8391b5daf11SMajd Dibbiny mlx5_ib_dbg(dev, "Invalid active_width %d\n", 8401b5daf11SMajd Dibbiny (int)active_width); 8411b5daf11SMajd Dibbiny err = -EINVAL; 8421b5daf11SMajd Dibbiny } 8431b5daf11SMajd Dibbiny 8441b5daf11SMajd Dibbiny return err; 8451b5daf11SMajd Dibbiny } 8461b5daf11SMajd Dibbiny 8471b5daf11SMajd Dibbiny static int mlx5_mtu_to_ib_mtu(int mtu) 8481b5daf11SMajd Dibbiny { 8491b5daf11SMajd Dibbiny switch (mtu) { 8501b5daf11SMajd Dibbiny case 256: return 1; 8511b5daf11SMajd Dibbiny case 512: return 2; 8521b5daf11SMajd Dibbiny case 1024: return 3; 8531b5daf11SMajd Dibbiny case 2048: return 4; 8541b5daf11SMajd Dibbiny case 4096: return 5; 8551b5daf11SMajd Dibbiny default: 8561b5daf11SMajd Dibbiny pr_warn("invalid mtu\n"); 8571b5daf11SMajd Dibbiny return -1; 8581b5daf11SMajd Dibbiny } 8591b5daf11SMajd Dibbiny } 8601b5daf11SMajd Dibbiny 8611b5daf11SMajd Dibbiny enum ib_max_vl_num { 8621b5daf11SMajd Dibbiny __IB_MAX_VL_0 = 1, 8631b5daf11SMajd Dibbiny __IB_MAX_VL_0_1 = 2, 8641b5daf11SMajd Dibbiny __IB_MAX_VL_0_3 = 3, 8651b5daf11SMajd Dibbiny __IB_MAX_VL_0_7 = 4, 8661b5daf11SMajd Dibbiny __IB_MAX_VL_0_14 = 5, 8671b5daf11SMajd Dibbiny }; 8681b5daf11SMajd Dibbiny 8691b5daf11SMajd Dibbiny enum mlx5_vl_hw_cap { 8701b5daf11SMajd Dibbiny MLX5_VL_HW_0 = 1, 8711b5daf11SMajd Dibbiny MLX5_VL_HW_0_1 = 2, 8721b5daf11SMajd Dibbiny MLX5_VL_HW_0_2 = 3, 8731b5daf11SMajd Dibbiny MLX5_VL_HW_0_3 = 4, 8741b5daf11SMajd Dibbiny MLX5_VL_HW_0_4 = 5, 8751b5daf11SMajd Dibbiny MLX5_VL_HW_0_5 = 6, 8761b5daf11SMajd Dibbiny MLX5_VL_HW_0_6 = 7, 8771b5daf11SMajd Dibbiny MLX5_VL_HW_0_7 = 8, 8781b5daf11SMajd Dibbiny MLX5_VL_HW_0_14 = 15 8791b5daf11SMajd Dibbiny }; 8801b5daf11SMajd Dibbiny 8811b5daf11SMajd Dibbiny static int translate_max_vl_num(struct ib_device *ibdev, u8 vl_hw_cap, 8821b5daf11SMajd Dibbiny u8 *max_vl_num) 8831b5daf11SMajd Dibbiny { 8841b5daf11SMajd Dibbiny switch (vl_hw_cap) { 8851b5daf11SMajd Dibbiny case MLX5_VL_HW_0: 8861b5daf11SMajd Dibbiny *max_vl_num = __IB_MAX_VL_0; 8871b5daf11SMajd Dibbiny break; 8881b5daf11SMajd Dibbiny case MLX5_VL_HW_0_1: 8891b5daf11SMajd Dibbiny *max_vl_num = __IB_MAX_VL_0_1; 8901b5daf11SMajd Dibbiny break; 8911b5daf11SMajd Dibbiny case MLX5_VL_HW_0_3: 8921b5daf11SMajd Dibbiny *max_vl_num = __IB_MAX_VL_0_3; 8931b5daf11SMajd Dibbiny break; 8941b5daf11SMajd Dibbiny case MLX5_VL_HW_0_7: 8951b5daf11SMajd Dibbiny *max_vl_num = __IB_MAX_VL_0_7; 8961b5daf11SMajd Dibbiny break; 8971b5daf11SMajd Dibbiny case MLX5_VL_HW_0_14: 8981b5daf11SMajd Dibbiny *max_vl_num = __IB_MAX_VL_0_14; 8991b5daf11SMajd Dibbiny break; 9001b5daf11SMajd Dibbiny 9011b5daf11SMajd Dibbiny default: 9021b5daf11SMajd Dibbiny return -EINVAL; 9031b5daf11SMajd Dibbiny } 9041b5daf11SMajd Dibbiny 9051b5daf11SMajd Dibbiny return 0; 9061b5daf11SMajd Dibbiny } 9071b5daf11SMajd Dibbiny 9081b5daf11SMajd Dibbiny static int mlx5_query_hca_port(struct ib_device *ibdev, u8 port, 9091b5daf11SMajd Dibbiny struct ib_port_attr *props) 9101b5daf11SMajd Dibbiny { 9111b5daf11SMajd Dibbiny struct mlx5_ib_dev *dev = to_mdev(ibdev); 9121b5daf11SMajd Dibbiny struct mlx5_core_dev *mdev = dev->mdev; 9131b5daf11SMajd Dibbiny struct mlx5_hca_vport_context *rep; 914046339eaSSaeed Mahameed u16 max_mtu; 915046339eaSSaeed Mahameed u16 oper_mtu; 9161b5daf11SMajd Dibbiny int err; 9171b5daf11SMajd Dibbiny u8 ib_link_width_oper; 9181b5daf11SMajd Dibbiny u8 vl_hw_cap; 9191b5daf11SMajd Dibbiny 9201b5daf11SMajd Dibbiny rep = kzalloc(sizeof(*rep), GFP_KERNEL); 9211b5daf11SMajd Dibbiny if (!rep) { 9221b5daf11SMajd Dibbiny err = -ENOMEM; 9231b5daf11SMajd Dibbiny goto out; 9241b5daf11SMajd Dibbiny } 9251b5daf11SMajd Dibbiny 926c4550c63SOr Gerlitz /* props being zeroed by the caller, avoid zeroing it here */ 9271b5daf11SMajd Dibbiny 9281b5daf11SMajd Dibbiny err = mlx5_query_hca_vport_context(mdev, 0, port, 0, rep); 9291b5daf11SMajd Dibbiny if (err) 9301b5daf11SMajd Dibbiny goto out; 9311b5daf11SMajd Dibbiny 9321b5daf11SMajd Dibbiny props->lid = rep->lid; 9331b5daf11SMajd Dibbiny props->lmc = rep->lmc; 9341b5daf11SMajd Dibbiny props->sm_lid = rep->sm_lid; 9351b5daf11SMajd Dibbiny props->sm_sl = rep->sm_sl; 9361b5daf11SMajd Dibbiny props->state = rep->vport_state; 9371b5daf11SMajd Dibbiny props->phys_state = rep->port_physical_state; 9381b5daf11SMajd Dibbiny props->port_cap_flags = rep->cap_mask1; 9391b5daf11SMajd Dibbiny props->gid_tbl_len = mlx5_get_gid_table_len(MLX5_CAP_GEN(mdev, gid_table_size)); 9401b5daf11SMajd Dibbiny props->max_msg_sz = 1 << MLX5_CAP_GEN(mdev, log_max_msg); 9411b5daf11SMajd Dibbiny props->pkey_tbl_len = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, pkey_table_size)); 9421b5daf11SMajd Dibbiny props->bad_pkey_cntr = rep->pkey_violation_counter; 9431b5daf11SMajd Dibbiny props->qkey_viol_cntr = rep->qkey_violation_counter; 9441b5daf11SMajd Dibbiny props->subnet_timeout = rep->subnet_timeout; 9451b5daf11SMajd Dibbiny props->init_type_reply = rep->init_type_reply; 946eff901d3SEli Cohen props->grh_required = rep->grh_required; 9471b5daf11SMajd Dibbiny 9481b5daf11SMajd Dibbiny err = mlx5_query_port_link_width_oper(mdev, &ib_link_width_oper, port); 9491b5daf11SMajd Dibbiny if (err) 9501b5daf11SMajd Dibbiny goto out; 9511b5daf11SMajd Dibbiny 9521b5daf11SMajd Dibbiny err = translate_active_width(ibdev, ib_link_width_oper, 9531b5daf11SMajd Dibbiny &props->active_width); 9541b5daf11SMajd Dibbiny if (err) 9551b5daf11SMajd Dibbiny goto out; 956d5beb7f2SNoa Osherovich err = mlx5_query_port_ib_proto_oper(mdev, &props->active_speed, port); 9571b5daf11SMajd Dibbiny if (err) 9581b5daf11SMajd Dibbiny goto out; 9591b5daf11SMajd Dibbiny 960facc9699SSaeed Mahameed mlx5_query_port_max_mtu(mdev, &max_mtu, port); 9611b5daf11SMajd Dibbiny 9621b5daf11SMajd Dibbiny props->max_mtu = mlx5_mtu_to_ib_mtu(max_mtu); 9631b5daf11SMajd Dibbiny 964facc9699SSaeed Mahameed mlx5_query_port_oper_mtu(mdev, &oper_mtu, port); 9651b5daf11SMajd Dibbiny 9661b5daf11SMajd Dibbiny props->active_mtu = mlx5_mtu_to_ib_mtu(oper_mtu); 9671b5daf11SMajd Dibbiny 9681b5daf11SMajd Dibbiny err = mlx5_query_port_vl_hw_cap(mdev, &vl_hw_cap, port); 9691b5daf11SMajd Dibbiny if (err) 9701b5daf11SMajd Dibbiny goto out; 9711b5daf11SMajd Dibbiny 9721b5daf11SMajd Dibbiny err = translate_max_vl_num(ibdev, vl_hw_cap, 9731b5daf11SMajd Dibbiny &props->max_vl_num); 9741b5daf11SMajd Dibbiny out: 9751b5daf11SMajd Dibbiny kfree(rep); 976e126ba97SEli Cohen return err; 977e126ba97SEli Cohen } 978e126ba97SEli Cohen 979e126ba97SEli Cohen int mlx5_ib_query_port(struct ib_device *ibdev, u8 port, 980e126ba97SEli Cohen struct ib_port_attr *props) 981e126ba97SEli Cohen { 9821b5daf11SMajd Dibbiny switch (mlx5_get_vport_access_method(ibdev)) { 9831b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_MAD: 9841b5daf11SMajd Dibbiny return mlx5_query_mad_ifc_port(ibdev, port, props); 985e126ba97SEli Cohen 9861b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_HCA: 9871b5daf11SMajd Dibbiny return mlx5_query_hca_port(ibdev, port, props); 9881b5daf11SMajd Dibbiny 9893f89a643SAchiad Shochat case MLX5_VPORT_ACCESS_METHOD_NIC: 99050f22fd8SNoa Osherovich mlx5_query_port_roce(ibdev, port, props); 99150f22fd8SNoa Osherovich return 0; 9923f89a643SAchiad Shochat 9931b5daf11SMajd Dibbiny default: 994e126ba97SEli Cohen return -EINVAL; 995e126ba97SEli Cohen } 996e126ba97SEli Cohen } 997e126ba97SEli Cohen 998e126ba97SEli Cohen static int mlx5_ib_query_gid(struct ib_device *ibdev, u8 port, int index, 999e126ba97SEli Cohen union ib_gid *gid) 1000e126ba97SEli Cohen { 10011b5daf11SMajd Dibbiny struct mlx5_ib_dev *dev = to_mdev(ibdev); 10021b5daf11SMajd Dibbiny struct mlx5_core_dev *mdev = dev->mdev; 1003e126ba97SEli Cohen 10041b5daf11SMajd Dibbiny switch (mlx5_get_vport_access_method(ibdev)) { 10051b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_MAD: 10061b5daf11SMajd Dibbiny return mlx5_query_mad_ifc_gids(ibdev, port, index, gid); 1007e126ba97SEli Cohen 10081b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_HCA: 10091b5daf11SMajd Dibbiny return mlx5_query_hca_vport_gid(mdev, 0, port, 0, index, gid); 1010e126ba97SEli Cohen 10111b5daf11SMajd Dibbiny default: 10121b5daf11SMajd Dibbiny return -EINVAL; 10131b5daf11SMajd Dibbiny } 1014e126ba97SEli Cohen 1015e126ba97SEli Cohen } 1016e126ba97SEli Cohen 1017e126ba97SEli Cohen static int mlx5_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index, 1018e126ba97SEli Cohen u16 *pkey) 1019e126ba97SEli Cohen { 10201b5daf11SMajd Dibbiny struct mlx5_ib_dev *dev = to_mdev(ibdev); 10211b5daf11SMajd Dibbiny struct mlx5_core_dev *mdev = dev->mdev; 1022e126ba97SEli Cohen 10231b5daf11SMajd Dibbiny switch (mlx5_get_vport_access_method(ibdev)) { 10241b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_MAD: 10251b5daf11SMajd Dibbiny return mlx5_query_mad_ifc_pkey(ibdev, port, index, pkey); 1026e126ba97SEli Cohen 10271b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_HCA: 10281b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_NIC: 10291b5daf11SMajd Dibbiny return mlx5_query_hca_vport_pkey(mdev, 0, port, 0, index, 10301b5daf11SMajd Dibbiny pkey); 10311b5daf11SMajd Dibbiny default: 10321b5daf11SMajd Dibbiny return -EINVAL; 1033e126ba97SEli Cohen } 10341b5daf11SMajd Dibbiny } 1035e126ba97SEli Cohen 1036e126ba97SEli Cohen static int mlx5_ib_modify_device(struct ib_device *ibdev, int mask, 1037e126ba97SEli Cohen struct ib_device_modify *props) 1038e126ba97SEli Cohen { 1039e126ba97SEli Cohen struct mlx5_ib_dev *dev = to_mdev(ibdev); 1040e126ba97SEli Cohen struct mlx5_reg_node_desc in; 1041e126ba97SEli Cohen struct mlx5_reg_node_desc out; 1042e126ba97SEli Cohen int err; 1043e126ba97SEli Cohen 1044e126ba97SEli Cohen if (mask & ~IB_DEVICE_MODIFY_NODE_DESC) 1045e126ba97SEli Cohen return -EOPNOTSUPP; 1046e126ba97SEli Cohen 1047e126ba97SEli Cohen if (!(mask & IB_DEVICE_MODIFY_NODE_DESC)) 1048e126ba97SEli Cohen return 0; 1049e126ba97SEli Cohen 1050e126ba97SEli Cohen /* 1051e126ba97SEli Cohen * If possible, pass node desc to FW, so it can generate 1052e126ba97SEli Cohen * a 144 trap. If cmd fails, just ignore. 1053e126ba97SEli Cohen */ 1054bd99fdeaSYuval Shaia memcpy(&in, props->node_desc, IB_DEVICE_NODE_DESC_MAX); 10559603b61dSJack Morgenstein err = mlx5_core_access_reg(dev->mdev, &in, sizeof(in), &out, 1056e126ba97SEli Cohen sizeof(out), MLX5_REG_NODE_DESC, 0, 1); 1057e126ba97SEli Cohen if (err) 1058e126ba97SEli Cohen return err; 1059e126ba97SEli Cohen 1060bd99fdeaSYuval Shaia memcpy(ibdev->node_desc, props->node_desc, IB_DEVICE_NODE_DESC_MAX); 1061e126ba97SEli Cohen 1062e126ba97SEli Cohen return err; 1063e126ba97SEli Cohen } 1064e126ba97SEli Cohen 1065cdbe33d0SEli Cohen static int set_port_caps_atomic(struct mlx5_ib_dev *dev, u8 port_num, u32 mask, 1066cdbe33d0SEli Cohen u32 value) 1067cdbe33d0SEli Cohen { 1068cdbe33d0SEli Cohen struct mlx5_hca_vport_context ctx = {}; 1069cdbe33d0SEli Cohen int err; 1070cdbe33d0SEli Cohen 1071cdbe33d0SEli Cohen err = mlx5_query_hca_vport_context(dev->mdev, 0, 1072cdbe33d0SEli Cohen port_num, 0, &ctx); 1073cdbe33d0SEli Cohen if (err) 1074cdbe33d0SEli Cohen return err; 1075cdbe33d0SEli Cohen 1076cdbe33d0SEli Cohen if (~ctx.cap_mask1_perm & mask) { 1077cdbe33d0SEli Cohen mlx5_ib_warn(dev, "trying to change bitmask 0x%X but change supported 0x%X\n", 1078cdbe33d0SEli Cohen mask, ctx.cap_mask1_perm); 1079cdbe33d0SEli Cohen return -EINVAL; 1080cdbe33d0SEli Cohen } 1081cdbe33d0SEli Cohen 1082cdbe33d0SEli Cohen ctx.cap_mask1 = value; 1083cdbe33d0SEli Cohen ctx.cap_mask1_perm = mask; 1084cdbe33d0SEli Cohen err = mlx5_core_modify_hca_vport_context(dev->mdev, 0, 1085cdbe33d0SEli Cohen port_num, 0, &ctx); 1086cdbe33d0SEli Cohen 1087cdbe33d0SEli Cohen return err; 1088cdbe33d0SEli Cohen } 1089cdbe33d0SEli Cohen 1090e126ba97SEli Cohen static int mlx5_ib_modify_port(struct ib_device *ibdev, u8 port, int mask, 1091e126ba97SEli Cohen struct ib_port_modify *props) 1092e126ba97SEli Cohen { 1093e126ba97SEli Cohen struct mlx5_ib_dev *dev = to_mdev(ibdev); 1094e126ba97SEli Cohen struct ib_port_attr attr; 1095e126ba97SEli Cohen u32 tmp; 1096e126ba97SEli Cohen int err; 1097cdbe33d0SEli Cohen u32 change_mask; 1098cdbe33d0SEli Cohen u32 value; 1099cdbe33d0SEli Cohen bool is_ib = (mlx5_ib_port_link_layer(ibdev, port) == 1100cdbe33d0SEli Cohen IB_LINK_LAYER_INFINIBAND); 1101cdbe33d0SEli Cohen 1102cdbe33d0SEli Cohen if (MLX5_CAP_GEN(dev->mdev, ib_virt) && is_ib) { 1103cdbe33d0SEli Cohen change_mask = props->clr_port_cap_mask | props->set_port_cap_mask; 1104cdbe33d0SEli Cohen value = ~props->clr_port_cap_mask | props->set_port_cap_mask; 1105cdbe33d0SEli Cohen return set_port_caps_atomic(dev, port, change_mask, value); 1106cdbe33d0SEli Cohen } 1107e126ba97SEli Cohen 1108e126ba97SEli Cohen mutex_lock(&dev->cap_mask_mutex); 1109e126ba97SEli Cohen 1110c4550c63SOr Gerlitz err = ib_query_port(ibdev, port, &attr); 1111e126ba97SEli Cohen if (err) 1112e126ba97SEli Cohen goto out; 1113e126ba97SEli Cohen 1114e126ba97SEli Cohen tmp = (attr.port_cap_flags | props->set_port_cap_mask) & 1115e126ba97SEli Cohen ~props->clr_port_cap_mask; 1116e126ba97SEli Cohen 11179603b61dSJack Morgenstein err = mlx5_set_port_caps(dev->mdev, port, tmp); 1118e126ba97SEli Cohen 1119e126ba97SEli Cohen out: 1120e126ba97SEli Cohen mutex_unlock(&dev->cap_mask_mutex); 1121e126ba97SEli Cohen return err; 1122e126ba97SEli Cohen } 1123e126ba97SEli Cohen 112430aa60b3SEli Cohen static void print_lib_caps(struct mlx5_ib_dev *dev, u64 caps) 112530aa60b3SEli Cohen { 112630aa60b3SEli Cohen mlx5_ib_dbg(dev, "MLX5_LIB_CAP_4K_UAR = %s\n", 112730aa60b3SEli Cohen caps & MLX5_LIB_CAP_4K_UAR ? "y" : "n"); 112830aa60b3SEli Cohen } 112930aa60b3SEli Cohen 1130b037c29aSEli Cohen static int calc_total_bfregs(struct mlx5_ib_dev *dev, bool lib_uar_4k, 1131b037c29aSEli Cohen struct mlx5_ib_alloc_ucontext_req_v2 *req, 1132b037c29aSEli Cohen u32 *num_sys_pages) 1133b037c29aSEli Cohen { 1134b037c29aSEli Cohen int uars_per_sys_page; 1135b037c29aSEli Cohen int bfregs_per_sys_page; 1136b037c29aSEli Cohen int ref_bfregs = req->total_num_bfregs; 1137b037c29aSEli Cohen 1138b037c29aSEli Cohen if (req->total_num_bfregs == 0) 1139b037c29aSEli Cohen return -EINVAL; 1140b037c29aSEli Cohen 1141b037c29aSEli Cohen BUILD_BUG_ON(MLX5_MAX_BFREGS % MLX5_NON_FP_BFREGS_IN_PAGE); 1142b037c29aSEli Cohen BUILD_BUG_ON(MLX5_MAX_BFREGS < MLX5_NON_FP_BFREGS_IN_PAGE); 1143b037c29aSEli Cohen 1144b037c29aSEli Cohen if (req->total_num_bfregs > MLX5_MAX_BFREGS) 1145b037c29aSEli Cohen return -ENOMEM; 1146b037c29aSEli Cohen 1147b037c29aSEli Cohen uars_per_sys_page = get_uars_per_sys_page(dev, lib_uar_4k); 1148b037c29aSEli Cohen bfregs_per_sys_page = uars_per_sys_page * MLX5_NON_FP_BFREGS_PER_UAR; 1149b037c29aSEli Cohen req->total_num_bfregs = ALIGN(req->total_num_bfregs, bfregs_per_sys_page); 1150b037c29aSEli Cohen *num_sys_pages = req->total_num_bfregs / bfregs_per_sys_page; 1151b037c29aSEli Cohen 1152b037c29aSEli Cohen if (req->num_low_latency_bfregs > req->total_num_bfregs - 1) 1153b037c29aSEli Cohen return -EINVAL; 1154b037c29aSEli Cohen 1155b037c29aSEli Cohen mlx5_ib_dbg(dev, "uar_4k: fw support %s, lib support %s, user requested %d bfregs, alloated %d, using %d sys pages\n", 1156b037c29aSEli Cohen MLX5_CAP_GEN(dev->mdev, uar_4k) ? "yes" : "no", 1157b037c29aSEli Cohen lib_uar_4k ? "yes" : "no", ref_bfregs, 1158b037c29aSEli Cohen req->total_num_bfregs, *num_sys_pages); 1159b037c29aSEli Cohen 1160b037c29aSEli Cohen return 0; 1161b037c29aSEli Cohen } 1162b037c29aSEli Cohen 1163b037c29aSEli Cohen static int allocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context) 1164b037c29aSEli Cohen { 1165b037c29aSEli Cohen struct mlx5_bfreg_info *bfregi; 1166b037c29aSEli Cohen int err; 1167b037c29aSEli Cohen int i; 1168b037c29aSEli Cohen 1169b037c29aSEli Cohen bfregi = &context->bfregi; 1170b037c29aSEli Cohen for (i = 0; i < bfregi->num_sys_pages; i++) { 1171b037c29aSEli Cohen err = mlx5_cmd_alloc_uar(dev->mdev, &bfregi->sys_pages[i]); 1172b037c29aSEli Cohen if (err) 1173b037c29aSEli Cohen goto error; 1174b037c29aSEli Cohen 1175b037c29aSEli Cohen mlx5_ib_dbg(dev, "allocated uar %d\n", bfregi->sys_pages[i]); 1176b037c29aSEli Cohen } 1177b037c29aSEli Cohen return 0; 1178b037c29aSEli Cohen 1179b037c29aSEli Cohen error: 1180b037c29aSEli Cohen for (--i; i >= 0; i--) 1181b037c29aSEli Cohen if (mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i])) 1182b037c29aSEli Cohen mlx5_ib_warn(dev, "failed to free uar %d\n", i); 1183b037c29aSEli Cohen 1184b037c29aSEli Cohen return err; 1185b037c29aSEli Cohen } 1186b037c29aSEli Cohen 1187b037c29aSEli Cohen static int deallocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context) 1188b037c29aSEli Cohen { 1189b037c29aSEli Cohen struct mlx5_bfreg_info *bfregi; 1190b037c29aSEli Cohen int err; 1191b037c29aSEli Cohen int i; 1192b037c29aSEli Cohen 1193b037c29aSEli Cohen bfregi = &context->bfregi; 1194b037c29aSEli Cohen for (i = 0; i < bfregi->num_sys_pages; i++) { 1195b037c29aSEli Cohen err = mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]); 1196b037c29aSEli Cohen if (err) { 1197b037c29aSEli Cohen mlx5_ib_warn(dev, "failed to free uar %d\n", i); 1198b037c29aSEli Cohen return err; 1199b037c29aSEli Cohen } 1200b037c29aSEli Cohen } 1201b037c29aSEli Cohen return 0; 1202b037c29aSEli Cohen } 1203b037c29aSEli Cohen 1204e126ba97SEli Cohen static struct ib_ucontext *mlx5_ib_alloc_ucontext(struct ib_device *ibdev, 1205e126ba97SEli Cohen struct ib_udata *udata) 1206e126ba97SEli Cohen { 1207e126ba97SEli Cohen struct mlx5_ib_dev *dev = to_mdev(ibdev); 1208b368d7cbSMatan Barak struct mlx5_ib_alloc_ucontext_req_v2 req = {}; 1209b368d7cbSMatan Barak struct mlx5_ib_alloc_ucontext_resp resp = {}; 1210e126ba97SEli Cohen struct mlx5_ib_ucontext *context; 12112f5ff264SEli Cohen struct mlx5_bfreg_info *bfregi; 121278c0f98cSEli Cohen int ver; 1213e126ba97SEli Cohen int err; 1214f241e749SJack Morgenstein size_t reqlen; 1215a168a41cSMajd Dibbiny size_t min_req_v2 = offsetof(struct mlx5_ib_alloc_ucontext_req_v2, 1216a168a41cSMajd Dibbiny max_cqe_version); 1217b037c29aSEli Cohen bool lib_uar_4k; 1218e126ba97SEli Cohen 1219e126ba97SEli Cohen if (!dev->ib_active) 1220e126ba97SEli Cohen return ERR_PTR(-EAGAIN); 1221e126ba97SEli Cohen 1222dfbee859SHaggai Abramovsky if (udata->inlen < sizeof(struct ib_uverbs_cmd_hdr)) 1223dfbee859SHaggai Abramovsky return ERR_PTR(-EINVAL); 1224dfbee859SHaggai Abramovsky 122578c0f98cSEli Cohen reqlen = udata->inlen - sizeof(struct ib_uverbs_cmd_hdr); 122678c0f98cSEli Cohen if (reqlen == sizeof(struct mlx5_ib_alloc_ucontext_req)) 122778c0f98cSEli Cohen ver = 0; 1228a168a41cSMajd Dibbiny else if (reqlen >= min_req_v2) 122978c0f98cSEli Cohen ver = 2; 123078c0f98cSEli Cohen else 123178c0f98cSEli Cohen return ERR_PTR(-EINVAL); 123278c0f98cSEli Cohen 1233b368d7cbSMatan Barak err = ib_copy_from_udata(&req, udata, min(reqlen, sizeof(req))); 1234e126ba97SEli Cohen if (err) 1235e126ba97SEli Cohen return ERR_PTR(err); 1236e126ba97SEli Cohen 1237b368d7cbSMatan Barak if (req.flags) 123878c0f98cSEli Cohen return ERR_PTR(-EINVAL); 123978c0f98cSEli Cohen 1240f72300c5SHaggai Abramovsky if (req.comp_mask || req.reserved0 || req.reserved1 || req.reserved2) 1241b368d7cbSMatan Barak return ERR_PTR(-EOPNOTSUPP); 1242b368d7cbSMatan Barak 12432f5ff264SEli Cohen req.total_num_bfregs = ALIGN(req.total_num_bfregs, 12442f5ff264SEli Cohen MLX5_NON_FP_BFREGS_PER_UAR); 12452f5ff264SEli Cohen if (req.num_low_latency_bfregs > req.total_num_bfregs - 1) 1246e126ba97SEli Cohen return ERR_PTR(-EINVAL); 1247e126ba97SEli Cohen 1248938fe83cSSaeed Mahameed resp.qp_tab_size = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp); 12492cc6ad5fSNoa Osherovich if (mlx5_core_is_pf(dev->mdev) && MLX5_CAP_GEN(dev->mdev, bf)) 1250938fe83cSSaeed Mahameed resp.bf_reg_size = 1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size); 1251b47bd6eaSDaniel Jurgens resp.cache_line_size = cache_line_size(); 1252938fe83cSSaeed Mahameed resp.max_sq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq); 1253938fe83cSSaeed Mahameed resp.max_rq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq); 1254938fe83cSSaeed Mahameed resp.max_send_wqebb = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz); 1255938fe83cSSaeed Mahameed resp.max_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz); 1256938fe83cSSaeed Mahameed resp.max_srq_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz); 1257f72300c5SHaggai Abramovsky resp.cqe_version = min_t(__u8, 1258f72300c5SHaggai Abramovsky (__u8)MLX5_CAP_GEN(dev->mdev, cqe_version), 1259f72300c5SHaggai Abramovsky req.max_cqe_version); 126030aa60b3SEli Cohen resp.log_uar_size = MLX5_CAP_GEN(dev->mdev, uar_4k) ? 126130aa60b3SEli Cohen MLX5_ADAPTER_PAGE_SHIFT : PAGE_SHIFT; 126230aa60b3SEli Cohen resp.num_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ? 126330aa60b3SEli Cohen MLX5_CAP_GEN(dev->mdev, num_of_uars_per_page) : 1; 1264b368d7cbSMatan Barak resp.response_length = min(offsetof(typeof(resp), response_length) + 1265b368d7cbSMatan Barak sizeof(resp.response_length), udata->outlen); 1266e126ba97SEli Cohen 1267e126ba97SEli Cohen context = kzalloc(sizeof(*context), GFP_KERNEL); 1268e126ba97SEli Cohen if (!context) 1269e126ba97SEli Cohen return ERR_PTR(-ENOMEM); 1270e126ba97SEli Cohen 127130aa60b3SEli Cohen lib_uar_4k = req.lib_caps & MLX5_LIB_CAP_4K_UAR; 12722f5ff264SEli Cohen bfregi = &context->bfregi; 1273b037c29aSEli Cohen 1274b037c29aSEli Cohen /* updates req->total_num_bfregs */ 1275b037c29aSEli Cohen err = calc_total_bfregs(dev, lib_uar_4k, &req, &bfregi->num_sys_pages); 1276b037c29aSEli Cohen if (err) 1277b037c29aSEli Cohen goto out_ctx; 1278b037c29aSEli Cohen 12792f5ff264SEli Cohen mutex_init(&bfregi->lock); 1280b037c29aSEli Cohen bfregi->lib_uar_4k = lib_uar_4k; 1281b037c29aSEli Cohen bfregi->count = kcalloc(req.total_num_bfregs, sizeof(*bfregi->count), 1282b037c29aSEli Cohen GFP_KERNEL); 1283b037c29aSEli Cohen if (!bfregi->count) { 1284e126ba97SEli Cohen err = -ENOMEM; 1285e126ba97SEli Cohen goto out_ctx; 1286e126ba97SEli Cohen } 1287e126ba97SEli Cohen 1288b037c29aSEli Cohen bfregi->sys_pages = kcalloc(bfregi->num_sys_pages, 1289b037c29aSEli Cohen sizeof(*bfregi->sys_pages), 1290e126ba97SEli Cohen GFP_KERNEL); 1291b037c29aSEli Cohen if (!bfregi->sys_pages) { 1292e126ba97SEli Cohen err = -ENOMEM; 1293e126ba97SEli Cohen goto out_count; 1294e126ba97SEli Cohen } 1295e126ba97SEli Cohen 1296b037c29aSEli Cohen err = allocate_uars(dev, context); 1297b037c29aSEli Cohen if (err) 1298b037c29aSEli Cohen goto out_sys_pages; 1299b037c29aSEli Cohen 1300b4cfe447SHaggai Eran #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING 1301b4cfe447SHaggai Eran context->ibucontext.invalidate_range = &mlx5_ib_invalidate_range; 1302b4cfe447SHaggai Eran #endif 1303b4cfe447SHaggai Eran 13047d0cc6edSArtemy Kovalyov context->upd_xlt_page = __get_free_page(GFP_KERNEL); 13057d0cc6edSArtemy Kovalyov if (!context->upd_xlt_page) { 13067d0cc6edSArtemy Kovalyov err = -ENOMEM; 13077d0cc6edSArtemy Kovalyov goto out_uars; 13087d0cc6edSArtemy Kovalyov } 13097d0cc6edSArtemy Kovalyov mutex_init(&context->upd_xlt_page_mutex); 13107d0cc6edSArtemy Kovalyov 1311146d2f1aSmajd@mellanox.com if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain)) { 1312146d2f1aSmajd@mellanox.com err = mlx5_core_alloc_transport_domain(dev->mdev, 1313146d2f1aSmajd@mellanox.com &context->tdn); 1314146d2f1aSmajd@mellanox.com if (err) 13157d0cc6edSArtemy Kovalyov goto out_page; 1316146d2f1aSmajd@mellanox.com } 1317146d2f1aSmajd@mellanox.com 13187c2344c3SMaor Gottlieb INIT_LIST_HEAD(&context->vma_private_list); 1319e126ba97SEli Cohen INIT_LIST_HEAD(&context->db_page_list); 1320e126ba97SEli Cohen mutex_init(&context->db_page_mutex); 1321e126ba97SEli Cohen 13222f5ff264SEli Cohen resp.tot_bfregs = req.total_num_bfregs; 1323938fe83cSSaeed Mahameed resp.num_ports = MLX5_CAP_GEN(dev->mdev, num_ports); 1324b368d7cbSMatan Barak 1325f72300c5SHaggai Abramovsky if (field_avail(typeof(resp), cqe_version, udata->outlen)) 1326f72300c5SHaggai Abramovsky resp.response_length += sizeof(resp.cqe_version); 1327b368d7cbSMatan Barak 1328402ca536SBodong Wang if (field_avail(typeof(resp), cmds_supp_uhw, udata->outlen)) { 13296ad279c5SMoni Shoua resp.cmds_supp_uhw |= MLX5_USER_CMDS_SUPP_UHW_QUERY_DEVICE | 13306ad279c5SMoni Shoua MLX5_USER_CMDS_SUPP_UHW_CREATE_AH; 1331402ca536SBodong Wang resp.response_length += sizeof(resp.cmds_supp_uhw); 1332402ca536SBodong Wang } 1333402ca536SBodong Wang 133478984898SOr Gerlitz if (field_avail(typeof(resp), eth_min_inline, udata->outlen)) { 133578984898SOr Gerlitz if (mlx5_ib_port_link_layer(ibdev, 1) == IB_LINK_LAYER_ETHERNET) { 133678984898SOr Gerlitz mlx5_query_min_inline(dev->mdev, &resp.eth_min_inline); 133778984898SOr Gerlitz resp.eth_min_inline++; 133878984898SOr Gerlitz } 133978984898SOr Gerlitz resp.response_length += sizeof(resp.eth_min_inline); 134078984898SOr Gerlitz } 134178984898SOr Gerlitz 1342bc5c6eedSNoa Osherovich /* 1343bc5c6eedSNoa Osherovich * We don't want to expose information from the PCI bar that is located 1344bc5c6eedSNoa Osherovich * after 4096 bytes, so if the arch only supports larger pages, let's 1345bc5c6eedSNoa Osherovich * pretend we don't support reading the HCA's core clock. This is also 1346bc5c6eedSNoa Osherovich * forced by mmap function. 1347bc5c6eedSNoa Osherovich */ 1348de8d6e02SEli Cohen if (field_avail(typeof(resp), hca_core_clock_offset, udata->outlen)) { 1349de8d6e02SEli Cohen if (PAGE_SIZE <= 4096) { 1350b368d7cbSMatan Barak resp.comp_mask |= 1351b368d7cbSMatan Barak MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_CORE_CLOCK_OFFSET; 1352b368d7cbSMatan Barak resp.hca_core_clock_offset = 1353de8d6e02SEli Cohen offsetof(struct mlx5_init_seg, internal_timer_h) % PAGE_SIZE; 1354de8d6e02SEli Cohen } 1355f72300c5SHaggai Abramovsky resp.response_length += sizeof(resp.hca_core_clock_offset) + 1356402ca536SBodong Wang sizeof(resp.reserved2); 1357b368d7cbSMatan Barak } 1358b368d7cbSMatan Barak 135930aa60b3SEli Cohen if (field_avail(typeof(resp), log_uar_size, udata->outlen)) 136030aa60b3SEli Cohen resp.response_length += sizeof(resp.log_uar_size); 136130aa60b3SEli Cohen 136230aa60b3SEli Cohen if (field_avail(typeof(resp), num_uars_per_page, udata->outlen)) 136330aa60b3SEli Cohen resp.response_length += sizeof(resp.num_uars_per_page); 136430aa60b3SEli Cohen 1365b368d7cbSMatan Barak err = ib_copy_to_udata(udata, &resp, resp.response_length); 1366e126ba97SEli Cohen if (err) 1367146d2f1aSmajd@mellanox.com goto out_td; 1368e126ba97SEli Cohen 13692f5ff264SEli Cohen bfregi->ver = ver; 13702f5ff264SEli Cohen bfregi->num_low_latency_bfregs = req.num_low_latency_bfregs; 1371f72300c5SHaggai Abramovsky context->cqe_version = resp.cqe_version; 137230aa60b3SEli Cohen context->lib_caps = req.lib_caps; 137330aa60b3SEli Cohen print_lib_caps(dev, context->lib_caps); 1374f72300c5SHaggai Abramovsky 1375e126ba97SEli Cohen return &context->ibucontext; 1376e126ba97SEli Cohen 1377146d2f1aSmajd@mellanox.com out_td: 1378146d2f1aSmajd@mellanox.com if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain)) 1379146d2f1aSmajd@mellanox.com mlx5_core_dealloc_transport_domain(dev->mdev, context->tdn); 1380146d2f1aSmajd@mellanox.com 13817d0cc6edSArtemy Kovalyov out_page: 13827d0cc6edSArtemy Kovalyov free_page(context->upd_xlt_page); 13837d0cc6edSArtemy Kovalyov 1384e126ba97SEli Cohen out_uars: 1385b037c29aSEli Cohen deallocate_uars(dev, context); 1386b037c29aSEli Cohen 1387b037c29aSEli Cohen out_sys_pages: 1388b037c29aSEli Cohen kfree(bfregi->sys_pages); 1389b037c29aSEli Cohen 1390e126ba97SEli Cohen out_count: 13912f5ff264SEli Cohen kfree(bfregi->count); 1392e126ba97SEli Cohen 1393e126ba97SEli Cohen out_ctx: 1394e126ba97SEli Cohen kfree(context); 1395b037c29aSEli Cohen 1396e126ba97SEli Cohen return ERR_PTR(err); 1397e126ba97SEli Cohen } 1398e126ba97SEli Cohen 1399e126ba97SEli Cohen static int mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext) 1400e126ba97SEli Cohen { 1401e126ba97SEli Cohen struct mlx5_ib_ucontext *context = to_mucontext(ibcontext); 1402e126ba97SEli Cohen struct mlx5_ib_dev *dev = to_mdev(ibcontext->device); 1403b037c29aSEli Cohen struct mlx5_bfreg_info *bfregi; 1404e126ba97SEli Cohen 1405b037c29aSEli Cohen bfregi = &context->bfregi; 1406146d2f1aSmajd@mellanox.com if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain)) 1407146d2f1aSmajd@mellanox.com mlx5_core_dealloc_transport_domain(dev->mdev, context->tdn); 1408146d2f1aSmajd@mellanox.com 14097d0cc6edSArtemy Kovalyov free_page(context->upd_xlt_page); 1410b037c29aSEli Cohen deallocate_uars(dev, context); 1411b037c29aSEli Cohen kfree(bfregi->sys_pages); 14122f5ff264SEli Cohen kfree(bfregi->count); 1413e126ba97SEli Cohen kfree(context); 1414e126ba97SEli Cohen 1415e126ba97SEli Cohen return 0; 1416e126ba97SEli Cohen } 1417e126ba97SEli Cohen 1418b037c29aSEli Cohen static phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev, 1419b037c29aSEli Cohen struct mlx5_bfreg_info *bfregi, 1420b037c29aSEli Cohen int idx) 1421e126ba97SEli Cohen { 1422b037c29aSEli Cohen int fw_uars_per_page; 1423b037c29aSEli Cohen 1424b037c29aSEli Cohen fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ? MLX5_UARS_IN_PAGE : 1; 1425b037c29aSEli Cohen 1426b037c29aSEli Cohen return (pci_resource_start(dev->mdev->pdev, 0) >> PAGE_SHIFT) + 1427b037c29aSEli Cohen bfregi->sys_pages[idx] / fw_uars_per_page; 1428e126ba97SEli Cohen } 1429e126ba97SEli Cohen 1430e126ba97SEli Cohen static int get_command(unsigned long offset) 1431e126ba97SEli Cohen { 1432e126ba97SEli Cohen return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK; 1433e126ba97SEli Cohen } 1434e126ba97SEli Cohen 1435e126ba97SEli Cohen static int get_arg(unsigned long offset) 1436e126ba97SEli Cohen { 1437e126ba97SEli Cohen return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1); 1438e126ba97SEli Cohen } 1439e126ba97SEli Cohen 1440e126ba97SEli Cohen static int get_index(unsigned long offset) 1441e126ba97SEli Cohen { 1442e126ba97SEli Cohen return get_arg(offset); 1443e126ba97SEli Cohen } 1444e126ba97SEli Cohen 14457c2344c3SMaor Gottlieb static void mlx5_ib_vma_open(struct vm_area_struct *area) 14467c2344c3SMaor Gottlieb { 14477c2344c3SMaor Gottlieb /* vma_open is called when a new VMA is created on top of our VMA. This 14487c2344c3SMaor Gottlieb * is done through either mremap flow or split_vma (usually due to 14497c2344c3SMaor Gottlieb * mlock, madvise, munmap, etc.) We do not support a clone of the VMA, 14507c2344c3SMaor Gottlieb * as this VMA is strongly hardware related. Therefore we set the 14517c2344c3SMaor Gottlieb * vm_ops of the newly created/cloned VMA to NULL, to prevent it from 14527c2344c3SMaor Gottlieb * calling us again and trying to do incorrect actions. We assume that 14537c2344c3SMaor Gottlieb * the original VMA size is exactly a single page, and therefore all 14547c2344c3SMaor Gottlieb * "splitting" operation will not happen to it. 14557c2344c3SMaor Gottlieb */ 14567c2344c3SMaor Gottlieb area->vm_ops = NULL; 14577c2344c3SMaor Gottlieb } 14587c2344c3SMaor Gottlieb 14597c2344c3SMaor Gottlieb static void mlx5_ib_vma_close(struct vm_area_struct *area) 14607c2344c3SMaor Gottlieb { 14617c2344c3SMaor Gottlieb struct mlx5_ib_vma_private_data *mlx5_ib_vma_priv_data; 14627c2344c3SMaor Gottlieb 14637c2344c3SMaor Gottlieb /* It's guaranteed that all VMAs opened on a FD are closed before the 14647c2344c3SMaor Gottlieb * file itself is closed, therefore no sync is needed with the regular 14657c2344c3SMaor Gottlieb * closing flow. (e.g. mlx5 ib_dealloc_ucontext) 14667c2344c3SMaor Gottlieb * However need a sync with accessing the vma as part of 14677c2344c3SMaor Gottlieb * mlx5_ib_disassociate_ucontext. 14687c2344c3SMaor Gottlieb * The close operation is usually called under mm->mmap_sem except when 14697c2344c3SMaor Gottlieb * process is exiting. 14707c2344c3SMaor Gottlieb * The exiting case is handled explicitly as part of 14717c2344c3SMaor Gottlieb * mlx5_ib_disassociate_ucontext. 14727c2344c3SMaor Gottlieb */ 14737c2344c3SMaor Gottlieb mlx5_ib_vma_priv_data = (struct mlx5_ib_vma_private_data *)area->vm_private_data; 14747c2344c3SMaor Gottlieb 14757c2344c3SMaor Gottlieb /* setting the vma context pointer to null in the mlx5_ib driver's 14767c2344c3SMaor Gottlieb * private data, to protect a race condition in 14777c2344c3SMaor Gottlieb * mlx5_ib_disassociate_ucontext(). 14787c2344c3SMaor Gottlieb */ 14797c2344c3SMaor Gottlieb mlx5_ib_vma_priv_data->vma = NULL; 14807c2344c3SMaor Gottlieb list_del(&mlx5_ib_vma_priv_data->list); 14817c2344c3SMaor Gottlieb kfree(mlx5_ib_vma_priv_data); 14827c2344c3SMaor Gottlieb } 14837c2344c3SMaor Gottlieb 14847c2344c3SMaor Gottlieb static const struct vm_operations_struct mlx5_ib_vm_ops = { 14857c2344c3SMaor Gottlieb .open = mlx5_ib_vma_open, 14867c2344c3SMaor Gottlieb .close = mlx5_ib_vma_close 14877c2344c3SMaor Gottlieb }; 14887c2344c3SMaor Gottlieb 14897c2344c3SMaor Gottlieb static int mlx5_ib_set_vma_data(struct vm_area_struct *vma, 14907c2344c3SMaor Gottlieb struct mlx5_ib_ucontext *ctx) 14917c2344c3SMaor Gottlieb { 14927c2344c3SMaor Gottlieb struct mlx5_ib_vma_private_data *vma_prv; 14937c2344c3SMaor Gottlieb struct list_head *vma_head = &ctx->vma_private_list; 14947c2344c3SMaor Gottlieb 14957c2344c3SMaor Gottlieb vma_prv = kzalloc(sizeof(*vma_prv), GFP_KERNEL); 14967c2344c3SMaor Gottlieb if (!vma_prv) 14977c2344c3SMaor Gottlieb return -ENOMEM; 14987c2344c3SMaor Gottlieb 14997c2344c3SMaor Gottlieb vma_prv->vma = vma; 15007c2344c3SMaor Gottlieb vma->vm_private_data = vma_prv; 15017c2344c3SMaor Gottlieb vma->vm_ops = &mlx5_ib_vm_ops; 15027c2344c3SMaor Gottlieb 15037c2344c3SMaor Gottlieb list_add(&vma_prv->list, vma_head); 15047c2344c3SMaor Gottlieb 15057c2344c3SMaor Gottlieb return 0; 15067c2344c3SMaor Gottlieb } 15077c2344c3SMaor Gottlieb 15087c2344c3SMaor Gottlieb static void mlx5_ib_disassociate_ucontext(struct ib_ucontext *ibcontext) 15097c2344c3SMaor Gottlieb { 15107c2344c3SMaor Gottlieb int ret; 15117c2344c3SMaor Gottlieb struct vm_area_struct *vma; 15127c2344c3SMaor Gottlieb struct mlx5_ib_vma_private_data *vma_private, *n; 15137c2344c3SMaor Gottlieb struct mlx5_ib_ucontext *context = to_mucontext(ibcontext); 15147c2344c3SMaor Gottlieb struct task_struct *owning_process = NULL; 15157c2344c3SMaor Gottlieb struct mm_struct *owning_mm = NULL; 15167c2344c3SMaor Gottlieb 15177c2344c3SMaor Gottlieb owning_process = get_pid_task(ibcontext->tgid, PIDTYPE_PID); 15187c2344c3SMaor Gottlieb if (!owning_process) 15197c2344c3SMaor Gottlieb return; 15207c2344c3SMaor Gottlieb 15217c2344c3SMaor Gottlieb owning_mm = get_task_mm(owning_process); 15227c2344c3SMaor Gottlieb if (!owning_mm) { 15237c2344c3SMaor Gottlieb pr_info("no mm, disassociate ucontext is pending task termination\n"); 15247c2344c3SMaor Gottlieb while (1) { 15257c2344c3SMaor Gottlieb put_task_struct(owning_process); 15267c2344c3SMaor Gottlieb usleep_range(1000, 2000); 15277c2344c3SMaor Gottlieb owning_process = get_pid_task(ibcontext->tgid, 15287c2344c3SMaor Gottlieb PIDTYPE_PID); 15297c2344c3SMaor Gottlieb if (!owning_process || 15307c2344c3SMaor Gottlieb owning_process->state == TASK_DEAD) { 15317c2344c3SMaor Gottlieb pr_info("disassociate ucontext done, task was terminated\n"); 15327c2344c3SMaor Gottlieb /* in case task was dead need to release the 15337c2344c3SMaor Gottlieb * task struct. 15347c2344c3SMaor Gottlieb */ 15357c2344c3SMaor Gottlieb if (owning_process) 15367c2344c3SMaor Gottlieb put_task_struct(owning_process); 15377c2344c3SMaor Gottlieb return; 15387c2344c3SMaor Gottlieb } 15397c2344c3SMaor Gottlieb } 15407c2344c3SMaor Gottlieb } 15417c2344c3SMaor Gottlieb 15427c2344c3SMaor Gottlieb /* need to protect from a race on closing the vma as part of 15437c2344c3SMaor Gottlieb * mlx5_ib_vma_close. 15447c2344c3SMaor Gottlieb */ 1545ecc7d83bSMaor Gottlieb down_write(&owning_mm->mmap_sem); 15467c2344c3SMaor Gottlieb list_for_each_entry_safe(vma_private, n, &context->vma_private_list, 15477c2344c3SMaor Gottlieb list) { 15487c2344c3SMaor Gottlieb vma = vma_private->vma; 15497c2344c3SMaor Gottlieb ret = zap_vma_ptes(vma, vma->vm_start, 15507c2344c3SMaor Gottlieb PAGE_SIZE); 15517c2344c3SMaor Gottlieb WARN_ONCE(ret, "%s: zap_vma_ptes failed", __func__); 15527c2344c3SMaor Gottlieb /* context going to be destroyed, should 15537c2344c3SMaor Gottlieb * not access ops any more. 15547c2344c3SMaor Gottlieb */ 155513776612SMaor Gottlieb vma->vm_flags &= ~(VM_SHARED | VM_MAYSHARE); 15567c2344c3SMaor Gottlieb vma->vm_ops = NULL; 15577c2344c3SMaor Gottlieb list_del(&vma_private->list); 15587c2344c3SMaor Gottlieb kfree(vma_private); 15597c2344c3SMaor Gottlieb } 1560ecc7d83bSMaor Gottlieb up_write(&owning_mm->mmap_sem); 15617c2344c3SMaor Gottlieb mmput(owning_mm); 15627c2344c3SMaor Gottlieb put_task_struct(owning_process); 15637c2344c3SMaor Gottlieb } 15647c2344c3SMaor Gottlieb 156537aa5c36SGuy Levi static inline char *mmap_cmd2str(enum mlx5_ib_mmap_cmd cmd) 1566e126ba97SEli Cohen { 156737aa5c36SGuy Levi switch (cmd) { 156837aa5c36SGuy Levi case MLX5_IB_MMAP_WC_PAGE: 156937aa5c36SGuy Levi return "WC"; 1570e126ba97SEli Cohen case MLX5_IB_MMAP_REGULAR_PAGE: 157137aa5c36SGuy Levi return "best effort WC"; 157237aa5c36SGuy Levi case MLX5_IB_MMAP_NC_PAGE: 157337aa5c36SGuy Levi return "NC"; 157437aa5c36SGuy Levi default: 157537aa5c36SGuy Levi return NULL; 157637aa5c36SGuy Levi } 157737aa5c36SGuy Levi } 157837aa5c36SGuy Levi 157937aa5c36SGuy Levi static int uar_mmap(struct mlx5_ib_dev *dev, enum mlx5_ib_mmap_cmd cmd, 15807c2344c3SMaor Gottlieb struct vm_area_struct *vma, 15817c2344c3SMaor Gottlieb struct mlx5_ib_ucontext *context) 158237aa5c36SGuy Levi { 15832f5ff264SEli Cohen struct mlx5_bfreg_info *bfregi = &context->bfregi; 158437aa5c36SGuy Levi int err; 158537aa5c36SGuy Levi unsigned long idx; 158637aa5c36SGuy Levi phys_addr_t pfn, pa; 158737aa5c36SGuy Levi pgprot_t prot; 1588b037c29aSEli Cohen int uars_per_page; 1589b037c29aSEli Cohen 1590b037c29aSEli Cohen if (vma->vm_end - vma->vm_start != PAGE_SIZE) 1591b037c29aSEli Cohen return -EINVAL; 1592b037c29aSEli Cohen 1593b037c29aSEli Cohen uars_per_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k); 1594b037c29aSEli Cohen idx = get_index(vma->vm_pgoff); 1595b037c29aSEli Cohen if (idx % uars_per_page || 1596b037c29aSEli Cohen idx * uars_per_page >= bfregi->num_sys_pages) { 1597b037c29aSEli Cohen mlx5_ib_warn(dev, "invalid uar index %lu\n", idx); 1598b037c29aSEli Cohen return -EINVAL; 1599b037c29aSEli Cohen } 160037aa5c36SGuy Levi 160137aa5c36SGuy Levi switch (cmd) { 160237aa5c36SGuy Levi case MLX5_IB_MMAP_WC_PAGE: 160337aa5c36SGuy Levi /* Some architectures don't support WC memory */ 160437aa5c36SGuy Levi #if defined(CONFIG_X86) 160537aa5c36SGuy Levi if (!pat_enabled()) 160637aa5c36SGuy Levi return -EPERM; 160737aa5c36SGuy Levi #elif !(defined(CONFIG_PPC) || (defined(CONFIG_ARM) && defined(CONFIG_MMU))) 160837aa5c36SGuy Levi return -EPERM; 160937aa5c36SGuy Levi #endif 161037aa5c36SGuy Levi /* fall through */ 161137aa5c36SGuy Levi case MLX5_IB_MMAP_REGULAR_PAGE: 161237aa5c36SGuy Levi /* For MLX5_IB_MMAP_REGULAR_PAGE do the best effort to get WC */ 161337aa5c36SGuy Levi prot = pgprot_writecombine(vma->vm_page_prot); 161437aa5c36SGuy Levi break; 161537aa5c36SGuy Levi case MLX5_IB_MMAP_NC_PAGE: 161637aa5c36SGuy Levi prot = pgprot_noncached(vma->vm_page_prot); 161737aa5c36SGuy Levi break; 161837aa5c36SGuy Levi default: 161937aa5c36SGuy Levi return -EINVAL; 162037aa5c36SGuy Levi } 162137aa5c36SGuy Levi 1622b037c29aSEli Cohen pfn = uar_index2pfn(dev, bfregi, idx); 162337aa5c36SGuy Levi mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn %pa\n", idx, &pfn); 1624e126ba97SEli Cohen 162537aa5c36SGuy Levi vma->vm_page_prot = prot; 162637aa5c36SGuy Levi err = io_remap_pfn_range(vma, vma->vm_start, pfn, 162737aa5c36SGuy Levi PAGE_SIZE, vma->vm_page_prot); 162837aa5c36SGuy Levi if (err) { 162937aa5c36SGuy Levi mlx5_ib_err(dev, "io_remap_pfn_range failed with error=%d, vm_start=0x%lx, pfn=%pa, mmap_cmd=%s\n", 163037aa5c36SGuy Levi err, vma->vm_start, &pfn, mmap_cmd2str(cmd)); 1631e126ba97SEli Cohen return -EAGAIN; 163237aa5c36SGuy Levi } 1633e126ba97SEli Cohen 163437aa5c36SGuy Levi pa = pfn << PAGE_SHIFT; 163537aa5c36SGuy Levi mlx5_ib_dbg(dev, "mapped %s at 0x%lx, PA %pa\n", mmap_cmd2str(cmd), 163637aa5c36SGuy Levi vma->vm_start, &pa); 163737aa5c36SGuy Levi 16387c2344c3SMaor Gottlieb return mlx5_ib_set_vma_data(vma, context); 163937aa5c36SGuy Levi } 164037aa5c36SGuy Levi 164137aa5c36SGuy Levi static int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma) 164237aa5c36SGuy Levi { 164337aa5c36SGuy Levi struct mlx5_ib_ucontext *context = to_mucontext(ibcontext); 164437aa5c36SGuy Levi struct mlx5_ib_dev *dev = to_mdev(ibcontext->device); 164537aa5c36SGuy Levi unsigned long command; 164637aa5c36SGuy Levi phys_addr_t pfn; 164737aa5c36SGuy Levi 164837aa5c36SGuy Levi command = get_command(vma->vm_pgoff); 164937aa5c36SGuy Levi switch (command) { 165037aa5c36SGuy Levi case MLX5_IB_MMAP_WC_PAGE: 165137aa5c36SGuy Levi case MLX5_IB_MMAP_NC_PAGE: 165237aa5c36SGuy Levi case MLX5_IB_MMAP_REGULAR_PAGE: 16537c2344c3SMaor Gottlieb return uar_mmap(dev, command, vma, context); 1654e126ba97SEli Cohen 1655e126ba97SEli Cohen case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES: 1656e126ba97SEli Cohen return -ENOSYS; 1657e126ba97SEli Cohen 1658d69e3bcfSMatan Barak case MLX5_IB_MMAP_CORE_CLOCK: 1659d69e3bcfSMatan Barak if (vma->vm_end - vma->vm_start != PAGE_SIZE) 1660d69e3bcfSMatan Barak return -EINVAL; 1661d69e3bcfSMatan Barak 16626cbac1e4SMatan Barak if (vma->vm_flags & VM_WRITE) 1663d69e3bcfSMatan Barak return -EPERM; 1664d69e3bcfSMatan Barak 1665d69e3bcfSMatan Barak /* Don't expose to user-space information it shouldn't have */ 1666d69e3bcfSMatan Barak if (PAGE_SIZE > 4096) 1667d69e3bcfSMatan Barak return -EOPNOTSUPP; 1668d69e3bcfSMatan Barak 1669d69e3bcfSMatan Barak vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); 1670d69e3bcfSMatan Barak pfn = (dev->mdev->iseg_base + 1671d69e3bcfSMatan Barak offsetof(struct mlx5_init_seg, internal_timer_h)) >> 1672d69e3bcfSMatan Barak PAGE_SHIFT; 1673d69e3bcfSMatan Barak if (io_remap_pfn_range(vma, vma->vm_start, pfn, 1674d69e3bcfSMatan Barak PAGE_SIZE, vma->vm_page_prot)) 1675d69e3bcfSMatan Barak return -EAGAIN; 1676d69e3bcfSMatan Barak 1677d69e3bcfSMatan Barak mlx5_ib_dbg(dev, "mapped internal timer at 0x%lx, PA 0x%llx\n", 1678d69e3bcfSMatan Barak vma->vm_start, 1679d69e3bcfSMatan Barak (unsigned long long)pfn << PAGE_SHIFT); 1680d69e3bcfSMatan Barak break; 1681d69e3bcfSMatan Barak 1682e126ba97SEli Cohen default: 1683e126ba97SEli Cohen return -EINVAL; 1684e126ba97SEli Cohen } 1685e126ba97SEli Cohen 1686e126ba97SEli Cohen return 0; 1687e126ba97SEli Cohen } 1688e126ba97SEli Cohen 1689e126ba97SEli Cohen static struct ib_pd *mlx5_ib_alloc_pd(struct ib_device *ibdev, 1690e126ba97SEli Cohen struct ib_ucontext *context, 1691e126ba97SEli Cohen struct ib_udata *udata) 1692e126ba97SEli Cohen { 1693e126ba97SEli Cohen struct mlx5_ib_alloc_pd_resp resp; 1694e126ba97SEli Cohen struct mlx5_ib_pd *pd; 1695e126ba97SEli Cohen int err; 1696e126ba97SEli Cohen 1697e126ba97SEli Cohen pd = kmalloc(sizeof(*pd), GFP_KERNEL); 1698e126ba97SEli Cohen if (!pd) 1699e126ba97SEli Cohen return ERR_PTR(-ENOMEM); 1700e126ba97SEli Cohen 17019603b61dSJack Morgenstein err = mlx5_core_alloc_pd(to_mdev(ibdev)->mdev, &pd->pdn); 1702e126ba97SEli Cohen if (err) { 1703e126ba97SEli Cohen kfree(pd); 1704e126ba97SEli Cohen return ERR_PTR(err); 1705e126ba97SEli Cohen } 1706e126ba97SEli Cohen 1707e126ba97SEli Cohen if (context) { 1708e126ba97SEli Cohen resp.pdn = pd->pdn; 1709e126ba97SEli Cohen if (ib_copy_to_udata(udata, &resp, sizeof(resp))) { 17109603b61dSJack Morgenstein mlx5_core_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn); 1711e126ba97SEli Cohen kfree(pd); 1712e126ba97SEli Cohen return ERR_PTR(-EFAULT); 1713e126ba97SEli Cohen } 1714e126ba97SEli Cohen } 1715e126ba97SEli Cohen 1716e126ba97SEli Cohen return &pd->ibpd; 1717e126ba97SEli Cohen } 1718e126ba97SEli Cohen 1719e126ba97SEli Cohen static int mlx5_ib_dealloc_pd(struct ib_pd *pd) 1720e126ba97SEli Cohen { 1721e126ba97SEli Cohen struct mlx5_ib_dev *mdev = to_mdev(pd->device); 1722e126ba97SEli Cohen struct mlx5_ib_pd *mpd = to_mpd(pd); 1723e126ba97SEli Cohen 17249603b61dSJack Morgenstein mlx5_core_dealloc_pd(mdev->mdev, mpd->pdn); 1725e126ba97SEli Cohen kfree(mpd); 1726e126ba97SEli Cohen 1727e126ba97SEli Cohen return 0; 1728e126ba97SEli Cohen } 1729e126ba97SEli Cohen 1730466fa6d2SMaor Gottlieb enum { 1731466fa6d2SMaor Gottlieb MATCH_CRITERIA_ENABLE_OUTER_BIT, 1732466fa6d2SMaor Gottlieb MATCH_CRITERIA_ENABLE_MISC_BIT, 1733466fa6d2SMaor Gottlieb MATCH_CRITERIA_ENABLE_INNER_BIT 1734466fa6d2SMaor Gottlieb }; 1735038d2ef8SMaor Gottlieb 1736466fa6d2SMaor Gottlieb #define HEADER_IS_ZERO(match_criteria, headers) \ 1737466fa6d2SMaor Gottlieb !(memchr_inv(MLX5_ADDR_OF(fte_match_param, match_criteria, headers), \ 1738466fa6d2SMaor Gottlieb 0, MLX5_FLD_SZ_BYTES(fte_match_param, headers))) \ 1739466fa6d2SMaor Gottlieb 1740466fa6d2SMaor Gottlieb static u8 get_match_criteria_enable(u32 *match_criteria) 1741466fa6d2SMaor Gottlieb { 1742466fa6d2SMaor Gottlieb u8 match_criteria_enable; 1743466fa6d2SMaor Gottlieb 1744466fa6d2SMaor Gottlieb match_criteria_enable = 1745466fa6d2SMaor Gottlieb (!HEADER_IS_ZERO(match_criteria, outer_headers)) << 1746466fa6d2SMaor Gottlieb MATCH_CRITERIA_ENABLE_OUTER_BIT; 1747466fa6d2SMaor Gottlieb match_criteria_enable |= 1748466fa6d2SMaor Gottlieb (!HEADER_IS_ZERO(match_criteria, misc_parameters)) << 1749466fa6d2SMaor Gottlieb MATCH_CRITERIA_ENABLE_MISC_BIT; 1750466fa6d2SMaor Gottlieb match_criteria_enable |= 1751466fa6d2SMaor Gottlieb (!HEADER_IS_ZERO(match_criteria, inner_headers)) << 1752466fa6d2SMaor Gottlieb MATCH_CRITERIA_ENABLE_INNER_BIT; 1753466fa6d2SMaor Gottlieb 1754466fa6d2SMaor Gottlieb return match_criteria_enable; 1755038d2ef8SMaor Gottlieb } 1756038d2ef8SMaor Gottlieb 1757ca0d4753SMaor Gottlieb static void set_proto(void *outer_c, void *outer_v, u8 mask, u8 val) 1758ca0d4753SMaor Gottlieb { 1759ca0d4753SMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_protocol, mask); 1760ca0d4753SMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_protocol, val); 1761ca0d4753SMaor Gottlieb } 1762ca0d4753SMaor Gottlieb 17632d1e697eSMoses Reuben static void set_flow_label(void *misc_c, void *misc_v, u8 mask, u8 val, 17642d1e697eSMoses Reuben bool inner) 17652d1e697eSMoses Reuben { 17662d1e697eSMoses Reuben if (inner) { 17672d1e697eSMoses Reuben MLX5_SET(fte_match_set_misc, 17682d1e697eSMoses Reuben misc_c, inner_ipv6_flow_label, mask); 17692d1e697eSMoses Reuben MLX5_SET(fte_match_set_misc, 17702d1e697eSMoses Reuben misc_v, inner_ipv6_flow_label, val); 17712d1e697eSMoses Reuben } else { 17722d1e697eSMoses Reuben MLX5_SET(fte_match_set_misc, 17732d1e697eSMoses Reuben misc_c, outer_ipv6_flow_label, mask); 17742d1e697eSMoses Reuben MLX5_SET(fte_match_set_misc, 17752d1e697eSMoses Reuben misc_v, outer_ipv6_flow_label, val); 17762d1e697eSMoses Reuben } 17772d1e697eSMoses Reuben } 17782d1e697eSMoses Reuben 1779ca0d4753SMaor Gottlieb static void set_tos(void *outer_c, void *outer_v, u8 mask, u8 val) 1780ca0d4753SMaor Gottlieb { 1781ca0d4753SMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_ecn, mask); 1782ca0d4753SMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_ecn, val); 1783ca0d4753SMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_dscp, mask >> 2); 1784ca0d4753SMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_dscp, val >> 2); 1785ca0d4753SMaor Gottlieb } 1786ca0d4753SMaor Gottlieb 1787c47ac6aeSMaor Gottlieb #define LAST_ETH_FIELD vlan_tag 1788c47ac6aeSMaor Gottlieb #define LAST_IB_FIELD sl 1789ca0d4753SMaor Gottlieb #define LAST_IPV4_FIELD tos 1790466fa6d2SMaor Gottlieb #define LAST_IPV6_FIELD traffic_class 1791c47ac6aeSMaor Gottlieb #define LAST_TCP_UDP_FIELD src_port 1792ffb30d8fSMoses Reuben #define LAST_TUNNEL_FIELD tunnel_id 17932ac693f9SMoses Reuben #define LAST_FLOW_TAG_FIELD tag_id 1794a22ed86cSSlava Shwartsman #define LAST_DROP_FIELD size 1795c47ac6aeSMaor Gottlieb 1796c47ac6aeSMaor Gottlieb /* Field is the last supported field */ 1797c47ac6aeSMaor Gottlieb #define FIELDS_NOT_SUPPORTED(filter, field)\ 1798c47ac6aeSMaor Gottlieb memchr_inv((void *)&filter.field +\ 1799c47ac6aeSMaor Gottlieb sizeof(filter.field), 0,\ 1800c47ac6aeSMaor Gottlieb sizeof(filter) -\ 1801c47ac6aeSMaor Gottlieb offsetof(typeof(filter), field) -\ 1802c47ac6aeSMaor Gottlieb sizeof(filter.field)) 1803c47ac6aeSMaor Gottlieb 180419cc7524SAriel Levkovich #define IPV4_VERSION 4 180519cc7524SAriel Levkovich #define IPV6_VERSION 6 180619cc7524SAriel Levkovich static int parse_flow_attr(struct mlx5_core_dev *mdev, u32 *match_c, 180719cc7524SAriel Levkovich u32 *match_v, const union ib_flow_spec *ib_spec, 1808a22ed86cSSlava Shwartsman u32 *tag_id, bool *is_drop) 1809038d2ef8SMaor Gottlieb { 1810466fa6d2SMaor Gottlieb void *misc_params_c = MLX5_ADDR_OF(fte_match_param, match_c, 1811466fa6d2SMaor Gottlieb misc_parameters); 1812466fa6d2SMaor Gottlieb void *misc_params_v = MLX5_ADDR_OF(fte_match_param, match_v, 1813466fa6d2SMaor Gottlieb misc_parameters); 18142d1e697eSMoses Reuben void *headers_c; 18152d1e697eSMoses Reuben void *headers_v; 181619cc7524SAriel Levkovich int match_ipv; 1817466fa6d2SMaor Gottlieb 18182d1e697eSMoses Reuben if (ib_spec->type & IB_FLOW_SPEC_INNER) { 18192d1e697eSMoses Reuben headers_c = MLX5_ADDR_OF(fte_match_param, match_c, 18202d1e697eSMoses Reuben inner_headers); 18212d1e697eSMoses Reuben headers_v = MLX5_ADDR_OF(fte_match_param, match_v, 18222d1e697eSMoses Reuben inner_headers); 182319cc7524SAriel Levkovich match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev, 182419cc7524SAriel Levkovich ft_field_support.inner_ip_version); 18252d1e697eSMoses Reuben } else { 18262d1e697eSMoses Reuben headers_c = MLX5_ADDR_OF(fte_match_param, match_c, 18272d1e697eSMoses Reuben outer_headers); 18282d1e697eSMoses Reuben headers_v = MLX5_ADDR_OF(fte_match_param, match_v, 18292d1e697eSMoses Reuben outer_headers); 183019cc7524SAriel Levkovich match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev, 183119cc7524SAriel Levkovich ft_field_support.outer_ip_version); 18322d1e697eSMoses Reuben } 18332d1e697eSMoses Reuben 18342d1e697eSMoses Reuben switch (ib_spec->type & ~IB_FLOW_SPEC_INNER) { 1835038d2ef8SMaor Gottlieb case IB_FLOW_SPEC_ETH: 1836c47ac6aeSMaor Gottlieb if (FIELDS_NOT_SUPPORTED(ib_spec->eth.mask, LAST_ETH_FIELD)) 18371ffd3a26SLeon Romanovsky return -EOPNOTSUPP; 1838038d2ef8SMaor Gottlieb 18392d1e697eSMoses Reuben ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c, 1840038d2ef8SMaor Gottlieb dmac_47_16), 1841038d2ef8SMaor Gottlieb ib_spec->eth.mask.dst_mac); 18422d1e697eSMoses Reuben ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v, 1843038d2ef8SMaor Gottlieb dmac_47_16), 1844038d2ef8SMaor Gottlieb ib_spec->eth.val.dst_mac); 1845038d2ef8SMaor Gottlieb 18462d1e697eSMoses Reuben ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c, 1847ee3da804SMaor Gottlieb smac_47_16), 1848ee3da804SMaor Gottlieb ib_spec->eth.mask.src_mac); 18492d1e697eSMoses Reuben ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v, 1850ee3da804SMaor Gottlieb smac_47_16), 1851ee3da804SMaor Gottlieb ib_spec->eth.val.src_mac); 1852ee3da804SMaor Gottlieb 1853038d2ef8SMaor Gottlieb if (ib_spec->eth.mask.vlan_tag) { 18542d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_c, 185510543365SMohamad Haj Yahia cvlan_tag, 1); 18562d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_v, 185710543365SMohamad Haj Yahia cvlan_tag, 1); 1858038d2ef8SMaor Gottlieb 18592d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_c, 1860038d2ef8SMaor Gottlieb first_vid, ntohs(ib_spec->eth.mask.vlan_tag)); 18612d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_v, 1862038d2ef8SMaor Gottlieb first_vid, ntohs(ib_spec->eth.val.vlan_tag)); 1863038d2ef8SMaor Gottlieb 18642d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_c, 1865038d2ef8SMaor Gottlieb first_cfi, 1866038d2ef8SMaor Gottlieb ntohs(ib_spec->eth.mask.vlan_tag) >> 12); 18672d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_v, 1868038d2ef8SMaor Gottlieb first_cfi, 1869038d2ef8SMaor Gottlieb ntohs(ib_spec->eth.val.vlan_tag) >> 12); 1870038d2ef8SMaor Gottlieb 18712d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_c, 1872038d2ef8SMaor Gottlieb first_prio, 1873038d2ef8SMaor Gottlieb ntohs(ib_spec->eth.mask.vlan_tag) >> 13); 18742d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_v, 1875038d2ef8SMaor Gottlieb first_prio, 1876038d2ef8SMaor Gottlieb ntohs(ib_spec->eth.val.vlan_tag) >> 13); 1877038d2ef8SMaor Gottlieb } 18782d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_c, 1879038d2ef8SMaor Gottlieb ethertype, ntohs(ib_spec->eth.mask.ether_type)); 18802d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_v, 1881038d2ef8SMaor Gottlieb ethertype, ntohs(ib_spec->eth.val.ether_type)); 1882038d2ef8SMaor Gottlieb break; 1883038d2ef8SMaor Gottlieb case IB_FLOW_SPEC_IPV4: 1884c47ac6aeSMaor Gottlieb if (FIELDS_NOT_SUPPORTED(ib_spec->ipv4.mask, LAST_IPV4_FIELD)) 18851ffd3a26SLeon Romanovsky return -EOPNOTSUPP; 1886038d2ef8SMaor Gottlieb 188719cc7524SAriel Levkovich if (match_ipv) { 188819cc7524SAriel Levkovich MLX5_SET(fte_match_set_lyr_2_4, headers_c, 188919cc7524SAriel Levkovich ip_version, 0xf); 189019cc7524SAriel Levkovich MLX5_SET(fte_match_set_lyr_2_4, headers_v, 189119cc7524SAriel Levkovich ip_version, IPV4_VERSION); 189219cc7524SAriel Levkovich } else { 18932d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_c, 1894038d2ef8SMaor Gottlieb ethertype, 0xffff); 18952d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_v, 1896038d2ef8SMaor Gottlieb ethertype, ETH_P_IP); 189719cc7524SAriel Levkovich } 1898038d2ef8SMaor Gottlieb 18992d1e697eSMoses Reuben memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c, 1900038d2ef8SMaor Gottlieb src_ipv4_src_ipv6.ipv4_layout.ipv4), 1901038d2ef8SMaor Gottlieb &ib_spec->ipv4.mask.src_ip, 1902038d2ef8SMaor Gottlieb sizeof(ib_spec->ipv4.mask.src_ip)); 19032d1e697eSMoses Reuben memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v, 1904038d2ef8SMaor Gottlieb src_ipv4_src_ipv6.ipv4_layout.ipv4), 1905038d2ef8SMaor Gottlieb &ib_spec->ipv4.val.src_ip, 1906038d2ef8SMaor Gottlieb sizeof(ib_spec->ipv4.val.src_ip)); 19072d1e697eSMoses Reuben memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c, 1908038d2ef8SMaor Gottlieb dst_ipv4_dst_ipv6.ipv4_layout.ipv4), 1909038d2ef8SMaor Gottlieb &ib_spec->ipv4.mask.dst_ip, 1910038d2ef8SMaor Gottlieb sizeof(ib_spec->ipv4.mask.dst_ip)); 19112d1e697eSMoses Reuben memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v, 1912038d2ef8SMaor Gottlieb dst_ipv4_dst_ipv6.ipv4_layout.ipv4), 1913038d2ef8SMaor Gottlieb &ib_spec->ipv4.val.dst_ip, 1914038d2ef8SMaor Gottlieb sizeof(ib_spec->ipv4.val.dst_ip)); 1915ca0d4753SMaor Gottlieb 19162d1e697eSMoses Reuben set_tos(headers_c, headers_v, 1917ca0d4753SMaor Gottlieb ib_spec->ipv4.mask.tos, ib_spec->ipv4.val.tos); 1918ca0d4753SMaor Gottlieb 19192d1e697eSMoses Reuben set_proto(headers_c, headers_v, 1920ca0d4753SMaor Gottlieb ib_spec->ipv4.mask.proto, ib_spec->ipv4.val.proto); 1921038d2ef8SMaor Gottlieb break; 1922026bae0cSMaor Gottlieb case IB_FLOW_SPEC_IPV6: 1923c47ac6aeSMaor Gottlieb if (FIELDS_NOT_SUPPORTED(ib_spec->ipv6.mask, LAST_IPV6_FIELD)) 19241ffd3a26SLeon Romanovsky return -EOPNOTSUPP; 1925026bae0cSMaor Gottlieb 192619cc7524SAriel Levkovich if (match_ipv) { 192719cc7524SAriel Levkovich MLX5_SET(fte_match_set_lyr_2_4, headers_c, 192819cc7524SAriel Levkovich ip_version, 0xf); 192919cc7524SAriel Levkovich MLX5_SET(fte_match_set_lyr_2_4, headers_v, 193019cc7524SAriel Levkovich ip_version, IPV6_VERSION); 193119cc7524SAriel Levkovich } else { 19322d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_c, 1933026bae0cSMaor Gottlieb ethertype, 0xffff); 19342d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_v, 1935026bae0cSMaor Gottlieb ethertype, ETH_P_IPV6); 193619cc7524SAriel Levkovich } 1937026bae0cSMaor Gottlieb 19382d1e697eSMoses Reuben memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c, 1939026bae0cSMaor Gottlieb src_ipv4_src_ipv6.ipv6_layout.ipv6), 1940026bae0cSMaor Gottlieb &ib_spec->ipv6.mask.src_ip, 1941026bae0cSMaor Gottlieb sizeof(ib_spec->ipv6.mask.src_ip)); 19422d1e697eSMoses Reuben memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v, 1943026bae0cSMaor Gottlieb src_ipv4_src_ipv6.ipv6_layout.ipv6), 1944026bae0cSMaor Gottlieb &ib_spec->ipv6.val.src_ip, 1945026bae0cSMaor Gottlieb sizeof(ib_spec->ipv6.val.src_ip)); 19462d1e697eSMoses Reuben memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c, 1947026bae0cSMaor Gottlieb dst_ipv4_dst_ipv6.ipv6_layout.ipv6), 1948026bae0cSMaor Gottlieb &ib_spec->ipv6.mask.dst_ip, 1949026bae0cSMaor Gottlieb sizeof(ib_spec->ipv6.mask.dst_ip)); 19502d1e697eSMoses Reuben memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v, 1951026bae0cSMaor Gottlieb dst_ipv4_dst_ipv6.ipv6_layout.ipv6), 1952026bae0cSMaor Gottlieb &ib_spec->ipv6.val.dst_ip, 1953026bae0cSMaor Gottlieb sizeof(ib_spec->ipv6.val.dst_ip)); 1954466fa6d2SMaor Gottlieb 19552d1e697eSMoses Reuben set_tos(headers_c, headers_v, 1956466fa6d2SMaor Gottlieb ib_spec->ipv6.mask.traffic_class, 1957466fa6d2SMaor Gottlieb ib_spec->ipv6.val.traffic_class); 1958466fa6d2SMaor Gottlieb 19592d1e697eSMoses Reuben set_proto(headers_c, headers_v, 1960466fa6d2SMaor Gottlieb ib_spec->ipv6.mask.next_hdr, 1961466fa6d2SMaor Gottlieb ib_spec->ipv6.val.next_hdr); 1962466fa6d2SMaor Gottlieb 19632d1e697eSMoses Reuben set_flow_label(misc_params_c, misc_params_v, 19642d1e697eSMoses Reuben ntohl(ib_spec->ipv6.mask.flow_label), 19652d1e697eSMoses Reuben ntohl(ib_spec->ipv6.val.flow_label), 19662d1e697eSMoses Reuben ib_spec->type & IB_FLOW_SPEC_INNER); 19672d1e697eSMoses Reuben 1968026bae0cSMaor Gottlieb break; 1969038d2ef8SMaor Gottlieb case IB_FLOW_SPEC_TCP: 1970c47ac6aeSMaor Gottlieb if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask, 1971c47ac6aeSMaor Gottlieb LAST_TCP_UDP_FIELD)) 19721ffd3a26SLeon Romanovsky return -EOPNOTSUPP; 1973038d2ef8SMaor Gottlieb 19742d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol, 1975038d2ef8SMaor Gottlieb 0xff); 19762d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol, 1977038d2ef8SMaor Gottlieb IPPROTO_TCP); 1978038d2ef8SMaor Gottlieb 19792d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_sport, 1980038d2ef8SMaor Gottlieb ntohs(ib_spec->tcp_udp.mask.src_port)); 19812d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_sport, 1982038d2ef8SMaor Gottlieb ntohs(ib_spec->tcp_udp.val.src_port)); 1983038d2ef8SMaor Gottlieb 19842d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_dport, 1985038d2ef8SMaor Gottlieb ntohs(ib_spec->tcp_udp.mask.dst_port)); 19862d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_dport, 1987038d2ef8SMaor Gottlieb ntohs(ib_spec->tcp_udp.val.dst_port)); 1988038d2ef8SMaor Gottlieb break; 1989038d2ef8SMaor Gottlieb case IB_FLOW_SPEC_UDP: 1990c47ac6aeSMaor Gottlieb if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask, 1991c47ac6aeSMaor Gottlieb LAST_TCP_UDP_FIELD)) 19921ffd3a26SLeon Romanovsky return -EOPNOTSUPP; 1993038d2ef8SMaor Gottlieb 19942d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol, 1995038d2ef8SMaor Gottlieb 0xff); 19962d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol, 1997038d2ef8SMaor Gottlieb IPPROTO_UDP); 1998038d2ef8SMaor Gottlieb 19992d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_sport, 2000038d2ef8SMaor Gottlieb ntohs(ib_spec->tcp_udp.mask.src_port)); 20012d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_sport, 2002038d2ef8SMaor Gottlieb ntohs(ib_spec->tcp_udp.val.src_port)); 2003038d2ef8SMaor Gottlieb 20042d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_dport, 2005038d2ef8SMaor Gottlieb ntohs(ib_spec->tcp_udp.mask.dst_port)); 20062d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport, 2007038d2ef8SMaor Gottlieb ntohs(ib_spec->tcp_udp.val.dst_port)); 2008038d2ef8SMaor Gottlieb break; 2009ffb30d8fSMoses Reuben case IB_FLOW_SPEC_VXLAN_TUNNEL: 2010ffb30d8fSMoses Reuben if (FIELDS_NOT_SUPPORTED(ib_spec->tunnel.mask, 2011ffb30d8fSMoses Reuben LAST_TUNNEL_FIELD)) 20121ffd3a26SLeon Romanovsky return -EOPNOTSUPP; 2013ffb30d8fSMoses Reuben 2014ffb30d8fSMoses Reuben MLX5_SET(fte_match_set_misc, misc_params_c, vxlan_vni, 2015ffb30d8fSMoses Reuben ntohl(ib_spec->tunnel.mask.tunnel_id)); 2016ffb30d8fSMoses Reuben MLX5_SET(fte_match_set_misc, misc_params_v, vxlan_vni, 2017ffb30d8fSMoses Reuben ntohl(ib_spec->tunnel.val.tunnel_id)); 2018ffb30d8fSMoses Reuben break; 20192ac693f9SMoses Reuben case IB_FLOW_SPEC_ACTION_TAG: 20202ac693f9SMoses Reuben if (FIELDS_NOT_SUPPORTED(ib_spec->flow_tag, 20212ac693f9SMoses Reuben LAST_FLOW_TAG_FIELD)) 20222ac693f9SMoses Reuben return -EOPNOTSUPP; 20232ac693f9SMoses Reuben if (ib_spec->flow_tag.tag_id >= BIT(24)) 20242ac693f9SMoses Reuben return -EINVAL; 20252ac693f9SMoses Reuben 20262ac693f9SMoses Reuben *tag_id = ib_spec->flow_tag.tag_id; 20272ac693f9SMoses Reuben break; 2028a22ed86cSSlava Shwartsman case IB_FLOW_SPEC_ACTION_DROP: 2029a22ed86cSSlava Shwartsman if (FIELDS_NOT_SUPPORTED(ib_spec->drop, 2030a22ed86cSSlava Shwartsman LAST_DROP_FIELD)) 2031a22ed86cSSlava Shwartsman return -EOPNOTSUPP; 2032a22ed86cSSlava Shwartsman *is_drop = true; 2033a22ed86cSSlava Shwartsman break; 2034038d2ef8SMaor Gottlieb default: 2035038d2ef8SMaor Gottlieb return -EINVAL; 2036038d2ef8SMaor Gottlieb } 2037038d2ef8SMaor Gottlieb 2038038d2ef8SMaor Gottlieb return 0; 2039038d2ef8SMaor Gottlieb } 2040038d2ef8SMaor Gottlieb 2041038d2ef8SMaor Gottlieb /* If a flow could catch both multicast and unicast packets, 2042038d2ef8SMaor Gottlieb * it won't fall into the multicast flow steering table and this rule 2043038d2ef8SMaor Gottlieb * could steal other multicast packets. 2044038d2ef8SMaor Gottlieb */ 2045038d2ef8SMaor Gottlieb static bool flow_is_multicast_only(struct ib_flow_attr *ib_attr) 2046038d2ef8SMaor Gottlieb { 2047038d2ef8SMaor Gottlieb struct ib_flow_spec_eth *eth_spec; 2048038d2ef8SMaor Gottlieb 2049038d2ef8SMaor Gottlieb if (ib_attr->type != IB_FLOW_ATTR_NORMAL || 2050038d2ef8SMaor Gottlieb ib_attr->size < sizeof(struct ib_flow_attr) + 2051038d2ef8SMaor Gottlieb sizeof(struct ib_flow_spec_eth) || 2052038d2ef8SMaor Gottlieb ib_attr->num_of_specs < 1) 2053038d2ef8SMaor Gottlieb return false; 2054038d2ef8SMaor Gottlieb 2055038d2ef8SMaor Gottlieb eth_spec = (struct ib_flow_spec_eth *)(ib_attr + 1); 2056038d2ef8SMaor Gottlieb if (eth_spec->type != IB_FLOW_SPEC_ETH || 2057038d2ef8SMaor Gottlieb eth_spec->size != sizeof(*eth_spec)) 2058038d2ef8SMaor Gottlieb return false; 2059038d2ef8SMaor Gottlieb 2060038d2ef8SMaor Gottlieb return is_multicast_ether_addr(eth_spec->mask.dst_mac) && 2061038d2ef8SMaor Gottlieb is_multicast_ether_addr(eth_spec->val.dst_mac); 2062038d2ef8SMaor Gottlieb } 2063038d2ef8SMaor Gottlieb 206419cc7524SAriel Levkovich static bool is_valid_ethertype(struct mlx5_core_dev *mdev, 206519cc7524SAriel Levkovich const struct ib_flow_attr *flow_attr, 20660f750966SAriel Levkovich bool check_inner) 2067038d2ef8SMaor Gottlieb { 2068038d2ef8SMaor Gottlieb union ib_flow_spec *ib_spec = (union ib_flow_spec *)(flow_attr + 1); 206919cc7524SAriel Levkovich int match_ipv = check_inner ? 207019cc7524SAriel Levkovich MLX5_CAP_FLOWTABLE_NIC_RX(mdev, 207119cc7524SAriel Levkovich ft_field_support.inner_ip_version) : 207219cc7524SAriel Levkovich MLX5_CAP_FLOWTABLE_NIC_RX(mdev, 207319cc7524SAriel Levkovich ft_field_support.outer_ip_version); 20740f750966SAriel Levkovich int inner_bit = check_inner ? IB_FLOW_SPEC_INNER : 0; 20750f750966SAriel Levkovich bool ipv4_spec_valid, ipv6_spec_valid; 20760f750966SAriel Levkovich unsigned int ip_spec_type = 0; 20770f750966SAriel Levkovich bool has_ethertype = false; 2078038d2ef8SMaor Gottlieb unsigned int spec_index; 20790f750966SAriel Levkovich bool mask_valid = true; 20800f750966SAriel Levkovich u16 eth_type = 0; 20810f750966SAriel Levkovich bool type_valid; 2082038d2ef8SMaor Gottlieb 2083038d2ef8SMaor Gottlieb /* Validate that ethertype is correct */ 2084038d2ef8SMaor Gottlieb for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) { 20850f750966SAriel Levkovich if ((ib_spec->type == (IB_FLOW_SPEC_ETH | inner_bit)) && 2086038d2ef8SMaor Gottlieb ib_spec->eth.mask.ether_type) { 20870f750966SAriel Levkovich mask_valid = (ib_spec->eth.mask.ether_type == 20880f750966SAriel Levkovich htons(0xffff)); 20890f750966SAriel Levkovich has_ethertype = true; 20900f750966SAriel Levkovich eth_type = ntohs(ib_spec->eth.val.ether_type); 20910f750966SAriel Levkovich } else if ((ib_spec->type == (IB_FLOW_SPEC_IPV4 | inner_bit)) || 20920f750966SAriel Levkovich (ib_spec->type == (IB_FLOW_SPEC_IPV6 | inner_bit))) { 20930f750966SAriel Levkovich ip_spec_type = ib_spec->type; 2094038d2ef8SMaor Gottlieb } 2095038d2ef8SMaor Gottlieb ib_spec = (void *)ib_spec + ib_spec->size; 2096038d2ef8SMaor Gottlieb } 20970f750966SAriel Levkovich 20980f750966SAriel Levkovich type_valid = (!has_ethertype) || (!ip_spec_type); 20990f750966SAriel Levkovich if (!type_valid && mask_valid) { 21000f750966SAriel Levkovich ipv4_spec_valid = (eth_type == ETH_P_IP) && 21010f750966SAriel Levkovich (ip_spec_type == (IB_FLOW_SPEC_IPV4 | inner_bit)); 21020f750966SAriel Levkovich ipv6_spec_valid = (eth_type == ETH_P_IPV6) && 21030f750966SAriel Levkovich (ip_spec_type == (IB_FLOW_SPEC_IPV6 | inner_bit)); 210419cc7524SAriel Levkovich 210519cc7524SAriel Levkovich type_valid = (ipv4_spec_valid) || (ipv6_spec_valid) || 210619cc7524SAriel Levkovich (((eth_type == ETH_P_MPLS_UC) || 210719cc7524SAriel Levkovich (eth_type == ETH_P_MPLS_MC)) && match_ipv); 21080f750966SAriel Levkovich } 21090f750966SAriel Levkovich 21100f750966SAriel Levkovich return type_valid; 21110f750966SAriel Levkovich } 21120f750966SAriel Levkovich 211319cc7524SAriel Levkovich static bool is_valid_attr(struct mlx5_core_dev *mdev, 211419cc7524SAriel Levkovich const struct ib_flow_attr *flow_attr) 21150f750966SAriel Levkovich { 211619cc7524SAriel Levkovich return is_valid_ethertype(mdev, flow_attr, false) && 211719cc7524SAriel Levkovich is_valid_ethertype(mdev, flow_attr, true); 2118038d2ef8SMaor Gottlieb } 2119038d2ef8SMaor Gottlieb 2120038d2ef8SMaor Gottlieb static void put_flow_table(struct mlx5_ib_dev *dev, 2121038d2ef8SMaor Gottlieb struct mlx5_ib_flow_prio *prio, bool ft_added) 2122038d2ef8SMaor Gottlieb { 2123038d2ef8SMaor Gottlieb prio->refcount -= !!ft_added; 2124038d2ef8SMaor Gottlieb if (!prio->refcount) { 2125038d2ef8SMaor Gottlieb mlx5_destroy_flow_table(prio->flow_table); 2126038d2ef8SMaor Gottlieb prio->flow_table = NULL; 2127038d2ef8SMaor Gottlieb } 2128038d2ef8SMaor Gottlieb } 2129038d2ef8SMaor Gottlieb 2130038d2ef8SMaor Gottlieb static int mlx5_ib_destroy_flow(struct ib_flow *flow_id) 2131038d2ef8SMaor Gottlieb { 2132038d2ef8SMaor Gottlieb struct mlx5_ib_dev *dev = to_mdev(flow_id->qp->device); 2133038d2ef8SMaor Gottlieb struct mlx5_ib_flow_handler *handler = container_of(flow_id, 2134038d2ef8SMaor Gottlieb struct mlx5_ib_flow_handler, 2135038d2ef8SMaor Gottlieb ibflow); 2136038d2ef8SMaor Gottlieb struct mlx5_ib_flow_handler *iter, *tmp; 2137038d2ef8SMaor Gottlieb 2138038d2ef8SMaor Gottlieb mutex_lock(&dev->flow_db.lock); 2139038d2ef8SMaor Gottlieb 2140038d2ef8SMaor Gottlieb list_for_each_entry_safe(iter, tmp, &handler->list, list) { 214174491de9SMark Bloch mlx5_del_flow_rules(iter->rule); 2142cc0e5d42SMaor Gottlieb put_flow_table(dev, iter->prio, true); 2143038d2ef8SMaor Gottlieb list_del(&iter->list); 2144038d2ef8SMaor Gottlieb kfree(iter); 2145038d2ef8SMaor Gottlieb } 2146038d2ef8SMaor Gottlieb 214774491de9SMark Bloch mlx5_del_flow_rules(handler->rule); 21485497adc6SMaor Gottlieb put_flow_table(dev, handler->prio, true); 2149038d2ef8SMaor Gottlieb mutex_unlock(&dev->flow_db.lock); 2150038d2ef8SMaor Gottlieb 2151038d2ef8SMaor Gottlieb kfree(handler); 2152038d2ef8SMaor Gottlieb 2153038d2ef8SMaor Gottlieb return 0; 2154038d2ef8SMaor Gottlieb } 2155038d2ef8SMaor Gottlieb 215635d19011SMaor Gottlieb static int ib_prio_to_core_prio(unsigned int priority, bool dont_trap) 215735d19011SMaor Gottlieb { 215835d19011SMaor Gottlieb priority *= 2; 215935d19011SMaor Gottlieb if (!dont_trap) 216035d19011SMaor Gottlieb priority++; 216135d19011SMaor Gottlieb return priority; 216235d19011SMaor Gottlieb } 216335d19011SMaor Gottlieb 2164cc0e5d42SMaor Gottlieb enum flow_table_type { 2165cc0e5d42SMaor Gottlieb MLX5_IB_FT_RX, 2166cc0e5d42SMaor Gottlieb MLX5_IB_FT_TX 2167cc0e5d42SMaor Gottlieb }; 2168cc0e5d42SMaor Gottlieb 216900b7c2abSMaor Gottlieb #define MLX5_FS_MAX_TYPES 6 217000b7c2abSMaor Gottlieb #define MLX5_FS_MAX_ENTRIES BIT(16) 2171038d2ef8SMaor Gottlieb static struct mlx5_ib_flow_prio *get_flow_table(struct mlx5_ib_dev *dev, 2172cc0e5d42SMaor Gottlieb struct ib_flow_attr *flow_attr, 2173cc0e5d42SMaor Gottlieb enum flow_table_type ft_type) 2174038d2ef8SMaor Gottlieb { 217535d19011SMaor Gottlieb bool dont_trap = flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP; 2176038d2ef8SMaor Gottlieb struct mlx5_flow_namespace *ns = NULL; 2177038d2ef8SMaor Gottlieb struct mlx5_ib_flow_prio *prio; 2178038d2ef8SMaor Gottlieb struct mlx5_flow_table *ft; 2179dac388efSMaor Gottlieb int max_table_size; 2180038d2ef8SMaor Gottlieb int num_entries; 2181038d2ef8SMaor Gottlieb int num_groups; 2182038d2ef8SMaor Gottlieb int priority; 2183038d2ef8SMaor Gottlieb int err = 0; 2184038d2ef8SMaor Gottlieb 2185dac388efSMaor Gottlieb max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, 2186dac388efSMaor Gottlieb log_max_ft_size)); 2187038d2ef8SMaor Gottlieb if (flow_attr->type == IB_FLOW_ATTR_NORMAL) { 218835d19011SMaor Gottlieb if (flow_is_multicast_only(flow_attr) && 218935d19011SMaor Gottlieb !dont_trap) 2190038d2ef8SMaor Gottlieb priority = MLX5_IB_FLOW_MCAST_PRIO; 2191038d2ef8SMaor Gottlieb else 219235d19011SMaor Gottlieb priority = ib_prio_to_core_prio(flow_attr->priority, 219335d19011SMaor Gottlieb dont_trap); 2194038d2ef8SMaor Gottlieb ns = mlx5_get_flow_namespace(dev->mdev, 2195038d2ef8SMaor Gottlieb MLX5_FLOW_NAMESPACE_BYPASS); 2196038d2ef8SMaor Gottlieb num_entries = MLX5_FS_MAX_ENTRIES; 2197038d2ef8SMaor Gottlieb num_groups = MLX5_FS_MAX_TYPES; 2198038d2ef8SMaor Gottlieb prio = &dev->flow_db.prios[priority]; 2199038d2ef8SMaor Gottlieb } else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT || 2200038d2ef8SMaor Gottlieb flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) { 2201038d2ef8SMaor Gottlieb ns = mlx5_get_flow_namespace(dev->mdev, 2202038d2ef8SMaor Gottlieb MLX5_FLOW_NAMESPACE_LEFTOVERS); 2203038d2ef8SMaor Gottlieb build_leftovers_ft_param(&priority, 2204038d2ef8SMaor Gottlieb &num_entries, 2205038d2ef8SMaor Gottlieb &num_groups); 2206038d2ef8SMaor Gottlieb prio = &dev->flow_db.prios[MLX5_IB_FLOW_LEFTOVERS_PRIO]; 2207cc0e5d42SMaor Gottlieb } else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) { 2208cc0e5d42SMaor Gottlieb if (!MLX5_CAP_FLOWTABLE(dev->mdev, 2209cc0e5d42SMaor Gottlieb allow_sniffer_and_nic_rx_shared_tir)) 2210cc0e5d42SMaor Gottlieb return ERR_PTR(-ENOTSUPP); 2211cc0e5d42SMaor Gottlieb 2212cc0e5d42SMaor Gottlieb ns = mlx5_get_flow_namespace(dev->mdev, ft_type == MLX5_IB_FT_RX ? 2213cc0e5d42SMaor Gottlieb MLX5_FLOW_NAMESPACE_SNIFFER_RX : 2214cc0e5d42SMaor Gottlieb MLX5_FLOW_NAMESPACE_SNIFFER_TX); 2215cc0e5d42SMaor Gottlieb 2216cc0e5d42SMaor Gottlieb prio = &dev->flow_db.sniffer[ft_type]; 2217cc0e5d42SMaor Gottlieb priority = 0; 2218cc0e5d42SMaor Gottlieb num_entries = 1; 2219cc0e5d42SMaor Gottlieb num_groups = 1; 2220038d2ef8SMaor Gottlieb } 2221038d2ef8SMaor Gottlieb 2222038d2ef8SMaor Gottlieb if (!ns) 2223038d2ef8SMaor Gottlieb return ERR_PTR(-ENOTSUPP); 2224038d2ef8SMaor Gottlieb 2225dac388efSMaor Gottlieb if (num_entries > max_table_size) 2226dac388efSMaor Gottlieb return ERR_PTR(-ENOMEM); 2227dac388efSMaor Gottlieb 2228038d2ef8SMaor Gottlieb ft = prio->flow_table; 2229038d2ef8SMaor Gottlieb if (!ft) { 2230038d2ef8SMaor Gottlieb ft = mlx5_create_auto_grouped_flow_table(ns, priority, 2231038d2ef8SMaor Gottlieb num_entries, 2232d63cd286SMaor Gottlieb num_groups, 2233c9f1b073SHadar Hen Zion 0, 0); 2234038d2ef8SMaor Gottlieb 2235038d2ef8SMaor Gottlieb if (!IS_ERR(ft)) { 2236038d2ef8SMaor Gottlieb prio->refcount = 0; 2237038d2ef8SMaor Gottlieb prio->flow_table = ft; 2238038d2ef8SMaor Gottlieb } else { 2239038d2ef8SMaor Gottlieb err = PTR_ERR(ft); 2240038d2ef8SMaor Gottlieb } 2241038d2ef8SMaor Gottlieb } 2242038d2ef8SMaor Gottlieb 2243038d2ef8SMaor Gottlieb return err ? ERR_PTR(err) : prio; 2244038d2ef8SMaor Gottlieb } 2245038d2ef8SMaor Gottlieb 2246038d2ef8SMaor Gottlieb static struct mlx5_ib_flow_handler *create_flow_rule(struct mlx5_ib_dev *dev, 2247038d2ef8SMaor Gottlieb struct mlx5_ib_flow_prio *ft_prio, 2248dd063d0eSMaor Gottlieb const struct ib_flow_attr *flow_attr, 2249038d2ef8SMaor Gottlieb struct mlx5_flow_destination *dst) 2250038d2ef8SMaor Gottlieb { 2251038d2ef8SMaor Gottlieb struct mlx5_flow_table *ft = ft_prio->flow_table; 2252038d2ef8SMaor Gottlieb struct mlx5_ib_flow_handler *handler; 225366958ed9SHadar Hen Zion struct mlx5_flow_act flow_act = {0}; 2254c5bb1730SMaor Gottlieb struct mlx5_flow_spec *spec; 2255a22ed86cSSlava Shwartsman struct mlx5_flow_destination *rule_dst = dst; 2256dd063d0eSMaor Gottlieb const void *ib_flow = (const void *)flow_attr + sizeof(*flow_attr); 2257038d2ef8SMaor Gottlieb unsigned int spec_index; 22582ac693f9SMoses Reuben u32 flow_tag = MLX5_FS_DEFAULT_FLOW_TAG; 2259a22ed86cSSlava Shwartsman bool is_drop = false; 2260038d2ef8SMaor Gottlieb int err = 0; 2261a22ed86cSSlava Shwartsman int dest_num = 1; 2262038d2ef8SMaor Gottlieb 226319cc7524SAriel Levkovich if (!is_valid_attr(dev->mdev, flow_attr)) 2264038d2ef8SMaor Gottlieb return ERR_PTR(-EINVAL); 2265038d2ef8SMaor Gottlieb 2266*1b9a07eeSLeon Romanovsky spec = kvzalloc(sizeof(*spec), GFP_KERNEL); 2267038d2ef8SMaor Gottlieb handler = kzalloc(sizeof(*handler), GFP_KERNEL); 2268c5bb1730SMaor Gottlieb if (!handler || !spec) { 2269038d2ef8SMaor Gottlieb err = -ENOMEM; 2270038d2ef8SMaor Gottlieb goto free; 2271038d2ef8SMaor Gottlieb } 2272038d2ef8SMaor Gottlieb 2273038d2ef8SMaor Gottlieb INIT_LIST_HEAD(&handler->list); 2274038d2ef8SMaor Gottlieb 2275038d2ef8SMaor Gottlieb for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) { 227619cc7524SAriel Levkovich err = parse_flow_attr(dev->mdev, spec->match_criteria, 2277a22ed86cSSlava Shwartsman spec->match_value, 2278a22ed86cSSlava Shwartsman ib_flow, &flow_tag, &is_drop); 2279038d2ef8SMaor Gottlieb if (err < 0) 2280038d2ef8SMaor Gottlieb goto free; 2281038d2ef8SMaor Gottlieb 2282038d2ef8SMaor Gottlieb ib_flow += ((union ib_flow_spec *)ib_flow)->size; 2283038d2ef8SMaor Gottlieb } 2284038d2ef8SMaor Gottlieb 2285466fa6d2SMaor Gottlieb spec->match_criteria_enable = get_match_criteria_enable(spec->match_criteria); 2286a22ed86cSSlava Shwartsman if (is_drop) { 2287a22ed86cSSlava Shwartsman flow_act.action = MLX5_FLOW_CONTEXT_ACTION_DROP; 2288a22ed86cSSlava Shwartsman rule_dst = NULL; 2289a22ed86cSSlava Shwartsman dest_num = 0; 2290a22ed86cSSlava Shwartsman } else { 229166958ed9SHadar Hen Zion flow_act.action = dst ? MLX5_FLOW_CONTEXT_ACTION_FWD_DEST : 229235d19011SMaor Gottlieb MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO; 2293a22ed86cSSlava Shwartsman } 22942ac693f9SMoses Reuben 22952ac693f9SMoses Reuben if (flow_tag != MLX5_FS_DEFAULT_FLOW_TAG && 22962ac693f9SMoses Reuben (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT || 22972ac693f9SMoses Reuben flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT)) { 22982ac693f9SMoses Reuben mlx5_ib_warn(dev, "Flow tag %u and attribute type %x isn't allowed in leftovers\n", 22992ac693f9SMoses Reuben flow_tag, flow_attr->type); 23002ac693f9SMoses Reuben err = -EINVAL; 23012ac693f9SMoses Reuben goto free; 23022ac693f9SMoses Reuben } 23032ac693f9SMoses Reuben flow_act.flow_tag = flow_tag; 230474491de9SMark Bloch handler->rule = mlx5_add_flow_rules(ft, spec, 230566958ed9SHadar Hen Zion &flow_act, 2306a22ed86cSSlava Shwartsman rule_dst, dest_num); 2307038d2ef8SMaor Gottlieb 2308038d2ef8SMaor Gottlieb if (IS_ERR(handler->rule)) { 2309038d2ef8SMaor Gottlieb err = PTR_ERR(handler->rule); 2310038d2ef8SMaor Gottlieb goto free; 2311038d2ef8SMaor Gottlieb } 2312038d2ef8SMaor Gottlieb 2313d9d4980aSMaor Gottlieb ft_prio->refcount++; 23145497adc6SMaor Gottlieb handler->prio = ft_prio; 2315038d2ef8SMaor Gottlieb 2316038d2ef8SMaor Gottlieb ft_prio->flow_table = ft; 2317038d2ef8SMaor Gottlieb free: 2318038d2ef8SMaor Gottlieb if (err) 2319038d2ef8SMaor Gottlieb kfree(handler); 2320c5bb1730SMaor Gottlieb kvfree(spec); 2321038d2ef8SMaor Gottlieb return err ? ERR_PTR(err) : handler; 2322038d2ef8SMaor Gottlieb } 2323038d2ef8SMaor Gottlieb 232435d19011SMaor Gottlieb static struct mlx5_ib_flow_handler *create_dont_trap_rule(struct mlx5_ib_dev *dev, 232535d19011SMaor Gottlieb struct mlx5_ib_flow_prio *ft_prio, 232635d19011SMaor Gottlieb struct ib_flow_attr *flow_attr, 232735d19011SMaor Gottlieb struct mlx5_flow_destination *dst) 232835d19011SMaor Gottlieb { 232935d19011SMaor Gottlieb struct mlx5_ib_flow_handler *handler_dst = NULL; 233035d19011SMaor Gottlieb struct mlx5_ib_flow_handler *handler = NULL; 233135d19011SMaor Gottlieb 233235d19011SMaor Gottlieb handler = create_flow_rule(dev, ft_prio, flow_attr, NULL); 233335d19011SMaor Gottlieb if (!IS_ERR(handler)) { 233435d19011SMaor Gottlieb handler_dst = create_flow_rule(dev, ft_prio, 233535d19011SMaor Gottlieb flow_attr, dst); 233635d19011SMaor Gottlieb if (IS_ERR(handler_dst)) { 233774491de9SMark Bloch mlx5_del_flow_rules(handler->rule); 2338d9d4980aSMaor Gottlieb ft_prio->refcount--; 233935d19011SMaor Gottlieb kfree(handler); 234035d19011SMaor Gottlieb handler = handler_dst; 234135d19011SMaor Gottlieb } else { 234235d19011SMaor Gottlieb list_add(&handler_dst->list, &handler->list); 234335d19011SMaor Gottlieb } 234435d19011SMaor Gottlieb } 234535d19011SMaor Gottlieb 234635d19011SMaor Gottlieb return handler; 234735d19011SMaor Gottlieb } 2348038d2ef8SMaor Gottlieb enum { 2349038d2ef8SMaor Gottlieb LEFTOVERS_MC, 2350038d2ef8SMaor Gottlieb LEFTOVERS_UC, 2351038d2ef8SMaor Gottlieb }; 2352038d2ef8SMaor Gottlieb 2353038d2ef8SMaor Gottlieb static struct mlx5_ib_flow_handler *create_leftovers_rule(struct mlx5_ib_dev *dev, 2354038d2ef8SMaor Gottlieb struct mlx5_ib_flow_prio *ft_prio, 2355038d2ef8SMaor Gottlieb struct ib_flow_attr *flow_attr, 2356038d2ef8SMaor Gottlieb struct mlx5_flow_destination *dst) 2357038d2ef8SMaor Gottlieb { 2358038d2ef8SMaor Gottlieb struct mlx5_ib_flow_handler *handler_ucast = NULL; 2359038d2ef8SMaor Gottlieb struct mlx5_ib_flow_handler *handler = NULL; 2360038d2ef8SMaor Gottlieb 2361038d2ef8SMaor Gottlieb static struct { 2362038d2ef8SMaor Gottlieb struct ib_flow_attr flow_attr; 2363038d2ef8SMaor Gottlieb struct ib_flow_spec_eth eth_flow; 2364038d2ef8SMaor Gottlieb } leftovers_specs[] = { 2365038d2ef8SMaor Gottlieb [LEFTOVERS_MC] = { 2366038d2ef8SMaor Gottlieb .flow_attr = { 2367038d2ef8SMaor Gottlieb .num_of_specs = 1, 2368038d2ef8SMaor Gottlieb .size = sizeof(leftovers_specs[0]) 2369038d2ef8SMaor Gottlieb }, 2370038d2ef8SMaor Gottlieb .eth_flow = { 2371038d2ef8SMaor Gottlieb .type = IB_FLOW_SPEC_ETH, 2372038d2ef8SMaor Gottlieb .size = sizeof(struct ib_flow_spec_eth), 2373038d2ef8SMaor Gottlieb .mask = {.dst_mac = {0x1} }, 2374038d2ef8SMaor Gottlieb .val = {.dst_mac = {0x1} } 2375038d2ef8SMaor Gottlieb } 2376038d2ef8SMaor Gottlieb }, 2377038d2ef8SMaor Gottlieb [LEFTOVERS_UC] = { 2378038d2ef8SMaor Gottlieb .flow_attr = { 2379038d2ef8SMaor Gottlieb .num_of_specs = 1, 2380038d2ef8SMaor Gottlieb .size = sizeof(leftovers_specs[0]) 2381038d2ef8SMaor Gottlieb }, 2382038d2ef8SMaor Gottlieb .eth_flow = { 2383038d2ef8SMaor Gottlieb .type = IB_FLOW_SPEC_ETH, 2384038d2ef8SMaor Gottlieb .size = sizeof(struct ib_flow_spec_eth), 2385038d2ef8SMaor Gottlieb .mask = {.dst_mac = {0x1} }, 2386038d2ef8SMaor Gottlieb .val = {.dst_mac = {} } 2387038d2ef8SMaor Gottlieb } 2388038d2ef8SMaor Gottlieb } 2389038d2ef8SMaor Gottlieb }; 2390038d2ef8SMaor Gottlieb 2391038d2ef8SMaor Gottlieb handler = create_flow_rule(dev, ft_prio, 2392038d2ef8SMaor Gottlieb &leftovers_specs[LEFTOVERS_MC].flow_attr, 2393038d2ef8SMaor Gottlieb dst); 2394038d2ef8SMaor Gottlieb if (!IS_ERR(handler) && 2395038d2ef8SMaor Gottlieb flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT) { 2396038d2ef8SMaor Gottlieb handler_ucast = create_flow_rule(dev, ft_prio, 2397038d2ef8SMaor Gottlieb &leftovers_specs[LEFTOVERS_UC].flow_attr, 2398038d2ef8SMaor Gottlieb dst); 2399038d2ef8SMaor Gottlieb if (IS_ERR(handler_ucast)) { 240074491de9SMark Bloch mlx5_del_flow_rules(handler->rule); 2401d9d4980aSMaor Gottlieb ft_prio->refcount--; 2402038d2ef8SMaor Gottlieb kfree(handler); 2403038d2ef8SMaor Gottlieb handler = handler_ucast; 2404038d2ef8SMaor Gottlieb } else { 2405038d2ef8SMaor Gottlieb list_add(&handler_ucast->list, &handler->list); 2406038d2ef8SMaor Gottlieb } 2407038d2ef8SMaor Gottlieb } 2408038d2ef8SMaor Gottlieb 2409038d2ef8SMaor Gottlieb return handler; 2410038d2ef8SMaor Gottlieb } 2411038d2ef8SMaor Gottlieb 2412cc0e5d42SMaor Gottlieb static struct mlx5_ib_flow_handler *create_sniffer_rule(struct mlx5_ib_dev *dev, 2413cc0e5d42SMaor Gottlieb struct mlx5_ib_flow_prio *ft_rx, 2414cc0e5d42SMaor Gottlieb struct mlx5_ib_flow_prio *ft_tx, 2415cc0e5d42SMaor Gottlieb struct mlx5_flow_destination *dst) 2416cc0e5d42SMaor Gottlieb { 2417cc0e5d42SMaor Gottlieb struct mlx5_ib_flow_handler *handler_rx; 2418cc0e5d42SMaor Gottlieb struct mlx5_ib_flow_handler *handler_tx; 2419cc0e5d42SMaor Gottlieb int err; 2420cc0e5d42SMaor Gottlieb static const struct ib_flow_attr flow_attr = { 2421cc0e5d42SMaor Gottlieb .num_of_specs = 0, 2422cc0e5d42SMaor Gottlieb .size = sizeof(flow_attr) 2423cc0e5d42SMaor Gottlieb }; 2424cc0e5d42SMaor Gottlieb 2425cc0e5d42SMaor Gottlieb handler_rx = create_flow_rule(dev, ft_rx, &flow_attr, dst); 2426cc0e5d42SMaor Gottlieb if (IS_ERR(handler_rx)) { 2427cc0e5d42SMaor Gottlieb err = PTR_ERR(handler_rx); 2428cc0e5d42SMaor Gottlieb goto err; 2429cc0e5d42SMaor Gottlieb } 2430cc0e5d42SMaor Gottlieb 2431cc0e5d42SMaor Gottlieb handler_tx = create_flow_rule(dev, ft_tx, &flow_attr, dst); 2432cc0e5d42SMaor Gottlieb if (IS_ERR(handler_tx)) { 2433cc0e5d42SMaor Gottlieb err = PTR_ERR(handler_tx); 2434cc0e5d42SMaor Gottlieb goto err_tx; 2435cc0e5d42SMaor Gottlieb } 2436cc0e5d42SMaor Gottlieb 2437cc0e5d42SMaor Gottlieb list_add(&handler_tx->list, &handler_rx->list); 2438cc0e5d42SMaor Gottlieb 2439cc0e5d42SMaor Gottlieb return handler_rx; 2440cc0e5d42SMaor Gottlieb 2441cc0e5d42SMaor Gottlieb err_tx: 244274491de9SMark Bloch mlx5_del_flow_rules(handler_rx->rule); 2443cc0e5d42SMaor Gottlieb ft_rx->refcount--; 2444cc0e5d42SMaor Gottlieb kfree(handler_rx); 2445cc0e5d42SMaor Gottlieb err: 2446cc0e5d42SMaor Gottlieb return ERR_PTR(err); 2447cc0e5d42SMaor Gottlieb } 2448cc0e5d42SMaor Gottlieb 2449038d2ef8SMaor Gottlieb static struct ib_flow *mlx5_ib_create_flow(struct ib_qp *qp, 2450038d2ef8SMaor Gottlieb struct ib_flow_attr *flow_attr, 2451038d2ef8SMaor Gottlieb int domain) 2452038d2ef8SMaor Gottlieb { 2453038d2ef8SMaor Gottlieb struct mlx5_ib_dev *dev = to_mdev(qp->device); 2454d9f88e5aSYishai Hadas struct mlx5_ib_qp *mqp = to_mqp(qp); 2455038d2ef8SMaor Gottlieb struct mlx5_ib_flow_handler *handler = NULL; 2456038d2ef8SMaor Gottlieb struct mlx5_flow_destination *dst = NULL; 2457cc0e5d42SMaor Gottlieb struct mlx5_ib_flow_prio *ft_prio_tx = NULL; 2458038d2ef8SMaor Gottlieb struct mlx5_ib_flow_prio *ft_prio; 2459038d2ef8SMaor Gottlieb int err; 2460038d2ef8SMaor Gottlieb 2461038d2ef8SMaor Gottlieb if (flow_attr->priority > MLX5_IB_FLOW_LAST_PRIO) 2462dac388efSMaor Gottlieb return ERR_PTR(-ENOMEM); 2463038d2ef8SMaor Gottlieb 2464038d2ef8SMaor Gottlieb if (domain != IB_FLOW_DOMAIN_USER || 2465038d2ef8SMaor Gottlieb flow_attr->port > MLX5_CAP_GEN(dev->mdev, num_ports) || 246635d19011SMaor Gottlieb (flow_attr->flags & ~IB_FLOW_ATTR_FLAGS_DONT_TRAP)) 2467038d2ef8SMaor Gottlieb return ERR_PTR(-EINVAL); 2468038d2ef8SMaor Gottlieb 2469038d2ef8SMaor Gottlieb dst = kzalloc(sizeof(*dst), GFP_KERNEL); 2470038d2ef8SMaor Gottlieb if (!dst) 2471038d2ef8SMaor Gottlieb return ERR_PTR(-ENOMEM); 2472038d2ef8SMaor Gottlieb 2473038d2ef8SMaor Gottlieb mutex_lock(&dev->flow_db.lock); 2474038d2ef8SMaor Gottlieb 2475cc0e5d42SMaor Gottlieb ft_prio = get_flow_table(dev, flow_attr, MLX5_IB_FT_RX); 2476038d2ef8SMaor Gottlieb if (IS_ERR(ft_prio)) { 2477038d2ef8SMaor Gottlieb err = PTR_ERR(ft_prio); 2478038d2ef8SMaor Gottlieb goto unlock; 2479038d2ef8SMaor Gottlieb } 2480cc0e5d42SMaor Gottlieb if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) { 2481cc0e5d42SMaor Gottlieb ft_prio_tx = get_flow_table(dev, flow_attr, MLX5_IB_FT_TX); 2482cc0e5d42SMaor Gottlieb if (IS_ERR(ft_prio_tx)) { 2483cc0e5d42SMaor Gottlieb err = PTR_ERR(ft_prio_tx); 2484cc0e5d42SMaor Gottlieb ft_prio_tx = NULL; 2485cc0e5d42SMaor Gottlieb goto destroy_ft; 2486cc0e5d42SMaor Gottlieb } 2487cc0e5d42SMaor Gottlieb } 2488038d2ef8SMaor Gottlieb 2489038d2ef8SMaor Gottlieb dst->type = MLX5_FLOW_DESTINATION_TYPE_TIR; 2490d9f88e5aSYishai Hadas if (mqp->flags & MLX5_IB_QP_RSS) 2491d9f88e5aSYishai Hadas dst->tir_num = mqp->rss_qp.tirn; 2492d9f88e5aSYishai Hadas else 2493d9f88e5aSYishai Hadas dst->tir_num = mqp->raw_packet_qp.rq.tirn; 2494038d2ef8SMaor Gottlieb 2495038d2ef8SMaor Gottlieb if (flow_attr->type == IB_FLOW_ATTR_NORMAL) { 249635d19011SMaor Gottlieb if (flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP) { 249735d19011SMaor Gottlieb handler = create_dont_trap_rule(dev, ft_prio, 249835d19011SMaor Gottlieb flow_attr, dst); 249935d19011SMaor Gottlieb } else { 2500038d2ef8SMaor Gottlieb handler = create_flow_rule(dev, ft_prio, flow_attr, 2501038d2ef8SMaor Gottlieb dst); 250235d19011SMaor Gottlieb } 2503038d2ef8SMaor Gottlieb } else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT || 2504038d2ef8SMaor Gottlieb flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) { 2505038d2ef8SMaor Gottlieb handler = create_leftovers_rule(dev, ft_prio, flow_attr, 2506038d2ef8SMaor Gottlieb dst); 2507cc0e5d42SMaor Gottlieb } else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) { 2508cc0e5d42SMaor Gottlieb handler = create_sniffer_rule(dev, ft_prio, ft_prio_tx, dst); 2509038d2ef8SMaor Gottlieb } else { 2510038d2ef8SMaor Gottlieb err = -EINVAL; 2511038d2ef8SMaor Gottlieb goto destroy_ft; 2512038d2ef8SMaor Gottlieb } 2513038d2ef8SMaor Gottlieb 2514038d2ef8SMaor Gottlieb if (IS_ERR(handler)) { 2515038d2ef8SMaor Gottlieb err = PTR_ERR(handler); 2516038d2ef8SMaor Gottlieb handler = NULL; 2517038d2ef8SMaor Gottlieb goto destroy_ft; 2518038d2ef8SMaor Gottlieb } 2519038d2ef8SMaor Gottlieb 2520038d2ef8SMaor Gottlieb mutex_unlock(&dev->flow_db.lock); 2521038d2ef8SMaor Gottlieb kfree(dst); 2522038d2ef8SMaor Gottlieb 2523038d2ef8SMaor Gottlieb return &handler->ibflow; 2524038d2ef8SMaor Gottlieb 2525038d2ef8SMaor Gottlieb destroy_ft: 2526038d2ef8SMaor Gottlieb put_flow_table(dev, ft_prio, false); 2527cc0e5d42SMaor Gottlieb if (ft_prio_tx) 2528cc0e5d42SMaor Gottlieb put_flow_table(dev, ft_prio_tx, false); 2529038d2ef8SMaor Gottlieb unlock: 2530038d2ef8SMaor Gottlieb mutex_unlock(&dev->flow_db.lock); 2531038d2ef8SMaor Gottlieb kfree(dst); 2532038d2ef8SMaor Gottlieb kfree(handler); 2533038d2ef8SMaor Gottlieb return ERR_PTR(err); 2534038d2ef8SMaor Gottlieb } 2535038d2ef8SMaor Gottlieb 2536e126ba97SEli Cohen static int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid) 2537e126ba97SEli Cohen { 2538e126ba97SEli Cohen struct mlx5_ib_dev *dev = to_mdev(ibqp->device); 2539e126ba97SEli Cohen int err; 2540e126ba97SEli Cohen 25419603b61dSJack Morgenstein err = mlx5_core_attach_mcg(dev->mdev, gid, ibqp->qp_num); 2542e126ba97SEli Cohen if (err) 2543e126ba97SEli Cohen mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n", 2544e126ba97SEli Cohen ibqp->qp_num, gid->raw); 2545e126ba97SEli Cohen 2546e126ba97SEli Cohen return err; 2547e126ba97SEli Cohen } 2548e126ba97SEli Cohen 2549e126ba97SEli Cohen static int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid) 2550e126ba97SEli Cohen { 2551e126ba97SEli Cohen struct mlx5_ib_dev *dev = to_mdev(ibqp->device); 2552e126ba97SEli Cohen int err; 2553e126ba97SEli Cohen 25549603b61dSJack Morgenstein err = mlx5_core_detach_mcg(dev->mdev, gid, ibqp->qp_num); 2555e126ba97SEli Cohen if (err) 2556e126ba97SEli Cohen mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n", 2557e126ba97SEli Cohen ibqp->qp_num, gid->raw); 2558e126ba97SEli Cohen 2559e126ba97SEli Cohen return err; 2560e126ba97SEli Cohen } 2561e126ba97SEli Cohen 2562e126ba97SEli Cohen static int init_node_data(struct mlx5_ib_dev *dev) 2563e126ba97SEli Cohen { 25641b5daf11SMajd Dibbiny int err; 2565e126ba97SEli Cohen 25661b5daf11SMajd Dibbiny err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc); 2567e126ba97SEli Cohen if (err) 2568e126ba97SEli Cohen return err; 25691b5daf11SMajd Dibbiny 25701b5daf11SMajd Dibbiny dev->mdev->rev_id = dev->mdev->pdev->revision; 25711b5daf11SMajd Dibbiny 25721b5daf11SMajd Dibbiny return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid); 2573e126ba97SEli Cohen } 2574e126ba97SEli Cohen 2575e126ba97SEli Cohen static ssize_t show_fw_pages(struct device *device, struct device_attribute *attr, 2576e126ba97SEli Cohen char *buf) 2577e126ba97SEli Cohen { 2578e126ba97SEli Cohen struct mlx5_ib_dev *dev = 2579e126ba97SEli Cohen container_of(device, struct mlx5_ib_dev, ib_dev.dev); 2580e126ba97SEli Cohen 25819603b61dSJack Morgenstein return sprintf(buf, "%d\n", dev->mdev->priv.fw_pages); 2582e126ba97SEli Cohen } 2583e126ba97SEli Cohen 2584e126ba97SEli Cohen static ssize_t show_reg_pages(struct device *device, 2585e126ba97SEli Cohen struct device_attribute *attr, char *buf) 2586e126ba97SEli Cohen { 2587e126ba97SEli Cohen struct mlx5_ib_dev *dev = 2588e126ba97SEli Cohen container_of(device, struct mlx5_ib_dev, ib_dev.dev); 2589e126ba97SEli Cohen 25906aec21f6SHaggai Eran return sprintf(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages)); 2591e126ba97SEli Cohen } 2592e126ba97SEli Cohen 2593e126ba97SEli Cohen static ssize_t show_hca(struct device *device, struct device_attribute *attr, 2594e126ba97SEli Cohen char *buf) 2595e126ba97SEli Cohen { 2596e126ba97SEli Cohen struct mlx5_ib_dev *dev = 2597e126ba97SEli Cohen container_of(device, struct mlx5_ib_dev, ib_dev.dev); 25989603b61dSJack Morgenstein return sprintf(buf, "MT%d\n", dev->mdev->pdev->device); 2599e126ba97SEli Cohen } 2600e126ba97SEli Cohen 2601e126ba97SEli Cohen static ssize_t show_rev(struct device *device, struct device_attribute *attr, 2602e126ba97SEli Cohen char *buf) 2603e126ba97SEli Cohen { 2604e126ba97SEli Cohen struct mlx5_ib_dev *dev = 2605e126ba97SEli Cohen container_of(device, struct mlx5_ib_dev, ib_dev.dev); 26069603b61dSJack Morgenstein return sprintf(buf, "%x\n", dev->mdev->rev_id); 2607e126ba97SEli Cohen } 2608e126ba97SEli Cohen 2609e126ba97SEli Cohen static ssize_t show_board(struct device *device, struct device_attribute *attr, 2610e126ba97SEli Cohen char *buf) 2611e126ba97SEli Cohen { 2612e126ba97SEli Cohen struct mlx5_ib_dev *dev = 2613e126ba97SEli Cohen container_of(device, struct mlx5_ib_dev, ib_dev.dev); 2614e126ba97SEli Cohen return sprintf(buf, "%.*s\n", MLX5_BOARD_ID_LEN, 26159603b61dSJack Morgenstein dev->mdev->board_id); 2616e126ba97SEli Cohen } 2617e126ba97SEli Cohen 2618e126ba97SEli Cohen static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL); 2619e126ba97SEli Cohen static DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL); 2620e126ba97SEli Cohen static DEVICE_ATTR(board_id, S_IRUGO, show_board, NULL); 2621e126ba97SEli Cohen static DEVICE_ATTR(fw_pages, S_IRUGO, show_fw_pages, NULL); 2622e126ba97SEli Cohen static DEVICE_ATTR(reg_pages, S_IRUGO, show_reg_pages, NULL); 2623e126ba97SEli Cohen 2624e126ba97SEli Cohen static struct device_attribute *mlx5_class_attributes[] = { 2625e126ba97SEli Cohen &dev_attr_hw_rev, 2626e126ba97SEli Cohen &dev_attr_hca_type, 2627e126ba97SEli Cohen &dev_attr_board_id, 2628e126ba97SEli Cohen &dev_attr_fw_pages, 2629e126ba97SEli Cohen &dev_attr_reg_pages, 2630e126ba97SEli Cohen }; 2631e126ba97SEli Cohen 26327722f47eSHaggai Eran static void pkey_change_handler(struct work_struct *work) 26337722f47eSHaggai Eran { 26347722f47eSHaggai Eran struct mlx5_ib_port_resources *ports = 26357722f47eSHaggai Eran container_of(work, struct mlx5_ib_port_resources, 26367722f47eSHaggai Eran pkey_change_work); 26377722f47eSHaggai Eran 26387722f47eSHaggai Eran mutex_lock(&ports->devr->mutex); 26397722f47eSHaggai Eran mlx5_ib_gsi_pkey_change(ports->gsi); 26407722f47eSHaggai Eran mutex_unlock(&ports->devr->mutex); 26417722f47eSHaggai Eran } 26427722f47eSHaggai Eran 264389ea94a7SMaor Gottlieb static void mlx5_ib_handle_internal_error(struct mlx5_ib_dev *ibdev) 264489ea94a7SMaor Gottlieb { 264589ea94a7SMaor Gottlieb struct mlx5_ib_qp *mqp; 264689ea94a7SMaor Gottlieb struct mlx5_ib_cq *send_mcq, *recv_mcq; 264789ea94a7SMaor Gottlieb struct mlx5_core_cq *mcq; 264889ea94a7SMaor Gottlieb struct list_head cq_armed_list; 264989ea94a7SMaor Gottlieb unsigned long flags_qp; 265089ea94a7SMaor Gottlieb unsigned long flags_cq; 265189ea94a7SMaor Gottlieb unsigned long flags; 265289ea94a7SMaor Gottlieb 265389ea94a7SMaor Gottlieb INIT_LIST_HEAD(&cq_armed_list); 265489ea94a7SMaor Gottlieb 265589ea94a7SMaor Gottlieb /* Go over qp list reside on that ibdev, sync with create/destroy qp.*/ 265689ea94a7SMaor Gottlieb spin_lock_irqsave(&ibdev->reset_flow_resource_lock, flags); 265789ea94a7SMaor Gottlieb list_for_each_entry(mqp, &ibdev->qp_list, qps_list) { 265889ea94a7SMaor Gottlieb spin_lock_irqsave(&mqp->sq.lock, flags_qp); 265989ea94a7SMaor Gottlieb if (mqp->sq.tail != mqp->sq.head) { 266089ea94a7SMaor Gottlieb send_mcq = to_mcq(mqp->ibqp.send_cq); 266189ea94a7SMaor Gottlieb spin_lock_irqsave(&send_mcq->lock, flags_cq); 266289ea94a7SMaor Gottlieb if (send_mcq->mcq.comp && 266389ea94a7SMaor Gottlieb mqp->ibqp.send_cq->comp_handler) { 266489ea94a7SMaor Gottlieb if (!send_mcq->mcq.reset_notify_added) { 266589ea94a7SMaor Gottlieb send_mcq->mcq.reset_notify_added = 1; 266689ea94a7SMaor Gottlieb list_add_tail(&send_mcq->mcq.reset_notify, 266789ea94a7SMaor Gottlieb &cq_armed_list); 266889ea94a7SMaor Gottlieb } 266989ea94a7SMaor Gottlieb } 267089ea94a7SMaor Gottlieb spin_unlock_irqrestore(&send_mcq->lock, flags_cq); 267189ea94a7SMaor Gottlieb } 267289ea94a7SMaor Gottlieb spin_unlock_irqrestore(&mqp->sq.lock, flags_qp); 267389ea94a7SMaor Gottlieb spin_lock_irqsave(&mqp->rq.lock, flags_qp); 267489ea94a7SMaor Gottlieb /* no handling is needed for SRQ */ 267589ea94a7SMaor Gottlieb if (!mqp->ibqp.srq) { 267689ea94a7SMaor Gottlieb if (mqp->rq.tail != mqp->rq.head) { 267789ea94a7SMaor Gottlieb recv_mcq = to_mcq(mqp->ibqp.recv_cq); 267889ea94a7SMaor Gottlieb spin_lock_irqsave(&recv_mcq->lock, flags_cq); 267989ea94a7SMaor Gottlieb if (recv_mcq->mcq.comp && 268089ea94a7SMaor Gottlieb mqp->ibqp.recv_cq->comp_handler) { 268189ea94a7SMaor Gottlieb if (!recv_mcq->mcq.reset_notify_added) { 268289ea94a7SMaor Gottlieb recv_mcq->mcq.reset_notify_added = 1; 268389ea94a7SMaor Gottlieb list_add_tail(&recv_mcq->mcq.reset_notify, 268489ea94a7SMaor Gottlieb &cq_armed_list); 268589ea94a7SMaor Gottlieb } 268689ea94a7SMaor Gottlieb } 268789ea94a7SMaor Gottlieb spin_unlock_irqrestore(&recv_mcq->lock, 268889ea94a7SMaor Gottlieb flags_cq); 268989ea94a7SMaor Gottlieb } 269089ea94a7SMaor Gottlieb } 269189ea94a7SMaor Gottlieb spin_unlock_irqrestore(&mqp->rq.lock, flags_qp); 269289ea94a7SMaor Gottlieb } 269389ea94a7SMaor Gottlieb /*At that point all inflight post send were put to be executed as of we 269489ea94a7SMaor Gottlieb * lock/unlock above locks Now need to arm all involved CQs. 269589ea94a7SMaor Gottlieb */ 269689ea94a7SMaor Gottlieb list_for_each_entry(mcq, &cq_armed_list, reset_notify) { 269789ea94a7SMaor Gottlieb mcq->comp(mcq); 269889ea94a7SMaor Gottlieb } 269989ea94a7SMaor Gottlieb spin_unlock_irqrestore(&ibdev->reset_flow_resource_lock, flags); 270089ea94a7SMaor Gottlieb } 270189ea94a7SMaor Gottlieb 27029603b61dSJack Morgenstein static void mlx5_ib_event(struct mlx5_core_dev *dev, void *context, 27034d2f9bbbSJack Morgenstein enum mlx5_dev_event event, unsigned long param) 2704e126ba97SEli Cohen { 27059603b61dSJack Morgenstein struct mlx5_ib_dev *ibdev = (struct mlx5_ib_dev *)context; 2706e126ba97SEli Cohen struct ib_event ibev; 2707dbaaff2aSEli Cohen bool fatal = false; 2708e126ba97SEli Cohen u8 port = 0; 2709e126ba97SEli Cohen 2710e126ba97SEli Cohen switch (event) { 2711e126ba97SEli Cohen case MLX5_DEV_EVENT_SYS_ERROR: 2712e126ba97SEli Cohen ibev.event = IB_EVENT_DEVICE_FATAL; 271389ea94a7SMaor Gottlieb mlx5_ib_handle_internal_error(ibdev); 2714dbaaff2aSEli Cohen fatal = true; 2715e126ba97SEli Cohen break; 2716e126ba97SEli Cohen 2717e126ba97SEli Cohen case MLX5_DEV_EVENT_PORT_UP: 2718e126ba97SEli Cohen case MLX5_DEV_EVENT_PORT_DOWN: 27192788cf3bSNoa Osherovich case MLX5_DEV_EVENT_PORT_INITIALIZED: 27204d2f9bbbSJack Morgenstein port = (u8)param; 27215ec8c83eSAviv Heller 27225ec8c83eSAviv Heller /* In RoCE, port up/down events are handled in 27235ec8c83eSAviv Heller * mlx5_netdev_event(). 27245ec8c83eSAviv Heller */ 27255ec8c83eSAviv Heller if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) == 27265ec8c83eSAviv Heller IB_LINK_LAYER_ETHERNET) 27275ec8c83eSAviv Heller return; 27285ec8c83eSAviv Heller 27295ec8c83eSAviv Heller ibev.event = (event == MLX5_DEV_EVENT_PORT_UP) ? 27305ec8c83eSAviv Heller IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR; 2731e126ba97SEli Cohen break; 2732e126ba97SEli Cohen 2733e126ba97SEli Cohen case MLX5_DEV_EVENT_LID_CHANGE: 2734e126ba97SEli Cohen ibev.event = IB_EVENT_LID_CHANGE; 27354d2f9bbbSJack Morgenstein port = (u8)param; 2736e126ba97SEli Cohen break; 2737e126ba97SEli Cohen 2738e126ba97SEli Cohen case MLX5_DEV_EVENT_PKEY_CHANGE: 2739e126ba97SEli Cohen ibev.event = IB_EVENT_PKEY_CHANGE; 27404d2f9bbbSJack Morgenstein port = (u8)param; 27417722f47eSHaggai Eran 27427722f47eSHaggai Eran schedule_work(&ibdev->devr.ports[port - 1].pkey_change_work); 2743e126ba97SEli Cohen break; 2744e126ba97SEli Cohen 2745e126ba97SEli Cohen case MLX5_DEV_EVENT_GUID_CHANGE: 2746e126ba97SEli Cohen ibev.event = IB_EVENT_GID_CHANGE; 27474d2f9bbbSJack Morgenstein port = (u8)param; 2748e126ba97SEli Cohen break; 2749e126ba97SEli Cohen 2750e126ba97SEli Cohen case MLX5_DEV_EVENT_CLIENT_REREG: 2751e126ba97SEli Cohen ibev.event = IB_EVENT_CLIENT_REREGISTER; 27524d2f9bbbSJack Morgenstein port = (u8)param; 2753e126ba97SEli Cohen break; 2754bdc37924SSaeed Mahameed default: 2755bdc37924SSaeed Mahameed return; 2756e126ba97SEli Cohen } 2757e126ba97SEli Cohen 2758e126ba97SEli Cohen ibev.device = &ibdev->ib_dev; 2759e126ba97SEli Cohen ibev.element.port_num = port; 2760e126ba97SEli Cohen 2761a0c84c32SEli Cohen if (port < 1 || port > ibdev->num_ports) { 2762a0c84c32SEli Cohen mlx5_ib_warn(ibdev, "warning: event on port %d\n", port); 2763a0c84c32SEli Cohen return; 2764a0c84c32SEli Cohen } 2765a0c84c32SEli Cohen 2766e126ba97SEli Cohen if (ibdev->ib_active) 2767e126ba97SEli Cohen ib_dispatch_event(&ibev); 2768dbaaff2aSEli Cohen 2769dbaaff2aSEli Cohen if (fatal) 2770dbaaff2aSEli Cohen ibdev->ib_active = false; 2771e126ba97SEli Cohen } 2772e126ba97SEli Cohen 2773c43f1112SMaor Gottlieb static int set_has_smi_cap(struct mlx5_ib_dev *dev) 2774c43f1112SMaor Gottlieb { 2775c43f1112SMaor Gottlieb struct mlx5_hca_vport_context vport_ctx; 2776c43f1112SMaor Gottlieb int err; 2777c43f1112SMaor Gottlieb int port; 2778c43f1112SMaor Gottlieb 2779c43f1112SMaor Gottlieb for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++) { 2780c43f1112SMaor Gottlieb dev->mdev->port_caps[port - 1].has_smi = false; 2781c43f1112SMaor Gottlieb if (MLX5_CAP_GEN(dev->mdev, port_type) == 2782c43f1112SMaor Gottlieb MLX5_CAP_PORT_TYPE_IB) { 2783c43f1112SMaor Gottlieb if (MLX5_CAP_GEN(dev->mdev, ib_virt)) { 2784c43f1112SMaor Gottlieb err = mlx5_query_hca_vport_context(dev->mdev, 0, 2785c43f1112SMaor Gottlieb port, 0, 2786c43f1112SMaor Gottlieb &vport_ctx); 2787c43f1112SMaor Gottlieb if (err) { 2788c43f1112SMaor Gottlieb mlx5_ib_err(dev, "query_hca_vport_context for port=%d failed %d\n", 2789c43f1112SMaor Gottlieb port, err); 2790c43f1112SMaor Gottlieb return err; 2791c43f1112SMaor Gottlieb } 2792c43f1112SMaor Gottlieb dev->mdev->port_caps[port - 1].has_smi = 2793c43f1112SMaor Gottlieb vport_ctx.has_smi; 2794c43f1112SMaor Gottlieb } else { 2795c43f1112SMaor Gottlieb dev->mdev->port_caps[port - 1].has_smi = true; 2796c43f1112SMaor Gottlieb } 2797c43f1112SMaor Gottlieb } 2798c43f1112SMaor Gottlieb } 2799c43f1112SMaor Gottlieb return 0; 2800c43f1112SMaor Gottlieb } 2801c43f1112SMaor Gottlieb 2802e126ba97SEli Cohen static void get_ext_port_caps(struct mlx5_ib_dev *dev) 2803e126ba97SEli Cohen { 2804e126ba97SEli Cohen int port; 2805e126ba97SEli Cohen 2806938fe83cSSaeed Mahameed for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++) 2807e126ba97SEli Cohen mlx5_query_ext_port_caps(dev, port); 2808e126ba97SEli Cohen } 2809e126ba97SEli Cohen 2810e126ba97SEli Cohen static int get_port_caps(struct mlx5_ib_dev *dev) 2811e126ba97SEli Cohen { 2812e126ba97SEli Cohen struct ib_device_attr *dprops = NULL; 2813e126ba97SEli Cohen struct ib_port_attr *pprops = NULL; 2814f614fc15SDan Carpenter int err = -ENOMEM; 2815e126ba97SEli Cohen int port; 28162528e33eSMatan Barak struct ib_udata uhw = {.inlen = 0, .outlen = 0}; 2817e126ba97SEli Cohen 2818e126ba97SEli Cohen pprops = kmalloc(sizeof(*pprops), GFP_KERNEL); 2819e126ba97SEli Cohen if (!pprops) 2820e126ba97SEli Cohen goto out; 2821e126ba97SEli Cohen 2822e126ba97SEli Cohen dprops = kmalloc(sizeof(*dprops), GFP_KERNEL); 2823e126ba97SEli Cohen if (!dprops) 2824e126ba97SEli Cohen goto out; 2825e126ba97SEli Cohen 2826c43f1112SMaor Gottlieb err = set_has_smi_cap(dev); 2827c43f1112SMaor Gottlieb if (err) 2828c43f1112SMaor Gottlieb goto out; 2829c43f1112SMaor Gottlieb 28302528e33eSMatan Barak err = mlx5_ib_query_device(&dev->ib_dev, dprops, &uhw); 2831e126ba97SEli Cohen if (err) { 2832e126ba97SEli Cohen mlx5_ib_warn(dev, "query_device failed %d\n", err); 2833e126ba97SEli Cohen goto out; 2834e126ba97SEli Cohen } 2835e126ba97SEli Cohen 2836938fe83cSSaeed Mahameed for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++) { 2837c4550c63SOr Gerlitz memset(pprops, 0, sizeof(*pprops)); 2838e126ba97SEli Cohen err = mlx5_ib_query_port(&dev->ib_dev, port, pprops); 2839e126ba97SEli Cohen if (err) { 2840938fe83cSSaeed Mahameed mlx5_ib_warn(dev, "query_port %d failed %d\n", 2841938fe83cSSaeed Mahameed port, err); 2842e126ba97SEli Cohen break; 2843e126ba97SEli Cohen } 2844938fe83cSSaeed Mahameed dev->mdev->port_caps[port - 1].pkey_table_len = 2845938fe83cSSaeed Mahameed dprops->max_pkeys; 2846938fe83cSSaeed Mahameed dev->mdev->port_caps[port - 1].gid_table_len = 2847938fe83cSSaeed Mahameed pprops->gid_tbl_len; 2848e126ba97SEli Cohen mlx5_ib_dbg(dev, "pkey_table_len %d, gid_table_len %d\n", 2849e126ba97SEli Cohen dprops->max_pkeys, pprops->gid_tbl_len); 2850e126ba97SEli Cohen } 2851e126ba97SEli Cohen 2852e126ba97SEli Cohen out: 2853e126ba97SEli Cohen kfree(pprops); 2854e126ba97SEli Cohen kfree(dprops); 2855e126ba97SEli Cohen 2856e126ba97SEli Cohen return err; 2857e126ba97SEli Cohen } 2858e126ba97SEli Cohen 2859e126ba97SEli Cohen static void destroy_umrc_res(struct mlx5_ib_dev *dev) 2860e126ba97SEli Cohen { 2861e126ba97SEli Cohen int err; 2862e126ba97SEli Cohen 2863e126ba97SEli Cohen err = mlx5_mr_cache_cleanup(dev); 2864e126ba97SEli Cohen if (err) 2865e126ba97SEli Cohen mlx5_ib_warn(dev, "mr cache cleanup failed\n"); 2866e126ba97SEli Cohen 2867e126ba97SEli Cohen mlx5_ib_destroy_qp(dev->umrc.qp); 2868add08d76SChristoph Hellwig ib_free_cq(dev->umrc.cq); 2869e126ba97SEli Cohen ib_dealloc_pd(dev->umrc.pd); 2870e126ba97SEli Cohen } 2871e126ba97SEli Cohen 2872e126ba97SEli Cohen enum { 2873e126ba97SEli Cohen MAX_UMR_WR = 128, 2874e126ba97SEli Cohen }; 2875e126ba97SEli Cohen 2876e126ba97SEli Cohen static int create_umr_res(struct mlx5_ib_dev *dev) 2877e126ba97SEli Cohen { 2878e126ba97SEli Cohen struct ib_qp_init_attr *init_attr = NULL; 2879e126ba97SEli Cohen struct ib_qp_attr *attr = NULL; 2880e126ba97SEli Cohen struct ib_pd *pd; 2881e126ba97SEli Cohen struct ib_cq *cq; 2882e126ba97SEli Cohen struct ib_qp *qp; 2883e126ba97SEli Cohen int ret; 2884e126ba97SEli Cohen 2885e126ba97SEli Cohen attr = kzalloc(sizeof(*attr), GFP_KERNEL); 2886e126ba97SEli Cohen init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL); 2887e126ba97SEli Cohen if (!attr || !init_attr) { 2888e126ba97SEli Cohen ret = -ENOMEM; 2889e126ba97SEli Cohen goto error_0; 2890e126ba97SEli Cohen } 2891e126ba97SEli Cohen 2892ed082d36SChristoph Hellwig pd = ib_alloc_pd(&dev->ib_dev, 0); 2893e126ba97SEli Cohen if (IS_ERR(pd)) { 2894e126ba97SEli Cohen mlx5_ib_dbg(dev, "Couldn't create PD for sync UMR QP\n"); 2895e126ba97SEli Cohen ret = PTR_ERR(pd); 2896e126ba97SEli Cohen goto error_0; 2897e126ba97SEli Cohen } 2898e126ba97SEli Cohen 2899add08d76SChristoph Hellwig cq = ib_alloc_cq(&dev->ib_dev, NULL, 128, 0, IB_POLL_SOFTIRQ); 2900e126ba97SEli Cohen if (IS_ERR(cq)) { 2901e126ba97SEli Cohen mlx5_ib_dbg(dev, "Couldn't create CQ for sync UMR QP\n"); 2902e126ba97SEli Cohen ret = PTR_ERR(cq); 2903e126ba97SEli Cohen goto error_2; 2904e126ba97SEli Cohen } 2905e126ba97SEli Cohen 2906e126ba97SEli Cohen init_attr->send_cq = cq; 2907e126ba97SEli Cohen init_attr->recv_cq = cq; 2908e126ba97SEli Cohen init_attr->sq_sig_type = IB_SIGNAL_ALL_WR; 2909e126ba97SEli Cohen init_attr->cap.max_send_wr = MAX_UMR_WR; 2910e126ba97SEli Cohen init_attr->cap.max_send_sge = 1; 2911e126ba97SEli Cohen init_attr->qp_type = MLX5_IB_QPT_REG_UMR; 2912e126ba97SEli Cohen init_attr->port_num = 1; 2913e126ba97SEli Cohen qp = mlx5_ib_create_qp(pd, init_attr, NULL); 2914e126ba97SEli Cohen if (IS_ERR(qp)) { 2915e126ba97SEli Cohen mlx5_ib_dbg(dev, "Couldn't create sync UMR QP\n"); 2916e126ba97SEli Cohen ret = PTR_ERR(qp); 2917e126ba97SEli Cohen goto error_3; 2918e126ba97SEli Cohen } 2919e126ba97SEli Cohen qp->device = &dev->ib_dev; 2920e126ba97SEli Cohen qp->real_qp = qp; 2921e126ba97SEli Cohen qp->uobject = NULL; 2922e126ba97SEli Cohen qp->qp_type = MLX5_IB_QPT_REG_UMR; 2923e126ba97SEli Cohen 2924e126ba97SEli Cohen attr->qp_state = IB_QPS_INIT; 2925e126ba97SEli Cohen attr->port_num = 1; 2926e126ba97SEli Cohen ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_PKEY_INDEX | 2927e126ba97SEli Cohen IB_QP_PORT, NULL); 2928e126ba97SEli Cohen if (ret) { 2929e126ba97SEli Cohen mlx5_ib_dbg(dev, "Couldn't modify UMR QP\n"); 2930e126ba97SEli Cohen goto error_4; 2931e126ba97SEli Cohen } 2932e126ba97SEli Cohen 2933e126ba97SEli Cohen memset(attr, 0, sizeof(*attr)); 2934e126ba97SEli Cohen attr->qp_state = IB_QPS_RTR; 2935e126ba97SEli Cohen attr->path_mtu = IB_MTU_256; 2936e126ba97SEli Cohen 2937e126ba97SEli Cohen ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL); 2938e126ba97SEli Cohen if (ret) { 2939e126ba97SEli Cohen mlx5_ib_dbg(dev, "Couldn't modify umr QP to rtr\n"); 2940e126ba97SEli Cohen goto error_4; 2941e126ba97SEli Cohen } 2942e126ba97SEli Cohen 2943e126ba97SEli Cohen memset(attr, 0, sizeof(*attr)); 2944e126ba97SEli Cohen attr->qp_state = IB_QPS_RTS; 2945e126ba97SEli Cohen ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL); 2946e126ba97SEli Cohen if (ret) { 2947e126ba97SEli Cohen mlx5_ib_dbg(dev, "Couldn't modify umr QP to rts\n"); 2948e126ba97SEli Cohen goto error_4; 2949e126ba97SEli Cohen } 2950e126ba97SEli Cohen 2951e126ba97SEli Cohen dev->umrc.qp = qp; 2952e126ba97SEli Cohen dev->umrc.cq = cq; 2953e126ba97SEli Cohen dev->umrc.pd = pd; 2954e126ba97SEli Cohen 2955e126ba97SEli Cohen sema_init(&dev->umrc.sem, MAX_UMR_WR); 2956e126ba97SEli Cohen ret = mlx5_mr_cache_init(dev); 2957e126ba97SEli Cohen if (ret) { 2958e126ba97SEli Cohen mlx5_ib_warn(dev, "mr cache init failed %d\n", ret); 2959e126ba97SEli Cohen goto error_4; 2960e126ba97SEli Cohen } 2961e126ba97SEli Cohen 2962e126ba97SEli Cohen kfree(attr); 2963e126ba97SEli Cohen kfree(init_attr); 2964e126ba97SEli Cohen 2965e126ba97SEli Cohen return 0; 2966e126ba97SEli Cohen 2967e126ba97SEli Cohen error_4: 2968e126ba97SEli Cohen mlx5_ib_destroy_qp(qp); 2969e126ba97SEli Cohen 2970e126ba97SEli Cohen error_3: 2971add08d76SChristoph Hellwig ib_free_cq(cq); 2972e126ba97SEli Cohen 2973e126ba97SEli Cohen error_2: 2974e126ba97SEli Cohen ib_dealloc_pd(pd); 2975e126ba97SEli Cohen 2976e126ba97SEli Cohen error_0: 2977e126ba97SEli Cohen kfree(attr); 2978e126ba97SEli Cohen kfree(init_attr); 2979e126ba97SEli Cohen return ret; 2980e126ba97SEli Cohen } 2981e126ba97SEli Cohen 2982e126ba97SEli Cohen static int create_dev_resources(struct mlx5_ib_resources *devr) 2983e126ba97SEli Cohen { 2984e126ba97SEli Cohen struct ib_srq_init_attr attr; 2985e126ba97SEli Cohen struct mlx5_ib_dev *dev; 2986bcf4c1eaSMatan Barak struct ib_cq_init_attr cq_attr = {.cqe = 1}; 29877722f47eSHaggai Eran int port; 2988e126ba97SEli Cohen int ret = 0; 2989e126ba97SEli Cohen 2990e126ba97SEli Cohen dev = container_of(devr, struct mlx5_ib_dev, devr); 2991e126ba97SEli Cohen 2992d16e91daSHaggai Eran mutex_init(&devr->mutex); 2993d16e91daSHaggai Eran 2994e126ba97SEli Cohen devr->p0 = mlx5_ib_alloc_pd(&dev->ib_dev, NULL, NULL); 2995e126ba97SEli Cohen if (IS_ERR(devr->p0)) { 2996e126ba97SEli Cohen ret = PTR_ERR(devr->p0); 2997e126ba97SEli Cohen goto error0; 2998e126ba97SEli Cohen } 2999e126ba97SEli Cohen devr->p0->device = &dev->ib_dev; 3000e126ba97SEli Cohen devr->p0->uobject = NULL; 3001e126ba97SEli Cohen atomic_set(&devr->p0->usecnt, 0); 3002e126ba97SEli Cohen 3003bcf4c1eaSMatan Barak devr->c0 = mlx5_ib_create_cq(&dev->ib_dev, &cq_attr, NULL, NULL); 3004e126ba97SEli Cohen if (IS_ERR(devr->c0)) { 3005e126ba97SEli Cohen ret = PTR_ERR(devr->c0); 3006e126ba97SEli Cohen goto error1; 3007e126ba97SEli Cohen } 3008e126ba97SEli Cohen devr->c0->device = &dev->ib_dev; 3009e126ba97SEli Cohen devr->c0->uobject = NULL; 3010e126ba97SEli Cohen devr->c0->comp_handler = NULL; 3011e126ba97SEli Cohen devr->c0->event_handler = NULL; 3012e126ba97SEli Cohen devr->c0->cq_context = NULL; 3013e126ba97SEli Cohen atomic_set(&devr->c0->usecnt, 0); 3014e126ba97SEli Cohen 3015e126ba97SEli Cohen devr->x0 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL); 3016e126ba97SEli Cohen if (IS_ERR(devr->x0)) { 3017e126ba97SEli Cohen ret = PTR_ERR(devr->x0); 3018e126ba97SEli Cohen goto error2; 3019e126ba97SEli Cohen } 3020e126ba97SEli Cohen devr->x0->device = &dev->ib_dev; 3021e126ba97SEli Cohen devr->x0->inode = NULL; 3022e126ba97SEli Cohen atomic_set(&devr->x0->usecnt, 0); 3023e126ba97SEli Cohen mutex_init(&devr->x0->tgt_qp_mutex); 3024e126ba97SEli Cohen INIT_LIST_HEAD(&devr->x0->tgt_qp_list); 3025e126ba97SEli Cohen 3026e126ba97SEli Cohen devr->x1 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL); 3027e126ba97SEli Cohen if (IS_ERR(devr->x1)) { 3028e126ba97SEli Cohen ret = PTR_ERR(devr->x1); 3029e126ba97SEli Cohen goto error3; 3030e126ba97SEli Cohen } 3031e126ba97SEli Cohen devr->x1->device = &dev->ib_dev; 3032e126ba97SEli Cohen devr->x1->inode = NULL; 3033e126ba97SEli Cohen atomic_set(&devr->x1->usecnt, 0); 3034e126ba97SEli Cohen mutex_init(&devr->x1->tgt_qp_mutex); 3035e126ba97SEli Cohen INIT_LIST_HEAD(&devr->x1->tgt_qp_list); 3036e126ba97SEli Cohen 3037e126ba97SEli Cohen memset(&attr, 0, sizeof(attr)); 3038e126ba97SEli Cohen attr.attr.max_sge = 1; 3039e126ba97SEli Cohen attr.attr.max_wr = 1; 3040e126ba97SEli Cohen attr.srq_type = IB_SRQT_XRC; 3041e126ba97SEli Cohen attr.ext.xrc.cq = devr->c0; 3042e126ba97SEli Cohen attr.ext.xrc.xrcd = devr->x0; 3043e126ba97SEli Cohen 3044e126ba97SEli Cohen devr->s0 = mlx5_ib_create_srq(devr->p0, &attr, NULL); 3045e126ba97SEli Cohen if (IS_ERR(devr->s0)) { 3046e126ba97SEli Cohen ret = PTR_ERR(devr->s0); 3047e126ba97SEli Cohen goto error4; 3048e126ba97SEli Cohen } 3049e126ba97SEli Cohen devr->s0->device = &dev->ib_dev; 3050e126ba97SEli Cohen devr->s0->pd = devr->p0; 3051e126ba97SEli Cohen devr->s0->uobject = NULL; 3052e126ba97SEli Cohen devr->s0->event_handler = NULL; 3053e126ba97SEli Cohen devr->s0->srq_context = NULL; 3054e126ba97SEli Cohen devr->s0->srq_type = IB_SRQT_XRC; 3055e126ba97SEli Cohen devr->s0->ext.xrc.xrcd = devr->x0; 3056e126ba97SEli Cohen devr->s0->ext.xrc.cq = devr->c0; 3057e126ba97SEli Cohen atomic_inc(&devr->s0->ext.xrc.xrcd->usecnt); 3058e126ba97SEli Cohen atomic_inc(&devr->s0->ext.xrc.cq->usecnt); 3059e126ba97SEli Cohen atomic_inc(&devr->p0->usecnt); 3060e126ba97SEli Cohen atomic_set(&devr->s0->usecnt, 0); 3061e126ba97SEli Cohen 30624aa17b28SHaggai Abramonvsky memset(&attr, 0, sizeof(attr)); 30634aa17b28SHaggai Abramonvsky attr.attr.max_sge = 1; 30644aa17b28SHaggai Abramonvsky attr.attr.max_wr = 1; 30654aa17b28SHaggai Abramonvsky attr.srq_type = IB_SRQT_BASIC; 30664aa17b28SHaggai Abramonvsky devr->s1 = mlx5_ib_create_srq(devr->p0, &attr, NULL); 30674aa17b28SHaggai Abramonvsky if (IS_ERR(devr->s1)) { 30684aa17b28SHaggai Abramonvsky ret = PTR_ERR(devr->s1); 30694aa17b28SHaggai Abramonvsky goto error5; 30704aa17b28SHaggai Abramonvsky } 30714aa17b28SHaggai Abramonvsky devr->s1->device = &dev->ib_dev; 30724aa17b28SHaggai Abramonvsky devr->s1->pd = devr->p0; 30734aa17b28SHaggai Abramonvsky devr->s1->uobject = NULL; 30744aa17b28SHaggai Abramonvsky devr->s1->event_handler = NULL; 30754aa17b28SHaggai Abramonvsky devr->s1->srq_context = NULL; 30764aa17b28SHaggai Abramonvsky devr->s1->srq_type = IB_SRQT_BASIC; 30774aa17b28SHaggai Abramonvsky devr->s1->ext.xrc.cq = devr->c0; 30784aa17b28SHaggai Abramonvsky atomic_inc(&devr->p0->usecnt); 30794aa17b28SHaggai Abramonvsky atomic_set(&devr->s0->usecnt, 0); 30804aa17b28SHaggai Abramonvsky 30817722f47eSHaggai Eran for (port = 0; port < ARRAY_SIZE(devr->ports); ++port) { 30827722f47eSHaggai Eran INIT_WORK(&devr->ports[port].pkey_change_work, 30837722f47eSHaggai Eran pkey_change_handler); 30847722f47eSHaggai Eran devr->ports[port].devr = devr; 30857722f47eSHaggai Eran } 30867722f47eSHaggai Eran 3087e126ba97SEli Cohen return 0; 3088e126ba97SEli Cohen 30894aa17b28SHaggai Abramonvsky error5: 30904aa17b28SHaggai Abramonvsky mlx5_ib_destroy_srq(devr->s0); 3091e126ba97SEli Cohen error4: 3092e126ba97SEli Cohen mlx5_ib_dealloc_xrcd(devr->x1); 3093e126ba97SEli Cohen error3: 3094e126ba97SEli Cohen mlx5_ib_dealloc_xrcd(devr->x0); 3095e126ba97SEli Cohen error2: 3096e126ba97SEli Cohen mlx5_ib_destroy_cq(devr->c0); 3097e126ba97SEli Cohen error1: 3098e126ba97SEli Cohen mlx5_ib_dealloc_pd(devr->p0); 3099e126ba97SEli Cohen error0: 3100e126ba97SEli Cohen return ret; 3101e126ba97SEli Cohen } 3102e126ba97SEli Cohen 3103e126ba97SEli Cohen static void destroy_dev_resources(struct mlx5_ib_resources *devr) 3104e126ba97SEli Cohen { 31057722f47eSHaggai Eran struct mlx5_ib_dev *dev = 31067722f47eSHaggai Eran container_of(devr, struct mlx5_ib_dev, devr); 31077722f47eSHaggai Eran int port; 31087722f47eSHaggai Eran 31094aa17b28SHaggai Abramonvsky mlx5_ib_destroy_srq(devr->s1); 3110e126ba97SEli Cohen mlx5_ib_destroy_srq(devr->s0); 3111e126ba97SEli Cohen mlx5_ib_dealloc_xrcd(devr->x0); 3112e126ba97SEli Cohen mlx5_ib_dealloc_xrcd(devr->x1); 3113e126ba97SEli Cohen mlx5_ib_destroy_cq(devr->c0); 3114e126ba97SEli Cohen mlx5_ib_dealloc_pd(devr->p0); 31157722f47eSHaggai Eran 31167722f47eSHaggai Eran /* Make sure no change P_Key work items are still executing */ 31177722f47eSHaggai Eran for (port = 0; port < dev->num_ports; ++port) 31187722f47eSHaggai Eran cancel_work_sync(&devr->ports[port].pkey_change_work); 3119e126ba97SEli Cohen } 3120e126ba97SEli Cohen 3121e53505a8SAchiad Shochat static u32 get_core_cap_flags(struct ib_device *ibdev) 3122e53505a8SAchiad Shochat { 3123e53505a8SAchiad Shochat struct mlx5_ib_dev *dev = to_mdev(ibdev); 3124e53505a8SAchiad Shochat enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, 1); 3125e53505a8SAchiad Shochat u8 l3_type_cap = MLX5_CAP_ROCE(dev->mdev, l3_type); 3126e53505a8SAchiad Shochat u8 roce_version_cap = MLX5_CAP_ROCE(dev->mdev, roce_version); 3127e53505a8SAchiad Shochat u32 ret = 0; 3128e53505a8SAchiad Shochat 3129e53505a8SAchiad Shochat if (ll == IB_LINK_LAYER_INFINIBAND) 3130e53505a8SAchiad Shochat return RDMA_CORE_PORT_IBA_IB; 3131e53505a8SAchiad Shochat 313272cd5717SOr Gerlitz ret = RDMA_CORE_PORT_RAW_PACKET; 313372cd5717SOr Gerlitz 3134e53505a8SAchiad Shochat if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV4_CAP)) 313572cd5717SOr Gerlitz return ret; 3136e53505a8SAchiad Shochat 3137e53505a8SAchiad Shochat if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV6_CAP)) 313872cd5717SOr Gerlitz return ret; 3139e53505a8SAchiad Shochat 3140e53505a8SAchiad Shochat if (roce_version_cap & MLX5_ROCE_VERSION_1_CAP) 3141e53505a8SAchiad Shochat ret |= RDMA_CORE_PORT_IBA_ROCE; 3142e53505a8SAchiad Shochat 3143e53505a8SAchiad Shochat if (roce_version_cap & MLX5_ROCE_VERSION_2_CAP) 3144e53505a8SAchiad Shochat ret |= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP; 3145e53505a8SAchiad Shochat 3146e53505a8SAchiad Shochat return ret; 3147e53505a8SAchiad Shochat } 3148e53505a8SAchiad Shochat 31497738613eSIra Weiny static int mlx5_port_immutable(struct ib_device *ibdev, u8 port_num, 31507738613eSIra Weiny struct ib_port_immutable *immutable) 31517738613eSIra Weiny { 31527738613eSIra Weiny struct ib_port_attr attr; 3153ca5b91d6SOr Gerlitz struct mlx5_ib_dev *dev = to_mdev(ibdev); 3154ca5b91d6SOr Gerlitz enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, port_num); 31557738613eSIra Weiny int err; 31567738613eSIra Weiny 3157c4550c63SOr Gerlitz immutable->core_cap_flags = get_core_cap_flags(ibdev); 3158c4550c63SOr Gerlitz 3159c4550c63SOr Gerlitz err = ib_query_port(ibdev, port_num, &attr); 31607738613eSIra Weiny if (err) 31617738613eSIra Weiny return err; 31627738613eSIra Weiny 31637738613eSIra Weiny immutable->pkey_tbl_len = attr.pkey_tbl_len; 31647738613eSIra Weiny immutable->gid_tbl_len = attr.gid_tbl_len; 3165e53505a8SAchiad Shochat immutable->core_cap_flags = get_core_cap_flags(ibdev); 3166ca5b91d6SOr Gerlitz if ((ll == IB_LINK_LAYER_INFINIBAND) || MLX5_CAP_GEN(dev->mdev, roce)) 3167337877a4SIra Weiny immutable->max_mad_size = IB_MGMT_MAD_SIZE; 31687738613eSIra Weiny 31697738613eSIra Weiny return 0; 31707738613eSIra Weiny } 31717738613eSIra Weiny 3172c7342823SIra Weiny static void get_dev_fw_str(struct ib_device *ibdev, char *str, 3173c7342823SIra Weiny size_t str_len) 3174c7342823SIra Weiny { 3175c7342823SIra Weiny struct mlx5_ib_dev *dev = 3176c7342823SIra Weiny container_of(ibdev, struct mlx5_ib_dev, ib_dev); 3177c7342823SIra Weiny snprintf(str, str_len, "%d.%d.%04d", fw_rev_maj(dev->mdev), 3178c7342823SIra Weiny fw_rev_min(dev->mdev), fw_rev_sub(dev->mdev)); 3179c7342823SIra Weiny } 3180c7342823SIra Weiny 318145f95acdSOr Gerlitz static int mlx5_eth_lag_init(struct mlx5_ib_dev *dev) 31829ef9c640SAviv Heller { 31839ef9c640SAviv Heller struct mlx5_core_dev *mdev = dev->mdev; 31849ef9c640SAviv Heller struct mlx5_flow_namespace *ns = mlx5_get_flow_namespace(mdev, 31859ef9c640SAviv Heller MLX5_FLOW_NAMESPACE_LAG); 31869ef9c640SAviv Heller struct mlx5_flow_table *ft; 31879ef9c640SAviv Heller int err; 31889ef9c640SAviv Heller 31899ef9c640SAviv Heller if (!ns || !mlx5_lag_is_active(mdev)) 31909ef9c640SAviv Heller return 0; 31919ef9c640SAviv Heller 31929ef9c640SAviv Heller err = mlx5_cmd_create_vport_lag(mdev); 31939ef9c640SAviv Heller if (err) 31949ef9c640SAviv Heller return err; 31959ef9c640SAviv Heller 31969ef9c640SAviv Heller ft = mlx5_create_lag_demux_flow_table(ns, 0, 0); 31979ef9c640SAviv Heller if (IS_ERR(ft)) { 31989ef9c640SAviv Heller err = PTR_ERR(ft); 31999ef9c640SAviv Heller goto err_destroy_vport_lag; 32009ef9c640SAviv Heller } 32019ef9c640SAviv Heller 32029ef9c640SAviv Heller dev->flow_db.lag_demux_ft = ft; 32039ef9c640SAviv Heller return 0; 32049ef9c640SAviv Heller 32059ef9c640SAviv Heller err_destroy_vport_lag: 32069ef9c640SAviv Heller mlx5_cmd_destroy_vport_lag(mdev); 32079ef9c640SAviv Heller return err; 32089ef9c640SAviv Heller } 32099ef9c640SAviv Heller 321045f95acdSOr Gerlitz static void mlx5_eth_lag_cleanup(struct mlx5_ib_dev *dev) 32119ef9c640SAviv Heller { 32129ef9c640SAviv Heller struct mlx5_core_dev *mdev = dev->mdev; 32139ef9c640SAviv Heller 32149ef9c640SAviv Heller if (dev->flow_db.lag_demux_ft) { 32159ef9c640SAviv Heller mlx5_destroy_flow_table(dev->flow_db.lag_demux_ft); 32169ef9c640SAviv Heller dev->flow_db.lag_demux_ft = NULL; 32179ef9c640SAviv Heller 32189ef9c640SAviv Heller mlx5_cmd_destroy_vport_lag(mdev); 32199ef9c640SAviv Heller } 32209ef9c640SAviv Heller } 32219ef9c640SAviv Heller 3222d012f5d6SOr Gerlitz static int mlx5_add_netdev_notifier(struct mlx5_ib_dev *dev) 3223fc24fc5eSAchiad Shochat { 3224e53505a8SAchiad Shochat int err; 3225e53505a8SAchiad Shochat 3226fc24fc5eSAchiad Shochat dev->roce.nb.notifier_call = mlx5_netdev_event; 3227e53505a8SAchiad Shochat err = register_netdevice_notifier(&dev->roce.nb); 32285ec8c83eSAviv Heller if (err) { 32295ec8c83eSAviv Heller dev->roce.nb.notifier_call = NULL; 3230e53505a8SAchiad Shochat return err; 32315ec8c83eSAviv Heller } 3232e53505a8SAchiad Shochat 3233d012f5d6SOr Gerlitz return 0; 3234d012f5d6SOr Gerlitz } 3235d012f5d6SOr Gerlitz 3236d012f5d6SOr Gerlitz static void mlx5_remove_netdev_notifier(struct mlx5_ib_dev *dev) 3237e126ba97SEli Cohen { 3238e126ba97SEli Cohen if (dev->roce.nb.notifier_call) { 3239e126ba97SEli Cohen unregister_netdevice_notifier(&dev->roce.nb); 3240e126ba97SEli Cohen dev->roce.nb.notifier_call = NULL; 3241e126ba97SEli Cohen } 3242e126ba97SEli Cohen } 3243e126ba97SEli Cohen 324445f95acdSOr Gerlitz static int mlx5_enable_eth(struct mlx5_ib_dev *dev) 3245e126ba97SEli Cohen { 3246e126ba97SEli Cohen int err; 3247e126ba97SEli Cohen 3248d012f5d6SOr Gerlitz err = mlx5_add_netdev_notifier(dev); 3249d012f5d6SOr Gerlitz if (err) 3250e53505a8SAchiad Shochat return err; 3251e53505a8SAchiad Shochat 3252ca5b91d6SOr Gerlitz if (MLX5_CAP_GEN(dev->mdev, roce)) { 3253e53505a8SAchiad Shochat err = mlx5_nic_vport_enable_roce(dev->mdev); 3254e53505a8SAchiad Shochat if (err) 3255e53505a8SAchiad Shochat goto err_unregister_netdevice_notifier; 3256ca5b91d6SOr Gerlitz } 3257e53505a8SAchiad Shochat 325845f95acdSOr Gerlitz err = mlx5_eth_lag_init(dev); 32599ef9c640SAviv Heller if (err) 32609ef9c640SAviv Heller goto err_disable_roce; 32619ef9c640SAviv Heller 3262e53505a8SAchiad Shochat return 0; 3263e53505a8SAchiad Shochat 32649ef9c640SAviv Heller err_disable_roce: 3265ca5b91d6SOr Gerlitz if (MLX5_CAP_GEN(dev->mdev, roce)) 32669ef9c640SAviv Heller mlx5_nic_vport_disable_roce(dev->mdev); 32679ef9c640SAviv Heller 3268e53505a8SAchiad Shochat err_unregister_netdevice_notifier: 3269d012f5d6SOr Gerlitz mlx5_remove_netdev_notifier(dev); 3270e53505a8SAchiad Shochat return err; 3271fc24fc5eSAchiad Shochat } 3272fc24fc5eSAchiad Shochat 327345f95acdSOr Gerlitz static void mlx5_disable_eth(struct mlx5_ib_dev *dev) 3274fc24fc5eSAchiad Shochat { 327545f95acdSOr Gerlitz mlx5_eth_lag_cleanup(dev); 3276ca5b91d6SOr Gerlitz if (MLX5_CAP_GEN(dev->mdev, roce)) 3277e53505a8SAchiad Shochat mlx5_nic_vport_disable_roce(dev->mdev); 3278fc24fc5eSAchiad Shochat } 3279fc24fc5eSAchiad Shochat 3280e1f24a79SParav Pandit struct mlx5_ib_counter { 32817c16f477SKamal Heib const char *name; 32827c16f477SKamal Heib size_t offset; 32837c16f477SKamal Heib }; 32847c16f477SKamal Heib 32857c16f477SKamal Heib #define INIT_Q_COUNTER(_name) \ 32867c16f477SKamal Heib { .name = #_name, .offset = MLX5_BYTE_OFF(query_q_counter_out, _name)} 32877c16f477SKamal Heib 3288e1f24a79SParav Pandit static const struct mlx5_ib_counter basic_q_cnts[] = { 32897c16f477SKamal Heib INIT_Q_COUNTER(rx_write_requests), 32907c16f477SKamal Heib INIT_Q_COUNTER(rx_read_requests), 32917c16f477SKamal Heib INIT_Q_COUNTER(rx_atomic_requests), 32927c16f477SKamal Heib INIT_Q_COUNTER(out_of_buffer), 32937c16f477SKamal Heib }; 32947c16f477SKamal Heib 3295e1f24a79SParav Pandit static const struct mlx5_ib_counter out_of_seq_q_cnts[] = { 32967c16f477SKamal Heib INIT_Q_COUNTER(out_of_sequence), 32977c16f477SKamal Heib }; 32987c16f477SKamal Heib 3299e1f24a79SParav Pandit static const struct mlx5_ib_counter retrans_q_cnts[] = { 33007c16f477SKamal Heib INIT_Q_COUNTER(duplicate_request), 33017c16f477SKamal Heib INIT_Q_COUNTER(rnr_nak_retry_err), 33027c16f477SKamal Heib INIT_Q_COUNTER(packet_seq_err), 33037c16f477SKamal Heib INIT_Q_COUNTER(implied_nak_seq_err), 33047c16f477SKamal Heib INIT_Q_COUNTER(local_ack_timeout_err), 33057c16f477SKamal Heib }; 33067c16f477SKamal Heib 3307e1f24a79SParav Pandit #define INIT_CONG_COUNTER(_name) \ 3308e1f24a79SParav Pandit { .name = #_name, .offset = \ 3309e1f24a79SParav Pandit MLX5_BYTE_OFF(query_cong_statistics_out, _name ## _high)} 3310e1f24a79SParav Pandit 3311e1f24a79SParav Pandit static const struct mlx5_ib_counter cong_cnts[] = { 3312e1f24a79SParav Pandit INIT_CONG_COUNTER(rp_cnp_ignored), 3313e1f24a79SParav Pandit INIT_CONG_COUNTER(rp_cnp_handled), 3314e1f24a79SParav Pandit INIT_CONG_COUNTER(np_ecn_marked_roce_packets), 3315e1f24a79SParav Pandit INIT_CONG_COUNTER(np_cnp_sent), 3316e1f24a79SParav Pandit }; 3317e1f24a79SParav Pandit 3318e1f24a79SParav Pandit static void mlx5_ib_dealloc_counters(struct mlx5_ib_dev *dev) 33190837e86aSMark Bloch { 33200837e86aSMark Bloch unsigned int i; 33210837e86aSMark Bloch 33227c16f477SKamal Heib for (i = 0; i < dev->num_ports; i++) { 33230837e86aSMark Bloch mlx5_core_dealloc_q_counter(dev->mdev, 3324e1f24a79SParav Pandit dev->port[i].cnts.set_id); 3325e1f24a79SParav Pandit kfree(dev->port[i].cnts.names); 3326e1f24a79SParav Pandit kfree(dev->port[i].cnts.offsets); 33277c16f477SKamal Heib } 33287c16f477SKamal Heib } 33297c16f477SKamal Heib 3330e1f24a79SParav Pandit static int __mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev, 3331e1f24a79SParav Pandit struct mlx5_ib_counters *cnts) 33327c16f477SKamal Heib { 33337c16f477SKamal Heib u32 num_counters; 33347c16f477SKamal Heib 33357c16f477SKamal Heib num_counters = ARRAY_SIZE(basic_q_cnts); 33367c16f477SKamal Heib 33377c16f477SKamal Heib if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt)) 33387c16f477SKamal Heib num_counters += ARRAY_SIZE(out_of_seq_q_cnts); 33397c16f477SKamal Heib 33407c16f477SKamal Heib if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters)) 33417c16f477SKamal Heib num_counters += ARRAY_SIZE(retrans_q_cnts); 3342e1f24a79SParav Pandit cnts->num_q_counters = num_counters; 33437c16f477SKamal Heib 3344e1f24a79SParav Pandit if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) { 3345e1f24a79SParav Pandit cnts->num_cong_counters = ARRAY_SIZE(cong_cnts); 3346e1f24a79SParav Pandit num_counters += ARRAY_SIZE(cong_cnts); 3347e1f24a79SParav Pandit } 3348e1f24a79SParav Pandit 3349e1f24a79SParav Pandit cnts->names = kcalloc(num_counters, sizeof(cnts->names), GFP_KERNEL); 3350e1f24a79SParav Pandit if (!cnts->names) 33517c16f477SKamal Heib return -ENOMEM; 33527c16f477SKamal Heib 3353e1f24a79SParav Pandit cnts->offsets = kcalloc(num_counters, 3354e1f24a79SParav Pandit sizeof(cnts->offsets), GFP_KERNEL); 3355e1f24a79SParav Pandit if (!cnts->offsets) 33567c16f477SKamal Heib goto err_names; 33577c16f477SKamal Heib 33587c16f477SKamal Heib return 0; 33597c16f477SKamal Heib 33607c16f477SKamal Heib err_names: 3361e1f24a79SParav Pandit kfree(cnts->names); 33627c16f477SKamal Heib return -ENOMEM; 33637c16f477SKamal Heib } 33647c16f477SKamal Heib 3365e1f24a79SParav Pandit static void mlx5_ib_fill_counters(struct mlx5_ib_dev *dev, 33667c16f477SKamal Heib const char **names, 33677c16f477SKamal Heib size_t *offsets) 33687c16f477SKamal Heib { 33697c16f477SKamal Heib int i; 33707c16f477SKamal Heib int j = 0; 33717c16f477SKamal Heib 33727c16f477SKamal Heib for (i = 0; i < ARRAY_SIZE(basic_q_cnts); i++, j++) { 33737c16f477SKamal Heib names[j] = basic_q_cnts[i].name; 33747c16f477SKamal Heib offsets[j] = basic_q_cnts[i].offset; 33757c16f477SKamal Heib } 33767c16f477SKamal Heib 33777c16f477SKamal Heib if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt)) { 33787c16f477SKamal Heib for (i = 0; i < ARRAY_SIZE(out_of_seq_q_cnts); i++, j++) { 33797c16f477SKamal Heib names[j] = out_of_seq_q_cnts[i].name; 33807c16f477SKamal Heib offsets[j] = out_of_seq_q_cnts[i].offset; 33817c16f477SKamal Heib } 33827c16f477SKamal Heib } 33837c16f477SKamal Heib 33847c16f477SKamal Heib if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters)) { 33857c16f477SKamal Heib for (i = 0; i < ARRAY_SIZE(retrans_q_cnts); i++, j++) { 33867c16f477SKamal Heib names[j] = retrans_q_cnts[i].name; 33877c16f477SKamal Heib offsets[j] = retrans_q_cnts[i].offset; 33887c16f477SKamal Heib } 33897c16f477SKamal Heib } 3390e1f24a79SParav Pandit 3391e1f24a79SParav Pandit if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) { 3392e1f24a79SParav Pandit for (i = 0; i < ARRAY_SIZE(cong_cnts); i++, j++) { 3393e1f24a79SParav Pandit names[j] = cong_cnts[i].name; 3394e1f24a79SParav Pandit offsets[j] = cong_cnts[i].offset; 3395e1f24a79SParav Pandit } 3396e1f24a79SParav Pandit } 33970837e86aSMark Bloch } 33980837e86aSMark Bloch 3399e1f24a79SParav Pandit static int mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev) 34000837e86aSMark Bloch { 34010837e86aSMark Bloch int i; 34020837e86aSMark Bloch int ret; 34030837e86aSMark Bloch 34040837e86aSMark Bloch for (i = 0; i < dev->num_ports; i++) { 34057c16f477SKamal Heib struct mlx5_ib_port *port = &dev->port[i]; 34067c16f477SKamal Heib 34070837e86aSMark Bloch ret = mlx5_core_alloc_q_counter(dev->mdev, 3408e1f24a79SParav Pandit &port->cnts.set_id); 34090837e86aSMark Bloch if (ret) { 34100837e86aSMark Bloch mlx5_ib_warn(dev, 34110837e86aSMark Bloch "couldn't allocate queue counter for port %d, err %d\n", 34120837e86aSMark Bloch i + 1, ret); 34130837e86aSMark Bloch goto dealloc_counters; 34140837e86aSMark Bloch } 34157c16f477SKamal Heib 3416e1f24a79SParav Pandit ret = __mlx5_ib_alloc_counters(dev, &port->cnts); 34177c16f477SKamal Heib if (ret) 34187c16f477SKamal Heib goto dealloc_counters; 34197c16f477SKamal Heib 3420e1f24a79SParav Pandit mlx5_ib_fill_counters(dev, port->cnts.names, 3421e1f24a79SParav Pandit port->cnts.offsets); 34220837e86aSMark Bloch } 34230837e86aSMark Bloch 34240837e86aSMark Bloch return 0; 34250837e86aSMark Bloch 34260837e86aSMark Bloch dealloc_counters: 34270837e86aSMark Bloch while (--i >= 0) 34280837e86aSMark Bloch mlx5_core_dealloc_q_counter(dev->mdev, 3429e1f24a79SParav Pandit dev->port[i].cnts.set_id); 34300837e86aSMark Bloch 34310837e86aSMark Bloch return ret; 34320837e86aSMark Bloch } 34330837e86aSMark Bloch 34340ad17a8fSMark Bloch static struct rdma_hw_stats *mlx5_ib_alloc_hw_stats(struct ib_device *ibdev, 34350ad17a8fSMark Bloch u8 port_num) 34360ad17a8fSMark Bloch { 34377c16f477SKamal Heib struct mlx5_ib_dev *dev = to_mdev(ibdev); 34387c16f477SKamal Heib struct mlx5_ib_port *port = &dev->port[port_num - 1]; 34390ad17a8fSMark Bloch 34400ad17a8fSMark Bloch /* We support only per port stats */ 34410ad17a8fSMark Bloch if (port_num == 0) 34420ad17a8fSMark Bloch return NULL; 34430ad17a8fSMark Bloch 3444e1f24a79SParav Pandit return rdma_alloc_hw_stats_struct(port->cnts.names, 3445e1f24a79SParav Pandit port->cnts.num_q_counters + 3446e1f24a79SParav Pandit port->cnts.num_cong_counters, 34470ad17a8fSMark Bloch RDMA_HW_STATS_DEFAULT_LIFESPAN); 34480ad17a8fSMark Bloch } 34490ad17a8fSMark Bloch 3450e1f24a79SParav Pandit static int mlx5_ib_query_q_counters(struct mlx5_ib_dev *dev, 3451e1f24a79SParav Pandit struct mlx5_ib_port *port, 3452e1f24a79SParav Pandit struct rdma_hw_stats *stats) 3453e1f24a79SParav Pandit { 3454e1f24a79SParav Pandit int outlen = MLX5_ST_SZ_BYTES(query_q_counter_out); 3455e1f24a79SParav Pandit void *out; 3456e1f24a79SParav Pandit __be32 val; 3457e1f24a79SParav Pandit int ret, i; 3458e1f24a79SParav Pandit 3459*1b9a07eeSLeon Romanovsky out = kvzalloc(outlen, GFP_KERNEL); 3460e1f24a79SParav Pandit if (!out) 3461e1f24a79SParav Pandit return -ENOMEM; 3462e1f24a79SParav Pandit 3463e1f24a79SParav Pandit ret = mlx5_core_query_q_counter(dev->mdev, 3464e1f24a79SParav Pandit port->cnts.set_id, 0, 3465e1f24a79SParav Pandit out, outlen); 3466e1f24a79SParav Pandit if (ret) 3467e1f24a79SParav Pandit goto free; 3468e1f24a79SParav Pandit 3469e1f24a79SParav Pandit for (i = 0; i < port->cnts.num_q_counters; i++) { 3470e1f24a79SParav Pandit val = *(__be32 *)(out + port->cnts.offsets[i]); 3471e1f24a79SParav Pandit stats->value[i] = (u64)be32_to_cpu(val); 3472e1f24a79SParav Pandit } 3473e1f24a79SParav Pandit 3474e1f24a79SParav Pandit free: 3475e1f24a79SParav Pandit kvfree(out); 3476e1f24a79SParav Pandit return ret; 3477e1f24a79SParav Pandit } 3478e1f24a79SParav Pandit 3479e1f24a79SParav Pandit static int mlx5_ib_query_cong_counters(struct mlx5_ib_dev *dev, 3480e1f24a79SParav Pandit struct mlx5_ib_port *port, 3481e1f24a79SParav Pandit struct rdma_hw_stats *stats) 3482e1f24a79SParav Pandit { 3483e1f24a79SParav Pandit int outlen = MLX5_ST_SZ_BYTES(query_cong_statistics_out); 3484e1f24a79SParav Pandit void *out; 3485e1f24a79SParav Pandit int ret, i; 3486e1f24a79SParav Pandit int offset = port->cnts.num_q_counters; 3487e1f24a79SParav Pandit 3488*1b9a07eeSLeon Romanovsky out = kvzalloc(outlen, GFP_KERNEL); 3489e1f24a79SParav Pandit if (!out) 3490e1f24a79SParav Pandit return -ENOMEM; 3491e1f24a79SParav Pandit 3492e1f24a79SParav Pandit ret = mlx5_cmd_query_cong_counter(dev->mdev, false, out, outlen); 3493e1f24a79SParav Pandit if (ret) 3494e1f24a79SParav Pandit goto free; 3495e1f24a79SParav Pandit 3496e1f24a79SParav Pandit for (i = 0; i < port->cnts.num_cong_counters; i++) { 3497e1f24a79SParav Pandit stats->value[i + offset] = 3498e1f24a79SParav Pandit be64_to_cpup((__be64 *)(out + 3499e1f24a79SParav Pandit port->cnts.offsets[i + offset])); 3500e1f24a79SParav Pandit } 3501e1f24a79SParav Pandit 3502e1f24a79SParav Pandit free: 3503e1f24a79SParav Pandit kvfree(out); 3504e1f24a79SParav Pandit return ret; 3505e1f24a79SParav Pandit } 3506e1f24a79SParav Pandit 35070ad17a8fSMark Bloch static int mlx5_ib_get_hw_stats(struct ib_device *ibdev, 35080ad17a8fSMark Bloch struct rdma_hw_stats *stats, 35097c16f477SKamal Heib u8 port_num, int index) 35100ad17a8fSMark Bloch { 35110ad17a8fSMark Bloch struct mlx5_ib_dev *dev = to_mdev(ibdev); 35127c16f477SKamal Heib struct mlx5_ib_port *port = &dev->port[port_num - 1]; 3513e1f24a79SParav Pandit int ret, num_counters; 35140ad17a8fSMark Bloch 35157c16f477SKamal Heib if (!stats) 3516e1f24a79SParav Pandit return -EINVAL; 35170ad17a8fSMark Bloch 3518e1f24a79SParav Pandit ret = mlx5_ib_query_q_counters(dev, port, stats); 35190ad17a8fSMark Bloch if (ret) 3520e1f24a79SParav Pandit return ret; 3521e1f24a79SParav Pandit num_counters = port->cnts.num_q_counters; 35220ad17a8fSMark Bloch 3523e1f24a79SParav Pandit if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) { 3524e1f24a79SParav Pandit ret = mlx5_ib_query_cong_counters(dev, port, stats); 3525e1f24a79SParav Pandit if (ret) 3526e1f24a79SParav Pandit return ret; 3527e1f24a79SParav Pandit num_counters += port->cnts.num_cong_counters; 35280ad17a8fSMark Bloch } 35297c16f477SKamal Heib 3530e1f24a79SParav Pandit return num_counters; 35310ad17a8fSMark Bloch } 35320ad17a8fSMark Bloch 3533693dfd5aSErez Shitrit static struct net_device* 3534693dfd5aSErez Shitrit mlx5_ib_alloc_rdma_netdev(struct ib_device *hca, 3535693dfd5aSErez Shitrit u8 port_num, 3536693dfd5aSErez Shitrit enum rdma_netdev_t type, 3537693dfd5aSErez Shitrit const char *name, 3538693dfd5aSErez Shitrit unsigned char name_assign_type, 3539693dfd5aSErez Shitrit void (*setup)(struct net_device *)) 3540693dfd5aSErez Shitrit { 3541693dfd5aSErez Shitrit if (type != RDMA_NETDEV_IPOIB) 3542693dfd5aSErez Shitrit return ERR_PTR(-EOPNOTSUPP); 3543693dfd5aSErez Shitrit 3544693dfd5aSErez Shitrit return mlx5_rdma_netdev_alloc(to_mdev(hca)->mdev, hca, 3545693dfd5aSErez Shitrit name, setup); 3546693dfd5aSErez Shitrit } 3547693dfd5aSErez Shitrit 3548693dfd5aSErez Shitrit static void mlx5_ib_free_rdma_netdev(struct net_device *netdev) 3549693dfd5aSErez Shitrit { 3550693dfd5aSErez Shitrit return mlx5_rdma_netdev_free(netdev); 3551693dfd5aSErez Shitrit } 3552693dfd5aSErez Shitrit 35539603b61dSJack Morgenstein static void *mlx5_ib_add(struct mlx5_core_dev *mdev) 3554e126ba97SEli Cohen { 3555e126ba97SEli Cohen struct mlx5_ib_dev *dev; 3556ebd61f68SAchiad Shochat enum rdma_link_layer ll; 3557ebd61f68SAchiad Shochat int port_type_cap; 35584babcf97SAviv Heller const char *name; 3559e126ba97SEli Cohen int err; 3560e126ba97SEli Cohen int i; 3561e126ba97SEli Cohen 3562ebd61f68SAchiad Shochat port_type_cap = MLX5_CAP_GEN(mdev, port_type); 3563ebd61f68SAchiad Shochat ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap); 3564ebd61f68SAchiad Shochat 3565e126ba97SEli Cohen printk_once(KERN_INFO "%s", mlx5_version); 3566e126ba97SEli Cohen 3567e126ba97SEli Cohen dev = (struct mlx5_ib_dev *)ib_alloc_device(sizeof(*dev)); 3568e126ba97SEli Cohen if (!dev) 35699603b61dSJack Morgenstein return NULL; 3570e126ba97SEli Cohen 35719603b61dSJack Morgenstein dev->mdev = mdev; 3572e126ba97SEli Cohen 35730837e86aSMark Bloch dev->port = kcalloc(MLX5_CAP_GEN(mdev, num_ports), sizeof(*dev->port), 35740837e86aSMark Bloch GFP_KERNEL); 35750837e86aSMark Bloch if (!dev->port) 35760837e86aSMark Bloch goto err_dealloc; 35770837e86aSMark Bloch 3578fc24fc5eSAchiad Shochat rwlock_init(&dev->roce.netdev_lock); 3579e126ba97SEli Cohen err = get_port_caps(dev); 3580e126ba97SEli Cohen if (err) 35810837e86aSMark Bloch goto err_free_port; 3582e126ba97SEli Cohen 35831b5daf11SMajd Dibbiny if (mlx5_use_mad_ifc(dev)) 3584e126ba97SEli Cohen get_ext_port_caps(dev); 3585e126ba97SEli Cohen 35864babcf97SAviv Heller if (!mlx5_lag_is_active(mdev)) 35874babcf97SAviv Heller name = "mlx5_%d"; 35884babcf97SAviv Heller else 35894babcf97SAviv Heller name = "mlx5_bond_%d"; 35904babcf97SAviv Heller 35914babcf97SAviv Heller strlcpy(dev->ib_dev.name, name, IB_DEVICE_NAME_MAX); 3592e126ba97SEli Cohen dev->ib_dev.owner = THIS_MODULE; 3593e126ba97SEli Cohen dev->ib_dev.node_type = RDMA_NODE_IB_CA; 3594c6790aa9SSagi Grimberg dev->ib_dev.local_dma_lkey = 0 /* not supported for now */; 3595938fe83cSSaeed Mahameed dev->num_ports = MLX5_CAP_GEN(mdev, num_ports); 3596e126ba97SEli Cohen dev->ib_dev.phys_port_cnt = dev->num_ports; 3597233d05d2SSaeed Mahameed dev->ib_dev.num_comp_vectors = 3598233d05d2SSaeed Mahameed dev->mdev->priv.eq_table.num_comp_vectors; 35999b0c289eSBart Van Assche dev->ib_dev.dev.parent = &mdev->pdev->dev; 3600e126ba97SEli Cohen 3601e126ba97SEli Cohen dev->ib_dev.uverbs_abi_ver = MLX5_IB_UVERBS_ABI_VERSION; 3602e126ba97SEli Cohen dev->ib_dev.uverbs_cmd_mask = 3603e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) | 3604e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) | 3605e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_QUERY_PORT) | 3606e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_ALLOC_PD) | 3607e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) | 360841c450fdSMoni Shoua (1ull << IB_USER_VERBS_CMD_CREATE_AH) | 360941c450fdSMoni Shoua (1ull << IB_USER_VERBS_CMD_DESTROY_AH) | 3610e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_REG_MR) | 361156e11d62SNoa Osherovich (1ull << IB_USER_VERBS_CMD_REREG_MR) | 3612e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_DEREG_MR) | 3613e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) | 3614e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_CREATE_CQ) | 3615e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_RESIZE_CQ) | 3616e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) | 3617e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_CREATE_QP) | 3618e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_MODIFY_QP) | 3619e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_QUERY_QP) | 3620e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_DESTROY_QP) | 3621e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST) | 3622e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_DETACH_MCAST) | 3623e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_CREATE_SRQ) | 3624e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ) | 3625e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_QUERY_SRQ) | 3626e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ) | 3627e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_CREATE_XSRQ) | 3628e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_OPEN_QP); 36291707cb4aSHaggai Eran dev->ib_dev.uverbs_ex_cmd_mask = 3630d4584ddfSMatan Barak (1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE) | 3631d4584ddfSMatan Barak (1ull << IB_USER_VERBS_EX_CMD_CREATE_CQ) | 36327d29f349SBodong Wang (1ull << IB_USER_VERBS_EX_CMD_CREATE_QP) | 36337d29f349SBodong Wang (1ull << IB_USER_VERBS_EX_CMD_MODIFY_QP); 3634e126ba97SEli Cohen 3635e126ba97SEli Cohen dev->ib_dev.query_device = mlx5_ib_query_device; 3636e126ba97SEli Cohen dev->ib_dev.query_port = mlx5_ib_query_port; 3637ebd61f68SAchiad Shochat dev->ib_dev.get_link_layer = mlx5_ib_port_link_layer; 3638fc24fc5eSAchiad Shochat if (ll == IB_LINK_LAYER_ETHERNET) 3639fc24fc5eSAchiad Shochat dev->ib_dev.get_netdev = mlx5_ib_get_netdev; 3640e126ba97SEli Cohen dev->ib_dev.query_gid = mlx5_ib_query_gid; 36413cca2606SAchiad Shochat dev->ib_dev.add_gid = mlx5_ib_add_gid; 36423cca2606SAchiad Shochat dev->ib_dev.del_gid = mlx5_ib_del_gid; 3643e126ba97SEli Cohen dev->ib_dev.query_pkey = mlx5_ib_query_pkey; 3644e126ba97SEli Cohen dev->ib_dev.modify_device = mlx5_ib_modify_device; 3645e126ba97SEli Cohen dev->ib_dev.modify_port = mlx5_ib_modify_port; 3646e126ba97SEli Cohen dev->ib_dev.alloc_ucontext = mlx5_ib_alloc_ucontext; 3647e126ba97SEli Cohen dev->ib_dev.dealloc_ucontext = mlx5_ib_dealloc_ucontext; 3648e126ba97SEli Cohen dev->ib_dev.mmap = mlx5_ib_mmap; 3649e126ba97SEli Cohen dev->ib_dev.alloc_pd = mlx5_ib_alloc_pd; 3650e126ba97SEli Cohen dev->ib_dev.dealloc_pd = mlx5_ib_dealloc_pd; 3651e126ba97SEli Cohen dev->ib_dev.create_ah = mlx5_ib_create_ah; 3652e126ba97SEli Cohen dev->ib_dev.query_ah = mlx5_ib_query_ah; 3653e126ba97SEli Cohen dev->ib_dev.destroy_ah = mlx5_ib_destroy_ah; 3654e126ba97SEli Cohen dev->ib_dev.create_srq = mlx5_ib_create_srq; 3655e126ba97SEli Cohen dev->ib_dev.modify_srq = mlx5_ib_modify_srq; 3656e126ba97SEli Cohen dev->ib_dev.query_srq = mlx5_ib_query_srq; 3657e126ba97SEli Cohen dev->ib_dev.destroy_srq = mlx5_ib_destroy_srq; 3658e126ba97SEli Cohen dev->ib_dev.post_srq_recv = mlx5_ib_post_srq_recv; 3659e126ba97SEli Cohen dev->ib_dev.create_qp = mlx5_ib_create_qp; 3660e126ba97SEli Cohen dev->ib_dev.modify_qp = mlx5_ib_modify_qp; 3661e126ba97SEli Cohen dev->ib_dev.query_qp = mlx5_ib_query_qp; 3662e126ba97SEli Cohen dev->ib_dev.destroy_qp = mlx5_ib_destroy_qp; 3663e126ba97SEli Cohen dev->ib_dev.post_send = mlx5_ib_post_send; 3664e126ba97SEli Cohen dev->ib_dev.post_recv = mlx5_ib_post_recv; 3665e126ba97SEli Cohen dev->ib_dev.create_cq = mlx5_ib_create_cq; 3666e126ba97SEli Cohen dev->ib_dev.modify_cq = mlx5_ib_modify_cq; 3667e126ba97SEli Cohen dev->ib_dev.resize_cq = mlx5_ib_resize_cq; 3668e126ba97SEli Cohen dev->ib_dev.destroy_cq = mlx5_ib_destroy_cq; 3669e126ba97SEli Cohen dev->ib_dev.poll_cq = mlx5_ib_poll_cq; 3670e126ba97SEli Cohen dev->ib_dev.req_notify_cq = mlx5_ib_arm_cq; 3671e126ba97SEli Cohen dev->ib_dev.get_dma_mr = mlx5_ib_get_dma_mr; 3672e126ba97SEli Cohen dev->ib_dev.reg_user_mr = mlx5_ib_reg_user_mr; 367356e11d62SNoa Osherovich dev->ib_dev.rereg_user_mr = mlx5_ib_rereg_user_mr; 3674e126ba97SEli Cohen dev->ib_dev.dereg_mr = mlx5_ib_dereg_mr; 3675e126ba97SEli Cohen dev->ib_dev.attach_mcast = mlx5_ib_mcg_attach; 3676e126ba97SEli Cohen dev->ib_dev.detach_mcast = mlx5_ib_mcg_detach; 3677e126ba97SEli Cohen dev->ib_dev.process_mad = mlx5_ib_process_mad; 36789bee178bSSagi Grimberg dev->ib_dev.alloc_mr = mlx5_ib_alloc_mr; 36798a187ee5SSagi Grimberg dev->ib_dev.map_mr_sg = mlx5_ib_map_mr_sg; 3680d5436ba0SSagi Grimberg dev->ib_dev.check_mr_status = mlx5_ib_check_mr_status; 36817738613eSIra Weiny dev->ib_dev.get_port_immutable = mlx5_port_immutable; 3682c7342823SIra Weiny dev->ib_dev.get_dev_fw_str = get_dev_fw_str; 3683693dfd5aSErez Shitrit dev->ib_dev.alloc_rdma_netdev = mlx5_ib_alloc_rdma_netdev; 3684693dfd5aSErez Shitrit dev->ib_dev.free_rdma_netdev = mlx5_ib_free_rdma_netdev; 3685eff901d3SEli Cohen if (mlx5_core_is_pf(mdev)) { 3686eff901d3SEli Cohen dev->ib_dev.get_vf_config = mlx5_ib_get_vf_config; 3687eff901d3SEli Cohen dev->ib_dev.set_vf_link_state = mlx5_ib_set_vf_link_state; 3688eff901d3SEli Cohen dev->ib_dev.get_vf_stats = mlx5_ib_get_vf_stats; 3689eff901d3SEli Cohen dev->ib_dev.set_vf_guid = mlx5_ib_set_vf_guid; 3690eff901d3SEli Cohen } 3691e126ba97SEli Cohen 36927c2344c3SMaor Gottlieb dev->ib_dev.disassociate_ucontext = mlx5_ib_disassociate_ucontext; 36937c2344c3SMaor Gottlieb 3694938fe83cSSaeed Mahameed mlx5_ib_internal_fill_odp_caps(dev); 36958cdd312cSHaggai Eran 3696d2370e0aSMatan Barak if (MLX5_CAP_GEN(mdev, imaicl)) { 3697d2370e0aSMatan Barak dev->ib_dev.alloc_mw = mlx5_ib_alloc_mw; 3698d2370e0aSMatan Barak dev->ib_dev.dealloc_mw = mlx5_ib_dealloc_mw; 3699d2370e0aSMatan Barak dev->ib_dev.uverbs_cmd_mask |= 3700d2370e0aSMatan Barak (1ull << IB_USER_VERBS_CMD_ALLOC_MW) | 3701d2370e0aSMatan Barak (1ull << IB_USER_VERBS_CMD_DEALLOC_MW); 3702d2370e0aSMatan Barak } 3703d2370e0aSMatan Barak 37047c16f477SKamal Heib if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt)) { 37050ad17a8fSMark Bloch dev->ib_dev.get_hw_stats = mlx5_ib_get_hw_stats; 37060ad17a8fSMark Bloch dev->ib_dev.alloc_hw_stats = mlx5_ib_alloc_hw_stats; 37070ad17a8fSMark Bloch } 37080ad17a8fSMark Bloch 3709938fe83cSSaeed Mahameed if (MLX5_CAP_GEN(mdev, xrc)) { 3710e126ba97SEli Cohen dev->ib_dev.alloc_xrcd = mlx5_ib_alloc_xrcd; 3711e126ba97SEli Cohen dev->ib_dev.dealloc_xrcd = mlx5_ib_dealloc_xrcd; 3712e126ba97SEli Cohen dev->ib_dev.uverbs_cmd_mask |= 3713e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_OPEN_XRCD) | 3714e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_CLOSE_XRCD); 3715e126ba97SEli Cohen } 3716e126ba97SEli Cohen 3717048ccca8SLinus Torvalds if (mlx5_ib_port_link_layer(&dev->ib_dev, 1) == 3718038d2ef8SMaor Gottlieb IB_LINK_LAYER_ETHERNET) { 3719038d2ef8SMaor Gottlieb dev->ib_dev.create_flow = mlx5_ib_create_flow; 3720038d2ef8SMaor Gottlieb dev->ib_dev.destroy_flow = mlx5_ib_destroy_flow; 372179b20a6cSYishai Hadas dev->ib_dev.create_wq = mlx5_ib_create_wq; 372279b20a6cSYishai Hadas dev->ib_dev.modify_wq = mlx5_ib_modify_wq; 372379b20a6cSYishai Hadas dev->ib_dev.destroy_wq = mlx5_ib_destroy_wq; 3724c5f90929SYishai Hadas dev->ib_dev.create_rwq_ind_table = mlx5_ib_create_rwq_ind_table; 3725c5f90929SYishai Hadas dev->ib_dev.destroy_rwq_ind_table = mlx5_ib_destroy_rwq_ind_table; 3726038d2ef8SMaor Gottlieb dev->ib_dev.uverbs_ex_cmd_mask |= 3727038d2ef8SMaor Gottlieb (1ull << IB_USER_VERBS_EX_CMD_CREATE_FLOW) | 372879b20a6cSYishai Hadas (1ull << IB_USER_VERBS_EX_CMD_DESTROY_FLOW) | 372979b20a6cSYishai Hadas (1ull << IB_USER_VERBS_EX_CMD_CREATE_WQ) | 373079b20a6cSYishai Hadas (1ull << IB_USER_VERBS_EX_CMD_MODIFY_WQ) | 3731c5f90929SYishai Hadas (1ull << IB_USER_VERBS_EX_CMD_DESTROY_WQ) | 3732c5f90929SYishai Hadas (1ull << IB_USER_VERBS_EX_CMD_CREATE_RWQ_IND_TBL) | 3733c5f90929SYishai Hadas (1ull << IB_USER_VERBS_EX_CMD_DESTROY_RWQ_IND_TBL); 3734038d2ef8SMaor Gottlieb } 3735e126ba97SEli Cohen err = init_node_data(dev); 3736e126ba97SEli Cohen if (err) 373790be7c8aSMajd Dibbiny goto err_free_port; 3738e126ba97SEli Cohen 3739038d2ef8SMaor Gottlieb mutex_init(&dev->flow_db.lock); 3740e126ba97SEli Cohen mutex_init(&dev->cap_mask_mutex); 374189ea94a7SMaor Gottlieb INIT_LIST_HEAD(&dev->qp_list); 374289ea94a7SMaor Gottlieb spin_lock_init(&dev->reset_flow_resource_lock); 3743e126ba97SEli Cohen 3744fc24fc5eSAchiad Shochat if (ll == IB_LINK_LAYER_ETHERNET) { 374545f95acdSOr Gerlitz err = mlx5_enable_eth(dev); 3746e126ba97SEli Cohen if (err) 374790be7c8aSMajd Dibbiny goto err_free_port; 3748fc24fc5eSAchiad Shochat } 3749fc24fc5eSAchiad Shochat 3750fc24fc5eSAchiad Shochat err = create_dev_resources(&dev->devr); 3751fc24fc5eSAchiad Shochat if (err) 375245f95acdSOr Gerlitz goto err_disable_eth; 3753e126ba97SEli Cohen 37546aec21f6SHaggai Eran err = mlx5_ib_odp_init_one(dev); 3755281d1a92SWei Yongjun if (err) 3756e126ba97SEli Cohen goto err_rsrc; 3757e126ba97SEli Cohen 375845bded2cSKamal Heib if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt)) { 3759e1f24a79SParav Pandit err = mlx5_ib_alloc_counters(dev); 37606aec21f6SHaggai Eran if (err) 37616aec21f6SHaggai Eran goto err_odp; 376245bded2cSKamal Heib } 37636aec21f6SHaggai Eran 37645fe9dec0SEli Cohen dev->mdev->priv.uar = mlx5_get_uars_page(dev->mdev); 37655fe9dec0SEli Cohen if (!dev->mdev->priv.uar) 3766e1f24a79SParav Pandit goto err_cnt; 37675fe9dec0SEli Cohen 37685fe9dec0SEli Cohen err = mlx5_alloc_bfreg(dev->mdev, &dev->bfreg, false, false); 37695fe9dec0SEli Cohen if (err) 37705fe9dec0SEli Cohen goto err_uar_page; 37715fe9dec0SEli Cohen 37725fe9dec0SEli Cohen err = mlx5_alloc_bfreg(dev->mdev, &dev->fp_bfreg, false, true); 37735fe9dec0SEli Cohen if (err) 37745fe9dec0SEli Cohen goto err_bfreg; 37755fe9dec0SEli Cohen 37760837e86aSMark Bloch err = ib_register_device(&dev->ib_dev, NULL); 37770837e86aSMark Bloch if (err) 37785fe9dec0SEli Cohen goto err_fp_bfreg; 37790837e86aSMark Bloch 3780e126ba97SEli Cohen err = create_umr_res(dev); 3781e126ba97SEli Cohen if (err) 3782e126ba97SEli Cohen goto err_dev; 3783e126ba97SEli Cohen 3784e126ba97SEli Cohen for (i = 0; i < ARRAY_SIZE(mlx5_class_attributes); i++) { 3785281d1a92SWei Yongjun err = device_create_file(&dev->ib_dev.dev, 3786281d1a92SWei Yongjun mlx5_class_attributes[i]); 3787281d1a92SWei Yongjun if (err) 3788e126ba97SEli Cohen goto err_umrc; 3789e126ba97SEli Cohen } 3790e126ba97SEli Cohen 3791e126ba97SEli Cohen dev->ib_active = true; 3792e126ba97SEli Cohen 37939603b61dSJack Morgenstein return dev; 3794e126ba97SEli Cohen 3795e126ba97SEli Cohen err_umrc: 3796e126ba97SEli Cohen destroy_umrc_res(dev); 3797e126ba97SEli Cohen 3798e126ba97SEli Cohen err_dev: 3799e126ba97SEli Cohen ib_unregister_device(&dev->ib_dev); 3800e126ba97SEli Cohen 38015fe9dec0SEli Cohen err_fp_bfreg: 38025fe9dec0SEli Cohen mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg); 38035fe9dec0SEli Cohen 38045fe9dec0SEli Cohen err_bfreg: 38055fe9dec0SEli Cohen mlx5_free_bfreg(dev->mdev, &dev->bfreg); 38065fe9dec0SEli Cohen 38075fe9dec0SEli Cohen err_uar_page: 38085fe9dec0SEli Cohen mlx5_put_uars_page(dev->mdev, dev->mdev->priv.uar); 38095fe9dec0SEli Cohen 3810e1f24a79SParav Pandit err_cnt: 381145bded2cSKamal Heib if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt)) 3812e1f24a79SParav Pandit mlx5_ib_dealloc_counters(dev); 38130837e86aSMark Bloch 38146aec21f6SHaggai Eran err_odp: 38156aec21f6SHaggai Eran mlx5_ib_odp_remove_one(dev); 38166aec21f6SHaggai Eran 3817e126ba97SEli Cohen err_rsrc: 3818e126ba97SEli Cohen destroy_dev_resources(&dev->devr); 3819e126ba97SEli Cohen 382045f95acdSOr Gerlitz err_disable_eth: 38215ec8c83eSAviv Heller if (ll == IB_LINK_LAYER_ETHERNET) { 382245f95acdSOr Gerlitz mlx5_disable_eth(dev); 3823d012f5d6SOr Gerlitz mlx5_remove_netdev_notifier(dev); 38245ec8c83eSAviv Heller } 3825fc24fc5eSAchiad Shochat 38260837e86aSMark Bloch err_free_port: 38270837e86aSMark Bloch kfree(dev->port); 38280837e86aSMark Bloch 38299603b61dSJack Morgenstein err_dealloc: 3830e126ba97SEli Cohen ib_dealloc_device((struct ib_device *)dev); 3831e126ba97SEli Cohen 38329603b61dSJack Morgenstein return NULL; 3833e126ba97SEli Cohen } 3834e126ba97SEli Cohen 38359603b61dSJack Morgenstein static void mlx5_ib_remove(struct mlx5_core_dev *mdev, void *context) 3836e126ba97SEli Cohen { 38379603b61dSJack Morgenstein struct mlx5_ib_dev *dev = context; 3838fc24fc5eSAchiad Shochat enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev, 1); 38396aec21f6SHaggai Eran 3840d012f5d6SOr Gerlitz mlx5_remove_netdev_notifier(dev); 3841e126ba97SEli Cohen ib_unregister_device(&dev->ib_dev); 38425fe9dec0SEli Cohen mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg); 38435fe9dec0SEli Cohen mlx5_free_bfreg(dev->mdev, &dev->bfreg); 38445fe9dec0SEli Cohen mlx5_put_uars_page(dev->mdev, mdev->priv.uar); 384545bded2cSKamal Heib if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt)) 3846e1f24a79SParav Pandit mlx5_ib_dealloc_counters(dev); 3847eefd56e5SEli Cohen destroy_umrc_res(dev); 38486aec21f6SHaggai Eran mlx5_ib_odp_remove_one(dev); 3849e126ba97SEli Cohen destroy_dev_resources(&dev->devr); 3850fc24fc5eSAchiad Shochat if (ll == IB_LINK_LAYER_ETHERNET) 385145f95acdSOr Gerlitz mlx5_disable_eth(dev); 38520837e86aSMark Bloch kfree(dev->port); 3853e126ba97SEli Cohen ib_dealloc_device(&dev->ib_dev); 3854e126ba97SEli Cohen } 3855e126ba97SEli Cohen 38569603b61dSJack Morgenstein static struct mlx5_interface mlx5_ib_interface = { 38579603b61dSJack Morgenstein .add = mlx5_ib_add, 38589603b61dSJack Morgenstein .remove = mlx5_ib_remove, 38599603b61dSJack Morgenstein .event = mlx5_ib_event, 3860d9aaed83SArtemy Kovalyov #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING 3861d9aaed83SArtemy Kovalyov .pfault = mlx5_ib_pfault, 3862d9aaed83SArtemy Kovalyov #endif 386364613d94SSaeed Mahameed .protocol = MLX5_INTERFACE_PROTOCOL_IB, 3864e126ba97SEli Cohen }; 3865e126ba97SEli Cohen 3866e126ba97SEli Cohen static int __init mlx5_ib_init(void) 3867e126ba97SEli Cohen { 38686aec21f6SHaggai Eran int err; 38696aec21f6SHaggai Eran 387081713d37SArtemy Kovalyov mlx5_ib_odp_init(); 38719603b61dSJack Morgenstein 38726aec21f6SHaggai Eran err = mlx5_register_interface(&mlx5_ib_interface); 38736aec21f6SHaggai Eran 38746aec21f6SHaggai Eran return err; 3875e126ba97SEli Cohen } 3876e126ba97SEli Cohen 3877e126ba97SEli Cohen static void __exit mlx5_ib_cleanup(void) 3878e126ba97SEli Cohen { 38799603b61dSJack Morgenstein mlx5_unregister_interface(&mlx5_ib_interface); 3880e126ba97SEli Cohen } 3881e126ba97SEli Cohen 3882e126ba97SEli Cohen module_init(mlx5_ib_init); 3883e126ba97SEli Cohen module_exit(mlx5_ib_cleanup); 3884