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 513f4df9a7cSParav Pandit static int mlx5_ib_add_gid(const struct ib_gid_attr *attr, 5143cca2606SAchiad Shochat __always_unused void **context) 5153cca2606SAchiad Shochat { 516414448d2SParav Pandit return set_roce_addr(to_mdev(attr->device), attr->port_num, 517f4df9a7cSParav Pandit attr->index, &attr->gid, attr); 5183cca2606SAchiad Shochat } 5193cca2606SAchiad Shochat 520414448d2SParav Pandit static int mlx5_ib_del_gid(const struct ib_gid_attr *attr, 521414448d2SParav Pandit __always_unused void **context) 5223cca2606SAchiad Shochat { 523414448d2SParav Pandit return set_roce_addr(to_mdev(attr->device), attr->port_num, 524414448d2SParav Pandit attr->index, NULL, NULL); 5253cca2606SAchiad Shochat } 5263cca2606SAchiad Shochat 52747ec3866SParav Pandit __be16 mlx5_get_roce_udp_sport(struct mlx5_ib_dev *dev, 52847ec3866SParav Pandit const struct ib_gid_attr *attr) 5292811ba51SAchiad Shochat { 53047ec3866SParav Pandit if (attr->gid_type != IB_GID_TYPE_ROCE_UDP_ENCAP) 5312811ba51SAchiad Shochat return 0; 5322811ba51SAchiad Shochat 5332811ba51SAchiad Shochat return cpu_to_be16(MLX5_CAP_ROCE(dev->mdev, r_roce_min_src_udp_port)); 5342811ba51SAchiad Shochat } 5352811ba51SAchiad Shochat 5361b5daf11SMajd Dibbiny static int mlx5_use_mad_ifc(struct mlx5_ib_dev *dev) 5371b5daf11SMajd Dibbiny { 5387fae6655SNoa Osherovich if (MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_IB) 539d603c809SEli Cohen return !MLX5_CAP_GEN(dev->mdev, ib_virt); 5407fae6655SNoa Osherovich return 0; 5411b5daf11SMajd Dibbiny } 5421b5daf11SMajd Dibbiny 5431b5daf11SMajd Dibbiny enum { 5441b5daf11SMajd Dibbiny MLX5_VPORT_ACCESS_METHOD_MAD, 5451b5daf11SMajd Dibbiny MLX5_VPORT_ACCESS_METHOD_HCA, 5461b5daf11SMajd Dibbiny MLX5_VPORT_ACCESS_METHOD_NIC, 5471b5daf11SMajd Dibbiny }; 5481b5daf11SMajd Dibbiny 5491b5daf11SMajd Dibbiny static int mlx5_get_vport_access_method(struct ib_device *ibdev) 5501b5daf11SMajd Dibbiny { 5511b5daf11SMajd Dibbiny if (mlx5_use_mad_ifc(to_mdev(ibdev))) 5521b5daf11SMajd Dibbiny return MLX5_VPORT_ACCESS_METHOD_MAD; 5531b5daf11SMajd Dibbiny 554ebd61f68SAchiad Shochat if (mlx5_ib_port_link_layer(ibdev, 1) == 5551b5daf11SMajd Dibbiny IB_LINK_LAYER_ETHERNET) 5561b5daf11SMajd Dibbiny return MLX5_VPORT_ACCESS_METHOD_NIC; 5571b5daf11SMajd Dibbiny 5581b5daf11SMajd Dibbiny return MLX5_VPORT_ACCESS_METHOD_HCA; 5591b5daf11SMajd Dibbiny } 5601b5daf11SMajd Dibbiny 561da7525d2SEran Ben Elisha static void get_atomic_caps(struct mlx5_ib_dev *dev, 562776a3906SMoni Shoua u8 atomic_size_qp, 563da7525d2SEran Ben Elisha struct ib_device_attr *props) 564da7525d2SEran Ben Elisha { 565da7525d2SEran Ben Elisha u8 tmp; 566da7525d2SEran Ben Elisha u8 atomic_operations = MLX5_CAP_ATOMIC(dev->mdev, atomic_operations); 567da7525d2SEran Ben Elisha u8 atomic_req_8B_endianness_mode = 568bd10838aSOr Gerlitz MLX5_CAP_ATOMIC(dev->mdev, atomic_req_8B_endianness_mode); 569da7525d2SEran Ben Elisha 570da7525d2SEran Ben Elisha /* Check if HW supports 8 bytes standard atomic operations and capable 571da7525d2SEran Ben Elisha * of host endianness respond 572da7525d2SEran Ben Elisha */ 573da7525d2SEran Ben Elisha tmp = MLX5_ATOMIC_OPS_CMP_SWAP | MLX5_ATOMIC_OPS_FETCH_ADD; 574da7525d2SEran Ben Elisha if (((atomic_operations & tmp) == tmp) && 575da7525d2SEran Ben Elisha (atomic_size_qp & MLX5_ATOMIC_SIZE_QP_8BYTES) && 576da7525d2SEran Ben Elisha (atomic_req_8B_endianness_mode)) { 577da7525d2SEran Ben Elisha props->atomic_cap = IB_ATOMIC_HCA; 578da7525d2SEran Ben Elisha } else { 579da7525d2SEran Ben Elisha props->atomic_cap = IB_ATOMIC_NONE; 580da7525d2SEran Ben Elisha } 581da7525d2SEran Ben Elisha } 582da7525d2SEran Ben Elisha 583776a3906SMoni Shoua static void get_atomic_caps_qp(struct mlx5_ib_dev *dev, 584776a3906SMoni Shoua struct ib_device_attr *props) 585776a3906SMoni Shoua { 586776a3906SMoni Shoua u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_qp); 587776a3906SMoni Shoua 588776a3906SMoni Shoua get_atomic_caps(dev, atomic_size_qp, props); 589776a3906SMoni Shoua } 590776a3906SMoni Shoua 591776a3906SMoni Shoua static void get_atomic_caps_dc(struct mlx5_ib_dev *dev, 592776a3906SMoni Shoua struct ib_device_attr *props) 593776a3906SMoni Shoua { 594776a3906SMoni Shoua u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_dc); 595776a3906SMoni Shoua 596776a3906SMoni Shoua get_atomic_caps(dev, atomic_size_qp, props); 597776a3906SMoni Shoua } 598776a3906SMoni Shoua 599776a3906SMoni Shoua bool mlx5_ib_dc_atomic_is_supported(struct mlx5_ib_dev *dev) 600776a3906SMoni Shoua { 601776a3906SMoni Shoua struct ib_device_attr props = {}; 602776a3906SMoni Shoua 603776a3906SMoni Shoua get_atomic_caps_dc(dev, &props); 604776a3906SMoni Shoua return (props.atomic_cap == IB_ATOMIC_HCA) ? true : false; 605776a3906SMoni Shoua } 6061b5daf11SMajd Dibbiny static int mlx5_query_system_image_guid(struct ib_device *ibdev, 6071b5daf11SMajd Dibbiny __be64 *sys_image_guid) 6081b5daf11SMajd Dibbiny { 6091b5daf11SMajd Dibbiny struct mlx5_ib_dev *dev = to_mdev(ibdev); 6101b5daf11SMajd Dibbiny struct mlx5_core_dev *mdev = dev->mdev; 6111b5daf11SMajd Dibbiny u64 tmp; 6121b5daf11SMajd Dibbiny int err; 6131b5daf11SMajd Dibbiny 6141b5daf11SMajd Dibbiny switch (mlx5_get_vport_access_method(ibdev)) { 6151b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_MAD: 6161b5daf11SMajd Dibbiny return mlx5_query_mad_ifc_system_image_guid(ibdev, 6171b5daf11SMajd Dibbiny sys_image_guid); 6181b5daf11SMajd Dibbiny 6191b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_HCA: 6201b5daf11SMajd Dibbiny err = mlx5_query_hca_vport_system_image_guid(mdev, &tmp); 6213f89a643SAchiad Shochat break; 6223f89a643SAchiad Shochat 6233f89a643SAchiad Shochat case MLX5_VPORT_ACCESS_METHOD_NIC: 6243f89a643SAchiad Shochat err = mlx5_query_nic_vport_system_image_guid(mdev, &tmp); 6253f89a643SAchiad Shochat break; 6261b5daf11SMajd Dibbiny 6271b5daf11SMajd Dibbiny default: 6281b5daf11SMajd Dibbiny return -EINVAL; 6291b5daf11SMajd Dibbiny } 6303f89a643SAchiad Shochat 6313f89a643SAchiad Shochat if (!err) 6323f89a643SAchiad Shochat *sys_image_guid = cpu_to_be64(tmp); 6333f89a643SAchiad Shochat 6343f89a643SAchiad Shochat return err; 6353f89a643SAchiad Shochat 6361b5daf11SMajd Dibbiny } 6371b5daf11SMajd Dibbiny 6381b5daf11SMajd Dibbiny static int mlx5_query_max_pkeys(struct ib_device *ibdev, 6391b5daf11SMajd Dibbiny u16 *max_pkeys) 6401b5daf11SMajd Dibbiny { 6411b5daf11SMajd Dibbiny struct mlx5_ib_dev *dev = to_mdev(ibdev); 6421b5daf11SMajd Dibbiny struct mlx5_core_dev *mdev = dev->mdev; 6431b5daf11SMajd Dibbiny 6441b5daf11SMajd Dibbiny switch (mlx5_get_vport_access_method(ibdev)) { 6451b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_MAD: 6461b5daf11SMajd Dibbiny return mlx5_query_mad_ifc_max_pkeys(ibdev, max_pkeys); 6471b5daf11SMajd Dibbiny 6481b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_HCA: 6491b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_NIC: 6501b5daf11SMajd Dibbiny *max_pkeys = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, 6511b5daf11SMajd Dibbiny pkey_table_size)); 6521b5daf11SMajd Dibbiny return 0; 6531b5daf11SMajd Dibbiny 6541b5daf11SMajd Dibbiny default: 6551b5daf11SMajd Dibbiny return -EINVAL; 6561b5daf11SMajd Dibbiny } 6571b5daf11SMajd Dibbiny } 6581b5daf11SMajd Dibbiny 6591b5daf11SMajd Dibbiny static int mlx5_query_vendor_id(struct ib_device *ibdev, 6601b5daf11SMajd Dibbiny u32 *vendor_id) 6611b5daf11SMajd Dibbiny { 6621b5daf11SMajd Dibbiny struct mlx5_ib_dev *dev = to_mdev(ibdev); 6631b5daf11SMajd Dibbiny 6641b5daf11SMajd Dibbiny switch (mlx5_get_vport_access_method(ibdev)) { 6651b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_MAD: 6661b5daf11SMajd Dibbiny return mlx5_query_mad_ifc_vendor_id(ibdev, vendor_id); 6671b5daf11SMajd Dibbiny 6681b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_HCA: 6691b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_NIC: 6701b5daf11SMajd Dibbiny return mlx5_core_query_vendor_id(dev->mdev, vendor_id); 6711b5daf11SMajd Dibbiny 6721b5daf11SMajd Dibbiny default: 6731b5daf11SMajd Dibbiny return -EINVAL; 6741b5daf11SMajd Dibbiny } 6751b5daf11SMajd Dibbiny } 6761b5daf11SMajd Dibbiny 6771b5daf11SMajd Dibbiny static int mlx5_query_node_guid(struct mlx5_ib_dev *dev, 6781b5daf11SMajd Dibbiny __be64 *node_guid) 6791b5daf11SMajd Dibbiny { 6801b5daf11SMajd Dibbiny u64 tmp; 6811b5daf11SMajd Dibbiny int err; 6821b5daf11SMajd Dibbiny 6831b5daf11SMajd Dibbiny switch (mlx5_get_vport_access_method(&dev->ib_dev)) { 6841b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_MAD: 6851b5daf11SMajd Dibbiny return mlx5_query_mad_ifc_node_guid(dev, node_guid); 6861b5daf11SMajd Dibbiny 6871b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_HCA: 6881b5daf11SMajd Dibbiny err = mlx5_query_hca_vport_node_guid(dev->mdev, &tmp); 6893f89a643SAchiad Shochat break; 6903f89a643SAchiad Shochat 6913f89a643SAchiad Shochat case MLX5_VPORT_ACCESS_METHOD_NIC: 6923f89a643SAchiad Shochat err = mlx5_query_nic_vport_node_guid(dev->mdev, &tmp); 6933f89a643SAchiad Shochat break; 6941b5daf11SMajd Dibbiny 6951b5daf11SMajd Dibbiny default: 6961b5daf11SMajd Dibbiny return -EINVAL; 6971b5daf11SMajd Dibbiny } 6983f89a643SAchiad Shochat 6993f89a643SAchiad Shochat if (!err) 7003f89a643SAchiad Shochat *node_guid = cpu_to_be64(tmp); 7013f89a643SAchiad Shochat 7023f89a643SAchiad Shochat return err; 7031b5daf11SMajd Dibbiny } 7041b5daf11SMajd Dibbiny 7051b5daf11SMajd Dibbiny struct mlx5_reg_node_desc { 706bd99fdeaSYuval Shaia u8 desc[IB_DEVICE_NODE_DESC_MAX]; 7071b5daf11SMajd Dibbiny }; 7081b5daf11SMajd Dibbiny 7091b5daf11SMajd Dibbiny static int mlx5_query_node_desc(struct mlx5_ib_dev *dev, char *node_desc) 7101b5daf11SMajd Dibbiny { 7111b5daf11SMajd Dibbiny struct mlx5_reg_node_desc in; 7121b5daf11SMajd Dibbiny 7131b5daf11SMajd Dibbiny if (mlx5_use_mad_ifc(dev)) 7141b5daf11SMajd Dibbiny return mlx5_query_mad_ifc_node_desc(dev, node_desc); 7151b5daf11SMajd Dibbiny 7161b5daf11SMajd Dibbiny memset(&in, 0, sizeof(in)); 7171b5daf11SMajd Dibbiny 7181b5daf11SMajd Dibbiny return mlx5_core_access_reg(dev->mdev, &in, sizeof(in), node_desc, 7191b5daf11SMajd Dibbiny sizeof(struct mlx5_reg_node_desc), 7201b5daf11SMajd Dibbiny MLX5_REG_NODE_DESC, 0, 0); 7211b5daf11SMajd Dibbiny } 7221b5daf11SMajd Dibbiny 723e126ba97SEli Cohen static int mlx5_ib_query_device(struct ib_device *ibdev, 7242528e33eSMatan Barak struct ib_device_attr *props, 7252528e33eSMatan Barak struct ib_udata *uhw) 726e126ba97SEli Cohen { 727e126ba97SEli Cohen struct mlx5_ib_dev *dev = to_mdev(ibdev); 728938fe83cSSaeed Mahameed struct mlx5_core_dev *mdev = dev->mdev; 729e126ba97SEli Cohen int err = -ENOMEM; 730288c01b7SEli Cohen int max_sq_desc; 731e126ba97SEli Cohen int max_rq_sg; 732e126ba97SEli Cohen int max_sq_sg; 733e0238a6aSSagi Grimberg u64 min_page_size = 1ull << MLX5_CAP_GEN(mdev, log_pg_sz); 73485c7c014SDaniel Jurgens bool raw_support = !mlx5_core_mp_enabled(mdev); 735402ca536SBodong Wang struct mlx5_ib_query_device_resp resp = {}; 736402ca536SBodong Wang size_t resp_len; 737402ca536SBodong Wang u64 max_tso; 738e126ba97SEli Cohen 739402ca536SBodong Wang resp_len = sizeof(resp.comp_mask) + sizeof(resp.response_length); 740402ca536SBodong Wang if (uhw->outlen && uhw->outlen < resp_len) 741402ca536SBodong Wang return -EINVAL; 742402ca536SBodong Wang else 743402ca536SBodong Wang resp.response_length = resp_len; 744402ca536SBodong Wang 745402ca536SBodong Wang if (uhw->inlen && !ib_is_udata_cleared(uhw, 0, uhw->inlen)) 7462528e33eSMatan Barak return -EINVAL; 7472528e33eSMatan Barak 748e126ba97SEli Cohen memset(props, 0, sizeof(*props)); 7491b5daf11SMajd Dibbiny err = mlx5_query_system_image_guid(ibdev, 7501b5daf11SMajd Dibbiny &props->sys_image_guid); 7511b5daf11SMajd Dibbiny if (err) 7521b5daf11SMajd Dibbiny return err; 7531b5daf11SMajd Dibbiny 7541b5daf11SMajd Dibbiny err = mlx5_query_max_pkeys(ibdev, &props->max_pkeys); 7551b5daf11SMajd Dibbiny if (err) 7561b5daf11SMajd Dibbiny return err; 7571b5daf11SMajd Dibbiny 7581b5daf11SMajd Dibbiny err = mlx5_query_vendor_id(ibdev, &props->vendor_id); 7591b5daf11SMajd Dibbiny if (err) 7601b5daf11SMajd Dibbiny return err; 761e126ba97SEli Cohen 7629603b61dSJack Morgenstein props->fw_ver = ((u64)fw_rev_maj(dev->mdev) << 32) | 7639603b61dSJack Morgenstein (fw_rev_min(dev->mdev) << 16) | 7649603b61dSJack Morgenstein fw_rev_sub(dev->mdev); 765e126ba97SEli Cohen props->device_cap_flags = IB_DEVICE_CHANGE_PHY_PORT | 766e126ba97SEli Cohen IB_DEVICE_PORT_ACTIVE_EVENT | 767e126ba97SEli Cohen IB_DEVICE_SYS_IMAGE_GUID | 7681a4c3a3dSEli Cohen IB_DEVICE_RC_RNR_NAK_GEN; 769938fe83cSSaeed Mahameed 770938fe83cSSaeed Mahameed if (MLX5_CAP_GEN(mdev, pkv)) 771e126ba97SEli Cohen props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR; 772938fe83cSSaeed Mahameed if (MLX5_CAP_GEN(mdev, qkv)) 773e126ba97SEli Cohen props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR; 774938fe83cSSaeed Mahameed if (MLX5_CAP_GEN(mdev, apm)) 775e126ba97SEli Cohen props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG; 776938fe83cSSaeed Mahameed if (MLX5_CAP_GEN(mdev, xrc)) 777e126ba97SEli Cohen props->device_cap_flags |= IB_DEVICE_XRC; 778d2370e0aSMatan Barak if (MLX5_CAP_GEN(mdev, imaicl)) { 779d2370e0aSMatan Barak props->device_cap_flags |= IB_DEVICE_MEM_WINDOW | 780d2370e0aSMatan Barak IB_DEVICE_MEM_WINDOW_TYPE_2B; 781d2370e0aSMatan Barak props->max_mw = 1 << MLX5_CAP_GEN(mdev, log_max_mkey); 782b005d316SSagi Grimberg /* We support 'Gappy' memory registration too */ 783b005d316SSagi Grimberg props->device_cap_flags |= IB_DEVICE_SG_GAPS_REG; 784d2370e0aSMatan Barak } 785e126ba97SEli Cohen props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS; 786938fe83cSSaeed Mahameed if (MLX5_CAP_GEN(mdev, sho)) { 7872dea9094SSagi Grimberg props->device_cap_flags |= IB_DEVICE_SIGNATURE_HANDOVER; 7882dea9094SSagi Grimberg /* At this stage no support for signature handover */ 7892dea9094SSagi Grimberg props->sig_prot_cap = IB_PROT_T10DIF_TYPE_1 | 7902dea9094SSagi Grimberg IB_PROT_T10DIF_TYPE_2 | 7912dea9094SSagi Grimberg IB_PROT_T10DIF_TYPE_3; 7922dea9094SSagi Grimberg props->sig_guard_cap = IB_GUARD_T10DIF_CRC | 7932dea9094SSagi Grimberg IB_GUARD_T10DIF_CSUM; 7942dea9094SSagi Grimberg } 795938fe83cSSaeed Mahameed if (MLX5_CAP_GEN(mdev, block_lb_mc)) 796f360d88aSEli Cohen props->device_cap_flags |= IB_DEVICE_BLOCK_MULTICAST_LOOPBACK; 797e126ba97SEli Cohen 79885c7c014SDaniel Jurgens if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) && raw_support) { 799e8161334SNoa Osherovich if (MLX5_CAP_ETH(mdev, csum_cap)) { 800e8161334SNoa Osherovich /* Legacy bit to support old userspace libraries */ 80188115fe7SBodong Wang props->device_cap_flags |= IB_DEVICE_RAW_IP_CSUM; 802e8161334SNoa Osherovich props->raw_packet_caps |= IB_RAW_PACKET_CAP_IP_CSUM; 803e8161334SNoa Osherovich } 804e8161334SNoa Osherovich 805e8161334SNoa Osherovich if (MLX5_CAP_ETH(dev->mdev, vlan_cap)) 806e8161334SNoa Osherovich props->raw_packet_caps |= 807e8161334SNoa Osherovich IB_RAW_PACKET_CAP_CVLAN_STRIPPING; 80888115fe7SBodong Wang 809402ca536SBodong Wang if (field_avail(typeof(resp), tso_caps, uhw->outlen)) { 810402ca536SBodong Wang max_tso = MLX5_CAP_ETH(mdev, max_lso_cap); 811402ca536SBodong Wang if (max_tso) { 812402ca536SBodong Wang resp.tso_caps.max_tso = 1 << max_tso; 813402ca536SBodong Wang resp.tso_caps.supported_qpts |= 814402ca536SBodong Wang 1 << IB_QPT_RAW_PACKET; 815402ca536SBodong Wang resp.response_length += sizeof(resp.tso_caps); 816402ca536SBodong Wang } 817402ca536SBodong Wang } 81831f69a82SYishai Hadas 81931f69a82SYishai Hadas if (field_avail(typeof(resp), rss_caps, uhw->outlen)) { 82031f69a82SYishai Hadas resp.rss_caps.rx_hash_function = 82131f69a82SYishai Hadas MLX5_RX_HASH_FUNC_TOEPLITZ; 82231f69a82SYishai Hadas resp.rss_caps.rx_hash_fields_mask = 82331f69a82SYishai Hadas MLX5_RX_HASH_SRC_IPV4 | 82431f69a82SYishai Hadas MLX5_RX_HASH_DST_IPV4 | 82531f69a82SYishai Hadas MLX5_RX_HASH_SRC_IPV6 | 82631f69a82SYishai Hadas MLX5_RX_HASH_DST_IPV6 | 82731f69a82SYishai Hadas MLX5_RX_HASH_SRC_PORT_TCP | 82831f69a82SYishai Hadas MLX5_RX_HASH_DST_PORT_TCP | 82931f69a82SYishai Hadas MLX5_RX_HASH_SRC_PORT_UDP | 8304e2b53a5SMaor Gottlieb MLX5_RX_HASH_DST_PORT_UDP | 8314e2b53a5SMaor Gottlieb MLX5_RX_HASH_INNER; 8322d93fc85SMatan Barak if (mlx5_accel_ipsec_device_caps(dev->mdev) & 8332d93fc85SMatan Barak MLX5_ACCEL_IPSEC_CAP_DEVICE) 8342d93fc85SMatan Barak resp.rss_caps.rx_hash_fields_mask |= 8352d93fc85SMatan Barak MLX5_RX_HASH_IPSEC_SPI; 83631f69a82SYishai Hadas resp.response_length += sizeof(resp.rss_caps); 83731f69a82SYishai Hadas } 83831f69a82SYishai Hadas } else { 83931f69a82SYishai Hadas if (field_avail(typeof(resp), tso_caps, uhw->outlen)) 84031f69a82SYishai Hadas resp.response_length += sizeof(resp.tso_caps); 84131f69a82SYishai Hadas if (field_avail(typeof(resp), rss_caps, uhw->outlen)) 84231f69a82SYishai Hadas resp.response_length += sizeof(resp.rss_caps); 843402ca536SBodong Wang } 844402ca536SBodong Wang 845f0313965SErez Shitrit if (MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) { 846f0313965SErez Shitrit props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM; 847f0313965SErez Shitrit props->device_cap_flags |= IB_DEVICE_UD_TSO; 848f0313965SErez Shitrit } 849f0313965SErez Shitrit 85003404e8aSMaor Gottlieb if (MLX5_CAP_GEN(dev->mdev, rq_delay_drop) && 85185c7c014SDaniel Jurgens MLX5_CAP_GEN(dev->mdev, general_notification_event) && 85285c7c014SDaniel Jurgens raw_support) 85303404e8aSMaor Gottlieb props->raw_packet_caps |= IB_RAW_PACKET_CAP_DELAY_DROP; 85403404e8aSMaor Gottlieb 8551d54f890SYishai Hadas if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads) && 8561d54f890SYishai Hadas MLX5_CAP_IPOIB_ENHANCED(mdev, csum_cap)) 8571d54f890SYishai Hadas props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM; 8581d54f890SYishai Hadas 859cff5a0f3SMajd Dibbiny if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) && 86085c7c014SDaniel Jurgens MLX5_CAP_ETH(dev->mdev, scatter_fcs) && 86185c7c014SDaniel Jurgens raw_support) { 862e8161334SNoa Osherovich /* Legacy bit to support old userspace libraries */ 863cff5a0f3SMajd Dibbiny props->device_cap_flags |= IB_DEVICE_RAW_SCATTER_FCS; 864e8161334SNoa Osherovich props->raw_packet_caps |= IB_RAW_PACKET_CAP_SCATTER_FCS; 865e8161334SNoa Osherovich } 866cff5a0f3SMajd Dibbiny 86724da0016SAriel Levkovich if (MLX5_CAP_DEV_MEM(mdev, memic)) { 86824da0016SAriel Levkovich props->max_dm_size = 86924da0016SAriel Levkovich MLX5_CAP_DEV_MEM(mdev, max_memic_size); 87024da0016SAriel Levkovich } 87124da0016SAriel Levkovich 872da6d6ba3SMaor Gottlieb if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS)) 873da6d6ba3SMaor Gottlieb props->device_cap_flags |= IB_DEVICE_MANAGED_FLOW_STEERING; 874da6d6ba3SMaor Gottlieb 875b1383aa6SNoa Osherovich if (MLX5_CAP_GEN(mdev, end_pad)) 876b1383aa6SNoa Osherovich props->device_cap_flags |= IB_DEVICE_PCI_WRITE_END_PADDING; 877b1383aa6SNoa Osherovich 8781b5daf11SMajd Dibbiny props->vendor_part_id = mdev->pdev->device; 8791b5daf11SMajd Dibbiny props->hw_ver = mdev->pdev->revision; 880e126ba97SEli Cohen 881e126ba97SEli Cohen props->max_mr_size = ~0ull; 882e0238a6aSSagi Grimberg props->page_size_cap = ~(min_page_size - 1); 883938fe83cSSaeed Mahameed props->max_qp = 1 << MLX5_CAP_GEN(mdev, log_max_qp); 884938fe83cSSaeed Mahameed props->max_qp_wr = 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz); 885938fe83cSSaeed Mahameed max_rq_sg = MLX5_CAP_GEN(mdev, max_wqe_sz_rq) / 886938fe83cSSaeed Mahameed sizeof(struct mlx5_wqe_data_seg); 887288c01b7SEli Cohen max_sq_desc = min_t(int, MLX5_CAP_GEN(mdev, max_wqe_sz_sq), 512); 888288c01b7SEli Cohen max_sq_sg = (max_sq_desc - sizeof(struct mlx5_wqe_ctrl_seg) - 889288c01b7SEli Cohen sizeof(struct mlx5_wqe_raddr_seg)) / 890e126ba97SEli Cohen sizeof(struct mlx5_wqe_data_seg); 89133023fb8SSteve Wise props->max_send_sge = max_sq_sg; 89233023fb8SSteve Wise props->max_recv_sge = max_rq_sg; 893986ef95eSSagi Grimberg props->max_sge_rd = MLX5_MAX_SGE_RD; 894938fe83cSSaeed Mahameed props->max_cq = 1 << MLX5_CAP_GEN(mdev, log_max_cq); 8959f177686SLeon Romanovsky props->max_cqe = (1 << MLX5_CAP_GEN(mdev, log_max_cq_sz)) - 1; 896938fe83cSSaeed Mahameed props->max_mr = 1 << MLX5_CAP_GEN(mdev, log_max_mkey); 897938fe83cSSaeed Mahameed props->max_pd = 1 << MLX5_CAP_GEN(mdev, log_max_pd); 898938fe83cSSaeed Mahameed props->max_qp_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_req_qp); 899938fe83cSSaeed Mahameed props->max_qp_init_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_res_qp); 900938fe83cSSaeed Mahameed props->max_srq = 1 << MLX5_CAP_GEN(mdev, log_max_srq); 901938fe83cSSaeed Mahameed props->max_srq_wr = (1 << MLX5_CAP_GEN(mdev, log_max_srq_sz)) - 1; 902938fe83cSSaeed Mahameed props->local_ca_ack_delay = MLX5_CAP_GEN(mdev, local_ca_ack_delay); 903e126ba97SEli Cohen props->max_res_rd_atom = props->max_qp_rd_atom * props->max_qp; 904e126ba97SEli Cohen props->max_srq_sge = max_rq_sg - 1; 905911f4331SSagi Grimberg props->max_fast_reg_page_list_len = 906911f4331SSagi Grimberg 1 << MLX5_CAP_GEN(mdev, log_max_klm_list_size); 907776a3906SMoni Shoua get_atomic_caps_qp(dev, props); 90881bea28fSEli Cohen props->masked_atomic_cap = IB_ATOMIC_NONE; 909938fe83cSSaeed Mahameed props->max_mcast_grp = 1 << MLX5_CAP_GEN(mdev, log_max_mcg); 910938fe83cSSaeed Mahameed props->max_mcast_qp_attach = MLX5_CAP_GEN(mdev, max_qp_mcg); 911e126ba97SEli Cohen props->max_total_mcast_qp_attach = props->max_mcast_qp_attach * 912e126ba97SEli Cohen props->max_mcast_grp; 913e126ba97SEli Cohen props->max_map_per_fmr = INT_MAX; /* no limit in ConnectIB */ 91486695a65SMaor Gottlieb props->max_ah = INT_MAX; 9157c60bcbbSMatan Barak props->hca_core_clock = MLX5_CAP_GEN(mdev, device_frequency_khz); 9167c60bcbbSMatan Barak props->timestamp_mask = 0x7FFFFFFFFFFFFFFFULL; 917e126ba97SEli Cohen 9188cdd312cSHaggai Eran #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING 919938fe83cSSaeed Mahameed if (MLX5_CAP_GEN(mdev, pg)) 9208cdd312cSHaggai Eran props->device_cap_flags |= IB_DEVICE_ON_DEMAND_PAGING; 9218cdd312cSHaggai Eran props->odp_caps = dev->odp_caps; 9228cdd312cSHaggai Eran #endif 9238cdd312cSHaggai Eran 924051f2630SLeon Romanovsky if (MLX5_CAP_GEN(mdev, cd)) 925051f2630SLeon Romanovsky props->device_cap_flags |= IB_DEVICE_CROSS_CHANNEL; 926051f2630SLeon Romanovsky 927eff901d3SEli Cohen if (!mlx5_core_is_pf(mdev)) 928eff901d3SEli Cohen props->device_cap_flags |= IB_DEVICE_VIRTUAL_FUNCTION; 929eff901d3SEli Cohen 93031f69a82SYishai Hadas if (mlx5_ib_port_link_layer(ibdev, 1) == 93185c7c014SDaniel Jurgens IB_LINK_LAYER_ETHERNET && raw_support) { 93231f69a82SYishai Hadas props->rss_caps.max_rwq_indirection_tables = 93331f69a82SYishai Hadas 1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt); 93431f69a82SYishai Hadas props->rss_caps.max_rwq_indirection_table_size = 93531f69a82SYishai Hadas 1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt_size); 93631f69a82SYishai Hadas props->rss_caps.supported_qpts = 1 << IB_QPT_RAW_PACKET; 93731f69a82SYishai Hadas props->max_wq_type_rq = 93831f69a82SYishai Hadas 1 << MLX5_CAP_GEN(dev->mdev, log_max_rq); 93931f69a82SYishai Hadas } 94031f69a82SYishai Hadas 941eb761894SArtemy Kovalyov if (MLX5_CAP_GEN(mdev, tag_matching)) { 94278b1beb0SLeon Romanovsky props->tm_caps.max_rndv_hdr_size = MLX5_TM_MAX_RNDV_MSG_SIZE; 94378b1beb0SLeon Romanovsky props->tm_caps.max_num_tags = 944eb761894SArtemy Kovalyov (1 << MLX5_CAP_GEN(mdev, log_tag_matching_list_sz)) - 1; 94578b1beb0SLeon Romanovsky props->tm_caps.flags = IB_TM_CAP_RC; 94678b1beb0SLeon Romanovsky props->tm_caps.max_ops = 947eb761894SArtemy Kovalyov 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz); 94878b1beb0SLeon Romanovsky props->tm_caps.max_sge = MLX5_TM_MAX_SGE; 949eb761894SArtemy Kovalyov } 950eb761894SArtemy Kovalyov 95187ab3f52SYonatan Cohen if (MLX5_CAP_GEN(dev->mdev, cq_moderation)) { 95287ab3f52SYonatan Cohen props->cq_caps.max_cq_moderation_count = 95387ab3f52SYonatan Cohen MLX5_MAX_CQ_COUNT; 95487ab3f52SYonatan Cohen props->cq_caps.max_cq_moderation_period = 95587ab3f52SYonatan Cohen MLX5_MAX_CQ_PERIOD; 95687ab3f52SYonatan Cohen } 95787ab3f52SYonatan Cohen 9587e43a2a5SBodong Wang if (field_avail(typeof(resp), cqe_comp_caps, uhw->outlen)) { 959572f46bfSYonatan Cohen resp.response_length += sizeof(resp.cqe_comp_caps); 960572f46bfSYonatan Cohen 961572f46bfSYonatan Cohen if (MLX5_CAP_GEN(dev->mdev, cqe_compression)) { 9627e43a2a5SBodong Wang resp.cqe_comp_caps.max_num = 963572f46bfSYonatan Cohen MLX5_CAP_GEN(dev->mdev, 964572f46bfSYonatan Cohen cqe_compression_max_num); 965572f46bfSYonatan Cohen 9667e43a2a5SBodong Wang resp.cqe_comp_caps.supported_format = 9677e43a2a5SBodong Wang MLX5_IB_CQE_RES_FORMAT_HASH | 9687e43a2a5SBodong Wang MLX5_IB_CQE_RES_FORMAT_CSUM; 9696f1006a4SYonatan Cohen 9706f1006a4SYonatan Cohen if (MLX5_CAP_GEN(dev->mdev, mini_cqe_resp_stride_index)) 9716f1006a4SYonatan Cohen resp.cqe_comp_caps.supported_format |= 9726f1006a4SYonatan Cohen MLX5_IB_CQE_RES_FORMAT_CSUM_STRIDX; 973572f46bfSYonatan Cohen } 9747e43a2a5SBodong Wang } 9757e43a2a5SBodong Wang 97685c7c014SDaniel Jurgens if (field_avail(typeof(resp), packet_pacing_caps, uhw->outlen) && 97785c7c014SDaniel Jurgens raw_support) { 978d949167dSBodong Wang if (MLX5_CAP_QOS(mdev, packet_pacing) && 979d949167dSBodong Wang MLX5_CAP_GEN(mdev, qos)) { 980d949167dSBodong Wang resp.packet_pacing_caps.qp_rate_limit_max = 981d949167dSBodong Wang MLX5_CAP_QOS(mdev, packet_pacing_max_rate); 982d949167dSBodong Wang resp.packet_pacing_caps.qp_rate_limit_min = 983d949167dSBodong Wang MLX5_CAP_QOS(mdev, packet_pacing_min_rate); 984d949167dSBodong Wang resp.packet_pacing_caps.supported_qpts |= 985d949167dSBodong Wang 1 << IB_QPT_RAW_PACKET; 98661147f39SBodong Wang if (MLX5_CAP_QOS(mdev, packet_pacing_burst_bound) && 98761147f39SBodong Wang MLX5_CAP_QOS(mdev, packet_pacing_typical_size)) 98861147f39SBodong Wang resp.packet_pacing_caps.cap_flags |= 98961147f39SBodong Wang MLX5_IB_PP_SUPPORT_BURST; 990d949167dSBodong Wang } 991d949167dSBodong Wang resp.response_length += sizeof(resp.packet_pacing_caps); 992d949167dSBodong Wang } 993d949167dSBodong Wang 9949f885201SLeon Romanovsky if (field_avail(typeof(resp), mlx5_ib_support_multi_pkt_send_wqes, 9959f885201SLeon Romanovsky uhw->outlen)) { 996795b609cSBodong Wang if (MLX5_CAP_ETH(mdev, multi_pkt_send_wqe)) 9979f885201SLeon Romanovsky resp.mlx5_ib_support_multi_pkt_send_wqes = 998795b609cSBodong Wang MLX5_IB_ALLOW_MPW; 999050da902SBodong Wang 1000050da902SBodong Wang if (MLX5_CAP_ETH(mdev, enhanced_multi_pkt_send_wqe)) 1001050da902SBodong Wang resp.mlx5_ib_support_multi_pkt_send_wqes |= 1002050da902SBodong Wang MLX5_IB_SUPPORT_EMPW; 1003050da902SBodong Wang 10049f885201SLeon Romanovsky resp.response_length += 10059f885201SLeon Romanovsky sizeof(resp.mlx5_ib_support_multi_pkt_send_wqes); 10069f885201SLeon Romanovsky } 10079f885201SLeon Romanovsky 1008de57f2adSGuy Levi if (field_avail(typeof(resp), flags, uhw->outlen)) { 1009de57f2adSGuy Levi resp.response_length += sizeof(resp.flags); 10107a0c8f42SGuy Levi 1011de57f2adSGuy Levi if (MLX5_CAP_GEN(mdev, cqe_compression_128)) 1012de57f2adSGuy Levi resp.flags |= 1013de57f2adSGuy Levi MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_COMP; 10147a0c8f42SGuy Levi 10157a0c8f42SGuy Levi if (MLX5_CAP_GEN(mdev, cqe_128_always)) 10167a0c8f42SGuy Levi resp.flags |= MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_PAD; 1017de57f2adSGuy Levi } 10189f885201SLeon Romanovsky 101996dc3fc5SNoa Osherovich if (field_avail(typeof(resp), sw_parsing_caps, 102096dc3fc5SNoa Osherovich uhw->outlen)) { 102196dc3fc5SNoa Osherovich resp.response_length += sizeof(resp.sw_parsing_caps); 102296dc3fc5SNoa Osherovich if (MLX5_CAP_ETH(mdev, swp)) { 102396dc3fc5SNoa Osherovich resp.sw_parsing_caps.sw_parsing_offloads |= 102496dc3fc5SNoa Osherovich MLX5_IB_SW_PARSING; 102596dc3fc5SNoa Osherovich 102696dc3fc5SNoa Osherovich if (MLX5_CAP_ETH(mdev, swp_csum)) 102796dc3fc5SNoa Osherovich resp.sw_parsing_caps.sw_parsing_offloads |= 102896dc3fc5SNoa Osherovich MLX5_IB_SW_PARSING_CSUM; 102996dc3fc5SNoa Osherovich 103096dc3fc5SNoa Osherovich if (MLX5_CAP_ETH(mdev, swp_lso)) 103196dc3fc5SNoa Osherovich resp.sw_parsing_caps.sw_parsing_offloads |= 103296dc3fc5SNoa Osherovich MLX5_IB_SW_PARSING_LSO; 103396dc3fc5SNoa Osherovich 103496dc3fc5SNoa Osherovich if (resp.sw_parsing_caps.sw_parsing_offloads) 103596dc3fc5SNoa Osherovich resp.sw_parsing_caps.supported_qpts = 103696dc3fc5SNoa Osherovich BIT(IB_QPT_RAW_PACKET); 103796dc3fc5SNoa Osherovich } 103896dc3fc5SNoa Osherovich } 103996dc3fc5SNoa Osherovich 104085c7c014SDaniel Jurgens if (field_avail(typeof(resp), striding_rq_caps, uhw->outlen) && 104185c7c014SDaniel Jurgens raw_support) { 1042b4f34597SNoa Osherovich resp.response_length += sizeof(resp.striding_rq_caps); 1043b4f34597SNoa Osherovich if (MLX5_CAP_GEN(mdev, striding_rq)) { 1044b4f34597SNoa Osherovich resp.striding_rq_caps.min_single_stride_log_num_of_bytes = 1045b4f34597SNoa Osherovich MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES; 1046b4f34597SNoa Osherovich resp.striding_rq_caps.max_single_stride_log_num_of_bytes = 1047b4f34597SNoa Osherovich MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES; 1048b4f34597SNoa Osherovich resp.striding_rq_caps.min_single_wqe_log_num_of_strides = 1049b4f34597SNoa Osherovich MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES; 1050b4f34597SNoa Osherovich resp.striding_rq_caps.max_single_wqe_log_num_of_strides = 1051b4f34597SNoa Osherovich MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES; 1052b4f34597SNoa Osherovich resp.striding_rq_caps.supported_qpts = 1053b4f34597SNoa Osherovich BIT(IB_QPT_RAW_PACKET); 1054b4f34597SNoa Osherovich } 1055b4f34597SNoa Osherovich } 1056b4f34597SNoa Osherovich 1057f95ef6cbSMaor Gottlieb if (field_avail(typeof(resp), tunnel_offloads_caps, 1058f95ef6cbSMaor Gottlieb uhw->outlen)) { 1059f95ef6cbSMaor Gottlieb resp.response_length += sizeof(resp.tunnel_offloads_caps); 1060f95ef6cbSMaor Gottlieb if (MLX5_CAP_ETH(mdev, tunnel_stateless_vxlan)) 1061f95ef6cbSMaor Gottlieb resp.tunnel_offloads_caps |= 1062f95ef6cbSMaor Gottlieb MLX5_IB_TUNNELED_OFFLOADS_VXLAN; 1063f95ef6cbSMaor Gottlieb if (MLX5_CAP_ETH(mdev, tunnel_stateless_geneve_rx)) 1064f95ef6cbSMaor Gottlieb resp.tunnel_offloads_caps |= 1065f95ef6cbSMaor Gottlieb MLX5_IB_TUNNELED_OFFLOADS_GENEVE; 1066f95ef6cbSMaor Gottlieb if (MLX5_CAP_ETH(mdev, tunnel_stateless_gre)) 1067f95ef6cbSMaor Gottlieb resp.tunnel_offloads_caps |= 1068f95ef6cbSMaor Gottlieb MLX5_IB_TUNNELED_OFFLOADS_GRE; 1069e818e255SAriel Levkovich if (MLX5_CAP_GEN(mdev, flex_parser_protocols) & 1070e818e255SAriel Levkovich MLX5_FLEX_PROTO_CW_MPLS_GRE) 1071e818e255SAriel Levkovich resp.tunnel_offloads_caps |= 1072e818e255SAriel Levkovich MLX5_IB_TUNNELED_OFFLOADS_MPLS_GRE; 1073e818e255SAriel Levkovich if (MLX5_CAP_GEN(mdev, flex_parser_protocols) & 1074e818e255SAriel Levkovich MLX5_FLEX_PROTO_CW_MPLS_UDP) 1075e818e255SAriel Levkovich resp.tunnel_offloads_caps |= 1076e818e255SAriel Levkovich MLX5_IB_TUNNELED_OFFLOADS_MPLS_UDP; 1077f95ef6cbSMaor Gottlieb } 1078f95ef6cbSMaor Gottlieb 1079402ca536SBodong Wang if (uhw->outlen) { 1080402ca536SBodong Wang err = ib_copy_to_udata(uhw, &resp, resp.response_length); 1081402ca536SBodong Wang 1082402ca536SBodong Wang if (err) 1083402ca536SBodong Wang return err; 1084402ca536SBodong Wang } 1085402ca536SBodong Wang 10861b5daf11SMajd Dibbiny return 0; 10871b5daf11SMajd Dibbiny } 1088e126ba97SEli Cohen 10891b5daf11SMajd Dibbiny enum mlx5_ib_width { 10901b5daf11SMajd Dibbiny MLX5_IB_WIDTH_1X = 1 << 0, 10911b5daf11SMajd Dibbiny MLX5_IB_WIDTH_2X = 1 << 1, 10921b5daf11SMajd Dibbiny MLX5_IB_WIDTH_4X = 1 << 2, 10931b5daf11SMajd Dibbiny MLX5_IB_WIDTH_8X = 1 << 3, 10941b5daf11SMajd Dibbiny MLX5_IB_WIDTH_12X = 1 << 4 10951b5daf11SMajd Dibbiny }; 10961b5daf11SMajd Dibbiny 10971b5daf11SMajd Dibbiny static int translate_active_width(struct ib_device *ibdev, u8 active_width, 10981b5daf11SMajd Dibbiny u8 *ib_width) 10991b5daf11SMajd Dibbiny { 11001b5daf11SMajd Dibbiny struct mlx5_ib_dev *dev = to_mdev(ibdev); 11011b5daf11SMajd Dibbiny int err = 0; 11021b5daf11SMajd Dibbiny 11031b5daf11SMajd Dibbiny if (active_width & MLX5_IB_WIDTH_1X) { 11041b5daf11SMajd Dibbiny *ib_width = IB_WIDTH_1X; 11051b5daf11SMajd Dibbiny } else if (active_width & MLX5_IB_WIDTH_2X) { 11061b5daf11SMajd Dibbiny mlx5_ib_dbg(dev, "active_width %d is not supported by IB spec\n", 11071b5daf11SMajd Dibbiny (int)active_width); 11081b5daf11SMajd Dibbiny err = -EINVAL; 11091b5daf11SMajd Dibbiny } else if (active_width & MLX5_IB_WIDTH_4X) { 11101b5daf11SMajd Dibbiny *ib_width = IB_WIDTH_4X; 11111b5daf11SMajd Dibbiny } else if (active_width & MLX5_IB_WIDTH_8X) { 11121b5daf11SMajd Dibbiny *ib_width = IB_WIDTH_8X; 11131b5daf11SMajd Dibbiny } else if (active_width & MLX5_IB_WIDTH_12X) { 11141b5daf11SMajd Dibbiny *ib_width = IB_WIDTH_12X; 11151b5daf11SMajd Dibbiny } else { 11161b5daf11SMajd Dibbiny mlx5_ib_dbg(dev, "Invalid active_width %d\n", 11171b5daf11SMajd Dibbiny (int)active_width); 11181b5daf11SMajd Dibbiny err = -EINVAL; 11191b5daf11SMajd Dibbiny } 11201b5daf11SMajd Dibbiny 11211b5daf11SMajd Dibbiny return err; 11221b5daf11SMajd Dibbiny } 11231b5daf11SMajd Dibbiny 11241b5daf11SMajd Dibbiny static int mlx5_mtu_to_ib_mtu(int mtu) 11251b5daf11SMajd Dibbiny { 11261b5daf11SMajd Dibbiny switch (mtu) { 11271b5daf11SMajd Dibbiny case 256: return 1; 11281b5daf11SMajd Dibbiny case 512: return 2; 11291b5daf11SMajd Dibbiny case 1024: return 3; 11301b5daf11SMajd Dibbiny case 2048: return 4; 11311b5daf11SMajd Dibbiny case 4096: return 5; 11321b5daf11SMajd Dibbiny default: 11331b5daf11SMajd Dibbiny pr_warn("invalid mtu\n"); 11341b5daf11SMajd Dibbiny return -1; 11351b5daf11SMajd Dibbiny } 11361b5daf11SMajd Dibbiny } 11371b5daf11SMajd Dibbiny 11381b5daf11SMajd Dibbiny enum ib_max_vl_num { 11391b5daf11SMajd Dibbiny __IB_MAX_VL_0 = 1, 11401b5daf11SMajd Dibbiny __IB_MAX_VL_0_1 = 2, 11411b5daf11SMajd Dibbiny __IB_MAX_VL_0_3 = 3, 11421b5daf11SMajd Dibbiny __IB_MAX_VL_0_7 = 4, 11431b5daf11SMajd Dibbiny __IB_MAX_VL_0_14 = 5, 11441b5daf11SMajd Dibbiny }; 11451b5daf11SMajd Dibbiny 11461b5daf11SMajd Dibbiny enum mlx5_vl_hw_cap { 11471b5daf11SMajd Dibbiny MLX5_VL_HW_0 = 1, 11481b5daf11SMajd Dibbiny MLX5_VL_HW_0_1 = 2, 11491b5daf11SMajd Dibbiny MLX5_VL_HW_0_2 = 3, 11501b5daf11SMajd Dibbiny MLX5_VL_HW_0_3 = 4, 11511b5daf11SMajd Dibbiny MLX5_VL_HW_0_4 = 5, 11521b5daf11SMajd Dibbiny MLX5_VL_HW_0_5 = 6, 11531b5daf11SMajd Dibbiny MLX5_VL_HW_0_6 = 7, 11541b5daf11SMajd Dibbiny MLX5_VL_HW_0_7 = 8, 11551b5daf11SMajd Dibbiny MLX5_VL_HW_0_14 = 15 11561b5daf11SMajd Dibbiny }; 11571b5daf11SMajd Dibbiny 11581b5daf11SMajd Dibbiny static int translate_max_vl_num(struct ib_device *ibdev, u8 vl_hw_cap, 11591b5daf11SMajd Dibbiny u8 *max_vl_num) 11601b5daf11SMajd Dibbiny { 11611b5daf11SMajd Dibbiny switch (vl_hw_cap) { 11621b5daf11SMajd Dibbiny case MLX5_VL_HW_0: 11631b5daf11SMajd Dibbiny *max_vl_num = __IB_MAX_VL_0; 11641b5daf11SMajd Dibbiny break; 11651b5daf11SMajd Dibbiny case MLX5_VL_HW_0_1: 11661b5daf11SMajd Dibbiny *max_vl_num = __IB_MAX_VL_0_1; 11671b5daf11SMajd Dibbiny break; 11681b5daf11SMajd Dibbiny case MLX5_VL_HW_0_3: 11691b5daf11SMajd Dibbiny *max_vl_num = __IB_MAX_VL_0_3; 11701b5daf11SMajd Dibbiny break; 11711b5daf11SMajd Dibbiny case MLX5_VL_HW_0_7: 11721b5daf11SMajd Dibbiny *max_vl_num = __IB_MAX_VL_0_7; 11731b5daf11SMajd Dibbiny break; 11741b5daf11SMajd Dibbiny case MLX5_VL_HW_0_14: 11751b5daf11SMajd Dibbiny *max_vl_num = __IB_MAX_VL_0_14; 11761b5daf11SMajd Dibbiny break; 11771b5daf11SMajd Dibbiny 11781b5daf11SMajd Dibbiny default: 11791b5daf11SMajd Dibbiny return -EINVAL; 11801b5daf11SMajd Dibbiny } 11811b5daf11SMajd Dibbiny 11821b5daf11SMajd Dibbiny return 0; 11831b5daf11SMajd Dibbiny } 11841b5daf11SMajd Dibbiny 11851b5daf11SMajd Dibbiny static int mlx5_query_hca_port(struct ib_device *ibdev, u8 port, 11861b5daf11SMajd Dibbiny struct ib_port_attr *props) 11871b5daf11SMajd Dibbiny { 11881b5daf11SMajd Dibbiny struct mlx5_ib_dev *dev = to_mdev(ibdev); 11891b5daf11SMajd Dibbiny struct mlx5_core_dev *mdev = dev->mdev; 11901b5daf11SMajd Dibbiny struct mlx5_hca_vport_context *rep; 1191046339eaSSaeed Mahameed u16 max_mtu; 1192046339eaSSaeed Mahameed u16 oper_mtu; 11931b5daf11SMajd Dibbiny int err; 11941b5daf11SMajd Dibbiny u8 ib_link_width_oper; 11951b5daf11SMajd Dibbiny u8 vl_hw_cap; 11961b5daf11SMajd Dibbiny 11971b5daf11SMajd Dibbiny rep = kzalloc(sizeof(*rep), GFP_KERNEL); 11981b5daf11SMajd Dibbiny if (!rep) { 11991b5daf11SMajd Dibbiny err = -ENOMEM; 12001b5daf11SMajd Dibbiny goto out; 12011b5daf11SMajd Dibbiny } 12021b5daf11SMajd Dibbiny 1203c4550c63SOr Gerlitz /* props being zeroed by the caller, avoid zeroing it here */ 12041b5daf11SMajd Dibbiny 12051b5daf11SMajd Dibbiny err = mlx5_query_hca_vport_context(mdev, 0, port, 0, rep); 12061b5daf11SMajd Dibbiny if (err) 12071b5daf11SMajd Dibbiny goto out; 12081b5daf11SMajd Dibbiny 12091b5daf11SMajd Dibbiny props->lid = rep->lid; 12101b5daf11SMajd Dibbiny props->lmc = rep->lmc; 12111b5daf11SMajd Dibbiny props->sm_lid = rep->sm_lid; 12121b5daf11SMajd Dibbiny props->sm_sl = rep->sm_sl; 12131b5daf11SMajd Dibbiny props->state = rep->vport_state; 12141b5daf11SMajd Dibbiny props->phys_state = rep->port_physical_state; 12151b5daf11SMajd Dibbiny props->port_cap_flags = rep->cap_mask1; 12161b5daf11SMajd Dibbiny props->gid_tbl_len = mlx5_get_gid_table_len(MLX5_CAP_GEN(mdev, gid_table_size)); 12171b5daf11SMajd Dibbiny props->max_msg_sz = 1 << MLX5_CAP_GEN(mdev, log_max_msg); 12181b5daf11SMajd Dibbiny props->pkey_tbl_len = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, pkey_table_size)); 12191b5daf11SMajd Dibbiny props->bad_pkey_cntr = rep->pkey_violation_counter; 12201b5daf11SMajd Dibbiny props->qkey_viol_cntr = rep->qkey_violation_counter; 12211b5daf11SMajd Dibbiny props->subnet_timeout = rep->subnet_timeout; 12221b5daf11SMajd Dibbiny props->init_type_reply = rep->init_type_reply; 1223eff901d3SEli Cohen props->grh_required = rep->grh_required; 12241b5daf11SMajd Dibbiny 12251b5daf11SMajd Dibbiny err = mlx5_query_port_link_width_oper(mdev, &ib_link_width_oper, port); 12261b5daf11SMajd Dibbiny if (err) 12271b5daf11SMajd Dibbiny goto out; 12281b5daf11SMajd Dibbiny 12291b5daf11SMajd Dibbiny err = translate_active_width(ibdev, ib_link_width_oper, 12301b5daf11SMajd Dibbiny &props->active_width); 12311b5daf11SMajd Dibbiny if (err) 12321b5daf11SMajd Dibbiny goto out; 1233d5beb7f2SNoa Osherovich err = mlx5_query_port_ib_proto_oper(mdev, &props->active_speed, port); 12341b5daf11SMajd Dibbiny if (err) 12351b5daf11SMajd Dibbiny goto out; 12361b5daf11SMajd Dibbiny 1237facc9699SSaeed Mahameed mlx5_query_port_max_mtu(mdev, &max_mtu, port); 12381b5daf11SMajd Dibbiny 12391b5daf11SMajd Dibbiny props->max_mtu = mlx5_mtu_to_ib_mtu(max_mtu); 12401b5daf11SMajd Dibbiny 1241facc9699SSaeed Mahameed mlx5_query_port_oper_mtu(mdev, &oper_mtu, port); 12421b5daf11SMajd Dibbiny 12431b5daf11SMajd Dibbiny props->active_mtu = mlx5_mtu_to_ib_mtu(oper_mtu); 12441b5daf11SMajd Dibbiny 12451b5daf11SMajd Dibbiny err = mlx5_query_port_vl_hw_cap(mdev, &vl_hw_cap, port); 12461b5daf11SMajd Dibbiny if (err) 12471b5daf11SMajd Dibbiny goto out; 12481b5daf11SMajd Dibbiny 12491b5daf11SMajd Dibbiny err = translate_max_vl_num(ibdev, vl_hw_cap, 12501b5daf11SMajd Dibbiny &props->max_vl_num); 12511b5daf11SMajd Dibbiny out: 12521b5daf11SMajd Dibbiny kfree(rep); 1253e126ba97SEli Cohen return err; 1254e126ba97SEli Cohen } 1255e126ba97SEli Cohen 1256e126ba97SEli Cohen int mlx5_ib_query_port(struct ib_device *ibdev, u8 port, 1257e126ba97SEli Cohen struct ib_port_attr *props) 1258e126ba97SEli Cohen { 1259095b0927SIlan Tayari unsigned int count; 1260095b0927SIlan Tayari int ret; 1261095b0927SIlan Tayari 12621b5daf11SMajd Dibbiny switch (mlx5_get_vport_access_method(ibdev)) { 12631b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_MAD: 1264095b0927SIlan Tayari ret = mlx5_query_mad_ifc_port(ibdev, port, props); 1265095b0927SIlan Tayari break; 1266e126ba97SEli Cohen 12671b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_HCA: 1268095b0927SIlan Tayari ret = mlx5_query_hca_port(ibdev, port, props); 1269095b0927SIlan Tayari break; 12701b5daf11SMajd Dibbiny 12713f89a643SAchiad Shochat case MLX5_VPORT_ACCESS_METHOD_NIC: 1272095b0927SIlan Tayari ret = mlx5_query_port_roce(ibdev, port, props); 1273095b0927SIlan Tayari break; 12743f89a643SAchiad Shochat 12751b5daf11SMajd Dibbiny default: 1276095b0927SIlan Tayari ret = -EINVAL; 1277e126ba97SEli Cohen } 1278095b0927SIlan Tayari 1279095b0927SIlan Tayari if (!ret && props) { 1280b3cbd6f0SDaniel Jurgens struct mlx5_ib_dev *dev = to_mdev(ibdev); 1281b3cbd6f0SDaniel Jurgens struct mlx5_core_dev *mdev; 1282b3cbd6f0SDaniel Jurgens bool put_mdev = true; 1283b3cbd6f0SDaniel Jurgens 1284b3cbd6f0SDaniel Jurgens mdev = mlx5_ib_get_native_port_mdev(dev, port, NULL); 1285b3cbd6f0SDaniel Jurgens if (!mdev) { 1286b3cbd6f0SDaniel Jurgens /* If the port isn't affiliated yet query the master. 1287b3cbd6f0SDaniel Jurgens * The master and slave will have the same values. 1288b3cbd6f0SDaniel Jurgens */ 1289b3cbd6f0SDaniel Jurgens mdev = dev->mdev; 1290b3cbd6f0SDaniel Jurgens port = 1; 1291b3cbd6f0SDaniel Jurgens put_mdev = false; 1292b3cbd6f0SDaniel Jurgens } 1293b3cbd6f0SDaniel Jurgens count = mlx5_core_reserved_gids_count(mdev); 1294b3cbd6f0SDaniel Jurgens if (put_mdev) 1295b3cbd6f0SDaniel Jurgens mlx5_ib_put_native_port_mdev(dev, port); 1296095b0927SIlan Tayari props->gid_tbl_len -= count; 1297095b0927SIlan Tayari } 1298095b0927SIlan Tayari return ret; 1299e126ba97SEli Cohen } 1300e126ba97SEli Cohen 13018e6efa3aSMark Bloch static int mlx5_ib_rep_query_port(struct ib_device *ibdev, u8 port, 13028e6efa3aSMark Bloch struct ib_port_attr *props) 13038e6efa3aSMark Bloch { 13048e6efa3aSMark Bloch int ret; 13058e6efa3aSMark Bloch 13068e6efa3aSMark Bloch /* Only link layer == ethernet is valid for representors */ 13078e6efa3aSMark Bloch ret = mlx5_query_port_roce(ibdev, port, props); 13088e6efa3aSMark Bloch if (ret || !props) 13098e6efa3aSMark Bloch return ret; 13108e6efa3aSMark Bloch 13118e6efa3aSMark Bloch /* We don't support GIDS */ 13128e6efa3aSMark Bloch props->gid_tbl_len = 0; 13138e6efa3aSMark Bloch 13148e6efa3aSMark Bloch return ret; 13158e6efa3aSMark Bloch } 13168e6efa3aSMark Bloch 1317e126ba97SEli Cohen static int mlx5_ib_query_gid(struct ib_device *ibdev, u8 port, int index, 1318e126ba97SEli Cohen union ib_gid *gid) 1319e126ba97SEli Cohen { 13201b5daf11SMajd Dibbiny struct mlx5_ib_dev *dev = to_mdev(ibdev); 13211b5daf11SMajd Dibbiny struct mlx5_core_dev *mdev = dev->mdev; 1322e126ba97SEli Cohen 13231b5daf11SMajd Dibbiny switch (mlx5_get_vport_access_method(ibdev)) { 13241b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_MAD: 13251b5daf11SMajd Dibbiny return mlx5_query_mad_ifc_gids(ibdev, port, index, gid); 1326e126ba97SEli Cohen 13271b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_HCA: 13281b5daf11SMajd Dibbiny return mlx5_query_hca_vport_gid(mdev, 0, port, 0, index, gid); 1329e126ba97SEli Cohen 13301b5daf11SMajd Dibbiny default: 13311b5daf11SMajd Dibbiny return -EINVAL; 13321b5daf11SMajd Dibbiny } 1333e126ba97SEli Cohen 1334e126ba97SEli Cohen } 1335e126ba97SEli Cohen 1336b3cbd6f0SDaniel Jurgens static int mlx5_query_hca_nic_pkey(struct ib_device *ibdev, u8 port, 1337b3cbd6f0SDaniel Jurgens u16 index, u16 *pkey) 1338b3cbd6f0SDaniel Jurgens { 1339b3cbd6f0SDaniel Jurgens struct mlx5_ib_dev *dev = to_mdev(ibdev); 1340b3cbd6f0SDaniel Jurgens struct mlx5_core_dev *mdev; 1341b3cbd6f0SDaniel Jurgens bool put_mdev = true; 1342b3cbd6f0SDaniel Jurgens u8 mdev_port_num; 1343b3cbd6f0SDaniel Jurgens int err; 1344b3cbd6f0SDaniel Jurgens 1345b3cbd6f0SDaniel Jurgens mdev = mlx5_ib_get_native_port_mdev(dev, port, &mdev_port_num); 1346b3cbd6f0SDaniel Jurgens if (!mdev) { 1347b3cbd6f0SDaniel Jurgens /* The port isn't affiliated yet, get the PKey from the master 1348b3cbd6f0SDaniel Jurgens * port. For RoCE the PKey tables will be the same. 1349b3cbd6f0SDaniel Jurgens */ 1350b3cbd6f0SDaniel Jurgens put_mdev = false; 1351b3cbd6f0SDaniel Jurgens mdev = dev->mdev; 1352b3cbd6f0SDaniel Jurgens mdev_port_num = 1; 1353b3cbd6f0SDaniel Jurgens } 1354b3cbd6f0SDaniel Jurgens 1355b3cbd6f0SDaniel Jurgens err = mlx5_query_hca_vport_pkey(mdev, 0, mdev_port_num, 0, 1356b3cbd6f0SDaniel Jurgens index, pkey); 1357b3cbd6f0SDaniel Jurgens if (put_mdev) 1358b3cbd6f0SDaniel Jurgens mlx5_ib_put_native_port_mdev(dev, port); 1359b3cbd6f0SDaniel Jurgens 1360b3cbd6f0SDaniel Jurgens return err; 1361b3cbd6f0SDaniel Jurgens } 1362b3cbd6f0SDaniel Jurgens 1363e126ba97SEli Cohen static int mlx5_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index, 1364e126ba97SEli Cohen u16 *pkey) 1365e126ba97SEli Cohen { 13661b5daf11SMajd Dibbiny switch (mlx5_get_vport_access_method(ibdev)) { 13671b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_MAD: 13681b5daf11SMajd Dibbiny return mlx5_query_mad_ifc_pkey(ibdev, port, index, pkey); 1369e126ba97SEli Cohen 13701b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_HCA: 13711b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_NIC: 1372b3cbd6f0SDaniel Jurgens return mlx5_query_hca_nic_pkey(ibdev, port, index, pkey); 13731b5daf11SMajd Dibbiny default: 13741b5daf11SMajd Dibbiny return -EINVAL; 1375e126ba97SEli Cohen } 13761b5daf11SMajd Dibbiny } 1377e126ba97SEli Cohen 1378e126ba97SEli Cohen static int mlx5_ib_modify_device(struct ib_device *ibdev, int mask, 1379e126ba97SEli Cohen struct ib_device_modify *props) 1380e126ba97SEli Cohen { 1381e126ba97SEli Cohen struct mlx5_ib_dev *dev = to_mdev(ibdev); 1382e126ba97SEli Cohen struct mlx5_reg_node_desc in; 1383e126ba97SEli Cohen struct mlx5_reg_node_desc out; 1384e126ba97SEli Cohen int err; 1385e126ba97SEli Cohen 1386e126ba97SEli Cohen if (mask & ~IB_DEVICE_MODIFY_NODE_DESC) 1387e126ba97SEli Cohen return -EOPNOTSUPP; 1388e126ba97SEli Cohen 1389e126ba97SEli Cohen if (!(mask & IB_DEVICE_MODIFY_NODE_DESC)) 1390e126ba97SEli Cohen return 0; 1391e126ba97SEli Cohen 1392e126ba97SEli Cohen /* 1393e126ba97SEli Cohen * If possible, pass node desc to FW, so it can generate 1394e126ba97SEli Cohen * a 144 trap. If cmd fails, just ignore. 1395e126ba97SEli Cohen */ 1396bd99fdeaSYuval Shaia memcpy(&in, props->node_desc, IB_DEVICE_NODE_DESC_MAX); 13979603b61dSJack Morgenstein err = mlx5_core_access_reg(dev->mdev, &in, sizeof(in), &out, 1398e126ba97SEli Cohen sizeof(out), MLX5_REG_NODE_DESC, 0, 1); 1399e126ba97SEli Cohen if (err) 1400e126ba97SEli Cohen return err; 1401e126ba97SEli Cohen 1402bd99fdeaSYuval Shaia memcpy(ibdev->node_desc, props->node_desc, IB_DEVICE_NODE_DESC_MAX); 1403e126ba97SEli Cohen 1404e126ba97SEli Cohen return err; 1405e126ba97SEli Cohen } 1406e126ba97SEli Cohen 1407cdbe33d0SEli Cohen static int set_port_caps_atomic(struct mlx5_ib_dev *dev, u8 port_num, u32 mask, 1408cdbe33d0SEli Cohen u32 value) 1409cdbe33d0SEli Cohen { 1410cdbe33d0SEli Cohen struct mlx5_hca_vport_context ctx = {}; 1411b3cbd6f0SDaniel Jurgens struct mlx5_core_dev *mdev; 1412b3cbd6f0SDaniel Jurgens u8 mdev_port_num; 1413cdbe33d0SEli Cohen int err; 1414cdbe33d0SEli Cohen 1415b3cbd6f0SDaniel Jurgens mdev = mlx5_ib_get_native_port_mdev(dev, port_num, &mdev_port_num); 1416b3cbd6f0SDaniel Jurgens if (!mdev) 1417b3cbd6f0SDaniel Jurgens return -ENODEV; 1418b3cbd6f0SDaniel Jurgens 1419b3cbd6f0SDaniel Jurgens err = mlx5_query_hca_vport_context(mdev, 0, mdev_port_num, 0, &ctx); 1420cdbe33d0SEli Cohen if (err) 1421b3cbd6f0SDaniel Jurgens goto out; 1422cdbe33d0SEli Cohen 1423cdbe33d0SEli Cohen if (~ctx.cap_mask1_perm & mask) { 1424cdbe33d0SEli Cohen mlx5_ib_warn(dev, "trying to change bitmask 0x%X but change supported 0x%X\n", 1425cdbe33d0SEli Cohen mask, ctx.cap_mask1_perm); 1426b3cbd6f0SDaniel Jurgens err = -EINVAL; 1427b3cbd6f0SDaniel Jurgens goto out; 1428cdbe33d0SEli Cohen } 1429cdbe33d0SEli Cohen 1430cdbe33d0SEli Cohen ctx.cap_mask1 = value; 1431cdbe33d0SEli Cohen ctx.cap_mask1_perm = mask; 1432b3cbd6f0SDaniel Jurgens err = mlx5_core_modify_hca_vport_context(mdev, 0, mdev_port_num, 1433b3cbd6f0SDaniel Jurgens 0, &ctx); 1434b3cbd6f0SDaniel Jurgens 1435b3cbd6f0SDaniel Jurgens out: 1436b3cbd6f0SDaniel Jurgens mlx5_ib_put_native_port_mdev(dev, port_num); 1437cdbe33d0SEli Cohen 1438cdbe33d0SEli Cohen return err; 1439cdbe33d0SEli Cohen } 1440cdbe33d0SEli Cohen 1441e126ba97SEli Cohen static int mlx5_ib_modify_port(struct ib_device *ibdev, u8 port, int mask, 1442e126ba97SEli Cohen struct ib_port_modify *props) 1443e126ba97SEli Cohen { 1444e126ba97SEli Cohen struct mlx5_ib_dev *dev = to_mdev(ibdev); 1445e126ba97SEli Cohen struct ib_port_attr attr; 1446e126ba97SEli Cohen u32 tmp; 1447e126ba97SEli Cohen int err; 1448cdbe33d0SEli Cohen u32 change_mask; 1449cdbe33d0SEli Cohen u32 value; 1450cdbe33d0SEli Cohen bool is_ib = (mlx5_ib_port_link_layer(ibdev, port) == 1451cdbe33d0SEli Cohen IB_LINK_LAYER_INFINIBAND); 1452cdbe33d0SEli Cohen 1453ec255879SMajd Dibbiny /* CM layer calls ib_modify_port() regardless of the link layer. For 1454ec255879SMajd Dibbiny * Ethernet ports, qkey violation and Port capabilities are meaningless. 1455ec255879SMajd Dibbiny */ 1456ec255879SMajd Dibbiny if (!is_ib) 1457ec255879SMajd Dibbiny return 0; 1458ec255879SMajd Dibbiny 1459cdbe33d0SEli Cohen if (MLX5_CAP_GEN(dev->mdev, ib_virt) && is_ib) { 1460cdbe33d0SEli Cohen change_mask = props->clr_port_cap_mask | props->set_port_cap_mask; 1461cdbe33d0SEli Cohen value = ~props->clr_port_cap_mask | props->set_port_cap_mask; 1462cdbe33d0SEli Cohen return set_port_caps_atomic(dev, port, change_mask, value); 1463cdbe33d0SEli Cohen } 1464e126ba97SEli Cohen 1465e126ba97SEli Cohen mutex_lock(&dev->cap_mask_mutex); 1466e126ba97SEli Cohen 1467c4550c63SOr Gerlitz err = ib_query_port(ibdev, port, &attr); 1468e126ba97SEli Cohen if (err) 1469e126ba97SEli Cohen goto out; 1470e126ba97SEli Cohen 1471e126ba97SEli Cohen tmp = (attr.port_cap_flags | props->set_port_cap_mask) & 1472e126ba97SEli Cohen ~props->clr_port_cap_mask; 1473e126ba97SEli Cohen 14749603b61dSJack Morgenstein err = mlx5_set_port_caps(dev->mdev, port, tmp); 1475e126ba97SEli Cohen 1476e126ba97SEli Cohen out: 1477e126ba97SEli Cohen mutex_unlock(&dev->cap_mask_mutex); 1478e126ba97SEli Cohen return err; 1479e126ba97SEli Cohen } 1480e126ba97SEli Cohen 148130aa60b3SEli Cohen static void print_lib_caps(struct mlx5_ib_dev *dev, u64 caps) 148230aa60b3SEli Cohen { 148330aa60b3SEli Cohen mlx5_ib_dbg(dev, "MLX5_LIB_CAP_4K_UAR = %s\n", 148430aa60b3SEli Cohen caps & MLX5_LIB_CAP_4K_UAR ? "y" : "n"); 148530aa60b3SEli Cohen } 148630aa60b3SEli Cohen 148731a78a5aSYishai Hadas static u16 calc_dynamic_bfregs(int uars_per_sys_page) 148831a78a5aSYishai Hadas { 148931a78a5aSYishai Hadas /* Large page with non 4k uar support might limit the dynamic size */ 149031a78a5aSYishai Hadas if (uars_per_sys_page == 1 && PAGE_SIZE > 4096) 149131a78a5aSYishai Hadas return MLX5_MIN_DYN_BFREGS; 149231a78a5aSYishai Hadas 149331a78a5aSYishai Hadas return MLX5_MAX_DYN_BFREGS; 149431a78a5aSYishai Hadas } 149531a78a5aSYishai Hadas 1496b037c29aSEli Cohen static int calc_total_bfregs(struct mlx5_ib_dev *dev, bool lib_uar_4k, 1497b037c29aSEli Cohen struct mlx5_ib_alloc_ucontext_req_v2 *req, 149831a78a5aSYishai Hadas struct mlx5_bfreg_info *bfregi) 1499b037c29aSEli Cohen { 1500b037c29aSEli Cohen int uars_per_sys_page; 1501b037c29aSEli Cohen int bfregs_per_sys_page; 1502b037c29aSEli Cohen int ref_bfregs = req->total_num_bfregs; 1503b037c29aSEli Cohen 1504b037c29aSEli Cohen if (req->total_num_bfregs == 0) 1505b037c29aSEli Cohen return -EINVAL; 1506b037c29aSEli Cohen 1507b037c29aSEli Cohen BUILD_BUG_ON(MLX5_MAX_BFREGS % MLX5_NON_FP_BFREGS_IN_PAGE); 1508b037c29aSEli Cohen BUILD_BUG_ON(MLX5_MAX_BFREGS < MLX5_NON_FP_BFREGS_IN_PAGE); 1509b037c29aSEli Cohen 1510b037c29aSEli Cohen if (req->total_num_bfregs > MLX5_MAX_BFREGS) 1511b037c29aSEli Cohen return -ENOMEM; 1512b037c29aSEli Cohen 1513b037c29aSEli Cohen uars_per_sys_page = get_uars_per_sys_page(dev, lib_uar_4k); 1514b037c29aSEli Cohen bfregs_per_sys_page = uars_per_sys_page * MLX5_NON_FP_BFREGS_PER_UAR; 151531a78a5aSYishai Hadas /* This holds the required static allocation asked by the user */ 1516b037c29aSEli Cohen req->total_num_bfregs = ALIGN(req->total_num_bfregs, bfregs_per_sys_page); 1517b037c29aSEli Cohen if (req->num_low_latency_bfregs > req->total_num_bfregs - 1) 1518b037c29aSEli Cohen return -EINVAL; 1519b037c29aSEli Cohen 152031a78a5aSYishai Hadas bfregi->num_static_sys_pages = req->total_num_bfregs / bfregs_per_sys_page; 152131a78a5aSYishai Hadas bfregi->num_dyn_bfregs = ALIGN(calc_dynamic_bfregs(uars_per_sys_page), bfregs_per_sys_page); 152231a78a5aSYishai Hadas bfregi->total_num_bfregs = req->total_num_bfregs + bfregi->num_dyn_bfregs; 152331a78a5aSYishai Hadas bfregi->num_sys_pages = bfregi->total_num_bfregs / bfregs_per_sys_page; 152431a78a5aSYishai Hadas 152531a78a5aSYishai 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", 1526b037c29aSEli Cohen MLX5_CAP_GEN(dev->mdev, uar_4k) ? "yes" : "no", 1527b037c29aSEli Cohen lib_uar_4k ? "yes" : "no", ref_bfregs, 152831a78a5aSYishai Hadas req->total_num_bfregs, bfregi->total_num_bfregs, 152931a78a5aSYishai Hadas bfregi->num_sys_pages); 1530b037c29aSEli Cohen 1531b037c29aSEli Cohen return 0; 1532b037c29aSEli Cohen } 1533b037c29aSEli Cohen 1534b037c29aSEli Cohen static int allocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context) 1535b037c29aSEli Cohen { 1536b037c29aSEli Cohen struct mlx5_bfreg_info *bfregi; 1537b037c29aSEli Cohen int err; 1538b037c29aSEli Cohen int i; 1539b037c29aSEli Cohen 1540b037c29aSEli Cohen bfregi = &context->bfregi; 154131a78a5aSYishai Hadas for (i = 0; i < bfregi->num_static_sys_pages; i++) { 1542b037c29aSEli Cohen err = mlx5_cmd_alloc_uar(dev->mdev, &bfregi->sys_pages[i]); 1543b037c29aSEli Cohen if (err) 1544b037c29aSEli Cohen goto error; 1545b037c29aSEli Cohen 1546b037c29aSEli Cohen mlx5_ib_dbg(dev, "allocated uar %d\n", bfregi->sys_pages[i]); 1547b037c29aSEli Cohen } 15484ed131d0SYishai Hadas 15494ed131d0SYishai Hadas for (i = bfregi->num_static_sys_pages; i < bfregi->num_sys_pages; i++) 15504ed131d0SYishai Hadas bfregi->sys_pages[i] = MLX5_IB_INVALID_UAR_INDEX; 15514ed131d0SYishai Hadas 1552b037c29aSEli Cohen return 0; 1553b037c29aSEli Cohen 1554b037c29aSEli Cohen error: 1555b037c29aSEli Cohen for (--i; i >= 0; i--) 1556b037c29aSEli Cohen if (mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i])) 1557b037c29aSEli Cohen mlx5_ib_warn(dev, "failed to free uar %d\n", i); 1558b037c29aSEli Cohen 1559b037c29aSEli Cohen return err; 1560b037c29aSEli Cohen } 1561b037c29aSEli Cohen 156215177999SLeon Romanovsky static void deallocate_uars(struct mlx5_ib_dev *dev, 156315177999SLeon Romanovsky struct mlx5_ib_ucontext *context) 1564b037c29aSEli Cohen { 1565b037c29aSEli Cohen struct mlx5_bfreg_info *bfregi; 1566b037c29aSEli Cohen int i; 1567b037c29aSEli Cohen 1568b037c29aSEli Cohen bfregi = &context->bfregi; 156915177999SLeon Romanovsky for (i = 0; i < bfregi->num_sys_pages; i++) 15704ed131d0SYishai Hadas if (i < bfregi->num_static_sys_pages || 157115177999SLeon Romanovsky bfregi->sys_pages[i] != MLX5_IB_INVALID_UAR_INDEX) 157215177999SLeon Romanovsky mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]); 1573b037c29aSEli Cohen } 1574b037c29aSEli Cohen 1575c85023e1SHuy Nguyen static int mlx5_ib_alloc_transport_domain(struct mlx5_ib_dev *dev, u32 *tdn) 1576c85023e1SHuy Nguyen { 1577c85023e1SHuy Nguyen int err; 1578c85023e1SHuy Nguyen 1579cfdeb893SLeon Romanovsky if (!MLX5_CAP_GEN(dev->mdev, log_max_transport_domain)) 1580cfdeb893SLeon Romanovsky return 0; 1581cfdeb893SLeon Romanovsky 1582c85023e1SHuy Nguyen err = mlx5_core_alloc_transport_domain(dev->mdev, tdn); 1583c85023e1SHuy Nguyen if (err) 1584c85023e1SHuy Nguyen return err; 1585c85023e1SHuy Nguyen 1586c85023e1SHuy Nguyen if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) || 15878978cc92SEran Ben Elisha (!MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) && 15888978cc92SEran Ben Elisha !MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc))) 1589c85023e1SHuy Nguyen return err; 1590c85023e1SHuy Nguyen 1591c85023e1SHuy Nguyen mutex_lock(&dev->lb_mutex); 1592c85023e1SHuy Nguyen dev->user_td++; 1593c85023e1SHuy Nguyen 1594c85023e1SHuy Nguyen if (dev->user_td == 2) 1595c85023e1SHuy Nguyen err = mlx5_nic_vport_update_local_lb(dev->mdev, true); 1596c85023e1SHuy Nguyen 1597c85023e1SHuy Nguyen mutex_unlock(&dev->lb_mutex); 1598c85023e1SHuy Nguyen return err; 1599c85023e1SHuy Nguyen } 1600c85023e1SHuy Nguyen 1601c85023e1SHuy Nguyen static void mlx5_ib_dealloc_transport_domain(struct mlx5_ib_dev *dev, u32 tdn) 1602c85023e1SHuy Nguyen { 1603cfdeb893SLeon Romanovsky if (!MLX5_CAP_GEN(dev->mdev, log_max_transport_domain)) 1604cfdeb893SLeon Romanovsky return; 1605cfdeb893SLeon Romanovsky 1606c85023e1SHuy Nguyen mlx5_core_dealloc_transport_domain(dev->mdev, tdn); 1607c85023e1SHuy Nguyen 1608c85023e1SHuy Nguyen if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) || 16098978cc92SEran Ben Elisha (!MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) && 16108978cc92SEran Ben Elisha !MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc))) 1611c85023e1SHuy Nguyen return; 1612c85023e1SHuy Nguyen 1613c85023e1SHuy Nguyen mutex_lock(&dev->lb_mutex); 1614c85023e1SHuy Nguyen dev->user_td--; 1615c85023e1SHuy Nguyen 1616c85023e1SHuy Nguyen if (dev->user_td < 2) 1617c85023e1SHuy Nguyen mlx5_nic_vport_update_local_lb(dev->mdev, false); 1618c85023e1SHuy Nguyen 1619c85023e1SHuy Nguyen mutex_unlock(&dev->lb_mutex); 1620c85023e1SHuy Nguyen } 1621c85023e1SHuy Nguyen 1622e126ba97SEli Cohen static struct ib_ucontext *mlx5_ib_alloc_ucontext(struct ib_device *ibdev, 1623e126ba97SEli Cohen struct ib_udata *udata) 1624e126ba97SEli Cohen { 1625e126ba97SEli Cohen struct mlx5_ib_dev *dev = to_mdev(ibdev); 1626b368d7cbSMatan Barak struct mlx5_ib_alloc_ucontext_req_v2 req = {}; 1627b368d7cbSMatan Barak struct mlx5_ib_alloc_ucontext_resp resp = {}; 16285c99eaecSFeras Daoud struct mlx5_core_dev *mdev = dev->mdev; 1629e126ba97SEli Cohen struct mlx5_ib_ucontext *context; 16302f5ff264SEli Cohen struct mlx5_bfreg_info *bfregi; 163178c0f98cSEli Cohen int ver; 1632e126ba97SEli Cohen int err; 1633a168a41cSMajd Dibbiny size_t min_req_v2 = offsetof(struct mlx5_ib_alloc_ucontext_req_v2, 1634a168a41cSMajd Dibbiny max_cqe_version); 163525bb36e7SYonatan Cohen u32 dump_fill_mkey; 1636b037c29aSEli Cohen bool lib_uar_4k; 1637e126ba97SEli Cohen 1638e126ba97SEli Cohen if (!dev->ib_active) 1639e126ba97SEli Cohen return ERR_PTR(-EAGAIN); 1640e126ba97SEli Cohen 1641e093111dSAmrani, Ram if (udata->inlen == sizeof(struct mlx5_ib_alloc_ucontext_req)) 164278c0f98cSEli Cohen ver = 0; 1643e093111dSAmrani, Ram else if (udata->inlen >= min_req_v2) 164478c0f98cSEli Cohen ver = 2; 164578c0f98cSEli Cohen else 164678c0f98cSEli Cohen return ERR_PTR(-EINVAL); 164778c0f98cSEli Cohen 1648e093111dSAmrani, Ram err = ib_copy_from_udata(&req, udata, min(udata->inlen, sizeof(req))); 1649e126ba97SEli Cohen if (err) 1650e126ba97SEli Cohen return ERR_PTR(err); 1651e126ba97SEli Cohen 1652a8b92ca1SYishai Hadas if (req.flags & ~MLX5_IB_ALLOC_UCTX_DEVX) 1653a8b92ca1SYishai Hadas return ERR_PTR(-EOPNOTSUPP); 165478c0f98cSEli Cohen 1655f72300c5SHaggai Abramovsky if (req.comp_mask || req.reserved0 || req.reserved1 || req.reserved2) 1656b368d7cbSMatan Barak return ERR_PTR(-EOPNOTSUPP); 1657b368d7cbSMatan Barak 16582f5ff264SEli Cohen req.total_num_bfregs = ALIGN(req.total_num_bfregs, 16592f5ff264SEli Cohen MLX5_NON_FP_BFREGS_PER_UAR); 16602f5ff264SEli Cohen if (req.num_low_latency_bfregs > req.total_num_bfregs - 1) 1661e126ba97SEli Cohen return ERR_PTR(-EINVAL); 1662e126ba97SEli Cohen 1663938fe83cSSaeed Mahameed resp.qp_tab_size = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp); 16642cc6ad5fSNoa Osherovich if (mlx5_core_is_pf(dev->mdev) && MLX5_CAP_GEN(dev->mdev, bf)) 1665938fe83cSSaeed Mahameed resp.bf_reg_size = 1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size); 1666b47bd6eaSDaniel Jurgens resp.cache_line_size = cache_line_size(); 1667938fe83cSSaeed Mahameed resp.max_sq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq); 1668938fe83cSSaeed Mahameed resp.max_rq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq); 1669938fe83cSSaeed Mahameed resp.max_send_wqebb = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz); 1670938fe83cSSaeed Mahameed resp.max_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz); 1671938fe83cSSaeed Mahameed resp.max_srq_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz); 1672f72300c5SHaggai Abramovsky resp.cqe_version = min_t(__u8, 1673f72300c5SHaggai Abramovsky (__u8)MLX5_CAP_GEN(dev->mdev, cqe_version), 1674f72300c5SHaggai Abramovsky req.max_cqe_version); 167530aa60b3SEli Cohen resp.log_uar_size = MLX5_CAP_GEN(dev->mdev, uar_4k) ? 167630aa60b3SEli Cohen MLX5_ADAPTER_PAGE_SHIFT : PAGE_SHIFT; 167730aa60b3SEli Cohen resp.num_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ? 167830aa60b3SEli Cohen MLX5_CAP_GEN(dev->mdev, num_of_uars_per_page) : 1; 1679b368d7cbSMatan Barak resp.response_length = min(offsetof(typeof(resp), response_length) + 1680b368d7cbSMatan Barak sizeof(resp.response_length), udata->outlen); 1681e126ba97SEli Cohen 1682c03faa56SMatan Barak if (mlx5_accel_ipsec_device_caps(dev->mdev) & MLX5_ACCEL_IPSEC_CAP_DEVICE) { 1683c03faa56SMatan Barak if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_EGRESS)) 1684c03faa56SMatan Barak resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM; 1685c03faa56SMatan Barak if (mlx5_accel_ipsec_device_caps(dev->mdev) & MLX5_ACCEL_IPSEC_CAP_REQUIRED_METADATA) 1686c03faa56SMatan Barak resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_REQ_METADATA; 1687c03faa56SMatan Barak if (MLX5_CAP_FLOWTABLE(dev->mdev, flow_table_properties_nic_receive.ft_field_support.outer_esp_spi)) 1688c03faa56SMatan Barak resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_SPI_STEERING; 1689c03faa56SMatan Barak if (mlx5_accel_ipsec_device_caps(dev->mdev) & MLX5_ACCEL_IPSEC_CAP_TX_IV_IS_ESN) 1690c03faa56SMatan Barak resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_TX_IV_IS_ESN; 1691c03faa56SMatan Barak /* MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_FULL_OFFLOAD is currently always 0 */ 1692c03faa56SMatan Barak } 1693c03faa56SMatan Barak 1694e126ba97SEli Cohen context = kzalloc(sizeof(*context), GFP_KERNEL); 1695e126ba97SEli Cohen if (!context) 1696e126ba97SEli Cohen return ERR_PTR(-ENOMEM); 1697e126ba97SEli Cohen 169830aa60b3SEli Cohen lib_uar_4k = req.lib_caps & MLX5_LIB_CAP_4K_UAR; 16992f5ff264SEli Cohen bfregi = &context->bfregi; 1700b037c29aSEli Cohen 1701b037c29aSEli Cohen /* updates req->total_num_bfregs */ 170231a78a5aSYishai Hadas err = calc_total_bfregs(dev, lib_uar_4k, &req, bfregi); 1703b037c29aSEli Cohen if (err) 1704b037c29aSEli Cohen goto out_ctx; 1705b037c29aSEli Cohen 17062f5ff264SEli Cohen mutex_init(&bfregi->lock); 1707b037c29aSEli Cohen bfregi->lib_uar_4k = lib_uar_4k; 170831a78a5aSYishai Hadas bfregi->count = kcalloc(bfregi->total_num_bfregs, sizeof(*bfregi->count), 1709b037c29aSEli Cohen GFP_KERNEL); 1710b037c29aSEli Cohen if (!bfregi->count) { 1711e126ba97SEli Cohen err = -ENOMEM; 1712e126ba97SEli Cohen goto out_ctx; 1713e126ba97SEli Cohen } 1714e126ba97SEli Cohen 1715b037c29aSEli Cohen bfregi->sys_pages = kcalloc(bfregi->num_sys_pages, 1716b037c29aSEli Cohen sizeof(*bfregi->sys_pages), 1717e126ba97SEli Cohen GFP_KERNEL); 1718b037c29aSEli Cohen if (!bfregi->sys_pages) { 1719e126ba97SEli Cohen err = -ENOMEM; 1720e126ba97SEli Cohen goto out_count; 1721e126ba97SEli Cohen } 1722e126ba97SEli Cohen 1723b037c29aSEli Cohen err = allocate_uars(dev, context); 1724b037c29aSEli Cohen if (err) 1725b037c29aSEli Cohen goto out_sys_pages; 1726b037c29aSEli Cohen 1727b4cfe447SHaggai Eran #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING 1728b4cfe447SHaggai Eran context->ibucontext.invalidate_range = &mlx5_ib_invalidate_range; 1729b4cfe447SHaggai Eran #endif 1730b4cfe447SHaggai Eran 1731c85023e1SHuy Nguyen err = mlx5_ib_alloc_transport_domain(dev, &context->tdn); 1732146d2f1aSmajd@mellanox.com if (err) 1733c44ef998SIlya Lesokhin goto out_uars; 1734146d2f1aSmajd@mellanox.com 1735a8b92ca1SYishai Hadas if (req.flags & MLX5_IB_ALLOC_UCTX_DEVX) { 1736a8b92ca1SYishai Hadas /* Block DEVX on Infiniband as of SELinux */ 1737a8b92ca1SYishai Hadas if (mlx5_ib_port_link_layer(ibdev, 1) != IB_LINK_LAYER_ETHERNET) { 1738a8b92ca1SYishai Hadas err = -EPERM; 1739a8b92ca1SYishai Hadas goto out_td; 1740a8b92ca1SYishai Hadas } 1741a8b92ca1SYishai Hadas 1742a8b92ca1SYishai Hadas err = mlx5_ib_devx_create(dev, context); 1743a8b92ca1SYishai Hadas if (err) 1744a8b92ca1SYishai Hadas goto out_td; 1745a8b92ca1SYishai Hadas } 1746a8b92ca1SYishai Hadas 174725bb36e7SYonatan Cohen if (MLX5_CAP_GEN(dev->mdev, dump_fill_mkey)) { 174825bb36e7SYonatan Cohen err = mlx5_cmd_dump_fill_mkey(dev->mdev, &dump_fill_mkey); 174925bb36e7SYonatan Cohen if (err) 1750*8193abb6SJason Gunthorpe goto out_mdev; 175125bb36e7SYonatan Cohen } 175225bb36e7SYonatan Cohen 17537c2344c3SMaor Gottlieb INIT_LIST_HEAD(&context->vma_private_list); 1754ad9a3668SMajd Dibbiny mutex_init(&context->vma_private_list_mutex); 1755e126ba97SEli Cohen INIT_LIST_HEAD(&context->db_page_list); 1756e126ba97SEli Cohen mutex_init(&context->db_page_mutex); 1757e126ba97SEli Cohen 17582f5ff264SEli Cohen resp.tot_bfregs = req.total_num_bfregs; 1759508562d6SDaniel Jurgens resp.num_ports = dev->num_ports; 1760b368d7cbSMatan Barak 1761f72300c5SHaggai Abramovsky if (field_avail(typeof(resp), cqe_version, udata->outlen)) 1762f72300c5SHaggai Abramovsky resp.response_length += sizeof(resp.cqe_version); 1763b368d7cbSMatan Barak 1764402ca536SBodong Wang if (field_avail(typeof(resp), cmds_supp_uhw, udata->outlen)) { 17656ad279c5SMoni Shoua resp.cmds_supp_uhw |= MLX5_USER_CMDS_SUPP_UHW_QUERY_DEVICE | 17666ad279c5SMoni Shoua MLX5_USER_CMDS_SUPP_UHW_CREATE_AH; 1767402ca536SBodong Wang resp.response_length += sizeof(resp.cmds_supp_uhw); 1768402ca536SBodong Wang } 1769402ca536SBodong Wang 177078984898SOr Gerlitz if (field_avail(typeof(resp), eth_min_inline, udata->outlen)) { 177178984898SOr Gerlitz if (mlx5_ib_port_link_layer(ibdev, 1) == IB_LINK_LAYER_ETHERNET) { 177278984898SOr Gerlitz mlx5_query_min_inline(dev->mdev, &resp.eth_min_inline); 177378984898SOr Gerlitz resp.eth_min_inline++; 177478984898SOr Gerlitz } 177578984898SOr Gerlitz resp.response_length += sizeof(resp.eth_min_inline); 177678984898SOr Gerlitz } 177778984898SOr Gerlitz 17785c99eaecSFeras Daoud if (field_avail(typeof(resp), clock_info_versions, udata->outlen)) { 17795c99eaecSFeras Daoud if (mdev->clock_info) 17805c99eaecSFeras Daoud resp.clock_info_versions = BIT(MLX5_IB_CLOCK_INFO_V1); 17815c99eaecSFeras Daoud resp.response_length += sizeof(resp.clock_info_versions); 17825c99eaecSFeras Daoud } 17835c99eaecSFeras Daoud 1784bc5c6eedSNoa Osherovich /* 1785bc5c6eedSNoa Osherovich * We don't want to expose information from the PCI bar that is located 1786bc5c6eedSNoa Osherovich * after 4096 bytes, so if the arch only supports larger pages, let's 1787bc5c6eedSNoa Osherovich * pretend we don't support reading the HCA's core clock. This is also 1788bc5c6eedSNoa Osherovich * forced by mmap function. 1789bc5c6eedSNoa Osherovich */ 1790de8d6e02SEli Cohen if (field_avail(typeof(resp), hca_core_clock_offset, udata->outlen)) { 1791de8d6e02SEli Cohen if (PAGE_SIZE <= 4096) { 1792b368d7cbSMatan Barak resp.comp_mask |= 1793b368d7cbSMatan Barak MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_CORE_CLOCK_OFFSET; 1794b368d7cbSMatan Barak resp.hca_core_clock_offset = 1795de8d6e02SEli Cohen offsetof(struct mlx5_init_seg, internal_timer_h) % PAGE_SIZE; 1796de8d6e02SEli Cohen } 17975c99eaecSFeras Daoud resp.response_length += sizeof(resp.hca_core_clock_offset); 1798b368d7cbSMatan Barak } 1799b368d7cbSMatan Barak 180030aa60b3SEli Cohen if (field_avail(typeof(resp), log_uar_size, udata->outlen)) 180130aa60b3SEli Cohen resp.response_length += sizeof(resp.log_uar_size); 180230aa60b3SEli Cohen 180330aa60b3SEli Cohen if (field_avail(typeof(resp), num_uars_per_page, udata->outlen)) 180430aa60b3SEli Cohen resp.response_length += sizeof(resp.num_uars_per_page); 180530aa60b3SEli Cohen 180631a78a5aSYishai Hadas if (field_avail(typeof(resp), num_dyn_bfregs, udata->outlen)) { 180731a78a5aSYishai Hadas resp.num_dyn_bfregs = bfregi->num_dyn_bfregs; 180831a78a5aSYishai Hadas resp.response_length += sizeof(resp.num_dyn_bfregs); 180931a78a5aSYishai Hadas } 181031a78a5aSYishai Hadas 181125bb36e7SYonatan Cohen if (field_avail(typeof(resp), dump_fill_mkey, udata->outlen)) { 181225bb36e7SYonatan Cohen if (MLX5_CAP_GEN(dev->mdev, dump_fill_mkey)) { 181325bb36e7SYonatan Cohen resp.dump_fill_mkey = dump_fill_mkey; 181425bb36e7SYonatan Cohen resp.comp_mask |= 181525bb36e7SYonatan Cohen MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_DUMP_FILL_MKEY; 181625bb36e7SYonatan Cohen } 181725bb36e7SYonatan Cohen resp.response_length += sizeof(resp.dump_fill_mkey); 181825bb36e7SYonatan Cohen } 181925bb36e7SYonatan Cohen 1820b368d7cbSMatan Barak err = ib_copy_to_udata(udata, &resp, resp.response_length); 1821e126ba97SEli Cohen if (err) 1822a8b92ca1SYishai Hadas goto out_mdev; 1823e126ba97SEli Cohen 18242f5ff264SEli Cohen bfregi->ver = ver; 18252f5ff264SEli Cohen bfregi->num_low_latency_bfregs = req.num_low_latency_bfregs; 1826f72300c5SHaggai Abramovsky context->cqe_version = resp.cqe_version; 182730aa60b3SEli Cohen context->lib_caps = req.lib_caps; 182830aa60b3SEli Cohen print_lib_caps(dev, context->lib_caps); 1829f72300c5SHaggai Abramovsky 1830e126ba97SEli Cohen return &context->ibucontext; 1831e126ba97SEli Cohen 1832a8b92ca1SYishai Hadas out_mdev: 1833a8b92ca1SYishai Hadas if (req.flags & MLX5_IB_ALLOC_UCTX_DEVX) 1834a8b92ca1SYishai Hadas mlx5_ib_devx_destroy(dev, context); 1835146d2f1aSmajd@mellanox.com out_td: 1836c85023e1SHuy Nguyen mlx5_ib_dealloc_transport_domain(dev, context->tdn); 1837146d2f1aSmajd@mellanox.com 1838e126ba97SEli Cohen out_uars: 1839b037c29aSEli Cohen deallocate_uars(dev, context); 1840b037c29aSEli Cohen 1841b037c29aSEli Cohen out_sys_pages: 1842b037c29aSEli Cohen kfree(bfregi->sys_pages); 1843b037c29aSEli Cohen 1844e126ba97SEli Cohen out_count: 18452f5ff264SEli Cohen kfree(bfregi->count); 1846e126ba97SEli Cohen 1847e126ba97SEli Cohen out_ctx: 1848e126ba97SEli Cohen kfree(context); 1849b037c29aSEli Cohen 1850e126ba97SEli Cohen return ERR_PTR(err); 1851e126ba97SEli Cohen } 1852e126ba97SEli Cohen 1853e126ba97SEli Cohen static int mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext) 1854e126ba97SEli Cohen { 1855e126ba97SEli Cohen struct mlx5_ib_ucontext *context = to_mucontext(ibcontext); 1856e126ba97SEli Cohen struct mlx5_ib_dev *dev = to_mdev(ibcontext->device); 1857b037c29aSEli Cohen struct mlx5_bfreg_info *bfregi; 1858e126ba97SEli Cohen 1859a8b92ca1SYishai Hadas if (context->devx_uid) 1860a8b92ca1SYishai Hadas mlx5_ib_devx_destroy(dev, context); 1861a8b92ca1SYishai Hadas 1862b037c29aSEli Cohen bfregi = &context->bfregi; 1863c85023e1SHuy Nguyen mlx5_ib_dealloc_transport_domain(dev, context->tdn); 1864146d2f1aSmajd@mellanox.com 1865b037c29aSEli Cohen deallocate_uars(dev, context); 1866b037c29aSEli Cohen kfree(bfregi->sys_pages); 18672f5ff264SEli Cohen kfree(bfregi->count); 1868e126ba97SEli Cohen kfree(context); 1869e126ba97SEli Cohen 1870e126ba97SEli Cohen return 0; 1871e126ba97SEli Cohen } 1872e126ba97SEli Cohen 1873b037c29aSEli Cohen static phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev, 18744ed131d0SYishai Hadas int uar_idx) 1875e126ba97SEli Cohen { 1876b037c29aSEli Cohen int fw_uars_per_page; 1877b037c29aSEli Cohen 1878b037c29aSEli Cohen fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ? MLX5_UARS_IN_PAGE : 1; 1879b037c29aSEli Cohen 18804ed131d0SYishai Hadas return (pci_resource_start(dev->mdev->pdev, 0) >> PAGE_SHIFT) + uar_idx / fw_uars_per_page; 1881e126ba97SEli Cohen } 1882e126ba97SEli Cohen 1883e126ba97SEli Cohen static int get_command(unsigned long offset) 1884e126ba97SEli Cohen { 1885e126ba97SEli Cohen return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK; 1886e126ba97SEli Cohen } 1887e126ba97SEli Cohen 1888e126ba97SEli Cohen static int get_arg(unsigned long offset) 1889e126ba97SEli Cohen { 1890e126ba97SEli Cohen return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1); 1891e126ba97SEli Cohen } 1892e126ba97SEli Cohen 1893e126ba97SEli Cohen static int get_index(unsigned long offset) 1894e126ba97SEli Cohen { 1895e126ba97SEli Cohen return get_arg(offset); 1896e126ba97SEli Cohen } 1897e126ba97SEli Cohen 18984ed131d0SYishai Hadas /* Index resides in an extra byte to enable larger values than 255 */ 18994ed131d0SYishai Hadas static int get_extended_index(unsigned long offset) 19004ed131d0SYishai Hadas { 19014ed131d0SYishai Hadas return get_arg(offset) | ((offset >> 16) & 0xff) << 8; 19024ed131d0SYishai Hadas } 19034ed131d0SYishai Hadas 19047c2344c3SMaor Gottlieb static void mlx5_ib_vma_open(struct vm_area_struct *area) 19057c2344c3SMaor Gottlieb { 19067c2344c3SMaor Gottlieb /* vma_open is called when a new VMA is created on top of our VMA. This 19077c2344c3SMaor Gottlieb * is done through either mremap flow or split_vma (usually due to 19087c2344c3SMaor Gottlieb * mlock, madvise, munmap, etc.) We do not support a clone of the VMA, 19097c2344c3SMaor Gottlieb * as this VMA is strongly hardware related. Therefore we set the 19107c2344c3SMaor Gottlieb * vm_ops of the newly created/cloned VMA to NULL, to prevent it from 19117c2344c3SMaor Gottlieb * calling us again and trying to do incorrect actions. We assume that 19127c2344c3SMaor Gottlieb * the original VMA size is exactly a single page, and therefore all 19137c2344c3SMaor Gottlieb * "splitting" operation will not happen to it. 19147c2344c3SMaor Gottlieb */ 19157c2344c3SMaor Gottlieb area->vm_ops = NULL; 19167c2344c3SMaor Gottlieb } 19177c2344c3SMaor Gottlieb 19187c2344c3SMaor Gottlieb static void mlx5_ib_vma_close(struct vm_area_struct *area) 19197c2344c3SMaor Gottlieb { 19207c2344c3SMaor Gottlieb struct mlx5_ib_vma_private_data *mlx5_ib_vma_priv_data; 19217c2344c3SMaor Gottlieb 19227c2344c3SMaor Gottlieb /* It's guaranteed that all VMAs opened on a FD are closed before the 19237c2344c3SMaor Gottlieb * file itself is closed, therefore no sync is needed with the regular 19247c2344c3SMaor Gottlieb * closing flow. (e.g. mlx5 ib_dealloc_ucontext) 19257c2344c3SMaor Gottlieb * However need a sync with accessing the vma as part of 19267c2344c3SMaor Gottlieb * mlx5_ib_disassociate_ucontext. 19277c2344c3SMaor Gottlieb * The close operation is usually called under mm->mmap_sem except when 19287c2344c3SMaor Gottlieb * process is exiting. 19297c2344c3SMaor Gottlieb * The exiting case is handled explicitly as part of 19307c2344c3SMaor Gottlieb * mlx5_ib_disassociate_ucontext. 19317c2344c3SMaor Gottlieb */ 19327c2344c3SMaor Gottlieb mlx5_ib_vma_priv_data = (struct mlx5_ib_vma_private_data *)area->vm_private_data; 19337c2344c3SMaor Gottlieb 19347c2344c3SMaor Gottlieb /* setting the vma context pointer to null in the mlx5_ib driver's 19357c2344c3SMaor Gottlieb * private data, to protect a race condition in 19367c2344c3SMaor Gottlieb * mlx5_ib_disassociate_ucontext(). 19377c2344c3SMaor Gottlieb */ 19387c2344c3SMaor Gottlieb mlx5_ib_vma_priv_data->vma = NULL; 1939ad9a3668SMajd Dibbiny mutex_lock(mlx5_ib_vma_priv_data->vma_private_list_mutex); 19407c2344c3SMaor Gottlieb list_del(&mlx5_ib_vma_priv_data->list); 1941ad9a3668SMajd Dibbiny mutex_unlock(mlx5_ib_vma_priv_data->vma_private_list_mutex); 19427c2344c3SMaor Gottlieb kfree(mlx5_ib_vma_priv_data); 19437c2344c3SMaor Gottlieb } 19447c2344c3SMaor Gottlieb 19457c2344c3SMaor Gottlieb static const struct vm_operations_struct mlx5_ib_vm_ops = { 19467c2344c3SMaor Gottlieb .open = mlx5_ib_vma_open, 19477c2344c3SMaor Gottlieb .close = mlx5_ib_vma_close 19487c2344c3SMaor Gottlieb }; 19497c2344c3SMaor Gottlieb 19507c2344c3SMaor Gottlieb static int mlx5_ib_set_vma_data(struct vm_area_struct *vma, 19517c2344c3SMaor Gottlieb struct mlx5_ib_ucontext *ctx) 19527c2344c3SMaor Gottlieb { 19537c2344c3SMaor Gottlieb struct mlx5_ib_vma_private_data *vma_prv; 19547c2344c3SMaor Gottlieb struct list_head *vma_head = &ctx->vma_private_list; 19557c2344c3SMaor Gottlieb 19567c2344c3SMaor Gottlieb vma_prv = kzalloc(sizeof(*vma_prv), GFP_KERNEL); 19577c2344c3SMaor Gottlieb if (!vma_prv) 19587c2344c3SMaor Gottlieb return -ENOMEM; 19597c2344c3SMaor Gottlieb 19607c2344c3SMaor Gottlieb vma_prv->vma = vma; 1961ad9a3668SMajd Dibbiny vma_prv->vma_private_list_mutex = &ctx->vma_private_list_mutex; 19627c2344c3SMaor Gottlieb vma->vm_private_data = vma_prv; 19637c2344c3SMaor Gottlieb vma->vm_ops = &mlx5_ib_vm_ops; 19647c2344c3SMaor Gottlieb 1965ad9a3668SMajd Dibbiny mutex_lock(&ctx->vma_private_list_mutex); 19667c2344c3SMaor Gottlieb list_add(&vma_prv->list, vma_head); 1967ad9a3668SMajd Dibbiny mutex_unlock(&ctx->vma_private_list_mutex); 19687c2344c3SMaor Gottlieb 19697c2344c3SMaor Gottlieb return 0; 19707c2344c3SMaor Gottlieb } 19717c2344c3SMaor Gottlieb 19727c2344c3SMaor Gottlieb static void mlx5_ib_disassociate_ucontext(struct ib_ucontext *ibcontext) 19737c2344c3SMaor Gottlieb { 19747c2344c3SMaor Gottlieb struct vm_area_struct *vma; 19757c2344c3SMaor Gottlieb struct mlx5_ib_vma_private_data *vma_private, *n; 19767c2344c3SMaor Gottlieb struct mlx5_ib_ucontext *context = to_mucontext(ibcontext); 19777c2344c3SMaor Gottlieb 1978ad9a3668SMajd Dibbiny mutex_lock(&context->vma_private_list_mutex); 19797c2344c3SMaor Gottlieb list_for_each_entry_safe(vma_private, n, &context->vma_private_list, 19807c2344c3SMaor Gottlieb list) { 19817c2344c3SMaor Gottlieb vma = vma_private->vma; 19822cb40791SLeon Romanovsky zap_vma_ptes(vma, vma->vm_start, PAGE_SIZE); 19837c2344c3SMaor Gottlieb /* context going to be destroyed, should 19847c2344c3SMaor Gottlieb * not access ops any more. 19857c2344c3SMaor Gottlieb */ 198613776612SMaor Gottlieb vma->vm_flags &= ~(VM_SHARED | VM_MAYSHARE); 19877c2344c3SMaor Gottlieb vma->vm_ops = NULL; 19887c2344c3SMaor Gottlieb list_del(&vma_private->list); 19897c2344c3SMaor Gottlieb kfree(vma_private); 19907c2344c3SMaor Gottlieb } 1991ad9a3668SMajd Dibbiny mutex_unlock(&context->vma_private_list_mutex); 19927c2344c3SMaor Gottlieb } 19937c2344c3SMaor Gottlieb 199437aa5c36SGuy Levi static inline char *mmap_cmd2str(enum mlx5_ib_mmap_cmd cmd) 1995e126ba97SEli Cohen { 199637aa5c36SGuy Levi switch (cmd) { 199737aa5c36SGuy Levi case MLX5_IB_MMAP_WC_PAGE: 199837aa5c36SGuy Levi return "WC"; 1999e126ba97SEli Cohen case MLX5_IB_MMAP_REGULAR_PAGE: 200037aa5c36SGuy Levi return "best effort WC"; 200137aa5c36SGuy Levi case MLX5_IB_MMAP_NC_PAGE: 200237aa5c36SGuy Levi return "NC"; 200324da0016SAriel Levkovich case MLX5_IB_MMAP_DEVICE_MEM: 200424da0016SAriel Levkovich return "Device Memory"; 200537aa5c36SGuy Levi default: 200637aa5c36SGuy Levi return NULL; 200737aa5c36SGuy Levi } 200837aa5c36SGuy Levi } 200937aa5c36SGuy Levi 20105c99eaecSFeras Daoud static int mlx5_ib_mmap_clock_info_page(struct mlx5_ib_dev *dev, 20115c99eaecSFeras Daoud struct vm_area_struct *vma, 20125c99eaecSFeras Daoud struct mlx5_ib_ucontext *context) 20135c99eaecSFeras Daoud { 20145c99eaecSFeras Daoud phys_addr_t pfn; 20155c99eaecSFeras Daoud int err; 20165c99eaecSFeras Daoud 20175c99eaecSFeras Daoud if (vma->vm_end - vma->vm_start != PAGE_SIZE) 20185c99eaecSFeras Daoud return -EINVAL; 20195c99eaecSFeras Daoud 20205c99eaecSFeras Daoud if (get_index(vma->vm_pgoff) != MLX5_IB_CLOCK_INFO_V1) 20215c99eaecSFeras Daoud return -EOPNOTSUPP; 20225c99eaecSFeras Daoud 20235c99eaecSFeras Daoud if (vma->vm_flags & VM_WRITE) 20245c99eaecSFeras Daoud return -EPERM; 20255c99eaecSFeras Daoud 20265c99eaecSFeras Daoud if (!dev->mdev->clock_info_page) 20275c99eaecSFeras Daoud return -EOPNOTSUPP; 20285c99eaecSFeras Daoud 20295c99eaecSFeras Daoud pfn = page_to_pfn(dev->mdev->clock_info_page); 20305c99eaecSFeras Daoud err = remap_pfn_range(vma, vma->vm_start, pfn, PAGE_SIZE, 20315c99eaecSFeras Daoud vma->vm_page_prot); 20325c99eaecSFeras Daoud if (err) 20335c99eaecSFeras Daoud return err; 20345c99eaecSFeras Daoud 20355c99eaecSFeras Daoud return mlx5_ib_set_vma_data(vma, context); 20365c99eaecSFeras Daoud } 20375c99eaecSFeras Daoud 203837aa5c36SGuy Levi static int uar_mmap(struct mlx5_ib_dev *dev, enum mlx5_ib_mmap_cmd cmd, 20397c2344c3SMaor Gottlieb struct vm_area_struct *vma, 20407c2344c3SMaor Gottlieb struct mlx5_ib_ucontext *context) 204137aa5c36SGuy Levi { 20422f5ff264SEli Cohen struct mlx5_bfreg_info *bfregi = &context->bfregi; 204337aa5c36SGuy Levi int err; 204437aa5c36SGuy Levi unsigned long idx; 204537aa5c36SGuy Levi phys_addr_t pfn, pa; 204637aa5c36SGuy Levi pgprot_t prot; 20474ed131d0SYishai Hadas u32 bfreg_dyn_idx = 0; 20484ed131d0SYishai Hadas u32 uar_index; 20494ed131d0SYishai Hadas int dyn_uar = (cmd == MLX5_IB_MMAP_ALLOC_WC); 20504ed131d0SYishai Hadas int max_valid_idx = dyn_uar ? bfregi->num_sys_pages : 20514ed131d0SYishai Hadas bfregi->num_static_sys_pages; 2052b037c29aSEli Cohen 2053b037c29aSEli Cohen if (vma->vm_end - vma->vm_start != PAGE_SIZE) 2054b037c29aSEli Cohen return -EINVAL; 2055b037c29aSEli Cohen 20564ed131d0SYishai Hadas if (dyn_uar) 20574ed131d0SYishai Hadas idx = get_extended_index(vma->vm_pgoff) + bfregi->num_static_sys_pages; 20584ed131d0SYishai Hadas else 2059b037c29aSEli Cohen idx = get_index(vma->vm_pgoff); 20604ed131d0SYishai Hadas 20614ed131d0SYishai Hadas if (idx >= max_valid_idx) { 20624ed131d0SYishai Hadas mlx5_ib_warn(dev, "invalid uar index %lu, max=%d\n", 20634ed131d0SYishai Hadas idx, max_valid_idx); 2064b037c29aSEli Cohen return -EINVAL; 2065b037c29aSEli Cohen } 206637aa5c36SGuy Levi 206737aa5c36SGuy Levi switch (cmd) { 206837aa5c36SGuy Levi case MLX5_IB_MMAP_WC_PAGE: 20694ed131d0SYishai Hadas case MLX5_IB_MMAP_ALLOC_WC: 207037aa5c36SGuy Levi /* Some architectures don't support WC memory */ 207137aa5c36SGuy Levi #if defined(CONFIG_X86) 207237aa5c36SGuy Levi if (!pat_enabled()) 207337aa5c36SGuy Levi return -EPERM; 207437aa5c36SGuy Levi #elif !(defined(CONFIG_PPC) || (defined(CONFIG_ARM) && defined(CONFIG_MMU))) 207537aa5c36SGuy Levi return -EPERM; 207637aa5c36SGuy Levi #endif 207737aa5c36SGuy Levi /* fall through */ 207837aa5c36SGuy Levi case MLX5_IB_MMAP_REGULAR_PAGE: 207937aa5c36SGuy Levi /* For MLX5_IB_MMAP_REGULAR_PAGE do the best effort to get WC */ 208037aa5c36SGuy Levi prot = pgprot_writecombine(vma->vm_page_prot); 208137aa5c36SGuy Levi break; 208237aa5c36SGuy Levi case MLX5_IB_MMAP_NC_PAGE: 208337aa5c36SGuy Levi prot = pgprot_noncached(vma->vm_page_prot); 208437aa5c36SGuy Levi break; 208537aa5c36SGuy Levi default: 208637aa5c36SGuy Levi return -EINVAL; 208737aa5c36SGuy Levi } 208837aa5c36SGuy Levi 20894ed131d0SYishai Hadas if (dyn_uar) { 20904ed131d0SYishai Hadas int uars_per_page; 20914ed131d0SYishai Hadas 20924ed131d0SYishai Hadas uars_per_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k); 20934ed131d0SYishai Hadas bfreg_dyn_idx = idx * (uars_per_page * MLX5_NON_FP_BFREGS_PER_UAR); 20944ed131d0SYishai Hadas if (bfreg_dyn_idx >= bfregi->total_num_bfregs) { 20954ed131d0SYishai Hadas mlx5_ib_warn(dev, "invalid bfreg_dyn_idx %u, max=%u\n", 20964ed131d0SYishai Hadas bfreg_dyn_idx, bfregi->total_num_bfregs); 20974ed131d0SYishai Hadas return -EINVAL; 20984ed131d0SYishai Hadas } 20994ed131d0SYishai Hadas 21004ed131d0SYishai Hadas mutex_lock(&bfregi->lock); 21014ed131d0SYishai Hadas /* Fail if uar already allocated, first bfreg index of each 21024ed131d0SYishai Hadas * page holds its count. 21034ed131d0SYishai Hadas */ 21044ed131d0SYishai Hadas if (bfregi->count[bfreg_dyn_idx]) { 21054ed131d0SYishai Hadas mlx5_ib_warn(dev, "wrong offset, idx %lu is busy, bfregn=%u\n", idx, bfreg_dyn_idx); 21064ed131d0SYishai Hadas mutex_unlock(&bfregi->lock); 21074ed131d0SYishai Hadas return -EINVAL; 21084ed131d0SYishai Hadas } 21094ed131d0SYishai Hadas 21104ed131d0SYishai Hadas bfregi->count[bfreg_dyn_idx]++; 21114ed131d0SYishai Hadas mutex_unlock(&bfregi->lock); 21124ed131d0SYishai Hadas 21134ed131d0SYishai Hadas err = mlx5_cmd_alloc_uar(dev->mdev, &uar_index); 21144ed131d0SYishai Hadas if (err) { 21154ed131d0SYishai Hadas mlx5_ib_warn(dev, "UAR alloc failed\n"); 21164ed131d0SYishai Hadas goto free_bfreg; 21174ed131d0SYishai Hadas } 21184ed131d0SYishai Hadas } else { 21194ed131d0SYishai Hadas uar_index = bfregi->sys_pages[idx]; 21204ed131d0SYishai Hadas } 21214ed131d0SYishai Hadas 21224ed131d0SYishai Hadas pfn = uar_index2pfn(dev, uar_index); 212337aa5c36SGuy Levi mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn %pa\n", idx, &pfn); 2124e126ba97SEli Cohen 212537aa5c36SGuy Levi vma->vm_page_prot = prot; 212637aa5c36SGuy Levi err = io_remap_pfn_range(vma, vma->vm_start, pfn, 212737aa5c36SGuy Levi PAGE_SIZE, vma->vm_page_prot); 212837aa5c36SGuy Levi if (err) { 21298f062287SLeon Romanovsky mlx5_ib_err(dev, 21308f062287SLeon Romanovsky "io_remap_pfn_range failed with error=%d, mmap_cmd=%s\n", 21318f062287SLeon Romanovsky err, mmap_cmd2str(cmd)); 21324ed131d0SYishai Hadas err = -EAGAIN; 21334ed131d0SYishai Hadas goto err; 213437aa5c36SGuy Levi } 2135e126ba97SEli Cohen 213637aa5c36SGuy Levi pa = pfn << PAGE_SHIFT; 213737aa5c36SGuy Levi 21384ed131d0SYishai Hadas err = mlx5_ib_set_vma_data(vma, context); 21394ed131d0SYishai Hadas if (err) 21404ed131d0SYishai Hadas goto err; 21414ed131d0SYishai Hadas 21424ed131d0SYishai Hadas if (dyn_uar) 21434ed131d0SYishai Hadas bfregi->sys_pages[idx] = uar_index; 21444ed131d0SYishai Hadas return 0; 21454ed131d0SYishai Hadas 21464ed131d0SYishai Hadas err: 21474ed131d0SYishai Hadas if (!dyn_uar) 21484ed131d0SYishai Hadas return err; 21494ed131d0SYishai Hadas 21504ed131d0SYishai Hadas mlx5_cmd_free_uar(dev->mdev, idx); 21514ed131d0SYishai Hadas 21524ed131d0SYishai Hadas free_bfreg: 21534ed131d0SYishai Hadas mlx5_ib_free_bfreg(dev, bfregi, bfreg_dyn_idx); 21544ed131d0SYishai Hadas 21554ed131d0SYishai Hadas return err; 215637aa5c36SGuy Levi } 215737aa5c36SGuy Levi 215824da0016SAriel Levkovich static int dm_mmap(struct ib_ucontext *context, struct vm_area_struct *vma) 215924da0016SAriel Levkovich { 216024da0016SAriel Levkovich struct mlx5_ib_ucontext *mctx = to_mucontext(context); 216124da0016SAriel Levkovich struct mlx5_ib_dev *dev = to_mdev(context->device); 216224da0016SAriel Levkovich u16 page_idx = get_extended_index(vma->vm_pgoff); 216324da0016SAriel Levkovich size_t map_size = vma->vm_end - vma->vm_start; 216424da0016SAriel Levkovich u32 npages = map_size >> PAGE_SHIFT; 216524da0016SAriel Levkovich phys_addr_t pfn; 216624da0016SAriel Levkovich pgprot_t prot; 216724da0016SAriel Levkovich 216824da0016SAriel Levkovich if (find_next_zero_bit(mctx->dm_pages, page_idx + npages, page_idx) != 216924da0016SAriel Levkovich page_idx + npages) 217024da0016SAriel Levkovich return -EINVAL; 217124da0016SAriel Levkovich 217224da0016SAriel Levkovich pfn = ((pci_resource_start(dev->mdev->pdev, 0) + 217324da0016SAriel Levkovich MLX5_CAP64_DEV_MEM(dev->mdev, memic_bar_start_addr)) >> 217424da0016SAriel Levkovich PAGE_SHIFT) + 217524da0016SAriel Levkovich page_idx; 217624da0016SAriel Levkovich prot = pgprot_writecombine(vma->vm_page_prot); 217724da0016SAriel Levkovich vma->vm_page_prot = prot; 217824da0016SAriel Levkovich 217924da0016SAriel Levkovich if (io_remap_pfn_range(vma, vma->vm_start, pfn, map_size, 218024da0016SAriel Levkovich vma->vm_page_prot)) 218124da0016SAriel Levkovich return -EAGAIN; 218224da0016SAriel Levkovich 218324da0016SAriel Levkovich return mlx5_ib_set_vma_data(vma, mctx); 218424da0016SAriel Levkovich } 218524da0016SAriel Levkovich 218637aa5c36SGuy Levi static int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma) 218737aa5c36SGuy Levi { 218837aa5c36SGuy Levi struct mlx5_ib_ucontext *context = to_mucontext(ibcontext); 218937aa5c36SGuy Levi struct mlx5_ib_dev *dev = to_mdev(ibcontext->device); 219037aa5c36SGuy Levi unsigned long command; 219137aa5c36SGuy Levi phys_addr_t pfn; 219237aa5c36SGuy Levi 219337aa5c36SGuy Levi command = get_command(vma->vm_pgoff); 219437aa5c36SGuy Levi switch (command) { 219537aa5c36SGuy Levi case MLX5_IB_MMAP_WC_PAGE: 219637aa5c36SGuy Levi case MLX5_IB_MMAP_NC_PAGE: 219737aa5c36SGuy Levi case MLX5_IB_MMAP_REGULAR_PAGE: 21984ed131d0SYishai Hadas case MLX5_IB_MMAP_ALLOC_WC: 21997c2344c3SMaor Gottlieb return uar_mmap(dev, command, vma, context); 2200e126ba97SEli Cohen 2201e126ba97SEli Cohen case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES: 2202e126ba97SEli Cohen return -ENOSYS; 2203e126ba97SEli Cohen 2204d69e3bcfSMatan Barak case MLX5_IB_MMAP_CORE_CLOCK: 2205d69e3bcfSMatan Barak if (vma->vm_end - vma->vm_start != PAGE_SIZE) 2206d69e3bcfSMatan Barak return -EINVAL; 2207d69e3bcfSMatan Barak 22086cbac1e4SMatan Barak if (vma->vm_flags & VM_WRITE) 2209d69e3bcfSMatan Barak return -EPERM; 2210d69e3bcfSMatan Barak 2211d69e3bcfSMatan Barak /* Don't expose to user-space information it shouldn't have */ 2212d69e3bcfSMatan Barak if (PAGE_SIZE > 4096) 2213d69e3bcfSMatan Barak return -EOPNOTSUPP; 2214d69e3bcfSMatan Barak 2215d69e3bcfSMatan Barak vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); 2216d69e3bcfSMatan Barak pfn = (dev->mdev->iseg_base + 2217d69e3bcfSMatan Barak offsetof(struct mlx5_init_seg, internal_timer_h)) >> 2218d69e3bcfSMatan Barak PAGE_SHIFT; 2219d69e3bcfSMatan Barak if (io_remap_pfn_range(vma, vma->vm_start, pfn, 2220d69e3bcfSMatan Barak PAGE_SIZE, vma->vm_page_prot)) 2221d69e3bcfSMatan Barak return -EAGAIN; 2222d69e3bcfSMatan Barak break; 22235c99eaecSFeras Daoud case MLX5_IB_MMAP_CLOCK_INFO: 22245c99eaecSFeras Daoud return mlx5_ib_mmap_clock_info_page(dev, vma, context); 2225d69e3bcfSMatan Barak 222624da0016SAriel Levkovich case MLX5_IB_MMAP_DEVICE_MEM: 222724da0016SAriel Levkovich return dm_mmap(ibcontext, vma); 222824da0016SAriel Levkovich 2229e126ba97SEli Cohen default: 2230e126ba97SEli Cohen return -EINVAL; 2231e126ba97SEli Cohen } 2232e126ba97SEli Cohen 2233e126ba97SEli Cohen return 0; 2234e126ba97SEli Cohen } 2235e126ba97SEli Cohen 223624da0016SAriel Levkovich struct ib_dm *mlx5_ib_alloc_dm(struct ib_device *ibdev, 223724da0016SAriel Levkovich struct ib_ucontext *context, 223824da0016SAriel Levkovich struct ib_dm_alloc_attr *attr, 223924da0016SAriel Levkovich struct uverbs_attr_bundle *attrs) 224024da0016SAriel Levkovich { 224124da0016SAriel Levkovich u64 act_size = roundup(attr->length, MLX5_MEMIC_BASE_SIZE); 224224da0016SAriel Levkovich struct mlx5_memic *memic = &to_mdev(ibdev)->memic; 224324da0016SAriel Levkovich phys_addr_t memic_addr; 224424da0016SAriel Levkovich struct mlx5_ib_dm *dm; 224524da0016SAriel Levkovich u64 start_offset; 224624da0016SAriel Levkovich u32 page_idx; 224724da0016SAriel Levkovich int err; 224824da0016SAriel Levkovich 224924da0016SAriel Levkovich dm = kzalloc(sizeof(*dm), GFP_KERNEL); 225024da0016SAriel Levkovich if (!dm) 225124da0016SAriel Levkovich return ERR_PTR(-ENOMEM); 225224da0016SAriel Levkovich 225324da0016SAriel Levkovich mlx5_ib_dbg(to_mdev(ibdev), "alloc_memic req: user_length=0x%llx act_length=0x%llx log_alignment=%d\n", 225424da0016SAriel Levkovich attr->length, act_size, attr->alignment); 225524da0016SAriel Levkovich 225624da0016SAriel Levkovich err = mlx5_cmd_alloc_memic(memic, &memic_addr, 225724da0016SAriel Levkovich act_size, attr->alignment); 225824da0016SAriel Levkovich if (err) 225924da0016SAriel Levkovich goto err_free; 226024da0016SAriel Levkovich 226124da0016SAriel Levkovich start_offset = memic_addr & ~PAGE_MASK; 226224da0016SAriel Levkovich page_idx = (memic_addr - pci_resource_start(memic->dev->pdev, 0) - 226324da0016SAriel Levkovich MLX5_CAP64_DEV_MEM(memic->dev, memic_bar_start_addr)) >> 226424da0016SAriel Levkovich PAGE_SHIFT; 226524da0016SAriel Levkovich 226624da0016SAriel Levkovich err = uverbs_copy_to(attrs, 226724da0016SAriel Levkovich MLX5_IB_ATTR_ALLOC_DM_RESP_START_OFFSET, 226824da0016SAriel Levkovich &start_offset, sizeof(start_offset)); 226924da0016SAriel Levkovich if (err) 227024da0016SAriel Levkovich goto err_dealloc; 227124da0016SAriel Levkovich 227224da0016SAriel Levkovich err = uverbs_copy_to(attrs, 227324da0016SAriel Levkovich MLX5_IB_ATTR_ALLOC_DM_RESP_PAGE_INDEX, 227424da0016SAriel Levkovich &page_idx, sizeof(page_idx)); 227524da0016SAriel Levkovich if (err) 227624da0016SAriel Levkovich goto err_dealloc; 227724da0016SAriel Levkovich 227824da0016SAriel Levkovich bitmap_set(to_mucontext(context)->dm_pages, page_idx, 227924da0016SAriel Levkovich DIV_ROUND_UP(act_size, PAGE_SIZE)); 228024da0016SAriel Levkovich 228124da0016SAriel Levkovich dm->dev_addr = memic_addr; 228224da0016SAriel Levkovich 228324da0016SAriel Levkovich return &dm->ibdm; 228424da0016SAriel Levkovich 228524da0016SAriel Levkovich err_dealloc: 228624da0016SAriel Levkovich mlx5_cmd_dealloc_memic(memic, memic_addr, 228724da0016SAriel Levkovich act_size); 228824da0016SAriel Levkovich err_free: 228924da0016SAriel Levkovich kfree(dm); 229024da0016SAriel Levkovich return ERR_PTR(err); 229124da0016SAriel Levkovich } 229224da0016SAriel Levkovich 229324da0016SAriel Levkovich int mlx5_ib_dealloc_dm(struct ib_dm *ibdm) 229424da0016SAriel Levkovich { 229524da0016SAriel Levkovich struct mlx5_memic *memic = &to_mdev(ibdm->device)->memic; 229624da0016SAriel Levkovich struct mlx5_ib_dm *dm = to_mdm(ibdm); 229724da0016SAriel Levkovich u64 act_size = roundup(dm->ibdm.length, MLX5_MEMIC_BASE_SIZE); 229824da0016SAriel Levkovich u32 page_idx; 229924da0016SAriel Levkovich int ret; 230024da0016SAriel Levkovich 230124da0016SAriel Levkovich ret = mlx5_cmd_dealloc_memic(memic, dm->dev_addr, act_size); 230224da0016SAriel Levkovich if (ret) 230324da0016SAriel Levkovich return ret; 230424da0016SAriel Levkovich 230524da0016SAriel Levkovich page_idx = (dm->dev_addr - pci_resource_start(memic->dev->pdev, 0) - 230624da0016SAriel Levkovich MLX5_CAP64_DEV_MEM(memic->dev, memic_bar_start_addr)) >> 230724da0016SAriel Levkovich PAGE_SHIFT; 230824da0016SAriel Levkovich bitmap_clear(to_mucontext(ibdm->uobject->context)->dm_pages, 230924da0016SAriel Levkovich page_idx, 231024da0016SAriel Levkovich DIV_ROUND_UP(act_size, PAGE_SIZE)); 231124da0016SAriel Levkovich 231224da0016SAriel Levkovich kfree(dm); 231324da0016SAriel Levkovich 231424da0016SAriel Levkovich return 0; 231524da0016SAriel Levkovich } 231624da0016SAriel Levkovich 2317e126ba97SEli Cohen static struct ib_pd *mlx5_ib_alloc_pd(struct ib_device *ibdev, 2318e126ba97SEli Cohen struct ib_ucontext *context, 2319e126ba97SEli Cohen struct ib_udata *udata) 2320e126ba97SEli Cohen { 2321e126ba97SEli Cohen struct mlx5_ib_alloc_pd_resp resp; 2322e126ba97SEli Cohen struct mlx5_ib_pd *pd; 2323e126ba97SEli Cohen int err; 2324e126ba97SEli Cohen 2325e126ba97SEli Cohen pd = kmalloc(sizeof(*pd), GFP_KERNEL); 2326e126ba97SEli Cohen if (!pd) 2327e126ba97SEli Cohen return ERR_PTR(-ENOMEM); 2328e126ba97SEli Cohen 23299603b61dSJack Morgenstein err = mlx5_core_alloc_pd(to_mdev(ibdev)->mdev, &pd->pdn); 2330e126ba97SEli Cohen if (err) { 2331e126ba97SEli Cohen kfree(pd); 2332e126ba97SEli Cohen return ERR_PTR(err); 2333e126ba97SEli Cohen } 2334e126ba97SEli Cohen 2335e126ba97SEli Cohen if (context) { 2336e126ba97SEli Cohen resp.pdn = pd->pdn; 2337e126ba97SEli Cohen if (ib_copy_to_udata(udata, &resp, sizeof(resp))) { 23389603b61dSJack Morgenstein mlx5_core_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn); 2339e126ba97SEli Cohen kfree(pd); 2340e126ba97SEli Cohen return ERR_PTR(-EFAULT); 2341e126ba97SEli Cohen } 2342e126ba97SEli Cohen } 2343e126ba97SEli Cohen 2344e126ba97SEli Cohen return &pd->ibpd; 2345e126ba97SEli Cohen } 2346e126ba97SEli Cohen 2347e126ba97SEli Cohen static int mlx5_ib_dealloc_pd(struct ib_pd *pd) 2348e126ba97SEli Cohen { 2349e126ba97SEli Cohen struct mlx5_ib_dev *mdev = to_mdev(pd->device); 2350e126ba97SEli Cohen struct mlx5_ib_pd *mpd = to_mpd(pd); 2351e126ba97SEli Cohen 23529603b61dSJack Morgenstein mlx5_core_dealloc_pd(mdev->mdev, mpd->pdn); 2353e126ba97SEli Cohen kfree(mpd); 2354e126ba97SEli Cohen 2355e126ba97SEli Cohen return 0; 2356e126ba97SEli Cohen } 2357e126ba97SEli Cohen 2358466fa6d2SMaor Gottlieb enum { 2359466fa6d2SMaor Gottlieb MATCH_CRITERIA_ENABLE_OUTER_BIT, 2360466fa6d2SMaor Gottlieb MATCH_CRITERIA_ENABLE_MISC_BIT, 236171c6e863SAriel Levkovich MATCH_CRITERIA_ENABLE_INNER_BIT, 236271c6e863SAriel Levkovich MATCH_CRITERIA_ENABLE_MISC2_BIT 2363466fa6d2SMaor Gottlieb }; 2364038d2ef8SMaor Gottlieb 2365466fa6d2SMaor Gottlieb #define HEADER_IS_ZERO(match_criteria, headers) \ 2366466fa6d2SMaor Gottlieb !(memchr_inv(MLX5_ADDR_OF(fte_match_param, match_criteria, headers), \ 2367466fa6d2SMaor Gottlieb 0, MLX5_FLD_SZ_BYTES(fte_match_param, headers))) \ 2368466fa6d2SMaor Gottlieb 2369466fa6d2SMaor Gottlieb static u8 get_match_criteria_enable(u32 *match_criteria) 2370466fa6d2SMaor Gottlieb { 2371466fa6d2SMaor Gottlieb u8 match_criteria_enable; 2372466fa6d2SMaor Gottlieb 2373466fa6d2SMaor Gottlieb match_criteria_enable = 2374466fa6d2SMaor Gottlieb (!HEADER_IS_ZERO(match_criteria, outer_headers)) << 2375466fa6d2SMaor Gottlieb MATCH_CRITERIA_ENABLE_OUTER_BIT; 2376466fa6d2SMaor Gottlieb match_criteria_enable |= 2377466fa6d2SMaor Gottlieb (!HEADER_IS_ZERO(match_criteria, misc_parameters)) << 2378466fa6d2SMaor Gottlieb MATCH_CRITERIA_ENABLE_MISC_BIT; 2379466fa6d2SMaor Gottlieb match_criteria_enable |= 2380466fa6d2SMaor Gottlieb (!HEADER_IS_ZERO(match_criteria, inner_headers)) << 2381466fa6d2SMaor Gottlieb MATCH_CRITERIA_ENABLE_INNER_BIT; 238271c6e863SAriel Levkovich match_criteria_enable |= 238371c6e863SAriel Levkovich (!HEADER_IS_ZERO(match_criteria, misc_parameters_2)) << 238471c6e863SAriel Levkovich MATCH_CRITERIA_ENABLE_MISC2_BIT; 2385466fa6d2SMaor Gottlieb 2386466fa6d2SMaor Gottlieb return match_criteria_enable; 2387038d2ef8SMaor Gottlieb } 2388038d2ef8SMaor Gottlieb 2389ca0d4753SMaor Gottlieb static void set_proto(void *outer_c, void *outer_v, u8 mask, u8 val) 2390ca0d4753SMaor Gottlieb { 2391ca0d4753SMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_protocol, mask); 2392ca0d4753SMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_protocol, val); 2393ca0d4753SMaor Gottlieb } 2394ca0d4753SMaor Gottlieb 239537da2a03SDaria Velikovsky static void set_flow_label(void *misc_c, void *misc_v, u32 mask, u32 val, 23962d1e697eSMoses Reuben bool inner) 23972d1e697eSMoses Reuben { 23982d1e697eSMoses Reuben if (inner) { 23992d1e697eSMoses Reuben MLX5_SET(fte_match_set_misc, 24002d1e697eSMoses Reuben misc_c, inner_ipv6_flow_label, mask); 24012d1e697eSMoses Reuben MLX5_SET(fte_match_set_misc, 24022d1e697eSMoses Reuben misc_v, inner_ipv6_flow_label, val); 24032d1e697eSMoses Reuben } else { 24042d1e697eSMoses Reuben MLX5_SET(fte_match_set_misc, 24052d1e697eSMoses Reuben misc_c, outer_ipv6_flow_label, mask); 24062d1e697eSMoses Reuben MLX5_SET(fte_match_set_misc, 24072d1e697eSMoses Reuben misc_v, outer_ipv6_flow_label, val); 24082d1e697eSMoses Reuben } 24092d1e697eSMoses Reuben } 24102d1e697eSMoses Reuben 2411ca0d4753SMaor Gottlieb static void set_tos(void *outer_c, void *outer_v, u8 mask, u8 val) 2412ca0d4753SMaor Gottlieb { 2413ca0d4753SMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_ecn, mask); 2414ca0d4753SMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_ecn, val); 2415ca0d4753SMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_dscp, mask >> 2); 2416ca0d4753SMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_dscp, val >> 2); 2417ca0d4753SMaor Gottlieb } 2418ca0d4753SMaor Gottlieb 241971c6e863SAriel Levkovich static int check_mpls_supp_fields(u32 field_support, const __be32 *set_mask) 242071c6e863SAriel Levkovich { 242171c6e863SAriel Levkovich if (MLX5_GET(fte_match_mpls, set_mask, mpls_label) && 242271c6e863SAriel Levkovich !(field_support & MLX5_FIELD_SUPPORT_MPLS_LABEL)) 242371c6e863SAriel Levkovich return -EOPNOTSUPP; 242471c6e863SAriel Levkovich 242571c6e863SAriel Levkovich if (MLX5_GET(fte_match_mpls, set_mask, mpls_exp) && 242671c6e863SAriel Levkovich !(field_support & MLX5_FIELD_SUPPORT_MPLS_EXP)) 242771c6e863SAriel Levkovich return -EOPNOTSUPP; 242871c6e863SAriel Levkovich 242971c6e863SAriel Levkovich if (MLX5_GET(fte_match_mpls, set_mask, mpls_s_bos) && 243071c6e863SAriel Levkovich !(field_support & MLX5_FIELD_SUPPORT_MPLS_S_BOS)) 243171c6e863SAriel Levkovich return -EOPNOTSUPP; 243271c6e863SAriel Levkovich 243371c6e863SAriel Levkovich if (MLX5_GET(fte_match_mpls, set_mask, mpls_ttl) && 243471c6e863SAriel Levkovich !(field_support & MLX5_FIELD_SUPPORT_MPLS_TTL)) 243571c6e863SAriel Levkovich return -EOPNOTSUPP; 243671c6e863SAriel Levkovich 243771c6e863SAriel Levkovich return 0; 243871c6e863SAriel Levkovich } 243971c6e863SAriel Levkovich 2440c47ac6aeSMaor Gottlieb #define LAST_ETH_FIELD vlan_tag 2441c47ac6aeSMaor Gottlieb #define LAST_IB_FIELD sl 2442ca0d4753SMaor Gottlieb #define LAST_IPV4_FIELD tos 2443466fa6d2SMaor Gottlieb #define LAST_IPV6_FIELD traffic_class 2444c47ac6aeSMaor Gottlieb #define LAST_TCP_UDP_FIELD src_port 2445ffb30d8fSMoses Reuben #define LAST_TUNNEL_FIELD tunnel_id 24462ac693f9SMoses Reuben #define LAST_FLOW_TAG_FIELD tag_id 2447a22ed86cSSlava Shwartsman #define LAST_DROP_FIELD size 24483b3233fbSRaed Salem #define LAST_COUNTERS_FIELD counters 2449c47ac6aeSMaor Gottlieb 2450c47ac6aeSMaor Gottlieb /* Field is the last supported field */ 2451c47ac6aeSMaor Gottlieb #define FIELDS_NOT_SUPPORTED(filter, field)\ 2452c47ac6aeSMaor Gottlieb memchr_inv((void *)&filter.field +\ 2453c47ac6aeSMaor Gottlieb sizeof(filter.field), 0,\ 2454c47ac6aeSMaor Gottlieb sizeof(filter) -\ 2455c47ac6aeSMaor Gottlieb offsetof(typeof(filter), field) -\ 2456c47ac6aeSMaor Gottlieb sizeof(filter.field)) 2457c47ac6aeSMaor Gottlieb 2458802c2125SAviad Yehezkel static int parse_flow_flow_action(const union ib_flow_spec *ib_spec, 2459802c2125SAviad Yehezkel const struct ib_flow_attr *flow_attr, 2460802c2125SAviad Yehezkel struct mlx5_flow_act *action) 2461802c2125SAviad Yehezkel { 2462802c2125SAviad Yehezkel struct mlx5_ib_flow_action *maction = to_mflow_act(ib_spec->action.act); 2463802c2125SAviad Yehezkel 2464802c2125SAviad Yehezkel switch (maction->ib_action.type) { 2465802c2125SAviad Yehezkel case IB_FLOW_ACTION_ESP: 2466802c2125SAviad Yehezkel /* Currently only AES_GCM keymat is supported by the driver */ 2467802c2125SAviad Yehezkel action->esp_id = (uintptr_t)maction->esp_aes_gcm.ctx; 2468802c2125SAviad Yehezkel action->action |= flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS ? 2469802c2125SAviad Yehezkel MLX5_FLOW_CONTEXT_ACTION_ENCRYPT : 2470802c2125SAviad Yehezkel MLX5_FLOW_CONTEXT_ACTION_DECRYPT; 2471802c2125SAviad Yehezkel return 0; 2472802c2125SAviad Yehezkel default: 2473802c2125SAviad Yehezkel return -EOPNOTSUPP; 2474802c2125SAviad Yehezkel } 2475802c2125SAviad Yehezkel } 2476802c2125SAviad Yehezkel 247719cc7524SAriel Levkovich static int parse_flow_attr(struct mlx5_core_dev *mdev, u32 *match_c, 247819cc7524SAriel Levkovich u32 *match_v, const union ib_flow_spec *ib_spec, 2479802c2125SAviad Yehezkel const struct ib_flow_attr *flow_attr, 248071c6e863SAriel Levkovich struct mlx5_flow_act *action, u32 prev_type) 2481038d2ef8SMaor Gottlieb { 2482466fa6d2SMaor Gottlieb void *misc_params_c = MLX5_ADDR_OF(fte_match_param, match_c, 2483466fa6d2SMaor Gottlieb misc_parameters); 2484466fa6d2SMaor Gottlieb void *misc_params_v = MLX5_ADDR_OF(fte_match_param, match_v, 2485466fa6d2SMaor Gottlieb misc_parameters); 248671c6e863SAriel Levkovich void *misc_params2_c = MLX5_ADDR_OF(fte_match_param, match_c, 248771c6e863SAriel Levkovich misc_parameters_2); 248871c6e863SAriel Levkovich void *misc_params2_v = MLX5_ADDR_OF(fte_match_param, match_v, 248971c6e863SAriel Levkovich misc_parameters_2); 24902d1e697eSMoses Reuben void *headers_c; 24912d1e697eSMoses Reuben void *headers_v; 249219cc7524SAriel Levkovich int match_ipv; 2493802c2125SAviad Yehezkel int ret; 2494466fa6d2SMaor Gottlieb 24952d1e697eSMoses Reuben if (ib_spec->type & IB_FLOW_SPEC_INNER) { 24962d1e697eSMoses Reuben headers_c = MLX5_ADDR_OF(fte_match_param, match_c, 24972d1e697eSMoses Reuben inner_headers); 24982d1e697eSMoses Reuben headers_v = MLX5_ADDR_OF(fte_match_param, match_v, 24992d1e697eSMoses Reuben inner_headers); 250019cc7524SAriel Levkovich match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev, 250119cc7524SAriel Levkovich ft_field_support.inner_ip_version); 25022d1e697eSMoses Reuben } else { 25032d1e697eSMoses Reuben headers_c = MLX5_ADDR_OF(fte_match_param, match_c, 25042d1e697eSMoses Reuben outer_headers); 25052d1e697eSMoses Reuben headers_v = MLX5_ADDR_OF(fte_match_param, match_v, 25062d1e697eSMoses Reuben outer_headers); 250719cc7524SAriel Levkovich match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev, 250819cc7524SAriel Levkovich ft_field_support.outer_ip_version); 25092d1e697eSMoses Reuben } 25102d1e697eSMoses Reuben 25112d1e697eSMoses Reuben switch (ib_spec->type & ~IB_FLOW_SPEC_INNER) { 2512038d2ef8SMaor Gottlieb case IB_FLOW_SPEC_ETH: 2513c47ac6aeSMaor Gottlieb if (FIELDS_NOT_SUPPORTED(ib_spec->eth.mask, LAST_ETH_FIELD)) 25141ffd3a26SLeon Romanovsky return -EOPNOTSUPP; 2515038d2ef8SMaor Gottlieb 25162d1e697eSMoses Reuben ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c, 2517038d2ef8SMaor Gottlieb dmac_47_16), 2518038d2ef8SMaor Gottlieb ib_spec->eth.mask.dst_mac); 25192d1e697eSMoses Reuben ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v, 2520038d2ef8SMaor Gottlieb dmac_47_16), 2521038d2ef8SMaor Gottlieb ib_spec->eth.val.dst_mac); 2522038d2ef8SMaor Gottlieb 25232d1e697eSMoses Reuben ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c, 2524ee3da804SMaor Gottlieb smac_47_16), 2525ee3da804SMaor Gottlieb ib_spec->eth.mask.src_mac); 25262d1e697eSMoses Reuben ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v, 2527ee3da804SMaor Gottlieb smac_47_16), 2528ee3da804SMaor Gottlieb ib_spec->eth.val.src_mac); 2529ee3da804SMaor Gottlieb 2530038d2ef8SMaor Gottlieb if (ib_spec->eth.mask.vlan_tag) { 25312d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_c, 253210543365SMohamad Haj Yahia cvlan_tag, 1); 25332d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_v, 253410543365SMohamad Haj Yahia cvlan_tag, 1); 2535038d2ef8SMaor Gottlieb 25362d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_c, 2537038d2ef8SMaor Gottlieb first_vid, ntohs(ib_spec->eth.mask.vlan_tag)); 25382d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_v, 2539038d2ef8SMaor Gottlieb first_vid, ntohs(ib_spec->eth.val.vlan_tag)); 2540038d2ef8SMaor Gottlieb 25412d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_c, 2542038d2ef8SMaor Gottlieb first_cfi, 2543038d2ef8SMaor Gottlieb ntohs(ib_spec->eth.mask.vlan_tag) >> 12); 25442d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_v, 2545038d2ef8SMaor Gottlieb first_cfi, 2546038d2ef8SMaor Gottlieb ntohs(ib_spec->eth.val.vlan_tag) >> 12); 2547038d2ef8SMaor Gottlieb 25482d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_c, 2549038d2ef8SMaor Gottlieb first_prio, 2550038d2ef8SMaor Gottlieb ntohs(ib_spec->eth.mask.vlan_tag) >> 13); 25512d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_v, 2552038d2ef8SMaor Gottlieb first_prio, 2553038d2ef8SMaor Gottlieb ntohs(ib_spec->eth.val.vlan_tag) >> 13); 2554038d2ef8SMaor Gottlieb } 25552d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_c, 2556038d2ef8SMaor Gottlieb ethertype, ntohs(ib_spec->eth.mask.ether_type)); 25572d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_v, 2558038d2ef8SMaor Gottlieb ethertype, ntohs(ib_spec->eth.val.ether_type)); 2559038d2ef8SMaor Gottlieb break; 2560038d2ef8SMaor Gottlieb case IB_FLOW_SPEC_IPV4: 2561c47ac6aeSMaor Gottlieb if (FIELDS_NOT_SUPPORTED(ib_spec->ipv4.mask, LAST_IPV4_FIELD)) 25621ffd3a26SLeon Romanovsky return -EOPNOTSUPP; 2563038d2ef8SMaor Gottlieb 256419cc7524SAriel Levkovich if (match_ipv) { 256519cc7524SAriel Levkovich MLX5_SET(fte_match_set_lyr_2_4, headers_c, 256619cc7524SAriel Levkovich ip_version, 0xf); 256719cc7524SAriel Levkovich MLX5_SET(fte_match_set_lyr_2_4, headers_v, 25683346c487SBoris Pismenny ip_version, MLX5_FS_IPV4_VERSION); 256919cc7524SAriel Levkovich } else { 25702d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_c, 2571038d2ef8SMaor Gottlieb ethertype, 0xffff); 25722d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_v, 2573038d2ef8SMaor Gottlieb ethertype, ETH_P_IP); 257419cc7524SAriel Levkovich } 2575038d2ef8SMaor Gottlieb 25762d1e697eSMoses Reuben memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c, 2577038d2ef8SMaor Gottlieb src_ipv4_src_ipv6.ipv4_layout.ipv4), 2578038d2ef8SMaor Gottlieb &ib_spec->ipv4.mask.src_ip, 2579038d2ef8SMaor Gottlieb sizeof(ib_spec->ipv4.mask.src_ip)); 25802d1e697eSMoses Reuben memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v, 2581038d2ef8SMaor Gottlieb src_ipv4_src_ipv6.ipv4_layout.ipv4), 2582038d2ef8SMaor Gottlieb &ib_spec->ipv4.val.src_ip, 2583038d2ef8SMaor Gottlieb sizeof(ib_spec->ipv4.val.src_ip)); 25842d1e697eSMoses Reuben memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c, 2585038d2ef8SMaor Gottlieb dst_ipv4_dst_ipv6.ipv4_layout.ipv4), 2586038d2ef8SMaor Gottlieb &ib_spec->ipv4.mask.dst_ip, 2587038d2ef8SMaor Gottlieb sizeof(ib_spec->ipv4.mask.dst_ip)); 25882d1e697eSMoses Reuben memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v, 2589038d2ef8SMaor Gottlieb dst_ipv4_dst_ipv6.ipv4_layout.ipv4), 2590038d2ef8SMaor Gottlieb &ib_spec->ipv4.val.dst_ip, 2591038d2ef8SMaor Gottlieb sizeof(ib_spec->ipv4.val.dst_ip)); 2592ca0d4753SMaor Gottlieb 25932d1e697eSMoses Reuben set_tos(headers_c, headers_v, 2594ca0d4753SMaor Gottlieb ib_spec->ipv4.mask.tos, ib_spec->ipv4.val.tos); 2595ca0d4753SMaor Gottlieb 25962d1e697eSMoses Reuben set_proto(headers_c, headers_v, 2597ca0d4753SMaor Gottlieb ib_spec->ipv4.mask.proto, ib_spec->ipv4.val.proto); 2598038d2ef8SMaor Gottlieb break; 2599026bae0cSMaor Gottlieb case IB_FLOW_SPEC_IPV6: 2600c47ac6aeSMaor Gottlieb if (FIELDS_NOT_SUPPORTED(ib_spec->ipv6.mask, LAST_IPV6_FIELD)) 26011ffd3a26SLeon Romanovsky return -EOPNOTSUPP; 2602026bae0cSMaor Gottlieb 260319cc7524SAriel Levkovich if (match_ipv) { 260419cc7524SAriel Levkovich MLX5_SET(fte_match_set_lyr_2_4, headers_c, 260519cc7524SAriel Levkovich ip_version, 0xf); 260619cc7524SAriel Levkovich MLX5_SET(fte_match_set_lyr_2_4, headers_v, 26073346c487SBoris Pismenny ip_version, MLX5_FS_IPV6_VERSION); 260819cc7524SAriel Levkovich } else { 26092d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_c, 2610026bae0cSMaor Gottlieb ethertype, 0xffff); 26112d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_v, 2612026bae0cSMaor Gottlieb ethertype, ETH_P_IPV6); 261319cc7524SAriel Levkovich } 2614026bae0cSMaor Gottlieb 26152d1e697eSMoses Reuben memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c, 2616026bae0cSMaor Gottlieb src_ipv4_src_ipv6.ipv6_layout.ipv6), 2617026bae0cSMaor Gottlieb &ib_spec->ipv6.mask.src_ip, 2618026bae0cSMaor Gottlieb sizeof(ib_spec->ipv6.mask.src_ip)); 26192d1e697eSMoses Reuben memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v, 2620026bae0cSMaor Gottlieb src_ipv4_src_ipv6.ipv6_layout.ipv6), 2621026bae0cSMaor Gottlieb &ib_spec->ipv6.val.src_ip, 2622026bae0cSMaor Gottlieb sizeof(ib_spec->ipv6.val.src_ip)); 26232d1e697eSMoses Reuben memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c, 2624026bae0cSMaor Gottlieb dst_ipv4_dst_ipv6.ipv6_layout.ipv6), 2625026bae0cSMaor Gottlieb &ib_spec->ipv6.mask.dst_ip, 2626026bae0cSMaor Gottlieb sizeof(ib_spec->ipv6.mask.dst_ip)); 26272d1e697eSMoses Reuben memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v, 2628026bae0cSMaor Gottlieb dst_ipv4_dst_ipv6.ipv6_layout.ipv6), 2629026bae0cSMaor Gottlieb &ib_spec->ipv6.val.dst_ip, 2630026bae0cSMaor Gottlieb sizeof(ib_spec->ipv6.val.dst_ip)); 2631466fa6d2SMaor Gottlieb 26322d1e697eSMoses Reuben set_tos(headers_c, headers_v, 2633466fa6d2SMaor Gottlieb ib_spec->ipv6.mask.traffic_class, 2634466fa6d2SMaor Gottlieb ib_spec->ipv6.val.traffic_class); 2635466fa6d2SMaor Gottlieb 26362d1e697eSMoses Reuben set_proto(headers_c, headers_v, 2637466fa6d2SMaor Gottlieb ib_spec->ipv6.mask.next_hdr, 2638466fa6d2SMaor Gottlieb ib_spec->ipv6.val.next_hdr); 2639466fa6d2SMaor Gottlieb 26402d1e697eSMoses Reuben set_flow_label(misc_params_c, misc_params_v, 26412d1e697eSMoses Reuben ntohl(ib_spec->ipv6.mask.flow_label), 26422d1e697eSMoses Reuben ntohl(ib_spec->ipv6.val.flow_label), 26432d1e697eSMoses Reuben ib_spec->type & IB_FLOW_SPEC_INNER); 2644802c2125SAviad Yehezkel break; 2645802c2125SAviad Yehezkel case IB_FLOW_SPEC_ESP: 2646802c2125SAviad Yehezkel if (ib_spec->esp.mask.seq) 2647802c2125SAviad Yehezkel return -EOPNOTSUPP; 26482d1e697eSMoses Reuben 2649802c2125SAviad Yehezkel MLX5_SET(fte_match_set_misc, misc_params_c, outer_esp_spi, 2650802c2125SAviad Yehezkel ntohl(ib_spec->esp.mask.spi)); 2651802c2125SAviad Yehezkel MLX5_SET(fte_match_set_misc, misc_params_v, outer_esp_spi, 2652802c2125SAviad Yehezkel ntohl(ib_spec->esp.val.spi)); 2653026bae0cSMaor Gottlieb break; 2654038d2ef8SMaor Gottlieb case IB_FLOW_SPEC_TCP: 2655c47ac6aeSMaor Gottlieb if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask, 2656c47ac6aeSMaor Gottlieb LAST_TCP_UDP_FIELD)) 26571ffd3a26SLeon Romanovsky return -EOPNOTSUPP; 2658038d2ef8SMaor Gottlieb 26592d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol, 2660038d2ef8SMaor Gottlieb 0xff); 26612d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol, 2662038d2ef8SMaor Gottlieb IPPROTO_TCP); 2663038d2ef8SMaor Gottlieb 26642d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_sport, 2665038d2ef8SMaor Gottlieb ntohs(ib_spec->tcp_udp.mask.src_port)); 26662d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_sport, 2667038d2ef8SMaor Gottlieb ntohs(ib_spec->tcp_udp.val.src_port)); 2668038d2ef8SMaor Gottlieb 26692d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_dport, 2670038d2ef8SMaor Gottlieb ntohs(ib_spec->tcp_udp.mask.dst_port)); 26712d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_dport, 2672038d2ef8SMaor Gottlieb ntohs(ib_spec->tcp_udp.val.dst_port)); 2673038d2ef8SMaor Gottlieb break; 2674038d2ef8SMaor Gottlieb case IB_FLOW_SPEC_UDP: 2675c47ac6aeSMaor Gottlieb if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask, 2676c47ac6aeSMaor Gottlieb LAST_TCP_UDP_FIELD)) 26771ffd3a26SLeon Romanovsky return -EOPNOTSUPP; 2678038d2ef8SMaor Gottlieb 26792d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol, 2680038d2ef8SMaor Gottlieb 0xff); 26812d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol, 2682038d2ef8SMaor Gottlieb IPPROTO_UDP); 2683038d2ef8SMaor Gottlieb 26842d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_sport, 2685038d2ef8SMaor Gottlieb ntohs(ib_spec->tcp_udp.mask.src_port)); 26862d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_sport, 2687038d2ef8SMaor Gottlieb ntohs(ib_spec->tcp_udp.val.src_port)); 2688038d2ef8SMaor Gottlieb 26892d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_dport, 2690038d2ef8SMaor Gottlieb ntohs(ib_spec->tcp_udp.mask.dst_port)); 26912d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport, 2692038d2ef8SMaor Gottlieb ntohs(ib_spec->tcp_udp.val.dst_port)); 2693038d2ef8SMaor Gottlieb break; 2694da2f22aeSAriel Levkovich case IB_FLOW_SPEC_GRE: 2695da2f22aeSAriel Levkovich if (ib_spec->gre.mask.c_ks_res0_ver) 2696da2f22aeSAriel Levkovich return -EOPNOTSUPP; 2697da2f22aeSAriel Levkovich 2698da2f22aeSAriel Levkovich MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol, 2699da2f22aeSAriel Levkovich 0xff); 2700da2f22aeSAriel Levkovich MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol, 2701da2f22aeSAriel Levkovich IPPROTO_GRE); 2702da2f22aeSAriel Levkovich 2703da2f22aeSAriel Levkovich MLX5_SET(fte_match_set_misc, misc_params_c, gre_protocol, 2704a93b632cSMaor Gottlieb ntohs(ib_spec->gre.mask.protocol)); 2705da2f22aeSAriel Levkovich MLX5_SET(fte_match_set_misc, misc_params_v, gre_protocol, 2706da2f22aeSAriel Levkovich ntohs(ib_spec->gre.val.protocol)); 2707da2f22aeSAriel Levkovich 2708da2f22aeSAriel Levkovich memcpy(MLX5_ADDR_OF(fte_match_set_misc, misc_params_c, 2709da2f22aeSAriel Levkovich gre_key_h), 2710da2f22aeSAriel Levkovich &ib_spec->gre.mask.key, 2711da2f22aeSAriel Levkovich sizeof(ib_spec->gre.mask.key)); 2712da2f22aeSAriel Levkovich memcpy(MLX5_ADDR_OF(fte_match_set_misc, misc_params_v, 2713da2f22aeSAriel Levkovich gre_key_h), 2714da2f22aeSAriel Levkovich &ib_spec->gre.val.key, 2715da2f22aeSAriel Levkovich sizeof(ib_spec->gre.val.key)); 2716da2f22aeSAriel Levkovich break; 271771c6e863SAriel Levkovich case IB_FLOW_SPEC_MPLS: 271871c6e863SAriel Levkovich switch (prev_type) { 271971c6e863SAriel Levkovich case IB_FLOW_SPEC_UDP: 272071c6e863SAriel Levkovich if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev, 272171c6e863SAriel Levkovich ft_field_support.outer_first_mpls_over_udp), 272271c6e863SAriel Levkovich &ib_spec->mpls.mask.tag)) 272371c6e863SAriel Levkovich return -EOPNOTSUPP; 272471c6e863SAriel Levkovich 272571c6e863SAriel Levkovich memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v, 272671c6e863SAriel Levkovich outer_first_mpls_over_udp), 272771c6e863SAriel Levkovich &ib_spec->mpls.val.tag, 272871c6e863SAriel Levkovich sizeof(ib_spec->mpls.val.tag)); 272971c6e863SAriel Levkovich memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c, 273071c6e863SAriel Levkovich outer_first_mpls_over_udp), 273171c6e863SAriel Levkovich &ib_spec->mpls.mask.tag, 273271c6e863SAriel Levkovich sizeof(ib_spec->mpls.mask.tag)); 273371c6e863SAriel Levkovich break; 273471c6e863SAriel Levkovich case IB_FLOW_SPEC_GRE: 273571c6e863SAriel Levkovich if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev, 273671c6e863SAriel Levkovich ft_field_support.outer_first_mpls_over_gre), 273771c6e863SAriel Levkovich &ib_spec->mpls.mask.tag)) 273871c6e863SAriel Levkovich return -EOPNOTSUPP; 273971c6e863SAriel Levkovich 274071c6e863SAriel Levkovich memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v, 274171c6e863SAriel Levkovich outer_first_mpls_over_gre), 274271c6e863SAriel Levkovich &ib_spec->mpls.val.tag, 274371c6e863SAriel Levkovich sizeof(ib_spec->mpls.val.tag)); 274471c6e863SAriel Levkovich memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c, 274571c6e863SAriel Levkovich outer_first_mpls_over_gre), 274671c6e863SAriel Levkovich &ib_spec->mpls.mask.tag, 274771c6e863SAriel Levkovich sizeof(ib_spec->mpls.mask.tag)); 274871c6e863SAriel Levkovich break; 274971c6e863SAriel Levkovich default: 275071c6e863SAriel Levkovich if (ib_spec->type & IB_FLOW_SPEC_INNER) { 275171c6e863SAriel Levkovich if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev, 275271c6e863SAriel Levkovich ft_field_support.inner_first_mpls), 275371c6e863SAriel Levkovich &ib_spec->mpls.mask.tag)) 275471c6e863SAriel Levkovich return -EOPNOTSUPP; 275571c6e863SAriel Levkovich 275671c6e863SAriel Levkovich memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v, 275771c6e863SAriel Levkovich inner_first_mpls), 275871c6e863SAriel Levkovich &ib_spec->mpls.val.tag, 275971c6e863SAriel Levkovich sizeof(ib_spec->mpls.val.tag)); 276071c6e863SAriel Levkovich memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c, 276171c6e863SAriel Levkovich inner_first_mpls), 276271c6e863SAriel Levkovich &ib_spec->mpls.mask.tag, 276371c6e863SAriel Levkovich sizeof(ib_spec->mpls.mask.tag)); 276471c6e863SAriel Levkovich } else { 276571c6e863SAriel Levkovich if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev, 276671c6e863SAriel Levkovich ft_field_support.outer_first_mpls), 276771c6e863SAriel Levkovich &ib_spec->mpls.mask.tag)) 276871c6e863SAriel Levkovich return -EOPNOTSUPP; 276971c6e863SAriel Levkovich 277071c6e863SAriel Levkovich memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v, 277171c6e863SAriel Levkovich outer_first_mpls), 277271c6e863SAriel Levkovich &ib_spec->mpls.val.tag, 277371c6e863SAriel Levkovich sizeof(ib_spec->mpls.val.tag)); 277471c6e863SAriel Levkovich memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c, 277571c6e863SAriel Levkovich outer_first_mpls), 277671c6e863SAriel Levkovich &ib_spec->mpls.mask.tag, 277771c6e863SAriel Levkovich sizeof(ib_spec->mpls.mask.tag)); 277871c6e863SAriel Levkovich } 277971c6e863SAriel Levkovich } 278071c6e863SAriel Levkovich break; 2781ffb30d8fSMoses Reuben case IB_FLOW_SPEC_VXLAN_TUNNEL: 2782ffb30d8fSMoses Reuben if (FIELDS_NOT_SUPPORTED(ib_spec->tunnel.mask, 2783ffb30d8fSMoses Reuben LAST_TUNNEL_FIELD)) 27841ffd3a26SLeon Romanovsky return -EOPNOTSUPP; 2785ffb30d8fSMoses Reuben 2786ffb30d8fSMoses Reuben MLX5_SET(fte_match_set_misc, misc_params_c, vxlan_vni, 2787ffb30d8fSMoses Reuben ntohl(ib_spec->tunnel.mask.tunnel_id)); 2788ffb30d8fSMoses Reuben MLX5_SET(fte_match_set_misc, misc_params_v, vxlan_vni, 2789ffb30d8fSMoses Reuben ntohl(ib_spec->tunnel.val.tunnel_id)); 2790ffb30d8fSMoses Reuben break; 27912ac693f9SMoses Reuben case IB_FLOW_SPEC_ACTION_TAG: 27922ac693f9SMoses Reuben if (FIELDS_NOT_SUPPORTED(ib_spec->flow_tag, 27932ac693f9SMoses Reuben LAST_FLOW_TAG_FIELD)) 27942ac693f9SMoses Reuben return -EOPNOTSUPP; 27952ac693f9SMoses Reuben if (ib_spec->flow_tag.tag_id >= BIT(24)) 27962ac693f9SMoses Reuben return -EINVAL; 27972ac693f9SMoses Reuben 2798075572d4SBoris Pismenny action->flow_tag = ib_spec->flow_tag.tag_id; 2799a9db0ecfSMatan Barak action->has_flow_tag = true; 28002ac693f9SMoses Reuben break; 2801a22ed86cSSlava Shwartsman case IB_FLOW_SPEC_ACTION_DROP: 2802a22ed86cSSlava Shwartsman if (FIELDS_NOT_SUPPORTED(ib_spec->drop, 2803a22ed86cSSlava Shwartsman LAST_DROP_FIELD)) 2804a22ed86cSSlava Shwartsman return -EOPNOTSUPP; 2805075572d4SBoris Pismenny action->action |= MLX5_FLOW_CONTEXT_ACTION_DROP; 2806a22ed86cSSlava Shwartsman break; 2807802c2125SAviad Yehezkel case IB_FLOW_SPEC_ACTION_HANDLE: 2808802c2125SAviad Yehezkel ret = parse_flow_flow_action(ib_spec, flow_attr, action); 2809802c2125SAviad Yehezkel if (ret) 2810802c2125SAviad Yehezkel return ret; 2811802c2125SAviad Yehezkel break; 28123b3233fbSRaed Salem case IB_FLOW_SPEC_ACTION_COUNT: 28133b3233fbSRaed Salem if (FIELDS_NOT_SUPPORTED(ib_spec->flow_count, 28143b3233fbSRaed Salem LAST_COUNTERS_FIELD)) 28153b3233fbSRaed Salem return -EOPNOTSUPP; 28163b3233fbSRaed Salem 28173b3233fbSRaed Salem /* for now support only one counters spec per flow */ 28183b3233fbSRaed Salem if (action->action & MLX5_FLOW_CONTEXT_ACTION_COUNT) 28193b3233fbSRaed Salem return -EINVAL; 28203b3233fbSRaed Salem 28213b3233fbSRaed Salem action->counters = ib_spec->flow_count.counters; 28223b3233fbSRaed Salem action->action |= MLX5_FLOW_CONTEXT_ACTION_COUNT; 28233b3233fbSRaed Salem break; 2824038d2ef8SMaor Gottlieb default: 2825038d2ef8SMaor Gottlieb return -EINVAL; 2826038d2ef8SMaor Gottlieb } 2827038d2ef8SMaor Gottlieb 2828038d2ef8SMaor Gottlieb return 0; 2829038d2ef8SMaor Gottlieb } 2830038d2ef8SMaor Gottlieb 2831038d2ef8SMaor Gottlieb /* If a flow could catch both multicast and unicast packets, 2832038d2ef8SMaor Gottlieb * it won't fall into the multicast flow steering table and this rule 2833038d2ef8SMaor Gottlieb * could steal other multicast packets. 2834038d2ef8SMaor Gottlieb */ 2835a550ddfcSYishai Hadas static bool flow_is_multicast_only(const struct ib_flow_attr *ib_attr) 2836038d2ef8SMaor Gottlieb { 283781e30880SYishai Hadas union ib_flow_spec *flow_spec; 2838038d2ef8SMaor Gottlieb 2839038d2ef8SMaor Gottlieb if (ib_attr->type != IB_FLOW_ATTR_NORMAL || 2840038d2ef8SMaor Gottlieb ib_attr->num_of_specs < 1) 2841038d2ef8SMaor Gottlieb return false; 2842038d2ef8SMaor Gottlieb 284381e30880SYishai Hadas flow_spec = (union ib_flow_spec *)(ib_attr + 1); 284481e30880SYishai Hadas if (flow_spec->type == IB_FLOW_SPEC_IPV4) { 284581e30880SYishai Hadas struct ib_flow_spec_ipv4 *ipv4_spec; 2846038d2ef8SMaor Gottlieb 284781e30880SYishai Hadas ipv4_spec = (struct ib_flow_spec_ipv4 *)flow_spec; 284881e30880SYishai Hadas if (ipv4_is_multicast(ipv4_spec->val.dst_ip)) 284981e30880SYishai Hadas return true; 285081e30880SYishai Hadas 285181e30880SYishai Hadas return false; 285281e30880SYishai Hadas } 285381e30880SYishai Hadas 285481e30880SYishai Hadas if (flow_spec->type == IB_FLOW_SPEC_ETH) { 285581e30880SYishai Hadas struct ib_flow_spec_eth *eth_spec; 285681e30880SYishai Hadas 285781e30880SYishai Hadas eth_spec = (struct ib_flow_spec_eth *)flow_spec; 2858038d2ef8SMaor Gottlieb return is_multicast_ether_addr(eth_spec->mask.dst_mac) && 2859038d2ef8SMaor Gottlieb is_multicast_ether_addr(eth_spec->val.dst_mac); 2860038d2ef8SMaor Gottlieb } 2861038d2ef8SMaor Gottlieb 286281e30880SYishai Hadas return false; 286381e30880SYishai Hadas } 286481e30880SYishai Hadas 2865802c2125SAviad Yehezkel enum valid_spec { 2866802c2125SAviad Yehezkel VALID_SPEC_INVALID, 2867802c2125SAviad Yehezkel VALID_SPEC_VALID, 2868802c2125SAviad Yehezkel VALID_SPEC_NA, 2869802c2125SAviad Yehezkel }; 2870802c2125SAviad Yehezkel 2871802c2125SAviad Yehezkel static enum valid_spec 2872802c2125SAviad Yehezkel is_valid_esp_aes_gcm(struct mlx5_core_dev *mdev, 2873802c2125SAviad Yehezkel const struct mlx5_flow_spec *spec, 2874802c2125SAviad Yehezkel const struct mlx5_flow_act *flow_act, 2875802c2125SAviad Yehezkel bool egress) 2876802c2125SAviad Yehezkel { 2877802c2125SAviad Yehezkel const u32 *match_c = spec->match_criteria; 2878802c2125SAviad Yehezkel bool is_crypto = 2879802c2125SAviad Yehezkel (flow_act->action & (MLX5_FLOW_CONTEXT_ACTION_ENCRYPT | 2880802c2125SAviad Yehezkel MLX5_FLOW_CONTEXT_ACTION_DECRYPT)); 2881802c2125SAviad Yehezkel bool is_ipsec = mlx5_fs_is_ipsec_flow(match_c); 2882802c2125SAviad Yehezkel bool is_drop = flow_act->action & MLX5_FLOW_CONTEXT_ACTION_DROP; 2883802c2125SAviad Yehezkel 2884802c2125SAviad Yehezkel /* 2885802c2125SAviad Yehezkel * Currently only crypto is supported in egress, when regular egress 2886802c2125SAviad Yehezkel * rules would be supported, always return VALID_SPEC_NA. 2887802c2125SAviad Yehezkel */ 2888802c2125SAviad Yehezkel if (!is_crypto) 2889802c2125SAviad Yehezkel return egress ? VALID_SPEC_INVALID : VALID_SPEC_NA; 2890802c2125SAviad Yehezkel 2891802c2125SAviad Yehezkel return is_crypto && is_ipsec && 2892802c2125SAviad Yehezkel (!egress || (!is_drop && !flow_act->has_flow_tag)) ? 2893802c2125SAviad Yehezkel VALID_SPEC_VALID : VALID_SPEC_INVALID; 2894802c2125SAviad Yehezkel } 2895802c2125SAviad Yehezkel 2896802c2125SAviad Yehezkel static bool is_valid_spec(struct mlx5_core_dev *mdev, 2897802c2125SAviad Yehezkel const struct mlx5_flow_spec *spec, 2898802c2125SAviad Yehezkel const struct mlx5_flow_act *flow_act, 2899802c2125SAviad Yehezkel bool egress) 2900802c2125SAviad Yehezkel { 2901802c2125SAviad Yehezkel /* We curretly only support ipsec egress flow */ 2902802c2125SAviad Yehezkel return is_valid_esp_aes_gcm(mdev, spec, flow_act, egress) != VALID_SPEC_INVALID; 2903802c2125SAviad Yehezkel } 2904802c2125SAviad Yehezkel 290519cc7524SAriel Levkovich static bool is_valid_ethertype(struct mlx5_core_dev *mdev, 290619cc7524SAriel Levkovich const struct ib_flow_attr *flow_attr, 29070f750966SAriel Levkovich bool check_inner) 2908038d2ef8SMaor Gottlieb { 2909038d2ef8SMaor Gottlieb union ib_flow_spec *ib_spec = (union ib_flow_spec *)(flow_attr + 1); 291019cc7524SAriel Levkovich int match_ipv = check_inner ? 291119cc7524SAriel Levkovich MLX5_CAP_FLOWTABLE_NIC_RX(mdev, 291219cc7524SAriel Levkovich ft_field_support.inner_ip_version) : 291319cc7524SAriel Levkovich MLX5_CAP_FLOWTABLE_NIC_RX(mdev, 291419cc7524SAriel Levkovich ft_field_support.outer_ip_version); 29150f750966SAriel Levkovich int inner_bit = check_inner ? IB_FLOW_SPEC_INNER : 0; 29160f750966SAriel Levkovich bool ipv4_spec_valid, ipv6_spec_valid; 29170f750966SAriel Levkovich unsigned int ip_spec_type = 0; 29180f750966SAriel Levkovich bool has_ethertype = false; 2919038d2ef8SMaor Gottlieb unsigned int spec_index; 29200f750966SAriel Levkovich bool mask_valid = true; 29210f750966SAriel Levkovich u16 eth_type = 0; 29220f750966SAriel Levkovich bool type_valid; 2923038d2ef8SMaor Gottlieb 2924038d2ef8SMaor Gottlieb /* Validate that ethertype is correct */ 2925038d2ef8SMaor Gottlieb for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) { 29260f750966SAriel Levkovich if ((ib_spec->type == (IB_FLOW_SPEC_ETH | inner_bit)) && 2927038d2ef8SMaor Gottlieb ib_spec->eth.mask.ether_type) { 29280f750966SAriel Levkovich mask_valid = (ib_spec->eth.mask.ether_type == 29290f750966SAriel Levkovich htons(0xffff)); 29300f750966SAriel Levkovich has_ethertype = true; 29310f750966SAriel Levkovich eth_type = ntohs(ib_spec->eth.val.ether_type); 29320f750966SAriel Levkovich } else if ((ib_spec->type == (IB_FLOW_SPEC_IPV4 | inner_bit)) || 29330f750966SAriel Levkovich (ib_spec->type == (IB_FLOW_SPEC_IPV6 | inner_bit))) { 29340f750966SAriel Levkovich ip_spec_type = ib_spec->type; 2935038d2ef8SMaor Gottlieb } 2936038d2ef8SMaor Gottlieb ib_spec = (void *)ib_spec + ib_spec->size; 2937038d2ef8SMaor Gottlieb } 29380f750966SAriel Levkovich 29390f750966SAriel Levkovich type_valid = (!has_ethertype) || (!ip_spec_type); 29400f750966SAriel Levkovich if (!type_valid && mask_valid) { 29410f750966SAriel Levkovich ipv4_spec_valid = (eth_type == ETH_P_IP) && 29420f750966SAriel Levkovich (ip_spec_type == (IB_FLOW_SPEC_IPV4 | inner_bit)); 29430f750966SAriel Levkovich ipv6_spec_valid = (eth_type == ETH_P_IPV6) && 29440f750966SAriel Levkovich (ip_spec_type == (IB_FLOW_SPEC_IPV6 | inner_bit)); 294519cc7524SAriel Levkovich 294619cc7524SAriel Levkovich type_valid = (ipv4_spec_valid) || (ipv6_spec_valid) || 294719cc7524SAriel Levkovich (((eth_type == ETH_P_MPLS_UC) || 294819cc7524SAriel Levkovich (eth_type == ETH_P_MPLS_MC)) && match_ipv); 29490f750966SAriel Levkovich } 29500f750966SAriel Levkovich 29510f750966SAriel Levkovich return type_valid; 29520f750966SAriel Levkovich } 29530f750966SAriel Levkovich 295419cc7524SAriel Levkovich static bool is_valid_attr(struct mlx5_core_dev *mdev, 295519cc7524SAriel Levkovich const struct ib_flow_attr *flow_attr) 29560f750966SAriel Levkovich { 295719cc7524SAriel Levkovich return is_valid_ethertype(mdev, flow_attr, false) && 295819cc7524SAriel Levkovich is_valid_ethertype(mdev, flow_attr, true); 2959038d2ef8SMaor Gottlieb } 2960038d2ef8SMaor Gottlieb 2961038d2ef8SMaor Gottlieb static void put_flow_table(struct mlx5_ib_dev *dev, 2962038d2ef8SMaor Gottlieb struct mlx5_ib_flow_prio *prio, bool ft_added) 2963038d2ef8SMaor Gottlieb { 2964038d2ef8SMaor Gottlieb prio->refcount -= !!ft_added; 2965038d2ef8SMaor Gottlieb if (!prio->refcount) { 2966038d2ef8SMaor Gottlieb mlx5_destroy_flow_table(prio->flow_table); 2967038d2ef8SMaor Gottlieb prio->flow_table = NULL; 2968038d2ef8SMaor Gottlieb } 2969038d2ef8SMaor Gottlieb } 2970038d2ef8SMaor Gottlieb 29713b3233fbSRaed Salem static void counters_clear_description(struct ib_counters *counters) 29723b3233fbSRaed Salem { 29733b3233fbSRaed Salem struct mlx5_ib_mcounters *mcounters = to_mcounters(counters); 29743b3233fbSRaed Salem 29753b3233fbSRaed Salem mutex_lock(&mcounters->mcntrs_mutex); 29763b3233fbSRaed Salem kfree(mcounters->counters_data); 29773b3233fbSRaed Salem mcounters->counters_data = NULL; 29783b3233fbSRaed Salem mcounters->cntrs_max_index = 0; 29793b3233fbSRaed Salem mutex_unlock(&mcounters->mcntrs_mutex); 29803b3233fbSRaed Salem } 29813b3233fbSRaed Salem 2982038d2ef8SMaor Gottlieb static int mlx5_ib_destroy_flow(struct ib_flow *flow_id) 2983038d2ef8SMaor Gottlieb { 2984038d2ef8SMaor Gottlieb struct mlx5_ib_dev *dev = to_mdev(flow_id->qp->device); 2985038d2ef8SMaor Gottlieb struct mlx5_ib_flow_handler *handler = container_of(flow_id, 2986038d2ef8SMaor Gottlieb struct mlx5_ib_flow_handler, 2987038d2ef8SMaor Gottlieb ibflow); 2988038d2ef8SMaor Gottlieb struct mlx5_ib_flow_handler *iter, *tmp; 2989038d2ef8SMaor Gottlieb 29909a4ca38dSMark Bloch mutex_lock(&dev->flow_db->lock); 2991038d2ef8SMaor Gottlieb 2992038d2ef8SMaor Gottlieb list_for_each_entry_safe(iter, tmp, &handler->list, list) { 299374491de9SMark Bloch mlx5_del_flow_rules(iter->rule); 2994cc0e5d42SMaor Gottlieb put_flow_table(dev, iter->prio, true); 2995038d2ef8SMaor Gottlieb list_del(&iter->list); 2996038d2ef8SMaor Gottlieb kfree(iter); 2997038d2ef8SMaor Gottlieb } 2998038d2ef8SMaor Gottlieb 299974491de9SMark Bloch mlx5_del_flow_rules(handler->rule); 30005497adc6SMaor Gottlieb put_flow_table(dev, handler->prio, true); 30013b3233fbSRaed Salem if (handler->ibcounters && 30023b3233fbSRaed Salem atomic_read(&handler->ibcounters->usecnt) == 1) 30033b3233fbSRaed Salem counters_clear_description(handler->ibcounters); 3004038d2ef8SMaor Gottlieb 30053b3233fbSRaed Salem mutex_unlock(&dev->flow_db->lock); 3006038d2ef8SMaor Gottlieb kfree(handler); 3007038d2ef8SMaor Gottlieb 3008038d2ef8SMaor Gottlieb return 0; 3009038d2ef8SMaor Gottlieb } 3010038d2ef8SMaor Gottlieb 301135d19011SMaor Gottlieb static int ib_prio_to_core_prio(unsigned int priority, bool dont_trap) 301235d19011SMaor Gottlieb { 301335d19011SMaor Gottlieb priority *= 2; 301435d19011SMaor Gottlieb if (!dont_trap) 301535d19011SMaor Gottlieb priority++; 301635d19011SMaor Gottlieb return priority; 301735d19011SMaor Gottlieb } 301835d19011SMaor Gottlieb 3019cc0e5d42SMaor Gottlieb enum flow_table_type { 3020cc0e5d42SMaor Gottlieb MLX5_IB_FT_RX, 3021cc0e5d42SMaor Gottlieb MLX5_IB_FT_TX 3022cc0e5d42SMaor Gottlieb }; 3023cc0e5d42SMaor Gottlieb 302400b7c2abSMaor Gottlieb #define MLX5_FS_MAX_TYPES 6 302500b7c2abSMaor Gottlieb #define MLX5_FS_MAX_ENTRIES BIT(16) 3026038d2ef8SMaor Gottlieb static struct mlx5_ib_flow_prio *get_flow_table(struct mlx5_ib_dev *dev, 3027cc0e5d42SMaor Gottlieb struct ib_flow_attr *flow_attr, 3028cc0e5d42SMaor Gottlieb enum flow_table_type ft_type) 3029038d2ef8SMaor Gottlieb { 303035d19011SMaor Gottlieb bool dont_trap = flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP; 3031038d2ef8SMaor Gottlieb struct mlx5_flow_namespace *ns = NULL; 3032038d2ef8SMaor Gottlieb struct mlx5_ib_flow_prio *prio; 3033038d2ef8SMaor Gottlieb struct mlx5_flow_table *ft; 3034dac388efSMaor Gottlieb int max_table_size; 3035038d2ef8SMaor Gottlieb int num_entries; 3036038d2ef8SMaor Gottlieb int num_groups; 3037038d2ef8SMaor Gottlieb int priority; 3038038d2ef8SMaor Gottlieb int err = 0; 3039038d2ef8SMaor Gottlieb 3040dac388efSMaor Gottlieb max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, 3041dac388efSMaor Gottlieb log_max_ft_size)); 3042038d2ef8SMaor Gottlieb if (flow_attr->type == IB_FLOW_ATTR_NORMAL) { 3043802c2125SAviad Yehezkel if (ft_type == MLX5_IB_FT_TX) 3044802c2125SAviad Yehezkel priority = 0; 3045802c2125SAviad Yehezkel else if (flow_is_multicast_only(flow_attr) && 304635d19011SMaor Gottlieb !dont_trap) 3047038d2ef8SMaor Gottlieb priority = MLX5_IB_FLOW_MCAST_PRIO; 3048038d2ef8SMaor Gottlieb else 304935d19011SMaor Gottlieb priority = ib_prio_to_core_prio(flow_attr->priority, 305035d19011SMaor Gottlieb dont_trap); 3051038d2ef8SMaor Gottlieb ns = mlx5_get_flow_namespace(dev->mdev, 3052802c2125SAviad Yehezkel ft_type == MLX5_IB_FT_TX ? 3053802c2125SAviad Yehezkel MLX5_FLOW_NAMESPACE_EGRESS : 3054038d2ef8SMaor Gottlieb MLX5_FLOW_NAMESPACE_BYPASS); 3055038d2ef8SMaor Gottlieb num_entries = MLX5_FS_MAX_ENTRIES; 3056038d2ef8SMaor Gottlieb num_groups = MLX5_FS_MAX_TYPES; 30579a4ca38dSMark Bloch prio = &dev->flow_db->prios[priority]; 3058038d2ef8SMaor Gottlieb } else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT || 3059038d2ef8SMaor Gottlieb flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) { 3060038d2ef8SMaor Gottlieb ns = mlx5_get_flow_namespace(dev->mdev, 3061038d2ef8SMaor Gottlieb MLX5_FLOW_NAMESPACE_LEFTOVERS); 3062038d2ef8SMaor Gottlieb build_leftovers_ft_param(&priority, 3063038d2ef8SMaor Gottlieb &num_entries, 3064038d2ef8SMaor Gottlieb &num_groups); 30659a4ca38dSMark Bloch prio = &dev->flow_db->prios[MLX5_IB_FLOW_LEFTOVERS_PRIO]; 3066cc0e5d42SMaor Gottlieb } else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) { 3067cc0e5d42SMaor Gottlieb if (!MLX5_CAP_FLOWTABLE(dev->mdev, 3068cc0e5d42SMaor Gottlieb allow_sniffer_and_nic_rx_shared_tir)) 3069cc0e5d42SMaor Gottlieb return ERR_PTR(-ENOTSUPP); 3070cc0e5d42SMaor Gottlieb 3071cc0e5d42SMaor Gottlieb ns = mlx5_get_flow_namespace(dev->mdev, ft_type == MLX5_IB_FT_RX ? 3072cc0e5d42SMaor Gottlieb MLX5_FLOW_NAMESPACE_SNIFFER_RX : 3073cc0e5d42SMaor Gottlieb MLX5_FLOW_NAMESPACE_SNIFFER_TX); 3074cc0e5d42SMaor Gottlieb 30759a4ca38dSMark Bloch prio = &dev->flow_db->sniffer[ft_type]; 3076cc0e5d42SMaor Gottlieb priority = 0; 3077cc0e5d42SMaor Gottlieb num_entries = 1; 3078cc0e5d42SMaor Gottlieb num_groups = 1; 3079038d2ef8SMaor Gottlieb } 3080038d2ef8SMaor Gottlieb 3081038d2ef8SMaor Gottlieb if (!ns) 3082038d2ef8SMaor Gottlieb return ERR_PTR(-ENOTSUPP); 3083038d2ef8SMaor Gottlieb 3084dac388efSMaor Gottlieb if (num_entries > max_table_size) 3085dac388efSMaor Gottlieb return ERR_PTR(-ENOMEM); 3086dac388efSMaor Gottlieb 3087038d2ef8SMaor Gottlieb ft = prio->flow_table; 3088038d2ef8SMaor Gottlieb if (!ft) { 3089038d2ef8SMaor Gottlieb ft = mlx5_create_auto_grouped_flow_table(ns, priority, 3090038d2ef8SMaor Gottlieb num_entries, 3091d63cd286SMaor Gottlieb num_groups, 3092c9f1b073SHadar Hen Zion 0, 0); 3093038d2ef8SMaor Gottlieb 3094038d2ef8SMaor Gottlieb if (!IS_ERR(ft)) { 3095038d2ef8SMaor Gottlieb prio->refcount = 0; 3096038d2ef8SMaor Gottlieb prio->flow_table = ft; 3097038d2ef8SMaor Gottlieb } else { 3098038d2ef8SMaor Gottlieb err = PTR_ERR(ft); 3099038d2ef8SMaor Gottlieb } 3100038d2ef8SMaor Gottlieb } 3101038d2ef8SMaor Gottlieb 3102038d2ef8SMaor Gottlieb return err ? ERR_PTR(err) : prio; 3103038d2ef8SMaor Gottlieb } 3104038d2ef8SMaor Gottlieb 3105a550ddfcSYishai Hadas static void set_underlay_qp(struct mlx5_ib_dev *dev, 3106a550ddfcSYishai Hadas struct mlx5_flow_spec *spec, 3107a550ddfcSYishai Hadas u32 underlay_qpn) 3108a550ddfcSYishai Hadas { 3109a550ddfcSYishai Hadas void *misc_params_c = MLX5_ADDR_OF(fte_match_param, 3110a550ddfcSYishai Hadas spec->match_criteria, 3111a550ddfcSYishai Hadas misc_parameters); 3112a550ddfcSYishai Hadas void *misc_params_v = MLX5_ADDR_OF(fte_match_param, spec->match_value, 3113a550ddfcSYishai Hadas misc_parameters); 3114a550ddfcSYishai Hadas 3115a550ddfcSYishai Hadas if (underlay_qpn && 3116a550ddfcSYishai Hadas MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, 3117a550ddfcSYishai Hadas ft_field_support.bth_dst_qp)) { 3118a550ddfcSYishai Hadas MLX5_SET(fte_match_set_misc, 3119a550ddfcSYishai Hadas misc_params_v, bth_dst_qp, underlay_qpn); 3120a550ddfcSYishai Hadas MLX5_SET(fte_match_set_misc, 3121a550ddfcSYishai Hadas misc_params_c, bth_dst_qp, 0xffffff); 3122a550ddfcSYishai Hadas } 3123a550ddfcSYishai Hadas } 3124a550ddfcSYishai Hadas 31255e95af5fSRaed Salem static int read_flow_counters(struct ib_device *ibdev, 31265e95af5fSRaed Salem struct mlx5_read_counters_attr *read_attr) 31275e95af5fSRaed Salem { 31285e95af5fSRaed Salem struct mlx5_fc *fc = read_attr->hw_cntrs_hndl; 31295e95af5fSRaed Salem struct mlx5_ib_dev *dev = to_mdev(ibdev); 31305e95af5fSRaed Salem 31315e95af5fSRaed Salem return mlx5_fc_query(dev->mdev, fc, 31325e95af5fSRaed Salem &read_attr->out[IB_COUNTER_PACKETS], 31335e95af5fSRaed Salem &read_attr->out[IB_COUNTER_BYTES]); 31345e95af5fSRaed Salem } 31355e95af5fSRaed Salem 31365e95af5fSRaed Salem /* flow counters currently expose two counters packets and bytes */ 31375e95af5fSRaed Salem #define FLOW_COUNTERS_NUM 2 31383b3233fbSRaed Salem static int counters_set_description(struct ib_counters *counters, 31393b3233fbSRaed Salem enum mlx5_ib_counters_type counters_type, 31403b3233fbSRaed Salem struct mlx5_ib_flow_counters_desc *desc_data, 31413b3233fbSRaed Salem u32 ncounters) 31423b3233fbSRaed Salem { 31433b3233fbSRaed Salem struct mlx5_ib_mcounters *mcounters = to_mcounters(counters); 31443b3233fbSRaed Salem u32 cntrs_max_index = 0; 31453b3233fbSRaed Salem int i; 31463b3233fbSRaed Salem 31473b3233fbSRaed Salem if (counters_type != MLX5_IB_COUNTERS_FLOW) 31483b3233fbSRaed Salem return -EINVAL; 31493b3233fbSRaed Salem 31503b3233fbSRaed Salem /* init the fields for the object */ 31513b3233fbSRaed Salem mcounters->type = counters_type; 31525e95af5fSRaed Salem mcounters->read_counters = read_flow_counters; 31535e95af5fSRaed Salem mcounters->counters_num = FLOW_COUNTERS_NUM; 31543b3233fbSRaed Salem mcounters->ncounters = ncounters; 31553b3233fbSRaed Salem /* each counter entry have both description and index pair */ 31563b3233fbSRaed Salem for (i = 0; i < ncounters; i++) { 31573b3233fbSRaed Salem if (desc_data[i].description > IB_COUNTER_BYTES) 31583b3233fbSRaed Salem return -EINVAL; 31593b3233fbSRaed Salem 31603b3233fbSRaed Salem if (cntrs_max_index <= desc_data[i].index) 31613b3233fbSRaed Salem cntrs_max_index = desc_data[i].index + 1; 31623b3233fbSRaed Salem } 31633b3233fbSRaed Salem 31643b3233fbSRaed Salem mutex_lock(&mcounters->mcntrs_mutex); 31653b3233fbSRaed Salem mcounters->counters_data = desc_data; 31663b3233fbSRaed Salem mcounters->cntrs_max_index = cntrs_max_index; 31673b3233fbSRaed Salem mutex_unlock(&mcounters->mcntrs_mutex); 31683b3233fbSRaed Salem 31693b3233fbSRaed Salem return 0; 31703b3233fbSRaed Salem } 31713b3233fbSRaed Salem 31723b3233fbSRaed Salem #define MAX_COUNTERS_NUM (USHRT_MAX / (sizeof(u32) * 2)) 31733b3233fbSRaed Salem static int flow_counters_set_data(struct ib_counters *ibcounters, 31743b3233fbSRaed Salem struct mlx5_ib_create_flow *ucmd) 31753b3233fbSRaed Salem { 31763b3233fbSRaed Salem struct mlx5_ib_mcounters *mcounters = to_mcounters(ibcounters); 31773b3233fbSRaed Salem struct mlx5_ib_flow_counters_data *cntrs_data = NULL; 31783b3233fbSRaed Salem struct mlx5_ib_flow_counters_desc *desc_data = NULL; 31793b3233fbSRaed Salem bool hw_hndl = false; 31803b3233fbSRaed Salem int ret = 0; 31813b3233fbSRaed Salem 31823b3233fbSRaed Salem if (ucmd && ucmd->ncounters_data != 0) { 31833b3233fbSRaed Salem cntrs_data = ucmd->data; 31843b3233fbSRaed Salem if (cntrs_data->ncounters > MAX_COUNTERS_NUM) 31853b3233fbSRaed Salem return -EINVAL; 31863b3233fbSRaed Salem 31873b3233fbSRaed Salem desc_data = kcalloc(cntrs_data->ncounters, 31883b3233fbSRaed Salem sizeof(*desc_data), 31893b3233fbSRaed Salem GFP_KERNEL); 31903b3233fbSRaed Salem if (!desc_data) 31913b3233fbSRaed Salem return -ENOMEM; 31923b3233fbSRaed Salem 31933b3233fbSRaed Salem if (copy_from_user(desc_data, 31943b3233fbSRaed Salem u64_to_user_ptr(cntrs_data->counters_data), 31953b3233fbSRaed Salem sizeof(*desc_data) * cntrs_data->ncounters)) { 31963b3233fbSRaed Salem ret = -EFAULT; 31973b3233fbSRaed Salem goto free; 31983b3233fbSRaed Salem } 31993b3233fbSRaed Salem } 32003b3233fbSRaed Salem 32013b3233fbSRaed Salem if (!mcounters->hw_cntrs_hndl) { 32023b3233fbSRaed Salem mcounters->hw_cntrs_hndl = mlx5_fc_create( 32033b3233fbSRaed Salem to_mdev(ibcounters->device)->mdev, false); 32043b3233fbSRaed Salem if (!mcounters->hw_cntrs_hndl) { 32053b3233fbSRaed Salem ret = -ENOMEM; 32063b3233fbSRaed Salem goto free; 32073b3233fbSRaed Salem } 32083b3233fbSRaed Salem hw_hndl = true; 32093b3233fbSRaed Salem } 32103b3233fbSRaed Salem 32113b3233fbSRaed Salem if (desc_data) { 32123b3233fbSRaed Salem /* counters already bound to at least one flow */ 32133b3233fbSRaed Salem if (mcounters->cntrs_max_index) { 32143b3233fbSRaed Salem ret = -EINVAL; 32153b3233fbSRaed Salem goto free_hndl; 32163b3233fbSRaed Salem } 32173b3233fbSRaed Salem 32183b3233fbSRaed Salem ret = counters_set_description(ibcounters, 32193b3233fbSRaed Salem MLX5_IB_COUNTERS_FLOW, 32203b3233fbSRaed Salem desc_data, 32213b3233fbSRaed Salem cntrs_data->ncounters); 32223b3233fbSRaed Salem if (ret) 32233b3233fbSRaed Salem goto free_hndl; 32243b3233fbSRaed Salem 32253b3233fbSRaed Salem } else if (!mcounters->cntrs_max_index) { 32263b3233fbSRaed Salem /* counters not bound yet, must have udata passed */ 32273b3233fbSRaed Salem ret = -EINVAL; 32283b3233fbSRaed Salem goto free_hndl; 32293b3233fbSRaed Salem } 32303b3233fbSRaed Salem 32313b3233fbSRaed Salem return 0; 32323b3233fbSRaed Salem 32333b3233fbSRaed Salem free_hndl: 32343b3233fbSRaed Salem if (hw_hndl) { 32353b3233fbSRaed Salem mlx5_fc_destroy(to_mdev(ibcounters->device)->mdev, 32363b3233fbSRaed Salem mcounters->hw_cntrs_hndl); 32373b3233fbSRaed Salem mcounters->hw_cntrs_hndl = NULL; 32383b3233fbSRaed Salem } 32393b3233fbSRaed Salem free: 32403b3233fbSRaed Salem kfree(desc_data); 32413b3233fbSRaed Salem return ret; 32423b3233fbSRaed Salem } 32433b3233fbSRaed Salem 3244a550ddfcSYishai Hadas static struct mlx5_ib_flow_handler *_create_flow_rule(struct mlx5_ib_dev *dev, 3245038d2ef8SMaor Gottlieb struct mlx5_ib_flow_prio *ft_prio, 3246dd063d0eSMaor Gottlieb const struct ib_flow_attr *flow_attr, 3247a550ddfcSYishai Hadas struct mlx5_flow_destination *dst, 32483b3233fbSRaed Salem u32 underlay_qpn, 32493b3233fbSRaed Salem struct mlx5_ib_create_flow *ucmd) 3250038d2ef8SMaor Gottlieb { 3251038d2ef8SMaor Gottlieb struct mlx5_flow_table *ft = ft_prio->flow_table; 3252038d2ef8SMaor Gottlieb struct mlx5_ib_flow_handler *handler; 3253075572d4SBoris Pismenny struct mlx5_flow_act flow_act = {.flow_tag = MLX5_FS_DEFAULT_FLOW_TAG}; 3254c5bb1730SMaor Gottlieb struct mlx5_flow_spec *spec; 32553b3233fbSRaed Salem struct mlx5_flow_destination dest_arr[2] = {}; 32563b3233fbSRaed Salem struct mlx5_flow_destination *rule_dst = dest_arr; 3257dd063d0eSMaor Gottlieb const void *ib_flow = (const void *)flow_attr + sizeof(*flow_attr); 3258038d2ef8SMaor Gottlieb unsigned int spec_index; 325971c6e863SAriel Levkovich u32 prev_type = 0; 3260038d2ef8SMaor Gottlieb int err = 0; 32613b3233fbSRaed Salem int dest_num = 0; 3262802c2125SAviad Yehezkel bool is_egress = flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS; 3263038d2ef8SMaor Gottlieb 326419cc7524SAriel Levkovich if (!is_valid_attr(dev->mdev, flow_attr)) 3265038d2ef8SMaor Gottlieb return ERR_PTR(-EINVAL); 3266038d2ef8SMaor Gottlieb 32671b9a07eeSLeon Romanovsky spec = kvzalloc(sizeof(*spec), GFP_KERNEL); 3268038d2ef8SMaor Gottlieb handler = kzalloc(sizeof(*handler), GFP_KERNEL); 3269c5bb1730SMaor Gottlieb if (!handler || !spec) { 3270038d2ef8SMaor Gottlieb err = -ENOMEM; 3271038d2ef8SMaor Gottlieb goto free; 3272038d2ef8SMaor Gottlieb } 3273038d2ef8SMaor Gottlieb 3274038d2ef8SMaor Gottlieb INIT_LIST_HEAD(&handler->list); 32753b3233fbSRaed Salem if (dst) { 32763b3233fbSRaed Salem memcpy(&dest_arr[0], dst, sizeof(*dst)); 32773b3233fbSRaed Salem dest_num++; 32783b3233fbSRaed Salem } 3279038d2ef8SMaor Gottlieb 3280038d2ef8SMaor Gottlieb for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) { 328119cc7524SAriel Levkovich err = parse_flow_attr(dev->mdev, spec->match_criteria, 3282a22ed86cSSlava Shwartsman spec->match_value, 328371c6e863SAriel Levkovich ib_flow, flow_attr, &flow_act, 328471c6e863SAriel Levkovich prev_type); 3285038d2ef8SMaor Gottlieb if (err < 0) 3286038d2ef8SMaor Gottlieb goto free; 3287038d2ef8SMaor Gottlieb 328871c6e863SAriel Levkovich prev_type = ((union ib_flow_spec *)ib_flow)->type; 3289038d2ef8SMaor Gottlieb ib_flow += ((union ib_flow_spec *)ib_flow)->size; 3290038d2ef8SMaor Gottlieb } 3291038d2ef8SMaor Gottlieb 3292a550ddfcSYishai Hadas if (!flow_is_multicast_only(flow_attr)) 3293a550ddfcSYishai Hadas set_underlay_qp(dev, spec, underlay_qpn); 3294a550ddfcSYishai Hadas 3295018a94eeSMark Bloch if (dev->rep) { 3296018a94eeSMark Bloch void *misc; 3297018a94eeSMark Bloch 3298018a94eeSMark Bloch misc = MLX5_ADDR_OF(fte_match_param, spec->match_value, 3299018a94eeSMark Bloch misc_parameters); 3300018a94eeSMark Bloch MLX5_SET(fte_match_set_misc, misc, source_port, 3301018a94eeSMark Bloch dev->rep->vport); 3302018a94eeSMark Bloch misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria, 3303018a94eeSMark Bloch misc_parameters); 3304018a94eeSMark Bloch MLX5_SET_TO_ONES(fte_match_set_misc, misc, source_port); 3305018a94eeSMark Bloch } 3306018a94eeSMark Bloch 3307466fa6d2SMaor Gottlieb spec->match_criteria_enable = get_match_criteria_enable(spec->match_criteria); 3308802c2125SAviad Yehezkel 3309802c2125SAviad Yehezkel if (is_egress && 3310802c2125SAviad Yehezkel !is_valid_spec(dev->mdev, spec, &flow_act, is_egress)) { 3311802c2125SAviad Yehezkel err = -EINVAL; 3312802c2125SAviad Yehezkel goto free; 3313802c2125SAviad Yehezkel } 3314802c2125SAviad Yehezkel 33153b3233fbSRaed Salem if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_COUNT) { 33163b3233fbSRaed Salem err = flow_counters_set_data(flow_act.counters, ucmd); 33173b3233fbSRaed Salem if (err) 33183b3233fbSRaed Salem goto free; 33193b3233fbSRaed Salem 33203b3233fbSRaed Salem handler->ibcounters = flow_act.counters; 33213b3233fbSRaed Salem dest_arr[dest_num].type = 33223b3233fbSRaed Salem MLX5_FLOW_DESTINATION_TYPE_COUNTER; 33233b3233fbSRaed Salem dest_arr[dest_num].counter = 33243b3233fbSRaed Salem to_mcounters(flow_act.counters)->hw_cntrs_hndl; 33253b3233fbSRaed Salem dest_num++; 33263b3233fbSRaed Salem } 33273b3233fbSRaed Salem 3328075572d4SBoris Pismenny if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_DROP) { 33293b3233fbSRaed Salem if (!(flow_act.action & MLX5_FLOW_CONTEXT_ACTION_COUNT)) { 3330a22ed86cSSlava Shwartsman rule_dst = NULL; 3331a22ed86cSSlava Shwartsman dest_num = 0; 33323b3233fbSRaed Salem } 3333a22ed86cSSlava Shwartsman } else { 3334802c2125SAviad Yehezkel if (is_egress) 3335802c2125SAviad Yehezkel flow_act.action |= MLX5_FLOW_CONTEXT_ACTION_ALLOW; 3336802c2125SAviad Yehezkel else 3337802c2125SAviad Yehezkel flow_act.action |= 33383b3233fbSRaed Salem dest_num ? MLX5_FLOW_CONTEXT_ACTION_FWD_DEST : 333935d19011SMaor Gottlieb MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO; 3340a22ed86cSSlava Shwartsman } 33412ac693f9SMoses Reuben 3342a9db0ecfSMatan Barak if (flow_act.has_flow_tag && 33432ac693f9SMoses Reuben (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT || 33442ac693f9SMoses Reuben flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT)) { 33452ac693f9SMoses Reuben mlx5_ib_warn(dev, "Flow tag %u and attribute type %x isn't allowed in leftovers\n", 3346075572d4SBoris Pismenny flow_act.flow_tag, flow_attr->type); 33472ac693f9SMoses Reuben err = -EINVAL; 33482ac693f9SMoses Reuben goto free; 33492ac693f9SMoses Reuben } 335074491de9SMark Bloch handler->rule = mlx5_add_flow_rules(ft, spec, 335166958ed9SHadar Hen Zion &flow_act, 3352a22ed86cSSlava Shwartsman rule_dst, dest_num); 3353038d2ef8SMaor Gottlieb 3354038d2ef8SMaor Gottlieb if (IS_ERR(handler->rule)) { 3355038d2ef8SMaor Gottlieb err = PTR_ERR(handler->rule); 3356038d2ef8SMaor Gottlieb goto free; 3357038d2ef8SMaor Gottlieb } 3358038d2ef8SMaor Gottlieb 3359d9d4980aSMaor Gottlieb ft_prio->refcount++; 33605497adc6SMaor Gottlieb handler->prio = ft_prio; 3361038d2ef8SMaor Gottlieb 3362038d2ef8SMaor Gottlieb ft_prio->flow_table = ft; 3363038d2ef8SMaor Gottlieb free: 33643b3233fbSRaed Salem if (err && handler) { 33653b3233fbSRaed Salem if (handler->ibcounters && 33663b3233fbSRaed Salem atomic_read(&handler->ibcounters->usecnt) == 1) 33673b3233fbSRaed Salem counters_clear_description(handler->ibcounters); 3368038d2ef8SMaor Gottlieb kfree(handler); 33693b3233fbSRaed Salem } 3370c5bb1730SMaor Gottlieb kvfree(spec); 3371038d2ef8SMaor Gottlieb return err ? ERR_PTR(err) : handler; 3372038d2ef8SMaor Gottlieb } 3373038d2ef8SMaor Gottlieb 3374a550ddfcSYishai Hadas static struct mlx5_ib_flow_handler *create_flow_rule(struct mlx5_ib_dev *dev, 3375a550ddfcSYishai Hadas struct mlx5_ib_flow_prio *ft_prio, 3376a550ddfcSYishai Hadas const struct ib_flow_attr *flow_attr, 3377a550ddfcSYishai Hadas struct mlx5_flow_destination *dst) 3378a550ddfcSYishai Hadas { 33793b3233fbSRaed Salem return _create_flow_rule(dev, ft_prio, flow_attr, dst, 0, NULL); 3380a550ddfcSYishai Hadas } 3381a550ddfcSYishai Hadas 338235d19011SMaor Gottlieb static struct mlx5_ib_flow_handler *create_dont_trap_rule(struct mlx5_ib_dev *dev, 338335d19011SMaor Gottlieb struct mlx5_ib_flow_prio *ft_prio, 338435d19011SMaor Gottlieb struct ib_flow_attr *flow_attr, 338535d19011SMaor Gottlieb struct mlx5_flow_destination *dst) 338635d19011SMaor Gottlieb { 338735d19011SMaor Gottlieb struct mlx5_ib_flow_handler *handler_dst = NULL; 338835d19011SMaor Gottlieb struct mlx5_ib_flow_handler *handler = NULL; 338935d19011SMaor Gottlieb 339035d19011SMaor Gottlieb handler = create_flow_rule(dev, ft_prio, flow_attr, NULL); 339135d19011SMaor Gottlieb if (!IS_ERR(handler)) { 339235d19011SMaor Gottlieb handler_dst = create_flow_rule(dev, ft_prio, 339335d19011SMaor Gottlieb flow_attr, dst); 339435d19011SMaor Gottlieb if (IS_ERR(handler_dst)) { 339574491de9SMark Bloch mlx5_del_flow_rules(handler->rule); 3396d9d4980aSMaor Gottlieb ft_prio->refcount--; 339735d19011SMaor Gottlieb kfree(handler); 339835d19011SMaor Gottlieb handler = handler_dst; 339935d19011SMaor Gottlieb } else { 340035d19011SMaor Gottlieb list_add(&handler_dst->list, &handler->list); 340135d19011SMaor Gottlieb } 340235d19011SMaor Gottlieb } 340335d19011SMaor Gottlieb 340435d19011SMaor Gottlieb return handler; 340535d19011SMaor Gottlieb } 3406038d2ef8SMaor Gottlieb enum { 3407038d2ef8SMaor Gottlieb LEFTOVERS_MC, 3408038d2ef8SMaor Gottlieb LEFTOVERS_UC, 3409038d2ef8SMaor Gottlieb }; 3410038d2ef8SMaor Gottlieb 3411038d2ef8SMaor Gottlieb static struct mlx5_ib_flow_handler *create_leftovers_rule(struct mlx5_ib_dev *dev, 3412038d2ef8SMaor Gottlieb struct mlx5_ib_flow_prio *ft_prio, 3413038d2ef8SMaor Gottlieb struct ib_flow_attr *flow_attr, 3414038d2ef8SMaor Gottlieb struct mlx5_flow_destination *dst) 3415038d2ef8SMaor Gottlieb { 3416038d2ef8SMaor Gottlieb struct mlx5_ib_flow_handler *handler_ucast = NULL; 3417038d2ef8SMaor Gottlieb struct mlx5_ib_flow_handler *handler = NULL; 3418038d2ef8SMaor Gottlieb 3419038d2ef8SMaor Gottlieb static struct { 3420038d2ef8SMaor Gottlieb struct ib_flow_attr flow_attr; 3421038d2ef8SMaor Gottlieb struct ib_flow_spec_eth eth_flow; 3422038d2ef8SMaor Gottlieb } leftovers_specs[] = { 3423038d2ef8SMaor Gottlieb [LEFTOVERS_MC] = { 3424038d2ef8SMaor Gottlieb .flow_attr = { 3425038d2ef8SMaor Gottlieb .num_of_specs = 1, 3426038d2ef8SMaor Gottlieb .size = sizeof(leftovers_specs[0]) 3427038d2ef8SMaor Gottlieb }, 3428038d2ef8SMaor Gottlieb .eth_flow = { 3429038d2ef8SMaor Gottlieb .type = IB_FLOW_SPEC_ETH, 3430038d2ef8SMaor Gottlieb .size = sizeof(struct ib_flow_spec_eth), 3431038d2ef8SMaor Gottlieb .mask = {.dst_mac = {0x1} }, 3432038d2ef8SMaor Gottlieb .val = {.dst_mac = {0x1} } 3433038d2ef8SMaor Gottlieb } 3434038d2ef8SMaor Gottlieb }, 3435038d2ef8SMaor Gottlieb [LEFTOVERS_UC] = { 3436038d2ef8SMaor Gottlieb .flow_attr = { 3437038d2ef8SMaor Gottlieb .num_of_specs = 1, 3438038d2ef8SMaor Gottlieb .size = sizeof(leftovers_specs[0]) 3439038d2ef8SMaor Gottlieb }, 3440038d2ef8SMaor Gottlieb .eth_flow = { 3441038d2ef8SMaor Gottlieb .type = IB_FLOW_SPEC_ETH, 3442038d2ef8SMaor Gottlieb .size = sizeof(struct ib_flow_spec_eth), 3443038d2ef8SMaor Gottlieb .mask = {.dst_mac = {0x1} }, 3444038d2ef8SMaor Gottlieb .val = {.dst_mac = {} } 3445038d2ef8SMaor Gottlieb } 3446038d2ef8SMaor Gottlieb } 3447038d2ef8SMaor Gottlieb }; 3448038d2ef8SMaor Gottlieb 3449038d2ef8SMaor Gottlieb handler = create_flow_rule(dev, ft_prio, 3450038d2ef8SMaor Gottlieb &leftovers_specs[LEFTOVERS_MC].flow_attr, 3451038d2ef8SMaor Gottlieb dst); 3452038d2ef8SMaor Gottlieb if (!IS_ERR(handler) && 3453038d2ef8SMaor Gottlieb flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT) { 3454038d2ef8SMaor Gottlieb handler_ucast = create_flow_rule(dev, ft_prio, 3455038d2ef8SMaor Gottlieb &leftovers_specs[LEFTOVERS_UC].flow_attr, 3456038d2ef8SMaor Gottlieb dst); 3457038d2ef8SMaor Gottlieb if (IS_ERR(handler_ucast)) { 345874491de9SMark Bloch mlx5_del_flow_rules(handler->rule); 3459d9d4980aSMaor Gottlieb ft_prio->refcount--; 3460038d2ef8SMaor Gottlieb kfree(handler); 3461038d2ef8SMaor Gottlieb handler = handler_ucast; 3462038d2ef8SMaor Gottlieb } else { 3463038d2ef8SMaor Gottlieb list_add(&handler_ucast->list, &handler->list); 3464038d2ef8SMaor Gottlieb } 3465038d2ef8SMaor Gottlieb } 3466038d2ef8SMaor Gottlieb 3467038d2ef8SMaor Gottlieb return handler; 3468038d2ef8SMaor Gottlieb } 3469038d2ef8SMaor Gottlieb 3470cc0e5d42SMaor Gottlieb static struct mlx5_ib_flow_handler *create_sniffer_rule(struct mlx5_ib_dev *dev, 3471cc0e5d42SMaor Gottlieb struct mlx5_ib_flow_prio *ft_rx, 3472cc0e5d42SMaor Gottlieb struct mlx5_ib_flow_prio *ft_tx, 3473cc0e5d42SMaor Gottlieb struct mlx5_flow_destination *dst) 3474cc0e5d42SMaor Gottlieb { 3475cc0e5d42SMaor Gottlieb struct mlx5_ib_flow_handler *handler_rx; 3476cc0e5d42SMaor Gottlieb struct mlx5_ib_flow_handler *handler_tx; 3477cc0e5d42SMaor Gottlieb int err; 3478cc0e5d42SMaor Gottlieb static const struct ib_flow_attr flow_attr = { 3479cc0e5d42SMaor Gottlieb .num_of_specs = 0, 3480cc0e5d42SMaor Gottlieb .size = sizeof(flow_attr) 3481cc0e5d42SMaor Gottlieb }; 3482cc0e5d42SMaor Gottlieb 3483cc0e5d42SMaor Gottlieb handler_rx = create_flow_rule(dev, ft_rx, &flow_attr, dst); 3484cc0e5d42SMaor Gottlieb if (IS_ERR(handler_rx)) { 3485cc0e5d42SMaor Gottlieb err = PTR_ERR(handler_rx); 3486cc0e5d42SMaor Gottlieb goto err; 3487cc0e5d42SMaor Gottlieb } 3488cc0e5d42SMaor Gottlieb 3489cc0e5d42SMaor Gottlieb handler_tx = create_flow_rule(dev, ft_tx, &flow_attr, dst); 3490cc0e5d42SMaor Gottlieb if (IS_ERR(handler_tx)) { 3491cc0e5d42SMaor Gottlieb err = PTR_ERR(handler_tx); 3492cc0e5d42SMaor Gottlieb goto err_tx; 3493cc0e5d42SMaor Gottlieb } 3494cc0e5d42SMaor Gottlieb 3495cc0e5d42SMaor Gottlieb list_add(&handler_tx->list, &handler_rx->list); 3496cc0e5d42SMaor Gottlieb 3497cc0e5d42SMaor Gottlieb return handler_rx; 3498cc0e5d42SMaor Gottlieb 3499cc0e5d42SMaor Gottlieb err_tx: 350074491de9SMark Bloch mlx5_del_flow_rules(handler_rx->rule); 3501cc0e5d42SMaor Gottlieb ft_rx->refcount--; 3502cc0e5d42SMaor Gottlieb kfree(handler_rx); 3503cc0e5d42SMaor Gottlieb err: 3504cc0e5d42SMaor Gottlieb return ERR_PTR(err); 3505cc0e5d42SMaor Gottlieb } 3506cc0e5d42SMaor Gottlieb 3507038d2ef8SMaor Gottlieb static struct ib_flow *mlx5_ib_create_flow(struct ib_qp *qp, 3508038d2ef8SMaor Gottlieb struct ib_flow_attr *flow_attr, 350959082a32SMatan Barak int domain, 351059082a32SMatan Barak struct ib_udata *udata) 3511038d2ef8SMaor Gottlieb { 3512038d2ef8SMaor Gottlieb struct mlx5_ib_dev *dev = to_mdev(qp->device); 3513d9f88e5aSYishai Hadas struct mlx5_ib_qp *mqp = to_mqp(qp); 3514038d2ef8SMaor Gottlieb struct mlx5_ib_flow_handler *handler = NULL; 3515038d2ef8SMaor Gottlieb struct mlx5_flow_destination *dst = NULL; 3516cc0e5d42SMaor Gottlieb struct mlx5_ib_flow_prio *ft_prio_tx = NULL; 3517038d2ef8SMaor Gottlieb struct mlx5_ib_flow_prio *ft_prio; 3518802c2125SAviad Yehezkel bool is_egress = flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS; 35193b3233fbSRaed Salem struct mlx5_ib_create_flow *ucmd = NULL, ucmd_hdr; 35203b3233fbSRaed Salem size_t min_ucmd_sz, required_ucmd_sz; 3521038d2ef8SMaor Gottlieb int err; 3522a550ddfcSYishai Hadas int underlay_qpn; 3523038d2ef8SMaor Gottlieb 35243b3233fbSRaed Salem if (udata && udata->inlen) { 35253b3233fbSRaed Salem min_ucmd_sz = offsetof(typeof(ucmd_hdr), reserved) + 35263b3233fbSRaed Salem sizeof(ucmd_hdr.reserved); 35273b3233fbSRaed Salem if (udata->inlen < min_ucmd_sz) 352859082a32SMatan Barak return ERR_PTR(-EOPNOTSUPP); 352959082a32SMatan Barak 35303b3233fbSRaed Salem err = ib_copy_from_udata(&ucmd_hdr, udata, min_ucmd_sz); 35313b3233fbSRaed Salem if (err) 35323b3233fbSRaed Salem return ERR_PTR(err); 35333b3233fbSRaed Salem 35343b3233fbSRaed Salem /* currently supports only one counters data */ 35353b3233fbSRaed Salem if (ucmd_hdr.ncounters_data > 1) 35363b3233fbSRaed Salem return ERR_PTR(-EINVAL); 35373b3233fbSRaed Salem 35383b3233fbSRaed Salem required_ucmd_sz = min_ucmd_sz + 35393b3233fbSRaed Salem sizeof(struct mlx5_ib_flow_counters_data) * 35403b3233fbSRaed Salem ucmd_hdr.ncounters_data; 35413b3233fbSRaed Salem if (udata->inlen > required_ucmd_sz && 35423b3233fbSRaed Salem !ib_is_udata_cleared(udata, required_ucmd_sz, 35433b3233fbSRaed Salem udata->inlen - required_ucmd_sz)) 35443b3233fbSRaed Salem return ERR_PTR(-EOPNOTSUPP); 35453b3233fbSRaed Salem 35463b3233fbSRaed Salem ucmd = kzalloc(required_ucmd_sz, GFP_KERNEL); 35473b3233fbSRaed Salem if (!ucmd) 35483b3233fbSRaed Salem return ERR_PTR(-ENOMEM); 35493b3233fbSRaed Salem 35503b3233fbSRaed Salem err = ib_copy_from_udata(ucmd, udata, required_ucmd_sz); 35513b3233fbSRaed Salem if (err) { 35523b3233fbSRaed Salem kfree(ucmd); 35533b3233fbSRaed Salem return ERR_PTR(err); 35543b3233fbSRaed Salem } 35553b3233fbSRaed Salem } 35563b3233fbSRaed Salem 3557038d2ef8SMaor Gottlieb if (flow_attr->priority > MLX5_IB_FLOW_LAST_PRIO) 3558dac388efSMaor Gottlieb return ERR_PTR(-ENOMEM); 3559038d2ef8SMaor Gottlieb 3560038d2ef8SMaor Gottlieb if (domain != IB_FLOW_DOMAIN_USER || 3561508562d6SDaniel Jurgens flow_attr->port > dev->num_ports || 3562802c2125SAviad Yehezkel (flow_attr->flags & ~(IB_FLOW_ATTR_FLAGS_DONT_TRAP | 3563802c2125SAviad Yehezkel IB_FLOW_ATTR_FLAGS_EGRESS))) 3564802c2125SAviad Yehezkel return ERR_PTR(-EINVAL); 3565802c2125SAviad Yehezkel 3566802c2125SAviad Yehezkel if (is_egress && 3567802c2125SAviad Yehezkel (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT || 3568802c2125SAviad Yehezkel flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT)) 3569038d2ef8SMaor Gottlieb return ERR_PTR(-EINVAL); 3570038d2ef8SMaor Gottlieb 3571038d2ef8SMaor Gottlieb dst = kzalloc(sizeof(*dst), GFP_KERNEL); 3572038d2ef8SMaor Gottlieb if (!dst) 3573038d2ef8SMaor Gottlieb return ERR_PTR(-ENOMEM); 3574038d2ef8SMaor Gottlieb 35759a4ca38dSMark Bloch mutex_lock(&dev->flow_db->lock); 3576038d2ef8SMaor Gottlieb 3577802c2125SAviad Yehezkel ft_prio = get_flow_table(dev, flow_attr, 3578802c2125SAviad Yehezkel is_egress ? MLX5_IB_FT_TX : MLX5_IB_FT_RX); 3579038d2ef8SMaor Gottlieb if (IS_ERR(ft_prio)) { 3580038d2ef8SMaor Gottlieb err = PTR_ERR(ft_prio); 3581038d2ef8SMaor Gottlieb goto unlock; 3582038d2ef8SMaor Gottlieb } 3583cc0e5d42SMaor Gottlieb if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) { 3584cc0e5d42SMaor Gottlieb ft_prio_tx = get_flow_table(dev, flow_attr, MLX5_IB_FT_TX); 3585cc0e5d42SMaor Gottlieb if (IS_ERR(ft_prio_tx)) { 3586cc0e5d42SMaor Gottlieb err = PTR_ERR(ft_prio_tx); 3587cc0e5d42SMaor Gottlieb ft_prio_tx = NULL; 3588cc0e5d42SMaor Gottlieb goto destroy_ft; 3589cc0e5d42SMaor Gottlieb } 3590cc0e5d42SMaor Gottlieb } 3591038d2ef8SMaor Gottlieb 3592802c2125SAviad Yehezkel if (is_egress) { 3593802c2125SAviad Yehezkel dst->type = MLX5_FLOW_DESTINATION_TYPE_PORT; 3594802c2125SAviad Yehezkel } else { 3595038d2ef8SMaor Gottlieb dst->type = MLX5_FLOW_DESTINATION_TYPE_TIR; 3596d9f88e5aSYishai Hadas if (mqp->flags & MLX5_IB_QP_RSS) 3597d9f88e5aSYishai Hadas dst->tir_num = mqp->rss_qp.tirn; 3598d9f88e5aSYishai Hadas else 3599d9f88e5aSYishai Hadas dst->tir_num = mqp->raw_packet_qp.rq.tirn; 3600802c2125SAviad Yehezkel } 3601038d2ef8SMaor Gottlieb 3602038d2ef8SMaor Gottlieb if (flow_attr->type == IB_FLOW_ATTR_NORMAL) { 360335d19011SMaor Gottlieb if (flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP) { 360435d19011SMaor Gottlieb handler = create_dont_trap_rule(dev, ft_prio, 360535d19011SMaor Gottlieb flow_attr, dst); 360635d19011SMaor Gottlieb } else { 3607a550ddfcSYishai Hadas underlay_qpn = (mqp->flags & MLX5_IB_QP_UNDERLAY) ? 3608a550ddfcSYishai Hadas mqp->underlay_qpn : 0; 3609a550ddfcSYishai Hadas handler = _create_flow_rule(dev, ft_prio, flow_attr, 36103b3233fbSRaed Salem dst, underlay_qpn, ucmd); 361135d19011SMaor Gottlieb } 3612038d2ef8SMaor Gottlieb } else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT || 3613038d2ef8SMaor Gottlieb flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) { 3614038d2ef8SMaor Gottlieb handler = create_leftovers_rule(dev, ft_prio, flow_attr, 3615038d2ef8SMaor Gottlieb dst); 3616cc0e5d42SMaor Gottlieb } else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) { 3617cc0e5d42SMaor Gottlieb handler = create_sniffer_rule(dev, ft_prio, ft_prio_tx, dst); 3618038d2ef8SMaor Gottlieb } else { 3619038d2ef8SMaor Gottlieb err = -EINVAL; 3620038d2ef8SMaor Gottlieb goto destroy_ft; 3621038d2ef8SMaor Gottlieb } 3622038d2ef8SMaor Gottlieb 3623038d2ef8SMaor Gottlieb if (IS_ERR(handler)) { 3624038d2ef8SMaor Gottlieb err = PTR_ERR(handler); 3625038d2ef8SMaor Gottlieb handler = NULL; 3626038d2ef8SMaor Gottlieb goto destroy_ft; 3627038d2ef8SMaor Gottlieb } 3628038d2ef8SMaor Gottlieb 36299a4ca38dSMark Bloch mutex_unlock(&dev->flow_db->lock); 3630038d2ef8SMaor Gottlieb kfree(dst); 36313b3233fbSRaed Salem kfree(ucmd); 3632038d2ef8SMaor Gottlieb 3633038d2ef8SMaor Gottlieb return &handler->ibflow; 3634038d2ef8SMaor Gottlieb 3635038d2ef8SMaor Gottlieb destroy_ft: 3636038d2ef8SMaor Gottlieb put_flow_table(dev, ft_prio, false); 3637cc0e5d42SMaor Gottlieb if (ft_prio_tx) 3638cc0e5d42SMaor Gottlieb put_flow_table(dev, ft_prio_tx, false); 3639038d2ef8SMaor Gottlieb unlock: 36409a4ca38dSMark Bloch mutex_unlock(&dev->flow_db->lock); 3641038d2ef8SMaor Gottlieb kfree(dst); 36423b3233fbSRaed Salem kfree(ucmd); 3643038d2ef8SMaor Gottlieb kfree(handler); 3644038d2ef8SMaor Gottlieb return ERR_PTR(err); 3645038d2ef8SMaor Gottlieb } 3646038d2ef8SMaor Gottlieb 3647c6475a0bSAviad Yehezkel static u32 mlx5_ib_flow_action_flags_to_accel_xfrm_flags(u32 mlx5_flags) 3648c6475a0bSAviad Yehezkel { 3649c6475a0bSAviad Yehezkel u32 flags = 0; 3650c6475a0bSAviad Yehezkel 3651c6475a0bSAviad Yehezkel if (mlx5_flags & MLX5_IB_UAPI_FLOW_ACTION_FLAGS_REQUIRE_METADATA) 3652c6475a0bSAviad Yehezkel flags |= MLX5_ACCEL_XFRM_FLAG_REQUIRE_METADATA; 3653c6475a0bSAviad Yehezkel 3654c6475a0bSAviad Yehezkel return flags; 3655c6475a0bSAviad Yehezkel } 3656c6475a0bSAviad Yehezkel 3657c6475a0bSAviad Yehezkel #define MLX5_FLOW_ACTION_ESP_CREATE_LAST_SUPPORTED MLX5_IB_UAPI_FLOW_ACTION_FLAGS_REQUIRE_METADATA 3658c6475a0bSAviad Yehezkel static struct ib_flow_action * 3659c6475a0bSAviad Yehezkel mlx5_ib_create_flow_action_esp(struct ib_device *device, 3660c6475a0bSAviad Yehezkel const struct ib_flow_action_attrs_esp *attr, 3661c6475a0bSAviad Yehezkel struct uverbs_attr_bundle *attrs) 3662c6475a0bSAviad Yehezkel { 3663c6475a0bSAviad Yehezkel struct mlx5_ib_dev *mdev = to_mdev(device); 3664c6475a0bSAviad Yehezkel struct ib_uverbs_flow_action_esp_keymat_aes_gcm *aes_gcm; 3665c6475a0bSAviad Yehezkel struct mlx5_accel_esp_xfrm_attrs accel_attrs = {}; 3666c6475a0bSAviad Yehezkel struct mlx5_ib_flow_action *action; 3667c6475a0bSAviad Yehezkel u64 action_flags; 3668c6475a0bSAviad Yehezkel u64 flags; 3669c6475a0bSAviad Yehezkel int err = 0; 3670c6475a0bSAviad Yehezkel 3671c6475a0bSAviad Yehezkel if (IS_UVERBS_COPY_ERR(uverbs_copy_from(&action_flags, attrs, 3672c6475a0bSAviad Yehezkel MLX5_IB_ATTR_CREATE_FLOW_ACTION_FLAGS))) 3673c6475a0bSAviad Yehezkel return ERR_PTR(-EFAULT); 3674c6475a0bSAviad Yehezkel 3675c6475a0bSAviad Yehezkel if (action_flags >= (MLX5_FLOW_ACTION_ESP_CREATE_LAST_SUPPORTED << 1)) 3676c6475a0bSAviad Yehezkel return ERR_PTR(-EOPNOTSUPP); 3677c6475a0bSAviad Yehezkel 3678c6475a0bSAviad Yehezkel flags = mlx5_ib_flow_action_flags_to_accel_xfrm_flags(action_flags); 3679c6475a0bSAviad Yehezkel 3680c6475a0bSAviad Yehezkel /* We current only support a subset of the standard features. Only a 3681c6475a0bSAviad Yehezkel * keymat of type AES_GCM, with icv_len == 16, iv_algo == SEQ and esn 3682c6475a0bSAviad Yehezkel * (with overlap). Full offload mode isn't supported. 3683c6475a0bSAviad Yehezkel */ 3684c6475a0bSAviad Yehezkel if (!attr->keymat || attr->replay || attr->encap || 3685c6475a0bSAviad Yehezkel attr->spi || attr->seq || attr->tfc_pad || 3686c6475a0bSAviad Yehezkel attr->hard_limit_pkts || 3687c6475a0bSAviad Yehezkel (attr->flags & ~(IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED | 3688c6475a0bSAviad Yehezkel IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ENCRYPT))) 3689c6475a0bSAviad Yehezkel return ERR_PTR(-EOPNOTSUPP); 3690c6475a0bSAviad Yehezkel 3691c6475a0bSAviad Yehezkel if (attr->keymat->protocol != 3692c6475a0bSAviad Yehezkel IB_UVERBS_FLOW_ACTION_ESP_KEYMAT_AES_GCM) 3693c6475a0bSAviad Yehezkel return ERR_PTR(-EOPNOTSUPP); 3694c6475a0bSAviad Yehezkel 3695c6475a0bSAviad Yehezkel aes_gcm = &attr->keymat->keymat.aes_gcm; 3696c6475a0bSAviad Yehezkel 3697c6475a0bSAviad Yehezkel if (aes_gcm->icv_len != 16 || 3698c6475a0bSAviad Yehezkel aes_gcm->iv_algo != IB_UVERBS_FLOW_ACTION_IV_ALGO_SEQ) 3699c6475a0bSAviad Yehezkel return ERR_PTR(-EOPNOTSUPP); 3700c6475a0bSAviad Yehezkel 3701c6475a0bSAviad Yehezkel action = kmalloc(sizeof(*action), GFP_KERNEL); 3702c6475a0bSAviad Yehezkel if (!action) 3703c6475a0bSAviad Yehezkel return ERR_PTR(-ENOMEM); 3704c6475a0bSAviad Yehezkel 3705c6475a0bSAviad Yehezkel action->esp_aes_gcm.ib_flags = attr->flags; 3706c6475a0bSAviad Yehezkel memcpy(&accel_attrs.keymat.aes_gcm.aes_key, &aes_gcm->aes_key, 3707c6475a0bSAviad Yehezkel sizeof(accel_attrs.keymat.aes_gcm.aes_key)); 3708c6475a0bSAviad Yehezkel accel_attrs.keymat.aes_gcm.key_len = aes_gcm->key_len * 8; 3709c6475a0bSAviad Yehezkel memcpy(&accel_attrs.keymat.aes_gcm.salt, &aes_gcm->salt, 3710c6475a0bSAviad Yehezkel sizeof(accel_attrs.keymat.aes_gcm.salt)); 3711c6475a0bSAviad Yehezkel memcpy(&accel_attrs.keymat.aes_gcm.seq_iv, &aes_gcm->iv, 3712c6475a0bSAviad Yehezkel sizeof(accel_attrs.keymat.aes_gcm.seq_iv)); 3713c6475a0bSAviad Yehezkel accel_attrs.keymat.aes_gcm.icv_len = aes_gcm->icv_len * 8; 3714c6475a0bSAviad Yehezkel accel_attrs.keymat.aes_gcm.iv_algo = MLX5_ACCEL_ESP_AES_GCM_IV_ALGO_SEQ; 3715c6475a0bSAviad Yehezkel accel_attrs.keymat_type = MLX5_ACCEL_ESP_KEYMAT_AES_GCM; 3716c6475a0bSAviad Yehezkel 3717c6475a0bSAviad Yehezkel accel_attrs.esn = attr->esn; 3718c6475a0bSAviad Yehezkel if (attr->flags & IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED) 3719c6475a0bSAviad Yehezkel accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_TRIGGERED; 3720c6475a0bSAviad Yehezkel if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW) 3721c6475a0bSAviad Yehezkel accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP; 3722c6475a0bSAviad Yehezkel 3723c6475a0bSAviad Yehezkel if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ENCRYPT) 3724c6475a0bSAviad Yehezkel accel_attrs.action |= MLX5_ACCEL_ESP_ACTION_ENCRYPT; 3725c6475a0bSAviad Yehezkel 3726c6475a0bSAviad Yehezkel action->esp_aes_gcm.ctx = 3727c6475a0bSAviad Yehezkel mlx5_accel_esp_create_xfrm(mdev->mdev, &accel_attrs, flags); 3728c6475a0bSAviad Yehezkel if (IS_ERR(action->esp_aes_gcm.ctx)) { 3729c6475a0bSAviad Yehezkel err = PTR_ERR(action->esp_aes_gcm.ctx); 3730c6475a0bSAviad Yehezkel goto err_parse; 3731c6475a0bSAviad Yehezkel } 3732c6475a0bSAviad Yehezkel 3733c6475a0bSAviad Yehezkel action->esp_aes_gcm.ib_flags = attr->flags; 3734c6475a0bSAviad Yehezkel 3735c6475a0bSAviad Yehezkel return &action->ib_action; 3736c6475a0bSAviad Yehezkel 3737c6475a0bSAviad Yehezkel err_parse: 3738c6475a0bSAviad Yehezkel kfree(action); 3739c6475a0bSAviad Yehezkel return ERR_PTR(err); 3740c6475a0bSAviad Yehezkel } 3741c6475a0bSAviad Yehezkel 3742349705c1SMatan Barak static int 3743349705c1SMatan Barak mlx5_ib_modify_flow_action_esp(struct ib_flow_action *action, 3744349705c1SMatan Barak const struct ib_flow_action_attrs_esp *attr, 3745349705c1SMatan Barak struct uverbs_attr_bundle *attrs) 3746349705c1SMatan Barak { 3747349705c1SMatan Barak struct mlx5_ib_flow_action *maction = to_mflow_act(action); 3748349705c1SMatan Barak struct mlx5_accel_esp_xfrm_attrs accel_attrs; 3749349705c1SMatan Barak int err = 0; 3750349705c1SMatan Barak 3751349705c1SMatan Barak if (attr->keymat || attr->replay || attr->encap || 3752349705c1SMatan Barak attr->spi || attr->seq || attr->tfc_pad || 3753349705c1SMatan Barak attr->hard_limit_pkts || 3754349705c1SMatan Barak (attr->flags & ~(IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED | 3755349705c1SMatan Barak IB_FLOW_ACTION_ESP_FLAGS_MOD_ESP_ATTRS | 3756349705c1SMatan Barak IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW))) 3757349705c1SMatan Barak return -EOPNOTSUPP; 3758349705c1SMatan Barak 3759349705c1SMatan Barak /* Only the ESN value or the MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP can 3760349705c1SMatan Barak * be modified. 3761349705c1SMatan Barak */ 3762349705c1SMatan Barak if (!(maction->esp_aes_gcm.ib_flags & 3763349705c1SMatan Barak IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED) && 3764349705c1SMatan Barak attr->flags & (IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED | 3765349705c1SMatan Barak IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW)) 3766349705c1SMatan Barak return -EINVAL; 3767349705c1SMatan Barak 3768349705c1SMatan Barak memcpy(&accel_attrs, &maction->esp_aes_gcm.ctx->attrs, 3769349705c1SMatan Barak sizeof(accel_attrs)); 3770349705c1SMatan Barak 3771349705c1SMatan Barak accel_attrs.esn = attr->esn; 3772349705c1SMatan Barak if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW) 3773349705c1SMatan Barak accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP; 3774349705c1SMatan Barak else 3775349705c1SMatan Barak accel_attrs.flags &= ~MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP; 3776349705c1SMatan Barak 3777349705c1SMatan Barak err = mlx5_accel_esp_modify_xfrm(maction->esp_aes_gcm.ctx, 3778349705c1SMatan Barak &accel_attrs); 3779349705c1SMatan Barak if (err) 3780349705c1SMatan Barak return err; 3781349705c1SMatan Barak 3782349705c1SMatan Barak maction->esp_aes_gcm.ib_flags &= 3783349705c1SMatan Barak ~IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW; 3784349705c1SMatan Barak maction->esp_aes_gcm.ib_flags |= 3785349705c1SMatan Barak attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW; 3786349705c1SMatan Barak 3787349705c1SMatan Barak return 0; 3788349705c1SMatan Barak } 3789349705c1SMatan Barak 3790c6475a0bSAviad Yehezkel static int mlx5_ib_destroy_flow_action(struct ib_flow_action *action) 3791c6475a0bSAviad Yehezkel { 3792c6475a0bSAviad Yehezkel struct mlx5_ib_flow_action *maction = to_mflow_act(action); 3793c6475a0bSAviad Yehezkel 3794c6475a0bSAviad Yehezkel switch (action->type) { 3795c6475a0bSAviad Yehezkel case IB_FLOW_ACTION_ESP: 3796c6475a0bSAviad Yehezkel /* 3797c6475a0bSAviad Yehezkel * We only support aes_gcm by now, so we implicitly know this is 3798c6475a0bSAviad Yehezkel * the underline crypto. 3799c6475a0bSAviad Yehezkel */ 3800c6475a0bSAviad Yehezkel mlx5_accel_esp_destroy_xfrm(maction->esp_aes_gcm.ctx); 3801c6475a0bSAviad Yehezkel break; 3802c6475a0bSAviad Yehezkel default: 3803c6475a0bSAviad Yehezkel WARN_ON(true); 3804c6475a0bSAviad Yehezkel break; 3805c6475a0bSAviad Yehezkel } 3806c6475a0bSAviad Yehezkel 3807c6475a0bSAviad Yehezkel kfree(maction); 3808c6475a0bSAviad Yehezkel return 0; 3809c6475a0bSAviad Yehezkel } 3810c6475a0bSAviad Yehezkel 3811e126ba97SEli Cohen static int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid) 3812e126ba97SEli Cohen { 3813e126ba97SEli Cohen struct mlx5_ib_dev *dev = to_mdev(ibqp->device); 381481e30880SYishai Hadas struct mlx5_ib_qp *mqp = to_mqp(ibqp); 3815e126ba97SEli Cohen int err; 3816e126ba97SEli Cohen 381781e30880SYishai Hadas if (mqp->flags & MLX5_IB_QP_UNDERLAY) { 381881e30880SYishai Hadas mlx5_ib_dbg(dev, "Attaching a multi cast group to underlay QP is not supported\n"); 381981e30880SYishai Hadas return -EOPNOTSUPP; 382081e30880SYishai Hadas } 382181e30880SYishai Hadas 38229603b61dSJack Morgenstein err = mlx5_core_attach_mcg(dev->mdev, gid, ibqp->qp_num); 3823e126ba97SEli Cohen if (err) 3824e126ba97SEli Cohen mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n", 3825e126ba97SEli Cohen ibqp->qp_num, gid->raw); 3826e126ba97SEli Cohen 3827e126ba97SEli Cohen return err; 3828e126ba97SEli Cohen } 3829e126ba97SEli Cohen 3830e126ba97SEli Cohen static int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid) 3831e126ba97SEli Cohen { 3832e126ba97SEli Cohen struct mlx5_ib_dev *dev = to_mdev(ibqp->device); 3833e126ba97SEli Cohen int err; 3834e126ba97SEli Cohen 38359603b61dSJack Morgenstein err = mlx5_core_detach_mcg(dev->mdev, gid, ibqp->qp_num); 3836e126ba97SEli Cohen if (err) 3837e126ba97SEli Cohen mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n", 3838e126ba97SEli Cohen ibqp->qp_num, gid->raw); 3839e126ba97SEli Cohen 3840e126ba97SEli Cohen return err; 3841e126ba97SEli Cohen } 3842e126ba97SEli Cohen 3843e126ba97SEli Cohen static int init_node_data(struct mlx5_ib_dev *dev) 3844e126ba97SEli Cohen { 38451b5daf11SMajd Dibbiny int err; 3846e126ba97SEli Cohen 38471b5daf11SMajd Dibbiny err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc); 3848e126ba97SEli Cohen if (err) 3849e126ba97SEli Cohen return err; 38501b5daf11SMajd Dibbiny 38511b5daf11SMajd Dibbiny dev->mdev->rev_id = dev->mdev->pdev->revision; 38521b5daf11SMajd Dibbiny 38531b5daf11SMajd Dibbiny return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid); 3854e126ba97SEli Cohen } 3855e126ba97SEli Cohen 3856e126ba97SEli Cohen static ssize_t show_fw_pages(struct device *device, struct device_attribute *attr, 3857e126ba97SEli Cohen char *buf) 3858e126ba97SEli Cohen { 3859e126ba97SEli Cohen struct mlx5_ib_dev *dev = 3860e126ba97SEli Cohen container_of(device, struct mlx5_ib_dev, ib_dev.dev); 3861e126ba97SEli Cohen 38629603b61dSJack Morgenstein return sprintf(buf, "%d\n", dev->mdev->priv.fw_pages); 3863e126ba97SEli Cohen } 3864e126ba97SEli Cohen 3865e126ba97SEli Cohen static ssize_t show_reg_pages(struct device *device, 3866e126ba97SEli Cohen struct device_attribute *attr, char *buf) 3867e126ba97SEli Cohen { 3868e126ba97SEli Cohen struct mlx5_ib_dev *dev = 3869e126ba97SEli Cohen container_of(device, struct mlx5_ib_dev, ib_dev.dev); 3870e126ba97SEli Cohen 38716aec21f6SHaggai Eran return sprintf(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages)); 3872e126ba97SEli Cohen } 3873e126ba97SEli Cohen 3874e126ba97SEli Cohen static ssize_t show_hca(struct device *device, struct device_attribute *attr, 3875e126ba97SEli Cohen char *buf) 3876e126ba97SEli Cohen { 3877e126ba97SEli Cohen struct mlx5_ib_dev *dev = 3878e126ba97SEli Cohen container_of(device, struct mlx5_ib_dev, ib_dev.dev); 38799603b61dSJack Morgenstein return sprintf(buf, "MT%d\n", dev->mdev->pdev->device); 3880e126ba97SEli Cohen } 3881e126ba97SEli Cohen 3882e126ba97SEli Cohen static ssize_t show_rev(struct device *device, struct device_attribute *attr, 3883e126ba97SEli Cohen char *buf) 3884e126ba97SEli Cohen { 3885e126ba97SEli Cohen struct mlx5_ib_dev *dev = 3886e126ba97SEli Cohen container_of(device, struct mlx5_ib_dev, ib_dev.dev); 38879603b61dSJack Morgenstein return sprintf(buf, "%x\n", dev->mdev->rev_id); 3888e126ba97SEli Cohen } 3889e126ba97SEli Cohen 3890e126ba97SEli Cohen static ssize_t show_board(struct device *device, struct device_attribute *attr, 3891e126ba97SEli Cohen char *buf) 3892e126ba97SEli Cohen { 3893e126ba97SEli Cohen struct mlx5_ib_dev *dev = 3894e126ba97SEli Cohen container_of(device, struct mlx5_ib_dev, ib_dev.dev); 3895e126ba97SEli Cohen return sprintf(buf, "%.*s\n", MLX5_BOARD_ID_LEN, 38969603b61dSJack Morgenstein dev->mdev->board_id); 3897e126ba97SEli Cohen } 3898e126ba97SEli Cohen 3899e126ba97SEli Cohen static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL); 3900e126ba97SEli Cohen static DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL); 3901e126ba97SEli Cohen static DEVICE_ATTR(board_id, S_IRUGO, show_board, NULL); 3902e126ba97SEli Cohen static DEVICE_ATTR(fw_pages, S_IRUGO, show_fw_pages, NULL); 3903e126ba97SEli Cohen static DEVICE_ATTR(reg_pages, S_IRUGO, show_reg_pages, NULL); 3904e126ba97SEli Cohen 3905e126ba97SEli Cohen static struct device_attribute *mlx5_class_attributes[] = { 3906e126ba97SEli Cohen &dev_attr_hw_rev, 3907e126ba97SEli Cohen &dev_attr_hca_type, 3908e126ba97SEli Cohen &dev_attr_board_id, 3909e126ba97SEli Cohen &dev_attr_fw_pages, 3910e126ba97SEli Cohen &dev_attr_reg_pages, 3911e126ba97SEli Cohen }; 3912e126ba97SEli Cohen 39137722f47eSHaggai Eran static void pkey_change_handler(struct work_struct *work) 39147722f47eSHaggai Eran { 39157722f47eSHaggai Eran struct mlx5_ib_port_resources *ports = 39167722f47eSHaggai Eran container_of(work, struct mlx5_ib_port_resources, 39177722f47eSHaggai Eran pkey_change_work); 39187722f47eSHaggai Eran 39197722f47eSHaggai Eran mutex_lock(&ports->devr->mutex); 39207722f47eSHaggai Eran mlx5_ib_gsi_pkey_change(ports->gsi); 39217722f47eSHaggai Eran mutex_unlock(&ports->devr->mutex); 39227722f47eSHaggai Eran } 39237722f47eSHaggai Eran 392489ea94a7SMaor Gottlieb static void mlx5_ib_handle_internal_error(struct mlx5_ib_dev *ibdev) 392589ea94a7SMaor Gottlieb { 392689ea94a7SMaor Gottlieb struct mlx5_ib_qp *mqp; 392789ea94a7SMaor Gottlieb struct mlx5_ib_cq *send_mcq, *recv_mcq; 392889ea94a7SMaor Gottlieb struct mlx5_core_cq *mcq; 392989ea94a7SMaor Gottlieb struct list_head cq_armed_list; 393089ea94a7SMaor Gottlieb unsigned long flags_qp; 393189ea94a7SMaor Gottlieb unsigned long flags_cq; 393289ea94a7SMaor Gottlieb unsigned long flags; 393389ea94a7SMaor Gottlieb 393489ea94a7SMaor Gottlieb INIT_LIST_HEAD(&cq_armed_list); 393589ea94a7SMaor Gottlieb 393689ea94a7SMaor Gottlieb /* Go over qp list reside on that ibdev, sync with create/destroy qp.*/ 393789ea94a7SMaor Gottlieb spin_lock_irqsave(&ibdev->reset_flow_resource_lock, flags); 393889ea94a7SMaor Gottlieb list_for_each_entry(mqp, &ibdev->qp_list, qps_list) { 393989ea94a7SMaor Gottlieb spin_lock_irqsave(&mqp->sq.lock, flags_qp); 394089ea94a7SMaor Gottlieb if (mqp->sq.tail != mqp->sq.head) { 394189ea94a7SMaor Gottlieb send_mcq = to_mcq(mqp->ibqp.send_cq); 394289ea94a7SMaor Gottlieb spin_lock_irqsave(&send_mcq->lock, flags_cq); 394389ea94a7SMaor Gottlieb if (send_mcq->mcq.comp && 394489ea94a7SMaor Gottlieb mqp->ibqp.send_cq->comp_handler) { 394589ea94a7SMaor Gottlieb if (!send_mcq->mcq.reset_notify_added) { 394689ea94a7SMaor Gottlieb send_mcq->mcq.reset_notify_added = 1; 394789ea94a7SMaor Gottlieb list_add_tail(&send_mcq->mcq.reset_notify, 394889ea94a7SMaor Gottlieb &cq_armed_list); 394989ea94a7SMaor Gottlieb } 395089ea94a7SMaor Gottlieb } 395189ea94a7SMaor Gottlieb spin_unlock_irqrestore(&send_mcq->lock, flags_cq); 395289ea94a7SMaor Gottlieb } 395389ea94a7SMaor Gottlieb spin_unlock_irqrestore(&mqp->sq.lock, flags_qp); 395489ea94a7SMaor Gottlieb spin_lock_irqsave(&mqp->rq.lock, flags_qp); 395589ea94a7SMaor Gottlieb /* no handling is needed for SRQ */ 395689ea94a7SMaor Gottlieb if (!mqp->ibqp.srq) { 395789ea94a7SMaor Gottlieb if (mqp->rq.tail != mqp->rq.head) { 395889ea94a7SMaor Gottlieb recv_mcq = to_mcq(mqp->ibqp.recv_cq); 395989ea94a7SMaor Gottlieb spin_lock_irqsave(&recv_mcq->lock, flags_cq); 396089ea94a7SMaor Gottlieb if (recv_mcq->mcq.comp && 396189ea94a7SMaor Gottlieb mqp->ibqp.recv_cq->comp_handler) { 396289ea94a7SMaor Gottlieb if (!recv_mcq->mcq.reset_notify_added) { 396389ea94a7SMaor Gottlieb recv_mcq->mcq.reset_notify_added = 1; 396489ea94a7SMaor Gottlieb list_add_tail(&recv_mcq->mcq.reset_notify, 396589ea94a7SMaor Gottlieb &cq_armed_list); 396689ea94a7SMaor Gottlieb } 396789ea94a7SMaor Gottlieb } 396889ea94a7SMaor Gottlieb spin_unlock_irqrestore(&recv_mcq->lock, 396989ea94a7SMaor Gottlieb flags_cq); 397089ea94a7SMaor Gottlieb } 397189ea94a7SMaor Gottlieb } 397289ea94a7SMaor Gottlieb spin_unlock_irqrestore(&mqp->rq.lock, flags_qp); 397389ea94a7SMaor Gottlieb } 397489ea94a7SMaor Gottlieb /*At that point all inflight post send were put to be executed as of we 397589ea94a7SMaor Gottlieb * lock/unlock above locks Now need to arm all involved CQs. 397689ea94a7SMaor Gottlieb */ 397789ea94a7SMaor Gottlieb list_for_each_entry(mcq, &cq_armed_list, reset_notify) { 397889ea94a7SMaor Gottlieb mcq->comp(mcq); 397989ea94a7SMaor Gottlieb } 398089ea94a7SMaor Gottlieb spin_unlock_irqrestore(&ibdev->reset_flow_resource_lock, flags); 398189ea94a7SMaor Gottlieb } 398289ea94a7SMaor Gottlieb 398303404e8aSMaor Gottlieb static void delay_drop_handler(struct work_struct *work) 398403404e8aSMaor Gottlieb { 398503404e8aSMaor Gottlieb int err; 398603404e8aSMaor Gottlieb struct mlx5_ib_delay_drop *delay_drop = 398703404e8aSMaor Gottlieb container_of(work, struct mlx5_ib_delay_drop, 398803404e8aSMaor Gottlieb delay_drop_work); 398903404e8aSMaor Gottlieb 3990fe248c3aSMaor Gottlieb atomic_inc(&delay_drop->events_cnt); 3991fe248c3aSMaor Gottlieb 399203404e8aSMaor Gottlieb mutex_lock(&delay_drop->lock); 399303404e8aSMaor Gottlieb err = mlx5_core_set_delay_drop(delay_drop->dev->mdev, 399403404e8aSMaor Gottlieb delay_drop->timeout); 399503404e8aSMaor Gottlieb if (err) { 399603404e8aSMaor Gottlieb mlx5_ib_warn(delay_drop->dev, "Failed to set delay drop, timeout=%u\n", 399703404e8aSMaor Gottlieb delay_drop->timeout); 399803404e8aSMaor Gottlieb delay_drop->activate = false; 399903404e8aSMaor Gottlieb } 400003404e8aSMaor Gottlieb mutex_unlock(&delay_drop->lock); 400103404e8aSMaor Gottlieb } 400203404e8aSMaor Gottlieb 4003d69a24e0SDaniel Jurgens static void mlx5_ib_handle_event(struct work_struct *_work) 4004e126ba97SEli Cohen { 4005d69a24e0SDaniel Jurgens struct mlx5_ib_event_work *work = 4006d69a24e0SDaniel Jurgens container_of(_work, struct mlx5_ib_event_work, work); 4007d69a24e0SDaniel Jurgens struct mlx5_ib_dev *ibdev; 4008e126ba97SEli Cohen struct ib_event ibev; 4009dbaaff2aSEli Cohen bool fatal = false; 4010aba46213SDaniel Jurgens u8 port = (u8)work->param; 4011e126ba97SEli Cohen 4012d69a24e0SDaniel Jurgens if (mlx5_core_is_mp_slave(work->dev)) { 4013d69a24e0SDaniel Jurgens ibdev = mlx5_ib_get_ibdev_from_mpi(work->context); 4014d69a24e0SDaniel Jurgens if (!ibdev) 4015d69a24e0SDaniel Jurgens goto out; 4016d69a24e0SDaniel Jurgens } else { 4017d69a24e0SDaniel Jurgens ibdev = work->context; 4018d69a24e0SDaniel Jurgens } 4019d69a24e0SDaniel Jurgens 4020d69a24e0SDaniel Jurgens switch (work->event) { 4021e126ba97SEli Cohen case MLX5_DEV_EVENT_SYS_ERROR: 4022e126ba97SEli Cohen ibev.event = IB_EVENT_DEVICE_FATAL; 402389ea94a7SMaor Gottlieb mlx5_ib_handle_internal_error(ibdev); 4024dbaaff2aSEli Cohen fatal = true; 4025e126ba97SEli Cohen break; 4026e126ba97SEli Cohen 4027e126ba97SEli Cohen case MLX5_DEV_EVENT_PORT_UP: 4028e126ba97SEli Cohen case MLX5_DEV_EVENT_PORT_DOWN: 40292788cf3bSNoa Osherovich case MLX5_DEV_EVENT_PORT_INITIALIZED: 40305ec8c83eSAviv Heller /* In RoCE, port up/down events are handled in 40315ec8c83eSAviv Heller * mlx5_netdev_event(). 40325ec8c83eSAviv Heller */ 40335ec8c83eSAviv Heller if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) == 40345ec8c83eSAviv Heller IB_LINK_LAYER_ETHERNET) 4035d69a24e0SDaniel Jurgens goto out; 40365ec8c83eSAviv Heller 4037d69a24e0SDaniel Jurgens ibev.event = (work->event == MLX5_DEV_EVENT_PORT_UP) ? 40385ec8c83eSAviv Heller IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR; 4039e126ba97SEli Cohen break; 4040e126ba97SEli Cohen 4041e126ba97SEli Cohen case MLX5_DEV_EVENT_LID_CHANGE: 4042e126ba97SEli Cohen ibev.event = IB_EVENT_LID_CHANGE; 4043e126ba97SEli Cohen break; 4044e126ba97SEli Cohen 4045e126ba97SEli Cohen case MLX5_DEV_EVENT_PKEY_CHANGE: 4046e126ba97SEli Cohen ibev.event = IB_EVENT_PKEY_CHANGE; 40477722f47eSHaggai Eran schedule_work(&ibdev->devr.ports[port - 1].pkey_change_work); 4048e126ba97SEli Cohen break; 4049e126ba97SEli Cohen 4050e126ba97SEli Cohen case MLX5_DEV_EVENT_GUID_CHANGE: 4051e126ba97SEli Cohen ibev.event = IB_EVENT_GID_CHANGE; 4052e126ba97SEli Cohen break; 4053e126ba97SEli Cohen 4054e126ba97SEli Cohen case MLX5_DEV_EVENT_CLIENT_REREG: 4055e126ba97SEli Cohen ibev.event = IB_EVENT_CLIENT_REREGISTER; 4056e126ba97SEli Cohen break; 405703404e8aSMaor Gottlieb case MLX5_DEV_EVENT_DELAY_DROP_TIMEOUT: 405803404e8aSMaor Gottlieb schedule_work(&ibdev->delay_drop.delay_drop_work); 405903404e8aSMaor Gottlieb goto out; 4060bdc37924SSaeed Mahameed default: 406103404e8aSMaor Gottlieb goto out; 4062e126ba97SEli Cohen } 4063e126ba97SEli Cohen 4064e126ba97SEli Cohen ibev.device = &ibdev->ib_dev; 4065e126ba97SEli Cohen ibev.element.port_num = port; 4066e126ba97SEli Cohen 4067aba46213SDaniel Jurgens if (!rdma_is_port_valid(&ibdev->ib_dev, port)) { 4068a0c84c32SEli Cohen mlx5_ib_warn(ibdev, "warning: event on port %d\n", port); 406903404e8aSMaor Gottlieb goto out; 4070a0c84c32SEli Cohen } 4071a0c84c32SEli Cohen 4072e126ba97SEli Cohen if (ibdev->ib_active) 4073e126ba97SEli Cohen ib_dispatch_event(&ibev); 4074dbaaff2aSEli Cohen 4075dbaaff2aSEli Cohen if (fatal) 4076dbaaff2aSEli Cohen ibdev->ib_active = false; 407703404e8aSMaor Gottlieb out: 4078d69a24e0SDaniel Jurgens kfree(work); 4079d69a24e0SDaniel Jurgens } 4080d69a24e0SDaniel Jurgens 4081d69a24e0SDaniel Jurgens static void mlx5_ib_event(struct mlx5_core_dev *dev, void *context, 4082d69a24e0SDaniel Jurgens enum mlx5_dev_event event, unsigned long param) 4083d69a24e0SDaniel Jurgens { 4084d69a24e0SDaniel Jurgens struct mlx5_ib_event_work *work; 4085d69a24e0SDaniel Jurgens 4086d69a24e0SDaniel Jurgens work = kmalloc(sizeof(*work), GFP_ATOMIC); 408710bea9c8SLeon Romanovsky if (!work) 408810bea9c8SLeon Romanovsky return; 408910bea9c8SLeon Romanovsky 4090d69a24e0SDaniel Jurgens INIT_WORK(&work->work, mlx5_ib_handle_event); 4091d69a24e0SDaniel Jurgens work->dev = dev; 4092d69a24e0SDaniel Jurgens work->param = param; 4093d69a24e0SDaniel Jurgens work->context = context; 4094d69a24e0SDaniel Jurgens work->event = event; 4095d69a24e0SDaniel Jurgens 4096d69a24e0SDaniel Jurgens queue_work(mlx5_ib_event_wq, &work->work); 4097d69a24e0SDaniel Jurgens } 4098d69a24e0SDaniel Jurgens 4099c43f1112SMaor Gottlieb static int set_has_smi_cap(struct mlx5_ib_dev *dev) 4100c43f1112SMaor Gottlieb { 4101c43f1112SMaor Gottlieb struct mlx5_hca_vport_context vport_ctx; 4102c43f1112SMaor Gottlieb int err; 4103c43f1112SMaor Gottlieb int port; 4104c43f1112SMaor Gottlieb 4105508562d6SDaniel Jurgens for (port = 1; port <= dev->num_ports; port++) { 4106c43f1112SMaor Gottlieb dev->mdev->port_caps[port - 1].has_smi = false; 4107c43f1112SMaor Gottlieb if (MLX5_CAP_GEN(dev->mdev, port_type) == 4108c43f1112SMaor Gottlieb MLX5_CAP_PORT_TYPE_IB) { 4109c43f1112SMaor Gottlieb if (MLX5_CAP_GEN(dev->mdev, ib_virt)) { 4110c43f1112SMaor Gottlieb err = mlx5_query_hca_vport_context(dev->mdev, 0, 4111c43f1112SMaor Gottlieb port, 0, 4112c43f1112SMaor Gottlieb &vport_ctx); 4113c43f1112SMaor Gottlieb if (err) { 4114c43f1112SMaor Gottlieb mlx5_ib_err(dev, "query_hca_vport_context for port=%d failed %d\n", 4115c43f1112SMaor Gottlieb port, err); 4116c43f1112SMaor Gottlieb return err; 4117c43f1112SMaor Gottlieb } 4118c43f1112SMaor Gottlieb dev->mdev->port_caps[port - 1].has_smi = 4119c43f1112SMaor Gottlieb vport_ctx.has_smi; 4120c43f1112SMaor Gottlieb } else { 4121c43f1112SMaor Gottlieb dev->mdev->port_caps[port - 1].has_smi = true; 4122c43f1112SMaor Gottlieb } 4123c43f1112SMaor Gottlieb } 4124c43f1112SMaor Gottlieb } 4125c43f1112SMaor Gottlieb return 0; 4126c43f1112SMaor Gottlieb } 4127c43f1112SMaor Gottlieb 4128e126ba97SEli Cohen static void get_ext_port_caps(struct mlx5_ib_dev *dev) 4129e126ba97SEli Cohen { 4130e126ba97SEli Cohen int port; 4131e126ba97SEli Cohen 4132508562d6SDaniel Jurgens for (port = 1; port <= dev->num_ports; port++) 4133e126ba97SEli Cohen mlx5_query_ext_port_caps(dev, port); 4134e126ba97SEli Cohen } 4135e126ba97SEli Cohen 413632f69e4bSDaniel Jurgens static int get_port_caps(struct mlx5_ib_dev *dev, u8 port) 4137e126ba97SEli Cohen { 4138e126ba97SEli Cohen struct ib_device_attr *dprops = NULL; 4139e126ba97SEli Cohen struct ib_port_attr *pprops = NULL; 4140f614fc15SDan Carpenter int err = -ENOMEM; 41412528e33eSMatan Barak struct ib_udata uhw = {.inlen = 0, .outlen = 0}; 4142e126ba97SEli Cohen 4143e126ba97SEli Cohen pprops = kmalloc(sizeof(*pprops), GFP_KERNEL); 4144e126ba97SEli Cohen if (!pprops) 4145e126ba97SEli Cohen goto out; 4146e126ba97SEli Cohen 4147e126ba97SEli Cohen dprops = kmalloc(sizeof(*dprops), GFP_KERNEL); 4148e126ba97SEli Cohen if (!dprops) 4149e126ba97SEli Cohen goto out; 4150e126ba97SEli Cohen 4151c43f1112SMaor Gottlieb err = set_has_smi_cap(dev); 4152c43f1112SMaor Gottlieb if (err) 4153c43f1112SMaor Gottlieb goto out; 4154c43f1112SMaor Gottlieb 41552528e33eSMatan Barak err = mlx5_ib_query_device(&dev->ib_dev, dprops, &uhw); 4156e126ba97SEli Cohen if (err) { 4157e126ba97SEli Cohen mlx5_ib_warn(dev, "query_device failed %d\n", err); 4158e126ba97SEli Cohen goto out; 4159e126ba97SEli Cohen } 4160e126ba97SEli Cohen 4161c4550c63SOr Gerlitz memset(pprops, 0, sizeof(*pprops)); 4162e126ba97SEli Cohen err = mlx5_ib_query_port(&dev->ib_dev, port, pprops); 4163e126ba97SEli Cohen if (err) { 4164938fe83cSSaeed Mahameed mlx5_ib_warn(dev, "query_port %d failed %d\n", 4165938fe83cSSaeed Mahameed port, err); 416632f69e4bSDaniel Jurgens goto out; 4167e126ba97SEli Cohen } 416832f69e4bSDaniel Jurgens 4169938fe83cSSaeed Mahameed dev->mdev->port_caps[port - 1].pkey_table_len = 4170938fe83cSSaeed Mahameed dprops->max_pkeys; 4171938fe83cSSaeed Mahameed dev->mdev->port_caps[port - 1].gid_table_len = 4172938fe83cSSaeed Mahameed pprops->gid_tbl_len; 417332f69e4bSDaniel Jurgens mlx5_ib_dbg(dev, "port %d: pkey_table_len %d, gid_table_len %d\n", 417432f69e4bSDaniel Jurgens port, dprops->max_pkeys, pprops->gid_tbl_len); 4175e126ba97SEli Cohen 4176e126ba97SEli Cohen out: 4177e126ba97SEli Cohen kfree(pprops); 4178e126ba97SEli Cohen kfree(dprops); 4179e126ba97SEli Cohen 4180e126ba97SEli Cohen return err; 4181e126ba97SEli Cohen } 4182e126ba97SEli Cohen 4183e126ba97SEli Cohen static void destroy_umrc_res(struct mlx5_ib_dev *dev) 4184e126ba97SEli Cohen { 4185e126ba97SEli Cohen int err; 4186e126ba97SEli Cohen 4187e126ba97SEli Cohen err = mlx5_mr_cache_cleanup(dev); 4188e126ba97SEli Cohen if (err) 4189e126ba97SEli Cohen mlx5_ib_warn(dev, "mr cache cleanup failed\n"); 4190e126ba97SEli Cohen 419132927e28SMark Bloch if (dev->umrc.qp) 4192e126ba97SEli Cohen mlx5_ib_destroy_qp(dev->umrc.qp); 419332927e28SMark Bloch if (dev->umrc.cq) 4194add08d76SChristoph Hellwig ib_free_cq(dev->umrc.cq); 419532927e28SMark Bloch if (dev->umrc.pd) 4196e126ba97SEli Cohen ib_dealloc_pd(dev->umrc.pd); 4197e126ba97SEli Cohen } 4198e126ba97SEli Cohen 4199e126ba97SEli Cohen enum { 4200e126ba97SEli Cohen MAX_UMR_WR = 128, 4201e126ba97SEli Cohen }; 4202e126ba97SEli Cohen 4203e126ba97SEli Cohen static int create_umr_res(struct mlx5_ib_dev *dev) 4204e126ba97SEli Cohen { 4205e126ba97SEli Cohen struct ib_qp_init_attr *init_attr = NULL; 4206e126ba97SEli Cohen struct ib_qp_attr *attr = NULL; 4207e126ba97SEli Cohen struct ib_pd *pd; 4208e126ba97SEli Cohen struct ib_cq *cq; 4209e126ba97SEli Cohen struct ib_qp *qp; 4210e126ba97SEli Cohen int ret; 4211e126ba97SEli Cohen 4212e126ba97SEli Cohen attr = kzalloc(sizeof(*attr), GFP_KERNEL); 4213e126ba97SEli Cohen init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL); 4214e126ba97SEli Cohen if (!attr || !init_attr) { 4215e126ba97SEli Cohen ret = -ENOMEM; 4216e126ba97SEli Cohen goto error_0; 4217e126ba97SEli Cohen } 4218e126ba97SEli Cohen 4219ed082d36SChristoph Hellwig pd = ib_alloc_pd(&dev->ib_dev, 0); 4220e126ba97SEli Cohen if (IS_ERR(pd)) { 4221e126ba97SEli Cohen mlx5_ib_dbg(dev, "Couldn't create PD for sync UMR QP\n"); 4222e126ba97SEli Cohen ret = PTR_ERR(pd); 4223e126ba97SEli Cohen goto error_0; 4224e126ba97SEli Cohen } 4225e126ba97SEli Cohen 4226add08d76SChristoph Hellwig cq = ib_alloc_cq(&dev->ib_dev, NULL, 128, 0, IB_POLL_SOFTIRQ); 4227e126ba97SEli Cohen if (IS_ERR(cq)) { 4228e126ba97SEli Cohen mlx5_ib_dbg(dev, "Couldn't create CQ for sync UMR QP\n"); 4229e126ba97SEli Cohen ret = PTR_ERR(cq); 4230e126ba97SEli Cohen goto error_2; 4231e126ba97SEli Cohen } 4232e126ba97SEli Cohen 4233e126ba97SEli Cohen init_attr->send_cq = cq; 4234e126ba97SEli Cohen init_attr->recv_cq = cq; 4235e126ba97SEli Cohen init_attr->sq_sig_type = IB_SIGNAL_ALL_WR; 4236e126ba97SEli Cohen init_attr->cap.max_send_wr = MAX_UMR_WR; 4237e126ba97SEli Cohen init_attr->cap.max_send_sge = 1; 4238e126ba97SEli Cohen init_attr->qp_type = MLX5_IB_QPT_REG_UMR; 4239e126ba97SEli Cohen init_attr->port_num = 1; 4240e126ba97SEli Cohen qp = mlx5_ib_create_qp(pd, init_attr, NULL); 4241e126ba97SEli Cohen if (IS_ERR(qp)) { 4242e126ba97SEli Cohen mlx5_ib_dbg(dev, "Couldn't create sync UMR QP\n"); 4243e126ba97SEli Cohen ret = PTR_ERR(qp); 4244e126ba97SEli Cohen goto error_3; 4245e126ba97SEli Cohen } 4246e126ba97SEli Cohen qp->device = &dev->ib_dev; 4247e126ba97SEli Cohen qp->real_qp = qp; 4248e126ba97SEli Cohen qp->uobject = NULL; 4249e126ba97SEli Cohen qp->qp_type = MLX5_IB_QPT_REG_UMR; 425031fde034SMajd Dibbiny qp->send_cq = init_attr->send_cq; 425131fde034SMajd Dibbiny qp->recv_cq = init_attr->recv_cq; 4252e126ba97SEli Cohen 4253e126ba97SEli Cohen attr->qp_state = IB_QPS_INIT; 4254e126ba97SEli Cohen attr->port_num = 1; 4255e126ba97SEli Cohen ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_PKEY_INDEX | 4256e126ba97SEli Cohen IB_QP_PORT, NULL); 4257e126ba97SEli Cohen if (ret) { 4258e126ba97SEli Cohen mlx5_ib_dbg(dev, "Couldn't modify UMR QP\n"); 4259e126ba97SEli Cohen goto error_4; 4260e126ba97SEli Cohen } 4261e126ba97SEli Cohen 4262e126ba97SEli Cohen memset(attr, 0, sizeof(*attr)); 4263e126ba97SEli Cohen attr->qp_state = IB_QPS_RTR; 4264e126ba97SEli Cohen attr->path_mtu = IB_MTU_256; 4265e126ba97SEli Cohen 4266e126ba97SEli Cohen ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL); 4267e126ba97SEli Cohen if (ret) { 4268e126ba97SEli Cohen mlx5_ib_dbg(dev, "Couldn't modify umr QP to rtr\n"); 4269e126ba97SEli Cohen goto error_4; 4270e126ba97SEli Cohen } 4271e126ba97SEli Cohen 4272e126ba97SEli Cohen memset(attr, 0, sizeof(*attr)); 4273e126ba97SEli Cohen attr->qp_state = IB_QPS_RTS; 4274e126ba97SEli Cohen ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL); 4275e126ba97SEli Cohen if (ret) { 4276e126ba97SEli Cohen mlx5_ib_dbg(dev, "Couldn't modify umr QP to rts\n"); 4277e126ba97SEli Cohen goto error_4; 4278e126ba97SEli Cohen } 4279e126ba97SEli Cohen 4280e126ba97SEli Cohen dev->umrc.qp = qp; 4281e126ba97SEli Cohen dev->umrc.cq = cq; 4282e126ba97SEli Cohen dev->umrc.pd = pd; 4283e126ba97SEli Cohen 4284e126ba97SEli Cohen sema_init(&dev->umrc.sem, MAX_UMR_WR); 4285e126ba97SEli Cohen ret = mlx5_mr_cache_init(dev); 4286e126ba97SEli Cohen if (ret) { 4287e126ba97SEli Cohen mlx5_ib_warn(dev, "mr cache init failed %d\n", ret); 4288e126ba97SEli Cohen goto error_4; 4289e126ba97SEli Cohen } 4290e126ba97SEli Cohen 4291e126ba97SEli Cohen kfree(attr); 4292e126ba97SEli Cohen kfree(init_attr); 4293e126ba97SEli Cohen 4294e126ba97SEli Cohen return 0; 4295e126ba97SEli Cohen 4296e126ba97SEli Cohen error_4: 4297e126ba97SEli Cohen mlx5_ib_destroy_qp(qp); 429832927e28SMark Bloch dev->umrc.qp = NULL; 4299e126ba97SEli Cohen 4300e126ba97SEli Cohen error_3: 4301add08d76SChristoph Hellwig ib_free_cq(cq); 430232927e28SMark Bloch dev->umrc.cq = NULL; 4303e126ba97SEli Cohen 4304e126ba97SEli Cohen error_2: 4305e126ba97SEli Cohen ib_dealloc_pd(pd); 430632927e28SMark Bloch dev->umrc.pd = NULL; 4307e126ba97SEli Cohen 4308e126ba97SEli Cohen error_0: 4309e126ba97SEli Cohen kfree(attr); 4310e126ba97SEli Cohen kfree(init_attr); 4311e126ba97SEli Cohen return ret; 4312e126ba97SEli Cohen } 4313e126ba97SEli Cohen 43146e8484c5SMax Gurtovoy static u8 mlx5_get_umr_fence(u8 umr_fence_cap) 43156e8484c5SMax Gurtovoy { 43166e8484c5SMax Gurtovoy switch (umr_fence_cap) { 43176e8484c5SMax Gurtovoy case MLX5_CAP_UMR_FENCE_NONE: 43186e8484c5SMax Gurtovoy return MLX5_FENCE_MODE_NONE; 43196e8484c5SMax Gurtovoy case MLX5_CAP_UMR_FENCE_SMALL: 43206e8484c5SMax Gurtovoy return MLX5_FENCE_MODE_INITIATOR_SMALL; 43216e8484c5SMax Gurtovoy default: 43226e8484c5SMax Gurtovoy return MLX5_FENCE_MODE_STRONG_ORDERING; 43236e8484c5SMax Gurtovoy } 43246e8484c5SMax Gurtovoy } 43256e8484c5SMax Gurtovoy 4326e126ba97SEli Cohen static int create_dev_resources(struct mlx5_ib_resources *devr) 4327e126ba97SEli Cohen { 4328e126ba97SEli Cohen struct ib_srq_init_attr attr; 4329e126ba97SEli Cohen struct mlx5_ib_dev *dev; 4330bcf4c1eaSMatan Barak struct ib_cq_init_attr cq_attr = {.cqe = 1}; 43317722f47eSHaggai Eran int port; 4332e126ba97SEli Cohen int ret = 0; 4333e126ba97SEli Cohen 4334e126ba97SEli Cohen dev = container_of(devr, struct mlx5_ib_dev, devr); 4335e126ba97SEli Cohen 4336d16e91daSHaggai Eran mutex_init(&devr->mutex); 4337d16e91daSHaggai Eran 4338e126ba97SEli Cohen devr->p0 = mlx5_ib_alloc_pd(&dev->ib_dev, NULL, NULL); 4339e126ba97SEli Cohen if (IS_ERR(devr->p0)) { 4340e126ba97SEli Cohen ret = PTR_ERR(devr->p0); 4341e126ba97SEli Cohen goto error0; 4342e126ba97SEli Cohen } 4343e126ba97SEli Cohen devr->p0->device = &dev->ib_dev; 4344e126ba97SEli Cohen devr->p0->uobject = NULL; 4345e126ba97SEli Cohen atomic_set(&devr->p0->usecnt, 0); 4346e126ba97SEli Cohen 4347bcf4c1eaSMatan Barak devr->c0 = mlx5_ib_create_cq(&dev->ib_dev, &cq_attr, NULL, NULL); 4348e126ba97SEli Cohen if (IS_ERR(devr->c0)) { 4349e126ba97SEli Cohen ret = PTR_ERR(devr->c0); 4350e126ba97SEli Cohen goto error1; 4351e126ba97SEli Cohen } 4352e126ba97SEli Cohen devr->c0->device = &dev->ib_dev; 4353e126ba97SEli Cohen devr->c0->uobject = NULL; 4354e126ba97SEli Cohen devr->c0->comp_handler = NULL; 4355e126ba97SEli Cohen devr->c0->event_handler = NULL; 4356e126ba97SEli Cohen devr->c0->cq_context = NULL; 4357e126ba97SEli Cohen atomic_set(&devr->c0->usecnt, 0); 4358e126ba97SEli Cohen 4359e126ba97SEli Cohen devr->x0 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL); 4360e126ba97SEli Cohen if (IS_ERR(devr->x0)) { 4361e126ba97SEli Cohen ret = PTR_ERR(devr->x0); 4362e126ba97SEli Cohen goto error2; 4363e126ba97SEli Cohen } 4364e126ba97SEli Cohen devr->x0->device = &dev->ib_dev; 4365e126ba97SEli Cohen devr->x0->inode = NULL; 4366e126ba97SEli Cohen atomic_set(&devr->x0->usecnt, 0); 4367e126ba97SEli Cohen mutex_init(&devr->x0->tgt_qp_mutex); 4368e126ba97SEli Cohen INIT_LIST_HEAD(&devr->x0->tgt_qp_list); 4369e126ba97SEli Cohen 4370e126ba97SEli Cohen devr->x1 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL); 4371e126ba97SEli Cohen if (IS_ERR(devr->x1)) { 4372e126ba97SEli Cohen ret = PTR_ERR(devr->x1); 4373e126ba97SEli Cohen goto error3; 4374e126ba97SEli Cohen } 4375e126ba97SEli Cohen devr->x1->device = &dev->ib_dev; 4376e126ba97SEli Cohen devr->x1->inode = NULL; 4377e126ba97SEli Cohen atomic_set(&devr->x1->usecnt, 0); 4378e126ba97SEli Cohen mutex_init(&devr->x1->tgt_qp_mutex); 4379e126ba97SEli Cohen INIT_LIST_HEAD(&devr->x1->tgt_qp_list); 4380e126ba97SEli Cohen 4381e126ba97SEli Cohen memset(&attr, 0, sizeof(attr)); 4382e126ba97SEli Cohen attr.attr.max_sge = 1; 4383e126ba97SEli Cohen attr.attr.max_wr = 1; 4384e126ba97SEli Cohen attr.srq_type = IB_SRQT_XRC; 43851a56ff6dSArtemy Kovalyov attr.ext.cq = devr->c0; 4386e126ba97SEli Cohen attr.ext.xrc.xrcd = devr->x0; 4387e126ba97SEli Cohen 4388e126ba97SEli Cohen devr->s0 = mlx5_ib_create_srq(devr->p0, &attr, NULL); 4389e126ba97SEli Cohen if (IS_ERR(devr->s0)) { 4390e126ba97SEli Cohen ret = PTR_ERR(devr->s0); 4391e126ba97SEli Cohen goto error4; 4392e126ba97SEli Cohen } 4393e126ba97SEli Cohen devr->s0->device = &dev->ib_dev; 4394e126ba97SEli Cohen devr->s0->pd = devr->p0; 4395e126ba97SEli Cohen devr->s0->uobject = NULL; 4396e126ba97SEli Cohen devr->s0->event_handler = NULL; 4397e126ba97SEli Cohen devr->s0->srq_context = NULL; 4398e126ba97SEli Cohen devr->s0->srq_type = IB_SRQT_XRC; 4399e126ba97SEli Cohen devr->s0->ext.xrc.xrcd = devr->x0; 44001a56ff6dSArtemy Kovalyov devr->s0->ext.cq = devr->c0; 4401e126ba97SEli Cohen atomic_inc(&devr->s0->ext.xrc.xrcd->usecnt); 44021a56ff6dSArtemy Kovalyov atomic_inc(&devr->s0->ext.cq->usecnt); 4403e126ba97SEli Cohen atomic_inc(&devr->p0->usecnt); 4404e126ba97SEli Cohen atomic_set(&devr->s0->usecnt, 0); 4405e126ba97SEli Cohen 44064aa17b28SHaggai Abramonvsky memset(&attr, 0, sizeof(attr)); 44074aa17b28SHaggai Abramonvsky attr.attr.max_sge = 1; 44084aa17b28SHaggai Abramonvsky attr.attr.max_wr = 1; 44094aa17b28SHaggai Abramonvsky attr.srq_type = IB_SRQT_BASIC; 44104aa17b28SHaggai Abramonvsky devr->s1 = mlx5_ib_create_srq(devr->p0, &attr, NULL); 44114aa17b28SHaggai Abramonvsky if (IS_ERR(devr->s1)) { 44124aa17b28SHaggai Abramonvsky ret = PTR_ERR(devr->s1); 44134aa17b28SHaggai Abramonvsky goto error5; 44144aa17b28SHaggai Abramonvsky } 44154aa17b28SHaggai Abramonvsky devr->s1->device = &dev->ib_dev; 44164aa17b28SHaggai Abramonvsky devr->s1->pd = devr->p0; 44174aa17b28SHaggai Abramonvsky devr->s1->uobject = NULL; 44184aa17b28SHaggai Abramonvsky devr->s1->event_handler = NULL; 44194aa17b28SHaggai Abramonvsky devr->s1->srq_context = NULL; 44204aa17b28SHaggai Abramonvsky devr->s1->srq_type = IB_SRQT_BASIC; 44211a56ff6dSArtemy Kovalyov devr->s1->ext.cq = devr->c0; 44224aa17b28SHaggai Abramonvsky atomic_inc(&devr->p0->usecnt); 44231a56ff6dSArtemy Kovalyov atomic_set(&devr->s1->usecnt, 0); 44244aa17b28SHaggai Abramonvsky 44257722f47eSHaggai Eran for (port = 0; port < ARRAY_SIZE(devr->ports); ++port) { 44267722f47eSHaggai Eran INIT_WORK(&devr->ports[port].pkey_change_work, 44277722f47eSHaggai Eran pkey_change_handler); 44287722f47eSHaggai Eran devr->ports[port].devr = devr; 44297722f47eSHaggai Eran } 44307722f47eSHaggai Eran 4431e126ba97SEli Cohen return 0; 4432e126ba97SEli Cohen 44334aa17b28SHaggai Abramonvsky error5: 44344aa17b28SHaggai Abramonvsky mlx5_ib_destroy_srq(devr->s0); 4435e126ba97SEli Cohen error4: 4436e126ba97SEli Cohen mlx5_ib_dealloc_xrcd(devr->x1); 4437e126ba97SEli Cohen error3: 4438e126ba97SEli Cohen mlx5_ib_dealloc_xrcd(devr->x0); 4439e126ba97SEli Cohen error2: 4440e126ba97SEli Cohen mlx5_ib_destroy_cq(devr->c0); 4441e126ba97SEli Cohen error1: 4442e126ba97SEli Cohen mlx5_ib_dealloc_pd(devr->p0); 4443e126ba97SEli Cohen error0: 4444e126ba97SEli Cohen return ret; 4445e126ba97SEli Cohen } 4446e126ba97SEli Cohen 4447e126ba97SEli Cohen static void destroy_dev_resources(struct mlx5_ib_resources *devr) 4448e126ba97SEli Cohen { 44497722f47eSHaggai Eran struct mlx5_ib_dev *dev = 44507722f47eSHaggai Eran container_of(devr, struct mlx5_ib_dev, devr); 44517722f47eSHaggai Eran int port; 44527722f47eSHaggai Eran 44534aa17b28SHaggai Abramonvsky mlx5_ib_destroy_srq(devr->s1); 4454e126ba97SEli Cohen mlx5_ib_destroy_srq(devr->s0); 4455e126ba97SEli Cohen mlx5_ib_dealloc_xrcd(devr->x0); 4456e126ba97SEli Cohen mlx5_ib_dealloc_xrcd(devr->x1); 4457e126ba97SEli Cohen mlx5_ib_destroy_cq(devr->c0); 4458e126ba97SEli Cohen mlx5_ib_dealloc_pd(devr->p0); 44597722f47eSHaggai Eran 44607722f47eSHaggai Eran /* Make sure no change P_Key work items are still executing */ 44617722f47eSHaggai Eran for (port = 0; port < dev->num_ports; ++port) 44627722f47eSHaggai Eran cancel_work_sync(&devr->ports[port].pkey_change_work); 4463e126ba97SEli Cohen } 4464e126ba97SEli Cohen 4465e53505a8SAchiad Shochat static u32 get_core_cap_flags(struct ib_device *ibdev) 4466e53505a8SAchiad Shochat { 4467e53505a8SAchiad Shochat struct mlx5_ib_dev *dev = to_mdev(ibdev); 4468e53505a8SAchiad Shochat enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, 1); 4469e53505a8SAchiad Shochat u8 l3_type_cap = MLX5_CAP_ROCE(dev->mdev, l3_type); 4470e53505a8SAchiad Shochat u8 roce_version_cap = MLX5_CAP_ROCE(dev->mdev, roce_version); 447185c7c014SDaniel Jurgens bool raw_support = !mlx5_core_mp_enabled(dev->mdev); 4472e53505a8SAchiad Shochat u32 ret = 0; 4473e53505a8SAchiad Shochat 4474e53505a8SAchiad Shochat if (ll == IB_LINK_LAYER_INFINIBAND) 4475e53505a8SAchiad Shochat return RDMA_CORE_PORT_IBA_IB; 4476e53505a8SAchiad Shochat 447785c7c014SDaniel Jurgens if (raw_support) 447872cd5717SOr Gerlitz ret = RDMA_CORE_PORT_RAW_PACKET; 447972cd5717SOr Gerlitz 4480e53505a8SAchiad Shochat if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV4_CAP)) 448172cd5717SOr Gerlitz return ret; 4482e53505a8SAchiad Shochat 4483e53505a8SAchiad Shochat if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV6_CAP)) 448472cd5717SOr Gerlitz return ret; 4485e53505a8SAchiad Shochat 4486e53505a8SAchiad Shochat if (roce_version_cap & MLX5_ROCE_VERSION_1_CAP) 4487e53505a8SAchiad Shochat ret |= RDMA_CORE_PORT_IBA_ROCE; 4488e53505a8SAchiad Shochat 4489e53505a8SAchiad Shochat if (roce_version_cap & MLX5_ROCE_VERSION_2_CAP) 4490e53505a8SAchiad Shochat ret |= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP; 4491e53505a8SAchiad Shochat 4492e53505a8SAchiad Shochat return ret; 4493e53505a8SAchiad Shochat } 4494e53505a8SAchiad Shochat 44957738613eSIra Weiny static int mlx5_port_immutable(struct ib_device *ibdev, u8 port_num, 44967738613eSIra Weiny struct ib_port_immutable *immutable) 44977738613eSIra Weiny { 44987738613eSIra Weiny struct ib_port_attr attr; 4499ca5b91d6SOr Gerlitz struct mlx5_ib_dev *dev = to_mdev(ibdev); 4500ca5b91d6SOr Gerlitz enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, port_num); 45017738613eSIra Weiny int err; 45027738613eSIra Weiny 4503c4550c63SOr Gerlitz immutable->core_cap_flags = get_core_cap_flags(ibdev); 4504c4550c63SOr Gerlitz 4505c4550c63SOr Gerlitz err = ib_query_port(ibdev, port_num, &attr); 45067738613eSIra Weiny if (err) 45077738613eSIra Weiny return err; 45087738613eSIra Weiny 45097738613eSIra Weiny immutable->pkey_tbl_len = attr.pkey_tbl_len; 45107738613eSIra Weiny immutable->gid_tbl_len = attr.gid_tbl_len; 4511e53505a8SAchiad Shochat immutable->core_cap_flags = get_core_cap_flags(ibdev); 4512ca5b91d6SOr Gerlitz if ((ll == IB_LINK_LAYER_INFINIBAND) || MLX5_CAP_GEN(dev->mdev, roce)) 4513337877a4SIra Weiny immutable->max_mad_size = IB_MGMT_MAD_SIZE; 45147738613eSIra Weiny 45157738613eSIra Weiny return 0; 45167738613eSIra Weiny } 45177738613eSIra Weiny 45188e6efa3aSMark Bloch static int mlx5_port_rep_immutable(struct ib_device *ibdev, u8 port_num, 45198e6efa3aSMark Bloch struct ib_port_immutable *immutable) 45208e6efa3aSMark Bloch { 45218e6efa3aSMark Bloch struct ib_port_attr attr; 45228e6efa3aSMark Bloch int err; 45238e6efa3aSMark Bloch 45248e6efa3aSMark Bloch immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET; 45258e6efa3aSMark Bloch 45268e6efa3aSMark Bloch err = ib_query_port(ibdev, port_num, &attr); 45278e6efa3aSMark Bloch if (err) 45288e6efa3aSMark Bloch return err; 45298e6efa3aSMark Bloch 45308e6efa3aSMark Bloch immutable->pkey_tbl_len = attr.pkey_tbl_len; 45318e6efa3aSMark Bloch immutable->gid_tbl_len = attr.gid_tbl_len; 45328e6efa3aSMark Bloch immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET; 45338e6efa3aSMark Bloch 45348e6efa3aSMark Bloch return 0; 45358e6efa3aSMark Bloch } 45368e6efa3aSMark Bloch 45379abb0d1bSLeon Romanovsky static void get_dev_fw_str(struct ib_device *ibdev, char *str) 4538c7342823SIra Weiny { 4539c7342823SIra Weiny struct mlx5_ib_dev *dev = 4540c7342823SIra Weiny container_of(ibdev, struct mlx5_ib_dev, ib_dev); 45419abb0d1bSLeon Romanovsky snprintf(str, IB_FW_VERSION_NAME_MAX, "%d.%d.%04d", 45429abb0d1bSLeon Romanovsky fw_rev_maj(dev->mdev), fw_rev_min(dev->mdev), 45439abb0d1bSLeon Romanovsky fw_rev_sub(dev->mdev)); 4544c7342823SIra Weiny } 4545c7342823SIra Weiny 454645f95acdSOr Gerlitz static int mlx5_eth_lag_init(struct mlx5_ib_dev *dev) 45479ef9c640SAviv Heller { 45489ef9c640SAviv Heller struct mlx5_core_dev *mdev = dev->mdev; 45499ef9c640SAviv Heller struct mlx5_flow_namespace *ns = mlx5_get_flow_namespace(mdev, 45509ef9c640SAviv Heller MLX5_FLOW_NAMESPACE_LAG); 45519ef9c640SAviv Heller struct mlx5_flow_table *ft; 45529ef9c640SAviv Heller int err; 45539ef9c640SAviv Heller 45549ef9c640SAviv Heller if (!ns || !mlx5_lag_is_active(mdev)) 45559ef9c640SAviv Heller return 0; 45569ef9c640SAviv Heller 45579ef9c640SAviv Heller err = mlx5_cmd_create_vport_lag(mdev); 45589ef9c640SAviv Heller if (err) 45599ef9c640SAviv Heller return err; 45609ef9c640SAviv Heller 45619ef9c640SAviv Heller ft = mlx5_create_lag_demux_flow_table(ns, 0, 0); 45629ef9c640SAviv Heller if (IS_ERR(ft)) { 45639ef9c640SAviv Heller err = PTR_ERR(ft); 45649ef9c640SAviv Heller goto err_destroy_vport_lag; 45659ef9c640SAviv Heller } 45669ef9c640SAviv Heller 45679a4ca38dSMark Bloch dev->flow_db->lag_demux_ft = ft; 45689ef9c640SAviv Heller return 0; 45699ef9c640SAviv Heller 45709ef9c640SAviv Heller err_destroy_vport_lag: 45719ef9c640SAviv Heller mlx5_cmd_destroy_vport_lag(mdev); 45729ef9c640SAviv Heller return err; 45739ef9c640SAviv Heller } 45749ef9c640SAviv Heller 457545f95acdSOr Gerlitz static void mlx5_eth_lag_cleanup(struct mlx5_ib_dev *dev) 45769ef9c640SAviv Heller { 45779ef9c640SAviv Heller struct mlx5_core_dev *mdev = dev->mdev; 45789ef9c640SAviv Heller 45799a4ca38dSMark Bloch if (dev->flow_db->lag_demux_ft) { 45809a4ca38dSMark Bloch mlx5_destroy_flow_table(dev->flow_db->lag_demux_ft); 45819a4ca38dSMark Bloch dev->flow_db->lag_demux_ft = NULL; 45829ef9c640SAviv Heller 45839ef9c640SAviv Heller mlx5_cmd_destroy_vport_lag(mdev); 45849ef9c640SAviv Heller } 45859ef9c640SAviv Heller } 45869ef9c640SAviv Heller 45877fd8aefbSDaniel Jurgens static int mlx5_add_netdev_notifier(struct mlx5_ib_dev *dev, u8 port_num) 4588fc24fc5eSAchiad Shochat { 4589e53505a8SAchiad Shochat int err; 4590e53505a8SAchiad Shochat 45917fd8aefbSDaniel Jurgens dev->roce[port_num].nb.notifier_call = mlx5_netdev_event; 45927fd8aefbSDaniel Jurgens err = register_netdevice_notifier(&dev->roce[port_num].nb); 45935ec8c83eSAviv Heller if (err) { 45947fd8aefbSDaniel Jurgens dev->roce[port_num].nb.notifier_call = NULL; 4595e53505a8SAchiad Shochat return err; 45965ec8c83eSAviv Heller } 4597e53505a8SAchiad Shochat 4598d012f5d6SOr Gerlitz return 0; 4599d012f5d6SOr Gerlitz } 4600d012f5d6SOr Gerlitz 46017fd8aefbSDaniel Jurgens static void mlx5_remove_netdev_notifier(struct mlx5_ib_dev *dev, u8 port_num) 4602e126ba97SEli Cohen { 46037fd8aefbSDaniel Jurgens if (dev->roce[port_num].nb.notifier_call) { 46047fd8aefbSDaniel Jurgens unregister_netdevice_notifier(&dev->roce[port_num].nb); 46057fd8aefbSDaniel Jurgens dev->roce[port_num].nb.notifier_call = NULL; 4606e126ba97SEli Cohen } 4607e126ba97SEli Cohen } 4608e126ba97SEli Cohen 46097fd8aefbSDaniel Jurgens static int mlx5_enable_eth(struct mlx5_ib_dev *dev, u8 port_num) 4610e126ba97SEli Cohen { 4611e126ba97SEli Cohen int err; 4612e126ba97SEli Cohen 4613ca5b91d6SOr Gerlitz if (MLX5_CAP_GEN(dev->mdev, roce)) { 4614e53505a8SAchiad Shochat err = mlx5_nic_vport_enable_roce(dev->mdev); 4615e53505a8SAchiad Shochat if (err) 46168e6efa3aSMark Bloch return err; 4617ca5b91d6SOr Gerlitz } 4618e53505a8SAchiad Shochat 461945f95acdSOr Gerlitz err = mlx5_eth_lag_init(dev); 46209ef9c640SAviv Heller if (err) 46219ef9c640SAviv Heller goto err_disable_roce; 46229ef9c640SAviv Heller 4623e53505a8SAchiad Shochat return 0; 4624e53505a8SAchiad Shochat 46259ef9c640SAviv Heller err_disable_roce: 4626ca5b91d6SOr Gerlitz if (MLX5_CAP_GEN(dev->mdev, roce)) 46279ef9c640SAviv Heller mlx5_nic_vport_disable_roce(dev->mdev); 46289ef9c640SAviv Heller 4629e53505a8SAchiad Shochat return err; 4630fc24fc5eSAchiad Shochat } 4631fc24fc5eSAchiad Shochat 463245f95acdSOr Gerlitz static void mlx5_disable_eth(struct mlx5_ib_dev *dev) 4633fc24fc5eSAchiad Shochat { 463445f95acdSOr Gerlitz mlx5_eth_lag_cleanup(dev); 4635ca5b91d6SOr Gerlitz if (MLX5_CAP_GEN(dev->mdev, roce)) 4636e53505a8SAchiad Shochat mlx5_nic_vport_disable_roce(dev->mdev); 4637fc24fc5eSAchiad Shochat } 4638fc24fc5eSAchiad Shochat 4639e1f24a79SParav Pandit struct mlx5_ib_counter { 46407c16f477SKamal Heib const char *name; 46417c16f477SKamal Heib size_t offset; 46427c16f477SKamal Heib }; 46437c16f477SKamal Heib 46447c16f477SKamal Heib #define INIT_Q_COUNTER(_name) \ 46457c16f477SKamal Heib { .name = #_name, .offset = MLX5_BYTE_OFF(query_q_counter_out, _name)} 46467c16f477SKamal Heib 4647e1f24a79SParav Pandit static const struct mlx5_ib_counter basic_q_cnts[] = { 46487c16f477SKamal Heib INIT_Q_COUNTER(rx_write_requests), 46497c16f477SKamal Heib INIT_Q_COUNTER(rx_read_requests), 46507c16f477SKamal Heib INIT_Q_COUNTER(rx_atomic_requests), 46517c16f477SKamal Heib INIT_Q_COUNTER(out_of_buffer), 46527c16f477SKamal Heib }; 46537c16f477SKamal Heib 4654e1f24a79SParav Pandit static const struct mlx5_ib_counter out_of_seq_q_cnts[] = { 46557c16f477SKamal Heib INIT_Q_COUNTER(out_of_sequence), 46567c16f477SKamal Heib }; 46577c16f477SKamal Heib 4658e1f24a79SParav Pandit static const struct mlx5_ib_counter retrans_q_cnts[] = { 46597c16f477SKamal Heib INIT_Q_COUNTER(duplicate_request), 46607c16f477SKamal Heib INIT_Q_COUNTER(rnr_nak_retry_err), 46617c16f477SKamal Heib INIT_Q_COUNTER(packet_seq_err), 46627c16f477SKamal Heib INIT_Q_COUNTER(implied_nak_seq_err), 46637c16f477SKamal Heib INIT_Q_COUNTER(local_ack_timeout_err), 46647c16f477SKamal Heib }; 46657c16f477SKamal Heib 4666e1f24a79SParav Pandit #define INIT_CONG_COUNTER(_name) \ 4667e1f24a79SParav Pandit { .name = #_name, .offset = \ 4668e1f24a79SParav Pandit MLX5_BYTE_OFF(query_cong_statistics_out, _name ## _high)} 4669e1f24a79SParav Pandit 4670e1f24a79SParav Pandit static const struct mlx5_ib_counter cong_cnts[] = { 4671e1f24a79SParav Pandit INIT_CONG_COUNTER(rp_cnp_ignored), 4672e1f24a79SParav Pandit INIT_CONG_COUNTER(rp_cnp_handled), 4673e1f24a79SParav Pandit INIT_CONG_COUNTER(np_ecn_marked_roce_packets), 4674e1f24a79SParav Pandit INIT_CONG_COUNTER(np_cnp_sent), 4675e1f24a79SParav Pandit }; 4676e1f24a79SParav Pandit 467758dcb60aSParav Pandit static const struct mlx5_ib_counter extended_err_cnts[] = { 467858dcb60aSParav Pandit INIT_Q_COUNTER(resp_local_length_error), 467958dcb60aSParav Pandit INIT_Q_COUNTER(resp_cqe_error), 468058dcb60aSParav Pandit INIT_Q_COUNTER(req_cqe_error), 468158dcb60aSParav Pandit INIT_Q_COUNTER(req_remote_invalid_request), 468258dcb60aSParav Pandit INIT_Q_COUNTER(req_remote_access_errors), 468358dcb60aSParav Pandit INIT_Q_COUNTER(resp_remote_access_errors), 468458dcb60aSParav Pandit INIT_Q_COUNTER(resp_cqe_flush_error), 468558dcb60aSParav Pandit INIT_Q_COUNTER(req_cqe_flush_error), 468658dcb60aSParav Pandit }; 468758dcb60aSParav Pandit 46889f876f3dSTalat Batheesh #define INIT_EXT_PPCNT_COUNTER(_name) \ 46899f876f3dSTalat Batheesh { .name = #_name, .offset = \ 46909f876f3dSTalat Batheesh MLX5_BYTE_OFF(ppcnt_reg, \ 46919f876f3dSTalat Batheesh counter_set.eth_extended_cntrs_grp_data_layout._name##_high)} 46929f876f3dSTalat Batheesh 46939f876f3dSTalat Batheesh static const struct mlx5_ib_counter ext_ppcnt_cnts[] = { 46949f876f3dSTalat Batheesh INIT_EXT_PPCNT_COUNTER(rx_icrc_encapsulated), 46959f876f3dSTalat Batheesh }; 46969f876f3dSTalat Batheesh 4697e1f24a79SParav Pandit static void mlx5_ib_dealloc_counters(struct mlx5_ib_dev *dev) 46980837e86aSMark Bloch { 4699aac4492eSDaniel Jurgens int i; 47000837e86aSMark Bloch 47017c16f477SKamal Heib for (i = 0; i < dev->num_ports; i++) { 4702aac4492eSDaniel Jurgens if (dev->port[i].cnts.set_id) 47030837e86aSMark Bloch mlx5_core_dealloc_q_counter(dev->mdev, 4704e1f24a79SParav Pandit dev->port[i].cnts.set_id); 4705e1f24a79SParav Pandit kfree(dev->port[i].cnts.names); 4706e1f24a79SParav Pandit kfree(dev->port[i].cnts.offsets); 47077c16f477SKamal Heib } 47087c16f477SKamal Heib } 47097c16f477SKamal Heib 4710e1f24a79SParav Pandit static int __mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev, 4711e1f24a79SParav Pandit struct mlx5_ib_counters *cnts) 47127c16f477SKamal Heib { 47137c16f477SKamal Heib u32 num_counters; 47147c16f477SKamal Heib 47157c16f477SKamal Heib num_counters = ARRAY_SIZE(basic_q_cnts); 47167c16f477SKamal Heib 47177c16f477SKamal Heib if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt)) 47187c16f477SKamal Heib num_counters += ARRAY_SIZE(out_of_seq_q_cnts); 47197c16f477SKamal Heib 47207c16f477SKamal Heib if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters)) 47217c16f477SKamal Heib num_counters += ARRAY_SIZE(retrans_q_cnts); 472258dcb60aSParav Pandit 472358dcb60aSParav Pandit if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters)) 472458dcb60aSParav Pandit num_counters += ARRAY_SIZE(extended_err_cnts); 472558dcb60aSParav Pandit 4726e1f24a79SParav Pandit cnts->num_q_counters = num_counters; 47277c16f477SKamal Heib 4728e1f24a79SParav Pandit if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) { 4729e1f24a79SParav Pandit cnts->num_cong_counters = ARRAY_SIZE(cong_cnts); 4730e1f24a79SParav Pandit num_counters += ARRAY_SIZE(cong_cnts); 4731e1f24a79SParav Pandit } 47329f876f3dSTalat Batheesh if (MLX5_CAP_PCAM_FEATURE(dev->mdev, rx_icrc_encapsulated_counter)) { 47339f876f3dSTalat Batheesh cnts->num_ext_ppcnt_counters = ARRAY_SIZE(ext_ppcnt_cnts); 47349f876f3dSTalat Batheesh num_counters += ARRAY_SIZE(ext_ppcnt_cnts); 47359f876f3dSTalat Batheesh } 4736e1f24a79SParav Pandit cnts->names = kcalloc(num_counters, sizeof(cnts->names), GFP_KERNEL); 4737e1f24a79SParav Pandit if (!cnts->names) 47387c16f477SKamal Heib return -ENOMEM; 47397c16f477SKamal Heib 4740e1f24a79SParav Pandit cnts->offsets = kcalloc(num_counters, 4741e1f24a79SParav Pandit sizeof(cnts->offsets), GFP_KERNEL); 4742e1f24a79SParav Pandit if (!cnts->offsets) 47437c16f477SKamal Heib goto err_names; 47447c16f477SKamal Heib 47457c16f477SKamal Heib return 0; 47467c16f477SKamal Heib 47477c16f477SKamal Heib err_names: 4748e1f24a79SParav Pandit kfree(cnts->names); 4749aac4492eSDaniel Jurgens cnts->names = NULL; 47507c16f477SKamal Heib return -ENOMEM; 47517c16f477SKamal Heib } 47527c16f477SKamal Heib 4753e1f24a79SParav Pandit static void mlx5_ib_fill_counters(struct mlx5_ib_dev *dev, 47547c16f477SKamal Heib const char **names, 47557c16f477SKamal Heib size_t *offsets) 47567c16f477SKamal Heib { 47577c16f477SKamal Heib int i; 47587c16f477SKamal Heib int j = 0; 47597c16f477SKamal Heib 47607c16f477SKamal Heib for (i = 0; i < ARRAY_SIZE(basic_q_cnts); i++, j++) { 47617c16f477SKamal Heib names[j] = basic_q_cnts[i].name; 47627c16f477SKamal Heib offsets[j] = basic_q_cnts[i].offset; 47637c16f477SKamal Heib } 47647c16f477SKamal Heib 47657c16f477SKamal Heib if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt)) { 47667c16f477SKamal Heib for (i = 0; i < ARRAY_SIZE(out_of_seq_q_cnts); i++, j++) { 47677c16f477SKamal Heib names[j] = out_of_seq_q_cnts[i].name; 47687c16f477SKamal Heib offsets[j] = out_of_seq_q_cnts[i].offset; 47697c16f477SKamal Heib } 47707c16f477SKamal Heib } 47717c16f477SKamal Heib 47727c16f477SKamal Heib if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters)) { 47737c16f477SKamal Heib for (i = 0; i < ARRAY_SIZE(retrans_q_cnts); i++, j++) { 47747c16f477SKamal Heib names[j] = retrans_q_cnts[i].name; 47757c16f477SKamal Heib offsets[j] = retrans_q_cnts[i].offset; 47767c16f477SKamal Heib } 47777c16f477SKamal Heib } 4778e1f24a79SParav Pandit 477958dcb60aSParav Pandit if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters)) { 478058dcb60aSParav Pandit for (i = 0; i < ARRAY_SIZE(extended_err_cnts); i++, j++) { 478158dcb60aSParav Pandit names[j] = extended_err_cnts[i].name; 478258dcb60aSParav Pandit offsets[j] = extended_err_cnts[i].offset; 478358dcb60aSParav Pandit } 478458dcb60aSParav Pandit } 478558dcb60aSParav Pandit 4786e1f24a79SParav Pandit if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) { 4787e1f24a79SParav Pandit for (i = 0; i < ARRAY_SIZE(cong_cnts); i++, j++) { 4788e1f24a79SParav Pandit names[j] = cong_cnts[i].name; 4789e1f24a79SParav Pandit offsets[j] = cong_cnts[i].offset; 4790e1f24a79SParav Pandit } 4791e1f24a79SParav Pandit } 47929f876f3dSTalat Batheesh 47939f876f3dSTalat Batheesh if (MLX5_CAP_PCAM_FEATURE(dev->mdev, rx_icrc_encapsulated_counter)) { 47949f876f3dSTalat Batheesh for (i = 0; i < ARRAY_SIZE(ext_ppcnt_cnts); i++, j++) { 47959f876f3dSTalat Batheesh names[j] = ext_ppcnt_cnts[i].name; 47969f876f3dSTalat Batheesh offsets[j] = ext_ppcnt_cnts[i].offset; 47979f876f3dSTalat Batheesh } 47989f876f3dSTalat Batheesh } 47990837e86aSMark Bloch } 48000837e86aSMark Bloch 4801e1f24a79SParav Pandit static int mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev) 48020837e86aSMark Bloch { 4803aac4492eSDaniel Jurgens int err = 0; 48040837e86aSMark Bloch int i; 48050837e86aSMark Bloch 48060837e86aSMark Bloch for (i = 0; i < dev->num_ports; i++) { 4807aac4492eSDaniel Jurgens err = __mlx5_ib_alloc_counters(dev, &dev->port[i].cnts); 4808aac4492eSDaniel Jurgens if (err) 4809aac4492eSDaniel Jurgens goto err_alloc; 48107c16f477SKamal Heib 4811aac4492eSDaniel Jurgens mlx5_ib_fill_counters(dev, dev->port[i].cnts.names, 4812aac4492eSDaniel Jurgens dev->port[i].cnts.offsets); 4813aac4492eSDaniel Jurgens 4814aac4492eSDaniel Jurgens err = mlx5_core_alloc_q_counter(dev->mdev, 4815aac4492eSDaniel Jurgens &dev->port[i].cnts.set_id); 4816aac4492eSDaniel Jurgens if (err) { 48170837e86aSMark Bloch mlx5_ib_warn(dev, 48180837e86aSMark Bloch "couldn't allocate queue counter for port %d, err %d\n", 4819aac4492eSDaniel Jurgens i + 1, err); 4820aac4492eSDaniel Jurgens goto err_alloc; 48210837e86aSMark Bloch } 4822aac4492eSDaniel Jurgens dev->port[i].cnts.set_id_valid = true; 48230837e86aSMark Bloch } 48240837e86aSMark Bloch 48250837e86aSMark Bloch return 0; 48260837e86aSMark Bloch 4827aac4492eSDaniel Jurgens err_alloc: 4828aac4492eSDaniel Jurgens mlx5_ib_dealloc_counters(dev); 4829aac4492eSDaniel Jurgens return err; 48300837e86aSMark Bloch } 48310837e86aSMark Bloch 48320ad17a8fSMark Bloch static struct rdma_hw_stats *mlx5_ib_alloc_hw_stats(struct ib_device *ibdev, 48330ad17a8fSMark Bloch u8 port_num) 48340ad17a8fSMark Bloch { 48357c16f477SKamal Heib struct mlx5_ib_dev *dev = to_mdev(ibdev); 48367c16f477SKamal Heib struct mlx5_ib_port *port = &dev->port[port_num - 1]; 48370ad17a8fSMark Bloch 48380ad17a8fSMark Bloch /* We support only per port stats */ 48390ad17a8fSMark Bloch if (port_num == 0) 48400ad17a8fSMark Bloch return NULL; 48410ad17a8fSMark Bloch 4842e1f24a79SParav Pandit return rdma_alloc_hw_stats_struct(port->cnts.names, 4843e1f24a79SParav Pandit port->cnts.num_q_counters + 48449f876f3dSTalat Batheesh port->cnts.num_cong_counters + 48459f876f3dSTalat Batheesh port->cnts.num_ext_ppcnt_counters, 48460ad17a8fSMark Bloch RDMA_HW_STATS_DEFAULT_LIFESPAN); 48470ad17a8fSMark Bloch } 48480ad17a8fSMark Bloch 4849aac4492eSDaniel Jurgens static int mlx5_ib_query_q_counters(struct mlx5_core_dev *mdev, 4850e1f24a79SParav Pandit struct mlx5_ib_port *port, 4851e1f24a79SParav Pandit struct rdma_hw_stats *stats) 4852e1f24a79SParav Pandit { 4853e1f24a79SParav Pandit int outlen = MLX5_ST_SZ_BYTES(query_q_counter_out); 4854e1f24a79SParav Pandit void *out; 4855e1f24a79SParav Pandit __be32 val; 4856e1f24a79SParav Pandit int ret, i; 4857e1f24a79SParav Pandit 48581b9a07eeSLeon Romanovsky out = kvzalloc(outlen, GFP_KERNEL); 4859e1f24a79SParav Pandit if (!out) 4860e1f24a79SParav Pandit return -ENOMEM; 4861e1f24a79SParav Pandit 4862aac4492eSDaniel Jurgens ret = mlx5_core_query_q_counter(mdev, 4863e1f24a79SParav Pandit port->cnts.set_id, 0, 4864e1f24a79SParav Pandit out, outlen); 4865e1f24a79SParav Pandit if (ret) 4866e1f24a79SParav Pandit goto free; 4867e1f24a79SParav Pandit 4868e1f24a79SParav Pandit for (i = 0; i < port->cnts.num_q_counters; i++) { 4869e1f24a79SParav Pandit val = *(__be32 *)(out + port->cnts.offsets[i]); 4870e1f24a79SParav Pandit stats->value[i] = (u64)be32_to_cpu(val); 4871e1f24a79SParav Pandit } 4872e1f24a79SParav Pandit 4873e1f24a79SParav Pandit free: 4874e1f24a79SParav Pandit kvfree(out); 4875e1f24a79SParav Pandit return ret; 4876e1f24a79SParav Pandit } 4877e1f24a79SParav Pandit 48789f876f3dSTalat Batheesh static int mlx5_ib_query_ext_ppcnt_counters(struct mlx5_ib_dev *dev, 48799f876f3dSTalat Batheesh struct mlx5_ib_port *port, 48809f876f3dSTalat Batheesh struct rdma_hw_stats *stats) 48819f876f3dSTalat Batheesh { 48829f876f3dSTalat Batheesh int offset = port->cnts.num_q_counters + port->cnts.num_cong_counters; 48839f876f3dSTalat Batheesh int sz = MLX5_ST_SZ_BYTES(ppcnt_reg); 48849f876f3dSTalat Batheesh int ret, i; 48859f876f3dSTalat Batheesh void *out; 48869f876f3dSTalat Batheesh 48879f876f3dSTalat Batheesh out = kvzalloc(sz, GFP_KERNEL); 48889f876f3dSTalat Batheesh if (!out) 48899f876f3dSTalat Batheesh return -ENOMEM; 48909f876f3dSTalat Batheesh 48919f876f3dSTalat Batheesh ret = mlx5_cmd_query_ext_ppcnt_counters(dev->mdev, out); 48929f876f3dSTalat Batheesh if (ret) 48939f876f3dSTalat Batheesh goto free; 48949f876f3dSTalat Batheesh 48959f876f3dSTalat Batheesh for (i = 0; i < port->cnts.num_ext_ppcnt_counters; i++) { 48969f876f3dSTalat Batheesh stats->value[i + offset] = 48979f876f3dSTalat Batheesh be64_to_cpup((__be64 *)(out + 48989f876f3dSTalat Batheesh port->cnts.offsets[i + offset])); 48999f876f3dSTalat Batheesh } 49009f876f3dSTalat Batheesh 49019f876f3dSTalat Batheesh free: 49029f876f3dSTalat Batheesh kvfree(out); 49039f876f3dSTalat Batheesh return ret; 49049f876f3dSTalat Batheesh } 49059f876f3dSTalat Batheesh 49060ad17a8fSMark Bloch static int mlx5_ib_get_hw_stats(struct ib_device *ibdev, 49070ad17a8fSMark Bloch struct rdma_hw_stats *stats, 49087c16f477SKamal Heib u8 port_num, int index) 49090ad17a8fSMark Bloch { 49100ad17a8fSMark Bloch struct mlx5_ib_dev *dev = to_mdev(ibdev); 49117c16f477SKamal Heib struct mlx5_ib_port *port = &dev->port[port_num - 1]; 4912aac4492eSDaniel Jurgens struct mlx5_core_dev *mdev; 4913e1f24a79SParav Pandit int ret, num_counters; 4914aac4492eSDaniel Jurgens u8 mdev_port_num; 49150ad17a8fSMark Bloch 49167c16f477SKamal Heib if (!stats) 4917e1f24a79SParav Pandit return -EINVAL; 49180ad17a8fSMark Bloch 49199f876f3dSTalat Batheesh num_counters = port->cnts.num_q_counters + 49209f876f3dSTalat Batheesh port->cnts.num_cong_counters + 49219f876f3dSTalat Batheesh port->cnts.num_ext_ppcnt_counters; 4922aac4492eSDaniel Jurgens 4923aac4492eSDaniel Jurgens /* q_counters are per IB device, query the master mdev */ 4924aac4492eSDaniel Jurgens ret = mlx5_ib_query_q_counters(dev->mdev, port, stats); 49250ad17a8fSMark Bloch if (ret) 4926e1f24a79SParav Pandit return ret; 49270ad17a8fSMark Bloch 49289f876f3dSTalat Batheesh if (MLX5_CAP_PCAM_FEATURE(dev->mdev, rx_icrc_encapsulated_counter)) { 49299f876f3dSTalat Batheesh ret = mlx5_ib_query_ext_ppcnt_counters(dev, port, stats); 49309f876f3dSTalat Batheesh if (ret) 49319f876f3dSTalat Batheesh return ret; 49329f876f3dSTalat Batheesh } 49339f876f3dSTalat Batheesh 4934e1f24a79SParav Pandit if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) { 4935aac4492eSDaniel Jurgens mdev = mlx5_ib_get_native_port_mdev(dev, port_num, 4936aac4492eSDaniel Jurgens &mdev_port_num); 4937aac4492eSDaniel Jurgens if (!mdev) { 4938aac4492eSDaniel Jurgens /* If port is not affiliated yet, its in down state 4939aac4492eSDaniel Jurgens * which doesn't have any counters yet, so it would be 4940aac4492eSDaniel Jurgens * zero. So no need to read from the HCA. 4941aac4492eSDaniel Jurgens */ 4942aac4492eSDaniel Jurgens goto done; 4943aac4492eSDaniel Jurgens } 494471a0ff65SMajd Dibbiny ret = mlx5_lag_query_cong_counters(dev->mdev, 494571a0ff65SMajd Dibbiny stats->value + 494671a0ff65SMajd Dibbiny port->cnts.num_q_counters, 494771a0ff65SMajd Dibbiny port->cnts.num_cong_counters, 494871a0ff65SMajd Dibbiny port->cnts.offsets + 494971a0ff65SMajd Dibbiny port->cnts.num_q_counters); 4950aac4492eSDaniel Jurgens 4951aac4492eSDaniel Jurgens mlx5_ib_put_native_port_mdev(dev, port_num); 4952e1f24a79SParav Pandit if (ret) 4953e1f24a79SParav Pandit return ret; 49540ad17a8fSMark Bloch } 49557c16f477SKamal Heib 4956aac4492eSDaniel Jurgens done: 4957e1f24a79SParav Pandit return num_counters; 49580ad17a8fSMark Bloch } 49590ad17a8fSMark Bloch 49608e959601SNiranjana Vishwanathapura static void mlx5_ib_free_rdma_netdev(struct net_device *netdev) 49618e959601SNiranjana Vishwanathapura { 49628e959601SNiranjana Vishwanathapura return mlx5_rdma_netdev_free(netdev); 49638e959601SNiranjana Vishwanathapura } 49648e959601SNiranjana Vishwanathapura 4965693dfd5aSErez Shitrit static struct net_device* 4966693dfd5aSErez Shitrit mlx5_ib_alloc_rdma_netdev(struct ib_device *hca, 4967693dfd5aSErez Shitrit u8 port_num, 4968693dfd5aSErez Shitrit enum rdma_netdev_t type, 4969693dfd5aSErez Shitrit const char *name, 4970693dfd5aSErez Shitrit unsigned char name_assign_type, 4971693dfd5aSErez Shitrit void (*setup)(struct net_device *)) 4972693dfd5aSErez Shitrit { 49738e959601SNiranjana Vishwanathapura struct net_device *netdev; 49748e959601SNiranjana Vishwanathapura struct rdma_netdev *rn; 49758e959601SNiranjana Vishwanathapura 4976693dfd5aSErez Shitrit if (type != RDMA_NETDEV_IPOIB) 4977693dfd5aSErez Shitrit return ERR_PTR(-EOPNOTSUPP); 4978693dfd5aSErez Shitrit 49798e959601SNiranjana Vishwanathapura netdev = mlx5_rdma_netdev_alloc(to_mdev(hca)->mdev, hca, 4980693dfd5aSErez Shitrit name, setup); 49818e959601SNiranjana Vishwanathapura if (likely(!IS_ERR_OR_NULL(netdev))) { 49828e959601SNiranjana Vishwanathapura rn = netdev_priv(netdev); 49838e959601SNiranjana Vishwanathapura rn->free_rdma_netdev = mlx5_ib_free_rdma_netdev; 4984693dfd5aSErez Shitrit } 49858e959601SNiranjana Vishwanathapura return netdev; 4986693dfd5aSErez Shitrit } 4987693dfd5aSErez Shitrit 4988fe248c3aSMaor Gottlieb static void delay_drop_debugfs_cleanup(struct mlx5_ib_dev *dev) 4989fe248c3aSMaor Gottlieb { 4990fe248c3aSMaor Gottlieb if (!dev->delay_drop.dbg) 4991fe248c3aSMaor Gottlieb return; 4992fe248c3aSMaor Gottlieb debugfs_remove_recursive(dev->delay_drop.dbg->dir_debugfs); 4993fe248c3aSMaor Gottlieb kfree(dev->delay_drop.dbg); 4994fe248c3aSMaor Gottlieb dev->delay_drop.dbg = NULL; 4995fe248c3aSMaor Gottlieb } 4996fe248c3aSMaor Gottlieb 499703404e8aSMaor Gottlieb static void cancel_delay_drop(struct mlx5_ib_dev *dev) 499803404e8aSMaor Gottlieb { 499903404e8aSMaor Gottlieb if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP)) 500003404e8aSMaor Gottlieb return; 500103404e8aSMaor Gottlieb 500203404e8aSMaor Gottlieb cancel_work_sync(&dev->delay_drop.delay_drop_work); 5003fe248c3aSMaor Gottlieb delay_drop_debugfs_cleanup(dev); 5004fe248c3aSMaor Gottlieb } 5005fe248c3aSMaor Gottlieb 5006fe248c3aSMaor Gottlieb static ssize_t delay_drop_timeout_read(struct file *filp, char __user *buf, 5007fe248c3aSMaor Gottlieb size_t count, loff_t *pos) 5008fe248c3aSMaor Gottlieb { 5009fe248c3aSMaor Gottlieb struct mlx5_ib_delay_drop *delay_drop = filp->private_data; 5010fe248c3aSMaor Gottlieb char lbuf[20]; 5011fe248c3aSMaor Gottlieb int len; 5012fe248c3aSMaor Gottlieb 5013fe248c3aSMaor Gottlieb len = snprintf(lbuf, sizeof(lbuf), "%u\n", delay_drop->timeout); 5014fe248c3aSMaor Gottlieb return simple_read_from_buffer(buf, count, pos, lbuf, len); 5015fe248c3aSMaor Gottlieb } 5016fe248c3aSMaor Gottlieb 5017fe248c3aSMaor Gottlieb static ssize_t delay_drop_timeout_write(struct file *filp, const char __user *buf, 5018fe248c3aSMaor Gottlieb size_t count, loff_t *pos) 5019fe248c3aSMaor Gottlieb { 5020fe248c3aSMaor Gottlieb struct mlx5_ib_delay_drop *delay_drop = filp->private_data; 5021fe248c3aSMaor Gottlieb u32 timeout; 5022fe248c3aSMaor Gottlieb u32 var; 5023fe248c3aSMaor Gottlieb 5024fe248c3aSMaor Gottlieb if (kstrtouint_from_user(buf, count, 0, &var)) 5025fe248c3aSMaor Gottlieb return -EFAULT; 5026fe248c3aSMaor Gottlieb 5027fe248c3aSMaor Gottlieb timeout = min_t(u32, roundup(var, 100), MLX5_MAX_DELAY_DROP_TIMEOUT_MS * 5028fe248c3aSMaor Gottlieb 1000); 5029fe248c3aSMaor Gottlieb if (timeout != var) 5030fe248c3aSMaor Gottlieb mlx5_ib_dbg(delay_drop->dev, "Round delay drop timeout to %u usec\n", 5031fe248c3aSMaor Gottlieb timeout); 5032fe248c3aSMaor Gottlieb 5033fe248c3aSMaor Gottlieb delay_drop->timeout = timeout; 5034fe248c3aSMaor Gottlieb 5035fe248c3aSMaor Gottlieb return count; 5036fe248c3aSMaor Gottlieb } 5037fe248c3aSMaor Gottlieb 5038fe248c3aSMaor Gottlieb static const struct file_operations fops_delay_drop_timeout = { 5039fe248c3aSMaor Gottlieb .owner = THIS_MODULE, 5040fe248c3aSMaor Gottlieb .open = simple_open, 5041fe248c3aSMaor Gottlieb .write = delay_drop_timeout_write, 5042fe248c3aSMaor Gottlieb .read = delay_drop_timeout_read, 5043fe248c3aSMaor Gottlieb }; 5044fe248c3aSMaor Gottlieb 5045fe248c3aSMaor Gottlieb static int delay_drop_debugfs_init(struct mlx5_ib_dev *dev) 5046fe248c3aSMaor Gottlieb { 5047fe248c3aSMaor Gottlieb struct mlx5_ib_dbg_delay_drop *dbg; 5048fe248c3aSMaor Gottlieb 5049fe248c3aSMaor Gottlieb if (!mlx5_debugfs_root) 5050fe248c3aSMaor Gottlieb return 0; 5051fe248c3aSMaor Gottlieb 5052fe248c3aSMaor Gottlieb dbg = kzalloc(sizeof(*dbg), GFP_KERNEL); 5053fe248c3aSMaor Gottlieb if (!dbg) 5054fe248c3aSMaor Gottlieb return -ENOMEM; 5055fe248c3aSMaor Gottlieb 5056cbafad87SSudip Mukherjee dev->delay_drop.dbg = dbg; 5057cbafad87SSudip Mukherjee 5058fe248c3aSMaor Gottlieb dbg->dir_debugfs = 5059fe248c3aSMaor Gottlieb debugfs_create_dir("delay_drop", 5060fe248c3aSMaor Gottlieb dev->mdev->priv.dbg_root); 5061fe248c3aSMaor Gottlieb if (!dbg->dir_debugfs) 5062cbafad87SSudip Mukherjee goto out_debugfs; 5063fe248c3aSMaor Gottlieb 5064fe248c3aSMaor Gottlieb dbg->events_cnt_debugfs = 5065fe248c3aSMaor Gottlieb debugfs_create_atomic_t("num_timeout_events", 0400, 5066fe248c3aSMaor Gottlieb dbg->dir_debugfs, 5067fe248c3aSMaor Gottlieb &dev->delay_drop.events_cnt); 5068fe248c3aSMaor Gottlieb if (!dbg->events_cnt_debugfs) 5069fe248c3aSMaor Gottlieb goto out_debugfs; 5070fe248c3aSMaor Gottlieb 5071fe248c3aSMaor Gottlieb dbg->rqs_cnt_debugfs = 5072fe248c3aSMaor Gottlieb debugfs_create_atomic_t("num_rqs", 0400, 5073fe248c3aSMaor Gottlieb dbg->dir_debugfs, 5074fe248c3aSMaor Gottlieb &dev->delay_drop.rqs_cnt); 5075fe248c3aSMaor Gottlieb if (!dbg->rqs_cnt_debugfs) 5076fe248c3aSMaor Gottlieb goto out_debugfs; 5077fe248c3aSMaor Gottlieb 5078fe248c3aSMaor Gottlieb dbg->timeout_debugfs = 5079fe248c3aSMaor Gottlieb debugfs_create_file("timeout", 0600, 5080fe248c3aSMaor Gottlieb dbg->dir_debugfs, 5081fe248c3aSMaor Gottlieb &dev->delay_drop, 5082fe248c3aSMaor Gottlieb &fops_delay_drop_timeout); 5083fe248c3aSMaor Gottlieb if (!dbg->timeout_debugfs) 5084fe248c3aSMaor Gottlieb goto out_debugfs; 5085fe248c3aSMaor Gottlieb 5086fe248c3aSMaor Gottlieb return 0; 5087fe248c3aSMaor Gottlieb 5088fe248c3aSMaor Gottlieb out_debugfs: 5089fe248c3aSMaor Gottlieb delay_drop_debugfs_cleanup(dev); 5090fe248c3aSMaor Gottlieb return -ENOMEM; 509103404e8aSMaor Gottlieb } 509203404e8aSMaor Gottlieb 509303404e8aSMaor Gottlieb static void init_delay_drop(struct mlx5_ib_dev *dev) 509403404e8aSMaor Gottlieb { 509503404e8aSMaor Gottlieb if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP)) 509603404e8aSMaor Gottlieb return; 509703404e8aSMaor Gottlieb 509803404e8aSMaor Gottlieb mutex_init(&dev->delay_drop.lock); 509903404e8aSMaor Gottlieb dev->delay_drop.dev = dev; 510003404e8aSMaor Gottlieb dev->delay_drop.activate = false; 510103404e8aSMaor Gottlieb dev->delay_drop.timeout = MLX5_MAX_DELAY_DROP_TIMEOUT_MS * 1000; 510203404e8aSMaor Gottlieb INIT_WORK(&dev->delay_drop.delay_drop_work, delay_drop_handler); 5103fe248c3aSMaor Gottlieb atomic_set(&dev->delay_drop.rqs_cnt, 0); 5104fe248c3aSMaor Gottlieb atomic_set(&dev->delay_drop.events_cnt, 0); 5105fe248c3aSMaor Gottlieb 5106fe248c3aSMaor Gottlieb if (delay_drop_debugfs_init(dev)) 5107fe248c3aSMaor Gottlieb mlx5_ib_warn(dev, "Failed to init delay drop debugfs\n"); 510803404e8aSMaor Gottlieb } 510903404e8aSMaor Gottlieb 511084305d71SLeon Romanovsky static const struct cpumask * 511184305d71SLeon Romanovsky mlx5_ib_get_vector_affinity(struct ib_device *ibdev, int comp_vector) 511240b24403SSagi Grimberg { 511340b24403SSagi Grimberg struct mlx5_ib_dev *dev = to_mdev(ibdev); 511440b24403SSagi Grimberg 51156082d9c9SIsrael Rukshin return mlx5_get_vector_affinity_hint(dev->mdev, comp_vector); 511640b24403SSagi Grimberg } 511740b24403SSagi Grimberg 511832f69e4bSDaniel Jurgens /* The mlx5_ib_multiport_mutex should be held when calling this function */ 511932f69e4bSDaniel Jurgens static void mlx5_ib_unbind_slave_port(struct mlx5_ib_dev *ibdev, 512032f69e4bSDaniel Jurgens struct mlx5_ib_multiport_info *mpi) 512132f69e4bSDaniel Jurgens { 512232f69e4bSDaniel Jurgens u8 port_num = mlx5_core_native_port_num(mpi->mdev) - 1; 512332f69e4bSDaniel Jurgens struct mlx5_ib_port *port = &ibdev->port[port_num]; 512432f69e4bSDaniel Jurgens int comps; 512532f69e4bSDaniel Jurgens int err; 512632f69e4bSDaniel Jurgens int i; 512732f69e4bSDaniel Jurgens 5128a9e546e7SParav Pandit mlx5_ib_cleanup_cong_debugfs(ibdev, port_num); 5129a9e546e7SParav Pandit 513032f69e4bSDaniel Jurgens spin_lock(&port->mp.mpi_lock); 513132f69e4bSDaniel Jurgens if (!mpi->ibdev) { 513232f69e4bSDaniel Jurgens spin_unlock(&port->mp.mpi_lock); 513332f69e4bSDaniel Jurgens return; 513432f69e4bSDaniel Jurgens } 513532f69e4bSDaniel Jurgens mpi->ibdev = NULL; 513632f69e4bSDaniel Jurgens 513732f69e4bSDaniel Jurgens spin_unlock(&port->mp.mpi_lock); 513832f69e4bSDaniel Jurgens mlx5_remove_netdev_notifier(ibdev, port_num); 513932f69e4bSDaniel Jurgens spin_lock(&port->mp.mpi_lock); 514032f69e4bSDaniel Jurgens 514132f69e4bSDaniel Jurgens comps = mpi->mdev_refcnt; 514232f69e4bSDaniel Jurgens if (comps) { 514332f69e4bSDaniel Jurgens mpi->unaffiliate = true; 514432f69e4bSDaniel Jurgens init_completion(&mpi->unref_comp); 514532f69e4bSDaniel Jurgens spin_unlock(&port->mp.mpi_lock); 514632f69e4bSDaniel Jurgens 514732f69e4bSDaniel Jurgens for (i = 0; i < comps; i++) 514832f69e4bSDaniel Jurgens wait_for_completion(&mpi->unref_comp); 514932f69e4bSDaniel Jurgens 515032f69e4bSDaniel Jurgens spin_lock(&port->mp.mpi_lock); 515132f69e4bSDaniel Jurgens mpi->unaffiliate = false; 515232f69e4bSDaniel Jurgens } 515332f69e4bSDaniel Jurgens 515432f69e4bSDaniel Jurgens port->mp.mpi = NULL; 515532f69e4bSDaniel Jurgens 515632f69e4bSDaniel Jurgens list_add_tail(&mpi->list, &mlx5_ib_unaffiliated_port_list); 515732f69e4bSDaniel Jurgens 515832f69e4bSDaniel Jurgens spin_unlock(&port->mp.mpi_lock); 515932f69e4bSDaniel Jurgens 516032f69e4bSDaniel Jurgens err = mlx5_nic_vport_unaffiliate_multiport(mpi->mdev); 516132f69e4bSDaniel Jurgens 516232f69e4bSDaniel Jurgens mlx5_ib_dbg(ibdev, "unaffiliated port %d\n", port_num + 1); 516332f69e4bSDaniel Jurgens /* Log an error, still needed to cleanup the pointers and add 516432f69e4bSDaniel Jurgens * it back to the list. 516532f69e4bSDaniel Jurgens */ 516632f69e4bSDaniel Jurgens if (err) 516732f69e4bSDaniel Jurgens mlx5_ib_err(ibdev, "Failed to unaffiliate port %u\n", 516832f69e4bSDaniel Jurgens port_num + 1); 516932f69e4bSDaniel Jurgens 517032f69e4bSDaniel Jurgens ibdev->roce[port_num].last_port_state = IB_PORT_DOWN; 517132f69e4bSDaniel Jurgens } 517232f69e4bSDaniel Jurgens 517332f69e4bSDaniel Jurgens /* The mlx5_ib_multiport_mutex should be held when calling this function */ 517432f69e4bSDaniel Jurgens static bool mlx5_ib_bind_slave_port(struct mlx5_ib_dev *ibdev, 517532f69e4bSDaniel Jurgens struct mlx5_ib_multiport_info *mpi) 517632f69e4bSDaniel Jurgens { 517732f69e4bSDaniel Jurgens u8 port_num = mlx5_core_native_port_num(mpi->mdev) - 1; 517832f69e4bSDaniel Jurgens int err; 517932f69e4bSDaniel Jurgens 518032f69e4bSDaniel Jurgens spin_lock(&ibdev->port[port_num].mp.mpi_lock); 518132f69e4bSDaniel Jurgens if (ibdev->port[port_num].mp.mpi) { 518232f69e4bSDaniel Jurgens mlx5_ib_warn(ibdev, "port %d already affiliated.\n", 518332f69e4bSDaniel Jurgens port_num + 1); 518432f69e4bSDaniel Jurgens spin_unlock(&ibdev->port[port_num].mp.mpi_lock); 518532f69e4bSDaniel Jurgens return false; 518632f69e4bSDaniel Jurgens } 518732f69e4bSDaniel Jurgens 518832f69e4bSDaniel Jurgens ibdev->port[port_num].mp.mpi = mpi; 518932f69e4bSDaniel Jurgens mpi->ibdev = ibdev; 519032f69e4bSDaniel Jurgens spin_unlock(&ibdev->port[port_num].mp.mpi_lock); 519132f69e4bSDaniel Jurgens 519232f69e4bSDaniel Jurgens err = mlx5_nic_vport_affiliate_multiport(ibdev->mdev, mpi->mdev); 519332f69e4bSDaniel Jurgens if (err) 519432f69e4bSDaniel Jurgens goto unbind; 519532f69e4bSDaniel Jurgens 519632f69e4bSDaniel Jurgens err = get_port_caps(ibdev, mlx5_core_native_port_num(mpi->mdev)); 519732f69e4bSDaniel Jurgens if (err) 519832f69e4bSDaniel Jurgens goto unbind; 519932f69e4bSDaniel Jurgens 520032f69e4bSDaniel Jurgens err = mlx5_add_netdev_notifier(ibdev, port_num); 520132f69e4bSDaniel Jurgens if (err) { 520232f69e4bSDaniel Jurgens mlx5_ib_err(ibdev, "failed adding netdev notifier for port %u\n", 520332f69e4bSDaniel Jurgens port_num + 1); 520432f69e4bSDaniel Jurgens goto unbind; 520532f69e4bSDaniel Jurgens } 520632f69e4bSDaniel Jurgens 5207a9e546e7SParav Pandit err = mlx5_ib_init_cong_debugfs(ibdev, port_num); 5208a9e546e7SParav Pandit if (err) 5209a9e546e7SParav Pandit goto unbind; 5210a9e546e7SParav Pandit 521132f69e4bSDaniel Jurgens return true; 521232f69e4bSDaniel Jurgens 521332f69e4bSDaniel Jurgens unbind: 521432f69e4bSDaniel Jurgens mlx5_ib_unbind_slave_port(ibdev, mpi); 521532f69e4bSDaniel Jurgens return false; 521632f69e4bSDaniel Jurgens } 521732f69e4bSDaniel Jurgens 521832f69e4bSDaniel Jurgens static int mlx5_ib_init_multiport_master(struct mlx5_ib_dev *dev) 521932f69e4bSDaniel Jurgens { 522032f69e4bSDaniel Jurgens int port_num = mlx5_core_native_port_num(dev->mdev) - 1; 522132f69e4bSDaniel Jurgens enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev, 522232f69e4bSDaniel Jurgens port_num + 1); 522332f69e4bSDaniel Jurgens struct mlx5_ib_multiport_info *mpi; 522432f69e4bSDaniel Jurgens int err; 522532f69e4bSDaniel Jurgens int i; 522632f69e4bSDaniel Jurgens 522732f69e4bSDaniel Jurgens if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET) 522832f69e4bSDaniel Jurgens return 0; 522932f69e4bSDaniel Jurgens 523032f69e4bSDaniel Jurgens err = mlx5_query_nic_vport_system_image_guid(dev->mdev, 523132f69e4bSDaniel Jurgens &dev->sys_image_guid); 523232f69e4bSDaniel Jurgens if (err) 523332f69e4bSDaniel Jurgens return err; 523432f69e4bSDaniel Jurgens 523532f69e4bSDaniel Jurgens err = mlx5_nic_vport_enable_roce(dev->mdev); 523632f69e4bSDaniel Jurgens if (err) 523732f69e4bSDaniel Jurgens return err; 523832f69e4bSDaniel Jurgens 523932f69e4bSDaniel Jurgens mutex_lock(&mlx5_ib_multiport_mutex); 524032f69e4bSDaniel Jurgens for (i = 0; i < dev->num_ports; i++) { 524132f69e4bSDaniel Jurgens bool bound = false; 524232f69e4bSDaniel Jurgens 524332f69e4bSDaniel Jurgens /* build a stub multiport info struct for the native port. */ 524432f69e4bSDaniel Jurgens if (i == port_num) { 524532f69e4bSDaniel Jurgens mpi = kzalloc(sizeof(*mpi), GFP_KERNEL); 524632f69e4bSDaniel Jurgens if (!mpi) { 524732f69e4bSDaniel Jurgens mutex_unlock(&mlx5_ib_multiport_mutex); 524832f69e4bSDaniel Jurgens mlx5_nic_vport_disable_roce(dev->mdev); 524932f69e4bSDaniel Jurgens return -ENOMEM; 525032f69e4bSDaniel Jurgens } 525132f69e4bSDaniel Jurgens 525232f69e4bSDaniel Jurgens mpi->is_master = true; 525332f69e4bSDaniel Jurgens mpi->mdev = dev->mdev; 525432f69e4bSDaniel Jurgens mpi->sys_image_guid = dev->sys_image_guid; 525532f69e4bSDaniel Jurgens dev->port[i].mp.mpi = mpi; 525632f69e4bSDaniel Jurgens mpi->ibdev = dev; 525732f69e4bSDaniel Jurgens mpi = NULL; 525832f69e4bSDaniel Jurgens continue; 525932f69e4bSDaniel Jurgens } 526032f69e4bSDaniel Jurgens 526132f69e4bSDaniel Jurgens list_for_each_entry(mpi, &mlx5_ib_unaffiliated_port_list, 526232f69e4bSDaniel Jurgens list) { 526332f69e4bSDaniel Jurgens if (dev->sys_image_guid == mpi->sys_image_guid && 526432f69e4bSDaniel Jurgens (mlx5_core_native_port_num(mpi->mdev) - 1) == i) { 526532f69e4bSDaniel Jurgens bound = mlx5_ib_bind_slave_port(dev, mpi); 526632f69e4bSDaniel Jurgens } 526732f69e4bSDaniel Jurgens 526832f69e4bSDaniel Jurgens if (bound) { 526932f69e4bSDaniel Jurgens dev_dbg(&mpi->mdev->pdev->dev, "removing port from unaffiliated list.\n"); 527032f69e4bSDaniel Jurgens mlx5_ib_dbg(dev, "port %d bound\n", i + 1); 527132f69e4bSDaniel Jurgens list_del(&mpi->list); 527232f69e4bSDaniel Jurgens break; 527332f69e4bSDaniel Jurgens } 527432f69e4bSDaniel Jurgens } 527532f69e4bSDaniel Jurgens if (!bound) { 527632f69e4bSDaniel Jurgens get_port_caps(dev, i + 1); 527732f69e4bSDaniel Jurgens mlx5_ib_dbg(dev, "no free port found for port %d\n", 527832f69e4bSDaniel Jurgens i + 1); 527932f69e4bSDaniel Jurgens } 528032f69e4bSDaniel Jurgens } 528132f69e4bSDaniel Jurgens 528232f69e4bSDaniel Jurgens list_add_tail(&dev->ib_dev_list, &mlx5_ib_dev_list); 528332f69e4bSDaniel Jurgens mutex_unlock(&mlx5_ib_multiport_mutex); 528432f69e4bSDaniel Jurgens return err; 528532f69e4bSDaniel Jurgens } 528632f69e4bSDaniel Jurgens 528732f69e4bSDaniel Jurgens static void mlx5_ib_cleanup_multiport_master(struct mlx5_ib_dev *dev) 528832f69e4bSDaniel Jurgens { 528932f69e4bSDaniel Jurgens int port_num = mlx5_core_native_port_num(dev->mdev) - 1; 529032f69e4bSDaniel Jurgens enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev, 529132f69e4bSDaniel Jurgens port_num + 1); 529232f69e4bSDaniel Jurgens int i; 529332f69e4bSDaniel Jurgens 529432f69e4bSDaniel Jurgens if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET) 529532f69e4bSDaniel Jurgens return; 529632f69e4bSDaniel Jurgens 529732f69e4bSDaniel Jurgens mutex_lock(&mlx5_ib_multiport_mutex); 529832f69e4bSDaniel Jurgens for (i = 0; i < dev->num_ports; i++) { 529932f69e4bSDaniel Jurgens if (dev->port[i].mp.mpi) { 530032f69e4bSDaniel Jurgens /* Destroy the native port stub */ 530132f69e4bSDaniel Jurgens if (i == port_num) { 530232f69e4bSDaniel Jurgens kfree(dev->port[i].mp.mpi); 530332f69e4bSDaniel Jurgens dev->port[i].mp.mpi = NULL; 530432f69e4bSDaniel Jurgens } else { 530532f69e4bSDaniel Jurgens mlx5_ib_dbg(dev, "unbinding port_num: %d\n", i + 1); 530632f69e4bSDaniel Jurgens mlx5_ib_unbind_slave_port(dev, dev->port[i].mp.mpi); 530732f69e4bSDaniel Jurgens } 530832f69e4bSDaniel Jurgens } 530932f69e4bSDaniel Jurgens } 531032f69e4bSDaniel Jurgens 531132f69e4bSDaniel Jurgens mlx5_ib_dbg(dev, "removing from devlist\n"); 531232f69e4bSDaniel Jurgens list_del(&dev->ib_dev_list); 531332f69e4bSDaniel Jurgens mutex_unlock(&mlx5_ib_multiport_mutex); 531432f69e4bSDaniel Jurgens 531532f69e4bSDaniel Jurgens mlx5_nic_vport_disable_roce(dev->mdev); 531632f69e4bSDaniel Jurgens } 531732f69e4bSDaniel Jurgens 531824da0016SAriel Levkovich ADD_UVERBS_ATTRIBUTES_SIMPLE(mlx5_ib_dm, UVERBS_OBJECT_DM, 531924da0016SAriel Levkovich UVERBS_METHOD_DM_ALLOC, 532024da0016SAriel Levkovich &UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_ALLOC_DM_RESP_START_OFFSET, 532124da0016SAriel Levkovich UVERBS_ATTR_TYPE(u64), 532224da0016SAriel Levkovich UA_FLAGS(UVERBS_ATTR_SPEC_F_MANDATORY)), 532324da0016SAriel Levkovich &UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_ALLOC_DM_RESP_PAGE_INDEX, 532424da0016SAriel Levkovich UVERBS_ATTR_TYPE(u16), 532524da0016SAriel Levkovich UA_FLAGS(UVERBS_ATTR_SPEC_F_MANDATORY))); 532624da0016SAriel Levkovich 5327c6475a0bSAviad Yehezkel ADD_UVERBS_ATTRIBUTES_SIMPLE(mlx5_ib_flow_action, UVERBS_OBJECT_FLOW_ACTION, 5328c6475a0bSAviad Yehezkel UVERBS_METHOD_FLOW_ACTION_ESP_CREATE, 5329c6475a0bSAviad Yehezkel &UVERBS_ATTR_PTR_IN(MLX5_IB_ATTR_CREATE_FLOW_ACTION_FLAGS, 5330c6475a0bSAviad Yehezkel UVERBS_ATTR_TYPE(u64), 5331c6475a0bSAviad Yehezkel UA_FLAGS(UVERBS_ATTR_SPEC_F_MANDATORY))); 5332c6475a0bSAviad Yehezkel 5333c59450c4SYishai Hadas #define NUM_TREES 3 53348c84660bSMatan Barak static int populate_specs_root(struct mlx5_ib_dev *dev) 53358c84660bSMatan Barak { 53368c84660bSMatan Barak const struct uverbs_object_tree_def *default_root[NUM_TREES + 1] = { 53378c84660bSMatan Barak uverbs_default_get_objects()}; 53388c84660bSMatan Barak size_t num_trees = 1; 53398c84660bSMatan Barak 5340c6475a0bSAviad Yehezkel if (mlx5_accel_ipsec_device_caps(dev->mdev) & MLX5_ACCEL_IPSEC_CAP_DEVICE && 5341c6475a0bSAviad Yehezkel !WARN_ON(num_trees >= ARRAY_SIZE(default_root))) 5342c6475a0bSAviad Yehezkel default_root[num_trees++] = &mlx5_ib_flow_action; 5343c6475a0bSAviad Yehezkel 534424da0016SAriel Levkovich if (MLX5_CAP_DEV_MEM(dev->mdev, memic) && 534524da0016SAriel Levkovich !WARN_ON(num_trees >= ARRAY_SIZE(default_root))) 534624da0016SAriel Levkovich default_root[num_trees++] = &mlx5_ib_dm; 534724da0016SAriel Levkovich 5348c59450c4SYishai Hadas if (MLX5_CAP_GEN_64(dev->mdev, general_obj_types) & 5349c59450c4SYishai Hadas MLX5_GENERAL_OBJ_TYPES_CAP_UCTX && 5350c59450c4SYishai Hadas !WARN_ON(num_trees >= ARRAY_SIZE(default_root))) 5351c59450c4SYishai Hadas default_root[num_trees++] = mlx5_ib_get_devx_tree(); 5352c59450c4SYishai Hadas 53538c84660bSMatan Barak dev->ib_dev.specs_root = 53548c84660bSMatan Barak uverbs_alloc_spec_tree(num_trees, default_root); 53558c84660bSMatan Barak 53568c84660bSMatan Barak return PTR_ERR_OR_ZERO(dev->ib_dev.specs_root); 53578c84660bSMatan Barak } 53588c84660bSMatan Barak 53598c84660bSMatan Barak static void depopulate_specs_root(struct mlx5_ib_dev *dev) 53608c84660bSMatan Barak { 53618c84660bSMatan Barak uverbs_free_spec_tree(dev->ib_dev.specs_root); 53628c84660bSMatan Barak } 53638c84660bSMatan Barak 53641a1e03dcSRaed Salem static int mlx5_ib_read_counters(struct ib_counters *counters, 53651a1e03dcSRaed Salem struct ib_counters_read_attr *read_attr, 53661a1e03dcSRaed Salem struct uverbs_attr_bundle *attrs) 53671a1e03dcSRaed Salem { 53681a1e03dcSRaed Salem struct mlx5_ib_mcounters *mcounters = to_mcounters(counters); 53691a1e03dcSRaed Salem struct mlx5_read_counters_attr mread_attr = {}; 53701a1e03dcSRaed Salem struct mlx5_ib_flow_counters_desc *desc; 53711a1e03dcSRaed Salem int ret, i; 53721a1e03dcSRaed Salem 53731a1e03dcSRaed Salem mutex_lock(&mcounters->mcntrs_mutex); 53741a1e03dcSRaed Salem if (mcounters->cntrs_max_index > read_attr->ncounters) { 53751a1e03dcSRaed Salem ret = -EINVAL; 53761a1e03dcSRaed Salem goto err_bound; 53771a1e03dcSRaed Salem } 53781a1e03dcSRaed Salem 53791a1e03dcSRaed Salem mread_attr.out = kcalloc(mcounters->counters_num, sizeof(u64), 53801a1e03dcSRaed Salem GFP_KERNEL); 53811a1e03dcSRaed Salem if (!mread_attr.out) { 53821a1e03dcSRaed Salem ret = -ENOMEM; 53831a1e03dcSRaed Salem goto err_bound; 53841a1e03dcSRaed Salem } 53851a1e03dcSRaed Salem 53861a1e03dcSRaed Salem mread_attr.hw_cntrs_hndl = mcounters->hw_cntrs_hndl; 53871a1e03dcSRaed Salem mread_attr.flags = read_attr->flags; 53881a1e03dcSRaed Salem ret = mcounters->read_counters(counters->device, &mread_attr); 53891a1e03dcSRaed Salem if (ret) 53901a1e03dcSRaed Salem goto err_read; 53911a1e03dcSRaed Salem 53921a1e03dcSRaed Salem /* do the pass over the counters data array to assign according to the 53931a1e03dcSRaed Salem * descriptions and indexing pairs 53941a1e03dcSRaed Salem */ 53951a1e03dcSRaed Salem desc = mcounters->counters_data; 53961a1e03dcSRaed Salem for (i = 0; i < mcounters->ncounters; i++) 53971a1e03dcSRaed Salem read_attr->counters_buff[desc[i].index] += mread_attr.out[desc[i].description]; 53981a1e03dcSRaed Salem 53991a1e03dcSRaed Salem err_read: 54001a1e03dcSRaed Salem kfree(mread_attr.out); 54011a1e03dcSRaed Salem err_bound: 54021a1e03dcSRaed Salem mutex_unlock(&mcounters->mcntrs_mutex); 54031a1e03dcSRaed Salem return ret; 54041a1e03dcSRaed Salem } 54051a1e03dcSRaed Salem 5406b29e2a13SRaed Salem static int mlx5_ib_destroy_counters(struct ib_counters *counters) 5407b29e2a13SRaed Salem { 5408b29e2a13SRaed Salem struct mlx5_ib_mcounters *mcounters = to_mcounters(counters); 5409b29e2a13SRaed Salem 54103b3233fbSRaed Salem counters_clear_description(counters); 54113b3233fbSRaed Salem if (mcounters->hw_cntrs_hndl) 54123b3233fbSRaed Salem mlx5_fc_destroy(to_mdev(counters->device)->mdev, 54133b3233fbSRaed Salem mcounters->hw_cntrs_hndl); 54143b3233fbSRaed Salem 5415b29e2a13SRaed Salem kfree(mcounters); 5416b29e2a13SRaed Salem 5417b29e2a13SRaed Salem return 0; 5418b29e2a13SRaed Salem } 5419b29e2a13SRaed Salem 5420b29e2a13SRaed Salem static struct ib_counters *mlx5_ib_create_counters(struct ib_device *device, 5421b29e2a13SRaed Salem struct uverbs_attr_bundle *attrs) 5422b29e2a13SRaed Salem { 5423b29e2a13SRaed Salem struct mlx5_ib_mcounters *mcounters; 5424b29e2a13SRaed Salem 5425b29e2a13SRaed Salem mcounters = kzalloc(sizeof(*mcounters), GFP_KERNEL); 5426b29e2a13SRaed Salem if (!mcounters) 5427b29e2a13SRaed Salem return ERR_PTR(-ENOMEM); 5428b29e2a13SRaed Salem 54293b3233fbSRaed Salem mutex_init(&mcounters->mcntrs_mutex); 54303b3233fbSRaed Salem 5431b29e2a13SRaed Salem return &mcounters->ibcntrs; 5432b29e2a13SRaed Salem } 5433b29e2a13SRaed Salem 5434b5ca15adSMark Bloch void mlx5_ib_stage_init_cleanup(struct mlx5_ib_dev *dev) 5435e126ba97SEli Cohen { 543632f69e4bSDaniel Jurgens mlx5_ib_cleanup_multiport_master(dev); 54373cc297dbSMark Bloch #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING 54383cc297dbSMark Bloch cleanup_srcu_struct(&dev->mr_srcu); 54393cc297dbSMark Bloch #endif 544016c1975fSMark Bloch kfree(dev->port); 544116c1975fSMark Bloch } 544216c1975fSMark Bloch 5443b5ca15adSMark Bloch int mlx5_ib_stage_init_init(struct mlx5_ib_dev *dev) 544416c1975fSMark Bloch { 544516c1975fSMark Bloch struct mlx5_core_dev *mdev = dev->mdev; 54464babcf97SAviv Heller const char *name; 5447e126ba97SEli Cohen int err; 544832f69e4bSDaniel Jurgens int i; 5449e126ba97SEli Cohen 5450508562d6SDaniel Jurgens dev->port = kcalloc(dev->num_ports, sizeof(*dev->port), 54510837e86aSMark Bloch GFP_KERNEL); 54520837e86aSMark Bloch if (!dev->port) 545316c1975fSMark Bloch return -ENOMEM; 54540837e86aSMark Bloch 545532f69e4bSDaniel Jurgens for (i = 0; i < dev->num_ports; i++) { 545632f69e4bSDaniel Jurgens spin_lock_init(&dev->port[i].mp.mpi_lock); 545732f69e4bSDaniel Jurgens rwlock_init(&dev->roce[i].netdev_lock); 545832f69e4bSDaniel Jurgens } 545932f69e4bSDaniel Jurgens 546032f69e4bSDaniel Jurgens err = mlx5_ib_init_multiport_master(dev); 5461e126ba97SEli Cohen if (err) 54620837e86aSMark Bloch goto err_free_port; 5463e126ba97SEli Cohen 546432f69e4bSDaniel Jurgens if (!mlx5_core_mp_enabled(mdev)) { 546532f69e4bSDaniel Jurgens for (i = 1; i <= dev->num_ports; i++) { 546632f69e4bSDaniel Jurgens err = get_port_caps(dev, i); 546732f69e4bSDaniel Jurgens if (err) 546832f69e4bSDaniel Jurgens break; 546932f69e4bSDaniel Jurgens } 547032f69e4bSDaniel Jurgens } else { 547132f69e4bSDaniel Jurgens err = get_port_caps(dev, mlx5_core_native_port_num(mdev)); 547232f69e4bSDaniel Jurgens } 547332f69e4bSDaniel Jurgens if (err) 547432f69e4bSDaniel Jurgens goto err_mp; 547532f69e4bSDaniel Jurgens 54761b5daf11SMajd Dibbiny if (mlx5_use_mad_ifc(dev)) 5477e126ba97SEli Cohen get_ext_port_caps(dev); 5478e126ba97SEli Cohen 54794babcf97SAviv Heller if (!mlx5_lag_is_active(mdev)) 54804babcf97SAviv Heller name = "mlx5_%d"; 54814babcf97SAviv Heller else 54824babcf97SAviv Heller name = "mlx5_bond_%d"; 54834babcf97SAviv Heller 54844babcf97SAviv Heller strlcpy(dev->ib_dev.name, name, IB_DEVICE_NAME_MAX); 5485e126ba97SEli Cohen dev->ib_dev.owner = THIS_MODULE; 5486e126ba97SEli Cohen dev->ib_dev.node_type = RDMA_NODE_IB_CA; 5487c6790aa9SSagi Grimberg dev->ib_dev.local_dma_lkey = 0 /* not supported for now */; 5488e126ba97SEli Cohen dev->ib_dev.phys_port_cnt = dev->num_ports; 5489233d05d2SSaeed Mahameed dev->ib_dev.num_comp_vectors = 5490233d05d2SSaeed Mahameed dev->mdev->priv.eq_table.num_comp_vectors; 54919b0c289eSBart Van Assche dev->ib_dev.dev.parent = &mdev->pdev->dev; 5492e126ba97SEli Cohen 54933cc297dbSMark Bloch mutex_init(&dev->cap_mask_mutex); 54943cc297dbSMark Bloch INIT_LIST_HEAD(&dev->qp_list); 54953cc297dbSMark Bloch spin_lock_init(&dev->reset_flow_resource_lock); 54963cc297dbSMark Bloch 549724da0016SAriel Levkovich spin_lock_init(&dev->memic.memic_lock); 549824da0016SAriel Levkovich dev->memic.dev = mdev; 549924da0016SAriel Levkovich 55003cc297dbSMark Bloch #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING 55013cc297dbSMark Bloch err = init_srcu_struct(&dev->mr_srcu); 55023cc297dbSMark Bloch if (err) 55033cc297dbSMark Bloch goto err_free_port; 55043cc297dbSMark Bloch #endif 55053cc297dbSMark Bloch 550616c1975fSMark Bloch return 0; 550732f69e4bSDaniel Jurgens err_mp: 550832f69e4bSDaniel Jurgens mlx5_ib_cleanup_multiport_master(dev); 550916c1975fSMark Bloch 551016c1975fSMark Bloch err_free_port: 551116c1975fSMark Bloch kfree(dev->port); 551216c1975fSMark Bloch 551316c1975fSMark Bloch return -ENOMEM; 551416c1975fSMark Bloch } 551516c1975fSMark Bloch 55169a4ca38dSMark Bloch static int mlx5_ib_stage_flow_db_init(struct mlx5_ib_dev *dev) 55179a4ca38dSMark Bloch { 55189a4ca38dSMark Bloch dev->flow_db = kzalloc(sizeof(*dev->flow_db), GFP_KERNEL); 55199a4ca38dSMark Bloch 55209a4ca38dSMark Bloch if (!dev->flow_db) 55219a4ca38dSMark Bloch return -ENOMEM; 55229a4ca38dSMark Bloch 55239a4ca38dSMark Bloch mutex_init(&dev->flow_db->lock); 55249a4ca38dSMark Bloch 55259a4ca38dSMark Bloch return 0; 55269a4ca38dSMark Bloch } 55279a4ca38dSMark Bloch 5528b5ca15adSMark Bloch int mlx5_ib_stage_rep_flow_db_init(struct mlx5_ib_dev *dev) 5529b5ca15adSMark Bloch { 5530b5ca15adSMark Bloch struct mlx5_ib_dev *nic_dev; 5531b5ca15adSMark Bloch 5532b5ca15adSMark Bloch nic_dev = mlx5_ib_get_uplink_ibdev(dev->mdev->priv.eswitch); 5533b5ca15adSMark Bloch 5534b5ca15adSMark Bloch if (!nic_dev) 5535b5ca15adSMark Bloch return -EINVAL; 5536b5ca15adSMark Bloch 5537b5ca15adSMark Bloch dev->flow_db = nic_dev->flow_db; 5538b5ca15adSMark Bloch 5539b5ca15adSMark Bloch return 0; 5540b5ca15adSMark Bloch } 5541b5ca15adSMark Bloch 55429a4ca38dSMark Bloch static void mlx5_ib_stage_flow_db_cleanup(struct mlx5_ib_dev *dev) 55439a4ca38dSMark Bloch { 55449a4ca38dSMark Bloch kfree(dev->flow_db); 55459a4ca38dSMark Bloch } 55469a4ca38dSMark Bloch 5547b5ca15adSMark Bloch int mlx5_ib_stage_caps_init(struct mlx5_ib_dev *dev) 554816c1975fSMark Bloch { 554916c1975fSMark Bloch struct mlx5_core_dev *mdev = dev->mdev; 555016c1975fSMark Bloch int err; 555116c1975fSMark Bloch 5552e126ba97SEli Cohen dev->ib_dev.uverbs_abi_ver = MLX5_IB_UVERBS_ABI_VERSION; 5553e126ba97SEli Cohen dev->ib_dev.uverbs_cmd_mask = 5554e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) | 5555e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) | 5556e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_QUERY_PORT) | 5557e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_ALLOC_PD) | 5558e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) | 555941c450fdSMoni Shoua (1ull << IB_USER_VERBS_CMD_CREATE_AH) | 556041c450fdSMoni Shoua (1ull << IB_USER_VERBS_CMD_DESTROY_AH) | 5561e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_REG_MR) | 556256e11d62SNoa Osherovich (1ull << IB_USER_VERBS_CMD_REREG_MR) | 5563e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_DEREG_MR) | 5564e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) | 5565e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_CREATE_CQ) | 5566e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_RESIZE_CQ) | 5567e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) | 5568e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_CREATE_QP) | 5569e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_MODIFY_QP) | 5570e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_QUERY_QP) | 5571e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_DESTROY_QP) | 5572e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST) | 5573e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_DETACH_MCAST) | 5574e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_CREATE_SRQ) | 5575e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ) | 5576e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_QUERY_SRQ) | 5577e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ) | 5578e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_CREATE_XSRQ) | 5579e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_OPEN_QP); 55801707cb4aSHaggai Eran dev->ib_dev.uverbs_ex_cmd_mask = 5581d4584ddfSMatan Barak (1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE) | 5582d4584ddfSMatan Barak (1ull << IB_USER_VERBS_EX_CMD_CREATE_CQ) | 55837d29f349SBodong Wang (1ull << IB_USER_VERBS_EX_CMD_CREATE_QP) | 5584b0e9df6dSYonatan Cohen (1ull << IB_USER_VERBS_EX_CMD_MODIFY_QP) | 5585b0e9df6dSYonatan Cohen (1ull << IB_USER_VERBS_EX_CMD_MODIFY_CQ); 5586e126ba97SEli Cohen 5587e126ba97SEli Cohen dev->ib_dev.query_device = mlx5_ib_query_device; 5588ebd61f68SAchiad Shochat dev->ib_dev.get_link_layer = mlx5_ib_port_link_layer; 5589e126ba97SEli Cohen dev->ib_dev.query_gid = mlx5_ib_query_gid; 55903cca2606SAchiad Shochat dev->ib_dev.add_gid = mlx5_ib_add_gid; 55913cca2606SAchiad Shochat dev->ib_dev.del_gid = mlx5_ib_del_gid; 5592e126ba97SEli Cohen dev->ib_dev.query_pkey = mlx5_ib_query_pkey; 5593e126ba97SEli Cohen dev->ib_dev.modify_device = mlx5_ib_modify_device; 5594e126ba97SEli Cohen dev->ib_dev.modify_port = mlx5_ib_modify_port; 5595e126ba97SEli Cohen dev->ib_dev.alloc_ucontext = mlx5_ib_alloc_ucontext; 5596e126ba97SEli Cohen dev->ib_dev.dealloc_ucontext = mlx5_ib_dealloc_ucontext; 5597e126ba97SEli Cohen dev->ib_dev.mmap = mlx5_ib_mmap; 5598e126ba97SEli Cohen dev->ib_dev.alloc_pd = mlx5_ib_alloc_pd; 5599e126ba97SEli Cohen dev->ib_dev.dealloc_pd = mlx5_ib_dealloc_pd; 5600e126ba97SEli Cohen dev->ib_dev.create_ah = mlx5_ib_create_ah; 5601e126ba97SEli Cohen dev->ib_dev.query_ah = mlx5_ib_query_ah; 5602e126ba97SEli Cohen dev->ib_dev.destroy_ah = mlx5_ib_destroy_ah; 5603e126ba97SEli Cohen dev->ib_dev.create_srq = mlx5_ib_create_srq; 5604e126ba97SEli Cohen dev->ib_dev.modify_srq = mlx5_ib_modify_srq; 5605e126ba97SEli Cohen dev->ib_dev.query_srq = mlx5_ib_query_srq; 5606e126ba97SEli Cohen dev->ib_dev.destroy_srq = mlx5_ib_destroy_srq; 5607e126ba97SEli Cohen dev->ib_dev.post_srq_recv = mlx5_ib_post_srq_recv; 5608e126ba97SEli Cohen dev->ib_dev.create_qp = mlx5_ib_create_qp; 5609e126ba97SEli Cohen dev->ib_dev.modify_qp = mlx5_ib_modify_qp; 5610e126ba97SEli Cohen dev->ib_dev.query_qp = mlx5_ib_query_qp; 5611e126ba97SEli Cohen dev->ib_dev.destroy_qp = mlx5_ib_destroy_qp; 5612d0e84c0aSYishai Hadas dev->ib_dev.drain_sq = mlx5_ib_drain_sq; 5613d0e84c0aSYishai Hadas dev->ib_dev.drain_rq = mlx5_ib_drain_rq; 5614e126ba97SEli Cohen dev->ib_dev.post_send = mlx5_ib_post_send; 5615e126ba97SEli Cohen dev->ib_dev.post_recv = mlx5_ib_post_recv; 5616e126ba97SEli Cohen dev->ib_dev.create_cq = mlx5_ib_create_cq; 5617e126ba97SEli Cohen dev->ib_dev.modify_cq = mlx5_ib_modify_cq; 5618e126ba97SEli Cohen dev->ib_dev.resize_cq = mlx5_ib_resize_cq; 5619e126ba97SEli Cohen dev->ib_dev.destroy_cq = mlx5_ib_destroy_cq; 5620e126ba97SEli Cohen dev->ib_dev.poll_cq = mlx5_ib_poll_cq; 5621e126ba97SEli Cohen dev->ib_dev.req_notify_cq = mlx5_ib_arm_cq; 5622e126ba97SEli Cohen dev->ib_dev.get_dma_mr = mlx5_ib_get_dma_mr; 5623e126ba97SEli Cohen dev->ib_dev.reg_user_mr = mlx5_ib_reg_user_mr; 562456e11d62SNoa Osherovich dev->ib_dev.rereg_user_mr = mlx5_ib_rereg_user_mr; 5625e126ba97SEli Cohen dev->ib_dev.dereg_mr = mlx5_ib_dereg_mr; 5626e126ba97SEli Cohen dev->ib_dev.attach_mcast = mlx5_ib_mcg_attach; 5627e126ba97SEli Cohen dev->ib_dev.detach_mcast = mlx5_ib_mcg_detach; 5628e126ba97SEli Cohen dev->ib_dev.process_mad = mlx5_ib_process_mad; 56299bee178bSSagi Grimberg dev->ib_dev.alloc_mr = mlx5_ib_alloc_mr; 56308a187ee5SSagi Grimberg dev->ib_dev.map_mr_sg = mlx5_ib_map_mr_sg; 5631d5436ba0SSagi Grimberg dev->ib_dev.check_mr_status = mlx5_ib_check_mr_status; 5632c7342823SIra Weiny dev->ib_dev.get_dev_fw_str = get_dev_fw_str; 563340b24403SSagi Grimberg dev->ib_dev.get_vector_affinity = mlx5_ib_get_vector_affinity; 56348e959601SNiranjana Vishwanathapura if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads)) 5635693dfd5aSErez Shitrit dev->ib_dev.alloc_rdma_netdev = mlx5_ib_alloc_rdma_netdev; 56368e959601SNiranjana Vishwanathapura 5637eff901d3SEli Cohen if (mlx5_core_is_pf(mdev)) { 5638eff901d3SEli Cohen dev->ib_dev.get_vf_config = mlx5_ib_get_vf_config; 5639eff901d3SEli Cohen dev->ib_dev.set_vf_link_state = mlx5_ib_set_vf_link_state; 5640eff901d3SEli Cohen dev->ib_dev.get_vf_stats = mlx5_ib_get_vf_stats; 5641eff901d3SEli Cohen dev->ib_dev.set_vf_guid = mlx5_ib_set_vf_guid; 5642eff901d3SEli Cohen } 5643e126ba97SEli Cohen 56447c2344c3SMaor Gottlieb dev->ib_dev.disassociate_ucontext = mlx5_ib_disassociate_ucontext; 56457c2344c3SMaor Gottlieb 56466e8484c5SMax Gurtovoy dev->umr_fence = mlx5_get_umr_fence(MLX5_CAP_GEN(mdev, umr_fence)); 56476e8484c5SMax Gurtovoy 5648d2370e0aSMatan Barak if (MLX5_CAP_GEN(mdev, imaicl)) { 5649d2370e0aSMatan Barak dev->ib_dev.alloc_mw = mlx5_ib_alloc_mw; 5650d2370e0aSMatan Barak dev->ib_dev.dealloc_mw = mlx5_ib_dealloc_mw; 5651d2370e0aSMatan Barak dev->ib_dev.uverbs_cmd_mask |= 5652d2370e0aSMatan Barak (1ull << IB_USER_VERBS_CMD_ALLOC_MW) | 5653d2370e0aSMatan Barak (1ull << IB_USER_VERBS_CMD_DEALLOC_MW); 5654d2370e0aSMatan Barak } 5655d2370e0aSMatan Barak 5656938fe83cSSaeed Mahameed if (MLX5_CAP_GEN(mdev, xrc)) { 5657e126ba97SEli Cohen dev->ib_dev.alloc_xrcd = mlx5_ib_alloc_xrcd; 5658e126ba97SEli Cohen dev->ib_dev.dealloc_xrcd = mlx5_ib_dealloc_xrcd; 5659e126ba97SEli Cohen dev->ib_dev.uverbs_cmd_mask |= 5660e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_OPEN_XRCD) | 5661e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_CLOSE_XRCD); 5662e126ba97SEli Cohen } 5663e126ba97SEli Cohen 566424da0016SAriel Levkovich if (MLX5_CAP_DEV_MEM(mdev, memic)) { 566524da0016SAriel Levkovich dev->ib_dev.alloc_dm = mlx5_ib_alloc_dm; 566624da0016SAriel Levkovich dev->ib_dev.dealloc_dm = mlx5_ib_dealloc_dm; 56676c29f57eSAriel Levkovich dev->ib_dev.reg_dm_mr = mlx5_ib_reg_dm_mr; 566824da0016SAriel Levkovich } 566924da0016SAriel Levkovich 5670038d2ef8SMaor Gottlieb dev->ib_dev.create_flow = mlx5_ib_create_flow; 5671038d2ef8SMaor Gottlieb dev->ib_dev.destroy_flow = mlx5_ib_destroy_flow; 567281e30880SYishai Hadas dev->ib_dev.uverbs_ex_cmd_mask |= 567381e30880SYishai Hadas (1ull << IB_USER_VERBS_EX_CMD_CREATE_FLOW) | 567481e30880SYishai Hadas (1ull << IB_USER_VERBS_EX_CMD_DESTROY_FLOW); 5675c6475a0bSAviad Yehezkel dev->ib_dev.create_flow_action_esp = mlx5_ib_create_flow_action_esp; 5676c6475a0bSAviad Yehezkel dev->ib_dev.destroy_flow_action = mlx5_ib_destroy_flow_action; 5677349705c1SMatan Barak dev->ib_dev.modify_flow_action_esp = mlx5_ib_modify_flow_action_esp; 56780ede73bcSMatan Barak dev->ib_dev.driver_id = RDMA_DRIVER_MLX5; 5679b29e2a13SRaed Salem dev->ib_dev.create_counters = mlx5_ib_create_counters; 5680b29e2a13SRaed Salem dev->ib_dev.destroy_counters = mlx5_ib_destroy_counters; 56811a1e03dcSRaed Salem dev->ib_dev.read_counters = mlx5_ib_read_counters; 568281e30880SYishai Hadas 5683e126ba97SEli Cohen err = init_node_data(dev); 5684e126ba97SEli Cohen if (err) 568516c1975fSMark Bloch return err; 5686e126ba97SEli Cohen 5687c8b89924SMark Bloch if ((MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_ETH) && 5688e7996a9aSJason Gunthorpe (MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) || 5689e7996a9aSJason Gunthorpe MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc))) 5690c8b89924SMark Bloch mutex_init(&dev->lb_mutex); 5691c8b89924SMark Bloch 569216c1975fSMark Bloch return 0; 569316c1975fSMark Bloch } 569416c1975fSMark Bloch 56958e6efa3aSMark Bloch static int mlx5_ib_stage_non_default_cb(struct mlx5_ib_dev *dev) 569616c1975fSMark Bloch { 56978e6efa3aSMark Bloch dev->ib_dev.get_port_immutable = mlx5_port_immutable; 56988e6efa3aSMark Bloch dev->ib_dev.query_port = mlx5_ib_query_port; 56998e6efa3aSMark Bloch 57008e6efa3aSMark Bloch return 0; 57018e6efa3aSMark Bloch } 57028e6efa3aSMark Bloch 5703b5ca15adSMark Bloch int mlx5_ib_stage_rep_non_default_cb(struct mlx5_ib_dev *dev) 57048e6efa3aSMark Bloch { 57058e6efa3aSMark Bloch dev->ib_dev.get_port_immutable = mlx5_port_rep_immutable; 57068e6efa3aSMark Bloch dev->ib_dev.query_port = mlx5_ib_rep_query_port; 57078e6efa3aSMark Bloch 57088e6efa3aSMark Bloch return 0; 57098e6efa3aSMark Bloch } 57108e6efa3aSMark Bloch 57118e6efa3aSMark Bloch static int mlx5_ib_stage_common_roce_init(struct mlx5_ib_dev *dev, 57128e6efa3aSMark Bloch u8 port_num) 57138e6efa3aSMark Bloch { 57147fd8aefbSDaniel Jurgens int i; 571516c1975fSMark Bloch 57167fd8aefbSDaniel Jurgens for (i = 0; i < dev->num_ports; i++) { 57177fd8aefbSDaniel Jurgens dev->roce[i].dev = dev; 57187fd8aefbSDaniel Jurgens dev->roce[i].native_port_num = i + 1; 57197fd8aefbSDaniel Jurgens dev->roce[i].last_port_state = IB_PORT_DOWN; 57207fd8aefbSDaniel Jurgens } 57217fd8aefbSDaniel Jurgens 5722c11a226aSMark Bloch dev->ib_dev.get_netdev = mlx5_ib_get_netdev; 5723c11a226aSMark Bloch dev->ib_dev.create_wq = mlx5_ib_create_wq; 5724c11a226aSMark Bloch dev->ib_dev.modify_wq = mlx5_ib_modify_wq; 5725c11a226aSMark Bloch dev->ib_dev.destroy_wq = mlx5_ib_destroy_wq; 5726c11a226aSMark Bloch dev->ib_dev.create_rwq_ind_table = mlx5_ib_create_rwq_ind_table; 5727c11a226aSMark Bloch dev->ib_dev.destroy_rwq_ind_table = mlx5_ib_destroy_rwq_ind_table; 57288e6efa3aSMark Bloch 5729c11a226aSMark Bloch dev->ib_dev.uverbs_ex_cmd_mask |= 5730c11a226aSMark Bloch (1ull << IB_USER_VERBS_EX_CMD_CREATE_WQ) | 5731c11a226aSMark Bloch (1ull << IB_USER_VERBS_EX_CMD_MODIFY_WQ) | 5732c11a226aSMark Bloch (1ull << IB_USER_VERBS_EX_CMD_DESTROY_WQ) | 5733c11a226aSMark Bloch (1ull << IB_USER_VERBS_EX_CMD_CREATE_RWQ_IND_TBL) | 5734c11a226aSMark Bloch (1ull << IB_USER_VERBS_EX_CMD_DESTROY_RWQ_IND_TBL); 57358e6efa3aSMark Bloch 57368e6efa3aSMark Bloch return mlx5_add_netdev_notifier(dev, port_num); 57378e6efa3aSMark Bloch } 57388e6efa3aSMark Bloch 57398e6efa3aSMark Bloch static void mlx5_ib_stage_common_roce_cleanup(struct mlx5_ib_dev *dev) 57408e6efa3aSMark Bloch { 57418e6efa3aSMark Bloch u8 port_num = mlx5_core_native_port_num(dev->mdev) - 1; 57428e6efa3aSMark Bloch 57438e6efa3aSMark Bloch mlx5_remove_netdev_notifier(dev, port_num); 57448e6efa3aSMark Bloch } 57458e6efa3aSMark Bloch 57468e6efa3aSMark Bloch int mlx5_ib_stage_rep_roce_init(struct mlx5_ib_dev *dev) 57478e6efa3aSMark Bloch { 57488e6efa3aSMark Bloch struct mlx5_core_dev *mdev = dev->mdev; 57498e6efa3aSMark Bloch enum rdma_link_layer ll; 57508e6efa3aSMark Bloch int port_type_cap; 57518e6efa3aSMark Bloch int err = 0; 57528e6efa3aSMark Bloch u8 port_num; 57538e6efa3aSMark Bloch 57548e6efa3aSMark Bloch port_num = mlx5_core_native_port_num(dev->mdev) - 1; 57558e6efa3aSMark Bloch port_type_cap = MLX5_CAP_GEN(mdev, port_type); 57568e6efa3aSMark Bloch ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap); 57578e6efa3aSMark Bloch 57588e6efa3aSMark Bloch if (ll == IB_LINK_LAYER_ETHERNET) 57598e6efa3aSMark Bloch err = mlx5_ib_stage_common_roce_init(dev, port_num); 57608e6efa3aSMark Bloch 576116c1975fSMark Bloch return err; 5762fc24fc5eSAchiad Shochat } 5763fc24fc5eSAchiad Shochat 57648e6efa3aSMark Bloch void mlx5_ib_stage_rep_roce_cleanup(struct mlx5_ib_dev *dev) 57658e6efa3aSMark Bloch { 57668e6efa3aSMark Bloch mlx5_ib_stage_common_roce_cleanup(dev); 57678e6efa3aSMark Bloch } 57688e6efa3aSMark Bloch 57698e6efa3aSMark Bloch static int mlx5_ib_stage_roce_init(struct mlx5_ib_dev *dev) 57708e6efa3aSMark Bloch { 57718e6efa3aSMark Bloch struct mlx5_core_dev *mdev = dev->mdev; 57728e6efa3aSMark Bloch enum rdma_link_layer ll; 57738e6efa3aSMark Bloch int port_type_cap; 57748e6efa3aSMark Bloch u8 port_num; 57758e6efa3aSMark Bloch int err; 57768e6efa3aSMark Bloch 57778e6efa3aSMark Bloch port_num = mlx5_core_native_port_num(dev->mdev) - 1; 57788e6efa3aSMark Bloch port_type_cap = MLX5_CAP_GEN(mdev, port_type); 57798e6efa3aSMark Bloch ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap); 57808e6efa3aSMark Bloch 57818e6efa3aSMark Bloch if (ll == IB_LINK_LAYER_ETHERNET) { 57828e6efa3aSMark Bloch err = mlx5_ib_stage_common_roce_init(dev, port_num); 57838e6efa3aSMark Bloch if (err) 57848e6efa3aSMark Bloch return err; 57858e6efa3aSMark Bloch 57868e6efa3aSMark Bloch err = mlx5_enable_eth(dev, port_num); 57878e6efa3aSMark Bloch if (err) 57888e6efa3aSMark Bloch goto cleanup; 57898e6efa3aSMark Bloch } 57908e6efa3aSMark Bloch 579116c1975fSMark Bloch return 0; 57928e6efa3aSMark Bloch cleanup: 57938e6efa3aSMark Bloch mlx5_ib_stage_common_roce_cleanup(dev); 57948e6efa3aSMark Bloch 57958e6efa3aSMark Bloch return err; 579645bded2cSKamal Heib } 57976aec21f6SHaggai Eran 579816c1975fSMark Bloch static void mlx5_ib_stage_roce_cleanup(struct mlx5_ib_dev *dev) 579916c1975fSMark Bloch { 580016c1975fSMark Bloch struct mlx5_core_dev *mdev = dev->mdev; 580116c1975fSMark Bloch enum rdma_link_layer ll; 580216c1975fSMark Bloch int port_type_cap; 58034a2da0b8SParav Pandit 580416c1975fSMark Bloch port_type_cap = MLX5_CAP_GEN(mdev, port_type); 580516c1975fSMark Bloch ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap); 580616c1975fSMark Bloch 580716c1975fSMark Bloch if (ll == IB_LINK_LAYER_ETHERNET) { 580816c1975fSMark Bloch mlx5_disable_eth(dev); 58098e6efa3aSMark Bloch mlx5_ib_stage_common_roce_cleanup(dev); 581016c1975fSMark Bloch } 581116c1975fSMark Bloch } 581216c1975fSMark Bloch 5813b5ca15adSMark Bloch int mlx5_ib_stage_dev_res_init(struct mlx5_ib_dev *dev) 581416c1975fSMark Bloch { 581516c1975fSMark Bloch return create_dev_resources(&dev->devr); 581616c1975fSMark Bloch } 581716c1975fSMark Bloch 5818b5ca15adSMark Bloch void mlx5_ib_stage_dev_res_cleanup(struct mlx5_ib_dev *dev) 581916c1975fSMark Bloch { 582016c1975fSMark Bloch destroy_dev_resources(&dev->devr); 582116c1975fSMark Bloch } 582216c1975fSMark Bloch 582316c1975fSMark Bloch static int mlx5_ib_stage_odp_init(struct mlx5_ib_dev *dev) 582416c1975fSMark Bloch { 582507321b3cSMark Bloch mlx5_ib_internal_fill_odp_caps(dev); 582607321b3cSMark Bloch 582716c1975fSMark Bloch return mlx5_ib_odp_init_one(dev); 582816c1975fSMark Bloch } 582916c1975fSMark Bloch 5830b5ca15adSMark Bloch int mlx5_ib_stage_counters_init(struct mlx5_ib_dev *dev) 583116c1975fSMark Bloch { 58325e1e7612SMark Bloch if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt)) { 58335e1e7612SMark Bloch dev->ib_dev.get_hw_stats = mlx5_ib_get_hw_stats; 58345e1e7612SMark Bloch dev->ib_dev.alloc_hw_stats = mlx5_ib_alloc_hw_stats; 58355e1e7612SMark Bloch 583616c1975fSMark Bloch return mlx5_ib_alloc_counters(dev); 58375e1e7612SMark Bloch } 583816c1975fSMark Bloch 583916c1975fSMark Bloch return 0; 584016c1975fSMark Bloch } 584116c1975fSMark Bloch 5842b5ca15adSMark Bloch void mlx5_ib_stage_counters_cleanup(struct mlx5_ib_dev *dev) 584316c1975fSMark Bloch { 584416c1975fSMark Bloch if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt)) 584516c1975fSMark Bloch mlx5_ib_dealloc_counters(dev); 584616c1975fSMark Bloch } 584716c1975fSMark Bloch 584816c1975fSMark Bloch static int mlx5_ib_stage_cong_debugfs_init(struct mlx5_ib_dev *dev) 584916c1975fSMark Bloch { 5850a9e546e7SParav Pandit return mlx5_ib_init_cong_debugfs(dev, 5851a9e546e7SParav Pandit mlx5_core_native_port_num(dev->mdev) - 1); 585216c1975fSMark Bloch } 585316c1975fSMark Bloch 585416c1975fSMark Bloch static void mlx5_ib_stage_cong_debugfs_cleanup(struct mlx5_ib_dev *dev) 585516c1975fSMark Bloch { 5856a9e546e7SParav Pandit mlx5_ib_cleanup_cong_debugfs(dev, 5857a9e546e7SParav Pandit mlx5_core_native_port_num(dev->mdev) - 1); 585816c1975fSMark Bloch } 585916c1975fSMark Bloch 586016c1975fSMark Bloch static int mlx5_ib_stage_uar_init(struct mlx5_ib_dev *dev) 586116c1975fSMark Bloch { 58625fe9dec0SEli Cohen dev->mdev->priv.uar = mlx5_get_uars_page(dev->mdev); 5863444261caSLeon Romanovsky return PTR_ERR_OR_ZERO(dev->mdev->priv.uar); 586416c1975fSMark Bloch } 586516c1975fSMark Bloch 586616c1975fSMark Bloch static void mlx5_ib_stage_uar_cleanup(struct mlx5_ib_dev *dev) 586716c1975fSMark Bloch { 586816c1975fSMark Bloch mlx5_put_uars_page(dev->mdev, dev->mdev->priv.uar); 586916c1975fSMark Bloch } 587016c1975fSMark Bloch 5871b5ca15adSMark Bloch int mlx5_ib_stage_bfrag_init(struct mlx5_ib_dev *dev) 587216c1975fSMark Bloch { 587316c1975fSMark Bloch int err; 58745fe9dec0SEli Cohen 58755fe9dec0SEli Cohen err = mlx5_alloc_bfreg(dev->mdev, &dev->bfreg, false, false); 58765fe9dec0SEli Cohen if (err) 587716c1975fSMark Bloch return err; 58785fe9dec0SEli Cohen 58795fe9dec0SEli Cohen err = mlx5_alloc_bfreg(dev->mdev, &dev->fp_bfreg, false, true); 58805fe9dec0SEli Cohen if (err) 588116c1975fSMark Bloch mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg); 58825fe9dec0SEli Cohen 588316c1975fSMark Bloch return err; 588416c1975fSMark Bloch } 58850837e86aSMark Bloch 5886b5ca15adSMark Bloch void mlx5_ib_stage_bfrag_cleanup(struct mlx5_ib_dev *dev) 588716c1975fSMark Bloch { 588816c1975fSMark Bloch mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg); 588916c1975fSMark Bloch mlx5_free_bfreg(dev->mdev, &dev->bfreg); 589016c1975fSMark Bloch } 5891e126ba97SEli Cohen 58928c84660bSMatan Barak static int mlx5_ib_stage_populate_specs(struct mlx5_ib_dev *dev) 58938c84660bSMatan Barak { 58948c84660bSMatan Barak return populate_specs_root(dev); 58958c84660bSMatan Barak } 58968c84660bSMatan Barak 5897b5ca15adSMark Bloch int mlx5_ib_stage_ib_reg_init(struct mlx5_ib_dev *dev) 589816c1975fSMark Bloch { 589916c1975fSMark Bloch return ib_register_device(&dev->ib_dev, NULL); 590016c1975fSMark Bloch } 590116c1975fSMark Bloch 59028c84660bSMatan Barak static void mlx5_ib_stage_depopulate_specs(struct mlx5_ib_dev *dev) 59038c84660bSMatan Barak { 59048c84660bSMatan Barak depopulate_specs_root(dev); 59058c84660bSMatan Barak } 59068c84660bSMatan Barak 590703fe2debSDavid S. Miller void mlx5_ib_stage_pre_ib_reg_umr_cleanup(struct mlx5_ib_dev *dev) 590842cea83fSMark Bloch { 590942cea83fSMark Bloch destroy_umrc_res(dev); 591042cea83fSMark Bloch } 591142cea83fSMark Bloch 5912b5ca15adSMark Bloch void mlx5_ib_stage_ib_reg_cleanup(struct mlx5_ib_dev *dev) 591316c1975fSMark Bloch { 591416c1975fSMark Bloch ib_unregister_device(&dev->ib_dev); 591516c1975fSMark Bloch } 591616c1975fSMark Bloch 591703fe2debSDavid S. Miller int mlx5_ib_stage_post_ib_reg_umr_init(struct mlx5_ib_dev *dev) 591816c1975fSMark Bloch { 591916c1975fSMark Bloch return create_umr_res(dev); 592016c1975fSMark Bloch } 592116c1975fSMark Bloch 592216c1975fSMark Bloch static int mlx5_ib_stage_delay_drop_init(struct mlx5_ib_dev *dev) 592316c1975fSMark Bloch { 592403404e8aSMaor Gottlieb init_delay_drop(dev); 592503404e8aSMaor Gottlieb 592616c1975fSMark Bloch return 0; 592716c1975fSMark Bloch } 592816c1975fSMark Bloch 592916c1975fSMark Bloch static void mlx5_ib_stage_delay_drop_cleanup(struct mlx5_ib_dev *dev) 593016c1975fSMark Bloch { 593116c1975fSMark Bloch cancel_delay_drop(dev); 593216c1975fSMark Bloch } 593316c1975fSMark Bloch 5934b5ca15adSMark Bloch int mlx5_ib_stage_class_attr_init(struct mlx5_ib_dev *dev) 593516c1975fSMark Bloch { 593616c1975fSMark Bloch int err; 593716c1975fSMark Bloch int i; 593816c1975fSMark Bloch 5939e126ba97SEli Cohen for (i = 0; i < ARRAY_SIZE(mlx5_class_attributes); i++) { 5940281d1a92SWei Yongjun err = device_create_file(&dev->ib_dev.dev, 5941281d1a92SWei Yongjun mlx5_class_attributes[i]); 5942281d1a92SWei Yongjun if (err) 594316c1975fSMark Bloch return err; 5944e126ba97SEli Cohen } 5945e126ba97SEli Cohen 594616c1975fSMark Bloch return 0; 594716c1975fSMark Bloch } 594816c1975fSMark Bloch 5949fc385b7aSMark Bloch static int mlx5_ib_stage_rep_reg_init(struct mlx5_ib_dev *dev) 5950fc385b7aSMark Bloch { 5951fc385b7aSMark Bloch mlx5_ib_register_vport_reps(dev); 5952fc385b7aSMark Bloch 5953fc385b7aSMark Bloch return 0; 5954fc385b7aSMark Bloch } 5955fc385b7aSMark Bloch 5956fc385b7aSMark Bloch static void mlx5_ib_stage_rep_reg_cleanup(struct mlx5_ib_dev *dev) 5957fc385b7aSMark Bloch { 5958fc385b7aSMark Bloch mlx5_ib_unregister_vport_reps(dev); 5959fc385b7aSMark Bloch } 5960fc385b7aSMark Bloch 5961b5ca15adSMark Bloch void __mlx5_ib_remove(struct mlx5_ib_dev *dev, 596216c1975fSMark Bloch const struct mlx5_ib_profile *profile, 596316c1975fSMark Bloch int stage) 596416c1975fSMark Bloch { 596516c1975fSMark Bloch /* Number of stages to cleanup */ 596616c1975fSMark Bloch while (stage) { 596716c1975fSMark Bloch stage--; 596816c1975fSMark Bloch if (profile->stage[stage].cleanup) 596916c1975fSMark Bloch profile->stage[stage].cleanup(dev); 597016c1975fSMark Bloch } 597116c1975fSMark Bloch 597216c1975fSMark Bloch ib_dealloc_device((struct ib_device *)dev); 597316c1975fSMark Bloch } 597416c1975fSMark Bloch 597532f69e4bSDaniel Jurgens static void *mlx5_ib_add_slave_port(struct mlx5_core_dev *mdev, u8 port_num); 597632f69e4bSDaniel Jurgens 5977b5ca15adSMark Bloch void *__mlx5_ib_add(struct mlx5_ib_dev *dev, 597816c1975fSMark Bloch const struct mlx5_ib_profile *profile) 597916c1975fSMark Bloch { 598016c1975fSMark Bloch int err; 598116c1975fSMark Bloch int i; 598216c1975fSMark Bloch 598316c1975fSMark Bloch printk_once(KERN_INFO "%s", mlx5_version); 598416c1975fSMark Bloch 598516c1975fSMark Bloch for (i = 0; i < MLX5_IB_STAGE_MAX; i++) { 598616c1975fSMark Bloch if (profile->stage[i].init) { 598716c1975fSMark Bloch err = profile->stage[i].init(dev); 598816c1975fSMark Bloch if (err) 598916c1975fSMark Bloch goto err_out; 599016c1975fSMark Bloch } 599116c1975fSMark Bloch } 599216c1975fSMark Bloch 599316c1975fSMark Bloch dev->profile = profile; 5994e126ba97SEli Cohen dev->ib_active = true; 5995e126ba97SEli Cohen 59969603b61dSJack Morgenstein return dev; 5997e126ba97SEli Cohen 599816c1975fSMark Bloch err_out: 599916c1975fSMark Bloch __mlx5_ib_remove(dev, profile, i); 6000e126ba97SEli Cohen 60019603b61dSJack Morgenstein return NULL; 6002e126ba97SEli Cohen } 6003e126ba97SEli Cohen 600416c1975fSMark Bloch static const struct mlx5_ib_profile pf_profile = { 600516c1975fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_INIT, 600616c1975fSMark Bloch mlx5_ib_stage_init_init, 600716c1975fSMark Bloch mlx5_ib_stage_init_cleanup), 60089a4ca38dSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_FLOW_DB, 60099a4ca38dSMark Bloch mlx5_ib_stage_flow_db_init, 60109a4ca38dSMark Bloch mlx5_ib_stage_flow_db_cleanup), 601116c1975fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_CAPS, 601216c1975fSMark Bloch mlx5_ib_stage_caps_init, 601316c1975fSMark Bloch NULL), 60148e6efa3aSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB, 60158e6efa3aSMark Bloch mlx5_ib_stage_non_default_cb, 60168e6efa3aSMark Bloch NULL), 601716c1975fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_ROCE, 601816c1975fSMark Bloch mlx5_ib_stage_roce_init, 601916c1975fSMark Bloch mlx5_ib_stage_roce_cleanup), 602016c1975fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES, 602116c1975fSMark Bloch mlx5_ib_stage_dev_res_init, 602216c1975fSMark Bloch mlx5_ib_stage_dev_res_cleanup), 602316c1975fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_ODP, 602416c1975fSMark Bloch mlx5_ib_stage_odp_init, 60253cc297dbSMark Bloch NULL), 602616c1975fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_COUNTERS, 602716c1975fSMark Bloch mlx5_ib_stage_counters_init, 602816c1975fSMark Bloch mlx5_ib_stage_counters_cleanup), 602916c1975fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_CONG_DEBUGFS, 603016c1975fSMark Bloch mlx5_ib_stage_cong_debugfs_init, 603116c1975fSMark Bloch mlx5_ib_stage_cong_debugfs_cleanup), 603216c1975fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_UAR, 603316c1975fSMark Bloch mlx5_ib_stage_uar_init, 603416c1975fSMark Bloch mlx5_ib_stage_uar_cleanup), 603516c1975fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_BFREG, 603616c1975fSMark Bloch mlx5_ib_stage_bfrag_init, 603716c1975fSMark Bloch mlx5_ib_stage_bfrag_cleanup), 603842cea83fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR, 603942cea83fSMark Bloch NULL, 604042cea83fSMark Bloch mlx5_ib_stage_pre_ib_reg_umr_cleanup), 60418c84660bSMatan Barak STAGE_CREATE(MLX5_IB_STAGE_SPECS, 60428c84660bSMatan Barak mlx5_ib_stage_populate_specs, 60438c84660bSMatan Barak mlx5_ib_stage_depopulate_specs), 604416c1975fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_IB_REG, 604516c1975fSMark Bloch mlx5_ib_stage_ib_reg_init, 604616c1975fSMark Bloch mlx5_ib_stage_ib_reg_cleanup), 604742cea83fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR, 604842cea83fSMark Bloch mlx5_ib_stage_post_ib_reg_umr_init, 604942cea83fSMark Bloch NULL), 605016c1975fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_DELAY_DROP, 605116c1975fSMark Bloch mlx5_ib_stage_delay_drop_init, 605216c1975fSMark Bloch mlx5_ib_stage_delay_drop_cleanup), 605316c1975fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_CLASS_ATTR, 605416c1975fSMark Bloch mlx5_ib_stage_class_attr_init, 605516c1975fSMark Bloch NULL), 605616c1975fSMark Bloch }; 605716c1975fSMark Bloch 6058b5ca15adSMark Bloch static const struct mlx5_ib_profile nic_rep_profile = { 6059b5ca15adSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_INIT, 6060b5ca15adSMark Bloch mlx5_ib_stage_init_init, 6061b5ca15adSMark Bloch mlx5_ib_stage_init_cleanup), 6062b5ca15adSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_FLOW_DB, 6063b5ca15adSMark Bloch mlx5_ib_stage_flow_db_init, 6064b5ca15adSMark Bloch mlx5_ib_stage_flow_db_cleanup), 6065b5ca15adSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_CAPS, 6066b5ca15adSMark Bloch mlx5_ib_stage_caps_init, 6067b5ca15adSMark Bloch NULL), 6068b5ca15adSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB, 6069b5ca15adSMark Bloch mlx5_ib_stage_rep_non_default_cb, 6070b5ca15adSMark Bloch NULL), 6071b5ca15adSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_ROCE, 6072b5ca15adSMark Bloch mlx5_ib_stage_rep_roce_init, 6073b5ca15adSMark Bloch mlx5_ib_stage_rep_roce_cleanup), 6074b5ca15adSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES, 6075b5ca15adSMark Bloch mlx5_ib_stage_dev_res_init, 6076b5ca15adSMark Bloch mlx5_ib_stage_dev_res_cleanup), 6077b5ca15adSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_COUNTERS, 6078b5ca15adSMark Bloch mlx5_ib_stage_counters_init, 6079b5ca15adSMark Bloch mlx5_ib_stage_counters_cleanup), 6080b5ca15adSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_UAR, 6081b5ca15adSMark Bloch mlx5_ib_stage_uar_init, 6082b5ca15adSMark Bloch mlx5_ib_stage_uar_cleanup), 6083b5ca15adSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_BFREG, 6084b5ca15adSMark Bloch mlx5_ib_stage_bfrag_init, 6085b5ca15adSMark Bloch mlx5_ib_stage_bfrag_cleanup), 608603fe2debSDavid S. Miller STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR, 608703fe2debSDavid S. Miller NULL, 608803fe2debSDavid S. Miller mlx5_ib_stage_pre_ib_reg_umr_cleanup), 60898c84660bSMatan Barak STAGE_CREATE(MLX5_IB_STAGE_SPECS, 60908c84660bSMatan Barak mlx5_ib_stage_populate_specs, 60918c84660bSMatan Barak mlx5_ib_stage_depopulate_specs), 6092b5ca15adSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_IB_REG, 6093b5ca15adSMark Bloch mlx5_ib_stage_ib_reg_init, 6094b5ca15adSMark Bloch mlx5_ib_stage_ib_reg_cleanup), 609503fe2debSDavid S. Miller STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR, 609603fe2debSDavid S. Miller mlx5_ib_stage_post_ib_reg_umr_init, 609703fe2debSDavid S. Miller NULL), 6098b5ca15adSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_CLASS_ATTR, 6099b5ca15adSMark Bloch mlx5_ib_stage_class_attr_init, 6100b5ca15adSMark Bloch NULL), 6101b5ca15adSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_REP_REG, 6102b5ca15adSMark Bloch mlx5_ib_stage_rep_reg_init, 6103b5ca15adSMark Bloch mlx5_ib_stage_rep_reg_cleanup), 6104b5ca15adSMark Bloch }; 6105b5ca15adSMark Bloch 610632f69e4bSDaniel Jurgens static void *mlx5_ib_add_slave_port(struct mlx5_core_dev *mdev, u8 port_num) 610732f69e4bSDaniel Jurgens { 610832f69e4bSDaniel Jurgens struct mlx5_ib_multiport_info *mpi; 610932f69e4bSDaniel Jurgens struct mlx5_ib_dev *dev; 611032f69e4bSDaniel Jurgens bool bound = false; 611132f69e4bSDaniel Jurgens int err; 611232f69e4bSDaniel Jurgens 611332f69e4bSDaniel Jurgens mpi = kzalloc(sizeof(*mpi), GFP_KERNEL); 611432f69e4bSDaniel Jurgens if (!mpi) 611532f69e4bSDaniel Jurgens return NULL; 611632f69e4bSDaniel Jurgens 611732f69e4bSDaniel Jurgens mpi->mdev = mdev; 611832f69e4bSDaniel Jurgens 611932f69e4bSDaniel Jurgens err = mlx5_query_nic_vport_system_image_guid(mdev, 612032f69e4bSDaniel Jurgens &mpi->sys_image_guid); 612132f69e4bSDaniel Jurgens if (err) { 612232f69e4bSDaniel Jurgens kfree(mpi); 612332f69e4bSDaniel Jurgens return NULL; 612432f69e4bSDaniel Jurgens } 612532f69e4bSDaniel Jurgens 612632f69e4bSDaniel Jurgens mutex_lock(&mlx5_ib_multiport_mutex); 612732f69e4bSDaniel Jurgens list_for_each_entry(dev, &mlx5_ib_dev_list, ib_dev_list) { 612832f69e4bSDaniel Jurgens if (dev->sys_image_guid == mpi->sys_image_guid) 612932f69e4bSDaniel Jurgens bound = mlx5_ib_bind_slave_port(dev, mpi); 613032f69e4bSDaniel Jurgens 613132f69e4bSDaniel Jurgens if (bound) { 613232f69e4bSDaniel Jurgens rdma_roce_rescan_device(&dev->ib_dev); 613332f69e4bSDaniel Jurgens break; 613432f69e4bSDaniel Jurgens } 613532f69e4bSDaniel Jurgens } 613632f69e4bSDaniel Jurgens 613732f69e4bSDaniel Jurgens if (!bound) { 613832f69e4bSDaniel Jurgens list_add_tail(&mpi->list, &mlx5_ib_unaffiliated_port_list); 613932f69e4bSDaniel Jurgens dev_dbg(&mdev->pdev->dev, "no suitable IB device found to bind to, added to unaffiliated list.\n"); 614032f69e4bSDaniel Jurgens } else { 614132f69e4bSDaniel Jurgens mlx5_ib_dbg(dev, "bound port %u\n", port_num + 1); 614232f69e4bSDaniel Jurgens } 614332f69e4bSDaniel Jurgens mutex_unlock(&mlx5_ib_multiport_mutex); 614432f69e4bSDaniel Jurgens 614532f69e4bSDaniel Jurgens return mpi; 614632f69e4bSDaniel Jurgens } 614732f69e4bSDaniel Jurgens 614816c1975fSMark Bloch static void *mlx5_ib_add(struct mlx5_core_dev *mdev) 614916c1975fSMark Bloch { 615032f69e4bSDaniel Jurgens enum rdma_link_layer ll; 6151b5ca15adSMark Bloch struct mlx5_ib_dev *dev; 615232f69e4bSDaniel Jurgens int port_type_cap; 615332f69e4bSDaniel Jurgens 6154b5ca15adSMark Bloch printk_once(KERN_INFO "%s", mlx5_version); 6155b5ca15adSMark Bloch 615632f69e4bSDaniel Jurgens port_type_cap = MLX5_CAP_GEN(mdev, port_type); 615732f69e4bSDaniel Jurgens ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap); 615832f69e4bSDaniel Jurgens 615932f69e4bSDaniel Jurgens if (mlx5_core_is_mp_slave(mdev) && ll == IB_LINK_LAYER_ETHERNET) { 616032f69e4bSDaniel Jurgens u8 port_num = mlx5_core_native_port_num(mdev) - 1; 616132f69e4bSDaniel Jurgens 616232f69e4bSDaniel Jurgens return mlx5_ib_add_slave_port(mdev, port_num); 616332f69e4bSDaniel Jurgens } 616432f69e4bSDaniel Jurgens 6165b5ca15adSMark Bloch dev = (struct mlx5_ib_dev *)ib_alloc_device(sizeof(*dev)); 6166b5ca15adSMark Bloch if (!dev) 6167b5ca15adSMark Bloch return NULL; 6168b5ca15adSMark Bloch 6169b5ca15adSMark Bloch dev->mdev = mdev; 6170b5ca15adSMark Bloch dev->num_ports = max(MLX5_CAP_GEN(mdev, num_ports), 6171b5ca15adSMark Bloch MLX5_CAP_GEN(mdev, num_vhca_ports)); 6172b5ca15adSMark Bloch 6173b5ca15adSMark Bloch if (MLX5_VPORT_MANAGER(mdev) && 6174b5ca15adSMark Bloch mlx5_ib_eswitch_mode(mdev->priv.eswitch) == SRIOV_OFFLOADS) { 6175b5ca15adSMark Bloch dev->rep = mlx5_ib_vport_rep(mdev->priv.eswitch, 0); 6176b5ca15adSMark Bloch 6177b5ca15adSMark Bloch return __mlx5_ib_add(dev, &nic_rep_profile); 6178b5ca15adSMark Bloch } 6179b5ca15adSMark Bloch 6180b5ca15adSMark Bloch return __mlx5_ib_add(dev, &pf_profile); 618116c1975fSMark Bloch } 618216c1975fSMark Bloch 61839603b61dSJack Morgenstein static void mlx5_ib_remove(struct mlx5_core_dev *mdev, void *context) 6184e126ba97SEli Cohen { 618532f69e4bSDaniel Jurgens struct mlx5_ib_multiport_info *mpi; 618632f69e4bSDaniel Jurgens struct mlx5_ib_dev *dev; 61876aec21f6SHaggai Eran 618832f69e4bSDaniel Jurgens if (mlx5_core_is_mp_slave(mdev)) { 618932f69e4bSDaniel Jurgens mpi = context; 619032f69e4bSDaniel Jurgens mutex_lock(&mlx5_ib_multiport_mutex); 619132f69e4bSDaniel Jurgens if (mpi->ibdev) 619232f69e4bSDaniel Jurgens mlx5_ib_unbind_slave_port(mpi->ibdev, mpi); 619332f69e4bSDaniel Jurgens list_del(&mpi->list); 619432f69e4bSDaniel Jurgens mutex_unlock(&mlx5_ib_multiport_mutex); 619532f69e4bSDaniel Jurgens return; 619632f69e4bSDaniel Jurgens } 619732f69e4bSDaniel Jurgens 619832f69e4bSDaniel Jurgens dev = context; 619916c1975fSMark Bloch __mlx5_ib_remove(dev, dev->profile, MLX5_IB_STAGE_MAX); 6200e126ba97SEli Cohen } 6201e126ba97SEli Cohen 62029603b61dSJack Morgenstein static struct mlx5_interface mlx5_ib_interface = { 62039603b61dSJack Morgenstein .add = mlx5_ib_add, 62049603b61dSJack Morgenstein .remove = mlx5_ib_remove, 62059603b61dSJack Morgenstein .event = mlx5_ib_event, 6206d9aaed83SArtemy Kovalyov #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING 6207d9aaed83SArtemy Kovalyov .pfault = mlx5_ib_pfault, 6208d9aaed83SArtemy Kovalyov #endif 620964613d94SSaeed Mahameed .protocol = MLX5_INTERFACE_PROTOCOL_IB, 6210e126ba97SEli Cohen }; 6211e126ba97SEli Cohen 6212c44ef998SIlya Lesokhin unsigned long mlx5_ib_get_xlt_emergency_page(void) 6213c44ef998SIlya Lesokhin { 6214c44ef998SIlya Lesokhin mutex_lock(&xlt_emergency_page_mutex); 6215c44ef998SIlya Lesokhin return xlt_emergency_page; 6216c44ef998SIlya Lesokhin } 6217c44ef998SIlya Lesokhin 6218c44ef998SIlya Lesokhin void mlx5_ib_put_xlt_emergency_page(void) 6219c44ef998SIlya Lesokhin { 6220c44ef998SIlya Lesokhin mutex_unlock(&xlt_emergency_page_mutex); 6221c44ef998SIlya Lesokhin } 6222c44ef998SIlya Lesokhin 6223e126ba97SEli Cohen static int __init mlx5_ib_init(void) 6224e126ba97SEli Cohen { 62256aec21f6SHaggai Eran int err; 62266aec21f6SHaggai Eran 6227c44ef998SIlya Lesokhin xlt_emergency_page = __get_free_page(GFP_KERNEL); 6228c44ef998SIlya Lesokhin if (!xlt_emergency_page) 6229d69a24e0SDaniel Jurgens return -ENOMEM; 6230d69a24e0SDaniel Jurgens 6231c44ef998SIlya Lesokhin mutex_init(&xlt_emergency_page_mutex); 6232c44ef998SIlya Lesokhin 6233c44ef998SIlya Lesokhin mlx5_ib_event_wq = alloc_ordered_workqueue("mlx5_ib_event_wq", 0); 6234c44ef998SIlya Lesokhin if (!mlx5_ib_event_wq) { 6235c44ef998SIlya Lesokhin free_page(xlt_emergency_page); 6236c44ef998SIlya Lesokhin return -ENOMEM; 6237c44ef998SIlya Lesokhin } 6238c44ef998SIlya Lesokhin 623981713d37SArtemy Kovalyov mlx5_ib_odp_init(); 62409603b61dSJack Morgenstein 62416aec21f6SHaggai Eran err = mlx5_register_interface(&mlx5_ib_interface); 62426aec21f6SHaggai Eran 62436aec21f6SHaggai Eran return err; 6244e126ba97SEli Cohen } 6245e126ba97SEli Cohen 6246e126ba97SEli Cohen static void __exit mlx5_ib_cleanup(void) 6247e126ba97SEli Cohen { 62489603b61dSJack Morgenstein mlx5_unregister_interface(&mlx5_ib_interface); 6249d69a24e0SDaniel Jurgens destroy_workqueue(mlx5_ib_event_wq); 6250c44ef998SIlya Lesokhin mutex_destroy(&xlt_emergency_page_mutex); 6251c44ef998SIlya Lesokhin free_page(xlt_emergency_page); 6252e126ba97SEli Cohen } 6253e126ba97SEli Cohen 6254e126ba97SEli Cohen module_init(mlx5_ib_init); 6255e126ba97SEli Cohen module_exit(mlx5_ib_cleanup); 6256