mlx5_ib_main.c revision 347801
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 347801 2019-05-16 17:14:08Z 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 55341987Shselasky#define DRIVER_VERSION "3.5.0" 56337101Shselasky#endif 57341987Shselasky#define DRIVER_RELDATE "November 2018" 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 66322810Shselaskystatic int deprecated_prof_sel = 2; 67322810Shselaskymodule_param_named(prof_sel, deprecated_prof_sel, int, 0444); 68322810ShselaskyMODULE_PARM_DESC(prof_sel, "profile selector. Deprecated here. Moved to module mlx5_core"); 69322810Shselasky 70337101Shselaskystatic const char mlx5_version[] = 71337101Shselasky DRIVER_NAME ": Mellanox Connect-IB Infiniband driver " 72331769Shselasky DRIVER_VERSION " (" DRIVER_RELDATE ")\n"; 73331769Shselasky 74322810Shselaskyenum { 75331769Shselasky MLX5_ATOMIC_SIZE_QP_8BYTES = 1 << 3, 76322810Shselasky}; 77322810Shselasky 78331769Shselaskystatic enum rdma_link_layer 79331769Shselaskymlx5_port_type_cap_to_rdma_ll(int port_type_cap) 80331769Shselasky{ 81331769Shselasky switch (port_type_cap) { 82331769Shselasky case MLX5_CAP_PORT_TYPE_IB: 83331769Shselasky return IB_LINK_LAYER_INFINIBAND; 84331769Shselasky case MLX5_CAP_PORT_TYPE_ETH: 85331769Shselasky return IB_LINK_LAYER_ETHERNET; 86331769Shselasky default: 87331769Shselasky return IB_LINK_LAYER_UNSPECIFIED; 88331769Shselasky } 89331769Shselasky} 90322810Shselasky 91331769Shselaskystatic enum rdma_link_layer 92331769Shselaskymlx5_ib_port_link_layer(struct ib_device *device, u8 port_num) 93331769Shselasky{ 94331769Shselasky struct mlx5_ib_dev *dev = to_mdev(device); 95331769Shselasky int port_type_cap = MLX5_CAP_GEN(dev->mdev, port_type); 96322810Shselasky 97331769Shselasky return mlx5_port_type_cap_to_rdma_ll(port_type_cap); 98331769Shselasky} 99331769Shselasky 100331769Shselaskystatic bool mlx5_netdev_match(struct net_device *ndev, 101331769Shselasky struct mlx5_core_dev *mdev, 102331769Shselasky const char *dname) 103322810Shselasky{ 104331769Shselasky return ndev->if_type == IFT_ETHER && 105331769Shselasky ndev->if_dname != NULL && 106331769Shselasky strcmp(ndev->if_dname, dname) == 0 && 107331769Shselasky ndev->if_softc != NULL && 108331769Shselasky *(struct mlx5_core_dev **)ndev->if_softc == mdev; 109331769Shselasky} 110322810Shselasky 111331769Shselaskystatic int mlx5_netdev_event(struct notifier_block *this, 112331769Shselasky unsigned long event, void *ptr) 113331769Shselasky{ 114331769Shselasky struct net_device *ndev = netdev_notifier_info_to_dev(ptr); 115331769Shselasky struct mlx5_ib_dev *ibdev = container_of(this, struct mlx5_ib_dev, 116331769Shselasky roce.nb); 117322810Shselasky 118331769Shselasky switch (event) { 119331769Shselasky case NETDEV_REGISTER: 120331769Shselasky case NETDEV_UNREGISTER: 121331769Shselasky write_lock(&ibdev->roce.netdev_lock); 122331769Shselasky /* check if network interface belongs to mlx5en */ 123331769Shselasky if (mlx5_netdev_match(ndev, ibdev->mdev, "mce")) 124331769Shselasky ibdev->roce.netdev = (event == NETDEV_UNREGISTER) ? 125331769Shselasky NULL : ndev; 126331769Shselasky write_unlock(&ibdev->roce.netdev_lock); 127331769Shselasky break; 128331769Shselasky 129331769Shselasky case NETDEV_UP: 130331769Shselasky case NETDEV_DOWN: { 131331769Shselasky struct net_device *upper = NULL; 132331769Shselasky 133331769Shselasky if ((upper == ndev || (!upper && ndev == ibdev->roce.netdev)) 134331769Shselasky && ibdev->ib_active) { 135331769Shselasky struct ib_event ibev = {0}; 136331769Shselasky 137331769Shselasky ibev.device = &ibdev->ib_dev; 138331769Shselasky ibev.event = (event == NETDEV_UP) ? 139331769Shselasky IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR; 140331769Shselasky ibev.element.port_num = 1; 141331769Shselasky ib_dispatch_event(&ibev); 142322810Shselasky } 143331769Shselasky break; 144322810Shselasky } 145322810Shselasky 146331769Shselasky default: 147331769Shselasky break; 148322810Shselasky } 149331769Shselasky 150331769Shselasky return NOTIFY_DONE; 151322810Shselasky} 152322810Shselasky 153331769Shselaskystatic struct net_device *mlx5_ib_get_netdev(struct ib_device *device, 154331769Shselasky u8 port_num) 155322810Shselasky{ 156331769Shselasky struct mlx5_ib_dev *ibdev = to_mdev(device); 157331769Shselasky struct net_device *ndev; 158331769Shselasky 159331769Shselasky /* Ensure ndev does not disappear before we invoke dev_hold() 160331769Shselasky */ 161331769Shselasky read_lock(&ibdev->roce.netdev_lock); 162331769Shselasky ndev = ibdev->roce.netdev; 163331769Shselasky if (ndev) 164331769Shselasky dev_hold(ndev); 165331769Shselasky read_unlock(&ibdev->roce.netdev_lock); 166331769Shselasky 167331769Shselasky return ndev; 168331769Shselasky} 169331769Shselasky 170331805Shselaskystatic int translate_eth_proto_oper(u32 eth_proto_oper, u8 *active_speed, 171331805Shselasky u8 *active_width) 172331805Shselasky{ 173331805Shselasky switch (eth_proto_oper) { 174331805Shselasky case MLX5E_PROT_MASK(MLX5E_1000BASE_CX_SGMII): 175331805Shselasky case MLX5E_PROT_MASK(MLX5E_1000BASE_KX): 176331805Shselasky case MLX5E_PROT_MASK(MLX5E_100BASE_TX): 177331805Shselasky case MLX5E_PROT_MASK(MLX5E_1000BASE_T): 178331805Shselasky *active_width = IB_WIDTH_1X; 179331805Shselasky *active_speed = IB_SPEED_SDR; 180331805Shselasky break; 181331805Shselasky case MLX5E_PROT_MASK(MLX5E_10GBASE_T): 182331805Shselasky case MLX5E_PROT_MASK(MLX5E_10GBASE_CX4): 183331805Shselasky case MLX5E_PROT_MASK(MLX5E_10GBASE_KX4): 184331805Shselasky case MLX5E_PROT_MASK(MLX5E_10GBASE_KR): 185331805Shselasky case MLX5E_PROT_MASK(MLX5E_10GBASE_CR): 186331805Shselasky case MLX5E_PROT_MASK(MLX5E_10GBASE_SR): 187331805Shselasky case MLX5E_PROT_MASK(MLX5E_10GBASE_ER): 188331805Shselasky *active_width = IB_WIDTH_1X; 189331805Shselasky *active_speed = IB_SPEED_QDR; 190331805Shselasky break; 191331805Shselasky case MLX5E_PROT_MASK(MLX5E_25GBASE_CR): 192331805Shselasky case MLX5E_PROT_MASK(MLX5E_25GBASE_KR): 193331805Shselasky case MLX5E_PROT_MASK(MLX5E_25GBASE_SR): 194331805Shselasky *active_width = IB_WIDTH_1X; 195331805Shselasky *active_speed = IB_SPEED_EDR; 196331805Shselasky break; 197331805Shselasky case MLX5E_PROT_MASK(MLX5E_40GBASE_CR4): 198331805Shselasky case MLX5E_PROT_MASK(MLX5E_40GBASE_KR4): 199331805Shselasky case MLX5E_PROT_MASK(MLX5E_40GBASE_SR4): 200331805Shselasky case MLX5E_PROT_MASK(MLX5E_40GBASE_LR4): 201331805Shselasky *active_width = IB_WIDTH_4X; 202331805Shselasky *active_speed = IB_SPEED_QDR; 203331805Shselasky break; 204331805Shselasky case MLX5E_PROT_MASK(MLX5E_50GBASE_CR2): 205331805Shselasky case MLX5E_PROT_MASK(MLX5E_50GBASE_KR2): 206331805Shselasky case MLX5E_PROT_MASK(MLX5E_50GBASE_SR2): 207331805Shselasky *active_width = IB_WIDTH_1X; 208331805Shselasky *active_speed = IB_SPEED_HDR; 209331805Shselasky break; 210331805Shselasky case MLX5E_PROT_MASK(MLX5E_56GBASE_R4): 211331805Shselasky *active_width = IB_WIDTH_4X; 212331805Shselasky *active_speed = IB_SPEED_FDR; 213331805Shselasky break; 214331805Shselasky case MLX5E_PROT_MASK(MLX5E_100GBASE_CR4): 215331805Shselasky case MLX5E_PROT_MASK(MLX5E_100GBASE_SR4): 216331805Shselasky case MLX5E_PROT_MASK(MLX5E_100GBASE_KR4): 217331805Shselasky case MLX5E_PROT_MASK(MLX5E_100GBASE_LR4): 218331805Shselasky *active_width = IB_WIDTH_4X; 219331805Shselasky *active_speed = IB_SPEED_EDR; 220331805Shselasky break; 221331805Shselasky default: 222341956Shselasky *active_width = IB_WIDTH_4X; 223341956Shselasky *active_speed = IB_SPEED_QDR; 224331805Shselasky return -EINVAL; 225331805Shselasky } 226331805Shselasky 227331805Shselasky return 0; 228331805Shselasky} 229331805Shselasky 230331769Shselaskystatic int mlx5_query_port_roce(struct ib_device *device, u8 port_num, 231331769Shselasky struct ib_port_attr *props) 232331769Shselasky{ 233322810Shselasky struct mlx5_ib_dev *dev = to_mdev(device); 234331769Shselasky struct net_device *ndev; 235331769Shselasky enum ib_mtu ndev_ib_mtu; 236331769Shselasky u16 qkey_viol_cntr; 237331805Shselasky u32 eth_prot_oper; 238331805Shselasky int err; 239322810Shselasky 240331769Shselasky memset(props, 0, sizeof(*props)); 241331769Shselasky 242331805Shselasky /* Possible bad flows are checked before filling out props so in case 243331805Shselasky * of an error it will still be zeroed out. 244331805Shselasky */ 245331805Shselasky err = mlx5_query_port_eth_proto_oper(dev->mdev, ð_prot_oper, port_num); 246331805Shselasky if (err) 247331805Shselasky return err; 248331805Shselasky 249331805Shselasky translate_eth_proto_oper(eth_prot_oper, &props->active_speed, 250331805Shselasky &props->active_width); 251331805Shselasky 252331769Shselasky props->port_cap_flags |= IB_PORT_CM_SUP; 253331769Shselasky props->port_cap_flags |= IB_PORT_IP_BASED_GIDS; 254331769Shselasky 255331769Shselasky props->gid_tbl_len = MLX5_CAP_ROCE(dev->mdev, 256331769Shselasky roce_address_table_size); 257331769Shselasky props->max_mtu = IB_MTU_4096; 258331769Shselasky props->max_msg_sz = 1 << MLX5_CAP_GEN(dev->mdev, log_max_msg); 259331769Shselasky props->pkey_tbl_len = 1; 260331769Shselasky props->state = IB_PORT_DOWN; 261331769Shselasky props->phys_state = 3; 262331769Shselasky 263331769Shselasky mlx5_query_nic_vport_qkey_viol_cntr(dev->mdev, &qkey_viol_cntr); 264331769Shselasky props->qkey_viol_cntr = qkey_viol_cntr; 265331769Shselasky 266331769Shselasky ndev = mlx5_ib_get_netdev(device, port_num); 267331769Shselasky if (!ndev) 268331769Shselasky return 0; 269331769Shselasky 270331769Shselasky if (netif_running(ndev) && netif_carrier_ok(ndev)) { 271331769Shselasky props->state = IB_PORT_ACTIVE; 272331769Shselasky props->phys_state = 5; 273331769Shselasky } 274331769Shselasky 275331769Shselasky ndev_ib_mtu = iboe_get_mtu(ndev->if_mtu); 276331769Shselasky 277331769Shselasky dev_put(ndev); 278331769Shselasky 279331769Shselasky props->active_mtu = min(props->max_mtu, ndev_ib_mtu); 280331769Shselasky return 0; 281331769Shselasky} 282331769Shselasky 283331769Shselaskystatic void ib_gid_to_mlx5_roce_addr(const union ib_gid *gid, 284331769Shselasky const struct ib_gid_attr *attr, 285331769Shselasky void *mlx5_addr) 286331769Shselasky{ 287331769Shselasky#define MLX5_SET_RA(p, f, v) MLX5_SET(roce_addr_layout, p, f, v) 288331769Shselasky char *mlx5_addr_l3_addr = MLX5_ADDR_OF(roce_addr_layout, mlx5_addr, 289331769Shselasky source_l3_address); 290331769Shselasky void *mlx5_addr_mac = MLX5_ADDR_OF(roce_addr_layout, mlx5_addr, 291331769Shselasky source_mac_47_32); 292337078Shselasky u16 vlan_id; 293331769Shselasky 294331769Shselasky if (!gid) 295331769Shselasky return; 296331769Shselasky ether_addr_copy(mlx5_addr_mac, IF_LLADDR(attr->ndev)); 297331769Shselasky 298337078Shselasky vlan_id = rdma_vlan_dev_vlan_id(attr->ndev); 299337078Shselasky if (vlan_id != 0xffff) { 300331769Shselasky MLX5_SET_RA(mlx5_addr, vlan_valid, 1); 301337078Shselasky MLX5_SET_RA(mlx5_addr, vlan_id, vlan_id); 302331769Shselasky } 303331769Shselasky 304331769Shselasky switch (attr->gid_type) { 305331769Shselasky case IB_GID_TYPE_IB: 306331769Shselasky MLX5_SET_RA(mlx5_addr, roce_version, MLX5_ROCE_VERSION_1); 307331769Shselasky break; 308331769Shselasky case IB_GID_TYPE_ROCE_UDP_ENCAP: 309331769Shselasky MLX5_SET_RA(mlx5_addr, roce_version, MLX5_ROCE_VERSION_2); 310331769Shselasky break; 311331769Shselasky 312322810Shselasky default: 313331769Shselasky WARN_ON(true); 314322810Shselasky } 315331769Shselasky 316331769Shselasky if (attr->gid_type != IB_GID_TYPE_IB) { 317331769Shselasky if (ipv6_addr_v4mapped((void *)gid)) 318331769Shselasky MLX5_SET_RA(mlx5_addr, roce_l3_type, 319331769Shselasky MLX5_ROCE_L3_TYPE_IPV4); 320331769Shselasky else 321331769Shselasky MLX5_SET_RA(mlx5_addr, roce_l3_type, 322331769Shselasky MLX5_ROCE_L3_TYPE_IPV6); 323331769Shselasky } 324331769Shselasky 325331769Shselasky if ((attr->gid_type == IB_GID_TYPE_IB) || 326331769Shselasky !ipv6_addr_v4mapped((void *)gid)) 327331769Shselasky memcpy(mlx5_addr_l3_addr, gid, sizeof(*gid)); 328331769Shselasky else 329331769Shselasky memcpy(&mlx5_addr_l3_addr[12], &gid->raw[12], 4); 330322810Shselasky} 331322810Shselasky 332331769Shselaskystatic int set_roce_addr(struct ib_device *device, u8 port_num, 333331769Shselasky unsigned int index, 334331769Shselasky const union ib_gid *gid, 335331769Shselasky const struct ib_gid_attr *attr) 336331769Shselasky{ 337331769Shselasky struct mlx5_ib_dev *dev = to_mdev(device); 338331769Shselasky u32 in[MLX5_ST_SZ_DW(set_roce_address_in)] = {0}; 339331769Shselasky u32 out[MLX5_ST_SZ_DW(set_roce_address_out)] = {0}; 340331769Shselasky void *in_addr = MLX5_ADDR_OF(set_roce_address_in, in, roce_address); 341331769Shselasky enum rdma_link_layer ll = mlx5_ib_port_link_layer(device, port_num); 342331769Shselasky 343331769Shselasky if (ll != IB_LINK_LAYER_ETHERNET) 344331769Shselasky return -EINVAL; 345331769Shselasky 346331769Shselasky ib_gid_to_mlx5_roce_addr(gid, attr, in_addr); 347331769Shselasky 348331769Shselasky MLX5_SET(set_roce_address_in, in, roce_address_index, index); 349331769Shselasky MLX5_SET(set_roce_address_in, in, opcode, MLX5_CMD_OP_SET_ROCE_ADDRESS); 350331769Shselasky return mlx5_cmd_exec(dev->mdev, in, sizeof(in), out, sizeof(out)); 351331769Shselasky} 352331769Shselasky 353331769Shselaskystatic int mlx5_ib_add_gid(struct ib_device *device, u8 port_num, 354331769Shselasky unsigned int index, const union ib_gid *gid, 355331769Shselasky const struct ib_gid_attr *attr, 356331769Shselasky __always_unused void **context) 357331769Shselasky{ 358331769Shselasky return set_roce_addr(device, port_num, index, gid, attr); 359331769Shselasky} 360331769Shselasky 361331769Shselaskystatic int mlx5_ib_del_gid(struct ib_device *device, u8 port_num, 362331769Shselasky unsigned int index, __always_unused void **context) 363331769Shselasky{ 364331769Shselasky return set_roce_addr(device, port_num, index, NULL, NULL); 365331769Shselasky} 366331769Shselasky 367331769Shselasky__be16 mlx5_get_roce_udp_sport(struct mlx5_ib_dev *dev, u8 port_num, 368331769Shselasky int index) 369331769Shselasky{ 370331769Shselasky struct ib_gid_attr attr; 371331769Shselasky union ib_gid gid; 372331769Shselasky 373331769Shselasky if (ib_get_cached_gid(&dev->ib_dev, port_num, index, &gid, &attr)) 374331769Shselasky return 0; 375331769Shselasky 376331769Shselasky if (!attr.ndev) 377331769Shselasky return 0; 378331769Shselasky 379331769Shselasky dev_put(attr.ndev); 380331769Shselasky 381331769Shselasky if (attr.gid_type != IB_GID_TYPE_ROCE_UDP_ENCAP) 382331769Shselasky return 0; 383331769Shselasky 384331769Shselasky return cpu_to_be16(MLX5_CAP_ROCE(dev->mdev, r_roce_min_src_udp_port)); 385331769Shselasky} 386331769Shselasky 387337098Shselaskyint mlx5_get_roce_gid_type(struct mlx5_ib_dev *dev, u8 port_num, 388337098Shselasky int index, enum ib_gid_type *gid_type) 389337098Shselasky{ 390337098Shselasky struct ib_gid_attr attr; 391337098Shselasky union ib_gid gid; 392337098Shselasky int ret; 393337098Shselasky 394337098Shselasky ret = ib_get_cached_gid(&dev->ib_dev, port_num, index, &gid, &attr); 395337098Shselasky if (ret) 396337098Shselasky return ret; 397337098Shselasky 398337098Shselasky if (!attr.ndev) 399337098Shselasky return -ENODEV; 400337098Shselasky 401337098Shselasky dev_put(attr.ndev); 402337098Shselasky 403337098Shselasky *gid_type = attr.gid_type; 404337098Shselasky 405337098Shselasky return 0; 406337098Shselasky} 407337098Shselasky 408322810Shselaskystatic int mlx5_use_mad_ifc(struct mlx5_ib_dev *dev) 409322810Shselasky{ 410331769Shselasky if (MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_IB) 411331769Shselasky return !MLX5_CAP_GEN(dev->mdev, ib_virt); 412331769Shselasky return 0; 413322810Shselasky} 414322810Shselasky 415322810Shselaskyenum { 416322810Shselasky MLX5_VPORT_ACCESS_METHOD_MAD, 417322810Shselasky MLX5_VPORT_ACCESS_METHOD_HCA, 418322810Shselasky MLX5_VPORT_ACCESS_METHOD_NIC, 419322810Shselasky}; 420322810Shselasky 421322810Shselaskystatic int mlx5_get_vport_access_method(struct ib_device *ibdev) 422322810Shselasky{ 423322810Shselasky if (mlx5_use_mad_ifc(to_mdev(ibdev))) 424322810Shselasky return MLX5_VPORT_ACCESS_METHOD_MAD; 425322810Shselasky 426322810Shselasky if (mlx5_ib_port_link_layer(ibdev, 1) == 427322810Shselasky IB_LINK_LAYER_ETHERNET) 428322810Shselasky return MLX5_VPORT_ACCESS_METHOD_NIC; 429322810Shselasky 430322810Shselasky return MLX5_VPORT_ACCESS_METHOD_HCA; 431322810Shselasky} 432322810Shselasky 433331769Shselaskystatic void get_atomic_caps(struct mlx5_ib_dev *dev, 434331769Shselasky struct ib_device_attr *props) 435331769Shselasky{ 436331769Shselasky u8 tmp; 437331769Shselasky u8 atomic_operations = MLX5_CAP_ATOMIC(dev->mdev, atomic_operations); 438331769Shselasky u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_qp); 439331769Shselasky u8 atomic_req_8B_endianness_mode = 440331769Shselasky MLX5_CAP_ATOMIC(dev->mdev, atomic_req_8B_endianess_mode); 441331769Shselasky 442331769Shselasky /* Check if HW supports 8 bytes standard atomic operations and capable 443331769Shselasky * of host endianness respond 444331769Shselasky */ 445331769Shselasky tmp = MLX5_ATOMIC_OPS_CMP_SWAP | MLX5_ATOMIC_OPS_FETCH_ADD; 446331769Shselasky if (((atomic_operations & tmp) == tmp) && 447331769Shselasky (atomic_size_qp & MLX5_ATOMIC_SIZE_QP_8BYTES) && 448331769Shselasky (atomic_req_8B_endianness_mode)) { 449331769Shselasky props->atomic_cap = IB_ATOMIC_HCA; 450331769Shselasky } else { 451331769Shselasky props->atomic_cap = IB_ATOMIC_NONE; 452331769Shselasky } 453331769Shselasky} 454331769Shselasky 455322810Shselaskystatic int mlx5_query_system_image_guid(struct ib_device *ibdev, 456322810Shselasky __be64 *sys_image_guid) 457322810Shselasky{ 458322810Shselasky struct mlx5_ib_dev *dev = to_mdev(ibdev); 459322810Shselasky struct mlx5_core_dev *mdev = dev->mdev; 460322810Shselasky u64 tmp; 461322810Shselasky int err; 462322810Shselasky 463322810Shselasky switch (mlx5_get_vport_access_method(ibdev)) { 464322810Shselasky case MLX5_VPORT_ACCESS_METHOD_MAD: 465331769Shselasky return mlx5_query_mad_ifc_system_image_guid(ibdev, 466322810Shselasky sys_image_guid); 467322810Shselasky 468322810Shselasky case MLX5_VPORT_ACCESS_METHOD_HCA: 469322810Shselasky err = mlx5_query_hca_vport_system_image_guid(mdev, &tmp); 470331769Shselasky break; 471322810Shselasky 472322810Shselasky case MLX5_VPORT_ACCESS_METHOD_NIC: 473322810Shselasky err = mlx5_query_nic_vport_system_image_guid(mdev, &tmp); 474331769Shselasky break; 475322810Shselasky 476322810Shselasky default: 477322810Shselasky return -EINVAL; 478322810Shselasky } 479331769Shselasky 480331769Shselasky if (!err) 481331769Shselasky *sys_image_guid = cpu_to_be64(tmp); 482331769Shselasky 483331769Shselasky return err; 484331769Shselasky 485322810Shselasky} 486322810Shselasky 487322810Shselaskystatic int mlx5_query_max_pkeys(struct ib_device *ibdev, 488322810Shselasky u16 *max_pkeys) 489322810Shselasky{ 490322810Shselasky struct mlx5_ib_dev *dev = to_mdev(ibdev); 491322810Shselasky struct mlx5_core_dev *mdev = dev->mdev; 492322810Shselasky 493322810Shselasky switch (mlx5_get_vport_access_method(ibdev)) { 494322810Shselasky case MLX5_VPORT_ACCESS_METHOD_MAD: 495331769Shselasky return mlx5_query_mad_ifc_max_pkeys(ibdev, max_pkeys); 496322810Shselasky 497322810Shselasky case MLX5_VPORT_ACCESS_METHOD_HCA: 498322810Shselasky case MLX5_VPORT_ACCESS_METHOD_NIC: 499322810Shselasky *max_pkeys = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, 500322810Shselasky pkey_table_size)); 501322810Shselasky return 0; 502322810Shselasky 503322810Shselasky default: 504322810Shselasky return -EINVAL; 505322810Shselasky } 506322810Shselasky} 507322810Shselasky 508322810Shselaskystatic int mlx5_query_vendor_id(struct ib_device *ibdev, 509322810Shselasky u32 *vendor_id) 510322810Shselasky{ 511322810Shselasky struct mlx5_ib_dev *dev = to_mdev(ibdev); 512322810Shselasky 513322810Shselasky switch (mlx5_get_vport_access_method(ibdev)) { 514322810Shselasky case MLX5_VPORT_ACCESS_METHOD_MAD: 515331769Shselasky return mlx5_query_mad_ifc_vendor_id(ibdev, vendor_id); 516322810Shselasky 517322810Shselasky case MLX5_VPORT_ACCESS_METHOD_HCA: 518322810Shselasky case MLX5_VPORT_ACCESS_METHOD_NIC: 519322810Shselasky return mlx5_core_query_vendor_id(dev->mdev, vendor_id); 520322810Shselasky 521322810Shselasky default: 522322810Shselasky return -EINVAL; 523322810Shselasky } 524322810Shselasky} 525322810Shselasky 526322810Shselaskystatic int mlx5_query_node_guid(struct mlx5_ib_dev *dev, 527322810Shselasky __be64 *node_guid) 528322810Shselasky{ 529322810Shselasky u64 tmp; 530322810Shselasky int err; 531322810Shselasky 532322810Shselasky switch (mlx5_get_vport_access_method(&dev->ib_dev)) { 533322810Shselasky case MLX5_VPORT_ACCESS_METHOD_MAD: 534331769Shselasky return mlx5_query_mad_ifc_node_guid(dev, node_guid); 535322810Shselasky 536322810Shselasky case MLX5_VPORT_ACCESS_METHOD_HCA: 537322810Shselasky err = mlx5_query_hca_vport_node_guid(dev->mdev, &tmp); 538331769Shselasky break; 539322810Shselasky 540322810Shselasky case MLX5_VPORT_ACCESS_METHOD_NIC: 541322810Shselasky err = mlx5_query_nic_vport_node_guid(dev->mdev, &tmp); 542331769Shselasky break; 543322810Shselasky 544322810Shselasky default: 545322810Shselasky return -EINVAL; 546322810Shselasky } 547331769Shselasky 548331769Shselasky if (!err) 549331769Shselasky *node_guid = cpu_to_be64(tmp); 550331769Shselasky 551331769Shselasky return err; 552322810Shselasky} 553322810Shselasky 554322810Shselaskystruct mlx5_reg_node_desc { 555331769Shselasky u8 desc[IB_DEVICE_NODE_DESC_MAX]; 556322810Shselasky}; 557322810Shselasky 558322810Shselaskystatic int mlx5_query_node_desc(struct mlx5_ib_dev *dev, char *node_desc) 559322810Shselasky{ 560322810Shselasky struct mlx5_reg_node_desc in; 561322810Shselasky 562322810Shselasky if (mlx5_use_mad_ifc(dev)) 563331769Shselasky return mlx5_query_mad_ifc_node_desc(dev, node_desc); 564322810Shselasky 565322810Shselasky memset(&in, 0, sizeof(in)); 566322810Shselasky 567322810Shselasky return mlx5_core_access_reg(dev->mdev, &in, sizeof(in), node_desc, 568322810Shselasky sizeof(struct mlx5_reg_node_desc), 569322810Shselasky MLX5_REG_NODE_DESC, 0, 0); 570322810Shselasky} 571322810Shselasky 572322810Shselaskystatic int mlx5_ib_query_device(struct ib_device *ibdev, 573331769Shselasky struct ib_device_attr *props, 574331769Shselasky struct ib_udata *uhw) 575322810Shselasky{ 576322810Shselasky struct mlx5_ib_dev *dev = to_mdev(ibdev); 577322810Shselasky struct mlx5_core_dev *mdev = dev->mdev; 578331769Shselasky int err = -ENOMEM; 579322810Shselasky int max_rq_sg; 580322810Shselasky int max_sq_sg; 581331769Shselasky u64 min_page_size = 1ull << MLX5_CAP_GEN(mdev, log_pg_sz); 582331769Shselasky struct mlx5_ib_query_device_resp resp = {}; 583331769Shselasky size_t resp_len; 584331769Shselasky u64 max_tso; 585322810Shselasky 586331769Shselasky resp_len = sizeof(resp.comp_mask) + sizeof(resp.response_length); 587331769Shselasky if (uhw->outlen && uhw->outlen < resp_len) 588331769Shselasky return -EINVAL; 589331769Shselasky else 590331769Shselasky resp.response_length = resp_len; 591322810Shselasky 592331769Shselasky if (uhw->inlen && !ib_is_udata_cleared(uhw, 0, uhw->inlen)) 593331769Shselasky return -EINVAL; 594331769Shselasky 595322810Shselasky memset(props, 0, sizeof(*props)); 596322810Shselasky err = mlx5_query_system_image_guid(ibdev, 597322810Shselasky &props->sys_image_guid); 598322810Shselasky if (err) 599322810Shselasky return err; 600322810Shselasky 601322810Shselasky err = mlx5_query_max_pkeys(ibdev, &props->max_pkeys); 602322810Shselasky if (err) 603322810Shselasky return err; 604322810Shselasky 605322810Shselasky err = mlx5_query_vendor_id(ibdev, &props->vendor_id); 606322810Shselasky if (err) 607322810Shselasky return err; 608322810Shselasky 609322810Shselasky props->fw_ver = ((u64)fw_rev_maj(dev->mdev) << 32) | 610341950Shselasky ((u32)fw_rev_min(dev->mdev) << 16) | 611322810Shselasky fw_rev_sub(dev->mdev); 612322810Shselasky props->device_cap_flags = IB_DEVICE_CHANGE_PHY_PORT | 613322810Shselasky IB_DEVICE_PORT_ACTIVE_EVENT | 614322810Shselasky IB_DEVICE_SYS_IMAGE_GUID | 615322810Shselasky IB_DEVICE_RC_RNR_NAK_GEN; 616322810Shselasky 617322810Shselasky if (MLX5_CAP_GEN(mdev, pkv)) 618322810Shselasky props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR; 619322810Shselasky if (MLX5_CAP_GEN(mdev, qkv)) 620322810Shselasky props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR; 621322810Shselasky if (MLX5_CAP_GEN(mdev, apm)) 622322810Shselasky props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG; 623322810Shselasky if (MLX5_CAP_GEN(mdev, xrc)) 624322810Shselasky props->device_cap_flags |= IB_DEVICE_XRC; 625331769Shselasky if (MLX5_CAP_GEN(mdev, imaicl)) { 626331769Shselasky props->device_cap_flags |= IB_DEVICE_MEM_WINDOW | 627331769Shselasky IB_DEVICE_MEM_WINDOW_TYPE_2B; 628331769Shselasky props->max_mw = 1 << MLX5_CAP_GEN(mdev, log_max_mkey); 629331769Shselasky /* We support 'Gappy' memory registration too */ 630331769Shselasky props->device_cap_flags |= IB_DEVICE_SG_GAPS_REG; 631331769Shselasky } 632322810Shselasky props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS; 633331769Shselasky if (MLX5_CAP_GEN(mdev, sho)) { 634331769Shselasky props->device_cap_flags |= IB_DEVICE_SIGNATURE_HANDOVER; 635331769Shselasky /* At this stage no support for signature handover */ 636331769Shselasky props->sig_prot_cap = IB_PROT_T10DIF_TYPE_1 | 637331769Shselasky IB_PROT_T10DIF_TYPE_2 | 638331769Shselasky IB_PROT_T10DIF_TYPE_3; 639331769Shselasky props->sig_guard_cap = IB_GUARD_T10DIF_CRC | 640331769Shselasky IB_GUARD_T10DIF_CSUM; 641331769Shselasky } 642322810Shselasky if (MLX5_CAP_GEN(mdev, block_lb_mc)) 643322810Shselasky props->device_cap_flags |= IB_DEVICE_BLOCK_MULTICAST_LOOPBACK; 644322810Shselasky 645331769Shselasky if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads)) { 646331769Shselasky if (MLX5_CAP_ETH(mdev, csum_cap)) 647331769Shselasky props->device_cap_flags |= IB_DEVICE_RAW_IP_CSUM; 648331769Shselasky 649331769Shselasky if (field_avail(typeof(resp), tso_caps, uhw->outlen)) { 650331769Shselasky max_tso = MLX5_CAP_ETH(mdev, max_lso_cap); 651331769Shselasky if (max_tso) { 652331769Shselasky resp.tso_caps.max_tso = 1 << max_tso; 653331769Shselasky resp.tso_caps.supported_qpts |= 654331769Shselasky 1 << IB_QPT_RAW_PACKET; 655331769Shselasky resp.response_length += sizeof(resp.tso_caps); 656331769Shselasky } 657331769Shselasky } 658331769Shselasky 659331769Shselasky if (field_avail(typeof(resp), rss_caps, uhw->outlen)) { 660331769Shselasky resp.rss_caps.rx_hash_function = 661331769Shselasky MLX5_RX_HASH_FUNC_TOEPLITZ; 662331769Shselasky resp.rss_caps.rx_hash_fields_mask = 663331769Shselasky MLX5_RX_HASH_SRC_IPV4 | 664331769Shselasky MLX5_RX_HASH_DST_IPV4 | 665331769Shselasky MLX5_RX_HASH_SRC_IPV6 | 666331769Shselasky MLX5_RX_HASH_DST_IPV6 | 667331769Shselasky MLX5_RX_HASH_SRC_PORT_TCP | 668331769Shselasky MLX5_RX_HASH_DST_PORT_TCP | 669331769Shselasky MLX5_RX_HASH_SRC_PORT_UDP | 670331769Shselasky MLX5_RX_HASH_DST_PORT_UDP; 671331769Shselasky resp.response_length += sizeof(resp.rss_caps); 672331769Shselasky } 673331769Shselasky } else { 674331769Shselasky if (field_avail(typeof(resp), tso_caps, uhw->outlen)) 675331769Shselasky resp.response_length += sizeof(resp.tso_caps); 676331769Shselasky if (field_avail(typeof(resp), rss_caps, uhw->outlen)) 677331769Shselasky resp.response_length += sizeof(resp.rss_caps); 678331769Shselasky } 679331769Shselasky 680331769Shselasky if (MLX5_CAP_GEN(mdev, ipoib_ipoib_offloads)) { 681331769Shselasky props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM; 682331769Shselasky props->device_cap_flags |= IB_DEVICE_UD_TSO; 683331769Shselasky } 684331769Shselasky 685331769Shselasky if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) && 686331769Shselasky MLX5_CAP_ETH(dev->mdev, scatter_fcs)) 687331769Shselasky props->device_cap_flags |= IB_DEVICE_RAW_SCATTER_FCS; 688331769Shselasky 689331769Shselasky if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS)) 690331769Shselasky props->device_cap_flags |= IB_DEVICE_MANAGED_FLOW_STEERING; 691331769Shselasky 692322810Shselasky props->vendor_part_id = mdev->pdev->device; 693322810Shselasky props->hw_ver = mdev->pdev->revision; 694322810Shselasky 695322810Shselasky props->max_mr_size = ~0ull; 696331769Shselasky props->page_size_cap = ~(min_page_size - 1); 697322810Shselasky props->max_qp = 1 << MLX5_CAP_GEN(mdev, log_max_qp); 698322810Shselasky props->max_qp_wr = 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz); 699322810Shselasky max_rq_sg = MLX5_CAP_GEN(mdev, max_wqe_sz_rq) / 700322810Shselasky sizeof(struct mlx5_wqe_data_seg); 701331769Shselasky max_sq_sg = (MLX5_CAP_GEN(mdev, max_wqe_sz_sq) - 702331769Shselasky sizeof(struct mlx5_wqe_ctrl_seg)) / 703331769Shselasky sizeof(struct mlx5_wqe_data_seg); 704322810Shselasky props->max_sge = min(max_rq_sg, max_sq_sg); 705331769Shselasky props->max_sge_rd = MLX5_MAX_SGE_RD; 706322810Shselasky props->max_cq = 1 << MLX5_CAP_GEN(mdev, log_max_cq); 707322810Shselasky props->max_cqe = (1 << MLX5_CAP_GEN(mdev, log_max_cq_sz)) - 1; 708322810Shselasky props->max_mr = 1 << MLX5_CAP_GEN(mdev, log_max_mkey); 709322810Shselasky props->max_pd = 1 << MLX5_CAP_GEN(mdev, log_max_pd); 710322810Shselasky props->max_qp_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_req_qp); 711322810Shselasky props->max_qp_init_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_res_qp); 712322810Shselasky props->max_srq = 1 << MLX5_CAP_GEN(mdev, log_max_srq); 713322810Shselasky props->max_srq_wr = (1 << MLX5_CAP_GEN(mdev, log_max_srq_sz)) - 1; 714322810Shselasky props->local_ca_ack_delay = MLX5_CAP_GEN(mdev, local_ca_ack_delay); 715322810Shselasky props->max_res_rd_atom = props->max_qp_rd_atom * props->max_qp; 716322810Shselasky props->max_srq_sge = max_rq_sg - 1; 717331769Shselasky props->max_fast_reg_page_list_len = 718331769Shselasky 1 << MLX5_CAP_GEN(mdev, log_max_klm_list_size); 719322810Shselasky get_atomic_caps(dev, props); 720331769Shselasky props->masked_atomic_cap = IB_ATOMIC_NONE; 721322810Shselasky props->max_mcast_grp = 1 << MLX5_CAP_GEN(mdev, log_max_mcg); 722322810Shselasky props->max_mcast_qp_attach = MLX5_CAP_GEN(mdev, max_qp_mcg); 723322810Shselasky props->max_total_mcast_qp_attach = props->max_mcast_qp_attach * 724322810Shselasky props->max_mcast_grp; 725322810Shselasky props->max_map_per_fmr = INT_MAX; /* no limit in ConnectIB */ 726331769Shselasky props->hca_core_clock = MLX5_CAP_GEN(mdev, device_frequency_khz); 727331769Shselasky props->timestamp_mask = 0x7FFFFFFFFFFFFFFFULL; 728322810Shselasky 729331769Shselasky#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING 730331769Shselasky if (MLX5_CAP_GEN(mdev, pg)) 731331769Shselasky props->device_cap_flags |= IB_DEVICE_ON_DEMAND_PAGING; 732331769Shselasky props->odp_caps = dev->odp_caps; 733331769Shselasky#endif 734331769Shselasky 735331769Shselasky if (MLX5_CAP_GEN(mdev, cd)) 736331769Shselasky props->device_cap_flags |= IB_DEVICE_CROSS_CHANNEL; 737331769Shselasky 738331769Shselasky if (!mlx5_core_is_pf(mdev)) 739331769Shselasky props->device_cap_flags |= IB_DEVICE_VIRTUAL_FUNCTION; 740331769Shselasky 741331769Shselasky if (mlx5_ib_port_link_layer(ibdev, 1) == 742331769Shselasky IB_LINK_LAYER_ETHERNET) { 743331769Shselasky props->rss_caps.max_rwq_indirection_tables = 744331769Shselasky 1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt); 745331769Shselasky props->rss_caps.max_rwq_indirection_table_size = 746331769Shselasky 1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt_size); 747331769Shselasky props->rss_caps.supported_qpts = 1 << IB_QPT_RAW_PACKET; 748331769Shselasky props->max_wq_type_rq = 749331769Shselasky 1 << MLX5_CAP_GEN(dev->mdev, log_max_rq); 750331769Shselasky } 751331769Shselasky 752331769Shselasky if (uhw->outlen) { 753331769Shselasky err = ib_copy_to_udata(uhw, &resp, resp.response_length); 754331769Shselasky 755331769Shselasky if (err) 756331769Shselasky return err; 757331769Shselasky } 758331769Shselasky 759322810Shselasky return 0; 760322810Shselasky} 761322810Shselasky 762322810Shselaskyenum mlx5_ib_width { 763322810Shselasky MLX5_IB_WIDTH_1X = 1 << 0, 764322810Shselasky MLX5_IB_WIDTH_2X = 1 << 1, 765322810Shselasky MLX5_IB_WIDTH_4X = 1 << 2, 766322810Shselasky MLX5_IB_WIDTH_8X = 1 << 3, 767322810Shselasky MLX5_IB_WIDTH_12X = 1 << 4 768322810Shselasky}; 769322810Shselasky 770322810Shselaskystatic int translate_active_width(struct ib_device *ibdev, u8 active_width, 771322810Shselasky u8 *ib_width) 772322810Shselasky{ 773322810Shselasky struct mlx5_ib_dev *dev = to_mdev(ibdev); 774322810Shselasky int err = 0; 775322810Shselasky 776322810Shselasky if (active_width & MLX5_IB_WIDTH_1X) { 777322810Shselasky *ib_width = IB_WIDTH_1X; 778322810Shselasky } else if (active_width & MLX5_IB_WIDTH_2X) { 779331769Shselasky mlx5_ib_dbg(dev, "active_width %d is not supported by IB spec\n", 780331769Shselasky (int)active_width); 781322810Shselasky err = -EINVAL; 782322810Shselasky } else if (active_width & MLX5_IB_WIDTH_4X) { 783322810Shselasky *ib_width = IB_WIDTH_4X; 784322810Shselasky } else if (active_width & MLX5_IB_WIDTH_8X) { 785322810Shselasky *ib_width = IB_WIDTH_8X; 786322810Shselasky } else if (active_width & MLX5_IB_WIDTH_12X) { 787322810Shselasky *ib_width = IB_WIDTH_12X; 788322810Shselasky } else { 789322810Shselasky mlx5_ib_dbg(dev, "Invalid active_width %d\n", 790322810Shselasky (int)active_width); 791322810Shselasky err = -EINVAL; 792322810Shselasky } 793322810Shselasky 794322810Shselasky return err; 795322810Shselasky} 796322810Shselasky 797322810Shselaskyenum ib_max_vl_num { 798322810Shselasky __IB_MAX_VL_0 = 1, 799322810Shselasky __IB_MAX_VL_0_1 = 2, 800322810Shselasky __IB_MAX_VL_0_3 = 3, 801322810Shselasky __IB_MAX_VL_0_7 = 4, 802322810Shselasky __IB_MAX_VL_0_14 = 5, 803322810Shselasky}; 804322810Shselasky 805322810Shselaskyenum mlx5_vl_hw_cap { 806322810Shselasky MLX5_VL_HW_0 = 1, 807322810Shselasky MLX5_VL_HW_0_1 = 2, 808322810Shselasky MLX5_VL_HW_0_2 = 3, 809322810Shselasky MLX5_VL_HW_0_3 = 4, 810322810Shselasky MLX5_VL_HW_0_4 = 5, 811322810Shselasky MLX5_VL_HW_0_5 = 6, 812322810Shselasky MLX5_VL_HW_0_6 = 7, 813322810Shselasky MLX5_VL_HW_0_7 = 8, 814322810Shselasky MLX5_VL_HW_0_14 = 15 815322810Shselasky}; 816322810Shselasky 817322810Shselaskystatic int translate_max_vl_num(struct ib_device *ibdev, u8 vl_hw_cap, 818322810Shselasky u8 *max_vl_num) 819322810Shselasky{ 820322810Shselasky switch (vl_hw_cap) { 821322810Shselasky case MLX5_VL_HW_0: 822322810Shselasky *max_vl_num = __IB_MAX_VL_0; 823322810Shselasky break; 824322810Shselasky case MLX5_VL_HW_0_1: 825322810Shselasky *max_vl_num = __IB_MAX_VL_0_1; 826322810Shselasky break; 827322810Shselasky case MLX5_VL_HW_0_3: 828322810Shselasky *max_vl_num = __IB_MAX_VL_0_3; 829322810Shselasky break; 830322810Shselasky case MLX5_VL_HW_0_7: 831322810Shselasky *max_vl_num = __IB_MAX_VL_0_7; 832322810Shselasky break; 833322810Shselasky case MLX5_VL_HW_0_14: 834322810Shselasky *max_vl_num = __IB_MAX_VL_0_14; 835322810Shselasky break; 836322810Shselasky 837322810Shselasky default: 838322810Shselasky return -EINVAL; 839322810Shselasky } 840322810Shselasky 841322810Shselasky return 0; 842322810Shselasky} 843322810Shselasky 844331769Shselaskystatic int mlx5_query_hca_port(struct ib_device *ibdev, u8 port, 845331769Shselasky struct ib_port_attr *props) 846322810Shselasky{ 847322810Shselasky struct mlx5_ib_dev *dev = to_mdev(ibdev); 848322810Shselasky struct mlx5_core_dev *mdev = dev->mdev; 849322810Shselasky u32 *rep; 850331769Shselasky int replen = MLX5_ST_SZ_BYTES(query_hca_vport_context_out); 851322810Shselasky struct mlx5_ptys_reg *ptys; 852322810Shselasky struct mlx5_pmtu_reg *pmtu; 853322810Shselasky struct mlx5_pvlc_reg pvlc; 854322810Shselasky void *ctx; 855322810Shselasky int err; 856322810Shselasky 857331769Shselasky rep = mlx5_vzalloc(replen); 858322810Shselasky ptys = kzalloc(sizeof(*ptys), GFP_KERNEL); 859322810Shselasky pmtu = kzalloc(sizeof(*pmtu), GFP_KERNEL); 860322810Shselasky if (!rep || !ptys || !pmtu) { 861322810Shselasky err = -ENOMEM; 862322810Shselasky goto out; 863322810Shselasky } 864322810Shselasky 865322810Shselasky memset(props, 0, sizeof(*props)); 866322810Shselasky 867331769Shselasky err = mlx5_query_hca_vport_context(mdev, port, 0, rep, replen); 868322810Shselasky if (err) 869322810Shselasky goto out; 870322810Shselasky 871322810Shselasky ctx = MLX5_ADDR_OF(query_hca_vport_context_out, rep, hca_vport_context); 872322810Shselasky 873322810Shselasky props->lid = MLX5_GET(hca_vport_context, ctx, lid); 874322810Shselasky props->lmc = MLX5_GET(hca_vport_context, ctx, lmc); 875322810Shselasky props->sm_lid = MLX5_GET(hca_vport_context, ctx, sm_lid); 876322810Shselasky props->sm_sl = MLX5_GET(hca_vport_context, ctx, sm_sl); 877322810Shselasky props->state = MLX5_GET(hca_vport_context, ctx, vport_state); 878322810Shselasky props->phys_state = MLX5_GET(hca_vport_context, ctx, 879322810Shselasky port_physical_state); 880322810Shselasky props->port_cap_flags = MLX5_GET(hca_vport_context, ctx, cap_mask1); 881322810Shselasky props->gid_tbl_len = mlx5_get_gid_table_len(MLX5_CAP_GEN(mdev, gid_table_size)); 882322810Shselasky props->max_msg_sz = 1 << MLX5_CAP_GEN(mdev, log_max_msg); 883322810Shselasky props->pkey_tbl_len = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, pkey_table_size)); 884322810Shselasky props->bad_pkey_cntr = MLX5_GET(hca_vport_context, ctx, 885331769Shselasky pkey_violation_counter); 886322810Shselasky props->qkey_viol_cntr = MLX5_GET(hca_vport_context, ctx, 887331769Shselasky qkey_violation_counter); 888322810Shselasky props->subnet_timeout = MLX5_GET(hca_vport_context, ctx, 889331769Shselasky subnet_timeout); 890322810Shselasky props->init_type_reply = MLX5_GET(hca_vport_context, ctx, 891331769Shselasky init_type_reply); 892331769Shselasky props->grh_required = MLX5_GET(hca_vport_context, ctx, grh_required); 893322810Shselasky 894322810Shselasky ptys->proto_mask |= MLX5_PTYS_IB; 895322810Shselasky ptys->local_port = port; 896322810Shselasky err = mlx5_core_access_ptys(mdev, ptys, 0); 897322810Shselasky if (err) 898322810Shselasky goto out; 899322810Shselasky 900322810Shselasky err = translate_active_width(ibdev, ptys->ib_link_width_oper, 901322810Shselasky &props->active_width); 902322810Shselasky if (err) 903322810Shselasky goto out; 904322810Shselasky 905322810Shselasky props->active_speed = (u8)ptys->ib_proto_oper; 906322810Shselasky 907322810Shselasky pmtu->local_port = port; 908322810Shselasky err = mlx5_core_access_pmtu(mdev, pmtu, 0); 909322810Shselasky if (err) 910322810Shselasky goto out; 911322810Shselasky 912322810Shselasky props->max_mtu = pmtu->max_mtu; 913322810Shselasky props->active_mtu = pmtu->oper_mtu; 914322810Shselasky 915322810Shselasky memset(&pvlc, 0, sizeof(pvlc)); 916322810Shselasky pvlc.local_port = port; 917322810Shselasky err = mlx5_core_access_pvlc(mdev, &pvlc, 0); 918322810Shselasky if (err) 919322810Shselasky goto out; 920322810Shselasky 921322810Shselasky err = translate_max_vl_num(ibdev, pvlc.vl_hw_cap, 922322810Shselasky &props->max_vl_num); 923322810Shselaskyout: 924322810Shselasky kvfree(rep); 925322810Shselasky kfree(ptys); 926322810Shselasky kfree(pmtu); 927322810Shselasky return err; 928322810Shselasky} 929322810Shselasky 930322810Shselaskyint mlx5_ib_query_port(struct ib_device *ibdev, u8 port, 931322810Shselasky struct ib_port_attr *props) 932322810Shselasky{ 933322810Shselasky switch (mlx5_get_vport_access_method(ibdev)) { 934322810Shselasky case MLX5_VPORT_ACCESS_METHOD_MAD: 935331769Shselasky return mlx5_query_mad_ifc_port(ibdev, port, props); 936322810Shselasky 937322810Shselasky case MLX5_VPORT_ACCESS_METHOD_HCA: 938331769Shselasky return mlx5_query_hca_port(ibdev, port, props); 939322810Shselasky 940322810Shselasky case MLX5_VPORT_ACCESS_METHOD_NIC: 941322810Shselasky return mlx5_query_port_roce(ibdev, port, props); 942322810Shselasky 943322810Shselasky default: 944322810Shselasky return -EINVAL; 945322810Shselasky } 946322810Shselasky} 947322810Shselasky 948322810Shselaskystatic int mlx5_ib_query_gid(struct ib_device *ibdev, u8 port, int index, 949322810Shselasky union ib_gid *gid) 950322810Shselasky{ 951322810Shselasky struct mlx5_ib_dev *dev = to_mdev(ibdev); 952322810Shselasky struct mlx5_core_dev *mdev = dev->mdev; 953322810Shselasky 954322810Shselasky switch (mlx5_get_vport_access_method(ibdev)) { 955322810Shselasky case MLX5_VPORT_ACCESS_METHOD_MAD: 956331769Shselasky return mlx5_query_mad_ifc_gids(ibdev, port, index, gid); 957322810Shselasky 958322810Shselasky case MLX5_VPORT_ACCESS_METHOD_HCA: 959322810Shselasky return mlx5_query_hca_vport_gid(mdev, port, 0, index, gid); 960322810Shselasky 961322810Shselasky default: 962322810Shselasky return -EINVAL; 963322810Shselasky } 964331769Shselasky 965322810Shselasky} 966322810Shselasky 967322810Shselaskystatic int mlx5_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index, 968322810Shselasky u16 *pkey) 969322810Shselasky{ 970322810Shselasky struct mlx5_ib_dev *dev = to_mdev(ibdev); 971322810Shselasky struct mlx5_core_dev *mdev = dev->mdev; 972322810Shselasky 973322810Shselasky switch (mlx5_get_vport_access_method(ibdev)) { 974322810Shselasky case MLX5_VPORT_ACCESS_METHOD_MAD: 975331769Shselasky return mlx5_query_mad_ifc_pkey(ibdev, port, index, pkey); 976322810Shselasky 977322810Shselasky case MLX5_VPORT_ACCESS_METHOD_HCA: 978322810Shselasky case MLX5_VPORT_ACCESS_METHOD_NIC: 979331769Shselasky return mlx5_query_hca_vport_pkey(mdev, 0, port, 0, index, 980322810Shselasky pkey); 981322810Shselasky default: 982322810Shselasky return -EINVAL; 983322810Shselasky } 984322810Shselasky} 985322810Shselasky 986322810Shselaskystatic int mlx5_ib_modify_device(struct ib_device *ibdev, int mask, 987322810Shselasky struct ib_device_modify *props) 988322810Shselasky{ 989322810Shselasky struct mlx5_ib_dev *dev = to_mdev(ibdev); 990322810Shselasky struct mlx5_reg_node_desc in; 991322810Shselasky struct mlx5_reg_node_desc out; 992322810Shselasky int err; 993322810Shselasky 994322810Shselasky if (mask & ~IB_DEVICE_MODIFY_NODE_DESC) 995322810Shselasky return -EOPNOTSUPP; 996322810Shselasky 997322810Shselasky if (!(mask & IB_DEVICE_MODIFY_NODE_DESC)) 998322810Shselasky return 0; 999322810Shselasky 1000322810Shselasky /* 1001322810Shselasky * If possible, pass node desc to FW, so it can generate 1002322810Shselasky * a 144 trap. If cmd fails, just ignore. 1003322810Shselasky */ 1004331769Shselasky memcpy(&in, props->node_desc, IB_DEVICE_NODE_DESC_MAX); 1005322810Shselasky err = mlx5_core_access_reg(dev->mdev, &in, sizeof(in), &out, 1006322810Shselasky sizeof(out), MLX5_REG_NODE_DESC, 0, 1); 1007322810Shselasky if (err) 1008322810Shselasky return err; 1009322810Shselasky 1010331769Shselasky memcpy(ibdev->node_desc, props->node_desc, IB_DEVICE_NODE_DESC_MAX); 1011322810Shselasky 1012322810Shselasky return err; 1013322810Shselasky} 1014322810Shselasky 1015322810Shselaskystatic int mlx5_ib_modify_port(struct ib_device *ibdev, u8 port, int mask, 1016322810Shselasky struct ib_port_modify *props) 1017322810Shselasky{ 1018322810Shselasky struct mlx5_ib_dev *dev = to_mdev(ibdev); 1019322810Shselasky struct ib_port_attr attr; 1020322810Shselasky u32 tmp; 1021322810Shselasky int err; 1022322810Shselasky 1023322810Shselasky mutex_lock(&dev->cap_mask_mutex); 1024322810Shselasky 1025322810Shselasky err = mlx5_ib_query_port(ibdev, port, &attr); 1026322810Shselasky if (err) 1027322810Shselasky goto out; 1028322810Shselasky 1029322810Shselasky tmp = (attr.port_cap_flags | props->set_port_cap_mask) & 1030322810Shselasky ~props->clr_port_cap_mask; 1031322810Shselasky 1032322810Shselasky err = mlx5_set_port_caps(dev->mdev, port, tmp); 1033322810Shselasky 1034322810Shselaskyout: 1035322810Shselasky mutex_unlock(&dev->cap_mask_mutex); 1036322810Shselasky return err; 1037322810Shselasky} 1038322810Shselasky 1039322810Shselaskystatic struct ib_ucontext *mlx5_ib_alloc_ucontext(struct ib_device *ibdev, 1040322810Shselasky struct ib_udata *udata) 1041322810Shselasky{ 1042322810Shselasky struct mlx5_ib_dev *dev = to_mdev(ibdev); 1043331769Shselasky struct mlx5_ib_alloc_ucontext_req_v2 req = {}; 1044331769Shselasky struct mlx5_ib_alloc_ucontext_resp resp = {}; 1045322810Shselasky struct mlx5_ib_ucontext *context; 1046322810Shselasky struct mlx5_uuar_info *uuari; 1047322810Shselasky struct mlx5_uar *uars; 1048322810Shselasky int gross_uuars; 1049322810Shselasky int num_uars; 1050322810Shselasky int ver; 1051322810Shselasky int uuarn; 1052322810Shselasky int err; 1053322810Shselasky int i; 1054322810Shselasky size_t reqlen; 1055331769Shselasky size_t min_req_v2 = offsetof(struct mlx5_ib_alloc_ucontext_req_v2, 1056331769Shselasky max_cqe_version); 1057322810Shselasky 1058322810Shselasky if (!dev->ib_active) 1059322810Shselasky return ERR_PTR(-EAGAIN); 1060322810Shselasky 1061331769Shselasky if (udata->inlen < sizeof(struct ib_uverbs_cmd_hdr)) 1062331769Shselasky return ERR_PTR(-EINVAL); 1063322810Shselasky 1064322810Shselasky reqlen = udata->inlen - sizeof(struct ib_uverbs_cmd_hdr); 1065322810Shselasky if (reqlen == sizeof(struct mlx5_ib_alloc_ucontext_req)) 1066322810Shselasky ver = 0; 1067331769Shselasky else if (reqlen >= min_req_v2) 1068322810Shselasky ver = 2; 1069331769Shselasky else 1070322810Shselasky return ERR_PTR(-EINVAL); 1071322810Shselasky 1072331769Shselasky err = ib_copy_from_udata(&req, udata, min(reqlen, sizeof(req))); 1073331769Shselasky if (err) 1074322810Shselasky return ERR_PTR(err); 1075322810Shselasky 1076331769Shselasky if (req.flags) 1077322810Shselasky return ERR_PTR(-EINVAL); 1078322810Shselasky 1079331769Shselasky if (req.total_num_uuars > MLX5_MAX_UUARS) 1080322810Shselasky return ERR_PTR(-ENOMEM); 1081322810Shselasky 1082331769Shselasky if (req.total_num_uuars == 0) 1083331769Shselasky return ERR_PTR(-EINVAL); 1084331769Shselasky 1085331769Shselasky if (req.comp_mask || req.reserved0 || req.reserved1 || req.reserved2) 1086331769Shselasky return ERR_PTR(-EOPNOTSUPP); 1087331769Shselasky 1088331769Shselasky if (reqlen > sizeof(req) && 1089331769Shselasky !ib_is_udata_cleared(udata, sizeof(req), 1090331769Shselasky reqlen - sizeof(req))) 1091331769Shselasky return ERR_PTR(-EOPNOTSUPP); 1092331769Shselasky 1093322810Shselasky req.total_num_uuars = ALIGN(req.total_num_uuars, 1094322810Shselasky MLX5_NON_FP_BF_REGS_PER_PAGE); 1095331769Shselasky if (req.num_low_latency_uuars > req.total_num_uuars - 1) 1096322810Shselasky return ERR_PTR(-EINVAL); 1097322810Shselasky 1098322810Shselasky num_uars = req.total_num_uuars / MLX5_NON_FP_BF_REGS_PER_PAGE; 1099322810Shselasky gross_uuars = num_uars * MLX5_BF_REGS_PER_PAGE; 1100322810Shselasky resp.qp_tab_size = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp); 1101322810Shselasky if (mlx5_core_is_pf(dev->mdev) && MLX5_CAP_GEN(dev->mdev, bf)) 1102322810Shselasky resp.bf_reg_size = 1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size); 1103331769Shselasky resp.cache_line_size = cache_line_size(); 1104322810Shselasky resp.max_sq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq); 1105322810Shselasky resp.max_rq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq); 1106322810Shselasky resp.max_send_wqebb = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz); 1107322810Shselasky resp.max_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz); 1108322810Shselasky resp.max_srq_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz); 1109331769Shselasky resp.cqe_version = min_t(__u8, 1110331769Shselasky (__u8)MLX5_CAP_GEN(dev->mdev, cqe_version), 1111331769Shselasky req.max_cqe_version); 1112331769Shselasky resp.response_length = min(offsetof(typeof(resp), response_length) + 1113331769Shselasky sizeof(resp.response_length), udata->outlen); 1114322810Shselasky 1115322810Shselasky context = kzalloc(sizeof(*context), GFP_KERNEL); 1116322810Shselasky if (!context) 1117322810Shselasky return ERR_PTR(-ENOMEM); 1118322810Shselasky 1119322810Shselasky uuari = &context->uuari; 1120322810Shselasky mutex_init(&uuari->lock); 1121322810Shselasky uars = kcalloc(num_uars, sizeof(*uars), GFP_KERNEL); 1122322810Shselasky if (!uars) { 1123322810Shselasky err = -ENOMEM; 1124322810Shselasky goto out_ctx; 1125322810Shselasky } 1126322810Shselasky 1127322810Shselasky uuari->bitmap = kcalloc(BITS_TO_LONGS(gross_uuars), 1128322810Shselasky sizeof(*uuari->bitmap), 1129322810Shselasky GFP_KERNEL); 1130322810Shselasky if (!uuari->bitmap) { 1131322810Shselasky err = -ENOMEM; 1132322810Shselasky goto out_uar_ctx; 1133322810Shselasky } 1134322810Shselasky /* 1135322810Shselasky * clear all fast path uuars 1136322810Shselasky */ 1137322810Shselasky for (i = 0; i < gross_uuars; i++) { 1138322810Shselasky uuarn = i & 3; 1139322810Shselasky if (uuarn == 2 || uuarn == 3) 1140322810Shselasky set_bit(i, uuari->bitmap); 1141322810Shselasky } 1142322810Shselasky 1143322810Shselasky uuari->count = kcalloc(gross_uuars, sizeof(*uuari->count), GFP_KERNEL); 1144322810Shselasky if (!uuari->count) { 1145322810Shselasky err = -ENOMEM; 1146322810Shselasky goto out_bitmap; 1147322810Shselasky } 1148322810Shselasky 1149322810Shselasky for (i = 0; i < num_uars; i++) { 1150322810Shselasky err = mlx5_cmd_alloc_uar(dev->mdev, &uars[i].index); 1151331769Shselasky if (err) 1152331769Shselasky goto out_count; 1153331769Shselasky } 1154331769Shselasky 1155331769Shselasky#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING 1156331769Shselasky context->ibucontext.invalidate_range = &mlx5_ib_invalidate_range; 1157331769Shselasky#endif 1158331769Shselasky 1159331769Shselasky if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain)) { 1160331769Shselasky err = mlx5_alloc_transport_domain(dev->mdev, 1161331769Shselasky &context->tdn); 1162331769Shselasky if (err) 1163322810Shselasky goto out_uars; 1164322810Shselasky } 1165322810Shselasky 1166331769Shselasky INIT_LIST_HEAD(&context->vma_private_list); 1167322810Shselasky INIT_LIST_HEAD(&context->db_page_list); 1168322810Shselasky mutex_init(&context->db_page_mutex); 1169322810Shselasky 1170322810Shselasky resp.tot_uuars = req.total_num_uuars; 1171322810Shselasky resp.num_ports = MLX5_CAP_GEN(dev->mdev, num_ports); 1172331769Shselasky 1173331769Shselasky if (field_avail(typeof(resp), cqe_version, udata->outlen)) 1174331769Shselasky resp.response_length += sizeof(resp.cqe_version); 1175331769Shselasky 1176331769Shselasky if (field_avail(typeof(resp), cmds_supp_uhw, udata->outlen)) { 1177331784Shselasky resp.cmds_supp_uhw |= MLX5_USER_CMDS_SUPP_UHW_QUERY_DEVICE | 1178331784Shselasky MLX5_USER_CMDS_SUPP_UHW_CREATE_AH; 1179331769Shselasky resp.response_length += sizeof(resp.cmds_supp_uhw); 1180331769Shselasky } 1181331769Shselasky 1182331769Shselasky /* 1183331769Shselasky * We don't want to expose information from the PCI bar that is located 1184331769Shselasky * after 4096 bytes, so if the arch only supports larger pages, let's 1185331769Shselasky * pretend we don't support reading the HCA's core clock. This is also 1186331769Shselasky * forced by mmap function. 1187331769Shselasky */ 1188331769Shselasky if (PAGE_SIZE <= 4096 && 1189331769Shselasky field_avail(typeof(resp), hca_core_clock_offset, udata->outlen)) { 1190331769Shselasky resp.comp_mask |= 1191331769Shselasky MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_CORE_CLOCK_OFFSET; 1192331769Shselasky resp.hca_core_clock_offset = 1193331769Shselasky offsetof(struct mlx5_init_seg, internal_timer_h) % 1194331769Shselasky PAGE_SIZE; 1195331769Shselasky resp.response_length += sizeof(resp.hca_core_clock_offset) + 1196331769Shselasky sizeof(resp.reserved2); 1197331769Shselasky } 1198331769Shselasky 1199331769Shselasky err = ib_copy_to_udata(udata, &resp, resp.response_length); 1200322810Shselasky if (err) 1201331769Shselasky goto out_td; 1202322810Shselasky 1203322810Shselasky uuari->ver = ver; 1204322810Shselasky uuari->num_low_latency_uuars = req.num_low_latency_uuars; 1205322810Shselasky uuari->uars = uars; 1206322810Shselasky uuari->num_uars = num_uars; 1207331769Shselasky context->cqe_version = resp.cqe_version; 1208322810Shselasky 1209322810Shselasky return &context->ibucontext; 1210322810Shselasky 1211331769Shselaskyout_td: 1212331769Shselasky if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain)) 1213331769Shselasky mlx5_dealloc_transport_domain(dev->mdev, context->tdn); 1214331769Shselasky 1215322810Shselaskyout_uars: 1216322810Shselasky for (i--; i >= 0; i--) 1217322810Shselasky mlx5_cmd_free_uar(dev->mdev, uars[i].index); 1218331769Shselaskyout_count: 1219322810Shselasky kfree(uuari->count); 1220322810Shselasky 1221322810Shselaskyout_bitmap: 1222322810Shselasky kfree(uuari->bitmap); 1223322810Shselasky 1224322810Shselaskyout_uar_ctx: 1225322810Shselasky kfree(uars); 1226322810Shselasky 1227322810Shselaskyout_ctx: 1228322810Shselasky kfree(context); 1229322810Shselasky return ERR_PTR(err); 1230322810Shselasky} 1231322810Shselasky 1232322810Shselaskystatic int mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext) 1233322810Shselasky{ 1234322810Shselasky struct mlx5_ib_ucontext *context = to_mucontext(ibcontext); 1235322810Shselasky struct mlx5_ib_dev *dev = to_mdev(ibcontext->device); 1236322810Shselasky struct mlx5_uuar_info *uuari = &context->uuari; 1237322810Shselasky int i; 1238322810Shselasky 1239331769Shselasky if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain)) 1240322810Shselasky mlx5_dealloc_transport_domain(dev->mdev, context->tdn); 1241322810Shselasky 1242322810Shselasky for (i = 0; i < uuari->num_uars; i++) { 1243322810Shselasky if (mlx5_cmd_free_uar(dev->mdev, uuari->uars[i].index)) 1244322810Shselasky mlx5_ib_warn(dev, "failed to free UAR 0x%x\n", uuari->uars[i].index); 1245322810Shselasky } 1246322810Shselasky 1247322810Shselasky kfree(uuari->count); 1248322810Shselasky kfree(uuari->bitmap); 1249322810Shselasky kfree(uuari->uars); 1250322810Shselasky kfree(context); 1251322810Shselasky 1252322810Shselasky return 0; 1253322810Shselasky} 1254322810Shselasky 1255322810Shselaskystatic phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev, int index) 1256322810Shselasky{ 1257322810Shselasky return (pci_resource_start(dev->mdev->pdev, 0) >> PAGE_SHIFT) + index; 1258322810Shselasky} 1259322810Shselasky 1260322810Shselaskystatic int get_command(unsigned long offset) 1261322810Shselasky{ 1262322810Shselasky return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK; 1263322810Shselasky} 1264322810Shselasky 1265322810Shselaskystatic int get_arg(unsigned long offset) 1266322810Shselasky{ 1267322810Shselasky return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1); 1268322810Shselasky} 1269322810Shselasky 1270322810Shselaskystatic int get_index(unsigned long offset) 1271322810Shselasky{ 1272322810Shselasky return get_arg(offset); 1273322810Shselasky} 1274322810Shselasky 1275331769Shselaskystatic void mlx5_ib_vma_open(struct vm_area_struct *area) 1276331769Shselasky{ 1277331769Shselasky /* vma_open is called when a new VMA is created on top of our VMA. This 1278331769Shselasky * is done through either mremap flow or split_vma (usually due to 1279331769Shselasky * mlock, madvise, munmap, etc.) We do not support a clone of the VMA, 1280331769Shselasky * as this VMA is strongly hardware related. Therefore we set the 1281331769Shselasky * vm_ops of the newly created/cloned VMA to NULL, to prevent it from 1282331769Shselasky * calling us again and trying to do incorrect actions. We assume that 1283331769Shselasky * the original VMA size is exactly a single page, and therefore all 1284331769Shselasky * "splitting" operation will not happen to it. 1285331769Shselasky */ 1286331769Shselasky area->vm_ops = NULL; 1287331769Shselasky} 1288331769Shselasky 1289331769Shselaskystatic void mlx5_ib_vma_close(struct vm_area_struct *area) 1290331769Shselasky{ 1291331769Shselasky struct mlx5_ib_vma_private_data *mlx5_ib_vma_priv_data; 1292331769Shselasky 1293331769Shselasky /* It's guaranteed that all VMAs opened on a FD are closed before the 1294331769Shselasky * file itself is closed, therefore no sync is needed with the regular 1295331769Shselasky * closing flow. (e.g. mlx5 ib_dealloc_ucontext) 1296331769Shselasky * However need a sync with accessing the vma as part of 1297331769Shselasky * mlx5_ib_disassociate_ucontext. 1298331769Shselasky * The close operation is usually called under mm->mmap_sem except when 1299331769Shselasky * process is exiting. 1300331769Shselasky * The exiting case is handled explicitly as part of 1301331769Shselasky * mlx5_ib_disassociate_ucontext. 1302331769Shselasky */ 1303331769Shselasky mlx5_ib_vma_priv_data = (struct mlx5_ib_vma_private_data *)area->vm_private_data; 1304331769Shselasky 1305331769Shselasky /* setting the vma context pointer to null in the mlx5_ib driver's 1306331769Shselasky * private data, to protect a race condition in 1307331769Shselasky * mlx5_ib_disassociate_ucontext(). 1308331769Shselasky */ 1309331769Shselasky mlx5_ib_vma_priv_data->vma = NULL; 1310331769Shselasky list_del(&mlx5_ib_vma_priv_data->list); 1311331769Shselasky kfree(mlx5_ib_vma_priv_data); 1312331769Shselasky} 1313331769Shselasky 1314331769Shselaskystatic const struct vm_operations_struct mlx5_ib_vm_ops = { 1315331769Shselasky .open = mlx5_ib_vma_open, 1316331769Shselasky .close = mlx5_ib_vma_close 1317331769Shselasky}; 1318331769Shselasky 1319331769Shselaskystatic int mlx5_ib_set_vma_data(struct vm_area_struct *vma, 1320331769Shselasky struct mlx5_ib_ucontext *ctx) 1321331769Shselasky{ 1322331769Shselasky struct mlx5_ib_vma_private_data *vma_prv; 1323331769Shselasky struct list_head *vma_head = &ctx->vma_private_list; 1324331769Shselasky 1325331769Shselasky vma_prv = kzalloc(sizeof(*vma_prv), GFP_KERNEL); 1326331769Shselasky if (!vma_prv) 1327331769Shselasky return -ENOMEM; 1328331769Shselasky 1329331769Shselasky vma_prv->vma = vma; 1330331769Shselasky vma->vm_private_data = vma_prv; 1331331769Shselasky vma->vm_ops = &mlx5_ib_vm_ops; 1332331769Shselasky 1333331769Shselasky list_add(&vma_prv->list, vma_head); 1334331769Shselasky 1335331769Shselasky return 0; 1336331769Shselasky} 1337331769Shselasky 1338347801Shselaskystatic void mlx5_ib_disassociate_ucontext(struct ib_ucontext *ibcontext) 1339347801Shselasky{ 1340347801Shselasky int ret; 1341347801Shselasky struct vm_area_struct *vma; 1342347801Shselasky struct mlx5_ib_vma_private_data *vma_private, *n; 1343347801Shselasky struct mlx5_ib_ucontext *context = to_mucontext(ibcontext); 1344347801Shselasky struct task_struct *owning_process = NULL; 1345347801Shselasky struct mm_struct *owning_mm = NULL; 1346347801Shselasky 1347347801Shselasky owning_process = get_pid_task(ibcontext->tgid, PIDTYPE_PID); 1348347801Shselasky if (!owning_process) 1349347801Shselasky return; 1350347801Shselasky 1351347801Shselasky owning_mm = get_task_mm(owning_process); 1352347801Shselasky if (!owning_mm) { 1353347801Shselasky pr_info("no mm, disassociate ucontext is pending task termination\n"); 1354347801Shselasky while (1) { 1355347801Shselasky put_task_struct(owning_process); 1356347801Shselasky usleep_range(1000, 2000); 1357347801Shselasky owning_process = get_pid_task(ibcontext->tgid, 1358347801Shselasky PIDTYPE_PID); 1359347801Shselasky if (!owning_process || owning_process->task_thread-> 1360347801Shselasky td_proc->p_state == PRS_ZOMBIE) { 1361347801Shselasky pr_info("disassociate ucontext done, task was terminated\n"); 1362347801Shselasky /* in case task was dead need to release the 1363347801Shselasky * task struct. 1364347801Shselasky */ 1365347801Shselasky if (owning_process) 1366347801Shselasky put_task_struct(owning_process); 1367347801Shselasky return; 1368347801Shselasky } 1369347801Shselasky } 1370347801Shselasky } 1371347801Shselasky 1372347801Shselasky /* need to protect from a race on closing the vma as part of 1373347801Shselasky * mlx5_ib_vma_close. 1374347801Shselasky */ 1375347801Shselasky down_write(&owning_mm->mmap_sem); 1376347801Shselasky list_for_each_entry_safe(vma_private, n, &context->vma_private_list, 1377347801Shselasky list) { 1378347801Shselasky vma = vma_private->vma; 1379347801Shselasky ret = zap_vma_ptes(vma, vma->vm_start, 1380347801Shselasky PAGE_SIZE); 1381347801Shselasky if (ret == -ENOTSUP) { 1382347801Shselasky if (bootverbose) 1383347801Shselasky WARN_ONCE( 1384347801Shselasky "%s: zap_vma_ptes not implemented for unmanaged mappings", __func__); 1385347801Shselasky } else { 1386347801Shselasky WARN(ret, "%s: zap_vma_ptes failed, error %d", 1387347801Shselasky __func__, -ret); 1388347801Shselasky } 1389347801Shselasky /* context going to be destroyed, should 1390347801Shselasky * not access ops any more. 1391347801Shselasky */ 1392347801Shselasky /* XXXKIB vma->vm_flags &= ~(VM_SHARED | VM_MAYSHARE); */ 1393347801Shselasky vma->vm_ops = NULL; 1394347801Shselasky list_del(&vma_private->list); 1395347801Shselasky kfree(vma_private); 1396347801Shselasky } 1397347801Shselasky up_write(&owning_mm->mmap_sem); 1398347801Shselasky mmput(owning_mm); 1399347801Shselasky put_task_struct(owning_process); 1400347801Shselasky} 1401347801Shselasky 1402331769Shselaskystatic inline char *mmap_cmd2str(enum mlx5_ib_mmap_cmd cmd) 1403331769Shselasky{ 1404331769Shselasky switch (cmd) { 1405331769Shselasky case MLX5_IB_MMAP_WC_PAGE: 1406331769Shselasky return "WC"; 1407331769Shselasky case MLX5_IB_MMAP_REGULAR_PAGE: 1408331769Shselasky return "best effort WC"; 1409331769Shselasky case MLX5_IB_MMAP_NC_PAGE: 1410331769Shselasky return "NC"; 1411331769Shselasky default: 1412331769Shselasky return NULL; 1413331769Shselasky } 1414331769Shselasky} 1415331769Shselasky 1416331769Shselaskystatic int uar_mmap(struct mlx5_ib_dev *dev, enum mlx5_ib_mmap_cmd cmd, 1417331769Shselasky struct vm_area_struct *vma, 1418322810Shselasky struct mlx5_ib_ucontext *context) 1419322810Shselasky{ 1420331769Shselasky struct mlx5_uuar_info *uuari = &context->uuari; 1421331769Shselasky int err; 1422322810Shselasky unsigned long idx; 1423331769Shselasky phys_addr_t pfn, pa; 1424331769Shselasky pgprot_t prot; 1425322810Shselasky 1426331769Shselasky switch (cmd) { 1427331769Shselasky case MLX5_IB_MMAP_WC_PAGE: 1428331769Shselasky/* Some architectures don't support WC memory */ 1429331769Shselasky#if defined(CONFIG_X86) 1430331769Shselasky if (!pat_enabled()) 1431331769Shselasky return -EPERM; 1432331769Shselasky#elif !(defined(CONFIG_PPC) || (defined(CONFIG_ARM) && defined(CONFIG_MMU))) 1433331769Shselasky return -EPERM; 1434331769Shselasky#endif 1435331769Shselasky /* fall through */ 1436331769Shselasky case MLX5_IB_MMAP_REGULAR_PAGE: 1437331769Shselasky /* For MLX5_IB_MMAP_REGULAR_PAGE do the best effort to get WC */ 1438331769Shselasky prot = pgprot_writecombine(vma->vm_page_prot); 1439331769Shselasky break; 1440331769Shselasky case MLX5_IB_MMAP_NC_PAGE: 1441331769Shselasky prot = pgprot_noncached(vma->vm_page_prot); 1442331769Shselasky break; 1443331769Shselasky default: 1444322810Shselasky return -EINVAL; 1445322810Shselasky } 1446322810Shselasky 1447331769Shselasky if (vma->vm_end - vma->vm_start != PAGE_SIZE) 1448331769Shselasky return -EINVAL; 1449331769Shselasky 1450322810Shselasky idx = get_index(vma->vm_pgoff); 1451331769Shselasky if (idx >= uuari->num_uars) 1452322810Shselasky return -EINVAL; 1453322810Shselasky 1454322810Shselasky pfn = uar_index2pfn(dev, uuari->uars[idx].index); 1455331769Shselasky mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn %pa\n", idx, &pfn); 1456322810Shselasky 1457322810Shselasky vma->vm_page_prot = prot; 1458331769Shselasky err = io_remap_pfn_range(vma, vma->vm_start, pfn, 1459331769Shselasky PAGE_SIZE, vma->vm_page_prot); 1460331769Shselasky if (err) { 1461331769Shselasky mlx5_ib_err(dev, "io_remap_pfn_range failed with error=%d, vm_start=0x%llx, pfn=%pa, mmap_cmd=%s\n", 1462331769Shselasky err, (unsigned long long)vma->vm_start, &pfn, mmap_cmd2str(cmd)); 1463322810Shselasky return -EAGAIN; 1464322810Shselasky } 1465322810Shselasky 1466331769Shselasky pa = pfn << PAGE_SHIFT; 1467331769Shselasky mlx5_ib_dbg(dev, "mapped %s at 0x%llx, PA %pa\n", mmap_cmd2str(cmd), 1468331769Shselasky (unsigned long long)vma->vm_start, &pa); 1469322810Shselasky 1470331769Shselasky return mlx5_ib_set_vma_data(vma, context); 1471322810Shselasky} 1472322810Shselasky 1473322810Shselaskystatic int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma) 1474322810Shselasky{ 1475322810Shselasky struct mlx5_ib_ucontext *context = to_mucontext(ibcontext); 1476322810Shselasky struct mlx5_ib_dev *dev = to_mdev(ibcontext->device); 1477322810Shselasky unsigned long command; 1478331769Shselasky phys_addr_t pfn; 1479322810Shselasky 1480322810Shselasky command = get_command(vma->vm_pgoff); 1481322810Shselasky switch (command) { 1482331769Shselasky case MLX5_IB_MMAP_WC_PAGE: 1483331769Shselasky case MLX5_IB_MMAP_NC_PAGE: 1484322810Shselasky case MLX5_IB_MMAP_REGULAR_PAGE: 1485331769Shselasky return uar_mmap(dev, command, vma, context); 1486322810Shselasky 1487331769Shselasky case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES: 1488331769Shselasky return -ENOSYS; 1489322810Shselasky 1490331769Shselasky case MLX5_IB_MMAP_CORE_CLOCK: 1491331769Shselasky if (vma->vm_end - vma->vm_start != PAGE_SIZE) 1492331769Shselasky return -EINVAL; 1493322810Shselasky 1494331769Shselasky if (vma->vm_flags & VM_WRITE) 1495331769Shselasky return -EPERM; 1496331769Shselasky 1497331769Shselasky /* Don't expose to user-space information it shouldn't have */ 1498331769Shselasky if (PAGE_SIZE > 4096) 1499331769Shselasky return -EOPNOTSUPP; 1500331769Shselasky 1501331769Shselasky vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); 1502331769Shselasky pfn = (dev->mdev->iseg_base + 1503331769Shselasky offsetof(struct mlx5_init_seg, internal_timer_h)) >> 1504331769Shselasky PAGE_SHIFT; 1505331769Shselasky if (io_remap_pfn_range(vma, vma->vm_start, pfn, 1506331769Shselasky PAGE_SIZE, vma->vm_page_prot)) 1507331769Shselasky return -EAGAIN; 1508331769Shselasky 1509331769Shselasky mlx5_ib_dbg(dev, "mapped internal timer at 0x%llx, PA 0x%llx\n", 1510331769Shselasky (unsigned long long)vma->vm_start, 1511331769Shselasky (unsigned long long)pfn << PAGE_SHIFT); 1512322810Shselasky break; 1513322810Shselasky 1514322810Shselasky default: 1515322810Shselasky return -EINVAL; 1516322810Shselasky } 1517322810Shselasky 1518322810Shselasky return 0; 1519322810Shselasky} 1520322810Shselasky 1521322810Shselaskystatic struct ib_pd *mlx5_ib_alloc_pd(struct ib_device *ibdev, 1522322810Shselasky struct ib_ucontext *context, 1523322810Shselasky struct ib_udata *udata) 1524322810Shselasky{ 1525322810Shselasky struct mlx5_ib_alloc_pd_resp resp; 1526322810Shselasky struct mlx5_ib_pd *pd; 1527322810Shselasky int err; 1528322810Shselasky 1529322810Shselasky pd = kmalloc(sizeof(*pd), GFP_KERNEL); 1530322810Shselasky if (!pd) 1531322810Shselasky return ERR_PTR(-ENOMEM); 1532322810Shselasky 1533322810Shselasky err = mlx5_core_alloc_pd(to_mdev(ibdev)->mdev, &pd->pdn); 1534322810Shselasky if (err) { 1535322810Shselasky kfree(pd); 1536322810Shselasky return ERR_PTR(err); 1537322810Shselasky } 1538322810Shselasky 1539322810Shselasky if (context) { 1540322810Shselasky resp.pdn = pd->pdn; 1541322810Shselasky if (ib_copy_to_udata(udata, &resp, sizeof(resp))) { 1542322810Shselasky mlx5_core_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn); 1543322810Shselasky kfree(pd); 1544322810Shselasky return ERR_PTR(-EFAULT); 1545322810Shselasky } 1546322810Shselasky } 1547322810Shselasky 1548322810Shselasky return &pd->ibpd; 1549322810Shselasky} 1550322810Shselasky 1551322810Shselaskystatic int mlx5_ib_dealloc_pd(struct ib_pd *pd) 1552322810Shselasky{ 1553322810Shselasky struct mlx5_ib_dev *mdev = to_mdev(pd->device); 1554322810Shselasky struct mlx5_ib_pd *mpd = to_mpd(pd); 1555322810Shselasky 1556322810Shselasky mlx5_core_dealloc_pd(mdev->mdev, mpd->pdn); 1557322810Shselasky kfree(mpd); 1558322810Shselasky 1559322810Shselasky return 0; 1560322810Shselasky} 1561322810Shselasky 1562331769Shselaskyenum { 1563331769Shselasky MATCH_CRITERIA_ENABLE_OUTER_BIT, 1564331769Shselasky MATCH_CRITERIA_ENABLE_MISC_BIT, 1565331769Shselasky MATCH_CRITERIA_ENABLE_INNER_BIT 1566331769Shselasky}; 1567331769Shselasky 1568331769Shselasky#define HEADER_IS_ZERO(match_criteria, headers) \ 1569331769Shselasky !(memchr_inv(MLX5_ADDR_OF(fte_match_param, match_criteria, headers), \ 1570331769Shselasky 0, MLX5_FLD_SZ_BYTES(fte_match_param, headers))) \ 1571331769Shselasky 1572331769Shselaskystatic u8 get_match_criteria_enable(u32 *match_criteria) 1573331769Shselasky{ 1574331769Shselasky u8 match_criteria_enable; 1575331769Shselasky 1576331769Shselasky match_criteria_enable = 1577331769Shselasky (!HEADER_IS_ZERO(match_criteria, outer_headers)) << 1578331769Shselasky MATCH_CRITERIA_ENABLE_OUTER_BIT; 1579331769Shselasky match_criteria_enable |= 1580331769Shselasky (!HEADER_IS_ZERO(match_criteria, misc_parameters)) << 1581331769Shselasky MATCH_CRITERIA_ENABLE_MISC_BIT; 1582331769Shselasky match_criteria_enable |= 1583331769Shselasky (!HEADER_IS_ZERO(match_criteria, inner_headers)) << 1584331769Shselasky MATCH_CRITERIA_ENABLE_INNER_BIT; 1585331769Shselasky 1586331769Shselasky return match_criteria_enable; 1587331769Shselasky} 1588331769Shselasky 1589331769Shselaskystatic void set_proto(void *outer_c, void *outer_v, u8 mask, u8 val) 1590331769Shselasky{ 1591331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_protocol, mask); 1592331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_protocol, val); 1593331769Shselasky} 1594331769Shselasky 1595331769Shselaskystatic void set_tos(void *outer_c, void *outer_v, u8 mask, u8 val) 1596331769Shselasky{ 1597331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_ecn, mask); 1598331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_ecn, val); 1599331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_dscp, mask >> 2); 1600331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_dscp, val >> 2); 1601331769Shselasky} 1602331769Shselasky 1603331769Shselasky#define LAST_ETH_FIELD vlan_tag 1604331769Shselasky#define LAST_IB_FIELD sl 1605331769Shselasky#define LAST_IPV4_FIELD tos 1606331769Shselasky#define LAST_IPV6_FIELD traffic_class 1607331769Shselasky#define LAST_TCP_UDP_FIELD src_port 1608331769Shselasky 1609331769Shselasky/* Field is the last supported field */ 1610331769Shselasky#define FIELDS_NOT_SUPPORTED(filter, field)\ 1611331769Shselasky memchr_inv((void *)&filter.field +\ 1612331769Shselasky sizeof(filter.field), 0,\ 1613331769Shselasky sizeof(filter) -\ 1614331769Shselasky offsetof(typeof(filter), field) -\ 1615331769Shselasky sizeof(filter.field)) 1616331769Shselasky 1617331769Shselaskystatic int parse_flow_attr(u32 *match_c, u32 *match_v, 1618331769Shselasky const union ib_flow_spec *ib_spec) 1619331769Shselasky{ 1620331769Shselasky void *outer_headers_c = MLX5_ADDR_OF(fte_match_param, match_c, 1621331769Shselasky outer_headers); 1622331769Shselasky void *outer_headers_v = MLX5_ADDR_OF(fte_match_param, match_v, 1623331769Shselasky outer_headers); 1624331769Shselasky void *misc_params_c = MLX5_ADDR_OF(fte_match_param, match_c, 1625331769Shselasky misc_parameters); 1626331769Shselasky void *misc_params_v = MLX5_ADDR_OF(fte_match_param, match_v, 1627331769Shselasky misc_parameters); 1628331769Shselasky 1629331769Shselasky switch (ib_spec->type) { 1630331769Shselasky case IB_FLOW_SPEC_ETH: 1631331769Shselasky if (FIELDS_NOT_SUPPORTED(ib_spec->eth.mask, LAST_ETH_FIELD)) 1632331769Shselasky return -ENOTSUPP; 1633331769Shselasky 1634331769Shselasky ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_c, 1635331769Shselasky dmac_47_16), 1636331769Shselasky ib_spec->eth.mask.dst_mac); 1637331769Shselasky ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_v, 1638331769Shselasky dmac_47_16), 1639331769Shselasky ib_spec->eth.val.dst_mac); 1640331769Shselasky 1641331769Shselasky ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_c, 1642331769Shselasky smac_47_16), 1643331769Shselasky ib_spec->eth.mask.src_mac); 1644331769Shselasky ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_v, 1645331769Shselasky smac_47_16), 1646331769Shselasky ib_spec->eth.val.src_mac); 1647331769Shselasky 1648331769Shselasky if (ib_spec->eth.mask.vlan_tag) { 1649331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, 1650331769Shselasky cvlan_tag, 1); 1651331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, 1652331769Shselasky cvlan_tag, 1); 1653331769Shselasky 1654331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, 1655331769Shselasky first_vid, ntohs(ib_spec->eth.mask.vlan_tag)); 1656331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, 1657331769Shselasky first_vid, ntohs(ib_spec->eth.val.vlan_tag)); 1658331769Shselasky 1659331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, 1660331769Shselasky first_cfi, 1661331769Shselasky ntohs(ib_spec->eth.mask.vlan_tag) >> 12); 1662331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, 1663331769Shselasky first_cfi, 1664331769Shselasky ntohs(ib_spec->eth.val.vlan_tag) >> 12); 1665331769Shselasky 1666331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, 1667331769Shselasky first_prio, 1668331769Shselasky ntohs(ib_spec->eth.mask.vlan_tag) >> 13); 1669331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, 1670331769Shselasky first_prio, 1671331769Shselasky ntohs(ib_spec->eth.val.vlan_tag) >> 13); 1672331769Shselasky } 1673331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, 1674331769Shselasky ethertype, ntohs(ib_spec->eth.mask.ether_type)); 1675331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, 1676331769Shselasky ethertype, ntohs(ib_spec->eth.val.ether_type)); 1677331769Shselasky break; 1678331769Shselasky case IB_FLOW_SPEC_IPV4: 1679331769Shselasky if (FIELDS_NOT_SUPPORTED(ib_spec->ipv4.mask, LAST_IPV4_FIELD)) 1680331769Shselasky return -ENOTSUPP; 1681331769Shselasky 1682331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, 1683331769Shselasky ethertype, 0xffff); 1684331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, 1685331769Shselasky ethertype, ETH_P_IP); 1686331769Shselasky 1687331769Shselasky memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_c, 1688331769Shselasky src_ipv4_src_ipv6.ipv4_layout.ipv4), 1689331769Shselasky &ib_spec->ipv4.mask.src_ip, 1690331769Shselasky sizeof(ib_spec->ipv4.mask.src_ip)); 1691331769Shselasky memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_v, 1692331769Shselasky src_ipv4_src_ipv6.ipv4_layout.ipv4), 1693331769Shselasky &ib_spec->ipv4.val.src_ip, 1694331769Shselasky sizeof(ib_spec->ipv4.val.src_ip)); 1695331769Shselasky memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_c, 1696331769Shselasky dst_ipv4_dst_ipv6.ipv4_layout.ipv4), 1697331769Shselasky &ib_spec->ipv4.mask.dst_ip, 1698331769Shselasky sizeof(ib_spec->ipv4.mask.dst_ip)); 1699331769Shselasky memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_v, 1700331769Shselasky dst_ipv4_dst_ipv6.ipv4_layout.ipv4), 1701331769Shselasky &ib_spec->ipv4.val.dst_ip, 1702331769Shselasky sizeof(ib_spec->ipv4.val.dst_ip)); 1703331769Shselasky 1704331769Shselasky set_tos(outer_headers_c, outer_headers_v, 1705331769Shselasky ib_spec->ipv4.mask.tos, ib_spec->ipv4.val.tos); 1706331769Shselasky 1707331769Shselasky set_proto(outer_headers_c, outer_headers_v, 1708331769Shselasky ib_spec->ipv4.mask.proto, ib_spec->ipv4.val.proto); 1709331769Shselasky break; 1710331769Shselasky case IB_FLOW_SPEC_IPV6: 1711331769Shselasky if (FIELDS_NOT_SUPPORTED(ib_spec->ipv6.mask, LAST_IPV6_FIELD)) 1712331769Shselasky return -ENOTSUPP; 1713331769Shselasky 1714331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, 1715331769Shselasky ethertype, 0xffff); 1716331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, 1717331769Shselasky ethertype, IPPROTO_IPV6); 1718331769Shselasky 1719331769Shselasky memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_c, 1720331769Shselasky src_ipv4_src_ipv6.ipv6_layout.ipv6), 1721331769Shselasky &ib_spec->ipv6.mask.src_ip, 1722331769Shselasky sizeof(ib_spec->ipv6.mask.src_ip)); 1723331769Shselasky memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_v, 1724331769Shselasky src_ipv4_src_ipv6.ipv6_layout.ipv6), 1725331769Shselasky &ib_spec->ipv6.val.src_ip, 1726331769Shselasky sizeof(ib_spec->ipv6.val.src_ip)); 1727331769Shselasky memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_c, 1728331769Shselasky dst_ipv4_dst_ipv6.ipv6_layout.ipv6), 1729331769Shselasky &ib_spec->ipv6.mask.dst_ip, 1730331769Shselasky sizeof(ib_spec->ipv6.mask.dst_ip)); 1731331769Shselasky memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_v, 1732331769Shselasky dst_ipv4_dst_ipv6.ipv6_layout.ipv6), 1733331769Shselasky &ib_spec->ipv6.val.dst_ip, 1734331769Shselasky sizeof(ib_spec->ipv6.val.dst_ip)); 1735331769Shselasky 1736331769Shselasky set_tos(outer_headers_c, outer_headers_v, 1737331769Shselasky ib_spec->ipv6.mask.traffic_class, 1738331769Shselasky ib_spec->ipv6.val.traffic_class); 1739331769Shselasky 1740331769Shselasky set_proto(outer_headers_c, outer_headers_v, 1741331769Shselasky ib_spec->ipv6.mask.next_hdr, 1742331769Shselasky ib_spec->ipv6.val.next_hdr); 1743331769Shselasky 1744331769Shselasky MLX5_SET(fte_match_set_misc, misc_params_c, 1745331769Shselasky outer_ipv6_flow_label, 1746331769Shselasky ntohl(ib_spec->ipv6.mask.flow_label)); 1747331769Shselasky MLX5_SET(fte_match_set_misc, misc_params_v, 1748331769Shselasky outer_ipv6_flow_label, 1749331769Shselasky ntohl(ib_spec->ipv6.val.flow_label)); 1750331769Shselasky break; 1751331769Shselasky case IB_FLOW_SPEC_TCP: 1752331769Shselasky if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask, 1753331769Shselasky LAST_TCP_UDP_FIELD)) 1754331769Shselasky return -ENOTSUPP; 1755331769Shselasky 1756331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, ip_protocol, 1757331769Shselasky 0xff); 1758331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, ip_protocol, 1759331769Shselasky IPPROTO_TCP); 1760331769Shselasky 1761331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, tcp_sport, 1762331769Shselasky ntohs(ib_spec->tcp_udp.mask.src_port)); 1763331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, tcp_sport, 1764331769Shselasky ntohs(ib_spec->tcp_udp.val.src_port)); 1765331769Shselasky 1766331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, tcp_dport, 1767331769Shselasky ntohs(ib_spec->tcp_udp.mask.dst_port)); 1768331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, tcp_dport, 1769331769Shselasky ntohs(ib_spec->tcp_udp.val.dst_port)); 1770331769Shselasky break; 1771331769Shselasky case IB_FLOW_SPEC_UDP: 1772331769Shselasky if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask, 1773331769Shselasky LAST_TCP_UDP_FIELD)) 1774331769Shselasky return -ENOTSUPP; 1775331769Shselasky 1776331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, ip_protocol, 1777331769Shselasky 0xff); 1778331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, ip_protocol, 1779331769Shselasky IPPROTO_UDP); 1780331769Shselasky 1781331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, udp_sport, 1782331769Shselasky ntohs(ib_spec->tcp_udp.mask.src_port)); 1783331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, udp_sport, 1784331769Shselasky ntohs(ib_spec->tcp_udp.val.src_port)); 1785331769Shselasky 1786331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, udp_dport, 1787331769Shselasky ntohs(ib_spec->tcp_udp.mask.dst_port)); 1788331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, udp_dport, 1789331769Shselasky ntohs(ib_spec->tcp_udp.val.dst_port)); 1790331769Shselasky break; 1791331769Shselasky default: 1792331769Shselasky return -EINVAL; 1793331769Shselasky } 1794331769Shselasky 1795331769Shselasky return 0; 1796331769Shselasky} 1797331769Shselasky 1798331769Shselasky/* If a flow could catch both multicast and unicast packets, 1799331769Shselasky * it won't fall into the multicast flow steering table and this rule 1800331769Shselasky * could steal other multicast packets. 1801331769Shselasky */ 1802331769Shselaskystatic bool flow_is_multicast_only(struct ib_flow_attr *ib_attr) 1803331769Shselasky{ 1804331769Shselasky struct ib_flow_spec_eth *eth_spec; 1805331769Shselasky 1806331769Shselasky if (ib_attr->type != IB_FLOW_ATTR_NORMAL || 1807331769Shselasky ib_attr->size < sizeof(struct ib_flow_attr) + 1808331769Shselasky sizeof(struct ib_flow_spec_eth) || 1809331769Shselasky ib_attr->num_of_specs < 1) 1810331769Shselasky return false; 1811331769Shselasky 1812331769Shselasky eth_spec = (struct ib_flow_spec_eth *)(ib_attr + 1); 1813331769Shselasky if (eth_spec->type != IB_FLOW_SPEC_ETH || 1814331769Shselasky eth_spec->size != sizeof(*eth_spec)) 1815331769Shselasky return false; 1816331769Shselasky 1817331769Shselasky return is_multicast_ether_addr(eth_spec->mask.dst_mac) && 1818331769Shselasky is_multicast_ether_addr(eth_spec->val.dst_mac); 1819331769Shselasky} 1820331769Shselasky 1821331769Shselaskystatic bool is_valid_attr(const struct ib_flow_attr *flow_attr) 1822331769Shselasky{ 1823331769Shselasky union ib_flow_spec *ib_spec = (union ib_flow_spec *)(flow_attr + 1); 1824331769Shselasky bool has_ipv4_spec = false; 1825331769Shselasky bool eth_type_ipv4 = true; 1826331769Shselasky unsigned int spec_index; 1827331769Shselasky 1828331769Shselasky /* Validate that ethertype is correct */ 1829331769Shselasky for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) { 1830331769Shselasky if (ib_spec->type == IB_FLOW_SPEC_ETH && 1831331769Shselasky ib_spec->eth.mask.ether_type) { 1832331769Shselasky if (!((ib_spec->eth.mask.ether_type == htons(0xffff)) && 1833331769Shselasky ib_spec->eth.val.ether_type == htons(ETH_P_IP))) 1834331769Shselasky eth_type_ipv4 = false; 1835331769Shselasky } else if (ib_spec->type == IB_FLOW_SPEC_IPV4) { 1836331769Shselasky has_ipv4_spec = true; 1837331769Shselasky } 1838331769Shselasky ib_spec = (void *)ib_spec + ib_spec->size; 1839331769Shselasky } 1840331769Shselasky return !has_ipv4_spec || eth_type_ipv4; 1841331769Shselasky} 1842331769Shselasky 1843331769Shselaskystatic void put_flow_table(struct mlx5_ib_dev *dev, 1844331769Shselasky struct mlx5_ib_flow_prio *prio, bool ft_added) 1845331769Shselasky{ 1846331769Shselasky prio->refcount -= !!ft_added; 1847331769Shselasky if (!prio->refcount) { 1848331769Shselasky mlx5_destroy_flow_table(prio->flow_table); 1849331769Shselasky prio->flow_table = NULL; 1850331769Shselasky } 1851331769Shselasky} 1852331769Shselasky 1853331769Shselaskystatic int mlx5_ib_destroy_flow(struct ib_flow *flow_id) 1854331769Shselasky{ 1855331769Shselasky struct mlx5_ib_dev *dev = to_mdev(flow_id->qp->device); 1856331769Shselasky struct mlx5_ib_flow_handler *handler = container_of(flow_id, 1857331769Shselasky struct mlx5_ib_flow_handler, 1858331769Shselasky ibflow); 1859331769Shselasky struct mlx5_ib_flow_handler *iter, *tmp; 1860331769Shselasky 1861331769Shselasky mutex_lock(&dev->flow_db.lock); 1862331769Shselasky 1863331769Shselasky list_for_each_entry_safe(iter, tmp, &handler->list, list) { 1864331769Shselasky mlx5_del_flow_rule(iter->rule); 1865331769Shselasky put_flow_table(dev, iter->prio, true); 1866331769Shselasky list_del(&iter->list); 1867331769Shselasky kfree(iter); 1868331769Shselasky } 1869331769Shselasky 1870331769Shselasky mlx5_del_flow_rule(handler->rule); 1871331769Shselasky put_flow_table(dev, handler->prio, true); 1872331769Shselasky mutex_unlock(&dev->flow_db.lock); 1873331769Shselasky 1874331769Shselasky kfree(handler); 1875331769Shselasky 1876331769Shselasky return 0; 1877331769Shselasky} 1878331769Shselasky 1879331769Shselaskystatic int ib_prio_to_core_prio(unsigned int priority, bool dont_trap) 1880331769Shselasky{ 1881331769Shselasky priority *= 2; 1882331769Shselasky if (!dont_trap) 1883331769Shselasky priority++; 1884331769Shselasky return priority; 1885331769Shselasky} 1886331769Shselasky 1887331769Shselaskyenum flow_table_type { 1888331769Shselasky MLX5_IB_FT_RX, 1889331769Shselasky MLX5_IB_FT_TX 1890331769Shselasky}; 1891331769Shselasky 1892331769Shselasky#define MLX5_FS_MAX_TYPES 10 1893331769Shselasky#define MLX5_FS_MAX_ENTRIES 32000UL 1894331769Shselaskystatic struct mlx5_ib_flow_prio *get_flow_table(struct mlx5_ib_dev *dev, 1895331769Shselasky struct ib_flow_attr *flow_attr, 1896331769Shselasky enum flow_table_type ft_type) 1897331769Shselasky{ 1898331769Shselasky bool dont_trap = flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP; 1899331769Shselasky struct mlx5_flow_namespace *ns = NULL; 1900331769Shselasky struct mlx5_ib_flow_prio *prio; 1901331769Shselasky struct mlx5_flow_table *ft; 1902331769Shselasky int num_entries; 1903331769Shselasky int num_groups; 1904331769Shselasky int priority; 1905331769Shselasky int err = 0; 1906331769Shselasky 1907331769Shselasky if (flow_attr->type == IB_FLOW_ATTR_NORMAL) { 1908331769Shselasky if (flow_is_multicast_only(flow_attr) && 1909331769Shselasky !dont_trap) 1910331769Shselasky priority = MLX5_IB_FLOW_MCAST_PRIO; 1911331769Shselasky else 1912331769Shselasky priority = ib_prio_to_core_prio(flow_attr->priority, 1913331769Shselasky dont_trap); 1914331769Shselasky ns = mlx5_get_flow_namespace(dev->mdev, 1915331769Shselasky MLX5_FLOW_NAMESPACE_BYPASS); 1916331769Shselasky num_entries = MLX5_FS_MAX_ENTRIES; 1917331769Shselasky num_groups = MLX5_FS_MAX_TYPES; 1918331769Shselasky prio = &dev->flow_db.prios[priority]; 1919331769Shselasky } else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT || 1920331769Shselasky flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) { 1921331769Shselasky ns = mlx5_get_flow_namespace(dev->mdev, 1922331769Shselasky MLX5_FLOW_NAMESPACE_LEFTOVERS); 1923331769Shselasky build_leftovers_ft_param("bypass", &priority, 1924331769Shselasky &num_entries, 1925331769Shselasky &num_groups); 1926331769Shselasky prio = &dev->flow_db.prios[MLX5_IB_FLOW_LEFTOVERS_PRIO]; 1927331769Shselasky } else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) { 1928331769Shselasky if (!MLX5_CAP_FLOWTABLE(dev->mdev, 1929331769Shselasky allow_sniffer_and_nic_rx_shared_tir)) 1930331769Shselasky return ERR_PTR(-ENOTSUPP); 1931331769Shselasky 1932331769Shselasky ns = mlx5_get_flow_namespace(dev->mdev, ft_type == MLX5_IB_FT_RX ? 1933331769Shselasky MLX5_FLOW_NAMESPACE_SNIFFER_RX : 1934331769Shselasky MLX5_FLOW_NAMESPACE_SNIFFER_TX); 1935331769Shselasky 1936331769Shselasky prio = &dev->flow_db.sniffer[ft_type]; 1937331769Shselasky priority = 0; 1938331769Shselasky num_entries = 1; 1939331769Shselasky num_groups = 1; 1940331769Shselasky } 1941331769Shselasky 1942331769Shselasky if (!ns) 1943331769Shselasky return ERR_PTR(-ENOTSUPP); 1944331769Shselasky 1945331769Shselasky ft = prio->flow_table; 1946331769Shselasky if (!ft) { 1947331769Shselasky ft = mlx5_create_auto_grouped_flow_table(ns, priority, "bypass", 1948331769Shselasky num_entries, 1949331769Shselasky num_groups); 1950331769Shselasky 1951331769Shselasky if (!IS_ERR(ft)) { 1952331769Shselasky prio->refcount = 0; 1953331769Shselasky prio->flow_table = ft; 1954331769Shselasky } else { 1955331769Shselasky err = PTR_ERR(ft); 1956331769Shselasky } 1957331769Shselasky } 1958331769Shselasky 1959331769Shselasky return err ? ERR_PTR(err) : prio; 1960331769Shselasky} 1961331769Shselasky 1962331769Shselaskystatic struct mlx5_ib_flow_handler *create_flow_rule(struct mlx5_ib_dev *dev, 1963331769Shselasky struct mlx5_ib_flow_prio *ft_prio, 1964331769Shselasky const struct ib_flow_attr *flow_attr, 1965331769Shselasky struct mlx5_flow_destination *dst) 1966331769Shselasky{ 1967331769Shselasky struct mlx5_flow_table *ft = ft_prio->flow_table; 1968331769Shselasky struct mlx5_ib_flow_handler *handler; 1969331769Shselasky struct mlx5_flow_spec *spec; 1970331769Shselasky const void *ib_flow = (const void *)flow_attr + sizeof(*flow_attr); 1971331769Shselasky unsigned int spec_index; 1972331769Shselasky u32 action; 1973331769Shselasky int err = 0; 1974331769Shselasky 1975331769Shselasky if (!is_valid_attr(flow_attr)) 1976331769Shselasky return ERR_PTR(-EINVAL); 1977331769Shselasky 1978331769Shselasky spec = mlx5_vzalloc(sizeof(*spec)); 1979331769Shselasky handler = kzalloc(sizeof(*handler), GFP_KERNEL); 1980331769Shselasky if (!handler || !spec) { 1981331769Shselasky err = -ENOMEM; 1982331769Shselasky goto free; 1983331769Shselasky } 1984331769Shselasky 1985331769Shselasky INIT_LIST_HEAD(&handler->list); 1986331769Shselasky 1987331769Shselasky for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) { 1988331769Shselasky err = parse_flow_attr(spec->match_criteria, 1989331769Shselasky spec->match_value, ib_flow); 1990331769Shselasky if (err < 0) 1991331769Shselasky goto free; 1992331769Shselasky 1993331769Shselasky ib_flow += ((union ib_flow_spec *)ib_flow)->size; 1994331769Shselasky } 1995331769Shselasky 1996331769Shselasky spec->match_criteria_enable = get_match_criteria_enable(spec->match_criteria); 1997331769Shselasky action = dst ? MLX5_FLOW_CONTEXT_ACTION_FWD_DEST : 1998331769Shselasky MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO; 1999331769Shselasky handler->rule = mlx5_add_flow_rule(ft, spec->match_criteria_enable, 2000331769Shselasky spec->match_criteria, 2001331769Shselasky spec->match_value, 2002331769Shselasky action, 2003331769Shselasky MLX5_FS_DEFAULT_FLOW_TAG, 2004331769Shselasky dst); 2005331769Shselasky 2006331769Shselasky if (IS_ERR(handler->rule)) { 2007331769Shselasky err = PTR_ERR(handler->rule); 2008331769Shselasky goto free; 2009331769Shselasky } 2010331769Shselasky 2011331769Shselasky ft_prio->refcount++; 2012331769Shselasky handler->prio = ft_prio; 2013331769Shselasky 2014331769Shselasky ft_prio->flow_table = ft; 2015331769Shselaskyfree: 2016331769Shselasky if (err) 2017331769Shselasky kfree(handler); 2018331769Shselasky kvfree(spec); 2019331769Shselasky return err ? ERR_PTR(err) : handler; 2020331769Shselasky} 2021331769Shselasky 2022331769Shselaskystatic struct mlx5_ib_flow_handler *create_dont_trap_rule(struct mlx5_ib_dev *dev, 2023331769Shselasky struct mlx5_ib_flow_prio *ft_prio, 2024331769Shselasky struct ib_flow_attr *flow_attr, 2025331769Shselasky struct mlx5_flow_destination *dst) 2026331769Shselasky{ 2027331769Shselasky struct mlx5_ib_flow_handler *handler_dst = NULL; 2028331769Shselasky struct mlx5_ib_flow_handler *handler = NULL; 2029331769Shselasky 2030331769Shselasky handler = create_flow_rule(dev, ft_prio, flow_attr, NULL); 2031331769Shselasky if (!IS_ERR(handler)) { 2032331769Shselasky handler_dst = create_flow_rule(dev, ft_prio, 2033331769Shselasky flow_attr, dst); 2034331769Shselasky if (IS_ERR(handler_dst)) { 2035331769Shselasky mlx5_del_flow_rule(handler->rule); 2036331769Shselasky ft_prio->refcount--; 2037331769Shselasky kfree(handler); 2038331769Shselasky handler = handler_dst; 2039331769Shselasky } else { 2040331769Shselasky list_add(&handler_dst->list, &handler->list); 2041331769Shselasky } 2042331769Shselasky } 2043331769Shselasky 2044331769Shselasky return handler; 2045331769Shselasky} 2046331769Shselaskyenum { 2047331769Shselasky LEFTOVERS_MC, 2048331769Shselasky LEFTOVERS_UC, 2049331769Shselasky}; 2050331769Shselasky 2051331769Shselaskystatic struct mlx5_ib_flow_handler *create_leftovers_rule(struct mlx5_ib_dev *dev, 2052331769Shselasky struct mlx5_ib_flow_prio *ft_prio, 2053331769Shselasky struct ib_flow_attr *flow_attr, 2054331769Shselasky struct mlx5_flow_destination *dst) 2055331769Shselasky{ 2056331769Shselasky struct mlx5_ib_flow_handler *handler_ucast = NULL; 2057331769Shselasky struct mlx5_ib_flow_handler *handler = NULL; 2058331769Shselasky 2059331769Shselasky static struct { 2060331769Shselasky struct ib_flow_attr flow_attr; 2061331769Shselasky struct ib_flow_spec_eth eth_flow; 2062331769Shselasky } leftovers_specs[] = { 2063331769Shselasky [LEFTOVERS_MC] = { 2064331769Shselasky .flow_attr = { 2065331769Shselasky .num_of_specs = 1, 2066331769Shselasky .size = sizeof(leftovers_specs[0]) 2067331769Shselasky }, 2068331769Shselasky .eth_flow = { 2069331769Shselasky .type = IB_FLOW_SPEC_ETH, 2070331769Shselasky .size = sizeof(struct ib_flow_spec_eth), 2071331769Shselasky .mask = {.dst_mac = {0x1} }, 2072331769Shselasky .val = {.dst_mac = {0x1} } 2073331769Shselasky } 2074331769Shselasky }, 2075331769Shselasky [LEFTOVERS_UC] = { 2076331769Shselasky .flow_attr = { 2077331769Shselasky .num_of_specs = 1, 2078331769Shselasky .size = sizeof(leftovers_specs[0]) 2079331769Shselasky }, 2080331769Shselasky .eth_flow = { 2081331769Shselasky .type = IB_FLOW_SPEC_ETH, 2082331769Shselasky .size = sizeof(struct ib_flow_spec_eth), 2083331769Shselasky .mask = {.dst_mac = {0x1} }, 2084331769Shselasky .val = {.dst_mac = {} } 2085331769Shselasky } 2086331769Shselasky } 2087331769Shselasky }; 2088331769Shselasky 2089331769Shselasky handler = create_flow_rule(dev, ft_prio, 2090331769Shselasky &leftovers_specs[LEFTOVERS_MC].flow_attr, 2091331769Shselasky dst); 2092331769Shselasky if (!IS_ERR(handler) && 2093331769Shselasky flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT) { 2094331769Shselasky handler_ucast = create_flow_rule(dev, ft_prio, 2095331769Shselasky &leftovers_specs[LEFTOVERS_UC].flow_attr, 2096331769Shselasky dst); 2097331769Shselasky if (IS_ERR(handler_ucast)) { 2098331769Shselasky mlx5_del_flow_rule(handler->rule); 2099331769Shselasky ft_prio->refcount--; 2100331769Shselasky kfree(handler); 2101331769Shselasky handler = handler_ucast; 2102331769Shselasky } else { 2103331769Shselasky list_add(&handler_ucast->list, &handler->list); 2104331769Shselasky } 2105331769Shselasky } 2106331769Shselasky 2107331769Shselasky return handler; 2108331769Shselasky} 2109331769Shselasky 2110331769Shselaskystatic struct mlx5_ib_flow_handler *create_sniffer_rule(struct mlx5_ib_dev *dev, 2111331769Shselasky struct mlx5_ib_flow_prio *ft_rx, 2112331769Shselasky struct mlx5_ib_flow_prio *ft_tx, 2113331769Shselasky struct mlx5_flow_destination *dst) 2114331769Shselasky{ 2115331769Shselasky struct mlx5_ib_flow_handler *handler_rx; 2116331769Shselasky struct mlx5_ib_flow_handler *handler_tx; 2117331769Shselasky int err; 2118331769Shselasky static const struct ib_flow_attr flow_attr = { 2119331769Shselasky .num_of_specs = 0, 2120331769Shselasky .size = sizeof(flow_attr) 2121331769Shselasky }; 2122331769Shselasky 2123331769Shselasky handler_rx = create_flow_rule(dev, ft_rx, &flow_attr, dst); 2124331769Shselasky if (IS_ERR(handler_rx)) { 2125331769Shselasky err = PTR_ERR(handler_rx); 2126331769Shselasky goto err; 2127331769Shselasky } 2128331769Shselasky 2129331769Shselasky handler_tx = create_flow_rule(dev, ft_tx, &flow_attr, dst); 2130331769Shselasky if (IS_ERR(handler_tx)) { 2131331769Shselasky err = PTR_ERR(handler_tx); 2132331769Shselasky goto err_tx; 2133331769Shselasky } 2134331769Shselasky 2135331769Shselasky list_add(&handler_tx->list, &handler_rx->list); 2136331769Shselasky 2137331769Shselasky return handler_rx; 2138331769Shselasky 2139331769Shselaskyerr_tx: 2140331769Shselasky mlx5_del_flow_rule(handler_rx->rule); 2141331769Shselasky ft_rx->refcount--; 2142331769Shselasky kfree(handler_rx); 2143331769Shselaskyerr: 2144331769Shselasky return ERR_PTR(err); 2145331769Shselasky} 2146331769Shselasky 2147331769Shselaskystatic struct ib_flow *mlx5_ib_create_flow(struct ib_qp *qp, 2148331769Shselasky struct ib_flow_attr *flow_attr, 2149331769Shselasky int domain) 2150331769Shselasky{ 2151331769Shselasky struct mlx5_ib_dev *dev = to_mdev(qp->device); 2152331769Shselasky struct mlx5_ib_qp *mqp = to_mqp(qp); 2153331769Shselasky struct mlx5_ib_flow_handler *handler = NULL; 2154331769Shselasky struct mlx5_flow_destination *dst = NULL; 2155331769Shselasky struct mlx5_ib_flow_prio *ft_prio_tx = NULL; 2156331769Shselasky struct mlx5_ib_flow_prio *ft_prio; 2157331769Shselasky int err; 2158331769Shselasky 2159331769Shselasky if (flow_attr->priority > MLX5_IB_FLOW_LAST_PRIO) 2160331769Shselasky return ERR_PTR(-ENOSPC); 2161331769Shselasky 2162331769Shselasky if (domain != IB_FLOW_DOMAIN_USER || 2163331769Shselasky flow_attr->port > MLX5_CAP_GEN(dev->mdev, num_ports) || 2164331769Shselasky (flow_attr->flags & ~IB_FLOW_ATTR_FLAGS_DONT_TRAP)) 2165331769Shselasky return ERR_PTR(-EINVAL); 2166331769Shselasky 2167331769Shselasky dst = kzalloc(sizeof(*dst), GFP_KERNEL); 2168331769Shselasky if (!dst) 2169331769Shselasky return ERR_PTR(-ENOMEM); 2170331769Shselasky 2171331769Shselasky mutex_lock(&dev->flow_db.lock); 2172331769Shselasky 2173331769Shselasky ft_prio = get_flow_table(dev, flow_attr, MLX5_IB_FT_RX); 2174331769Shselasky if (IS_ERR(ft_prio)) { 2175331769Shselasky err = PTR_ERR(ft_prio); 2176331769Shselasky goto unlock; 2177331769Shselasky } 2178331769Shselasky if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) { 2179331769Shselasky ft_prio_tx = get_flow_table(dev, flow_attr, MLX5_IB_FT_TX); 2180331769Shselasky if (IS_ERR(ft_prio_tx)) { 2181331769Shselasky err = PTR_ERR(ft_prio_tx); 2182331769Shselasky ft_prio_tx = NULL; 2183331769Shselasky goto destroy_ft; 2184331769Shselasky } 2185331769Shselasky } 2186331769Shselasky 2187331769Shselasky dst->type = MLX5_FLOW_DESTINATION_TYPE_TIR; 2188331769Shselasky if (mqp->flags & MLX5_IB_QP_RSS) 2189331769Shselasky dst->tir_num = mqp->rss_qp.tirn; 2190331769Shselasky else 2191331769Shselasky dst->tir_num = mqp->raw_packet_qp.rq.tirn; 2192331769Shselasky 2193331769Shselasky if (flow_attr->type == IB_FLOW_ATTR_NORMAL) { 2194331769Shselasky if (flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP) { 2195331769Shselasky handler = create_dont_trap_rule(dev, ft_prio, 2196331769Shselasky flow_attr, dst); 2197331769Shselasky } else { 2198331769Shselasky handler = create_flow_rule(dev, ft_prio, flow_attr, 2199331769Shselasky dst); 2200331769Shselasky } 2201331769Shselasky } else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT || 2202331769Shselasky flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) { 2203331769Shselasky handler = create_leftovers_rule(dev, ft_prio, flow_attr, 2204331769Shselasky dst); 2205331769Shselasky } else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) { 2206331769Shselasky handler = create_sniffer_rule(dev, ft_prio, ft_prio_tx, dst); 2207331769Shselasky } else { 2208331769Shselasky err = -EINVAL; 2209331769Shselasky goto destroy_ft; 2210331769Shselasky } 2211331769Shselasky 2212331769Shselasky if (IS_ERR(handler)) { 2213331769Shselasky err = PTR_ERR(handler); 2214331769Shselasky handler = NULL; 2215331769Shselasky goto destroy_ft; 2216331769Shselasky } 2217331769Shselasky 2218331769Shselasky mutex_unlock(&dev->flow_db.lock); 2219331769Shselasky kfree(dst); 2220331769Shselasky 2221331769Shselasky return &handler->ibflow; 2222331769Shselasky 2223331769Shselaskydestroy_ft: 2224331769Shselasky put_flow_table(dev, ft_prio, false); 2225331769Shselasky if (ft_prio_tx) 2226331769Shselasky put_flow_table(dev, ft_prio_tx, false); 2227331769Shselaskyunlock: 2228331769Shselasky mutex_unlock(&dev->flow_db.lock); 2229331769Shselasky kfree(dst); 2230331769Shselasky kfree(handler); 2231331769Shselasky return ERR_PTR(err); 2232331769Shselasky} 2233331769Shselasky 2234322810Shselaskystatic int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid) 2235322810Shselasky{ 2236322810Shselasky struct mlx5_ib_dev *dev = to_mdev(ibqp->device); 2237322810Shselasky int err; 2238322810Shselasky 2239331769Shselasky err = mlx5_core_attach_mcg(dev->mdev, gid, ibqp->qp_num); 2240322810Shselasky if (err) 2241322810Shselasky mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n", 2242322810Shselasky ibqp->qp_num, gid->raw); 2243322810Shselasky 2244322810Shselasky return err; 2245322810Shselasky} 2246322810Shselasky 2247322810Shselaskystatic int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid) 2248322810Shselasky{ 2249322810Shselasky struct mlx5_ib_dev *dev = to_mdev(ibqp->device); 2250322810Shselasky int err; 2251322810Shselasky 2252331769Shselasky err = mlx5_core_detach_mcg(dev->mdev, gid, ibqp->qp_num); 2253322810Shselasky if (err) 2254322810Shselasky mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n", 2255322810Shselasky ibqp->qp_num, gid->raw); 2256322810Shselasky 2257322810Shselasky return err; 2258322810Shselasky} 2259322810Shselasky 2260322810Shselaskystatic int init_node_data(struct mlx5_ib_dev *dev) 2261322810Shselasky{ 2262322810Shselasky int err; 2263322810Shselasky 2264322810Shselasky err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc); 2265322810Shselasky if (err) 2266322810Shselasky return err; 2267322810Shselasky 2268322810Shselasky return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid); 2269322810Shselasky} 2270322810Shselasky 2271322810Shselaskystatic ssize_t show_fw_pages(struct device *device, struct device_attribute *attr, 2272322810Shselasky char *buf) 2273322810Shselasky{ 2274322810Shselasky struct mlx5_ib_dev *dev = 2275322810Shselasky container_of(device, struct mlx5_ib_dev, ib_dev.dev); 2276322810Shselasky 2277322810Shselasky return sprintf(buf, "%lld\n", (long long)dev->mdev->priv.fw_pages); 2278322810Shselasky} 2279322810Shselasky 2280322810Shselaskystatic ssize_t show_reg_pages(struct device *device, 2281322810Shselasky struct device_attribute *attr, char *buf) 2282322810Shselasky{ 2283322810Shselasky struct mlx5_ib_dev *dev = 2284322810Shselasky container_of(device, struct mlx5_ib_dev, ib_dev.dev); 2285322810Shselasky 2286322810Shselasky return sprintf(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages)); 2287322810Shselasky} 2288322810Shselasky 2289322810Shselaskystatic ssize_t show_hca(struct device *device, struct device_attribute *attr, 2290322810Shselasky char *buf) 2291322810Shselasky{ 2292322810Shselasky struct mlx5_ib_dev *dev = 2293322810Shselasky container_of(device, struct mlx5_ib_dev, ib_dev.dev); 2294322810Shselasky return sprintf(buf, "MT%d\n", dev->mdev->pdev->device); 2295322810Shselasky} 2296322810Shselasky 2297322810Shselaskystatic ssize_t show_rev(struct device *device, struct device_attribute *attr, 2298322810Shselasky char *buf) 2299322810Shselasky{ 2300322810Shselasky struct mlx5_ib_dev *dev = 2301322810Shselasky container_of(device, struct mlx5_ib_dev, ib_dev.dev); 2302331769Shselasky return sprintf(buf, "%x\n", dev->mdev->pdev->revision); 2303322810Shselasky} 2304322810Shselasky 2305322810Shselaskystatic ssize_t show_board(struct device *device, struct device_attribute *attr, 2306322810Shselasky char *buf) 2307322810Shselasky{ 2308322810Shselasky struct mlx5_ib_dev *dev = 2309322810Shselasky container_of(device, struct mlx5_ib_dev, ib_dev.dev); 2310322810Shselasky return sprintf(buf, "%.*s\n", MLX5_BOARD_ID_LEN, 2311322810Shselasky dev->mdev->board_id); 2312322810Shselasky} 2313322810Shselasky 2314322810Shselaskystatic DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL); 2315322810Shselaskystatic DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL); 2316322810Shselaskystatic DEVICE_ATTR(board_id, S_IRUGO, show_board, NULL); 2317322810Shselaskystatic DEVICE_ATTR(fw_pages, S_IRUGO, show_fw_pages, NULL); 2318322810Shselaskystatic DEVICE_ATTR(reg_pages, S_IRUGO, show_reg_pages, NULL); 2319322810Shselasky 2320322810Shselaskystatic struct device_attribute *mlx5_class_attributes[] = { 2321322810Shselasky &dev_attr_hw_rev, 2322322810Shselasky &dev_attr_hca_type, 2323322810Shselasky &dev_attr_board_id, 2324322810Shselasky &dev_attr_fw_pages, 2325322810Shselasky &dev_attr_reg_pages, 2326322810Shselasky}; 2327322810Shselasky 2328331769Shselaskystatic void pkey_change_handler(struct work_struct *work) 2329331769Shselasky{ 2330331769Shselasky struct mlx5_ib_port_resources *ports = 2331331769Shselasky container_of(work, struct mlx5_ib_port_resources, 2332331769Shselasky pkey_change_work); 2333331769Shselasky 2334331769Shselasky mutex_lock(&ports->devr->mutex); 2335331769Shselasky mlx5_ib_gsi_pkey_change(ports->gsi); 2336331769Shselasky mutex_unlock(&ports->devr->mutex); 2337331769Shselasky} 2338331769Shselasky 2339322810Shselaskystatic void mlx5_ib_handle_internal_error(struct mlx5_ib_dev *ibdev) 2340322810Shselasky{ 2341322810Shselasky struct mlx5_ib_qp *mqp; 2342322810Shselasky struct mlx5_ib_cq *send_mcq, *recv_mcq; 2343322810Shselasky struct mlx5_core_cq *mcq; 2344322810Shselasky struct list_head cq_armed_list; 2345322810Shselasky unsigned long flags_qp; 2346322810Shselasky unsigned long flags_cq; 2347322810Shselasky unsigned long flags; 2348322810Shselasky 2349322810Shselasky INIT_LIST_HEAD(&cq_armed_list); 2350322810Shselasky 2351322810Shselasky /* Go over qp list reside on that ibdev, sync with create/destroy qp.*/ 2352322810Shselasky spin_lock_irqsave(&ibdev->reset_flow_resource_lock, flags); 2353322810Shselasky list_for_each_entry(mqp, &ibdev->qp_list, qps_list) { 2354322810Shselasky spin_lock_irqsave(&mqp->sq.lock, flags_qp); 2355322810Shselasky if (mqp->sq.tail != mqp->sq.head) { 2356322810Shselasky send_mcq = to_mcq(mqp->ibqp.send_cq); 2357322810Shselasky spin_lock_irqsave(&send_mcq->lock, flags_cq); 2358322810Shselasky if (send_mcq->mcq.comp && 2359322810Shselasky mqp->ibqp.send_cq->comp_handler) { 2360322810Shselasky if (!send_mcq->mcq.reset_notify_added) { 2361322810Shselasky send_mcq->mcq.reset_notify_added = 1; 2362322810Shselasky list_add_tail(&send_mcq->mcq.reset_notify, 2363322810Shselasky &cq_armed_list); 2364322810Shselasky } 2365322810Shselasky } 2366322810Shselasky spin_unlock_irqrestore(&send_mcq->lock, flags_cq); 2367322810Shselasky } 2368322810Shselasky spin_unlock_irqrestore(&mqp->sq.lock, flags_qp); 2369322810Shselasky spin_lock_irqsave(&mqp->rq.lock, flags_qp); 2370322810Shselasky /* no handling is needed for SRQ */ 2371322810Shselasky if (!mqp->ibqp.srq) { 2372322810Shselasky if (mqp->rq.tail != mqp->rq.head) { 2373322810Shselasky recv_mcq = to_mcq(mqp->ibqp.recv_cq); 2374322810Shselasky spin_lock_irqsave(&recv_mcq->lock, flags_cq); 2375322810Shselasky if (recv_mcq->mcq.comp && 2376322810Shselasky mqp->ibqp.recv_cq->comp_handler) { 2377322810Shselasky if (!recv_mcq->mcq.reset_notify_added) { 2378322810Shselasky recv_mcq->mcq.reset_notify_added = 1; 2379322810Shselasky list_add_tail(&recv_mcq->mcq.reset_notify, 2380322810Shselasky &cq_armed_list); 2381322810Shselasky } 2382322810Shselasky } 2383322810Shselasky spin_unlock_irqrestore(&recv_mcq->lock, 2384322810Shselasky flags_cq); 2385322810Shselasky } 2386322810Shselasky } 2387322810Shselasky spin_unlock_irqrestore(&mqp->rq.lock, flags_qp); 2388322810Shselasky } 2389322810Shselasky /*At that point all inflight post send were put to be executed as of we 2390322810Shselasky * lock/unlock above locks Now need to arm all involved CQs. 2391322810Shselasky */ 2392322810Shselasky list_for_each_entry(mcq, &cq_armed_list, reset_notify) { 2393322810Shselasky mcq->comp(mcq); 2394322810Shselasky } 2395322810Shselasky spin_unlock_irqrestore(&ibdev->reset_flow_resource_lock, flags); 2396322810Shselasky} 2397322810Shselasky 2398322810Shselaskystatic void mlx5_ib_event(struct mlx5_core_dev *dev, void *context, 2399322810Shselasky enum mlx5_dev_event event, unsigned long param) 2400322810Shselasky{ 2401322810Shselasky struct mlx5_ib_dev *ibdev = (struct mlx5_ib_dev *)context; 2402322810Shselasky struct ib_event ibev; 2403331769Shselasky bool fatal = false; 2404341922Shselasky u8 port = (u8)param; 2405322810Shselasky 2406322810Shselasky switch (event) { 2407322810Shselasky case MLX5_DEV_EVENT_SYS_ERROR: 2408322810Shselasky ibev.event = IB_EVENT_DEVICE_FATAL; 2409322810Shselasky mlx5_ib_handle_internal_error(ibdev); 2410331769Shselasky fatal = true; 2411322810Shselasky break; 2412322810Shselasky 2413322810Shselasky case MLX5_DEV_EVENT_PORT_UP: 2414322810Shselasky case MLX5_DEV_EVENT_PORT_DOWN: 2415322810Shselasky case MLX5_DEV_EVENT_PORT_INITIALIZED: 2416331769Shselasky /* In RoCE, port up/down events are handled in 2417331769Shselasky * mlx5_netdev_event(). 2418331769Shselasky */ 2419331769Shselasky if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) == 2420331769Shselasky IB_LINK_LAYER_ETHERNET) 2421331769Shselasky return; 2422331769Shselasky 2423331769Shselasky ibev.event = (event == MLX5_DEV_EVENT_PORT_UP) ? 2424331769Shselasky IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR; 2425322810Shselasky break; 2426322810Shselasky 2427322810Shselasky case MLX5_DEV_EVENT_LID_CHANGE: 2428322810Shselasky ibev.event = IB_EVENT_LID_CHANGE; 2429322810Shselasky break; 2430322810Shselasky 2431322810Shselasky case MLX5_DEV_EVENT_PKEY_CHANGE: 2432322810Shselasky ibev.event = IB_EVENT_PKEY_CHANGE; 2433331769Shselasky 2434331769Shselasky schedule_work(&ibdev->devr.ports[port - 1].pkey_change_work); 2435322810Shselasky break; 2436322810Shselasky 2437322810Shselasky case MLX5_DEV_EVENT_GUID_CHANGE: 2438322810Shselasky ibev.event = IB_EVENT_GID_CHANGE; 2439322810Shselasky break; 2440322810Shselasky 2441322810Shselasky case MLX5_DEV_EVENT_CLIENT_REREG: 2442322810Shselasky ibev.event = IB_EVENT_CLIENT_REREGISTER; 2443322810Shselasky break; 2444322810Shselasky 2445322810Shselasky default: 2446337100Shselasky /* unsupported event */ 2447337100Shselasky return; 2448322810Shselasky } 2449322810Shselasky 2450322810Shselasky ibev.device = &ibdev->ib_dev; 2451322810Shselasky ibev.element.port_num = port; 2452322810Shselasky 2453341922Shselasky if (!rdma_is_port_valid(&ibdev->ib_dev, port)) { 2454337100Shselasky mlx5_ib_warn(ibdev, "warning: event(%d) on port %d\n", event, port); 2455322810Shselasky return; 2456322810Shselasky } 2457322810Shselasky 2458322810Shselasky if (ibdev->ib_active) 2459322810Shselasky ib_dispatch_event(&ibev); 2460331769Shselasky 2461331769Shselasky if (fatal) 2462331769Shselasky ibdev->ib_active = false; 2463322810Shselasky} 2464322810Shselasky 2465322810Shselaskystatic void get_ext_port_caps(struct mlx5_ib_dev *dev) 2466322810Shselasky{ 2467322810Shselasky int port; 2468322810Shselasky 2469322810Shselasky for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++) 2470322810Shselasky mlx5_query_ext_port_caps(dev, port); 2471322810Shselasky} 2472322810Shselasky 2473322810Shselaskystatic int get_port_caps(struct mlx5_ib_dev *dev) 2474322810Shselasky{ 2475322810Shselasky struct ib_device_attr *dprops = NULL; 2476322810Shselasky struct ib_port_attr *pprops = NULL; 2477322810Shselasky int err = -ENOMEM; 2478322810Shselasky int port; 2479331769Shselasky struct ib_udata uhw = {.inlen = 0, .outlen = 0}; 2480322810Shselasky 2481322810Shselasky pprops = kmalloc(sizeof(*pprops), GFP_KERNEL); 2482322810Shselasky if (!pprops) 2483322810Shselasky goto out; 2484322810Shselasky 2485322810Shselasky dprops = kmalloc(sizeof(*dprops), GFP_KERNEL); 2486322810Shselasky if (!dprops) 2487322810Shselasky goto out; 2488322810Shselasky 2489331769Shselasky err = mlx5_ib_query_device(&dev->ib_dev, dprops, &uhw); 2490322810Shselasky if (err) { 2491322810Shselasky mlx5_ib_warn(dev, "query_device failed %d\n", err); 2492322810Shselasky goto out; 2493322810Shselasky } 2494322810Shselasky 2495322810Shselasky for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++) { 2496322810Shselasky err = mlx5_ib_query_port(&dev->ib_dev, port, pprops); 2497322810Shselasky if (err) { 2498322810Shselasky mlx5_ib_warn(dev, "query_port %d failed %d\n", 2499322810Shselasky port, err); 2500322810Shselasky break; 2501322810Shselasky } 2502331769Shselasky dev->mdev->port_caps[port - 1].pkey_table_len = 2503331769Shselasky dprops->max_pkeys; 2504331769Shselasky dev->mdev->port_caps[port - 1].gid_table_len = 2505331769Shselasky pprops->gid_tbl_len; 2506322810Shselasky mlx5_ib_dbg(dev, "pkey_table_len %d, gid_table_len %d\n", 2507322810Shselasky dprops->max_pkeys, pprops->gid_tbl_len); 2508322810Shselasky } 2509322810Shselasky 2510322810Shselaskyout: 2511322810Shselasky kfree(pprops); 2512322810Shselasky kfree(dprops); 2513322810Shselasky 2514322810Shselasky return err; 2515322810Shselasky} 2516322810Shselasky 2517322810Shselaskystatic void destroy_umrc_res(struct mlx5_ib_dev *dev) 2518322810Shselasky{ 2519322810Shselasky int err; 2520322810Shselasky 2521322810Shselasky err = mlx5_mr_cache_cleanup(dev); 2522322810Shselasky if (err) 2523322810Shselasky mlx5_ib_warn(dev, "mr cache cleanup failed\n"); 2524322810Shselasky 2525331769Shselasky mlx5_ib_destroy_qp(dev->umrc.qp); 2526331769Shselasky ib_free_cq(dev->umrc.cq); 2527322810Shselasky ib_dealloc_pd(dev->umrc.pd); 2528322810Shselasky} 2529322810Shselasky 2530322810Shselaskyenum { 2531322810Shselasky MAX_UMR_WR = 128, 2532322810Shselasky}; 2533322810Shselasky 2534322810Shselaskystatic int create_umr_res(struct mlx5_ib_dev *dev) 2535322810Shselasky{ 2536331769Shselasky struct ib_qp_init_attr *init_attr = NULL; 2537331769Shselasky struct ib_qp_attr *attr = NULL; 2538322810Shselasky struct ib_pd *pd; 2539331769Shselasky struct ib_cq *cq; 2540331769Shselasky struct ib_qp *qp; 2541322810Shselasky int ret; 2542322810Shselasky 2543331769Shselasky attr = kzalloc(sizeof(*attr), GFP_KERNEL); 2544331769Shselasky init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL); 2545331769Shselasky if (!attr || !init_attr) { 2546331769Shselasky ret = -ENOMEM; 2547331769Shselasky goto error_0; 2548331769Shselasky } 2549331769Shselasky 2550331769Shselasky pd = ib_alloc_pd(&dev->ib_dev, 0); 2551322810Shselasky if (IS_ERR(pd)) { 2552322810Shselasky mlx5_ib_dbg(dev, "Couldn't create PD for sync UMR QP\n"); 2553322810Shselasky ret = PTR_ERR(pd); 2554322810Shselasky goto error_0; 2555322810Shselasky } 2556322810Shselasky 2557331769Shselasky cq = ib_alloc_cq(&dev->ib_dev, NULL, 128, 0, IB_POLL_SOFTIRQ); 2558331769Shselasky if (IS_ERR(cq)) { 2559331769Shselasky mlx5_ib_dbg(dev, "Couldn't create CQ for sync UMR QP\n"); 2560331769Shselasky ret = PTR_ERR(cq); 2561331769Shselasky goto error_2; 2562322810Shselasky } 2563322810Shselasky 2564331769Shselasky init_attr->send_cq = cq; 2565331769Shselasky init_attr->recv_cq = cq; 2566331769Shselasky init_attr->sq_sig_type = IB_SIGNAL_ALL_WR; 2567331769Shselasky init_attr->cap.max_send_wr = MAX_UMR_WR; 2568331769Shselasky init_attr->cap.max_send_sge = 1; 2569331769Shselasky init_attr->qp_type = MLX5_IB_QPT_REG_UMR; 2570331769Shselasky init_attr->port_num = 1; 2571331769Shselasky qp = mlx5_ib_create_qp(pd, init_attr, NULL); 2572331769Shselasky if (IS_ERR(qp)) { 2573331769Shselasky mlx5_ib_dbg(dev, "Couldn't create sync UMR QP\n"); 2574331769Shselasky ret = PTR_ERR(qp); 2575331769Shselasky goto error_3; 2576331769Shselasky } 2577331769Shselasky qp->device = &dev->ib_dev; 2578331769Shselasky qp->real_qp = qp; 2579331769Shselasky qp->uobject = NULL; 2580331769Shselasky qp->qp_type = MLX5_IB_QPT_REG_UMR; 2581331769Shselasky 2582331769Shselasky attr->qp_state = IB_QPS_INIT; 2583331769Shselasky attr->port_num = 1; 2584331769Shselasky ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_PKEY_INDEX | 2585331769Shselasky IB_QP_PORT, NULL); 2586331769Shselasky if (ret) { 2587331769Shselasky mlx5_ib_dbg(dev, "Couldn't modify UMR QP\n"); 2588331769Shselasky goto error_4; 2589331769Shselasky } 2590331769Shselasky 2591331769Shselasky memset(attr, 0, sizeof(*attr)); 2592331769Shselasky attr->qp_state = IB_QPS_RTR; 2593331769Shselasky attr->path_mtu = IB_MTU_256; 2594331769Shselasky 2595331769Shselasky ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL); 2596331769Shselasky if (ret) { 2597331769Shselasky mlx5_ib_dbg(dev, "Couldn't modify umr QP to rtr\n"); 2598331769Shselasky goto error_4; 2599331769Shselasky } 2600331769Shselasky 2601331769Shselasky memset(attr, 0, sizeof(*attr)); 2602331769Shselasky attr->qp_state = IB_QPS_RTS; 2603331769Shselasky ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL); 2604331769Shselasky if (ret) { 2605331769Shselasky mlx5_ib_dbg(dev, "Couldn't modify umr QP to rts\n"); 2606331769Shselasky goto error_4; 2607331769Shselasky } 2608331769Shselasky 2609331769Shselasky dev->umrc.qp = qp; 2610331769Shselasky dev->umrc.cq = cq; 2611322810Shselasky dev->umrc.pd = pd; 2612322810Shselasky 2613331769Shselasky sema_init(&dev->umrc.sem, MAX_UMR_WR); 2614322810Shselasky ret = mlx5_mr_cache_init(dev); 2615322810Shselasky if (ret) { 2616322810Shselasky mlx5_ib_warn(dev, "mr cache init failed %d\n", ret); 2617322810Shselasky goto error_4; 2618322810Shselasky } 2619322810Shselasky 2620331769Shselasky kfree(attr); 2621331769Shselasky kfree(init_attr); 2622331769Shselasky 2623322810Shselasky return 0; 2624322810Shselasky 2625322810Shselaskyerror_4: 2626331769Shselasky mlx5_ib_destroy_qp(qp); 2627331769Shselasky 2628331769Shselaskyerror_3: 2629331769Shselasky ib_free_cq(cq); 2630331769Shselasky 2631331769Shselaskyerror_2: 2632322810Shselasky ib_dealloc_pd(pd); 2633331769Shselasky 2634322810Shselaskyerror_0: 2635331769Shselasky kfree(attr); 2636331769Shselasky kfree(init_attr); 2637322810Shselasky return ret; 2638322810Shselasky} 2639322810Shselasky 2640322810Shselaskystatic int create_dev_resources(struct mlx5_ib_resources *devr) 2641322810Shselasky{ 2642322810Shselasky struct ib_srq_init_attr attr; 2643322810Shselasky struct mlx5_ib_dev *dev; 2644331769Shselasky struct ib_cq_init_attr cq_attr = {.cqe = 1}; 2645331769Shselasky int port; 2646322810Shselasky int ret = 0; 2647322810Shselasky 2648322810Shselasky dev = container_of(devr, struct mlx5_ib_dev, devr); 2649322810Shselasky 2650331769Shselasky mutex_init(&devr->mutex); 2651331769Shselasky 2652322810Shselasky devr->p0 = mlx5_ib_alloc_pd(&dev->ib_dev, NULL, NULL); 2653322810Shselasky if (IS_ERR(devr->p0)) { 2654322810Shselasky ret = PTR_ERR(devr->p0); 2655322810Shselasky goto error0; 2656322810Shselasky } 2657322810Shselasky devr->p0->device = &dev->ib_dev; 2658322810Shselasky devr->p0->uobject = NULL; 2659322810Shselasky atomic_set(&devr->p0->usecnt, 0); 2660322810Shselasky 2661322810Shselasky devr->c0 = mlx5_ib_create_cq(&dev->ib_dev, &cq_attr, NULL, NULL); 2662322810Shselasky if (IS_ERR(devr->c0)) { 2663322810Shselasky ret = PTR_ERR(devr->c0); 2664322810Shselasky goto error1; 2665322810Shselasky } 2666322810Shselasky devr->c0->device = &dev->ib_dev; 2667322810Shselasky devr->c0->uobject = NULL; 2668322810Shselasky devr->c0->comp_handler = NULL; 2669322810Shselasky devr->c0->event_handler = NULL; 2670322810Shselasky devr->c0->cq_context = NULL; 2671322810Shselasky atomic_set(&devr->c0->usecnt, 0); 2672322810Shselasky 2673322810Shselasky devr->x0 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL); 2674322810Shselasky if (IS_ERR(devr->x0)) { 2675322810Shselasky ret = PTR_ERR(devr->x0); 2676322810Shselasky goto error2; 2677322810Shselasky } 2678322810Shselasky devr->x0->device = &dev->ib_dev; 2679322810Shselasky devr->x0->inode = NULL; 2680322810Shselasky atomic_set(&devr->x0->usecnt, 0); 2681322810Shselasky mutex_init(&devr->x0->tgt_qp_mutex); 2682322810Shselasky INIT_LIST_HEAD(&devr->x0->tgt_qp_list); 2683322810Shselasky 2684322810Shselasky devr->x1 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL); 2685322810Shselasky if (IS_ERR(devr->x1)) { 2686322810Shselasky ret = PTR_ERR(devr->x1); 2687322810Shselasky goto error3; 2688322810Shselasky } 2689322810Shselasky devr->x1->device = &dev->ib_dev; 2690322810Shselasky devr->x1->inode = NULL; 2691322810Shselasky atomic_set(&devr->x1->usecnt, 0); 2692322810Shselasky mutex_init(&devr->x1->tgt_qp_mutex); 2693322810Shselasky INIT_LIST_HEAD(&devr->x1->tgt_qp_list); 2694322810Shselasky 2695322810Shselasky memset(&attr, 0, sizeof(attr)); 2696322810Shselasky attr.attr.max_sge = 1; 2697322810Shselasky attr.attr.max_wr = 1; 2698322810Shselasky attr.srq_type = IB_SRQT_XRC; 2699322810Shselasky attr.ext.xrc.cq = devr->c0; 2700322810Shselasky attr.ext.xrc.xrcd = devr->x0; 2701322810Shselasky 2702322810Shselasky devr->s0 = mlx5_ib_create_srq(devr->p0, &attr, NULL); 2703322810Shselasky if (IS_ERR(devr->s0)) { 2704322810Shselasky ret = PTR_ERR(devr->s0); 2705322810Shselasky goto error4; 2706322810Shselasky } 2707322810Shselasky devr->s0->device = &dev->ib_dev; 2708322810Shselasky devr->s0->pd = devr->p0; 2709322810Shselasky devr->s0->uobject = NULL; 2710322810Shselasky devr->s0->event_handler = NULL; 2711322810Shselasky devr->s0->srq_context = NULL; 2712322810Shselasky devr->s0->srq_type = IB_SRQT_XRC; 2713331769Shselasky devr->s0->ext.xrc.xrcd = devr->x0; 2714322810Shselasky devr->s0->ext.xrc.cq = devr->c0; 2715322810Shselasky atomic_inc(&devr->s0->ext.xrc.xrcd->usecnt); 2716322810Shselasky atomic_inc(&devr->s0->ext.xrc.cq->usecnt); 2717322810Shselasky atomic_inc(&devr->p0->usecnt); 2718322810Shselasky atomic_set(&devr->s0->usecnt, 0); 2719322810Shselasky 2720322810Shselasky memset(&attr, 0, sizeof(attr)); 2721322810Shselasky attr.attr.max_sge = 1; 2722322810Shselasky attr.attr.max_wr = 1; 2723322810Shselasky attr.srq_type = IB_SRQT_BASIC; 2724322810Shselasky devr->s1 = mlx5_ib_create_srq(devr->p0, &attr, NULL); 2725322810Shselasky if (IS_ERR(devr->s1)) { 2726322810Shselasky ret = PTR_ERR(devr->s1); 2727322810Shselasky goto error5; 2728322810Shselasky } 2729322810Shselasky devr->s1->device = &dev->ib_dev; 2730322810Shselasky devr->s1->pd = devr->p0; 2731322810Shselasky devr->s1->uobject = NULL; 2732322810Shselasky devr->s1->event_handler = NULL; 2733322810Shselasky devr->s1->srq_context = NULL; 2734322810Shselasky devr->s1->srq_type = IB_SRQT_BASIC; 2735322810Shselasky devr->s1->ext.xrc.cq = devr->c0; 2736322810Shselasky atomic_inc(&devr->p0->usecnt); 2737331769Shselasky atomic_set(&devr->s0->usecnt, 0); 2738322810Shselasky 2739331769Shselasky for (port = 0; port < ARRAY_SIZE(devr->ports); ++port) { 2740331769Shselasky INIT_WORK(&devr->ports[port].pkey_change_work, 2741331769Shselasky pkey_change_handler); 2742331769Shselasky devr->ports[port].devr = devr; 2743331769Shselasky } 2744331769Shselasky 2745322810Shselasky return 0; 2746322810Shselasky 2747322810Shselaskyerror5: 2748322810Shselasky mlx5_ib_destroy_srq(devr->s0); 2749322810Shselaskyerror4: 2750322810Shselasky mlx5_ib_dealloc_xrcd(devr->x1); 2751322810Shselaskyerror3: 2752322810Shselasky mlx5_ib_dealloc_xrcd(devr->x0); 2753322810Shselaskyerror2: 2754322810Shselasky mlx5_ib_destroy_cq(devr->c0); 2755322810Shselaskyerror1: 2756322810Shselasky mlx5_ib_dealloc_pd(devr->p0); 2757322810Shselaskyerror0: 2758322810Shselasky return ret; 2759322810Shselasky} 2760322810Shselasky 2761322810Shselaskystatic void destroy_dev_resources(struct mlx5_ib_resources *devr) 2762322810Shselasky{ 2763331769Shselasky struct mlx5_ib_dev *dev = 2764331769Shselasky container_of(devr, struct mlx5_ib_dev, devr); 2765331769Shselasky int port; 2766331769Shselasky 2767322810Shselasky mlx5_ib_destroy_srq(devr->s1); 2768322810Shselasky mlx5_ib_destroy_srq(devr->s0); 2769322810Shselasky mlx5_ib_dealloc_xrcd(devr->x0); 2770322810Shselasky mlx5_ib_dealloc_xrcd(devr->x1); 2771322810Shselasky mlx5_ib_destroy_cq(devr->c0); 2772322810Shselasky mlx5_ib_dealloc_pd(devr->p0); 2773331769Shselasky 2774331769Shselasky /* Make sure no change P_Key work items are still executing */ 2775331769Shselasky for (port = 0; port < dev->num_ports; ++port) 2776331769Shselasky cancel_work_sync(&devr->ports[port].pkey_change_work); 2777322810Shselasky} 2778322810Shselasky 2779325604Shselaskystatic u32 get_core_cap_flags(struct ib_device *ibdev) 2780325604Shselasky{ 2781325604Shselasky struct mlx5_ib_dev *dev = to_mdev(ibdev); 2782325604Shselasky enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, 1); 2783325604Shselasky u8 l3_type_cap = MLX5_CAP_ROCE(dev->mdev, l3_type); 2784325604Shselasky u8 roce_version_cap = MLX5_CAP_ROCE(dev->mdev, roce_version); 2785325604Shselasky u32 ret = 0; 2786325604Shselasky 2787325604Shselasky if (ll == IB_LINK_LAYER_INFINIBAND) 2788325604Shselasky return RDMA_CORE_PORT_IBA_IB; 2789325604Shselasky 2790325604Shselasky if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV4_CAP)) 2791331769Shselasky return 0; 2792325604Shselasky 2793325604Shselasky if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV6_CAP)) 2794331769Shselasky return 0; 2795325604Shselasky 2796325604Shselasky if (roce_version_cap & MLX5_ROCE_VERSION_1_CAP) 2797325604Shselasky ret |= RDMA_CORE_PORT_IBA_ROCE; 2798325604Shselasky 2799325604Shselasky if (roce_version_cap & MLX5_ROCE_VERSION_2_CAP) 2800325604Shselasky ret |= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP; 2801325604Shselasky 2802325604Shselasky return ret; 2803325604Shselasky} 2804325604Shselasky 2805325604Shselaskystatic int mlx5_port_immutable(struct ib_device *ibdev, u8 port_num, 2806325604Shselasky struct ib_port_immutable *immutable) 2807325604Shselasky{ 2808325604Shselasky struct ib_port_attr attr; 2809325604Shselasky int err; 2810325604Shselasky 2811331769Shselasky err = mlx5_ib_query_port(ibdev, port_num, &attr); 2812325604Shselasky if (err) 2813325604Shselasky return err; 2814325604Shselasky 2815325604Shselasky immutable->pkey_tbl_len = attr.pkey_tbl_len; 2816325604Shselasky immutable->gid_tbl_len = attr.gid_tbl_len; 2817325604Shselasky immutable->core_cap_flags = get_core_cap_flags(ibdev); 2818331769Shselasky immutable->max_mad_size = IB_MGMT_MAD_SIZE; 2819325604Shselasky 2820325604Shselasky return 0; 2821325604Shselasky} 2822325604Shselasky 2823331769Shselaskystatic void get_dev_fw_str(struct ib_device *ibdev, char *str, 2824331769Shselasky size_t str_len) 2825322810Shselasky{ 2826331769Shselasky struct mlx5_ib_dev *dev = 2827331769Shselasky container_of(ibdev, struct mlx5_ib_dev, ib_dev); 2828331769Shselasky snprintf(str, str_len, "%d.%d.%04d", fw_rev_maj(dev->mdev), 2829331769Shselasky fw_rev_min(dev->mdev), fw_rev_sub(dev->mdev)); 2830331769Shselasky} 2831322810Shselasky 2832331769Shselaskystatic int mlx5_roce_lag_init(struct mlx5_ib_dev *dev) 2833331769Shselasky{ 2834331769Shselasky return 0; 2835331769Shselasky} 2836322810Shselasky 2837331769Shselaskystatic void mlx5_roce_lag_cleanup(struct mlx5_ib_dev *dev) 2838331769Shselasky{ 2839331769Shselasky} 2840322810Shselasky 2841331769Shselaskystatic void mlx5_remove_roce_notifier(struct mlx5_ib_dev *dev) 2842331769Shselasky{ 2843331769Shselasky if (dev->roce.nb.notifier_call) { 2844331769Shselasky unregister_netdevice_notifier(&dev->roce.nb); 2845331769Shselasky dev->roce.nb.notifier_call = NULL; 2846322810Shselasky } 2847322810Shselasky} 2848322810Shselasky 2849331769Shselaskystatic int mlx5_enable_roce(struct mlx5_ib_dev *dev) 2850322810Shselasky{ 2851331769Shselasky VNET_ITERATOR_DECL(vnet_iter); 2852331769Shselasky struct net_device *idev; 2853322810Shselasky int err; 2854322810Shselasky 2855331769Shselasky /* Check if mlx5en net device already exists */ 2856331769Shselasky VNET_LIST_RLOCK(); 2857331769Shselasky VNET_FOREACH(vnet_iter) { 2858331769Shselasky IFNET_RLOCK(); 2859331769Shselasky CURVNET_SET_QUIET(vnet_iter); 2860331769Shselasky TAILQ_FOREACH(idev, &V_ifnet, if_link) { 2861331769Shselasky /* check if network interface belongs to mlx5en */ 2862331769Shselasky if (!mlx5_netdev_match(idev, dev->mdev, "mce")) 2863331769Shselasky continue; 2864331769Shselasky write_lock(&dev->roce.netdev_lock); 2865331769Shselasky dev->roce.netdev = idev; 2866331769Shselasky write_unlock(&dev->roce.netdev_lock); 2867331769Shselasky } 2868331769Shselasky CURVNET_RESTORE(); 2869331769Shselasky IFNET_RUNLOCK(); 2870331769Shselasky } 2871331769Shselasky VNET_LIST_RUNLOCK(); 2872322810Shselasky 2873331769Shselasky dev->roce.nb.notifier_call = mlx5_netdev_event; 2874331769Shselasky err = register_netdevice_notifier(&dev->roce.nb); 2875322810Shselasky if (err) { 2876331769Shselasky dev->roce.nb.notifier_call = NULL; 2877331769Shselasky return err; 2878322810Shselasky } 2879322810Shselasky 2880331769Shselasky err = mlx5_nic_vport_enable_roce(dev->mdev); 2881331769Shselasky if (err) 2882331769Shselasky goto err_unregister_netdevice_notifier; 2883322810Shselasky 2884331769Shselasky err = mlx5_roce_lag_init(dev); 2885331769Shselasky if (err) 2886331769Shselasky goto err_disable_roce; 2887322810Shselasky 2888331769Shselasky return 0; 2889322810Shselasky 2890331769Shselaskyerr_disable_roce: 2891331769Shselasky mlx5_nic_vport_disable_roce(dev->mdev); 2892322810Shselasky 2893331769Shselaskyerr_unregister_netdevice_notifier: 2894331769Shselasky mlx5_remove_roce_notifier(dev); 2895331769Shselasky return err; 2896322810Shselasky} 2897322810Shselasky 2898331769Shselaskystatic void mlx5_disable_roce(struct mlx5_ib_dev *dev) 2899322810Shselasky{ 2900331769Shselasky mlx5_roce_lag_cleanup(dev); 2901331769Shselasky mlx5_nic_vport_disable_roce(dev->mdev); 2902322810Shselasky} 2903322810Shselasky 2904322810Shselaskystatic void mlx5_ib_dealloc_q_port_counter(struct mlx5_ib_dev *dev, u8 port_num) 2905322810Shselasky{ 2906322810Shselasky mlx5_vport_dealloc_q_counter(dev->mdev, 2907322810Shselasky MLX5_INTERFACE_PROTOCOL_IB, 2908322810Shselasky dev->port[port_num].q_cnt_id); 2909322810Shselasky dev->port[port_num].q_cnt_id = 0; 2910322810Shselasky} 2911322810Shselasky 2912322810Shselaskystatic void mlx5_ib_dealloc_q_counters(struct mlx5_ib_dev *dev) 2913322810Shselasky{ 2914322810Shselasky unsigned int i; 2915322810Shselasky 2916322810Shselasky for (i = 0; i < dev->num_ports; i++) 2917322810Shselasky mlx5_ib_dealloc_q_port_counter(dev, i); 2918322810Shselasky} 2919322810Shselasky 2920322810Shselaskystatic int mlx5_ib_alloc_q_counters(struct mlx5_ib_dev *dev) 2921322810Shselasky{ 2922322810Shselasky int i; 2923322810Shselasky int ret; 2924322810Shselasky 2925322810Shselasky for (i = 0; i < dev->num_ports; i++) { 2926322810Shselasky ret = mlx5_vport_alloc_q_counter(dev->mdev, 2927322810Shselasky MLX5_INTERFACE_PROTOCOL_IB, 2928322810Shselasky &dev->port[i].q_cnt_id); 2929322810Shselasky if (ret) { 2930322810Shselasky mlx5_ib_warn(dev, 2931331769Shselasky "couldn't allocate queue counter for port %d, err %d\n", 2932331769Shselasky i + 1, ret); 2933322810Shselasky goto dealloc_counters; 2934322810Shselasky } 2935322810Shselasky } 2936322810Shselasky 2937322810Shselasky return 0; 2938322810Shselasky 2939322810Shselaskydealloc_counters: 2940322810Shselasky while (--i >= 0) 2941322810Shselasky mlx5_ib_dealloc_q_port_counter(dev, i); 2942322810Shselasky 2943322810Shselasky return ret; 2944322810Shselasky} 2945322810Shselasky 2946331769Shselaskystatic const char * const names[] = { 2947331769Shselasky "rx_write_requests", 2948331769Shselasky "rx_read_requests", 2949331769Shselasky "rx_atomic_requests", 2950331769Shselasky "out_of_buffer", 2951331769Shselasky "out_of_sequence", 2952331769Shselasky "duplicate_request", 2953331769Shselasky "rnr_nak_retry_err", 2954331769Shselasky "packet_seq_err", 2955331769Shselasky "implied_nak_seq_err", 2956331769Shselasky "local_ack_timeout_err", 2957322810Shselasky}; 2958322810Shselasky 2959331769Shselaskystatic const size_t stats_offsets[] = { 2960331769Shselasky MLX5_BYTE_OFF(query_q_counter_out, rx_write_requests), 2961331769Shselasky MLX5_BYTE_OFF(query_q_counter_out, rx_read_requests), 2962331769Shselasky MLX5_BYTE_OFF(query_q_counter_out, rx_atomic_requests), 2963331769Shselasky MLX5_BYTE_OFF(query_q_counter_out, out_of_buffer), 2964331769Shselasky MLX5_BYTE_OFF(query_q_counter_out, out_of_sequence), 2965331769Shselasky MLX5_BYTE_OFF(query_q_counter_out, duplicate_request), 2966331769Shselasky MLX5_BYTE_OFF(query_q_counter_out, rnr_nak_retry_err), 2967331769Shselasky MLX5_BYTE_OFF(query_q_counter_out, packet_seq_err), 2968331769Shselasky MLX5_BYTE_OFF(query_q_counter_out, implied_nak_seq_err), 2969331769Shselasky MLX5_BYTE_OFF(query_q_counter_out, local_ack_timeout_err), 2970322810Shselasky}; 2971322810Shselasky 2972331769Shselaskystatic struct rdma_hw_stats *mlx5_ib_alloc_hw_stats(struct ib_device *ibdev, 2973331769Shselasky u8 port_num) 2974322810Shselasky{ 2975331769Shselasky BUILD_BUG_ON(ARRAY_SIZE(names) != ARRAY_SIZE(stats_offsets)); 2976322810Shselasky 2977331769Shselasky /* We support only per port stats */ 2978331769Shselasky if (port_num == 0) 2979331769Shselasky return NULL; 2980322810Shselasky 2981331769Shselasky return rdma_alloc_hw_stats_struct(names, ARRAY_SIZE(names), 2982331769Shselasky RDMA_HW_STATS_DEFAULT_LIFESPAN); 2983322810Shselasky} 2984322810Shselasky 2985331769Shselaskystatic int mlx5_ib_get_hw_stats(struct ib_device *ibdev, 2986331769Shselasky struct rdma_hw_stats *stats, 2987331769Shselasky u8 port, int index) 2988322810Shselasky{ 2989331769Shselasky struct mlx5_ib_dev *dev = to_mdev(ibdev); 2990322810Shselasky int outlen = MLX5_ST_SZ_BYTES(query_q_counter_out); 2991322810Shselasky void *out; 2992331769Shselasky __be32 val; 2993322810Shselasky int ret; 2994331769Shselasky int i; 2995322810Shselasky 2996331769Shselasky if (!port || !stats) 2997331769Shselasky return -ENOSYS; 2998331769Shselasky 2999322810Shselasky out = mlx5_vzalloc(outlen); 3000322810Shselasky if (!out) 3001322810Shselasky return -ENOMEM; 3002322810Shselasky 3003331769Shselasky ret = mlx5_vport_query_q_counter(dev->mdev, 3004331769Shselasky dev->port[port - 1].q_cnt_id, 0, 3005331769Shselasky out, outlen); 3006322810Shselasky if (ret) 3007322810Shselasky goto free; 3008322810Shselasky 3009331769Shselasky for (i = 0; i < ARRAY_SIZE(names); i++) { 3010331769Shselasky val = *(__be32 *)(out + stats_offsets[i]); 3011331769Shselasky stats->value[i] = (u64)be32_to_cpu(val); 3012331769Shselasky } 3013322810Shselaskyfree: 3014331769Shselasky kvfree(out); 3015331769Shselasky return ARRAY_SIZE(names); 3016322810Shselasky} 3017322810Shselasky 3018322810Shselaskystatic void *mlx5_ib_add(struct mlx5_core_dev *mdev) 3019322810Shselasky{ 3020322810Shselasky struct mlx5_ib_dev *dev; 3021331769Shselasky enum rdma_link_layer ll; 3022331769Shselasky int port_type_cap; 3023322810Shselasky int err; 3024322810Shselasky int i; 3025322810Shselasky 3026331769Shselasky port_type_cap = MLX5_CAP_GEN(mdev, port_type); 3027331769Shselasky ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap); 3028331769Shselasky 3029331769Shselasky if ((ll == IB_LINK_LAYER_ETHERNET) && !MLX5_CAP_GEN(mdev, roce)) 3030331769Shselasky return NULL; 3031331769Shselasky 3032322810Shselasky dev = (struct mlx5_ib_dev *)ib_alloc_device(sizeof(*dev)); 3033322810Shselasky if (!dev) 3034322810Shselasky return NULL; 3035322810Shselasky 3036322810Shselasky dev->mdev = mdev; 3037322810Shselasky 3038322810Shselasky dev->port = kcalloc(MLX5_CAP_GEN(mdev, num_ports), sizeof(*dev->port), 3039331769Shselasky GFP_KERNEL); 3040322810Shselasky if (!dev->port) 3041322810Shselasky goto err_dealloc; 3042322810Shselasky 3043331769Shselasky rwlock_init(&dev->roce.netdev_lock); 3044322810Shselasky err = get_port_caps(dev); 3045322810Shselasky if (err) 3046322810Shselasky goto err_free_port; 3047322810Shselasky 3048322810Shselasky if (mlx5_use_mad_ifc(dev)) 3049322810Shselasky get_ext_port_caps(dev); 3050322810Shselasky 3051322810Shselasky MLX5_INIT_DOORBELL_LOCK(&dev->uar_lock); 3052322810Shselasky 3053337099Shselasky snprintf(dev->ib_dev.name, IB_DEVICE_NAME_MAX, "mlx5_%d", device_get_unit(mdev->pdev->dev.bsddev)); 3054322810Shselasky dev->ib_dev.owner = THIS_MODULE; 3055322810Shselasky dev->ib_dev.node_type = RDMA_NODE_IB_CA; 3056331769Shselasky dev->ib_dev.local_dma_lkey = 0 /* not supported for now */; 3057322810Shselasky dev->num_ports = MLX5_CAP_GEN(mdev, num_ports); 3058322810Shselasky dev->ib_dev.phys_port_cnt = dev->num_ports; 3059322810Shselasky dev->ib_dev.num_comp_vectors = 3060322810Shselasky dev->mdev->priv.eq_table.num_comp_vectors; 3061322810Shselasky dev->ib_dev.dma_device = &mdev->pdev->dev; 3062322810Shselasky 3063322810Shselasky dev->ib_dev.uverbs_abi_ver = MLX5_IB_UVERBS_ABI_VERSION; 3064322810Shselasky dev->ib_dev.uverbs_cmd_mask = 3065322810Shselasky (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) | 3066322810Shselasky (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) | 3067322810Shselasky (1ull << IB_USER_VERBS_CMD_QUERY_PORT) | 3068322810Shselasky (1ull << IB_USER_VERBS_CMD_ALLOC_PD) | 3069322810Shselasky (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) | 3070331784Shselasky (1ull << IB_USER_VERBS_CMD_CREATE_AH) | 3071331784Shselasky (1ull << IB_USER_VERBS_CMD_DESTROY_AH) | 3072322810Shselasky (1ull << IB_USER_VERBS_CMD_REG_MR) | 3073331769Shselasky (1ull << IB_USER_VERBS_CMD_REREG_MR) | 3074322810Shselasky (1ull << IB_USER_VERBS_CMD_DEREG_MR) | 3075322810Shselasky (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) | 3076322810Shselasky (1ull << IB_USER_VERBS_CMD_CREATE_CQ) | 3077322810Shselasky (1ull << IB_USER_VERBS_CMD_RESIZE_CQ) | 3078322810Shselasky (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) | 3079322810Shselasky (1ull << IB_USER_VERBS_CMD_CREATE_QP) | 3080322810Shselasky (1ull << IB_USER_VERBS_CMD_MODIFY_QP) | 3081322810Shselasky (1ull << IB_USER_VERBS_CMD_QUERY_QP) | 3082322810Shselasky (1ull << IB_USER_VERBS_CMD_DESTROY_QP) | 3083322810Shselasky (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST) | 3084322810Shselasky (1ull << IB_USER_VERBS_CMD_DETACH_MCAST) | 3085322810Shselasky (1ull << IB_USER_VERBS_CMD_CREATE_SRQ) | 3086322810Shselasky (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ) | 3087322810Shselasky (1ull << IB_USER_VERBS_CMD_QUERY_SRQ) | 3088322810Shselasky (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ) | 3089322810Shselasky (1ull << IB_USER_VERBS_CMD_CREATE_XSRQ) | 3090322810Shselasky (1ull << IB_USER_VERBS_CMD_OPEN_QP); 3091331769Shselasky dev->ib_dev.uverbs_ex_cmd_mask = 3092331769Shselasky (1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE) | 3093331769Shselasky (1ull << IB_USER_VERBS_EX_CMD_CREATE_CQ) | 3094331769Shselasky (1ull << IB_USER_VERBS_EX_CMD_CREATE_QP); 3095322810Shselasky 3096322810Shselasky dev->ib_dev.query_device = mlx5_ib_query_device; 3097322810Shselasky dev->ib_dev.query_port = mlx5_ib_query_port; 3098322810Shselasky dev->ib_dev.get_link_layer = mlx5_ib_port_link_layer; 3099331769Shselasky if (ll == IB_LINK_LAYER_ETHERNET) 3100331769Shselasky dev->ib_dev.get_netdev = mlx5_ib_get_netdev; 3101322810Shselasky dev->ib_dev.query_gid = mlx5_ib_query_gid; 3102331769Shselasky dev->ib_dev.add_gid = mlx5_ib_add_gid; 3103331769Shselasky dev->ib_dev.del_gid = mlx5_ib_del_gid; 3104322810Shselasky dev->ib_dev.query_pkey = mlx5_ib_query_pkey; 3105322810Shselasky dev->ib_dev.modify_device = mlx5_ib_modify_device; 3106322810Shselasky dev->ib_dev.modify_port = mlx5_ib_modify_port; 3107322810Shselasky dev->ib_dev.alloc_ucontext = mlx5_ib_alloc_ucontext; 3108322810Shselasky dev->ib_dev.dealloc_ucontext = mlx5_ib_dealloc_ucontext; 3109322810Shselasky dev->ib_dev.mmap = mlx5_ib_mmap; 3110322810Shselasky dev->ib_dev.alloc_pd = mlx5_ib_alloc_pd; 3111322810Shselasky dev->ib_dev.dealloc_pd = mlx5_ib_dealloc_pd; 3112322810Shselasky dev->ib_dev.create_ah = mlx5_ib_create_ah; 3113322810Shselasky dev->ib_dev.query_ah = mlx5_ib_query_ah; 3114322810Shselasky dev->ib_dev.destroy_ah = mlx5_ib_destroy_ah; 3115322810Shselasky dev->ib_dev.create_srq = mlx5_ib_create_srq; 3116322810Shselasky dev->ib_dev.modify_srq = mlx5_ib_modify_srq; 3117322810Shselasky dev->ib_dev.query_srq = mlx5_ib_query_srq; 3118322810Shselasky dev->ib_dev.destroy_srq = mlx5_ib_destroy_srq; 3119322810Shselasky dev->ib_dev.post_srq_recv = mlx5_ib_post_srq_recv; 3120322810Shselasky dev->ib_dev.create_qp = mlx5_ib_create_qp; 3121322810Shselasky dev->ib_dev.modify_qp = mlx5_ib_modify_qp; 3122322810Shselasky dev->ib_dev.query_qp = mlx5_ib_query_qp; 3123322810Shselasky dev->ib_dev.destroy_qp = mlx5_ib_destroy_qp; 3124322810Shselasky dev->ib_dev.post_send = mlx5_ib_post_send; 3125322810Shselasky dev->ib_dev.post_recv = mlx5_ib_post_recv; 3126322810Shselasky dev->ib_dev.create_cq = mlx5_ib_create_cq; 3127322810Shselasky dev->ib_dev.modify_cq = mlx5_ib_modify_cq; 3128322810Shselasky dev->ib_dev.resize_cq = mlx5_ib_resize_cq; 3129322810Shselasky dev->ib_dev.destroy_cq = mlx5_ib_destroy_cq; 3130322810Shselasky dev->ib_dev.poll_cq = mlx5_ib_poll_cq; 3131322810Shselasky dev->ib_dev.req_notify_cq = mlx5_ib_arm_cq; 3132322810Shselasky dev->ib_dev.get_dma_mr = mlx5_ib_get_dma_mr; 3133322810Shselasky dev->ib_dev.reg_user_mr = mlx5_ib_reg_user_mr; 3134331769Shselasky dev->ib_dev.rereg_user_mr = mlx5_ib_rereg_user_mr; 3135322810Shselasky dev->ib_dev.reg_phys_mr = mlx5_ib_reg_phys_mr; 3136322810Shselasky dev->ib_dev.dereg_mr = mlx5_ib_dereg_mr; 3137322810Shselasky dev->ib_dev.attach_mcast = mlx5_ib_mcg_attach; 3138322810Shselasky dev->ib_dev.detach_mcast = mlx5_ib_mcg_detach; 3139322810Shselasky dev->ib_dev.process_mad = mlx5_ib_process_mad; 3140331769Shselasky dev->ib_dev.alloc_mr = mlx5_ib_alloc_mr; 3141331769Shselasky dev->ib_dev.map_mr_sg = mlx5_ib_map_mr_sg; 3142331769Shselasky dev->ib_dev.check_mr_status = mlx5_ib_check_mr_status; 3143325604Shselasky dev->ib_dev.get_port_immutable = mlx5_port_immutable; 3144331769Shselasky dev->ib_dev.get_dev_fw_str = get_dev_fw_str; 3145331769Shselasky if (mlx5_core_is_pf(mdev)) { 3146331769Shselasky dev->ib_dev.get_vf_config = mlx5_ib_get_vf_config; 3147331769Shselasky dev->ib_dev.set_vf_link_state = mlx5_ib_set_vf_link_state; 3148331769Shselasky dev->ib_dev.get_vf_stats = mlx5_ib_get_vf_stats; 3149331769Shselasky dev->ib_dev.set_vf_guid = mlx5_ib_set_vf_guid; 3150331769Shselasky } 3151322810Shselasky 3152347801Shselasky dev->ib_dev.disassociate_ucontext = mlx5_ib_disassociate_ucontext; 3153347801Shselasky 3154331769Shselasky mlx5_ib_internal_fill_odp_caps(dev); 3155331769Shselasky 3156331769Shselasky if (MLX5_CAP_GEN(mdev, imaicl)) { 3157331769Shselasky dev->ib_dev.alloc_mw = mlx5_ib_alloc_mw; 3158331769Shselasky dev->ib_dev.dealloc_mw = mlx5_ib_dealloc_mw; 3159331769Shselasky dev->ib_dev.uverbs_cmd_mask |= 3160331769Shselasky (1ull << IB_USER_VERBS_CMD_ALLOC_MW) | 3161331769Shselasky (1ull << IB_USER_VERBS_CMD_DEALLOC_MW); 3162331769Shselasky } 3163331769Shselasky 3164331769Shselasky if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt) && 3165331769Shselasky MLX5_CAP_GEN(dev->mdev, retransmission_q_counters)) { 3166331769Shselasky dev->ib_dev.get_hw_stats = mlx5_ib_get_hw_stats; 3167331769Shselasky dev->ib_dev.alloc_hw_stats = mlx5_ib_alloc_hw_stats; 3168331769Shselasky } 3169331769Shselasky 3170322810Shselasky if (MLX5_CAP_GEN(mdev, xrc)) { 3171322810Shselasky dev->ib_dev.alloc_xrcd = mlx5_ib_alloc_xrcd; 3172322810Shselasky dev->ib_dev.dealloc_xrcd = mlx5_ib_dealloc_xrcd; 3173322810Shselasky dev->ib_dev.uverbs_cmd_mask |= 3174322810Shselasky (1ull << IB_USER_VERBS_CMD_OPEN_XRCD) | 3175322810Shselasky (1ull << IB_USER_VERBS_CMD_CLOSE_XRCD); 3176322810Shselasky } 3177322810Shselasky 3178331769Shselasky if (mlx5_ib_port_link_layer(&dev->ib_dev, 1) == 3179331769Shselasky IB_LINK_LAYER_ETHERNET) { 3180331769Shselasky dev->ib_dev.create_flow = mlx5_ib_create_flow; 3181331769Shselasky dev->ib_dev.destroy_flow = mlx5_ib_destroy_flow; 3182331769Shselasky dev->ib_dev.create_wq = mlx5_ib_create_wq; 3183331769Shselasky dev->ib_dev.modify_wq = mlx5_ib_modify_wq; 3184331769Shselasky dev->ib_dev.destroy_wq = mlx5_ib_destroy_wq; 3185331769Shselasky dev->ib_dev.create_rwq_ind_table = mlx5_ib_create_rwq_ind_table; 3186331769Shselasky dev->ib_dev.destroy_rwq_ind_table = mlx5_ib_destroy_rwq_ind_table; 3187331769Shselasky dev->ib_dev.uverbs_ex_cmd_mask |= 3188331769Shselasky (1ull << IB_USER_VERBS_EX_CMD_CREATE_FLOW) | 3189331769Shselasky (1ull << IB_USER_VERBS_EX_CMD_DESTROY_FLOW) | 3190331769Shselasky (1ull << IB_USER_VERBS_EX_CMD_CREATE_WQ) | 3191331769Shselasky (1ull << IB_USER_VERBS_EX_CMD_MODIFY_WQ) | 3192331769Shselasky (1ull << IB_USER_VERBS_EX_CMD_DESTROY_WQ) | 3193331769Shselasky (1ull << IB_USER_VERBS_EX_CMD_CREATE_RWQ_IND_TBL) | 3194331769Shselasky (1ull << IB_USER_VERBS_EX_CMD_DESTROY_RWQ_IND_TBL); 3195331769Shselasky } 3196322810Shselasky err = init_node_data(dev); 3197322810Shselasky if (err) 3198331769Shselasky goto err_free_port; 3199322810Shselasky 3200331769Shselasky mutex_init(&dev->flow_db.lock); 3201322810Shselasky mutex_init(&dev->cap_mask_mutex); 3202322810Shselasky INIT_LIST_HEAD(&dev->qp_list); 3203322810Shselasky spin_lock_init(&dev->reset_flow_resource_lock); 3204322810Shselasky 3205331769Shselasky if (ll == IB_LINK_LAYER_ETHERNET) { 3206331769Shselasky err = mlx5_enable_roce(dev); 3207331769Shselasky if (err) 3208331769Shselasky goto err_free_port; 3209331769Shselasky } 3210331769Shselasky 3211322810Shselasky err = create_dev_resources(&dev->devr); 3212322810Shselasky if (err) 3213322810Shselasky goto err_disable_roce; 3214322810Shselasky 3215331769Shselasky err = mlx5_ib_odp_init_one(dev); 3216331769Shselasky if (err) 3217331769Shselasky goto err_rsrc; 3218322810Shselasky 3219322810Shselasky err = mlx5_ib_alloc_q_counters(dev); 3220322810Shselasky if (err) 3221322810Shselasky goto err_odp; 3222322810Shselasky 3223322810Shselasky err = ib_register_device(&dev->ib_dev, NULL); 3224322810Shselasky if (err) 3225322810Shselasky goto err_q_cnt; 3226322810Shselasky 3227322810Shselasky err = create_umr_res(dev); 3228322810Shselasky if (err) 3229322810Shselasky goto err_dev; 3230322810Shselasky 3231322810Shselasky for (i = 0; i < ARRAY_SIZE(mlx5_class_attributes); i++) { 3232322810Shselasky err = device_create_file(&dev->ib_dev.dev, 3233322810Shselasky mlx5_class_attributes[i]); 3234322810Shselasky if (err) 3235331769Shselasky goto err_umrc; 3236322810Shselasky } 3237322810Shselasky 3238331808Shselasky err = mlx5_ib_init_congestion(dev); 3239331808Shselasky if (err) 3240331808Shselasky goto err_umrc; 3241331808Shselasky 3242322810Shselasky dev->ib_active = true; 3243322810Shselasky 3244322810Shselasky return dev; 3245322810Shselasky 3246331769Shselaskyerr_umrc: 3247322810Shselasky destroy_umrc_res(dev); 3248322810Shselasky 3249322810Shselaskyerr_dev: 3250322810Shselasky ib_unregister_device(&dev->ib_dev); 3251322810Shselasky 3252322810Shselaskyerr_q_cnt: 3253322810Shselasky mlx5_ib_dealloc_q_counters(dev); 3254322810Shselasky 3255322810Shselaskyerr_odp: 3256331769Shselasky mlx5_ib_odp_remove_one(dev); 3257331769Shselasky 3258331769Shselaskyerr_rsrc: 3259322810Shselasky destroy_dev_resources(&dev->devr); 3260322810Shselasky 3261322810Shselaskyerr_disable_roce: 3262331769Shselasky if (ll == IB_LINK_LAYER_ETHERNET) { 3263331769Shselasky mlx5_disable_roce(dev); 3264331769Shselasky mlx5_remove_roce_notifier(dev); 3265331769Shselasky } 3266331769Shselasky 3267322810Shselaskyerr_free_port: 3268322810Shselasky kfree(dev->port); 3269322810Shselasky 3270322810Shselaskyerr_dealloc: 3271322810Shselasky ib_dealloc_device((struct ib_device *)dev); 3272322810Shselasky 3273322810Shselasky return NULL; 3274322810Shselasky} 3275322810Shselasky 3276322810Shselaskystatic void mlx5_ib_remove(struct mlx5_core_dev *mdev, void *context) 3277322810Shselasky{ 3278322810Shselasky struct mlx5_ib_dev *dev = context; 3279331769Shselasky enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev, 1); 3280322810Shselasky 3281331808Shselasky mlx5_ib_cleanup_congestion(dev); 3282331769Shselasky mlx5_remove_roce_notifier(dev); 3283331769Shselasky ib_unregister_device(&dev->ib_dev); 3284322810Shselasky mlx5_ib_dealloc_q_counters(dev); 3285322810Shselasky destroy_umrc_res(dev); 3286331769Shselasky mlx5_ib_odp_remove_one(dev); 3287322810Shselasky destroy_dev_resources(&dev->devr); 3288331769Shselasky if (ll == IB_LINK_LAYER_ETHERNET) 3289331769Shselasky mlx5_disable_roce(dev); 3290322810Shselasky kfree(dev->port); 3291322810Shselasky ib_dealloc_device(&dev->ib_dev); 3292322810Shselasky} 3293322810Shselasky 3294322810Shselaskystatic struct mlx5_interface mlx5_ib_interface = { 3295322810Shselasky .add = mlx5_ib_add, 3296322810Shselasky .remove = mlx5_ib_remove, 3297322810Shselasky .event = mlx5_ib_event, 3298322810Shselasky .protocol = MLX5_INTERFACE_PROTOCOL_IB, 3299322810Shselasky}; 3300322810Shselasky 3301322810Shselaskystatic int __init mlx5_ib_init(void) 3302322810Shselasky{ 3303322810Shselasky int err; 3304322810Shselasky 3305322810Shselasky if (deprecated_prof_sel != 2) 3306331769Shselasky pr_warn("prof_sel is deprecated for mlx5_ib, set it for mlx5_core\n"); 3307322810Shselasky 3308331769Shselasky err = mlx5_ib_odp_init(); 3309331769Shselasky if (err) 3310331769Shselasky return err; 3311331769Shselasky 3312322810Shselasky err = mlx5_register_interface(&mlx5_ib_interface); 3313322810Shselasky if (err) 3314322810Shselasky goto clean_odp; 3315322810Shselasky 3316322810Shselasky return err; 3317322810Shselasky 3318322810Shselaskyclean_odp: 3319331769Shselasky mlx5_ib_odp_cleanup(); 3320322810Shselasky return err; 3321322810Shselasky} 3322322810Shselasky 3323322810Shselaskystatic void __exit mlx5_ib_cleanup(void) 3324322810Shselasky{ 3325322810Shselasky mlx5_unregister_interface(&mlx5_ib_interface); 3326331769Shselasky mlx5_ib_odp_cleanup(); 3327322810Shselasky} 3328322810Shselasky 3329341948Shselaskystatic void 3330341948Shselaskymlx5_ib_show_version(void __unused *arg) 3331341948Shselasky{ 3332341948Shselasky 3333341948Shselasky printf("%s", mlx5_version); 3334341948Shselasky} 3335341948ShselaskySYSINIT(mlx5_ib_show_version, SI_SUB_DRIVERS, SI_ORDER_ANY, mlx5_ib_show_version, NULL); 3336341948Shselasky 3337322810Shselaskymodule_init_order(mlx5_ib_init, SI_ORDER_THIRD); 3338322810Shselaskymodule_exit_order(mlx5_ib_cleanup, SI_ORDER_THIRD); 3339