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> 27fdd350feSPatrisious Haddad #include <linux/mlx5/driver.h> 287c2344c3SMaor Gottlieb #include <linux/list.h> 29e126ba97SEli Cohen #include <rdma/ib_smi.h> 30ca7ef7adSDaisuke Matsuda #include <rdma/ib_umem_odp.h> 31cfc1a89eSMaor Gottlieb #include <rdma/lag.h> 32038d2ef8SMaor Gottlieb #include <linux/in.h> 33038d2ef8SMaor Gottlieb #include <linux/etherdevice.h> 34e126ba97SEli Cohen #include "mlx5_ib.h" 35fc385b7aSMark Bloch #include "ib_rep.h" 36e1f24a79SParav Pandit #include "cmd.h" 37d8b7515eSLeon Romanovsky #include "devx.h" 38831df883SMaor Gottlieb #include "dm.h" 39f7c4ffdaSLeon Romanovsky #include "fs.h" 40f3da6577SLeon Romanovsky #include "srq.h" 41333fbaa0SLeon Romanovsky #include "qp.h" 42029e88fdSLeon Romanovsky #include "wr.h" 43b572ebe6SLeon Romanovsky #include "restrack.h" 4464825827SLeon Romanovsky #include "counters.h" 4504876c12SAharon Landau #include "umr.h" 468c84660bSMatan Barak #include <rdma/uverbs_std_types.h> 472904bb37SYishai Hadas #include <rdma/uverbs_ioctl.h> 48c6475a0bSAviad Yehezkel #include <rdma/mlx5_user_ioctl_verbs.h> 49c6475a0bSAviad Yehezkel #include <rdma/mlx5_user_ioctl_cmds.h> 50758ce14aSPatrisious Haddad #include "macsec.h" 518c84660bSMatan Barak 528c84660bSMatan Barak #define UVERBS_MODULE_NAME mlx5_ib 538c84660bSMatan Barak #include <rdma/uverbs_named_ioctl.h> 54e126ba97SEli Cohen 55e126ba97SEli Cohen MODULE_AUTHOR("Eli Cohen <eli@mellanox.com>"); 56daeee976SShay Drory MODULE_DESCRIPTION("Mellanox 5th generation network adapters (ConnectX series) IB driver"); 57e126ba97SEli Cohen MODULE_LICENSE("Dual BSD/GPL"); 58e126ba97SEli Cohen 59d69a24e0SDaniel Jurgens struct mlx5_ib_event_work { 60d69a24e0SDaniel Jurgens struct work_struct work; 61df097a27SSaeed Mahameed union { 62df097a27SSaeed Mahameed struct mlx5_ib_dev *dev; 63df097a27SSaeed Mahameed struct mlx5_ib_multiport_info *mpi; 64df097a27SSaeed Mahameed }; 65df097a27SSaeed Mahameed bool is_slave; 66134e9349SSaeed Mahameed unsigned int event; 67df097a27SSaeed Mahameed void *param; 68d69a24e0SDaniel Jurgens }; 69d69a24e0SDaniel Jurgens 70da7525d2SEran Ben Elisha enum { 71da7525d2SEran Ben Elisha MLX5_ATOMIC_SIZE_QP_8BYTES = 1 << 3, 72da7525d2SEran Ben Elisha }; 731b5daf11SMajd Dibbiny 74d69a24e0SDaniel Jurgens static struct workqueue_struct *mlx5_ib_event_wq; 7532f69e4bSDaniel Jurgens static LIST_HEAD(mlx5_ib_unaffiliated_port_list); 7632f69e4bSDaniel Jurgens static LIST_HEAD(mlx5_ib_dev_list); 7732f69e4bSDaniel Jurgens /* 7832f69e4bSDaniel Jurgens * This mutex should be held when accessing either of the above lists 7932f69e4bSDaniel Jurgens */ 8032f69e4bSDaniel Jurgens static DEFINE_MUTEX(mlx5_ib_multiport_mutex); 8132f69e4bSDaniel Jurgens 8232f69e4bSDaniel Jurgens struct mlx5_ib_dev *mlx5_ib_get_ibdev_from_mpi(struct mlx5_ib_multiport_info *mpi) 8332f69e4bSDaniel Jurgens { 8432f69e4bSDaniel Jurgens struct mlx5_ib_dev *dev; 8532f69e4bSDaniel Jurgens 8632f69e4bSDaniel Jurgens mutex_lock(&mlx5_ib_multiport_mutex); 8732f69e4bSDaniel Jurgens dev = mpi->ibdev; 8832f69e4bSDaniel Jurgens mutex_unlock(&mlx5_ib_multiport_mutex); 8932f69e4bSDaniel Jurgens return dev; 9032f69e4bSDaniel Jurgens } 9132f69e4bSDaniel Jurgens 921b5daf11SMajd Dibbiny static enum rdma_link_layer 93ebd61f68SAchiad Shochat mlx5_port_type_cap_to_rdma_ll(int port_type_cap) 941b5daf11SMajd Dibbiny { 95ebd61f68SAchiad Shochat switch (port_type_cap) { 961b5daf11SMajd Dibbiny case MLX5_CAP_PORT_TYPE_IB: 971b5daf11SMajd Dibbiny return IB_LINK_LAYER_INFINIBAND; 981b5daf11SMajd Dibbiny case MLX5_CAP_PORT_TYPE_ETH: 991b5daf11SMajd Dibbiny return IB_LINK_LAYER_ETHERNET; 1001b5daf11SMajd Dibbiny default: 1011b5daf11SMajd Dibbiny return IB_LINK_LAYER_UNSPECIFIED; 1021b5daf11SMajd Dibbiny } 1031b5daf11SMajd Dibbiny } 1041b5daf11SMajd Dibbiny 105ebd61f68SAchiad Shochat static enum rdma_link_layer 1061fb7f897SMark Bloch mlx5_ib_port_link_layer(struct ib_device *device, u32 port_num) 107ebd61f68SAchiad Shochat { 108ebd61f68SAchiad Shochat struct mlx5_ib_dev *dev = to_mdev(device); 109ebd61f68SAchiad Shochat int port_type_cap = MLX5_CAP_GEN(dev->mdev, port_type); 110ebd61f68SAchiad Shochat 111ebd61f68SAchiad Shochat return mlx5_port_type_cap_to_rdma_ll(port_type_cap); 112ebd61f68SAchiad Shochat } 113ebd61f68SAchiad Shochat 114fd65f1b8SMoni Shoua static int get_port_state(struct ib_device *ibdev, 1151fb7f897SMark Bloch u32 port_num, 116fd65f1b8SMoni Shoua enum ib_port_state *state) 117fd65f1b8SMoni Shoua { 118fd65f1b8SMoni Shoua struct ib_port_attr attr; 119fd65f1b8SMoni Shoua int ret; 120fd65f1b8SMoni Shoua 121fd65f1b8SMoni Shoua memset(&attr, 0, sizeof(attr)); 1223023a1e9SKamal Heib ret = ibdev->ops.query_port(ibdev, port_num, &attr); 123fd65f1b8SMoni Shoua if (!ret) 124fd65f1b8SMoni Shoua *state = attr.state; 125fd65f1b8SMoni Shoua return ret; 126fd65f1b8SMoni Shoua } 127fd65f1b8SMoni Shoua 12835b0aa67SMark Bloch static struct mlx5_roce *mlx5_get_rep_roce(struct mlx5_ib_dev *dev, 12935b0aa67SMark Bloch struct net_device *ndev, 130c446d9daSMark Bloch struct net_device *upper, 1311fb7f897SMark Bloch u32 *port_num) 13235b0aa67SMark Bloch { 13335b0aa67SMark Bloch struct net_device *rep_ndev; 13435b0aa67SMark Bloch struct mlx5_ib_port *port; 13535b0aa67SMark Bloch int i; 13635b0aa67SMark Bloch 13735b0aa67SMark Bloch for (i = 0; i < dev->num_ports; i++) { 13835b0aa67SMark Bloch port = &dev->port[i]; 13935b0aa67SMark Bloch if (!port->rep) 14035b0aa67SMark Bloch continue; 14135b0aa67SMark Bloch 142c446d9daSMark Bloch if (upper == ndev && port->rep->vport == MLX5_VPORT_UPLINK) { 143c446d9daSMark Bloch *port_num = i + 1; 144c446d9daSMark Bloch return &port->roce; 145c446d9daSMark Bloch } 146c446d9daSMark Bloch 147c446d9daSMark Bloch if (upper && port->rep->vport == MLX5_VPORT_UPLINK) 148c446d9daSMark Bloch continue; 149c446d9daSMark Bloch 15035b0aa67SMark Bloch read_lock(&port->roce.netdev_lock); 151658cfcebSMark Bloch rep_ndev = mlx5_ib_get_rep_netdev(port->rep->esw, 15235b0aa67SMark Bloch port->rep->vport); 15335b0aa67SMark Bloch if (rep_ndev == ndev) { 15435b0aa67SMark Bloch read_unlock(&port->roce.netdev_lock); 15535b0aa67SMark Bloch *port_num = i + 1; 15635b0aa67SMark Bloch return &port->roce; 15735b0aa67SMark Bloch } 15835b0aa67SMark Bloch read_unlock(&port->roce.netdev_lock); 15935b0aa67SMark Bloch } 16035b0aa67SMark Bloch 16135b0aa67SMark Bloch return NULL; 16235b0aa67SMark Bloch } 16335b0aa67SMark Bloch 164fc24fc5eSAchiad Shochat static int mlx5_netdev_event(struct notifier_block *this, 165fc24fc5eSAchiad Shochat unsigned long event, void *ptr) 166fc24fc5eSAchiad Shochat { 1677fd8aefbSDaniel Jurgens struct mlx5_roce *roce = container_of(this, struct mlx5_roce, nb); 168fc24fc5eSAchiad Shochat struct net_device *ndev = netdev_notifier_info_to_dev(ptr); 1691fb7f897SMark Bloch u32 port_num = roce->native_port_num; 1707fd8aefbSDaniel Jurgens struct mlx5_core_dev *mdev; 1717fd8aefbSDaniel Jurgens struct mlx5_ib_dev *ibdev; 1727fd8aefbSDaniel Jurgens 1737fd8aefbSDaniel Jurgens ibdev = roce->dev; 17432f69e4bSDaniel Jurgens mdev = mlx5_ib_get_native_port_mdev(ibdev, port_num, NULL); 17532f69e4bSDaniel Jurgens if (!mdev) 17632f69e4bSDaniel Jurgens return NOTIFY_DONE; 177fc24fc5eSAchiad Shochat 1785ec8c83eSAviv Heller switch (event) { 1795ec8c83eSAviv Heller case NETDEV_REGISTER: 18035b0aa67SMark Bloch /* Should already be registered during the load */ 18135b0aa67SMark Bloch if (ibdev->is_rep) 18235b0aa67SMark Bloch break; 1837fd8aefbSDaniel Jurgens write_lock(&roce->netdev_lock); 184dce45af5SLinus Torvalds if (ndev->dev.parent == mdev->device) 185842a9c83SOr Gerlitz roce->netdev = ndev; 1867fd8aefbSDaniel Jurgens write_unlock(&roce->netdev_lock); 1875ec8c83eSAviv Heller break; 1885ec8c83eSAviv Heller 189842a9c83SOr Gerlitz case NETDEV_UNREGISTER: 19035b0aa67SMark Bloch /* In case of reps, ib device goes away before the netdevs */ 191842a9c83SOr Gerlitz write_lock(&roce->netdev_lock); 192842a9c83SOr Gerlitz if (roce->netdev == ndev) 193842a9c83SOr Gerlitz roce->netdev = NULL; 194842a9c83SOr Gerlitz write_unlock(&roce->netdev_lock); 195842a9c83SOr Gerlitz break; 196842a9c83SOr Gerlitz 197fd65f1b8SMoni Shoua case NETDEV_CHANGE: 1985ec8c83eSAviv Heller case NETDEV_UP: 19988621dfeSAviv Heller case NETDEV_DOWN: { 2007fd8aefbSDaniel Jurgens struct net_device *lag_ndev = mlx5_lag_get_roce_netdev(mdev); 20188621dfeSAviv Heller struct net_device *upper = NULL; 20288621dfeSAviv Heller 20388621dfeSAviv Heller if (lag_ndev) { 20488621dfeSAviv Heller upper = netdev_master_upper_dev_get(lag_ndev); 20588621dfeSAviv Heller dev_put(lag_ndev); 20688621dfeSAviv Heller } 20788621dfeSAviv Heller 20835b0aa67SMark Bloch if (ibdev->is_rep) 209c446d9daSMark Bloch roce = mlx5_get_rep_roce(ibdev, ndev, upper, &port_num); 21035b0aa67SMark Bloch if (!roce) 21135b0aa67SMark Bloch return NOTIFY_DONE; 212c446d9daSMark Bloch if ((upper == ndev || 213c446d9daSMark Bloch ((!upper || ibdev->is_rep) && ndev == roce->netdev)) && 214c446d9daSMark Bloch ibdev->ib_active) { 215626bc02dSBart Van Assche struct ib_event ibev = { }; 216fd65f1b8SMoni Shoua enum ib_port_state port_state; 2175ec8c83eSAviv Heller 2187fd8aefbSDaniel Jurgens if (get_port_state(&ibdev->ib_dev, port_num, 2197fd8aefbSDaniel Jurgens &port_state)) 2207fd8aefbSDaniel Jurgens goto done; 221fd65f1b8SMoni Shoua 2227fd8aefbSDaniel Jurgens if (roce->last_port_state == port_state) 2237fd8aefbSDaniel Jurgens goto done; 224fd65f1b8SMoni Shoua 2257fd8aefbSDaniel Jurgens roce->last_port_state = port_state; 2265ec8c83eSAviv Heller ibev.device = &ibdev->ib_dev; 227fd65f1b8SMoni Shoua if (port_state == IB_PORT_DOWN) 228fd65f1b8SMoni Shoua ibev.event = IB_EVENT_PORT_ERR; 229fd65f1b8SMoni Shoua else if (port_state == IB_PORT_ACTIVE) 230fd65f1b8SMoni Shoua ibev.event = IB_EVENT_PORT_ACTIVE; 231fd65f1b8SMoni Shoua else 2327fd8aefbSDaniel Jurgens goto done; 233fd65f1b8SMoni Shoua 2347fd8aefbSDaniel Jurgens ibev.element.port_num = port_num; 2355ec8c83eSAviv Heller ib_dispatch_event(&ibev); 2365ec8c83eSAviv Heller } 2375ec8c83eSAviv Heller break; 23888621dfeSAviv Heller } 2395ec8c83eSAviv Heller 2405ec8c83eSAviv Heller default: 2415ec8c83eSAviv Heller break; 2425ec8c83eSAviv Heller } 2437fd8aefbSDaniel Jurgens done: 24432f69e4bSDaniel Jurgens mlx5_ib_put_native_port_mdev(ibdev, port_num); 245fc24fc5eSAchiad Shochat return NOTIFY_DONE; 246fc24fc5eSAchiad Shochat } 247fc24fc5eSAchiad Shochat 248fc24fc5eSAchiad Shochat static struct net_device *mlx5_ib_get_netdev(struct ib_device *device, 2491fb7f897SMark Bloch u32 port_num) 250fc24fc5eSAchiad Shochat { 251fc24fc5eSAchiad Shochat struct mlx5_ib_dev *ibdev = to_mdev(device); 252fc24fc5eSAchiad Shochat struct net_device *ndev; 25332f69e4bSDaniel Jurgens struct mlx5_core_dev *mdev; 254fc24fc5eSAchiad Shochat 25532f69e4bSDaniel Jurgens mdev = mlx5_ib_get_native_port_mdev(ibdev, port_num, NULL); 25632f69e4bSDaniel Jurgens if (!mdev) 25732f69e4bSDaniel Jurgens return NULL; 25832f69e4bSDaniel Jurgens 25932f69e4bSDaniel Jurgens ndev = mlx5_lag_get_roce_netdev(mdev); 26088621dfeSAviv Heller if (ndev) 26132f69e4bSDaniel Jurgens goto out; 26288621dfeSAviv Heller 263fc24fc5eSAchiad Shochat /* Ensure ndev does not disappear before we invoke dev_hold() 264fc24fc5eSAchiad Shochat */ 26595579e78SMark Bloch read_lock(&ibdev->port[port_num - 1].roce.netdev_lock); 26695579e78SMark Bloch ndev = ibdev->port[port_num - 1].roce.netdev; 267fc24fc5eSAchiad Shochat if (ndev) 268fc24fc5eSAchiad Shochat dev_hold(ndev); 26995579e78SMark Bloch read_unlock(&ibdev->port[port_num - 1].roce.netdev_lock); 270fc24fc5eSAchiad Shochat 27132f69e4bSDaniel Jurgens out: 27232f69e4bSDaniel Jurgens mlx5_ib_put_native_port_mdev(ibdev, port_num); 273fc24fc5eSAchiad Shochat return ndev; 274fc24fc5eSAchiad Shochat } 275fc24fc5eSAchiad Shochat 27632f69e4bSDaniel Jurgens struct mlx5_core_dev *mlx5_ib_get_native_port_mdev(struct mlx5_ib_dev *ibdev, 2771fb7f897SMark Bloch u32 ib_port_num, 2781fb7f897SMark Bloch u32 *native_port_num) 27932f69e4bSDaniel Jurgens { 28032f69e4bSDaniel Jurgens enum rdma_link_layer ll = mlx5_ib_port_link_layer(&ibdev->ib_dev, 28132f69e4bSDaniel Jurgens ib_port_num); 28232f69e4bSDaniel Jurgens struct mlx5_core_dev *mdev = NULL; 28332f69e4bSDaniel Jurgens struct mlx5_ib_multiport_info *mpi; 28432f69e4bSDaniel Jurgens struct mlx5_ib_port *port; 28532f69e4bSDaniel Jurgens 286210b1f78SMark Bloch if (!mlx5_core_mp_enabled(ibdev->mdev) || 287210b1f78SMark Bloch ll != IB_LINK_LAYER_ETHERNET) { 288210b1f78SMark Bloch if (native_port_num) 289210b1f78SMark Bloch *native_port_num = ib_port_num; 290210b1f78SMark Bloch return ibdev->mdev; 291210b1f78SMark Bloch } 292210b1f78SMark Bloch 29332f69e4bSDaniel Jurgens if (native_port_num) 29432f69e4bSDaniel Jurgens *native_port_num = 1; 29532f69e4bSDaniel Jurgens 29632f69e4bSDaniel Jurgens port = &ibdev->port[ib_port_num - 1]; 29732f69e4bSDaniel Jurgens spin_lock(&port->mp.mpi_lock); 29832f69e4bSDaniel Jurgens mpi = ibdev->port[ib_port_num - 1].mp.mpi; 29932f69e4bSDaniel Jurgens if (mpi && !mpi->unaffiliate) { 30032f69e4bSDaniel Jurgens mdev = mpi->mdev; 30132f69e4bSDaniel Jurgens /* If it's the master no need to refcount, it'll exist 30232f69e4bSDaniel Jurgens * as long as the ib_dev exists. 30332f69e4bSDaniel Jurgens */ 30432f69e4bSDaniel Jurgens if (!mpi->is_master) 30532f69e4bSDaniel Jurgens mpi->mdev_refcnt++; 30632f69e4bSDaniel Jurgens } 30732f69e4bSDaniel Jurgens spin_unlock(&port->mp.mpi_lock); 30832f69e4bSDaniel Jurgens 30932f69e4bSDaniel Jurgens return mdev; 31032f69e4bSDaniel Jurgens } 31132f69e4bSDaniel Jurgens 3121fb7f897SMark Bloch void mlx5_ib_put_native_port_mdev(struct mlx5_ib_dev *ibdev, u32 port_num) 31332f69e4bSDaniel Jurgens { 31432f69e4bSDaniel Jurgens enum rdma_link_layer ll = mlx5_ib_port_link_layer(&ibdev->ib_dev, 31532f69e4bSDaniel Jurgens port_num); 31632f69e4bSDaniel Jurgens struct mlx5_ib_multiport_info *mpi; 31732f69e4bSDaniel Jurgens struct mlx5_ib_port *port; 31832f69e4bSDaniel Jurgens 31932f69e4bSDaniel Jurgens if (!mlx5_core_mp_enabled(ibdev->mdev) || ll != IB_LINK_LAYER_ETHERNET) 32032f69e4bSDaniel Jurgens return; 32132f69e4bSDaniel Jurgens 32232f69e4bSDaniel Jurgens port = &ibdev->port[port_num - 1]; 32332f69e4bSDaniel Jurgens 32432f69e4bSDaniel Jurgens spin_lock(&port->mp.mpi_lock); 32532f69e4bSDaniel Jurgens mpi = ibdev->port[port_num - 1].mp.mpi; 32632f69e4bSDaniel Jurgens if (mpi->is_master) 32732f69e4bSDaniel Jurgens goto out; 32832f69e4bSDaniel Jurgens 32932f69e4bSDaniel Jurgens mpi->mdev_refcnt--; 33032f69e4bSDaniel Jurgens if (mpi->unaffiliate) 33132f69e4bSDaniel Jurgens complete(&mpi->unref_comp); 33232f69e4bSDaniel Jurgens out: 33332f69e4bSDaniel Jurgens spin_unlock(&port->mp.mpi_lock); 33432f69e4bSDaniel Jurgens } 33532f69e4bSDaniel Jurgens 336639bf441SAharon Landau static int translate_eth_legacy_proto_oper(u32 eth_proto_oper, 337639bf441SAharon Landau u16 *active_speed, u8 *active_width) 338f1b65df5SNoa Osherovich { 339f1b65df5SNoa Osherovich switch (eth_proto_oper) { 340f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_1000BASE_CX_SGMII): 341f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_1000BASE_KX): 342f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_100BASE_TX): 343f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_1000BASE_T): 344f1b65df5SNoa Osherovich *active_width = IB_WIDTH_1X; 345f1b65df5SNoa Osherovich *active_speed = IB_SPEED_SDR; 346f1b65df5SNoa Osherovich break; 347f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_10GBASE_T): 348f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_10GBASE_CX4): 349f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_10GBASE_KX4): 350f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_10GBASE_KR): 351f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_10GBASE_CR): 352f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_10GBASE_SR): 353f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_10GBASE_ER): 354f1b65df5SNoa Osherovich *active_width = IB_WIDTH_1X; 355f1b65df5SNoa Osherovich *active_speed = IB_SPEED_QDR; 356f1b65df5SNoa Osherovich break; 357f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_25GBASE_CR): 358f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_25GBASE_KR): 359f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_25GBASE_SR): 360f1b65df5SNoa Osherovich *active_width = IB_WIDTH_1X; 361f1b65df5SNoa Osherovich *active_speed = IB_SPEED_EDR; 362f1b65df5SNoa Osherovich break; 363f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_40GBASE_CR4): 364f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_40GBASE_KR4): 365f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_40GBASE_SR4): 366f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_40GBASE_LR4): 367f1b65df5SNoa Osherovich *active_width = IB_WIDTH_4X; 368f1b65df5SNoa Osherovich *active_speed = IB_SPEED_QDR; 369f1b65df5SNoa Osherovich break; 370f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_50GBASE_CR2): 371f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_50GBASE_KR2): 372f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_50GBASE_SR2): 373f1b65df5SNoa Osherovich *active_width = IB_WIDTH_1X; 374f1b65df5SNoa Osherovich *active_speed = IB_SPEED_HDR; 375f1b65df5SNoa Osherovich break; 376f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_56GBASE_R4): 377f1b65df5SNoa Osherovich *active_width = IB_WIDTH_4X; 378f1b65df5SNoa Osherovich *active_speed = IB_SPEED_FDR; 379f1b65df5SNoa Osherovich break; 380f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_100GBASE_CR4): 381f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_100GBASE_SR4): 382f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_100GBASE_KR4): 383f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_100GBASE_LR4): 384f1b65df5SNoa Osherovich *active_width = IB_WIDTH_4X; 385f1b65df5SNoa Osherovich *active_speed = IB_SPEED_EDR; 386f1b65df5SNoa Osherovich break; 387f1b65df5SNoa Osherovich default: 388f1b65df5SNoa Osherovich return -EINVAL; 389f1b65df5SNoa Osherovich } 390f1b65df5SNoa Osherovich 391f1b65df5SNoa Osherovich return 0; 392f1b65df5SNoa Osherovich } 393f1b65df5SNoa Osherovich 394639bf441SAharon Landau static int translate_eth_ext_proto_oper(u32 eth_proto_oper, u16 *active_speed, 39508e8676fSAya Levin u8 *active_width) 39608e8676fSAya Levin { 39708e8676fSAya Levin switch (eth_proto_oper) { 39808e8676fSAya Levin case MLX5E_PROT_MASK(MLX5E_SGMII_100M): 39908e8676fSAya Levin case MLX5E_PROT_MASK(MLX5E_1000BASE_X_SGMII): 40008e8676fSAya Levin *active_width = IB_WIDTH_1X; 40108e8676fSAya Levin *active_speed = IB_SPEED_SDR; 40208e8676fSAya Levin break; 40308e8676fSAya Levin case MLX5E_PROT_MASK(MLX5E_5GBASE_R): 40408e8676fSAya Levin *active_width = IB_WIDTH_1X; 40508e8676fSAya Levin *active_speed = IB_SPEED_DDR; 40608e8676fSAya Levin break; 40708e8676fSAya Levin case MLX5E_PROT_MASK(MLX5E_10GBASE_XFI_XAUI_1): 40808e8676fSAya Levin *active_width = IB_WIDTH_1X; 40908e8676fSAya Levin *active_speed = IB_SPEED_QDR; 41008e8676fSAya Levin break; 41108e8676fSAya Levin case MLX5E_PROT_MASK(MLX5E_40GBASE_XLAUI_4_XLPPI_4): 41208e8676fSAya Levin *active_width = IB_WIDTH_4X; 41308e8676fSAya Levin *active_speed = IB_SPEED_QDR; 41408e8676fSAya Levin break; 41508e8676fSAya Levin case MLX5E_PROT_MASK(MLX5E_25GAUI_1_25GBASE_CR_KR): 41608e8676fSAya Levin *active_width = IB_WIDTH_1X; 41708e8676fSAya Levin *active_speed = IB_SPEED_EDR; 41808e8676fSAya Levin break; 41908e8676fSAya Levin case MLX5E_PROT_MASK(MLX5E_50GAUI_2_LAUI_2_50GBASE_CR2_KR2): 420cd272875SAya Levin *active_width = IB_WIDTH_2X; 421cd272875SAya Levin *active_speed = IB_SPEED_EDR; 422cd272875SAya Levin break; 42308e8676fSAya Levin case MLX5E_PROT_MASK(MLX5E_50GAUI_1_LAUI_1_50GBASE_CR_KR): 42408e8676fSAya Levin *active_width = IB_WIDTH_1X; 42508e8676fSAya Levin *active_speed = IB_SPEED_HDR; 42608e8676fSAya Levin break; 427cd272875SAya Levin case MLX5E_PROT_MASK(MLX5E_CAUI_4_100GBASE_CR4_KR4): 428cd272875SAya Levin *active_width = IB_WIDTH_4X; 429cd272875SAya Levin *active_speed = IB_SPEED_EDR; 430cd272875SAya Levin break; 43108e8676fSAya Levin case MLX5E_PROT_MASK(MLX5E_100GAUI_2_100GBASE_CR2_KR2): 43208e8676fSAya Levin *active_width = IB_WIDTH_2X; 43308e8676fSAya Levin *active_speed = IB_SPEED_HDR; 43408e8676fSAya Levin break; 435f946e45fSMeir Lichtinger case MLX5E_PROT_MASK(MLX5E_100GAUI_1_100GBASE_CR_KR): 436f946e45fSMeir Lichtinger *active_width = IB_WIDTH_1X; 437f946e45fSMeir Lichtinger *active_speed = IB_SPEED_NDR; 438f946e45fSMeir Lichtinger break; 43908e8676fSAya Levin case MLX5E_PROT_MASK(MLX5E_200GAUI_4_200GBASE_CR4_KR4): 44008e8676fSAya Levin *active_width = IB_WIDTH_4X; 44108e8676fSAya Levin *active_speed = IB_SPEED_HDR; 44208e8676fSAya Levin break; 443f946e45fSMeir Lichtinger case MLX5E_PROT_MASK(MLX5E_200GAUI_2_200GBASE_CR2_KR2): 444f946e45fSMeir Lichtinger *active_width = IB_WIDTH_2X; 445f946e45fSMeir Lichtinger *active_speed = IB_SPEED_NDR; 446f946e45fSMeir Lichtinger break; 447*4ce59074SPatrisious Haddad case MLX5E_PROT_MASK(MLX5E_400GAUI_8_400GBASE_CR8): 44888c9483fSMaher Sanalla *active_width = IB_WIDTH_8X; 44988c9483fSMaher Sanalla *active_speed = IB_SPEED_HDR; 45088c9483fSMaher Sanalla break; 451f946e45fSMeir Lichtinger case MLX5E_PROT_MASK(MLX5E_400GAUI_4_400GBASE_CR4_KR4): 452f946e45fSMeir Lichtinger *active_width = IB_WIDTH_4X; 453f946e45fSMeir Lichtinger *active_speed = IB_SPEED_NDR; 454f946e45fSMeir Lichtinger break; 45508e8676fSAya Levin default: 45608e8676fSAya Levin return -EINVAL; 45708e8676fSAya Levin } 45808e8676fSAya Levin 45908e8676fSAya Levin return 0; 46008e8676fSAya Levin } 46108e8676fSAya Levin 462639bf441SAharon Landau static int translate_eth_proto_oper(u32 eth_proto_oper, u16 *active_speed, 46308e8676fSAya Levin u8 *active_width, bool ext) 46408e8676fSAya Levin { 46508e8676fSAya Levin return ext ? 46608e8676fSAya Levin translate_eth_ext_proto_oper(eth_proto_oper, active_speed, 46708e8676fSAya Levin active_width) : 46808e8676fSAya Levin translate_eth_legacy_proto_oper(eth_proto_oper, active_speed, 46908e8676fSAya Levin active_width); 47008e8676fSAya Levin } 47108e8676fSAya Levin 4721fb7f897SMark Bloch static int mlx5_query_port_roce(struct ib_device *device, u32 port_num, 4733f89a643SAchiad Shochat struct ib_port_attr *props) 4743f89a643SAchiad Shochat { 4753f89a643SAchiad Shochat struct mlx5_ib_dev *dev = to_mdev(device); 476bc4e12ffSAya Levin u32 out[MLX5_ST_SZ_DW(ptys_reg)] = {0}; 477da005f9fSColin Ian King struct mlx5_core_dev *mdev; 47888621dfeSAviv Heller struct net_device *ndev, *upper; 4793f89a643SAchiad Shochat enum ib_mtu ndev_ib_mtu; 480b3cbd6f0SDaniel Jurgens bool put_mdev = true; 481f1b65df5SNoa Osherovich u32 eth_prot_oper; 4821fb7f897SMark Bloch u32 mdev_port_num; 48308e8676fSAya Levin bool ext; 484095b0927SIlan Tayari int err; 4853f89a643SAchiad Shochat 486b3cbd6f0SDaniel Jurgens mdev = mlx5_ib_get_native_port_mdev(dev, port_num, &mdev_port_num); 487b3cbd6f0SDaniel Jurgens if (!mdev) { 488b3cbd6f0SDaniel Jurgens /* This means the port isn't affiliated yet. Get the 489b3cbd6f0SDaniel Jurgens * info for the master port instead. 490b3cbd6f0SDaniel Jurgens */ 491b3cbd6f0SDaniel Jurgens put_mdev = false; 492b3cbd6f0SDaniel Jurgens mdev = dev->mdev; 493b3cbd6f0SDaniel Jurgens mdev_port_num = 1; 494b3cbd6f0SDaniel Jurgens port_num = 1; 495b3cbd6f0SDaniel Jurgens } 496b3cbd6f0SDaniel Jurgens 497f1b65df5SNoa Osherovich /* Possible bad flows are checked before filling out props so in case 498f1b65df5SNoa Osherovich * of an error it will still be zeroed out. 49926628e2dSMark Bloch * Use native port in case of reps 50050f22fd8SNoa Osherovich */ 50126628e2dSMark Bloch if (dev->is_rep) 50226628e2dSMark Bloch err = mlx5_query_port_ptys(mdev, out, sizeof(out), MLX5_PTYS_EN, 50326628e2dSMark Bloch 1); 50426628e2dSMark Bloch else 505bc4e12ffSAya Levin err = mlx5_query_port_ptys(mdev, out, sizeof(out), MLX5_PTYS_EN, 506b3cbd6f0SDaniel Jurgens mdev_port_num); 507095b0927SIlan Tayari if (err) 508b3cbd6f0SDaniel Jurgens goto out; 509530c8632SAya Levin ext = !!MLX5_GET_ETH_PROTO(ptys_reg, out, true, eth_proto_capability); 51008e8676fSAya Levin eth_prot_oper = MLX5_GET_ETH_PROTO(ptys_reg, out, ext, eth_proto_oper); 511f1b65df5SNoa Osherovich 5127672ed33SHonggang Li props->active_width = IB_WIDTH_4X; 5137672ed33SHonggang Li props->active_speed = IB_SPEED_QDR; 5147672ed33SHonggang Li 515f1b65df5SNoa Osherovich translate_eth_proto_oper(eth_prot_oper, &props->active_speed, 51608e8676fSAya Levin &props->active_width, ext); 5173f89a643SAchiad Shochat 5187852546fSMaor Gottlieb if (!dev->is_rep && dev->mdev->roce.roce_en) { 5197a58779eSParav Pandit u16 qkey_viol_cntr; 5207a58779eSParav Pandit 5213f89a643SAchiad Shochat props->port_cap_flags |= IB_PORT_CM_SUP; 5222f944c0fSJason Gunthorpe props->ip_gids = true; 5233f89a643SAchiad Shochat props->gid_tbl_len = MLX5_CAP_ROCE(dev->mdev, 5243f89a643SAchiad Shochat roce_address_table_size); 5257a58779eSParav Pandit mlx5_query_nic_vport_qkey_viol_cntr(mdev, &qkey_viol_cntr); 5267a58779eSParav Pandit props->qkey_viol_cntr = qkey_viol_cntr; 5277a58779eSParav Pandit } 5283f89a643SAchiad Shochat props->max_mtu = IB_MTU_4096; 5293f89a643SAchiad Shochat props->max_msg_sz = 1 << MLX5_CAP_GEN(dev->mdev, log_max_msg); 5303f89a643SAchiad Shochat props->pkey_tbl_len = 1; 5313f89a643SAchiad Shochat props->state = IB_PORT_DOWN; 53272a7720fSKamal Heib props->phys_state = IB_PORT_PHYS_STATE_DISABLED; 5333f89a643SAchiad Shochat 534b3cbd6f0SDaniel Jurgens /* If this is a stub query for an unaffiliated port stop here */ 535b3cbd6f0SDaniel Jurgens if (!put_mdev) 536b3cbd6f0SDaniel Jurgens goto out; 537b3cbd6f0SDaniel Jurgens 5383f89a643SAchiad Shochat ndev = mlx5_ib_get_netdev(device, port_num); 5393f89a643SAchiad Shochat if (!ndev) 540b3cbd6f0SDaniel Jurgens goto out; 5413f89a643SAchiad Shochat 5427c34ec19SAviv Heller if (dev->lag_active) { 54388621dfeSAviv Heller rcu_read_lock(); 54488621dfeSAviv Heller upper = netdev_master_upper_dev_get_rcu(ndev); 54588621dfeSAviv Heller if (upper) { 54688621dfeSAviv Heller dev_put(ndev); 54788621dfeSAviv Heller ndev = upper; 54888621dfeSAviv Heller dev_hold(ndev); 54988621dfeSAviv Heller } 55088621dfeSAviv Heller rcu_read_unlock(); 55188621dfeSAviv Heller } 55288621dfeSAviv Heller 5533f89a643SAchiad Shochat if (netif_running(ndev) && netif_carrier_ok(ndev)) { 5543f89a643SAchiad Shochat props->state = IB_PORT_ACTIVE; 55572a7720fSKamal Heib props->phys_state = IB_PORT_PHYS_STATE_LINK_UP; 5563f89a643SAchiad Shochat } 5573f89a643SAchiad Shochat 5583f89a643SAchiad Shochat ndev_ib_mtu = iboe_get_mtu(ndev->mtu); 5593f89a643SAchiad Shochat 5603f89a643SAchiad Shochat dev_put(ndev); 5613f89a643SAchiad Shochat 5623f89a643SAchiad Shochat props->active_mtu = min(props->max_mtu, ndev_ib_mtu); 563b3cbd6f0SDaniel Jurgens out: 564b3cbd6f0SDaniel Jurgens if (put_mdev) 565b3cbd6f0SDaniel Jurgens mlx5_ib_put_native_port_mdev(dev, port_num); 566b3cbd6f0SDaniel Jurgens return err; 5673f89a643SAchiad Shochat } 5683f89a643SAchiad Shochat 569758ce14aSPatrisious Haddad int set_roce_addr(struct mlx5_ib_dev *dev, u32 port_num, 570095b0927SIlan Tayari unsigned int index, const union ib_gid *gid, 571095b0927SIlan Tayari const struct ib_gid_attr *attr) 5723cca2606SAchiad Shochat { 573dedbc2d3SParav Pandit enum ib_gid_type gid_type; 574a70c0739SParav Pandit u16 vlan_id = 0xffff; 575095b0927SIlan Tayari u8 roce_version = 0; 576095b0927SIlan Tayari u8 roce_l3_type = 0; 577095b0927SIlan Tayari u8 mac[ETH_ALEN]; 578a70c0739SParav Pandit int ret; 5793cca2606SAchiad Shochat 580095b0927SIlan Tayari gid_type = attr->gid_type; 581dedbc2d3SParav Pandit if (gid) { 582a70c0739SParav Pandit ret = rdma_read_gid_l2_fields(attr, &vlan_id, &mac[0]); 583a70c0739SParav Pandit if (ret) 584a70c0739SParav Pandit return ret; 5853cca2606SAchiad Shochat } 5863cca2606SAchiad Shochat 587095b0927SIlan Tayari switch (gid_type) { 5881c15b4f2SAvihai Horon case IB_GID_TYPE_ROCE: 589095b0927SIlan Tayari roce_version = MLX5_ROCE_VERSION_1; 5903cca2606SAchiad Shochat break; 5913cca2606SAchiad Shochat case IB_GID_TYPE_ROCE_UDP_ENCAP: 592095b0927SIlan Tayari roce_version = MLX5_ROCE_VERSION_2; 593dedbc2d3SParav Pandit if (gid && ipv6_addr_v4mapped((void *)gid)) 594095b0927SIlan Tayari roce_l3_type = MLX5_ROCE_L3_TYPE_IPV4; 595095b0927SIlan Tayari else 596095b0927SIlan Tayari roce_l3_type = MLX5_ROCE_L3_TYPE_IPV6; 5973cca2606SAchiad Shochat break; 5983cca2606SAchiad Shochat 5993cca2606SAchiad Shochat default: 600095b0927SIlan Tayari mlx5_ib_warn(dev, "Unexpected GID type %u\n", gid_type); 6013cca2606SAchiad Shochat } 6023cca2606SAchiad Shochat 603095b0927SIlan Tayari return mlx5_core_roce_gid_set(dev->mdev, index, roce_version, 604cf34e1feSParav Pandit roce_l3_type, gid->raw, mac, 605a70c0739SParav Pandit vlan_id < VLAN_CFI_MASK, vlan_id, 606cf34e1feSParav Pandit port_num); 6073cca2606SAchiad Shochat } 6083cca2606SAchiad Shochat 609f4df9a7cSParav Pandit static int mlx5_ib_add_gid(const struct ib_gid_attr *attr, 6103cca2606SAchiad Shochat __always_unused void **context) 6113cca2606SAchiad Shochat { 612758ce14aSPatrisious Haddad int ret; 613758ce14aSPatrisious Haddad 614758ce14aSPatrisious Haddad ret = mlx5r_add_gid_macsec_operations(attr); 615758ce14aSPatrisious Haddad if (ret) 616758ce14aSPatrisious Haddad return ret; 617758ce14aSPatrisious Haddad 618414448d2SParav Pandit return set_roce_addr(to_mdev(attr->device), attr->port_num, 619f4df9a7cSParav Pandit attr->index, &attr->gid, attr); 6203cca2606SAchiad Shochat } 6213cca2606SAchiad Shochat 622414448d2SParav Pandit static int mlx5_ib_del_gid(const struct ib_gid_attr *attr, 623414448d2SParav Pandit __always_unused void **context) 6243cca2606SAchiad Shochat { 625758ce14aSPatrisious Haddad int ret; 626758ce14aSPatrisious Haddad 627758ce14aSPatrisious Haddad ret = set_roce_addr(to_mdev(attr->device), attr->port_num, 628dedbc2d3SParav Pandit attr->index, NULL, attr); 629758ce14aSPatrisious Haddad if (ret) 630758ce14aSPatrisious Haddad return ret; 631758ce14aSPatrisious Haddad 632758ce14aSPatrisious Haddad mlx5r_del_gid_macsec_operations(attr); 633758ce14aSPatrisious Haddad return 0; 6343cca2606SAchiad Shochat } 6353cca2606SAchiad Shochat 6365ac55dfcSMark Zhang __be16 mlx5_get_roce_udp_sport_min(const struct mlx5_ib_dev *dev, 63747ec3866SParav Pandit const struct ib_gid_attr *attr) 6382811ba51SAchiad Shochat { 63947ec3866SParav Pandit if (attr->gid_type != IB_GID_TYPE_ROCE_UDP_ENCAP) 6402811ba51SAchiad Shochat return 0; 6412811ba51SAchiad Shochat 6422811ba51SAchiad Shochat return cpu_to_be16(MLX5_CAP_ROCE(dev->mdev, r_roce_min_src_udp_port)); 6432811ba51SAchiad Shochat } 6442811ba51SAchiad Shochat 6451b5daf11SMajd Dibbiny static int mlx5_use_mad_ifc(struct mlx5_ib_dev *dev) 6461b5daf11SMajd Dibbiny { 6477fae6655SNoa Osherovich if (MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_IB) 648d603c809SEli Cohen return !MLX5_CAP_GEN(dev->mdev, ib_virt); 6497fae6655SNoa Osherovich return 0; 6501b5daf11SMajd Dibbiny } 6511b5daf11SMajd Dibbiny 6521b5daf11SMajd Dibbiny enum { 6531b5daf11SMajd Dibbiny MLX5_VPORT_ACCESS_METHOD_MAD, 6541b5daf11SMajd Dibbiny MLX5_VPORT_ACCESS_METHOD_HCA, 6551b5daf11SMajd Dibbiny MLX5_VPORT_ACCESS_METHOD_NIC, 6561b5daf11SMajd Dibbiny }; 6571b5daf11SMajd Dibbiny 6581b5daf11SMajd Dibbiny static int mlx5_get_vport_access_method(struct ib_device *ibdev) 6591b5daf11SMajd Dibbiny { 6601b5daf11SMajd Dibbiny if (mlx5_use_mad_ifc(to_mdev(ibdev))) 6611b5daf11SMajd Dibbiny return MLX5_VPORT_ACCESS_METHOD_MAD; 6621b5daf11SMajd Dibbiny 663ebd61f68SAchiad Shochat if (mlx5_ib_port_link_layer(ibdev, 1) == 6641b5daf11SMajd Dibbiny IB_LINK_LAYER_ETHERNET) 6651b5daf11SMajd Dibbiny return MLX5_VPORT_ACCESS_METHOD_NIC; 6661b5daf11SMajd Dibbiny 6671b5daf11SMajd Dibbiny return MLX5_VPORT_ACCESS_METHOD_HCA; 6681b5daf11SMajd Dibbiny } 6691b5daf11SMajd Dibbiny 670da7525d2SEran Ben Elisha static void get_atomic_caps(struct mlx5_ib_dev *dev, 671776a3906SMoni Shoua u8 atomic_size_qp, 672da7525d2SEran Ben Elisha struct ib_device_attr *props) 673da7525d2SEran Ben Elisha { 674da7525d2SEran Ben Elisha u8 tmp; 675da7525d2SEran Ben Elisha u8 atomic_operations = MLX5_CAP_ATOMIC(dev->mdev, atomic_operations); 676da7525d2SEran Ben Elisha u8 atomic_req_8B_endianness_mode = 677bd10838aSOr Gerlitz MLX5_CAP_ATOMIC(dev->mdev, atomic_req_8B_endianness_mode); 678da7525d2SEran Ben Elisha 679da7525d2SEran Ben Elisha /* Check if HW supports 8 bytes standard atomic operations and capable 680da7525d2SEran Ben Elisha * of host endianness respond 681da7525d2SEran Ben Elisha */ 682da7525d2SEran Ben Elisha tmp = MLX5_ATOMIC_OPS_CMP_SWAP | MLX5_ATOMIC_OPS_FETCH_ADD; 683da7525d2SEran Ben Elisha if (((atomic_operations & tmp) == tmp) && 684da7525d2SEran Ben Elisha (atomic_size_qp & MLX5_ATOMIC_SIZE_QP_8BYTES) && 685da7525d2SEran Ben Elisha (atomic_req_8B_endianness_mode)) { 686da7525d2SEran Ben Elisha props->atomic_cap = IB_ATOMIC_HCA; 687da7525d2SEran Ben Elisha } else { 688da7525d2SEran Ben Elisha props->atomic_cap = IB_ATOMIC_NONE; 689da7525d2SEran Ben Elisha } 690da7525d2SEran Ben Elisha } 691da7525d2SEran Ben Elisha 692776a3906SMoni Shoua static void get_atomic_caps_qp(struct mlx5_ib_dev *dev, 693776a3906SMoni Shoua struct ib_device_attr *props) 694776a3906SMoni Shoua { 695776a3906SMoni Shoua u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_qp); 696776a3906SMoni Shoua 697776a3906SMoni Shoua get_atomic_caps(dev, atomic_size_qp, props); 698776a3906SMoni Shoua } 699776a3906SMoni Shoua 7001b5daf11SMajd Dibbiny static int mlx5_query_system_image_guid(struct ib_device *ibdev, 7011b5daf11SMajd Dibbiny __be64 *sys_image_guid) 7021b5daf11SMajd Dibbiny { 7031b5daf11SMajd Dibbiny struct mlx5_ib_dev *dev = to_mdev(ibdev); 7041b5daf11SMajd Dibbiny struct mlx5_core_dev *mdev = dev->mdev; 7051b5daf11SMajd Dibbiny u64 tmp; 7061b5daf11SMajd Dibbiny int err; 7071b5daf11SMajd Dibbiny 7081b5daf11SMajd Dibbiny switch (mlx5_get_vport_access_method(ibdev)) { 7091b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_MAD: 7101b5daf11SMajd Dibbiny return mlx5_query_mad_ifc_system_image_guid(ibdev, 7111b5daf11SMajd Dibbiny sys_image_guid); 7121b5daf11SMajd Dibbiny 7131b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_HCA: 7141b5daf11SMajd Dibbiny err = mlx5_query_hca_vport_system_image_guid(mdev, &tmp); 7153f89a643SAchiad Shochat break; 7163f89a643SAchiad Shochat 7173f89a643SAchiad Shochat case MLX5_VPORT_ACCESS_METHOD_NIC: 7183f89a643SAchiad Shochat err = mlx5_query_nic_vport_system_image_guid(mdev, &tmp); 7193f89a643SAchiad Shochat break; 7201b5daf11SMajd Dibbiny 7211b5daf11SMajd Dibbiny default: 7221b5daf11SMajd Dibbiny return -EINVAL; 7231b5daf11SMajd Dibbiny } 7243f89a643SAchiad Shochat 7253f89a643SAchiad Shochat if (!err) 7263f89a643SAchiad Shochat *sys_image_guid = cpu_to_be64(tmp); 7273f89a643SAchiad Shochat 7283f89a643SAchiad Shochat return err; 7293f89a643SAchiad Shochat 7301b5daf11SMajd Dibbiny } 7311b5daf11SMajd Dibbiny 7321b5daf11SMajd Dibbiny static int mlx5_query_max_pkeys(struct ib_device *ibdev, 7331b5daf11SMajd Dibbiny u16 *max_pkeys) 7341b5daf11SMajd Dibbiny { 7351b5daf11SMajd Dibbiny struct mlx5_ib_dev *dev = to_mdev(ibdev); 7361b5daf11SMajd Dibbiny struct mlx5_core_dev *mdev = dev->mdev; 7371b5daf11SMajd Dibbiny 7381b5daf11SMajd Dibbiny switch (mlx5_get_vport_access_method(ibdev)) { 7391b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_MAD: 7401b5daf11SMajd Dibbiny return mlx5_query_mad_ifc_max_pkeys(ibdev, max_pkeys); 7411b5daf11SMajd Dibbiny 7421b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_HCA: 7431b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_NIC: 7441b5daf11SMajd Dibbiny *max_pkeys = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, 7451b5daf11SMajd Dibbiny pkey_table_size)); 7461b5daf11SMajd Dibbiny return 0; 7471b5daf11SMajd Dibbiny 7481b5daf11SMajd Dibbiny default: 7491b5daf11SMajd Dibbiny return -EINVAL; 7501b5daf11SMajd Dibbiny } 7511b5daf11SMajd Dibbiny } 7521b5daf11SMajd Dibbiny 7531b5daf11SMajd Dibbiny static int mlx5_query_vendor_id(struct ib_device *ibdev, 7541b5daf11SMajd Dibbiny u32 *vendor_id) 7551b5daf11SMajd Dibbiny { 7561b5daf11SMajd Dibbiny struct mlx5_ib_dev *dev = to_mdev(ibdev); 7571b5daf11SMajd Dibbiny 7581b5daf11SMajd Dibbiny switch (mlx5_get_vport_access_method(ibdev)) { 7591b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_MAD: 7601b5daf11SMajd Dibbiny return mlx5_query_mad_ifc_vendor_id(ibdev, vendor_id); 7611b5daf11SMajd Dibbiny 7621b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_HCA: 7631b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_NIC: 7641b5daf11SMajd Dibbiny return mlx5_core_query_vendor_id(dev->mdev, vendor_id); 7651b5daf11SMajd Dibbiny 7661b5daf11SMajd Dibbiny default: 7671b5daf11SMajd Dibbiny return -EINVAL; 7681b5daf11SMajd Dibbiny } 7691b5daf11SMajd Dibbiny } 7701b5daf11SMajd Dibbiny 7711b5daf11SMajd Dibbiny static int mlx5_query_node_guid(struct mlx5_ib_dev *dev, 7721b5daf11SMajd Dibbiny __be64 *node_guid) 7731b5daf11SMajd Dibbiny { 7741b5daf11SMajd Dibbiny u64 tmp; 7751b5daf11SMajd Dibbiny int err; 7761b5daf11SMajd Dibbiny 7771b5daf11SMajd Dibbiny switch (mlx5_get_vport_access_method(&dev->ib_dev)) { 7781b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_MAD: 7791b5daf11SMajd Dibbiny return mlx5_query_mad_ifc_node_guid(dev, node_guid); 7801b5daf11SMajd Dibbiny 7811b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_HCA: 7821b5daf11SMajd Dibbiny err = mlx5_query_hca_vport_node_guid(dev->mdev, &tmp); 7833f89a643SAchiad Shochat break; 7843f89a643SAchiad Shochat 7853f89a643SAchiad Shochat case MLX5_VPORT_ACCESS_METHOD_NIC: 7863f89a643SAchiad Shochat err = mlx5_query_nic_vport_node_guid(dev->mdev, &tmp); 7873f89a643SAchiad Shochat break; 7881b5daf11SMajd Dibbiny 7891b5daf11SMajd Dibbiny default: 7901b5daf11SMajd Dibbiny return -EINVAL; 7911b5daf11SMajd Dibbiny } 7923f89a643SAchiad Shochat 7933f89a643SAchiad Shochat if (!err) 7943f89a643SAchiad Shochat *node_guid = cpu_to_be64(tmp); 7953f89a643SAchiad Shochat 7963f89a643SAchiad Shochat return err; 7971b5daf11SMajd Dibbiny } 7981b5daf11SMajd Dibbiny 7991b5daf11SMajd Dibbiny struct mlx5_reg_node_desc { 800bd99fdeaSYuval Shaia u8 desc[IB_DEVICE_NODE_DESC_MAX]; 8011b5daf11SMajd Dibbiny }; 8021b5daf11SMajd Dibbiny 8031b5daf11SMajd Dibbiny static int mlx5_query_node_desc(struct mlx5_ib_dev *dev, char *node_desc) 8041b5daf11SMajd Dibbiny { 8051b5daf11SMajd Dibbiny struct mlx5_reg_node_desc in; 8061b5daf11SMajd Dibbiny 8071b5daf11SMajd Dibbiny if (mlx5_use_mad_ifc(dev)) 8081b5daf11SMajd Dibbiny return mlx5_query_mad_ifc_node_desc(dev, node_desc); 8091b5daf11SMajd Dibbiny 8101b5daf11SMajd Dibbiny memset(&in, 0, sizeof(in)); 8111b5daf11SMajd Dibbiny 8121b5daf11SMajd Dibbiny return mlx5_core_access_reg(dev->mdev, &in, sizeof(in), node_desc, 8131b5daf11SMajd Dibbiny sizeof(struct mlx5_reg_node_desc), 8141b5daf11SMajd Dibbiny MLX5_REG_NODE_DESC, 0, 0); 8151b5daf11SMajd Dibbiny } 8161b5daf11SMajd Dibbiny 817e126ba97SEli Cohen static int mlx5_ib_query_device(struct ib_device *ibdev, 8182528e33eSMatan Barak struct ib_device_attr *props, 8192528e33eSMatan Barak struct ib_udata *uhw) 820e126ba97SEli Cohen { 82148357091SLeon Romanovsky size_t uhw_outlen = (uhw) ? uhw->outlen : 0; 822e126ba97SEli Cohen struct mlx5_ib_dev *dev = to_mdev(ibdev); 823938fe83cSSaeed Mahameed struct mlx5_core_dev *mdev = dev->mdev; 824e126ba97SEli Cohen int err = -ENOMEM; 825288c01b7SEli Cohen int max_sq_desc; 826e126ba97SEli Cohen int max_rq_sg; 827e126ba97SEli Cohen int max_sq_sg; 828e0238a6aSSagi Grimberg u64 min_page_size = 1ull << MLX5_CAP_GEN(mdev, log_pg_sz); 82985c7c014SDaniel Jurgens bool raw_support = !mlx5_core_mp_enabled(mdev); 830402ca536SBodong Wang struct mlx5_ib_query_device_resp resp = {}; 831402ca536SBodong Wang size_t resp_len; 832402ca536SBodong Wang u64 max_tso; 833e126ba97SEli Cohen 834402ca536SBodong Wang resp_len = sizeof(resp.comp_mask) + sizeof(resp.response_length); 83548357091SLeon Romanovsky if (uhw_outlen && uhw_outlen < resp_len) 836402ca536SBodong Wang return -EINVAL; 8376f26b2acSErez Alfasi 838402ca536SBodong Wang resp.response_length = resp_len; 839402ca536SBodong Wang 84048357091SLeon Romanovsky if (uhw && uhw->inlen && !ib_is_udata_cleared(uhw, 0, uhw->inlen)) 8412528e33eSMatan Barak return -EINVAL; 8422528e33eSMatan Barak 843e126ba97SEli Cohen memset(props, 0, sizeof(*props)); 8441b5daf11SMajd Dibbiny err = mlx5_query_system_image_guid(ibdev, 8451b5daf11SMajd Dibbiny &props->sys_image_guid); 8461b5daf11SMajd Dibbiny if (err) 8471b5daf11SMajd Dibbiny return err; 8481b5daf11SMajd Dibbiny 8492019d70eSParav Pandit props->max_pkeys = dev->pkey_table_len; 8501b5daf11SMajd Dibbiny 8511b5daf11SMajd Dibbiny err = mlx5_query_vendor_id(ibdev, &props->vendor_id); 8521b5daf11SMajd Dibbiny if (err) 8531b5daf11SMajd Dibbiny return err; 854e126ba97SEli Cohen 8559603b61dSJack Morgenstein props->fw_ver = ((u64)fw_rev_maj(dev->mdev) << 32) | 8569603b61dSJack Morgenstein (fw_rev_min(dev->mdev) << 16) | 8579603b61dSJack Morgenstein fw_rev_sub(dev->mdev); 858e126ba97SEli Cohen props->device_cap_flags = IB_DEVICE_CHANGE_PHY_PORT | 859e126ba97SEli Cohen IB_DEVICE_PORT_ACTIVE_EVENT | 860e126ba97SEli Cohen IB_DEVICE_SYS_IMAGE_GUID | 8611a4c3a3dSEli Cohen IB_DEVICE_RC_RNR_NAK_GEN; 862938fe83cSSaeed Mahameed 863938fe83cSSaeed Mahameed if (MLX5_CAP_GEN(mdev, pkv)) 864e126ba97SEli Cohen props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR; 865938fe83cSSaeed Mahameed if (MLX5_CAP_GEN(mdev, qkv)) 866e126ba97SEli Cohen props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR; 867938fe83cSSaeed Mahameed if (MLX5_CAP_GEN(mdev, apm)) 868e126ba97SEli Cohen props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG; 869938fe83cSSaeed Mahameed if (MLX5_CAP_GEN(mdev, xrc)) 870e126ba97SEli Cohen props->device_cap_flags |= IB_DEVICE_XRC; 871d2370e0aSMatan Barak if (MLX5_CAP_GEN(mdev, imaicl)) { 872d2370e0aSMatan Barak props->device_cap_flags |= IB_DEVICE_MEM_WINDOW | 873d2370e0aSMatan Barak IB_DEVICE_MEM_WINDOW_TYPE_2B; 874d2370e0aSMatan Barak props->max_mw = 1 << MLX5_CAP_GEN(mdev, log_max_mkey); 875b005d316SSagi Grimberg /* We support 'Gappy' memory registration too */ 876e945c653SJason Gunthorpe props->kernel_cap_flags |= IBK_SG_GAPS_REG; 877d2370e0aSMatan Barak } 8780ec52f01SJason Gunthorpe /* IB_WR_REG_MR always requires changing the entity size with UMR */ 8790ec52f01SJason Gunthorpe if (!MLX5_CAP_GEN(dev->mdev, umr_modify_entity_size_disabled)) 880e126ba97SEli Cohen props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS; 881938fe83cSSaeed Mahameed if (MLX5_CAP_GEN(mdev, sho)) { 882e945c653SJason Gunthorpe props->kernel_cap_flags |= IBK_INTEGRITY_HANDOVER; 8832dea9094SSagi Grimberg /* At this stage no support for signature handover */ 8842dea9094SSagi Grimberg props->sig_prot_cap = IB_PROT_T10DIF_TYPE_1 | 8852dea9094SSagi Grimberg IB_PROT_T10DIF_TYPE_2 | 8862dea9094SSagi Grimberg IB_PROT_T10DIF_TYPE_3; 8872dea9094SSagi Grimberg props->sig_guard_cap = IB_GUARD_T10DIF_CRC | 8882dea9094SSagi Grimberg IB_GUARD_T10DIF_CSUM; 8892dea9094SSagi Grimberg } 890938fe83cSSaeed Mahameed if (MLX5_CAP_GEN(mdev, block_lb_mc)) 891e945c653SJason Gunthorpe props->kernel_cap_flags |= IBK_BLOCK_MULTICAST_LOOPBACK; 892e126ba97SEli Cohen 89385c7c014SDaniel Jurgens if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) && raw_support) { 894e8161334SNoa Osherovich if (MLX5_CAP_ETH(mdev, csum_cap)) { 895e8161334SNoa Osherovich /* Legacy bit to support old userspace libraries */ 89688115fe7SBodong Wang props->device_cap_flags |= IB_DEVICE_RAW_IP_CSUM; 897e8161334SNoa Osherovich props->raw_packet_caps |= IB_RAW_PACKET_CAP_IP_CSUM; 898e8161334SNoa Osherovich } 899e8161334SNoa Osherovich 900e8161334SNoa Osherovich if (MLX5_CAP_ETH(dev->mdev, vlan_cap)) 901e8161334SNoa Osherovich props->raw_packet_caps |= 902e8161334SNoa Osherovich IB_RAW_PACKET_CAP_CVLAN_STRIPPING; 90388115fe7SBodong Wang 904a762d460SLeon Romanovsky if (offsetofend(typeof(resp), tso_caps) <= uhw_outlen) { 905402ca536SBodong Wang max_tso = MLX5_CAP_ETH(mdev, max_lso_cap); 906402ca536SBodong Wang if (max_tso) { 907402ca536SBodong Wang resp.tso_caps.max_tso = 1 << max_tso; 908402ca536SBodong Wang resp.tso_caps.supported_qpts |= 909402ca536SBodong Wang 1 << IB_QPT_RAW_PACKET; 910402ca536SBodong Wang resp.response_length += sizeof(resp.tso_caps); 911402ca536SBodong Wang } 912402ca536SBodong Wang } 91331f69a82SYishai Hadas 914a762d460SLeon Romanovsky if (offsetofend(typeof(resp), rss_caps) <= uhw_outlen) { 91531f69a82SYishai Hadas resp.rss_caps.rx_hash_function = 91631f69a82SYishai Hadas MLX5_RX_HASH_FUNC_TOEPLITZ; 91731f69a82SYishai Hadas resp.rss_caps.rx_hash_fields_mask = 91831f69a82SYishai Hadas MLX5_RX_HASH_SRC_IPV4 | 91931f69a82SYishai Hadas MLX5_RX_HASH_DST_IPV4 | 92031f69a82SYishai Hadas MLX5_RX_HASH_SRC_IPV6 | 92131f69a82SYishai Hadas MLX5_RX_HASH_DST_IPV6 | 92231f69a82SYishai Hadas MLX5_RX_HASH_SRC_PORT_TCP | 92331f69a82SYishai Hadas MLX5_RX_HASH_DST_PORT_TCP | 92431f69a82SYishai Hadas MLX5_RX_HASH_SRC_PORT_UDP | 9254e2b53a5SMaor Gottlieb MLX5_RX_HASH_DST_PORT_UDP | 9264e2b53a5SMaor Gottlieb MLX5_RX_HASH_INNER; 92731f69a82SYishai Hadas resp.response_length += sizeof(resp.rss_caps); 92831f69a82SYishai Hadas } 92931f69a82SYishai Hadas } else { 930a762d460SLeon Romanovsky if (offsetofend(typeof(resp), tso_caps) <= uhw_outlen) 93131f69a82SYishai Hadas resp.response_length += sizeof(resp.tso_caps); 932a762d460SLeon Romanovsky if (offsetofend(typeof(resp), rss_caps) <= uhw_outlen) 93331f69a82SYishai Hadas resp.response_length += sizeof(resp.rss_caps); 934402ca536SBodong Wang } 935402ca536SBodong Wang 936f0313965SErez Shitrit if (MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) { 937f0313965SErez Shitrit props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM; 938e945c653SJason Gunthorpe props->kernel_cap_flags |= IBK_UD_TSO; 939f0313965SErez Shitrit } 940f0313965SErez Shitrit 94103404e8aSMaor Gottlieb if (MLX5_CAP_GEN(dev->mdev, rq_delay_drop) && 94285c7c014SDaniel Jurgens MLX5_CAP_GEN(dev->mdev, general_notification_event) && 94385c7c014SDaniel Jurgens raw_support) 94403404e8aSMaor Gottlieb props->raw_packet_caps |= IB_RAW_PACKET_CAP_DELAY_DROP; 94503404e8aSMaor Gottlieb 9461d54f890SYishai Hadas if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads) && 9471d54f890SYishai Hadas MLX5_CAP_IPOIB_ENHANCED(mdev, csum_cap)) 9481d54f890SYishai Hadas props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM; 9491d54f890SYishai Hadas 950cff5a0f3SMajd Dibbiny if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) && 95185c7c014SDaniel Jurgens MLX5_CAP_ETH(dev->mdev, scatter_fcs) && 95285c7c014SDaniel Jurgens raw_support) { 953e8161334SNoa Osherovich /* Legacy bit to support old userspace libraries */ 954cff5a0f3SMajd Dibbiny props->device_cap_flags |= IB_DEVICE_RAW_SCATTER_FCS; 955e8161334SNoa Osherovich props->raw_packet_caps |= IB_RAW_PACKET_CAP_SCATTER_FCS; 956e8161334SNoa Osherovich } 957cff5a0f3SMajd Dibbiny 95824da0016SAriel Levkovich if (MLX5_CAP_DEV_MEM(mdev, memic)) { 95924da0016SAriel Levkovich props->max_dm_size = 96024da0016SAriel Levkovich MLX5_CAP_DEV_MEM(mdev, max_memic_size); 96124da0016SAriel Levkovich } 96224da0016SAriel Levkovich 963da6d6ba3SMaor Gottlieb if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS)) 964da6d6ba3SMaor Gottlieb props->device_cap_flags |= IB_DEVICE_MANAGED_FLOW_STEERING; 965da6d6ba3SMaor Gottlieb 966b1383aa6SNoa Osherovich if (MLX5_CAP_GEN(mdev, end_pad)) 967b1383aa6SNoa Osherovich props->device_cap_flags |= IB_DEVICE_PCI_WRITE_END_PADDING; 968b1383aa6SNoa Osherovich 9691b5daf11SMajd Dibbiny props->vendor_part_id = mdev->pdev->device; 9701b5daf11SMajd Dibbiny props->hw_ver = mdev->pdev->revision; 971e126ba97SEli Cohen 972e126ba97SEli Cohen props->max_mr_size = ~0ull; 973e0238a6aSSagi Grimberg props->page_size_cap = ~(min_page_size - 1); 974938fe83cSSaeed Mahameed props->max_qp = 1 << MLX5_CAP_GEN(mdev, log_max_qp); 975938fe83cSSaeed Mahameed props->max_qp_wr = 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz); 976938fe83cSSaeed Mahameed max_rq_sg = MLX5_CAP_GEN(mdev, max_wqe_sz_rq) / 977938fe83cSSaeed Mahameed sizeof(struct mlx5_wqe_data_seg); 978288c01b7SEli Cohen max_sq_desc = min_t(int, MLX5_CAP_GEN(mdev, max_wqe_sz_sq), 512); 979288c01b7SEli Cohen max_sq_sg = (max_sq_desc - sizeof(struct mlx5_wqe_ctrl_seg) - 980288c01b7SEli Cohen sizeof(struct mlx5_wqe_raddr_seg)) / 981e126ba97SEli Cohen sizeof(struct mlx5_wqe_data_seg); 98233023fb8SSteve Wise props->max_send_sge = max_sq_sg; 98333023fb8SSteve Wise props->max_recv_sge = max_rq_sg; 984986ef95eSSagi Grimberg props->max_sge_rd = MLX5_MAX_SGE_RD; 985938fe83cSSaeed Mahameed props->max_cq = 1 << MLX5_CAP_GEN(mdev, log_max_cq); 9869f177686SLeon Romanovsky props->max_cqe = (1 << MLX5_CAP_GEN(mdev, log_max_cq_sz)) - 1; 987938fe83cSSaeed Mahameed props->max_mr = 1 << MLX5_CAP_GEN(mdev, log_max_mkey); 988938fe83cSSaeed Mahameed props->max_pd = 1 << MLX5_CAP_GEN(mdev, log_max_pd); 989938fe83cSSaeed Mahameed props->max_qp_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_req_qp); 990938fe83cSSaeed Mahameed props->max_qp_init_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_res_qp); 991938fe83cSSaeed Mahameed props->max_srq = 1 << MLX5_CAP_GEN(mdev, log_max_srq); 992938fe83cSSaeed Mahameed props->max_srq_wr = (1 << MLX5_CAP_GEN(mdev, log_max_srq_sz)) - 1; 993938fe83cSSaeed Mahameed props->local_ca_ack_delay = MLX5_CAP_GEN(mdev, local_ca_ack_delay); 994e126ba97SEli Cohen props->max_res_rd_atom = props->max_qp_rd_atom * props->max_qp; 995e126ba97SEli Cohen props->max_srq_sge = max_rq_sg - 1; 996911f4331SSagi Grimberg props->max_fast_reg_page_list_len = 997911f4331SSagi Grimberg 1 << MLX5_CAP_GEN(mdev, log_max_klm_list_size); 99862e3c379SMax Gurtovoy props->max_pi_fast_reg_page_list_len = 99962e3c379SMax Gurtovoy props->max_fast_reg_page_list_len / 2; 100036609056SYamin Friedman props->max_sgl_rd = 100136609056SYamin Friedman MLX5_CAP_GEN(mdev, max_sgl_for_optimized_performance); 1002776a3906SMoni Shoua get_atomic_caps_qp(dev, props); 100381bea28fSEli Cohen props->masked_atomic_cap = IB_ATOMIC_NONE; 1004938fe83cSSaeed Mahameed props->max_mcast_grp = 1 << MLX5_CAP_GEN(mdev, log_max_mcg); 1005938fe83cSSaeed Mahameed props->max_mcast_qp_attach = MLX5_CAP_GEN(mdev, max_qp_mcg); 1006e126ba97SEli Cohen props->max_total_mcast_qp_attach = props->max_mcast_qp_attach * 1007e126ba97SEli Cohen props->max_mcast_grp; 100886695a65SMaor Gottlieb props->max_ah = INT_MAX; 10097c60bcbbSMatan Barak props->hca_core_clock = MLX5_CAP_GEN(mdev, device_frequency_khz); 10107c60bcbbSMatan Barak props->timestamp_mask = 0x7FFFFFFFFFFFFFFFULL; 1011e126ba97SEli Cohen 1012e502b8b0SLeon Romanovsky if (IS_ENABLED(CONFIG_INFINIBAND_ON_DEMAND_PAGING)) { 101300815752SMoni Shoua if (dev->odp_caps.general_caps & IB_ODP_SUPPORT) 1014e945c653SJason Gunthorpe props->kernel_cap_flags |= IBK_ON_DEMAND_PAGING; 10158cdd312cSHaggai Eran props->odp_caps = dev->odp_caps; 1016a73a8955SMoni Shoua if (!uhw) { 1017a73a8955SMoni Shoua /* ODP for kernel QPs is not implemented for receive 1018a73a8955SMoni Shoua * WQEs and SRQ WQEs 1019a73a8955SMoni Shoua */ 1020a73a8955SMoni Shoua props->odp_caps.per_transport_caps.rc_odp_caps &= 1021a73a8955SMoni Shoua ~(IB_ODP_SUPPORT_READ | 1022a73a8955SMoni Shoua IB_ODP_SUPPORT_SRQ_RECV); 1023a73a8955SMoni Shoua props->odp_caps.per_transport_caps.uc_odp_caps &= 1024a73a8955SMoni Shoua ~(IB_ODP_SUPPORT_READ | 1025a73a8955SMoni Shoua IB_ODP_SUPPORT_SRQ_RECV); 1026a73a8955SMoni Shoua props->odp_caps.per_transport_caps.ud_odp_caps &= 1027a73a8955SMoni Shoua ~(IB_ODP_SUPPORT_READ | 1028a73a8955SMoni Shoua IB_ODP_SUPPORT_SRQ_RECV); 1029a73a8955SMoni Shoua props->odp_caps.per_transport_caps.xrc_odp_caps &= 1030a73a8955SMoni Shoua ~(IB_ODP_SUPPORT_READ | 1031a73a8955SMoni Shoua IB_ODP_SUPPORT_SRQ_RECV); 1032a73a8955SMoni Shoua } 1033e502b8b0SLeon Romanovsky } 10348cdd312cSHaggai Eran 1035e53a9d26SParav Pandit if (mlx5_core_is_vf(mdev)) 1036e945c653SJason Gunthorpe props->kernel_cap_flags |= IBK_VIRTUAL_FUNCTION; 1037eff901d3SEli Cohen 103831f69a82SYishai Hadas if (mlx5_ib_port_link_layer(ibdev, 1) == 103985c7c014SDaniel Jurgens IB_LINK_LAYER_ETHERNET && raw_support) { 104031f69a82SYishai Hadas props->rss_caps.max_rwq_indirection_tables = 104131f69a82SYishai Hadas 1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt); 104231f69a82SYishai Hadas props->rss_caps.max_rwq_indirection_table_size = 104331f69a82SYishai Hadas 1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt_size); 104431f69a82SYishai Hadas props->rss_caps.supported_qpts = 1 << IB_QPT_RAW_PACKET; 104531f69a82SYishai Hadas props->max_wq_type_rq = 104631f69a82SYishai Hadas 1 << MLX5_CAP_GEN(dev->mdev, log_max_rq); 104731f69a82SYishai Hadas } 104831f69a82SYishai Hadas 1049eb761894SArtemy Kovalyov if (MLX5_CAP_GEN(mdev, tag_matching)) { 105078b1beb0SLeon Romanovsky props->tm_caps.max_num_tags = 1051eb761894SArtemy Kovalyov (1 << MLX5_CAP_GEN(mdev, log_tag_matching_list_sz)) - 1; 105278b1beb0SLeon Romanovsky props->tm_caps.max_ops = 1053eb761894SArtemy Kovalyov 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz); 105478b1beb0SLeon Romanovsky props->tm_caps.max_sge = MLX5_TM_MAX_SGE; 1055eb761894SArtemy Kovalyov } 1056eb761894SArtemy Kovalyov 105789705e92SDanit Goldberg if (MLX5_CAP_GEN(mdev, tag_matching) && 105889705e92SDanit Goldberg MLX5_CAP_GEN(mdev, rndv_offload_rc)) { 105989705e92SDanit Goldberg props->tm_caps.flags = IB_TM_CAP_RNDV_RC; 106089705e92SDanit Goldberg props->tm_caps.max_rndv_hdr_size = MLX5_TM_MAX_RNDV_MSG_SIZE; 106189705e92SDanit Goldberg } 106289705e92SDanit Goldberg 106387ab3f52SYonatan Cohen if (MLX5_CAP_GEN(dev->mdev, cq_moderation)) { 106487ab3f52SYonatan Cohen props->cq_caps.max_cq_moderation_count = 106587ab3f52SYonatan Cohen MLX5_MAX_CQ_COUNT; 106687ab3f52SYonatan Cohen props->cq_caps.max_cq_moderation_period = 106787ab3f52SYonatan Cohen MLX5_MAX_CQ_PERIOD; 106887ab3f52SYonatan Cohen } 106987ab3f52SYonatan Cohen 1070a762d460SLeon Romanovsky if (offsetofend(typeof(resp), cqe_comp_caps) <= uhw_outlen) { 1071572f46bfSYonatan Cohen resp.response_length += sizeof(resp.cqe_comp_caps); 1072572f46bfSYonatan Cohen 1073572f46bfSYonatan Cohen if (MLX5_CAP_GEN(dev->mdev, cqe_compression)) { 10747e43a2a5SBodong Wang resp.cqe_comp_caps.max_num = 1075572f46bfSYonatan Cohen MLX5_CAP_GEN(dev->mdev, 1076572f46bfSYonatan Cohen cqe_compression_max_num); 1077572f46bfSYonatan Cohen 10787e43a2a5SBodong Wang resp.cqe_comp_caps.supported_format = 10797e43a2a5SBodong Wang MLX5_IB_CQE_RES_FORMAT_HASH | 10807e43a2a5SBodong Wang MLX5_IB_CQE_RES_FORMAT_CSUM; 10816f1006a4SYonatan Cohen 10826f1006a4SYonatan Cohen if (MLX5_CAP_GEN(dev->mdev, mini_cqe_resp_stride_index)) 10836f1006a4SYonatan Cohen resp.cqe_comp_caps.supported_format |= 10846f1006a4SYonatan Cohen MLX5_IB_CQE_RES_FORMAT_CSUM_STRIDX; 1085572f46bfSYonatan Cohen } 10867e43a2a5SBodong Wang } 10877e43a2a5SBodong Wang 1088a762d460SLeon Romanovsky if (offsetofend(typeof(resp), packet_pacing_caps) <= uhw_outlen && 108985c7c014SDaniel Jurgens raw_support) { 1090d949167dSBodong Wang if (MLX5_CAP_QOS(mdev, packet_pacing) && 1091d949167dSBodong Wang MLX5_CAP_GEN(mdev, qos)) { 1092d949167dSBodong Wang resp.packet_pacing_caps.qp_rate_limit_max = 1093d949167dSBodong Wang MLX5_CAP_QOS(mdev, packet_pacing_max_rate); 1094d949167dSBodong Wang resp.packet_pacing_caps.qp_rate_limit_min = 1095d949167dSBodong Wang MLX5_CAP_QOS(mdev, packet_pacing_min_rate); 1096d949167dSBodong Wang resp.packet_pacing_caps.supported_qpts |= 1097d949167dSBodong Wang 1 << IB_QPT_RAW_PACKET; 109861147f39SBodong Wang if (MLX5_CAP_QOS(mdev, packet_pacing_burst_bound) && 109961147f39SBodong Wang MLX5_CAP_QOS(mdev, packet_pacing_typical_size)) 110061147f39SBodong Wang resp.packet_pacing_caps.cap_flags |= 110161147f39SBodong Wang MLX5_IB_PP_SUPPORT_BURST; 1102d949167dSBodong Wang } 1103d949167dSBodong Wang resp.response_length += sizeof(resp.packet_pacing_caps); 1104d949167dSBodong Wang } 1105d949167dSBodong Wang 1106a762d460SLeon Romanovsky if (offsetofend(typeof(resp), mlx5_ib_support_multi_pkt_send_wqes) <= 1107a762d460SLeon Romanovsky uhw_outlen) { 1108795b609cSBodong Wang if (MLX5_CAP_ETH(mdev, multi_pkt_send_wqe)) 11099f885201SLeon Romanovsky resp.mlx5_ib_support_multi_pkt_send_wqes = 1110795b609cSBodong Wang MLX5_IB_ALLOW_MPW; 1111050da902SBodong Wang 1112050da902SBodong Wang if (MLX5_CAP_ETH(mdev, enhanced_multi_pkt_send_wqe)) 1113050da902SBodong Wang resp.mlx5_ib_support_multi_pkt_send_wqes |= 1114050da902SBodong Wang MLX5_IB_SUPPORT_EMPW; 1115050da902SBodong Wang 11169f885201SLeon Romanovsky resp.response_length += 11179f885201SLeon Romanovsky sizeof(resp.mlx5_ib_support_multi_pkt_send_wqes); 11189f885201SLeon Romanovsky } 11199f885201SLeon Romanovsky 1120a762d460SLeon Romanovsky if (offsetofend(typeof(resp), flags) <= uhw_outlen) { 1121de57f2adSGuy Levi resp.response_length += sizeof(resp.flags); 11227a0c8f42SGuy Levi 1123de57f2adSGuy Levi if (MLX5_CAP_GEN(mdev, cqe_compression_128)) 1124de57f2adSGuy Levi resp.flags |= 1125de57f2adSGuy Levi MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_COMP; 11267a0c8f42SGuy Levi 11277a0c8f42SGuy Levi if (MLX5_CAP_GEN(mdev, cqe_128_always)) 11287a0c8f42SGuy Levi resp.flags |= MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_PAD; 11297e11b911SDanit Goldberg if (MLX5_CAP_GEN(mdev, qp_packet_based)) 11307e11b911SDanit Goldberg resp.flags |= 11317e11b911SDanit Goldberg MLX5_IB_QUERY_DEV_RESP_PACKET_BASED_CREDIT_MODE; 11327249c8eaSGuy Levi 11337249c8eaSGuy Levi resp.flags |= MLX5_IB_QUERY_DEV_RESP_FLAGS_SCAT2CQE_DCT; 1134de57f2adSGuy Levi } 11359f885201SLeon Romanovsky 1136a762d460SLeon Romanovsky if (offsetofend(typeof(resp), sw_parsing_caps) <= uhw_outlen) { 113796dc3fc5SNoa Osherovich resp.response_length += sizeof(resp.sw_parsing_caps); 113896dc3fc5SNoa Osherovich if (MLX5_CAP_ETH(mdev, swp)) { 113996dc3fc5SNoa Osherovich resp.sw_parsing_caps.sw_parsing_offloads |= 114096dc3fc5SNoa Osherovich MLX5_IB_SW_PARSING; 114196dc3fc5SNoa Osherovich 114296dc3fc5SNoa Osherovich if (MLX5_CAP_ETH(mdev, swp_csum)) 114396dc3fc5SNoa Osherovich resp.sw_parsing_caps.sw_parsing_offloads |= 114496dc3fc5SNoa Osherovich MLX5_IB_SW_PARSING_CSUM; 114596dc3fc5SNoa Osherovich 114696dc3fc5SNoa Osherovich if (MLX5_CAP_ETH(mdev, swp_lso)) 114796dc3fc5SNoa Osherovich resp.sw_parsing_caps.sw_parsing_offloads |= 114896dc3fc5SNoa Osherovich MLX5_IB_SW_PARSING_LSO; 114996dc3fc5SNoa Osherovich 115096dc3fc5SNoa Osherovich if (resp.sw_parsing_caps.sw_parsing_offloads) 115196dc3fc5SNoa Osherovich resp.sw_parsing_caps.supported_qpts = 115296dc3fc5SNoa Osherovich BIT(IB_QPT_RAW_PACKET); 115396dc3fc5SNoa Osherovich } 115496dc3fc5SNoa Osherovich } 115596dc3fc5SNoa Osherovich 1156a762d460SLeon Romanovsky if (offsetofend(typeof(resp), striding_rq_caps) <= uhw_outlen && 115785c7c014SDaniel Jurgens raw_support) { 1158b4f34597SNoa Osherovich resp.response_length += sizeof(resp.striding_rq_caps); 1159b4f34597SNoa Osherovich if (MLX5_CAP_GEN(mdev, striding_rq)) { 1160b4f34597SNoa Osherovich resp.striding_rq_caps.min_single_stride_log_num_of_bytes = 1161b4f34597SNoa Osherovich MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES; 1162b4f34597SNoa Osherovich resp.striding_rq_caps.max_single_stride_log_num_of_bytes = 1163b4f34597SNoa Osherovich MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES; 1164c16339b6SMark Zhang if (MLX5_CAP_GEN(dev->mdev, ext_stride_num_range)) 1165c16339b6SMark Zhang resp.striding_rq_caps 1166c16339b6SMark Zhang .min_single_wqe_log_num_of_strides = 1167c16339b6SMark Zhang MLX5_EXT_MIN_SINGLE_WQE_LOG_NUM_STRIDES; 1168c16339b6SMark Zhang else 1169c16339b6SMark Zhang resp.striding_rq_caps 1170c16339b6SMark Zhang .min_single_wqe_log_num_of_strides = 1171b4f34597SNoa Osherovich MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES; 1172b4f34597SNoa Osherovich resp.striding_rq_caps.max_single_wqe_log_num_of_strides = 1173b4f34597SNoa Osherovich MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES; 1174b4f34597SNoa Osherovich resp.striding_rq_caps.supported_qpts = 1175b4f34597SNoa Osherovich BIT(IB_QPT_RAW_PACKET); 1176b4f34597SNoa Osherovich } 1177b4f34597SNoa Osherovich } 1178b4f34597SNoa Osherovich 1179a762d460SLeon Romanovsky if (offsetofend(typeof(resp), tunnel_offloads_caps) <= uhw_outlen) { 1180f95ef6cbSMaor Gottlieb resp.response_length += sizeof(resp.tunnel_offloads_caps); 1181f95ef6cbSMaor Gottlieb if (MLX5_CAP_ETH(mdev, tunnel_stateless_vxlan)) 1182f95ef6cbSMaor Gottlieb resp.tunnel_offloads_caps |= 1183f95ef6cbSMaor Gottlieb MLX5_IB_TUNNELED_OFFLOADS_VXLAN; 1184f95ef6cbSMaor Gottlieb if (MLX5_CAP_ETH(mdev, tunnel_stateless_geneve_rx)) 1185f95ef6cbSMaor Gottlieb resp.tunnel_offloads_caps |= 1186f95ef6cbSMaor Gottlieb MLX5_IB_TUNNELED_OFFLOADS_GENEVE; 1187f95ef6cbSMaor Gottlieb if (MLX5_CAP_ETH(mdev, tunnel_stateless_gre)) 1188f95ef6cbSMaor Gottlieb resp.tunnel_offloads_caps |= 1189f95ef6cbSMaor Gottlieb MLX5_IB_TUNNELED_OFFLOADS_GRE; 119041e684efSAlex Vesker if (MLX5_CAP_ETH(mdev, tunnel_stateless_mpls_over_gre)) 1191e818e255SAriel Levkovich resp.tunnel_offloads_caps |= 1192e818e255SAriel Levkovich MLX5_IB_TUNNELED_OFFLOADS_MPLS_GRE; 119341e684efSAlex Vesker if (MLX5_CAP_ETH(mdev, tunnel_stateless_mpls_over_udp)) 1194e818e255SAriel Levkovich resp.tunnel_offloads_caps |= 1195e818e255SAriel Levkovich MLX5_IB_TUNNELED_OFFLOADS_MPLS_UDP; 1196f95ef6cbSMaor Gottlieb } 1197f95ef6cbSMaor Gottlieb 119811656f59SLior Nahmanson if (offsetofend(typeof(resp), dci_streams_caps) <= uhw_outlen) { 119911656f59SLior Nahmanson resp.response_length += sizeof(resp.dci_streams_caps); 120011656f59SLior Nahmanson 120111656f59SLior Nahmanson resp.dci_streams_caps.max_log_num_concurent = 120211656f59SLior Nahmanson MLX5_CAP_GEN(mdev, log_max_dci_stream_channels); 120311656f59SLior Nahmanson 120411656f59SLior Nahmanson resp.dci_streams_caps.max_log_num_errored = 120511656f59SLior Nahmanson MLX5_CAP_GEN(mdev, log_max_dci_errored_streams); 120611656f59SLior Nahmanson } 120711656f59SLior Nahmanson 120848357091SLeon Romanovsky if (uhw_outlen) { 1209402ca536SBodong Wang err = ib_copy_to_udata(uhw, &resp, resp.response_length); 1210402ca536SBodong Wang 1211402ca536SBodong Wang if (err) 1212402ca536SBodong Wang return err; 1213402ca536SBodong Wang } 1214402ca536SBodong Wang 12151b5daf11SMajd Dibbiny return 0; 12161b5daf11SMajd Dibbiny } 1217e126ba97SEli Cohen 1218639bf441SAharon Landau static void translate_active_width(struct ib_device *ibdev, u16 active_width, 12191b5daf11SMajd Dibbiny u8 *ib_width) 12201b5daf11SMajd Dibbiny { 12211b5daf11SMajd Dibbiny struct mlx5_ib_dev *dev = to_mdev(ibdev); 12221b5daf11SMajd Dibbiny 1223e27014bdSAharon Landau if (active_width & MLX5_PTYS_WIDTH_1X) 12241b5daf11SMajd Dibbiny *ib_width = IB_WIDTH_1X; 1225e27014bdSAharon Landau else if (active_width & MLX5_PTYS_WIDTH_2X) 1226d764970bSMichael Guralnik *ib_width = IB_WIDTH_2X; 1227e27014bdSAharon Landau else if (active_width & MLX5_PTYS_WIDTH_4X) 12281b5daf11SMajd Dibbiny *ib_width = IB_WIDTH_4X; 1229e27014bdSAharon Landau else if (active_width & MLX5_PTYS_WIDTH_8X) 12301b5daf11SMajd Dibbiny *ib_width = IB_WIDTH_8X; 1231e27014bdSAharon Landau else if (active_width & MLX5_PTYS_WIDTH_12X) 12321b5daf11SMajd Dibbiny *ib_width = IB_WIDTH_12X; 1233db7a691aSMichael Guralnik else { 1234db7a691aSMichael Guralnik mlx5_ib_dbg(dev, "Invalid active_width %d, setting width to default value: 4x\n", 1235e27014bdSAharon Landau active_width); 1236db7a691aSMichael Guralnik *ib_width = IB_WIDTH_4X; 12371b5daf11SMajd Dibbiny } 12381b5daf11SMajd Dibbiny 1239db7a691aSMichael Guralnik return; 12401b5daf11SMajd Dibbiny } 12411b5daf11SMajd Dibbiny 12421b5daf11SMajd Dibbiny static int mlx5_mtu_to_ib_mtu(int mtu) 12431b5daf11SMajd Dibbiny { 12441b5daf11SMajd Dibbiny switch (mtu) { 12451b5daf11SMajd Dibbiny case 256: return 1; 12461b5daf11SMajd Dibbiny case 512: return 2; 12471b5daf11SMajd Dibbiny case 1024: return 3; 12481b5daf11SMajd Dibbiny case 2048: return 4; 12491b5daf11SMajd Dibbiny case 4096: return 5; 12501b5daf11SMajd Dibbiny default: 12511b5daf11SMajd Dibbiny pr_warn("invalid mtu\n"); 12521b5daf11SMajd Dibbiny return -1; 12531b5daf11SMajd Dibbiny } 12541b5daf11SMajd Dibbiny } 12551b5daf11SMajd Dibbiny 12561b5daf11SMajd Dibbiny enum ib_max_vl_num { 12571b5daf11SMajd Dibbiny __IB_MAX_VL_0 = 1, 12581b5daf11SMajd Dibbiny __IB_MAX_VL_0_1 = 2, 12591b5daf11SMajd Dibbiny __IB_MAX_VL_0_3 = 3, 12601b5daf11SMajd Dibbiny __IB_MAX_VL_0_7 = 4, 12611b5daf11SMajd Dibbiny __IB_MAX_VL_0_14 = 5, 12621b5daf11SMajd Dibbiny }; 12631b5daf11SMajd Dibbiny 12641b5daf11SMajd Dibbiny enum mlx5_vl_hw_cap { 12651b5daf11SMajd Dibbiny MLX5_VL_HW_0 = 1, 12661b5daf11SMajd Dibbiny MLX5_VL_HW_0_1 = 2, 12671b5daf11SMajd Dibbiny MLX5_VL_HW_0_2 = 3, 12681b5daf11SMajd Dibbiny MLX5_VL_HW_0_3 = 4, 12691b5daf11SMajd Dibbiny MLX5_VL_HW_0_4 = 5, 12701b5daf11SMajd Dibbiny MLX5_VL_HW_0_5 = 6, 12711b5daf11SMajd Dibbiny MLX5_VL_HW_0_6 = 7, 12721b5daf11SMajd Dibbiny MLX5_VL_HW_0_7 = 8, 12731b5daf11SMajd Dibbiny MLX5_VL_HW_0_14 = 15 12741b5daf11SMajd Dibbiny }; 12751b5daf11SMajd Dibbiny 12761b5daf11SMajd Dibbiny static int translate_max_vl_num(struct ib_device *ibdev, u8 vl_hw_cap, 12771b5daf11SMajd Dibbiny u8 *max_vl_num) 12781b5daf11SMajd Dibbiny { 12791b5daf11SMajd Dibbiny switch (vl_hw_cap) { 12801b5daf11SMajd Dibbiny case MLX5_VL_HW_0: 12811b5daf11SMajd Dibbiny *max_vl_num = __IB_MAX_VL_0; 12821b5daf11SMajd Dibbiny break; 12831b5daf11SMajd Dibbiny case MLX5_VL_HW_0_1: 12841b5daf11SMajd Dibbiny *max_vl_num = __IB_MAX_VL_0_1; 12851b5daf11SMajd Dibbiny break; 12861b5daf11SMajd Dibbiny case MLX5_VL_HW_0_3: 12871b5daf11SMajd Dibbiny *max_vl_num = __IB_MAX_VL_0_3; 12881b5daf11SMajd Dibbiny break; 12891b5daf11SMajd Dibbiny case MLX5_VL_HW_0_7: 12901b5daf11SMajd Dibbiny *max_vl_num = __IB_MAX_VL_0_7; 12911b5daf11SMajd Dibbiny break; 12921b5daf11SMajd Dibbiny case MLX5_VL_HW_0_14: 12931b5daf11SMajd Dibbiny *max_vl_num = __IB_MAX_VL_0_14; 12941b5daf11SMajd Dibbiny break; 12951b5daf11SMajd Dibbiny 12961b5daf11SMajd Dibbiny default: 12971b5daf11SMajd Dibbiny return -EINVAL; 12981b5daf11SMajd Dibbiny } 12991b5daf11SMajd Dibbiny 13001b5daf11SMajd Dibbiny return 0; 13011b5daf11SMajd Dibbiny } 13021b5daf11SMajd Dibbiny 13031fb7f897SMark Bloch static int mlx5_query_hca_port(struct ib_device *ibdev, u32 port, 13041b5daf11SMajd Dibbiny struct ib_port_attr *props) 13051b5daf11SMajd Dibbiny { 13061b5daf11SMajd Dibbiny struct mlx5_ib_dev *dev = to_mdev(ibdev); 13071b5daf11SMajd Dibbiny struct mlx5_core_dev *mdev = dev->mdev; 13081b5daf11SMajd Dibbiny struct mlx5_hca_vport_context *rep; 1309046339eaSSaeed Mahameed u16 max_mtu; 1310046339eaSSaeed Mahameed u16 oper_mtu; 13111b5daf11SMajd Dibbiny int err; 1312639bf441SAharon Landau u16 ib_link_width_oper; 13131b5daf11SMajd Dibbiny u8 vl_hw_cap; 13141b5daf11SMajd Dibbiny 13151b5daf11SMajd Dibbiny rep = kzalloc(sizeof(*rep), GFP_KERNEL); 13161b5daf11SMajd Dibbiny if (!rep) { 13171b5daf11SMajd Dibbiny err = -ENOMEM; 13181b5daf11SMajd Dibbiny goto out; 13191b5daf11SMajd Dibbiny } 13201b5daf11SMajd Dibbiny 1321c4550c63SOr Gerlitz /* props being zeroed by the caller, avoid zeroing it here */ 13221b5daf11SMajd Dibbiny 13231b5daf11SMajd Dibbiny err = mlx5_query_hca_vport_context(mdev, 0, port, 0, rep); 13241b5daf11SMajd Dibbiny if (err) 13251b5daf11SMajd Dibbiny goto out; 13261b5daf11SMajd Dibbiny 13271b5daf11SMajd Dibbiny props->lid = rep->lid; 13281b5daf11SMajd Dibbiny props->lmc = rep->lmc; 13291b5daf11SMajd Dibbiny props->sm_lid = rep->sm_lid; 13301b5daf11SMajd Dibbiny props->sm_sl = rep->sm_sl; 13311b5daf11SMajd Dibbiny props->state = rep->vport_state; 13321b5daf11SMajd Dibbiny props->phys_state = rep->port_physical_state; 13331b5daf11SMajd Dibbiny props->port_cap_flags = rep->cap_mask1; 13341b5daf11SMajd Dibbiny props->gid_tbl_len = mlx5_get_gid_table_len(MLX5_CAP_GEN(mdev, gid_table_size)); 13351b5daf11SMajd Dibbiny props->max_msg_sz = 1 << MLX5_CAP_GEN(mdev, log_max_msg); 13361b5daf11SMajd Dibbiny props->pkey_tbl_len = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, pkey_table_size)); 13371b5daf11SMajd Dibbiny props->bad_pkey_cntr = rep->pkey_violation_counter; 13381b5daf11SMajd Dibbiny props->qkey_viol_cntr = rep->qkey_violation_counter; 13391b5daf11SMajd Dibbiny props->subnet_timeout = rep->subnet_timeout; 13401b5daf11SMajd Dibbiny props->init_type_reply = rep->init_type_reply; 13411b5daf11SMajd Dibbiny 13424106a758SMichael Guralnik if (props->port_cap_flags & IB_PORT_CAP_MASK2_SUP) 13434106a758SMichael Guralnik props->port_cap_flags2 = rep->cap_mask2; 13444106a758SMichael Guralnik 1345639bf441SAharon Landau err = mlx5_query_ib_port_oper(mdev, &ib_link_width_oper, 1346376ceb31SAharon Landau &props->active_speed, port); 13471b5daf11SMajd Dibbiny if (err) 13481b5daf11SMajd Dibbiny goto out; 13491b5daf11SMajd Dibbiny 1350db7a691aSMichael Guralnik translate_active_width(ibdev, ib_link_width_oper, &props->active_width); 1351db7a691aSMichael Guralnik 1352facc9699SSaeed Mahameed mlx5_query_port_max_mtu(mdev, &max_mtu, port); 13531b5daf11SMajd Dibbiny 13541b5daf11SMajd Dibbiny props->max_mtu = mlx5_mtu_to_ib_mtu(max_mtu); 13551b5daf11SMajd Dibbiny 1356facc9699SSaeed Mahameed mlx5_query_port_oper_mtu(mdev, &oper_mtu, port); 13571b5daf11SMajd Dibbiny 13581b5daf11SMajd Dibbiny props->active_mtu = mlx5_mtu_to_ib_mtu(oper_mtu); 13591b5daf11SMajd Dibbiny 13601b5daf11SMajd Dibbiny err = mlx5_query_port_vl_hw_cap(mdev, &vl_hw_cap, port); 13611b5daf11SMajd Dibbiny if (err) 13621b5daf11SMajd Dibbiny goto out; 13631b5daf11SMajd Dibbiny 13641b5daf11SMajd Dibbiny err = translate_max_vl_num(ibdev, vl_hw_cap, 13651b5daf11SMajd Dibbiny &props->max_vl_num); 13661b5daf11SMajd Dibbiny out: 13671b5daf11SMajd Dibbiny kfree(rep); 1368e126ba97SEli Cohen return err; 1369e126ba97SEli Cohen } 1370e126ba97SEli Cohen 13711fb7f897SMark Bloch int mlx5_ib_query_port(struct ib_device *ibdev, u32 port, 1372e126ba97SEli Cohen struct ib_port_attr *props) 1373e126ba97SEli Cohen { 1374095b0927SIlan Tayari unsigned int count; 1375095b0927SIlan Tayari int ret; 1376095b0927SIlan Tayari 13771b5daf11SMajd Dibbiny switch (mlx5_get_vport_access_method(ibdev)) { 13781b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_MAD: 1379095b0927SIlan Tayari ret = mlx5_query_mad_ifc_port(ibdev, port, props); 1380095b0927SIlan Tayari break; 1381e126ba97SEli Cohen 13821b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_HCA: 1383095b0927SIlan Tayari ret = mlx5_query_hca_port(ibdev, port, props); 1384095b0927SIlan Tayari break; 13851b5daf11SMajd Dibbiny 13863f89a643SAchiad Shochat case MLX5_VPORT_ACCESS_METHOD_NIC: 1387095b0927SIlan Tayari ret = mlx5_query_port_roce(ibdev, port, props); 1388095b0927SIlan Tayari break; 13893f89a643SAchiad Shochat 13901b5daf11SMajd Dibbiny default: 1391095b0927SIlan Tayari ret = -EINVAL; 1392e126ba97SEli Cohen } 1393095b0927SIlan Tayari 1394095b0927SIlan Tayari if (!ret && props) { 1395b3cbd6f0SDaniel Jurgens struct mlx5_ib_dev *dev = to_mdev(ibdev); 1396b3cbd6f0SDaniel Jurgens struct mlx5_core_dev *mdev; 1397b3cbd6f0SDaniel Jurgens bool put_mdev = true; 1398b3cbd6f0SDaniel Jurgens 1399b3cbd6f0SDaniel Jurgens mdev = mlx5_ib_get_native_port_mdev(dev, port, NULL); 1400b3cbd6f0SDaniel Jurgens if (!mdev) { 1401b3cbd6f0SDaniel Jurgens /* If the port isn't affiliated yet query the master. 1402b3cbd6f0SDaniel Jurgens * The master and slave will have the same values. 1403b3cbd6f0SDaniel Jurgens */ 1404b3cbd6f0SDaniel Jurgens mdev = dev->mdev; 1405b3cbd6f0SDaniel Jurgens port = 1; 1406b3cbd6f0SDaniel Jurgens put_mdev = false; 1407b3cbd6f0SDaniel Jurgens } 1408b3cbd6f0SDaniel Jurgens count = mlx5_core_reserved_gids_count(mdev); 1409b3cbd6f0SDaniel Jurgens if (put_mdev) 1410b3cbd6f0SDaniel Jurgens mlx5_ib_put_native_port_mdev(dev, port); 1411095b0927SIlan Tayari props->gid_tbl_len -= count; 1412095b0927SIlan Tayari } 1413095b0927SIlan Tayari return ret; 1414e126ba97SEli Cohen } 1415e126ba97SEli Cohen 14161fb7f897SMark Bloch static int mlx5_ib_rep_query_port(struct ib_device *ibdev, u32 port, 14178e6efa3aSMark Bloch struct ib_port_attr *props) 14188e6efa3aSMark Bloch { 14197a58779eSParav Pandit return mlx5_query_port_roce(ibdev, port, props); 14208e6efa3aSMark Bloch } 14218e6efa3aSMark Bloch 14221fb7f897SMark Bloch static int mlx5_ib_rep_query_pkey(struct ib_device *ibdev, u32 port, u16 index, 1423d6fd59e1SParav Pandit u16 *pkey) 1424d6fd59e1SParav Pandit { 1425d6fd59e1SParav Pandit /* Default special Pkey for representor device port as per the 1426d6fd59e1SParav Pandit * IB specification 1.3 section 10.9.1.2. 1427d6fd59e1SParav Pandit */ 1428d6fd59e1SParav Pandit *pkey = 0xffff; 1429d6fd59e1SParav Pandit return 0; 1430d6fd59e1SParav Pandit } 1431d6fd59e1SParav Pandit 14321fb7f897SMark Bloch static int mlx5_ib_query_gid(struct ib_device *ibdev, u32 port, int index, 1433e126ba97SEli Cohen union ib_gid *gid) 1434e126ba97SEli Cohen { 14351b5daf11SMajd Dibbiny struct mlx5_ib_dev *dev = to_mdev(ibdev); 14361b5daf11SMajd Dibbiny struct mlx5_core_dev *mdev = dev->mdev; 1437e126ba97SEli Cohen 14381b5daf11SMajd Dibbiny switch (mlx5_get_vport_access_method(ibdev)) { 14391b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_MAD: 14401b5daf11SMajd Dibbiny return mlx5_query_mad_ifc_gids(ibdev, port, index, gid); 1441e126ba97SEli Cohen 14421b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_HCA: 14431b5daf11SMajd Dibbiny return mlx5_query_hca_vport_gid(mdev, 0, port, 0, index, gid); 1444e126ba97SEli Cohen 14451b5daf11SMajd Dibbiny default: 14461b5daf11SMajd Dibbiny return -EINVAL; 14471b5daf11SMajd Dibbiny } 1448e126ba97SEli Cohen 1449e126ba97SEli Cohen } 1450e126ba97SEli Cohen 14511fb7f897SMark Bloch static int mlx5_query_hca_nic_pkey(struct ib_device *ibdev, u32 port, 1452b3cbd6f0SDaniel Jurgens u16 index, u16 *pkey) 1453b3cbd6f0SDaniel Jurgens { 1454b3cbd6f0SDaniel Jurgens struct mlx5_ib_dev *dev = to_mdev(ibdev); 1455b3cbd6f0SDaniel Jurgens struct mlx5_core_dev *mdev; 1456b3cbd6f0SDaniel Jurgens bool put_mdev = true; 14571fb7f897SMark Bloch u32 mdev_port_num; 1458b3cbd6f0SDaniel Jurgens int err; 1459b3cbd6f0SDaniel Jurgens 1460b3cbd6f0SDaniel Jurgens mdev = mlx5_ib_get_native_port_mdev(dev, port, &mdev_port_num); 1461b3cbd6f0SDaniel Jurgens if (!mdev) { 1462b3cbd6f0SDaniel Jurgens /* The port isn't affiliated yet, get the PKey from the master 1463b3cbd6f0SDaniel Jurgens * port. For RoCE the PKey tables will be the same. 1464b3cbd6f0SDaniel Jurgens */ 1465b3cbd6f0SDaniel Jurgens put_mdev = false; 1466b3cbd6f0SDaniel Jurgens mdev = dev->mdev; 1467b3cbd6f0SDaniel Jurgens mdev_port_num = 1; 1468b3cbd6f0SDaniel Jurgens } 1469b3cbd6f0SDaniel Jurgens 1470b3cbd6f0SDaniel Jurgens err = mlx5_query_hca_vport_pkey(mdev, 0, mdev_port_num, 0, 1471b3cbd6f0SDaniel Jurgens index, pkey); 1472b3cbd6f0SDaniel Jurgens if (put_mdev) 1473b3cbd6f0SDaniel Jurgens mlx5_ib_put_native_port_mdev(dev, port); 1474b3cbd6f0SDaniel Jurgens 1475b3cbd6f0SDaniel Jurgens return err; 1476b3cbd6f0SDaniel Jurgens } 1477b3cbd6f0SDaniel Jurgens 14781fb7f897SMark Bloch static int mlx5_ib_query_pkey(struct ib_device *ibdev, u32 port, u16 index, 1479e126ba97SEli Cohen u16 *pkey) 1480e126ba97SEli Cohen { 14811b5daf11SMajd Dibbiny switch (mlx5_get_vport_access_method(ibdev)) { 14821b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_MAD: 14831b5daf11SMajd Dibbiny return mlx5_query_mad_ifc_pkey(ibdev, port, index, pkey); 1484e126ba97SEli Cohen 14851b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_HCA: 14861b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_NIC: 1487b3cbd6f0SDaniel Jurgens return mlx5_query_hca_nic_pkey(ibdev, port, index, pkey); 14881b5daf11SMajd Dibbiny default: 14891b5daf11SMajd Dibbiny return -EINVAL; 1490e126ba97SEli Cohen } 14911b5daf11SMajd Dibbiny } 1492e126ba97SEli Cohen 1493e126ba97SEli Cohen static int mlx5_ib_modify_device(struct ib_device *ibdev, int mask, 1494e126ba97SEli Cohen struct ib_device_modify *props) 1495e126ba97SEli Cohen { 1496e126ba97SEli Cohen struct mlx5_ib_dev *dev = to_mdev(ibdev); 1497e126ba97SEli Cohen struct mlx5_reg_node_desc in; 1498e126ba97SEli Cohen struct mlx5_reg_node_desc out; 1499e126ba97SEli Cohen int err; 1500e126ba97SEli Cohen 1501e126ba97SEli Cohen if (mask & ~IB_DEVICE_MODIFY_NODE_DESC) 1502e126ba97SEli Cohen return -EOPNOTSUPP; 1503e126ba97SEli Cohen 1504e126ba97SEli Cohen if (!(mask & IB_DEVICE_MODIFY_NODE_DESC)) 1505e126ba97SEli Cohen return 0; 1506e126ba97SEli Cohen 1507e126ba97SEli Cohen /* 1508e126ba97SEli Cohen * If possible, pass node desc to FW, so it can generate 1509e126ba97SEli Cohen * a 144 trap. If cmd fails, just ignore. 1510e126ba97SEli Cohen */ 1511bd99fdeaSYuval Shaia memcpy(&in, props->node_desc, IB_DEVICE_NODE_DESC_MAX); 15129603b61dSJack Morgenstein err = mlx5_core_access_reg(dev->mdev, &in, sizeof(in), &out, 1513e126ba97SEli Cohen sizeof(out), MLX5_REG_NODE_DESC, 0, 1); 1514e126ba97SEli Cohen if (err) 1515e126ba97SEli Cohen return err; 1516e126ba97SEli Cohen 1517bd99fdeaSYuval Shaia memcpy(ibdev->node_desc, props->node_desc, IB_DEVICE_NODE_DESC_MAX); 1518e126ba97SEli Cohen 1519e126ba97SEli Cohen return err; 1520e126ba97SEli Cohen } 1521e126ba97SEli Cohen 15221fb7f897SMark Bloch static int set_port_caps_atomic(struct mlx5_ib_dev *dev, u32 port_num, u32 mask, 1523cdbe33d0SEli Cohen u32 value) 1524cdbe33d0SEli Cohen { 1525cdbe33d0SEli Cohen struct mlx5_hca_vport_context ctx = {}; 1526b3cbd6f0SDaniel Jurgens struct mlx5_core_dev *mdev; 15271fb7f897SMark Bloch u32 mdev_port_num; 1528cdbe33d0SEli Cohen int err; 1529cdbe33d0SEli Cohen 1530b3cbd6f0SDaniel Jurgens mdev = mlx5_ib_get_native_port_mdev(dev, port_num, &mdev_port_num); 1531b3cbd6f0SDaniel Jurgens if (!mdev) 1532b3cbd6f0SDaniel Jurgens return -ENODEV; 1533b3cbd6f0SDaniel Jurgens 1534b3cbd6f0SDaniel Jurgens err = mlx5_query_hca_vport_context(mdev, 0, mdev_port_num, 0, &ctx); 1535cdbe33d0SEli Cohen if (err) 1536b3cbd6f0SDaniel Jurgens goto out; 1537cdbe33d0SEli Cohen 1538cdbe33d0SEli Cohen if (~ctx.cap_mask1_perm & mask) { 1539cdbe33d0SEli Cohen mlx5_ib_warn(dev, "trying to change bitmask 0x%X but change supported 0x%X\n", 1540cdbe33d0SEli Cohen mask, ctx.cap_mask1_perm); 1541b3cbd6f0SDaniel Jurgens err = -EINVAL; 1542b3cbd6f0SDaniel Jurgens goto out; 1543cdbe33d0SEli Cohen } 1544cdbe33d0SEli Cohen 1545cdbe33d0SEli Cohen ctx.cap_mask1 = value; 1546cdbe33d0SEli Cohen ctx.cap_mask1_perm = mask; 1547b3cbd6f0SDaniel Jurgens err = mlx5_core_modify_hca_vport_context(mdev, 0, mdev_port_num, 1548b3cbd6f0SDaniel Jurgens 0, &ctx); 1549b3cbd6f0SDaniel Jurgens 1550b3cbd6f0SDaniel Jurgens out: 1551b3cbd6f0SDaniel Jurgens mlx5_ib_put_native_port_mdev(dev, port_num); 1552cdbe33d0SEli Cohen 1553cdbe33d0SEli Cohen return err; 1554cdbe33d0SEli Cohen } 1555cdbe33d0SEli Cohen 15561fb7f897SMark Bloch static int mlx5_ib_modify_port(struct ib_device *ibdev, u32 port, int mask, 1557e126ba97SEli Cohen struct ib_port_modify *props) 1558e126ba97SEli Cohen { 1559e126ba97SEli Cohen struct mlx5_ib_dev *dev = to_mdev(ibdev); 1560e126ba97SEli Cohen struct ib_port_attr attr; 1561e126ba97SEli Cohen u32 tmp; 1562e126ba97SEli Cohen int err; 1563cdbe33d0SEli Cohen u32 change_mask; 1564cdbe33d0SEli Cohen u32 value; 1565cdbe33d0SEli Cohen bool is_ib = (mlx5_ib_port_link_layer(ibdev, port) == 1566cdbe33d0SEli Cohen IB_LINK_LAYER_INFINIBAND); 1567cdbe33d0SEli Cohen 1568ec255879SMajd Dibbiny /* CM layer calls ib_modify_port() regardless of the link layer. For 1569ec255879SMajd Dibbiny * Ethernet ports, qkey violation and Port capabilities are meaningless. 1570ec255879SMajd Dibbiny */ 1571ec255879SMajd Dibbiny if (!is_ib) 1572ec255879SMajd Dibbiny return 0; 1573ec255879SMajd Dibbiny 1574cdbe33d0SEli Cohen if (MLX5_CAP_GEN(dev->mdev, ib_virt) && is_ib) { 1575cdbe33d0SEli Cohen change_mask = props->clr_port_cap_mask | props->set_port_cap_mask; 1576cdbe33d0SEli Cohen value = ~props->clr_port_cap_mask | props->set_port_cap_mask; 1577cdbe33d0SEli Cohen return set_port_caps_atomic(dev, port, change_mask, value); 1578cdbe33d0SEli Cohen } 1579e126ba97SEli Cohen 1580e126ba97SEli Cohen mutex_lock(&dev->cap_mask_mutex); 1581e126ba97SEli Cohen 1582c4550c63SOr Gerlitz err = ib_query_port(ibdev, port, &attr); 1583e126ba97SEli Cohen if (err) 1584e126ba97SEli Cohen goto out; 1585e126ba97SEli Cohen 1586e126ba97SEli Cohen tmp = (attr.port_cap_flags | props->set_port_cap_mask) & 1587e126ba97SEli Cohen ~props->clr_port_cap_mask; 1588e126ba97SEli Cohen 15899603b61dSJack Morgenstein err = mlx5_set_port_caps(dev->mdev, port, tmp); 1590e126ba97SEli Cohen 1591e126ba97SEli Cohen out: 1592e126ba97SEli Cohen mutex_unlock(&dev->cap_mask_mutex); 1593e126ba97SEli Cohen return err; 1594e126ba97SEli Cohen } 1595e126ba97SEli Cohen 159630aa60b3SEli Cohen static void print_lib_caps(struct mlx5_ib_dev *dev, u64 caps) 159730aa60b3SEli Cohen { 159830aa60b3SEli Cohen mlx5_ib_dbg(dev, "MLX5_LIB_CAP_4K_UAR = %s\n", 159930aa60b3SEli Cohen caps & MLX5_LIB_CAP_4K_UAR ? "y" : "n"); 160030aa60b3SEli Cohen } 160130aa60b3SEli Cohen 160231a78a5aSYishai Hadas static u16 calc_dynamic_bfregs(int uars_per_sys_page) 160331a78a5aSYishai Hadas { 160431a78a5aSYishai Hadas /* Large page with non 4k uar support might limit the dynamic size */ 160531a78a5aSYishai Hadas if (uars_per_sys_page == 1 && PAGE_SIZE > 4096) 160631a78a5aSYishai Hadas return MLX5_MIN_DYN_BFREGS; 160731a78a5aSYishai Hadas 160831a78a5aSYishai Hadas return MLX5_MAX_DYN_BFREGS; 160931a78a5aSYishai Hadas } 161031a78a5aSYishai Hadas 1611b037c29aSEli Cohen static int calc_total_bfregs(struct mlx5_ib_dev *dev, bool lib_uar_4k, 1612b037c29aSEli Cohen struct mlx5_ib_alloc_ucontext_req_v2 *req, 161331a78a5aSYishai Hadas struct mlx5_bfreg_info *bfregi) 1614b037c29aSEli Cohen { 1615b037c29aSEli Cohen int uars_per_sys_page; 1616b037c29aSEli Cohen int bfregs_per_sys_page; 1617b037c29aSEli Cohen int ref_bfregs = req->total_num_bfregs; 1618b037c29aSEli Cohen 1619b037c29aSEli Cohen if (req->total_num_bfregs == 0) 1620b037c29aSEli Cohen return -EINVAL; 1621b037c29aSEli Cohen 1622b037c29aSEli Cohen BUILD_BUG_ON(MLX5_MAX_BFREGS % MLX5_NON_FP_BFREGS_IN_PAGE); 1623b037c29aSEli Cohen BUILD_BUG_ON(MLX5_MAX_BFREGS < MLX5_NON_FP_BFREGS_IN_PAGE); 1624b037c29aSEli Cohen 1625b037c29aSEli Cohen if (req->total_num_bfregs > MLX5_MAX_BFREGS) 1626b037c29aSEli Cohen return -ENOMEM; 1627b037c29aSEli Cohen 1628b037c29aSEli Cohen uars_per_sys_page = get_uars_per_sys_page(dev, lib_uar_4k); 1629b037c29aSEli Cohen bfregs_per_sys_page = uars_per_sys_page * MLX5_NON_FP_BFREGS_PER_UAR; 163031a78a5aSYishai Hadas /* This holds the required static allocation asked by the user */ 1631b037c29aSEli Cohen req->total_num_bfregs = ALIGN(req->total_num_bfregs, bfregs_per_sys_page); 1632b037c29aSEli Cohen if (req->num_low_latency_bfregs > req->total_num_bfregs - 1) 1633b037c29aSEli Cohen return -EINVAL; 1634b037c29aSEli Cohen 163531a78a5aSYishai Hadas bfregi->num_static_sys_pages = req->total_num_bfregs / bfregs_per_sys_page; 163631a78a5aSYishai Hadas bfregi->num_dyn_bfregs = ALIGN(calc_dynamic_bfregs(uars_per_sys_page), bfregs_per_sys_page); 163731a78a5aSYishai Hadas bfregi->total_num_bfregs = req->total_num_bfregs + bfregi->num_dyn_bfregs; 163831a78a5aSYishai Hadas bfregi->num_sys_pages = bfregi->total_num_bfregs / bfregs_per_sys_page; 163931a78a5aSYishai Hadas 164031a78a5aSYishai 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", 1641b037c29aSEli Cohen MLX5_CAP_GEN(dev->mdev, uar_4k) ? "yes" : "no", 1642b037c29aSEli Cohen lib_uar_4k ? "yes" : "no", ref_bfregs, 164331a78a5aSYishai Hadas req->total_num_bfregs, bfregi->total_num_bfregs, 164431a78a5aSYishai Hadas bfregi->num_sys_pages); 1645b037c29aSEli Cohen 1646b037c29aSEli Cohen return 0; 1647b037c29aSEli Cohen } 1648b037c29aSEli Cohen 1649b037c29aSEli Cohen static int allocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context) 1650b037c29aSEli Cohen { 1651b037c29aSEli Cohen struct mlx5_bfreg_info *bfregi; 1652b037c29aSEli Cohen int err; 1653b037c29aSEli Cohen int i; 1654b037c29aSEli Cohen 1655b037c29aSEli Cohen bfregi = &context->bfregi; 165631a78a5aSYishai Hadas for (i = 0; i < bfregi->num_static_sys_pages; i++) { 1657d2c8a155SMeir Lichtinger err = mlx5_cmd_uar_alloc(dev->mdev, &bfregi->sys_pages[i], 1658d2c8a155SMeir Lichtinger context->devx_uid); 1659b037c29aSEli Cohen if (err) 1660b037c29aSEli Cohen goto error; 1661b037c29aSEli Cohen 1662b037c29aSEli Cohen mlx5_ib_dbg(dev, "allocated uar %d\n", bfregi->sys_pages[i]); 1663b037c29aSEli Cohen } 16644ed131d0SYishai Hadas 16654ed131d0SYishai Hadas for (i = bfregi->num_static_sys_pages; i < bfregi->num_sys_pages; i++) 16664ed131d0SYishai Hadas bfregi->sys_pages[i] = MLX5_IB_INVALID_UAR_INDEX; 16674ed131d0SYishai Hadas 1668b037c29aSEli Cohen return 0; 1669b037c29aSEli Cohen 1670b037c29aSEli Cohen error: 1671b037c29aSEli Cohen for (--i; i >= 0; i--) 1672d2c8a155SMeir Lichtinger if (mlx5_cmd_uar_dealloc(dev->mdev, bfregi->sys_pages[i], 1673d2c8a155SMeir Lichtinger context->devx_uid)) 1674b037c29aSEli Cohen mlx5_ib_warn(dev, "failed to free uar %d\n", i); 1675b037c29aSEli Cohen 1676b037c29aSEli Cohen return err; 1677b037c29aSEli Cohen } 1678b037c29aSEli Cohen 167915177999SLeon Romanovsky static void deallocate_uars(struct mlx5_ib_dev *dev, 168015177999SLeon Romanovsky struct mlx5_ib_ucontext *context) 1681b037c29aSEli Cohen { 1682b037c29aSEli Cohen struct mlx5_bfreg_info *bfregi; 1683b037c29aSEli Cohen int i; 1684b037c29aSEli Cohen 1685b037c29aSEli Cohen bfregi = &context->bfregi; 168615177999SLeon Romanovsky for (i = 0; i < bfregi->num_sys_pages; i++) 16874ed131d0SYishai Hadas if (i < bfregi->num_static_sys_pages || 168815177999SLeon Romanovsky bfregi->sys_pages[i] != MLX5_IB_INVALID_UAR_INDEX) 1689d2c8a155SMeir Lichtinger mlx5_cmd_uar_dealloc(dev->mdev, bfregi->sys_pages[i], 1690d2c8a155SMeir Lichtinger context->devx_uid); 1691b037c29aSEli Cohen } 1692b037c29aSEli Cohen 16930042f9e4SMark Bloch int mlx5_ib_enable_lb(struct mlx5_ib_dev *dev, bool td, bool qp) 1694a560f1d9SMark Bloch { 1695a560f1d9SMark Bloch int err = 0; 1696a560f1d9SMark Bloch 1697a560f1d9SMark Bloch mutex_lock(&dev->lb.mutex); 16980042f9e4SMark Bloch if (td) 1699a560f1d9SMark Bloch dev->lb.user_td++; 17000042f9e4SMark Bloch if (qp) 17010042f9e4SMark Bloch dev->lb.qps++; 1702a560f1d9SMark Bloch 17030042f9e4SMark Bloch if (dev->lb.user_td == 2 || 17040042f9e4SMark Bloch dev->lb.qps == 1) { 17050042f9e4SMark Bloch if (!dev->lb.enabled) { 1706a560f1d9SMark Bloch err = mlx5_nic_vport_update_local_lb(dev->mdev, true); 17070042f9e4SMark Bloch dev->lb.enabled = true; 17080042f9e4SMark Bloch } 17090042f9e4SMark Bloch } 1710a560f1d9SMark Bloch 1711a560f1d9SMark Bloch mutex_unlock(&dev->lb.mutex); 1712a560f1d9SMark Bloch 1713a560f1d9SMark Bloch return err; 1714a560f1d9SMark Bloch } 1715a560f1d9SMark Bloch 17160042f9e4SMark Bloch void mlx5_ib_disable_lb(struct mlx5_ib_dev *dev, bool td, bool qp) 1717a560f1d9SMark Bloch { 1718a560f1d9SMark Bloch mutex_lock(&dev->lb.mutex); 17190042f9e4SMark Bloch if (td) 1720a560f1d9SMark Bloch dev->lb.user_td--; 17210042f9e4SMark Bloch if (qp) 17220042f9e4SMark Bloch dev->lb.qps--; 1723a560f1d9SMark Bloch 17240042f9e4SMark Bloch if (dev->lb.user_td == 1 && 17250042f9e4SMark Bloch dev->lb.qps == 0) { 17260042f9e4SMark Bloch if (dev->lb.enabled) { 1727a560f1d9SMark Bloch mlx5_nic_vport_update_local_lb(dev->mdev, false); 17280042f9e4SMark Bloch dev->lb.enabled = false; 17290042f9e4SMark Bloch } 17300042f9e4SMark Bloch } 1731a560f1d9SMark Bloch 1732a560f1d9SMark Bloch mutex_unlock(&dev->lb.mutex); 1733a560f1d9SMark Bloch } 1734a560f1d9SMark Bloch 1735d2d19121SYishai Hadas static int mlx5_ib_alloc_transport_domain(struct mlx5_ib_dev *dev, u32 *tdn, 1736d2d19121SYishai Hadas u16 uid) 1737c85023e1SHuy Nguyen { 1738c85023e1SHuy Nguyen int err; 1739c85023e1SHuy Nguyen 1740cfdeb893SLeon Romanovsky if (!MLX5_CAP_GEN(dev->mdev, log_max_transport_domain)) 1741cfdeb893SLeon Romanovsky return 0; 1742cfdeb893SLeon Romanovsky 1743d2d19121SYishai Hadas err = mlx5_cmd_alloc_transport_domain(dev->mdev, tdn, uid); 1744c85023e1SHuy Nguyen if (err) 1745c85023e1SHuy Nguyen return err; 1746c85023e1SHuy Nguyen 1747c85023e1SHuy Nguyen if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) || 17488978cc92SEran Ben Elisha (!MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) && 17498978cc92SEran Ben Elisha !MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc))) 1750c85023e1SHuy Nguyen return err; 1751c85023e1SHuy Nguyen 17520042f9e4SMark Bloch return mlx5_ib_enable_lb(dev, true, false); 1753c85023e1SHuy Nguyen } 1754c85023e1SHuy Nguyen 1755d2d19121SYishai Hadas static void mlx5_ib_dealloc_transport_domain(struct mlx5_ib_dev *dev, u32 tdn, 1756d2d19121SYishai Hadas u16 uid) 1757c85023e1SHuy Nguyen { 1758cfdeb893SLeon Romanovsky if (!MLX5_CAP_GEN(dev->mdev, log_max_transport_domain)) 1759cfdeb893SLeon Romanovsky return; 1760cfdeb893SLeon Romanovsky 1761d2d19121SYishai Hadas mlx5_cmd_dealloc_transport_domain(dev->mdev, tdn, uid); 1762c85023e1SHuy Nguyen 1763c85023e1SHuy Nguyen if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) || 17648978cc92SEran Ben Elisha (!MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) && 17658978cc92SEran Ben Elisha !MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc))) 1766c85023e1SHuy Nguyen return; 1767c85023e1SHuy Nguyen 17680042f9e4SMark Bloch mlx5_ib_disable_lb(dev, true, false); 1769c85023e1SHuy Nguyen } 1770c85023e1SHuy Nguyen 177145ec21c9SYishai Hadas static int set_ucontext_resp(struct ib_ucontext *uctx, 177245ec21c9SYishai Hadas struct mlx5_ib_alloc_ucontext_resp *resp) 177345ec21c9SYishai Hadas { 177445ec21c9SYishai Hadas struct ib_device *ibdev = uctx->device; 177545ec21c9SYishai Hadas struct mlx5_ib_dev *dev = to_mdev(ibdev); 177645ec21c9SYishai Hadas struct mlx5_ib_ucontext *context = to_mucontext(uctx); 177745ec21c9SYishai Hadas struct mlx5_bfreg_info *bfregi = &context->bfregi; 177845ec21c9SYishai Hadas 177945ec21c9SYishai Hadas if (MLX5_CAP_GEN(dev->mdev, dump_fill_mkey)) { 1780594cac11SOr Har-Toov resp->dump_fill_mkey = dev->mkeys.dump_fill_mkey; 178145ec21c9SYishai Hadas resp->comp_mask |= 178245ec21c9SYishai Hadas MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_DUMP_FILL_MKEY; 178345ec21c9SYishai Hadas } 178445ec21c9SYishai Hadas 178545ec21c9SYishai Hadas resp->qp_tab_size = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp); 178645ec21c9SYishai Hadas if (dev->wc_support) 178745ec21c9SYishai Hadas resp->bf_reg_size = 1 << MLX5_CAP_GEN(dev->mdev, 178845ec21c9SYishai Hadas log_bf_reg_size); 178945ec21c9SYishai Hadas resp->cache_line_size = cache_line_size(); 179045ec21c9SYishai Hadas resp->max_sq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq); 179145ec21c9SYishai Hadas resp->max_rq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq); 179245ec21c9SYishai Hadas resp->max_send_wqebb = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz); 179345ec21c9SYishai Hadas resp->max_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz); 179445ec21c9SYishai Hadas resp->max_srq_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz); 179545ec21c9SYishai Hadas resp->cqe_version = context->cqe_version; 179645ec21c9SYishai Hadas resp->log_uar_size = MLX5_CAP_GEN(dev->mdev, uar_4k) ? 179745ec21c9SYishai Hadas MLX5_ADAPTER_PAGE_SHIFT : PAGE_SHIFT; 179845ec21c9SYishai Hadas resp->num_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ? 179945ec21c9SYishai Hadas MLX5_CAP_GEN(dev->mdev, 180045ec21c9SYishai Hadas num_of_uars_per_page) : 1; 180145ec21c9SYishai Hadas resp->tot_bfregs = bfregi->lib_uar_dyn ? 0 : 180245ec21c9SYishai Hadas bfregi->total_num_bfregs - bfregi->num_dyn_bfregs; 180345ec21c9SYishai Hadas resp->num_ports = dev->num_ports; 180445ec21c9SYishai Hadas resp->cmds_supp_uhw |= MLX5_USER_CMDS_SUPP_UHW_QUERY_DEVICE | 180545ec21c9SYishai Hadas MLX5_USER_CMDS_SUPP_UHW_CREATE_AH; 180645ec21c9SYishai Hadas 180745ec21c9SYishai Hadas if (mlx5_ib_port_link_layer(ibdev, 1) == IB_LINK_LAYER_ETHERNET) { 180845ec21c9SYishai Hadas mlx5_query_min_inline(dev->mdev, &resp->eth_min_inline); 180945ec21c9SYishai Hadas resp->eth_min_inline++; 181045ec21c9SYishai Hadas } 181145ec21c9SYishai Hadas 181245ec21c9SYishai Hadas if (dev->mdev->clock_info) 181345ec21c9SYishai Hadas resp->clock_info_versions = BIT(MLX5_IB_CLOCK_INFO_V1); 181445ec21c9SYishai Hadas 181545ec21c9SYishai Hadas /* 181645ec21c9SYishai Hadas * We don't want to expose information from the PCI bar that is located 181745ec21c9SYishai Hadas * after 4096 bytes, so if the arch only supports larger pages, let's 181845ec21c9SYishai Hadas * pretend we don't support reading the HCA's core clock. This is also 181945ec21c9SYishai Hadas * forced by mmap function. 182045ec21c9SYishai Hadas */ 182145ec21c9SYishai Hadas if (PAGE_SIZE <= 4096) { 182245ec21c9SYishai Hadas resp->comp_mask |= 182345ec21c9SYishai Hadas MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_CORE_CLOCK_OFFSET; 182445ec21c9SYishai Hadas resp->hca_core_clock_offset = 182545ec21c9SYishai Hadas offsetof(struct mlx5_init_seg, 182645ec21c9SYishai Hadas internal_timer_h) % PAGE_SIZE; 182745ec21c9SYishai Hadas } 182845ec21c9SYishai Hadas 182945ec21c9SYishai Hadas if (MLX5_CAP_GEN(dev->mdev, ece_support)) 183045ec21c9SYishai Hadas resp->comp_mask |= MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_ECE; 183145ec21c9SYishai Hadas 183233652951SAharon Landau if (rt_supported(MLX5_CAP_GEN(dev->mdev, sq_ts_format)) && 183333652951SAharon Landau rt_supported(MLX5_CAP_GEN(dev->mdev, rq_ts_format)) && 183433652951SAharon Landau rt_supported(MLX5_CAP_ROCE(dev->mdev, qp_ts_format))) 183533652951SAharon Landau resp->comp_mask |= 183633652951SAharon Landau MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_REAL_TIME_TS; 183733652951SAharon Landau 183845ec21c9SYishai Hadas resp->num_dyn_bfregs = bfregi->num_dyn_bfregs; 1839c906b86eSSergey Gorenko 1840c906b86eSSergey Gorenko if (MLX5_CAP_GEN(dev->mdev, drain_sigerr)) 1841c906b86eSSergey Gorenko resp->comp_mask |= MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_SQD2RTS; 1842c906b86eSSergey Gorenko 184313ad1125SAharon Landau resp->comp_mask |= 184413ad1125SAharon Landau MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_MKEY_UPDATE_TAG; 184513ad1125SAharon Landau 184645ec21c9SYishai Hadas return 0; 184745ec21c9SYishai Hadas } 184845ec21c9SYishai Hadas 1849a2a074efSLeon Romanovsky static int mlx5_ib_alloc_ucontext(struct ib_ucontext *uctx, 1850e126ba97SEli Cohen struct ib_udata *udata) 1851e126ba97SEli Cohen { 1852a2a074efSLeon Romanovsky struct ib_device *ibdev = uctx->device; 1853e126ba97SEli Cohen struct mlx5_ib_dev *dev = to_mdev(ibdev); 1854b368d7cbSMatan Barak struct mlx5_ib_alloc_ucontext_req_v2 req = {}; 1855b368d7cbSMatan Barak struct mlx5_ib_alloc_ucontext_resp resp = {}; 1856a2a074efSLeon Romanovsky struct mlx5_ib_ucontext *context = to_mucontext(uctx); 18572f5ff264SEli Cohen struct mlx5_bfreg_info *bfregi; 185878c0f98cSEli Cohen int ver; 1859e126ba97SEli Cohen int err; 1860a168a41cSMajd Dibbiny size_t min_req_v2 = offsetof(struct mlx5_ib_alloc_ucontext_req_v2, 1861a168a41cSMajd Dibbiny max_cqe_version); 1862b037c29aSEli Cohen bool lib_uar_4k; 18630a2fd01cSYishai Hadas bool lib_uar_dyn; 1864e126ba97SEli Cohen 1865e126ba97SEli Cohen if (!dev->ib_active) 1866a2a074efSLeon Romanovsky return -EAGAIN; 1867e126ba97SEli Cohen 1868e093111dSAmrani, Ram if (udata->inlen == sizeof(struct mlx5_ib_alloc_ucontext_req)) 186978c0f98cSEli Cohen ver = 0; 1870e093111dSAmrani, Ram else if (udata->inlen >= min_req_v2) 187178c0f98cSEli Cohen ver = 2; 187278c0f98cSEli Cohen else 1873a2a074efSLeon Romanovsky return -EINVAL; 187478c0f98cSEli Cohen 1875e093111dSAmrani, Ram err = ib_copy_from_udata(&req, udata, min(udata->inlen, sizeof(req))); 1876e126ba97SEli Cohen if (err) 1877a2a074efSLeon Romanovsky return err; 1878e126ba97SEli Cohen 1879a8b92ca1SYishai Hadas if (req.flags & ~MLX5_IB_ALLOC_UCTX_DEVX) 1880a2a074efSLeon Romanovsky return -EOPNOTSUPP; 188178c0f98cSEli Cohen 1882f72300c5SHaggai Abramovsky if (req.comp_mask || req.reserved0 || req.reserved1 || req.reserved2) 1883a2a074efSLeon Romanovsky return -EOPNOTSUPP; 1884b368d7cbSMatan Barak 18852f5ff264SEli Cohen req.total_num_bfregs = ALIGN(req.total_num_bfregs, 18862f5ff264SEli Cohen MLX5_NON_FP_BFREGS_PER_UAR); 18872f5ff264SEli Cohen if (req.num_low_latency_bfregs > req.total_num_bfregs - 1) 1888a2a074efSLeon Romanovsky return -EINVAL; 1889e126ba97SEli Cohen 1890d2c8a155SMeir Lichtinger if (req.flags & MLX5_IB_ALLOC_UCTX_DEVX) { 1891d2c8a155SMeir Lichtinger err = mlx5_ib_devx_create(dev, true); 1892d2c8a155SMeir Lichtinger if (err < 0) 1893d2c8a155SMeir Lichtinger goto out_ctx; 1894d2c8a155SMeir Lichtinger context->devx_uid = err; 1895d2c8a155SMeir Lichtinger } 1896d2c8a155SMeir Lichtinger 189730aa60b3SEli Cohen lib_uar_4k = req.lib_caps & MLX5_LIB_CAP_4K_UAR; 18980a2fd01cSYishai Hadas lib_uar_dyn = req.lib_caps & MLX5_LIB_CAP_DYN_UAR; 18992f5ff264SEli Cohen bfregi = &context->bfregi; 1900b037c29aSEli Cohen 19010a2fd01cSYishai Hadas if (lib_uar_dyn) { 19020a2fd01cSYishai Hadas bfregi->lib_uar_dyn = lib_uar_dyn; 19030a2fd01cSYishai Hadas goto uar_done; 19040a2fd01cSYishai Hadas } 19050a2fd01cSYishai Hadas 1906b037c29aSEli Cohen /* updates req->total_num_bfregs */ 190731a78a5aSYishai Hadas err = calc_total_bfregs(dev, lib_uar_4k, &req, bfregi); 1908b037c29aSEli Cohen if (err) 1909d2c8a155SMeir Lichtinger goto out_devx; 1910b037c29aSEli Cohen 19112f5ff264SEli Cohen mutex_init(&bfregi->lock); 1912b037c29aSEli Cohen bfregi->lib_uar_4k = lib_uar_4k; 191331a78a5aSYishai Hadas bfregi->count = kcalloc(bfregi->total_num_bfregs, sizeof(*bfregi->count), 1914b037c29aSEli Cohen GFP_KERNEL); 1915b037c29aSEli Cohen if (!bfregi->count) { 1916e126ba97SEli Cohen err = -ENOMEM; 1917d2c8a155SMeir Lichtinger goto out_devx; 1918e126ba97SEli Cohen } 1919e126ba97SEli Cohen 1920b037c29aSEli Cohen bfregi->sys_pages = kcalloc(bfregi->num_sys_pages, 1921b037c29aSEli Cohen sizeof(*bfregi->sys_pages), 1922e126ba97SEli Cohen GFP_KERNEL); 1923b037c29aSEli Cohen if (!bfregi->sys_pages) { 1924e126ba97SEli Cohen err = -ENOMEM; 1925e126ba97SEli Cohen goto out_count; 1926e126ba97SEli Cohen } 1927e126ba97SEli Cohen 1928b037c29aSEli Cohen err = allocate_uars(dev, context); 1929b037c29aSEli Cohen if (err) 1930b037c29aSEli Cohen goto out_sys_pages; 1931b037c29aSEli Cohen 19320a2fd01cSYishai Hadas uar_done: 1933d2d19121SYishai Hadas err = mlx5_ib_alloc_transport_domain(dev, &context->tdn, 1934d2d19121SYishai Hadas context->devx_uid); 1935a8b92ca1SYishai Hadas if (err) 1936d2c8a155SMeir Lichtinger goto out_uars; 1937a8b92ca1SYishai Hadas 1938e126ba97SEli Cohen INIT_LIST_HEAD(&context->db_page_list); 1939e126ba97SEli Cohen mutex_init(&context->db_page_mutex); 1940e126ba97SEli Cohen 194145ec21c9SYishai Hadas context->cqe_version = min_t(__u8, 194245ec21c9SYishai Hadas (__u8)MLX5_CAP_GEN(dev->mdev, cqe_version), 194345ec21c9SYishai Hadas req.max_cqe_version); 1944b368d7cbSMatan Barak 194545ec21c9SYishai Hadas err = set_ucontext_resp(uctx, &resp); 194645ec21c9SYishai Hadas if (err) 194745ec21c9SYishai Hadas goto out_mdev; 1948b368d7cbSMatan Barak 194945ec21c9SYishai Hadas resp.response_length = min(udata->outlen, sizeof(resp)); 1950b368d7cbSMatan Barak err = ib_copy_to_udata(udata, &resp, resp.response_length); 1951e126ba97SEli Cohen if (err) 1952a8b92ca1SYishai Hadas goto out_mdev; 1953e126ba97SEli Cohen 19542f5ff264SEli Cohen bfregi->ver = ver; 19552f5ff264SEli Cohen bfregi->num_low_latency_bfregs = req.num_low_latency_bfregs; 195630aa60b3SEli Cohen context->lib_caps = req.lib_caps; 195730aa60b3SEli Cohen print_lib_caps(dev, context->lib_caps); 1958f72300c5SHaggai Abramovsky 1959802dcc7fSMark Zhang if (mlx5_ib_lag_should_assign_affinity(dev)) { 19601fb7f897SMark Bloch u32 port = mlx5_core_native_port_num(dev->mdev) - 1; 1961c6a21c38SMajd Dibbiny 1962c6a21c38SMajd Dibbiny atomic_set(&context->tx_port_affinity, 1963c6a21c38SMajd Dibbiny atomic_add_return( 196495579e78SMark Bloch 1, &dev->port[port].roce.tx_port_affinity)); 1965c6a21c38SMajd Dibbiny } 1966c6a21c38SMajd Dibbiny 1967a2a074efSLeon Romanovsky return 0; 1968e126ba97SEli Cohen 1969a8b92ca1SYishai Hadas out_mdev: 1970d2d19121SYishai Hadas mlx5_ib_dealloc_transport_domain(dev, context->tdn, context->devx_uid); 1971146d2f1aSmajd@mellanox.com 1972e126ba97SEli Cohen out_uars: 1973b037c29aSEli Cohen deallocate_uars(dev, context); 1974b037c29aSEli Cohen 1975b037c29aSEli Cohen out_sys_pages: 1976b037c29aSEli Cohen kfree(bfregi->sys_pages); 1977b037c29aSEli Cohen 1978e126ba97SEli Cohen out_count: 19792f5ff264SEli Cohen kfree(bfregi->count); 1980e126ba97SEli Cohen 1981d2c8a155SMeir Lichtinger out_devx: 1982d2c8a155SMeir Lichtinger if (req.flags & MLX5_IB_ALLOC_UCTX_DEVX) 1983d2c8a155SMeir Lichtinger mlx5_ib_devx_destroy(dev, context->devx_uid); 1984d2c8a155SMeir Lichtinger 1985e126ba97SEli Cohen out_ctx: 1986a2a074efSLeon Romanovsky return err; 1987e126ba97SEli Cohen } 1988e126ba97SEli Cohen 19890fb556b2SYishai Hadas static int mlx5_ib_query_ucontext(struct ib_ucontext *ibcontext, 19900fb556b2SYishai Hadas struct uverbs_attr_bundle *attrs) 19910fb556b2SYishai Hadas { 19920fb556b2SYishai Hadas struct mlx5_ib_alloc_ucontext_resp uctx_resp = {}; 19930fb556b2SYishai Hadas int ret; 19940fb556b2SYishai Hadas 19950fb556b2SYishai Hadas ret = set_ucontext_resp(ibcontext, &uctx_resp); 19960fb556b2SYishai Hadas if (ret) 19970fb556b2SYishai Hadas return ret; 19980fb556b2SYishai Hadas 19990fb556b2SYishai Hadas uctx_resp.response_length = 20000fb556b2SYishai Hadas min_t(size_t, 20010fb556b2SYishai Hadas uverbs_attr_get_len(attrs, 20020fb556b2SYishai Hadas MLX5_IB_ATTR_QUERY_CONTEXT_RESP_UCTX), 20030fb556b2SYishai Hadas sizeof(uctx_resp)); 20040fb556b2SYishai Hadas 20050fb556b2SYishai Hadas ret = uverbs_copy_to_struct_or_zero(attrs, 20060fb556b2SYishai Hadas MLX5_IB_ATTR_QUERY_CONTEXT_RESP_UCTX, 20070fb556b2SYishai Hadas &uctx_resp, 20080fb556b2SYishai Hadas sizeof(uctx_resp)); 20090fb556b2SYishai Hadas return ret; 20100fb556b2SYishai Hadas } 20110fb556b2SYishai Hadas 2012a2a074efSLeon Romanovsky static void mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext) 2013e126ba97SEli Cohen { 2014e126ba97SEli Cohen struct mlx5_ib_ucontext *context = to_mucontext(ibcontext); 2015e126ba97SEli Cohen struct mlx5_ib_dev *dev = to_mdev(ibcontext->device); 2016b037c29aSEli Cohen struct mlx5_bfreg_info *bfregi; 2017e126ba97SEli Cohen 2018b037c29aSEli Cohen bfregi = &context->bfregi; 2019d2d19121SYishai Hadas mlx5_ib_dealloc_transport_domain(dev, context->tdn, context->devx_uid); 2020d2d19121SYishai Hadas 2021b037c29aSEli Cohen deallocate_uars(dev, context); 2022b037c29aSEli Cohen kfree(bfregi->sys_pages); 20232f5ff264SEli Cohen kfree(bfregi->count); 2024d2c8a155SMeir Lichtinger 2025d2c8a155SMeir Lichtinger if (context->devx_uid) 2026d2c8a155SMeir Lichtinger mlx5_ib_devx_destroy(dev, context->devx_uid); 2027e126ba97SEli Cohen } 2028e126ba97SEli Cohen 2029b037c29aSEli Cohen static phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev, 20304ed131d0SYishai Hadas int uar_idx) 2031e126ba97SEli Cohen { 2032b037c29aSEli Cohen int fw_uars_per_page; 2033b037c29aSEli Cohen 2034b037c29aSEli Cohen fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ? MLX5_UARS_IN_PAGE : 1; 2035b037c29aSEli Cohen 2036aa8106f1SHuy Nguyen return (dev->mdev->bar_addr >> PAGE_SHIFT) + uar_idx / fw_uars_per_page; 2037e126ba97SEli Cohen } 2038e126ba97SEli Cohen 2039342ee59dSYishai Hadas static u64 uar_index2paddress(struct mlx5_ib_dev *dev, 2040342ee59dSYishai Hadas int uar_idx) 2041342ee59dSYishai Hadas { 2042342ee59dSYishai Hadas unsigned int fw_uars_per_page; 2043342ee59dSYishai Hadas 2044342ee59dSYishai Hadas fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ? 2045342ee59dSYishai Hadas MLX5_UARS_IN_PAGE : 1; 2046342ee59dSYishai Hadas 2047342ee59dSYishai Hadas return (dev->mdev->bar_addr + (uar_idx / fw_uars_per_page) * PAGE_SIZE); 2048342ee59dSYishai Hadas } 2049342ee59dSYishai Hadas 2050e126ba97SEli Cohen static int get_command(unsigned long offset) 2051e126ba97SEli Cohen { 2052e126ba97SEli Cohen return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK; 2053e126ba97SEli Cohen } 2054e126ba97SEli Cohen 2055e126ba97SEli Cohen static int get_arg(unsigned long offset) 2056e126ba97SEli Cohen { 2057e126ba97SEli Cohen return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1); 2058e126ba97SEli Cohen } 2059e126ba97SEli Cohen 2060e126ba97SEli Cohen static int get_index(unsigned long offset) 2061e126ba97SEli Cohen { 2062e126ba97SEli Cohen return get_arg(offset); 2063e126ba97SEli Cohen } 2064e126ba97SEli Cohen 20654ed131d0SYishai Hadas /* Index resides in an extra byte to enable larger values than 255 */ 20664ed131d0SYishai Hadas static int get_extended_index(unsigned long offset) 20674ed131d0SYishai Hadas { 20684ed131d0SYishai Hadas return get_arg(offset) | ((offset >> 16) & 0xff) << 8; 20694ed131d0SYishai Hadas } 20704ed131d0SYishai Hadas 20717c2344c3SMaor Gottlieb 20727c2344c3SMaor Gottlieb static void mlx5_ib_disassociate_ucontext(struct ib_ucontext *ibcontext) 20737c2344c3SMaor Gottlieb { 20747c2344c3SMaor Gottlieb } 20757c2344c3SMaor Gottlieb 207637aa5c36SGuy Levi static inline char *mmap_cmd2str(enum mlx5_ib_mmap_cmd cmd) 2077e126ba97SEli Cohen { 207837aa5c36SGuy Levi switch (cmd) { 207937aa5c36SGuy Levi case MLX5_IB_MMAP_WC_PAGE: 208037aa5c36SGuy Levi return "WC"; 2081e126ba97SEli Cohen case MLX5_IB_MMAP_REGULAR_PAGE: 208237aa5c36SGuy Levi return "best effort WC"; 208337aa5c36SGuy Levi case MLX5_IB_MMAP_NC_PAGE: 208437aa5c36SGuy Levi return "NC"; 208524da0016SAriel Levkovich case MLX5_IB_MMAP_DEVICE_MEM: 208624da0016SAriel Levkovich return "Device Memory"; 208737aa5c36SGuy Levi default: 2088dab994bcSShay Drory return "Unknown"; 208937aa5c36SGuy Levi } 209037aa5c36SGuy Levi } 209137aa5c36SGuy Levi 20925c99eaecSFeras Daoud static int mlx5_ib_mmap_clock_info_page(struct mlx5_ib_dev *dev, 20935c99eaecSFeras Daoud struct vm_area_struct *vma, 20945c99eaecSFeras Daoud struct mlx5_ib_ucontext *context) 20955c99eaecSFeras Daoud { 20964eb6ab13SJason Gunthorpe if ((vma->vm_end - vma->vm_start != PAGE_SIZE) || 20974eb6ab13SJason Gunthorpe !(vma->vm_flags & VM_SHARED)) 20985c99eaecSFeras Daoud return -EINVAL; 20995c99eaecSFeras Daoud 21005c99eaecSFeras Daoud if (get_index(vma->vm_pgoff) != MLX5_IB_CLOCK_INFO_V1) 21015c99eaecSFeras Daoud return -EOPNOTSUPP; 21025c99eaecSFeras Daoud 21034eb6ab13SJason Gunthorpe if (vma->vm_flags & (VM_WRITE | VM_EXEC)) 21045c99eaecSFeras Daoud return -EPERM; 21051c71222eSSuren Baghdasaryan vm_flags_clear(vma, VM_MAYWRITE); 21065c99eaecSFeras Daoud 2107ddcdc368SJason Gunthorpe if (!dev->mdev->clock_info) 21085c99eaecSFeras Daoud return -EOPNOTSUPP; 21095c99eaecSFeras Daoud 21104eb6ab13SJason Gunthorpe return vm_insert_page(vma, vma->vm_start, 21114eb6ab13SJason Gunthorpe virt_to_page(dev->mdev->clock_info)); 21125c99eaecSFeras Daoud } 21135c99eaecSFeras Daoud 2114dc2316ebSYishai Hadas static void mlx5_ib_mmap_free(struct rdma_user_mmap_entry *entry) 2115dc2316ebSYishai Hadas { 2116dc2316ebSYishai Hadas struct mlx5_user_mmap_entry *mentry = to_mmmap(entry); 2117dc2316ebSYishai Hadas struct mlx5_ib_dev *dev = to_mdev(entry->ucontext->device); 21187be76befSYishai Hadas struct mlx5_var_table *var_table = &dev->var_table; 2119d2c8a155SMeir Lichtinger struct mlx5_ib_ucontext *context = to_mucontext(entry->ucontext); 2120dc2316ebSYishai Hadas 2121dc2316ebSYishai Hadas switch (mentry->mmap_flag) { 2122dc2316ebSYishai Hadas case MLX5_IB_MMAP_TYPE_MEMIC: 2123cea85fa5SMaor Gottlieb case MLX5_IB_MMAP_TYPE_MEMIC_OP: 2124cea85fa5SMaor Gottlieb mlx5_ib_dm_mmap_free(dev, mentry); 2125dc2316ebSYishai Hadas break; 21267be76befSYishai Hadas case MLX5_IB_MMAP_TYPE_VAR: 21277be76befSYishai Hadas mutex_lock(&var_table->bitmap_lock); 21287be76befSYishai Hadas clear_bit(mentry->page_idx, var_table->bitmap); 21297be76befSYishai Hadas mutex_unlock(&var_table->bitmap_lock); 21307be76befSYishai Hadas kfree(mentry); 21317be76befSYishai Hadas break; 2132342ee59dSYishai Hadas case MLX5_IB_MMAP_TYPE_UAR_WC: 2133342ee59dSYishai Hadas case MLX5_IB_MMAP_TYPE_UAR_NC: 2134d2c8a155SMeir Lichtinger mlx5_cmd_uar_dealloc(dev->mdev, mentry->page_idx, 2135d2c8a155SMeir Lichtinger context->devx_uid); 2136342ee59dSYishai Hadas kfree(mentry); 2137342ee59dSYishai Hadas break; 2138dc2316ebSYishai Hadas default: 2139dc2316ebSYishai Hadas WARN_ON(true); 2140dc2316ebSYishai Hadas } 2141dc2316ebSYishai Hadas } 2142dc2316ebSYishai Hadas 214337aa5c36SGuy Levi static int uar_mmap(struct mlx5_ib_dev *dev, enum mlx5_ib_mmap_cmd cmd, 21447c2344c3SMaor Gottlieb struct vm_area_struct *vma, 21457c2344c3SMaor Gottlieb struct mlx5_ib_ucontext *context) 214637aa5c36SGuy Levi { 21472f5ff264SEli Cohen struct mlx5_bfreg_info *bfregi = &context->bfregi; 214837aa5c36SGuy Levi int err; 214937aa5c36SGuy Levi unsigned long idx; 2150aa09ea6eSKamal Heib phys_addr_t pfn; 215137aa5c36SGuy Levi pgprot_t prot; 21524ed131d0SYishai Hadas u32 bfreg_dyn_idx = 0; 21534ed131d0SYishai Hadas u32 uar_index; 21544ed131d0SYishai Hadas int dyn_uar = (cmd == MLX5_IB_MMAP_ALLOC_WC); 21554ed131d0SYishai Hadas int max_valid_idx = dyn_uar ? bfregi->num_sys_pages : 21564ed131d0SYishai Hadas bfregi->num_static_sys_pages; 2157b037c29aSEli Cohen 21580a2fd01cSYishai Hadas if (bfregi->lib_uar_dyn) 21590a2fd01cSYishai Hadas return -EINVAL; 21600a2fd01cSYishai Hadas 2161b037c29aSEli Cohen if (vma->vm_end - vma->vm_start != PAGE_SIZE) 2162b037c29aSEli Cohen return -EINVAL; 2163b037c29aSEli Cohen 21644ed131d0SYishai Hadas if (dyn_uar) 21654ed131d0SYishai Hadas idx = get_extended_index(vma->vm_pgoff) + bfregi->num_static_sys_pages; 21664ed131d0SYishai Hadas else 2167b037c29aSEli Cohen idx = get_index(vma->vm_pgoff); 21684ed131d0SYishai Hadas 21694ed131d0SYishai Hadas if (idx >= max_valid_idx) { 21704ed131d0SYishai Hadas mlx5_ib_warn(dev, "invalid uar index %lu, max=%d\n", 21714ed131d0SYishai Hadas idx, max_valid_idx); 2172b037c29aSEli Cohen return -EINVAL; 2173b037c29aSEli Cohen } 217437aa5c36SGuy Levi 217537aa5c36SGuy Levi switch (cmd) { 217637aa5c36SGuy Levi case MLX5_IB_MMAP_WC_PAGE: 21774ed131d0SYishai Hadas case MLX5_IB_MMAP_ALLOC_WC: 217837aa5c36SGuy Levi case MLX5_IB_MMAP_REGULAR_PAGE: 217937aa5c36SGuy Levi /* For MLX5_IB_MMAP_REGULAR_PAGE do the best effort to get WC */ 218037aa5c36SGuy Levi prot = pgprot_writecombine(vma->vm_page_prot); 218137aa5c36SGuy Levi break; 218237aa5c36SGuy Levi case MLX5_IB_MMAP_NC_PAGE: 218337aa5c36SGuy Levi prot = pgprot_noncached(vma->vm_page_prot); 218437aa5c36SGuy Levi break; 218537aa5c36SGuy Levi default: 218637aa5c36SGuy Levi return -EINVAL; 218737aa5c36SGuy Levi } 218837aa5c36SGuy Levi 21894ed131d0SYishai Hadas if (dyn_uar) { 21904ed131d0SYishai Hadas int uars_per_page; 21914ed131d0SYishai Hadas 21924ed131d0SYishai Hadas uars_per_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k); 21934ed131d0SYishai Hadas bfreg_dyn_idx = idx * (uars_per_page * MLX5_NON_FP_BFREGS_PER_UAR); 21944ed131d0SYishai Hadas if (bfreg_dyn_idx >= bfregi->total_num_bfregs) { 21954ed131d0SYishai Hadas mlx5_ib_warn(dev, "invalid bfreg_dyn_idx %u, max=%u\n", 21964ed131d0SYishai Hadas bfreg_dyn_idx, bfregi->total_num_bfregs); 21974ed131d0SYishai Hadas return -EINVAL; 21984ed131d0SYishai Hadas } 21994ed131d0SYishai Hadas 22004ed131d0SYishai Hadas mutex_lock(&bfregi->lock); 22014ed131d0SYishai Hadas /* Fail if uar already allocated, first bfreg index of each 22024ed131d0SYishai Hadas * page holds its count. 22034ed131d0SYishai Hadas */ 22044ed131d0SYishai Hadas if (bfregi->count[bfreg_dyn_idx]) { 22054ed131d0SYishai Hadas mlx5_ib_warn(dev, "wrong offset, idx %lu is busy, bfregn=%u\n", idx, bfreg_dyn_idx); 22064ed131d0SYishai Hadas mutex_unlock(&bfregi->lock); 22074ed131d0SYishai Hadas return -EINVAL; 22084ed131d0SYishai Hadas } 22094ed131d0SYishai Hadas 22104ed131d0SYishai Hadas bfregi->count[bfreg_dyn_idx]++; 22114ed131d0SYishai Hadas mutex_unlock(&bfregi->lock); 22124ed131d0SYishai Hadas 2213d2c8a155SMeir Lichtinger err = mlx5_cmd_uar_alloc(dev->mdev, &uar_index, 2214d2c8a155SMeir Lichtinger context->devx_uid); 22154ed131d0SYishai Hadas if (err) { 22164ed131d0SYishai Hadas mlx5_ib_warn(dev, "UAR alloc failed\n"); 22174ed131d0SYishai Hadas goto free_bfreg; 22184ed131d0SYishai Hadas } 22194ed131d0SYishai Hadas } else { 22204ed131d0SYishai Hadas uar_index = bfregi->sys_pages[idx]; 22214ed131d0SYishai Hadas } 22224ed131d0SYishai Hadas 22234ed131d0SYishai Hadas pfn = uar_index2pfn(dev, uar_index); 222437aa5c36SGuy Levi mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn %pa\n", idx, &pfn); 2225e126ba97SEli Cohen 2226e2cd1d1aSJason Gunthorpe err = rdma_user_mmap_io(&context->ibucontext, vma, pfn, PAGE_SIZE, 2227c043ff2cSMichal Kalderon prot, NULL); 222837aa5c36SGuy Levi if (err) { 22298f062287SLeon Romanovsky mlx5_ib_err(dev, 2230e2cd1d1aSJason Gunthorpe "rdma_user_mmap_io failed with error=%d, mmap_cmd=%s\n", 22318f062287SLeon Romanovsky err, mmap_cmd2str(cmd)); 22324ed131d0SYishai Hadas goto err; 223337aa5c36SGuy Levi } 2234e126ba97SEli Cohen 22354ed131d0SYishai Hadas if (dyn_uar) 22364ed131d0SYishai Hadas bfregi->sys_pages[idx] = uar_index; 22374ed131d0SYishai Hadas return 0; 22384ed131d0SYishai Hadas 22394ed131d0SYishai Hadas err: 22404ed131d0SYishai Hadas if (!dyn_uar) 22414ed131d0SYishai Hadas return err; 22424ed131d0SYishai Hadas 2243d2c8a155SMeir Lichtinger mlx5_cmd_uar_dealloc(dev->mdev, idx, context->devx_uid); 22444ed131d0SYishai Hadas 22454ed131d0SYishai Hadas free_bfreg: 22464ed131d0SYishai Hadas mlx5_ib_free_bfreg(dev, bfregi, bfreg_dyn_idx); 22474ed131d0SYishai Hadas 22484ed131d0SYishai Hadas return err; 224937aa5c36SGuy Levi } 225037aa5c36SGuy Levi 2251dc2316ebSYishai Hadas static unsigned long mlx5_vma_to_pgoff(struct vm_area_struct *vma) 2252dc2316ebSYishai Hadas { 2253dc2316ebSYishai Hadas unsigned long idx; 2254dc2316ebSYishai Hadas u8 command; 2255dc2316ebSYishai Hadas 2256dc2316ebSYishai Hadas command = get_command(vma->vm_pgoff); 2257dc2316ebSYishai Hadas idx = get_extended_index(vma->vm_pgoff); 2258dc2316ebSYishai Hadas 2259dc2316ebSYishai Hadas return (command << 16 | idx); 2260dc2316ebSYishai Hadas } 2261dc2316ebSYishai Hadas 2262dc2316ebSYishai Hadas static int mlx5_ib_mmap_offset(struct mlx5_ib_dev *dev, 2263dc2316ebSYishai Hadas struct vm_area_struct *vma, 2264dc2316ebSYishai Hadas struct ib_ucontext *ucontext) 2265dc2316ebSYishai Hadas { 2266dc2316ebSYishai Hadas struct mlx5_user_mmap_entry *mentry; 2267dc2316ebSYishai Hadas struct rdma_user_mmap_entry *entry; 2268dc2316ebSYishai Hadas unsigned long pgoff; 2269dc2316ebSYishai Hadas pgprot_t prot; 2270dc2316ebSYishai Hadas phys_addr_t pfn; 2271dc2316ebSYishai Hadas int ret; 2272dc2316ebSYishai Hadas 2273dc2316ebSYishai Hadas pgoff = mlx5_vma_to_pgoff(vma); 2274dc2316ebSYishai Hadas entry = rdma_user_mmap_entry_get_pgoff(ucontext, pgoff); 2275dc2316ebSYishai Hadas if (!entry) 227624da0016SAriel Levkovich return -EINVAL; 227724da0016SAriel Levkovich 2278dc2316ebSYishai Hadas mentry = to_mmmap(entry); 2279dc2316ebSYishai Hadas pfn = (mentry->address >> PAGE_SHIFT); 2280342ee59dSYishai Hadas if (mentry->mmap_flag == MLX5_IB_MMAP_TYPE_VAR || 2281342ee59dSYishai Hadas mentry->mmap_flag == MLX5_IB_MMAP_TYPE_UAR_NC) 22823f59b6c3SYishai Hadas prot = pgprot_noncached(vma->vm_page_prot); 22833f59b6c3SYishai Hadas else 2284dc2316ebSYishai Hadas prot = pgprot_writecombine(vma->vm_page_prot); 2285dc2316ebSYishai Hadas ret = rdma_user_mmap_io(ucontext, vma, pfn, 2286dc2316ebSYishai Hadas entry->npages * PAGE_SIZE, 2287dc2316ebSYishai Hadas prot, 2288dc2316ebSYishai Hadas entry); 2289dc2316ebSYishai Hadas rdma_user_mmap_entry_put(&mentry->rdma_entry); 2290dc2316ebSYishai Hadas return ret; 229124da0016SAriel Levkovich } 229224da0016SAriel Levkovich 22937be76befSYishai Hadas static u64 mlx5_entry_to_mmap_offset(struct mlx5_user_mmap_entry *entry) 22947be76befSYishai Hadas { 22959b6d3bbcSLeon Romanovsky u64 cmd = (entry->rdma_entry.start_pgoff >> 16) & 0xFFFF; 22969b6d3bbcSLeon Romanovsky u64 index = entry->rdma_entry.start_pgoff & 0xFFFF; 22977be76befSYishai Hadas 22987be76befSYishai Hadas return (((index >> 8) << 16) | (cmd << MLX5_IB_MMAP_CMD_SHIFT) | 22997be76befSYishai Hadas (index & 0xFF)) << PAGE_SHIFT; 23007be76befSYishai Hadas } 23017be76befSYishai Hadas 230237aa5c36SGuy Levi static int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma) 230337aa5c36SGuy Levi { 230437aa5c36SGuy Levi struct mlx5_ib_ucontext *context = to_mucontext(ibcontext); 230537aa5c36SGuy Levi struct mlx5_ib_dev *dev = to_mdev(ibcontext->device); 230637aa5c36SGuy Levi unsigned long command; 230737aa5c36SGuy Levi phys_addr_t pfn; 230837aa5c36SGuy Levi 230937aa5c36SGuy Levi command = get_command(vma->vm_pgoff); 231037aa5c36SGuy Levi switch (command) { 231137aa5c36SGuy Levi case MLX5_IB_MMAP_WC_PAGE: 23121f3db161SYishai Hadas case MLX5_IB_MMAP_ALLOC_WC: 23131f3db161SYishai Hadas if (!dev->wc_support) 23141f3db161SYishai Hadas return -EPERM; 23151f3db161SYishai Hadas fallthrough; 231637aa5c36SGuy Levi case MLX5_IB_MMAP_NC_PAGE: 231737aa5c36SGuy Levi case MLX5_IB_MMAP_REGULAR_PAGE: 23187c2344c3SMaor Gottlieb return uar_mmap(dev, command, vma, context); 2319e126ba97SEli Cohen 2320e126ba97SEli Cohen case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES: 2321e126ba97SEli Cohen return -ENOSYS; 2322e126ba97SEli Cohen 2323d69e3bcfSMatan Barak case MLX5_IB_MMAP_CORE_CLOCK: 2324d69e3bcfSMatan Barak if (vma->vm_end - vma->vm_start != PAGE_SIZE) 2325d69e3bcfSMatan Barak return -EINVAL; 2326d69e3bcfSMatan Barak 23276cbac1e4SMatan Barak if (vma->vm_flags & VM_WRITE) 2328d69e3bcfSMatan Barak return -EPERM; 23291c71222eSSuren Baghdasaryan vm_flags_clear(vma, VM_MAYWRITE); 2330d69e3bcfSMatan Barak 2331d69e3bcfSMatan Barak /* Don't expose to user-space information it shouldn't have */ 2332d69e3bcfSMatan Barak if (PAGE_SIZE > 4096) 2333d69e3bcfSMatan Barak return -EOPNOTSUPP; 2334d69e3bcfSMatan Barak 2335d69e3bcfSMatan Barak pfn = (dev->mdev->iseg_base + 2336d69e3bcfSMatan Barak offsetof(struct mlx5_init_seg, internal_timer_h)) >> 2337d69e3bcfSMatan Barak PAGE_SHIFT; 2338d5e560d3SJason Gunthorpe return rdma_user_mmap_io(&context->ibucontext, vma, pfn, 2339d5e560d3SJason Gunthorpe PAGE_SIZE, 2340c043ff2cSMichal Kalderon pgprot_noncached(vma->vm_page_prot), 2341c043ff2cSMichal Kalderon NULL); 23425c99eaecSFeras Daoud case MLX5_IB_MMAP_CLOCK_INFO: 23435c99eaecSFeras Daoud return mlx5_ib_mmap_clock_info_page(dev, vma, context); 2344d69e3bcfSMatan Barak 2345e126ba97SEli Cohen default: 2346dc2316ebSYishai Hadas return mlx5_ib_mmap_offset(dev, vma, ibcontext); 2347e126ba97SEli Cohen } 2348e126ba97SEli Cohen 2349e126ba97SEli Cohen return 0; 2350e126ba97SEli Cohen } 2351e126ba97SEli Cohen 2352ff23dfa1SShamir Rabinovitch static int mlx5_ib_alloc_pd(struct ib_pd *ibpd, struct ib_udata *udata) 2353e126ba97SEli Cohen { 235421a428a0SLeon Romanovsky struct mlx5_ib_pd *pd = to_mpd(ibpd); 235521a428a0SLeon Romanovsky struct ib_device *ibdev = ibpd->device; 2356e126ba97SEli Cohen struct mlx5_ib_alloc_pd_resp resp; 2357e126ba97SEli Cohen int err; 2358a1069c1cSYishai Hadas u32 out[MLX5_ST_SZ_DW(alloc_pd_out)] = {}; 2359a1069c1cSYishai Hadas u32 in[MLX5_ST_SZ_DW(alloc_pd_in)] = {}; 2360a1069c1cSYishai Hadas u16 uid = 0; 2361ff23dfa1SShamir Rabinovitch struct mlx5_ib_ucontext *context = rdma_udata_to_drv_context( 2362ff23dfa1SShamir Rabinovitch udata, struct mlx5_ib_ucontext, ibucontext); 2363e126ba97SEli Cohen 2364ff23dfa1SShamir Rabinovitch uid = context ? context->devx_uid : 0; 2365a1069c1cSYishai Hadas MLX5_SET(alloc_pd_in, in, opcode, MLX5_CMD_OP_ALLOC_PD); 2366a1069c1cSYishai Hadas MLX5_SET(alloc_pd_in, in, uid, uid); 236731578defSLeon Romanovsky err = mlx5_cmd_exec_inout(to_mdev(ibdev)->mdev, alloc_pd, in, out); 236821a428a0SLeon Romanovsky if (err) 236921a428a0SLeon Romanovsky return err; 2370e126ba97SEli Cohen 2371a1069c1cSYishai Hadas pd->pdn = MLX5_GET(alloc_pd_out, out, pd); 2372a1069c1cSYishai Hadas pd->uid = uid; 2373ff23dfa1SShamir Rabinovitch if (udata) { 2374e126ba97SEli Cohen resp.pdn = pd->pdn; 2375e126ba97SEli Cohen if (ib_copy_to_udata(udata, &resp, sizeof(resp))) { 2376a1069c1cSYishai Hadas mlx5_cmd_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn, uid); 237721a428a0SLeon Romanovsky return -EFAULT; 2378e126ba97SEli Cohen } 2379e126ba97SEli Cohen } 2380e126ba97SEli Cohen 238121a428a0SLeon Romanovsky return 0; 2382e126ba97SEli Cohen } 2383e126ba97SEli Cohen 238491a7c58fSLeon Romanovsky static int mlx5_ib_dealloc_pd(struct ib_pd *pd, struct ib_udata *udata) 2385e126ba97SEli Cohen { 2386e126ba97SEli Cohen struct mlx5_ib_dev *mdev = to_mdev(pd->device); 2387e126ba97SEli Cohen struct mlx5_ib_pd *mpd = to_mpd(pd); 2388e126ba97SEli Cohen 238991a7c58fSLeon Romanovsky return mlx5_cmd_dealloc_pd(mdev->mdev, mpd->pdn, mpd->uid); 2390e126ba97SEli Cohen } 2391e126ba97SEli Cohen 2392e126ba97SEli Cohen static int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid) 2393e126ba97SEli Cohen { 2394e126ba97SEli Cohen struct mlx5_ib_dev *dev = to_mdev(ibqp->device); 239581e30880SYishai Hadas struct mlx5_ib_qp *mqp = to_mqp(ibqp); 2396e126ba97SEli Cohen int err; 2397539ec982SYishai Hadas u16 uid; 2398539ec982SYishai Hadas 2399539ec982SYishai Hadas uid = ibqp->pd ? 2400539ec982SYishai Hadas to_mpd(ibqp->pd)->uid : 0; 2401e126ba97SEli Cohen 24022be08c30SLeon Romanovsky if (mqp->flags & IB_QP_CREATE_SOURCE_QPN) { 240381e30880SYishai Hadas mlx5_ib_dbg(dev, "Attaching a multi cast group to underlay QP is not supported\n"); 240481e30880SYishai Hadas return -EOPNOTSUPP; 240581e30880SYishai Hadas } 240681e30880SYishai Hadas 2407539ec982SYishai Hadas err = mlx5_cmd_attach_mcg(dev->mdev, gid, ibqp->qp_num, uid); 2408e126ba97SEli Cohen if (err) 2409e126ba97SEli Cohen mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n", 2410e126ba97SEli Cohen ibqp->qp_num, gid->raw); 2411e126ba97SEli Cohen 2412e126ba97SEli Cohen return err; 2413e126ba97SEli Cohen } 2414e126ba97SEli Cohen 2415e126ba97SEli Cohen static int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid) 2416e126ba97SEli Cohen { 2417e126ba97SEli Cohen struct mlx5_ib_dev *dev = to_mdev(ibqp->device); 2418e126ba97SEli Cohen int err; 2419539ec982SYishai Hadas u16 uid; 2420e126ba97SEli Cohen 2421539ec982SYishai Hadas uid = ibqp->pd ? 2422539ec982SYishai Hadas to_mpd(ibqp->pd)->uid : 0; 2423539ec982SYishai Hadas err = mlx5_cmd_detach_mcg(dev->mdev, gid, ibqp->qp_num, uid); 2424e126ba97SEli Cohen if (err) 2425e126ba97SEli Cohen mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n", 2426e126ba97SEli Cohen ibqp->qp_num, gid->raw); 2427e126ba97SEli Cohen 2428e126ba97SEli Cohen return err; 2429e126ba97SEli Cohen } 2430e126ba97SEli Cohen 2431e126ba97SEli Cohen static int init_node_data(struct mlx5_ib_dev *dev) 2432e126ba97SEli Cohen { 24331b5daf11SMajd Dibbiny int err; 2434e126ba97SEli Cohen 24351b5daf11SMajd Dibbiny err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc); 2436e126ba97SEli Cohen if (err) 2437e126ba97SEli Cohen return err; 24381b5daf11SMajd Dibbiny 24391b5daf11SMajd Dibbiny dev->mdev->rev_id = dev->mdev->pdev->revision; 24401b5daf11SMajd Dibbiny 24411b5daf11SMajd Dibbiny return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid); 2442e126ba97SEli Cohen } 2443e126ba97SEli Cohen 2444508a523fSParav Pandit static ssize_t fw_pages_show(struct device *device, 2445508a523fSParav Pandit 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", dev->mdev->priv.fw_pages); 2451e126ba97SEli Cohen } 2452508a523fSParav Pandit static DEVICE_ATTR_RO(fw_pages); 2453e126ba97SEli Cohen 2454508a523fSParav Pandit static ssize_t reg_pages_show(struct device *device, 2455e126ba97SEli Cohen 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); 2459e126ba97SEli Cohen 24601c7fd726SJoe Perches return sysfs_emit(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages)); 2461e126ba97SEli Cohen } 2462508a523fSParav Pandit static DEVICE_ATTR_RO(reg_pages); 2463e126ba97SEli Cohen 2464508a523fSParav Pandit static ssize_t hca_type_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, "MT%d\n", dev->mdev->pdev->device); 2471e126ba97SEli Cohen } 2472508a523fSParav Pandit static DEVICE_ATTR_RO(hca_type); 2473e126ba97SEli Cohen 2474508a523fSParav Pandit static ssize_t hw_rev_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, "%x\n", dev->mdev->rev_id); 2481e126ba97SEli Cohen } 2482508a523fSParav Pandit static DEVICE_ATTR_RO(hw_rev); 2483e126ba97SEli Cohen 2484508a523fSParav Pandit static ssize_t board_id_show(struct device *device, 2485508a523fSParav Pandit struct device_attribute *attr, char *buf) 2486e126ba97SEli Cohen { 2487e126ba97SEli Cohen struct mlx5_ib_dev *dev = 248854747231SParav Pandit rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev); 248954747231SParav Pandit 24901c7fd726SJoe Perches return sysfs_emit(buf, "%.*s\n", MLX5_BOARD_ID_LEN, 24919603b61dSJack Morgenstein dev->mdev->board_id); 2492e126ba97SEli Cohen } 2493508a523fSParav Pandit static DEVICE_ATTR_RO(board_id); 2494e126ba97SEli Cohen 2495508a523fSParav Pandit static struct attribute *mlx5_class_attributes[] = { 2496508a523fSParav Pandit &dev_attr_hw_rev.attr, 2497508a523fSParav Pandit &dev_attr_hca_type.attr, 2498508a523fSParav Pandit &dev_attr_board_id.attr, 2499508a523fSParav Pandit &dev_attr_fw_pages.attr, 2500508a523fSParav Pandit &dev_attr_reg_pages.attr, 2501508a523fSParav Pandit NULL, 2502508a523fSParav Pandit }; 2503e126ba97SEli Cohen 2504508a523fSParav Pandit static const struct attribute_group mlx5_attr_group = { 2505508a523fSParav Pandit .attrs = mlx5_class_attributes, 2506e126ba97SEli Cohen }; 2507e126ba97SEli Cohen 25087722f47eSHaggai Eran static void pkey_change_handler(struct work_struct *work) 25097722f47eSHaggai Eran { 25107722f47eSHaggai Eran struct mlx5_ib_port_resources *ports = 25117722f47eSHaggai Eran container_of(work, struct mlx5_ib_port_resources, 25127722f47eSHaggai Eran pkey_change_work); 25137722f47eSHaggai Eran 25148c9e7f03SLeon Romanovsky if (!ports->gsi) 25158c9e7f03SLeon Romanovsky /* 25168c9e7f03SLeon Romanovsky * We got this event before device was fully configured 25178c9e7f03SLeon Romanovsky * and MAD registration code wasn't called/finished yet. 25188c9e7f03SLeon Romanovsky */ 25198c9e7f03SLeon Romanovsky return; 25208c9e7f03SLeon Romanovsky 25217722f47eSHaggai Eran mlx5_ib_gsi_pkey_change(ports->gsi); 25227722f47eSHaggai Eran } 25237722f47eSHaggai Eran 252489ea94a7SMaor Gottlieb static void mlx5_ib_handle_internal_error(struct mlx5_ib_dev *ibdev) 252589ea94a7SMaor Gottlieb { 252689ea94a7SMaor Gottlieb struct mlx5_ib_qp *mqp; 252789ea94a7SMaor Gottlieb struct mlx5_ib_cq *send_mcq, *recv_mcq; 252889ea94a7SMaor Gottlieb struct mlx5_core_cq *mcq; 252989ea94a7SMaor Gottlieb struct list_head cq_armed_list; 253089ea94a7SMaor Gottlieb unsigned long flags_qp; 253189ea94a7SMaor Gottlieb unsigned long flags_cq; 253289ea94a7SMaor Gottlieb unsigned long flags; 253389ea94a7SMaor Gottlieb 253489ea94a7SMaor Gottlieb INIT_LIST_HEAD(&cq_armed_list); 253589ea94a7SMaor Gottlieb 253689ea94a7SMaor Gottlieb /* Go over qp list reside on that ibdev, sync with create/destroy qp.*/ 253789ea94a7SMaor Gottlieb spin_lock_irqsave(&ibdev->reset_flow_resource_lock, flags); 253889ea94a7SMaor Gottlieb list_for_each_entry(mqp, &ibdev->qp_list, qps_list) { 253989ea94a7SMaor Gottlieb spin_lock_irqsave(&mqp->sq.lock, flags_qp); 254089ea94a7SMaor Gottlieb if (mqp->sq.tail != mqp->sq.head) { 254189ea94a7SMaor Gottlieb send_mcq = to_mcq(mqp->ibqp.send_cq); 254289ea94a7SMaor Gottlieb spin_lock_irqsave(&send_mcq->lock, flags_cq); 254389ea94a7SMaor Gottlieb if (send_mcq->mcq.comp && 254489ea94a7SMaor Gottlieb mqp->ibqp.send_cq->comp_handler) { 254589ea94a7SMaor Gottlieb if (!send_mcq->mcq.reset_notify_added) { 254689ea94a7SMaor Gottlieb send_mcq->mcq.reset_notify_added = 1; 254789ea94a7SMaor Gottlieb list_add_tail(&send_mcq->mcq.reset_notify, 254889ea94a7SMaor Gottlieb &cq_armed_list); 254989ea94a7SMaor Gottlieb } 255089ea94a7SMaor Gottlieb } 255189ea94a7SMaor Gottlieb spin_unlock_irqrestore(&send_mcq->lock, flags_cq); 255289ea94a7SMaor Gottlieb } 255389ea94a7SMaor Gottlieb spin_unlock_irqrestore(&mqp->sq.lock, flags_qp); 255489ea94a7SMaor Gottlieb spin_lock_irqsave(&mqp->rq.lock, flags_qp); 255589ea94a7SMaor Gottlieb /* no handling is needed for SRQ */ 255689ea94a7SMaor Gottlieb if (!mqp->ibqp.srq) { 255789ea94a7SMaor Gottlieb if (mqp->rq.tail != mqp->rq.head) { 255889ea94a7SMaor Gottlieb recv_mcq = to_mcq(mqp->ibqp.recv_cq); 255989ea94a7SMaor Gottlieb spin_lock_irqsave(&recv_mcq->lock, flags_cq); 256089ea94a7SMaor Gottlieb if (recv_mcq->mcq.comp && 256189ea94a7SMaor Gottlieb mqp->ibqp.recv_cq->comp_handler) { 256289ea94a7SMaor Gottlieb if (!recv_mcq->mcq.reset_notify_added) { 256389ea94a7SMaor Gottlieb recv_mcq->mcq.reset_notify_added = 1; 256489ea94a7SMaor Gottlieb list_add_tail(&recv_mcq->mcq.reset_notify, 256589ea94a7SMaor Gottlieb &cq_armed_list); 256689ea94a7SMaor Gottlieb } 256789ea94a7SMaor Gottlieb } 256889ea94a7SMaor Gottlieb spin_unlock_irqrestore(&recv_mcq->lock, 256989ea94a7SMaor Gottlieb flags_cq); 257089ea94a7SMaor Gottlieb } 257189ea94a7SMaor Gottlieb } 257289ea94a7SMaor Gottlieb spin_unlock_irqrestore(&mqp->rq.lock, flags_qp); 257389ea94a7SMaor Gottlieb } 257489ea94a7SMaor Gottlieb /*At that point all inflight post send were put to be executed as of we 257589ea94a7SMaor Gottlieb * lock/unlock above locks Now need to arm all involved CQs. 257689ea94a7SMaor Gottlieb */ 257789ea94a7SMaor Gottlieb list_for_each_entry(mcq, &cq_armed_list, reset_notify) { 25784e0e2ea1SYishai Hadas mcq->comp(mcq, NULL); 257989ea94a7SMaor Gottlieb } 258089ea94a7SMaor Gottlieb spin_unlock_irqrestore(&ibdev->reset_flow_resource_lock, flags); 258189ea94a7SMaor Gottlieb } 258289ea94a7SMaor Gottlieb 258303404e8aSMaor Gottlieb static void delay_drop_handler(struct work_struct *work) 258403404e8aSMaor Gottlieb { 258503404e8aSMaor Gottlieb int err; 258603404e8aSMaor Gottlieb struct mlx5_ib_delay_drop *delay_drop = 258703404e8aSMaor Gottlieb container_of(work, struct mlx5_ib_delay_drop, 258803404e8aSMaor Gottlieb delay_drop_work); 258903404e8aSMaor Gottlieb 2590fe248c3aSMaor Gottlieb atomic_inc(&delay_drop->events_cnt); 2591fe248c3aSMaor Gottlieb 259203404e8aSMaor Gottlieb mutex_lock(&delay_drop->lock); 2593333fbaa0SLeon Romanovsky err = mlx5_core_set_delay_drop(delay_drop->dev, delay_drop->timeout); 259403404e8aSMaor Gottlieb if (err) { 259503404e8aSMaor Gottlieb mlx5_ib_warn(delay_drop->dev, "Failed to set delay drop, timeout=%u\n", 259603404e8aSMaor Gottlieb delay_drop->timeout); 259703404e8aSMaor Gottlieb delay_drop->activate = false; 259803404e8aSMaor Gottlieb } 259903404e8aSMaor Gottlieb mutex_unlock(&delay_drop->lock); 260003404e8aSMaor Gottlieb } 260103404e8aSMaor Gottlieb 260209e574faSSaeed Mahameed static void handle_general_event(struct mlx5_ib_dev *ibdev, struct mlx5_eqe *eqe, 260309e574faSSaeed Mahameed struct ib_event *ibev) 260409e574faSSaeed Mahameed { 26051fb7f897SMark Bloch u32 port = (eqe->data.port.port >> 4) & 0xf; 26066cfdc7e4SAya Levin 260709e574faSSaeed Mahameed switch (eqe->sub_type) { 260809e574faSSaeed Mahameed case MLX5_GENERAL_SUBTYPE_DELAY_DROP_TIMEOUT: 26096cfdc7e4SAya Levin if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) == 26106cfdc7e4SAya Levin IB_LINK_LAYER_ETHERNET) 261109e574faSSaeed Mahameed schedule_work(&ibdev->delay_drop.delay_drop_work); 261209e574faSSaeed Mahameed break; 261309e574faSSaeed Mahameed default: /* do nothing */ 261409e574faSSaeed Mahameed return; 261509e574faSSaeed Mahameed } 261609e574faSSaeed Mahameed } 261709e574faSSaeed Mahameed 2618134e9349SSaeed Mahameed static int handle_port_change(struct mlx5_ib_dev *ibdev, struct mlx5_eqe *eqe, 2619134e9349SSaeed Mahameed struct ib_event *ibev) 2620134e9349SSaeed Mahameed { 26211fb7f897SMark Bloch u32 port = (eqe->data.port.port >> 4) & 0xf; 2622134e9349SSaeed Mahameed 2623134e9349SSaeed Mahameed ibev->element.port_num = port; 2624134e9349SSaeed Mahameed 2625134e9349SSaeed Mahameed switch (eqe->sub_type) { 2626134e9349SSaeed Mahameed case MLX5_PORT_CHANGE_SUBTYPE_ACTIVE: 2627134e9349SSaeed Mahameed case MLX5_PORT_CHANGE_SUBTYPE_DOWN: 2628134e9349SSaeed Mahameed case MLX5_PORT_CHANGE_SUBTYPE_INITIALIZED: 2629134e9349SSaeed Mahameed /* In RoCE, port up/down events are handled in 2630134e9349SSaeed Mahameed * mlx5_netdev_event(). 2631134e9349SSaeed Mahameed */ 2632134e9349SSaeed Mahameed if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) == 2633134e9349SSaeed Mahameed IB_LINK_LAYER_ETHERNET) 2634134e9349SSaeed Mahameed return -EINVAL; 2635134e9349SSaeed Mahameed 2636134e9349SSaeed Mahameed ibev->event = (eqe->sub_type == MLX5_PORT_CHANGE_SUBTYPE_ACTIVE) ? 2637134e9349SSaeed Mahameed IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR; 2638134e9349SSaeed Mahameed break; 2639134e9349SSaeed Mahameed 2640134e9349SSaeed Mahameed case MLX5_PORT_CHANGE_SUBTYPE_LID: 2641134e9349SSaeed Mahameed ibev->event = IB_EVENT_LID_CHANGE; 2642134e9349SSaeed Mahameed break; 2643134e9349SSaeed Mahameed 2644134e9349SSaeed Mahameed case MLX5_PORT_CHANGE_SUBTYPE_PKEY: 2645134e9349SSaeed Mahameed ibev->event = IB_EVENT_PKEY_CHANGE; 2646134e9349SSaeed Mahameed schedule_work(&ibdev->devr.ports[port - 1].pkey_change_work); 2647134e9349SSaeed Mahameed break; 2648134e9349SSaeed Mahameed 2649134e9349SSaeed Mahameed case MLX5_PORT_CHANGE_SUBTYPE_GUID: 2650134e9349SSaeed Mahameed ibev->event = IB_EVENT_GID_CHANGE; 2651134e9349SSaeed Mahameed break; 2652134e9349SSaeed Mahameed 2653134e9349SSaeed Mahameed case MLX5_PORT_CHANGE_SUBTYPE_CLIENT_REREG: 2654134e9349SSaeed Mahameed ibev->event = IB_EVENT_CLIENT_REREGISTER; 2655134e9349SSaeed Mahameed break; 2656134e9349SSaeed Mahameed default: 2657134e9349SSaeed Mahameed return -EINVAL; 2658134e9349SSaeed Mahameed } 2659134e9349SSaeed Mahameed 2660134e9349SSaeed Mahameed return 0; 2661134e9349SSaeed Mahameed } 2662134e9349SSaeed Mahameed 2663d69a24e0SDaniel Jurgens static void mlx5_ib_handle_event(struct work_struct *_work) 2664e126ba97SEli Cohen { 2665d69a24e0SDaniel Jurgens struct mlx5_ib_event_work *work = 2666d69a24e0SDaniel Jurgens container_of(_work, struct mlx5_ib_event_work, work); 2667d69a24e0SDaniel Jurgens struct mlx5_ib_dev *ibdev; 2668e126ba97SEli Cohen struct ib_event ibev; 2669dbaaff2aSEli Cohen bool fatal = false; 2670e126ba97SEli Cohen 2671df097a27SSaeed Mahameed if (work->is_slave) { 2672df097a27SSaeed Mahameed ibdev = mlx5_ib_get_ibdev_from_mpi(work->mpi); 2673d69a24e0SDaniel Jurgens if (!ibdev) 2674d69a24e0SDaniel Jurgens goto out; 2675d69a24e0SDaniel Jurgens } else { 2676df097a27SSaeed Mahameed ibdev = work->dev; 2677d69a24e0SDaniel Jurgens } 2678d69a24e0SDaniel Jurgens 2679d69a24e0SDaniel Jurgens switch (work->event) { 2680e126ba97SEli Cohen case MLX5_DEV_EVENT_SYS_ERROR: 2681e126ba97SEli Cohen ibev.event = IB_EVENT_DEVICE_FATAL; 268289ea94a7SMaor Gottlieb mlx5_ib_handle_internal_error(ibdev); 2683134e9349SSaeed Mahameed ibev.element.port_num = (u8)(unsigned long)work->param; 2684dbaaff2aSEli Cohen fatal = true; 2685e126ba97SEli Cohen break; 2686134e9349SSaeed Mahameed case MLX5_EVENT_TYPE_PORT_CHANGE: 2687134e9349SSaeed Mahameed if (handle_port_change(ibdev, work->param, &ibev)) 2688d69a24e0SDaniel Jurgens goto out; 2689e126ba97SEli Cohen break; 269009e574faSSaeed Mahameed case MLX5_EVENT_TYPE_GENERAL_EVENT: 269109e574faSSaeed Mahameed handle_general_event(ibdev, work->param, &ibev); 2692df561f66SGustavo A. R. Silva fallthrough; 2693bdc37924SSaeed Mahameed default: 269403404e8aSMaor Gottlieb goto out; 2695e126ba97SEli Cohen } 2696e126ba97SEli Cohen 2697e126ba97SEli Cohen ibev.device = &ibdev->ib_dev; 2698e126ba97SEli Cohen 2699134e9349SSaeed Mahameed if (!rdma_is_port_valid(&ibdev->ib_dev, ibev.element.port_num)) { 2700134e9349SSaeed Mahameed mlx5_ib_warn(ibdev, "warning: event on port %d\n", ibev.element.port_num); 270103404e8aSMaor Gottlieb goto out; 2702a0c84c32SEli Cohen } 2703a0c84c32SEli Cohen 2704e126ba97SEli Cohen if (ibdev->ib_active) 2705e126ba97SEli Cohen ib_dispatch_event(&ibev); 2706dbaaff2aSEli Cohen 2707dbaaff2aSEli Cohen if (fatal) 2708dbaaff2aSEli Cohen ibdev->ib_active = false; 270903404e8aSMaor Gottlieb out: 2710d69a24e0SDaniel Jurgens kfree(work); 2711d69a24e0SDaniel Jurgens } 2712d69a24e0SDaniel Jurgens 2713df097a27SSaeed Mahameed static int mlx5_ib_event(struct notifier_block *nb, 2714df097a27SSaeed Mahameed unsigned long event, void *param) 2715d69a24e0SDaniel Jurgens { 2716d69a24e0SDaniel Jurgens struct mlx5_ib_event_work *work; 2717d69a24e0SDaniel Jurgens 2718d69a24e0SDaniel Jurgens work = kmalloc(sizeof(*work), GFP_ATOMIC); 271910bea9c8SLeon Romanovsky if (!work) 2720df097a27SSaeed Mahameed return NOTIFY_DONE; 272110bea9c8SLeon Romanovsky 2722d69a24e0SDaniel Jurgens INIT_WORK(&work->work, mlx5_ib_handle_event); 2723df097a27SSaeed Mahameed work->dev = container_of(nb, struct mlx5_ib_dev, mdev_events); 2724df097a27SSaeed Mahameed work->is_slave = false; 2725d69a24e0SDaniel Jurgens work->param = param; 2726d69a24e0SDaniel Jurgens work->event = event; 2727d69a24e0SDaniel Jurgens 2728d69a24e0SDaniel Jurgens queue_work(mlx5_ib_event_wq, &work->work); 2729df097a27SSaeed Mahameed 2730df097a27SSaeed Mahameed return NOTIFY_OK; 2731df097a27SSaeed Mahameed } 2732df097a27SSaeed Mahameed 2733df097a27SSaeed Mahameed static int mlx5_ib_event_slave_port(struct notifier_block *nb, 2734df097a27SSaeed Mahameed unsigned long event, void *param) 2735df097a27SSaeed Mahameed { 2736df097a27SSaeed Mahameed struct mlx5_ib_event_work *work; 2737df097a27SSaeed Mahameed 2738df097a27SSaeed Mahameed work = kmalloc(sizeof(*work), GFP_ATOMIC); 2739df097a27SSaeed Mahameed if (!work) 2740df097a27SSaeed Mahameed return NOTIFY_DONE; 2741df097a27SSaeed Mahameed 2742df097a27SSaeed Mahameed INIT_WORK(&work->work, mlx5_ib_handle_event); 2743df097a27SSaeed Mahameed work->mpi = container_of(nb, struct mlx5_ib_multiport_info, mdev_events); 2744df097a27SSaeed Mahameed work->is_slave = true; 2745df097a27SSaeed Mahameed work->param = param; 2746df097a27SSaeed Mahameed work->event = event; 2747df097a27SSaeed Mahameed queue_work(mlx5_ib_event_wq, &work->work); 2748df097a27SSaeed Mahameed 2749df097a27SSaeed Mahameed return NOTIFY_OK; 2750d69a24e0SDaniel Jurgens } 2751d69a24e0SDaniel Jurgens 2752c43f1112SMaor Gottlieb static int set_has_smi_cap(struct mlx5_ib_dev *dev) 2753c43f1112SMaor Gottlieb { 2754c43f1112SMaor Gottlieb struct mlx5_hca_vport_context vport_ctx; 2755c43f1112SMaor Gottlieb int err; 2756c43f1112SMaor Gottlieb int port; 2757c43f1112SMaor Gottlieb 27584b83c3caSMark Bloch if (MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_IB) 27594b83c3caSMark Bloch return 0; 27604b83c3caSMark Bloch 27614b83c3caSMark Bloch for (port = 1; port <= dev->num_ports; port++) { 27624b83c3caSMark Bloch if (!MLX5_CAP_GEN(dev->mdev, ib_virt)) { 27634b83c3caSMark Bloch dev->port_caps[port - 1].has_smi = true; 27644b83c3caSMark Bloch continue; 27654b83c3caSMark Bloch } 27664b83c3caSMark Bloch err = mlx5_query_hca_vport_context(dev->mdev, 0, port, 0, 2767c43f1112SMaor Gottlieb &vport_ctx); 2768c43f1112SMaor Gottlieb if (err) { 2769c43f1112SMaor Gottlieb mlx5_ib_err(dev, "query_hca_vport_context for port=%d failed %d\n", 2770c43f1112SMaor Gottlieb port, err); 2771c43f1112SMaor Gottlieb return err; 2772c43f1112SMaor Gottlieb } 27734b83c3caSMark Bloch dev->port_caps[port - 1].has_smi = vport_ctx.has_smi; 2774c43f1112SMaor Gottlieb } 27754b83c3caSMark Bloch 2776c43f1112SMaor Gottlieb return 0; 2777c43f1112SMaor Gottlieb } 2778c43f1112SMaor Gottlieb 2779e126ba97SEli Cohen static void get_ext_port_caps(struct mlx5_ib_dev *dev) 2780e126ba97SEli Cohen { 278113179652SParav Pandit unsigned int port; 2782e126ba97SEli Cohen 278313179652SParav Pandit rdma_for_each_port (&dev->ib_dev, port) 2784e126ba97SEli Cohen mlx5_query_ext_port_caps(dev, port); 2785e126ba97SEli Cohen } 2786e126ba97SEli Cohen 27876e8484c5SMax Gurtovoy static u8 mlx5_get_umr_fence(u8 umr_fence_cap) 27886e8484c5SMax Gurtovoy { 27896e8484c5SMax Gurtovoy switch (umr_fence_cap) { 27906e8484c5SMax Gurtovoy case MLX5_CAP_UMR_FENCE_NONE: 27916e8484c5SMax Gurtovoy return MLX5_FENCE_MODE_NONE; 27926e8484c5SMax Gurtovoy case MLX5_CAP_UMR_FENCE_SMALL: 27936e8484c5SMax Gurtovoy return MLX5_FENCE_MODE_INITIATOR_SMALL; 27946e8484c5SMax Gurtovoy default: 27956e8484c5SMax Gurtovoy return MLX5_FENCE_MODE_STRONG_ORDERING; 27966e8484c5SMax Gurtovoy } 27976e8484c5SMax Gurtovoy } 27986e8484c5SMax Gurtovoy 27991e2b5a90SLeon Romanovsky static int mlx5_ib_dev_res_init(struct mlx5_ib_dev *dev) 2800e126ba97SEli Cohen { 28011e2b5a90SLeon Romanovsky struct mlx5_ib_resources *devr = &dev->devr; 2802e126ba97SEli Cohen struct ib_srq_init_attr attr; 280321a428a0SLeon Romanovsky struct ib_device *ibdev; 2804bcf4c1eaSMatan Barak struct ib_cq_init_attr cq_attr = {.cqe = 1}; 28057722f47eSHaggai Eran int port; 2806e126ba97SEli Cohen int ret = 0; 2807e126ba97SEli Cohen 280821a428a0SLeon Romanovsky ibdev = &dev->ib_dev; 2809e126ba97SEli Cohen 2810f4375443SLeon Romanovsky if (!MLX5_CAP_GEN(dev->mdev, xrc)) 2811f4375443SLeon Romanovsky return -EOPNOTSUPP; 2812f4375443SLeon Romanovsky 281320da44dfSLeon Romanovsky devr->p0 = ib_alloc_pd(ibdev, 0); 281420da44dfSLeon Romanovsky if (IS_ERR(devr->p0)) 281520da44dfSLeon Romanovsky return PTR_ERR(devr->p0); 2816d16e91daSHaggai Eran 281720da44dfSLeon Romanovsky devr->c0 = ib_create_cq(ibdev, NULL, NULL, NULL, &cq_attr); 281820da44dfSLeon Romanovsky if (IS_ERR(devr->c0)) { 281920da44dfSLeon Romanovsky ret = PTR_ERR(devr->c0); 2820e126ba97SEli Cohen goto error1; 2821e126ba97SEli Cohen } 2822e39afe3dSLeon Romanovsky 2823f4375443SLeon Romanovsky ret = mlx5_cmd_xrcd_alloc(dev->mdev, &devr->xrcdn0, 0); 2824f4375443SLeon Romanovsky if (ret) 2825e126ba97SEli Cohen goto error2; 2826e126ba97SEli Cohen 2827f4375443SLeon Romanovsky ret = mlx5_cmd_xrcd_alloc(dev->mdev, &devr->xrcdn1, 0); 2828f4375443SLeon Romanovsky if (ret) 2829e126ba97SEli Cohen goto error3; 2830e126ba97SEli Cohen 2831e126ba97SEli Cohen memset(&attr, 0, sizeof(attr)); 2832e126ba97SEli Cohen attr.attr.max_sge = 1; 2833e126ba97SEli Cohen attr.attr.max_wr = 1; 2834e126ba97SEli Cohen attr.srq_type = IB_SRQT_XRC; 28351a56ff6dSArtemy Kovalyov attr.ext.cq = devr->c0; 2836e126ba97SEli Cohen 283720da44dfSLeon Romanovsky devr->s0 = ib_create_srq(devr->p0, &attr); 283820da44dfSLeon Romanovsky if (IS_ERR(devr->s0)) { 283920da44dfSLeon Romanovsky ret = PTR_ERR(devr->s0); 284068e326deSLeon Romanovsky goto err_create; 284120da44dfSLeon Romanovsky } 2842e126ba97SEli Cohen 28434aa17b28SHaggai Abramonvsky memset(&attr, 0, sizeof(attr)); 28444aa17b28SHaggai Abramonvsky attr.attr.max_sge = 1; 28454aa17b28SHaggai Abramonvsky attr.attr.max_wr = 1; 28464aa17b28SHaggai Abramonvsky attr.srq_type = IB_SRQT_BASIC; 284768e326deSLeon Romanovsky 284820da44dfSLeon Romanovsky devr->s1 = ib_create_srq(devr->p0, &attr); 284920da44dfSLeon Romanovsky if (IS_ERR(devr->s1)) { 285020da44dfSLeon Romanovsky ret = PTR_ERR(devr->s1); 285168e326deSLeon Romanovsky goto error6; 285220da44dfSLeon Romanovsky } 28534aa17b28SHaggai Abramonvsky 2854f8225e34SLeon Romanovsky for (port = 0; port < ARRAY_SIZE(devr->ports); ++port) 28557722f47eSHaggai Eran INIT_WORK(&devr->ports[port].pkey_change_work, 28567722f47eSHaggai Eran pkey_change_handler); 28577722f47eSHaggai Eran 2858e126ba97SEli Cohen return 0; 2859e126ba97SEli Cohen 286068e326deSLeon Romanovsky error6: 286120da44dfSLeon Romanovsky ib_destroy_srq(devr->s0); 286268e326deSLeon Romanovsky err_create: 2863f4375443SLeon Romanovsky mlx5_cmd_xrcd_dealloc(dev->mdev, devr->xrcdn1, 0); 2864e126ba97SEli Cohen error3: 2865f4375443SLeon Romanovsky mlx5_cmd_xrcd_dealloc(dev->mdev, devr->xrcdn0, 0); 2866e126ba97SEli Cohen error2: 286720da44dfSLeon Romanovsky ib_destroy_cq(devr->c0); 2868e126ba97SEli Cohen error1: 286920da44dfSLeon Romanovsky ib_dealloc_pd(devr->p0); 2870e126ba97SEli Cohen return ret; 2871e126ba97SEli Cohen } 2872e126ba97SEli Cohen 28731e2b5a90SLeon Romanovsky static void mlx5_ib_dev_res_cleanup(struct mlx5_ib_dev *dev) 2874e126ba97SEli Cohen { 28751e2b5a90SLeon Romanovsky struct mlx5_ib_resources *devr = &dev->devr; 28767722f47eSHaggai Eran int port; 28777722f47eSHaggai Eran 2878b0791dbfSLeon Romanovsky /* 2879b0791dbfSLeon Romanovsky * Make sure no change P_Key work items are still executing. 2880b0791dbfSLeon Romanovsky * 2881b0791dbfSLeon Romanovsky * At this stage, the mlx5_ib_event should be unregistered 2882b0791dbfSLeon Romanovsky * and it ensures that no new works are added. 2883b0791dbfSLeon Romanovsky */ 28845d8f6a0eSMark Bloch for (port = 0; port < ARRAY_SIZE(devr->ports); ++port) 28857722f47eSHaggai Eran cancel_work_sync(&devr->ports[port].pkey_change_work); 2886b0791dbfSLeon Romanovsky 288720da44dfSLeon Romanovsky ib_destroy_srq(devr->s1); 288820da44dfSLeon Romanovsky ib_destroy_srq(devr->s0); 2889e126ba97SEli Cohen mlx5_cmd_xrcd_dealloc(dev->mdev, devr->xrcdn1, 0); 2890e126ba97SEli Cohen mlx5_cmd_xrcd_dealloc(dev->mdev, devr->xrcdn0, 0); 289120da44dfSLeon Romanovsky ib_destroy_cq(devr->c0); 289220da44dfSLeon Romanovsky ib_dealloc_pd(devr->p0); 2893e126ba97SEli Cohen } 2894e126ba97SEli Cohen 2895b02289b3SArtemy Kovalyov static u32 get_core_cap_flags(struct ib_device *ibdev, 2896b02289b3SArtemy Kovalyov struct mlx5_hca_vport_context *rep) 2897e53505a8SAchiad Shochat { 2898e53505a8SAchiad Shochat struct mlx5_ib_dev *dev = to_mdev(ibdev); 2899e53505a8SAchiad Shochat enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, 1); 2900e53505a8SAchiad Shochat u8 l3_type_cap = MLX5_CAP_ROCE(dev->mdev, l3_type); 2901e53505a8SAchiad Shochat u8 roce_version_cap = MLX5_CAP_ROCE(dev->mdev, roce_version); 290285c7c014SDaniel Jurgens bool raw_support = !mlx5_core_mp_enabled(dev->mdev); 2903e53505a8SAchiad Shochat u32 ret = 0; 2904e53505a8SAchiad Shochat 2905b02289b3SArtemy Kovalyov if (rep->grh_required) 2906b02289b3SArtemy Kovalyov ret |= RDMA_CORE_CAP_IB_GRH_REQUIRED; 2907b02289b3SArtemy Kovalyov 2908e53505a8SAchiad Shochat if (ll == IB_LINK_LAYER_INFINIBAND) 2909b02289b3SArtemy Kovalyov return ret | RDMA_CORE_PORT_IBA_IB; 2910e53505a8SAchiad Shochat 291185c7c014SDaniel Jurgens if (raw_support) 2912b02289b3SArtemy Kovalyov ret |= RDMA_CORE_PORT_RAW_PACKET; 291372cd5717SOr Gerlitz 2914e53505a8SAchiad Shochat if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV4_CAP)) 291572cd5717SOr Gerlitz return ret; 2916e53505a8SAchiad Shochat 2917e53505a8SAchiad Shochat if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV6_CAP)) 291872cd5717SOr Gerlitz return ret; 2919e53505a8SAchiad Shochat 2920e53505a8SAchiad Shochat if (roce_version_cap & MLX5_ROCE_VERSION_1_CAP) 2921e53505a8SAchiad Shochat ret |= RDMA_CORE_PORT_IBA_ROCE; 2922e53505a8SAchiad Shochat 2923e53505a8SAchiad Shochat if (roce_version_cap & MLX5_ROCE_VERSION_2_CAP) 2924e53505a8SAchiad Shochat ret |= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP; 2925e53505a8SAchiad Shochat 2926e53505a8SAchiad Shochat return ret; 2927e53505a8SAchiad Shochat } 2928e53505a8SAchiad Shochat 29291fb7f897SMark Bloch static int mlx5_port_immutable(struct ib_device *ibdev, u32 port_num, 29307738613eSIra Weiny struct ib_port_immutable *immutable) 29317738613eSIra Weiny { 29327738613eSIra Weiny struct ib_port_attr attr; 2933ca5b91d6SOr Gerlitz struct mlx5_ib_dev *dev = to_mdev(ibdev); 2934ca5b91d6SOr Gerlitz enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, port_num); 2935b02289b3SArtemy Kovalyov struct mlx5_hca_vport_context rep = {0}; 29367738613eSIra Weiny int err; 29377738613eSIra Weiny 2938c4550c63SOr Gerlitz err = ib_query_port(ibdev, port_num, &attr); 29397738613eSIra Weiny if (err) 29407738613eSIra Weiny return err; 29417738613eSIra Weiny 2942b02289b3SArtemy Kovalyov if (ll == IB_LINK_LAYER_INFINIBAND) { 2943b02289b3SArtemy Kovalyov err = mlx5_query_hca_vport_context(dev->mdev, 0, port_num, 0, 2944b02289b3SArtemy Kovalyov &rep); 2945b02289b3SArtemy Kovalyov if (err) 2946b02289b3SArtemy Kovalyov return err; 2947b02289b3SArtemy Kovalyov } 2948b02289b3SArtemy Kovalyov 29497738613eSIra Weiny immutable->pkey_tbl_len = attr.pkey_tbl_len; 29507738613eSIra Weiny immutable->gid_tbl_len = attr.gid_tbl_len; 2951b02289b3SArtemy Kovalyov immutable->core_cap_flags = get_core_cap_flags(ibdev, &rep); 2952337877a4SIra Weiny immutable->max_mad_size = IB_MGMT_MAD_SIZE; 29537738613eSIra Weiny 29547738613eSIra Weiny return 0; 29557738613eSIra Weiny } 29567738613eSIra Weiny 29571fb7f897SMark Bloch static int mlx5_port_rep_immutable(struct ib_device *ibdev, u32 port_num, 29588e6efa3aSMark Bloch struct ib_port_immutable *immutable) 29598e6efa3aSMark Bloch { 29608e6efa3aSMark Bloch struct ib_port_attr attr; 29618e6efa3aSMark Bloch int err; 29628e6efa3aSMark Bloch 29638e6efa3aSMark Bloch immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET; 29648e6efa3aSMark Bloch 29658e6efa3aSMark Bloch err = ib_query_port(ibdev, port_num, &attr); 29668e6efa3aSMark Bloch if (err) 29678e6efa3aSMark Bloch return err; 29688e6efa3aSMark Bloch 29698e6efa3aSMark Bloch immutable->pkey_tbl_len = attr.pkey_tbl_len; 29708e6efa3aSMark Bloch immutable->gid_tbl_len = attr.gid_tbl_len; 29718e6efa3aSMark Bloch immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET; 29728e6efa3aSMark Bloch 29738e6efa3aSMark Bloch return 0; 29748e6efa3aSMark Bloch } 29758e6efa3aSMark Bloch 29769abb0d1bSLeon Romanovsky static void get_dev_fw_str(struct ib_device *ibdev, char *str) 2977c7342823SIra Weiny { 2978c7342823SIra Weiny struct mlx5_ib_dev *dev = 2979c7342823SIra Weiny container_of(ibdev, struct mlx5_ib_dev, ib_dev); 29809abb0d1bSLeon Romanovsky snprintf(str, IB_FW_VERSION_NAME_MAX, "%d.%d.%04d", 29819abb0d1bSLeon Romanovsky fw_rev_maj(dev->mdev), fw_rev_min(dev->mdev), 29829abb0d1bSLeon Romanovsky fw_rev_sub(dev->mdev)); 2983c7342823SIra Weiny } 2984c7342823SIra Weiny 298545f95acdSOr Gerlitz static int mlx5_eth_lag_init(struct mlx5_ib_dev *dev) 29869ef9c640SAviv Heller { 29879ef9c640SAviv Heller struct mlx5_core_dev *mdev = dev->mdev; 29889ef9c640SAviv Heller struct mlx5_flow_namespace *ns = mlx5_get_flow_namespace(mdev, 29899ef9c640SAviv Heller MLX5_FLOW_NAMESPACE_LAG); 29909ef9c640SAviv Heller struct mlx5_flow_table *ft; 29919ef9c640SAviv Heller int err; 29929ef9c640SAviv Heller 2993c446d9daSMark Bloch if (!ns || !mlx5_lag_is_active(mdev)) 29949ef9c640SAviv Heller return 0; 29959ef9c640SAviv Heller 29969ef9c640SAviv Heller err = mlx5_cmd_create_vport_lag(mdev); 29979ef9c640SAviv Heller if (err) 29989ef9c640SAviv Heller return err; 29999ef9c640SAviv Heller 30009ef9c640SAviv Heller ft = mlx5_create_lag_demux_flow_table(ns, 0, 0); 30019ef9c640SAviv Heller if (IS_ERR(ft)) { 30029ef9c640SAviv Heller err = PTR_ERR(ft); 30039ef9c640SAviv Heller goto err_destroy_vport_lag; 30049ef9c640SAviv Heller } 30059ef9c640SAviv Heller 30069a4ca38dSMark Bloch dev->flow_db->lag_demux_ft = ft; 300734a30d76SMark Bloch dev->lag_ports = mlx5_lag_get_num_ports(mdev); 30087c34ec19SAviv Heller dev->lag_active = true; 30099ef9c640SAviv Heller return 0; 30109ef9c640SAviv Heller 30119ef9c640SAviv Heller err_destroy_vport_lag: 30129ef9c640SAviv Heller mlx5_cmd_destroy_vport_lag(mdev); 30139ef9c640SAviv Heller return err; 30149ef9c640SAviv Heller } 30159ef9c640SAviv Heller 301645f95acdSOr Gerlitz static void mlx5_eth_lag_cleanup(struct mlx5_ib_dev *dev) 30179ef9c640SAviv Heller { 30189ef9c640SAviv Heller struct mlx5_core_dev *mdev = dev->mdev; 30199ef9c640SAviv Heller 30207c34ec19SAviv Heller if (dev->lag_active) { 30217c34ec19SAviv Heller dev->lag_active = false; 30227c34ec19SAviv Heller 30239a4ca38dSMark Bloch mlx5_destroy_flow_table(dev->flow_db->lag_demux_ft); 30249a4ca38dSMark Bloch dev->flow_db->lag_demux_ft = NULL; 30259ef9c640SAviv Heller 30269ef9c640SAviv Heller mlx5_cmd_destroy_vport_lag(mdev); 30279ef9c640SAviv Heller } 30289ef9c640SAviv Heller } 30299ef9c640SAviv Heller 3030dca55da0SJiri Pirko static void mlx5_netdev_notifier_register(struct mlx5_roce *roce, 3031dca55da0SJiri Pirko struct net_device *netdev) 3032fc24fc5eSAchiad Shochat { 3033e53505a8SAchiad Shochat int err; 3034e53505a8SAchiad Shochat 3035dca55da0SJiri Pirko if (roce->tracking_netdev) 3036dca55da0SJiri Pirko return; 3037dca55da0SJiri Pirko roce->tracking_netdev = netdev; 3038dca55da0SJiri Pirko roce->nb.notifier_call = mlx5_netdev_event; 3039dca55da0SJiri Pirko err = register_netdevice_notifier_dev_net(netdev, &roce->nb, &roce->nn); 3040dca55da0SJiri Pirko WARN_ON(err); 30415ec8c83eSAviv Heller } 3042e53505a8SAchiad Shochat 3043dca55da0SJiri Pirko static void mlx5_netdev_notifier_unregister(struct mlx5_roce *roce) 3044e126ba97SEli Cohen { 3045dca55da0SJiri Pirko if (!roce->tracking_netdev) 3046dca55da0SJiri Pirko return; 3047dca55da0SJiri Pirko unregister_netdevice_notifier_dev_net(roce->tracking_netdev, &roce->nb, 3048dca55da0SJiri Pirko &roce->nn); 3049dca55da0SJiri Pirko roce->tracking_netdev = NULL; 3050e126ba97SEli Cohen } 3051dca55da0SJiri Pirko 3052dca55da0SJiri Pirko static int mlx5e_mdev_notifier_event(struct notifier_block *nb, 3053dca55da0SJiri Pirko unsigned long event, void *data) 3054dca55da0SJiri Pirko { 3055dca55da0SJiri Pirko struct mlx5_roce *roce = container_of(nb, struct mlx5_roce, mdev_nb); 3056dca55da0SJiri Pirko struct net_device *netdev = data; 3057dca55da0SJiri Pirko 3058dca55da0SJiri Pirko switch (event) { 3059dca55da0SJiri Pirko case MLX5_DRIVER_EVENT_UPLINK_NETDEV: 3060dca55da0SJiri Pirko if (netdev) 3061dca55da0SJiri Pirko mlx5_netdev_notifier_register(roce, netdev); 3062dca55da0SJiri Pirko else 3063dca55da0SJiri Pirko mlx5_netdev_notifier_unregister(roce); 3064dca55da0SJiri Pirko break; 3065dca55da0SJiri Pirko default: 3066dca55da0SJiri Pirko return NOTIFY_DONE; 3067dca55da0SJiri Pirko } 3068dca55da0SJiri Pirko 3069dca55da0SJiri Pirko return NOTIFY_OK; 3070dca55da0SJiri Pirko } 3071dca55da0SJiri Pirko 3072dca55da0SJiri Pirko static void mlx5_mdev_netdev_track(struct mlx5_ib_dev *dev, u32 port_num) 3073dca55da0SJiri Pirko { 3074dca55da0SJiri Pirko struct mlx5_roce *roce = &dev->port[port_num].roce; 3075dca55da0SJiri Pirko 3076dca55da0SJiri Pirko roce->mdev_nb.notifier_call = mlx5e_mdev_notifier_event; 3077dca55da0SJiri Pirko mlx5_blocking_notifier_register(dev->mdev, &roce->mdev_nb); 3078dca55da0SJiri Pirko mlx5_core_uplink_netdev_event_replay(dev->mdev); 3079dca55da0SJiri Pirko } 3080dca55da0SJiri Pirko 3081dca55da0SJiri Pirko static void mlx5_mdev_netdev_untrack(struct mlx5_ib_dev *dev, u32 port_num) 3082dca55da0SJiri Pirko { 3083dca55da0SJiri Pirko struct mlx5_roce *roce = &dev->port[port_num].roce; 3084dca55da0SJiri Pirko 3085dca55da0SJiri Pirko mlx5_blocking_notifier_unregister(dev->mdev, &roce->mdev_nb); 3086dca55da0SJiri Pirko mlx5_netdev_notifier_unregister(roce); 3087e126ba97SEli Cohen } 3088e126ba97SEli Cohen 3089e3f1ed1fSLeon Romanovsky static int mlx5_enable_eth(struct mlx5_ib_dev *dev) 3090e126ba97SEli Cohen { 3091e126ba97SEli Cohen int err; 3092e126ba97SEli Cohen 3093c446d9daSMark Bloch if (!dev->is_rep && dev->profile != &raw_eth_profile) { 3094e53505a8SAchiad Shochat err = mlx5_nic_vport_enable_roce(dev->mdev); 3095e53505a8SAchiad Shochat if (err) 30968e6efa3aSMark Bloch return err; 3097c446d9daSMark Bloch } 3098e53505a8SAchiad Shochat 309945f95acdSOr Gerlitz err = mlx5_eth_lag_init(dev); 31009ef9c640SAviv Heller if (err) 31019ef9c640SAviv Heller goto err_disable_roce; 31029ef9c640SAviv Heller 3103e53505a8SAchiad Shochat return 0; 3104e53505a8SAchiad Shochat 31059ef9c640SAviv Heller err_disable_roce: 3106c446d9daSMark Bloch if (!dev->is_rep && dev->profile != &raw_eth_profile) 31079ef9c640SAviv Heller mlx5_nic_vport_disable_roce(dev->mdev); 31089ef9c640SAviv Heller 3109e53505a8SAchiad Shochat return err; 3110fc24fc5eSAchiad Shochat } 3111fc24fc5eSAchiad Shochat 311245f95acdSOr Gerlitz static void mlx5_disable_eth(struct mlx5_ib_dev *dev) 3113fc24fc5eSAchiad Shochat { 311445f95acdSOr Gerlitz mlx5_eth_lag_cleanup(dev); 3115c446d9daSMark Bloch if (!dev->is_rep && dev->profile != &raw_eth_profile) 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 3179fdd350feSPatrisious Haddad mlx5_core_mp_event_replay(ibdev->mdev, 3180fdd350feSPatrisious Haddad MLX5_DRIVER_EVENT_AFFILIATION_REMOVED, 3181fdd350feSPatrisious Haddad NULL); 3182fdd350feSPatrisious Haddad mlx5_core_mp_event_replay(mpi->mdev, 3183fdd350feSPatrisious Haddad MLX5_DRIVER_EVENT_AFFILIATION_REMOVED, 3184fdd350feSPatrisious Haddad NULL); 3185fdd350feSPatrisious Haddad 3186a9e546e7SParav Pandit mlx5_ib_cleanup_cong_debugfs(ibdev, port_num); 3187a9e546e7SParav Pandit 318832f69e4bSDaniel Jurgens spin_lock(&port->mp.mpi_lock); 318932f69e4bSDaniel Jurgens if (!mpi->ibdev) { 319032f69e4bSDaniel Jurgens spin_unlock(&port->mp.mpi_lock); 319132f69e4bSDaniel Jurgens return; 319232f69e4bSDaniel Jurgens } 3193df097a27SSaeed Mahameed 319432f69e4bSDaniel Jurgens mpi->ibdev = NULL; 319532f69e4bSDaniel Jurgens 319632f69e4bSDaniel Jurgens spin_unlock(&port->mp.mpi_lock); 319723eaf3b5SLeon Romanovsky if (mpi->mdev_events.notifier_call) 319823eaf3b5SLeon Romanovsky mlx5_notifier_unregister(mpi->mdev, &mpi->mdev_events); 319923eaf3b5SLeon Romanovsky mpi->mdev_events.notifier_call = NULL; 3200dca55da0SJiri Pirko mlx5_mdev_netdev_untrack(ibdev, port_num); 320132f69e4bSDaniel Jurgens spin_lock(&port->mp.mpi_lock); 320232f69e4bSDaniel Jurgens 320332f69e4bSDaniel Jurgens comps = mpi->mdev_refcnt; 320432f69e4bSDaniel Jurgens if (comps) { 320532f69e4bSDaniel Jurgens mpi->unaffiliate = true; 320632f69e4bSDaniel Jurgens init_completion(&mpi->unref_comp); 320732f69e4bSDaniel Jurgens spin_unlock(&port->mp.mpi_lock); 320832f69e4bSDaniel Jurgens 320932f69e4bSDaniel Jurgens for (i = 0; i < comps; i++) 321032f69e4bSDaniel Jurgens wait_for_completion(&mpi->unref_comp); 321132f69e4bSDaniel Jurgens 321232f69e4bSDaniel Jurgens spin_lock(&port->mp.mpi_lock); 321332f69e4bSDaniel Jurgens mpi->unaffiliate = false; 321432f69e4bSDaniel Jurgens } 321532f69e4bSDaniel Jurgens 321632f69e4bSDaniel Jurgens port->mp.mpi = NULL; 321732f69e4bSDaniel Jurgens 321832f69e4bSDaniel Jurgens spin_unlock(&port->mp.mpi_lock); 321932f69e4bSDaniel Jurgens 322032f69e4bSDaniel Jurgens err = mlx5_nic_vport_unaffiliate_multiport(mpi->mdev); 322132f69e4bSDaniel Jurgens 32221fb7f897SMark Bloch mlx5_ib_dbg(ibdev, "unaffiliated port %u\n", port_num + 1); 322332f69e4bSDaniel Jurgens /* Log an error, still needed to cleanup the pointers and add 322432f69e4bSDaniel Jurgens * it back to the list. 322532f69e4bSDaniel Jurgens */ 322632f69e4bSDaniel Jurgens if (err) 322732f69e4bSDaniel Jurgens mlx5_ib_err(ibdev, "Failed to unaffiliate port %u\n", 322832f69e4bSDaniel Jurgens port_num + 1); 322932f69e4bSDaniel Jurgens 323095579e78SMark Bloch ibdev->port[port_num].roce.last_port_state = IB_PORT_DOWN; 323132f69e4bSDaniel Jurgens } 323232f69e4bSDaniel Jurgens 323332f69e4bSDaniel Jurgens static bool mlx5_ib_bind_slave_port(struct mlx5_ib_dev *ibdev, 323432f69e4bSDaniel Jurgens struct mlx5_ib_multiport_info *mpi) 323532f69e4bSDaniel Jurgens { 32361fb7f897SMark Bloch u32 port_num = mlx5_core_native_port_num(mpi->mdev) - 1; 3237fdd350feSPatrisious Haddad u64 key; 323832f69e4bSDaniel Jurgens int err; 323932f69e4bSDaniel Jurgens 32409dc4cfffSLeon Romanovsky lockdep_assert_held(&mlx5_ib_multiport_mutex); 32419dc4cfffSLeon Romanovsky 324232f69e4bSDaniel Jurgens spin_lock(&ibdev->port[port_num].mp.mpi_lock); 324332f69e4bSDaniel Jurgens if (ibdev->port[port_num].mp.mpi) { 32441fb7f897SMark Bloch mlx5_ib_dbg(ibdev, "port %u already affiliated.\n", 324532f69e4bSDaniel Jurgens port_num + 1); 324632f69e4bSDaniel Jurgens spin_unlock(&ibdev->port[port_num].mp.mpi_lock); 324732f69e4bSDaniel Jurgens return false; 324832f69e4bSDaniel Jurgens } 324932f69e4bSDaniel Jurgens 325032f69e4bSDaniel Jurgens ibdev->port[port_num].mp.mpi = mpi; 325132f69e4bSDaniel Jurgens mpi->ibdev = ibdev; 3252df097a27SSaeed Mahameed mpi->mdev_events.notifier_call = NULL; 325332f69e4bSDaniel Jurgens spin_unlock(&ibdev->port[port_num].mp.mpi_lock); 325432f69e4bSDaniel Jurgens 325532f69e4bSDaniel Jurgens err = mlx5_nic_vport_affiliate_multiport(ibdev->mdev, mpi->mdev); 325632f69e4bSDaniel Jurgens if (err) 325732f69e4bSDaniel Jurgens goto unbind; 325832f69e4bSDaniel Jurgens 3259dca55da0SJiri Pirko mlx5_mdev_netdev_track(ibdev, port_num); 326032f69e4bSDaniel Jurgens 3261df097a27SSaeed Mahameed mpi->mdev_events.notifier_call = mlx5_ib_event_slave_port; 3262df097a27SSaeed Mahameed mlx5_notifier_register(mpi->mdev, &mpi->mdev_events); 3263df097a27SSaeed Mahameed 326473eb8f03SGreg Kroah-Hartman mlx5_ib_init_cong_debugfs(ibdev, port_num); 3265a9e546e7SParav Pandit 3266885faf3cSPatrisious Haddad key = mpi->mdev->priv.adev_idx; 3267fdd350feSPatrisious Haddad mlx5_core_mp_event_replay(mpi->mdev, 3268fdd350feSPatrisious Haddad MLX5_DRIVER_EVENT_AFFILIATION_DONE, 3269fdd350feSPatrisious Haddad &key); 3270fdd350feSPatrisious Haddad mlx5_core_mp_event_replay(ibdev->mdev, 3271fdd350feSPatrisious Haddad MLX5_DRIVER_EVENT_AFFILIATION_DONE, 3272fdd350feSPatrisious Haddad &key); 3273fdd350feSPatrisious Haddad 327432f69e4bSDaniel Jurgens return true; 327532f69e4bSDaniel Jurgens 327632f69e4bSDaniel Jurgens unbind: 327732f69e4bSDaniel Jurgens mlx5_ib_unbind_slave_port(ibdev, mpi); 327832f69e4bSDaniel Jurgens return false; 327932f69e4bSDaniel Jurgens } 328032f69e4bSDaniel Jurgens 328132f69e4bSDaniel Jurgens static int mlx5_ib_init_multiport_master(struct mlx5_ib_dev *dev) 328232f69e4bSDaniel Jurgens { 32831fb7f897SMark Bloch u32 port_num = mlx5_core_native_port_num(dev->mdev) - 1; 328432f69e4bSDaniel Jurgens enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev, 328532f69e4bSDaniel Jurgens port_num + 1); 328632f69e4bSDaniel Jurgens struct mlx5_ib_multiport_info *mpi; 328732f69e4bSDaniel Jurgens int err; 32881fb7f897SMark Bloch u32 i; 328932f69e4bSDaniel Jurgens 329032f69e4bSDaniel Jurgens if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET) 329132f69e4bSDaniel Jurgens return 0; 329232f69e4bSDaniel Jurgens 329332f69e4bSDaniel Jurgens err = mlx5_query_nic_vport_system_image_guid(dev->mdev, 329432f69e4bSDaniel Jurgens &dev->sys_image_guid); 329532f69e4bSDaniel Jurgens if (err) 329632f69e4bSDaniel Jurgens return err; 329732f69e4bSDaniel Jurgens 329832f69e4bSDaniel Jurgens err = mlx5_nic_vport_enable_roce(dev->mdev); 329932f69e4bSDaniel Jurgens if (err) 330032f69e4bSDaniel Jurgens return err; 330132f69e4bSDaniel Jurgens 330232f69e4bSDaniel Jurgens mutex_lock(&mlx5_ib_multiport_mutex); 330332f69e4bSDaniel Jurgens for (i = 0; i < dev->num_ports; i++) { 330432f69e4bSDaniel Jurgens bool bound = false; 330532f69e4bSDaniel Jurgens 330632f69e4bSDaniel Jurgens /* build a stub multiport info struct for the native port. */ 330732f69e4bSDaniel Jurgens if (i == port_num) { 330832f69e4bSDaniel Jurgens mpi = kzalloc(sizeof(*mpi), GFP_KERNEL); 330932f69e4bSDaniel Jurgens if (!mpi) { 331032f69e4bSDaniel Jurgens mutex_unlock(&mlx5_ib_multiport_mutex); 331132f69e4bSDaniel Jurgens mlx5_nic_vport_disable_roce(dev->mdev); 331232f69e4bSDaniel Jurgens return -ENOMEM; 331332f69e4bSDaniel Jurgens } 331432f69e4bSDaniel Jurgens 331532f69e4bSDaniel Jurgens mpi->is_master = true; 331632f69e4bSDaniel Jurgens mpi->mdev = dev->mdev; 331732f69e4bSDaniel Jurgens mpi->sys_image_guid = dev->sys_image_guid; 331832f69e4bSDaniel Jurgens dev->port[i].mp.mpi = mpi; 331932f69e4bSDaniel Jurgens mpi->ibdev = dev; 332032f69e4bSDaniel Jurgens mpi = NULL; 332132f69e4bSDaniel Jurgens continue; 332232f69e4bSDaniel Jurgens } 332332f69e4bSDaniel Jurgens 332432f69e4bSDaniel Jurgens list_for_each_entry(mpi, &mlx5_ib_unaffiliated_port_list, 332532f69e4bSDaniel Jurgens list) { 332632f69e4bSDaniel Jurgens if (dev->sys_image_guid == mpi->sys_image_guid && 332732f69e4bSDaniel Jurgens (mlx5_core_native_port_num(mpi->mdev) - 1) == i) { 332832f69e4bSDaniel Jurgens bound = mlx5_ib_bind_slave_port(dev, mpi); 332932f69e4bSDaniel Jurgens } 333032f69e4bSDaniel Jurgens 333132f69e4bSDaniel Jurgens if (bound) { 3332c42260f1SVu Pham dev_dbg(mpi->mdev->device, 3333c42260f1SVu Pham "removing port from unaffiliated list.\n"); 333432f69e4bSDaniel Jurgens mlx5_ib_dbg(dev, "port %d bound\n", i + 1); 333532f69e4bSDaniel Jurgens list_del(&mpi->list); 333632f69e4bSDaniel Jurgens break; 333732f69e4bSDaniel Jurgens } 333832f69e4bSDaniel Jurgens } 33397416790eSParav Pandit if (!bound) 334032f69e4bSDaniel Jurgens mlx5_ib_dbg(dev, "no free port found for port %d\n", 334132f69e4bSDaniel Jurgens i + 1); 334232f69e4bSDaniel Jurgens } 334332f69e4bSDaniel Jurgens 334432f69e4bSDaniel Jurgens list_add_tail(&dev->ib_dev_list, &mlx5_ib_dev_list); 334532f69e4bSDaniel Jurgens mutex_unlock(&mlx5_ib_multiport_mutex); 334632f69e4bSDaniel Jurgens return err; 334732f69e4bSDaniel Jurgens } 334832f69e4bSDaniel Jurgens 334932f69e4bSDaniel Jurgens static void mlx5_ib_cleanup_multiport_master(struct mlx5_ib_dev *dev) 335032f69e4bSDaniel Jurgens { 33511fb7f897SMark Bloch u32 port_num = mlx5_core_native_port_num(dev->mdev) - 1; 335232f69e4bSDaniel Jurgens enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev, 335332f69e4bSDaniel Jurgens port_num + 1); 33541fb7f897SMark Bloch u32 i; 335532f69e4bSDaniel Jurgens 335632f69e4bSDaniel Jurgens if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET) 335732f69e4bSDaniel Jurgens return; 335832f69e4bSDaniel Jurgens 335932f69e4bSDaniel Jurgens mutex_lock(&mlx5_ib_multiport_mutex); 336032f69e4bSDaniel Jurgens for (i = 0; i < dev->num_ports; i++) { 336132f69e4bSDaniel Jurgens if (dev->port[i].mp.mpi) { 336232f69e4bSDaniel Jurgens /* Destroy the native port stub */ 336332f69e4bSDaniel Jurgens if (i == port_num) { 336432f69e4bSDaniel Jurgens kfree(dev->port[i].mp.mpi); 336532f69e4bSDaniel Jurgens dev->port[i].mp.mpi = NULL; 336632f69e4bSDaniel Jurgens } else { 33671fb7f897SMark Bloch mlx5_ib_dbg(dev, "unbinding port_num: %u\n", 33681fb7f897SMark Bloch i + 1); 33697ce6095eSLeon Romanovsky list_add_tail(&dev->port[i].mp.mpi->list, 33707ce6095eSLeon Romanovsky &mlx5_ib_unaffiliated_port_list); 33714a754d76SLeon Romanovsky mlx5_ib_unbind_slave_port(dev, 33724a754d76SLeon Romanovsky dev->port[i].mp.mpi); 337332f69e4bSDaniel Jurgens } 337432f69e4bSDaniel Jurgens } 337532f69e4bSDaniel Jurgens } 337632f69e4bSDaniel Jurgens 337732f69e4bSDaniel Jurgens mlx5_ib_dbg(dev, "removing from devlist\n"); 337832f69e4bSDaniel Jurgens list_del(&dev->ib_dev_list); 337932f69e4bSDaniel Jurgens mutex_unlock(&mlx5_ib_multiport_mutex); 338032f69e4bSDaniel Jurgens 338132f69e4bSDaniel Jurgens mlx5_nic_vport_disable_roce(dev->mdev); 338232f69e4bSDaniel Jurgens } 338332f69e4bSDaniel Jurgens 3384342ee59dSYishai Hadas static int mmap_obj_cleanup(struct ib_uobject *uobject, 33857be76befSYishai Hadas enum rdma_remove_reason why, 33867be76befSYishai Hadas struct uverbs_attr_bundle *attrs) 33877be76befSYishai Hadas { 33887be76befSYishai Hadas struct mlx5_user_mmap_entry *obj = uobject->object; 33897be76befSYishai Hadas 33907be76befSYishai Hadas rdma_user_mmap_entry_remove(&obj->rdma_entry); 33917be76befSYishai Hadas return 0; 33927be76befSYishai Hadas } 33937be76befSYishai Hadas 3394342ee59dSYishai Hadas static int mlx5_rdma_user_mmap_entry_insert(struct mlx5_ib_ucontext *c, 3395342ee59dSYishai Hadas struct mlx5_user_mmap_entry *entry, 3396342ee59dSYishai Hadas size_t length) 3397342ee59dSYishai Hadas { 3398342ee59dSYishai Hadas return rdma_user_mmap_entry_insert_range( 3399342ee59dSYishai Hadas &c->ibucontext, &entry->rdma_entry, length, 3400342ee59dSYishai Hadas (MLX5_IB_MMAP_OFFSET_START << 16), 3401342ee59dSYishai Hadas ((MLX5_IB_MMAP_OFFSET_END << 16) + (1UL << 16) - 1)); 3402342ee59dSYishai Hadas } 3403342ee59dSYishai Hadas 34047be76befSYishai Hadas static struct mlx5_user_mmap_entry * 34057be76befSYishai Hadas alloc_var_entry(struct mlx5_ib_ucontext *c) 34067be76befSYishai Hadas { 34077be76befSYishai Hadas struct mlx5_user_mmap_entry *entry; 34087be76befSYishai Hadas struct mlx5_var_table *var_table; 34097be76befSYishai Hadas u32 page_idx; 34107be76befSYishai Hadas int err; 34117be76befSYishai Hadas 34127be76befSYishai Hadas var_table = &to_mdev(c->ibucontext.device)->var_table; 34137be76befSYishai Hadas entry = kzalloc(sizeof(*entry), GFP_KERNEL); 34147be76befSYishai Hadas if (!entry) 34157be76befSYishai Hadas return ERR_PTR(-ENOMEM); 34167be76befSYishai Hadas 34177be76befSYishai Hadas mutex_lock(&var_table->bitmap_lock); 34187be76befSYishai Hadas page_idx = find_first_zero_bit(var_table->bitmap, 34197be76befSYishai Hadas var_table->num_var_hw_entries); 34207be76befSYishai Hadas if (page_idx >= var_table->num_var_hw_entries) { 34217be76befSYishai Hadas err = -ENOSPC; 34227be76befSYishai Hadas mutex_unlock(&var_table->bitmap_lock); 34237be76befSYishai Hadas goto end; 34247be76befSYishai Hadas } 34257be76befSYishai Hadas 34267be76befSYishai Hadas set_bit(page_idx, var_table->bitmap); 34277be76befSYishai Hadas mutex_unlock(&var_table->bitmap_lock); 34287be76befSYishai Hadas 34297be76befSYishai Hadas entry->address = var_table->hw_start_addr + 34307be76befSYishai Hadas (page_idx * var_table->stride_size); 34317be76befSYishai Hadas entry->page_idx = page_idx; 34327be76befSYishai Hadas entry->mmap_flag = MLX5_IB_MMAP_TYPE_VAR; 34337be76befSYishai Hadas 3434342ee59dSYishai Hadas err = mlx5_rdma_user_mmap_entry_insert(c, entry, 3435342ee59dSYishai Hadas var_table->stride_size); 34367be76befSYishai Hadas if (err) 34377be76befSYishai Hadas goto err_insert; 34387be76befSYishai Hadas 34397be76befSYishai Hadas return entry; 34407be76befSYishai Hadas 34417be76befSYishai Hadas err_insert: 34427be76befSYishai Hadas mutex_lock(&var_table->bitmap_lock); 34437be76befSYishai Hadas clear_bit(page_idx, var_table->bitmap); 34447be76befSYishai Hadas mutex_unlock(&var_table->bitmap_lock); 34457be76befSYishai Hadas end: 34467be76befSYishai Hadas kfree(entry); 34477be76befSYishai Hadas return ERR_PTR(err); 34487be76befSYishai Hadas } 34497be76befSYishai Hadas 34507be76befSYishai Hadas static int UVERBS_HANDLER(MLX5_IB_METHOD_VAR_OBJ_ALLOC)( 34517be76befSYishai Hadas struct uverbs_attr_bundle *attrs) 34527be76befSYishai Hadas { 34537be76befSYishai Hadas struct ib_uobject *uobj = uverbs_attr_get_uobject( 34547be76befSYishai Hadas attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_HANDLE); 34557be76befSYishai Hadas struct mlx5_ib_ucontext *c; 34567be76befSYishai Hadas struct mlx5_user_mmap_entry *entry; 34577be76befSYishai Hadas u64 mmap_offset; 34587be76befSYishai Hadas u32 length; 34597be76befSYishai Hadas int err; 34607be76befSYishai Hadas 34617be76befSYishai Hadas c = to_mucontext(ib_uverbs_get_ucontext(attrs)); 34627be76befSYishai Hadas if (IS_ERR(c)) 34637be76befSYishai Hadas return PTR_ERR(c); 34647be76befSYishai Hadas 34657be76befSYishai Hadas entry = alloc_var_entry(c); 34667be76befSYishai Hadas if (IS_ERR(entry)) 34677be76befSYishai Hadas return PTR_ERR(entry); 34687be76befSYishai Hadas 34697be76befSYishai Hadas mmap_offset = mlx5_entry_to_mmap_offset(entry); 34707be76befSYishai Hadas length = entry->rdma_entry.npages * PAGE_SIZE; 34717be76befSYishai Hadas uobj->object = entry; 34720ac8903cSJason Gunthorpe uverbs_finalize_uobj_create(attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_HANDLE); 34737be76befSYishai Hadas 34747be76befSYishai Hadas err = uverbs_copy_to(attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_MMAP_OFFSET, 34757be76befSYishai Hadas &mmap_offset, sizeof(mmap_offset)); 34767be76befSYishai Hadas if (err) 34770ac8903cSJason Gunthorpe return err; 34787be76befSYishai Hadas 34797be76befSYishai Hadas err = uverbs_copy_to(attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_PAGE_ID, 34807be76befSYishai Hadas &entry->page_idx, sizeof(entry->page_idx)); 34817be76befSYishai Hadas if (err) 34820ac8903cSJason Gunthorpe return err; 34837be76befSYishai Hadas 34847be76befSYishai Hadas err = uverbs_copy_to(attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_MMAP_LENGTH, 34857be76befSYishai Hadas &length, sizeof(length)); 34867be76befSYishai Hadas return err; 34877be76befSYishai Hadas } 34887be76befSYishai Hadas 34897be76befSYishai Hadas DECLARE_UVERBS_NAMED_METHOD( 34907be76befSYishai Hadas MLX5_IB_METHOD_VAR_OBJ_ALLOC, 34917be76befSYishai Hadas UVERBS_ATTR_IDR(MLX5_IB_ATTR_VAR_OBJ_ALLOC_HANDLE, 34927be76befSYishai Hadas MLX5_IB_OBJECT_VAR, 34937be76befSYishai Hadas UVERBS_ACCESS_NEW, 34947be76befSYishai Hadas UA_MANDATORY), 34957be76befSYishai Hadas UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_VAR_OBJ_ALLOC_PAGE_ID, 34967be76befSYishai Hadas UVERBS_ATTR_TYPE(u32), 34977be76befSYishai Hadas UA_MANDATORY), 34987be76befSYishai Hadas UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_VAR_OBJ_ALLOC_MMAP_LENGTH, 34997be76befSYishai Hadas UVERBS_ATTR_TYPE(u32), 35007be76befSYishai Hadas UA_MANDATORY), 35017be76befSYishai Hadas UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_VAR_OBJ_ALLOC_MMAP_OFFSET, 35027be76befSYishai Hadas UVERBS_ATTR_TYPE(u64), 35037be76befSYishai Hadas UA_MANDATORY)); 35047be76befSYishai Hadas 35057be76befSYishai Hadas DECLARE_UVERBS_NAMED_METHOD_DESTROY( 35067be76befSYishai Hadas MLX5_IB_METHOD_VAR_OBJ_DESTROY, 35077be76befSYishai Hadas UVERBS_ATTR_IDR(MLX5_IB_ATTR_VAR_OBJ_DESTROY_HANDLE, 35087be76befSYishai Hadas MLX5_IB_OBJECT_VAR, 35097be76befSYishai Hadas UVERBS_ACCESS_DESTROY, 35107be76befSYishai Hadas UA_MANDATORY)); 35117be76befSYishai Hadas 35127be76befSYishai Hadas DECLARE_UVERBS_NAMED_OBJECT(MLX5_IB_OBJECT_VAR, 3513342ee59dSYishai Hadas UVERBS_TYPE_ALLOC_IDR(mmap_obj_cleanup), 35147be76befSYishai Hadas &UVERBS_METHOD(MLX5_IB_METHOD_VAR_OBJ_ALLOC), 35157be76befSYishai Hadas &UVERBS_METHOD(MLX5_IB_METHOD_VAR_OBJ_DESTROY)); 35167be76befSYishai Hadas 35177be76befSYishai Hadas static bool var_is_supported(struct ib_device *device) 35187be76befSYishai Hadas { 35197be76befSYishai Hadas struct mlx5_ib_dev *dev = to_mdev(device); 35207be76befSYishai Hadas 35217be76befSYishai Hadas return (MLX5_CAP_GEN_64(dev->mdev, general_obj_types) & 35227be76befSYishai Hadas MLX5_GENERAL_OBJ_TYPES_CAP_VIRTIO_NET_Q); 35237be76befSYishai Hadas } 35247be76befSYishai Hadas 3525342ee59dSYishai Hadas static struct mlx5_user_mmap_entry * 3526342ee59dSYishai Hadas alloc_uar_entry(struct mlx5_ib_ucontext *c, 3527342ee59dSYishai Hadas enum mlx5_ib_uapi_uar_alloc_type alloc_type) 3528342ee59dSYishai Hadas { 3529342ee59dSYishai Hadas struct mlx5_user_mmap_entry *entry; 3530342ee59dSYishai Hadas struct mlx5_ib_dev *dev; 3531342ee59dSYishai Hadas u32 uar_index; 3532342ee59dSYishai Hadas int err; 3533342ee59dSYishai Hadas 3534342ee59dSYishai Hadas entry = kzalloc(sizeof(*entry), GFP_KERNEL); 3535342ee59dSYishai Hadas if (!entry) 3536342ee59dSYishai Hadas return ERR_PTR(-ENOMEM); 3537342ee59dSYishai Hadas 3538342ee59dSYishai Hadas dev = to_mdev(c->ibucontext.device); 3539d2c8a155SMeir Lichtinger err = mlx5_cmd_uar_alloc(dev->mdev, &uar_index, c->devx_uid); 3540342ee59dSYishai Hadas if (err) 3541342ee59dSYishai Hadas goto end; 3542342ee59dSYishai Hadas 3543342ee59dSYishai Hadas entry->page_idx = uar_index; 3544342ee59dSYishai Hadas entry->address = uar_index2paddress(dev, uar_index); 3545342ee59dSYishai Hadas if (alloc_type == MLX5_IB_UAPI_UAR_ALLOC_TYPE_BF) 3546342ee59dSYishai Hadas entry->mmap_flag = MLX5_IB_MMAP_TYPE_UAR_WC; 3547342ee59dSYishai Hadas else 3548342ee59dSYishai Hadas entry->mmap_flag = MLX5_IB_MMAP_TYPE_UAR_NC; 3549342ee59dSYishai Hadas 3550342ee59dSYishai Hadas err = mlx5_rdma_user_mmap_entry_insert(c, entry, PAGE_SIZE); 3551342ee59dSYishai Hadas if (err) 3552342ee59dSYishai Hadas goto err_insert; 3553342ee59dSYishai Hadas 3554342ee59dSYishai Hadas return entry; 3555342ee59dSYishai Hadas 3556342ee59dSYishai Hadas err_insert: 3557d2c8a155SMeir Lichtinger mlx5_cmd_uar_dealloc(dev->mdev, uar_index, c->devx_uid); 3558342ee59dSYishai Hadas end: 3559342ee59dSYishai Hadas kfree(entry); 3560342ee59dSYishai Hadas return ERR_PTR(err); 3561342ee59dSYishai Hadas } 3562342ee59dSYishai Hadas 3563342ee59dSYishai Hadas static int UVERBS_HANDLER(MLX5_IB_METHOD_UAR_OBJ_ALLOC)( 3564342ee59dSYishai Hadas struct uverbs_attr_bundle *attrs) 3565342ee59dSYishai Hadas { 3566342ee59dSYishai Hadas struct ib_uobject *uobj = uverbs_attr_get_uobject( 3567342ee59dSYishai Hadas attrs, MLX5_IB_ATTR_UAR_OBJ_ALLOC_HANDLE); 3568342ee59dSYishai Hadas enum mlx5_ib_uapi_uar_alloc_type alloc_type; 3569342ee59dSYishai Hadas struct mlx5_ib_ucontext *c; 3570342ee59dSYishai Hadas struct mlx5_user_mmap_entry *entry; 3571342ee59dSYishai Hadas u64 mmap_offset; 3572342ee59dSYishai Hadas u32 length; 3573342ee59dSYishai Hadas int err; 3574342ee59dSYishai Hadas 3575342ee59dSYishai Hadas c = to_mucontext(ib_uverbs_get_ucontext(attrs)); 3576342ee59dSYishai Hadas if (IS_ERR(c)) 3577342ee59dSYishai Hadas return PTR_ERR(c); 3578342ee59dSYishai Hadas 3579342ee59dSYishai Hadas err = uverbs_get_const(&alloc_type, attrs, 3580342ee59dSYishai Hadas MLX5_IB_ATTR_UAR_OBJ_ALLOC_TYPE); 3581342ee59dSYishai Hadas if (err) 3582342ee59dSYishai Hadas return err; 3583342ee59dSYishai Hadas 3584342ee59dSYishai Hadas if (alloc_type != MLX5_IB_UAPI_UAR_ALLOC_TYPE_BF && 3585342ee59dSYishai Hadas alloc_type != MLX5_IB_UAPI_UAR_ALLOC_TYPE_NC) 3586342ee59dSYishai Hadas return -EOPNOTSUPP; 3587342ee59dSYishai Hadas 3588342ee59dSYishai Hadas if (!to_mdev(c->ibucontext.device)->wc_support && 3589342ee59dSYishai Hadas alloc_type == MLX5_IB_UAPI_UAR_ALLOC_TYPE_BF) 3590342ee59dSYishai Hadas return -EOPNOTSUPP; 3591342ee59dSYishai Hadas 3592342ee59dSYishai Hadas entry = alloc_uar_entry(c, alloc_type); 3593342ee59dSYishai Hadas if (IS_ERR(entry)) 3594342ee59dSYishai Hadas return PTR_ERR(entry); 3595342ee59dSYishai Hadas 3596342ee59dSYishai Hadas mmap_offset = mlx5_entry_to_mmap_offset(entry); 3597342ee59dSYishai Hadas length = entry->rdma_entry.npages * PAGE_SIZE; 3598342ee59dSYishai Hadas uobj->object = entry; 35990ac8903cSJason Gunthorpe uverbs_finalize_uobj_create(attrs, MLX5_IB_ATTR_UAR_OBJ_ALLOC_HANDLE); 3600342ee59dSYishai Hadas 3601342ee59dSYishai Hadas err = uverbs_copy_to(attrs, MLX5_IB_ATTR_UAR_OBJ_ALLOC_MMAP_OFFSET, 3602342ee59dSYishai Hadas &mmap_offset, sizeof(mmap_offset)); 3603342ee59dSYishai Hadas if (err) 36040ac8903cSJason Gunthorpe return err; 3605342ee59dSYishai Hadas 3606342ee59dSYishai Hadas err = uverbs_copy_to(attrs, MLX5_IB_ATTR_UAR_OBJ_ALLOC_PAGE_ID, 3607342ee59dSYishai Hadas &entry->page_idx, sizeof(entry->page_idx)); 3608342ee59dSYishai Hadas if (err) 36090ac8903cSJason Gunthorpe return err; 3610342ee59dSYishai Hadas 3611342ee59dSYishai Hadas err = uverbs_copy_to(attrs, MLX5_IB_ATTR_UAR_OBJ_ALLOC_MMAP_LENGTH, 3612342ee59dSYishai Hadas &length, sizeof(length)); 3613342ee59dSYishai Hadas return err; 3614342ee59dSYishai Hadas } 3615342ee59dSYishai Hadas 3616342ee59dSYishai Hadas DECLARE_UVERBS_NAMED_METHOD( 3617342ee59dSYishai Hadas MLX5_IB_METHOD_UAR_OBJ_ALLOC, 3618342ee59dSYishai Hadas UVERBS_ATTR_IDR(MLX5_IB_ATTR_UAR_OBJ_ALLOC_HANDLE, 3619342ee59dSYishai Hadas MLX5_IB_OBJECT_UAR, 3620342ee59dSYishai Hadas UVERBS_ACCESS_NEW, 3621342ee59dSYishai Hadas UA_MANDATORY), 3622342ee59dSYishai Hadas UVERBS_ATTR_CONST_IN(MLX5_IB_ATTR_UAR_OBJ_ALLOC_TYPE, 3623342ee59dSYishai Hadas enum mlx5_ib_uapi_uar_alloc_type, 3624342ee59dSYishai Hadas UA_MANDATORY), 3625342ee59dSYishai Hadas UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_UAR_OBJ_ALLOC_PAGE_ID, 3626342ee59dSYishai Hadas UVERBS_ATTR_TYPE(u32), 3627342ee59dSYishai Hadas UA_MANDATORY), 3628342ee59dSYishai Hadas UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_UAR_OBJ_ALLOC_MMAP_LENGTH, 3629342ee59dSYishai Hadas UVERBS_ATTR_TYPE(u32), 3630342ee59dSYishai Hadas UA_MANDATORY), 3631342ee59dSYishai Hadas UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_UAR_OBJ_ALLOC_MMAP_OFFSET, 3632342ee59dSYishai Hadas UVERBS_ATTR_TYPE(u64), 3633342ee59dSYishai Hadas UA_MANDATORY)); 3634342ee59dSYishai Hadas 3635342ee59dSYishai Hadas DECLARE_UVERBS_NAMED_METHOD_DESTROY( 3636342ee59dSYishai Hadas MLX5_IB_METHOD_UAR_OBJ_DESTROY, 3637342ee59dSYishai Hadas UVERBS_ATTR_IDR(MLX5_IB_ATTR_UAR_OBJ_DESTROY_HANDLE, 3638342ee59dSYishai Hadas MLX5_IB_OBJECT_UAR, 3639342ee59dSYishai Hadas UVERBS_ACCESS_DESTROY, 3640342ee59dSYishai Hadas UA_MANDATORY)); 3641342ee59dSYishai Hadas 3642342ee59dSYishai Hadas DECLARE_UVERBS_NAMED_OBJECT(MLX5_IB_OBJECT_UAR, 3643342ee59dSYishai Hadas UVERBS_TYPE_ALLOC_IDR(mmap_obj_cleanup), 3644342ee59dSYishai Hadas &UVERBS_METHOD(MLX5_IB_METHOD_UAR_OBJ_ALLOC), 3645342ee59dSYishai Hadas &UVERBS_METHOD(MLX5_IB_METHOD_UAR_OBJ_DESTROY)); 3646342ee59dSYishai Hadas 36479a119cd5SJason Gunthorpe ADD_UVERBS_ATTRIBUTES_SIMPLE( 36480fb556b2SYishai Hadas mlx5_ib_query_context, 36490fb556b2SYishai Hadas UVERBS_OBJECT_DEVICE, 36500fb556b2SYishai Hadas UVERBS_METHOD_QUERY_CONTEXT, 36510fb556b2SYishai Hadas UVERBS_ATTR_PTR_OUT( 36520fb556b2SYishai Hadas MLX5_IB_ATTR_QUERY_CONTEXT_RESP_UCTX, 36530fb556b2SYishai Hadas UVERBS_ATTR_STRUCT(struct mlx5_ib_alloc_ucontext_resp, 36540fb556b2SYishai Hadas dump_fill_mkey), 36550fb556b2SYishai Hadas UA_MANDATORY)); 36560fb556b2SYishai Hadas 36570cbf432dSJason Gunthorpe static const struct uapi_definition mlx5_ib_defs[] = { 365836e235c8SJason Gunthorpe UAPI_DEF_CHAIN(mlx5_ib_devx_defs), 36590cbf432dSJason Gunthorpe UAPI_DEF_CHAIN(mlx5_ib_flow_defs), 366030f2fe40SYishai Hadas UAPI_DEF_CHAIN(mlx5_ib_qos_defs), 366105f71ef9SYishai Hadas UAPI_DEF_CHAIN(mlx5_ib_std_types_defs), 3662831df883SMaor Gottlieb UAPI_DEF_CHAIN(mlx5_ib_dm_defs), 36638c84660bSMatan Barak 36640fb556b2SYishai Hadas UAPI_DEF_CHAIN_OBJ_TREE(UVERBS_OBJECT_DEVICE, &mlx5_ib_query_context), 36657be76befSYishai Hadas UAPI_DEF_CHAIN_OBJ_TREE_NAMED(MLX5_IB_OBJECT_VAR, 36667be76befSYishai Hadas UAPI_DEF_IS_OBJ_SUPPORTED(var_is_supported)), 3667342ee59dSYishai Hadas UAPI_DEF_CHAIN_OBJ_TREE_NAMED(MLX5_IB_OBJECT_UAR), 36680cbf432dSJason Gunthorpe {} 36690cbf432dSJason Gunthorpe }; 36708c84660bSMatan Barak 3671fb652d32SMark Bloch static void mlx5_ib_stage_init_cleanup(struct mlx5_ib_dev *dev) 3672e126ba97SEli Cohen { 367332f69e4bSDaniel Jurgens mlx5_ib_cleanup_multiport_master(dev); 3674806b101bSJason Gunthorpe WARN_ON(!xa_empty(&dev->odp_mkeys)); 3675ab40530aSParav Pandit mutex_destroy(&dev->cap_mask_mutex); 367650211ec9SJason Gunthorpe WARN_ON(!xa_empty(&dev->sig_mrs)); 36774056b12eSAriel Levkovich WARN_ON(!bitmap_empty(dev->dm.memic_alloc_pages, MLX5_MAX_MEMIC_PAGES)); 3678758ce14aSPatrisious Haddad mlx5r_macsec_dealloc_gids(dev); 367916c1975fSMark Bloch } 368016c1975fSMark Bloch 3681fb652d32SMark Bloch static int mlx5_ib_stage_init_init(struct mlx5_ib_dev *dev) 368216c1975fSMark Bloch { 368316c1975fSMark Bloch struct mlx5_core_dev *mdev = dev->mdev; 3684758ce14aSPatrisious Haddad int err, i; 3685e126ba97SEli Cohen 368613179652SParav Pandit dev->ib_dev.node_type = RDMA_NODE_IB_CA; 368713179652SParav Pandit dev->ib_dev.local_dma_lkey = 0 /* not supported for now */; 368813179652SParav Pandit dev->ib_dev.phys_port_cnt = dev->num_ports; 368913179652SParav Pandit dev->ib_dev.dev.parent = mdev->device; 369013179652SParav Pandit dev->ib_dev.lag_flags = RDMA_LAG_FLAGS_HASH_ALL_SLAVES; 369113179652SParav Pandit 369232f69e4bSDaniel Jurgens for (i = 0; i < dev->num_ports; i++) { 369332f69e4bSDaniel Jurgens spin_lock_init(&dev->port[i].mp.mpi_lock); 369495579e78SMark Bloch rwlock_init(&dev->port[i].roce.netdev_lock); 3695d3b5cc1cSMark Bloch dev->port[i].roce.dev = dev; 3696d3b5cc1cSMark Bloch dev->port[i].roce.native_port_num = i + 1; 3697d3b5cc1cSMark Bloch dev->port[i].roce.last_port_state = IB_PORT_DOWN; 369832f69e4bSDaniel Jurgens } 369932f69e4bSDaniel Jurgens 3700594cac11SOr Har-Toov err = mlx5r_cmd_query_special_mkeys(dev); 3701594cac11SOr Har-Toov if (err) 3702594cac11SOr Har-Toov return err; 3703594cac11SOr Har-Toov 370458dbd642SPatrisious Haddad err = mlx5r_macsec_init_gids_and_devlist(dev); 3705e126ba97SEli Cohen if (err) 3706da796ccbSMark Bloch return err; 3707e126ba97SEli Cohen 3708758ce14aSPatrisious Haddad err = mlx5_ib_init_multiport_master(dev); 3709758ce14aSPatrisious Haddad if (err) 3710758ce14aSPatrisious Haddad goto err; 3711758ce14aSPatrisious Haddad 3712a989ea01SMark Bloch err = set_has_smi_cap(dev); 3713a989ea01SMark Bloch if (err) 37142cb091f6SParav Pandit goto err_mp; 3715e126ba97SEli Cohen 37162019d70eSParav Pandit err = mlx5_query_max_pkeys(&dev->ib_dev, &dev->pkey_table_len); 37172019d70eSParav Pandit if (err) 37182019d70eSParav Pandit goto err_mp; 37192019d70eSParav Pandit 37201b5daf11SMajd Dibbiny if (mlx5_use_mad_ifc(dev)) 3721e126ba97SEli Cohen get_ext_port_caps(dev); 3722e126ba97SEli Cohen 3723674dd4e2SMaher Sanalla dev->ib_dev.num_comp_vectors = mlx5_comp_vectors_max(mdev); 3724e126ba97SEli Cohen 37253cc297dbSMark Bloch mutex_init(&dev->cap_mask_mutex); 37263cc297dbSMark Bloch INIT_LIST_HEAD(&dev->qp_list); 37273cc297dbSMark Bloch spin_lock_init(&dev->reset_flow_resource_lock); 3728806b101bSJason Gunthorpe xa_init(&dev->odp_mkeys); 372950211ec9SJason Gunthorpe xa_init(&dev->sig_mrs); 3730f743ff3bSSaeed Mahameed atomic_set(&dev->mkey_var, 0); 37313cc297dbSMark Bloch 37323b113a1eSAriel Levkovich spin_lock_init(&dev->dm.lock); 37333b113a1eSAriel Levkovich dev->dm.dev = mdev; 373416c1975fSMark Bloch return 0; 373532f69e4bSDaniel Jurgens err_mp: 373632f69e4bSDaniel Jurgens mlx5_ib_cleanup_multiport_master(dev); 37373b733016SYishai Hadas err: 37383b733016SYishai Hadas mlx5r_macsec_dealloc_gids(dev); 3739d286ac1dSParav Pandit return err; 374016c1975fSMark Bloch } 374116c1975fSMark Bloch 3742ac47bf5eSLeon Romanovsky static int mlx5_ib_enable_driver(struct ib_device *dev) 37439a4ca38dSMark Bloch { 3744ac47bf5eSLeon Romanovsky struct mlx5_ib_dev *mdev = to_mdev(dev); 3745ac47bf5eSLeon Romanovsky int ret; 37469a4ca38dSMark Bloch 3747ac47bf5eSLeon Romanovsky ret = mlx5_ib_test_wc(mdev); 3748ac47bf5eSLeon Romanovsky mlx5_ib_dbg(mdev, "Write-Combining %s", 3749ac47bf5eSLeon Romanovsky mdev->wc_support ? "supported" : "not supported"); 37509a4ca38dSMark Bloch 3751ac47bf5eSLeon Romanovsky return ret; 37529a4ca38dSMark Bloch } 37539a4ca38dSMark Bloch 375496458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_ops = { 37557a154142SJason Gunthorpe .owner = THIS_MODULE, 3756b9560a41SJason Gunthorpe .driver_id = RDMA_DRIVER_MLX5, 375772c6ec18SJason Gunthorpe .uverbs_abi_ver = MLX5_IB_UVERBS_ABI_VERSION, 3758b9560a41SJason Gunthorpe 375996458233SKamal Heib .add_gid = mlx5_ib_add_gid, 376096458233SKamal Heib .alloc_mr = mlx5_ib_alloc_mr, 37616c984472SMax Gurtovoy .alloc_mr_integrity = mlx5_ib_alloc_mr_integrity, 376296458233SKamal Heib .alloc_pd = mlx5_ib_alloc_pd, 376396458233SKamal Heib .alloc_ucontext = mlx5_ib_alloc_ucontext, 376496458233SKamal Heib .attach_mcast = mlx5_ib_mcg_attach, 376596458233SKamal Heib .check_mr_status = mlx5_ib_check_mr_status, 376696458233SKamal Heib .create_ah = mlx5_ib_create_ah, 376796458233SKamal Heib .create_cq = mlx5_ib_create_cq, 376896458233SKamal Heib .create_qp = mlx5_ib_create_qp, 376996458233SKamal Heib .create_srq = mlx5_ib_create_srq, 3770676a80adSJason Gunthorpe .create_user_ah = mlx5_ib_create_ah, 377196458233SKamal Heib .dealloc_pd = mlx5_ib_dealloc_pd, 377296458233SKamal Heib .dealloc_ucontext = mlx5_ib_dealloc_ucontext, 377396458233SKamal Heib .del_gid = mlx5_ib_del_gid, 377496458233SKamal Heib .dereg_mr = mlx5_ib_dereg_mr, 377596458233SKamal Heib .destroy_ah = mlx5_ib_destroy_ah, 377696458233SKamal Heib .destroy_cq = mlx5_ib_destroy_cq, 377796458233SKamal Heib .destroy_qp = mlx5_ib_destroy_qp, 377896458233SKamal Heib .destroy_srq = mlx5_ib_destroy_srq, 377996458233SKamal Heib .detach_mcast = mlx5_ib_mcg_detach, 378096458233SKamal Heib .disassociate_ucontext = mlx5_ib_disassociate_ucontext, 378196458233SKamal Heib .drain_rq = mlx5_ib_drain_rq, 378296458233SKamal Heib .drain_sq = mlx5_ib_drain_sq, 3783915e4af5SJason Gunthorpe .device_group = &mlx5_attr_group, 378411f552e2SMichael Guralnik .enable_driver = mlx5_ib_enable_driver, 378596458233SKamal Heib .get_dev_fw_str = get_dev_fw_str, 378696458233SKamal Heib .get_dma_mr = mlx5_ib_get_dma_mr, 378796458233SKamal Heib .get_link_layer = mlx5_ib_port_link_layer, 378896458233SKamal Heib .map_mr_sg = mlx5_ib_map_mr_sg, 37896c984472SMax Gurtovoy .map_mr_sg_pi = mlx5_ib_map_mr_sg_pi, 379096458233SKamal Heib .mmap = mlx5_ib_mmap, 3791dc2316ebSYishai Hadas .mmap_free = mlx5_ib_mmap_free, 379296458233SKamal Heib .modify_cq = mlx5_ib_modify_cq, 379396458233SKamal Heib .modify_device = mlx5_ib_modify_device, 379496458233SKamal Heib .modify_port = mlx5_ib_modify_port, 379596458233SKamal Heib .modify_qp = mlx5_ib_modify_qp, 379696458233SKamal Heib .modify_srq = mlx5_ib_modify_srq, 379796458233SKamal Heib .poll_cq = mlx5_ib_poll_cq, 3798029e88fdSLeon Romanovsky .post_recv = mlx5_ib_post_recv_nodrain, 3799029e88fdSLeon Romanovsky .post_send = mlx5_ib_post_send_nodrain, 380096458233SKamal Heib .post_srq_recv = mlx5_ib_post_srq_recv, 380196458233SKamal Heib .process_mad = mlx5_ib_process_mad, 380296458233SKamal Heib .query_ah = mlx5_ib_query_ah, 380396458233SKamal Heib .query_device = mlx5_ib_query_device, 380496458233SKamal Heib .query_gid = mlx5_ib_query_gid, 380596458233SKamal Heib .query_pkey = mlx5_ib_query_pkey, 380696458233SKamal Heib .query_qp = mlx5_ib_query_qp, 380796458233SKamal Heib .query_srq = mlx5_ib_query_srq, 38080fb556b2SYishai Hadas .query_ucontext = mlx5_ib_query_ucontext, 380996458233SKamal Heib .reg_user_mr = mlx5_ib_reg_user_mr, 381090da7dc8SJianxin Xiong .reg_user_mr_dmabuf = mlx5_ib_reg_user_mr_dmabuf, 381196458233SKamal Heib .req_notify_cq = mlx5_ib_arm_cq, 381296458233SKamal Heib .rereg_user_mr = mlx5_ib_rereg_user_mr, 381396458233SKamal Heib .resize_cq = mlx5_ib_resize_cq, 3814d3456914SLeon Romanovsky 3815d3456914SLeon Romanovsky INIT_RDMA_OBJ_SIZE(ib_ah, mlx5_ib_ah, ibah), 38163b023e1bSLeon Romanovsky INIT_RDMA_OBJ_SIZE(ib_counters, mlx5_ib_mcounters, ibcntrs), 3817e39afe3dSLeon Romanovsky INIT_RDMA_OBJ_SIZE(ib_cq, mlx5_ib_cq, ibcq), 381821a428a0SLeon Romanovsky INIT_RDMA_OBJ_SIZE(ib_pd, mlx5_ib_pd, ibpd), 3819514aee66SLeon Romanovsky INIT_RDMA_OBJ_SIZE(ib_qp, mlx5_ib_qp, ibqp), 382068e326deSLeon Romanovsky INIT_RDMA_OBJ_SIZE(ib_srq, mlx5_ib_srq, ibsrq), 3821a2a074efSLeon Romanovsky INIT_RDMA_OBJ_SIZE(ib_ucontext, mlx5_ib_ucontext, ibucontext), 382296458233SKamal Heib }; 382396458233SKamal Heib 382496458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_ipoib_enhanced_ops = { 382596458233SKamal Heib .rdma_netdev_get_params = mlx5_ib_rn_get_params, 382696458233SKamal Heib }; 382796458233SKamal Heib 382896458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_sriov_ops = { 382996458233SKamal Heib .get_vf_config = mlx5_ib_get_vf_config, 38309c0015efSDanit Goldberg .get_vf_guid = mlx5_ib_get_vf_guid, 383196458233SKamal Heib .get_vf_stats = mlx5_ib_get_vf_stats, 383296458233SKamal Heib .set_vf_guid = mlx5_ib_set_vf_guid, 383396458233SKamal Heib .set_vf_link_state = mlx5_ib_set_vf_link_state, 383496458233SKamal Heib }; 383596458233SKamal Heib 383696458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_mw_ops = { 383796458233SKamal Heib .alloc_mw = mlx5_ib_alloc_mw, 383896458233SKamal Heib .dealloc_mw = mlx5_ib_dealloc_mw, 3839d18bb3e1SLeon Romanovsky 3840d18bb3e1SLeon Romanovsky INIT_RDMA_OBJ_SIZE(ib_mw, mlx5_ib_mw, ibmw), 384196458233SKamal Heib }; 384296458233SKamal Heib 384396458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_xrc_ops = { 384496458233SKamal Heib .alloc_xrcd = mlx5_ib_alloc_xrcd, 384596458233SKamal Heib .dealloc_xrcd = mlx5_ib_dealloc_xrcd, 384628ad5f65SLeon Romanovsky 384728ad5f65SLeon Romanovsky INIT_RDMA_OBJ_SIZE(ib_xrcd, mlx5_ib_xrcd, ibxrcd), 384896458233SKamal Heib }; 384996458233SKamal Heib 3850f164be8cSYishai Hadas static int mlx5_ib_init_var_table(struct mlx5_ib_dev *dev) 3851f164be8cSYishai Hadas { 3852f164be8cSYishai Hadas struct mlx5_core_dev *mdev = dev->mdev; 3853f164be8cSYishai Hadas struct mlx5_var_table *var_table = &dev->var_table; 3854f164be8cSYishai Hadas u8 log_doorbell_bar_size; 3855f164be8cSYishai Hadas u8 log_doorbell_stride; 3856f164be8cSYishai Hadas u64 bar_size; 3857f164be8cSYishai Hadas 3858f164be8cSYishai Hadas log_doorbell_bar_size = MLX5_CAP_DEV_VDPA_EMULATION(mdev, 3859f164be8cSYishai Hadas log_doorbell_bar_size); 3860f164be8cSYishai Hadas log_doorbell_stride = MLX5_CAP_DEV_VDPA_EMULATION(mdev, 3861f164be8cSYishai Hadas log_doorbell_stride); 3862f164be8cSYishai Hadas var_table->hw_start_addr = dev->mdev->bar_addr + 3863f164be8cSYishai Hadas MLX5_CAP64_DEV_VDPA_EMULATION(mdev, 3864f164be8cSYishai Hadas doorbell_bar_offset); 3865f164be8cSYishai Hadas bar_size = (1ULL << log_doorbell_bar_size) * 4096; 3866f164be8cSYishai Hadas var_table->stride_size = 1ULL << log_doorbell_stride; 386791b74bf5SAlexander Lobakin var_table->num_var_hw_entries = div_u64(bar_size, 386891b74bf5SAlexander Lobakin var_table->stride_size); 3869f164be8cSYishai Hadas mutex_init(&var_table->bitmap_lock); 3870f164be8cSYishai Hadas var_table->bitmap = bitmap_zalloc(var_table->num_var_hw_entries, 3871f164be8cSYishai Hadas GFP_KERNEL); 3872f164be8cSYishai Hadas return (var_table->bitmap) ? 0 : -ENOMEM; 3873f164be8cSYishai Hadas } 3874f164be8cSYishai Hadas 3875f164be8cSYishai Hadas static void mlx5_ib_stage_caps_cleanup(struct mlx5_ib_dev *dev) 3876f164be8cSYishai Hadas { 3877f164be8cSYishai Hadas bitmap_free(dev->var_table.bitmap); 3878f164be8cSYishai Hadas } 3879f164be8cSYishai Hadas 3880fb652d32SMark Bloch static int mlx5_ib_stage_caps_init(struct mlx5_ib_dev *dev) 388116c1975fSMark Bloch { 388216c1975fSMark Bloch struct mlx5_core_dev *mdev = dev->mdev; 388316c1975fSMark Bloch int err; 388416c1975fSMark Bloch 3885f6a8a19bSDenis Drozdov if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads) && 3886f6a8a19bSDenis Drozdov IS_ENABLED(CONFIG_MLX5_CORE_IPOIB)) 388796458233SKamal Heib ib_set_device_ops(&dev->ib_dev, 388896458233SKamal Heib &mlx5_ib_dev_ipoib_enhanced_ops); 38898e959601SNiranjana Vishwanathapura 389096458233SKamal Heib if (mlx5_core_is_pf(mdev)) 389196458233SKamal Heib ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_sriov_ops); 38927c2344c3SMaor Gottlieb 38936e8484c5SMax Gurtovoy dev->umr_fence = mlx5_get_umr_fence(MLX5_CAP_GEN(mdev, umr_fence)); 38946e8484c5SMax Gurtovoy 389544ce37bcSJason Gunthorpe if (MLX5_CAP_GEN(mdev, imaicl)) 389696458233SKamal Heib ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_mw_ops); 3897d2370e0aSMatan Barak 389844ce37bcSJason Gunthorpe if (MLX5_CAP_GEN(mdev, xrc)) 389996458233SKamal Heib ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_xrc_ops); 3900e126ba97SEli Cohen 390125c13324SAriel Levkovich if (MLX5_CAP_DEV_MEM(mdev, memic) || 390225c13324SAriel Levkovich MLX5_CAP_GEN_64(dev->mdev, general_obj_types) & 390325c13324SAriel Levkovich MLX5_GENERAL_OBJ_TYPES_CAP_SW_ICM) 390496458233SKamal Heib ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_dm_ops); 390524da0016SAriel Levkovich 390696458233SKamal Heib ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_ops); 390781e30880SYishai Hadas 390836e235c8SJason Gunthorpe if (IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS)) 390936e235c8SJason Gunthorpe dev->ib_dev.driver_def = mlx5_ib_defs; 391081e30880SYishai Hadas 3911e126ba97SEli Cohen err = init_node_data(dev); 3912e126ba97SEli Cohen if (err) 391316c1975fSMark Bloch return err; 3914e126ba97SEli Cohen 3915c8b89924SMark Bloch if ((MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_ETH) && 3916e7996a9aSJason Gunthorpe (MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) || 3917e7996a9aSJason Gunthorpe MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc))) 3918a560f1d9SMark Bloch mutex_init(&dev->lb.mutex); 3919c8b89924SMark Bloch 3920f164be8cSYishai Hadas if (MLX5_CAP_GEN_64(dev->mdev, general_obj_types) & 3921f164be8cSYishai Hadas MLX5_GENERAL_OBJ_TYPES_CAP_VIRTIO_NET_Q) { 3922f164be8cSYishai Hadas err = mlx5_ib_init_var_table(dev); 3923f164be8cSYishai Hadas if (err) 3924f164be8cSYishai Hadas return err; 3925f164be8cSYishai Hadas } 3926f164be8cSYishai Hadas 392796e2fd73SLeon Romanovsky dev->ib_dev.use_cq_dim = true; 392896e2fd73SLeon Romanovsky 392916c1975fSMark Bloch return 0; 393016c1975fSMark Bloch } 393116c1975fSMark Bloch 393296458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_port_ops = { 393396458233SKamal Heib .get_port_immutable = mlx5_port_immutable, 393496458233SKamal Heib .query_port = mlx5_ib_query_port, 393596458233SKamal Heib }; 393696458233SKamal Heib 39378e6efa3aSMark Bloch static int mlx5_ib_stage_non_default_cb(struct mlx5_ib_dev *dev) 393816c1975fSMark Bloch { 393996458233SKamal Heib ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_port_ops); 39408e6efa3aSMark Bloch return 0; 39418e6efa3aSMark Bloch } 39428e6efa3aSMark Bloch 394396458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_port_rep_ops = { 394496458233SKamal Heib .get_port_immutable = mlx5_port_rep_immutable, 394596458233SKamal Heib .query_port = mlx5_ib_rep_query_port, 3946d6fd59e1SParav Pandit .query_pkey = mlx5_ib_rep_query_pkey, 394796458233SKamal Heib }; 394896458233SKamal Heib 3949b5a498baSMichael Guralnik static int mlx5_ib_stage_raw_eth_non_default_cb(struct mlx5_ib_dev *dev) 39508e6efa3aSMark Bloch { 395196458233SKamal Heib ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_port_rep_ops); 39528e6efa3aSMark Bloch return 0; 39538e6efa3aSMark Bloch } 39548e6efa3aSMark Bloch 395596458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_common_roce_ops = { 395696458233SKamal Heib .create_rwq_ind_table = mlx5_ib_create_rwq_ind_table, 395796458233SKamal Heib .create_wq = mlx5_ib_create_wq, 395896458233SKamal Heib .destroy_rwq_ind_table = mlx5_ib_destroy_rwq_ind_table, 395996458233SKamal Heib .destroy_wq = mlx5_ib_destroy_wq, 396096458233SKamal Heib .get_netdev = mlx5_ib_get_netdev, 396196458233SKamal Heib .modify_wq = mlx5_ib_modify_wq, 3962c0a6b5ecSLeon Romanovsky 3963c0a6b5ecSLeon Romanovsky INIT_RDMA_OBJ_SIZE(ib_rwq_ind_table, mlx5_ib_rwq_ind_table, 3964c0a6b5ecSLeon Romanovsky ib_rwq_ind_tbl), 396596458233SKamal Heib }; 396696458233SKamal Heib 39671e2b5a90SLeon Romanovsky static int mlx5_ib_roce_init(struct mlx5_ib_dev *dev) 39688e6efa3aSMark Bloch { 39691e2b5a90SLeon Romanovsky struct mlx5_core_dev *mdev = dev->mdev; 39701e2b5a90SLeon Romanovsky enum rdma_link_layer ll; 39711e2b5a90SLeon Romanovsky int port_type_cap; 39721fb7f897SMark Bloch u32 port_num = 0; 39731e2b5a90SLeon Romanovsky int err; 39747fd8aefbSDaniel Jurgens 39751e2b5a90SLeon Romanovsky port_type_cap = MLX5_CAP_GEN(mdev, port_type); 39761e2b5a90SLeon Romanovsky ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap); 39771e2b5a90SLeon Romanovsky 39781e2b5a90SLeon Romanovsky if (ll == IB_LINK_LAYER_ETHERNET) { 397996458233SKamal Heib ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_common_roce_ops); 39808e6efa3aSMark Bloch 3981e3f1ed1fSLeon Romanovsky port_num = mlx5_core_native_port_num(dev->mdev) - 1; 3982e3f1ed1fSLeon Romanovsky 398326628e2dSMark Bloch /* Register only for native ports */ 3984dca55da0SJiri Pirko mlx5_mdev_netdev_track(dev, port_num); 39858e6efa3aSMark Bloch 3986e3f1ed1fSLeon Romanovsky err = mlx5_enable_eth(dev); 39878e6efa3aSMark Bloch if (err) 39888e6efa3aSMark Bloch goto cleanup; 39898e6efa3aSMark Bloch } 39908e6efa3aSMark Bloch 399116c1975fSMark Bloch return 0; 39928e6efa3aSMark Bloch cleanup: 3993dca55da0SJiri Pirko mlx5_mdev_netdev_untrack(dev, port_num); 39948e6efa3aSMark Bloch return err; 399545bded2cSKamal Heib } 39966aec21f6SHaggai Eran 39971e2b5a90SLeon Romanovsky static void mlx5_ib_roce_cleanup(struct mlx5_ib_dev *dev) 399816c1975fSMark Bloch { 399916c1975fSMark Bloch struct mlx5_core_dev *mdev = dev->mdev; 400016c1975fSMark Bloch enum rdma_link_layer ll; 400116c1975fSMark Bloch int port_type_cap; 40021fb7f897SMark Bloch u32 port_num; 40034a2da0b8SParav Pandit 400416c1975fSMark Bloch port_type_cap = MLX5_CAP_GEN(mdev, port_type); 400516c1975fSMark Bloch ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap); 400616c1975fSMark Bloch 400716c1975fSMark Bloch if (ll == IB_LINK_LAYER_ETHERNET) { 400816c1975fSMark Bloch mlx5_disable_eth(dev); 400916c1975fSMark Bloch 40101e2b5a90SLeon Romanovsky port_num = mlx5_core_native_port_num(dev->mdev) - 1; 4011dca55da0SJiri Pirko mlx5_mdev_netdev_untrack(dev, port_num); 401216c1975fSMark Bloch } 401316c1975fSMark Bloch } 401416c1975fSMark Bloch 401516c1975fSMark Bloch static int mlx5_ib_stage_cong_debugfs_init(struct mlx5_ib_dev *dev) 401616c1975fSMark Bloch { 401773eb8f03SGreg Kroah-Hartman mlx5_ib_init_cong_debugfs(dev, 4018a9e546e7SParav Pandit mlx5_core_native_port_num(dev->mdev) - 1); 401973eb8f03SGreg Kroah-Hartman return 0; 402016c1975fSMark Bloch } 402116c1975fSMark Bloch 402216c1975fSMark Bloch static void mlx5_ib_stage_cong_debugfs_cleanup(struct mlx5_ib_dev *dev) 402316c1975fSMark Bloch { 4024a9e546e7SParav Pandit mlx5_ib_cleanup_cong_debugfs(dev, 4025a9e546e7SParav Pandit mlx5_core_native_port_num(dev->mdev) - 1); 402616c1975fSMark Bloch } 402716c1975fSMark Bloch 402816c1975fSMark Bloch static int mlx5_ib_stage_uar_init(struct mlx5_ib_dev *dev) 402916c1975fSMark Bloch { 40305fe9dec0SEli Cohen dev->mdev->priv.uar = mlx5_get_uars_page(dev->mdev); 4031444261caSLeon Romanovsky return PTR_ERR_OR_ZERO(dev->mdev->priv.uar); 403216c1975fSMark Bloch } 403316c1975fSMark Bloch 403416c1975fSMark Bloch static void mlx5_ib_stage_uar_cleanup(struct mlx5_ib_dev *dev) 403516c1975fSMark Bloch { 403616c1975fSMark Bloch mlx5_put_uars_page(dev->mdev, dev->mdev->priv.uar); 403716c1975fSMark Bloch } 403816c1975fSMark Bloch 4039fb652d32SMark Bloch static int mlx5_ib_stage_bfrag_init(struct mlx5_ib_dev *dev) 404016c1975fSMark Bloch { 404116c1975fSMark Bloch int err; 40425fe9dec0SEli Cohen 40435fe9dec0SEli Cohen err = mlx5_alloc_bfreg(dev->mdev, &dev->bfreg, false, false); 40445fe9dec0SEli Cohen if (err) 404516c1975fSMark Bloch return err; 40465fe9dec0SEli Cohen 40475fe9dec0SEli Cohen err = mlx5_alloc_bfreg(dev->mdev, &dev->fp_bfreg, false, true); 40485fe9dec0SEli Cohen if (err) 40491c3aa6bdSMark Bloch mlx5_free_bfreg(dev->mdev, &dev->bfreg); 40505fe9dec0SEli Cohen 405116c1975fSMark Bloch return err; 405216c1975fSMark Bloch } 40530837e86aSMark Bloch 4054fb652d32SMark Bloch static void mlx5_ib_stage_bfrag_cleanup(struct mlx5_ib_dev *dev) 405516c1975fSMark Bloch { 405616c1975fSMark Bloch mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg); 405716c1975fSMark Bloch mlx5_free_bfreg(dev->mdev, &dev->bfreg); 405816c1975fSMark Bloch } 4059e126ba97SEli Cohen 4060fb652d32SMark Bloch static int mlx5_ib_stage_ib_reg_init(struct mlx5_ib_dev *dev) 406116c1975fSMark Bloch { 4062e349f858SJason Gunthorpe const char *name; 4063e349f858SJason Gunthorpe 4064c446d9daSMark Bloch if (!mlx5_lag_is_active(dev->mdev)) 4065e349f858SJason Gunthorpe name = "mlx5_%d"; 4066e349f858SJason Gunthorpe else 4067e349f858SJason Gunthorpe name = "mlx5_bond_%d"; 4068e0477b34SJason Gunthorpe return ib_register_device(&dev->ib_dev, name, &dev->mdev->pdev->dev); 406916c1975fSMark Bloch } 407016c1975fSMark Bloch 4071fb652d32SMark Bloch static void mlx5_ib_stage_pre_ib_reg_umr_cleanup(struct mlx5_ib_dev *dev) 407242cea83fSMark Bloch { 407385f9e38aSLeon Romanovsky mlx5_mkey_cache_cleanup(dev); 407404876c12SAharon Landau mlx5r_umr_resource_cleanup(dev); 407542cea83fSMark Bloch } 407642cea83fSMark Bloch 4077fb652d32SMark Bloch static void mlx5_ib_stage_ib_reg_cleanup(struct mlx5_ib_dev *dev) 407816c1975fSMark Bloch { 407916c1975fSMark Bloch ib_unregister_device(&dev->ib_dev); 408016c1975fSMark Bloch } 408116c1975fSMark Bloch 4082fb652d32SMark Bloch static int mlx5_ib_stage_post_ib_reg_umr_init(struct mlx5_ib_dev *dev) 408316c1975fSMark Bloch { 40841e2b5a90SLeon Romanovsky int ret; 40851e2b5a90SLeon Romanovsky 408604876c12SAharon Landau ret = mlx5r_umr_resource_init(dev); 408704876c12SAharon Landau if (ret) 408804876c12SAharon Landau return ret; 40891e2b5a90SLeon Romanovsky 409001137808SAharon Landau ret = mlx5_mkey_cache_init(dev); 40916387f269SGeorge Kennedy if (ret) 40921e2b5a90SLeon Romanovsky mlx5_ib_warn(dev, "mr cache init failed %d\n", ret); 40931e2b5a90SLeon Romanovsky return ret; 409416c1975fSMark Bloch } 409516c1975fSMark Bloch 409616c1975fSMark Bloch static int mlx5_ib_stage_delay_drop_init(struct mlx5_ib_dev *dev) 409716c1975fSMark Bloch { 40981e2b5a90SLeon Romanovsky struct dentry *root; 409903404e8aSMaor Gottlieb 41001e2b5a90SLeon Romanovsky if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP)) 41011e2b5a90SLeon Romanovsky return 0; 41021e2b5a90SLeon Romanovsky 41031e2b5a90SLeon Romanovsky mutex_init(&dev->delay_drop.lock); 41041e2b5a90SLeon Romanovsky dev->delay_drop.dev = dev; 41051e2b5a90SLeon Romanovsky dev->delay_drop.activate = false; 41061e2b5a90SLeon Romanovsky dev->delay_drop.timeout = MLX5_MAX_DELAY_DROP_TIMEOUT_MS * 1000; 41071e2b5a90SLeon Romanovsky INIT_WORK(&dev->delay_drop.delay_drop_work, delay_drop_handler); 41081e2b5a90SLeon Romanovsky atomic_set(&dev->delay_drop.rqs_cnt, 0); 41091e2b5a90SLeon Romanovsky atomic_set(&dev->delay_drop.events_cnt, 0); 41101e2b5a90SLeon Romanovsky 41111e2b5a90SLeon Romanovsky if (!mlx5_debugfs_root) 41121e2b5a90SLeon Romanovsky return 0; 41131e2b5a90SLeon Romanovsky 411466771a1cSMoshe Shemesh root = debugfs_create_dir("delay_drop", mlx5_debugfs_get_dev_root(dev->mdev)); 41151e2b5a90SLeon Romanovsky dev->delay_drop.dir_debugfs = root; 41161e2b5a90SLeon Romanovsky 41171e2b5a90SLeon Romanovsky debugfs_create_atomic_t("num_timeout_events", 0400, root, 41181e2b5a90SLeon Romanovsky &dev->delay_drop.events_cnt); 41191e2b5a90SLeon Romanovsky debugfs_create_atomic_t("num_rqs", 0400, root, 41201e2b5a90SLeon Romanovsky &dev->delay_drop.rqs_cnt); 41211e2b5a90SLeon Romanovsky debugfs_create_file("timeout", 0600, root, &dev->delay_drop, 41221e2b5a90SLeon Romanovsky &fops_delay_drop_timeout); 412316c1975fSMark Bloch return 0; 412416c1975fSMark Bloch } 412516c1975fSMark Bloch 412616c1975fSMark Bloch static void mlx5_ib_stage_delay_drop_cleanup(struct mlx5_ib_dev *dev) 412716c1975fSMark Bloch { 41281e2b5a90SLeon Romanovsky if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP)) 41291e2b5a90SLeon Romanovsky return; 41301e2b5a90SLeon Romanovsky 41311e2b5a90SLeon Romanovsky cancel_work_sync(&dev->delay_drop.delay_drop_work); 41321e2b5a90SLeon Romanovsky if (!dev->delay_drop.dir_debugfs) 41331e2b5a90SLeon Romanovsky return; 41341e2b5a90SLeon Romanovsky 41351e2b5a90SLeon Romanovsky debugfs_remove_recursive(dev->delay_drop.dir_debugfs); 41361e2b5a90SLeon Romanovsky dev->delay_drop.dir_debugfs = NULL; 413716c1975fSMark Bloch } 413816c1975fSMark Bloch 4139df097a27SSaeed Mahameed static int mlx5_ib_stage_dev_notifier_init(struct mlx5_ib_dev *dev) 4140df097a27SSaeed Mahameed { 4141df097a27SSaeed Mahameed dev->mdev_events.notifier_call = mlx5_ib_event; 4142df097a27SSaeed Mahameed mlx5_notifier_register(dev->mdev, &dev->mdev_events); 414358dbd642SPatrisious Haddad 414458dbd642SPatrisious Haddad mlx5r_macsec_event_register(dev); 414558dbd642SPatrisious Haddad 4146df097a27SSaeed Mahameed return 0; 4147df097a27SSaeed Mahameed } 4148df097a27SSaeed Mahameed 4149df097a27SSaeed Mahameed static void mlx5_ib_stage_dev_notifier_cleanup(struct mlx5_ib_dev *dev) 4150df097a27SSaeed Mahameed { 415158dbd642SPatrisious Haddad mlx5r_macsec_event_unregister(dev); 4152df097a27SSaeed Mahameed mlx5_notifier_unregister(dev->mdev, &dev->mdev_events); 4153df097a27SSaeed Mahameed } 4154df097a27SSaeed Mahameed 4155b5ca15adSMark Bloch void __mlx5_ib_remove(struct mlx5_ib_dev *dev, 415616c1975fSMark Bloch const struct mlx5_ib_profile *profile, 415716c1975fSMark Bloch int stage) 415816c1975fSMark Bloch { 41594cca96a8SParav Pandit dev->ib_active = false; 41604cca96a8SParav Pandit 416116c1975fSMark Bloch /* Number of stages to cleanup */ 416216c1975fSMark Bloch while (stage) { 416316c1975fSMark Bloch stage--; 416416c1975fSMark Bloch if (profile->stage[stage].cleanup) 416516c1975fSMark Bloch profile->stage[stage].cleanup(dev); 416616c1975fSMark Bloch } 41674a6dc855SMark Bloch 4168da796ccbSMark Bloch kfree(dev->port); 41694a6dc855SMark Bloch ib_dealloc_device(&dev->ib_dev); 417016c1975fSMark Bloch } 417116c1975fSMark Bloch 417293f82444SLeon Romanovsky int __mlx5_ib_add(struct mlx5_ib_dev *dev, 417316c1975fSMark Bloch const struct mlx5_ib_profile *profile) 417416c1975fSMark Bloch { 417516c1975fSMark Bloch int err; 417616c1975fSMark Bloch int i; 417716c1975fSMark Bloch 41788d93efb8SMark Bloch dev->profile = profile; 41798d93efb8SMark Bloch 418016c1975fSMark Bloch for (i = 0; i < MLX5_IB_STAGE_MAX; i++) { 418116c1975fSMark Bloch if (profile->stage[i].init) { 418216c1975fSMark Bloch err = profile->stage[i].init(dev); 418316c1975fSMark Bloch if (err) 418416c1975fSMark Bloch goto err_out; 418516c1975fSMark Bloch } 418616c1975fSMark Bloch } 418716c1975fSMark Bloch 4188e126ba97SEli Cohen dev->ib_active = true; 418993f82444SLeon Romanovsky return 0; 4190e126ba97SEli Cohen 419116c1975fSMark Bloch err_out: 419293f82444SLeon Romanovsky /* Clean up stages which were initialized */ 419393f82444SLeon Romanovsky while (i) { 419493f82444SLeon Romanovsky i--; 419593f82444SLeon Romanovsky if (profile->stage[i].cleanup) 419693f82444SLeon Romanovsky profile->stage[i].cleanup(dev); 419793f82444SLeon Romanovsky } 419893f82444SLeon Romanovsky return -ENOMEM; 4199e126ba97SEli Cohen } 4200e126ba97SEli Cohen 420116c1975fSMark Bloch static const struct mlx5_ib_profile pf_profile = { 420216c1975fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_INIT, 420316c1975fSMark Bloch mlx5_ib_stage_init_init, 420416c1975fSMark Bloch mlx5_ib_stage_init_cleanup), 4205f7c4ffdaSLeon Romanovsky STAGE_CREATE(MLX5_IB_STAGE_FS, 4206f7c4ffdaSLeon Romanovsky mlx5_ib_fs_init, 4207f7c4ffdaSLeon Romanovsky mlx5_ib_fs_cleanup), 420816c1975fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_CAPS, 420916c1975fSMark Bloch mlx5_ib_stage_caps_init, 4210f164be8cSYishai Hadas mlx5_ib_stage_caps_cleanup), 42118e6efa3aSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB, 42128e6efa3aSMark Bloch mlx5_ib_stage_non_default_cb, 42138e6efa3aSMark Bloch NULL), 421416c1975fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_ROCE, 42151e2b5a90SLeon Romanovsky mlx5_ib_roce_init, 42161e2b5a90SLeon Romanovsky mlx5_ib_roce_cleanup), 4217333fbaa0SLeon Romanovsky STAGE_CREATE(MLX5_IB_STAGE_QP, 4218333fbaa0SLeon Romanovsky mlx5_init_qp_table, 4219333fbaa0SLeon Romanovsky mlx5_cleanup_qp_table), 4220f3da6577SLeon Romanovsky STAGE_CREATE(MLX5_IB_STAGE_SRQ, 4221f3da6577SLeon Romanovsky mlx5_init_srq_table, 4222f3da6577SLeon Romanovsky mlx5_cleanup_srq_table), 422316c1975fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES, 42241e2b5a90SLeon Romanovsky mlx5_ib_dev_res_init, 42251e2b5a90SLeon Romanovsky mlx5_ib_dev_res_cleanup), 4226df097a27SSaeed Mahameed STAGE_CREATE(MLX5_IB_STAGE_DEVICE_NOTIFIER, 4227df097a27SSaeed Mahameed mlx5_ib_stage_dev_notifier_init, 4228df097a27SSaeed Mahameed mlx5_ib_stage_dev_notifier_cleanup), 422916c1975fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_ODP, 42301e2b5a90SLeon Romanovsky mlx5_ib_odp_init_one, 42311e2b5a90SLeon Romanovsky mlx5_ib_odp_cleanup_one), 423216c1975fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_COUNTERS, 423364825827SLeon Romanovsky mlx5_ib_counters_init, 423464825827SLeon Romanovsky mlx5_ib_counters_cleanup), 423516c1975fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_CONG_DEBUGFS, 423616c1975fSMark Bloch mlx5_ib_stage_cong_debugfs_init, 423716c1975fSMark Bloch mlx5_ib_stage_cong_debugfs_cleanup), 423816c1975fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_UAR, 423916c1975fSMark Bloch mlx5_ib_stage_uar_init, 424016c1975fSMark Bloch mlx5_ib_stage_uar_cleanup), 424116c1975fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_BFREG, 424216c1975fSMark Bloch mlx5_ib_stage_bfrag_init, 424316c1975fSMark Bloch mlx5_ib_stage_bfrag_cleanup), 424442cea83fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR, 424542cea83fSMark Bloch NULL, 424642cea83fSMark Bloch mlx5_ib_stage_pre_ib_reg_umr_cleanup), 424781773ce5SLeon Romanovsky STAGE_CREATE(MLX5_IB_STAGE_WHITELIST_UID, 4248d8b7515eSLeon Romanovsky mlx5_ib_devx_init, 4249d8b7515eSLeon Romanovsky mlx5_ib_devx_cleanup), 425016c1975fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_IB_REG, 425116c1975fSMark Bloch mlx5_ib_stage_ib_reg_init, 425216c1975fSMark Bloch mlx5_ib_stage_ib_reg_cleanup), 425342cea83fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR, 425442cea83fSMark Bloch mlx5_ib_stage_post_ib_reg_umr_init, 425542cea83fSMark Bloch NULL), 425616c1975fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_DELAY_DROP, 425716c1975fSMark Bloch mlx5_ib_stage_delay_drop_init, 425816c1975fSMark Bloch mlx5_ib_stage_delay_drop_cleanup), 4259b572ebe6SLeon Romanovsky STAGE_CREATE(MLX5_IB_STAGE_RESTRACK, 4260b572ebe6SLeon Romanovsky mlx5_ib_restrack_init, 4261b572ebe6SLeon Romanovsky NULL), 426216c1975fSMark Bloch }; 426316c1975fSMark Bloch 4264b5a498baSMichael Guralnik const struct mlx5_ib_profile raw_eth_profile = { 4265b5ca15adSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_INIT, 4266b5ca15adSMark Bloch mlx5_ib_stage_init_init, 4267b5ca15adSMark Bloch mlx5_ib_stage_init_cleanup), 4268f7c4ffdaSLeon Romanovsky STAGE_CREATE(MLX5_IB_STAGE_FS, 4269f7c4ffdaSLeon Romanovsky mlx5_ib_fs_init, 4270f7c4ffdaSLeon Romanovsky mlx5_ib_fs_cleanup), 4271b5ca15adSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_CAPS, 4272b5ca15adSMark Bloch mlx5_ib_stage_caps_init, 4273f164be8cSYishai Hadas mlx5_ib_stage_caps_cleanup), 4274b5ca15adSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB, 4275b5a498baSMichael Guralnik mlx5_ib_stage_raw_eth_non_default_cb, 4276b5ca15adSMark Bloch NULL), 4277b5ca15adSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_ROCE, 42781e2b5a90SLeon Romanovsky mlx5_ib_roce_init, 42791e2b5a90SLeon Romanovsky mlx5_ib_roce_cleanup), 4280333fbaa0SLeon Romanovsky STAGE_CREATE(MLX5_IB_STAGE_QP, 4281333fbaa0SLeon Romanovsky mlx5_init_qp_table, 4282333fbaa0SLeon Romanovsky mlx5_cleanup_qp_table), 4283f3da6577SLeon Romanovsky STAGE_CREATE(MLX5_IB_STAGE_SRQ, 4284f3da6577SLeon Romanovsky mlx5_init_srq_table, 4285f3da6577SLeon Romanovsky mlx5_cleanup_srq_table), 4286b5ca15adSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES, 42871e2b5a90SLeon Romanovsky mlx5_ib_dev_res_init, 42881e2b5a90SLeon Romanovsky mlx5_ib_dev_res_cleanup), 4289df097a27SSaeed Mahameed STAGE_CREATE(MLX5_IB_STAGE_DEVICE_NOTIFIER, 4290df097a27SSaeed Mahameed mlx5_ib_stage_dev_notifier_init, 4291df097a27SSaeed Mahameed mlx5_ib_stage_dev_notifier_cleanup), 4292b5ca15adSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_COUNTERS, 429364825827SLeon Romanovsky mlx5_ib_counters_init, 429464825827SLeon Romanovsky mlx5_ib_counters_cleanup), 429579db784eSParav Pandit STAGE_CREATE(MLX5_IB_STAGE_CONG_DEBUGFS, 429679db784eSParav Pandit mlx5_ib_stage_cong_debugfs_init, 429779db784eSParav Pandit mlx5_ib_stage_cong_debugfs_cleanup), 4298b5ca15adSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_UAR, 4299b5ca15adSMark Bloch mlx5_ib_stage_uar_init, 4300b5ca15adSMark Bloch mlx5_ib_stage_uar_cleanup), 4301b5ca15adSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_BFREG, 4302b5ca15adSMark Bloch mlx5_ib_stage_bfrag_init, 4303b5ca15adSMark Bloch mlx5_ib_stage_bfrag_cleanup), 430403fe2debSDavid S. Miller STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR, 430503fe2debSDavid S. Miller NULL, 430603fe2debSDavid S. Miller mlx5_ib_stage_pre_ib_reg_umr_cleanup), 43077f575103SMark Bloch STAGE_CREATE(MLX5_IB_STAGE_WHITELIST_UID, 4308d8b7515eSLeon Romanovsky mlx5_ib_devx_init, 4309d8b7515eSLeon Romanovsky mlx5_ib_devx_cleanup), 4310b5ca15adSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_IB_REG, 4311b5ca15adSMark Bloch mlx5_ib_stage_ib_reg_init, 4312b5ca15adSMark Bloch mlx5_ib_stage_ib_reg_cleanup), 431303fe2debSDavid S. Miller STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR, 431403fe2debSDavid S. Miller mlx5_ib_stage_post_ib_reg_umr_init, 431503fe2debSDavid S. Miller NULL), 4316ee4d269eSMaher Sanalla STAGE_CREATE(MLX5_IB_STAGE_DELAY_DROP, 4317ee4d269eSMaher Sanalla mlx5_ib_stage_delay_drop_init, 4318ee4d269eSMaher Sanalla mlx5_ib_stage_delay_drop_cleanup), 4319b572ebe6SLeon Romanovsky STAGE_CREATE(MLX5_IB_STAGE_RESTRACK, 4320b572ebe6SLeon Romanovsky mlx5_ib_restrack_init, 4321b572ebe6SLeon Romanovsky NULL), 4322b5ca15adSMark Bloch }; 4323b5ca15adSMark Bloch 432493f82444SLeon Romanovsky static int mlx5r_mp_probe(struct auxiliary_device *adev, 432593f82444SLeon Romanovsky const struct auxiliary_device_id *id) 432632f69e4bSDaniel Jurgens { 432793f82444SLeon Romanovsky struct mlx5_adev *idev = container_of(adev, struct mlx5_adev, adev); 432893f82444SLeon Romanovsky struct mlx5_core_dev *mdev = idev->mdev; 432932f69e4bSDaniel Jurgens struct mlx5_ib_multiport_info *mpi; 433032f69e4bSDaniel Jurgens struct mlx5_ib_dev *dev; 433132f69e4bSDaniel Jurgens bool bound = false; 433232f69e4bSDaniel Jurgens int err; 433332f69e4bSDaniel Jurgens 433432f69e4bSDaniel Jurgens mpi = kzalloc(sizeof(*mpi), GFP_KERNEL); 433532f69e4bSDaniel Jurgens if (!mpi) 433693f82444SLeon Romanovsky return -ENOMEM; 433732f69e4bSDaniel Jurgens 433832f69e4bSDaniel Jurgens mpi->mdev = mdev; 433932f69e4bSDaniel Jurgens err = mlx5_query_nic_vport_system_image_guid(mdev, 434032f69e4bSDaniel Jurgens &mpi->sys_image_guid); 434132f69e4bSDaniel Jurgens if (err) { 434232f69e4bSDaniel Jurgens kfree(mpi); 434393f82444SLeon Romanovsky return err; 434432f69e4bSDaniel Jurgens } 434532f69e4bSDaniel Jurgens 434632f69e4bSDaniel Jurgens mutex_lock(&mlx5_ib_multiport_mutex); 434732f69e4bSDaniel Jurgens list_for_each_entry(dev, &mlx5_ib_dev_list, ib_dev_list) { 434832f69e4bSDaniel Jurgens if (dev->sys_image_guid == mpi->sys_image_guid) 434932f69e4bSDaniel Jurgens bound = mlx5_ib_bind_slave_port(dev, mpi); 435032f69e4bSDaniel Jurgens 435132f69e4bSDaniel Jurgens if (bound) { 435232f69e4bSDaniel Jurgens rdma_roce_rescan_device(&dev->ib_dev); 435397f30d32SMaor Gottlieb mpi->ibdev->ib_active = true; 435432f69e4bSDaniel Jurgens break; 435532f69e4bSDaniel Jurgens } 435632f69e4bSDaniel Jurgens } 435732f69e4bSDaniel Jurgens 435832f69e4bSDaniel Jurgens if (!bound) { 435932f69e4bSDaniel Jurgens list_add_tail(&mpi->list, &mlx5_ib_unaffiliated_port_list); 4360c42260f1SVu Pham dev_dbg(mdev->device, 4361c42260f1SVu Pham "no suitable IB device found to bind to, added to unaffiliated list.\n"); 436232f69e4bSDaniel Jurgens } 436332f69e4bSDaniel Jurgens mutex_unlock(&mlx5_ib_multiport_mutex); 436432f69e4bSDaniel Jurgens 436527963d3dSDavid E. Box auxiliary_set_drvdata(adev, mpi); 436693f82444SLeon Romanovsky return 0; 436732f69e4bSDaniel Jurgens } 436832f69e4bSDaniel Jurgens 436993f82444SLeon Romanovsky static void mlx5r_mp_remove(struct auxiliary_device *adev) 437016c1975fSMark Bloch { 437193f82444SLeon Romanovsky struct mlx5_ib_multiport_info *mpi; 437293f82444SLeon Romanovsky 437327963d3dSDavid E. Box mpi = auxiliary_get_drvdata(adev); 437493f82444SLeon Romanovsky mutex_lock(&mlx5_ib_multiport_mutex); 437593f82444SLeon Romanovsky if (mpi->ibdev) 437693f82444SLeon Romanovsky mlx5_ib_unbind_slave_port(mpi->ibdev, mpi); 4377da78fe5fSMaor Gottlieb else 437893f82444SLeon Romanovsky list_del(&mpi->list); 437993f82444SLeon Romanovsky mutex_unlock(&mlx5_ib_multiport_mutex); 438093f82444SLeon Romanovsky kfree(mpi); 438193f82444SLeon Romanovsky } 438293f82444SLeon Romanovsky 438393f82444SLeon Romanovsky static int mlx5r_probe(struct auxiliary_device *adev, 438493f82444SLeon Romanovsky const struct auxiliary_device_id *id) 438593f82444SLeon Romanovsky { 438693f82444SLeon Romanovsky struct mlx5_adev *idev = container_of(adev, struct mlx5_adev, adev); 438793f82444SLeon Romanovsky struct mlx5_core_dev *mdev = idev->mdev; 438894de879cSMichael Guralnik const struct mlx5_ib_profile *profile; 438993f82444SLeon Romanovsky int port_type_cap, num_ports, ret; 439032f69e4bSDaniel Jurgens enum rdma_link_layer ll; 4391b5ca15adSMark Bloch struct mlx5_ib_dev *dev; 4392f0666f1fSBodong Wang 439332f69e4bSDaniel Jurgens port_type_cap = MLX5_CAP_GEN(mdev, port_type); 439432f69e4bSDaniel Jurgens ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap); 439532f69e4bSDaniel Jurgens 4396da796ccbSMark Bloch num_ports = max(MLX5_CAP_GEN(mdev, num_ports), 4397da796ccbSMark Bloch MLX5_CAP_GEN(mdev, num_vhca_ports)); 4398459cc69fSLeon Romanovsky dev = ib_alloc_device(mlx5_ib_dev, ib_dev); 4399b5ca15adSMark Bloch if (!dev) 440093f82444SLeon Romanovsky return -ENOMEM; 4401da796ccbSMark Bloch dev->port = kcalloc(num_ports, sizeof(*dev->port), 4402da796ccbSMark Bloch GFP_KERNEL); 4403da796ccbSMark Bloch if (!dev->port) { 4404a5c9c299SParav Pandit ib_dealloc_device(&dev->ib_dev); 440593f82444SLeon Romanovsky return -ENOMEM; 4406da796ccbSMark Bloch } 4407b5ca15adSMark Bloch 4408b5ca15adSMark Bloch dev->mdev = mdev; 4409da796ccbSMark Bloch dev->num_ports = num_ports; 4410b5ca15adSMark Bloch 44119ca05b0fSMaher Sanalla if (ll == IB_LINK_LAYER_ETHERNET && !mlx5_get_roce_state(mdev)) 441294de879cSMichael Guralnik profile = &raw_eth_profile; 441394de879cSMichael Guralnik else 441494de879cSMichael Guralnik profile = &pf_profile; 441594de879cSMichael Guralnik 441693f82444SLeon Romanovsky ret = __mlx5_ib_add(dev, profile); 441793f82444SLeon Romanovsky if (ret) { 441893f82444SLeon Romanovsky kfree(dev->port); 441993f82444SLeon Romanovsky ib_dealloc_device(&dev->ib_dev); 442093f82444SLeon Romanovsky return ret; 442116c1975fSMark Bloch } 442216c1975fSMark Bloch 442327963d3dSDavid E. Box auxiliary_set_drvdata(adev, dev); 442493f82444SLeon Romanovsky return 0; 442593f82444SLeon Romanovsky } 442693f82444SLeon Romanovsky 442793f82444SLeon Romanovsky static void mlx5r_remove(struct auxiliary_device *adev) 4428e126ba97SEli Cohen { 442932f69e4bSDaniel Jurgens struct mlx5_ib_dev *dev; 44306aec21f6SHaggai Eran 443127963d3dSDavid E. Box dev = auxiliary_get_drvdata(adev); 443216c1975fSMark Bloch __mlx5_ib_remove(dev, dev->profile, MLX5_IB_STAGE_MAX); 4433e126ba97SEli Cohen } 4434e126ba97SEli Cohen 443593f82444SLeon Romanovsky static const struct auxiliary_device_id mlx5r_mp_id_table[] = { 443693f82444SLeon Romanovsky { .name = MLX5_ADEV_NAME ".multiport", }, 443793f82444SLeon Romanovsky {}, 443893f82444SLeon Romanovsky }; 443993f82444SLeon Romanovsky 444093f82444SLeon Romanovsky static const struct auxiliary_device_id mlx5r_id_table[] = { 444193f82444SLeon Romanovsky { .name = MLX5_ADEV_NAME ".rdma", }, 444293f82444SLeon Romanovsky {}, 444393f82444SLeon Romanovsky }; 444493f82444SLeon Romanovsky 444593f82444SLeon Romanovsky MODULE_DEVICE_TABLE(auxiliary, mlx5r_mp_id_table); 444693f82444SLeon Romanovsky MODULE_DEVICE_TABLE(auxiliary, mlx5r_id_table); 444793f82444SLeon Romanovsky 444893f82444SLeon Romanovsky static struct auxiliary_driver mlx5r_mp_driver = { 444993f82444SLeon Romanovsky .name = "multiport", 445093f82444SLeon Romanovsky .probe = mlx5r_mp_probe, 445193f82444SLeon Romanovsky .remove = mlx5r_mp_remove, 445293f82444SLeon Romanovsky .id_table = mlx5r_mp_id_table, 445393f82444SLeon Romanovsky }; 445493f82444SLeon Romanovsky 445593f82444SLeon Romanovsky static struct auxiliary_driver mlx5r_driver = { 445693f82444SLeon Romanovsky .name = "rdma", 445793f82444SLeon Romanovsky .probe = mlx5r_probe, 445893f82444SLeon Romanovsky .remove = mlx5r_remove, 445993f82444SLeon Romanovsky .id_table = mlx5r_id_table, 4460e126ba97SEli Cohen }; 4461e126ba97SEli Cohen 4462e126ba97SEli Cohen static int __init mlx5_ib_init(void) 4463e126ba97SEli Cohen { 446493f82444SLeon Romanovsky int ret; 44656aec21f6SHaggai Eran 44668010d74bSJason Gunthorpe xlt_emergency_page = (void *)__get_free_page(GFP_KERNEL); 4467c44ef998SIlya Lesokhin if (!xlt_emergency_page) 4468d69a24e0SDaniel Jurgens return -ENOMEM; 4469d69a24e0SDaniel Jurgens 4470c44ef998SIlya Lesokhin mlx5_ib_event_wq = alloc_ordered_workqueue("mlx5_ib_event_wq", 0); 4471c44ef998SIlya Lesokhin if (!mlx5_ib_event_wq) { 44728010d74bSJason Gunthorpe free_page((unsigned long)xlt_emergency_page); 4473c44ef998SIlya Lesokhin return -ENOMEM; 4474c44ef998SIlya Lesokhin } 4475c44ef998SIlya Lesokhin 4476312b8f79SMark Zhang ret = mlx5_ib_qp_event_init(); 4477312b8f79SMark Zhang if (ret) 4478312b8f79SMark Zhang goto qp_event_err; 4479312b8f79SMark Zhang 448081713d37SArtemy Kovalyov mlx5_ib_odp_init(); 448193f82444SLeon Romanovsky ret = mlx5r_rep_init(); 448293f82444SLeon Romanovsky if (ret) 448393f82444SLeon Romanovsky goto rep_err; 448493f82444SLeon Romanovsky ret = auxiliary_driver_register(&mlx5r_mp_driver); 448593f82444SLeon Romanovsky if (ret) 448693f82444SLeon Romanovsky goto mp_err; 448793f82444SLeon Romanovsky ret = auxiliary_driver_register(&mlx5r_driver); 448893f82444SLeon Romanovsky if (ret) 448993f82444SLeon Romanovsky goto drv_err; 449093f82444SLeon Romanovsky return 0; 44919603b61dSJack Morgenstein 449293f82444SLeon Romanovsky drv_err: 449393f82444SLeon Romanovsky auxiliary_driver_unregister(&mlx5r_mp_driver); 449493f82444SLeon Romanovsky mp_err: 449593f82444SLeon Romanovsky mlx5r_rep_cleanup(); 449693f82444SLeon Romanovsky rep_err: 4497312b8f79SMark Zhang mlx5_ib_qp_event_cleanup(); 4498312b8f79SMark Zhang qp_event_err: 449993f82444SLeon Romanovsky destroy_workqueue(mlx5_ib_event_wq); 450093f82444SLeon Romanovsky free_page((unsigned long)xlt_emergency_page); 450193f82444SLeon Romanovsky return ret; 4502e126ba97SEli Cohen } 4503e126ba97SEli Cohen 4504e126ba97SEli Cohen static void __exit mlx5_ib_cleanup(void) 4505e126ba97SEli Cohen { 450693f82444SLeon Romanovsky auxiliary_driver_unregister(&mlx5r_driver); 450793f82444SLeon Romanovsky auxiliary_driver_unregister(&mlx5r_mp_driver); 450893f82444SLeon Romanovsky mlx5r_rep_cleanup(); 450993f82444SLeon Romanovsky 4510312b8f79SMark Zhang mlx5_ib_qp_event_cleanup(); 4511d69a24e0SDaniel Jurgens destroy_workqueue(mlx5_ib_event_wq); 45128010d74bSJason Gunthorpe free_page((unsigned long)xlt_emergency_page); 4513e126ba97SEli Cohen } 4514e126ba97SEli Cohen 4515e126ba97SEli Cohen module_init(mlx5_ib_init); 4516e126ba97SEli Cohen module_exit(mlx5_ib_cleanup); 4517