1b572ebe6SLeon Romanovsky // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB 2e126ba97SEli Cohen /* 3b572ebe6SLeon Romanovsky * Copyright (c) 2013-2020, Mellanox Technologies inc. All rights reserved. 490da7dc8SJianxin Xiong * Copyright (c) 2020, Intel Corporation. All rights reserved. 5e126ba97SEli Cohen */ 6e126ba97SEli Cohen 7fe248c3aSMaor Gottlieb #include <linux/debugfs.h> 8adec640eSChristoph Hellwig #include <linux/highmem.h> 9e126ba97SEli Cohen #include <linux/module.h> 10e126ba97SEli Cohen #include <linux/init.h> 11e126ba97SEli Cohen #include <linux/errno.h> 12e126ba97SEli Cohen #include <linux/pci.h> 13e126ba97SEli Cohen #include <linux/dma-mapping.h> 14e126ba97SEli Cohen #include <linux/slab.h> 1524da0016SAriel Levkovich #include <linux/bitmap.h> 16e126ba97SEli Cohen #include <linux/sched.h> 176e84f315SIngo Molnar #include <linux/sched/mm.h> 180881e7bdSIngo Molnar #include <linux/sched/task.h> 197c2344c3SMaor Gottlieb #include <linux/delay.h> 20e126ba97SEli Cohen #include <rdma/ib_user_verbs.h> 213f89a643SAchiad Shochat #include <rdma/ib_addr.h> 222811ba51SAchiad Shochat #include <rdma/ib_cache.h> 23ada68c31SAchiad Shochat #include <linux/mlx5/port.h> 241b5daf11SMajd Dibbiny #include <linux/mlx5/vport.h> 2572c7fe90SPravin Shedge #include <linux/mlx5/fs.h> 26cecae747SMaor Gottlieb #include <linux/mlx5/eswitch.h> 277c2344c3SMaor Gottlieb #include <linux/list.h> 28e126ba97SEli Cohen #include <rdma/ib_smi.h> 29e126ba97SEli Cohen #include <rdma/ib_umem.h> 30cfc1a89eSMaor Gottlieb #include <rdma/lag.h> 31038d2ef8SMaor Gottlieb #include <linux/in.h> 32038d2ef8SMaor Gottlieb #include <linux/etherdevice.h> 33e126ba97SEli Cohen #include "mlx5_ib.h" 34fc385b7aSMark Bloch #include "ib_rep.h" 35e1f24a79SParav Pandit #include "cmd.h" 36d8b7515eSLeon Romanovsky #include "devx.h" 37831df883SMaor Gottlieb #include "dm.h" 38f7c4ffdaSLeon Romanovsky #include "fs.h" 39f3da6577SLeon Romanovsky #include "srq.h" 40333fbaa0SLeon Romanovsky #include "qp.h" 41029e88fdSLeon Romanovsky #include "wr.h" 42b572ebe6SLeon Romanovsky #include "restrack.h" 4364825827SLeon Romanovsky #include "counters.h" 44*04876c12SAharon Landau #include "umr.h" 458c84660bSMatan Barak #include <rdma/uverbs_std_types.h> 462904bb37SYishai Hadas #include <rdma/uverbs_ioctl.h> 47c6475a0bSAviad Yehezkel #include <rdma/mlx5_user_ioctl_verbs.h> 48c6475a0bSAviad Yehezkel #include <rdma/mlx5_user_ioctl_cmds.h> 494061ff7aSErez Alfasi #include <rdma/ib_umem_odp.h> 508c84660bSMatan Barak 518c84660bSMatan Barak #define UVERBS_MODULE_NAME mlx5_ib 528c84660bSMatan Barak #include <rdma/uverbs_named_ioctl.h> 53e126ba97SEli Cohen 54e126ba97SEli Cohen MODULE_AUTHOR("Eli Cohen <eli@mellanox.com>"); 55daeee976SShay Drory MODULE_DESCRIPTION("Mellanox 5th generation network adapters (ConnectX series) IB driver"); 56e126ba97SEli Cohen MODULE_LICENSE("Dual BSD/GPL"); 57e126ba97SEli Cohen 58d69a24e0SDaniel Jurgens struct mlx5_ib_event_work { 59d69a24e0SDaniel Jurgens struct work_struct work; 60df097a27SSaeed Mahameed union { 61df097a27SSaeed Mahameed struct mlx5_ib_dev *dev; 62df097a27SSaeed Mahameed struct mlx5_ib_multiport_info *mpi; 63df097a27SSaeed Mahameed }; 64df097a27SSaeed Mahameed bool is_slave; 65134e9349SSaeed Mahameed unsigned int event; 66df097a27SSaeed Mahameed void *param; 67d69a24e0SDaniel Jurgens }; 68d69a24e0SDaniel Jurgens 69da7525d2SEran Ben Elisha enum { 70da7525d2SEran Ben Elisha MLX5_ATOMIC_SIZE_QP_8BYTES = 1 << 3, 71da7525d2SEran Ben Elisha }; 721b5daf11SMajd Dibbiny 73d69a24e0SDaniel Jurgens static struct workqueue_struct *mlx5_ib_event_wq; 7432f69e4bSDaniel Jurgens static LIST_HEAD(mlx5_ib_unaffiliated_port_list); 7532f69e4bSDaniel Jurgens static LIST_HEAD(mlx5_ib_dev_list); 7632f69e4bSDaniel Jurgens /* 7732f69e4bSDaniel Jurgens * This mutex should be held when accessing either of the above lists 7832f69e4bSDaniel Jurgens */ 7932f69e4bSDaniel Jurgens static DEFINE_MUTEX(mlx5_ib_multiport_mutex); 8032f69e4bSDaniel Jurgens 8132f69e4bSDaniel Jurgens struct mlx5_ib_dev *mlx5_ib_get_ibdev_from_mpi(struct mlx5_ib_multiport_info *mpi) 8232f69e4bSDaniel Jurgens { 8332f69e4bSDaniel Jurgens struct mlx5_ib_dev *dev; 8432f69e4bSDaniel Jurgens 8532f69e4bSDaniel Jurgens mutex_lock(&mlx5_ib_multiport_mutex); 8632f69e4bSDaniel Jurgens dev = mpi->ibdev; 8732f69e4bSDaniel Jurgens mutex_unlock(&mlx5_ib_multiport_mutex); 8832f69e4bSDaniel Jurgens return dev; 8932f69e4bSDaniel Jurgens } 9032f69e4bSDaniel Jurgens 911b5daf11SMajd Dibbiny static enum rdma_link_layer 92ebd61f68SAchiad Shochat mlx5_port_type_cap_to_rdma_ll(int port_type_cap) 931b5daf11SMajd Dibbiny { 94ebd61f68SAchiad Shochat switch (port_type_cap) { 951b5daf11SMajd Dibbiny case MLX5_CAP_PORT_TYPE_IB: 961b5daf11SMajd Dibbiny return IB_LINK_LAYER_INFINIBAND; 971b5daf11SMajd Dibbiny case MLX5_CAP_PORT_TYPE_ETH: 981b5daf11SMajd Dibbiny return IB_LINK_LAYER_ETHERNET; 991b5daf11SMajd Dibbiny default: 1001b5daf11SMajd Dibbiny return IB_LINK_LAYER_UNSPECIFIED; 1011b5daf11SMajd Dibbiny } 1021b5daf11SMajd Dibbiny } 1031b5daf11SMajd Dibbiny 104ebd61f68SAchiad Shochat static enum rdma_link_layer 1051fb7f897SMark Bloch mlx5_ib_port_link_layer(struct ib_device *device, u32 port_num) 106ebd61f68SAchiad Shochat { 107ebd61f68SAchiad Shochat struct mlx5_ib_dev *dev = to_mdev(device); 108ebd61f68SAchiad Shochat int port_type_cap = MLX5_CAP_GEN(dev->mdev, port_type); 109ebd61f68SAchiad Shochat 110ebd61f68SAchiad Shochat return mlx5_port_type_cap_to_rdma_ll(port_type_cap); 111ebd61f68SAchiad Shochat } 112ebd61f68SAchiad Shochat 113fd65f1b8SMoni Shoua static int get_port_state(struct ib_device *ibdev, 1141fb7f897SMark Bloch u32 port_num, 115fd65f1b8SMoni Shoua enum ib_port_state *state) 116fd65f1b8SMoni Shoua { 117fd65f1b8SMoni Shoua struct ib_port_attr attr; 118fd65f1b8SMoni Shoua int ret; 119fd65f1b8SMoni Shoua 120fd65f1b8SMoni Shoua memset(&attr, 0, sizeof(attr)); 1213023a1e9SKamal Heib ret = ibdev->ops.query_port(ibdev, port_num, &attr); 122fd65f1b8SMoni Shoua if (!ret) 123fd65f1b8SMoni Shoua *state = attr.state; 124fd65f1b8SMoni Shoua return ret; 125fd65f1b8SMoni Shoua } 126fd65f1b8SMoni Shoua 12735b0aa67SMark Bloch static struct mlx5_roce *mlx5_get_rep_roce(struct mlx5_ib_dev *dev, 12835b0aa67SMark Bloch struct net_device *ndev, 129c446d9daSMark Bloch struct net_device *upper, 1301fb7f897SMark Bloch u32 *port_num) 13135b0aa67SMark Bloch { 13235b0aa67SMark Bloch struct net_device *rep_ndev; 13335b0aa67SMark Bloch struct mlx5_ib_port *port; 13435b0aa67SMark Bloch int i; 13535b0aa67SMark Bloch 13635b0aa67SMark Bloch for (i = 0; i < dev->num_ports; i++) { 13735b0aa67SMark Bloch port = &dev->port[i]; 13835b0aa67SMark Bloch if (!port->rep) 13935b0aa67SMark Bloch continue; 14035b0aa67SMark Bloch 141c446d9daSMark Bloch if (upper == ndev && port->rep->vport == MLX5_VPORT_UPLINK) { 142c446d9daSMark Bloch *port_num = i + 1; 143c446d9daSMark Bloch return &port->roce; 144c446d9daSMark Bloch } 145c446d9daSMark Bloch 146c446d9daSMark Bloch if (upper && port->rep->vport == MLX5_VPORT_UPLINK) 147c446d9daSMark Bloch continue; 148c446d9daSMark Bloch 14935b0aa67SMark Bloch read_lock(&port->roce.netdev_lock); 150658cfcebSMark Bloch rep_ndev = mlx5_ib_get_rep_netdev(port->rep->esw, 15135b0aa67SMark Bloch port->rep->vport); 15235b0aa67SMark Bloch if (rep_ndev == ndev) { 15335b0aa67SMark Bloch read_unlock(&port->roce.netdev_lock); 15435b0aa67SMark Bloch *port_num = i + 1; 15535b0aa67SMark Bloch return &port->roce; 15635b0aa67SMark Bloch } 15735b0aa67SMark Bloch read_unlock(&port->roce.netdev_lock); 15835b0aa67SMark Bloch } 15935b0aa67SMark Bloch 16035b0aa67SMark Bloch return NULL; 16135b0aa67SMark Bloch } 16235b0aa67SMark Bloch 163fc24fc5eSAchiad Shochat static int mlx5_netdev_event(struct notifier_block *this, 164fc24fc5eSAchiad Shochat unsigned long event, void *ptr) 165fc24fc5eSAchiad Shochat { 1667fd8aefbSDaniel Jurgens struct mlx5_roce *roce = container_of(this, struct mlx5_roce, nb); 167fc24fc5eSAchiad Shochat struct net_device *ndev = netdev_notifier_info_to_dev(ptr); 1681fb7f897SMark Bloch u32 port_num = roce->native_port_num; 1697fd8aefbSDaniel Jurgens struct mlx5_core_dev *mdev; 1707fd8aefbSDaniel Jurgens struct mlx5_ib_dev *ibdev; 1717fd8aefbSDaniel Jurgens 1727fd8aefbSDaniel Jurgens ibdev = roce->dev; 17332f69e4bSDaniel Jurgens mdev = mlx5_ib_get_native_port_mdev(ibdev, port_num, NULL); 17432f69e4bSDaniel Jurgens if (!mdev) 17532f69e4bSDaniel Jurgens return NOTIFY_DONE; 176fc24fc5eSAchiad Shochat 1775ec8c83eSAviv Heller switch (event) { 1785ec8c83eSAviv Heller case NETDEV_REGISTER: 17935b0aa67SMark Bloch /* Should already be registered during the load */ 18035b0aa67SMark Bloch if (ibdev->is_rep) 18135b0aa67SMark Bloch break; 1827fd8aefbSDaniel Jurgens write_lock(&roce->netdev_lock); 183dce45af5SLinus Torvalds if (ndev->dev.parent == mdev->device) 184842a9c83SOr Gerlitz roce->netdev = ndev; 1857fd8aefbSDaniel Jurgens write_unlock(&roce->netdev_lock); 1865ec8c83eSAviv Heller break; 1875ec8c83eSAviv Heller 188842a9c83SOr Gerlitz case NETDEV_UNREGISTER: 18935b0aa67SMark Bloch /* In case of reps, ib device goes away before the netdevs */ 190842a9c83SOr Gerlitz write_lock(&roce->netdev_lock); 191842a9c83SOr Gerlitz if (roce->netdev == ndev) 192842a9c83SOr Gerlitz roce->netdev = NULL; 193842a9c83SOr Gerlitz write_unlock(&roce->netdev_lock); 194842a9c83SOr Gerlitz break; 195842a9c83SOr Gerlitz 196fd65f1b8SMoni Shoua case NETDEV_CHANGE: 1975ec8c83eSAviv Heller case NETDEV_UP: 19888621dfeSAviv Heller case NETDEV_DOWN: { 1997fd8aefbSDaniel Jurgens struct net_device *lag_ndev = mlx5_lag_get_roce_netdev(mdev); 20088621dfeSAviv Heller struct net_device *upper = NULL; 20188621dfeSAviv Heller 20288621dfeSAviv Heller if (lag_ndev) { 20388621dfeSAviv Heller upper = netdev_master_upper_dev_get(lag_ndev); 20488621dfeSAviv Heller dev_put(lag_ndev); 20588621dfeSAviv Heller } 20688621dfeSAviv Heller 20735b0aa67SMark Bloch if (ibdev->is_rep) 208c446d9daSMark Bloch roce = mlx5_get_rep_roce(ibdev, ndev, upper, &port_num); 20935b0aa67SMark Bloch if (!roce) 21035b0aa67SMark Bloch return NOTIFY_DONE; 211c446d9daSMark Bloch if ((upper == ndev || 212c446d9daSMark Bloch ((!upper || ibdev->is_rep) && ndev == roce->netdev)) && 213c446d9daSMark Bloch ibdev->ib_active) { 214626bc02dSBart Van Assche struct ib_event ibev = { }; 215fd65f1b8SMoni Shoua enum ib_port_state port_state; 2165ec8c83eSAviv Heller 2177fd8aefbSDaniel Jurgens if (get_port_state(&ibdev->ib_dev, port_num, 2187fd8aefbSDaniel Jurgens &port_state)) 2197fd8aefbSDaniel Jurgens goto done; 220fd65f1b8SMoni Shoua 2217fd8aefbSDaniel Jurgens if (roce->last_port_state == port_state) 2227fd8aefbSDaniel Jurgens goto done; 223fd65f1b8SMoni Shoua 2247fd8aefbSDaniel Jurgens roce->last_port_state = port_state; 2255ec8c83eSAviv Heller ibev.device = &ibdev->ib_dev; 226fd65f1b8SMoni Shoua if (port_state == IB_PORT_DOWN) 227fd65f1b8SMoni Shoua ibev.event = IB_EVENT_PORT_ERR; 228fd65f1b8SMoni Shoua else if (port_state == IB_PORT_ACTIVE) 229fd65f1b8SMoni Shoua ibev.event = IB_EVENT_PORT_ACTIVE; 230fd65f1b8SMoni Shoua else 2317fd8aefbSDaniel Jurgens goto done; 232fd65f1b8SMoni Shoua 2337fd8aefbSDaniel Jurgens ibev.element.port_num = port_num; 2345ec8c83eSAviv Heller ib_dispatch_event(&ibev); 2355ec8c83eSAviv Heller } 2365ec8c83eSAviv Heller break; 23788621dfeSAviv Heller } 2385ec8c83eSAviv Heller 2395ec8c83eSAviv Heller default: 2405ec8c83eSAviv Heller break; 2415ec8c83eSAviv Heller } 2427fd8aefbSDaniel Jurgens done: 24332f69e4bSDaniel Jurgens mlx5_ib_put_native_port_mdev(ibdev, port_num); 244fc24fc5eSAchiad Shochat return NOTIFY_DONE; 245fc24fc5eSAchiad Shochat } 246fc24fc5eSAchiad Shochat 247fc24fc5eSAchiad Shochat static struct net_device *mlx5_ib_get_netdev(struct ib_device *device, 2481fb7f897SMark Bloch u32 port_num) 249fc24fc5eSAchiad Shochat { 250fc24fc5eSAchiad Shochat struct mlx5_ib_dev *ibdev = to_mdev(device); 251fc24fc5eSAchiad Shochat struct net_device *ndev; 25232f69e4bSDaniel Jurgens struct mlx5_core_dev *mdev; 253fc24fc5eSAchiad Shochat 25432f69e4bSDaniel Jurgens mdev = mlx5_ib_get_native_port_mdev(ibdev, port_num, NULL); 25532f69e4bSDaniel Jurgens if (!mdev) 25632f69e4bSDaniel Jurgens return NULL; 25732f69e4bSDaniel Jurgens 25832f69e4bSDaniel Jurgens ndev = mlx5_lag_get_roce_netdev(mdev); 25988621dfeSAviv Heller if (ndev) 26032f69e4bSDaniel Jurgens goto out; 26188621dfeSAviv Heller 262fc24fc5eSAchiad Shochat /* Ensure ndev does not disappear before we invoke dev_hold() 263fc24fc5eSAchiad Shochat */ 26495579e78SMark Bloch read_lock(&ibdev->port[port_num - 1].roce.netdev_lock); 26595579e78SMark Bloch ndev = ibdev->port[port_num - 1].roce.netdev; 266fc24fc5eSAchiad Shochat if (ndev) 267fc24fc5eSAchiad Shochat dev_hold(ndev); 26895579e78SMark Bloch read_unlock(&ibdev->port[port_num - 1].roce.netdev_lock); 269fc24fc5eSAchiad Shochat 27032f69e4bSDaniel Jurgens out: 27132f69e4bSDaniel Jurgens mlx5_ib_put_native_port_mdev(ibdev, port_num); 272fc24fc5eSAchiad Shochat return ndev; 273fc24fc5eSAchiad Shochat } 274fc24fc5eSAchiad Shochat 27532f69e4bSDaniel Jurgens struct mlx5_core_dev *mlx5_ib_get_native_port_mdev(struct mlx5_ib_dev *ibdev, 2761fb7f897SMark Bloch u32 ib_port_num, 2771fb7f897SMark Bloch u32 *native_port_num) 27832f69e4bSDaniel Jurgens { 27932f69e4bSDaniel Jurgens enum rdma_link_layer ll = mlx5_ib_port_link_layer(&ibdev->ib_dev, 28032f69e4bSDaniel Jurgens ib_port_num); 28132f69e4bSDaniel Jurgens struct mlx5_core_dev *mdev = NULL; 28232f69e4bSDaniel Jurgens struct mlx5_ib_multiport_info *mpi; 28332f69e4bSDaniel Jurgens struct mlx5_ib_port *port; 28432f69e4bSDaniel Jurgens 285210b1f78SMark Bloch if (!mlx5_core_mp_enabled(ibdev->mdev) || 286210b1f78SMark Bloch ll != IB_LINK_LAYER_ETHERNET) { 287210b1f78SMark Bloch if (native_port_num) 288210b1f78SMark Bloch *native_port_num = ib_port_num; 289210b1f78SMark Bloch return ibdev->mdev; 290210b1f78SMark Bloch } 291210b1f78SMark Bloch 29232f69e4bSDaniel Jurgens if (native_port_num) 29332f69e4bSDaniel Jurgens *native_port_num = 1; 29432f69e4bSDaniel Jurgens 29532f69e4bSDaniel Jurgens port = &ibdev->port[ib_port_num - 1]; 29632f69e4bSDaniel Jurgens spin_lock(&port->mp.mpi_lock); 29732f69e4bSDaniel Jurgens mpi = ibdev->port[ib_port_num - 1].mp.mpi; 29832f69e4bSDaniel Jurgens if (mpi && !mpi->unaffiliate) { 29932f69e4bSDaniel Jurgens mdev = mpi->mdev; 30032f69e4bSDaniel Jurgens /* If it's the master no need to refcount, it'll exist 30132f69e4bSDaniel Jurgens * as long as the ib_dev exists. 30232f69e4bSDaniel Jurgens */ 30332f69e4bSDaniel Jurgens if (!mpi->is_master) 30432f69e4bSDaniel Jurgens mpi->mdev_refcnt++; 30532f69e4bSDaniel Jurgens } 30632f69e4bSDaniel Jurgens spin_unlock(&port->mp.mpi_lock); 30732f69e4bSDaniel Jurgens 30832f69e4bSDaniel Jurgens return mdev; 30932f69e4bSDaniel Jurgens } 31032f69e4bSDaniel Jurgens 3111fb7f897SMark Bloch void mlx5_ib_put_native_port_mdev(struct mlx5_ib_dev *ibdev, u32 port_num) 31232f69e4bSDaniel Jurgens { 31332f69e4bSDaniel Jurgens enum rdma_link_layer ll = mlx5_ib_port_link_layer(&ibdev->ib_dev, 31432f69e4bSDaniel Jurgens port_num); 31532f69e4bSDaniel Jurgens struct mlx5_ib_multiport_info *mpi; 31632f69e4bSDaniel Jurgens struct mlx5_ib_port *port; 31732f69e4bSDaniel Jurgens 31832f69e4bSDaniel Jurgens if (!mlx5_core_mp_enabled(ibdev->mdev) || ll != IB_LINK_LAYER_ETHERNET) 31932f69e4bSDaniel Jurgens return; 32032f69e4bSDaniel Jurgens 32132f69e4bSDaniel Jurgens port = &ibdev->port[port_num - 1]; 32232f69e4bSDaniel Jurgens 32332f69e4bSDaniel Jurgens spin_lock(&port->mp.mpi_lock); 32432f69e4bSDaniel Jurgens mpi = ibdev->port[port_num - 1].mp.mpi; 32532f69e4bSDaniel Jurgens if (mpi->is_master) 32632f69e4bSDaniel Jurgens goto out; 32732f69e4bSDaniel Jurgens 32832f69e4bSDaniel Jurgens mpi->mdev_refcnt--; 32932f69e4bSDaniel Jurgens if (mpi->unaffiliate) 33032f69e4bSDaniel Jurgens complete(&mpi->unref_comp); 33132f69e4bSDaniel Jurgens out: 33232f69e4bSDaniel Jurgens spin_unlock(&port->mp.mpi_lock); 33332f69e4bSDaniel Jurgens } 33432f69e4bSDaniel Jurgens 335639bf441SAharon Landau static int translate_eth_legacy_proto_oper(u32 eth_proto_oper, 336639bf441SAharon Landau u16 *active_speed, u8 *active_width) 337f1b65df5SNoa Osherovich { 338f1b65df5SNoa Osherovich switch (eth_proto_oper) { 339f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_1000BASE_CX_SGMII): 340f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_1000BASE_KX): 341f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_100BASE_TX): 342f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_1000BASE_T): 343f1b65df5SNoa Osherovich *active_width = IB_WIDTH_1X; 344f1b65df5SNoa Osherovich *active_speed = IB_SPEED_SDR; 345f1b65df5SNoa Osherovich break; 346f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_10GBASE_T): 347f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_10GBASE_CX4): 348f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_10GBASE_KX4): 349f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_10GBASE_KR): 350f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_10GBASE_CR): 351f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_10GBASE_SR): 352f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_10GBASE_ER): 353f1b65df5SNoa Osherovich *active_width = IB_WIDTH_1X; 354f1b65df5SNoa Osherovich *active_speed = IB_SPEED_QDR; 355f1b65df5SNoa Osherovich break; 356f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_25GBASE_CR): 357f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_25GBASE_KR): 358f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_25GBASE_SR): 359f1b65df5SNoa Osherovich *active_width = IB_WIDTH_1X; 360f1b65df5SNoa Osherovich *active_speed = IB_SPEED_EDR; 361f1b65df5SNoa Osherovich break; 362f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_40GBASE_CR4): 363f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_40GBASE_KR4): 364f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_40GBASE_SR4): 365f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_40GBASE_LR4): 366f1b65df5SNoa Osherovich *active_width = IB_WIDTH_4X; 367f1b65df5SNoa Osherovich *active_speed = IB_SPEED_QDR; 368f1b65df5SNoa Osherovich break; 369f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_50GBASE_CR2): 370f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_50GBASE_KR2): 371f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_50GBASE_SR2): 372f1b65df5SNoa Osherovich *active_width = IB_WIDTH_1X; 373f1b65df5SNoa Osherovich *active_speed = IB_SPEED_HDR; 374f1b65df5SNoa Osherovich break; 375f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_56GBASE_R4): 376f1b65df5SNoa Osherovich *active_width = IB_WIDTH_4X; 377f1b65df5SNoa Osherovich *active_speed = IB_SPEED_FDR; 378f1b65df5SNoa Osherovich break; 379f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_100GBASE_CR4): 380f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_100GBASE_SR4): 381f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_100GBASE_KR4): 382f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_100GBASE_LR4): 383f1b65df5SNoa Osherovich *active_width = IB_WIDTH_4X; 384f1b65df5SNoa Osherovich *active_speed = IB_SPEED_EDR; 385f1b65df5SNoa Osherovich break; 386f1b65df5SNoa Osherovich default: 387f1b65df5SNoa Osherovich return -EINVAL; 388f1b65df5SNoa Osherovich } 389f1b65df5SNoa Osherovich 390f1b65df5SNoa Osherovich return 0; 391f1b65df5SNoa Osherovich } 392f1b65df5SNoa Osherovich 393639bf441SAharon Landau static int translate_eth_ext_proto_oper(u32 eth_proto_oper, u16 *active_speed, 39408e8676fSAya Levin u8 *active_width) 39508e8676fSAya Levin { 39608e8676fSAya Levin switch (eth_proto_oper) { 39708e8676fSAya Levin case MLX5E_PROT_MASK(MLX5E_SGMII_100M): 39808e8676fSAya Levin case MLX5E_PROT_MASK(MLX5E_1000BASE_X_SGMII): 39908e8676fSAya Levin *active_width = IB_WIDTH_1X; 40008e8676fSAya Levin *active_speed = IB_SPEED_SDR; 40108e8676fSAya Levin break; 40208e8676fSAya Levin case MLX5E_PROT_MASK(MLX5E_5GBASE_R): 40308e8676fSAya Levin *active_width = IB_WIDTH_1X; 40408e8676fSAya Levin *active_speed = IB_SPEED_DDR; 40508e8676fSAya Levin break; 40608e8676fSAya Levin case MLX5E_PROT_MASK(MLX5E_10GBASE_XFI_XAUI_1): 40708e8676fSAya Levin *active_width = IB_WIDTH_1X; 40808e8676fSAya Levin *active_speed = IB_SPEED_QDR; 40908e8676fSAya Levin break; 41008e8676fSAya Levin case MLX5E_PROT_MASK(MLX5E_40GBASE_XLAUI_4_XLPPI_4): 41108e8676fSAya Levin *active_width = IB_WIDTH_4X; 41208e8676fSAya Levin *active_speed = IB_SPEED_QDR; 41308e8676fSAya Levin break; 41408e8676fSAya Levin case MLX5E_PROT_MASK(MLX5E_25GAUI_1_25GBASE_CR_KR): 41508e8676fSAya Levin *active_width = IB_WIDTH_1X; 41608e8676fSAya Levin *active_speed = IB_SPEED_EDR; 41708e8676fSAya Levin break; 41808e8676fSAya Levin case MLX5E_PROT_MASK(MLX5E_50GAUI_2_LAUI_2_50GBASE_CR2_KR2): 419cd272875SAya Levin *active_width = IB_WIDTH_2X; 420cd272875SAya Levin *active_speed = IB_SPEED_EDR; 421cd272875SAya Levin break; 42208e8676fSAya Levin case MLX5E_PROT_MASK(MLX5E_50GAUI_1_LAUI_1_50GBASE_CR_KR): 42308e8676fSAya Levin *active_width = IB_WIDTH_1X; 42408e8676fSAya Levin *active_speed = IB_SPEED_HDR; 42508e8676fSAya Levin break; 426cd272875SAya Levin case MLX5E_PROT_MASK(MLX5E_CAUI_4_100GBASE_CR4_KR4): 427cd272875SAya Levin *active_width = IB_WIDTH_4X; 428cd272875SAya Levin *active_speed = IB_SPEED_EDR; 429cd272875SAya Levin break; 43008e8676fSAya Levin case MLX5E_PROT_MASK(MLX5E_100GAUI_2_100GBASE_CR2_KR2): 43108e8676fSAya Levin *active_width = IB_WIDTH_2X; 43208e8676fSAya Levin *active_speed = IB_SPEED_HDR; 43308e8676fSAya Levin break; 434f946e45fSMeir Lichtinger case MLX5E_PROT_MASK(MLX5E_100GAUI_1_100GBASE_CR_KR): 435f946e45fSMeir Lichtinger *active_width = IB_WIDTH_1X; 436f946e45fSMeir Lichtinger *active_speed = IB_SPEED_NDR; 437f946e45fSMeir Lichtinger break; 43808e8676fSAya Levin case MLX5E_PROT_MASK(MLX5E_200GAUI_4_200GBASE_CR4_KR4): 43908e8676fSAya Levin *active_width = IB_WIDTH_4X; 44008e8676fSAya Levin *active_speed = IB_SPEED_HDR; 44108e8676fSAya Levin break; 442f946e45fSMeir Lichtinger case MLX5E_PROT_MASK(MLX5E_200GAUI_2_200GBASE_CR2_KR2): 443f946e45fSMeir Lichtinger *active_width = IB_WIDTH_2X; 444f946e45fSMeir Lichtinger *active_speed = IB_SPEED_NDR; 445f946e45fSMeir Lichtinger break; 446f946e45fSMeir Lichtinger case MLX5E_PROT_MASK(MLX5E_400GAUI_4_400GBASE_CR4_KR4): 447f946e45fSMeir Lichtinger *active_width = IB_WIDTH_4X; 448f946e45fSMeir Lichtinger *active_speed = IB_SPEED_NDR; 449f946e45fSMeir Lichtinger break; 45008e8676fSAya Levin default: 45108e8676fSAya Levin return -EINVAL; 45208e8676fSAya Levin } 45308e8676fSAya Levin 45408e8676fSAya Levin return 0; 45508e8676fSAya Levin } 45608e8676fSAya Levin 457639bf441SAharon Landau static int translate_eth_proto_oper(u32 eth_proto_oper, u16 *active_speed, 45808e8676fSAya Levin u8 *active_width, bool ext) 45908e8676fSAya Levin { 46008e8676fSAya Levin return ext ? 46108e8676fSAya Levin translate_eth_ext_proto_oper(eth_proto_oper, active_speed, 46208e8676fSAya Levin active_width) : 46308e8676fSAya Levin translate_eth_legacy_proto_oper(eth_proto_oper, active_speed, 46408e8676fSAya Levin active_width); 46508e8676fSAya Levin } 46608e8676fSAya Levin 4671fb7f897SMark Bloch static int mlx5_query_port_roce(struct ib_device *device, u32 port_num, 4683f89a643SAchiad Shochat struct ib_port_attr *props) 4693f89a643SAchiad Shochat { 4703f89a643SAchiad Shochat struct mlx5_ib_dev *dev = to_mdev(device); 471bc4e12ffSAya Levin u32 out[MLX5_ST_SZ_DW(ptys_reg)] = {0}; 472da005f9fSColin Ian King struct mlx5_core_dev *mdev; 47388621dfeSAviv Heller struct net_device *ndev, *upper; 4743f89a643SAchiad Shochat enum ib_mtu ndev_ib_mtu; 475b3cbd6f0SDaniel Jurgens bool put_mdev = true; 476f1b65df5SNoa Osherovich u32 eth_prot_oper; 4771fb7f897SMark Bloch u32 mdev_port_num; 47808e8676fSAya Levin bool ext; 479095b0927SIlan Tayari int err; 4803f89a643SAchiad Shochat 481b3cbd6f0SDaniel Jurgens mdev = mlx5_ib_get_native_port_mdev(dev, port_num, &mdev_port_num); 482b3cbd6f0SDaniel Jurgens if (!mdev) { 483b3cbd6f0SDaniel Jurgens /* This means the port isn't affiliated yet. Get the 484b3cbd6f0SDaniel Jurgens * info for the master port instead. 485b3cbd6f0SDaniel Jurgens */ 486b3cbd6f0SDaniel Jurgens put_mdev = false; 487b3cbd6f0SDaniel Jurgens mdev = dev->mdev; 488b3cbd6f0SDaniel Jurgens mdev_port_num = 1; 489b3cbd6f0SDaniel Jurgens port_num = 1; 490b3cbd6f0SDaniel Jurgens } 491b3cbd6f0SDaniel Jurgens 492f1b65df5SNoa Osherovich /* Possible bad flows are checked before filling out props so in case 493f1b65df5SNoa Osherovich * of an error it will still be zeroed out. 49426628e2dSMark Bloch * Use native port in case of reps 49550f22fd8SNoa Osherovich */ 49626628e2dSMark Bloch if (dev->is_rep) 49726628e2dSMark Bloch err = mlx5_query_port_ptys(mdev, out, sizeof(out), MLX5_PTYS_EN, 49826628e2dSMark Bloch 1); 49926628e2dSMark Bloch else 500bc4e12ffSAya Levin err = mlx5_query_port_ptys(mdev, out, sizeof(out), MLX5_PTYS_EN, 501b3cbd6f0SDaniel Jurgens mdev_port_num); 502095b0927SIlan Tayari if (err) 503b3cbd6f0SDaniel Jurgens goto out; 504530c8632SAya Levin ext = !!MLX5_GET_ETH_PROTO(ptys_reg, out, true, eth_proto_capability); 50508e8676fSAya Levin eth_prot_oper = MLX5_GET_ETH_PROTO(ptys_reg, out, ext, eth_proto_oper); 506f1b65df5SNoa Osherovich 5077672ed33SHonggang Li props->active_width = IB_WIDTH_4X; 5087672ed33SHonggang Li props->active_speed = IB_SPEED_QDR; 5097672ed33SHonggang Li 510f1b65df5SNoa Osherovich translate_eth_proto_oper(eth_prot_oper, &props->active_speed, 51108e8676fSAya Levin &props->active_width, ext); 5123f89a643SAchiad Shochat 5137852546fSMaor Gottlieb if (!dev->is_rep && dev->mdev->roce.roce_en) { 5147a58779eSParav Pandit u16 qkey_viol_cntr; 5157a58779eSParav Pandit 5163f89a643SAchiad Shochat props->port_cap_flags |= IB_PORT_CM_SUP; 5172f944c0fSJason Gunthorpe props->ip_gids = true; 5183f89a643SAchiad Shochat props->gid_tbl_len = MLX5_CAP_ROCE(dev->mdev, 5193f89a643SAchiad Shochat roce_address_table_size); 5207a58779eSParav Pandit mlx5_query_nic_vport_qkey_viol_cntr(mdev, &qkey_viol_cntr); 5217a58779eSParav Pandit props->qkey_viol_cntr = qkey_viol_cntr; 5227a58779eSParav Pandit } 5233f89a643SAchiad Shochat props->max_mtu = IB_MTU_4096; 5243f89a643SAchiad Shochat props->max_msg_sz = 1 << MLX5_CAP_GEN(dev->mdev, log_max_msg); 5253f89a643SAchiad Shochat props->pkey_tbl_len = 1; 5263f89a643SAchiad Shochat props->state = IB_PORT_DOWN; 52772a7720fSKamal Heib props->phys_state = IB_PORT_PHYS_STATE_DISABLED; 5283f89a643SAchiad Shochat 529b3cbd6f0SDaniel Jurgens /* If this is a stub query for an unaffiliated port stop here */ 530b3cbd6f0SDaniel Jurgens if (!put_mdev) 531b3cbd6f0SDaniel Jurgens goto out; 532b3cbd6f0SDaniel Jurgens 5333f89a643SAchiad Shochat ndev = mlx5_ib_get_netdev(device, port_num); 5343f89a643SAchiad Shochat if (!ndev) 535b3cbd6f0SDaniel Jurgens goto out; 5363f89a643SAchiad Shochat 5377c34ec19SAviv Heller if (dev->lag_active) { 53888621dfeSAviv Heller rcu_read_lock(); 53988621dfeSAviv Heller upper = netdev_master_upper_dev_get_rcu(ndev); 54088621dfeSAviv Heller if (upper) { 54188621dfeSAviv Heller dev_put(ndev); 54288621dfeSAviv Heller ndev = upper; 54388621dfeSAviv Heller dev_hold(ndev); 54488621dfeSAviv Heller } 54588621dfeSAviv Heller rcu_read_unlock(); 54688621dfeSAviv Heller } 54788621dfeSAviv Heller 5483f89a643SAchiad Shochat if (netif_running(ndev) && netif_carrier_ok(ndev)) { 5493f89a643SAchiad Shochat props->state = IB_PORT_ACTIVE; 55072a7720fSKamal Heib props->phys_state = IB_PORT_PHYS_STATE_LINK_UP; 5513f89a643SAchiad Shochat } 5523f89a643SAchiad Shochat 5533f89a643SAchiad Shochat ndev_ib_mtu = iboe_get_mtu(ndev->mtu); 5543f89a643SAchiad Shochat 5553f89a643SAchiad Shochat dev_put(ndev); 5563f89a643SAchiad Shochat 5573f89a643SAchiad Shochat props->active_mtu = min(props->max_mtu, ndev_ib_mtu); 558b3cbd6f0SDaniel Jurgens out: 559b3cbd6f0SDaniel Jurgens if (put_mdev) 560b3cbd6f0SDaniel Jurgens mlx5_ib_put_native_port_mdev(dev, port_num); 561b3cbd6f0SDaniel Jurgens return err; 5623f89a643SAchiad Shochat } 5633f89a643SAchiad Shochat 5641fb7f897SMark Bloch static int set_roce_addr(struct mlx5_ib_dev *dev, u32 port_num, 565095b0927SIlan Tayari unsigned int index, const union ib_gid *gid, 566095b0927SIlan Tayari const struct ib_gid_attr *attr) 5673cca2606SAchiad Shochat { 568dedbc2d3SParav Pandit enum ib_gid_type gid_type; 569a70c0739SParav Pandit u16 vlan_id = 0xffff; 570095b0927SIlan Tayari u8 roce_version = 0; 571095b0927SIlan Tayari u8 roce_l3_type = 0; 572095b0927SIlan Tayari u8 mac[ETH_ALEN]; 573a70c0739SParav Pandit int ret; 5743cca2606SAchiad Shochat 575095b0927SIlan Tayari gid_type = attr->gid_type; 576dedbc2d3SParav Pandit if (gid) { 577a70c0739SParav Pandit ret = rdma_read_gid_l2_fields(attr, &vlan_id, &mac[0]); 578a70c0739SParav Pandit if (ret) 579a70c0739SParav Pandit return ret; 5803cca2606SAchiad Shochat } 5813cca2606SAchiad Shochat 582095b0927SIlan Tayari switch (gid_type) { 5831c15b4f2SAvihai Horon case IB_GID_TYPE_ROCE: 584095b0927SIlan Tayari roce_version = MLX5_ROCE_VERSION_1; 5853cca2606SAchiad Shochat break; 5863cca2606SAchiad Shochat case IB_GID_TYPE_ROCE_UDP_ENCAP: 587095b0927SIlan Tayari roce_version = MLX5_ROCE_VERSION_2; 588dedbc2d3SParav Pandit if (gid && ipv6_addr_v4mapped((void *)gid)) 589095b0927SIlan Tayari roce_l3_type = MLX5_ROCE_L3_TYPE_IPV4; 590095b0927SIlan Tayari else 591095b0927SIlan Tayari roce_l3_type = MLX5_ROCE_L3_TYPE_IPV6; 5923cca2606SAchiad Shochat break; 5933cca2606SAchiad Shochat 5943cca2606SAchiad Shochat default: 595095b0927SIlan Tayari mlx5_ib_warn(dev, "Unexpected GID type %u\n", gid_type); 5963cca2606SAchiad Shochat } 5973cca2606SAchiad Shochat 598095b0927SIlan Tayari return mlx5_core_roce_gid_set(dev->mdev, index, roce_version, 599cf34e1feSParav Pandit roce_l3_type, gid->raw, mac, 600a70c0739SParav Pandit vlan_id < VLAN_CFI_MASK, vlan_id, 601cf34e1feSParav Pandit port_num); 6023cca2606SAchiad Shochat } 6033cca2606SAchiad Shochat 604f4df9a7cSParav Pandit static int mlx5_ib_add_gid(const struct ib_gid_attr *attr, 6053cca2606SAchiad Shochat __always_unused void **context) 6063cca2606SAchiad Shochat { 607414448d2SParav Pandit return set_roce_addr(to_mdev(attr->device), attr->port_num, 608f4df9a7cSParav Pandit attr->index, &attr->gid, attr); 6093cca2606SAchiad Shochat } 6103cca2606SAchiad Shochat 611414448d2SParav Pandit static int mlx5_ib_del_gid(const struct ib_gid_attr *attr, 612414448d2SParav Pandit __always_unused void **context) 6133cca2606SAchiad Shochat { 614414448d2SParav Pandit return set_roce_addr(to_mdev(attr->device), attr->port_num, 615dedbc2d3SParav Pandit attr->index, NULL, attr); 6163cca2606SAchiad Shochat } 6173cca2606SAchiad Shochat 6185ac55dfcSMark Zhang __be16 mlx5_get_roce_udp_sport_min(const struct mlx5_ib_dev *dev, 61947ec3866SParav Pandit const struct ib_gid_attr *attr) 6202811ba51SAchiad Shochat { 62147ec3866SParav Pandit if (attr->gid_type != IB_GID_TYPE_ROCE_UDP_ENCAP) 6222811ba51SAchiad Shochat return 0; 6232811ba51SAchiad Shochat 6242811ba51SAchiad Shochat return cpu_to_be16(MLX5_CAP_ROCE(dev->mdev, r_roce_min_src_udp_port)); 6252811ba51SAchiad Shochat } 6262811ba51SAchiad Shochat 6271b5daf11SMajd Dibbiny static int mlx5_use_mad_ifc(struct mlx5_ib_dev *dev) 6281b5daf11SMajd Dibbiny { 6297fae6655SNoa Osherovich if (MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_IB) 630d603c809SEli Cohen return !MLX5_CAP_GEN(dev->mdev, ib_virt); 6317fae6655SNoa Osherovich return 0; 6321b5daf11SMajd Dibbiny } 6331b5daf11SMajd Dibbiny 6341b5daf11SMajd Dibbiny enum { 6351b5daf11SMajd Dibbiny MLX5_VPORT_ACCESS_METHOD_MAD, 6361b5daf11SMajd Dibbiny MLX5_VPORT_ACCESS_METHOD_HCA, 6371b5daf11SMajd Dibbiny MLX5_VPORT_ACCESS_METHOD_NIC, 6381b5daf11SMajd Dibbiny }; 6391b5daf11SMajd Dibbiny 6401b5daf11SMajd Dibbiny static int mlx5_get_vport_access_method(struct ib_device *ibdev) 6411b5daf11SMajd Dibbiny { 6421b5daf11SMajd Dibbiny if (mlx5_use_mad_ifc(to_mdev(ibdev))) 6431b5daf11SMajd Dibbiny return MLX5_VPORT_ACCESS_METHOD_MAD; 6441b5daf11SMajd Dibbiny 645ebd61f68SAchiad Shochat if (mlx5_ib_port_link_layer(ibdev, 1) == 6461b5daf11SMajd Dibbiny IB_LINK_LAYER_ETHERNET) 6471b5daf11SMajd Dibbiny return MLX5_VPORT_ACCESS_METHOD_NIC; 6481b5daf11SMajd Dibbiny 6491b5daf11SMajd Dibbiny return MLX5_VPORT_ACCESS_METHOD_HCA; 6501b5daf11SMajd Dibbiny } 6511b5daf11SMajd Dibbiny 652da7525d2SEran Ben Elisha static void get_atomic_caps(struct mlx5_ib_dev *dev, 653776a3906SMoni Shoua u8 atomic_size_qp, 654da7525d2SEran Ben Elisha struct ib_device_attr *props) 655da7525d2SEran Ben Elisha { 656da7525d2SEran Ben Elisha u8 tmp; 657da7525d2SEran Ben Elisha u8 atomic_operations = MLX5_CAP_ATOMIC(dev->mdev, atomic_operations); 658da7525d2SEran Ben Elisha u8 atomic_req_8B_endianness_mode = 659bd10838aSOr Gerlitz MLX5_CAP_ATOMIC(dev->mdev, atomic_req_8B_endianness_mode); 660da7525d2SEran Ben Elisha 661da7525d2SEran Ben Elisha /* Check if HW supports 8 bytes standard atomic operations and capable 662da7525d2SEran Ben Elisha * of host endianness respond 663da7525d2SEran Ben Elisha */ 664da7525d2SEran Ben Elisha tmp = MLX5_ATOMIC_OPS_CMP_SWAP | MLX5_ATOMIC_OPS_FETCH_ADD; 665da7525d2SEran Ben Elisha if (((atomic_operations & tmp) == tmp) && 666da7525d2SEran Ben Elisha (atomic_size_qp & MLX5_ATOMIC_SIZE_QP_8BYTES) && 667da7525d2SEran Ben Elisha (atomic_req_8B_endianness_mode)) { 668da7525d2SEran Ben Elisha props->atomic_cap = IB_ATOMIC_HCA; 669da7525d2SEran Ben Elisha } else { 670da7525d2SEran Ben Elisha props->atomic_cap = IB_ATOMIC_NONE; 671da7525d2SEran Ben Elisha } 672da7525d2SEran Ben Elisha } 673da7525d2SEran Ben Elisha 674776a3906SMoni Shoua static void get_atomic_caps_qp(struct mlx5_ib_dev *dev, 675776a3906SMoni Shoua struct ib_device_attr *props) 676776a3906SMoni Shoua { 677776a3906SMoni Shoua u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_qp); 678776a3906SMoni Shoua 679776a3906SMoni Shoua get_atomic_caps(dev, atomic_size_qp, props); 680776a3906SMoni Shoua } 681776a3906SMoni Shoua 6821b5daf11SMajd Dibbiny static int mlx5_query_system_image_guid(struct ib_device *ibdev, 6831b5daf11SMajd Dibbiny __be64 *sys_image_guid) 6841b5daf11SMajd Dibbiny { 6851b5daf11SMajd Dibbiny struct mlx5_ib_dev *dev = to_mdev(ibdev); 6861b5daf11SMajd Dibbiny struct mlx5_core_dev *mdev = dev->mdev; 6871b5daf11SMajd Dibbiny u64 tmp; 6881b5daf11SMajd Dibbiny int err; 6891b5daf11SMajd Dibbiny 6901b5daf11SMajd Dibbiny switch (mlx5_get_vport_access_method(ibdev)) { 6911b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_MAD: 6921b5daf11SMajd Dibbiny return mlx5_query_mad_ifc_system_image_guid(ibdev, 6931b5daf11SMajd Dibbiny sys_image_guid); 6941b5daf11SMajd Dibbiny 6951b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_HCA: 6961b5daf11SMajd Dibbiny err = mlx5_query_hca_vport_system_image_guid(mdev, &tmp); 6973f89a643SAchiad Shochat break; 6983f89a643SAchiad Shochat 6993f89a643SAchiad Shochat case MLX5_VPORT_ACCESS_METHOD_NIC: 7003f89a643SAchiad Shochat err = mlx5_query_nic_vport_system_image_guid(mdev, &tmp); 7013f89a643SAchiad Shochat break; 7021b5daf11SMajd Dibbiny 7031b5daf11SMajd Dibbiny default: 7041b5daf11SMajd Dibbiny return -EINVAL; 7051b5daf11SMajd Dibbiny } 7063f89a643SAchiad Shochat 7073f89a643SAchiad Shochat if (!err) 7083f89a643SAchiad Shochat *sys_image_guid = cpu_to_be64(tmp); 7093f89a643SAchiad Shochat 7103f89a643SAchiad Shochat return err; 7113f89a643SAchiad Shochat 7121b5daf11SMajd Dibbiny } 7131b5daf11SMajd Dibbiny 7141b5daf11SMajd Dibbiny static int mlx5_query_max_pkeys(struct ib_device *ibdev, 7151b5daf11SMajd Dibbiny u16 *max_pkeys) 7161b5daf11SMajd Dibbiny { 7171b5daf11SMajd Dibbiny struct mlx5_ib_dev *dev = to_mdev(ibdev); 7181b5daf11SMajd Dibbiny struct mlx5_core_dev *mdev = dev->mdev; 7191b5daf11SMajd Dibbiny 7201b5daf11SMajd Dibbiny switch (mlx5_get_vport_access_method(ibdev)) { 7211b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_MAD: 7221b5daf11SMajd Dibbiny return mlx5_query_mad_ifc_max_pkeys(ibdev, max_pkeys); 7231b5daf11SMajd Dibbiny 7241b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_HCA: 7251b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_NIC: 7261b5daf11SMajd Dibbiny *max_pkeys = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, 7271b5daf11SMajd Dibbiny pkey_table_size)); 7281b5daf11SMajd Dibbiny return 0; 7291b5daf11SMajd Dibbiny 7301b5daf11SMajd Dibbiny default: 7311b5daf11SMajd Dibbiny return -EINVAL; 7321b5daf11SMajd Dibbiny } 7331b5daf11SMajd Dibbiny } 7341b5daf11SMajd Dibbiny 7351b5daf11SMajd Dibbiny static int mlx5_query_vendor_id(struct ib_device *ibdev, 7361b5daf11SMajd Dibbiny u32 *vendor_id) 7371b5daf11SMajd Dibbiny { 7381b5daf11SMajd Dibbiny struct mlx5_ib_dev *dev = to_mdev(ibdev); 7391b5daf11SMajd Dibbiny 7401b5daf11SMajd Dibbiny switch (mlx5_get_vport_access_method(ibdev)) { 7411b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_MAD: 7421b5daf11SMajd Dibbiny return mlx5_query_mad_ifc_vendor_id(ibdev, vendor_id); 7431b5daf11SMajd Dibbiny 7441b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_HCA: 7451b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_NIC: 7461b5daf11SMajd Dibbiny return mlx5_core_query_vendor_id(dev->mdev, vendor_id); 7471b5daf11SMajd Dibbiny 7481b5daf11SMajd Dibbiny default: 7491b5daf11SMajd Dibbiny return -EINVAL; 7501b5daf11SMajd Dibbiny } 7511b5daf11SMajd Dibbiny } 7521b5daf11SMajd Dibbiny 7531b5daf11SMajd Dibbiny static int mlx5_query_node_guid(struct mlx5_ib_dev *dev, 7541b5daf11SMajd Dibbiny __be64 *node_guid) 7551b5daf11SMajd Dibbiny { 7561b5daf11SMajd Dibbiny u64 tmp; 7571b5daf11SMajd Dibbiny int err; 7581b5daf11SMajd Dibbiny 7591b5daf11SMajd Dibbiny switch (mlx5_get_vport_access_method(&dev->ib_dev)) { 7601b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_MAD: 7611b5daf11SMajd Dibbiny return mlx5_query_mad_ifc_node_guid(dev, node_guid); 7621b5daf11SMajd Dibbiny 7631b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_HCA: 7641b5daf11SMajd Dibbiny err = mlx5_query_hca_vport_node_guid(dev->mdev, &tmp); 7653f89a643SAchiad Shochat break; 7663f89a643SAchiad Shochat 7673f89a643SAchiad Shochat case MLX5_VPORT_ACCESS_METHOD_NIC: 7683f89a643SAchiad Shochat err = mlx5_query_nic_vport_node_guid(dev->mdev, &tmp); 7693f89a643SAchiad Shochat break; 7701b5daf11SMajd Dibbiny 7711b5daf11SMajd Dibbiny default: 7721b5daf11SMajd Dibbiny return -EINVAL; 7731b5daf11SMajd Dibbiny } 7743f89a643SAchiad Shochat 7753f89a643SAchiad Shochat if (!err) 7763f89a643SAchiad Shochat *node_guid = cpu_to_be64(tmp); 7773f89a643SAchiad Shochat 7783f89a643SAchiad Shochat return err; 7791b5daf11SMajd Dibbiny } 7801b5daf11SMajd Dibbiny 7811b5daf11SMajd Dibbiny struct mlx5_reg_node_desc { 782bd99fdeaSYuval Shaia u8 desc[IB_DEVICE_NODE_DESC_MAX]; 7831b5daf11SMajd Dibbiny }; 7841b5daf11SMajd Dibbiny 7851b5daf11SMajd Dibbiny static int mlx5_query_node_desc(struct mlx5_ib_dev *dev, char *node_desc) 7861b5daf11SMajd Dibbiny { 7871b5daf11SMajd Dibbiny struct mlx5_reg_node_desc in; 7881b5daf11SMajd Dibbiny 7891b5daf11SMajd Dibbiny if (mlx5_use_mad_ifc(dev)) 7901b5daf11SMajd Dibbiny return mlx5_query_mad_ifc_node_desc(dev, node_desc); 7911b5daf11SMajd Dibbiny 7921b5daf11SMajd Dibbiny memset(&in, 0, sizeof(in)); 7931b5daf11SMajd Dibbiny 7941b5daf11SMajd Dibbiny return mlx5_core_access_reg(dev->mdev, &in, sizeof(in), node_desc, 7951b5daf11SMajd Dibbiny sizeof(struct mlx5_reg_node_desc), 7961b5daf11SMajd Dibbiny MLX5_REG_NODE_DESC, 0, 0); 7971b5daf11SMajd Dibbiny } 7981b5daf11SMajd Dibbiny 799e126ba97SEli Cohen static int mlx5_ib_query_device(struct ib_device *ibdev, 8002528e33eSMatan Barak struct ib_device_attr *props, 8012528e33eSMatan Barak struct ib_udata *uhw) 802e126ba97SEli Cohen { 80348357091SLeon Romanovsky size_t uhw_outlen = (uhw) ? uhw->outlen : 0; 804e126ba97SEli Cohen struct mlx5_ib_dev *dev = to_mdev(ibdev); 805938fe83cSSaeed Mahameed struct mlx5_core_dev *mdev = dev->mdev; 806e126ba97SEli Cohen int err = -ENOMEM; 807288c01b7SEli Cohen int max_sq_desc; 808e126ba97SEli Cohen int max_rq_sg; 809e126ba97SEli Cohen int max_sq_sg; 810e0238a6aSSagi Grimberg u64 min_page_size = 1ull << MLX5_CAP_GEN(mdev, log_pg_sz); 81185c7c014SDaniel Jurgens bool raw_support = !mlx5_core_mp_enabled(mdev); 812402ca536SBodong Wang struct mlx5_ib_query_device_resp resp = {}; 813402ca536SBodong Wang size_t resp_len; 814402ca536SBodong Wang u64 max_tso; 815e126ba97SEli Cohen 816402ca536SBodong Wang resp_len = sizeof(resp.comp_mask) + sizeof(resp.response_length); 81748357091SLeon Romanovsky if (uhw_outlen && uhw_outlen < resp_len) 818402ca536SBodong Wang return -EINVAL; 8196f26b2acSErez Alfasi 820402ca536SBodong Wang resp.response_length = resp_len; 821402ca536SBodong Wang 82248357091SLeon Romanovsky if (uhw && uhw->inlen && !ib_is_udata_cleared(uhw, 0, uhw->inlen)) 8232528e33eSMatan Barak return -EINVAL; 8242528e33eSMatan Barak 825e126ba97SEli Cohen memset(props, 0, sizeof(*props)); 8261b5daf11SMajd Dibbiny err = mlx5_query_system_image_guid(ibdev, 8271b5daf11SMajd Dibbiny &props->sys_image_guid); 8281b5daf11SMajd Dibbiny if (err) 8291b5daf11SMajd Dibbiny return err; 8301b5daf11SMajd Dibbiny 8312019d70eSParav Pandit props->max_pkeys = dev->pkey_table_len; 8321b5daf11SMajd Dibbiny 8331b5daf11SMajd Dibbiny err = mlx5_query_vendor_id(ibdev, &props->vendor_id); 8341b5daf11SMajd Dibbiny if (err) 8351b5daf11SMajd Dibbiny return err; 836e126ba97SEli Cohen 8379603b61dSJack Morgenstein props->fw_ver = ((u64)fw_rev_maj(dev->mdev) << 32) | 8389603b61dSJack Morgenstein (fw_rev_min(dev->mdev) << 16) | 8399603b61dSJack Morgenstein fw_rev_sub(dev->mdev); 840e126ba97SEli Cohen props->device_cap_flags = IB_DEVICE_CHANGE_PHY_PORT | 841e126ba97SEli Cohen IB_DEVICE_PORT_ACTIVE_EVENT | 842e126ba97SEli Cohen IB_DEVICE_SYS_IMAGE_GUID | 8431a4c3a3dSEli Cohen IB_DEVICE_RC_RNR_NAK_GEN; 844938fe83cSSaeed Mahameed 845938fe83cSSaeed Mahameed if (MLX5_CAP_GEN(mdev, pkv)) 846e126ba97SEli Cohen props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR; 847938fe83cSSaeed Mahameed if (MLX5_CAP_GEN(mdev, qkv)) 848e126ba97SEli Cohen props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR; 849938fe83cSSaeed Mahameed if (MLX5_CAP_GEN(mdev, apm)) 850e126ba97SEli Cohen props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG; 851938fe83cSSaeed Mahameed if (MLX5_CAP_GEN(mdev, xrc)) 852e126ba97SEli Cohen props->device_cap_flags |= IB_DEVICE_XRC; 853d2370e0aSMatan Barak if (MLX5_CAP_GEN(mdev, imaicl)) { 854d2370e0aSMatan Barak props->device_cap_flags |= IB_DEVICE_MEM_WINDOW | 855d2370e0aSMatan Barak IB_DEVICE_MEM_WINDOW_TYPE_2B; 856d2370e0aSMatan Barak props->max_mw = 1 << MLX5_CAP_GEN(mdev, log_max_mkey); 857b005d316SSagi Grimberg /* We support 'Gappy' memory registration too */ 858e945c653SJason Gunthorpe props->kernel_cap_flags |= IBK_SG_GAPS_REG; 859d2370e0aSMatan Barak } 8600ec52f01SJason Gunthorpe /* IB_WR_REG_MR always requires changing the entity size with UMR */ 8610ec52f01SJason Gunthorpe if (!MLX5_CAP_GEN(dev->mdev, umr_modify_entity_size_disabled)) 862e126ba97SEli Cohen props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS; 863938fe83cSSaeed Mahameed if (MLX5_CAP_GEN(mdev, sho)) { 864e945c653SJason Gunthorpe props->kernel_cap_flags |= IBK_INTEGRITY_HANDOVER; 8652dea9094SSagi Grimberg /* At this stage no support for signature handover */ 8662dea9094SSagi Grimberg props->sig_prot_cap = IB_PROT_T10DIF_TYPE_1 | 8672dea9094SSagi Grimberg IB_PROT_T10DIF_TYPE_2 | 8682dea9094SSagi Grimberg IB_PROT_T10DIF_TYPE_3; 8692dea9094SSagi Grimberg props->sig_guard_cap = IB_GUARD_T10DIF_CRC | 8702dea9094SSagi Grimberg IB_GUARD_T10DIF_CSUM; 8712dea9094SSagi Grimberg } 872938fe83cSSaeed Mahameed if (MLX5_CAP_GEN(mdev, block_lb_mc)) 873e945c653SJason Gunthorpe props->kernel_cap_flags |= IBK_BLOCK_MULTICAST_LOOPBACK; 874e126ba97SEli Cohen 87585c7c014SDaniel Jurgens if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) && raw_support) { 876e8161334SNoa Osherovich if (MLX5_CAP_ETH(mdev, csum_cap)) { 877e8161334SNoa Osherovich /* Legacy bit to support old userspace libraries */ 87888115fe7SBodong Wang props->device_cap_flags |= IB_DEVICE_RAW_IP_CSUM; 879e8161334SNoa Osherovich props->raw_packet_caps |= IB_RAW_PACKET_CAP_IP_CSUM; 880e8161334SNoa Osherovich } 881e8161334SNoa Osherovich 882e8161334SNoa Osherovich if (MLX5_CAP_ETH(dev->mdev, vlan_cap)) 883e8161334SNoa Osherovich props->raw_packet_caps |= 884e8161334SNoa Osherovich IB_RAW_PACKET_CAP_CVLAN_STRIPPING; 88588115fe7SBodong Wang 886a762d460SLeon Romanovsky if (offsetofend(typeof(resp), tso_caps) <= uhw_outlen) { 887402ca536SBodong Wang max_tso = MLX5_CAP_ETH(mdev, max_lso_cap); 888402ca536SBodong Wang if (max_tso) { 889402ca536SBodong Wang resp.tso_caps.max_tso = 1 << max_tso; 890402ca536SBodong Wang resp.tso_caps.supported_qpts |= 891402ca536SBodong Wang 1 << IB_QPT_RAW_PACKET; 892402ca536SBodong Wang resp.response_length += sizeof(resp.tso_caps); 893402ca536SBodong Wang } 894402ca536SBodong Wang } 89531f69a82SYishai Hadas 896a762d460SLeon Romanovsky if (offsetofend(typeof(resp), rss_caps) <= uhw_outlen) { 89731f69a82SYishai Hadas resp.rss_caps.rx_hash_function = 89831f69a82SYishai Hadas MLX5_RX_HASH_FUNC_TOEPLITZ; 89931f69a82SYishai Hadas resp.rss_caps.rx_hash_fields_mask = 90031f69a82SYishai Hadas MLX5_RX_HASH_SRC_IPV4 | 90131f69a82SYishai Hadas MLX5_RX_HASH_DST_IPV4 | 90231f69a82SYishai Hadas MLX5_RX_HASH_SRC_IPV6 | 90331f69a82SYishai Hadas MLX5_RX_HASH_DST_IPV6 | 90431f69a82SYishai Hadas MLX5_RX_HASH_SRC_PORT_TCP | 90531f69a82SYishai Hadas MLX5_RX_HASH_DST_PORT_TCP | 90631f69a82SYishai Hadas MLX5_RX_HASH_SRC_PORT_UDP | 9074e2b53a5SMaor Gottlieb MLX5_RX_HASH_DST_PORT_UDP | 9084e2b53a5SMaor Gottlieb MLX5_RX_HASH_INNER; 90931f69a82SYishai Hadas resp.response_length += sizeof(resp.rss_caps); 91031f69a82SYishai Hadas } 91131f69a82SYishai Hadas } else { 912a762d460SLeon Romanovsky if (offsetofend(typeof(resp), tso_caps) <= uhw_outlen) 91331f69a82SYishai Hadas resp.response_length += sizeof(resp.tso_caps); 914a762d460SLeon Romanovsky if (offsetofend(typeof(resp), rss_caps) <= uhw_outlen) 91531f69a82SYishai Hadas resp.response_length += sizeof(resp.rss_caps); 916402ca536SBodong Wang } 917402ca536SBodong Wang 918f0313965SErez Shitrit if (MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) { 919f0313965SErez Shitrit props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM; 920e945c653SJason Gunthorpe props->kernel_cap_flags |= IBK_UD_TSO; 921f0313965SErez Shitrit } 922f0313965SErez Shitrit 92303404e8aSMaor Gottlieb if (MLX5_CAP_GEN(dev->mdev, rq_delay_drop) && 92485c7c014SDaniel Jurgens MLX5_CAP_GEN(dev->mdev, general_notification_event) && 92585c7c014SDaniel Jurgens raw_support) 92603404e8aSMaor Gottlieb props->raw_packet_caps |= IB_RAW_PACKET_CAP_DELAY_DROP; 92703404e8aSMaor Gottlieb 9281d54f890SYishai Hadas if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads) && 9291d54f890SYishai Hadas MLX5_CAP_IPOIB_ENHANCED(mdev, csum_cap)) 9301d54f890SYishai Hadas props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM; 9311d54f890SYishai Hadas 932cff5a0f3SMajd Dibbiny if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) && 93385c7c014SDaniel Jurgens MLX5_CAP_ETH(dev->mdev, scatter_fcs) && 93485c7c014SDaniel Jurgens raw_support) { 935e8161334SNoa Osherovich /* Legacy bit to support old userspace libraries */ 936cff5a0f3SMajd Dibbiny props->device_cap_flags |= IB_DEVICE_RAW_SCATTER_FCS; 937e8161334SNoa Osherovich props->raw_packet_caps |= IB_RAW_PACKET_CAP_SCATTER_FCS; 938e8161334SNoa Osherovich } 939cff5a0f3SMajd Dibbiny 94024da0016SAriel Levkovich if (MLX5_CAP_DEV_MEM(mdev, memic)) { 94124da0016SAriel Levkovich props->max_dm_size = 94224da0016SAriel Levkovich MLX5_CAP_DEV_MEM(mdev, max_memic_size); 94324da0016SAriel Levkovich } 94424da0016SAriel Levkovich 945da6d6ba3SMaor Gottlieb if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS)) 946da6d6ba3SMaor Gottlieb props->device_cap_flags |= IB_DEVICE_MANAGED_FLOW_STEERING; 947da6d6ba3SMaor Gottlieb 948b1383aa6SNoa Osherovich if (MLX5_CAP_GEN(mdev, end_pad)) 949b1383aa6SNoa Osherovich props->device_cap_flags |= IB_DEVICE_PCI_WRITE_END_PADDING; 950b1383aa6SNoa Osherovich 9511b5daf11SMajd Dibbiny props->vendor_part_id = mdev->pdev->device; 9521b5daf11SMajd Dibbiny props->hw_ver = mdev->pdev->revision; 953e126ba97SEli Cohen 954e126ba97SEli Cohen props->max_mr_size = ~0ull; 955e0238a6aSSagi Grimberg props->page_size_cap = ~(min_page_size - 1); 956938fe83cSSaeed Mahameed props->max_qp = 1 << MLX5_CAP_GEN(mdev, log_max_qp); 957938fe83cSSaeed Mahameed props->max_qp_wr = 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz); 958938fe83cSSaeed Mahameed max_rq_sg = MLX5_CAP_GEN(mdev, max_wqe_sz_rq) / 959938fe83cSSaeed Mahameed sizeof(struct mlx5_wqe_data_seg); 960288c01b7SEli Cohen max_sq_desc = min_t(int, MLX5_CAP_GEN(mdev, max_wqe_sz_sq), 512); 961288c01b7SEli Cohen max_sq_sg = (max_sq_desc - sizeof(struct mlx5_wqe_ctrl_seg) - 962288c01b7SEli Cohen sizeof(struct mlx5_wqe_raddr_seg)) / 963e126ba97SEli Cohen sizeof(struct mlx5_wqe_data_seg); 96433023fb8SSteve Wise props->max_send_sge = max_sq_sg; 96533023fb8SSteve Wise props->max_recv_sge = max_rq_sg; 966986ef95eSSagi Grimberg props->max_sge_rd = MLX5_MAX_SGE_RD; 967938fe83cSSaeed Mahameed props->max_cq = 1 << MLX5_CAP_GEN(mdev, log_max_cq); 9689f177686SLeon Romanovsky props->max_cqe = (1 << MLX5_CAP_GEN(mdev, log_max_cq_sz)) - 1; 969938fe83cSSaeed Mahameed props->max_mr = 1 << MLX5_CAP_GEN(mdev, log_max_mkey); 970938fe83cSSaeed Mahameed props->max_pd = 1 << MLX5_CAP_GEN(mdev, log_max_pd); 971938fe83cSSaeed Mahameed props->max_qp_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_req_qp); 972938fe83cSSaeed Mahameed props->max_qp_init_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_res_qp); 973938fe83cSSaeed Mahameed props->max_srq = 1 << MLX5_CAP_GEN(mdev, log_max_srq); 974938fe83cSSaeed Mahameed props->max_srq_wr = (1 << MLX5_CAP_GEN(mdev, log_max_srq_sz)) - 1; 975938fe83cSSaeed Mahameed props->local_ca_ack_delay = MLX5_CAP_GEN(mdev, local_ca_ack_delay); 976e126ba97SEli Cohen props->max_res_rd_atom = props->max_qp_rd_atom * props->max_qp; 977e126ba97SEli Cohen props->max_srq_sge = max_rq_sg - 1; 978911f4331SSagi Grimberg props->max_fast_reg_page_list_len = 979911f4331SSagi Grimberg 1 << MLX5_CAP_GEN(mdev, log_max_klm_list_size); 98062e3c379SMax Gurtovoy props->max_pi_fast_reg_page_list_len = 98162e3c379SMax Gurtovoy props->max_fast_reg_page_list_len / 2; 98236609056SYamin Friedman props->max_sgl_rd = 98336609056SYamin Friedman MLX5_CAP_GEN(mdev, max_sgl_for_optimized_performance); 984776a3906SMoni Shoua get_atomic_caps_qp(dev, props); 98581bea28fSEli Cohen props->masked_atomic_cap = IB_ATOMIC_NONE; 986938fe83cSSaeed Mahameed props->max_mcast_grp = 1 << MLX5_CAP_GEN(mdev, log_max_mcg); 987938fe83cSSaeed Mahameed props->max_mcast_qp_attach = MLX5_CAP_GEN(mdev, max_qp_mcg); 988e126ba97SEli Cohen props->max_total_mcast_qp_attach = props->max_mcast_qp_attach * 989e126ba97SEli Cohen props->max_mcast_grp; 99086695a65SMaor Gottlieb props->max_ah = INT_MAX; 9917c60bcbbSMatan Barak props->hca_core_clock = MLX5_CAP_GEN(mdev, device_frequency_khz); 9927c60bcbbSMatan Barak props->timestamp_mask = 0x7FFFFFFFFFFFFFFFULL; 993e126ba97SEli Cohen 994e502b8b0SLeon Romanovsky if (IS_ENABLED(CONFIG_INFINIBAND_ON_DEMAND_PAGING)) { 99500815752SMoni Shoua if (dev->odp_caps.general_caps & IB_ODP_SUPPORT) 996e945c653SJason Gunthorpe props->kernel_cap_flags |= IBK_ON_DEMAND_PAGING; 9978cdd312cSHaggai Eran props->odp_caps = dev->odp_caps; 998a73a8955SMoni Shoua if (!uhw) { 999a73a8955SMoni Shoua /* ODP for kernel QPs is not implemented for receive 1000a73a8955SMoni Shoua * WQEs and SRQ WQEs 1001a73a8955SMoni Shoua */ 1002a73a8955SMoni Shoua props->odp_caps.per_transport_caps.rc_odp_caps &= 1003a73a8955SMoni Shoua ~(IB_ODP_SUPPORT_READ | 1004a73a8955SMoni Shoua IB_ODP_SUPPORT_SRQ_RECV); 1005a73a8955SMoni Shoua props->odp_caps.per_transport_caps.uc_odp_caps &= 1006a73a8955SMoni Shoua ~(IB_ODP_SUPPORT_READ | 1007a73a8955SMoni Shoua IB_ODP_SUPPORT_SRQ_RECV); 1008a73a8955SMoni Shoua props->odp_caps.per_transport_caps.ud_odp_caps &= 1009a73a8955SMoni Shoua ~(IB_ODP_SUPPORT_READ | 1010a73a8955SMoni Shoua IB_ODP_SUPPORT_SRQ_RECV); 1011a73a8955SMoni Shoua props->odp_caps.per_transport_caps.xrc_odp_caps &= 1012a73a8955SMoni Shoua ~(IB_ODP_SUPPORT_READ | 1013a73a8955SMoni Shoua IB_ODP_SUPPORT_SRQ_RECV); 1014a73a8955SMoni Shoua } 1015e502b8b0SLeon Romanovsky } 10168cdd312cSHaggai Eran 1017e53a9d26SParav Pandit if (mlx5_core_is_vf(mdev)) 1018e945c653SJason Gunthorpe props->kernel_cap_flags |= IBK_VIRTUAL_FUNCTION; 1019eff901d3SEli Cohen 102031f69a82SYishai Hadas if (mlx5_ib_port_link_layer(ibdev, 1) == 102185c7c014SDaniel Jurgens IB_LINK_LAYER_ETHERNET && raw_support) { 102231f69a82SYishai Hadas props->rss_caps.max_rwq_indirection_tables = 102331f69a82SYishai Hadas 1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt); 102431f69a82SYishai Hadas props->rss_caps.max_rwq_indirection_table_size = 102531f69a82SYishai Hadas 1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt_size); 102631f69a82SYishai Hadas props->rss_caps.supported_qpts = 1 << IB_QPT_RAW_PACKET; 102731f69a82SYishai Hadas props->max_wq_type_rq = 102831f69a82SYishai Hadas 1 << MLX5_CAP_GEN(dev->mdev, log_max_rq); 102931f69a82SYishai Hadas } 103031f69a82SYishai Hadas 1031eb761894SArtemy Kovalyov if (MLX5_CAP_GEN(mdev, tag_matching)) { 103278b1beb0SLeon Romanovsky props->tm_caps.max_num_tags = 1033eb761894SArtemy Kovalyov (1 << MLX5_CAP_GEN(mdev, log_tag_matching_list_sz)) - 1; 103478b1beb0SLeon Romanovsky props->tm_caps.max_ops = 1035eb761894SArtemy Kovalyov 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz); 103678b1beb0SLeon Romanovsky props->tm_caps.max_sge = MLX5_TM_MAX_SGE; 1037eb761894SArtemy Kovalyov } 1038eb761894SArtemy Kovalyov 103989705e92SDanit Goldberg if (MLX5_CAP_GEN(mdev, tag_matching) && 104089705e92SDanit Goldberg MLX5_CAP_GEN(mdev, rndv_offload_rc)) { 104189705e92SDanit Goldberg props->tm_caps.flags = IB_TM_CAP_RNDV_RC; 104289705e92SDanit Goldberg props->tm_caps.max_rndv_hdr_size = MLX5_TM_MAX_RNDV_MSG_SIZE; 104389705e92SDanit Goldberg } 104489705e92SDanit Goldberg 104587ab3f52SYonatan Cohen if (MLX5_CAP_GEN(dev->mdev, cq_moderation)) { 104687ab3f52SYonatan Cohen props->cq_caps.max_cq_moderation_count = 104787ab3f52SYonatan Cohen MLX5_MAX_CQ_COUNT; 104887ab3f52SYonatan Cohen props->cq_caps.max_cq_moderation_period = 104987ab3f52SYonatan Cohen MLX5_MAX_CQ_PERIOD; 105087ab3f52SYonatan Cohen } 105187ab3f52SYonatan Cohen 1052a762d460SLeon Romanovsky if (offsetofend(typeof(resp), cqe_comp_caps) <= uhw_outlen) { 1053572f46bfSYonatan Cohen resp.response_length += sizeof(resp.cqe_comp_caps); 1054572f46bfSYonatan Cohen 1055572f46bfSYonatan Cohen if (MLX5_CAP_GEN(dev->mdev, cqe_compression)) { 10567e43a2a5SBodong Wang resp.cqe_comp_caps.max_num = 1057572f46bfSYonatan Cohen MLX5_CAP_GEN(dev->mdev, 1058572f46bfSYonatan Cohen cqe_compression_max_num); 1059572f46bfSYonatan Cohen 10607e43a2a5SBodong Wang resp.cqe_comp_caps.supported_format = 10617e43a2a5SBodong Wang MLX5_IB_CQE_RES_FORMAT_HASH | 10627e43a2a5SBodong Wang MLX5_IB_CQE_RES_FORMAT_CSUM; 10636f1006a4SYonatan Cohen 10646f1006a4SYonatan Cohen if (MLX5_CAP_GEN(dev->mdev, mini_cqe_resp_stride_index)) 10656f1006a4SYonatan Cohen resp.cqe_comp_caps.supported_format |= 10666f1006a4SYonatan Cohen MLX5_IB_CQE_RES_FORMAT_CSUM_STRIDX; 1067572f46bfSYonatan Cohen } 10687e43a2a5SBodong Wang } 10697e43a2a5SBodong Wang 1070a762d460SLeon Romanovsky if (offsetofend(typeof(resp), packet_pacing_caps) <= uhw_outlen && 107185c7c014SDaniel Jurgens raw_support) { 1072d949167dSBodong Wang if (MLX5_CAP_QOS(mdev, packet_pacing) && 1073d949167dSBodong Wang MLX5_CAP_GEN(mdev, qos)) { 1074d949167dSBodong Wang resp.packet_pacing_caps.qp_rate_limit_max = 1075d949167dSBodong Wang MLX5_CAP_QOS(mdev, packet_pacing_max_rate); 1076d949167dSBodong Wang resp.packet_pacing_caps.qp_rate_limit_min = 1077d949167dSBodong Wang MLX5_CAP_QOS(mdev, packet_pacing_min_rate); 1078d949167dSBodong Wang resp.packet_pacing_caps.supported_qpts |= 1079d949167dSBodong Wang 1 << IB_QPT_RAW_PACKET; 108061147f39SBodong Wang if (MLX5_CAP_QOS(mdev, packet_pacing_burst_bound) && 108161147f39SBodong Wang MLX5_CAP_QOS(mdev, packet_pacing_typical_size)) 108261147f39SBodong Wang resp.packet_pacing_caps.cap_flags |= 108361147f39SBodong Wang MLX5_IB_PP_SUPPORT_BURST; 1084d949167dSBodong Wang } 1085d949167dSBodong Wang resp.response_length += sizeof(resp.packet_pacing_caps); 1086d949167dSBodong Wang } 1087d949167dSBodong Wang 1088a762d460SLeon Romanovsky if (offsetofend(typeof(resp), mlx5_ib_support_multi_pkt_send_wqes) <= 1089a762d460SLeon Romanovsky uhw_outlen) { 1090795b609cSBodong Wang if (MLX5_CAP_ETH(mdev, multi_pkt_send_wqe)) 10919f885201SLeon Romanovsky resp.mlx5_ib_support_multi_pkt_send_wqes = 1092795b609cSBodong Wang MLX5_IB_ALLOW_MPW; 1093050da902SBodong Wang 1094050da902SBodong Wang if (MLX5_CAP_ETH(mdev, enhanced_multi_pkt_send_wqe)) 1095050da902SBodong Wang resp.mlx5_ib_support_multi_pkt_send_wqes |= 1096050da902SBodong Wang MLX5_IB_SUPPORT_EMPW; 1097050da902SBodong Wang 10989f885201SLeon Romanovsky resp.response_length += 10999f885201SLeon Romanovsky sizeof(resp.mlx5_ib_support_multi_pkt_send_wqes); 11009f885201SLeon Romanovsky } 11019f885201SLeon Romanovsky 1102a762d460SLeon Romanovsky if (offsetofend(typeof(resp), flags) <= uhw_outlen) { 1103de57f2adSGuy Levi resp.response_length += sizeof(resp.flags); 11047a0c8f42SGuy Levi 1105de57f2adSGuy Levi if (MLX5_CAP_GEN(mdev, cqe_compression_128)) 1106de57f2adSGuy Levi resp.flags |= 1107de57f2adSGuy Levi MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_COMP; 11087a0c8f42SGuy Levi 11097a0c8f42SGuy Levi if (MLX5_CAP_GEN(mdev, cqe_128_always)) 11107a0c8f42SGuy Levi resp.flags |= MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_PAD; 11117e11b911SDanit Goldberg if (MLX5_CAP_GEN(mdev, qp_packet_based)) 11127e11b911SDanit Goldberg resp.flags |= 11137e11b911SDanit Goldberg MLX5_IB_QUERY_DEV_RESP_PACKET_BASED_CREDIT_MODE; 11147249c8eaSGuy Levi 11157249c8eaSGuy Levi resp.flags |= MLX5_IB_QUERY_DEV_RESP_FLAGS_SCAT2CQE_DCT; 1116de57f2adSGuy Levi } 11179f885201SLeon Romanovsky 1118a762d460SLeon Romanovsky if (offsetofend(typeof(resp), sw_parsing_caps) <= uhw_outlen) { 111996dc3fc5SNoa Osherovich resp.response_length += sizeof(resp.sw_parsing_caps); 112096dc3fc5SNoa Osherovich if (MLX5_CAP_ETH(mdev, swp)) { 112196dc3fc5SNoa Osherovich resp.sw_parsing_caps.sw_parsing_offloads |= 112296dc3fc5SNoa Osherovich MLX5_IB_SW_PARSING; 112396dc3fc5SNoa Osherovich 112496dc3fc5SNoa Osherovich if (MLX5_CAP_ETH(mdev, swp_csum)) 112596dc3fc5SNoa Osherovich resp.sw_parsing_caps.sw_parsing_offloads |= 112696dc3fc5SNoa Osherovich MLX5_IB_SW_PARSING_CSUM; 112796dc3fc5SNoa Osherovich 112896dc3fc5SNoa Osherovich if (MLX5_CAP_ETH(mdev, swp_lso)) 112996dc3fc5SNoa Osherovich resp.sw_parsing_caps.sw_parsing_offloads |= 113096dc3fc5SNoa Osherovich MLX5_IB_SW_PARSING_LSO; 113196dc3fc5SNoa Osherovich 113296dc3fc5SNoa Osherovich if (resp.sw_parsing_caps.sw_parsing_offloads) 113396dc3fc5SNoa Osherovich resp.sw_parsing_caps.supported_qpts = 113496dc3fc5SNoa Osherovich BIT(IB_QPT_RAW_PACKET); 113596dc3fc5SNoa Osherovich } 113696dc3fc5SNoa Osherovich } 113796dc3fc5SNoa Osherovich 1138a762d460SLeon Romanovsky if (offsetofend(typeof(resp), striding_rq_caps) <= uhw_outlen && 113985c7c014SDaniel Jurgens raw_support) { 1140b4f34597SNoa Osherovich resp.response_length += sizeof(resp.striding_rq_caps); 1141b4f34597SNoa Osherovich if (MLX5_CAP_GEN(mdev, striding_rq)) { 1142b4f34597SNoa Osherovich resp.striding_rq_caps.min_single_stride_log_num_of_bytes = 1143b4f34597SNoa Osherovich MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES; 1144b4f34597SNoa Osherovich resp.striding_rq_caps.max_single_stride_log_num_of_bytes = 1145b4f34597SNoa Osherovich MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES; 1146c16339b6SMark Zhang if (MLX5_CAP_GEN(dev->mdev, ext_stride_num_range)) 1147c16339b6SMark Zhang resp.striding_rq_caps 1148c16339b6SMark Zhang .min_single_wqe_log_num_of_strides = 1149c16339b6SMark Zhang MLX5_EXT_MIN_SINGLE_WQE_LOG_NUM_STRIDES; 1150c16339b6SMark Zhang else 1151c16339b6SMark Zhang resp.striding_rq_caps 1152c16339b6SMark Zhang .min_single_wqe_log_num_of_strides = 1153b4f34597SNoa Osherovich MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES; 1154b4f34597SNoa Osherovich resp.striding_rq_caps.max_single_wqe_log_num_of_strides = 1155b4f34597SNoa Osherovich MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES; 1156b4f34597SNoa Osherovich resp.striding_rq_caps.supported_qpts = 1157b4f34597SNoa Osherovich BIT(IB_QPT_RAW_PACKET); 1158b4f34597SNoa Osherovich } 1159b4f34597SNoa Osherovich } 1160b4f34597SNoa Osherovich 1161a762d460SLeon Romanovsky if (offsetofend(typeof(resp), tunnel_offloads_caps) <= uhw_outlen) { 1162f95ef6cbSMaor Gottlieb resp.response_length += sizeof(resp.tunnel_offloads_caps); 1163f95ef6cbSMaor Gottlieb if (MLX5_CAP_ETH(mdev, tunnel_stateless_vxlan)) 1164f95ef6cbSMaor Gottlieb resp.tunnel_offloads_caps |= 1165f95ef6cbSMaor Gottlieb MLX5_IB_TUNNELED_OFFLOADS_VXLAN; 1166f95ef6cbSMaor Gottlieb if (MLX5_CAP_ETH(mdev, tunnel_stateless_geneve_rx)) 1167f95ef6cbSMaor Gottlieb resp.tunnel_offloads_caps |= 1168f95ef6cbSMaor Gottlieb MLX5_IB_TUNNELED_OFFLOADS_GENEVE; 1169f95ef6cbSMaor Gottlieb if (MLX5_CAP_ETH(mdev, tunnel_stateless_gre)) 1170f95ef6cbSMaor Gottlieb resp.tunnel_offloads_caps |= 1171f95ef6cbSMaor Gottlieb MLX5_IB_TUNNELED_OFFLOADS_GRE; 117241e684efSAlex Vesker if (MLX5_CAP_ETH(mdev, tunnel_stateless_mpls_over_gre)) 1173e818e255SAriel Levkovich resp.tunnel_offloads_caps |= 1174e818e255SAriel Levkovich MLX5_IB_TUNNELED_OFFLOADS_MPLS_GRE; 117541e684efSAlex Vesker if (MLX5_CAP_ETH(mdev, tunnel_stateless_mpls_over_udp)) 1176e818e255SAriel Levkovich resp.tunnel_offloads_caps |= 1177e818e255SAriel Levkovich MLX5_IB_TUNNELED_OFFLOADS_MPLS_UDP; 1178f95ef6cbSMaor Gottlieb } 1179f95ef6cbSMaor Gottlieb 118011656f59SLior Nahmanson if (offsetofend(typeof(resp), dci_streams_caps) <= uhw_outlen) { 118111656f59SLior Nahmanson resp.response_length += sizeof(resp.dci_streams_caps); 118211656f59SLior Nahmanson 118311656f59SLior Nahmanson resp.dci_streams_caps.max_log_num_concurent = 118411656f59SLior Nahmanson MLX5_CAP_GEN(mdev, log_max_dci_stream_channels); 118511656f59SLior Nahmanson 118611656f59SLior Nahmanson resp.dci_streams_caps.max_log_num_errored = 118711656f59SLior Nahmanson MLX5_CAP_GEN(mdev, log_max_dci_errored_streams); 118811656f59SLior Nahmanson } 118911656f59SLior Nahmanson 119048357091SLeon Romanovsky if (uhw_outlen) { 1191402ca536SBodong Wang err = ib_copy_to_udata(uhw, &resp, resp.response_length); 1192402ca536SBodong Wang 1193402ca536SBodong Wang if (err) 1194402ca536SBodong Wang return err; 1195402ca536SBodong Wang } 1196402ca536SBodong Wang 11971b5daf11SMajd Dibbiny return 0; 11981b5daf11SMajd Dibbiny } 1199e126ba97SEli Cohen 1200639bf441SAharon Landau static void translate_active_width(struct ib_device *ibdev, u16 active_width, 12011b5daf11SMajd Dibbiny u8 *ib_width) 12021b5daf11SMajd Dibbiny { 12031b5daf11SMajd Dibbiny struct mlx5_ib_dev *dev = to_mdev(ibdev); 12041b5daf11SMajd Dibbiny 1205e27014bdSAharon Landau if (active_width & MLX5_PTYS_WIDTH_1X) 12061b5daf11SMajd Dibbiny *ib_width = IB_WIDTH_1X; 1207e27014bdSAharon Landau else if (active_width & MLX5_PTYS_WIDTH_2X) 1208d764970bSMichael Guralnik *ib_width = IB_WIDTH_2X; 1209e27014bdSAharon Landau else if (active_width & MLX5_PTYS_WIDTH_4X) 12101b5daf11SMajd Dibbiny *ib_width = IB_WIDTH_4X; 1211e27014bdSAharon Landau else if (active_width & MLX5_PTYS_WIDTH_8X) 12121b5daf11SMajd Dibbiny *ib_width = IB_WIDTH_8X; 1213e27014bdSAharon Landau else if (active_width & MLX5_PTYS_WIDTH_12X) 12141b5daf11SMajd Dibbiny *ib_width = IB_WIDTH_12X; 1215db7a691aSMichael Guralnik else { 1216db7a691aSMichael Guralnik mlx5_ib_dbg(dev, "Invalid active_width %d, setting width to default value: 4x\n", 1217e27014bdSAharon Landau active_width); 1218db7a691aSMichael Guralnik *ib_width = IB_WIDTH_4X; 12191b5daf11SMajd Dibbiny } 12201b5daf11SMajd Dibbiny 1221db7a691aSMichael Guralnik return; 12221b5daf11SMajd Dibbiny } 12231b5daf11SMajd Dibbiny 12241b5daf11SMajd Dibbiny static int mlx5_mtu_to_ib_mtu(int mtu) 12251b5daf11SMajd Dibbiny { 12261b5daf11SMajd Dibbiny switch (mtu) { 12271b5daf11SMajd Dibbiny case 256: return 1; 12281b5daf11SMajd Dibbiny case 512: return 2; 12291b5daf11SMajd Dibbiny case 1024: return 3; 12301b5daf11SMajd Dibbiny case 2048: return 4; 12311b5daf11SMajd Dibbiny case 4096: return 5; 12321b5daf11SMajd Dibbiny default: 12331b5daf11SMajd Dibbiny pr_warn("invalid mtu\n"); 12341b5daf11SMajd Dibbiny return -1; 12351b5daf11SMajd Dibbiny } 12361b5daf11SMajd Dibbiny } 12371b5daf11SMajd Dibbiny 12381b5daf11SMajd Dibbiny enum ib_max_vl_num { 12391b5daf11SMajd Dibbiny __IB_MAX_VL_0 = 1, 12401b5daf11SMajd Dibbiny __IB_MAX_VL_0_1 = 2, 12411b5daf11SMajd Dibbiny __IB_MAX_VL_0_3 = 3, 12421b5daf11SMajd Dibbiny __IB_MAX_VL_0_7 = 4, 12431b5daf11SMajd Dibbiny __IB_MAX_VL_0_14 = 5, 12441b5daf11SMajd Dibbiny }; 12451b5daf11SMajd Dibbiny 12461b5daf11SMajd Dibbiny enum mlx5_vl_hw_cap { 12471b5daf11SMajd Dibbiny MLX5_VL_HW_0 = 1, 12481b5daf11SMajd Dibbiny MLX5_VL_HW_0_1 = 2, 12491b5daf11SMajd Dibbiny MLX5_VL_HW_0_2 = 3, 12501b5daf11SMajd Dibbiny MLX5_VL_HW_0_3 = 4, 12511b5daf11SMajd Dibbiny MLX5_VL_HW_0_4 = 5, 12521b5daf11SMajd Dibbiny MLX5_VL_HW_0_5 = 6, 12531b5daf11SMajd Dibbiny MLX5_VL_HW_0_6 = 7, 12541b5daf11SMajd Dibbiny MLX5_VL_HW_0_7 = 8, 12551b5daf11SMajd Dibbiny MLX5_VL_HW_0_14 = 15 12561b5daf11SMajd Dibbiny }; 12571b5daf11SMajd Dibbiny 12581b5daf11SMajd Dibbiny static int translate_max_vl_num(struct ib_device *ibdev, u8 vl_hw_cap, 12591b5daf11SMajd Dibbiny u8 *max_vl_num) 12601b5daf11SMajd Dibbiny { 12611b5daf11SMajd Dibbiny switch (vl_hw_cap) { 12621b5daf11SMajd Dibbiny case MLX5_VL_HW_0: 12631b5daf11SMajd Dibbiny *max_vl_num = __IB_MAX_VL_0; 12641b5daf11SMajd Dibbiny break; 12651b5daf11SMajd Dibbiny case MLX5_VL_HW_0_1: 12661b5daf11SMajd Dibbiny *max_vl_num = __IB_MAX_VL_0_1; 12671b5daf11SMajd Dibbiny break; 12681b5daf11SMajd Dibbiny case MLX5_VL_HW_0_3: 12691b5daf11SMajd Dibbiny *max_vl_num = __IB_MAX_VL_0_3; 12701b5daf11SMajd Dibbiny break; 12711b5daf11SMajd Dibbiny case MLX5_VL_HW_0_7: 12721b5daf11SMajd Dibbiny *max_vl_num = __IB_MAX_VL_0_7; 12731b5daf11SMajd Dibbiny break; 12741b5daf11SMajd Dibbiny case MLX5_VL_HW_0_14: 12751b5daf11SMajd Dibbiny *max_vl_num = __IB_MAX_VL_0_14; 12761b5daf11SMajd Dibbiny break; 12771b5daf11SMajd Dibbiny 12781b5daf11SMajd Dibbiny default: 12791b5daf11SMajd Dibbiny return -EINVAL; 12801b5daf11SMajd Dibbiny } 12811b5daf11SMajd Dibbiny 12821b5daf11SMajd Dibbiny return 0; 12831b5daf11SMajd Dibbiny } 12841b5daf11SMajd Dibbiny 12851fb7f897SMark Bloch static int mlx5_query_hca_port(struct ib_device *ibdev, u32 port, 12861b5daf11SMajd Dibbiny struct ib_port_attr *props) 12871b5daf11SMajd Dibbiny { 12881b5daf11SMajd Dibbiny struct mlx5_ib_dev *dev = to_mdev(ibdev); 12891b5daf11SMajd Dibbiny struct mlx5_core_dev *mdev = dev->mdev; 12901b5daf11SMajd Dibbiny struct mlx5_hca_vport_context *rep; 1291046339eaSSaeed Mahameed u16 max_mtu; 1292046339eaSSaeed Mahameed u16 oper_mtu; 12931b5daf11SMajd Dibbiny int err; 1294639bf441SAharon Landau u16 ib_link_width_oper; 12951b5daf11SMajd Dibbiny u8 vl_hw_cap; 12961b5daf11SMajd Dibbiny 12971b5daf11SMajd Dibbiny rep = kzalloc(sizeof(*rep), GFP_KERNEL); 12981b5daf11SMajd Dibbiny if (!rep) { 12991b5daf11SMajd Dibbiny err = -ENOMEM; 13001b5daf11SMajd Dibbiny goto out; 13011b5daf11SMajd Dibbiny } 13021b5daf11SMajd Dibbiny 1303c4550c63SOr Gerlitz /* props being zeroed by the caller, avoid zeroing it here */ 13041b5daf11SMajd Dibbiny 13051b5daf11SMajd Dibbiny err = mlx5_query_hca_vport_context(mdev, 0, port, 0, rep); 13061b5daf11SMajd Dibbiny if (err) 13071b5daf11SMajd Dibbiny goto out; 13081b5daf11SMajd Dibbiny 13091b5daf11SMajd Dibbiny props->lid = rep->lid; 13101b5daf11SMajd Dibbiny props->lmc = rep->lmc; 13111b5daf11SMajd Dibbiny props->sm_lid = rep->sm_lid; 13121b5daf11SMajd Dibbiny props->sm_sl = rep->sm_sl; 13131b5daf11SMajd Dibbiny props->state = rep->vport_state; 13141b5daf11SMajd Dibbiny props->phys_state = rep->port_physical_state; 13151b5daf11SMajd Dibbiny props->port_cap_flags = rep->cap_mask1; 13161b5daf11SMajd Dibbiny props->gid_tbl_len = mlx5_get_gid_table_len(MLX5_CAP_GEN(mdev, gid_table_size)); 13171b5daf11SMajd Dibbiny props->max_msg_sz = 1 << MLX5_CAP_GEN(mdev, log_max_msg); 13181b5daf11SMajd Dibbiny props->pkey_tbl_len = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, pkey_table_size)); 13191b5daf11SMajd Dibbiny props->bad_pkey_cntr = rep->pkey_violation_counter; 13201b5daf11SMajd Dibbiny props->qkey_viol_cntr = rep->qkey_violation_counter; 13211b5daf11SMajd Dibbiny props->subnet_timeout = rep->subnet_timeout; 13221b5daf11SMajd Dibbiny props->init_type_reply = rep->init_type_reply; 13231b5daf11SMajd Dibbiny 13244106a758SMichael Guralnik if (props->port_cap_flags & IB_PORT_CAP_MASK2_SUP) 13254106a758SMichael Guralnik props->port_cap_flags2 = rep->cap_mask2; 13264106a758SMichael Guralnik 1327639bf441SAharon Landau err = mlx5_query_ib_port_oper(mdev, &ib_link_width_oper, 1328376ceb31SAharon Landau &props->active_speed, port); 13291b5daf11SMajd Dibbiny if (err) 13301b5daf11SMajd Dibbiny goto out; 13311b5daf11SMajd Dibbiny 1332db7a691aSMichael Guralnik translate_active_width(ibdev, ib_link_width_oper, &props->active_width); 1333db7a691aSMichael Guralnik 1334facc9699SSaeed Mahameed mlx5_query_port_max_mtu(mdev, &max_mtu, port); 13351b5daf11SMajd Dibbiny 13361b5daf11SMajd Dibbiny props->max_mtu = mlx5_mtu_to_ib_mtu(max_mtu); 13371b5daf11SMajd Dibbiny 1338facc9699SSaeed Mahameed mlx5_query_port_oper_mtu(mdev, &oper_mtu, port); 13391b5daf11SMajd Dibbiny 13401b5daf11SMajd Dibbiny props->active_mtu = mlx5_mtu_to_ib_mtu(oper_mtu); 13411b5daf11SMajd Dibbiny 13421b5daf11SMajd Dibbiny err = mlx5_query_port_vl_hw_cap(mdev, &vl_hw_cap, port); 13431b5daf11SMajd Dibbiny if (err) 13441b5daf11SMajd Dibbiny goto out; 13451b5daf11SMajd Dibbiny 13461b5daf11SMajd Dibbiny err = translate_max_vl_num(ibdev, vl_hw_cap, 13471b5daf11SMajd Dibbiny &props->max_vl_num); 13481b5daf11SMajd Dibbiny out: 13491b5daf11SMajd Dibbiny kfree(rep); 1350e126ba97SEli Cohen return err; 1351e126ba97SEli Cohen } 1352e126ba97SEli Cohen 13531fb7f897SMark Bloch int mlx5_ib_query_port(struct ib_device *ibdev, u32 port, 1354e126ba97SEli Cohen struct ib_port_attr *props) 1355e126ba97SEli Cohen { 1356095b0927SIlan Tayari unsigned int count; 1357095b0927SIlan Tayari int ret; 1358095b0927SIlan Tayari 13591b5daf11SMajd Dibbiny switch (mlx5_get_vport_access_method(ibdev)) { 13601b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_MAD: 1361095b0927SIlan Tayari ret = mlx5_query_mad_ifc_port(ibdev, port, props); 1362095b0927SIlan Tayari break; 1363e126ba97SEli Cohen 13641b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_HCA: 1365095b0927SIlan Tayari ret = mlx5_query_hca_port(ibdev, port, props); 1366095b0927SIlan Tayari break; 13671b5daf11SMajd Dibbiny 13683f89a643SAchiad Shochat case MLX5_VPORT_ACCESS_METHOD_NIC: 1369095b0927SIlan Tayari ret = mlx5_query_port_roce(ibdev, port, props); 1370095b0927SIlan Tayari break; 13713f89a643SAchiad Shochat 13721b5daf11SMajd Dibbiny default: 1373095b0927SIlan Tayari ret = -EINVAL; 1374e126ba97SEli Cohen } 1375095b0927SIlan Tayari 1376095b0927SIlan Tayari if (!ret && props) { 1377b3cbd6f0SDaniel Jurgens struct mlx5_ib_dev *dev = to_mdev(ibdev); 1378b3cbd6f0SDaniel Jurgens struct mlx5_core_dev *mdev; 1379b3cbd6f0SDaniel Jurgens bool put_mdev = true; 1380b3cbd6f0SDaniel Jurgens 1381b3cbd6f0SDaniel Jurgens mdev = mlx5_ib_get_native_port_mdev(dev, port, NULL); 1382b3cbd6f0SDaniel Jurgens if (!mdev) { 1383b3cbd6f0SDaniel Jurgens /* If the port isn't affiliated yet query the master. 1384b3cbd6f0SDaniel Jurgens * The master and slave will have the same values. 1385b3cbd6f0SDaniel Jurgens */ 1386b3cbd6f0SDaniel Jurgens mdev = dev->mdev; 1387b3cbd6f0SDaniel Jurgens port = 1; 1388b3cbd6f0SDaniel Jurgens put_mdev = false; 1389b3cbd6f0SDaniel Jurgens } 1390b3cbd6f0SDaniel Jurgens count = mlx5_core_reserved_gids_count(mdev); 1391b3cbd6f0SDaniel Jurgens if (put_mdev) 1392b3cbd6f0SDaniel Jurgens mlx5_ib_put_native_port_mdev(dev, port); 1393095b0927SIlan Tayari props->gid_tbl_len -= count; 1394095b0927SIlan Tayari } 1395095b0927SIlan Tayari return ret; 1396e126ba97SEli Cohen } 1397e126ba97SEli Cohen 13981fb7f897SMark Bloch static int mlx5_ib_rep_query_port(struct ib_device *ibdev, u32 port, 13998e6efa3aSMark Bloch struct ib_port_attr *props) 14008e6efa3aSMark Bloch { 14017a58779eSParav Pandit return mlx5_query_port_roce(ibdev, port, props); 14028e6efa3aSMark Bloch } 14038e6efa3aSMark Bloch 14041fb7f897SMark Bloch static int mlx5_ib_rep_query_pkey(struct ib_device *ibdev, u32 port, u16 index, 1405d6fd59e1SParav Pandit u16 *pkey) 1406d6fd59e1SParav Pandit { 1407d6fd59e1SParav Pandit /* Default special Pkey for representor device port as per the 1408d6fd59e1SParav Pandit * IB specification 1.3 section 10.9.1.2. 1409d6fd59e1SParav Pandit */ 1410d6fd59e1SParav Pandit *pkey = 0xffff; 1411d6fd59e1SParav Pandit return 0; 1412d6fd59e1SParav Pandit } 1413d6fd59e1SParav Pandit 14141fb7f897SMark Bloch static int mlx5_ib_query_gid(struct ib_device *ibdev, u32 port, int index, 1415e126ba97SEli Cohen union ib_gid *gid) 1416e126ba97SEli Cohen { 14171b5daf11SMajd Dibbiny struct mlx5_ib_dev *dev = to_mdev(ibdev); 14181b5daf11SMajd Dibbiny struct mlx5_core_dev *mdev = dev->mdev; 1419e126ba97SEli Cohen 14201b5daf11SMajd Dibbiny switch (mlx5_get_vport_access_method(ibdev)) { 14211b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_MAD: 14221b5daf11SMajd Dibbiny return mlx5_query_mad_ifc_gids(ibdev, port, index, gid); 1423e126ba97SEli Cohen 14241b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_HCA: 14251b5daf11SMajd Dibbiny return mlx5_query_hca_vport_gid(mdev, 0, port, 0, index, gid); 1426e126ba97SEli Cohen 14271b5daf11SMajd Dibbiny default: 14281b5daf11SMajd Dibbiny return -EINVAL; 14291b5daf11SMajd Dibbiny } 1430e126ba97SEli Cohen 1431e126ba97SEli Cohen } 1432e126ba97SEli Cohen 14331fb7f897SMark Bloch static int mlx5_query_hca_nic_pkey(struct ib_device *ibdev, u32 port, 1434b3cbd6f0SDaniel Jurgens u16 index, u16 *pkey) 1435b3cbd6f0SDaniel Jurgens { 1436b3cbd6f0SDaniel Jurgens struct mlx5_ib_dev *dev = to_mdev(ibdev); 1437b3cbd6f0SDaniel Jurgens struct mlx5_core_dev *mdev; 1438b3cbd6f0SDaniel Jurgens bool put_mdev = true; 14391fb7f897SMark Bloch u32 mdev_port_num; 1440b3cbd6f0SDaniel Jurgens int err; 1441b3cbd6f0SDaniel Jurgens 1442b3cbd6f0SDaniel Jurgens mdev = mlx5_ib_get_native_port_mdev(dev, port, &mdev_port_num); 1443b3cbd6f0SDaniel Jurgens if (!mdev) { 1444b3cbd6f0SDaniel Jurgens /* The port isn't affiliated yet, get the PKey from the master 1445b3cbd6f0SDaniel Jurgens * port. For RoCE the PKey tables will be the same. 1446b3cbd6f0SDaniel Jurgens */ 1447b3cbd6f0SDaniel Jurgens put_mdev = false; 1448b3cbd6f0SDaniel Jurgens mdev = dev->mdev; 1449b3cbd6f0SDaniel Jurgens mdev_port_num = 1; 1450b3cbd6f0SDaniel Jurgens } 1451b3cbd6f0SDaniel Jurgens 1452b3cbd6f0SDaniel Jurgens err = mlx5_query_hca_vport_pkey(mdev, 0, mdev_port_num, 0, 1453b3cbd6f0SDaniel Jurgens index, pkey); 1454b3cbd6f0SDaniel Jurgens if (put_mdev) 1455b3cbd6f0SDaniel Jurgens mlx5_ib_put_native_port_mdev(dev, port); 1456b3cbd6f0SDaniel Jurgens 1457b3cbd6f0SDaniel Jurgens return err; 1458b3cbd6f0SDaniel Jurgens } 1459b3cbd6f0SDaniel Jurgens 14601fb7f897SMark Bloch static int mlx5_ib_query_pkey(struct ib_device *ibdev, u32 port, u16 index, 1461e126ba97SEli Cohen u16 *pkey) 1462e126ba97SEli Cohen { 14631b5daf11SMajd Dibbiny switch (mlx5_get_vport_access_method(ibdev)) { 14641b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_MAD: 14651b5daf11SMajd Dibbiny return mlx5_query_mad_ifc_pkey(ibdev, port, index, pkey); 1466e126ba97SEli Cohen 14671b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_HCA: 14681b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_NIC: 1469b3cbd6f0SDaniel Jurgens return mlx5_query_hca_nic_pkey(ibdev, port, index, pkey); 14701b5daf11SMajd Dibbiny default: 14711b5daf11SMajd Dibbiny return -EINVAL; 1472e126ba97SEli Cohen } 14731b5daf11SMajd Dibbiny } 1474e126ba97SEli Cohen 1475e126ba97SEli Cohen static int mlx5_ib_modify_device(struct ib_device *ibdev, int mask, 1476e126ba97SEli Cohen struct ib_device_modify *props) 1477e126ba97SEli Cohen { 1478e126ba97SEli Cohen struct mlx5_ib_dev *dev = to_mdev(ibdev); 1479e126ba97SEli Cohen struct mlx5_reg_node_desc in; 1480e126ba97SEli Cohen struct mlx5_reg_node_desc out; 1481e126ba97SEli Cohen int err; 1482e126ba97SEli Cohen 1483e126ba97SEli Cohen if (mask & ~IB_DEVICE_MODIFY_NODE_DESC) 1484e126ba97SEli Cohen return -EOPNOTSUPP; 1485e126ba97SEli Cohen 1486e126ba97SEli Cohen if (!(mask & IB_DEVICE_MODIFY_NODE_DESC)) 1487e126ba97SEli Cohen return 0; 1488e126ba97SEli Cohen 1489e126ba97SEli Cohen /* 1490e126ba97SEli Cohen * If possible, pass node desc to FW, so it can generate 1491e126ba97SEli Cohen * a 144 trap. If cmd fails, just ignore. 1492e126ba97SEli Cohen */ 1493bd99fdeaSYuval Shaia memcpy(&in, props->node_desc, IB_DEVICE_NODE_DESC_MAX); 14949603b61dSJack Morgenstein err = mlx5_core_access_reg(dev->mdev, &in, sizeof(in), &out, 1495e126ba97SEli Cohen sizeof(out), MLX5_REG_NODE_DESC, 0, 1); 1496e126ba97SEli Cohen if (err) 1497e126ba97SEli Cohen return err; 1498e126ba97SEli Cohen 1499bd99fdeaSYuval Shaia memcpy(ibdev->node_desc, props->node_desc, IB_DEVICE_NODE_DESC_MAX); 1500e126ba97SEli Cohen 1501e126ba97SEli Cohen return err; 1502e126ba97SEli Cohen } 1503e126ba97SEli Cohen 15041fb7f897SMark Bloch static int set_port_caps_atomic(struct mlx5_ib_dev *dev, u32 port_num, u32 mask, 1505cdbe33d0SEli Cohen u32 value) 1506cdbe33d0SEli Cohen { 1507cdbe33d0SEli Cohen struct mlx5_hca_vport_context ctx = {}; 1508b3cbd6f0SDaniel Jurgens struct mlx5_core_dev *mdev; 15091fb7f897SMark Bloch u32 mdev_port_num; 1510cdbe33d0SEli Cohen int err; 1511cdbe33d0SEli Cohen 1512b3cbd6f0SDaniel Jurgens mdev = mlx5_ib_get_native_port_mdev(dev, port_num, &mdev_port_num); 1513b3cbd6f0SDaniel Jurgens if (!mdev) 1514b3cbd6f0SDaniel Jurgens return -ENODEV; 1515b3cbd6f0SDaniel Jurgens 1516b3cbd6f0SDaniel Jurgens err = mlx5_query_hca_vport_context(mdev, 0, mdev_port_num, 0, &ctx); 1517cdbe33d0SEli Cohen if (err) 1518b3cbd6f0SDaniel Jurgens goto out; 1519cdbe33d0SEli Cohen 1520cdbe33d0SEli Cohen if (~ctx.cap_mask1_perm & mask) { 1521cdbe33d0SEli Cohen mlx5_ib_warn(dev, "trying to change bitmask 0x%X but change supported 0x%X\n", 1522cdbe33d0SEli Cohen mask, ctx.cap_mask1_perm); 1523b3cbd6f0SDaniel Jurgens err = -EINVAL; 1524b3cbd6f0SDaniel Jurgens goto out; 1525cdbe33d0SEli Cohen } 1526cdbe33d0SEli Cohen 1527cdbe33d0SEli Cohen ctx.cap_mask1 = value; 1528cdbe33d0SEli Cohen ctx.cap_mask1_perm = mask; 1529b3cbd6f0SDaniel Jurgens err = mlx5_core_modify_hca_vport_context(mdev, 0, mdev_port_num, 1530b3cbd6f0SDaniel Jurgens 0, &ctx); 1531b3cbd6f0SDaniel Jurgens 1532b3cbd6f0SDaniel Jurgens out: 1533b3cbd6f0SDaniel Jurgens mlx5_ib_put_native_port_mdev(dev, port_num); 1534cdbe33d0SEli Cohen 1535cdbe33d0SEli Cohen return err; 1536cdbe33d0SEli Cohen } 1537cdbe33d0SEli Cohen 15381fb7f897SMark Bloch static int mlx5_ib_modify_port(struct ib_device *ibdev, u32 port, int mask, 1539e126ba97SEli Cohen struct ib_port_modify *props) 1540e126ba97SEli Cohen { 1541e126ba97SEli Cohen struct mlx5_ib_dev *dev = to_mdev(ibdev); 1542e126ba97SEli Cohen struct ib_port_attr attr; 1543e126ba97SEli Cohen u32 tmp; 1544e126ba97SEli Cohen int err; 1545cdbe33d0SEli Cohen u32 change_mask; 1546cdbe33d0SEli Cohen u32 value; 1547cdbe33d0SEli Cohen bool is_ib = (mlx5_ib_port_link_layer(ibdev, port) == 1548cdbe33d0SEli Cohen IB_LINK_LAYER_INFINIBAND); 1549cdbe33d0SEli Cohen 1550ec255879SMajd Dibbiny /* CM layer calls ib_modify_port() regardless of the link layer. For 1551ec255879SMajd Dibbiny * Ethernet ports, qkey violation and Port capabilities are meaningless. 1552ec255879SMajd Dibbiny */ 1553ec255879SMajd Dibbiny if (!is_ib) 1554ec255879SMajd Dibbiny return 0; 1555ec255879SMajd Dibbiny 1556cdbe33d0SEli Cohen if (MLX5_CAP_GEN(dev->mdev, ib_virt) && is_ib) { 1557cdbe33d0SEli Cohen change_mask = props->clr_port_cap_mask | props->set_port_cap_mask; 1558cdbe33d0SEli Cohen value = ~props->clr_port_cap_mask | props->set_port_cap_mask; 1559cdbe33d0SEli Cohen return set_port_caps_atomic(dev, port, change_mask, value); 1560cdbe33d0SEli Cohen } 1561e126ba97SEli Cohen 1562e126ba97SEli Cohen mutex_lock(&dev->cap_mask_mutex); 1563e126ba97SEli Cohen 1564c4550c63SOr Gerlitz err = ib_query_port(ibdev, port, &attr); 1565e126ba97SEli Cohen if (err) 1566e126ba97SEli Cohen goto out; 1567e126ba97SEli Cohen 1568e126ba97SEli Cohen tmp = (attr.port_cap_flags | props->set_port_cap_mask) & 1569e126ba97SEli Cohen ~props->clr_port_cap_mask; 1570e126ba97SEli Cohen 15719603b61dSJack Morgenstein err = mlx5_set_port_caps(dev->mdev, port, tmp); 1572e126ba97SEli Cohen 1573e126ba97SEli Cohen out: 1574e126ba97SEli Cohen mutex_unlock(&dev->cap_mask_mutex); 1575e126ba97SEli Cohen return err; 1576e126ba97SEli Cohen } 1577e126ba97SEli Cohen 157830aa60b3SEli Cohen static void print_lib_caps(struct mlx5_ib_dev *dev, u64 caps) 157930aa60b3SEli Cohen { 158030aa60b3SEli Cohen mlx5_ib_dbg(dev, "MLX5_LIB_CAP_4K_UAR = %s\n", 158130aa60b3SEli Cohen caps & MLX5_LIB_CAP_4K_UAR ? "y" : "n"); 158230aa60b3SEli Cohen } 158330aa60b3SEli Cohen 158431a78a5aSYishai Hadas static u16 calc_dynamic_bfregs(int uars_per_sys_page) 158531a78a5aSYishai Hadas { 158631a78a5aSYishai Hadas /* Large page with non 4k uar support might limit the dynamic size */ 158731a78a5aSYishai Hadas if (uars_per_sys_page == 1 && PAGE_SIZE > 4096) 158831a78a5aSYishai Hadas return MLX5_MIN_DYN_BFREGS; 158931a78a5aSYishai Hadas 159031a78a5aSYishai Hadas return MLX5_MAX_DYN_BFREGS; 159131a78a5aSYishai Hadas } 159231a78a5aSYishai Hadas 1593b037c29aSEli Cohen static int calc_total_bfregs(struct mlx5_ib_dev *dev, bool lib_uar_4k, 1594b037c29aSEli Cohen struct mlx5_ib_alloc_ucontext_req_v2 *req, 159531a78a5aSYishai Hadas struct mlx5_bfreg_info *bfregi) 1596b037c29aSEli Cohen { 1597b037c29aSEli Cohen int uars_per_sys_page; 1598b037c29aSEli Cohen int bfregs_per_sys_page; 1599b037c29aSEli Cohen int ref_bfregs = req->total_num_bfregs; 1600b037c29aSEli Cohen 1601b037c29aSEli Cohen if (req->total_num_bfregs == 0) 1602b037c29aSEli Cohen return -EINVAL; 1603b037c29aSEli Cohen 1604b037c29aSEli Cohen BUILD_BUG_ON(MLX5_MAX_BFREGS % MLX5_NON_FP_BFREGS_IN_PAGE); 1605b037c29aSEli Cohen BUILD_BUG_ON(MLX5_MAX_BFREGS < MLX5_NON_FP_BFREGS_IN_PAGE); 1606b037c29aSEli Cohen 1607b037c29aSEli Cohen if (req->total_num_bfregs > MLX5_MAX_BFREGS) 1608b037c29aSEli Cohen return -ENOMEM; 1609b037c29aSEli Cohen 1610b037c29aSEli Cohen uars_per_sys_page = get_uars_per_sys_page(dev, lib_uar_4k); 1611b037c29aSEli Cohen bfregs_per_sys_page = uars_per_sys_page * MLX5_NON_FP_BFREGS_PER_UAR; 161231a78a5aSYishai Hadas /* This holds the required static allocation asked by the user */ 1613b037c29aSEli Cohen req->total_num_bfregs = ALIGN(req->total_num_bfregs, bfregs_per_sys_page); 1614b037c29aSEli Cohen if (req->num_low_latency_bfregs > req->total_num_bfregs - 1) 1615b037c29aSEli Cohen return -EINVAL; 1616b037c29aSEli Cohen 161731a78a5aSYishai Hadas bfregi->num_static_sys_pages = req->total_num_bfregs / bfregs_per_sys_page; 161831a78a5aSYishai Hadas bfregi->num_dyn_bfregs = ALIGN(calc_dynamic_bfregs(uars_per_sys_page), bfregs_per_sys_page); 161931a78a5aSYishai Hadas bfregi->total_num_bfregs = req->total_num_bfregs + bfregi->num_dyn_bfregs; 162031a78a5aSYishai Hadas bfregi->num_sys_pages = bfregi->total_num_bfregs / bfregs_per_sys_page; 162131a78a5aSYishai Hadas 162231a78a5aSYishai Hadas mlx5_ib_dbg(dev, "uar_4k: fw support %s, lib support %s, user requested %d bfregs, allocated %d, total bfregs %d, using %d sys pages\n", 1623b037c29aSEli Cohen MLX5_CAP_GEN(dev->mdev, uar_4k) ? "yes" : "no", 1624b037c29aSEli Cohen lib_uar_4k ? "yes" : "no", ref_bfregs, 162531a78a5aSYishai Hadas req->total_num_bfregs, bfregi->total_num_bfregs, 162631a78a5aSYishai Hadas bfregi->num_sys_pages); 1627b037c29aSEli Cohen 1628b037c29aSEli Cohen return 0; 1629b037c29aSEli Cohen } 1630b037c29aSEli Cohen 1631b037c29aSEli Cohen static int allocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context) 1632b037c29aSEli Cohen { 1633b037c29aSEli Cohen struct mlx5_bfreg_info *bfregi; 1634b037c29aSEli Cohen int err; 1635b037c29aSEli Cohen int i; 1636b037c29aSEli Cohen 1637b037c29aSEli Cohen bfregi = &context->bfregi; 163831a78a5aSYishai Hadas for (i = 0; i < bfregi->num_static_sys_pages; i++) { 1639d2c8a155SMeir Lichtinger err = mlx5_cmd_uar_alloc(dev->mdev, &bfregi->sys_pages[i], 1640d2c8a155SMeir Lichtinger context->devx_uid); 1641b037c29aSEli Cohen if (err) 1642b037c29aSEli Cohen goto error; 1643b037c29aSEli Cohen 1644b037c29aSEli Cohen mlx5_ib_dbg(dev, "allocated uar %d\n", bfregi->sys_pages[i]); 1645b037c29aSEli Cohen } 16464ed131d0SYishai Hadas 16474ed131d0SYishai Hadas for (i = bfregi->num_static_sys_pages; i < bfregi->num_sys_pages; i++) 16484ed131d0SYishai Hadas bfregi->sys_pages[i] = MLX5_IB_INVALID_UAR_INDEX; 16494ed131d0SYishai Hadas 1650b037c29aSEli Cohen return 0; 1651b037c29aSEli Cohen 1652b037c29aSEli Cohen error: 1653b037c29aSEli Cohen for (--i; i >= 0; i--) 1654d2c8a155SMeir Lichtinger if (mlx5_cmd_uar_dealloc(dev->mdev, bfregi->sys_pages[i], 1655d2c8a155SMeir Lichtinger context->devx_uid)) 1656b037c29aSEli Cohen mlx5_ib_warn(dev, "failed to free uar %d\n", i); 1657b037c29aSEli Cohen 1658b037c29aSEli Cohen return err; 1659b037c29aSEli Cohen } 1660b037c29aSEli Cohen 166115177999SLeon Romanovsky static void deallocate_uars(struct mlx5_ib_dev *dev, 166215177999SLeon Romanovsky struct mlx5_ib_ucontext *context) 1663b037c29aSEli Cohen { 1664b037c29aSEli Cohen struct mlx5_bfreg_info *bfregi; 1665b037c29aSEli Cohen int i; 1666b037c29aSEli Cohen 1667b037c29aSEli Cohen bfregi = &context->bfregi; 166815177999SLeon Romanovsky for (i = 0; i < bfregi->num_sys_pages; i++) 16694ed131d0SYishai Hadas if (i < bfregi->num_static_sys_pages || 167015177999SLeon Romanovsky bfregi->sys_pages[i] != MLX5_IB_INVALID_UAR_INDEX) 1671d2c8a155SMeir Lichtinger mlx5_cmd_uar_dealloc(dev->mdev, bfregi->sys_pages[i], 1672d2c8a155SMeir Lichtinger context->devx_uid); 1673b037c29aSEli Cohen } 1674b037c29aSEli Cohen 16750042f9e4SMark Bloch int mlx5_ib_enable_lb(struct mlx5_ib_dev *dev, bool td, bool qp) 1676a560f1d9SMark Bloch { 1677a560f1d9SMark Bloch int err = 0; 1678a560f1d9SMark Bloch 1679a560f1d9SMark Bloch mutex_lock(&dev->lb.mutex); 16800042f9e4SMark Bloch if (td) 1681a560f1d9SMark Bloch dev->lb.user_td++; 16820042f9e4SMark Bloch if (qp) 16830042f9e4SMark Bloch dev->lb.qps++; 1684a560f1d9SMark Bloch 16850042f9e4SMark Bloch if (dev->lb.user_td == 2 || 16860042f9e4SMark Bloch dev->lb.qps == 1) { 16870042f9e4SMark Bloch if (!dev->lb.enabled) { 1688a560f1d9SMark Bloch err = mlx5_nic_vport_update_local_lb(dev->mdev, true); 16890042f9e4SMark Bloch dev->lb.enabled = true; 16900042f9e4SMark Bloch } 16910042f9e4SMark Bloch } 1692a560f1d9SMark Bloch 1693a560f1d9SMark Bloch mutex_unlock(&dev->lb.mutex); 1694a560f1d9SMark Bloch 1695a560f1d9SMark Bloch return err; 1696a560f1d9SMark Bloch } 1697a560f1d9SMark Bloch 16980042f9e4SMark Bloch void mlx5_ib_disable_lb(struct mlx5_ib_dev *dev, bool td, bool qp) 1699a560f1d9SMark Bloch { 1700a560f1d9SMark Bloch mutex_lock(&dev->lb.mutex); 17010042f9e4SMark Bloch if (td) 1702a560f1d9SMark Bloch dev->lb.user_td--; 17030042f9e4SMark Bloch if (qp) 17040042f9e4SMark Bloch dev->lb.qps--; 1705a560f1d9SMark Bloch 17060042f9e4SMark Bloch if (dev->lb.user_td == 1 && 17070042f9e4SMark Bloch dev->lb.qps == 0) { 17080042f9e4SMark Bloch if (dev->lb.enabled) { 1709a560f1d9SMark Bloch mlx5_nic_vport_update_local_lb(dev->mdev, false); 17100042f9e4SMark Bloch dev->lb.enabled = false; 17110042f9e4SMark Bloch } 17120042f9e4SMark Bloch } 1713a560f1d9SMark Bloch 1714a560f1d9SMark Bloch mutex_unlock(&dev->lb.mutex); 1715a560f1d9SMark Bloch } 1716a560f1d9SMark Bloch 1717d2d19121SYishai Hadas static int mlx5_ib_alloc_transport_domain(struct mlx5_ib_dev *dev, u32 *tdn, 1718d2d19121SYishai Hadas u16 uid) 1719c85023e1SHuy Nguyen { 1720c85023e1SHuy Nguyen int err; 1721c85023e1SHuy Nguyen 1722cfdeb893SLeon Romanovsky if (!MLX5_CAP_GEN(dev->mdev, log_max_transport_domain)) 1723cfdeb893SLeon Romanovsky return 0; 1724cfdeb893SLeon Romanovsky 1725d2d19121SYishai Hadas err = mlx5_cmd_alloc_transport_domain(dev->mdev, tdn, uid); 1726c85023e1SHuy Nguyen if (err) 1727c85023e1SHuy Nguyen return err; 1728c85023e1SHuy Nguyen 1729c85023e1SHuy Nguyen if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) || 17308978cc92SEran Ben Elisha (!MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) && 17318978cc92SEran Ben Elisha !MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc))) 1732c85023e1SHuy Nguyen return err; 1733c85023e1SHuy Nguyen 17340042f9e4SMark Bloch return mlx5_ib_enable_lb(dev, true, false); 1735c85023e1SHuy Nguyen } 1736c85023e1SHuy Nguyen 1737d2d19121SYishai Hadas static void mlx5_ib_dealloc_transport_domain(struct mlx5_ib_dev *dev, u32 tdn, 1738d2d19121SYishai Hadas u16 uid) 1739c85023e1SHuy Nguyen { 1740cfdeb893SLeon Romanovsky if (!MLX5_CAP_GEN(dev->mdev, log_max_transport_domain)) 1741cfdeb893SLeon Romanovsky return; 1742cfdeb893SLeon Romanovsky 1743d2d19121SYishai Hadas mlx5_cmd_dealloc_transport_domain(dev->mdev, tdn, uid); 1744c85023e1SHuy Nguyen 1745c85023e1SHuy Nguyen if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) || 17468978cc92SEran Ben Elisha (!MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) && 17478978cc92SEran Ben Elisha !MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc))) 1748c85023e1SHuy Nguyen return; 1749c85023e1SHuy Nguyen 17500042f9e4SMark Bloch mlx5_ib_disable_lb(dev, true, false); 1751c85023e1SHuy Nguyen } 1752c85023e1SHuy Nguyen 175345ec21c9SYishai Hadas static int set_ucontext_resp(struct ib_ucontext *uctx, 175445ec21c9SYishai Hadas struct mlx5_ib_alloc_ucontext_resp *resp) 175545ec21c9SYishai Hadas { 175645ec21c9SYishai Hadas struct ib_device *ibdev = uctx->device; 175745ec21c9SYishai Hadas struct mlx5_ib_dev *dev = to_mdev(ibdev); 175845ec21c9SYishai Hadas struct mlx5_ib_ucontext *context = to_mucontext(uctx); 175945ec21c9SYishai Hadas struct mlx5_bfreg_info *bfregi = &context->bfregi; 176045ec21c9SYishai Hadas int err; 176145ec21c9SYishai Hadas 176245ec21c9SYishai Hadas if (MLX5_CAP_GEN(dev->mdev, dump_fill_mkey)) { 176345ec21c9SYishai Hadas err = mlx5_cmd_dump_fill_mkey(dev->mdev, 176445ec21c9SYishai Hadas &resp->dump_fill_mkey); 176545ec21c9SYishai Hadas if (err) 176645ec21c9SYishai Hadas return err; 176745ec21c9SYishai Hadas resp->comp_mask |= 176845ec21c9SYishai Hadas MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_DUMP_FILL_MKEY; 176945ec21c9SYishai Hadas } 177045ec21c9SYishai Hadas 177145ec21c9SYishai Hadas resp->qp_tab_size = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp); 177245ec21c9SYishai Hadas if (dev->wc_support) 177345ec21c9SYishai Hadas resp->bf_reg_size = 1 << MLX5_CAP_GEN(dev->mdev, 177445ec21c9SYishai Hadas log_bf_reg_size); 177545ec21c9SYishai Hadas resp->cache_line_size = cache_line_size(); 177645ec21c9SYishai Hadas resp->max_sq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq); 177745ec21c9SYishai Hadas resp->max_rq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq); 177845ec21c9SYishai Hadas resp->max_send_wqebb = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz); 177945ec21c9SYishai Hadas resp->max_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz); 178045ec21c9SYishai Hadas resp->max_srq_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz); 178145ec21c9SYishai Hadas resp->cqe_version = context->cqe_version; 178245ec21c9SYishai Hadas resp->log_uar_size = MLX5_CAP_GEN(dev->mdev, uar_4k) ? 178345ec21c9SYishai Hadas MLX5_ADAPTER_PAGE_SHIFT : PAGE_SHIFT; 178445ec21c9SYishai Hadas resp->num_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ? 178545ec21c9SYishai Hadas MLX5_CAP_GEN(dev->mdev, 178645ec21c9SYishai Hadas num_of_uars_per_page) : 1; 178745ec21c9SYishai Hadas resp->tot_bfregs = bfregi->lib_uar_dyn ? 0 : 178845ec21c9SYishai Hadas bfregi->total_num_bfregs - bfregi->num_dyn_bfregs; 178945ec21c9SYishai Hadas resp->num_ports = dev->num_ports; 179045ec21c9SYishai Hadas resp->cmds_supp_uhw |= MLX5_USER_CMDS_SUPP_UHW_QUERY_DEVICE | 179145ec21c9SYishai Hadas MLX5_USER_CMDS_SUPP_UHW_CREATE_AH; 179245ec21c9SYishai Hadas 179345ec21c9SYishai Hadas if (mlx5_ib_port_link_layer(ibdev, 1) == IB_LINK_LAYER_ETHERNET) { 179445ec21c9SYishai Hadas mlx5_query_min_inline(dev->mdev, &resp->eth_min_inline); 179545ec21c9SYishai Hadas resp->eth_min_inline++; 179645ec21c9SYishai Hadas } 179745ec21c9SYishai Hadas 179845ec21c9SYishai Hadas if (dev->mdev->clock_info) 179945ec21c9SYishai Hadas resp->clock_info_versions = BIT(MLX5_IB_CLOCK_INFO_V1); 180045ec21c9SYishai Hadas 180145ec21c9SYishai Hadas /* 180245ec21c9SYishai Hadas * We don't want to expose information from the PCI bar that is located 180345ec21c9SYishai Hadas * after 4096 bytes, so if the arch only supports larger pages, let's 180445ec21c9SYishai Hadas * pretend we don't support reading the HCA's core clock. This is also 180545ec21c9SYishai Hadas * forced by mmap function. 180645ec21c9SYishai Hadas */ 180745ec21c9SYishai Hadas if (PAGE_SIZE <= 4096) { 180845ec21c9SYishai Hadas resp->comp_mask |= 180945ec21c9SYishai Hadas MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_CORE_CLOCK_OFFSET; 181045ec21c9SYishai Hadas resp->hca_core_clock_offset = 181145ec21c9SYishai Hadas offsetof(struct mlx5_init_seg, 181245ec21c9SYishai Hadas internal_timer_h) % PAGE_SIZE; 181345ec21c9SYishai Hadas } 181445ec21c9SYishai Hadas 181545ec21c9SYishai Hadas if (MLX5_CAP_GEN(dev->mdev, ece_support)) 181645ec21c9SYishai Hadas resp->comp_mask |= MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_ECE; 181745ec21c9SYishai Hadas 181833652951SAharon Landau if (rt_supported(MLX5_CAP_GEN(dev->mdev, sq_ts_format)) && 181933652951SAharon Landau rt_supported(MLX5_CAP_GEN(dev->mdev, rq_ts_format)) && 182033652951SAharon Landau rt_supported(MLX5_CAP_ROCE(dev->mdev, qp_ts_format))) 182133652951SAharon Landau resp->comp_mask |= 182233652951SAharon Landau MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_REAL_TIME_TS; 182333652951SAharon Landau 182445ec21c9SYishai Hadas resp->num_dyn_bfregs = bfregi->num_dyn_bfregs; 1825c906b86eSSergey Gorenko 1826c906b86eSSergey Gorenko if (MLX5_CAP_GEN(dev->mdev, drain_sigerr)) 1827c906b86eSSergey Gorenko resp->comp_mask |= MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_SQD2RTS; 1828c906b86eSSergey Gorenko 182945ec21c9SYishai Hadas return 0; 183045ec21c9SYishai Hadas } 183145ec21c9SYishai Hadas 1832a2a074efSLeon Romanovsky static int mlx5_ib_alloc_ucontext(struct ib_ucontext *uctx, 1833e126ba97SEli Cohen struct ib_udata *udata) 1834e126ba97SEli Cohen { 1835a2a074efSLeon Romanovsky struct ib_device *ibdev = uctx->device; 1836e126ba97SEli Cohen struct mlx5_ib_dev *dev = to_mdev(ibdev); 1837b368d7cbSMatan Barak struct mlx5_ib_alloc_ucontext_req_v2 req = {}; 1838b368d7cbSMatan Barak struct mlx5_ib_alloc_ucontext_resp resp = {}; 1839a2a074efSLeon Romanovsky struct mlx5_ib_ucontext *context = to_mucontext(uctx); 18402f5ff264SEli Cohen struct mlx5_bfreg_info *bfregi; 184178c0f98cSEli Cohen int ver; 1842e126ba97SEli Cohen int err; 1843a168a41cSMajd Dibbiny size_t min_req_v2 = offsetof(struct mlx5_ib_alloc_ucontext_req_v2, 1844a168a41cSMajd Dibbiny max_cqe_version); 1845b037c29aSEli Cohen bool lib_uar_4k; 18460a2fd01cSYishai Hadas bool lib_uar_dyn; 1847e126ba97SEli Cohen 1848e126ba97SEli Cohen if (!dev->ib_active) 1849a2a074efSLeon Romanovsky return -EAGAIN; 1850e126ba97SEli Cohen 1851e093111dSAmrani, Ram if (udata->inlen == sizeof(struct mlx5_ib_alloc_ucontext_req)) 185278c0f98cSEli Cohen ver = 0; 1853e093111dSAmrani, Ram else if (udata->inlen >= min_req_v2) 185478c0f98cSEli Cohen ver = 2; 185578c0f98cSEli Cohen else 1856a2a074efSLeon Romanovsky return -EINVAL; 185778c0f98cSEli Cohen 1858e093111dSAmrani, Ram err = ib_copy_from_udata(&req, udata, min(udata->inlen, sizeof(req))); 1859e126ba97SEli Cohen if (err) 1860a2a074efSLeon Romanovsky return err; 1861e126ba97SEli Cohen 1862a8b92ca1SYishai Hadas if (req.flags & ~MLX5_IB_ALLOC_UCTX_DEVX) 1863a2a074efSLeon Romanovsky return -EOPNOTSUPP; 186478c0f98cSEli Cohen 1865f72300c5SHaggai Abramovsky if (req.comp_mask || req.reserved0 || req.reserved1 || req.reserved2) 1866a2a074efSLeon Romanovsky return -EOPNOTSUPP; 1867b368d7cbSMatan Barak 18682f5ff264SEli Cohen req.total_num_bfregs = ALIGN(req.total_num_bfregs, 18692f5ff264SEli Cohen MLX5_NON_FP_BFREGS_PER_UAR); 18702f5ff264SEli Cohen if (req.num_low_latency_bfregs > req.total_num_bfregs - 1) 1871a2a074efSLeon Romanovsky return -EINVAL; 1872e126ba97SEli Cohen 1873d2c8a155SMeir Lichtinger if (req.flags & MLX5_IB_ALLOC_UCTX_DEVX) { 1874d2c8a155SMeir Lichtinger err = mlx5_ib_devx_create(dev, true); 1875d2c8a155SMeir Lichtinger if (err < 0) 1876d2c8a155SMeir Lichtinger goto out_ctx; 1877d2c8a155SMeir Lichtinger context->devx_uid = err; 1878d2c8a155SMeir Lichtinger } 1879d2c8a155SMeir Lichtinger 188030aa60b3SEli Cohen lib_uar_4k = req.lib_caps & MLX5_LIB_CAP_4K_UAR; 18810a2fd01cSYishai Hadas lib_uar_dyn = req.lib_caps & MLX5_LIB_CAP_DYN_UAR; 18822f5ff264SEli Cohen bfregi = &context->bfregi; 1883b037c29aSEli Cohen 18840a2fd01cSYishai Hadas if (lib_uar_dyn) { 18850a2fd01cSYishai Hadas bfregi->lib_uar_dyn = lib_uar_dyn; 18860a2fd01cSYishai Hadas goto uar_done; 18870a2fd01cSYishai Hadas } 18880a2fd01cSYishai Hadas 1889b037c29aSEli Cohen /* updates req->total_num_bfregs */ 189031a78a5aSYishai Hadas err = calc_total_bfregs(dev, lib_uar_4k, &req, bfregi); 1891b037c29aSEli Cohen if (err) 1892d2c8a155SMeir Lichtinger goto out_devx; 1893b037c29aSEli Cohen 18942f5ff264SEli Cohen mutex_init(&bfregi->lock); 1895b037c29aSEli Cohen bfregi->lib_uar_4k = lib_uar_4k; 189631a78a5aSYishai Hadas bfregi->count = kcalloc(bfregi->total_num_bfregs, sizeof(*bfregi->count), 1897b037c29aSEli Cohen GFP_KERNEL); 1898b037c29aSEli Cohen if (!bfregi->count) { 1899e126ba97SEli Cohen err = -ENOMEM; 1900d2c8a155SMeir Lichtinger goto out_devx; 1901e126ba97SEli Cohen } 1902e126ba97SEli Cohen 1903b037c29aSEli Cohen bfregi->sys_pages = kcalloc(bfregi->num_sys_pages, 1904b037c29aSEli Cohen sizeof(*bfregi->sys_pages), 1905e126ba97SEli Cohen GFP_KERNEL); 1906b037c29aSEli Cohen if (!bfregi->sys_pages) { 1907e126ba97SEli Cohen err = -ENOMEM; 1908e126ba97SEli Cohen goto out_count; 1909e126ba97SEli Cohen } 1910e126ba97SEli Cohen 1911b037c29aSEli Cohen err = allocate_uars(dev, context); 1912b037c29aSEli Cohen if (err) 1913b037c29aSEli Cohen goto out_sys_pages; 1914b037c29aSEli Cohen 19150a2fd01cSYishai Hadas uar_done: 1916d2d19121SYishai Hadas err = mlx5_ib_alloc_transport_domain(dev, &context->tdn, 1917d2d19121SYishai Hadas context->devx_uid); 1918a8b92ca1SYishai Hadas if (err) 1919d2c8a155SMeir Lichtinger goto out_uars; 1920a8b92ca1SYishai Hadas 1921e126ba97SEli Cohen INIT_LIST_HEAD(&context->db_page_list); 1922e126ba97SEli Cohen mutex_init(&context->db_page_mutex); 1923e126ba97SEli Cohen 192445ec21c9SYishai Hadas context->cqe_version = min_t(__u8, 192545ec21c9SYishai Hadas (__u8)MLX5_CAP_GEN(dev->mdev, cqe_version), 192645ec21c9SYishai Hadas req.max_cqe_version); 1927b368d7cbSMatan Barak 192845ec21c9SYishai Hadas err = set_ucontext_resp(uctx, &resp); 192945ec21c9SYishai Hadas if (err) 193045ec21c9SYishai Hadas goto out_mdev; 1931b368d7cbSMatan Barak 193245ec21c9SYishai Hadas resp.response_length = min(udata->outlen, sizeof(resp)); 1933b368d7cbSMatan Barak err = ib_copy_to_udata(udata, &resp, resp.response_length); 1934e126ba97SEli Cohen if (err) 1935a8b92ca1SYishai Hadas goto out_mdev; 1936e126ba97SEli Cohen 19372f5ff264SEli Cohen bfregi->ver = ver; 19382f5ff264SEli Cohen bfregi->num_low_latency_bfregs = req.num_low_latency_bfregs; 193930aa60b3SEli Cohen context->lib_caps = req.lib_caps; 194030aa60b3SEli Cohen print_lib_caps(dev, context->lib_caps); 1941f72300c5SHaggai Abramovsky 1942802dcc7fSMark Zhang if (mlx5_ib_lag_should_assign_affinity(dev)) { 19431fb7f897SMark Bloch u32 port = mlx5_core_native_port_num(dev->mdev) - 1; 1944c6a21c38SMajd Dibbiny 1945c6a21c38SMajd Dibbiny atomic_set(&context->tx_port_affinity, 1946c6a21c38SMajd Dibbiny atomic_add_return( 194795579e78SMark Bloch 1, &dev->port[port].roce.tx_port_affinity)); 1948c6a21c38SMajd Dibbiny } 1949c6a21c38SMajd Dibbiny 1950a2a074efSLeon Romanovsky return 0; 1951e126ba97SEli Cohen 1952a8b92ca1SYishai Hadas out_mdev: 1953d2d19121SYishai Hadas mlx5_ib_dealloc_transport_domain(dev, context->tdn, context->devx_uid); 1954146d2f1aSmajd@mellanox.com 1955e126ba97SEli Cohen out_uars: 1956b037c29aSEli Cohen deallocate_uars(dev, context); 1957b037c29aSEli Cohen 1958b037c29aSEli Cohen out_sys_pages: 1959b037c29aSEli Cohen kfree(bfregi->sys_pages); 1960b037c29aSEli Cohen 1961e126ba97SEli Cohen out_count: 19622f5ff264SEli Cohen kfree(bfregi->count); 1963e126ba97SEli Cohen 1964d2c8a155SMeir Lichtinger out_devx: 1965d2c8a155SMeir Lichtinger if (req.flags & MLX5_IB_ALLOC_UCTX_DEVX) 1966d2c8a155SMeir Lichtinger mlx5_ib_devx_destroy(dev, context->devx_uid); 1967d2c8a155SMeir Lichtinger 1968e126ba97SEli Cohen out_ctx: 1969a2a074efSLeon Romanovsky return err; 1970e126ba97SEli Cohen } 1971e126ba97SEli Cohen 19720fb556b2SYishai Hadas static int mlx5_ib_query_ucontext(struct ib_ucontext *ibcontext, 19730fb556b2SYishai Hadas struct uverbs_attr_bundle *attrs) 19740fb556b2SYishai Hadas { 19750fb556b2SYishai Hadas struct mlx5_ib_alloc_ucontext_resp uctx_resp = {}; 19760fb556b2SYishai Hadas int ret; 19770fb556b2SYishai Hadas 19780fb556b2SYishai Hadas ret = set_ucontext_resp(ibcontext, &uctx_resp); 19790fb556b2SYishai Hadas if (ret) 19800fb556b2SYishai Hadas return ret; 19810fb556b2SYishai Hadas 19820fb556b2SYishai Hadas uctx_resp.response_length = 19830fb556b2SYishai Hadas min_t(size_t, 19840fb556b2SYishai Hadas uverbs_attr_get_len(attrs, 19850fb556b2SYishai Hadas MLX5_IB_ATTR_QUERY_CONTEXT_RESP_UCTX), 19860fb556b2SYishai Hadas sizeof(uctx_resp)); 19870fb556b2SYishai Hadas 19880fb556b2SYishai Hadas ret = uverbs_copy_to_struct_or_zero(attrs, 19890fb556b2SYishai Hadas MLX5_IB_ATTR_QUERY_CONTEXT_RESP_UCTX, 19900fb556b2SYishai Hadas &uctx_resp, 19910fb556b2SYishai Hadas sizeof(uctx_resp)); 19920fb556b2SYishai Hadas return ret; 19930fb556b2SYishai Hadas } 19940fb556b2SYishai Hadas 1995a2a074efSLeon Romanovsky static void mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext) 1996e126ba97SEli Cohen { 1997e126ba97SEli Cohen struct mlx5_ib_ucontext *context = to_mucontext(ibcontext); 1998e126ba97SEli Cohen struct mlx5_ib_dev *dev = to_mdev(ibcontext->device); 1999b037c29aSEli Cohen struct mlx5_bfreg_info *bfregi; 2000e126ba97SEli Cohen 2001b037c29aSEli Cohen bfregi = &context->bfregi; 2002d2d19121SYishai Hadas mlx5_ib_dealloc_transport_domain(dev, context->tdn, context->devx_uid); 2003d2d19121SYishai Hadas 2004b037c29aSEli Cohen deallocate_uars(dev, context); 2005b037c29aSEli Cohen kfree(bfregi->sys_pages); 20062f5ff264SEli Cohen kfree(bfregi->count); 2007d2c8a155SMeir Lichtinger 2008d2c8a155SMeir Lichtinger if (context->devx_uid) 2009d2c8a155SMeir Lichtinger mlx5_ib_devx_destroy(dev, context->devx_uid); 2010e126ba97SEli Cohen } 2011e126ba97SEli Cohen 2012b037c29aSEli Cohen static phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev, 20134ed131d0SYishai Hadas int uar_idx) 2014e126ba97SEli Cohen { 2015b037c29aSEli Cohen int fw_uars_per_page; 2016b037c29aSEli Cohen 2017b037c29aSEli Cohen fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ? MLX5_UARS_IN_PAGE : 1; 2018b037c29aSEli Cohen 2019aa8106f1SHuy Nguyen return (dev->mdev->bar_addr >> PAGE_SHIFT) + uar_idx / fw_uars_per_page; 2020e126ba97SEli Cohen } 2021e126ba97SEli Cohen 2022342ee59dSYishai Hadas static u64 uar_index2paddress(struct mlx5_ib_dev *dev, 2023342ee59dSYishai Hadas int uar_idx) 2024342ee59dSYishai Hadas { 2025342ee59dSYishai Hadas unsigned int fw_uars_per_page; 2026342ee59dSYishai Hadas 2027342ee59dSYishai Hadas fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ? 2028342ee59dSYishai Hadas MLX5_UARS_IN_PAGE : 1; 2029342ee59dSYishai Hadas 2030342ee59dSYishai Hadas return (dev->mdev->bar_addr + (uar_idx / fw_uars_per_page) * PAGE_SIZE); 2031342ee59dSYishai Hadas } 2032342ee59dSYishai Hadas 2033e126ba97SEli Cohen static int get_command(unsigned long offset) 2034e126ba97SEli Cohen { 2035e126ba97SEli Cohen return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK; 2036e126ba97SEli Cohen } 2037e126ba97SEli Cohen 2038e126ba97SEli Cohen static int get_arg(unsigned long offset) 2039e126ba97SEli Cohen { 2040e126ba97SEli Cohen return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1); 2041e126ba97SEli Cohen } 2042e126ba97SEli Cohen 2043e126ba97SEli Cohen static int get_index(unsigned long offset) 2044e126ba97SEli Cohen { 2045e126ba97SEli Cohen return get_arg(offset); 2046e126ba97SEli Cohen } 2047e126ba97SEli Cohen 20484ed131d0SYishai Hadas /* Index resides in an extra byte to enable larger values than 255 */ 20494ed131d0SYishai Hadas static int get_extended_index(unsigned long offset) 20504ed131d0SYishai Hadas { 20514ed131d0SYishai Hadas return get_arg(offset) | ((offset >> 16) & 0xff) << 8; 20524ed131d0SYishai Hadas } 20534ed131d0SYishai Hadas 20547c2344c3SMaor Gottlieb 20557c2344c3SMaor Gottlieb static void mlx5_ib_disassociate_ucontext(struct ib_ucontext *ibcontext) 20567c2344c3SMaor Gottlieb { 20577c2344c3SMaor Gottlieb } 20587c2344c3SMaor Gottlieb 205937aa5c36SGuy Levi static inline char *mmap_cmd2str(enum mlx5_ib_mmap_cmd cmd) 2060e126ba97SEli Cohen { 206137aa5c36SGuy Levi switch (cmd) { 206237aa5c36SGuy Levi case MLX5_IB_MMAP_WC_PAGE: 206337aa5c36SGuy Levi return "WC"; 2064e126ba97SEli Cohen case MLX5_IB_MMAP_REGULAR_PAGE: 206537aa5c36SGuy Levi return "best effort WC"; 206637aa5c36SGuy Levi case MLX5_IB_MMAP_NC_PAGE: 206737aa5c36SGuy Levi return "NC"; 206824da0016SAriel Levkovich case MLX5_IB_MMAP_DEVICE_MEM: 206924da0016SAriel Levkovich return "Device Memory"; 207037aa5c36SGuy Levi default: 207137aa5c36SGuy Levi return NULL; 207237aa5c36SGuy Levi } 207337aa5c36SGuy Levi } 207437aa5c36SGuy Levi 20755c99eaecSFeras Daoud static int mlx5_ib_mmap_clock_info_page(struct mlx5_ib_dev *dev, 20765c99eaecSFeras Daoud struct vm_area_struct *vma, 20775c99eaecSFeras Daoud struct mlx5_ib_ucontext *context) 20785c99eaecSFeras Daoud { 20794eb6ab13SJason Gunthorpe if ((vma->vm_end - vma->vm_start != PAGE_SIZE) || 20804eb6ab13SJason Gunthorpe !(vma->vm_flags & VM_SHARED)) 20815c99eaecSFeras Daoud return -EINVAL; 20825c99eaecSFeras Daoud 20835c99eaecSFeras Daoud if (get_index(vma->vm_pgoff) != MLX5_IB_CLOCK_INFO_V1) 20845c99eaecSFeras Daoud return -EOPNOTSUPP; 20855c99eaecSFeras Daoud 20864eb6ab13SJason Gunthorpe if (vma->vm_flags & (VM_WRITE | VM_EXEC)) 20875c99eaecSFeras Daoud return -EPERM; 2088c660133cSJason Gunthorpe vma->vm_flags &= ~VM_MAYWRITE; 20895c99eaecSFeras Daoud 2090ddcdc368SJason Gunthorpe if (!dev->mdev->clock_info) 20915c99eaecSFeras Daoud return -EOPNOTSUPP; 20925c99eaecSFeras Daoud 20934eb6ab13SJason Gunthorpe return vm_insert_page(vma, vma->vm_start, 20944eb6ab13SJason Gunthorpe virt_to_page(dev->mdev->clock_info)); 20955c99eaecSFeras Daoud } 20965c99eaecSFeras Daoud 2097dc2316ebSYishai Hadas static void mlx5_ib_mmap_free(struct rdma_user_mmap_entry *entry) 2098dc2316ebSYishai Hadas { 2099dc2316ebSYishai Hadas struct mlx5_user_mmap_entry *mentry = to_mmmap(entry); 2100dc2316ebSYishai Hadas struct mlx5_ib_dev *dev = to_mdev(entry->ucontext->device); 21017be76befSYishai Hadas struct mlx5_var_table *var_table = &dev->var_table; 2102d2c8a155SMeir Lichtinger struct mlx5_ib_ucontext *context = to_mucontext(entry->ucontext); 2103dc2316ebSYishai Hadas 2104dc2316ebSYishai Hadas switch (mentry->mmap_flag) { 2105dc2316ebSYishai Hadas case MLX5_IB_MMAP_TYPE_MEMIC: 2106cea85fa5SMaor Gottlieb case MLX5_IB_MMAP_TYPE_MEMIC_OP: 2107cea85fa5SMaor Gottlieb mlx5_ib_dm_mmap_free(dev, mentry); 2108dc2316ebSYishai Hadas break; 21097be76befSYishai Hadas case MLX5_IB_MMAP_TYPE_VAR: 21107be76befSYishai Hadas mutex_lock(&var_table->bitmap_lock); 21117be76befSYishai Hadas clear_bit(mentry->page_idx, var_table->bitmap); 21127be76befSYishai Hadas mutex_unlock(&var_table->bitmap_lock); 21137be76befSYishai Hadas kfree(mentry); 21147be76befSYishai Hadas break; 2115342ee59dSYishai Hadas case MLX5_IB_MMAP_TYPE_UAR_WC: 2116342ee59dSYishai Hadas case MLX5_IB_MMAP_TYPE_UAR_NC: 2117d2c8a155SMeir Lichtinger mlx5_cmd_uar_dealloc(dev->mdev, mentry->page_idx, 2118d2c8a155SMeir Lichtinger context->devx_uid); 2119342ee59dSYishai Hadas kfree(mentry); 2120342ee59dSYishai Hadas break; 2121dc2316ebSYishai Hadas default: 2122dc2316ebSYishai Hadas WARN_ON(true); 2123dc2316ebSYishai Hadas } 2124dc2316ebSYishai Hadas } 2125dc2316ebSYishai Hadas 212637aa5c36SGuy Levi static int uar_mmap(struct mlx5_ib_dev *dev, enum mlx5_ib_mmap_cmd cmd, 21277c2344c3SMaor Gottlieb struct vm_area_struct *vma, 21287c2344c3SMaor Gottlieb struct mlx5_ib_ucontext *context) 212937aa5c36SGuy Levi { 21302f5ff264SEli Cohen struct mlx5_bfreg_info *bfregi = &context->bfregi; 213137aa5c36SGuy Levi int err; 213237aa5c36SGuy Levi unsigned long idx; 2133aa09ea6eSKamal Heib phys_addr_t pfn; 213437aa5c36SGuy Levi pgprot_t prot; 21354ed131d0SYishai Hadas u32 bfreg_dyn_idx = 0; 21364ed131d0SYishai Hadas u32 uar_index; 21374ed131d0SYishai Hadas int dyn_uar = (cmd == MLX5_IB_MMAP_ALLOC_WC); 21384ed131d0SYishai Hadas int max_valid_idx = dyn_uar ? bfregi->num_sys_pages : 21394ed131d0SYishai Hadas bfregi->num_static_sys_pages; 2140b037c29aSEli Cohen 21410a2fd01cSYishai Hadas if (bfregi->lib_uar_dyn) 21420a2fd01cSYishai Hadas return -EINVAL; 21430a2fd01cSYishai Hadas 2144b037c29aSEli Cohen if (vma->vm_end - vma->vm_start != PAGE_SIZE) 2145b037c29aSEli Cohen return -EINVAL; 2146b037c29aSEli Cohen 21474ed131d0SYishai Hadas if (dyn_uar) 21484ed131d0SYishai Hadas idx = get_extended_index(vma->vm_pgoff) + bfregi->num_static_sys_pages; 21494ed131d0SYishai Hadas else 2150b037c29aSEli Cohen idx = get_index(vma->vm_pgoff); 21514ed131d0SYishai Hadas 21524ed131d0SYishai Hadas if (idx >= max_valid_idx) { 21534ed131d0SYishai Hadas mlx5_ib_warn(dev, "invalid uar index %lu, max=%d\n", 21544ed131d0SYishai Hadas idx, max_valid_idx); 2155b037c29aSEli Cohen return -EINVAL; 2156b037c29aSEli Cohen } 215737aa5c36SGuy Levi 215837aa5c36SGuy Levi switch (cmd) { 215937aa5c36SGuy Levi case MLX5_IB_MMAP_WC_PAGE: 21604ed131d0SYishai Hadas case MLX5_IB_MMAP_ALLOC_WC: 216137aa5c36SGuy Levi case MLX5_IB_MMAP_REGULAR_PAGE: 216237aa5c36SGuy Levi /* For MLX5_IB_MMAP_REGULAR_PAGE do the best effort to get WC */ 216337aa5c36SGuy Levi prot = pgprot_writecombine(vma->vm_page_prot); 216437aa5c36SGuy Levi break; 216537aa5c36SGuy Levi case MLX5_IB_MMAP_NC_PAGE: 216637aa5c36SGuy Levi prot = pgprot_noncached(vma->vm_page_prot); 216737aa5c36SGuy Levi break; 216837aa5c36SGuy Levi default: 216937aa5c36SGuy Levi return -EINVAL; 217037aa5c36SGuy Levi } 217137aa5c36SGuy Levi 21724ed131d0SYishai Hadas if (dyn_uar) { 21734ed131d0SYishai Hadas int uars_per_page; 21744ed131d0SYishai Hadas 21754ed131d0SYishai Hadas uars_per_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k); 21764ed131d0SYishai Hadas bfreg_dyn_idx = idx * (uars_per_page * MLX5_NON_FP_BFREGS_PER_UAR); 21774ed131d0SYishai Hadas if (bfreg_dyn_idx >= bfregi->total_num_bfregs) { 21784ed131d0SYishai Hadas mlx5_ib_warn(dev, "invalid bfreg_dyn_idx %u, max=%u\n", 21794ed131d0SYishai Hadas bfreg_dyn_idx, bfregi->total_num_bfregs); 21804ed131d0SYishai Hadas return -EINVAL; 21814ed131d0SYishai Hadas } 21824ed131d0SYishai Hadas 21834ed131d0SYishai Hadas mutex_lock(&bfregi->lock); 21844ed131d0SYishai Hadas /* Fail if uar already allocated, first bfreg index of each 21854ed131d0SYishai Hadas * page holds its count. 21864ed131d0SYishai Hadas */ 21874ed131d0SYishai Hadas if (bfregi->count[bfreg_dyn_idx]) { 21884ed131d0SYishai Hadas mlx5_ib_warn(dev, "wrong offset, idx %lu is busy, bfregn=%u\n", idx, bfreg_dyn_idx); 21894ed131d0SYishai Hadas mutex_unlock(&bfregi->lock); 21904ed131d0SYishai Hadas return -EINVAL; 21914ed131d0SYishai Hadas } 21924ed131d0SYishai Hadas 21934ed131d0SYishai Hadas bfregi->count[bfreg_dyn_idx]++; 21944ed131d0SYishai Hadas mutex_unlock(&bfregi->lock); 21954ed131d0SYishai Hadas 2196d2c8a155SMeir Lichtinger err = mlx5_cmd_uar_alloc(dev->mdev, &uar_index, 2197d2c8a155SMeir Lichtinger context->devx_uid); 21984ed131d0SYishai Hadas if (err) { 21994ed131d0SYishai Hadas mlx5_ib_warn(dev, "UAR alloc failed\n"); 22004ed131d0SYishai Hadas goto free_bfreg; 22014ed131d0SYishai Hadas } 22024ed131d0SYishai Hadas } else { 22034ed131d0SYishai Hadas uar_index = bfregi->sys_pages[idx]; 22044ed131d0SYishai Hadas } 22054ed131d0SYishai Hadas 22064ed131d0SYishai Hadas pfn = uar_index2pfn(dev, uar_index); 220737aa5c36SGuy Levi mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn %pa\n", idx, &pfn); 2208e126ba97SEli Cohen 2209e2cd1d1aSJason Gunthorpe err = rdma_user_mmap_io(&context->ibucontext, vma, pfn, PAGE_SIZE, 2210c043ff2cSMichal Kalderon prot, NULL); 221137aa5c36SGuy Levi if (err) { 22128f062287SLeon Romanovsky mlx5_ib_err(dev, 2213e2cd1d1aSJason Gunthorpe "rdma_user_mmap_io failed with error=%d, mmap_cmd=%s\n", 22148f062287SLeon Romanovsky err, mmap_cmd2str(cmd)); 22154ed131d0SYishai Hadas goto err; 221637aa5c36SGuy Levi } 2217e126ba97SEli Cohen 22184ed131d0SYishai Hadas if (dyn_uar) 22194ed131d0SYishai Hadas bfregi->sys_pages[idx] = uar_index; 22204ed131d0SYishai Hadas return 0; 22214ed131d0SYishai Hadas 22224ed131d0SYishai Hadas err: 22234ed131d0SYishai Hadas if (!dyn_uar) 22244ed131d0SYishai Hadas return err; 22254ed131d0SYishai Hadas 2226d2c8a155SMeir Lichtinger mlx5_cmd_uar_dealloc(dev->mdev, idx, context->devx_uid); 22274ed131d0SYishai Hadas 22284ed131d0SYishai Hadas free_bfreg: 22294ed131d0SYishai Hadas mlx5_ib_free_bfreg(dev, bfregi, bfreg_dyn_idx); 22304ed131d0SYishai Hadas 22314ed131d0SYishai Hadas return err; 223237aa5c36SGuy Levi } 223337aa5c36SGuy Levi 2234dc2316ebSYishai Hadas static unsigned long mlx5_vma_to_pgoff(struct vm_area_struct *vma) 2235dc2316ebSYishai Hadas { 2236dc2316ebSYishai Hadas unsigned long idx; 2237dc2316ebSYishai Hadas u8 command; 2238dc2316ebSYishai Hadas 2239dc2316ebSYishai Hadas command = get_command(vma->vm_pgoff); 2240dc2316ebSYishai Hadas idx = get_extended_index(vma->vm_pgoff); 2241dc2316ebSYishai Hadas 2242dc2316ebSYishai Hadas return (command << 16 | idx); 2243dc2316ebSYishai Hadas } 2244dc2316ebSYishai Hadas 2245dc2316ebSYishai Hadas static int mlx5_ib_mmap_offset(struct mlx5_ib_dev *dev, 2246dc2316ebSYishai Hadas struct vm_area_struct *vma, 2247dc2316ebSYishai Hadas struct ib_ucontext *ucontext) 2248dc2316ebSYishai Hadas { 2249dc2316ebSYishai Hadas struct mlx5_user_mmap_entry *mentry; 2250dc2316ebSYishai Hadas struct rdma_user_mmap_entry *entry; 2251dc2316ebSYishai Hadas unsigned long pgoff; 2252dc2316ebSYishai Hadas pgprot_t prot; 2253dc2316ebSYishai Hadas phys_addr_t pfn; 2254dc2316ebSYishai Hadas int ret; 2255dc2316ebSYishai Hadas 2256dc2316ebSYishai Hadas pgoff = mlx5_vma_to_pgoff(vma); 2257dc2316ebSYishai Hadas entry = rdma_user_mmap_entry_get_pgoff(ucontext, pgoff); 2258dc2316ebSYishai Hadas if (!entry) 225924da0016SAriel Levkovich return -EINVAL; 226024da0016SAriel Levkovich 2261dc2316ebSYishai Hadas mentry = to_mmmap(entry); 2262dc2316ebSYishai Hadas pfn = (mentry->address >> PAGE_SHIFT); 2263342ee59dSYishai Hadas if (mentry->mmap_flag == MLX5_IB_MMAP_TYPE_VAR || 2264342ee59dSYishai Hadas mentry->mmap_flag == MLX5_IB_MMAP_TYPE_UAR_NC) 22653f59b6c3SYishai Hadas prot = pgprot_noncached(vma->vm_page_prot); 22663f59b6c3SYishai Hadas else 2267dc2316ebSYishai Hadas prot = pgprot_writecombine(vma->vm_page_prot); 2268dc2316ebSYishai Hadas ret = rdma_user_mmap_io(ucontext, vma, pfn, 2269dc2316ebSYishai Hadas entry->npages * PAGE_SIZE, 2270dc2316ebSYishai Hadas prot, 2271dc2316ebSYishai Hadas entry); 2272dc2316ebSYishai Hadas rdma_user_mmap_entry_put(&mentry->rdma_entry); 2273dc2316ebSYishai Hadas return ret; 227424da0016SAriel Levkovich } 227524da0016SAriel Levkovich 22767be76befSYishai Hadas static u64 mlx5_entry_to_mmap_offset(struct mlx5_user_mmap_entry *entry) 22777be76befSYishai Hadas { 22789b6d3bbcSLeon Romanovsky u64 cmd = (entry->rdma_entry.start_pgoff >> 16) & 0xFFFF; 22799b6d3bbcSLeon Romanovsky u64 index = entry->rdma_entry.start_pgoff & 0xFFFF; 22807be76befSYishai Hadas 22817be76befSYishai Hadas return (((index >> 8) << 16) | (cmd << MLX5_IB_MMAP_CMD_SHIFT) | 22827be76befSYishai Hadas (index & 0xFF)) << PAGE_SHIFT; 22837be76befSYishai Hadas } 22847be76befSYishai Hadas 228537aa5c36SGuy Levi static int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma) 228637aa5c36SGuy Levi { 228737aa5c36SGuy Levi struct mlx5_ib_ucontext *context = to_mucontext(ibcontext); 228837aa5c36SGuy Levi struct mlx5_ib_dev *dev = to_mdev(ibcontext->device); 228937aa5c36SGuy Levi unsigned long command; 229037aa5c36SGuy Levi phys_addr_t pfn; 229137aa5c36SGuy Levi 229237aa5c36SGuy Levi command = get_command(vma->vm_pgoff); 229337aa5c36SGuy Levi switch (command) { 229437aa5c36SGuy Levi case MLX5_IB_MMAP_WC_PAGE: 22951f3db161SYishai Hadas case MLX5_IB_MMAP_ALLOC_WC: 22961f3db161SYishai Hadas if (!dev->wc_support) 22971f3db161SYishai Hadas return -EPERM; 22981f3db161SYishai Hadas fallthrough; 229937aa5c36SGuy Levi case MLX5_IB_MMAP_NC_PAGE: 230037aa5c36SGuy Levi case MLX5_IB_MMAP_REGULAR_PAGE: 23017c2344c3SMaor Gottlieb return uar_mmap(dev, command, vma, context); 2302e126ba97SEli Cohen 2303e126ba97SEli Cohen case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES: 2304e126ba97SEli Cohen return -ENOSYS; 2305e126ba97SEli Cohen 2306d69e3bcfSMatan Barak case MLX5_IB_MMAP_CORE_CLOCK: 2307d69e3bcfSMatan Barak if (vma->vm_end - vma->vm_start != PAGE_SIZE) 2308d69e3bcfSMatan Barak return -EINVAL; 2309d69e3bcfSMatan Barak 23106cbac1e4SMatan Barak if (vma->vm_flags & VM_WRITE) 2311d69e3bcfSMatan Barak return -EPERM; 2312c660133cSJason Gunthorpe vma->vm_flags &= ~VM_MAYWRITE; 2313d69e3bcfSMatan Barak 2314d69e3bcfSMatan Barak /* Don't expose to user-space information it shouldn't have */ 2315d69e3bcfSMatan Barak if (PAGE_SIZE > 4096) 2316d69e3bcfSMatan Barak return -EOPNOTSUPP; 2317d69e3bcfSMatan Barak 2318d69e3bcfSMatan Barak pfn = (dev->mdev->iseg_base + 2319d69e3bcfSMatan Barak offsetof(struct mlx5_init_seg, internal_timer_h)) >> 2320d69e3bcfSMatan Barak PAGE_SHIFT; 2321d5e560d3SJason Gunthorpe return rdma_user_mmap_io(&context->ibucontext, vma, pfn, 2322d5e560d3SJason Gunthorpe PAGE_SIZE, 2323c043ff2cSMichal Kalderon pgprot_noncached(vma->vm_page_prot), 2324c043ff2cSMichal Kalderon NULL); 23255c99eaecSFeras Daoud case MLX5_IB_MMAP_CLOCK_INFO: 23265c99eaecSFeras Daoud return mlx5_ib_mmap_clock_info_page(dev, vma, context); 2327d69e3bcfSMatan Barak 2328e126ba97SEli Cohen default: 2329dc2316ebSYishai Hadas return mlx5_ib_mmap_offset(dev, vma, ibcontext); 2330e126ba97SEli Cohen } 2331e126ba97SEli Cohen 2332e126ba97SEli Cohen return 0; 2333e126ba97SEli Cohen } 2334e126ba97SEli Cohen 2335ff23dfa1SShamir Rabinovitch static int mlx5_ib_alloc_pd(struct ib_pd *ibpd, struct ib_udata *udata) 2336e126ba97SEli Cohen { 233721a428a0SLeon Romanovsky struct mlx5_ib_pd *pd = to_mpd(ibpd); 233821a428a0SLeon Romanovsky struct ib_device *ibdev = ibpd->device; 2339e126ba97SEli Cohen struct mlx5_ib_alloc_pd_resp resp; 2340e126ba97SEli Cohen int err; 2341a1069c1cSYishai Hadas u32 out[MLX5_ST_SZ_DW(alloc_pd_out)] = {}; 2342a1069c1cSYishai Hadas u32 in[MLX5_ST_SZ_DW(alloc_pd_in)] = {}; 2343a1069c1cSYishai Hadas u16 uid = 0; 2344ff23dfa1SShamir Rabinovitch struct mlx5_ib_ucontext *context = rdma_udata_to_drv_context( 2345ff23dfa1SShamir Rabinovitch udata, struct mlx5_ib_ucontext, ibucontext); 2346e126ba97SEli Cohen 2347ff23dfa1SShamir Rabinovitch uid = context ? context->devx_uid : 0; 2348a1069c1cSYishai Hadas MLX5_SET(alloc_pd_in, in, opcode, MLX5_CMD_OP_ALLOC_PD); 2349a1069c1cSYishai Hadas MLX5_SET(alloc_pd_in, in, uid, uid); 235031578defSLeon Romanovsky err = mlx5_cmd_exec_inout(to_mdev(ibdev)->mdev, alloc_pd, in, out); 235121a428a0SLeon Romanovsky if (err) 235221a428a0SLeon Romanovsky return err; 2353e126ba97SEli Cohen 2354a1069c1cSYishai Hadas pd->pdn = MLX5_GET(alloc_pd_out, out, pd); 2355a1069c1cSYishai Hadas pd->uid = uid; 2356ff23dfa1SShamir Rabinovitch if (udata) { 2357e126ba97SEli Cohen resp.pdn = pd->pdn; 2358e126ba97SEli Cohen if (ib_copy_to_udata(udata, &resp, sizeof(resp))) { 2359a1069c1cSYishai Hadas mlx5_cmd_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn, uid); 236021a428a0SLeon Romanovsky return -EFAULT; 2361e126ba97SEli Cohen } 2362e126ba97SEli Cohen } 2363e126ba97SEli Cohen 236421a428a0SLeon Romanovsky return 0; 2365e126ba97SEli Cohen } 2366e126ba97SEli Cohen 236791a7c58fSLeon Romanovsky static int mlx5_ib_dealloc_pd(struct ib_pd *pd, struct ib_udata *udata) 2368e126ba97SEli Cohen { 2369e126ba97SEli Cohen struct mlx5_ib_dev *mdev = to_mdev(pd->device); 2370e126ba97SEli Cohen struct mlx5_ib_pd *mpd = to_mpd(pd); 2371e126ba97SEli Cohen 237291a7c58fSLeon Romanovsky return mlx5_cmd_dealloc_pd(mdev->mdev, mpd->pdn, mpd->uid); 2373e126ba97SEli Cohen } 2374e126ba97SEli Cohen 2375e126ba97SEli Cohen static int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid) 2376e126ba97SEli Cohen { 2377e126ba97SEli Cohen struct mlx5_ib_dev *dev = to_mdev(ibqp->device); 237881e30880SYishai Hadas struct mlx5_ib_qp *mqp = to_mqp(ibqp); 2379e126ba97SEli Cohen int err; 2380539ec982SYishai Hadas u16 uid; 2381539ec982SYishai Hadas 2382539ec982SYishai Hadas uid = ibqp->pd ? 2383539ec982SYishai Hadas to_mpd(ibqp->pd)->uid : 0; 2384e126ba97SEli Cohen 23852be08c30SLeon Romanovsky if (mqp->flags & IB_QP_CREATE_SOURCE_QPN) { 238681e30880SYishai Hadas mlx5_ib_dbg(dev, "Attaching a multi cast group to underlay QP is not supported\n"); 238781e30880SYishai Hadas return -EOPNOTSUPP; 238881e30880SYishai Hadas } 238981e30880SYishai Hadas 2390539ec982SYishai Hadas err = mlx5_cmd_attach_mcg(dev->mdev, gid, ibqp->qp_num, uid); 2391e126ba97SEli Cohen if (err) 2392e126ba97SEli Cohen mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n", 2393e126ba97SEli Cohen ibqp->qp_num, gid->raw); 2394e126ba97SEli Cohen 2395e126ba97SEli Cohen return err; 2396e126ba97SEli Cohen } 2397e126ba97SEli Cohen 2398e126ba97SEli Cohen static int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid) 2399e126ba97SEli Cohen { 2400e126ba97SEli Cohen struct mlx5_ib_dev *dev = to_mdev(ibqp->device); 2401e126ba97SEli Cohen int err; 2402539ec982SYishai Hadas u16 uid; 2403e126ba97SEli Cohen 2404539ec982SYishai Hadas uid = ibqp->pd ? 2405539ec982SYishai Hadas to_mpd(ibqp->pd)->uid : 0; 2406539ec982SYishai Hadas err = mlx5_cmd_detach_mcg(dev->mdev, gid, ibqp->qp_num, uid); 2407e126ba97SEli Cohen if (err) 2408e126ba97SEli Cohen mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n", 2409e126ba97SEli Cohen ibqp->qp_num, gid->raw); 2410e126ba97SEli Cohen 2411e126ba97SEli Cohen return err; 2412e126ba97SEli Cohen } 2413e126ba97SEli Cohen 2414e126ba97SEli Cohen static int init_node_data(struct mlx5_ib_dev *dev) 2415e126ba97SEli Cohen { 24161b5daf11SMajd Dibbiny int err; 2417e126ba97SEli Cohen 24181b5daf11SMajd Dibbiny err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc); 2419e126ba97SEli Cohen if (err) 2420e126ba97SEli Cohen return err; 24211b5daf11SMajd Dibbiny 24221b5daf11SMajd Dibbiny dev->mdev->rev_id = dev->mdev->pdev->revision; 24231b5daf11SMajd Dibbiny 24241b5daf11SMajd Dibbiny return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid); 2425e126ba97SEli Cohen } 2426e126ba97SEli Cohen 2427508a523fSParav Pandit static ssize_t fw_pages_show(struct device *device, 2428508a523fSParav Pandit struct device_attribute *attr, char *buf) 2429e126ba97SEli Cohen { 2430e126ba97SEli Cohen struct mlx5_ib_dev *dev = 243154747231SParav Pandit rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev); 2432e126ba97SEli Cohen 24331c7fd726SJoe Perches return sysfs_emit(buf, "%d\n", dev->mdev->priv.fw_pages); 2434e126ba97SEli Cohen } 2435508a523fSParav Pandit static DEVICE_ATTR_RO(fw_pages); 2436e126ba97SEli Cohen 2437508a523fSParav Pandit static ssize_t reg_pages_show(struct device *device, 2438e126ba97SEli Cohen struct device_attribute *attr, char *buf) 2439e126ba97SEli Cohen { 2440e126ba97SEli Cohen struct mlx5_ib_dev *dev = 244154747231SParav Pandit rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev); 2442e126ba97SEli Cohen 24431c7fd726SJoe Perches return sysfs_emit(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages)); 2444e126ba97SEli Cohen } 2445508a523fSParav Pandit static DEVICE_ATTR_RO(reg_pages); 2446e126ba97SEli Cohen 2447508a523fSParav Pandit static ssize_t hca_type_show(struct device *device, 2448508a523fSParav Pandit struct device_attribute *attr, char *buf) 2449e126ba97SEli Cohen { 2450e126ba97SEli Cohen struct mlx5_ib_dev *dev = 245154747231SParav Pandit rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev); 245254747231SParav Pandit 24531c7fd726SJoe Perches return sysfs_emit(buf, "MT%d\n", dev->mdev->pdev->device); 2454e126ba97SEli Cohen } 2455508a523fSParav Pandit static DEVICE_ATTR_RO(hca_type); 2456e126ba97SEli Cohen 2457508a523fSParav Pandit static ssize_t hw_rev_show(struct device *device, 2458508a523fSParav Pandit struct device_attribute *attr, char *buf) 2459e126ba97SEli Cohen { 2460e126ba97SEli Cohen struct mlx5_ib_dev *dev = 246154747231SParav Pandit rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev); 246254747231SParav Pandit 24631c7fd726SJoe Perches return sysfs_emit(buf, "%x\n", dev->mdev->rev_id); 2464e126ba97SEli Cohen } 2465508a523fSParav Pandit static DEVICE_ATTR_RO(hw_rev); 2466e126ba97SEli Cohen 2467508a523fSParav Pandit static ssize_t board_id_show(struct device *device, 2468508a523fSParav Pandit struct device_attribute *attr, char *buf) 2469e126ba97SEli Cohen { 2470e126ba97SEli Cohen struct mlx5_ib_dev *dev = 247154747231SParav Pandit rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev); 247254747231SParav Pandit 24731c7fd726SJoe Perches return sysfs_emit(buf, "%.*s\n", MLX5_BOARD_ID_LEN, 24749603b61dSJack Morgenstein dev->mdev->board_id); 2475e126ba97SEli Cohen } 2476508a523fSParav Pandit static DEVICE_ATTR_RO(board_id); 2477e126ba97SEli Cohen 2478508a523fSParav Pandit static struct attribute *mlx5_class_attributes[] = { 2479508a523fSParav Pandit &dev_attr_hw_rev.attr, 2480508a523fSParav Pandit &dev_attr_hca_type.attr, 2481508a523fSParav Pandit &dev_attr_board_id.attr, 2482508a523fSParav Pandit &dev_attr_fw_pages.attr, 2483508a523fSParav Pandit &dev_attr_reg_pages.attr, 2484508a523fSParav Pandit NULL, 2485508a523fSParav Pandit }; 2486e126ba97SEli Cohen 2487508a523fSParav Pandit static const struct attribute_group mlx5_attr_group = { 2488508a523fSParav Pandit .attrs = mlx5_class_attributes, 2489e126ba97SEli Cohen }; 2490e126ba97SEli Cohen 24917722f47eSHaggai Eran static void pkey_change_handler(struct work_struct *work) 24927722f47eSHaggai Eran { 24937722f47eSHaggai Eran struct mlx5_ib_port_resources *ports = 24947722f47eSHaggai Eran container_of(work, struct mlx5_ib_port_resources, 24957722f47eSHaggai Eran pkey_change_work); 24967722f47eSHaggai Eran 24978c9e7f03SLeon Romanovsky if (!ports->gsi) 24988c9e7f03SLeon Romanovsky /* 24998c9e7f03SLeon Romanovsky * We got this event before device was fully configured 25008c9e7f03SLeon Romanovsky * and MAD registration code wasn't called/finished yet. 25018c9e7f03SLeon Romanovsky */ 25028c9e7f03SLeon Romanovsky return; 25038c9e7f03SLeon Romanovsky 25047722f47eSHaggai Eran mlx5_ib_gsi_pkey_change(ports->gsi); 25057722f47eSHaggai Eran } 25067722f47eSHaggai Eran 250789ea94a7SMaor Gottlieb static void mlx5_ib_handle_internal_error(struct mlx5_ib_dev *ibdev) 250889ea94a7SMaor Gottlieb { 250989ea94a7SMaor Gottlieb struct mlx5_ib_qp *mqp; 251089ea94a7SMaor Gottlieb struct mlx5_ib_cq *send_mcq, *recv_mcq; 251189ea94a7SMaor Gottlieb struct mlx5_core_cq *mcq; 251289ea94a7SMaor Gottlieb struct list_head cq_armed_list; 251389ea94a7SMaor Gottlieb unsigned long flags_qp; 251489ea94a7SMaor Gottlieb unsigned long flags_cq; 251589ea94a7SMaor Gottlieb unsigned long flags; 251689ea94a7SMaor Gottlieb 251789ea94a7SMaor Gottlieb INIT_LIST_HEAD(&cq_armed_list); 251889ea94a7SMaor Gottlieb 251989ea94a7SMaor Gottlieb /* Go over qp list reside on that ibdev, sync with create/destroy qp.*/ 252089ea94a7SMaor Gottlieb spin_lock_irqsave(&ibdev->reset_flow_resource_lock, flags); 252189ea94a7SMaor Gottlieb list_for_each_entry(mqp, &ibdev->qp_list, qps_list) { 252289ea94a7SMaor Gottlieb spin_lock_irqsave(&mqp->sq.lock, flags_qp); 252389ea94a7SMaor Gottlieb if (mqp->sq.tail != mqp->sq.head) { 252489ea94a7SMaor Gottlieb send_mcq = to_mcq(mqp->ibqp.send_cq); 252589ea94a7SMaor Gottlieb spin_lock_irqsave(&send_mcq->lock, flags_cq); 252689ea94a7SMaor Gottlieb if (send_mcq->mcq.comp && 252789ea94a7SMaor Gottlieb mqp->ibqp.send_cq->comp_handler) { 252889ea94a7SMaor Gottlieb if (!send_mcq->mcq.reset_notify_added) { 252989ea94a7SMaor Gottlieb send_mcq->mcq.reset_notify_added = 1; 253089ea94a7SMaor Gottlieb list_add_tail(&send_mcq->mcq.reset_notify, 253189ea94a7SMaor Gottlieb &cq_armed_list); 253289ea94a7SMaor Gottlieb } 253389ea94a7SMaor Gottlieb } 253489ea94a7SMaor Gottlieb spin_unlock_irqrestore(&send_mcq->lock, flags_cq); 253589ea94a7SMaor Gottlieb } 253689ea94a7SMaor Gottlieb spin_unlock_irqrestore(&mqp->sq.lock, flags_qp); 253789ea94a7SMaor Gottlieb spin_lock_irqsave(&mqp->rq.lock, flags_qp); 253889ea94a7SMaor Gottlieb /* no handling is needed for SRQ */ 253989ea94a7SMaor Gottlieb if (!mqp->ibqp.srq) { 254089ea94a7SMaor Gottlieb if (mqp->rq.tail != mqp->rq.head) { 254189ea94a7SMaor Gottlieb recv_mcq = to_mcq(mqp->ibqp.recv_cq); 254289ea94a7SMaor Gottlieb spin_lock_irqsave(&recv_mcq->lock, flags_cq); 254389ea94a7SMaor Gottlieb if (recv_mcq->mcq.comp && 254489ea94a7SMaor Gottlieb mqp->ibqp.recv_cq->comp_handler) { 254589ea94a7SMaor Gottlieb if (!recv_mcq->mcq.reset_notify_added) { 254689ea94a7SMaor Gottlieb recv_mcq->mcq.reset_notify_added = 1; 254789ea94a7SMaor Gottlieb list_add_tail(&recv_mcq->mcq.reset_notify, 254889ea94a7SMaor Gottlieb &cq_armed_list); 254989ea94a7SMaor Gottlieb } 255089ea94a7SMaor Gottlieb } 255189ea94a7SMaor Gottlieb spin_unlock_irqrestore(&recv_mcq->lock, 255289ea94a7SMaor Gottlieb flags_cq); 255389ea94a7SMaor Gottlieb } 255489ea94a7SMaor Gottlieb } 255589ea94a7SMaor Gottlieb spin_unlock_irqrestore(&mqp->rq.lock, flags_qp); 255689ea94a7SMaor Gottlieb } 255789ea94a7SMaor Gottlieb /*At that point all inflight post send were put to be executed as of we 255889ea94a7SMaor Gottlieb * lock/unlock above locks Now need to arm all involved CQs. 255989ea94a7SMaor Gottlieb */ 256089ea94a7SMaor Gottlieb list_for_each_entry(mcq, &cq_armed_list, reset_notify) { 25614e0e2ea1SYishai Hadas mcq->comp(mcq, NULL); 256289ea94a7SMaor Gottlieb } 256389ea94a7SMaor Gottlieb spin_unlock_irqrestore(&ibdev->reset_flow_resource_lock, flags); 256489ea94a7SMaor Gottlieb } 256589ea94a7SMaor Gottlieb 256603404e8aSMaor Gottlieb static void delay_drop_handler(struct work_struct *work) 256703404e8aSMaor Gottlieb { 256803404e8aSMaor Gottlieb int err; 256903404e8aSMaor Gottlieb struct mlx5_ib_delay_drop *delay_drop = 257003404e8aSMaor Gottlieb container_of(work, struct mlx5_ib_delay_drop, 257103404e8aSMaor Gottlieb delay_drop_work); 257203404e8aSMaor Gottlieb 2573fe248c3aSMaor Gottlieb atomic_inc(&delay_drop->events_cnt); 2574fe248c3aSMaor Gottlieb 257503404e8aSMaor Gottlieb mutex_lock(&delay_drop->lock); 2576333fbaa0SLeon Romanovsky err = mlx5_core_set_delay_drop(delay_drop->dev, delay_drop->timeout); 257703404e8aSMaor Gottlieb if (err) { 257803404e8aSMaor Gottlieb mlx5_ib_warn(delay_drop->dev, "Failed to set delay drop, timeout=%u\n", 257903404e8aSMaor Gottlieb delay_drop->timeout); 258003404e8aSMaor Gottlieb delay_drop->activate = false; 258103404e8aSMaor Gottlieb } 258203404e8aSMaor Gottlieb mutex_unlock(&delay_drop->lock); 258303404e8aSMaor Gottlieb } 258403404e8aSMaor Gottlieb 258509e574faSSaeed Mahameed static void handle_general_event(struct mlx5_ib_dev *ibdev, struct mlx5_eqe *eqe, 258609e574faSSaeed Mahameed struct ib_event *ibev) 258709e574faSSaeed Mahameed { 25881fb7f897SMark Bloch u32 port = (eqe->data.port.port >> 4) & 0xf; 25896cfdc7e4SAya Levin 259009e574faSSaeed Mahameed switch (eqe->sub_type) { 259109e574faSSaeed Mahameed case MLX5_GENERAL_SUBTYPE_DELAY_DROP_TIMEOUT: 25926cfdc7e4SAya Levin if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) == 25936cfdc7e4SAya Levin IB_LINK_LAYER_ETHERNET) 259409e574faSSaeed Mahameed schedule_work(&ibdev->delay_drop.delay_drop_work); 259509e574faSSaeed Mahameed break; 259609e574faSSaeed Mahameed default: /* do nothing */ 259709e574faSSaeed Mahameed return; 259809e574faSSaeed Mahameed } 259909e574faSSaeed Mahameed } 260009e574faSSaeed Mahameed 2601134e9349SSaeed Mahameed static int handle_port_change(struct mlx5_ib_dev *ibdev, struct mlx5_eqe *eqe, 2602134e9349SSaeed Mahameed struct ib_event *ibev) 2603134e9349SSaeed Mahameed { 26041fb7f897SMark Bloch u32 port = (eqe->data.port.port >> 4) & 0xf; 2605134e9349SSaeed Mahameed 2606134e9349SSaeed Mahameed ibev->element.port_num = port; 2607134e9349SSaeed Mahameed 2608134e9349SSaeed Mahameed switch (eqe->sub_type) { 2609134e9349SSaeed Mahameed case MLX5_PORT_CHANGE_SUBTYPE_ACTIVE: 2610134e9349SSaeed Mahameed case MLX5_PORT_CHANGE_SUBTYPE_DOWN: 2611134e9349SSaeed Mahameed case MLX5_PORT_CHANGE_SUBTYPE_INITIALIZED: 2612134e9349SSaeed Mahameed /* In RoCE, port up/down events are handled in 2613134e9349SSaeed Mahameed * mlx5_netdev_event(). 2614134e9349SSaeed Mahameed */ 2615134e9349SSaeed Mahameed if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) == 2616134e9349SSaeed Mahameed IB_LINK_LAYER_ETHERNET) 2617134e9349SSaeed Mahameed return -EINVAL; 2618134e9349SSaeed Mahameed 2619134e9349SSaeed Mahameed ibev->event = (eqe->sub_type == MLX5_PORT_CHANGE_SUBTYPE_ACTIVE) ? 2620134e9349SSaeed Mahameed IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR; 2621134e9349SSaeed Mahameed break; 2622134e9349SSaeed Mahameed 2623134e9349SSaeed Mahameed case MLX5_PORT_CHANGE_SUBTYPE_LID: 2624134e9349SSaeed Mahameed ibev->event = IB_EVENT_LID_CHANGE; 2625134e9349SSaeed Mahameed break; 2626134e9349SSaeed Mahameed 2627134e9349SSaeed Mahameed case MLX5_PORT_CHANGE_SUBTYPE_PKEY: 2628134e9349SSaeed Mahameed ibev->event = IB_EVENT_PKEY_CHANGE; 2629134e9349SSaeed Mahameed schedule_work(&ibdev->devr.ports[port - 1].pkey_change_work); 2630134e9349SSaeed Mahameed break; 2631134e9349SSaeed Mahameed 2632134e9349SSaeed Mahameed case MLX5_PORT_CHANGE_SUBTYPE_GUID: 2633134e9349SSaeed Mahameed ibev->event = IB_EVENT_GID_CHANGE; 2634134e9349SSaeed Mahameed break; 2635134e9349SSaeed Mahameed 2636134e9349SSaeed Mahameed case MLX5_PORT_CHANGE_SUBTYPE_CLIENT_REREG: 2637134e9349SSaeed Mahameed ibev->event = IB_EVENT_CLIENT_REREGISTER; 2638134e9349SSaeed Mahameed break; 2639134e9349SSaeed Mahameed default: 2640134e9349SSaeed Mahameed return -EINVAL; 2641134e9349SSaeed Mahameed } 2642134e9349SSaeed Mahameed 2643134e9349SSaeed Mahameed return 0; 2644134e9349SSaeed Mahameed } 2645134e9349SSaeed Mahameed 2646d69a24e0SDaniel Jurgens static void mlx5_ib_handle_event(struct work_struct *_work) 2647e126ba97SEli Cohen { 2648d69a24e0SDaniel Jurgens struct mlx5_ib_event_work *work = 2649d69a24e0SDaniel Jurgens container_of(_work, struct mlx5_ib_event_work, work); 2650d69a24e0SDaniel Jurgens struct mlx5_ib_dev *ibdev; 2651e126ba97SEli Cohen struct ib_event ibev; 2652dbaaff2aSEli Cohen bool fatal = false; 2653e126ba97SEli Cohen 2654df097a27SSaeed Mahameed if (work->is_slave) { 2655df097a27SSaeed Mahameed ibdev = mlx5_ib_get_ibdev_from_mpi(work->mpi); 2656d69a24e0SDaniel Jurgens if (!ibdev) 2657d69a24e0SDaniel Jurgens goto out; 2658d69a24e0SDaniel Jurgens } else { 2659df097a27SSaeed Mahameed ibdev = work->dev; 2660d69a24e0SDaniel Jurgens } 2661d69a24e0SDaniel Jurgens 2662d69a24e0SDaniel Jurgens switch (work->event) { 2663e126ba97SEli Cohen case MLX5_DEV_EVENT_SYS_ERROR: 2664e126ba97SEli Cohen ibev.event = IB_EVENT_DEVICE_FATAL; 266589ea94a7SMaor Gottlieb mlx5_ib_handle_internal_error(ibdev); 2666134e9349SSaeed Mahameed ibev.element.port_num = (u8)(unsigned long)work->param; 2667dbaaff2aSEli Cohen fatal = true; 2668e126ba97SEli Cohen break; 2669134e9349SSaeed Mahameed case MLX5_EVENT_TYPE_PORT_CHANGE: 2670134e9349SSaeed Mahameed if (handle_port_change(ibdev, work->param, &ibev)) 2671d69a24e0SDaniel Jurgens goto out; 2672e126ba97SEli Cohen break; 267309e574faSSaeed Mahameed case MLX5_EVENT_TYPE_GENERAL_EVENT: 267409e574faSSaeed Mahameed handle_general_event(ibdev, work->param, &ibev); 2675df561f66SGustavo A. R. Silva fallthrough; 2676bdc37924SSaeed Mahameed default: 267703404e8aSMaor Gottlieb goto out; 2678e126ba97SEli Cohen } 2679e126ba97SEli Cohen 2680e126ba97SEli Cohen ibev.device = &ibdev->ib_dev; 2681e126ba97SEli Cohen 2682134e9349SSaeed Mahameed if (!rdma_is_port_valid(&ibdev->ib_dev, ibev.element.port_num)) { 2683134e9349SSaeed Mahameed mlx5_ib_warn(ibdev, "warning: event on port %d\n", ibev.element.port_num); 268403404e8aSMaor Gottlieb goto out; 2685a0c84c32SEli Cohen } 2686a0c84c32SEli Cohen 2687e126ba97SEli Cohen if (ibdev->ib_active) 2688e126ba97SEli Cohen ib_dispatch_event(&ibev); 2689dbaaff2aSEli Cohen 2690dbaaff2aSEli Cohen if (fatal) 2691dbaaff2aSEli Cohen ibdev->ib_active = false; 269203404e8aSMaor Gottlieb out: 2693d69a24e0SDaniel Jurgens kfree(work); 2694d69a24e0SDaniel Jurgens } 2695d69a24e0SDaniel Jurgens 2696df097a27SSaeed Mahameed static int mlx5_ib_event(struct notifier_block *nb, 2697df097a27SSaeed Mahameed unsigned long event, void *param) 2698d69a24e0SDaniel Jurgens { 2699d69a24e0SDaniel Jurgens struct mlx5_ib_event_work *work; 2700d69a24e0SDaniel Jurgens 2701d69a24e0SDaniel Jurgens work = kmalloc(sizeof(*work), GFP_ATOMIC); 270210bea9c8SLeon Romanovsky if (!work) 2703df097a27SSaeed Mahameed return NOTIFY_DONE; 270410bea9c8SLeon Romanovsky 2705d69a24e0SDaniel Jurgens INIT_WORK(&work->work, mlx5_ib_handle_event); 2706df097a27SSaeed Mahameed work->dev = container_of(nb, struct mlx5_ib_dev, mdev_events); 2707df097a27SSaeed Mahameed work->is_slave = false; 2708d69a24e0SDaniel Jurgens work->param = param; 2709d69a24e0SDaniel Jurgens work->event = event; 2710d69a24e0SDaniel Jurgens 2711d69a24e0SDaniel Jurgens queue_work(mlx5_ib_event_wq, &work->work); 2712df097a27SSaeed Mahameed 2713df097a27SSaeed Mahameed return NOTIFY_OK; 2714df097a27SSaeed Mahameed } 2715df097a27SSaeed Mahameed 2716df097a27SSaeed Mahameed static int mlx5_ib_event_slave_port(struct notifier_block *nb, 2717df097a27SSaeed Mahameed unsigned long event, void *param) 2718df097a27SSaeed Mahameed { 2719df097a27SSaeed Mahameed struct mlx5_ib_event_work *work; 2720df097a27SSaeed Mahameed 2721df097a27SSaeed Mahameed work = kmalloc(sizeof(*work), GFP_ATOMIC); 2722df097a27SSaeed Mahameed if (!work) 2723df097a27SSaeed Mahameed return NOTIFY_DONE; 2724df097a27SSaeed Mahameed 2725df097a27SSaeed Mahameed INIT_WORK(&work->work, mlx5_ib_handle_event); 2726df097a27SSaeed Mahameed work->mpi = container_of(nb, struct mlx5_ib_multiport_info, mdev_events); 2727df097a27SSaeed Mahameed work->is_slave = true; 2728df097a27SSaeed Mahameed work->param = param; 2729df097a27SSaeed Mahameed work->event = event; 2730df097a27SSaeed Mahameed queue_work(mlx5_ib_event_wq, &work->work); 2731df097a27SSaeed Mahameed 2732df097a27SSaeed Mahameed return NOTIFY_OK; 2733d69a24e0SDaniel Jurgens } 2734d69a24e0SDaniel Jurgens 2735c43f1112SMaor Gottlieb static int set_has_smi_cap(struct mlx5_ib_dev *dev) 2736c43f1112SMaor Gottlieb { 2737c43f1112SMaor Gottlieb struct mlx5_hca_vport_context vport_ctx; 2738c43f1112SMaor Gottlieb int err; 2739c43f1112SMaor Gottlieb int port; 2740c43f1112SMaor Gottlieb 27413ce60f44SParav Pandit for (port = 1; port <= ARRAY_SIZE(dev->port_caps); port++) { 27423ce60f44SParav Pandit dev->port_caps[port - 1].has_smi = false; 2743c43f1112SMaor Gottlieb if (MLX5_CAP_GEN(dev->mdev, port_type) == 2744c43f1112SMaor Gottlieb MLX5_CAP_PORT_TYPE_IB) { 2745c43f1112SMaor Gottlieb if (MLX5_CAP_GEN(dev->mdev, ib_virt)) { 2746c43f1112SMaor Gottlieb err = mlx5_query_hca_vport_context(dev->mdev, 0, 2747c43f1112SMaor Gottlieb port, 0, 2748c43f1112SMaor Gottlieb &vport_ctx); 2749c43f1112SMaor Gottlieb if (err) { 2750c43f1112SMaor Gottlieb mlx5_ib_err(dev, "query_hca_vport_context for port=%d failed %d\n", 2751c43f1112SMaor Gottlieb port, err); 2752c43f1112SMaor Gottlieb return err; 2753c43f1112SMaor Gottlieb } 27543ce60f44SParav Pandit dev->port_caps[port - 1].has_smi = 2755c43f1112SMaor Gottlieb vport_ctx.has_smi; 2756c43f1112SMaor Gottlieb } else { 27573ce60f44SParav Pandit dev->port_caps[port - 1].has_smi = true; 2758c43f1112SMaor Gottlieb } 2759c43f1112SMaor Gottlieb } 2760c43f1112SMaor Gottlieb } 2761c43f1112SMaor Gottlieb return 0; 2762c43f1112SMaor Gottlieb } 2763c43f1112SMaor Gottlieb 2764e126ba97SEli Cohen static void get_ext_port_caps(struct mlx5_ib_dev *dev) 2765e126ba97SEli Cohen { 276613179652SParav Pandit unsigned int port; 2767e126ba97SEli Cohen 276813179652SParav Pandit rdma_for_each_port (&dev->ib_dev, port) 2769e126ba97SEli Cohen mlx5_query_ext_port_caps(dev, port); 2770e126ba97SEli Cohen } 2771e126ba97SEli Cohen 27726e8484c5SMax Gurtovoy static u8 mlx5_get_umr_fence(u8 umr_fence_cap) 27736e8484c5SMax Gurtovoy { 27746e8484c5SMax Gurtovoy switch (umr_fence_cap) { 27756e8484c5SMax Gurtovoy case MLX5_CAP_UMR_FENCE_NONE: 27766e8484c5SMax Gurtovoy return MLX5_FENCE_MODE_NONE; 27776e8484c5SMax Gurtovoy case MLX5_CAP_UMR_FENCE_SMALL: 27786e8484c5SMax Gurtovoy return MLX5_FENCE_MODE_INITIATOR_SMALL; 27796e8484c5SMax Gurtovoy default: 27806e8484c5SMax Gurtovoy return MLX5_FENCE_MODE_STRONG_ORDERING; 27816e8484c5SMax Gurtovoy } 27826e8484c5SMax Gurtovoy } 27836e8484c5SMax Gurtovoy 27841e2b5a90SLeon Romanovsky static int mlx5_ib_dev_res_init(struct mlx5_ib_dev *dev) 2785e126ba97SEli Cohen { 27861e2b5a90SLeon Romanovsky struct mlx5_ib_resources *devr = &dev->devr; 2787e126ba97SEli Cohen struct ib_srq_init_attr attr; 278821a428a0SLeon Romanovsky struct ib_device *ibdev; 2789bcf4c1eaSMatan Barak struct ib_cq_init_attr cq_attr = {.cqe = 1}; 27907722f47eSHaggai Eran int port; 2791e126ba97SEli Cohen int ret = 0; 2792e126ba97SEli Cohen 279321a428a0SLeon Romanovsky ibdev = &dev->ib_dev; 2794e126ba97SEli Cohen 2795f4375443SLeon Romanovsky if (!MLX5_CAP_GEN(dev->mdev, xrc)) 2796f4375443SLeon Romanovsky return -EOPNOTSUPP; 2797f4375443SLeon Romanovsky 279820da44dfSLeon Romanovsky devr->p0 = ib_alloc_pd(ibdev, 0); 279920da44dfSLeon Romanovsky if (IS_ERR(devr->p0)) 280020da44dfSLeon Romanovsky return PTR_ERR(devr->p0); 2801d16e91daSHaggai Eran 280220da44dfSLeon Romanovsky devr->c0 = ib_create_cq(ibdev, NULL, NULL, NULL, &cq_attr); 280320da44dfSLeon Romanovsky if (IS_ERR(devr->c0)) { 280420da44dfSLeon Romanovsky ret = PTR_ERR(devr->c0); 2805e126ba97SEli Cohen goto error1; 2806e126ba97SEli Cohen } 2807e39afe3dSLeon Romanovsky 2808f4375443SLeon Romanovsky ret = mlx5_cmd_xrcd_alloc(dev->mdev, &devr->xrcdn0, 0); 2809f4375443SLeon Romanovsky if (ret) 2810e126ba97SEli Cohen goto error2; 2811e126ba97SEli Cohen 2812f4375443SLeon Romanovsky ret = mlx5_cmd_xrcd_alloc(dev->mdev, &devr->xrcdn1, 0); 2813f4375443SLeon Romanovsky if (ret) 2814e126ba97SEli Cohen goto error3; 2815e126ba97SEli Cohen 2816e126ba97SEli Cohen memset(&attr, 0, sizeof(attr)); 2817e126ba97SEli Cohen attr.attr.max_sge = 1; 2818e126ba97SEli Cohen attr.attr.max_wr = 1; 2819e126ba97SEli Cohen attr.srq_type = IB_SRQT_XRC; 28201a56ff6dSArtemy Kovalyov attr.ext.cq = devr->c0; 2821e126ba97SEli Cohen 282220da44dfSLeon Romanovsky devr->s0 = ib_create_srq(devr->p0, &attr); 282320da44dfSLeon Romanovsky if (IS_ERR(devr->s0)) { 282420da44dfSLeon Romanovsky ret = PTR_ERR(devr->s0); 282568e326deSLeon Romanovsky goto err_create; 282620da44dfSLeon Romanovsky } 2827e126ba97SEli Cohen 28284aa17b28SHaggai Abramonvsky memset(&attr, 0, sizeof(attr)); 28294aa17b28SHaggai Abramonvsky attr.attr.max_sge = 1; 28304aa17b28SHaggai Abramonvsky attr.attr.max_wr = 1; 28314aa17b28SHaggai Abramonvsky attr.srq_type = IB_SRQT_BASIC; 283268e326deSLeon Romanovsky 283320da44dfSLeon Romanovsky devr->s1 = ib_create_srq(devr->p0, &attr); 283420da44dfSLeon Romanovsky if (IS_ERR(devr->s1)) { 283520da44dfSLeon Romanovsky ret = PTR_ERR(devr->s1); 283668e326deSLeon Romanovsky goto error6; 283720da44dfSLeon Romanovsky } 28384aa17b28SHaggai Abramonvsky 2839f8225e34SLeon Romanovsky for (port = 0; port < ARRAY_SIZE(devr->ports); ++port) 28407722f47eSHaggai Eran INIT_WORK(&devr->ports[port].pkey_change_work, 28417722f47eSHaggai Eran pkey_change_handler); 28427722f47eSHaggai Eran 2843e126ba97SEli Cohen return 0; 2844e126ba97SEli Cohen 284568e326deSLeon Romanovsky error6: 284620da44dfSLeon Romanovsky ib_destroy_srq(devr->s0); 284768e326deSLeon Romanovsky err_create: 2848f4375443SLeon Romanovsky mlx5_cmd_xrcd_dealloc(dev->mdev, devr->xrcdn1, 0); 2849e126ba97SEli Cohen error3: 2850f4375443SLeon Romanovsky mlx5_cmd_xrcd_dealloc(dev->mdev, devr->xrcdn0, 0); 2851e126ba97SEli Cohen error2: 285220da44dfSLeon Romanovsky ib_destroy_cq(devr->c0); 2853e126ba97SEli Cohen error1: 285420da44dfSLeon Romanovsky ib_dealloc_pd(devr->p0); 2855e126ba97SEli Cohen return ret; 2856e126ba97SEli Cohen } 2857e126ba97SEli Cohen 28581e2b5a90SLeon Romanovsky static void mlx5_ib_dev_res_cleanup(struct mlx5_ib_dev *dev) 2859e126ba97SEli Cohen { 28601e2b5a90SLeon Romanovsky struct mlx5_ib_resources *devr = &dev->devr; 28617722f47eSHaggai Eran int port; 28627722f47eSHaggai Eran 2863b0791dbfSLeon Romanovsky /* 2864b0791dbfSLeon Romanovsky * Make sure no change P_Key work items are still executing. 2865b0791dbfSLeon Romanovsky * 2866b0791dbfSLeon Romanovsky * At this stage, the mlx5_ib_event should be unregistered 2867b0791dbfSLeon Romanovsky * and it ensures that no new works are added. 2868b0791dbfSLeon Romanovsky */ 28695d8f6a0eSMark Bloch for (port = 0; port < ARRAY_SIZE(devr->ports); ++port) 28707722f47eSHaggai Eran cancel_work_sync(&devr->ports[port].pkey_change_work); 2871b0791dbfSLeon Romanovsky 287220da44dfSLeon Romanovsky ib_destroy_srq(devr->s1); 287320da44dfSLeon Romanovsky ib_destroy_srq(devr->s0); 2874e126ba97SEli Cohen mlx5_cmd_xrcd_dealloc(dev->mdev, devr->xrcdn1, 0); 2875e126ba97SEli Cohen mlx5_cmd_xrcd_dealloc(dev->mdev, devr->xrcdn0, 0); 287620da44dfSLeon Romanovsky ib_destroy_cq(devr->c0); 287720da44dfSLeon Romanovsky ib_dealloc_pd(devr->p0); 2878e126ba97SEli Cohen } 2879e126ba97SEli Cohen 2880b02289b3SArtemy Kovalyov static u32 get_core_cap_flags(struct ib_device *ibdev, 2881b02289b3SArtemy Kovalyov struct mlx5_hca_vport_context *rep) 2882e53505a8SAchiad Shochat { 2883e53505a8SAchiad Shochat struct mlx5_ib_dev *dev = to_mdev(ibdev); 2884e53505a8SAchiad Shochat enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, 1); 2885e53505a8SAchiad Shochat u8 l3_type_cap = MLX5_CAP_ROCE(dev->mdev, l3_type); 2886e53505a8SAchiad Shochat u8 roce_version_cap = MLX5_CAP_ROCE(dev->mdev, roce_version); 288785c7c014SDaniel Jurgens bool raw_support = !mlx5_core_mp_enabled(dev->mdev); 2888e53505a8SAchiad Shochat u32 ret = 0; 2889e53505a8SAchiad Shochat 2890b02289b3SArtemy Kovalyov if (rep->grh_required) 2891b02289b3SArtemy Kovalyov ret |= RDMA_CORE_CAP_IB_GRH_REQUIRED; 2892b02289b3SArtemy Kovalyov 2893e53505a8SAchiad Shochat if (ll == IB_LINK_LAYER_INFINIBAND) 2894b02289b3SArtemy Kovalyov return ret | RDMA_CORE_PORT_IBA_IB; 2895e53505a8SAchiad Shochat 289685c7c014SDaniel Jurgens if (raw_support) 2897b02289b3SArtemy Kovalyov ret |= RDMA_CORE_PORT_RAW_PACKET; 289872cd5717SOr Gerlitz 2899e53505a8SAchiad Shochat if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV4_CAP)) 290072cd5717SOr Gerlitz return ret; 2901e53505a8SAchiad Shochat 2902e53505a8SAchiad Shochat if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV6_CAP)) 290372cd5717SOr Gerlitz return ret; 2904e53505a8SAchiad Shochat 2905e53505a8SAchiad Shochat if (roce_version_cap & MLX5_ROCE_VERSION_1_CAP) 2906e53505a8SAchiad Shochat ret |= RDMA_CORE_PORT_IBA_ROCE; 2907e53505a8SAchiad Shochat 2908e53505a8SAchiad Shochat if (roce_version_cap & MLX5_ROCE_VERSION_2_CAP) 2909e53505a8SAchiad Shochat ret |= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP; 2910e53505a8SAchiad Shochat 2911e53505a8SAchiad Shochat return ret; 2912e53505a8SAchiad Shochat } 2913e53505a8SAchiad Shochat 29141fb7f897SMark Bloch static int mlx5_port_immutable(struct ib_device *ibdev, u32 port_num, 29157738613eSIra Weiny struct ib_port_immutable *immutable) 29167738613eSIra Weiny { 29177738613eSIra Weiny struct ib_port_attr attr; 2918ca5b91d6SOr Gerlitz struct mlx5_ib_dev *dev = to_mdev(ibdev); 2919ca5b91d6SOr Gerlitz enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, port_num); 2920b02289b3SArtemy Kovalyov struct mlx5_hca_vport_context rep = {0}; 29217738613eSIra Weiny int err; 29227738613eSIra Weiny 2923c4550c63SOr Gerlitz err = ib_query_port(ibdev, port_num, &attr); 29247738613eSIra Weiny if (err) 29257738613eSIra Weiny return err; 29267738613eSIra Weiny 2927b02289b3SArtemy Kovalyov if (ll == IB_LINK_LAYER_INFINIBAND) { 2928b02289b3SArtemy Kovalyov err = mlx5_query_hca_vport_context(dev->mdev, 0, port_num, 0, 2929b02289b3SArtemy Kovalyov &rep); 2930b02289b3SArtemy Kovalyov if (err) 2931b02289b3SArtemy Kovalyov return err; 2932b02289b3SArtemy Kovalyov } 2933b02289b3SArtemy Kovalyov 29347738613eSIra Weiny immutable->pkey_tbl_len = attr.pkey_tbl_len; 29357738613eSIra Weiny immutable->gid_tbl_len = attr.gid_tbl_len; 2936b02289b3SArtemy Kovalyov immutable->core_cap_flags = get_core_cap_flags(ibdev, &rep); 2937337877a4SIra Weiny immutable->max_mad_size = IB_MGMT_MAD_SIZE; 29387738613eSIra Weiny 29397738613eSIra Weiny return 0; 29407738613eSIra Weiny } 29417738613eSIra Weiny 29421fb7f897SMark Bloch static int mlx5_port_rep_immutable(struct ib_device *ibdev, u32 port_num, 29438e6efa3aSMark Bloch struct ib_port_immutable *immutable) 29448e6efa3aSMark Bloch { 29458e6efa3aSMark Bloch struct ib_port_attr attr; 29468e6efa3aSMark Bloch int err; 29478e6efa3aSMark Bloch 29488e6efa3aSMark Bloch immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET; 29498e6efa3aSMark Bloch 29508e6efa3aSMark Bloch err = ib_query_port(ibdev, port_num, &attr); 29518e6efa3aSMark Bloch if (err) 29528e6efa3aSMark Bloch return err; 29538e6efa3aSMark Bloch 29548e6efa3aSMark Bloch immutable->pkey_tbl_len = attr.pkey_tbl_len; 29558e6efa3aSMark Bloch immutable->gid_tbl_len = attr.gid_tbl_len; 29568e6efa3aSMark Bloch immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET; 29578e6efa3aSMark Bloch 29588e6efa3aSMark Bloch return 0; 29598e6efa3aSMark Bloch } 29608e6efa3aSMark Bloch 29619abb0d1bSLeon Romanovsky static void get_dev_fw_str(struct ib_device *ibdev, char *str) 2962c7342823SIra Weiny { 2963c7342823SIra Weiny struct mlx5_ib_dev *dev = 2964c7342823SIra Weiny container_of(ibdev, struct mlx5_ib_dev, ib_dev); 29659abb0d1bSLeon Romanovsky snprintf(str, IB_FW_VERSION_NAME_MAX, "%d.%d.%04d", 29669abb0d1bSLeon Romanovsky fw_rev_maj(dev->mdev), fw_rev_min(dev->mdev), 29679abb0d1bSLeon Romanovsky fw_rev_sub(dev->mdev)); 2968c7342823SIra Weiny } 2969c7342823SIra Weiny 297045f95acdSOr Gerlitz static int mlx5_eth_lag_init(struct mlx5_ib_dev *dev) 29719ef9c640SAviv Heller { 29729ef9c640SAviv Heller struct mlx5_core_dev *mdev = dev->mdev; 29739ef9c640SAviv Heller struct mlx5_flow_namespace *ns = mlx5_get_flow_namespace(mdev, 29749ef9c640SAviv Heller MLX5_FLOW_NAMESPACE_LAG); 29759ef9c640SAviv Heller struct mlx5_flow_table *ft; 29769ef9c640SAviv Heller int err; 29779ef9c640SAviv Heller 2978c446d9daSMark Bloch if (!ns || !mlx5_lag_is_active(mdev)) 29799ef9c640SAviv Heller return 0; 29809ef9c640SAviv Heller 29819ef9c640SAviv Heller err = mlx5_cmd_create_vport_lag(mdev); 29829ef9c640SAviv Heller if (err) 29839ef9c640SAviv Heller return err; 29849ef9c640SAviv Heller 29859ef9c640SAviv Heller ft = mlx5_create_lag_demux_flow_table(ns, 0, 0); 29869ef9c640SAviv Heller if (IS_ERR(ft)) { 29879ef9c640SAviv Heller err = PTR_ERR(ft); 29889ef9c640SAviv Heller goto err_destroy_vport_lag; 29899ef9c640SAviv Heller } 29909ef9c640SAviv Heller 29919a4ca38dSMark Bloch dev->flow_db->lag_demux_ft = ft; 29927c34ec19SAviv Heller dev->lag_active = true; 29939ef9c640SAviv Heller return 0; 29949ef9c640SAviv Heller 29959ef9c640SAviv Heller err_destroy_vport_lag: 29969ef9c640SAviv Heller mlx5_cmd_destroy_vport_lag(mdev); 29979ef9c640SAviv Heller return err; 29989ef9c640SAviv Heller } 29999ef9c640SAviv Heller 300045f95acdSOr Gerlitz static void mlx5_eth_lag_cleanup(struct mlx5_ib_dev *dev) 30019ef9c640SAviv Heller { 30029ef9c640SAviv Heller struct mlx5_core_dev *mdev = dev->mdev; 30039ef9c640SAviv Heller 30047c34ec19SAviv Heller if (dev->lag_active) { 30057c34ec19SAviv Heller dev->lag_active = false; 30067c34ec19SAviv Heller 30079a4ca38dSMark Bloch mlx5_destroy_flow_table(dev->flow_db->lag_demux_ft); 30089a4ca38dSMark Bloch dev->flow_db->lag_demux_ft = NULL; 30099ef9c640SAviv Heller 30109ef9c640SAviv Heller mlx5_cmd_destroy_vport_lag(mdev); 30119ef9c640SAviv Heller } 30129ef9c640SAviv Heller } 30139ef9c640SAviv Heller 30141fb7f897SMark Bloch static int mlx5_add_netdev_notifier(struct mlx5_ib_dev *dev, u32 port_num) 3015fc24fc5eSAchiad Shochat { 3016e53505a8SAchiad Shochat int err; 3017e53505a8SAchiad Shochat 301895579e78SMark Bloch dev->port[port_num].roce.nb.notifier_call = mlx5_netdev_event; 3019de641d74SParav Pandit err = register_netdevice_notifier(&dev->port[port_num].roce.nb); 30205ec8c83eSAviv Heller if (err) { 302195579e78SMark Bloch dev->port[port_num].roce.nb.notifier_call = NULL; 3022e53505a8SAchiad Shochat return err; 30235ec8c83eSAviv Heller } 3024e53505a8SAchiad Shochat 3025d012f5d6SOr Gerlitz return 0; 3026d012f5d6SOr Gerlitz } 3027d012f5d6SOr Gerlitz 30281fb7f897SMark Bloch static void mlx5_remove_netdev_notifier(struct mlx5_ib_dev *dev, u32 port_num) 3029e126ba97SEli Cohen { 303095579e78SMark Bloch if (dev->port[port_num].roce.nb.notifier_call) { 3031de641d74SParav Pandit unregister_netdevice_notifier(&dev->port[port_num].roce.nb); 303295579e78SMark Bloch dev->port[port_num].roce.nb.notifier_call = NULL; 3033e126ba97SEli Cohen } 3034e126ba97SEli Cohen } 3035e126ba97SEli Cohen 3036e3f1ed1fSLeon Romanovsky static int mlx5_enable_eth(struct mlx5_ib_dev *dev) 3037e126ba97SEli Cohen { 3038e126ba97SEli Cohen int err; 3039e126ba97SEli Cohen 3040c446d9daSMark Bloch if (!dev->is_rep && dev->profile != &raw_eth_profile) { 3041e53505a8SAchiad Shochat err = mlx5_nic_vport_enable_roce(dev->mdev); 3042e53505a8SAchiad Shochat if (err) 30438e6efa3aSMark Bloch return err; 3044c446d9daSMark Bloch } 3045e53505a8SAchiad Shochat 304645f95acdSOr Gerlitz err = mlx5_eth_lag_init(dev); 30479ef9c640SAviv Heller if (err) 30489ef9c640SAviv Heller goto err_disable_roce; 30499ef9c640SAviv Heller 3050e53505a8SAchiad Shochat return 0; 3051e53505a8SAchiad Shochat 30529ef9c640SAviv Heller err_disable_roce: 3053c446d9daSMark Bloch if (!dev->is_rep && dev->profile != &raw_eth_profile) 30549ef9c640SAviv Heller mlx5_nic_vport_disable_roce(dev->mdev); 30559ef9c640SAviv Heller 3056e53505a8SAchiad Shochat return err; 3057fc24fc5eSAchiad Shochat } 3058fc24fc5eSAchiad Shochat 305945f95acdSOr Gerlitz static void mlx5_disable_eth(struct mlx5_ib_dev *dev) 3060fc24fc5eSAchiad Shochat { 306145f95acdSOr Gerlitz mlx5_eth_lag_cleanup(dev); 3062c446d9daSMark Bloch if (!dev->is_rep && dev->profile != &raw_eth_profile) 3063e53505a8SAchiad Shochat mlx5_nic_vport_disable_roce(dev->mdev); 3064fc24fc5eSAchiad Shochat } 3065fc24fc5eSAchiad Shochat 30661fb7f897SMark Bloch static int mlx5_ib_rn_get_params(struct ib_device *device, u32 port_num, 3067693dfd5aSErez Shitrit enum rdma_netdev_t type, 3068f6a8a19bSDenis Drozdov struct rdma_netdev_alloc_params *params) 3069693dfd5aSErez Shitrit { 3070693dfd5aSErez Shitrit if (type != RDMA_NETDEV_IPOIB) 3071f6a8a19bSDenis Drozdov return -EOPNOTSUPP; 3072693dfd5aSErez Shitrit 3073f6a8a19bSDenis Drozdov return mlx5_rdma_rn_get_params(to_mdev(device)->mdev, device, params); 3074693dfd5aSErez Shitrit } 3075693dfd5aSErez Shitrit 3076fe248c3aSMaor Gottlieb static ssize_t delay_drop_timeout_read(struct file *filp, char __user *buf, 3077fe248c3aSMaor Gottlieb size_t count, loff_t *pos) 3078fe248c3aSMaor Gottlieb { 3079fe248c3aSMaor Gottlieb struct mlx5_ib_delay_drop *delay_drop = filp->private_data; 3080fe248c3aSMaor Gottlieb char lbuf[20]; 3081fe248c3aSMaor Gottlieb int len; 3082fe248c3aSMaor Gottlieb 3083fe248c3aSMaor Gottlieb len = snprintf(lbuf, sizeof(lbuf), "%u\n", delay_drop->timeout); 3084fe248c3aSMaor Gottlieb return simple_read_from_buffer(buf, count, pos, lbuf, len); 3085fe248c3aSMaor Gottlieb } 3086fe248c3aSMaor Gottlieb 3087fe248c3aSMaor Gottlieb static ssize_t delay_drop_timeout_write(struct file *filp, const char __user *buf, 3088fe248c3aSMaor Gottlieb size_t count, loff_t *pos) 3089fe248c3aSMaor Gottlieb { 3090fe248c3aSMaor Gottlieb struct mlx5_ib_delay_drop *delay_drop = filp->private_data; 3091fe248c3aSMaor Gottlieb u32 timeout; 3092fe248c3aSMaor Gottlieb u32 var; 3093fe248c3aSMaor Gottlieb 3094fe248c3aSMaor Gottlieb if (kstrtouint_from_user(buf, count, 0, &var)) 3095fe248c3aSMaor Gottlieb return -EFAULT; 3096fe248c3aSMaor Gottlieb 3097fe248c3aSMaor Gottlieb timeout = min_t(u32, roundup(var, 100), MLX5_MAX_DELAY_DROP_TIMEOUT_MS * 3098fe248c3aSMaor Gottlieb 1000); 3099fe248c3aSMaor Gottlieb if (timeout != var) 3100fe248c3aSMaor Gottlieb mlx5_ib_dbg(delay_drop->dev, "Round delay drop timeout to %u usec\n", 3101fe248c3aSMaor Gottlieb timeout); 3102fe248c3aSMaor Gottlieb 3103fe248c3aSMaor Gottlieb delay_drop->timeout = timeout; 3104fe248c3aSMaor Gottlieb 3105fe248c3aSMaor Gottlieb return count; 3106fe248c3aSMaor Gottlieb } 3107fe248c3aSMaor Gottlieb 3108fe248c3aSMaor Gottlieb static const struct file_operations fops_delay_drop_timeout = { 3109fe248c3aSMaor Gottlieb .owner = THIS_MODULE, 3110fe248c3aSMaor Gottlieb .open = simple_open, 3111fe248c3aSMaor Gottlieb .write = delay_drop_timeout_write, 3112fe248c3aSMaor Gottlieb .read = delay_drop_timeout_read, 3113fe248c3aSMaor Gottlieb }; 3114fe248c3aSMaor Gottlieb 311532f69e4bSDaniel Jurgens static void mlx5_ib_unbind_slave_port(struct mlx5_ib_dev *ibdev, 311632f69e4bSDaniel Jurgens struct mlx5_ib_multiport_info *mpi) 311732f69e4bSDaniel Jurgens { 31181fb7f897SMark Bloch u32 port_num = mlx5_core_native_port_num(mpi->mdev) - 1; 311932f69e4bSDaniel Jurgens struct mlx5_ib_port *port = &ibdev->port[port_num]; 312032f69e4bSDaniel Jurgens int comps; 312132f69e4bSDaniel Jurgens int err; 312232f69e4bSDaniel Jurgens int i; 312332f69e4bSDaniel Jurgens 31249dc4cfffSLeon Romanovsky lockdep_assert_held(&mlx5_ib_multiport_mutex); 31259dc4cfffSLeon Romanovsky 3126a9e546e7SParav Pandit mlx5_ib_cleanup_cong_debugfs(ibdev, port_num); 3127a9e546e7SParav Pandit 312832f69e4bSDaniel Jurgens spin_lock(&port->mp.mpi_lock); 312932f69e4bSDaniel Jurgens if (!mpi->ibdev) { 313032f69e4bSDaniel Jurgens spin_unlock(&port->mp.mpi_lock); 313132f69e4bSDaniel Jurgens return; 313232f69e4bSDaniel Jurgens } 3133df097a27SSaeed Mahameed 313432f69e4bSDaniel Jurgens mpi->ibdev = NULL; 313532f69e4bSDaniel Jurgens 313632f69e4bSDaniel Jurgens spin_unlock(&port->mp.mpi_lock); 313723eaf3b5SLeon Romanovsky if (mpi->mdev_events.notifier_call) 313823eaf3b5SLeon Romanovsky mlx5_notifier_unregister(mpi->mdev, &mpi->mdev_events); 313923eaf3b5SLeon Romanovsky mpi->mdev_events.notifier_call = NULL; 314032f69e4bSDaniel Jurgens mlx5_remove_netdev_notifier(ibdev, port_num); 314132f69e4bSDaniel Jurgens spin_lock(&port->mp.mpi_lock); 314232f69e4bSDaniel Jurgens 314332f69e4bSDaniel Jurgens comps = mpi->mdev_refcnt; 314432f69e4bSDaniel Jurgens if (comps) { 314532f69e4bSDaniel Jurgens mpi->unaffiliate = true; 314632f69e4bSDaniel Jurgens init_completion(&mpi->unref_comp); 314732f69e4bSDaniel Jurgens spin_unlock(&port->mp.mpi_lock); 314832f69e4bSDaniel Jurgens 314932f69e4bSDaniel Jurgens for (i = 0; i < comps; i++) 315032f69e4bSDaniel Jurgens wait_for_completion(&mpi->unref_comp); 315132f69e4bSDaniel Jurgens 315232f69e4bSDaniel Jurgens spin_lock(&port->mp.mpi_lock); 315332f69e4bSDaniel Jurgens mpi->unaffiliate = false; 315432f69e4bSDaniel Jurgens } 315532f69e4bSDaniel Jurgens 315632f69e4bSDaniel Jurgens port->mp.mpi = NULL; 315732f69e4bSDaniel Jurgens 315832f69e4bSDaniel Jurgens spin_unlock(&port->mp.mpi_lock); 315932f69e4bSDaniel Jurgens 316032f69e4bSDaniel Jurgens err = mlx5_nic_vport_unaffiliate_multiport(mpi->mdev); 316132f69e4bSDaniel Jurgens 31621fb7f897SMark Bloch mlx5_ib_dbg(ibdev, "unaffiliated port %u\n", port_num + 1); 316332f69e4bSDaniel Jurgens /* Log an error, still needed to cleanup the pointers and add 316432f69e4bSDaniel Jurgens * it back to the list. 316532f69e4bSDaniel Jurgens */ 316632f69e4bSDaniel Jurgens if (err) 316732f69e4bSDaniel Jurgens mlx5_ib_err(ibdev, "Failed to unaffiliate port %u\n", 316832f69e4bSDaniel Jurgens port_num + 1); 316932f69e4bSDaniel Jurgens 317095579e78SMark Bloch ibdev->port[port_num].roce.last_port_state = IB_PORT_DOWN; 317132f69e4bSDaniel Jurgens } 317232f69e4bSDaniel Jurgens 317332f69e4bSDaniel Jurgens static bool mlx5_ib_bind_slave_port(struct mlx5_ib_dev *ibdev, 317432f69e4bSDaniel Jurgens struct mlx5_ib_multiport_info *mpi) 317532f69e4bSDaniel Jurgens { 31761fb7f897SMark Bloch u32 port_num = mlx5_core_native_port_num(mpi->mdev) - 1; 317732f69e4bSDaniel Jurgens int err; 317832f69e4bSDaniel Jurgens 31799dc4cfffSLeon Romanovsky lockdep_assert_held(&mlx5_ib_multiport_mutex); 31809dc4cfffSLeon Romanovsky 318132f69e4bSDaniel Jurgens spin_lock(&ibdev->port[port_num].mp.mpi_lock); 318232f69e4bSDaniel Jurgens if (ibdev->port[port_num].mp.mpi) { 31831fb7f897SMark Bloch mlx5_ib_dbg(ibdev, "port %u already affiliated.\n", 318432f69e4bSDaniel Jurgens port_num + 1); 318532f69e4bSDaniel Jurgens spin_unlock(&ibdev->port[port_num].mp.mpi_lock); 318632f69e4bSDaniel Jurgens return false; 318732f69e4bSDaniel Jurgens } 318832f69e4bSDaniel Jurgens 318932f69e4bSDaniel Jurgens ibdev->port[port_num].mp.mpi = mpi; 319032f69e4bSDaniel Jurgens mpi->ibdev = ibdev; 3191df097a27SSaeed Mahameed mpi->mdev_events.notifier_call = NULL; 319232f69e4bSDaniel Jurgens spin_unlock(&ibdev->port[port_num].mp.mpi_lock); 319332f69e4bSDaniel Jurgens 319432f69e4bSDaniel Jurgens err = mlx5_nic_vport_affiliate_multiport(ibdev->mdev, mpi->mdev); 319532f69e4bSDaniel Jurgens if (err) 319632f69e4bSDaniel Jurgens goto unbind; 319732f69e4bSDaniel Jurgens 319832f69e4bSDaniel Jurgens err = mlx5_add_netdev_notifier(ibdev, port_num); 319932f69e4bSDaniel Jurgens if (err) { 320032f69e4bSDaniel Jurgens mlx5_ib_err(ibdev, "failed adding netdev notifier for port %u\n", 320132f69e4bSDaniel Jurgens port_num + 1); 320232f69e4bSDaniel Jurgens goto unbind; 320332f69e4bSDaniel Jurgens } 320432f69e4bSDaniel Jurgens 3205df097a27SSaeed Mahameed mpi->mdev_events.notifier_call = mlx5_ib_event_slave_port; 3206df097a27SSaeed Mahameed mlx5_notifier_register(mpi->mdev, &mpi->mdev_events); 3207df097a27SSaeed Mahameed 320873eb8f03SGreg Kroah-Hartman mlx5_ib_init_cong_debugfs(ibdev, port_num); 3209a9e546e7SParav Pandit 321032f69e4bSDaniel Jurgens return true; 321132f69e4bSDaniel Jurgens 321232f69e4bSDaniel Jurgens unbind: 321332f69e4bSDaniel Jurgens mlx5_ib_unbind_slave_port(ibdev, mpi); 321432f69e4bSDaniel Jurgens return false; 321532f69e4bSDaniel Jurgens } 321632f69e4bSDaniel Jurgens 321732f69e4bSDaniel Jurgens static int mlx5_ib_init_multiport_master(struct mlx5_ib_dev *dev) 321832f69e4bSDaniel Jurgens { 32191fb7f897SMark Bloch u32 port_num = mlx5_core_native_port_num(dev->mdev) - 1; 322032f69e4bSDaniel Jurgens enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev, 322132f69e4bSDaniel Jurgens port_num + 1); 322232f69e4bSDaniel Jurgens struct mlx5_ib_multiport_info *mpi; 322332f69e4bSDaniel Jurgens int err; 32241fb7f897SMark Bloch u32 i; 322532f69e4bSDaniel Jurgens 322632f69e4bSDaniel Jurgens if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET) 322732f69e4bSDaniel Jurgens return 0; 322832f69e4bSDaniel Jurgens 322932f69e4bSDaniel Jurgens err = mlx5_query_nic_vport_system_image_guid(dev->mdev, 323032f69e4bSDaniel Jurgens &dev->sys_image_guid); 323132f69e4bSDaniel Jurgens if (err) 323232f69e4bSDaniel Jurgens return err; 323332f69e4bSDaniel Jurgens 323432f69e4bSDaniel Jurgens err = mlx5_nic_vport_enable_roce(dev->mdev); 323532f69e4bSDaniel Jurgens if (err) 323632f69e4bSDaniel Jurgens return err; 323732f69e4bSDaniel Jurgens 323832f69e4bSDaniel Jurgens mutex_lock(&mlx5_ib_multiport_mutex); 323932f69e4bSDaniel Jurgens for (i = 0; i < dev->num_ports; i++) { 324032f69e4bSDaniel Jurgens bool bound = false; 324132f69e4bSDaniel Jurgens 324232f69e4bSDaniel Jurgens /* build a stub multiport info struct for the native port. */ 324332f69e4bSDaniel Jurgens if (i == port_num) { 324432f69e4bSDaniel Jurgens mpi = kzalloc(sizeof(*mpi), GFP_KERNEL); 324532f69e4bSDaniel Jurgens if (!mpi) { 324632f69e4bSDaniel Jurgens mutex_unlock(&mlx5_ib_multiport_mutex); 324732f69e4bSDaniel Jurgens mlx5_nic_vport_disable_roce(dev->mdev); 324832f69e4bSDaniel Jurgens return -ENOMEM; 324932f69e4bSDaniel Jurgens } 325032f69e4bSDaniel Jurgens 325132f69e4bSDaniel Jurgens mpi->is_master = true; 325232f69e4bSDaniel Jurgens mpi->mdev = dev->mdev; 325332f69e4bSDaniel Jurgens mpi->sys_image_guid = dev->sys_image_guid; 325432f69e4bSDaniel Jurgens dev->port[i].mp.mpi = mpi; 325532f69e4bSDaniel Jurgens mpi->ibdev = dev; 325632f69e4bSDaniel Jurgens mpi = NULL; 325732f69e4bSDaniel Jurgens continue; 325832f69e4bSDaniel Jurgens } 325932f69e4bSDaniel Jurgens 326032f69e4bSDaniel Jurgens list_for_each_entry(mpi, &mlx5_ib_unaffiliated_port_list, 326132f69e4bSDaniel Jurgens list) { 326232f69e4bSDaniel Jurgens if (dev->sys_image_guid == mpi->sys_image_guid && 326332f69e4bSDaniel Jurgens (mlx5_core_native_port_num(mpi->mdev) - 1) == i) { 326432f69e4bSDaniel Jurgens bound = mlx5_ib_bind_slave_port(dev, mpi); 326532f69e4bSDaniel Jurgens } 326632f69e4bSDaniel Jurgens 326732f69e4bSDaniel Jurgens if (bound) { 3268c42260f1SVu Pham dev_dbg(mpi->mdev->device, 3269c42260f1SVu Pham "removing port from unaffiliated list.\n"); 327032f69e4bSDaniel Jurgens mlx5_ib_dbg(dev, "port %d bound\n", i + 1); 327132f69e4bSDaniel Jurgens list_del(&mpi->list); 327232f69e4bSDaniel Jurgens break; 327332f69e4bSDaniel Jurgens } 327432f69e4bSDaniel Jurgens } 32757416790eSParav Pandit if (!bound) 327632f69e4bSDaniel Jurgens mlx5_ib_dbg(dev, "no free port found for port %d\n", 327732f69e4bSDaniel Jurgens i + 1); 327832f69e4bSDaniel Jurgens } 327932f69e4bSDaniel Jurgens 328032f69e4bSDaniel Jurgens list_add_tail(&dev->ib_dev_list, &mlx5_ib_dev_list); 328132f69e4bSDaniel Jurgens mutex_unlock(&mlx5_ib_multiport_mutex); 328232f69e4bSDaniel Jurgens return err; 328332f69e4bSDaniel Jurgens } 328432f69e4bSDaniel Jurgens 328532f69e4bSDaniel Jurgens static void mlx5_ib_cleanup_multiport_master(struct mlx5_ib_dev *dev) 328632f69e4bSDaniel Jurgens { 32871fb7f897SMark Bloch u32 port_num = mlx5_core_native_port_num(dev->mdev) - 1; 328832f69e4bSDaniel Jurgens enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev, 328932f69e4bSDaniel Jurgens port_num + 1); 32901fb7f897SMark Bloch u32 i; 329132f69e4bSDaniel Jurgens 329232f69e4bSDaniel Jurgens if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET) 329332f69e4bSDaniel Jurgens return; 329432f69e4bSDaniel Jurgens 329532f69e4bSDaniel Jurgens mutex_lock(&mlx5_ib_multiport_mutex); 329632f69e4bSDaniel Jurgens for (i = 0; i < dev->num_ports; i++) { 329732f69e4bSDaniel Jurgens if (dev->port[i].mp.mpi) { 329832f69e4bSDaniel Jurgens /* Destroy the native port stub */ 329932f69e4bSDaniel Jurgens if (i == port_num) { 330032f69e4bSDaniel Jurgens kfree(dev->port[i].mp.mpi); 330132f69e4bSDaniel Jurgens dev->port[i].mp.mpi = NULL; 330232f69e4bSDaniel Jurgens } else { 33031fb7f897SMark Bloch mlx5_ib_dbg(dev, "unbinding port_num: %u\n", 33041fb7f897SMark Bloch i + 1); 33057ce6095eSLeon Romanovsky list_add_tail(&dev->port[i].mp.mpi->list, 33067ce6095eSLeon Romanovsky &mlx5_ib_unaffiliated_port_list); 33074a754d76SLeon Romanovsky mlx5_ib_unbind_slave_port(dev, 33084a754d76SLeon Romanovsky dev->port[i].mp.mpi); 330932f69e4bSDaniel Jurgens } 331032f69e4bSDaniel Jurgens } 331132f69e4bSDaniel Jurgens } 331232f69e4bSDaniel Jurgens 331332f69e4bSDaniel Jurgens mlx5_ib_dbg(dev, "removing from devlist\n"); 331432f69e4bSDaniel Jurgens list_del(&dev->ib_dev_list); 331532f69e4bSDaniel Jurgens mutex_unlock(&mlx5_ib_multiport_mutex); 331632f69e4bSDaniel Jurgens 331732f69e4bSDaniel Jurgens mlx5_nic_vport_disable_roce(dev->mdev); 331832f69e4bSDaniel Jurgens } 331932f69e4bSDaniel Jurgens 3320342ee59dSYishai Hadas static int mmap_obj_cleanup(struct ib_uobject *uobject, 33217be76befSYishai Hadas enum rdma_remove_reason why, 33227be76befSYishai Hadas struct uverbs_attr_bundle *attrs) 33237be76befSYishai Hadas { 33247be76befSYishai Hadas struct mlx5_user_mmap_entry *obj = uobject->object; 33257be76befSYishai Hadas 33267be76befSYishai Hadas rdma_user_mmap_entry_remove(&obj->rdma_entry); 33277be76befSYishai Hadas return 0; 33287be76befSYishai Hadas } 33297be76befSYishai Hadas 3330342ee59dSYishai Hadas static int mlx5_rdma_user_mmap_entry_insert(struct mlx5_ib_ucontext *c, 3331342ee59dSYishai Hadas struct mlx5_user_mmap_entry *entry, 3332342ee59dSYishai Hadas size_t length) 3333342ee59dSYishai Hadas { 3334342ee59dSYishai Hadas return rdma_user_mmap_entry_insert_range( 3335342ee59dSYishai Hadas &c->ibucontext, &entry->rdma_entry, length, 3336342ee59dSYishai Hadas (MLX5_IB_MMAP_OFFSET_START << 16), 3337342ee59dSYishai Hadas ((MLX5_IB_MMAP_OFFSET_END << 16) + (1UL << 16) - 1)); 3338342ee59dSYishai Hadas } 3339342ee59dSYishai Hadas 33407be76befSYishai Hadas static struct mlx5_user_mmap_entry * 33417be76befSYishai Hadas alloc_var_entry(struct mlx5_ib_ucontext *c) 33427be76befSYishai Hadas { 33437be76befSYishai Hadas struct mlx5_user_mmap_entry *entry; 33447be76befSYishai Hadas struct mlx5_var_table *var_table; 33457be76befSYishai Hadas u32 page_idx; 33467be76befSYishai Hadas int err; 33477be76befSYishai Hadas 33487be76befSYishai Hadas var_table = &to_mdev(c->ibucontext.device)->var_table; 33497be76befSYishai Hadas entry = kzalloc(sizeof(*entry), GFP_KERNEL); 33507be76befSYishai Hadas if (!entry) 33517be76befSYishai Hadas return ERR_PTR(-ENOMEM); 33527be76befSYishai Hadas 33537be76befSYishai Hadas mutex_lock(&var_table->bitmap_lock); 33547be76befSYishai Hadas page_idx = find_first_zero_bit(var_table->bitmap, 33557be76befSYishai Hadas var_table->num_var_hw_entries); 33567be76befSYishai Hadas if (page_idx >= var_table->num_var_hw_entries) { 33577be76befSYishai Hadas err = -ENOSPC; 33587be76befSYishai Hadas mutex_unlock(&var_table->bitmap_lock); 33597be76befSYishai Hadas goto end; 33607be76befSYishai Hadas } 33617be76befSYishai Hadas 33627be76befSYishai Hadas set_bit(page_idx, var_table->bitmap); 33637be76befSYishai Hadas mutex_unlock(&var_table->bitmap_lock); 33647be76befSYishai Hadas 33657be76befSYishai Hadas entry->address = var_table->hw_start_addr + 33667be76befSYishai Hadas (page_idx * var_table->stride_size); 33677be76befSYishai Hadas entry->page_idx = page_idx; 33687be76befSYishai Hadas entry->mmap_flag = MLX5_IB_MMAP_TYPE_VAR; 33697be76befSYishai Hadas 3370342ee59dSYishai Hadas err = mlx5_rdma_user_mmap_entry_insert(c, entry, 3371342ee59dSYishai Hadas var_table->stride_size); 33727be76befSYishai Hadas if (err) 33737be76befSYishai Hadas goto err_insert; 33747be76befSYishai Hadas 33757be76befSYishai Hadas return entry; 33767be76befSYishai Hadas 33777be76befSYishai Hadas err_insert: 33787be76befSYishai Hadas mutex_lock(&var_table->bitmap_lock); 33797be76befSYishai Hadas clear_bit(page_idx, var_table->bitmap); 33807be76befSYishai Hadas mutex_unlock(&var_table->bitmap_lock); 33817be76befSYishai Hadas end: 33827be76befSYishai Hadas kfree(entry); 33837be76befSYishai Hadas return ERR_PTR(err); 33847be76befSYishai Hadas } 33857be76befSYishai Hadas 33867be76befSYishai Hadas static int UVERBS_HANDLER(MLX5_IB_METHOD_VAR_OBJ_ALLOC)( 33877be76befSYishai Hadas struct uverbs_attr_bundle *attrs) 33887be76befSYishai Hadas { 33897be76befSYishai Hadas struct ib_uobject *uobj = uverbs_attr_get_uobject( 33907be76befSYishai Hadas attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_HANDLE); 33917be76befSYishai Hadas struct mlx5_ib_ucontext *c; 33927be76befSYishai Hadas struct mlx5_user_mmap_entry *entry; 33937be76befSYishai Hadas u64 mmap_offset; 33947be76befSYishai Hadas u32 length; 33957be76befSYishai Hadas int err; 33967be76befSYishai Hadas 33977be76befSYishai Hadas c = to_mucontext(ib_uverbs_get_ucontext(attrs)); 33987be76befSYishai Hadas if (IS_ERR(c)) 33997be76befSYishai Hadas return PTR_ERR(c); 34007be76befSYishai Hadas 34017be76befSYishai Hadas entry = alloc_var_entry(c); 34027be76befSYishai Hadas if (IS_ERR(entry)) 34037be76befSYishai Hadas return PTR_ERR(entry); 34047be76befSYishai Hadas 34057be76befSYishai Hadas mmap_offset = mlx5_entry_to_mmap_offset(entry); 34067be76befSYishai Hadas length = entry->rdma_entry.npages * PAGE_SIZE; 34077be76befSYishai Hadas uobj->object = entry; 34080ac8903cSJason Gunthorpe uverbs_finalize_uobj_create(attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_HANDLE); 34097be76befSYishai Hadas 34107be76befSYishai Hadas err = uverbs_copy_to(attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_MMAP_OFFSET, 34117be76befSYishai Hadas &mmap_offset, sizeof(mmap_offset)); 34127be76befSYishai Hadas if (err) 34130ac8903cSJason Gunthorpe return err; 34147be76befSYishai Hadas 34157be76befSYishai Hadas err = uverbs_copy_to(attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_PAGE_ID, 34167be76befSYishai Hadas &entry->page_idx, sizeof(entry->page_idx)); 34177be76befSYishai Hadas if (err) 34180ac8903cSJason Gunthorpe return err; 34197be76befSYishai Hadas 34207be76befSYishai Hadas err = uverbs_copy_to(attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_MMAP_LENGTH, 34217be76befSYishai Hadas &length, sizeof(length)); 34227be76befSYishai Hadas return err; 34237be76befSYishai Hadas } 34247be76befSYishai Hadas 34257be76befSYishai Hadas DECLARE_UVERBS_NAMED_METHOD( 34267be76befSYishai Hadas MLX5_IB_METHOD_VAR_OBJ_ALLOC, 34277be76befSYishai Hadas UVERBS_ATTR_IDR(MLX5_IB_ATTR_VAR_OBJ_ALLOC_HANDLE, 34287be76befSYishai Hadas MLX5_IB_OBJECT_VAR, 34297be76befSYishai Hadas UVERBS_ACCESS_NEW, 34307be76befSYishai Hadas UA_MANDATORY), 34317be76befSYishai Hadas UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_VAR_OBJ_ALLOC_PAGE_ID, 34327be76befSYishai Hadas UVERBS_ATTR_TYPE(u32), 34337be76befSYishai Hadas UA_MANDATORY), 34347be76befSYishai Hadas UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_VAR_OBJ_ALLOC_MMAP_LENGTH, 34357be76befSYishai Hadas UVERBS_ATTR_TYPE(u32), 34367be76befSYishai Hadas UA_MANDATORY), 34377be76befSYishai Hadas UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_VAR_OBJ_ALLOC_MMAP_OFFSET, 34387be76befSYishai Hadas UVERBS_ATTR_TYPE(u64), 34397be76befSYishai Hadas UA_MANDATORY)); 34407be76befSYishai Hadas 34417be76befSYishai Hadas DECLARE_UVERBS_NAMED_METHOD_DESTROY( 34427be76befSYishai Hadas MLX5_IB_METHOD_VAR_OBJ_DESTROY, 34437be76befSYishai Hadas UVERBS_ATTR_IDR(MLX5_IB_ATTR_VAR_OBJ_DESTROY_HANDLE, 34447be76befSYishai Hadas MLX5_IB_OBJECT_VAR, 34457be76befSYishai Hadas UVERBS_ACCESS_DESTROY, 34467be76befSYishai Hadas UA_MANDATORY)); 34477be76befSYishai Hadas 34487be76befSYishai Hadas DECLARE_UVERBS_NAMED_OBJECT(MLX5_IB_OBJECT_VAR, 3449342ee59dSYishai Hadas UVERBS_TYPE_ALLOC_IDR(mmap_obj_cleanup), 34507be76befSYishai Hadas &UVERBS_METHOD(MLX5_IB_METHOD_VAR_OBJ_ALLOC), 34517be76befSYishai Hadas &UVERBS_METHOD(MLX5_IB_METHOD_VAR_OBJ_DESTROY)); 34527be76befSYishai Hadas 34537be76befSYishai Hadas static bool var_is_supported(struct ib_device *device) 34547be76befSYishai Hadas { 34557be76befSYishai Hadas struct mlx5_ib_dev *dev = to_mdev(device); 34567be76befSYishai Hadas 34577be76befSYishai Hadas return (MLX5_CAP_GEN_64(dev->mdev, general_obj_types) & 34587be76befSYishai Hadas MLX5_GENERAL_OBJ_TYPES_CAP_VIRTIO_NET_Q); 34597be76befSYishai Hadas } 34607be76befSYishai Hadas 3461342ee59dSYishai Hadas static struct mlx5_user_mmap_entry * 3462342ee59dSYishai Hadas alloc_uar_entry(struct mlx5_ib_ucontext *c, 3463342ee59dSYishai Hadas enum mlx5_ib_uapi_uar_alloc_type alloc_type) 3464342ee59dSYishai Hadas { 3465342ee59dSYishai Hadas struct mlx5_user_mmap_entry *entry; 3466342ee59dSYishai Hadas struct mlx5_ib_dev *dev; 3467342ee59dSYishai Hadas u32 uar_index; 3468342ee59dSYishai Hadas int err; 3469342ee59dSYishai Hadas 3470342ee59dSYishai Hadas entry = kzalloc(sizeof(*entry), GFP_KERNEL); 3471342ee59dSYishai Hadas if (!entry) 3472342ee59dSYishai Hadas return ERR_PTR(-ENOMEM); 3473342ee59dSYishai Hadas 3474342ee59dSYishai Hadas dev = to_mdev(c->ibucontext.device); 3475d2c8a155SMeir Lichtinger err = mlx5_cmd_uar_alloc(dev->mdev, &uar_index, c->devx_uid); 3476342ee59dSYishai Hadas if (err) 3477342ee59dSYishai Hadas goto end; 3478342ee59dSYishai Hadas 3479342ee59dSYishai Hadas entry->page_idx = uar_index; 3480342ee59dSYishai Hadas entry->address = uar_index2paddress(dev, uar_index); 3481342ee59dSYishai Hadas if (alloc_type == MLX5_IB_UAPI_UAR_ALLOC_TYPE_BF) 3482342ee59dSYishai Hadas entry->mmap_flag = MLX5_IB_MMAP_TYPE_UAR_WC; 3483342ee59dSYishai Hadas else 3484342ee59dSYishai Hadas entry->mmap_flag = MLX5_IB_MMAP_TYPE_UAR_NC; 3485342ee59dSYishai Hadas 3486342ee59dSYishai Hadas err = mlx5_rdma_user_mmap_entry_insert(c, entry, PAGE_SIZE); 3487342ee59dSYishai Hadas if (err) 3488342ee59dSYishai Hadas goto err_insert; 3489342ee59dSYishai Hadas 3490342ee59dSYishai Hadas return entry; 3491342ee59dSYishai Hadas 3492342ee59dSYishai Hadas err_insert: 3493d2c8a155SMeir Lichtinger mlx5_cmd_uar_dealloc(dev->mdev, uar_index, c->devx_uid); 3494342ee59dSYishai Hadas end: 3495342ee59dSYishai Hadas kfree(entry); 3496342ee59dSYishai Hadas return ERR_PTR(err); 3497342ee59dSYishai Hadas } 3498342ee59dSYishai Hadas 3499342ee59dSYishai Hadas static int UVERBS_HANDLER(MLX5_IB_METHOD_UAR_OBJ_ALLOC)( 3500342ee59dSYishai Hadas struct uverbs_attr_bundle *attrs) 3501342ee59dSYishai Hadas { 3502342ee59dSYishai Hadas struct ib_uobject *uobj = uverbs_attr_get_uobject( 3503342ee59dSYishai Hadas attrs, MLX5_IB_ATTR_UAR_OBJ_ALLOC_HANDLE); 3504342ee59dSYishai Hadas enum mlx5_ib_uapi_uar_alloc_type alloc_type; 3505342ee59dSYishai Hadas struct mlx5_ib_ucontext *c; 3506342ee59dSYishai Hadas struct mlx5_user_mmap_entry *entry; 3507342ee59dSYishai Hadas u64 mmap_offset; 3508342ee59dSYishai Hadas u32 length; 3509342ee59dSYishai Hadas int err; 3510342ee59dSYishai Hadas 3511342ee59dSYishai Hadas c = to_mucontext(ib_uverbs_get_ucontext(attrs)); 3512342ee59dSYishai Hadas if (IS_ERR(c)) 3513342ee59dSYishai Hadas return PTR_ERR(c); 3514342ee59dSYishai Hadas 3515342ee59dSYishai Hadas err = uverbs_get_const(&alloc_type, attrs, 3516342ee59dSYishai Hadas MLX5_IB_ATTR_UAR_OBJ_ALLOC_TYPE); 3517342ee59dSYishai Hadas if (err) 3518342ee59dSYishai Hadas return err; 3519342ee59dSYishai Hadas 3520342ee59dSYishai Hadas if (alloc_type != MLX5_IB_UAPI_UAR_ALLOC_TYPE_BF && 3521342ee59dSYishai Hadas alloc_type != MLX5_IB_UAPI_UAR_ALLOC_TYPE_NC) 3522342ee59dSYishai Hadas return -EOPNOTSUPP; 3523342ee59dSYishai Hadas 3524342ee59dSYishai Hadas if (!to_mdev(c->ibucontext.device)->wc_support && 3525342ee59dSYishai Hadas alloc_type == MLX5_IB_UAPI_UAR_ALLOC_TYPE_BF) 3526342ee59dSYishai Hadas return -EOPNOTSUPP; 3527342ee59dSYishai Hadas 3528342ee59dSYishai Hadas entry = alloc_uar_entry(c, alloc_type); 3529342ee59dSYishai Hadas if (IS_ERR(entry)) 3530342ee59dSYishai Hadas return PTR_ERR(entry); 3531342ee59dSYishai Hadas 3532342ee59dSYishai Hadas mmap_offset = mlx5_entry_to_mmap_offset(entry); 3533342ee59dSYishai Hadas length = entry->rdma_entry.npages * PAGE_SIZE; 3534342ee59dSYishai Hadas uobj->object = entry; 35350ac8903cSJason Gunthorpe uverbs_finalize_uobj_create(attrs, MLX5_IB_ATTR_UAR_OBJ_ALLOC_HANDLE); 3536342ee59dSYishai Hadas 3537342ee59dSYishai Hadas err = uverbs_copy_to(attrs, MLX5_IB_ATTR_UAR_OBJ_ALLOC_MMAP_OFFSET, 3538342ee59dSYishai Hadas &mmap_offset, sizeof(mmap_offset)); 3539342ee59dSYishai Hadas if (err) 35400ac8903cSJason Gunthorpe return err; 3541342ee59dSYishai Hadas 3542342ee59dSYishai Hadas err = uverbs_copy_to(attrs, MLX5_IB_ATTR_UAR_OBJ_ALLOC_PAGE_ID, 3543342ee59dSYishai Hadas &entry->page_idx, sizeof(entry->page_idx)); 3544342ee59dSYishai Hadas if (err) 35450ac8903cSJason Gunthorpe return err; 3546342ee59dSYishai Hadas 3547342ee59dSYishai Hadas err = uverbs_copy_to(attrs, MLX5_IB_ATTR_UAR_OBJ_ALLOC_MMAP_LENGTH, 3548342ee59dSYishai Hadas &length, sizeof(length)); 3549342ee59dSYishai Hadas return err; 3550342ee59dSYishai Hadas } 3551342ee59dSYishai Hadas 3552342ee59dSYishai Hadas DECLARE_UVERBS_NAMED_METHOD( 3553342ee59dSYishai Hadas MLX5_IB_METHOD_UAR_OBJ_ALLOC, 3554342ee59dSYishai Hadas UVERBS_ATTR_IDR(MLX5_IB_ATTR_UAR_OBJ_ALLOC_HANDLE, 3555342ee59dSYishai Hadas MLX5_IB_OBJECT_UAR, 3556342ee59dSYishai Hadas UVERBS_ACCESS_NEW, 3557342ee59dSYishai Hadas UA_MANDATORY), 3558342ee59dSYishai Hadas UVERBS_ATTR_CONST_IN(MLX5_IB_ATTR_UAR_OBJ_ALLOC_TYPE, 3559342ee59dSYishai Hadas enum mlx5_ib_uapi_uar_alloc_type, 3560342ee59dSYishai Hadas UA_MANDATORY), 3561342ee59dSYishai Hadas UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_UAR_OBJ_ALLOC_PAGE_ID, 3562342ee59dSYishai Hadas UVERBS_ATTR_TYPE(u32), 3563342ee59dSYishai Hadas UA_MANDATORY), 3564342ee59dSYishai Hadas UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_UAR_OBJ_ALLOC_MMAP_LENGTH, 3565342ee59dSYishai Hadas UVERBS_ATTR_TYPE(u32), 3566342ee59dSYishai Hadas UA_MANDATORY), 3567342ee59dSYishai Hadas UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_UAR_OBJ_ALLOC_MMAP_OFFSET, 3568342ee59dSYishai Hadas UVERBS_ATTR_TYPE(u64), 3569342ee59dSYishai Hadas UA_MANDATORY)); 3570342ee59dSYishai Hadas 3571342ee59dSYishai Hadas DECLARE_UVERBS_NAMED_METHOD_DESTROY( 3572342ee59dSYishai Hadas MLX5_IB_METHOD_UAR_OBJ_DESTROY, 3573342ee59dSYishai Hadas UVERBS_ATTR_IDR(MLX5_IB_ATTR_UAR_OBJ_DESTROY_HANDLE, 3574342ee59dSYishai Hadas MLX5_IB_OBJECT_UAR, 3575342ee59dSYishai Hadas UVERBS_ACCESS_DESTROY, 3576342ee59dSYishai Hadas UA_MANDATORY)); 3577342ee59dSYishai Hadas 3578342ee59dSYishai Hadas DECLARE_UVERBS_NAMED_OBJECT(MLX5_IB_OBJECT_UAR, 3579342ee59dSYishai Hadas UVERBS_TYPE_ALLOC_IDR(mmap_obj_cleanup), 3580342ee59dSYishai Hadas &UVERBS_METHOD(MLX5_IB_METHOD_UAR_OBJ_ALLOC), 3581342ee59dSYishai Hadas &UVERBS_METHOD(MLX5_IB_METHOD_UAR_OBJ_DESTROY)); 3582342ee59dSYishai Hadas 35839a119cd5SJason Gunthorpe ADD_UVERBS_ATTRIBUTES_SIMPLE( 35840fb556b2SYishai Hadas mlx5_ib_query_context, 35850fb556b2SYishai Hadas UVERBS_OBJECT_DEVICE, 35860fb556b2SYishai Hadas UVERBS_METHOD_QUERY_CONTEXT, 35870fb556b2SYishai Hadas UVERBS_ATTR_PTR_OUT( 35880fb556b2SYishai Hadas MLX5_IB_ATTR_QUERY_CONTEXT_RESP_UCTX, 35890fb556b2SYishai Hadas UVERBS_ATTR_STRUCT(struct mlx5_ib_alloc_ucontext_resp, 35900fb556b2SYishai Hadas dump_fill_mkey), 35910fb556b2SYishai Hadas UA_MANDATORY)); 35920fb556b2SYishai Hadas 35930cbf432dSJason Gunthorpe static const struct uapi_definition mlx5_ib_defs[] = { 359436e235c8SJason Gunthorpe UAPI_DEF_CHAIN(mlx5_ib_devx_defs), 35950cbf432dSJason Gunthorpe UAPI_DEF_CHAIN(mlx5_ib_flow_defs), 359630f2fe40SYishai Hadas UAPI_DEF_CHAIN(mlx5_ib_qos_defs), 359705f71ef9SYishai Hadas UAPI_DEF_CHAIN(mlx5_ib_std_types_defs), 3598831df883SMaor Gottlieb UAPI_DEF_CHAIN(mlx5_ib_dm_defs), 35998c84660bSMatan Barak 36000fb556b2SYishai Hadas UAPI_DEF_CHAIN_OBJ_TREE(UVERBS_OBJECT_DEVICE, &mlx5_ib_query_context), 36017be76befSYishai Hadas UAPI_DEF_CHAIN_OBJ_TREE_NAMED(MLX5_IB_OBJECT_VAR, 36027be76befSYishai Hadas UAPI_DEF_IS_OBJ_SUPPORTED(var_is_supported)), 3603342ee59dSYishai Hadas UAPI_DEF_CHAIN_OBJ_TREE_NAMED(MLX5_IB_OBJECT_UAR), 36040cbf432dSJason Gunthorpe {} 36050cbf432dSJason Gunthorpe }; 36068c84660bSMatan Barak 3607fb652d32SMark Bloch static void mlx5_ib_stage_init_cleanup(struct mlx5_ib_dev *dev) 3608e126ba97SEli Cohen { 360932f69e4bSDaniel Jurgens mlx5_ib_cleanup_multiport_master(dev); 3610806b101bSJason Gunthorpe WARN_ON(!xa_empty(&dev->odp_mkeys)); 3611ab40530aSParav Pandit mutex_destroy(&dev->cap_mask_mutex); 361250211ec9SJason Gunthorpe WARN_ON(!xa_empty(&dev->sig_mrs)); 36134056b12eSAriel Levkovich WARN_ON(!bitmap_empty(dev->dm.memic_alloc_pages, MLX5_MAX_MEMIC_PAGES)); 361416c1975fSMark Bloch } 361516c1975fSMark Bloch 3616fb652d32SMark Bloch static int mlx5_ib_stage_init_init(struct mlx5_ib_dev *dev) 361716c1975fSMark Bloch { 361816c1975fSMark Bloch struct mlx5_core_dev *mdev = dev->mdev; 3619e126ba97SEli Cohen int err; 362032f69e4bSDaniel Jurgens int i; 3621e126ba97SEli Cohen 362213179652SParav Pandit dev->ib_dev.node_type = RDMA_NODE_IB_CA; 362313179652SParav Pandit dev->ib_dev.local_dma_lkey = 0 /* not supported for now */; 362413179652SParav Pandit dev->ib_dev.phys_port_cnt = dev->num_ports; 362513179652SParav Pandit dev->ib_dev.dev.parent = mdev->device; 362613179652SParav Pandit dev->ib_dev.lag_flags = RDMA_LAG_FLAGS_HASH_ALL_SLAVES; 362713179652SParav Pandit 362832f69e4bSDaniel Jurgens for (i = 0; i < dev->num_ports; i++) { 362932f69e4bSDaniel Jurgens spin_lock_init(&dev->port[i].mp.mpi_lock); 363095579e78SMark Bloch rwlock_init(&dev->port[i].roce.netdev_lock); 3631d3b5cc1cSMark Bloch dev->port[i].roce.dev = dev; 3632d3b5cc1cSMark Bloch dev->port[i].roce.native_port_num = i + 1; 3633d3b5cc1cSMark Bloch dev->port[i].roce.last_port_state = IB_PORT_DOWN; 363432f69e4bSDaniel Jurgens } 363532f69e4bSDaniel Jurgens 363632f69e4bSDaniel Jurgens err = mlx5_ib_init_multiport_master(dev); 3637e126ba97SEli Cohen if (err) 3638da796ccbSMark Bloch return err; 3639e126ba97SEli Cohen 3640a989ea01SMark Bloch err = set_has_smi_cap(dev); 3641a989ea01SMark Bloch if (err) 36422cb091f6SParav Pandit goto err_mp; 3643e126ba97SEli Cohen 36442019d70eSParav Pandit err = mlx5_query_max_pkeys(&dev->ib_dev, &dev->pkey_table_len); 36452019d70eSParav Pandit if (err) 36462019d70eSParav Pandit goto err_mp; 36472019d70eSParav Pandit 36481b5daf11SMajd Dibbiny if (mlx5_use_mad_ifc(dev)) 3649e126ba97SEli Cohen get_ext_port_caps(dev); 3650e126ba97SEli Cohen 3651f2f3df55SSaeed Mahameed dev->ib_dev.num_comp_vectors = mlx5_comp_vectors_count(mdev); 3652e126ba97SEli Cohen 36533cc297dbSMark Bloch mutex_init(&dev->cap_mask_mutex); 36543cc297dbSMark Bloch INIT_LIST_HEAD(&dev->qp_list); 36553cc297dbSMark Bloch spin_lock_init(&dev->reset_flow_resource_lock); 3656806b101bSJason Gunthorpe xa_init(&dev->odp_mkeys); 365750211ec9SJason Gunthorpe xa_init(&dev->sig_mrs); 3658f743ff3bSSaeed Mahameed atomic_set(&dev->mkey_var, 0); 36593cc297dbSMark Bloch 36603b113a1eSAriel Levkovich spin_lock_init(&dev->dm.lock); 36613b113a1eSAriel Levkovich dev->dm.dev = mdev; 366216c1975fSMark Bloch return 0; 366325c13324SAriel Levkovich 366432f69e4bSDaniel Jurgens err_mp: 366532f69e4bSDaniel Jurgens mlx5_ib_cleanup_multiport_master(dev); 3666d286ac1dSParav Pandit return err; 366716c1975fSMark Bloch } 366816c1975fSMark Bloch 3669ac47bf5eSLeon Romanovsky static int mlx5_ib_enable_driver(struct ib_device *dev) 36709a4ca38dSMark Bloch { 3671ac47bf5eSLeon Romanovsky struct mlx5_ib_dev *mdev = to_mdev(dev); 3672ac47bf5eSLeon Romanovsky int ret; 36739a4ca38dSMark Bloch 3674ac47bf5eSLeon Romanovsky ret = mlx5_ib_test_wc(mdev); 3675ac47bf5eSLeon Romanovsky mlx5_ib_dbg(mdev, "Write-Combining %s", 3676ac47bf5eSLeon Romanovsky mdev->wc_support ? "supported" : "not supported"); 36779a4ca38dSMark Bloch 3678ac47bf5eSLeon Romanovsky return ret; 36799a4ca38dSMark Bloch } 36809a4ca38dSMark Bloch 368196458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_ops = { 36827a154142SJason Gunthorpe .owner = THIS_MODULE, 3683b9560a41SJason Gunthorpe .driver_id = RDMA_DRIVER_MLX5, 368472c6ec18SJason Gunthorpe .uverbs_abi_ver = MLX5_IB_UVERBS_ABI_VERSION, 3685b9560a41SJason Gunthorpe 368696458233SKamal Heib .add_gid = mlx5_ib_add_gid, 368796458233SKamal Heib .alloc_mr = mlx5_ib_alloc_mr, 36886c984472SMax Gurtovoy .alloc_mr_integrity = mlx5_ib_alloc_mr_integrity, 368996458233SKamal Heib .alloc_pd = mlx5_ib_alloc_pd, 369096458233SKamal Heib .alloc_ucontext = mlx5_ib_alloc_ucontext, 369196458233SKamal Heib .attach_mcast = mlx5_ib_mcg_attach, 369296458233SKamal Heib .check_mr_status = mlx5_ib_check_mr_status, 369396458233SKamal Heib .create_ah = mlx5_ib_create_ah, 369496458233SKamal Heib .create_cq = mlx5_ib_create_cq, 369596458233SKamal Heib .create_qp = mlx5_ib_create_qp, 369696458233SKamal Heib .create_srq = mlx5_ib_create_srq, 3697676a80adSJason Gunthorpe .create_user_ah = mlx5_ib_create_ah, 369896458233SKamal Heib .dealloc_pd = mlx5_ib_dealloc_pd, 369996458233SKamal Heib .dealloc_ucontext = mlx5_ib_dealloc_ucontext, 370096458233SKamal Heib .del_gid = mlx5_ib_del_gid, 370196458233SKamal Heib .dereg_mr = mlx5_ib_dereg_mr, 370296458233SKamal Heib .destroy_ah = mlx5_ib_destroy_ah, 370396458233SKamal Heib .destroy_cq = mlx5_ib_destroy_cq, 370496458233SKamal Heib .destroy_qp = mlx5_ib_destroy_qp, 370596458233SKamal Heib .destroy_srq = mlx5_ib_destroy_srq, 370696458233SKamal Heib .detach_mcast = mlx5_ib_mcg_detach, 370796458233SKamal Heib .disassociate_ucontext = mlx5_ib_disassociate_ucontext, 370896458233SKamal Heib .drain_rq = mlx5_ib_drain_rq, 370996458233SKamal Heib .drain_sq = mlx5_ib_drain_sq, 3710915e4af5SJason Gunthorpe .device_group = &mlx5_attr_group, 371111f552e2SMichael Guralnik .enable_driver = mlx5_ib_enable_driver, 371296458233SKamal Heib .get_dev_fw_str = get_dev_fw_str, 371396458233SKamal Heib .get_dma_mr = mlx5_ib_get_dma_mr, 371496458233SKamal Heib .get_link_layer = mlx5_ib_port_link_layer, 371596458233SKamal Heib .map_mr_sg = mlx5_ib_map_mr_sg, 37166c984472SMax Gurtovoy .map_mr_sg_pi = mlx5_ib_map_mr_sg_pi, 371796458233SKamal Heib .mmap = mlx5_ib_mmap, 3718dc2316ebSYishai Hadas .mmap_free = mlx5_ib_mmap_free, 371996458233SKamal Heib .modify_cq = mlx5_ib_modify_cq, 372096458233SKamal Heib .modify_device = mlx5_ib_modify_device, 372196458233SKamal Heib .modify_port = mlx5_ib_modify_port, 372296458233SKamal Heib .modify_qp = mlx5_ib_modify_qp, 372396458233SKamal Heib .modify_srq = mlx5_ib_modify_srq, 372496458233SKamal Heib .poll_cq = mlx5_ib_poll_cq, 3725029e88fdSLeon Romanovsky .post_recv = mlx5_ib_post_recv_nodrain, 3726029e88fdSLeon Romanovsky .post_send = mlx5_ib_post_send_nodrain, 372796458233SKamal Heib .post_srq_recv = mlx5_ib_post_srq_recv, 372896458233SKamal Heib .process_mad = mlx5_ib_process_mad, 372996458233SKamal Heib .query_ah = mlx5_ib_query_ah, 373096458233SKamal Heib .query_device = mlx5_ib_query_device, 373196458233SKamal Heib .query_gid = mlx5_ib_query_gid, 373296458233SKamal Heib .query_pkey = mlx5_ib_query_pkey, 373396458233SKamal Heib .query_qp = mlx5_ib_query_qp, 373496458233SKamal Heib .query_srq = mlx5_ib_query_srq, 37350fb556b2SYishai Hadas .query_ucontext = mlx5_ib_query_ucontext, 373696458233SKamal Heib .reg_user_mr = mlx5_ib_reg_user_mr, 373790da7dc8SJianxin Xiong .reg_user_mr_dmabuf = mlx5_ib_reg_user_mr_dmabuf, 373896458233SKamal Heib .req_notify_cq = mlx5_ib_arm_cq, 373996458233SKamal Heib .rereg_user_mr = mlx5_ib_rereg_user_mr, 374096458233SKamal Heib .resize_cq = mlx5_ib_resize_cq, 3741d3456914SLeon Romanovsky 3742d3456914SLeon Romanovsky INIT_RDMA_OBJ_SIZE(ib_ah, mlx5_ib_ah, ibah), 37433b023e1bSLeon Romanovsky INIT_RDMA_OBJ_SIZE(ib_counters, mlx5_ib_mcounters, ibcntrs), 3744e39afe3dSLeon Romanovsky INIT_RDMA_OBJ_SIZE(ib_cq, mlx5_ib_cq, ibcq), 374521a428a0SLeon Romanovsky INIT_RDMA_OBJ_SIZE(ib_pd, mlx5_ib_pd, ibpd), 3746514aee66SLeon Romanovsky INIT_RDMA_OBJ_SIZE(ib_qp, mlx5_ib_qp, ibqp), 374768e326deSLeon Romanovsky INIT_RDMA_OBJ_SIZE(ib_srq, mlx5_ib_srq, ibsrq), 3748a2a074efSLeon Romanovsky INIT_RDMA_OBJ_SIZE(ib_ucontext, mlx5_ib_ucontext, ibucontext), 374996458233SKamal Heib }; 375096458233SKamal Heib 375196458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_ipoib_enhanced_ops = { 375296458233SKamal Heib .rdma_netdev_get_params = mlx5_ib_rn_get_params, 375396458233SKamal Heib }; 375496458233SKamal Heib 375596458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_sriov_ops = { 375696458233SKamal Heib .get_vf_config = mlx5_ib_get_vf_config, 37579c0015efSDanit Goldberg .get_vf_guid = mlx5_ib_get_vf_guid, 375896458233SKamal Heib .get_vf_stats = mlx5_ib_get_vf_stats, 375996458233SKamal Heib .set_vf_guid = mlx5_ib_set_vf_guid, 376096458233SKamal Heib .set_vf_link_state = mlx5_ib_set_vf_link_state, 376196458233SKamal Heib }; 376296458233SKamal Heib 376396458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_mw_ops = { 376496458233SKamal Heib .alloc_mw = mlx5_ib_alloc_mw, 376596458233SKamal Heib .dealloc_mw = mlx5_ib_dealloc_mw, 3766d18bb3e1SLeon Romanovsky 3767d18bb3e1SLeon Romanovsky INIT_RDMA_OBJ_SIZE(ib_mw, mlx5_ib_mw, ibmw), 376896458233SKamal Heib }; 376996458233SKamal Heib 377096458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_xrc_ops = { 377196458233SKamal Heib .alloc_xrcd = mlx5_ib_alloc_xrcd, 377296458233SKamal Heib .dealloc_xrcd = mlx5_ib_dealloc_xrcd, 377328ad5f65SLeon Romanovsky 377428ad5f65SLeon Romanovsky INIT_RDMA_OBJ_SIZE(ib_xrcd, mlx5_ib_xrcd, ibxrcd), 377596458233SKamal Heib }; 377696458233SKamal Heib 3777f164be8cSYishai Hadas static int mlx5_ib_init_var_table(struct mlx5_ib_dev *dev) 3778f164be8cSYishai Hadas { 3779f164be8cSYishai Hadas struct mlx5_core_dev *mdev = dev->mdev; 3780f164be8cSYishai Hadas struct mlx5_var_table *var_table = &dev->var_table; 3781f164be8cSYishai Hadas u8 log_doorbell_bar_size; 3782f164be8cSYishai Hadas u8 log_doorbell_stride; 3783f164be8cSYishai Hadas u64 bar_size; 3784f164be8cSYishai Hadas 3785f164be8cSYishai Hadas log_doorbell_bar_size = MLX5_CAP_DEV_VDPA_EMULATION(mdev, 3786f164be8cSYishai Hadas log_doorbell_bar_size); 3787f164be8cSYishai Hadas log_doorbell_stride = MLX5_CAP_DEV_VDPA_EMULATION(mdev, 3788f164be8cSYishai Hadas log_doorbell_stride); 3789f164be8cSYishai Hadas var_table->hw_start_addr = dev->mdev->bar_addr + 3790f164be8cSYishai Hadas MLX5_CAP64_DEV_VDPA_EMULATION(mdev, 3791f164be8cSYishai Hadas doorbell_bar_offset); 3792f164be8cSYishai Hadas bar_size = (1ULL << log_doorbell_bar_size) * 4096; 3793f164be8cSYishai Hadas var_table->stride_size = 1ULL << log_doorbell_stride; 379491b74bf5SAlexander Lobakin var_table->num_var_hw_entries = div_u64(bar_size, 379591b74bf5SAlexander Lobakin var_table->stride_size); 3796f164be8cSYishai Hadas mutex_init(&var_table->bitmap_lock); 3797f164be8cSYishai Hadas var_table->bitmap = bitmap_zalloc(var_table->num_var_hw_entries, 3798f164be8cSYishai Hadas GFP_KERNEL); 3799f164be8cSYishai Hadas return (var_table->bitmap) ? 0 : -ENOMEM; 3800f164be8cSYishai Hadas } 3801f164be8cSYishai Hadas 3802f164be8cSYishai Hadas static void mlx5_ib_stage_caps_cleanup(struct mlx5_ib_dev *dev) 3803f164be8cSYishai Hadas { 3804f164be8cSYishai Hadas bitmap_free(dev->var_table.bitmap); 3805f164be8cSYishai Hadas } 3806f164be8cSYishai Hadas 3807fb652d32SMark Bloch static int mlx5_ib_stage_caps_init(struct mlx5_ib_dev *dev) 380816c1975fSMark Bloch { 380916c1975fSMark Bloch struct mlx5_core_dev *mdev = dev->mdev; 381016c1975fSMark Bloch int err; 381116c1975fSMark Bloch 3812f6a8a19bSDenis Drozdov if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads) && 3813f6a8a19bSDenis Drozdov IS_ENABLED(CONFIG_MLX5_CORE_IPOIB)) 381496458233SKamal Heib ib_set_device_ops(&dev->ib_dev, 381596458233SKamal Heib &mlx5_ib_dev_ipoib_enhanced_ops); 38168e959601SNiranjana Vishwanathapura 381796458233SKamal Heib if (mlx5_core_is_pf(mdev)) 381896458233SKamal Heib ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_sriov_ops); 38197c2344c3SMaor Gottlieb 38206e8484c5SMax Gurtovoy dev->umr_fence = mlx5_get_umr_fence(MLX5_CAP_GEN(mdev, umr_fence)); 38216e8484c5SMax Gurtovoy 382244ce37bcSJason Gunthorpe if (MLX5_CAP_GEN(mdev, imaicl)) 382396458233SKamal Heib ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_mw_ops); 3824d2370e0aSMatan Barak 382544ce37bcSJason Gunthorpe if (MLX5_CAP_GEN(mdev, xrc)) 382696458233SKamal Heib ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_xrc_ops); 3827e126ba97SEli Cohen 382825c13324SAriel Levkovich if (MLX5_CAP_DEV_MEM(mdev, memic) || 382925c13324SAriel Levkovich MLX5_CAP_GEN_64(dev->mdev, general_obj_types) & 383025c13324SAriel Levkovich MLX5_GENERAL_OBJ_TYPES_CAP_SW_ICM) 383196458233SKamal Heib ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_dm_ops); 383224da0016SAriel Levkovich 383396458233SKamal Heib ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_ops); 383481e30880SYishai Hadas 383536e235c8SJason Gunthorpe if (IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS)) 383636e235c8SJason Gunthorpe dev->ib_dev.driver_def = mlx5_ib_defs; 383781e30880SYishai Hadas 3838e126ba97SEli Cohen err = init_node_data(dev); 3839e126ba97SEli Cohen if (err) 384016c1975fSMark Bloch return err; 3841e126ba97SEli Cohen 3842c8b89924SMark Bloch if ((MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_ETH) && 3843e7996a9aSJason Gunthorpe (MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) || 3844e7996a9aSJason Gunthorpe MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc))) 3845a560f1d9SMark Bloch mutex_init(&dev->lb.mutex); 3846c8b89924SMark Bloch 3847f164be8cSYishai Hadas if (MLX5_CAP_GEN_64(dev->mdev, general_obj_types) & 3848f164be8cSYishai Hadas MLX5_GENERAL_OBJ_TYPES_CAP_VIRTIO_NET_Q) { 3849f164be8cSYishai Hadas err = mlx5_ib_init_var_table(dev); 3850f164be8cSYishai Hadas if (err) 3851f164be8cSYishai Hadas return err; 3852f164be8cSYishai Hadas } 3853f164be8cSYishai Hadas 385496e2fd73SLeon Romanovsky dev->ib_dev.use_cq_dim = true; 385596e2fd73SLeon Romanovsky 385616c1975fSMark Bloch return 0; 385716c1975fSMark Bloch } 385816c1975fSMark Bloch 385996458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_port_ops = { 386096458233SKamal Heib .get_port_immutable = mlx5_port_immutable, 386196458233SKamal Heib .query_port = mlx5_ib_query_port, 386296458233SKamal Heib }; 386396458233SKamal Heib 38648e6efa3aSMark Bloch static int mlx5_ib_stage_non_default_cb(struct mlx5_ib_dev *dev) 386516c1975fSMark Bloch { 386696458233SKamal Heib ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_port_ops); 38678e6efa3aSMark Bloch return 0; 38688e6efa3aSMark Bloch } 38698e6efa3aSMark Bloch 387096458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_port_rep_ops = { 387196458233SKamal Heib .get_port_immutable = mlx5_port_rep_immutable, 387296458233SKamal Heib .query_port = mlx5_ib_rep_query_port, 3873d6fd59e1SParav Pandit .query_pkey = mlx5_ib_rep_query_pkey, 387496458233SKamal Heib }; 387596458233SKamal Heib 3876b5a498baSMichael Guralnik static int mlx5_ib_stage_raw_eth_non_default_cb(struct mlx5_ib_dev *dev) 38778e6efa3aSMark Bloch { 387896458233SKamal Heib ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_port_rep_ops); 38798e6efa3aSMark Bloch return 0; 38808e6efa3aSMark Bloch } 38818e6efa3aSMark Bloch 388296458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_common_roce_ops = { 388396458233SKamal Heib .create_rwq_ind_table = mlx5_ib_create_rwq_ind_table, 388496458233SKamal Heib .create_wq = mlx5_ib_create_wq, 388596458233SKamal Heib .destroy_rwq_ind_table = mlx5_ib_destroy_rwq_ind_table, 388696458233SKamal Heib .destroy_wq = mlx5_ib_destroy_wq, 388796458233SKamal Heib .get_netdev = mlx5_ib_get_netdev, 388896458233SKamal Heib .modify_wq = mlx5_ib_modify_wq, 3889c0a6b5ecSLeon Romanovsky 3890c0a6b5ecSLeon Romanovsky INIT_RDMA_OBJ_SIZE(ib_rwq_ind_table, mlx5_ib_rwq_ind_table, 3891c0a6b5ecSLeon Romanovsky ib_rwq_ind_tbl), 389296458233SKamal Heib }; 389396458233SKamal Heib 38941e2b5a90SLeon Romanovsky static int mlx5_ib_roce_init(struct mlx5_ib_dev *dev) 38958e6efa3aSMark Bloch { 38961e2b5a90SLeon Romanovsky struct mlx5_core_dev *mdev = dev->mdev; 38971e2b5a90SLeon Romanovsky enum rdma_link_layer ll; 38981e2b5a90SLeon Romanovsky int port_type_cap; 38991fb7f897SMark Bloch u32 port_num = 0; 39001e2b5a90SLeon Romanovsky int err; 39017fd8aefbSDaniel Jurgens 39021e2b5a90SLeon Romanovsky port_type_cap = MLX5_CAP_GEN(mdev, port_type); 39031e2b5a90SLeon Romanovsky ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap); 39041e2b5a90SLeon Romanovsky 39051e2b5a90SLeon Romanovsky if (ll == IB_LINK_LAYER_ETHERNET) { 390696458233SKamal Heib ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_common_roce_ops); 39078e6efa3aSMark Bloch 3908e3f1ed1fSLeon Romanovsky port_num = mlx5_core_native_port_num(dev->mdev) - 1; 3909e3f1ed1fSLeon Romanovsky 391026628e2dSMark Bloch /* Register only for native ports */ 39111e2b5a90SLeon Romanovsky err = mlx5_add_netdev_notifier(dev, port_num); 3912c446d9daSMark Bloch if (err) 39138e6efa3aSMark Bloch return err; 39148e6efa3aSMark Bloch 3915e3f1ed1fSLeon Romanovsky err = mlx5_enable_eth(dev); 39168e6efa3aSMark Bloch if (err) 39178e6efa3aSMark Bloch goto cleanup; 39188e6efa3aSMark Bloch } 39198e6efa3aSMark Bloch 392016c1975fSMark Bloch return 0; 39218e6efa3aSMark Bloch cleanup: 39221e2b5a90SLeon Romanovsky mlx5_remove_netdev_notifier(dev, port_num); 39238e6efa3aSMark Bloch return err; 392445bded2cSKamal Heib } 39256aec21f6SHaggai Eran 39261e2b5a90SLeon Romanovsky static void mlx5_ib_roce_cleanup(struct mlx5_ib_dev *dev) 392716c1975fSMark Bloch { 392816c1975fSMark Bloch struct mlx5_core_dev *mdev = dev->mdev; 392916c1975fSMark Bloch enum rdma_link_layer ll; 393016c1975fSMark Bloch int port_type_cap; 39311fb7f897SMark Bloch u32 port_num; 39324a2da0b8SParav Pandit 393316c1975fSMark Bloch port_type_cap = MLX5_CAP_GEN(mdev, port_type); 393416c1975fSMark Bloch ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap); 393516c1975fSMark Bloch 393616c1975fSMark Bloch if (ll == IB_LINK_LAYER_ETHERNET) { 393716c1975fSMark Bloch mlx5_disable_eth(dev); 393816c1975fSMark Bloch 39391e2b5a90SLeon Romanovsky port_num = mlx5_core_native_port_num(dev->mdev) - 1; 39401e2b5a90SLeon Romanovsky mlx5_remove_netdev_notifier(dev, port_num); 394116c1975fSMark Bloch } 394216c1975fSMark Bloch } 394316c1975fSMark Bloch 394416c1975fSMark Bloch static int mlx5_ib_stage_cong_debugfs_init(struct mlx5_ib_dev *dev) 394516c1975fSMark Bloch { 394673eb8f03SGreg Kroah-Hartman mlx5_ib_init_cong_debugfs(dev, 3947a9e546e7SParav Pandit mlx5_core_native_port_num(dev->mdev) - 1); 394873eb8f03SGreg Kroah-Hartman return 0; 394916c1975fSMark Bloch } 395016c1975fSMark Bloch 395116c1975fSMark Bloch static void mlx5_ib_stage_cong_debugfs_cleanup(struct mlx5_ib_dev *dev) 395216c1975fSMark Bloch { 3953a9e546e7SParav Pandit mlx5_ib_cleanup_cong_debugfs(dev, 3954a9e546e7SParav Pandit mlx5_core_native_port_num(dev->mdev) - 1); 395516c1975fSMark Bloch } 395616c1975fSMark Bloch 395716c1975fSMark Bloch static int mlx5_ib_stage_uar_init(struct mlx5_ib_dev *dev) 395816c1975fSMark Bloch { 39595fe9dec0SEli Cohen dev->mdev->priv.uar = mlx5_get_uars_page(dev->mdev); 3960444261caSLeon Romanovsky return PTR_ERR_OR_ZERO(dev->mdev->priv.uar); 396116c1975fSMark Bloch } 396216c1975fSMark Bloch 396316c1975fSMark Bloch static void mlx5_ib_stage_uar_cleanup(struct mlx5_ib_dev *dev) 396416c1975fSMark Bloch { 396516c1975fSMark Bloch mlx5_put_uars_page(dev->mdev, dev->mdev->priv.uar); 396616c1975fSMark Bloch } 396716c1975fSMark Bloch 3968fb652d32SMark Bloch static int mlx5_ib_stage_bfrag_init(struct mlx5_ib_dev *dev) 396916c1975fSMark Bloch { 397016c1975fSMark Bloch int err; 39715fe9dec0SEli Cohen 39725fe9dec0SEli Cohen err = mlx5_alloc_bfreg(dev->mdev, &dev->bfreg, false, false); 39735fe9dec0SEli Cohen if (err) 397416c1975fSMark Bloch return err; 39755fe9dec0SEli Cohen 39765fe9dec0SEli Cohen err = mlx5_alloc_bfreg(dev->mdev, &dev->fp_bfreg, false, true); 39775fe9dec0SEli Cohen if (err) 39781c3aa6bdSMark Bloch mlx5_free_bfreg(dev->mdev, &dev->bfreg); 39795fe9dec0SEli Cohen 398016c1975fSMark Bloch return err; 398116c1975fSMark Bloch } 39820837e86aSMark Bloch 3983fb652d32SMark Bloch static void mlx5_ib_stage_bfrag_cleanup(struct mlx5_ib_dev *dev) 398416c1975fSMark Bloch { 398516c1975fSMark Bloch mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg); 398616c1975fSMark Bloch mlx5_free_bfreg(dev->mdev, &dev->bfreg); 398716c1975fSMark Bloch } 3988e126ba97SEli Cohen 3989fb652d32SMark Bloch static int mlx5_ib_stage_ib_reg_init(struct mlx5_ib_dev *dev) 399016c1975fSMark Bloch { 3991e349f858SJason Gunthorpe const char *name; 3992e349f858SJason Gunthorpe 3993c446d9daSMark Bloch if (!mlx5_lag_is_active(dev->mdev)) 3994e349f858SJason Gunthorpe name = "mlx5_%d"; 3995e349f858SJason Gunthorpe else 3996e349f858SJason Gunthorpe name = "mlx5_bond_%d"; 3997e0477b34SJason Gunthorpe return ib_register_device(&dev->ib_dev, name, &dev->mdev->pdev->dev); 399816c1975fSMark Bloch } 399916c1975fSMark Bloch 4000fb652d32SMark Bloch static void mlx5_ib_stage_pre_ib_reg_umr_cleanup(struct mlx5_ib_dev *dev) 400142cea83fSMark Bloch { 40021e2b5a90SLeon Romanovsky int err; 40031e2b5a90SLeon Romanovsky 40041e2b5a90SLeon Romanovsky err = mlx5_mr_cache_cleanup(dev); 40051e2b5a90SLeon Romanovsky if (err) 40061e2b5a90SLeon Romanovsky mlx5_ib_warn(dev, "mr cache cleanup failed\n"); 40071e2b5a90SLeon Romanovsky 4008*04876c12SAharon Landau mlx5r_umr_resource_cleanup(dev); 400942cea83fSMark Bloch } 401042cea83fSMark Bloch 4011fb652d32SMark Bloch static void mlx5_ib_stage_ib_reg_cleanup(struct mlx5_ib_dev *dev) 401216c1975fSMark Bloch { 401316c1975fSMark Bloch ib_unregister_device(&dev->ib_dev); 401416c1975fSMark Bloch } 401516c1975fSMark Bloch 4016fb652d32SMark Bloch static int mlx5_ib_stage_post_ib_reg_umr_init(struct mlx5_ib_dev *dev) 401716c1975fSMark Bloch { 40181e2b5a90SLeon Romanovsky int ret; 40191e2b5a90SLeon Romanovsky 4020*04876c12SAharon Landau ret = mlx5r_umr_resource_init(dev); 4021*04876c12SAharon Landau if (ret) 4022*04876c12SAharon Landau return ret; 40231e2b5a90SLeon Romanovsky 40241e2b5a90SLeon Romanovsky ret = mlx5_mr_cache_init(dev); 40251e2b5a90SLeon Romanovsky if (ret) { 40261e2b5a90SLeon Romanovsky mlx5_ib_warn(dev, "mr cache init failed %d\n", ret); 4027*04876c12SAharon Landau mlx5r_umr_resource_cleanup(dev); 40281e2b5a90SLeon Romanovsky } 40291e2b5a90SLeon Romanovsky return ret; 403016c1975fSMark Bloch } 403116c1975fSMark Bloch 403216c1975fSMark Bloch static int mlx5_ib_stage_delay_drop_init(struct mlx5_ib_dev *dev) 403316c1975fSMark Bloch { 40341e2b5a90SLeon Romanovsky struct dentry *root; 403503404e8aSMaor Gottlieb 40361e2b5a90SLeon Romanovsky if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP)) 40371e2b5a90SLeon Romanovsky return 0; 40381e2b5a90SLeon Romanovsky 40391e2b5a90SLeon Romanovsky mutex_init(&dev->delay_drop.lock); 40401e2b5a90SLeon Romanovsky dev->delay_drop.dev = dev; 40411e2b5a90SLeon Romanovsky dev->delay_drop.activate = false; 40421e2b5a90SLeon Romanovsky dev->delay_drop.timeout = MLX5_MAX_DELAY_DROP_TIMEOUT_MS * 1000; 40431e2b5a90SLeon Romanovsky INIT_WORK(&dev->delay_drop.delay_drop_work, delay_drop_handler); 40441e2b5a90SLeon Romanovsky atomic_set(&dev->delay_drop.rqs_cnt, 0); 40451e2b5a90SLeon Romanovsky atomic_set(&dev->delay_drop.events_cnt, 0); 40461e2b5a90SLeon Romanovsky 40471e2b5a90SLeon Romanovsky if (!mlx5_debugfs_root) 40481e2b5a90SLeon Romanovsky return 0; 40491e2b5a90SLeon Romanovsky 405066771a1cSMoshe Shemesh root = debugfs_create_dir("delay_drop", mlx5_debugfs_get_dev_root(dev->mdev)); 40511e2b5a90SLeon Romanovsky dev->delay_drop.dir_debugfs = root; 40521e2b5a90SLeon Romanovsky 40531e2b5a90SLeon Romanovsky debugfs_create_atomic_t("num_timeout_events", 0400, root, 40541e2b5a90SLeon Romanovsky &dev->delay_drop.events_cnt); 40551e2b5a90SLeon Romanovsky debugfs_create_atomic_t("num_rqs", 0400, root, 40561e2b5a90SLeon Romanovsky &dev->delay_drop.rqs_cnt); 40571e2b5a90SLeon Romanovsky debugfs_create_file("timeout", 0600, root, &dev->delay_drop, 40581e2b5a90SLeon Romanovsky &fops_delay_drop_timeout); 405916c1975fSMark Bloch return 0; 406016c1975fSMark Bloch } 406116c1975fSMark Bloch 406216c1975fSMark Bloch static void mlx5_ib_stage_delay_drop_cleanup(struct mlx5_ib_dev *dev) 406316c1975fSMark Bloch { 40641e2b5a90SLeon Romanovsky if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP)) 40651e2b5a90SLeon Romanovsky return; 40661e2b5a90SLeon Romanovsky 40671e2b5a90SLeon Romanovsky cancel_work_sync(&dev->delay_drop.delay_drop_work); 40681e2b5a90SLeon Romanovsky if (!dev->delay_drop.dir_debugfs) 40691e2b5a90SLeon Romanovsky return; 40701e2b5a90SLeon Romanovsky 40711e2b5a90SLeon Romanovsky debugfs_remove_recursive(dev->delay_drop.dir_debugfs); 40721e2b5a90SLeon Romanovsky dev->delay_drop.dir_debugfs = NULL; 407316c1975fSMark Bloch } 407416c1975fSMark Bloch 4075df097a27SSaeed Mahameed static int mlx5_ib_stage_dev_notifier_init(struct mlx5_ib_dev *dev) 4076df097a27SSaeed Mahameed { 4077df097a27SSaeed Mahameed dev->mdev_events.notifier_call = mlx5_ib_event; 4078df097a27SSaeed Mahameed mlx5_notifier_register(dev->mdev, &dev->mdev_events); 4079df097a27SSaeed Mahameed return 0; 4080df097a27SSaeed Mahameed } 4081df097a27SSaeed Mahameed 4082df097a27SSaeed Mahameed static void mlx5_ib_stage_dev_notifier_cleanup(struct mlx5_ib_dev *dev) 4083df097a27SSaeed Mahameed { 4084df097a27SSaeed Mahameed mlx5_notifier_unregister(dev->mdev, &dev->mdev_events); 4085df097a27SSaeed Mahameed } 4086df097a27SSaeed Mahameed 4087b5ca15adSMark Bloch void __mlx5_ib_remove(struct mlx5_ib_dev *dev, 408816c1975fSMark Bloch const struct mlx5_ib_profile *profile, 408916c1975fSMark Bloch int stage) 409016c1975fSMark Bloch { 40914cca96a8SParav Pandit dev->ib_active = false; 40924cca96a8SParav Pandit 409316c1975fSMark Bloch /* Number of stages to cleanup */ 409416c1975fSMark Bloch while (stage) { 409516c1975fSMark Bloch stage--; 409616c1975fSMark Bloch if (profile->stage[stage].cleanup) 409716c1975fSMark Bloch profile->stage[stage].cleanup(dev); 409816c1975fSMark Bloch } 40994a6dc855SMark Bloch 4100da796ccbSMark Bloch kfree(dev->port); 41014a6dc855SMark Bloch ib_dealloc_device(&dev->ib_dev); 410216c1975fSMark Bloch } 410316c1975fSMark Bloch 410493f82444SLeon Romanovsky int __mlx5_ib_add(struct mlx5_ib_dev *dev, 410516c1975fSMark Bloch const struct mlx5_ib_profile *profile) 410616c1975fSMark Bloch { 410716c1975fSMark Bloch int err; 410816c1975fSMark Bloch int i; 410916c1975fSMark Bloch 41108d93efb8SMark Bloch dev->profile = profile; 41118d93efb8SMark Bloch 411216c1975fSMark Bloch for (i = 0; i < MLX5_IB_STAGE_MAX; i++) { 411316c1975fSMark Bloch if (profile->stage[i].init) { 411416c1975fSMark Bloch err = profile->stage[i].init(dev); 411516c1975fSMark Bloch if (err) 411616c1975fSMark Bloch goto err_out; 411716c1975fSMark Bloch } 411816c1975fSMark Bloch } 411916c1975fSMark Bloch 4120e126ba97SEli Cohen dev->ib_active = true; 412193f82444SLeon Romanovsky return 0; 4122e126ba97SEli Cohen 412316c1975fSMark Bloch err_out: 412493f82444SLeon Romanovsky /* Clean up stages which were initialized */ 412593f82444SLeon Romanovsky while (i) { 412693f82444SLeon Romanovsky i--; 412793f82444SLeon Romanovsky if (profile->stage[i].cleanup) 412893f82444SLeon Romanovsky profile->stage[i].cleanup(dev); 412993f82444SLeon Romanovsky } 413093f82444SLeon Romanovsky return -ENOMEM; 4131e126ba97SEli Cohen } 4132e126ba97SEli Cohen 413316c1975fSMark Bloch static const struct mlx5_ib_profile pf_profile = { 413416c1975fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_INIT, 413516c1975fSMark Bloch mlx5_ib_stage_init_init, 413616c1975fSMark Bloch mlx5_ib_stage_init_cleanup), 4137f7c4ffdaSLeon Romanovsky STAGE_CREATE(MLX5_IB_STAGE_FS, 4138f7c4ffdaSLeon Romanovsky mlx5_ib_fs_init, 4139f7c4ffdaSLeon Romanovsky mlx5_ib_fs_cleanup), 414016c1975fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_CAPS, 414116c1975fSMark Bloch mlx5_ib_stage_caps_init, 4142f164be8cSYishai Hadas mlx5_ib_stage_caps_cleanup), 41438e6efa3aSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB, 41448e6efa3aSMark Bloch mlx5_ib_stage_non_default_cb, 41458e6efa3aSMark Bloch NULL), 414616c1975fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_ROCE, 41471e2b5a90SLeon Romanovsky mlx5_ib_roce_init, 41481e2b5a90SLeon Romanovsky mlx5_ib_roce_cleanup), 4149333fbaa0SLeon Romanovsky STAGE_CREATE(MLX5_IB_STAGE_QP, 4150333fbaa0SLeon Romanovsky mlx5_init_qp_table, 4151333fbaa0SLeon Romanovsky mlx5_cleanup_qp_table), 4152f3da6577SLeon Romanovsky STAGE_CREATE(MLX5_IB_STAGE_SRQ, 4153f3da6577SLeon Romanovsky mlx5_init_srq_table, 4154f3da6577SLeon Romanovsky mlx5_cleanup_srq_table), 415516c1975fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES, 41561e2b5a90SLeon Romanovsky mlx5_ib_dev_res_init, 41571e2b5a90SLeon Romanovsky mlx5_ib_dev_res_cleanup), 4158df097a27SSaeed Mahameed STAGE_CREATE(MLX5_IB_STAGE_DEVICE_NOTIFIER, 4159df097a27SSaeed Mahameed mlx5_ib_stage_dev_notifier_init, 4160df097a27SSaeed Mahameed mlx5_ib_stage_dev_notifier_cleanup), 416116c1975fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_ODP, 41621e2b5a90SLeon Romanovsky mlx5_ib_odp_init_one, 41631e2b5a90SLeon Romanovsky mlx5_ib_odp_cleanup_one), 416416c1975fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_COUNTERS, 416564825827SLeon Romanovsky mlx5_ib_counters_init, 416664825827SLeon Romanovsky mlx5_ib_counters_cleanup), 416716c1975fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_CONG_DEBUGFS, 416816c1975fSMark Bloch mlx5_ib_stage_cong_debugfs_init, 416916c1975fSMark Bloch mlx5_ib_stage_cong_debugfs_cleanup), 417016c1975fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_UAR, 417116c1975fSMark Bloch mlx5_ib_stage_uar_init, 417216c1975fSMark Bloch mlx5_ib_stage_uar_cleanup), 417316c1975fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_BFREG, 417416c1975fSMark Bloch mlx5_ib_stage_bfrag_init, 417516c1975fSMark Bloch mlx5_ib_stage_bfrag_cleanup), 417642cea83fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR, 417742cea83fSMark Bloch NULL, 417842cea83fSMark Bloch mlx5_ib_stage_pre_ib_reg_umr_cleanup), 417981773ce5SLeon Romanovsky STAGE_CREATE(MLX5_IB_STAGE_WHITELIST_UID, 4180d8b7515eSLeon Romanovsky mlx5_ib_devx_init, 4181d8b7515eSLeon Romanovsky mlx5_ib_devx_cleanup), 418216c1975fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_IB_REG, 418316c1975fSMark Bloch mlx5_ib_stage_ib_reg_init, 418416c1975fSMark Bloch mlx5_ib_stage_ib_reg_cleanup), 418542cea83fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR, 418642cea83fSMark Bloch mlx5_ib_stage_post_ib_reg_umr_init, 418742cea83fSMark Bloch NULL), 418816c1975fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_DELAY_DROP, 418916c1975fSMark Bloch mlx5_ib_stage_delay_drop_init, 419016c1975fSMark Bloch mlx5_ib_stage_delay_drop_cleanup), 4191b572ebe6SLeon Romanovsky STAGE_CREATE(MLX5_IB_STAGE_RESTRACK, 4192b572ebe6SLeon Romanovsky mlx5_ib_restrack_init, 4193b572ebe6SLeon Romanovsky NULL), 419416c1975fSMark Bloch }; 419516c1975fSMark Bloch 4196b5a498baSMichael Guralnik const struct mlx5_ib_profile raw_eth_profile = { 4197b5ca15adSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_INIT, 4198b5ca15adSMark Bloch mlx5_ib_stage_init_init, 4199b5ca15adSMark Bloch mlx5_ib_stage_init_cleanup), 4200f7c4ffdaSLeon Romanovsky STAGE_CREATE(MLX5_IB_STAGE_FS, 4201f7c4ffdaSLeon Romanovsky mlx5_ib_fs_init, 4202f7c4ffdaSLeon Romanovsky mlx5_ib_fs_cleanup), 4203b5ca15adSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_CAPS, 4204b5ca15adSMark Bloch mlx5_ib_stage_caps_init, 4205f164be8cSYishai Hadas mlx5_ib_stage_caps_cleanup), 4206b5ca15adSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB, 4207b5a498baSMichael Guralnik mlx5_ib_stage_raw_eth_non_default_cb, 4208b5ca15adSMark Bloch NULL), 4209b5ca15adSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_ROCE, 42101e2b5a90SLeon Romanovsky mlx5_ib_roce_init, 42111e2b5a90SLeon Romanovsky mlx5_ib_roce_cleanup), 4212333fbaa0SLeon Romanovsky STAGE_CREATE(MLX5_IB_STAGE_QP, 4213333fbaa0SLeon Romanovsky mlx5_init_qp_table, 4214333fbaa0SLeon Romanovsky mlx5_cleanup_qp_table), 4215f3da6577SLeon Romanovsky STAGE_CREATE(MLX5_IB_STAGE_SRQ, 4216f3da6577SLeon Romanovsky mlx5_init_srq_table, 4217f3da6577SLeon Romanovsky mlx5_cleanup_srq_table), 4218b5ca15adSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES, 42191e2b5a90SLeon Romanovsky mlx5_ib_dev_res_init, 42201e2b5a90SLeon Romanovsky mlx5_ib_dev_res_cleanup), 4221df097a27SSaeed Mahameed STAGE_CREATE(MLX5_IB_STAGE_DEVICE_NOTIFIER, 4222df097a27SSaeed Mahameed mlx5_ib_stage_dev_notifier_init, 4223df097a27SSaeed Mahameed mlx5_ib_stage_dev_notifier_cleanup), 4224b5ca15adSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_COUNTERS, 422564825827SLeon Romanovsky mlx5_ib_counters_init, 422664825827SLeon Romanovsky mlx5_ib_counters_cleanup), 422779db784eSParav Pandit STAGE_CREATE(MLX5_IB_STAGE_CONG_DEBUGFS, 422879db784eSParav Pandit mlx5_ib_stage_cong_debugfs_init, 422979db784eSParav Pandit mlx5_ib_stage_cong_debugfs_cleanup), 4230b5ca15adSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_UAR, 4231b5ca15adSMark Bloch mlx5_ib_stage_uar_init, 4232b5ca15adSMark Bloch mlx5_ib_stage_uar_cleanup), 4233b5ca15adSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_BFREG, 4234b5ca15adSMark Bloch mlx5_ib_stage_bfrag_init, 4235b5ca15adSMark Bloch mlx5_ib_stage_bfrag_cleanup), 423603fe2debSDavid S. Miller STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR, 423703fe2debSDavid S. Miller NULL, 423803fe2debSDavid S. Miller mlx5_ib_stage_pre_ib_reg_umr_cleanup), 42397f575103SMark Bloch STAGE_CREATE(MLX5_IB_STAGE_WHITELIST_UID, 4240d8b7515eSLeon Romanovsky mlx5_ib_devx_init, 4241d8b7515eSLeon Romanovsky mlx5_ib_devx_cleanup), 4242b5ca15adSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_IB_REG, 4243b5ca15adSMark Bloch mlx5_ib_stage_ib_reg_init, 4244b5ca15adSMark Bloch mlx5_ib_stage_ib_reg_cleanup), 424503fe2debSDavid S. Miller STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR, 424603fe2debSDavid S. Miller mlx5_ib_stage_post_ib_reg_umr_init, 424703fe2debSDavid S. Miller NULL), 4248b572ebe6SLeon Romanovsky STAGE_CREATE(MLX5_IB_STAGE_RESTRACK, 4249b572ebe6SLeon Romanovsky mlx5_ib_restrack_init, 4250b572ebe6SLeon Romanovsky NULL), 4251b5ca15adSMark Bloch }; 4252b5ca15adSMark Bloch 425393f82444SLeon Romanovsky static int mlx5r_mp_probe(struct auxiliary_device *adev, 425493f82444SLeon Romanovsky const struct auxiliary_device_id *id) 425532f69e4bSDaniel Jurgens { 425693f82444SLeon Romanovsky struct mlx5_adev *idev = container_of(adev, struct mlx5_adev, adev); 425793f82444SLeon Romanovsky struct mlx5_core_dev *mdev = idev->mdev; 425832f69e4bSDaniel Jurgens struct mlx5_ib_multiport_info *mpi; 425932f69e4bSDaniel Jurgens struct mlx5_ib_dev *dev; 426032f69e4bSDaniel Jurgens bool bound = false; 426132f69e4bSDaniel Jurgens int err; 426232f69e4bSDaniel Jurgens 426332f69e4bSDaniel Jurgens mpi = kzalloc(sizeof(*mpi), GFP_KERNEL); 426432f69e4bSDaniel Jurgens if (!mpi) 426593f82444SLeon Romanovsky return -ENOMEM; 426632f69e4bSDaniel Jurgens 426732f69e4bSDaniel Jurgens mpi->mdev = mdev; 426832f69e4bSDaniel Jurgens err = mlx5_query_nic_vport_system_image_guid(mdev, 426932f69e4bSDaniel Jurgens &mpi->sys_image_guid); 427032f69e4bSDaniel Jurgens if (err) { 427132f69e4bSDaniel Jurgens kfree(mpi); 427293f82444SLeon Romanovsky return err; 427332f69e4bSDaniel Jurgens } 427432f69e4bSDaniel Jurgens 427532f69e4bSDaniel Jurgens mutex_lock(&mlx5_ib_multiport_mutex); 427632f69e4bSDaniel Jurgens list_for_each_entry(dev, &mlx5_ib_dev_list, ib_dev_list) { 427732f69e4bSDaniel Jurgens if (dev->sys_image_guid == mpi->sys_image_guid) 427832f69e4bSDaniel Jurgens bound = mlx5_ib_bind_slave_port(dev, mpi); 427932f69e4bSDaniel Jurgens 428032f69e4bSDaniel Jurgens if (bound) { 428132f69e4bSDaniel Jurgens rdma_roce_rescan_device(&dev->ib_dev); 428297f30d32SMaor Gottlieb mpi->ibdev->ib_active = true; 428332f69e4bSDaniel Jurgens break; 428432f69e4bSDaniel Jurgens } 428532f69e4bSDaniel Jurgens } 428632f69e4bSDaniel Jurgens 428732f69e4bSDaniel Jurgens if (!bound) { 428832f69e4bSDaniel Jurgens list_add_tail(&mpi->list, &mlx5_ib_unaffiliated_port_list); 4289c42260f1SVu Pham dev_dbg(mdev->device, 4290c42260f1SVu Pham "no suitable IB device found to bind to, added to unaffiliated list.\n"); 429132f69e4bSDaniel Jurgens } 429232f69e4bSDaniel Jurgens mutex_unlock(&mlx5_ib_multiport_mutex); 429332f69e4bSDaniel Jurgens 429427963d3dSDavid E. Box auxiliary_set_drvdata(adev, mpi); 429593f82444SLeon Romanovsky return 0; 429632f69e4bSDaniel Jurgens } 429732f69e4bSDaniel Jurgens 429893f82444SLeon Romanovsky static void mlx5r_mp_remove(struct auxiliary_device *adev) 429916c1975fSMark Bloch { 430093f82444SLeon Romanovsky struct mlx5_ib_multiport_info *mpi; 430193f82444SLeon Romanovsky 430227963d3dSDavid E. Box mpi = auxiliary_get_drvdata(adev); 430393f82444SLeon Romanovsky mutex_lock(&mlx5_ib_multiport_mutex); 430493f82444SLeon Romanovsky if (mpi->ibdev) 430593f82444SLeon Romanovsky mlx5_ib_unbind_slave_port(mpi->ibdev, mpi); 4306da78fe5fSMaor Gottlieb else 430793f82444SLeon Romanovsky list_del(&mpi->list); 430893f82444SLeon Romanovsky mutex_unlock(&mlx5_ib_multiport_mutex); 430993f82444SLeon Romanovsky kfree(mpi); 431093f82444SLeon Romanovsky } 431193f82444SLeon Romanovsky 431293f82444SLeon Romanovsky static int mlx5r_probe(struct auxiliary_device *adev, 431393f82444SLeon Romanovsky const struct auxiliary_device_id *id) 431493f82444SLeon Romanovsky { 431593f82444SLeon Romanovsky struct mlx5_adev *idev = container_of(adev, struct mlx5_adev, adev); 431693f82444SLeon Romanovsky struct mlx5_core_dev *mdev = idev->mdev; 431794de879cSMichael Guralnik const struct mlx5_ib_profile *profile; 431893f82444SLeon Romanovsky int port_type_cap, num_ports, ret; 431932f69e4bSDaniel Jurgens enum rdma_link_layer ll; 4320b5ca15adSMark Bloch struct mlx5_ib_dev *dev; 4321f0666f1fSBodong Wang 432232f69e4bSDaniel Jurgens port_type_cap = MLX5_CAP_GEN(mdev, port_type); 432332f69e4bSDaniel Jurgens ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap); 432432f69e4bSDaniel Jurgens 4325da796ccbSMark Bloch num_ports = max(MLX5_CAP_GEN(mdev, num_ports), 4326da796ccbSMark Bloch MLX5_CAP_GEN(mdev, num_vhca_ports)); 4327459cc69fSLeon Romanovsky dev = ib_alloc_device(mlx5_ib_dev, ib_dev); 4328b5ca15adSMark Bloch if (!dev) 432993f82444SLeon Romanovsky return -ENOMEM; 4330da796ccbSMark Bloch dev->port = kcalloc(num_ports, sizeof(*dev->port), 4331da796ccbSMark Bloch GFP_KERNEL); 4332da796ccbSMark Bloch if (!dev->port) { 4333a5c9c299SParav Pandit ib_dealloc_device(&dev->ib_dev); 433493f82444SLeon Romanovsky return -ENOMEM; 4335da796ccbSMark Bloch } 4336b5ca15adSMark Bloch 4337b5ca15adSMark Bloch dev->mdev = mdev; 4338da796ccbSMark Bloch dev->num_ports = num_ports; 4339b5ca15adSMark Bloch 43407852546fSMaor Gottlieb if (ll == IB_LINK_LAYER_ETHERNET && !mlx5_is_roce_init_enabled(mdev)) 434194de879cSMichael Guralnik profile = &raw_eth_profile; 434294de879cSMichael Guralnik else 434394de879cSMichael Guralnik profile = &pf_profile; 434494de879cSMichael Guralnik 434593f82444SLeon Romanovsky ret = __mlx5_ib_add(dev, profile); 434693f82444SLeon Romanovsky if (ret) { 434793f82444SLeon Romanovsky kfree(dev->port); 434893f82444SLeon Romanovsky ib_dealloc_device(&dev->ib_dev); 434993f82444SLeon Romanovsky return ret; 435016c1975fSMark Bloch } 435116c1975fSMark Bloch 435227963d3dSDavid E. Box auxiliary_set_drvdata(adev, dev); 435393f82444SLeon Romanovsky return 0; 435493f82444SLeon Romanovsky } 435593f82444SLeon Romanovsky 435693f82444SLeon Romanovsky static void mlx5r_remove(struct auxiliary_device *adev) 4357e126ba97SEli Cohen { 435832f69e4bSDaniel Jurgens struct mlx5_ib_dev *dev; 43596aec21f6SHaggai Eran 436027963d3dSDavid E. Box dev = auxiliary_get_drvdata(adev); 436116c1975fSMark Bloch __mlx5_ib_remove(dev, dev->profile, MLX5_IB_STAGE_MAX); 4362e126ba97SEli Cohen } 4363e126ba97SEli Cohen 436493f82444SLeon Romanovsky static const struct auxiliary_device_id mlx5r_mp_id_table[] = { 436593f82444SLeon Romanovsky { .name = MLX5_ADEV_NAME ".multiport", }, 436693f82444SLeon Romanovsky {}, 436793f82444SLeon Romanovsky }; 436893f82444SLeon Romanovsky 436993f82444SLeon Romanovsky static const struct auxiliary_device_id mlx5r_id_table[] = { 437093f82444SLeon Romanovsky { .name = MLX5_ADEV_NAME ".rdma", }, 437193f82444SLeon Romanovsky {}, 437293f82444SLeon Romanovsky }; 437393f82444SLeon Romanovsky 437493f82444SLeon Romanovsky MODULE_DEVICE_TABLE(auxiliary, mlx5r_mp_id_table); 437593f82444SLeon Romanovsky MODULE_DEVICE_TABLE(auxiliary, mlx5r_id_table); 437693f82444SLeon Romanovsky 437793f82444SLeon Romanovsky static struct auxiliary_driver mlx5r_mp_driver = { 437893f82444SLeon Romanovsky .name = "multiport", 437993f82444SLeon Romanovsky .probe = mlx5r_mp_probe, 438093f82444SLeon Romanovsky .remove = mlx5r_mp_remove, 438193f82444SLeon Romanovsky .id_table = mlx5r_mp_id_table, 438293f82444SLeon Romanovsky }; 438393f82444SLeon Romanovsky 438493f82444SLeon Romanovsky static struct auxiliary_driver mlx5r_driver = { 438593f82444SLeon Romanovsky .name = "rdma", 438693f82444SLeon Romanovsky .probe = mlx5r_probe, 438793f82444SLeon Romanovsky .remove = mlx5r_remove, 438893f82444SLeon Romanovsky .id_table = mlx5r_id_table, 4389e126ba97SEli Cohen }; 4390e126ba97SEli Cohen 4391e126ba97SEli Cohen static int __init mlx5_ib_init(void) 4392e126ba97SEli Cohen { 439393f82444SLeon Romanovsky int ret; 43946aec21f6SHaggai Eran 43958010d74bSJason Gunthorpe xlt_emergency_page = (void *)__get_free_page(GFP_KERNEL); 4396c44ef998SIlya Lesokhin if (!xlt_emergency_page) 4397d69a24e0SDaniel Jurgens return -ENOMEM; 4398d69a24e0SDaniel Jurgens 4399c44ef998SIlya Lesokhin mlx5_ib_event_wq = alloc_ordered_workqueue("mlx5_ib_event_wq", 0); 4400c44ef998SIlya Lesokhin if (!mlx5_ib_event_wq) { 44018010d74bSJason Gunthorpe free_page((unsigned long)xlt_emergency_page); 4402c44ef998SIlya Lesokhin return -ENOMEM; 4403c44ef998SIlya Lesokhin } 4404c44ef998SIlya Lesokhin 440581713d37SArtemy Kovalyov mlx5_ib_odp_init(); 440693f82444SLeon Romanovsky ret = mlx5r_rep_init(); 440793f82444SLeon Romanovsky if (ret) 440893f82444SLeon Romanovsky goto rep_err; 440993f82444SLeon Romanovsky ret = auxiliary_driver_register(&mlx5r_mp_driver); 441093f82444SLeon Romanovsky if (ret) 441193f82444SLeon Romanovsky goto mp_err; 441293f82444SLeon Romanovsky ret = auxiliary_driver_register(&mlx5r_driver); 441393f82444SLeon Romanovsky if (ret) 441493f82444SLeon Romanovsky goto drv_err; 441593f82444SLeon Romanovsky return 0; 44169603b61dSJack Morgenstein 441793f82444SLeon Romanovsky drv_err: 441893f82444SLeon Romanovsky auxiliary_driver_unregister(&mlx5r_mp_driver); 441993f82444SLeon Romanovsky mp_err: 442093f82444SLeon Romanovsky mlx5r_rep_cleanup(); 442193f82444SLeon Romanovsky rep_err: 442293f82444SLeon Romanovsky destroy_workqueue(mlx5_ib_event_wq); 442393f82444SLeon Romanovsky free_page((unsigned long)xlt_emergency_page); 442493f82444SLeon Romanovsky return ret; 4425e126ba97SEli Cohen } 4426e126ba97SEli Cohen 4427e126ba97SEli Cohen static void __exit mlx5_ib_cleanup(void) 4428e126ba97SEli Cohen { 442993f82444SLeon Romanovsky auxiliary_driver_unregister(&mlx5r_driver); 443093f82444SLeon Romanovsky auxiliary_driver_unregister(&mlx5r_mp_driver); 443193f82444SLeon Romanovsky mlx5r_rep_cleanup(); 443293f82444SLeon Romanovsky 4433d69a24e0SDaniel Jurgens destroy_workqueue(mlx5_ib_event_wq); 44348010d74bSJason Gunthorpe free_page((unsigned long)xlt_emergency_page); 4435e126ba97SEli Cohen } 4436e126ba97SEli Cohen 4437e126ba97SEli Cohen module_init(mlx5_ib_init); 4438e126ba97SEli Cohen module_exit(mlx5_ib_cleanup); 4439