1322810Shselasky/*- 2322810Shselasky * Copyright (c) 2013-2015, Mellanox Technologies, Ltd. All rights reserved. 3322810Shselasky * 4322810Shselasky * Redistribution and use in source and binary forms, with or without 5322810Shselasky * modification, are permitted provided that the following conditions 6322810Shselasky * are met: 7322810Shselasky * 1. Redistributions of source code must retain the above copyright 8322810Shselasky * notice, this list of conditions and the following disclaimer. 9322810Shselasky * 2. Redistributions in binary form must reproduce the above copyright 10322810Shselasky * notice, this list of conditions and the following disclaimer in the 11322810Shselasky * documentation and/or other materials provided with the distribution. 12322810Shselasky * 13322810Shselasky * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS `AS IS' AND 14322810Shselasky * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15322810Shselasky * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16322810Shselasky * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE 17322810Shselasky * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18322810Shselasky * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19322810Shselasky * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20322810Shselasky * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21322810Shselasky * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22322810Shselasky * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23322810Shselasky * SUCH DAMAGE. 24322810Shselasky * 25322810Shselasky * $FreeBSD: stable/11/sys/dev/mlx5/mlx5_ib/mlx5_ib_main.c 369102 2021-01-22 12:50:12Z hselasky $ 26322810Shselasky */ 27322810Shselasky 28331769Shselasky#include <linux/module.h> 29322810Shselasky#include <linux/errno.h> 30322810Shselasky#include <linux/pci.h> 31322810Shselasky#include <linux/dma-mapping.h> 32322810Shselasky#include <linux/slab.h> 33331769Shselasky#if defined(CONFIG_X86) 34331769Shselasky#include <asm/pat.h> 35331769Shselasky#endif 36322810Shselasky#include <linux/sched.h> 37331769Shselasky#include <linux/delay.h> 38322810Shselasky#include <linux/fs.h> 39322810Shselasky#undef inode 40322810Shselasky#include <rdma/ib_user_verbs.h> 41331769Shselasky#include <rdma/ib_addr.h> 42331769Shselasky#include <rdma/ib_cache.h> 43331769Shselasky#include <dev/mlx5/port.h> 44331769Shselasky#include <dev/mlx5/vport.h> 45331769Shselasky#include <linux/list.h> 46322810Shselasky#include <rdma/ib_smi.h> 47322810Shselasky#include <rdma/ib_umem.h> 48331769Shselasky#include <linux/in.h> 49331769Shselasky#include <linux/etherdevice.h> 50331769Shselasky#include <dev/mlx5/fs.h> 51322810Shselasky#include "mlx5_ib.h" 52322810Shselasky 53341948Shselasky#define DRIVER_NAME "mlx5ib" 54337101Shselasky#ifndef DRIVER_VERSION 55369102Shselasky#define DRIVER_VERSION "3.6.0" 56337101Shselasky#endif 57369102Shselasky#define DRIVER_RELDATE "December 2020" 58322810Shselasky 59322810ShselaskyMODULE_DESCRIPTION("Mellanox Connect-IB HCA IB driver"); 60322810ShselaskyMODULE_LICENSE("Dual BSD/GPL"); 61322810ShselaskyMODULE_DEPEND(mlx5ib, linuxkpi, 1, 1, 1); 62322810ShselaskyMODULE_DEPEND(mlx5ib, mlx5, 1, 1, 1); 63322810ShselaskyMODULE_DEPEND(mlx5ib, ibcore, 1, 1, 1); 64322810ShselaskyMODULE_VERSION(mlx5ib, 1); 65322810Shselasky 66337101Shselaskystatic const char mlx5_version[] = 67337101Shselasky DRIVER_NAME ": Mellanox Connect-IB Infiniband driver " 68331769Shselasky DRIVER_VERSION " (" DRIVER_RELDATE ")\n"; 69331769Shselasky 70322810Shselaskyenum { 71331769Shselasky MLX5_ATOMIC_SIZE_QP_8BYTES = 1 << 3, 72322810Shselasky}; 73322810Shselasky 74331769Shselaskystatic enum rdma_link_layer 75331769Shselaskymlx5_port_type_cap_to_rdma_ll(int port_type_cap) 76331769Shselasky{ 77331769Shselasky switch (port_type_cap) { 78331769Shselasky case MLX5_CAP_PORT_TYPE_IB: 79331769Shselasky return IB_LINK_LAYER_INFINIBAND; 80331769Shselasky case MLX5_CAP_PORT_TYPE_ETH: 81331769Shselasky return IB_LINK_LAYER_ETHERNET; 82331769Shselasky default: 83331769Shselasky return IB_LINK_LAYER_UNSPECIFIED; 84331769Shselasky } 85331769Shselasky} 86322810Shselasky 87331769Shselaskystatic enum rdma_link_layer 88331769Shselaskymlx5_ib_port_link_layer(struct ib_device *device, u8 port_num) 89331769Shselasky{ 90331769Shselasky struct mlx5_ib_dev *dev = to_mdev(device); 91331769Shselasky int port_type_cap = MLX5_CAP_GEN(dev->mdev, port_type); 92322810Shselasky 93331769Shselasky return mlx5_port_type_cap_to_rdma_ll(port_type_cap); 94331769Shselasky} 95331769Shselasky 96331769Shselaskystatic bool mlx5_netdev_match(struct net_device *ndev, 97331769Shselasky struct mlx5_core_dev *mdev, 98331769Shselasky const char *dname) 99322810Shselasky{ 100331769Shselasky return ndev->if_type == IFT_ETHER && 101331769Shselasky ndev->if_dname != NULL && 102331769Shselasky strcmp(ndev->if_dname, dname) == 0 && 103331769Shselasky ndev->if_softc != NULL && 104331769Shselasky *(struct mlx5_core_dev **)ndev->if_softc == mdev; 105331769Shselasky} 106322810Shselasky 107331769Shselaskystatic int mlx5_netdev_event(struct notifier_block *this, 108331769Shselasky unsigned long event, void *ptr) 109331769Shselasky{ 110331769Shselasky struct net_device *ndev = netdev_notifier_info_to_dev(ptr); 111331769Shselasky struct mlx5_ib_dev *ibdev = container_of(this, struct mlx5_ib_dev, 112331769Shselasky roce.nb); 113322810Shselasky 114331769Shselasky switch (event) { 115331769Shselasky case NETDEV_REGISTER: 116331769Shselasky case NETDEV_UNREGISTER: 117331769Shselasky write_lock(&ibdev->roce.netdev_lock); 118331769Shselasky /* check if network interface belongs to mlx5en */ 119331769Shselasky if (mlx5_netdev_match(ndev, ibdev->mdev, "mce")) 120331769Shselasky ibdev->roce.netdev = (event == NETDEV_UNREGISTER) ? 121331769Shselasky NULL : ndev; 122331769Shselasky write_unlock(&ibdev->roce.netdev_lock); 123331769Shselasky break; 124331769Shselasky 125331769Shselasky case NETDEV_UP: 126331769Shselasky case NETDEV_DOWN: { 127331769Shselasky struct net_device *upper = NULL; 128331769Shselasky 129331769Shselasky if ((upper == ndev || (!upper && ndev == ibdev->roce.netdev)) 130331769Shselasky && ibdev->ib_active) { 131331769Shselasky struct ib_event ibev = {0}; 132331769Shselasky 133331769Shselasky ibev.device = &ibdev->ib_dev; 134331769Shselasky ibev.event = (event == NETDEV_UP) ? 135331769Shselasky IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR; 136331769Shselasky ibev.element.port_num = 1; 137331769Shselasky ib_dispatch_event(&ibev); 138322810Shselasky } 139331769Shselasky break; 140322810Shselasky } 141322810Shselasky 142331769Shselasky default: 143331769Shselasky break; 144322810Shselasky } 145331769Shselasky 146331769Shselasky return NOTIFY_DONE; 147322810Shselasky} 148322810Shselasky 149331769Shselaskystatic struct net_device *mlx5_ib_get_netdev(struct ib_device *device, 150331769Shselasky u8 port_num) 151322810Shselasky{ 152331769Shselasky struct mlx5_ib_dev *ibdev = to_mdev(device); 153331769Shselasky struct net_device *ndev; 154331769Shselasky 155331769Shselasky /* Ensure ndev does not disappear before we invoke dev_hold() 156331769Shselasky */ 157331769Shselasky read_lock(&ibdev->roce.netdev_lock); 158331769Shselasky ndev = ibdev->roce.netdev; 159331769Shselasky if (ndev) 160331769Shselasky dev_hold(ndev); 161331769Shselasky read_unlock(&ibdev->roce.netdev_lock); 162331769Shselasky 163331769Shselasky return ndev; 164331769Shselasky} 165331769Shselasky 166331805Shselaskystatic int translate_eth_proto_oper(u32 eth_proto_oper, u8 *active_speed, 167331805Shselasky u8 *active_width) 168331805Shselasky{ 169331805Shselasky switch (eth_proto_oper) { 170331805Shselasky case MLX5E_PROT_MASK(MLX5E_1000BASE_CX_SGMII): 171331805Shselasky case MLX5E_PROT_MASK(MLX5E_1000BASE_KX): 172331805Shselasky case MLX5E_PROT_MASK(MLX5E_100BASE_TX): 173331805Shselasky case MLX5E_PROT_MASK(MLX5E_1000BASE_T): 174331805Shselasky *active_width = IB_WIDTH_1X; 175331805Shselasky *active_speed = IB_SPEED_SDR; 176331805Shselasky break; 177331805Shselasky case MLX5E_PROT_MASK(MLX5E_10GBASE_T): 178331805Shselasky case MLX5E_PROT_MASK(MLX5E_10GBASE_CX4): 179331805Shselasky case MLX5E_PROT_MASK(MLX5E_10GBASE_KX4): 180331805Shselasky case MLX5E_PROT_MASK(MLX5E_10GBASE_KR): 181331805Shselasky case MLX5E_PROT_MASK(MLX5E_10GBASE_CR): 182331805Shselasky case MLX5E_PROT_MASK(MLX5E_10GBASE_SR): 183347855Shselasky case MLX5E_PROT_MASK(MLX5E_10GBASE_ER_LR): 184331805Shselasky *active_width = IB_WIDTH_1X; 185331805Shselasky *active_speed = IB_SPEED_QDR; 186331805Shselasky break; 187331805Shselasky case MLX5E_PROT_MASK(MLX5E_25GBASE_CR): 188331805Shselasky case MLX5E_PROT_MASK(MLX5E_25GBASE_KR): 189331805Shselasky case MLX5E_PROT_MASK(MLX5E_25GBASE_SR): 190331805Shselasky *active_width = IB_WIDTH_1X; 191331805Shselasky *active_speed = IB_SPEED_EDR; 192331805Shselasky break; 193331805Shselasky case MLX5E_PROT_MASK(MLX5E_40GBASE_CR4): 194331805Shselasky case MLX5E_PROT_MASK(MLX5E_40GBASE_KR4): 195331805Shselasky case MLX5E_PROT_MASK(MLX5E_40GBASE_SR4): 196347855Shselasky case MLX5E_PROT_MASK(MLX5E_40GBASE_LR4_ER4): 197331805Shselasky *active_width = IB_WIDTH_4X; 198331805Shselasky *active_speed = IB_SPEED_QDR; 199331805Shselasky break; 200331805Shselasky case MLX5E_PROT_MASK(MLX5E_50GBASE_CR2): 201331805Shselasky case MLX5E_PROT_MASK(MLX5E_50GBASE_KR2): 202331805Shselasky case MLX5E_PROT_MASK(MLX5E_50GBASE_SR2): 203331805Shselasky *active_width = IB_WIDTH_1X; 204331805Shselasky *active_speed = IB_SPEED_HDR; 205331805Shselasky break; 206331805Shselasky case MLX5E_PROT_MASK(MLX5E_56GBASE_R4): 207331805Shselasky *active_width = IB_WIDTH_4X; 208331805Shselasky *active_speed = IB_SPEED_FDR; 209331805Shselasky break; 210331805Shselasky case MLX5E_PROT_MASK(MLX5E_100GBASE_CR4): 211331805Shselasky case MLX5E_PROT_MASK(MLX5E_100GBASE_SR4): 212331805Shselasky case MLX5E_PROT_MASK(MLX5E_100GBASE_KR4): 213331805Shselasky case MLX5E_PROT_MASK(MLX5E_100GBASE_LR4): 214331805Shselasky *active_width = IB_WIDTH_4X; 215331805Shselasky *active_speed = IB_SPEED_EDR; 216331805Shselasky break; 217331805Shselasky default: 218341956Shselasky *active_width = IB_WIDTH_4X; 219341956Shselasky *active_speed = IB_SPEED_QDR; 220331805Shselasky return -EINVAL; 221331805Shselasky } 222331805Shselasky 223331805Shselasky return 0; 224331805Shselasky} 225331805Shselasky 226347859Shselaskystatic int translate_eth_ext_proto_oper(u32 eth_proto_oper, u8 *active_speed, 227347859Shselasky u8 *active_width) 228347859Shselasky{ 229347859Shselasky switch (eth_proto_oper) { 230347859Shselasky case MLX5E_PROT_MASK(MLX5E_SGMII_100M): 231347859Shselasky case MLX5E_PROT_MASK(MLX5E_1000BASE_X_SGMII): 232347859Shselasky *active_width = IB_WIDTH_1X; 233347859Shselasky *active_speed = IB_SPEED_SDR; 234347859Shselasky break; 235347859Shselasky case MLX5E_PROT_MASK(MLX5E_5GBASE_R): 236347859Shselasky *active_width = IB_WIDTH_1X; 237347859Shselasky *active_speed = IB_SPEED_DDR; 238347859Shselasky break; 239347859Shselasky case MLX5E_PROT_MASK(MLX5E_10GBASE_XFI_XAUI_1): 240347859Shselasky *active_width = IB_WIDTH_1X; 241347859Shselasky *active_speed = IB_SPEED_QDR; 242347859Shselasky break; 243347859Shselasky case MLX5E_PROT_MASK(MLX5E_40GBASE_XLAUI_4_XLPPI_4): 244347859Shselasky *active_width = IB_WIDTH_4X; 245347859Shselasky *active_speed = IB_SPEED_QDR; 246347859Shselasky break; 247347859Shselasky case MLX5E_PROT_MASK(MLX5E_25GAUI_1_25GBASE_CR_KR): 248347859Shselasky *active_width = IB_WIDTH_1X; 249347859Shselasky *active_speed = IB_SPEED_EDR; 250347859Shselasky break; 251347859Shselasky case MLX5E_PROT_MASK(MLX5E_50GAUI_2_LAUI_2_50GBASE_CR2_KR2): 252347859Shselasky *active_width = IB_WIDTH_2X; 253347859Shselasky *active_speed = IB_SPEED_EDR; 254347859Shselasky break; 255347859Shselasky case MLX5E_PROT_MASK(MLX5E_50GAUI_1_LAUI_1_50GBASE_CR_KR): 256347859Shselasky *active_width = IB_WIDTH_1X; 257347859Shselasky *active_speed = IB_SPEED_HDR; 258347859Shselasky break; 259347859Shselasky case MLX5E_PROT_MASK(MLX5E_CAUI_4_100GBASE_CR4_KR4): 260347859Shselasky *active_width = IB_WIDTH_4X; 261347859Shselasky *active_speed = IB_SPEED_EDR; 262347859Shselasky break; 263347859Shselasky case MLX5E_PROT_MASK(MLX5E_100GAUI_2_100GBASE_CR2_KR2): 264347859Shselasky *active_width = IB_WIDTH_2X; 265347859Shselasky *active_speed = IB_SPEED_HDR; 266347859Shselasky break; 267347859Shselasky case MLX5E_PROT_MASK(MLX5E_200GAUI_4_200GBASE_CR4_KR4): 268347859Shselasky *active_width = IB_WIDTH_4X; 269347859Shselasky *active_speed = IB_SPEED_HDR; 270347859Shselasky break; 271347859Shselasky default: 272347859Shselasky *active_width = IB_WIDTH_4X; 273347859Shselasky *active_speed = IB_SPEED_QDR; 274347859Shselasky return -EINVAL; 275347859Shselasky } 276347859Shselasky 277347859Shselasky return 0; 278347859Shselasky} 279347859Shselasky 280331769Shselaskystatic int mlx5_query_port_roce(struct ib_device *device, u8 port_num, 281331769Shselasky struct ib_port_attr *props) 282331769Shselasky{ 283322810Shselasky struct mlx5_ib_dev *dev = to_mdev(device); 284347859Shselasky u32 out[MLX5_ST_SZ_DW(ptys_reg)] = {}; 285331769Shselasky struct net_device *ndev; 286331769Shselasky enum ib_mtu ndev_ib_mtu; 287331769Shselasky u16 qkey_viol_cntr; 288331805Shselasky u32 eth_prot_oper; 289347859Shselasky bool ext; 290331805Shselasky int err; 291322810Shselasky 292331769Shselasky memset(props, 0, sizeof(*props)); 293331769Shselasky 294331805Shselasky /* Possible bad flows are checked before filling out props so in case 295331805Shselasky * of an error it will still be zeroed out. 296331805Shselasky */ 297347859Shselasky err = mlx5_query_port_ptys(dev->mdev, out, sizeof(out), MLX5_PTYS_EN, 298347859Shselasky port_num); 299331805Shselasky if (err) 300331805Shselasky return err; 301331805Shselasky 302347859Shselasky ext = MLX5_CAP_PCAM_FEATURE(dev->mdev, ptys_extended_ethernet); 303347859Shselasky eth_prot_oper = MLX5_GET_ETH_PROTO(ptys_reg, out, ext, eth_proto_oper); 304331805Shselasky 305347859Shselasky if (ext) 306347859Shselasky translate_eth_ext_proto_oper(eth_prot_oper, &props->active_speed, 307347859Shselasky &props->active_width); 308347859Shselasky else 309347859Shselasky translate_eth_proto_oper(eth_prot_oper, &props->active_speed, 310347859Shselasky &props->active_width); 311347859Shselasky 312331769Shselasky props->port_cap_flags |= IB_PORT_CM_SUP; 313331769Shselasky props->port_cap_flags |= IB_PORT_IP_BASED_GIDS; 314331769Shselasky 315331769Shselasky props->gid_tbl_len = MLX5_CAP_ROCE(dev->mdev, 316331769Shselasky roce_address_table_size); 317331769Shselasky props->max_mtu = IB_MTU_4096; 318331769Shselasky props->max_msg_sz = 1 << MLX5_CAP_GEN(dev->mdev, log_max_msg); 319331769Shselasky props->pkey_tbl_len = 1; 320331769Shselasky props->state = IB_PORT_DOWN; 321331769Shselasky props->phys_state = 3; 322331769Shselasky 323331769Shselasky mlx5_query_nic_vport_qkey_viol_cntr(dev->mdev, &qkey_viol_cntr); 324331769Shselasky props->qkey_viol_cntr = qkey_viol_cntr; 325331769Shselasky 326331769Shselasky ndev = mlx5_ib_get_netdev(device, port_num); 327331769Shselasky if (!ndev) 328331769Shselasky return 0; 329331769Shselasky 330331769Shselasky if (netif_running(ndev) && netif_carrier_ok(ndev)) { 331331769Shselasky props->state = IB_PORT_ACTIVE; 332331769Shselasky props->phys_state = 5; 333331769Shselasky } 334331769Shselasky 335331769Shselasky ndev_ib_mtu = iboe_get_mtu(ndev->if_mtu); 336331769Shselasky 337331769Shselasky dev_put(ndev); 338331769Shselasky 339331769Shselasky props->active_mtu = min(props->max_mtu, ndev_ib_mtu); 340331769Shselasky return 0; 341331769Shselasky} 342331769Shselasky 343331769Shselaskystatic void ib_gid_to_mlx5_roce_addr(const union ib_gid *gid, 344331769Shselasky const struct ib_gid_attr *attr, 345331769Shselasky void *mlx5_addr) 346331769Shselasky{ 347331769Shselasky#define MLX5_SET_RA(p, f, v) MLX5_SET(roce_addr_layout, p, f, v) 348331769Shselasky char *mlx5_addr_l3_addr = MLX5_ADDR_OF(roce_addr_layout, mlx5_addr, 349331769Shselasky source_l3_address); 350331769Shselasky void *mlx5_addr_mac = MLX5_ADDR_OF(roce_addr_layout, mlx5_addr, 351331769Shselasky source_mac_47_32); 352337078Shselasky u16 vlan_id; 353331769Shselasky 354331769Shselasky if (!gid) 355331769Shselasky return; 356331769Shselasky ether_addr_copy(mlx5_addr_mac, IF_LLADDR(attr->ndev)); 357331769Shselasky 358337078Shselasky vlan_id = rdma_vlan_dev_vlan_id(attr->ndev); 359337078Shselasky if (vlan_id != 0xffff) { 360331769Shselasky MLX5_SET_RA(mlx5_addr, vlan_valid, 1); 361337078Shselasky MLX5_SET_RA(mlx5_addr, vlan_id, vlan_id); 362331769Shselasky } 363331769Shselasky 364331769Shselasky switch (attr->gid_type) { 365331769Shselasky case IB_GID_TYPE_IB: 366331769Shselasky MLX5_SET_RA(mlx5_addr, roce_version, MLX5_ROCE_VERSION_1); 367331769Shselasky break; 368331769Shselasky case IB_GID_TYPE_ROCE_UDP_ENCAP: 369331769Shselasky MLX5_SET_RA(mlx5_addr, roce_version, MLX5_ROCE_VERSION_2); 370331769Shselasky break; 371331769Shselasky 372322810Shselasky default: 373331769Shselasky WARN_ON(true); 374322810Shselasky } 375331769Shselasky 376331769Shselasky if (attr->gid_type != IB_GID_TYPE_IB) { 377331769Shselasky if (ipv6_addr_v4mapped((void *)gid)) 378331769Shselasky MLX5_SET_RA(mlx5_addr, roce_l3_type, 379331769Shselasky MLX5_ROCE_L3_TYPE_IPV4); 380331769Shselasky else 381331769Shselasky MLX5_SET_RA(mlx5_addr, roce_l3_type, 382331769Shselasky MLX5_ROCE_L3_TYPE_IPV6); 383331769Shselasky } 384331769Shselasky 385331769Shselasky if ((attr->gid_type == IB_GID_TYPE_IB) || 386331769Shselasky !ipv6_addr_v4mapped((void *)gid)) 387331769Shselasky memcpy(mlx5_addr_l3_addr, gid, sizeof(*gid)); 388331769Shselasky else 389331769Shselasky memcpy(&mlx5_addr_l3_addr[12], &gid->raw[12], 4); 390322810Shselasky} 391322810Shselasky 392331769Shselaskystatic int set_roce_addr(struct ib_device *device, u8 port_num, 393331769Shselasky unsigned int index, 394331769Shselasky const union ib_gid *gid, 395331769Shselasky const struct ib_gid_attr *attr) 396331769Shselasky{ 397331769Shselasky struct mlx5_ib_dev *dev = to_mdev(device); 398331769Shselasky u32 in[MLX5_ST_SZ_DW(set_roce_address_in)] = {0}; 399331769Shselasky u32 out[MLX5_ST_SZ_DW(set_roce_address_out)] = {0}; 400331769Shselasky void *in_addr = MLX5_ADDR_OF(set_roce_address_in, in, roce_address); 401331769Shselasky enum rdma_link_layer ll = mlx5_ib_port_link_layer(device, port_num); 402331769Shselasky 403331769Shselasky if (ll != IB_LINK_LAYER_ETHERNET) 404331769Shselasky return -EINVAL; 405331769Shselasky 406331769Shselasky ib_gid_to_mlx5_roce_addr(gid, attr, in_addr); 407331769Shselasky 408331769Shselasky MLX5_SET(set_roce_address_in, in, roce_address_index, index); 409331769Shselasky MLX5_SET(set_roce_address_in, in, opcode, MLX5_CMD_OP_SET_ROCE_ADDRESS); 410331769Shselasky return mlx5_cmd_exec(dev->mdev, in, sizeof(in), out, sizeof(out)); 411331769Shselasky} 412331769Shselasky 413331769Shselaskystatic int mlx5_ib_add_gid(struct ib_device *device, u8 port_num, 414331769Shselasky unsigned int index, const union ib_gid *gid, 415331769Shselasky const struct ib_gid_attr *attr, 416331769Shselasky __always_unused void **context) 417331769Shselasky{ 418331769Shselasky return set_roce_addr(device, port_num, index, gid, attr); 419331769Shselasky} 420331769Shselasky 421331769Shselaskystatic int mlx5_ib_del_gid(struct ib_device *device, u8 port_num, 422331769Shselasky unsigned int index, __always_unused void **context) 423331769Shselasky{ 424331769Shselasky return set_roce_addr(device, port_num, index, NULL, NULL); 425331769Shselasky} 426331769Shselasky 427331769Shselasky__be16 mlx5_get_roce_udp_sport(struct mlx5_ib_dev *dev, u8 port_num, 428331769Shselasky int index) 429331769Shselasky{ 430331769Shselasky struct ib_gid_attr attr; 431331769Shselasky union ib_gid gid; 432331769Shselasky 433331769Shselasky if (ib_get_cached_gid(&dev->ib_dev, port_num, index, &gid, &attr)) 434331769Shselasky return 0; 435331769Shselasky 436331769Shselasky if (!attr.ndev) 437331769Shselasky return 0; 438331769Shselasky 439331769Shselasky dev_put(attr.ndev); 440331769Shselasky 441331769Shselasky if (attr.gid_type != IB_GID_TYPE_ROCE_UDP_ENCAP) 442331769Shselasky return 0; 443331769Shselasky 444331769Shselasky return cpu_to_be16(MLX5_CAP_ROCE(dev->mdev, r_roce_min_src_udp_port)); 445331769Shselasky} 446331769Shselasky 447337098Shselaskyint mlx5_get_roce_gid_type(struct mlx5_ib_dev *dev, u8 port_num, 448337098Shselasky int index, enum ib_gid_type *gid_type) 449337098Shselasky{ 450337098Shselasky struct ib_gid_attr attr; 451337098Shselasky union ib_gid gid; 452337098Shselasky int ret; 453337098Shselasky 454337098Shselasky ret = ib_get_cached_gid(&dev->ib_dev, port_num, index, &gid, &attr); 455337098Shselasky if (ret) 456337098Shselasky return ret; 457337098Shselasky 458337098Shselasky if (!attr.ndev) 459337098Shselasky return -ENODEV; 460337098Shselasky 461337098Shselasky dev_put(attr.ndev); 462337098Shselasky 463337098Shselasky *gid_type = attr.gid_type; 464337098Shselasky 465337098Shselasky return 0; 466337098Shselasky} 467337098Shselasky 468322810Shselaskystatic int mlx5_use_mad_ifc(struct mlx5_ib_dev *dev) 469322810Shselasky{ 470331769Shselasky if (MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_IB) 471331769Shselasky return !MLX5_CAP_GEN(dev->mdev, ib_virt); 472331769Shselasky return 0; 473322810Shselasky} 474322810Shselasky 475322810Shselaskyenum { 476322810Shselasky MLX5_VPORT_ACCESS_METHOD_MAD, 477322810Shselasky MLX5_VPORT_ACCESS_METHOD_HCA, 478322810Shselasky MLX5_VPORT_ACCESS_METHOD_NIC, 479322810Shselasky}; 480322810Shselasky 481322810Shselaskystatic int mlx5_get_vport_access_method(struct ib_device *ibdev) 482322810Shselasky{ 483322810Shselasky if (mlx5_use_mad_ifc(to_mdev(ibdev))) 484322810Shselasky return MLX5_VPORT_ACCESS_METHOD_MAD; 485322810Shselasky 486322810Shselasky if (mlx5_ib_port_link_layer(ibdev, 1) == 487322810Shselasky IB_LINK_LAYER_ETHERNET) 488322810Shselasky return MLX5_VPORT_ACCESS_METHOD_NIC; 489322810Shselasky 490322810Shselasky return MLX5_VPORT_ACCESS_METHOD_HCA; 491322810Shselasky} 492322810Shselasky 493331769Shselaskystatic void get_atomic_caps(struct mlx5_ib_dev *dev, 494331769Shselasky struct ib_device_attr *props) 495331769Shselasky{ 496331769Shselasky u8 tmp; 497331769Shselasky u8 atomic_operations = MLX5_CAP_ATOMIC(dev->mdev, atomic_operations); 498331769Shselasky u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_qp); 499331769Shselasky u8 atomic_req_8B_endianness_mode = 500331769Shselasky MLX5_CAP_ATOMIC(dev->mdev, atomic_req_8B_endianess_mode); 501331769Shselasky 502331769Shselasky /* Check if HW supports 8 bytes standard atomic operations and capable 503331769Shselasky * of host endianness respond 504331769Shselasky */ 505331769Shselasky tmp = MLX5_ATOMIC_OPS_CMP_SWAP | MLX5_ATOMIC_OPS_FETCH_ADD; 506331769Shselasky if (((atomic_operations & tmp) == tmp) && 507331769Shselasky (atomic_size_qp & MLX5_ATOMIC_SIZE_QP_8BYTES) && 508331769Shselasky (atomic_req_8B_endianness_mode)) { 509331769Shselasky props->atomic_cap = IB_ATOMIC_HCA; 510331769Shselasky } else { 511331769Shselasky props->atomic_cap = IB_ATOMIC_NONE; 512331769Shselasky } 513331769Shselasky} 514331769Shselasky 515322810Shselaskystatic int mlx5_query_system_image_guid(struct ib_device *ibdev, 516322810Shselasky __be64 *sys_image_guid) 517322810Shselasky{ 518322810Shselasky struct mlx5_ib_dev *dev = to_mdev(ibdev); 519322810Shselasky struct mlx5_core_dev *mdev = dev->mdev; 520322810Shselasky u64 tmp; 521322810Shselasky int err; 522322810Shselasky 523322810Shselasky switch (mlx5_get_vport_access_method(ibdev)) { 524322810Shselasky case MLX5_VPORT_ACCESS_METHOD_MAD: 525331769Shselasky return mlx5_query_mad_ifc_system_image_guid(ibdev, 526322810Shselasky sys_image_guid); 527322810Shselasky 528322810Shselasky case MLX5_VPORT_ACCESS_METHOD_HCA: 529322810Shselasky err = mlx5_query_hca_vport_system_image_guid(mdev, &tmp); 530331769Shselasky break; 531322810Shselasky 532322810Shselasky case MLX5_VPORT_ACCESS_METHOD_NIC: 533322810Shselasky err = mlx5_query_nic_vport_system_image_guid(mdev, &tmp); 534331769Shselasky break; 535322810Shselasky 536322810Shselasky default: 537322810Shselasky return -EINVAL; 538322810Shselasky } 539331769Shselasky 540331769Shselasky if (!err) 541331769Shselasky *sys_image_guid = cpu_to_be64(tmp); 542331769Shselasky 543331769Shselasky return err; 544331769Shselasky 545322810Shselasky} 546322810Shselasky 547322810Shselaskystatic int mlx5_query_max_pkeys(struct ib_device *ibdev, 548322810Shselasky u16 *max_pkeys) 549322810Shselasky{ 550322810Shselasky struct mlx5_ib_dev *dev = to_mdev(ibdev); 551322810Shselasky struct mlx5_core_dev *mdev = dev->mdev; 552322810Shselasky 553322810Shselasky switch (mlx5_get_vport_access_method(ibdev)) { 554322810Shselasky case MLX5_VPORT_ACCESS_METHOD_MAD: 555331769Shselasky return mlx5_query_mad_ifc_max_pkeys(ibdev, max_pkeys); 556322810Shselasky 557322810Shselasky case MLX5_VPORT_ACCESS_METHOD_HCA: 558322810Shselasky case MLX5_VPORT_ACCESS_METHOD_NIC: 559322810Shselasky *max_pkeys = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, 560322810Shselasky pkey_table_size)); 561322810Shselasky return 0; 562322810Shselasky 563322810Shselasky default: 564322810Shselasky return -EINVAL; 565322810Shselasky } 566322810Shselasky} 567322810Shselasky 568322810Shselaskystatic int mlx5_query_vendor_id(struct ib_device *ibdev, 569322810Shselasky u32 *vendor_id) 570322810Shselasky{ 571322810Shselasky struct mlx5_ib_dev *dev = to_mdev(ibdev); 572322810Shselasky 573322810Shselasky switch (mlx5_get_vport_access_method(ibdev)) { 574322810Shselasky case MLX5_VPORT_ACCESS_METHOD_MAD: 575331769Shselasky return mlx5_query_mad_ifc_vendor_id(ibdev, vendor_id); 576322810Shselasky 577322810Shselasky case MLX5_VPORT_ACCESS_METHOD_HCA: 578322810Shselasky case MLX5_VPORT_ACCESS_METHOD_NIC: 579322810Shselasky return mlx5_core_query_vendor_id(dev->mdev, vendor_id); 580322810Shselasky 581322810Shselasky default: 582322810Shselasky return -EINVAL; 583322810Shselasky } 584322810Shselasky} 585322810Shselasky 586322810Shselaskystatic int mlx5_query_node_guid(struct mlx5_ib_dev *dev, 587322810Shselasky __be64 *node_guid) 588322810Shselasky{ 589322810Shselasky u64 tmp; 590322810Shselasky int err; 591322810Shselasky 592322810Shselasky switch (mlx5_get_vport_access_method(&dev->ib_dev)) { 593322810Shselasky case MLX5_VPORT_ACCESS_METHOD_MAD: 594331769Shselasky return mlx5_query_mad_ifc_node_guid(dev, node_guid); 595322810Shselasky 596322810Shselasky case MLX5_VPORT_ACCESS_METHOD_HCA: 597322810Shselasky err = mlx5_query_hca_vport_node_guid(dev->mdev, &tmp); 598331769Shselasky break; 599322810Shselasky 600322810Shselasky case MLX5_VPORT_ACCESS_METHOD_NIC: 601322810Shselasky err = mlx5_query_nic_vport_node_guid(dev->mdev, &tmp); 602331769Shselasky break; 603322810Shselasky 604322810Shselasky default: 605322810Shselasky return -EINVAL; 606322810Shselasky } 607331769Shselasky 608331769Shselasky if (!err) 609331769Shselasky *node_guid = cpu_to_be64(tmp); 610331769Shselasky 611331769Shselasky return err; 612322810Shselasky} 613322810Shselasky 614322810Shselaskystruct mlx5_reg_node_desc { 615331769Shselasky u8 desc[IB_DEVICE_NODE_DESC_MAX]; 616322810Shselasky}; 617322810Shselasky 618322810Shselaskystatic int mlx5_query_node_desc(struct mlx5_ib_dev *dev, char *node_desc) 619322810Shselasky{ 620322810Shselasky struct mlx5_reg_node_desc in; 621322810Shselasky 622322810Shselasky if (mlx5_use_mad_ifc(dev)) 623331769Shselasky return mlx5_query_mad_ifc_node_desc(dev, node_desc); 624322810Shselasky 625322810Shselasky memset(&in, 0, sizeof(in)); 626322810Shselasky 627322810Shselasky return mlx5_core_access_reg(dev->mdev, &in, sizeof(in), node_desc, 628322810Shselasky sizeof(struct mlx5_reg_node_desc), 629322810Shselasky MLX5_REG_NODE_DESC, 0, 0); 630322810Shselasky} 631322810Shselasky 632322810Shselaskystatic int mlx5_ib_query_device(struct ib_device *ibdev, 633331769Shselasky struct ib_device_attr *props, 634331769Shselasky struct ib_udata *uhw) 635322810Shselasky{ 636322810Shselasky struct mlx5_ib_dev *dev = to_mdev(ibdev); 637322810Shselasky struct mlx5_core_dev *mdev = dev->mdev; 638331769Shselasky int err = -ENOMEM; 639353185Shselasky int max_sq_desc; 640322810Shselasky int max_rq_sg; 641322810Shselasky int max_sq_sg; 642331769Shselasky u64 min_page_size = 1ull << MLX5_CAP_GEN(mdev, log_pg_sz); 643331769Shselasky struct mlx5_ib_query_device_resp resp = {}; 644331769Shselasky size_t resp_len; 645331769Shselasky u64 max_tso; 646322810Shselasky 647331769Shselasky resp_len = sizeof(resp.comp_mask) + sizeof(resp.response_length); 648331769Shselasky if (uhw->outlen && uhw->outlen < resp_len) 649331769Shselasky return -EINVAL; 650331769Shselasky else 651331769Shselasky resp.response_length = resp_len; 652322810Shselasky 653331769Shselasky if (uhw->inlen && !ib_is_udata_cleared(uhw, 0, uhw->inlen)) 654331769Shselasky return -EINVAL; 655331769Shselasky 656322810Shselasky memset(props, 0, sizeof(*props)); 657322810Shselasky err = mlx5_query_system_image_guid(ibdev, 658322810Shselasky &props->sys_image_guid); 659322810Shselasky if (err) 660322810Shselasky return err; 661322810Shselasky 662322810Shselasky err = mlx5_query_max_pkeys(ibdev, &props->max_pkeys); 663322810Shselasky if (err) 664322810Shselasky return err; 665322810Shselasky 666322810Shselasky err = mlx5_query_vendor_id(ibdev, &props->vendor_id); 667322810Shselasky if (err) 668322810Shselasky return err; 669322810Shselasky 670322810Shselasky props->fw_ver = ((u64)fw_rev_maj(dev->mdev) << 32) | 671341950Shselasky ((u32)fw_rev_min(dev->mdev) << 16) | 672322810Shselasky fw_rev_sub(dev->mdev); 673322810Shselasky props->device_cap_flags = IB_DEVICE_CHANGE_PHY_PORT | 674322810Shselasky IB_DEVICE_PORT_ACTIVE_EVENT | 675322810Shselasky IB_DEVICE_SYS_IMAGE_GUID | 676322810Shselasky IB_DEVICE_RC_RNR_NAK_GEN; 677322810Shselasky 678322810Shselasky if (MLX5_CAP_GEN(mdev, pkv)) 679322810Shselasky props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR; 680322810Shselasky if (MLX5_CAP_GEN(mdev, qkv)) 681322810Shselasky props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR; 682322810Shselasky if (MLX5_CAP_GEN(mdev, apm)) 683322810Shselasky props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG; 684322810Shselasky if (MLX5_CAP_GEN(mdev, xrc)) 685322810Shselasky props->device_cap_flags |= IB_DEVICE_XRC; 686331769Shselasky if (MLX5_CAP_GEN(mdev, imaicl)) { 687331769Shselasky props->device_cap_flags |= IB_DEVICE_MEM_WINDOW | 688331769Shselasky IB_DEVICE_MEM_WINDOW_TYPE_2B; 689331769Shselasky props->max_mw = 1 << MLX5_CAP_GEN(mdev, log_max_mkey); 690331769Shselasky /* We support 'Gappy' memory registration too */ 691331769Shselasky props->device_cap_flags |= IB_DEVICE_SG_GAPS_REG; 692331769Shselasky } 693322810Shselasky props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS; 694331769Shselasky if (MLX5_CAP_GEN(mdev, sho)) { 695331769Shselasky props->device_cap_flags |= IB_DEVICE_SIGNATURE_HANDOVER; 696331769Shselasky /* At this stage no support for signature handover */ 697331769Shselasky props->sig_prot_cap = IB_PROT_T10DIF_TYPE_1 | 698331769Shselasky IB_PROT_T10DIF_TYPE_2 | 699331769Shselasky IB_PROT_T10DIF_TYPE_3; 700331769Shselasky props->sig_guard_cap = IB_GUARD_T10DIF_CRC | 701331769Shselasky IB_GUARD_T10DIF_CSUM; 702331769Shselasky } 703322810Shselasky if (MLX5_CAP_GEN(mdev, block_lb_mc)) 704322810Shselasky props->device_cap_flags |= IB_DEVICE_BLOCK_MULTICAST_LOOPBACK; 705322810Shselasky 706331769Shselasky if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads)) { 707331769Shselasky if (MLX5_CAP_ETH(mdev, csum_cap)) 708331769Shselasky props->device_cap_flags |= IB_DEVICE_RAW_IP_CSUM; 709331769Shselasky 710331769Shselasky if (field_avail(typeof(resp), tso_caps, uhw->outlen)) { 711331769Shselasky max_tso = MLX5_CAP_ETH(mdev, max_lso_cap); 712331769Shselasky if (max_tso) { 713331769Shselasky resp.tso_caps.max_tso = 1 << max_tso; 714331769Shselasky resp.tso_caps.supported_qpts |= 715331769Shselasky 1 << IB_QPT_RAW_PACKET; 716331769Shselasky resp.response_length += sizeof(resp.tso_caps); 717331769Shselasky } 718331769Shselasky } 719331769Shselasky 720331769Shselasky if (field_avail(typeof(resp), rss_caps, uhw->outlen)) { 721331769Shselasky resp.rss_caps.rx_hash_function = 722331769Shselasky MLX5_RX_HASH_FUNC_TOEPLITZ; 723331769Shselasky resp.rss_caps.rx_hash_fields_mask = 724331769Shselasky MLX5_RX_HASH_SRC_IPV4 | 725331769Shselasky MLX5_RX_HASH_DST_IPV4 | 726331769Shselasky MLX5_RX_HASH_SRC_IPV6 | 727331769Shselasky MLX5_RX_HASH_DST_IPV6 | 728331769Shselasky MLX5_RX_HASH_SRC_PORT_TCP | 729331769Shselasky MLX5_RX_HASH_DST_PORT_TCP | 730331769Shselasky MLX5_RX_HASH_SRC_PORT_UDP | 731331769Shselasky MLX5_RX_HASH_DST_PORT_UDP; 732331769Shselasky resp.response_length += sizeof(resp.rss_caps); 733331769Shselasky } 734331769Shselasky } else { 735331769Shselasky if (field_avail(typeof(resp), tso_caps, uhw->outlen)) 736331769Shselasky resp.response_length += sizeof(resp.tso_caps); 737331769Shselasky if (field_avail(typeof(resp), rss_caps, uhw->outlen)) 738331769Shselasky resp.response_length += sizeof(resp.rss_caps); 739331769Shselasky } 740331769Shselasky 741331769Shselasky if (MLX5_CAP_GEN(mdev, ipoib_ipoib_offloads)) { 742331769Shselasky props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM; 743331769Shselasky props->device_cap_flags |= IB_DEVICE_UD_TSO; 744331769Shselasky } 745331769Shselasky 746331769Shselasky if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) && 747331769Shselasky MLX5_CAP_ETH(dev->mdev, scatter_fcs)) 748331769Shselasky props->device_cap_flags |= IB_DEVICE_RAW_SCATTER_FCS; 749331769Shselasky 750331769Shselasky if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS)) 751331769Shselasky props->device_cap_flags |= IB_DEVICE_MANAGED_FLOW_STEERING; 752331769Shselasky 753322810Shselasky props->vendor_part_id = mdev->pdev->device; 754322810Shselasky props->hw_ver = mdev->pdev->revision; 755322810Shselasky 756322810Shselasky props->max_mr_size = ~0ull; 757331769Shselasky props->page_size_cap = ~(min_page_size - 1); 758322810Shselasky props->max_qp = 1 << MLX5_CAP_GEN(mdev, log_max_qp); 759322810Shselasky props->max_qp_wr = 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz); 760322810Shselasky max_rq_sg = MLX5_CAP_GEN(mdev, max_wqe_sz_rq) / 761322810Shselasky sizeof(struct mlx5_wqe_data_seg); 762353185Shselasky max_sq_desc = min_t(int, MLX5_CAP_GEN(mdev, max_wqe_sz_sq), 512); 763353185Shselasky max_sq_sg = (max_sq_desc - sizeof(struct mlx5_wqe_ctrl_seg) - 764353185Shselasky sizeof(struct mlx5_wqe_raddr_seg)) / 765353185Shselasky sizeof(struct mlx5_wqe_data_seg); 766322810Shselasky props->max_sge = min(max_rq_sg, max_sq_sg); 767331769Shselasky props->max_sge_rd = MLX5_MAX_SGE_RD; 768322810Shselasky props->max_cq = 1 << MLX5_CAP_GEN(mdev, log_max_cq); 769322810Shselasky props->max_cqe = (1 << MLX5_CAP_GEN(mdev, log_max_cq_sz)) - 1; 770322810Shselasky props->max_mr = 1 << MLX5_CAP_GEN(mdev, log_max_mkey); 771322810Shselasky props->max_pd = 1 << MLX5_CAP_GEN(mdev, log_max_pd); 772322810Shselasky props->max_qp_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_req_qp); 773322810Shselasky props->max_qp_init_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_res_qp); 774322810Shselasky props->max_srq = 1 << MLX5_CAP_GEN(mdev, log_max_srq); 775322810Shselasky props->max_srq_wr = (1 << MLX5_CAP_GEN(mdev, log_max_srq_sz)) - 1; 776322810Shselasky props->local_ca_ack_delay = MLX5_CAP_GEN(mdev, local_ca_ack_delay); 777322810Shselasky props->max_res_rd_atom = props->max_qp_rd_atom * props->max_qp; 778322810Shselasky props->max_srq_sge = max_rq_sg - 1; 779331769Shselasky props->max_fast_reg_page_list_len = 780331769Shselasky 1 << MLX5_CAP_GEN(mdev, log_max_klm_list_size); 781322810Shselasky get_atomic_caps(dev, props); 782331769Shselasky props->masked_atomic_cap = IB_ATOMIC_NONE; 783322810Shselasky props->max_mcast_grp = 1 << MLX5_CAP_GEN(mdev, log_max_mcg); 784322810Shselasky props->max_mcast_qp_attach = MLX5_CAP_GEN(mdev, max_qp_mcg); 785322810Shselasky props->max_total_mcast_qp_attach = props->max_mcast_qp_attach * 786322810Shselasky props->max_mcast_grp; 787322810Shselasky props->max_map_per_fmr = INT_MAX; /* no limit in ConnectIB */ 788331769Shselasky props->hca_core_clock = MLX5_CAP_GEN(mdev, device_frequency_khz); 789331769Shselasky props->timestamp_mask = 0x7FFFFFFFFFFFFFFFULL; 790322810Shselasky 791331769Shselasky#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING 792331769Shselasky if (MLX5_CAP_GEN(mdev, pg)) 793331769Shselasky props->device_cap_flags |= IB_DEVICE_ON_DEMAND_PAGING; 794331769Shselasky props->odp_caps = dev->odp_caps; 795331769Shselasky#endif 796331769Shselasky 797331769Shselasky if (MLX5_CAP_GEN(mdev, cd)) 798331769Shselasky props->device_cap_flags |= IB_DEVICE_CROSS_CHANNEL; 799331769Shselasky 800331769Shselasky if (!mlx5_core_is_pf(mdev)) 801331769Shselasky props->device_cap_flags |= IB_DEVICE_VIRTUAL_FUNCTION; 802331769Shselasky 803331769Shselasky if (mlx5_ib_port_link_layer(ibdev, 1) == 804331769Shselasky IB_LINK_LAYER_ETHERNET) { 805331769Shselasky props->rss_caps.max_rwq_indirection_tables = 806331769Shselasky 1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt); 807331769Shselasky props->rss_caps.max_rwq_indirection_table_size = 808331769Shselasky 1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt_size); 809331769Shselasky props->rss_caps.supported_qpts = 1 << IB_QPT_RAW_PACKET; 810331769Shselasky props->max_wq_type_rq = 811331769Shselasky 1 << MLX5_CAP_GEN(dev->mdev, log_max_rq); 812331769Shselasky } 813331769Shselasky 814331769Shselasky if (uhw->outlen) { 815331769Shselasky err = ib_copy_to_udata(uhw, &resp, resp.response_length); 816331769Shselasky 817331769Shselasky if (err) 818331769Shselasky return err; 819331769Shselasky } 820331769Shselasky 821322810Shselasky return 0; 822322810Shselasky} 823322810Shselasky 824322810Shselaskyenum mlx5_ib_width { 825322810Shselasky MLX5_IB_WIDTH_1X = 1 << 0, 826322810Shselasky MLX5_IB_WIDTH_2X = 1 << 1, 827322810Shselasky MLX5_IB_WIDTH_4X = 1 << 2, 828322810Shselasky MLX5_IB_WIDTH_8X = 1 << 3, 829322810Shselasky MLX5_IB_WIDTH_12X = 1 << 4 830322810Shselasky}; 831322810Shselasky 832322810Shselaskystatic int translate_active_width(struct ib_device *ibdev, u8 active_width, 833322810Shselasky u8 *ib_width) 834322810Shselasky{ 835322810Shselasky struct mlx5_ib_dev *dev = to_mdev(ibdev); 836322810Shselasky int err = 0; 837322810Shselasky 838322810Shselasky if (active_width & MLX5_IB_WIDTH_1X) { 839322810Shselasky *ib_width = IB_WIDTH_1X; 840322810Shselasky } else if (active_width & MLX5_IB_WIDTH_2X) { 841347859Shselasky *ib_width = IB_WIDTH_2X; 842322810Shselasky } else if (active_width & MLX5_IB_WIDTH_4X) { 843322810Shselasky *ib_width = IB_WIDTH_4X; 844322810Shselasky } else if (active_width & MLX5_IB_WIDTH_8X) { 845322810Shselasky *ib_width = IB_WIDTH_8X; 846322810Shselasky } else if (active_width & MLX5_IB_WIDTH_12X) { 847322810Shselasky *ib_width = IB_WIDTH_12X; 848322810Shselasky } else { 849322810Shselasky mlx5_ib_dbg(dev, "Invalid active_width %d\n", 850322810Shselasky (int)active_width); 851322810Shselasky err = -EINVAL; 852322810Shselasky } 853322810Shselasky 854322810Shselasky return err; 855322810Shselasky} 856322810Shselasky 857322810Shselaskyenum ib_max_vl_num { 858322810Shselasky __IB_MAX_VL_0 = 1, 859322810Shselasky __IB_MAX_VL_0_1 = 2, 860322810Shselasky __IB_MAX_VL_0_3 = 3, 861322810Shselasky __IB_MAX_VL_0_7 = 4, 862322810Shselasky __IB_MAX_VL_0_14 = 5, 863322810Shselasky}; 864322810Shselasky 865322810Shselaskyenum mlx5_vl_hw_cap { 866322810Shselasky MLX5_VL_HW_0 = 1, 867322810Shselasky MLX5_VL_HW_0_1 = 2, 868322810Shselasky MLX5_VL_HW_0_2 = 3, 869322810Shselasky MLX5_VL_HW_0_3 = 4, 870322810Shselasky MLX5_VL_HW_0_4 = 5, 871322810Shselasky MLX5_VL_HW_0_5 = 6, 872322810Shselasky MLX5_VL_HW_0_6 = 7, 873322810Shselasky MLX5_VL_HW_0_7 = 8, 874322810Shselasky MLX5_VL_HW_0_14 = 15 875322810Shselasky}; 876322810Shselasky 877322810Shselaskystatic int translate_max_vl_num(struct ib_device *ibdev, u8 vl_hw_cap, 878322810Shselasky u8 *max_vl_num) 879322810Shselasky{ 880322810Shselasky switch (vl_hw_cap) { 881322810Shselasky case MLX5_VL_HW_0: 882322810Shselasky *max_vl_num = __IB_MAX_VL_0; 883322810Shselasky break; 884322810Shselasky case MLX5_VL_HW_0_1: 885322810Shselasky *max_vl_num = __IB_MAX_VL_0_1; 886322810Shselasky break; 887322810Shselasky case MLX5_VL_HW_0_3: 888322810Shselasky *max_vl_num = __IB_MAX_VL_0_3; 889322810Shselasky break; 890322810Shselasky case MLX5_VL_HW_0_7: 891322810Shselasky *max_vl_num = __IB_MAX_VL_0_7; 892322810Shselasky break; 893322810Shselasky case MLX5_VL_HW_0_14: 894322810Shselasky *max_vl_num = __IB_MAX_VL_0_14; 895322810Shselasky break; 896322810Shselasky 897322810Shselasky default: 898322810Shselasky return -EINVAL; 899322810Shselasky } 900322810Shselasky 901322810Shselasky return 0; 902322810Shselasky} 903322810Shselasky 904331769Shselaskystatic int mlx5_query_hca_port(struct ib_device *ibdev, u8 port, 905331769Shselasky struct ib_port_attr *props) 906322810Shselasky{ 907322810Shselasky struct mlx5_ib_dev *dev = to_mdev(ibdev); 908322810Shselasky struct mlx5_core_dev *mdev = dev->mdev; 909322810Shselasky u32 *rep; 910331769Shselasky int replen = MLX5_ST_SZ_BYTES(query_hca_vport_context_out); 911322810Shselasky struct mlx5_ptys_reg *ptys; 912322810Shselasky struct mlx5_pmtu_reg *pmtu; 913322810Shselasky struct mlx5_pvlc_reg pvlc; 914322810Shselasky void *ctx; 915322810Shselasky int err; 916322810Shselasky 917331769Shselasky rep = mlx5_vzalloc(replen); 918322810Shselasky ptys = kzalloc(sizeof(*ptys), GFP_KERNEL); 919322810Shselasky pmtu = kzalloc(sizeof(*pmtu), GFP_KERNEL); 920322810Shselasky if (!rep || !ptys || !pmtu) { 921322810Shselasky err = -ENOMEM; 922322810Shselasky goto out; 923322810Shselasky } 924322810Shselasky 925322810Shselasky memset(props, 0, sizeof(*props)); 926322810Shselasky 927331769Shselasky err = mlx5_query_hca_vport_context(mdev, port, 0, rep, replen); 928322810Shselasky if (err) 929322810Shselasky goto out; 930322810Shselasky 931322810Shselasky ctx = MLX5_ADDR_OF(query_hca_vport_context_out, rep, hca_vport_context); 932322810Shselasky 933322810Shselasky props->lid = MLX5_GET(hca_vport_context, ctx, lid); 934322810Shselasky props->lmc = MLX5_GET(hca_vport_context, ctx, lmc); 935322810Shselasky props->sm_lid = MLX5_GET(hca_vport_context, ctx, sm_lid); 936322810Shselasky props->sm_sl = MLX5_GET(hca_vport_context, ctx, sm_sl); 937322810Shselasky props->state = MLX5_GET(hca_vport_context, ctx, vport_state); 938322810Shselasky props->phys_state = MLX5_GET(hca_vport_context, ctx, 939322810Shselasky port_physical_state); 940322810Shselasky props->port_cap_flags = MLX5_GET(hca_vport_context, ctx, cap_mask1); 941322810Shselasky props->gid_tbl_len = mlx5_get_gid_table_len(MLX5_CAP_GEN(mdev, gid_table_size)); 942322810Shselasky props->max_msg_sz = 1 << MLX5_CAP_GEN(mdev, log_max_msg); 943322810Shselasky props->pkey_tbl_len = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, pkey_table_size)); 944322810Shselasky props->bad_pkey_cntr = MLX5_GET(hca_vport_context, ctx, 945331769Shselasky pkey_violation_counter); 946322810Shselasky props->qkey_viol_cntr = MLX5_GET(hca_vport_context, ctx, 947331769Shselasky qkey_violation_counter); 948322810Shselasky props->subnet_timeout = MLX5_GET(hca_vport_context, ctx, 949331769Shselasky subnet_timeout); 950322810Shselasky props->init_type_reply = MLX5_GET(hca_vport_context, ctx, 951331769Shselasky init_type_reply); 952331769Shselasky props->grh_required = MLX5_GET(hca_vport_context, ctx, grh_required); 953322810Shselasky 954322810Shselasky ptys->proto_mask |= MLX5_PTYS_IB; 955322810Shselasky ptys->local_port = port; 956322810Shselasky err = mlx5_core_access_ptys(mdev, ptys, 0); 957322810Shselasky if (err) 958322810Shselasky goto out; 959322810Shselasky 960322810Shselasky err = translate_active_width(ibdev, ptys->ib_link_width_oper, 961322810Shselasky &props->active_width); 962322810Shselasky if (err) 963322810Shselasky goto out; 964322810Shselasky 965322810Shselasky props->active_speed = (u8)ptys->ib_proto_oper; 966322810Shselasky 967322810Shselasky pmtu->local_port = port; 968322810Shselasky err = mlx5_core_access_pmtu(mdev, pmtu, 0); 969322810Shselasky if (err) 970322810Shselasky goto out; 971322810Shselasky 972322810Shselasky props->max_mtu = pmtu->max_mtu; 973322810Shselasky props->active_mtu = pmtu->oper_mtu; 974322810Shselasky 975322810Shselasky memset(&pvlc, 0, sizeof(pvlc)); 976322810Shselasky pvlc.local_port = port; 977322810Shselasky err = mlx5_core_access_pvlc(mdev, &pvlc, 0); 978322810Shselasky if (err) 979322810Shselasky goto out; 980322810Shselasky 981322810Shselasky err = translate_max_vl_num(ibdev, pvlc.vl_hw_cap, 982322810Shselasky &props->max_vl_num); 983322810Shselaskyout: 984322810Shselasky kvfree(rep); 985322810Shselasky kfree(ptys); 986322810Shselasky kfree(pmtu); 987322810Shselasky return err; 988322810Shselasky} 989322810Shselasky 990322810Shselaskyint mlx5_ib_query_port(struct ib_device *ibdev, u8 port, 991322810Shselasky struct ib_port_attr *props) 992322810Shselasky{ 993322810Shselasky switch (mlx5_get_vport_access_method(ibdev)) { 994322810Shselasky case MLX5_VPORT_ACCESS_METHOD_MAD: 995331769Shselasky return mlx5_query_mad_ifc_port(ibdev, port, props); 996322810Shselasky 997322810Shselasky case MLX5_VPORT_ACCESS_METHOD_HCA: 998331769Shselasky return mlx5_query_hca_port(ibdev, port, props); 999322810Shselasky 1000322810Shselasky case MLX5_VPORT_ACCESS_METHOD_NIC: 1001322810Shselasky return mlx5_query_port_roce(ibdev, port, props); 1002322810Shselasky 1003322810Shselasky default: 1004322810Shselasky return -EINVAL; 1005322810Shselasky } 1006322810Shselasky} 1007322810Shselasky 1008322810Shselaskystatic int mlx5_ib_query_gid(struct ib_device *ibdev, u8 port, int index, 1009322810Shselasky union ib_gid *gid) 1010322810Shselasky{ 1011322810Shselasky struct mlx5_ib_dev *dev = to_mdev(ibdev); 1012322810Shselasky struct mlx5_core_dev *mdev = dev->mdev; 1013322810Shselasky 1014322810Shselasky switch (mlx5_get_vport_access_method(ibdev)) { 1015322810Shselasky case MLX5_VPORT_ACCESS_METHOD_MAD: 1016331769Shselasky return mlx5_query_mad_ifc_gids(ibdev, port, index, gid); 1017322810Shselasky 1018322810Shselasky case MLX5_VPORT_ACCESS_METHOD_HCA: 1019322810Shselasky return mlx5_query_hca_vport_gid(mdev, port, 0, index, gid); 1020322810Shselasky 1021322810Shselasky default: 1022322810Shselasky return -EINVAL; 1023322810Shselasky } 1024331769Shselasky 1025322810Shselasky} 1026322810Shselasky 1027322810Shselaskystatic int mlx5_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index, 1028322810Shselasky u16 *pkey) 1029322810Shselasky{ 1030322810Shselasky struct mlx5_ib_dev *dev = to_mdev(ibdev); 1031322810Shselasky struct mlx5_core_dev *mdev = dev->mdev; 1032322810Shselasky 1033322810Shselasky switch (mlx5_get_vport_access_method(ibdev)) { 1034322810Shselasky case MLX5_VPORT_ACCESS_METHOD_MAD: 1035331769Shselasky return mlx5_query_mad_ifc_pkey(ibdev, port, index, pkey); 1036322810Shselasky 1037322810Shselasky case MLX5_VPORT_ACCESS_METHOD_HCA: 1038322810Shselasky case MLX5_VPORT_ACCESS_METHOD_NIC: 1039331769Shselasky return mlx5_query_hca_vport_pkey(mdev, 0, port, 0, index, 1040322810Shselasky pkey); 1041322810Shselasky default: 1042322810Shselasky return -EINVAL; 1043322810Shselasky } 1044322810Shselasky} 1045322810Shselasky 1046322810Shselaskystatic int mlx5_ib_modify_device(struct ib_device *ibdev, int mask, 1047322810Shselasky struct ib_device_modify *props) 1048322810Shselasky{ 1049322810Shselasky struct mlx5_ib_dev *dev = to_mdev(ibdev); 1050322810Shselasky struct mlx5_reg_node_desc in; 1051322810Shselasky struct mlx5_reg_node_desc out; 1052322810Shselasky int err; 1053322810Shselasky 1054322810Shselasky if (mask & ~IB_DEVICE_MODIFY_NODE_DESC) 1055322810Shselasky return -EOPNOTSUPP; 1056322810Shselasky 1057322810Shselasky if (!(mask & IB_DEVICE_MODIFY_NODE_DESC)) 1058322810Shselasky return 0; 1059322810Shselasky 1060322810Shselasky /* 1061322810Shselasky * If possible, pass node desc to FW, so it can generate 1062322810Shselasky * a 144 trap. If cmd fails, just ignore. 1063322810Shselasky */ 1064331769Shselasky memcpy(&in, props->node_desc, IB_DEVICE_NODE_DESC_MAX); 1065322810Shselasky err = mlx5_core_access_reg(dev->mdev, &in, sizeof(in), &out, 1066322810Shselasky sizeof(out), MLX5_REG_NODE_DESC, 0, 1); 1067322810Shselasky if (err) 1068322810Shselasky return err; 1069322810Shselasky 1070331769Shselasky memcpy(ibdev->node_desc, props->node_desc, IB_DEVICE_NODE_DESC_MAX); 1071322810Shselasky 1072322810Shselasky return err; 1073322810Shselasky} 1074322810Shselasky 1075322810Shselaskystatic int mlx5_ib_modify_port(struct ib_device *ibdev, u8 port, int mask, 1076322810Shselasky struct ib_port_modify *props) 1077322810Shselasky{ 1078322810Shselasky struct mlx5_ib_dev *dev = to_mdev(ibdev); 1079322810Shselasky struct ib_port_attr attr; 1080322810Shselasky u32 tmp; 1081322810Shselasky int err; 1082322810Shselasky 1083347860Shselasky /* 1084347860Shselasky * CM layer calls ib_modify_port() regardless of the link 1085347860Shselasky * layer. For Ethernet ports, qkey violation and Port 1086347860Shselasky * capabilities are meaningless. 1087347860Shselasky */ 1088347860Shselasky if (mlx5_ib_port_link_layer(ibdev, port) == IB_LINK_LAYER_ETHERNET) 1089347860Shselasky return 0; 1090347860Shselasky 1091322810Shselasky mutex_lock(&dev->cap_mask_mutex); 1092322810Shselasky 1093322810Shselasky err = mlx5_ib_query_port(ibdev, port, &attr); 1094322810Shselasky if (err) 1095322810Shselasky goto out; 1096322810Shselasky 1097322810Shselasky tmp = (attr.port_cap_flags | props->set_port_cap_mask) & 1098322810Shselasky ~props->clr_port_cap_mask; 1099322810Shselasky 1100322810Shselasky err = mlx5_set_port_caps(dev->mdev, port, tmp); 1101322810Shselasky 1102322810Shselaskyout: 1103322810Shselasky mutex_unlock(&dev->cap_mask_mutex); 1104322810Shselasky return err; 1105322810Shselasky} 1106322810Shselasky 1107322810Shselaskystatic struct ib_ucontext *mlx5_ib_alloc_ucontext(struct ib_device *ibdev, 1108322810Shselasky struct ib_udata *udata) 1109322810Shselasky{ 1110322810Shselasky struct mlx5_ib_dev *dev = to_mdev(ibdev); 1111331769Shselasky struct mlx5_ib_alloc_ucontext_req_v2 req = {}; 1112331769Shselasky struct mlx5_ib_alloc_ucontext_resp resp = {}; 1113322810Shselasky struct mlx5_ib_ucontext *context; 1114322810Shselasky struct mlx5_uuar_info *uuari; 1115322810Shselasky struct mlx5_uar *uars; 1116322810Shselasky int gross_uuars; 1117322810Shselasky int num_uars; 1118322810Shselasky int ver; 1119322810Shselasky int uuarn; 1120322810Shselasky int err; 1121322810Shselasky int i; 1122322810Shselasky size_t reqlen; 1123331769Shselasky size_t min_req_v2 = offsetof(struct mlx5_ib_alloc_ucontext_req_v2, 1124331769Shselasky max_cqe_version); 1125322810Shselasky 1126322810Shselasky if (!dev->ib_active) 1127322810Shselasky return ERR_PTR(-EAGAIN); 1128322810Shselasky 1129331769Shselasky if (udata->inlen < sizeof(struct ib_uverbs_cmd_hdr)) 1130331769Shselasky return ERR_PTR(-EINVAL); 1131322810Shselasky 1132322810Shselasky reqlen = udata->inlen - sizeof(struct ib_uverbs_cmd_hdr); 1133322810Shselasky if (reqlen == sizeof(struct mlx5_ib_alloc_ucontext_req)) 1134322810Shselasky ver = 0; 1135331769Shselasky else if (reqlen >= min_req_v2) 1136322810Shselasky ver = 2; 1137331769Shselasky else 1138322810Shselasky return ERR_PTR(-EINVAL); 1139322810Shselasky 1140331769Shselasky err = ib_copy_from_udata(&req, udata, min(reqlen, sizeof(req))); 1141331769Shselasky if (err) 1142322810Shselasky return ERR_PTR(err); 1143322810Shselasky 1144331769Shselasky if (req.flags) 1145322810Shselasky return ERR_PTR(-EINVAL); 1146322810Shselasky 1147331769Shselasky if (req.total_num_uuars > MLX5_MAX_UUARS) 1148322810Shselasky return ERR_PTR(-ENOMEM); 1149322810Shselasky 1150331769Shselasky if (req.total_num_uuars == 0) 1151331769Shselasky return ERR_PTR(-EINVAL); 1152331769Shselasky 1153331769Shselasky if (req.comp_mask || req.reserved0 || req.reserved1 || req.reserved2) 1154331769Shselasky return ERR_PTR(-EOPNOTSUPP); 1155331769Shselasky 1156331769Shselasky if (reqlen > sizeof(req) && 1157331769Shselasky !ib_is_udata_cleared(udata, sizeof(req), 1158331769Shselasky reqlen - sizeof(req))) 1159331769Shselasky return ERR_PTR(-EOPNOTSUPP); 1160331769Shselasky 1161322810Shselasky req.total_num_uuars = ALIGN(req.total_num_uuars, 1162322810Shselasky MLX5_NON_FP_BF_REGS_PER_PAGE); 1163331769Shselasky if (req.num_low_latency_uuars > req.total_num_uuars - 1) 1164322810Shselasky return ERR_PTR(-EINVAL); 1165322810Shselasky 1166322810Shselasky num_uars = req.total_num_uuars / MLX5_NON_FP_BF_REGS_PER_PAGE; 1167322810Shselasky gross_uuars = num_uars * MLX5_BF_REGS_PER_PAGE; 1168322810Shselasky resp.qp_tab_size = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp); 1169322810Shselasky if (mlx5_core_is_pf(dev->mdev) && MLX5_CAP_GEN(dev->mdev, bf)) 1170322810Shselasky resp.bf_reg_size = 1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size); 1171331769Shselasky resp.cache_line_size = cache_line_size(); 1172322810Shselasky resp.max_sq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq); 1173322810Shselasky resp.max_rq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq); 1174322810Shselasky resp.max_send_wqebb = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz); 1175322810Shselasky resp.max_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz); 1176322810Shselasky resp.max_srq_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz); 1177331769Shselasky resp.cqe_version = min_t(__u8, 1178331769Shselasky (__u8)MLX5_CAP_GEN(dev->mdev, cqe_version), 1179331769Shselasky req.max_cqe_version); 1180331769Shselasky resp.response_length = min(offsetof(typeof(resp), response_length) + 1181331769Shselasky sizeof(resp.response_length), udata->outlen); 1182322810Shselasky 1183322810Shselasky context = kzalloc(sizeof(*context), GFP_KERNEL); 1184322810Shselasky if (!context) 1185322810Shselasky return ERR_PTR(-ENOMEM); 1186322810Shselasky 1187322810Shselasky uuari = &context->uuari; 1188322810Shselasky mutex_init(&uuari->lock); 1189322810Shselasky uars = kcalloc(num_uars, sizeof(*uars), GFP_KERNEL); 1190322810Shselasky if (!uars) { 1191322810Shselasky err = -ENOMEM; 1192322810Shselasky goto out_ctx; 1193322810Shselasky } 1194322810Shselasky 1195322810Shselasky uuari->bitmap = kcalloc(BITS_TO_LONGS(gross_uuars), 1196322810Shselasky sizeof(*uuari->bitmap), 1197322810Shselasky GFP_KERNEL); 1198322810Shselasky if (!uuari->bitmap) { 1199322810Shselasky err = -ENOMEM; 1200322810Shselasky goto out_uar_ctx; 1201322810Shselasky } 1202322810Shselasky /* 1203322810Shselasky * clear all fast path uuars 1204322810Shselasky */ 1205322810Shselasky for (i = 0; i < gross_uuars; i++) { 1206322810Shselasky uuarn = i & 3; 1207322810Shselasky if (uuarn == 2 || uuarn == 3) 1208322810Shselasky set_bit(i, uuari->bitmap); 1209322810Shselasky } 1210322810Shselasky 1211322810Shselasky uuari->count = kcalloc(gross_uuars, sizeof(*uuari->count), GFP_KERNEL); 1212322810Shselasky if (!uuari->count) { 1213322810Shselasky err = -ENOMEM; 1214322810Shselasky goto out_bitmap; 1215322810Shselasky } 1216322810Shselasky 1217322810Shselasky for (i = 0; i < num_uars; i++) { 1218322810Shselasky err = mlx5_cmd_alloc_uar(dev->mdev, &uars[i].index); 1219331769Shselasky if (err) 1220331769Shselasky goto out_count; 1221331769Shselasky } 1222331769Shselasky 1223331769Shselasky#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING 1224331769Shselasky context->ibucontext.invalidate_range = &mlx5_ib_invalidate_range; 1225331769Shselasky#endif 1226331769Shselasky 1227331769Shselasky if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain)) { 1228331769Shselasky err = mlx5_alloc_transport_domain(dev->mdev, 1229331769Shselasky &context->tdn); 1230331769Shselasky if (err) 1231322810Shselasky goto out_uars; 1232322810Shselasky } 1233322810Shselasky 1234331769Shselasky INIT_LIST_HEAD(&context->vma_private_list); 1235322810Shselasky INIT_LIST_HEAD(&context->db_page_list); 1236322810Shselasky mutex_init(&context->db_page_mutex); 1237322810Shselasky 1238322810Shselasky resp.tot_uuars = req.total_num_uuars; 1239322810Shselasky resp.num_ports = MLX5_CAP_GEN(dev->mdev, num_ports); 1240331769Shselasky 1241331769Shselasky if (field_avail(typeof(resp), cqe_version, udata->outlen)) 1242331769Shselasky resp.response_length += sizeof(resp.cqe_version); 1243331769Shselasky 1244331769Shselasky if (field_avail(typeof(resp), cmds_supp_uhw, udata->outlen)) { 1245331784Shselasky resp.cmds_supp_uhw |= MLX5_USER_CMDS_SUPP_UHW_QUERY_DEVICE | 1246331784Shselasky MLX5_USER_CMDS_SUPP_UHW_CREATE_AH; 1247331769Shselasky resp.response_length += sizeof(resp.cmds_supp_uhw); 1248331769Shselasky } 1249331769Shselasky 1250331769Shselasky /* 1251331769Shselasky * We don't want to expose information from the PCI bar that is located 1252331769Shselasky * after 4096 bytes, so if the arch only supports larger pages, let's 1253331769Shselasky * pretend we don't support reading the HCA's core clock. This is also 1254331769Shselasky * forced by mmap function. 1255331769Shselasky */ 1256331769Shselasky if (PAGE_SIZE <= 4096 && 1257331769Shselasky field_avail(typeof(resp), hca_core_clock_offset, udata->outlen)) { 1258331769Shselasky resp.comp_mask |= 1259331769Shselasky MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_CORE_CLOCK_OFFSET; 1260331769Shselasky resp.hca_core_clock_offset = 1261331769Shselasky offsetof(struct mlx5_init_seg, internal_timer_h) % 1262331769Shselasky PAGE_SIZE; 1263331769Shselasky resp.response_length += sizeof(resp.hca_core_clock_offset) + 1264331769Shselasky sizeof(resp.reserved2); 1265331769Shselasky } 1266331769Shselasky 1267331769Shselasky err = ib_copy_to_udata(udata, &resp, resp.response_length); 1268322810Shselasky if (err) 1269331769Shselasky goto out_td; 1270322810Shselasky 1271322810Shselasky uuari->ver = ver; 1272322810Shselasky uuari->num_low_latency_uuars = req.num_low_latency_uuars; 1273322810Shselasky uuari->uars = uars; 1274322810Shselasky uuari->num_uars = num_uars; 1275331769Shselasky context->cqe_version = resp.cqe_version; 1276322810Shselasky 1277322810Shselasky return &context->ibucontext; 1278322810Shselasky 1279331769Shselaskyout_td: 1280331769Shselasky if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain)) 1281331769Shselasky mlx5_dealloc_transport_domain(dev->mdev, context->tdn); 1282331769Shselasky 1283322810Shselaskyout_uars: 1284322810Shselasky for (i--; i >= 0; i--) 1285322810Shselasky mlx5_cmd_free_uar(dev->mdev, uars[i].index); 1286331769Shselaskyout_count: 1287322810Shselasky kfree(uuari->count); 1288322810Shselasky 1289322810Shselaskyout_bitmap: 1290322810Shselasky kfree(uuari->bitmap); 1291322810Shselasky 1292322810Shselaskyout_uar_ctx: 1293322810Shselasky kfree(uars); 1294322810Shselasky 1295322810Shselaskyout_ctx: 1296322810Shselasky kfree(context); 1297322810Shselasky return ERR_PTR(err); 1298322810Shselasky} 1299322810Shselasky 1300322810Shselaskystatic int mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext) 1301322810Shselasky{ 1302322810Shselasky struct mlx5_ib_ucontext *context = to_mucontext(ibcontext); 1303322810Shselasky struct mlx5_ib_dev *dev = to_mdev(ibcontext->device); 1304322810Shselasky struct mlx5_uuar_info *uuari = &context->uuari; 1305322810Shselasky int i; 1306322810Shselasky 1307331769Shselasky if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain)) 1308322810Shselasky mlx5_dealloc_transport_domain(dev->mdev, context->tdn); 1309322810Shselasky 1310322810Shselasky for (i = 0; i < uuari->num_uars; i++) { 1311322810Shselasky if (mlx5_cmd_free_uar(dev->mdev, uuari->uars[i].index)) 1312322810Shselasky mlx5_ib_warn(dev, "failed to free UAR 0x%x\n", uuari->uars[i].index); 1313322810Shselasky } 1314322810Shselasky 1315322810Shselasky kfree(uuari->count); 1316322810Shselasky kfree(uuari->bitmap); 1317322810Shselasky kfree(uuari->uars); 1318322810Shselasky kfree(context); 1319322810Shselasky 1320322810Shselasky return 0; 1321322810Shselasky} 1322322810Shselasky 1323322810Shselaskystatic phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev, int index) 1324322810Shselasky{ 1325322810Shselasky return (pci_resource_start(dev->mdev->pdev, 0) >> PAGE_SHIFT) + index; 1326322810Shselasky} 1327322810Shselasky 1328322810Shselaskystatic int get_command(unsigned long offset) 1329322810Shselasky{ 1330322810Shselasky return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK; 1331322810Shselasky} 1332322810Shselasky 1333322810Shselaskystatic int get_arg(unsigned long offset) 1334322810Shselasky{ 1335322810Shselasky return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1); 1336322810Shselasky} 1337322810Shselasky 1338322810Shselaskystatic int get_index(unsigned long offset) 1339322810Shselasky{ 1340322810Shselasky return get_arg(offset); 1341322810Shselasky} 1342322810Shselasky 1343331769Shselaskystatic void mlx5_ib_vma_open(struct vm_area_struct *area) 1344331769Shselasky{ 1345331769Shselasky /* vma_open is called when a new VMA is created on top of our VMA. This 1346331769Shselasky * is done through either mremap flow or split_vma (usually due to 1347331769Shselasky * mlock, madvise, munmap, etc.) We do not support a clone of the VMA, 1348331769Shselasky * as this VMA is strongly hardware related. Therefore we set the 1349331769Shselasky * vm_ops of the newly created/cloned VMA to NULL, to prevent it from 1350331769Shselasky * calling us again and trying to do incorrect actions. We assume that 1351331769Shselasky * the original VMA size is exactly a single page, and therefore all 1352331769Shselasky * "splitting" operation will not happen to it. 1353331769Shselasky */ 1354331769Shselasky area->vm_ops = NULL; 1355331769Shselasky} 1356331769Shselasky 1357331769Shselaskystatic void mlx5_ib_vma_close(struct vm_area_struct *area) 1358331769Shselasky{ 1359331769Shselasky struct mlx5_ib_vma_private_data *mlx5_ib_vma_priv_data; 1360331769Shselasky 1361331769Shselasky /* It's guaranteed that all VMAs opened on a FD are closed before the 1362331769Shselasky * file itself is closed, therefore no sync is needed with the regular 1363331769Shselasky * closing flow. (e.g. mlx5 ib_dealloc_ucontext) 1364331769Shselasky * However need a sync with accessing the vma as part of 1365331769Shselasky * mlx5_ib_disassociate_ucontext. 1366331769Shselasky * The close operation is usually called under mm->mmap_sem except when 1367331769Shselasky * process is exiting. 1368331769Shselasky * The exiting case is handled explicitly as part of 1369331769Shselasky * mlx5_ib_disassociate_ucontext. 1370331769Shselasky */ 1371331769Shselasky mlx5_ib_vma_priv_data = (struct mlx5_ib_vma_private_data *)area->vm_private_data; 1372331769Shselasky 1373331769Shselasky /* setting the vma context pointer to null in the mlx5_ib driver's 1374331769Shselasky * private data, to protect a race condition in 1375331769Shselasky * mlx5_ib_disassociate_ucontext(). 1376331769Shselasky */ 1377331769Shselasky mlx5_ib_vma_priv_data->vma = NULL; 1378331769Shselasky list_del(&mlx5_ib_vma_priv_data->list); 1379331769Shselasky kfree(mlx5_ib_vma_priv_data); 1380331769Shselasky} 1381331769Shselasky 1382331769Shselaskystatic const struct vm_operations_struct mlx5_ib_vm_ops = { 1383331769Shselasky .open = mlx5_ib_vma_open, 1384331769Shselasky .close = mlx5_ib_vma_close 1385331769Shselasky}; 1386331769Shselasky 1387331769Shselaskystatic int mlx5_ib_set_vma_data(struct vm_area_struct *vma, 1388331769Shselasky struct mlx5_ib_ucontext *ctx) 1389331769Shselasky{ 1390331769Shselasky struct mlx5_ib_vma_private_data *vma_prv; 1391331769Shselasky struct list_head *vma_head = &ctx->vma_private_list; 1392331769Shselasky 1393331769Shselasky vma_prv = kzalloc(sizeof(*vma_prv), GFP_KERNEL); 1394331769Shselasky if (!vma_prv) 1395331769Shselasky return -ENOMEM; 1396331769Shselasky 1397331769Shselasky vma_prv->vma = vma; 1398331769Shselasky vma->vm_private_data = vma_prv; 1399331769Shselasky vma->vm_ops = &mlx5_ib_vm_ops; 1400331769Shselasky 1401331769Shselasky list_add(&vma_prv->list, vma_head); 1402331769Shselasky 1403331769Shselasky return 0; 1404331769Shselasky} 1405331769Shselasky 1406347801Shselaskystatic void mlx5_ib_disassociate_ucontext(struct ib_ucontext *ibcontext) 1407347801Shselasky{ 1408347801Shselasky int ret; 1409347801Shselasky struct vm_area_struct *vma; 1410347801Shselasky struct mlx5_ib_vma_private_data *vma_private, *n; 1411347801Shselasky struct mlx5_ib_ucontext *context = to_mucontext(ibcontext); 1412347801Shselasky struct task_struct *owning_process = NULL; 1413347801Shselasky struct mm_struct *owning_mm = NULL; 1414347801Shselasky 1415347801Shselasky owning_process = get_pid_task(ibcontext->tgid, PIDTYPE_PID); 1416347801Shselasky if (!owning_process) 1417347801Shselasky return; 1418347801Shselasky 1419347801Shselasky owning_mm = get_task_mm(owning_process); 1420347801Shselasky if (!owning_mm) { 1421347801Shselasky pr_info("no mm, disassociate ucontext is pending task termination\n"); 1422347801Shselasky while (1) { 1423347801Shselasky put_task_struct(owning_process); 1424347801Shselasky usleep_range(1000, 2000); 1425347801Shselasky owning_process = get_pid_task(ibcontext->tgid, 1426347801Shselasky PIDTYPE_PID); 1427347801Shselasky if (!owning_process || owning_process->task_thread-> 1428347801Shselasky td_proc->p_state == PRS_ZOMBIE) { 1429347801Shselasky pr_info("disassociate ucontext done, task was terminated\n"); 1430347801Shselasky /* in case task was dead need to release the 1431347801Shselasky * task struct. 1432347801Shselasky */ 1433347801Shselasky if (owning_process) 1434347801Shselasky put_task_struct(owning_process); 1435347801Shselasky return; 1436347801Shselasky } 1437347801Shselasky } 1438347801Shselasky } 1439347801Shselasky 1440347801Shselasky /* need to protect from a race on closing the vma as part of 1441347801Shselasky * mlx5_ib_vma_close. 1442347801Shselasky */ 1443347801Shselasky down_write(&owning_mm->mmap_sem); 1444347801Shselasky list_for_each_entry_safe(vma_private, n, &context->vma_private_list, 1445347801Shselasky list) { 1446347801Shselasky vma = vma_private->vma; 1447347801Shselasky ret = zap_vma_ptes(vma, vma->vm_start, 1448347801Shselasky PAGE_SIZE); 1449347801Shselasky if (ret == -ENOTSUP) { 1450347801Shselasky if (bootverbose) 1451347801Shselasky WARN_ONCE( 1452347801Shselasky "%s: zap_vma_ptes not implemented for unmanaged mappings", __func__); 1453347801Shselasky } else { 1454347801Shselasky WARN(ret, "%s: zap_vma_ptes failed, error %d", 1455347801Shselasky __func__, -ret); 1456347801Shselasky } 1457347801Shselasky /* context going to be destroyed, should 1458347801Shselasky * not access ops any more. 1459347801Shselasky */ 1460347801Shselasky /* XXXKIB vma->vm_flags &= ~(VM_SHARED | VM_MAYSHARE); */ 1461347801Shselasky vma->vm_ops = NULL; 1462347801Shselasky list_del(&vma_private->list); 1463347801Shselasky kfree(vma_private); 1464347801Shselasky } 1465347801Shselasky up_write(&owning_mm->mmap_sem); 1466347801Shselasky mmput(owning_mm); 1467347801Shselasky put_task_struct(owning_process); 1468347801Shselasky} 1469347801Shselasky 1470331769Shselaskystatic inline char *mmap_cmd2str(enum mlx5_ib_mmap_cmd cmd) 1471331769Shselasky{ 1472331769Shselasky switch (cmd) { 1473331769Shselasky case MLX5_IB_MMAP_WC_PAGE: 1474331769Shselasky return "WC"; 1475331769Shselasky case MLX5_IB_MMAP_REGULAR_PAGE: 1476331769Shselasky return "best effort WC"; 1477331769Shselasky case MLX5_IB_MMAP_NC_PAGE: 1478331769Shselasky return "NC"; 1479331769Shselasky default: 1480331769Shselasky return NULL; 1481331769Shselasky } 1482331769Shselasky} 1483331769Shselasky 1484331769Shselaskystatic int uar_mmap(struct mlx5_ib_dev *dev, enum mlx5_ib_mmap_cmd cmd, 1485331769Shselasky struct vm_area_struct *vma, 1486322810Shselasky struct mlx5_ib_ucontext *context) 1487322810Shselasky{ 1488331769Shselasky struct mlx5_uuar_info *uuari = &context->uuari; 1489331769Shselasky int err; 1490322810Shselasky unsigned long idx; 1491331769Shselasky phys_addr_t pfn, pa; 1492331769Shselasky pgprot_t prot; 1493322810Shselasky 1494331769Shselasky switch (cmd) { 1495331769Shselasky case MLX5_IB_MMAP_WC_PAGE: 1496331769Shselasky/* Some architectures don't support WC memory */ 1497331769Shselasky#if defined(CONFIG_X86) 1498331769Shselasky if (!pat_enabled()) 1499331769Shselasky return -EPERM; 1500331769Shselasky#elif !(defined(CONFIG_PPC) || (defined(CONFIG_ARM) && defined(CONFIG_MMU))) 1501331769Shselasky return -EPERM; 1502331769Shselasky#endif 1503331769Shselasky /* fall through */ 1504331769Shselasky case MLX5_IB_MMAP_REGULAR_PAGE: 1505331769Shselasky /* For MLX5_IB_MMAP_REGULAR_PAGE do the best effort to get WC */ 1506331769Shselasky prot = pgprot_writecombine(vma->vm_page_prot); 1507331769Shselasky break; 1508331769Shselasky case MLX5_IB_MMAP_NC_PAGE: 1509331769Shselasky prot = pgprot_noncached(vma->vm_page_prot); 1510331769Shselasky break; 1511331769Shselasky default: 1512322810Shselasky return -EINVAL; 1513322810Shselasky } 1514322810Shselasky 1515331769Shselasky if (vma->vm_end - vma->vm_start != PAGE_SIZE) 1516331769Shselasky return -EINVAL; 1517331769Shselasky 1518322810Shselasky idx = get_index(vma->vm_pgoff); 1519331769Shselasky if (idx >= uuari->num_uars) 1520322810Shselasky return -EINVAL; 1521322810Shselasky 1522322810Shselasky pfn = uar_index2pfn(dev, uuari->uars[idx].index); 1523331769Shselasky mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn %pa\n", idx, &pfn); 1524322810Shselasky 1525322810Shselasky vma->vm_page_prot = prot; 1526331769Shselasky err = io_remap_pfn_range(vma, vma->vm_start, pfn, 1527331769Shselasky PAGE_SIZE, vma->vm_page_prot); 1528331769Shselasky if (err) { 1529331769Shselasky mlx5_ib_err(dev, "io_remap_pfn_range failed with error=%d, vm_start=0x%llx, pfn=%pa, mmap_cmd=%s\n", 1530331769Shselasky err, (unsigned long long)vma->vm_start, &pfn, mmap_cmd2str(cmd)); 1531322810Shselasky return -EAGAIN; 1532322810Shselasky } 1533322810Shselasky 1534331769Shselasky pa = pfn << PAGE_SHIFT; 1535331769Shselasky mlx5_ib_dbg(dev, "mapped %s at 0x%llx, PA %pa\n", mmap_cmd2str(cmd), 1536331769Shselasky (unsigned long long)vma->vm_start, &pa); 1537322810Shselasky 1538331769Shselasky return mlx5_ib_set_vma_data(vma, context); 1539322810Shselasky} 1540322810Shselasky 1541322810Shselaskystatic int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma) 1542322810Shselasky{ 1543322810Shselasky struct mlx5_ib_ucontext *context = to_mucontext(ibcontext); 1544322810Shselasky struct mlx5_ib_dev *dev = to_mdev(ibcontext->device); 1545322810Shselasky unsigned long command; 1546331769Shselasky phys_addr_t pfn; 1547322810Shselasky 1548322810Shselasky command = get_command(vma->vm_pgoff); 1549322810Shselasky switch (command) { 1550331769Shselasky case MLX5_IB_MMAP_WC_PAGE: 1551331769Shselasky case MLX5_IB_MMAP_NC_PAGE: 1552322810Shselasky case MLX5_IB_MMAP_REGULAR_PAGE: 1553331769Shselasky return uar_mmap(dev, command, vma, context); 1554322810Shselasky 1555331769Shselasky case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES: 1556331769Shselasky return -ENOSYS; 1557322810Shselasky 1558331769Shselasky case MLX5_IB_MMAP_CORE_CLOCK: 1559331769Shselasky if (vma->vm_end - vma->vm_start != PAGE_SIZE) 1560331769Shselasky return -EINVAL; 1561322810Shselasky 1562331769Shselasky if (vma->vm_flags & VM_WRITE) 1563331769Shselasky return -EPERM; 1564331769Shselasky 1565331769Shselasky /* Don't expose to user-space information it shouldn't have */ 1566331769Shselasky if (PAGE_SIZE > 4096) 1567331769Shselasky return -EOPNOTSUPP; 1568331769Shselasky 1569331769Shselasky vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); 1570331769Shselasky pfn = (dev->mdev->iseg_base + 1571331769Shselasky offsetof(struct mlx5_init_seg, internal_timer_h)) >> 1572331769Shselasky PAGE_SHIFT; 1573331769Shselasky if (io_remap_pfn_range(vma, vma->vm_start, pfn, 1574331769Shselasky PAGE_SIZE, vma->vm_page_prot)) 1575331769Shselasky return -EAGAIN; 1576331769Shselasky 1577331769Shselasky mlx5_ib_dbg(dev, "mapped internal timer at 0x%llx, PA 0x%llx\n", 1578331769Shselasky (unsigned long long)vma->vm_start, 1579331769Shselasky (unsigned long long)pfn << PAGE_SHIFT); 1580322810Shselasky break; 1581322810Shselasky 1582322810Shselasky default: 1583322810Shselasky return -EINVAL; 1584322810Shselasky } 1585322810Shselasky 1586322810Shselasky return 0; 1587322810Shselasky} 1588322810Shselasky 1589322810Shselaskystatic struct ib_pd *mlx5_ib_alloc_pd(struct ib_device *ibdev, 1590322810Shselasky struct ib_ucontext *context, 1591322810Shselasky struct ib_udata *udata) 1592322810Shselasky{ 1593322810Shselasky struct mlx5_ib_alloc_pd_resp resp; 1594322810Shselasky struct mlx5_ib_pd *pd; 1595322810Shselasky int err; 1596322810Shselasky 1597322810Shselasky pd = kmalloc(sizeof(*pd), GFP_KERNEL); 1598322810Shselasky if (!pd) 1599322810Shselasky return ERR_PTR(-ENOMEM); 1600322810Shselasky 1601322810Shselasky err = mlx5_core_alloc_pd(to_mdev(ibdev)->mdev, &pd->pdn); 1602322810Shselasky if (err) { 1603322810Shselasky kfree(pd); 1604322810Shselasky return ERR_PTR(err); 1605322810Shselasky } 1606322810Shselasky 1607322810Shselasky if (context) { 1608322810Shselasky resp.pdn = pd->pdn; 1609322810Shselasky if (ib_copy_to_udata(udata, &resp, sizeof(resp))) { 1610322810Shselasky mlx5_core_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn); 1611322810Shselasky kfree(pd); 1612322810Shselasky return ERR_PTR(-EFAULT); 1613322810Shselasky } 1614322810Shselasky } 1615322810Shselasky 1616322810Shselasky return &pd->ibpd; 1617322810Shselasky} 1618322810Shselasky 1619322810Shselaskystatic int mlx5_ib_dealloc_pd(struct ib_pd *pd) 1620322810Shselasky{ 1621322810Shselasky struct mlx5_ib_dev *mdev = to_mdev(pd->device); 1622322810Shselasky struct mlx5_ib_pd *mpd = to_mpd(pd); 1623322810Shselasky 1624322810Shselasky mlx5_core_dealloc_pd(mdev->mdev, mpd->pdn); 1625322810Shselasky kfree(mpd); 1626322810Shselasky 1627322810Shselasky return 0; 1628322810Shselasky} 1629322810Shselasky 1630331769Shselaskyenum { 1631331769Shselasky MATCH_CRITERIA_ENABLE_OUTER_BIT, 1632331769Shselasky MATCH_CRITERIA_ENABLE_MISC_BIT, 1633331769Shselasky MATCH_CRITERIA_ENABLE_INNER_BIT 1634331769Shselasky}; 1635331769Shselasky 1636331769Shselasky#define HEADER_IS_ZERO(match_criteria, headers) \ 1637331769Shselasky !(memchr_inv(MLX5_ADDR_OF(fte_match_param, match_criteria, headers), \ 1638331769Shselasky 0, MLX5_FLD_SZ_BYTES(fte_match_param, headers))) \ 1639331769Shselasky 1640331769Shselaskystatic u8 get_match_criteria_enable(u32 *match_criteria) 1641331769Shselasky{ 1642331769Shselasky u8 match_criteria_enable; 1643331769Shselasky 1644331769Shselasky match_criteria_enable = 1645331769Shselasky (!HEADER_IS_ZERO(match_criteria, outer_headers)) << 1646331769Shselasky MATCH_CRITERIA_ENABLE_OUTER_BIT; 1647331769Shselasky match_criteria_enable |= 1648331769Shselasky (!HEADER_IS_ZERO(match_criteria, misc_parameters)) << 1649331769Shselasky MATCH_CRITERIA_ENABLE_MISC_BIT; 1650331769Shselasky match_criteria_enable |= 1651331769Shselasky (!HEADER_IS_ZERO(match_criteria, inner_headers)) << 1652331769Shselasky MATCH_CRITERIA_ENABLE_INNER_BIT; 1653331769Shselasky 1654331769Shselasky return match_criteria_enable; 1655331769Shselasky} 1656331769Shselasky 1657331769Shselaskystatic void set_proto(void *outer_c, void *outer_v, u8 mask, u8 val) 1658331769Shselasky{ 1659331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_protocol, mask); 1660331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_protocol, val); 1661331769Shselasky} 1662331769Shselasky 1663331769Shselaskystatic void set_tos(void *outer_c, void *outer_v, u8 mask, u8 val) 1664331769Shselasky{ 1665331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_ecn, mask); 1666331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_ecn, val); 1667331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_dscp, mask >> 2); 1668331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_dscp, val >> 2); 1669331769Shselasky} 1670331769Shselasky 1671331769Shselasky#define LAST_ETH_FIELD vlan_tag 1672331769Shselasky#define LAST_IB_FIELD sl 1673331769Shselasky#define LAST_IPV4_FIELD tos 1674331769Shselasky#define LAST_IPV6_FIELD traffic_class 1675331769Shselasky#define LAST_TCP_UDP_FIELD src_port 1676331769Shselasky 1677331769Shselasky/* Field is the last supported field */ 1678331769Shselasky#define FIELDS_NOT_SUPPORTED(filter, field)\ 1679331769Shselasky memchr_inv((void *)&filter.field +\ 1680331769Shselasky sizeof(filter.field), 0,\ 1681331769Shselasky sizeof(filter) -\ 1682331769Shselasky offsetof(typeof(filter), field) -\ 1683331769Shselasky sizeof(filter.field)) 1684331769Shselasky 1685331769Shselaskystatic int parse_flow_attr(u32 *match_c, u32 *match_v, 1686331769Shselasky const union ib_flow_spec *ib_spec) 1687331769Shselasky{ 1688331769Shselasky void *outer_headers_c = MLX5_ADDR_OF(fte_match_param, match_c, 1689331769Shselasky outer_headers); 1690331769Shselasky void *outer_headers_v = MLX5_ADDR_OF(fte_match_param, match_v, 1691331769Shselasky outer_headers); 1692331769Shselasky void *misc_params_c = MLX5_ADDR_OF(fte_match_param, match_c, 1693331769Shselasky misc_parameters); 1694331769Shselasky void *misc_params_v = MLX5_ADDR_OF(fte_match_param, match_v, 1695331769Shselasky misc_parameters); 1696331769Shselasky 1697331769Shselasky switch (ib_spec->type) { 1698331769Shselasky case IB_FLOW_SPEC_ETH: 1699331769Shselasky if (FIELDS_NOT_SUPPORTED(ib_spec->eth.mask, LAST_ETH_FIELD)) 1700331769Shselasky return -ENOTSUPP; 1701331769Shselasky 1702331769Shselasky ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_c, 1703331769Shselasky dmac_47_16), 1704331769Shselasky ib_spec->eth.mask.dst_mac); 1705331769Shselasky ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_v, 1706331769Shselasky dmac_47_16), 1707331769Shselasky ib_spec->eth.val.dst_mac); 1708331769Shselasky 1709331769Shselasky ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_c, 1710331769Shselasky smac_47_16), 1711331769Shselasky ib_spec->eth.mask.src_mac); 1712331769Shselasky ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_v, 1713331769Shselasky smac_47_16), 1714331769Shselasky ib_spec->eth.val.src_mac); 1715331769Shselasky 1716331769Shselasky if (ib_spec->eth.mask.vlan_tag) { 1717331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, 1718331769Shselasky cvlan_tag, 1); 1719331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, 1720331769Shselasky cvlan_tag, 1); 1721331769Shselasky 1722331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, 1723331769Shselasky first_vid, ntohs(ib_spec->eth.mask.vlan_tag)); 1724331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, 1725331769Shselasky first_vid, ntohs(ib_spec->eth.val.vlan_tag)); 1726331769Shselasky 1727331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, 1728331769Shselasky first_cfi, 1729331769Shselasky ntohs(ib_spec->eth.mask.vlan_tag) >> 12); 1730331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, 1731331769Shselasky first_cfi, 1732331769Shselasky ntohs(ib_spec->eth.val.vlan_tag) >> 12); 1733331769Shselasky 1734331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, 1735331769Shselasky first_prio, 1736331769Shselasky ntohs(ib_spec->eth.mask.vlan_tag) >> 13); 1737331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, 1738331769Shselasky first_prio, 1739331769Shselasky ntohs(ib_spec->eth.val.vlan_tag) >> 13); 1740331769Shselasky } 1741331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, 1742331769Shselasky ethertype, ntohs(ib_spec->eth.mask.ether_type)); 1743331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, 1744331769Shselasky ethertype, ntohs(ib_spec->eth.val.ether_type)); 1745331769Shselasky break; 1746331769Shselasky case IB_FLOW_SPEC_IPV4: 1747331769Shselasky if (FIELDS_NOT_SUPPORTED(ib_spec->ipv4.mask, LAST_IPV4_FIELD)) 1748331769Shselasky return -ENOTSUPP; 1749331769Shselasky 1750331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, 1751331769Shselasky ethertype, 0xffff); 1752331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, 1753331769Shselasky ethertype, ETH_P_IP); 1754331769Shselasky 1755331769Shselasky memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_c, 1756331769Shselasky src_ipv4_src_ipv6.ipv4_layout.ipv4), 1757331769Shselasky &ib_spec->ipv4.mask.src_ip, 1758331769Shselasky sizeof(ib_spec->ipv4.mask.src_ip)); 1759331769Shselasky memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_v, 1760331769Shselasky src_ipv4_src_ipv6.ipv4_layout.ipv4), 1761331769Shselasky &ib_spec->ipv4.val.src_ip, 1762331769Shselasky sizeof(ib_spec->ipv4.val.src_ip)); 1763331769Shselasky memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_c, 1764331769Shselasky dst_ipv4_dst_ipv6.ipv4_layout.ipv4), 1765331769Shselasky &ib_spec->ipv4.mask.dst_ip, 1766331769Shselasky sizeof(ib_spec->ipv4.mask.dst_ip)); 1767331769Shselasky memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_v, 1768331769Shselasky dst_ipv4_dst_ipv6.ipv4_layout.ipv4), 1769331769Shselasky &ib_spec->ipv4.val.dst_ip, 1770331769Shselasky sizeof(ib_spec->ipv4.val.dst_ip)); 1771331769Shselasky 1772331769Shselasky set_tos(outer_headers_c, outer_headers_v, 1773331769Shselasky ib_spec->ipv4.mask.tos, ib_spec->ipv4.val.tos); 1774331769Shselasky 1775331769Shselasky set_proto(outer_headers_c, outer_headers_v, 1776331769Shselasky ib_spec->ipv4.mask.proto, ib_spec->ipv4.val.proto); 1777331769Shselasky break; 1778331769Shselasky case IB_FLOW_SPEC_IPV6: 1779331769Shselasky if (FIELDS_NOT_SUPPORTED(ib_spec->ipv6.mask, LAST_IPV6_FIELD)) 1780331769Shselasky return -ENOTSUPP; 1781331769Shselasky 1782331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, 1783331769Shselasky ethertype, 0xffff); 1784331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, 1785331769Shselasky ethertype, IPPROTO_IPV6); 1786331769Shselasky 1787331769Shselasky memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_c, 1788331769Shselasky src_ipv4_src_ipv6.ipv6_layout.ipv6), 1789331769Shselasky &ib_spec->ipv6.mask.src_ip, 1790331769Shselasky sizeof(ib_spec->ipv6.mask.src_ip)); 1791331769Shselasky memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_v, 1792331769Shselasky src_ipv4_src_ipv6.ipv6_layout.ipv6), 1793331769Shselasky &ib_spec->ipv6.val.src_ip, 1794331769Shselasky sizeof(ib_spec->ipv6.val.src_ip)); 1795331769Shselasky memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_c, 1796331769Shselasky dst_ipv4_dst_ipv6.ipv6_layout.ipv6), 1797331769Shselasky &ib_spec->ipv6.mask.dst_ip, 1798331769Shselasky sizeof(ib_spec->ipv6.mask.dst_ip)); 1799331769Shselasky memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_v, 1800331769Shselasky dst_ipv4_dst_ipv6.ipv6_layout.ipv6), 1801331769Shselasky &ib_spec->ipv6.val.dst_ip, 1802331769Shselasky sizeof(ib_spec->ipv6.val.dst_ip)); 1803331769Shselasky 1804331769Shselasky set_tos(outer_headers_c, outer_headers_v, 1805331769Shselasky ib_spec->ipv6.mask.traffic_class, 1806331769Shselasky ib_spec->ipv6.val.traffic_class); 1807331769Shselasky 1808331769Shselasky set_proto(outer_headers_c, outer_headers_v, 1809331769Shselasky ib_spec->ipv6.mask.next_hdr, 1810331769Shselasky ib_spec->ipv6.val.next_hdr); 1811331769Shselasky 1812331769Shselasky MLX5_SET(fte_match_set_misc, misc_params_c, 1813331769Shselasky outer_ipv6_flow_label, 1814331769Shselasky ntohl(ib_spec->ipv6.mask.flow_label)); 1815331769Shselasky MLX5_SET(fte_match_set_misc, misc_params_v, 1816331769Shselasky outer_ipv6_flow_label, 1817331769Shselasky ntohl(ib_spec->ipv6.val.flow_label)); 1818331769Shselasky break; 1819331769Shselasky case IB_FLOW_SPEC_TCP: 1820331769Shselasky if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask, 1821331769Shselasky LAST_TCP_UDP_FIELD)) 1822331769Shselasky return -ENOTSUPP; 1823331769Shselasky 1824331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, ip_protocol, 1825331769Shselasky 0xff); 1826331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, ip_protocol, 1827331769Shselasky IPPROTO_TCP); 1828331769Shselasky 1829331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, tcp_sport, 1830331769Shselasky ntohs(ib_spec->tcp_udp.mask.src_port)); 1831331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, tcp_sport, 1832331769Shselasky ntohs(ib_spec->tcp_udp.val.src_port)); 1833331769Shselasky 1834331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, tcp_dport, 1835331769Shselasky ntohs(ib_spec->tcp_udp.mask.dst_port)); 1836331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, tcp_dport, 1837331769Shselasky ntohs(ib_spec->tcp_udp.val.dst_port)); 1838331769Shselasky break; 1839331769Shselasky case IB_FLOW_SPEC_UDP: 1840331769Shselasky if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask, 1841331769Shselasky LAST_TCP_UDP_FIELD)) 1842331769Shselasky return -ENOTSUPP; 1843331769Shselasky 1844331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, ip_protocol, 1845331769Shselasky 0xff); 1846331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, ip_protocol, 1847331769Shselasky IPPROTO_UDP); 1848331769Shselasky 1849331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, udp_sport, 1850331769Shselasky ntohs(ib_spec->tcp_udp.mask.src_port)); 1851331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, udp_sport, 1852331769Shselasky ntohs(ib_spec->tcp_udp.val.src_port)); 1853331769Shselasky 1854331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, udp_dport, 1855331769Shselasky ntohs(ib_spec->tcp_udp.mask.dst_port)); 1856331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, udp_dport, 1857331769Shselasky ntohs(ib_spec->tcp_udp.val.dst_port)); 1858331769Shselasky break; 1859331769Shselasky default: 1860331769Shselasky return -EINVAL; 1861331769Shselasky } 1862331769Shselasky 1863331769Shselasky return 0; 1864331769Shselasky} 1865331769Shselasky 1866331769Shselasky/* If a flow could catch both multicast and unicast packets, 1867331769Shselasky * it won't fall into the multicast flow steering table and this rule 1868331769Shselasky * could steal other multicast packets. 1869331769Shselasky */ 1870331769Shselaskystatic bool flow_is_multicast_only(struct ib_flow_attr *ib_attr) 1871331769Shselasky{ 1872331769Shselasky struct ib_flow_spec_eth *eth_spec; 1873331769Shselasky 1874331769Shselasky if (ib_attr->type != IB_FLOW_ATTR_NORMAL || 1875331769Shselasky ib_attr->size < sizeof(struct ib_flow_attr) + 1876331769Shselasky sizeof(struct ib_flow_spec_eth) || 1877331769Shselasky ib_attr->num_of_specs < 1) 1878331769Shselasky return false; 1879331769Shselasky 1880331769Shselasky eth_spec = (struct ib_flow_spec_eth *)(ib_attr + 1); 1881331769Shselasky if (eth_spec->type != IB_FLOW_SPEC_ETH || 1882331769Shselasky eth_spec->size != sizeof(*eth_spec)) 1883331769Shselasky return false; 1884331769Shselasky 1885331769Shselasky return is_multicast_ether_addr(eth_spec->mask.dst_mac) && 1886331769Shselasky is_multicast_ether_addr(eth_spec->val.dst_mac); 1887331769Shselasky} 1888331769Shselasky 1889331769Shselaskystatic bool is_valid_attr(const struct ib_flow_attr *flow_attr) 1890331769Shselasky{ 1891331769Shselasky union ib_flow_spec *ib_spec = (union ib_flow_spec *)(flow_attr + 1); 1892331769Shselasky bool has_ipv4_spec = false; 1893331769Shselasky bool eth_type_ipv4 = true; 1894331769Shselasky unsigned int spec_index; 1895331769Shselasky 1896331769Shselasky /* Validate that ethertype is correct */ 1897331769Shselasky for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) { 1898331769Shselasky if (ib_spec->type == IB_FLOW_SPEC_ETH && 1899331769Shselasky ib_spec->eth.mask.ether_type) { 1900331769Shselasky if (!((ib_spec->eth.mask.ether_type == htons(0xffff)) && 1901331769Shselasky ib_spec->eth.val.ether_type == htons(ETH_P_IP))) 1902331769Shselasky eth_type_ipv4 = false; 1903331769Shselasky } else if (ib_spec->type == IB_FLOW_SPEC_IPV4) { 1904331769Shselasky has_ipv4_spec = true; 1905331769Shselasky } 1906331769Shselasky ib_spec = (void *)ib_spec + ib_spec->size; 1907331769Shselasky } 1908331769Shselasky return !has_ipv4_spec || eth_type_ipv4; 1909331769Shselasky} 1910331769Shselasky 1911331769Shselaskystatic void put_flow_table(struct mlx5_ib_dev *dev, 1912331769Shselasky struct mlx5_ib_flow_prio *prio, bool ft_added) 1913331769Shselasky{ 1914331769Shselasky prio->refcount -= !!ft_added; 1915331769Shselasky if (!prio->refcount) { 1916331769Shselasky mlx5_destroy_flow_table(prio->flow_table); 1917331769Shselasky prio->flow_table = NULL; 1918331769Shselasky } 1919331769Shselasky} 1920331769Shselasky 1921331769Shselaskystatic int mlx5_ib_destroy_flow(struct ib_flow *flow_id) 1922331769Shselasky{ 1923331769Shselasky struct mlx5_ib_dev *dev = to_mdev(flow_id->qp->device); 1924331769Shselasky struct mlx5_ib_flow_handler *handler = container_of(flow_id, 1925331769Shselasky struct mlx5_ib_flow_handler, 1926331769Shselasky ibflow); 1927331769Shselasky struct mlx5_ib_flow_handler *iter, *tmp; 1928331769Shselasky 1929331769Shselasky mutex_lock(&dev->flow_db.lock); 1930331769Shselasky 1931331769Shselasky list_for_each_entry_safe(iter, tmp, &handler->list, list) { 1932331769Shselasky mlx5_del_flow_rule(iter->rule); 1933331769Shselasky put_flow_table(dev, iter->prio, true); 1934331769Shselasky list_del(&iter->list); 1935331769Shselasky kfree(iter); 1936331769Shselasky } 1937331769Shselasky 1938331769Shselasky mlx5_del_flow_rule(handler->rule); 1939331769Shselasky put_flow_table(dev, handler->prio, true); 1940331769Shselasky mutex_unlock(&dev->flow_db.lock); 1941331769Shselasky 1942331769Shselasky kfree(handler); 1943331769Shselasky 1944331769Shselasky return 0; 1945331769Shselasky} 1946331769Shselasky 1947331769Shselaskystatic int ib_prio_to_core_prio(unsigned int priority, bool dont_trap) 1948331769Shselasky{ 1949331769Shselasky priority *= 2; 1950331769Shselasky if (!dont_trap) 1951331769Shselasky priority++; 1952331769Shselasky return priority; 1953331769Shselasky} 1954331769Shselasky 1955331769Shselaskyenum flow_table_type { 1956331769Shselasky MLX5_IB_FT_RX, 1957331769Shselasky MLX5_IB_FT_TX 1958331769Shselasky}; 1959331769Shselasky 1960331769Shselasky#define MLX5_FS_MAX_TYPES 10 1961331769Shselasky#define MLX5_FS_MAX_ENTRIES 32000UL 1962331769Shselaskystatic struct mlx5_ib_flow_prio *get_flow_table(struct mlx5_ib_dev *dev, 1963331769Shselasky struct ib_flow_attr *flow_attr, 1964331769Shselasky enum flow_table_type ft_type) 1965331769Shselasky{ 1966331769Shselasky bool dont_trap = flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP; 1967331769Shselasky struct mlx5_flow_namespace *ns = NULL; 1968331769Shselasky struct mlx5_ib_flow_prio *prio; 1969331769Shselasky struct mlx5_flow_table *ft; 1970331769Shselasky int num_entries; 1971331769Shselasky int num_groups; 1972331769Shselasky int priority; 1973331769Shselasky int err = 0; 1974331769Shselasky 1975331769Shselasky if (flow_attr->type == IB_FLOW_ATTR_NORMAL) { 1976331769Shselasky if (flow_is_multicast_only(flow_attr) && 1977331769Shselasky !dont_trap) 1978331769Shselasky priority = MLX5_IB_FLOW_MCAST_PRIO; 1979331769Shselasky else 1980331769Shselasky priority = ib_prio_to_core_prio(flow_attr->priority, 1981331769Shselasky dont_trap); 1982331769Shselasky ns = mlx5_get_flow_namespace(dev->mdev, 1983331769Shselasky MLX5_FLOW_NAMESPACE_BYPASS); 1984331769Shselasky num_entries = MLX5_FS_MAX_ENTRIES; 1985331769Shselasky num_groups = MLX5_FS_MAX_TYPES; 1986331769Shselasky prio = &dev->flow_db.prios[priority]; 1987331769Shselasky } else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT || 1988331769Shselasky flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) { 1989331769Shselasky ns = mlx5_get_flow_namespace(dev->mdev, 1990331769Shselasky MLX5_FLOW_NAMESPACE_LEFTOVERS); 1991331769Shselasky build_leftovers_ft_param("bypass", &priority, 1992331769Shselasky &num_entries, 1993331769Shselasky &num_groups); 1994331769Shselasky prio = &dev->flow_db.prios[MLX5_IB_FLOW_LEFTOVERS_PRIO]; 1995331769Shselasky } else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) { 1996331769Shselasky if (!MLX5_CAP_FLOWTABLE(dev->mdev, 1997331769Shselasky allow_sniffer_and_nic_rx_shared_tir)) 1998331769Shselasky return ERR_PTR(-ENOTSUPP); 1999331769Shselasky 2000331769Shselasky ns = mlx5_get_flow_namespace(dev->mdev, ft_type == MLX5_IB_FT_RX ? 2001331769Shselasky MLX5_FLOW_NAMESPACE_SNIFFER_RX : 2002331769Shselasky MLX5_FLOW_NAMESPACE_SNIFFER_TX); 2003331769Shselasky 2004331769Shselasky prio = &dev->flow_db.sniffer[ft_type]; 2005331769Shselasky priority = 0; 2006331769Shselasky num_entries = 1; 2007331769Shselasky num_groups = 1; 2008331769Shselasky } 2009331769Shselasky 2010331769Shselasky if (!ns) 2011331769Shselasky return ERR_PTR(-ENOTSUPP); 2012331769Shselasky 2013331769Shselasky ft = prio->flow_table; 2014331769Shselasky if (!ft) { 2015331769Shselasky ft = mlx5_create_auto_grouped_flow_table(ns, priority, "bypass", 2016331769Shselasky num_entries, 2017331769Shselasky num_groups); 2018331769Shselasky 2019331769Shselasky if (!IS_ERR(ft)) { 2020331769Shselasky prio->refcount = 0; 2021331769Shselasky prio->flow_table = ft; 2022331769Shselasky } else { 2023331769Shselasky err = PTR_ERR(ft); 2024331769Shselasky } 2025331769Shselasky } 2026331769Shselasky 2027331769Shselasky return err ? ERR_PTR(err) : prio; 2028331769Shselasky} 2029331769Shselasky 2030331769Shselaskystatic struct mlx5_ib_flow_handler *create_flow_rule(struct mlx5_ib_dev *dev, 2031331769Shselasky struct mlx5_ib_flow_prio *ft_prio, 2032331769Shselasky const struct ib_flow_attr *flow_attr, 2033331769Shselasky struct mlx5_flow_destination *dst) 2034331769Shselasky{ 2035331769Shselasky struct mlx5_flow_table *ft = ft_prio->flow_table; 2036331769Shselasky struct mlx5_ib_flow_handler *handler; 2037331769Shselasky struct mlx5_flow_spec *spec; 2038331769Shselasky const void *ib_flow = (const void *)flow_attr + sizeof(*flow_attr); 2039331769Shselasky unsigned int spec_index; 2040331769Shselasky u32 action; 2041331769Shselasky int err = 0; 2042331769Shselasky 2043331769Shselasky if (!is_valid_attr(flow_attr)) 2044331769Shselasky return ERR_PTR(-EINVAL); 2045331769Shselasky 2046331769Shselasky spec = mlx5_vzalloc(sizeof(*spec)); 2047331769Shselasky handler = kzalloc(sizeof(*handler), GFP_KERNEL); 2048331769Shselasky if (!handler || !spec) { 2049331769Shselasky err = -ENOMEM; 2050331769Shselasky goto free; 2051331769Shselasky } 2052331769Shselasky 2053331769Shselasky INIT_LIST_HEAD(&handler->list); 2054331769Shselasky 2055331769Shselasky for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) { 2056331769Shselasky err = parse_flow_attr(spec->match_criteria, 2057331769Shselasky spec->match_value, ib_flow); 2058331769Shselasky if (err < 0) 2059331769Shselasky goto free; 2060331769Shselasky 2061331769Shselasky ib_flow += ((union ib_flow_spec *)ib_flow)->size; 2062331769Shselasky } 2063331769Shselasky 2064331769Shselasky spec->match_criteria_enable = get_match_criteria_enable(spec->match_criteria); 2065331769Shselasky action = dst ? MLX5_FLOW_CONTEXT_ACTION_FWD_DEST : 2066331769Shselasky MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO; 2067331769Shselasky handler->rule = mlx5_add_flow_rule(ft, spec->match_criteria_enable, 2068331769Shselasky spec->match_criteria, 2069331769Shselasky spec->match_value, 2070331769Shselasky action, 2071331769Shselasky MLX5_FS_DEFAULT_FLOW_TAG, 2072331769Shselasky dst); 2073331769Shselasky 2074331769Shselasky if (IS_ERR(handler->rule)) { 2075331769Shselasky err = PTR_ERR(handler->rule); 2076331769Shselasky goto free; 2077331769Shselasky } 2078331769Shselasky 2079331769Shselasky ft_prio->refcount++; 2080331769Shselasky handler->prio = ft_prio; 2081331769Shselasky 2082331769Shselasky ft_prio->flow_table = ft; 2083331769Shselaskyfree: 2084331769Shselasky if (err) 2085331769Shselasky kfree(handler); 2086331769Shselasky kvfree(spec); 2087331769Shselasky return err ? ERR_PTR(err) : handler; 2088331769Shselasky} 2089331769Shselasky 2090331769Shselaskystatic struct mlx5_ib_flow_handler *create_dont_trap_rule(struct mlx5_ib_dev *dev, 2091331769Shselasky struct mlx5_ib_flow_prio *ft_prio, 2092331769Shselasky struct ib_flow_attr *flow_attr, 2093331769Shselasky struct mlx5_flow_destination *dst) 2094331769Shselasky{ 2095331769Shselasky struct mlx5_ib_flow_handler *handler_dst = NULL; 2096331769Shselasky struct mlx5_ib_flow_handler *handler = NULL; 2097331769Shselasky 2098331769Shselasky handler = create_flow_rule(dev, ft_prio, flow_attr, NULL); 2099331769Shselasky if (!IS_ERR(handler)) { 2100331769Shselasky handler_dst = create_flow_rule(dev, ft_prio, 2101331769Shselasky flow_attr, dst); 2102331769Shselasky if (IS_ERR(handler_dst)) { 2103331769Shselasky mlx5_del_flow_rule(handler->rule); 2104331769Shselasky ft_prio->refcount--; 2105331769Shselasky kfree(handler); 2106331769Shselasky handler = handler_dst; 2107331769Shselasky } else { 2108331769Shselasky list_add(&handler_dst->list, &handler->list); 2109331769Shselasky } 2110331769Shselasky } 2111331769Shselasky 2112331769Shselasky return handler; 2113331769Shselasky} 2114331769Shselaskyenum { 2115331769Shselasky LEFTOVERS_MC, 2116331769Shselasky LEFTOVERS_UC, 2117331769Shselasky}; 2118331769Shselasky 2119331769Shselaskystatic struct mlx5_ib_flow_handler *create_leftovers_rule(struct mlx5_ib_dev *dev, 2120331769Shselasky struct mlx5_ib_flow_prio *ft_prio, 2121331769Shselasky struct ib_flow_attr *flow_attr, 2122331769Shselasky struct mlx5_flow_destination *dst) 2123331769Shselasky{ 2124331769Shselasky struct mlx5_ib_flow_handler *handler_ucast = NULL; 2125331769Shselasky struct mlx5_ib_flow_handler *handler = NULL; 2126331769Shselasky 2127331769Shselasky static struct { 2128331769Shselasky struct ib_flow_attr flow_attr; 2129331769Shselasky struct ib_flow_spec_eth eth_flow; 2130331769Shselasky } leftovers_specs[] = { 2131331769Shselasky [LEFTOVERS_MC] = { 2132331769Shselasky .flow_attr = { 2133331769Shselasky .num_of_specs = 1, 2134331769Shselasky .size = sizeof(leftovers_specs[0]) 2135331769Shselasky }, 2136331769Shselasky .eth_flow = { 2137331769Shselasky .type = IB_FLOW_SPEC_ETH, 2138331769Shselasky .size = sizeof(struct ib_flow_spec_eth), 2139331769Shselasky .mask = {.dst_mac = {0x1} }, 2140331769Shselasky .val = {.dst_mac = {0x1} } 2141331769Shselasky } 2142331769Shselasky }, 2143331769Shselasky [LEFTOVERS_UC] = { 2144331769Shselasky .flow_attr = { 2145331769Shselasky .num_of_specs = 1, 2146331769Shselasky .size = sizeof(leftovers_specs[0]) 2147331769Shselasky }, 2148331769Shselasky .eth_flow = { 2149331769Shselasky .type = IB_FLOW_SPEC_ETH, 2150331769Shselasky .size = sizeof(struct ib_flow_spec_eth), 2151331769Shselasky .mask = {.dst_mac = {0x1} }, 2152331769Shselasky .val = {.dst_mac = {} } 2153331769Shselasky } 2154331769Shselasky } 2155331769Shselasky }; 2156331769Shselasky 2157331769Shselasky handler = create_flow_rule(dev, ft_prio, 2158331769Shselasky &leftovers_specs[LEFTOVERS_MC].flow_attr, 2159331769Shselasky dst); 2160331769Shselasky if (!IS_ERR(handler) && 2161331769Shselasky flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT) { 2162331769Shselasky handler_ucast = create_flow_rule(dev, ft_prio, 2163331769Shselasky &leftovers_specs[LEFTOVERS_UC].flow_attr, 2164331769Shselasky dst); 2165331769Shselasky if (IS_ERR(handler_ucast)) { 2166331769Shselasky mlx5_del_flow_rule(handler->rule); 2167331769Shselasky ft_prio->refcount--; 2168331769Shselasky kfree(handler); 2169331769Shselasky handler = handler_ucast; 2170331769Shselasky } else { 2171331769Shselasky list_add(&handler_ucast->list, &handler->list); 2172331769Shselasky } 2173331769Shselasky } 2174331769Shselasky 2175331769Shselasky return handler; 2176331769Shselasky} 2177331769Shselasky 2178331769Shselaskystatic struct mlx5_ib_flow_handler *create_sniffer_rule(struct mlx5_ib_dev *dev, 2179331769Shselasky struct mlx5_ib_flow_prio *ft_rx, 2180331769Shselasky struct mlx5_ib_flow_prio *ft_tx, 2181331769Shselasky struct mlx5_flow_destination *dst) 2182331769Shselasky{ 2183331769Shselasky struct mlx5_ib_flow_handler *handler_rx; 2184331769Shselasky struct mlx5_ib_flow_handler *handler_tx; 2185331769Shselasky int err; 2186331769Shselasky static const struct ib_flow_attr flow_attr = { 2187331769Shselasky .num_of_specs = 0, 2188331769Shselasky .size = sizeof(flow_attr) 2189331769Shselasky }; 2190331769Shselasky 2191331769Shselasky handler_rx = create_flow_rule(dev, ft_rx, &flow_attr, dst); 2192331769Shselasky if (IS_ERR(handler_rx)) { 2193331769Shselasky err = PTR_ERR(handler_rx); 2194331769Shselasky goto err; 2195331769Shselasky } 2196331769Shselasky 2197331769Shselasky handler_tx = create_flow_rule(dev, ft_tx, &flow_attr, dst); 2198331769Shselasky if (IS_ERR(handler_tx)) { 2199331769Shselasky err = PTR_ERR(handler_tx); 2200331769Shselasky goto err_tx; 2201331769Shselasky } 2202331769Shselasky 2203331769Shselasky list_add(&handler_tx->list, &handler_rx->list); 2204331769Shselasky 2205331769Shselasky return handler_rx; 2206331769Shselasky 2207331769Shselaskyerr_tx: 2208331769Shselasky mlx5_del_flow_rule(handler_rx->rule); 2209331769Shselasky ft_rx->refcount--; 2210331769Shselasky kfree(handler_rx); 2211331769Shselaskyerr: 2212331769Shselasky return ERR_PTR(err); 2213331769Shselasky} 2214331769Shselasky 2215331769Shselaskystatic struct ib_flow *mlx5_ib_create_flow(struct ib_qp *qp, 2216331769Shselasky struct ib_flow_attr *flow_attr, 2217331769Shselasky int domain) 2218331769Shselasky{ 2219331769Shselasky struct mlx5_ib_dev *dev = to_mdev(qp->device); 2220331769Shselasky struct mlx5_ib_qp *mqp = to_mqp(qp); 2221331769Shselasky struct mlx5_ib_flow_handler *handler = NULL; 2222331769Shselasky struct mlx5_flow_destination *dst = NULL; 2223331769Shselasky struct mlx5_ib_flow_prio *ft_prio_tx = NULL; 2224331769Shselasky struct mlx5_ib_flow_prio *ft_prio; 2225331769Shselasky int err; 2226331769Shselasky 2227331769Shselasky if (flow_attr->priority > MLX5_IB_FLOW_LAST_PRIO) 2228331769Shselasky return ERR_PTR(-ENOSPC); 2229331769Shselasky 2230331769Shselasky if (domain != IB_FLOW_DOMAIN_USER || 2231331769Shselasky flow_attr->port > MLX5_CAP_GEN(dev->mdev, num_ports) || 2232331769Shselasky (flow_attr->flags & ~IB_FLOW_ATTR_FLAGS_DONT_TRAP)) 2233331769Shselasky return ERR_PTR(-EINVAL); 2234331769Shselasky 2235331769Shselasky dst = kzalloc(sizeof(*dst), GFP_KERNEL); 2236331769Shselasky if (!dst) 2237331769Shselasky return ERR_PTR(-ENOMEM); 2238331769Shselasky 2239331769Shselasky mutex_lock(&dev->flow_db.lock); 2240331769Shselasky 2241331769Shselasky ft_prio = get_flow_table(dev, flow_attr, MLX5_IB_FT_RX); 2242331769Shselasky if (IS_ERR(ft_prio)) { 2243331769Shselasky err = PTR_ERR(ft_prio); 2244331769Shselasky goto unlock; 2245331769Shselasky } 2246331769Shselasky if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) { 2247331769Shselasky ft_prio_tx = get_flow_table(dev, flow_attr, MLX5_IB_FT_TX); 2248331769Shselasky if (IS_ERR(ft_prio_tx)) { 2249331769Shselasky err = PTR_ERR(ft_prio_tx); 2250331769Shselasky ft_prio_tx = NULL; 2251331769Shselasky goto destroy_ft; 2252331769Shselasky } 2253331769Shselasky } 2254331769Shselasky 2255331769Shselasky dst->type = MLX5_FLOW_DESTINATION_TYPE_TIR; 2256331769Shselasky if (mqp->flags & MLX5_IB_QP_RSS) 2257331769Shselasky dst->tir_num = mqp->rss_qp.tirn; 2258331769Shselasky else 2259331769Shselasky dst->tir_num = mqp->raw_packet_qp.rq.tirn; 2260331769Shselasky 2261331769Shselasky if (flow_attr->type == IB_FLOW_ATTR_NORMAL) { 2262331769Shselasky if (flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP) { 2263331769Shselasky handler = create_dont_trap_rule(dev, ft_prio, 2264331769Shselasky flow_attr, dst); 2265331769Shselasky } else { 2266331769Shselasky handler = create_flow_rule(dev, ft_prio, flow_attr, 2267331769Shselasky dst); 2268331769Shselasky } 2269331769Shselasky } else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT || 2270331769Shselasky flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) { 2271331769Shselasky handler = create_leftovers_rule(dev, ft_prio, flow_attr, 2272331769Shselasky dst); 2273331769Shselasky } else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) { 2274331769Shselasky handler = create_sniffer_rule(dev, ft_prio, ft_prio_tx, dst); 2275331769Shselasky } else { 2276331769Shselasky err = -EINVAL; 2277331769Shselasky goto destroy_ft; 2278331769Shselasky } 2279331769Shselasky 2280331769Shselasky if (IS_ERR(handler)) { 2281331769Shselasky err = PTR_ERR(handler); 2282331769Shselasky handler = NULL; 2283331769Shselasky goto destroy_ft; 2284331769Shselasky } 2285331769Shselasky 2286331769Shselasky mutex_unlock(&dev->flow_db.lock); 2287331769Shselasky kfree(dst); 2288331769Shselasky 2289331769Shselasky return &handler->ibflow; 2290331769Shselasky 2291331769Shselaskydestroy_ft: 2292331769Shselasky put_flow_table(dev, ft_prio, false); 2293331769Shselasky if (ft_prio_tx) 2294331769Shselasky put_flow_table(dev, ft_prio_tx, false); 2295331769Shselaskyunlock: 2296331769Shselasky mutex_unlock(&dev->flow_db.lock); 2297331769Shselasky kfree(dst); 2298331769Shselasky kfree(handler); 2299331769Shselasky return ERR_PTR(err); 2300331769Shselasky} 2301331769Shselasky 2302322810Shselaskystatic int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid) 2303322810Shselasky{ 2304322810Shselasky struct mlx5_ib_dev *dev = to_mdev(ibqp->device); 2305322810Shselasky int err; 2306322810Shselasky 2307331769Shselasky err = mlx5_core_attach_mcg(dev->mdev, gid, ibqp->qp_num); 2308322810Shselasky if (err) 2309322810Shselasky mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n", 2310322810Shselasky ibqp->qp_num, gid->raw); 2311322810Shselasky 2312322810Shselasky return err; 2313322810Shselasky} 2314322810Shselasky 2315322810Shselaskystatic int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid) 2316322810Shselasky{ 2317322810Shselasky struct mlx5_ib_dev *dev = to_mdev(ibqp->device); 2318322810Shselasky int err; 2319322810Shselasky 2320331769Shselasky err = mlx5_core_detach_mcg(dev->mdev, gid, ibqp->qp_num); 2321322810Shselasky if (err) 2322322810Shselasky mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n", 2323322810Shselasky ibqp->qp_num, gid->raw); 2324322810Shselasky 2325322810Shselasky return err; 2326322810Shselasky} 2327322810Shselasky 2328322810Shselaskystatic int init_node_data(struct mlx5_ib_dev *dev) 2329322810Shselasky{ 2330322810Shselasky int err; 2331322810Shselasky 2332322810Shselasky err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc); 2333322810Shselasky if (err) 2334322810Shselasky return err; 2335322810Shselasky 2336322810Shselasky return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid); 2337322810Shselasky} 2338322810Shselasky 2339322810Shselaskystatic ssize_t show_fw_pages(struct device *device, struct device_attribute *attr, 2340322810Shselasky char *buf) 2341322810Shselasky{ 2342322810Shselasky struct mlx5_ib_dev *dev = 2343322810Shselasky container_of(device, struct mlx5_ib_dev, ib_dev.dev); 2344322810Shselasky 2345322810Shselasky return sprintf(buf, "%lld\n", (long long)dev->mdev->priv.fw_pages); 2346322810Shselasky} 2347322810Shselasky 2348322810Shselaskystatic ssize_t show_reg_pages(struct device *device, 2349322810Shselasky struct device_attribute *attr, char *buf) 2350322810Shselasky{ 2351322810Shselasky struct mlx5_ib_dev *dev = 2352322810Shselasky container_of(device, struct mlx5_ib_dev, ib_dev.dev); 2353322810Shselasky 2354322810Shselasky return sprintf(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages)); 2355322810Shselasky} 2356322810Shselasky 2357322810Shselaskystatic ssize_t show_hca(struct device *device, struct device_attribute *attr, 2358322810Shselasky char *buf) 2359322810Shselasky{ 2360322810Shselasky struct mlx5_ib_dev *dev = 2361322810Shselasky container_of(device, struct mlx5_ib_dev, ib_dev.dev); 2362322810Shselasky return sprintf(buf, "MT%d\n", dev->mdev->pdev->device); 2363322810Shselasky} 2364322810Shselasky 2365322810Shselaskystatic ssize_t show_rev(struct device *device, struct device_attribute *attr, 2366322810Shselasky char *buf) 2367322810Shselasky{ 2368322810Shselasky struct mlx5_ib_dev *dev = 2369322810Shselasky container_of(device, struct mlx5_ib_dev, ib_dev.dev); 2370331769Shselasky return sprintf(buf, "%x\n", dev->mdev->pdev->revision); 2371322810Shselasky} 2372322810Shselasky 2373322810Shselaskystatic ssize_t show_board(struct device *device, struct device_attribute *attr, 2374322810Shselasky char *buf) 2375322810Shselasky{ 2376322810Shselasky struct mlx5_ib_dev *dev = 2377322810Shselasky container_of(device, struct mlx5_ib_dev, ib_dev.dev); 2378322810Shselasky return sprintf(buf, "%.*s\n", MLX5_BOARD_ID_LEN, 2379322810Shselasky dev->mdev->board_id); 2380322810Shselasky} 2381322810Shselasky 2382322810Shselaskystatic DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL); 2383322810Shselaskystatic DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL); 2384322810Shselaskystatic DEVICE_ATTR(board_id, S_IRUGO, show_board, NULL); 2385322810Shselaskystatic DEVICE_ATTR(fw_pages, S_IRUGO, show_fw_pages, NULL); 2386322810Shselaskystatic DEVICE_ATTR(reg_pages, S_IRUGO, show_reg_pages, NULL); 2387322810Shselasky 2388322810Shselaskystatic struct device_attribute *mlx5_class_attributes[] = { 2389322810Shselasky &dev_attr_hw_rev, 2390322810Shselasky &dev_attr_hca_type, 2391322810Shselasky &dev_attr_board_id, 2392322810Shselasky &dev_attr_fw_pages, 2393322810Shselasky &dev_attr_reg_pages, 2394322810Shselasky}; 2395322810Shselasky 2396331769Shselaskystatic void pkey_change_handler(struct work_struct *work) 2397331769Shselasky{ 2398331769Shselasky struct mlx5_ib_port_resources *ports = 2399331769Shselasky container_of(work, struct mlx5_ib_port_resources, 2400331769Shselasky pkey_change_work); 2401331769Shselasky 2402331769Shselasky mutex_lock(&ports->devr->mutex); 2403331769Shselasky mlx5_ib_gsi_pkey_change(ports->gsi); 2404331769Shselasky mutex_unlock(&ports->devr->mutex); 2405331769Shselasky} 2406331769Shselasky 2407322810Shselaskystatic void mlx5_ib_handle_internal_error(struct mlx5_ib_dev *ibdev) 2408322810Shselasky{ 2409322810Shselasky struct mlx5_ib_qp *mqp; 2410322810Shselasky struct mlx5_ib_cq *send_mcq, *recv_mcq; 2411322810Shselasky struct mlx5_core_cq *mcq; 2412322810Shselasky struct list_head cq_armed_list; 2413322810Shselasky unsigned long flags_qp; 2414322810Shselasky unsigned long flags_cq; 2415322810Shselasky unsigned long flags; 2416322810Shselasky 2417322810Shselasky INIT_LIST_HEAD(&cq_armed_list); 2418322810Shselasky 2419322810Shselasky /* Go over qp list reside on that ibdev, sync with create/destroy qp.*/ 2420322810Shselasky spin_lock_irqsave(&ibdev->reset_flow_resource_lock, flags); 2421322810Shselasky list_for_each_entry(mqp, &ibdev->qp_list, qps_list) { 2422322810Shselasky spin_lock_irqsave(&mqp->sq.lock, flags_qp); 2423322810Shselasky if (mqp->sq.tail != mqp->sq.head) { 2424322810Shselasky send_mcq = to_mcq(mqp->ibqp.send_cq); 2425322810Shselasky spin_lock_irqsave(&send_mcq->lock, flags_cq); 2426322810Shselasky if (send_mcq->mcq.comp && 2427322810Shselasky mqp->ibqp.send_cq->comp_handler) { 2428322810Shselasky if (!send_mcq->mcq.reset_notify_added) { 2429322810Shselasky send_mcq->mcq.reset_notify_added = 1; 2430322810Shselasky list_add_tail(&send_mcq->mcq.reset_notify, 2431322810Shselasky &cq_armed_list); 2432322810Shselasky } 2433322810Shselasky } 2434322810Shselasky spin_unlock_irqrestore(&send_mcq->lock, flags_cq); 2435322810Shselasky } 2436322810Shselasky spin_unlock_irqrestore(&mqp->sq.lock, flags_qp); 2437322810Shselasky spin_lock_irqsave(&mqp->rq.lock, flags_qp); 2438322810Shselasky /* no handling is needed for SRQ */ 2439322810Shselasky if (!mqp->ibqp.srq) { 2440322810Shselasky if (mqp->rq.tail != mqp->rq.head) { 2441322810Shselasky recv_mcq = to_mcq(mqp->ibqp.recv_cq); 2442322810Shselasky spin_lock_irqsave(&recv_mcq->lock, flags_cq); 2443322810Shselasky if (recv_mcq->mcq.comp && 2444322810Shselasky mqp->ibqp.recv_cq->comp_handler) { 2445322810Shselasky if (!recv_mcq->mcq.reset_notify_added) { 2446322810Shselasky recv_mcq->mcq.reset_notify_added = 1; 2447322810Shselasky list_add_tail(&recv_mcq->mcq.reset_notify, 2448322810Shselasky &cq_armed_list); 2449322810Shselasky } 2450322810Shselasky } 2451322810Shselasky spin_unlock_irqrestore(&recv_mcq->lock, 2452322810Shselasky flags_cq); 2453322810Shselasky } 2454322810Shselasky } 2455322810Shselasky spin_unlock_irqrestore(&mqp->rq.lock, flags_qp); 2456322810Shselasky } 2457322810Shselasky /*At that point all inflight post send were put to be executed as of we 2458322810Shselasky * lock/unlock above locks Now need to arm all involved CQs. 2459322810Shselasky */ 2460322810Shselasky list_for_each_entry(mcq, &cq_armed_list, reset_notify) { 2461322810Shselasky mcq->comp(mcq); 2462322810Shselasky } 2463322810Shselasky spin_unlock_irqrestore(&ibdev->reset_flow_resource_lock, flags); 2464322810Shselasky} 2465322810Shselasky 2466322810Shselaskystatic void mlx5_ib_event(struct mlx5_core_dev *dev, void *context, 2467322810Shselasky enum mlx5_dev_event event, unsigned long param) 2468322810Shselasky{ 2469322810Shselasky struct mlx5_ib_dev *ibdev = (struct mlx5_ib_dev *)context; 2470322810Shselasky struct ib_event ibev; 2471331769Shselasky bool fatal = false; 2472341922Shselasky u8 port = (u8)param; 2473322810Shselasky 2474322810Shselasky switch (event) { 2475322810Shselasky case MLX5_DEV_EVENT_SYS_ERROR: 2476322810Shselasky ibev.event = IB_EVENT_DEVICE_FATAL; 2477322810Shselasky mlx5_ib_handle_internal_error(ibdev); 2478331769Shselasky fatal = true; 2479322810Shselasky break; 2480322810Shselasky 2481322810Shselasky case MLX5_DEV_EVENT_PORT_UP: 2482322810Shselasky case MLX5_DEV_EVENT_PORT_DOWN: 2483322810Shselasky case MLX5_DEV_EVENT_PORT_INITIALIZED: 2484331769Shselasky /* In RoCE, port up/down events are handled in 2485331769Shselasky * mlx5_netdev_event(). 2486331769Shselasky */ 2487331769Shselasky if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) == 2488331769Shselasky IB_LINK_LAYER_ETHERNET) 2489331769Shselasky return; 2490331769Shselasky 2491331769Shselasky ibev.event = (event == MLX5_DEV_EVENT_PORT_UP) ? 2492331769Shselasky IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR; 2493322810Shselasky break; 2494322810Shselasky 2495322810Shselasky case MLX5_DEV_EVENT_LID_CHANGE: 2496322810Shselasky ibev.event = IB_EVENT_LID_CHANGE; 2497322810Shselasky break; 2498322810Shselasky 2499322810Shselasky case MLX5_DEV_EVENT_PKEY_CHANGE: 2500322810Shselasky ibev.event = IB_EVENT_PKEY_CHANGE; 2501331769Shselasky 2502331769Shselasky schedule_work(&ibdev->devr.ports[port - 1].pkey_change_work); 2503322810Shselasky break; 2504322810Shselasky 2505322810Shselasky case MLX5_DEV_EVENT_GUID_CHANGE: 2506322810Shselasky ibev.event = IB_EVENT_GID_CHANGE; 2507322810Shselasky break; 2508322810Shselasky 2509322810Shselasky case MLX5_DEV_EVENT_CLIENT_REREG: 2510322810Shselasky ibev.event = IB_EVENT_CLIENT_REREGISTER; 2511322810Shselasky break; 2512322810Shselasky 2513322810Shselasky default: 2514337100Shselasky /* unsupported event */ 2515337100Shselasky return; 2516322810Shselasky } 2517322810Shselasky 2518322810Shselasky ibev.device = &ibdev->ib_dev; 2519322810Shselasky ibev.element.port_num = port; 2520322810Shselasky 2521341922Shselasky if (!rdma_is_port_valid(&ibdev->ib_dev, port)) { 2522337100Shselasky mlx5_ib_warn(ibdev, "warning: event(%d) on port %d\n", event, port); 2523322810Shselasky return; 2524322810Shselasky } 2525322810Shselasky 2526322810Shselasky if (ibdev->ib_active) 2527322810Shselasky ib_dispatch_event(&ibev); 2528331769Shselasky 2529331769Shselasky if (fatal) 2530331769Shselasky ibdev->ib_active = false; 2531322810Shselasky} 2532322810Shselasky 2533322810Shselaskystatic void get_ext_port_caps(struct mlx5_ib_dev *dev) 2534322810Shselasky{ 2535322810Shselasky int port; 2536322810Shselasky 2537322810Shselasky for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++) 2538322810Shselasky mlx5_query_ext_port_caps(dev, port); 2539322810Shselasky} 2540322810Shselasky 2541322810Shselaskystatic int get_port_caps(struct mlx5_ib_dev *dev) 2542322810Shselasky{ 2543322810Shselasky struct ib_device_attr *dprops = NULL; 2544322810Shselasky struct ib_port_attr *pprops = NULL; 2545322810Shselasky int err = -ENOMEM; 2546322810Shselasky int port; 2547331769Shselasky struct ib_udata uhw = {.inlen = 0, .outlen = 0}; 2548322810Shselasky 2549322810Shselasky pprops = kmalloc(sizeof(*pprops), GFP_KERNEL); 2550322810Shselasky if (!pprops) 2551322810Shselasky goto out; 2552322810Shselasky 2553322810Shselasky dprops = kmalloc(sizeof(*dprops), GFP_KERNEL); 2554322810Shselasky if (!dprops) 2555322810Shselasky goto out; 2556322810Shselasky 2557331769Shselasky err = mlx5_ib_query_device(&dev->ib_dev, dprops, &uhw); 2558322810Shselasky if (err) { 2559322810Shselasky mlx5_ib_warn(dev, "query_device failed %d\n", err); 2560322810Shselasky goto out; 2561322810Shselasky } 2562322810Shselasky 2563322810Shselasky for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++) { 2564322810Shselasky err = mlx5_ib_query_port(&dev->ib_dev, port, pprops); 2565322810Shselasky if (err) { 2566322810Shselasky mlx5_ib_warn(dev, "query_port %d failed %d\n", 2567322810Shselasky port, err); 2568322810Shselasky break; 2569322810Shselasky } 2570331769Shselasky dev->mdev->port_caps[port - 1].pkey_table_len = 2571331769Shselasky dprops->max_pkeys; 2572331769Shselasky dev->mdev->port_caps[port - 1].gid_table_len = 2573331769Shselasky pprops->gid_tbl_len; 2574322810Shselasky mlx5_ib_dbg(dev, "pkey_table_len %d, gid_table_len %d\n", 2575322810Shselasky dprops->max_pkeys, pprops->gid_tbl_len); 2576322810Shselasky } 2577322810Shselasky 2578322810Shselaskyout: 2579322810Shselasky kfree(pprops); 2580322810Shselasky kfree(dprops); 2581322810Shselasky 2582322810Shselasky return err; 2583322810Shselasky} 2584322810Shselasky 2585322810Shselaskystatic void destroy_umrc_res(struct mlx5_ib_dev *dev) 2586322810Shselasky{ 2587322810Shselasky int err; 2588322810Shselasky 2589322810Shselasky err = mlx5_mr_cache_cleanup(dev); 2590322810Shselasky if (err) 2591322810Shselasky mlx5_ib_warn(dev, "mr cache cleanup failed\n"); 2592322810Shselasky 2593331769Shselasky mlx5_ib_destroy_qp(dev->umrc.qp); 2594331769Shselasky ib_free_cq(dev->umrc.cq); 2595322810Shselasky ib_dealloc_pd(dev->umrc.pd); 2596322810Shselasky} 2597322810Shselasky 2598322810Shselaskyenum { 2599322810Shselasky MAX_UMR_WR = 128, 2600322810Shselasky}; 2601322810Shselasky 2602322810Shselaskystatic int create_umr_res(struct mlx5_ib_dev *dev) 2603322810Shselasky{ 2604331769Shselasky struct ib_qp_init_attr *init_attr = NULL; 2605331769Shselasky struct ib_qp_attr *attr = NULL; 2606322810Shselasky struct ib_pd *pd; 2607331769Shselasky struct ib_cq *cq; 2608331769Shselasky struct ib_qp *qp; 2609322810Shselasky int ret; 2610322810Shselasky 2611331769Shselasky attr = kzalloc(sizeof(*attr), GFP_KERNEL); 2612331769Shselasky init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL); 2613331769Shselasky if (!attr || !init_attr) { 2614331769Shselasky ret = -ENOMEM; 2615331769Shselasky goto error_0; 2616331769Shselasky } 2617331769Shselasky 2618331769Shselasky pd = ib_alloc_pd(&dev->ib_dev, 0); 2619322810Shselasky if (IS_ERR(pd)) { 2620322810Shselasky mlx5_ib_dbg(dev, "Couldn't create PD for sync UMR QP\n"); 2621322810Shselasky ret = PTR_ERR(pd); 2622322810Shselasky goto error_0; 2623322810Shselasky } 2624322810Shselasky 2625331769Shselasky cq = ib_alloc_cq(&dev->ib_dev, NULL, 128, 0, IB_POLL_SOFTIRQ); 2626331769Shselasky if (IS_ERR(cq)) { 2627331769Shselasky mlx5_ib_dbg(dev, "Couldn't create CQ for sync UMR QP\n"); 2628331769Shselasky ret = PTR_ERR(cq); 2629331769Shselasky goto error_2; 2630322810Shselasky } 2631322810Shselasky 2632331769Shselasky init_attr->send_cq = cq; 2633331769Shselasky init_attr->recv_cq = cq; 2634331769Shselasky init_attr->sq_sig_type = IB_SIGNAL_ALL_WR; 2635331769Shselasky init_attr->cap.max_send_wr = MAX_UMR_WR; 2636331769Shselasky init_attr->cap.max_send_sge = 1; 2637331769Shselasky init_attr->qp_type = MLX5_IB_QPT_REG_UMR; 2638331769Shselasky init_attr->port_num = 1; 2639331769Shselasky qp = mlx5_ib_create_qp(pd, init_attr, NULL); 2640331769Shselasky if (IS_ERR(qp)) { 2641331769Shselasky mlx5_ib_dbg(dev, "Couldn't create sync UMR QP\n"); 2642331769Shselasky ret = PTR_ERR(qp); 2643331769Shselasky goto error_3; 2644331769Shselasky } 2645331769Shselasky qp->device = &dev->ib_dev; 2646331769Shselasky qp->real_qp = qp; 2647331769Shselasky qp->uobject = NULL; 2648331769Shselasky qp->qp_type = MLX5_IB_QPT_REG_UMR; 2649331769Shselasky 2650331769Shselasky attr->qp_state = IB_QPS_INIT; 2651331769Shselasky attr->port_num = 1; 2652331769Shselasky ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_PKEY_INDEX | 2653331769Shselasky IB_QP_PORT, NULL); 2654331769Shselasky if (ret) { 2655331769Shselasky mlx5_ib_dbg(dev, "Couldn't modify UMR QP\n"); 2656331769Shselasky goto error_4; 2657331769Shselasky } 2658331769Shselasky 2659331769Shselasky memset(attr, 0, sizeof(*attr)); 2660331769Shselasky attr->qp_state = IB_QPS_RTR; 2661331769Shselasky attr->path_mtu = IB_MTU_256; 2662331769Shselasky 2663331769Shselasky ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL); 2664331769Shselasky if (ret) { 2665331769Shselasky mlx5_ib_dbg(dev, "Couldn't modify umr QP to rtr\n"); 2666331769Shselasky goto error_4; 2667331769Shselasky } 2668331769Shselasky 2669331769Shselasky memset(attr, 0, sizeof(*attr)); 2670331769Shselasky attr->qp_state = IB_QPS_RTS; 2671331769Shselasky ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL); 2672331769Shselasky if (ret) { 2673331769Shselasky mlx5_ib_dbg(dev, "Couldn't modify umr QP to rts\n"); 2674331769Shselasky goto error_4; 2675331769Shselasky } 2676331769Shselasky 2677331769Shselasky dev->umrc.qp = qp; 2678331769Shselasky dev->umrc.cq = cq; 2679322810Shselasky dev->umrc.pd = pd; 2680322810Shselasky 2681331769Shselasky sema_init(&dev->umrc.sem, MAX_UMR_WR); 2682322810Shselasky ret = mlx5_mr_cache_init(dev); 2683322810Shselasky if (ret) { 2684322810Shselasky mlx5_ib_warn(dev, "mr cache init failed %d\n", ret); 2685322810Shselasky goto error_4; 2686322810Shselasky } 2687322810Shselasky 2688331769Shselasky kfree(attr); 2689331769Shselasky kfree(init_attr); 2690331769Shselasky 2691322810Shselasky return 0; 2692322810Shselasky 2693322810Shselaskyerror_4: 2694331769Shselasky mlx5_ib_destroy_qp(qp); 2695331769Shselasky 2696331769Shselaskyerror_3: 2697331769Shselasky ib_free_cq(cq); 2698331769Shselasky 2699331769Shselaskyerror_2: 2700322810Shselasky ib_dealloc_pd(pd); 2701331769Shselasky 2702322810Shselaskyerror_0: 2703331769Shselasky kfree(attr); 2704331769Shselasky kfree(init_attr); 2705322810Shselasky return ret; 2706322810Shselasky} 2707322810Shselasky 2708322810Shselaskystatic int create_dev_resources(struct mlx5_ib_resources *devr) 2709322810Shselasky{ 2710322810Shselasky struct ib_srq_init_attr attr; 2711322810Shselasky struct mlx5_ib_dev *dev; 2712331769Shselasky struct ib_cq_init_attr cq_attr = {.cqe = 1}; 2713331769Shselasky int port; 2714322810Shselasky int ret = 0; 2715322810Shselasky 2716322810Shselasky dev = container_of(devr, struct mlx5_ib_dev, devr); 2717322810Shselasky 2718331769Shselasky mutex_init(&devr->mutex); 2719331769Shselasky 2720322810Shselasky devr->p0 = mlx5_ib_alloc_pd(&dev->ib_dev, NULL, NULL); 2721322810Shselasky if (IS_ERR(devr->p0)) { 2722322810Shselasky ret = PTR_ERR(devr->p0); 2723322810Shselasky goto error0; 2724322810Shselasky } 2725322810Shselasky devr->p0->device = &dev->ib_dev; 2726322810Shselasky devr->p0->uobject = NULL; 2727322810Shselasky atomic_set(&devr->p0->usecnt, 0); 2728322810Shselasky 2729322810Shselasky devr->c0 = mlx5_ib_create_cq(&dev->ib_dev, &cq_attr, NULL, NULL); 2730322810Shselasky if (IS_ERR(devr->c0)) { 2731322810Shselasky ret = PTR_ERR(devr->c0); 2732322810Shselasky goto error1; 2733322810Shselasky } 2734322810Shselasky devr->c0->device = &dev->ib_dev; 2735322810Shselasky devr->c0->uobject = NULL; 2736322810Shselasky devr->c0->comp_handler = NULL; 2737322810Shselasky devr->c0->event_handler = NULL; 2738322810Shselasky devr->c0->cq_context = NULL; 2739322810Shselasky atomic_set(&devr->c0->usecnt, 0); 2740322810Shselasky 2741322810Shselasky devr->x0 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL); 2742322810Shselasky if (IS_ERR(devr->x0)) { 2743322810Shselasky ret = PTR_ERR(devr->x0); 2744322810Shselasky goto error2; 2745322810Shselasky } 2746322810Shselasky devr->x0->device = &dev->ib_dev; 2747322810Shselasky devr->x0->inode = NULL; 2748322810Shselasky atomic_set(&devr->x0->usecnt, 0); 2749322810Shselasky mutex_init(&devr->x0->tgt_qp_mutex); 2750322810Shselasky INIT_LIST_HEAD(&devr->x0->tgt_qp_list); 2751322810Shselasky 2752322810Shselasky devr->x1 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL); 2753322810Shselasky if (IS_ERR(devr->x1)) { 2754322810Shselasky ret = PTR_ERR(devr->x1); 2755322810Shselasky goto error3; 2756322810Shselasky } 2757322810Shselasky devr->x1->device = &dev->ib_dev; 2758322810Shselasky devr->x1->inode = NULL; 2759322810Shselasky atomic_set(&devr->x1->usecnt, 0); 2760322810Shselasky mutex_init(&devr->x1->tgt_qp_mutex); 2761322810Shselasky INIT_LIST_HEAD(&devr->x1->tgt_qp_list); 2762322810Shselasky 2763322810Shselasky memset(&attr, 0, sizeof(attr)); 2764322810Shselasky attr.attr.max_sge = 1; 2765322810Shselasky attr.attr.max_wr = 1; 2766322810Shselasky attr.srq_type = IB_SRQT_XRC; 2767322810Shselasky attr.ext.xrc.cq = devr->c0; 2768322810Shselasky attr.ext.xrc.xrcd = devr->x0; 2769322810Shselasky 2770322810Shselasky devr->s0 = mlx5_ib_create_srq(devr->p0, &attr, NULL); 2771322810Shselasky if (IS_ERR(devr->s0)) { 2772322810Shselasky ret = PTR_ERR(devr->s0); 2773322810Shselasky goto error4; 2774322810Shselasky } 2775322810Shselasky devr->s0->device = &dev->ib_dev; 2776322810Shselasky devr->s0->pd = devr->p0; 2777322810Shselasky devr->s0->uobject = NULL; 2778322810Shselasky devr->s0->event_handler = NULL; 2779322810Shselasky devr->s0->srq_context = NULL; 2780322810Shselasky devr->s0->srq_type = IB_SRQT_XRC; 2781331769Shselasky devr->s0->ext.xrc.xrcd = devr->x0; 2782322810Shselasky devr->s0->ext.xrc.cq = devr->c0; 2783322810Shselasky atomic_inc(&devr->s0->ext.xrc.xrcd->usecnt); 2784322810Shselasky atomic_inc(&devr->s0->ext.xrc.cq->usecnt); 2785322810Shselasky atomic_inc(&devr->p0->usecnt); 2786322810Shselasky atomic_set(&devr->s0->usecnt, 0); 2787322810Shselasky 2788322810Shselasky memset(&attr, 0, sizeof(attr)); 2789322810Shselasky attr.attr.max_sge = 1; 2790322810Shselasky attr.attr.max_wr = 1; 2791322810Shselasky attr.srq_type = IB_SRQT_BASIC; 2792322810Shselasky devr->s1 = mlx5_ib_create_srq(devr->p0, &attr, NULL); 2793322810Shselasky if (IS_ERR(devr->s1)) { 2794322810Shselasky ret = PTR_ERR(devr->s1); 2795322810Shselasky goto error5; 2796322810Shselasky } 2797322810Shselasky devr->s1->device = &dev->ib_dev; 2798322810Shselasky devr->s1->pd = devr->p0; 2799322810Shselasky devr->s1->uobject = NULL; 2800322810Shselasky devr->s1->event_handler = NULL; 2801322810Shselasky devr->s1->srq_context = NULL; 2802322810Shselasky devr->s1->srq_type = IB_SRQT_BASIC; 2803322810Shselasky devr->s1->ext.xrc.cq = devr->c0; 2804322810Shselasky atomic_inc(&devr->p0->usecnt); 2805331769Shselasky atomic_set(&devr->s0->usecnt, 0); 2806322810Shselasky 2807331769Shselasky for (port = 0; port < ARRAY_SIZE(devr->ports); ++port) { 2808331769Shselasky INIT_WORK(&devr->ports[port].pkey_change_work, 2809331769Shselasky pkey_change_handler); 2810331769Shselasky devr->ports[port].devr = devr; 2811331769Shselasky } 2812331769Shselasky 2813322810Shselasky return 0; 2814322810Shselasky 2815322810Shselaskyerror5: 2816322810Shselasky mlx5_ib_destroy_srq(devr->s0); 2817322810Shselaskyerror4: 2818322810Shselasky mlx5_ib_dealloc_xrcd(devr->x1); 2819322810Shselaskyerror3: 2820322810Shselasky mlx5_ib_dealloc_xrcd(devr->x0); 2821322810Shselaskyerror2: 2822322810Shselasky mlx5_ib_destroy_cq(devr->c0); 2823322810Shselaskyerror1: 2824322810Shselasky mlx5_ib_dealloc_pd(devr->p0); 2825322810Shselaskyerror0: 2826322810Shselasky return ret; 2827322810Shselasky} 2828322810Shselasky 2829322810Shselaskystatic void destroy_dev_resources(struct mlx5_ib_resources *devr) 2830322810Shselasky{ 2831331769Shselasky struct mlx5_ib_dev *dev = 2832331769Shselasky container_of(devr, struct mlx5_ib_dev, devr); 2833331769Shselasky int port; 2834331769Shselasky 2835322810Shselasky mlx5_ib_destroy_srq(devr->s1); 2836322810Shselasky mlx5_ib_destroy_srq(devr->s0); 2837322810Shselasky mlx5_ib_dealloc_xrcd(devr->x0); 2838322810Shselasky mlx5_ib_dealloc_xrcd(devr->x1); 2839322810Shselasky mlx5_ib_destroy_cq(devr->c0); 2840322810Shselasky mlx5_ib_dealloc_pd(devr->p0); 2841331769Shselasky 2842331769Shselasky /* Make sure no change P_Key work items are still executing */ 2843331769Shselasky for (port = 0; port < dev->num_ports; ++port) 2844331769Shselasky cancel_work_sync(&devr->ports[port].pkey_change_work); 2845322810Shselasky} 2846322810Shselasky 2847325604Shselaskystatic u32 get_core_cap_flags(struct ib_device *ibdev) 2848325604Shselasky{ 2849325604Shselasky struct mlx5_ib_dev *dev = to_mdev(ibdev); 2850325604Shselasky enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, 1); 2851325604Shselasky u8 l3_type_cap = MLX5_CAP_ROCE(dev->mdev, l3_type); 2852325604Shselasky u8 roce_version_cap = MLX5_CAP_ROCE(dev->mdev, roce_version); 2853325604Shselasky u32 ret = 0; 2854325604Shselasky 2855325604Shselasky if (ll == IB_LINK_LAYER_INFINIBAND) 2856325604Shselasky return RDMA_CORE_PORT_IBA_IB; 2857325604Shselasky 2858325604Shselasky if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV4_CAP)) 2859331769Shselasky return 0; 2860325604Shselasky 2861325604Shselasky if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV6_CAP)) 2862331769Shselasky return 0; 2863325604Shselasky 2864325604Shselasky if (roce_version_cap & MLX5_ROCE_VERSION_1_CAP) 2865325604Shselasky ret |= RDMA_CORE_PORT_IBA_ROCE; 2866325604Shselasky 2867325604Shselasky if (roce_version_cap & MLX5_ROCE_VERSION_2_CAP) 2868325604Shselasky ret |= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP; 2869325604Shselasky 2870325604Shselasky return ret; 2871325604Shselasky} 2872325604Shselasky 2873325604Shselaskystatic int mlx5_port_immutable(struct ib_device *ibdev, u8 port_num, 2874325604Shselasky struct ib_port_immutable *immutable) 2875325604Shselasky{ 2876325604Shselasky struct ib_port_attr attr; 2877325604Shselasky int err; 2878325604Shselasky 2879331769Shselasky err = mlx5_ib_query_port(ibdev, port_num, &attr); 2880325604Shselasky if (err) 2881325604Shselasky return err; 2882325604Shselasky 2883325604Shselasky immutable->pkey_tbl_len = attr.pkey_tbl_len; 2884325604Shselasky immutable->gid_tbl_len = attr.gid_tbl_len; 2885325604Shselasky immutable->core_cap_flags = get_core_cap_flags(ibdev); 2886331769Shselasky immutable->max_mad_size = IB_MGMT_MAD_SIZE; 2887325604Shselasky 2888325604Shselasky return 0; 2889325604Shselasky} 2890325604Shselasky 2891331769Shselaskystatic void get_dev_fw_str(struct ib_device *ibdev, char *str, 2892331769Shselasky size_t str_len) 2893322810Shselasky{ 2894331769Shselasky struct mlx5_ib_dev *dev = 2895331769Shselasky container_of(ibdev, struct mlx5_ib_dev, ib_dev); 2896331769Shselasky snprintf(str, str_len, "%d.%d.%04d", fw_rev_maj(dev->mdev), 2897331769Shselasky fw_rev_min(dev->mdev), fw_rev_sub(dev->mdev)); 2898331769Shselasky} 2899322810Shselasky 2900331769Shselaskystatic int mlx5_roce_lag_init(struct mlx5_ib_dev *dev) 2901331769Shselasky{ 2902331769Shselasky return 0; 2903331769Shselasky} 2904322810Shselasky 2905331769Shselaskystatic void mlx5_roce_lag_cleanup(struct mlx5_ib_dev *dev) 2906331769Shselasky{ 2907331769Shselasky} 2908322810Shselasky 2909331769Shselaskystatic void mlx5_remove_roce_notifier(struct mlx5_ib_dev *dev) 2910331769Shselasky{ 2911331769Shselasky if (dev->roce.nb.notifier_call) { 2912331769Shselasky unregister_netdevice_notifier(&dev->roce.nb); 2913331769Shselasky dev->roce.nb.notifier_call = NULL; 2914322810Shselasky } 2915322810Shselasky} 2916322810Shselasky 2917331769Shselaskystatic int mlx5_enable_roce(struct mlx5_ib_dev *dev) 2918322810Shselasky{ 2919331769Shselasky VNET_ITERATOR_DECL(vnet_iter); 2920331769Shselasky struct net_device *idev; 2921322810Shselasky int err; 2922322810Shselasky 2923331769Shselasky /* Check if mlx5en net device already exists */ 2924331769Shselasky VNET_LIST_RLOCK(); 2925331769Shselasky VNET_FOREACH(vnet_iter) { 2926331769Shselasky IFNET_RLOCK(); 2927331769Shselasky CURVNET_SET_QUIET(vnet_iter); 2928331769Shselasky TAILQ_FOREACH(idev, &V_ifnet, if_link) { 2929331769Shselasky /* check if network interface belongs to mlx5en */ 2930331769Shselasky if (!mlx5_netdev_match(idev, dev->mdev, "mce")) 2931331769Shselasky continue; 2932331769Shselasky write_lock(&dev->roce.netdev_lock); 2933331769Shselasky dev->roce.netdev = idev; 2934331769Shselasky write_unlock(&dev->roce.netdev_lock); 2935331769Shselasky } 2936331769Shselasky CURVNET_RESTORE(); 2937331769Shselasky IFNET_RUNLOCK(); 2938331769Shselasky } 2939331769Shselasky VNET_LIST_RUNLOCK(); 2940322810Shselasky 2941331769Shselasky dev->roce.nb.notifier_call = mlx5_netdev_event; 2942331769Shselasky err = register_netdevice_notifier(&dev->roce.nb); 2943322810Shselasky if (err) { 2944331769Shselasky dev->roce.nb.notifier_call = NULL; 2945331769Shselasky return err; 2946322810Shselasky } 2947322810Shselasky 2948331769Shselasky err = mlx5_nic_vport_enable_roce(dev->mdev); 2949331769Shselasky if (err) 2950331769Shselasky goto err_unregister_netdevice_notifier; 2951322810Shselasky 2952331769Shselasky err = mlx5_roce_lag_init(dev); 2953331769Shselasky if (err) 2954331769Shselasky goto err_disable_roce; 2955322810Shselasky 2956331769Shselasky return 0; 2957322810Shselasky 2958331769Shselaskyerr_disable_roce: 2959331769Shselasky mlx5_nic_vport_disable_roce(dev->mdev); 2960322810Shselasky 2961331769Shselaskyerr_unregister_netdevice_notifier: 2962331769Shselasky mlx5_remove_roce_notifier(dev); 2963331769Shselasky return err; 2964322810Shselasky} 2965322810Shselasky 2966331769Shselaskystatic void mlx5_disable_roce(struct mlx5_ib_dev *dev) 2967322810Shselasky{ 2968331769Shselasky mlx5_roce_lag_cleanup(dev); 2969331769Shselasky mlx5_nic_vport_disable_roce(dev->mdev); 2970322810Shselasky} 2971322810Shselasky 2972322810Shselaskystatic void mlx5_ib_dealloc_q_port_counter(struct mlx5_ib_dev *dev, u8 port_num) 2973322810Shselasky{ 2974322810Shselasky mlx5_vport_dealloc_q_counter(dev->mdev, 2975322810Shselasky MLX5_INTERFACE_PROTOCOL_IB, 2976322810Shselasky dev->port[port_num].q_cnt_id); 2977322810Shselasky dev->port[port_num].q_cnt_id = 0; 2978322810Shselasky} 2979322810Shselasky 2980322810Shselaskystatic void mlx5_ib_dealloc_q_counters(struct mlx5_ib_dev *dev) 2981322810Shselasky{ 2982322810Shselasky unsigned int i; 2983322810Shselasky 2984322810Shselasky for (i = 0; i < dev->num_ports; i++) 2985322810Shselasky mlx5_ib_dealloc_q_port_counter(dev, i); 2986322810Shselasky} 2987322810Shselasky 2988322810Shselaskystatic int mlx5_ib_alloc_q_counters(struct mlx5_ib_dev *dev) 2989322810Shselasky{ 2990322810Shselasky int i; 2991322810Shselasky int ret; 2992322810Shselasky 2993322810Shselasky for (i = 0; i < dev->num_ports; i++) { 2994322810Shselasky ret = mlx5_vport_alloc_q_counter(dev->mdev, 2995322810Shselasky MLX5_INTERFACE_PROTOCOL_IB, 2996322810Shselasky &dev->port[i].q_cnt_id); 2997322810Shselasky if (ret) { 2998322810Shselasky mlx5_ib_warn(dev, 2999331769Shselasky "couldn't allocate queue counter for port %d, err %d\n", 3000331769Shselasky i + 1, ret); 3001322810Shselasky goto dealloc_counters; 3002322810Shselasky } 3003322810Shselasky } 3004322810Shselasky 3005322810Shselasky return 0; 3006322810Shselasky 3007322810Shselaskydealloc_counters: 3008322810Shselasky while (--i >= 0) 3009322810Shselasky mlx5_ib_dealloc_q_port_counter(dev, i); 3010322810Shselasky 3011322810Shselasky return ret; 3012322810Shselasky} 3013322810Shselasky 3014331769Shselaskystatic const char * const names[] = { 3015331769Shselasky "rx_write_requests", 3016331769Shselasky "rx_read_requests", 3017331769Shselasky "rx_atomic_requests", 3018331769Shselasky "out_of_buffer", 3019331769Shselasky "out_of_sequence", 3020331769Shselasky "duplicate_request", 3021331769Shselasky "rnr_nak_retry_err", 3022331769Shselasky "packet_seq_err", 3023331769Shselasky "implied_nak_seq_err", 3024331769Shselasky "local_ack_timeout_err", 3025322810Shselasky}; 3026322810Shselasky 3027331769Shselaskystatic const size_t stats_offsets[] = { 3028331769Shselasky MLX5_BYTE_OFF(query_q_counter_out, rx_write_requests), 3029331769Shselasky MLX5_BYTE_OFF(query_q_counter_out, rx_read_requests), 3030331769Shselasky MLX5_BYTE_OFF(query_q_counter_out, rx_atomic_requests), 3031331769Shselasky MLX5_BYTE_OFF(query_q_counter_out, out_of_buffer), 3032331769Shselasky MLX5_BYTE_OFF(query_q_counter_out, out_of_sequence), 3033331769Shselasky MLX5_BYTE_OFF(query_q_counter_out, duplicate_request), 3034331769Shselasky MLX5_BYTE_OFF(query_q_counter_out, rnr_nak_retry_err), 3035331769Shselasky MLX5_BYTE_OFF(query_q_counter_out, packet_seq_err), 3036331769Shselasky MLX5_BYTE_OFF(query_q_counter_out, implied_nak_seq_err), 3037331769Shselasky MLX5_BYTE_OFF(query_q_counter_out, local_ack_timeout_err), 3038322810Shselasky}; 3039322810Shselasky 3040331769Shselaskystatic struct rdma_hw_stats *mlx5_ib_alloc_hw_stats(struct ib_device *ibdev, 3041331769Shselasky u8 port_num) 3042322810Shselasky{ 3043331769Shselasky BUILD_BUG_ON(ARRAY_SIZE(names) != ARRAY_SIZE(stats_offsets)); 3044322810Shselasky 3045331769Shselasky /* We support only per port stats */ 3046331769Shselasky if (port_num == 0) 3047331769Shselasky return NULL; 3048322810Shselasky 3049331769Shselasky return rdma_alloc_hw_stats_struct(names, ARRAY_SIZE(names), 3050331769Shselasky RDMA_HW_STATS_DEFAULT_LIFESPAN); 3051322810Shselasky} 3052322810Shselasky 3053331769Shselaskystatic int mlx5_ib_get_hw_stats(struct ib_device *ibdev, 3054331769Shselasky struct rdma_hw_stats *stats, 3055331769Shselasky u8 port, int index) 3056322810Shselasky{ 3057331769Shselasky struct mlx5_ib_dev *dev = to_mdev(ibdev); 3058322810Shselasky int outlen = MLX5_ST_SZ_BYTES(query_q_counter_out); 3059322810Shselasky void *out; 3060331769Shselasky __be32 val; 3061322810Shselasky int ret; 3062331769Shselasky int i; 3063322810Shselasky 3064331769Shselasky if (!port || !stats) 3065331769Shselasky return -ENOSYS; 3066331769Shselasky 3067322810Shselasky out = mlx5_vzalloc(outlen); 3068322810Shselasky if (!out) 3069322810Shselasky return -ENOMEM; 3070322810Shselasky 3071331769Shselasky ret = mlx5_vport_query_q_counter(dev->mdev, 3072331769Shselasky dev->port[port - 1].q_cnt_id, 0, 3073331769Shselasky out, outlen); 3074322810Shselasky if (ret) 3075322810Shselasky goto free; 3076322810Shselasky 3077331769Shselasky for (i = 0; i < ARRAY_SIZE(names); i++) { 3078331769Shselasky val = *(__be32 *)(out + stats_offsets[i]); 3079331769Shselasky stats->value[i] = (u64)be32_to_cpu(val); 3080331769Shselasky } 3081322810Shselaskyfree: 3082331769Shselasky kvfree(out); 3083331769Shselasky return ARRAY_SIZE(names); 3084322810Shselasky} 3085322810Shselasky 3086322810Shselaskystatic void *mlx5_ib_add(struct mlx5_core_dev *mdev) 3087322810Shselasky{ 3088322810Shselasky struct mlx5_ib_dev *dev; 3089331769Shselasky enum rdma_link_layer ll; 3090331769Shselasky int port_type_cap; 3091322810Shselasky int err; 3092322810Shselasky int i; 3093322810Shselasky 3094331769Shselasky port_type_cap = MLX5_CAP_GEN(mdev, port_type); 3095331769Shselasky ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap); 3096331769Shselasky 3097331769Shselasky if ((ll == IB_LINK_LAYER_ETHERNET) && !MLX5_CAP_GEN(mdev, roce)) 3098331769Shselasky return NULL; 3099331769Shselasky 3100322810Shselasky dev = (struct mlx5_ib_dev *)ib_alloc_device(sizeof(*dev)); 3101322810Shselasky if (!dev) 3102322810Shselasky return NULL; 3103322810Shselasky 3104322810Shselasky dev->mdev = mdev; 3105322810Shselasky 3106322810Shselasky dev->port = kcalloc(MLX5_CAP_GEN(mdev, num_ports), sizeof(*dev->port), 3107331769Shselasky GFP_KERNEL); 3108322810Shselasky if (!dev->port) 3109322810Shselasky goto err_dealloc; 3110322810Shselasky 3111331769Shselasky rwlock_init(&dev->roce.netdev_lock); 3112322810Shselasky err = get_port_caps(dev); 3113322810Shselasky if (err) 3114322810Shselasky goto err_free_port; 3115322810Shselasky 3116322810Shselasky if (mlx5_use_mad_ifc(dev)) 3117322810Shselasky get_ext_port_caps(dev); 3118322810Shselasky 3119322810Shselasky MLX5_INIT_DOORBELL_LOCK(&dev->uar_lock); 3120322810Shselasky 3121337099Shselasky snprintf(dev->ib_dev.name, IB_DEVICE_NAME_MAX, "mlx5_%d", device_get_unit(mdev->pdev->dev.bsddev)); 3122322810Shselasky dev->ib_dev.owner = THIS_MODULE; 3123322810Shselasky dev->ib_dev.node_type = RDMA_NODE_IB_CA; 3124331769Shselasky dev->ib_dev.local_dma_lkey = 0 /* not supported for now */; 3125322810Shselasky dev->num_ports = MLX5_CAP_GEN(mdev, num_ports); 3126322810Shselasky dev->ib_dev.phys_port_cnt = dev->num_ports; 3127322810Shselasky dev->ib_dev.num_comp_vectors = 3128322810Shselasky dev->mdev->priv.eq_table.num_comp_vectors; 3129322810Shselasky dev->ib_dev.dma_device = &mdev->pdev->dev; 3130322810Shselasky 3131322810Shselasky dev->ib_dev.uverbs_abi_ver = MLX5_IB_UVERBS_ABI_VERSION; 3132322810Shselasky dev->ib_dev.uverbs_cmd_mask = 3133322810Shselasky (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) | 3134322810Shselasky (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) | 3135322810Shselasky (1ull << IB_USER_VERBS_CMD_QUERY_PORT) | 3136322810Shselasky (1ull << IB_USER_VERBS_CMD_ALLOC_PD) | 3137322810Shselasky (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) | 3138331784Shselasky (1ull << IB_USER_VERBS_CMD_CREATE_AH) | 3139331784Shselasky (1ull << IB_USER_VERBS_CMD_DESTROY_AH) | 3140322810Shselasky (1ull << IB_USER_VERBS_CMD_REG_MR) | 3141331769Shselasky (1ull << IB_USER_VERBS_CMD_REREG_MR) | 3142322810Shselasky (1ull << IB_USER_VERBS_CMD_DEREG_MR) | 3143322810Shselasky (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) | 3144322810Shselasky (1ull << IB_USER_VERBS_CMD_CREATE_CQ) | 3145322810Shselasky (1ull << IB_USER_VERBS_CMD_RESIZE_CQ) | 3146322810Shselasky (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) | 3147322810Shselasky (1ull << IB_USER_VERBS_CMD_CREATE_QP) | 3148322810Shselasky (1ull << IB_USER_VERBS_CMD_MODIFY_QP) | 3149322810Shselasky (1ull << IB_USER_VERBS_CMD_QUERY_QP) | 3150322810Shselasky (1ull << IB_USER_VERBS_CMD_DESTROY_QP) | 3151322810Shselasky (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST) | 3152322810Shselasky (1ull << IB_USER_VERBS_CMD_DETACH_MCAST) | 3153322810Shselasky (1ull << IB_USER_VERBS_CMD_CREATE_SRQ) | 3154322810Shselasky (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ) | 3155322810Shselasky (1ull << IB_USER_VERBS_CMD_QUERY_SRQ) | 3156322810Shselasky (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ) | 3157322810Shselasky (1ull << IB_USER_VERBS_CMD_CREATE_XSRQ) | 3158322810Shselasky (1ull << IB_USER_VERBS_CMD_OPEN_QP); 3159331769Shselasky dev->ib_dev.uverbs_ex_cmd_mask = 3160331769Shselasky (1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE) | 3161331769Shselasky (1ull << IB_USER_VERBS_EX_CMD_CREATE_CQ) | 3162331769Shselasky (1ull << IB_USER_VERBS_EX_CMD_CREATE_QP); 3163322810Shselasky 3164322810Shselasky dev->ib_dev.query_device = mlx5_ib_query_device; 3165322810Shselasky dev->ib_dev.query_port = mlx5_ib_query_port; 3166322810Shselasky dev->ib_dev.get_link_layer = mlx5_ib_port_link_layer; 3167331769Shselasky if (ll == IB_LINK_LAYER_ETHERNET) 3168331769Shselasky dev->ib_dev.get_netdev = mlx5_ib_get_netdev; 3169322810Shselasky dev->ib_dev.query_gid = mlx5_ib_query_gid; 3170331769Shselasky dev->ib_dev.add_gid = mlx5_ib_add_gid; 3171331769Shselasky dev->ib_dev.del_gid = mlx5_ib_del_gid; 3172322810Shselasky dev->ib_dev.query_pkey = mlx5_ib_query_pkey; 3173322810Shselasky dev->ib_dev.modify_device = mlx5_ib_modify_device; 3174322810Shselasky dev->ib_dev.modify_port = mlx5_ib_modify_port; 3175322810Shselasky dev->ib_dev.alloc_ucontext = mlx5_ib_alloc_ucontext; 3176322810Shselasky dev->ib_dev.dealloc_ucontext = mlx5_ib_dealloc_ucontext; 3177322810Shselasky dev->ib_dev.mmap = mlx5_ib_mmap; 3178322810Shselasky dev->ib_dev.alloc_pd = mlx5_ib_alloc_pd; 3179322810Shselasky dev->ib_dev.dealloc_pd = mlx5_ib_dealloc_pd; 3180322810Shselasky dev->ib_dev.create_ah = mlx5_ib_create_ah; 3181322810Shselasky dev->ib_dev.query_ah = mlx5_ib_query_ah; 3182322810Shselasky dev->ib_dev.destroy_ah = mlx5_ib_destroy_ah; 3183322810Shselasky dev->ib_dev.create_srq = mlx5_ib_create_srq; 3184322810Shselasky dev->ib_dev.modify_srq = mlx5_ib_modify_srq; 3185322810Shselasky dev->ib_dev.query_srq = mlx5_ib_query_srq; 3186322810Shselasky dev->ib_dev.destroy_srq = mlx5_ib_destroy_srq; 3187322810Shselasky dev->ib_dev.post_srq_recv = mlx5_ib_post_srq_recv; 3188322810Shselasky dev->ib_dev.create_qp = mlx5_ib_create_qp; 3189322810Shselasky dev->ib_dev.modify_qp = mlx5_ib_modify_qp; 3190322810Shselasky dev->ib_dev.query_qp = mlx5_ib_query_qp; 3191322810Shselasky dev->ib_dev.destroy_qp = mlx5_ib_destroy_qp; 3192322810Shselasky dev->ib_dev.post_send = mlx5_ib_post_send; 3193322810Shselasky dev->ib_dev.post_recv = mlx5_ib_post_recv; 3194322810Shselasky dev->ib_dev.create_cq = mlx5_ib_create_cq; 3195322810Shselasky dev->ib_dev.modify_cq = mlx5_ib_modify_cq; 3196322810Shselasky dev->ib_dev.resize_cq = mlx5_ib_resize_cq; 3197322810Shselasky dev->ib_dev.destroy_cq = mlx5_ib_destroy_cq; 3198322810Shselasky dev->ib_dev.poll_cq = mlx5_ib_poll_cq; 3199322810Shselasky dev->ib_dev.req_notify_cq = mlx5_ib_arm_cq; 3200322810Shselasky dev->ib_dev.get_dma_mr = mlx5_ib_get_dma_mr; 3201322810Shselasky dev->ib_dev.reg_user_mr = mlx5_ib_reg_user_mr; 3202331769Shselasky dev->ib_dev.rereg_user_mr = mlx5_ib_rereg_user_mr; 3203322810Shselasky dev->ib_dev.reg_phys_mr = mlx5_ib_reg_phys_mr; 3204322810Shselasky dev->ib_dev.dereg_mr = mlx5_ib_dereg_mr; 3205322810Shselasky dev->ib_dev.attach_mcast = mlx5_ib_mcg_attach; 3206322810Shselasky dev->ib_dev.detach_mcast = mlx5_ib_mcg_detach; 3207322810Shselasky dev->ib_dev.process_mad = mlx5_ib_process_mad; 3208331769Shselasky dev->ib_dev.alloc_mr = mlx5_ib_alloc_mr; 3209331769Shselasky dev->ib_dev.map_mr_sg = mlx5_ib_map_mr_sg; 3210331769Shselasky dev->ib_dev.check_mr_status = mlx5_ib_check_mr_status; 3211325604Shselasky dev->ib_dev.get_port_immutable = mlx5_port_immutable; 3212331769Shselasky dev->ib_dev.get_dev_fw_str = get_dev_fw_str; 3213331769Shselasky if (mlx5_core_is_pf(mdev)) { 3214331769Shselasky dev->ib_dev.get_vf_config = mlx5_ib_get_vf_config; 3215331769Shselasky dev->ib_dev.set_vf_link_state = mlx5_ib_set_vf_link_state; 3216331769Shselasky dev->ib_dev.get_vf_stats = mlx5_ib_get_vf_stats; 3217331769Shselasky dev->ib_dev.set_vf_guid = mlx5_ib_set_vf_guid; 3218331769Shselasky } 3219322810Shselasky 3220347801Shselasky dev->ib_dev.disassociate_ucontext = mlx5_ib_disassociate_ucontext; 3221347801Shselasky 3222331769Shselasky mlx5_ib_internal_fill_odp_caps(dev); 3223331769Shselasky 3224331769Shselasky if (MLX5_CAP_GEN(mdev, imaicl)) { 3225331769Shselasky dev->ib_dev.alloc_mw = mlx5_ib_alloc_mw; 3226331769Shselasky dev->ib_dev.dealloc_mw = mlx5_ib_dealloc_mw; 3227331769Shselasky dev->ib_dev.uverbs_cmd_mask |= 3228331769Shselasky (1ull << IB_USER_VERBS_CMD_ALLOC_MW) | 3229331769Shselasky (1ull << IB_USER_VERBS_CMD_DEALLOC_MW); 3230331769Shselasky } 3231331769Shselasky 3232331769Shselasky if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt) && 3233331769Shselasky MLX5_CAP_GEN(dev->mdev, retransmission_q_counters)) { 3234331769Shselasky dev->ib_dev.get_hw_stats = mlx5_ib_get_hw_stats; 3235331769Shselasky dev->ib_dev.alloc_hw_stats = mlx5_ib_alloc_hw_stats; 3236331769Shselasky } 3237331769Shselasky 3238322810Shselasky if (MLX5_CAP_GEN(mdev, xrc)) { 3239322810Shselasky dev->ib_dev.alloc_xrcd = mlx5_ib_alloc_xrcd; 3240322810Shselasky dev->ib_dev.dealloc_xrcd = mlx5_ib_dealloc_xrcd; 3241322810Shselasky dev->ib_dev.uverbs_cmd_mask |= 3242322810Shselasky (1ull << IB_USER_VERBS_CMD_OPEN_XRCD) | 3243322810Shselasky (1ull << IB_USER_VERBS_CMD_CLOSE_XRCD); 3244322810Shselasky } 3245322810Shselasky 3246331769Shselasky if (mlx5_ib_port_link_layer(&dev->ib_dev, 1) == 3247331769Shselasky IB_LINK_LAYER_ETHERNET) { 3248331769Shselasky dev->ib_dev.create_flow = mlx5_ib_create_flow; 3249331769Shselasky dev->ib_dev.destroy_flow = mlx5_ib_destroy_flow; 3250331769Shselasky dev->ib_dev.create_wq = mlx5_ib_create_wq; 3251331769Shselasky dev->ib_dev.modify_wq = mlx5_ib_modify_wq; 3252331769Shselasky dev->ib_dev.destroy_wq = mlx5_ib_destroy_wq; 3253331769Shselasky dev->ib_dev.create_rwq_ind_table = mlx5_ib_create_rwq_ind_table; 3254331769Shselasky dev->ib_dev.destroy_rwq_ind_table = mlx5_ib_destroy_rwq_ind_table; 3255331769Shselasky dev->ib_dev.uverbs_ex_cmd_mask |= 3256331769Shselasky (1ull << IB_USER_VERBS_EX_CMD_CREATE_FLOW) | 3257331769Shselasky (1ull << IB_USER_VERBS_EX_CMD_DESTROY_FLOW) | 3258331769Shselasky (1ull << IB_USER_VERBS_EX_CMD_CREATE_WQ) | 3259331769Shselasky (1ull << IB_USER_VERBS_EX_CMD_MODIFY_WQ) | 3260331769Shselasky (1ull << IB_USER_VERBS_EX_CMD_DESTROY_WQ) | 3261331769Shselasky (1ull << IB_USER_VERBS_EX_CMD_CREATE_RWQ_IND_TBL) | 3262331769Shselasky (1ull << IB_USER_VERBS_EX_CMD_DESTROY_RWQ_IND_TBL); 3263331769Shselasky } 3264322810Shselasky err = init_node_data(dev); 3265322810Shselasky if (err) 3266331769Shselasky goto err_free_port; 3267322810Shselasky 3268331769Shselasky mutex_init(&dev->flow_db.lock); 3269322810Shselasky mutex_init(&dev->cap_mask_mutex); 3270322810Shselasky INIT_LIST_HEAD(&dev->qp_list); 3271322810Shselasky spin_lock_init(&dev->reset_flow_resource_lock); 3272322810Shselasky 3273331769Shselasky if (ll == IB_LINK_LAYER_ETHERNET) { 3274331769Shselasky err = mlx5_enable_roce(dev); 3275331769Shselasky if (err) 3276331769Shselasky goto err_free_port; 3277331769Shselasky } 3278331769Shselasky 3279322810Shselasky err = create_dev_resources(&dev->devr); 3280322810Shselasky if (err) 3281322810Shselasky goto err_disable_roce; 3282322810Shselasky 3283331769Shselasky err = mlx5_ib_odp_init_one(dev); 3284331769Shselasky if (err) 3285331769Shselasky goto err_rsrc; 3286322810Shselasky 3287322810Shselasky err = mlx5_ib_alloc_q_counters(dev); 3288322810Shselasky if (err) 3289322810Shselasky goto err_odp; 3290322810Shselasky 3291322810Shselasky err = ib_register_device(&dev->ib_dev, NULL); 3292322810Shselasky if (err) 3293322810Shselasky goto err_q_cnt; 3294322810Shselasky 3295322810Shselasky err = create_umr_res(dev); 3296322810Shselasky if (err) 3297322810Shselasky goto err_dev; 3298322810Shselasky 3299322810Shselasky for (i = 0; i < ARRAY_SIZE(mlx5_class_attributes); i++) { 3300322810Shselasky err = device_create_file(&dev->ib_dev.dev, 3301322810Shselasky mlx5_class_attributes[i]); 3302322810Shselasky if (err) 3303331769Shselasky goto err_umrc; 3304322810Shselasky } 3305322810Shselasky 3306331808Shselasky err = mlx5_ib_init_congestion(dev); 3307331808Shselasky if (err) 3308331808Shselasky goto err_umrc; 3309331808Shselasky 3310322810Shselasky dev->ib_active = true; 3311322810Shselasky 3312322810Shselasky return dev; 3313322810Shselasky 3314331769Shselaskyerr_umrc: 3315322810Shselasky destroy_umrc_res(dev); 3316322810Shselasky 3317322810Shselaskyerr_dev: 3318322810Shselasky ib_unregister_device(&dev->ib_dev); 3319322810Shselasky 3320322810Shselaskyerr_q_cnt: 3321322810Shselasky mlx5_ib_dealloc_q_counters(dev); 3322322810Shselasky 3323322810Shselaskyerr_odp: 3324331769Shselasky mlx5_ib_odp_remove_one(dev); 3325331769Shselasky 3326331769Shselaskyerr_rsrc: 3327322810Shselasky destroy_dev_resources(&dev->devr); 3328322810Shselasky 3329322810Shselaskyerr_disable_roce: 3330331769Shselasky if (ll == IB_LINK_LAYER_ETHERNET) { 3331331769Shselasky mlx5_disable_roce(dev); 3332331769Shselasky mlx5_remove_roce_notifier(dev); 3333331769Shselasky } 3334331769Shselasky 3335322810Shselaskyerr_free_port: 3336322810Shselasky kfree(dev->port); 3337322810Shselasky 3338322810Shselaskyerr_dealloc: 3339322810Shselasky ib_dealloc_device((struct ib_device *)dev); 3340322810Shselasky 3341322810Shselasky return NULL; 3342322810Shselasky} 3343322810Shselasky 3344322810Shselaskystatic void mlx5_ib_remove(struct mlx5_core_dev *mdev, void *context) 3345322810Shselasky{ 3346322810Shselasky struct mlx5_ib_dev *dev = context; 3347331769Shselasky enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev, 1); 3348322810Shselasky 3349331808Shselasky mlx5_ib_cleanup_congestion(dev); 3350331769Shselasky mlx5_remove_roce_notifier(dev); 3351331769Shselasky ib_unregister_device(&dev->ib_dev); 3352322810Shselasky mlx5_ib_dealloc_q_counters(dev); 3353322810Shselasky destroy_umrc_res(dev); 3354331769Shselasky mlx5_ib_odp_remove_one(dev); 3355322810Shselasky destroy_dev_resources(&dev->devr); 3356331769Shselasky if (ll == IB_LINK_LAYER_ETHERNET) 3357331769Shselasky mlx5_disable_roce(dev); 3358322810Shselasky kfree(dev->port); 3359322810Shselasky ib_dealloc_device(&dev->ib_dev); 3360322810Shselasky} 3361322810Shselasky 3362322810Shselaskystatic struct mlx5_interface mlx5_ib_interface = { 3363322810Shselasky .add = mlx5_ib_add, 3364322810Shselasky .remove = mlx5_ib_remove, 3365322810Shselasky .event = mlx5_ib_event, 3366322810Shselasky .protocol = MLX5_INTERFACE_PROTOCOL_IB, 3367322810Shselasky}; 3368322810Shselasky 3369322810Shselaskystatic int __init mlx5_ib_init(void) 3370322810Shselasky{ 3371322810Shselasky int err; 3372322810Shselasky 3373331769Shselasky err = mlx5_ib_odp_init(); 3374331769Shselasky if (err) 3375331769Shselasky return err; 3376331769Shselasky 3377322810Shselasky err = mlx5_register_interface(&mlx5_ib_interface); 3378322810Shselasky if (err) 3379322810Shselasky goto clean_odp; 3380322810Shselasky 3381322810Shselasky return err; 3382322810Shselasky 3383322810Shselaskyclean_odp: 3384331769Shselasky mlx5_ib_odp_cleanup(); 3385322810Shselasky return err; 3386322810Shselasky} 3387322810Shselasky 3388322810Shselaskystatic void __exit mlx5_ib_cleanup(void) 3389322810Shselasky{ 3390322810Shselasky mlx5_unregister_interface(&mlx5_ib_interface); 3391331769Shselasky mlx5_ib_odp_cleanup(); 3392322810Shselasky} 3393322810Shselasky 3394341948Shselaskystatic void 3395341948Shselaskymlx5_ib_show_version(void __unused *arg) 3396341948Shselasky{ 3397341948Shselasky 3398341948Shselasky printf("%s", mlx5_version); 3399341948Shselasky} 3400341948ShselaskySYSINIT(mlx5_ib_show_version, SI_SUB_DRIVERS, SI_ORDER_ANY, mlx5_ib_show_version, NULL); 3401341948Shselasky 3402363151Shselaskymodule_init_order(mlx5_ib_init, SI_ORDER_SEVENTH); 3403363151Shselaskymodule_exit_order(mlx5_ib_cleanup, SI_ORDER_SEVENTH); 3404