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" 44c6475a0bSAviad Yehezkel #include <linux/mlx5/accel.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, 1291fb7f897SMark Bloch u32 *port_num) 13035b0aa67SMark Bloch { 13135b0aa67SMark Bloch struct net_device *rep_ndev; 13235b0aa67SMark Bloch struct mlx5_ib_port *port; 13335b0aa67SMark Bloch int i; 13435b0aa67SMark Bloch 13535b0aa67SMark Bloch for (i = 0; i < dev->num_ports; i++) { 13635b0aa67SMark Bloch port = &dev->port[i]; 13735b0aa67SMark Bloch if (!port->rep) 13835b0aa67SMark Bloch continue; 13935b0aa67SMark Bloch 14035b0aa67SMark Bloch read_lock(&port->roce.netdev_lock); 141658cfcebSMark Bloch rep_ndev = mlx5_ib_get_rep_netdev(port->rep->esw, 14235b0aa67SMark Bloch port->rep->vport); 14335b0aa67SMark Bloch if (rep_ndev == ndev) { 14435b0aa67SMark Bloch read_unlock(&port->roce.netdev_lock); 14535b0aa67SMark Bloch *port_num = i + 1; 14635b0aa67SMark Bloch return &port->roce; 14735b0aa67SMark Bloch } 14835b0aa67SMark Bloch read_unlock(&port->roce.netdev_lock); 14935b0aa67SMark Bloch } 15035b0aa67SMark Bloch 15135b0aa67SMark Bloch return NULL; 15235b0aa67SMark Bloch } 15335b0aa67SMark Bloch 154fc24fc5eSAchiad Shochat static int mlx5_netdev_event(struct notifier_block *this, 155fc24fc5eSAchiad Shochat unsigned long event, void *ptr) 156fc24fc5eSAchiad Shochat { 1577fd8aefbSDaniel Jurgens struct mlx5_roce *roce = container_of(this, struct mlx5_roce, nb); 158fc24fc5eSAchiad Shochat struct net_device *ndev = netdev_notifier_info_to_dev(ptr); 1591fb7f897SMark Bloch u32 port_num = roce->native_port_num; 1607fd8aefbSDaniel Jurgens struct mlx5_core_dev *mdev; 1617fd8aefbSDaniel Jurgens struct mlx5_ib_dev *ibdev; 1627fd8aefbSDaniel Jurgens 1637fd8aefbSDaniel Jurgens ibdev = roce->dev; 16432f69e4bSDaniel Jurgens mdev = mlx5_ib_get_native_port_mdev(ibdev, port_num, NULL); 16532f69e4bSDaniel Jurgens if (!mdev) 16632f69e4bSDaniel Jurgens return NOTIFY_DONE; 167fc24fc5eSAchiad Shochat 1685ec8c83eSAviv Heller switch (event) { 1695ec8c83eSAviv Heller case NETDEV_REGISTER: 17035b0aa67SMark Bloch /* Should already be registered during the load */ 17135b0aa67SMark Bloch if (ibdev->is_rep) 17235b0aa67SMark Bloch break; 1737fd8aefbSDaniel Jurgens write_lock(&roce->netdev_lock); 174dce45af5SLinus Torvalds if (ndev->dev.parent == mdev->device) 175842a9c83SOr Gerlitz roce->netdev = ndev; 1767fd8aefbSDaniel Jurgens write_unlock(&roce->netdev_lock); 1775ec8c83eSAviv Heller break; 1785ec8c83eSAviv Heller 179842a9c83SOr Gerlitz case NETDEV_UNREGISTER: 18035b0aa67SMark Bloch /* In case of reps, ib device goes away before the netdevs */ 181842a9c83SOr Gerlitz write_lock(&roce->netdev_lock); 182842a9c83SOr Gerlitz if (roce->netdev == ndev) 183842a9c83SOr Gerlitz roce->netdev = NULL; 184842a9c83SOr Gerlitz write_unlock(&roce->netdev_lock); 185842a9c83SOr Gerlitz break; 186842a9c83SOr Gerlitz 187fd65f1b8SMoni Shoua case NETDEV_CHANGE: 1885ec8c83eSAviv Heller case NETDEV_UP: 18988621dfeSAviv Heller case NETDEV_DOWN: { 1907fd8aefbSDaniel Jurgens struct net_device *lag_ndev = mlx5_lag_get_roce_netdev(mdev); 19188621dfeSAviv Heller struct net_device *upper = NULL; 19288621dfeSAviv Heller 19388621dfeSAviv Heller if (lag_ndev) { 19488621dfeSAviv Heller upper = netdev_master_upper_dev_get(lag_ndev); 19588621dfeSAviv Heller dev_put(lag_ndev); 19688621dfeSAviv Heller } 19788621dfeSAviv Heller 19835b0aa67SMark Bloch if (ibdev->is_rep) 19935b0aa67SMark Bloch roce = mlx5_get_rep_roce(ibdev, ndev, &port_num); 20035b0aa67SMark Bloch if (!roce) 20135b0aa67SMark Bloch return NOTIFY_DONE; 2027fd8aefbSDaniel Jurgens if ((upper == ndev || (!upper && ndev == roce->netdev)) 20388621dfeSAviv Heller && ibdev->ib_active) { 204626bc02dSBart Van Assche struct ib_event ibev = { }; 205fd65f1b8SMoni Shoua enum ib_port_state port_state; 2065ec8c83eSAviv Heller 2077fd8aefbSDaniel Jurgens if (get_port_state(&ibdev->ib_dev, port_num, 2087fd8aefbSDaniel Jurgens &port_state)) 2097fd8aefbSDaniel Jurgens goto done; 210fd65f1b8SMoni Shoua 2117fd8aefbSDaniel Jurgens if (roce->last_port_state == port_state) 2127fd8aefbSDaniel Jurgens goto done; 213fd65f1b8SMoni Shoua 2147fd8aefbSDaniel Jurgens roce->last_port_state = port_state; 2155ec8c83eSAviv Heller ibev.device = &ibdev->ib_dev; 216fd65f1b8SMoni Shoua if (port_state == IB_PORT_DOWN) 217fd65f1b8SMoni Shoua ibev.event = IB_EVENT_PORT_ERR; 218fd65f1b8SMoni Shoua else if (port_state == IB_PORT_ACTIVE) 219fd65f1b8SMoni Shoua ibev.event = IB_EVENT_PORT_ACTIVE; 220fd65f1b8SMoni Shoua else 2217fd8aefbSDaniel Jurgens goto done; 222fd65f1b8SMoni Shoua 2237fd8aefbSDaniel Jurgens ibev.element.port_num = port_num; 2245ec8c83eSAviv Heller ib_dispatch_event(&ibev); 2255ec8c83eSAviv Heller } 2265ec8c83eSAviv Heller break; 22788621dfeSAviv Heller } 2285ec8c83eSAviv Heller 2295ec8c83eSAviv Heller default: 2305ec8c83eSAviv Heller break; 2315ec8c83eSAviv Heller } 2327fd8aefbSDaniel Jurgens done: 23332f69e4bSDaniel Jurgens mlx5_ib_put_native_port_mdev(ibdev, port_num); 234fc24fc5eSAchiad Shochat return NOTIFY_DONE; 235fc24fc5eSAchiad Shochat } 236fc24fc5eSAchiad Shochat 237fc24fc5eSAchiad Shochat static struct net_device *mlx5_ib_get_netdev(struct ib_device *device, 2381fb7f897SMark Bloch u32 port_num) 239fc24fc5eSAchiad Shochat { 240fc24fc5eSAchiad Shochat struct mlx5_ib_dev *ibdev = to_mdev(device); 241fc24fc5eSAchiad Shochat struct net_device *ndev; 24232f69e4bSDaniel Jurgens struct mlx5_core_dev *mdev; 243fc24fc5eSAchiad Shochat 24432f69e4bSDaniel Jurgens mdev = mlx5_ib_get_native_port_mdev(ibdev, port_num, NULL); 24532f69e4bSDaniel Jurgens if (!mdev) 24632f69e4bSDaniel Jurgens return NULL; 24732f69e4bSDaniel Jurgens 24832f69e4bSDaniel Jurgens ndev = mlx5_lag_get_roce_netdev(mdev); 24988621dfeSAviv Heller if (ndev) 25032f69e4bSDaniel Jurgens goto out; 25188621dfeSAviv Heller 252fc24fc5eSAchiad Shochat /* Ensure ndev does not disappear before we invoke dev_hold() 253fc24fc5eSAchiad Shochat */ 25495579e78SMark Bloch read_lock(&ibdev->port[port_num - 1].roce.netdev_lock); 25595579e78SMark Bloch ndev = ibdev->port[port_num - 1].roce.netdev; 256fc24fc5eSAchiad Shochat if (ndev) 257fc24fc5eSAchiad Shochat dev_hold(ndev); 25895579e78SMark Bloch read_unlock(&ibdev->port[port_num - 1].roce.netdev_lock); 259fc24fc5eSAchiad Shochat 26032f69e4bSDaniel Jurgens out: 26132f69e4bSDaniel Jurgens mlx5_ib_put_native_port_mdev(ibdev, port_num); 262fc24fc5eSAchiad Shochat return ndev; 263fc24fc5eSAchiad Shochat } 264fc24fc5eSAchiad Shochat 26532f69e4bSDaniel Jurgens struct mlx5_core_dev *mlx5_ib_get_native_port_mdev(struct mlx5_ib_dev *ibdev, 2661fb7f897SMark Bloch u32 ib_port_num, 2671fb7f897SMark Bloch u32 *native_port_num) 26832f69e4bSDaniel Jurgens { 26932f69e4bSDaniel Jurgens enum rdma_link_layer ll = mlx5_ib_port_link_layer(&ibdev->ib_dev, 27032f69e4bSDaniel Jurgens ib_port_num); 27132f69e4bSDaniel Jurgens struct mlx5_core_dev *mdev = NULL; 27232f69e4bSDaniel Jurgens struct mlx5_ib_multiport_info *mpi; 27332f69e4bSDaniel Jurgens struct mlx5_ib_port *port; 27432f69e4bSDaniel Jurgens 275210b1f78SMark Bloch if (!mlx5_core_mp_enabled(ibdev->mdev) || 276210b1f78SMark Bloch ll != IB_LINK_LAYER_ETHERNET) { 277210b1f78SMark Bloch if (native_port_num) 278210b1f78SMark Bloch *native_port_num = ib_port_num; 279210b1f78SMark Bloch return ibdev->mdev; 280210b1f78SMark Bloch } 281210b1f78SMark Bloch 28232f69e4bSDaniel Jurgens if (native_port_num) 28332f69e4bSDaniel Jurgens *native_port_num = 1; 28432f69e4bSDaniel Jurgens 28532f69e4bSDaniel Jurgens port = &ibdev->port[ib_port_num - 1]; 28632f69e4bSDaniel Jurgens spin_lock(&port->mp.mpi_lock); 28732f69e4bSDaniel Jurgens mpi = ibdev->port[ib_port_num - 1].mp.mpi; 28832f69e4bSDaniel Jurgens if (mpi && !mpi->unaffiliate) { 28932f69e4bSDaniel Jurgens mdev = mpi->mdev; 29032f69e4bSDaniel Jurgens /* If it's the master no need to refcount, it'll exist 29132f69e4bSDaniel Jurgens * as long as the ib_dev exists. 29232f69e4bSDaniel Jurgens */ 29332f69e4bSDaniel Jurgens if (!mpi->is_master) 29432f69e4bSDaniel Jurgens mpi->mdev_refcnt++; 29532f69e4bSDaniel Jurgens } 29632f69e4bSDaniel Jurgens spin_unlock(&port->mp.mpi_lock); 29732f69e4bSDaniel Jurgens 29832f69e4bSDaniel Jurgens return mdev; 29932f69e4bSDaniel Jurgens } 30032f69e4bSDaniel Jurgens 3011fb7f897SMark Bloch void mlx5_ib_put_native_port_mdev(struct mlx5_ib_dev *ibdev, u32 port_num) 30232f69e4bSDaniel Jurgens { 30332f69e4bSDaniel Jurgens enum rdma_link_layer ll = mlx5_ib_port_link_layer(&ibdev->ib_dev, 30432f69e4bSDaniel Jurgens port_num); 30532f69e4bSDaniel Jurgens struct mlx5_ib_multiport_info *mpi; 30632f69e4bSDaniel Jurgens struct mlx5_ib_port *port; 30732f69e4bSDaniel Jurgens 30832f69e4bSDaniel Jurgens if (!mlx5_core_mp_enabled(ibdev->mdev) || ll != IB_LINK_LAYER_ETHERNET) 30932f69e4bSDaniel Jurgens return; 31032f69e4bSDaniel Jurgens 31132f69e4bSDaniel Jurgens port = &ibdev->port[port_num - 1]; 31232f69e4bSDaniel Jurgens 31332f69e4bSDaniel Jurgens spin_lock(&port->mp.mpi_lock); 31432f69e4bSDaniel Jurgens mpi = ibdev->port[port_num - 1].mp.mpi; 31532f69e4bSDaniel Jurgens if (mpi->is_master) 31632f69e4bSDaniel Jurgens goto out; 31732f69e4bSDaniel Jurgens 31832f69e4bSDaniel Jurgens mpi->mdev_refcnt--; 31932f69e4bSDaniel Jurgens if (mpi->unaffiliate) 32032f69e4bSDaniel Jurgens complete(&mpi->unref_comp); 32132f69e4bSDaniel Jurgens out: 32232f69e4bSDaniel Jurgens spin_unlock(&port->mp.mpi_lock); 32332f69e4bSDaniel Jurgens } 32432f69e4bSDaniel Jurgens 325639bf441SAharon Landau static int translate_eth_legacy_proto_oper(u32 eth_proto_oper, 326639bf441SAharon Landau u16 *active_speed, u8 *active_width) 327f1b65df5SNoa Osherovich { 328f1b65df5SNoa Osherovich switch (eth_proto_oper) { 329f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_1000BASE_CX_SGMII): 330f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_1000BASE_KX): 331f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_100BASE_TX): 332f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_1000BASE_T): 333f1b65df5SNoa Osherovich *active_width = IB_WIDTH_1X; 334f1b65df5SNoa Osherovich *active_speed = IB_SPEED_SDR; 335f1b65df5SNoa Osherovich break; 336f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_10GBASE_T): 337f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_10GBASE_CX4): 338f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_10GBASE_KX4): 339f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_10GBASE_KR): 340f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_10GBASE_CR): 341f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_10GBASE_SR): 342f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_10GBASE_ER): 343f1b65df5SNoa Osherovich *active_width = IB_WIDTH_1X; 344f1b65df5SNoa Osherovich *active_speed = IB_SPEED_QDR; 345f1b65df5SNoa Osherovich break; 346f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_25GBASE_CR): 347f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_25GBASE_KR): 348f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_25GBASE_SR): 349f1b65df5SNoa Osherovich *active_width = IB_WIDTH_1X; 350f1b65df5SNoa Osherovich *active_speed = IB_SPEED_EDR; 351f1b65df5SNoa Osherovich break; 352f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_40GBASE_CR4): 353f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_40GBASE_KR4): 354f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_40GBASE_SR4): 355f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_40GBASE_LR4): 356f1b65df5SNoa Osherovich *active_width = IB_WIDTH_4X; 357f1b65df5SNoa Osherovich *active_speed = IB_SPEED_QDR; 358f1b65df5SNoa Osherovich break; 359f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_50GBASE_CR2): 360f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_50GBASE_KR2): 361f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_50GBASE_SR2): 362f1b65df5SNoa Osherovich *active_width = IB_WIDTH_1X; 363f1b65df5SNoa Osherovich *active_speed = IB_SPEED_HDR; 364f1b65df5SNoa Osherovich break; 365f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_56GBASE_R4): 366f1b65df5SNoa Osherovich *active_width = IB_WIDTH_4X; 367f1b65df5SNoa Osherovich *active_speed = IB_SPEED_FDR; 368f1b65df5SNoa Osherovich break; 369f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_100GBASE_CR4): 370f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_100GBASE_SR4): 371f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_100GBASE_KR4): 372f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_100GBASE_LR4): 373f1b65df5SNoa Osherovich *active_width = IB_WIDTH_4X; 374f1b65df5SNoa Osherovich *active_speed = IB_SPEED_EDR; 375f1b65df5SNoa Osherovich break; 376f1b65df5SNoa Osherovich default: 377f1b65df5SNoa Osherovich return -EINVAL; 378f1b65df5SNoa Osherovich } 379f1b65df5SNoa Osherovich 380f1b65df5SNoa Osherovich return 0; 381f1b65df5SNoa Osherovich } 382f1b65df5SNoa Osherovich 383639bf441SAharon Landau static int translate_eth_ext_proto_oper(u32 eth_proto_oper, u16 *active_speed, 38408e8676fSAya Levin u8 *active_width) 38508e8676fSAya Levin { 38608e8676fSAya Levin switch (eth_proto_oper) { 38708e8676fSAya Levin case MLX5E_PROT_MASK(MLX5E_SGMII_100M): 38808e8676fSAya Levin case MLX5E_PROT_MASK(MLX5E_1000BASE_X_SGMII): 38908e8676fSAya Levin *active_width = IB_WIDTH_1X; 39008e8676fSAya Levin *active_speed = IB_SPEED_SDR; 39108e8676fSAya Levin break; 39208e8676fSAya Levin case MLX5E_PROT_MASK(MLX5E_5GBASE_R): 39308e8676fSAya Levin *active_width = IB_WIDTH_1X; 39408e8676fSAya Levin *active_speed = IB_SPEED_DDR; 39508e8676fSAya Levin break; 39608e8676fSAya Levin case MLX5E_PROT_MASK(MLX5E_10GBASE_XFI_XAUI_1): 39708e8676fSAya Levin *active_width = IB_WIDTH_1X; 39808e8676fSAya Levin *active_speed = IB_SPEED_QDR; 39908e8676fSAya Levin break; 40008e8676fSAya Levin case MLX5E_PROT_MASK(MLX5E_40GBASE_XLAUI_4_XLPPI_4): 40108e8676fSAya Levin *active_width = IB_WIDTH_4X; 40208e8676fSAya Levin *active_speed = IB_SPEED_QDR; 40308e8676fSAya Levin break; 40408e8676fSAya Levin case MLX5E_PROT_MASK(MLX5E_25GAUI_1_25GBASE_CR_KR): 40508e8676fSAya Levin *active_width = IB_WIDTH_1X; 40608e8676fSAya Levin *active_speed = IB_SPEED_EDR; 40708e8676fSAya Levin break; 40808e8676fSAya Levin case MLX5E_PROT_MASK(MLX5E_50GAUI_2_LAUI_2_50GBASE_CR2_KR2): 409cd272875SAya Levin *active_width = IB_WIDTH_2X; 410cd272875SAya Levin *active_speed = IB_SPEED_EDR; 411cd272875SAya Levin break; 41208e8676fSAya Levin case MLX5E_PROT_MASK(MLX5E_50GAUI_1_LAUI_1_50GBASE_CR_KR): 41308e8676fSAya Levin *active_width = IB_WIDTH_1X; 41408e8676fSAya Levin *active_speed = IB_SPEED_HDR; 41508e8676fSAya Levin break; 416cd272875SAya Levin case MLX5E_PROT_MASK(MLX5E_CAUI_4_100GBASE_CR4_KR4): 417cd272875SAya Levin *active_width = IB_WIDTH_4X; 418cd272875SAya Levin *active_speed = IB_SPEED_EDR; 419cd272875SAya Levin break; 42008e8676fSAya Levin case MLX5E_PROT_MASK(MLX5E_100GAUI_2_100GBASE_CR2_KR2): 42108e8676fSAya Levin *active_width = IB_WIDTH_2X; 42208e8676fSAya Levin *active_speed = IB_SPEED_HDR; 42308e8676fSAya Levin break; 424f946e45fSMeir Lichtinger case MLX5E_PROT_MASK(MLX5E_100GAUI_1_100GBASE_CR_KR): 425f946e45fSMeir Lichtinger *active_width = IB_WIDTH_1X; 426f946e45fSMeir Lichtinger *active_speed = IB_SPEED_NDR; 427f946e45fSMeir Lichtinger break; 42808e8676fSAya Levin case MLX5E_PROT_MASK(MLX5E_200GAUI_4_200GBASE_CR4_KR4): 42908e8676fSAya Levin *active_width = IB_WIDTH_4X; 43008e8676fSAya Levin *active_speed = IB_SPEED_HDR; 43108e8676fSAya Levin break; 432f946e45fSMeir Lichtinger case MLX5E_PROT_MASK(MLX5E_200GAUI_2_200GBASE_CR2_KR2): 433f946e45fSMeir Lichtinger *active_width = IB_WIDTH_2X; 434f946e45fSMeir Lichtinger *active_speed = IB_SPEED_NDR; 435f946e45fSMeir Lichtinger break; 436f946e45fSMeir Lichtinger case MLX5E_PROT_MASK(MLX5E_400GAUI_4_400GBASE_CR4_KR4): 437f946e45fSMeir Lichtinger *active_width = IB_WIDTH_4X; 438f946e45fSMeir Lichtinger *active_speed = IB_SPEED_NDR; 439f946e45fSMeir Lichtinger break; 44008e8676fSAya Levin default: 44108e8676fSAya Levin return -EINVAL; 44208e8676fSAya Levin } 44308e8676fSAya Levin 44408e8676fSAya Levin return 0; 44508e8676fSAya Levin } 44608e8676fSAya Levin 447639bf441SAharon Landau static int translate_eth_proto_oper(u32 eth_proto_oper, u16 *active_speed, 44808e8676fSAya Levin u8 *active_width, bool ext) 44908e8676fSAya Levin { 45008e8676fSAya Levin return ext ? 45108e8676fSAya Levin translate_eth_ext_proto_oper(eth_proto_oper, active_speed, 45208e8676fSAya Levin active_width) : 45308e8676fSAya Levin translate_eth_legacy_proto_oper(eth_proto_oper, active_speed, 45408e8676fSAya Levin active_width); 45508e8676fSAya Levin } 45608e8676fSAya Levin 4571fb7f897SMark Bloch static int mlx5_query_port_roce(struct ib_device *device, u32 port_num, 4583f89a643SAchiad Shochat struct ib_port_attr *props) 4593f89a643SAchiad Shochat { 4603f89a643SAchiad Shochat struct mlx5_ib_dev *dev = to_mdev(device); 461bc4e12ffSAya Levin u32 out[MLX5_ST_SZ_DW(ptys_reg)] = {0}; 462da005f9fSColin Ian King struct mlx5_core_dev *mdev; 46388621dfeSAviv Heller struct net_device *ndev, *upper; 4643f89a643SAchiad Shochat enum ib_mtu ndev_ib_mtu; 465b3cbd6f0SDaniel Jurgens bool put_mdev = true; 466f1b65df5SNoa Osherovich u32 eth_prot_oper; 4671fb7f897SMark Bloch u32 mdev_port_num; 46808e8676fSAya Levin bool ext; 469095b0927SIlan Tayari int err; 4703f89a643SAchiad Shochat 471b3cbd6f0SDaniel Jurgens mdev = mlx5_ib_get_native_port_mdev(dev, port_num, &mdev_port_num); 472b3cbd6f0SDaniel Jurgens if (!mdev) { 473b3cbd6f0SDaniel Jurgens /* This means the port isn't affiliated yet. Get the 474b3cbd6f0SDaniel Jurgens * info for the master port instead. 475b3cbd6f0SDaniel Jurgens */ 476b3cbd6f0SDaniel Jurgens put_mdev = false; 477b3cbd6f0SDaniel Jurgens mdev = dev->mdev; 478b3cbd6f0SDaniel Jurgens mdev_port_num = 1; 479b3cbd6f0SDaniel Jurgens port_num = 1; 480b3cbd6f0SDaniel Jurgens } 481b3cbd6f0SDaniel Jurgens 482f1b65df5SNoa Osherovich /* Possible bad flows are checked before filling out props so in case 483f1b65df5SNoa Osherovich * of an error it will still be zeroed out. 48426628e2dSMark Bloch * Use native port in case of reps 48550f22fd8SNoa Osherovich */ 48626628e2dSMark Bloch if (dev->is_rep) 48726628e2dSMark Bloch err = mlx5_query_port_ptys(mdev, out, sizeof(out), MLX5_PTYS_EN, 48826628e2dSMark Bloch 1); 48926628e2dSMark Bloch else 490bc4e12ffSAya Levin err = mlx5_query_port_ptys(mdev, out, sizeof(out), MLX5_PTYS_EN, 491b3cbd6f0SDaniel Jurgens mdev_port_num); 492095b0927SIlan Tayari if (err) 493b3cbd6f0SDaniel Jurgens goto out; 494530c8632SAya Levin ext = !!MLX5_GET_ETH_PROTO(ptys_reg, out, true, eth_proto_capability); 49508e8676fSAya Levin eth_prot_oper = MLX5_GET_ETH_PROTO(ptys_reg, out, ext, eth_proto_oper); 496f1b65df5SNoa Osherovich 4977672ed33SHonggang Li props->active_width = IB_WIDTH_4X; 4987672ed33SHonggang Li props->active_speed = IB_SPEED_QDR; 4997672ed33SHonggang Li 500f1b65df5SNoa Osherovich translate_eth_proto_oper(eth_prot_oper, &props->active_speed, 50108e8676fSAya Levin &props->active_width, ext); 5023f89a643SAchiad Shochat 5037852546fSMaor Gottlieb if (!dev->is_rep && dev->mdev->roce.roce_en) { 5047a58779eSParav Pandit u16 qkey_viol_cntr; 5057a58779eSParav Pandit 5063f89a643SAchiad Shochat props->port_cap_flags |= IB_PORT_CM_SUP; 5072f944c0fSJason Gunthorpe props->ip_gids = true; 5083f89a643SAchiad Shochat props->gid_tbl_len = MLX5_CAP_ROCE(dev->mdev, 5093f89a643SAchiad Shochat roce_address_table_size); 5107a58779eSParav Pandit mlx5_query_nic_vport_qkey_viol_cntr(mdev, &qkey_viol_cntr); 5117a58779eSParav Pandit props->qkey_viol_cntr = qkey_viol_cntr; 5127a58779eSParav Pandit } 5133f89a643SAchiad Shochat props->max_mtu = IB_MTU_4096; 5143f89a643SAchiad Shochat props->max_msg_sz = 1 << MLX5_CAP_GEN(dev->mdev, log_max_msg); 5153f89a643SAchiad Shochat props->pkey_tbl_len = 1; 5163f89a643SAchiad Shochat props->state = IB_PORT_DOWN; 51772a7720fSKamal Heib props->phys_state = IB_PORT_PHYS_STATE_DISABLED; 5183f89a643SAchiad Shochat 519b3cbd6f0SDaniel Jurgens /* If this is a stub query for an unaffiliated port stop here */ 520b3cbd6f0SDaniel Jurgens if (!put_mdev) 521b3cbd6f0SDaniel Jurgens goto out; 522b3cbd6f0SDaniel Jurgens 5233f89a643SAchiad Shochat ndev = mlx5_ib_get_netdev(device, port_num); 5243f89a643SAchiad Shochat if (!ndev) 525b3cbd6f0SDaniel Jurgens goto out; 5263f89a643SAchiad Shochat 5277c34ec19SAviv Heller if (dev->lag_active) { 52888621dfeSAviv Heller rcu_read_lock(); 52988621dfeSAviv Heller upper = netdev_master_upper_dev_get_rcu(ndev); 53088621dfeSAviv Heller if (upper) { 53188621dfeSAviv Heller dev_put(ndev); 53288621dfeSAviv Heller ndev = upper; 53388621dfeSAviv Heller dev_hold(ndev); 53488621dfeSAviv Heller } 53588621dfeSAviv Heller rcu_read_unlock(); 53688621dfeSAviv Heller } 53788621dfeSAviv Heller 5383f89a643SAchiad Shochat if (netif_running(ndev) && netif_carrier_ok(ndev)) { 5393f89a643SAchiad Shochat props->state = IB_PORT_ACTIVE; 54072a7720fSKamal Heib props->phys_state = IB_PORT_PHYS_STATE_LINK_UP; 5413f89a643SAchiad Shochat } 5423f89a643SAchiad Shochat 5433f89a643SAchiad Shochat ndev_ib_mtu = iboe_get_mtu(ndev->mtu); 5443f89a643SAchiad Shochat 5453f89a643SAchiad Shochat dev_put(ndev); 5463f89a643SAchiad Shochat 5473f89a643SAchiad Shochat props->active_mtu = min(props->max_mtu, ndev_ib_mtu); 548b3cbd6f0SDaniel Jurgens out: 549b3cbd6f0SDaniel Jurgens if (put_mdev) 550b3cbd6f0SDaniel Jurgens mlx5_ib_put_native_port_mdev(dev, port_num); 551b3cbd6f0SDaniel Jurgens return err; 5523f89a643SAchiad Shochat } 5533f89a643SAchiad Shochat 5541fb7f897SMark Bloch static int set_roce_addr(struct mlx5_ib_dev *dev, u32 port_num, 555095b0927SIlan Tayari unsigned int index, const union ib_gid *gid, 556095b0927SIlan Tayari const struct ib_gid_attr *attr) 5573cca2606SAchiad Shochat { 558dedbc2d3SParav Pandit enum ib_gid_type gid_type; 559a70c0739SParav Pandit u16 vlan_id = 0xffff; 560095b0927SIlan Tayari u8 roce_version = 0; 561095b0927SIlan Tayari u8 roce_l3_type = 0; 562095b0927SIlan Tayari u8 mac[ETH_ALEN]; 563a70c0739SParav Pandit int ret; 5643cca2606SAchiad Shochat 565095b0927SIlan Tayari gid_type = attr->gid_type; 566dedbc2d3SParav Pandit if (gid) { 567a70c0739SParav Pandit ret = rdma_read_gid_l2_fields(attr, &vlan_id, &mac[0]); 568a70c0739SParav Pandit if (ret) 569a70c0739SParav Pandit return ret; 5703cca2606SAchiad Shochat } 5713cca2606SAchiad Shochat 572095b0927SIlan Tayari switch (gid_type) { 5731c15b4f2SAvihai Horon case IB_GID_TYPE_ROCE: 574095b0927SIlan Tayari roce_version = MLX5_ROCE_VERSION_1; 5753cca2606SAchiad Shochat break; 5763cca2606SAchiad Shochat case IB_GID_TYPE_ROCE_UDP_ENCAP: 577095b0927SIlan Tayari roce_version = MLX5_ROCE_VERSION_2; 578dedbc2d3SParav Pandit if (gid && ipv6_addr_v4mapped((void *)gid)) 579095b0927SIlan Tayari roce_l3_type = MLX5_ROCE_L3_TYPE_IPV4; 580095b0927SIlan Tayari else 581095b0927SIlan Tayari roce_l3_type = MLX5_ROCE_L3_TYPE_IPV6; 5823cca2606SAchiad Shochat break; 5833cca2606SAchiad Shochat 5843cca2606SAchiad Shochat default: 585095b0927SIlan Tayari mlx5_ib_warn(dev, "Unexpected GID type %u\n", gid_type); 5863cca2606SAchiad Shochat } 5873cca2606SAchiad Shochat 588095b0927SIlan Tayari return mlx5_core_roce_gid_set(dev->mdev, index, roce_version, 589cf34e1feSParav Pandit roce_l3_type, gid->raw, mac, 590a70c0739SParav Pandit vlan_id < VLAN_CFI_MASK, vlan_id, 591cf34e1feSParav Pandit port_num); 5923cca2606SAchiad Shochat } 5933cca2606SAchiad Shochat 594f4df9a7cSParav Pandit static int mlx5_ib_add_gid(const struct ib_gid_attr *attr, 5953cca2606SAchiad Shochat __always_unused void **context) 5963cca2606SAchiad Shochat { 597414448d2SParav Pandit return set_roce_addr(to_mdev(attr->device), attr->port_num, 598f4df9a7cSParav Pandit attr->index, &attr->gid, attr); 5993cca2606SAchiad Shochat } 6003cca2606SAchiad Shochat 601414448d2SParav Pandit static int mlx5_ib_del_gid(const struct ib_gid_attr *attr, 602414448d2SParav Pandit __always_unused void **context) 6033cca2606SAchiad Shochat { 604414448d2SParav Pandit return set_roce_addr(to_mdev(attr->device), attr->port_num, 605dedbc2d3SParav Pandit attr->index, NULL, attr); 6063cca2606SAchiad Shochat } 6073cca2606SAchiad Shochat 6085ac55dfcSMark Zhang __be16 mlx5_get_roce_udp_sport_min(const struct mlx5_ib_dev *dev, 60947ec3866SParav Pandit const struct ib_gid_attr *attr) 6102811ba51SAchiad Shochat { 61147ec3866SParav Pandit if (attr->gid_type != IB_GID_TYPE_ROCE_UDP_ENCAP) 6122811ba51SAchiad Shochat return 0; 6132811ba51SAchiad Shochat 6142811ba51SAchiad Shochat return cpu_to_be16(MLX5_CAP_ROCE(dev->mdev, r_roce_min_src_udp_port)); 6152811ba51SAchiad Shochat } 6162811ba51SAchiad Shochat 6171b5daf11SMajd Dibbiny static int mlx5_use_mad_ifc(struct mlx5_ib_dev *dev) 6181b5daf11SMajd Dibbiny { 6197fae6655SNoa Osherovich if (MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_IB) 620d603c809SEli Cohen return !MLX5_CAP_GEN(dev->mdev, ib_virt); 6217fae6655SNoa Osherovich return 0; 6221b5daf11SMajd Dibbiny } 6231b5daf11SMajd Dibbiny 6241b5daf11SMajd Dibbiny enum { 6251b5daf11SMajd Dibbiny MLX5_VPORT_ACCESS_METHOD_MAD, 6261b5daf11SMajd Dibbiny MLX5_VPORT_ACCESS_METHOD_HCA, 6271b5daf11SMajd Dibbiny MLX5_VPORT_ACCESS_METHOD_NIC, 6281b5daf11SMajd Dibbiny }; 6291b5daf11SMajd Dibbiny 6301b5daf11SMajd Dibbiny static int mlx5_get_vport_access_method(struct ib_device *ibdev) 6311b5daf11SMajd Dibbiny { 6321b5daf11SMajd Dibbiny if (mlx5_use_mad_ifc(to_mdev(ibdev))) 6331b5daf11SMajd Dibbiny return MLX5_VPORT_ACCESS_METHOD_MAD; 6341b5daf11SMajd Dibbiny 635ebd61f68SAchiad Shochat if (mlx5_ib_port_link_layer(ibdev, 1) == 6361b5daf11SMajd Dibbiny IB_LINK_LAYER_ETHERNET) 6371b5daf11SMajd Dibbiny return MLX5_VPORT_ACCESS_METHOD_NIC; 6381b5daf11SMajd Dibbiny 6391b5daf11SMajd Dibbiny return MLX5_VPORT_ACCESS_METHOD_HCA; 6401b5daf11SMajd Dibbiny } 6411b5daf11SMajd Dibbiny 642da7525d2SEran Ben Elisha static void get_atomic_caps(struct mlx5_ib_dev *dev, 643776a3906SMoni Shoua u8 atomic_size_qp, 644da7525d2SEran Ben Elisha struct ib_device_attr *props) 645da7525d2SEran Ben Elisha { 646da7525d2SEran Ben Elisha u8 tmp; 647da7525d2SEran Ben Elisha u8 atomic_operations = MLX5_CAP_ATOMIC(dev->mdev, atomic_operations); 648da7525d2SEran Ben Elisha u8 atomic_req_8B_endianness_mode = 649bd10838aSOr Gerlitz MLX5_CAP_ATOMIC(dev->mdev, atomic_req_8B_endianness_mode); 650da7525d2SEran Ben Elisha 651da7525d2SEran Ben Elisha /* Check if HW supports 8 bytes standard atomic operations and capable 652da7525d2SEran Ben Elisha * of host endianness respond 653da7525d2SEran Ben Elisha */ 654da7525d2SEran Ben Elisha tmp = MLX5_ATOMIC_OPS_CMP_SWAP | MLX5_ATOMIC_OPS_FETCH_ADD; 655da7525d2SEran Ben Elisha if (((atomic_operations & tmp) == tmp) && 656da7525d2SEran Ben Elisha (atomic_size_qp & MLX5_ATOMIC_SIZE_QP_8BYTES) && 657da7525d2SEran Ben Elisha (atomic_req_8B_endianness_mode)) { 658da7525d2SEran Ben Elisha props->atomic_cap = IB_ATOMIC_HCA; 659da7525d2SEran Ben Elisha } else { 660da7525d2SEran Ben Elisha props->atomic_cap = IB_ATOMIC_NONE; 661da7525d2SEran Ben Elisha } 662da7525d2SEran Ben Elisha } 663da7525d2SEran Ben Elisha 664776a3906SMoni Shoua static void get_atomic_caps_qp(struct mlx5_ib_dev *dev, 665776a3906SMoni Shoua struct ib_device_attr *props) 666776a3906SMoni Shoua { 667776a3906SMoni Shoua u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_qp); 668776a3906SMoni Shoua 669776a3906SMoni Shoua get_atomic_caps(dev, atomic_size_qp, props); 670776a3906SMoni Shoua } 671776a3906SMoni Shoua 6721b5daf11SMajd Dibbiny static int mlx5_query_system_image_guid(struct ib_device *ibdev, 6731b5daf11SMajd Dibbiny __be64 *sys_image_guid) 6741b5daf11SMajd Dibbiny { 6751b5daf11SMajd Dibbiny struct mlx5_ib_dev *dev = to_mdev(ibdev); 6761b5daf11SMajd Dibbiny struct mlx5_core_dev *mdev = dev->mdev; 6771b5daf11SMajd Dibbiny u64 tmp; 6781b5daf11SMajd Dibbiny int err; 6791b5daf11SMajd Dibbiny 6801b5daf11SMajd Dibbiny switch (mlx5_get_vport_access_method(ibdev)) { 6811b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_MAD: 6821b5daf11SMajd Dibbiny return mlx5_query_mad_ifc_system_image_guid(ibdev, 6831b5daf11SMajd Dibbiny sys_image_guid); 6841b5daf11SMajd Dibbiny 6851b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_HCA: 6861b5daf11SMajd Dibbiny err = mlx5_query_hca_vport_system_image_guid(mdev, &tmp); 6873f89a643SAchiad Shochat break; 6883f89a643SAchiad Shochat 6893f89a643SAchiad Shochat case MLX5_VPORT_ACCESS_METHOD_NIC: 6903f89a643SAchiad Shochat err = mlx5_query_nic_vport_system_image_guid(mdev, &tmp); 6913f89a643SAchiad Shochat break; 6921b5daf11SMajd Dibbiny 6931b5daf11SMajd Dibbiny default: 6941b5daf11SMajd Dibbiny return -EINVAL; 6951b5daf11SMajd Dibbiny } 6963f89a643SAchiad Shochat 6973f89a643SAchiad Shochat if (!err) 6983f89a643SAchiad Shochat *sys_image_guid = cpu_to_be64(tmp); 6993f89a643SAchiad Shochat 7003f89a643SAchiad Shochat return err; 7013f89a643SAchiad Shochat 7021b5daf11SMajd Dibbiny } 7031b5daf11SMajd Dibbiny 7041b5daf11SMajd Dibbiny static int mlx5_query_max_pkeys(struct ib_device *ibdev, 7051b5daf11SMajd Dibbiny u16 *max_pkeys) 7061b5daf11SMajd Dibbiny { 7071b5daf11SMajd Dibbiny struct mlx5_ib_dev *dev = to_mdev(ibdev); 7081b5daf11SMajd Dibbiny struct mlx5_core_dev *mdev = dev->mdev; 7091b5daf11SMajd Dibbiny 7101b5daf11SMajd Dibbiny switch (mlx5_get_vport_access_method(ibdev)) { 7111b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_MAD: 7121b5daf11SMajd Dibbiny return mlx5_query_mad_ifc_max_pkeys(ibdev, max_pkeys); 7131b5daf11SMajd Dibbiny 7141b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_HCA: 7151b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_NIC: 7161b5daf11SMajd Dibbiny *max_pkeys = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, 7171b5daf11SMajd Dibbiny pkey_table_size)); 7181b5daf11SMajd Dibbiny return 0; 7191b5daf11SMajd Dibbiny 7201b5daf11SMajd Dibbiny default: 7211b5daf11SMajd Dibbiny return -EINVAL; 7221b5daf11SMajd Dibbiny } 7231b5daf11SMajd Dibbiny } 7241b5daf11SMajd Dibbiny 7251b5daf11SMajd Dibbiny static int mlx5_query_vendor_id(struct ib_device *ibdev, 7261b5daf11SMajd Dibbiny u32 *vendor_id) 7271b5daf11SMajd Dibbiny { 7281b5daf11SMajd Dibbiny struct mlx5_ib_dev *dev = to_mdev(ibdev); 7291b5daf11SMajd Dibbiny 7301b5daf11SMajd Dibbiny switch (mlx5_get_vport_access_method(ibdev)) { 7311b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_MAD: 7321b5daf11SMajd Dibbiny return mlx5_query_mad_ifc_vendor_id(ibdev, vendor_id); 7331b5daf11SMajd Dibbiny 7341b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_HCA: 7351b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_NIC: 7361b5daf11SMajd Dibbiny return mlx5_core_query_vendor_id(dev->mdev, vendor_id); 7371b5daf11SMajd Dibbiny 7381b5daf11SMajd Dibbiny default: 7391b5daf11SMajd Dibbiny return -EINVAL; 7401b5daf11SMajd Dibbiny } 7411b5daf11SMajd Dibbiny } 7421b5daf11SMajd Dibbiny 7431b5daf11SMajd Dibbiny static int mlx5_query_node_guid(struct mlx5_ib_dev *dev, 7441b5daf11SMajd Dibbiny __be64 *node_guid) 7451b5daf11SMajd Dibbiny { 7461b5daf11SMajd Dibbiny u64 tmp; 7471b5daf11SMajd Dibbiny int err; 7481b5daf11SMajd Dibbiny 7491b5daf11SMajd Dibbiny switch (mlx5_get_vport_access_method(&dev->ib_dev)) { 7501b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_MAD: 7511b5daf11SMajd Dibbiny return mlx5_query_mad_ifc_node_guid(dev, node_guid); 7521b5daf11SMajd Dibbiny 7531b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_HCA: 7541b5daf11SMajd Dibbiny err = mlx5_query_hca_vport_node_guid(dev->mdev, &tmp); 7553f89a643SAchiad Shochat break; 7563f89a643SAchiad Shochat 7573f89a643SAchiad Shochat case MLX5_VPORT_ACCESS_METHOD_NIC: 7583f89a643SAchiad Shochat err = mlx5_query_nic_vport_node_guid(dev->mdev, &tmp); 7593f89a643SAchiad Shochat break; 7601b5daf11SMajd Dibbiny 7611b5daf11SMajd Dibbiny default: 7621b5daf11SMajd Dibbiny return -EINVAL; 7631b5daf11SMajd Dibbiny } 7643f89a643SAchiad Shochat 7653f89a643SAchiad Shochat if (!err) 7663f89a643SAchiad Shochat *node_guid = cpu_to_be64(tmp); 7673f89a643SAchiad Shochat 7683f89a643SAchiad Shochat return err; 7691b5daf11SMajd Dibbiny } 7701b5daf11SMajd Dibbiny 7711b5daf11SMajd Dibbiny struct mlx5_reg_node_desc { 772bd99fdeaSYuval Shaia u8 desc[IB_DEVICE_NODE_DESC_MAX]; 7731b5daf11SMajd Dibbiny }; 7741b5daf11SMajd Dibbiny 7751b5daf11SMajd Dibbiny static int mlx5_query_node_desc(struct mlx5_ib_dev *dev, char *node_desc) 7761b5daf11SMajd Dibbiny { 7771b5daf11SMajd Dibbiny struct mlx5_reg_node_desc in; 7781b5daf11SMajd Dibbiny 7791b5daf11SMajd Dibbiny if (mlx5_use_mad_ifc(dev)) 7801b5daf11SMajd Dibbiny return mlx5_query_mad_ifc_node_desc(dev, node_desc); 7811b5daf11SMajd Dibbiny 7821b5daf11SMajd Dibbiny memset(&in, 0, sizeof(in)); 7831b5daf11SMajd Dibbiny 7841b5daf11SMajd Dibbiny return mlx5_core_access_reg(dev->mdev, &in, sizeof(in), node_desc, 7851b5daf11SMajd Dibbiny sizeof(struct mlx5_reg_node_desc), 7861b5daf11SMajd Dibbiny MLX5_REG_NODE_DESC, 0, 0); 7871b5daf11SMajd Dibbiny } 7881b5daf11SMajd Dibbiny 789e126ba97SEli Cohen static int mlx5_ib_query_device(struct ib_device *ibdev, 7902528e33eSMatan Barak struct ib_device_attr *props, 7912528e33eSMatan Barak struct ib_udata *uhw) 792e126ba97SEli Cohen { 79348357091SLeon Romanovsky size_t uhw_outlen = (uhw) ? uhw->outlen : 0; 794e126ba97SEli Cohen struct mlx5_ib_dev *dev = to_mdev(ibdev); 795938fe83cSSaeed Mahameed struct mlx5_core_dev *mdev = dev->mdev; 796e126ba97SEli Cohen int err = -ENOMEM; 797288c01b7SEli Cohen int max_sq_desc; 798e126ba97SEli Cohen int max_rq_sg; 799e126ba97SEli Cohen int max_sq_sg; 800e0238a6aSSagi Grimberg u64 min_page_size = 1ull << MLX5_CAP_GEN(mdev, log_pg_sz); 80185c7c014SDaniel Jurgens bool raw_support = !mlx5_core_mp_enabled(mdev); 802402ca536SBodong Wang struct mlx5_ib_query_device_resp resp = {}; 803402ca536SBodong Wang size_t resp_len; 804402ca536SBodong Wang u64 max_tso; 805e126ba97SEli Cohen 806402ca536SBodong Wang resp_len = sizeof(resp.comp_mask) + sizeof(resp.response_length); 80748357091SLeon Romanovsky if (uhw_outlen && uhw_outlen < resp_len) 808402ca536SBodong Wang return -EINVAL; 8096f26b2acSErez Alfasi 810402ca536SBodong Wang resp.response_length = resp_len; 811402ca536SBodong Wang 81248357091SLeon Romanovsky if (uhw && uhw->inlen && !ib_is_udata_cleared(uhw, 0, uhw->inlen)) 8132528e33eSMatan Barak return -EINVAL; 8142528e33eSMatan Barak 815e126ba97SEli Cohen memset(props, 0, sizeof(*props)); 8161b5daf11SMajd Dibbiny err = mlx5_query_system_image_guid(ibdev, 8171b5daf11SMajd Dibbiny &props->sys_image_guid); 8181b5daf11SMajd Dibbiny if (err) 8191b5daf11SMajd Dibbiny return err; 8201b5daf11SMajd Dibbiny 8212019d70eSParav Pandit props->max_pkeys = dev->pkey_table_len; 8221b5daf11SMajd Dibbiny 8231b5daf11SMajd Dibbiny err = mlx5_query_vendor_id(ibdev, &props->vendor_id); 8241b5daf11SMajd Dibbiny if (err) 8251b5daf11SMajd Dibbiny return err; 826e126ba97SEli Cohen 8279603b61dSJack Morgenstein props->fw_ver = ((u64)fw_rev_maj(dev->mdev) << 32) | 8289603b61dSJack Morgenstein (fw_rev_min(dev->mdev) << 16) | 8299603b61dSJack Morgenstein fw_rev_sub(dev->mdev); 830e126ba97SEli Cohen props->device_cap_flags = IB_DEVICE_CHANGE_PHY_PORT | 831e126ba97SEli Cohen IB_DEVICE_PORT_ACTIVE_EVENT | 832e126ba97SEli Cohen IB_DEVICE_SYS_IMAGE_GUID | 8331a4c3a3dSEli Cohen IB_DEVICE_RC_RNR_NAK_GEN; 834938fe83cSSaeed Mahameed 835938fe83cSSaeed Mahameed if (MLX5_CAP_GEN(mdev, pkv)) 836e126ba97SEli Cohen props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR; 837938fe83cSSaeed Mahameed if (MLX5_CAP_GEN(mdev, qkv)) 838e126ba97SEli Cohen props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR; 839938fe83cSSaeed Mahameed if (MLX5_CAP_GEN(mdev, apm)) 840e126ba97SEli Cohen props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG; 841938fe83cSSaeed Mahameed if (MLX5_CAP_GEN(mdev, xrc)) 842e126ba97SEli Cohen props->device_cap_flags |= IB_DEVICE_XRC; 843d2370e0aSMatan Barak if (MLX5_CAP_GEN(mdev, imaicl)) { 844d2370e0aSMatan Barak props->device_cap_flags |= IB_DEVICE_MEM_WINDOW | 845d2370e0aSMatan Barak IB_DEVICE_MEM_WINDOW_TYPE_2B; 846d2370e0aSMatan Barak props->max_mw = 1 << MLX5_CAP_GEN(mdev, log_max_mkey); 847b005d316SSagi Grimberg /* We support 'Gappy' memory registration too */ 848b005d316SSagi Grimberg props->device_cap_flags |= IB_DEVICE_SG_GAPS_REG; 849d2370e0aSMatan Barak } 8500ec52f01SJason Gunthorpe /* IB_WR_REG_MR always requires changing the entity size with UMR */ 8510ec52f01SJason Gunthorpe if (!MLX5_CAP_GEN(dev->mdev, umr_modify_entity_size_disabled)) 852e126ba97SEli Cohen props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS; 853938fe83cSSaeed Mahameed if (MLX5_CAP_GEN(mdev, sho)) { 854c0a6cbb9SIsrael Rukshin props->device_cap_flags |= IB_DEVICE_INTEGRITY_HANDOVER; 8552dea9094SSagi Grimberg /* At this stage no support for signature handover */ 8562dea9094SSagi Grimberg props->sig_prot_cap = IB_PROT_T10DIF_TYPE_1 | 8572dea9094SSagi Grimberg IB_PROT_T10DIF_TYPE_2 | 8582dea9094SSagi Grimberg IB_PROT_T10DIF_TYPE_3; 8592dea9094SSagi Grimberg props->sig_guard_cap = IB_GUARD_T10DIF_CRC | 8602dea9094SSagi Grimberg IB_GUARD_T10DIF_CSUM; 8612dea9094SSagi Grimberg } 862938fe83cSSaeed Mahameed if (MLX5_CAP_GEN(mdev, block_lb_mc)) 863f360d88aSEli Cohen props->device_cap_flags |= IB_DEVICE_BLOCK_MULTICAST_LOOPBACK; 864e126ba97SEli Cohen 86585c7c014SDaniel Jurgens if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) && raw_support) { 866e8161334SNoa Osherovich if (MLX5_CAP_ETH(mdev, csum_cap)) { 867e8161334SNoa Osherovich /* Legacy bit to support old userspace libraries */ 86888115fe7SBodong Wang props->device_cap_flags |= IB_DEVICE_RAW_IP_CSUM; 869e8161334SNoa Osherovich props->raw_packet_caps |= IB_RAW_PACKET_CAP_IP_CSUM; 870e8161334SNoa Osherovich } 871e8161334SNoa Osherovich 872e8161334SNoa Osherovich if (MLX5_CAP_ETH(dev->mdev, vlan_cap)) 873e8161334SNoa Osherovich props->raw_packet_caps |= 874e8161334SNoa Osherovich IB_RAW_PACKET_CAP_CVLAN_STRIPPING; 87588115fe7SBodong Wang 876a762d460SLeon Romanovsky if (offsetofend(typeof(resp), tso_caps) <= uhw_outlen) { 877402ca536SBodong Wang max_tso = MLX5_CAP_ETH(mdev, max_lso_cap); 878402ca536SBodong Wang if (max_tso) { 879402ca536SBodong Wang resp.tso_caps.max_tso = 1 << max_tso; 880402ca536SBodong Wang resp.tso_caps.supported_qpts |= 881402ca536SBodong Wang 1 << IB_QPT_RAW_PACKET; 882402ca536SBodong Wang resp.response_length += sizeof(resp.tso_caps); 883402ca536SBodong Wang } 884402ca536SBodong Wang } 88531f69a82SYishai Hadas 886a762d460SLeon Romanovsky if (offsetofend(typeof(resp), rss_caps) <= uhw_outlen) { 88731f69a82SYishai Hadas resp.rss_caps.rx_hash_function = 88831f69a82SYishai Hadas MLX5_RX_HASH_FUNC_TOEPLITZ; 88931f69a82SYishai Hadas resp.rss_caps.rx_hash_fields_mask = 89031f69a82SYishai Hadas MLX5_RX_HASH_SRC_IPV4 | 89131f69a82SYishai Hadas MLX5_RX_HASH_DST_IPV4 | 89231f69a82SYishai Hadas MLX5_RX_HASH_SRC_IPV6 | 89331f69a82SYishai Hadas MLX5_RX_HASH_DST_IPV6 | 89431f69a82SYishai Hadas MLX5_RX_HASH_SRC_PORT_TCP | 89531f69a82SYishai Hadas MLX5_RX_HASH_DST_PORT_TCP | 89631f69a82SYishai Hadas MLX5_RX_HASH_SRC_PORT_UDP | 8974e2b53a5SMaor Gottlieb MLX5_RX_HASH_DST_PORT_UDP | 8984e2b53a5SMaor Gottlieb MLX5_RX_HASH_INNER; 8992d93fc85SMatan Barak if (mlx5_accel_ipsec_device_caps(dev->mdev) & 9002d93fc85SMatan Barak MLX5_ACCEL_IPSEC_CAP_DEVICE) 9012d93fc85SMatan Barak resp.rss_caps.rx_hash_fields_mask |= 9022d93fc85SMatan Barak MLX5_RX_HASH_IPSEC_SPI; 90331f69a82SYishai Hadas resp.response_length += sizeof(resp.rss_caps); 90431f69a82SYishai Hadas } 90531f69a82SYishai Hadas } else { 906a762d460SLeon Romanovsky if (offsetofend(typeof(resp), tso_caps) <= uhw_outlen) 90731f69a82SYishai Hadas resp.response_length += sizeof(resp.tso_caps); 908a762d460SLeon Romanovsky if (offsetofend(typeof(resp), rss_caps) <= uhw_outlen) 90931f69a82SYishai Hadas resp.response_length += sizeof(resp.rss_caps); 910402ca536SBodong Wang } 911402ca536SBodong Wang 912f0313965SErez Shitrit if (MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) { 913f0313965SErez Shitrit props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM; 914f0313965SErez Shitrit props->device_cap_flags |= IB_DEVICE_UD_TSO; 915f0313965SErez Shitrit } 916f0313965SErez Shitrit 91703404e8aSMaor Gottlieb if (MLX5_CAP_GEN(dev->mdev, rq_delay_drop) && 91885c7c014SDaniel Jurgens MLX5_CAP_GEN(dev->mdev, general_notification_event) && 91985c7c014SDaniel Jurgens raw_support) 92003404e8aSMaor Gottlieb props->raw_packet_caps |= IB_RAW_PACKET_CAP_DELAY_DROP; 92103404e8aSMaor Gottlieb 9221d54f890SYishai Hadas if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads) && 9231d54f890SYishai Hadas MLX5_CAP_IPOIB_ENHANCED(mdev, csum_cap)) 9241d54f890SYishai Hadas props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM; 9251d54f890SYishai Hadas 926cff5a0f3SMajd Dibbiny if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) && 92785c7c014SDaniel Jurgens MLX5_CAP_ETH(dev->mdev, scatter_fcs) && 92885c7c014SDaniel Jurgens raw_support) { 929e8161334SNoa Osherovich /* Legacy bit to support old userspace libraries */ 930cff5a0f3SMajd Dibbiny props->device_cap_flags |= IB_DEVICE_RAW_SCATTER_FCS; 931e8161334SNoa Osherovich props->raw_packet_caps |= IB_RAW_PACKET_CAP_SCATTER_FCS; 932e8161334SNoa Osherovich } 933cff5a0f3SMajd Dibbiny 93424da0016SAriel Levkovich if (MLX5_CAP_DEV_MEM(mdev, memic)) { 93524da0016SAriel Levkovich props->max_dm_size = 93624da0016SAriel Levkovich MLX5_CAP_DEV_MEM(mdev, max_memic_size); 93724da0016SAriel Levkovich } 93824da0016SAriel Levkovich 939da6d6ba3SMaor Gottlieb if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS)) 940da6d6ba3SMaor Gottlieb props->device_cap_flags |= IB_DEVICE_MANAGED_FLOW_STEERING; 941da6d6ba3SMaor Gottlieb 942b1383aa6SNoa Osherovich if (MLX5_CAP_GEN(mdev, end_pad)) 943b1383aa6SNoa Osherovich props->device_cap_flags |= IB_DEVICE_PCI_WRITE_END_PADDING; 944b1383aa6SNoa Osherovich 9451b5daf11SMajd Dibbiny props->vendor_part_id = mdev->pdev->device; 9461b5daf11SMajd Dibbiny props->hw_ver = mdev->pdev->revision; 947e126ba97SEli Cohen 948e126ba97SEli Cohen props->max_mr_size = ~0ull; 949e0238a6aSSagi Grimberg props->page_size_cap = ~(min_page_size - 1); 950938fe83cSSaeed Mahameed props->max_qp = 1 << MLX5_CAP_GEN(mdev, log_max_qp); 951938fe83cSSaeed Mahameed props->max_qp_wr = 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz); 952938fe83cSSaeed Mahameed max_rq_sg = MLX5_CAP_GEN(mdev, max_wqe_sz_rq) / 953938fe83cSSaeed Mahameed sizeof(struct mlx5_wqe_data_seg); 954288c01b7SEli Cohen max_sq_desc = min_t(int, MLX5_CAP_GEN(mdev, max_wqe_sz_sq), 512); 955288c01b7SEli Cohen max_sq_sg = (max_sq_desc - sizeof(struct mlx5_wqe_ctrl_seg) - 956288c01b7SEli Cohen sizeof(struct mlx5_wqe_raddr_seg)) / 957e126ba97SEli Cohen sizeof(struct mlx5_wqe_data_seg); 95833023fb8SSteve Wise props->max_send_sge = max_sq_sg; 95933023fb8SSteve Wise props->max_recv_sge = max_rq_sg; 960986ef95eSSagi Grimberg props->max_sge_rd = MLX5_MAX_SGE_RD; 961938fe83cSSaeed Mahameed props->max_cq = 1 << MLX5_CAP_GEN(mdev, log_max_cq); 9629f177686SLeon Romanovsky props->max_cqe = (1 << MLX5_CAP_GEN(mdev, log_max_cq_sz)) - 1; 963938fe83cSSaeed Mahameed props->max_mr = 1 << MLX5_CAP_GEN(mdev, log_max_mkey); 964938fe83cSSaeed Mahameed props->max_pd = 1 << MLX5_CAP_GEN(mdev, log_max_pd); 965938fe83cSSaeed Mahameed props->max_qp_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_req_qp); 966938fe83cSSaeed Mahameed props->max_qp_init_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_res_qp); 967938fe83cSSaeed Mahameed props->max_srq = 1 << MLX5_CAP_GEN(mdev, log_max_srq); 968938fe83cSSaeed Mahameed props->max_srq_wr = (1 << MLX5_CAP_GEN(mdev, log_max_srq_sz)) - 1; 969938fe83cSSaeed Mahameed props->local_ca_ack_delay = MLX5_CAP_GEN(mdev, local_ca_ack_delay); 970e126ba97SEli Cohen props->max_res_rd_atom = props->max_qp_rd_atom * props->max_qp; 971e126ba97SEli Cohen props->max_srq_sge = max_rq_sg - 1; 972911f4331SSagi Grimberg props->max_fast_reg_page_list_len = 973911f4331SSagi Grimberg 1 << MLX5_CAP_GEN(mdev, log_max_klm_list_size); 97462e3c379SMax Gurtovoy props->max_pi_fast_reg_page_list_len = 97562e3c379SMax Gurtovoy props->max_fast_reg_page_list_len / 2; 97636609056SYamin Friedman props->max_sgl_rd = 97736609056SYamin Friedman MLX5_CAP_GEN(mdev, max_sgl_for_optimized_performance); 978776a3906SMoni Shoua get_atomic_caps_qp(dev, props); 97981bea28fSEli Cohen props->masked_atomic_cap = IB_ATOMIC_NONE; 980938fe83cSSaeed Mahameed props->max_mcast_grp = 1 << MLX5_CAP_GEN(mdev, log_max_mcg); 981938fe83cSSaeed Mahameed props->max_mcast_qp_attach = MLX5_CAP_GEN(mdev, max_qp_mcg); 982e126ba97SEli Cohen props->max_total_mcast_qp_attach = props->max_mcast_qp_attach * 983e126ba97SEli Cohen props->max_mcast_grp; 98486695a65SMaor Gottlieb props->max_ah = INT_MAX; 9857c60bcbbSMatan Barak props->hca_core_clock = MLX5_CAP_GEN(mdev, device_frequency_khz); 9867c60bcbbSMatan Barak props->timestamp_mask = 0x7FFFFFFFFFFFFFFFULL; 987e126ba97SEli Cohen 988e502b8b0SLeon Romanovsky if (IS_ENABLED(CONFIG_INFINIBAND_ON_DEMAND_PAGING)) { 98900815752SMoni Shoua if (dev->odp_caps.general_caps & IB_ODP_SUPPORT) 9908cdd312cSHaggai Eran props->device_cap_flags |= IB_DEVICE_ON_DEMAND_PAGING; 9918cdd312cSHaggai Eran props->odp_caps = dev->odp_caps; 992a73a8955SMoni Shoua if (!uhw) { 993a73a8955SMoni Shoua /* ODP for kernel QPs is not implemented for receive 994a73a8955SMoni Shoua * WQEs and SRQ WQEs 995a73a8955SMoni Shoua */ 996a73a8955SMoni Shoua props->odp_caps.per_transport_caps.rc_odp_caps &= 997a73a8955SMoni Shoua ~(IB_ODP_SUPPORT_READ | 998a73a8955SMoni Shoua IB_ODP_SUPPORT_SRQ_RECV); 999a73a8955SMoni Shoua props->odp_caps.per_transport_caps.uc_odp_caps &= 1000a73a8955SMoni Shoua ~(IB_ODP_SUPPORT_READ | 1001a73a8955SMoni Shoua IB_ODP_SUPPORT_SRQ_RECV); 1002a73a8955SMoni Shoua props->odp_caps.per_transport_caps.ud_odp_caps &= 1003a73a8955SMoni Shoua ~(IB_ODP_SUPPORT_READ | 1004a73a8955SMoni Shoua IB_ODP_SUPPORT_SRQ_RECV); 1005a73a8955SMoni Shoua props->odp_caps.per_transport_caps.xrc_odp_caps &= 1006a73a8955SMoni Shoua ~(IB_ODP_SUPPORT_READ | 1007a73a8955SMoni Shoua IB_ODP_SUPPORT_SRQ_RECV); 1008a73a8955SMoni Shoua } 1009e502b8b0SLeon Romanovsky } 10108cdd312cSHaggai Eran 1011051f2630SLeon Romanovsky if (MLX5_CAP_GEN(mdev, cd)) 1012051f2630SLeon Romanovsky props->device_cap_flags |= IB_DEVICE_CROSS_CHANNEL; 1013051f2630SLeon Romanovsky 1014e53a9d26SParav Pandit if (mlx5_core_is_vf(mdev)) 1015eff901d3SEli Cohen props->device_cap_flags |= IB_DEVICE_VIRTUAL_FUNCTION; 1016eff901d3SEli Cohen 101731f69a82SYishai Hadas if (mlx5_ib_port_link_layer(ibdev, 1) == 101885c7c014SDaniel Jurgens IB_LINK_LAYER_ETHERNET && raw_support) { 101931f69a82SYishai Hadas props->rss_caps.max_rwq_indirection_tables = 102031f69a82SYishai Hadas 1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt); 102131f69a82SYishai Hadas props->rss_caps.max_rwq_indirection_table_size = 102231f69a82SYishai Hadas 1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt_size); 102331f69a82SYishai Hadas props->rss_caps.supported_qpts = 1 << IB_QPT_RAW_PACKET; 102431f69a82SYishai Hadas props->max_wq_type_rq = 102531f69a82SYishai Hadas 1 << MLX5_CAP_GEN(dev->mdev, log_max_rq); 102631f69a82SYishai Hadas } 102731f69a82SYishai Hadas 1028eb761894SArtemy Kovalyov if (MLX5_CAP_GEN(mdev, tag_matching)) { 102978b1beb0SLeon Romanovsky props->tm_caps.max_num_tags = 1030eb761894SArtemy Kovalyov (1 << MLX5_CAP_GEN(mdev, log_tag_matching_list_sz)) - 1; 103178b1beb0SLeon Romanovsky props->tm_caps.max_ops = 1032eb761894SArtemy Kovalyov 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz); 103378b1beb0SLeon Romanovsky props->tm_caps.max_sge = MLX5_TM_MAX_SGE; 1034eb761894SArtemy Kovalyov } 1035eb761894SArtemy Kovalyov 103689705e92SDanit Goldberg if (MLX5_CAP_GEN(mdev, tag_matching) && 103789705e92SDanit Goldberg MLX5_CAP_GEN(mdev, rndv_offload_rc)) { 103889705e92SDanit Goldberg props->tm_caps.flags = IB_TM_CAP_RNDV_RC; 103989705e92SDanit Goldberg props->tm_caps.max_rndv_hdr_size = MLX5_TM_MAX_RNDV_MSG_SIZE; 104089705e92SDanit Goldberg } 104189705e92SDanit Goldberg 104287ab3f52SYonatan Cohen if (MLX5_CAP_GEN(dev->mdev, cq_moderation)) { 104387ab3f52SYonatan Cohen props->cq_caps.max_cq_moderation_count = 104487ab3f52SYonatan Cohen MLX5_MAX_CQ_COUNT; 104587ab3f52SYonatan Cohen props->cq_caps.max_cq_moderation_period = 104687ab3f52SYonatan Cohen MLX5_MAX_CQ_PERIOD; 104787ab3f52SYonatan Cohen } 104887ab3f52SYonatan Cohen 1049a762d460SLeon Romanovsky if (offsetofend(typeof(resp), cqe_comp_caps) <= uhw_outlen) { 1050572f46bfSYonatan Cohen resp.response_length += sizeof(resp.cqe_comp_caps); 1051572f46bfSYonatan Cohen 1052572f46bfSYonatan Cohen if (MLX5_CAP_GEN(dev->mdev, cqe_compression)) { 10537e43a2a5SBodong Wang resp.cqe_comp_caps.max_num = 1054572f46bfSYonatan Cohen MLX5_CAP_GEN(dev->mdev, 1055572f46bfSYonatan Cohen cqe_compression_max_num); 1056572f46bfSYonatan Cohen 10577e43a2a5SBodong Wang resp.cqe_comp_caps.supported_format = 10587e43a2a5SBodong Wang MLX5_IB_CQE_RES_FORMAT_HASH | 10597e43a2a5SBodong Wang MLX5_IB_CQE_RES_FORMAT_CSUM; 10606f1006a4SYonatan Cohen 10616f1006a4SYonatan Cohen if (MLX5_CAP_GEN(dev->mdev, mini_cqe_resp_stride_index)) 10626f1006a4SYonatan Cohen resp.cqe_comp_caps.supported_format |= 10636f1006a4SYonatan Cohen MLX5_IB_CQE_RES_FORMAT_CSUM_STRIDX; 1064572f46bfSYonatan Cohen } 10657e43a2a5SBodong Wang } 10667e43a2a5SBodong Wang 1067a762d460SLeon Romanovsky if (offsetofend(typeof(resp), packet_pacing_caps) <= uhw_outlen && 106885c7c014SDaniel Jurgens raw_support) { 1069d949167dSBodong Wang if (MLX5_CAP_QOS(mdev, packet_pacing) && 1070d949167dSBodong Wang MLX5_CAP_GEN(mdev, qos)) { 1071d949167dSBodong Wang resp.packet_pacing_caps.qp_rate_limit_max = 1072d949167dSBodong Wang MLX5_CAP_QOS(mdev, packet_pacing_max_rate); 1073d949167dSBodong Wang resp.packet_pacing_caps.qp_rate_limit_min = 1074d949167dSBodong Wang MLX5_CAP_QOS(mdev, packet_pacing_min_rate); 1075d949167dSBodong Wang resp.packet_pacing_caps.supported_qpts |= 1076d949167dSBodong Wang 1 << IB_QPT_RAW_PACKET; 107761147f39SBodong Wang if (MLX5_CAP_QOS(mdev, packet_pacing_burst_bound) && 107861147f39SBodong Wang MLX5_CAP_QOS(mdev, packet_pacing_typical_size)) 107961147f39SBodong Wang resp.packet_pacing_caps.cap_flags |= 108061147f39SBodong Wang MLX5_IB_PP_SUPPORT_BURST; 1081d949167dSBodong Wang } 1082d949167dSBodong Wang resp.response_length += sizeof(resp.packet_pacing_caps); 1083d949167dSBodong Wang } 1084d949167dSBodong Wang 1085a762d460SLeon Romanovsky if (offsetofend(typeof(resp), mlx5_ib_support_multi_pkt_send_wqes) <= 1086a762d460SLeon Romanovsky uhw_outlen) { 1087795b609cSBodong Wang if (MLX5_CAP_ETH(mdev, multi_pkt_send_wqe)) 10889f885201SLeon Romanovsky resp.mlx5_ib_support_multi_pkt_send_wqes = 1089795b609cSBodong Wang MLX5_IB_ALLOW_MPW; 1090050da902SBodong Wang 1091050da902SBodong Wang if (MLX5_CAP_ETH(mdev, enhanced_multi_pkt_send_wqe)) 1092050da902SBodong Wang resp.mlx5_ib_support_multi_pkt_send_wqes |= 1093050da902SBodong Wang MLX5_IB_SUPPORT_EMPW; 1094050da902SBodong Wang 10959f885201SLeon Romanovsky resp.response_length += 10969f885201SLeon Romanovsky sizeof(resp.mlx5_ib_support_multi_pkt_send_wqes); 10979f885201SLeon Romanovsky } 10989f885201SLeon Romanovsky 1099a762d460SLeon Romanovsky if (offsetofend(typeof(resp), flags) <= uhw_outlen) { 1100de57f2adSGuy Levi resp.response_length += sizeof(resp.flags); 11017a0c8f42SGuy Levi 1102de57f2adSGuy Levi if (MLX5_CAP_GEN(mdev, cqe_compression_128)) 1103de57f2adSGuy Levi resp.flags |= 1104de57f2adSGuy Levi MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_COMP; 11057a0c8f42SGuy Levi 11067a0c8f42SGuy Levi if (MLX5_CAP_GEN(mdev, cqe_128_always)) 11077a0c8f42SGuy Levi resp.flags |= MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_PAD; 11087e11b911SDanit Goldberg if (MLX5_CAP_GEN(mdev, qp_packet_based)) 11097e11b911SDanit Goldberg resp.flags |= 11107e11b911SDanit Goldberg MLX5_IB_QUERY_DEV_RESP_PACKET_BASED_CREDIT_MODE; 11117249c8eaSGuy Levi 11127249c8eaSGuy Levi resp.flags |= MLX5_IB_QUERY_DEV_RESP_FLAGS_SCAT2CQE_DCT; 1113de57f2adSGuy Levi } 11149f885201SLeon Romanovsky 1115a762d460SLeon Romanovsky if (offsetofend(typeof(resp), sw_parsing_caps) <= uhw_outlen) { 111696dc3fc5SNoa Osherovich resp.response_length += sizeof(resp.sw_parsing_caps); 111796dc3fc5SNoa Osherovich if (MLX5_CAP_ETH(mdev, swp)) { 111896dc3fc5SNoa Osherovich resp.sw_parsing_caps.sw_parsing_offloads |= 111996dc3fc5SNoa Osherovich MLX5_IB_SW_PARSING; 112096dc3fc5SNoa Osherovich 112196dc3fc5SNoa Osherovich if (MLX5_CAP_ETH(mdev, swp_csum)) 112296dc3fc5SNoa Osherovich resp.sw_parsing_caps.sw_parsing_offloads |= 112396dc3fc5SNoa Osherovich MLX5_IB_SW_PARSING_CSUM; 112496dc3fc5SNoa Osherovich 112596dc3fc5SNoa Osherovich if (MLX5_CAP_ETH(mdev, swp_lso)) 112696dc3fc5SNoa Osherovich resp.sw_parsing_caps.sw_parsing_offloads |= 112796dc3fc5SNoa Osherovich MLX5_IB_SW_PARSING_LSO; 112896dc3fc5SNoa Osherovich 112996dc3fc5SNoa Osherovich if (resp.sw_parsing_caps.sw_parsing_offloads) 113096dc3fc5SNoa Osherovich resp.sw_parsing_caps.supported_qpts = 113196dc3fc5SNoa Osherovich BIT(IB_QPT_RAW_PACKET); 113296dc3fc5SNoa Osherovich } 113396dc3fc5SNoa Osherovich } 113496dc3fc5SNoa Osherovich 1135a762d460SLeon Romanovsky if (offsetofend(typeof(resp), striding_rq_caps) <= uhw_outlen && 113685c7c014SDaniel Jurgens raw_support) { 1137b4f34597SNoa Osherovich resp.response_length += sizeof(resp.striding_rq_caps); 1138b4f34597SNoa Osherovich if (MLX5_CAP_GEN(mdev, striding_rq)) { 1139b4f34597SNoa Osherovich resp.striding_rq_caps.min_single_stride_log_num_of_bytes = 1140b4f34597SNoa Osherovich MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES; 1141b4f34597SNoa Osherovich resp.striding_rq_caps.max_single_stride_log_num_of_bytes = 1142b4f34597SNoa Osherovich MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES; 1143c16339b6SMark Zhang if (MLX5_CAP_GEN(dev->mdev, ext_stride_num_range)) 1144c16339b6SMark Zhang resp.striding_rq_caps 1145c16339b6SMark Zhang .min_single_wqe_log_num_of_strides = 1146c16339b6SMark Zhang MLX5_EXT_MIN_SINGLE_WQE_LOG_NUM_STRIDES; 1147c16339b6SMark Zhang else 1148c16339b6SMark Zhang resp.striding_rq_caps 1149c16339b6SMark Zhang .min_single_wqe_log_num_of_strides = 1150b4f34597SNoa Osherovich MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES; 1151b4f34597SNoa Osherovich resp.striding_rq_caps.max_single_wqe_log_num_of_strides = 1152b4f34597SNoa Osherovich MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES; 1153b4f34597SNoa Osherovich resp.striding_rq_caps.supported_qpts = 1154b4f34597SNoa Osherovich BIT(IB_QPT_RAW_PACKET); 1155b4f34597SNoa Osherovich } 1156b4f34597SNoa Osherovich } 1157b4f34597SNoa Osherovich 1158a762d460SLeon Romanovsky if (offsetofend(typeof(resp), tunnel_offloads_caps) <= uhw_outlen) { 1159f95ef6cbSMaor Gottlieb resp.response_length += sizeof(resp.tunnel_offloads_caps); 1160f95ef6cbSMaor Gottlieb if (MLX5_CAP_ETH(mdev, tunnel_stateless_vxlan)) 1161f95ef6cbSMaor Gottlieb resp.tunnel_offloads_caps |= 1162f95ef6cbSMaor Gottlieb MLX5_IB_TUNNELED_OFFLOADS_VXLAN; 1163f95ef6cbSMaor Gottlieb if (MLX5_CAP_ETH(mdev, tunnel_stateless_geneve_rx)) 1164f95ef6cbSMaor Gottlieb resp.tunnel_offloads_caps |= 1165f95ef6cbSMaor Gottlieb MLX5_IB_TUNNELED_OFFLOADS_GENEVE; 1166f95ef6cbSMaor Gottlieb if (MLX5_CAP_ETH(mdev, tunnel_stateless_gre)) 1167f95ef6cbSMaor Gottlieb resp.tunnel_offloads_caps |= 1168f95ef6cbSMaor Gottlieb MLX5_IB_TUNNELED_OFFLOADS_GRE; 116941e684efSAlex Vesker if (MLX5_CAP_ETH(mdev, tunnel_stateless_mpls_over_gre)) 1170e818e255SAriel Levkovich resp.tunnel_offloads_caps |= 1171e818e255SAriel Levkovich MLX5_IB_TUNNELED_OFFLOADS_MPLS_GRE; 117241e684efSAlex Vesker if (MLX5_CAP_ETH(mdev, tunnel_stateless_mpls_over_udp)) 1173e818e255SAriel Levkovich resp.tunnel_offloads_caps |= 1174e818e255SAriel Levkovich MLX5_IB_TUNNELED_OFFLOADS_MPLS_UDP; 1175f95ef6cbSMaor Gottlieb } 1176f95ef6cbSMaor Gottlieb 117711656f59SLior Nahmanson if (offsetofend(typeof(resp), dci_streams_caps) <= uhw_outlen) { 117811656f59SLior Nahmanson resp.response_length += sizeof(resp.dci_streams_caps); 117911656f59SLior Nahmanson 118011656f59SLior Nahmanson resp.dci_streams_caps.max_log_num_concurent = 118111656f59SLior Nahmanson MLX5_CAP_GEN(mdev, log_max_dci_stream_channels); 118211656f59SLior Nahmanson 118311656f59SLior Nahmanson resp.dci_streams_caps.max_log_num_errored = 118411656f59SLior Nahmanson MLX5_CAP_GEN(mdev, log_max_dci_errored_streams); 118511656f59SLior Nahmanson } 118611656f59SLior Nahmanson 118748357091SLeon Romanovsky if (uhw_outlen) { 1188402ca536SBodong Wang err = ib_copy_to_udata(uhw, &resp, resp.response_length); 1189402ca536SBodong Wang 1190402ca536SBodong Wang if (err) 1191402ca536SBodong Wang return err; 1192402ca536SBodong Wang } 1193402ca536SBodong Wang 11941b5daf11SMajd Dibbiny return 0; 11951b5daf11SMajd Dibbiny } 1196e126ba97SEli Cohen 1197639bf441SAharon Landau static void translate_active_width(struct ib_device *ibdev, u16 active_width, 11981b5daf11SMajd Dibbiny u8 *ib_width) 11991b5daf11SMajd Dibbiny { 12001b5daf11SMajd Dibbiny struct mlx5_ib_dev *dev = to_mdev(ibdev); 12011b5daf11SMajd Dibbiny 1202e27014bdSAharon Landau if (active_width & MLX5_PTYS_WIDTH_1X) 12031b5daf11SMajd Dibbiny *ib_width = IB_WIDTH_1X; 1204e27014bdSAharon Landau else if (active_width & MLX5_PTYS_WIDTH_2X) 1205d764970bSMichael Guralnik *ib_width = IB_WIDTH_2X; 1206e27014bdSAharon Landau else if (active_width & MLX5_PTYS_WIDTH_4X) 12071b5daf11SMajd Dibbiny *ib_width = IB_WIDTH_4X; 1208e27014bdSAharon Landau else if (active_width & MLX5_PTYS_WIDTH_8X) 12091b5daf11SMajd Dibbiny *ib_width = IB_WIDTH_8X; 1210e27014bdSAharon Landau else if (active_width & MLX5_PTYS_WIDTH_12X) 12111b5daf11SMajd Dibbiny *ib_width = IB_WIDTH_12X; 1212db7a691aSMichael Guralnik else { 1213db7a691aSMichael Guralnik mlx5_ib_dbg(dev, "Invalid active_width %d, setting width to default value: 4x\n", 1214e27014bdSAharon Landau active_width); 1215db7a691aSMichael Guralnik *ib_width = IB_WIDTH_4X; 12161b5daf11SMajd Dibbiny } 12171b5daf11SMajd Dibbiny 1218db7a691aSMichael Guralnik return; 12191b5daf11SMajd Dibbiny } 12201b5daf11SMajd Dibbiny 12211b5daf11SMajd Dibbiny static int mlx5_mtu_to_ib_mtu(int mtu) 12221b5daf11SMajd Dibbiny { 12231b5daf11SMajd Dibbiny switch (mtu) { 12241b5daf11SMajd Dibbiny case 256: return 1; 12251b5daf11SMajd Dibbiny case 512: return 2; 12261b5daf11SMajd Dibbiny case 1024: return 3; 12271b5daf11SMajd Dibbiny case 2048: return 4; 12281b5daf11SMajd Dibbiny case 4096: return 5; 12291b5daf11SMajd Dibbiny default: 12301b5daf11SMajd Dibbiny pr_warn("invalid mtu\n"); 12311b5daf11SMajd Dibbiny return -1; 12321b5daf11SMajd Dibbiny } 12331b5daf11SMajd Dibbiny } 12341b5daf11SMajd Dibbiny 12351b5daf11SMajd Dibbiny enum ib_max_vl_num { 12361b5daf11SMajd Dibbiny __IB_MAX_VL_0 = 1, 12371b5daf11SMajd Dibbiny __IB_MAX_VL_0_1 = 2, 12381b5daf11SMajd Dibbiny __IB_MAX_VL_0_3 = 3, 12391b5daf11SMajd Dibbiny __IB_MAX_VL_0_7 = 4, 12401b5daf11SMajd Dibbiny __IB_MAX_VL_0_14 = 5, 12411b5daf11SMajd Dibbiny }; 12421b5daf11SMajd Dibbiny 12431b5daf11SMajd Dibbiny enum mlx5_vl_hw_cap { 12441b5daf11SMajd Dibbiny MLX5_VL_HW_0 = 1, 12451b5daf11SMajd Dibbiny MLX5_VL_HW_0_1 = 2, 12461b5daf11SMajd Dibbiny MLX5_VL_HW_0_2 = 3, 12471b5daf11SMajd Dibbiny MLX5_VL_HW_0_3 = 4, 12481b5daf11SMajd Dibbiny MLX5_VL_HW_0_4 = 5, 12491b5daf11SMajd Dibbiny MLX5_VL_HW_0_5 = 6, 12501b5daf11SMajd Dibbiny MLX5_VL_HW_0_6 = 7, 12511b5daf11SMajd Dibbiny MLX5_VL_HW_0_7 = 8, 12521b5daf11SMajd Dibbiny MLX5_VL_HW_0_14 = 15 12531b5daf11SMajd Dibbiny }; 12541b5daf11SMajd Dibbiny 12551b5daf11SMajd Dibbiny static int translate_max_vl_num(struct ib_device *ibdev, u8 vl_hw_cap, 12561b5daf11SMajd Dibbiny u8 *max_vl_num) 12571b5daf11SMajd Dibbiny { 12581b5daf11SMajd Dibbiny switch (vl_hw_cap) { 12591b5daf11SMajd Dibbiny case MLX5_VL_HW_0: 12601b5daf11SMajd Dibbiny *max_vl_num = __IB_MAX_VL_0; 12611b5daf11SMajd Dibbiny break; 12621b5daf11SMajd Dibbiny case MLX5_VL_HW_0_1: 12631b5daf11SMajd Dibbiny *max_vl_num = __IB_MAX_VL_0_1; 12641b5daf11SMajd Dibbiny break; 12651b5daf11SMajd Dibbiny case MLX5_VL_HW_0_3: 12661b5daf11SMajd Dibbiny *max_vl_num = __IB_MAX_VL_0_3; 12671b5daf11SMajd Dibbiny break; 12681b5daf11SMajd Dibbiny case MLX5_VL_HW_0_7: 12691b5daf11SMajd Dibbiny *max_vl_num = __IB_MAX_VL_0_7; 12701b5daf11SMajd Dibbiny break; 12711b5daf11SMajd Dibbiny case MLX5_VL_HW_0_14: 12721b5daf11SMajd Dibbiny *max_vl_num = __IB_MAX_VL_0_14; 12731b5daf11SMajd Dibbiny break; 12741b5daf11SMajd Dibbiny 12751b5daf11SMajd Dibbiny default: 12761b5daf11SMajd Dibbiny return -EINVAL; 12771b5daf11SMajd Dibbiny } 12781b5daf11SMajd Dibbiny 12791b5daf11SMajd Dibbiny return 0; 12801b5daf11SMajd Dibbiny } 12811b5daf11SMajd Dibbiny 12821fb7f897SMark Bloch static int mlx5_query_hca_port(struct ib_device *ibdev, u32 port, 12831b5daf11SMajd Dibbiny struct ib_port_attr *props) 12841b5daf11SMajd Dibbiny { 12851b5daf11SMajd Dibbiny struct mlx5_ib_dev *dev = to_mdev(ibdev); 12861b5daf11SMajd Dibbiny struct mlx5_core_dev *mdev = dev->mdev; 12871b5daf11SMajd Dibbiny struct mlx5_hca_vport_context *rep; 1288046339eaSSaeed Mahameed u16 max_mtu; 1289046339eaSSaeed Mahameed u16 oper_mtu; 12901b5daf11SMajd Dibbiny int err; 1291639bf441SAharon Landau u16 ib_link_width_oper; 12921b5daf11SMajd Dibbiny u8 vl_hw_cap; 12931b5daf11SMajd Dibbiny 12941b5daf11SMajd Dibbiny rep = kzalloc(sizeof(*rep), GFP_KERNEL); 12951b5daf11SMajd Dibbiny if (!rep) { 12961b5daf11SMajd Dibbiny err = -ENOMEM; 12971b5daf11SMajd Dibbiny goto out; 12981b5daf11SMajd Dibbiny } 12991b5daf11SMajd Dibbiny 1300c4550c63SOr Gerlitz /* props being zeroed by the caller, avoid zeroing it here */ 13011b5daf11SMajd Dibbiny 13021b5daf11SMajd Dibbiny err = mlx5_query_hca_vport_context(mdev, 0, port, 0, rep); 13031b5daf11SMajd Dibbiny if (err) 13041b5daf11SMajd Dibbiny goto out; 13051b5daf11SMajd Dibbiny 13061b5daf11SMajd Dibbiny props->lid = rep->lid; 13071b5daf11SMajd Dibbiny props->lmc = rep->lmc; 13081b5daf11SMajd Dibbiny props->sm_lid = rep->sm_lid; 13091b5daf11SMajd Dibbiny props->sm_sl = rep->sm_sl; 13101b5daf11SMajd Dibbiny props->state = rep->vport_state; 13111b5daf11SMajd Dibbiny props->phys_state = rep->port_physical_state; 13121b5daf11SMajd Dibbiny props->port_cap_flags = rep->cap_mask1; 13131b5daf11SMajd Dibbiny props->gid_tbl_len = mlx5_get_gid_table_len(MLX5_CAP_GEN(mdev, gid_table_size)); 13141b5daf11SMajd Dibbiny props->max_msg_sz = 1 << MLX5_CAP_GEN(mdev, log_max_msg); 13151b5daf11SMajd Dibbiny props->pkey_tbl_len = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, pkey_table_size)); 13161b5daf11SMajd Dibbiny props->bad_pkey_cntr = rep->pkey_violation_counter; 13171b5daf11SMajd Dibbiny props->qkey_viol_cntr = rep->qkey_violation_counter; 13181b5daf11SMajd Dibbiny props->subnet_timeout = rep->subnet_timeout; 13191b5daf11SMajd Dibbiny props->init_type_reply = rep->init_type_reply; 13201b5daf11SMajd Dibbiny 13214106a758SMichael Guralnik if (props->port_cap_flags & IB_PORT_CAP_MASK2_SUP) 13224106a758SMichael Guralnik props->port_cap_flags2 = rep->cap_mask2; 13234106a758SMichael Guralnik 1324639bf441SAharon Landau err = mlx5_query_ib_port_oper(mdev, &ib_link_width_oper, 1325376ceb31SAharon Landau &props->active_speed, port); 13261b5daf11SMajd Dibbiny if (err) 13271b5daf11SMajd Dibbiny goto out; 13281b5daf11SMajd Dibbiny 1329db7a691aSMichael Guralnik translate_active_width(ibdev, ib_link_width_oper, &props->active_width); 1330db7a691aSMichael Guralnik 1331facc9699SSaeed Mahameed mlx5_query_port_max_mtu(mdev, &max_mtu, port); 13321b5daf11SMajd Dibbiny 13331b5daf11SMajd Dibbiny props->max_mtu = mlx5_mtu_to_ib_mtu(max_mtu); 13341b5daf11SMajd Dibbiny 1335facc9699SSaeed Mahameed mlx5_query_port_oper_mtu(mdev, &oper_mtu, port); 13361b5daf11SMajd Dibbiny 13371b5daf11SMajd Dibbiny props->active_mtu = mlx5_mtu_to_ib_mtu(oper_mtu); 13381b5daf11SMajd Dibbiny 13391b5daf11SMajd Dibbiny err = mlx5_query_port_vl_hw_cap(mdev, &vl_hw_cap, port); 13401b5daf11SMajd Dibbiny if (err) 13411b5daf11SMajd Dibbiny goto out; 13421b5daf11SMajd Dibbiny 13431b5daf11SMajd Dibbiny err = translate_max_vl_num(ibdev, vl_hw_cap, 13441b5daf11SMajd Dibbiny &props->max_vl_num); 13451b5daf11SMajd Dibbiny out: 13461b5daf11SMajd Dibbiny kfree(rep); 1347e126ba97SEli Cohen return err; 1348e126ba97SEli Cohen } 1349e126ba97SEli Cohen 13501fb7f897SMark Bloch int mlx5_ib_query_port(struct ib_device *ibdev, u32 port, 1351e126ba97SEli Cohen struct ib_port_attr *props) 1352e126ba97SEli Cohen { 1353095b0927SIlan Tayari unsigned int count; 1354095b0927SIlan Tayari int ret; 1355095b0927SIlan Tayari 13561b5daf11SMajd Dibbiny switch (mlx5_get_vport_access_method(ibdev)) { 13571b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_MAD: 1358095b0927SIlan Tayari ret = mlx5_query_mad_ifc_port(ibdev, port, props); 1359095b0927SIlan Tayari break; 1360e126ba97SEli Cohen 13611b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_HCA: 1362095b0927SIlan Tayari ret = mlx5_query_hca_port(ibdev, port, props); 1363095b0927SIlan Tayari break; 13641b5daf11SMajd Dibbiny 13653f89a643SAchiad Shochat case MLX5_VPORT_ACCESS_METHOD_NIC: 1366095b0927SIlan Tayari ret = mlx5_query_port_roce(ibdev, port, props); 1367095b0927SIlan Tayari break; 13683f89a643SAchiad Shochat 13691b5daf11SMajd Dibbiny default: 1370095b0927SIlan Tayari ret = -EINVAL; 1371e126ba97SEli Cohen } 1372095b0927SIlan Tayari 1373095b0927SIlan Tayari if (!ret && props) { 1374b3cbd6f0SDaniel Jurgens struct mlx5_ib_dev *dev = to_mdev(ibdev); 1375b3cbd6f0SDaniel Jurgens struct mlx5_core_dev *mdev; 1376b3cbd6f0SDaniel Jurgens bool put_mdev = true; 1377b3cbd6f0SDaniel Jurgens 1378b3cbd6f0SDaniel Jurgens mdev = mlx5_ib_get_native_port_mdev(dev, port, NULL); 1379b3cbd6f0SDaniel Jurgens if (!mdev) { 1380b3cbd6f0SDaniel Jurgens /* If the port isn't affiliated yet query the master. 1381b3cbd6f0SDaniel Jurgens * The master and slave will have the same values. 1382b3cbd6f0SDaniel Jurgens */ 1383b3cbd6f0SDaniel Jurgens mdev = dev->mdev; 1384b3cbd6f0SDaniel Jurgens port = 1; 1385b3cbd6f0SDaniel Jurgens put_mdev = false; 1386b3cbd6f0SDaniel Jurgens } 1387b3cbd6f0SDaniel Jurgens count = mlx5_core_reserved_gids_count(mdev); 1388b3cbd6f0SDaniel Jurgens if (put_mdev) 1389b3cbd6f0SDaniel Jurgens mlx5_ib_put_native_port_mdev(dev, port); 1390095b0927SIlan Tayari props->gid_tbl_len -= count; 1391095b0927SIlan Tayari } 1392095b0927SIlan Tayari return ret; 1393e126ba97SEli Cohen } 1394e126ba97SEli Cohen 13951fb7f897SMark Bloch static int mlx5_ib_rep_query_port(struct ib_device *ibdev, u32 port, 13968e6efa3aSMark Bloch struct ib_port_attr *props) 13978e6efa3aSMark Bloch { 13987a58779eSParav Pandit return mlx5_query_port_roce(ibdev, port, props); 13998e6efa3aSMark Bloch } 14008e6efa3aSMark Bloch 14011fb7f897SMark Bloch static int mlx5_ib_rep_query_pkey(struct ib_device *ibdev, u32 port, u16 index, 1402d6fd59e1SParav Pandit u16 *pkey) 1403d6fd59e1SParav Pandit { 1404d6fd59e1SParav Pandit /* Default special Pkey for representor device port as per the 1405d6fd59e1SParav Pandit * IB specification 1.3 section 10.9.1.2. 1406d6fd59e1SParav Pandit */ 1407d6fd59e1SParav Pandit *pkey = 0xffff; 1408d6fd59e1SParav Pandit return 0; 1409d6fd59e1SParav Pandit } 1410d6fd59e1SParav Pandit 14111fb7f897SMark Bloch static int mlx5_ib_query_gid(struct ib_device *ibdev, u32 port, int index, 1412e126ba97SEli Cohen union ib_gid *gid) 1413e126ba97SEli Cohen { 14141b5daf11SMajd Dibbiny struct mlx5_ib_dev *dev = to_mdev(ibdev); 14151b5daf11SMajd Dibbiny struct mlx5_core_dev *mdev = dev->mdev; 1416e126ba97SEli Cohen 14171b5daf11SMajd Dibbiny switch (mlx5_get_vport_access_method(ibdev)) { 14181b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_MAD: 14191b5daf11SMajd Dibbiny return mlx5_query_mad_ifc_gids(ibdev, port, index, gid); 1420e126ba97SEli Cohen 14211b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_HCA: 14221b5daf11SMajd Dibbiny return mlx5_query_hca_vport_gid(mdev, 0, port, 0, index, gid); 1423e126ba97SEli Cohen 14241b5daf11SMajd Dibbiny default: 14251b5daf11SMajd Dibbiny return -EINVAL; 14261b5daf11SMajd Dibbiny } 1427e126ba97SEli Cohen 1428e126ba97SEli Cohen } 1429e126ba97SEli Cohen 14301fb7f897SMark Bloch static int mlx5_query_hca_nic_pkey(struct ib_device *ibdev, u32 port, 1431b3cbd6f0SDaniel Jurgens u16 index, u16 *pkey) 1432b3cbd6f0SDaniel Jurgens { 1433b3cbd6f0SDaniel Jurgens struct mlx5_ib_dev *dev = to_mdev(ibdev); 1434b3cbd6f0SDaniel Jurgens struct mlx5_core_dev *mdev; 1435b3cbd6f0SDaniel Jurgens bool put_mdev = true; 14361fb7f897SMark Bloch u32 mdev_port_num; 1437b3cbd6f0SDaniel Jurgens int err; 1438b3cbd6f0SDaniel Jurgens 1439b3cbd6f0SDaniel Jurgens mdev = mlx5_ib_get_native_port_mdev(dev, port, &mdev_port_num); 1440b3cbd6f0SDaniel Jurgens if (!mdev) { 1441b3cbd6f0SDaniel Jurgens /* The port isn't affiliated yet, get the PKey from the master 1442b3cbd6f0SDaniel Jurgens * port. For RoCE the PKey tables will be the same. 1443b3cbd6f0SDaniel Jurgens */ 1444b3cbd6f0SDaniel Jurgens put_mdev = false; 1445b3cbd6f0SDaniel Jurgens mdev = dev->mdev; 1446b3cbd6f0SDaniel Jurgens mdev_port_num = 1; 1447b3cbd6f0SDaniel Jurgens } 1448b3cbd6f0SDaniel Jurgens 1449b3cbd6f0SDaniel Jurgens err = mlx5_query_hca_vport_pkey(mdev, 0, mdev_port_num, 0, 1450b3cbd6f0SDaniel Jurgens index, pkey); 1451b3cbd6f0SDaniel Jurgens if (put_mdev) 1452b3cbd6f0SDaniel Jurgens mlx5_ib_put_native_port_mdev(dev, port); 1453b3cbd6f0SDaniel Jurgens 1454b3cbd6f0SDaniel Jurgens return err; 1455b3cbd6f0SDaniel Jurgens } 1456b3cbd6f0SDaniel Jurgens 14571fb7f897SMark Bloch static int mlx5_ib_query_pkey(struct ib_device *ibdev, u32 port, u16 index, 1458e126ba97SEli Cohen u16 *pkey) 1459e126ba97SEli Cohen { 14601b5daf11SMajd Dibbiny switch (mlx5_get_vport_access_method(ibdev)) { 14611b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_MAD: 14621b5daf11SMajd Dibbiny return mlx5_query_mad_ifc_pkey(ibdev, port, index, pkey); 1463e126ba97SEli Cohen 14641b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_HCA: 14651b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_NIC: 1466b3cbd6f0SDaniel Jurgens return mlx5_query_hca_nic_pkey(ibdev, port, index, pkey); 14671b5daf11SMajd Dibbiny default: 14681b5daf11SMajd Dibbiny return -EINVAL; 1469e126ba97SEli Cohen } 14701b5daf11SMajd Dibbiny } 1471e126ba97SEli Cohen 1472e126ba97SEli Cohen static int mlx5_ib_modify_device(struct ib_device *ibdev, int mask, 1473e126ba97SEli Cohen struct ib_device_modify *props) 1474e126ba97SEli Cohen { 1475e126ba97SEli Cohen struct mlx5_ib_dev *dev = to_mdev(ibdev); 1476e126ba97SEli Cohen struct mlx5_reg_node_desc in; 1477e126ba97SEli Cohen struct mlx5_reg_node_desc out; 1478e126ba97SEli Cohen int err; 1479e126ba97SEli Cohen 1480e126ba97SEli Cohen if (mask & ~IB_DEVICE_MODIFY_NODE_DESC) 1481e126ba97SEli Cohen return -EOPNOTSUPP; 1482e126ba97SEli Cohen 1483e126ba97SEli Cohen if (!(mask & IB_DEVICE_MODIFY_NODE_DESC)) 1484e126ba97SEli Cohen return 0; 1485e126ba97SEli Cohen 1486e126ba97SEli Cohen /* 1487e126ba97SEli Cohen * If possible, pass node desc to FW, so it can generate 1488e126ba97SEli Cohen * a 144 trap. If cmd fails, just ignore. 1489e126ba97SEli Cohen */ 1490bd99fdeaSYuval Shaia memcpy(&in, props->node_desc, IB_DEVICE_NODE_DESC_MAX); 14919603b61dSJack Morgenstein err = mlx5_core_access_reg(dev->mdev, &in, sizeof(in), &out, 1492e126ba97SEli Cohen sizeof(out), MLX5_REG_NODE_DESC, 0, 1); 1493e126ba97SEli Cohen if (err) 1494e126ba97SEli Cohen return err; 1495e126ba97SEli Cohen 1496bd99fdeaSYuval Shaia memcpy(ibdev->node_desc, props->node_desc, IB_DEVICE_NODE_DESC_MAX); 1497e126ba97SEli Cohen 1498e126ba97SEli Cohen return err; 1499e126ba97SEli Cohen } 1500e126ba97SEli Cohen 15011fb7f897SMark Bloch static int set_port_caps_atomic(struct mlx5_ib_dev *dev, u32 port_num, u32 mask, 1502cdbe33d0SEli Cohen u32 value) 1503cdbe33d0SEli Cohen { 1504cdbe33d0SEli Cohen struct mlx5_hca_vport_context ctx = {}; 1505b3cbd6f0SDaniel Jurgens struct mlx5_core_dev *mdev; 15061fb7f897SMark Bloch u32 mdev_port_num; 1507cdbe33d0SEli Cohen int err; 1508cdbe33d0SEli Cohen 1509b3cbd6f0SDaniel Jurgens mdev = mlx5_ib_get_native_port_mdev(dev, port_num, &mdev_port_num); 1510b3cbd6f0SDaniel Jurgens if (!mdev) 1511b3cbd6f0SDaniel Jurgens return -ENODEV; 1512b3cbd6f0SDaniel Jurgens 1513b3cbd6f0SDaniel Jurgens err = mlx5_query_hca_vport_context(mdev, 0, mdev_port_num, 0, &ctx); 1514cdbe33d0SEli Cohen if (err) 1515b3cbd6f0SDaniel Jurgens goto out; 1516cdbe33d0SEli Cohen 1517cdbe33d0SEli Cohen if (~ctx.cap_mask1_perm & mask) { 1518cdbe33d0SEli Cohen mlx5_ib_warn(dev, "trying to change bitmask 0x%X but change supported 0x%X\n", 1519cdbe33d0SEli Cohen mask, ctx.cap_mask1_perm); 1520b3cbd6f0SDaniel Jurgens err = -EINVAL; 1521b3cbd6f0SDaniel Jurgens goto out; 1522cdbe33d0SEli Cohen } 1523cdbe33d0SEli Cohen 1524cdbe33d0SEli Cohen ctx.cap_mask1 = value; 1525cdbe33d0SEli Cohen ctx.cap_mask1_perm = mask; 1526b3cbd6f0SDaniel Jurgens err = mlx5_core_modify_hca_vport_context(mdev, 0, mdev_port_num, 1527b3cbd6f0SDaniel Jurgens 0, &ctx); 1528b3cbd6f0SDaniel Jurgens 1529b3cbd6f0SDaniel Jurgens out: 1530b3cbd6f0SDaniel Jurgens mlx5_ib_put_native_port_mdev(dev, port_num); 1531cdbe33d0SEli Cohen 1532cdbe33d0SEli Cohen return err; 1533cdbe33d0SEli Cohen } 1534cdbe33d0SEli Cohen 15351fb7f897SMark Bloch static int mlx5_ib_modify_port(struct ib_device *ibdev, u32 port, int mask, 1536e126ba97SEli Cohen struct ib_port_modify *props) 1537e126ba97SEli Cohen { 1538e126ba97SEli Cohen struct mlx5_ib_dev *dev = to_mdev(ibdev); 1539e126ba97SEli Cohen struct ib_port_attr attr; 1540e126ba97SEli Cohen u32 tmp; 1541e126ba97SEli Cohen int err; 1542cdbe33d0SEli Cohen u32 change_mask; 1543cdbe33d0SEli Cohen u32 value; 1544cdbe33d0SEli Cohen bool is_ib = (mlx5_ib_port_link_layer(ibdev, port) == 1545cdbe33d0SEli Cohen IB_LINK_LAYER_INFINIBAND); 1546cdbe33d0SEli Cohen 1547ec255879SMajd Dibbiny /* CM layer calls ib_modify_port() regardless of the link layer. For 1548ec255879SMajd Dibbiny * Ethernet ports, qkey violation and Port capabilities are meaningless. 1549ec255879SMajd Dibbiny */ 1550ec255879SMajd Dibbiny if (!is_ib) 1551ec255879SMajd Dibbiny return 0; 1552ec255879SMajd Dibbiny 1553cdbe33d0SEli Cohen if (MLX5_CAP_GEN(dev->mdev, ib_virt) && is_ib) { 1554cdbe33d0SEli Cohen change_mask = props->clr_port_cap_mask | props->set_port_cap_mask; 1555cdbe33d0SEli Cohen value = ~props->clr_port_cap_mask | props->set_port_cap_mask; 1556cdbe33d0SEli Cohen return set_port_caps_atomic(dev, port, change_mask, value); 1557cdbe33d0SEli Cohen } 1558e126ba97SEli Cohen 1559e126ba97SEli Cohen mutex_lock(&dev->cap_mask_mutex); 1560e126ba97SEli Cohen 1561c4550c63SOr Gerlitz err = ib_query_port(ibdev, port, &attr); 1562e126ba97SEli Cohen if (err) 1563e126ba97SEli Cohen goto out; 1564e126ba97SEli Cohen 1565e126ba97SEli Cohen tmp = (attr.port_cap_flags | props->set_port_cap_mask) & 1566e126ba97SEli Cohen ~props->clr_port_cap_mask; 1567e126ba97SEli Cohen 15689603b61dSJack Morgenstein err = mlx5_set_port_caps(dev->mdev, port, tmp); 1569e126ba97SEli Cohen 1570e126ba97SEli Cohen out: 1571e126ba97SEli Cohen mutex_unlock(&dev->cap_mask_mutex); 1572e126ba97SEli Cohen return err; 1573e126ba97SEli Cohen } 1574e126ba97SEli Cohen 157530aa60b3SEli Cohen static void print_lib_caps(struct mlx5_ib_dev *dev, u64 caps) 157630aa60b3SEli Cohen { 157730aa60b3SEli Cohen mlx5_ib_dbg(dev, "MLX5_LIB_CAP_4K_UAR = %s\n", 157830aa60b3SEli Cohen caps & MLX5_LIB_CAP_4K_UAR ? "y" : "n"); 157930aa60b3SEli Cohen } 158030aa60b3SEli Cohen 158131a78a5aSYishai Hadas static u16 calc_dynamic_bfregs(int uars_per_sys_page) 158231a78a5aSYishai Hadas { 158331a78a5aSYishai Hadas /* Large page with non 4k uar support might limit the dynamic size */ 158431a78a5aSYishai Hadas if (uars_per_sys_page == 1 && PAGE_SIZE > 4096) 158531a78a5aSYishai Hadas return MLX5_MIN_DYN_BFREGS; 158631a78a5aSYishai Hadas 158731a78a5aSYishai Hadas return MLX5_MAX_DYN_BFREGS; 158831a78a5aSYishai Hadas } 158931a78a5aSYishai Hadas 1590b037c29aSEli Cohen static int calc_total_bfregs(struct mlx5_ib_dev *dev, bool lib_uar_4k, 1591b037c29aSEli Cohen struct mlx5_ib_alloc_ucontext_req_v2 *req, 159231a78a5aSYishai Hadas struct mlx5_bfreg_info *bfregi) 1593b037c29aSEli Cohen { 1594b037c29aSEli Cohen int uars_per_sys_page; 1595b037c29aSEli Cohen int bfregs_per_sys_page; 1596b037c29aSEli Cohen int ref_bfregs = req->total_num_bfregs; 1597b037c29aSEli Cohen 1598b037c29aSEli Cohen if (req->total_num_bfregs == 0) 1599b037c29aSEli Cohen return -EINVAL; 1600b037c29aSEli Cohen 1601b037c29aSEli Cohen BUILD_BUG_ON(MLX5_MAX_BFREGS % MLX5_NON_FP_BFREGS_IN_PAGE); 1602b037c29aSEli Cohen BUILD_BUG_ON(MLX5_MAX_BFREGS < MLX5_NON_FP_BFREGS_IN_PAGE); 1603b037c29aSEli Cohen 1604b037c29aSEli Cohen if (req->total_num_bfregs > MLX5_MAX_BFREGS) 1605b037c29aSEli Cohen return -ENOMEM; 1606b037c29aSEli Cohen 1607b037c29aSEli Cohen uars_per_sys_page = get_uars_per_sys_page(dev, lib_uar_4k); 1608b037c29aSEli Cohen bfregs_per_sys_page = uars_per_sys_page * MLX5_NON_FP_BFREGS_PER_UAR; 160931a78a5aSYishai Hadas /* This holds the required static allocation asked by the user */ 1610b037c29aSEli Cohen req->total_num_bfregs = ALIGN(req->total_num_bfregs, bfregs_per_sys_page); 1611b037c29aSEli Cohen if (req->num_low_latency_bfregs > req->total_num_bfregs - 1) 1612b037c29aSEli Cohen return -EINVAL; 1613b037c29aSEli Cohen 161431a78a5aSYishai Hadas bfregi->num_static_sys_pages = req->total_num_bfregs / bfregs_per_sys_page; 161531a78a5aSYishai Hadas bfregi->num_dyn_bfregs = ALIGN(calc_dynamic_bfregs(uars_per_sys_page), bfregs_per_sys_page); 161631a78a5aSYishai Hadas bfregi->total_num_bfregs = req->total_num_bfregs + bfregi->num_dyn_bfregs; 161731a78a5aSYishai Hadas bfregi->num_sys_pages = bfregi->total_num_bfregs / bfregs_per_sys_page; 161831a78a5aSYishai Hadas 161931a78a5aSYishai 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", 1620b037c29aSEli Cohen MLX5_CAP_GEN(dev->mdev, uar_4k) ? "yes" : "no", 1621b037c29aSEli Cohen lib_uar_4k ? "yes" : "no", ref_bfregs, 162231a78a5aSYishai Hadas req->total_num_bfregs, bfregi->total_num_bfregs, 162331a78a5aSYishai Hadas bfregi->num_sys_pages); 1624b037c29aSEli Cohen 1625b037c29aSEli Cohen return 0; 1626b037c29aSEli Cohen } 1627b037c29aSEli Cohen 1628b037c29aSEli Cohen static int allocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context) 1629b037c29aSEli Cohen { 1630b037c29aSEli Cohen struct mlx5_bfreg_info *bfregi; 1631b037c29aSEli Cohen int err; 1632b037c29aSEli Cohen int i; 1633b037c29aSEli Cohen 1634b037c29aSEli Cohen bfregi = &context->bfregi; 163531a78a5aSYishai Hadas for (i = 0; i < bfregi->num_static_sys_pages; i++) { 1636b037c29aSEli Cohen err = mlx5_cmd_alloc_uar(dev->mdev, &bfregi->sys_pages[i]); 1637b037c29aSEli Cohen if (err) 1638b037c29aSEli Cohen goto error; 1639b037c29aSEli Cohen 1640b037c29aSEli Cohen mlx5_ib_dbg(dev, "allocated uar %d\n", bfregi->sys_pages[i]); 1641b037c29aSEli Cohen } 16424ed131d0SYishai Hadas 16434ed131d0SYishai Hadas for (i = bfregi->num_static_sys_pages; i < bfregi->num_sys_pages; i++) 16444ed131d0SYishai Hadas bfregi->sys_pages[i] = MLX5_IB_INVALID_UAR_INDEX; 16454ed131d0SYishai Hadas 1646b037c29aSEli Cohen return 0; 1647b037c29aSEli Cohen 1648b037c29aSEli Cohen error: 1649b037c29aSEli Cohen for (--i; i >= 0; i--) 1650b037c29aSEli Cohen if (mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i])) 1651b037c29aSEli Cohen mlx5_ib_warn(dev, "failed to free uar %d\n", i); 1652b037c29aSEli Cohen 1653b037c29aSEli Cohen return err; 1654b037c29aSEli Cohen } 1655b037c29aSEli Cohen 165615177999SLeon Romanovsky static void deallocate_uars(struct mlx5_ib_dev *dev, 165715177999SLeon Romanovsky struct mlx5_ib_ucontext *context) 1658b037c29aSEli Cohen { 1659b037c29aSEli Cohen struct mlx5_bfreg_info *bfregi; 1660b037c29aSEli Cohen int i; 1661b037c29aSEli Cohen 1662b037c29aSEli Cohen bfregi = &context->bfregi; 166315177999SLeon Romanovsky for (i = 0; i < bfregi->num_sys_pages; i++) 16644ed131d0SYishai Hadas if (i < bfregi->num_static_sys_pages || 166515177999SLeon Romanovsky bfregi->sys_pages[i] != MLX5_IB_INVALID_UAR_INDEX) 166615177999SLeon Romanovsky mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]); 1667b037c29aSEli Cohen } 1668b037c29aSEli Cohen 16690042f9e4SMark Bloch int mlx5_ib_enable_lb(struct mlx5_ib_dev *dev, bool td, bool qp) 1670a560f1d9SMark Bloch { 1671a560f1d9SMark Bloch int err = 0; 1672a560f1d9SMark Bloch 1673a560f1d9SMark Bloch mutex_lock(&dev->lb.mutex); 16740042f9e4SMark Bloch if (td) 1675a560f1d9SMark Bloch dev->lb.user_td++; 16760042f9e4SMark Bloch if (qp) 16770042f9e4SMark Bloch dev->lb.qps++; 1678a560f1d9SMark Bloch 16790042f9e4SMark Bloch if (dev->lb.user_td == 2 || 16800042f9e4SMark Bloch dev->lb.qps == 1) { 16810042f9e4SMark Bloch if (!dev->lb.enabled) { 1682a560f1d9SMark Bloch err = mlx5_nic_vport_update_local_lb(dev->mdev, true); 16830042f9e4SMark Bloch dev->lb.enabled = true; 16840042f9e4SMark Bloch } 16850042f9e4SMark Bloch } 1686a560f1d9SMark Bloch 1687a560f1d9SMark Bloch mutex_unlock(&dev->lb.mutex); 1688a560f1d9SMark Bloch 1689a560f1d9SMark Bloch return err; 1690a560f1d9SMark Bloch } 1691a560f1d9SMark Bloch 16920042f9e4SMark Bloch void mlx5_ib_disable_lb(struct mlx5_ib_dev *dev, bool td, bool qp) 1693a560f1d9SMark Bloch { 1694a560f1d9SMark Bloch mutex_lock(&dev->lb.mutex); 16950042f9e4SMark Bloch if (td) 1696a560f1d9SMark Bloch dev->lb.user_td--; 16970042f9e4SMark Bloch if (qp) 16980042f9e4SMark Bloch dev->lb.qps--; 1699a560f1d9SMark Bloch 17000042f9e4SMark Bloch if (dev->lb.user_td == 1 && 17010042f9e4SMark Bloch dev->lb.qps == 0) { 17020042f9e4SMark Bloch if (dev->lb.enabled) { 1703a560f1d9SMark Bloch mlx5_nic_vport_update_local_lb(dev->mdev, false); 17040042f9e4SMark Bloch dev->lb.enabled = false; 17050042f9e4SMark Bloch } 17060042f9e4SMark Bloch } 1707a560f1d9SMark Bloch 1708a560f1d9SMark Bloch mutex_unlock(&dev->lb.mutex); 1709a560f1d9SMark Bloch } 1710a560f1d9SMark Bloch 1711d2d19121SYishai Hadas static int mlx5_ib_alloc_transport_domain(struct mlx5_ib_dev *dev, u32 *tdn, 1712d2d19121SYishai Hadas u16 uid) 1713c85023e1SHuy Nguyen { 1714c85023e1SHuy Nguyen int err; 1715c85023e1SHuy Nguyen 1716cfdeb893SLeon Romanovsky if (!MLX5_CAP_GEN(dev->mdev, log_max_transport_domain)) 1717cfdeb893SLeon Romanovsky return 0; 1718cfdeb893SLeon Romanovsky 1719d2d19121SYishai Hadas err = mlx5_cmd_alloc_transport_domain(dev->mdev, tdn, uid); 1720c85023e1SHuy Nguyen if (err) 1721c85023e1SHuy Nguyen return err; 1722c85023e1SHuy Nguyen 1723c85023e1SHuy Nguyen if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) || 17248978cc92SEran Ben Elisha (!MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) && 17258978cc92SEran Ben Elisha !MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc))) 1726c85023e1SHuy Nguyen return err; 1727c85023e1SHuy Nguyen 17280042f9e4SMark Bloch return mlx5_ib_enable_lb(dev, true, false); 1729c85023e1SHuy Nguyen } 1730c85023e1SHuy Nguyen 1731d2d19121SYishai Hadas static void mlx5_ib_dealloc_transport_domain(struct mlx5_ib_dev *dev, u32 tdn, 1732d2d19121SYishai Hadas u16 uid) 1733c85023e1SHuy Nguyen { 1734cfdeb893SLeon Romanovsky if (!MLX5_CAP_GEN(dev->mdev, log_max_transport_domain)) 1735cfdeb893SLeon Romanovsky return; 1736cfdeb893SLeon Romanovsky 1737d2d19121SYishai Hadas mlx5_cmd_dealloc_transport_domain(dev->mdev, tdn, uid); 1738c85023e1SHuy Nguyen 1739c85023e1SHuy Nguyen if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) || 17408978cc92SEran Ben Elisha (!MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) && 17418978cc92SEran Ben Elisha !MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc))) 1742c85023e1SHuy Nguyen return; 1743c85023e1SHuy Nguyen 17440042f9e4SMark Bloch mlx5_ib_disable_lb(dev, true, false); 1745c85023e1SHuy Nguyen } 1746c85023e1SHuy Nguyen 174745ec21c9SYishai Hadas static int set_ucontext_resp(struct ib_ucontext *uctx, 174845ec21c9SYishai Hadas struct mlx5_ib_alloc_ucontext_resp *resp) 174945ec21c9SYishai Hadas { 175045ec21c9SYishai Hadas struct ib_device *ibdev = uctx->device; 175145ec21c9SYishai Hadas struct mlx5_ib_dev *dev = to_mdev(ibdev); 175245ec21c9SYishai Hadas struct mlx5_ib_ucontext *context = to_mucontext(uctx); 175345ec21c9SYishai Hadas struct mlx5_bfreg_info *bfregi = &context->bfregi; 175445ec21c9SYishai Hadas int err; 175545ec21c9SYishai Hadas 175645ec21c9SYishai Hadas if (MLX5_CAP_GEN(dev->mdev, dump_fill_mkey)) { 175745ec21c9SYishai Hadas err = mlx5_cmd_dump_fill_mkey(dev->mdev, 175845ec21c9SYishai Hadas &resp->dump_fill_mkey); 175945ec21c9SYishai Hadas if (err) 176045ec21c9SYishai Hadas return err; 176145ec21c9SYishai Hadas resp->comp_mask |= 176245ec21c9SYishai Hadas MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_DUMP_FILL_MKEY; 176345ec21c9SYishai Hadas } 176445ec21c9SYishai Hadas 176545ec21c9SYishai Hadas resp->qp_tab_size = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp); 176645ec21c9SYishai Hadas if (dev->wc_support) 176745ec21c9SYishai Hadas resp->bf_reg_size = 1 << MLX5_CAP_GEN(dev->mdev, 176845ec21c9SYishai Hadas log_bf_reg_size); 176945ec21c9SYishai Hadas resp->cache_line_size = cache_line_size(); 177045ec21c9SYishai Hadas resp->max_sq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq); 177145ec21c9SYishai Hadas resp->max_rq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq); 177245ec21c9SYishai Hadas resp->max_send_wqebb = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz); 177345ec21c9SYishai Hadas resp->max_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz); 177445ec21c9SYishai Hadas resp->max_srq_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz); 177545ec21c9SYishai Hadas resp->cqe_version = context->cqe_version; 177645ec21c9SYishai Hadas resp->log_uar_size = MLX5_CAP_GEN(dev->mdev, uar_4k) ? 177745ec21c9SYishai Hadas MLX5_ADAPTER_PAGE_SHIFT : PAGE_SHIFT; 177845ec21c9SYishai Hadas resp->num_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ? 177945ec21c9SYishai Hadas MLX5_CAP_GEN(dev->mdev, 178045ec21c9SYishai Hadas num_of_uars_per_page) : 1; 178145ec21c9SYishai Hadas 178245ec21c9SYishai Hadas if (mlx5_accel_ipsec_device_caps(dev->mdev) & 178345ec21c9SYishai Hadas MLX5_ACCEL_IPSEC_CAP_DEVICE) { 178445ec21c9SYishai Hadas if (mlx5_get_flow_namespace(dev->mdev, 178545ec21c9SYishai Hadas MLX5_FLOW_NAMESPACE_EGRESS)) 178645ec21c9SYishai Hadas resp->flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM; 178745ec21c9SYishai Hadas if (mlx5_accel_ipsec_device_caps(dev->mdev) & 178845ec21c9SYishai Hadas MLX5_ACCEL_IPSEC_CAP_REQUIRED_METADATA) 178945ec21c9SYishai Hadas resp->flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_REQ_METADATA; 179045ec21c9SYishai Hadas if (MLX5_CAP_FLOWTABLE(dev->mdev, flow_table_properties_nic_receive.ft_field_support.outer_esp_spi)) 179145ec21c9SYishai Hadas resp->flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_SPI_STEERING; 179245ec21c9SYishai Hadas if (mlx5_accel_ipsec_device_caps(dev->mdev) & 179345ec21c9SYishai Hadas MLX5_ACCEL_IPSEC_CAP_TX_IV_IS_ESN) 179445ec21c9SYishai Hadas resp->flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_TX_IV_IS_ESN; 179545ec21c9SYishai Hadas /* MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_FULL_OFFLOAD is currently always 0 */ 179645ec21c9SYishai Hadas } 179745ec21c9SYishai Hadas 179845ec21c9SYishai Hadas resp->tot_bfregs = bfregi->lib_uar_dyn ? 0 : 179945ec21c9SYishai Hadas bfregi->total_num_bfregs - bfregi->num_dyn_bfregs; 180045ec21c9SYishai Hadas resp->num_ports = dev->num_ports; 180145ec21c9SYishai Hadas resp->cmds_supp_uhw |= MLX5_USER_CMDS_SUPP_UHW_QUERY_DEVICE | 180245ec21c9SYishai Hadas MLX5_USER_CMDS_SUPP_UHW_CREATE_AH; 180345ec21c9SYishai Hadas 180445ec21c9SYishai Hadas if (mlx5_ib_port_link_layer(ibdev, 1) == IB_LINK_LAYER_ETHERNET) { 180545ec21c9SYishai Hadas mlx5_query_min_inline(dev->mdev, &resp->eth_min_inline); 180645ec21c9SYishai Hadas resp->eth_min_inline++; 180745ec21c9SYishai Hadas } 180845ec21c9SYishai Hadas 180945ec21c9SYishai Hadas if (dev->mdev->clock_info) 181045ec21c9SYishai Hadas resp->clock_info_versions = BIT(MLX5_IB_CLOCK_INFO_V1); 181145ec21c9SYishai Hadas 181245ec21c9SYishai Hadas /* 181345ec21c9SYishai Hadas * We don't want to expose information from the PCI bar that is located 181445ec21c9SYishai Hadas * after 4096 bytes, so if the arch only supports larger pages, let's 181545ec21c9SYishai Hadas * pretend we don't support reading the HCA's core clock. This is also 181645ec21c9SYishai Hadas * forced by mmap function. 181745ec21c9SYishai Hadas */ 181845ec21c9SYishai Hadas if (PAGE_SIZE <= 4096) { 181945ec21c9SYishai Hadas resp->comp_mask |= 182045ec21c9SYishai Hadas MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_CORE_CLOCK_OFFSET; 182145ec21c9SYishai Hadas resp->hca_core_clock_offset = 182245ec21c9SYishai Hadas offsetof(struct mlx5_init_seg, 182345ec21c9SYishai Hadas internal_timer_h) % PAGE_SIZE; 182445ec21c9SYishai Hadas } 182545ec21c9SYishai Hadas 182645ec21c9SYishai Hadas if (MLX5_CAP_GEN(dev->mdev, ece_support)) 182745ec21c9SYishai Hadas resp->comp_mask |= MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_ECE; 182845ec21c9SYishai Hadas 182933652951SAharon Landau if (rt_supported(MLX5_CAP_GEN(dev->mdev, sq_ts_format)) && 183033652951SAharon Landau rt_supported(MLX5_CAP_GEN(dev->mdev, rq_ts_format)) && 183133652951SAharon Landau rt_supported(MLX5_CAP_ROCE(dev->mdev, qp_ts_format))) 183233652951SAharon Landau resp->comp_mask |= 183333652951SAharon Landau MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_REAL_TIME_TS; 183433652951SAharon Landau 183545ec21c9SYishai Hadas resp->num_dyn_bfregs = bfregi->num_dyn_bfregs; 1836c906b86eSSergey Gorenko 1837c906b86eSSergey Gorenko if (MLX5_CAP_GEN(dev->mdev, drain_sigerr)) 1838c906b86eSSergey Gorenko resp->comp_mask |= MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_SQD2RTS; 1839c906b86eSSergey Gorenko 184045ec21c9SYishai Hadas return 0; 184145ec21c9SYishai Hadas } 184245ec21c9SYishai Hadas 1843a2a074efSLeon Romanovsky static int mlx5_ib_alloc_ucontext(struct ib_ucontext *uctx, 1844e126ba97SEli Cohen struct ib_udata *udata) 1845e126ba97SEli Cohen { 1846a2a074efSLeon Romanovsky struct ib_device *ibdev = uctx->device; 1847e126ba97SEli Cohen struct mlx5_ib_dev *dev = to_mdev(ibdev); 1848b368d7cbSMatan Barak struct mlx5_ib_alloc_ucontext_req_v2 req = {}; 1849b368d7cbSMatan Barak struct mlx5_ib_alloc_ucontext_resp resp = {}; 1850a2a074efSLeon Romanovsky struct mlx5_ib_ucontext *context = to_mucontext(uctx); 18512f5ff264SEli Cohen struct mlx5_bfreg_info *bfregi; 185278c0f98cSEli Cohen int ver; 1853e126ba97SEli Cohen int err; 1854a168a41cSMajd Dibbiny size_t min_req_v2 = offsetof(struct mlx5_ib_alloc_ucontext_req_v2, 1855a168a41cSMajd Dibbiny max_cqe_version); 1856b037c29aSEli Cohen bool lib_uar_4k; 18570a2fd01cSYishai Hadas bool lib_uar_dyn; 1858e126ba97SEli Cohen 1859e126ba97SEli Cohen if (!dev->ib_active) 1860a2a074efSLeon Romanovsky return -EAGAIN; 1861e126ba97SEli Cohen 1862e093111dSAmrani, Ram if (udata->inlen == sizeof(struct mlx5_ib_alloc_ucontext_req)) 186378c0f98cSEli Cohen ver = 0; 1864e093111dSAmrani, Ram else if (udata->inlen >= min_req_v2) 186578c0f98cSEli Cohen ver = 2; 186678c0f98cSEli Cohen else 1867a2a074efSLeon Romanovsky return -EINVAL; 186878c0f98cSEli Cohen 1869e093111dSAmrani, Ram err = ib_copy_from_udata(&req, udata, min(udata->inlen, sizeof(req))); 1870e126ba97SEli Cohen if (err) 1871a2a074efSLeon Romanovsky return err; 1872e126ba97SEli Cohen 1873a8b92ca1SYishai Hadas if (req.flags & ~MLX5_IB_ALLOC_UCTX_DEVX) 1874a2a074efSLeon Romanovsky return -EOPNOTSUPP; 187578c0f98cSEli Cohen 1876f72300c5SHaggai Abramovsky if (req.comp_mask || req.reserved0 || req.reserved1 || req.reserved2) 1877a2a074efSLeon Romanovsky return -EOPNOTSUPP; 1878b368d7cbSMatan Barak 18792f5ff264SEli Cohen req.total_num_bfregs = ALIGN(req.total_num_bfregs, 18802f5ff264SEli Cohen MLX5_NON_FP_BFREGS_PER_UAR); 18812f5ff264SEli Cohen if (req.num_low_latency_bfregs > req.total_num_bfregs - 1) 1882a2a074efSLeon Romanovsky return -EINVAL; 1883e126ba97SEli Cohen 188430aa60b3SEli Cohen lib_uar_4k = req.lib_caps & MLX5_LIB_CAP_4K_UAR; 18850a2fd01cSYishai Hadas lib_uar_dyn = req.lib_caps & MLX5_LIB_CAP_DYN_UAR; 18862f5ff264SEli Cohen bfregi = &context->bfregi; 1887b037c29aSEli Cohen 18880a2fd01cSYishai Hadas if (lib_uar_dyn) { 18890a2fd01cSYishai Hadas bfregi->lib_uar_dyn = lib_uar_dyn; 18900a2fd01cSYishai Hadas goto uar_done; 18910a2fd01cSYishai Hadas } 18920a2fd01cSYishai Hadas 1893b037c29aSEli Cohen /* updates req->total_num_bfregs */ 189431a78a5aSYishai Hadas err = calc_total_bfregs(dev, lib_uar_4k, &req, bfregi); 1895b037c29aSEli Cohen if (err) 1896b037c29aSEli Cohen goto out_ctx; 1897b037c29aSEli Cohen 18982f5ff264SEli Cohen mutex_init(&bfregi->lock); 1899b037c29aSEli Cohen bfregi->lib_uar_4k = lib_uar_4k; 190031a78a5aSYishai Hadas bfregi->count = kcalloc(bfregi->total_num_bfregs, sizeof(*bfregi->count), 1901b037c29aSEli Cohen GFP_KERNEL); 1902b037c29aSEli Cohen if (!bfregi->count) { 1903e126ba97SEli Cohen err = -ENOMEM; 1904e126ba97SEli Cohen goto out_ctx; 1905e126ba97SEli Cohen } 1906e126ba97SEli Cohen 1907b037c29aSEli Cohen bfregi->sys_pages = kcalloc(bfregi->num_sys_pages, 1908b037c29aSEli Cohen sizeof(*bfregi->sys_pages), 1909e126ba97SEli Cohen GFP_KERNEL); 1910b037c29aSEli Cohen if (!bfregi->sys_pages) { 1911e126ba97SEli Cohen err = -ENOMEM; 1912e126ba97SEli Cohen goto out_count; 1913e126ba97SEli Cohen } 1914e126ba97SEli Cohen 1915b037c29aSEli Cohen err = allocate_uars(dev, context); 1916b037c29aSEli Cohen if (err) 1917b037c29aSEli Cohen goto out_sys_pages; 1918b037c29aSEli Cohen 19190a2fd01cSYishai Hadas uar_done: 1920a8b92ca1SYishai Hadas if (req.flags & MLX5_IB_ALLOC_UCTX_DEVX) { 1921fb98153bSYishai Hadas err = mlx5_ib_devx_create(dev, true); 192276dc5a84SYishai Hadas if (err < 0) 1923d2d19121SYishai Hadas goto out_uars; 192476dc5a84SYishai Hadas context->devx_uid = err; 1925a8b92ca1SYishai Hadas } 1926a8b92ca1SYishai Hadas 1927d2d19121SYishai Hadas err = mlx5_ib_alloc_transport_domain(dev, &context->tdn, 1928d2d19121SYishai Hadas context->devx_uid); 1929a8b92ca1SYishai Hadas if (err) 1930d2d19121SYishai Hadas goto out_devx; 1931a8b92ca1SYishai Hadas 1932e126ba97SEli Cohen INIT_LIST_HEAD(&context->db_page_list); 1933e126ba97SEli Cohen mutex_init(&context->db_page_mutex); 1934e126ba97SEli Cohen 193545ec21c9SYishai Hadas context->cqe_version = min_t(__u8, 193645ec21c9SYishai Hadas (__u8)MLX5_CAP_GEN(dev->mdev, cqe_version), 193745ec21c9SYishai Hadas req.max_cqe_version); 1938b368d7cbSMatan Barak 193945ec21c9SYishai Hadas err = set_ucontext_resp(uctx, &resp); 194045ec21c9SYishai Hadas if (err) 194145ec21c9SYishai Hadas goto out_mdev; 1942b368d7cbSMatan Barak 194345ec21c9SYishai Hadas resp.response_length = min(udata->outlen, sizeof(resp)); 1944b368d7cbSMatan Barak err = ib_copy_to_udata(udata, &resp, resp.response_length); 1945e126ba97SEli Cohen if (err) 1946a8b92ca1SYishai Hadas goto out_mdev; 1947e126ba97SEli Cohen 19482f5ff264SEli Cohen bfregi->ver = ver; 19492f5ff264SEli Cohen bfregi->num_low_latency_bfregs = req.num_low_latency_bfregs; 195030aa60b3SEli Cohen context->lib_caps = req.lib_caps; 195130aa60b3SEli Cohen print_lib_caps(dev, context->lib_caps); 1952f72300c5SHaggai Abramovsky 1953802dcc7fSMark Zhang if (mlx5_ib_lag_should_assign_affinity(dev)) { 19541fb7f897SMark Bloch u32 port = mlx5_core_native_port_num(dev->mdev) - 1; 1955c6a21c38SMajd Dibbiny 1956c6a21c38SMajd Dibbiny atomic_set(&context->tx_port_affinity, 1957c6a21c38SMajd Dibbiny atomic_add_return( 195895579e78SMark Bloch 1, &dev->port[port].roce.tx_port_affinity)); 1959c6a21c38SMajd Dibbiny } 1960c6a21c38SMajd Dibbiny 1961a2a074efSLeon Romanovsky return 0; 1962e126ba97SEli Cohen 1963a8b92ca1SYishai Hadas out_mdev: 1964d2d19121SYishai Hadas mlx5_ib_dealloc_transport_domain(dev, context->tdn, context->devx_uid); 1965d2d19121SYishai Hadas out_devx: 1966a8b92ca1SYishai Hadas if (req.flags & MLX5_IB_ALLOC_UCTX_DEVX) 196776dc5a84SYishai Hadas mlx5_ib_devx_destroy(dev, context->devx_uid); 1968146d2f1aSmajd@mellanox.com 1969e126ba97SEli Cohen out_uars: 1970b037c29aSEli Cohen deallocate_uars(dev, context); 1971b037c29aSEli Cohen 1972b037c29aSEli Cohen out_sys_pages: 1973b037c29aSEli Cohen kfree(bfregi->sys_pages); 1974b037c29aSEli Cohen 1975e126ba97SEli Cohen out_count: 19762f5ff264SEli Cohen kfree(bfregi->count); 1977e126ba97SEli Cohen 1978e126ba97SEli Cohen out_ctx: 1979a2a074efSLeon Romanovsky return err; 1980e126ba97SEli Cohen } 1981e126ba97SEli Cohen 19820fb556b2SYishai Hadas static int mlx5_ib_query_ucontext(struct ib_ucontext *ibcontext, 19830fb556b2SYishai Hadas struct uverbs_attr_bundle *attrs) 19840fb556b2SYishai Hadas { 19850fb556b2SYishai Hadas struct mlx5_ib_alloc_ucontext_resp uctx_resp = {}; 19860fb556b2SYishai Hadas int ret; 19870fb556b2SYishai Hadas 19880fb556b2SYishai Hadas ret = set_ucontext_resp(ibcontext, &uctx_resp); 19890fb556b2SYishai Hadas if (ret) 19900fb556b2SYishai Hadas return ret; 19910fb556b2SYishai Hadas 19920fb556b2SYishai Hadas uctx_resp.response_length = 19930fb556b2SYishai Hadas min_t(size_t, 19940fb556b2SYishai Hadas uverbs_attr_get_len(attrs, 19950fb556b2SYishai Hadas MLX5_IB_ATTR_QUERY_CONTEXT_RESP_UCTX), 19960fb556b2SYishai Hadas sizeof(uctx_resp)); 19970fb556b2SYishai Hadas 19980fb556b2SYishai Hadas ret = uverbs_copy_to_struct_or_zero(attrs, 19990fb556b2SYishai Hadas MLX5_IB_ATTR_QUERY_CONTEXT_RESP_UCTX, 20000fb556b2SYishai Hadas &uctx_resp, 20010fb556b2SYishai Hadas sizeof(uctx_resp)); 20020fb556b2SYishai Hadas return ret; 20030fb556b2SYishai Hadas } 20040fb556b2SYishai Hadas 2005a2a074efSLeon Romanovsky static void mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext) 2006e126ba97SEli Cohen { 2007e126ba97SEli Cohen struct mlx5_ib_ucontext *context = to_mucontext(ibcontext); 2008e126ba97SEli Cohen struct mlx5_ib_dev *dev = to_mdev(ibcontext->device); 2009b037c29aSEli Cohen struct mlx5_bfreg_info *bfregi; 2010e126ba97SEli Cohen 2011b037c29aSEli Cohen bfregi = &context->bfregi; 2012d2d19121SYishai Hadas mlx5_ib_dealloc_transport_domain(dev, context->tdn, context->devx_uid); 2013d2d19121SYishai Hadas 2014b037c29aSEli Cohen if (context->devx_uid) 201576dc5a84SYishai Hadas mlx5_ib_devx_destroy(dev, context->devx_uid); 2016e126ba97SEli Cohen 2017e126ba97SEli Cohen deallocate_uars(dev, context); 20182f5ff264SEli Cohen kfree(bfregi->sys_pages); 20192f5ff264SEli Cohen kfree(bfregi->count); 2020e126ba97SEli Cohen } 2021e126ba97SEli Cohen 2022e126ba97SEli Cohen static phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev, 2023e126ba97SEli Cohen int uar_idx) 2024e126ba97SEli Cohen { 2025b037c29aSEli Cohen int fw_uars_per_page; 2026b037c29aSEli Cohen 2027b037c29aSEli Cohen fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ? MLX5_UARS_IN_PAGE : 1; 2028b037c29aSEli Cohen 2029aa8106f1SHuy Nguyen return (dev->mdev->bar_addr >> PAGE_SHIFT) + uar_idx / fw_uars_per_page; 2030e126ba97SEli Cohen } 2031e126ba97SEli Cohen 2032342ee59dSYishai Hadas static u64 uar_index2paddress(struct mlx5_ib_dev *dev, 2033342ee59dSYishai Hadas int uar_idx) 2034342ee59dSYishai Hadas { 2035342ee59dSYishai Hadas unsigned int fw_uars_per_page; 2036342ee59dSYishai Hadas 2037342ee59dSYishai Hadas fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ? 2038342ee59dSYishai Hadas MLX5_UARS_IN_PAGE : 1; 2039342ee59dSYishai Hadas 2040342ee59dSYishai Hadas return (dev->mdev->bar_addr + (uar_idx / fw_uars_per_page) * PAGE_SIZE); 2041342ee59dSYishai Hadas } 2042342ee59dSYishai Hadas 2043e126ba97SEli Cohen static int get_command(unsigned long offset) 2044e126ba97SEli Cohen { 2045e126ba97SEli Cohen return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK; 2046e126ba97SEli Cohen } 2047e126ba97SEli Cohen 2048e126ba97SEli Cohen static int get_arg(unsigned long offset) 2049e126ba97SEli Cohen { 2050e126ba97SEli Cohen return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1); 2051e126ba97SEli Cohen } 2052e126ba97SEli Cohen 2053e126ba97SEli Cohen static int get_index(unsigned long offset) 2054e126ba97SEli Cohen { 2055e126ba97SEli Cohen return get_arg(offset); 2056e126ba97SEli Cohen } 2057e126ba97SEli Cohen 20584ed131d0SYishai Hadas /* Index resides in an extra byte to enable larger values than 255 */ 20594ed131d0SYishai Hadas static int get_extended_index(unsigned long offset) 20604ed131d0SYishai Hadas { 20614ed131d0SYishai Hadas return get_arg(offset) | ((offset >> 16) & 0xff) << 8; 20624ed131d0SYishai Hadas } 20634ed131d0SYishai Hadas 20647c2344c3SMaor Gottlieb 20657c2344c3SMaor Gottlieb static void mlx5_ib_disassociate_ucontext(struct ib_ucontext *ibcontext) 20667c2344c3SMaor Gottlieb { 20677c2344c3SMaor Gottlieb } 20687c2344c3SMaor Gottlieb 206937aa5c36SGuy Levi static inline char *mmap_cmd2str(enum mlx5_ib_mmap_cmd cmd) 2070e126ba97SEli Cohen { 207137aa5c36SGuy Levi switch (cmd) { 207237aa5c36SGuy Levi case MLX5_IB_MMAP_WC_PAGE: 207337aa5c36SGuy Levi return "WC"; 2074e126ba97SEli Cohen case MLX5_IB_MMAP_REGULAR_PAGE: 207537aa5c36SGuy Levi return "best effort WC"; 207637aa5c36SGuy Levi case MLX5_IB_MMAP_NC_PAGE: 207737aa5c36SGuy Levi return "NC"; 207824da0016SAriel Levkovich case MLX5_IB_MMAP_DEVICE_MEM: 207924da0016SAriel Levkovich return "Device Memory"; 208037aa5c36SGuy Levi default: 208137aa5c36SGuy Levi return NULL; 208237aa5c36SGuy Levi } 208337aa5c36SGuy Levi } 208437aa5c36SGuy Levi 20855c99eaecSFeras Daoud static int mlx5_ib_mmap_clock_info_page(struct mlx5_ib_dev *dev, 20865c99eaecSFeras Daoud struct vm_area_struct *vma, 20875c99eaecSFeras Daoud struct mlx5_ib_ucontext *context) 20885c99eaecSFeras Daoud { 20894eb6ab13SJason Gunthorpe if ((vma->vm_end - vma->vm_start != PAGE_SIZE) || 20904eb6ab13SJason Gunthorpe !(vma->vm_flags & VM_SHARED)) 20915c99eaecSFeras Daoud return -EINVAL; 20925c99eaecSFeras Daoud 20935c99eaecSFeras Daoud if (get_index(vma->vm_pgoff) != MLX5_IB_CLOCK_INFO_V1) 20945c99eaecSFeras Daoud return -EOPNOTSUPP; 20955c99eaecSFeras Daoud 20964eb6ab13SJason Gunthorpe if (vma->vm_flags & (VM_WRITE | VM_EXEC)) 20975c99eaecSFeras Daoud return -EPERM; 2098c660133cSJason Gunthorpe vma->vm_flags &= ~VM_MAYWRITE; 20995c99eaecSFeras Daoud 2100ddcdc368SJason Gunthorpe if (!dev->mdev->clock_info) 21015c99eaecSFeras Daoud return -EOPNOTSUPP; 21025c99eaecSFeras Daoud 21034eb6ab13SJason Gunthorpe return vm_insert_page(vma, vma->vm_start, 21044eb6ab13SJason Gunthorpe virt_to_page(dev->mdev->clock_info)); 21055c99eaecSFeras Daoud } 21065c99eaecSFeras Daoud 2107dc2316ebSYishai Hadas static void mlx5_ib_mmap_free(struct rdma_user_mmap_entry *entry) 2108dc2316ebSYishai Hadas { 2109dc2316ebSYishai Hadas struct mlx5_user_mmap_entry *mentry = to_mmmap(entry); 2110dc2316ebSYishai Hadas struct mlx5_ib_dev *dev = to_mdev(entry->ucontext->device); 21117be76befSYishai Hadas struct mlx5_var_table *var_table = &dev->var_table; 2112dc2316ebSYishai Hadas 2113dc2316ebSYishai Hadas switch (mentry->mmap_flag) { 2114dc2316ebSYishai Hadas case MLX5_IB_MMAP_TYPE_MEMIC: 2115cea85fa5SMaor Gottlieb case MLX5_IB_MMAP_TYPE_MEMIC_OP: 2116cea85fa5SMaor Gottlieb mlx5_ib_dm_mmap_free(dev, mentry); 2117dc2316ebSYishai Hadas break; 21187be76befSYishai Hadas case MLX5_IB_MMAP_TYPE_VAR: 21197be76befSYishai Hadas mutex_lock(&var_table->bitmap_lock); 21207be76befSYishai Hadas clear_bit(mentry->page_idx, var_table->bitmap); 21217be76befSYishai Hadas mutex_unlock(&var_table->bitmap_lock); 21227be76befSYishai Hadas kfree(mentry); 21237be76befSYishai Hadas break; 2124342ee59dSYishai Hadas case MLX5_IB_MMAP_TYPE_UAR_WC: 2125342ee59dSYishai Hadas case MLX5_IB_MMAP_TYPE_UAR_NC: 2126342ee59dSYishai Hadas mlx5_cmd_free_uar(dev->mdev, mentry->page_idx); 2127342ee59dSYishai Hadas kfree(mentry); 2128342ee59dSYishai Hadas break; 2129dc2316ebSYishai Hadas default: 2130dc2316ebSYishai Hadas WARN_ON(true); 2131dc2316ebSYishai Hadas } 2132dc2316ebSYishai Hadas } 2133dc2316ebSYishai Hadas 213437aa5c36SGuy Levi static int uar_mmap(struct mlx5_ib_dev *dev, enum mlx5_ib_mmap_cmd cmd, 21357c2344c3SMaor Gottlieb struct vm_area_struct *vma, 21367c2344c3SMaor Gottlieb struct mlx5_ib_ucontext *context) 213737aa5c36SGuy Levi { 21382f5ff264SEli Cohen struct mlx5_bfreg_info *bfregi = &context->bfregi; 213937aa5c36SGuy Levi int err; 214037aa5c36SGuy Levi unsigned long idx; 2141aa09ea6eSKamal Heib phys_addr_t pfn; 214237aa5c36SGuy Levi pgprot_t prot; 21434ed131d0SYishai Hadas u32 bfreg_dyn_idx = 0; 21444ed131d0SYishai Hadas u32 uar_index; 21454ed131d0SYishai Hadas int dyn_uar = (cmd == MLX5_IB_MMAP_ALLOC_WC); 21464ed131d0SYishai Hadas int max_valid_idx = dyn_uar ? bfregi->num_sys_pages : 21474ed131d0SYishai Hadas bfregi->num_static_sys_pages; 2148b037c29aSEli Cohen 21490a2fd01cSYishai Hadas if (bfregi->lib_uar_dyn) 21500a2fd01cSYishai Hadas return -EINVAL; 21510a2fd01cSYishai Hadas 2152b037c29aSEli Cohen if (vma->vm_end - vma->vm_start != PAGE_SIZE) 2153b037c29aSEli Cohen return -EINVAL; 2154b037c29aSEli Cohen 21554ed131d0SYishai Hadas if (dyn_uar) 21564ed131d0SYishai Hadas idx = get_extended_index(vma->vm_pgoff) + bfregi->num_static_sys_pages; 21574ed131d0SYishai Hadas else 2158b037c29aSEli Cohen idx = get_index(vma->vm_pgoff); 21594ed131d0SYishai Hadas 21604ed131d0SYishai Hadas if (idx >= max_valid_idx) { 21614ed131d0SYishai Hadas mlx5_ib_warn(dev, "invalid uar index %lu, max=%d\n", 21624ed131d0SYishai Hadas idx, max_valid_idx); 2163b037c29aSEli Cohen return -EINVAL; 2164b037c29aSEli Cohen } 216537aa5c36SGuy Levi 216637aa5c36SGuy Levi switch (cmd) { 216737aa5c36SGuy Levi case MLX5_IB_MMAP_WC_PAGE: 21684ed131d0SYishai Hadas case MLX5_IB_MMAP_ALLOC_WC: 216937aa5c36SGuy Levi case MLX5_IB_MMAP_REGULAR_PAGE: 217037aa5c36SGuy Levi /* For MLX5_IB_MMAP_REGULAR_PAGE do the best effort to get WC */ 217137aa5c36SGuy Levi prot = pgprot_writecombine(vma->vm_page_prot); 217237aa5c36SGuy Levi break; 217337aa5c36SGuy Levi case MLX5_IB_MMAP_NC_PAGE: 217437aa5c36SGuy Levi prot = pgprot_noncached(vma->vm_page_prot); 217537aa5c36SGuy Levi break; 217637aa5c36SGuy Levi default: 217737aa5c36SGuy Levi return -EINVAL; 217837aa5c36SGuy Levi } 217937aa5c36SGuy Levi 21804ed131d0SYishai Hadas if (dyn_uar) { 21814ed131d0SYishai Hadas int uars_per_page; 21824ed131d0SYishai Hadas 21834ed131d0SYishai Hadas uars_per_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k); 21844ed131d0SYishai Hadas bfreg_dyn_idx = idx * (uars_per_page * MLX5_NON_FP_BFREGS_PER_UAR); 21854ed131d0SYishai Hadas if (bfreg_dyn_idx >= bfregi->total_num_bfregs) { 21864ed131d0SYishai Hadas mlx5_ib_warn(dev, "invalid bfreg_dyn_idx %u, max=%u\n", 21874ed131d0SYishai Hadas bfreg_dyn_idx, bfregi->total_num_bfregs); 21884ed131d0SYishai Hadas return -EINVAL; 21894ed131d0SYishai Hadas } 21904ed131d0SYishai Hadas 21914ed131d0SYishai Hadas mutex_lock(&bfregi->lock); 21924ed131d0SYishai Hadas /* Fail if uar already allocated, first bfreg index of each 21934ed131d0SYishai Hadas * page holds its count. 21944ed131d0SYishai Hadas */ 21954ed131d0SYishai Hadas if (bfregi->count[bfreg_dyn_idx]) { 21964ed131d0SYishai Hadas mlx5_ib_warn(dev, "wrong offset, idx %lu is busy, bfregn=%u\n", idx, bfreg_dyn_idx); 21974ed131d0SYishai Hadas mutex_unlock(&bfregi->lock); 21984ed131d0SYishai Hadas return -EINVAL; 21994ed131d0SYishai Hadas } 22004ed131d0SYishai Hadas 22014ed131d0SYishai Hadas bfregi->count[bfreg_dyn_idx]++; 22024ed131d0SYishai Hadas mutex_unlock(&bfregi->lock); 22034ed131d0SYishai Hadas 22044ed131d0SYishai Hadas err = mlx5_cmd_alloc_uar(dev->mdev, &uar_index); 22054ed131d0SYishai Hadas if (err) { 22064ed131d0SYishai Hadas mlx5_ib_warn(dev, "UAR alloc failed\n"); 22074ed131d0SYishai Hadas goto free_bfreg; 22084ed131d0SYishai Hadas } 22094ed131d0SYishai Hadas } else { 22104ed131d0SYishai Hadas uar_index = bfregi->sys_pages[idx]; 22114ed131d0SYishai Hadas } 22124ed131d0SYishai Hadas 22134ed131d0SYishai Hadas pfn = uar_index2pfn(dev, uar_index); 221437aa5c36SGuy Levi mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn %pa\n", idx, &pfn); 2215e126ba97SEli Cohen 2216e2cd1d1aSJason Gunthorpe err = rdma_user_mmap_io(&context->ibucontext, vma, pfn, PAGE_SIZE, 2217c043ff2cSMichal Kalderon prot, NULL); 221837aa5c36SGuy Levi if (err) { 22198f062287SLeon Romanovsky mlx5_ib_err(dev, 2220e2cd1d1aSJason Gunthorpe "rdma_user_mmap_io failed with error=%d, mmap_cmd=%s\n", 22218f062287SLeon Romanovsky err, mmap_cmd2str(cmd)); 22224ed131d0SYishai Hadas goto err; 222337aa5c36SGuy Levi } 2224e126ba97SEli Cohen 22254ed131d0SYishai Hadas if (dyn_uar) 22264ed131d0SYishai Hadas bfregi->sys_pages[idx] = uar_index; 22274ed131d0SYishai Hadas return 0; 22284ed131d0SYishai Hadas 22294ed131d0SYishai Hadas err: 22304ed131d0SYishai Hadas if (!dyn_uar) 22314ed131d0SYishai Hadas return err; 22324ed131d0SYishai Hadas 22334ed131d0SYishai Hadas mlx5_cmd_free_uar(dev->mdev, idx); 22344ed131d0SYishai Hadas 22354ed131d0SYishai Hadas free_bfreg: 22364ed131d0SYishai Hadas mlx5_ib_free_bfreg(dev, bfregi, bfreg_dyn_idx); 22374ed131d0SYishai Hadas 22384ed131d0SYishai Hadas return err; 223937aa5c36SGuy Levi } 224037aa5c36SGuy Levi 2241dc2316ebSYishai Hadas static unsigned long mlx5_vma_to_pgoff(struct vm_area_struct *vma) 2242dc2316ebSYishai Hadas { 2243dc2316ebSYishai Hadas unsigned long idx; 2244dc2316ebSYishai Hadas u8 command; 2245dc2316ebSYishai Hadas 2246dc2316ebSYishai Hadas command = get_command(vma->vm_pgoff); 2247dc2316ebSYishai Hadas idx = get_extended_index(vma->vm_pgoff); 2248dc2316ebSYishai Hadas 2249dc2316ebSYishai Hadas return (command << 16 | idx); 2250dc2316ebSYishai Hadas } 2251dc2316ebSYishai Hadas 2252dc2316ebSYishai Hadas static int mlx5_ib_mmap_offset(struct mlx5_ib_dev *dev, 2253dc2316ebSYishai Hadas struct vm_area_struct *vma, 2254dc2316ebSYishai Hadas struct ib_ucontext *ucontext) 2255dc2316ebSYishai Hadas { 2256dc2316ebSYishai Hadas struct mlx5_user_mmap_entry *mentry; 2257dc2316ebSYishai Hadas struct rdma_user_mmap_entry *entry; 2258dc2316ebSYishai Hadas unsigned long pgoff; 2259dc2316ebSYishai Hadas pgprot_t prot; 2260dc2316ebSYishai Hadas phys_addr_t pfn; 2261dc2316ebSYishai Hadas int ret; 2262dc2316ebSYishai Hadas 2263dc2316ebSYishai Hadas pgoff = mlx5_vma_to_pgoff(vma); 2264dc2316ebSYishai Hadas entry = rdma_user_mmap_entry_get_pgoff(ucontext, pgoff); 2265dc2316ebSYishai Hadas if (!entry) 226624da0016SAriel Levkovich return -EINVAL; 226724da0016SAriel Levkovich 2268dc2316ebSYishai Hadas mentry = to_mmmap(entry); 2269dc2316ebSYishai Hadas pfn = (mentry->address >> PAGE_SHIFT); 2270342ee59dSYishai Hadas if (mentry->mmap_flag == MLX5_IB_MMAP_TYPE_VAR || 2271342ee59dSYishai Hadas mentry->mmap_flag == MLX5_IB_MMAP_TYPE_UAR_NC) 22723f59b6c3SYishai Hadas prot = pgprot_noncached(vma->vm_page_prot); 22733f59b6c3SYishai Hadas else 2274dc2316ebSYishai Hadas prot = pgprot_writecombine(vma->vm_page_prot); 2275dc2316ebSYishai Hadas ret = rdma_user_mmap_io(ucontext, vma, pfn, 2276dc2316ebSYishai Hadas entry->npages * PAGE_SIZE, 2277dc2316ebSYishai Hadas prot, 2278dc2316ebSYishai Hadas entry); 2279dc2316ebSYishai Hadas rdma_user_mmap_entry_put(&mentry->rdma_entry); 2280dc2316ebSYishai Hadas return ret; 228124da0016SAriel Levkovich } 228224da0016SAriel Levkovich 22837be76befSYishai Hadas static u64 mlx5_entry_to_mmap_offset(struct mlx5_user_mmap_entry *entry) 22847be76befSYishai Hadas { 22859b6d3bbcSLeon Romanovsky u64 cmd = (entry->rdma_entry.start_pgoff >> 16) & 0xFFFF; 22869b6d3bbcSLeon Romanovsky u64 index = entry->rdma_entry.start_pgoff & 0xFFFF; 22877be76befSYishai Hadas 22887be76befSYishai Hadas return (((index >> 8) << 16) | (cmd << MLX5_IB_MMAP_CMD_SHIFT) | 22897be76befSYishai Hadas (index & 0xFF)) << PAGE_SHIFT; 22907be76befSYishai Hadas } 22917be76befSYishai Hadas 229237aa5c36SGuy Levi static int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma) 229337aa5c36SGuy Levi { 229437aa5c36SGuy Levi struct mlx5_ib_ucontext *context = to_mucontext(ibcontext); 229537aa5c36SGuy Levi struct mlx5_ib_dev *dev = to_mdev(ibcontext->device); 229637aa5c36SGuy Levi unsigned long command; 229737aa5c36SGuy Levi phys_addr_t pfn; 229837aa5c36SGuy Levi 229937aa5c36SGuy Levi command = get_command(vma->vm_pgoff); 230037aa5c36SGuy Levi switch (command) { 230137aa5c36SGuy Levi case MLX5_IB_MMAP_WC_PAGE: 23021f3db161SYishai Hadas case MLX5_IB_MMAP_ALLOC_WC: 23031f3db161SYishai Hadas if (!dev->wc_support) 23041f3db161SYishai Hadas return -EPERM; 23051f3db161SYishai Hadas fallthrough; 230637aa5c36SGuy Levi case MLX5_IB_MMAP_NC_PAGE: 230737aa5c36SGuy Levi case MLX5_IB_MMAP_REGULAR_PAGE: 23087c2344c3SMaor Gottlieb return uar_mmap(dev, command, vma, context); 2309e126ba97SEli Cohen 2310e126ba97SEli Cohen case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES: 2311e126ba97SEli Cohen return -ENOSYS; 2312e126ba97SEli Cohen 2313d69e3bcfSMatan Barak case MLX5_IB_MMAP_CORE_CLOCK: 2314d69e3bcfSMatan Barak if (vma->vm_end - vma->vm_start != PAGE_SIZE) 2315d69e3bcfSMatan Barak return -EINVAL; 2316d69e3bcfSMatan Barak 23176cbac1e4SMatan Barak if (vma->vm_flags & VM_WRITE) 2318d69e3bcfSMatan Barak return -EPERM; 2319c660133cSJason Gunthorpe vma->vm_flags &= ~VM_MAYWRITE; 2320d69e3bcfSMatan Barak 2321d69e3bcfSMatan Barak /* Don't expose to user-space information it shouldn't have */ 2322d69e3bcfSMatan Barak if (PAGE_SIZE > 4096) 2323d69e3bcfSMatan Barak return -EOPNOTSUPP; 2324d69e3bcfSMatan Barak 2325d69e3bcfSMatan Barak pfn = (dev->mdev->iseg_base + 2326d69e3bcfSMatan Barak offsetof(struct mlx5_init_seg, internal_timer_h)) >> 2327d69e3bcfSMatan Barak PAGE_SHIFT; 2328d5e560d3SJason Gunthorpe return rdma_user_mmap_io(&context->ibucontext, vma, pfn, 2329d5e560d3SJason Gunthorpe PAGE_SIZE, 2330c043ff2cSMichal Kalderon pgprot_noncached(vma->vm_page_prot), 2331c043ff2cSMichal Kalderon NULL); 23325c99eaecSFeras Daoud case MLX5_IB_MMAP_CLOCK_INFO: 23335c99eaecSFeras Daoud return mlx5_ib_mmap_clock_info_page(dev, vma, context); 2334d69e3bcfSMatan Barak 2335e126ba97SEli Cohen default: 2336dc2316ebSYishai Hadas return mlx5_ib_mmap_offset(dev, vma, ibcontext); 2337e126ba97SEli Cohen } 2338e126ba97SEli Cohen 2339e126ba97SEli Cohen return 0; 2340e126ba97SEli Cohen } 2341e126ba97SEli Cohen 2342ff23dfa1SShamir Rabinovitch static int mlx5_ib_alloc_pd(struct ib_pd *ibpd, struct ib_udata *udata) 2343e126ba97SEli Cohen { 234421a428a0SLeon Romanovsky struct mlx5_ib_pd *pd = to_mpd(ibpd); 234521a428a0SLeon Romanovsky struct ib_device *ibdev = ibpd->device; 2346e126ba97SEli Cohen struct mlx5_ib_alloc_pd_resp resp; 2347e126ba97SEli Cohen int err; 2348a1069c1cSYishai Hadas u32 out[MLX5_ST_SZ_DW(alloc_pd_out)] = {}; 2349a1069c1cSYishai Hadas u32 in[MLX5_ST_SZ_DW(alloc_pd_in)] = {}; 2350a1069c1cSYishai Hadas u16 uid = 0; 2351ff23dfa1SShamir Rabinovitch struct mlx5_ib_ucontext *context = rdma_udata_to_drv_context( 2352ff23dfa1SShamir Rabinovitch udata, struct mlx5_ib_ucontext, ibucontext); 2353e126ba97SEli Cohen 2354ff23dfa1SShamir Rabinovitch uid = context ? context->devx_uid : 0; 2355a1069c1cSYishai Hadas MLX5_SET(alloc_pd_in, in, opcode, MLX5_CMD_OP_ALLOC_PD); 2356a1069c1cSYishai Hadas MLX5_SET(alloc_pd_in, in, uid, uid); 235731578defSLeon Romanovsky err = mlx5_cmd_exec_inout(to_mdev(ibdev)->mdev, alloc_pd, in, out); 235821a428a0SLeon Romanovsky if (err) 235921a428a0SLeon Romanovsky return err; 2360e126ba97SEli Cohen 2361a1069c1cSYishai Hadas pd->pdn = MLX5_GET(alloc_pd_out, out, pd); 2362a1069c1cSYishai Hadas pd->uid = uid; 2363ff23dfa1SShamir Rabinovitch if (udata) { 2364e126ba97SEli Cohen resp.pdn = pd->pdn; 2365e126ba97SEli Cohen if (ib_copy_to_udata(udata, &resp, sizeof(resp))) { 2366a1069c1cSYishai Hadas mlx5_cmd_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn, uid); 236721a428a0SLeon Romanovsky return -EFAULT; 2368e126ba97SEli Cohen } 2369e126ba97SEli Cohen } 2370e126ba97SEli Cohen 237121a428a0SLeon Romanovsky return 0; 2372e126ba97SEli Cohen } 2373e126ba97SEli Cohen 237491a7c58fSLeon Romanovsky static int mlx5_ib_dealloc_pd(struct ib_pd *pd, struct ib_udata *udata) 2375e126ba97SEli Cohen { 2376e126ba97SEli Cohen struct mlx5_ib_dev *mdev = to_mdev(pd->device); 2377e126ba97SEli Cohen struct mlx5_ib_pd *mpd = to_mpd(pd); 2378e126ba97SEli Cohen 237991a7c58fSLeon Romanovsky return mlx5_cmd_dealloc_pd(mdev->mdev, mpd->pdn, mpd->uid); 2380e126ba97SEli Cohen } 2381e126ba97SEli Cohen 2382e126ba97SEli Cohen static int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid) 2383e126ba97SEli Cohen { 2384e126ba97SEli Cohen struct mlx5_ib_dev *dev = to_mdev(ibqp->device); 238581e30880SYishai Hadas struct mlx5_ib_qp *mqp = to_mqp(ibqp); 2386e126ba97SEli Cohen int err; 2387539ec982SYishai Hadas u16 uid; 2388539ec982SYishai Hadas 2389539ec982SYishai Hadas uid = ibqp->pd ? 2390539ec982SYishai Hadas to_mpd(ibqp->pd)->uid : 0; 2391e126ba97SEli Cohen 23922be08c30SLeon Romanovsky if (mqp->flags & IB_QP_CREATE_SOURCE_QPN) { 239381e30880SYishai Hadas mlx5_ib_dbg(dev, "Attaching a multi cast group to underlay QP is not supported\n"); 239481e30880SYishai Hadas return -EOPNOTSUPP; 239581e30880SYishai Hadas } 239681e30880SYishai Hadas 2397539ec982SYishai Hadas err = mlx5_cmd_attach_mcg(dev->mdev, gid, ibqp->qp_num, uid); 2398e126ba97SEli Cohen if (err) 2399e126ba97SEli Cohen mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n", 2400e126ba97SEli Cohen ibqp->qp_num, gid->raw); 2401e126ba97SEli Cohen 2402e126ba97SEli Cohen return err; 2403e126ba97SEli Cohen } 2404e126ba97SEli Cohen 2405e126ba97SEli Cohen static int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid) 2406e126ba97SEli Cohen { 2407e126ba97SEli Cohen struct mlx5_ib_dev *dev = to_mdev(ibqp->device); 2408e126ba97SEli Cohen int err; 2409539ec982SYishai Hadas u16 uid; 2410e126ba97SEli Cohen 2411539ec982SYishai Hadas uid = ibqp->pd ? 2412539ec982SYishai Hadas to_mpd(ibqp->pd)->uid : 0; 2413539ec982SYishai Hadas err = mlx5_cmd_detach_mcg(dev->mdev, gid, ibqp->qp_num, uid); 2414e126ba97SEli Cohen if (err) 2415e126ba97SEli Cohen mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n", 2416e126ba97SEli Cohen ibqp->qp_num, gid->raw); 2417e126ba97SEli Cohen 2418e126ba97SEli Cohen return err; 2419e126ba97SEli Cohen } 2420e126ba97SEli Cohen 2421e126ba97SEli Cohen static int init_node_data(struct mlx5_ib_dev *dev) 2422e126ba97SEli Cohen { 24231b5daf11SMajd Dibbiny int err; 2424e126ba97SEli Cohen 24251b5daf11SMajd Dibbiny err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc); 2426e126ba97SEli Cohen if (err) 2427e126ba97SEli Cohen return err; 24281b5daf11SMajd Dibbiny 24291b5daf11SMajd Dibbiny dev->mdev->rev_id = dev->mdev->pdev->revision; 24301b5daf11SMajd Dibbiny 24311b5daf11SMajd Dibbiny return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid); 2432e126ba97SEli Cohen } 2433e126ba97SEli Cohen 2434508a523fSParav Pandit static ssize_t fw_pages_show(struct device *device, 2435508a523fSParav Pandit struct device_attribute *attr, char *buf) 2436e126ba97SEli Cohen { 2437e126ba97SEli Cohen struct mlx5_ib_dev *dev = 243854747231SParav Pandit rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev); 2439e126ba97SEli Cohen 24401c7fd726SJoe Perches return sysfs_emit(buf, "%d\n", dev->mdev->priv.fw_pages); 2441e126ba97SEli Cohen } 2442508a523fSParav Pandit static DEVICE_ATTR_RO(fw_pages); 2443e126ba97SEli Cohen 2444508a523fSParav Pandit static ssize_t reg_pages_show(struct device *device, 2445e126ba97SEli Cohen struct device_attribute *attr, char *buf) 2446e126ba97SEli Cohen { 2447e126ba97SEli Cohen struct mlx5_ib_dev *dev = 244854747231SParav Pandit rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev); 2449e126ba97SEli Cohen 24501c7fd726SJoe Perches return sysfs_emit(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages)); 2451e126ba97SEli Cohen } 2452508a523fSParav Pandit static DEVICE_ATTR_RO(reg_pages); 2453e126ba97SEli Cohen 2454508a523fSParav Pandit static ssize_t hca_type_show(struct device *device, 2455508a523fSParav Pandit struct device_attribute *attr, char *buf) 2456e126ba97SEli Cohen { 2457e126ba97SEli Cohen struct mlx5_ib_dev *dev = 245854747231SParav Pandit rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev); 245954747231SParav Pandit 24601c7fd726SJoe Perches return sysfs_emit(buf, "MT%d\n", dev->mdev->pdev->device); 2461e126ba97SEli Cohen } 2462508a523fSParav Pandit static DEVICE_ATTR_RO(hca_type); 2463e126ba97SEli Cohen 2464508a523fSParav Pandit static ssize_t hw_rev_show(struct device *device, 2465508a523fSParav Pandit struct device_attribute *attr, char *buf) 2466e126ba97SEli Cohen { 2467e126ba97SEli Cohen struct mlx5_ib_dev *dev = 246854747231SParav Pandit rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev); 246954747231SParav Pandit 24701c7fd726SJoe Perches return sysfs_emit(buf, "%x\n", dev->mdev->rev_id); 2471e126ba97SEli Cohen } 2472508a523fSParav Pandit static DEVICE_ATTR_RO(hw_rev); 2473e126ba97SEli Cohen 2474508a523fSParav Pandit static ssize_t board_id_show(struct device *device, 2475508a523fSParav Pandit struct device_attribute *attr, char *buf) 2476e126ba97SEli Cohen { 2477e126ba97SEli Cohen struct mlx5_ib_dev *dev = 247854747231SParav Pandit rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev); 247954747231SParav Pandit 24801c7fd726SJoe Perches return sysfs_emit(buf, "%.*s\n", MLX5_BOARD_ID_LEN, 24819603b61dSJack Morgenstein dev->mdev->board_id); 2482e126ba97SEli Cohen } 2483508a523fSParav Pandit static DEVICE_ATTR_RO(board_id); 2484e126ba97SEli Cohen 2485508a523fSParav Pandit static struct attribute *mlx5_class_attributes[] = { 2486508a523fSParav Pandit &dev_attr_hw_rev.attr, 2487508a523fSParav Pandit &dev_attr_hca_type.attr, 2488508a523fSParav Pandit &dev_attr_board_id.attr, 2489508a523fSParav Pandit &dev_attr_fw_pages.attr, 2490508a523fSParav Pandit &dev_attr_reg_pages.attr, 2491508a523fSParav Pandit NULL, 2492508a523fSParav Pandit }; 2493e126ba97SEli Cohen 2494508a523fSParav Pandit static const struct attribute_group mlx5_attr_group = { 2495508a523fSParav Pandit .attrs = mlx5_class_attributes, 2496e126ba97SEli Cohen }; 2497e126ba97SEli Cohen 24987722f47eSHaggai Eran static void pkey_change_handler(struct work_struct *work) 24997722f47eSHaggai Eran { 25007722f47eSHaggai Eran struct mlx5_ib_port_resources *ports = 25017722f47eSHaggai Eran container_of(work, struct mlx5_ib_port_resources, 25027722f47eSHaggai Eran pkey_change_work); 25037722f47eSHaggai Eran 2504*8c9e7f03SLeon Romanovsky if (!ports->gsi) 2505*8c9e7f03SLeon Romanovsky /* 2506*8c9e7f03SLeon Romanovsky * We got this event before device was fully configured 2507*8c9e7f03SLeon Romanovsky * and MAD registration code wasn't called/finished yet. 2508*8c9e7f03SLeon Romanovsky */ 2509*8c9e7f03SLeon Romanovsky return; 2510*8c9e7f03SLeon Romanovsky 25117722f47eSHaggai Eran mlx5_ib_gsi_pkey_change(ports->gsi); 25127722f47eSHaggai Eran } 25137722f47eSHaggai Eran 251489ea94a7SMaor Gottlieb static void mlx5_ib_handle_internal_error(struct mlx5_ib_dev *ibdev) 251589ea94a7SMaor Gottlieb { 251689ea94a7SMaor Gottlieb struct mlx5_ib_qp *mqp; 251789ea94a7SMaor Gottlieb struct mlx5_ib_cq *send_mcq, *recv_mcq; 251889ea94a7SMaor Gottlieb struct mlx5_core_cq *mcq; 251989ea94a7SMaor Gottlieb struct list_head cq_armed_list; 252089ea94a7SMaor Gottlieb unsigned long flags_qp; 252189ea94a7SMaor Gottlieb unsigned long flags_cq; 252289ea94a7SMaor Gottlieb unsigned long flags; 252389ea94a7SMaor Gottlieb 252489ea94a7SMaor Gottlieb INIT_LIST_HEAD(&cq_armed_list); 252589ea94a7SMaor Gottlieb 252689ea94a7SMaor Gottlieb /* Go over qp list reside on that ibdev, sync with create/destroy qp.*/ 252789ea94a7SMaor Gottlieb spin_lock_irqsave(&ibdev->reset_flow_resource_lock, flags); 252889ea94a7SMaor Gottlieb list_for_each_entry(mqp, &ibdev->qp_list, qps_list) { 252989ea94a7SMaor Gottlieb spin_lock_irqsave(&mqp->sq.lock, flags_qp); 253089ea94a7SMaor Gottlieb if (mqp->sq.tail != mqp->sq.head) { 253189ea94a7SMaor Gottlieb send_mcq = to_mcq(mqp->ibqp.send_cq); 253289ea94a7SMaor Gottlieb spin_lock_irqsave(&send_mcq->lock, flags_cq); 253389ea94a7SMaor Gottlieb if (send_mcq->mcq.comp && 253489ea94a7SMaor Gottlieb mqp->ibqp.send_cq->comp_handler) { 253589ea94a7SMaor Gottlieb if (!send_mcq->mcq.reset_notify_added) { 253689ea94a7SMaor Gottlieb send_mcq->mcq.reset_notify_added = 1; 253789ea94a7SMaor Gottlieb list_add_tail(&send_mcq->mcq.reset_notify, 253889ea94a7SMaor Gottlieb &cq_armed_list); 253989ea94a7SMaor Gottlieb } 254089ea94a7SMaor Gottlieb } 254189ea94a7SMaor Gottlieb spin_unlock_irqrestore(&send_mcq->lock, flags_cq); 254289ea94a7SMaor Gottlieb } 254389ea94a7SMaor Gottlieb spin_unlock_irqrestore(&mqp->sq.lock, flags_qp); 254489ea94a7SMaor Gottlieb spin_lock_irqsave(&mqp->rq.lock, flags_qp); 254589ea94a7SMaor Gottlieb /* no handling is needed for SRQ */ 254689ea94a7SMaor Gottlieb if (!mqp->ibqp.srq) { 254789ea94a7SMaor Gottlieb if (mqp->rq.tail != mqp->rq.head) { 254889ea94a7SMaor Gottlieb recv_mcq = to_mcq(mqp->ibqp.recv_cq); 254989ea94a7SMaor Gottlieb spin_lock_irqsave(&recv_mcq->lock, flags_cq); 255089ea94a7SMaor Gottlieb if (recv_mcq->mcq.comp && 255189ea94a7SMaor Gottlieb mqp->ibqp.recv_cq->comp_handler) { 255289ea94a7SMaor Gottlieb if (!recv_mcq->mcq.reset_notify_added) { 255389ea94a7SMaor Gottlieb recv_mcq->mcq.reset_notify_added = 1; 255489ea94a7SMaor Gottlieb list_add_tail(&recv_mcq->mcq.reset_notify, 255589ea94a7SMaor Gottlieb &cq_armed_list); 255689ea94a7SMaor Gottlieb } 255789ea94a7SMaor Gottlieb } 255889ea94a7SMaor Gottlieb spin_unlock_irqrestore(&recv_mcq->lock, 255989ea94a7SMaor Gottlieb flags_cq); 256089ea94a7SMaor Gottlieb } 256189ea94a7SMaor Gottlieb } 256289ea94a7SMaor Gottlieb spin_unlock_irqrestore(&mqp->rq.lock, flags_qp); 256389ea94a7SMaor Gottlieb } 256489ea94a7SMaor Gottlieb /*At that point all inflight post send were put to be executed as of we 256589ea94a7SMaor Gottlieb * lock/unlock above locks Now need to arm all involved CQs. 256689ea94a7SMaor Gottlieb */ 256789ea94a7SMaor Gottlieb list_for_each_entry(mcq, &cq_armed_list, reset_notify) { 25684e0e2ea1SYishai Hadas mcq->comp(mcq, NULL); 256989ea94a7SMaor Gottlieb } 257089ea94a7SMaor Gottlieb spin_unlock_irqrestore(&ibdev->reset_flow_resource_lock, flags); 257189ea94a7SMaor Gottlieb } 257289ea94a7SMaor Gottlieb 257303404e8aSMaor Gottlieb static void delay_drop_handler(struct work_struct *work) 257403404e8aSMaor Gottlieb { 257503404e8aSMaor Gottlieb int err; 257603404e8aSMaor Gottlieb struct mlx5_ib_delay_drop *delay_drop = 257703404e8aSMaor Gottlieb container_of(work, struct mlx5_ib_delay_drop, 257803404e8aSMaor Gottlieb delay_drop_work); 257903404e8aSMaor Gottlieb 2580fe248c3aSMaor Gottlieb atomic_inc(&delay_drop->events_cnt); 2581fe248c3aSMaor Gottlieb 258203404e8aSMaor Gottlieb mutex_lock(&delay_drop->lock); 2583333fbaa0SLeon Romanovsky err = mlx5_core_set_delay_drop(delay_drop->dev, delay_drop->timeout); 258403404e8aSMaor Gottlieb if (err) { 258503404e8aSMaor Gottlieb mlx5_ib_warn(delay_drop->dev, "Failed to set delay drop, timeout=%u\n", 258603404e8aSMaor Gottlieb delay_drop->timeout); 258703404e8aSMaor Gottlieb delay_drop->activate = false; 258803404e8aSMaor Gottlieb } 258903404e8aSMaor Gottlieb mutex_unlock(&delay_drop->lock); 259003404e8aSMaor Gottlieb } 259103404e8aSMaor Gottlieb 259209e574faSSaeed Mahameed static void handle_general_event(struct mlx5_ib_dev *ibdev, struct mlx5_eqe *eqe, 259309e574faSSaeed Mahameed struct ib_event *ibev) 259409e574faSSaeed Mahameed { 25951fb7f897SMark Bloch u32 port = (eqe->data.port.port >> 4) & 0xf; 25966cfdc7e4SAya Levin 259709e574faSSaeed Mahameed switch (eqe->sub_type) { 259809e574faSSaeed Mahameed case MLX5_GENERAL_SUBTYPE_DELAY_DROP_TIMEOUT: 25996cfdc7e4SAya Levin if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) == 26006cfdc7e4SAya Levin IB_LINK_LAYER_ETHERNET) 260109e574faSSaeed Mahameed schedule_work(&ibdev->delay_drop.delay_drop_work); 260209e574faSSaeed Mahameed break; 260309e574faSSaeed Mahameed default: /* do nothing */ 260409e574faSSaeed Mahameed return; 260509e574faSSaeed Mahameed } 260609e574faSSaeed Mahameed } 260709e574faSSaeed Mahameed 2608134e9349SSaeed Mahameed static int handle_port_change(struct mlx5_ib_dev *ibdev, struct mlx5_eqe *eqe, 2609134e9349SSaeed Mahameed struct ib_event *ibev) 2610134e9349SSaeed Mahameed { 26111fb7f897SMark Bloch u32 port = (eqe->data.port.port >> 4) & 0xf; 2612134e9349SSaeed Mahameed 2613134e9349SSaeed Mahameed ibev->element.port_num = port; 2614134e9349SSaeed Mahameed 2615134e9349SSaeed Mahameed switch (eqe->sub_type) { 2616134e9349SSaeed Mahameed case MLX5_PORT_CHANGE_SUBTYPE_ACTIVE: 2617134e9349SSaeed Mahameed case MLX5_PORT_CHANGE_SUBTYPE_DOWN: 2618134e9349SSaeed Mahameed case MLX5_PORT_CHANGE_SUBTYPE_INITIALIZED: 2619134e9349SSaeed Mahameed /* In RoCE, port up/down events are handled in 2620134e9349SSaeed Mahameed * mlx5_netdev_event(). 2621134e9349SSaeed Mahameed */ 2622134e9349SSaeed Mahameed if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) == 2623134e9349SSaeed Mahameed IB_LINK_LAYER_ETHERNET) 2624134e9349SSaeed Mahameed return -EINVAL; 2625134e9349SSaeed Mahameed 2626134e9349SSaeed Mahameed ibev->event = (eqe->sub_type == MLX5_PORT_CHANGE_SUBTYPE_ACTIVE) ? 2627134e9349SSaeed Mahameed IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR; 2628134e9349SSaeed Mahameed break; 2629134e9349SSaeed Mahameed 2630134e9349SSaeed Mahameed case MLX5_PORT_CHANGE_SUBTYPE_LID: 2631134e9349SSaeed Mahameed ibev->event = IB_EVENT_LID_CHANGE; 2632134e9349SSaeed Mahameed break; 2633134e9349SSaeed Mahameed 2634134e9349SSaeed Mahameed case MLX5_PORT_CHANGE_SUBTYPE_PKEY: 2635134e9349SSaeed Mahameed ibev->event = IB_EVENT_PKEY_CHANGE; 2636134e9349SSaeed Mahameed schedule_work(&ibdev->devr.ports[port - 1].pkey_change_work); 2637134e9349SSaeed Mahameed break; 2638134e9349SSaeed Mahameed 2639134e9349SSaeed Mahameed case MLX5_PORT_CHANGE_SUBTYPE_GUID: 2640134e9349SSaeed Mahameed ibev->event = IB_EVENT_GID_CHANGE; 2641134e9349SSaeed Mahameed break; 2642134e9349SSaeed Mahameed 2643134e9349SSaeed Mahameed case MLX5_PORT_CHANGE_SUBTYPE_CLIENT_REREG: 2644134e9349SSaeed Mahameed ibev->event = IB_EVENT_CLIENT_REREGISTER; 2645134e9349SSaeed Mahameed break; 2646134e9349SSaeed Mahameed default: 2647134e9349SSaeed Mahameed return -EINVAL; 2648134e9349SSaeed Mahameed } 2649134e9349SSaeed Mahameed 2650134e9349SSaeed Mahameed return 0; 2651134e9349SSaeed Mahameed } 2652134e9349SSaeed Mahameed 2653d69a24e0SDaniel Jurgens static void mlx5_ib_handle_event(struct work_struct *_work) 2654e126ba97SEli Cohen { 2655d69a24e0SDaniel Jurgens struct mlx5_ib_event_work *work = 2656d69a24e0SDaniel Jurgens container_of(_work, struct mlx5_ib_event_work, work); 2657d69a24e0SDaniel Jurgens struct mlx5_ib_dev *ibdev; 2658e126ba97SEli Cohen struct ib_event ibev; 2659dbaaff2aSEli Cohen bool fatal = false; 2660e126ba97SEli Cohen 2661df097a27SSaeed Mahameed if (work->is_slave) { 2662df097a27SSaeed Mahameed ibdev = mlx5_ib_get_ibdev_from_mpi(work->mpi); 2663d69a24e0SDaniel Jurgens if (!ibdev) 2664d69a24e0SDaniel Jurgens goto out; 2665d69a24e0SDaniel Jurgens } else { 2666df097a27SSaeed Mahameed ibdev = work->dev; 2667d69a24e0SDaniel Jurgens } 2668d69a24e0SDaniel Jurgens 2669d69a24e0SDaniel Jurgens switch (work->event) { 2670e126ba97SEli Cohen case MLX5_DEV_EVENT_SYS_ERROR: 2671e126ba97SEli Cohen ibev.event = IB_EVENT_DEVICE_FATAL; 267289ea94a7SMaor Gottlieb mlx5_ib_handle_internal_error(ibdev); 2673134e9349SSaeed Mahameed ibev.element.port_num = (u8)(unsigned long)work->param; 2674dbaaff2aSEli Cohen fatal = true; 2675e126ba97SEli Cohen break; 2676134e9349SSaeed Mahameed case MLX5_EVENT_TYPE_PORT_CHANGE: 2677134e9349SSaeed Mahameed if (handle_port_change(ibdev, work->param, &ibev)) 2678d69a24e0SDaniel Jurgens goto out; 2679e126ba97SEli Cohen break; 268009e574faSSaeed Mahameed case MLX5_EVENT_TYPE_GENERAL_EVENT: 268109e574faSSaeed Mahameed handle_general_event(ibdev, work->param, &ibev); 2682df561f66SGustavo A. R. Silva fallthrough; 2683bdc37924SSaeed Mahameed default: 268403404e8aSMaor Gottlieb goto out; 2685e126ba97SEli Cohen } 2686e126ba97SEli Cohen 2687e126ba97SEli Cohen ibev.device = &ibdev->ib_dev; 2688e126ba97SEli Cohen 2689134e9349SSaeed Mahameed if (!rdma_is_port_valid(&ibdev->ib_dev, ibev.element.port_num)) { 2690134e9349SSaeed Mahameed mlx5_ib_warn(ibdev, "warning: event on port %d\n", ibev.element.port_num); 269103404e8aSMaor Gottlieb goto out; 2692a0c84c32SEli Cohen } 2693a0c84c32SEli Cohen 2694e126ba97SEli Cohen if (ibdev->ib_active) 2695e126ba97SEli Cohen ib_dispatch_event(&ibev); 2696dbaaff2aSEli Cohen 2697dbaaff2aSEli Cohen if (fatal) 2698dbaaff2aSEli Cohen ibdev->ib_active = false; 269903404e8aSMaor Gottlieb out: 2700d69a24e0SDaniel Jurgens kfree(work); 2701d69a24e0SDaniel Jurgens } 2702d69a24e0SDaniel Jurgens 2703df097a27SSaeed Mahameed static int mlx5_ib_event(struct notifier_block *nb, 2704df097a27SSaeed Mahameed unsigned long event, void *param) 2705d69a24e0SDaniel Jurgens { 2706d69a24e0SDaniel Jurgens struct mlx5_ib_event_work *work; 2707d69a24e0SDaniel Jurgens 2708d69a24e0SDaniel Jurgens work = kmalloc(sizeof(*work), GFP_ATOMIC); 270910bea9c8SLeon Romanovsky if (!work) 2710df097a27SSaeed Mahameed return NOTIFY_DONE; 271110bea9c8SLeon Romanovsky 2712d69a24e0SDaniel Jurgens INIT_WORK(&work->work, mlx5_ib_handle_event); 2713df097a27SSaeed Mahameed work->dev = container_of(nb, struct mlx5_ib_dev, mdev_events); 2714df097a27SSaeed Mahameed work->is_slave = false; 2715d69a24e0SDaniel Jurgens work->param = param; 2716d69a24e0SDaniel Jurgens work->event = event; 2717d69a24e0SDaniel Jurgens 2718d69a24e0SDaniel Jurgens queue_work(mlx5_ib_event_wq, &work->work); 2719df097a27SSaeed Mahameed 2720df097a27SSaeed Mahameed return NOTIFY_OK; 2721df097a27SSaeed Mahameed } 2722df097a27SSaeed Mahameed 2723df097a27SSaeed Mahameed static int mlx5_ib_event_slave_port(struct notifier_block *nb, 2724df097a27SSaeed Mahameed unsigned long event, void *param) 2725df097a27SSaeed Mahameed { 2726df097a27SSaeed Mahameed struct mlx5_ib_event_work *work; 2727df097a27SSaeed Mahameed 2728df097a27SSaeed Mahameed work = kmalloc(sizeof(*work), GFP_ATOMIC); 2729df097a27SSaeed Mahameed if (!work) 2730df097a27SSaeed Mahameed return NOTIFY_DONE; 2731df097a27SSaeed Mahameed 2732df097a27SSaeed Mahameed INIT_WORK(&work->work, mlx5_ib_handle_event); 2733df097a27SSaeed Mahameed work->mpi = container_of(nb, struct mlx5_ib_multiport_info, mdev_events); 2734df097a27SSaeed Mahameed work->is_slave = true; 2735df097a27SSaeed Mahameed work->param = param; 2736df097a27SSaeed Mahameed work->event = event; 2737df097a27SSaeed Mahameed queue_work(mlx5_ib_event_wq, &work->work); 2738df097a27SSaeed Mahameed 2739df097a27SSaeed Mahameed return NOTIFY_OK; 2740d69a24e0SDaniel Jurgens } 2741d69a24e0SDaniel Jurgens 2742c43f1112SMaor Gottlieb static int set_has_smi_cap(struct mlx5_ib_dev *dev) 2743c43f1112SMaor Gottlieb { 2744c43f1112SMaor Gottlieb struct mlx5_hca_vport_context vport_ctx; 2745c43f1112SMaor Gottlieb int err; 2746c43f1112SMaor Gottlieb int port; 2747c43f1112SMaor Gottlieb 27483ce60f44SParav Pandit for (port = 1; port <= ARRAY_SIZE(dev->port_caps); port++) { 27493ce60f44SParav Pandit dev->port_caps[port - 1].has_smi = false; 2750c43f1112SMaor Gottlieb if (MLX5_CAP_GEN(dev->mdev, port_type) == 2751c43f1112SMaor Gottlieb MLX5_CAP_PORT_TYPE_IB) { 2752c43f1112SMaor Gottlieb if (MLX5_CAP_GEN(dev->mdev, ib_virt)) { 2753c43f1112SMaor Gottlieb err = mlx5_query_hca_vport_context(dev->mdev, 0, 2754c43f1112SMaor Gottlieb port, 0, 2755c43f1112SMaor Gottlieb &vport_ctx); 2756c43f1112SMaor Gottlieb if (err) { 2757c43f1112SMaor Gottlieb mlx5_ib_err(dev, "query_hca_vport_context for port=%d failed %d\n", 2758c43f1112SMaor Gottlieb port, err); 2759c43f1112SMaor Gottlieb return err; 2760c43f1112SMaor Gottlieb } 27613ce60f44SParav Pandit dev->port_caps[port - 1].has_smi = 2762c43f1112SMaor Gottlieb vport_ctx.has_smi; 2763c43f1112SMaor Gottlieb } else { 27643ce60f44SParav Pandit dev->port_caps[port - 1].has_smi = true; 2765c43f1112SMaor Gottlieb } 2766c43f1112SMaor Gottlieb } 2767c43f1112SMaor Gottlieb } 2768c43f1112SMaor Gottlieb return 0; 2769c43f1112SMaor Gottlieb } 2770c43f1112SMaor Gottlieb 2771e126ba97SEli Cohen static void get_ext_port_caps(struct mlx5_ib_dev *dev) 2772e126ba97SEli Cohen { 277313179652SParav Pandit unsigned int port; 2774e126ba97SEli Cohen 277513179652SParav Pandit rdma_for_each_port (&dev->ib_dev, port) 2776e126ba97SEli Cohen mlx5_query_ext_port_caps(dev, port); 2777e126ba97SEli Cohen } 2778e126ba97SEli Cohen 27796e8484c5SMax Gurtovoy static u8 mlx5_get_umr_fence(u8 umr_fence_cap) 27806e8484c5SMax Gurtovoy { 27816e8484c5SMax Gurtovoy switch (umr_fence_cap) { 27826e8484c5SMax Gurtovoy case MLX5_CAP_UMR_FENCE_NONE: 27836e8484c5SMax Gurtovoy return MLX5_FENCE_MODE_NONE; 27846e8484c5SMax Gurtovoy case MLX5_CAP_UMR_FENCE_SMALL: 27856e8484c5SMax Gurtovoy return MLX5_FENCE_MODE_INITIATOR_SMALL; 27866e8484c5SMax Gurtovoy default: 27876e8484c5SMax Gurtovoy return MLX5_FENCE_MODE_STRONG_ORDERING; 27886e8484c5SMax Gurtovoy } 27896e8484c5SMax Gurtovoy } 27906e8484c5SMax Gurtovoy 27911e2b5a90SLeon Romanovsky static int mlx5_ib_dev_res_init(struct mlx5_ib_dev *dev) 2792e126ba97SEli Cohen { 27931e2b5a90SLeon Romanovsky struct mlx5_ib_resources *devr = &dev->devr; 2794e126ba97SEli Cohen struct ib_srq_init_attr attr; 279521a428a0SLeon Romanovsky struct ib_device *ibdev; 2796bcf4c1eaSMatan Barak struct ib_cq_init_attr cq_attr = {.cqe = 1}; 27977722f47eSHaggai Eran int port; 2798e126ba97SEli Cohen int ret = 0; 2799e126ba97SEli Cohen 280021a428a0SLeon Romanovsky ibdev = &dev->ib_dev; 2801e126ba97SEli Cohen 2802f4375443SLeon Romanovsky if (!MLX5_CAP_GEN(dev->mdev, xrc)) 2803f4375443SLeon Romanovsky return -EOPNOTSUPP; 2804f4375443SLeon Romanovsky 280521a428a0SLeon Romanovsky devr->p0 = rdma_zalloc_drv_obj(ibdev, ib_pd); 280621a428a0SLeon Romanovsky if (!devr->p0) 280721a428a0SLeon Romanovsky return -ENOMEM; 280821a428a0SLeon Romanovsky 280921a428a0SLeon Romanovsky devr->p0->device = ibdev; 2810e126ba97SEli Cohen devr->p0->uobject = NULL; 2811e126ba97SEli Cohen atomic_set(&devr->p0->usecnt, 0); 2812e126ba97SEli Cohen 2813ff23dfa1SShamir Rabinovitch ret = mlx5_ib_alloc_pd(devr->p0, NULL); 281421a428a0SLeon Romanovsky if (ret) 281521a428a0SLeon Romanovsky goto error0; 281621a428a0SLeon Romanovsky 2817e39afe3dSLeon Romanovsky devr->c0 = rdma_zalloc_drv_obj(ibdev, ib_cq); 2818e39afe3dSLeon Romanovsky if (!devr->c0) { 2819e39afe3dSLeon Romanovsky ret = -ENOMEM; 2820e126ba97SEli Cohen goto error1; 2821e126ba97SEli Cohen } 2822e39afe3dSLeon Romanovsky 2823e126ba97SEli Cohen devr->c0->device = &dev->ib_dev; 2824e126ba97SEli Cohen atomic_set(&devr->c0->usecnt, 0); 2825e126ba97SEli Cohen 2826e39afe3dSLeon Romanovsky ret = mlx5_ib_create_cq(devr->c0, &cq_attr, NULL); 2827e39afe3dSLeon Romanovsky if (ret) 2828e39afe3dSLeon Romanovsky goto err_create_cq; 2829e39afe3dSLeon Romanovsky 2830f4375443SLeon Romanovsky ret = mlx5_cmd_xrcd_alloc(dev->mdev, &devr->xrcdn0, 0); 2831f4375443SLeon Romanovsky if (ret) 2832e126ba97SEli Cohen goto error2; 2833e126ba97SEli Cohen 2834f4375443SLeon Romanovsky ret = mlx5_cmd_xrcd_alloc(dev->mdev, &devr->xrcdn1, 0); 2835f4375443SLeon Romanovsky if (ret) 2836e126ba97SEli Cohen goto error3; 2837e126ba97SEli Cohen 2838e126ba97SEli Cohen memset(&attr, 0, sizeof(attr)); 2839e126ba97SEli Cohen attr.attr.max_sge = 1; 2840e126ba97SEli Cohen attr.attr.max_wr = 1; 2841e126ba97SEli Cohen attr.srq_type = IB_SRQT_XRC; 28421a56ff6dSArtemy Kovalyov attr.ext.cq = devr->c0; 2843e126ba97SEli Cohen 284468e326deSLeon Romanovsky devr->s0 = rdma_zalloc_drv_obj(ibdev, ib_srq); 284568e326deSLeon Romanovsky if (!devr->s0) { 284668e326deSLeon Romanovsky ret = -ENOMEM; 2847e126ba97SEli Cohen goto error4; 2848e126ba97SEli Cohen } 284968e326deSLeon Romanovsky 2850e126ba97SEli Cohen devr->s0->device = &dev->ib_dev; 2851e126ba97SEli Cohen devr->s0->pd = devr->p0; 2852e126ba97SEli Cohen devr->s0->srq_type = IB_SRQT_XRC; 28531a56ff6dSArtemy Kovalyov devr->s0->ext.cq = devr->c0; 285468e326deSLeon Romanovsky ret = mlx5_ib_create_srq(devr->s0, &attr, NULL); 285568e326deSLeon Romanovsky if (ret) 285668e326deSLeon Romanovsky goto err_create; 285768e326deSLeon Romanovsky 28581a56ff6dSArtemy Kovalyov atomic_inc(&devr->s0->ext.cq->usecnt); 2859e126ba97SEli Cohen atomic_inc(&devr->p0->usecnt); 2860e126ba97SEli Cohen atomic_set(&devr->s0->usecnt, 0); 2861e126ba97SEli Cohen 28624aa17b28SHaggai Abramonvsky memset(&attr, 0, sizeof(attr)); 28634aa17b28SHaggai Abramonvsky attr.attr.max_sge = 1; 28644aa17b28SHaggai Abramonvsky attr.attr.max_wr = 1; 28654aa17b28SHaggai Abramonvsky attr.srq_type = IB_SRQT_BASIC; 286668e326deSLeon Romanovsky devr->s1 = rdma_zalloc_drv_obj(ibdev, ib_srq); 286768e326deSLeon Romanovsky if (!devr->s1) { 286868e326deSLeon Romanovsky ret = -ENOMEM; 28694aa17b28SHaggai Abramonvsky goto error5; 28704aa17b28SHaggai Abramonvsky } 287168e326deSLeon Romanovsky 28724aa17b28SHaggai Abramonvsky devr->s1->device = &dev->ib_dev; 28734aa17b28SHaggai Abramonvsky devr->s1->pd = devr->p0; 28744aa17b28SHaggai Abramonvsky devr->s1->srq_type = IB_SRQT_BASIC; 28751a56ff6dSArtemy Kovalyov devr->s1->ext.cq = devr->c0; 287668e326deSLeon Romanovsky 287768e326deSLeon Romanovsky ret = mlx5_ib_create_srq(devr->s1, &attr, NULL); 287868e326deSLeon Romanovsky if (ret) 287968e326deSLeon Romanovsky goto error6; 288068e326deSLeon Romanovsky 28814aa17b28SHaggai Abramonvsky atomic_inc(&devr->p0->usecnt); 28821a56ff6dSArtemy Kovalyov atomic_set(&devr->s1->usecnt, 0); 28834aa17b28SHaggai Abramonvsky 2884f8225e34SLeon Romanovsky for (port = 0; port < ARRAY_SIZE(devr->ports); ++port) 28857722f47eSHaggai Eran INIT_WORK(&devr->ports[port].pkey_change_work, 28867722f47eSHaggai Eran pkey_change_handler); 28877722f47eSHaggai Eran 2888e126ba97SEli Cohen return 0; 2889e126ba97SEli Cohen 289068e326deSLeon Romanovsky error6: 289168e326deSLeon Romanovsky kfree(devr->s1); 28924aa17b28SHaggai Abramonvsky error5: 2893c4367a26SShamir Rabinovitch mlx5_ib_destroy_srq(devr->s0, NULL); 289468e326deSLeon Romanovsky err_create: 289568e326deSLeon Romanovsky kfree(devr->s0); 2896e126ba97SEli Cohen error4: 2897f4375443SLeon Romanovsky mlx5_cmd_xrcd_dealloc(dev->mdev, devr->xrcdn1, 0); 2898e126ba97SEli Cohen error3: 2899f4375443SLeon Romanovsky mlx5_cmd_xrcd_dealloc(dev->mdev, devr->xrcdn0, 0); 2900e126ba97SEli Cohen error2: 2901c4367a26SShamir Rabinovitch mlx5_ib_destroy_cq(devr->c0, NULL); 2902e39afe3dSLeon Romanovsky err_create_cq: 2903e39afe3dSLeon Romanovsky kfree(devr->c0); 2904e126ba97SEli Cohen error1: 2905c4367a26SShamir Rabinovitch mlx5_ib_dealloc_pd(devr->p0, NULL); 2906e126ba97SEli Cohen error0: 290721a428a0SLeon Romanovsky kfree(devr->p0); 2908e126ba97SEli Cohen return ret; 2909e126ba97SEli Cohen } 2910e126ba97SEli Cohen 29111e2b5a90SLeon Romanovsky static void mlx5_ib_dev_res_cleanup(struct mlx5_ib_dev *dev) 2912e126ba97SEli Cohen { 29131e2b5a90SLeon Romanovsky struct mlx5_ib_resources *devr = &dev->devr; 29147722f47eSHaggai Eran int port; 29157722f47eSHaggai Eran 2916b0791dbfSLeon Romanovsky /* 2917b0791dbfSLeon Romanovsky * Make sure no change P_Key work items are still executing. 2918b0791dbfSLeon Romanovsky * 2919b0791dbfSLeon Romanovsky * At this stage, the mlx5_ib_event should be unregistered 2920b0791dbfSLeon Romanovsky * and it ensures that no new works are added. 2921b0791dbfSLeon Romanovsky */ 2922b0791dbfSLeon Romanovsky for (port = 0; port < ARRAY_SIZE(devr->ports); ++port) 2923b0791dbfSLeon Romanovsky cancel_work_sync(&devr->ports[port].pkey_change_work); 2924b0791dbfSLeon Romanovsky 2925c4367a26SShamir Rabinovitch mlx5_ib_destroy_srq(devr->s1, NULL); 292668e326deSLeon Romanovsky kfree(devr->s1); 2927c4367a26SShamir Rabinovitch mlx5_ib_destroy_srq(devr->s0, NULL); 292868e326deSLeon Romanovsky kfree(devr->s0); 2929f4375443SLeon Romanovsky mlx5_cmd_xrcd_dealloc(dev->mdev, devr->xrcdn1, 0); 2930f4375443SLeon Romanovsky mlx5_cmd_xrcd_dealloc(dev->mdev, devr->xrcdn0, 0); 2931c4367a26SShamir Rabinovitch mlx5_ib_destroy_cq(devr->c0, NULL); 2932e39afe3dSLeon Romanovsky kfree(devr->c0); 2933c4367a26SShamir Rabinovitch mlx5_ib_dealloc_pd(devr->p0, NULL); 293421a428a0SLeon Romanovsky kfree(devr->p0); 2935e126ba97SEli Cohen } 2936e126ba97SEli Cohen 2937b02289b3SArtemy Kovalyov static u32 get_core_cap_flags(struct ib_device *ibdev, 2938b02289b3SArtemy Kovalyov struct mlx5_hca_vport_context *rep) 2939e53505a8SAchiad Shochat { 2940e53505a8SAchiad Shochat struct mlx5_ib_dev *dev = to_mdev(ibdev); 2941e53505a8SAchiad Shochat enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, 1); 2942e53505a8SAchiad Shochat u8 l3_type_cap = MLX5_CAP_ROCE(dev->mdev, l3_type); 2943e53505a8SAchiad Shochat u8 roce_version_cap = MLX5_CAP_ROCE(dev->mdev, roce_version); 294485c7c014SDaniel Jurgens bool raw_support = !mlx5_core_mp_enabled(dev->mdev); 2945e53505a8SAchiad Shochat u32 ret = 0; 2946e53505a8SAchiad Shochat 2947b02289b3SArtemy Kovalyov if (rep->grh_required) 2948b02289b3SArtemy Kovalyov ret |= RDMA_CORE_CAP_IB_GRH_REQUIRED; 2949b02289b3SArtemy Kovalyov 2950e53505a8SAchiad Shochat if (ll == IB_LINK_LAYER_INFINIBAND) 2951b02289b3SArtemy Kovalyov return ret | RDMA_CORE_PORT_IBA_IB; 2952e53505a8SAchiad Shochat 295385c7c014SDaniel Jurgens if (raw_support) 2954b02289b3SArtemy Kovalyov ret |= RDMA_CORE_PORT_RAW_PACKET; 295572cd5717SOr Gerlitz 2956e53505a8SAchiad Shochat if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV4_CAP)) 295772cd5717SOr Gerlitz return ret; 2958e53505a8SAchiad Shochat 2959e53505a8SAchiad Shochat if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV6_CAP)) 296072cd5717SOr Gerlitz return ret; 2961e53505a8SAchiad Shochat 2962e53505a8SAchiad Shochat if (roce_version_cap & MLX5_ROCE_VERSION_1_CAP) 2963e53505a8SAchiad Shochat ret |= RDMA_CORE_PORT_IBA_ROCE; 2964e53505a8SAchiad Shochat 2965e53505a8SAchiad Shochat if (roce_version_cap & MLX5_ROCE_VERSION_2_CAP) 2966e53505a8SAchiad Shochat ret |= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP; 2967e53505a8SAchiad Shochat 2968e53505a8SAchiad Shochat return ret; 2969e53505a8SAchiad Shochat } 2970e53505a8SAchiad Shochat 29711fb7f897SMark Bloch static int mlx5_port_immutable(struct ib_device *ibdev, u32 port_num, 29727738613eSIra Weiny struct ib_port_immutable *immutable) 29737738613eSIra Weiny { 29747738613eSIra Weiny struct ib_port_attr attr; 2975ca5b91d6SOr Gerlitz struct mlx5_ib_dev *dev = to_mdev(ibdev); 2976ca5b91d6SOr Gerlitz enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, port_num); 2977b02289b3SArtemy Kovalyov struct mlx5_hca_vport_context rep = {0}; 29787738613eSIra Weiny int err; 29797738613eSIra Weiny 2980c4550c63SOr Gerlitz err = ib_query_port(ibdev, port_num, &attr); 29817738613eSIra Weiny if (err) 29827738613eSIra Weiny return err; 29837738613eSIra Weiny 2984b02289b3SArtemy Kovalyov if (ll == IB_LINK_LAYER_INFINIBAND) { 2985b02289b3SArtemy Kovalyov err = mlx5_query_hca_vport_context(dev->mdev, 0, port_num, 0, 2986b02289b3SArtemy Kovalyov &rep); 2987b02289b3SArtemy Kovalyov if (err) 2988b02289b3SArtemy Kovalyov return err; 2989b02289b3SArtemy Kovalyov } 2990b02289b3SArtemy Kovalyov 29917738613eSIra Weiny immutable->pkey_tbl_len = attr.pkey_tbl_len; 29927738613eSIra Weiny immutable->gid_tbl_len = attr.gid_tbl_len; 2993b02289b3SArtemy Kovalyov immutable->core_cap_flags = get_core_cap_flags(ibdev, &rep); 2994337877a4SIra Weiny immutable->max_mad_size = IB_MGMT_MAD_SIZE; 29957738613eSIra Weiny 29967738613eSIra Weiny return 0; 29977738613eSIra Weiny } 29987738613eSIra Weiny 29991fb7f897SMark Bloch static int mlx5_port_rep_immutable(struct ib_device *ibdev, u32 port_num, 30008e6efa3aSMark Bloch struct ib_port_immutable *immutable) 30018e6efa3aSMark Bloch { 30028e6efa3aSMark Bloch struct ib_port_attr attr; 30038e6efa3aSMark Bloch int err; 30048e6efa3aSMark Bloch 30058e6efa3aSMark Bloch immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET; 30068e6efa3aSMark Bloch 30078e6efa3aSMark Bloch err = ib_query_port(ibdev, port_num, &attr); 30088e6efa3aSMark Bloch if (err) 30098e6efa3aSMark Bloch return err; 30108e6efa3aSMark Bloch 30118e6efa3aSMark Bloch immutable->pkey_tbl_len = attr.pkey_tbl_len; 30128e6efa3aSMark Bloch immutable->gid_tbl_len = attr.gid_tbl_len; 30138e6efa3aSMark Bloch immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET; 30148e6efa3aSMark Bloch 30158e6efa3aSMark Bloch return 0; 30168e6efa3aSMark Bloch } 30178e6efa3aSMark Bloch 30189abb0d1bSLeon Romanovsky static void get_dev_fw_str(struct ib_device *ibdev, char *str) 3019c7342823SIra Weiny { 3020c7342823SIra Weiny struct mlx5_ib_dev *dev = 3021c7342823SIra Weiny container_of(ibdev, struct mlx5_ib_dev, ib_dev); 30229abb0d1bSLeon Romanovsky snprintf(str, IB_FW_VERSION_NAME_MAX, "%d.%d.%04d", 30239abb0d1bSLeon Romanovsky fw_rev_maj(dev->mdev), fw_rev_min(dev->mdev), 30249abb0d1bSLeon Romanovsky fw_rev_sub(dev->mdev)); 3025c7342823SIra Weiny } 3026c7342823SIra Weiny 302745f95acdSOr Gerlitz static int mlx5_eth_lag_init(struct mlx5_ib_dev *dev) 30289ef9c640SAviv Heller { 30299ef9c640SAviv Heller struct mlx5_core_dev *mdev = dev->mdev; 30309ef9c640SAviv Heller struct mlx5_flow_namespace *ns = mlx5_get_flow_namespace(mdev, 30319ef9c640SAviv Heller MLX5_FLOW_NAMESPACE_LAG); 30329ef9c640SAviv Heller struct mlx5_flow_table *ft; 30339ef9c640SAviv Heller int err; 30349ef9c640SAviv Heller 30357c34ec19SAviv Heller if (!ns || !mlx5_lag_is_roce(mdev)) 30369ef9c640SAviv Heller return 0; 30379ef9c640SAviv Heller 30389ef9c640SAviv Heller err = mlx5_cmd_create_vport_lag(mdev); 30399ef9c640SAviv Heller if (err) 30409ef9c640SAviv Heller return err; 30419ef9c640SAviv Heller 30429ef9c640SAviv Heller ft = mlx5_create_lag_demux_flow_table(ns, 0, 0); 30439ef9c640SAviv Heller if (IS_ERR(ft)) { 30449ef9c640SAviv Heller err = PTR_ERR(ft); 30459ef9c640SAviv Heller goto err_destroy_vport_lag; 30469ef9c640SAviv Heller } 30479ef9c640SAviv Heller 30489a4ca38dSMark Bloch dev->flow_db->lag_demux_ft = ft; 30497c34ec19SAviv Heller dev->lag_active = true; 30509ef9c640SAviv Heller return 0; 30519ef9c640SAviv Heller 30529ef9c640SAviv Heller err_destroy_vport_lag: 30539ef9c640SAviv Heller mlx5_cmd_destroy_vport_lag(mdev); 30549ef9c640SAviv Heller return err; 30559ef9c640SAviv Heller } 30569ef9c640SAviv Heller 305745f95acdSOr Gerlitz static void mlx5_eth_lag_cleanup(struct mlx5_ib_dev *dev) 30589ef9c640SAviv Heller { 30599ef9c640SAviv Heller struct mlx5_core_dev *mdev = dev->mdev; 30609ef9c640SAviv Heller 30617c34ec19SAviv Heller if (dev->lag_active) { 30627c34ec19SAviv Heller dev->lag_active = false; 30637c34ec19SAviv Heller 30649a4ca38dSMark Bloch mlx5_destroy_flow_table(dev->flow_db->lag_demux_ft); 30659a4ca38dSMark Bloch dev->flow_db->lag_demux_ft = NULL; 30669ef9c640SAviv Heller 30679ef9c640SAviv Heller mlx5_cmd_destroy_vport_lag(mdev); 30689ef9c640SAviv Heller } 30699ef9c640SAviv Heller } 30709ef9c640SAviv Heller 30711fb7f897SMark Bloch static int mlx5_add_netdev_notifier(struct mlx5_ib_dev *dev, u32 port_num) 3072fc24fc5eSAchiad Shochat { 3073e53505a8SAchiad Shochat int err; 3074e53505a8SAchiad Shochat 307595579e78SMark Bloch dev->port[port_num].roce.nb.notifier_call = mlx5_netdev_event; 3076de641d74SParav Pandit err = register_netdevice_notifier(&dev->port[port_num].roce.nb); 30775ec8c83eSAviv Heller if (err) { 307895579e78SMark Bloch dev->port[port_num].roce.nb.notifier_call = NULL; 3079e53505a8SAchiad Shochat return err; 30805ec8c83eSAviv Heller } 3081e53505a8SAchiad Shochat 3082d012f5d6SOr Gerlitz return 0; 3083d012f5d6SOr Gerlitz } 3084d012f5d6SOr Gerlitz 30851fb7f897SMark Bloch static void mlx5_remove_netdev_notifier(struct mlx5_ib_dev *dev, u32 port_num) 3086e126ba97SEli Cohen { 308795579e78SMark Bloch if (dev->port[port_num].roce.nb.notifier_call) { 3088de641d74SParav Pandit unregister_netdevice_notifier(&dev->port[port_num].roce.nb); 308995579e78SMark Bloch dev->port[port_num].roce.nb.notifier_call = NULL; 3090e126ba97SEli Cohen } 3091e126ba97SEli Cohen } 3092e126ba97SEli Cohen 3093e3f1ed1fSLeon Romanovsky static int mlx5_enable_eth(struct mlx5_ib_dev *dev) 3094e126ba97SEli Cohen { 3095e126ba97SEli Cohen int err; 3096e126ba97SEli Cohen 3097e53505a8SAchiad Shochat err = mlx5_nic_vport_enable_roce(dev->mdev); 3098e53505a8SAchiad Shochat if (err) 30998e6efa3aSMark Bloch return err; 3100e53505a8SAchiad Shochat 310145f95acdSOr Gerlitz err = mlx5_eth_lag_init(dev); 31029ef9c640SAviv Heller if (err) 31039ef9c640SAviv Heller goto err_disable_roce; 31049ef9c640SAviv Heller 3105e53505a8SAchiad Shochat return 0; 3106e53505a8SAchiad Shochat 31079ef9c640SAviv Heller err_disable_roce: 31089ef9c640SAviv Heller mlx5_nic_vport_disable_roce(dev->mdev); 31099ef9c640SAviv Heller 3110e53505a8SAchiad Shochat return err; 3111fc24fc5eSAchiad Shochat } 3112fc24fc5eSAchiad Shochat 311345f95acdSOr Gerlitz static void mlx5_disable_eth(struct mlx5_ib_dev *dev) 3114fc24fc5eSAchiad Shochat { 311545f95acdSOr Gerlitz mlx5_eth_lag_cleanup(dev); 3116e53505a8SAchiad Shochat mlx5_nic_vport_disable_roce(dev->mdev); 3117fc24fc5eSAchiad Shochat } 3118fc24fc5eSAchiad Shochat 31191fb7f897SMark Bloch static int mlx5_ib_rn_get_params(struct ib_device *device, u32 port_num, 3120693dfd5aSErez Shitrit enum rdma_netdev_t type, 3121f6a8a19bSDenis Drozdov struct rdma_netdev_alloc_params *params) 3122693dfd5aSErez Shitrit { 3123693dfd5aSErez Shitrit if (type != RDMA_NETDEV_IPOIB) 3124f6a8a19bSDenis Drozdov return -EOPNOTSUPP; 3125693dfd5aSErez Shitrit 3126f6a8a19bSDenis Drozdov return mlx5_rdma_rn_get_params(to_mdev(device)->mdev, device, params); 3127693dfd5aSErez Shitrit } 3128693dfd5aSErez Shitrit 3129fe248c3aSMaor Gottlieb static ssize_t delay_drop_timeout_read(struct file *filp, char __user *buf, 3130fe248c3aSMaor Gottlieb size_t count, loff_t *pos) 3131fe248c3aSMaor Gottlieb { 3132fe248c3aSMaor Gottlieb struct mlx5_ib_delay_drop *delay_drop = filp->private_data; 3133fe248c3aSMaor Gottlieb char lbuf[20]; 3134fe248c3aSMaor Gottlieb int len; 3135fe248c3aSMaor Gottlieb 3136fe248c3aSMaor Gottlieb len = snprintf(lbuf, sizeof(lbuf), "%u\n", delay_drop->timeout); 3137fe248c3aSMaor Gottlieb return simple_read_from_buffer(buf, count, pos, lbuf, len); 3138fe248c3aSMaor Gottlieb } 3139fe248c3aSMaor Gottlieb 3140fe248c3aSMaor Gottlieb static ssize_t delay_drop_timeout_write(struct file *filp, const char __user *buf, 3141fe248c3aSMaor Gottlieb size_t count, loff_t *pos) 3142fe248c3aSMaor Gottlieb { 3143fe248c3aSMaor Gottlieb struct mlx5_ib_delay_drop *delay_drop = filp->private_data; 3144fe248c3aSMaor Gottlieb u32 timeout; 3145fe248c3aSMaor Gottlieb u32 var; 3146fe248c3aSMaor Gottlieb 3147fe248c3aSMaor Gottlieb if (kstrtouint_from_user(buf, count, 0, &var)) 3148fe248c3aSMaor Gottlieb return -EFAULT; 3149fe248c3aSMaor Gottlieb 3150fe248c3aSMaor Gottlieb timeout = min_t(u32, roundup(var, 100), MLX5_MAX_DELAY_DROP_TIMEOUT_MS * 3151fe248c3aSMaor Gottlieb 1000); 3152fe248c3aSMaor Gottlieb if (timeout != var) 3153fe248c3aSMaor Gottlieb mlx5_ib_dbg(delay_drop->dev, "Round delay drop timeout to %u usec\n", 3154fe248c3aSMaor Gottlieb timeout); 3155fe248c3aSMaor Gottlieb 3156fe248c3aSMaor Gottlieb delay_drop->timeout = timeout; 3157fe248c3aSMaor Gottlieb 3158fe248c3aSMaor Gottlieb return count; 3159fe248c3aSMaor Gottlieb } 3160fe248c3aSMaor Gottlieb 3161fe248c3aSMaor Gottlieb static const struct file_operations fops_delay_drop_timeout = { 3162fe248c3aSMaor Gottlieb .owner = THIS_MODULE, 3163fe248c3aSMaor Gottlieb .open = simple_open, 3164fe248c3aSMaor Gottlieb .write = delay_drop_timeout_write, 3165fe248c3aSMaor Gottlieb .read = delay_drop_timeout_read, 3166fe248c3aSMaor Gottlieb }; 3167fe248c3aSMaor Gottlieb 316832f69e4bSDaniel Jurgens static void mlx5_ib_unbind_slave_port(struct mlx5_ib_dev *ibdev, 316932f69e4bSDaniel Jurgens struct mlx5_ib_multiport_info *mpi) 317032f69e4bSDaniel Jurgens { 31711fb7f897SMark Bloch u32 port_num = mlx5_core_native_port_num(mpi->mdev) - 1; 317232f69e4bSDaniel Jurgens struct mlx5_ib_port *port = &ibdev->port[port_num]; 317332f69e4bSDaniel Jurgens int comps; 317432f69e4bSDaniel Jurgens int err; 317532f69e4bSDaniel Jurgens int i; 317632f69e4bSDaniel Jurgens 31779dc4cfffSLeon Romanovsky lockdep_assert_held(&mlx5_ib_multiport_mutex); 31789dc4cfffSLeon Romanovsky 3179a9e546e7SParav Pandit mlx5_ib_cleanup_cong_debugfs(ibdev, port_num); 3180a9e546e7SParav Pandit 318132f69e4bSDaniel Jurgens spin_lock(&port->mp.mpi_lock); 318232f69e4bSDaniel Jurgens if (!mpi->ibdev) { 318332f69e4bSDaniel Jurgens spin_unlock(&port->mp.mpi_lock); 318432f69e4bSDaniel Jurgens return; 318532f69e4bSDaniel Jurgens } 3186df097a27SSaeed Mahameed 318732f69e4bSDaniel Jurgens mpi->ibdev = NULL; 318832f69e4bSDaniel Jurgens 318932f69e4bSDaniel Jurgens spin_unlock(&port->mp.mpi_lock); 319023eaf3b5SLeon Romanovsky if (mpi->mdev_events.notifier_call) 319123eaf3b5SLeon Romanovsky mlx5_notifier_unregister(mpi->mdev, &mpi->mdev_events); 319223eaf3b5SLeon Romanovsky mpi->mdev_events.notifier_call = NULL; 319332f69e4bSDaniel Jurgens mlx5_remove_netdev_notifier(ibdev, port_num); 319432f69e4bSDaniel Jurgens spin_lock(&port->mp.mpi_lock); 319532f69e4bSDaniel Jurgens 319632f69e4bSDaniel Jurgens comps = mpi->mdev_refcnt; 319732f69e4bSDaniel Jurgens if (comps) { 319832f69e4bSDaniel Jurgens mpi->unaffiliate = true; 319932f69e4bSDaniel Jurgens init_completion(&mpi->unref_comp); 320032f69e4bSDaniel Jurgens spin_unlock(&port->mp.mpi_lock); 320132f69e4bSDaniel Jurgens 320232f69e4bSDaniel Jurgens for (i = 0; i < comps; i++) 320332f69e4bSDaniel Jurgens wait_for_completion(&mpi->unref_comp); 320432f69e4bSDaniel Jurgens 320532f69e4bSDaniel Jurgens spin_lock(&port->mp.mpi_lock); 320632f69e4bSDaniel Jurgens mpi->unaffiliate = false; 320732f69e4bSDaniel Jurgens } 320832f69e4bSDaniel Jurgens 320932f69e4bSDaniel Jurgens port->mp.mpi = NULL; 321032f69e4bSDaniel Jurgens 321132f69e4bSDaniel Jurgens spin_unlock(&port->mp.mpi_lock); 321232f69e4bSDaniel Jurgens 321332f69e4bSDaniel Jurgens err = mlx5_nic_vport_unaffiliate_multiport(mpi->mdev); 321432f69e4bSDaniel Jurgens 32151fb7f897SMark Bloch mlx5_ib_dbg(ibdev, "unaffiliated port %u\n", port_num + 1); 321632f69e4bSDaniel Jurgens /* Log an error, still needed to cleanup the pointers and add 321732f69e4bSDaniel Jurgens * it back to the list. 321832f69e4bSDaniel Jurgens */ 321932f69e4bSDaniel Jurgens if (err) 322032f69e4bSDaniel Jurgens mlx5_ib_err(ibdev, "Failed to unaffiliate port %u\n", 322132f69e4bSDaniel Jurgens port_num + 1); 322232f69e4bSDaniel Jurgens 322395579e78SMark Bloch ibdev->port[port_num].roce.last_port_state = IB_PORT_DOWN; 322432f69e4bSDaniel Jurgens } 322532f69e4bSDaniel Jurgens 322632f69e4bSDaniel Jurgens static bool mlx5_ib_bind_slave_port(struct mlx5_ib_dev *ibdev, 322732f69e4bSDaniel Jurgens struct mlx5_ib_multiport_info *mpi) 322832f69e4bSDaniel Jurgens { 32291fb7f897SMark Bloch u32 port_num = mlx5_core_native_port_num(mpi->mdev) - 1; 323032f69e4bSDaniel Jurgens int err; 323132f69e4bSDaniel Jurgens 32329dc4cfffSLeon Romanovsky lockdep_assert_held(&mlx5_ib_multiport_mutex); 32339dc4cfffSLeon Romanovsky 323432f69e4bSDaniel Jurgens spin_lock(&ibdev->port[port_num].mp.mpi_lock); 323532f69e4bSDaniel Jurgens if (ibdev->port[port_num].mp.mpi) { 32361fb7f897SMark Bloch mlx5_ib_dbg(ibdev, "port %u already affiliated.\n", 323732f69e4bSDaniel Jurgens port_num + 1); 323832f69e4bSDaniel Jurgens spin_unlock(&ibdev->port[port_num].mp.mpi_lock); 323932f69e4bSDaniel Jurgens return false; 324032f69e4bSDaniel Jurgens } 324132f69e4bSDaniel Jurgens 324232f69e4bSDaniel Jurgens ibdev->port[port_num].mp.mpi = mpi; 324332f69e4bSDaniel Jurgens mpi->ibdev = ibdev; 3244df097a27SSaeed Mahameed mpi->mdev_events.notifier_call = NULL; 324532f69e4bSDaniel Jurgens spin_unlock(&ibdev->port[port_num].mp.mpi_lock); 324632f69e4bSDaniel Jurgens 324732f69e4bSDaniel Jurgens err = mlx5_nic_vport_affiliate_multiport(ibdev->mdev, mpi->mdev); 324832f69e4bSDaniel Jurgens if (err) 324932f69e4bSDaniel Jurgens goto unbind; 325032f69e4bSDaniel Jurgens 325132f69e4bSDaniel Jurgens err = mlx5_add_netdev_notifier(ibdev, port_num); 325232f69e4bSDaniel Jurgens if (err) { 325332f69e4bSDaniel Jurgens mlx5_ib_err(ibdev, "failed adding netdev notifier for port %u\n", 325432f69e4bSDaniel Jurgens port_num + 1); 325532f69e4bSDaniel Jurgens goto unbind; 325632f69e4bSDaniel Jurgens } 325732f69e4bSDaniel Jurgens 3258df097a27SSaeed Mahameed mpi->mdev_events.notifier_call = mlx5_ib_event_slave_port; 3259df097a27SSaeed Mahameed mlx5_notifier_register(mpi->mdev, &mpi->mdev_events); 3260df097a27SSaeed Mahameed 326173eb8f03SGreg Kroah-Hartman mlx5_ib_init_cong_debugfs(ibdev, port_num); 3262a9e546e7SParav Pandit 326332f69e4bSDaniel Jurgens return true; 326432f69e4bSDaniel Jurgens 326532f69e4bSDaniel Jurgens unbind: 326632f69e4bSDaniel Jurgens mlx5_ib_unbind_slave_port(ibdev, mpi); 326732f69e4bSDaniel Jurgens return false; 326832f69e4bSDaniel Jurgens } 326932f69e4bSDaniel Jurgens 327032f69e4bSDaniel Jurgens static int mlx5_ib_init_multiport_master(struct mlx5_ib_dev *dev) 327132f69e4bSDaniel Jurgens { 32721fb7f897SMark Bloch u32 port_num = mlx5_core_native_port_num(dev->mdev) - 1; 327332f69e4bSDaniel Jurgens enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev, 327432f69e4bSDaniel Jurgens port_num + 1); 327532f69e4bSDaniel Jurgens struct mlx5_ib_multiport_info *mpi; 327632f69e4bSDaniel Jurgens int err; 32771fb7f897SMark Bloch u32 i; 327832f69e4bSDaniel Jurgens 327932f69e4bSDaniel Jurgens if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET) 328032f69e4bSDaniel Jurgens return 0; 328132f69e4bSDaniel Jurgens 328232f69e4bSDaniel Jurgens err = mlx5_query_nic_vport_system_image_guid(dev->mdev, 328332f69e4bSDaniel Jurgens &dev->sys_image_guid); 328432f69e4bSDaniel Jurgens if (err) 328532f69e4bSDaniel Jurgens return err; 328632f69e4bSDaniel Jurgens 328732f69e4bSDaniel Jurgens err = mlx5_nic_vport_enable_roce(dev->mdev); 328832f69e4bSDaniel Jurgens if (err) 328932f69e4bSDaniel Jurgens return err; 329032f69e4bSDaniel Jurgens 329132f69e4bSDaniel Jurgens mutex_lock(&mlx5_ib_multiport_mutex); 329232f69e4bSDaniel Jurgens for (i = 0; i < dev->num_ports; i++) { 329332f69e4bSDaniel Jurgens bool bound = false; 329432f69e4bSDaniel Jurgens 329532f69e4bSDaniel Jurgens /* build a stub multiport info struct for the native port. */ 329632f69e4bSDaniel Jurgens if (i == port_num) { 329732f69e4bSDaniel Jurgens mpi = kzalloc(sizeof(*mpi), GFP_KERNEL); 329832f69e4bSDaniel Jurgens if (!mpi) { 329932f69e4bSDaniel Jurgens mutex_unlock(&mlx5_ib_multiport_mutex); 330032f69e4bSDaniel Jurgens mlx5_nic_vport_disable_roce(dev->mdev); 330132f69e4bSDaniel Jurgens return -ENOMEM; 330232f69e4bSDaniel Jurgens } 330332f69e4bSDaniel Jurgens 330432f69e4bSDaniel Jurgens mpi->is_master = true; 330532f69e4bSDaniel Jurgens mpi->mdev = dev->mdev; 330632f69e4bSDaniel Jurgens mpi->sys_image_guid = dev->sys_image_guid; 330732f69e4bSDaniel Jurgens dev->port[i].mp.mpi = mpi; 330832f69e4bSDaniel Jurgens mpi->ibdev = dev; 330932f69e4bSDaniel Jurgens mpi = NULL; 331032f69e4bSDaniel Jurgens continue; 331132f69e4bSDaniel Jurgens } 331232f69e4bSDaniel Jurgens 331332f69e4bSDaniel Jurgens list_for_each_entry(mpi, &mlx5_ib_unaffiliated_port_list, 331432f69e4bSDaniel Jurgens list) { 331532f69e4bSDaniel Jurgens if (dev->sys_image_guid == mpi->sys_image_guid && 331632f69e4bSDaniel Jurgens (mlx5_core_native_port_num(mpi->mdev) - 1) == i) { 331732f69e4bSDaniel Jurgens bound = mlx5_ib_bind_slave_port(dev, mpi); 331832f69e4bSDaniel Jurgens } 331932f69e4bSDaniel Jurgens 332032f69e4bSDaniel Jurgens if (bound) { 3321c42260f1SVu Pham dev_dbg(mpi->mdev->device, 3322c42260f1SVu Pham "removing port from unaffiliated list.\n"); 332332f69e4bSDaniel Jurgens mlx5_ib_dbg(dev, "port %d bound\n", i + 1); 332432f69e4bSDaniel Jurgens list_del(&mpi->list); 332532f69e4bSDaniel Jurgens break; 332632f69e4bSDaniel Jurgens } 332732f69e4bSDaniel Jurgens } 33287416790eSParav Pandit if (!bound) 332932f69e4bSDaniel Jurgens mlx5_ib_dbg(dev, "no free port found for port %d\n", 333032f69e4bSDaniel Jurgens i + 1); 333132f69e4bSDaniel Jurgens } 333232f69e4bSDaniel Jurgens 333332f69e4bSDaniel Jurgens list_add_tail(&dev->ib_dev_list, &mlx5_ib_dev_list); 333432f69e4bSDaniel Jurgens mutex_unlock(&mlx5_ib_multiport_mutex); 333532f69e4bSDaniel Jurgens return err; 333632f69e4bSDaniel Jurgens } 333732f69e4bSDaniel Jurgens 333832f69e4bSDaniel Jurgens static void mlx5_ib_cleanup_multiport_master(struct mlx5_ib_dev *dev) 333932f69e4bSDaniel Jurgens { 33401fb7f897SMark Bloch u32 port_num = mlx5_core_native_port_num(dev->mdev) - 1; 334132f69e4bSDaniel Jurgens enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev, 334232f69e4bSDaniel Jurgens port_num + 1); 33431fb7f897SMark Bloch u32 i; 334432f69e4bSDaniel Jurgens 334532f69e4bSDaniel Jurgens if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET) 334632f69e4bSDaniel Jurgens return; 334732f69e4bSDaniel Jurgens 334832f69e4bSDaniel Jurgens mutex_lock(&mlx5_ib_multiport_mutex); 334932f69e4bSDaniel Jurgens for (i = 0; i < dev->num_ports; i++) { 335032f69e4bSDaniel Jurgens if (dev->port[i].mp.mpi) { 335132f69e4bSDaniel Jurgens /* Destroy the native port stub */ 335232f69e4bSDaniel Jurgens if (i == port_num) { 335332f69e4bSDaniel Jurgens kfree(dev->port[i].mp.mpi); 335432f69e4bSDaniel Jurgens dev->port[i].mp.mpi = NULL; 335532f69e4bSDaniel Jurgens } else { 33561fb7f897SMark Bloch mlx5_ib_dbg(dev, "unbinding port_num: %u\n", 33571fb7f897SMark Bloch i + 1); 33587ce6095eSLeon Romanovsky list_add_tail(&dev->port[i].mp.mpi->list, 33597ce6095eSLeon Romanovsky &mlx5_ib_unaffiliated_port_list); 33604a754d76SLeon Romanovsky mlx5_ib_unbind_slave_port(dev, 33614a754d76SLeon Romanovsky dev->port[i].mp.mpi); 336232f69e4bSDaniel Jurgens } 336332f69e4bSDaniel Jurgens } 336432f69e4bSDaniel Jurgens } 336532f69e4bSDaniel Jurgens 336632f69e4bSDaniel Jurgens mlx5_ib_dbg(dev, "removing from devlist\n"); 336732f69e4bSDaniel Jurgens list_del(&dev->ib_dev_list); 336832f69e4bSDaniel Jurgens mutex_unlock(&mlx5_ib_multiport_mutex); 336932f69e4bSDaniel Jurgens 337032f69e4bSDaniel Jurgens mlx5_nic_vport_disable_roce(dev->mdev); 337132f69e4bSDaniel Jurgens } 337232f69e4bSDaniel Jurgens 3373342ee59dSYishai Hadas static int mmap_obj_cleanup(struct ib_uobject *uobject, 33747be76befSYishai Hadas enum rdma_remove_reason why, 33757be76befSYishai Hadas struct uverbs_attr_bundle *attrs) 33767be76befSYishai Hadas { 33777be76befSYishai Hadas struct mlx5_user_mmap_entry *obj = uobject->object; 33787be76befSYishai Hadas 33797be76befSYishai Hadas rdma_user_mmap_entry_remove(&obj->rdma_entry); 33807be76befSYishai Hadas return 0; 33817be76befSYishai Hadas } 33827be76befSYishai Hadas 3383342ee59dSYishai Hadas static int mlx5_rdma_user_mmap_entry_insert(struct mlx5_ib_ucontext *c, 3384342ee59dSYishai Hadas struct mlx5_user_mmap_entry *entry, 3385342ee59dSYishai Hadas size_t length) 3386342ee59dSYishai Hadas { 3387342ee59dSYishai Hadas return rdma_user_mmap_entry_insert_range( 3388342ee59dSYishai Hadas &c->ibucontext, &entry->rdma_entry, length, 3389342ee59dSYishai Hadas (MLX5_IB_MMAP_OFFSET_START << 16), 3390342ee59dSYishai Hadas ((MLX5_IB_MMAP_OFFSET_END << 16) + (1UL << 16) - 1)); 3391342ee59dSYishai Hadas } 3392342ee59dSYishai Hadas 33937be76befSYishai Hadas static struct mlx5_user_mmap_entry * 33947be76befSYishai Hadas alloc_var_entry(struct mlx5_ib_ucontext *c) 33957be76befSYishai Hadas { 33967be76befSYishai Hadas struct mlx5_user_mmap_entry *entry; 33977be76befSYishai Hadas struct mlx5_var_table *var_table; 33987be76befSYishai Hadas u32 page_idx; 33997be76befSYishai Hadas int err; 34007be76befSYishai Hadas 34017be76befSYishai Hadas var_table = &to_mdev(c->ibucontext.device)->var_table; 34027be76befSYishai Hadas entry = kzalloc(sizeof(*entry), GFP_KERNEL); 34037be76befSYishai Hadas if (!entry) 34047be76befSYishai Hadas return ERR_PTR(-ENOMEM); 34057be76befSYishai Hadas 34067be76befSYishai Hadas mutex_lock(&var_table->bitmap_lock); 34077be76befSYishai Hadas page_idx = find_first_zero_bit(var_table->bitmap, 34087be76befSYishai Hadas var_table->num_var_hw_entries); 34097be76befSYishai Hadas if (page_idx >= var_table->num_var_hw_entries) { 34107be76befSYishai Hadas err = -ENOSPC; 34117be76befSYishai Hadas mutex_unlock(&var_table->bitmap_lock); 34127be76befSYishai Hadas goto end; 34137be76befSYishai Hadas } 34147be76befSYishai Hadas 34157be76befSYishai Hadas set_bit(page_idx, var_table->bitmap); 34167be76befSYishai Hadas mutex_unlock(&var_table->bitmap_lock); 34177be76befSYishai Hadas 34187be76befSYishai Hadas entry->address = var_table->hw_start_addr + 34197be76befSYishai Hadas (page_idx * var_table->stride_size); 34207be76befSYishai Hadas entry->page_idx = page_idx; 34217be76befSYishai Hadas entry->mmap_flag = MLX5_IB_MMAP_TYPE_VAR; 34227be76befSYishai Hadas 3423342ee59dSYishai Hadas err = mlx5_rdma_user_mmap_entry_insert(c, entry, 3424342ee59dSYishai Hadas var_table->stride_size); 34257be76befSYishai Hadas if (err) 34267be76befSYishai Hadas goto err_insert; 34277be76befSYishai Hadas 34287be76befSYishai Hadas return entry; 34297be76befSYishai Hadas 34307be76befSYishai Hadas err_insert: 34317be76befSYishai Hadas mutex_lock(&var_table->bitmap_lock); 34327be76befSYishai Hadas clear_bit(page_idx, var_table->bitmap); 34337be76befSYishai Hadas mutex_unlock(&var_table->bitmap_lock); 34347be76befSYishai Hadas end: 34357be76befSYishai Hadas kfree(entry); 34367be76befSYishai Hadas return ERR_PTR(err); 34377be76befSYishai Hadas } 34387be76befSYishai Hadas 34397be76befSYishai Hadas static int UVERBS_HANDLER(MLX5_IB_METHOD_VAR_OBJ_ALLOC)( 34407be76befSYishai Hadas struct uverbs_attr_bundle *attrs) 34417be76befSYishai Hadas { 34427be76befSYishai Hadas struct ib_uobject *uobj = uverbs_attr_get_uobject( 34437be76befSYishai Hadas attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_HANDLE); 34447be76befSYishai Hadas struct mlx5_ib_ucontext *c; 34457be76befSYishai Hadas struct mlx5_user_mmap_entry *entry; 34467be76befSYishai Hadas u64 mmap_offset; 34477be76befSYishai Hadas u32 length; 34487be76befSYishai Hadas int err; 34497be76befSYishai Hadas 34507be76befSYishai Hadas c = to_mucontext(ib_uverbs_get_ucontext(attrs)); 34517be76befSYishai Hadas if (IS_ERR(c)) 34527be76befSYishai Hadas return PTR_ERR(c); 34537be76befSYishai Hadas 34547be76befSYishai Hadas entry = alloc_var_entry(c); 34557be76befSYishai Hadas if (IS_ERR(entry)) 34567be76befSYishai Hadas return PTR_ERR(entry); 34577be76befSYishai Hadas 34587be76befSYishai Hadas mmap_offset = mlx5_entry_to_mmap_offset(entry); 34597be76befSYishai Hadas length = entry->rdma_entry.npages * PAGE_SIZE; 34607be76befSYishai Hadas uobj->object = entry; 34610ac8903cSJason Gunthorpe uverbs_finalize_uobj_create(attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_HANDLE); 34627be76befSYishai Hadas 34637be76befSYishai Hadas err = uverbs_copy_to(attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_MMAP_OFFSET, 34647be76befSYishai Hadas &mmap_offset, sizeof(mmap_offset)); 34657be76befSYishai Hadas if (err) 34660ac8903cSJason Gunthorpe return err; 34677be76befSYishai Hadas 34687be76befSYishai Hadas err = uverbs_copy_to(attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_PAGE_ID, 34697be76befSYishai Hadas &entry->page_idx, sizeof(entry->page_idx)); 34707be76befSYishai Hadas if (err) 34710ac8903cSJason Gunthorpe return err; 34727be76befSYishai Hadas 34737be76befSYishai Hadas err = uverbs_copy_to(attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_MMAP_LENGTH, 34747be76befSYishai Hadas &length, sizeof(length)); 34757be76befSYishai Hadas return err; 34767be76befSYishai Hadas } 34777be76befSYishai Hadas 34787be76befSYishai Hadas DECLARE_UVERBS_NAMED_METHOD( 34797be76befSYishai Hadas MLX5_IB_METHOD_VAR_OBJ_ALLOC, 34807be76befSYishai Hadas UVERBS_ATTR_IDR(MLX5_IB_ATTR_VAR_OBJ_ALLOC_HANDLE, 34817be76befSYishai Hadas MLX5_IB_OBJECT_VAR, 34827be76befSYishai Hadas UVERBS_ACCESS_NEW, 34837be76befSYishai Hadas UA_MANDATORY), 34847be76befSYishai Hadas UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_VAR_OBJ_ALLOC_PAGE_ID, 34857be76befSYishai Hadas UVERBS_ATTR_TYPE(u32), 34867be76befSYishai Hadas UA_MANDATORY), 34877be76befSYishai Hadas UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_VAR_OBJ_ALLOC_MMAP_LENGTH, 34887be76befSYishai Hadas UVERBS_ATTR_TYPE(u32), 34897be76befSYishai Hadas UA_MANDATORY), 34907be76befSYishai Hadas UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_VAR_OBJ_ALLOC_MMAP_OFFSET, 34917be76befSYishai Hadas UVERBS_ATTR_TYPE(u64), 34927be76befSYishai Hadas UA_MANDATORY)); 34937be76befSYishai Hadas 34947be76befSYishai Hadas DECLARE_UVERBS_NAMED_METHOD_DESTROY( 34957be76befSYishai Hadas MLX5_IB_METHOD_VAR_OBJ_DESTROY, 34967be76befSYishai Hadas UVERBS_ATTR_IDR(MLX5_IB_ATTR_VAR_OBJ_DESTROY_HANDLE, 34977be76befSYishai Hadas MLX5_IB_OBJECT_VAR, 34987be76befSYishai Hadas UVERBS_ACCESS_DESTROY, 34997be76befSYishai Hadas UA_MANDATORY)); 35007be76befSYishai Hadas 35017be76befSYishai Hadas DECLARE_UVERBS_NAMED_OBJECT(MLX5_IB_OBJECT_VAR, 3502342ee59dSYishai Hadas UVERBS_TYPE_ALLOC_IDR(mmap_obj_cleanup), 35037be76befSYishai Hadas &UVERBS_METHOD(MLX5_IB_METHOD_VAR_OBJ_ALLOC), 35047be76befSYishai Hadas &UVERBS_METHOD(MLX5_IB_METHOD_VAR_OBJ_DESTROY)); 35057be76befSYishai Hadas 35067be76befSYishai Hadas static bool var_is_supported(struct ib_device *device) 35077be76befSYishai Hadas { 35087be76befSYishai Hadas struct mlx5_ib_dev *dev = to_mdev(device); 35097be76befSYishai Hadas 35107be76befSYishai Hadas return (MLX5_CAP_GEN_64(dev->mdev, general_obj_types) & 35117be76befSYishai Hadas MLX5_GENERAL_OBJ_TYPES_CAP_VIRTIO_NET_Q); 35127be76befSYishai Hadas } 35137be76befSYishai Hadas 3514342ee59dSYishai Hadas static struct mlx5_user_mmap_entry * 3515342ee59dSYishai Hadas alloc_uar_entry(struct mlx5_ib_ucontext *c, 3516342ee59dSYishai Hadas enum mlx5_ib_uapi_uar_alloc_type alloc_type) 3517342ee59dSYishai Hadas { 3518342ee59dSYishai Hadas struct mlx5_user_mmap_entry *entry; 3519342ee59dSYishai Hadas struct mlx5_ib_dev *dev; 3520342ee59dSYishai Hadas u32 uar_index; 3521342ee59dSYishai Hadas int err; 3522342ee59dSYishai Hadas 3523342ee59dSYishai Hadas entry = kzalloc(sizeof(*entry), GFP_KERNEL); 3524342ee59dSYishai Hadas if (!entry) 3525342ee59dSYishai Hadas return ERR_PTR(-ENOMEM); 3526342ee59dSYishai Hadas 3527342ee59dSYishai Hadas dev = to_mdev(c->ibucontext.device); 3528342ee59dSYishai Hadas err = mlx5_cmd_alloc_uar(dev->mdev, &uar_index); 3529342ee59dSYishai Hadas if (err) 3530342ee59dSYishai Hadas goto end; 3531342ee59dSYishai Hadas 3532342ee59dSYishai Hadas entry->page_idx = uar_index; 3533342ee59dSYishai Hadas entry->address = uar_index2paddress(dev, uar_index); 3534342ee59dSYishai Hadas if (alloc_type == MLX5_IB_UAPI_UAR_ALLOC_TYPE_BF) 3535342ee59dSYishai Hadas entry->mmap_flag = MLX5_IB_MMAP_TYPE_UAR_WC; 3536342ee59dSYishai Hadas else 3537342ee59dSYishai Hadas entry->mmap_flag = MLX5_IB_MMAP_TYPE_UAR_NC; 3538342ee59dSYishai Hadas 3539342ee59dSYishai Hadas err = mlx5_rdma_user_mmap_entry_insert(c, entry, PAGE_SIZE); 3540342ee59dSYishai Hadas if (err) 3541342ee59dSYishai Hadas goto err_insert; 3542342ee59dSYishai Hadas 3543342ee59dSYishai Hadas return entry; 3544342ee59dSYishai Hadas 3545342ee59dSYishai Hadas err_insert: 3546342ee59dSYishai Hadas mlx5_cmd_free_uar(dev->mdev, uar_index); 3547342ee59dSYishai Hadas end: 3548342ee59dSYishai Hadas kfree(entry); 3549342ee59dSYishai Hadas return ERR_PTR(err); 3550342ee59dSYishai Hadas } 3551342ee59dSYishai Hadas 3552342ee59dSYishai Hadas static int UVERBS_HANDLER(MLX5_IB_METHOD_UAR_OBJ_ALLOC)( 3553342ee59dSYishai Hadas struct uverbs_attr_bundle *attrs) 3554342ee59dSYishai Hadas { 3555342ee59dSYishai Hadas struct ib_uobject *uobj = uverbs_attr_get_uobject( 3556342ee59dSYishai Hadas attrs, MLX5_IB_ATTR_UAR_OBJ_ALLOC_HANDLE); 3557342ee59dSYishai Hadas enum mlx5_ib_uapi_uar_alloc_type alloc_type; 3558342ee59dSYishai Hadas struct mlx5_ib_ucontext *c; 3559342ee59dSYishai Hadas struct mlx5_user_mmap_entry *entry; 3560342ee59dSYishai Hadas u64 mmap_offset; 3561342ee59dSYishai Hadas u32 length; 3562342ee59dSYishai Hadas int err; 3563342ee59dSYishai Hadas 3564342ee59dSYishai Hadas c = to_mucontext(ib_uverbs_get_ucontext(attrs)); 3565342ee59dSYishai Hadas if (IS_ERR(c)) 3566342ee59dSYishai Hadas return PTR_ERR(c); 3567342ee59dSYishai Hadas 3568342ee59dSYishai Hadas err = uverbs_get_const(&alloc_type, attrs, 3569342ee59dSYishai Hadas MLX5_IB_ATTR_UAR_OBJ_ALLOC_TYPE); 3570342ee59dSYishai Hadas if (err) 3571342ee59dSYishai Hadas return err; 3572342ee59dSYishai Hadas 3573342ee59dSYishai Hadas if (alloc_type != MLX5_IB_UAPI_UAR_ALLOC_TYPE_BF && 3574342ee59dSYishai Hadas alloc_type != MLX5_IB_UAPI_UAR_ALLOC_TYPE_NC) 3575342ee59dSYishai Hadas return -EOPNOTSUPP; 3576342ee59dSYishai Hadas 3577342ee59dSYishai Hadas if (!to_mdev(c->ibucontext.device)->wc_support && 3578342ee59dSYishai Hadas alloc_type == MLX5_IB_UAPI_UAR_ALLOC_TYPE_BF) 3579342ee59dSYishai Hadas return -EOPNOTSUPP; 3580342ee59dSYishai Hadas 3581342ee59dSYishai Hadas entry = alloc_uar_entry(c, alloc_type); 3582342ee59dSYishai Hadas if (IS_ERR(entry)) 3583342ee59dSYishai Hadas return PTR_ERR(entry); 3584342ee59dSYishai Hadas 3585342ee59dSYishai Hadas mmap_offset = mlx5_entry_to_mmap_offset(entry); 3586342ee59dSYishai Hadas length = entry->rdma_entry.npages * PAGE_SIZE; 3587342ee59dSYishai Hadas uobj->object = entry; 35880ac8903cSJason Gunthorpe uverbs_finalize_uobj_create(attrs, MLX5_IB_ATTR_UAR_OBJ_ALLOC_HANDLE); 3589342ee59dSYishai Hadas 3590342ee59dSYishai Hadas err = uverbs_copy_to(attrs, MLX5_IB_ATTR_UAR_OBJ_ALLOC_MMAP_OFFSET, 3591342ee59dSYishai Hadas &mmap_offset, sizeof(mmap_offset)); 3592342ee59dSYishai Hadas if (err) 35930ac8903cSJason Gunthorpe return err; 3594342ee59dSYishai Hadas 3595342ee59dSYishai Hadas err = uverbs_copy_to(attrs, MLX5_IB_ATTR_UAR_OBJ_ALLOC_PAGE_ID, 3596342ee59dSYishai Hadas &entry->page_idx, sizeof(entry->page_idx)); 3597342ee59dSYishai Hadas if (err) 35980ac8903cSJason Gunthorpe return err; 3599342ee59dSYishai Hadas 3600342ee59dSYishai Hadas err = uverbs_copy_to(attrs, MLX5_IB_ATTR_UAR_OBJ_ALLOC_MMAP_LENGTH, 3601342ee59dSYishai Hadas &length, sizeof(length)); 3602342ee59dSYishai Hadas return err; 3603342ee59dSYishai Hadas } 3604342ee59dSYishai Hadas 3605342ee59dSYishai Hadas DECLARE_UVERBS_NAMED_METHOD( 3606342ee59dSYishai Hadas MLX5_IB_METHOD_UAR_OBJ_ALLOC, 3607342ee59dSYishai Hadas UVERBS_ATTR_IDR(MLX5_IB_ATTR_UAR_OBJ_ALLOC_HANDLE, 3608342ee59dSYishai Hadas MLX5_IB_OBJECT_UAR, 3609342ee59dSYishai Hadas UVERBS_ACCESS_NEW, 3610342ee59dSYishai Hadas UA_MANDATORY), 3611342ee59dSYishai Hadas UVERBS_ATTR_CONST_IN(MLX5_IB_ATTR_UAR_OBJ_ALLOC_TYPE, 3612342ee59dSYishai Hadas enum mlx5_ib_uapi_uar_alloc_type, 3613342ee59dSYishai Hadas UA_MANDATORY), 3614342ee59dSYishai Hadas UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_UAR_OBJ_ALLOC_PAGE_ID, 3615342ee59dSYishai Hadas UVERBS_ATTR_TYPE(u32), 3616342ee59dSYishai Hadas UA_MANDATORY), 3617342ee59dSYishai Hadas UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_UAR_OBJ_ALLOC_MMAP_LENGTH, 3618342ee59dSYishai Hadas UVERBS_ATTR_TYPE(u32), 3619342ee59dSYishai Hadas UA_MANDATORY), 3620342ee59dSYishai Hadas UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_UAR_OBJ_ALLOC_MMAP_OFFSET, 3621342ee59dSYishai Hadas UVERBS_ATTR_TYPE(u64), 3622342ee59dSYishai Hadas UA_MANDATORY)); 3623342ee59dSYishai Hadas 3624342ee59dSYishai Hadas DECLARE_UVERBS_NAMED_METHOD_DESTROY( 3625342ee59dSYishai Hadas MLX5_IB_METHOD_UAR_OBJ_DESTROY, 3626342ee59dSYishai Hadas UVERBS_ATTR_IDR(MLX5_IB_ATTR_UAR_OBJ_DESTROY_HANDLE, 3627342ee59dSYishai Hadas MLX5_IB_OBJECT_UAR, 3628342ee59dSYishai Hadas UVERBS_ACCESS_DESTROY, 3629342ee59dSYishai Hadas UA_MANDATORY)); 3630342ee59dSYishai Hadas 3631342ee59dSYishai Hadas DECLARE_UVERBS_NAMED_OBJECT(MLX5_IB_OBJECT_UAR, 3632342ee59dSYishai Hadas UVERBS_TYPE_ALLOC_IDR(mmap_obj_cleanup), 3633342ee59dSYishai Hadas &UVERBS_METHOD(MLX5_IB_METHOD_UAR_OBJ_ALLOC), 3634342ee59dSYishai Hadas &UVERBS_METHOD(MLX5_IB_METHOD_UAR_OBJ_DESTROY)); 3635342ee59dSYishai Hadas 36369a119cd5SJason Gunthorpe ADD_UVERBS_ATTRIBUTES_SIMPLE( 36379a119cd5SJason Gunthorpe mlx5_ib_flow_action, 36389a119cd5SJason Gunthorpe UVERBS_OBJECT_FLOW_ACTION, 3639c6475a0bSAviad Yehezkel UVERBS_METHOD_FLOW_ACTION_ESP_CREATE, 3640bccd0622SJason Gunthorpe UVERBS_ATTR_FLAGS_IN(MLX5_IB_ATTR_CREATE_FLOW_ACTION_FLAGS, 3641bccd0622SJason Gunthorpe enum mlx5_ib_uapi_flow_action_flags)); 3642c6475a0bSAviad Yehezkel 36430fb556b2SYishai Hadas ADD_UVERBS_ATTRIBUTES_SIMPLE( 36440fb556b2SYishai Hadas mlx5_ib_query_context, 36450fb556b2SYishai Hadas UVERBS_OBJECT_DEVICE, 36460fb556b2SYishai Hadas UVERBS_METHOD_QUERY_CONTEXT, 36470fb556b2SYishai Hadas UVERBS_ATTR_PTR_OUT( 36480fb556b2SYishai Hadas MLX5_IB_ATTR_QUERY_CONTEXT_RESP_UCTX, 36490fb556b2SYishai Hadas UVERBS_ATTR_STRUCT(struct mlx5_ib_alloc_ucontext_resp, 36500fb556b2SYishai Hadas dump_fill_mkey), 36510fb556b2SYishai Hadas UA_MANDATORY)); 36520fb556b2SYishai Hadas 36530cbf432dSJason Gunthorpe static const struct uapi_definition mlx5_ib_defs[] = { 365436e235c8SJason Gunthorpe UAPI_DEF_CHAIN(mlx5_ib_devx_defs), 36550cbf432dSJason Gunthorpe UAPI_DEF_CHAIN(mlx5_ib_flow_defs), 365630f2fe40SYishai Hadas UAPI_DEF_CHAIN(mlx5_ib_qos_defs), 365705f71ef9SYishai Hadas UAPI_DEF_CHAIN(mlx5_ib_std_types_defs), 3658831df883SMaor Gottlieb UAPI_DEF_CHAIN(mlx5_ib_dm_defs), 36598c84660bSMatan Barak 36600cbf432dSJason Gunthorpe UAPI_DEF_CHAIN_OBJ_TREE(UVERBS_OBJECT_FLOW_ACTION, 36610cbf432dSJason Gunthorpe &mlx5_ib_flow_action), 36620fb556b2SYishai Hadas UAPI_DEF_CHAIN_OBJ_TREE(UVERBS_OBJECT_DEVICE, &mlx5_ib_query_context), 36637be76befSYishai Hadas UAPI_DEF_CHAIN_OBJ_TREE_NAMED(MLX5_IB_OBJECT_VAR, 36647be76befSYishai Hadas UAPI_DEF_IS_OBJ_SUPPORTED(var_is_supported)), 3665342ee59dSYishai Hadas UAPI_DEF_CHAIN_OBJ_TREE_NAMED(MLX5_IB_OBJECT_UAR), 36660cbf432dSJason Gunthorpe {} 36670cbf432dSJason Gunthorpe }; 36688c84660bSMatan Barak 3669fb652d32SMark Bloch static void mlx5_ib_stage_init_cleanup(struct mlx5_ib_dev *dev) 3670e126ba97SEli Cohen { 367132f69e4bSDaniel Jurgens mlx5_ib_cleanup_multiport_master(dev); 3672806b101bSJason Gunthorpe WARN_ON(!xa_empty(&dev->odp_mkeys)); 3673ab40530aSParav Pandit mutex_destroy(&dev->cap_mask_mutex); 367450211ec9SJason Gunthorpe WARN_ON(!xa_empty(&dev->sig_mrs)); 36754056b12eSAriel Levkovich WARN_ON(!bitmap_empty(dev->dm.memic_alloc_pages, MLX5_MAX_MEMIC_PAGES)); 367616c1975fSMark Bloch } 367716c1975fSMark Bloch 3678fb652d32SMark Bloch static int mlx5_ib_stage_init_init(struct mlx5_ib_dev *dev) 367916c1975fSMark Bloch { 368016c1975fSMark Bloch struct mlx5_core_dev *mdev = dev->mdev; 3681e126ba97SEli Cohen int err; 368232f69e4bSDaniel Jurgens int i; 3683e126ba97SEli Cohen 368413179652SParav Pandit dev->ib_dev.node_type = RDMA_NODE_IB_CA; 368513179652SParav Pandit dev->ib_dev.local_dma_lkey = 0 /* not supported for now */; 368613179652SParav Pandit dev->ib_dev.phys_port_cnt = dev->num_ports; 368713179652SParav Pandit dev->ib_dev.dev.parent = mdev->device; 368813179652SParav Pandit dev->ib_dev.lag_flags = RDMA_LAG_FLAGS_HASH_ALL_SLAVES; 368913179652SParav Pandit 369032f69e4bSDaniel Jurgens for (i = 0; i < dev->num_ports; i++) { 369132f69e4bSDaniel Jurgens spin_lock_init(&dev->port[i].mp.mpi_lock); 369295579e78SMark Bloch rwlock_init(&dev->port[i].roce.netdev_lock); 3693d3b5cc1cSMark Bloch dev->port[i].roce.dev = dev; 3694d3b5cc1cSMark Bloch dev->port[i].roce.native_port_num = i + 1; 3695d3b5cc1cSMark Bloch dev->port[i].roce.last_port_state = IB_PORT_DOWN; 369632f69e4bSDaniel Jurgens } 369732f69e4bSDaniel Jurgens 369832f69e4bSDaniel Jurgens err = mlx5_ib_init_multiport_master(dev); 3699e126ba97SEli Cohen if (err) 3700da796ccbSMark Bloch return err; 3701e126ba97SEli Cohen 3702a989ea01SMark Bloch err = set_has_smi_cap(dev); 3703a989ea01SMark Bloch if (err) 37042cb091f6SParav Pandit goto err_mp; 3705e126ba97SEli Cohen 37062019d70eSParav Pandit err = mlx5_query_max_pkeys(&dev->ib_dev, &dev->pkey_table_len); 37072019d70eSParav Pandit if (err) 37082019d70eSParav Pandit goto err_mp; 37092019d70eSParav Pandit 37101b5daf11SMajd Dibbiny if (mlx5_use_mad_ifc(dev)) 3711e126ba97SEli Cohen get_ext_port_caps(dev); 3712e126ba97SEli Cohen 3713f2f3df55SSaeed Mahameed dev->ib_dev.num_comp_vectors = mlx5_comp_vectors_count(mdev); 3714e126ba97SEli Cohen 37153cc297dbSMark Bloch mutex_init(&dev->cap_mask_mutex); 37163cc297dbSMark Bloch INIT_LIST_HEAD(&dev->qp_list); 37173cc297dbSMark Bloch spin_lock_init(&dev->reset_flow_resource_lock); 3718806b101bSJason Gunthorpe xa_init(&dev->odp_mkeys); 371950211ec9SJason Gunthorpe xa_init(&dev->sig_mrs); 3720f743ff3bSSaeed Mahameed atomic_set(&dev->mkey_var, 0); 37213cc297dbSMark Bloch 37223b113a1eSAriel Levkovich spin_lock_init(&dev->dm.lock); 37233b113a1eSAriel Levkovich dev->dm.dev = mdev; 372416c1975fSMark Bloch return 0; 372525c13324SAriel Levkovich 372632f69e4bSDaniel Jurgens err_mp: 372732f69e4bSDaniel Jurgens mlx5_ib_cleanup_multiport_master(dev); 3728d286ac1dSParav Pandit return err; 372916c1975fSMark Bloch } 373016c1975fSMark Bloch 3731ac47bf5eSLeon Romanovsky static int mlx5_ib_enable_driver(struct ib_device *dev) 37329a4ca38dSMark Bloch { 3733ac47bf5eSLeon Romanovsky struct mlx5_ib_dev *mdev = to_mdev(dev); 3734ac47bf5eSLeon Romanovsky int ret; 37359a4ca38dSMark Bloch 3736ac47bf5eSLeon Romanovsky ret = mlx5_ib_test_wc(mdev); 3737ac47bf5eSLeon Romanovsky mlx5_ib_dbg(mdev, "Write-Combining %s", 3738ac47bf5eSLeon Romanovsky mdev->wc_support ? "supported" : "not supported"); 37399a4ca38dSMark Bloch 3740ac47bf5eSLeon Romanovsky return ret; 37419a4ca38dSMark Bloch } 37429a4ca38dSMark Bloch 374396458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_ops = { 37447a154142SJason Gunthorpe .owner = THIS_MODULE, 3745b9560a41SJason Gunthorpe .driver_id = RDMA_DRIVER_MLX5, 374672c6ec18SJason Gunthorpe .uverbs_abi_ver = MLX5_IB_UVERBS_ABI_VERSION, 3747b9560a41SJason Gunthorpe 374896458233SKamal Heib .add_gid = mlx5_ib_add_gid, 374996458233SKamal Heib .alloc_mr = mlx5_ib_alloc_mr, 37506c984472SMax Gurtovoy .alloc_mr_integrity = mlx5_ib_alloc_mr_integrity, 375196458233SKamal Heib .alloc_pd = mlx5_ib_alloc_pd, 375296458233SKamal Heib .alloc_ucontext = mlx5_ib_alloc_ucontext, 375396458233SKamal Heib .attach_mcast = mlx5_ib_mcg_attach, 375496458233SKamal Heib .check_mr_status = mlx5_ib_check_mr_status, 375596458233SKamal Heib .create_ah = mlx5_ib_create_ah, 375696458233SKamal Heib .create_cq = mlx5_ib_create_cq, 375796458233SKamal Heib .create_qp = mlx5_ib_create_qp, 375896458233SKamal Heib .create_srq = mlx5_ib_create_srq, 3759676a80adSJason Gunthorpe .create_user_ah = mlx5_ib_create_ah, 376096458233SKamal Heib .dealloc_pd = mlx5_ib_dealloc_pd, 376196458233SKamal Heib .dealloc_ucontext = mlx5_ib_dealloc_ucontext, 376296458233SKamal Heib .del_gid = mlx5_ib_del_gid, 376396458233SKamal Heib .dereg_mr = mlx5_ib_dereg_mr, 376496458233SKamal Heib .destroy_ah = mlx5_ib_destroy_ah, 376596458233SKamal Heib .destroy_cq = mlx5_ib_destroy_cq, 376696458233SKamal Heib .destroy_qp = mlx5_ib_destroy_qp, 376796458233SKamal Heib .destroy_srq = mlx5_ib_destroy_srq, 376896458233SKamal Heib .detach_mcast = mlx5_ib_mcg_detach, 376996458233SKamal Heib .disassociate_ucontext = mlx5_ib_disassociate_ucontext, 377096458233SKamal Heib .drain_rq = mlx5_ib_drain_rq, 377196458233SKamal Heib .drain_sq = mlx5_ib_drain_sq, 3772915e4af5SJason Gunthorpe .device_group = &mlx5_attr_group, 377311f552e2SMichael Guralnik .enable_driver = mlx5_ib_enable_driver, 377496458233SKamal Heib .get_dev_fw_str = get_dev_fw_str, 377596458233SKamal Heib .get_dma_mr = mlx5_ib_get_dma_mr, 377696458233SKamal Heib .get_link_layer = mlx5_ib_port_link_layer, 377796458233SKamal Heib .map_mr_sg = mlx5_ib_map_mr_sg, 37786c984472SMax Gurtovoy .map_mr_sg_pi = mlx5_ib_map_mr_sg_pi, 377996458233SKamal Heib .mmap = mlx5_ib_mmap, 3780dc2316ebSYishai Hadas .mmap_free = mlx5_ib_mmap_free, 378196458233SKamal Heib .modify_cq = mlx5_ib_modify_cq, 378296458233SKamal Heib .modify_device = mlx5_ib_modify_device, 378396458233SKamal Heib .modify_port = mlx5_ib_modify_port, 378496458233SKamal Heib .modify_qp = mlx5_ib_modify_qp, 378596458233SKamal Heib .modify_srq = mlx5_ib_modify_srq, 378696458233SKamal Heib .poll_cq = mlx5_ib_poll_cq, 3787029e88fdSLeon Romanovsky .post_recv = mlx5_ib_post_recv_nodrain, 3788029e88fdSLeon Romanovsky .post_send = mlx5_ib_post_send_nodrain, 378996458233SKamal Heib .post_srq_recv = mlx5_ib_post_srq_recv, 379096458233SKamal Heib .process_mad = mlx5_ib_process_mad, 379196458233SKamal Heib .query_ah = mlx5_ib_query_ah, 379296458233SKamal Heib .query_device = mlx5_ib_query_device, 379396458233SKamal Heib .query_gid = mlx5_ib_query_gid, 379496458233SKamal Heib .query_pkey = mlx5_ib_query_pkey, 379596458233SKamal Heib .query_qp = mlx5_ib_query_qp, 379696458233SKamal Heib .query_srq = mlx5_ib_query_srq, 37970fb556b2SYishai Hadas .query_ucontext = mlx5_ib_query_ucontext, 379896458233SKamal Heib .reg_user_mr = mlx5_ib_reg_user_mr, 379990da7dc8SJianxin Xiong .reg_user_mr_dmabuf = mlx5_ib_reg_user_mr_dmabuf, 380096458233SKamal Heib .req_notify_cq = mlx5_ib_arm_cq, 380196458233SKamal Heib .rereg_user_mr = mlx5_ib_rereg_user_mr, 380296458233SKamal Heib .resize_cq = mlx5_ib_resize_cq, 3803d3456914SLeon Romanovsky 3804d3456914SLeon Romanovsky INIT_RDMA_OBJ_SIZE(ib_ah, mlx5_ib_ah, ibah), 38053b023e1bSLeon Romanovsky INIT_RDMA_OBJ_SIZE(ib_counters, mlx5_ib_mcounters, ibcntrs), 3806e39afe3dSLeon Romanovsky INIT_RDMA_OBJ_SIZE(ib_cq, mlx5_ib_cq, ibcq), 380721a428a0SLeon Romanovsky INIT_RDMA_OBJ_SIZE(ib_pd, mlx5_ib_pd, ibpd), 380868e326deSLeon Romanovsky INIT_RDMA_OBJ_SIZE(ib_srq, mlx5_ib_srq, ibsrq), 3809a2a074efSLeon Romanovsky INIT_RDMA_OBJ_SIZE(ib_ucontext, mlx5_ib_ucontext, ibucontext), 381096458233SKamal Heib }; 381196458233SKamal Heib 381296458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_ipoib_enhanced_ops = { 381396458233SKamal Heib .rdma_netdev_get_params = mlx5_ib_rn_get_params, 381496458233SKamal Heib }; 381596458233SKamal Heib 381696458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_sriov_ops = { 381796458233SKamal Heib .get_vf_config = mlx5_ib_get_vf_config, 38189c0015efSDanit Goldberg .get_vf_guid = mlx5_ib_get_vf_guid, 381996458233SKamal Heib .get_vf_stats = mlx5_ib_get_vf_stats, 382096458233SKamal Heib .set_vf_guid = mlx5_ib_set_vf_guid, 382196458233SKamal Heib .set_vf_link_state = mlx5_ib_set_vf_link_state, 382296458233SKamal Heib }; 382396458233SKamal Heib 382496458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_mw_ops = { 382596458233SKamal Heib .alloc_mw = mlx5_ib_alloc_mw, 382696458233SKamal Heib .dealloc_mw = mlx5_ib_dealloc_mw, 3827d18bb3e1SLeon Romanovsky 3828d18bb3e1SLeon Romanovsky INIT_RDMA_OBJ_SIZE(ib_mw, mlx5_ib_mw, ibmw), 382996458233SKamal Heib }; 383096458233SKamal Heib 383196458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_xrc_ops = { 383296458233SKamal Heib .alloc_xrcd = mlx5_ib_alloc_xrcd, 383396458233SKamal Heib .dealloc_xrcd = mlx5_ib_dealloc_xrcd, 383428ad5f65SLeon Romanovsky 383528ad5f65SLeon Romanovsky INIT_RDMA_OBJ_SIZE(ib_xrcd, mlx5_ib_xrcd, ibxrcd), 383696458233SKamal Heib }; 383796458233SKamal Heib 3838f164be8cSYishai Hadas static int mlx5_ib_init_var_table(struct mlx5_ib_dev *dev) 3839f164be8cSYishai Hadas { 3840f164be8cSYishai Hadas struct mlx5_core_dev *mdev = dev->mdev; 3841f164be8cSYishai Hadas struct mlx5_var_table *var_table = &dev->var_table; 3842f164be8cSYishai Hadas u8 log_doorbell_bar_size; 3843f164be8cSYishai Hadas u8 log_doorbell_stride; 3844f164be8cSYishai Hadas u64 bar_size; 3845f164be8cSYishai Hadas 3846f164be8cSYishai Hadas log_doorbell_bar_size = MLX5_CAP_DEV_VDPA_EMULATION(mdev, 3847f164be8cSYishai Hadas log_doorbell_bar_size); 3848f164be8cSYishai Hadas log_doorbell_stride = MLX5_CAP_DEV_VDPA_EMULATION(mdev, 3849f164be8cSYishai Hadas log_doorbell_stride); 3850f164be8cSYishai Hadas var_table->hw_start_addr = dev->mdev->bar_addr + 3851f164be8cSYishai Hadas MLX5_CAP64_DEV_VDPA_EMULATION(mdev, 3852f164be8cSYishai Hadas doorbell_bar_offset); 3853f164be8cSYishai Hadas bar_size = (1ULL << log_doorbell_bar_size) * 4096; 3854f164be8cSYishai Hadas var_table->stride_size = 1ULL << log_doorbell_stride; 385591b74bf5SAlexander Lobakin var_table->num_var_hw_entries = div_u64(bar_size, 385691b74bf5SAlexander Lobakin var_table->stride_size); 3857f164be8cSYishai Hadas mutex_init(&var_table->bitmap_lock); 3858f164be8cSYishai Hadas var_table->bitmap = bitmap_zalloc(var_table->num_var_hw_entries, 3859f164be8cSYishai Hadas GFP_KERNEL); 3860f164be8cSYishai Hadas return (var_table->bitmap) ? 0 : -ENOMEM; 3861f164be8cSYishai Hadas } 3862f164be8cSYishai Hadas 3863f164be8cSYishai Hadas static void mlx5_ib_stage_caps_cleanup(struct mlx5_ib_dev *dev) 3864f164be8cSYishai Hadas { 3865f164be8cSYishai Hadas bitmap_free(dev->var_table.bitmap); 3866f164be8cSYishai Hadas } 3867f164be8cSYishai Hadas 3868fb652d32SMark Bloch static int mlx5_ib_stage_caps_init(struct mlx5_ib_dev *dev) 386916c1975fSMark Bloch { 387016c1975fSMark Bloch struct mlx5_core_dev *mdev = dev->mdev; 387116c1975fSMark Bloch int err; 387216c1975fSMark Bloch 3873f6a8a19bSDenis Drozdov if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads) && 3874f6a8a19bSDenis Drozdov IS_ENABLED(CONFIG_MLX5_CORE_IPOIB)) 387596458233SKamal Heib ib_set_device_ops(&dev->ib_dev, 387696458233SKamal Heib &mlx5_ib_dev_ipoib_enhanced_ops); 38778e959601SNiranjana Vishwanathapura 387896458233SKamal Heib if (mlx5_core_is_pf(mdev)) 387996458233SKamal Heib ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_sriov_ops); 38807c2344c3SMaor Gottlieb 38816e8484c5SMax Gurtovoy dev->umr_fence = mlx5_get_umr_fence(MLX5_CAP_GEN(mdev, umr_fence)); 38826e8484c5SMax Gurtovoy 388344ce37bcSJason Gunthorpe if (MLX5_CAP_GEN(mdev, imaicl)) 388496458233SKamal Heib ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_mw_ops); 3885d2370e0aSMatan Barak 388644ce37bcSJason Gunthorpe if (MLX5_CAP_GEN(mdev, xrc)) 388796458233SKamal Heib ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_xrc_ops); 3888e126ba97SEli Cohen 388925c13324SAriel Levkovich if (MLX5_CAP_DEV_MEM(mdev, memic) || 389025c13324SAriel Levkovich MLX5_CAP_GEN_64(dev->mdev, general_obj_types) & 389125c13324SAriel Levkovich MLX5_GENERAL_OBJ_TYPES_CAP_SW_ICM) 389296458233SKamal Heib ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_dm_ops); 389324da0016SAriel Levkovich 389496458233SKamal Heib ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_ops); 389581e30880SYishai Hadas 389636e235c8SJason Gunthorpe if (IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS)) 389736e235c8SJason Gunthorpe dev->ib_dev.driver_def = mlx5_ib_defs; 389881e30880SYishai Hadas 3899e126ba97SEli Cohen err = init_node_data(dev); 3900e126ba97SEli Cohen if (err) 390116c1975fSMark Bloch return err; 3902e126ba97SEli Cohen 3903c8b89924SMark Bloch if ((MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_ETH) && 3904e7996a9aSJason Gunthorpe (MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) || 3905e7996a9aSJason Gunthorpe MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc))) 3906a560f1d9SMark Bloch mutex_init(&dev->lb.mutex); 3907c8b89924SMark Bloch 3908f164be8cSYishai Hadas if (MLX5_CAP_GEN_64(dev->mdev, general_obj_types) & 3909f164be8cSYishai Hadas MLX5_GENERAL_OBJ_TYPES_CAP_VIRTIO_NET_Q) { 3910f164be8cSYishai Hadas err = mlx5_ib_init_var_table(dev); 3911f164be8cSYishai Hadas if (err) 3912f164be8cSYishai Hadas return err; 3913f164be8cSYishai Hadas } 3914f164be8cSYishai Hadas 391596e2fd73SLeon Romanovsky dev->ib_dev.use_cq_dim = true; 391696e2fd73SLeon Romanovsky 391716c1975fSMark Bloch return 0; 391816c1975fSMark Bloch } 391916c1975fSMark Bloch 392096458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_port_ops = { 392196458233SKamal Heib .get_port_immutable = mlx5_port_immutable, 392296458233SKamal Heib .query_port = mlx5_ib_query_port, 392396458233SKamal Heib }; 392496458233SKamal Heib 39258e6efa3aSMark Bloch static int mlx5_ib_stage_non_default_cb(struct mlx5_ib_dev *dev) 392616c1975fSMark Bloch { 392796458233SKamal Heib ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_port_ops); 39288e6efa3aSMark Bloch return 0; 39298e6efa3aSMark Bloch } 39308e6efa3aSMark Bloch 393196458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_port_rep_ops = { 393296458233SKamal Heib .get_port_immutable = mlx5_port_rep_immutable, 393396458233SKamal Heib .query_port = mlx5_ib_rep_query_port, 3934d6fd59e1SParav Pandit .query_pkey = mlx5_ib_rep_query_pkey, 393596458233SKamal Heib }; 393696458233SKamal Heib 3937b5a498baSMichael Guralnik static int mlx5_ib_stage_raw_eth_non_default_cb(struct mlx5_ib_dev *dev) 39388e6efa3aSMark Bloch { 393996458233SKamal Heib ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_port_rep_ops); 39408e6efa3aSMark Bloch return 0; 39418e6efa3aSMark Bloch } 39428e6efa3aSMark Bloch 394396458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_common_roce_ops = { 394496458233SKamal Heib .create_rwq_ind_table = mlx5_ib_create_rwq_ind_table, 394596458233SKamal Heib .create_wq = mlx5_ib_create_wq, 394696458233SKamal Heib .destroy_rwq_ind_table = mlx5_ib_destroy_rwq_ind_table, 394796458233SKamal Heib .destroy_wq = mlx5_ib_destroy_wq, 394896458233SKamal Heib .get_netdev = mlx5_ib_get_netdev, 394996458233SKamal Heib .modify_wq = mlx5_ib_modify_wq, 3950c0a6b5ecSLeon Romanovsky 3951c0a6b5ecSLeon Romanovsky INIT_RDMA_OBJ_SIZE(ib_rwq_ind_table, mlx5_ib_rwq_ind_table, 3952c0a6b5ecSLeon Romanovsky ib_rwq_ind_tbl), 395396458233SKamal Heib }; 395496458233SKamal Heib 39551e2b5a90SLeon Romanovsky static int mlx5_ib_roce_init(struct mlx5_ib_dev *dev) 39568e6efa3aSMark Bloch { 39571e2b5a90SLeon Romanovsky struct mlx5_core_dev *mdev = dev->mdev; 39581e2b5a90SLeon Romanovsky enum rdma_link_layer ll; 39591e2b5a90SLeon Romanovsky int port_type_cap; 39601fb7f897SMark Bloch u32 port_num = 0; 39611e2b5a90SLeon Romanovsky int err; 39627fd8aefbSDaniel Jurgens 39631e2b5a90SLeon Romanovsky port_type_cap = MLX5_CAP_GEN(mdev, port_type); 39641e2b5a90SLeon Romanovsky ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap); 39651e2b5a90SLeon Romanovsky 39661e2b5a90SLeon Romanovsky if (ll == IB_LINK_LAYER_ETHERNET) { 396796458233SKamal Heib ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_common_roce_ops); 39688e6efa3aSMark Bloch 3969e3f1ed1fSLeon Romanovsky port_num = mlx5_core_native_port_num(dev->mdev) - 1; 3970e3f1ed1fSLeon Romanovsky 397126628e2dSMark Bloch /* Register only for native ports */ 39721e2b5a90SLeon Romanovsky err = mlx5_add_netdev_notifier(dev, port_num); 39737852546fSMaor Gottlieb if (err || dev->is_rep || !mlx5_is_roce_init_enabled(mdev)) 39741e2b5a90SLeon Romanovsky /* 39751e2b5a90SLeon Romanovsky * We don't enable ETH interface for 39761e2b5a90SLeon Romanovsky * 1. IB representors 39771e2b5a90SLeon Romanovsky * 2. User disabled ROCE through devlink interface 39781e2b5a90SLeon Romanovsky */ 39798e6efa3aSMark Bloch return err; 39808e6efa3aSMark Bloch 3981e3f1ed1fSLeon Romanovsky err = mlx5_enable_eth(dev); 39828e6efa3aSMark Bloch if (err) 39838e6efa3aSMark Bloch goto cleanup; 39848e6efa3aSMark Bloch } 39858e6efa3aSMark Bloch 398616c1975fSMark Bloch return 0; 39878e6efa3aSMark Bloch cleanup: 39881e2b5a90SLeon Romanovsky mlx5_remove_netdev_notifier(dev, port_num); 39898e6efa3aSMark Bloch return err; 399045bded2cSKamal Heib } 39916aec21f6SHaggai Eran 39921e2b5a90SLeon Romanovsky static void mlx5_ib_roce_cleanup(struct mlx5_ib_dev *dev) 399316c1975fSMark Bloch { 399416c1975fSMark Bloch struct mlx5_core_dev *mdev = dev->mdev; 399516c1975fSMark Bloch enum rdma_link_layer ll; 399616c1975fSMark Bloch int port_type_cap; 39971fb7f897SMark Bloch u32 port_num; 39984a2da0b8SParav Pandit 399916c1975fSMark Bloch port_type_cap = MLX5_CAP_GEN(mdev, port_type); 400016c1975fSMark Bloch ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap); 400116c1975fSMark Bloch 400216c1975fSMark Bloch if (ll == IB_LINK_LAYER_ETHERNET) { 40031e2b5a90SLeon Romanovsky if (!dev->is_rep) 400416c1975fSMark Bloch mlx5_disable_eth(dev); 400516c1975fSMark Bloch 40061e2b5a90SLeon Romanovsky port_num = mlx5_core_native_port_num(dev->mdev) - 1; 40071e2b5a90SLeon Romanovsky mlx5_remove_netdev_notifier(dev, port_num); 400816c1975fSMark Bloch } 400916c1975fSMark Bloch } 401016c1975fSMark Bloch 401116c1975fSMark Bloch static int mlx5_ib_stage_cong_debugfs_init(struct mlx5_ib_dev *dev) 401216c1975fSMark Bloch { 401373eb8f03SGreg Kroah-Hartman mlx5_ib_init_cong_debugfs(dev, 4014a9e546e7SParav Pandit mlx5_core_native_port_num(dev->mdev) - 1); 401573eb8f03SGreg Kroah-Hartman return 0; 401616c1975fSMark Bloch } 401716c1975fSMark Bloch 401816c1975fSMark Bloch static void mlx5_ib_stage_cong_debugfs_cleanup(struct mlx5_ib_dev *dev) 401916c1975fSMark Bloch { 4020a9e546e7SParav Pandit mlx5_ib_cleanup_cong_debugfs(dev, 4021a9e546e7SParav Pandit mlx5_core_native_port_num(dev->mdev) - 1); 402216c1975fSMark Bloch } 402316c1975fSMark Bloch 402416c1975fSMark Bloch static int mlx5_ib_stage_uar_init(struct mlx5_ib_dev *dev) 402516c1975fSMark Bloch { 40265fe9dec0SEli Cohen dev->mdev->priv.uar = mlx5_get_uars_page(dev->mdev); 4027444261caSLeon Romanovsky return PTR_ERR_OR_ZERO(dev->mdev->priv.uar); 402816c1975fSMark Bloch } 402916c1975fSMark Bloch 403016c1975fSMark Bloch static void mlx5_ib_stage_uar_cleanup(struct mlx5_ib_dev *dev) 403116c1975fSMark Bloch { 403216c1975fSMark Bloch mlx5_put_uars_page(dev->mdev, dev->mdev->priv.uar); 403316c1975fSMark Bloch } 403416c1975fSMark Bloch 4035fb652d32SMark Bloch static int mlx5_ib_stage_bfrag_init(struct mlx5_ib_dev *dev) 403616c1975fSMark Bloch { 403716c1975fSMark Bloch int err; 40385fe9dec0SEli Cohen 40395fe9dec0SEli Cohen err = mlx5_alloc_bfreg(dev->mdev, &dev->bfreg, false, false); 40405fe9dec0SEli Cohen if (err) 404116c1975fSMark Bloch return err; 40425fe9dec0SEli Cohen 40435fe9dec0SEli Cohen err = mlx5_alloc_bfreg(dev->mdev, &dev->fp_bfreg, false, true); 40445fe9dec0SEli Cohen if (err) 40451c3aa6bdSMark Bloch mlx5_free_bfreg(dev->mdev, &dev->bfreg); 40465fe9dec0SEli Cohen 404716c1975fSMark Bloch return err; 404816c1975fSMark Bloch } 40490837e86aSMark Bloch 4050fb652d32SMark Bloch static void mlx5_ib_stage_bfrag_cleanup(struct mlx5_ib_dev *dev) 405116c1975fSMark Bloch { 405216c1975fSMark Bloch mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg); 405316c1975fSMark Bloch mlx5_free_bfreg(dev->mdev, &dev->bfreg); 405416c1975fSMark Bloch } 4055e126ba97SEli Cohen 4056fb652d32SMark Bloch static int mlx5_ib_stage_ib_reg_init(struct mlx5_ib_dev *dev) 405716c1975fSMark Bloch { 4058e349f858SJason Gunthorpe const char *name; 4059e349f858SJason Gunthorpe 40607c34ec19SAviv Heller if (!mlx5_lag_is_roce(dev->mdev)) 4061e349f858SJason Gunthorpe name = "mlx5_%d"; 4062e349f858SJason Gunthorpe else 4063e349f858SJason Gunthorpe name = "mlx5_bond_%d"; 4064e0477b34SJason Gunthorpe return ib_register_device(&dev->ib_dev, name, &dev->mdev->pdev->dev); 406516c1975fSMark Bloch } 406616c1975fSMark Bloch 4067fb652d32SMark Bloch static void mlx5_ib_stage_pre_ib_reg_umr_cleanup(struct mlx5_ib_dev *dev) 406842cea83fSMark Bloch { 40691e2b5a90SLeon Romanovsky int err; 40701e2b5a90SLeon Romanovsky 40711e2b5a90SLeon Romanovsky err = mlx5_mr_cache_cleanup(dev); 40721e2b5a90SLeon Romanovsky if (err) 40731e2b5a90SLeon Romanovsky mlx5_ib_warn(dev, "mr cache cleanup failed\n"); 40741e2b5a90SLeon Romanovsky 40751e2b5a90SLeon Romanovsky if (dev->umrc.qp) 40761e2b5a90SLeon Romanovsky mlx5_ib_destroy_qp(dev->umrc.qp, NULL); 40771e2b5a90SLeon Romanovsky if (dev->umrc.cq) 40781e2b5a90SLeon Romanovsky ib_free_cq(dev->umrc.cq); 40791e2b5a90SLeon Romanovsky if (dev->umrc.pd) 40801e2b5a90SLeon Romanovsky ib_dealloc_pd(dev->umrc.pd); 408142cea83fSMark Bloch } 408242cea83fSMark Bloch 4083fb652d32SMark Bloch static void mlx5_ib_stage_ib_reg_cleanup(struct mlx5_ib_dev *dev) 408416c1975fSMark Bloch { 408516c1975fSMark Bloch ib_unregister_device(&dev->ib_dev); 408616c1975fSMark Bloch } 408716c1975fSMark Bloch 40881e2b5a90SLeon Romanovsky enum { 40891e2b5a90SLeon Romanovsky MAX_UMR_WR = 128, 40901e2b5a90SLeon Romanovsky }; 40911e2b5a90SLeon Romanovsky 4092fb652d32SMark Bloch static int mlx5_ib_stage_post_ib_reg_umr_init(struct mlx5_ib_dev *dev) 409316c1975fSMark Bloch { 40941e2b5a90SLeon Romanovsky struct ib_qp_init_attr *init_attr = NULL; 40951e2b5a90SLeon Romanovsky struct ib_qp_attr *attr = NULL; 40961e2b5a90SLeon Romanovsky struct ib_pd *pd; 40971e2b5a90SLeon Romanovsky struct ib_cq *cq; 40981e2b5a90SLeon Romanovsky struct ib_qp *qp; 40991e2b5a90SLeon Romanovsky int ret; 41001e2b5a90SLeon Romanovsky 41011e2b5a90SLeon Romanovsky attr = kzalloc(sizeof(*attr), GFP_KERNEL); 41021e2b5a90SLeon Romanovsky init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL); 41031e2b5a90SLeon Romanovsky if (!attr || !init_attr) { 41041e2b5a90SLeon Romanovsky ret = -ENOMEM; 41051e2b5a90SLeon Romanovsky goto error_0; 41061e2b5a90SLeon Romanovsky } 41071e2b5a90SLeon Romanovsky 41081e2b5a90SLeon Romanovsky pd = ib_alloc_pd(&dev->ib_dev, 0); 41091e2b5a90SLeon Romanovsky if (IS_ERR(pd)) { 41101e2b5a90SLeon Romanovsky mlx5_ib_dbg(dev, "Couldn't create PD for sync UMR QP\n"); 41111e2b5a90SLeon Romanovsky ret = PTR_ERR(pd); 41121e2b5a90SLeon Romanovsky goto error_0; 41131e2b5a90SLeon Romanovsky } 41141e2b5a90SLeon Romanovsky 41151e2b5a90SLeon Romanovsky cq = ib_alloc_cq(&dev->ib_dev, NULL, 128, 0, IB_POLL_SOFTIRQ); 41161e2b5a90SLeon Romanovsky if (IS_ERR(cq)) { 41171e2b5a90SLeon Romanovsky mlx5_ib_dbg(dev, "Couldn't create CQ for sync UMR QP\n"); 41181e2b5a90SLeon Romanovsky ret = PTR_ERR(cq); 41191e2b5a90SLeon Romanovsky goto error_2; 41201e2b5a90SLeon Romanovsky } 41211e2b5a90SLeon Romanovsky 41221e2b5a90SLeon Romanovsky init_attr->send_cq = cq; 41231e2b5a90SLeon Romanovsky init_attr->recv_cq = cq; 41241e2b5a90SLeon Romanovsky init_attr->sq_sig_type = IB_SIGNAL_ALL_WR; 41251e2b5a90SLeon Romanovsky init_attr->cap.max_send_wr = MAX_UMR_WR; 41261e2b5a90SLeon Romanovsky init_attr->cap.max_send_sge = 1; 41271e2b5a90SLeon Romanovsky init_attr->qp_type = MLX5_IB_QPT_REG_UMR; 41281e2b5a90SLeon Romanovsky init_attr->port_num = 1; 41291e2b5a90SLeon Romanovsky qp = mlx5_ib_create_qp(pd, init_attr, NULL); 41301e2b5a90SLeon Romanovsky if (IS_ERR(qp)) { 41311e2b5a90SLeon Romanovsky mlx5_ib_dbg(dev, "Couldn't create sync UMR QP\n"); 41321e2b5a90SLeon Romanovsky ret = PTR_ERR(qp); 41331e2b5a90SLeon Romanovsky goto error_3; 41341e2b5a90SLeon Romanovsky } 41351e2b5a90SLeon Romanovsky qp->device = &dev->ib_dev; 41361e2b5a90SLeon Romanovsky qp->real_qp = qp; 41371e2b5a90SLeon Romanovsky qp->uobject = NULL; 41381e2b5a90SLeon Romanovsky qp->qp_type = MLX5_IB_QPT_REG_UMR; 41391e2b5a90SLeon Romanovsky qp->send_cq = init_attr->send_cq; 41401e2b5a90SLeon Romanovsky qp->recv_cq = init_attr->recv_cq; 41411e2b5a90SLeon Romanovsky 41421e2b5a90SLeon Romanovsky attr->qp_state = IB_QPS_INIT; 41431e2b5a90SLeon Romanovsky attr->port_num = 1; 41441e2b5a90SLeon Romanovsky ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_PKEY_INDEX | 41451e2b5a90SLeon Romanovsky IB_QP_PORT, NULL); 41461e2b5a90SLeon Romanovsky if (ret) { 41471e2b5a90SLeon Romanovsky mlx5_ib_dbg(dev, "Couldn't modify UMR QP\n"); 41481e2b5a90SLeon Romanovsky goto error_4; 41491e2b5a90SLeon Romanovsky } 41501e2b5a90SLeon Romanovsky 41511e2b5a90SLeon Romanovsky memset(attr, 0, sizeof(*attr)); 41521e2b5a90SLeon Romanovsky attr->qp_state = IB_QPS_RTR; 41531e2b5a90SLeon Romanovsky attr->path_mtu = IB_MTU_256; 41541e2b5a90SLeon Romanovsky 41551e2b5a90SLeon Romanovsky ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL); 41561e2b5a90SLeon Romanovsky if (ret) { 41571e2b5a90SLeon Romanovsky mlx5_ib_dbg(dev, "Couldn't modify umr QP to rtr\n"); 41581e2b5a90SLeon Romanovsky goto error_4; 41591e2b5a90SLeon Romanovsky } 41601e2b5a90SLeon Romanovsky 41611e2b5a90SLeon Romanovsky memset(attr, 0, sizeof(*attr)); 41621e2b5a90SLeon Romanovsky attr->qp_state = IB_QPS_RTS; 41631e2b5a90SLeon Romanovsky ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL); 41641e2b5a90SLeon Romanovsky if (ret) { 41651e2b5a90SLeon Romanovsky mlx5_ib_dbg(dev, "Couldn't modify umr QP to rts\n"); 41661e2b5a90SLeon Romanovsky goto error_4; 41671e2b5a90SLeon Romanovsky } 41681e2b5a90SLeon Romanovsky 41691e2b5a90SLeon Romanovsky dev->umrc.qp = qp; 41701e2b5a90SLeon Romanovsky dev->umrc.cq = cq; 41711e2b5a90SLeon Romanovsky dev->umrc.pd = pd; 41721e2b5a90SLeon Romanovsky 41731e2b5a90SLeon Romanovsky sema_init(&dev->umrc.sem, MAX_UMR_WR); 41741e2b5a90SLeon Romanovsky ret = mlx5_mr_cache_init(dev); 41751e2b5a90SLeon Romanovsky if (ret) { 41761e2b5a90SLeon Romanovsky mlx5_ib_warn(dev, "mr cache init failed %d\n", ret); 41771e2b5a90SLeon Romanovsky goto error_4; 41781e2b5a90SLeon Romanovsky } 41791e2b5a90SLeon Romanovsky 41801e2b5a90SLeon Romanovsky kfree(attr); 41811e2b5a90SLeon Romanovsky kfree(init_attr); 41821e2b5a90SLeon Romanovsky 41831e2b5a90SLeon Romanovsky return 0; 41841e2b5a90SLeon Romanovsky 41851e2b5a90SLeon Romanovsky error_4: 41861e2b5a90SLeon Romanovsky mlx5_ib_destroy_qp(qp, NULL); 41871e2b5a90SLeon Romanovsky dev->umrc.qp = NULL; 41881e2b5a90SLeon Romanovsky 41891e2b5a90SLeon Romanovsky error_3: 41901e2b5a90SLeon Romanovsky ib_free_cq(cq); 41911e2b5a90SLeon Romanovsky dev->umrc.cq = NULL; 41921e2b5a90SLeon Romanovsky 41931e2b5a90SLeon Romanovsky error_2: 41941e2b5a90SLeon Romanovsky ib_dealloc_pd(pd); 41951e2b5a90SLeon Romanovsky dev->umrc.pd = NULL; 41961e2b5a90SLeon Romanovsky 41971e2b5a90SLeon Romanovsky error_0: 41981e2b5a90SLeon Romanovsky kfree(attr); 41991e2b5a90SLeon Romanovsky kfree(init_attr); 42001e2b5a90SLeon Romanovsky return ret; 420116c1975fSMark Bloch } 420216c1975fSMark Bloch 420316c1975fSMark Bloch static int mlx5_ib_stage_delay_drop_init(struct mlx5_ib_dev *dev) 420416c1975fSMark Bloch { 42051e2b5a90SLeon Romanovsky struct dentry *root; 420603404e8aSMaor Gottlieb 42071e2b5a90SLeon Romanovsky if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP)) 42081e2b5a90SLeon Romanovsky return 0; 42091e2b5a90SLeon Romanovsky 42101e2b5a90SLeon Romanovsky mutex_init(&dev->delay_drop.lock); 42111e2b5a90SLeon Romanovsky dev->delay_drop.dev = dev; 42121e2b5a90SLeon Romanovsky dev->delay_drop.activate = false; 42131e2b5a90SLeon Romanovsky dev->delay_drop.timeout = MLX5_MAX_DELAY_DROP_TIMEOUT_MS * 1000; 42141e2b5a90SLeon Romanovsky INIT_WORK(&dev->delay_drop.delay_drop_work, delay_drop_handler); 42151e2b5a90SLeon Romanovsky atomic_set(&dev->delay_drop.rqs_cnt, 0); 42161e2b5a90SLeon Romanovsky atomic_set(&dev->delay_drop.events_cnt, 0); 42171e2b5a90SLeon Romanovsky 42181e2b5a90SLeon Romanovsky if (!mlx5_debugfs_root) 42191e2b5a90SLeon Romanovsky return 0; 42201e2b5a90SLeon Romanovsky 42211e2b5a90SLeon Romanovsky root = debugfs_create_dir("delay_drop", dev->mdev->priv.dbg_root); 42221e2b5a90SLeon Romanovsky dev->delay_drop.dir_debugfs = root; 42231e2b5a90SLeon Romanovsky 42241e2b5a90SLeon Romanovsky debugfs_create_atomic_t("num_timeout_events", 0400, root, 42251e2b5a90SLeon Romanovsky &dev->delay_drop.events_cnt); 42261e2b5a90SLeon Romanovsky debugfs_create_atomic_t("num_rqs", 0400, root, 42271e2b5a90SLeon Romanovsky &dev->delay_drop.rqs_cnt); 42281e2b5a90SLeon Romanovsky debugfs_create_file("timeout", 0600, root, &dev->delay_drop, 42291e2b5a90SLeon Romanovsky &fops_delay_drop_timeout); 423016c1975fSMark Bloch return 0; 423116c1975fSMark Bloch } 423216c1975fSMark Bloch 423316c1975fSMark Bloch static void mlx5_ib_stage_delay_drop_cleanup(struct mlx5_ib_dev *dev) 423416c1975fSMark Bloch { 42351e2b5a90SLeon Romanovsky if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP)) 42361e2b5a90SLeon Romanovsky return; 42371e2b5a90SLeon Romanovsky 42381e2b5a90SLeon Romanovsky cancel_work_sync(&dev->delay_drop.delay_drop_work); 42391e2b5a90SLeon Romanovsky if (!dev->delay_drop.dir_debugfs) 42401e2b5a90SLeon Romanovsky return; 42411e2b5a90SLeon Romanovsky 42421e2b5a90SLeon Romanovsky debugfs_remove_recursive(dev->delay_drop.dir_debugfs); 42431e2b5a90SLeon Romanovsky dev->delay_drop.dir_debugfs = NULL; 424416c1975fSMark Bloch } 424516c1975fSMark Bloch 4246df097a27SSaeed Mahameed static int mlx5_ib_stage_dev_notifier_init(struct mlx5_ib_dev *dev) 4247df097a27SSaeed Mahameed { 4248df097a27SSaeed Mahameed dev->mdev_events.notifier_call = mlx5_ib_event; 4249df097a27SSaeed Mahameed mlx5_notifier_register(dev->mdev, &dev->mdev_events); 4250df097a27SSaeed Mahameed return 0; 4251df097a27SSaeed Mahameed } 4252df097a27SSaeed Mahameed 4253df097a27SSaeed Mahameed static void mlx5_ib_stage_dev_notifier_cleanup(struct mlx5_ib_dev *dev) 4254df097a27SSaeed Mahameed { 4255df097a27SSaeed Mahameed mlx5_notifier_unregister(dev->mdev, &dev->mdev_events); 4256df097a27SSaeed Mahameed } 4257df097a27SSaeed Mahameed 4258b5ca15adSMark Bloch void __mlx5_ib_remove(struct mlx5_ib_dev *dev, 425916c1975fSMark Bloch const struct mlx5_ib_profile *profile, 426016c1975fSMark Bloch int stage) 426116c1975fSMark Bloch { 42624cca96a8SParav Pandit dev->ib_active = false; 42634cca96a8SParav Pandit 426416c1975fSMark Bloch /* Number of stages to cleanup */ 426516c1975fSMark Bloch while (stage) { 426616c1975fSMark Bloch stage--; 426716c1975fSMark Bloch if (profile->stage[stage].cleanup) 426816c1975fSMark Bloch profile->stage[stage].cleanup(dev); 426916c1975fSMark Bloch } 42704a6dc855SMark Bloch 4271da796ccbSMark Bloch kfree(dev->port); 42724a6dc855SMark Bloch ib_dealloc_device(&dev->ib_dev); 427316c1975fSMark Bloch } 427416c1975fSMark Bloch 427593f82444SLeon Romanovsky int __mlx5_ib_add(struct mlx5_ib_dev *dev, 427616c1975fSMark Bloch const struct mlx5_ib_profile *profile) 427716c1975fSMark Bloch { 427816c1975fSMark Bloch int err; 427916c1975fSMark Bloch int i; 428016c1975fSMark Bloch 42818d93efb8SMark Bloch dev->profile = profile; 42828d93efb8SMark Bloch 428316c1975fSMark Bloch for (i = 0; i < MLX5_IB_STAGE_MAX; i++) { 428416c1975fSMark Bloch if (profile->stage[i].init) { 428516c1975fSMark Bloch err = profile->stage[i].init(dev); 428616c1975fSMark Bloch if (err) 428716c1975fSMark Bloch goto err_out; 428816c1975fSMark Bloch } 428916c1975fSMark Bloch } 429016c1975fSMark Bloch 4291e126ba97SEli Cohen dev->ib_active = true; 429293f82444SLeon Romanovsky return 0; 4293e126ba97SEli Cohen 429416c1975fSMark Bloch err_out: 429593f82444SLeon Romanovsky /* Clean up stages which were initialized */ 429693f82444SLeon Romanovsky while (i) { 429793f82444SLeon Romanovsky i--; 429893f82444SLeon Romanovsky if (profile->stage[i].cleanup) 429993f82444SLeon Romanovsky profile->stage[i].cleanup(dev); 430093f82444SLeon Romanovsky } 430193f82444SLeon Romanovsky return -ENOMEM; 4302e126ba97SEli Cohen } 4303e126ba97SEli Cohen 430416c1975fSMark Bloch static const struct mlx5_ib_profile pf_profile = { 430516c1975fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_INIT, 430616c1975fSMark Bloch mlx5_ib_stage_init_init, 430716c1975fSMark Bloch mlx5_ib_stage_init_cleanup), 4308f7c4ffdaSLeon Romanovsky STAGE_CREATE(MLX5_IB_STAGE_FS, 4309f7c4ffdaSLeon Romanovsky mlx5_ib_fs_init, 4310f7c4ffdaSLeon Romanovsky mlx5_ib_fs_cleanup), 431116c1975fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_CAPS, 431216c1975fSMark Bloch mlx5_ib_stage_caps_init, 4313f164be8cSYishai Hadas mlx5_ib_stage_caps_cleanup), 43148e6efa3aSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB, 43158e6efa3aSMark Bloch mlx5_ib_stage_non_default_cb, 43168e6efa3aSMark Bloch NULL), 431716c1975fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_ROCE, 43181e2b5a90SLeon Romanovsky mlx5_ib_roce_init, 43191e2b5a90SLeon Romanovsky mlx5_ib_roce_cleanup), 4320333fbaa0SLeon Romanovsky STAGE_CREATE(MLX5_IB_STAGE_QP, 4321333fbaa0SLeon Romanovsky mlx5_init_qp_table, 4322333fbaa0SLeon Romanovsky mlx5_cleanup_qp_table), 4323f3da6577SLeon Romanovsky STAGE_CREATE(MLX5_IB_STAGE_SRQ, 4324f3da6577SLeon Romanovsky mlx5_init_srq_table, 4325f3da6577SLeon Romanovsky mlx5_cleanup_srq_table), 432616c1975fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES, 43271e2b5a90SLeon Romanovsky mlx5_ib_dev_res_init, 43281e2b5a90SLeon Romanovsky mlx5_ib_dev_res_cleanup), 4329df097a27SSaeed Mahameed STAGE_CREATE(MLX5_IB_STAGE_DEVICE_NOTIFIER, 4330df097a27SSaeed Mahameed mlx5_ib_stage_dev_notifier_init, 4331df097a27SSaeed Mahameed mlx5_ib_stage_dev_notifier_cleanup), 433216c1975fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_ODP, 43331e2b5a90SLeon Romanovsky mlx5_ib_odp_init_one, 43341e2b5a90SLeon Romanovsky mlx5_ib_odp_cleanup_one), 433516c1975fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_COUNTERS, 433664825827SLeon Romanovsky mlx5_ib_counters_init, 433764825827SLeon Romanovsky mlx5_ib_counters_cleanup), 433816c1975fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_CONG_DEBUGFS, 433916c1975fSMark Bloch mlx5_ib_stage_cong_debugfs_init, 434016c1975fSMark Bloch mlx5_ib_stage_cong_debugfs_cleanup), 434116c1975fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_UAR, 434216c1975fSMark Bloch mlx5_ib_stage_uar_init, 434316c1975fSMark Bloch mlx5_ib_stage_uar_cleanup), 434416c1975fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_BFREG, 434516c1975fSMark Bloch mlx5_ib_stage_bfrag_init, 434616c1975fSMark Bloch mlx5_ib_stage_bfrag_cleanup), 434742cea83fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR, 434842cea83fSMark Bloch NULL, 434942cea83fSMark Bloch mlx5_ib_stage_pre_ib_reg_umr_cleanup), 435081773ce5SLeon Romanovsky STAGE_CREATE(MLX5_IB_STAGE_WHITELIST_UID, 4351d8b7515eSLeon Romanovsky mlx5_ib_devx_init, 4352d8b7515eSLeon Romanovsky mlx5_ib_devx_cleanup), 435316c1975fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_IB_REG, 435416c1975fSMark Bloch mlx5_ib_stage_ib_reg_init, 435516c1975fSMark Bloch mlx5_ib_stage_ib_reg_cleanup), 435642cea83fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR, 435742cea83fSMark Bloch mlx5_ib_stage_post_ib_reg_umr_init, 435842cea83fSMark Bloch NULL), 435916c1975fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_DELAY_DROP, 436016c1975fSMark Bloch mlx5_ib_stage_delay_drop_init, 436116c1975fSMark Bloch mlx5_ib_stage_delay_drop_cleanup), 4362b572ebe6SLeon Romanovsky STAGE_CREATE(MLX5_IB_STAGE_RESTRACK, 4363b572ebe6SLeon Romanovsky mlx5_ib_restrack_init, 4364b572ebe6SLeon Romanovsky NULL), 436516c1975fSMark Bloch }; 436616c1975fSMark Bloch 4367b5a498baSMichael Guralnik const struct mlx5_ib_profile raw_eth_profile = { 4368b5ca15adSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_INIT, 4369b5ca15adSMark Bloch mlx5_ib_stage_init_init, 4370b5ca15adSMark Bloch mlx5_ib_stage_init_cleanup), 4371f7c4ffdaSLeon Romanovsky STAGE_CREATE(MLX5_IB_STAGE_FS, 4372f7c4ffdaSLeon Romanovsky mlx5_ib_fs_init, 4373f7c4ffdaSLeon Romanovsky mlx5_ib_fs_cleanup), 4374b5ca15adSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_CAPS, 4375b5ca15adSMark Bloch mlx5_ib_stage_caps_init, 4376f164be8cSYishai Hadas mlx5_ib_stage_caps_cleanup), 4377b5ca15adSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB, 4378b5a498baSMichael Guralnik mlx5_ib_stage_raw_eth_non_default_cb, 4379b5ca15adSMark Bloch NULL), 4380b5ca15adSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_ROCE, 43811e2b5a90SLeon Romanovsky mlx5_ib_roce_init, 43821e2b5a90SLeon Romanovsky mlx5_ib_roce_cleanup), 4383333fbaa0SLeon Romanovsky STAGE_CREATE(MLX5_IB_STAGE_QP, 4384333fbaa0SLeon Romanovsky mlx5_init_qp_table, 4385333fbaa0SLeon Romanovsky mlx5_cleanup_qp_table), 4386f3da6577SLeon Romanovsky STAGE_CREATE(MLX5_IB_STAGE_SRQ, 4387f3da6577SLeon Romanovsky mlx5_init_srq_table, 4388f3da6577SLeon Romanovsky mlx5_cleanup_srq_table), 4389b5ca15adSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES, 43901e2b5a90SLeon Romanovsky mlx5_ib_dev_res_init, 43911e2b5a90SLeon Romanovsky mlx5_ib_dev_res_cleanup), 4392df097a27SSaeed Mahameed STAGE_CREATE(MLX5_IB_STAGE_DEVICE_NOTIFIER, 4393df097a27SSaeed Mahameed mlx5_ib_stage_dev_notifier_init, 4394df097a27SSaeed Mahameed mlx5_ib_stage_dev_notifier_cleanup), 4395b5ca15adSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_COUNTERS, 439664825827SLeon Romanovsky mlx5_ib_counters_init, 439764825827SLeon Romanovsky mlx5_ib_counters_cleanup), 439879db784eSParav Pandit STAGE_CREATE(MLX5_IB_STAGE_CONG_DEBUGFS, 439979db784eSParav Pandit mlx5_ib_stage_cong_debugfs_init, 440079db784eSParav Pandit mlx5_ib_stage_cong_debugfs_cleanup), 4401b5ca15adSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_UAR, 4402b5ca15adSMark Bloch mlx5_ib_stage_uar_init, 4403b5ca15adSMark Bloch mlx5_ib_stage_uar_cleanup), 4404b5ca15adSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_BFREG, 4405b5ca15adSMark Bloch mlx5_ib_stage_bfrag_init, 4406b5ca15adSMark Bloch mlx5_ib_stage_bfrag_cleanup), 440703fe2debSDavid S. Miller STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR, 440803fe2debSDavid S. Miller NULL, 440903fe2debSDavid S. Miller mlx5_ib_stage_pre_ib_reg_umr_cleanup), 44107f575103SMark Bloch STAGE_CREATE(MLX5_IB_STAGE_WHITELIST_UID, 4411d8b7515eSLeon Romanovsky mlx5_ib_devx_init, 4412d8b7515eSLeon Romanovsky mlx5_ib_devx_cleanup), 4413b5ca15adSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_IB_REG, 4414b5ca15adSMark Bloch mlx5_ib_stage_ib_reg_init, 4415b5ca15adSMark Bloch mlx5_ib_stage_ib_reg_cleanup), 441603fe2debSDavid S. Miller STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR, 441703fe2debSDavid S. Miller mlx5_ib_stage_post_ib_reg_umr_init, 441803fe2debSDavid S. Miller NULL), 4419b572ebe6SLeon Romanovsky STAGE_CREATE(MLX5_IB_STAGE_RESTRACK, 4420b572ebe6SLeon Romanovsky mlx5_ib_restrack_init, 4421b572ebe6SLeon Romanovsky NULL), 4422b5ca15adSMark Bloch }; 4423b5ca15adSMark Bloch 442493f82444SLeon Romanovsky static int mlx5r_mp_probe(struct auxiliary_device *adev, 442593f82444SLeon Romanovsky const struct auxiliary_device_id *id) 442632f69e4bSDaniel Jurgens { 442793f82444SLeon Romanovsky struct mlx5_adev *idev = container_of(adev, struct mlx5_adev, adev); 442893f82444SLeon Romanovsky struct mlx5_core_dev *mdev = idev->mdev; 442932f69e4bSDaniel Jurgens struct mlx5_ib_multiport_info *mpi; 443032f69e4bSDaniel Jurgens struct mlx5_ib_dev *dev; 443132f69e4bSDaniel Jurgens bool bound = false; 443232f69e4bSDaniel Jurgens int err; 443332f69e4bSDaniel Jurgens 443432f69e4bSDaniel Jurgens mpi = kzalloc(sizeof(*mpi), GFP_KERNEL); 443532f69e4bSDaniel Jurgens if (!mpi) 443693f82444SLeon Romanovsky return -ENOMEM; 443732f69e4bSDaniel Jurgens 443832f69e4bSDaniel Jurgens mpi->mdev = mdev; 443932f69e4bSDaniel Jurgens err = mlx5_query_nic_vport_system_image_guid(mdev, 444032f69e4bSDaniel Jurgens &mpi->sys_image_guid); 444132f69e4bSDaniel Jurgens if (err) { 444232f69e4bSDaniel Jurgens kfree(mpi); 444393f82444SLeon Romanovsky return err; 444432f69e4bSDaniel Jurgens } 444532f69e4bSDaniel Jurgens 444632f69e4bSDaniel Jurgens mutex_lock(&mlx5_ib_multiport_mutex); 444732f69e4bSDaniel Jurgens list_for_each_entry(dev, &mlx5_ib_dev_list, ib_dev_list) { 444832f69e4bSDaniel Jurgens if (dev->sys_image_guid == mpi->sys_image_guid) 444932f69e4bSDaniel Jurgens bound = mlx5_ib_bind_slave_port(dev, mpi); 445032f69e4bSDaniel Jurgens 445132f69e4bSDaniel Jurgens if (bound) { 445232f69e4bSDaniel Jurgens rdma_roce_rescan_device(&dev->ib_dev); 445397f30d32SMaor Gottlieb mpi->ibdev->ib_active = true; 445432f69e4bSDaniel Jurgens break; 445532f69e4bSDaniel Jurgens } 445632f69e4bSDaniel Jurgens } 445732f69e4bSDaniel Jurgens 445832f69e4bSDaniel Jurgens if (!bound) { 445932f69e4bSDaniel Jurgens list_add_tail(&mpi->list, &mlx5_ib_unaffiliated_port_list); 4460c42260f1SVu Pham dev_dbg(mdev->device, 4461c42260f1SVu Pham "no suitable IB device found to bind to, added to unaffiliated list.\n"); 446232f69e4bSDaniel Jurgens } 446332f69e4bSDaniel Jurgens mutex_unlock(&mlx5_ib_multiport_mutex); 446432f69e4bSDaniel Jurgens 446593f82444SLeon Romanovsky dev_set_drvdata(&adev->dev, mpi); 446693f82444SLeon Romanovsky return 0; 446732f69e4bSDaniel Jurgens } 446832f69e4bSDaniel Jurgens 446993f82444SLeon Romanovsky static void mlx5r_mp_remove(struct auxiliary_device *adev) 447016c1975fSMark Bloch { 447193f82444SLeon Romanovsky struct mlx5_ib_multiport_info *mpi; 447293f82444SLeon Romanovsky 447393f82444SLeon Romanovsky mpi = dev_get_drvdata(&adev->dev); 447493f82444SLeon Romanovsky mutex_lock(&mlx5_ib_multiport_mutex); 447593f82444SLeon Romanovsky if (mpi->ibdev) 447693f82444SLeon Romanovsky mlx5_ib_unbind_slave_port(mpi->ibdev, mpi); 447793f82444SLeon Romanovsky list_del(&mpi->list); 447893f82444SLeon Romanovsky mutex_unlock(&mlx5_ib_multiport_mutex); 447993f82444SLeon Romanovsky kfree(mpi); 448093f82444SLeon Romanovsky } 448193f82444SLeon Romanovsky 448293f82444SLeon Romanovsky static int mlx5r_probe(struct auxiliary_device *adev, 448393f82444SLeon Romanovsky const struct auxiliary_device_id *id) 448493f82444SLeon Romanovsky { 448593f82444SLeon Romanovsky struct mlx5_adev *idev = container_of(adev, struct mlx5_adev, adev); 448693f82444SLeon Romanovsky struct mlx5_core_dev *mdev = idev->mdev; 448794de879cSMichael Guralnik const struct mlx5_ib_profile *profile; 448893f82444SLeon Romanovsky int port_type_cap, num_ports, ret; 448932f69e4bSDaniel Jurgens enum rdma_link_layer ll; 4490b5ca15adSMark Bloch struct mlx5_ib_dev *dev; 4491f0666f1fSBodong Wang 449232f69e4bSDaniel Jurgens port_type_cap = MLX5_CAP_GEN(mdev, port_type); 449332f69e4bSDaniel Jurgens ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap); 449432f69e4bSDaniel Jurgens 4495da796ccbSMark Bloch num_ports = max(MLX5_CAP_GEN(mdev, num_ports), 4496da796ccbSMark Bloch MLX5_CAP_GEN(mdev, num_vhca_ports)); 4497459cc69fSLeon Romanovsky dev = ib_alloc_device(mlx5_ib_dev, ib_dev); 4498b5ca15adSMark Bloch if (!dev) 449993f82444SLeon Romanovsky return -ENOMEM; 4500da796ccbSMark Bloch dev->port = kcalloc(num_ports, sizeof(*dev->port), 4501da796ccbSMark Bloch GFP_KERNEL); 4502da796ccbSMark Bloch if (!dev->port) { 4503a5c9c299SParav Pandit ib_dealloc_device(&dev->ib_dev); 450493f82444SLeon Romanovsky return -ENOMEM; 4505da796ccbSMark Bloch } 4506b5ca15adSMark Bloch 4507b5ca15adSMark Bloch dev->mdev = mdev; 4508da796ccbSMark Bloch dev->num_ports = num_ports; 4509b5ca15adSMark Bloch 45107852546fSMaor Gottlieb if (ll == IB_LINK_LAYER_ETHERNET && !mlx5_is_roce_init_enabled(mdev)) 451194de879cSMichael Guralnik profile = &raw_eth_profile; 451294de879cSMichael Guralnik else 451394de879cSMichael Guralnik profile = &pf_profile; 451494de879cSMichael Guralnik 451593f82444SLeon Romanovsky ret = __mlx5_ib_add(dev, profile); 451693f82444SLeon Romanovsky if (ret) { 451793f82444SLeon Romanovsky kfree(dev->port); 451893f82444SLeon Romanovsky ib_dealloc_device(&dev->ib_dev); 451993f82444SLeon Romanovsky return ret; 452016c1975fSMark Bloch } 452116c1975fSMark Bloch 452293f82444SLeon Romanovsky dev_set_drvdata(&adev->dev, dev); 452393f82444SLeon Romanovsky return 0; 452493f82444SLeon Romanovsky } 452593f82444SLeon Romanovsky 452693f82444SLeon Romanovsky static void mlx5r_remove(struct auxiliary_device *adev) 4527e126ba97SEli Cohen { 452832f69e4bSDaniel Jurgens struct mlx5_ib_dev *dev; 45296aec21f6SHaggai Eran 453093f82444SLeon Romanovsky dev = dev_get_drvdata(&adev->dev); 453116c1975fSMark Bloch __mlx5_ib_remove(dev, dev->profile, MLX5_IB_STAGE_MAX); 4532e126ba97SEli Cohen } 4533e126ba97SEli Cohen 453493f82444SLeon Romanovsky static const struct auxiliary_device_id mlx5r_mp_id_table[] = { 453593f82444SLeon Romanovsky { .name = MLX5_ADEV_NAME ".multiport", }, 453693f82444SLeon Romanovsky {}, 453793f82444SLeon Romanovsky }; 453893f82444SLeon Romanovsky 453993f82444SLeon Romanovsky static const struct auxiliary_device_id mlx5r_id_table[] = { 454093f82444SLeon Romanovsky { .name = MLX5_ADEV_NAME ".rdma", }, 454193f82444SLeon Romanovsky {}, 454293f82444SLeon Romanovsky }; 454393f82444SLeon Romanovsky 454493f82444SLeon Romanovsky MODULE_DEVICE_TABLE(auxiliary, mlx5r_mp_id_table); 454593f82444SLeon Romanovsky MODULE_DEVICE_TABLE(auxiliary, mlx5r_id_table); 454693f82444SLeon Romanovsky 454793f82444SLeon Romanovsky static struct auxiliary_driver mlx5r_mp_driver = { 454893f82444SLeon Romanovsky .name = "multiport", 454993f82444SLeon Romanovsky .probe = mlx5r_mp_probe, 455093f82444SLeon Romanovsky .remove = mlx5r_mp_remove, 455193f82444SLeon Romanovsky .id_table = mlx5r_mp_id_table, 455293f82444SLeon Romanovsky }; 455393f82444SLeon Romanovsky 455493f82444SLeon Romanovsky static struct auxiliary_driver mlx5r_driver = { 455593f82444SLeon Romanovsky .name = "rdma", 455693f82444SLeon Romanovsky .probe = mlx5r_probe, 455793f82444SLeon Romanovsky .remove = mlx5r_remove, 455893f82444SLeon Romanovsky .id_table = mlx5r_id_table, 4559e126ba97SEli Cohen }; 4560e126ba97SEli Cohen 4561e126ba97SEli Cohen static int __init mlx5_ib_init(void) 4562e126ba97SEli Cohen { 456393f82444SLeon Romanovsky int ret; 45646aec21f6SHaggai Eran 45658010d74bSJason Gunthorpe xlt_emergency_page = (void *)__get_free_page(GFP_KERNEL); 4566c44ef998SIlya Lesokhin if (!xlt_emergency_page) 4567d69a24e0SDaniel Jurgens return -ENOMEM; 4568d69a24e0SDaniel Jurgens 4569c44ef998SIlya Lesokhin mlx5_ib_event_wq = alloc_ordered_workqueue("mlx5_ib_event_wq", 0); 4570c44ef998SIlya Lesokhin if (!mlx5_ib_event_wq) { 45718010d74bSJason Gunthorpe free_page((unsigned long)xlt_emergency_page); 4572c44ef998SIlya Lesokhin return -ENOMEM; 4573c44ef998SIlya Lesokhin } 4574c44ef998SIlya Lesokhin 457581713d37SArtemy Kovalyov mlx5_ib_odp_init(); 457693f82444SLeon Romanovsky ret = mlx5r_rep_init(); 457793f82444SLeon Romanovsky if (ret) 457893f82444SLeon Romanovsky goto rep_err; 457993f82444SLeon Romanovsky ret = auxiliary_driver_register(&mlx5r_mp_driver); 458093f82444SLeon Romanovsky if (ret) 458193f82444SLeon Romanovsky goto mp_err; 458293f82444SLeon Romanovsky ret = auxiliary_driver_register(&mlx5r_driver); 458393f82444SLeon Romanovsky if (ret) 458493f82444SLeon Romanovsky goto drv_err; 458593f82444SLeon Romanovsky return 0; 45869603b61dSJack Morgenstein 458793f82444SLeon Romanovsky drv_err: 458893f82444SLeon Romanovsky auxiliary_driver_unregister(&mlx5r_mp_driver); 458993f82444SLeon Romanovsky mp_err: 459093f82444SLeon Romanovsky mlx5r_rep_cleanup(); 459193f82444SLeon Romanovsky rep_err: 459293f82444SLeon Romanovsky destroy_workqueue(mlx5_ib_event_wq); 459393f82444SLeon Romanovsky free_page((unsigned long)xlt_emergency_page); 459493f82444SLeon Romanovsky return ret; 4595e126ba97SEli Cohen } 4596e126ba97SEli Cohen 4597e126ba97SEli Cohen static void __exit mlx5_ib_cleanup(void) 4598e126ba97SEli Cohen { 459993f82444SLeon Romanovsky auxiliary_driver_unregister(&mlx5r_driver); 460093f82444SLeon Romanovsky auxiliary_driver_unregister(&mlx5r_mp_driver); 460193f82444SLeon Romanovsky mlx5r_rep_cleanup(); 460293f82444SLeon Romanovsky 4603d69a24e0SDaniel Jurgens destroy_workqueue(mlx5_ib_event_wq); 46048010d74bSJason Gunthorpe free_page((unsigned long)xlt_emergency_page); 4605e126ba97SEli Cohen } 4606e126ba97SEli Cohen 4607e126ba97SEli Cohen module_init(mlx5_ib_init); 4608e126ba97SEli Cohen module_exit(mlx5_ib_cleanup); 4609