1e126ba97SEli Cohen /* 26cf0a15fSSaeed Mahameed * Copyright (c) 2013-2015, Mellanox Technologies. All rights reserved. 3e126ba97SEli Cohen * 4e126ba97SEli Cohen * This software is available to you under a choice of one of two 5e126ba97SEli Cohen * licenses. You may choose to be licensed under the terms of the GNU 6e126ba97SEli Cohen * General Public License (GPL) Version 2, available from the file 7e126ba97SEli Cohen * COPYING in the main directory of this source tree, or the 8e126ba97SEli Cohen * OpenIB.org BSD license below: 9e126ba97SEli Cohen * 10e126ba97SEli Cohen * Redistribution and use in source and binary forms, with or 11e126ba97SEli Cohen * without modification, are permitted provided that the following 12e126ba97SEli Cohen * conditions are met: 13e126ba97SEli Cohen * 14e126ba97SEli Cohen * - Redistributions of source code must retain the above 15e126ba97SEli Cohen * copyright notice, this list of conditions and the following 16e126ba97SEli Cohen * disclaimer. 17e126ba97SEli Cohen * 18e126ba97SEli Cohen * - Redistributions in binary form must reproduce the above 19e126ba97SEli Cohen * copyright notice, this list of conditions and the following 20e126ba97SEli Cohen * disclaimer in the documentation and/or other materials 21e126ba97SEli Cohen * provided with the distribution. 22e126ba97SEli Cohen * 23e126ba97SEli Cohen * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 24e126ba97SEli Cohen * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 25e126ba97SEli Cohen * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 26e126ba97SEli Cohen * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 27e126ba97SEli Cohen * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 28e126ba97SEli Cohen * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 29e126ba97SEli Cohen * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 30e126ba97SEli Cohen * SOFTWARE. 31e126ba97SEli Cohen */ 32e126ba97SEli Cohen 33fe248c3aSMaor Gottlieb #include <linux/debugfs.h> 34adec640eSChristoph Hellwig #include <linux/highmem.h> 35e126ba97SEli Cohen #include <linux/module.h> 36e126ba97SEli Cohen #include <linux/init.h> 37e126ba97SEli Cohen #include <linux/errno.h> 38e126ba97SEli Cohen #include <linux/pci.h> 39e126ba97SEli Cohen #include <linux/dma-mapping.h> 40e126ba97SEli Cohen #include <linux/slab.h> 4124da0016SAriel Levkovich #include <linux/bitmap.h> 4237aa5c36SGuy Levi #if defined(CONFIG_X86) 4337aa5c36SGuy Levi #include <asm/pat.h> 4437aa5c36SGuy Levi #endif 45e126ba97SEli Cohen #include <linux/sched.h> 466e84f315SIngo Molnar #include <linux/sched/mm.h> 470881e7bdSIngo Molnar #include <linux/sched/task.h> 487c2344c3SMaor Gottlieb #include <linux/delay.h> 49e126ba97SEli Cohen #include <rdma/ib_user_verbs.h> 503f89a643SAchiad Shochat #include <rdma/ib_addr.h> 512811ba51SAchiad Shochat #include <rdma/ib_cache.h> 52ada68c31SAchiad Shochat #include <linux/mlx5/port.h> 531b5daf11SMajd Dibbiny #include <linux/mlx5/vport.h> 5472c7fe90SPravin Shedge #include <linux/mlx5/fs.h> 557c2344c3SMaor Gottlieb #include <linux/list.h> 56e126ba97SEli Cohen #include <rdma/ib_smi.h> 57e126ba97SEli Cohen #include <rdma/ib_umem.h> 58038d2ef8SMaor Gottlieb #include <linux/in.h> 59038d2ef8SMaor Gottlieb #include <linux/etherdevice.h> 60e126ba97SEli Cohen #include "mlx5_ib.h" 61fc385b7aSMark Bloch #include "ib_rep.h" 62e1f24a79SParav Pandit #include "cmd.h" 633346c487SBoris Pismenny #include <linux/mlx5/fs_helpers.h> 64c6475a0bSAviad Yehezkel #include <linux/mlx5/accel.h> 658c84660bSMatan Barak #include <rdma/uverbs_std_types.h> 66c6475a0bSAviad Yehezkel #include <rdma/mlx5_user_ioctl_verbs.h> 67c6475a0bSAviad Yehezkel #include <rdma/mlx5_user_ioctl_cmds.h> 688c84660bSMatan Barak 698c84660bSMatan Barak #define UVERBS_MODULE_NAME mlx5_ib 708c84660bSMatan Barak #include <rdma/uverbs_named_ioctl.h> 71e126ba97SEli Cohen 72e126ba97SEli Cohen #define DRIVER_NAME "mlx5_ib" 73b359911dSTariq Toukan #define DRIVER_VERSION "5.0-0" 74e126ba97SEli Cohen 75e126ba97SEli Cohen MODULE_AUTHOR("Eli Cohen <eli@mellanox.com>"); 76e126ba97SEli Cohen MODULE_DESCRIPTION("Mellanox Connect-IB HCA IB driver"); 77e126ba97SEli Cohen MODULE_LICENSE("Dual BSD/GPL"); 78e126ba97SEli Cohen 79e126ba97SEli Cohen static char mlx5_version[] = 80e126ba97SEli Cohen DRIVER_NAME ": Mellanox Connect-IB Infiniband driver v" 81b359911dSTariq Toukan DRIVER_VERSION "\n"; 82e126ba97SEli Cohen 83d69a24e0SDaniel Jurgens struct mlx5_ib_event_work { 84d69a24e0SDaniel Jurgens struct work_struct work; 85d69a24e0SDaniel Jurgens struct mlx5_core_dev *dev; 86d69a24e0SDaniel Jurgens void *context; 87d69a24e0SDaniel Jurgens enum mlx5_dev_event event; 88d69a24e0SDaniel Jurgens unsigned long param; 89d69a24e0SDaniel Jurgens }; 90d69a24e0SDaniel Jurgens 91da7525d2SEran Ben Elisha enum { 92da7525d2SEran Ben Elisha MLX5_ATOMIC_SIZE_QP_8BYTES = 1 << 3, 93da7525d2SEran Ben Elisha }; 941b5daf11SMajd Dibbiny 95d69a24e0SDaniel Jurgens static struct workqueue_struct *mlx5_ib_event_wq; 9632f69e4bSDaniel Jurgens static LIST_HEAD(mlx5_ib_unaffiliated_port_list); 9732f69e4bSDaniel Jurgens static LIST_HEAD(mlx5_ib_dev_list); 9832f69e4bSDaniel Jurgens /* 9932f69e4bSDaniel Jurgens * This mutex should be held when accessing either of the above lists 10032f69e4bSDaniel Jurgens */ 10132f69e4bSDaniel Jurgens static DEFINE_MUTEX(mlx5_ib_multiport_mutex); 10232f69e4bSDaniel Jurgens 103c44ef998SIlya Lesokhin /* We can't use an array for xlt_emergency_page because dma_map_single 104c44ef998SIlya Lesokhin * doesn't work on kernel modules memory 105c44ef998SIlya Lesokhin */ 106c44ef998SIlya Lesokhin static unsigned long xlt_emergency_page; 107c44ef998SIlya Lesokhin static struct mutex xlt_emergency_page_mutex; 108c44ef998SIlya Lesokhin 10932f69e4bSDaniel Jurgens struct mlx5_ib_dev *mlx5_ib_get_ibdev_from_mpi(struct mlx5_ib_multiport_info *mpi) 11032f69e4bSDaniel Jurgens { 11132f69e4bSDaniel Jurgens struct mlx5_ib_dev *dev; 11232f69e4bSDaniel Jurgens 11332f69e4bSDaniel Jurgens mutex_lock(&mlx5_ib_multiport_mutex); 11432f69e4bSDaniel Jurgens dev = mpi->ibdev; 11532f69e4bSDaniel Jurgens mutex_unlock(&mlx5_ib_multiport_mutex); 11632f69e4bSDaniel Jurgens return dev; 11732f69e4bSDaniel Jurgens } 11832f69e4bSDaniel Jurgens 1191b5daf11SMajd Dibbiny static enum rdma_link_layer 120ebd61f68SAchiad Shochat mlx5_port_type_cap_to_rdma_ll(int port_type_cap) 1211b5daf11SMajd Dibbiny { 122ebd61f68SAchiad Shochat switch (port_type_cap) { 1231b5daf11SMajd Dibbiny case MLX5_CAP_PORT_TYPE_IB: 1241b5daf11SMajd Dibbiny return IB_LINK_LAYER_INFINIBAND; 1251b5daf11SMajd Dibbiny case MLX5_CAP_PORT_TYPE_ETH: 1261b5daf11SMajd Dibbiny return IB_LINK_LAYER_ETHERNET; 1271b5daf11SMajd Dibbiny default: 1281b5daf11SMajd Dibbiny return IB_LINK_LAYER_UNSPECIFIED; 1291b5daf11SMajd Dibbiny } 1301b5daf11SMajd Dibbiny } 1311b5daf11SMajd Dibbiny 132ebd61f68SAchiad Shochat static enum rdma_link_layer 133ebd61f68SAchiad Shochat mlx5_ib_port_link_layer(struct ib_device *device, u8 port_num) 134ebd61f68SAchiad Shochat { 135ebd61f68SAchiad Shochat struct mlx5_ib_dev *dev = to_mdev(device); 136ebd61f68SAchiad Shochat int port_type_cap = MLX5_CAP_GEN(dev->mdev, port_type); 137ebd61f68SAchiad Shochat 138ebd61f68SAchiad Shochat return mlx5_port_type_cap_to_rdma_ll(port_type_cap); 139ebd61f68SAchiad Shochat } 140ebd61f68SAchiad Shochat 141fd65f1b8SMoni Shoua static int get_port_state(struct ib_device *ibdev, 142fd65f1b8SMoni Shoua u8 port_num, 143fd65f1b8SMoni Shoua enum ib_port_state *state) 144fd65f1b8SMoni Shoua { 145fd65f1b8SMoni Shoua struct ib_port_attr attr; 146fd65f1b8SMoni Shoua int ret; 147fd65f1b8SMoni Shoua 148fd65f1b8SMoni Shoua memset(&attr, 0, sizeof(attr)); 1498e6efa3aSMark Bloch ret = ibdev->query_port(ibdev, port_num, &attr); 150fd65f1b8SMoni Shoua if (!ret) 151fd65f1b8SMoni Shoua *state = attr.state; 152fd65f1b8SMoni Shoua return ret; 153fd65f1b8SMoni Shoua } 154fd65f1b8SMoni Shoua 155fc24fc5eSAchiad Shochat static int mlx5_netdev_event(struct notifier_block *this, 156fc24fc5eSAchiad Shochat unsigned long event, void *ptr) 157fc24fc5eSAchiad Shochat { 1587fd8aefbSDaniel Jurgens struct mlx5_roce *roce = container_of(this, struct mlx5_roce, nb); 159fc24fc5eSAchiad Shochat struct net_device *ndev = netdev_notifier_info_to_dev(ptr); 1607fd8aefbSDaniel Jurgens u8 port_num = roce->native_port_num; 1617fd8aefbSDaniel Jurgens struct mlx5_core_dev *mdev; 1627fd8aefbSDaniel Jurgens struct mlx5_ib_dev *ibdev; 1637fd8aefbSDaniel Jurgens 1647fd8aefbSDaniel Jurgens ibdev = roce->dev; 16532f69e4bSDaniel Jurgens mdev = mlx5_ib_get_native_port_mdev(ibdev, port_num, NULL); 16632f69e4bSDaniel Jurgens if (!mdev) 16732f69e4bSDaniel Jurgens return NOTIFY_DONE; 168fc24fc5eSAchiad Shochat 1695ec8c83eSAviv Heller switch (event) { 1705ec8c83eSAviv Heller case NETDEV_REGISTER: 1715ec8c83eSAviv Heller case NETDEV_UNREGISTER: 1727fd8aefbSDaniel Jurgens write_lock(&roce->netdev_lock); 173bcf87f1dSMark Bloch if (ibdev->rep) { 174bcf87f1dSMark Bloch struct mlx5_eswitch *esw = ibdev->mdev->priv.eswitch; 175bcf87f1dSMark Bloch struct net_device *rep_ndev; 1767fd8aefbSDaniel Jurgens 177bcf87f1dSMark Bloch rep_ndev = mlx5_ib_get_rep_netdev(esw, 178bcf87f1dSMark Bloch ibdev->rep->vport); 179bcf87f1dSMark Bloch if (rep_ndev == ndev) 1807fd8aefbSDaniel Jurgens roce->netdev = (event == NETDEV_UNREGISTER) ? 1815ec8c83eSAviv Heller NULL : ndev; 18284a6a7a9SParav Pandit } else if (ndev->dev.parent == &mdev->pdev->dev) { 183bcf87f1dSMark Bloch roce->netdev = (event == NETDEV_UNREGISTER) ? 184bcf87f1dSMark Bloch NULL : ndev; 185bcf87f1dSMark Bloch } 1867fd8aefbSDaniel Jurgens write_unlock(&roce->netdev_lock); 1875ec8c83eSAviv Heller break; 1885ec8c83eSAviv Heller 189fd65f1b8SMoni Shoua case NETDEV_CHANGE: 1905ec8c83eSAviv Heller case NETDEV_UP: 19188621dfeSAviv Heller case NETDEV_DOWN: { 1927fd8aefbSDaniel Jurgens struct net_device *lag_ndev = mlx5_lag_get_roce_netdev(mdev); 19388621dfeSAviv Heller struct net_device *upper = NULL; 19488621dfeSAviv Heller 19588621dfeSAviv Heller if (lag_ndev) { 19688621dfeSAviv Heller upper = netdev_master_upper_dev_get(lag_ndev); 19788621dfeSAviv Heller dev_put(lag_ndev); 19888621dfeSAviv Heller } 19988621dfeSAviv Heller 2007fd8aefbSDaniel Jurgens if ((upper == ndev || (!upper && ndev == roce->netdev)) 20188621dfeSAviv Heller && ibdev->ib_active) { 202626bc02dSBart Van Assche struct ib_event ibev = { }; 203fd65f1b8SMoni Shoua enum ib_port_state port_state; 2045ec8c83eSAviv Heller 2057fd8aefbSDaniel Jurgens if (get_port_state(&ibdev->ib_dev, port_num, 2067fd8aefbSDaniel Jurgens &port_state)) 2077fd8aefbSDaniel Jurgens goto done; 208fd65f1b8SMoni Shoua 2097fd8aefbSDaniel Jurgens if (roce->last_port_state == port_state) 2107fd8aefbSDaniel Jurgens goto done; 211fd65f1b8SMoni Shoua 2127fd8aefbSDaniel Jurgens roce->last_port_state = port_state; 2135ec8c83eSAviv Heller ibev.device = &ibdev->ib_dev; 214fd65f1b8SMoni Shoua if (port_state == IB_PORT_DOWN) 215fd65f1b8SMoni Shoua ibev.event = IB_EVENT_PORT_ERR; 216fd65f1b8SMoni Shoua else if (port_state == IB_PORT_ACTIVE) 217fd65f1b8SMoni Shoua ibev.event = IB_EVENT_PORT_ACTIVE; 218fd65f1b8SMoni Shoua else 2197fd8aefbSDaniel Jurgens goto done; 220fd65f1b8SMoni Shoua 2217fd8aefbSDaniel Jurgens ibev.element.port_num = port_num; 2225ec8c83eSAviv Heller ib_dispatch_event(&ibev); 2235ec8c83eSAviv Heller } 2245ec8c83eSAviv Heller break; 22588621dfeSAviv Heller } 2265ec8c83eSAviv Heller 2275ec8c83eSAviv Heller default: 2285ec8c83eSAviv Heller break; 2295ec8c83eSAviv Heller } 2307fd8aefbSDaniel Jurgens done: 23132f69e4bSDaniel Jurgens mlx5_ib_put_native_port_mdev(ibdev, port_num); 232fc24fc5eSAchiad Shochat return NOTIFY_DONE; 233fc24fc5eSAchiad Shochat } 234fc24fc5eSAchiad Shochat 235fc24fc5eSAchiad Shochat static struct net_device *mlx5_ib_get_netdev(struct ib_device *device, 236fc24fc5eSAchiad Shochat u8 port_num) 237fc24fc5eSAchiad Shochat { 238fc24fc5eSAchiad Shochat struct mlx5_ib_dev *ibdev = to_mdev(device); 239fc24fc5eSAchiad Shochat struct net_device *ndev; 24032f69e4bSDaniel Jurgens struct mlx5_core_dev *mdev; 241fc24fc5eSAchiad Shochat 24232f69e4bSDaniel Jurgens mdev = mlx5_ib_get_native_port_mdev(ibdev, port_num, NULL); 24332f69e4bSDaniel Jurgens if (!mdev) 24432f69e4bSDaniel Jurgens return NULL; 24532f69e4bSDaniel Jurgens 24632f69e4bSDaniel Jurgens ndev = mlx5_lag_get_roce_netdev(mdev); 24788621dfeSAviv Heller if (ndev) 24832f69e4bSDaniel Jurgens goto out; 24988621dfeSAviv Heller 250fc24fc5eSAchiad Shochat /* Ensure ndev does not disappear before we invoke dev_hold() 251fc24fc5eSAchiad Shochat */ 2527fd8aefbSDaniel Jurgens read_lock(&ibdev->roce[port_num - 1].netdev_lock); 2537fd8aefbSDaniel Jurgens ndev = ibdev->roce[port_num - 1].netdev; 254fc24fc5eSAchiad Shochat if (ndev) 255fc24fc5eSAchiad Shochat dev_hold(ndev); 2567fd8aefbSDaniel Jurgens read_unlock(&ibdev->roce[port_num - 1].netdev_lock); 257fc24fc5eSAchiad Shochat 25832f69e4bSDaniel Jurgens out: 25932f69e4bSDaniel Jurgens mlx5_ib_put_native_port_mdev(ibdev, port_num); 260fc24fc5eSAchiad Shochat return ndev; 261fc24fc5eSAchiad Shochat } 262fc24fc5eSAchiad Shochat 26332f69e4bSDaniel Jurgens struct mlx5_core_dev *mlx5_ib_get_native_port_mdev(struct mlx5_ib_dev *ibdev, 26432f69e4bSDaniel Jurgens u8 ib_port_num, 26532f69e4bSDaniel Jurgens u8 *native_port_num) 26632f69e4bSDaniel Jurgens { 26732f69e4bSDaniel Jurgens enum rdma_link_layer ll = mlx5_ib_port_link_layer(&ibdev->ib_dev, 26832f69e4bSDaniel Jurgens ib_port_num); 26932f69e4bSDaniel Jurgens struct mlx5_core_dev *mdev = NULL; 27032f69e4bSDaniel Jurgens struct mlx5_ib_multiport_info *mpi; 27132f69e4bSDaniel Jurgens struct mlx5_ib_port *port; 27232f69e4bSDaniel Jurgens 273210b1f78SMark Bloch if (!mlx5_core_mp_enabled(ibdev->mdev) || 274210b1f78SMark Bloch ll != IB_LINK_LAYER_ETHERNET) { 275210b1f78SMark Bloch if (native_port_num) 276210b1f78SMark Bloch *native_port_num = ib_port_num; 277210b1f78SMark Bloch return ibdev->mdev; 278210b1f78SMark Bloch } 279210b1f78SMark Bloch 28032f69e4bSDaniel Jurgens if (native_port_num) 28132f69e4bSDaniel Jurgens *native_port_num = 1; 28232f69e4bSDaniel Jurgens 28332f69e4bSDaniel Jurgens port = &ibdev->port[ib_port_num - 1]; 28432f69e4bSDaniel Jurgens if (!port) 28532f69e4bSDaniel Jurgens return NULL; 28632f69e4bSDaniel Jurgens 28732f69e4bSDaniel Jurgens spin_lock(&port->mp.mpi_lock); 28832f69e4bSDaniel Jurgens mpi = ibdev->port[ib_port_num - 1].mp.mpi; 28932f69e4bSDaniel Jurgens if (mpi && !mpi->unaffiliate) { 29032f69e4bSDaniel Jurgens mdev = mpi->mdev; 29132f69e4bSDaniel Jurgens /* If it's the master no need to refcount, it'll exist 29232f69e4bSDaniel Jurgens * as long as the ib_dev exists. 29332f69e4bSDaniel Jurgens */ 29432f69e4bSDaniel Jurgens if (!mpi->is_master) 29532f69e4bSDaniel Jurgens mpi->mdev_refcnt++; 29632f69e4bSDaniel Jurgens } 29732f69e4bSDaniel Jurgens spin_unlock(&port->mp.mpi_lock); 29832f69e4bSDaniel Jurgens 29932f69e4bSDaniel Jurgens return mdev; 30032f69e4bSDaniel Jurgens } 30132f69e4bSDaniel Jurgens 30232f69e4bSDaniel Jurgens void mlx5_ib_put_native_port_mdev(struct mlx5_ib_dev *ibdev, u8 port_num) 30332f69e4bSDaniel Jurgens { 30432f69e4bSDaniel Jurgens enum rdma_link_layer ll = mlx5_ib_port_link_layer(&ibdev->ib_dev, 30532f69e4bSDaniel Jurgens port_num); 30632f69e4bSDaniel Jurgens struct mlx5_ib_multiport_info *mpi; 30732f69e4bSDaniel Jurgens struct mlx5_ib_port *port; 30832f69e4bSDaniel Jurgens 30932f69e4bSDaniel Jurgens if (!mlx5_core_mp_enabled(ibdev->mdev) || ll != IB_LINK_LAYER_ETHERNET) 31032f69e4bSDaniel Jurgens return; 31132f69e4bSDaniel Jurgens 31232f69e4bSDaniel Jurgens port = &ibdev->port[port_num - 1]; 31332f69e4bSDaniel Jurgens 31432f69e4bSDaniel Jurgens spin_lock(&port->mp.mpi_lock); 31532f69e4bSDaniel Jurgens mpi = ibdev->port[port_num - 1].mp.mpi; 31632f69e4bSDaniel Jurgens if (mpi->is_master) 31732f69e4bSDaniel Jurgens goto out; 31832f69e4bSDaniel Jurgens 31932f69e4bSDaniel Jurgens mpi->mdev_refcnt--; 32032f69e4bSDaniel Jurgens if (mpi->unaffiliate) 32132f69e4bSDaniel Jurgens complete(&mpi->unref_comp); 32232f69e4bSDaniel Jurgens out: 32332f69e4bSDaniel Jurgens spin_unlock(&port->mp.mpi_lock); 32432f69e4bSDaniel Jurgens } 32532f69e4bSDaniel Jurgens 326f1b65df5SNoa Osherovich static int translate_eth_proto_oper(u32 eth_proto_oper, u8 *active_speed, 327f1b65df5SNoa Osherovich u8 *active_width) 328f1b65df5SNoa Osherovich { 329f1b65df5SNoa Osherovich switch (eth_proto_oper) { 330f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_1000BASE_CX_SGMII): 331f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_1000BASE_KX): 332f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_100BASE_TX): 333f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_1000BASE_T): 334f1b65df5SNoa Osherovich *active_width = IB_WIDTH_1X; 335f1b65df5SNoa Osherovich *active_speed = IB_SPEED_SDR; 336f1b65df5SNoa Osherovich break; 337f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_10GBASE_T): 338f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_10GBASE_CX4): 339f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_10GBASE_KX4): 340f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_10GBASE_KR): 341f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_10GBASE_CR): 342f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_10GBASE_SR): 343f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_10GBASE_ER): 344f1b65df5SNoa Osherovich *active_width = IB_WIDTH_1X; 345f1b65df5SNoa Osherovich *active_speed = IB_SPEED_QDR; 346f1b65df5SNoa Osherovich break; 347f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_25GBASE_CR): 348f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_25GBASE_KR): 349f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_25GBASE_SR): 350f1b65df5SNoa Osherovich *active_width = IB_WIDTH_1X; 351f1b65df5SNoa Osherovich *active_speed = IB_SPEED_EDR; 352f1b65df5SNoa Osherovich break; 353f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_40GBASE_CR4): 354f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_40GBASE_KR4): 355f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_40GBASE_SR4): 356f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_40GBASE_LR4): 357f1b65df5SNoa Osherovich *active_width = IB_WIDTH_4X; 358f1b65df5SNoa Osherovich *active_speed = IB_SPEED_QDR; 359f1b65df5SNoa Osherovich break; 360f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_50GBASE_CR2): 361f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_50GBASE_KR2): 362f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_50GBASE_SR2): 363f1b65df5SNoa Osherovich *active_width = IB_WIDTH_1X; 364f1b65df5SNoa Osherovich *active_speed = IB_SPEED_HDR; 365f1b65df5SNoa Osherovich break; 366f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_56GBASE_R4): 367f1b65df5SNoa Osherovich *active_width = IB_WIDTH_4X; 368f1b65df5SNoa Osherovich *active_speed = IB_SPEED_FDR; 369f1b65df5SNoa Osherovich break; 370f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_100GBASE_CR4): 371f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_100GBASE_SR4): 372f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_100GBASE_KR4): 373f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_100GBASE_LR4): 374f1b65df5SNoa Osherovich *active_width = IB_WIDTH_4X; 375f1b65df5SNoa Osherovich *active_speed = IB_SPEED_EDR; 376f1b65df5SNoa Osherovich break; 377f1b65df5SNoa Osherovich default: 378f1b65df5SNoa Osherovich return -EINVAL; 379f1b65df5SNoa Osherovich } 380f1b65df5SNoa Osherovich 381f1b65df5SNoa Osherovich return 0; 382f1b65df5SNoa Osherovich } 383f1b65df5SNoa Osherovich 384095b0927SIlan Tayari static int mlx5_query_port_roce(struct ib_device *device, u8 port_num, 3853f89a643SAchiad Shochat struct ib_port_attr *props) 3863f89a643SAchiad Shochat { 3873f89a643SAchiad Shochat struct mlx5_ib_dev *dev = to_mdev(device); 388da005f9fSColin Ian King struct mlx5_core_dev *mdev; 38988621dfeSAviv Heller struct net_device *ndev, *upper; 3903f89a643SAchiad Shochat enum ib_mtu ndev_ib_mtu; 391b3cbd6f0SDaniel Jurgens bool put_mdev = true; 392c876a1b7SLeon Romanovsky u16 qkey_viol_cntr; 393f1b65df5SNoa Osherovich u32 eth_prot_oper; 394b3cbd6f0SDaniel Jurgens u8 mdev_port_num; 395095b0927SIlan Tayari int err; 3963f89a643SAchiad Shochat 397b3cbd6f0SDaniel Jurgens mdev = mlx5_ib_get_native_port_mdev(dev, port_num, &mdev_port_num); 398b3cbd6f0SDaniel Jurgens if (!mdev) { 399b3cbd6f0SDaniel Jurgens /* This means the port isn't affiliated yet. Get the 400b3cbd6f0SDaniel Jurgens * info for the master port instead. 401b3cbd6f0SDaniel Jurgens */ 402b3cbd6f0SDaniel Jurgens put_mdev = false; 403b3cbd6f0SDaniel Jurgens mdev = dev->mdev; 404b3cbd6f0SDaniel Jurgens mdev_port_num = 1; 405b3cbd6f0SDaniel Jurgens port_num = 1; 406b3cbd6f0SDaniel Jurgens } 407b3cbd6f0SDaniel Jurgens 408f1b65df5SNoa Osherovich /* Possible bad flows are checked before filling out props so in case 409f1b65df5SNoa Osherovich * of an error it will still be zeroed out. 41050f22fd8SNoa Osherovich */ 411b3cbd6f0SDaniel Jurgens err = mlx5_query_port_eth_proto_oper(mdev, ð_prot_oper, 412b3cbd6f0SDaniel Jurgens mdev_port_num); 413095b0927SIlan Tayari if (err) 414b3cbd6f0SDaniel Jurgens goto out; 415f1b65df5SNoa Osherovich 4167672ed33SHonggang Li props->active_width = IB_WIDTH_4X; 4177672ed33SHonggang Li props->active_speed = IB_SPEED_QDR; 4187672ed33SHonggang Li 419f1b65df5SNoa Osherovich translate_eth_proto_oper(eth_prot_oper, &props->active_speed, 420f1b65df5SNoa Osherovich &props->active_width); 4213f89a643SAchiad Shochat 4223f89a643SAchiad Shochat props->port_cap_flags |= IB_PORT_CM_SUP; 4233f89a643SAchiad Shochat props->port_cap_flags |= IB_PORT_IP_BASED_GIDS; 4243f89a643SAchiad Shochat 4253f89a643SAchiad Shochat props->gid_tbl_len = MLX5_CAP_ROCE(dev->mdev, 4263f89a643SAchiad Shochat roce_address_table_size); 4273f89a643SAchiad Shochat props->max_mtu = IB_MTU_4096; 4283f89a643SAchiad Shochat props->max_msg_sz = 1 << MLX5_CAP_GEN(dev->mdev, log_max_msg); 4293f89a643SAchiad Shochat props->pkey_tbl_len = 1; 4303f89a643SAchiad Shochat props->state = IB_PORT_DOWN; 4313f89a643SAchiad Shochat props->phys_state = 3; 4323f89a643SAchiad Shochat 433b3cbd6f0SDaniel Jurgens mlx5_query_nic_vport_qkey_viol_cntr(mdev, &qkey_viol_cntr); 434c876a1b7SLeon Romanovsky props->qkey_viol_cntr = qkey_viol_cntr; 4353f89a643SAchiad Shochat 436b3cbd6f0SDaniel Jurgens /* If this is a stub query for an unaffiliated port stop here */ 437b3cbd6f0SDaniel Jurgens if (!put_mdev) 438b3cbd6f0SDaniel Jurgens goto out; 439b3cbd6f0SDaniel Jurgens 4403f89a643SAchiad Shochat ndev = mlx5_ib_get_netdev(device, port_num); 4413f89a643SAchiad Shochat if (!ndev) 442b3cbd6f0SDaniel Jurgens goto out; 4433f89a643SAchiad Shochat 44488621dfeSAviv Heller if (mlx5_lag_is_active(dev->mdev)) { 44588621dfeSAviv Heller rcu_read_lock(); 44688621dfeSAviv Heller upper = netdev_master_upper_dev_get_rcu(ndev); 44788621dfeSAviv Heller if (upper) { 44888621dfeSAviv Heller dev_put(ndev); 44988621dfeSAviv Heller ndev = upper; 45088621dfeSAviv Heller dev_hold(ndev); 45188621dfeSAviv Heller } 45288621dfeSAviv Heller rcu_read_unlock(); 45388621dfeSAviv Heller } 45488621dfeSAviv Heller 4553f89a643SAchiad Shochat if (netif_running(ndev) && netif_carrier_ok(ndev)) { 4563f89a643SAchiad Shochat props->state = IB_PORT_ACTIVE; 4573f89a643SAchiad Shochat props->phys_state = 5; 4583f89a643SAchiad Shochat } 4593f89a643SAchiad Shochat 4603f89a643SAchiad Shochat ndev_ib_mtu = iboe_get_mtu(ndev->mtu); 4613f89a643SAchiad Shochat 4623f89a643SAchiad Shochat dev_put(ndev); 4633f89a643SAchiad Shochat 4643f89a643SAchiad Shochat props->active_mtu = min(props->max_mtu, ndev_ib_mtu); 465b3cbd6f0SDaniel Jurgens out: 466b3cbd6f0SDaniel Jurgens if (put_mdev) 467b3cbd6f0SDaniel Jurgens mlx5_ib_put_native_port_mdev(dev, port_num); 468b3cbd6f0SDaniel Jurgens return err; 4693f89a643SAchiad Shochat } 4703f89a643SAchiad Shochat 471095b0927SIlan Tayari static int set_roce_addr(struct mlx5_ib_dev *dev, u8 port_num, 472095b0927SIlan Tayari unsigned int index, const union ib_gid *gid, 473095b0927SIlan Tayari const struct ib_gid_attr *attr) 4743cca2606SAchiad Shochat { 475095b0927SIlan Tayari enum ib_gid_type gid_type = IB_GID_TYPE_IB; 476095b0927SIlan Tayari u8 roce_version = 0; 477095b0927SIlan Tayari u8 roce_l3_type = 0; 478095b0927SIlan Tayari bool vlan = false; 479095b0927SIlan Tayari u8 mac[ETH_ALEN]; 480095b0927SIlan Tayari u16 vlan_id = 0; 4813cca2606SAchiad Shochat 482095b0927SIlan Tayari if (gid) { 483095b0927SIlan Tayari gid_type = attr->gid_type; 484095b0927SIlan Tayari ether_addr_copy(mac, attr->ndev->dev_addr); 4853cca2606SAchiad Shochat 4863cca2606SAchiad Shochat if (is_vlan_dev(attr->ndev)) { 487095b0927SIlan Tayari vlan = true; 488095b0927SIlan Tayari vlan_id = vlan_dev_vlan_id(attr->ndev); 489095b0927SIlan Tayari } 4903cca2606SAchiad Shochat } 4913cca2606SAchiad Shochat 492095b0927SIlan Tayari switch (gid_type) { 4933cca2606SAchiad Shochat case IB_GID_TYPE_IB: 494095b0927SIlan Tayari roce_version = MLX5_ROCE_VERSION_1; 4953cca2606SAchiad Shochat break; 4963cca2606SAchiad Shochat case IB_GID_TYPE_ROCE_UDP_ENCAP: 497095b0927SIlan Tayari roce_version = MLX5_ROCE_VERSION_2; 498095b0927SIlan Tayari if (ipv6_addr_v4mapped((void *)gid)) 499095b0927SIlan Tayari roce_l3_type = MLX5_ROCE_L3_TYPE_IPV4; 500095b0927SIlan Tayari else 501095b0927SIlan Tayari roce_l3_type = MLX5_ROCE_L3_TYPE_IPV6; 5023cca2606SAchiad Shochat break; 5033cca2606SAchiad Shochat 5043cca2606SAchiad Shochat default: 505095b0927SIlan Tayari mlx5_ib_warn(dev, "Unexpected GID type %u\n", gid_type); 5063cca2606SAchiad Shochat } 5073cca2606SAchiad Shochat 508095b0927SIlan Tayari return mlx5_core_roce_gid_set(dev->mdev, index, roce_version, 509095b0927SIlan Tayari roce_l3_type, gid->raw, mac, vlan, 510cfe4e37fSDaniel Jurgens vlan_id, port_num); 5113cca2606SAchiad Shochat } 5123cca2606SAchiad Shochat 513414448d2SParav Pandit static int mlx5_ib_add_gid(const union ib_gid *gid, 5143cca2606SAchiad Shochat const struct ib_gid_attr *attr, 5153cca2606SAchiad Shochat __always_unused void **context) 5163cca2606SAchiad Shochat { 517414448d2SParav Pandit return set_roce_addr(to_mdev(attr->device), attr->port_num, 518414448d2SParav Pandit attr->index, gid, attr); 5193cca2606SAchiad Shochat } 5203cca2606SAchiad Shochat 521414448d2SParav Pandit static int mlx5_ib_del_gid(const struct ib_gid_attr *attr, 522414448d2SParav Pandit __always_unused void **context) 5233cca2606SAchiad Shochat { 524414448d2SParav Pandit return set_roce_addr(to_mdev(attr->device), attr->port_num, 525414448d2SParav Pandit attr->index, NULL, NULL); 5263cca2606SAchiad Shochat } 5273cca2606SAchiad Shochat 5282811ba51SAchiad Shochat __be16 mlx5_get_roce_udp_sport(struct mlx5_ib_dev *dev, u8 port_num, 5292811ba51SAchiad Shochat int index) 5302811ba51SAchiad Shochat { 5312811ba51SAchiad Shochat struct ib_gid_attr attr; 5322811ba51SAchiad Shochat union ib_gid gid; 5332811ba51SAchiad Shochat 5342811ba51SAchiad Shochat if (ib_get_cached_gid(&dev->ib_dev, port_num, index, &gid, &attr)) 5352811ba51SAchiad Shochat return 0; 5362811ba51SAchiad Shochat 5372811ba51SAchiad Shochat dev_put(attr.ndev); 5382811ba51SAchiad Shochat 5392811ba51SAchiad Shochat if (attr.gid_type != IB_GID_TYPE_ROCE_UDP_ENCAP) 5402811ba51SAchiad Shochat return 0; 5412811ba51SAchiad Shochat 5422811ba51SAchiad Shochat return cpu_to_be16(MLX5_CAP_ROCE(dev->mdev, r_roce_min_src_udp_port)); 5432811ba51SAchiad Shochat } 5442811ba51SAchiad Shochat 545ed88451eSMajd Dibbiny int mlx5_get_roce_gid_type(struct mlx5_ib_dev *dev, u8 port_num, 546ed88451eSMajd Dibbiny int index, enum ib_gid_type *gid_type) 547ed88451eSMajd Dibbiny { 548ed88451eSMajd Dibbiny struct ib_gid_attr attr; 549ed88451eSMajd Dibbiny union ib_gid gid; 550ed88451eSMajd Dibbiny int ret; 551ed88451eSMajd Dibbiny 552ed88451eSMajd Dibbiny ret = ib_get_cached_gid(&dev->ib_dev, port_num, index, &gid, &attr); 553ed88451eSMajd Dibbiny if (ret) 554ed88451eSMajd Dibbiny return ret; 555ed88451eSMajd Dibbiny 556ed88451eSMajd Dibbiny dev_put(attr.ndev); 557ed88451eSMajd Dibbiny 558ed88451eSMajd Dibbiny *gid_type = attr.gid_type; 559ed88451eSMajd Dibbiny 560ed88451eSMajd Dibbiny return 0; 561ed88451eSMajd Dibbiny } 562ed88451eSMajd Dibbiny 5631b5daf11SMajd Dibbiny static int mlx5_use_mad_ifc(struct mlx5_ib_dev *dev) 5641b5daf11SMajd Dibbiny { 5657fae6655SNoa Osherovich if (MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_IB) 566d603c809SEli Cohen return !MLX5_CAP_GEN(dev->mdev, ib_virt); 5677fae6655SNoa Osherovich return 0; 5681b5daf11SMajd Dibbiny } 5691b5daf11SMajd Dibbiny 5701b5daf11SMajd Dibbiny enum { 5711b5daf11SMajd Dibbiny MLX5_VPORT_ACCESS_METHOD_MAD, 5721b5daf11SMajd Dibbiny MLX5_VPORT_ACCESS_METHOD_HCA, 5731b5daf11SMajd Dibbiny MLX5_VPORT_ACCESS_METHOD_NIC, 5741b5daf11SMajd Dibbiny }; 5751b5daf11SMajd Dibbiny 5761b5daf11SMajd Dibbiny static int mlx5_get_vport_access_method(struct ib_device *ibdev) 5771b5daf11SMajd Dibbiny { 5781b5daf11SMajd Dibbiny if (mlx5_use_mad_ifc(to_mdev(ibdev))) 5791b5daf11SMajd Dibbiny return MLX5_VPORT_ACCESS_METHOD_MAD; 5801b5daf11SMajd Dibbiny 581ebd61f68SAchiad Shochat if (mlx5_ib_port_link_layer(ibdev, 1) == 5821b5daf11SMajd Dibbiny IB_LINK_LAYER_ETHERNET) 5831b5daf11SMajd Dibbiny return MLX5_VPORT_ACCESS_METHOD_NIC; 5841b5daf11SMajd Dibbiny 5851b5daf11SMajd Dibbiny return MLX5_VPORT_ACCESS_METHOD_HCA; 5861b5daf11SMajd Dibbiny } 5871b5daf11SMajd Dibbiny 588da7525d2SEran Ben Elisha static void get_atomic_caps(struct mlx5_ib_dev *dev, 589776a3906SMoni Shoua u8 atomic_size_qp, 590da7525d2SEran Ben Elisha struct ib_device_attr *props) 591da7525d2SEran Ben Elisha { 592da7525d2SEran Ben Elisha u8 tmp; 593da7525d2SEran Ben Elisha u8 atomic_operations = MLX5_CAP_ATOMIC(dev->mdev, atomic_operations); 594da7525d2SEran Ben Elisha u8 atomic_req_8B_endianness_mode = 595bd10838aSOr Gerlitz MLX5_CAP_ATOMIC(dev->mdev, atomic_req_8B_endianness_mode); 596da7525d2SEran Ben Elisha 597da7525d2SEran Ben Elisha /* Check if HW supports 8 bytes standard atomic operations and capable 598da7525d2SEran Ben Elisha * of host endianness respond 599da7525d2SEran Ben Elisha */ 600da7525d2SEran Ben Elisha tmp = MLX5_ATOMIC_OPS_CMP_SWAP | MLX5_ATOMIC_OPS_FETCH_ADD; 601da7525d2SEran Ben Elisha if (((atomic_operations & tmp) == tmp) && 602da7525d2SEran Ben Elisha (atomic_size_qp & MLX5_ATOMIC_SIZE_QP_8BYTES) && 603da7525d2SEran Ben Elisha (atomic_req_8B_endianness_mode)) { 604da7525d2SEran Ben Elisha props->atomic_cap = IB_ATOMIC_HCA; 605da7525d2SEran Ben Elisha } else { 606da7525d2SEran Ben Elisha props->atomic_cap = IB_ATOMIC_NONE; 607da7525d2SEran Ben Elisha } 608da7525d2SEran Ben Elisha } 609da7525d2SEran Ben Elisha 610776a3906SMoni Shoua static void get_atomic_caps_qp(struct mlx5_ib_dev *dev, 611776a3906SMoni Shoua struct ib_device_attr *props) 612776a3906SMoni Shoua { 613776a3906SMoni Shoua u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_qp); 614776a3906SMoni Shoua 615776a3906SMoni Shoua get_atomic_caps(dev, atomic_size_qp, props); 616776a3906SMoni Shoua } 617776a3906SMoni Shoua 618776a3906SMoni Shoua static void get_atomic_caps_dc(struct mlx5_ib_dev *dev, 619776a3906SMoni Shoua struct ib_device_attr *props) 620776a3906SMoni Shoua { 621776a3906SMoni Shoua u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_dc); 622776a3906SMoni Shoua 623776a3906SMoni Shoua get_atomic_caps(dev, atomic_size_qp, props); 624776a3906SMoni Shoua } 625776a3906SMoni Shoua 626776a3906SMoni Shoua bool mlx5_ib_dc_atomic_is_supported(struct mlx5_ib_dev *dev) 627776a3906SMoni Shoua { 628776a3906SMoni Shoua struct ib_device_attr props = {}; 629776a3906SMoni Shoua 630776a3906SMoni Shoua get_atomic_caps_dc(dev, &props); 631776a3906SMoni Shoua return (props.atomic_cap == IB_ATOMIC_HCA) ? true : false; 632776a3906SMoni Shoua } 6331b5daf11SMajd Dibbiny static int mlx5_query_system_image_guid(struct ib_device *ibdev, 6341b5daf11SMajd Dibbiny __be64 *sys_image_guid) 6351b5daf11SMajd Dibbiny { 6361b5daf11SMajd Dibbiny struct mlx5_ib_dev *dev = to_mdev(ibdev); 6371b5daf11SMajd Dibbiny struct mlx5_core_dev *mdev = dev->mdev; 6381b5daf11SMajd Dibbiny u64 tmp; 6391b5daf11SMajd Dibbiny int err; 6401b5daf11SMajd Dibbiny 6411b5daf11SMajd Dibbiny switch (mlx5_get_vport_access_method(ibdev)) { 6421b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_MAD: 6431b5daf11SMajd Dibbiny return mlx5_query_mad_ifc_system_image_guid(ibdev, 6441b5daf11SMajd Dibbiny sys_image_guid); 6451b5daf11SMajd Dibbiny 6461b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_HCA: 6471b5daf11SMajd Dibbiny err = mlx5_query_hca_vport_system_image_guid(mdev, &tmp); 6483f89a643SAchiad Shochat break; 6493f89a643SAchiad Shochat 6503f89a643SAchiad Shochat case MLX5_VPORT_ACCESS_METHOD_NIC: 6513f89a643SAchiad Shochat err = mlx5_query_nic_vport_system_image_guid(mdev, &tmp); 6523f89a643SAchiad Shochat break; 6531b5daf11SMajd Dibbiny 6541b5daf11SMajd Dibbiny default: 6551b5daf11SMajd Dibbiny return -EINVAL; 6561b5daf11SMajd Dibbiny } 6573f89a643SAchiad Shochat 6583f89a643SAchiad Shochat if (!err) 6593f89a643SAchiad Shochat *sys_image_guid = cpu_to_be64(tmp); 6603f89a643SAchiad Shochat 6613f89a643SAchiad Shochat return err; 6623f89a643SAchiad Shochat 6631b5daf11SMajd Dibbiny } 6641b5daf11SMajd Dibbiny 6651b5daf11SMajd Dibbiny static int mlx5_query_max_pkeys(struct ib_device *ibdev, 6661b5daf11SMajd Dibbiny u16 *max_pkeys) 6671b5daf11SMajd Dibbiny { 6681b5daf11SMajd Dibbiny struct mlx5_ib_dev *dev = to_mdev(ibdev); 6691b5daf11SMajd Dibbiny struct mlx5_core_dev *mdev = dev->mdev; 6701b5daf11SMajd Dibbiny 6711b5daf11SMajd Dibbiny switch (mlx5_get_vport_access_method(ibdev)) { 6721b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_MAD: 6731b5daf11SMajd Dibbiny return mlx5_query_mad_ifc_max_pkeys(ibdev, max_pkeys); 6741b5daf11SMajd Dibbiny 6751b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_HCA: 6761b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_NIC: 6771b5daf11SMajd Dibbiny *max_pkeys = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, 6781b5daf11SMajd Dibbiny pkey_table_size)); 6791b5daf11SMajd Dibbiny return 0; 6801b5daf11SMajd Dibbiny 6811b5daf11SMajd Dibbiny default: 6821b5daf11SMajd Dibbiny return -EINVAL; 6831b5daf11SMajd Dibbiny } 6841b5daf11SMajd Dibbiny } 6851b5daf11SMajd Dibbiny 6861b5daf11SMajd Dibbiny static int mlx5_query_vendor_id(struct ib_device *ibdev, 6871b5daf11SMajd Dibbiny u32 *vendor_id) 6881b5daf11SMajd Dibbiny { 6891b5daf11SMajd Dibbiny struct mlx5_ib_dev *dev = to_mdev(ibdev); 6901b5daf11SMajd Dibbiny 6911b5daf11SMajd Dibbiny switch (mlx5_get_vport_access_method(ibdev)) { 6921b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_MAD: 6931b5daf11SMajd Dibbiny return mlx5_query_mad_ifc_vendor_id(ibdev, vendor_id); 6941b5daf11SMajd Dibbiny 6951b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_HCA: 6961b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_NIC: 6971b5daf11SMajd Dibbiny return mlx5_core_query_vendor_id(dev->mdev, vendor_id); 6981b5daf11SMajd Dibbiny 6991b5daf11SMajd Dibbiny default: 7001b5daf11SMajd Dibbiny return -EINVAL; 7011b5daf11SMajd Dibbiny } 7021b5daf11SMajd Dibbiny } 7031b5daf11SMajd Dibbiny 7041b5daf11SMajd Dibbiny static int mlx5_query_node_guid(struct mlx5_ib_dev *dev, 7051b5daf11SMajd Dibbiny __be64 *node_guid) 7061b5daf11SMajd Dibbiny { 7071b5daf11SMajd Dibbiny u64 tmp; 7081b5daf11SMajd Dibbiny int err; 7091b5daf11SMajd Dibbiny 7101b5daf11SMajd Dibbiny switch (mlx5_get_vport_access_method(&dev->ib_dev)) { 7111b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_MAD: 7121b5daf11SMajd Dibbiny return mlx5_query_mad_ifc_node_guid(dev, node_guid); 7131b5daf11SMajd Dibbiny 7141b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_HCA: 7151b5daf11SMajd Dibbiny err = mlx5_query_hca_vport_node_guid(dev->mdev, &tmp); 7163f89a643SAchiad Shochat break; 7173f89a643SAchiad Shochat 7183f89a643SAchiad Shochat case MLX5_VPORT_ACCESS_METHOD_NIC: 7193f89a643SAchiad Shochat err = mlx5_query_nic_vport_node_guid(dev->mdev, &tmp); 7203f89a643SAchiad Shochat break; 7211b5daf11SMajd Dibbiny 7221b5daf11SMajd Dibbiny default: 7231b5daf11SMajd Dibbiny return -EINVAL; 7241b5daf11SMajd Dibbiny } 7253f89a643SAchiad Shochat 7263f89a643SAchiad Shochat if (!err) 7273f89a643SAchiad Shochat *node_guid = cpu_to_be64(tmp); 7283f89a643SAchiad Shochat 7293f89a643SAchiad Shochat return err; 7301b5daf11SMajd Dibbiny } 7311b5daf11SMajd Dibbiny 7321b5daf11SMajd Dibbiny struct mlx5_reg_node_desc { 733bd99fdeaSYuval Shaia u8 desc[IB_DEVICE_NODE_DESC_MAX]; 7341b5daf11SMajd Dibbiny }; 7351b5daf11SMajd Dibbiny 7361b5daf11SMajd Dibbiny static int mlx5_query_node_desc(struct mlx5_ib_dev *dev, char *node_desc) 7371b5daf11SMajd Dibbiny { 7381b5daf11SMajd Dibbiny struct mlx5_reg_node_desc in; 7391b5daf11SMajd Dibbiny 7401b5daf11SMajd Dibbiny if (mlx5_use_mad_ifc(dev)) 7411b5daf11SMajd Dibbiny return mlx5_query_mad_ifc_node_desc(dev, node_desc); 7421b5daf11SMajd Dibbiny 7431b5daf11SMajd Dibbiny memset(&in, 0, sizeof(in)); 7441b5daf11SMajd Dibbiny 7451b5daf11SMajd Dibbiny return mlx5_core_access_reg(dev->mdev, &in, sizeof(in), node_desc, 7461b5daf11SMajd Dibbiny sizeof(struct mlx5_reg_node_desc), 7471b5daf11SMajd Dibbiny MLX5_REG_NODE_DESC, 0, 0); 7481b5daf11SMajd Dibbiny } 7491b5daf11SMajd Dibbiny 750e126ba97SEli Cohen static int mlx5_ib_query_device(struct ib_device *ibdev, 7512528e33eSMatan Barak struct ib_device_attr *props, 7522528e33eSMatan Barak struct ib_udata *uhw) 753e126ba97SEli Cohen { 754e126ba97SEli Cohen struct mlx5_ib_dev *dev = to_mdev(ibdev); 755938fe83cSSaeed Mahameed struct mlx5_core_dev *mdev = dev->mdev; 756e126ba97SEli Cohen int err = -ENOMEM; 757288c01b7SEli Cohen int max_sq_desc; 758e126ba97SEli Cohen int max_rq_sg; 759e126ba97SEli Cohen int max_sq_sg; 760e0238a6aSSagi Grimberg u64 min_page_size = 1ull << MLX5_CAP_GEN(mdev, log_pg_sz); 76185c7c014SDaniel Jurgens bool raw_support = !mlx5_core_mp_enabled(mdev); 762402ca536SBodong Wang struct mlx5_ib_query_device_resp resp = {}; 763402ca536SBodong Wang size_t resp_len; 764402ca536SBodong Wang u64 max_tso; 765e126ba97SEli Cohen 766402ca536SBodong Wang resp_len = sizeof(resp.comp_mask) + sizeof(resp.response_length); 767402ca536SBodong Wang if (uhw->outlen && uhw->outlen < resp_len) 768402ca536SBodong Wang return -EINVAL; 769402ca536SBodong Wang else 770402ca536SBodong Wang resp.response_length = resp_len; 771402ca536SBodong Wang 772402ca536SBodong Wang if (uhw->inlen && !ib_is_udata_cleared(uhw, 0, uhw->inlen)) 7732528e33eSMatan Barak return -EINVAL; 7742528e33eSMatan Barak 775e126ba97SEli Cohen memset(props, 0, sizeof(*props)); 7761b5daf11SMajd Dibbiny err = mlx5_query_system_image_guid(ibdev, 7771b5daf11SMajd Dibbiny &props->sys_image_guid); 7781b5daf11SMajd Dibbiny if (err) 7791b5daf11SMajd Dibbiny return err; 7801b5daf11SMajd Dibbiny 7811b5daf11SMajd Dibbiny err = mlx5_query_max_pkeys(ibdev, &props->max_pkeys); 7821b5daf11SMajd Dibbiny if (err) 7831b5daf11SMajd Dibbiny return err; 7841b5daf11SMajd Dibbiny 7851b5daf11SMajd Dibbiny err = mlx5_query_vendor_id(ibdev, &props->vendor_id); 7861b5daf11SMajd Dibbiny if (err) 7871b5daf11SMajd Dibbiny return err; 788e126ba97SEli Cohen 7899603b61dSJack Morgenstein props->fw_ver = ((u64)fw_rev_maj(dev->mdev) << 32) | 7909603b61dSJack Morgenstein (fw_rev_min(dev->mdev) << 16) | 7919603b61dSJack Morgenstein fw_rev_sub(dev->mdev); 792e126ba97SEli Cohen props->device_cap_flags = IB_DEVICE_CHANGE_PHY_PORT | 793e126ba97SEli Cohen IB_DEVICE_PORT_ACTIVE_EVENT | 794e126ba97SEli Cohen IB_DEVICE_SYS_IMAGE_GUID | 7951a4c3a3dSEli Cohen IB_DEVICE_RC_RNR_NAK_GEN; 796938fe83cSSaeed Mahameed 797938fe83cSSaeed Mahameed if (MLX5_CAP_GEN(mdev, pkv)) 798e126ba97SEli Cohen props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR; 799938fe83cSSaeed Mahameed if (MLX5_CAP_GEN(mdev, qkv)) 800e126ba97SEli Cohen props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR; 801938fe83cSSaeed Mahameed if (MLX5_CAP_GEN(mdev, apm)) 802e126ba97SEli Cohen props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG; 803938fe83cSSaeed Mahameed if (MLX5_CAP_GEN(mdev, xrc)) 804e126ba97SEli Cohen props->device_cap_flags |= IB_DEVICE_XRC; 805d2370e0aSMatan Barak if (MLX5_CAP_GEN(mdev, imaicl)) { 806d2370e0aSMatan Barak props->device_cap_flags |= IB_DEVICE_MEM_WINDOW | 807d2370e0aSMatan Barak IB_DEVICE_MEM_WINDOW_TYPE_2B; 808d2370e0aSMatan Barak props->max_mw = 1 << MLX5_CAP_GEN(mdev, log_max_mkey); 809b005d316SSagi Grimberg /* We support 'Gappy' memory registration too */ 810b005d316SSagi Grimberg props->device_cap_flags |= IB_DEVICE_SG_GAPS_REG; 811d2370e0aSMatan Barak } 812e126ba97SEli Cohen props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS; 813938fe83cSSaeed Mahameed if (MLX5_CAP_GEN(mdev, sho)) { 8142dea9094SSagi Grimberg props->device_cap_flags |= IB_DEVICE_SIGNATURE_HANDOVER; 8152dea9094SSagi Grimberg /* At this stage no support for signature handover */ 8162dea9094SSagi Grimberg props->sig_prot_cap = IB_PROT_T10DIF_TYPE_1 | 8172dea9094SSagi Grimberg IB_PROT_T10DIF_TYPE_2 | 8182dea9094SSagi Grimberg IB_PROT_T10DIF_TYPE_3; 8192dea9094SSagi Grimberg props->sig_guard_cap = IB_GUARD_T10DIF_CRC | 8202dea9094SSagi Grimberg IB_GUARD_T10DIF_CSUM; 8212dea9094SSagi Grimberg } 822938fe83cSSaeed Mahameed if (MLX5_CAP_GEN(mdev, block_lb_mc)) 823f360d88aSEli Cohen props->device_cap_flags |= IB_DEVICE_BLOCK_MULTICAST_LOOPBACK; 824e126ba97SEli Cohen 82585c7c014SDaniel Jurgens if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) && raw_support) { 826e8161334SNoa Osherovich if (MLX5_CAP_ETH(mdev, csum_cap)) { 827e8161334SNoa Osherovich /* Legacy bit to support old userspace libraries */ 82888115fe7SBodong Wang props->device_cap_flags |= IB_DEVICE_RAW_IP_CSUM; 829e8161334SNoa Osherovich props->raw_packet_caps |= IB_RAW_PACKET_CAP_IP_CSUM; 830e8161334SNoa Osherovich } 831e8161334SNoa Osherovich 832e8161334SNoa Osherovich if (MLX5_CAP_ETH(dev->mdev, vlan_cap)) 833e8161334SNoa Osherovich props->raw_packet_caps |= 834e8161334SNoa Osherovich IB_RAW_PACKET_CAP_CVLAN_STRIPPING; 83588115fe7SBodong Wang 836402ca536SBodong Wang if (field_avail(typeof(resp), tso_caps, uhw->outlen)) { 837402ca536SBodong Wang max_tso = MLX5_CAP_ETH(mdev, max_lso_cap); 838402ca536SBodong Wang if (max_tso) { 839402ca536SBodong Wang resp.tso_caps.max_tso = 1 << max_tso; 840402ca536SBodong Wang resp.tso_caps.supported_qpts |= 841402ca536SBodong Wang 1 << IB_QPT_RAW_PACKET; 842402ca536SBodong Wang resp.response_length += sizeof(resp.tso_caps); 843402ca536SBodong Wang } 844402ca536SBodong Wang } 84531f69a82SYishai Hadas 84631f69a82SYishai Hadas if (field_avail(typeof(resp), rss_caps, uhw->outlen)) { 84731f69a82SYishai Hadas resp.rss_caps.rx_hash_function = 84831f69a82SYishai Hadas MLX5_RX_HASH_FUNC_TOEPLITZ; 84931f69a82SYishai Hadas resp.rss_caps.rx_hash_fields_mask = 85031f69a82SYishai Hadas MLX5_RX_HASH_SRC_IPV4 | 85131f69a82SYishai Hadas MLX5_RX_HASH_DST_IPV4 | 85231f69a82SYishai Hadas MLX5_RX_HASH_SRC_IPV6 | 85331f69a82SYishai Hadas MLX5_RX_HASH_DST_IPV6 | 85431f69a82SYishai Hadas MLX5_RX_HASH_SRC_PORT_TCP | 85531f69a82SYishai Hadas MLX5_RX_HASH_DST_PORT_TCP | 85631f69a82SYishai Hadas MLX5_RX_HASH_SRC_PORT_UDP | 8574e2b53a5SMaor Gottlieb MLX5_RX_HASH_DST_PORT_UDP | 8584e2b53a5SMaor Gottlieb MLX5_RX_HASH_INNER; 8592d93fc85SMatan Barak if (mlx5_accel_ipsec_device_caps(dev->mdev) & 8602d93fc85SMatan Barak MLX5_ACCEL_IPSEC_CAP_DEVICE) 8612d93fc85SMatan Barak resp.rss_caps.rx_hash_fields_mask |= 8622d93fc85SMatan Barak MLX5_RX_HASH_IPSEC_SPI; 86331f69a82SYishai Hadas resp.response_length += sizeof(resp.rss_caps); 86431f69a82SYishai Hadas } 86531f69a82SYishai Hadas } else { 86631f69a82SYishai Hadas if (field_avail(typeof(resp), tso_caps, uhw->outlen)) 86731f69a82SYishai Hadas resp.response_length += sizeof(resp.tso_caps); 86831f69a82SYishai Hadas if (field_avail(typeof(resp), rss_caps, uhw->outlen)) 86931f69a82SYishai Hadas resp.response_length += sizeof(resp.rss_caps); 870402ca536SBodong Wang } 871402ca536SBodong Wang 872f0313965SErez Shitrit if (MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) { 873f0313965SErez Shitrit props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM; 874f0313965SErez Shitrit props->device_cap_flags |= IB_DEVICE_UD_TSO; 875f0313965SErez Shitrit } 876f0313965SErez Shitrit 87703404e8aSMaor Gottlieb if (MLX5_CAP_GEN(dev->mdev, rq_delay_drop) && 87885c7c014SDaniel Jurgens MLX5_CAP_GEN(dev->mdev, general_notification_event) && 87985c7c014SDaniel Jurgens raw_support) 88003404e8aSMaor Gottlieb props->raw_packet_caps |= IB_RAW_PACKET_CAP_DELAY_DROP; 88103404e8aSMaor Gottlieb 8821d54f890SYishai Hadas if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads) && 8831d54f890SYishai Hadas MLX5_CAP_IPOIB_ENHANCED(mdev, csum_cap)) 8841d54f890SYishai Hadas props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM; 8851d54f890SYishai Hadas 886cff5a0f3SMajd Dibbiny if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) && 88785c7c014SDaniel Jurgens MLX5_CAP_ETH(dev->mdev, scatter_fcs) && 88885c7c014SDaniel Jurgens raw_support) { 889e8161334SNoa Osherovich /* Legacy bit to support old userspace libraries */ 890cff5a0f3SMajd Dibbiny props->device_cap_flags |= IB_DEVICE_RAW_SCATTER_FCS; 891e8161334SNoa Osherovich props->raw_packet_caps |= IB_RAW_PACKET_CAP_SCATTER_FCS; 892e8161334SNoa Osherovich } 893cff5a0f3SMajd Dibbiny 89424da0016SAriel Levkovich if (MLX5_CAP_DEV_MEM(mdev, memic)) { 89524da0016SAriel Levkovich props->max_dm_size = 89624da0016SAriel Levkovich MLX5_CAP_DEV_MEM(mdev, max_memic_size); 89724da0016SAriel Levkovich } 89824da0016SAriel Levkovich 899da6d6ba3SMaor Gottlieb if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS)) 900da6d6ba3SMaor Gottlieb props->device_cap_flags |= IB_DEVICE_MANAGED_FLOW_STEERING; 901da6d6ba3SMaor Gottlieb 902b1383aa6SNoa Osherovich if (MLX5_CAP_GEN(mdev, end_pad)) 903b1383aa6SNoa Osherovich props->device_cap_flags |= IB_DEVICE_PCI_WRITE_END_PADDING; 904b1383aa6SNoa Osherovich 9051b5daf11SMajd Dibbiny props->vendor_part_id = mdev->pdev->device; 9061b5daf11SMajd Dibbiny props->hw_ver = mdev->pdev->revision; 907e126ba97SEli Cohen 908e126ba97SEli Cohen props->max_mr_size = ~0ull; 909e0238a6aSSagi Grimberg props->page_size_cap = ~(min_page_size - 1); 910938fe83cSSaeed Mahameed props->max_qp = 1 << MLX5_CAP_GEN(mdev, log_max_qp); 911938fe83cSSaeed Mahameed props->max_qp_wr = 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz); 912938fe83cSSaeed Mahameed max_rq_sg = MLX5_CAP_GEN(mdev, max_wqe_sz_rq) / 913938fe83cSSaeed Mahameed sizeof(struct mlx5_wqe_data_seg); 914288c01b7SEli Cohen max_sq_desc = min_t(int, MLX5_CAP_GEN(mdev, max_wqe_sz_sq), 512); 915288c01b7SEli Cohen max_sq_sg = (max_sq_desc - sizeof(struct mlx5_wqe_ctrl_seg) - 916288c01b7SEli Cohen sizeof(struct mlx5_wqe_raddr_seg)) / 917e126ba97SEli Cohen sizeof(struct mlx5_wqe_data_seg); 918e126ba97SEli Cohen props->max_sge = min(max_rq_sg, max_sq_sg); 919986ef95eSSagi Grimberg props->max_sge_rd = MLX5_MAX_SGE_RD; 920938fe83cSSaeed Mahameed props->max_cq = 1 << MLX5_CAP_GEN(mdev, log_max_cq); 9219f177686SLeon Romanovsky props->max_cqe = (1 << MLX5_CAP_GEN(mdev, log_max_cq_sz)) - 1; 922938fe83cSSaeed Mahameed props->max_mr = 1 << MLX5_CAP_GEN(mdev, log_max_mkey); 923938fe83cSSaeed Mahameed props->max_pd = 1 << MLX5_CAP_GEN(mdev, log_max_pd); 924938fe83cSSaeed Mahameed props->max_qp_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_req_qp); 925938fe83cSSaeed Mahameed props->max_qp_init_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_res_qp); 926938fe83cSSaeed Mahameed props->max_srq = 1 << MLX5_CAP_GEN(mdev, log_max_srq); 927938fe83cSSaeed Mahameed props->max_srq_wr = (1 << MLX5_CAP_GEN(mdev, log_max_srq_sz)) - 1; 928938fe83cSSaeed Mahameed props->local_ca_ack_delay = MLX5_CAP_GEN(mdev, local_ca_ack_delay); 929e126ba97SEli Cohen props->max_res_rd_atom = props->max_qp_rd_atom * props->max_qp; 930e126ba97SEli Cohen props->max_srq_sge = max_rq_sg - 1; 931911f4331SSagi Grimberg props->max_fast_reg_page_list_len = 932911f4331SSagi Grimberg 1 << MLX5_CAP_GEN(mdev, log_max_klm_list_size); 933776a3906SMoni Shoua get_atomic_caps_qp(dev, props); 93481bea28fSEli Cohen props->masked_atomic_cap = IB_ATOMIC_NONE; 935938fe83cSSaeed Mahameed props->max_mcast_grp = 1 << MLX5_CAP_GEN(mdev, log_max_mcg); 936938fe83cSSaeed Mahameed props->max_mcast_qp_attach = MLX5_CAP_GEN(mdev, max_qp_mcg); 937e126ba97SEli Cohen props->max_total_mcast_qp_attach = props->max_mcast_qp_attach * 938e126ba97SEli Cohen props->max_mcast_grp; 939e126ba97SEli Cohen props->max_map_per_fmr = INT_MAX; /* no limit in ConnectIB */ 94086695a65SMaor Gottlieb props->max_ah = INT_MAX; 9417c60bcbbSMatan Barak props->hca_core_clock = MLX5_CAP_GEN(mdev, device_frequency_khz); 9427c60bcbbSMatan Barak props->timestamp_mask = 0x7FFFFFFFFFFFFFFFULL; 943e126ba97SEli Cohen 9448cdd312cSHaggai Eran #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING 945938fe83cSSaeed Mahameed if (MLX5_CAP_GEN(mdev, pg)) 9468cdd312cSHaggai Eran props->device_cap_flags |= IB_DEVICE_ON_DEMAND_PAGING; 9478cdd312cSHaggai Eran props->odp_caps = dev->odp_caps; 9488cdd312cSHaggai Eran #endif 9498cdd312cSHaggai Eran 950051f2630SLeon Romanovsky if (MLX5_CAP_GEN(mdev, cd)) 951051f2630SLeon Romanovsky props->device_cap_flags |= IB_DEVICE_CROSS_CHANNEL; 952051f2630SLeon Romanovsky 953eff901d3SEli Cohen if (!mlx5_core_is_pf(mdev)) 954eff901d3SEli Cohen props->device_cap_flags |= IB_DEVICE_VIRTUAL_FUNCTION; 955eff901d3SEli Cohen 95631f69a82SYishai Hadas if (mlx5_ib_port_link_layer(ibdev, 1) == 95785c7c014SDaniel Jurgens IB_LINK_LAYER_ETHERNET && raw_support) { 95831f69a82SYishai Hadas props->rss_caps.max_rwq_indirection_tables = 95931f69a82SYishai Hadas 1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt); 96031f69a82SYishai Hadas props->rss_caps.max_rwq_indirection_table_size = 96131f69a82SYishai Hadas 1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt_size); 96231f69a82SYishai Hadas props->rss_caps.supported_qpts = 1 << IB_QPT_RAW_PACKET; 96331f69a82SYishai Hadas props->max_wq_type_rq = 96431f69a82SYishai Hadas 1 << MLX5_CAP_GEN(dev->mdev, log_max_rq); 96531f69a82SYishai Hadas } 96631f69a82SYishai Hadas 967eb761894SArtemy Kovalyov if (MLX5_CAP_GEN(mdev, tag_matching)) { 96878b1beb0SLeon Romanovsky props->tm_caps.max_rndv_hdr_size = MLX5_TM_MAX_RNDV_MSG_SIZE; 96978b1beb0SLeon Romanovsky props->tm_caps.max_num_tags = 970eb761894SArtemy Kovalyov (1 << MLX5_CAP_GEN(mdev, log_tag_matching_list_sz)) - 1; 97178b1beb0SLeon Romanovsky props->tm_caps.flags = IB_TM_CAP_RC; 97278b1beb0SLeon Romanovsky props->tm_caps.max_ops = 973eb761894SArtemy Kovalyov 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz); 97478b1beb0SLeon Romanovsky props->tm_caps.max_sge = MLX5_TM_MAX_SGE; 975eb761894SArtemy Kovalyov } 976eb761894SArtemy Kovalyov 97787ab3f52SYonatan Cohen if (MLX5_CAP_GEN(dev->mdev, cq_moderation)) { 97887ab3f52SYonatan Cohen props->cq_caps.max_cq_moderation_count = 97987ab3f52SYonatan Cohen MLX5_MAX_CQ_COUNT; 98087ab3f52SYonatan Cohen props->cq_caps.max_cq_moderation_period = 98187ab3f52SYonatan Cohen MLX5_MAX_CQ_PERIOD; 98287ab3f52SYonatan Cohen } 98387ab3f52SYonatan Cohen 9847e43a2a5SBodong Wang if (field_avail(typeof(resp), cqe_comp_caps, uhw->outlen)) { 9857e43a2a5SBodong Wang resp.cqe_comp_caps.max_num = 9867e43a2a5SBodong Wang MLX5_CAP_GEN(dev->mdev, cqe_compression) ? 9877e43a2a5SBodong Wang MLX5_CAP_GEN(dev->mdev, cqe_compression_max_num) : 0; 9887e43a2a5SBodong Wang resp.cqe_comp_caps.supported_format = 9897e43a2a5SBodong Wang MLX5_IB_CQE_RES_FORMAT_HASH | 9907e43a2a5SBodong Wang MLX5_IB_CQE_RES_FORMAT_CSUM; 9917e43a2a5SBodong Wang resp.response_length += sizeof(resp.cqe_comp_caps); 9927e43a2a5SBodong Wang } 9937e43a2a5SBodong Wang 99485c7c014SDaniel Jurgens if (field_avail(typeof(resp), packet_pacing_caps, uhw->outlen) && 99585c7c014SDaniel Jurgens raw_support) { 996d949167dSBodong Wang if (MLX5_CAP_QOS(mdev, packet_pacing) && 997d949167dSBodong Wang MLX5_CAP_GEN(mdev, qos)) { 998d949167dSBodong Wang resp.packet_pacing_caps.qp_rate_limit_max = 999d949167dSBodong Wang MLX5_CAP_QOS(mdev, packet_pacing_max_rate); 1000d949167dSBodong Wang resp.packet_pacing_caps.qp_rate_limit_min = 1001d949167dSBodong Wang MLX5_CAP_QOS(mdev, packet_pacing_min_rate); 1002d949167dSBodong Wang resp.packet_pacing_caps.supported_qpts |= 1003d949167dSBodong Wang 1 << IB_QPT_RAW_PACKET; 100461147f39SBodong Wang if (MLX5_CAP_QOS(mdev, packet_pacing_burst_bound) && 100561147f39SBodong Wang MLX5_CAP_QOS(mdev, packet_pacing_typical_size)) 100661147f39SBodong Wang resp.packet_pacing_caps.cap_flags |= 100761147f39SBodong Wang MLX5_IB_PP_SUPPORT_BURST; 1008d949167dSBodong Wang } 1009d949167dSBodong Wang resp.response_length += sizeof(resp.packet_pacing_caps); 1010d949167dSBodong Wang } 1011d949167dSBodong Wang 10129f885201SLeon Romanovsky if (field_avail(typeof(resp), mlx5_ib_support_multi_pkt_send_wqes, 10139f885201SLeon Romanovsky uhw->outlen)) { 1014795b609cSBodong Wang if (MLX5_CAP_ETH(mdev, multi_pkt_send_wqe)) 10159f885201SLeon Romanovsky resp.mlx5_ib_support_multi_pkt_send_wqes = 1016795b609cSBodong Wang MLX5_IB_ALLOW_MPW; 1017050da902SBodong Wang 1018050da902SBodong Wang if (MLX5_CAP_ETH(mdev, enhanced_multi_pkt_send_wqe)) 1019050da902SBodong Wang resp.mlx5_ib_support_multi_pkt_send_wqes |= 1020050da902SBodong Wang MLX5_IB_SUPPORT_EMPW; 1021050da902SBodong Wang 10229f885201SLeon Romanovsky resp.response_length += 10239f885201SLeon Romanovsky sizeof(resp.mlx5_ib_support_multi_pkt_send_wqes); 10249f885201SLeon Romanovsky } 10259f885201SLeon Romanovsky 1026de57f2adSGuy Levi if (field_avail(typeof(resp), flags, uhw->outlen)) { 1027de57f2adSGuy Levi resp.response_length += sizeof(resp.flags); 10287a0c8f42SGuy Levi 1029de57f2adSGuy Levi if (MLX5_CAP_GEN(mdev, cqe_compression_128)) 1030de57f2adSGuy Levi resp.flags |= 1031de57f2adSGuy Levi MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_COMP; 10327a0c8f42SGuy Levi 10337a0c8f42SGuy Levi if (MLX5_CAP_GEN(mdev, cqe_128_always)) 10347a0c8f42SGuy Levi resp.flags |= MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_PAD; 1035de57f2adSGuy Levi } 10369f885201SLeon Romanovsky 103796dc3fc5SNoa Osherovich if (field_avail(typeof(resp), sw_parsing_caps, 103896dc3fc5SNoa Osherovich uhw->outlen)) { 103996dc3fc5SNoa Osherovich resp.response_length += sizeof(resp.sw_parsing_caps); 104096dc3fc5SNoa Osherovich if (MLX5_CAP_ETH(mdev, swp)) { 104196dc3fc5SNoa Osherovich resp.sw_parsing_caps.sw_parsing_offloads |= 104296dc3fc5SNoa Osherovich MLX5_IB_SW_PARSING; 104396dc3fc5SNoa Osherovich 104496dc3fc5SNoa Osherovich if (MLX5_CAP_ETH(mdev, swp_csum)) 104596dc3fc5SNoa Osherovich resp.sw_parsing_caps.sw_parsing_offloads |= 104696dc3fc5SNoa Osherovich MLX5_IB_SW_PARSING_CSUM; 104796dc3fc5SNoa Osherovich 104896dc3fc5SNoa Osherovich if (MLX5_CAP_ETH(mdev, swp_lso)) 104996dc3fc5SNoa Osherovich resp.sw_parsing_caps.sw_parsing_offloads |= 105096dc3fc5SNoa Osherovich MLX5_IB_SW_PARSING_LSO; 105196dc3fc5SNoa Osherovich 105296dc3fc5SNoa Osherovich if (resp.sw_parsing_caps.sw_parsing_offloads) 105396dc3fc5SNoa Osherovich resp.sw_parsing_caps.supported_qpts = 105496dc3fc5SNoa Osherovich BIT(IB_QPT_RAW_PACKET); 105596dc3fc5SNoa Osherovich } 105696dc3fc5SNoa Osherovich } 105796dc3fc5SNoa Osherovich 105885c7c014SDaniel Jurgens if (field_avail(typeof(resp), striding_rq_caps, uhw->outlen) && 105985c7c014SDaniel Jurgens raw_support) { 1060b4f34597SNoa Osherovich resp.response_length += sizeof(resp.striding_rq_caps); 1061b4f34597SNoa Osherovich if (MLX5_CAP_GEN(mdev, striding_rq)) { 1062b4f34597SNoa Osherovich resp.striding_rq_caps.min_single_stride_log_num_of_bytes = 1063b4f34597SNoa Osherovich MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES; 1064b4f34597SNoa Osherovich resp.striding_rq_caps.max_single_stride_log_num_of_bytes = 1065b4f34597SNoa Osherovich MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES; 1066b4f34597SNoa Osherovich resp.striding_rq_caps.min_single_wqe_log_num_of_strides = 1067b4f34597SNoa Osherovich MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES; 1068b4f34597SNoa Osherovich resp.striding_rq_caps.max_single_wqe_log_num_of_strides = 1069b4f34597SNoa Osherovich MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES; 1070b4f34597SNoa Osherovich resp.striding_rq_caps.supported_qpts = 1071b4f34597SNoa Osherovich BIT(IB_QPT_RAW_PACKET); 1072b4f34597SNoa Osherovich } 1073b4f34597SNoa Osherovich } 1074b4f34597SNoa Osherovich 1075f95ef6cbSMaor Gottlieb if (field_avail(typeof(resp), tunnel_offloads_caps, 1076f95ef6cbSMaor Gottlieb uhw->outlen)) { 1077f95ef6cbSMaor Gottlieb resp.response_length += sizeof(resp.tunnel_offloads_caps); 1078f95ef6cbSMaor Gottlieb if (MLX5_CAP_ETH(mdev, tunnel_stateless_vxlan)) 1079f95ef6cbSMaor Gottlieb resp.tunnel_offloads_caps |= 1080f95ef6cbSMaor Gottlieb MLX5_IB_TUNNELED_OFFLOADS_VXLAN; 1081f95ef6cbSMaor Gottlieb if (MLX5_CAP_ETH(mdev, tunnel_stateless_geneve_rx)) 1082f95ef6cbSMaor Gottlieb resp.tunnel_offloads_caps |= 1083f95ef6cbSMaor Gottlieb MLX5_IB_TUNNELED_OFFLOADS_GENEVE; 1084f95ef6cbSMaor Gottlieb if (MLX5_CAP_ETH(mdev, tunnel_stateless_gre)) 1085f95ef6cbSMaor Gottlieb resp.tunnel_offloads_caps |= 1086f95ef6cbSMaor Gottlieb MLX5_IB_TUNNELED_OFFLOADS_GRE; 1087e818e255SAriel Levkovich if (MLX5_CAP_GEN(mdev, flex_parser_protocols) & 1088e818e255SAriel Levkovich MLX5_FLEX_PROTO_CW_MPLS_GRE) 1089e818e255SAriel Levkovich resp.tunnel_offloads_caps |= 1090e818e255SAriel Levkovich MLX5_IB_TUNNELED_OFFLOADS_MPLS_GRE; 1091e818e255SAriel Levkovich if (MLX5_CAP_GEN(mdev, flex_parser_protocols) & 1092e818e255SAriel Levkovich MLX5_FLEX_PROTO_CW_MPLS_UDP) 1093e818e255SAriel Levkovich resp.tunnel_offloads_caps |= 1094e818e255SAriel Levkovich MLX5_IB_TUNNELED_OFFLOADS_MPLS_UDP; 1095f95ef6cbSMaor Gottlieb } 1096f95ef6cbSMaor Gottlieb 1097402ca536SBodong Wang if (uhw->outlen) { 1098402ca536SBodong Wang err = ib_copy_to_udata(uhw, &resp, resp.response_length); 1099402ca536SBodong Wang 1100402ca536SBodong Wang if (err) 1101402ca536SBodong Wang return err; 1102402ca536SBodong Wang } 1103402ca536SBodong Wang 11041b5daf11SMajd Dibbiny return 0; 11051b5daf11SMajd Dibbiny } 1106e126ba97SEli Cohen 11071b5daf11SMajd Dibbiny enum mlx5_ib_width { 11081b5daf11SMajd Dibbiny MLX5_IB_WIDTH_1X = 1 << 0, 11091b5daf11SMajd Dibbiny MLX5_IB_WIDTH_2X = 1 << 1, 11101b5daf11SMajd Dibbiny MLX5_IB_WIDTH_4X = 1 << 2, 11111b5daf11SMajd Dibbiny MLX5_IB_WIDTH_8X = 1 << 3, 11121b5daf11SMajd Dibbiny MLX5_IB_WIDTH_12X = 1 << 4 11131b5daf11SMajd Dibbiny }; 11141b5daf11SMajd Dibbiny 11151b5daf11SMajd Dibbiny static int translate_active_width(struct ib_device *ibdev, u8 active_width, 11161b5daf11SMajd Dibbiny u8 *ib_width) 11171b5daf11SMajd Dibbiny { 11181b5daf11SMajd Dibbiny struct mlx5_ib_dev *dev = to_mdev(ibdev); 11191b5daf11SMajd Dibbiny int err = 0; 11201b5daf11SMajd Dibbiny 11211b5daf11SMajd Dibbiny if (active_width & MLX5_IB_WIDTH_1X) { 11221b5daf11SMajd Dibbiny *ib_width = IB_WIDTH_1X; 11231b5daf11SMajd Dibbiny } else if (active_width & MLX5_IB_WIDTH_2X) { 11241b5daf11SMajd Dibbiny mlx5_ib_dbg(dev, "active_width %d is not supported by IB spec\n", 11251b5daf11SMajd Dibbiny (int)active_width); 11261b5daf11SMajd Dibbiny err = -EINVAL; 11271b5daf11SMajd Dibbiny } else if (active_width & MLX5_IB_WIDTH_4X) { 11281b5daf11SMajd Dibbiny *ib_width = IB_WIDTH_4X; 11291b5daf11SMajd Dibbiny } else if (active_width & MLX5_IB_WIDTH_8X) { 11301b5daf11SMajd Dibbiny *ib_width = IB_WIDTH_8X; 11311b5daf11SMajd Dibbiny } else if (active_width & MLX5_IB_WIDTH_12X) { 11321b5daf11SMajd Dibbiny *ib_width = IB_WIDTH_12X; 11331b5daf11SMajd Dibbiny } else { 11341b5daf11SMajd Dibbiny mlx5_ib_dbg(dev, "Invalid active_width %d\n", 11351b5daf11SMajd Dibbiny (int)active_width); 11361b5daf11SMajd Dibbiny err = -EINVAL; 11371b5daf11SMajd Dibbiny } 11381b5daf11SMajd Dibbiny 11391b5daf11SMajd Dibbiny return err; 11401b5daf11SMajd Dibbiny } 11411b5daf11SMajd Dibbiny 11421b5daf11SMajd Dibbiny static int mlx5_mtu_to_ib_mtu(int mtu) 11431b5daf11SMajd Dibbiny { 11441b5daf11SMajd Dibbiny switch (mtu) { 11451b5daf11SMajd Dibbiny case 256: return 1; 11461b5daf11SMajd Dibbiny case 512: return 2; 11471b5daf11SMajd Dibbiny case 1024: return 3; 11481b5daf11SMajd Dibbiny case 2048: return 4; 11491b5daf11SMajd Dibbiny case 4096: return 5; 11501b5daf11SMajd Dibbiny default: 11511b5daf11SMajd Dibbiny pr_warn("invalid mtu\n"); 11521b5daf11SMajd Dibbiny return -1; 11531b5daf11SMajd Dibbiny } 11541b5daf11SMajd Dibbiny } 11551b5daf11SMajd Dibbiny 11561b5daf11SMajd Dibbiny enum ib_max_vl_num { 11571b5daf11SMajd Dibbiny __IB_MAX_VL_0 = 1, 11581b5daf11SMajd Dibbiny __IB_MAX_VL_0_1 = 2, 11591b5daf11SMajd Dibbiny __IB_MAX_VL_0_3 = 3, 11601b5daf11SMajd Dibbiny __IB_MAX_VL_0_7 = 4, 11611b5daf11SMajd Dibbiny __IB_MAX_VL_0_14 = 5, 11621b5daf11SMajd Dibbiny }; 11631b5daf11SMajd Dibbiny 11641b5daf11SMajd Dibbiny enum mlx5_vl_hw_cap { 11651b5daf11SMajd Dibbiny MLX5_VL_HW_0 = 1, 11661b5daf11SMajd Dibbiny MLX5_VL_HW_0_1 = 2, 11671b5daf11SMajd Dibbiny MLX5_VL_HW_0_2 = 3, 11681b5daf11SMajd Dibbiny MLX5_VL_HW_0_3 = 4, 11691b5daf11SMajd Dibbiny MLX5_VL_HW_0_4 = 5, 11701b5daf11SMajd Dibbiny MLX5_VL_HW_0_5 = 6, 11711b5daf11SMajd Dibbiny MLX5_VL_HW_0_6 = 7, 11721b5daf11SMajd Dibbiny MLX5_VL_HW_0_7 = 8, 11731b5daf11SMajd Dibbiny MLX5_VL_HW_0_14 = 15 11741b5daf11SMajd Dibbiny }; 11751b5daf11SMajd Dibbiny 11761b5daf11SMajd Dibbiny static int translate_max_vl_num(struct ib_device *ibdev, u8 vl_hw_cap, 11771b5daf11SMajd Dibbiny u8 *max_vl_num) 11781b5daf11SMajd Dibbiny { 11791b5daf11SMajd Dibbiny switch (vl_hw_cap) { 11801b5daf11SMajd Dibbiny case MLX5_VL_HW_0: 11811b5daf11SMajd Dibbiny *max_vl_num = __IB_MAX_VL_0; 11821b5daf11SMajd Dibbiny break; 11831b5daf11SMajd Dibbiny case MLX5_VL_HW_0_1: 11841b5daf11SMajd Dibbiny *max_vl_num = __IB_MAX_VL_0_1; 11851b5daf11SMajd Dibbiny break; 11861b5daf11SMajd Dibbiny case MLX5_VL_HW_0_3: 11871b5daf11SMajd Dibbiny *max_vl_num = __IB_MAX_VL_0_3; 11881b5daf11SMajd Dibbiny break; 11891b5daf11SMajd Dibbiny case MLX5_VL_HW_0_7: 11901b5daf11SMajd Dibbiny *max_vl_num = __IB_MAX_VL_0_7; 11911b5daf11SMajd Dibbiny break; 11921b5daf11SMajd Dibbiny case MLX5_VL_HW_0_14: 11931b5daf11SMajd Dibbiny *max_vl_num = __IB_MAX_VL_0_14; 11941b5daf11SMajd Dibbiny break; 11951b5daf11SMajd Dibbiny 11961b5daf11SMajd Dibbiny default: 11971b5daf11SMajd Dibbiny return -EINVAL; 11981b5daf11SMajd Dibbiny } 11991b5daf11SMajd Dibbiny 12001b5daf11SMajd Dibbiny return 0; 12011b5daf11SMajd Dibbiny } 12021b5daf11SMajd Dibbiny 12031b5daf11SMajd Dibbiny static int mlx5_query_hca_port(struct ib_device *ibdev, u8 port, 12041b5daf11SMajd Dibbiny struct ib_port_attr *props) 12051b5daf11SMajd Dibbiny { 12061b5daf11SMajd Dibbiny struct mlx5_ib_dev *dev = to_mdev(ibdev); 12071b5daf11SMajd Dibbiny struct mlx5_core_dev *mdev = dev->mdev; 12081b5daf11SMajd Dibbiny struct mlx5_hca_vport_context *rep; 1209046339eaSSaeed Mahameed u16 max_mtu; 1210046339eaSSaeed Mahameed u16 oper_mtu; 12111b5daf11SMajd Dibbiny int err; 12121b5daf11SMajd Dibbiny u8 ib_link_width_oper; 12131b5daf11SMajd Dibbiny u8 vl_hw_cap; 12141b5daf11SMajd Dibbiny 12151b5daf11SMajd Dibbiny rep = kzalloc(sizeof(*rep), GFP_KERNEL); 12161b5daf11SMajd Dibbiny if (!rep) { 12171b5daf11SMajd Dibbiny err = -ENOMEM; 12181b5daf11SMajd Dibbiny goto out; 12191b5daf11SMajd Dibbiny } 12201b5daf11SMajd Dibbiny 1221c4550c63SOr Gerlitz /* props being zeroed by the caller, avoid zeroing it here */ 12221b5daf11SMajd Dibbiny 12231b5daf11SMajd Dibbiny err = mlx5_query_hca_vport_context(mdev, 0, port, 0, rep); 12241b5daf11SMajd Dibbiny if (err) 12251b5daf11SMajd Dibbiny goto out; 12261b5daf11SMajd Dibbiny 12271b5daf11SMajd Dibbiny props->lid = rep->lid; 12281b5daf11SMajd Dibbiny props->lmc = rep->lmc; 12291b5daf11SMajd Dibbiny props->sm_lid = rep->sm_lid; 12301b5daf11SMajd Dibbiny props->sm_sl = rep->sm_sl; 12311b5daf11SMajd Dibbiny props->state = rep->vport_state; 12321b5daf11SMajd Dibbiny props->phys_state = rep->port_physical_state; 12331b5daf11SMajd Dibbiny props->port_cap_flags = rep->cap_mask1; 12341b5daf11SMajd Dibbiny props->gid_tbl_len = mlx5_get_gid_table_len(MLX5_CAP_GEN(mdev, gid_table_size)); 12351b5daf11SMajd Dibbiny props->max_msg_sz = 1 << MLX5_CAP_GEN(mdev, log_max_msg); 12361b5daf11SMajd Dibbiny props->pkey_tbl_len = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, pkey_table_size)); 12371b5daf11SMajd Dibbiny props->bad_pkey_cntr = rep->pkey_violation_counter; 12381b5daf11SMajd Dibbiny props->qkey_viol_cntr = rep->qkey_violation_counter; 12391b5daf11SMajd Dibbiny props->subnet_timeout = rep->subnet_timeout; 12401b5daf11SMajd Dibbiny props->init_type_reply = rep->init_type_reply; 1241eff901d3SEli Cohen props->grh_required = rep->grh_required; 12421b5daf11SMajd Dibbiny 12431b5daf11SMajd Dibbiny err = mlx5_query_port_link_width_oper(mdev, &ib_link_width_oper, port); 12441b5daf11SMajd Dibbiny if (err) 12451b5daf11SMajd Dibbiny goto out; 12461b5daf11SMajd Dibbiny 12471b5daf11SMajd Dibbiny err = translate_active_width(ibdev, ib_link_width_oper, 12481b5daf11SMajd Dibbiny &props->active_width); 12491b5daf11SMajd Dibbiny if (err) 12501b5daf11SMajd Dibbiny goto out; 1251d5beb7f2SNoa Osherovich err = mlx5_query_port_ib_proto_oper(mdev, &props->active_speed, port); 12521b5daf11SMajd Dibbiny if (err) 12531b5daf11SMajd Dibbiny goto out; 12541b5daf11SMajd Dibbiny 1255facc9699SSaeed Mahameed mlx5_query_port_max_mtu(mdev, &max_mtu, port); 12561b5daf11SMajd Dibbiny 12571b5daf11SMajd Dibbiny props->max_mtu = mlx5_mtu_to_ib_mtu(max_mtu); 12581b5daf11SMajd Dibbiny 1259facc9699SSaeed Mahameed mlx5_query_port_oper_mtu(mdev, &oper_mtu, port); 12601b5daf11SMajd Dibbiny 12611b5daf11SMajd Dibbiny props->active_mtu = mlx5_mtu_to_ib_mtu(oper_mtu); 12621b5daf11SMajd Dibbiny 12631b5daf11SMajd Dibbiny err = mlx5_query_port_vl_hw_cap(mdev, &vl_hw_cap, port); 12641b5daf11SMajd Dibbiny if (err) 12651b5daf11SMajd Dibbiny goto out; 12661b5daf11SMajd Dibbiny 12671b5daf11SMajd Dibbiny err = translate_max_vl_num(ibdev, vl_hw_cap, 12681b5daf11SMajd Dibbiny &props->max_vl_num); 12691b5daf11SMajd Dibbiny out: 12701b5daf11SMajd Dibbiny kfree(rep); 1271e126ba97SEli Cohen return err; 1272e126ba97SEli Cohen } 1273e126ba97SEli Cohen 1274e126ba97SEli Cohen int mlx5_ib_query_port(struct ib_device *ibdev, u8 port, 1275e126ba97SEli Cohen struct ib_port_attr *props) 1276e126ba97SEli Cohen { 1277095b0927SIlan Tayari unsigned int count; 1278095b0927SIlan Tayari int ret; 1279095b0927SIlan Tayari 12801b5daf11SMajd Dibbiny switch (mlx5_get_vport_access_method(ibdev)) { 12811b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_MAD: 1282095b0927SIlan Tayari ret = mlx5_query_mad_ifc_port(ibdev, port, props); 1283095b0927SIlan Tayari break; 1284e126ba97SEli Cohen 12851b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_HCA: 1286095b0927SIlan Tayari ret = mlx5_query_hca_port(ibdev, port, props); 1287095b0927SIlan Tayari break; 12881b5daf11SMajd Dibbiny 12893f89a643SAchiad Shochat case MLX5_VPORT_ACCESS_METHOD_NIC: 1290095b0927SIlan Tayari ret = mlx5_query_port_roce(ibdev, port, props); 1291095b0927SIlan Tayari break; 12923f89a643SAchiad Shochat 12931b5daf11SMajd Dibbiny default: 1294095b0927SIlan Tayari ret = -EINVAL; 1295e126ba97SEli Cohen } 1296095b0927SIlan Tayari 1297095b0927SIlan Tayari if (!ret && props) { 1298b3cbd6f0SDaniel Jurgens struct mlx5_ib_dev *dev = to_mdev(ibdev); 1299b3cbd6f0SDaniel Jurgens struct mlx5_core_dev *mdev; 1300b3cbd6f0SDaniel Jurgens bool put_mdev = true; 1301b3cbd6f0SDaniel Jurgens 1302b3cbd6f0SDaniel Jurgens mdev = mlx5_ib_get_native_port_mdev(dev, port, NULL); 1303b3cbd6f0SDaniel Jurgens if (!mdev) { 1304b3cbd6f0SDaniel Jurgens /* If the port isn't affiliated yet query the master. 1305b3cbd6f0SDaniel Jurgens * The master and slave will have the same values. 1306b3cbd6f0SDaniel Jurgens */ 1307b3cbd6f0SDaniel Jurgens mdev = dev->mdev; 1308b3cbd6f0SDaniel Jurgens port = 1; 1309b3cbd6f0SDaniel Jurgens put_mdev = false; 1310b3cbd6f0SDaniel Jurgens } 1311b3cbd6f0SDaniel Jurgens count = mlx5_core_reserved_gids_count(mdev); 1312b3cbd6f0SDaniel Jurgens if (put_mdev) 1313b3cbd6f0SDaniel Jurgens mlx5_ib_put_native_port_mdev(dev, port); 1314095b0927SIlan Tayari props->gid_tbl_len -= count; 1315095b0927SIlan Tayari } 1316095b0927SIlan Tayari return ret; 1317e126ba97SEli Cohen } 1318e126ba97SEli Cohen 13198e6efa3aSMark Bloch static int mlx5_ib_rep_query_port(struct ib_device *ibdev, u8 port, 13208e6efa3aSMark Bloch struct ib_port_attr *props) 13218e6efa3aSMark Bloch { 13228e6efa3aSMark Bloch int ret; 13238e6efa3aSMark Bloch 13248e6efa3aSMark Bloch /* Only link layer == ethernet is valid for representors */ 13258e6efa3aSMark Bloch ret = mlx5_query_port_roce(ibdev, port, props); 13268e6efa3aSMark Bloch if (ret || !props) 13278e6efa3aSMark Bloch return ret; 13288e6efa3aSMark Bloch 13298e6efa3aSMark Bloch /* We don't support GIDS */ 13308e6efa3aSMark Bloch props->gid_tbl_len = 0; 13318e6efa3aSMark Bloch 13328e6efa3aSMark Bloch return ret; 13338e6efa3aSMark Bloch } 13348e6efa3aSMark Bloch 1335e126ba97SEli Cohen static int mlx5_ib_query_gid(struct ib_device *ibdev, u8 port, int index, 1336e126ba97SEli Cohen union ib_gid *gid) 1337e126ba97SEli Cohen { 13381b5daf11SMajd Dibbiny struct mlx5_ib_dev *dev = to_mdev(ibdev); 13391b5daf11SMajd Dibbiny struct mlx5_core_dev *mdev = dev->mdev; 1340e126ba97SEli Cohen 13411b5daf11SMajd Dibbiny switch (mlx5_get_vport_access_method(ibdev)) { 13421b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_MAD: 13431b5daf11SMajd Dibbiny return mlx5_query_mad_ifc_gids(ibdev, port, index, gid); 1344e126ba97SEli Cohen 13451b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_HCA: 13461b5daf11SMajd Dibbiny return mlx5_query_hca_vport_gid(mdev, 0, port, 0, index, gid); 1347e126ba97SEli Cohen 13481b5daf11SMajd Dibbiny default: 13491b5daf11SMajd Dibbiny return -EINVAL; 13501b5daf11SMajd Dibbiny } 1351e126ba97SEli Cohen 1352e126ba97SEli Cohen } 1353e126ba97SEli Cohen 1354b3cbd6f0SDaniel Jurgens static int mlx5_query_hca_nic_pkey(struct ib_device *ibdev, u8 port, 1355b3cbd6f0SDaniel Jurgens u16 index, u16 *pkey) 1356b3cbd6f0SDaniel Jurgens { 1357b3cbd6f0SDaniel Jurgens struct mlx5_ib_dev *dev = to_mdev(ibdev); 1358b3cbd6f0SDaniel Jurgens struct mlx5_core_dev *mdev; 1359b3cbd6f0SDaniel Jurgens bool put_mdev = true; 1360b3cbd6f0SDaniel Jurgens u8 mdev_port_num; 1361b3cbd6f0SDaniel Jurgens int err; 1362b3cbd6f0SDaniel Jurgens 1363b3cbd6f0SDaniel Jurgens mdev = mlx5_ib_get_native_port_mdev(dev, port, &mdev_port_num); 1364b3cbd6f0SDaniel Jurgens if (!mdev) { 1365b3cbd6f0SDaniel Jurgens /* The port isn't affiliated yet, get the PKey from the master 1366b3cbd6f0SDaniel Jurgens * port. For RoCE the PKey tables will be the same. 1367b3cbd6f0SDaniel Jurgens */ 1368b3cbd6f0SDaniel Jurgens put_mdev = false; 1369b3cbd6f0SDaniel Jurgens mdev = dev->mdev; 1370b3cbd6f0SDaniel Jurgens mdev_port_num = 1; 1371b3cbd6f0SDaniel Jurgens } 1372b3cbd6f0SDaniel Jurgens 1373b3cbd6f0SDaniel Jurgens err = mlx5_query_hca_vport_pkey(mdev, 0, mdev_port_num, 0, 1374b3cbd6f0SDaniel Jurgens index, pkey); 1375b3cbd6f0SDaniel Jurgens if (put_mdev) 1376b3cbd6f0SDaniel Jurgens mlx5_ib_put_native_port_mdev(dev, port); 1377b3cbd6f0SDaniel Jurgens 1378b3cbd6f0SDaniel Jurgens return err; 1379b3cbd6f0SDaniel Jurgens } 1380b3cbd6f0SDaniel Jurgens 1381e126ba97SEli Cohen static int mlx5_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index, 1382e126ba97SEli Cohen u16 *pkey) 1383e126ba97SEli Cohen { 13841b5daf11SMajd Dibbiny switch (mlx5_get_vport_access_method(ibdev)) { 13851b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_MAD: 13861b5daf11SMajd Dibbiny return mlx5_query_mad_ifc_pkey(ibdev, port, index, pkey); 1387e126ba97SEli Cohen 13881b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_HCA: 13891b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_NIC: 1390b3cbd6f0SDaniel Jurgens return mlx5_query_hca_nic_pkey(ibdev, port, index, pkey); 13911b5daf11SMajd Dibbiny default: 13921b5daf11SMajd Dibbiny return -EINVAL; 1393e126ba97SEli Cohen } 13941b5daf11SMajd Dibbiny } 1395e126ba97SEli Cohen 1396e126ba97SEli Cohen static int mlx5_ib_modify_device(struct ib_device *ibdev, int mask, 1397e126ba97SEli Cohen struct ib_device_modify *props) 1398e126ba97SEli Cohen { 1399e126ba97SEli Cohen struct mlx5_ib_dev *dev = to_mdev(ibdev); 1400e126ba97SEli Cohen struct mlx5_reg_node_desc in; 1401e126ba97SEli Cohen struct mlx5_reg_node_desc out; 1402e126ba97SEli Cohen int err; 1403e126ba97SEli Cohen 1404e126ba97SEli Cohen if (mask & ~IB_DEVICE_MODIFY_NODE_DESC) 1405e126ba97SEli Cohen return -EOPNOTSUPP; 1406e126ba97SEli Cohen 1407e126ba97SEli Cohen if (!(mask & IB_DEVICE_MODIFY_NODE_DESC)) 1408e126ba97SEli Cohen return 0; 1409e126ba97SEli Cohen 1410e126ba97SEli Cohen /* 1411e126ba97SEli Cohen * If possible, pass node desc to FW, so it can generate 1412e126ba97SEli Cohen * a 144 trap. If cmd fails, just ignore. 1413e126ba97SEli Cohen */ 1414bd99fdeaSYuval Shaia memcpy(&in, props->node_desc, IB_DEVICE_NODE_DESC_MAX); 14159603b61dSJack Morgenstein err = mlx5_core_access_reg(dev->mdev, &in, sizeof(in), &out, 1416e126ba97SEli Cohen sizeof(out), MLX5_REG_NODE_DESC, 0, 1); 1417e126ba97SEli Cohen if (err) 1418e126ba97SEli Cohen return err; 1419e126ba97SEli Cohen 1420bd99fdeaSYuval Shaia memcpy(ibdev->node_desc, props->node_desc, IB_DEVICE_NODE_DESC_MAX); 1421e126ba97SEli Cohen 1422e126ba97SEli Cohen return err; 1423e126ba97SEli Cohen } 1424e126ba97SEli Cohen 1425cdbe33d0SEli Cohen static int set_port_caps_atomic(struct mlx5_ib_dev *dev, u8 port_num, u32 mask, 1426cdbe33d0SEli Cohen u32 value) 1427cdbe33d0SEli Cohen { 1428cdbe33d0SEli Cohen struct mlx5_hca_vport_context ctx = {}; 1429b3cbd6f0SDaniel Jurgens struct mlx5_core_dev *mdev; 1430b3cbd6f0SDaniel Jurgens u8 mdev_port_num; 1431cdbe33d0SEli Cohen int err; 1432cdbe33d0SEli Cohen 1433b3cbd6f0SDaniel Jurgens mdev = mlx5_ib_get_native_port_mdev(dev, port_num, &mdev_port_num); 1434b3cbd6f0SDaniel Jurgens if (!mdev) 1435b3cbd6f0SDaniel Jurgens return -ENODEV; 1436b3cbd6f0SDaniel Jurgens 1437b3cbd6f0SDaniel Jurgens err = mlx5_query_hca_vport_context(mdev, 0, mdev_port_num, 0, &ctx); 1438cdbe33d0SEli Cohen if (err) 1439b3cbd6f0SDaniel Jurgens goto out; 1440cdbe33d0SEli Cohen 1441cdbe33d0SEli Cohen if (~ctx.cap_mask1_perm & mask) { 1442cdbe33d0SEli Cohen mlx5_ib_warn(dev, "trying to change bitmask 0x%X but change supported 0x%X\n", 1443cdbe33d0SEli Cohen mask, ctx.cap_mask1_perm); 1444b3cbd6f0SDaniel Jurgens err = -EINVAL; 1445b3cbd6f0SDaniel Jurgens goto out; 1446cdbe33d0SEli Cohen } 1447cdbe33d0SEli Cohen 1448cdbe33d0SEli Cohen ctx.cap_mask1 = value; 1449cdbe33d0SEli Cohen ctx.cap_mask1_perm = mask; 1450b3cbd6f0SDaniel Jurgens err = mlx5_core_modify_hca_vport_context(mdev, 0, mdev_port_num, 1451b3cbd6f0SDaniel Jurgens 0, &ctx); 1452b3cbd6f0SDaniel Jurgens 1453b3cbd6f0SDaniel Jurgens out: 1454b3cbd6f0SDaniel Jurgens mlx5_ib_put_native_port_mdev(dev, port_num); 1455cdbe33d0SEli Cohen 1456cdbe33d0SEli Cohen return err; 1457cdbe33d0SEli Cohen } 1458cdbe33d0SEli Cohen 1459e126ba97SEli Cohen static int mlx5_ib_modify_port(struct ib_device *ibdev, u8 port, int mask, 1460e126ba97SEli Cohen struct ib_port_modify *props) 1461e126ba97SEli Cohen { 1462e126ba97SEli Cohen struct mlx5_ib_dev *dev = to_mdev(ibdev); 1463e126ba97SEli Cohen struct ib_port_attr attr; 1464e126ba97SEli Cohen u32 tmp; 1465e126ba97SEli Cohen int err; 1466cdbe33d0SEli Cohen u32 change_mask; 1467cdbe33d0SEli Cohen u32 value; 1468cdbe33d0SEli Cohen bool is_ib = (mlx5_ib_port_link_layer(ibdev, port) == 1469cdbe33d0SEli Cohen IB_LINK_LAYER_INFINIBAND); 1470cdbe33d0SEli Cohen 1471ec255879SMajd Dibbiny /* CM layer calls ib_modify_port() regardless of the link layer. For 1472ec255879SMajd Dibbiny * Ethernet ports, qkey violation and Port capabilities are meaningless. 1473ec255879SMajd Dibbiny */ 1474ec255879SMajd Dibbiny if (!is_ib) 1475ec255879SMajd Dibbiny return 0; 1476ec255879SMajd Dibbiny 1477cdbe33d0SEli Cohen if (MLX5_CAP_GEN(dev->mdev, ib_virt) && is_ib) { 1478cdbe33d0SEli Cohen change_mask = props->clr_port_cap_mask | props->set_port_cap_mask; 1479cdbe33d0SEli Cohen value = ~props->clr_port_cap_mask | props->set_port_cap_mask; 1480cdbe33d0SEli Cohen return set_port_caps_atomic(dev, port, change_mask, value); 1481cdbe33d0SEli Cohen } 1482e126ba97SEli Cohen 1483e126ba97SEli Cohen mutex_lock(&dev->cap_mask_mutex); 1484e126ba97SEli Cohen 1485c4550c63SOr Gerlitz err = ib_query_port(ibdev, port, &attr); 1486e126ba97SEli Cohen if (err) 1487e126ba97SEli Cohen goto out; 1488e126ba97SEli Cohen 1489e126ba97SEli Cohen tmp = (attr.port_cap_flags | props->set_port_cap_mask) & 1490e126ba97SEli Cohen ~props->clr_port_cap_mask; 1491e126ba97SEli Cohen 14929603b61dSJack Morgenstein err = mlx5_set_port_caps(dev->mdev, port, tmp); 1493e126ba97SEli Cohen 1494e126ba97SEli Cohen out: 1495e126ba97SEli Cohen mutex_unlock(&dev->cap_mask_mutex); 1496e126ba97SEli Cohen return err; 1497e126ba97SEli Cohen } 1498e126ba97SEli Cohen 149930aa60b3SEli Cohen static void print_lib_caps(struct mlx5_ib_dev *dev, u64 caps) 150030aa60b3SEli Cohen { 150130aa60b3SEli Cohen mlx5_ib_dbg(dev, "MLX5_LIB_CAP_4K_UAR = %s\n", 150230aa60b3SEli Cohen caps & MLX5_LIB_CAP_4K_UAR ? "y" : "n"); 150330aa60b3SEli Cohen } 150430aa60b3SEli Cohen 150531a78a5aSYishai Hadas static u16 calc_dynamic_bfregs(int uars_per_sys_page) 150631a78a5aSYishai Hadas { 150731a78a5aSYishai Hadas /* Large page with non 4k uar support might limit the dynamic size */ 150831a78a5aSYishai Hadas if (uars_per_sys_page == 1 && PAGE_SIZE > 4096) 150931a78a5aSYishai Hadas return MLX5_MIN_DYN_BFREGS; 151031a78a5aSYishai Hadas 151131a78a5aSYishai Hadas return MLX5_MAX_DYN_BFREGS; 151231a78a5aSYishai Hadas } 151331a78a5aSYishai Hadas 1514b037c29aSEli Cohen static int calc_total_bfregs(struct mlx5_ib_dev *dev, bool lib_uar_4k, 1515b037c29aSEli Cohen struct mlx5_ib_alloc_ucontext_req_v2 *req, 151631a78a5aSYishai Hadas struct mlx5_bfreg_info *bfregi) 1517b037c29aSEli Cohen { 1518b037c29aSEli Cohen int uars_per_sys_page; 1519b037c29aSEli Cohen int bfregs_per_sys_page; 1520b037c29aSEli Cohen int ref_bfregs = req->total_num_bfregs; 1521b037c29aSEli Cohen 1522b037c29aSEli Cohen if (req->total_num_bfregs == 0) 1523b037c29aSEli Cohen return -EINVAL; 1524b037c29aSEli Cohen 1525b037c29aSEli Cohen BUILD_BUG_ON(MLX5_MAX_BFREGS % MLX5_NON_FP_BFREGS_IN_PAGE); 1526b037c29aSEli Cohen BUILD_BUG_ON(MLX5_MAX_BFREGS < MLX5_NON_FP_BFREGS_IN_PAGE); 1527b037c29aSEli Cohen 1528b037c29aSEli Cohen if (req->total_num_bfregs > MLX5_MAX_BFREGS) 1529b037c29aSEli Cohen return -ENOMEM; 1530b037c29aSEli Cohen 1531b037c29aSEli Cohen uars_per_sys_page = get_uars_per_sys_page(dev, lib_uar_4k); 1532b037c29aSEli Cohen bfregs_per_sys_page = uars_per_sys_page * MLX5_NON_FP_BFREGS_PER_UAR; 153331a78a5aSYishai Hadas /* This holds the required static allocation asked by the user */ 1534b037c29aSEli Cohen req->total_num_bfregs = ALIGN(req->total_num_bfregs, bfregs_per_sys_page); 1535b037c29aSEli Cohen if (req->num_low_latency_bfregs > req->total_num_bfregs - 1) 1536b037c29aSEli Cohen return -EINVAL; 1537b037c29aSEli Cohen 153831a78a5aSYishai Hadas bfregi->num_static_sys_pages = req->total_num_bfregs / bfregs_per_sys_page; 153931a78a5aSYishai Hadas bfregi->num_dyn_bfregs = ALIGN(calc_dynamic_bfregs(uars_per_sys_page), bfregs_per_sys_page); 154031a78a5aSYishai Hadas bfregi->total_num_bfregs = req->total_num_bfregs + bfregi->num_dyn_bfregs; 154131a78a5aSYishai Hadas bfregi->num_sys_pages = bfregi->total_num_bfregs / bfregs_per_sys_page; 154231a78a5aSYishai Hadas 154331a78a5aSYishai 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", 1544b037c29aSEli Cohen MLX5_CAP_GEN(dev->mdev, uar_4k) ? "yes" : "no", 1545b037c29aSEli Cohen lib_uar_4k ? "yes" : "no", ref_bfregs, 154631a78a5aSYishai Hadas req->total_num_bfregs, bfregi->total_num_bfregs, 154731a78a5aSYishai Hadas bfregi->num_sys_pages); 1548b037c29aSEli Cohen 1549b037c29aSEli Cohen return 0; 1550b037c29aSEli Cohen } 1551b037c29aSEli Cohen 1552b037c29aSEli Cohen static int allocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context) 1553b037c29aSEli Cohen { 1554b037c29aSEli Cohen struct mlx5_bfreg_info *bfregi; 1555b037c29aSEli Cohen int err; 1556b037c29aSEli Cohen int i; 1557b037c29aSEli Cohen 1558b037c29aSEli Cohen bfregi = &context->bfregi; 155931a78a5aSYishai Hadas for (i = 0; i < bfregi->num_static_sys_pages; i++) { 1560b037c29aSEli Cohen err = mlx5_cmd_alloc_uar(dev->mdev, &bfregi->sys_pages[i]); 1561b037c29aSEli Cohen if (err) 1562b037c29aSEli Cohen goto error; 1563b037c29aSEli Cohen 1564b037c29aSEli Cohen mlx5_ib_dbg(dev, "allocated uar %d\n", bfregi->sys_pages[i]); 1565b037c29aSEli Cohen } 15664ed131d0SYishai Hadas 15674ed131d0SYishai Hadas for (i = bfregi->num_static_sys_pages; i < bfregi->num_sys_pages; i++) 15684ed131d0SYishai Hadas bfregi->sys_pages[i] = MLX5_IB_INVALID_UAR_INDEX; 15694ed131d0SYishai Hadas 1570b037c29aSEli Cohen return 0; 1571b037c29aSEli Cohen 1572b037c29aSEli Cohen error: 1573b037c29aSEli Cohen for (--i; i >= 0; i--) 1574b037c29aSEli Cohen if (mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i])) 1575b037c29aSEli Cohen mlx5_ib_warn(dev, "failed to free uar %d\n", i); 1576b037c29aSEli Cohen 1577b037c29aSEli Cohen return err; 1578b037c29aSEli Cohen } 1579b037c29aSEli Cohen 1580b037c29aSEli Cohen static int deallocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context) 1581b037c29aSEli Cohen { 1582b037c29aSEli Cohen struct mlx5_bfreg_info *bfregi; 1583b037c29aSEli Cohen int err; 1584b037c29aSEli Cohen int i; 1585b037c29aSEli Cohen 1586b037c29aSEli Cohen bfregi = &context->bfregi; 15874ed131d0SYishai Hadas for (i = 0; i < bfregi->num_sys_pages; i++) { 15884ed131d0SYishai Hadas if (i < bfregi->num_static_sys_pages || 15894ed131d0SYishai Hadas bfregi->sys_pages[i] != MLX5_IB_INVALID_UAR_INDEX) { 1590b037c29aSEli Cohen err = mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]); 1591b037c29aSEli Cohen if (err) { 15924ed131d0SYishai Hadas mlx5_ib_warn(dev, "failed to free uar %d, err=%d\n", i, err); 1593b037c29aSEli Cohen return err; 1594b037c29aSEli Cohen } 1595b037c29aSEli Cohen } 15964ed131d0SYishai Hadas } 15974ed131d0SYishai Hadas 1598b037c29aSEli Cohen return 0; 1599b037c29aSEli Cohen } 1600b037c29aSEli Cohen 1601c85023e1SHuy Nguyen static int mlx5_ib_alloc_transport_domain(struct mlx5_ib_dev *dev, u32 *tdn) 1602c85023e1SHuy Nguyen { 1603c85023e1SHuy Nguyen int err; 1604c85023e1SHuy Nguyen 1605c85023e1SHuy Nguyen err = mlx5_core_alloc_transport_domain(dev->mdev, tdn); 1606c85023e1SHuy Nguyen if (err) 1607c85023e1SHuy Nguyen return err; 1608c85023e1SHuy Nguyen 1609c85023e1SHuy Nguyen if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) || 16108978cc92SEran Ben Elisha (!MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) && 16118978cc92SEran Ben Elisha !MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc))) 1612c85023e1SHuy Nguyen return err; 1613c85023e1SHuy Nguyen 1614c85023e1SHuy Nguyen mutex_lock(&dev->lb_mutex); 1615c85023e1SHuy Nguyen dev->user_td++; 1616c85023e1SHuy Nguyen 1617c85023e1SHuy Nguyen if (dev->user_td == 2) 1618c85023e1SHuy Nguyen err = mlx5_nic_vport_update_local_lb(dev->mdev, true); 1619c85023e1SHuy Nguyen 1620c85023e1SHuy Nguyen mutex_unlock(&dev->lb_mutex); 1621c85023e1SHuy Nguyen return err; 1622c85023e1SHuy Nguyen } 1623c85023e1SHuy Nguyen 1624c85023e1SHuy Nguyen static void mlx5_ib_dealloc_transport_domain(struct mlx5_ib_dev *dev, u32 tdn) 1625c85023e1SHuy Nguyen { 1626c85023e1SHuy Nguyen mlx5_core_dealloc_transport_domain(dev->mdev, tdn); 1627c85023e1SHuy Nguyen 1628c85023e1SHuy Nguyen if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) || 16298978cc92SEran Ben Elisha (!MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) && 16308978cc92SEran Ben Elisha !MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc))) 1631c85023e1SHuy Nguyen return; 1632c85023e1SHuy Nguyen 1633c85023e1SHuy Nguyen mutex_lock(&dev->lb_mutex); 1634c85023e1SHuy Nguyen dev->user_td--; 1635c85023e1SHuy Nguyen 1636c85023e1SHuy Nguyen if (dev->user_td < 2) 1637c85023e1SHuy Nguyen mlx5_nic_vport_update_local_lb(dev->mdev, false); 1638c85023e1SHuy Nguyen 1639c85023e1SHuy Nguyen mutex_unlock(&dev->lb_mutex); 1640c85023e1SHuy Nguyen } 1641c85023e1SHuy Nguyen 1642e126ba97SEli Cohen static struct ib_ucontext *mlx5_ib_alloc_ucontext(struct ib_device *ibdev, 1643e126ba97SEli Cohen struct ib_udata *udata) 1644e126ba97SEli Cohen { 1645e126ba97SEli Cohen struct mlx5_ib_dev *dev = to_mdev(ibdev); 1646b368d7cbSMatan Barak struct mlx5_ib_alloc_ucontext_req_v2 req = {}; 1647b368d7cbSMatan Barak struct mlx5_ib_alloc_ucontext_resp resp = {}; 16485c99eaecSFeras Daoud struct mlx5_core_dev *mdev = dev->mdev; 1649e126ba97SEli Cohen struct mlx5_ib_ucontext *context; 16502f5ff264SEli Cohen struct mlx5_bfreg_info *bfregi; 165178c0f98cSEli Cohen int ver; 1652e126ba97SEli Cohen int err; 1653a168a41cSMajd Dibbiny size_t min_req_v2 = offsetof(struct mlx5_ib_alloc_ucontext_req_v2, 1654a168a41cSMajd Dibbiny max_cqe_version); 1655b037c29aSEli Cohen bool lib_uar_4k; 1656e126ba97SEli Cohen 1657e126ba97SEli Cohen if (!dev->ib_active) 1658e126ba97SEli Cohen return ERR_PTR(-EAGAIN); 1659e126ba97SEli Cohen 1660e093111dSAmrani, Ram if (udata->inlen == sizeof(struct mlx5_ib_alloc_ucontext_req)) 166178c0f98cSEli Cohen ver = 0; 1662e093111dSAmrani, Ram else if (udata->inlen >= min_req_v2) 166378c0f98cSEli Cohen ver = 2; 166478c0f98cSEli Cohen else 166578c0f98cSEli Cohen return ERR_PTR(-EINVAL); 166678c0f98cSEli Cohen 1667e093111dSAmrani, Ram err = ib_copy_from_udata(&req, udata, min(udata->inlen, sizeof(req))); 1668e126ba97SEli Cohen if (err) 1669e126ba97SEli Cohen return ERR_PTR(err); 1670e126ba97SEli Cohen 1671b368d7cbSMatan Barak if (req.flags) 167278c0f98cSEli Cohen return ERR_PTR(-EINVAL); 167378c0f98cSEli Cohen 1674f72300c5SHaggai Abramovsky if (req.comp_mask || req.reserved0 || req.reserved1 || req.reserved2) 1675b368d7cbSMatan Barak return ERR_PTR(-EOPNOTSUPP); 1676b368d7cbSMatan Barak 16772f5ff264SEli Cohen req.total_num_bfregs = ALIGN(req.total_num_bfregs, 16782f5ff264SEli Cohen MLX5_NON_FP_BFREGS_PER_UAR); 16792f5ff264SEli Cohen if (req.num_low_latency_bfregs > req.total_num_bfregs - 1) 1680e126ba97SEli Cohen return ERR_PTR(-EINVAL); 1681e126ba97SEli Cohen 1682938fe83cSSaeed Mahameed resp.qp_tab_size = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp); 16832cc6ad5fSNoa Osherovich if (mlx5_core_is_pf(dev->mdev) && MLX5_CAP_GEN(dev->mdev, bf)) 1684938fe83cSSaeed Mahameed resp.bf_reg_size = 1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size); 1685b47bd6eaSDaniel Jurgens resp.cache_line_size = cache_line_size(); 1686938fe83cSSaeed Mahameed resp.max_sq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq); 1687938fe83cSSaeed Mahameed resp.max_rq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq); 1688938fe83cSSaeed Mahameed resp.max_send_wqebb = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz); 1689938fe83cSSaeed Mahameed resp.max_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz); 1690938fe83cSSaeed Mahameed resp.max_srq_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz); 1691f72300c5SHaggai Abramovsky resp.cqe_version = min_t(__u8, 1692f72300c5SHaggai Abramovsky (__u8)MLX5_CAP_GEN(dev->mdev, cqe_version), 1693f72300c5SHaggai Abramovsky req.max_cqe_version); 169430aa60b3SEli Cohen resp.log_uar_size = MLX5_CAP_GEN(dev->mdev, uar_4k) ? 169530aa60b3SEli Cohen MLX5_ADAPTER_PAGE_SHIFT : PAGE_SHIFT; 169630aa60b3SEli Cohen resp.num_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ? 169730aa60b3SEli Cohen MLX5_CAP_GEN(dev->mdev, num_of_uars_per_page) : 1; 1698b368d7cbSMatan Barak resp.response_length = min(offsetof(typeof(resp), response_length) + 1699b368d7cbSMatan Barak sizeof(resp.response_length), udata->outlen); 1700e126ba97SEli Cohen 1701c03faa56SMatan Barak if (mlx5_accel_ipsec_device_caps(dev->mdev) & MLX5_ACCEL_IPSEC_CAP_DEVICE) { 1702c03faa56SMatan Barak if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_EGRESS)) 1703c03faa56SMatan Barak resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM; 1704c03faa56SMatan Barak if (mlx5_accel_ipsec_device_caps(dev->mdev) & MLX5_ACCEL_IPSEC_CAP_REQUIRED_METADATA) 1705c03faa56SMatan Barak resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_REQ_METADATA; 1706c03faa56SMatan Barak if (MLX5_CAP_FLOWTABLE(dev->mdev, flow_table_properties_nic_receive.ft_field_support.outer_esp_spi)) 1707c03faa56SMatan Barak resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_SPI_STEERING; 1708c03faa56SMatan Barak if (mlx5_accel_ipsec_device_caps(dev->mdev) & MLX5_ACCEL_IPSEC_CAP_TX_IV_IS_ESN) 1709c03faa56SMatan Barak resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_TX_IV_IS_ESN; 1710c03faa56SMatan Barak /* MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_FULL_OFFLOAD is currently always 0 */ 1711c03faa56SMatan Barak } 1712c03faa56SMatan Barak 1713e126ba97SEli Cohen context = kzalloc(sizeof(*context), GFP_KERNEL); 1714e126ba97SEli Cohen if (!context) 1715e126ba97SEli Cohen return ERR_PTR(-ENOMEM); 1716e126ba97SEli Cohen 171730aa60b3SEli Cohen lib_uar_4k = req.lib_caps & MLX5_LIB_CAP_4K_UAR; 17182f5ff264SEli Cohen bfregi = &context->bfregi; 1719b037c29aSEli Cohen 1720b037c29aSEli Cohen /* updates req->total_num_bfregs */ 172131a78a5aSYishai Hadas err = calc_total_bfregs(dev, lib_uar_4k, &req, bfregi); 1722b037c29aSEli Cohen if (err) 1723b037c29aSEli Cohen goto out_ctx; 1724b037c29aSEli Cohen 17252f5ff264SEli Cohen mutex_init(&bfregi->lock); 1726b037c29aSEli Cohen bfregi->lib_uar_4k = lib_uar_4k; 172731a78a5aSYishai Hadas bfregi->count = kcalloc(bfregi->total_num_bfregs, sizeof(*bfregi->count), 1728b037c29aSEli Cohen GFP_KERNEL); 1729b037c29aSEli Cohen if (!bfregi->count) { 1730e126ba97SEli Cohen err = -ENOMEM; 1731e126ba97SEli Cohen goto out_ctx; 1732e126ba97SEli Cohen } 1733e126ba97SEli Cohen 1734b037c29aSEli Cohen bfregi->sys_pages = kcalloc(bfregi->num_sys_pages, 1735b037c29aSEli Cohen sizeof(*bfregi->sys_pages), 1736e126ba97SEli Cohen GFP_KERNEL); 1737b037c29aSEli Cohen if (!bfregi->sys_pages) { 1738e126ba97SEli Cohen err = -ENOMEM; 1739e126ba97SEli Cohen goto out_count; 1740e126ba97SEli Cohen } 1741e126ba97SEli Cohen 1742b037c29aSEli Cohen err = allocate_uars(dev, context); 1743b037c29aSEli Cohen if (err) 1744b037c29aSEli Cohen goto out_sys_pages; 1745b037c29aSEli Cohen 1746b4cfe447SHaggai Eran #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING 1747b4cfe447SHaggai Eran context->ibucontext.invalidate_range = &mlx5_ib_invalidate_range; 1748b4cfe447SHaggai Eran #endif 1749b4cfe447SHaggai Eran 1750146d2f1aSmajd@mellanox.com if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain)) { 1751c85023e1SHuy Nguyen err = mlx5_ib_alloc_transport_domain(dev, &context->tdn); 1752146d2f1aSmajd@mellanox.com if (err) 1753c44ef998SIlya Lesokhin goto out_uars; 1754146d2f1aSmajd@mellanox.com } 1755146d2f1aSmajd@mellanox.com 17567c2344c3SMaor Gottlieb INIT_LIST_HEAD(&context->vma_private_list); 1757ad9a3668SMajd Dibbiny mutex_init(&context->vma_private_list_mutex); 1758e126ba97SEli Cohen INIT_LIST_HEAD(&context->db_page_list); 1759e126ba97SEli Cohen mutex_init(&context->db_page_mutex); 1760e126ba97SEli Cohen 17612f5ff264SEli Cohen resp.tot_bfregs = req.total_num_bfregs; 1762508562d6SDaniel Jurgens resp.num_ports = dev->num_ports; 1763b368d7cbSMatan Barak 1764f72300c5SHaggai Abramovsky if (field_avail(typeof(resp), cqe_version, udata->outlen)) 1765f72300c5SHaggai Abramovsky resp.response_length += sizeof(resp.cqe_version); 1766b368d7cbSMatan Barak 1767402ca536SBodong Wang if (field_avail(typeof(resp), cmds_supp_uhw, udata->outlen)) { 17686ad279c5SMoni Shoua resp.cmds_supp_uhw |= MLX5_USER_CMDS_SUPP_UHW_QUERY_DEVICE | 17696ad279c5SMoni Shoua MLX5_USER_CMDS_SUPP_UHW_CREATE_AH; 1770402ca536SBodong Wang resp.response_length += sizeof(resp.cmds_supp_uhw); 1771402ca536SBodong Wang } 1772402ca536SBodong Wang 177378984898SOr Gerlitz if (field_avail(typeof(resp), eth_min_inline, udata->outlen)) { 177478984898SOr Gerlitz if (mlx5_ib_port_link_layer(ibdev, 1) == IB_LINK_LAYER_ETHERNET) { 177578984898SOr Gerlitz mlx5_query_min_inline(dev->mdev, &resp.eth_min_inline); 177678984898SOr Gerlitz resp.eth_min_inline++; 177778984898SOr Gerlitz } 177878984898SOr Gerlitz resp.response_length += sizeof(resp.eth_min_inline); 177978984898SOr Gerlitz } 178078984898SOr Gerlitz 17815c99eaecSFeras Daoud if (field_avail(typeof(resp), clock_info_versions, udata->outlen)) { 17825c99eaecSFeras Daoud if (mdev->clock_info) 17835c99eaecSFeras Daoud resp.clock_info_versions = BIT(MLX5_IB_CLOCK_INFO_V1); 17845c99eaecSFeras Daoud resp.response_length += sizeof(resp.clock_info_versions); 17855c99eaecSFeras Daoud } 17865c99eaecSFeras Daoud 1787bc5c6eedSNoa Osherovich /* 1788bc5c6eedSNoa Osherovich * We don't want to expose information from the PCI bar that is located 1789bc5c6eedSNoa Osherovich * after 4096 bytes, so if the arch only supports larger pages, let's 1790bc5c6eedSNoa Osherovich * pretend we don't support reading the HCA's core clock. This is also 1791bc5c6eedSNoa Osherovich * forced by mmap function. 1792bc5c6eedSNoa Osherovich */ 1793de8d6e02SEli Cohen if (field_avail(typeof(resp), hca_core_clock_offset, udata->outlen)) { 1794de8d6e02SEli Cohen if (PAGE_SIZE <= 4096) { 1795b368d7cbSMatan Barak resp.comp_mask |= 1796b368d7cbSMatan Barak MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_CORE_CLOCK_OFFSET; 1797b368d7cbSMatan Barak resp.hca_core_clock_offset = 1798de8d6e02SEli Cohen offsetof(struct mlx5_init_seg, internal_timer_h) % PAGE_SIZE; 1799de8d6e02SEli Cohen } 18005c99eaecSFeras Daoud resp.response_length += sizeof(resp.hca_core_clock_offset); 1801b368d7cbSMatan Barak } 1802b368d7cbSMatan Barak 180330aa60b3SEli Cohen if (field_avail(typeof(resp), log_uar_size, udata->outlen)) 180430aa60b3SEli Cohen resp.response_length += sizeof(resp.log_uar_size); 180530aa60b3SEli Cohen 180630aa60b3SEli Cohen if (field_avail(typeof(resp), num_uars_per_page, udata->outlen)) 180730aa60b3SEli Cohen resp.response_length += sizeof(resp.num_uars_per_page); 180830aa60b3SEli Cohen 180931a78a5aSYishai Hadas if (field_avail(typeof(resp), num_dyn_bfregs, udata->outlen)) { 181031a78a5aSYishai Hadas resp.num_dyn_bfregs = bfregi->num_dyn_bfregs; 181131a78a5aSYishai Hadas resp.response_length += sizeof(resp.num_dyn_bfregs); 181231a78a5aSYishai Hadas } 181331a78a5aSYishai Hadas 1814b368d7cbSMatan Barak err = ib_copy_to_udata(udata, &resp, resp.response_length); 1815e126ba97SEli Cohen if (err) 1816146d2f1aSmajd@mellanox.com goto out_td; 1817e126ba97SEli Cohen 18182f5ff264SEli Cohen bfregi->ver = ver; 18192f5ff264SEli Cohen bfregi->num_low_latency_bfregs = req.num_low_latency_bfregs; 1820f72300c5SHaggai Abramovsky context->cqe_version = resp.cqe_version; 182130aa60b3SEli Cohen context->lib_caps = req.lib_caps; 182230aa60b3SEli Cohen print_lib_caps(dev, context->lib_caps); 1823f72300c5SHaggai Abramovsky 1824e126ba97SEli Cohen return &context->ibucontext; 1825e126ba97SEli Cohen 1826146d2f1aSmajd@mellanox.com out_td: 1827146d2f1aSmajd@mellanox.com if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain)) 1828c85023e1SHuy Nguyen mlx5_ib_dealloc_transport_domain(dev, context->tdn); 1829146d2f1aSmajd@mellanox.com 1830e126ba97SEli Cohen out_uars: 1831b037c29aSEli Cohen deallocate_uars(dev, context); 1832b037c29aSEli Cohen 1833b037c29aSEli Cohen out_sys_pages: 1834b037c29aSEli Cohen kfree(bfregi->sys_pages); 1835b037c29aSEli Cohen 1836e126ba97SEli Cohen out_count: 18372f5ff264SEli Cohen kfree(bfregi->count); 1838e126ba97SEli Cohen 1839e126ba97SEli Cohen out_ctx: 1840e126ba97SEli Cohen kfree(context); 1841b037c29aSEli Cohen 1842e126ba97SEli Cohen return ERR_PTR(err); 1843e126ba97SEli Cohen } 1844e126ba97SEli Cohen 1845e126ba97SEli Cohen static int mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext) 1846e126ba97SEli Cohen { 1847e126ba97SEli Cohen struct mlx5_ib_ucontext *context = to_mucontext(ibcontext); 1848e126ba97SEli Cohen struct mlx5_ib_dev *dev = to_mdev(ibcontext->device); 1849b037c29aSEli Cohen struct mlx5_bfreg_info *bfregi; 1850e126ba97SEli Cohen 1851b037c29aSEli Cohen bfregi = &context->bfregi; 1852146d2f1aSmajd@mellanox.com if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain)) 1853c85023e1SHuy Nguyen mlx5_ib_dealloc_transport_domain(dev, context->tdn); 1854146d2f1aSmajd@mellanox.com 1855b037c29aSEli Cohen deallocate_uars(dev, context); 1856b037c29aSEli Cohen kfree(bfregi->sys_pages); 18572f5ff264SEli Cohen kfree(bfregi->count); 1858e126ba97SEli Cohen kfree(context); 1859e126ba97SEli Cohen 1860e126ba97SEli Cohen return 0; 1861e126ba97SEli Cohen } 1862e126ba97SEli Cohen 1863b037c29aSEli Cohen static phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev, 18644ed131d0SYishai Hadas int uar_idx) 1865e126ba97SEli Cohen { 1866b037c29aSEli Cohen int fw_uars_per_page; 1867b037c29aSEli Cohen 1868b037c29aSEli Cohen fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ? MLX5_UARS_IN_PAGE : 1; 1869b037c29aSEli Cohen 18704ed131d0SYishai Hadas return (pci_resource_start(dev->mdev->pdev, 0) >> PAGE_SHIFT) + uar_idx / fw_uars_per_page; 1871e126ba97SEli Cohen } 1872e126ba97SEli Cohen 1873e126ba97SEli Cohen static int get_command(unsigned long offset) 1874e126ba97SEli Cohen { 1875e126ba97SEli Cohen return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK; 1876e126ba97SEli Cohen } 1877e126ba97SEli Cohen 1878e126ba97SEli Cohen static int get_arg(unsigned long offset) 1879e126ba97SEli Cohen { 1880e126ba97SEli Cohen return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1); 1881e126ba97SEli Cohen } 1882e126ba97SEli Cohen 1883e126ba97SEli Cohen static int get_index(unsigned long offset) 1884e126ba97SEli Cohen { 1885e126ba97SEli Cohen return get_arg(offset); 1886e126ba97SEli Cohen } 1887e126ba97SEli Cohen 18884ed131d0SYishai Hadas /* Index resides in an extra byte to enable larger values than 255 */ 18894ed131d0SYishai Hadas static int get_extended_index(unsigned long offset) 18904ed131d0SYishai Hadas { 18914ed131d0SYishai Hadas return get_arg(offset) | ((offset >> 16) & 0xff) << 8; 18924ed131d0SYishai Hadas } 18934ed131d0SYishai Hadas 18947c2344c3SMaor Gottlieb static void mlx5_ib_vma_open(struct vm_area_struct *area) 18957c2344c3SMaor Gottlieb { 18967c2344c3SMaor Gottlieb /* vma_open is called when a new VMA is created on top of our VMA. This 18977c2344c3SMaor Gottlieb * is done through either mremap flow or split_vma (usually due to 18987c2344c3SMaor Gottlieb * mlock, madvise, munmap, etc.) We do not support a clone of the VMA, 18997c2344c3SMaor Gottlieb * as this VMA is strongly hardware related. Therefore we set the 19007c2344c3SMaor Gottlieb * vm_ops of the newly created/cloned VMA to NULL, to prevent it from 19017c2344c3SMaor Gottlieb * calling us again and trying to do incorrect actions. We assume that 19027c2344c3SMaor Gottlieb * the original VMA size is exactly a single page, and therefore all 19037c2344c3SMaor Gottlieb * "splitting" operation will not happen to it. 19047c2344c3SMaor Gottlieb */ 19057c2344c3SMaor Gottlieb area->vm_ops = NULL; 19067c2344c3SMaor Gottlieb } 19077c2344c3SMaor Gottlieb 19087c2344c3SMaor Gottlieb static void mlx5_ib_vma_close(struct vm_area_struct *area) 19097c2344c3SMaor Gottlieb { 19107c2344c3SMaor Gottlieb struct mlx5_ib_vma_private_data *mlx5_ib_vma_priv_data; 19117c2344c3SMaor Gottlieb 19127c2344c3SMaor Gottlieb /* It's guaranteed that all VMAs opened on a FD are closed before the 19137c2344c3SMaor Gottlieb * file itself is closed, therefore no sync is needed with the regular 19147c2344c3SMaor Gottlieb * closing flow. (e.g. mlx5 ib_dealloc_ucontext) 19157c2344c3SMaor Gottlieb * However need a sync with accessing the vma as part of 19167c2344c3SMaor Gottlieb * mlx5_ib_disassociate_ucontext. 19177c2344c3SMaor Gottlieb * The close operation is usually called under mm->mmap_sem except when 19187c2344c3SMaor Gottlieb * process is exiting. 19197c2344c3SMaor Gottlieb * The exiting case is handled explicitly as part of 19207c2344c3SMaor Gottlieb * mlx5_ib_disassociate_ucontext. 19217c2344c3SMaor Gottlieb */ 19227c2344c3SMaor Gottlieb mlx5_ib_vma_priv_data = (struct mlx5_ib_vma_private_data *)area->vm_private_data; 19237c2344c3SMaor Gottlieb 19247c2344c3SMaor Gottlieb /* setting the vma context pointer to null in the mlx5_ib driver's 19257c2344c3SMaor Gottlieb * private data, to protect a race condition in 19267c2344c3SMaor Gottlieb * mlx5_ib_disassociate_ucontext(). 19277c2344c3SMaor Gottlieb */ 19287c2344c3SMaor Gottlieb mlx5_ib_vma_priv_data->vma = NULL; 1929ad9a3668SMajd Dibbiny mutex_lock(mlx5_ib_vma_priv_data->vma_private_list_mutex); 19307c2344c3SMaor Gottlieb list_del(&mlx5_ib_vma_priv_data->list); 1931ad9a3668SMajd Dibbiny mutex_unlock(mlx5_ib_vma_priv_data->vma_private_list_mutex); 19327c2344c3SMaor Gottlieb kfree(mlx5_ib_vma_priv_data); 19337c2344c3SMaor Gottlieb } 19347c2344c3SMaor Gottlieb 19357c2344c3SMaor Gottlieb static const struct vm_operations_struct mlx5_ib_vm_ops = { 19367c2344c3SMaor Gottlieb .open = mlx5_ib_vma_open, 19377c2344c3SMaor Gottlieb .close = mlx5_ib_vma_close 19387c2344c3SMaor Gottlieb }; 19397c2344c3SMaor Gottlieb 19407c2344c3SMaor Gottlieb static int mlx5_ib_set_vma_data(struct vm_area_struct *vma, 19417c2344c3SMaor Gottlieb struct mlx5_ib_ucontext *ctx) 19427c2344c3SMaor Gottlieb { 19437c2344c3SMaor Gottlieb struct mlx5_ib_vma_private_data *vma_prv; 19447c2344c3SMaor Gottlieb struct list_head *vma_head = &ctx->vma_private_list; 19457c2344c3SMaor Gottlieb 19467c2344c3SMaor Gottlieb vma_prv = kzalloc(sizeof(*vma_prv), GFP_KERNEL); 19477c2344c3SMaor Gottlieb if (!vma_prv) 19487c2344c3SMaor Gottlieb return -ENOMEM; 19497c2344c3SMaor Gottlieb 19507c2344c3SMaor Gottlieb vma_prv->vma = vma; 1951ad9a3668SMajd Dibbiny vma_prv->vma_private_list_mutex = &ctx->vma_private_list_mutex; 19527c2344c3SMaor Gottlieb vma->vm_private_data = vma_prv; 19537c2344c3SMaor Gottlieb vma->vm_ops = &mlx5_ib_vm_ops; 19547c2344c3SMaor Gottlieb 1955ad9a3668SMajd Dibbiny mutex_lock(&ctx->vma_private_list_mutex); 19567c2344c3SMaor Gottlieb list_add(&vma_prv->list, vma_head); 1957ad9a3668SMajd Dibbiny mutex_unlock(&ctx->vma_private_list_mutex); 19587c2344c3SMaor Gottlieb 19597c2344c3SMaor Gottlieb return 0; 19607c2344c3SMaor Gottlieb } 19617c2344c3SMaor Gottlieb 19627c2344c3SMaor Gottlieb static void mlx5_ib_disassociate_ucontext(struct ib_ucontext *ibcontext) 19637c2344c3SMaor Gottlieb { 19647c2344c3SMaor Gottlieb int ret; 19657c2344c3SMaor Gottlieb struct vm_area_struct *vma; 19667c2344c3SMaor Gottlieb struct mlx5_ib_vma_private_data *vma_private, *n; 19677c2344c3SMaor Gottlieb struct mlx5_ib_ucontext *context = to_mucontext(ibcontext); 19687c2344c3SMaor Gottlieb struct task_struct *owning_process = NULL; 19697c2344c3SMaor Gottlieb struct mm_struct *owning_mm = NULL; 19707c2344c3SMaor Gottlieb 19717c2344c3SMaor Gottlieb owning_process = get_pid_task(ibcontext->tgid, PIDTYPE_PID); 19727c2344c3SMaor Gottlieb if (!owning_process) 19737c2344c3SMaor Gottlieb return; 19747c2344c3SMaor Gottlieb 19757c2344c3SMaor Gottlieb owning_mm = get_task_mm(owning_process); 19767c2344c3SMaor Gottlieb if (!owning_mm) { 19777c2344c3SMaor Gottlieb pr_info("no mm, disassociate ucontext is pending task termination\n"); 19787c2344c3SMaor Gottlieb while (1) { 19797c2344c3SMaor Gottlieb put_task_struct(owning_process); 19807c2344c3SMaor Gottlieb usleep_range(1000, 2000); 19817c2344c3SMaor Gottlieb owning_process = get_pid_task(ibcontext->tgid, 19827c2344c3SMaor Gottlieb PIDTYPE_PID); 19837c2344c3SMaor Gottlieb if (!owning_process || 19847c2344c3SMaor Gottlieb owning_process->state == TASK_DEAD) { 19857c2344c3SMaor Gottlieb pr_info("disassociate ucontext done, task was terminated\n"); 19867c2344c3SMaor Gottlieb /* in case task was dead need to release the 19877c2344c3SMaor Gottlieb * task struct. 19887c2344c3SMaor Gottlieb */ 19897c2344c3SMaor Gottlieb if (owning_process) 19907c2344c3SMaor Gottlieb put_task_struct(owning_process); 19917c2344c3SMaor Gottlieb return; 19927c2344c3SMaor Gottlieb } 19937c2344c3SMaor Gottlieb } 19947c2344c3SMaor Gottlieb } 19957c2344c3SMaor Gottlieb 19967c2344c3SMaor Gottlieb /* need to protect from a race on closing the vma as part of 19977c2344c3SMaor Gottlieb * mlx5_ib_vma_close. 19987c2344c3SMaor Gottlieb */ 1999ecc7d83bSMaor Gottlieb down_write(&owning_mm->mmap_sem); 2000ad9a3668SMajd Dibbiny mutex_lock(&context->vma_private_list_mutex); 20017c2344c3SMaor Gottlieb list_for_each_entry_safe(vma_private, n, &context->vma_private_list, 20027c2344c3SMaor Gottlieb list) { 20037c2344c3SMaor Gottlieb vma = vma_private->vma; 20047c2344c3SMaor Gottlieb ret = zap_vma_ptes(vma, vma->vm_start, 20057c2344c3SMaor Gottlieb PAGE_SIZE); 20067c2344c3SMaor Gottlieb WARN_ONCE(ret, "%s: zap_vma_ptes failed", __func__); 20077c2344c3SMaor Gottlieb /* context going to be destroyed, should 20087c2344c3SMaor Gottlieb * not access ops any more. 20097c2344c3SMaor Gottlieb */ 201013776612SMaor Gottlieb vma->vm_flags &= ~(VM_SHARED | VM_MAYSHARE); 20117c2344c3SMaor Gottlieb vma->vm_ops = NULL; 20127c2344c3SMaor Gottlieb list_del(&vma_private->list); 20137c2344c3SMaor Gottlieb kfree(vma_private); 20147c2344c3SMaor Gottlieb } 2015ad9a3668SMajd Dibbiny mutex_unlock(&context->vma_private_list_mutex); 2016ecc7d83bSMaor Gottlieb up_write(&owning_mm->mmap_sem); 20177c2344c3SMaor Gottlieb mmput(owning_mm); 20187c2344c3SMaor Gottlieb put_task_struct(owning_process); 20197c2344c3SMaor Gottlieb } 20207c2344c3SMaor Gottlieb 202137aa5c36SGuy Levi static inline char *mmap_cmd2str(enum mlx5_ib_mmap_cmd cmd) 2022e126ba97SEli Cohen { 202337aa5c36SGuy Levi switch (cmd) { 202437aa5c36SGuy Levi case MLX5_IB_MMAP_WC_PAGE: 202537aa5c36SGuy Levi return "WC"; 2026e126ba97SEli Cohen case MLX5_IB_MMAP_REGULAR_PAGE: 202737aa5c36SGuy Levi return "best effort WC"; 202837aa5c36SGuy Levi case MLX5_IB_MMAP_NC_PAGE: 202937aa5c36SGuy Levi return "NC"; 203024da0016SAriel Levkovich case MLX5_IB_MMAP_DEVICE_MEM: 203124da0016SAriel Levkovich return "Device Memory"; 203237aa5c36SGuy Levi default: 203337aa5c36SGuy Levi return NULL; 203437aa5c36SGuy Levi } 203537aa5c36SGuy Levi } 203637aa5c36SGuy Levi 20375c99eaecSFeras Daoud static int mlx5_ib_mmap_clock_info_page(struct mlx5_ib_dev *dev, 20385c99eaecSFeras Daoud struct vm_area_struct *vma, 20395c99eaecSFeras Daoud struct mlx5_ib_ucontext *context) 20405c99eaecSFeras Daoud { 20415c99eaecSFeras Daoud phys_addr_t pfn; 20425c99eaecSFeras Daoud int err; 20435c99eaecSFeras Daoud 20445c99eaecSFeras Daoud if (vma->vm_end - vma->vm_start != PAGE_SIZE) 20455c99eaecSFeras Daoud return -EINVAL; 20465c99eaecSFeras Daoud 20475c99eaecSFeras Daoud if (get_index(vma->vm_pgoff) != MLX5_IB_CLOCK_INFO_V1) 20485c99eaecSFeras Daoud return -EOPNOTSUPP; 20495c99eaecSFeras Daoud 20505c99eaecSFeras Daoud if (vma->vm_flags & VM_WRITE) 20515c99eaecSFeras Daoud return -EPERM; 20525c99eaecSFeras Daoud 20535c99eaecSFeras Daoud if (!dev->mdev->clock_info_page) 20545c99eaecSFeras Daoud return -EOPNOTSUPP; 20555c99eaecSFeras Daoud 20565c99eaecSFeras Daoud pfn = page_to_pfn(dev->mdev->clock_info_page); 20575c99eaecSFeras Daoud err = remap_pfn_range(vma, vma->vm_start, pfn, PAGE_SIZE, 20585c99eaecSFeras Daoud vma->vm_page_prot); 20595c99eaecSFeras Daoud if (err) 20605c99eaecSFeras Daoud return err; 20615c99eaecSFeras Daoud 20625c99eaecSFeras Daoud return mlx5_ib_set_vma_data(vma, context); 20635c99eaecSFeras Daoud } 20645c99eaecSFeras Daoud 206537aa5c36SGuy Levi static int uar_mmap(struct mlx5_ib_dev *dev, enum mlx5_ib_mmap_cmd cmd, 20667c2344c3SMaor Gottlieb struct vm_area_struct *vma, 20677c2344c3SMaor Gottlieb struct mlx5_ib_ucontext *context) 206837aa5c36SGuy Levi { 20692f5ff264SEli Cohen struct mlx5_bfreg_info *bfregi = &context->bfregi; 207037aa5c36SGuy Levi int err; 207137aa5c36SGuy Levi unsigned long idx; 207237aa5c36SGuy Levi phys_addr_t pfn, pa; 207337aa5c36SGuy Levi pgprot_t prot; 20744ed131d0SYishai Hadas u32 bfreg_dyn_idx = 0; 20754ed131d0SYishai Hadas u32 uar_index; 20764ed131d0SYishai Hadas int dyn_uar = (cmd == MLX5_IB_MMAP_ALLOC_WC); 20774ed131d0SYishai Hadas int max_valid_idx = dyn_uar ? bfregi->num_sys_pages : 20784ed131d0SYishai Hadas bfregi->num_static_sys_pages; 2079b037c29aSEli Cohen 2080b037c29aSEli Cohen if (vma->vm_end - vma->vm_start != PAGE_SIZE) 2081b037c29aSEli Cohen return -EINVAL; 2082b037c29aSEli Cohen 20834ed131d0SYishai Hadas if (dyn_uar) 20844ed131d0SYishai Hadas idx = get_extended_index(vma->vm_pgoff) + bfregi->num_static_sys_pages; 20854ed131d0SYishai Hadas else 2086b037c29aSEli Cohen idx = get_index(vma->vm_pgoff); 20874ed131d0SYishai Hadas 20884ed131d0SYishai Hadas if (idx >= max_valid_idx) { 20894ed131d0SYishai Hadas mlx5_ib_warn(dev, "invalid uar index %lu, max=%d\n", 20904ed131d0SYishai Hadas idx, max_valid_idx); 2091b037c29aSEli Cohen return -EINVAL; 2092b037c29aSEli Cohen } 209337aa5c36SGuy Levi 209437aa5c36SGuy Levi switch (cmd) { 209537aa5c36SGuy Levi case MLX5_IB_MMAP_WC_PAGE: 20964ed131d0SYishai Hadas case MLX5_IB_MMAP_ALLOC_WC: 209737aa5c36SGuy Levi /* Some architectures don't support WC memory */ 209837aa5c36SGuy Levi #if defined(CONFIG_X86) 209937aa5c36SGuy Levi if (!pat_enabled()) 210037aa5c36SGuy Levi return -EPERM; 210137aa5c36SGuy Levi #elif !(defined(CONFIG_PPC) || (defined(CONFIG_ARM) && defined(CONFIG_MMU))) 210237aa5c36SGuy Levi return -EPERM; 210337aa5c36SGuy Levi #endif 210437aa5c36SGuy Levi /* fall through */ 210537aa5c36SGuy Levi case MLX5_IB_MMAP_REGULAR_PAGE: 210637aa5c36SGuy Levi /* For MLX5_IB_MMAP_REGULAR_PAGE do the best effort to get WC */ 210737aa5c36SGuy Levi prot = pgprot_writecombine(vma->vm_page_prot); 210837aa5c36SGuy Levi break; 210937aa5c36SGuy Levi case MLX5_IB_MMAP_NC_PAGE: 211037aa5c36SGuy Levi prot = pgprot_noncached(vma->vm_page_prot); 211137aa5c36SGuy Levi break; 211237aa5c36SGuy Levi default: 211337aa5c36SGuy Levi return -EINVAL; 211437aa5c36SGuy Levi } 211537aa5c36SGuy Levi 21164ed131d0SYishai Hadas if (dyn_uar) { 21174ed131d0SYishai Hadas int uars_per_page; 21184ed131d0SYishai Hadas 21194ed131d0SYishai Hadas uars_per_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k); 21204ed131d0SYishai Hadas bfreg_dyn_idx = idx * (uars_per_page * MLX5_NON_FP_BFREGS_PER_UAR); 21214ed131d0SYishai Hadas if (bfreg_dyn_idx >= bfregi->total_num_bfregs) { 21224ed131d0SYishai Hadas mlx5_ib_warn(dev, "invalid bfreg_dyn_idx %u, max=%u\n", 21234ed131d0SYishai Hadas bfreg_dyn_idx, bfregi->total_num_bfregs); 21244ed131d0SYishai Hadas return -EINVAL; 21254ed131d0SYishai Hadas } 21264ed131d0SYishai Hadas 21274ed131d0SYishai Hadas mutex_lock(&bfregi->lock); 21284ed131d0SYishai Hadas /* Fail if uar already allocated, first bfreg index of each 21294ed131d0SYishai Hadas * page holds its count. 21304ed131d0SYishai Hadas */ 21314ed131d0SYishai Hadas if (bfregi->count[bfreg_dyn_idx]) { 21324ed131d0SYishai Hadas mlx5_ib_warn(dev, "wrong offset, idx %lu is busy, bfregn=%u\n", idx, bfreg_dyn_idx); 21334ed131d0SYishai Hadas mutex_unlock(&bfregi->lock); 21344ed131d0SYishai Hadas return -EINVAL; 21354ed131d0SYishai Hadas } 21364ed131d0SYishai Hadas 21374ed131d0SYishai Hadas bfregi->count[bfreg_dyn_idx]++; 21384ed131d0SYishai Hadas mutex_unlock(&bfregi->lock); 21394ed131d0SYishai Hadas 21404ed131d0SYishai Hadas err = mlx5_cmd_alloc_uar(dev->mdev, &uar_index); 21414ed131d0SYishai Hadas if (err) { 21424ed131d0SYishai Hadas mlx5_ib_warn(dev, "UAR alloc failed\n"); 21434ed131d0SYishai Hadas goto free_bfreg; 21444ed131d0SYishai Hadas } 21454ed131d0SYishai Hadas } else { 21464ed131d0SYishai Hadas uar_index = bfregi->sys_pages[idx]; 21474ed131d0SYishai Hadas } 21484ed131d0SYishai Hadas 21494ed131d0SYishai Hadas pfn = uar_index2pfn(dev, uar_index); 215037aa5c36SGuy Levi mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn %pa\n", idx, &pfn); 2151e126ba97SEli Cohen 215237aa5c36SGuy Levi vma->vm_page_prot = prot; 215337aa5c36SGuy Levi err = io_remap_pfn_range(vma, vma->vm_start, pfn, 215437aa5c36SGuy Levi PAGE_SIZE, vma->vm_page_prot); 215537aa5c36SGuy Levi if (err) { 2156*8f062287SLeon Romanovsky mlx5_ib_err(dev, 2157*8f062287SLeon Romanovsky "io_remap_pfn_range failed with error=%d, mmap_cmd=%s\n", 2158*8f062287SLeon Romanovsky err, mmap_cmd2str(cmd)); 21594ed131d0SYishai Hadas err = -EAGAIN; 21604ed131d0SYishai Hadas goto err; 216137aa5c36SGuy Levi } 2162e126ba97SEli Cohen 216337aa5c36SGuy Levi pa = pfn << PAGE_SHIFT; 216437aa5c36SGuy Levi 21654ed131d0SYishai Hadas err = mlx5_ib_set_vma_data(vma, context); 21664ed131d0SYishai Hadas if (err) 21674ed131d0SYishai Hadas goto err; 21684ed131d0SYishai Hadas 21694ed131d0SYishai Hadas if (dyn_uar) 21704ed131d0SYishai Hadas bfregi->sys_pages[idx] = uar_index; 21714ed131d0SYishai Hadas return 0; 21724ed131d0SYishai Hadas 21734ed131d0SYishai Hadas err: 21744ed131d0SYishai Hadas if (!dyn_uar) 21754ed131d0SYishai Hadas return err; 21764ed131d0SYishai Hadas 21774ed131d0SYishai Hadas mlx5_cmd_free_uar(dev->mdev, idx); 21784ed131d0SYishai Hadas 21794ed131d0SYishai Hadas free_bfreg: 21804ed131d0SYishai Hadas mlx5_ib_free_bfreg(dev, bfregi, bfreg_dyn_idx); 21814ed131d0SYishai Hadas 21824ed131d0SYishai Hadas return err; 218337aa5c36SGuy Levi } 218437aa5c36SGuy Levi 218524da0016SAriel Levkovich static int dm_mmap(struct ib_ucontext *context, struct vm_area_struct *vma) 218624da0016SAriel Levkovich { 218724da0016SAriel Levkovich struct mlx5_ib_ucontext *mctx = to_mucontext(context); 218824da0016SAriel Levkovich struct mlx5_ib_dev *dev = to_mdev(context->device); 218924da0016SAriel Levkovich u16 page_idx = get_extended_index(vma->vm_pgoff); 219024da0016SAriel Levkovich size_t map_size = vma->vm_end - vma->vm_start; 219124da0016SAriel Levkovich u32 npages = map_size >> PAGE_SHIFT; 219224da0016SAriel Levkovich phys_addr_t pfn; 219324da0016SAriel Levkovich pgprot_t prot; 219424da0016SAriel Levkovich 219524da0016SAriel Levkovich if (find_next_zero_bit(mctx->dm_pages, page_idx + npages, page_idx) != 219624da0016SAriel Levkovich page_idx + npages) 219724da0016SAriel Levkovich return -EINVAL; 219824da0016SAriel Levkovich 219924da0016SAriel Levkovich pfn = ((pci_resource_start(dev->mdev->pdev, 0) + 220024da0016SAriel Levkovich MLX5_CAP64_DEV_MEM(dev->mdev, memic_bar_start_addr)) >> 220124da0016SAriel Levkovich PAGE_SHIFT) + 220224da0016SAriel Levkovich page_idx; 220324da0016SAriel Levkovich prot = pgprot_writecombine(vma->vm_page_prot); 220424da0016SAriel Levkovich vma->vm_page_prot = prot; 220524da0016SAriel Levkovich 220624da0016SAriel Levkovich if (io_remap_pfn_range(vma, vma->vm_start, pfn, map_size, 220724da0016SAriel Levkovich vma->vm_page_prot)) 220824da0016SAriel Levkovich return -EAGAIN; 220924da0016SAriel Levkovich 221024da0016SAriel Levkovich return mlx5_ib_set_vma_data(vma, mctx); 221124da0016SAriel Levkovich } 221224da0016SAriel Levkovich 221337aa5c36SGuy Levi static int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma) 221437aa5c36SGuy Levi { 221537aa5c36SGuy Levi struct mlx5_ib_ucontext *context = to_mucontext(ibcontext); 221637aa5c36SGuy Levi struct mlx5_ib_dev *dev = to_mdev(ibcontext->device); 221737aa5c36SGuy Levi unsigned long command; 221837aa5c36SGuy Levi phys_addr_t pfn; 221937aa5c36SGuy Levi 222037aa5c36SGuy Levi command = get_command(vma->vm_pgoff); 222137aa5c36SGuy Levi switch (command) { 222237aa5c36SGuy Levi case MLX5_IB_MMAP_WC_PAGE: 222337aa5c36SGuy Levi case MLX5_IB_MMAP_NC_PAGE: 222437aa5c36SGuy Levi case MLX5_IB_MMAP_REGULAR_PAGE: 22254ed131d0SYishai Hadas case MLX5_IB_MMAP_ALLOC_WC: 22267c2344c3SMaor Gottlieb return uar_mmap(dev, command, vma, context); 2227e126ba97SEli Cohen 2228e126ba97SEli Cohen case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES: 2229e126ba97SEli Cohen return -ENOSYS; 2230e126ba97SEli Cohen 2231d69e3bcfSMatan Barak case MLX5_IB_MMAP_CORE_CLOCK: 2232d69e3bcfSMatan Barak if (vma->vm_end - vma->vm_start != PAGE_SIZE) 2233d69e3bcfSMatan Barak return -EINVAL; 2234d69e3bcfSMatan Barak 22356cbac1e4SMatan Barak if (vma->vm_flags & VM_WRITE) 2236d69e3bcfSMatan Barak return -EPERM; 2237d69e3bcfSMatan Barak 2238d69e3bcfSMatan Barak /* Don't expose to user-space information it shouldn't have */ 2239d69e3bcfSMatan Barak if (PAGE_SIZE > 4096) 2240d69e3bcfSMatan Barak return -EOPNOTSUPP; 2241d69e3bcfSMatan Barak 2242d69e3bcfSMatan Barak vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); 2243d69e3bcfSMatan Barak pfn = (dev->mdev->iseg_base + 2244d69e3bcfSMatan Barak offsetof(struct mlx5_init_seg, internal_timer_h)) >> 2245d69e3bcfSMatan Barak PAGE_SHIFT; 2246d69e3bcfSMatan Barak if (io_remap_pfn_range(vma, vma->vm_start, pfn, 2247d69e3bcfSMatan Barak PAGE_SIZE, vma->vm_page_prot)) 2248d69e3bcfSMatan Barak return -EAGAIN; 2249d69e3bcfSMatan Barak break; 22505c99eaecSFeras Daoud case MLX5_IB_MMAP_CLOCK_INFO: 22515c99eaecSFeras Daoud return mlx5_ib_mmap_clock_info_page(dev, vma, context); 2252d69e3bcfSMatan Barak 225324da0016SAriel Levkovich case MLX5_IB_MMAP_DEVICE_MEM: 225424da0016SAriel Levkovich return dm_mmap(ibcontext, vma); 225524da0016SAriel Levkovich 2256e126ba97SEli Cohen default: 2257e126ba97SEli Cohen return -EINVAL; 2258e126ba97SEli Cohen } 2259e126ba97SEli Cohen 2260e126ba97SEli Cohen return 0; 2261e126ba97SEli Cohen } 2262e126ba97SEli Cohen 226324da0016SAriel Levkovich struct ib_dm *mlx5_ib_alloc_dm(struct ib_device *ibdev, 226424da0016SAriel Levkovich struct ib_ucontext *context, 226524da0016SAriel Levkovich struct ib_dm_alloc_attr *attr, 226624da0016SAriel Levkovich struct uverbs_attr_bundle *attrs) 226724da0016SAriel Levkovich { 226824da0016SAriel Levkovich u64 act_size = roundup(attr->length, MLX5_MEMIC_BASE_SIZE); 226924da0016SAriel Levkovich struct mlx5_memic *memic = &to_mdev(ibdev)->memic; 227024da0016SAriel Levkovich phys_addr_t memic_addr; 227124da0016SAriel Levkovich struct mlx5_ib_dm *dm; 227224da0016SAriel Levkovich u64 start_offset; 227324da0016SAriel Levkovich u32 page_idx; 227424da0016SAriel Levkovich int err; 227524da0016SAriel Levkovich 227624da0016SAriel Levkovich dm = kzalloc(sizeof(*dm), GFP_KERNEL); 227724da0016SAriel Levkovich if (!dm) 227824da0016SAriel Levkovich return ERR_PTR(-ENOMEM); 227924da0016SAriel Levkovich 228024da0016SAriel Levkovich mlx5_ib_dbg(to_mdev(ibdev), "alloc_memic req: user_length=0x%llx act_length=0x%llx log_alignment=%d\n", 228124da0016SAriel Levkovich attr->length, act_size, attr->alignment); 228224da0016SAriel Levkovich 228324da0016SAriel Levkovich err = mlx5_cmd_alloc_memic(memic, &memic_addr, 228424da0016SAriel Levkovich act_size, attr->alignment); 228524da0016SAriel Levkovich if (err) 228624da0016SAriel Levkovich goto err_free; 228724da0016SAriel Levkovich 228824da0016SAriel Levkovich start_offset = memic_addr & ~PAGE_MASK; 228924da0016SAriel Levkovich page_idx = (memic_addr - pci_resource_start(memic->dev->pdev, 0) - 229024da0016SAriel Levkovich MLX5_CAP64_DEV_MEM(memic->dev, memic_bar_start_addr)) >> 229124da0016SAriel Levkovich PAGE_SHIFT; 229224da0016SAriel Levkovich 229324da0016SAriel Levkovich err = uverbs_copy_to(attrs, 229424da0016SAriel Levkovich MLX5_IB_ATTR_ALLOC_DM_RESP_START_OFFSET, 229524da0016SAriel Levkovich &start_offset, sizeof(start_offset)); 229624da0016SAriel Levkovich if (err) 229724da0016SAriel Levkovich goto err_dealloc; 229824da0016SAriel Levkovich 229924da0016SAriel Levkovich err = uverbs_copy_to(attrs, 230024da0016SAriel Levkovich MLX5_IB_ATTR_ALLOC_DM_RESP_PAGE_INDEX, 230124da0016SAriel Levkovich &page_idx, sizeof(page_idx)); 230224da0016SAriel Levkovich if (err) 230324da0016SAriel Levkovich goto err_dealloc; 230424da0016SAriel Levkovich 230524da0016SAriel Levkovich bitmap_set(to_mucontext(context)->dm_pages, page_idx, 230624da0016SAriel Levkovich DIV_ROUND_UP(act_size, PAGE_SIZE)); 230724da0016SAriel Levkovich 230824da0016SAriel Levkovich dm->dev_addr = memic_addr; 230924da0016SAriel Levkovich 231024da0016SAriel Levkovich return &dm->ibdm; 231124da0016SAriel Levkovich 231224da0016SAriel Levkovich err_dealloc: 231324da0016SAriel Levkovich mlx5_cmd_dealloc_memic(memic, memic_addr, 231424da0016SAriel Levkovich act_size); 231524da0016SAriel Levkovich err_free: 231624da0016SAriel Levkovich kfree(dm); 231724da0016SAriel Levkovich return ERR_PTR(err); 231824da0016SAriel Levkovich } 231924da0016SAriel Levkovich 232024da0016SAriel Levkovich int mlx5_ib_dealloc_dm(struct ib_dm *ibdm) 232124da0016SAriel Levkovich { 232224da0016SAriel Levkovich struct mlx5_memic *memic = &to_mdev(ibdm->device)->memic; 232324da0016SAriel Levkovich struct mlx5_ib_dm *dm = to_mdm(ibdm); 232424da0016SAriel Levkovich u64 act_size = roundup(dm->ibdm.length, MLX5_MEMIC_BASE_SIZE); 232524da0016SAriel Levkovich u32 page_idx; 232624da0016SAriel Levkovich int ret; 232724da0016SAriel Levkovich 232824da0016SAriel Levkovich ret = mlx5_cmd_dealloc_memic(memic, dm->dev_addr, act_size); 232924da0016SAriel Levkovich if (ret) 233024da0016SAriel Levkovich return ret; 233124da0016SAriel Levkovich 233224da0016SAriel Levkovich page_idx = (dm->dev_addr - pci_resource_start(memic->dev->pdev, 0) - 233324da0016SAriel Levkovich MLX5_CAP64_DEV_MEM(memic->dev, memic_bar_start_addr)) >> 233424da0016SAriel Levkovich PAGE_SHIFT; 233524da0016SAriel Levkovich bitmap_clear(to_mucontext(ibdm->uobject->context)->dm_pages, 233624da0016SAriel Levkovich page_idx, 233724da0016SAriel Levkovich DIV_ROUND_UP(act_size, PAGE_SIZE)); 233824da0016SAriel Levkovich 233924da0016SAriel Levkovich kfree(dm); 234024da0016SAriel Levkovich 234124da0016SAriel Levkovich return 0; 234224da0016SAriel Levkovich } 234324da0016SAriel Levkovich 2344e126ba97SEli Cohen static struct ib_pd *mlx5_ib_alloc_pd(struct ib_device *ibdev, 2345e126ba97SEli Cohen struct ib_ucontext *context, 2346e126ba97SEli Cohen struct ib_udata *udata) 2347e126ba97SEli Cohen { 2348e126ba97SEli Cohen struct mlx5_ib_alloc_pd_resp resp; 2349e126ba97SEli Cohen struct mlx5_ib_pd *pd; 2350e126ba97SEli Cohen int err; 2351e126ba97SEli Cohen 2352e126ba97SEli Cohen pd = kmalloc(sizeof(*pd), GFP_KERNEL); 2353e126ba97SEli Cohen if (!pd) 2354e126ba97SEli Cohen return ERR_PTR(-ENOMEM); 2355e126ba97SEli Cohen 23569603b61dSJack Morgenstein err = mlx5_core_alloc_pd(to_mdev(ibdev)->mdev, &pd->pdn); 2357e126ba97SEli Cohen if (err) { 2358e126ba97SEli Cohen kfree(pd); 2359e126ba97SEli Cohen return ERR_PTR(err); 2360e126ba97SEli Cohen } 2361e126ba97SEli Cohen 2362e126ba97SEli Cohen if (context) { 2363e126ba97SEli Cohen resp.pdn = pd->pdn; 2364e126ba97SEli Cohen if (ib_copy_to_udata(udata, &resp, sizeof(resp))) { 23659603b61dSJack Morgenstein mlx5_core_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn); 2366e126ba97SEli Cohen kfree(pd); 2367e126ba97SEli Cohen return ERR_PTR(-EFAULT); 2368e126ba97SEli Cohen } 2369e126ba97SEli Cohen } 2370e126ba97SEli Cohen 2371e126ba97SEli Cohen return &pd->ibpd; 2372e126ba97SEli Cohen } 2373e126ba97SEli Cohen 2374e126ba97SEli Cohen static int mlx5_ib_dealloc_pd(struct ib_pd *pd) 2375e126ba97SEli Cohen { 2376e126ba97SEli Cohen struct mlx5_ib_dev *mdev = to_mdev(pd->device); 2377e126ba97SEli Cohen struct mlx5_ib_pd *mpd = to_mpd(pd); 2378e126ba97SEli Cohen 23799603b61dSJack Morgenstein mlx5_core_dealloc_pd(mdev->mdev, mpd->pdn); 2380e126ba97SEli Cohen kfree(mpd); 2381e126ba97SEli Cohen 2382e126ba97SEli Cohen return 0; 2383e126ba97SEli Cohen } 2384e126ba97SEli Cohen 2385466fa6d2SMaor Gottlieb enum { 2386466fa6d2SMaor Gottlieb MATCH_CRITERIA_ENABLE_OUTER_BIT, 2387466fa6d2SMaor Gottlieb MATCH_CRITERIA_ENABLE_MISC_BIT, 238871c6e863SAriel Levkovich MATCH_CRITERIA_ENABLE_INNER_BIT, 238971c6e863SAriel Levkovich MATCH_CRITERIA_ENABLE_MISC2_BIT 2390466fa6d2SMaor Gottlieb }; 2391038d2ef8SMaor Gottlieb 2392466fa6d2SMaor Gottlieb #define HEADER_IS_ZERO(match_criteria, headers) \ 2393466fa6d2SMaor Gottlieb !(memchr_inv(MLX5_ADDR_OF(fte_match_param, match_criteria, headers), \ 2394466fa6d2SMaor Gottlieb 0, MLX5_FLD_SZ_BYTES(fte_match_param, headers))) \ 2395466fa6d2SMaor Gottlieb 2396466fa6d2SMaor Gottlieb static u8 get_match_criteria_enable(u32 *match_criteria) 2397466fa6d2SMaor Gottlieb { 2398466fa6d2SMaor Gottlieb u8 match_criteria_enable; 2399466fa6d2SMaor Gottlieb 2400466fa6d2SMaor Gottlieb match_criteria_enable = 2401466fa6d2SMaor Gottlieb (!HEADER_IS_ZERO(match_criteria, outer_headers)) << 2402466fa6d2SMaor Gottlieb MATCH_CRITERIA_ENABLE_OUTER_BIT; 2403466fa6d2SMaor Gottlieb match_criteria_enable |= 2404466fa6d2SMaor Gottlieb (!HEADER_IS_ZERO(match_criteria, misc_parameters)) << 2405466fa6d2SMaor Gottlieb MATCH_CRITERIA_ENABLE_MISC_BIT; 2406466fa6d2SMaor Gottlieb match_criteria_enable |= 2407466fa6d2SMaor Gottlieb (!HEADER_IS_ZERO(match_criteria, inner_headers)) << 2408466fa6d2SMaor Gottlieb MATCH_CRITERIA_ENABLE_INNER_BIT; 240971c6e863SAriel Levkovich match_criteria_enable |= 241071c6e863SAriel Levkovich (!HEADER_IS_ZERO(match_criteria, misc_parameters_2)) << 241171c6e863SAriel Levkovich MATCH_CRITERIA_ENABLE_MISC2_BIT; 2412466fa6d2SMaor Gottlieb 2413466fa6d2SMaor Gottlieb return match_criteria_enable; 2414038d2ef8SMaor Gottlieb } 2415038d2ef8SMaor Gottlieb 2416ca0d4753SMaor Gottlieb static void set_proto(void *outer_c, void *outer_v, u8 mask, u8 val) 2417ca0d4753SMaor Gottlieb { 2418ca0d4753SMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_protocol, mask); 2419ca0d4753SMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_protocol, val); 2420ca0d4753SMaor Gottlieb } 2421ca0d4753SMaor Gottlieb 24222d1e697eSMoses Reuben static void set_flow_label(void *misc_c, void *misc_v, u8 mask, u8 val, 24232d1e697eSMoses Reuben bool inner) 24242d1e697eSMoses Reuben { 24252d1e697eSMoses Reuben if (inner) { 24262d1e697eSMoses Reuben MLX5_SET(fte_match_set_misc, 24272d1e697eSMoses Reuben misc_c, inner_ipv6_flow_label, mask); 24282d1e697eSMoses Reuben MLX5_SET(fte_match_set_misc, 24292d1e697eSMoses Reuben misc_v, inner_ipv6_flow_label, val); 24302d1e697eSMoses Reuben } else { 24312d1e697eSMoses Reuben MLX5_SET(fte_match_set_misc, 24322d1e697eSMoses Reuben misc_c, outer_ipv6_flow_label, mask); 24332d1e697eSMoses Reuben MLX5_SET(fte_match_set_misc, 24342d1e697eSMoses Reuben misc_v, outer_ipv6_flow_label, val); 24352d1e697eSMoses Reuben } 24362d1e697eSMoses Reuben } 24372d1e697eSMoses Reuben 2438ca0d4753SMaor Gottlieb static void set_tos(void *outer_c, void *outer_v, u8 mask, u8 val) 2439ca0d4753SMaor Gottlieb { 2440ca0d4753SMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_ecn, mask); 2441ca0d4753SMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_ecn, val); 2442ca0d4753SMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_dscp, mask >> 2); 2443ca0d4753SMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_dscp, val >> 2); 2444ca0d4753SMaor Gottlieb } 2445ca0d4753SMaor Gottlieb 244671c6e863SAriel Levkovich static int check_mpls_supp_fields(u32 field_support, const __be32 *set_mask) 244771c6e863SAriel Levkovich { 244871c6e863SAriel Levkovich if (MLX5_GET(fte_match_mpls, set_mask, mpls_label) && 244971c6e863SAriel Levkovich !(field_support & MLX5_FIELD_SUPPORT_MPLS_LABEL)) 245071c6e863SAriel Levkovich return -EOPNOTSUPP; 245171c6e863SAriel Levkovich 245271c6e863SAriel Levkovich if (MLX5_GET(fte_match_mpls, set_mask, mpls_exp) && 245371c6e863SAriel Levkovich !(field_support & MLX5_FIELD_SUPPORT_MPLS_EXP)) 245471c6e863SAriel Levkovich return -EOPNOTSUPP; 245571c6e863SAriel Levkovich 245671c6e863SAriel Levkovich if (MLX5_GET(fte_match_mpls, set_mask, mpls_s_bos) && 245771c6e863SAriel Levkovich !(field_support & MLX5_FIELD_SUPPORT_MPLS_S_BOS)) 245871c6e863SAriel Levkovich return -EOPNOTSUPP; 245971c6e863SAriel Levkovich 246071c6e863SAriel Levkovich if (MLX5_GET(fte_match_mpls, set_mask, mpls_ttl) && 246171c6e863SAriel Levkovich !(field_support & MLX5_FIELD_SUPPORT_MPLS_TTL)) 246271c6e863SAriel Levkovich return -EOPNOTSUPP; 246371c6e863SAriel Levkovich 246471c6e863SAriel Levkovich return 0; 246571c6e863SAriel Levkovich } 246671c6e863SAriel Levkovich 2467c47ac6aeSMaor Gottlieb #define LAST_ETH_FIELD vlan_tag 2468c47ac6aeSMaor Gottlieb #define LAST_IB_FIELD sl 2469ca0d4753SMaor Gottlieb #define LAST_IPV4_FIELD tos 2470466fa6d2SMaor Gottlieb #define LAST_IPV6_FIELD traffic_class 2471c47ac6aeSMaor Gottlieb #define LAST_TCP_UDP_FIELD src_port 2472ffb30d8fSMoses Reuben #define LAST_TUNNEL_FIELD tunnel_id 24732ac693f9SMoses Reuben #define LAST_FLOW_TAG_FIELD tag_id 2474a22ed86cSSlava Shwartsman #define LAST_DROP_FIELD size 2475da2f22aeSAriel Levkovich #define LAST_DROP_FIELD size 2476c47ac6aeSMaor Gottlieb 2477c47ac6aeSMaor Gottlieb /* Field is the last supported field */ 2478c47ac6aeSMaor Gottlieb #define FIELDS_NOT_SUPPORTED(filter, field)\ 2479c47ac6aeSMaor Gottlieb memchr_inv((void *)&filter.field +\ 2480c47ac6aeSMaor Gottlieb sizeof(filter.field), 0,\ 2481c47ac6aeSMaor Gottlieb sizeof(filter) -\ 2482c47ac6aeSMaor Gottlieb offsetof(typeof(filter), field) -\ 2483c47ac6aeSMaor Gottlieb sizeof(filter.field)) 2484c47ac6aeSMaor Gottlieb 2485802c2125SAviad Yehezkel static int parse_flow_flow_action(const union ib_flow_spec *ib_spec, 2486802c2125SAviad Yehezkel const struct ib_flow_attr *flow_attr, 2487802c2125SAviad Yehezkel struct mlx5_flow_act *action) 2488802c2125SAviad Yehezkel { 2489802c2125SAviad Yehezkel struct mlx5_ib_flow_action *maction = to_mflow_act(ib_spec->action.act); 2490802c2125SAviad Yehezkel 2491802c2125SAviad Yehezkel switch (maction->ib_action.type) { 2492802c2125SAviad Yehezkel case IB_FLOW_ACTION_ESP: 2493802c2125SAviad Yehezkel /* Currently only AES_GCM keymat is supported by the driver */ 2494802c2125SAviad Yehezkel action->esp_id = (uintptr_t)maction->esp_aes_gcm.ctx; 2495802c2125SAviad Yehezkel action->action |= flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS ? 2496802c2125SAviad Yehezkel MLX5_FLOW_CONTEXT_ACTION_ENCRYPT : 2497802c2125SAviad Yehezkel MLX5_FLOW_CONTEXT_ACTION_DECRYPT; 2498802c2125SAviad Yehezkel return 0; 2499802c2125SAviad Yehezkel default: 2500802c2125SAviad Yehezkel return -EOPNOTSUPP; 2501802c2125SAviad Yehezkel } 2502802c2125SAviad Yehezkel } 2503802c2125SAviad Yehezkel 250419cc7524SAriel Levkovich static int parse_flow_attr(struct mlx5_core_dev *mdev, u32 *match_c, 250519cc7524SAriel Levkovich u32 *match_v, const union ib_flow_spec *ib_spec, 2506802c2125SAviad Yehezkel const struct ib_flow_attr *flow_attr, 250771c6e863SAriel Levkovich struct mlx5_flow_act *action, u32 prev_type) 2508038d2ef8SMaor Gottlieb { 2509466fa6d2SMaor Gottlieb void *misc_params_c = MLX5_ADDR_OF(fte_match_param, match_c, 2510466fa6d2SMaor Gottlieb misc_parameters); 2511466fa6d2SMaor Gottlieb void *misc_params_v = MLX5_ADDR_OF(fte_match_param, match_v, 2512466fa6d2SMaor Gottlieb misc_parameters); 251371c6e863SAriel Levkovich void *misc_params2_c = MLX5_ADDR_OF(fte_match_param, match_c, 251471c6e863SAriel Levkovich misc_parameters_2); 251571c6e863SAriel Levkovich void *misc_params2_v = MLX5_ADDR_OF(fte_match_param, match_v, 251671c6e863SAriel Levkovich misc_parameters_2); 25172d1e697eSMoses Reuben void *headers_c; 25182d1e697eSMoses Reuben void *headers_v; 251919cc7524SAriel Levkovich int match_ipv; 2520802c2125SAviad Yehezkel int ret; 2521466fa6d2SMaor Gottlieb 25222d1e697eSMoses Reuben if (ib_spec->type & IB_FLOW_SPEC_INNER) { 25232d1e697eSMoses Reuben headers_c = MLX5_ADDR_OF(fte_match_param, match_c, 25242d1e697eSMoses Reuben inner_headers); 25252d1e697eSMoses Reuben headers_v = MLX5_ADDR_OF(fte_match_param, match_v, 25262d1e697eSMoses Reuben inner_headers); 252719cc7524SAriel Levkovich match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev, 252819cc7524SAriel Levkovich ft_field_support.inner_ip_version); 25292d1e697eSMoses Reuben } else { 25302d1e697eSMoses Reuben headers_c = MLX5_ADDR_OF(fte_match_param, match_c, 25312d1e697eSMoses Reuben outer_headers); 25322d1e697eSMoses Reuben headers_v = MLX5_ADDR_OF(fte_match_param, match_v, 25332d1e697eSMoses Reuben outer_headers); 253419cc7524SAriel Levkovich match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev, 253519cc7524SAriel Levkovich ft_field_support.outer_ip_version); 25362d1e697eSMoses Reuben } 25372d1e697eSMoses Reuben 25382d1e697eSMoses Reuben switch (ib_spec->type & ~IB_FLOW_SPEC_INNER) { 2539038d2ef8SMaor Gottlieb case IB_FLOW_SPEC_ETH: 2540c47ac6aeSMaor Gottlieb if (FIELDS_NOT_SUPPORTED(ib_spec->eth.mask, LAST_ETH_FIELD)) 25411ffd3a26SLeon Romanovsky return -EOPNOTSUPP; 2542038d2ef8SMaor Gottlieb 25432d1e697eSMoses Reuben ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c, 2544038d2ef8SMaor Gottlieb dmac_47_16), 2545038d2ef8SMaor Gottlieb ib_spec->eth.mask.dst_mac); 25462d1e697eSMoses Reuben ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v, 2547038d2ef8SMaor Gottlieb dmac_47_16), 2548038d2ef8SMaor Gottlieb ib_spec->eth.val.dst_mac); 2549038d2ef8SMaor Gottlieb 25502d1e697eSMoses Reuben ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c, 2551ee3da804SMaor Gottlieb smac_47_16), 2552ee3da804SMaor Gottlieb ib_spec->eth.mask.src_mac); 25532d1e697eSMoses Reuben ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v, 2554ee3da804SMaor Gottlieb smac_47_16), 2555ee3da804SMaor Gottlieb ib_spec->eth.val.src_mac); 2556ee3da804SMaor Gottlieb 2557038d2ef8SMaor Gottlieb if (ib_spec->eth.mask.vlan_tag) { 25582d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_c, 255910543365SMohamad Haj Yahia cvlan_tag, 1); 25602d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_v, 256110543365SMohamad Haj Yahia cvlan_tag, 1); 2562038d2ef8SMaor Gottlieb 25632d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_c, 2564038d2ef8SMaor Gottlieb first_vid, ntohs(ib_spec->eth.mask.vlan_tag)); 25652d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_v, 2566038d2ef8SMaor Gottlieb first_vid, ntohs(ib_spec->eth.val.vlan_tag)); 2567038d2ef8SMaor Gottlieb 25682d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_c, 2569038d2ef8SMaor Gottlieb first_cfi, 2570038d2ef8SMaor Gottlieb ntohs(ib_spec->eth.mask.vlan_tag) >> 12); 25712d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_v, 2572038d2ef8SMaor Gottlieb first_cfi, 2573038d2ef8SMaor Gottlieb ntohs(ib_spec->eth.val.vlan_tag) >> 12); 2574038d2ef8SMaor Gottlieb 25752d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_c, 2576038d2ef8SMaor Gottlieb first_prio, 2577038d2ef8SMaor Gottlieb ntohs(ib_spec->eth.mask.vlan_tag) >> 13); 25782d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_v, 2579038d2ef8SMaor Gottlieb first_prio, 2580038d2ef8SMaor Gottlieb ntohs(ib_spec->eth.val.vlan_tag) >> 13); 2581038d2ef8SMaor Gottlieb } 25822d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_c, 2583038d2ef8SMaor Gottlieb ethertype, ntohs(ib_spec->eth.mask.ether_type)); 25842d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_v, 2585038d2ef8SMaor Gottlieb ethertype, ntohs(ib_spec->eth.val.ether_type)); 2586038d2ef8SMaor Gottlieb break; 2587038d2ef8SMaor Gottlieb case IB_FLOW_SPEC_IPV4: 2588c47ac6aeSMaor Gottlieb if (FIELDS_NOT_SUPPORTED(ib_spec->ipv4.mask, LAST_IPV4_FIELD)) 25891ffd3a26SLeon Romanovsky return -EOPNOTSUPP; 2590038d2ef8SMaor Gottlieb 259119cc7524SAriel Levkovich if (match_ipv) { 259219cc7524SAriel Levkovich MLX5_SET(fte_match_set_lyr_2_4, headers_c, 259319cc7524SAriel Levkovich ip_version, 0xf); 259419cc7524SAriel Levkovich MLX5_SET(fte_match_set_lyr_2_4, headers_v, 25953346c487SBoris Pismenny ip_version, MLX5_FS_IPV4_VERSION); 259619cc7524SAriel Levkovich } else { 25972d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_c, 2598038d2ef8SMaor Gottlieb ethertype, 0xffff); 25992d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_v, 2600038d2ef8SMaor Gottlieb ethertype, ETH_P_IP); 260119cc7524SAriel Levkovich } 2602038d2ef8SMaor Gottlieb 26032d1e697eSMoses Reuben memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c, 2604038d2ef8SMaor Gottlieb src_ipv4_src_ipv6.ipv4_layout.ipv4), 2605038d2ef8SMaor Gottlieb &ib_spec->ipv4.mask.src_ip, 2606038d2ef8SMaor Gottlieb sizeof(ib_spec->ipv4.mask.src_ip)); 26072d1e697eSMoses Reuben memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v, 2608038d2ef8SMaor Gottlieb src_ipv4_src_ipv6.ipv4_layout.ipv4), 2609038d2ef8SMaor Gottlieb &ib_spec->ipv4.val.src_ip, 2610038d2ef8SMaor Gottlieb sizeof(ib_spec->ipv4.val.src_ip)); 26112d1e697eSMoses Reuben memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c, 2612038d2ef8SMaor Gottlieb dst_ipv4_dst_ipv6.ipv4_layout.ipv4), 2613038d2ef8SMaor Gottlieb &ib_spec->ipv4.mask.dst_ip, 2614038d2ef8SMaor Gottlieb sizeof(ib_spec->ipv4.mask.dst_ip)); 26152d1e697eSMoses Reuben memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v, 2616038d2ef8SMaor Gottlieb dst_ipv4_dst_ipv6.ipv4_layout.ipv4), 2617038d2ef8SMaor Gottlieb &ib_spec->ipv4.val.dst_ip, 2618038d2ef8SMaor Gottlieb sizeof(ib_spec->ipv4.val.dst_ip)); 2619ca0d4753SMaor Gottlieb 26202d1e697eSMoses Reuben set_tos(headers_c, headers_v, 2621ca0d4753SMaor Gottlieb ib_spec->ipv4.mask.tos, ib_spec->ipv4.val.tos); 2622ca0d4753SMaor Gottlieb 26232d1e697eSMoses Reuben set_proto(headers_c, headers_v, 2624ca0d4753SMaor Gottlieb ib_spec->ipv4.mask.proto, ib_spec->ipv4.val.proto); 2625038d2ef8SMaor Gottlieb break; 2626026bae0cSMaor Gottlieb case IB_FLOW_SPEC_IPV6: 2627c47ac6aeSMaor Gottlieb if (FIELDS_NOT_SUPPORTED(ib_spec->ipv6.mask, LAST_IPV6_FIELD)) 26281ffd3a26SLeon Romanovsky return -EOPNOTSUPP; 2629026bae0cSMaor Gottlieb 263019cc7524SAriel Levkovich if (match_ipv) { 263119cc7524SAriel Levkovich MLX5_SET(fte_match_set_lyr_2_4, headers_c, 263219cc7524SAriel Levkovich ip_version, 0xf); 263319cc7524SAriel Levkovich MLX5_SET(fte_match_set_lyr_2_4, headers_v, 26343346c487SBoris Pismenny ip_version, MLX5_FS_IPV6_VERSION); 263519cc7524SAriel Levkovich } else { 26362d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_c, 2637026bae0cSMaor Gottlieb ethertype, 0xffff); 26382d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_v, 2639026bae0cSMaor Gottlieb ethertype, ETH_P_IPV6); 264019cc7524SAriel Levkovich } 2641026bae0cSMaor Gottlieb 26422d1e697eSMoses Reuben memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c, 2643026bae0cSMaor Gottlieb src_ipv4_src_ipv6.ipv6_layout.ipv6), 2644026bae0cSMaor Gottlieb &ib_spec->ipv6.mask.src_ip, 2645026bae0cSMaor Gottlieb sizeof(ib_spec->ipv6.mask.src_ip)); 26462d1e697eSMoses Reuben memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v, 2647026bae0cSMaor Gottlieb src_ipv4_src_ipv6.ipv6_layout.ipv6), 2648026bae0cSMaor Gottlieb &ib_spec->ipv6.val.src_ip, 2649026bae0cSMaor Gottlieb sizeof(ib_spec->ipv6.val.src_ip)); 26502d1e697eSMoses Reuben memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c, 2651026bae0cSMaor Gottlieb dst_ipv4_dst_ipv6.ipv6_layout.ipv6), 2652026bae0cSMaor Gottlieb &ib_spec->ipv6.mask.dst_ip, 2653026bae0cSMaor Gottlieb sizeof(ib_spec->ipv6.mask.dst_ip)); 26542d1e697eSMoses Reuben memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v, 2655026bae0cSMaor Gottlieb dst_ipv4_dst_ipv6.ipv6_layout.ipv6), 2656026bae0cSMaor Gottlieb &ib_spec->ipv6.val.dst_ip, 2657026bae0cSMaor Gottlieb sizeof(ib_spec->ipv6.val.dst_ip)); 2658466fa6d2SMaor Gottlieb 26592d1e697eSMoses Reuben set_tos(headers_c, headers_v, 2660466fa6d2SMaor Gottlieb ib_spec->ipv6.mask.traffic_class, 2661466fa6d2SMaor Gottlieb ib_spec->ipv6.val.traffic_class); 2662466fa6d2SMaor Gottlieb 26632d1e697eSMoses Reuben set_proto(headers_c, headers_v, 2664466fa6d2SMaor Gottlieb ib_spec->ipv6.mask.next_hdr, 2665466fa6d2SMaor Gottlieb ib_spec->ipv6.val.next_hdr); 2666466fa6d2SMaor Gottlieb 26672d1e697eSMoses Reuben set_flow_label(misc_params_c, misc_params_v, 26682d1e697eSMoses Reuben ntohl(ib_spec->ipv6.mask.flow_label), 26692d1e697eSMoses Reuben ntohl(ib_spec->ipv6.val.flow_label), 26702d1e697eSMoses Reuben ib_spec->type & IB_FLOW_SPEC_INNER); 2671802c2125SAviad Yehezkel break; 2672802c2125SAviad Yehezkel case IB_FLOW_SPEC_ESP: 2673802c2125SAviad Yehezkel if (ib_spec->esp.mask.seq) 2674802c2125SAviad Yehezkel return -EOPNOTSUPP; 26752d1e697eSMoses Reuben 2676802c2125SAviad Yehezkel MLX5_SET(fte_match_set_misc, misc_params_c, outer_esp_spi, 2677802c2125SAviad Yehezkel ntohl(ib_spec->esp.mask.spi)); 2678802c2125SAviad Yehezkel MLX5_SET(fte_match_set_misc, misc_params_v, outer_esp_spi, 2679802c2125SAviad Yehezkel ntohl(ib_spec->esp.val.spi)); 2680026bae0cSMaor Gottlieb break; 2681038d2ef8SMaor Gottlieb case IB_FLOW_SPEC_TCP: 2682c47ac6aeSMaor Gottlieb if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask, 2683c47ac6aeSMaor Gottlieb LAST_TCP_UDP_FIELD)) 26841ffd3a26SLeon Romanovsky return -EOPNOTSUPP; 2685038d2ef8SMaor Gottlieb 26862d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol, 2687038d2ef8SMaor Gottlieb 0xff); 26882d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol, 2689038d2ef8SMaor Gottlieb IPPROTO_TCP); 2690038d2ef8SMaor Gottlieb 26912d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_sport, 2692038d2ef8SMaor Gottlieb ntohs(ib_spec->tcp_udp.mask.src_port)); 26932d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_sport, 2694038d2ef8SMaor Gottlieb ntohs(ib_spec->tcp_udp.val.src_port)); 2695038d2ef8SMaor Gottlieb 26962d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_dport, 2697038d2ef8SMaor Gottlieb ntohs(ib_spec->tcp_udp.mask.dst_port)); 26982d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_dport, 2699038d2ef8SMaor Gottlieb ntohs(ib_spec->tcp_udp.val.dst_port)); 2700038d2ef8SMaor Gottlieb break; 2701038d2ef8SMaor Gottlieb case IB_FLOW_SPEC_UDP: 2702c47ac6aeSMaor Gottlieb if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask, 2703c47ac6aeSMaor Gottlieb LAST_TCP_UDP_FIELD)) 27041ffd3a26SLeon Romanovsky return -EOPNOTSUPP; 2705038d2ef8SMaor Gottlieb 27062d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol, 2707038d2ef8SMaor Gottlieb 0xff); 27082d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol, 2709038d2ef8SMaor Gottlieb IPPROTO_UDP); 2710038d2ef8SMaor Gottlieb 27112d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_sport, 2712038d2ef8SMaor Gottlieb ntohs(ib_spec->tcp_udp.mask.src_port)); 27132d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_sport, 2714038d2ef8SMaor Gottlieb ntohs(ib_spec->tcp_udp.val.src_port)); 2715038d2ef8SMaor Gottlieb 27162d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_dport, 2717038d2ef8SMaor Gottlieb ntohs(ib_spec->tcp_udp.mask.dst_port)); 27182d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport, 2719038d2ef8SMaor Gottlieb ntohs(ib_spec->tcp_udp.val.dst_port)); 2720038d2ef8SMaor Gottlieb break; 2721da2f22aeSAriel Levkovich case IB_FLOW_SPEC_GRE: 2722da2f22aeSAriel Levkovich if (ib_spec->gre.mask.c_ks_res0_ver) 2723da2f22aeSAriel Levkovich return -EOPNOTSUPP; 2724da2f22aeSAriel Levkovich 2725da2f22aeSAriel Levkovich MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol, 2726da2f22aeSAriel Levkovich 0xff); 2727da2f22aeSAriel Levkovich MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol, 2728da2f22aeSAriel Levkovich IPPROTO_GRE); 2729da2f22aeSAriel Levkovich 2730da2f22aeSAriel Levkovich MLX5_SET(fte_match_set_misc, misc_params_c, gre_protocol, 2731da2f22aeSAriel Levkovich 0xffff); 2732da2f22aeSAriel Levkovich MLX5_SET(fte_match_set_misc, misc_params_v, gre_protocol, 2733da2f22aeSAriel Levkovich ntohs(ib_spec->gre.val.protocol)); 2734da2f22aeSAriel Levkovich 2735da2f22aeSAriel Levkovich memcpy(MLX5_ADDR_OF(fte_match_set_misc, misc_params_c, 2736da2f22aeSAriel Levkovich gre_key_h), 2737da2f22aeSAriel Levkovich &ib_spec->gre.mask.key, 2738da2f22aeSAriel Levkovich sizeof(ib_spec->gre.mask.key)); 2739da2f22aeSAriel Levkovich memcpy(MLX5_ADDR_OF(fte_match_set_misc, misc_params_v, 2740da2f22aeSAriel Levkovich gre_key_h), 2741da2f22aeSAriel Levkovich &ib_spec->gre.val.key, 2742da2f22aeSAriel Levkovich sizeof(ib_spec->gre.val.key)); 2743da2f22aeSAriel Levkovich break; 274471c6e863SAriel Levkovich case IB_FLOW_SPEC_MPLS: 274571c6e863SAriel Levkovich switch (prev_type) { 274671c6e863SAriel Levkovich case IB_FLOW_SPEC_UDP: 274771c6e863SAriel Levkovich if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev, 274871c6e863SAriel Levkovich ft_field_support.outer_first_mpls_over_udp), 274971c6e863SAriel Levkovich &ib_spec->mpls.mask.tag)) 275071c6e863SAriel Levkovich return -EOPNOTSUPP; 275171c6e863SAriel Levkovich 275271c6e863SAriel Levkovich memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v, 275371c6e863SAriel Levkovich outer_first_mpls_over_udp), 275471c6e863SAriel Levkovich &ib_spec->mpls.val.tag, 275571c6e863SAriel Levkovich sizeof(ib_spec->mpls.val.tag)); 275671c6e863SAriel Levkovich memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c, 275771c6e863SAriel Levkovich outer_first_mpls_over_udp), 275871c6e863SAriel Levkovich &ib_spec->mpls.mask.tag, 275971c6e863SAriel Levkovich sizeof(ib_spec->mpls.mask.tag)); 276071c6e863SAriel Levkovich break; 276171c6e863SAriel Levkovich case IB_FLOW_SPEC_GRE: 276271c6e863SAriel Levkovich if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev, 276371c6e863SAriel Levkovich ft_field_support.outer_first_mpls_over_gre), 276471c6e863SAriel Levkovich &ib_spec->mpls.mask.tag)) 276571c6e863SAriel Levkovich return -EOPNOTSUPP; 276671c6e863SAriel Levkovich 276771c6e863SAriel Levkovich memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v, 276871c6e863SAriel Levkovich outer_first_mpls_over_gre), 276971c6e863SAriel Levkovich &ib_spec->mpls.val.tag, 277071c6e863SAriel Levkovich sizeof(ib_spec->mpls.val.tag)); 277171c6e863SAriel Levkovich memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c, 277271c6e863SAriel Levkovich outer_first_mpls_over_gre), 277371c6e863SAriel Levkovich &ib_spec->mpls.mask.tag, 277471c6e863SAriel Levkovich sizeof(ib_spec->mpls.mask.tag)); 277571c6e863SAriel Levkovich break; 277671c6e863SAriel Levkovich default: 277771c6e863SAriel Levkovich if (ib_spec->type & IB_FLOW_SPEC_INNER) { 277871c6e863SAriel Levkovich if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev, 277971c6e863SAriel Levkovich ft_field_support.inner_first_mpls), 278071c6e863SAriel Levkovich &ib_spec->mpls.mask.tag)) 278171c6e863SAriel Levkovich return -EOPNOTSUPP; 278271c6e863SAriel Levkovich 278371c6e863SAriel Levkovich memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v, 278471c6e863SAriel Levkovich inner_first_mpls), 278571c6e863SAriel Levkovich &ib_spec->mpls.val.tag, 278671c6e863SAriel Levkovich sizeof(ib_spec->mpls.val.tag)); 278771c6e863SAriel Levkovich memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c, 278871c6e863SAriel Levkovich inner_first_mpls), 278971c6e863SAriel Levkovich &ib_spec->mpls.mask.tag, 279071c6e863SAriel Levkovich sizeof(ib_spec->mpls.mask.tag)); 279171c6e863SAriel Levkovich } else { 279271c6e863SAriel Levkovich if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev, 279371c6e863SAriel Levkovich ft_field_support.outer_first_mpls), 279471c6e863SAriel Levkovich &ib_spec->mpls.mask.tag)) 279571c6e863SAriel Levkovich return -EOPNOTSUPP; 279671c6e863SAriel Levkovich 279771c6e863SAriel Levkovich memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v, 279871c6e863SAriel Levkovich outer_first_mpls), 279971c6e863SAriel Levkovich &ib_spec->mpls.val.tag, 280071c6e863SAriel Levkovich sizeof(ib_spec->mpls.val.tag)); 280171c6e863SAriel Levkovich memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c, 280271c6e863SAriel Levkovich outer_first_mpls), 280371c6e863SAriel Levkovich &ib_spec->mpls.mask.tag, 280471c6e863SAriel Levkovich sizeof(ib_spec->mpls.mask.tag)); 280571c6e863SAriel Levkovich } 280671c6e863SAriel Levkovich } 280771c6e863SAriel Levkovich break; 2808ffb30d8fSMoses Reuben case IB_FLOW_SPEC_VXLAN_TUNNEL: 2809ffb30d8fSMoses Reuben if (FIELDS_NOT_SUPPORTED(ib_spec->tunnel.mask, 2810ffb30d8fSMoses Reuben LAST_TUNNEL_FIELD)) 28111ffd3a26SLeon Romanovsky return -EOPNOTSUPP; 2812ffb30d8fSMoses Reuben 2813ffb30d8fSMoses Reuben MLX5_SET(fte_match_set_misc, misc_params_c, vxlan_vni, 2814ffb30d8fSMoses Reuben ntohl(ib_spec->tunnel.mask.tunnel_id)); 2815ffb30d8fSMoses Reuben MLX5_SET(fte_match_set_misc, misc_params_v, vxlan_vni, 2816ffb30d8fSMoses Reuben ntohl(ib_spec->tunnel.val.tunnel_id)); 2817ffb30d8fSMoses Reuben break; 28182ac693f9SMoses Reuben case IB_FLOW_SPEC_ACTION_TAG: 28192ac693f9SMoses Reuben if (FIELDS_NOT_SUPPORTED(ib_spec->flow_tag, 28202ac693f9SMoses Reuben LAST_FLOW_TAG_FIELD)) 28212ac693f9SMoses Reuben return -EOPNOTSUPP; 28222ac693f9SMoses Reuben if (ib_spec->flow_tag.tag_id >= BIT(24)) 28232ac693f9SMoses Reuben return -EINVAL; 28242ac693f9SMoses Reuben 2825075572d4SBoris Pismenny action->flow_tag = ib_spec->flow_tag.tag_id; 2826a9db0ecfSMatan Barak action->has_flow_tag = true; 28272ac693f9SMoses Reuben break; 2828a22ed86cSSlava Shwartsman case IB_FLOW_SPEC_ACTION_DROP: 2829a22ed86cSSlava Shwartsman if (FIELDS_NOT_SUPPORTED(ib_spec->drop, 2830a22ed86cSSlava Shwartsman LAST_DROP_FIELD)) 2831a22ed86cSSlava Shwartsman return -EOPNOTSUPP; 2832075572d4SBoris Pismenny action->action |= MLX5_FLOW_CONTEXT_ACTION_DROP; 2833a22ed86cSSlava Shwartsman break; 2834802c2125SAviad Yehezkel case IB_FLOW_SPEC_ACTION_HANDLE: 2835802c2125SAviad Yehezkel ret = parse_flow_flow_action(ib_spec, flow_attr, action); 2836802c2125SAviad Yehezkel if (ret) 2837802c2125SAviad Yehezkel return ret; 2838802c2125SAviad Yehezkel break; 2839038d2ef8SMaor Gottlieb default: 2840038d2ef8SMaor Gottlieb return -EINVAL; 2841038d2ef8SMaor Gottlieb } 2842038d2ef8SMaor Gottlieb 2843038d2ef8SMaor Gottlieb return 0; 2844038d2ef8SMaor Gottlieb } 2845038d2ef8SMaor Gottlieb 2846038d2ef8SMaor Gottlieb /* If a flow could catch both multicast and unicast packets, 2847038d2ef8SMaor Gottlieb * it won't fall into the multicast flow steering table and this rule 2848038d2ef8SMaor Gottlieb * could steal other multicast packets. 2849038d2ef8SMaor Gottlieb */ 2850a550ddfcSYishai Hadas static bool flow_is_multicast_only(const struct ib_flow_attr *ib_attr) 2851038d2ef8SMaor Gottlieb { 285281e30880SYishai Hadas union ib_flow_spec *flow_spec; 2853038d2ef8SMaor Gottlieb 2854038d2ef8SMaor Gottlieb if (ib_attr->type != IB_FLOW_ATTR_NORMAL || 2855038d2ef8SMaor Gottlieb ib_attr->num_of_specs < 1) 2856038d2ef8SMaor Gottlieb return false; 2857038d2ef8SMaor Gottlieb 285881e30880SYishai Hadas flow_spec = (union ib_flow_spec *)(ib_attr + 1); 285981e30880SYishai Hadas if (flow_spec->type == IB_FLOW_SPEC_IPV4) { 286081e30880SYishai Hadas struct ib_flow_spec_ipv4 *ipv4_spec; 2861038d2ef8SMaor Gottlieb 286281e30880SYishai Hadas ipv4_spec = (struct ib_flow_spec_ipv4 *)flow_spec; 286381e30880SYishai Hadas if (ipv4_is_multicast(ipv4_spec->val.dst_ip)) 286481e30880SYishai Hadas return true; 286581e30880SYishai Hadas 286681e30880SYishai Hadas return false; 286781e30880SYishai Hadas } 286881e30880SYishai Hadas 286981e30880SYishai Hadas if (flow_spec->type == IB_FLOW_SPEC_ETH) { 287081e30880SYishai Hadas struct ib_flow_spec_eth *eth_spec; 287181e30880SYishai Hadas 287281e30880SYishai Hadas eth_spec = (struct ib_flow_spec_eth *)flow_spec; 2873038d2ef8SMaor Gottlieb return is_multicast_ether_addr(eth_spec->mask.dst_mac) && 2874038d2ef8SMaor Gottlieb is_multicast_ether_addr(eth_spec->val.dst_mac); 2875038d2ef8SMaor Gottlieb } 2876038d2ef8SMaor Gottlieb 287781e30880SYishai Hadas return false; 287881e30880SYishai Hadas } 287981e30880SYishai Hadas 2880802c2125SAviad Yehezkel enum valid_spec { 2881802c2125SAviad Yehezkel VALID_SPEC_INVALID, 2882802c2125SAviad Yehezkel VALID_SPEC_VALID, 2883802c2125SAviad Yehezkel VALID_SPEC_NA, 2884802c2125SAviad Yehezkel }; 2885802c2125SAviad Yehezkel 2886802c2125SAviad Yehezkel static enum valid_spec 2887802c2125SAviad Yehezkel is_valid_esp_aes_gcm(struct mlx5_core_dev *mdev, 2888802c2125SAviad Yehezkel const struct mlx5_flow_spec *spec, 2889802c2125SAviad Yehezkel const struct mlx5_flow_act *flow_act, 2890802c2125SAviad Yehezkel bool egress) 2891802c2125SAviad Yehezkel { 2892802c2125SAviad Yehezkel const u32 *match_c = spec->match_criteria; 2893802c2125SAviad Yehezkel bool is_crypto = 2894802c2125SAviad Yehezkel (flow_act->action & (MLX5_FLOW_CONTEXT_ACTION_ENCRYPT | 2895802c2125SAviad Yehezkel MLX5_FLOW_CONTEXT_ACTION_DECRYPT)); 2896802c2125SAviad Yehezkel bool is_ipsec = mlx5_fs_is_ipsec_flow(match_c); 2897802c2125SAviad Yehezkel bool is_drop = flow_act->action & MLX5_FLOW_CONTEXT_ACTION_DROP; 2898802c2125SAviad Yehezkel 2899802c2125SAviad Yehezkel /* 2900802c2125SAviad Yehezkel * Currently only crypto is supported in egress, when regular egress 2901802c2125SAviad Yehezkel * rules would be supported, always return VALID_SPEC_NA. 2902802c2125SAviad Yehezkel */ 2903802c2125SAviad Yehezkel if (!is_crypto) 2904802c2125SAviad Yehezkel return egress ? VALID_SPEC_INVALID : VALID_SPEC_NA; 2905802c2125SAviad Yehezkel 2906802c2125SAviad Yehezkel return is_crypto && is_ipsec && 2907802c2125SAviad Yehezkel (!egress || (!is_drop && !flow_act->has_flow_tag)) ? 2908802c2125SAviad Yehezkel VALID_SPEC_VALID : VALID_SPEC_INVALID; 2909802c2125SAviad Yehezkel } 2910802c2125SAviad Yehezkel 2911802c2125SAviad Yehezkel static bool is_valid_spec(struct mlx5_core_dev *mdev, 2912802c2125SAviad Yehezkel const struct mlx5_flow_spec *spec, 2913802c2125SAviad Yehezkel const struct mlx5_flow_act *flow_act, 2914802c2125SAviad Yehezkel bool egress) 2915802c2125SAviad Yehezkel { 2916802c2125SAviad Yehezkel /* We curretly only support ipsec egress flow */ 2917802c2125SAviad Yehezkel return is_valid_esp_aes_gcm(mdev, spec, flow_act, egress) != VALID_SPEC_INVALID; 2918802c2125SAviad Yehezkel } 2919802c2125SAviad Yehezkel 292019cc7524SAriel Levkovich static bool is_valid_ethertype(struct mlx5_core_dev *mdev, 292119cc7524SAriel Levkovich const struct ib_flow_attr *flow_attr, 29220f750966SAriel Levkovich bool check_inner) 2923038d2ef8SMaor Gottlieb { 2924038d2ef8SMaor Gottlieb union ib_flow_spec *ib_spec = (union ib_flow_spec *)(flow_attr + 1); 292519cc7524SAriel Levkovich int match_ipv = check_inner ? 292619cc7524SAriel Levkovich MLX5_CAP_FLOWTABLE_NIC_RX(mdev, 292719cc7524SAriel Levkovich ft_field_support.inner_ip_version) : 292819cc7524SAriel Levkovich MLX5_CAP_FLOWTABLE_NIC_RX(mdev, 292919cc7524SAriel Levkovich ft_field_support.outer_ip_version); 29300f750966SAriel Levkovich int inner_bit = check_inner ? IB_FLOW_SPEC_INNER : 0; 29310f750966SAriel Levkovich bool ipv4_spec_valid, ipv6_spec_valid; 29320f750966SAriel Levkovich unsigned int ip_spec_type = 0; 29330f750966SAriel Levkovich bool has_ethertype = false; 2934038d2ef8SMaor Gottlieb unsigned int spec_index; 29350f750966SAriel Levkovich bool mask_valid = true; 29360f750966SAriel Levkovich u16 eth_type = 0; 29370f750966SAriel Levkovich bool type_valid; 2938038d2ef8SMaor Gottlieb 2939038d2ef8SMaor Gottlieb /* Validate that ethertype is correct */ 2940038d2ef8SMaor Gottlieb for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) { 29410f750966SAriel Levkovich if ((ib_spec->type == (IB_FLOW_SPEC_ETH | inner_bit)) && 2942038d2ef8SMaor Gottlieb ib_spec->eth.mask.ether_type) { 29430f750966SAriel Levkovich mask_valid = (ib_spec->eth.mask.ether_type == 29440f750966SAriel Levkovich htons(0xffff)); 29450f750966SAriel Levkovich has_ethertype = true; 29460f750966SAriel Levkovich eth_type = ntohs(ib_spec->eth.val.ether_type); 29470f750966SAriel Levkovich } else if ((ib_spec->type == (IB_FLOW_SPEC_IPV4 | inner_bit)) || 29480f750966SAriel Levkovich (ib_spec->type == (IB_FLOW_SPEC_IPV6 | inner_bit))) { 29490f750966SAriel Levkovich ip_spec_type = ib_spec->type; 2950038d2ef8SMaor Gottlieb } 2951038d2ef8SMaor Gottlieb ib_spec = (void *)ib_spec + ib_spec->size; 2952038d2ef8SMaor Gottlieb } 29530f750966SAriel Levkovich 29540f750966SAriel Levkovich type_valid = (!has_ethertype) || (!ip_spec_type); 29550f750966SAriel Levkovich if (!type_valid && mask_valid) { 29560f750966SAriel Levkovich ipv4_spec_valid = (eth_type == ETH_P_IP) && 29570f750966SAriel Levkovich (ip_spec_type == (IB_FLOW_SPEC_IPV4 | inner_bit)); 29580f750966SAriel Levkovich ipv6_spec_valid = (eth_type == ETH_P_IPV6) && 29590f750966SAriel Levkovich (ip_spec_type == (IB_FLOW_SPEC_IPV6 | inner_bit)); 296019cc7524SAriel Levkovich 296119cc7524SAriel Levkovich type_valid = (ipv4_spec_valid) || (ipv6_spec_valid) || 296219cc7524SAriel Levkovich (((eth_type == ETH_P_MPLS_UC) || 296319cc7524SAriel Levkovich (eth_type == ETH_P_MPLS_MC)) && match_ipv); 29640f750966SAriel Levkovich } 29650f750966SAriel Levkovich 29660f750966SAriel Levkovich return type_valid; 29670f750966SAriel Levkovich } 29680f750966SAriel Levkovich 296919cc7524SAriel Levkovich static bool is_valid_attr(struct mlx5_core_dev *mdev, 297019cc7524SAriel Levkovich const struct ib_flow_attr *flow_attr) 29710f750966SAriel Levkovich { 297219cc7524SAriel Levkovich return is_valid_ethertype(mdev, flow_attr, false) && 297319cc7524SAriel Levkovich is_valid_ethertype(mdev, flow_attr, true); 2974038d2ef8SMaor Gottlieb } 2975038d2ef8SMaor Gottlieb 2976038d2ef8SMaor Gottlieb static void put_flow_table(struct mlx5_ib_dev *dev, 2977038d2ef8SMaor Gottlieb struct mlx5_ib_flow_prio *prio, bool ft_added) 2978038d2ef8SMaor Gottlieb { 2979038d2ef8SMaor Gottlieb prio->refcount -= !!ft_added; 2980038d2ef8SMaor Gottlieb if (!prio->refcount) { 2981038d2ef8SMaor Gottlieb mlx5_destroy_flow_table(prio->flow_table); 2982038d2ef8SMaor Gottlieb prio->flow_table = NULL; 2983038d2ef8SMaor Gottlieb } 2984038d2ef8SMaor Gottlieb } 2985038d2ef8SMaor Gottlieb 2986038d2ef8SMaor Gottlieb static int mlx5_ib_destroy_flow(struct ib_flow *flow_id) 2987038d2ef8SMaor Gottlieb { 2988038d2ef8SMaor Gottlieb struct mlx5_ib_dev *dev = to_mdev(flow_id->qp->device); 2989038d2ef8SMaor Gottlieb struct mlx5_ib_flow_handler *handler = container_of(flow_id, 2990038d2ef8SMaor Gottlieb struct mlx5_ib_flow_handler, 2991038d2ef8SMaor Gottlieb ibflow); 2992038d2ef8SMaor Gottlieb struct mlx5_ib_flow_handler *iter, *tmp; 2993038d2ef8SMaor Gottlieb 29949a4ca38dSMark Bloch mutex_lock(&dev->flow_db->lock); 2995038d2ef8SMaor Gottlieb 2996038d2ef8SMaor Gottlieb list_for_each_entry_safe(iter, tmp, &handler->list, list) { 299774491de9SMark Bloch mlx5_del_flow_rules(iter->rule); 2998cc0e5d42SMaor Gottlieb put_flow_table(dev, iter->prio, true); 2999038d2ef8SMaor Gottlieb list_del(&iter->list); 3000038d2ef8SMaor Gottlieb kfree(iter); 3001038d2ef8SMaor Gottlieb } 3002038d2ef8SMaor Gottlieb 300374491de9SMark Bloch mlx5_del_flow_rules(handler->rule); 30045497adc6SMaor Gottlieb put_flow_table(dev, handler->prio, true); 30059a4ca38dSMark Bloch mutex_unlock(&dev->flow_db->lock); 3006038d2ef8SMaor Gottlieb 3007038d2ef8SMaor Gottlieb kfree(handler); 3008038d2ef8SMaor Gottlieb 3009038d2ef8SMaor Gottlieb return 0; 3010038d2ef8SMaor Gottlieb } 3011038d2ef8SMaor Gottlieb 301235d19011SMaor Gottlieb static int ib_prio_to_core_prio(unsigned int priority, bool dont_trap) 301335d19011SMaor Gottlieb { 301435d19011SMaor Gottlieb priority *= 2; 301535d19011SMaor Gottlieb if (!dont_trap) 301635d19011SMaor Gottlieb priority++; 301735d19011SMaor Gottlieb return priority; 301835d19011SMaor Gottlieb } 301935d19011SMaor Gottlieb 3020cc0e5d42SMaor Gottlieb enum flow_table_type { 3021cc0e5d42SMaor Gottlieb MLX5_IB_FT_RX, 3022cc0e5d42SMaor Gottlieb MLX5_IB_FT_TX 3023cc0e5d42SMaor Gottlieb }; 3024cc0e5d42SMaor Gottlieb 302500b7c2abSMaor Gottlieb #define MLX5_FS_MAX_TYPES 6 302600b7c2abSMaor Gottlieb #define MLX5_FS_MAX_ENTRIES BIT(16) 3027038d2ef8SMaor Gottlieb static struct mlx5_ib_flow_prio *get_flow_table(struct mlx5_ib_dev *dev, 3028cc0e5d42SMaor Gottlieb struct ib_flow_attr *flow_attr, 3029cc0e5d42SMaor Gottlieb enum flow_table_type ft_type) 3030038d2ef8SMaor Gottlieb { 303135d19011SMaor Gottlieb bool dont_trap = flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP; 3032038d2ef8SMaor Gottlieb struct mlx5_flow_namespace *ns = NULL; 3033038d2ef8SMaor Gottlieb struct mlx5_ib_flow_prio *prio; 3034038d2ef8SMaor Gottlieb struct mlx5_flow_table *ft; 3035dac388efSMaor Gottlieb int max_table_size; 3036038d2ef8SMaor Gottlieb int num_entries; 3037038d2ef8SMaor Gottlieb int num_groups; 3038038d2ef8SMaor Gottlieb int priority; 3039038d2ef8SMaor Gottlieb int err = 0; 3040038d2ef8SMaor Gottlieb 3041dac388efSMaor Gottlieb max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, 3042dac388efSMaor Gottlieb log_max_ft_size)); 3043038d2ef8SMaor Gottlieb if (flow_attr->type == IB_FLOW_ATTR_NORMAL) { 3044802c2125SAviad Yehezkel if (ft_type == MLX5_IB_FT_TX) 3045802c2125SAviad Yehezkel priority = 0; 3046802c2125SAviad Yehezkel else if (flow_is_multicast_only(flow_attr) && 304735d19011SMaor Gottlieb !dont_trap) 3048038d2ef8SMaor Gottlieb priority = MLX5_IB_FLOW_MCAST_PRIO; 3049038d2ef8SMaor Gottlieb else 305035d19011SMaor Gottlieb priority = ib_prio_to_core_prio(flow_attr->priority, 305135d19011SMaor Gottlieb dont_trap); 3052038d2ef8SMaor Gottlieb ns = mlx5_get_flow_namespace(dev->mdev, 3053802c2125SAviad Yehezkel ft_type == MLX5_IB_FT_TX ? 3054802c2125SAviad Yehezkel MLX5_FLOW_NAMESPACE_EGRESS : 3055038d2ef8SMaor Gottlieb MLX5_FLOW_NAMESPACE_BYPASS); 3056038d2ef8SMaor Gottlieb num_entries = MLX5_FS_MAX_ENTRIES; 3057038d2ef8SMaor Gottlieb num_groups = MLX5_FS_MAX_TYPES; 30589a4ca38dSMark Bloch prio = &dev->flow_db->prios[priority]; 3059038d2ef8SMaor Gottlieb } else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT || 3060038d2ef8SMaor Gottlieb flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) { 3061038d2ef8SMaor Gottlieb ns = mlx5_get_flow_namespace(dev->mdev, 3062038d2ef8SMaor Gottlieb MLX5_FLOW_NAMESPACE_LEFTOVERS); 3063038d2ef8SMaor Gottlieb build_leftovers_ft_param(&priority, 3064038d2ef8SMaor Gottlieb &num_entries, 3065038d2ef8SMaor Gottlieb &num_groups); 30669a4ca38dSMark Bloch prio = &dev->flow_db->prios[MLX5_IB_FLOW_LEFTOVERS_PRIO]; 3067cc0e5d42SMaor Gottlieb } else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) { 3068cc0e5d42SMaor Gottlieb if (!MLX5_CAP_FLOWTABLE(dev->mdev, 3069cc0e5d42SMaor Gottlieb allow_sniffer_and_nic_rx_shared_tir)) 3070cc0e5d42SMaor Gottlieb return ERR_PTR(-ENOTSUPP); 3071cc0e5d42SMaor Gottlieb 3072cc0e5d42SMaor Gottlieb ns = mlx5_get_flow_namespace(dev->mdev, ft_type == MLX5_IB_FT_RX ? 3073cc0e5d42SMaor Gottlieb MLX5_FLOW_NAMESPACE_SNIFFER_RX : 3074cc0e5d42SMaor Gottlieb MLX5_FLOW_NAMESPACE_SNIFFER_TX); 3075cc0e5d42SMaor Gottlieb 30769a4ca38dSMark Bloch prio = &dev->flow_db->sniffer[ft_type]; 3077cc0e5d42SMaor Gottlieb priority = 0; 3078cc0e5d42SMaor Gottlieb num_entries = 1; 3079cc0e5d42SMaor Gottlieb num_groups = 1; 3080038d2ef8SMaor Gottlieb } 3081038d2ef8SMaor Gottlieb 3082038d2ef8SMaor Gottlieb if (!ns) 3083038d2ef8SMaor Gottlieb return ERR_PTR(-ENOTSUPP); 3084038d2ef8SMaor Gottlieb 3085dac388efSMaor Gottlieb if (num_entries > max_table_size) 3086dac388efSMaor Gottlieb return ERR_PTR(-ENOMEM); 3087dac388efSMaor Gottlieb 3088038d2ef8SMaor Gottlieb ft = prio->flow_table; 3089038d2ef8SMaor Gottlieb if (!ft) { 3090038d2ef8SMaor Gottlieb ft = mlx5_create_auto_grouped_flow_table(ns, priority, 3091038d2ef8SMaor Gottlieb num_entries, 3092d63cd286SMaor Gottlieb num_groups, 3093c9f1b073SHadar Hen Zion 0, 0); 3094038d2ef8SMaor Gottlieb 3095038d2ef8SMaor Gottlieb if (!IS_ERR(ft)) { 3096038d2ef8SMaor Gottlieb prio->refcount = 0; 3097038d2ef8SMaor Gottlieb prio->flow_table = ft; 3098038d2ef8SMaor Gottlieb } else { 3099038d2ef8SMaor Gottlieb err = PTR_ERR(ft); 3100038d2ef8SMaor Gottlieb } 3101038d2ef8SMaor Gottlieb } 3102038d2ef8SMaor Gottlieb 3103038d2ef8SMaor Gottlieb return err ? ERR_PTR(err) : prio; 3104038d2ef8SMaor Gottlieb } 3105038d2ef8SMaor Gottlieb 3106a550ddfcSYishai Hadas static void set_underlay_qp(struct mlx5_ib_dev *dev, 3107a550ddfcSYishai Hadas struct mlx5_flow_spec *spec, 3108a550ddfcSYishai Hadas u32 underlay_qpn) 3109a550ddfcSYishai Hadas { 3110a550ddfcSYishai Hadas void *misc_params_c = MLX5_ADDR_OF(fte_match_param, 3111a550ddfcSYishai Hadas spec->match_criteria, 3112a550ddfcSYishai Hadas misc_parameters); 3113a550ddfcSYishai Hadas void *misc_params_v = MLX5_ADDR_OF(fte_match_param, spec->match_value, 3114a550ddfcSYishai Hadas misc_parameters); 3115a550ddfcSYishai Hadas 3116a550ddfcSYishai Hadas if (underlay_qpn && 3117a550ddfcSYishai Hadas MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, 3118a550ddfcSYishai Hadas ft_field_support.bth_dst_qp)) { 3119a550ddfcSYishai Hadas MLX5_SET(fte_match_set_misc, 3120a550ddfcSYishai Hadas misc_params_v, bth_dst_qp, underlay_qpn); 3121a550ddfcSYishai Hadas MLX5_SET(fte_match_set_misc, 3122a550ddfcSYishai Hadas misc_params_c, bth_dst_qp, 0xffffff); 3123a550ddfcSYishai Hadas } 3124a550ddfcSYishai Hadas } 3125a550ddfcSYishai Hadas 3126a550ddfcSYishai Hadas static struct mlx5_ib_flow_handler *_create_flow_rule(struct mlx5_ib_dev *dev, 3127038d2ef8SMaor Gottlieb struct mlx5_ib_flow_prio *ft_prio, 3128dd063d0eSMaor Gottlieb const struct ib_flow_attr *flow_attr, 3129a550ddfcSYishai Hadas struct mlx5_flow_destination *dst, 3130a550ddfcSYishai Hadas u32 underlay_qpn) 3131038d2ef8SMaor Gottlieb { 3132038d2ef8SMaor Gottlieb struct mlx5_flow_table *ft = ft_prio->flow_table; 3133038d2ef8SMaor Gottlieb struct mlx5_ib_flow_handler *handler; 3134075572d4SBoris Pismenny struct mlx5_flow_act flow_act = {.flow_tag = MLX5_FS_DEFAULT_FLOW_TAG}; 3135c5bb1730SMaor Gottlieb struct mlx5_flow_spec *spec; 3136a22ed86cSSlava Shwartsman struct mlx5_flow_destination *rule_dst = dst; 3137dd063d0eSMaor Gottlieb const void *ib_flow = (const void *)flow_attr + sizeof(*flow_attr); 3138038d2ef8SMaor Gottlieb unsigned int spec_index; 313971c6e863SAriel Levkovich u32 prev_type = 0; 3140038d2ef8SMaor Gottlieb int err = 0; 3141a22ed86cSSlava Shwartsman int dest_num = 1; 3142802c2125SAviad Yehezkel bool is_egress = flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS; 3143038d2ef8SMaor Gottlieb 314419cc7524SAriel Levkovich if (!is_valid_attr(dev->mdev, flow_attr)) 3145038d2ef8SMaor Gottlieb return ERR_PTR(-EINVAL); 3146038d2ef8SMaor Gottlieb 31471b9a07eeSLeon Romanovsky spec = kvzalloc(sizeof(*spec), GFP_KERNEL); 3148038d2ef8SMaor Gottlieb handler = kzalloc(sizeof(*handler), GFP_KERNEL); 3149c5bb1730SMaor Gottlieb if (!handler || !spec) { 3150038d2ef8SMaor Gottlieb err = -ENOMEM; 3151038d2ef8SMaor Gottlieb goto free; 3152038d2ef8SMaor Gottlieb } 3153038d2ef8SMaor Gottlieb 3154038d2ef8SMaor Gottlieb INIT_LIST_HEAD(&handler->list); 3155038d2ef8SMaor Gottlieb 3156038d2ef8SMaor Gottlieb for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) { 315719cc7524SAriel Levkovich err = parse_flow_attr(dev->mdev, spec->match_criteria, 3158a22ed86cSSlava Shwartsman spec->match_value, 315971c6e863SAriel Levkovich ib_flow, flow_attr, &flow_act, 316071c6e863SAriel Levkovich prev_type); 3161038d2ef8SMaor Gottlieb if (err < 0) 3162038d2ef8SMaor Gottlieb goto free; 3163038d2ef8SMaor Gottlieb 316471c6e863SAriel Levkovich prev_type = ((union ib_flow_spec *)ib_flow)->type; 3165038d2ef8SMaor Gottlieb ib_flow += ((union ib_flow_spec *)ib_flow)->size; 3166038d2ef8SMaor Gottlieb } 3167038d2ef8SMaor Gottlieb 3168a550ddfcSYishai Hadas if (!flow_is_multicast_only(flow_attr)) 3169a550ddfcSYishai Hadas set_underlay_qp(dev, spec, underlay_qpn); 3170a550ddfcSYishai Hadas 3171018a94eeSMark Bloch if (dev->rep) { 3172018a94eeSMark Bloch void *misc; 3173018a94eeSMark Bloch 3174018a94eeSMark Bloch misc = MLX5_ADDR_OF(fte_match_param, spec->match_value, 3175018a94eeSMark Bloch misc_parameters); 3176018a94eeSMark Bloch MLX5_SET(fte_match_set_misc, misc, source_port, 3177018a94eeSMark Bloch dev->rep->vport); 3178018a94eeSMark Bloch misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria, 3179018a94eeSMark Bloch misc_parameters); 3180018a94eeSMark Bloch MLX5_SET_TO_ONES(fte_match_set_misc, misc, source_port); 3181018a94eeSMark Bloch } 3182018a94eeSMark Bloch 3183466fa6d2SMaor Gottlieb spec->match_criteria_enable = get_match_criteria_enable(spec->match_criteria); 3184802c2125SAviad Yehezkel 3185802c2125SAviad Yehezkel if (is_egress && 3186802c2125SAviad Yehezkel !is_valid_spec(dev->mdev, spec, &flow_act, is_egress)) { 3187802c2125SAviad Yehezkel err = -EINVAL; 3188802c2125SAviad Yehezkel goto free; 3189802c2125SAviad Yehezkel } 3190802c2125SAviad Yehezkel 3191075572d4SBoris Pismenny if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_DROP) { 3192a22ed86cSSlava Shwartsman rule_dst = NULL; 3193a22ed86cSSlava Shwartsman dest_num = 0; 3194a22ed86cSSlava Shwartsman } else { 3195802c2125SAviad Yehezkel if (is_egress) 3196802c2125SAviad Yehezkel flow_act.action |= MLX5_FLOW_CONTEXT_ACTION_ALLOW; 3197802c2125SAviad Yehezkel else 3198802c2125SAviad Yehezkel flow_act.action |= 3199802c2125SAviad Yehezkel dst ? MLX5_FLOW_CONTEXT_ACTION_FWD_DEST : 320035d19011SMaor Gottlieb MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO; 3201a22ed86cSSlava Shwartsman } 32022ac693f9SMoses Reuben 3203a9db0ecfSMatan Barak if (flow_act.has_flow_tag && 32042ac693f9SMoses Reuben (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT || 32052ac693f9SMoses Reuben flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT)) { 32062ac693f9SMoses Reuben mlx5_ib_warn(dev, "Flow tag %u and attribute type %x isn't allowed in leftovers\n", 3207075572d4SBoris Pismenny flow_act.flow_tag, flow_attr->type); 32082ac693f9SMoses Reuben err = -EINVAL; 32092ac693f9SMoses Reuben goto free; 32102ac693f9SMoses Reuben } 321174491de9SMark Bloch handler->rule = mlx5_add_flow_rules(ft, spec, 321266958ed9SHadar Hen Zion &flow_act, 3213a22ed86cSSlava Shwartsman rule_dst, dest_num); 3214038d2ef8SMaor Gottlieb 3215038d2ef8SMaor Gottlieb if (IS_ERR(handler->rule)) { 3216038d2ef8SMaor Gottlieb err = PTR_ERR(handler->rule); 3217038d2ef8SMaor Gottlieb goto free; 3218038d2ef8SMaor Gottlieb } 3219038d2ef8SMaor Gottlieb 3220d9d4980aSMaor Gottlieb ft_prio->refcount++; 32215497adc6SMaor Gottlieb handler->prio = ft_prio; 3222038d2ef8SMaor Gottlieb 3223038d2ef8SMaor Gottlieb ft_prio->flow_table = ft; 3224038d2ef8SMaor Gottlieb free: 3225038d2ef8SMaor Gottlieb if (err) 3226038d2ef8SMaor Gottlieb kfree(handler); 3227c5bb1730SMaor Gottlieb kvfree(spec); 3228038d2ef8SMaor Gottlieb return err ? ERR_PTR(err) : handler; 3229038d2ef8SMaor Gottlieb } 3230038d2ef8SMaor Gottlieb 3231a550ddfcSYishai Hadas static struct mlx5_ib_flow_handler *create_flow_rule(struct mlx5_ib_dev *dev, 3232a550ddfcSYishai Hadas struct mlx5_ib_flow_prio *ft_prio, 3233a550ddfcSYishai Hadas const struct ib_flow_attr *flow_attr, 3234a550ddfcSYishai Hadas struct mlx5_flow_destination *dst) 3235a550ddfcSYishai Hadas { 3236a550ddfcSYishai Hadas return _create_flow_rule(dev, ft_prio, flow_attr, dst, 0); 3237a550ddfcSYishai Hadas } 3238a550ddfcSYishai Hadas 323935d19011SMaor Gottlieb static struct mlx5_ib_flow_handler *create_dont_trap_rule(struct mlx5_ib_dev *dev, 324035d19011SMaor Gottlieb struct mlx5_ib_flow_prio *ft_prio, 324135d19011SMaor Gottlieb struct ib_flow_attr *flow_attr, 324235d19011SMaor Gottlieb struct mlx5_flow_destination *dst) 324335d19011SMaor Gottlieb { 324435d19011SMaor Gottlieb struct mlx5_ib_flow_handler *handler_dst = NULL; 324535d19011SMaor Gottlieb struct mlx5_ib_flow_handler *handler = NULL; 324635d19011SMaor Gottlieb 324735d19011SMaor Gottlieb handler = create_flow_rule(dev, ft_prio, flow_attr, NULL); 324835d19011SMaor Gottlieb if (!IS_ERR(handler)) { 324935d19011SMaor Gottlieb handler_dst = create_flow_rule(dev, ft_prio, 325035d19011SMaor Gottlieb flow_attr, dst); 325135d19011SMaor Gottlieb if (IS_ERR(handler_dst)) { 325274491de9SMark Bloch mlx5_del_flow_rules(handler->rule); 3253d9d4980aSMaor Gottlieb ft_prio->refcount--; 325435d19011SMaor Gottlieb kfree(handler); 325535d19011SMaor Gottlieb handler = handler_dst; 325635d19011SMaor Gottlieb } else { 325735d19011SMaor Gottlieb list_add(&handler_dst->list, &handler->list); 325835d19011SMaor Gottlieb } 325935d19011SMaor Gottlieb } 326035d19011SMaor Gottlieb 326135d19011SMaor Gottlieb return handler; 326235d19011SMaor Gottlieb } 3263038d2ef8SMaor Gottlieb enum { 3264038d2ef8SMaor Gottlieb LEFTOVERS_MC, 3265038d2ef8SMaor Gottlieb LEFTOVERS_UC, 3266038d2ef8SMaor Gottlieb }; 3267038d2ef8SMaor Gottlieb 3268038d2ef8SMaor Gottlieb static struct mlx5_ib_flow_handler *create_leftovers_rule(struct mlx5_ib_dev *dev, 3269038d2ef8SMaor Gottlieb struct mlx5_ib_flow_prio *ft_prio, 3270038d2ef8SMaor Gottlieb struct ib_flow_attr *flow_attr, 3271038d2ef8SMaor Gottlieb struct mlx5_flow_destination *dst) 3272038d2ef8SMaor Gottlieb { 3273038d2ef8SMaor Gottlieb struct mlx5_ib_flow_handler *handler_ucast = NULL; 3274038d2ef8SMaor Gottlieb struct mlx5_ib_flow_handler *handler = NULL; 3275038d2ef8SMaor Gottlieb 3276038d2ef8SMaor Gottlieb static struct { 3277038d2ef8SMaor Gottlieb struct ib_flow_attr flow_attr; 3278038d2ef8SMaor Gottlieb struct ib_flow_spec_eth eth_flow; 3279038d2ef8SMaor Gottlieb } leftovers_specs[] = { 3280038d2ef8SMaor Gottlieb [LEFTOVERS_MC] = { 3281038d2ef8SMaor Gottlieb .flow_attr = { 3282038d2ef8SMaor Gottlieb .num_of_specs = 1, 3283038d2ef8SMaor Gottlieb .size = sizeof(leftovers_specs[0]) 3284038d2ef8SMaor Gottlieb }, 3285038d2ef8SMaor Gottlieb .eth_flow = { 3286038d2ef8SMaor Gottlieb .type = IB_FLOW_SPEC_ETH, 3287038d2ef8SMaor Gottlieb .size = sizeof(struct ib_flow_spec_eth), 3288038d2ef8SMaor Gottlieb .mask = {.dst_mac = {0x1} }, 3289038d2ef8SMaor Gottlieb .val = {.dst_mac = {0x1} } 3290038d2ef8SMaor Gottlieb } 3291038d2ef8SMaor Gottlieb }, 3292038d2ef8SMaor Gottlieb [LEFTOVERS_UC] = { 3293038d2ef8SMaor Gottlieb .flow_attr = { 3294038d2ef8SMaor Gottlieb .num_of_specs = 1, 3295038d2ef8SMaor Gottlieb .size = sizeof(leftovers_specs[0]) 3296038d2ef8SMaor Gottlieb }, 3297038d2ef8SMaor Gottlieb .eth_flow = { 3298038d2ef8SMaor Gottlieb .type = IB_FLOW_SPEC_ETH, 3299038d2ef8SMaor Gottlieb .size = sizeof(struct ib_flow_spec_eth), 3300038d2ef8SMaor Gottlieb .mask = {.dst_mac = {0x1} }, 3301038d2ef8SMaor Gottlieb .val = {.dst_mac = {} } 3302038d2ef8SMaor Gottlieb } 3303038d2ef8SMaor Gottlieb } 3304038d2ef8SMaor Gottlieb }; 3305038d2ef8SMaor Gottlieb 3306038d2ef8SMaor Gottlieb handler = create_flow_rule(dev, ft_prio, 3307038d2ef8SMaor Gottlieb &leftovers_specs[LEFTOVERS_MC].flow_attr, 3308038d2ef8SMaor Gottlieb dst); 3309038d2ef8SMaor Gottlieb if (!IS_ERR(handler) && 3310038d2ef8SMaor Gottlieb flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT) { 3311038d2ef8SMaor Gottlieb handler_ucast = create_flow_rule(dev, ft_prio, 3312038d2ef8SMaor Gottlieb &leftovers_specs[LEFTOVERS_UC].flow_attr, 3313038d2ef8SMaor Gottlieb dst); 3314038d2ef8SMaor Gottlieb if (IS_ERR(handler_ucast)) { 331574491de9SMark Bloch mlx5_del_flow_rules(handler->rule); 3316d9d4980aSMaor Gottlieb ft_prio->refcount--; 3317038d2ef8SMaor Gottlieb kfree(handler); 3318038d2ef8SMaor Gottlieb handler = handler_ucast; 3319038d2ef8SMaor Gottlieb } else { 3320038d2ef8SMaor Gottlieb list_add(&handler_ucast->list, &handler->list); 3321038d2ef8SMaor Gottlieb } 3322038d2ef8SMaor Gottlieb } 3323038d2ef8SMaor Gottlieb 3324038d2ef8SMaor Gottlieb return handler; 3325038d2ef8SMaor Gottlieb } 3326038d2ef8SMaor Gottlieb 3327cc0e5d42SMaor Gottlieb static struct mlx5_ib_flow_handler *create_sniffer_rule(struct mlx5_ib_dev *dev, 3328cc0e5d42SMaor Gottlieb struct mlx5_ib_flow_prio *ft_rx, 3329cc0e5d42SMaor Gottlieb struct mlx5_ib_flow_prio *ft_tx, 3330cc0e5d42SMaor Gottlieb struct mlx5_flow_destination *dst) 3331cc0e5d42SMaor Gottlieb { 3332cc0e5d42SMaor Gottlieb struct mlx5_ib_flow_handler *handler_rx; 3333cc0e5d42SMaor Gottlieb struct mlx5_ib_flow_handler *handler_tx; 3334cc0e5d42SMaor Gottlieb int err; 3335cc0e5d42SMaor Gottlieb static const struct ib_flow_attr flow_attr = { 3336cc0e5d42SMaor Gottlieb .num_of_specs = 0, 3337cc0e5d42SMaor Gottlieb .size = sizeof(flow_attr) 3338cc0e5d42SMaor Gottlieb }; 3339cc0e5d42SMaor Gottlieb 3340cc0e5d42SMaor Gottlieb handler_rx = create_flow_rule(dev, ft_rx, &flow_attr, dst); 3341cc0e5d42SMaor Gottlieb if (IS_ERR(handler_rx)) { 3342cc0e5d42SMaor Gottlieb err = PTR_ERR(handler_rx); 3343cc0e5d42SMaor Gottlieb goto err; 3344cc0e5d42SMaor Gottlieb } 3345cc0e5d42SMaor Gottlieb 3346cc0e5d42SMaor Gottlieb handler_tx = create_flow_rule(dev, ft_tx, &flow_attr, dst); 3347cc0e5d42SMaor Gottlieb if (IS_ERR(handler_tx)) { 3348cc0e5d42SMaor Gottlieb err = PTR_ERR(handler_tx); 3349cc0e5d42SMaor Gottlieb goto err_tx; 3350cc0e5d42SMaor Gottlieb } 3351cc0e5d42SMaor Gottlieb 3352cc0e5d42SMaor Gottlieb list_add(&handler_tx->list, &handler_rx->list); 3353cc0e5d42SMaor Gottlieb 3354cc0e5d42SMaor Gottlieb return handler_rx; 3355cc0e5d42SMaor Gottlieb 3356cc0e5d42SMaor Gottlieb err_tx: 335774491de9SMark Bloch mlx5_del_flow_rules(handler_rx->rule); 3358cc0e5d42SMaor Gottlieb ft_rx->refcount--; 3359cc0e5d42SMaor Gottlieb kfree(handler_rx); 3360cc0e5d42SMaor Gottlieb err: 3361cc0e5d42SMaor Gottlieb return ERR_PTR(err); 3362cc0e5d42SMaor Gottlieb } 3363cc0e5d42SMaor Gottlieb 3364038d2ef8SMaor Gottlieb static struct ib_flow *mlx5_ib_create_flow(struct ib_qp *qp, 3365038d2ef8SMaor Gottlieb struct ib_flow_attr *flow_attr, 3366038d2ef8SMaor Gottlieb int domain) 3367038d2ef8SMaor Gottlieb { 3368038d2ef8SMaor Gottlieb struct mlx5_ib_dev *dev = to_mdev(qp->device); 3369d9f88e5aSYishai Hadas struct mlx5_ib_qp *mqp = to_mqp(qp); 3370038d2ef8SMaor Gottlieb struct mlx5_ib_flow_handler *handler = NULL; 3371038d2ef8SMaor Gottlieb struct mlx5_flow_destination *dst = NULL; 3372cc0e5d42SMaor Gottlieb struct mlx5_ib_flow_prio *ft_prio_tx = NULL; 3373038d2ef8SMaor Gottlieb struct mlx5_ib_flow_prio *ft_prio; 3374802c2125SAviad Yehezkel bool is_egress = flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS; 3375038d2ef8SMaor Gottlieb int err; 3376a550ddfcSYishai Hadas int underlay_qpn; 3377038d2ef8SMaor Gottlieb 3378038d2ef8SMaor Gottlieb if (flow_attr->priority > MLX5_IB_FLOW_LAST_PRIO) 3379dac388efSMaor Gottlieb return ERR_PTR(-ENOMEM); 3380038d2ef8SMaor Gottlieb 3381038d2ef8SMaor Gottlieb if (domain != IB_FLOW_DOMAIN_USER || 3382508562d6SDaniel Jurgens flow_attr->port > dev->num_ports || 3383802c2125SAviad Yehezkel (flow_attr->flags & ~(IB_FLOW_ATTR_FLAGS_DONT_TRAP | 3384802c2125SAviad Yehezkel IB_FLOW_ATTR_FLAGS_EGRESS))) 3385802c2125SAviad Yehezkel return ERR_PTR(-EINVAL); 3386802c2125SAviad Yehezkel 3387802c2125SAviad Yehezkel if (is_egress && 3388802c2125SAviad Yehezkel (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT || 3389802c2125SAviad Yehezkel flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT)) 3390038d2ef8SMaor Gottlieb return ERR_PTR(-EINVAL); 3391038d2ef8SMaor Gottlieb 3392038d2ef8SMaor Gottlieb dst = kzalloc(sizeof(*dst), GFP_KERNEL); 3393038d2ef8SMaor Gottlieb if (!dst) 3394038d2ef8SMaor Gottlieb return ERR_PTR(-ENOMEM); 3395038d2ef8SMaor Gottlieb 33969a4ca38dSMark Bloch mutex_lock(&dev->flow_db->lock); 3397038d2ef8SMaor Gottlieb 3398802c2125SAviad Yehezkel ft_prio = get_flow_table(dev, flow_attr, 3399802c2125SAviad Yehezkel is_egress ? MLX5_IB_FT_TX : MLX5_IB_FT_RX); 3400038d2ef8SMaor Gottlieb if (IS_ERR(ft_prio)) { 3401038d2ef8SMaor Gottlieb err = PTR_ERR(ft_prio); 3402038d2ef8SMaor Gottlieb goto unlock; 3403038d2ef8SMaor Gottlieb } 3404cc0e5d42SMaor Gottlieb if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) { 3405cc0e5d42SMaor Gottlieb ft_prio_tx = get_flow_table(dev, flow_attr, MLX5_IB_FT_TX); 3406cc0e5d42SMaor Gottlieb if (IS_ERR(ft_prio_tx)) { 3407cc0e5d42SMaor Gottlieb err = PTR_ERR(ft_prio_tx); 3408cc0e5d42SMaor Gottlieb ft_prio_tx = NULL; 3409cc0e5d42SMaor Gottlieb goto destroy_ft; 3410cc0e5d42SMaor Gottlieb } 3411cc0e5d42SMaor Gottlieb } 3412038d2ef8SMaor Gottlieb 3413802c2125SAviad Yehezkel if (is_egress) { 3414802c2125SAviad Yehezkel dst->type = MLX5_FLOW_DESTINATION_TYPE_PORT; 3415802c2125SAviad Yehezkel } else { 3416038d2ef8SMaor Gottlieb dst->type = MLX5_FLOW_DESTINATION_TYPE_TIR; 3417d9f88e5aSYishai Hadas if (mqp->flags & MLX5_IB_QP_RSS) 3418d9f88e5aSYishai Hadas dst->tir_num = mqp->rss_qp.tirn; 3419d9f88e5aSYishai Hadas else 3420d9f88e5aSYishai Hadas dst->tir_num = mqp->raw_packet_qp.rq.tirn; 3421802c2125SAviad Yehezkel } 3422038d2ef8SMaor Gottlieb 3423038d2ef8SMaor Gottlieb if (flow_attr->type == IB_FLOW_ATTR_NORMAL) { 342435d19011SMaor Gottlieb if (flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP) { 342535d19011SMaor Gottlieb handler = create_dont_trap_rule(dev, ft_prio, 342635d19011SMaor Gottlieb flow_attr, dst); 342735d19011SMaor Gottlieb } else { 3428a550ddfcSYishai Hadas underlay_qpn = (mqp->flags & MLX5_IB_QP_UNDERLAY) ? 3429a550ddfcSYishai Hadas mqp->underlay_qpn : 0; 3430a550ddfcSYishai Hadas handler = _create_flow_rule(dev, ft_prio, flow_attr, 3431a550ddfcSYishai Hadas dst, underlay_qpn); 343235d19011SMaor Gottlieb } 3433038d2ef8SMaor Gottlieb } else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT || 3434038d2ef8SMaor Gottlieb flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) { 3435038d2ef8SMaor Gottlieb handler = create_leftovers_rule(dev, ft_prio, flow_attr, 3436038d2ef8SMaor Gottlieb dst); 3437cc0e5d42SMaor Gottlieb } else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) { 3438cc0e5d42SMaor Gottlieb handler = create_sniffer_rule(dev, ft_prio, ft_prio_tx, dst); 3439038d2ef8SMaor Gottlieb } else { 3440038d2ef8SMaor Gottlieb err = -EINVAL; 3441038d2ef8SMaor Gottlieb goto destroy_ft; 3442038d2ef8SMaor Gottlieb } 3443038d2ef8SMaor Gottlieb 3444038d2ef8SMaor Gottlieb if (IS_ERR(handler)) { 3445038d2ef8SMaor Gottlieb err = PTR_ERR(handler); 3446038d2ef8SMaor Gottlieb handler = NULL; 3447038d2ef8SMaor Gottlieb goto destroy_ft; 3448038d2ef8SMaor Gottlieb } 3449038d2ef8SMaor Gottlieb 34509a4ca38dSMark Bloch mutex_unlock(&dev->flow_db->lock); 3451038d2ef8SMaor Gottlieb kfree(dst); 3452038d2ef8SMaor Gottlieb 3453038d2ef8SMaor Gottlieb return &handler->ibflow; 3454038d2ef8SMaor Gottlieb 3455038d2ef8SMaor Gottlieb destroy_ft: 3456038d2ef8SMaor Gottlieb put_flow_table(dev, ft_prio, false); 3457cc0e5d42SMaor Gottlieb if (ft_prio_tx) 3458cc0e5d42SMaor Gottlieb put_flow_table(dev, ft_prio_tx, false); 3459038d2ef8SMaor Gottlieb unlock: 34609a4ca38dSMark Bloch mutex_unlock(&dev->flow_db->lock); 3461038d2ef8SMaor Gottlieb kfree(dst); 3462038d2ef8SMaor Gottlieb kfree(handler); 3463038d2ef8SMaor Gottlieb return ERR_PTR(err); 3464038d2ef8SMaor Gottlieb } 3465038d2ef8SMaor Gottlieb 3466c6475a0bSAviad Yehezkel static u32 mlx5_ib_flow_action_flags_to_accel_xfrm_flags(u32 mlx5_flags) 3467c6475a0bSAviad Yehezkel { 3468c6475a0bSAviad Yehezkel u32 flags = 0; 3469c6475a0bSAviad Yehezkel 3470c6475a0bSAviad Yehezkel if (mlx5_flags & MLX5_IB_UAPI_FLOW_ACTION_FLAGS_REQUIRE_METADATA) 3471c6475a0bSAviad Yehezkel flags |= MLX5_ACCEL_XFRM_FLAG_REQUIRE_METADATA; 3472c6475a0bSAviad Yehezkel 3473c6475a0bSAviad Yehezkel return flags; 3474c6475a0bSAviad Yehezkel } 3475c6475a0bSAviad Yehezkel 3476c6475a0bSAviad Yehezkel #define MLX5_FLOW_ACTION_ESP_CREATE_LAST_SUPPORTED MLX5_IB_UAPI_FLOW_ACTION_FLAGS_REQUIRE_METADATA 3477c6475a0bSAviad Yehezkel static struct ib_flow_action * 3478c6475a0bSAviad Yehezkel mlx5_ib_create_flow_action_esp(struct ib_device *device, 3479c6475a0bSAviad Yehezkel const struct ib_flow_action_attrs_esp *attr, 3480c6475a0bSAviad Yehezkel struct uverbs_attr_bundle *attrs) 3481c6475a0bSAviad Yehezkel { 3482c6475a0bSAviad Yehezkel struct mlx5_ib_dev *mdev = to_mdev(device); 3483c6475a0bSAviad Yehezkel struct ib_uverbs_flow_action_esp_keymat_aes_gcm *aes_gcm; 3484c6475a0bSAviad Yehezkel struct mlx5_accel_esp_xfrm_attrs accel_attrs = {}; 3485c6475a0bSAviad Yehezkel struct mlx5_ib_flow_action *action; 3486c6475a0bSAviad Yehezkel u64 action_flags; 3487c6475a0bSAviad Yehezkel u64 flags; 3488c6475a0bSAviad Yehezkel int err = 0; 3489c6475a0bSAviad Yehezkel 3490c6475a0bSAviad Yehezkel if (IS_UVERBS_COPY_ERR(uverbs_copy_from(&action_flags, attrs, 3491c6475a0bSAviad Yehezkel MLX5_IB_ATTR_CREATE_FLOW_ACTION_FLAGS))) 3492c6475a0bSAviad Yehezkel return ERR_PTR(-EFAULT); 3493c6475a0bSAviad Yehezkel 3494c6475a0bSAviad Yehezkel if (action_flags >= (MLX5_FLOW_ACTION_ESP_CREATE_LAST_SUPPORTED << 1)) 3495c6475a0bSAviad Yehezkel return ERR_PTR(-EOPNOTSUPP); 3496c6475a0bSAviad Yehezkel 3497c6475a0bSAviad Yehezkel flags = mlx5_ib_flow_action_flags_to_accel_xfrm_flags(action_flags); 3498c6475a0bSAviad Yehezkel 3499c6475a0bSAviad Yehezkel /* We current only support a subset of the standard features. Only a 3500c6475a0bSAviad Yehezkel * keymat of type AES_GCM, with icv_len == 16, iv_algo == SEQ and esn 3501c6475a0bSAviad Yehezkel * (with overlap). Full offload mode isn't supported. 3502c6475a0bSAviad Yehezkel */ 3503c6475a0bSAviad Yehezkel if (!attr->keymat || attr->replay || attr->encap || 3504c6475a0bSAviad Yehezkel attr->spi || attr->seq || attr->tfc_pad || 3505c6475a0bSAviad Yehezkel attr->hard_limit_pkts || 3506c6475a0bSAviad Yehezkel (attr->flags & ~(IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED | 3507c6475a0bSAviad Yehezkel IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ENCRYPT))) 3508c6475a0bSAviad Yehezkel return ERR_PTR(-EOPNOTSUPP); 3509c6475a0bSAviad Yehezkel 3510c6475a0bSAviad Yehezkel if (attr->keymat->protocol != 3511c6475a0bSAviad Yehezkel IB_UVERBS_FLOW_ACTION_ESP_KEYMAT_AES_GCM) 3512c6475a0bSAviad Yehezkel return ERR_PTR(-EOPNOTSUPP); 3513c6475a0bSAviad Yehezkel 3514c6475a0bSAviad Yehezkel aes_gcm = &attr->keymat->keymat.aes_gcm; 3515c6475a0bSAviad Yehezkel 3516c6475a0bSAviad Yehezkel if (aes_gcm->icv_len != 16 || 3517c6475a0bSAviad Yehezkel aes_gcm->iv_algo != IB_UVERBS_FLOW_ACTION_IV_ALGO_SEQ) 3518c6475a0bSAviad Yehezkel return ERR_PTR(-EOPNOTSUPP); 3519c6475a0bSAviad Yehezkel 3520c6475a0bSAviad Yehezkel action = kmalloc(sizeof(*action), GFP_KERNEL); 3521c6475a0bSAviad Yehezkel if (!action) 3522c6475a0bSAviad Yehezkel return ERR_PTR(-ENOMEM); 3523c6475a0bSAviad Yehezkel 3524c6475a0bSAviad Yehezkel action->esp_aes_gcm.ib_flags = attr->flags; 3525c6475a0bSAviad Yehezkel memcpy(&accel_attrs.keymat.aes_gcm.aes_key, &aes_gcm->aes_key, 3526c6475a0bSAviad Yehezkel sizeof(accel_attrs.keymat.aes_gcm.aes_key)); 3527c6475a0bSAviad Yehezkel accel_attrs.keymat.aes_gcm.key_len = aes_gcm->key_len * 8; 3528c6475a0bSAviad Yehezkel memcpy(&accel_attrs.keymat.aes_gcm.salt, &aes_gcm->salt, 3529c6475a0bSAviad Yehezkel sizeof(accel_attrs.keymat.aes_gcm.salt)); 3530c6475a0bSAviad Yehezkel memcpy(&accel_attrs.keymat.aes_gcm.seq_iv, &aes_gcm->iv, 3531c6475a0bSAviad Yehezkel sizeof(accel_attrs.keymat.aes_gcm.seq_iv)); 3532c6475a0bSAviad Yehezkel accel_attrs.keymat.aes_gcm.icv_len = aes_gcm->icv_len * 8; 3533c6475a0bSAviad Yehezkel accel_attrs.keymat.aes_gcm.iv_algo = MLX5_ACCEL_ESP_AES_GCM_IV_ALGO_SEQ; 3534c6475a0bSAviad Yehezkel accel_attrs.keymat_type = MLX5_ACCEL_ESP_KEYMAT_AES_GCM; 3535c6475a0bSAviad Yehezkel 3536c6475a0bSAviad Yehezkel accel_attrs.esn = attr->esn; 3537c6475a0bSAviad Yehezkel if (attr->flags & IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED) 3538c6475a0bSAviad Yehezkel accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_TRIGGERED; 3539c6475a0bSAviad Yehezkel if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW) 3540c6475a0bSAviad Yehezkel accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP; 3541c6475a0bSAviad Yehezkel 3542c6475a0bSAviad Yehezkel if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ENCRYPT) 3543c6475a0bSAviad Yehezkel accel_attrs.action |= MLX5_ACCEL_ESP_ACTION_ENCRYPT; 3544c6475a0bSAviad Yehezkel 3545c6475a0bSAviad Yehezkel action->esp_aes_gcm.ctx = 3546c6475a0bSAviad Yehezkel mlx5_accel_esp_create_xfrm(mdev->mdev, &accel_attrs, flags); 3547c6475a0bSAviad Yehezkel if (IS_ERR(action->esp_aes_gcm.ctx)) { 3548c6475a0bSAviad Yehezkel err = PTR_ERR(action->esp_aes_gcm.ctx); 3549c6475a0bSAviad Yehezkel goto err_parse; 3550c6475a0bSAviad Yehezkel } 3551c6475a0bSAviad Yehezkel 3552c6475a0bSAviad Yehezkel action->esp_aes_gcm.ib_flags = attr->flags; 3553c6475a0bSAviad Yehezkel 3554c6475a0bSAviad Yehezkel return &action->ib_action; 3555c6475a0bSAviad Yehezkel 3556c6475a0bSAviad Yehezkel err_parse: 3557c6475a0bSAviad Yehezkel kfree(action); 3558c6475a0bSAviad Yehezkel return ERR_PTR(err); 3559c6475a0bSAviad Yehezkel } 3560c6475a0bSAviad Yehezkel 3561349705c1SMatan Barak static int 3562349705c1SMatan Barak mlx5_ib_modify_flow_action_esp(struct ib_flow_action *action, 3563349705c1SMatan Barak const struct ib_flow_action_attrs_esp *attr, 3564349705c1SMatan Barak struct uverbs_attr_bundle *attrs) 3565349705c1SMatan Barak { 3566349705c1SMatan Barak struct mlx5_ib_flow_action *maction = to_mflow_act(action); 3567349705c1SMatan Barak struct mlx5_accel_esp_xfrm_attrs accel_attrs; 3568349705c1SMatan Barak int err = 0; 3569349705c1SMatan Barak 3570349705c1SMatan Barak if (attr->keymat || attr->replay || attr->encap || 3571349705c1SMatan Barak attr->spi || attr->seq || attr->tfc_pad || 3572349705c1SMatan Barak attr->hard_limit_pkts || 3573349705c1SMatan Barak (attr->flags & ~(IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED | 3574349705c1SMatan Barak IB_FLOW_ACTION_ESP_FLAGS_MOD_ESP_ATTRS | 3575349705c1SMatan Barak IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW))) 3576349705c1SMatan Barak return -EOPNOTSUPP; 3577349705c1SMatan Barak 3578349705c1SMatan Barak /* Only the ESN value or the MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP can 3579349705c1SMatan Barak * be modified. 3580349705c1SMatan Barak */ 3581349705c1SMatan Barak if (!(maction->esp_aes_gcm.ib_flags & 3582349705c1SMatan Barak IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED) && 3583349705c1SMatan Barak attr->flags & (IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED | 3584349705c1SMatan Barak IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW)) 3585349705c1SMatan Barak return -EINVAL; 3586349705c1SMatan Barak 3587349705c1SMatan Barak memcpy(&accel_attrs, &maction->esp_aes_gcm.ctx->attrs, 3588349705c1SMatan Barak sizeof(accel_attrs)); 3589349705c1SMatan Barak 3590349705c1SMatan Barak accel_attrs.esn = attr->esn; 3591349705c1SMatan Barak if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW) 3592349705c1SMatan Barak accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP; 3593349705c1SMatan Barak else 3594349705c1SMatan Barak accel_attrs.flags &= ~MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP; 3595349705c1SMatan Barak 3596349705c1SMatan Barak err = mlx5_accel_esp_modify_xfrm(maction->esp_aes_gcm.ctx, 3597349705c1SMatan Barak &accel_attrs); 3598349705c1SMatan Barak if (err) 3599349705c1SMatan Barak return err; 3600349705c1SMatan Barak 3601349705c1SMatan Barak maction->esp_aes_gcm.ib_flags &= 3602349705c1SMatan Barak ~IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW; 3603349705c1SMatan Barak maction->esp_aes_gcm.ib_flags |= 3604349705c1SMatan Barak attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW; 3605349705c1SMatan Barak 3606349705c1SMatan Barak return 0; 3607349705c1SMatan Barak } 3608349705c1SMatan Barak 3609c6475a0bSAviad Yehezkel static int mlx5_ib_destroy_flow_action(struct ib_flow_action *action) 3610c6475a0bSAviad Yehezkel { 3611c6475a0bSAviad Yehezkel struct mlx5_ib_flow_action *maction = to_mflow_act(action); 3612c6475a0bSAviad Yehezkel 3613c6475a0bSAviad Yehezkel switch (action->type) { 3614c6475a0bSAviad Yehezkel case IB_FLOW_ACTION_ESP: 3615c6475a0bSAviad Yehezkel /* 3616c6475a0bSAviad Yehezkel * We only support aes_gcm by now, so we implicitly know this is 3617c6475a0bSAviad Yehezkel * the underline crypto. 3618c6475a0bSAviad Yehezkel */ 3619c6475a0bSAviad Yehezkel mlx5_accel_esp_destroy_xfrm(maction->esp_aes_gcm.ctx); 3620c6475a0bSAviad Yehezkel break; 3621c6475a0bSAviad Yehezkel default: 3622c6475a0bSAviad Yehezkel WARN_ON(true); 3623c6475a0bSAviad Yehezkel break; 3624c6475a0bSAviad Yehezkel } 3625c6475a0bSAviad Yehezkel 3626c6475a0bSAviad Yehezkel kfree(maction); 3627c6475a0bSAviad Yehezkel return 0; 3628c6475a0bSAviad Yehezkel } 3629c6475a0bSAviad Yehezkel 3630e126ba97SEli Cohen static int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid) 3631e126ba97SEli Cohen { 3632e126ba97SEli Cohen struct mlx5_ib_dev *dev = to_mdev(ibqp->device); 363381e30880SYishai Hadas struct mlx5_ib_qp *mqp = to_mqp(ibqp); 3634e126ba97SEli Cohen int err; 3635e126ba97SEli Cohen 363681e30880SYishai Hadas if (mqp->flags & MLX5_IB_QP_UNDERLAY) { 363781e30880SYishai Hadas mlx5_ib_dbg(dev, "Attaching a multi cast group to underlay QP is not supported\n"); 363881e30880SYishai Hadas return -EOPNOTSUPP; 363981e30880SYishai Hadas } 364081e30880SYishai Hadas 36419603b61dSJack Morgenstein err = mlx5_core_attach_mcg(dev->mdev, gid, ibqp->qp_num); 3642e126ba97SEli Cohen if (err) 3643e126ba97SEli Cohen mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n", 3644e126ba97SEli Cohen ibqp->qp_num, gid->raw); 3645e126ba97SEli Cohen 3646e126ba97SEli Cohen return err; 3647e126ba97SEli Cohen } 3648e126ba97SEli Cohen 3649e126ba97SEli Cohen static int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid) 3650e126ba97SEli Cohen { 3651e126ba97SEli Cohen struct mlx5_ib_dev *dev = to_mdev(ibqp->device); 3652e126ba97SEli Cohen int err; 3653e126ba97SEli Cohen 36549603b61dSJack Morgenstein err = mlx5_core_detach_mcg(dev->mdev, gid, ibqp->qp_num); 3655e126ba97SEli Cohen if (err) 3656e126ba97SEli Cohen mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n", 3657e126ba97SEli Cohen ibqp->qp_num, gid->raw); 3658e126ba97SEli Cohen 3659e126ba97SEli Cohen return err; 3660e126ba97SEli Cohen } 3661e126ba97SEli Cohen 3662e126ba97SEli Cohen static int init_node_data(struct mlx5_ib_dev *dev) 3663e126ba97SEli Cohen { 36641b5daf11SMajd Dibbiny int err; 3665e126ba97SEli Cohen 36661b5daf11SMajd Dibbiny err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc); 3667e126ba97SEli Cohen if (err) 3668e126ba97SEli Cohen return err; 36691b5daf11SMajd Dibbiny 36701b5daf11SMajd Dibbiny dev->mdev->rev_id = dev->mdev->pdev->revision; 36711b5daf11SMajd Dibbiny 36721b5daf11SMajd Dibbiny return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid); 3673e126ba97SEli Cohen } 3674e126ba97SEli Cohen 3675e126ba97SEli Cohen static ssize_t show_fw_pages(struct device *device, struct device_attribute *attr, 3676e126ba97SEli Cohen char *buf) 3677e126ba97SEli Cohen { 3678e126ba97SEli Cohen struct mlx5_ib_dev *dev = 3679e126ba97SEli Cohen container_of(device, struct mlx5_ib_dev, ib_dev.dev); 3680e126ba97SEli Cohen 36819603b61dSJack Morgenstein return sprintf(buf, "%d\n", dev->mdev->priv.fw_pages); 3682e126ba97SEli Cohen } 3683e126ba97SEli Cohen 3684e126ba97SEli Cohen static ssize_t show_reg_pages(struct device *device, 3685e126ba97SEli Cohen struct device_attribute *attr, char *buf) 3686e126ba97SEli Cohen { 3687e126ba97SEli Cohen struct mlx5_ib_dev *dev = 3688e126ba97SEli Cohen container_of(device, struct mlx5_ib_dev, ib_dev.dev); 3689e126ba97SEli Cohen 36906aec21f6SHaggai Eran return sprintf(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages)); 3691e126ba97SEli Cohen } 3692e126ba97SEli Cohen 3693e126ba97SEli Cohen static ssize_t show_hca(struct device *device, struct device_attribute *attr, 3694e126ba97SEli Cohen char *buf) 3695e126ba97SEli Cohen { 3696e126ba97SEli Cohen struct mlx5_ib_dev *dev = 3697e126ba97SEli Cohen container_of(device, struct mlx5_ib_dev, ib_dev.dev); 36989603b61dSJack Morgenstein return sprintf(buf, "MT%d\n", dev->mdev->pdev->device); 3699e126ba97SEli Cohen } 3700e126ba97SEli Cohen 3701e126ba97SEli Cohen static ssize_t show_rev(struct device *device, struct device_attribute *attr, 3702e126ba97SEli Cohen char *buf) 3703e126ba97SEli Cohen { 3704e126ba97SEli Cohen struct mlx5_ib_dev *dev = 3705e126ba97SEli Cohen container_of(device, struct mlx5_ib_dev, ib_dev.dev); 37069603b61dSJack Morgenstein return sprintf(buf, "%x\n", dev->mdev->rev_id); 3707e126ba97SEli Cohen } 3708e126ba97SEli Cohen 3709e126ba97SEli Cohen static ssize_t show_board(struct device *device, struct device_attribute *attr, 3710e126ba97SEli Cohen char *buf) 3711e126ba97SEli Cohen { 3712e126ba97SEli Cohen struct mlx5_ib_dev *dev = 3713e126ba97SEli Cohen container_of(device, struct mlx5_ib_dev, ib_dev.dev); 3714e126ba97SEli Cohen return sprintf(buf, "%.*s\n", MLX5_BOARD_ID_LEN, 37159603b61dSJack Morgenstein dev->mdev->board_id); 3716e126ba97SEli Cohen } 3717e126ba97SEli Cohen 3718e126ba97SEli Cohen static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL); 3719e126ba97SEli Cohen static DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL); 3720e126ba97SEli Cohen static DEVICE_ATTR(board_id, S_IRUGO, show_board, NULL); 3721e126ba97SEli Cohen static DEVICE_ATTR(fw_pages, S_IRUGO, show_fw_pages, NULL); 3722e126ba97SEli Cohen static DEVICE_ATTR(reg_pages, S_IRUGO, show_reg_pages, NULL); 3723e126ba97SEli Cohen 3724e126ba97SEli Cohen static struct device_attribute *mlx5_class_attributes[] = { 3725e126ba97SEli Cohen &dev_attr_hw_rev, 3726e126ba97SEli Cohen &dev_attr_hca_type, 3727e126ba97SEli Cohen &dev_attr_board_id, 3728e126ba97SEli Cohen &dev_attr_fw_pages, 3729e126ba97SEli Cohen &dev_attr_reg_pages, 3730e126ba97SEli Cohen }; 3731e126ba97SEli Cohen 37327722f47eSHaggai Eran static void pkey_change_handler(struct work_struct *work) 37337722f47eSHaggai Eran { 37347722f47eSHaggai Eran struct mlx5_ib_port_resources *ports = 37357722f47eSHaggai Eran container_of(work, struct mlx5_ib_port_resources, 37367722f47eSHaggai Eran pkey_change_work); 37377722f47eSHaggai Eran 37387722f47eSHaggai Eran mutex_lock(&ports->devr->mutex); 37397722f47eSHaggai Eran mlx5_ib_gsi_pkey_change(ports->gsi); 37407722f47eSHaggai Eran mutex_unlock(&ports->devr->mutex); 37417722f47eSHaggai Eran } 37427722f47eSHaggai Eran 374389ea94a7SMaor Gottlieb static void mlx5_ib_handle_internal_error(struct mlx5_ib_dev *ibdev) 374489ea94a7SMaor Gottlieb { 374589ea94a7SMaor Gottlieb struct mlx5_ib_qp *mqp; 374689ea94a7SMaor Gottlieb struct mlx5_ib_cq *send_mcq, *recv_mcq; 374789ea94a7SMaor Gottlieb struct mlx5_core_cq *mcq; 374889ea94a7SMaor Gottlieb struct list_head cq_armed_list; 374989ea94a7SMaor Gottlieb unsigned long flags_qp; 375089ea94a7SMaor Gottlieb unsigned long flags_cq; 375189ea94a7SMaor Gottlieb unsigned long flags; 375289ea94a7SMaor Gottlieb 375389ea94a7SMaor Gottlieb INIT_LIST_HEAD(&cq_armed_list); 375489ea94a7SMaor Gottlieb 375589ea94a7SMaor Gottlieb /* Go over qp list reside on that ibdev, sync with create/destroy qp.*/ 375689ea94a7SMaor Gottlieb spin_lock_irqsave(&ibdev->reset_flow_resource_lock, flags); 375789ea94a7SMaor Gottlieb list_for_each_entry(mqp, &ibdev->qp_list, qps_list) { 375889ea94a7SMaor Gottlieb spin_lock_irqsave(&mqp->sq.lock, flags_qp); 375989ea94a7SMaor Gottlieb if (mqp->sq.tail != mqp->sq.head) { 376089ea94a7SMaor Gottlieb send_mcq = to_mcq(mqp->ibqp.send_cq); 376189ea94a7SMaor Gottlieb spin_lock_irqsave(&send_mcq->lock, flags_cq); 376289ea94a7SMaor Gottlieb if (send_mcq->mcq.comp && 376389ea94a7SMaor Gottlieb mqp->ibqp.send_cq->comp_handler) { 376489ea94a7SMaor Gottlieb if (!send_mcq->mcq.reset_notify_added) { 376589ea94a7SMaor Gottlieb send_mcq->mcq.reset_notify_added = 1; 376689ea94a7SMaor Gottlieb list_add_tail(&send_mcq->mcq.reset_notify, 376789ea94a7SMaor Gottlieb &cq_armed_list); 376889ea94a7SMaor Gottlieb } 376989ea94a7SMaor Gottlieb } 377089ea94a7SMaor Gottlieb spin_unlock_irqrestore(&send_mcq->lock, flags_cq); 377189ea94a7SMaor Gottlieb } 377289ea94a7SMaor Gottlieb spin_unlock_irqrestore(&mqp->sq.lock, flags_qp); 377389ea94a7SMaor Gottlieb spin_lock_irqsave(&mqp->rq.lock, flags_qp); 377489ea94a7SMaor Gottlieb /* no handling is needed for SRQ */ 377589ea94a7SMaor Gottlieb if (!mqp->ibqp.srq) { 377689ea94a7SMaor Gottlieb if (mqp->rq.tail != mqp->rq.head) { 377789ea94a7SMaor Gottlieb recv_mcq = to_mcq(mqp->ibqp.recv_cq); 377889ea94a7SMaor Gottlieb spin_lock_irqsave(&recv_mcq->lock, flags_cq); 377989ea94a7SMaor Gottlieb if (recv_mcq->mcq.comp && 378089ea94a7SMaor Gottlieb mqp->ibqp.recv_cq->comp_handler) { 378189ea94a7SMaor Gottlieb if (!recv_mcq->mcq.reset_notify_added) { 378289ea94a7SMaor Gottlieb recv_mcq->mcq.reset_notify_added = 1; 378389ea94a7SMaor Gottlieb list_add_tail(&recv_mcq->mcq.reset_notify, 378489ea94a7SMaor Gottlieb &cq_armed_list); 378589ea94a7SMaor Gottlieb } 378689ea94a7SMaor Gottlieb } 378789ea94a7SMaor Gottlieb spin_unlock_irqrestore(&recv_mcq->lock, 378889ea94a7SMaor Gottlieb flags_cq); 378989ea94a7SMaor Gottlieb } 379089ea94a7SMaor Gottlieb } 379189ea94a7SMaor Gottlieb spin_unlock_irqrestore(&mqp->rq.lock, flags_qp); 379289ea94a7SMaor Gottlieb } 379389ea94a7SMaor Gottlieb /*At that point all inflight post send were put to be executed as of we 379489ea94a7SMaor Gottlieb * lock/unlock above locks Now need to arm all involved CQs. 379589ea94a7SMaor Gottlieb */ 379689ea94a7SMaor Gottlieb list_for_each_entry(mcq, &cq_armed_list, reset_notify) { 379789ea94a7SMaor Gottlieb mcq->comp(mcq); 379889ea94a7SMaor Gottlieb } 379989ea94a7SMaor Gottlieb spin_unlock_irqrestore(&ibdev->reset_flow_resource_lock, flags); 380089ea94a7SMaor Gottlieb } 380189ea94a7SMaor Gottlieb 380203404e8aSMaor Gottlieb static void delay_drop_handler(struct work_struct *work) 380303404e8aSMaor Gottlieb { 380403404e8aSMaor Gottlieb int err; 380503404e8aSMaor Gottlieb struct mlx5_ib_delay_drop *delay_drop = 380603404e8aSMaor Gottlieb container_of(work, struct mlx5_ib_delay_drop, 380703404e8aSMaor Gottlieb delay_drop_work); 380803404e8aSMaor Gottlieb 3809fe248c3aSMaor Gottlieb atomic_inc(&delay_drop->events_cnt); 3810fe248c3aSMaor Gottlieb 381103404e8aSMaor Gottlieb mutex_lock(&delay_drop->lock); 381203404e8aSMaor Gottlieb err = mlx5_core_set_delay_drop(delay_drop->dev->mdev, 381303404e8aSMaor Gottlieb delay_drop->timeout); 381403404e8aSMaor Gottlieb if (err) { 381503404e8aSMaor Gottlieb mlx5_ib_warn(delay_drop->dev, "Failed to set delay drop, timeout=%u\n", 381603404e8aSMaor Gottlieb delay_drop->timeout); 381703404e8aSMaor Gottlieb delay_drop->activate = false; 381803404e8aSMaor Gottlieb } 381903404e8aSMaor Gottlieb mutex_unlock(&delay_drop->lock); 382003404e8aSMaor Gottlieb } 382103404e8aSMaor Gottlieb 3822d69a24e0SDaniel Jurgens static void mlx5_ib_handle_event(struct work_struct *_work) 3823e126ba97SEli Cohen { 3824d69a24e0SDaniel Jurgens struct mlx5_ib_event_work *work = 3825d69a24e0SDaniel Jurgens container_of(_work, struct mlx5_ib_event_work, work); 3826d69a24e0SDaniel Jurgens struct mlx5_ib_dev *ibdev; 3827e126ba97SEli Cohen struct ib_event ibev; 3828dbaaff2aSEli Cohen bool fatal = false; 3829aba46213SDaniel Jurgens u8 port = (u8)work->param; 3830e126ba97SEli Cohen 3831d69a24e0SDaniel Jurgens if (mlx5_core_is_mp_slave(work->dev)) { 3832d69a24e0SDaniel Jurgens ibdev = mlx5_ib_get_ibdev_from_mpi(work->context); 3833d69a24e0SDaniel Jurgens if (!ibdev) 3834d69a24e0SDaniel Jurgens goto out; 3835d69a24e0SDaniel Jurgens } else { 3836d69a24e0SDaniel Jurgens ibdev = work->context; 3837d69a24e0SDaniel Jurgens } 3838d69a24e0SDaniel Jurgens 3839d69a24e0SDaniel Jurgens switch (work->event) { 3840e126ba97SEli Cohen case MLX5_DEV_EVENT_SYS_ERROR: 3841e126ba97SEli Cohen ibev.event = IB_EVENT_DEVICE_FATAL; 384289ea94a7SMaor Gottlieb mlx5_ib_handle_internal_error(ibdev); 3843dbaaff2aSEli Cohen fatal = true; 3844e126ba97SEli Cohen break; 3845e126ba97SEli Cohen 3846e126ba97SEli Cohen case MLX5_DEV_EVENT_PORT_UP: 3847e126ba97SEli Cohen case MLX5_DEV_EVENT_PORT_DOWN: 38482788cf3bSNoa Osherovich case MLX5_DEV_EVENT_PORT_INITIALIZED: 38495ec8c83eSAviv Heller /* In RoCE, port up/down events are handled in 38505ec8c83eSAviv Heller * mlx5_netdev_event(). 38515ec8c83eSAviv Heller */ 38525ec8c83eSAviv Heller if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) == 38535ec8c83eSAviv Heller IB_LINK_LAYER_ETHERNET) 3854d69a24e0SDaniel Jurgens goto out; 38555ec8c83eSAviv Heller 3856d69a24e0SDaniel Jurgens ibev.event = (work->event == MLX5_DEV_EVENT_PORT_UP) ? 38575ec8c83eSAviv Heller IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR; 3858e126ba97SEli Cohen break; 3859e126ba97SEli Cohen 3860e126ba97SEli Cohen case MLX5_DEV_EVENT_LID_CHANGE: 3861e126ba97SEli Cohen ibev.event = IB_EVENT_LID_CHANGE; 3862e126ba97SEli Cohen break; 3863e126ba97SEli Cohen 3864e126ba97SEli Cohen case MLX5_DEV_EVENT_PKEY_CHANGE: 3865e126ba97SEli Cohen ibev.event = IB_EVENT_PKEY_CHANGE; 38667722f47eSHaggai Eran schedule_work(&ibdev->devr.ports[port - 1].pkey_change_work); 3867e126ba97SEli Cohen break; 3868e126ba97SEli Cohen 3869e126ba97SEli Cohen case MLX5_DEV_EVENT_GUID_CHANGE: 3870e126ba97SEli Cohen ibev.event = IB_EVENT_GID_CHANGE; 3871e126ba97SEli Cohen break; 3872e126ba97SEli Cohen 3873e126ba97SEli Cohen case MLX5_DEV_EVENT_CLIENT_REREG: 3874e126ba97SEli Cohen ibev.event = IB_EVENT_CLIENT_REREGISTER; 3875e126ba97SEli Cohen break; 387603404e8aSMaor Gottlieb case MLX5_DEV_EVENT_DELAY_DROP_TIMEOUT: 387703404e8aSMaor Gottlieb schedule_work(&ibdev->delay_drop.delay_drop_work); 387803404e8aSMaor Gottlieb goto out; 3879bdc37924SSaeed Mahameed default: 388003404e8aSMaor Gottlieb goto out; 3881e126ba97SEli Cohen } 3882e126ba97SEli Cohen 3883e126ba97SEli Cohen ibev.device = &ibdev->ib_dev; 3884e126ba97SEli Cohen ibev.element.port_num = port; 3885e126ba97SEli Cohen 3886aba46213SDaniel Jurgens if (!rdma_is_port_valid(&ibdev->ib_dev, port)) { 3887a0c84c32SEli Cohen mlx5_ib_warn(ibdev, "warning: event on port %d\n", port); 388803404e8aSMaor Gottlieb goto out; 3889a0c84c32SEli Cohen } 3890a0c84c32SEli Cohen 3891e126ba97SEli Cohen if (ibdev->ib_active) 3892e126ba97SEli Cohen ib_dispatch_event(&ibev); 3893dbaaff2aSEli Cohen 3894dbaaff2aSEli Cohen if (fatal) 3895dbaaff2aSEli Cohen ibdev->ib_active = false; 389603404e8aSMaor Gottlieb out: 3897d69a24e0SDaniel Jurgens kfree(work); 3898d69a24e0SDaniel Jurgens } 3899d69a24e0SDaniel Jurgens 3900d69a24e0SDaniel Jurgens static void mlx5_ib_event(struct mlx5_core_dev *dev, void *context, 3901d69a24e0SDaniel Jurgens enum mlx5_dev_event event, unsigned long param) 3902d69a24e0SDaniel Jurgens { 3903d69a24e0SDaniel Jurgens struct mlx5_ib_event_work *work; 3904d69a24e0SDaniel Jurgens 3905d69a24e0SDaniel Jurgens work = kmalloc(sizeof(*work), GFP_ATOMIC); 390610bea9c8SLeon Romanovsky if (!work) 390710bea9c8SLeon Romanovsky return; 390810bea9c8SLeon Romanovsky 3909d69a24e0SDaniel Jurgens INIT_WORK(&work->work, mlx5_ib_handle_event); 3910d69a24e0SDaniel Jurgens work->dev = dev; 3911d69a24e0SDaniel Jurgens work->param = param; 3912d69a24e0SDaniel Jurgens work->context = context; 3913d69a24e0SDaniel Jurgens work->event = event; 3914d69a24e0SDaniel Jurgens 3915d69a24e0SDaniel Jurgens queue_work(mlx5_ib_event_wq, &work->work); 3916d69a24e0SDaniel Jurgens } 3917d69a24e0SDaniel Jurgens 3918c43f1112SMaor Gottlieb static int set_has_smi_cap(struct mlx5_ib_dev *dev) 3919c43f1112SMaor Gottlieb { 3920c43f1112SMaor Gottlieb struct mlx5_hca_vport_context vport_ctx; 3921c43f1112SMaor Gottlieb int err; 3922c43f1112SMaor Gottlieb int port; 3923c43f1112SMaor Gottlieb 3924508562d6SDaniel Jurgens for (port = 1; port <= dev->num_ports; port++) { 3925c43f1112SMaor Gottlieb dev->mdev->port_caps[port - 1].has_smi = false; 3926c43f1112SMaor Gottlieb if (MLX5_CAP_GEN(dev->mdev, port_type) == 3927c43f1112SMaor Gottlieb MLX5_CAP_PORT_TYPE_IB) { 3928c43f1112SMaor Gottlieb if (MLX5_CAP_GEN(dev->mdev, ib_virt)) { 3929c43f1112SMaor Gottlieb err = mlx5_query_hca_vport_context(dev->mdev, 0, 3930c43f1112SMaor Gottlieb port, 0, 3931c43f1112SMaor Gottlieb &vport_ctx); 3932c43f1112SMaor Gottlieb if (err) { 3933c43f1112SMaor Gottlieb mlx5_ib_err(dev, "query_hca_vport_context for port=%d failed %d\n", 3934c43f1112SMaor Gottlieb port, err); 3935c43f1112SMaor Gottlieb return err; 3936c43f1112SMaor Gottlieb } 3937c43f1112SMaor Gottlieb dev->mdev->port_caps[port - 1].has_smi = 3938c43f1112SMaor Gottlieb vport_ctx.has_smi; 3939c43f1112SMaor Gottlieb } else { 3940c43f1112SMaor Gottlieb dev->mdev->port_caps[port - 1].has_smi = true; 3941c43f1112SMaor Gottlieb } 3942c43f1112SMaor Gottlieb } 3943c43f1112SMaor Gottlieb } 3944c43f1112SMaor Gottlieb return 0; 3945c43f1112SMaor Gottlieb } 3946c43f1112SMaor Gottlieb 3947e126ba97SEli Cohen static void get_ext_port_caps(struct mlx5_ib_dev *dev) 3948e126ba97SEli Cohen { 3949e126ba97SEli Cohen int port; 3950e126ba97SEli Cohen 3951508562d6SDaniel Jurgens for (port = 1; port <= dev->num_ports; port++) 3952e126ba97SEli Cohen mlx5_query_ext_port_caps(dev, port); 3953e126ba97SEli Cohen } 3954e126ba97SEli Cohen 395532f69e4bSDaniel Jurgens static int get_port_caps(struct mlx5_ib_dev *dev, u8 port) 3956e126ba97SEli Cohen { 3957e126ba97SEli Cohen struct ib_device_attr *dprops = NULL; 3958e126ba97SEli Cohen struct ib_port_attr *pprops = NULL; 3959f614fc15SDan Carpenter int err = -ENOMEM; 39602528e33eSMatan Barak struct ib_udata uhw = {.inlen = 0, .outlen = 0}; 3961e126ba97SEli Cohen 3962e126ba97SEli Cohen pprops = kmalloc(sizeof(*pprops), GFP_KERNEL); 3963e126ba97SEli Cohen if (!pprops) 3964e126ba97SEli Cohen goto out; 3965e126ba97SEli Cohen 3966e126ba97SEli Cohen dprops = kmalloc(sizeof(*dprops), GFP_KERNEL); 3967e126ba97SEli Cohen if (!dprops) 3968e126ba97SEli Cohen goto out; 3969e126ba97SEli Cohen 3970c43f1112SMaor Gottlieb err = set_has_smi_cap(dev); 3971c43f1112SMaor Gottlieb if (err) 3972c43f1112SMaor Gottlieb goto out; 3973c43f1112SMaor Gottlieb 39742528e33eSMatan Barak err = mlx5_ib_query_device(&dev->ib_dev, dprops, &uhw); 3975e126ba97SEli Cohen if (err) { 3976e126ba97SEli Cohen mlx5_ib_warn(dev, "query_device failed %d\n", err); 3977e126ba97SEli Cohen goto out; 3978e126ba97SEli Cohen } 3979e126ba97SEli Cohen 3980c4550c63SOr Gerlitz memset(pprops, 0, sizeof(*pprops)); 3981e126ba97SEli Cohen err = mlx5_ib_query_port(&dev->ib_dev, port, pprops); 3982e126ba97SEli Cohen if (err) { 3983938fe83cSSaeed Mahameed mlx5_ib_warn(dev, "query_port %d failed %d\n", 3984938fe83cSSaeed Mahameed port, err); 398532f69e4bSDaniel Jurgens goto out; 3986e126ba97SEli Cohen } 398732f69e4bSDaniel Jurgens 3988938fe83cSSaeed Mahameed dev->mdev->port_caps[port - 1].pkey_table_len = 3989938fe83cSSaeed Mahameed dprops->max_pkeys; 3990938fe83cSSaeed Mahameed dev->mdev->port_caps[port - 1].gid_table_len = 3991938fe83cSSaeed Mahameed pprops->gid_tbl_len; 399232f69e4bSDaniel Jurgens mlx5_ib_dbg(dev, "port %d: pkey_table_len %d, gid_table_len %d\n", 399332f69e4bSDaniel Jurgens port, dprops->max_pkeys, pprops->gid_tbl_len); 3994e126ba97SEli Cohen 3995e126ba97SEli Cohen out: 3996e126ba97SEli Cohen kfree(pprops); 3997e126ba97SEli Cohen kfree(dprops); 3998e126ba97SEli Cohen 3999e126ba97SEli Cohen return err; 4000e126ba97SEli Cohen } 4001e126ba97SEli Cohen 4002e126ba97SEli Cohen static void destroy_umrc_res(struct mlx5_ib_dev *dev) 4003e126ba97SEli Cohen { 4004e126ba97SEli Cohen int err; 4005e126ba97SEli Cohen 4006e126ba97SEli Cohen err = mlx5_mr_cache_cleanup(dev); 4007e126ba97SEli Cohen if (err) 4008e126ba97SEli Cohen mlx5_ib_warn(dev, "mr cache cleanup failed\n"); 4009e126ba97SEli Cohen 401032927e28SMark Bloch if (dev->umrc.qp) 4011e126ba97SEli Cohen mlx5_ib_destroy_qp(dev->umrc.qp); 401232927e28SMark Bloch if (dev->umrc.cq) 4013add08d76SChristoph Hellwig ib_free_cq(dev->umrc.cq); 401432927e28SMark Bloch if (dev->umrc.pd) 4015e126ba97SEli Cohen ib_dealloc_pd(dev->umrc.pd); 4016e126ba97SEli Cohen } 4017e126ba97SEli Cohen 4018e126ba97SEli Cohen enum { 4019e126ba97SEli Cohen MAX_UMR_WR = 128, 4020e126ba97SEli Cohen }; 4021e126ba97SEli Cohen 4022e126ba97SEli Cohen static int create_umr_res(struct mlx5_ib_dev *dev) 4023e126ba97SEli Cohen { 4024e126ba97SEli Cohen struct ib_qp_init_attr *init_attr = NULL; 4025e126ba97SEli Cohen struct ib_qp_attr *attr = NULL; 4026e126ba97SEli Cohen struct ib_pd *pd; 4027e126ba97SEli Cohen struct ib_cq *cq; 4028e126ba97SEli Cohen struct ib_qp *qp; 4029e126ba97SEli Cohen int ret; 4030e126ba97SEli Cohen 4031e126ba97SEli Cohen attr = kzalloc(sizeof(*attr), GFP_KERNEL); 4032e126ba97SEli Cohen init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL); 4033e126ba97SEli Cohen if (!attr || !init_attr) { 4034e126ba97SEli Cohen ret = -ENOMEM; 4035e126ba97SEli Cohen goto error_0; 4036e126ba97SEli Cohen } 4037e126ba97SEli Cohen 4038ed082d36SChristoph Hellwig pd = ib_alloc_pd(&dev->ib_dev, 0); 4039e126ba97SEli Cohen if (IS_ERR(pd)) { 4040e126ba97SEli Cohen mlx5_ib_dbg(dev, "Couldn't create PD for sync UMR QP\n"); 4041e126ba97SEli Cohen ret = PTR_ERR(pd); 4042e126ba97SEli Cohen goto error_0; 4043e126ba97SEli Cohen } 4044e126ba97SEli Cohen 4045add08d76SChristoph Hellwig cq = ib_alloc_cq(&dev->ib_dev, NULL, 128, 0, IB_POLL_SOFTIRQ); 4046e126ba97SEli Cohen if (IS_ERR(cq)) { 4047e126ba97SEli Cohen mlx5_ib_dbg(dev, "Couldn't create CQ for sync UMR QP\n"); 4048e126ba97SEli Cohen ret = PTR_ERR(cq); 4049e126ba97SEli Cohen goto error_2; 4050e126ba97SEli Cohen } 4051e126ba97SEli Cohen 4052e126ba97SEli Cohen init_attr->send_cq = cq; 4053e126ba97SEli Cohen init_attr->recv_cq = cq; 4054e126ba97SEli Cohen init_attr->sq_sig_type = IB_SIGNAL_ALL_WR; 4055e126ba97SEli Cohen init_attr->cap.max_send_wr = MAX_UMR_WR; 4056e126ba97SEli Cohen init_attr->cap.max_send_sge = 1; 4057e126ba97SEli Cohen init_attr->qp_type = MLX5_IB_QPT_REG_UMR; 4058e126ba97SEli Cohen init_attr->port_num = 1; 4059e126ba97SEli Cohen qp = mlx5_ib_create_qp(pd, init_attr, NULL); 4060e126ba97SEli Cohen if (IS_ERR(qp)) { 4061e126ba97SEli Cohen mlx5_ib_dbg(dev, "Couldn't create sync UMR QP\n"); 4062e126ba97SEli Cohen ret = PTR_ERR(qp); 4063e126ba97SEli Cohen goto error_3; 4064e126ba97SEli Cohen } 4065e126ba97SEli Cohen qp->device = &dev->ib_dev; 4066e126ba97SEli Cohen qp->real_qp = qp; 4067e126ba97SEli Cohen qp->uobject = NULL; 4068e126ba97SEli Cohen qp->qp_type = MLX5_IB_QPT_REG_UMR; 406931fde034SMajd Dibbiny qp->send_cq = init_attr->send_cq; 407031fde034SMajd Dibbiny qp->recv_cq = init_attr->recv_cq; 4071e126ba97SEli Cohen 4072e126ba97SEli Cohen attr->qp_state = IB_QPS_INIT; 4073e126ba97SEli Cohen attr->port_num = 1; 4074e126ba97SEli Cohen ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_PKEY_INDEX | 4075e126ba97SEli Cohen IB_QP_PORT, NULL); 4076e126ba97SEli Cohen if (ret) { 4077e126ba97SEli Cohen mlx5_ib_dbg(dev, "Couldn't modify UMR QP\n"); 4078e126ba97SEli Cohen goto error_4; 4079e126ba97SEli Cohen } 4080e126ba97SEli Cohen 4081e126ba97SEli Cohen memset(attr, 0, sizeof(*attr)); 4082e126ba97SEli Cohen attr->qp_state = IB_QPS_RTR; 4083e126ba97SEli Cohen attr->path_mtu = IB_MTU_256; 4084e126ba97SEli Cohen 4085e126ba97SEli Cohen ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL); 4086e126ba97SEli Cohen if (ret) { 4087e126ba97SEli Cohen mlx5_ib_dbg(dev, "Couldn't modify umr QP to rtr\n"); 4088e126ba97SEli Cohen goto error_4; 4089e126ba97SEli Cohen } 4090e126ba97SEli Cohen 4091e126ba97SEli Cohen memset(attr, 0, sizeof(*attr)); 4092e126ba97SEli Cohen attr->qp_state = IB_QPS_RTS; 4093e126ba97SEli Cohen ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL); 4094e126ba97SEli Cohen if (ret) { 4095e126ba97SEli Cohen mlx5_ib_dbg(dev, "Couldn't modify umr QP to rts\n"); 4096e126ba97SEli Cohen goto error_4; 4097e126ba97SEli Cohen } 4098e126ba97SEli Cohen 4099e126ba97SEli Cohen dev->umrc.qp = qp; 4100e126ba97SEli Cohen dev->umrc.cq = cq; 4101e126ba97SEli Cohen dev->umrc.pd = pd; 4102e126ba97SEli Cohen 4103e126ba97SEli Cohen sema_init(&dev->umrc.sem, MAX_UMR_WR); 4104e126ba97SEli Cohen ret = mlx5_mr_cache_init(dev); 4105e126ba97SEli Cohen if (ret) { 4106e126ba97SEli Cohen mlx5_ib_warn(dev, "mr cache init failed %d\n", ret); 4107e126ba97SEli Cohen goto error_4; 4108e126ba97SEli Cohen } 4109e126ba97SEli Cohen 4110e126ba97SEli Cohen kfree(attr); 4111e126ba97SEli Cohen kfree(init_attr); 4112e126ba97SEli Cohen 4113e126ba97SEli Cohen return 0; 4114e126ba97SEli Cohen 4115e126ba97SEli Cohen error_4: 4116e126ba97SEli Cohen mlx5_ib_destroy_qp(qp); 411732927e28SMark Bloch dev->umrc.qp = NULL; 4118e126ba97SEli Cohen 4119e126ba97SEli Cohen error_3: 4120add08d76SChristoph Hellwig ib_free_cq(cq); 412132927e28SMark Bloch dev->umrc.cq = NULL; 4122e126ba97SEli Cohen 4123e126ba97SEli Cohen error_2: 4124e126ba97SEli Cohen ib_dealloc_pd(pd); 412532927e28SMark Bloch dev->umrc.pd = NULL; 4126e126ba97SEli Cohen 4127e126ba97SEli Cohen error_0: 4128e126ba97SEli Cohen kfree(attr); 4129e126ba97SEli Cohen kfree(init_attr); 4130e126ba97SEli Cohen return ret; 4131e126ba97SEli Cohen } 4132e126ba97SEli Cohen 41336e8484c5SMax Gurtovoy static u8 mlx5_get_umr_fence(u8 umr_fence_cap) 41346e8484c5SMax Gurtovoy { 41356e8484c5SMax Gurtovoy switch (umr_fence_cap) { 41366e8484c5SMax Gurtovoy case MLX5_CAP_UMR_FENCE_NONE: 41376e8484c5SMax Gurtovoy return MLX5_FENCE_MODE_NONE; 41386e8484c5SMax Gurtovoy case MLX5_CAP_UMR_FENCE_SMALL: 41396e8484c5SMax Gurtovoy return MLX5_FENCE_MODE_INITIATOR_SMALL; 41406e8484c5SMax Gurtovoy default: 41416e8484c5SMax Gurtovoy return MLX5_FENCE_MODE_STRONG_ORDERING; 41426e8484c5SMax Gurtovoy } 41436e8484c5SMax Gurtovoy } 41446e8484c5SMax Gurtovoy 4145e126ba97SEli Cohen static int create_dev_resources(struct mlx5_ib_resources *devr) 4146e126ba97SEli Cohen { 4147e126ba97SEli Cohen struct ib_srq_init_attr attr; 4148e126ba97SEli Cohen struct mlx5_ib_dev *dev; 4149bcf4c1eaSMatan Barak struct ib_cq_init_attr cq_attr = {.cqe = 1}; 41507722f47eSHaggai Eran int port; 4151e126ba97SEli Cohen int ret = 0; 4152e126ba97SEli Cohen 4153e126ba97SEli Cohen dev = container_of(devr, struct mlx5_ib_dev, devr); 4154e126ba97SEli Cohen 4155d16e91daSHaggai Eran mutex_init(&devr->mutex); 4156d16e91daSHaggai Eran 4157e126ba97SEli Cohen devr->p0 = mlx5_ib_alloc_pd(&dev->ib_dev, NULL, NULL); 4158e126ba97SEli Cohen if (IS_ERR(devr->p0)) { 4159e126ba97SEli Cohen ret = PTR_ERR(devr->p0); 4160e126ba97SEli Cohen goto error0; 4161e126ba97SEli Cohen } 4162e126ba97SEli Cohen devr->p0->device = &dev->ib_dev; 4163e126ba97SEli Cohen devr->p0->uobject = NULL; 4164e126ba97SEli Cohen atomic_set(&devr->p0->usecnt, 0); 4165e126ba97SEli Cohen 4166bcf4c1eaSMatan Barak devr->c0 = mlx5_ib_create_cq(&dev->ib_dev, &cq_attr, NULL, NULL); 4167e126ba97SEli Cohen if (IS_ERR(devr->c0)) { 4168e126ba97SEli Cohen ret = PTR_ERR(devr->c0); 4169e126ba97SEli Cohen goto error1; 4170e126ba97SEli Cohen } 4171e126ba97SEli Cohen devr->c0->device = &dev->ib_dev; 4172e126ba97SEli Cohen devr->c0->uobject = NULL; 4173e126ba97SEli Cohen devr->c0->comp_handler = NULL; 4174e126ba97SEli Cohen devr->c0->event_handler = NULL; 4175e126ba97SEli Cohen devr->c0->cq_context = NULL; 4176e126ba97SEli Cohen atomic_set(&devr->c0->usecnt, 0); 4177e126ba97SEli Cohen 4178e126ba97SEli Cohen devr->x0 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL); 4179e126ba97SEli Cohen if (IS_ERR(devr->x0)) { 4180e126ba97SEli Cohen ret = PTR_ERR(devr->x0); 4181e126ba97SEli Cohen goto error2; 4182e126ba97SEli Cohen } 4183e126ba97SEli Cohen devr->x0->device = &dev->ib_dev; 4184e126ba97SEli Cohen devr->x0->inode = NULL; 4185e126ba97SEli Cohen atomic_set(&devr->x0->usecnt, 0); 4186e126ba97SEli Cohen mutex_init(&devr->x0->tgt_qp_mutex); 4187e126ba97SEli Cohen INIT_LIST_HEAD(&devr->x0->tgt_qp_list); 4188e126ba97SEli Cohen 4189e126ba97SEli Cohen devr->x1 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL); 4190e126ba97SEli Cohen if (IS_ERR(devr->x1)) { 4191e126ba97SEli Cohen ret = PTR_ERR(devr->x1); 4192e126ba97SEli Cohen goto error3; 4193e126ba97SEli Cohen } 4194e126ba97SEli Cohen devr->x1->device = &dev->ib_dev; 4195e126ba97SEli Cohen devr->x1->inode = NULL; 4196e126ba97SEli Cohen atomic_set(&devr->x1->usecnt, 0); 4197e126ba97SEli Cohen mutex_init(&devr->x1->tgt_qp_mutex); 4198e126ba97SEli Cohen INIT_LIST_HEAD(&devr->x1->tgt_qp_list); 4199e126ba97SEli Cohen 4200e126ba97SEli Cohen memset(&attr, 0, sizeof(attr)); 4201e126ba97SEli Cohen attr.attr.max_sge = 1; 4202e126ba97SEli Cohen attr.attr.max_wr = 1; 4203e126ba97SEli Cohen attr.srq_type = IB_SRQT_XRC; 42041a56ff6dSArtemy Kovalyov attr.ext.cq = devr->c0; 4205e126ba97SEli Cohen attr.ext.xrc.xrcd = devr->x0; 4206e126ba97SEli Cohen 4207e126ba97SEli Cohen devr->s0 = mlx5_ib_create_srq(devr->p0, &attr, NULL); 4208e126ba97SEli Cohen if (IS_ERR(devr->s0)) { 4209e126ba97SEli Cohen ret = PTR_ERR(devr->s0); 4210e126ba97SEli Cohen goto error4; 4211e126ba97SEli Cohen } 4212e126ba97SEli Cohen devr->s0->device = &dev->ib_dev; 4213e126ba97SEli Cohen devr->s0->pd = devr->p0; 4214e126ba97SEli Cohen devr->s0->uobject = NULL; 4215e126ba97SEli Cohen devr->s0->event_handler = NULL; 4216e126ba97SEli Cohen devr->s0->srq_context = NULL; 4217e126ba97SEli Cohen devr->s0->srq_type = IB_SRQT_XRC; 4218e126ba97SEli Cohen devr->s0->ext.xrc.xrcd = devr->x0; 42191a56ff6dSArtemy Kovalyov devr->s0->ext.cq = devr->c0; 4220e126ba97SEli Cohen atomic_inc(&devr->s0->ext.xrc.xrcd->usecnt); 42211a56ff6dSArtemy Kovalyov atomic_inc(&devr->s0->ext.cq->usecnt); 4222e126ba97SEli Cohen atomic_inc(&devr->p0->usecnt); 4223e126ba97SEli Cohen atomic_set(&devr->s0->usecnt, 0); 4224e126ba97SEli Cohen 42254aa17b28SHaggai Abramonvsky memset(&attr, 0, sizeof(attr)); 42264aa17b28SHaggai Abramonvsky attr.attr.max_sge = 1; 42274aa17b28SHaggai Abramonvsky attr.attr.max_wr = 1; 42284aa17b28SHaggai Abramonvsky attr.srq_type = IB_SRQT_BASIC; 42294aa17b28SHaggai Abramonvsky devr->s1 = mlx5_ib_create_srq(devr->p0, &attr, NULL); 42304aa17b28SHaggai Abramonvsky if (IS_ERR(devr->s1)) { 42314aa17b28SHaggai Abramonvsky ret = PTR_ERR(devr->s1); 42324aa17b28SHaggai Abramonvsky goto error5; 42334aa17b28SHaggai Abramonvsky } 42344aa17b28SHaggai Abramonvsky devr->s1->device = &dev->ib_dev; 42354aa17b28SHaggai Abramonvsky devr->s1->pd = devr->p0; 42364aa17b28SHaggai Abramonvsky devr->s1->uobject = NULL; 42374aa17b28SHaggai Abramonvsky devr->s1->event_handler = NULL; 42384aa17b28SHaggai Abramonvsky devr->s1->srq_context = NULL; 42394aa17b28SHaggai Abramonvsky devr->s1->srq_type = IB_SRQT_BASIC; 42401a56ff6dSArtemy Kovalyov devr->s1->ext.cq = devr->c0; 42414aa17b28SHaggai Abramonvsky atomic_inc(&devr->p0->usecnt); 42421a56ff6dSArtemy Kovalyov atomic_set(&devr->s1->usecnt, 0); 42434aa17b28SHaggai Abramonvsky 42447722f47eSHaggai Eran for (port = 0; port < ARRAY_SIZE(devr->ports); ++port) { 42457722f47eSHaggai Eran INIT_WORK(&devr->ports[port].pkey_change_work, 42467722f47eSHaggai Eran pkey_change_handler); 42477722f47eSHaggai Eran devr->ports[port].devr = devr; 42487722f47eSHaggai Eran } 42497722f47eSHaggai Eran 4250e126ba97SEli Cohen return 0; 4251e126ba97SEli Cohen 42524aa17b28SHaggai Abramonvsky error5: 42534aa17b28SHaggai Abramonvsky mlx5_ib_destroy_srq(devr->s0); 4254e126ba97SEli Cohen error4: 4255e126ba97SEli Cohen mlx5_ib_dealloc_xrcd(devr->x1); 4256e126ba97SEli Cohen error3: 4257e126ba97SEli Cohen mlx5_ib_dealloc_xrcd(devr->x0); 4258e126ba97SEli Cohen error2: 4259e126ba97SEli Cohen mlx5_ib_destroy_cq(devr->c0); 4260e126ba97SEli Cohen error1: 4261e126ba97SEli Cohen mlx5_ib_dealloc_pd(devr->p0); 4262e126ba97SEli Cohen error0: 4263e126ba97SEli Cohen return ret; 4264e126ba97SEli Cohen } 4265e126ba97SEli Cohen 4266e126ba97SEli Cohen static void destroy_dev_resources(struct mlx5_ib_resources *devr) 4267e126ba97SEli Cohen { 42687722f47eSHaggai Eran struct mlx5_ib_dev *dev = 42697722f47eSHaggai Eran container_of(devr, struct mlx5_ib_dev, devr); 42707722f47eSHaggai Eran int port; 42717722f47eSHaggai Eran 42724aa17b28SHaggai Abramonvsky mlx5_ib_destroy_srq(devr->s1); 4273e126ba97SEli Cohen mlx5_ib_destroy_srq(devr->s0); 4274e126ba97SEli Cohen mlx5_ib_dealloc_xrcd(devr->x0); 4275e126ba97SEli Cohen mlx5_ib_dealloc_xrcd(devr->x1); 4276e126ba97SEli Cohen mlx5_ib_destroy_cq(devr->c0); 4277e126ba97SEli Cohen mlx5_ib_dealloc_pd(devr->p0); 42787722f47eSHaggai Eran 42797722f47eSHaggai Eran /* Make sure no change P_Key work items are still executing */ 42807722f47eSHaggai Eran for (port = 0; port < dev->num_ports; ++port) 42817722f47eSHaggai Eran cancel_work_sync(&devr->ports[port].pkey_change_work); 4282e126ba97SEli Cohen } 4283e126ba97SEli Cohen 4284e53505a8SAchiad Shochat static u32 get_core_cap_flags(struct ib_device *ibdev) 4285e53505a8SAchiad Shochat { 4286e53505a8SAchiad Shochat struct mlx5_ib_dev *dev = to_mdev(ibdev); 4287e53505a8SAchiad Shochat enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, 1); 4288e53505a8SAchiad Shochat u8 l3_type_cap = MLX5_CAP_ROCE(dev->mdev, l3_type); 4289e53505a8SAchiad Shochat u8 roce_version_cap = MLX5_CAP_ROCE(dev->mdev, roce_version); 429085c7c014SDaniel Jurgens bool raw_support = !mlx5_core_mp_enabled(dev->mdev); 4291e53505a8SAchiad Shochat u32 ret = 0; 4292e53505a8SAchiad Shochat 4293e53505a8SAchiad Shochat if (ll == IB_LINK_LAYER_INFINIBAND) 4294e53505a8SAchiad Shochat return RDMA_CORE_PORT_IBA_IB; 4295e53505a8SAchiad Shochat 429685c7c014SDaniel Jurgens if (raw_support) 429772cd5717SOr Gerlitz ret = RDMA_CORE_PORT_RAW_PACKET; 429872cd5717SOr Gerlitz 4299e53505a8SAchiad Shochat if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV4_CAP)) 430072cd5717SOr Gerlitz return ret; 4301e53505a8SAchiad Shochat 4302e53505a8SAchiad Shochat if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV6_CAP)) 430372cd5717SOr Gerlitz return ret; 4304e53505a8SAchiad Shochat 4305e53505a8SAchiad Shochat if (roce_version_cap & MLX5_ROCE_VERSION_1_CAP) 4306e53505a8SAchiad Shochat ret |= RDMA_CORE_PORT_IBA_ROCE; 4307e53505a8SAchiad Shochat 4308e53505a8SAchiad Shochat if (roce_version_cap & MLX5_ROCE_VERSION_2_CAP) 4309e53505a8SAchiad Shochat ret |= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP; 4310e53505a8SAchiad Shochat 4311e53505a8SAchiad Shochat return ret; 4312e53505a8SAchiad Shochat } 4313e53505a8SAchiad Shochat 43147738613eSIra Weiny static int mlx5_port_immutable(struct ib_device *ibdev, u8 port_num, 43157738613eSIra Weiny struct ib_port_immutable *immutable) 43167738613eSIra Weiny { 43177738613eSIra Weiny struct ib_port_attr attr; 4318ca5b91d6SOr Gerlitz struct mlx5_ib_dev *dev = to_mdev(ibdev); 4319ca5b91d6SOr Gerlitz enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, port_num); 43207738613eSIra Weiny int err; 43217738613eSIra Weiny 4322c4550c63SOr Gerlitz immutable->core_cap_flags = get_core_cap_flags(ibdev); 4323c4550c63SOr Gerlitz 4324c4550c63SOr Gerlitz err = ib_query_port(ibdev, port_num, &attr); 43257738613eSIra Weiny if (err) 43267738613eSIra Weiny return err; 43277738613eSIra Weiny 43287738613eSIra Weiny immutable->pkey_tbl_len = attr.pkey_tbl_len; 43297738613eSIra Weiny immutable->gid_tbl_len = attr.gid_tbl_len; 4330e53505a8SAchiad Shochat immutable->core_cap_flags = get_core_cap_flags(ibdev); 4331ca5b91d6SOr Gerlitz if ((ll == IB_LINK_LAYER_INFINIBAND) || MLX5_CAP_GEN(dev->mdev, roce)) 4332337877a4SIra Weiny immutable->max_mad_size = IB_MGMT_MAD_SIZE; 43337738613eSIra Weiny 43347738613eSIra Weiny return 0; 43357738613eSIra Weiny } 43367738613eSIra Weiny 43378e6efa3aSMark Bloch static int mlx5_port_rep_immutable(struct ib_device *ibdev, u8 port_num, 43388e6efa3aSMark Bloch struct ib_port_immutable *immutable) 43398e6efa3aSMark Bloch { 43408e6efa3aSMark Bloch struct ib_port_attr attr; 43418e6efa3aSMark Bloch int err; 43428e6efa3aSMark Bloch 43438e6efa3aSMark Bloch immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET; 43448e6efa3aSMark Bloch 43458e6efa3aSMark Bloch err = ib_query_port(ibdev, port_num, &attr); 43468e6efa3aSMark Bloch if (err) 43478e6efa3aSMark Bloch return err; 43488e6efa3aSMark Bloch 43498e6efa3aSMark Bloch immutable->pkey_tbl_len = attr.pkey_tbl_len; 43508e6efa3aSMark Bloch immutable->gid_tbl_len = attr.gid_tbl_len; 43518e6efa3aSMark Bloch immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET; 43528e6efa3aSMark Bloch 43538e6efa3aSMark Bloch return 0; 43548e6efa3aSMark Bloch } 43558e6efa3aSMark Bloch 43569abb0d1bSLeon Romanovsky static void get_dev_fw_str(struct ib_device *ibdev, char *str) 4357c7342823SIra Weiny { 4358c7342823SIra Weiny struct mlx5_ib_dev *dev = 4359c7342823SIra Weiny container_of(ibdev, struct mlx5_ib_dev, ib_dev); 43609abb0d1bSLeon Romanovsky snprintf(str, IB_FW_VERSION_NAME_MAX, "%d.%d.%04d", 43619abb0d1bSLeon Romanovsky fw_rev_maj(dev->mdev), fw_rev_min(dev->mdev), 43629abb0d1bSLeon Romanovsky fw_rev_sub(dev->mdev)); 4363c7342823SIra Weiny } 4364c7342823SIra Weiny 436545f95acdSOr Gerlitz static int mlx5_eth_lag_init(struct mlx5_ib_dev *dev) 43669ef9c640SAviv Heller { 43679ef9c640SAviv Heller struct mlx5_core_dev *mdev = dev->mdev; 43689ef9c640SAviv Heller struct mlx5_flow_namespace *ns = mlx5_get_flow_namespace(mdev, 43699ef9c640SAviv Heller MLX5_FLOW_NAMESPACE_LAG); 43709ef9c640SAviv Heller struct mlx5_flow_table *ft; 43719ef9c640SAviv Heller int err; 43729ef9c640SAviv Heller 43739ef9c640SAviv Heller if (!ns || !mlx5_lag_is_active(mdev)) 43749ef9c640SAviv Heller return 0; 43759ef9c640SAviv Heller 43769ef9c640SAviv Heller err = mlx5_cmd_create_vport_lag(mdev); 43779ef9c640SAviv Heller if (err) 43789ef9c640SAviv Heller return err; 43799ef9c640SAviv Heller 43809ef9c640SAviv Heller ft = mlx5_create_lag_demux_flow_table(ns, 0, 0); 43819ef9c640SAviv Heller if (IS_ERR(ft)) { 43829ef9c640SAviv Heller err = PTR_ERR(ft); 43839ef9c640SAviv Heller goto err_destroy_vport_lag; 43849ef9c640SAviv Heller } 43859ef9c640SAviv Heller 43869a4ca38dSMark Bloch dev->flow_db->lag_demux_ft = ft; 43879ef9c640SAviv Heller return 0; 43889ef9c640SAviv Heller 43899ef9c640SAviv Heller err_destroy_vport_lag: 43909ef9c640SAviv Heller mlx5_cmd_destroy_vport_lag(mdev); 43919ef9c640SAviv Heller return err; 43929ef9c640SAviv Heller } 43939ef9c640SAviv Heller 439445f95acdSOr Gerlitz static void mlx5_eth_lag_cleanup(struct mlx5_ib_dev *dev) 43959ef9c640SAviv Heller { 43969ef9c640SAviv Heller struct mlx5_core_dev *mdev = dev->mdev; 43979ef9c640SAviv Heller 43989a4ca38dSMark Bloch if (dev->flow_db->lag_demux_ft) { 43999a4ca38dSMark Bloch mlx5_destroy_flow_table(dev->flow_db->lag_demux_ft); 44009a4ca38dSMark Bloch dev->flow_db->lag_demux_ft = NULL; 44019ef9c640SAviv Heller 44029ef9c640SAviv Heller mlx5_cmd_destroy_vport_lag(mdev); 44039ef9c640SAviv Heller } 44049ef9c640SAviv Heller } 44059ef9c640SAviv Heller 44067fd8aefbSDaniel Jurgens static int mlx5_add_netdev_notifier(struct mlx5_ib_dev *dev, u8 port_num) 4407fc24fc5eSAchiad Shochat { 4408e53505a8SAchiad Shochat int err; 4409e53505a8SAchiad Shochat 44107fd8aefbSDaniel Jurgens dev->roce[port_num].nb.notifier_call = mlx5_netdev_event; 44117fd8aefbSDaniel Jurgens err = register_netdevice_notifier(&dev->roce[port_num].nb); 44125ec8c83eSAviv Heller if (err) { 44137fd8aefbSDaniel Jurgens dev->roce[port_num].nb.notifier_call = NULL; 4414e53505a8SAchiad Shochat return err; 44155ec8c83eSAviv Heller } 4416e53505a8SAchiad Shochat 4417d012f5d6SOr Gerlitz return 0; 4418d012f5d6SOr Gerlitz } 4419d012f5d6SOr Gerlitz 44207fd8aefbSDaniel Jurgens static void mlx5_remove_netdev_notifier(struct mlx5_ib_dev *dev, u8 port_num) 4421e126ba97SEli Cohen { 44227fd8aefbSDaniel Jurgens if (dev->roce[port_num].nb.notifier_call) { 44237fd8aefbSDaniel Jurgens unregister_netdevice_notifier(&dev->roce[port_num].nb); 44247fd8aefbSDaniel Jurgens dev->roce[port_num].nb.notifier_call = NULL; 4425e126ba97SEli Cohen } 4426e126ba97SEli Cohen } 4427e126ba97SEli Cohen 44287fd8aefbSDaniel Jurgens static int mlx5_enable_eth(struct mlx5_ib_dev *dev, u8 port_num) 4429e126ba97SEli Cohen { 4430e126ba97SEli Cohen int err; 4431e126ba97SEli Cohen 4432ca5b91d6SOr Gerlitz if (MLX5_CAP_GEN(dev->mdev, roce)) { 4433e53505a8SAchiad Shochat err = mlx5_nic_vport_enable_roce(dev->mdev); 4434e53505a8SAchiad Shochat if (err) 44358e6efa3aSMark Bloch return err; 4436ca5b91d6SOr Gerlitz } 4437e53505a8SAchiad Shochat 443845f95acdSOr Gerlitz err = mlx5_eth_lag_init(dev); 44399ef9c640SAviv Heller if (err) 44409ef9c640SAviv Heller goto err_disable_roce; 44419ef9c640SAviv Heller 4442e53505a8SAchiad Shochat return 0; 4443e53505a8SAchiad Shochat 44449ef9c640SAviv Heller err_disable_roce: 4445ca5b91d6SOr Gerlitz if (MLX5_CAP_GEN(dev->mdev, roce)) 44469ef9c640SAviv Heller mlx5_nic_vport_disable_roce(dev->mdev); 44479ef9c640SAviv Heller 4448e53505a8SAchiad Shochat return err; 4449fc24fc5eSAchiad Shochat } 4450fc24fc5eSAchiad Shochat 445145f95acdSOr Gerlitz static void mlx5_disable_eth(struct mlx5_ib_dev *dev) 4452fc24fc5eSAchiad Shochat { 445345f95acdSOr Gerlitz mlx5_eth_lag_cleanup(dev); 4454ca5b91d6SOr Gerlitz if (MLX5_CAP_GEN(dev->mdev, roce)) 4455e53505a8SAchiad Shochat mlx5_nic_vport_disable_roce(dev->mdev); 4456fc24fc5eSAchiad Shochat } 4457fc24fc5eSAchiad Shochat 4458e1f24a79SParav Pandit struct mlx5_ib_counter { 44597c16f477SKamal Heib const char *name; 44607c16f477SKamal Heib size_t offset; 44617c16f477SKamal Heib }; 44627c16f477SKamal Heib 44637c16f477SKamal Heib #define INIT_Q_COUNTER(_name) \ 44647c16f477SKamal Heib { .name = #_name, .offset = MLX5_BYTE_OFF(query_q_counter_out, _name)} 44657c16f477SKamal Heib 4466e1f24a79SParav Pandit static const struct mlx5_ib_counter basic_q_cnts[] = { 44677c16f477SKamal Heib INIT_Q_COUNTER(rx_write_requests), 44687c16f477SKamal Heib INIT_Q_COUNTER(rx_read_requests), 44697c16f477SKamal Heib INIT_Q_COUNTER(rx_atomic_requests), 44707c16f477SKamal Heib INIT_Q_COUNTER(out_of_buffer), 44717c16f477SKamal Heib }; 44727c16f477SKamal Heib 4473e1f24a79SParav Pandit static const struct mlx5_ib_counter out_of_seq_q_cnts[] = { 44747c16f477SKamal Heib INIT_Q_COUNTER(out_of_sequence), 44757c16f477SKamal Heib }; 44767c16f477SKamal Heib 4477e1f24a79SParav Pandit static const struct mlx5_ib_counter retrans_q_cnts[] = { 44787c16f477SKamal Heib INIT_Q_COUNTER(duplicate_request), 44797c16f477SKamal Heib INIT_Q_COUNTER(rnr_nak_retry_err), 44807c16f477SKamal Heib INIT_Q_COUNTER(packet_seq_err), 44817c16f477SKamal Heib INIT_Q_COUNTER(implied_nak_seq_err), 44827c16f477SKamal Heib INIT_Q_COUNTER(local_ack_timeout_err), 44837c16f477SKamal Heib }; 44847c16f477SKamal Heib 4485e1f24a79SParav Pandit #define INIT_CONG_COUNTER(_name) \ 4486e1f24a79SParav Pandit { .name = #_name, .offset = \ 4487e1f24a79SParav Pandit MLX5_BYTE_OFF(query_cong_statistics_out, _name ## _high)} 4488e1f24a79SParav Pandit 4489e1f24a79SParav Pandit static const struct mlx5_ib_counter cong_cnts[] = { 4490e1f24a79SParav Pandit INIT_CONG_COUNTER(rp_cnp_ignored), 4491e1f24a79SParav Pandit INIT_CONG_COUNTER(rp_cnp_handled), 4492e1f24a79SParav Pandit INIT_CONG_COUNTER(np_ecn_marked_roce_packets), 4493e1f24a79SParav Pandit INIT_CONG_COUNTER(np_cnp_sent), 4494e1f24a79SParav Pandit }; 4495e1f24a79SParav Pandit 449658dcb60aSParav Pandit static const struct mlx5_ib_counter extended_err_cnts[] = { 449758dcb60aSParav Pandit INIT_Q_COUNTER(resp_local_length_error), 449858dcb60aSParav Pandit INIT_Q_COUNTER(resp_cqe_error), 449958dcb60aSParav Pandit INIT_Q_COUNTER(req_cqe_error), 450058dcb60aSParav Pandit INIT_Q_COUNTER(req_remote_invalid_request), 450158dcb60aSParav Pandit INIT_Q_COUNTER(req_remote_access_errors), 450258dcb60aSParav Pandit INIT_Q_COUNTER(resp_remote_access_errors), 450358dcb60aSParav Pandit INIT_Q_COUNTER(resp_cqe_flush_error), 450458dcb60aSParav Pandit INIT_Q_COUNTER(req_cqe_flush_error), 450558dcb60aSParav Pandit }; 450658dcb60aSParav Pandit 4507e1f24a79SParav Pandit static void mlx5_ib_dealloc_counters(struct mlx5_ib_dev *dev) 45080837e86aSMark Bloch { 4509aac4492eSDaniel Jurgens int i; 45100837e86aSMark Bloch 45117c16f477SKamal Heib for (i = 0; i < dev->num_ports; i++) { 4512aac4492eSDaniel Jurgens if (dev->port[i].cnts.set_id) 45130837e86aSMark Bloch mlx5_core_dealloc_q_counter(dev->mdev, 4514e1f24a79SParav Pandit dev->port[i].cnts.set_id); 4515e1f24a79SParav Pandit kfree(dev->port[i].cnts.names); 4516e1f24a79SParav Pandit kfree(dev->port[i].cnts.offsets); 45177c16f477SKamal Heib } 45187c16f477SKamal Heib } 45197c16f477SKamal Heib 4520e1f24a79SParav Pandit static int __mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev, 4521e1f24a79SParav Pandit struct mlx5_ib_counters *cnts) 45227c16f477SKamal Heib { 45237c16f477SKamal Heib u32 num_counters; 45247c16f477SKamal Heib 45257c16f477SKamal Heib num_counters = ARRAY_SIZE(basic_q_cnts); 45267c16f477SKamal Heib 45277c16f477SKamal Heib if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt)) 45287c16f477SKamal Heib num_counters += ARRAY_SIZE(out_of_seq_q_cnts); 45297c16f477SKamal Heib 45307c16f477SKamal Heib if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters)) 45317c16f477SKamal Heib num_counters += ARRAY_SIZE(retrans_q_cnts); 453258dcb60aSParav Pandit 453358dcb60aSParav Pandit if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters)) 453458dcb60aSParav Pandit num_counters += ARRAY_SIZE(extended_err_cnts); 453558dcb60aSParav Pandit 4536e1f24a79SParav Pandit cnts->num_q_counters = num_counters; 45377c16f477SKamal Heib 4538e1f24a79SParav Pandit if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) { 4539e1f24a79SParav Pandit cnts->num_cong_counters = ARRAY_SIZE(cong_cnts); 4540e1f24a79SParav Pandit num_counters += ARRAY_SIZE(cong_cnts); 4541e1f24a79SParav Pandit } 4542e1f24a79SParav Pandit 4543e1f24a79SParav Pandit cnts->names = kcalloc(num_counters, sizeof(cnts->names), GFP_KERNEL); 4544e1f24a79SParav Pandit if (!cnts->names) 45457c16f477SKamal Heib return -ENOMEM; 45467c16f477SKamal Heib 4547e1f24a79SParav Pandit cnts->offsets = kcalloc(num_counters, 4548e1f24a79SParav Pandit sizeof(cnts->offsets), GFP_KERNEL); 4549e1f24a79SParav Pandit if (!cnts->offsets) 45507c16f477SKamal Heib goto err_names; 45517c16f477SKamal Heib 45527c16f477SKamal Heib return 0; 45537c16f477SKamal Heib 45547c16f477SKamal Heib err_names: 4555e1f24a79SParav Pandit kfree(cnts->names); 4556aac4492eSDaniel Jurgens cnts->names = NULL; 45577c16f477SKamal Heib return -ENOMEM; 45587c16f477SKamal Heib } 45597c16f477SKamal Heib 4560e1f24a79SParav Pandit static void mlx5_ib_fill_counters(struct mlx5_ib_dev *dev, 45617c16f477SKamal Heib const char **names, 45627c16f477SKamal Heib size_t *offsets) 45637c16f477SKamal Heib { 45647c16f477SKamal Heib int i; 45657c16f477SKamal Heib int j = 0; 45667c16f477SKamal Heib 45677c16f477SKamal Heib for (i = 0; i < ARRAY_SIZE(basic_q_cnts); i++, j++) { 45687c16f477SKamal Heib names[j] = basic_q_cnts[i].name; 45697c16f477SKamal Heib offsets[j] = basic_q_cnts[i].offset; 45707c16f477SKamal Heib } 45717c16f477SKamal Heib 45727c16f477SKamal Heib if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt)) { 45737c16f477SKamal Heib for (i = 0; i < ARRAY_SIZE(out_of_seq_q_cnts); i++, j++) { 45747c16f477SKamal Heib names[j] = out_of_seq_q_cnts[i].name; 45757c16f477SKamal Heib offsets[j] = out_of_seq_q_cnts[i].offset; 45767c16f477SKamal Heib } 45777c16f477SKamal Heib } 45787c16f477SKamal Heib 45797c16f477SKamal Heib if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters)) { 45807c16f477SKamal Heib for (i = 0; i < ARRAY_SIZE(retrans_q_cnts); i++, j++) { 45817c16f477SKamal Heib names[j] = retrans_q_cnts[i].name; 45827c16f477SKamal Heib offsets[j] = retrans_q_cnts[i].offset; 45837c16f477SKamal Heib } 45847c16f477SKamal Heib } 4585e1f24a79SParav Pandit 458658dcb60aSParav Pandit if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters)) { 458758dcb60aSParav Pandit for (i = 0; i < ARRAY_SIZE(extended_err_cnts); i++, j++) { 458858dcb60aSParav Pandit names[j] = extended_err_cnts[i].name; 458958dcb60aSParav Pandit offsets[j] = extended_err_cnts[i].offset; 459058dcb60aSParav Pandit } 459158dcb60aSParav Pandit } 459258dcb60aSParav Pandit 4593e1f24a79SParav Pandit if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) { 4594e1f24a79SParav Pandit for (i = 0; i < ARRAY_SIZE(cong_cnts); i++, j++) { 4595e1f24a79SParav Pandit names[j] = cong_cnts[i].name; 4596e1f24a79SParav Pandit offsets[j] = cong_cnts[i].offset; 4597e1f24a79SParav Pandit } 4598e1f24a79SParav Pandit } 45990837e86aSMark Bloch } 46000837e86aSMark Bloch 4601e1f24a79SParav Pandit static int mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev) 46020837e86aSMark Bloch { 4603aac4492eSDaniel Jurgens int err = 0; 46040837e86aSMark Bloch int i; 46050837e86aSMark Bloch 46060837e86aSMark Bloch for (i = 0; i < dev->num_ports; i++) { 4607aac4492eSDaniel Jurgens err = __mlx5_ib_alloc_counters(dev, &dev->port[i].cnts); 4608aac4492eSDaniel Jurgens if (err) 4609aac4492eSDaniel Jurgens goto err_alloc; 46107c16f477SKamal Heib 4611aac4492eSDaniel Jurgens mlx5_ib_fill_counters(dev, dev->port[i].cnts.names, 4612aac4492eSDaniel Jurgens dev->port[i].cnts.offsets); 4613aac4492eSDaniel Jurgens 4614aac4492eSDaniel Jurgens err = mlx5_core_alloc_q_counter(dev->mdev, 4615aac4492eSDaniel Jurgens &dev->port[i].cnts.set_id); 4616aac4492eSDaniel Jurgens if (err) { 46170837e86aSMark Bloch mlx5_ib_warn(dev, 46180837e86aSMark Bloch "couldn't allocate queue counter for port %d, err %d\n", 4619aac4492eSDaniel Jurgens i + 1, err); 4620aac4492eSDaniel Jurgens goto err_alloc; 46210837e86aSMark Bloch } 4622aac4492eSDaniel Jurgens dev->port[i].cnts.set_id_valid = true; 46230837e86aSMark Bloch } 46240837e86aSMark Bloch 46250837e86aSMark Bloch return 0; 46260837e86aSMark Bloch 4627aac4492eSDaniel Jurgens err_alloc: 4628aac4492eSDaniel Jurgens mlx5_ib_dealloc_counters(dev); 4629aac4492eSDaniel Jurgens return err; 46300837e86aSMark Bloch } 46310837e86aSMark Bloch 46320ad17a8fSMark Bloch static struct rdma_hw_stats *mlx5_ib_alloc_hw_stats(struct ib_device *ibdev, 46330ad17a8fSMark Bloch u8 port_num) 46340ad17a8fSMark Bloch { 46357c16f477SKamal Heib struct mlx5_ib_dev *dev = to_mdev(ibdev); 46367c16f477SKamal Heib struct mlx5_ib_port *port = &dev->port[port_num - 1]; 46370ad17a8fSMark Bloch 46380ad17a8fSMark Bloch /* We support only per port stats */ 46390ad17a8fSMark Bloch if (port_num == 0) 46400ad17a8fSMark Bloch return NULL; 46410ad17a8fSMark Bloch 4642e1f24a79SParav Pandit return rdma_alloc_hw_stats_struct(port->cnts.names, 4643e1f24a79SParav Pandit port->cnts.num_q_counters + 4644e1f24a79SParav Pandit port->cnts.num_cong_counters, 46450ad17a8fSMark Bloch RDMA_HW_STATS_DEFAULT_LIFESPAN); 46460ad17a8fSMark Bloch } 46470ad17a8fSMark Bloch 4648aac4492eSDaniel Jurgens static int mlx5_ib_query_q_counters(struct mlx5_core_dev *mdev, 4649e1f24a79SParav Pandit struct mlx5_ib_port *port, 4650e1f24a79SParav Pandit struct rdma_hw_stats *stats) 4651e1f24a79SParav Pandit { 4652e1f24a79SParav Pandit int outlen = MLX5_ST_SZ_BYTES(query_q_counter_out); 4653e1f24a79SParav Pandit void *out; 4654e1f24a79SParav Pandit __be32 val; 4655e1f24a79SParav Pandit int ret, i; 4656e1f24a79SParav Pandit 46571b9a07eeSLeon Romanovsky out = kvzalloc(outlen, GFP_KERNEL); 4658e1f24a79SParav Pandit if (!out) 4659e1f24a79SParav Pandit return -ENOMEM; 4660e1f24a79SParav Pandit 4661aac4492eSDaniel Jurgens ret = mlx5_core_query_q_counter(mdev, 4662e1f24a79SParav Pandit port->cnts.set_id, 0, 4663e1f24a79SParav Pandit out, outlen); 4664e1f24a79SParav Pandit if (ret) 4665e1f24a79SParav Pandit goto free; 4666e1f24a79SParav Pandit 4667e1f24a79SParav Pandit for (i = 0; i < port->cnts.num_q_counters; i++) { 4668e1f24a79SParav Pandit val = *(__be32 *)(out + port->cnts.offsets[i]); 4669e1f24a79SParav Pandit stats->value[i] = (u64)be32_to_cpu(val); 4670e1f24a79SParav Pandit } 4671e1f24a79SParav Pandit 4672e1f24a79SParav Pandit free: 4673e1f24a79SParav Pandit kvfree(out); 4674e1f24a79SParav Pandit return ret; 4675e1f24a79SParav Pandit } 4676e1f24a79SParav Pandit 46770ad17a8fSMark Bloch static int mlx5_ib_get_hw_stats(struct ib_device *ibdev, 46780ad17a8fSMark Bloch struct rdma_hw_stats *stats, 46797c16f477SKamal Heib u8 port_num, int index) 46800ad17a8fSMark Bloch { 46810ad17a8fSMark Bloch struct mlx5_ib_dev *dev = to_mdev(ibdev); 46827c16f477SKamal Heib struct mlx5_ib_port *port = &dev->port[port_num - 1]; 4683aac4492eSDaniel Jurgens struct mlx5_core_dev *mdev; 4684e1f24a79SParav Pandit int ret, num_counters; 4685aac4492eSDaniel Jurgens u8 mdev_port_num; 46860ad17a8fSMark Bloch 46877c16f477SKamal Heib if (!stats) 4688e1f24a79SParav Pandit return -EINVAL; 46890ad17a8fSMark Bloch 4690aac4492eSDaniel Jurgens num_counters = port->cnts.num_q_counters + port->cnts.num_cong_counters; 4691aac4492eSDaniel Jurgens 4692aac4492eSDaniel Jurgens /* q_counters are per IB device, query the master mdev */ 4693aac4492eSDaniel Jurgens ret = mlx5_ib_query_q_counters(dev->mdev, port, stats); 46940ad17a8fSMark Bloch if (ret) 4695e1f24a79SParav Pandit return ret; 46960ad17a8fSMark Bloch 4697e1f24a79SParav Pandit if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) { 4698aac4492eSDaniel Jurgens mdev = mlx5_ib_get_native_port_mdev(dev, port_num, 4699aac4492eSDaniel Jurgens &mdev_port_num); 4700aac4492eSDaniel Jurgens if (!mdev) { 4701aac4492eSDaniel Jurgens /* If port is not affiliated yet, its in down state 4702aac4492eSDaniel Jurgens * which doesn't have any counters yet, so it would be 4703aac4492eSDaniel Jurgens * zero. So no need to read from the HCA. 4704aac4492eSDaniel Jurgens */ 4705aac4492eSDaniel Jurgens goto done; 4706aac4492eSDaniel Jurgens } 470771a0ff65SMajd Dibbiny ret = mlx5_lag_query_cong_counters(dev->mdev, 470871a0ff65SMajd Dibbiny stats->value + 470971a0ff65SMajd Dibbiny port->cnts.num_q_counters, 471071a0ff65SMajd Dibbiny port->cnts.num_cong_counters, 471171a0ff65SMajd Dibbiny port->cnts.offsets + 471271a0ff65SMajd Dibbiny port->cnts.num_q_counters); 4713aac4492eSDaniel Jurgens 4714aac4492eSDaniel Jurgens mlx5_ib_put_native_port_mdev(dev, port_num); 4715e1f24a79SParav Pandit if (ret) 4716e1f24a79SParav Pandit return ret; 47170ad17a8fSMark Bloch } 47187c16f477SKamal Heib 4719aac4492eSDaniel Jurgens done: 4720e1f24a79SParav Pandit return num_counters; 47210ad17a8fSMark Bloch } 47220ad17a8fSMark Bloch 47238e959601SNiranjana Vishwanathapura static void mlx5_ib_free_rdma_netdev(struct net_device *netdev) 47248e959601SNiranjana Vishwanathapura { 47258e959601SNiranjana Vishwanathapura return mlx5_rdma_netdev_free(netdev); 47268e959601SNiranjana Vishwanathapura } 47278e959601SNiranjana Vishwanathapura 4728693dfd5aSErez Shitrit static struct net_device* 4729693dfd5aSErez Shitrit mlx5_ib_alloc_rdma_netdev(struct ib_device *hca, 4730693dfd5aSErez Shitrit u8 port_num, 4731693dfd5aSErez Shitrit enum rdma_netdev_t type, 4732693dfd5aSErez Shitrit const char *name, 4733693dfd5aSErez Shitrit unsigned char name_assign_type, 4734693dfd5aSErez Shitrit void (*setup)(struct net_device *)) 4735693dfd5aSErez Shitrit { 47368e959601SNiranjana Vishwanathapura struct net_device *netdev; 47378e959601SNiranjana Vishwanathapura struct rdma_netdev *rn; 47388e959601SNiranjana Vishwanathapura 4739693dfd5aSErez Shitrit if (type != RDMA_NETDEV_IPOIB) 4740693dfd5aSErez Shitrit return ERR_PTR(-EOPNOTSUPP); 4741693dfd5aSErez Shitrit 47428e959601SNiranjana Vishwanathapura netdev = mlx5_rdma_netdev_alloc(to_mdev(hca)->mdev, hca, 4743693dfd5aSErez Shitrit name, setup); 47448e959601SNiranjana Vishwanathapura if (likely(!IS_ERR_OR_NULL(netdev))) { 47458e959601SNiranjana Vishwanathapura rn = netdev_priv(netdev); 47468e959601SNiranjana Vishwanathapura rn->free_rdma_netdev = mlx5_ib_free_rdma_netdev; 4747693dfd5aSErez Shitrit } 47488e959601SNiranjana Vishwanathapura return netdev; 4749693dfd5aSErez Shitrit } 4750693dfd5aSErez Shitrit 4751fe248c3aSMaor Gottlieb static void delay_drop_debugfs_cleanup(struct mlx5_ib_dev *dev) 4752fe248c3aSMaor Gottlieb { 4753fe248c3aSMaor Gottlieb if (!dev->delay_drop.dbg) 4754fe248c3aSMaor Gottlieb return; 4755fe248c3aSMaor Gottlieb debugfs_remove_recursive(dev->delay_drop.dbg->dir_debugfs); 4756fe248c3aSMaor Gottlieb kfree(dev->delay_drop.dbg); 4757fe248c3aSMaor Gottlieb dev->delay_drop.dbg = NULL; 4758fe248c3aSMaor Gottlieb } 4759fe248c3aSMaor Gottlieb 476003404e8aSMaor Gottlieb static void cancel_delay_drop(struct mlx5_ib_dev *dev) 476103404e8aSMaor Gottlieb { 476203404e8aSMaor Gottlieb if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP)) 476303404e8aSMaor Gottlieb return; 476403404e8aSMaor Gottlieb 476503404e8aSMaor Gottlieb cancel_work_sync(&dev->delay_drop.delay_drop_work); 4766fe248c3aSMaor Gottlieb delay_drop_debugfs_cleanup(dev); 4767fe248c3aSMaor Gottlieb } 4768fe248c3aSMaor Gottlieb 4769fe248c3aSMaor Gottlieb static ssize_t delay_drop_timeout_read(struct file *filp, char __user *buf, 4770fe248c3aSMaor Gottlieb size_t count, loff_t *pos) 4771fe248c3aSMaor Gottlieb { 4772fe248c3aSMaor Gottlieb struct mlx5_ib_delay_drop *delay_drop = filp->private_data; 4773fe248c3aSMaor Gottlieb char lbuf[20]; 4774fe248c3aSMaor Gottlieb int len; 4775fe248c3aSMaor Gottlieb 4776fe248c3aSMaor Gottlieb len = snprintf(lbuf, sizeof(lbuf), "%u\n", delay_drop->timeout); 4777fe248c3aSMaor Gottlieb return simple_read_from_buffer(buf, count, pos, lbuf, len); 4778fe248c3aSMaor Gottlieb } 4779fe248c3aSMaor Gottlieb 4780fe248c3aSMaor Gottlieb static ssize_t delay_drop_timeout_write(struct file *filp, const char __user *buf, 4781fe248c3aSMaor Gottlieb size_t count, loff_t *pos) 4782fe248c3aSMaor Gottlieb { 4783fe248c3aSMaor Gottlieb struct mlx5_ib_delay_drop *delay_drop = filp->private_data; 4784fe248c3aSMaor Gottlieb u32 timeout; 4785fe248c3aSMaor Gottlieb u32 var; 4786fe248c3aSMaor Gottlieb 4787fe248c3aSMaor Gottlieb if (kstrtouint_from_user(buf, count, 0, &var)) 4788fe248c3aSMaor Gottlieb return -EFAULT; 4789fe248c3aSMaor Gottlieb 4790fe248c3aSMaor Gottlieb timeout = min_t(u32, roundup(var, 100), MLX5_MAX_DELAY_DROP_TIMEOUT_MS * 4791fe248c3aSMaor Gottlieb 1000); 4792fe248c3aSMaor Gottlieb if (timeout != var) 4793fe248c3aSMaor Gottlieb mlx5_ib_dbg(delay_drop->dev, "Round delay drop timeout to %u usec\n", 4794fe248c3aSMaor Gottlieb timeout); 4795fe248c3aSMaor Gottlieb 4796fe248c3aSMaor Gottlieb delay_drop->timeout = timeout; 4797fe248c3aSMaor Gottlieb 4798fe248c3aSMaor Gottlieb return count; 4799fe248c3aSMaor Gottlieb } 4800fe248c3aSMaor Gottlieb 4801fe248c3aSMaor Gottlieb static const struct file_operations fops_delay_drop_timeout = { 4802fe248c3aSMaor Gottlieb .owner = THIS_MODULE, 4803fe248c3aSMaor Gottlieb .open = simple_open, 4804fe248c3aSMaor Gottlieb .write = delay_drop_timeout_write, 4805fe248c3aSMaor Gottlieb .read = delay_drop_timeout_read, 4806fe248c3aSMaor Gottlieb }; 4807fe248c3aSMaor Gottlieb 4808fe248c3aSMaor Gottlieb static int delay_drop_debugfs_init(struct mlx5_ib_dev *dev) 4809fe248c3aSMaor Gottlieb { 4810fe248c3aSMaor Gottlieb struct mlx5_ib_dbg_delay_drop *dbg; 4811fe248c3aSMaor Gottlieb 4812fe248c3aSMaor Gottlieb if (!mlx5_debugfs_root) 4813fe248c3aSMaor Gottlieb return 0; 4814fe248c3aSMaor Gottlieb 4815fe248c3aSMaor Gottlieb dbg = kzalloc(sizeof(*dbg), GFP_KERNEL); 4816fe248c3aSMaor Gottlieb if (!dbg) 4817fe248c3aSMaor Gottlieb return -ENOMEM; 4818fe248c3aSMaor Gottlieb 4819cbafad87SSudip Mukherjee dev->delay_drop.dbg = dbg; 4820cbafad87SSudip Mukherjee 4821fe248c3aSMaor Gottlieb dbg->dir_debugfs = 4822fe248c3aSMaor Gottlieb debugfs_create_dir("delay_drop", 4823fe248c3aSMaor Gottlieb dev->mdev->priv.dbg_root); 4824fe248c3aSMaor Gottlieb if (!dbg->dir_debugfs) 4825cbafad87SSudip Mukherjee goto out_debugfs; 4826fe248c3aSMaor Gottlieb 4827fe248c3aSMaor Gottlieb dbg->events_cnt_debugfs = 4828fe248c3aSMaor Gottlieb debugfs_create_atomic_t("num_timeout_events", 0400, 4829fe248c3aSMaor Gottlieb dbg->dir_debugfs, 4830fe248c3aSMaor Gottlieb &dev->delay_drop.events_cnt); 4831fe248c3aSMaor Gottlieb if (!dbg->events_cnt_debugfs) 4832fe248c3aSMaor Gottlieb goto out_debugfs; 4833fe248c3aSMaor Gottlieb 4834fe248c3aSMaor Gottlieb dbg->rqs_cnt_debugfs = 4835fe248c3aSMaor Gottlieb debugfs_create_atomic_t("num_rqs", 0400, 4836fe248c3aSMaor Gottlieb dbg->dir_debugfs, 4837fe248c3aSMaor Gottlieb &dev->delay_drop.rqs_cnt); 4838fe248c3aSMaor Gottlieb if (!dbg->rqs_cnt_debugfs) 4839fe248c3aSMaor Gottlieb goto out_debugfs; 4840fe248c3aSMaor Gottlieb 4841fe248c3aSMaor Gottlieb dbg->timeout_debugfs = 4842fe248c3aSMaor Gottlieb debugfs_create_file("timeout", 0600, 4843fe248c3aSMaor Gottlieb dbg->dir_debugfs, 4844fe248c3aSMaor Gottlieb &dev->delay_drop, 4845fe248c3aSMaor Gottlieb &fops_delay_drop_timeout); 4846fe248c3aSMaor Gottlieb if (!dbg->timeout_debugfs) 4847fe248c3aSMaor Gottlieb goto out_debugfs; 4848fe248c3aSMaor Gottlieb 4849fe248c3aSMaor Gottlieb return 0; 4850fe248c3aSMaor Gottlieb 4851fe248c3aSMaor Gottlieb out_debugfs: 4852fe248c3aSMaor Gottlieb delay_drop_debugfs_cleanup(dev); 4853fe248c3aSMaor Gottlieb return -ENOMEM; 485403404e8aSMaor Gottlieb } 485503404e8aSMaor Gottlieb 485603404e8aSMaor Gottlieb static void init_delay_drop(struct mlx5_ib_dev *dev) 485703404e8aSMaor Gottlieb { 485803404e8aSMaor Gottlieb if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP)) 485903404e8aSMaor Gottlieb return; 486003404e8aSMaor Gottlieb 486103404e8aSMaor Gottlieb mutex_init(&dev->delay_drop.lock); 486203404e8aSMaor Gottlieb dev->delay_drop.dev = dev; 486303404e8aSMaor Gottlieb dev->delay_drop.activate = false; 486403404e8aSMaor Gottlieb dev->delay_drop.timeout = MLX5_MAX_DELAY_DROP_TIMEOUT_MS * 1000; 486503404e8aSMaor Gottlieb INIT_WORK(&dev->delay_drop.delay_drop_work, delay_drop_handler); 4866fe248c3aSMaor Gottlieb atomic_set(&dev->delay_drop.rqs_cnt, 0); 4867fe248c3aSMaor Gottlieb atomic_set(&dev->delay_drop.events_cnt, 0); 4868fe248c3aSMaor Gottlieb 4869fe248c3aSMaor Gottlieb if (delay_drop_debugfs_init(dev)) 4870fe248c3aSMaor Gottlieb mlx5_ib_warn(dev, "Failed to init delay drop debugfs\n"); 487103404e8aSMaor Gottlieb } 487203404e8aSMaor Gottlieb 487384305d71SLeon Romanovsky static const struct cpumask * 487484305d71SLeon Romanovsky mlx5_ib_get_vector_affinity(struct ib_device *ibdev, int comp_vector) 487540b24403SSagi Grimberg { 487640b24403SSagi Grimberg struct mlx5_ib_dev *dev = to_mdev(ibdev); 487740b24403SSagi Grimberg 487840b24403SSagi Grimberg return mlx5_get_vector_affinity(dev->mdev, comp_vector); 487940b24403SSagi Grimberg } 488040b24403SSagi Grimberg 488132f69e4bSDaniel Jurgens /* The mlx5_ib_multiport_mutex should be held when calling this function */ 488232f69e4bSDaniel Jurgens static void mlx5_ib_unbind_slave_port(struct mlx5_ib_dev *ibdev, 488332f69e4bSDaniel Jurgens struct mlx5_ib_multiport_info *mpi) 488432f69e4bSDaniel Jurgens { 488532f69e4bSDaniel Jurgens u8 port_num = mlx5_core_native_port_num(mpi->mdev) - 1; 488632f69e4bSDaniel Jurgens struct mlx5_ib_port *port = &ibdev->port[port_num]; 488732f69e4bSDaniel Jurgens int comps; 488832f69e4bSDaniel Jurgens int err; 488932f69e4bSDaniel Jurgens int i; 489032f69e4bSDaniel Jurgens 4891a9e546e7SParav Pandit mlx5_ib_cleanup_cong_debugfs(ibdev, port_num); 4892a9e546e7SParav Pandit 489332f69e4bSDaniel Jurgens spin_lock(&port->mp.mpi_lock); 489432f69e4bSDaniel Jurgens if (!mpi->ibdev) { 489532f69e4bSDaniel Jurgens spin_unlock(&port->mp.mpi_lock); 489632f69e4bSDaniel Jurgens return; 489732f69e4bSDaniel Jurgens } 489832f69e4bSDaniel Jurgens mpi->ibdev = NULL; 489932f69e4bSDaniel Jurgens 490032f69e4bSDaniel Jurgens spin_unlock(&port->mp.mpi_lock); 490132f69e4bSDaniel Jurgens mlx5_remove_netdev_notifier(ibdev, port_num); 490232f69e4bSDaniel Jurgens spin_lock(&port->mp.mpi_lock); 490332f69e4bSDaniel Jurgens 490432f69e4bSDaniel Jurgens comps = mpi->mdev_refcnt; 490532f69e4bSDaniel Jurgens if (comps) { 490632f69e4bSDaniel Jurgens mpi->unaffiliate = true; 490732f69e4bSDaniel Jurgens init_completion(&mpi->unref_comp); 490832f69e4bSDaniel Jurgens spin_unlock(&port->mp.mpi_lock); 490932f69e4bSDaniel Jurgens 491032f69e4bSDaniel Jurgens for (i = 0; i < comps; i++) 491132f69e4bSDaniel Jurgens wait_for_completion(&mpi->unref_comp); 491232f69e4bSDaniel Jurgens 491332f69e4bSDaniel Jurgens spin_lock(&port->mp.mpi_lock); 491432f69e4bSDaniel Jurgens mpi->unaffiliate = false; 491532f69e4bSDaniel Jurgens } 491632f69e4bSDaniel Jurgens 491732f69e4bSDaniel Jurgens port->mp.mpi = NULL; 491832f69e4bSDaniel Jurgens 491932f69e4bSDaniel Jurgens list_add_tail(&mpi->list, &mlx5_ib_unaffiliated_port_list); 492032f69e4bSDaniel Jurgens 492132f69e4bSDaniel Jurgens spin_unlock(&port->mp.mpi_lock); 492232f69e4bSDaniel Jurgens 492332f69e4bSDaniel Jurgens err = mlx5_nic_vport_unaffiliate_multiport(mpi->mdev); 492432f69e4bSDaniel Jurgens 492532f69e4bSDaniel Jurgens mlx5_ib_dbg(ibdev, "unaffiliated port %d\n", port_num + 1); 492632f69e4bSDaniel Jurgens /* Log an error, still needed to cleanup the pointers and add 492732f69e4bSDaniel Jurgens * it back to the list. 492832f69e4bSDaniel Jurgens */ 492932f69e4bSDaniel Jurgens if (err) 493032f69e4bSDaniel Jurgens mlx5_ib_err(ibdev, "Failed to unaffiliate port %u\n", 493132f69e4bSDaniel Jurgens port_num + 1); 493232f69e4bSDaniel Jurgens 493332f69e4bSDaniel Jurgens ibdev->roce[port_num].last_port_state = IB_PORT_DOWN; 493432f69e4bSDaniel Jurgens } 493532f69e4bSDaniel Jurgens 493632f69e4bSDaniel Jurgens /* The mlx5_ib_multiport_mutex should be held when calling this function */ 493732f69e4bSDaniel Jurgens static bool mlx5_ib_bind_slave_port(struct mlx5_ib_dev *ibdev, 493832f69e4bSDaniel Jurgens struct mlx5_ib_multiport_info *mpi) 493932f69e4bSDaniel Jurgens { 494032f69e4bSDaniel Jurgens u8 port_num = mlx5_core_native_port_num(mpi->mdev) - 1; 494132f69e4bSDaniel Jurgens int err; 494232f69e4bSDaniel Jurgens 494332f69e4bSDaniel Jurgens spin_lock(&ibdev->port[port_num].mp.mpi_lock); 494432f69e4bSDaniel Jurgens if (ibdev->port[port_num].mp.mpi) { 494532f69e4bSDaniel Jurgens mlx5_ib_warn(ibdev, "port %d already affiliated.\n", 494632f69e4bSDaniel Jurgens port_num + 1); 494732f69e4bSDaniel Jurgens spin_unlock(&ibdev->port[port_num].mp.mpi_lock); 494832f69e4bSDaniel Jurgens return false; 494932f69e4bSDaniel Jurgens } 495032f69e4bSDaniel Jurgens 495132f69e4bSDaniel Jurgens ibdev->port[port_num].mp.mpi = mpi; 495232f69e4bSDaniel Jurgens mpi->ibdev = ibdev; 495332f69e4bSDaniel Jurgens spin_unlock(&ibdev->port[port_num].mp.mpi_lock); 495432f69e4bSDaniel Jurgens 495532f69e4bSDaniel Jurgens err = mlx5_nic_vport_affiliate_multiport(ibdev->mdev, mpi->mdev); 495632f69e4bSDaniel Jurgens if (err) 495732f69e4bSDaniel Jurgens goto unbind; 495832f69e4bSDaniel Jurgens 495932f69e4bSDaniel Jurgens err = get_port_caps(ibdev, mlx5_core_native_port_num(mpi->mdev)); 496032f69e4bSDaniel Jurgens if (err) 496132f69e4bSDaniel Jurgens goto unbind; 496232f69e4bSDaniel Jurgens 496332f69e4bSDaniel Jurgens err = mlx5_add_netdev_notifier(ibdev, port_num); 496432f69e4bSDaniel Jurgens if (err) { 496532f69e4bSDaniel Jurgens mlx5_ib_err(ibdev, "failed adding netdev notifier for port %u\n", 496632f69e4bSDaniel Jurgens port_num + 1); 496732f69e4bSDaniel Jurgens goto unbind; 496832f69e4bSDaniel Jurgens } 496932f69e4bSDaniel Jurgens 4970a9e546e7SParav Pandit err = mlx5_ib_init_cong_debugfs(ibdev, port_num); 4971a9e546e7SParav Pandit if (err) 4972a9e546e7SParav Pandit goto unbind; 4973a9e546e7SParav Pandit 497432f69e4bSDaniel Jurgens return true; 497532f69e4bSDaniel Jurgens 497632f69e4bSDaniel Jurgens unbind: 497732f69e4bSDaniel Jurgens mlx5_ib_unbind_slave_port(ibdev, mpi); 497832f69e4bSDaniel Jurgens return false; 497932f69e4bSDaniel Jurgens } 498032f69e4bSDaniel Jurgens 498132f69e4bSDaniel Jurgens static int mlx5_ib_init_multiport_master(struct mlx5_ib_dev *dev) 498232f69e4bSDaniel Jurgens { 498332f69e4bSDaniel Jurgens int port_num = mlx5_core_native_port_num(dev->mdev) - 1; 498432f69e4bSDaniel Jurgens enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev, 498532f69e4bSDaniel Jurgens port_num + 1); 498632f69e4bSDaniel Jurgens struct mlx5_ib_multiport_info *mpi; 498732f69e4bSDaniel Jurgens int err; 498832f69e4bSDaniel Jurgens int i; 498932f69e4bSDaniel Jurgens 499032f69e4bSDaniel Jurgens if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET) 499132f69e4bSDaniel Jurgens return 0; 499232f69e4bSDaniel Jurgens 499332f69e4bSDaniel Jurgens err = mlx5_query_nic_vport_system_image_guid(dev->mdev, 499432f69e4bSDaniel Jurgens &dev->sys_image_guid); 499532f69e4bSDaniel Jurgens if (err) 499632f69e4bSDaniel Jurgens return err; 499732f69e4bSDaniel Jurgens 499832f69e4bSDaniel Jurgens err = mlx5_nic_vport_enable_roce(dev->mdev); 499932f69e4bSDaniel Jurgens if (err) 500032f69e4bSDaniel Jurgens return err; 500132f69e4bSDaniel Jurgens 500232f69e4bSDaniel Jurgens mutex_lock(&mlx5_ib_multiport_mutex); 500332f69e4bSDaniel Jurgens for (i = 0; i < dev->num_ports; i++) { 500432f69e4bSDaniel Jurgens bool bound = false; 500532f69e4bSDaniel Jurgens 500632f69e4bSDaniel Jurgens /* build a stub multiport info struct for the native port. */ 500732f69e4bSDaniel Jurgens if (i == port_num) { 500832f69e4bSDaniel Jurgens mpi = kzalloc(sizeof(*mpi), GFP_KERNEL); 500932f69e4bSDaniel Jurgens if (!mpi) { 501032f69e4bSDaniel Jurgens mutex_unlock(&mlx5_ib_multiport_mutex); 501132f69e4bSDaniel Jurgens mlx5_nic_vport_disable_roce(dev->mdev); 501232f69e4bSDaniel Jurgens return -ENOMEM; 501332f69e4bSDaniel Jurgens } 501432f69e4bSDaniel Jurgens 501532f69e4bSDaniel Jurgens mpi->is_master = true; 501632f69e4bSDaniel Jurgens mpi->mdev = dev->mdev; 501732f69e4bSDaniel Jurgens mpi->sys_image_guid = dev->sys_image_guid; 501832f69e4bSDaniel Jurgens dev->port[i].mp.mpi = mpi; 501932f69e4bSDaniel Jurgens mpi->ibdev = dev; 502032f69e4bSDaniel Jurgens mpi = NULL; 502132f69e4bSDaniel Jurgens continue; 502232f69e4bSDaniel Jurgens } 502332f69e4bSDaniel Jurgens 502432f69e4bSDaniel Jurgens list_for_each_entry(mpi, &mlx5_ib_unaffiliated_port_list, 502532f69e4bSDaniel Jurgens list) { 502632f69e4bSDaniel Jurgens if (dev->sys_image_guid == mpi->sys_image_guid && 502732f69e4bSDaniel Jurgens (mlx5_core_native_port_num(mpi->mdev) - 1) == i) { 502832f69e4bSDaniel Jurgens bound = mlx5_ib_bind_slave_port(dev, mpi); 502932f69e4bSDaniel Jurgens } 503032f69e4bSDaniel Jurgens 503132f69e4bSDaniel Jurgens if (bound) { 503232f69e4bSDaniel Jurgens dev_dbg(&mpi->mdev->pdev->dev, "removing port from unaffiliated list.\n"); 503332f69e4bSDaniel Jurgens mlx5_ib_dbg(dev, "port %d bound\n", i + 1); 503432f69e4bSDaniel Jurgens list_del(&mpi->list); 503532f69e4bSDaniel Jurgens break; 503632f69e4bSDaniel Jurgens } 503732f69e4bSDaniel Jurgens } 503832f69e4bSDaniel Jurgens if (!bound) { 503932f69e4bSDaniel Jurgens get_port_caps(dev, i + 1); 504032f69e4bSDaniel Jurgens mlx5_ib_dbg(dev, "no free port found for port %d\n", 504132f69e4bSDaniel Jurgens i + 1); 504232f69e4bSDaniel Jurgens } 504332f69e4bSDaniel Jurgens } 504432f69e4bSDaniel Jurgens 504532f69e4bSDaniel Jurgens list_add_tail(&dev->ib_dev_list, &mlx5_ib_dev_list); 504632f69e4bSDaniel Jurgens mutex_unlock(&mlx5_ib_multiport_mutex); 504732f69e4bSDaniel Jurgens return err; 504832f69e4bSDaniel Jurgens } 504932f69e4bSDaniel Jurgens 505032f69e4bSDaniel Jurgens static void mlx5_ib_cleanup_multiport_master(struct mlx5_ib_dev *dev) 505132f69e4bSDaniel Jurgens { 505232f69e4bSDaniel Jurgens int port_num = mlx5_core_native_port_num(dev->mdev) - 1; 505332f69e4bSDaniel Jurgens enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev, 505432f69e4bSDaniel Jurgens port_num + 1); 505532f69e4bSDaniel Jurgens int i; 505632f69e4bSDaniel Jurgens 505732f69e4bSDaniel Jurgens if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET) 505832f69e4bSDaniel Jurgens return; 505932f69e4bSDaniel Jurgens 506032f69e4bSDaniel Jurgens mutex_lock(&mlx5_ib_multiport_mutex); 506132f69e4bSDaniel Jurgens for (i = 0; i < dev->num_ports; i++) { 506232f69e4bSDaniel Jurgens if (dev->port[i].mp.mpi) { 506332f69e4bSDaniel Jurgens /* Destroy the native port stub */ 506432f69e4bSDaniel Jurgens if (i == port_num) { 506532f69e4bSDaniel Jurgens kfree(dev->port[i].mp.mpi); 506632f69e4bSDaniel Jurgens dev->port[i].mp.mpi = NULL; 506732f69e4bSDaniel Jurgens } else { 506832f69e4bSDaniel Jurgens mlx5_ib_dbg(dev, "unbinding port_num: %d\n", i + 1); 506932f69e4bSDaniel Jurgens mlx5_ib_unbind_slave_port(dev, dev->port[i].mp.mpi); 507032f69e4bSDaniel Jurgens } 507132f69e4bSDaniel Jurgens } 507232f69e4bSDaniel Jurgens } 507332f69e4bSDaniel Jurgens 507432f69e4bSDaniel Jurgens mlx5_ib_dbg(dev, "removing from devlist\n"); 507532f69e4bSDaniel Jurgens list_del(&dev->ib_dev_list); 507632f69e4bSDaniel Jurgens mutex_unlock(&mlx5_ib_multiport_mutex); 507732f69e4bSDaniel Jurgens 507832f69e4bSDaniel Jurgens mlx5_nic_vport_disable_roce(dev->mdev); 507932f69e4bSDaniel Jurgens } 508032f69e4bSDaniel Jurgens 508124da0016SAriel Levkovich ADD_UVERBS_ATTRIBUTES_SIMPLE(mlx5_ib_dm, UVERBS_OBJECT_DM, 508224da0016SAriel Levkovich UVERBS_METHOD_DM_ALLOC, 508324da0016SAriel Levkovich &UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_ALLOC_DM_RESP_START_OFFSET, 508424da0016SAriel Levkovich UVERBS_ATTR_TYPE(u64), 508524da0016SAriel Levkovich UA_FLAGS(UVERBS_ATTR_SPEC_F_MANDATORY)), 508624da0016SAriel Levkovich &UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_ALLOC_DM_RESP_PAGE_INDEX, 508724da0016SAriel Levkovich UVERBS_ATTR_TYPE(u16), 508824da0016SAriel Levkovich UA_FLAGS(UVERBS_ATTR_SPEC_F_MANDATORY))); 508924da0016SAriel Levkovich 5090c6475a0bSAviad Yehezkel ADD_UVERBS_ATTRIBUTES_SIMPLE(mlx5_ib_flow_action, UVERBS_OBJECT_FLOW_ACTION, 5091c6475a0bSAviad Yehezkel UVERBS_METHOD_FLOW_ACTION_ESP_CREATE, 5092c6475a0bSAviad Yehezkel &UVERBS_ATTR_PTR_IN(MLX5_IB_ATTR_CREATE_FLOW_ACTION_FLAGS, 5093c6475a0bSAviad Yehezkel UVERBS_ATTR_TYPE(u64), 5094c6475a0bSAviad Yehezkel UA_FLAGS(UVERBS_ATTR_SPEC_F_MANDATORY))); 5095c6475a0bSAviad Yehezkel 509624da0016SAriel Levkovich #define NUM_TREES 2 50978c84660bSMatan Barak static int populate_specs_root(struct mlx5_ib_dev *dev) 50988c84660bSMatan Barak { 50998c84660bSMatan Barak const struct uverbs_object_tree_def *default_root[NUM_TREES + 1] = { 51008c84660bSMatan Barak uverbs_default_get_objects()}; 51018c84660bSMatan Barak size_t num_trees = 1; 51028c84660bSMatan Barak 5103c6475a0bSAviad Yehezkel if (mlx5_accel_ipsec_device_caps(dev->mdev) & MLX5_ACCEL_IPSEC_CAP_DEVICE && 5104c6475a0bSAviad Yehezkel !WARN_ON(num_trees >= ARRAY_SIZE(default_root))) 5105c6475a0bSAviad Yehezkel default_root[num_trees++] = &mlx5_ib_flow_action; 5106c6475a0bSAviad Yehezkel 510724da0016SAriel Levkovich if (MLX5_CAP_DEV_MEM(dev->mdev, memic) && 510824da0016SAriel Levkovich !WARN_ON(num_trees >= ARRAY_SIZE(default_root))) 510924da0016SAriel Levkovich default_root[num_trees++] = &mlx5_ib_dm; 511024da0016SAriel Levkovich 51118c84660bSMatan Barak dev->ib_dev.specs_root = 51128c84660bSMatan Barak uverbs_alloc_spec_tree(num_trees, default_root); 51138c84660bSMatan Barak 51148c84660bSMatan Barak return PTR_ERR_OR_ZERO(dev->ib_dev.specs_root); 51158c84660bSMatan Barak } 51168c84660bSMatan Barak 51178c84660bSMatan Barak static void depopulate_specs_root(struct mlx5_ib_dev *dev) 51188c84660bSMatan Barak { 51198c84660bSMatan Barak uverbs_free_spec_tree(dev->ib_dev.specs_root); 51208c84660bSMatan Barak } 51218c84660bSMatan Barak 5122b5ca15adSMark Bloch void mlx5_ib_stage_init_cleanup(struct mlx5_ib_dev *dev) 5123e126ba97SEli Cohen { 512432f69e4bSDaniel Jurgens mlx5_ib_cleanup_multiport_master(dev); 51253cc297dbSMark Bloch #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING 51263cc297dbSMark Bloch cleanup_srcu_struct(&dev->mr_srcu); 51273cc297dbSMark Bloch #endif 512816c1975fSMark Bloch kfree(dev->port); 512916c1975fSMark Bloch } 513016c1975fSMark Bloch 5131b5ca15adSMark Bloch int mlx5_ib_stage_init_init(struct mlx5_ib_dev *dev) 513216c1975fSMark Bloch { 513316c1975fSMark Bloch struct mlx5_core_dev *mdev = dev->mdev; 51344babcf97SAviv Heller const char *name; 5135e126ba97SEli Cohen int err; 513632f69e4bSDaniel Jurgens int i; 5137e126ba97SEli Cohen 5138508562d6SDaniel Jurgens dev->port = kcalloc(dev->num_ports, sizeof(*dev->port), 51390837e86aSMark Bloch GFP_KERNEL); 51400837e86aSMark Bloch if (!dev->port) 514116c1975fSMark Bloch return -ENOMEM; 51420837e86aSMark Bloch 514332f69e4bSDaniel Jurgens for (i = 0; i < dev->num_ports; i++) { 514432f69e4bSDaniel Jurgens spin_lock_init(&dev->port[i].mp.mpi_lock); 514532f69e4bSDaniel Jurgens rwlock_init(&dev->roce[i].netdev_lock); 514632f69e4bSDaniel Jurgens } 514732f69e4bSDaniel Jurgens 514832f69e4bSDaniel Jurgens err = mlx5_ib_init_multiport_master(dev); 5149e126ba97SEli Cohen if (err) 51500837e86aSMark Bloch goto err_free_port; 5151e126ba97SEli Cohen 515232f69e4bSDaniel Jurgens if (!mlx5_core_mp_enabled(mdev)) { 515332f69e4bSDaniel Jurgens for (i = 1; i <= dev->num_ports; i++) { 515432f69e4bSDaniel Jurgens err = get_port_caps(dev, i); 515532f69e4bSDaniel Jurgens if (err) 515632f69e4bSDaniel Jurgens break; 515732f69e4bSDaniel Jurgens } 515832f69e4bSDaniel Jurgens } else { 515932f69e4bSDaniel Jurgens err = get_port_caps(dev, mlx5_core_native_port_num(mdev)); 516032f69e4bSDaniel Jurgens } 516132f69e4bSDaniel Jurgens if (err) 516232f69e4bSDaniel Jurgens goto err_mp; 516332f69e4bSDaniel Jurgens 51641b5daf11SMajd Dibbiny if (mlx5_use_mad_ifc(dev)) 5165e126ba97SEli Cohen get_ext_port_caps(dev); 5166e126ba97SEli Cohen 51674babcf97SAviv Heller if (!mlx5_lag_is_active(mdev)) 51684babcf97SAviv Heller name = "mlx5_%d"; 51694babcf97SAviv Heller else 51704babcf97SAviv Heller name = "mlx5_bond_%d"; 51714babcf97SAviv Heller 51724babcf97SAviv Heller strlcpy(dev->ib_dev.name, name, IB_DEVICE_NAME_MAX); 5173e126ba97SEli Cohen dev->ib_dev.owner = THIS_MODULE; 5174e126ba97SEli Cohen dev->ib_dev.node_type = RDMA_NODE_IB_CA; 5175c6790aa9SSagi Grimberg dev->ib_dev.local_dma_lkey = 0 /* not supported for now */; 5176e126ba97SEli Cohen dev->ib_dev.phys_port_cnt = dev->num_ports; 5177233d05d2SSaeed Mahameed dev->ib_dev.num_comp_vectors = 5178233d05d2SSaeed Mahameed dev->mdev->priv.eq_table.num_comp_vectors; 51799b0c289eSBart Van Assche dev->ib_dev.dev.parent = &mdev->pdev->dev; 5180e126ba97SEli Cohen 51813cc297dbSMark Bloch mutex_init(&dev->cap_mask_mutex); 51823cc297dbSMark Bloch INIT_LIST_HEAD(&dev->qp_list); 51833cc297dbSMark Bloch spin_lock_init(&dev->reset_flow_resource_lock); 51843cc297dbSMark Bloch 518524da0016SAriel Levkovich spin_lock_init(&dev->memic.memic_lock); 518624da0016SAriel Levkovich dev->memic.dev = mdev; 518724da0016SAriel Levkovich 51883cc297dbSMark Bloch #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING 51893cc297dbSMark Bloch err = init_srcu_struct(&dev->mr_srcu); 51903cc297dbSMark Bloch if (err) 51913cc297dbSMark Bloch goto err_free_port; 51923cc297dbSMark Bloch #endif 51933cc297dbSMark Bloch 519416c1975fSMark Bloch return 0; 519532f69e4bSDaniel Jurgens err_mp: 519632f69e4bSDaniel Jurgens mlx5_ib_cleanup_multiport_master(dev); 519716c1975fSMark Bloch 519816c1975fSMark Bloch err_free_port: 519916c1975fSMark Bloch kfree(dev->port); 520016c1975fSMark Bloch 520116c1975fSMark Bloch return -ENOMEM; 520216c1975fSMark Bloch } 520316c1975fSMark Bloch 52049a4ca38dSMark Bloch static int mlx5_ib_stage_flow_db_init(struct mlx5_ib_dev *dev) 52059a4ca38dSMark Bloch { 52069a4ca38dSMark Bloch dev->flow_db = kzalloc(sizeof(*dev->flow_db), GFP_KERNEL); 52079a4ca38dSMark Bloch 52089a4ca38dSMark Bloch if (!dev->flow_db) 52099a4ca38dSMark Bloch return -ENOMEM; 52109a4ca38dSMark Bloch 52119a4ca38dSMark Bloch mutex_init(&dev->flow_db->lock); 52129a4ca38dSMark Bloch 52139a4ca38dSMark Bloch return 0; 52149a4ca38dSMark Bloch } 52159a4ca38dSMark Bloch 5216b5ca15adSMark Bloch int mlx5_ib_stage_rep_flow_db_init(struct mlx5_ib_dev *dev) 5217b5ca15adSMark Bloch { 5218b5ca15adSMark Bloch struct mlx5_ib_dev *nic_dev; 5219b5ca15adSMark Bloch 5220b5ca15adSMark Bloch nic_dev = mlx5_ib_get_uplink_ibdev(dev->mdev->priv.eswitch); 5221b5ca15adSMark Bloch 5222b5ca15adSMark Bloch if (!nic_dev) 5223b5ca15adSMark Bloch return -EINVAL; 5224b5ca15adSMark Bloch 5225b5ca15adSMark Bloch dev->flow_db = nic_dev->flow_db; 5226b5ca15adSMark Bloch 5227b5ca15adSMark Bloch return 0; 5228b5ca15adSMark Bloch } 5229b5ca15adSMark Bloch 52309a4ca38dSMark Bloch static void mlx5_ib_stage_flow_db_cleanup(struct mlx5_ib_dev *dev) 52319a4ca38dSMark Bloch { 52329a4ca38dSMark Bloch kfree(dev->flow_db); 52339a4ca38dSMark Bloch } 52349a4ca38dSMark Bloch 5235b5ca15adSMark Bloch int mlx5_ib_stage_caps_init(struct mlx5_ib_dev *dev) 523616c1975fSMark Bloch { 523716c1975fSMark Bloch struct mlx5_core_dev *mdev = dev->mdev; 523816c1975fSMark Bloch int err; 523916c1975fSMark Bloch 5240e126ba97SEli Cohen dev->ib_dev.uverbs_abi_ver = MLX5_IB_UVERBS_ABI_VERSION; 5241e126ba97SEli Cohen dev->ib_dev.uverbs_cmd_mask = 5242e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) | 5243e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) | 5244e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_QUERY_PORT) | 5245e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_ALLOC_PD) | 5246e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) | 524741c450fdSMoni Shoua (1ull << IB_USER_VERBS_CMD_CREATE_AH) | 524841c450fdSMoni Shoua (1ull << IB_USER_VERBS_CMD_DESTROY_AH) | 5249e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_REG_MR) | 525056e11d62SNoa Osherovich (1ull << IB_USER_VERBS_CMD_REREG_MR) | 5251e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_DEREG_MR) | 5252e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) | 5253e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_CREATE_CQ) | 5254e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_RESIZE_CQ) | 5255e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) | 5256e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_CREATE_QP) | 5257e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_MODIFY_QP) | 5258e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_QUERY_QP) | 5259e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_DESTROY_QP) | 5260e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST) | 5261e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_DETACH_MCAST) | 5262e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_CREATE_SRQ) | 5263e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ) | 5264e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_QUERY_SRQ) | 5265e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ) | 5266e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_CREATE_XSRQ) | 5267e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_OPEN_QP); 52681707cb4aSHaggai Eran dev->ib_dev.uverbs_ex_cmd_mask = 5269d4584ddfSMatan Barak (1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE) | 5270d4584ddfSMatan Barak (1ull << IB_USER_VERBS_EX_CMD_CREATE_CQ) | 52717d29f349SBodong Wang (1ull << IB_USER_VERBS_EX_CMD_CREATE_QP) | 5272b0e9df6dSYonatan Cohen (1ull << IB_USER_VERBS_EX_CMD_MODIFY_QP) | 5273b0e9df6dSYonatan Cohen (1ull << IB_USER_VERBS_EX_CMD_MODIFY_CQ); 5274e126ba97SEli Cohen 5275e126ba97SEli Cohen dev->ib_dev.query_device = mlx5_ib_query_device; 5276ebd61f68SAchiad Shochat dev->ib_dev.get_link_layer = mlx5_ib_port_link_layer; 5277e126ba97SEli Cohen dev->ib_dev.query_gid = mlx5_ib_query_gid; 52783cca2606SAchiad Shochat dev->ib_dev.add_gid = mlx5_ib_add_gid; 52793cca2606SAchiad Shochat dev->ib_dev.del_gid = mlx5_ib_del_gid; 5280e126ba97SEli Cohen dev->ib_dev.query_pkey = mlx5_ib_query_pkey; 5281e126ba97SEli Cohen dev->ib_dev.modify_device = mlx5_ib_modify_device; 5282e126ba97SEli Cohen dev->ib_dev.modify_port = mlx5_ib_modify_port; 5283e126ba97SEli Cohen dev->ib_dev.alloc_ucontext = mlx5_ib_alloc_ucontext; 5284e126ba97SEli Cohen dev->ib_dev.dealloc_ucontext = mlx5_ib_dealloc_ucontext; 5285e126ba97SEli Cohen dev->ib_dev.mmap = mlx5_ib_mmap; 5286e126ba97SEli Cohen dev->ib_dev.alloc_pd = mlx5_ib_alloc_pd; 5287e126ba97SEli Cohen dev->ib_dev.dealloc_pd = mlx5_ib_dealloc_pd; 5288e126ba97SEli Cohen dev->ib_dev.create_ah = mlx5_ib_create_ah; 5289e126ba97SEli Cohen dev->ib_dev.query_ah = mlx5_ib_query_ah; 5290e126ba97SEli Cohen dev->ib_dev.destroy_ah = mlx5_ib_destroy_ah; 5291e126ba97SEli Cohen dev->ib_dev.create_srq = mlx5_ib_create_srq; 5292e126ba97SEli Cohen dev->ib_dev.modify_srq = mlx5_ib_modify_srq; 5293e126ba97SEli Cohen dev->ib_dev.query_srq = mlx5_ib_query_srq; 5294e126ba97SEli Cohen dev->ib_dev.destroy_srq = mlx5_ib_destroy_srq; 5295e126ba97SEli Cohen dev->ib_dev.post_srq_recv = mlx5_ib_post_srq_recv; 5296e126ba97SEli Cohen dev->ib_dev.create_qp = mlx5_ib_create_qp; 5297e126ba97SEli Cohen dev->ib_dev.modify_qp = mlx5_ib_modify_qp; 5298e126ba97SEli Cohen dev->ib_dev.query_qp = mlx5_ib_query_qp; 5299e126ba97SEli Cohen dev->ib_dev.destroy_qp = mlx5_ib_destroy_qp; 5300e126ba97SEli Cohen dev->ib_dev.post_send = mlx5_ib_post_send; 5301e126ba97SEli Cohen dev->ib_dev.post_recv = mlx5_ib_post_recv; 5302e126ba97SEli Cohen dev->ib_dev.create_cq = mlx5_ib_create_cq; 5303e126ba97SEli Cohen dev->ib_dev.modify_cq = mlx5_ib_modify_cq; 5304e126ba97SEli Cohen dev->ib_dev.resize_cq = mlx5_ib_resize_cq; 5305e126ba97SEli Cohen dev->ib_dev.destroy_cq = mlx5_ib_destroy_cq; 5306e126ba97SEli Cohen dev->ib_dev.poll_cq = mlx5_ib_poll_cq; 5307e126ba97SEli Cohen dev->ib_dev.req_notify_cq = mlx5_ib_arm_cq; 5308e126ba97SEli Cohen dev->ib_dev.get_dma_mr = mlx5_ib_get_dma_mr; 5309e126ba97SEli Cohen dev->ib_dev.reg_user_mr = mlx5_ib_reg_user_mr; 531056e11d62SNoa Osherovich dev->ib_dev.rereg_user_mr = mlx5_ib_rereg_user_mr; 5311e126ba97SEli Cohen dev->ib_dev.dereg_mr = mlx5_ib_dereg_mr; 5312e126ba97SEli Cohen dev->ib_dev.attach_mcast = mlx5_ib_mcg_attach; 5313e126ba97SEli Cohen dev->ib_dev.detach_mcast = mlx5_ib_mcg_detach; 5314e126ba97SEli Cohen dev->ib_dev.process_mad = mlx5_ib_process_mad; 53159bee178bSSagi Grimberg dev->ib_dev.alloc_mr = mlx5_ib_alloc_mr; 53168a187ee5SSagi Grimberg dev->ib_dev.map_mr_sg = mlx5_ib_map_mr_sg; 5317d5436ba0SSagi Grimberg dev->ib_dev.check_mr_status = mlx5_ib_check_mr_status; 5318c7342823SIra Weiny dev->ib_dev.get_dev_fw_str = get_dev_fw_str; 531940b24403SSagi Grimberg dev->ib_dev.get_vector_affinity = mlx5_ib_get_vector_affinity; 53208e959601SNiranjana Vishwanathapura if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads)) 5321693dfd5aSErez Shitrit dev->ib_dev.alloc_rdma_netdev = mlx5_ib_alloc_rdma_netdev; 53228e959601SNiranjana Vishwanathapura 5323eff901d3SEli Cohen if (mlx5_core_is_pf(mdev)) { 5324eff901d3SEli Cohen dev->ib_dev.get_vf_config = mlx5_ib_get_vf_config; 5325eff901d3SEli Cohen dev->ib_dev.set_vf_link_state = mlx5_ib_set_vf_link_state; 5326eff901d3SEli Cohen dev->ib_dev.get_vf_stats = mlx5_ib_get_vf_stats; 5327eff901d3SEli Cohen dev->ib_dev.set_vf_guid = mlx5_ib_set_vf_guid; 5328eff901d3SEli Cohen } 5329e126ba97SEli Cohen 53307c2344c3SMaor Gottlieb dev->ib_dev.disassociate_ucontext = mlx5_ib_disassociate_ucontext; 53317c2344c3SMaor Gottlieb 53326e8484c5SMax Gurtovoy dev->umr_fence = mlx5_get_umr_fence(MLX5_CAP_GEN(mdev, umr_fence)); 53336e8484c5SMax Gurtovoy 5334d2370e0aSMatan Barak if (MLX5_CAP_GEN(mdev, imaicl)) { 5335d2370e0aSMatan Barak dev->ib_dev.alloc_mw = mlx5_ib_alloc_mw; 5336d2370e0aSMatan Barak dev->ib_dev.dealloc_mw = mlx5_ib_dealloc_mw; 5337d2370e0aSMatan Barak dev->ib_dev.uverbs_cmd_mask |= 5338d2370e0aSMatan Barak (1ull << IB_USER_VERBS_CMD_ALLOC_MW) | 5339d2370e0aSMatan Barak (1ull << IB_USER_VERBS_CMD_DEALLOC_MW); 5340d2370e0aSMatan Barak } 5341d2370e0aSMatan Barak 5342938fe83cSSaeed Mahameed if (MLX5_CAP_GEN(mdev, xrc)) { 5343e126ba97SEli Cohen dev->ib_dev.alloc_xrcd = mlx5_ib_alloc_xrcd; 5344e126ba97SEli Cohen dev->ib_dev.dealloc_xrcd = mlx5_ib_dealloc_xrcd; 5345e126ba97SEli Cohen dev->ib_dev.uverbs_cmd_mask |= 5346e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_OPEN_XRCD) | 5347e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_CLOSE_XRCD); 5348e126ba97SEli Cohen } 5349e126ba97SEli Cohen 535024da0016SAriel Levkovich if (MLX5_CAP_DEV_MEM(mdev, memic)) { 535124da0016SAriel Levkovich dev->ib_dev.alloc_dm = mlx5_ib_alloc_dm; 535224da0016SAriel Levkovich dev->ib_dev.dealloc_dm = mlx5_ib_dealloc_dm; 53536c29f57eSAriel Levkovich dev->ib_dev.reg_dm_mr = mlx5_ib_reg_dm_mr; 535424da0016SAriel Levkovich } 535524da0016SAriel Levkovich 5356038d2ef8SMaor Gottlieb dev->ib_dev.create_flow = mlx5_ib_create_flow; 5357038d2ef8SMaor Gottlieb dev->ib_dev.destroy_flow = mlx5_ib_destroy_flow; 535881e30880SYishai Hadas dev->ib_dev.uverbs_ex_cmd_mask |= 535981e30880SYishai Hadas (1ull << IB_USER_VERBS_EX_CMD_CREATE_FLOW) | 536081e30880SYishai Hadas (1ull << IB_USER_VERBS_EX_CMD_DESTROY_FLOW); 5361c6475a0bSAviad Yehezkel dev->ib_dev.create_flow_action_esp = mlx5_ib_create_flow_action_esp; 5362c6475a0bSAviad Yehezkel dev->ib_dev.destroy_flow_action = mlx5_ib_destroy_flow_action; 5363349705c1SMatan Barak dev->ib_dev.modify_flow_action_esp = mlx5_ib_modify_flow_action_esp; 53640ede73bcSMatan Barak dev->ib_dev.driver_id = RDMA_DRIVER_MLX5; 536581e30880SYishai Hadas 5366e126ba97SEli Cohen err = init_node_data(dev); 5367e126ba97SEli Cohen if (err) 536816c1975fSMark Bloch return err; 5369e126ba97SEli Cohen 5370c8b89924SMark Bloch if ((MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_ETH) && 5371e7996a9aSJason Gunthorpe (MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) || 5372e7996a9aSJason Gunthorpe MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc))) 5373c8b89924SMark Bloch mutex_init(&dev->lb_mutex); 5374c8b89924SMark Bloch 537516c1975fSMark Bloch return 0; 537616c1975fSMark Bloch } 537716c1975fSMark Bloch 53788e6efa3aSMark Bloch static int mlx5_ib_stage_non_default_cb(struct mlx5_ib_dev *dev) 537916c1975fSMark Bloch { 53808e6efa3aSMark Bloch dev->ib_dev.get_port_immutable = mlx5_port_immutable; 53818e6efa3aSMark Bloch dev->ib_dev.query_port = mlx5_ib_query_port; 53828e6efa3aSMark Bloch 53838e6efa3aSMark Bloch return 0; 53848e6efa3aSMark Bloch } 53858e6efa3aSMark Bloch 5386b5ca15adSMark Bloch int mlx5_ib_stage_rep_non_default_cb(struct mlx5_ib_dev *dev) 53878e6efa3aSMark Bloch { 53888e6efa3aSMark Bloch dev->ib_dev.get_port_immutable = mlx5_port_rep_immutable; 53898e6efa3aSMark Bloch dev->ib_dev.query_port = mlx5_ib_rep_query_port; 53908e6efa3aSMark Bloch 53918e6efa3aSMark Bloch return 0; 53928e6efa3aSMark Bloch } 53938e6efa3aSMark Bloch 53948e6efa3aSMark Bloch static int mlx5_ib_stage_common_roce_init(struct mlx5_ib_dev *dev, 53958e6efa3aSMark Bloch u8 port_num) 53968e6efa3aSMark Bloch { 53977fd8aefbSDaniel Jurgens int i; 539816c1975fSMark Bloch 53997fd8aefbSDaniel Jurgens for (i = 0; i < dev->num_ports; i++) { 54007fd8aefbSDaniel Jurgens dev->roce[i].dev = dev; 54017fd8aefbSDaniel Jurgens dev->roce[i].native_port_num = i + 1; 54027fd8aefbSDaniel Jurgens dev->roce[i].last_port_state = IB_PORT_DOWN; 54037fd8aefbSDaniel Jurgens } 54047fd8aefbSDaniel Jurgens 5405c11a226aSMark Bloch dev->ib_dev.get_netdev = mlx5_ib_get_netdev; 5406c11a226aSMark Bloch dev->ib_dev.create_wq = mlx5_ib_create_wq; 5407c11a226aSMark Bloch dev->ib_dev.modify_wq = mlx5_ib_modify_wq; 5408c11a226aSMark Bloch dev->ib_dev.destroy_wq = mlx5_ib_destroy_wq; 5409c11a226aSMark Bloch dev->ib_dev.create_rwq_ind_table = mlx5_ib_create_rwq_ind_table; 5410c11a226aSMark Bloch dev->ib_dev.destroy_rwq_ind_table = mlx5_ib_destroy_rwq_ind_table; 54118e6efa3aSMark Bloch 5412c11a226aSMark Bloch dev->ib_dev.uverbs_ex_cmd_mask |= 5413c11a226aSMark Bloch (1ull << IB_USER_VERBS_EX_CMD_CREATE_WQ) | 5414c11a226aSMark Bloch (1ull << IB_USER_VERBS_EX_CMD_MODIFY_WQ) | 5415c11a226aSMark Bloch (1ull << IB_USER_VERBS_EX_CMD_DESTROY_WQ) | 5416c11a226aSMark Bloch (1ull << IB_USER_VERBS_EX_CMD_CREATE_RWQ_IND_TBL) | 5417c11a226aSMark Bloch (1ull << IB_USER_VERBS_EX_CMD_DESTROY_RWQ_IND_TBL); 54188e6efa3aSMark Bloch 54198e6efa3aSMark Bloch return mlx5_add_netdev_notifier(dev, port_num); 54208e6efa3aSMark Bloch } 54218e6efa3aSMark Bloch 54228e6efa3aSMark Bloch static void mlx5_ib_stage_common_roce_cleanup(struct mlx5_ib_dev *dev) 54238e6efa3aSMark Bloch { 54248e6efa3aSMark Bloch u8 port_num = mlx5_core_native_port_num(dev->mdev) - 1; 54258e6efa3aSMark Bloch 54268e6efa3aSMark Bloch mlx5_remove_netdev_notifier(dev, port_num); 54278e6efa3aSMark Bloch } 54288e6efa3aSMark Bloch 54298e6efa3aSMark Bloch int mlx5_ib_stage_rep_roce_init(struct mlx5_ib_dev *dev) 54308e6efa3aSMark Bloch { 54318e6efa3aSMark Bloch struct mlx5_core_dev *mdev = dev->mdev; 54328e6efa3aSMark Bloch enum rdma_link_layer ll; 54338e6efa3aSMark Bloch int port_type_cap; 54348e6efa3aSMark Bloch int err = 0; 54358e6efa3aSMark Bloch u8 port_num; 54368e6efa3aSMark Bloch 54378e6efa3aSMark Bloch port_num = mlx5_core_native_port_num(dev->mdev) - 1; 54388e6efa3aSMark Bloch port_type_cap = MLX5_CAP_GEN(mdev, port_type); 54398e6efa3aSMark Bloch ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap); 54408e6efa3aSMark Bloch 54418e6efa3aSMark Bloch if (ll == IB_LINK_LAYER_ETHERNET) 54428e6efa3aSMark Bloch err = mlx5_ib_stage_common_roce_init(dev, port_num); 54438e6efa3aSMark Bloch 544416c1975fSMark Bloch return err; 5445fc24fc5eSAchiad Shochat } 5446fc24fc5eSAchiad Shochat 54478e6efa3aSMark Bloch void mlx5_ib_stage_rep_roce_cleanup(struct mlx5_ib_dev *dev) 54488e6efa3aSMark Bloch { 54498e6efa3aSMark Bloch mlx5_ib_stage_common_roce_cleanup(dev); 54508e6efa3aSMark Bloch } 54518e6efa3aSMark Bloch 54528e6efa3aSMark Bloch static int mlx5_ib_stage_roce_init(struct mlx5_ib_dev *dev) 54538e6efa3aSMark Bloch { 54548e6efa3aSMark Bloch struct mlx5_core_dev *mdev = dev->mdev; 54558e6efa3aSMark Bloch enum rdma_link_layer ll; 54568e6efa3aSMark Bloch int port_type_cap; 54578e6efa3aSMark Bloch u8 port_num; 54588e6efa3aSMark Bloch int err; 54598e6efa3aSMark Bloch 54608e6efa3aSMark Bloch port_num = mlx5_core_native_port_num(dev->mdev) - 1; 54618e6efa3aSMark Bloch port_type_cap = MLX5_CAP_GEN(mdev, port_type); 54628e6efa3aSMark Bloch ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap); 54638e6efa3aSMark Bloch 54648e6efa3aSMark Bloch if (ll == IB_LINK_LAYER_ETHERNET) { 54658e6efa3aSMark Bloch err = mlx5_ib_stage_common_roce_init(dev, port_num); 54668e6efa3aSMark Bloch if (err) 54678e6efa3aSMark Bloch return err; 54688e6efa3aSMark Bloch 54698e6efa3aSMark Bloch err = mlx5_enable_eth(dev, port_num); 54708e6efa3aSMark Bloch if (err) 54718e6efa3aSMark Bloch goto cleanup; 54728e6efa3aSMark Bloch } 54738e6efa3aSMark Bloch 547416c1975fSMark Bloch return 0; 54758e6efa3aSMark Bloch cleanup: 54768e6efa3aSMark Bloch mlx5_ib_stage_common_roce_cleanup(dev); 54778e6efa3aSMark Bloch 54788e6efa3aSMark Bloch return err; 547945bded2cSKamal Heib } 54806aec21f6SHaggai Eran 548116c1975fSMark Bloch static void mlx5_ib_stage_roce_cleanup(struct mlx5_ib_dev *dev) 548216c1975fSMark Bloch { 548316c1975fSMark Bloch struct mlx5_core_dev *mdev = dev->mdev; 548416c1975fSMark Bloch enum rdma_link_layer ll; 548516c1975fSMark Bloch int port_type_cap; 548632f69e4bSDaniel Jurgens u8 port_num; 54874a2da0b8SParav Pandit 548832f69e4bSDaniel Jurgens port_num = mlx5_core_native_port_num(dev->mdev) - 1; 548916c1975fSMark Bloch port_type_cap = MLX5_CAP_GEN(mdev, port_type); 549016c1975fSMark Bloch ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap); 549116c1975fSMark Bloch 549216c1975fSMark Bloch if (ll == IB_LINK_LAYER_ETHERNET) { 549316c1975fSMark Bloch mlx5_disable_eth(dev); 54948e6efa3aSMark Bloch mlx5_ib_stage_common_roce_cleanup(dev); 549516c1975fSMark Bloch } 549616c1975fSMark Bloch } 549716c1975fSMark Bloch 5498b5ca15adSMark Bloch int mlx5_ib_stage_dev_res_init(struct mlx5_ib_dev *dev) 549916c1975fSMark Bloch { 550016c1975fSMark Bloch return create_dev_resources(&dev->devr); 550116c1975fSMark Bloch } 550216c1975fSMark Bloch 5503b5ca15adSMark Bloch void mlx5_ib_stage_dev_res_cleanup(struct mlx5_ib_dev *dev) 550416c1975fSMark Bloch { 550516c1975fSMark Bloch destroy_dev_resources(&dev->devr); 550616c1975fSMark Bloch } 550716c1975fSMark Bloch 550816c1975fSMark Bloch static int mlx5_ib_stage_odp_init(struct mlx5_ib_dev *dev) 550916c1975fSMark Bloch { 551007321b3cSMark Bloch mlx5_ib_internal_fill_odp_caps(dev); 551107321b3cSMark Bloch 551216c1975fSMark Bloch return mlx5_ib_odp_init_one(dev); 551316c1975fSMark Bloch } 551416c1975fSMark Bloch 5515b5ca15adSMark Bloch int mlx5_ib_stage_counters_init(struct mlx5_ib_dev *dev) 551616c1975fSMark Bloch { 55175e1e7612SMark Bloch if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt)) { 55185e1e7612SMark Bloch dev->ib_dev.get_hw_stats = mlx5_ib_get_hw_stats; 55195e1e7612SMark Bloch dev->ib_dev.alloc_hw_stats = mlx5_ib_alloc_hw_stats; 55205e1e7612SMark Bloch 552116c1975fSMark Bloch return mlx5_ib_alloc_counters(dev); 55225e1e7612SMark Bloch } 552316c1975fSMark Bloch 552416c1975fSMark Bloch return 0; 552516c1975fSMark Bloch } 552616c1975fSMark Bloch 5527b5ca15adSMark Bloch void mlx5_ib_stage_counters_cleanup(struct mlx5_ib_dev *dev) 552816c1975fSMark Bloch { 552916c1975fSMark Bloch if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt)) 553016c1975fSMark Bloch mlx5_ib_dealloc_counters(dev); 553116c1975fSMark Bloch } 553216c1975fSMark Bloch 553316c1975fSMark Bloch static int mlx5_ib_stage_cong_debugfs_init(struct mlx5_ib_dev *dev) 553416c1975fSMark Bloch { 5535a9e546e7SParav Pandit return mlx5_ib_init_cong_debugfs(dev, 5536a9e546e7SParav Pandit mlx5_core_native_port_num(dev->mdev) - 1); 553716c1975fSMark Bloch } 553816c1975fSMark Bloch 553916c1975fSMark Bloch static void mlx5_ib_stage_cong_debugfs_cleanup(struct mlx5_ib_dev *dev) 554016c1975fSMark Bloch { 5541a9e546e7SParav Pandit mlx5_ib_cleanup_cong_debugfs(dev, 5542a9e546e7SParav Pandit mlx5_core_native_port_num(dev->mdev) - 1); 554316c1975fSMark Bloch } 554416c1975fSMark Bloch 554516c1975fSMark Bloch static int mlx5_ib_stage_uar_init(struct mlx5_ib_dev *dev) 554616c1975fSMark Bloch { 55475fe9dec0SEli Cohen dev->mdev->priv.uar = mlx5_get_uars_page(dev->mdev); 5548444261caSLeon Romanovsky return PTR_ERR_OR_ZERO(dev->mdev->priv.uar); 554916c1975fSMark Bloch } 555016c1975fSMark Bloch 555116c1975fSMark Bloch static void mlx5_ib_stage_uar_cleanup(struct mlx5_ib_dev *dev) 555216c1975fSMark Bloch { 555316c1975fSMark Bloch mlx5_put_uars_page(dev->mdev, dev->mdev->priv.uar); 555416c1975fSMark Bloch } 555516c1975fSMark Bloch 5556b5ca15adSMark Bloch int mlx5_ib_stage_bfrag_init(struct mlx5_ib_dev *dev) 555716c1975fSMark Bloch { 555816c1975fSMark Bloch int err; 55595fe9dec0SEli Cohen 55605fe9dec0SEli Cohen err = mlx5_alloc_bfreg(dev->mdev, &dev->bfreg, false, false); 55615fe9dec0SEli Cohen if (err) 556216c1975fSMark Bloch return err; 55635fe9dec0SEli Cohen 55645fe9dec0SEli Cohen err = mlx5_alloc_bfreg(dev->mdev, &dev->fp_bfreg, false, true); 55655fe9dec0SEli Cohen if (err) 556616c1975fSMark Bloch mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg); 55675fe9dec0SEli Cohen 556816c1975fSMark Bloch return err; 556916c1975fSMark Bloch } 55700837e86aSMark Bloch 5571b5ca15adSMark Bloch void mlx5_ib_stage_bfrag_cleanup(struct mlx5_ib_dev *dev) 557216c1975fSMark Bloch { 557316c1975fSMark Bloch mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg); 557416c1975fSMark Bloch mlx5_free_bfreg(dev->mdev, &dev->bfreg); 557516c1975fSMark Bloch } 5576e126ba97SEli Cohen 55778c84660bSMatan Barak static int mlx5_ib_stage_populate_specs(struct mlx5_ib_dev *dev) 55788c84660bSMatan Barak { 55798c84660bSMatan Barak return populate_specs_root(dev); 55808c84660bSMatan Barak } 55818c84660bSMatan Barak 5582b5ca15adSMark Bloch int mlx5_ib_stage_ib_reg_init(struct mlx5_ib_dev *dev) 558316c1975fSMark Bloch { 558416c1975fSMark Bloch return ib_register_device(&dev->ib_dev, NULL); 558516c1975fSMark Bloch } 558616c1975fSMark Bloch 55878c84660bSMatan Barak static void mlx5_ib_stage_depopulate_specs(struct mlx5_ib_dev *dev) 55888c84660bSMatan Barak { 55898c84660bSMatan Barak depopulate_specs_root(dev); 55908c84660bSMatan Barak } 55918c84660bSMatan Barak 559203fe2debSDavid S. Miller void mlx5_ib_stage_pre_ib_reg_umr_cleanup(struct mlx5_ib_dev *dev) 559342cea83fSMark Bloch { 559442cea83fSMark Bloch destroy_umrc_res(dev); 559542cea83fSMark Bloch } 559642cea83fSMark Bloch 5597b5ca15adSMark Bloch void mlx5_ib_stage_ib_reg_cleanup(struct mlx5_ib_dev *dev) 559816c1975fSMark Bloch { 559916c1975fSMark Bloch ib_unregister_device(&dev->ib_dev); 560016c1975fSMark Bloch } 560116c1975fSMark Bloch 560203fe2debSDavid S. Miller int mlx5_ib_stage_post_ib_reg_umr_init(struct mlx5_ib_dev *dev) 560316c1975fSMark Bloch { 560416c1975fSMark Bloch return create_umr_res(dev); 560516c1975fSMark Bloch } 560616c1975fSMark Bloch 560716c1975fSMark Bloch static int mlx5_ib_stage_delay_drop_init(struct mlx5_ib_dev *dev) 560816c1975fSMark Bloch { 560903404e8aSMaor Gottlieb init_delay_drop(dev); 561003404e8aSMaor Gottlieb 561116c1975fSMark Bloch return 0; 561216c1975fSMark Bloch } 561316c1975fSMark Bloch 561416c1975fSMark Bloch static void mlx5_ib_stage_delay_drop_cleanup(struct mlx5_ib_dev *dev) 561516c1975fSMark Bloch { 561616c1975fSMark Bloch cancel_delay_drop(dev); 561716c1975fSMark Bloch } 561816c1975fSMark Bloch 5619b5ca15adSMark Bloch int mlx5_ib_stage_class_attr_init(struct mlx5_ib_dev *dev) 562016c1975fSMark Bloch { 562116c1975fSMark Bloch int err; 562216c1975fSMark Bloch int i; 562316c1975fSMark Bloch 5624e126ba97SEli Cohen for (i = 0; i < ARRAY_SIZE(mlx5_class_attributes); i++) { 5625281d1a92SWei Yongjun err = device_create_file(&dev->ib_dev.dev, 5626281d1a92SWei Yongjun mlx5_class_attributes[i]); 5627281d1a92SWei Yongjun if (err) 562816c1975fSMark Bloch return err; 5629e126ba97SEli Cohen } 5630e126ba97SEli Cohen 563116c1975fSMark Bloch return 0; 563216c1975fSMark Bloch } 563316c1975fSMark Bloch 5634fc385b7aSMark Bloch static int mlx5_ib_stage_rep_reg_init(struct mlx5_ib_dev *dev) 5635fc385b7aSMark Bloch { 5636fc385b7aSMark Bloch mlx5_ib_register_vport_reps(dev); 5637fc385b7aSMark Bloch 5638fc385b7aSMark Bloch return 0; 5639fc385b7aSMark Bloch } 5640fc385b7aSMark Bloch 5641fc385b7aSMark Bloch static void mlx5_ib_stage_rep_reg_cleanup(struct mlx5_ib_dev *dev) 5642fc385b7aSMark Bloch { 5643fc385b7aSMark Bloch mlx5_ib_unregister_vport_reps(dev); 5644fc385b7aSMark Bloch } 5645fc385b7aSMark Bloch 5646b5ca15adSMark Bloch void __mlx5_ib_remove(struct mlx5_ib_dev *dev, 564716c1975fSMark Bloch const struct mlx5_ib_profile *profile, 564816c1975fSMark Bloch int stage) 564916c1975fSMark Bloch { 565016c1975fSMark Bloch /* Number of stages to cleanup */ 565116c1975fSMark Bloch while (stage) { 565216c1975fSMark Bloch stage--; 565316c1975fSMark Bloch if (profile->stage[stage].cleanup) 565416c1975fSMark Bloch profile->stage[stage].cleanup(dev); 565516c1975fSMark Bloch } 565616c1975fSMark Bloch 565716c1975fSMark Bloch ib_dealloc_device((struct ib_device *)dev); 565816c1975fSMark Bloch } 565916c1975fSMark Bloch 566032f69e4bSDaniel Jurgens static void *mlx5_ib_add_slave_port(struct mlx5_core_dev *mdev, u8 port_num); 566132f69e4bSDaniel Jurgens 5662b5ca15adSMark Bloch void *__mlx5_ib_add(struct mlx5_ib_dev *dev, 566316c1975fSMark Bloch const struct mlx5_ib_profile *profile) 566416c1975fSMark Bloch { 566516c1975fSMark Bloch int err; 566616c1975fSMark Bloch int i; 566716c1975fSMark Bloch 566816c1975fSMark Bloch printk_once(KERN_INFO "%s", mlx5_version); 566916c1975fSMark Bloch 567016c1975fSMark Bloch for (i = 0; i < MLX5_IB_STAGE_MAX; i++) { 567116c1975fSMark Bloch if (profile->stage[i].init) { 567216c1975fSMark Bloch err = profile->stage[i].init(dev); 567316c1975fSMark Bloch if (err) 567416c1975fSMark Bloch goto err_out; 567516c1975fSMark Bloch } 567616c1975fSMark Bloch } 567716c1975fSMark Bloch 567816c1975fSMark Bloch dev->profile = profile; 5679e126ba97SEli Cohen dev->ib_active = true; 5680e126ba97SEli Cohen 56819603b61dSJack Morgenstein return dev; 5682e126ba97SEli Cohen 568316c1975fSMark Bloch err_out: 568416c1975fSMark Bloch __mlx5_ib_remove(dev, profile, i); 5685e126ba97SEli Cohen 56869603b61dSJack Morgenstein return NULL; 5687e126ba97SEli Cohen } 5688e126ba97SEli Cohen 568916c1975fSMark Bloch static const struct mlx5_ib_profile pf_profile = { 569016c1975fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_INIT, 569116c1975fSMark Bloch mlx5_ib_stage_init_init, 569216c1975fSMark Bloch mlx5_ib_stage_init_cleanup), 56939a4ca38dSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_FLOW_DB, 56949a4ca38dSMark Bloch mlx5_ib_stage_flow_db_init, 56959a4ca38dSMark Bloch mlx5_ib_stage_flow_db_cleanup), 569616c1975fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_CAPS, 569716c1975fSMark Bloch mlx5_ib_stage_caps_init, 569816c1975fSMark Bloch NULL), 56998e6efa3aSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB, 57008e6efa3aSMark Bloch mlx5_ib_stage_non_default_cb, 57018e6efa3aSMark Bloch NULL), 570216c1975fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_ROCE, 570316c1975fSMark Bloch mlx5_ib_stage_roce_init, 570416c1975fSMark Bloch mlx5_ib_stage_roce_cleanup), 570516c1975fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES, 570616c1975fSMark Bloch mlx5_ib_stage_dev_res_init, 570716c1975fSMark Bloch mlx5_ib_stage_dev_res_cleanup), 570816c1975fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_ODP, 570916c1975fSMark Bloch mlx5_ib_stage_odp_init, 57103cc297dbSMark Bloch NULL), 571116c1975fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_COUNTERS, 571216c1975fSMark Bloch mlx5_ib_stage_counters_init, 571316c1975fSMark Bloch mlx5_ib_stage_counters_cleanup), 571416c1975fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_CONG_DEBUGFS, 571516c1975fSMark Bloch mlx5_ib_stage_cong_debugfs_init, 571616c1975fSMark Bloch mlx5_ib_stage_cong_debugfs_cleanup), 571716c1975fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_UAR, 571816c1975fSMark Bloch mlx5_ib_stage_uar_init, 571916c1975fSMark Bloch mlx5_ib_stage_uar_cleanup), 572016c1975fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_BFREG, 572116c1975fSMark Bloch mlx5_ib_stage_bfrag_init, 572216c1975fSMark Bloch mlx5_ib_stage_bfrag_cleanup), 572342cea83fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR, 572442cea83fSMark Bloch NULL, 572542cea83fSMark Bloch mlx5_ib_stage_pre_ib_reg_umr_cleanup), 57268c84660bSMatan Barak STAGE_CREATE(MLX5_IB_STAGE_SPECS, 57278c84660bSMatan Barak mlx5_ib_stage_populate_specs, 57288c84660bSMatan Barak mlx5_ib_stage_depopulate_specs), 572916c1975fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_IB_REG, 573016c1975fSMark Bloch mlx5_ib_stage_ib_reg_init, 573116c1975fSMark Bloch mlx5_ib_stage_ib_reg_cleanup), 573242cea83fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR, 573342cea83fSMark Bloch mlx5_ib_stage_post_ib_reg_umr_init, 573442cea83fSMark Bloch NULL), 573516c1975fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_DELAY_DROP, 573616c1975fSMark Bloch mlx5_ib_stage_delay_drop_init, 573716c1975fSMark Bloch mlx5_ib_stage_delay_drop_cleanup), 573816c1975fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_CLASS_ATTR, 573916c1975fSMark Bloch mlx5_ib_stage_class_attr_init, 574016c1975fSMark Bloch NULL), 574116c1975fSMark Bloch }; 574216c1975fSMark Bloch 5743b5ca15adSMark Bloch static const struct mlx5_ib_profile nic_rep_profile = { 5744b5ca15adSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_INIT, 5745b5ca15adSMark Bloch mlx5_ib_stage_init_init, 5746b5ca15adSMark Bloch mlx5_ib_stage_init_cleanup), 5747b5ca15adSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_FLOW_DB, 5748b5ca15adSMark Bloch mlx5_ib_stage_flow_db_init, 5749b5ca15adSMark Bloch mlx5_ib_stage_flow_db_cleanup), 5750b5ca15adSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_CAPS, 5751b5ca15adSMark Bloch mlx5_ib_stage_caps_init, 5752b5ca15adSMark Bloch NULL), 5753b5ca15adSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB, 5754b5ca15adSMark Bloch mlx5_ib_stage_rep_non_default_cb, 5755b5ca15adSMark Bloch NULL), 5756b5ca15adSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_ROCE, 5757b5ca15adSMark Bloch mlx5_ib_stage_rep_roce_init, 5758b5ca15adSMark Bloch mlx5_ib_stage_rep_roce_cleanup), 5759b5ca15adSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES, 5760b5ca15adSMark Bloch mlx5_ib_stage_dev_res_init, 5761b5ca15adSMark Bloch mlx5_ib_stage_dev_res_cleanup), 5762b5ca15adSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_COUNTERS, 5763b5ca15adSMark Bloch mlx5_ib_stage_counters_init, 5764b5ca15adSMark Bloch mlx5_ib_stage_counters_cleanup), 5765b5ca15adSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_UAR, 5766b5ca15adSMark Bloch mlx5_ib_stage_uar_init, 5767b5ca15adSMark Bloch mlx5_ib_stage_uar_cleanup), 5768b5ca15adSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_BFREG, 5769b5ca15adSMark Bloch mlx5_ib_stage_bfrag_init, 5770b5ca15adSMark Bloch mlx5_ib_stage_bfrag_cleanup), 577103fe2debSDavid S. Miller STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR, 577203fe2debSDavid S. Miller NULL, 577303fe2debSDavid S. Miller mlx5_ib_stage_pre_ib_reg_umr_cleanup), 57748c84660bSMatan Barak STAGE_CREATE(MLX5_IB_STAGE_SPECS, 57758c84660bSMatan Barak mlx5_ib_stage_populate_specs, 57768c84660bSMatan Barak mlx5_ib_stage_depopulate_specs), 5777b5ca15adSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_IB_REG, 5778b5ca15adSMark Bloch mlx5_ib_stage_ib_reg_init, 5779b5ca15adSMark Bloch mlx5_ib_stage_ib_reg_cleanup), 578003fe2debSDavid S. Miller STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR, 578103fe2debSDavid S. Miller mlx5_ib_stage_post_ib_reg_umr_init, 578203fe2debSDavid S. Miller NULL), 5783b5ca15adSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_CLASS_ATTR, 5784b5ca15adSMark Bloch mlx5_ib_stage_class_attr_init, 5785b5ca15adSMark Bloch NULL), 5786b5ca15adSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_REP_REG, 5787b5ca15adSMark Bloch mlx5_ib_stage_rep_reg_init, 5788b5ca15adSMark Bloch mlx5_ib_stage_rep_reg_cleanup), 5789b5ca15adSMark Bloch }; 5790b5ca15adSMark Bloch 579132f69e4bSDaniel Jurgens static void *mlx5_ib_add_slave_port(struct mlx5_core_dev *mdev, u8 port_num) 579232f69e4bSDaniel Jurgens { 579332f69e4bSDaniel Jurgens struct mlx5_ib_multiport_info *mpi; 579432f69e4bSDaniel Jurgens struct mlx5_ib_dev *dev; 579532f69e4bSDaniel Jurgens bool bound = false; 579632f69e4bSDaniel Jurgens int err; 579732f69e4bSDaniel Jurgens 579832f69e4bSDaniel Jurgens mpi = kzalloc(sizeof(*mpi), GFP_KERNEL); 579932f69e4bSDaniel Jurgens if (!mpi) 580032f69e4bSDaniel Jurgens return NULL; 580132f69e4bSDaniel Jurgens 580232f69e4bSDaniel Jurgens mpi->mdev = mdev; 580332f69e4bSDaniel Jurgens 580432f69e4bSDaniel Jurgens err = mlx5_query_nic_vport_system_image_guid(mdev, 580532f69e4bSDaniel Jurgens &mpi->sys_image_guid); 580632f69e4bSDaniel Jurgens if (err) { 580732f69e4bSDaniel Jurgens kfree(mpi); 580832f69e4bSDaniel Jurgens return NULL; 580932f69e4bSDaniel Jurgens } 581032f69e4bSDaniel Jurgens 581132f69e4bSDaniel Jurgens mutex_lock(&mlx5_ib_multiport_mutex); 581232f69e4bSDaniel Jurgens list_for_each_entry(dev, &mlx5_ib_dev_list, ib_dev_list) { 581332f69e4bSDaniel Jurgens if (dev->sys_image_guid == mpi->sys_image_guid) 581432f69e4bSDaniel Jurgens bound = mlx5_ib_bind_slave_port(dev, mpi); 581532f69e4bSDaniel Jurgens 581632f69e4bSDaniel Jurgens if (bound) { 581732f69e4bSDaniel Jurgens rdma_roce_rescan_device(&dev->ib_dev); 581832f69e4bSDaniel Jurgens break; 581932f69e4bSDaniel Jurgens } 582032f69e4bSDaniel Jurgens } 582132f69e4bSDaniel Jurgens 582232f69e4bSDaniel Jurgens if (!bound) { 582332f69e4bSDaniel Jurgens list_add_tail(&mpi->list, &mlx5_ib_unaffiliated_port_list); 582432f69e4bSDaniel Jurgens dev_dbg(&mdev->pdev->dev, "no suitable IB device found to bind to, added to unaffiliated list.\n"); 582532f69e4bSDaniel Jurgens } else { 582632f69e4bSDaniel Jurgens mlx5_ib_dbg(dev, "bound port %u\n", port_num + 1); 582732f69e4bSDaniel Jurgens } 582832f69e4bSDaniel Jurgens mutex_unlock(&mlx5_ib_multiport_mutex); 582932f69e4bSDaniel Jurgens 583032f69e4bSDaniel Jurgens return mpi; 583132f69e4bSDaniel Jurgens } 583232f69e4bSDaniel Jurgens 583316c1975fSMark Bloch static void *mlx5_ib_add(struct mlx5_core_dev *mdev) 583416c1975fSMark Bloch { 583532f69e4bSDaniel Jurgens enum rdma_link_layer ll; 5836b5ca15adSMark Bloch struct mlx5_ib_dev *dev; 583732f69e4bSDaniel Jurgens int port_type_cap; 583832f69e4bSDaniel Jurgens 5839b5ca15adSMark Bloch printk_once(KERN_INFO "%s", mlx5_version); 5840b5ca15adSMark Bloch 584132f69e4bSDaniel Jurgens port_type_cap = MLX5_CAP_GEN(mdev, port_type); 584232f69e4bSDaniel Jurgens ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap); 584332f69e4bSDaniel Jurgens 584432f69e4bSDaniel Jurgens if (mlx5_core_is_mp_slave(mdev) && ll == IB_LINK_LAYER_ETHERNET) { 584532f69e4bSDaniel Jurgens u8 port_num = mlx5_core_native_port_num(mdev) - 1; 584632f69e4bSDaniel Jurgens 584732f69e4bSDaniel Jurgens return mlx5_ib_add_slave_port(mdev, port_num); 584832f69e4bSDaniel Jurgens } 584932f69e4bSDaniel Jurgens 5850b5ca15adSMark Bloch dev = (struct mlx5_ib_dev *)ib_alloc_device(sizeof(*dev)); 5851b5ca15adSMark Bloch if (!dev) 5852b5ca15adSMark Bloch return NULL; 5853b5ca15adSMark Bloch 5854b5ca15adSMark Bloch dev->mdev = mdev; 5855b5ca15adSMark Bloch dev->num_ports = max(MLX5_CAP_GEN(mdev, num_ports), 5856b5ca15adSMark Bloch MLX5_CAP_GEN(mdev, num_vhca_ports)); 5857b5ca15adSMark Bloch 5858b5ca15adSMark Bloch if (MLX5_VPORT_MANAGER(mdev) && 5859b5ca15adSMark Bloch mlx5_ib_eswitch_mode(mdev->priv.eswitch) == SRIOV_OFFLOADS) { 5860b5ca15adSMark Bloch dev->rep = mlx5_ib_vport_rep(mdev->priv.eswitch, 0); 5861b5ca15adSMark Bloch 5862b5ca15adSMark Bloch return __mlx5_ib_add(dev, &nic_rep_profile); 5863b5ca15adSMark Bloch } 5864b5ca15adSMark Bloch 5865b5ca15adSMark Bloch return __mlx5_ib_add(dev, &pf_profile); 586616c1975fSMark Bloch } 586716c1975fSMark Bloch 58689603b61dSJack Morgenstein static void mlx5_ib_remove(struct mlx5_core_dev *mdev, void *context) 5869e126ba97SEli Cohen { 587032f69e4bSDaniel Jurgens struct mlx5_ib_multiport_info *mpi; 587132f69e4bSDaniel Jurgens struct mlx5_ib_dev *dev; 58726aec21f6SHaggai Eran 587332f69e4bSDaniel Jurgens if (mlx5_core_is_mp_slave(mdev)) { 587432f69e4bSDaniel Jurgens mpi = context; 587532f69e4bSDaniel Jurgens mutex_lock(&mlx5_ib_multiport_mutex); 587632f69e4bSDaniel Jurgens if (mpi->ibdev) 587732f69e4bSDaniel Jurgens mlx5_ib_unbind_slave_port(mpi->ibdev, mpi); 587832f69e4bSDaniel Jurgens list_del(&mpi->list); 587932f69e4bSDaniel Jurgens mutex_unlock(&mlx5_ib_multiport_mutex); 588032f69e4bSDaniel Jurgens return; 588132f69e4bSDaniel Jurgens } 588232f69e4bSDaniel Jurgens 588332f69e4bSDaniel Jurgens dev = context; 588416c1975fSMark Bloch __mlx5_ib_remove(dev, dev->profile, MLX5_IB_STAGE_MAX); 5885e126ba97SEli Cohen } 5886e126ba97SEli Cohen 58879603b61dSJack Morgenstein static struct mlx5_interface mlx5_ib_interface = { 58889603b61dSJack Morgenstein .add = mlx5_ib_add, 58899603b61dSJack Morgenstein .remove = mlx5_ib_remove, 58909603b61dSJack Morgenstein .event = mlx5_ib_event, 5891d9aaed83SArtemy Kovalyov #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING 5892d9aaed83SArtemy Kovalyov .pfault = mlx5_ib_pfault, 5893d9aaed83SArtemy Kovalyov #endif 589464613d94SSaeed Mahameed .protocol = MLX5_INTERFACE_PROTOCOL_IB, 5895e126ba97SEli Cohen }; 5896e126ba97SEli Cohen 5897c44ef998SIlya Lesokhin unsigned long mlx5_ib_get_xlt_emergency_page(void) 5898c44ef998SIlya Lesokhin { 5899c44ef998SIlya Lesokhin mutex_lock(&xlt_emergency_page_mutex); 5900c44ef998SIlya Lesokhin return xlt_emergency_page; 5901c44ef998SIlya Lesokhin } 5902c44ef998SIlya Lesokhin 5903c44ef998SIlya Lesokhin void mlx5_ib_put_xlt_emergency_page(void) 5904c44ef998SIlya Lesokhin { 5905c44ef998SIlya Lesokhin mutex_unlock(&xlt_emergency_page_mutex); 5906c44ef998SIlya Lesokhin } 5907c44ef998SIlya Lesokhin 5908e126ba97SEli Cohen static int __init mlx5_ib_init(void) 5909e126ba97SEli Cohen { 59106aec21f6SHaggai Eran int err; 59116aec21f6SHaggai Eran 5912c44ef998SIlya Lesokhin xlt_emergency_page = __get_free_page(GFP_KERNEL); 5913c44ef998SIlya Lesokhin if (!xlt_emergency_page) 5914d69a24e0SDaniel Jurgens return -ENOMEM; 5915d69a24e0SDaniel Jurgens 5916c44ef998SIlya Lesokhin mutex_init(&xlt_emergency_page_mutex); 5917c44ef998SIlya Lesokhin 5918c44ef998SIlya Lesokhin mlx5_ib_event_wq = alloc_ordered_workqueue("mlx5_ib_event_wq", 0); 5919c44ef998SIlya Lesokhin if (!mlx5_ib_event_wq) { 5920c44ef998SIlya Lesokhin free_page(xlt_emergency_page); 5921c44ef998SIlya Lesokhin return -ENOMEM; 5922c44ef998SIlya Lesokhin } 5923c44ef998SIlya Lesokhin 592481713d37SArtemy Kovalyov mlx5_ib_odp_init(); 59259603b61dSJack Morgenstein 59266aec21f6SHaggai Eran err = mlx5_register_interface(&mlx5_ib_interface); 59276aec21f6SHaggai Eran 59286aec21f6SHaggai Eran return err; 5929e126ba97SEli Cohen } 5930e126ba97SEli Cohen 5931e126ba97SEli Cohen static void __exit mlx5_ib_cleanup(void) 5932e126ba97SEli Cohen { 59339603b61dSJack Morgenstein mlx5_unregister_interface(&mlx5_ib_interface); 5934d69a24e0SDaniel Jurgens destroy_workqueue(mlx5_ib_event_wq); 5935c44ef998SIlya Lesokhin mutex_destroy(&xlt_emergency_page_mutex); 5936c44ef998SIlya Lesokhin free_page(xlt_emergency_page); 5937e126ba97SEli Cohen } 5938e126ba97SEli Cohen 5939e126ba97SEli Cohen module_init(mlx5_ib_init); 5940e126ba97SEli Cohen module_exit(mlx5_ib_cleanup); 5941