mlx5_ib_main.c revision 341956
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 341956 2018-12-12 12:35:51Z 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 55337101Shselasky#define DRIVER_VERSION "3.4.2" 56337101Shselasky#endif 57337101Shselasky#define DRIVER_RELDATE "July 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 1338331769Shselaskystatic inline char *mmap_cmd2str(enum mlx5_ib_mmap_cmd cmd) 1339331769Shselasky{ 1340331769Shselasky switch (cmd) { 1341331769Shselasky case MLX5_IB_MMAP_WC_PAGE: 1342331769Shselasky return "WC"; 1343331769Shselasky case MLX5_IB_MMAP_REGULAR_PAGE: 1344331769Shselasky return "best effort WC"; 1345331769Shselasky case MLX5_IB_MMAP_NC_PAGE: 1346331769Shselasky return "NC"; 1347331769Shselasky default: 1348331769Shselasky return NULL; 1349331769Shselasky } 1350331769Shselasky} 1351331769Shselasky 1352331769Shselaskystatic int uar_mmap(struct mlx5_ib_dev *dev, enum mlx5_ib_mmap_cmd cmd, 1353331769Shselasky struct vm_area_struct *vma, 1354322810Shselasky struct mlx5_ib_ucontext *context) 1355322810Shselasky{ 1356331769Shselasky struct mlx5_uuar_info *uuari = &context->uuari; 1357331769Shselasky int err; 1358322810Shselasky unsigned long idx; 1359331769Shselasky phys_addr_t pfn, pa; 1360331769Shselasky pgprot_t prot; 1361322810Shselasky 1362331769Shselasky switch (cmd) { 1363331769Shselasky case MLX5_IB_MMAP_WC_PAGE: 1364331769Shselasky/* Some architectures don't support WC memory */ 1365331769Shselasky#if defined(CONFIG_X86) 1366331769Shselasky if (!pat_enabled()) 1367331769Shselasky return -EPERM; 1368331769Shselasky#elif !(defined(CONFIG_PPC) || (defined(CONFIG_ARM) && defined(CONFIG_MMU))) 1369331769Shselasky return -EPERM; 1370331769Shselasky#endif 1371331769Shselasky /* fall through */ 1372331769Shselasky case MLX5_IB_MMAP_REGULAR_PAGE: 1373331769Shselasky /* For MLX5_IB_MMAP_REGULAR_PAGE do the best effort to get WC */ 1374331769Shselasky prot = pgprot_writecombine(vma->vm_page_prot); 1375331769Shselasky break; 1376331769Shselasky case MLX5_IB_MMAP_NC_PAGE: 1377331769Shselasky prot = pgprot_noncached(vma->vm_page_prot); 1378331769Shselasky break; 1379331769Shselasky default: 1380322810Shselasky return -EINVAL; 1381322810Shselasky } 1382322810Shselasky 1383331769Shselasky if (vma->vm_end - vma->vm_start != PAGE_SIZE) 1384331769Shselasky return -EINVAL; 1385331769Shselasky 1386322810Shselasky idx = get_index(vma->vm_pgoff); 1387331769Shselasky if (idx >= uuari->num_uars) 1388322810Shselasky return -EINVAL; 1389322810Shselasky 1390322810Shselasky pfn = uar_index2pfn(dev, uuari->uars[idx].index); 1391331769Shselasky mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn %pa\n", idx, &pfn); 1392322810Shselasky 1393322810Shselasky vma->vm_page_prot = prot; 1394331769Shselasky err = io_remap_pfn_range(vma, vma->vm_start, pfn, 1395331769Shselasky PAGE_SIZE, vma->vm_page_prot); 1396331769Shselasky if (err) { 1397331769Shselasky mlx5_ib_err(dev, "io_remap_pfn_range failed with error=%d, vm_start=0x%llx, pfn=%pa, mmap_cmd=%s\n", 1398331769Shselasky err, (unsigned long long)vma->vm_start, &pfn, mmap_cmd2str(cmd)); 1399322810Shselasky return -EAGAIN; 1400322810Shselasky } 1401322810Shselasky 1402331769Shselasky pa = pfn << PAGE_SHIFT; 1403331769Shselasky mlx5_ib_dbg(dev, "mapped %s at 0x%llx, PA %pa\n", mmap_cmd2str(cmd), 1404331769Shselasky (unsigned long long)vma->vm_start, &pa); 1405322810Shselasky 1406331769Shselasky return mlx5_ib_set_vma_data(vma, context); 1407322810Shselasky} 1408322810Shselasky 1409322810Shselaskystatic int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma) 1410322810Shselasky{ 1411322810Shselasky struct mlx5_ib_ucontext *context = to_mucontext(ibcontext); 1412322810Shselasky struct mlx5_ib_dev *dev = to_mdev(ibcontext->device); 1413322810Shselasky unsigned long command; 1414331769Shselasky phys_addr_t pfn; 1415322810Shselasky 1416322810Shselasky command = get_command(vma->vm_pgoff); 1417322810Shselasky switch (command) { 1418331769Shselasky case MLX5_IB_MMAP_WC_PAGE: 1419331769Shselasky case MLX5_IB_MMAP_NC_PAGE: 1420322810Shselasky case MLX5_IB_MMAP_REGULAR_PAGE: 1421331769Shselasky return uar_mmap(dev, command, vma, context); 1422322810Shselasky 1423331769Shselasky case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES: 1424331769Shselasky return -ENOSYS; 1425322810Shselasky 1426331769Shselasky case MLX5_IB_MMAP_CORE_CLOCK: 1427331769Shselasky if (vma->vm_end - vma->vm_start != PAGE_SIZE) 1428331769Shselasky return -EINVAL; 1429322810Shselasky 1430331769Shselasky if (vma->vm_flags & VM_WRITE) 1431331769Shselasky return -EPERM; 1432331769Shselasky 1433331769Shselasky /* Don't expose to user-space information it shouldn't have */ 1434331769Shselasky if (PAGE_SIZE > 4096) 1435331769Shselasky return -EOPNOTSUPP; 1436331769Shselasky 1437331769Shselasky vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); 1438331769Shselasky pfn = (dev->mdev->iseg_base + 1439331769Shselasky offsetof(struct mlx5_init_seg, internal_timer_h)) >> 1440331769Shselasky PAGE_SHIFT; 1441331769Shselasky if (io_remap_pfn_range(vma, vma->vm_start, pfn, 1442331769Shselasky PAGE_SIZE, vma->vm_page_prot)) 1443331769Shselasky return -EAGAIN; 1444331769Shselasky 1445331769Shselasky mlx5_ib_dbg(dev, "mapped internal timer at 0x%llx, PA 0x%llx\n", 1446331769Shselasky (unsigned long long)vma->vm_start, 1447331769Shselasky (unsigned long long)pfn << PAGE_SHIFT); 1448322810Shselasky break; 1449322810Shselasky 1450322810Shselasky default: 1451322810Shselasky return -EINVAL; 1452322810Shselasky } 1453322810Shselasky 1454322810Shselasky return 0; 1455322810Shselasky} 1456322810Shselasky 1457322810Shselaskystatic struct ib_pd *mlx5_ib_alloc_pd(struct ib_device *ibdev, 1458322810Shselasky struct ib_ucontext *context, 1459322810Shselasky struct ib_udata *udata) 1460322810Shselasky{ 1461322810Shselasky struct mlx5_ib_alloc_pd_resp resp; 1462322810Shselasky struct mlx5_ib_pd *pd; 1463322810Shselasky int err; 1464322810Shselasky 1465322810Shselasky pd = kmalloc(sizeof(*pd), GFP_KERNEL); 1466322810Shselasky if (!pd) 1467322810Shselasky return ERR_PTR(-ENOMEM); 1468322810Shselasky 1469322810Shselasky err = mlx5_core_alloc_pd(to_mdev(ibdev)->mdev, &pd->pdn); 1470322810Shselasky if (err) { 1471322810Shselasky kfree(pd); 1472322810Shselasky return ERR_PTR(err); 1473322810Shselasky } 1474322810Shselasky 1475322810Shselasky if (context) { 1476322810Shselasky resp.pdn = pd->pdn; 1477322810Shselasky if (ib_copy_to_udata(udata, &resp, sizeof(resp))) { 1478322810Shselasky mlx5_core_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn); 1479322810Shselasky kfree(pd); 1480322810Shselasky return ERR_PTR(-EFAULT); 1481322810Shselasky } 1482322810Shselasky } 1483322810Shselasky 1484322810Shselasky return &pd->ibpd; 1485322810Shselasky} 1486322810Shselasky 1487322810Shselaskystatic int mlx5_ib_dealloc_pd(struct ib_pd *pd) 1488322810Shselasky{ 1489322810Shselasky struct mlx5_ib_dev *mdev = to_mdev(pd->device); 1490322810Shselasky struct mlx5_ib_pd *mpd = to_mpd(pd); 1491322810Shselasky 1492322810Shselasky mlx5_core_dealloc_pd(mdev->mdev, mpd->pdn); 1493322810Shselasky kfree(mpd); 1494322810Shselasky 1495322810Shselasky return 0; 1496322810Shselasky} 1497322810Shselasky 1498331769Shselaskyenum { 1499331769Shselasky MATCH_CRITERIA_ENABLE_OUTER_BIT, 1500331769Shselasky MATCH_CRITERIA_ENABLE_MISC_BIT, 1501331769Shselasky MATCH_CRITERIA_ENABLE_INNER_BIT 1502331769Shselasky}; 1503331769Shselasky 1504331769Shselasky#define HEADER_IS_ZERO(match_criteria, headers) \ 1505331769Shselasky !(memchr_inv(MLX5_ADDR_OF(fte_match_param, match_criteria, headers), \ 1506331769Shselasky 0, MLX5_FLD_SZ_BYTES(fte_match_param, headers))) \ 1507331769Shselasky 1508331769Shselaskystatic u8 get_match_criteria_enable(u32 *match_criteria) 1509331769Shselasky{ 1510331769Shselasky u8 match_criteria_enable; 1511331769Shselasky 1512331769Shselasky match_criteria_enable = 1513331769Shselasky (!HEADER_IS_ZERO(match_criteria, outer_headers)) << 1514331769Shselasky MATCH_CRITERIA_ENABLE_OUTER_BIT; 1515331769Shselasky match_criteria_enable |= 1516331769Shselasky (!HEADER_IS_ZERO(match_criteria, misc_parameters)) << 1517331769Shselasky MATCH_CRITERIA_ENABLE_MISC_BIT; 1518331769Shselasky match_criteria_enable |= 1519331769Shselasky (!HEADER_IS_ZERO(match_criteria, inner_headers)) << 1520331769Shselasky MATCH_CRITERIA_ENABLE_INNER_BIT; 1521331769Shselasky 1522331769Shselasky return match_criteria_enable; 1523331769Shselasky} 1524331769Shselasky 1525331769Shselaskystatic void set_proto(void *outer_c, void *outer_v, u8 mask, u8 val) 1526331769Shselasky{ 1527331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_protocol, mask); 1528331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_protocol, val); 1529331769Shselasky} 1530331769Shselasky 1531331769Shselaskystatic void set_tos(void *outer_c, void *outer_v, u8 mask, u8 val) 1532331769Shselasky{ 1533331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_ecn, mask); 1534331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_ecn, val); 1535331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_dscp, mask >> 2); 1536331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_dscp, val >> 2); 1537331769Shselasky} 1538331769Shselasky 1539331769Shselasky#define LAST_ETH_FIELD vlan_tag 1540331769Shselasky#define LAST_IB_FIELD sl 1541331769Shselasky#define LAST_IPV4_FIELD tos 1542331769Shselasky#define LAST_IPV6_FIELD traffic_class 1543331769Shselasky#define LAST_TCP_UDP_FIELD src_port 1544331769Shselasky 1545331769Shselasky/* Field is the last supported field */ 1546331769Shselasky#define FIELDS_NOT_SUPPORTED(filter, field)\ 1547331769Shselasky memchr_inv((void *)&filter.field +\ 1548331769Shselasky sizeof(filter.field), 0,\ 1549331769Shselasky sizeof(filter) -\ 1550331769Shselasky offsetof(typeof(filter), field) -\ 1551331769Shselasky sizeof(filter.field)) 1552331769Shselasky 1553331769Shselaskystatic int parse_flow_attr(u32 *match_c, u32 *match_v, 1554331769Shselasky const union ib_flow_spec *ib_spec) 1555331769Shselasky{ 1556331769Shselasky void *outer_headers_c = MLX5_ADDR_OF(fte_match_param, match_c, 1557331769Shselasky outer_headers); 1558331769Shselasky void *outer_headers_v = MLX5_ADDR_OF(fte_match_param, match_v, 1559331769Shselasky outer_headers); 1560331769Shselasky void *misc_params_c = MLX5_ADDR_OF(fte_match_param, match_c, 1561331769Shselasky misc_parameters); 1562331769Shselasky void *misc_params_v = MLX5_ADDR_OF(fte_match_param, match_v, 1563331769Shselasky misc_parameters); 1564331769Shselasky 1565331769Shselasky switch (ib_spec->type) { 1566331769Shselasky case IB_FLOW_SPEC_ETH: 1567331769Shselasky if (FIELDS_NOT_SUPPORTED(ib_spec->eth.mask, LAST_ETH_FIELD)) 1568331769Shselasky return -ENOTSUPP; 1569331769Shselasky 1570331769Shselasky ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_c, 1571331769Shselasky dmac_47_16), 1572331769Shselasky ib_spec->eth.mask.dst_mac); 1573331769Shselasky ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_v, 1574331769Shselasky dmac_47_16), 1575331769Shselasky ib_spec->eth.val.dst_mac); 1576331769Shselasky 1577331769Shselasky ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_c, 1578331769Shselasky smac_47_16), 1579331769Shselasky ib_spec->eth.mask.src_mac); 1580331769Shselasky ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_v, 1581331769Shselasky smac_47_16), 1582331769Shselasky ib_spec->eth.val.src_mac); 1583331769Shselasky 1584331769Shselasky if (ib_spec->eth.mask.vlan_tag) { 1585331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, 1586331769Shselasky cvlan_tag, 1); 1587331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, 1588331769Shselasky cvlan_tag, 1); 1589331769Shselasky 1590331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, 1591331769Shselasky first_vid, ntohs(ib_spec->eth.mask.vlan_tag)); 1592331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, 1593331769Shselasky first_vid, ntohs(ib_spec->eth.val.vlan_tag)); 1594331769Shselasky 1595331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, 1596331769Shselasky first_cfi, 1597331769Shselasky ntohs(ib_spec->eth.mask.vlan_tag) >> 12); 1598331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, 1599331769Shselasky first_cfi, 1600331769Shselasky ntohs(ib_spec->eth.val.vlan_tag) >> 12); 1601331769Shselasky 1602331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, 1603331769Shselasky first_prio, 1604331769Shselasky ntohs(ib_spec->eth.mask.vlan_tag) >> 13); 1605331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, 1606331769Shselasky first_prio, 1607331769Shselasky ntohs(ib_spec->eth.val.vlan_tag) >> 13); 1608331769Shselasky } 1609331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, 1610331769Shselasky ethertype, ntohs(ib_spec->eth.mask.ether_type)); 1611331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, 1612331769Shselasky ethertype, ntohs(ib_spec->eth.val.ether_type)); 1613331769Shselasky break; 1614331769Shselasky case IB_FLOW_SPEC_IPV4: 1615331769Shselasky if (FIELDS_NOT_SUPPORTED(ib_spec->ipv4.mask, LAST_IPV4_FIELD)) 1616331769Shselasky return -ENOTSUPP; 1617331769Shselasky 1618331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, 1619331769Shselasky ethertype, 0xffff); 1620331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, 1621331769Shselasky ethertype, ETH_P_IP); 1622331769Shselasky 1623331769Shselasky memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_c, 1624331769Shselasky src_ipv4_src_ipv6.ipv4_layout.ipv4), 1625331769Shselasky &ib_spec->ipv4.mask.src_ip, 1626331769Shselasky sizeof(ib_spec->ipv4.mask.src_ip)); 1627331769Shselasky memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_v, 1628331769Shselasky src_ipv4_src_ipv6.ipv4_layout.ipv4), 1629331769Shselasky &ib_spec->ipv4.val.src_ip, 1630331769Shselasky sizeof(ib_spec->ipv4.val.src_ip)); 1631331769Shselasky memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_c, 1632331769Shselasky dst_ipv4_dst_ipv6.ipv4_layout.ipv4), 1633331769Shselasky &ib_spec->ipv4.mask.dst_ip, 1634331769Shselasky sizeof(ib_spec->ipv4.mask.dst_ip)); 1635331769Shselasky memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_v, 1636331769Shselasky dst_ipv4_dst_ipv6.ipv4_layout.ipv4), 1637331769Shselasky &ib_spec->ipv4.val.dst_ip, 1638331769Shselasky sizeof(ib_spec->ipv4.val.dst_ip)); 1639331769Shselasky 1640331769Shselasky set_tos(outer_headers_c, outer_headers_v, 1641331769Shselasky ib_spec->ipv4.mask.tos, ib_spec->ipv4.val.tos); 1642331769Shselasky 1643331769Shselasky set_proto(outer_headers_c, outer_headers_v, 1644331769Shselasky ib_spec->ipv4.mask.proto, ib_spec->ipv4.val.proto); 1645331769Shselasky break; 1646331769Shselasky case IB_FLOW_SPEC_IPV6: 1647331769Shselasky if (FIELDS_NOT_SUPPORTED(ib_spec->ipv6.mask, LAST_IPV6_FIELD)) 1648331769Shselasky return -ENOTSUPP; 1649331769Shselasky 1650331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, 1651331769Shselasky ethertype, 0xffff); 1652331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, 1653331769Shselasky ethertype, IPPROTO_IPV6); 1654331769Shselasky 1655331769Shselasky memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_c, 1656331769Shselasky src_ipv4_src_ipv6.ipv6_layout.ipv6), 1657331769Shselasky &ib_spec->ipv6.mask.src_ip, 1658331769Shselasky sizeof(ib_spec->ipv6.mask.src_ip)); 1659331769Shselasky memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_v, 1660331769Shselasky src_ipv4_src_ipv6.ipv6_layout.ipv6), 1661331769Shselasky &ib_spec->ipv6.val.src_ip, 1662331769Shselasky sizeof(ib_spec->ipv6.val.src_ip)); 1663331769Shselasky memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_c, 1664331769Shselasky dst_ipv4_dst_ipv6.ipv6_layout.ipv6), 1665331769Shselasky &ib_spec->ipv6.mask.dst_ip, 1666331769Shselasky sizeof(ib_spec->ipv6.mask.dst_ip)); 1667331769Shselasky memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_v, 1668331769Shselasky dst_ipv4_dst_ipv6.ipv6_layout.ipv6), 1669331769Shselasky &ib_spec->ipv6.val.dst_ip, 1670331769Shselasky sizeof(ib_spec->ipv6.val.dst_ip)); 1671331769Shselasky 1672331769Shselasky set_tos(outer_headers_c, outer_headers_v, 1673331769Shselasky ib_spec->ipv6.mask.traffic_class, 1674331769Shselasky ib_spec->ipv6.val.traffic_class); 1675331769Shselasky 1676331769Shselasky set_proto(outer_headers_c, outer_headers_v, 1677331769Shselasky ib_spec->ipv6.mask.next_hdr, 1678331769Shselasky ib_spec->ipv6.val.next_hdr); 1679331769Shselasky 1680331769Shselasky MLX5_SET(fte_match_set_misc, misc_params_c, 1681331769Shselasky outer_ipv6_flow_label, 1682331769Shselasky ntohl(ib_spec->ipv6.mask.flow_label)); 1683331769Shselasky MLX5_SET(fte_match_set_misc, misc_params_v, 1684331769Shselasky outer_ipv6_flow_label, 1685331769Shselasky ntohl(ib_spec->ipv6.val.flow_label)); 1686331769Shselasky break; 1687331769Shselasky case IB_FLOW_SPEC_TCP: 1688331769Shselasky if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask, 1689331769Shselasky LAST_TCP_UDP_FIELD)) 1690331769Shselasky return -ENOTSUPP; 1691331769Shselasky 1692331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, ip_protocol, 1693331769Shselasky 0xff); 1694331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, ip_protocol, 1695331769Shselasky IPPROTO_TCP); 1696331769Shselasky 1697331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, tcp_sport, 1698331769Shselasky ntohs(ib_spec->tcp_udp.mask.src_port)); 1699331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, tcp_sport, 1700331769Shselasky ntohs(ib_spec->tcp_udp.val.src_port)); 1701331769Shselasky 1702331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, tcp_dport, 1703331769Shselasky ntohs(ib_spec->tcp_udp.mask.dst_port)); 1704331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, tcp_dport, 1705331769Shselasky ntohs(ib_spec->tcp_udp.val.dst_port)); 1706331769Shselasky break; 1707331769Shselasky case IB_FLOW_SPEC_UDP: 1708331769Shselasky if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask, 1709331769Shselasky LAST_TCP_UDP_FIELD)) 1710331769Shselasky return -ENOTSUPP; 1711331769Shselasky 1712331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, ip_protocol, 1713331769Shselasky 0xff); 1714331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, ip_protocol, 1715331769Shselasky IPPROTO_UDP); 1716331769Shselasky 1717331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, udp_sport, 1718331769Shselasky ntohs(ib_spec->tcp_udp.mask.src_port)); 1719331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, udp_sport, 1720331769Shselasky ntohs(ib_spec->tcp_udp.val.src_port)); 1721331769Shselasky 1722331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, udp_dport, 1723331769Shselasky ntohs(ib_spec->tcp_udp.mask.dst_port)); 1724331769Shselasky MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, udp_dport, 1725331769Shselasky ntohs(ib_spec->tcp_udp.val.dst_port)); 1726331769Shselasky break; 1727331769Shselasky default: 1728331769Shselasky return -EINVAL; 1729331769Shselasky } 1730331769Shselasky 1731331769Shselasky return 0; 1732331769Shselasky} 1733331769Shselasky 1734331769Shselasky/* If a flow could catch both multicast and unicast packets, 1735331769Shselasky * it won't fall into the multicast flow steering table and this rule 1736331769Shselasky * could steal other multicast packets. 1737331769Shselasky */ 1738331769Shselaskystatic bool flow_is_multicast_only(struct ib_flow_attr *ib_attr) 1739331769Shselasky{ 1740331769Shselasky struct ib_flow_spec_eth *eth_spec; 1741331769Shselasky 1742331769Shselasky if (ib_attr->type != IB_FLOW_ATTR_NORMAL || 1743331769Shselasky ib_attr->size < sizeof(struct ib_flow_attr) + 1744331769Shselasky sizeof(struct ib_flow_spec_eth) || 1745331769Shselasky ib_attr->num_of_specs < 1) 1746331769Shselasky return false; 1747331769Shselasky 1748331769Shselasky eth_spec = (struct ib_flow_spec_eth *)(ib_attr + 1); 1749331769Shselasky if (eth_spec->type != IB_FLOW_SPEC_ETH || 1750331769Shselasky eth_spec->size != sizeof(*eth_spec)) 1751331769Shselasky return false; 1752331769Shselasky 1753331769Shselasky return is_multicast_ether_addr(eth_spec->mask.dst_mac) && 1754331769Shselasky is_multicast_ether_addr(eth_spec->val.dst_mac); 1755331769Shselasky} 1756331769Shselasky 1757331769Shselaskystatic bool is_valid_attr(const struct ib_flow_attr *flow_attr) 1758331769Shselasky{ 1759331769Shselasky union ib_flow_spec *ib_spec = (union ib_flow_spec *)(flow_attr + 1); 1760331769Shselasky bool has_ipv4_spec = false; 1761331769Shselasky bool eth_type_ipv4 = true; 1762331769Shselasky unsigned int spec_index; 1763331769Shselasky 1764331769Shselasky /* Validate that ethertype is correct */ 1765331769Shselasky for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) { 1766331769Shselasky if (ib_spec->type == IB_FLOW_SPEC_ETH && 1767331769Shselasky ib_spec->eth.mask.ether_type) { 1768331769Shselasky if (!((ib_spec->eth.mask.ether_type == htons(0xffff)) && 1769331769Shselasky ib_spec->eth.val.ether_type == htons(ETH_P_IP))) 1770331769Shselasky eth_type_ipv4 = false; 1771331769Shselasky } else if (ib_spec->type == IB_FLOW_SPEC_IPV4) { 1772331769Shselasky has_ipv4_spec = true; 1773331769Shselasky } 1774331769Shselasky ib_spec = (void *)ib_spec + ib_spec->size; 1775331769Shselasky } 1776331769Shselasky return !has_ipv4_spec || eth_type_ipv4; 1777331769Shselasky} 1778331769Shselasky 1779331769Shselaskystatic void put_flow_table(struct mlx5_ib_dev *dev, 1780331769Shselasky struct mlx5_ib_flow_prio *prio, bool ft_added) 1781331769Shselasky{ 1782331769Shselasky prio->refcount -= !!ft_added; 1783331769Shselasky if (!prio->refcount) { 1784331769Shselasky mlx5_destroy_flow_table(prio->flow_table); 1785331769Shselasky prio->flow_table = NULL; 1786331769Shselasky } 1787331769Shselasky} 1788331769Shselasky 1789331769Shselaskystatic int mlx5_ib_destroy_flow(struct ib_flow *flow_id) 1790331769Shselasky{ 1791331769Shselasky struct mlx5_ib_dev *dev = to_mdev(flow_id->qp->device); 1792331769Shselasky struct mlx5_ib_flow_handler *handler = container_of(flow_id, 1793331769Shselasky struct mlx5_ib_flow_handler, 1794331769Shselasky ibflow); 1795331769Shselasky struct mlx5_ib_flow_handler *iter, *tmp; 1796331769Shselasky 1797331769Shselasky mutex_lock(&dev->flow_db.lock); 1798331769Shselasky 1799331769Shselasky list_for_each_entry_safe(iter, tmp, &handler->list, list) { 1800331769Shselasky mlx5_del_flow_rule(iter->rule); 1801331769Shselasky put_flow_table(dev, iter->prio, true); 1802331769Shselasky list_del(&iter->list); 1803331769Shselasky kfree(iter); 1804331769Shselasky } 1805331769Shselasky 1806331769Shselasky mlx5_del_flow_rule(handler->rule); 1807331769Shselasky put_flow_table(dev, handler->prio, true); 1808331769Shselasky mutex_unlock(&dev->flow_db.lock); 1809331769Shselasky 1810331769Shselasky kfree(handler); 1811331769Shselasky 1812331769Shselasky return 0; 1813331769Shselasky} 1814331769Shselasky 1815331769Shselaskystatic int ib_prio_to_core_prio(unsigned int priority, bool dont_trap) 1816331769Shselasky{ 1817331769Shselasky priority *= 2; 1818331769Shselasky if (!dont_trap) 1819331769Shselasky priority++; 1820331769Shselasky return priority; 1821331769Shselasky} 1822331769Shselasky 1823331769Shselaskyenum flow_table_type { 1824331769Shselasky MLX5_IB_FT_RX, 1825331769Shselasky MLX5_IB_FT_TX 1826331769Shselasky}; 1827331769Shselasky 1828331769Shselasky#define MLX5_FS_MAX_TYPES 10 1829331769Shselasky#define MLX5_FS_MAX_ENTRIES 32000UL 1830331769Shselaskystatic struct mlx5_ib_flow_prio *get_flow_table(struct mlx5_ib_dev *dev, 1831331769Shselasky struct ib_flow_attr *flow_attr, 1832331769Shselasky enum flow_table_type ft_type) 1833331769Shselasky{ 1834331769Shselasky bool dont_trap = flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP; 1835331769Shselasky struct mlx5_flow_namespace *ns = NULL; 1836331769Shselasky struct mlx5_ib_flow_prio *prio; 1837331769Shselasky struct mlx5_flow_table *ft; 1838331769Shselasky int num_entries; 1839331769Shselasky int num_groups; 1840331769Shselasky int priority; 1841331769Shselasky int err = 0; 1842331769Shselasky 1843331769Shselasky if (flow_attr->type == IB_FLOW_ATTR_NORMAL) { 1844331769Shselasky if (flow_is_multicast_only(flow_attr) && 1845331769Shselasky !dont_trap) 1846331769Shselasky priority = MLX5_IB_FLOW_MCAST_PRIO; 1847331769Shselasky else 1848331769Shselasky priority = ib_prio_to_core_prio(flow_attr->priority, 1849331769Shselasky dont_trap); 1850331769Shselasky ns = mlx5_get_flow_namespace(dev->mdev, 1851331769Shselasky MLX5_FLOW_NAMESPACE_BYPASS); 1852331769Shselasky num_entries = MLX5_FS_MAX_ENTRIES; 1853331769Shselasky num_groups = MLX5_FS_MAX_TYPES; 1854331769Shselasky prio = &dev->flow_db.prios[priority]; 1855331769Shselasky } else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT || 1856331769Shselasky flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) { 1857331769Shselasky ns = mlx5_get_flow_namespace(dev->mdev, 1858331769Shselasky MLX5_FLOW_NAMESPACE_LEFTOVERS); 1859331769Shselasky build_leftovers_ft_param("bypass", &priority, 1860331769Shselasky &num_entries, 1861331769Shselasky &num_groups); 1862331769Shselasky prio = &dev->flow_db.prios[MLX5_IB_FLOW_LEFTOVERS_PRIO]; 1863331769Shselasky } else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) { 1864331769Shselasky if (!MLX5_CAP_FLOWTABLE(dev->mdev, 1865331769Shselasky allow_sniffer_and_nic_rx_shared_tir)) 1866331769Shselasky return ERR_PTR(-ENOTSUPP); 1867331769Shselasky 1868331769Shselasky ns = mlx5_get_flow_namespace(dev->mdev, ft_type == MLX5_IB_FT_RX ? 1869331769Shselasky MLX5_FLOW_NAMESPACE_SNIFFER_RX : 1870331769Shselasky MLX5_FLOW_NAMESPACE_SNIFFER_TX); 1871331769Shselasky 1872331769Shselasky prio = &dev->flow_db.sniffer[ft_type]; 1873331769Shselasky priority = 0; 1874331769Shselasky num_entries = 1; 1875331769Shselasky num_groups = 1; 1876331769Shselasky } 1877331769Shselasky 1878331769Shselasky if (!ns) 1879331769Shselasky return ERR_PTR(-ENOTSUPP); 1880331769Shselasky 1881331769Shselasky ft = prio->flow_table; 1882331769Shselasky if (!ft) { 1883331769Shselasky ft = mlx5_create_auto_grouped_flow_table(ns, priority, "bypass", 1884331769Shselasky num_entries, 1885331769Shselasky num_groups); 1886331769Shselasky 1887331769Shselasky if (!IS_ERR(ft)) { 1888331769Shselasky prio->refcount = 0; 1889331769Shselasky prio->flow_table = ft; 1890331769Shselasky } else { 1891331769Shselasky err = PTR_ERR(ft); 1892331769Shselasky } 1893331769Shselasky } 1894331769Shselasky 1895331769Shselasky return err ? ERR_PTR(err) : prio; 1896331769Shselasky} 1897331769Shselasky 1898331769Shselaskystatic struct mlx5_ib_flow_handler *create_flow_rule(struct mlx5_ib_dev *dev, 1899331769Shselasky struct mlx5_ib_flow_prio *ft_prio, 1900331769Shselasky const struct ib_flow_attr *flow_attr, 1901331769Shselasky struct mlx5_flow_destination *dst) 1902331769Shselasky{ 1903331769Shselasky struct mlx5_flow_table *ft = ft_prio->flow_table; 1904331769Shselasky struct mlx5_ib_flow_handler *handler; 1905331769Shselasky struct mlx5_flow_spec *spec; 1906331769Shselasky const void *ib_flow = (const void *)flow_attr + sizeof(*flow_attr); 1907331769Shselasky unsigned int spec_index; 1908331769Shselasky u32 action; 1909331769Shselasky int err = 0; 1910331769Shselasky 1911331769Shselasky if (!is_valid_attr(flow_attr)) 1912331769Shselasky return ERR_PTR(-EINVAL); 1913331769Shselasky 1914331769Shselasky spec = mlx5_vzalloc(sizeof(*spec)); 1915331769Shselasky handler = kzalloc(sizeof(*handler), GFP_KERNEL); 1916331769Shselasky if (!handler || !spec) { 1917331769Shselasky err = -ENOMEM; 1918331769Shselasky goto free; 1919331769Shselasky } 1920331769Shselasky 1921331769Shselasky INIT_LIST_HEAD(&handler->list); 1922331769Shselasky 1923331769Shselasky for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) { 1924331769Shselasky err = parse_flow_attr(spec->match_criteria, 1925331769Shselasky spec->match_value, ib_flow); 1926331769Shselasky if (err < 0) 1927331769Shselasky goto free; 1928331769Shselasky 1929331769Shselasky ib_flow += ((union ib_flow_spec *)ib_flow)->size; 1930331769Shselasky } 1931331769Shselasky 1932331769Shselasky spec->match_criteria_enable = get_match_criteria_enable(spec->match_criteria); 1933331769Shselasky action = dst ? MLX5_FLOW_CONTEXT_ACTION_FWD_DEST : 1934331769Shselasky MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO; 1935331769Shselasky handler->rule = mlx5_add_flow_rule(ft, spec->match_criteria_enable, 1936331769Shselasky spec->match_criteria, 1937331769Shselasky spec->match_value, 1938331769Shselasky action, 1939331769Shselasky MLX5_FS_DEFAULT_FLOW_TAG, 1940331769Shselasky dst); 1941331769Shselasky 1942331769Shselasky if (IS_ERR(handler->rule)) { 1943331769Shselasky err = PTR_ERR(handler->rule); 1944331769Shselasky goto free; 1945331769Shselasky } 1946331769Shselasky 1947331769Shselasky ft_prio->refcount++; 1948331769Shselasky handler->prio = ft_prio; 1949331769Shselasky 1950331769Shselasky ft_prio->flow_table = ft; 1951331769Shselaskyfree: 1952331769Shselasky if (err) 1953331769Shselasky kfree(handler); 1954331769Shselasky kvfree(spec); 1955331769Shselasky return err ? ERR_PTR(err) : handler; 1956331769Shselasky} 1957331769Shselasky 1958331769Shselaskystatic struct mlx5_ib_flow_handler *create_dont_trap_rule(struct mlx5_ib_dev *dev, 1959331769Shselasky struct mlx5_ib_flow_prio *ft_prio, 1960331769Shselasky struct ib_flow_attr *flow_attr, 1961331769Shselasky struct mlx5_flow_destination *dst) 1962331769Shselasky{ 1963331769Shselasky struct mlx5_ib_flow_handler *handler_dst = NULL; 1964331769Shselasky struct mlx5_ib_flow_handler *handler = NULL; 1965331769Shselasky 1966331769Shselasky handler = create_flow_rule(dev, ft_prio, flow_attr, NULL); 1967331769Shselasky if (!IS_ERR(handler)) { 1968331769Shselasky handler_dst = create_flow_rule(dev, ft_prio, 1969331769Shselasky flow_attr, dst); 1970331769Shselasky if (IS_ERR(handler_dst)) { 1971331769Shselasky mlx5_del_flow_rule(handler->rule); 1972331769Shselasky ft_prio->refcount--; 1973331769Shselasky kfree(handler); 1974331769Shselasky handler = handler_dst; 1975331769Shselasky } else { 1976331769Shselasky list_add(&handler_dst->list, &handler->list); 1977331769Shselasky } 1978331769Shselasky } 1979331769Shselasky 1980331769Shselasky return handler; 1981331769Shselasky} 1982331769Shselaskyenum { 1983331769Shselasky LEFTOVERS_MC, 1984331769Shselasky LEFTOVERS_UC, 1985331769Shselasky}; 1986331769Shselasky 1987331769Shselaskystatic struct mlx5_ib_flow_handler *create_leftovers_rule(struct mlx5_ib_dev *dev, 1988331769Shselasky struct mlx5_ib_flow_prio *ft_prio, 1989331769Shselasky struct ib_flow_attr *flow_attr, 1990331769Shselasky struct mlx5_flow_destination *dst) 1991331769Shselasky{ 1992331769Shselasky struct mlx5_ib_flow_handler *handler_ucast = NULL; 1993331769Shselasky struct mlx5_ib_flow_handler *handler = NULL; 1994331769Shselasky 1995331769Shselasky static struct { 1996331769Shselasky struct ib_flow_attr flow_attr; 1997331769Shselasky struct ib_flow_spec_eth eth_flow; 1998331769Shselasky } leftovers_specs[] = { 1999331769Shselasky [LEFTOVERS_MC] = { 2000331769Shselasky .flow_attr = { 2001331769Shselasky .num_of_specs = 1, 2002331769Shselasky .size = sizeof(leftovers_specs[0]) 2003331769Shselasky }, 2004331769Shselasky .eth_flow = { 2005331769Shselasky .type = IB_FLOW_SPEC_ETH, 2006331769Shselasky .size = sizeof(struct ib_flow_spec_eth), 2007331769Shselasky .mask = {.dst_mac = {0x1} }, 2008331769Shselasky .val = {.dst_mac = {0x1} } 2009331769Shselasky } 2010331769Shselasky }, 2011331769Shselasky [LEFTOVERS_UC] = { 2012331769Shselasky .flow_attr = { 2013331769Shselasky .num_of_specs = 1, 2014331769Shselasky .size = sizeof(leftovers_specs[0]) 2015331769Shselasky }, 2016331769Shselasky .eth_flow = { 2017331769Shselasky .type = IB_FLOW_SPEC_ETH, 2018331769Shselasky .size = sizeof(struct ib_flow_spec_eth), 2019331769Shselasky .mask = {.dst_mac = {0x1} }, 2020331769Shselasky .val = {.dst_mac = {} } 2021331769Shselasky } 2022331769Shselasky } 2023331769Shselasky }; 2024331769Shselasky 2025331769Shselasky handler = create_flow_rule(dev, ft_prio, 2026331769Shselasky &leftovers_specs[LEFTOVERS_MC].flow_attr, 2027331769Shselasky dst); 2028331769Shselasky if (!IS_ERR(handler) && 2029331769Shselasky flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT) { 2030331769Shselasky handler_ucast = create_flow_rule(dev, ft_prio, 2031331769Shselasky &leftovers_specs[LEFTOVERS_UC].flow_attr, 2032331769Shselasky dst); 2033331769Shselasky if (IS_ERR(handler_ucast)) { 2034331769Shselasky mlx5_del_flow_rule(handler->rule); 2035331769Shselasky ft_prio->refcount--; 2036331769Shselasky kfree(handler); 2037331769Shselasky handler = handler_ucast; 2038331769Shselasky } else { 2039331769Shselasky list_add(&handler_ucast->list, &handler->list); 2040331769Shselasky } 2041331769Shselasky } 2042331769Shselasky 2043331769Shselasky return handler; 2044331769Shselasky} 2045331769Shselasky 2046331769Shselaskystatic struct mlx5_ib_flow_handler *create_sniffer_rule(struct mlx5_ib_dev *dev, 2047331769Shselasky struct mlx5_ib_flow_prio *ft_rx, 2048331769Shselasky struct mlx5_ib_flow_prio *ft_tx, 2049331769Shselasky struct mlx5_flow_destination *dst) 2050331769Shselasky{ 2051331769Shselasky struct mlx5_ib_flow_handler *handler_rx; 2052331769Shselasky struct mlx5_ib_flow_handler *handler_tx; 2053331769Shselasky int err; 2054331769Shselasky static const struct ib_flow_attr flow_attr = { 2055331769Shselasky .num_of_specs = 0, 2056331769Shselasky .size = sizeof(flow_attr) 2057331769Shselasky }; 2058331769Shselasky 2059331769Shselasky handler_rx = create_flow_rule(dev, ft_rx, &flow_attr, dst); 2060331769Shselasky if (IS_ERR(handler_rx)) { 2061331769Shselasky err = PTR_ERR(handler_rx); 2062331769Shselasky goto err; 2063331769Shselasky } 2064331769Shselasky 2065331769Shselasky handler_tx = create_flow_rule(dev, ft_tx, &flow_attr, dst); 2066331769Shselasky if (IS_ERR(handler_tx)) { 2067331769Shselasky err = PTR_ERR(handler_tx); 2068331769Shselasky goto err_tx; 2069331769Shselasky } 2070331769Shselasky 2071331769Shselasky list_add(&handler_tx->list, &handler_rx->list); 2072331769Shselasky 2073331769Shselasky return handler_rx; 2074331769Shselasky 2075331769Shselaskyerr_tx: 2076331769Shselasky mlx5_del_flow_rule(handler_rx->rule); 2077331769Shselasky ft_rx->refcount--; 2078331769Shselasky kfree(handler_rx); 2079331769Shselaskyerr: 2080331769Shselasky return ERR_PTR(err); 2081331769Shselasky} 2082331769Shselasky 2083331769Shselaskystatic struct ib_flow *mlx5_ib_create_flow(struct ib_qp *qp, 2084331769Shselasky struct ib_flow_attr *flow_attr, 2085331769Shselasky int domain) 2086331769Shselasky{ 2087331769Shselasky struct mlx5_ib_dev *dev = to_mdev(qp->device); 2088331769Shselasky struct mlx5_ib_qp *mqp = to_mqp(qp); 2089331769Shselasky struct mlx5_ib_flow_handler *handler = NULL; 2090331769Shselasky struct mlx5_flow_destination *dst = NULL; 2091331769Shselasky struct mlx5_ib_flow_prio *ft_prio_tx = NULL; 2092331769Shselasky struct mlx5_ib_flow_prio *ft_prio; 2093331769Shselasky int err; 2094331769Shselasky 2095331769Shselasky if (flow_attr->priority > MLX5_IB_FLOW_LAST_PRIO) 2096331769Shselasky return ERR_PTR(-ENOSPC); 2097331769Shselasky 2098331769Shselasky if (domain != IB_FLOW_DOMAIN_USER || 2099331769Shselasky flow_attr->port > MLX5_CAP_GEN(dev->mdev, num_ports) || 2100331769Shselasky (flow_attr->flags & ~IB_FLOW_ATTR_FLAGS_DONT_TRAP)) 2101331769Shselasky return ERR_PTR(-EINVAL); 2102331769Shselasky 2103331769Shselasky dst = kzalloc(sizeof(*dst), GFP_KERNEL); 2104331769Shselasky if (!dst) 2105331769Shselasky return ERR_PTR(-ENOMEM); 2106331769Shselasky 2107331769Shselasky mutex_lock(&dev->flow_db.lock); 2108331769Shselasky 2109331769Shselasky ft_prio = get_flow_table(dev, flow_attr, MLX5_IB_FT_RX); 2110331769Shselasky if (IS_ERR(ft_prio)) { 2111331769Shselasky err = PTR_ERR(ft_prio); 2112331769Shselasky goto unlock; 2113331769Shselasky } 2114331769Shselasky if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) { 2115331769Shselasky ft_prio_tx = get_flow_table(dev, flow_attr, MLX5_IB_FT_TX); 2116331769Shselasky if (IS_ERR(ft_prio_tx)) { 2117331769Shselasky err = PTR_ERR(ft_prio_tx); 2118331769Shselasky ft_prio_tx = NULL; 2119331769Shselasky goto destroy_ft; 2120331769Shselasky } 2121331769Shselasky } 2122331769Shselasky 2123331769Shselasky dst->type = MLX5_FLOW_DESTINATION_TYPE_TIR; 2124331769Shselasky if (mqp->flags & MLX5_IB_QP_RSS) 2125331769Shselasky dst->tir_num = mqp->rss_qp.tirn; 2126331769Shselasky else 2127331769Shselasky dst->tir_num = mqp->raw_packet_qp.rq.tirn; 2128331769Shselasky 2129331769Shselasky if (flow_attr->type == IB_FLOW_ATTR_NORMAL) { 2130331769Shselasky if (flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP) { 2131331769Shselasky handler = create_dont_trap_rule(dev, ft_prio, 2132331769Shselasky flow_attr, dst); 2133331769Shselasky } else { 2134331769Shselasky handler = create_flow_rule(dev, ft_prio, flow_attr, 2135331769Shselasky dst); 2136331769Shselasky } 2137331769Shselasky } else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT || 2138331769Shselasky flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) { 2139331769Shselasky handler = create_leftovers_rule(dev, ft_prio, flow_attr, 2140331769Shselasky dst); 2141331769Shselasky } else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) { 2142331769Shselasky handler = create_sniffer_rule(dev, ft_prio, ft_prio_tx, dst); 2143331769Shselasky } else { 2144331769Shselasky err = -EINVAL; 2145331769Shselasky goto destroy_ft; 2146331769Shselasky } 2147331769Shselasky 2148331769Shselasky if (IS_ERR(handler)) { 2149331769Shselasky err = PTR_ERR(handler); 2150331769Shselasky handler = NULL; 2151331769Shselasky goto destroy_ft; 2152331769Shselasky } 2153331769Shselasky 2154331769Shselasky mutex_unlock(&dev->flow_db.lock); 2155331769Shselasky kfree(dst); 2156331769Shselasky 2157331769Shselasky return &handler->ibflow; 2158331769Shselasky 2159331769Shselaskydestroy_ft: 2160331769Shselasky put_flow_table(dev, ft_prio, false); 2161331769Shselasky if (ft_prio_tx) 2162331769Shselasky put_flow_table(dev, ft_prio_tx, false); 2163331769Shselaskyunlock: 2164331769Shselasky mutex_unlock(&dev->flow_db.lock); 2165331769Shselasky kfree(dst); 2166331769Shselasky kfree(handler); 2167331769Shselasky return ERR_PTR(err); 2168331769Shselasky} 2169331769Shselasky 2170322810Shselaskystatic int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid) 2171322810Shselasky{ 2172322810Shselasky struct mlx5_ib_dev *dev = to_mdev(ibqp->device); 2173322810Shselasky int err; 2174322810Shselasky 2175331769Shselasky err = mlx5_core_attach_mcg(dev->mdev, gid, ibqp->qp_num); 2176322810Shselasky if (err) 2177322810Shselasky mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n", 2178322810Shselasky ibqp->qp_num, gid->raw); 2179322810Shselasky 2180322810Shselasky return err; 2181322810Shselasky} 2182322810Shselasky 2183322810Shselaskystatic int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid) 2184322810Shselasky{ 2185322810Shselasky struct mlx5_ib_dev *dev = to_mdev(ibqp->device); 2186322810Shselasky int err; 2187322810Shselasky 2188331769Shselasky err = mlx5_core_detach_mcg(dev->mdev, gid, ibqp->qp_num); 2189322810Shselasky if (err) 2190322810Shselasky mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n", 2191322810Shselasky ibqp->qp_num, gid->raw); 2192322810Shselasky 2193322810Shselasky return err; 2194322810Shselasky} 2195322810Shselasky 2196322810Shselaskystatic int init_node_data(struct mlx5_ib_dev *dev) 2197322810Shselasky{ 2198322810Shselasky int err; 2199322810Shselasky 2200322810Shselasky err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc); 2201322810Shselasky if (err) 2202322810Shselasky return err; 2203322810Shselasky 2204322810Shselasky return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid); 2205322810Shselasky} 2206322810Shselasky 2207322810Shselaskystatic ssize_t show_fw_pages(struct device *device, struct device_attribute *attr, 2208322810Shselasky char *buf) 2209322810Shselasky{ 2210322810Shselasky struct mlx5_ib_dev *dev = 2211322810Shselasky container_of(device, struct mlx5_ib_dev, ib_dev.dev); 2212322810Shselasky 2213322810Shselasky return sprintf(buf, "%lld\n", (long long)dev->mdev->priv.fw_pages); 2214322810Shselasky} 2215322810Shselasky 2216322810Shselaskystatic ssize_t show_reg_pages(struct device *device, 2217322810Shselasky struct device_attribute *attr, char *buf) 2218322810Shselasky{ 2219322810Shselasky struct mlx5_ib_dev *dev = 2220322810Shselasky container_of(device, struct mlx5_ib_dev, ib_dev.dev); 2221322810Shselasky 2222322810Shselasky return sprintf(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages)); 2223322810Shselasky} 2224322810Shselasky 2225322810Shselaskystatic ssize_t show_hca(struct device *device, struct device_attribute *attr, 2226322810Shselasky char *buf) 2227322810Shselasky{ 2228322810Shselasky struct mlx5_ib_dev *dev = 2229322810Shselasky container_of(device, struct mlx5_ib_dev, ib_dev.dev); 2230322810Shselasky return sprintf(buf, "MT%d\n", dev->mdev->pdev->device); 2231322810Shselasky} 2232322810Shselasky 2233322810Shselaskystatic ssize_t show_rev(struct device *device, struct device_attribute *attr, 2234322810Shselasky char *buf) 2235322810Shselasky{ 2236322810Shselasky struct mlx5_ib_dev *dev = 2237322810Shselasky container_of(device, struct mlx5_ib_dev, ib_dev.dev); 2238331769Shselasky return sprintf(buf, "%x\n", dev->mdev->pdev->revision); 2239322810Shselasky} 2240322810Shselasky 2241322810Shselaskystatic ssize_t show_board(struct device *device, struct device_attribute *attr, 2242322810Shselasky char *buf) 2243322810Shselasky{ 2244322810Shselasky struct mlx5_ib_dev *dev = 2245322810Shselasky container_of(device, struct mlx5_ib_dev, ib_dev.dev); 2246322810Shselasky return sprintf(buf, "%.*s\n", MLX5_BOARD_ID_LEN, 2247322810Shselasky dev->mdev->board_id); 2248322810Shselasky} 2249322810Shselasky 2250322810Shselaskystatic DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL); 2251322810Shselaskystatic DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL); 2252322810Shselaskystatic DEVICE_ATTR(board_id, S_IRUGO, show_board, NULL); 2253322810Shselaskystatic DEVICE_ATTR(fw_pages, S_IRUGO, show_fw_pages, NULL); 2254322810Shselaskystatic DEVICE_ATTR(reg_pages, S_IRUGO, show_reg_pages, NULL); 2255322810Shselasky 2256322810Shselaskystatic struct device_attribute *mlx5_class_attributes[] = { 2257322810Shselasky &dev_attr_hw_rev, 2258322810Shselasky &dev_attr_hca_type, 2259322810Shselasky &dev_attr_board_id, 2260322810Shselasky &dev_attr_fw_pages, 2261322810Shselasky &dev_attr_reg_pages, 2262322810Shselasky}; 2263322810Shselasky 2264331769Shselaskystatic void pkey_change_handler(struct work_struct *work) 2265331769Shselasky{ 2266331769Shselasky struct mlx5_ib_port_resources *ports = 2267331769Shselasky container_of(work, struct mlx5_ib_port_resources, 2268331769Shselasky pkey_change_work); 2269331769Shselasky 2270331769Shselasky mutex_lock(&ports->devr->mutex); 2271331769Shselasky mlx5_ib_gsi_pkey_change(ports->gsi); 2272331769Shselasky mutex_unlock(&ports->devr->mutex); 2273331769Shselasky} 2274331769Shselasky 2275322810Shselaskystatic void mlx5_ib_handle_internal_error(struct mlx5_ib_dev *ibdev) 2276322810Shselasky{ 2277322810Shselasky struct mlx5_ib_qp *mqp; 2278322810Shselasky struct mlx5_ib_cq *send_mcq, *recv_mcq; 2279322810Shselasky struct mlx5_core_cq *mcq; 2280322810Shselasky struct list_head cq_armed_list; 2281322810Shselasky unsigned long flags_qp; 2282322810Shselasky unsigned long flags_cq; 2283322810Shselasky unsigned long flags; 2284322810Shselasky 2285322810Shselasky INIT_LIST_HEAD(&cq_armed_list); 2286322810Shselasky 2287322810Shselasky /* Go over qp list reside on that ibdev, sync with create/destroy qp.*/ 2288322810Shselasky spin_lock_irqsave(&ibdev->reset_flow_resource_lock, flags); 2289322810Shselasky list_for_each_entry(mqp, &ibdev->qp_list, qps_list) { 2290322810Shselasky spin_lock_irqsave(&mqp->sq.lock, flags_qp); 2291322810Shselasky if (mqp->sq.tail != mqp->sq.head) { 2292322810Shselasky send_mcq = to_mcq(mqp->ibqp.send_cq); 2293322810Shselasky spin_lock_irqsave(&send_mcq->lock, flags_cq); 2294322810Shselasky if (send_mcq->mcq.comp && 2295322810Shselasky mqp->ibqp.send_cq->comp_handler) { 2296322810Shselasky if (!send_mcq->mcq.reset_notify_added) { 2297322810Shselasky send_mcq->mcq.reset_notify_added = 1; 2298322810Shselasky list_add_tail(&send_mcq->mcq.reset_notify, 2299322810Shselasky &cq_armed_list); 2300322810Shselasky } 2301322810Shselasky } 2302322810Shselasky spin_unlock_irqrestore(&send_mcq->lock, flags_cq); 2303322810Shselasky } 2304322810Shselasky spin_unlock_irqrestore(&mqp->sq.lock, flags_qp); 2305322810Shselasky spin_lock_irqsave(&mqp->rq.lock, flags_qp); 2306322810Shselasky /* no handling is needed for SRQ */ 2307322810Shselasky if (!mqp->ibqp.srq) { 2308322810Shselasky if (mqp->rq.tail != mqp->rq.head) { 2309322810Shselasky recv_mcq = to_mcq(mqp->ibqp.recv_cq); 2310322810Shselasky spin_lock_irqsave(&recv_mcq->lock, flags_cq); 2311322810Shselasky if (recv_mcq->mcq.comp && 2312322810Shselasky mqp->ibqp.recv_cq->comp_handler) { 2313322810Shselasky if (!recv_mcq->mcq.reset_notify_added) { 2314322810Shselasky recv_mcq->mcq.reset_notify_added = 1; 2315322810Shselasky list_add_tail(&recv_mcq->mcq.reset_notify, 2316322810Shselasky &cq_armed_list); 2317322810Shselasky } 2318322810Shselasky } 2319322810Shselasky spin_unlock_irqrestore(&recv_mcq->lock, 2320322810Shselasky flags_cq); 2321322810Shselasky } 2322322810Shselasky } 2323322810Shselasky spin_unlock_irqrestore(&mqp->rq.lock, flags_qp); 2324322810Shselasky } 2325322810Shselasky /*At that point all inflight post send were put to be executed as of we 2326322810Shselasky * lock/unlock above locks Now need to arm all involved CQs. 2327322810Shselasky */ 2328322810Shselasky list_for_each_entry(mcq, &cq_armed_list, reset_notify) { 2329322810Shselasky mcq->comp(mcq); 2330322810Shselasky } 2331322810Shselasky spin_unlock_irqrestore(&ibdev->reset_flow_resource_lock, flags); 2332322810Shselasky} 2333322810Shselasky 2334322810Shselaskystatic void mlx5_ib_event(struct mlx5_core_dev *dev, void *context, 2335322810Shselasky enum mlx5_dev_event event, unsigned long param) 2336322810Shselasky{ 2337322810Shselasky struct mlx5_ib_dev *ibdev = (struct mlx5_ib_dev *)context; 2338322810Shselasky struct ib_event ibev; 2339331769Shselasky bool fatal = false; 2340341922Shselasky u8 port = (u8)param; 2341322810Shselasky 2342322810Shselasky switch (event) { 2343322810Shselasky case MLX5_DEV_EVENT_SYS_ERROR: 2344322810Shselasky ibev.event = IB_EVENT_DEVICE_FATAL; 2345322810Shselasky mlx5_ib_handle_internal_error(ibdev); 2346331769Shselasky fatal = true; 2347322810Shselasky break; 2348322810Shselasky 2349322810Shselasky case MLX5_DEV_EVENT_PORT_UP: 2350322810Shselasky case MLX5_DEV_EVENT_PORT_DOWN: 2351322810Shselasky case MLX5_DEV_EVENT_PORT_INITIALIZED: 2352331769Shselasky /* In RoCE, port up/down events are handled in 2353331769Shselasky * mlx5_netdev_event(). 2354331769Shselasky */ 2355331769Shselasky if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) == 2356331769Shselasky IB_LINK_LAYER_ETHERNET) 2357331769Shselasky return; 2358331769Shselasky 2359331769Shselasky ibev.event = (event == MLX5_DEV_EVENT_PORT_UP) ? 2360331769Shselasky IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR; 2361322810Shselasky break; 2362322810Shselasky 2363322810Shselasky case MLX5_DEV_EVENT_LID_CHANGE: 2364322810Shselasky ibev.event = IB_EVENT_LID_CHANGE; 2365322810Shselasky break; 2366322810Shselasky 2367322810Shselasky case MLX5_DEV_EVENT_PKEY_CHANGE: 2368322810Shselasky ibev.event = IB_EVENT_PKEY_CHANGE; 2369331769Shselasky 2370331769Shselasky schedule_work(&ibdev->devr.ports[port - 1].pkey_change_work); 2371322810Shselasky break; 2372322810Shselasky 2373322810Shselasky case MLX5_DEV_EVENT_GUID_CHANGE: 2374322810Shselasky ibev.event = IB_EVENT_GID_CHANGE; 2375322810Shselasky break; 2376322810Shselasky 2377322810Shselasky case MLX5_DEV_EVENT_CLIENT_REREG: 2378322810Shselasky ibev.event = IB_EVENT_CLIENT_REREGISTER; 2379322810Shselasky break; 2380322810Shselasky 2381322810Shselasky default: 2382337100Shselasky /* unsupported event */ 2383337100Shselasky return; 2384322810Shselasky } 2385322810Shselasky 2386322810Shselasky ibev.device = &ibdev->ib_dev; 2387322810Shselasky ibev.element.port_num = port; 2388322810Shselasky 2389341922Shselasky if (!rdma_is_port_valid(&ibdev->ib_dev, port)) { 2390337100Shselasky mlx5_ib_warn(ibdev, "warning: event(%d) on port %d\n", event, port); 2391322810Shselasky return; 2392322810Shselasky } 2393322810Shselasky 2394322810Shselasky if (ibdev->ib_active) 2395322810Shselasky ib_dispatch_event(&ibev); 2396331769Shselasky 2397331769Shselasky if (fatal) 2398331769Shselasky ibdev->ib_active = false; 2399322810Shselasky} 2400322810Shselasky 2401322810Shselaskystatic void get_ext_port_caps(struct mlx5_ib_dev *dev) 2402322810Shselasky{ 2403322810Shselasky int port; 2404322810Shselasky 2405322810Shselasky for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++) 2406322810Shselasky mlx5_query_ext_port_caps(dev, port); 2407322810Shselasky} 2408322810Shselasky 2409322810Shselaskystatic int get_port_caps(struct mlx5_ib_dev *dev) 2410322810Shselasky{ 2411322810Shselasky struct ib_device_attr *dprops = NULL; 2412322810Shselasky struct ib_port_attr *pprops = NULL; 2413322810Shselasky int err = -ENOMEM; 2414322810Shselasky int port; 2415331769Shselasky struct ib_udata uhw = {.inlen = 0, .outlen = 0}; 2416322810Shselasky 2417322810Shselasky pprops = kmalloc(sizeof(*pprops), GFP_KERNEL); 2418322810Shselasky if (!pprops) 2419322810Shselasky goto out; 2420322810Shselasky 2421322810Shselasky dprops = kmalloc(sizeof(*dprops), GFP_KERNEL); 2422322810Shselasky if (!dprops) 2423322810Shselasky goto out; 2424322810Shselasky 2425331769Shselasky err = mlx5_ib_query_device(&dev->ib_dev, dprops, &uhw); 2426322810Shselasky if (err) { 2427322810Shselasky mlx5_ib_warn(dev, "query_device failed %d\n", err); 2428322810Shselasky goto out; 2429322810Shselasky } 2430322810Shselasky 2431322810Shselasky for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++) { 2432322810Shselasky err = mlx5_ib_query_port(&dev->ib_dev, port, pprops); 2433322810Shselasky if (err) { 2434322810Shselasky mlx5_ib_warn(dev, "query_port %d failed %d\n", 2435322810Shselasky port, err); 2436322810Shselasky break; 2437322810Shselasky } 2438331769Shselasky dev->mdev->port_caps[port - 1].pkey_table_len = 2439331769Shselasky dprops->max_pkeys; 2440331769Shselasky dev->mdev->port_caps[port - 1].gid_table_len = 2441331769Shselasky pprops->gid_tbl_len; 2442322810Shselasky mlx5_ib_dbg(dev, "pkey_table_len %d, gid_table_len %d\n", 2443322810Shselasky dprops->max_pkeys, pprops->gid_tbl_len); 2444322810Shselasky } 2445322810Shselasky 2446322810Shselaskyout: 2447322810Shselasky kfree(pprops); 2448322810Shselasky kfree(dprops); 2449322810Shselasky 2450322810Shselasky return err; 2451322810Shselasky} 2452322810Shselasky 2453322810Shselaskystatic void destroy_umrc_res(struct mlx5_ib_dev *dev) 2454322810Shselasky{ 2455322810Shselasky int err; 2456322810Shselasky 2457322810Shselasky err = mlx5_mr_cache_cleanup(dev); 2458322810Shselasky if (err) 2459322810Shselasky mlx5_ib_warn(dev, "mr cache cleanup failed\n"); 2460322810Shselasky 2461331769Shselasky mlx5_ib_destroy_qp(dev->umrc.qp); 2462331769Shselasky ib_free_cq(dev->umrc.cq); 2463322810Shselasky ib_dealloc_pd(dev->umrc.pd); 2464322810Shselasky} 2465322810Shselasky 2466322810Shselaskyenum { 2467322810Shselasky MAX_UMR_WR = 128, 2468322810Shselasky}; 2469322810Shselasky 2470322810Shselaskystatic int create_umr_res(struct mlx5_ib_dev *dev) 2471322810Shselasky{ 2472331769Shselasky struct ib_qp_init_attr *init_attr = NULL; 2473331769Shselasky struct ib_qp_attr *attr = NULL; 2474322810Shselasky struct ib_pd *pd; 2475331769Shselasky struct ib_cq *cq; 2476331769Shselasky struct ib_qp *qp; 2477322810Shselasky int ret; 2478322810Shselasky 2479331769Shselasky attr = kzalloc(sizeof(*attr), GFP_KERNEL); 2480331769Shselasky init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL); 2481331769Shselasky if (!attr || !init_attr) { 2482331769Shselasky ret = -ENOMEM; 2483331769Shselasky goto error_0; 2484331769Shselasky } 2485331769Shselasky 2486331769Shselasky pd = ib_alloc_pd(&dev->ib_dev, 0); 2487322810Shselasky if (IS_ERR(pd)) { 2488322810Shselasky mlx5_ib_dbg(dev, "Couldn't create PD for sync UMR QP\n"); 2489322810Shselasky ret = PTR_ERR(pd); 2490322810Shselasky goto error_0; 2491322810Shselasky } 2492322810Shselasky 2493331769Shselasky cq = ib_alloc_cq(&dev->ib_dev, NULL, 128, 0, IB_POLL_SOFTIRQ); 2494331769Shselasky if (IS_ERR(cq)) { 2495331769Shselasky mlx5_ib_dbg(dev, "Couldn't create CQ for sync UMR QP\n"); 2496331769Shselasky ret = PTR_ERR(cq); 2497331769Shselasky goto error_2; 2498322810Shselasky } 2499322810Shselasky 2500331769Shselasky init_attr->send_cq = cq; 2501331769Shselasky init_attr->recv_cq = cq; 2502331769Shselasky init_attr->sq_sig_type = IB_SIGNAL_ALL_WR; 2503331769Shselasky init_attr->cap.max_send_wr = MAX_UMR_WR; 2504331769Shselasky init_attr->cap.max_send_sge = 1; 2505331769Shselasky init_attr->qp_type = MLX5_IB_QPT_REG_UMR; 2506331769Shselasky init_attr->port_num = 1; 2507331769Shselasky qp = mlx5_ib_create_qp(pd, init_attr, NULL); 2508331769Shselasky if (IS_ERR(qp)) { 2509331769Shselasky mlx5_ib_dbg(dev, "Couldn't create sync UMR QP\n"); 2510331769Shselasky ret = PTR_ERR(qp); 2511331769Shselasky goto error_3; 2512331769Shselasky } 2513331769Shselasky qp->device = &dev->ib_dev; 2514331769Shselasky qp->real_qp = qp; 2515331769Shselasky qp->uobject = NULL; 2516331769Shselasky qp->qp_type = MLX5_IB_QPT_REG_UMR; 2517331769Shselasky 2518331769Shselasky attr->qp_state = IB_QPS_INIT; 2519331769Shselasky attr->port_num = 1; 2520331769Shselasky ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_PKEY_INDEX | 2521331769Shselasky IB_QP_PORT, NULL); 2522331769Shselasky if (ret) { 2523331769Shselasky mlx5_ib_dbg(dev, "Couldn't modify UMR QP\n"); 2524331769Shselasky goto error_4; 2525331769Shselasky } 2526331769Shselasky 2527331769Shselasky memset(attr, 0, sizeof(*attr)); 2528331769Shselasky attr->qp_state = IB_QPS_RTR; 2529331769Shselasky attr->path_mtu = IB_MTU_256; 2530331769Shselasky 2531331769Shselasky ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL); 2532331769Shselasky if (ret) { 2533331769Shselasky mlx5_ib_dbg(dev, "Couldn't modify umr QP to rtr\n"); 2534331769Shselasky goto error_4; 2535331769Shselasky } 2536331769Shselasky 2537331769Shselasky memset(attr, 0, sizeof(*attr)); 2538331769Shselasky attr->qp_state = IB_QPS_RTS; 2539331769Shselasky ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL); 2540331769Shselasky if (ret) { 2541331769Shselasky mlx5_ib_dbg(dev, "Couldn't modify umr QP to rts\n"); 2542331769Shselasky goto error_4; 2543331769Shselasky } 2544331769Shselasky 2545331769Shselasky dev->umrc.qp = qp; 2546331769Shselasky dev->umrc.cq = cq; 2547322810Shselasky dev->umrc.pd = pd; 2548322810Shselasky 2549331769Shselasky sema_init(&dev->umrc.sem, MAX_UMR_WR); 2550322810Shselasky ret = mlx5_mr_cache_init(dev); 2551322810Shselasky if (ret) { 2552322810Shselasky mlx5_ib_warn(dev, "mr cache init failed %d\n", ret); 2553322810Shselasky goto error_4; 2554322810Shselasky } 2555322810Shselasky 2556331769Shselasky kfree(attr); 2557331769Shselasky kfree(init_attr); 2558331769Shselasky 2559322810Shselasky return 0; 2560322810Shselasky 2561322810Shselaskyerror_4: 2562331769Shselasky mlx5_ib_destroy_qp(qp); 2563331769Shselasky 2564331769Shselaskyerror_3: 2565331769Shselasky ib_free_cq(cq); 2566331769Shselasky 2567331769Shselaskyerror_2: 2568322810Shselasky ib_dealloc_pd(pd); 2569331769Shselasky 2570322810Shselaskyerror_0: 2571331769Shselasky kfree(attr); 2572331769Shselasky kfree(init_attr); 2573322810Shselasky return ret; 2574322810Shselasky} 2575322810Shselasky 2576322810Shselaskystatic int create_dev_resources(struct mlx5_ib_resources *devr) 2577322810Shselasky{ 2578322810Shselasky struct ib_srq_init_attr attr; 2579322810Shselasky struct mlx5_ib_dev *dev; 2580331769Shselasky struct ib_cq_init_attr cq_attr = {.cqe = 1}; 2581331769Shselasky int port; 2582322810Shselasky int ret = 0; 2583322810Shselasky 2584322810Shselasky dev = container_of(devr, struct mlx5_ib_dev, devr); 2585322810Shselasky 2586331769Shselasky mutex_init(&devr->mutex); 2587331769Shselasky 2588322810Shselasky devr->p0 = mlx5_ib_alloc_pd(&dev->ib_dev, NULL, NULL); 2589322810Shselasky if (IS_ERR(devr->p0)) { 2590322810Shselasky ret = PTR_ERR(devr->p0); 2591322810Shselasky goto error0; 2592322810Shselasky } 2593322810Shselasky devr->p0->device = &dev->ib_dev; 2594322810Shselasky devr->p0->uobject = NULL; 2595322810Shselasky atomic_set(&devr->p0->usecnt, 0); 2596322810Shselasky 2597322810Shselasky devr->c0 = mlx5_ib_create_cq(&dev->ib_dev, &cq_attr, NULL, NULL); 2598322810Shselasky if (IS_ERR(devr->c0)) { 2599322810Shselasky ret = PTR_ERR(devr->c0); 2600322810Shselasky goto error1; 2601322810Shselasky } 2602322810Shselasky devr->c0->device = &dev->ib_dev; 2603322810Shselasky devr->c0->uobject = NULL; 2604322810Shselasky devr->c0->comp_handler = NULL; 2605322810Shselasky devr->c0->event_handler = NULL; 2606322810Shselasky devr->c0->cq_context = NULL; 2607322810Shselasky atomic_set(&devr->c0->usecnt, 0); 2608322810Shselasky 2609322810Shselasky devr->x0 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL); 2610322810Shselasky if (IS_ERR(devr->x0)) { 2611322810Shselasky ret = PTR_ERR(devr->x0); 2612322810Shselasky goto error2; 2613322810Shselasky } 2614322810Shselasky devr->x0->device = &dev->ib_dev; 2615322810Shselasky devr->x0->inode = NULL; 2616322810Shselasky atomic_set(&devr->x0->usecnt, 0); 2617322810Shselasky mutex_init(&devr->x0->tgt_qp_mutex); 2618322810Shselasky INIT_LIST_HEAD(&devr->x0->tgt_qp_list); 2619322810Shselasky 2620322810Shselasky devr->x1 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL); 2621322810Shselasky if (IS_ERR(devr->x1)) { 2622322810Shselasky ret = PTR_ERR(devr->x1); 2623322810Shselasky goto error3; 2624322810Shselasky } 2625322810Shselasky devr->x1->device = &dev->ib_dev; 2626322810Shselasky devr->x1->inode = NULL; 2627322810Shselasky atomic_set(&devr->x1->usecnt, 0); 2628322810Shselasky mutex_init(&devr->x1->tgt_qp_mutex); 2629322810Shselasky INIT_LIST_HEAD(&devr->x1->tgt_qp_list); 2630322810Shselasky 2631322810Shselasky memset(&attr, 0, sizeof(attr)); 2632322810Shselasky attr.attr.max_sge = 1; 2633322810Shselasky attr.attr.max_wr = 1; 2634322810Shselasky attr.srq_type = IB_SRQT_XRC; 2635322810Shselasky attr.ext.xrc.cq = devr->c0; 2636322810Shselasky attr.ext.xrc.xrcd = devr->x0; 2637322810Shselasky 2638322810Shselasky devr->s0 = mlx5_ib_create_srq(devr->p0, &attr, NULL); 2639322810Shselasky if (IS_ERR(devr->s0)) { 2640322810Shselasky ret = PTR_ERR(devr->s0); 2641322810Shselasky goto error4; 2642322810Shselasky } 2643322810Shselasky devr->s0->device = &dev->ib_dev; 2644322810Shselasky devr->s0->pd = devr->p0; 2645322810Shselasky devr->s0->uobject = NULL; 2646322810Shselasky devr->s0->event_handler = NULL; 2647322810Shselasky devr->s0->srq_context = NULL; 2648322810Shselasky devr->s0->srq_type = IB_SRQT_XRC; 2649331769Shselasky devr->s0->ext.xrc.xrcd = devr->x0; 2650322810Shselasky devr->s0->ext.xrc.cq = devr->c0; 2651322810Shselasky atomic_inc(&devr->s0->ext.xrc.xrcd->usecnt); 2652322810Shselasky atomic_inc(&devr->s0->ext.xrc.cq->usecnt); 2653322810Shselasky atomic_inc(&devr->p0->usecnt); 2654322810Shselasky atomic_set(&devr->s0->usecnt, 0); 2655322810Shselasky 2656322810Shselasky memset(&attr, 0, sizeof(attr)); 2657322810Shselasky attr.attr.max_sge = 1; 2658322810Shselasky attr.attr.max_wr = 1; 2659322810Shselasky attr.srq_type = IB_SRQT_BASIC; 2660322810Shselasky devr->s1 = mlx5_ib_create_srq(devr->p0, &attr, NULL); 2661322810Shselasky if (IS_ERR(devr->s1)) { 2662322810Shselasky ret = PTR_ERR(devr->s1); 2663322810Shselasky goto error5; 2664322810Shselasky } 2665322810Shselasky devr->s1->device = &dev->ib_dev; 2666322810Shselasky devr->s1->pd = devr->p0; 2667322810Shselasky devr->s1->uobject = NULL; 2668322810Shselasky devr->s1->event_handler = NULL; 2669322810Shselasky devr->s1->srq_context = NULL; 2670322810Shselasky devr->s1->srq_type = IB_SRQT_BASIC; 2671322810Shselasky devr->s1->ext.xrc.cq = devr->c0; 2672322810Shselasky atomic_inc(&devr->p0->usecnt); 2673331769Shselasky atomic_set(&devr->s0->usecnt, 0); 2674322810Shselasky 2675331769Shselasky for (port = 0; port < ARRAY_SIZE(devr->ports); ++port) { 2676331769Shselasky INIT_WORK(&devr->ports[port].pkey_change_work, 2677331769Shselasky pkey_change_handler); 2678331769Shselasky devr->ports[port].devr = devr; 2679331769Shselasky } 2680331769Shselasky 2681322810Shselasky return 0; 2682322810Shselasky 2683322810Shselaskyerror5: 2684322810Shselasky mlx5_ib_destroy_srq(devr->s0); 2685322810Shselaskyerror4: 2686322810Shselasky mlx5_ib_dealloc_xrcd(devr->x1); 2687322810Shselaskyerror3: 2688322810Shselasky mlx5_ib_dealloc_xrcd(devr->x0); 2689322810Shselaskyerror2: 2690322810Shselasky mlx5_ib_destroy_cq(devr->c0); 2691322810Shselaskyerror1: 2692322810Shselasky mlx5_ib_dealloc_pd(devr->p0); 2693322810Shselaskyerror0: 2694322810Shselasky return ret; 2695322810Shselasky} 2696322810Shselasky 2697322810Shselaskystatic void destroy_dev_resources(struct mlx5_ib_resources *devr) 2698322810Shselasky{ 2699331769Shselasky struct mlx5_ib_dev *dev = 2700331769Shselasky container_of(devr, struct mlx5_ib_dev, devr); 2701331769Shselasky int port; 2702331769Shselasky 2703322810Shselasky mlx5_ib_destroy_srq(devr->s1); 2704322810Shselasky mlx5_ib_destroy_srq(devr->s0); 2705322810Shselasky mlx5_ib_dealloc_xrcd(devr->x0); 2706322810Shselasky mlx5_ib_dealloc_xrcd(devr->x1); 2707322810Shselasky mlx5_ib_destroy_cq(devr->c0); 2708322810Shselasky mlx5_ib_dealloc_pd(devr->p0); 2709331769Shselasky 2710331769Shselasky /* Make sure no change P_Key work items are still executing */ 2711331769Shselasky for (port = 0; port < dev->num_ports; ++port) 2712331769Shselasky cancel_work_sync(&devr->ports[port].pkey_change_work); 2713322810Shselasky} 2714322810Shselasky 2715325604Shselaskystatic u32 get_core_cap_flags(struct ib_device *ibdev) 2716325604Shselasky{ 2717325604Shselasky struct mlx5_ib_dev *dev = to_mdev(ibdev); 2718325604Shselasky enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, 1); 2719325604Shselasky u8 l3_type_cap = MLX5_CAP_ROCE(dev->mdev, l3_type); 2720325604Shselasky u8 roce_version_cap = MLX5_CAP_ROCE(dev->mdev, roce_version); 2721325604Shselasky u32 ret = 0; 2722325604Shselasky 2723325604Shselasky if (ll == IB_LINK_LAYER_INFINIBAND) 2724325604Shselasky return RDMA_CORE_PORT_IBA_IB; 2725325604Shselasky 2726325604Shselasky if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV4_CAP)) 2727331769Shselasky return 0; 2728325604Shselasky 2729325604Shselasky if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV6_CAP)) 2730331769Shselasky return 0; 2731325604Shselasky 2732325604Shselasky if (roce_version_cap & MLX5_ROCE_VERSION_1_CAP) 2733325604Shselasky ret |= RDMA_CORE_PORT_IBA_ROCE; 2734325604Shselasky 2735325604Shselasky if (roce_version_cap & MLX5_ROCE_VERSION_2_CAP) 2736325604Shselasky ret |= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP; 2737325604Shselasky 2738325604Shselasky return ret; 2739325604Shselasky} 2740325604Shselasky 2741325604Shselaskystatic int mlx5_port_immutable(struct ib_device *ibdev, u8 port_num, 2742325604Shselasky struct ib_port_immutable *immutable) 2743325604Shselasky{ 2744325604Shselasky struct ib_port_attr attr; 2745325604Shselasky int err; 2746325604Shselasky 2747331769Shselasky err = mlx5_ib_query_port(ibdev, port_num, &attr); 2748325604Shselasky if (err) 2749325604Shselasky return err; 2750325604Shselasky 2751325604Shselasky immutable->pkey_tbl_len = attr.pkey_tbl_len; 2752325604Shselasky immutable->gid_tbl_len = attr.gid_tbl_len; 2753325604Shselasky immutable->core_cap_flags = get_core_cap_flags(ibdev); 2754331769Shselasky immutable->max_mad_size = IB_MGMT_MAD_SIZE; 2755325604Shselasky 2756325604Shselasky return 0; 2757325604Shselasky} 2758325604Shselasky 2759331769Shselaskystatic void get_dev_fw_str(struct ib_device *ibdev, char *str, 2760331769Shselasky size_t str_len) 2761322810Shselasky{ 2762331769Shselasky struct mlx5_ib_dev *dev = 2763331769Shselasky container_of(ibdev, struct mlx5_ib_dev, ib_dev); 2764331769Shselasky snprintf(str, str_len, "%d.%d.%04d", fw_rev_maj(dev->mdev), 2765331769Shselasky fw_rev_min(dev->mdev), fw_rev_sub(dev->mdev)); 2766331769Shselasky} 2767322810Shselasky 2768331769Shselaskystatic int mlx5_roce_lag_init(struct mlx5_ib_dev *dev) 2769331769Shselasky{ 2770331769Shselasky return 0; 2771331769Shselasky} 2772322810Shselasky 2773331769Shselaskystatic void mlx5_roce_lag_cleanup(struct mlx5_ib_dev *dev) 2774331769Shselasky{ 2775331769Shselasky} 2776322810Shselasky 2777331769Shselaskystatic void mlx5_remove_roce_notifier(struct mlx5_ib_dev *dev) 2778331769Shselasky{ 2779331769Shselasky if (dev->roce.nb.notifier_call) { 2780331769Shselasky unregister_netdevice_notifier(&dev->roce.nb); 2781331769Shselasky dev->roce.nb.notifier_call = NULL; 2782322810Shselasky } 2783322810Shselasky} 2784322810Shselasky 2785331769Shselaskystatic int mlx5_enable_roce(struct mlx5_ib_dev *dev) 2786322810Shselasky{ 2787331769Shselasky VNET_ITERATOR_DECL(vnet_iter); 2788331769Shselasky struct net_device *idev; 2789322810Shselasky int err; 2790322810Shselasky 2791331769Shselasky /* Check if mlx5en net device already exists */ 2792331769Shselasky VNET_LIST_RLOCK(); 2793331769Shselasky VNET_FOREACH(vnet_iter) { 2794331769Shselasky IFNET_RLOCK(); 2795331769Shselasky CURVNET_SET_QUIET(vnet_iter); 2796331769Shselasky TAILQ_FOREACH(idev, &V_ifnet, if_link) { 2797331769Shselasky /* check if network interface belongs to mlx5en */ 2798331769Shselasky if (!mlx5_netdev_match(idev, dev->mdev, "mce")) 2799331769Shselasky continue; 2800331769Shselasky write_lock(&dev->roce.netdev_lock); 2801331769Shselasky dev->roce.netdev = idev; 2802331769Shselasky write_unlock(&dev->roce.netdev_lock); 2803331769Shselasky } 2804331769Shselasky CURVNET_RESTORE(); 2805331769Shselasky IFNET_RUNLOCK(); 2806331769Shselasky } 2807331769Shselasky VNET_LIST_RUNLOCK(); 2808322810Shselasky 2809331769Shselasky dev->roce.nb.notifier_call = mlx5_netdev_event; 2810331769Shselasky err = register_netdevice_notifier(&dev->roce.nb); 2811322810Shselasky if (err) { 2812331769Shselasky dev->roce.nb.notifier_call = NULL; 2813331769Shselasky return err; 2814322810Shselasky } 2815322810Shselasky 2816331769Shselasky err = mlx5_nic_vport_enable_roce(dev->mdev); 2817331769Shselasky if (err) 2818331769Shselasky goto err_unregister_netdevice_notifier; 2819322810Shselasky 2820331769Shselasky err = mlx5_roce_lag_init(dev); 2821331769Shselasky if (err) 2822331769Shselasky goto err_disable_roce; 2823322810Shselasky 2824331769Shselasky return 0; 2825322810Shselasky 2826331769Shselaskyerr_disable_roce: 2827331769Shselasky mlx5_nic_vport_disable_roce(dev->mdev); 2828322810Shselasky 2829331769Shselaskyerr_unregister_netdevice_notifier: 2830331769Shselasky mlx5_remove_roce_notifier(dev); 2831331769Shselasky return err; 2832322810Shselasky} 2833322810Shselasky 2834331769Shselaskystatic void mlx5_disable_roce(struct mlx5_ib_dev *dev) 2835322810Shselasky{ 2836331769Shselasky mlx5_roce_lag_cleanup(dev); 2837331769Shselasky mlx5_nic_vport_disable_roce(dev->mdev); 2838322810Shselasky} 2839322810Shselasky 2840322810Shselaskystatic void mlx5_ib_dealloc_q_port_counter(struct mlx5_ib_dev *dev, u8 port_num) 2841322810Shselasky{ 2842322810Shselasky mlx5_vport_dealloc_q_counter(dev->mdev, 2843322810Shselasky MLX5_INTERFACE_PROTOCOL_IB, 2844322810Shselasky dev->port[port_num].q_cnt_id); 2845322810Shselasky dev->port[port_num].q_cnt_id = 0; 2846322810Shselasky} 2847322810Shselasky 2848322810Shselaskystatic void mlx5_ib_dealloc_q_counters(struct mlx5_ib_dev *dev) 2849322810Shselasky{ 2850322810Shselasky unsigned int i; 2851322810Shselasky 2852322810Shselasky for (i = 0; i < dev->num_ports; i++) 2853322810Shselasky mlx5_ib_dealloc_q_port_counter(dev, i); 2854322810Shselasky} 2855322810Shselasky 2856322810Shselaskystatic int mlx5_ib_alloc_q_counters(struct mlx5_ib_dev *dev) 2857322810Shselasky{ 2858322810Shselasky int i; 2859322810Shselasky int ret; 2860322810Shselasky 2861322810Shselasky for (i = 0; i < dev->num_ports; i++) { 2862322810Shselasky ret = mlx5_vport_alloc_q_counter(dev->mdev, 2863322810Shselasky MLX5_INTERFACE_PROTOCOL_IB, 2864322810Shselasky &dev->port[i].q_cnt_id); 2865322810Shselasky if (ret) { 2866322810Shselasky mlx5_ib_warn(dev, 2867331769Shselasky "couldn't allocate queue counter for port %d, err %d\n", 2868331769Shselasky i + 1, ret); 2869322810Shselasky goto dealloc_counters; 2870322810Shselasky } 2871322810Shselasky } 2872322810Shselasky 2873322810Shselasky return 0; 2874322810Shselasky 2875322810Shselaskydealloc_counters: 2876322810Shselasky while (--i >= 0) 2877322810Shselasky mlx5_ib_dealloc_q_port_counter(dev, i); 2878322810Shselasky 2879322810Shselasky return ret; 2880322810Shselasky} 2881322810Shselasky 2882331769Shselaskystatic const char * const names[] = { 2883331769Shselasky "rx_write_requests", 2884331769Shselasky "rx_read_requests", 2885331769Shselasky "rx_atomic_requests", 2886331769Shselasky "out_of_buffer", 2887331769Shselasky "out_of_sequence", 2888331769Shselasky "duplicate_request", 2889331769Shselasky "rnr_nak_retry_err", 2890331769Shselasky "packet_seq_err", 2891331769Shselasky "implied_nak_seq_err", 2892331769Shselasky "local_ack_timeout_err", 2893322810Shselasky}; 2894322810Shselasky 2895331769Shselaskystatic const size_t stats_offsets[] = { 2896331769Shselasky MLX5_BYTE_OFF(query_q_counter_out, rx_write_requests), 2897331769Shselasky MLX5_BYTE_OFF(query_q_counter_out, rx_read_requests), 2898331769Shselasky MLX5_BYTE_OFF(query_q_counter_out, rx_atomic_requests), 2899331769Shselasky MLX5_BYTE_OFF(query_q_counter_out, out_of_buffer), 2900331769Shselasky MLX5_BYTE_OFF(query_q_counter_out, out_of_sequence), 2901331769Shselasky MLX5_BYTE_OFF(query_q_counter_out, duplicate_request), 2902331769Shselasky MLX5_BYTE_OFF(query_q_counter_out, rnr_nak_retry_err), 2903331769Shselasky MLX5_BYTE_OFF(query_q_counter_out, packet_seq_err), 2904331769Shselasky MLX5_BYTE_OFF(query_q_counter_out, implied_nak_seq_err), 2905331769Shselasky MLX5_BYTE_OFF(query_q_counter_out, local_ack_timeout_err), 2906322810Shselasky}; 2907322810Shselasky 2908331769Shselaskystatic struct rdma_hw_stats *mlx5_ib_alloc_hw_stats(struct ib_device *ibdev, 2909331769Shselasky u8 port_num) 2910322810Shselasky{ 2911331769Shselasky BUILD_BUG_ON(ARRAY_SIZE(names) != ARRAY_SIZE(stats_offsets)); 2912322810Shselasky 2913331769Shselasky /* We support only per port stats */ 2914331769Shselasky if (port_num == 0) 2915331769Shselasky return NULL; 2916322810Shselasky 2917331769Shselasky return rdma_alloc_hw_stats_struct(names, ARRAY_SIZE(names), 2918331769Shselasky RDMA_HW_STATS_DEFAULT_LIFESPAN); 2919322810Shselasky} 2920322810Shselasky 2921331769Shselaskystatic int mlx5_ib_get_hw_stats(struct ib_device *ibdev, 2922331769Shselasky struct rdma_hw_stats *stats, 2923331769Shselasky u8 port, int index) 2924322810Shselasky{ 2925331769Shselasky struct mlx5_ib_dev *dev = to_mdev(ibdev); 2926322810Shselasky int outlen = MLX5_ST_SZ_BYTES(query_q_counter_out); 2927322810Shselasky void *out; 2928331769Shselasky __be32 val; 2929322810Shselasky int ret; 2930331769Shselasky int i; 2931322810Shselasky 2932331769Shselasky if (!port || !stats) 2933331769Shselasky return -ENOSYS; 2934331769Shselasky 2935322810Shselasky out = mlx5_vzalloc(outlen); 2936322810Shselasky if (!out) 2937322810Shselasky return -ENOMEM; 2938322810Shselasky 2939331769Shselasky ret = mlx5_vport_query_q_counter(dev->mdev, 2940331769Shselasky dev->port[port - 1].q_cnt_id, 0, 2941331769Shselasky out, outlen); 2942322810Shselasky if (ret) 2943322810Shselasky goto free; 2944322810Shselasky 2945331769Shselasky for (i = 0; i < ARRAY_SIZE(names); i++) { 2946331769Shselasky val = *(__be32 *)(out + stats_offsets[i]); 2947331769Shselasky stats->value[i] = (u64)be32_to_cpu(val); 2948331769Shselasky } 2949322810Shselaskyfree: 2950331769Shselasky kvfree(out); 2951331769Shselasky return ARRAY_SIZE(names); 2952322810Shselasky} 2953322810Shselasky 2954322810Shselaskystatic void *mlx5_ib_add(struct mlx5_core_dev *mdev) 2955322810Shselasky{ 2956322810Shselasky struct mlx5_ib_dev *dev; 2957331769Shselasky enum rdma_link_layer ll; 2958331769Shselasky int port_type_cap; 2959322810Shselasky int err; 2960322810Shselasky int i; 2961322810Shselasky 2962331769Shselasky port_type_cap = MLX5_CAP_GEN(mdev, port_type); 2963331769Shselasky ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap); 2964331769Shselasky 2965331769Shselasky if ((ll == IB_LINK_LAYER_ETHERNET) && !MLX5_CAP_GEN(mdev, roce)) 2966331769Shselasky return NULL; 2967331769Shselasky 2968322810Shselasky dev = (struct mlx5_ib_dev *)ib_alloc_device(sizeof(*dev)); 2969322810Shselasky if (!dev) 2970322810Shselasky return NULL; 2971322810Shselasky 2972322810Shselasky dev->mdev = mdev; 2973322810Shselasky 2974322810Shselasky dev->port = kcalloc(MLX5_CAP_GEN(mdev, num_ports), sizeof(*dev->port), 2975331769Shselasky GFP_KERNEL); 2976322810Shselasky if (!dev->port) 2977322810Shselasky goto err_dealloc; 2978322810Shselasky 2979331769Shselasky rwlock_init(&dev->roce.netdev_lock); 2980322810Shselasky err = get_port_caps(dev); 2981322810Shselasky if (err) 2982322810Shselasky goto err_free_port; 2983322810Shselasky 2984322810Shselasky if (mlx5_use_mad_ifc(dev)) 2985322810Shselasky get_ext_port_caps(dev); 2986322810Shselasky 2987322810Shselasky MLX5_INIT_DOORBELL_LOCK(&dev->uar_lock); 2988322810Shselasky 2989337099Shselasky snprintf(dev->ib_dev.name, IB_DEVICE_NAME_MAX, "mlx5_%d", device_get_unit(mdev->pdev->dev.bsddev)); 2990322810Shselasky dev->ib_dev.owner = THIS_MODULE; 2991322810Shselasky dev->ib_dev.node_type = RDMA_NODE_IB_CA; 2992331769Shselasky dev->ib_dev.local_dma_lkey = 0 /* not supported for now */; 2993322810Shselasky dev->num_ports = MLX5_CAP_GEN(mdev, num_ports); 2994322810Shselasky dev->ib_dev.phys_port_cnt = dev->num_ports; 2995322810Shselasky dev->ib_dev.num_comp_vectors = 2996322810Shselasky dev->mdev->priv.eq_table.num_comp_vectors; 2997322810Shselasky dev->ib_dev.dma_device = &mdev->pdev->dev; 2998322810Shselasky 2999322810Shselasky dev->ib_dev.uverbs_abi_ver = MLX5_IB_UVERBS_ABI_VERSION; 3000322810Shselasky dev->ib_dev.uverbs_cmd_mask = 3001322810Shselasky (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) | 3002322810Shselasky (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) | 3003322810Shselasky (1ull << IB_USER_VERBS_CMD_QUERY_PORT) | 3004322810Shselasky (1ull << IB_USER_VERBS_CMD_ALLOC_PD) | 3005322810Shselasky (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) | 3006331784Shselasky (1ull << IB_USER_VERBS_CMD_CREATE_AH) | 3007331784Shselasky (1ull << IB_USER_VERBS_CMD_DESTROY_AH) | 3008322810Shselasky (1ull << IB_USER_VERBS_CMD_REG_MR) | 3009331769Shselasky (1ull << IB_USER_VERBS_CMD_REREG_MR) | 3010322810Shselasky (1ull << IB_USER_VERBS_CMD_DEREG_MR) | 3011322810Shselasky (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) | 3012322810Shselasky (1ull << IB_USER_VERBS_CMD_CREATE_CQ) | 3013322810Shselasky (1ull << IB_USER_VERBS_CMD_RESIZE_CQ) | 3014322810Shselasky (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) | 3015322810Shselasky (1ull << IB_USER_VERBS_CMD_CREATE_QP) | 3016322810Shselasky (1ull << IB_USER_VERBS_CMD_MODIFY_QP) | 3017322810Shselasky (1ull << IB_USER_VERBS_CMD_QUERY_QP) | 3018322810Shselasky (1ull << IB_USER_VERBS_CMD_DESTROY_QP) | 3019322810Shselasky (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST) | 3020322810Shselasky (1ull << IB_USER_VERBS_CMD_DETACH_MCAST) | 3021322810Shselasky (1ull << IB_USER_VERBS_CMD_CREATE_SRQ) | 3022322810Shselasky (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ) | 3023322810Shselasky (1ull << IB_USER_VERBS_CMD_QUERY_SRQ) | 3024322810Shselasky (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ) | 3025322810Shselasky (1ull << IB_USER_VERBS_CMD_CREATE_XSRQ) | 3026322810Shselasky (1ull << IB_USER_VERBS_CMD_OPEN_QP); 3027331769Shselasky dev->ib_dev.uverbs_ex_cmd_mask = 3028331769Shselasky (1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE) | 3029331769Shselasky (1ull << IB_USER_VERBS_EX_CMD_CREATE_CQ) | 3030331769Shselasky (1ull << IB_USER_VERBS_EX_CMD_CREATE_QP); 3031322810Shselasky 3032322810Shselasky dev->ib_dev.query_device = mlx5_ib_query_device; 3033322810Shselasky dev->ib_dev.query_port = mlx5_ib_query_port; 3034322810Shselasky dev->ib_dev.get_link_layer = mlx5_ib_port_link_layer; 3035331769Shselasky if (ll == IB_LINK_LAYER_ETHERNET) 3036331769Shselasky dev->ib_dev.get_netdev = mlx5_ib_get_netdev; 3037322810Shselasky dev->ib_dev.query_gid = mlx5_ib_query_gid; 3038331769Shselasky dev->ib_dev.add_gid = mlx5_ib_add_gid; 3039331769Shselasky dev->ib_dev.del_gid = mlx5_ib_del_gid; 3040322810Shselasky dev->ib_dev.query_pkey = mlx5_ib_query_pkey; 3041322810Shselasky dev->ib_dev.modify_device = mlx5_ib_modify_device; 3042322810Shselasky dev->ib_dev.modify_port = mlx5_ib_modify_port; 3043322810Shselasky dev->ib_dev.alloc_ucontext = mlx5_ib_alloc_ucontext; 3044322810Shselasky dev->ib_dev.dealloc_ucontext = mlx5_ib_dealloc_ucontext; 3045322810Shselasky dev->ib_dev.mmap = mlx5_ib_mmap; 3046322810Shselasky dev->ib_dev.alloc_pd = mlx5_ib_alloc_pd; 3047322810Shselasky dev->ib_dev.dealloc_pd = mlx5_ib_dealloc_pd; 3048322810Shselasky dev->ib_dev.create_ah = mlx5_ib_create_ah; 3049322810Shselasky dev->ib_dev.query_ah = mlx5_ib_query_ah; 3050322810Shselasky dev->ib_dev.destroy_ah = mlx5_ib_destroy_ah; 3051322810Shselasky dev->ib_dev.create_srq = mlx5_ib_create_srq; 3052322810Shselasky dev->ib_dev.modify_srq = mlx5_ib_modify_srq; 3053322810Shselasky dev->ib_dev.query_srq = mlx5_ib_query_srq; 3054322810Shselasky dev->ib_dev.destroy_srq = mlx5_ib_destroy_srq; 3055322810Shselasky dev->ib_dev.post_srq_recv = mlx5_ib_post_srq_recv; 3056322810Shselasky dev->ib_dev.create_qp = mlx5_ib_create_qp; 3057322810Shselasky dev->ib_dev.modify_qp = mlx5_ib_modify_qp; 3058322810Shselasky dev->ib_dev.query_qp = mlx5_ib_query_qp; 3059322810Shselasky dev->ib_dev.destroy_qp = mlx5_ib_destroy_qp; 3060322810Shselasky dev->ib_dev.post_send = mlx5_ib_post_send; 3061322810Shselasky dev->ib_dev.post_recv = mlx5_ib_post_recv; 3062322810Shselasky dev->ib_dev.create_cq = mlx5_ib_create_cq; 3063322810Shselasky dev->ib_dev.modify_cq = mlx5_ib_modify_cq; 3064322810Shselasky dev->ib_dev.resize_cq = mlx5_ib_resize_cq; 3065322810Shselasky dev->ib_dev.destroy_cq = mlx5_ib_destroy_cq; 3066322810Shselasky dev->ib_dev.poll_cq = mlx5_ib_poll_cq; 3067322810Shselasky dev->ib_dev.req_notify_cq = mlx5_ib_arm_cq; 3068322810Shselasky dev->ib_dev.get_dma_mr = mlx5_ib_get_dma_mr; 3069322810Shselasky dev->ib_dev.reg_user_mr = mlx5_ib_reg_user_mr; 3070331769Shselasky dev->ib_dev.rereg_user_mr = mlx5_ib_rereg_user_mr; 3071322810Shselasky dev->ib_dev.reg_phys_mr = mlx5_ib_reg_phys_mr; 3072322810Shselasky dev->ib_dev.dereg_mr = mlx5_ib_dereg_mr; 3073322810Shselasky dev->ib_dev.attach_mcast = mlx5_ib_mcg_attach; 3074322810Shselasky dev->ib_dev.detach_mcast = mlx5_ib_mcg_detach; 3075322810Shselasky dev->ib_dev.process_mad = mlx5_ib_process_mad; 3076331769Shselasky dev->ib_dev.alloc_mr = mlx5_ib_alloc_mr; 3077331769Shselasky dev->ib_dev.map_mr_sg = mlx5_ib_map_mr_sg; 3078331769Shselasky dev->ib_dev.check_mr_status = mlx5_ib_check_mr_status; 3079325604Shselasky dev->ib_dev.get_port_immutable = mlx5_port_immutable; 3080331769Shselasky dev->ib_dev.get_dev_fw_str = get_dev_fw_str; 3081331769Shselasky if (mlx5_core_is_pf(mdev)) { 3082331769Shselasky dev->ib_dev.get_vf_config = mlx5_ib_get_vf_config; 3083331769Shselasky dev->ib_dev.set_vf_link_state = mlx5_ib_set_vf_link_state; 3084331769Shselasky dev->ib_dev.get_vf_stats = mlx5_ib_get_vf_stats; 3085331769Shselasky dev->ib_dev.set_vf_guid = mlx5_ib_set_vf_guid; 3086331769Shselasky } 3087322810Shselasky 3088331769Shselasky mlx5_ib_internal_fill_odp_caps(dev); 3089331769Shselasky 3090331769Shselasky if (MLX5_CAP_GEN(mdev, imaicl)) { 3091331769Shselasky dev->ib_dev.alloc_mw = mlx5_ib_alloc_mw; 3092331769Shselasky dev->ib_dev.dealloc_mw = mlx5_ib_dealloc_mw; 3093331769Shselasky dev->ib_dev.uverbs_cmd_mask |= 3094331769Shselasky (1ull << IB_USER_VERBS_CMD_ALLOC_MW) | 3095331769Shselasky (1ull << IB_USER_VERBS_CMD_DEALLOC_MW); 3096331769Shselasky } 3097331769Shselasky 3098331769Shselasky if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt) && 3099331769Shselasky MLX5_CAP_GEN(dev->mdev, retransmission_q_counters)) { 3100331769Shselasky dev->ib_dev.get_hw_stats = mlx5_ib_get_hw_stats; 3101331769Shselasky dev->ib_dev.alloc_hw_stats = mlx5_ib_alloc_hw_stats; 3102331769Shselasky } 3103331769Shselasky 3104322810Shselasky if (MLX5_CAP_GEN(mdev, xrc)) { 3105322810Shselasky dev->ib_dev.alloc_xrcd = mlx5_ib_alloc_xrcd; 3106322810Shselasky dev->ib_dev.dealloc_xrcd = mlx5_ib_dealloc_xrcd; 3107322810Shselasky dev->ib_dev.uverbs_cmd_mask |= 3108322810Shselasky (1ull << IB_USER_VERBS_CMD_OPEN_XRCD) | 3109322810Shselasky (1ull << IB_USER_VERBS_CMD_CLOSE_XRCD); 3110322810Shselasky } 3111322810Shselasky 3112331769Shselasky if (mlx5_ib_port_link_layer(&dev->ib_dev, 1) == 3113331769Shselasky IB_LINK_LAYER_ETHERNET) { 3114331769Shselasky dev->ib_dev.create_flow = mlx5_ib_create_flow; 3115331769Shselasky dev->ib_dev.destroy_flow = mlx5_ib_destroy_flow; 3116331769Shselasky dev->ib_dev.create_wq = mlx5_ib_create_wq; 3117331769Shselasky dev->ib_dev.modify_wq = mlx5_ib_modify_wq; 3118331769Shselasky dev->ib_dev.destroy_wq = mlx5_ib_destroy_wq; 3119331769Shselasky dev->ib_dev.create_rwq_ind_table = mlx5_ib_create_rwq_ind_table; 3120331769Shselasky dev->ib_dev.destroy_rwq_ind_table = mlx5_ib_destroy_rwq_ind_table; 3121331769Shselasky dev->ib_dev.uverbs_ex_cmd_mask |= 3122331769Shselasky (1ull << IB_USER_VERBS_EX_CMD_CREATE_FLOW) | 3123331769Shselasky (1ull << IB_USER_VERBS_EX_CMD_DESTROY_FLOW) | 3124331769Shselasky (1ull << IB_USER_VERBS_EX_CMD_CREATE_WQ) | 3125331769Shselasky (1ull << IB_USER_VERBS_EX_CMD_MODIFY_WQ) | 3126331769Shselasky (1ull << IB_USER_VERBS_EX_CMD_DESTROY_WQ) | 3127331769Shselasky (1ull << IB_USER_VERBS_EX_CMD_CREATE_RWQ_IND_TBL) | 3128331769Shselasky (1ull << IB_USER_VERBS_EX_CMD_DESTROY_RWQ_IND_TBL); 3129331769Shselasky } 3130322810Shselasky err = init_node_data(dev); 3131322810Shselasky if (err) 3132331769Shselasky goto err_free_port; 3133322810Shselasky 3134331769Shselasky mutex_init(&dev->flow_db.lock); 3135322810Shselasky mutex_init(&dev->cap_mask_mutex); 3136322810Shselasky INIT_LIST_HEAD(&dev->qp_list); 3137322810Shselasky spin_lock_init(&dev->reset_flow_resource_lock); 3138322810Shselasky 3139331769Shselasky if (ll == IB_LINK_LAYER_ETHERNET) { 3140331769Shselasky err = mlx5_enable_roce(dev); 3141331769Shselasky if (err) 3142331769Shselasky goto err_free_port; 3143331769Shselasky } 3144331769Shselasky 3145322810Shselasky err = create_dev_resources(&dev->devr); 3146322810Shselasky if (err) 3147322810Shselasky goto err_disable_roce; 3148322810Shselasky 3149331769Shselasky err = mlx5_ib_odp_init_one(dev); 3150331769Shselasky if (err) 3151331769Shselasky goto err_rsrc; 3152322810Shselasky 3153322810Shselasky err = mlx5_ib_alloc_q_counters(dev); 3154322810Shselasky if (err) 3155322810Shselasky goto err_odp; 3156322810Shselasky 3157322810Shselasky err = ib_register_device(&dev->ib_dev, NULL); 3158322810Shselasky if (err) 3159322810Shselasky goto err_q_cnt; 3160322810Shselasky 3161322810Shselasky err = create_umr_res(dev); 3162322810Shselasky if (err) 3163322810Shselasky goto err_dev; 3164322810Shselasky 3165322810Shselasky for (i = 0; i < ARRAY_SIZE(mlx5_class_attributes); i++) { 3166322810Shselasky err = device_create_file(&dev->ib_dev.dev, 3167322810Shselasky mlx5_class_attributes[i]); 3168322810Shselasky if (err) 3169331769Shselasky goto err_umrc; 3170322810Shselasky } 3171322810Shselasky 3172331808Shselasky err = mlx5_ib_init_congestion(dev); 3173331808Shselasky if (err) 3174331808Shselasky goto err_umrc; 3175331808Shselasky 3176322810Shselasky dev->ib_active = true; 3177322810Shselasky 3178322810Shselasky return dev; 3179322810Shselasky 3180331769Shselaskyerr_umrc: 3181322810Shselasky destroy_umrc_res(dev); 3182322810Shselasky 3183322810Shselaskyerr_dev: 3184322810Shselasky ib_unregister_device(&dev->ib_dev); 3185322810Shselasky 3186322810Shselaskyerr_q_cnt: 3187322810Shselasky mlx5_ib_dealloc_q_counters(dev); 3188322810Shselasky 3189322810Shselaskyerr_odp: 3190331769Shselasky mlx5_ib_odp_remove_one(dev); 3191331769Shselasky 3192331769Shselaskyerr_rsrc: 3193322810Shselasky destroy_dev_resources(&dev->devr); 3194322810Shselasky 3195322810Shselaskyerr_disable_roce: 3196331769Shselasky if (ll == IB_LINK_LAYER_ETHERNET) { 3197331769Shselasky mlx5_disable_roce(dev); 3198331769Shselasky mlx5_remove_roce_notifier(dev); 3199331769Shselasky } 3200331769Shselasky 3201322810Shselaskyerr_free_port: 3202322810Shselasky kfree(dev->port); 3203322810Shselasky 3204322810Shselaskyerr_dealloc: 3205322810Shselasky ib_dealloc_device((struct ib_device *)dev); 3206322810Shselasky 3207322810Shselasky return NULL; 3208322810Shselasky} 3209322810Shselasky 3210322810Shselaskystatic void mlx5_ib_remove(struct mlx5_core_dev *mdev, void *context) 3211322810Shselasky{ 3212322810Shselasky struct mlx5_ib_dev *dev = context; 3213331769Shselasky enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev, 1); 3214322810Shselasky 3215331808Shselasky mlx5_ib_cleanup_congestion(dev); 3216331769Shselasky mlx5_remove_roce_notifier(dev); 3217331769Shselasky ib_unregister_device(&dev->ib_dev); 3218322810Shselasky mlx5_ib_dealloc_q_counters(dev); 3219322810Shselasky destroy_umrc_res(dev); 3220331769Shselasky mlx5_ib_odp_remove_one(dev); 3221322810Shselasky destroy_dev_resources(&dev->devr); 3222331769Shselasky if (ll == IB_LINK_LAYER_ETHERNET) 3223331769Shselasky mlx5_disable_roce(dev); 3224322810Shselasky kfree(dev->port); 3225322810Shselasky ib_dealloc_device(&dev->ib_dev); 3226322810Shselasky} 3227322810Shselasky 3228322810Shselaskystatic struct mlx5_interface mlx5_ib_interface = { 3229322810Shselasky .add = mlx5_ib_add, 3230322810Shselasky .remove = mlx5_ib_remove, 3231322810Shselasky .event = mlx5_ib_event, 3232322810Shselasky .protocol = MLX5_INTERFACE_PROTOCOL_IB, 3233322810Shselasky}; 3234322810Shselasky 3235322810Shselaskystatic int __init mlx5_ib_init(void) 3236322810Shselasky{ 3237322810Shselasky int err; 3238322810Shselasky 3239322810Shselasky if (deprecated_prof_sel != 2) 3240331769Shselasky pr_warn("prof_sel is deprecated for mlx5_ib, set it for mlx5_core\n"); 3241322810Shselasky 3242331769Shselasky err = mlx5_ib_odp_init(); 3243331769Shselasky if (err) 3244331769Shselasky return err; 3245331769Shselasky 3246322810Shselasky err = mlx5_register_interface(&mlx5_ib_interface); 3247322810Shselasky if (err) 3248322810Shselasky goto clean_odp; 3249322810Shselasky 3250322810Shselasky return err; 3251322810Shselasky 3252322810Shselaskyclean_odp: 3253331769Shselasky mlx5_ib_odp_cleanup(); 3254322810Shselasky return err; 3255322810Shselasky} 3256322810Shselasky 3257322810Shselaskystatic void __exit mlx5_ib_cleanup(void) 3258322810Shselasky{ 3259322810Shselasky mlx5_unregister_interface(&mlx5_ib_interface); 3260331769Shselasky mlx5_ib_odp_cleanup(); 3261322810Shselasky} 3262322810Shselasky 3263341948Shselaskystatic void 3264341948Shselaskymlx5_ib_show_version(void __unused *arg) 3265341948Shselasky{ 3266341948Shselasky 3267341948Shselasky printf("%s", mlx5_version); 3268341948Shselasky} 3269341948ShselaskySYSINIT(mlx5_ib_show_version, SI_SUB_DRIVERS, SI_ORDER_ANY, mlx5_ib_show_version, NULL); 3270341948Shselasky 3271322810Shselaskymodule_init_order(mlx5_ib_init, SI_ORDER_THIRD); 3272322810Shselaskymodule_exit_order(mlx5_ib_cleanup, SI_ORDER_THIRD); 3273