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) 43eb243d1dSIngo Molnar #include <asm/memtype.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> 55cecae747SMaor Gottlieb #include <linux/mlx5/eswitch.h> 567c2344c3SMaor Gottlieb #include <linux/list.h> 57e126ba97SEli Cohen #include <rdma/ib_smi.h> 58e126ba97SEli Cohen #include <rdma/ib_umem.h> 59038d2ef8SMaor Gottlieb #include <linux/in.h> 60038d2ef8SMaor Gottlieb #include <linux/etherdevice.h> 61e126ba97SEli Cohen #include "mlx5_ib.h" 62fc385b7aSMark Bloch #include "ib_rep.h" 63e1f24a79SParav Pandit #include "cmd.h" 64f3da6577SLeon Romanovsky #include "srq.h" 653346c487SBoris Pismenny #include <linux/mlx5/fs_helpers.h> 66c6475a0bSAviad Yehezkel #include <linux/mlx5/accel.h> 678c84660bSMatan Barak #include <rdma/uverbs_std_types.h> 68c6475a0bSAviad Yehezkel #include <rdma/mlx5_user_ioctl_verbs.h> 69c6475a0bSAviad Yehezkel #include <rdma/mlx5_user_ioctl_cmds.h> 704061ff7aSErez Alfasi #include <rdma/ib_umem_odp.h> 718c84660bSMatan Barak 728c84660bSMatan Barak #define UVERBS_MODULE_NAME mlx5_ib 738c84660bSMatan Barak #include <rdma/uverbs_named_ioctl.h> 74e126ba97SEli Cohen 75e126ba97SEli Cohen #define DRIVER_NAME "mlx5_ib" 76b359911dSTariq Toukan #define DRIVER_VERSION "5.0-0" 77e126ba97SEli Cohen 78e126ba97SEli Cohen MODULE_AUTHOR("Eli Cohen <eli@mellanox.com>"); 79e126ba97SEli Cohen MODULE_DESCRIPTION("Mellanox Connect-IB HCA IB driver"); 80e126ba97SEli Cohen MODULE_LICENSE("Dual BSD/GPL"); 81e126ba97SEli Cohen 82e126ba97SEli Cohen static char mlx5_version[] = 83e126ba97SEli Cohen DRIVER_NAME ": Mellanox Connect-IB Infiniband driver v" 84b359911dSTariq Toukan DRIVER_VERSION "\n"; 85e126ba97SEli Cohen 86d69a24e0SDaniel Jurgens struct mlx5_ib_event_work { 87d69a24e0SDaniel Jurgens struct work_struct work; 88df097a27SSaeed Mahameed union { 89df097a27SSaeed Mahameed struct mlx5_ib_dev *dev; 90df097a27SSaeed Mahameed struct mlx5_ib_multiport_info *mpi; 91df097a27SSaeed Mahameed }; 92df097a27SSaeed Mahameed bool is_slave; 93134e9349SSaeed Mahameed unsigned int event; 94df097a27SSaeed Mahameed void *param; 95d69a24e0SDaniel Jurgens }; 96d69a24e0SDaniel Jurgens 97da7525d2SEran Ben Elisha enum { 98da7525d2SEran Ben Elisha MLX5_ATOMIC_SIZE_QP_8BYTES = 1 << 3, 99da7525d2SEran Ben Elisha }; 1001b5daf11SMajd Dibbiny 101d69a24e0SDaniel Jurgens static struct workqueue_struct *mlx5_ib_event_wq; 10232f69e4bSDaniel Jurgens static LIST_HEAD(mlx5_ib_unaffiliated_port_list); 10332f69e4bSDaniel Jurgens static LIST_HEAD(mlx5_ib_dev_list); 10432f69e4bSDaniel Jurgens /* 10532f69e4bSDaniel Jurgens * This mutex should be held when accessing either of the above lists 10632f69e4bSDaniel Jurgens */ 10732f69e4bSDaniel Jurgens static DEFINE_MUTEX(mlx5_ib_multiport_mutex); 10832f69e4bSDaniel Jurgens 109c44ef998SIlya Lesokhin /* We can't use an array for xlt_emergency_page because dma_map_single 110c44ef998SIlya Lesokhin * doesn't work on kernel modules memory 111c44ef998SIlya Lesokhin */ 112c44ef998SIlya Lesokhin static unsigned long xlt_emergency_page; 113c44ef998SIlya Lesokhin static struct mutex xlt_emergency_page_mutex; 114c44ef998SIlya Lesokhin 11532f69e4bSDaniel Jurgens struct mlx5_ib_dev *mlx5_ib_get_ibdev_from_mpi(struct mlx5_ib_multiport_info *mpi) 11632f69e4bSDaniel Jurgens { 11732f69e4bSDaniel Jurgens struct mlx5_ib_dev *dev; 11832f69e4bSDaniel Jurgens 11932f69e4bSDaniel Jurgens mutex_lock(&mlx5_ib_multiport_mutex); 12032f69e4bSDaniel Jurgens dev = mpi->ibdev; 12132f69e4bSDaniel Jurgens mutex_unlock(&mlx5_ib_multiport_mutex); 12232f69e4bSDaniel Jurgens return dev; 12332f69e4bSDaniel Jurgens } 12432f69e4bSDaniel Jurgens 1251b5daf11SMajd Dibbiny static enum rdma_link_layer 126ebd61f68SAchiad Shochat mlx5_port_type_cap_to_rdma_ll(int port_type_cap) 1271b5daf11SMajd Dibbiny { 128ebd61f68SAchiad Shochat switch (port_type_cap) { 1291b5daf11SMajd Dibbiny case MLX5_CAP_PORT_TYPE_IB: 1301b5daf11SMajd Dibbiny return IB_LINK_LAYER_INFINIBAND; 1311b5daf11SMajd Dibbiny case MLX5_CAP_PORT_TYPE_ETH: 1321b5daf11SMajd Dibbiny return IB_LINK_LAYER_ETHERNET; 1331b5daf11SMajd Dibbiny default: 1341b5daf11SMajd Dibbiny return IB_LINK_LAYER_UNSPECIFIED; 1351b5daf11SMajd Dibbiny } 1361b5daf11SMajd Dibbiny } 1371b5daf11SMajd Dibbiny 138ebd61f68SAchiad Shochat static enum rdma_link_layer 139ebd61f68SAchiad Shochat mlx5_ib_port_link_layer(struct ib_device *device, u8 port_num) 140ebd61f68SAchiad Shochat { 141ebd61f68SAchiad Shochat struct mlx5_ib_dev *dev = to_mdev(device); 142ebd61f68SAchiad Shochat int port_type_cap = MLX5_CAP_GEN(dev->mdev, port_type); 143ebd61f68SAchiad Shochat 144ebd61f68SAchiad Shochat return mlx5_port_type_cap_to_rdma_ll(port_type_cap); 145ebd61f68SAchiad Shochat } 146ebd61f68SAchiad Shochat 147fd65f1b8SMoni Shoua static int get_port_state(struct ib_device *ibdev, 148fd65f1b8SMoni Shoua u8 port_num, 149fd65f1b8SMoni Shoua enum ib_port_state *state) 150fd65f1b8SMoni Shoua { 151fd65f1b8SMoni Shoua struct ib_port_attr attr; 152fd65f1b8SMoni Shoua int ret; 153fd65f1b8SMoni Shoua 154fd65f1b8SMoni Shoua memset(&attr, 0, sizeof(attr)); 1553023a1e9SKamal Heib ret = ibdev->ops.query_port(ibdev, port_num, &attr); 156fd65f1b8SMoni Shoua if (!ret) 157fd65f1b8SMoni Shoua *state = attr.state; 158fd65f1b8SMoni Shoua return ret; 159fd65f1b8SMoni Shoua } 160fd65f1b8SMoni Shoua 16135b0aa67SMark Bloch static struct mlx5_roce *mlx5_get_rep_roce(struct mlx5_ib_dev *dev, 16235b0aa67SMark Bloch struct net_device *ndev, 16335b0aa67SMark Bloch u8 *port_num) 16435b0aa67SMark Bloch { 16535b0aa67SMark Bloch struct mlx5_eswitch *esw = dev->mdev->priv.eswitch; 16635b0aa67SMark Bloch struct net_device *rep_ndev; 16735b0aa67SMark Bloch struct mlx5_ib_port *port; 16835b0aa67SMark Bloch int i; 16935b0aa67SMark Bloch 17035b0aa67SMark Bloch for (i = 0; i < dev->num_ports; i++) { 17135b0aa67SMark Bloch port = &dev->port[i]; 17235b0aa67SMark Bloch if (!port->rep) 17335b0aa67SMark Bloch continue; 17435b0aa67SMark Bloch 17535b0aa67SMark Bloch read_lock(&port->roce.netdev_lock); 17635b0aa67SMark Bloch rep_ndev = mlx5_ib_get_rep_netdev(esw, 17735b0aa67SMark Bloch port->rep->vport); 17835b0aa67SMark Bloch if (rep_ndev == ndev) { 17935b0aa67SMark Bloch read_unlock(&port->roce.netdev_lock); 18035b0aa67SMark Bloch *port_num = i + 1; 18135b0aa67SMark Bloch return &port->roce; 18235b0aa67SMark Bloch } 18335b0aa67SMark Bloch read_unlock(&port->roce.netdev_lock); 18435b0aa67SMark Bloch } 18535b0aa67SMark Bloch 18635b0aa67SMark Bloch return NULL; 18735b0aa67SMark Bloch } 18835b0aa67SMark Bloch 189fc24fc5eSAchiad Shochat static int mlx5_netdev_event(struct notifier_block *this, 190fc24fc5eSAchiad Shochat unsigned long event, void *ptr) 191fc24fc5eSAchiad Shochat { 1927fd8aefbSDaniel Jurgens struct mlx5_roce *roce = container_of(this, struct mlx5_roce, nb); 193fc24fc5eSAchiad Shochat struct net_device *ndev = netdev_notifier_info_to_dev(ptr); 1947fd8aefbSDaniel Jurgens u8 port_num = roce->native_port_num; 1957fd8aefbSDaniel Jurgens struct mlx5_core_dev *mdev; 1967fd8aefbSDaniel Jurgens struct mlx5_ib_dev *ibdev; 1977fd8aefbSDaniel Jurgens 1987fd8aefbSDaniel Jurgens ibdev = roce->dev; 19932f69e4bSDaniel Jurgens mdev = mlx5_ib_get_native_port_mdev(ibdev, port_num, NULL); 20032f69e4bSDaniel Jurgens if (!mdev) 20132f69e4bSDaniel Jurgens return NOTIFY_DONE; 202fc24fc5eSAchiad Shochat 2035ec8c83eSAviv Heller switch (event) { 2045ec8c83eSAviv Heller case NETDEV_REGISTER: 20535b0aa67SMark Bloch /* Should already be registered during the load */ 20635b0aa67SMark Bloch if (ibdev->is_rep) 20735b0aa67SMark Bloch break; 2087fd8aefbSDaniel Jurgens write_lock(&roce->netdev_lock); 209dce45af5SLinus Torvalds if (ndev->dev.parent == mdev->device) 210842a9c83SOr Gerlitz roce->netdev = ndev; 2117fd8aefbSDaniel Jurgens write_unlock(&roce->netdev_lock); 2125ec8c83eSAviv Heller break; 2135ec8c83eSAviv Heller 214842a9c83SOr Gerlitz case NETDEV_UNREGISTER: 21535b0aa67SMark Bloch /* In case of reps, ib device goes away before the netdevs */ 216842a9c83SOr Gerlitz write_lock(&roce->netdev_lock); 217842a9c83SOr Gerlitz if (roce->netdev == ndev) 218842a9c83SOr Gerlitz roce->netdev = NULL; 219842a9c83SOr Gerlitz write_unlock(&roce->netdev_lock); 220842a9c83SOr Gerlitz break; 221842a9c83SOr Gerlitz 222fd65f1b8SMoni Shoua case NETDEV_CHANGE: 2235ec8c83eSAviv Heller case NETDEV_UP: 22488621dfeSAviv Heller case NETDEV_DOWN: { 2257fd8aefbSDaniel Jurgens struct net_device *lag_ndev = mlx5_lag_get_roce_netdev(mdev); 22688621dfeSAviv Heller struct net_device *upper = NULL; 22788621dfeSAviv Heller 22888621dfeSAviv Heller if (lag_ndev) { 22988621dfeSAviv Heller upper = netdev_master_upper_dev_get(lag_ndev); 23088621dfeSAviv Heller dev_put(lag_ndev); 23188621dfeSAviv Heller } 23288621dfeSAviv Heller 23335b0aa67SMark Bloch if (ibdev->is_rep) 23435b0aa67SMark Bloch roce = mlx5_get_rep_roce(ibdev, ndev, &port_num); 23535b0aa67SMark Bloch if (!roce) 23635b0aa67SMark Bloch return NOTIFY_DONE; 2377fd8aefbSDaniel Jurgens if ((upper == ndev || (!upper && ndev == roce->netdev)) 23888621dfeSAviv Heller && ibdev->ib_active) { 239626bc02dSBart Van Assche struct ib_event ibev = { }; 240fd65f1b8SMoni Shoua enum ib_port_state port_state; 2415ec8c83eSAviv Heller 2427fd8aefbSDaniel Jurgens if (get_port_state(&ibdev->ib_dev, port_num, 2437fd8aefbSDaniel Jurgens &port_state)) 2447fd8aefbSDaniel Jurgens goto done; 245fd65f1b8SMoni Shoua 2467fd8aefbSDaniel Jurgens if (roce->last_port_state == port_state) 2477fd8aefbSDaniel Jurgens goto done; 248fd65f1b8SMoni Shoua 2497fd8aefbSDaniel Jurgens roce->last_port_state = port_state; 2505ec8c83eSAviv Heller ibev.device = &ibdev->ib_dev; 251fd65f1b8SMoni Shoua if (port_state == IB_PORT_DOWN) 252fd65f1b8SMoni Shoua ibev.event = IB_EVENT_PORT_ERR; 253fd65f1b8SMoni Shoua else if (port_state == IB_PORT_ACTIVE) 254fd65f1b8SMoni Shoua ibev.event = IB_EVENT_PORT_ACTIVE; 255fd65f1b8SMoni Shoua else 2567fd8aefbSDaniel Jurgens goto done; 257fd65f1b8SMoni Shoua 2587fd8aefbSDaniel Jurgens ibev.element.port_num = port_num; 2595ec8c83eSAviv Heller ib_dispatch_event(&ibev); 2605ec8c83eSAviv Heller } 2615ec8c83eSAviv Heller break; 26288621dfeSAviv Heller } 2635ec8c83eSAviv Heller 2645ec8c83eSAviv Heller default: 2655ec8c83eSAviv Heller break; 2665ec8c83eSAviv Heller } 2677fd8aefbSDaniel Jurgens done: 26832f69e4bSDaniel Jurgens mlx5_ib_put_native_port_mdev(ibdev, port_num); 269fc24fc5eSAchiad Shochat return NOTIFY_DONE; 270fc24fc5eSAchiad Shochat } 271fc24fc5eSAchiad Shochat 272fc24fc5eSAchiad Shochat static struct net_device *mlx5_ib_get_netdev(struct ib_device *device, 273fc24fc5eSAchiad Shochat u8 port_num) 274fc24fc5eSAchiad Shochat { 275fc24fc5eSAchiad Shochat struct mlx5_ib_dev *ibdev = to_mdev(device); 276fc24fc5eSAchiad Shochat struct net_device *ndev; 27732f69e4bSDaniel Jurgens struct mlx5_core_dev *mdev; 278fc24fc5eSAchiad Shochat 27932f69e4bSDaniel Jurgens mdev = mlx5_ib_get_native_port_mdev(ibdev, port_num, NULL); 28032f69e4bSDaniel Jurgens if (!mdev) 28132f69e4bSDaniel Jurgens return NULL; 28232f69e4bSDaniel Jurgens 28332f69e4bSDaniel Jurgens ndev = mlx5_lag_get_roce_netdev(mdev); 28488621dfeSAviv Heller if (ndev) 28532f69e4bSDaniel Jurgens goto out; 28688621dfeSAviv Heller 287fc24fc5eSAchiad Shochat /* Ensure ndev does not disappear before we invoke dev_hold() 288fc24fc5eSAchiad Shochat */ 28995579e78SMark Bloch read_lock(&ibdev->port[port_num - 1].roce.netdev_lock); 29095579e78SMark Bloch ndev = ibdev->port[port_num - 1].roce.netdev; 291fc24fc5eSAchiad Shochat if (ndev) 292fc24fc5eSAchiad Shochat dev_hold(ndev); 29395579e78SMark Bloch read_unlock(&ibdev->port[port_num - 1].roce.netdev_lock); 294fc24fc5eSAchiad Shochat 29532f69e4bSDaniel Jurgens out: 29632f69e4bSDaniel Jurgens mlx5_ib_put_native_port_mdev(ibdev, port_num); 297fc24fc5eSAchiad Shochat return ndev; 298fc24fc5eSAchiad Shochat } 299fc24fc5eSAchiad Shochat 30032f69e4bSDaniel Jurgens struct mlx5_core_dev *mlx5_ib_get_native_port_mdev(struct mlx5_ib_dev *ibdev, 30132f69e4bSDaniel Jurgens u8 ib_port_num, 30232f69e4bSDaniel Jurgens u8 *native_port_num) 30332f69e4bSDaniel Jurgens { 30432f69e4bSDaniel Jurgens enum rdma_link_layer ll = mlx5_ib_port_link_layer(&ibdev->ib_dev, 30532f69e4bSDaniel Jurgens ib_port_num); 30632f69e4bSDaniel Jurgens struct mlx5_core_dev *mdev = NULL; 30732f69e4bSDaniel Jurgens struct mlx5_ib_multiport_info *mpi; 30832f69e4bSDaniel Jurgens struct mlx5_ib_port *port; 30932f69e4bSDaniel Jurgens 310210b1f78SMark Bloch if (!mlx5_core_mp_enabled(ibdev->mdev) || 311210b1f78SMark Bloch ll != IB_LINK_LAYER_ETHERNET) { 312210b1f78SMark Bloch if (native_port_num) 313210b1f78SMark Bloch *native_port_num = ib_port_num; 314210b1f78SMark Bloch return ibdev->mdev; 315210b1f78SMark Bloch } 316210b1f78SMark Bloch 31732f69e4bSDaniel Jurgens if (native_port_num) 31832f69e4bSDaniel Jurgens *native_port_num = 1; 31932f69e4bSDaniel Jurgens 32032f69e4bSDaniel Jurgens port = &ibdev->port[ib_port_num - 1]; 32132f69e4bSDaniel Jurgens if (!port) 32232f69e4bSDaniel Jurgens return NULL; 32332f69e4bSDaniel Jurgens 32432f69e4bSDaniel Jurgens spin_lock(&port->mp.mpi_lock); 32532f69e4bSDaniel Jurgens mpi = ibdev->port[ib_port_num - 1].mp.mpi; 32632f69e4bSDaniel Jurgens if (mpi && !mpi->unaffiliate) { 32732f69e4bSDaniel Jurgens mdev = mpi->mdev; 32832f69e4bSDaniel Jurgens /* If it's the master no need to refcount, it'll exist 32932f69e4bSDaniel Jurgens * as long as the ib_dev exists. 33032f69e4bSDaniel Jurgens */ 33132f69e4bSDaniel Jurgens if (!mpi->is_master) 33232f69e4bSDaniel Jurgens mpi->mdev_refcnt++; 33332f69e4bSDaniel Jurgens } 33432f69e4bSDaniel Jurgens spin_unlock(&port->mp.mpi_lock); 33532f69e4bSDaniel Jurgens 33632f69e4bSDaniel Jurgens return mdev; 33732f69e4bSDaniel Jurgens } 33832f69e4bSDaniel Jurgens 33932f69e4bSDaniel Jurgens void mlx5_ib_put_native_port_mdev(struct mlx5_ib_dev *ibdev, u8 port_num) 34032f69e4bSDaniel Jurgens { 34132f69e4bSDaniel Jurgens enum rdma_link_layer ll = mlx5_ib_port_link_layer(&ibdev->ib_dev, 34232f69e4bSDaniel Jurgens port_num); 34332f69e4bSDaniel Jurgens struct mlx5_ib_multiport_info *mpi; 34432f69e4bSDaniel Jurgens struct mlx5_ib_port *port; 34532f69e4bSDaniel Jurgens 34632f69e4bSDaniel Jurgens if (!mlx5_core_mp_enabled(ibdev->mdev) || ll != IB_LINK_LAYER_ETHERNET) 34732f69e4bSDaniel Jurgens return; 34832f69e4bSDaniel Jurgens 34932f69e4bSDaniel Jurgens port = &ibdev->port[port_num - 1]; 35032f69e4bSDaniel Jurgens 35132f69e4bSDaniel Jurgens spin_lock(&port->mp.mpi_lock); 35232f69e4bSDaniel Jurgens mpi = ibdev->port[port_num - 1].mp.mpi; 35332f69e4bSDaniel Jurgens if (mpi->is_master) 35432f69e4bSDaniel Jurgens goto out; 35532f69e4bSDaniel Jurgens 35632f69e4bSDaniel Jurgens mpi->mdev_refcnt--; 35732f69e4bSDaniel Jurgens if (mpi->unaffiliate) 35832f69e4bSDaniel Jurgens complete(&mpi->unref_comp); 35932f69e4bSDaniel Jurgens out: 36032f69e4bSDaniel Jurgens spin_unlock(&port->mp.mpi_lock); 36132f69e4bSDaniel Jurgens } 36232f69e4bSDaniel Jurgens 36308e8676fSAya Levin static int translate_eth_legacy_proto_oper(u32 eth_proto_oper, u8 *active_speed, 364f1b65df5SNoa Osherovich u8 *active_width) 365f1b65df5SNoa Osherovich { 366f1b65df5SNoa Osherovich switch (eth_proto_oper) { 367f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_1000BASE_CX_SGMII): 368f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_1000BASE_KX): 369f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_100BASE_TX): 370f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_1000BASE_T): 371f1b65df5SNoa Osherovich *active_width = IB_WIDTH_1X; 372f1b65df5SNoa Osherovich *active_speed = IB_SPEED_SDR; 373f1b65df5SNoa Osherovich break; 374f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_10GBASE_T): 375f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_10GBASE_CX4): 376f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_10GBASE_KX4): 377f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_10GBASE_KR): 378f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_10GBASE_CR): 379f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_10GBASE_SR): 380f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_10GBASE_ER): 381f1b65df5SNoa Osherovich *active_width = IB_WIDTH_1X; 382f1b65df5SNoa Osherovich *active_speed = IB_SPEED_QDR; 383f1b65df5SNoa Osherovich break; 384f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_25GBASE_CR): 385f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_25GBASE_KR): 386f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_25GBASE_SR): 387f1b65df5SNoa Osherovich *active_width = IB_WIDTH_1X; 388f1b65df5SNoa Osherovich *active_speed = IB_SPEED_EDR; 389f1b65df5SNoa Osherovich break; 390f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_40GBASE_CR4): 391f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_40GBASE_KR4): 392f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_40GBASE_SR4): 393f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_40GBASE_LR4): 394f1b65df5SNoa Osherovich *active_width = IB_WIDTH_4X; 395f1b65df5SNoa Osherovich *active_speed = IB_SPEED_QDR; 396f1b65df5SNoa Osherovich break; 397f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_50GBASE_CR2): 398f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_50GBASE_KR2): 399f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_50GBASE_SR2): 400f1b65df5SNoa Osherovich *active_width = IB_WIDTH_1X; 401f1b65df5SNoa Osherovich *active_speed = IB_SPEED_HDR; 402f1b65df5SNoa Osherovich break; 403f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_56GBASE_R4): 404f1b65df5SNoa Osherovich *active_width = IB_WIDTH_4X; 405f1b65df5SNoa Osherovich *active_speed = IB_SPEED_FDR; 406f1b65df5SNoa Osherovich break; 407f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_100GBASE_CR4): 408f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_100GBASE_SR4): 409f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_100GBASE_KR4): 410f1b65df5SNoa Osherovich case MLX5E_PROT_MASK(MLX5E_100GBASE_LR4): 411f1b65df5SNoa Osherovich *active_width = IB_WIDTH_4X; 412f1b65df5SNoa Osherovich *active_speed = IB_SPEED_EDR; 413f1b65df5SNoa Osherovich break; 414f1b65df5SNoa Osherovich default: 415f1b65df5SNoa Osherovich return -EINVAL; 416f1b65df5SNoa Osherovich } 417f1b65df5SNoa Osherovich 418f1b65df5SNoa Osherovich return 0; 419f1b65df5SNoa Osherovich } 420f1b65df5SNoa Osherovich 42108e8676fSAya Levin static int translate_eth_ext_proto_oper(u32 eth_proto_oper, u8 *active_speed, 42208e8676fSAya Levin u8 *active_width) 42308e8676fSAya Levin { 42408e8676fSAya Levin switch (eth_proto_oper) { 42508e8676fSAya Levin case MLX5E_PROT_MASK(MLX5E_SGMII_100M): 42608e8676fSAya Levin case MLX5E_PROT_MASK(MLX5E_1000BASE_X_SGMII): 42708e8676fSAya Levin *active_width = IB_WIDTH_1X; 42808e8676fSAya Levin *active_speed = IB_SPEED_SDR; 42908e8676fSAya Levin break; 43008e8676fSAya Levin case MLX5E_PROT_MASK(MLX5E_5GBASE_R): 43108e8676fSAya Levin *active_width = IB_WIDTH_1X; 43208e8676fSAya Levin *active_speed = IB_SPEED_DDR; 43308e8676fSAya Levin break; 43408e8676fSAya Levin case MLX5E_PROT_MASK(MLX5E_10GBASE_XFI_XAUI_1): 43508e8676fSAya Levin *active_width = IB_WIDTH_1X; 43608e8676fSAya Levin *active_speed = IB_SPEED_QDR; 43708e8676fSAya Levin break; 43808e8676fSAya Levin case MLX5E_PROT_MASK(MLX5E_40GBASE_XLAUI_4_XLPPI_4): 43908e8676fSAya Levin *active_width = IB_WIDTH_4X; 44008e8676fSAya Levin *active_speed = IB_SPEED_QDR; 44108e8676fSAya Levin break; 44208e8676fSAya Levin case MLX5E_PROT_MASK(MLX5E_25GAUI_1_25GBASE_CR_KR): 44308e8676fSAya Levin *active_width = IB_WIDTH_1X; 44408e8676fSAya Levin *active_speed = IB_SPEED_EDR; 44508e8676fSAya Levin break; 44608e8676fSAya Levin case MLX5E_PROT_MASK(MLX5E_50GAUI_2_LAUI_2_50GBASE_CR2_KR2): 447cd272875SAya Levin *active_width = IB_WIDTH_2X; 448cd272875SAya Levin *active_speed = IB_SPEED_EDR; 449cd272875SAya Levin break; 45008e8676fSAya Levin case MLX5E_PROT_MASK(MLX5E_50GAUI_1_LAUI_1_50GBASE_CR_KR): 45108e8676fSAya Levin *active_width = IB_WIDTH_1X; 45208e8676fSAya Levin *active_speed = IB_SPEED_HDR; 45308e8676fSAya Levin break; 454cd272875SAya Levin case MLX5E_PROT_MASK(MLX5E_CAUI_4_100GBASE_CR4_KR4): 455cd272875SAya Levin *active_width = IB_WIDTH_4X; 456cd272875SAya Levin *active_speed = IB_SPEED_EDR; 457cd272875SAya Levin break; 45808e8676fSAya Levin case MLX5E_PROT_MASK(MLX5E_100GAUI_2_100GBASE_CR2_KR2): 45908e8676fSAya Levin *active_width = IB_WIDTH_2X; 46008e8676fSAya Levin *active_speed = IB_SPEED_HDR; 46108e8676fSAya Levin break; 46208e8676fSAya Levin case MLX5E_PROT_MASK(MLX5E_200GAUI_4_200GBASE_CR4_KR4): 46308e8676fSAya Levin *active_width = IB_WIDTH_4X; 46408e8676fSAya Levin *active_speed = IB_SPEED_HDR; 46508e8676fSAya Levin break; 46608e8676fSAya Levin default: 46708e8676fSAya Levin return -EINVAL; 46808e8676fSAya Levin } 46908e8676fSAya Levin 47008e8676fSAya Levin return 0; 47108e8676fSAya Levin } 47208e8676fSAya Levin 47308e8676fSAya Levin static int translate_eth_proto_oper(u32 eth_proto_oper, u8 *active_speed, 47408e8676fSAya Levin u8 *active_width, bool ext) 47508e8676fSAya Levin { 47608e8676fSAya Levin return ext ? 47708e8676fSAya Levin translate_eth_ext_proto_oper(eth_proto_oper, active_speed, 47808e8676fSAya Levin active_width) : 47908e8676fSAya Levin translate_eth_legacy_proto_oper(eth_proto_oper, active_speed, 48008e8676fSAya Levin active_width); 48108e8676fSAya Levin } 48208e8676fSAya Levin 483095b0927SIlan Tayari static int mlx5_query_port_roce(struct ib_device *device, u8 port_num, 4843f89a643SAchiad Shochat struct ib_port_attr *props) 4853f89a643SAchiad Shochat { 4863f89a643SAchiad Shochat struct mlx5_ib_dev *dev = to_mdev(device); 487bc4e12ffSAya Levin u32 out[MLX5_ST_SZ_DW(ptys_reg)] = {0}; 488da005f9fSColin Ian King struct mlx5_core_dev *mdev; 48988621dfeSAviv Heller struct net_device *ndev, *upper; 4903f89a643SAchiad Shochat enum ib_mtu ndev_ib_mtu; 491b3cbd6f0SDaniel Jurgens bool put_mdev = true; 492c876a1b7SLeon Romanovsky u16 qkey_viol_cntr; 493f1b65df5SNoa Osherovich u32 eth_prot_oper; 494b3cbd6f0SDaniel Jurgens u8 mdev_port_num; 49508e8676fSAya Levin bool ext; 496095b0927SIlan Tayari int err; 4973f89a643SAchiad Shochat 498b3cbd6f0SDaniel Jurgens mdev = mlx5_ib_get_native_port_mdev(dev, port_num, &mdev_port_num); 499b3cbd6f0SDaniel Jurgens if (!mdev) { 500b3cbd6f0SDaniel Jurgens /* This means the port isn't affiliated yet. Get the 501b3cbd6f0SDaniel Jurgens * info for the master port instead. 502b3cbd6f0SDaniel Jurgens */ 503b3cbd6f0SDaniel Jurgens put_mdev = false; 504b3cbd6f0SDaniel Jurgens mdev = dev->mdev; 505b3cbd6f0SDaniel Jurgens mdev_port_num = 1; 506b3cbd6f0SDaniel Jurgens port_num = 1; 507b3cbd6f0SDaniel Jurgens } 508b3cbd6f0SDaniel Jurgens 509f1b65df5SNoa Osherovich /* Possible bad flows are checked before filling out props so in case 510f1b65df5SNoa Osherovich * of an error it will still be zeroed out. 51126628e2dSMark Bloch * Use native port in case of reps 51250f22fd8SNoa Osherovich */ 51326628e2dSMark Bloch if (dev->is_rep) 51426628e2dSMark Bloch err = mlx5_query_port_ptys(mdev, out, sizeof(out), MLX5_PTYS_EN, 51526628e2dSMark Bloch 1); 51626628e2dSMark Bloch else 517bc4e12ffSAya Levin err = mlx5_query_port_ptys(mdev, out, sizeof(out), MLX5_PTYS_EN, 518b3cbd6f0SDaniel Jurgens mdev_port_num); 519095b0927SIlan Tayari if (err) 520b3cbd6f0SDaniel Jurgens goto out; 52108e8676fSAya Levin ext = MLX5_CAP_PCAM_FEATURE(dev->mdev, ptys_extended_ethernet); 52208e8676fSAya Levin eth_prot_oper = MLX5_GET_ETH_PROTO(ptys_reg, out, ext, eth_proto_oper); 523f1b65df5SNoa Osherovich 5247672ed33SHonggang Li props->active_width = IB_WIDTH_4X; 5257672ed33SHonggang Li props->active_speed = IB_SPEED_QDR; 5267672ed33SHonggang Li 527f1b65df5SNoa Osherovich translate_eth_proto_oper(eth_prot_oper, &props->active_speed, 52808e8676fSAya Levin &props->active_width, ext); 5293f89a643SAchiad Shochat 5303f89a643SAchiad Shochat props->port_cap_flags |= IB_PORT_CM_SUP; 5312f944c0fSJason Gunthorpe props->ip_gids = true; 5323f89a643SAchiad Shochat 5333f89a643SAchiad Shochat props->gid_tbl_len = MLX5_CAP_ROCE(dev->mdev, 5343f89a643SAchiad Shochat roce_address_table_size); 5353f89a643SAchiad Shochat props->max_mtu = IB_MTU_4096; 5363f89a643SAchiad Shochat props->max_msg_sz = 1 << MLX5_CAP_GEN(dev->mdev, log_max_msg); 5373f89a643SAchiad Shochat props->pkey_tbl_len = 1; 5383f89a643SAchiad Shochat props->state = IB_PORT_DOWN; 53972a7720fSKamal Heib props->phys_state = IB_PORT_PHYS_STATE_DISABLED; 5403f89a643SAchiad Shochat 541b3cbd6f0SDaniel Jurgens mlx5_query_nic_vport_qkey_viol_cntr(mdev, &qkey_viol_cntr); 542c876a1b7SLeon Romanovsky props->qkey_viol_cntr = qkey_viol_cntr; 5433f89a643SAchiad Shochat 544b3cbd6f0SDaniel Jurgens /* If this is a stub query for an unaffiliated port stop here */ 545b3cbd6f0SDaniel Jurgens if (!put_mdev) 546b3cbd6f0SDaniel Jurgens goto out; 547b3cbd6f0SDaniel Jurgens 5483f89a643SAchiad Shochat ndev = mlx5_ib_get_netdev(device, port_num); 5493f89a643SAchiad Shochat if (!ndev) 550b3cbd6f0SDaniel Jurgens goto out; 5513f89a643SAchiad Shochat 5527c34ec19SAviv Heller if (dev->lag_active) { 55388621dfeSAviv Heller rcu_read_lock(); 55488621dfeSAviv Heller upper = netdev_master_upper_dev_get_rcu(ndev); 55588621dfeSAviv Heller if (upper) { 55688621dfeSAviv Heller dev_put(ndev); 55788621dfeSAviv Heller ndev = upper; 55888621dfeSAviv Heller dev_hold(ndev); 55988621dfeSAviv Heller } 56088621dfeSAviv Heller rcu_read_unlock(); 56188621dfeSAviv Heller } 56288621dfeSAviv Heller 5633f89a643SAchiad Shochat if (netif_running(ndev) && netif_carrier_ok(ndev)) { 5643f89a643SAchiad Shochat props->state = IB_PORT_ACTIVE; 56572a7720fSKamal Heib props->phys_state = IB_PORT_PHYS_STATE_LINK_UP; 5663f89a643SAchiad Shochat } 5673f89a643SAchiad Shochat 5683f89a643SAchiad Shochat ndev_ib_mtu = iboe_get_mtu(ndev->mtu); 5693f89a643SAchiad Shochat 5703f89a643SAchiad Shochat dev_put(ndev); 5713f89a643SAchiad Shochat 5723f89a643SAchiad Shochat props->active_mtu = min(props->max_mtu, ndev_ib_mtu); 573b3cbd6f0SDaniel Jurgens out: 574b3cbd6f0SDaniel Jurgens if (put_mdev) 575b3cbd6f0SDaniel Jurgens mlx5_ib_put_native_port_mdev(dev, port_num); 576b3cbd6f0SDaniel Jurgens return err; 5773f89a643SAchiad Shochat } 5783f89a643SAchiad Shochat 579095b0927SIlan Tayari static int set_roce_addr(struct mlx5_ib_dev *dev, u8 port_num, 580095b0927SIlan Tayari unsigned int index, const union ib_gid *gid, 581095b0927SIlan Tayari const struct ib_gid_attr *attr) 5823cca2606SAchiad Shochat { 583095b0927SIlan Tayari enum ib_gid_type gid_type = IB_GID_TYPE_IB; 584a70c0739SParav Pandit u16 vlan_id = 0xffff; 585095b0927SIlan Tayari u8 roce_version = 0; 586095b0927SIlan Tayari u8 roce_l3_type = 0; 587095b0927SIlan Tayari u8 mac[ETH_ALEN]; 588a70c0739SParav Pandit int ret; 5893cca2606SAchiad Shochat 590095b0927SIlan Tayari if (gid) { 591095b0927SIlan Tayari gid_type = attr->gid_type; 592a70c0739SParav Pandit ret = rdma_read_gid_l2_fields(attr, &vlan_id, &mac[0]); 593a70c0739SParav Pandit if (ret) 594a70c0739SParav Pandit return ret; 5953cca2606SAchiad Shochat } 5963cca2606SAchiad Shochat 597095b0927SIlan Tayari switch (gid_type) { 5983cca2606SAchiad Shochat case IB_GID_TYPE_IB: 599095b0927SIlan Tayari roce_version = MLX5_ROCE_VERSION_1; 6003cca2606SAchiad Shochat break; 6013cca2606SAchiad Shochat case IB_GID_TYPE_ROCE_UDP_ENCAP: 602095b0927SIlan Tayari roce_version = MLX5_ROCE_VERSION_2; 603095b0927SIlan Tayari if (ipv6_addr_v4mapped((void *)gid)) 604095b0927SIlan Tayari roce_l3_type = MLX5_ROCE_L3_TYPE_IPV4; 605095b0927SIlan Tayari else 606095b0927SIlan Tayari roce_l3_type = MLX5_ROCE_L3_TYPE_IPV6; 6073cca2606SAchiad Shochat break; 6083cca2606SAchiad Shochat 6093cca2606SAchiad Shochat default: 610095b0927SIlan Tayari mlx5_ib_warn(dev, "Unexpected GID type %u\n", gid_type); 6113cca2606SAchiad Shochat } 6123cca2606SAchiad Shochat 613095b0927SIlan Tayari return mlx5_core_roce_gid_set(dev->mdev, index, roce_version, 614cf34e1feSParav Pandit roce_l3_type, gid->raw, mac, 615a70c0739SParav Pandit vlan_id < VLAN_CFI_MASK, vlan_id, 616cf34e1feSParav Pandit port_num); 6173cca2606SAchiad Shochat } 6183cca2606SAchiad Shochat 619f4df9a7cSParav Pandit static int mlx5_ib_add_gid(const struct ib_gid_attr *attr, 6203cca2606SAchiad Shochat __always_unused void **context) 6213cca2606SAchiad Shochat { 622414448d2SParav Pandit return set_roce_addr(to_mdev(attr->device), attr->port_num, 623f4df9a7cSParav Pandit attr->index, &attr->gid, attr); 6243cca2606SAchiad Shochat } 6253cca2606SAchiad Shochat 626414448d2SParav Pandit static int mlx5_ib_del_gid(const struct ib_gid_attr *attr, 627414448d2SParav Pandit __always_unused void **context) 6283cca2606SAchiad Shochat { 629414448d2SParav Pandit return set_roce_addr(to_mdev(attr->device), attr->port_num, 630414448d2SParav Pandit attr->index, NULL, NULL); 6313cca2606SAchiad Shochat } 6323cca2606SAchiad Shochat 63347ec3866SParav Pandit __be16 mlx5_get_roce_udp_sport(struct mlx5_ib_dev *dev, 63447ec3866SParav Pandit const struct ib_gid_attr *attr) 6352811ba51SAchiad Shochat { 63647ec3866SParav Pandit if (attr->gid_type != IB_GID_TYPE_ROCE_UDP_ENCAP) 6372811ba51SAchiad Shochat return 0; 6382811ba51SAchiad Shochat 6392811ba51SAchiad Shochat return cpu_to_be16(MLX5_CAP_ROCE(dev->mdev, r_roce_min_src_udp_port)); 6402811ba51SAchiad Shochat } 6412811ba51SAchiad Shochat 6421b5daf11SMajd Dibbiny static int mlx5_use_mad_ifc(struct mlx5_ib_dev *dev) 6431b5daf11SMajd Dibbiny { 6447fae6655SNoa Osherovich if (MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_IB) 645d603c809SEli Cohen return !MLX5_CAP_GEN(dev->mdev, ib_virt); 6467fae6655SNoa Osherovich return 0; 6471b5daf11SMajd Dibbiny } 6481b5daf11SMajd Dibbiny 6491b5daf11SMajd Dibbiny enum { 6501b5daf11SMajd Dibbiny MLX5_VPORT_ACCESS_METHOD_MAD, 6511b5daf11SMajd Dibbiny MLX5_VPORT_ACCESS_METHOD_HCA, 6521b5daf11SMajd Dibbiny MLX5_VPORT_ACCESS_METHOD_NIC, 6531b5daf11SMajd Dibbiny }; 6541b5daf11SMajd Dibbiny 6551b5daf11SMajd Dibbiny static int mlx5_get_vport_access_method(struct ib_device *ibdev) 6561b5daf11SMajd Dibbiny { 6571b5daf11SMajd Dibbiny if (mlx5_use_mad_ifc(to_mdev(ibdev))) 6581b5daf11SMajd Dibbiny return MLX5_VPORT_ACCESS_METHOD_MAD; 6591b5daf11SMajd Dibbiny 660ebd61f68SAchiad Shochat if (mlx5_ib_port_link_layer(ibdev, 1) == 6611b5daf11SMajd Dibbiny IB_LINK_LAYER_ETHERNET) 6621b5daf11SMajd Dibbiny return MLX5_VPORT_ACCESS_METHOD_NIC; 6631b5daf11SMajd Dibbiny 6641b5daf11SMajd Dibbiny return MLX5_VPORT_ACCESS_METHOD_HCA; 6651b5daf11SMajd Dibbiny } 6661b5daf11SMajd Dibbiny 667da7525d2SEran Ben Elisha static void get_atomic_caps(struct mlx5_ib_dev *dev, 668776a3906SMoni Shoua u8 atomic_size_qp, 669da7525d2SEran Ben Elisha struct ib_device_attr *props) 670da7525d2SEran Ben Elisha { 671da7525d2SEran Ben Elisha u8 tmp; 672da7525d2SEran Ben Elisha u8 atomic_operations = MLX5_CAP_ATOMIC(dev->mdev, atomic_operations); 673da7525d2SEran Ben Elisha u8 atomic_req_8B_endianness_mode = 674bd10838aSOr Gerlitz MLX5_CAP_ATOMIC(dev->mdev, atomic_req_8B_endianness_mode); 675da7525d2SEran Ben Elisha 676da7525d2SEran Ben Elisha /* Check if HW supports 8 bytes standard atomic operations and capable 677da7525d2SEran Ben Elisha * of host endianness respond 678da7525d2SEran Ben Elisha */ 679da7525d2SEran Ben Elisha tmp = MLX5_ATOMIC_OPS_CMP_SWAP | MLX5_ATOMIC_OPS_FETCH_ADD; 680da7525d2SEran Ben Elisha if (((atomic_operations & tmp) == tmp) && 681da7525d2SEran Ben Elisha (atomic_size_qp & MLX5_ATOMIC_SIZE_QP_8BYTES) && 682da7525d2SEran Ben Elisha (atomic_req_8B_endianness_mode)) { 683da7525d2SEran Ben Elisha props->atomic_cap = IB_ATOMIC_HCA; 684da7525d2SEran Ben Elisha } else { 685da7525d2SEran Ben Elisha props->atomic_cap = IB_ATOMIC_NONE; 686da7525d2SEran Ben Elisha } 687da7525d2SEran Ben Elisha } 688da7525d2SEran Ben Elisha 689776a3906SMoni Shoua static void get_atomic_caps_qp(struct mlx5_ib_dev *dev, 690776a3906SMoni Shoua struct ib_device_attr *props) 691776a3906SMoni Shoua { 692776a3906SMoni Shoua u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_qp); 693776a3906SMoni Shoua 694776a3906SMoni Shoua get_atomic_caps(dev, atomic_size_qp, props); 695776a3906SMoni Shoua } 696776a3906SMoni Shoua 6971b5daf11SMajd Dibbiny static int mlx5_query_system_image_guid(struct ib_device *ibdev, 6981b5daf11SMajd Dibbiny __be64 *sys_image_guid) 6991b5daf11SMajd Dibbiny { 7001b5daf11SMajd Dibbiny struct mlx5_ib_dev *dev = to_mdev(ibdev); 7011b5daf11SMajd Dibbiny struct mlx5_core_dev *mdev = dev->mdev; 7021b5daf11SMajd Dibbiny u64 tmp; 7031b5daf11SMajd Dibbiny int err; 7041b5daf11SMajd Dibbiny 7051b5daf11SMajd Dibbiny switch (mlx5_get_vport_access_method(ibdev)) { 7061b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_MAD: 7071b5daf11SMajd Dibbiny return mlx5_query_mad_ifc_system_image_guid(ibdev, 7081b5daf11SMajd Dibbiny sys_image_guid); 7091b5daf11SMajd Dibbiny 7101b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_HCA: 7111b5daf11SMajd Dibbiny err = mlx5_query_hca_vport_system_image_guid(mdev, &tmp); 7123f89a643SAchiad Shochat break; 7133f89a643SAchiad Shochat 7143f89a643SAchiad Shochat case MLX5_VPORT_ACCESS_METHOD_NIC: 7153f89a643SAchiad Shochat err = mlx5_query_nic_vport_system_image_guid(mdev, &tmp); 7163f89a643SAchiad Shochat break; 7171b5daf11SMajd Dibbiny 7181b5daf11SMajd Dibbiny default: 7191b5daf11SMajd Dibbiny return -EINVAL; 7201b5daf11SMajd Dibbiny } 7213f89a643SAchiad Shochat 7223f89a643SAchiad Shochat if (!err) 7233f89a643SAchiad Shochat *sys_image_guid = cpu_to_be64(tmp); 7243f89a643SAchiad Shochat 7253f89a643SAchiad Shochat return err; 7263f89a643SAchiad Shochat 7271b5daf11SMajd Dibbiny } 7281b5daf11SMajd Dibbiny 7291b5daf11SMajd Dibbiny static int mlx5_query_max_pkeys(struct ib_device *ibdev, 7301b5daf11SMajd Dibbiny u16 *max_pkeys) 7311b5daf11SMajd Dibbiny { 7321b5daf11SMajd Dibbiny struct mlx5_ib_dev *dev = to_mdev(ibdev); 7331b5daf11SMajd Dibbiny struct mlx5_core_dev *mdev = dev->mdev; 7341b5daf11SMajd Dibbiny 7351b5daf11SMajd Dibbiny switch (mlx5_get_vport_access_method(ibdev)) { 7361b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_MAD: 7371b5daf11SMajd Dibbiny return mlx5_query_mad_ifc_max_pkeys(ibdev, max_pkeys); 7381b5daf11SMajd Dibbiny 7391b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_HCA: 7401b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_NIC: 7411b5daf11SMajd Dibbiny *max_pkeys = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, 7421b5daf11SMajd Dibbiny pkey_table_size)); 7431b5daf11SMajd Dibbiny return 0; 7441b5daf11SMajd Dibbiny 7451b5daf11SMajd Dibbiny default: 7461b5daf11SMajd Dibbiny return -EINVAL; 7471b5daf11SMajd Dibbiny } 7481b5daf11SMajd Dibbiny } 7491b5daf11SMajd Dibbiny 7501b5daf11SMajd Dibbiny static int mlx5_query_vendor_id(struct ib_device *ibdev, 7511b5daf11SMajd Dibbiny u32 *vendor_id) 7521b5daf11SMajd Dibbiny { 7531b5daf11SMajd Dibbiny struct mlx5_ib_dev *dev = to_mdev(ibdev); 7541b5daf11SMajd Dibbiny 7551b5daf11SMajd Dibbiny switch (mlx5_get_vport_access_method(ibdev)) { 7561b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_MAD: 7571b5daf11SMajd Dibbiny return mlx5_query_mad_ifc_vendor_id(ibdev, vendor_id); 7581b5daf11SMajd Dibbiny 7591b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_HCA: 7601b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_NIC: 7611b5daf11SMajd Dibbiny return mlx5_core_query_vendor_id(dev->mdev, vendor_id); 7621b5daf11SMajd Dibbiny 7631b5daf11SMajd Dibbiny default: 7641b5daf11SMajd Dibbiny return -EINVAL; 7651b5daf11SMajd Dibbiny } 7661b5daf11SMajd Dibbiny } 7671b5daf11SMajd Dibbiny 7681b5daf11SMajd Dibbiny static int mlx5_query_node_guid(struct mlx5_ib_dev *dev, 7691b5daf11SMajd Dibbiny __be64 *node_guid) 7701b5daf11SMajd Dibbiny { 7711b5daf11SMajd Dibbiny u64 tmp; 7721b5daf11SMajd Dibbiny int err; 7731b5daf11SMajd Dibbiny 7741b5daf11SMajd Dibbiny switch (mlx5_get_vport_access_method(&dev->ib_dev)) { 7751b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_MAD: 7761b5daf11SMajd Dibbiny return mlx5_query_mad_ifc_node_guid(dev, node_guid); 7771b5daf11SMajd Dibbiny 7781b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_HCA: 7791b5daf11SMajd Dibbiny err = mlx5_query_hca_vport_node_guid(dev->mdev, &tmp); 7803f89a643SAchiad Shochat break; 7813f89a643SAchiad Shochat 7823f89a643SAchiad Shochat case MLX5_VPORT_ACCESS_METHOD_NIC: 7833f89a643SAchiad Shochat err = mlx5_query_nic_vport_node_guid(dev->mdev, &tmp); 7843f89a643SAchiad Shochat break; 7851b5daf11SMajd Dibbiny 7861b5daf11SMajd Dibbiny default: 7871b5daf11SMajd Dibbiny return -EINVAL; 7881b5daf11SMajd Dibbiny } 7893f89a643SAchiad Shochat 7903f89a643SAchiad Shochat if (!err) 7913f89a643SAchiad Shochat *node_guid = cpu_to_be64(tmp); 7923f89a643SAchiad Shochat 7933f89a643SAchiad Shochat return err; 7941b5daf11SMajd Dibbiny } 7951b5daf11SMajd Dibbiny 7961b5daf11SMajd Dibbiny struct mlx5_reg_node_desc { 797bd99fdeaSYuval Shaia u8 desc[IB_DEVICE_NODE_DESC_MAX]; 7981b5daf11SMajd Dibbiny }; 7991b5daf11SMajd Dibbiny 8001b5daf11SMajd Dibbiny static int mlx5_query_node_desc(struct mlx5_ib_dev *dev, char *node_desc) 8011b5daf11SMajd Dibbiny { 8021b5daf11SMajd Dibbiny struct mlx5_reg_node_desc in; 8031b5daf11SMajd Dibbiny 8041b5daf11SMajd Dibbiny if (mlx5_use_mad_ifc(dev)) 8051b5daf11SMajd Dibbiny return mlx5_query_mad_ifc_node_desc(dev, node_desc); 8061b5daf11SMajd Dibbiny 8071b5daf11SMajd Dibbiny memset(&in, 0, sizeof(in)); 8081b5daf11SMajd Dibbiny 8091b5daf11SMajd Dibbiny return mlx5_core_access_reg(dev->mdev, &in, sizeof(in), node_desc, 8101b5daf11SMajd Dibbiny sizeof(struct mlx5_reg_node_desc), 8111b5daf11SMajd Dibbiny MLX5_REG_NODE_DESC, 0, 0); 8121b5daf11SMajd Dibbiny } 8131b5daf11SMajd Dibbiny 814e126ba97SEli Cohen static int mlx5_ib_query_device(struct ib_device *ibdev, 8152528e33eSMatan Barak struct ib_device_attr *props, 8162528e33eSMatan Barak struct ib_udata *uhw) 817e126ba97SEli Cohen { 81848357091SLeon Romanovsky size_t uhw_outlen = (uhw) ? uhw->outlen : 0; 819e126ba97SEli Cohen struct mlx5_ib_dev *dev = to_mdev(ibdev); 820938fe83cSSaeed Mahameed struct mlx5_core_dev *mdev = dev->mdev; 821e126ba97SEli Cohen int err = -ENOMEM; 822288c01b7SEli Cohen int max_sq_desc; 823e126ba97SEli Cohen int max_rq_sg; 824e126ba97SEli Cohen int max_sq_sg; 825e0238a6aSSagi Grimberg u64 min_page_size = 1ull << MLX5_CAP_GEN(mdev, log_pg_sz); 82685c7c014SDaniel Jurgens bool raw_support = !mlx5_core_mp_enabled(mdev); 827402ca536SBodong Wang struct mlx5_ib_query_device_resp resp = {}; 828402ca536SBodong Wang size_t resp_len; 829402ca536SBodong Wang u64 max_tso; 830e126ba97SEli Cohen 831402ca536SBodong Wang resp_len = sizeof(resp.comp_mask) + sizeof(resp.response_length); 83248357091SLeon Romanovsky if (uhw_outlen && uhw_outlen < resp_len) 833402ca536SBodong Wang return -EINVAL; 8346f26b2acSErez Alfasi 835402ca536SBodong Wang resp.response_length = resp_len; 836402ca536SBodong Wang 83748357091SLeon Romanovsky if (uhw && uhw->inlen && !ib_is_udata_cleared(uhw, 0, uhw->inlen)) 8382528e33eSMatan Barak return -EINVAL; 8392528e33eSMatan Barak 840e126ba97SEli Cohen memset(props, 0, sizeof(*props)); 8411b5daf11SMajd Dibbiny err = mlx5_query_system_image_guid(ibdev, 8421b5daf11SMajd Dibbiny &props->sys_image_guid); 8431b5daf11SMajd Dibbiny if (err) 8441b5daf11SMajd Dibbiny return err; 8451b5daf11SMajd Dibbiny 8461b5daf11SMajd Dibbiny err = mlx5_query_max_pkeys(ibdev, &props->max_pkeys); 8471b5daf11SMajd Dibbiny if (err) 8481b5daf11SMajd Dibbiny return err; 8491b5daf11SMajd Dibbiny 8501b5daf11SMajd Dibbiny err = mlx5_query_vendor_id(ibdev, &props->vendor_id); 8511b5daf11SMajd Dibbiny if (err) 8521b5daf11SMajd Dibbiny return err; 853e126ba97SEli Cohen 8549603b61dSJack Morgenstein props->fw_ver = ((u64)fw_rev_maj(dev->mdev) << 32) | 8559603b61dSJack Morgenstein (fw_rev_min(dev->mdev) << 16) | 8569603b61dSJack Morgenstein fw_rev_sub(dev->mdev); 857e126ba97SEli Cohen props->device_cap_flags = IB_DEVICE_CHANGE_PHY_PORT | 858e126ba97SEli Cohen IB_DEVICE_PORT_ACTIVE_EVENT | 859e126ba97SEli Cohen IB_DEVICE_SYS_IMAGE_GUID | 8601a4c3a3dSEli Cohen IB_DEVICE_RC_RNR_NAK_GEN; 861938fe83cSSaeed Mahameed 862938fe83cSSaeed Mahameed if (MLX5_CAP_GEN(mdev, pkv)) 863e126ba97SEli Cohen props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR; 864938fe83cSSaeed Mahameed if (MLX5_CAP_GEN(mdev, qkv)) 865e126ba97SEli Cohen props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR; 866938fe83cSSaeed Mahameed if (MLX5_CAP_GEN(mdev, apm)) 867e126ba97SEli Cohen props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG; 868938fe83cSSaeed Mahameed if (MLX5_CAP_GEN(mdev, xrc)) 869e126ba97SEli Cohen props->device_cap_flags |= IB_DEVICE_XRC; 870d2370e0aSMatan Barak if (MLX5_CAP_GEN(mdev, imaicl)) { 871d2370e0aSMatan Barak props->device_cap_flags |= IB_DEVICE_MEM_WINDOW | 872d2370e0aSMatan Barak IB_DEVICE_MEM_WINDOW_TYPE_2B; 873d2370e0aSMatan Barak props->max_mw = 1 << MLX5_CAP_GEN(mdev, log_max_mkey); 874b005d316SSagi Grimberg /* We support 'Gappy' memory registration too */ 875b005d316SSagi Grimberg props->device_cap_flags |= IB_DEVICE_SG_GAPS_REG; 876d2370e0aSMatan Barak } 877e126ba97SEli Cohen props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS; 878938fe83cSSaeed Mahameed if (MLX5_CAP_GEN(mdev, sho)) { 879c0a6cbb9SIsrael Rukshin props->device_cap_flags |= IB_DEVICE_INTEGRITY_HANDOVER; 8802dea9094SSagi Grimberg /* At this stage no support for signature handover */ 8812dea9094SSagi Grimberg props->sig_prot_cap = IB_PROT_T10DIF_TYPE_1 | 8822dea9094SSagi Grimberg IB_PROT_T10DIF_TYPE_2 | 8832dea9094SSagi Grimberg IB_PROT_T10DIF_TYPE_3; 8842dea9094SSagi Grimberg props->sig_guard_cap = IB_GUARD_T10DIF_CRC | 8852dea9094SSagi Grimberg IB_GUARD_T10DIF_CSUM; 8862dea9094SSagi Grimberg } 887938fe83cSSaeed Mahameed if (MLX5_CAP_GEN(mdev, block_lb_mc)) 888f360d88aSEli Cohen props->device_cap_flags |= IB_DEVICE_BLOCK_MULTICAST_LOOPBACK; 889e126ba97SEli Cohen 89085c7c014SDaniel Jurgens if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) && raw_support) { 891e8161334SNoa Osherovich if (MLX5_CAP_ETH(mdev, csum_cap)) { 892e8161334SNoa Osherovich /* Legacy bit to support old userspace libraries */ 89388115fe7SBodong Wang props->device_cap_flags |= IB_DEVICE_RAW_IP_CSUM; 894e8161334SNoa Osherovich props->raw_packet_caps |= IB_RAW_PACKET_CAP_IP_CSUM; 895e8161334SNoa Osherovich } 896e8161334SNoa Osherovich 897e8161334SNoa Osherovich if (MLX5_CAP_ETH(dev->mdev, vlan_cap)) 898e8161334SNoa Osherovich props->raw_packet_caps |= 899e8161334SNoa Osherovich IB_RAW_PACKET_CAP_CVLAN_STRIPPING; 90088115fe7SBodong Wang 90148357091SLeon Romanovsky if (field_avail(typeof(resp), tso_caps, uhw_outlen)) { 902402ca536SBodong Wang max_tso = MLX5_CAP_ETH(mdev, max_lso_cap); 903402ca536SBodong Wang if (max_tso) { 904402ca536SBodong Wang resp.tso_caps.max_tso = 1 << max_tso; 905402ca536SBodong Wang resp.tso_caps.supported_qpts |= 906402ca536SBodong Wang 1 << IB_QPT_RAW_PACKET; 907402ca536SBodong Wang resp.response_length += sizeof(resp.tso_caps); 908402ca536SBodong Wang } 909402ca536SBodong Wang } 91031f69a82SYishai Hadas 91148357091SLeon Romanovsky if (field_avail(typeof(resp), rss_caps, uhw_outlen)) { 91231f69a82SYishai Hadas resp.rss_caps.rx_hash_function = 91331f69a82SYishai Hadas MLX5_RX_HASH_FUNC_TOEPLITZ; 91431f69a82SYishai Hadas resp.rss_caps.rx_hash_fields_mask = 91531f69a82SYishai Hadas MLX5_RX_HASH_SRC_IPV4 | 91631f69a82SYishai Hadas MLX5_RX_HASH_DST_IPV4 | 91731f69a82SYishai Hadas MLX5_RX_HASH_SRC_IPV6 | 91831f69a82SYishai Hadas MLX5_RX_HASH_DST_IPV6 | 91931f69a82SYishai Hadas MLX5_RX_HASH_SRC_PORT_TCP | 92031f69a82SYishai Hadas MLX5_RX_HASH_DST_PORT_TCP | 92131f69a82SYishai Hadas MLX5_RX_HASH_SRC_PORT_UDP | 9224e2b53a5SMaor Gottlieb MLX5_RX_HASH_DST_PORT_UDP | 9234e2b53a5SMaor Gottlieb MLX5_RX_HASH_INNER; 9242d93fc85SMatan Barak if (mlx5_accel_ipsec_device_caps(dev->mdev) & 9252d93fc85SMatan Barak MLX5_ACCEL_IPSEC_CAP_DEVICE) 9262d93fc85SMatan Barak resp.rss_caps.rx_hash_fields_mask |= 9272d93fc85SMatan Barak MLX5_RX_HASH_IPSEC_SPI; 92831f69a82SYishai Hadas resp.response_length += sizeof(resp.rss_caps); 92931f69a82SYishai Hadas } 93031f69a82SYishai Hadas } else { 93148357091SLeon Romanovsky if (field_avail(typeof(resp), tso_caps, uhw_outlen)) 93231f69a82SYishai Hadas resp.response_length += sizeof(resp.tso_caps); 93348357091SLeon Romanovsky if (field_avail(typeof(resp), rss_caps, uhw_outlen)) 93431f69a82SYishai Hadas resp.response_length += sizeof(resp.rss_caps); 935402ca536SBodong Wang } 936402ca536SBodong Wang 937f0313965SErez Shitrit if (MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) { 938f0313965SErez Shitrit props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM; 939f0313965SErez Shitrit props->device_cap_flags |= IB_DEVICE_UD_TSO; 940f0313965SErez Shitrit } 941f0313965SErez Shitrit 94203404e8aSMaor Gottlieb if (MLX5_CAP_GEN(dev->mdev, rq_delay_drop) && 94385c7c014SDaniel Jurgens MLX5_CAP_GEN(dev->mdev, general_notification_event) && 94485c7c014SDaniel Jurgens raw_support) 94503404e8aSMaor Gottlieb props->raw_packet_caps |= IB_RAW_PACKET_CAP_DELAY_DROP; 94603404e8aSMaor Gottlieb 9471d54f890SYishai Hadas if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads) && 9481d54f890SYishai Hadas MLX5_CAP_IPOIB_ENHANCED(mdev, csum_cap)) 9491d54f890SYishai Hadas props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM; 9501d54f890SYishai Hadas 951cff5a0f3SMajd Dibbiny if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) && 95285c7c014SDaniel Jurgens MLX5_CAP_ETH(dev->mdev, scatter_fcs) && 95385c7c014SDaniel Jurgens raw_support) { 954e8161334SNoa Osherovich /* Legacy bit to support old userspace libraries */ 955cff5a0f3SMajd Dibbiny props->device_cap_flags |= IB_DEVICE_RAW_SCATTER_FCS; 956e8161334SNoa Osherovich props->raw_packet_caps |= IB_RAW_PACKET_CAP_SCATTER_FCS; 957e8161334SNoa Osherovich } 958cff5a0f3SMajd Dibbiny 95924da0016SAriel Levkovich if (MLX5_CAP_DEV_MEM(mdev, memic)) { 96024da0016SAriel Levkovich props->max_dm_size = 96124da0016SAriel Levkovich MLX5_CAP_DEV_MEM(mdev, max_memic_size); 96224da0016SAriel Levkovich } 96324da0016SAriel Levkovich 964da6d6ba3SMaor Gottlieb if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS)) 965da6d6ba3SMaor Gottlieb props->device_cap_flags |= IB_DEVICE_MANAGED_FLOW_STEERING; 966da6d6ba3SMaor Gottlieb 967b1383aa6SNoa Osherovich if (MLX5_CAP_GEN(mdev, end_pad)) 968b1383aa6SNoa Osherovich props->device_cap_flags |= IB_DEVICE_PCI_WRITE_END_PADDING; 969b1383aa6SNoa Osherovich 9701b5daf11SMajd Dibbiny props->vendor_part_id = mdev->pdev->device; 9711b5daf11SMajd Dibbiny props->hw_ver = mdev->pdev->revision; 972e126ba97SEli Cohen 973e126ba97SEli Cohen props->max_mr_size = ~0ull; 974e0238a6aSSagi Grimberg props->page_size_cap = ~(min_page_size - 1); 975938fe83cSSaeed Mahameed props->max_qp = 1 << MLX5_CAP_GEN(mdev, log_max_qp); 976938fe83cSSaeed Mahameed props->max_qp_wr = 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz); 977938fe83cSSaeed Mahameed max_rq_sg = MLX5_CAP_GEN(mdev, max_wqe_sz_rq) / 978938fe83cSSaeed Mahameed sizeof(struct mlx5_wqe_data_seg); 979288c01b7SEli Cohen max_sq_desc = min_t(int, MLX5_CAP_GEN(mdev, max_wqe_sz_sq), 512); 980288c01b7SEli Cohen max_sq_sg = (max_sq_desc - sizeof(struct mlx5_wqe_ctrl_seg) - 981288c01b7SEli Cohen sizeof(struct mlx5_wqe_raddr_seg)) / 982e126ba97SEli Cohen sizeof(struct mlx5_wqe_data_seg); 98333023fb8SSteve Wise props->max_send_sge = max_sq_sg; 98433023fb8SSteve Wise props->max_recv_sge = max_rq_sg; 985986ef95eSSagi Grimberg props->max_sge_rd = MLX5_MAX_SGE_RD; 986938fe83cSSaeed Mahameed props->max_cq = 1 << MLX5_CAP_GEN(mdev, log_max_cq); 9879f177686SLeon Romanovsky props->max_cqe = (1 << MLX5_CAP_GEN(mdev, log_max_cq_sz)) - 1; 988938fe83cSSaeed Mahameed props->max_mr = 1 << MLX5_CAP_GEN(mdev, log_max_mkey); 989938fe83cSSaeed Mahameed props->max_pd = 1 << MLX5_CAP_GEN(mdev, log_max_pd); 990938fe83cSSaeed Mahameed props->max_qp_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_req_qp); 991938fe83cSSaeed Mahameed props->max_qp_init_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_res_qp); 992938fe83cSSaeed Mahameed props->max_srq = 1 << MLX5_CAP_GEN(mdev, log_max_srq); 993938fe83cSSaeed Mahameed props->max_srq_wr = (1 << MLX5_CAP_GEN(mdev, log_max_srq_sz)) - 1; 994938fe83cSSaeed Mahameed props->local_ca_ack_delay = MLX5_CAP_GEN(mdev, local_ca_ack_delay); 995e126ba97SEli Cohen props->max_res_rd_atom = props->max_qp_rd_atom * props->max_qp; 996e126ba97SEli Cohen props->max_srq_sge = max_rq_sg - 1; 997911f4331SSagi Grimberg props->max_fast_reg_page_list_len = 998911f4331SSagi Grimberg 1 << MLX5_CAP_GEN(mdev, log_max_klm_list_size); 99962e3c379SMax Gurtovoy props->max_pi_fast_reg_page_list_len = 100062e3c379SMax Gurtovoy props->max_fast_reg_page_list_len / 2; 100136609056SYamin Friedman props->max_sgl_rd = 100236609056SYamin Friedman MLX5_CAP_GEN(mdev, max_sgl_for_optimized_performance); 1003776a3906SMoni Shoua get_atomic_caps_qp(dev, props); 100481bea28fSEli Cohen props->masked_atomic_cap = IB_ATOMIC_NONE; 1005938fe83cSSaeed Mahameed props->max_mcast_grp = 1 << MLX5_CAP_GEN(mdev, log_max_mcg); 1006938fe83cSSaeed Mahameed props->max_mcast_qp_attach = MLX5_CAP_GEN(mdev, max_qp_mcg); 1007e126ba97SEli Cohen props->max_total_mcast_qp_attach = props->max_mcast_qp_attach * 1008e126ba97SEli Cohen props->max_mcast_grp; 1009e126ba97SEli Cohen props->max_map_per_fmr = INT_MAX; /* no limit in ConnectIB */ 101086695a65SMaor Gottlieb props->max_ah = INT_MAX; 10117c60bcbbSMatan Barak props->hca_core_clock = MLX5_CAP_GEN(mdev, device_frequency_khz); 10127c60bcbbSMatan Barak props->timestamp_mask = 0x7FFFFFFFFFFFFFFFULL; 1013e126ba97SEli Cohen 1014e502b8b0SLeon Romanovsky if (IS_ENABLED(CONFIG_INFINIBAND_ON_DEMAND_PAGING)) { 101500815752SMoni Shoua if (dev->odp_caps.general_caps & IB_ODP_SUPPORT) 10168cdd312cSHaggai Eran props->device_cap_flags |= IB_DEVICE_ON_DEMAND_PAGING; 10178cdd312cSHaggai Eran props->odp_caps = dev->odp_caps; 1018a73a8955SMoni Shoua if (!uhw) { 1019a73a8955SMoni Shoua /* ODP for kernel QPs is not implemented for receive 1020a73a8955SMoni Shoua * WQEs and SRQ WQEs 1021a73a8955SMoni Shoua */ 1022a73a8955SMoni Shoua props->odp_caps.per_transport_caps.rc_odp_caps &= 1023a73a8955SMoni Shoua ~(IB_ODP_SUPPORT_READ | 1024a73a8955SMoni Shoua IB_ODP_SUPPORT_SRQ_RECV); 1025a73a8955SMoni Shoua props->odp_caps.per_transport_caps.uc_odp_caps &= 1026a73a8955SMoni Shoua ~(IB_ODP_SUPPORT_READ | 1027a73a8955SMoni Shoua IB_ODP_SUPPORT_SRQ_RECV); 1028a73a8955SMoni Shoua props->odp_caps.per_transport_caps.ud_odp_caps &= 1029a73a8955SMoni Shoua ~(IB_ODP_SUPPORT_READ | 1030a73a8955SMoni Shoua IB_ODP_SUPPORT_SRQ_RECV); 1031a73a8955SMoni Shoua props->odp_caps.per_transport_caps.xrc_odp_caps &= 1032a73a8955SMoni Shoua ~(IB_ODP_SUPPORT_READ | 1033a73a8955SMoni Shoua IB_ODP_SUPPORT_SRQ_RECV); 1034a73a8955SMoni Shoua } 1035e502b8b0SLeon Romanovsky } 10368cdd312cSHaggai Eran 1037051f2630SLeon Romanovsky if (MLX5_CAP_GEN(mdev, cd)) 1038051f2630SLeon Romanovsky props->device_cap_flags |= IB_DEVICE_CROSS_CHANNEL; 1039051f2630SLeon Romanovsky 1040e53a9d26SParav Pandit if (mlx5_core_is_vf(mdev)) 1041eff901d3SEli Cohen props->device_cap_flags |= IB_DEVICE_VIRTUAL_FUNCTION; 1042eff901d3SEli Cohen 104331f69a82SYishai Hadas if (mlx5_ib_port_link_layer(ibdev, 1) == 104485c7c014SDaniel Jurgens IB_LINK_LAYER_ETHERNET && raw_support) { 104531f69a82SYishai Hadas props->rss_caps.max_rwq_indirection_tables = 104631f69a82SYishai Hadas 1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt); 104731f69a82SYishai Hadas props->rss_caps.max_rwq_indirection_table_size = 104831f69a82SYishai Hadas 1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt_size); 104931f69a82SYishai Hadas props->rss_caps.supported_qpts = 1 << IB_QPT_RAW_PACKET; 105031f69a82SYishai Hadas props->max_wq_type_rq = 105131f69a82SYishai Hadas 1 << MLX5_CAP_GEN(dev->mdev, log_max_rq); 105231f69a82SYishai Hadas } 105331f69a82SYishai Hadas 1054eb761894SArtemy Kovalyov if (MLX5_CAP_GEN(mdev, tag_matching)) { 105578b1beb0SLeon Romanovsky props->tm_caps.max_num_tags = 1056eb761894SArtemy Kovalyov (1 << MLX5_CAP_GEN(mdev, log_tag_matching_list_sz)) - 1; 105778b1beb0SLeon Romanovsky props->tm_caps.max_ops = 1058eb761894SArtemy Kovalyov 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz); 105978b1beb0SLeon Romanovsky props->tm_caps.max_sge = MLX5_TM_MAX_SGE; 1060eb761894SArtemy Kovalyov } 1061eb761894SArtemy Kovalyov 106289705e92SDanit Goldberg if (MLX5_CAP_GEN(mdev, tag_matching) && 106389705e92SDanit Goldberg MLX5_CAP_GEN(mdev, rndv_offload_rc)) { 106489705e92SDanit Goldberg props->tm_caps.flags = IB_TM_CAP_RNDV_RC; 106589705e92SDanit Goldberg props->tm_caps.max_rndv_hdr_size = MLX5_TM_MAX_RNDV_MSG_SIZE; 106689705e92SDanit Goldberg } 106789705e92SDanit Goldberg 106887ab3f52SYonatan Cohen if (MLX5_CAP_GEN(dev->mdev, cq_moderation)) { 106987ab3f52SYonatan Cohen props->cq_caps.max_cq_moderation_count = 107087ab3f52SYonatan Cohen MLX5_MAX_CQ_COUNT; 107187ab3f52SYonatan Cohen props->cq_caps.max_cq_moderation_period = 107287ab3f52SYonatan Cohen MLX5_MAX_CQ_PERIOD; 107387ab3f52SYonatan Cohen } 107487ab3f52SYonatan Cohen 107548357091SLeon Romanovsky if (field_avail(typeof(resp), cqe_comp_caps, uhw_outlen)) { 1076572f46bfSYonatan Cohen resp.response_length += sizeof(resp.cqe_comp_caps); 1077572f46bfSYonatan Cohen 1078572f46bfSYonatan Cohen if (MLX5_CAP_GEN(dev->mdev, cqe_compression)) { 10797e43a2a5SBodong Wang resp.cqe_comp_caps.max_num = 1080572f46bfSYonatan Cohen MLX5_CAP_GEN(dev->mdev, 1081572f46bfSYonatan Cohen cqe_compression_max_num); 1082572f46bfSYonatan Cohen 10837e43a2a5SBodong Wang resp.cqe_comp_caps.supported_format = 10847e43a2a5SBodong Wang MLX5_IB_CQE_RES_FORMAT_HASH | 10857e43a2a5SBodong Wang MLX5_IB_CQE_RES_FORMAT_CSUM; 10866f1006a4SYonatan Cohen 10876f1006a4SYonatan Cohen if (MLX5_CAP_GEN(dev->mdev, mini_cqe_resp_stride_index)) 10886f1006a4SYonatan Cohen resp.cqe_comp_caps.supported_format |= 10896f1006a4SYonatan Cohen MLX5_IB_CQE_RES_FORMAT_CSUM_STRIDX; 1090572f46bfSYonatan Cohen } 10917e43a2a5SBodong Wang } 10927e43a2a5SBodong Wang 109348357091SLeon Romanovsky if (field_avail(typeof(resp), packet_pacing_caps, uhw_outlen) && 109485c7c014SDaniel Jurgens raw_support) { 1095d949167dSBodong Wang if (MLX5_CAP_QOS(mdev, packet_pacing) && 1096d949167dSBodong Wang MLX5_CAP_GEN(mdev, qos)) { 1097d949167dSBodong Wang resp.packet_pacing_caps.qp_rate_limit_max = 1098d949167dSBodong Wang MLX5_CAP_QOS(mdev, packet_pacing_max_rate); 1099d949167dSBodong Wang resp.packet_pacing_caps.qp_rate_limit_min = 1100d949167dSBodong Wang MLX5_CAP_QOS(mdev, packet_pacing_min_rate); 1101d949167dSBodong Wang resp.packet_pacing_caps.supported_qpts |= 1102d949167dSBodong Wang 1 << IB_QPT_RAW_PACKET; 110361147f39SBodong Wang if (MLX5_CAP_QOS(mdev, packet_pacing_burst_bound) && 110461147f39SBodong Wang MLX5_CAP_QOS(mdev, packet_pacing_typical_size)) 110561147f39SBodong Wang resp.packet_pacing_caps.cap_flags |= 110661147f39SBodong Wang MLX5_IB_PP_SUPPORT_BURST; 1107d949167dSBodong Wang } 1108d949167dSBodong Wang resp.response_length += sizeof(resp.packet_pacing_caps); 1109d949167dSBodong Wang } 1110d949167dSBodong Wang 11119f885201SLeon Romanovsky if (field_avail(typeof(resp), mlx5_ib_support_multi_pkt_send_wqes, 111248357091SLeon Romanovsky uhw_outlen)) { 1113795b609cSBodong Wang if (MLX5_CAP_ETH(mdev, multi_pkt_send_wqe)) 11149f885201SLeon Romanovsky resp.mlx5_ib_support_multi_pkt_send_wqes = 1115795b609cSBodong Wang MLX5_IB_ALLOW_MPW; 1116050da902SBodong Wang 1117050da902SBodong Wang if (MLX5_CAP_ETH(mdev, enhanced_multi_pkt_send_wqe)) 1118050da902SBodong Wang resp.mlx5_ib_support_multi_pkt_send_wqes |= 1119050da902SBodong Wang MLX5_IB_SUPPORT_EMPW; 1120050da902SBodong Wang 11219f885201SLeon Romanovsky resp.response_length += 11229f885201SLeon Romanovsky sizeof(resp.mlx5_ib_support_multi_pkt_send_wqes); 11239f885201SLeon Romanovsky } 11249f885201SLeon Romanovsky 112548357091SLeon Romanovsky if (field_avail(typeof(resp), flags, uhw_outlen)) { 1126de57f2adSGuy Levi resp.response_length += sizeof(resp.flags); 11277a0c8f42SGuy Levi 1128de57f2adSGuy Levi if (MLX5_CAP_GEN(mdev, cqe_compression_128)) 1129de57f2adSGuy Levi resp.flags |= 1130de57f2adSGuy Levi MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_COMP; 11317a0c8f42SGuy Levi 11327a0c8f42SGuy Levi if (MLX5_CAP_GEN(mdev, cqe_128_always)) 11337a0c8f42SGuy Levi resp.flags |= MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_PAD; 11347e11b911SDanit Goldberg if (MLX5_CAP_GEN(mdev, qp_packet_based)) 11357e11b911SDanit Goldberg resp.flags |= 11367e11b911SDanit Goldberg MLX5_IB_QUERY_DEV_RESP_PACKET_BASED_CREDIT_MODE; 11377249c8eaSGuy Levi 11387249c8eaSGuy Levi resp.flags |= MLX5_IB_QUERY_DEV_RESP_FLAGS_SCAT2CQE_DCT; 1139de57f2adSGuy Levi } 11409f885201SLeon Romanovsky 114148357091SLeon Romanovsky if (field_avail(typeof(resp), sw_parsing_caps, uhw_outlen)) { 114296dc3fc5SNoa Osherovich resp.response_length += sizeof(resp.sw_parsing_caps); 114396dc3fc5SNoa Osherovich if (MLX5_CAP_ETH(mdev, swp)) { 114496dc3fc5SNoa Osherovich resp.sw_parsing_caps.sw_parsing_offloads |= 114596dc3fc5SNoa Osherovich MLX5_IB_SW_PARSING; 114696dc3fc5SNoa Osherovich 114796dc3fc5SNoa Osherovich if (MLX5_CAP_ETH(mdev, swp_csum)) 114896dc3fc5SNoa Osherovich resp.sw_parsing_caps.sw_parsing_offloads |= 114996dc3fc5SNoa Osherovich MLX5_IB_SW_PARSING_CSUM; 115096dc3fc5SNoa Osherovich 115196dc3fc5SNoa Osherovich if (MLX5_CAP_ETH(mdev, swp_lso)) 115296dc3fc5SNoa Osherovich resp.sw_parsing_caps.sw_parsing_offloads |= 115396dc3fc5SNoa Osherovich MLX5_IB_SW_PARSING_LSO; 115496dc3fc5SNoa Osherovich 115596dc3fc5SNoa Osherovich if (resp.sw_parsing_caps.sw_parsing_offloads) 115696dc3fc5SNoa Osherovich resp.sw_parsing_caps.supported_qpts = 115796dc3fc5SNoa Osherovich BIT(IB_QPT_RAW_PACKET); 115896dc3fc5SNoa Osherovich } 115996dc3fc5SNoa Osherovich } 116096dc3fc5SNoa Osherovich 116148357091SLeon Romanovsky if (field_avail(typeof(resp), striding_rq_caps, uhw_outlen) && 116285c7c014SDaniel Jurgens raw_support) { 1163b4f34597SNoa Osherovich resp.response_length += sizeof(resp.striding_rq_caps); 1164b4f34597SNoa Osherovich if (MLX5_CAP_GEN(mdev, striding_rq)) { 1165b4f34597SNoa Osherovich resp.striding_rq_caps.min_single_stride_log_num_of_bytes = 1166b4f34597SNoa Osherovich MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES; 1167b4f34597SNoa Osherovich resp.striding_rq_caps.max_single_stride_log_num_of_bytes = 1168b4f34597SNoa Osherovich MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES; 1169c16339b6SMark Zhang if (MLX5_CAP_GEN(dev->mdev, ext_stride_num_range)) 1170c16339b6SMark Zhang resp.striding_rq_caps 1171c16339b6SMark Zhang .min_single_wqe_log_num_of_strides = 1172c16339b6SMark Zhang MLX5_EXT_MIN_SINGLE_WQE_LOG_NUM_STRIDES; 1173c16339b6SMark Zhang else 1174c16339b6SMark Zhang resp.striding_rq_caps 1175c16339b6SMark Zhang .min_single_wqe_log_num_of_strides = 1176b4f34597SNoa Osherovich MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES; 1177b4f34597SNoa Osherovich resp.striding_rq_caps.max_single_wqe_log_num_of_strides = 1178b4f34597SNoa Osherovich MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES; 1179b4f34597SNoa Osherovich resp.striding_rq_caps.supported_qpts = 1180b4f34597SNoa Osherovich BIT(IB_QPT_RAW_PACKET); 1181b4f34597SNoa Osherovich } 1182b4f34597SNoa Osherovich } 1183b4f34597SNoa Osherovich 118448357091SLeon Romanovsky if (field_avail(typeof(resp), tunnel_offloads_caps, uhw_outlen)) { 1185f95ef6cbSMaor Gottlieb resp.response_length += sizeof(resp.tunnel_offloads_caps); 1186f95ef6cbSMaor Gottlieb if (MLX5_CAP_ETH(mdev, tunnel_stateless_vxlan)) 1187f95ef6cbSMaor Gottlieb resp.tunnel_offloads_caps |= 1188f95ef6cbSMaor Gottlieb MLX5_IB_TUNNELED_OFFLOADS_VXLAN; 1189f95ef6cbSMaor Gottlieb if (MLX5_CAP_ETH(mdev, tunnel_stateless_geneve_rx)) 1190f95ef6cbSMaor Gottlieb resp.tunnel_offloads_caps |= 1191f95ef6cbSMaor Gottlieb MLX5_IB_TUNNELED_OFFLOADS_GENEVE; 1192f95ef6cbSMaor Gottlieb if (MLX5_CAP_ETH(mdev, tunnel_stateless_gre)) 1193f95ef6cbSMaor Gottlieb resp.tunnel_offloads_caps |= 1194f95ef6cbSMaor Gottlieb MLX5_IB_TUNNELED_OFFLOADS_GRE; 1195e818e255SAriel Levkovich if (MLX5_CAP_GEN(mdev, flex_parser_protocols) & 1196e818e255SAriel Levkovich MLX5_FLEX_PROTO_CW_MPLS_GRE) 1197e818e255SAriel Levkovich resp.tunnel_offloads_caps |= 1198e818e255SAriel Levkovich MLX5_IB_TUNNELED_OFFLOADS_MPLS_GRE; 1199e818e255SAriel Levkovich if (MLX5_CAP_GEN(mdev, flex_parser_protocols) & 1200e818e255SAriel Levkovich MLX5_FLEX_PROTO_CW_MPLS_UDP) 1201e818e255SAriel Levkovich resp.tunnel_offloads_caps |= 1202e818e255SAriel Levkovich MLX5_IB_TUNNELED_OFFLOADS_MPLS_UDP; 1203f95ef6cbSMaor Gottlieb } 1204f95ef6cbSMaor Gottlieb 120548357091SLeon Romanovsky if (uhw_outlen) { 1206402ca536SBodong Wang err = ib_copy_to_udata(uhw, &resp, resp.response_length); 1207402ca536SBodong Wang 1208402ca536SBodong Wang if (err) 1209402ca536SBodong Wang return err; 1210402ca536SBodong Wang } 1211402ca536SBodong Wang 12121b5daf11SMajd Dibbiny return 0; 12131b5daf11SMajd Dibbiny } 1214e126ba97SEli Cohen 12151b5daf11SMajd Dibbiny enum mlx5_ib_width { 12161b5daf11SMajd Dibbiny MLX5_IB_WIDTH_1X = 1 << 0, 12171b5daf11SMajd Dibbiny MLX5_IB_WIDTH_2X = 1 << 1, 12181b5daf11SMajd Dibbiny MLX5_IB_WIDTH_4X = 1 << 2, 12191b5daf11SMajd Dibbiny MLX5_IB_WIDTH_8X = 1 << 3, 12201b5daf11SMajd Dibbiny MLX5_IB_WIDTH_12X = 1 << 4 12211b5daf11SMajd Dibbiny }; 12221b5daf11SMajd Dibbiny 1223db7a691aSMichael Guralnik static void translate_active_width(struct ib_device *ibdev, u8 active_width, 12241b5daf11SMajd Dibbiny u8 *ib_width) 12251b5daf11SMajd Dibbiny { 12261b5daf11SMajd Dibbiny struct mlx5_ib_dev *dev = to_mdev(ibdev); 12271b5daf11SMajd Dibbiny 1228db7a691aSMichael Guralnik if (active_width & MLX5_IB_WIDTH_1X) 12291b5daf11SMajd Dibbiny *ib_width = IB_WIDTH_1X; 1230d764970bSMichael Guralnik else if (active_width & MLX5_IB_WIDTH_2X) 1231d764970bSMichael Guralnik *ib_width = IB_WIDTH_2X; 1232db7a691aSMichael Guralnik else if (active_width & MLX5_IB_WIDTH_4X) 12331b5daf11SMajd Dibbiny *ib_width = IB_WIDTH_4X; 1234db7a691aSMichael Guralnik else if (active_width & MLX5_IB_WIDTH_8X) 12351b5daf11SMajd Dibbiny *ib_width = IB_WIDTH_8X; 1236db7a691aSMichael Guralnik else if (active_width & MLX5_IB_WIDTH_12X) 12371b5daf11SMajd Dibbiny *ib_width = IB_WIDTH_12X; 1238db7a691aSMichael Guralnik else { 1239db7a691aSMichael Guralnik mlx5_ib_dbg(dev, "Invalid active_width %d, setting width to default value: 4x\n", 12401b5daf11SMajd Dibbiny (int)active_width); 1241db7a691aSMichael Guralnik *ib_width = IB_WIDTH_4X; 12421b5daf11SMajd Dibbiny } 12431b5daf11SMajd Dibbiny 1244db7a691aSMichael Guralnik return; 12451b5daf11SMajd Dibbiny } 12461b5daf11SMajd Dibbiny 12471b5daf11SMajd Dibbiny static int mlx5_mtu_to_ib_mtu(int mtu) 12481b5daf11SMajd Dibbiny { 12491b5daf11SMajd Dibbiny switch (mtu) { 12501b5daf11SMajd Dibbiny case 256: return 1; 12511b5daf11SMajd Dibbiny case 512: return 2; 12521b5daf11SMajd Dibbiny case 1024: return 3; 12531b5daf11SMajd Dibbiny case 2048: return 4; 12541b5daf11SMajd Dibbiny case 4096: return 5; 12551b5daf11SMajd Dibbiny default: 12561b5daf11SMajd Dibbiny pr_warn("invalid mtu\n"); 12571b5daf11SMajd Dibbiny return -1; 12581b5daf11SMajd Dibbiny } 12591b5daf11SMajd Dibbiny } 12601b5daf11SMajd Dibbiny 12611b5daf11SMajd Dibbiny enum ib_max_vl_num { 12621b5daf11SMajd Dibbiny __IB_MAX_VL_0 = 1, 12631b5daf11SMajd Dibbiny __IB_MAX_VL_0_1 = 2, 12641b5daf11SMajd Dibbiny __IB_MAX_VL_0_3 = 3, 12651b5daf11SMajd Dibbiny __IB_MAX_VL_0_7 = 4, 12661b5daf11SMajd Dibbiny __IB_MAX_VL_0_14 = 5, 12671b5daf11SMajd Dibbiny }; 12681b5daf11SMajd Dibbiny 12691b5daf11SMajd Dibbiny enum mlx5_vl_hw_cap { 12701b5daf11SMajd Dibbiny MLX5_VL_HW_0 = 1, 12711b5daf11SMajd Dibbiny MLX5_VL_HW_0_1 = 2, 12721b5daf11SMajd Dibbiny MLX5_VL_HW_0_2 = 3, 12731b5daf11SMajd Dibbiny MLX5_VL_HW_0_3 = 4, 12741b5daf11SMajd Dibbiny MLX5_VL_HW_0_4 = 5, 12751b5daf11SMajd Dibbiny MLX5_VL_HW_0_5 = 6, 12761b5daf11SMajd Dibbiny MLX5_VL_HW_0_6 = 7, 12771b5daf11SMajd Dibbiny MLX5_VL_HW_0_7 = 8, 12781b5daf11SMajd Dibbiny MLX5_VL_HW_0_14 = 15 12791b5daf11SMajd Dibbiny }; 12801b5daf11SMajd Dibbiny 12811b5daf11SMajd Dibbiny static int translate_max_vl_num(struct ib_device *ibdev, u8 vl_hw_cap, 12821b5daf11SMajd Dibbiny u8 *max_vl_num) 12831b5daf11SMajd Dibbiny { 12841b5daf11SMajd Dibbiny switch (vl_hw_cap) { 12851b5daf11SMajd Dibbiny case MLX5_VL_HW_0: 12861b5daf11SMajd Dibbiny *max_vl_num = __IB_MAX_VL_0; 12871b5daf11SMajd Dibbiny break; 12881b5daf11SMajd Dibbiny case MLX5_VL_HW_0_1: 12891b5daf11SMajd Dibbiny *max_vl_num = __IB_MAX_VL_0_1; 12901b5daf11SMajd Dibbiny break; 12911b5daf11SMajd Dibbiny case MLX5_VL_HW_0_3: 12921b5daf11SMajd Dibbiny *max_vl_num = __IB_MAX_VL_0_3; 12931b5daf11SMajd Dibbiny break; 12941b5daf11SMajd Dibbiny case MLX5_VL_HW_0_7: 12951b5daf11SMajd Dibbiny *max_vl_num = __IB_MAX_VL_0_7; 12961b5daf11SMajd Dibbiny break; 12971b5daf11SMajd Dibbiny case MLX5_VL_HW_0_14: 12981b5daf11SMajd Dibbiny *max_vl_num = __IB_MAX_VL_0_14; 12991b5daf11SMajd Dibbiny break; 13001b5daf11SMajd Dibbiny 13011b5daf11SMajd Dibbiny default: 13021b5daf11SMajd Dibbiny return -EINVAL; 13031b5daf11SMajd Dibbiny } 13041b5daf11SMajd Dibbiny 13051b5daf11SMajd Dibbiny return 0; 13061b5daf11SMajd Dibbiny } 13071b5daf11SMajd Dibbiny 13081b5daf11SMajd Dibbiny static int mlx5_query_hca_port(struct ib_device *ibdev, u8 port, 13091b5daf11SMajd Dibbiny struct ib_port_attr *props) 13101b5daf11SMajd Dibbiny { 13111b5daf11SMajd Dibbiny struct mlx5_ib_dev *dev = to_mdev(ibdev); 13121b5daf11SMajd Dibbiny struct mlx5_core_dev *mdev = dev->mdev; 13131b5daf11SMajd Dibbiny struct mlx5_hca_vport_context *rep; 1314046339eaSSaeed Mahameed u16 max_mtu; 1315046339eaSSaeed Mahameed u16 oper_mtu; 13161b5daf11SMajd Dibbiny int err; 13171b5daf11SMajd Dibbiny u8 ib_link_width_oper; 13181b5daf11SMajd Dibbiny u8 vl_hw_cap; 13191b5daf11SMajd Dibbiny 13201b5daf11SMajd Dibbiny rep = kzalloc(sizeof(*rep), GFP_KERNEL); 13211b5daf11SMajd Dibbiny if (!rep) { 13221b5daf11SMajd Dibbiny err = -ENOMEM; 13231b5daf11SMajd Dibbiny goto out; 13241b5daf11SMajd Dibbiny } 13251b5daf11SMajd Dibbiny 1326c4550c63SOr Gerlitz /* props being zeroed by the caller, avoid zeroing it here */ 13271b5daf11SMajd Dibbiny 13281b5daf11SMajd Dibbiny err = mlx5_query_hca_vport_context(mdev, 0, port, 0, rep); 13291b5daf11SMajd Dibbiny if (err) 13301b5daf11SMajd Dibbiny goto out; 13311b5daf11SMajd Dibbiny 13321b5daf11SMajd Dibbiny props->lid = rep->lid; 13331b5daf11SMajd Dibbiny props->lmc = rep->lmc; 13341b5daf11SMajd Dibbiny props->sm_lid = rep->sm_lid; 13351b5daf11SMajd Dibbiny props->sm_sl = rep->sm_sl; 13361b5daf11SMajd Dibbiny props->state = rep->vport_state; 13371b5daf11SMajd Dibbiny props->phys_state = rep->port_physical_state; 13381b5daf11SMajd Dibbiny props->port_cap_flags = rep->cap_mask1; 13391b5daf11SMajd Dibbiny props->gid_tbl_len = mlx5_get_gid_table_len(MLX5_CAP_GEN(mdev, gid_table_size)); 13401b5daf11SMajd Dibbiny props->max_msg_sz = 1 << MLX5_CAP_GEN(mdev, log_max_msg); 13411b5daf11SMajd Dibbiny props->pkey_tbl_len = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, pkey_table_size)); 13421b5daf11SMajd Dibbiny props->bad_pkey_cntr = rep->pkey_violation_counter; 13431b5daf11SMajd Dibbiny props->qkey_viol_cntr = rep->qkey_violation_counter; 13441b5daf11SMajd Dibbiny props->subnet_timeout = rep->subnet_timeout; 13451b5daf11SMajd Dibbiny props->init_type_reply = rep->init_type_reply; 13461b5daf11SMajd Dibbiny 13474106a758SMichael Guralnik if (props->port_cap_flags & IB_PORT_CAP_MASK2_SUP) 13484106a758SMichael Guralnik props->port_cap_flags2 = rep->cap_mask2; 13494106a758SMichael Guralnik 13501b5daf11SMajd Dibbiny err = mlx5_query_port_link_width_oper(mdev, &ib_link_width_oper, port); 13511b5daf11SMajd Dibbiny if (err) 13521b5daf11SMajd Dibbiny goto out; 13531b5daf11SMajd Dibbiny 1354db7a691aSMichael Guralnik translate_active_width(ibdev, ib_link_width_oper, &props->active_width); 1355db7a691aSMichael Guralnik 1356d5beb7f2SNoa Osherovich err = mlx5_query_port_ib_proto_oper(mdev, &props->active_speed, port); 13571b5daf11SMajd Dibbiny if (err) 13581b5daf11SMajd Dibbiny goto out; 13591b5daf11SMajd Dibbiny 1360facc9699SSaeed Mahameed mlx5_query_port_max_mtu(mdev, &max_mtu, port); 13611b5daf11SMajd Dibbiny 13621b5daf11SMajd Dibbiny props->max_mtu = mlx5_mtu_to_ib_mtu(max_mtu); 13631b5daf11SMajd Dibbiny 1364facc9699SSaeed Mahameed mlx5_query_port_oper_mtu(mdev, &oper_mtu, port); 13651b5daf11SMajd Dibbiny 13661b5daf11SMajd Dibbiny props->active_mtu = mlx5_mtu_to_ib_mtu(oper_mtu); 13671b5daf11SMajd Dibbiny 13681b5daf11SMajd Dibbiny err = mlx5_query_port_vl_hw_cap(mdev, &vl_hw_cap, port); 13691b5daf11SMajd Dibbiny if (err) 13701b5daf11SMajd Dibbiny goto out; 13711b5daf11SMajd Dibbiny 13721b5daf11SMajd Dibbiny err = translate_max_vl_num(ibdev, vl_hw_cap, 13731b5daf11SMajd Dibbiny &props->max_vl_num); 13741b5daf11SMajd Dibbiny out: 13751b5daf11SMajd Dibbiny kfree(rep); 1376e126ba97SEli Cohen return err; 1377e126ba97SEli Cohen } 1378e126ba97SEli Cohen 1379e126ba97SEli Cohen int mlx5_ib_query_port(struct ib_device *ibdev, u8 port, 1380e126ba97SEli Cohen struct ib_port_attr *props) 1381e126ba97SEli Cohen { 1382095b0927SIlan Tayari unsigned int count; 1383095b0927SIlan Tayari int ret; 1384095b0927SIlan Tayari 13851b5daf11SMajd Dibbiny switch (mlx5_get_vport_access_method(ibdev)) { 13861b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_MAD: 1387095b0927SIlan Tayari ret = mlx5_query_mad_ifc_port(ibdev, port, props); 1388095b0927SIlan Tayari break; 1389e126ba97SEli Cohen 13901b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_HCA: 1391095b0927SIlan Tayari ret = mlx5_query_hca_port(ibdev, port, props); 1392095b0927SIlan Tayari break; 13931b5daf11SMajd Dibbiny 13943f89a643SAchiad Shochat case MLX5_VPORT_ACCESS_METHOD_NIC: 1395095b0927SIlan Tayari ret = mlx5_query_port_roce(ibdev, port, props); 1396095b0927SIlan Tayari break; 13973f89a643SAchiad Shochat 13981b5daf11SMajd Dibbiny default: 1399095b0927SIlan Tayari ret = -EINVAL; 1400e126ba97SEli Cohen } 1401095b0927SIlan Tayari 1402095b0927SIlan Tayari if (!ret && props) { 1403b3cbd6f0SDaniel Jurgens struct mlx5_ib_dev *dev = to_mdev(ibdev); 1404b3cbd6f0SDaniel Jurgens struct mlx5_core_dev *mdev; 1405b3cbd6f0SDaniel Jurgens bool put_mdev = true; 1406b3cbd6f0SDaniel Jurgens 1407b3cbd6f0SDaniel Jurgens mdev = mlx5_ib_get_native_port_mdev(dev, port, NULL); 1408b3cbd6f0SDaniel Jurgens if (!mdev) { 1409b3cbd6f0SDaniel Jurgens /* If the port isn't affiliated yet query the master. 1410b3cbd6f0SDaniel Jurgens * The master and slave will have the same values. 1411b3cbd6f0SDaniel Jurgens */ 1412b3cbd6f0SDaniel Jurgens mdev = dev->mdev; 1413b3cbd6f0SDaniel Jurgens port = 1; 1414b3cbd6f0SDaniel Jurgens put_mdev = false; 1415b3cbd6f0SDaniel Jurgens } 1416b3cbd6f0SDaniel Jurgens count = mlx5_core_reserved_gids_count(mdev); 1417b3cbd6f0SDaniel Jurgens if (put_mdev) 1418b3cbd6f0SDaniel Jurgens mlx5_ib_put_native_port_mdev(dev, port); 1419095b0927SIlan Tayari props->gid_tbl_len -= count; 1420095b0927SIlan Tayari } 1421095b0927SIlan Tayari return ret; 1422e126ba97SEli Cohen } 1423e126ba97SEli Cohen 14248e6efa3aSMark Bloch static int mlx5_ib_rep_query_port(struct ib_device *ibdev, u8 port, 14258e6efa3aSMark Bloch struct ib_port_attr *props) 14268e6efa3aSMark Bloch { 14278e6efa3aSMark Bloch int ret; 14288e6efa3aSMark Bloch 142926628e2dSMark Bloch /* Only link layer == ethernet is valid for representors 143026628e2dSMark Bloch * and we always use port 1 143126628e2dSMark Bloch */ 14328e6efa3aSMark Bloch ret = mlx5_query_port_roce(ibdev, port, props); 14338e6efa3aSMark Bloch if (ret || !props) 14348e6efa3aSMark Bloch return ret; 14358e6efa3aSMark Bloch 14368e6efa3aSMark Bloch /* We don't support GIDS */ 14378e6efa3aSMark Bloch props->gid_tbl_len = 0; 14388e6efa3aSMark Bloch 14398e6efa3aSMark Bloch return ret; 14408e6efa3aSMark Bloch } 14418e6efa3aSMark Bloch 1442e126ba97SEli Cohen static int mlx5_ib_query_gid(struct ib_device *ibdev, u8 port, int index, 1443e126ba97SEli Cohen union ib_gid *gid) 1444e126ba97SEli Cohen { 14451b5daf11SMajd Dibbiny struct mlx5_ib_dev *dev = to_mdev(ibdev); 14461b5daf11SMajd Dibbiny struct mlx5_core_dev *mdev = dev->mdev; 1447e126ba97SEli Cohen 14481b5daf11SMajd Dibbiny switch (mlx5_get_vport_access_method(ibdev)) { 14491b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_MAD: 14501b5daf11SMajd Dibbiny return mlx5_query_mad_ifc_gids(ibdev, port, index, gid); 1451e126ba97SEli Cohen 14521b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_HCA: 14531b5daf11SMajd Dibbiny return mlx5_query_hca_vport_gid(mdev, 0, port, 0, index, gid); 1454e126ba97SEli Cohen 14551b5daf11SMajd Dibbiny default: 14561b5daf11SMajd Dibbiny return -EINVAL; 14571b5daf11SMajd Dibbiny } 1458e126ba97SEli Cohen 1459e126ba97SEli Cohen } 1460e126ba97SEli Cohen 1461b3cbd6f0SDaniel Jurgens static int mlx5_query_hca_nic_pkey(struct ib_device *ibdev, u8 port, 1462b3cbd6f0SDaniel Jurgens u16 index, u16 *pkey) 1463b3cbd6f0SDaniel Jurgens { 1464b3cbd6f0SDaniel Jurgens struct mlx5_ib_dev *dev = to_mdev(ibdev); 1465b3cbd6f0SDaniel Jurgens struct mlx5_core_dev *mdev; 1466b3cbd6f0SDaniel Jurgens bool put_mdev = true; 1467b3cbd6f0SDaniel Jurgens u8 mdev_port_num; 1468b3cbd6f0SDaniel Jurgens int err; 1469b3cbd6f0SDaniel Jurgens 1470b3cbd6f0SDaniel Jurgens mdev = mlx5_ib_get_native_port_mdev(dev, port, &mdev_port_num); 1471b3cbd6f0SDaniel Jurgens if (!mdev) { 1472b3cbd6f0SDaniel Jurgens /* The port isn't affiliated yet, get the PKey from the master 1473b3cbd6f0SDaniel Jurgens * port. For RoCE the PKey tables will be the same. 1474b3cbd6f0SDaniel Jurgens */ 1475b3cbd6f0SDaniel Jurgens put_mdev = false; 1476b3cbd6f0SDaniel Jurgens mdev = dev->mdev; 1477b3cbd6f0SDaniel Jurgens mdev_port_num = 1; 1478b3cbd6f0SDaniel Jurgens } 1479b3cbd6f0SDaniel Jurgens 1480b3cbd6f0SDaniel Jurgens err = mlx5_query_hca_vport_pkey(mdev, 0, mdev_port_num, 0, 1481b3cbd6f0SDaniel Jurgens index, pkey); 1482b3cbd6f0SDaniel Jurgens if (put_mdev) 1483b3cbd6f0SDaniel Jurgens mlx5_ib_put_native_port_mdev(dev, port); 1484b3cbd6f0SDaniel Jurgens 1485b3cbd6f0SDaniel Jurgens return err; 1486b3cbd6f0SDaniel Jurgens } 1487b3cbd6f0SDaniel Jurgens 1488e126ba97SEli Cohen static int mlx5_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index, 1489e126ba97SEli Cohen u16 *pkey) 1490e126ba97SEli Cohen { 14911b5daf11SMajd Dibbiny switch (mlx5_get_vport_access_method(ibdev)) { 14921b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_MAD: 14931b5daf11SMajd Dibbiny return mlx5_query_mad_ifc_pkey(ibdev, port, index, pkey); 1494e126ba97SEli Cohen 14951b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_HCA: 14961b5daf11SMajd Dibbiny case MLX5_VPORT_ACCESS_METHOD_NIC: 1497b3cbd6f0SDaniel Jurgens return mlx5_query_hca_nic_pkey(ibdev, port, index, pkey); 14981b5daf11SMajd Dibbiny default: 14991b5daf11SMajd Dibbiny return -EINVAL; 1500e126ba97SEli Cohen } 15011b5daf11SMajd Dibbiny } 1502e126ba97SEli Cohen 1503e126ba97SEli Cohen static int mlx5_ib_modify_device(struct ib_device *ibdev, int mask, 1504e126ba97SEli Cohen struct ib_device_modify *props) 1505e126ba97SEli Cohen { 1506e126ba97SEli Cohen struct mlx5_ib_dev *dev = to_mdev(ibdev); 1507e126ba97SEli Cohen struct mlx5_reg_node_desc in; 1508e126ba97SEli Cohen struct mlx5_reg_node_desc out; 1509e126ba97SEli Cohen int err; 1510e126ba97SEli Cohen 1511e126ba97SEli Cohen if (mask & ~IB_DEVICE_MODIFY_NODE_DESC) 1512e126ba97SEli Cohen return -EOPNOTSUPP; 1513e126ba97SEli Cohen 1514e126ba97SEli Cohen if (!(mask & IB_DEVICE_MODIFY_NODE_DESC)) 1515e126ba97SEli Cohen return 0; 1516e126ba97SEli Cohen 1517e126ba97SEli Cohen /* 1518e126ba97SEli Cohen * If possible, pass node desc to FW, so it can generate 1519e126ba97SEli Cohen * a 144 trap. If cmd fails, just ignore. 1520e126ba97SEli Cohen */ 1521bd99fdeaSYuval Shaia memcpy(&in, props->node_desc, IB_DEVICE_NODE_DESC_MAX); 15229603b61dSJack Morgenstein err = mlx5_core_access_reg(dev->mdev, &in, sizeof(in), &out, 1523e126ba97SEli Cohen sizeof(out), MLX5_REG_NODE_DESC, 0, 1); 1524e126ba97SEli Cohen if (err) 1525e126ba97SEli Cohen return err; 1526e126ba97SEli Cohen 1527bd99fdeaSYuval Shaia memcpy(ibdev->node_desc, props->node_desc, IB_DEVICE_NODE_DESC_MAX); 1528e126ba97SEli Cohen 1529e126ba97SEli Cohen return err; 1530e126ba97SEli Cohen } 1531e126ba97SEli Cohen 1532cdbe33d0SEli Cohen static int set_port_caps_atomic(struct mlx5_ib_dev *dev, u8 port_num, u32 mask, 1533cdbe33d0SEli Cohen u32 value) 1534cdbe33d0SEli Cohen { 1535cdbe33d0SEli Cohen struct mlx5_hca_vport_context ctx = {}; 1536b3cbd6f0SDaniel Jurgens struct mlx5_core_dev *mdev; 1537b3cbd6f0SDaniel Jurgens u8 mdev_port_num; 1538cdbe33d0SEli Cohen int err; 1539cdbe33d0SEli Cohen 1540b3cbd6f0SDaniel Jurgens mdev = mlx5_ib_get_native_port_mdev(dev, port_num, &mdev_port_num); 1541b3cbd6f0SDaniel Jurgens if (!mdev) 1542b3cbd6f0SDaniel Jurgens return -ENODEV; 1543b3cbd6f0SDaniel Jurgens 1544b3cbd6f0SDaniel Jurgens err = mlx5_query_hca_vport_context(mdev, 0, mdev_port_num, 0, &ctx); 1545cdbe33d0SEli Cohen if (err) 1546b3cbd6f0SDaniel Jurgens goto out; 1547cdbe33d0SEli Cohen 1548cdbe33d0SEli Cohen if (~ctx.cap_mask1_perm & mask) { 1549cdbe33d0SEli Cohen mlx5_ib_warn(dev, "trying to change bitmask 0x%X but change supported 0x%X\n", 1550cdbe33d0SEli Cohen mask, ctx.cap_mask1_perm); 1551b3cbd6f0SDaniel Jurgens err = -EINVAL; 1552b3cbd6f0SDaniel Jurgens goto out; 1553cdbe33d0SEli Cohen } 1554cdbe33d0SEli Cohen 1555cdbe33d0SEli Cohen ctx.cap_mask1 = value; 1556cdbe33d0SEli Cohen ctx.cap_mask1_perm = mask; 1557b3cbd6f0SDaniel Jurgens err = mlx5_core_modify_hca_vport_context(mdev, 0, mdev_port_num, 1558b3cbd6f0SDaniel Jurgens 0, &ctx); 1559b3cbd6f0SDaniel Jurgens 1560b3cbd6f0SDaniel Jurgens out: 1561b3cbd6f0SDaniel Jurgens mlx5_ib_put_native_port_mdev(dev, port_num); 1562cdbe33d0SEli Cohen 1563cdbe33d0SEli Cohen return err; 1564cdbe33d0SEli Cohen } 1565cdbe33d0SEli Cohen 1566e126ba97SEli Cohen static int mlx5_ib_modify_port(struct ib_device *ibdev, u8 port, int mask, 1567e126ba97SEli Cohen struct ib_port_modify *props) 1568e126ba97SEli Cohen { 1569e126ba97SEli Cohen struct mlx5_ib_dev *dev = to_mdev(ibdev); 1570e126ba97SEli Cohen struct ib_port_attr attr; 1571e126ba97SEli Cohen u32 tmp; 1572e126ba97SEli Cohen int err; 1573cdbe33d0SEli Cohen u32 change_mask; 1574cdbe33d0SEli Cohen u32 value; 1575cdbe33d0SEli Cohen bool is_ib = (mlx5_ib_port_link_layer(ibdev, port) == 1576cdbe33d0SEli Cohen IB_LINK_LAYER_INFINIBAND); 1577cdbe33d0SEli Cohen 1578ec255879SMajd Dibbiny /* CM layer calls ib_modify_port() regardless of the link layer. For 1579ec255879SMajd Dibbiny * Ethernet ports, qkey violation and Port capabilities are meaningless. 1580ec255879SMajd Dibbiny */ 1581ec255879SMajd Dibbiny if (!is_ib) 1582ec255879SMajd Dibbiny return 0; 1583ec255879SMajd Dibbiny 1584cdbe33d0SEli Cohen if (MLX5_CAP_GEN(dev->mdev, ib_virt) && is_ib) { 1585cdbe33d0SEli Cohen change_mask = props->clr_port_cap_mask | props->set_port_cap_mask; 1586cdbe33d0SEli Cohen value = ~props->clr_port_cap_mask | props->set_port_cap_mask; 1587cdbe33d0SEli Cohen return set_port_caps_atomic(dev, port, change_mask, value); 1588cdbe33d0SEli Cohen } 1589e126ba97SEli Cohen 1590e126ba97SEli Cohen mutex_lock(&dev->cap_mask_mutex); 1591e126ba97SEli Cohen 1592c4550c63SOr Gerlitz err = ib_query_port(ibdev, port, &attr); 1593e126ba97SEli Cohen if (err) 1594e126ba97SEli Cohen goto out; 1595e126ba97SEli Cohen 1596e126ba97SEli Cohen tmp = (attr.port_cap_flags | props->set_port_cap_mask) & 1597e126ba97SEli Cohen ~props->clr_port_cap_mask; 1598e126ba97SEli Cohen 15999603b61dSJack Morgenstein err = mlx5_set_port_caps(dev->mdev, port, tmp); 1600e126ba97SEli Cohen 1601e126ba97SEli Cohen out: 1602e126ba97SEli Cohen mutex_unlock(&dev->cap_mask_mutex); 1603e126ba97SEli Cohen return err; 1604e126ba97SEli Cohen } 1605e126ba97SEli Cohen 160630aa60b3SEli Cohen static void print_lib_caps(struct mlx5_ib_dev *dev, u64 caps) 160730aa60b3SEli Cohen { 160830aa60b3SEli Cohen mlx5_ib_dbg(dev, "MLX5_LIB_CAP_4K_UAR = %s\n", 160930aa60b3SEli Cohen caps & MLX5_LIB_CAP_4K_UAR ? "y" : "n"); 161030aa60b3SEli Cohen } 161130aa60b3SEli Cohen 161231a78a5aSYishai Hadas static u16 calc_dynamic_bfregs(int uars_per_sys_page) 161331a78a5aSYishai Hadas { 161431a78a5aSYishai Hadas /* Large page with non 4k uar support might limit the dynamic size */ 161531a78a5aSYishai Hadas if (uars_per_sys_page == 1 && PAGE_SIZE > 4096) 161631a78a5aSYishai Hadas return MLX5_MIN_DYN_BFREGS; 161731a78a5aSYishai Hadas 161831a78a5aSYishai Hadas return MLX5_MAX_DYN_BFREGS; 161931a78a5aSYishai Hadas } 162031a78a5aSYishai Hadas 1621b037c29aSEli Cohen static int calc_total_bfregs(struct mlx5_ib_dev *dev, bool lib_uar_4k, 1622b037c29aSEli Cohen struct mlx5_ib_alloc_ucontext_req_v2 *req, 162331a78a5aSYishai Hadas struct mlx5_bfreg_info *bfregi) 1624b037c29aSEli Cohen { 1625b037c29aSEli Cohen int uars_per_sys_page; 1626b037c29aSEli Cohen int bfregs_per_sys_page; 1627b037c29aSEli Cohen int ref_bfregs = req->total_num_bfregs; 1628b037c29aSEli Cohen 1629b037c29aSEli Cohen if (req->total_num_bfregs == 0) 1630b037c29aSEli Cohen return -EINVAL; 1631b037c29aSEli Cohen 1632b037c29aSEli Cohen BUILD_BUG_ON(MLX5_MAX_BFREGS % MLX5_NON_FP_BFREGS_IN_PAGE); 1633b037c29aSEli Cohen BUILD_BUG_ON(MLX5_MAX_BFREGS < MLX5_NON_FP_BFREGS_IN_PAGE); 1634b037c29aSEli Cohen 1635b037c29aSEli Cohen if (req->total_num_bfregs > MLX5_MAX_BFREGS) 1636b037c29aSEli Cohen return -ENOMEM; 1637b037c29aSEli Cohen 1638b037c29aSEli Cohen uars_per_sys_page = get_uars_per_sys_page(dev, lib_uar_4k); 1639b037c29aSEli Cohen bfregs_per_sys_page = uars_per_sys_page * MLX5_NON_FP_BFREGS_PER_UAR; 164031a78a5aSYishai Hadas /* This holds the required static allocation asked by the user */ 1641b037c29aSEli Cohen req->total_num_bfregs = ALIGN(req->total_num_bfregs, bfregs_per_sys_page); 1642b037c29aSEli Cohen if (req->num_low_latency_bfregs > req->total_num_bfregs - 1) 1643b037c29aSEli Cohen return -EINVAL; 1644b037c29aSEli Cohen 164531a78a5aSYishai Hadas bfregi->num_static_sys_pages = req->total_num_bfregs / bfregs_per_sys_page; 164631a78a5aSYishai Hadas bfregi->num_dyn_bfregs = ALIGN(calc_dynamic_bfregs(uars_per_sys_page), bfregs_per_sys_page); 164731a78a5aSYishai Hadas bfregi->total_num_bfregs = req->total_num_bfregs + bfregi->num_dyn_bfregs; 164831a78a5aSYishai Hadas bfregi->num_sys_pages = bfregi->total_num_bfregs / bfregs_per_sys_page; 164931a78a5aSYishai Hadas 165031a78a5aSYishai 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", 1651b037c29aSEli Cohen MLX5_CAP_GEN(dev->mdev, uar_4k) ? "yes" : "no", 1652b037c29aSEli Cohen lib_uar_4k ? "yes" : "no", ref_bfregs, 165331a78a5aSYishai Hadas req->total_num_bfregs, bfregi->total_num_bfregs, 165431a78a5aSYishai Hadas bfregi->num_sys_pages); 1655b037c29aSEli Cohen 1656b037c29aSEli Cohen return 0; 1657b037c29aSEli Cohen } 1658b037c29aSEli Cohen 1659b037c29aSEli Cohen static int allocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context) 1660b037c29aSEli Cohen { 1661b037c29aSEli Cohen struct mlx5_bfreg_info *bfregi; 1662b037c29aSEli Cohen int err; 1663b037c29aSEli Cohen int i; 1664b037c29aSEli Cohen 1665b037c29aSEli Cohen bfregi = &context->bfregi; 166631a78a5aSYishai Hadas for (i = 0; i < bfregi->num_static_sys_pages; i++) { 1667b037c29aSEli Cohen err = mlx5_cmd_alloc_uar(dev->mdev, &bfregi->sys_pages[i]); 1668b037c29aSEli Cohen if (err) 1669b037c29aSEli Cohen goto error; 1670b037c29aSEli Cohen 1671b037c29aSEli Cohen mlx5_ib_dbg(dev, "allocated uar %d\n", bfregi->sys_pages[i]); 1672b037c29aSEli Cohen } 16734ed131d0SYishai Hadas 16744ed131d0SYishai Hadas for (i = bfregi->num_static_sys_pages; i < bfregi->num_sys_pages; i++) 16754ed131d0SYishai Hadas bfregi->sys_pages[i] = MLX5_IB_INVALID_UAR_INDEX; 16764ed131d0SYishai Hadas 1677b037c29aSEli Cohen return 0; 1678b037c29aSEli Cohen 1679b037c29aSEli Cohen error: 1680b037c29aSEli Cohen for (--i; i >= 0; i--) 1681b037c29aSEli Cohen if (mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i])) 1682b037c29aSEli Cohen mlx5_ib_warn(dev, "failed to free uar %d\n", i); 1683b037c29aSEli Cohen 1684b037c29aSEli Cohen return err; 1685b037c29aSEli Cohen } 1686b037c29aSEli Cohen 168715177999SLeon Romanovsky static void deallocate_uars(struct mlx5_ib_dev *dev, 168815177999SLeon Romanovsky struct mlx5_ib_ucontext *context) 1689b037c29aSEli Cohen { 1690b037c29aSEli Cohen struct mlx5_bfreg_info *bfregi; 1691b037c29aSEli Cohen int i; 1692b037c29aSEli Cohen 1693b037c29aSEli Cohen bfregi = &context->bfregi; 169415177999SLeon Romanovsky for (i = 0; i < bfregi->num_sys_pages; i++) 16954ed131d0SYishai Hadas if (i < bfregi->num_static_sys_pages || 169615177999SLeon Romanovsky bfregi->sys_pages[i] != MLX5_IB_INVALID_UAR_INDEX) 169715177999SLeon Romanovsky mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]); 1698b037c29aSEli Cohen } 1699b037c29aSEli Cohen 17000042f9e4SMark Bloch int mlx5_ib_enable_lb(struct mlx5_ib_dev *dev, bool td, bool qp) 1701a560f1d9SMark Bloch { 1702a560f1d9SMark Bloch int err = 0; 1703a560f1d9SMark Bloch 1704a560f1d9SMark Bloch mutex_lock(&dev->lb.mutex); 17050042f9e4SMark Bloch if (td) 1706a560f1d9SMark Bloch dev->lb.user_td++; 17070042f9e4SMark Bloch if (qp) 17080042f9e4SMark Bloch dev->lb.qps++; 1709a560f1d9SMark Bloch 17100042f9e4SMark Bloch if (dev->lb.user_td == 2 || 17110042f9e4SMark Bloch dev->lb.qps == 1) { 17120042f9e4SMark Bloch if (!dev->lb.enabled) { 1713a560f1d9SMark Bloch err = mlx5_nic_vport_update_local_lb(dev->mdev, true); 17140042f9e4SMark Bloch dev->lb.enabled = true; 17150042f9e4SMark Bloch } 17160042f9e4SMark Bloch } 1717a560f1d9SMark Bloch 1718a560f1d9SMark Bloch mutex_unlock(&dev->lb.mutex); 1719a560f1d9SMark Bloch 1720a560f1d9SMark Bloch return err; 1721a560f1d9SMark Bloch } 1722a560f1d9SMark Bloch 17230042f9e4SMark Bloch void mlx5_ib_disable_lb(struct mlx5_ib_dev *dev, bool td, bool qp) 1724a560f1d9SMark Bloch { 1725a560f1d9SMark Bloch mutex_lock(&dev->lb.mutex); 17260042f9e4SMark Bloch if (td) 1727a560f1d9SMark Bloch dev->lb.user_td--; 17280042f9e4SMark Bloch if (qp) 17290042f9e4SMark Bloch dev->lb.qps--; 1730a560f1d9SMark Bloch 17310042f9e4SMark Bloch if (dev->lb.user_td == 1 && 17320042f9e4SMark Bloch dev->lb.qps == 0) { 17330042f9e4SMark Bloch if (dev->lb.enabled) { 1734a560f1d9SMark Bloch mlx5_nic_vport_update_local_lb(dev->mdev, false); 17350042f9e4SMark Bloch dev->lb.enabled = false; 17360042f9e4SMark Bloch } 17370042f9e4SMark Bloch } 1738a560f1d9SMark Bloch 1739a560f1d9SMark Bloch mutex_unlock(&dev->lb.mutex); 1740a560f1d9SMark Bloch } 1741a560f1d9SMark Bloch 1742d2d19121SYishai Hadas static int mlx5_ib_alloc_transport_domain(struct mlx5_ib_dev *dev, u32 *tdn, 1743d2d19121SYishai Hadas u16 uid) 1744c85023e1SHuy Nguyen { 1745c85023e1SHuy Nguyen int err; 1746c85023e1SHuy Nguyen 1747cfdeb893SLeon Romanovsky if (!MLX5_CAP_GEN(dev->mdev, log_max_transport_domain)) 1748cfdeb893SLeon Romanovsky return 0; 1749cfdeb893SLeon Romanovsky 1750d2d19121SYishai Hadas err = mlx5_cmd_alloc_transport_domain(dev->mdev, tdn, uid); 1751c85023e1SHuy Nguyen if (err) 1752c85023e1SHuy Nguyen return err; 1753c85023e1SHuy Nguyen 1754c85023e1SHuy Nguyen if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) || 17558978cc92SEran Ben Elisha (!MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) && 17568978cc92SEran Ben Elisha !MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc))) 1757c85023e1SHuy Nguyen return err; 1758c85023e1SHuy Nguyen 17590042f9e4SMark Bloch return mlx5_ib_enable_lb(dev, true, false); 1760c85023e1SHuy Nguyen } 1761c85023e1SHuy Nguyen 1762d2d19121SYishai Hadas static void mlx5_ib_dealloc_transport_domain(struct mlx5_ib_dev *dev, u32 tdn, 1763d2d19121SYishai Hadas u16 uid) 1764c85023e1SHuy Nguyen { 1765cfdeb893SLeon Romanovsky if (!MLX5_CAP_GEN(dev->mdev, log_max_transport_domain)) 1766cfdeb893SLeon Romanovsky return; 1767cfdeb893SLeon Romanovsky 1768d2d19121SYishai Hadas mlx5_cmd_dealloc_transport_domain(dev->mdev, tdn, uid); 1769c85023e1SHuy Nguyen 1770c85023e1SHuy Nguyen if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) || 17718978cc92SEran Ben Elisha (!MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) && 17728978cc92SEran Ben Elisha !MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc))) 1773c85023e1SHuy Nguyen return; 1774c85023e1SHuy Nguyen 17750042f9e4SMark Bloch mlx5_ib_disable_lb(dev, true, false); 1776c85023e1SHuy Nguyen } 1777c85023e1SHuy Nguyen 1778a2a074efSLeon Romanovsky static int mlx5_ib_alloc_ucontext(struct ib_ucontext *uctx, 1779e126ba97SEli Cohen struct ib_udata *udata) 1780e126ba97SEli Cohen { 1781a2a074efSLeon Romanovsky struct ib_device *ibdev = uctx->device; 1782e126ba97SEli Cohen struct mlx5_ib_dev *dev = to_mdev(ibdev); 1783b368d7cbSMatan Barak struct mlx5_ib_alloc_ucontext_req_v2 req = {}; 1784b368d7cbSMatan Barak struct mlx5_ib_alloc_ucontext_resp resp = {}; 17855c99eaecSFeras Daoud struct mlx5_core_dev *mdev = dev->mdev; 1786a2a074efSLeon Romanovsky struct mlx5_ib_ucontext *context = to_mucontext(uctx); 17872f5ff264SEli Cohen struct mlx5_bfreg_info *bfregi; 178878c0f98cSEli Cohen int ver; 1789e126ba97SEli Cohen int err; 1790a168a41cSMajd Dibbiny size_t min_req_v2 = offsetof(struct mlx5_ib_alloc_ucontext_req_v2, 1791a168a41cSMajd Dibbiny max_cqe_version); 179225bb36e7SYonatan Cohen u32 dump_fill_mkey; 1793b037c29aSEli Cohen bool lib_uar_4k; 1794e126ba97SEli Cohen 1795e126ba97SEli Cohen if (!dev->ib_active) 1796a2a074efSLeon Romanovsky return -EAGAIN; 1797e126ba97SEli Cohen 1798e093111dSAmrani, Ram if (udata->inlen == sizeof(struct mlx5_ib_alloc_ucontext_req)) 179978c0f98cSEli Cohen ver = 0; 1800e093111dSAmrani, Ram else if (udata->inlen >= min_req_v2) 180178c0f98cSEli Cohen ver = 2; 180278c0f98cSEli Cohen else 1803a2a074efSLeon Romanovsky return -EINVAL; 180478c0f98cSEli Cohen 1805e093111dSAmrani, Ram err = ib_copy_from_udata(&req, udata, min(udata->inlen, sizeof(req))); 1806e126ba97SEli Cohen if (err) 1807a2a074efSLeon Romanovsky return err; 1808e126ba97SEli Cohen 1809a8b92ca1SYishai Hadas if (req.flags & ~MLX5_IB_ALLOC_UCTX_DEVX) 1810a2a074efSLeon Romanovsky return -EOPNOTSUPP; 181178c0f98cSEli Cohen 1812f72300c5SHaggai Abramovsky if (req.comp_mask || req.reserved0 || req.reserved1 || req.reserved2) 1813a2a074efSLeon Romanovsky return -EOPNOTSUPP; 1814b368d7cbSMatan Barak 18152f5ff264SEli Cohen req.total_num_bfregs = ALIGN(req.total_num_bfregs, 18162f5ff264SEli Cohen MLX5_NON_FP_BFREGS_PER_UAR); 18172f5ff264SEli Cohen if (req.num_low_latency_bfregs > req.total_num_bfregs - 1) 1818a2a074efSLeon Romanovsky return -EINVAL; 1819e126ba97SEli Cohen 1820938fe83cSSaeed Mahameed resp.qp_tab_size = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp); 182111f552e2SMichael Guralnik if (dev->wc_support) 1822938fe83cSSaeed Mahameed resp.bf_reg_size = 1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size); 1823b47bd6eaSDaniel Jurgens resp.cache_line_size = cache_line_size(); 1824938fe83cSSaeed Mahameed resp.max_sq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq); 1825938fe83cSSaeed Mahameed resp.max_rq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq); 1826938fe83cSSaeed Mahameed resp.max_send_wqebb = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz); 1827938fe83cSSaeed Mahameed resp.max_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz); 1828938fe83cSSaeed Mahameed resp.max_srq_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz); 1829f72300c5SHaggai Abramovsky resp.cqe_version = min_t(__u8, 1830f72300c5SHaggai Abramovsky (__u8)MLX5_CAP_GEN(dev->mdev, cqe_version), 1831f72300c5SHaggai Abramovsky req.max_cqe_version); 183230aa60b3SEli Cohen resp.log_uar_size = MLX5_CAP_GEN(dev->mdev, uar_4k) ? 183330aa60b3SEli Cohen MLX5_ADAPTER_PAGE_SHIFT : PAGE_SHIFT; 183430aa60b3SEli Cohen resp.num_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ? 183530aa60b3SEli Cohen MLX5_CAP_GEN(dev->mdev, num_of_uars_per_page) : 1; 1836b368d7cbSMatan Barak resp.response_length = min(offsetof(typeof(resp), response_length) + 1837b368d7cbSMatan Barak sizeof(resp.response_length), udata->outlen); 1838e126ba97SEli Cohen 1839c03faa56SMatan Barak if (mlx5_accel_ipsec_device_caps(dev->mdev) & MLX5_ACCEL_IPSEC_CAP_DEVICE) { 1840c03faa56SMatan Barak if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_EGRESS)) 1841c03faa56SMatan Barak resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM; 1842c03faa56SMatan Barak if (mlx5_accel_ipsec_device_caps(dev->mdev) & MLX5_ACCEL_IPSEC_CAP_REQUIRED_METADATA) 1843c03faa56SMatan Barak resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_REQ_METADATA; 1844c03faa56SMatan Barak if (MLX5_CAP_FLOWTABLE(dev->mdev, flow_table_properties_nic_receive.ft_field_support.outer_esp_spi)) 1845c03faa56SMatan Barak resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_SPI_STEERING; 1846c03faa56SMatan Barak if (mlx5_accel_ipsec_device_caps(dev->mdev) & MLX5_ACCEL_IPSEC_CAP_TX_IV_IS_ESN) 1847c03faa56SMatan Barak resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_TX_IV_IS_ESN; 1848c03faa56SMatan Barak /* MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_FULL_OFFLOAD is currently always 0 */ 1849c03faa56SMatan Barak } 1850c03faa56SMatan Barak 185130aa60b3SEli Cohen lib_uar_4k = req.lib_caps & MLX5_LIB_CAP_4K_UAR; 18522f5ff264SEli Cohen bfregi = &context->bfregi; 1853b037c29aSEli Cohen 1854b037c29aSEli Cohen /* updates req->total_num_bfregs */ 185531a78a5aSYishai Hadas err = calc_total_bfregs(dev, lib_uar_4k, &req, bfregi); 1856b037c29aSEli Cohen if (err) 1857b037c29aSEli Cohen goto out_ctx; 1858b037c29aSEli Cohen 18592f5ff264SEli Cohen mutex_init(&bfregi->lock); 1860b037c29aSEli Cohen bfregi->lib_uar_4k = lib_uar_4k; 186131a78a5aSYishai Hadas bfregi->count = kcalloc(bfregi->total_num_bfregs, sizeof(*bfregi->count), 1862b037c29aSEli Cohen GFP_KERNEL); 1863b037c29aSEli Cohen if (!bfregi->count) { 1864e126ba97SEli Cohen err = -ENOMEM; 1865e126ba97SEli Cohen goto out_ctx; 1866e126ba97SEli Cohen } 1867e126ba97SEli Cohen 1868b037c29aSEli Cohen bfregi->sys_pages = kcalloc(bfregi->num_sys_pages, 1869b037c29aSEli Cohen sizeof(*bfregi->sys_pages), 1870e126ba97SEli Cohen GFP_KERNEL); 1871b037c29aSEli Cohen if (!bfregi->sys_pages) { 1872e126ba97SEli Cohen err = -ENOMEM; 1873e126ba97SEli Cohen goto out_count; 1874e126ba97SEli Cohen } 1875e126ba97SEli Cohen 1876b037c29aSEli Cohen err = allocate_uars(dev, context); 1877b037c29aSEli Cohen if (err) 1878b037c29aSEli Cohen goto out_sys_pages; 1879b037c29aSEli Cohen 1880a8b92ca1SYishai Hadas if (req.flags & MLX5_IB_ALLOC_UCTX_DEVX) { 1881fb98153bSYishai Hadas err = mlx5_ib_devx_create(dev, true); 188276dc5a84SYishai Hadas if (err < 0) 1883d2d19121SYishai Hadas goto out_uars; 188476dc5a84SYishai Hadas context->devx_uid = err; 1885a8b92ca1SYishai Hadas } 1886a8b92ca1SYishai Hadas 1887d2d19121SYishai Hadas err = mlx5_ib_alloc_transport_domain(dev, &context->tdn, 1888d2d19121SYishai Hadas context->devx_uid); 1889a8b92ca1SYishai Hadas if (err) 1890d2d19121SYishai Hadas goto out_devx; 1891a8b92ca1SYishai Hadas 189225bb36e7SYonatan Cohen if (MLX5_CAP_GEN(dev->mdev, dump_fill_mkey)) { 189325bb36e7SYonatan Cohen err = mlx5_cmd_dump_fill_mkey(dev->mdev, &dump_fill_mkey); 189425bb36e7SYonatan Cohen if (err) 18958193abb6SJason Gunthorpe goto out_mdev; 189625bb36e7SYonatan Cohen } 189725bb36e7SYonatan Cohen 1898e126ba97SEli Cohen INIT_LIST_HEAD(&context->db_page_list); 1899e126ba97SEli Cohen mutex_init(&context->db_page_mutex); 1900e126ba97SEli Cohen 19012f5ff264SEli Cohen resp.tot_bfregs = req.total_num_bfregs; 1902508562d6SDaniel Jurgens resp.num_ports = dev->num_ports; 1903b368d7cbSMatan Barak 1904f72300c5SHaggai Abramovsky if (field_avail(typeof(resp), cqe_version, udata->outlen)) 1905f72300c5SHaggai Abramovsky resp.response_length += sizeof(resp.cqe_version); 1906b368d7cbSMatan Barak 1907402ca536SBodong Wang if (field_avail(typeof(resp), cmds_supp_uhw, udata->outlen)) { 19086ad279c5SMoni Shoua resp.cmds_supp_uhw |= MLX5_USER_CMDS_SUPP_UHW_QUERY_DEVICE | 19096ad279c5SMoni Shoua MLX5_USER_CMDS_SUPP_UHW_CREATE_AH; 1910402ca536SBodong Wang resp.response_length += sizeof(resp.cmds_supp_uhw); 1911402ca536SBodong Wang } 1912402ca536SBodong Wang 191378984898SOr Gerlitz if (field_avail(typeof(resp), eth_min_inline, udata->outlen)) { 191478984898SOr Gerlitz if (mlx5_ib_port_link_layer(ibdev, 1) == IB_LINK_LAYER_ETHERNET) { 191578984898SOr Gerlitz mlx5_query_min_inline(dev->mdev, &resp.eth_min_inline); 191678984898SOr Gerlitz resp.eth_min_inline++; 191778984898SOr Gerlitz } 191878984898SOr Gerlitz resp.response_length += sizeof(resp.eth_min_inline); 191978984898SOr Gerlitz } 192078984898SOr Gerlitz 19215c99eaecSFeras Daoud if (field_avail(typeof(resp), clock_info_versions, udata->outlen)) { 19225c99eaecSFeras Daoud if (mdev->clock_info) 19235c99eaecSFeras Daoud resp.clock_info_versions = BIT(MLX5_IB_CLOCK_INFO_V1); 19245c99eaecSFeras Daoud resp.response_length += sizeof(resp.clock_info_versions); 19255c99eaecSFeras Daoud } 19265c99eaecSFeras Daoud 1927bc5c6eedSNoa Osherovich /* 1928bc5c6eedSNoa Osherovich * We don't want to expose information from the PCI bar that is located 1929bc5c6eedSNoa Osherovich * after 4096 bytes, so if the arch only supports larger pages, let's 1930bc5c6eedSNoa Osherovich * pretend we don't support reading the HCA's core clock. This is also 1931bc5c6eedSNoa Osherovich * forced by mmap function. 1932bc5c6eedSNoa Osherovich */ 1933de8d6e02SEli Cohen if (field_avail(typeof(resp), hca_core_clock_offset, udata->outlen)) { 1934de8d6e02SEli Cohen if (PAGE_SIZE <= 4096) { 1935b368d7cbSMatan Barak resp.comp_mask |= 1936b368d7cbSMatan Barak MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_CORE_CLOCK_OFFSET; 1937b368d7cbSMatan Barak resp.hca_core_clock_offset = 1938de8d6e02SEli Cohen offsetof(struct mlx5_init_seg, internal_timer_h) % PAGE_SIZE; 1939de8d6e02SEli Cohen } 19405c99eaecSFeras Daoud resp.response_length += sizeof(resp.hca_core_clock_offset); 1941b368d7cbSMatan Barak } 1942b368d7cbSMatan Barak 194330aa60b3SEli Cohen if (field_avail(typeof(resp), log_uar_size, udata->outlen)) 194430aa60b3SEli Cohen resp.response_length += sizeof(resp.log_uar_size); 194530aa60b3SEli Cohen 194630aa60b3SEli Cohen if (field_avail(typeof(resp), num_uars_per_page, udata->outlen)) 194730aa60b3SEli Cohen resp.response_length += sizeof(resp.num_uars_per_page); 194830aa60b3SEli Cohen 194931a78a5aSYishai Hadas if (field_avail(typeof(resp), num_dyn_bfregs, udata->outlen)) { 195031a78a5aSYishai Hadas resp.num_dyn_bfregs = bfregi->num_dyn_bfregs; 195131a78a5aSYishai Hadas resp.response_length += sizeof(resp.num_dyn_bfregs); 195231a78a5aSYishai Hadas } 195331a78a5aSYishai Hadas 195425bb36e7SYonatan Cohen if (field_avail(typeof(resp), dump_fill_mkey, udata->outlen)) { 195525bb36e7SYonatan Cohen if (MLX5_CAP_GEN(dev->mdev, dump_fill_mkey)) { 195625bb36e7SYonatan Cohen resp.dump_fill_mkey = dump_fill_mkey; 195725bb36e7SYonatan Cohen resp.comp_mask |= 195825bb36e7SYonatan Cohen MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_DUMP_FILL_MKEY; 195925bb36e7SYonatan Cohen } 196025bb36e7SYonatan Cohen resp.response_length += sizeof(resp.dump_fill_mkey); 196125bb36e7SYonatan Cohen } 196225bb36e7SYonatan Cohen 1963b368d7cbSMatan Barak err = ib_copy_to_udata(udata, &resp, resp.response_length); 1964e126ba97SEli Cohen if (err) 1965a8b92ca1SYishai Hadas goto out_mdev; 1966e126ba97SEli Cohen 19672f5ff264SEli Cohen bfregi->ver = ver; 19682f5ff264SEli Cohen bfregi->num_low_latency_bfregs = req.num_low_latency_bfregs; 1969f72300c5SHaggai Abramovsky context->cqe_version = resp.cqe_version; 197030aa60b3SEli Cohen context->lib_caps = req.lib_caps; 197130aa60b3SEli Cohen print_lib_caps(dev, context->lib_caps); 1972f72300c5SHaggai Abramovsky 19737c34ec19SAviv Heller if (dev->lag_active) { 197495579e78SMark Bloch u8 port = mlx5_core_native_port_num(dev->mdev) - 1; 1975c6a21c38SMajd Dibbiny 1976c6a21c38SMajd Dibbiny atomic_set(&context->tx_port_affinity, 1977c6a21c38SMajd Dibbiny atomic_add_return( 197895579e78SMark Bloch 1, &dev->port[port].roce.tx_port_affinity)); 1979c6a21c38SMajd Dibbiny } 1980c6a21c38SMajd Dibbiny 1981a2a074efSLeon Romanovsky return 0; 1982e126ba97SEli Cohen 1983a8b92ca1SYishai Hadas out_mdev: 1984d2d19121SYishai Hadas mlx5_ib_dealloc_transport_domain(dev, context->tdn, context->devx_uid); 1985d2d19121SYishai Hadas out_devx: 1986a8b92ca1SYishai Hadas if (req.flags & MLX5_IB_ALLOC_UCTX_DEVX) 198776dc5a84SYishai Hadas mlx5_ib_devx_destroy(dev, context->devx_uid); 1988146d2f1aSmajd@mellanox.com 1989e126ba97SEli Cohen out_uars: 1990b037c29aSEli Cohen deallocate_uars(dev, context); 1991b037c29aSEli Cohen 1992b037c29aSEli Cohen out_sys_pages: 1993b037c29aSEli Cohen kfree(bfregi->sys_pages); 1994b037c29aSEli Cohen 1995e126ba97SEli Cohen out_count: 19962f5ff264SEli Cohen kfree(bfregi->count); 1997e126ba97SEli Cohen 1998e126ba97SEli Cohen out_ctx: 1999a2a074efSLeon Romanovsky return err; 2000e126ba97SEli Cohen } 2001e126ba97SEli Cohen 2002a2a074efSLeon Romanovsky static void mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext) 2003e126ba97SEli Cohen { 2004e126ba97SEli Cohen struct mlx5_ib_ucontext *context = to_mucontext(ibcontext); 2005e126ba97SEli Cohen struct mlx5_ib_dev *dev = to_mdev(ibcontext->device); 2006b037c29aSEli Cohen struct mlx5_bfreg_info *bfregi; 2007e126ba97SEli Cohen 2008b037c29aSEli Cohen bfregi = &context->bfregi; 2009d2d19121SYishai Hadas mlx5_ib_dealloc_transport_domain(dev, context->tdn, context->devx_uid); 2010d2d19121SYishai Hadas 2011b037c29aSEli Cohen if (context->devx_uid) 201276dc5a84SYishai Hadas mlx5_ib_devx_destroy(dev, context->devx_uid); 2013e126ba97SEli Cohen 2014e126ba97SEli Cohen deallocate_uars(dev, context); 20152f5ff264SEli Cohen kfree(bfregi->sys_pages); 20162f5ff264SEli Cohen kfree(bfregi->count); 2017e126ba97SEli Cohen } 2018e126ba97SEli Cohen 2019e126ba97SEli Cohen static phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev, 2020e126ba97SEli Cohen int uar_idx) 2021e126ba97SEli Cohen { 2022b037c29aSEli Cohen int fw_uars_per_page; 2023b037c29aSEli Cohen 2024b037c29aSEli Cohen fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ? MLX5_UARS_IN_PAGE : 1; 2025b037c29aSEli Cohen 2026aa8106f1SHuy Nguyen return (dev->mdev->bar_addr >> PAGE_SHIFT) + uar_idx / fw_uars_per_page; 2027e126ba97SEli Cohen } 2028e126ba97SEli Cohen 2029e126ba97SEli Cohen static int get_command(unsigned long offset) 2030e126ba97SEli Cohen { 2031e126ba97SEli Cohen return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK; 2032e126ba97SEli Cohen } 2033e126ba97SEli Cohen 2034e126ba97SEli Cohen static int get_arg(unsigned long offset) 2035e126ba97SEli Cohen { 2036e126ba97SEli Cohen return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1); 2037e126ba97SEli Cohen } 2038e126ba97SEli Cohen 2039e126ba97SEli Cohen static int get_index(unsigned long offset) 2040e126ba97SEli Cohen { 2041e126ba97SEli Cohen return get_arg(offset); 2042e126ba97SEli Cohen } 2043e126ba97SEli Cohen 20444ed131d0SYishai Hadas /* Index resides in an extra byte to enable larger values than 255 */ 20454ed131d0SYishai Hadas static int get_extended_index(unsigned long offset) 20464ed131d0SYishai Hadas { 20474ed131d0SYishai Hadas return get_arg(offset) | ((offset >> 16) & 0xff) << 8; 20484ed131d0SYishai Hadas } 20494ed131d0SYishai Hadas 20507c2344c3SMaor Gottlieb 20517c2344c3SMaor Gottlieb static void mlx5_ib_disassociate_ucontext(struct ib_ucontext *ibcontext) 20527c2344c3SMaor Gottlieb { 20537c2344c3SMaor Gottlieb } 20547c2344c3SMaor Gottlieb 205537aa5c36SGuy Levi static inline char *mmap_cmd2str(enum mlx5_ib_mmap_cmd cmd) 2056e126ba97SEli Cohen { 205737aa5c36SGuy Levi switch (cmd) { 205837aa5c36SGuy Levi case MLX5_IB_MMAP_WC_PAGE: 205937aa5c36SGuy Levi return "WC"; 2060e126ba97SEli Cohen case MLX5_IB_MMAP_REGULAR_PAGE: 206137aa5c36SGuy Levi return "best effort WC"; 206237aa5c36SGuy Levi case MLX5_IB_MMAP_NC_PAGE: 206337aa5c36SGuy Levi return "NC"; 206424da0016SAriel Levkovich case MLX5_IB_MMAP_DEVICE_MEM: 206524da0016SAriel Levkovich return "Device Memory"; 206637aa5c36SGuy Levi default: 206737aa5c36SGuy Levi return NULL; 206837aa5c36SGuy Levi } 206937aa5c36SGuy Levi } 207037aa5c36SGuy Levi 20715c99eaecSFeras Daoud static int mlx5_ib_mmap_clock_info_page(struct mlx5_ib_dev *dev, 20725c99eaecSFeras Daoud struct vm_area_struct *vma, 20735c99eaecSFeras Daoud struct mlx5_ib_ucontext *context) 20745c99eaecSFeras Daoud { 20754eb6ab13SJason Gunthorpe if ((vma->vm_end - vma->vm_start != PAGE_SIZE) || 20764eb6ab13SJason Gunthorpe !(vma->vm_flags & VM_SHARED)) 20775c99eaecSFeras Daoud return -EINVAL; 20785c99eaecSFeras Daoud 20795c99eaecSFeras Daoud if (get_index(vma->vm_pgoff) != MLX5_IB_CLOCK_INFO_V1) 20805c99eaecSFeras Daoud return -EOPNOTSUPP; 20815c99eaecSFeras Daoud 20824eb6ab13SJason Gunthorpe if (vma->vm_flags & (VM_WRITE | VM_EXEC)) 20835c99eaecSFeras Daoud return -EPERM; 2084c660133cSJason Gunthorpe vma->vm_flags &= ~VM_MAYWRITE; 20855c99eaecSFeras Daoud 2086ddcdc368SJason Gunthorpe if (!dev->mdev->clock_info) 20875c99eaecSFeras Daoud return -EOPNOTSUPP; 20885c99eaecSFeras Daoud 20894eb6ab13SJason Gunthorpe return vm_insert_page(vma, vma->vm_start, 20904eb6ab13SJason Gunthorpe virt_to_page(dev->mdev->clock_info)); 20915c99eaecSFeras Daoud } 20925c99eaecSFeras Daoud 2093dc2316ebSYishai Hadas static void mlx5_ib_mmap_free(struct rdma_user_mmap_entry *entry) 2094dc2316ebSYishai Hadas { 2095dc2316ebSYishai Hadas struct mlx5_user_mmap_entry *mentry = to_mmmap(entry); 2096dc2316ebSYishai Hadas struct mlx5_ib_dev *dev = to_mdev(entry->ucontext->device); 20977be76befSYishai Hadas struct mlx5_var_table *var_table = &dev->var_table; 2098dc2316ebSYishai Hadas struct mlx5_ib_dm *mdm; 2099dc2316ebSYishai Hadas 2100dc2316ebSYishai Hadas switch (mentry->mmap_flag) { 2101dc2316ebSYishai Hadas case MLX5_IB_MMAP_TYPE_MEMIC: 2102dc2316ebSYishai Hadas mdm = container_of(mentry, struct mlx5_ib_dm, mentry); 2103dc2316ebSYishai Hadas mlx5_cmd_dealloc_memic(&dev->dm, mdm->dev_addr, 2104dc2316ebSYishai Hadas mdm->size); 2105dc2316ebSYishai Hadas kfree(mdm); 2106dc2316ebSYishai Hadas break; 21077be76befSYishai Hadas case MLX5_IB_MMAP_TYPE_VAR: 21087be76befSYishai Hadas mutex_lock(&var_table->bitmap_lock); 21097be76befSYishai Hadas clear_bit(mentry->page_idx, var_table->bitmap); 21107be76befSYishai Hadas mutex_unlock(&var_table->bitmap_lock); 21117be76befSYishai Hadas kfree(mentry); 21127be76befSYishai Hadas break; 2113dc2316ebSYishai Hadas default: 2114dc2316ebSYishai Hadas WARN_ON(true); 2115dc2316ebSYishai Hadas } 2116dc2316ebSYishai Hadas } 2117dc2316ebSYishai Hadas 211837aa5c36SGuy Levi static int uar_mmap(struct mlx5_ib_dev *dev, enum mlx5_ib_mmap_cmd cmd, 21197c2344c3SMaor Gottlieb struct vm_area_struct *vma, 21207c2344c3SMaor Gottlieb struct mlx5_ib_ucontext *context) 212137aa5c36SGuy Levi { 21222f5ff264SEli Cohen struct mlx5_bfreg_info *bfregi = &context->bfregi; 212337aa5c36SGuy Levi int err; 212437aa5c36SGuy Levi unsigned long idx; 2125aa09ea6eSKamal Heib phys_addr_t pfn; 212637aa5c36SGuy Levi pgprot_t prot; 21274ed131d0SYishai Hadas u32 bfreg_dyn_idx = 0; 21284ed131d0SYishai Hadas u32 uar_index; 21294ed131d0SYishai Hadas int dyn_uar = (cmd == MLX5_IB_MMAP_ALLOC_WC); 21304ed131d0SYishai Hadas int max_valid_idx = dyn_uar ? bfregi->num_sys_pages : 21314ed131d0SYishai Hadas bfregi->num_static_sys_pages; 2132b037c29aSEli Cohen 2133b037c29aSEli Cohen if (vma->vm_end - vma->vm_start != PAGE_SIZE) 2134b037c29aSEli Cohen return -EINVAL; 2135b037c29aSEli Cohen 21364ed131d0SYishai Hadas if (dyn_uar) 21374ed131d0SYishai Hadas idx = get_extended_index(vma->vm_pgoff) + bfregi->num_static_sys_pages; 21384ed131d0SYishai Hadas else 2139b037c29aSEli Cohen idx = get_index(vma->vm_pgoff); 21404ed131d0SYishai Hadas 21414ed131d0SYishai Hadas if (idx >= max_valid_idx) { 21424ed131d0SYishai Hadas mlx5_ib_warn(dev, "invalid uar index %lu, max=%d\n", 21434ed131d0SYishai Hadas idx, max_valid_idx); 2144b037c29aSEli Cohen return -EINVAL; 2145b037c29aSEli Cohen } 214637aa5c36SGuy Levi 214737aa5c36SGuy Levi switch (cmd) { 214837aa5c36SGuy Levi case MLX5_IB_MMAP_WC_PAGE: 21494ed131d0SYishai Hadas case MLX5_IB_MMAP_ALLOC_WC: 215037aa5c36SGuy Levi /* Some architectures don't support WC memory */ 215137aa5c36SGuy Levi #if defined(CONFIG_X86) 215237aa5c36SGuy Levi if (!pat_enabled()) 215337aa5c36SGuy Levi return -EPERM; 215437aa5c36SGuy Levi #elif !(defined(CONFIG_PPC) || (defined(CONFIG_ARM) && defined(CONFIG_MMU))) 215537aa5c36SGuy Levi return -EPERM; 215637aa5c36SGuy Levi #endif 215737aa5c36SGuy Levi /* fall through */ 215837aa5c36SGuy Levi case MLX5_IB_MMAP_REGULAR_PAGE: 215937aa5c36SGuy Levi /* For MLX5_IB_MMAP_REGULAR_PAGE do the best effort to get WC */ 216037aa5c36SGuy Levi prot = pgprot_writecombine(vma->vm_page_prot); 216137aa5c36SGuy Levi break; 216237aa5c36SGuy Levi case MLX5_IB_MMAP_NC_PAGE: 216337aa5c36SGuy Levi prot = pgprot_noncached(vma->vm_page_prot); 216437aa5c36SGuy Levi break; 216537aa5c36SGuy Levi default: 216637aa5c36SGuy Levi return -EINVAL; 216737aa5c36SGuy Levi } 216837aa5c36SGuy Levi 21694ed131d0SYishai Hadas if (dyn_uar) { 21704ed131d0SYishai Hadas int uars_per_page; 21714ed131d0SYishai Hadas 21724ed131d0SYishai Hadas uars_per_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k); 21734ed131d0SYishai Hadas bfreg_dyn_idx = idx * (uars_per_page * MLX5_NON_FP_BFREGS_PER_UAR); 21744ed131d0SYishai Hadas if (bfreg_dyn_idx >= bfregi->total_num_bfregs) { 21754ed131d0SYishai Hadas mlx5_ib_warn(dev, "invalid bfreg_dyn_idx %u, max=%u\n", 21764ed131d0SYishai Hadas bfreg_dyn_idx, bfregi->total_num_bfregs); 21774ed131d0SYishai Hadas return -EINVAL; 21784ed131d0SYishai Hadas } 21794ed131d0SYishai Hadas 21804ed131d0SYishai Hadas mutex_lock(&bfregi->lock); 21814ed131d0SYishai Hadas /* Fail if uar already allocated, first bfreg index of each 21824ed131d0SYishai Hadas * page holds its count. 21834ed131d0SYishai Hadas */ 21844ed131d0SYishai Hadas if (bfregi->count[bfreg_dyn_idx]) { 21854ed131d0SYishai Hadas mlx5_ib_warn(dev, "wrong offset, idx %lu is busy, bfregn=%u\n", idx, bfreg_dyn_idx); 21864ed131d0SYishai Hadas mutex_unlock(&bfregi->lock); 21874ed131d0SYishai Hadas return -EINVAL; 21884ed131d0SYishai Hadas } 21894ed131d0SYishai Hadas 21904ed131d0SYishai Hadas bfregi->count[bfreg_dyn_idx]++; 21914ed131d0SYishai Hadas mutex_unlock(&bfregi->lock); 21924ed131d0SYishai Hadas 21934ed131d0SYishai Hadas err = mlx5_cmd_alloc_uar(dev->mdev, &uar_index); 21944ed131d0SYishai Hadas if (err) { 21954ed131d0SYishai Hadas mlx5_ib_warn(dev, "UAR alloc failed\n"); 21964ed131d0SYishai Hadas goto free_bfreg; 21974ed131d0SYishai Hadas } 21984ed131d0SYishai Hadas } else { 21994ed131d0SYishai Hadas uar_index = bfregi->sys_pages[idx]; 22004ed131d0SYishai Hadas } 22014ed131d0SYishai Hadas 22024ed131d0SYishai Hadas pfn = uar_index2pfn(dev, uar_index); 220337aa5c36SGuy Levi mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn %pa\n", idx, &pfn); 2204e126ba97SEli Cohen 2205e2cd1d1aSJason Gunthorpe err = rdma_user_mmap_io(&context->ibucontext, vma, pfn, PAGE_SIZE, 2206c043ff2cSMichal Kalderon prot, NULL); 220737aa5c36SGuy Levi if (err) { 22088f062287SLeon Romanovsky mlx5_ib_err(dev, 2209e2cd1d1aSJason Gunthorpe "rdma_user_mmap_io failed with error=%d, mmap_cmd=%s\n", 22108f062287SLeon Romanovsky err, mmap_cmd2str(cmd)); 22114ed131d0SYishai Hadas goto err; 221237aa5c36SGuy Levi } 2213e126ba97SEli Cohen 22144ed131d0SYishai Hadas if (dyn_uar) 22154ed131d0SYishai Hadas bfregi->sys_pages[idx] = uar_index; 22164ed131d0SYishai Hadas return 0; 22174ed131d0SYishai Hadas 22184ed131d0SYishai Hadas err: 22194ed131d0SYishai Hadas if (!dyn_uar) 22204ed131d0SYishai Hadas return err; 22214ed131d0SYishai Hadas 22224ed131d0SYishai Hadas mlx5_cmd_free_uar(dev->mdev, idx); 22234ed131d0SYishai Hadas 22244ed131d0SYishai Hadas free_bfreg: 22254ed131d0SYishai Hadas mlx5_ib_free_bfreg(dev, bfregi, bfreg_dyn_idx); 22264ed131d0SYishai Hadas 22274ed131d0SYishai Hadas return err; 222837aa5c36SGuy Levi } 222937aa5c36SGuy Levi 2230dc2316ebSYishai Hadas static int add_dm_mmap_entry(struct ib_ucontext *context, 2231dc2316ebSYishai Hadas struct mlx5_ib_dm *mdm, 2232dc2316ebSYishai Hadas u64 address) 223324da0016SAriel Levkovich { 2234dc2316ebSYishai Hadas mdm->mentry.mmap_flag = MLX5_IB_MMAP_TYPE_MEMIC; 2235dc2316ebSYishai Hadas mdm->mentry.address = address; 2236dc2316ebSYishai Hadas return rdma_user_mmap_entry_insert_range( 2237dc2316ebSYishai Hadas context, &mdm->mentry.rdma_entry, 2238dc2316ebSYishai Hadas mdm->size, 2239dc2316ebSYishai Hadas MLX5_IB_MMAP_DEVICE_MEM << 16, 2240dc2316ebSYishai Hadas (MLX5_IB_MMAP_DEVICE_MEM << 16) + (1UL << 16) - 1); 2241dc2316ebSYishai Hadas } 224224da0016SAriel Levkovich 2243dc2316ebSYishai Hadas static unsigned long mlx5_vma_to_pgoff(struct vm_area_struct *vma) 2244dc2316ebSYishai Hadas { 2245dc2316ebSYishai Hadas unsigned long idx; 2246dc2316ebSYishai Hadas u8 command; 2247dc2316ebSYishai Hadas 2248dc2316ebSYishai Hadas command = get_command(vma->vm_pgoff); 2249dc2316ebSYishai Hadas idx = get_extended_index(vma->vm_pgoff); 2250dc2316ebSYishai Hadas 2251dc2316ebSYishai Hadas return (command << 16 | idx); 2252dc2316ebSYishai Hadas } 2253dc2316ebSYishai Hadas 2254dc2316ebSYishai Hadas static int mlx5_ib_mmap_offset(struct mlx5_ib_dev *dev, 2255dc2316ebSYishai Hadas struct vm_area_struct *vma, 2256dc2316ebSYishai Hadas struct ib_ucontext *ucontext) 2257dc2316ebSYishai Hadas { 2258dc2316ebSYishai Hadas struct mlx5_user_mmap_entry *mentry; 2259dc2316ebSYishai Hadas struct rdma_user_mmap_entry *entry; 2260dc2316ebSYishai Hadas unsigned long pgoff; 2261dc2316ebSYishai Hadas pgprot_t prot; 2262dc2316ebSYishai Hadas phys_addr_t pfn; 2263dc2316ebSYishai Hadas int ret; 2264dc2316ebSYishai Hadas 2265dc2316ebSYishai Hadas pgoff = mlx5_vma_to_pgoff(vma); 2266dc2316ebSYishai Hadas entry = rdma_user_mmap_entry_get_pgoff(ucontext, pgoff); 2267dc2316ebSYishai Hadas if (!entry) 226824da0016SAriel Levkovich return -EINVAL; 226924da0016SAriel Levkovich 2270dc2316ebSYishai Hadas mentry = to_mmmap(entry); 2271dc2316ebSYishai Hadas pfn = (mentry->address >> PAGE_SHIFT); 22723f59b6c3SYishai Hadas if (mentry->mmap_flag == MLX5_IB_MMAP_TYPE_VAR) 22733f59b6c3SYishai Hadas prot = pgprot_noncached(vma->vm_page_prot); 22743f59b6c3SYishai Hadas else 2275dc2316ebSYishai Hadas prot = pgprot_writecombine(vma->vm_page_prot); 2276dc2316ebSYishai Hadas ret = rdma_user_mmap_io(ucontext, vma, pfn, 2277dc2316ebSYishai Hadas entry->npages * PAGE_SIZE, 2278dc2316ebSYishai Hadas prot, 2279dc2316ebSYishai Hadas entry); 2280dc2316ebSYishai Hadas rdma_user_mmap_entry_put(&mentry->rdma_entry); 2281dc2316ebSYishai Hadas return ret; 228224da0016SAriel Levkovich } 228324da0016SAriel Levkovich 22847be76befSYishai Hadas static u64 mlx5_entry_to_mmap_offset(struct mlx5_user_mmap_entry *entry) 22857be76befSYishai Hadas { 22867be76befSYishai Hadas u16 cmd = entry->rdma_entry.start_pgoff >> 16; 22877be76befSYishai Hadas u16 index = entry->rdma_entry.start_pgoff & 0xFFFF; 22887be76befSYishai Hadas 22897be76befSYishai Hadas return (((index >> 8) << 16) | (cmd << MLX5_IB_MMAP_CMD_SHIFT) | 22907be76befSYishai Hadas (index & 0xFF)) << PAGE_SHIFT; 22917be76befSYishai Hadas } 22927be76befSYishai Hadas 229337aa5c36SGuy Levi static int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma) 229437aa5c36SGuy Levi { 229537aa5c36SGuy Levi struct mlx5_ib_ucontext *context = to_mucontext(ibcontext); 229637aa5c36SGuy Levi struct mlx5_ib_dev *dev = to_mdev(ibcontext->device); 229737aa5c36SGuy Levi unsigned long command; 229837aa5c36SGuy Levi phys_addr_t pfn; 229937aa5c36SGuy Levi 230037aa5c36SGuy Levi command = get_command(vma->vm_pgoff); 230137aa5c36SGuy Levi switch (command) { 230237aa5c36SGuy Levi case MLX5_IB_MMAP_WC_PAGE: 230337aa5c36SGuy Levi case MLX5_IB_MMAP_NC_PAGE: 230437aa5c36SGuy Levi case MLX5_IB_MMAP_REGULAR_PAGE: 23054ed131d0SYishai Hadas case MLX5_IB_MMAP_ALLOC_WC: 23067c2344c3SMaor Gottlieb return uar_mmap(dev, command, vma, context); 2307e126ba97SEli Cohen 2308e126ba97SEli Cohen case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES: 2309e126ba97SEli Cohen return -ENOSYS; 2310e126ba97SEli Cohen 2311d69e3bcfSMatan Barak case MLX5_IB_MMAP_CORE_CLOCK: 2312d69e3bcfSMatan Barak if (vma->vm_end - vma->vm_start != PAGE_SIZE) 2313d69e3bcfSMatan Barak return -EINVAL; 2314d69e3bcfSMatan Barak 23156cbac1e4SMatan Barak if (vma->vm_flags & VM_WRITE) 2316d69e3bcfSMatan Barak return -EPERM; 2317c660133cSJason Gunthorpe vma->vm_flags &= ~VM_MAYWRITE; 2318d69e3bcfSMatan Barak 2319d69e3bcfSMatan Barak /* Don't expose to user-space information it shouldn't have */ 2320d69e3bcfSMatan Barak if (PAGE_SIZE > 4096) 2321d69e3bcfSMatan Barak return -EOPNOTSUPP; 2322d69e3bcfSMatan Barak 2323d69e3bcfSMatan Barak pfn = (dev->mdev->iseg_base + 2324d69e3bcfSMatan Barak offsetof(struct mlx5_init_seg, internal_timer_h)) >> 2325d69e3bcfSMatan Barak PAGE_SHIFT; 2326d5e560d3SJason Gunthorpe return rdma_user_mmap_io(&context->ibucontext, vma, pfn, 2327d5e560d3SJason Gunthorpe PAGE_SIZE, 2328c043ff2cSMichal Kalderon pgprot_noncached(vma->vm_page_prot), 2329c043ff2cSMichal Kalderon NULL); 23305c99eaecSFeras Daoud case MLX5_IB_MMAP_CLOCK_INFO: 23315c99eaecSFeras Daoud return mlx5_ib_mmap_clock_info_page(dev, vma, context); 2332d69e3bcfSMatan Barak 2333e126ba97SEli Cohen default: 2334dc2316ebSYishai Hadas return mlx5_ib_mmap_offset(dev, vma, ibcontext); 2335e126ba97SEli Cohen } 2336e126ba97SEli Cohen 2337e126ba97SEli Cohen return 0; 2338e126ba97SEli Cohen } 2339e126ba97SEli Cohen 234025c13324SAriel Levkovich static inline int check_dm_type_support(struct mlx5_ib_dev *dev, 234125c13324SAriel Levkovich u32 type) 234225c13324SAriel Levkovich { 234325c13324SAriel Levkovich switch (type) { 234425c13324SAriel Levkovich case MLX5_IB_UAPI_DM_TYPE_MEMIC: 234525c13324SAriel Levkovich if (!MLX5_CAP_DEV_MEM(dev->mdev, memic)) 234625c13324SAriel Levkovich return -EOPNOTSUPP; 234725c13324SAriel Levkovich break; 234825c13324SAriel Levkovich case MLX5_IB_UAPI_DM_TYPE_STEERING_SW_ICM: 2349c9b9dcb4SAriel Levkovich case MLX5_IB_UAPI_DM_TYPE_HEADER_MODIFY_SW_ICM: 235025c13324SAriel Levkovich if (!capable(CAP_SYS_RAWIO) || 235125c13324SAriel Levkovich !capable(CAP_NET_RAW)) 235225c13324SAriel Levkovich return -EPERM; 235325c13324SAriel Levkovich 235425c13324SAriel Levkovich if (!(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, sw_owner) || 235525c13324SAriel Levkovich MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev, sw_owner))) 235625c13324SAriel Levkovich return -EOPNOTSUPP; 235725c13324SAriel Levkovich break; 235825c13324SAriel Levkovich } 235925c13324SAriel Levkovich 236025c13324SAriel Levkovich return 0; 236125c13324SAriel Levkovich } 236225c13324SAriel Levkovich 23633b113a1eSAriel Levkovich static int handle_alloc_dm_memic(struct ib_ucontext *ctx, 23643b113a1eSAriel Levkovich struct mlx5_ib_dm *dm, 236524da0016SAriel Levkovich struct ib_dm_alloc_attr *attr, 236624da0016SAriel Levkovich struct uverbs_attr_bundle *attrs) 236724da0016SAriel Levkovich { 23683b113a1eSAriel Levkovich struct mlx5_dm *dm_db = &to_mdev(ctx->device)->dm; 236924da0016SAriel Levkovich u64 start_offset; 2370dc2316ebSYishai Hadas u16 page_idx; 237124da0016SAriel Levkovich int err; 2372dc2316ebSYishai Hadas u64 address; 237324da0016SAriel Levkovich 23743b113a1eSAriel Levkovich dm->size = roundup(attr->length, MLX5_MEMIC_BASE_SIZE); 237524da0016SAriel Levkovich 23763b113a1eSAriel Levkovich err = mlx5_cmd_alloc_memic(dm_db, &dm->dev_addr, 23773b113a1eSAriel Levkovich dm->size, attr->alignment); 237824da0016SAriel Levkovich if (err) 23793b113a1eSAriel Levkovich return err; 238024da0016SAriel Levkovich 2381dc2316ebSYishai Hadas address = dm->dev_addr & PAGE_MASK; 2382dc2316ebSYishai Hadas err = add_dm_mmap_entry(ctx, dm, address); 238324da0016SAriel Levkovich if (err) 238424da0016SAriel Levkovich goto err_dealloc; 238524da0016SAriel Levkovich 2386dc2316ebSYishai Hadas page_idx = dm->mentry.rdma_entry.start_pgoff & 0xFFFF; 2387dc2316ebSYishai Hadas err = uverbs_copy_to(attrs, 2388dc2316ebSYishai Hadas MLX5_IB_ATTR_ALLOC_DM_RESP_PAGE_INDEX, 2389dc2316ebSYishai Hadas &page_idx, 2390dc2316ebSYishai Hadas sizeof(page_idx)); 2391dc2316ebSYishai Hadas if (err) 2392dc2316ebSYishai Hadas goto err_copy; 2393dc2316ebSYishai Hadas 23943b113a1eSAriel Levkovich start_offset = dm->dev_addr & ~PAGE_MASK; 23953b113a1eSAriel Levkovich err = uverbs_copy_to(attrs, 23963b113a1eSAriel Levkovich MLX5_IB_ATTR_ALLOC_DM_RESP_START_OFFSET, 23973b113a1eSAriel Levkovich &start_offset, sizeof(start_offset)); 23983b113a1eSAriel Levkovich if (err) 2399dc2316ebSYishai Hadas goto err_copy; 24003b113a1eSAriel Levkovich 24013b113a1eSAriel Levkovich return 0; 24023b113a1eSAriel Levkovich 2403dc2316ebSYishai Hadas err_copy: 2404dc2316ebSYishai Hadas rdma_user_mmap_entry_remove(&dm->mentry.rdma_entry); 24053b113a1eSAriel Levkovich err_dealloc: 24063b113a1eSAriel Levkovich mlx5_cmd_dealloc_memic(dm_db, dm->dev_addr, dm->size); 24073b113a1eSAriel Levkovich 24083b113a1eSAriel Levkovich return err; 24093b113a1eSAriel Levkovich } 24103b113a1eSAriel Levkovich 241125c13324SAriel Levkovich static int handle_alloc_dm_sw_icm(struct ib_ucontext *ctx, 241225c13324SAriel Levkovich struct mlx5_ib_dm *dm, 241325c13324SAriel Levkovich struct ib_dm_alloc_attr *attr, 241425c13324SAriel Levkovich struct uverbs_attr_bundle *attrs, 241525c13324SAriel Levkovich int type) 241625c13324SAriel Levkovich { 2417c9b9dcb4SAriel Levkovich struct mlx5_core_dev *dev = to_mdev(ctx->device)->mdev; 241825c13324SAriel Levkovich u64 act_size; 241925c13324SAriel Levkovich int err; 242025c13324SAriel Levkovich 242125c13324SAriel Levkovich /* Allocation size must a multiple of the basic block size 242225c13324SAriel Levkovich * and a power of 2. 242325c13324SAriel Levkovich */ 2424c9b9dcb4SAriel Levkovich act_size = round_up(attr->length, MLX5_SW_ICM_BLOCK_SIZE(dev)); 242525c13324SAriel Levkovich act_size = roundup_pow_of_two(act_size); 242625c13324SAriel Levkovich 242725c13324SAriel Levkovich dm->size = act_size; 2428c9b9dcb4SAriel Levkovich err = mlx5_dm_sw_icm_alloc(dev, type, act_size, 242925c13324SAriel Levkovich to_mucontext(ctx)->devx_uid, &dm->dev_addr, 243025c13324SAriel Levkovich &dm->icm_dm.obj_id); 243125c13324SAriel Levkovich if (err) 243225c13324SAriel Levkovich return err; 243325c13324SAriel Levkovich 243425c13324SAriel Levkovich err = uverbs_copy_to(attrs, 243525c13324SAriel Levkovich MLX5_IB_ATTR_ALLOC_DM_RESP_START_OFFSET, 243625c13324SAriel Levkovich &dm->dev_addr, sizeof(dm->dev_addr)); 243725c13324SAriel Levkovich if (err) 2438c9b9dcb4SAriel Levkovich mlx5_dm_sw_icm_dealloc(dev, type, dm->size, 2439c9b9dcb4SAriel Levkovich to_mucontext(ctx)->devx_uid, dm->dev_addr, 2440c9b9dcb4SAriel Levkovich dm->icm_dm.obj_id); 244125c13324SAriel Levkovich 244225c13324SAriel Levkovich return err; 244325c13324SAriel Levkovich } 244425c13324SAriel Levkovich 24453b113a1eSAriel Levkovich struct ib_dm *mlx5_ib_alloc_dm(struct ib_device *ibdev, 24463b113a1eSAriel Levkovich struct ib_ucontext *context, 24473b113a1eSAriel Levkovich struct ib_dm_alloc_attr *attr, 24483b113a1eSAriel Levkovich struct uverbs_attr_bundle *attrs) 24493b113a1eSAriel Levkovich { 24503b113a1eSAriel Levkovich struct mlx5_ib_dm *dm; 24513b113a1eSAriel Levkovich enum mlx5_ib_uapi_dm_type type; 24523b113a1eSAriel Levkovich int err; 24533b113a1eSAriel Levkovich 24543b113a1eSAriel Levkovich err = uverbs_get_const_default(&type, attrs, 24553b113a1eSAriel Levkovich MLX5_IB_ATTR_ALLOC_DM_REQ_TYPE, 24563b113a1eSAriel Levkovich MLX5_IB_UAPI_DM_TYPE_MEMIC); 24573b113a1eSAriel Levkovich if (err) 24583b113a1eSAriel Levkovich return ERR_PTR(err); 24593b113a1eSAriel Levkovich 24603b113a1eSAriel Levkovich mlx5_ib_dbg(to_mdev(ibdev), "alloc_dm req: dm_type=%d user_length=0x%llx log_alignment=%d\n", 24613b113a1eSAriel Levkovich type, attr->length, attr->alignment); 24623b113a1eSAriel Levkovich 246325c13324SAriel Levkovich err = check_dm_type_support(to_mdev(ibdev), type); 246425c13324SAriel Levkovich if (err) 246525c13324SAriel Levkovich return ERR_PTR(err); 246625c13324SAriel Levkovich 24673b113a1eSAriel Levkovich dm = kzalloc(sizeof(*dm), GFP_KERNEL); 24683b113a1eSAriel Levkovich if (!dm) 24693b113a1eSAriel Levkovich return ERR_PTR(-ENOMEM); 24703b113a1eSAriel Levkovich 24713b113a1eSAriel Levkovich dm->type = type; 24723b113a1eSAriel Levkovich 24733b113a1eSAriel Levkovich switch (type) { 24743b113a1eSAriel Levkovich case MLX5_IB_UAPI_DM_TYPE_MEMIC: 24753b113a1eSAriel Levkovich err = handle_alloc_dm_memic(context, dm, 24763b113a1eSAriel Levkovich attr, 24773b113a1eSAriel Levkovich attrs); 24783b113a1eSAriel Levkovich break; 247925c13324SAriel Levkovich case MLX5_IB_UAPI_DM_TYPE_STEERING_SW_ICM: 2480c9b9dcb4SAriel Levkovich err = handle_alloc_dm_sw_icm(context, dm, 2481c9b9dcb4SAriel Levkovich attr, attrs, 2482c9b9dcb4SAriel Levkovich MLX5_SW_ICM_TYPE_STEERING); 2483c9b9dcb4SAriel Levkovich break; 248425c13324SAriel Levkovich case MLX5_IB_UAPI_DM_TYPE_HEADER_MODIFY_SW_ICM: 2485c9b9dcb4SAriel Levkovich err = handle_alloc_dm_sw_icm(context, dm, 2486c9b9dcb4SAriel Levkovich attr, attrs, 2487c9b9dcb4SAriel Levkovich MLX5_SW_ICM_TYPE_HEADER_MODIFY); 248825c13324SAriel Levkovich break; 24893b113a1eSAriel Levkovich default: 24903b113a1eSAriel Levkovich err = -EOPNOTSUPP; 24913b113a1eSAriel Levkovich } 24923b113a1eSAriel Levkovich 24933b113a1eSAriel Levkovich if (err) 24943b113a1eSAriel Levkovich goto err_free; 249524da0016SAriel Levkovich 249624da0016SAriel Levkovich return &dm->ibdm; 249724da0016SAriel Levkovich 249824da0016SAriel Levkovich err_free: 249924da0016SAriel Levkovich kfree(dm); 250024da0016SAriel Levkovich return ERR_PTR(err); 250124da0016SAriel Levkovich } 250224da0016SAriel Levkovich 2503c4367a26SShamir Rabinovitch int mlx5_ib_dealloc_dm(struct ib_dm *ibdm, struct uverbs_attr_bundle *attrs) 250424da0016SAriel Levkovich { 250525c13324SAriel Levkovich struct mlx5_ib_ucontext *ctx = rdma_udata_to_drv_context( 250625c13324SAriel Levkovich &attrs->driver_udata, struct mlx5_ib_ucontext, ibucontext); 2507c9b9dcb4SAriel Levkovich struct mlx5_core_dev *dev = to_mdev(ibdm->device)->mdev; 250824da0016SAriel Levkovich struct mlx5_ib_dm *dm = to_mdm(ibdm); 250924da0016SAriel Levkovich int ret; 251024da0016SAriel Levkovich 25113b113a1eSAriel Levkovich switch (dm->type) { 25123b113a1eSAriel Levkovich case MLX5_IB_UAPI_DM_TYPE_MEMIC: 2513dc2316ebSYishai Hadas rdma_user_mmap_entry_remove(&dm->mentry.rdma_entry); 2514dc2316ebSYishai Hadas return 0; 251525c13324SAriel Levkovich case MLX5_IB_UAPI_DM_TYPE_STEERING_SW_ICM: 2516c9b9dcb4SAriel Levkovich ret = mlx5_dm_sw_icm_dealloc(dev, MLX5_SW_ICM_TYPE_STEERING, 2517c9b9dcb4SAriel Levkovich dm->size, ctx->devx_uid, dm->dev_addr, 2518c9b9dcb4SAriel Levkovich dm->icm_dm.obj_id); 2519c9b9dcb4SAriel Levkovich if (ret) 2520c9b9dcb4SAriel Levkovich return ret; 2521c9b9dcb4SAriel Levkovich break; 252225c13324SAriel Levkovich case MLX5_IB_UAPI_DM_TYPE_HEADER_MODIFY_SW_ICM: 2523c9b9dcb4SAriel Levkovich ret = mlx5_dm_sw_icm_dealloc(dev, MLX5_SW_ICM_TYPE_HEADER_MODIFY, 2524c9b9dcb4SAriel Levkovich dm->size, ctx->devx_uid, dm->dev_addr, 252525c13324SAriel Levkovich dm->icm_dm.obj_id); 252625c13324SAriel Levkovich if (ret) 252725c13324SAriel Levkovich return ret; 25283b113a1eSAriel Levkovich break; 25293b113a1eSAriel Levkovich default: 25303b113a1eSAriel Levkovich return -EOPNOTSUPP; 25313b113a1eSAriel Levkovich } 253224da0016SAriel Levkovich 253324da0016SAriel Levkovich kfree(dm); 253424da0016SAriel Levkovich 253524da0016SAriel Levkovich return 0; 253624da0016SAriel Levkovich } 253724da0016SAriel Levkovich 2538ff23dfa1SShamir Rabinovitch static int mlx5_ib_alloc_pd(struct ib_pd *ibpd, struct ib_udata *udata) 2539e126ba97SEli Cohen { 254021a428a0SLeon Romanovsky struct mlx5_ib_pd *pd = to_mpd(ibpd); 254121a428a0SLeon Romanovsky struct ib_device *ibdev = ibpd->device; 2542e126ba97SEli Cohen struct mlx5_ib_alloc_pd_resp resp; 2543e126ba97SEli Cohen int err; 2544a1069c1cSYishai Hadas u32 out[MLX5_ST_SZ_DW(alloc_pd_out)] = {}; 2545a1069c1cSYishai Hadas u32 in[MLX5_ST_SZ_DW(alloc_pd_in)] = {}; 2546a1069c1cSYishai Hadas u16 uid = 0; 2547ff23dfa1SShamir Rabinovitch struct mlx5_ib_ucontext *context = rdma_udata_to_drv_context( 2548ff23dfa1SShamir Rabinovitch udata, struct mlx5_ib_ucontext, ibucontext); 2549e126ba97SEli Cohen 2550ff23dfa1SShamir Rabinovitch uid = context ? context->devx_uid : 0; 2551a1069c1cSYishai Hadas MLX5_SET(alloc_pd_in, in, opcode, MLX5_CMD_OP_ALLOC_PD); 2552a1069c1cSYishai Hadas MLX5_SET(alloc_pd_in, in, uid, uid); 2553a1069c1cSYishai Hadas err = mlx5_cmd_exec(to_mdev(ibdev)->mdev, in, sizeof(in), 2554a1069c1cSYishai Hadas out, sizeof(out)); 255521a428a0SLeon Romanovsky if (err) 255621a428a0SLeon Romanovsky return err; 2557e126ba97SEli Cohen 2558a1069c1cSYishai Hadas pd->pdn = MLX5_GET(alloc_pd_out, out, pd); 2559a1069c1cSYishai Hadas pd->uid = uid; 2560ff23dfa1SShamir Rabinovitch if (udata) { 2561e126ba97SEli Cohen resp.pdn = pd->pdn; 2562e126ba97SEli Cohen if (ib_copy_to_udata(udata, &resp, sizeof(resp))) { 2563a1069c1cSYishai Hadas mlx5_cmd_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn, uid); 256421a428a0SLeon Romanovsky return -EFAULT; 2565e126ba97SEli Cohen } 2566e126ba97SEli Cohen } 2567e126ba97SEli Cohen 256821a428a0SLeon Romanovsky return 0; 2569e126ba97SEli Cohen } 2570e126ba97SEli Cohen 2571c4367a26SShamir Rabinovitch static void mlx5_ib_dealloc_pd(struct ib_pd *pd, struct ib_udata *udata) 2572e126ba97SEli Cohen { 2573e126ba97SEli Cohen struct mlx5_ib_dev *mdev = to_mdev(pd->device); 2574e126ba97SEli Cohen struct mlx5_ib_pd *mpd = to_mpd(pd); 2575e126ba97SEli Cohen 2576a1069c1cSYishai Hadas mlx5_cmd_dealloc_pd(mdev->mdev, mpd->pdn, mpd->uid); 2577e126ba97SEli Cohen } 2578e126ba97SEli Cohen 2579466fa6d2SMaor Gottlieb enum { 2580466fa6d2SMaor Gottlieb MATCH_CRITERIA_ENABLE_OUTER_BIT, 2581466fa6d2SMaor Gottlieb MATCH_CRITERIA_ENABLE_MISC_BIT, 258271c6e863SAriel Levkovich MATCH_CRITERIA_ENABLE_INNER_BIT, 258371c6e863SAriel Levkovich MATCH_CRITERIA_ENABLE_MISC2_BIT 2584466fa6d2SMaor Gottlieb }; 2585038d2ef8SMaor Gottlieb 2586466fa6d2SMaor Gottlieb #define HEADER_IS_ZERO(match_criteria, headers) \ 2587466fa6d2SMaor Gottlieb !(memchr_inv(MLX5_ADDR_OF(fte_match_param, match_criteria, headers), \ 2588466fa6d2SMaor Gottlieb 0, MLX5_FLD_SZ_BYTES(fte_match_param, headers))) \ 2589466fa6d2SMaor Gottlieb 2590466fa6d2SMaor Gottlieb static u8 get_match_criteria_enable(u32 *match_criteria) 2591466fa6d2SMaor Gottlieb { 2592466fa6d2SMaor Gottlieb u8 match_criteria_enable; 2593466fa6d2SMaor Gottlieb 2594466fa6d2SMaor Gottlieb match_criteria_enable = 2595466fa6d2SMaor Gottlieb (!HEADER_IS_ZERO(match_criteria, outer_headers)) << 2596466fa6d2SMaor Gottlieb MATCH_CRITERIA_ENABLE_OUTER_BIT; 2597466fa6d2SMaor Gottlieb match_criteria_enable |= 2598466fa6d2SMaor Gottlieb (!HEADER_IS_ZERO(match_criteria, misc_parameters)) << 2599466fa6d2SMaor Gottlieb MATCH_CRITERIA_ENABLE_MISC_BIT; 2600466fa6d2SMaor Gottlieb match_criteria_enable |= 2601466fa6d2SMaor Gottlieb (!HEADER_IS_ZERO(match_criteria, inner_headers)) << 2602466fa6d2SMaor Gottlieb MATCH_CRITERIA_ENABLE_INNER_BIT; 260371c6e863SAriel Levkovich match_criteria_enable |= 260471c6e863SAriel Levkovich (!HEADER_IS_ZERO(match_criteria, misc_parameters_2)) << 260571c6e863SAriel Levkovich MATCH_CRITERIA_ENABLE_MISC2_BIT; 2606466fa6d2SMaor Gottlieb 2607466fa6d2SMaor Gottlieb return match_criteria_enable; 2608038d2ef8SMaor Gottlieb } 2609038d2ef8SMaor Gottlieb 26106113cc44SMaor Gottlieb static int set_proto(void *outer_c, void *outer_v, u8 mask, u8 val) 2611ca0d4753SMaor Gottlieb { 26126113cc44SMaor Gottlieb u8 entry_mask; 26136113cc44SMaor Gottlieb u8 entry_val; 26146113cc44SMaor Gottlieb int err = 0; 26156113cc44SMaor Gottlieb 26166113cc44SMaor Gottlieb if (!mask) 26176113cc44SMaor Gottlieb goto out; 26186113cc44SMaor Gottlieb 26196113cc44SMaor Gottlieb entry_mask = MLX5_GET(fte_match_set_lyr_2_4, outer_c, 26206113cc44SMaor Gottlieb ip_protocol); 26216113cc44SMaor Gottlieb entry_val = MLX5_GET(fte_match_set_lyr_2_4, outer_v, 26226113cc44SMaor Gottlieb ip_protocol); 26236113cc44SMaor Gottlieb if (!entry_mask) { 2624ca0d4753SMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_protocol, mask); 2625ca0d4753SMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_protocol, val); 26266113cc44SMaor Gottlieb goto out; 26276113cc44SMaor Gottlieb } 26286113cc44SMaor Gottlieb /* Don't override existing ip protocol */ 26296113cc44SMaor Gottlieb if (mask != entry_mask || val != entry_val) 26306113cc44SMaor Gottlieb err = -EINVAL; 26316113cc44SMaor Gottlieb out: 26326113cc44SMaor Gottlieb return err; 2633ca0d4753SMaor Gottlieb } 2634ca0d4753SMaor Gottlieb 263537da2a03SDaria Velikovsky static void set_flow_label(void *misc_c, void *misc_v, u32 mask, u32 val, 26362d1e697eSMoses Reuben bool inner) 26372d1e697eSMoses Reuben { 26382d1e697eSMoses Reuben if (inner) { 26392d1e697eSMoses Reuben MLX5_SET(fte_match_set_misc, 26402d1e697eSMoses Reuben misc_c, inner_ipv6_flow_label, mask); 26412d1e697eSMoses Reuben MLX5_SET(fte_match_set_misc, 26422d1e697eSMoses Reuben misc_v, inner_ipv6_flow_label, val); 26432d1e697eSMoses Reuben } else { 26442d1e697eSMoses Reuben MLX5_SET(fte_match_set_misc, 26452d1e697eSMoses Reuben misc_c, outer_ipv6_flow_label, mask); 26462d1e697eSMoses Reuben MLX5_SET(fte_match_set_misc, 26472d1e697eSMoses Reuben misc_v, outer_ipv6_flow_label, val); 26482d1e697eSMoses Reuben } 26492d1e697eSMoses Reuben } 26502d1e697eSMoses Reuben 2651ca0d4753SMaor Gottlieb static void set_tos(void *outer_c, void *outer_v, u8 mask, u8 val) 2652ca0d4753SMaor Gottlieb { 2653ca0d4753SMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_ecn, mask); 2654ca0d4753SMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_ecn, val); 2655ca0d4753SMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_dscp, mask >> 2); 2656ca0d4753SMaor Gottlieb MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_dscp, val >> 2); 2657ca0d4753SMaor Gottlieb } 2658ca0d4753SMaor Gottlieb 265971c6e863SAriel Levkovich static int check_mpls_supp_fields(u32 field_support, const __be32 *set_mask) 266071c6e863SAriel Levkovich { 266171c6e863SAriel Levkovich if (MLX5_GET(fte_match_mpls, set_mask, mpls_label) && 266271c6e863SAriel Levkovich !(field_support & MLX5_FIELD_SUPPORT_MPLS_LABEL)) 266371c6e863SAriel Levkovich return -EOPNOTSUPP; 266471c6e863SAriel Levkovich 266571c6e863SAriel Levkovich if (MLX5_GET(fte_match_mpls, set_mask, mpls_exp) && 266671c6e863SAriel Levkovich !(field_support & MLX5_FIELD_SUPPORT_MPLS_EXP)) 266771c6e863SAriel Levkovich return -EOPNOTSUPP; 266871c6e863SAriel Levkovich 266971c6e863SAriel Levkovich if (MLX5_GET(fte_match_mpls, set_mask, mpls_s_bos) && 267071c6e863SAriel Levkovich !(field_support & MLX5_FIELD_SUPPORT_MPLS_S_BOS)) 267171c6e863SAriel Levkovich return -EOPNOTSUPP; 267271c6e863SAriel Levkovich 267371c6e863SAriel Levkovich if (MLX5_GET(fte_match_mpls, set_mask, mpls_ttl) && 267471c6e863SAriel Levkovich !(field_support & MLX5_FIELD_SUPPORT_MPLS_TTL)) 267571c6e863SAriel Levkovich return -EOPNOTSUPP; 267671c6e863SAriel Levkovich 267771c6e863SAriel Levkovich return 0; 267871c6e863SAriel Levkovich } 267971c6e863SAriel Levkovich 2680c47ac6aeSMaor Gottlieb #define LAST_ETH_FIELD vlan_tag 2681c47ac6aeSMaor Gottlieb #define LAST_IB_FIELD sl 2682ca0d4753SMaor Gottlieb #define LAST_IPV4_FIELD tos 2683466fa6d2SMaor Gottlieb #define LAST_IPV6_FIELD traffic_class 2684c47ac6aeSMaor Gottlieb #define LAST_TCP_UDP_FIELD src_port 2685ffb30d8fSMoses Reuben #define LAST_TUNNEL_FIELD tunnel_id 26862ac693f9SMoses Reuben #define LAST_FLOW_TAG_FIELD tag_id 2687a22ed86cSSlava Shwartsman #define LAST_DROP_FIELD size 26883b3233fbSRaed Salem #define LAST_COUNTERS_FIELD counters 2689c47ac6aeSMaor Gottlieb 2690c47ac6aeSMaor Gottlieb /* Field is the last supported field */ 2691c47ac6aeSMaor Gottlieb #define FIELDS_NOT_SUPPORTED(filter, field)\ 2692c47ac6aeSMaor Gottlieb memchr_inv((void *)&filter.field +\ 2693c47ac6aeSMaor Gottlieb sizeof(filter.field), 0,\ 2694c47ac6aeSMaor Gottlieb sizeof(filter) -\ 2695c47ac6aeSMaor Gottlieb offsetof(typeof(filter), field) -\ 2696c47ac6aeSMaor Gottlieb sizeof(filter.field)) 2697c47ac6aeSMaor Gottlieb 26982ea26203SMark Bloch int parse_flow_flow_action(struct mlx5_ib_flow_action *maction, 26992ea26203SMark Bloch bool is_egress, 2700802c2125SAviad Yehezkel struct mlx5_flow_act *action) 2701802c2125SAviad Yehezkel { 2702802c2125SAviad Yehezkel 2703802c2125SAviad Yehezkel switch (maction->ib_action.type) { 2704802c2125SAviad Yehezkel case IB_FLOW_ACTION_ESP: 2705501f14e3SMark Bloch if (action->action & (MLX5_FLOW_CONTEXT_ACTION_ENCRYPT | 2706501f14e3SMark Bloch MLX5_FLOW_CONTEXT_ACTION_DECRYPT)) 2707501f14e3SMark Bloch return -EINVAL; 2708802c2125SAviad Yehezkel /* Currently only AES_GCM keymat is supported by the driver */ 2709802c2125SAviad Yehezkel action->esp_id = (uintptr_t)maction->esp_aes_gcm.ctx; 27102ea26203SMark Bloch action->action |= is_egress ? 2711802c2125SAviad Yehezkel MLX5_FLOW_CONTEXT_ACTION_ENCRYPT : 2712802c2125SAviad Yehezkel MLX5_FLOW_CONTEXT_ACTION_DECRYPT; 2713802c2125SAviad Yehezkel return 0; 2714b1085be3SMark Bloch case IB_FLOW_ACTION_UNSPECIFIED: 2715b1085be3SMark Bloch if (maction->flow_action_raw.sub_type == 2716b1085be3SMark Bloch MLX5_IB_FLOW_ACTION_MODIFY_HEADER) { 2717501f14e3SMark Bloch if (action->action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR) 2718501f14e3SMark Bloch return -EINVAL; 2719b1085be3SMark Bloch action->action |= MLX5_FLOW_CONTEXT_ACTION_MOD_HDR; 27202b688ea5SMaor Gottlieb action->modify_hdr = 27212b688ea5SMaor Gottlieb maction->flow_action_raw.modify_hdr; 2722b1085be3SMark Bloch return 0; 2723b1085be3SMark Bloch } 272410a30896SMark Bloch if (maction->flow_action_raw.sub_type == 272510a30896SMark Bloch MLX5_IB_FLOW_ACTION_DECAP) { 2726501f14e3SMark Bloch if (action->action & MLX5_FLOW_CONTEXT_ACTION_DECAP) 2727501f14e3SMark Bloch return -EINVAL; 272810a30896SMark Bloch action->action |= MLX5_FLOW_CONTEXT_ACTION_DECAP; 272910a30896SMark Bloch return 0; 273010a30896SMark Bloch } 2731e806f932SMark Bloch if (maction->flow_action_raw.sub_type == 2732e806f932SMark Bloch MLX5_IB_FLOW_ACTION_PACKET_REFORMAT) { 2733501f14e3SMark Bloch if (action->action & 2734501f14e3SMark Bloch MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT) 2735501f14e3SMark Bloch return -EINVAL; 2736e806f932SMark Bloch action->action |= 2737e806f932SMark Bloch MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT; 27382b688ea5SMaor Gottlieb action->pkt_reformat = 27392b688ea5SMaor Gottlieb maction->flow_action_raw.pkt_reformat; 2740e806f932SMark Bloch return 0; 2741e806f932SMark Bloch } 2742b1085be3SMark Bloch /* fall through */ 2743802c2125SAviad Yehezkel default: 2744802c2125SAviad Yehezkel return -EOPNOTSUPP; 2745802c2125SAviad Yehezkel } 2746802c2125SAviad Yehezkel } 2747802c2125SAviad Yehezkel 2748bb0ee7dcSJianbo Liu static int parse_flow_attr(struct mlx5_core_dev *mdev, 2749bb0ee7dcSJianbo Liu struct mlx5_flow_spec *spec, 2750bb0ee7dcSJianbo Liu const union ib_flow_spec *ib_spec, 2751802c2125SAviad Yehezkel const struct ib_flow_attr *flow_attr, 275271c6e863SAriel Levkovich struct mlx5_flow_act *action, u32 prev_type) 2753038d2ef8SMaor Gottlieb { 2754bb0ee7dcSJianbo Liu struct mlx5_flow_context *flow_context = &spec->flow_context; 2755bb0ee7dcSJianbo Liu u32 *match_c = spec->match_criteria; 2756bb0ee7dcSJianbo Liu u32 *match_v = spec->match_value; 2757466fa6d2SMaor Gottlieb void *misc_params_c = MLX5_ADDR_OF(fte_match_param, match_c, 2758466fa6d2SMaor Gottlieb misc_parameters); 2759466fa6d2SMaor Gottlieb void *misc_params_v = MLX5_ADDR_OF(fte_match_param, match_v, 2760466fa6d2SMaor Gottlieb misc_parameters); 276171c6e863SAriel Levkovich void *misc_params2_c = MLX5_ADDR_OF(fte_match_param, match_c, 276271c6e863SAriel Levkovich misc_parameters_2); 276371c6e863SAriel Levkovich void *misc_params2_v = MLX5_ADDR_OF(fte_match_param, match_v, 276471c6e863SAriel Levkovich misc_parameters_2); 27652d1e697eSMoses Reuben void *headers_c; 27662d1e697eSMoses Reuben void *headers_v; 276719cc7524SAriel Levkovich int match_ipv; 2768802c2125SAviad Yehezkel int ret; 2769466fa6d2SMaor Gottlieb 27702d1e697eSMoses Reuben if (ib_spec->type & IB_FLOW_SPEC_INNER) { 27712d1e697eSMoses Reuben headers_c = MLX5_ADDR_OF(fte_match_param, match_c, 27722d1e697eSMoses Reuben inner_headers); 27732d1e697eSMoses Reuben headers_v = MLX5_ADDR_OF(fte_match_param, match_v, 27742d1e697eSMoses Reuben inner_headers); 277519cc7524SAriel Levkovich match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev, 277619cc7524SAriel Levkovich ft_field_support.inner_ip_version); 27772d1e697eSMoses Reuben } else { 27782d1e697eSMoses Reuben headers_c = MLX5_ADDR_OF(fte_match_param, match_c, 27792d1e697eSMoses Reuben outer_headers); 27802d1e697eSMoses Reuben headers_v = MLX5_ADDR_OF(fte_match_param, match_v, 27812d1e697eSMoses Reuben outer_headers); 278219cc7524SAriel Levkovich match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev, 278319cc7524SAriel Levkovich ft_field_support.outer_ip_version); 27842d1e697eSMoses Reuben } 27852d1e697eSMoses Reuben 27862d1e697eSMoses Reuben switch (ib_spec->type & ~IB_FLOW_SPEC_INNER) { 2787038d2ef8SMaor Gottlieb case IB_FLOW_SPEC_ETH: 2788c47ac6aeSMaor Gottlieb if (FIELDS_NOT_SUPPORTED(ib_spec->eth.mask, LAST_ETH_FIELD)) 27891ffd3a26SLeon Romanovsky return -EOPNOTSUPP; 2790038d2ef8SMaor Gottlieb 27912d1e697eSMoses Reuben ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c, 2792038d2ef8SMaor Gottlieb dmac_47_16), 2793038d2ef8SMaor Gottlieb ib_spec->eth.mask.dst_mac); 27942d1e697eSMoses Reuben ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v, 2795038d2ef8SMaor Gottlieb dmac_47_16), 2796038d2ef8SMaor Gottlieb ib_spec->eth.val.dst_mac); 2797038d2ef8SMaor Gottlieb 27982d1e697eSMoses Reuben ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c, 2799ee3da804SMaor Gottlieb smac_47_16), 2800ee3da804SMaor Gottlieb ib_spec->eth.mask.src_mac); 28012d1e697eSMoses Reuben ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v, 2802ee3da804SMaor Gottlieb smac_47_16), 2803ee3da804SMaor Gottlieb ib_spec->eth.val.src_mac); 2804ee3da804SMaor Gottlieb 2805038d2ef8SMaor Gottlieb if (ib_spec->eth.mask.vlan_tag) { 28062d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_c, 280710543365SMohamad Haj Yahia cvlan_tag, 1); 28082d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_v, 280910543365SMohamad Haj Yahia cvlan_tag, 1); 2810038d2ef8SMaor Gottlieb 28112d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_c, 2812038d2ef8SMaor Gottlieb first_vid, ntohs(ib_spec->eth.mask.vlan_tag)); 28132d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_v, 2814038d2ef8SMaor Gottlieb first_vid, ntohs(ib_spec->eth.val.vlan_tag)); 2815038d2ef8SMaor Gottlieb 28162d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_c, 2817038d2ef8SMaor Gottlieb first_cfi, 2818038d2ef8SMaor Gottlieb ntohs(ib_spec->eth.mask.vlan_tag) >> 12); 28192d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_v, 2820038d2ef8SMaor Gottlieb first_cfi, 2821038d2ef8SMaor Gottlieb ntohs(ib_spec->eth.val.vlan_tag) >> 12); 2822038d2ef8SMaor Gottlieb 28232d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_c, 2824038d2ef8SMaor Gottlieb first_prio, 2825038d2ef8SMaor Gottlieb ntohs(ib_spec->eth.mask.vlan_tag) >> 13); 28262d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_v, 2827038d2ef8SMaor Gottlieb first_prio, 2828038d2ef8SMaor Gottlieb ntohs(ib_spec->eth.val.vlan_tag) >> 13); 2829038d2ef8SMaor Gottlieb } 28302d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_c, 2831038d2ef8SMaor Gottlieb ethertype, ntohs(ib_spec->eth.mask.ether_type)); 28322d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_v, 2833038d2ef8SMaor Gottlieb ethertype, ntohs(ib_spec->eth.val.ether_type)); 2834038d2ef8SMaor Gottlieb break; 2835038d2ef8SMaor Gottlieb case IB_FLOW_SPEC_IPV4: 2836c47ac6aeSMaor Gottlieb if (FIELDS_NOT_SUPPORTED(ib_spec->ipv4.mask, LAST_IPV4_FIELD)) 28371ffd3a26SLeon Romanovsky return -EOPNOTSUPP; 2838038d2ef8SMaor Gottlieb 283919cc7524SAriel Levkovich if (match_ipv) { 284019cc7524SAriel Levkovich MLX5_SET(fte_match_set_lyr_2_4, headers_c, 284119cc7524SAriel Levkovich ip_version, 0xf); 284219cc7524SAriel Levkovich MLX5_SET(fte_match_set_lyr_2_4, headers_v, 28433346c487SBoris Pismenny ip_version, MLX5_FS_IPV4_VERSION); 284419cc7524SAriel Levkovich } else { 28452d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_c, 2846038d2ef8SMaor Gottlieb ethertype, 0xffff); 28472d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_v, 2848038d2ef8SMaor Gottlieb ethertype, ETH_P_IP); 284919cc7524SAriel Levkovich } 2850038d2ef8SMaor Gottlieb 28512d1e697eSMoses Reuben memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c, 2852038d2ef8SMaor Gottlieb src_ipv4_src_ipv6.ipv4_layout.ipv4), 2853038d2ef8SMaor Gottlieb &ib_spec->ipv4.mask.src_ip, 2854038d2ef8SMaor Gottlieb sizeof(ib_spec->ipv4.mask.src_ip)); 28552d1e697eSMoses Reuben memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v, 2856038d2ef8SMaor Gottlieb src_ipv4_src_ipv6.ipv4_layout.ipv4), 2857038d2ef8SMaor Gottlieb &ib_spec->ipv4.val.src_ip, 2858038d2ef8SMaor Gottlieb sizeof(ib_spec->ipv4.val.src_ip)); 28592d1e697eSMoses Reuben memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c, 2860038d2ef8SMaor Gottlieb dst_ipv4_dst_ipv6.ipv4_layout.ipv4), 2861038d2ef8SMaor Gottlieb &ib_spec->ipv4.mask.dst_ip, 2862038d2ef8SMaor Gottlieb sizeof(ib_spec->ipv4.mask.dst_ip)); 28632d1e697eSMoses Reuben memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v, 2864038d2ef8SMaor Gottlieb dst_ipv4_dst_ipv6.ipv4_layout.ipv4), 2865038d2ef8SMaor Gottlieb &ib_spec->ipv4.val.dst_ip, 2866038d2ef8SMaor Gottlieb sizeof(ib_spec->ipv4.val.dst_ip)); 2867ca0d4753SMaor Gottlieb 28682d1e697eSMoses Reuben set_tos(headers_c, headers_v, 2869ca0d4753SMaor Gottlieb ib_spec->ipv4.mask.tos, ib_spec->ipv4.val.tos); 2870ca0d4753SMaor Gottlieb 28716113cc44SMaor Gottlieb if (set_proto(headers_c, headers_v, 28726113cc44SMaor Gottlieb ib_spec->ipv4.mask.proto, 28736113cc44SMaor Gottlieb ib_spec->ipv4.val.proto)) 28746113cc44SMaor Gottlieb return -EINVAL; 2875038d2ef8SMaor Gottlieb break; 2876026bae0cSMaor Gottlieb case IB_FLOW_SPEC_IPV6: 2877c47ac6aeSMaor Gottlieb if (FIELDS_NOT_SUPPORTED(ib_spec->ipv6.mask, LAST_IPV6_FIELD)) 28781ffd3a26SLeon Romanovsky return -EOPNOTSUPP; 2879026bae0cSMaor Gottlieb 288019cc7524SAriel Levkovich if (match_ipv) { 288119cc7524SAriel Levkovich MLX5_SET(fte_match_set_lyr_2_4, headers_c, 288219cc7524SAriel Levkovich ip_version, 0xf); 288319cc7524SAriel Levkovich MLX5_SET(fte_match_set_lyr_2_4, headers_v, 28843346c487SBoris Pismenny ip_version, MLX5_FS_IPV6_VERSION); 288519cc7524SAriel Levkovich } else { 28862d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_c, 2887026bae0cSMaor Gottlieb ethertype, 0xffff); 28882d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_v, 2889026bae0cSMaor Gottlieb ethertype, ETH_P_IPV6); 289019cc7524SAriel Levkovich } 2891026bae0cSMaor Gottlieb 28922d1e697eSMoses Reuben memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c, 2893026bae0cSMaor Gottlieb src_ipv4_src_ipv6.ipv6_layout.ipv6), 2894026bae0cSMaor Gottlieb &ib_spec->ipv6.mask.src_ip, 2895026bae0cSMaor Gottlieb sizeof(ib_spec->ipv6.mask.src_ip)); 28962d1e697eSMoses Reuben memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v, 2897026bae0cSMaor Gottlieb src_ipv4_src_ipv6.ipv6_layout.ipv6), 2898026bae0cSMaor Gottlieb &ib_spec->ipv6.val.src_ip, 2899026bae0cSMaor Gottlieb sizeof(ib_spec->ipv6.val.src_ip)); 29002d1e697eSMoses Reuben memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c, 2901026bae0cSMaor Gottlieb dst_ipv4_dst_ipv6.ipv6_layout.ipv6), 2902026bae0cSMaor Gottlieb &ib_spec->ipv6.mask.dst_ip, 2903026bae0cSMaor Gottlieb sizeof(ib_spec->ipv6.mask.dst_ip)); 29042d1e697eSMoses Reuben memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v, 2905026bae0cSMaor Gottlieb dst_ipv4_dst_ipv6.ipv6_layout.ipv6), 2906026bae0cSMaor Gottlieb &ib_spec->ipv6.val.dst_ip, 2907026bae0cSMaor Gottlieb sizeof(ib_spec->ipv6.val.dst_ip)); 2908466fa6d2SMaor Gottlieb 29092d1e697eSMoses Reuben set_tos(headers_c, headers_v, 2910466fa6d2SMaor Gottlieb ib_spec->ipv6.mask.traffic_class, 2911466fa6d2SMaor Gottlieb ib_spec->ipv6.val.traffic_class); 2912466fa6d2SMaor Gottlieb 29136113cc44SMaor Gottlieb if (set_proto(headers_c, headers_v, 2914466fa6d2SMaor Gottlieb ib_spec->ipv6.mask.next_hdr, 29156113cc44SMaor Gottlieb ib_spec->ipv6.val.next_hdr)) 29166113cc44SMaor Gottlieb return -EINVAL; 2917466fa6d2SMaor Gottlieb 29182d1e697eSMoses Reuben set_flow_label(misc_params_c, misc_params_v, 29192d1e697eSMoses Reuben ntohl(ib_spec->ipv6.mask.flow_label), 29202d1e697eSMoses Reuben ntohl(ib_spec->ipv6.val.flow_label), 29212d1e697eSMoses Reuben ib_spec->type & IB_FLOW_SPEC_INNER); 2922802c2125SAviad Yehezkel break; 2923802c2125SAviad Yehezkel case IB_FLOW_SPEC_ESP: 2924802c2125SAviad Yehezkel if (ib_spec->esp.mask.seq) 2925802c2125SAviad Yehezkel return -EOPNOTSUPP; 29262d1e697eSMoses Reuben 2927802c2125SAviad Yehezkel MLX5_SET(fte_match_set_misc, misc_params_c, outer_esp_spi, 2928802c2125SAviad Yehezkel ntohl(ib_spec->esp.mask.spi)); 2929802c2125SAviad Yehezkel MLX5_SET(fte_match_set_misc, misc_params_v, outer_esp_spi, 2930802c2125SAviad Yehezkel ntohl(ib_spec->esp.val.spi)); 2931026bae0cSMaor Gottlieb break; 2932038d2ef8SMaor Gottlieb case IB_FLOW_SPEC_TCP: 2933c47ac6aeSMaor Gottlieb if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask, 2934c47ac6aeSMaor Gottlieb LAST_TCP_UDP_FIELD)) 29351ffd3a26SLeon Romanovsky return -EOPNOTSUPP; 2936038d2ef8SMaor Gottlieb 29376113cc44SMaor Gottlieb if (set_proto(headers_c, headers_v, 0xff, IPPROTO_TCP)) 29386113cc44SMaor Gottlieb return -EINVAL; 2939038d2ef8SMaor Gottlieb 29402d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_sport, 2941038d2ef8SMaor Gottlieb ntohs(ib_spec->tcp_udp.mask.src_port)); 29422d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_sport, 2943038d2ef8SMaor Gottlieb ntohs(ib_spec->tcp_udp.val.src_port)); 2944038d2ef8SMaor Gottlieb 29452d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_dport, 2946038d2ef8SMaor Gottlieb ntohs(ib_spec->tcp_udp.mask.dst_port)); 29472d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_dport, 2948038d2ef8SMaor Gottlieb ntohs(ib_spec->tcp_udp.val.dst_port)); 2949038d2ef8SMaor Gottlieb break; 2950038d2ef8SMaor Gottlieb case IB_FLOW_SPEC_UDP: 2951c47ac6aeSMaor Gottlieb if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask, 2952c47ac6aeSMaor Gottlieb LAST_TCP_UDP_FIELD)) 29531ffd3a26SLeon Romanovsky return -EOPNOTSUPP; 2954038d2ef8SMaor Gottlieb 29556113cc44SMaor Gottlieb if (set_proto(headers_c, headers_v, 0xff, IPPROTO_UDP)) 29566113cc44SMaor Gottlieb return -EINVAL; 2957038d2ef8SMaor Gottlieb 29582d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_sport, 2959038d2ef8SMaor Gottlieb ntohs(ib_spec->tcp_udp.mask.src_port)); 29602d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_sport, 2961038d2ef8SMaor Gottlieb ntohs(ib_spec->tcp_udp.val.src_port)); 2962038d2ef8SMaor Gottlieb 29632d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_dport, 2964038d2ef8SMaor Gottlieb ntohs(ib_spec->tcp_udp.mask.dst_port)); 29652d1e697eSMoses Reuben MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport, 2966038d2ef8SMaor Gottlieb ntohs(ib_spec->tcp_udp.val.dst_port)); 2967038d2ef8SMaor Gottlieb break; 2968da2f22aeSAriel Levkovich case IB_FLOW_SPEC_GRE: 2969da2f22aeSAriel Levkovich if (ib_spec->gre.mask.c_ks_res0_ver) 2970da2f22aeSAriel Levkovich return -EOPNOTSUPP; 2971da2f22aeSAriel Levkovich 29726113cc44SMaor Gottlieb if (set_proto(headers_c, headers_v, 0xff, IPPROTO_GRE)) 29736113cc44SMaor Gottlieb return -EINVAL; 29746113cc44SMaor Gottlieb 2975da2f22aeSAriel Levkovich MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol, 2976da2f22aeSAriel Levkovich 0xff); 2977da2f22aeSAriel Levkovich MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol, 2978da2f22aeSAriel Levkovich IPPROTO_GRE); 2979da2f22aeSAriel Levkovich 2980da2f22aeSAriel Levkovich MLX5_SET(fte_match_set_misc, misc_params_c, gre_protocol, 2981a93b632cSMaor Gottlieb ntohs(ib_spec->gre.mask.protocol)); 2982da2f22aeSAriel Levkovich MLX5_SET(fte_match_set_misc, misc_params_v, gre_protocol, 2983da2f22aeSAriel Levkovich ntohs(ib_spec->gre.val.protocol)); 2984da2f22aeSAriel Levkovich 2985da2f22aeSAriel Levkovich memcpy(MLX5_ADDR_OF(fte_match_set_misc, misc_params_c, 29865886a96aSOz Shlomo gre_key.nvgre.hi), 2987da2f22aeSAriel Levkovich &ib_spec->gre.mask.key, 2988da2f22aeSAriel Levkovich sizeof(ib_spec->gre.mask.key)); 2989da2f22aeSAriel Levkovich memcpy(MLX5_ADDR_OF(fte_match_set_misc, misc_params_v, 29905886a96aSOz Shlomo gre_key.nvgre.hi), 2991da2f22aeSAriel Levkovich &ib_spec->gre.val.key, 2992da2f22aeSAriel Levkovich sizeof(ib_spec->gre.val.key)); 2993da2f22aeSAriel Levkovich break; 299471c6e863SAriel Levkovich case IB_FLOW_SPEC_MPLS: 299571c6e863SAriel Levkovich switch (prev_type) { 299671c6e863SAriel Levkovich case IB_FLOW_SPEC_UDP: 299771c6e863SAriel Levkovich if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev, 299871c6e863SAriel Levkovich ft_field_support.outer_first_mpls_over_udp), 299971c6e863SAriel Levkovich &ib_spec->mpls.mask.tag)) 300071c6e863SAriel Levkovich return -EOPNOTSUPP; 300171c6e863SAriel Levkovich 300271c6e863SAriel Levkovich memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v, 300371c6e863SAriel Levkovich outer_first_mpls_over_udp), 300471c6e863SAriel Levkovich &ib_spec->mpls.val.tag, 300571c6e863SAriel Levkovich sizeof(ib_spec->mpls.val.tag)); 300671c6e863SAriel Levkovich memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c, 300771c6e863SAriel Levkovich outer_first_mpls_over_udp), 300871c6e863SAriel Levkovich &ib_spec->mpls.mask.tag, 300971c6e863SAriel Levkovich sizeof(ib_spec->mpls.mask.tag)); 301071c6e863SAriel Levkovich break; 301171c6e863SAriel Levkovich case IB_FLOW_SPEC_GRE: 301271c6e863SAriel Levkovich if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev, 301371c6e863SAriel Levkovich ft_field_support.outer_first_mpls_over_gre), 301471c6e863SAriel Levkovich &ib_spec->mpls.mask.tag)) 301571c6e863SAriel Levkovich return -EOPNOTSUPP; 301671c6e863SAriel Levkovich 301771c6e863SAriel Levkovich memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v, 301871c6e863SAriel Levkovich outer_first_mpls_over_gre), 301971c6e863SAriel Levkovich &ib_spec->mpls.val.tag, 302071c6e863SAriel Levkovich sizeof(ib_spec->mpls.val.tag)); 302171c6e863SAriel Levkovich memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c, 302271c6e863SAriel Levkovich outer_first_mpls_over_gre), 302371c6e863SAriel Levkovich &ib_spec->mpls.mask.tag, 302471c6e863SAriel Levkovich sizeof(ib_spec->mpls.mask.tag)); 302571c6e863SAriel Levkovich break; 302671c6e863SAriel Levkovich default: 302771c6e863SAriel Levkovich if (ib_spec->type & IB_FLOW_SPEC_INNER) { 302871c6e863SAriel Levkovich if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev, 302971c6e863SAriel Levkovich ft_field_support.inner_first_mpls), 303071c6e863SAriel Levkovich &ib_spec->mpls.mask.tag)) 303171c6e863SAriel Levkovich return -EOPNOTSUPP; 303271c6e863SAriel Levkovich 303371c6e863SAriel Levkovich memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v, 303471c6e863SAriel Levkovich inner_first_mpls), 303571c6e863SAriel Levkovich &ib_spec->mpls.val.tag, 303671c6e863SAriel Levkovich sizeof(ib_spec->mpls.val.tag)); 303771c6e863SAriel Levkovich memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c, 303871c6e863SAriel Levkovich inner_first_mpls), 303971c6e863SAriel Levkovich &ib_spec->mpls.mask.tag, 304071c6e863SAriel Levkovich sizeof(ib_spec->mpls.mask.tag)); 304171c6e863SAriel Levkovich } else { 304271c6e863SAriel Levkovich if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev, 304371c6e863SAriel Levkovich ft_field_support.outer_first_mpls), 304471c6e863SAriel Levkovich &ib_spec->mpls.mask.tag)) 304571c6e863SAriel Levkovich return -EOPNOTSUPP; 304671c6e863SAriel Levkovich 304771c6e863SAriel Levkovich memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v, 304871c6e863SAriel Levkovich outer_first_mpls), 304971c6e863SAriel Levkovich &ib_spec->mpls.val.tag, 305071c6e863SAriel Levkovich sizeof(ib_spec->mpls.val.tag)); 305171c6e863SAriel Levkovich memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c, 305271c6e863SAriel Levkovich outer_first_mpls), 305371c6e863SAriel Levkovich &ib_spec->mpls.mask.tag, 305471c6e863SAriel Levkovich sizeof(ib_spec->mpls.mask.tag)); 305571c6e863SAriel Levkovich } 305671c6e863SAriel Levkovich } 305771c6e863SAriel Levkovich break; 3058ffb30d8fSMoses Reuben case IB_FLOW_SPEC_VXLAN_TUNNEL: 3059ffb30d8fSMoses Reuben if (FIELDS_NOT_SUPPORTED(ib_spec->tunnel.mask, 3060ffb30d8fSMoses Reuben LAST_TUNNEL_FIELD)) 30611ffd3a26SLeon Romanovsky return -EOPNOTSUPP; 3062ffb30d8fSMoses Reuben 3063ffb30d8fSMoses Reuben MLX5_SET(fte_match_set_misc, misc_params_c, vxlan_vni, 3064ffb30d8fSMoses Reuben ntohl(ib_spec->tunnel.mask.tunnel_id)); 3065ffb30d8fSMoses Reuben MLX5_SET(fte_match_set_misc, misc_params_v, vxlan_vni, 3066ffb30d8fSMoses Reuben ntohl(ib_spec->tunnel.val.tunnel_id)); 3067ffb30d8fSMoses Reuben break; 30682ac693f9SMoses Reuben case IB_FLOW_SPEC_ACTION_TAG: 30692ac693f9SMoses Reuben if (FIELDS_NOT_SUPPORTED(ib_spec->flow_tag, 30702ac693f9SMoses Reuben LAST_FLOW_TAG_FIELD)) 30712ac693f9SMoses Reuben return -EOPNOTSUPP; 30722ac693f9SMoses Reuben if (ib_spec->flow_tag.tag_id >= BIT(24)) 30732ac693f9SMoses Reuben return -EINVAL; 30742ac693f9SMoses Reuben 3075bb0ee7dcSJianbo Liu flow_context->flow_tag = ib_spec->flow_tag.tag_id; 3076bb0ee7dcSJianbo Liu flow_context->flags |= FLOW_CONTEXT_HAS_TAG; 30772ac693f9SMoses Reuben break; 3078a22ed86cSSlava Shwartsman case IB_FLOW_SPEC_ACTION_DROP: 3079a22ed86cSSlava Shwartsman if (FIELDS_NOT_SUPPORTED(ib_spec->drop, 3080a22ed86cSSlava Shwartsman LAST_DROP_FIELD)) 3081a22ed86cSSlava Shwartsman return -EOPNOTSUPP; 3082075572d4SBoris Pismenny action->action |= MLX5_FLOW_CONTEXT_ACTION_DROP; 3083a22ed86cSSlava Shwartsman break; 3084802c2125SAviad Yehezkel case IB_FLOW_SPEC_ACTION_HANDLE: 30852ea26203SMark Bloch ret = parse_flow_flow_action(to_mflow_act(ib_spec->action.act), 30862ea26203SMark Bloch flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS, action); 3087802c2125SAviad Yehezkel if (ret) 3088802c2125SAviad Yehezkel return ret; 3089802c2125SAviad Yehezkel break; 30903b3233fbSRaed Salem case IB_FLOW_SPEC_ACTION_COUNT: 30913b3233fbSRaed Salem if (FIELDS_NOT_SUPPORTED(ib_spec->flow_count, 30923b3233fbSRaed Salem LAST_COUNTERS_FIELD)) 30933b3233fbSRaed Salem return -EOPNOTSUPP; 30943b3233fbSRaed Salem 30953b3233fbSRaed Salem /* for now support only one counters spec per flow */ 30963b3233fbSRaed Salem if (action->action & MLX5_FLOW_CONTEXT_ACTION_COUNT) 30973b3233fbSRaed Salem return -EINVAL; 30983b3233fbSRaed Salem 30993b3233fbSRaed Salem action->counters = ib_spec->flow_count.counters; 31003b3233fbSRaed Salem action->action |= MLX5_FLOW_CONTEXT_ACTION_COUNT; 31013b3233fbSRaed Salem break; 3102038d2ef8SMaor Gottlieb default: 3103038d2ef8SMaor Gottlieb return -EINVAL; 3104038d2ef8SMaor Gottlieb } 3105038d2ef8SMaor Gottlieb 3106038d2ef8SMaor Gottlieb return 0; 3107038d2ef8SMaor Gottlieb } 3108038d2ef8SMaor Gottlieb 3109038d2ef8SMaor Gottlieb /* If a flow could catch both multicast and unicast packets, 3110038d2ef8SMaor Gottlieb * it won't fall into the multicast flow steering table and this rule 3111038d2ef8SMaor Gottlieb * could steal other multicast packets. 3112038d2ef8SMaor Gottlieb */ 3113a550ddfcSYishai Hadas static bool flow_is_multicast_only(const struct ib_flow_attr *ib_attr) 3114038d2ef8SMaor Gottlieb { 311581e30880SYishai Hadas union ib_flow_spec *flow_spec; 3116038d2ef8SMaor Gottlieb 3117038d2ef8SMaor Gottlieb if (ib_attr->type != IB_FLOW_ATTR_NORMAL || 3118038d2ef8SMaor Gottlieb ib_attr->num_of_specs < 1) 3119038d2ef8SMaor Gottlieb return false; 3120038d2ef8SMaor Gottlieb 312181e30880SYishai Hadas flow_spec = (union ib_flow_spec *)(ib_attr + 1); 312281e30880SYishai Hadas if (flow_spec->type == IB_FLOW_SPEC_IPV4) { 312381e30880SYishai Hadas struct ib_flow_spec_ipv4 *ipv4_spec; 3124038d2ef8SMaor Gottlieb 312581e30880SYishai Hadas ipv4_spec = (struct ib_flow_spec_ipv4 *)flow_spec; 312681e30880SYishai Hadas if (ipv4_is_multicast(ipv4_spec->val.dst_ip)) 312781e30880SYishai Hadas return true; 312881e30880SYishai Hadas 312981e30880SYishai Hadas return false; 313081e30880SYishai Hadas } 313181e30880SYishai Hadas 313281e30880SYishai Hadas if (flow_spec->type == IB_FLOW_SPEC_ETH) { 313381e30880SYishai Hadas struct ib_flow_spec_eth *eth_spec; 313481e30880SYishai Hadas 313581e30880SYishai Hadas eth_spec = (struct ib_flow_spec_eth *)flow_spec; 3136038d2ef8SMaor Gottlieb return is_multicast_ether_addr(eth_spec->mask.dst_mac) && 3137038d2ef8SMaor Gottlieb is_multicast_ether_addr(eth_spec->val.dst_mac); 3138038d2ef8SMaor Gottlieb } 3139038d2ef8SMaor Gottlieb 314081e30880SYishai Hadas return false; 314181e30880SYishai Hadas } 314281e30880SYishai Hadas 3143802c2125SAviad Yehezkel enum valid_spec { 3144802c2125SAviad Yehezkel VALID_SPEC_INVALID, 3145802c2125SAviad Yehezkel VALID_SPEC_VALID, 3146802c2125SAviad Yehezkel VALID_SPEC_NA, 3147802c2125SAviad Yehezkel }; 3148802c2125SAviad Yehezkel 3149802c2125SAviad Yehezkel static enum valid_spec 3150802c2125SAviad Yehezkel is_valid_esp_aes_gcm(struct mlx5_core_dev *mdev, 3151802c2125SAviad Yehezkel const struct mlx5_flow_spec *spec, 3152802c2125SAviad Yehezkel const struct mlx5_flow_act *flow_act, 3153802c2125SAviad Yehezkel bool egress) 3154802c2125SAviad Yehezkel { 3155802c2125SAviad Yehezkel const u32 *match_c = spec->match_criteria; 3156802c2125SAviad Yehezkel bool is_crypto = 3157802c2125SAviad Yehezkel (flow_act->action & (MLX5_FLOW_CONTEXT_ACTION_ENCRYPT | 3158802c2125SAviad Yehezkel MLX5_FLOW_CONTEXT_ACTION_DECRYPT)); 3159802c2125SAviad Yehezkel bool is_ipsec = mlx5_fs_is_ipsec_flow(match_c); 3160802c2125SAviad Yehezkel bool is_drop = flow_act->action & MLX5_FLOW_CONTEXT_ACTION_DROP; 3161802c2125SAviad Yehezkel 3162802c2125SAviad Yehezkel /* 3163802c2125SAviad Yehezkel * Currently only crypto is supported in egress, when regular egress 3164802c2125SAviad Yehezkel * rules would be supported, always return VALID_SPEC_NA. 3165802c2125SAviad Yehezkel */ 3166802c2125SAviad Yehezkel if (!is_crypto) 316778dd0c43SMark Bloch return VALID_SPEC_NA; 3168802c2125SAviad Yehezkel 3169802c2125SAviad Yehezkel return is_crypto && is_ipsec && 3170bb0ee7dcSJianbo Liu (!egress || (!is_drop && 3171bb0ee7dcSJianbo Liu !(spec->flow_context.flags & FLOW_CONTEXT_HAS_TAG))) ? 3172802c2125SAviad Yehezkel VALID_SPEC_VALID : VALID_SPEC_INVALID; 3173802c2125SAviad Yehezkel } 3174802c2125SAviad Yehezkel 3175802c2125SAviad Yehezkel static bool is_valid_spec(struct mlx5_core_dev *mdev, 3176802c2125SAviad Yehezkel const struct mlx5_flow_spec *spec, 3177802c2125SAviad Yehezkel const struct mlx5_flow_act *flow_act, 3178802c2125SAviad Yehezkel bool egress) 3179802c2125SAviad Yehezkel { 3180802c2125SAviad Yehezkel /* We curretly only support ipsec egress flow */ 3181802c2125SAviad Yehezkel return is_valid_esp_aes_gcm(mdev, spec, flow_act, egress) != VALID_SPEC_INVALID; 3182802c2125SAviad Yehezkel } 3183802c2125SAviad Yehezkel 318419cc7524SAriel Levkovich static bool is_valid_ethertype(struct mlx5_core_dev *mdev, 318519cc7524SAriel Levkovich const struct ib_flow_attr *flow_attr, 31860f750966SAriel Levkovich bool check_inner) 3187038d2ef8SMaor Gottlieb { 3188038d2ef8SMaor Gottlieb union ib_flow_spec *ib_spec = (union ib_flow_spec *)(flow_attr + 1); 318919cc7524SAriel Levkovich int match_ipv = check_inner ? 319019cc7524SAriel Levkovich MLX5_CAP_FLOWTABLE_NIC_RX(mdev, 319119cc7524SAriel Levkovich ft_field_support.inner_ip_version) : 319219cc7524SAriel Levkovich MLX5_CAP_FLOWTABLE_NIC_RX(mdev, 319319cc7524SAriel Levkovich ft_field_support.outer_ip_version); 31940f750966SAriel Levkovich int inner_bit = check_inner ? IB_FLOW_SPEC_INNER : 0; 31950f750966SAriel Levkovich bool ipv4_spec_valid, ipv6_spec_valid; 31960f750966SAriel Levkovich unsigned int ip_spec_type = 0; 31970f750966SAriel Levkovich bool has_ethertype = false; 3198038d2ef8SMaor Gottlieb unsigned int spec_index; 31990f750966SAriel Levkovich bool mask_valid = true; 32000f750966SAriel Levkovich u16 eth_type = 0; 32010f750966SAriel Levkovich bool type_valid; 3202038d2ef8SMaor Gottlieb 3203038d2ef8SMaor Gottlieb /* Validate that ethertype is correct */ 3204038d2ef8SMaor Gottlieb for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) { 32050f750966SAriel Levkovich if ((ib_spec->type == (IB_FLOW_SPEC_ETH | inner_bit)) && 3206038d2ef8SMaor Gottlieb ib_spec->eth.mask.ether_type) { 32070f750966SAriel Levkovich mask_valid = (ib_spec->eth.mask.ether_type == 32080f750966SAriel Levkovich htons(0xffff)); 32090f750966SAriel Levkovich has_ethertype = true; 32100f750966SAriel Levkovich eth_type = ntohs(ib_spec->eth.val.ether_type); 32110f750966SAriel Levkovich } else if ((ib_spec->type == (IB_FLOW_SPEC_IPV4 | inner_bit)) || 32120f750966SAriel Levkovich (ib_spec->type == (IB_FLOW_SPEC_IPV6 | inner_bit))) { 32130f750966SAriel Levkovich ip_spec_type = ib_spec->type; 3214038d2ef8SMaor Gottlieb } 3215038d2ef8SMaor Gottlieb ib_spec = (void *)ib_spec + ib_spec->size; 3216038d2ef8SMaor Gottlieb } 32170f750966SAriel Levkovich 32180f750966SAriel Levkovich type_valid = (!has_ethertype) || (!ip_spec_type); 32190f750966SAriel Levkovich if (!type_valid && mask_valid) { 32200f750966SAriel Levkovich ipv4_spec_valid = (eth_type == ETH_P_IP) && 32210f750966SAriel Levkovich (ip_spec_type == (IB_FLOW_SPEC_IPV4 | inner_bit)); 32220f750966SAriel Levkovich ipv6_spec_valid = (eth_type == ETH_P_IPV6) && 32230f750966SAriel Levkovich (ip_spec_type == (IB_FLOW_SPEC_IPV6 | inner_bit)); 322419cc7524SAriel Levkovich 322519cc7524SAriel Levkovich type_valid = (ipv4_spec_valid) || (ipv6_spec_valid) || 322619cc7524SAriel Levkovich (((eth_type == ETH_P_MPLS_UC) || 322719cc7524SAriel Levkovich (eth_type == ETH_P_MPLS_MC)) && match_ipv); 32280f750966SAriel Levkovich } 32290f750966SAriel Levkovich 32300f750966SAriel Levkovich return type_valid; 32310f750966SAriel Levkovich } 32320f750966SAriel Levkovich 323319cc7524SAriel Levkovich static bool is_valid_attr(struct mlx5_core_dev *mdev, 323419cc7524SAriel Levkovich const struct ib_flow_attr *flow_attr) 32350f750966SAriel Levkovich { 323619cc7524SAriel Levkovich return is_valid_ethertype(mdev, flow_attr, false) && 323719cc7524SAriel Levkovich is_valid_ethertype(mdev, flow_attr, true); 3238038d2ef8SMaor Gottlieb } 3239038d2ef8SMaor Gottlieb 3240038d2ef8SMaor Gottlieb static void put_flow_table(struct mlx5_ib_dev *dev, 3241038d2ef8SMaor Gottlieb struct mlx5_ib_flow_prio *prio, bool ft_added) 3242038d2ef8SMaor Gottlieb { 3243038d2ef8SMaor Gottlieb prio->refcount -= !!ft_added; 3244038d2ef8SMaor Gottlieb if (!prio->refcount) { 3245038d2ef8SMaor Gottlieb mlx5_destroy_flow_table(prio->flow_table); 3246038d2ef8SMaor Gottlieb prio->flow_table = NULL; 3247038d2ef8SMaor Gottlieb } 3248038d2ef8SMaor Gottlieb } 3249038d2ef8SMaor Gottlieb 32503b3233fbSRaed Salem static void counters_clear_description(struct ib_counters *counters) 32513b3233fbSRaed Salem { 32523b3233fbSRaed Salem struct mlx5_ib_mcounters *mcounters = to_mcounters(counters); 32533b3233fbSRaed Salem 32543b3233fbSRaed Salem mutex_lock(&mcounters->mcntrs_mutex); 32553b3233fbSRaed Salem kfree(mcounters->counters_data); 32563b3233fbSRaed Salem mcounters->counters_data = NULL; 32573b3233fbSRaed Salem mcounters->cntrs_max_index = 0; 32583b3233fbSRaed Salem mutex_unlock(&mcounters->mcntrs_mutex); 32593b3233fbSRaed Salem } 32603b3233fbSRaed Salem 3261038d2ef8SMaor Gottlieb static int mlx5_ib_destroy_flow(struct ib_flow *flow_id) 3262038d2ef8SMaor Gottlieb { 3263038d2ef8SMaor Gottlieb struct mlx5_ib_flow_handler *handler = container_of(flow_id, 3264038d2ef8SMaor Gottlieb struct mlx5_ib_flow_handler, 3265038d2ef8SMaor Gottlieb ibflow); 3266038d2ef8SMaor Gottlieb struct mlx5_ib_flow_handler *iter, *tmp; 3267d4be3f44SYishai Hadas struct mlx5_ib_dev *dev = handler->dev; 3268038d2ef8SMaor Gottlieb 32699a4ca38dSMark Bloch mutex_lock(&dev->flow_db->lock); 3270038d2ef8SMaor Gottlieb 3271038d2ef8SMaor Gottlieb list_for_each_entry_safe(iter, tmp, &handler->list, list) { 327274491de9SMark Bloch mlx5_del_flow_rules(iter->rule); 3273cc0e5d42SMaor Gottlieb put_flow_table(dev, iter->prio, true); 3274038d2ef8SMaor Gottlieb list_del(&iter->list); 3275038d2ef8SMaor Gottlieb kfree(iter); 3276038d2ef8SMaor Gottlieb } 3277038d2ef8SMaor Gottlieb 327874491de9SMark Bloch mlx5_del_flow_rules(handler->rule); 32795497adc6SMaor Gottlieb put_flow_table(dev, handler->prio, true); 32803b3233fbSRaed Salem if (handler->ibcounters && 32813b3233fbSRaed Salem atomic_read(&handler->ibcounters->usecnt) == 1) 32823b3233fbSRaed Salem counters_clear_description(handler->ibcounters); 3283038d2ef8SMaor Gottlieb 32843b3233fbSRaed Salem mutex_unlock(&dev->flow_db->lock); 3285d4be3f44SYishai Hadas if (handler->flow_matcher) 3286d4be3f44SYishai Hadas atomic_dec(&handler->flow_matcher->usecnt); 3287038d2ef8SMaor Gottlieb kfree(handler); 3288038d2ef8SMaor Gottlieb 3289038d2ef8SMaor Gottlieb return 0; 3290038d2ef8SMaor Gottlieb } 3291038d2ef8SMaor Gottlieb 329235d19011SMaor Gottlieb static int ib_prio_to_core_prio(unsigned int priority, bool dont_trap) 329335d19011SMaor Gottlieb { 329435d19011SMaor Gottlieb priority *= 2; 329535d19011SMaor Gottlieb if (!dont_trap) 329635d19011SMaor Gottlieb priority++; 329735d19011SMaor Gottlieb return priority; 329835d19011SMaor Gottlieb } 329935d19011SMaor Gottlieb 3300cc0e5d42SMaor Gottlieb enum flow_table_type { 3301cc0e5d42SMaor Gottlieb MLX5_IB_FT_RX, 3302cc0e5d42SMaor Gottlieb MLX5_IB_FT_TX 3303cc0e5d42SMaor Gottlieb }; 3304cc0e5d42SMaor Gottlieb 330500b7c2abSMaor Gottlieb #define MLX5_FS_MAX_TYPES 6 330600b7c2abSMaor Gottlieb #define MLX5_FS_MAX_ENTRIES BIT(16) 3307d4be3f44SYishai Hadas 3308d4be3f44SYishai Hadas static struct mlx5_ib_flow_prio *_get_prio(struct mlx5_flow_namespace *ns, 3309d4be3f44SYishai Hadas struct mlx5_ib_flow_prio *prio, 3310d4be3f44SYishai Hadas int priority, 33114adda112SMark Bloch int num_entries, int num_groups, 33124adda112SMark Bloch u32 flags) 3313d4be3f44SYishai Hadas { 331461dc7b01SPaul Blakey struct mlx5_flow_table_attr ft_attr = {}; 3315d4be3f44SYishai Hadas struct mlx5_flow_table *ft; 3316d4be3f44SYishai Hadas 331761dc7b01SPaul Blakey ft_attr.prio = priority; 331861dc7b01SPaul Blakey ft_attr.max_fte = num_entries; 331961dc7b01SPaul Blakey ft_attr.flags = flags; 332061dc7b01SPaul Blakey ft_attr.autogroup.max_num_groups = num_groups; 332161dc7b01SPaul Blakey ft = mlx5_create_auto_grouped_flow_table(ns, &ft_attr); 3322d4be3f44SYishai Hadas if (IS_ERR(ft)) 3323d4be3f44SYishai Hadas return ERR_CAST(ft); 3324d4be3f44SYishai Hadas 3325d4be3f44SYishai Hadas prio->flow_table = ft; 3326d4be3f44SYishai Hadas prio->refcount = 0; 3327d4be3f44SYishai Hadas return prio; 3328d4be3f44SYishai Hadas } 3329d4be3f44SYishai Hadas 3330038d2ef8SMaor Gottlieb static struct mlx5_ib_flow_prio *get_flow_table(struct mlx5_ib_dev *dev, 3331cc0e5d42SMaor Gottlieb struct ib_flow_attr *flow_attr, 3332cc0e5d42SMaor Gottlieb enum flow_table_type ft_type) 3333038d2ef8SMaor Gottlieb { 333435d19011SMaor Gottlieb bool dont_trap = flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP; 3335038d2ef8SMaor Gottlieb struct mlx5_flow_namespace *ns = NULL; 3336038d2ef8SMaor Gottlieb struct mlx5_ib_flow_prio *prio; 3337038d2ef8SMaor Gottlieb struct mlx5_flow_table *ft; 3338dac388efSMaor Gottlieb int max_table_size; 3339038d2ef8SMaor Gottlieb int num_entries; 3340038d2ef8SMaor Gottlieb int num_groups; 3341cecae747SMaor Gottlieb bool esw_encap; 33424adda112SMark Bloch u32 flags = 0; 3343038d2ef8SMaor Gottlieb int priority; 3344038d2ef8SMaor Gottlieb 3345dac388efSMaor Gottlieb max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, 3346dac388efSMaor Gottlieb log_max_ft_size)); 3347cecae747SMaor Gottlieb esw_encap = mlx5_eswitch_get_encap_mode(dev->mdev) != 3348cecae747SMaor Gottlieb DEVLINK_ESWITCH_ENCAP_MODE_NONE; 3349038d2ef8SMaor Gottlieb if (flow_attr->type == IB_FLOW_ATTR_NORMAL) { 335078dd0c43SMark Bloch enum mlx5_flow_namespace_type fn_type; 335178dd0c43SMark Bloch 335278dd0c43SMark Bloch if (flow_is_multicast_only(flow_attr) && 335335d19011SMaor Gottlieb !dont_trap) 3354038d2ef8SMaor Gottlieb priority = MLX5_IB_FLOW_MCAST_PRIO; 3355038d2ef8SMaor Gottlieb else 335635d19011SMaor Gottlieb priority = ib_prio_to_core_prio(flow_attr->priority, 335735d19011SMaor Gottlieb dont_trap); 335878dd0c43SMark Bloch if (ft_type == MLX5_IB_FT_RX) { 335978dd0c43SMark Bloch fn_type = MLX5_FLOW_NAMESPACE_BYPASS; 336078dd0c43SMark Bloch prio = &dev->flow_db->prios[priority]; 3361cecae747SMaor Gottlieb if (!dev->is_rep && !esw_encap && 33624adda112SMark Bloch MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, decap)) 33634adda112SMark Bloch flags |= MLX5_FLOW_TABLE_TUNNEL_EN_DECAP; 3364cecae747SMaor Gottlieb if (!dev->is_rep && !esw_encap && 33655c2db53fSMark Bloch MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, 33665c2db53fSMark Bloch reformat_l3_tunnel_to_l2)) 33675c2db53fSMark Bloch flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT; 336878dd0c43SMark Bloch } else { 336978dd0c43SMark Bloch max_table_size = 337078dd0c43SMark Bloch BIT(MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev, 337178dd0c43SMark Bloch log_max_ft_size)); 337278dd0c43SMark Bloch fn_type = MLX5_FLOW_NAMESPACE_EGRESS; 337378dd0c43SMark Bloch prio = &dev->flow_db->egress_prios[priority]; 3374cecae747SMaor Gottlieb if (!dev->is_rep && !esw_encap && 33754adda112SMark Bloch MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev, reformat)) 33764adda112SMark Bloch flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT; 337778dd0c43SMark Bloch } 337878dd0c43SMark Bloch ns = mlx5_get_flow_namespace(dev->mdev, fn_type); 3379038d2ef8SMaor Gottlieb num_entries = MLX5_FS_MAX_ENTRIES; 3380038d2ef8SMaor Gottlieb num_groups = MLX5_FS_MAX_TYPES; 3381038d2ef8SMaor Gottlieb } else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT || 3382038d2ef8SMaor Gottlieb flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) { 3383038d2ef8SMaor Gottlieb ns = mlx5_get_flow_namespace(dev->mdev, 3384038d2ef8SMaor Gottlieb MLX5_FLOW_NAMESPACE_LEFTOVERS); 3385038d2ef8SMaor Gottlieb build_leftovers_ft_param(&priority, 3386038d2ef8SMaor Gottlieb &num_entries, 3387038d2ef8SMaor Gottlieb &num_groups); 33889a4ca38dSMark Bloch prio = &dev->flow_db->prios[MLX5_IB_FLOW_LEFTOVERS_PRIO]; 3389cc0e5d42SMaor Gottlieb } else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) { 3390cc0e5d42SMaor Gottlieb if (!MLX5_CAP_FLOWTABLE(dev->mdev, 3391cc0e5d42SMaor Gottlieb allow_sniffer_and_nic_rx_shared_tir)) 3392cc0e5d42SMaor Gottlieb return ERR_PTR(-ENOTSUPP); 3393cc0e5d42SMaor Gottlieb 3394cc0e5d42SMaor Gottlieb ns = mlx5_get_flow_namespace(dev->mdev, ft_type == MLX5_IB_FT_RX ? 3395cc0e5d42SMaor Gottlieb MLX5_FLOW_NAMESPACE_SNIFFER_RX : 3396cc0e5d42SMaor Gottlieb MLX5_FLOW_NAMESPACE_SNIFFER_TX); 3397cc0e5d42SMaor Gottlieb 33989a4ca38dSMark Bloch prio = &dev->flow_db->sniffer[ft_type]; 3399cc0e5d42SMaor Gottlieb priority = 0; 3400cc0e5d42SMaor Gottlieb num_entries = 1; 3401cc0e5d42SMaor Gottlieb num_groups = 1; 3402038d2ef8SMaor Gottlieb } 3403038d2ef8SMaor Gottlieb 3404038d2ef8SMaor Gottlieb if (!ns) 3405038d2ef8SMaor Gottlieb return ERR_PTR(-ENOTSUPP); 3406038d2ef8SMaor Gottlieb 34073b70508aSMark Bloch max_table_size = min_t(int, num_entries, max_table_size); 3408dac388efSMaor Gottlieb 3409038d2ef8SMaor Gottlieb ft = prio->flow_table; 3410d4be3f44SYishai Hadas if (!ft) 34113b70508aSMark Bloch return _get_prio(ns, prio, priority, max_table_size, num_groups, 34124adda112SMark Bloch flags); 3413038d2ef8SMaor Gottlieb 3414d4be3f44SYishai Hadas return prio; 3415038d2ef8SMaor Gottlieb } 3416038d2ef8SMaor Gottlieb 3417a550ddfcSYishai Hadas static void set_underlay_qp(struct mlx5_ib_dev *dev, 3418a550ddfcSYishai Hadas struct mlx5_flow_spec *spec, 3419a550ddfcSYishai Hadas u32 underlay_qpn) 3420a550ddfcSYishai Hadas { 3421a550ddfcSYishai Hadas void *misc_params_c = MLX5_ADDR_OF(fte_match_param, 3422a550ddfcSYishai Hadas spec->match_criteria, 3423a550ddfcSYishai Hadas misc_parameters); 3424a550ddfcSYishai Hadas void *misc_params_v = MLX5_ADDR_OF(fte_match_param, spec->match_value, 3425a550ddfcSYishai Hadas misc_parameters); 3426a550ddfcSYishai Hadas 3427a550ddfcSYishai Hadas if (underlay_qpn && 3428a550ddfcSYishai Hadas MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, 3429a550ddfcSYishai Hadas ft_field_support.bth_dst_qp)) { 3430a550ddfcSYishai Hadas MLX5_SET(fte_match_set_misc, 3431a550ddfcSYishai Hadas misc_params_v, bth_dst_qp, underlay_qpn); 3432a550ddfcSYishai Hadas MLX5_SET(fte_match_set_misc, 3433a550ddfcSYishai Hadas misc_params_c, bth_dst_qp, 0xffffff); 3434a550ddfcSYishai Hadas } 3435a550ddfcSYishai Hadas } 3436a550ddfcSYishai Hadas 34375e95af5fSRaed Salem static int read_flow_counters(struct ib_device *ibdev, 34385e95af5fSRaed Salem struct mlx5_read_counters_attr *read_attr) 34395e95af5fSRaed Salem { 34405e95af5fSRaed Salem struct mlx5_fc *fc = read_attr->hw_cntrs_hndl; 34415e95af5fSRaed Salem struct mlx5_ib_dev *dev = to_mdev(ibdev); 34425e95af5fSRaed Salem 34435e95af5fSRaed Salem return mlx5_fc_query(dev->mdev, fc, 34445e95af5fSRaed Salem &read_attr->out[IB_COUNTER_PACKETS], 34455e95af5fSRaed Salem &read_attr->out[IB_COUNTER_BYTES]); 34465e95af5fSRaed Salem } 34475e95af5fSRaed Salem 34485e95af5fSRaed Salem /* flow counters currently expose two counters packets and bytes */ 34495e95af5fSRaed Salem #define FLOW_COUNTERS_NUM 2 34503b3233fbSRaed Salem static int counters_set_description(struct ib_counters *counters, 34513b3233fbSRaed Salem enum mlx5_ib_counters_type counters_type, 34523b3233fbSRaed Salem struct mlx5_ib_flow_counters_desc *desc_data, 34533b3233fbSRaed Salem u32 ncounters) 34543b3233fbSRaed Salem { 34553b3233fbSRaed Salem struct mlx5_ib_mcounters *mcounters = to_mcounters(counters); 34563b3233fbSRaed Salem u32 cntrs_max_index = 0; 34573b3233fbSRaed Salem int i; 34583b3233fbSRaed Salem 34593b3233fbSRaed Salem if (counters_type != MLX5_IB_COUNTERS_FLOW) 34603b3233fbSRaed Salem return -EINVAL; 34613b3233fbSRaed Salem 34623b3233fbSRaed Salem /* init the fields for the object */ 34633b3233fbSRaed Salem mcounters->type = counters_type; 34645e95af5fSRaed Salem mcounters->read_counters = read_flow_counters; 34655e95af5fSRaed Salem mcounters->counters_num = FLOW_COUNTERS_NUM; 34663b3233fbSRaed Salem mcounters->ncounters = ncounters; 34673b3233fbSRaed Salem /* each counter entry have both description and index pair */ 34683b3233fbSRaed Salem for (i = 0; i < ncounters; i++) { 34693b3233fbSRaed Salem if (desc_data[i].description > IB_COUNTER_BYTES) 34703b3233fbSRaed Salem return -EINVAL; 34713b3233fbSRaed Salem 34723b3233fbSRaed Salem if (cntrs_max_index <= desc_data[i].index) 34733b3233fbSRaed Salem cntrs_max_index = desc_data[i].index + 1; 34743b3233fbSRaed Salem } 34753b3233fbSRaed Salem 34763b3233fbSRaed Salem mutex_lock(&mcounters->mcntrs_mutex); 34773b3233fbSRaed Salem mcounters->counters_data = desc_data; 34783b3233fbSRaed Salem mcounters->cntrs_max_index = cntrs_max_index; 34793b3233fbSRaed Salem mutex_unlock(&mcounters->mcntrs_mutex); 34803b3233fbSRaed Salem 34813b3233fbSRaed Salem return 0; 34823b3233fbSRaed Salem } 34833b3233fbSRaed Salem 34843b3233fbSRaed Salem #define MAX_COUNTERS_NUM (USHRT_MAX / (sizeof(u32) * 2)) 34853b3233fbSRaed Salem static int flow_counters_set_data(struct ib_counters *ibcounters, 34863b3233fbSRaed Salem struct mlx5_ib_create_flow *ucmd) 34873b3233fbSRaed Salem { 34883b3233fbSRaed Salem struct mlx5_ib_mcounters *mcounters = to_mcounters(ibcounters); 34893b3233fbSRaed Salem struct mlx5_ib_flow_counters_data *cntrs_data = NULL; 34903b3233fbSRaed Salem struct mlx5_ib_flow_counters_desc *desc_data = NULL; 34913b3233fbSRaed Salem bool hw_hndl = false; 34923b3233fbSRaed Salem int ret = 0; 34933b3233fbSRaed Salem 34943b3233fbSRaed Salem if (ucmd && ucmd->ncounters_data != 0) { 34953b3233fbSRaed Salem cntrs_data = ucmd->data; 34963b3233fbSRaed Salem if (cntrs_data->ncounters > MAX_COUNTERS_NUM) 34973b3233fbSRaed Salem return -EINVAL; 34983b3233fbSRaed Salem 34993b3233fbSRaed Salem desc_data = kcalloc(cntrs_data->ncounters, 35003b3233fbSRaed Salem sizeof(*desc_data), 35013b3233fbSRaed Salem GFP_KERNEL); 35023b3233fbSRaed Salem if (!desc_data) 35033b3233fbSRaed Salem return -ENOMEM; 35043b3233fbSRaed Salem 35053b3233fbSRaed Salem if (copy_from_user(desc_data, 35063b3233fbSRaed Salem u64_to_user_ptr(cntrs_data->counters_data), 35073b3233fbSRaed Salem sizeof(*desc_data) * cntrs_data->ncounters)) { 35083b3233fbSRaed Salem ret = -EFAULT; 35093b3233fbSRaed Salem goto free; 35103b3233fbSRaed Salem } 35113b3233fbSRaed Salem } 35123b3233fbSRaed Salem 35133b3233fbSRaed Salem if (!mcounters->hw_cntrs_hndl) { 35143b3233fbSRaed Salem mcounters->hw_cntrs_hndl = mlx5_fc_create( 35153b3233fbSRaed Salem to_mdev(ibcounters->device)->mdev, false); 3516e31abf76Sweiyongjun (A) if (IS_ERR(mcounters->hw_cntrs_hndl)) { 3517e31abf76Sweiyongjun (A) ret = PTR_ERR(mcounters->hw_cntrs_hndl); 35183b3233fbSRaed Salem goto free; 35193b3233fbSRaed Salem } 35203b3233fbSRaed Salem hw_hndl = true; 35213b3233fbSRaed Salem } 35223b3233fbSRaed Salem 35233b3233fbSRaed Salem if (desc_data) { 35243b3233fbSRaed Salem /* counters already bound to at least one flow */ 35253b3233fbSRaed Salem if (mcounters->cntrs_max_index) { 35263b3233fbSRaed Salem ret = -EINVAL; 35273b3233fbSRaed Salem goto free_hndl; 35283b3233fbSRaed Salem } 35293b3233fbSRaed Salem 35303b3233fbSRaed Salem ret = counters_set_description(ibcounters, 35313b3233fbSRaed Salem MLX5_IB_COUNTERS_FLOW, 35323b3233fbSRaed Salem desc_data, 35333b3233fbSRaed Salem cntrs_data->ncounters); 35343b3233fbSRaed Salem if (ret) 35353b3233fbSRaed Salem goto free_hndl; 35363b3233fbSRaed Salem 35373b3233fbSRaed Salem } else if (!mcounters->cntrs_max_index) { 35383b3233fbSRaed Salem /* counters not bound yet, must have udata passed */ 35393b3233fbSRaed Salem ret = -EINVAL; 35403b3233fbSRaed Salem goto free_hndl; 35413b3233fbSRaed Salem } 35423b3233fbSRaed Salem 35433b3233fbSRaed Salem return 0; 35443b3233fbSRaed Salem 35453b3233fbSRaed Salem free_hndl: 35463b3233fbSRaed Salem if (hw_hndl) { 35473b3233fbSRaed Salem mlx5_fc_destroy(to_mdev(ibcounters->device)->mdev, 35483b3233fbSRaed Salem mcounters->hw_cntrs_hndl); 35493b3233fbSRaed Salem mcounters->hw_cntrs_hndl = NULL; 35503b3233fbSRaed Salem } 35513b3233fbSRaed Salem free: 35523b3233fbSRaed Salem kfree(desc_data); 35533b3233fbSRaed Salem return ret; 35543b3233fbSRaed Salem } 35553b3233fbSRaed Salem 3556669ff1e3SJianbo Liu static void mlx5_ib_set_rule_source_port(struct mlx5_ib_dev *dev, 3557669ff1e3SJianbo Liu struct mlx5_flow_spec *spec, 3558669ff1e3SJianbo Liu struct mlx5_eswitch_rep *rep) 3559669ff1e3SJianbo Liu { 3560669ff1e3SJianbo Liu struct mlx5_eswitch *esw = dev->mdev->priv.eswitch; 3561669ff1e3SJianbo Liu void *misc; 3562669ff1e3SJianbo Liu 3563669ff1e3SJianbo Liu if (mlx5_eswitch_vport_match_metadata_enabled(esw)) { 3564669ff1e3SJianbo Liu misc = MLX5_ADDR_OF(fte_match_param, spec->match_value, 3565669ff1e3SJianbo Liu misc_parameters_2); 3566669ff1e3SJianbo Liu 3567669ff1e3SJianbo Liu MLX5_SET(fte_match_set_misc2, misc, metadata_reg_c_0, 3568669ff1e3SJianbo Liu mlx5_eswitch_get_vport_metadata_for_match(esw, 3569669ff1e3SJianbo Liu rep->vport)); 3570669ff1e3SJianbo Liu misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria, 3571669ff1e3SJianbo Liu misc_parameters_2); 3572669ff1e3SJianbo Liu 3573*0f0d3827SPaul Blakey MLX5_SET(fte_match_set_misc2, misc, metadata_reg_c_0, 3574*0f0d3827SPaul Blakey mlx5_eswitch_get_vport_metadata_mask()); 3575669ff1e3SJianbo Liu } else { 3576669ff1e3SJianbo Liu misc = MLX5_ADDR_OF(fte_match_param, spec->match_value, 3577669ff1e3SJianbo Liu misc_parameters); 3578669ff1e3SJianbo Liu 3579669ff1e3SJianbo Liu MLX5_SET(fte_match_set_misc, misc, source_port, rep->vport); 3580669ff1e3SJianbo Liu 3581669ff1e3SJianbo Liu misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria, 3582669ff1e3SJianbo Liu misc_parameters); 3583669ff1e3SJianbo Liu 3584669ff1e3SJianbo Liu MLX5_SET_TO_ONES(fte_match_set_misc, misc, source_port); 3585669ff1e3SJianbo Liu } 3586669ff1e3SJianbo Liu } 3587669ff1e3SJianbo Liu 3588a550ddfcSYishai Hadas static struct mlx5_ib_flow_handler *_create_flow_rule(struct mlx5_ib_dev *dev, 3589038d2ef8SMaor Gottlieb struct mlx5_ib_flow_prio *ft_prio, 3590dd063d0eSMaor Gottlieb const struct ib_flow_attr *flow_attr, 3591a550ddfcSYishai Hadas struct mlx5_flow_destination *dst, 35923b3233fbSRaed Salem u32 underlay_qpn, 35933b3233fbSRaed Salem struct mlx5_ib_create_flow *ucmd) 3594038d2ef8SMaor Gottlieb { 3595038d2ef8SMaor Gottlieb struct mlx5_flow_table *ft = ft_prio->flow_table; 3596038d2ef8SMaor Gottlieb struct mlx5_ib_flow_handler *handler; 3597bb0ee7dcSJianbo Liu struct mlx5_flow_act flow_act = {}; 3598c5bb1730SMaor Gottlieb struct mlx5_flow_spec *spec; 35993b3233fbSRaed Salem struct mlx5_flow_destination dest_arr[2] = {}; 36003b3233fbSRaed Salem struct mlx5_flow_destination *rule_dst = dest_arr; 3601dd063d0eSMaor Gottlieb const void *ib_flow = (const void *)flow_attr + sizeof(*flow_attr); 3602038d2ef8SMaor Gottlieb unsigned int spec_index; 360371c6e863SAriel Levkovich u32 prev_type = 0; 3604038d2ef8SMaor Gottlieb int err = 0; 36053b3233fbSRaed Salem int dest_num = 0; 3606802c2125SAviad Yehezkel bool is_egress = flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS; 3607038d2ef8SMaor Gottlieb 360819cc7524SAriel Levkovich if (!is_valid_attr(dev->mdev, flow_attr)) 3609038d2ef8SMaor Gottlieb return ERR_PTR(-EINVAL); 3610038d2ef8SMaor Gottlieb 36116a4d00beSMark Bloch if (dev->is_rep && is_egress) 361278dd0c43SMark Bloch return ERR_PTR(-EINVAL); 361378dd0c43SMark Bloch 36141b9a07eeSLeon Romanovsky spec = kvzalloc(sizeof(*spec), GFP_KERNEL); 3615038d2ef8SMaor Gottlieb handler = kzalloc(sizeof(*handler), GFP_KERNEL); 3616c5bb1730SMaor Gottlieb if (!handler || !spec) { 3617038d2ef8SMaor Gottlieb err = -ENOMEM; 3618038d2ef8SMaor Gottlieb goto free; 3619038d2ef8SMaor Gottlieb } 3620038d2ef8SMaor Gottlieb 3621038d2ef8SMaor Gottlieb INIT_LIST_HEAD(&handler->list); 3622038d2ef8SMaor Gottlieb 3623038d2ef8SMaor Gottlieb for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) { 3624bb0ee7dcSJianbo Liu err = parse_flow_attr(dev->mdev, spec, 362571c6e863SAriel Levkovich ib_flow, flow_attr, &flow_act, 362671c6e863SAriel Levkovich prev_type); 3627038d2ef8SMaor Gottlieb if (err < 0) 3628038d2ef8SMaor Gottlieb goto free; 3629038d2ef8SMaor Gottlieb 363071c6e863SAriel Levkovich prev_type = ((union ib_flow_spec *)ib_flow)->type; 3631038d2ef8SMaor Gottlieb ib_flow += ((union ib_flow_spec *)ib_flow)->size; 3632038d2ef8SMaor Gottlieb } 3633038d2ef8SMaor Gottlieb 3634ed9085feSMaor Gottlieb if (dst && !(flow_act.action & MLX5_FLOW_CONTEXT_ACTION_DROP)) { 3635ed9085feSMaor Gottlieb memcpy(&dest_arr[0], dst, sizeof(*dst)); 3636ed9085feSMaor Gottlieb dest_num++; 3637ed9085feSMaor Gottlieb } 3638ed9085feSMaor Gottlieb 3639a550ddfcSYishai Hadas if (!flow_is_multicast_only(flow_attr)) 3640a550ddfcSYishai Hadas set_underlay_qp(dev, spec, underlay_qpn); 3641a550ddfcSYishai Hadas 36426a4d00beSMark Bloch if (dev->is_rep) { 3643669ff1e3SJianbo Liu struct mlx5_eswitch_rep *rep; 3644018a94eeSMark Bloch 3645669ff1e3SJianbo Liu rep = dev->port[flow_attr->port - 1].rep; 3646669ff1e3SJianbo Liu if (!rep) { 36476a4d00beSMark Bloch err = -EINVAL; 36486a4d00beSMark Bloch goto free; 36496a4d00beSMark Bloch } 3650669ff1e3SJianbo Liu 3651669ff1e3SJianbo Liu mlx5_ib_set_rule_source_port(dev, spec, rep); 3652018a94eeSMark Bloch } 3653018a94eeSMark Bloch 3654466fa6d2SMaor Gottlieb spec->match_criteria_enable = get_match_criteria_enable(spec->match_criteria); 3655802c2125SAviad Yehezkel 3656802c2125SAviad Yehezkel if (is_egress && 3657802c2125SAviad Yehezkel !is_valid_spec(dev->mdev, spec, &flow_act, is_egress)) { 3658802c2125SAviad Yehezkel err = -EINVAL; 3659802c2125SAviad Yehezkel goto free; 3660802c2125SAviad Yehezkel } 3661802c2125SAviad Yehezkel 36623b3233fbSRaed Salem if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_COUNT) { 3663171c7625SMark Bloch struct mlx5_ib_mcounters *mcounters; 3664171c7625SMark Bloch 36653b3233fbSRaed Salem err = flow_counters_set_data(flow_act.counters, ucmd); 36663b3233fbSRaed Salem if (err) 36673b3233fbSRaed Salem goto free; 36683b3233fbSRaed Salem 3669171c7625SMark Bloch mcounters = to_mcounters(flow_act.counters); 36703b3233fbSRaed Salem handler->ibcounters = flow_act.counters; 36713b3233fbSRaed Salem dest_arr[dest_num].type = 36723b3233fbSRaed Salem MLX5_FLOW_DESTINATION_TYPE_COUNTER; 3673171c7625SMark Bloch dest_arr[dest_num].counter_id = 3674171c7625SMark Bloch mlx5_fc_id(mcounters->hw_cntrs_hndl); 36753b3233fbSRaed Salem dest_num++; 36763b3233fbSRaed Salem } 36773b3233fbSRaed Salem 3678075572d4SBoris Pismenny if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_DROP) { 3679ed9085feSMaor Gottlieb if (!dest_num) 3680a22ed86cSSlava Shwartsman rule_dst = NULL; 3681a22ed86cSSlava Shwartsman } else { 3682802c2125SAviad Yehezkel if (is_egress) 3683802c2125SAviad Yehezkel flow_act.action |= MLX5_FLOW_CONTEXT_ACTION_ALLOW; 3684802c2125SAviad Yehezkel else 3685802c2125SAviad Yehezkel flow_act.action |= 36863b3233fbSRaed Salem dest_num ? MLX5_FLOW_CONTEXT_ACTION_FWD_DEST : 368735d19011SMaor Gottlieb MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO; 3688a22ed86cSSlava Shwartsman } 36892ac693f9SMoses Reuben 3690bb0ee7dcSJianbo Liu if ((spec->flow_context.flags & FLOW_CONTEXT_HAS_TAG) && 36912ac693f9SMoses Reuben (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT || 36922ac693f9SMoses Reuben flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT)) { 36932ac693f9SMoses Reuben mlx5_ib_warn(dev, "Flow tag %u and attribute type %x isn't allowed in leftovers\n", 3694bb0ee7dcSJianbo Liu spec->flow_context.flow_tag, flow_attr->type); 36952ac693f9SMoses Reuben err = -EINVAL; 36962ac693f9SMoses Reuben goto free; 36972ac693f9SMoses Reuben } 369874491de9SMark Bloch handler->rule = mlx5_add_flow_rules(ft, spec, 369966958ed9SHadar Hen Zion &flow_act, 3700a22ed86cSSlava Shwartsman rule_dst, dest_num); 3701038d2ef8SMaor Gottlieb 3702038d2ef8SMaor Gottlieb if (IS_ERR(handler->rule)) { 3703038d2ef8SMaor Gottlieb err = PTR_ERR(handler->rule); 3704038d2ef8SMaor Gottlieb goto free; 3705038d2ef8SMaor Gottlieb } 3706038d2ef8SMaor Gottlieb 3707d9d4980aSMaor Gottlieb ft_prio->refcount++; 37085497adc6SMaor Gottlieb handler->prio = ft_prio; 3709d4be3f44SYishai Hadas handler->dev = dev; 3710038d2ef8SMaor Gottlieb 3711038d2ef8SMaor Gottlieb ft_prio->flow_table = ft; 3712038d2ef8SMaor Gottlieb free: 37133b3233fbSRaed Salem if (err && handler) { 37143b3233fbSRaed Salem if (handler->ibcounters && 37153b3233fbSRaed Salem atomic_read(&handler->ibcounters->usecnt) == 1) 37163b3233fbSRaed Salem counters_clear_description(handler->ibcounters); 3717038d2ef8SMaor Gottlieb kfree(handler); 37183b3233fbSRaed Salem } 3719c5bb1730SMaor Gottlieb kvfree(spec); 3720038d2ef8SMaor Gottlieb return err ? ERR_PTR(err) : handler; 3721038d2ef8SMaor Gottlieb } 3722038d2ef8SMaor Gottlieb 3723a550ddfcSYishai Hadas static struct mlx5_ib_flow_handler *create_flow_rule(struct mlx5_ib_dev *dev, 3724a550ddfcSYishai Hadas struct mlx5_ib_flow_prio *ft_prio, 3725a550ddfcSYishai Hadas const struct ib_flow_attr *flow_attr, 3726a550ddfcSYishai Hadas struct mlx5_flow_destination *dst) 3727a550ddfcSYishai Hadas { 37283b3233fbSRaed Salem return _create_flow_rule(dev, ft_prio, flow_attr, dst, 0, NULL); 3729a550ddfcSYishai Hadas } 3730a550ddfcSYishai Hadas 373135d19011SMaor Gottlieb static struct mlx5_ib_flow_handler *create_dont_trap_rule(struct mlx5_ib_dev *dev, 373235d19011SMaor Gottlieb struct mlx5_ib_flow_prio *ft_prio, 373335d19011SMaor Gottlieb struct ib_flow_attr *flow_attr, 373435d19011SMaor Gottlieb struct mlx5_flow_destination *dst) 373535d19011SMaor Gottlieb { 373635d19011SMaor Gottlieb struct mlx5_ib_flow_handler *handler_dst = NULL; 373735d19011SMaor Gottlieb struct mlx5_ib_flow_handler *handler = NULL; 373835d19011SMaor Gottlieb 373935d19011SMaor Gottlieb handler = create_flow_rule(dev, ft_prio, flow_attr, NULL); 374035d19011SMaor Gottlieb if (!IS_ERR(handler)) { 374135d19011SMaor Gottlieb handler_dst = create_flow_rule(dev, ft_prio, 374235d19011SMaor Gottlieb flow_attr, dst); 374335d19011SMaor Gottlieb if (IS_ERR(handler_dst)) { 374474491de9SMark Bloch mlx5_del_flow_rules(handler->rule); 3745d9d4980aSMaor Gottlieb ft_prio->refcount--; 374635d19011SMaor Gottlieb kfree(handler); 374735d19011SMaor Gottlieb handler = handler_dst; 374835d19011SMaor Gottlieb } else { 374935d19011SMaor Gottlieb list_add(&handler_dst->list, &handler->list); 375035d19011SMaor Gottlieb } 375135d19011SMaor Gottlieb } 375235d19011SMaor Gottlieb 375335d19011SMaor Gottlieb return handler; 375435d19011SMaor Gottlieb } 3755038d2ef8SMaor Gottlieb enum { 3756038d2ef8SMaor Gottlieb LEFTOVERS_MC, 3757038d2ef8SMaor Gottlieb LEFTOVERS_UC, 3758038d2ef8SMaor Gottlieb }; 3759038d2ef8SMaor Gottlieb 3760038d2ef8SMaor Gottlieb static struct mlx5_ib_flow_handler *create_leftovers_rule(struct mlx5_ib_dev *dev, 3761038d2ef8SMaor Gottlieb struct mlx5_ib_flow_prio *ft_prio, 3762038d2ef8SMaor Gottlieb struct ib_flow_attr *flow_attr, 3763038d2ef8SMaor Gottlieb struct mlx5_flow_destination *dst) 3764038d2ef8SMaor Gottlieb { 3765038d2ef8SMaor Gottlieb struct mlx5_ib_flow_handler *handler_ucast = NULL; 3766038d2ef8SMaor Gottlieb struct mlx5_ib_flow_handler *handler = NULL; 3767038d2ef8SMaor Gottlieb 3768038d2ef8SMaor Gottlieb static struct { 3769038d2ef8SMaor Gottlieb struct ib_flow_attr flow_attr; 3770038d2ef8SMaor Gottlieb struct ib_flow_spec_eth eth_flow; 3771038d2ef8SMaor Gottlieb } leftovers_specs[] = { 3772038d2ef8SMaor Gottlieb [LEFTOVERS_MC] = { 3773038d2ef8SMaor Gottlieb .flow_attr = { 3774038d2ef8SMaor Gottlieb .num_of_specs = 1, 3775038d2ef8SMaor Gottlieb .size = sizeof(leftovers_specs[0]) 3776038d2ef8SMaor Gottlieb }, 3777038d2ef8SMaor Gottlieb .eth_flow = { 3778038d2ef8SMaor Gottlieb .type = IB_FLOW_SPEC_ETH, 3779038d2ef8SMaor Gottlieb .size = sizeof(struct ib_flow_spec_eth), 3780038d2ef8SMaor Gottlieb .mask = {.dst_mac = {0x1} }, 3781038d2ef8SMaor Gottlieb .val = {.dst_mac = {0x1} } 3782038d2ef8SMaor Gottlieb } 3783038d2ef8SMaor Gottlieb }, 3784038d2ef8SMaor Gottlieb [LEFTOVERS_UC] = { 3785038d2ef8SMaor Gottlieb .flow_attr = { 3786038d2ef8SMaor Gottlieb .num_of_specs = 1, 3787038d2ef8SMaor Gottlieb .size = sizeof(leftovers_specs[0]) 3788038d2ef8SMaor Gottlieb }, 3789038d2ef8SMaor Gottlieb .eth_flow = { 3790038d2ef8SMaor Gottlieb .type = IB_FLOW_SPEC_ETH, 3791038d2ef8SMaor Gottlieb .size = sizeof(struct ib_flow_spec_eth), 3792038d2ef8SMaor Gottlieb .mask = {.dst_mac = {0x1} }, 3793038d2ef8SMaor Gottlieb .val = {.dst_mac = {} } 3794038d2ef8SMaor Gottlieb } 3795038d2ef8SMaor Gottlieb } 3796038d2ef8SMaor Gottlieb }; 3797038d2ef8SMaor Gottlieb 3798038d2ef8SMaor Gottlieb handler = create_flow_rule(dev, ft_prio, 3799038d2ef8SMaor Gottlieb &leftovers_specs[LEFTOVERS_MC].flow_attr, 3800038d2ef8SMaor Gottlieb dst); 3801038d2ef8SMaor Gottlieb if (!IS_ERR(handler) && 3802038d2ef8SMaor Gottlieb flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT) { 3803038d2ef8SMaor Gottlieb handler_ucast = create_flow_rule(dev, ft_prio, 3804038d2ef8SMaor Gottlieb &leftovers_specs[LEFTOVERS_UC].flow_attr, 3805038d2ef8SMaor Gottlieb dst); 3806038d2ef8SMaor Gottlieb if (IS_ERR(handler_ucast)) { 380774491de9SMark Bloch mlx5_del_flow_rules(handler->rule); 3808d9d4980aSMaor Gottlieb ft_prio->refcount--; 3809038d2ef8SMaor Gottlieb kfree(handler); 3810038d2ef8SMaor Gottlieb handler = handler_ucast; 3811038d2ef8SMaor Gottlieb } else { 3812038d2ef8SMaor Gottlieb list_add(&handler_ucast->list, &handler->list); 3813038d2ef8SMaor Gottlieb } 3814038d2ef8SMaor Gottlieb } 3815038d2ef8SMaor Gottlieb 3816038d2ef8SMaor Gottlieb return handler; 3817038d2ef8SMaor Gottlieb } 3818038d2ef8SMaor Gottlieb 3819cc0e5d42SMaor Gottlieb static struct mlx5_ib_flow_handler *create_sniffer_rule(struct mlx5_ib_dev *dev, 3820cc0e5d42SMaor Gottlieb struct mlx5_ib_flow_prio *ft_rx, 3821cc0e5d42SMaor Gottlieb struct mlx5_ib_flow_prio *ft_tx, 3822cc0e5d42SMaor Gottlieb struct mlx5_flow_destination *dst) 3823cc0e5d42SMaor Gottlieb { 3824cc0e5d42SMaor Gottlieb struct mlx5_ib_flow_handler *handler_rx; 3825cc0e5d42SMaor Gottlieb struct mlx5_ib_flow_handler *handler_tx; 3826cc0e5d42SMaor Gottlieb int err; 3827cc0e5d42SMaor Gottlieb static const struct ib_flow_attr flow_attr = { 3828cc0e5d42SMaor Gottlieb .num_of_specs = 0, 3829cc0e5d42SMaor Gottlieb .size = sizeof(flow_attr) 3830cc0e5d42SMaor Gottlieb }; 3831cc0e5d42SMaor Gottlieb 3832cc0e5d42SMaor Gottlieb handler_rx = create_flow_rule(dev, ft_rx, &flow_attr, dst); 3833cc0e5d42SMaor Gottlieb if (IS_ERR(handler_rx)) { 3834cc0e5d42SMaor Gottlieb err = PTR_ERR(handler_rx); 3835cc0e5d42SMaor Gottlieb goto err; 3836cc0e5d42SMaor Gottlieb } 3837cc0e5d42SMaor Gottlieb 3838cc0e5d42SMaor Gottlieb handler_tx = create_flow_rule(dev, ft_tx, &flow_attr, dst); 3839cc0e5d42SMaor Gottlieb if (IS_ERR(handler_tx)) { 3840cc0e5d42SMaor Gottlieb err = PTR_ERR(handler_tx); 3841cc0e5d42SMaor Gottlieb goto err_tx; 3842cc0e5d42SMaor Gottlieb } 3843cc0e5d42SMaor Gottlieb 3844cc0e5d42SMaor Gottlieb list_add(&handler_tx->list, &handler_rx->list); 3845cc0e5d42SMaor Gottlieb 3846cc0e5d42SMaor Gottlieb return handler_rx; 3847cc0e5d42SMaor Gottlieb 3848cc0e5d42SMaor Gottlieb err_tx: 384974491de9SMark Bloch mlx5_del_flow_rules(handler_rx->rule); 3850cc0e5d42SMaor Gottlieb ft_rx->refcount--; 3851cc0e5d42SMaor Gottlieb kfree(handler_rx); 3852cc0e5d42SMaor Gottlieb err: 3853cc0e5d42SMaor Gottlieb return ERR_PTR(err); 3854cc0e5d42SMaor Gottlieb } 3855cc0e5d42SMaor Gottlieb 3856038d2ef8SMaor Gottlieb static struct ib_flow *mlx5_ib_create_flow(struct ib_qp *qp, 3857038d2ef8SMaor Gottlieb struct ib_flow_attr *flow_attr, 385859082a32SMatan Barak int domain, 385959082a32SMatan Barak struct ib_udata *udata) 3860038d2ef8SMaor Gottlieb { 3861038d2ef8SMaor Gottlieb struct mlx5_ib_dev *dev = to_mdev(qp->device); 3862d9f88e5aSYishai Hadas struct mlx5_ib_qp *mqp = to_mqp(qp); 3863038d2ef8SMaor Gottlieb struct mlx5_ib_flow_handler *handler = NULL; 3864038d2ef8SMaor Gottlieb struct mlx5_flow_destination *dst = NULL; 3865cc0e5d42SMaor Gottlieb struct mlx5_ib_flow_prio *ft_prio_tx = NULL; 3866038d2ef8SMaor Gottlieb struct mlx5_ib_flow_prio *ft_prio; 3867802c2125SAviad Yehezkel bool is_egress = flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS; 38683b3233fbSRaed Salem struct mlx5_ib_create_flow *ucmd = NULL, ucmd_hdr; 38693b3233fbSRaed Salem size_t min_ucmd_sz, required_ucmd_sz; 3870038d2ef8SMaor Gottlieb int err; 3871a550ddfcSYishai Hadas int underlay_qpn; 3872038d2ef8SMaor Gottlieb 38733b3233fbSRaed Salem if (udata && udata->inlen) { 38743b3233fbSRaed Salem min_ucmd_sz = offsetof(typeof(ucmd_hdr), reserved) + 38753b3233fbSRaed Salem sizeof(ucmd_hdr.reserved); 38763b3233fbSRaed Salem if (udata->inlen < min_ucmd_sz) 387759082a32SMatan Barak return ERR_PTR(-EOPNOTSUPP); 387859082a32SMatan Barak 38793b3233fbSRaed Salem err = ib_copy_from_udata(&ucmd_hdr, udata, min_ucmd_sz); 38803b3233fbSRaed Salem if (err) 38813b3233fbSRaed Salem return ERR_PTR(err); 38823b3233fbSRaed Salem 38833b3233fbSRaed Salem /* currently supports only one counters data */ 38843b3233fbSRaed Salem if (ucmd_hdr.ncounters_data > 1) 38853b3233fbSRaed Salem return ERR_PTR(-EINVAL); 38863b3233fbSRaed Salem 38873b3233fbSRaed Salem required_ucmd_sz = min_ucmd_sz + 38883b3233fbSRaed Salem sizeof(struct mlx5_ib_flow_counters_data) * 38893b3233fbSRaed Salem ucmd_hdr.ncounters_data; 38903b3233fbSRaed Salem if (udata->inlen > required_ucmd_sz && 38913b3233fbSRaed Salem !ib_is_udata_cleared(udata, required_ucmd_sz, 38923b3233fbSRaed Salem udata->inlen - required_ucmd_sz)) 38933b3233fbSRaed Salem return ERR_PTR(-EOPNOTSUPP); 38943b3233fbSRaed Salem 38953b3233fbSRaed Salem ucmd = kzalloc(required_ucmd_sz, GFP_KERNEL); 38963b3233fbSRaed Salem if (!ucmd) 38973b3233fbSRaed Salem return ERR_PTR(-ENOMEM); 38983b3233fbSRaed Salem 38993b3233fbSRaed Salem err = ib_copy_from_udata(ucmd, udata, required_ucmd_sz); 3900299eafeeSGustavo A. R. Silva if (err) 3901299eafeeSGustavo A. R. Silva goto free_ucmd; 39023b3233fbSRaed Salem } 39033b3233fbSRaed Salem 3904299eafeeSGustavo A. R. Silva if (flow_attr->priority > MLX5_IB_FLOW_LAST_PRIO) { 3905299eafeeSGustavo A. R. Silva err = -ENOMEM; 3906299eafeeSGustavo A. R. Silva goto free_ucmd; 3907299eafeeSGustavo A. R. Silva } 3908038d2ef8SMaor Gottlieb 3909038d2ef8SMaor Gottlieb if (domain != IB_FLOW_DOMAIN_USER || 3910508562d6SDaniel Jurgens flow_attr->port > dev->num_ports || 3911802c2125SAviad Yehezkel (flow_attr->flags & ~(IB_FLOW_ATTR_FLAGS_DONT_TRAP | 3912299eafeeSGustavo A. R. Silva IB_FLOW_ATTR_FLAGS_EGRESS))) { 3913299eafeeSGustavo A. R. Silva err = -EINVAL; 3914299eafeeSGustavo A. R. Silva goto free_ucmd; 3915299eafeeSGustavo A. R. Silva } 3916802c2125SAviad Yehezkel 3917802c2125SAviad Yehezkel if (is_egress && 3918802c2125SAviad Yehezkel (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT || 3919299eafeeSGustavo A. R. Silva flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT)) { 3920299eafeeSGustavo A. R. Silva err = -EINVAL; 3921299eafeeSGustavo A. R. Silva goto free_ucmd; 3922299eafeeSGustavo A. R. Silva } 3923038d2ef8SMaor Gottlieb 3924038d2ef8SMaor Gottlieb dst = kzalloc(sizeof(*dst), GFP_KERNEL); 3925299eafeeSGustavo A. R. Silva if (!dst) { 3926299eafeeSGustavo A. R. Silva err = -ENOMEM; 3927299eafeeSGustavo A. R. Silva goto free_ucmd; 3928299eafeeSGustavo A. R. Silva } 3929038d2ef8SMaor Gottlieb 39309a4ca38dSMark Bloch mutex_lock(&dev->flow_db->lock); 3931038d2ef8SMaor Gottlieb 3932802c2125SAviad Yehezkel ft_prio = get_flow_table(dev, flow_attr, 3933802c2125SAviad Yehezkel is_egress ? MLX5_IB_FT_TX : MLX5_IB_FT_RX); 3934038d2ef8SMaor Gottlieb if (IS_ERR(ft_prio)) { 3935038d2ef8SMaor Gottlieb err = PTR_ERR(ft_prio); 3936038d2ef8SMaor Gottlieb goto unlock; 3937038d2ef8SMaor Gottlieb } 3938cc0e5d42SMaor Gottlieb if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) { 3939cc0e5d42SMaor Gottlieb ft_prio_tx = get_flow_table(dev, flow_attr, MLX5_IB_FT_TX); 3940cc0e5d42SMaor Gottlieb if (IS_ERR(ft_prio_tx)) { 3941cc0e5d42SMaor Gottlieb err = PTR_ERR(ft_prio_tx); 3942cc0e5d42SMaor Gottlieb ft_prio_tx = NULL; 3943cc0e5d42SMaor Gottlieb goto destroy_ft; 3944cc0e5d42SMaor Gottlieb } 3945cc0e5d42SMaor Gottlieb } 3946038d2ef8SMaor Gottlieb 3947802c2125SAviad Yehezkel if (is_egress) { 3948802c2125SAviad Yehezkel dst->type = MLX5_FLOW_DESTINATION_TYPE_PORT; 3949802c2125SAviad Yehezkel } else { 3950038d2ef8SMaor Gottlieb dst->type = MLX5_FLOW_DESTINATION_TYPE_TIR; 3951d9f88e5aSYishai Hadas if (mqp->flags & MLX5_IB_QP_RSS) 3952d9f88e5aSYishai Hadas dst->tir_num = mqp->rss_qp.tirn; 3953d9f88e5aSYishai Hadas else 3954d9f88e5aSYishai Hadas dst->tir_num = mqp->raw_packet_qp.rq.tirn; 3955802c2125SAviad Yehezkel } 3956038d2ef8SMaor Gottlieb 3957038d2ef8SMaor Gottlieb if (flow_attr->type == IB_FLOW_ATTR_NORMAL) { 395835d19011SMaor Gottlieb if (flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP) { 395935d19011SMaor Gottlieb handler = create_dont_trap_rule(dev, ft_prio, 396035d19011SMaor Gottlieb flow_attr, dst); 396135d19011SMaor Gottlieb } else { 3962a550ddfcSYishai Hadas underlay_qpn = (mqp->flags & MLX5_IB_QP_UNDERLAY) ? 3963a550ddfcSYishai Hadas mqp->underlay_qpn : 0; 3964a550ddfcSYishai Hadas handler = _create_flow_rule(dev, ft_prio, flow_attr, 39653b3233fbSRaed Salem dst, underlay_qpn, ucmd); 396635d19011SMaor Gottlieb } 3967038d2ef8SMaor Gottlieb } else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT || 3968038d2ef8SMaor Gottlieb flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) { 3969038d2ef8SMaor Gottlieb handler = create_leftovers_rule(dev, ft_prio, flow_attr, 3970038d2ef8SMaor Gottlieb dst); 3971cc0e5d42SMaor Gottlieb } else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) { 3972cc0e5d42SMaor Gottlieb handler = create_sniffer_rule(dev, ft_prio, ft_prio_tx, dst); 3973038d2ef8SMaor Gottlieb } else { 3974038d2ef8SMaor Gottlieb err = -EINVAL; 3975038d2ef8SMaor Gottlieb goto destroy_ft; 3976038d2ef8SMaor Gottlieb } 3977038d2ef8SMaor Gottlieb 3978038d2ef8SMaor Gottlieb if (IS_ERR(handler)) { 3979038d2ef8SMaor Gottlieb err = PTR_ERR(handler); 3980038d2ef8SMaor Gottlieb handler = NULL; 3981038d2ef8SMaor Gottlieb goto destroy_ft; 3982038d2ef8SMaor Gottlieb } 3983038d2ef8SMaor Gottlieb 39849a4ca38dSMark Bloch mutex_unlock(&dev->flow_db->lock); 3985038d2ef8SMaor Gottlieb kfree(dst); 39863b3233fbSRaed Salem kfree(ucmd); 3987038d2ef8SMaor Gottlieb 3988038d2ef8SMaor Gottlieb return &handler->ibflow; 3989038d2ef8SMaor Gottlieb 3990038d2ef8SMaor Gottlieb destroy_ft: 3991038d2ef8SMaor Gottlieb put_flow_table(dev, ft_prio, false); 3992cc0e5d42SMaor Gottlieb if (ft_prio_tx) 3993cc0e5d42SMaor Gottlieb put_flow_table(dev, ft_prio_tx, false); 3994038d2ef8SMaor Gottlieb unlock: 39959a4ca38dSMark Bloch mutex_unlock(&dev->flow_db->lock); 3996038d2ef8SMaor Gottlieb kfree(dst); 3997299eafeeSGustavo A. R. Silva free_ucmd: 39983b3233fbSRaed Salem kfree(ucmd); 3999038d2ef8SMaor Gottlieb return ERR_PTR(err); 4000038d2ef8SMaor Gottlieb } 4001038d2ef8SMaor Gottlieb 4002b47fd4ffSMark Bloch static struct mlx5_ib_flow_prio * 4003b47fd4ffSMark Bloch _get_flow_table(struct mlx5_ib_dev *dev, 4004b47fd4ffSMark Bloch struct mlx5_ib_flow_matcher *fs_matcher, 4005b47fd4ffSMark Bloch bool mcast) 4006d4be3f44SYishai Hadas { 4007d4be3f44SYishai Hadas struct mlx5_flow_namespace *ns = NULL; 400813a43765SMark Bloch struct mlx5_ib_flow_prio *prio = NULL; 400913a43765SMark Bloch int max_table_size = 0; 4010cecae747SMaor Gottlieb bool esw_encap; 4011b47fd4ffSMark Bloch u32 flags = 0; 4012b47fd4ffSMark Bloch int priority; 4013d4be3f44SYishai Hadas 401413a43765SMark Bloch if (mcast) 401513a43765SMark Bloch priority = MLX5_IB_FLOW_MCAST_PRIO; 401613a43765SMark Bloch else 401713a43765SMark Bloch priority = ib_prio_to_core_prio(fs_matcher->priority, false); 401813a43765SMark Bloch 4019cecae747SMaor Gottlieb esw_encap = mlx5_eswitch_get_encap_mode(dev->mdev) != 4020cecae747SMaor Gottlieb DEVLINK_ESWITCH_ENCAP_MODE_NONE; 4021b47fd4ffSMark Bloch if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_BYPASS) { 4022d4be3f44SYishai Hadas max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, 4023d4be3f44SYishai Hadas log_max_ft_size)); 4024cecae747SMaor Gottlieb if (MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, decap) && !esw_encap) 4025b47fd4ffSMark Bloch flags |= MLX5_FLOW_TABLE_TUNNEL_EN_DECAP; 4026b47fd4ffSMark Bloch if (MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, 4027cecae747SMaor Gottlieb reformat_l3_tunnel_to_l2) && 4028cecae747SMaor Gottlieb !esw_encap) 4029b47fd4ffSMark Bloch flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT; 403013a43765SMark Bloch } else if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_EGRESS) { 403113a43765SMark Bloch max_table_size = BIT( 403213a43765SMark Bloch MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev, log_max_ft_size)); 4033cecae747SMaor Gottlieb if (MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev, reformat) && !esw_encap) 4034b47fd4ffSMark Bloch flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT; 403513a43765SMark Bloch } else if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_FDB) { 403613a43765SMark Bloch max_table_size = BIT( 403713a43765SMark Bloch MLX5_CAP_ESW_FLOWTABLE_FDB(dev->mdev, log_max_ft_size)); 403809d985beSMaor Gottlieb if (MLX5_CAP_ESW_FLOWTABLE_FDB(dev->mdev, decap) && esw_encap) 403909d985beSMaor Gottlieb flags |= MLX5_FLOW_TABLE_TUNNEL_EN_DECAP; 404009d985beSMaor Gottlieb if (MLX5_CAP_ESW_FLOWTABLE_FDB(dev->mdev, reformat_l3_tunnel_to_l2) && 404109d985beSMaor Gottlieb esw_encap) 404209d985beSMaor Gottlieb flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT; 404313a43765SMark Bloch priority = FDB_BYPASS_PATH; 4044d8abe884SMark Zhang } else if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_RDMA_RX) { 4045d8abe884SMark Zhang max_table_size = 4046d8abe884SMark Zhang BIT(MLX5_CAP_FLOWTABLE_RDMA_RX(dev->mdev, 4047d8abe884SMark Zhang log_max_ft_size)); 4048d8abe884SMark Zhang priority = fs_matcher->priority; 4049b47fd4ffSMark Bloch } 4050b47fd4ffSMark Bloch 40513b70508aSMark Bloch max_table_size = min_t(int, max_table_size, MLX5_FS_MAX_ENTRIES); 4052d4be3f44SYishai Hadas 4053b47fd4ffSMark Bloch ns = mlx5_get_flow_namespace(dev->mdev, fs_matcher->ns_type); 4054d4be3f44SYishai Hadas if (!ns) 4055d4be3f44SYishai Hadas return ERR_PTR(-ENOTSUPP); 4056d4be3f44SYishai Hadas 4057b47fd4ffSMark Bloch if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_BYPASS) 4058d4be3f44SYishai Hadas prio = &dev->flow_db->prios[priority]; 405913a43765SMark Bloch else if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_EGRESS) 4060b47fd4ffSMark Bloch prio = &dev->flow_db->egress_prios[priority]; 406113a43765SMark Bloch else if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_FDB) 406213a43765SMark Bloch prio = &dev->flow_db->fdb; 4063d8abe884SMark Zhang else if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_RDMA_RX) 4064d8abe884SMark Zhang prio = &dev->flow_db->rdma_rx[priority]; 406513a43765SMark Bloch 406613a43765SMark Bloch if (!prio) 406713a43765SMark Bloch return ERR_PTR(-EINVAL); 4068d4be3f44SYishai Hadas 4069d4be3f44SYishai Hadas if (prio->flow_table) 4070d4be3f44SYishai Hadas return prio; 4071d4be3f44SYishai Hadas 40723b70508aSMark Bloch return _get_prio(ns, prio, priority, max_table_size, 4073b47fd4ffSMark Bloch MLX5_FS_MAX_TYPES, flags); 4074d4be3f44SYishai Hadas } 4075d4be3f44SYishai Hadas 4076d4be3f44SYishai Hadas static struct mlx5_ib_flow_handler * 4077d4be3f44SYishai Hadas _create_raw_flow_rule(struct mlx5_ib_dev *dev, 4078d4be3f44SYishai Hadas struct mlx5_ib_flow_prio *ft_prio, 4079d4be3f44SYishai Hadas struct mlx5_flow_destination *dst, 4080d4be3f44SYishai Hadas struct mlx5_ib_flow_matcher *fs_matcher, 4081bb0ee7dcSJianbo Liu struct mlx5_flow_context *flow_context, 4082b823dd6dSMark Bloch struct mlx5_flow_act *flow_act, 4083bfc5d839SMark Bloch void *cmd_in, int inlen, 4084bfc5d839SMark Bloch int dst_num) 4085d4be3f44SYishai Hadas { 4086d4be3f44SYishai Hadas struct mlx5_ib_flow_handler *handler; 4087d4be3f44SYishai Hadas struct mlx5_flow_spec *spec; 4088d4be3f44SYishai Hadas struct mlx5_flow_table *ft = ft_prio->flow_table; 4089d4be3f44SYishai Hadas int err = 0; 4090d4be3f44SYishai Hadas 4091d4be3f44SYishai Hadas spec = kvzalloc(sizeof(*spec), GFP_KERNEL); 4092d4be3f44SYishai Hadas handler = kzalloc(sizeof(*handler), GFP_KERNEL); 4093d4be3f44SYishai Hadas if (!handler || !spec) { 4094d4be3f44SYishai Hadas err = -ENOMEM; 4095d4be3f44SYishai Hadas goto free; 4096d4be3f44SYishai Hadas } 4097d4be3f44SYishai Hadas 4098d4be3f44SYishai Hadas INIT_LIST_HEAD(&handler->list); 4099d4be3f44SYishai Hadas 4100d4be3f44SYishai Hadas memcpy(spec->match_value, cmd_in, inlen); 4101d4be3f44SYishai Hadas memcpy(spec->match_criteria, fs_matcher->matcher_mask.match_params, 4102d4be3f44SYishai Hadas fs_matcher->mask_len); 4103d4be3f44SYishai Hadas spec->match_criteria_enable = fs_matcher->match_criteria_enable; 4104bb0ee7dcSJianbo Liu spec->flow_context = *flow_context; 4105d4be3f44SYishai Hadas 4106d4be3f44SYishai Hadas handler->rule = mlx5_add_flow_rules(ft, spec, 4107bfc5d839SMark Bloch flow_act, dst, dst_num); 4108d4be3f44SYishai Hadas 4109d4be3f44SYishai Hadas if (IS_ERR(handler->rule)) { 4110d4be3f44SYishai Hadas err = PTR_ERR(handler->rule); 4111d4be3f44SYishai Hadas goto free; 4112d4be3f44SYishai Hadas } 4113d4be3f44SYishai Hadas 4114d4be3f44SYishai Hadas ft_prio->refcount++; 4115d4be3f44SYishai Hadas handler->prio = ft_prio; 4116d4be3f44SYishai Hadas handler->dev = dev; 4117d4be3f44SYishai Hadas ft_prio->flow_table = ft; 4118d4be3f44SYishai Hadas 4119d4be3f44SYishai Hadas free: 4120d4be3f44SYishai Hadas if (err) 4121d4be3f44SYishai Hadas kfree(handler); 4122d4be3f44SYishai Hadas kvfree(spec); 4123d4be3f44SYishai Hadas return err ? ERR_PTR(err) : handler; 4124d4be3f44SYishai Hadas } 4125d4be3f44SYishai Hadas 4126d4be3f44SYishai Hadas static bool raw_fs_is_multicast(struct mlx5_ib_flow_matcher *fs_matcher, 4127d4be3f44SYishai Hadas void *match_v) 4128d4be3f44SYishai Hadas { 4129d4be3f44SYishai Hadas void *match_c; 4130d4be3f44SYishai Hadas void *match_v_set_lyr_2_4, *match_c_set_lyr_2_4; 4131d4be3f44SYishai Hadas void *dmac, *dmac_mask; 4132d4be3f44SYishai Hadas void *ipv4, *ipv4_mask; 4133d4be3f44SYishai Hadas 4134d4be3f44SYishai Hadas if (!(fs_matcher->match_criteria_enable & 4135d4be3f44SYishai Hadas (1 << MATCH_CRITERIA_ENABLE_OUTER_BIT))) 4136d4be3f44SYishai Hadas return false; 4137d4be3f44SYishai Hadas 4138d4be3f44SYishai Hadas match_c = fs_matcher->matcher_mask.match_params; 4139d4be3f44SYishai Hadas match_v_set_lyr_2_4 = MLX5_ADDR_OF(fte_match_param, match_v, 4140d4be3f44SYishai Hadas outer_headers); 4141d4be3f44SYishai Hadas match_c_set_lyr_2_4 = MLX5_ADDR_OF(fte_match_param, match_c, 4142d4be3f44SYishai Hadas outer_headers); 4143d4be3f44SYishai Hadas 4144d4be3f44SYishai Hadas dmac = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_v_set_lyr_2_4, 4145d4be3f44SYishai Hadas dmac_47_16); 4146d4be3f44SYishai Hadas dmac_mask = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_c_set_lyr_2_4, 4147d4be3f44SYishai Hadas dmac_47_16); 4148d4be3f44SYishai Hadas 4149d4be3f44SYishai Hadas if (is_multicast_ether_addr(dmac) && 4150d4be3f44SYishai Hadas is_multicast_ether_addr(dmac_mask)) 4151d4be3f44SYishai Hadas return true; 4152d4be3f44SYishai Hadas 4153d4be3f44SYishai Hadas ipv4 = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_v_set_lyr_2_4, 4154d4be3f44SYishai Hadas dst_ipv4_dst_ipv6.ipv4_layout.ipv4); 4155d4be3f44SYishai Hadas 4156d4be3f44SYishai Hadas ipv4_mask = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_c_set_lyr_2_4, 4157d4be3f44SYishai Hadas dst_ipv4_dst_ipv6.ipv4_layout.ipv4); 4158d4be3f44SYishai Hadas 4159d4be3f44SYishai Hadas if (ipv4_is_multicast(*(__be32 *)(ipv4)) && 4160d4be3f44SYishai Hadas ipv4_is_multicast(*(__be32 *)(ipv4_mask))) 4161d4be3f44SYishai Hadas return true; 4162d4be3f44SYishai Hadas 4163d4be3f44SYishai Hadas return false; 4164d4be3f44SYishai Hadas } 4165d4be3f44SYishai Hadas 416632269441SYishai Hadas struct mlx5_ib_flow_handler * 416732269441SYishai Hadas mlx5_ib_raw_fs_rule_add(struct mlx5_ib_dev *dev, 416832269441SYishai Hadas struct mlx5_ib_flow_matcher *fs_matcher, 4169bb0ee7dcSJianbo Liu struct mlx5_flow_context *flow_context, 4170b823dd6dSMark Bloch struct mlx5_flow_act *flow_act, 4171bfc5d839SMark Bloch u32 counter_id, 417232269441SYishai Hadas void *cmd_in, int inlen, int dest_id, 417332269441SYishai Hadas int dest_type) 417432269441SYishai Hadas { 4175d4be3f44SYishai Hadas struct mlx5_flow_destination *dst; 4176d4be3f44SYishai Hadas struct mlx5_ib_flow_prio *ft_prio; 4177d4be3f44SYishai Hadas struct mlx5_ib_flow_handler *handler; 4178bfc5d839SMark Bloch int dst_num = 0; 4179d4be3f44SYishai Hadas bool mcast; 4180d4be3f44SYishai Hadas int err; 4181d4be3f44SYishai Hadas 4182d4be3f44SYishai Hadas if (fs_matcher->flow_type != MLX5_IB_FLOW_TYPE_NORMAL) 418332269441SYishai Hadas return ERR_PTR(-EOPNOTSUPP); 4184d4be3f44SYishai Hadas 4185d4be3f44SYishai Hadas if (fs_matcher->priority > MLX5_IB_FLOW_LAST_PRIO) 4186d4be3f44SYishai Hadas return ERR_PTR(-ENOMEM); 4187d4be3f44SYishai Hadas 41888e8aa145SGustavo A. R. Silva dst = kcalloc(2, sizeof(*dst), GFP_KERNEL); 4189d4be3f44SYishai Hadas if (!dst) 4190d4be3f44SYishai Hadas return ERR_PTR(-ENOMEM); 4191d4be3f44SYishai Hadas 4192d4be3f44SYishai Hadas mcast = raw_fs_is_multicast(fs_matcher, cmd_in); 4193d4be3f44SYishai Hadas mutex_lock(&dev->flow_db->lock); 4194d4be3f44SYishai Hadas 4195b47fd4ffSMark Bloch ft_prio = _get_flow_table(dev, fs_matcher, mcast); 4196d4be3f44SYishai Hadas if (IS_ERR(ft_prio)) { 4197d4be3f44SYishai Hadas err = PTR_ERR(ft_prio); 4198d4be3f44SYishai Hadas goto unlock; 4199d4be3f44SYishai Hadas } 4200d4be3f44SYishai Hadas 42016346f0bfSYishai Hadas if (dest_type == MLX5_FLOW_DESTINATION_TYPE_TIR) { 4202bfc5d839SMark Bloch dst[dst_num].type = dest_type; 4203bfc5d839SMark Bloch dst[dst_num].tir_num = dest_id; 4204b823dd6dSMark Bloch flow_act->action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST; 4205a7ee18bdSMark Bloch } else if (dest_type == MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE) { 4206bfc5d839SMark Bloch dst[dst_num].type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE_NUM; 4207bfc5d839SMark Bloch dst[dst_num].ft_num = dest_id; 4208b823dd6dSMark Bloch flow_act->action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST; 4209a7ee18bdSMark Bloch } else { 4210bfc5d839SMark Bloch dst[dst_num].type = MLX5_FLOW_DESTINATION_TYPE_PORT; 4211a7ee18bdSMark Bloch flow_act->action |= MLX5_FLOW_CONTEXT_ACTION_ALLOW; 42126346f0bfSYishai Hadas } 42136346f0bfSYishai Hadas 4214bfc5d839SMark Bloch dst_num++; 4215bfc5d839SMark Bloch 4216bfc5d839SMark Bloch if (flow_act->action & MLX5_FLOW_CONTEXT_ACTION_COUNT) { 4217bfc5d839SMark Bloch dst[dst_num].type = MLX5_FLOW_DESTINATION_TYPE_COUNTER; 4218bfc5d839SMark Bloch dst[dst_num].counter_id = counter_id; 4219bfc5d839SMark Bloch dst_num++; 4220bfc5d839SMark Bloch } 4221bfc5d839SMark Bloch 4222bb0ee7dcSJianbo Liu handler = _create_raw_flow_rule(dev, ft_prio, dst, fs_matcher, 4223bb0ee7dcSJianbo Liu flow_context, flow_act, 4224bfc5d839SMark Bloch cmd_in, inlen, dst_num); 4225d4be3f44SYishai Hadas 4226d4be3f44SYishai Hadas if (IS_ERR(handler)) { 4227d4be3f44SYishai Hadas err = PTR_ERR(handler); 4228d4be3f44SYishai Hadas goto destroy_ft; 4229d4be3f44SYishai Hadas } 4230d4be3f44SYishai Hadas 4231d4be3f44SYishai Hadas mutex_unlock(&dev->flow_db->lock); 4232d4be3f44SYishai Hadas atomic_inc(&fs_matcher->usecnt); 4233d4be3f44SYishai Hadas handler->flow_matcher = fs_matcher; 4234d4be3f44SYishai Hadas 4235d4be3f44SYishai Hadas kfree(dst); 4236d4be3f44SYishai Hadas 4237d4be3f44SYishai Hadas return handler; 4238d4be3f44SYishai Hadas 4239d4be3f44SYishai Hadas destroy_ft: 4240d4be3f44SYishai Hadas put_flow_table(dev, ft_prio, false); 4241d4be3f44SYishai Hadas unlock: 4242d4be3f44SYishai Hadas mutex_unlock(&dev->flow_db->lock); 4243d4be3f44SYishai Hadas kfree(dst); 4244d4be3f44SYishai Hadas 4245d4be3f44SYishai Hadas return ERR_PTR(err); 424632269441SYishai Hadas } 424732269441SYishai Hadas 4248c6475a0bSAviad Yehezkel static u32 mlx5_ib_flow_action_flags_to_accel_xfrm_flags(u32 mlx5_flags) 4249c6475a0bSAviad Yehezkel { 4250c6475a0bSAviad Yehezkel u32 flags = 0; 4251c6475a0bSAviad Yehezkel 4252c6475a0bSAviad Yehezkel if (mlx5_flags & MLX5_IB_UAPI_FLOW_ACTION_FLAGS_REQUIRE_METADATA) 4253c6475a0bSAviad Yehezkel flags |= MLX5_ACCEL_XFRM_FLAG_REQUIRE_METADATA; 4254c6475a0bSAviad Yehezkel 4255c6475a0bSAviad Yehezkel return flags; 4256c6475a0bSAviad Yehezkel } 4257c6475a0bSAviad Yehezkel 4258c6475a0bSAviad Yehezkel #define MLX5_FLOW_ACTION_ESP_CREATE_LAST_SUPPORTED MLX5_IB_UAPI_FLOW_ACTION_FLAGS_REQUIRE_METADATA 4259c6475a0bSAviad Yehezkel static struct ib_flow_action * 4260c6475a0bSAviad Yehezkel mlx5_ib_create_flow_action_esp(struct ib_device *device, 4261c6475a0bSAviad Yehezkel const struct ib_flow_action_attrs_esp *attr, 4262c6475a0bSAviad Yehezkel struct uverbs_attr_bundle *attrs) 4263c6475a0bSAviad Yehezkel { 4264c6475a0bSAviad Yehezkel struct mlx5_ib_dev *mdev = to_mdev(device); 4265c6475a0bSAviad Yehezkel struct ib_uverbs_flow_action_esp_keymat_aes_gcm *aes_gcm; 4266c6475a0bSAviad Yehezkel struct mlx5_accel_esp_xfrm_attrs accel_attrs = {}; 4267c6475a0bSAviad Yehezkel struct mlx5_ib_flow_action *action; 4268c6475a0bSAviad Yehezkel u64 action_flags; 4269c6475a0bSAviad Yehezkel u64 flags; 4270c6475a0bSAviad Yehezkel int err = 0; 4271c6475a0bSAviad Yehezkel 4272bccd0622SJason Gunthorpe err = uverbs_get_flags64( 4273bccd0622SJason Gunthorpe &action_flags, attrs, MLX5_IB_ATTR_CREATE_FLOW_ACTION_FLAGS, 4274bccd0622SJason Gunthorpe ((MLX5_FLOW_ACTION_ESP_CREATE_LAST_SUPPORTED << 1) - 1)); 4275bccd0622SJason Gunthorpe if (err) 4276bccd0622SJason Gunthorpe return ERR_PTR(err); 4277c6475a0bSAviad Yehezkel 4278c6475a0bSAviad Yehezkel flags = mlx5_ib_flow_action_flags_to_accel_xfrm_flags(action_flags); 4279c6475a0bSAviad Yehezkel 4280c6475a0bSAviad Yehezkel /* We current only support a subset of the standard features. Only a 4281c6475a0bSAviad Yehezkel * keymat of type AES_GCM, with icv_len == 16, iv_algo == SEQ and esn 4282c6475a0bSAviad Yehezkel * (with overlap). Full offload mode isn't supported. 4283c6475a0bSAviad Yehezkel */ 4284c6475a0bSAviad Yehezkel if (!attr->keymat || attr->replay || attr->encap || 4285c6475a0bSAviad Yehezkel attr->spi || attr->seq || attr->tfc_pad || 4286c6475a0bSAviad Yehezkel attr->hard_limit_pkts || 4287c6475a0bSAviad Yehezkel (attr->flags & ~(IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED | 4288c6475a0bSAviad Yehezkel IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ENCRYPT))) 4289c6475a0bSAviad Yehezkel return ERR_PTR(-EOPNOTSUPP); 4290c6475a0bSAviad Yehezkel 4291c6475a0bSAviad Yehezkel if (attr->keymat->protocol != 4292c6475a0bSAviad Yehezkel IB_UVERBS_FLOW_ACTION_ESP_KEYMAT_AES_GCM) 4293c6475a0bSAviad Yehezkel return ERR_PTR(-EOPNOTSUPP); 4294c6475a0bSAviad Yehezkel 4295c6475a0bSAviad Yehezkel aes_gcm = &attr->keymat->keymat.aes_gcm; 4296c6475a0bSAviad Yehezkel 4297c6475a0bSAviad Yehezkel if (aes_gcm->icv_len != 16 || 4298c6475a0bSAviad Yehezkel aes_gcm->iv_algo != IB_UVERBS_FLOW_ACTION_IV_ALGO_SEQ) 4299c6475a0bSAviad Yehezkel return ERR_PTR(-EOPNOTSUPP); 4300c6475a0bSAviad Yehezkel 4301c6475a0bSAviad Yehezkel action = kmalloc(sizeof(*action), GFP_KERNEL); 4302c6475a0bSAviad Yehezkel if (!action) 4303c6475a0bSAviad Yehezkel return ERR_PTR(-ENOMEM); 4304c6475a0bSAviad Yehezkel 4305c6475a0bSAviad Yehezkel action->esp_aes_gcm.ib_flags = attr->flags; 4306c6475a0bSAviad Yehezkel memcpy(&accel_attrs.keymat.aes_gcm.aes_key, &aes_gcm->aes_key, 4307c6475a0bSAviad Yehezkel sizeof(accel_attrs.keymat.aes_gcm.aes_key)); 4308c6475a0bSAviad Yehezkel accel_attrs.keymat.aes_gcm.key_len = aes_gcm->key_len * 8; 4309c6475a0bSAviad Yehezkel memcpy(&accel_attrs.keymat.aes_gcm.salt, &aes_gcm->salt, 4310c6475a0bSAviad Yehezkel sizeof(accel_attrs.keymat.aes_gcm.salt)); 4311c6475a0bSAviad Yehezkel memcpy(&accel_attrs.keymat.aes_gcm.seq_iv, &aes_gcm->iv, 4312c6475a0bSAviad Yehezkel sizeof(accel_attrs.keymat.aes_gcm.seq_iv)); 4313c6475a0bSAviad Yehezkel accel_attrs.keymat.aes_gcm.icv_len = aes_gcm->icv_len * 8; 4314c6475a0bSAviad Yehezkel accel_attrs.keymat.aes_gcm.iv_algo = MLX5_ACCEL_ESP_AES_GCM_IV_ALGO_SEQ; 4315c6475a0bSAviad Yehezkel accel_attrs.keymat_type = MLX5_ACCEL_ESP_KEYMAT_AES_GCM; 4316c6475a0bSAviad Yehezkel 4317c6475a0bSAviad Yehezkel accel_attrs.esn = attr->esn; 4318c6475a0bSAviad Yehezkel if (attr->flags & IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED) 4319c6475a0bSAviad Yehezkel accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_TRIGGERED; 4320c6475a0bSAviad Yehezkel if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW) 4321c6475a0bSAviad Yehezkel accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP; 4322c6475a0bSAviad Yehezkel 4323c6475a0bSAviad Yehezkel if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ENCRYPT) 4324c6475a0bSAviad Yehezkel accel_attrs.action |= MLX5_ACCEL_ESP_ACTION_ENCRYPT; 4325c6475a0bSAviad Yehezkel 4326c6475a0bSAviad Yehezkel action->esp_aes_gcm.ctx = 4327c6475a0bSAviad Yehezkel mlx5_accel_esp_create_xfrm(mdev->mdev, &accel_attrs, flags); 4328c6475a0bSAviad Yehezkel if (IS_ERR(action->esp_aes_gcm.ctx)) { 4329c6475a0bSAviad Yehezkel err = PTR_ERR(action->esp_aes_gcm.ctx); 4330c6475a0bSAviad Yehezkel goto err_parse; 4331c6475a0bSAviad Yehezkel } 4332c6475a0bSAviad Yehezkel 4333c6475a0bSAviad Yehezkel action->esp_aes_gcm.ib_flags = attr->flags; 4334c6475a0bSAviad Yehezkel 4335c6475a0bSAviad Yehezkel return &action->ib_action; 4336c6475a0bSAviad Yehezkel 4337c6475a0bSAviad Yehezkel err_parse: 4338c6475a0bSAviad Yehezkel kfree(action); 4339c6475a0bSAviad Yehezkel return ERR_PTR(err); 4340c6475a0bSAviad Yehezkel } 4341c6475a0bSAviad Yehezkel 4342349705c1SMatan Barak static int 4343349705c1SMatan Barak mlx5_ib_modify_flow_action_esp(struct ib_flow_action *action, 4344349705c1SMatan Barak const struct ib_flow_action_attrs_esp *attr, 4345349705c1SMatan Barak struct uverbs_attr_bundle *attrs) 4346349705c1SMatan Barak { 4347349705c1SMatan Barak struct mlx5_ib_flow_action *maction = to_mflow_act(action); 4348349705c1SMatan Barak struct mlx5_accel_esp_xfrm_attrs accel_attrs; 4349349705c1SMatan Barak int err = 0; 4350349705c1SMatan Barak 4351349705c1SMatan Barak if (attr->keymat || attr->replay || attr->encap || 4352349705c1SMatan Barak attr->spi || attr->seq || attr->tfc_pad || 4353349705c1SMatan Barak attr->hard_limit_pkts || 4354349705c1SMatan Barak (attr->flags & ~(IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED | 4355349705c1SMatan Barak IB_FLOW_ACTION_ESP_FLAGS_MOD_ESP_ATTRS | 4356349705c1SMatan Barak IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW))) 4357349705c1SMatan Barak return -EOPNOTSUPP; 4358349705c1SMatan Barak 4359349705c1SMatan Barak /* Only the ESN value or the MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP can 4360349705c1SMatan Barak * be modified. 4361349705c1SMatan Barak */ 4362349705c1SMatan Barak if (!(maction->esp_aes_gcm.ib_flags & 4363349705c1SMatan Barak IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED) && 4364349705c1SMatan Barak attr->flags & (IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED | 4365349705c1SMatan Barak IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW)) 4366349705c1SMatan Barak return -EINVAL; 4367349705c1SMatan Barak 4368349705c1SMatan Barak memcpy(&accel_attrs, &maction->esp_aes_gcm.ctx->attrs, 4369349705c1SMatan Barak sizeof(accel_attrs)); 4370349705c1SMatan Barak 4371349705c1SMatan Barak accel_attrs.esn = attr->esn; 4372349705c1SMatan Barak if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW) 4373349705c1SMatan Barak accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP; 4374349705c1SMatan Barak else 4375349705c1SMatan Barak accel_attrs.flags &= ~MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP; 4376349705c1SMatan Barak 4377349705c1SMatan Barak err = mlx5_accel_esp_modify_xfrm(maction->esp_aes_gcm.ctx, 4378349705c1SMatan Barak &accel_attrs); 4379349705c1SMatan Barak if (err) 4380349705c1SMatan Barak return err; 4381349705c1SMatan Barak 4382349705c1SMatan Barak maction->esp_aes_gcm.ib_flags &= 4383349705c1SMatan Barak ~IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW; 4384349705c1SMatan Barak maction->esp_aes_gcm.ib_flags |= 4385349705c1SMatan Barak attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW; 4386349705c1SMatan Barak 4387349705c1SMatan Barak return 0; 4388349705c1SMatan Barak } 4389349705c1SMatan Barak 4390c6475a0bSAviad Yehezkel static int mlx5_ib_destroy_flow_action(struct ib_flow_action *action) 4391c6475a0bSAviad Yehezkel { 4392c6475a0bSAviad Yehezkel struct mlx5_ib_flow_action *maction = to_mflow_act(action); 4393c6475a0bSAviad Yehezkel 4394c6475a0bSAviad Yehezkel switch (action->type) { 4395c6475a0bSAviad Yehezkel case IB_FLOW_ACTION_ESP: 4396c6475a0bSAviad Yehezkel /* 4397c6475a0bSAviad Yehezkel * We only support aes_gcm by now, so we implicitly know this is 4398c6475a0bSAviad Yehezkel * the underline crypto. 4399c6475a0bSAviad Yehezkel */ 4400c6475a0bSAviad Yehezkel mlx5_accel_esp_destroy_xfrm(maction->esp_aes_gcm.ctx); 4401c6475a0bSAviad Yehezkel break; 4402b4749bf2SMark Bloch case IB_FLOW_ACTION_UNSPECIFIED: 4403b4749bf2SMark Bloch mlx5_ib_destroy_flow_action_raw(maction); 4404b4749bf2SMark Bloch break; 4405c6475a0bSAviad Yehezkel default: 4406c6475a0bSAviad Yehezkel WARN_ON(true); 4407c6475a0bSAviad Yehezkel break; 4408c6475a0bSAviad Yehezkel } 4409c6475a0bSAviad Yehezkel 4410c6475a0bSAviad Yehezkel kfree(maction); 4411c6475a0bSAviad Yehezkel return 0; 4412c6475a0bSAviad Yehezkel } 4413c6475a0bSAviad Yehezkel 4414e126ba97SEli Cohen static int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid) 4415e126ba97SEli Cohen { 4416e126ba97SEli Cohen struct mlx5_ib_dev *dev = to_mdev(ibqp->device); 441781e30880SYishai Hadas struct mlx5_ib_qp *mqp = to_mqp(ibqp); 4418e126ba97SEli Cohen int err; 4419539ec982SYishai Hadas u16 uid; 4420539ec982SYishai Hadas 4421539ec982SYishai Hadas uid = ibqp->pd ? 4422539ec982SYishai Hadas to_mpd(ibqp->pd)->uid : 0; 4423e126ba97SEli Cohen 442481e30880SYishai Hadas if (mqp->flags & MLX5_IB_QP_UNDERLAY) { 442581e30880SYishai Hadas mlx5_ib_dbg(dev, "Attaching a multi cast group to underlay QP is not supported\n"); 442681e30880SYishai Hadas return -EOPNOTSUPP; 442781e30880SYishai Hadas } 442881e30880SYishai Hadas 4429539ec982SYishai Hadas err = mlx5_cmd_attach_mcg(dev->mdev, gid, ibqp->qp_num, uid); 4430e126ba97SEli Cohen if (err) 4431e126ba97SEli Cohen mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n", 4432e126ba97SEli Cohen ibqp->qp_num, gid->raw); 4433e126ba97SEli Cohen 4434e126ba97SEli Cohen return err; 4435e126ba97SEli Cohen } 4436e126ba97SEli Cohen 4437e126ba97SEli Cohen static int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid) 4438e126ba97SEli Cohen { 4439e126ba97SEli Cohen struct mlx5_ib_dev *dev = to_mdev(ibqp->device); 4440e126ba97SEli Cohen int err; 4441539ec982SYishai Hadas u16 uid; 4442e126ba97SEli Cohen 4443539ec982SYishai Hadas uid = ibqp->pd ? 4444539ec982SYishai Hadas to_mpd(ibqp->pd)->uid : 0; 4445539ec982SYishai Hadas err = mlx5_cmd_detach_mcg(dev->mdev, gid, ibqp->qp_num, uid); 4446e126ba97SEli Cohen if (err) 4447e126ba97SEli Cohen mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n", 4448e126ba97SEli Cohen ibqp->qp_num, gid->raw); 4449e126ba97SEli Cohen 4450e126ba97SEli Cohen return err; 4451e126ba97SEli Cohen } 4452e126ba97SEli Cohen 4453e126ba97SEli Cohen static int init_node_data(struct mlx5_ib_dev *dev) 4454e126ba97SEli Cohen { 44551b5daf11SMajd Dibbiny int err; 4456e126ba97SEli Cohen 44571b5daf11SMajd Dibbiny err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc); 4458e126ba97SEli Cohen if (err) 4459e126ba97SEli Cohen return err; 44601b5daf11SMajd Dibbiny 44611b5daf11SMajd Dibbiny dev->mdev->rev_id = dev->mdev->pdev->revision; 44621b5daf11SMajd Dibbiny 44631b5daf11SMajd Dibbiny return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid); 4464e126ba97SEli Cohen } 4465e126ba97SEli Cohen 4466508a523fSParav Pandit static ssize_t fw_pages_show(struct device *device, 4467508a523fSParav Pandit struct device_attribute *attr, char *buf) 4468e126ba97SEli Cohen { 4469e126ba97SEli Cohen struct mlx5_ib_dev *dev = 447054747231SParav Pandit rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev); 4471e126ba97SEli Cohen 44729603b61dSJack Morgenstein return sprintf(buf, "%d\n", dev->mdev->priv.fw_pages); 4473e126ba97SEli Cohen } 4474508a523fSParav Pandit static DEVICE_ATTR_RO(fw_pages); 4475e126ba97SEli Cohen 4476508a523fSParav Pandit static ssize_t reg_pages_show(struct device *device, 4477e126ba97SEli Cohen struct device_attribute *attr, char *buf) 4478e126ba97SEli Cohen { 4479e126ba97SEli Cohen struct mlx5_ib_dev *dev = 448054747231SParav Pandit rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev); 4481e126ba97SEli Cohen 44826aec21f6SHaggai Eran return sprintf(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages)); 4483e126ba97SEli Cohen } 4484508a523fSParav Pandit static DEVICE_ATTR_RO(reg_pages); 4485e126ba97SEli Cohen 4486508a523fSParav Pandit static ssize_t hca_type_show(struct device *device, 4487508a523fSParav Pandit struct device_attribute *attr, char *buf) 4488e126ba97SEli Cohen { 4489e126ba97SEli Cohen struct mlx5_ib_dev *dev = 449054747231SParav Pandit rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev); 449154747231SParav Pandit 44929603b61dSJack Morgenstein return sprintf(buf, "MT%d\n", dev->mdev->pdev->device); 4493e126ba97SEli Cohen } 4494508a523fSParav Pandit static DEVICE_ATTR_RO(hca_type); 4495e126ba97SEli Cohen 4496508a523fSParav Pandit static ssize_t hw_rev_show(struct device *device, 4497508a523fSParav Pandit struct device_attribute *attr, char *buf) 4498e126ba97SEli Cohen { 4499e126ba97SEli Cohen struct mlx5_ib_dev *dev = 450054747231SParav Pandit rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev); 450154747231SParav Pandit 45029603b61dSJack Morgenstein return sprintf(buf, "%x\n", dev->mdev->rev_id); 4503e126ba97SEli Cohen } 4504508a523fSParav Pandit static DEVICE_ATTR_RO(hw_rev); 4505e126ba97SEli Cohen 4506508a523fSParav Pandit static ssize_t board_id_show(struct device *device, 4507508a523fSParav Pandit struct device_attribute *attr, char *buf) 4508e126ba97SEli Cohen { 4509e126ba97SEli Cohen struct mlx5_ib_dev *dev = 451054747231SParav Pandit rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev); 451154747231SParav Pandit 4512e126ba97SEli Cohen return sprintf(buf, "%.*s\n", MLX5_BOARD_ID_LEN, 45139603b61dSJack Morgenstein dev->mdev->board_id); 4514e126ba97SEli Cohen } 4515508a523fSParav Pandit static DEVICE_ATTR_RO(board_id); 4516e126ba97SEli Cohen 4517508a523fSParav Pandit static struct attribute *mlx5_class_attributes[] = { 4518508a523fSParav Pandit &dev_attr_hw_rev.attr, 4519508a523fSParav Pandit &dev_attr_hca_type.attr, 4520508a523fSParav Pandit &dev_attr_board_id.attr, 4521508a523fSParav Pandit &dev_attr_fw_pages.attr, 4522508a523fSParav Pandit &dev_attr_reg_pages.attr, 4523508a523fSParav Pandit NULL, 4524508a523fSParav Pandit }; 4525e126ba97SEli Cohen 4526508a523fSParav Pandit static const struct attribute_group mlx5_attr_group = { 4527508a523fSParav Pandit .attrs = mlx5_class_attributes, 4528e126ba97SEli Cohen }; 4529e126ba97SEli Cohen 45307722f47eSHaggai Eran static void pkey_change_handler(struct work_struct *work) 45317722f47eSHaggai Eran { 45327722f47eSHaggai Eran struct mlx5_ib_port_resources *ports = 45337722f47eSHaggai Eran container_of(work, struct mlx5_ib_port_resources, 45347722f47eSHaggai Eran pkey_change_work); 45357722f47eSHaggai Eran 45367722f47eSHaggai Eran mutex_lock(&ports->devr->mutex); 45377722f47eSHaggai Eran mlx5_ib_gsi_pkey_change(ports->gsi); 45387722f47eSHaggai Eran mutex_unlock(&ports->devr->mutex); 45397722f47eSHaggai Eran } 45407722f47eSHaggai Eran 454189ea94a7SMaor Gottlieb static void mlx5_ib_handle_internal_error(struct mlx5_ib_dev *ibdev) 454289ea94a7SMaor Gottlieb { 454389ea94a7SMaor Gottlieb struct mlx5_ib_qp *mqp; 454489ea94a7SMaor Gottlieb struct mlx5_ib_cq *send_mcq, *recv_mcq; 454589ea94a7SMaor Gottlieb struct mlx5_core_cq *mcq; 454689ea94a7SMaor Gottlieb struct list_head cq_armed_list; 454789ea94a7SMaor Gottlieb unsigned long flags_qp; 454889ea94a7SMaor Gottlieb unsigned long flags_cq; 454989ea94a7SMaor Gottlieb unsigned long flags; 455089ea94a7SMaor Gottlieb 455189ea94a7SMaor Gottlieb INIT_LIST_HEAD(&cq_armed_list); 455289ea94a7SMaor Gottlieb 455389ea94a7SMaor Gottlieb /* Go over qp list reside on that ibdev, sync with create/destroy qp.*/ 455489ea94a7SMaor Gottlieb spin_lock_irqsave(&ibdev->reset_flow_resource_lock, flags); 455589ea94a7SMaor Gottlieb list_for_each_entry(mqp, &ibdev->qp_list, qps_list) { 455689ea94a7SMaor Gottlieb spin_lock_irqsave(&mqp->sq.lock, flags_qp); 455789ea94a7SMaor Gottlieb if (mqp->sq.tail != mqp->sq.head) { 455889ea94a7SMaor Gottlieb send_mcq = to_mcq(mqp->ibqp.send_cq); 455989ea94a7SMaor Gottlieb spin_lock_irqsave(&send_mcq->lock, flags_cq); 456089ea94a7SMaor Gottlieb if (send_mcq->mcq.comp && 456189ea94a7SMaor Gottlieb mqp->ibqp.send_cq->comp_handler) { 456289ea94a7SMaor Gottlieb if (!send_mcq->mcq.reset_notify_added) { 456389ea94a7SMaor Gottlieb send_mcq->mcq.reset_notify_added = 1; 456489ea94a7SMaor Gottlieb list_add_tail(&send_mcq->mcq.reset_notify, 456589ea94a7SMaor Gottlieb &cq_armed_list); 456689ea94a7SMaor Gottlieb } 456789ea94a7SMaor Gottlieb } 456889ea94a7SMaor Gottlieb spin_unlock_irqrestore(&send_mcq->lock, flags_cq); 456989ea94a7SMaor Gottlieb } 457089ea94a7SMaor Gottlieb spin_unlock_irqrestore(&mqp->sq.lock, flags_qp); 457189ea94a7SMaor Gottlieb spin_lock_irqsave(&mqp->rq.lock, flags_qp); 457289ea94a7SMaor Gottlieb /* no handling is needed for SRQ */ 457389ea94a7SMaor Gottlieb if (!mqp->ibqp.srq) { 457489ea94a7SMaor Gottlieb if (mqp->rq.tail != mqp->rq.head) { 457589ea94a7SMaor Gottlieb recv_mcq = to_mcq(mqp->ibqp.recv_cq); 457689ea94a7SMaor Gottlieb spin_lock_irqsave(&recv_mcq->lock, flags_cq); 457789ea94a7SMaor Gottlieb if (recv_mcq->mcq.comp && 457889ea94a7SMaor Gottlieb mqp->ibqp.recv_cq->comp_handler) { 457989ea94a7SMaor Gottlieb if (!recv_mcq->mcq.reset_notify_added) { 458089ea94a7SMaor Gottlieb recv_mcq->mcq.reset_notify_added = 1; 458189ea94a7SMaor Gottlieb list_add_tail(&recv_mcq->mcq.reset_notify, 458289ea94a7SMaor Gottlieb &cq_armed_list); 458389ea94a7SMaor Gottlieb } 458489ea94a7SMaor Gottlieb } 458589ea94a7SMaor Gottlieb spin_unlock_irqrestore(&recv_mcq->lock, 458689ea94a7SMaor Gottlieb flags_cq); 458789ea94a7SMaor Gottlieb } 458889ea94a7SMaor Gottlieb } 458989ea94a7SMaor Gottlieb spin_unlock_irqrestore(&mqp->rq.lock, flags_qp); 459089ea94a7SMaor Gottlieb } 459189ea94a7SMaor Gottlieb /*At that point all inflight post send were put to be executed as of we 459289ea94a7SMaor Gottlieb * lock/unlock above locks Now need to arm all involved CQs. 459389ea94a7SMaor Gottlieb */ 459489ea94a7SMaor Gottlieb list_for_each_entry(mcq, &cq_armed_list, reset_notify) { 45954e0e2ea1SYishai Hadas mcq->comp(mcq, NULL); 459689ea94a7SMaor Gottlieb } 459789ea94a7SMaor Gottlieb spin_unlock_irqrestore(&ibdev->reset_flow_resource_lock, flags); 459889ea94a7SMaor Gottlieb } 459989ea94a7SMaor Gottlieb 460003404e8aSMaor Gottlieb static void delay_drop_handler(struct work_struct *work) 460103404e8aSMaor Gottlieb { 460203404e8aSMaor Gottlieb int err; 460303404e8aSMaor Gottlieb struct mlx5_ib_delay_drop *delay_drop = 460403404e8aSMaor Gottlieb container_of(work, struct mlx5_ib_delay_drop, 460503404e8aSMaor Gottlieb delay_drop_work); 460603404e8aSMaor Gottlieb 4607fe248c3aSMaor Gottlieb atomic_inc(&delay_drop->events_cnt); 4608fe248c3aSMaor Gottlieb 460903404e8aSMaor Gottlieb mutex_lock(&delay_drop->lock); 461003404e8aSMaor Gottlieb err = mlx5_core_set_delay_drop(delay_drop->dev->mdev, 461103404e8aSMaor Gottlieb delay_drop->timeout); 461203404e8aSMaor Gottlieb if (err) { 461303404e8aSMaor Gottlieb mlx5_ib_warn(delay_drop->dev, "Failed to set delay drop, timeout=%u\n", 461403404e8aSMaor Gottlieb delay_drop->timeout); 461503404e8aSMaor Gottlieb delay_drop->activate = false; 461603404e8aSMaor Gottlieb } 461703404e8aSMaor Gottlieb mutex_unlock(&delay_drop->lock); 461803404e8aSMaor Gottlieb } 461903404e8aSMaor Gottlieb 462009e574faSSaeed Mahameed static void handle_general_event(struct mlx5_ib_dev *ibdev, struct mlx5_eqe *eqe, 462109e574faSSaeed Mahameed struct ib_event *ibev) 462209e574faSSaeed Mahameed { 46236cfdc7e4SAya Levin u8 port = (eqe->data.port.port >> 4) & 0xf; 46246cfdc7e4SAya Levin 462509e574faSSaeed Mahameed switch (eqe->sub_type) { 462609e574faSSaeed Mahameed case MLX5_GENERAL_SUBTYPE_DELAY_DROP_TIMEOUT: 46276cfdc7e4SAya Levin if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) == 46286cfdc7e4SAya Levin IB_LINK_LAYER_ETHERNET) 462909e574faSSaeed Mahameed schedule_work(&ibdev->delay_drop.delay_drop_work); 463009e574faSSaeed Mahameed break; 463109e574faSSaeed Mahameed default: /* do nothing */ 463209e574faSSaeed Mahameed return; 463309e574faSSaeed Mahameed } 463409e574faSSaeed Mahameed } 463509e574faSSaeed Mahameed 4636134e9349SSaeed Mahameed static int handle_port_change(struct mlx5_ib_dev *ibdev, struct mlx5_eqe *eqe, 4637134e9349SSaeed Mahameed struct ib_event *ibev) 4638134e9349SSaeed Mahameed { 4639134e9349SSaeed Mahameed u8 port = (eqe->data.port.port >> 4) & 0xf; 4640134e9349SSaeed Mahameed 4641134e9349SSaeed Mahameed ibev->element.port_num = port; 4642134e9349SSaeed Mahameed 4643134e9349SSaeed Mahameed switch (eqe->sub_type) { 4644134e9349SSaeed Mahameed case MLX5_PORT_CHANGE_SUBTYPE_ACTIVE: 4645134e9349SSaeed Mahameed case MLX5_PORT_CHANGE_SUBTYPE_DOWN: 4646134e9349SSaeed Mahameed case MLX5_PORT_CHANGE_SUBTYPE_INITIALIZED: 4647134e9349SSaeed Mahameed /* In RoCE, port up/down events are handled in 4648134e9349SSaeed Mahameed * mlx5_netdev_event(). 4649134e9349SSaeed Mahameed */ 4650134e9349SSaeed Mahameed if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) == 4651134e9349SSaeed Mahameed IB_LINK_LAYER_ETHERNET) 4652134e9349SSaeed Mahameed return -EINVAL; 4653134e9349SSaeed Mahameed 4654134e9349SSaeed Mahameed ibev->event = (eqe->sub_type == MLX5_PORT_CHANGE_SUBTYPE_ACTIVE) ? 4655134e9349SSaeed Mahameed IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR; 4656134e9349SSaeed Mahameed break; 4657134e9349SSaeed Mahameed 4658134e9349SSaeed Mahameed case MLX5_PORT_CHANGE_SUBTYPE_LID: 4659134e9349SSaeed Mahameed ibev->event = IB_EVENT_LID_CHANGE; 4660134e9349SSaeed Mahameed break; 4661134e9349SSaeed Mahameed 4662134e9349SSaeed Mahameed case MLX5_PORT_CHANGE_SUBTYPE_PKEY: 4663134e9349SSaeed Mahameed ibev->event = IB_EVENT_PKEY_CHANGE; 4664134e9349SSaeed Mahameed schedule_work(&ibdev->devr.ports[port - 1].pkey_change_work); 4665134e9349SSaeed Mahameed break; 4666134e9349SSaeed Mahameed 4667134e9349SSaeed Mahameed case MLX5_PORT_CHANGE_SUBTYPE_GUID: 4668134e9349SSaeed Mahameed ibev->event = IB_EVENT_GID_CHANGE; 4669134e9349SSaeed Mahameed break; 4670134e9349SSaeed Mahameed 4671134e9349SSaeed Mahameed case MLX5_PORT_CHANGE_SUBTYPE_CLIENT_REREG: 4672134e9349SSaeed Mahameed ibev->event = IB_EVENT_CLIENT_REREGISTER; 4673134e9349SSaeed Mahameed break; 4674134e9349SSaeed Mahameed default: 4675134e9349SSaeed Mahameed return -EINVAL; 4676134e9349SSaeed Mahameed } 4677134e9349SSaeed Mahameed 4678134e9349SSaeed Mahameed return 0; 4679134e9349SSaeed Mahameed } 4680134e9349SSaeed Mahameed 4681d69a24e0SDaniel Jurgens static void mlx5_ib_handle_event(struct work_struct *_work) 4682e126ba97SEli Cohen { 4683d69a24e0SDaniel Jurgens struct mlx5_ib_event_work *work = 4684d69a24e0SDaniel Jurgens container_of(_work, struct mlx5_ib_event_work, work); 4685d69a24e0SDaniel Jurgens struct mlx5_ib_dev *ibdev; 4686e126ba97SEli Cohen struct ib_event ibev; 4687dbaaff2aSEli Cohen bool fatal = false; 4688e126ba97SEli Cohen 4689df097a27SSaeed Mahameed if (work->is_slave) { 4690df097a27SSaeed Mahameed ibdev = mlx5_ib_get_ibdev_from_mpi(work->mpi); 4691d69a24e0SDaniel Jurgens if (!ibdev) 4692d69a24e0SDaniel Jurgens goto out; 4693d69a24e0SDaniel Jurgens } else { 4694df097a27SSaeed Mahameed ibdev = work->dev; 4695d69a24e0SDaniel Jurgens } 4696d69a24e0SDaniel Jurgens 4697d69a24e0SDaniel Jurgens switch (work->event) { 4698e126ba97SEli Cohen case MLX5_DEV_EVENT_SYS_ERROR: 4699e126ba97SEli Cohen ibev.event = IB_EVENT_DEVICE_FATAL; 470089ea94a7SMaor Gottlieb mlx5_ib_handle_internal_error(ibdev); 4701134e9349SSaeed Mahameed ibev.element.port_num = (u8)(unsigned long)work->param; 4702dbaaff2aSEli Cohen fatal = true; 4703e126ba97SEli Cohen break; 4704134e9349SSaeed Mahameed case MLX5_EVENT_TYPE_PORT_CHANGE: 4705134e9349SSaeed Mahameed if (handle_port_change(ibdev, work->param, &ibev)) 4706d69a24e0SDaniel Jurgens goto out; 4707e126ba97SEli Cohen break; 470809e574faSSaeed Mahameed case MLX5_EVENT_TYPE_GENERAL_EVENT: 470909e574faSSaeed Mahameed handle_general_event(ibdev, work->param, &ibev); 471009e574faSSaeed Mahameed /* fall through */ 4711bdc37924SSaeed Mahameed default: 471203404e8aSMaor Gottlieb goto out; 4713e126ba97SEli Cohen } 4714e126ba97SEli Cohen 4715e126ba97SEli Cohen ibev.device = &ibdev->ib_dev; 4716e126ba97SEli Cohen 4717134e9349SSaeed Mahameed if (!rdma_is_port_valid(&ibdev->ib_dev, ibev.element.port_num)) { 4718134e9349SSaeed Mahameed mlx5_ib_warn(ibdev, "warning: event on port %d\n", ibev.element.port_num); 471903404e8aSMaor Gottlieb goto out; 4720a0c84c32SEli Cohen } 4721a0c84c32SEli Cohen 4722e126ba97SEli Cohen if (ibdev->ib_active) 4723e126ba97SEli Cohen ib_dispatch_event(&ibev); 4724dbaaff2aSEli Cohen 4725dbaaff2aSEli Cohen if (fatal) 4726dbaaff2aSEli Cohen ibdev->ib_active = false; 472703404e8aSMaor Gottlieb out: 4728d69a24e0SDaniel Jurgens kfree(work); 4729d69a24e0SDaniel Jurgens } 4730d69a24e0SDaniel Jurgens 4731df097a27SSaeed Mahameed static int mlx5_ib_event(struct notifier_block *nb, 4732df097a27SSaeed Mahameed unsigned long event, void *param) 4733d69a24e0SDaniel Jurgens { 4734d69a24e0SDaniel Jurgens struct mlx5_ib_event_work *work; 4735d69a24e0SDaniel Jurgens 4736d69a24e0SDaniel Jurgens work = kmalloc(sizeof(*work), GFP_ATOMIC); 473710bea9c8SLeon Romanovsky if (!work) 4738df097a27SSaeed Mahameed return NOTIFY_DONE; 473910bea9c8SLeon Romanovsky 4740d69a24e0SDaniel Jurgens INIT_WORK(&work->work, mlx5_ib_handle_event); 4741df097a27SSaeed Mahameed work->dev = container_of(nb, struct mlx5_ib_dev, mdev_events); 4742df097a27SSaeed Mahameed work->is_slave = false; 4743d69a24e0SDaniel Jurgens work->param = param; 4744d69a24e0SDaniel Jurgens work->event = event; 4745d69a24e0SDaniel Jurgens 4746d69a24e0SDaniel Jurgens queue_work(mlx5_ib_event_wq, &work->work); 4747df097a27SSaeed Mahameed 4748df097a27SSaeed Mahameed return NOTIFY_OK; 4749df097a27SSaeed Mahameed } 4750df097a27SSaeed Mahameed 4751df097a27SSaeed Mahameed static int mlx5_ib_event_slave_port(struct notifier_block *nb, 4752df097a27SSaeed Mahameed unsigned long event, void *param) 4753df097a27SSaeed Mahameed { 4754df097a27SSaeed Mahameed struct mlx5_ib_event_work *work; 4755df097a27SSaeed Mahameed 4756df097a27SSaeed Mahameed work = kmalloc(sizeof(*work), GFP_ATOMIC); 4757df097a27SSaeed Mahameed if (!work) 4758df097a27SSaeed Mahameed return NOTIFY_DONE; 4759df097a27SSaeed Mahameed 4760df097a27SSaeed Mahameed INIT_WORK(&work->work, mlx5_ib_handle_event); 4761df097a27SSaeed Mahameed work->mpi = container_of(nb, struct mlx5_ib_multiport_info, mdev_events); 4762df097a27SSaeed Mahameed work->is_slave = true; 4763df097a27SSaeed Mahameed work->param = param; 4764df097a27SSaeed Mahameed work->event = event; 4765df097a27SSaeed Mahameed queue_work(mlx5_ib_event_wq, &work->work); 4766df097a27SSaeed Mahameed 4767df097a27SSaeed Mahameed return NOTIFY_OK; 4768d69a24e0SDaniel Jurgens } 4769d69a24e0SDaniel Jurgens 4770c43f1112SMaor Gottlieb static int set_has_smi_cap(struct mlx5_ib_dev *dev) 4771c43f1112SMaor Gottlieb { 4772c43f1112SMaor Gottlieb struct mlx5_hca_vport_context vport_ctx; 4773c43f1112SMaor Gottlieb int err; 4774c43f1112SMaor Gottlieb int port; 4775c43f1112SMaor Gottlieb 4776a989ea01SMark Bloch for (port = 1; port <= ARRAY_SIZE(dev->mdev->port_caps); port++) { 4777c43f1112SMaor Gottlieb dev->mdev->port_caps[port - 1].has_smi = false; 4778c43f1112SMaor Gottlieb if (MLX5_CAP_GEN(dev->mdev, port_type) == 4779c43f1112SMaor Gottlieb MLX5_CAP_PORT_TYPE_IB) { 4780c43f1112SMaor Gottlieb if (MLX5_CAP_GEN(dev->mdev, ib_virt)) { 4781c43f1112SMaor Gottlieb err = mlx5_query_hca_vport_context(dev->mdev, 0, 4782c43f1112SMaor Gottlieb port, 0, 4783c43f1112SMaor Gottlieb &vport_ctx); 4784c43f1112SMaor Gottlieb if (err) { 4785c43f1112SMaor Gottlieb mlx5_ib_err(dev, "query_hca_vport_context for port=%d failed %d\n", 4786c43f1112SMaor Gottlieb port, err); 4787c43f1112SMaor Gottlieb return err; 4788c43f1112SMaor Gottlieb } 4789c43f1112SMaor Gottlieb dev->mdev->port_caps[port - 1].has_smi = 4790c43f1112SMaor Gottlieb vport_ctx.has_smi; 4791c43f1112SMaor Gottlieb } else { 4792c43f1112SMaor Gottlieb dev->mdev->port_caps[port - 1].has_smi = true; 4793c43f1112SMaor Gottlieb } 4794c43f1112SMaor Gottlieb } 4795c43f1112SMaor Gottlieb } 4796c43f1112SMaor Gottlieb return 0; 4797c43f1112SMaor Gottlieb } 4798c43f1112SMaor Gottlieb 4799e126ba97SEli Cohen static void get_ext_port_caps(struct mlx5_ib_dev *dev) 4800e126ba97SEli Cohen { 4801e126ba97SEli Cohen int port; 4802e126ba97SEli Cohen 4803508562d6SDaniel Jurgens for (port = 1; port <= dev->num_ports; port++) 4804e126ba97SEli Cohen mlx5_query_ext_port_caps(dev, port); 4805e126ba97SEli Cohen } 4806e126ba97SEli Cohen 480726628e2dSMark Bloch static int __get_port_caps(struct mlx5_ib_dev *dev, u8 port) 4808e126ba97SEli Cohen { 4809e126ba97SEli Cohen struct ib_device_attr *dprops = NULL; 4810e126ba97SEli Cohen struct ib_port_attr *pprops = NULL; 4811f614fc15SDan Carpenter int err = -ENOMEM; 4812e126ba97SEli Cohen 481350ba3c18SLeon Romanovsky pprops = kzalloc(sizeof(*pprops), GFP_KERNEL); 4814e126ba97SEli Cohen if (!pprops) 4815e126ba97SEli Cohen goto out; 4816e126ba97SEli Cohen 4817e126ba97SEli Cohen dprops = kmalloc(sizeof(*dprops), GFP_KERNEL); 4818e126ba97SEli Cohen if (!dprops) 4819e126ba97SEli Cohen goto out; 4820e126ba97SEli Cohen 482148357091SLeon Romanovsky err = mlx5_ib_query_device(&dev->ib_dev, dprops, NULL); 4822e126ba97SEli Cohen if (err) { 4823e126ba97SEli Cohen mlx5_ib_warn(dev, "query_device failed %d\n", err); 4824e126ba97SEli Cohen goto out; 4825e126ba97SEli Cohen } 4826e126ba97SEli Cohen 4827e126ba97SEli Cohen err = mlx5_ib_query_port(&dev->ib_dev, port, pprops); 4828e126ba97SEli Cohen if (err) { 4829938fe83cSSaeed Mahameed mlx5_ib_warn(dev, "query_port %d failed %d\n", 4830938fe83cSSaeed Mahameed port, err); 483132f69e4bSDaniel Jurgens goto out; 4832e126ba97SEli Cohen } 483332f69e4bSDaniel Jurgens 4834938fe83cSSaeed Mahameed dev->mdev->port_caps[port - 1].pkey_table_len = 4835938fe83cSSaeed Mahameed dprops->max_pkeys; 4836938fe83cSSaeed Mahameed dev->mdev->port_caps[port - 1].gid_table_len = 4837938fe83cSSaeed Mahameed pprops->gid_tbl_len; 483832f69e4bSDaniel Jurgens mlx5_ib_dbg(dev, "port %d: pkey_table_len %d, gid_table_len %d\n", 483932f69e4bSDaniel Jurgens port, dprops->max_pkeys, pprops->gid_tbl_len); 4840e126ba97SEli Cohen 4841e126ba97SEli Cohen out: 4842e126ba97SEli Cohen kfree(pprops); 4843e126ba97SEli Cohen kfree(dprops); 4844e126ba97SEli Cohen 4845e126ba97SEli Cohen return err; 4846e126ba97SEli Cohen } 4847e126ba97SEli Cohen 484826628e2dSMark Bloch static int get_port_caps(struct mlx5_ib_dev *dev, u8 port) 484926628e2dSMark Bloch { 485026628e2dSMark Bloch /* For representors use port 1, is this is the only native 485126628e2dSMark Bloch * port 485226628e2dSMark Bloch */ 485326628e2dSMark Bloch if (dev->is_rep) 485426628e2dSMark Bloch return __get_port_caps(dev, 1); 485526628e2dSMark Bloch return __get_port_caps(dev, port); 485626628e2dSMark Bloch } 485726628e2dSMark Bloch 4858e126ba97SEli Cohen static void destroy_umrc_res(struct mlx5_ib_dev *dev) 4859e126ba97SEli Cohen { 4860e126ba97SEli Cohen int err; 4861e126ba97SEli Cohen 4862e126ba97SEli Cohen err = mlx5_mr_cache_cleanup(dev); 4863e126ba97SEli Cohen if (err) 4864e126ba97SEli Cohen mlx5_ib_warn(dev, "mr cache cleanup failed\n"); 4865e126ba97SEli Cohen 486632927e28SMark Bloch if (dev->umrc.qp) 4867c4367a26SShamir Rabinovitch mlx5_ib_destroy_qp(dev->umrc.qp, NULL); 486832927e28SMark Bloch if (dev->umrc.cq) 4869add08d76SChristoph Hellwig ib_free_cq(dev->umrc.cq); 487032927e28SMark Bloch if (dev->umrc.pd) 4871e126ba97SEli Cohen ib_dealloc_pd(dev->umrc.pd); 4872e126ba97SEli Cohen } 4873e126ba97SEli Cohen 4874e126ba97SEli Cohen enum { 4875e126ba97SEli Cohen MAX_UMR_WR = 128, 4876e126ba97SEli Cohen }; 4877e126ba97SEli Cohen 4878e126ba97SEli Cohen static int create_umr_res(struct mlx5_ib_dev *dev) 4879e126ba97SEli Cohen { 4880e126ba97SEli Cohen struct ib_qp_init_attr *init_attr = NULL; 4881e126ba97SEli Cohen struct ib_qp_attr *attr = NULL; 4882e126ba97SEli Cohen struct ib_pd *pd; 4883e126ba97SEli Cohen struct ib_cq *cq; 4884e126ba97SEli Cohen struct ib_qp *qp; 4885e126ba97SEli Cohen int ret; 4886e126ba97SEli Cohen 4887e126ba97SEli Cohen attr = kzalloc(sizeof(*attr), GFP_KERNEL); 4888e126ba97SEli Cohen init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL); 4889e126ba97SEli Cohen if (!attr || !init_attr) { 4890e126ba97SEli Cohen ret = -ENOMEM; 4891e126ba97SEli Cohen goto error_0; 4892e126ba97SEli Cohen } 4893e126ba97SEli Cohen 4894ed082d36SChristoph Hellwig pd = ib_alloc_pd(&dev->ib_dev, 0); 4895e126ba97SEli Cohen if (IS_ERR(pd)) { 4896e126ba97SEli Cohen mlx5_ib_dbg(dev, "Couldn't create PD for sync UMR QP\n"); 4897e126ba97SEli Cohen ret = PTR_ERR(pd); 4898e126ba97SEli Cohen goto error_0; 4899e126ba97SEli Cohen } 4900e126ba97SEli Cohen 4901add08d76SChristoph Hellwig cq = ib_alloc_cq(&dev->ib_dev, NULL, 128, 0, IB_POLL_SOFTIRQ); 4902e126ba97SEli Cohen if (IS_ERR(cq)) { 4903e126ba97SEli Cohen mlx5_ib_dbg(dev, "Couldn't create CQ for sync UMR QP\n"); 4904e126ba97SEli Cohen ret = PTR_ERR(cq); 4905e126ba97SEli Cohen goto error_2; 4906e126ba97SEli Cohen } 4907e126ba97SEli Cohen 4908e126ba97SEli Cohen init_attr->send_cq = cq; 4909e126ba97SEli Cohen init_attr->recv_cq = cq; 4910e126ba97SEli Cohen init_attr->sq_sig_type = IB_SIGNAL_ALL_WR; 4911e126ba97SEli Cohen init_attr->cap.max_send_wr = MAX_UMR_WR; 4912e126ba97SEli Cohen init_attr->cap.max_send_sge = 1; 4913e126ba97SEli Cohen init_attr->qp_type = MLX5_IB_QPT_REG_UMR; 4914e126ba97SEli Cohen init_attr->port_num = 1; 4915e126ba97SEli Cohen qp = mlx5_ib_create_qp(pd, init_attr, NULL); 4916e126ba97SEli Cohen if (IS_ERR(qp)) { 4917e126ba97SEli Cohen mlx5_ib_dbg(dev, "Couldn't create sync UMR QP\n"); 4918e126ba97SEli Cohen ret = PTR_ERR(qp); 4919e126ba97SEli Cohen goto error_3; 4920e126ba97SEli Cohen } 4921e126ba97SEli Cohen qp->device = &dev->ib_dev; 4922e126ba97SEli Cohen qp->real_qp = qp; 4923e126ba97SEli Cohen qp->uobject = NULL; 4924e126ba97SEli Cohen qp->qp_type = MLX5_IB_QPT_REG_UMR; 492531fde034SMajd Dibbiny qp->send_cq = init_attr->send_cq; 492631fde034SMajd Dibbiny qp->recv_cq = init_attr->recv_cq; 4927e126ba97SEli Cohen 4928e126ba97SEli Cohen attr->qp_state = IB_QPS_INIT; 4929e126ba97SEli Cohen attr->port_num = 1; 4930e126ba97SEli Cohen ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_PKEY_INDEX | 4931e126ba97SEli Cohen IB_QP_PORT, NULL); 4932e126ba97SEli Cohen if (ret) { 4933e126ba97SEli Cohen mlx5_ib_dbg(dev, "Couldn't modify UMR QP\n"); 4934e126ba97SEli Cohen goto error_4; 4935e126ba97SEli Cohen } 4936e126ba97SEli Cohen 4937e126ba97SEli Cohen memset(attr, 0, sizeof(*attr)); 4938e126ba97SEli Cohen attr->qp_state = IB_QPS_RTR; 4939e126ba97SEli Cohen attr->path_mtu = IB_MTU_256; 4940e126ba97SEli Cohen 4941e126ba97SEli Cohen ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL); 4942e126ba97SEli Cohen if (ret) { 4943e126ba97SEli Cohen mlx5_ib_dbg(dev, "Couldn't modify umr QP to rtr\n"); 4944e126ba97SEli Cohen goto error_4; 4945e126ba97SEli Cohen } 4946e126ba97SEli Cohen 4947e126ba97SEli Cohen memset(attr, 0, sizeof(*attr)); 4948e126ba97SEli Cohen attr->qp_state = IB_QPS_RTS; 4949e126ba97SEli Cohen ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL); 4950e126ba97SEli Cohen if (ret) { 4951e126ba97SEli Cohen mlx5_ib_dbg(dev, "Couldn't modify umr QP to rts\n"); 4952e126ba97SEli Cohen goto error_4; 4953e126ba97SEli Cohen } 4954e126ba97SEli Cohen 4955e126ba97SEli Cohen dev->umrc.qp = qp; 4956e126ba97SEli Cohen dev->umrc.cq = cq; 4957e126ba97SEli Cohen dev->umrc.pd = pd; 4958e126ba97SEli Cohen 4959e126ba97SEli Cohen sema_init(&dev->umrc.sem, MAX_UMR_WR); 4960e126ba97SEli Cohen ret = mlx5_mr_cache_init(dev); 4961e126ba97SEli Cohen if (ret) { 4962e126ba97SEli Cohen mlx5_ib_warn(dev, "mr cache init failed %d\n", ret); 4963e126ba97SEli Cohen goto error_4; 4964e126ba97SEli Cohen } 4965e126ba97SEli Cohen 4966e126ba97SEli Cohen kfree(attr); 4967e126ba97SEli Cohen kfree(init_attr); 4968e126ba97SEli Cohen 4969e126ba97SEli Cohen return 0; 4970e126ba97SEli Cohen 4971e126ba97SEli Cohen error_4: 4972c4367a26SShamir Rabinovitch mlx5_ib_destroy_qp(qp, NULL); 497332927e28SMark Bloch dev->umrc.qp = NULL; 4974e126ba97SEli Cohen 4975e126ba97SEli Cohen error_3: 4976add08d76SChristoph Hellwig ib_free_cq(cq); 497732927e28SMark Bloch dev->umrc.cq = NULL; 4978e126ba97SEli Cohen 4979e126ba97SEli Cohen error_2: 4980e126ba97SEli Cohen ib_dealloc_pd(pd); 498132927e28SMark Bloch dev->umrc.pd = NULL; 4982e126ba97SEli Cohen 4983e126ba97SEli Cohen error_0: 4984e126ba97SEli Cohen kfree(attr); 4985e126ba97SEli Cohen kfree(init_attr); 4986e126ba97SEli Cohen return ret; 4987e126ba97SEli Cohen } 4988e126ba97SEli Cohen 49896e8484c5SMax Gurtovoy static u8 mlx5_get_umr_fence(u8 umr_fence_cap) 49906e8484c5SMax Gurtovoy { 49916e8484c5SMax Gurtovoy switch (umr_fence_cap) { 49926e8484c5SMax Gurtovoy case MLX5_CAP_UMR_FENCE_NONE: 49936e8484c5SMax Gurtovoy return MLX5_FENCE_MODE_NONE; 49946e8484c5SMax Gurtovoy case MLX5_CAP_UMR_FENCE_SMALL: 49956e8484c5SMax Gurtovoy return MLX5_FENCE_MODE_INITIATOR_SMALL; 49966e8484c5SMax Gurtovoy default: 49976e8484c5SMax Gurtovoy return MLX5_FENCE_MODE_STRONG_ORDERING; 49986e8484c5SMax Gurtovoy } 49996e8484c5SMax Gurtovoy } 50006e8484c5SMax Gurtovoy 5001e126ba97SEli Cohen static int create_dev_resources(struct mlx5_ib_resources *devr) 5002e126ba97SEli Cohen { 5003e126ba97SEli Cohen struct ib_srq_init_attr attr; 5004e126ba97SEli Cohen struct mlx5_ib_dev *dev; 500521a428a0SLeon Romanovsky struct ib_device *ibdev; 5006bcf4c1eaSMatan Barak struct ib_cq_init_attr cq_attr = {.cqe = 1}; 50077722f47eSHaggai Eran int port; 5008e126ba97SEli Cohen int ret = 0; 5009e126ba97SEli Cohen 5010e126ba97SEli Cohen dev = container_of(devr, struct mlx5_ib_dev, devr); 501121a428a0SLeon Romanovsky ibdev = &dev->ib_dev; 5012e126ba97SEli Cohen 5013d16e91daSHaggai Eran mutex_init(&devr->mutex); 5014d16e91daSHaggai Eran 501521a428a0SLeon Romanovsky devr->p0 = rdma_zalloc_drv_obj(ibdev, ib_pd); 501621a428a0SLeon Romanovsky if (!devr->p0) 501721a428a0SLeon Romanovsky return -ENOMEM; 501821a428a0SLeon Romanovsky 501921a428a0SLeon Romanovsky devr->p0->device = ibdev; 5020e126ba97SEli Cohen devr->p0->uobject = NULL; 5021e126ba97SEli Cohen atomic_set(&devr->p0->usecnt, 0); 5022e126ba97SEli Cohen 5023ff23dfa1SShamir Rabinovitch ret = mlx5_ib_alloc_pd(devr->p0, NULL); 502421a428a0SLeon Romanovsky if (ret) 502521a428a0SLeon Romanovsky goto error0; 502621a428a0SLeon Romanovsky 5027e39afe3dSLeon Romanovsky devr->c0 = rdma_zalloc_drv_obj(ibdev, ib_cq); 5028e39afe3dSLeon Romanovsky if (!devr->c0) { 5029e39afe3dSLeon Romanovsky ret = -ENOMEM; 5030e126ba97SEli Cohen goto error1; 5031e126ba97SEli Cohen } 5032e39afe3dSLeon Romanovsky 5033e126ba97SEli Cohen devr->c0->device = &dev->ib_dev; 5034e126ba97SEli Cohen atomic_set(&devr->c0->usecnt, 0); 5035e126ba97SEli Cohen 5036e39afe3dSLeon Romanovsky ret = mlx5_ib_create_cq(devr->c0, &cq_attr, NULL); 5037e39afe3dSLeon Romanovsky if (ret) 5038e39afe3dSLeon Romanovsky goto err_create_cq; 5039e39afe3dSLeon Romanovsky 5040ff23dfa1SShamir Rabinovitch devr->x0 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL); 5041e126ba97SEli Cohen if (IS_ERR(devr->x0)) { 5042e126ba97SEli Cohen ret = PTR_ERR(devr->x0); 5043e126ba97SEli Cohen goto error2; 5044e126ba97SEli Cohen } 5045e126ba97SEli Cohen devr->x0->device = &dev->ib_dev; 5046e126ba97SEli Cohen devr->x0->inode = NULL; 5047e126ba97SEli Cohen atomic_set(&devr->x0->usecnt, 0); 5048e126ba97SEli Cohen mutex_init(&devr->x0->tgt_qp_mutex); 5049e126ba97SEli Cohen INIT_LIST_HEAD(&devr->x0->tgt_qp_list); 5050e126ba97SEli Cohen 5051ff23dfa1SShamir Rabinovitch devr->x1 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL); 5052e126ba97SEli Cohen if (IS_ERR(devr->x1)) { 5053e126ba97SEli Cohen ret = PTR_ERR(devr->x1); 5054e126ba97SEli Cohen goto error3; 5055e126ba97SEli Cohen } 5056e126ba97SEli Cohen devr->x1->device = &dev->ib_dev; 5057e126ba97SEli Cohen devr->x1->inode = NULL; 5058e126ba97SEli Cohen atomic_set(&devr->x1->usecnt, 0); 5059e126ba97SEli Cohen mutex_init(&devr->x1->tgt_qp_mutex); 5060e126ba97SEli Cohen INIT_LIST_HEAD(&devr->x1->tgt_qp_list); 5061e126ba97SEli Cohen 5062e126ba97SEli Cohen memset(&attr, 0, sizeof(attr)); 5063e126ba97SEli Cohen attr.attr.max_sge = 1; 5064e126ba97SEli Cohen attr.attr.max_wr = 1; 5065e126ba97SEli Cohen attr.srq_type = IB_SRQT_XRC; 50661a56ff6dSArtemy Kovalyov attr.ext.cq = devr->c0; 5067e126ba97SEli Cohen attr.ext.xrc.xrcd = devr->x0; 5068e126ba97SEli Cohen 506968e326deSLeon Romanovsky devr->s0 = rdma_zalloc_drv_obj(ibdev, ib_srq); 507068e326deSLeon Romanovsky if (!devr->s0) { 507168e326deSLeon Romanovsky ret = -ENOMEM; 5072e126ba97SEli Cohen goto error4; 5073e126ba97SEli Cohen } 507468e326deSLeon Romanovsky 5075e126ba97SEli Cohen devr->s0->device = &dev->ib_dev; 5076e126ba97SEli Cohen devr->s0->pd = devr->p0; 5077e126ba97SEli Cohen devr->s0->srq_type = IB_SRQT_XRC; 5078e126ba97SEli Cohen devr->s0->ext.xrc.xrcd = devr->x0; 50791a56ff6dSArtemy Kovalyov devr->s0->ext.cq = devr->c0; 508068e326deSLeon Romanovsky ret = mlx5_ib_create_srq(devr->s0, &attr, NULL); 508168e326deSLeon Romanovsky if (ret) 508268e326deSLeon Romanovsky goto err_create; 508368e326deSLeon Romanovsky 5084e126ba97SEli Cohen atomic_inc(&devr->s0->ext.xrc.xrcd->usecnt); 50851a56ff6dSArtemy Kovalyov atomic_inc(&devr->s0->ext.cq->usecnt); 5086e126ba97SEli Cohen atomic_inc(&devr->p0->usecnt); 5087e126ba97SEli Cohen atomic_set(&devr->s0->usecnt, 0); 5088e126ba97SEli Cohen 50894aa17b28SHaggai Abramonvsky memset(&attr, 0, sizeof(attr)); 50904aa17b28SHaggai Abramonvsky attr.attr.max_sge = 1; 50914aa17b28SHaggai Abramonvsky attr.attr.max_wr = 1; 50924aa17b28SHaggai Abramonvsky attr.srq_type = IB_SRQT_BASIC; 509368e326deSLeon Romanovsky devr->s1 = rdma_zalloc_drv_obj(ibdev, ib_srq); 509468e326deSLeon Romanovsky if (!devr->s1) { 509568e326deSLeon Romanovsky ret = -ENOMEM; 50964aa17b28SHaggai Abramonvsky goto error5; 50974aa17b28SHaggai Abramonvsky } 509868e326deSLeon Romanovsky 50994aa17b28SHaggai Abramonvsky devr->s1->device = &dev->ib_dev; 51004aa17b28SHaggai Abramonvsky devr->s1->pd = devr->p0; 51014aa17b28SHaggai Abramonvsky devr->s1->srq_type = IB_SRQT_BASIC; 51021a56ff6dSArtemy Kovalyov devr->s1->ext.cq = devr->c0; 510368e326deSLeon Romanovsky 510468e326deSLeon Romanovsky ret = mlx5_ib_create_srq(devr->s1, &attr, NULL); 510568e326deSLeon Romanovsky if (ret) 510668e326deSLeon Romanovsky goto error6; 510768e326deSLeon Romanovsky 51084aa17b28SHaggai Abramonvsky atomic_inc(&devr->p0->usecnt); 51091a56ff6dSArtemy Kovalyov atomic_set(&devr->s1->usecnt, 0); 51104aa17b28SHaggai Abramonvsky 51117722f47eSHaggai Eran for (port = 0; port < ARRAY_SIZE(devr->ports); ++port) { 51127722f47eSHaggai Eran INIT_WORK(&devr->ports[port].pkey_change_work, 51137722f47eSHaggai Eran pkey_change_handler); 51147722f47eSHaggai Eran devr->ports[port].devr = devr; 51157722f47eSHaggai Eran } 51167722f47eSHaggai Eran 5117e126ba97SEli Cohen return 0; 5118e126ba97SEli Cohen 511968e326deSLeon Romanovsky error6: 512068e326deSLeon Romanovsky kfree(devr->s1); 51214aa17b28SHaggai Abramonvsky error5: 5122c4367a26SShamir Rabinovitch mlx5_ib_destroy_srq(devr->s0, NULL); 512368e326deSLeon Romanovsky err_create: 512468e326deSLeon Romanovsky kfree(devr->s0); 5125e126ba97SEli Cohen error4: 5126c4367a26SShamir Rabinovitch mlx5_ib_dealloc_xrcd(devr->x1, NULL); 5127e126ba97SEli Cohen error3: 5128c4367a26SShamir Rabinovitch mlx5_ib_dealloc_xrcd(devr->x0, NULL); 5129e126ba97SEli Cohen error2: 5130c4367a26SShamir Rabinovitch mlx5_ib_destroy_cq(devr->c0, NULL); 5131e39afe3dSLeon Romanovsky err_create_cq: 5132e39afe3dSLeon Romanovsky kfree(devr->c0); 5133e126ba97SEli Cohen error1: 5134c4367a26SShamir Rabinovitch mlx5_ib_dealloc_pd(devr->p0, NULL); 5135e126ba97SEli Cohen error0: 513621a428a0SLeon Romanovsky kfree(devr->p0); 5137e126ba97SEli Cohen return ret; 5138e126ba97SEli Cohen } 5139e126ba97SEli Cohen 5140e126ba97SEli Cohen static void destroy_dev_resources(struct mlx5_ib_resources *devr) 5141e126ba97SEli Cohen { 51427722f47eSHaggai Eran int port; 51437722f47eSHaggai Eran 5144c4367a26SShamir Rabinovitch mlx5_ib_destroy_srq(devr->s1, NULL); 514568e326deSLeon Romanovsky kfree(devr->s1); 5146c4367a26SShamir Rabinovitch mlx5_ib_destroy_srq(devr->s0, NULL); 514768e326deSLeon Romanovsky kfree(devr->s0); 5148c4367a26SShamir Rabinovitch mlx5_ib_dealloc_xrcd(devr->x0, NULL); 5149c4367a26SShamir Rabinovitch mlx5_ib_dealloc_xrcd(devr->x1, NULL); 5150c4367a26SShamir Rabinovitch mlx5_ib_destroy_cq(devr->c0, NULL); 5151e39afe3dSLeon Romanovsky kfree(devr->c0); 5152c4367a26SShamir Rabinovitch mlx5_ib_dealloc_pd(devr->p0, NULL); 515321a428a0SLeon Romanovsky kfree(devr->p0); 51547722f47eSHaggai Eran 51557722f47eSHaggai Eran /* Make sure no change P_Key work items are still executing */ 51565d8f6a0eSMark Bloch for (port = 0; port < ARRAY_SIZE(devr->ports); ++port) 51577722f47eSHaggai Eran cancel_work_sync(&devr->ports[port].pkey_change_work); 5158e126ba97SEli Cohen } 5159e126ba97SEli Cohen 5160b02289b3SArtemy Kovalyov static u32 get_core_cap_flags(struct ib_device *ibdev, 5161b02289b3SArtemy Kovalyov struct mlx5_hca_vport_context *rep) 5162e53505a8SAchiad Shochat { 5163e53505a8SAchiad Shochat struct mlx5_ib_dev *dev = to_mdev(ibdev); 5164e53505a8SAchiad Shochat enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, 1); 5165e53505a8SAchiad Shochat u8 l3_type_cap = MLX5_CAP_ROCE(dev->mdev, l3_type); 5166e53505a8SAchiad Shochat u8 roce_version_cap = MLX5_CAP_ROCE(dev->mdev, roce_version); 516785c7c014SDaniel Jurgens bool raw_support = !mlx5_core_mp_enabled(dev->mdev); 5168e53505a8SAchiad Shochat u32 ret = 0; 5169e53505a8SAchiad Shochat 5170b02289b3SArtemy Kovalyov if (rep->grh_required) 5171b02289b3SArtemy Kovalyov ret |= RDMA_CORE_CAP_IB_GRH_REQUIRED; 5172b02289b3SArtemy Kovalyov 5173e53505a8SAchiad Shochat if (ll == IB_LINK_LAYER_INFINIBAND) 5174b02289b3SArtemy Kovalyov return ret | RDMA_CORE_PORT_IBA_IB; 5175e53505a8SAchiad Shochat 517685c7c014SDaniel Jurgens if (raw_support) 5177b02289b3SArtemy Kovalyov ret |= RDMA_CORE_PORT_RAW_PACKET; 517872cd5717SOr Gerlitz 5179e53505a8SAchiad Shochat if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV4_CAP)) 518072cd5717SOr Gerlitz return ret; 5181e53505a8SAchiad Shochat 5182e53505a8SAchiad Shochat if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV6_CAP)) 518372cd5717SOr Gerlitz return ret; 5184e53505a8SAchiad Shochat 5185e53505a8SAchiad Shochat if (roce_version_cap & MLX5_ROCE_VERSION_1_CAP) 5186e53505a8SAchiad Shochat ret |= RDMA_CORE_PORT_IBA_ROCE; 5187e53505a8SAchiad Shochat 5188e53505a8SAchiad Shochat if (roce_version_cap & MLX5_ROCE_VERSION_2_CAP) 5189e53505a8SAchiad Shochat ret |= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP; 5190e53505a8SAchiad Shochat 5191e53505a8SAchiad Shochat return ret; 5192e53505a8SAchiad Shochat } 5193e53505a8SAchiad Shochat 51947738613eSIra Weiny static int mlx5_port_immutable(struct ib_device *ibdev, u8 port_num, 51957738613eSIra Weiny struct ib_port_immutable *immutable) 51967738613eSIra Weiny { 51977738613eSIra Weiny struct ib_port_attr attr; 5198ca5b91d6SOr Gerlitz struct mlx5_ib_dev *dev = to_mdev(ibdev); 5199ca5b91d6SOr Gerlitz enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, port_num); 5200b02289b3SArtemy Kovalyov struct mlx5_hca_vport_context rep = {0}; 52017738613eSIra Weiny int err; 52027738613eSIra Weiny 5203c4550c63SOr Gerlitz err = ib_query_port(ibdev, port_num, &attr); 52047738613eSIra Weiny if (err) 52057738613eSIra Weiny return err; 52067738613eSIra Weiny 5207b02289b3SArtemy Kovalyov if (ll == IB_LINK_LAYER_INFINIBAND) { 5208b02289b3SArtemy Kovalyov err = mlx5_query_hca_vport_context(dev->mdev, 0, port_num, 0, 5209b02289b3SArtemy Kovalyov &rep); 5210b02289b3SArtemy Kovalyov if (err) 5211b02289b3SArtemy Kovalyov return err; 5212b02289b3SArtemy Kovalyov } 5213b02289b3SArtemy Kovalyov 52147738613eSIra Weiny immutable->pkey_tbl_len = attr.pkey_tbl_len; 52157738613eSIra Weiny immutable->gid_tbl_len = attr.gid_tbl_len; 5216b02289b3SArtemy Kovalyov immutable->core_cap_flags = get_core_cap_flags(ibdev, &rep); 5217337877a4SIra Weiny immutable->max_mad_size = IB_MGMT_MAD_SIZE; 52187738613eSIra Weiny 52197738613eSIra Weiny return 0; 52207738613eSIra Weiny } 52217738613eSIra Weiny 52228e6efa3aSMark Bloch static int mlx5_port_rep_immutable(struct ib_device *ibdev, u8 port_num, 52238e6efa3aSMark Bloch struct ib_port_immutable *immutable) 52248e6efa3aSMark Bloch { 52258e6efa3aSMark Bloch struct ib_port_attr attr; 52268e6efa3aSMark Bloch int err; 52278e6efa3aSMark Bloch 52288e6efa3aSMark Bloch immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET; 52298e6efa3aSMark Bloch 52308e6efa3aSMark Bloch err = ib_query_port(ibdev, port_num, &attr); 52318e6efa3aSMark Bloch if (err) 52328e6efa3aSMark Bloch return err; 52338e6efa3aSMark Bloch 52348e6efa3aSMark Bloch immutable->pkey_tbl_len = attr.pkey_tbl_len; 52358e6efa3aSMark Bloch immutable->gid_tbl_len = attr.gid_tbl_len; 52368e6efa3aSMark Bloch immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET; 52378e6efa3aSMark Bloch 52388e6efa3aSMark Bloch return 0; 52398e6efa3aSMark Bloch } 52408e6efa3aSMark Bloch 52419abb0d1bSLeon Romanovsky static void get_dev_fw_str(struct ib_device *ibdev, char *str) 5242c7342823SIra Weiny { 5243c7342823SIra Weiny struct mlx5_ib_dev *dev = 5244c7342823SIra Weiny container_of(ibdev, struct mlx5_ib_dev, ib_dev); 52459abb0d1bSLeon Romanovsky snprintf(str, IB_FW_VERSION_NAME_MAX, "%d.%d.%04d", 52469abb0d1bSLeon Romanovsky fw_rev_maj(dev->mdev), fw_rev_min(dev->mdev), 52479abb0d1bSLeon Romanovsky fw_rev_sub(dev->mdev)); 5248c7342823SIra Weiny } 5249c7342823SIra Weiny 525045f95acdSOr Gerlitz static int mlx5_eth_lag_init(struct mlx5_ib_dev *dev) 52519ef9c640SAviv Heller { 52529ef9c640SAviv Heller struct mlx5_core_dev *mdev = dev->mdev; 52539ef9c640SAviv Heller struct mlx5_flow_namespace *ns = mlx5_get_flow_namespace(mdev, 52549ef9c640SAviv Heller MLX5_FLOW_NAMESPACE_LAG); 52559ef9c640SAviv Heller struct mlx5_flow_table *ft; 52569ef9c640SAviv Heller int err; 52579ef9c640SAviv Heller 52587c34ec19SAviv Heller if (!ns || !mlx5_lag_is_roce(mdev)) 52599ef9c640SAviv Heller return 0; 52609ef9c640SAviv Heller 52619ef9c640SAviv Heller err = mlx5_cmd_create_vport_lag(mdev); 52629ef9c640SAviv Heller if (err) 52639ef9c640SAviv Heller return err; 52649ef9c640SAviv Heller 52659ef9c640SAviv Heller ft = mlx5_create_lag_demux_flow_table(ns, 0, 0); 52669ef9c640SAviv Heller if (IS_ERR(ft)) { 52679ef9c640SAviv Heller err = PTR_ERR(ft); 52689ef9c640SAviv Heller goto err_destroy_vport_lag; 52699ef9c640SAviv Heller } 52709ef9c640SAviv Heller 52719a4ca38dSMark Bloch dev->flow_db->lag_demux_ft = ft; 52727c34ec19SAviv Heller dev->lag_active = true; 52739ef9c640SAviv Heller return 0; 52749ef9c640SAviv Heller 52759ef9c640SAviv Heller err_destroy_vport_lag: 52769ef9c640SAviv Heller mlx5_cmd_destroy_vport_lag(mdev); 52779ef9c640SAviv Heller return err; 52789ef9c640SAviv Heller } 52799ef9c640SAviv Heller 528045f95acdSOr Gerlitz static void mlx5_eth_lag_cleanup(struct mlx5_ib_dev *dev) 52819ef9c640SAviv Heller { 52829ef9c640SAviv Heller struct mlx5_core_dev *mdev = dev->mdev; 52839ef9c640SAviv Heller 52847c34ec19SAviv Heller if (dev->lag_active) { 52857c34ec19SAviv Heller dev->lag_active = false; 52867c34ec19SAviv Heller 52879a4ca38dSMark Bloch mlx5_destroy_flow_table(dev->flow_db->lag_demux_ft); 52889a4ca38dSMark Bloch dev->flow_db->lag_demux_ft = NULL; 52899ef9c640SAviv Heller 52909ef9c640SAviv Heller mlx5_cmd_destroy_vport_lag(mdev); 52919ef9c640SAviv Heller } 52929ef9c640SAviv Heller } 52939ef9c640SAviv Heller 52947fd8aefbSDaniel Jurgens static int mlx5_add_netdev_notifier(struct mlx5_ib_dev *dev, u8 port_num) 5295fc24fc5eSAchiad Shochat { 5296e53505a8SAchiad Shochat int err; 5297e53505a8SAchiad Shochat 529895579e78SMark Bloch dev->port[port_num].roce.nb.notifier_call = mlx5_netdev_event; 529995579e78SMark Bloch err = register_netdevice_notifier(&dev->port[port_num].roce.nb); 53005ec8c83eSAviv Heller if (err) { 530195579e78SMark Bloch dev->port[port_num].roce.nb.notifier_call = NULL; 5302e53505a8SAchiad Shochat return err; 53035ec8c83eSAviv Heller } 5304e53505a8SAchiad Shochat 5305d012f5d6SOr Gerlitz return 0; 5306d012f5d6SOr Gerlitz } 5307d012f5d6SOr Gerlitz 53087fd8aefbSDaniel Jurgens static void mlx5_remove_netdev_notifier(struct mlx5_ib_dev *dev, u8 port_num) 5309e126ba97SEli Cohen { 531095579e78SMark Bloch if (dev->port[port_num].roce.nb.notifier_call) { 531195579e78SMark Bloch unregister_netdevice_notifier(&dev->port[port_num].roce.nb); 531295579e78SMark Bloch dev->port[port_num].roce.nb.notifier_call = NULL; 5313e126ba97SEli Cohen } 5314e126ba97SEli Cohen } 5315e126ba97SEli Cohen 5316e3f1ed1fSLeon Romanovsky static int mlx5_enable_eth(struct mlx5_ib_dev *dev) 5317e126ba97SEli Cohen { 5318e126ba97SEli Cohen int err; 5319e126ba97SEli Cohen 5320e53505a8SAchiad Shochat err = mlx5_nic_vport_enable_roce(dev->mdev); 5321e53505a8SAchiad Shochat if (err) 53228e6efa3aSMark Bloch return err; 5323e53505a8SAchiad Shochat 532445f95acdSOr Gerlitz err = mlx5_eth_lag_init(dev); 53259ef9c640SAviv Heller if (err) 53269ef9c640SAviv Heller goto err_disable_roce; 53279ef9c640SAviv Heller 5328e53505a8SAchiad Shochat return 0; 5329e53505a8SAchiad Shochat 53309ef9c640SAviv Heller err_disable_roce: 53319ef9c640SAviv Heller mlx5_nic_vport_disable_roce(dev->mdev); 53329ef9c640SAviv Heller 5333e53505a8SAchiad Shochat return err; 5334fc24fc5eSAchiad Shochat } 5335fc24fc5eSAchiad Shochat 533645f95acdSOr Gerlitz static void mlx5_disable_eth(struct mlx5_ib_dev *dev) 5337fc24fc5eSAchiad Shochat { 533845f95acdSOr Gerlitz mlx5_eth_lag_cleanup(dev); 5339e53505a8SAchiad Shochat mlx5_nic_vport_disable_roce(dev->mdev); 5340fc24fc5eSAchiad Shochat } 5341fc24fc5eSAchiad Shochat 5342e1f24a79SParav Pandit struct mlx5_ib_counter { 53437c16f477SKamal Heib const char *name; 53447c16f477SKamal Heib size_t offset; 53457c16f477SKamal Heib }; 53467c16f477SKamal Heib 53477c16f477SKamal Heib #define INIT_Q_COUNTER(_name) \ 53487c16f477SKamal Heib { .name = #_name, .offset = MLX5_BYTE_OFF(query_q_counter_out, _name)} 53497c16f477SKamal Heib 5350e1f24a79SParav Pandit static const struct mlx5_ib_counter basic_q_cnts[] = { 53517c16f477SKamal Heib INIT_Q_COUNTER(rx_write_requests), 53527c16f477SKamal Heib INIT_Q_COUNTER(rx_read_requests), 53537c16f477SKamal Heib INIT_Q_COUNTER(rx_atomic_requests), 53547c16f477SKamal Heib INIT_Q_COUNTER(out_of_buffer), 53557c16f477SKamal Heib }; 53567c16f477SKamal Heib 5357e1f24a79SParav Pandit static const struct mlx5_ib_counter out_of_seq_q_cnts[] = { 53587c16f477SKamal Heib INIT_Q_COUNTER(out_of_sequence), 53597c16f477SKamal Heib }; 53607c16f477SKamal Heib 5361e1f24a79SParav Pandit static const struct mlx5_ib_counter retrans_q_cnts[] = { 53627c16f477SKamal Heib INIT_Q_COUNTER(duplicate_request), 53637c16f477SKamal Heib INIT_Q_COUNTER(rnr_nak_retry_err), 53647c16f477SKamal Heib INIT_Q_COUNTER(packet_seq_err), 53657c16f477SKamal Heib INIT_Q_COUNTER(implied_nak_seq_err), 53667c16f477SKamal Heib INIT_Q_COUNTER(local_ack_timeout_err), 53677c16f477SKamal Heib }; 53687c16f477SKamal Heib 5369e1f24a79SParav Pandit #define INIT_CONG_COUNTER(_name) \ 5370e1f24a79SParav Pandit { .name = #_name, .offset = \ 5371e1f24a79SParav Pandit MLX5_BYTE_OFF(query_cong_statistics_out, _name ## _high)} 5372e1f24a79SParav Pandit 5373e1f24a79SParav Pandit static const struct mlx5_ib_counter cong_cnts[] = { 5374e1f24a79SParav Pandit INIT_CONG_COUNTER(rp_cnp_ignored), 5375e1f24a79SParav Pandit INIT_CONG_COUNTER(rp_cnp_handled), 5376e1f24a79SParav Pandit INIT_CONG_COUNTER(np_ecn_marked_roce_packets), 5377e1f24a79SParav Pandit INIT_CONG_COUNTER(np_cnp_sent), 5378e1f24a79SParav Pandit }; 5379e1f24a79SParav Pandit 538058dcb60aSParav Pandit static const struct mlx5_ib_counter extended_err_cnts[] = { 538158dcb60aSParav Pandit INIT_Q_COUNTER(resp_local_length_error), 538258dcb60aSParav Pandit INIT_Q_COUNTER(resp_cqe_error), 538358dcb60aSParav Pandit INIT_Q_COUNTER(req_cqe_error), 538458dcb60aSParav Pandit INIT_Q_COUNTER(req_remote_invalid_request), 538558dcb60aSParav Pandit INIT_Q_COUNTER(req_remote_access_errors), 538658dcb60aSParav Pandit INIT_Q_COUNTER(resp_remote_access_errors), 538758dcb60aSParav Pandit INIT_Q_COUNTER(resp_cqe_flush_error), 538858dcb60aSParav Pandit INIT_Q_COUNTER(req_cqe_flush_error), 538958dcb60aSParav Pandit }; 539058dcb60aSParav Pandit 5391d7fab916SAvihai Horon static const struct mlx5_ib_counter roce_accl_cnts[] = { 5392d7fab916SAvihai Horon INIT_Q_COUNTER(roce_adp_retrans), 5393d7fab916SAvihai Horon INIT_Q_COUNTER(roce_adp_retrans_to), 5394d7fab916SAvihai Horon INIT_Q_COUNTER(roce_slow_restart), 5395d7fab916SAvihai Horon INIT_Q_COUNTER(roce_slow_restart_cnps), 5396d7fab916SAvihai Horon INIT_Q_COUNTER(roce_slow_restart_trans), 5397d7fab916SAvihai Horon }; 5398d7fab916SAvihai Horon 53999f876f3dSTalat Batheesh #define INIT_EXT_PPCNT_COUNTER(_name) \ 54009f876f3dSTalat Batheesh { .name = #_name, .offset = \ 54019f876f3dSTalat Batheesh MLX5_BYTE_OFF(ppcnt_reg, \ 54029f876f3dSTalat Batheesh counter_set.eth_extended_cntrs_grp_data_layout._name##_high)} 54039f876f3dSTalat Batheesh 54049f876f3dSTalat Batheesh static const struct mlx5_ib_counter ext_ppcnt_cnts[] = { 54059f876f3dSTalat Batheesh INIT_EXT_PPCNT_COUNTER(rx_icrc_encapsulated), 54069f876f3dSTalat Batheesh }; 54079f876f3dSTalat Batheesh 54083e1f000fSParav Pandit static bool is_mdev_switchdev_mode(const struct mlx5_core_dev *mdev) 54093e1f000fSParav Pandit { 54103e1f000fSParav Pandit return MLX5_ESWITCH_MANAGER(mdev) && 54113e1f000fSParav Pandit mlx5_ib_eswitch_mode(mdev->priv.eswitch) == 54123e1f000fSParav Pandit MLX5_ESWITCH_OFFLOADS; 54133e1f000fSParav Pandit } 54143e1f000fSParav Pandit 5415e1f24a79SParav Pandit static void mlx5_ib_dealloc_counters(struct mlx5_ib_dev *dev) 54160837e86aSMark Bloch { 54173e1f000fSParav Pandit int num_cnt_ports; 5418aac4492eSDaniel Jurgens int i; 54190837e86aSMark Bloch 54203e1f000fSParav Pandit num_cnt_ports = is_mdev_switchdev_mode(dev->mdev) ? 1 : dev->num_ports; 54213e1f000fSParav Pandit 54223e1f000fSParav Pandit for (i = 0; i < num_cnt_ports; i++) { 5423921c0f5bSParav Pandit if (dev->port[i].cnts.set_id_valid) 54240837e86aSMark Bloch mlx5_core_dealloc_q_counter(dev->mdev, 5425e1f24a79SParav Pandit dev->port[i].cnts.set_id); 5426e1f24a79SParav Pandit kfree(dev->port[i].cnts.names); 5427e1f24a79SParav Pandit kfree(dev->port[i].cnts.offsets); 54287c16f477SKamal Heib } 54297c16f477SKamal Heib } 54307c16f477SKamal Heib 5431e1f24a79SParav Pandit static int __mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev, 5432e1f24a79SParav Pandit struct mlx5_ib_counters *cnts) 54337c16f477SKamal Heib { 54347c16f477SKamal Heib u32 num_counters; 54357c16f477SKamal Heib 54367c16f477SKamal Heib num_counters = ARRAY_SIZE(basic_q_cnts); 54377c16f477SKamal Heib 54387c16f477SKamal Heib if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt)) 54397c16f477SKamal Heib num_counters += ARRAY_SIZE(out_of_seq_q_cnts); 54407c16f477SKamal Heib 54417c16f477SKamal Heib if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters)) 54427c16f477SKamal Heib num_counters += ARRAY_SIZE(retrans_q_cnts); 544358dcb60aSParav Pandit 544458dcb60aSParav Pandit if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters)) 544558dcb60aSParav Pandit num_counters += ARRAY_SIZE(extended_err_cnts); 544658dcb60aSParav Pandit 5447d7fab916SAvihai Horon if (MLX5_CAP_GEN(dev->mdev, roce_accl)) 5448d7fab916SAvihai Horon num_counters += ARRAY_SIZE(roce_accl_cnts); 5449d7fab916SAvihai Horon 5450e1f24a79SParav Pandit cnts->num_q_counters = num_counters; 54517c16f477SKamal Heib 5452e1f24a79SParav Pandit if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) { 5453e1f24a79SParav Pandit cnts->num_cong_counters = ARRAY_SIZE(cong_cnts); 5454e1f24a79SParav Pandit num_counters += ARRAY_SIZE(cong_cnts); 5455e1f24a79SParav Pandit } 54569f876f3dSTalat Batheesh if (MLX5_CAP_PCAM_FEATURE(dev->mdev, rx_icrc_encapsulated_counter)) { 54579f876f3dSTalat Batheesh cnts->num_ext_ppcnt_counters = ARRAY_SIZE(ext_ppcnt_cnts); 54589f876f3dSTalat Batheesh num_counters += ARRAY_SIZE(ext_ppcnt_cnts); 54599f876f3dSTalat Batheesh } 5460e1f24a79SParav Pandit cnts->names = kcalloc(num_counters, sizeof(cnts->names), GFP_KERNEL); 5461e1f24a79SParav Pandit if (!cnts->names) 54627c16f477SKamal Heib return -ENOMEM; 54637c16f477SKamal Heib 5464e1f24a79SParav Pandit cnts->offsets = kcalloc(num_counters, 5465e1f24a79SParav Pandit sizeof(cnts->offsets), GFP_KERNEL); 5466e1f24a79SParav Pandit if (!cnts->offsets) 54677c16f477SKamal Heib goto err_names; 54687c16f477SKamal Heib 54697c16f477SKamal Heib return 0; 54707c16f477SKamal Heib 54717c16f477SKamal Heib err_names: 5472e1f24a79SParav Pandit kfree(cnts->names); 5473aac4492eSDaniel Jurgens cnts->names = NULL; 54747c16f477SKamal Heib return -ENOMEM; 54757c16f477SKamal Heib } 54767c16f477SKamal Heib 5477e1f24a79SParav Pandit static void mlx5_ib_fill_counters(struct mlx5_ib_dev *dev, 54787c16f477SKamal Heib const char **names, 54797c16f477SKamal Heib size_t *offsets) 54807c16f477SKamal Heib { 54817c16f477SKamal Heib int i; 54827c16f477SKamal Heib int j = 0; 54837c16f477SKamal Heib 54847c16f477SKamal Heib for (i = 0; i < ARRAY_SIZE(basic_q_cnts); i++, j++) { 54857c16f477SKamal Heib names[j] = basic_q_cnts[i].name; 54867c16f477SKamal Heib offsets[j] = basic_q_cnts[i].offset; 54877c16f477SKamal Heib } 54887c16f477SKamal Heib 54897c16f477SKamal Heib if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt)) { 54907c16f477SKamal Heib for (i = 0; i < ARRAY_SIZE(out_of_seq_q_cnts); i++, j++) { 54917c16f477SKamal Heib names[j] = out_of_seq_q_cnts[i].name; 54927c16f477SKamal Heib offsets[j] = out_of_seq_q_cnts[i].offset; 54937c16f477SKamal Heib } 54947c16f477SKamal Heib } 54957c16f477SKamal Heib 54967c16f477SKamal Heib if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters)) { 54977c16f477SKamal Heib for (i = 0; i < ARRAY_SIZE(retrans_q_cnts); i++, j++) { 54987c16f477SKamal Heib names[j] = retrans_q_cnts[i].name; 54997c16f477SKamal Heib offsets[j] = retrans_q_cnts[i].offset; 55007c16f477SKamal Heib } 55017c16f477SKamal Heib } 5502e1f24a79SParav Pandit 550358dcb60aSParav Pandit if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters)) { 550458dcb60aSParav Pandit for (i = 0; i < ARRAY_SIZE(extended_err_cnts); i++, j++) { 550558dcb60aSParav Pandit names[j] = extended_err_cnts[i].name; 550658dcb60aSParav Pandit offsets[j] = extended_err_cnts[i].offset; 550758dcb60aSParav Pandit } 550858dcb60aSParav Pandit } 550958dcb60aSParav Pandit 5510d7fab916SAvihai Horon if (MLX5_CAP_GEN(dev->mdev, roce_accl)) { 5511d7fab916SAvihai Horon for (i = 0; i < ARRAY_SIZE(roce_accl_cnts); i++, j++) { 5512d7fab916SAvihai Horon names[j] = roce_accl_cnts[i].name; 5513d7fab916SAvihai Horon offsets[j] = roce_accl_cnts[i].offset; 5514d7fab916SAvihai Horon } 5515d7fab916SAvihai Horon } 5516d7fab916SAvihai Horon 5517e1f24a79SParav Pandit if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) { 5518e1f24a79SParav Pandit for (i = 0; i < ARRAY_SIZE(cong_cnts); i++, j++) { 5519e1f24a79SParav Pandit names[j] = cong_cnts[i].name; 5520e1f24a79SParav Pandit offsets[j] = cong_cnts[i].offset; 5521e1f24a79SParav Pandit } 5522e1f24a79SParav Pandit } 55239f876f3dSTalat Batheesh 55249f876f3dSTalat Batheesh if (MLX5_CAP_PCAM_FEATURE(dev->mdev, rx_icrc_encapsulated_counter)) { 55259f876f3dSTalat Batheesh for (i = 0; i < ARRAY_SIZE(ext_ppcnt_cnts); i++, j++) { 55269f876f3dSTalat Batheesh names[j] = ext_ppcnt_cnts[i].name; 55279f876f3dSTalat Batheesh offsets[j] = ext_ppcnt_cnts[i].offset; 55289f876f3dSTalat Batheesh } 55299f876f3dSTalat Batheesh } 55300837e86aSMark Bloch } 55310837e86aSMark Bloch 5532e1f24a79SParav Pandit static int mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev) 55330837e86aSMark Bloch { 55343e1f000fSParav Pandit int num_cnt_ports; 5535aac4492eSDaniel Jurgens int err = 0; 55360837e86aSMark Bloch int i; 5537aa74be6eSYishai Hadas bool is_shared; 5538aa74be6eSYishai Hadas 5539aa74be6eSYishai Hadas is_shared = MLX5_CAP_GEN(dev->mdev, log_max_uctx) != 0; 55403e1f000fSParav Pandit num_cnt_ports = is_mdev_switchdev_mode(dev->mdev) ? 1 : dev->num_ports; 55410837e86aSMark Bloch 55423e1f000fSParav Pandit for (i = 0; i < num_cnt_ports; i++) { 5543aac4492eSDaniel Jurgens err = __mlx5_ib_alloc_counters(dev, &dev->port[i].cnts); 5544aac4492eSDaniel Jurgens if (err) 5545aac4492eSDaniel Jurgens goto err_alloc; 55467c16f477SKamal Heib 5547aac4492eSDaniel Jurgens mlx5_ib_fill_counters(dev, dev->port[i].cnts.names, 5548aac4492eSDaniel Jurgens dev->port[i].cnts.offsets); 5549aac4492eSDaniel Jurgens 5550aa74be6eSYishai Hadas err = mlx5_cmd_alloc_q_counter(dev->mdev, 5551aa74be6eSYishai Hadas &dev->port[i].cnts.set_id, 5552aa74be6eSYishai Hadas is_shared ? 5553aa74be6eSYishai Hadas MLX5_SHARED_RESOURCE_UID : 0); 5554aac4492eSDaniel Jurgens if (err) { 55550837e86aSMark Bloch mlx5_ib_warn(dev, 55560837e86aSMark Bloch "couldn't allocate queue counter for port %d, err %d\n", 5557aac4492eSDaniel Jurgens i + 1, err); 5558aac4492eSDaniel Jurgens goto err_alloc; 55590837e86aSMark Bloch } 5560aac4492eSDaniel Jurgens dev->port[i].cnts.set_id_valid = true; 55610837e86aSMark Bloch } 55620837e86aSMark Bloch return 0; 55630837e86aSMark Bloch 5564aac4492eSDaniel Jurgens err_alloc: 5565aac4492eSDaniel Jurgens mlx5_ib_dealloc_counters(dev); 5566aac4492eSDaniel Jurgens return err; 55670837e86aSMark Bloch } 55680837e86aSMark Bloch 55693e1f000fSParav Pandit static const struct mlx5_ib_counters *get_counters(struct mlx5_ib_dev *dev, 55703e1f000fSParav Pandit u8 port_num) 55713e1f000fSParav Pandit { 55723e1f000fSParav Pandit return is_mdev_switchdev_mode(dev->mdev) ? &dev->port[0].cnts : 55733e1f000fSParav Pandit &dev->port[port_num].cnts; 55743e1f000fSParav Pandit } 55753e1f000fSParav Pandit 55763e1f000fSParav Pandit /** 55773e1f000fSParav Pandit * mlx5_ib_get_counters_id - Returns counters id to use for device+port 55783e1f000fSParav Pandit * @dev: Pointer to mlx5 IB device 55793e1f000fSParav Pandit * @port_num: Zero based port number 55803e1f000fSParav Pandit * 55813e1f000fSParav Pandit * mlx5_ib_get_counters_id() Returns counters set id to use for given 55823e1f000fSParav Pandit * device port combination in switchdev and non switchdev mode of the 55833e1f000fSParav Pandit * parent device. 55843e1f000fSParav Pandit */ 55853e1f000fSParav Pandit u16 mlx5_ib_get_counters_id(struct mlx5_ib_dev *dev, u8 port_num) 55863e1f000fSParav Pandit { 55873e1f000fSParav Pandit const struct mlx5_ib_counters *cnts = get_counters(dev, port_num); 55883e1f000fSParav Pandit 55893e1f000fSParav Pandit return cnts->set_id; 55903e1f000fSParav Pandit } 55913e1f000fSParav Pandit 55920ad17a8fSMark Bloch static struct rdma_hw_stats *mlx5_ib_alloc_hw_stats(struct ib_device *ibdev, 55930ad17a8fSMark Bloch u8 port_num) 55940ad17a8fSMark Bloch { 55957c16f477SKamal Heib struct mlx5_ib_dev *dev = to_mdev(ibdev); 55963e1f000fSParav Pandit const struct mlx5_ib_counters *cnts; 55973e1f000fSParav Pandit bool is_switchdev = is_mdev_switchdev_mode(dev->mdev); 55980ad17a8fSMark Bloch 55993e1f000fSParav Pandit if ((is_switchdev && port_num) || (!is_switchdev && !port_num)) 56000ad17a8fSMark Bloch return NULL; 56010ad17a8fSMark Bloch 56023e1f000fSParav Pandit cnts = get_counters(dev, port_num - 1); 56033e1f000fSParav Pandit 56045dcecbc9SParav Pandit return rdma_alloc_hw_stats_struct(cnts->names, 56055dcecbc9SParav Pandit cnts->num_q_counters + 56065dcecbc9SParav Pandit cnts->num_cong_counters + 56075dcecbc9SParav Pandit cnts->num_ext_ppcnt_counters, 56080ad17a8fSMark Bloch RDMA_HW_STATS_DEFAULT_LIFESPAN); 56090ad17a8fSMark Bloch } 56100ad17a8fSMark Bloch 5611aac4492eSDaniel Jurgens static int mlx5_ib_query_q_counters(struct mlx5_core_dev *mdev, 56125dcecbc9SParav Pandit const struct mlx5_ib_counters *cnts, 5613318d535cSMark Zhang struct rdma_hw_stats *stats, 5614318d535cSMark Zhang u16 set_id) 5615e1f24a79SParav Pandit { 5616e1f24a79SParav Pandit int outlen = MLX5_ST_SZ_BYTES(query_q_counter_out); 5617e1f24a79SParav Pandit void *out; 5618e1f24a79SParav Pandit __be32 val; 5619e1f24a79SParav Pandit int ret, i; 5620e1f24a79SParav Pandit 56211b9a07eeSLeon Romanovsky out = kvzalloc(outlen, GFP_KERNEL); 5622e1f24a79SParav Pandit if (!out) 5623e1f24a79SParav Pandit return -ENOMEM; 5624e1f24a79SParav Pandit 5625318d535cSMark Zhang ret = mlx5_core_query_q_counter(mdev, set_id, 0, out, outlen); 5626e1f24a79SParav Pandit if (ret) 5627e1f24a79SParav Pandit goto free; 5628e1f24a79SParav Pandit 56295dcecbc9SParav Pandit for (i = 0; i < cnts->num_q_counters; i++) { 56305dcecbc9SParav Pandit val = *(__be32 *)(out + cnts->offsets[i]); 5631e1f24a79SParav Pandit stats->value[i] = (u64)be32_to_cpu(val); 5632e1f24a79SParav Pandit } 5633e1f24a79SParav Pandit 5634e1f24a79SParav Pandit free: 5635e1f24a79SParav Pandit kvfree(out); 5636e1f24a79SParav Pandit return ret; 5637e1f24a79SParav Pandit } 5638e1f24a79SParav Pandit 56399f876f3dSTalat Batheesh static int mlx5_ib_query_ext_ppcnt_counters(struct mlx5_ib_dev *dev, 56405dcecbc9SParav Pandit const struct mlx5_ib_counters *cnts, 56419f876f3dSTalat Batheesh struct rdma_hw_stats *stats) 56429f876f3dSTalat Batheesh { 56435dcecbc9SParav Pandit int offset = cnts->num_q_counters + cnts->num_cong_counters; 56449f876f3dSTalat Batheesh int sz = MLX5_ST_SZ_BYTES(ppcnt_reg); 56459f876f3dSTalat Batheesh int ret, i; 56469f876f3dSTalat Batheesh void *out; 56479f876f3dSTalat Batheesh 56489f876f3dSTalat Batheesh out = kvzalloc(sz, GFP_KERNEL); 56499f876f3dSTalat Batheesh if (!out) 56509f876f3dSTalat Batheesh return -ENOMEM; 56519f876f3dSTalat Batheesh 56529f876f3dSTalat Batheesh ret = mlx5_cmd_query_ext_ppcnt_counters(dev->mdev, out); 56539f876f3dSTalat Batheesh if (ret) 56549f876f3dSTalat Batheesh goto free; 56559f876f3dSTalat Batheesh 56565dcecbc9SParav Pandit for (i = 0; i < cnts->num_ext_ppcnt_counters; i++) 56579f876f3dSTalat Batheesh stats->value[i + offset] = 56589f876f3dSTalat Batheesh be64_to_cpup((__be64 *)(out + 56595dcecbc9SParav Pandit cnts->offsets[i + offset])); 56609f876f3dSTalat Batheesh free: 56619f876f3dSTalat Batheesh kvfree(out); 56629f876f3dSTalat Batheesh return ret; 56639f876f3dSTalat Batheesh } 56649f876f3dSTalat Batheesh 56650ad17a8fSMark Bloch static int mlx5_ib_get_hw_stats(struct ib_device *ibdev, 56660ad17a8fSMark Bloch struct rdma_hw_stats *stats, 56677c16f477SKamal Heib u8 port_num, int index) 56680ad17a8fSMark Bloch { 56690ad17a8fSMark Bloch struct mlx5_ib_dev *dev = to_mdev(ibdev); 56703e1f000fSParav Pandit const struct mlx5_ib_counters *cnts = get_counters(dev, port_num - 1); 5671aac4492eSDaniel Jurgens struct mlx5_core_dev *mdev; 5672e1f24a79SParav Pandit int ret, num_counters; 5673aac4492eSDaniel Jurgens u8 mdev_port_num; 56740ad17a8fSMark Bloch 56757c16f477SKamal Heib if (!stats) 5676e1f24a79SParav Pandit return -EINVAL; 56770ad17a8fSMark Bloch 56785dcecbc9SParav Pandit num_counters = cnts->num_q_counters + 56795dcecbc9SParav Pandit cnts->num_cong_counters + 56805dcecbc9SParav Pandit cnts->num_ext_ppcnt_counters; 5681aac4492eSDaniel Jurgens 5682aac4492eSDaniel Jurgens /* q_counters are per IB device, query the master mdev */ 56835dcecbc9SParav Pandit ret = mlx5_ib_query_q_counters(dev->mdev, cnts, stats, cnts->set_id); 56840ad17a8fSMark Bloch if (ret) 5685e1f24a79SParav Pandit return ret; 56860ad17a8fSMark Bloch 56879f876f3dSTalat Batheesh if (MLX5_CAP_PCAM_FEATURE(dev->mdev, rx_icrc_encapsulated_counter)) { 56885dcecbc9SParav Pandit ret = mlx5_ib_query_ext_ppcnt_counters(dev, cnts, stats); 56899f876f3dSTalat Batheesh if (ret) 56909f876f3dSTalat Batheesh return ret; 56919f876f3dSTalat Batheesh } 56929f876f3dSTalat Batheesh 5693e1f24a79SParav Pandit if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) { 5694aac4492eSDaniel Jurgens mdev = mlx5_ib_get_native_port_mdev(dev, port_num, 5695aac4492eSDaniel Jurgens &mdev_port_num); 5696aac4492eSDaniel Jurgens if (!mdev) { 5697aac4492eSDaniel Jurgens /* If port is not affiliated yet, its in down state 5698aac4492eSDaniel Jurgens * which doesn't have any counters yet, so it would be 5699aac4492eSDaniel Jurgens * zero. So no need to read from the HCA. 5700aac4492eSDaniel Jurgens */ 5701aac4492eSDaniel Jurgens goto done; 5702aac4492eSDaniel Jurgens } 570371a0ff65SMajd Dibbiny ret = mlx5_lag_query_cong_counters(dev->mdev, 570471a0ff65SMajd Dibbiny stats->value + 57055dcecbc9SParav Pandit cnts->num_q_counters, 57065dcecbc9SParav Pandit cnts->num_cong_counters, 57075dcecbc9SParav Pandit cnts->offsets + 57085dcecbc9SParav Pandit cnts->num_q_counters); 5709aac4492eSDaniel Jurgens 5710aac4492eSDaniel Jurgens mlx5_ib_put_native_port_mdev(dev, port_num); 5711e1f24a79SParav Pandit if (ret) 5712e1f24a79SParav Pandit return ret; 57130ad17a8fSMark Bloch } 57147c16f477SKamal Heib 5715aac4492eSDaniel Jurgens done: 5716e1f24a79SParav Pandit return num_counters; 57170ad17a8fSMark Bloch } 57180ad17a8fSMark Bloch 571918d422ceSMark Zhang static struct rdma_hw_stats * 572018d422ceSMark Zhang mlx5_ib_counter_alloc_stats(struct rdma_counter *counter) 572118d422ceSMark Zhang { 572218d422ceSMark Zhang struct mlx5_ib_dev *dev = to_mdev(counter->device); 57235dcecbc9SParav Pandit const struct mlx5_ib_counters *cnts = 57243e1f000fSParav Pandit get_counters(dev, counter->port - 1); 572518d422ceSMark Zhang 572618d422ceSMark Zhang /* Q counters are in the beginning of all counters */ 57275dcecbc9SParav Pandit return rdma_alloc_hw_stats_struct(cnts->names, 57285dcecbc9SParav Pandit cnts->num_q_counters, 572918d422ceSMark Zhang RDMA_HW_STATS_DEFAULT_LIFESPAN); 573018d422ceSMark Zhang } 573118d422ceSMark Zhang 573218d422ceSMark Zhang static int mlx5_ib_counter_update_stats(struct rdma_counter *counter) 573318d422ceSMark Zhang { 573418d422ceSMark Zhang struct mlx5_ib_dev *dev = to_mdev(counter->device); 57353e1f000fSParav Pandit const struct mlx5_ib_counters *cnts = 57363e1f000fSParav Pandit get_counters(dev, counter->port - 1); 573718d422ceSMark Zhang 57385dcecbc9SParav Pandit return mlx5_ib_query_q_counters(dev->mdev, cnts, 573918d422ceSMark Zhang counter->stats, counter->id); 574018d422ceSMark Zhang } 574118d422ceSMark Zhang 574245842fc6SMark Zhang static int mlx5_ib_counter_bind_qp(struct rdma_counter *counter, 574345842fc6SMark Zhang struct ib_qp *qp) 574445842fc6SMark Zhang { 574545842fc6SMark Zhang struct mlx5_ib_dev *dev = to_mdev(qp->device); 574645842fc6SMark Zhang u16 cnt_set_id = 0; 574745842fc6SMark Zhang int err; 574845842fc6SMark Zhang 574945842fc6SMark Zhang if (!counter->id) { 575045842fc6SMark Zhang err = mlx5_cmd_alloc_q_counter(dev->mdev, 575145842fc6SMark Zhang &cnt_set_id, 575245842fc6SMark Zhang MLX5_SHARED_RESOURCE_UID); 575345842fc6SMark Zhang if (err) 575445842fc6SMark Zhang return err; 575545842fc6SMark Zhang counter->id = cnt_set_id; 575645842fc6SMark Zhang } 575745842fc6SMark Zhang 575845842fc6SMark Zhang err = mlx5_ib_qp_set_counter(qp, counter); 575945842fc6SMark Zhang if (err) 576045842fc6SMark Zhang goto fail_set_counter; 576145842fc6SMark Zhang 576245842fc6SMark Zhang return 0; 576345842fc6SMark Zhang 576445842fc6SMark Zhang fail_set_counter: 576545842fc6SMark Zhang mlx5_core_dealloc_q_counter(dev->mdev, cnt_set_id); 576645842fc6SMark Zhang counter->id = 0; 576745842fc6SMark Zhang 576845842fc6SMark Zhang return err; 576945842fc6SMark Zhang } 577045842fc6SMark Zhang 577145842fc6SMark Zhang static int mlx5_ib_counter_unbind_qp(struct ib_qp *qp) 577245842fc6SMark Zhang { 577345842fc6SMark Zhang return mlx5_ib_qp_set_counter(qp, NULL); 577445842fc6SMark Zhang } 577545842fc6SMark Zhang 577645842fc6SMark Zhang static int mlx5_ib_counter_dealloc(struct rdma_counter *counter) 577745842fc6SMark Zhang { 577845842fc6SMark Zhang struct mlx5_ib_dev *dev = to_mdev(counter->device); 577945842fc6SMark Zhang 578045842fc6SMark Zhang return mlx5_core_dealloc_q_counter(dev->mdev, counter->id); 578145842fc6SMark Zhang } 578245842fc6SMark Zhang 5783f6a8a19bSDenis Drozdov static int mlx5_ib_rn_get_params(struct ib_device *device, u8 port_num, 5784693dfd5aSErez Shitrit enum rdma_netdev_t type, 5785f6a8a19bSDenis Drozdov struct rdma_netdev_alloc_params *params) 5786693dfd5aSErez Shitrit { 5787693dfd5aSErez Shitrit if (type != RDMA_NETDEV_IPOIB) 5788f6a8a19bSDenis Drozdov return -EOPNOTSUPP; 5789693dfd5aSErez Shitrit 5790f6a8a19bSDenis Drozdov return mlx5_rdma_rn_get_params(to_mdev(device)->mdev, device, params); 5791693dfd5aSErez Shitrit } 5792693dfd5aSErez Shitrit 5793fe248c3aSMaor Gottlieb static void delay_drop_debugfs_cleanup(struct mlx5_ib_dev *dev) 5794fe248c3aSMaor Gottlieb { 579509b0965eSGreg Kroah-Hartman if (!dev->delay_drop.dir_debugfs) 5796fe248c3aSMaor Gottlieb return; 579709b0965eSGreg Kroah-Hartman debugfs_remove_recursive(dev->delay_drop.dir_debugfs); 579809b0965eSGreg Kroah-Hartman dev->delay_drop.dir_debugfs = NULL; 5799fe248c3aSMaor Gottlieb } 5800fe248c3aSMaor Gottlieb 580103404e8aSMaor Gottlieb static void cancel_delay_drop(struct mlx5_ib_dev *dev) 580203404e8aSMaor Gottlieb { 580303404e8aSMaor Gottlieb if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP)) 580403404e8aSMaor Gottlieb return; 580503404e8aSMaor Gottlieb 580603404e8aSMaor Gottlieb cancel_work_sync(&dev->delay_drop.delay_drop_work); 5807fe248c3aSMaor Gottlieb delay_drop_debugfs_cleanup(dev); 5808fe248c3aSMaor Gottlieb } 5809fe248c3aSMaor Gottlieb 5810fe248c3aSMaor Gottlieb static ssize_t delay_drop_timeout_read(struct file *filp, char __user *buf, 5811fe248c3aSMaor Gottlieb size_t count, loff_t *pos) 5812fe248c3aSMaor Gottlieb { 5813fe248c3aSMaor Gottlieb struct mlx5_ib_delay_drop *delay_drop = filp->private_data; 5814fe248c3aSMaor Gottlieb char lbuf[20]; 5815fe248c3aSMaor Gottlieb int len; 5816fe248c3aSMaor Gottlieb 5817fe248c3aSMaor Gottlieb len = snprintf(lbuf, sizeof(lbuf), "%u\n", delay_drop->timeout); 5818fe248c3aSMaor Gottlieb return simple_read_from_buffer(buf, count, pos, lbuf, len); 5819fe248c3aSMaor Gottlieb } 5820fe248c3aSMaor Gottlieb 5821fe248c3aSMaor Gottlieb static ssize_t delay_drop_timeout_write(struct file *filp, const char __user *buf, 5822fe248c3aSMaor Gottlieb size_t count, loff_t *pos) 5823fe248c3aSMaor Gottlieb { 5824fe248c3aSMaor Gottlieb struct mlx5_ib_delay_drop *delay_drop = filp->private_data; 5825fe248c3aSMaor Gottlieb u32 timeout; 5826fe248c3aSMaor Gottlieb u32 var; 5827fe248c3aSMaor Gottlieb 5828fe248c3aSMaor Gottlieb if (kstrtouint_from_user(buf, count, 0, &var)) 5829fe248c3aSMaor Gottlieb return -EFAULT; 5830fe248c3aSMaor Gottlieb 5831fe248c3aSMaor Gottlieb timeout = min_t(u32, roundup(var, 100), MLX5_MAX_DELAY_DROP_TIMEOUT_MS * 5832fe248c3aSMaor Gottlieb 1000); 5833fe248c3aSMaor Gottlieb if (timeout != var) 5834fe248c3aSMaor Gottlieb mlx5_ib_dbg(delay_drop->dev, "Round delay drop timeout to %u usec\n", 5835fe248c3aSMaor Gottlieb timeout); 5836fe248c3aSMaor Gottlieb 5837fe248c3aSMaor Gottlieb delay_drop->timeout = timeout; 5838fe248c3aSMaor Gottlieb 5839fe248c3aSMaor Gottlieb return count; 5840fe248c3aSMaor Gottlieb } 5841fe248c3aSMaor Gottlieb 5842fe248c3aSMaor Gottlieb static const struct file_operations fops_delay_drop_timeout = { 5843fe248c3aSMaor Gottlieb .owner = THIS_MODULE, 5844fe248c3aSMaor Gottlieb .open = simple_open, 5845fe248c3aSMaor Gottlieb .write = delay_drop_timeout_write, 5846fe248c3aSMaor Gottlieb .read = delay_drop_timeout_read, 5847fe248c3aSMaor Gottlieb }; 5848fe248c3aSMaor Gottlieb 584909b0965eSGreg Kroah-Hartman static void delay_drop_debugfs_init(struct mlx5_ib_dev *dev) 5850fe248c3aSMaor Gottlieb { 585109b0965eSGreg Kroah-Hartman struct dentry *root; 5852fe248c3aSMaor Gottlieb 5853fe248c3aSMaor Gottlieb if (!mlx5_debugfs_root) 585409b0965eSGreg Kroah-Hartman return; 5855fe248c3aSMaor Gottlieb 585609b0965eSGreg Kroah-Hartman root = debugfs_create_dir("delay_drop", dev->mdev->priv.dbg_root); 585709b0965eSGreg Kroah-Hartman dev->delay_drop.dir_debugfs = root; 5858fe248c3aSMaor Gottlieb 585909b0965eSGreg Kroah-Hartman debugfs_create_atomic_t("num_timeout_events", 0400, root, 5860fe248c3aSMaor Gottlieb &dev->delay_drop.events_cnt); 586109b0965eSGreg Kroah-Hartman debugfs_create_atomic_t("num_rqs", 0400, root, 5862fe248c3aSMaor Gottlieb &dev->delay_drop.rqs_cnt); 586309b0965eSGreg Kroah-Hartman debugfs_create_file("timeout", 0600, root, &dev->delay_drop, 5864fe248c3aSMaor Gottlieb &fops_delay_drop_timeout); 586503404e8aSMaor Gottlieb } 586603404e8aSMaor Gottlieb 586703404e8aSMaor Gottlieb static void init_delay_drop(struct mlx5_ib_dev *dev) 586803404e8aSMaor Gottlieb { 586903404e8aSMaor Gottlieb if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP)) 587003404e8aSMaor Gottlieb return; 587103404e8aSMaor Gottlieb 587203404e8aSMaor Gottlieb mutex_init(&dev->delay_drop.lock); 587303404e8aSMaor Gottlieb dev->delay_drop.dev = dev; 587403404e8aSMaor Gottlieb dev->delay_drop.activate = false; 587503404e8aSMaor Gottlieb dev->delay_drop.timeout = MLX5_MAX_DELAY_DROP_TIMEOUT_MS * 1000; 587603404e8aSMaor Gottlieb INIT_WORK(&dev->delay_drop.delay_drop_work, delay_drop_handler); 5877fe248c3aSMaor Gottlieb atomic_set(&dev->delay_drop.rqs_cnt, 0); 5878fe248c3aSMaor Gottlieb atomic_set(&dev->delay_drop.events_cnt, 0); 5879fe248c3aSMaor Gottlieb 588009b0965eSGreg Kroah-Hartman delay_drop_debugfs_init(dev); 588103404e8aSMaor Gottlieb } 588203404e8aSMaor Gottlieb 588332f69e4bSDaniel Jurgens static void mlx5_ib_unbind_slave_port(struct mlx5_ib_dev *ibdev, 588432f69e4bSDaniel Jurgens struct mlx5_ib_multiport_info *mpi) 588532f69e4bSDaniel Jurgens { 588632f69e4bSDaniel Jurgens u8 port_num = mlx5_core_native_port_num(mpi->mdev) - 1; 588732f69e4bSDaniel Jurgens struct mlx5_ib_port *port = &ibdev->port[port_num]; 588832f69e4bSDaniel Jurgens int comps; 588932f69e4bSDaniel Jurgens int err; 589032f69e4bSDaniel Jurgens int i; 589132f69e4bSDaniel Jurgens 58929dc4cfffSLeon Romanovsky lockdep_assert_held(&mlx5_ib_multiport_mutex); 58939dc4cfffSLeon Romanovsky 5894a9e546e7SParav Pandit mlx5_ib_cleanup_cong_debugfs(ibdev, port_num); 5895a9e546e7SParav Pandit 589632f69e4bSDaniel Jurgens spin_lock(&port->mp.mpi_lock); 589732f69e4bSDaniel Jurgens if (!mpi->ibdev) { 589832f69e4bSDaniel Jurgens spin_unlock(&port->mp.mpi_lock); 589932f69e4bSDaniel Jurgens return; 590032f69e4bSDaniel Jurgens } 5901df097a27SSaeed Mahameed 590232f69e4bSDaniel Jurgens mpi->ibdev = NULL; 590332f69e4bSDaniel Jurgens 590432f69e4bSDaniel Jurgens spin_unlock(&port->mp.mpi_lock); 590523eaf3b5SLeon Romanovsky if (mpi->mdev_events.notifier_call) 590623eaf3b5SLeon Romanovsky mlx5_notifier_unregister(mpi->mdev, &mpi->mdev_events); 590723eaf3b5SLeon Romanovsky mpi->mdev_events.notifier_call = NULL; 590832f69e4bSDaniel Jurgens mlx5_remove_netdev_notifier(ibdev, port_num); 590932f69e4bSDaniel Jurgens spin_lock(&port->mp.mpi_lock); 591032f69e4bSDaniel Jurgens 591132f69e4bSDaniel Jurgens comps = mpi->mdev_refcnt; 591232f69e4bSDaniel Jurgens if (comps) { 591332f69e4bSDaniel Jurgens mpi->unaffiliate = true; 591432f69e4bSDaniel Jurgens init_completion(&mpi->unref_comp); 591532f69e4bSDaniel Jurgens spin_unlock(&port->mp.mpi_lock); 591632f69e4bSDaniel Jurgens 591732f69e4bSDaniel Jurgens for (i = 0; i < comps; i++) 591832f69e4bSDaniel Jurgens wait_for_completion(&mpi->unref_comp); 591932f69e4bSDaniel Jurgens 592032f69e4bSDaniel Jurgens spin_lock(&port->mp.mpi_lock); 592132f69e4bSDaniel Jurgens mpi->unaffiliate = false; 592232f69e4bSDaniel Jurgens } 592332f69e4bSDaniel Jurgens 592432f69e4bSDaniel Jurgens port->mp.mpi = NULL; 592532f69e4bSDaniel Jurgens 592632f69e4bSDaniel Jurgens list_add_tail(&mpi->list, &mlx5_ib_unaffiliated_port_list); 592732f69e4bSDaniel Jurgens 592832f69e4bSDaniel Jurgens spin_unlock(&port->mp.mpi_lock); 592932f69e4bSDaniel Jurgens 593032f69e4bSDaniel Jurgens err = mlx5_nic_vport_unaffiliate_multiport(mpi->mdev); 593132f69e4bSDaniel Jurgens 593232f69e4bSDaniel Jurgens mlx5_ib_dbg(ibdev, "unaffiliated port %d\n", port_num + 1); 593332f69e4bSDaniel Jurgens /* Log an error, still needed to cleanup the pointers and add 593432f69e4bSDaniel Jurgens * it back to the list. 593532f69e4bSDaniel Jurgens */ 593632f69e4bSDaniel Jurgens if (err) 593732f69e4bSDaniel Jurgens mlx5_ib_err(ibdev, "Failed to unaffiliate port %u\n", 593832f69e4bSDaniel Jurgens port_num + 1); 593932f69e4bSDaniel Jurgens 594095579e78SMark Bloch ibdev->port[port_num].roce.last_port_state = IB_PORT_DOWN; 594132f69e4bSDaniel Jurgens } 594232f69e4bSDaniel Jurgens 594332f69e4bSDaniel Jurgens static bool mlx5_ib_bind_slave_port(struct mlx5_ib_dev *ibdev, 594432f69e4bSDaniel Jurgens struct mlx5_ib_multiport_info *mpi) 594532f69e4bSDaniel Jurgens { 594632f69e4bSDaniel Jurgens u8 port_num = mlx5_core_native_port_num(mpi->mdev) - 1; 594732f69e4bSDaniel Jurgens int err; 594832f69e4bSDaniel Jurgens 59499dc4cfffSLeon Romanovsky lockdep_assert_held(&mlx5_ib_multiport_mutex); 59509dc4cfffSLeon Romanovsky 595132f69e4bSDaniel Jurgens spin_lock(&ibdev->port[port_num].mp.mpi_lock); 595232f69e4bSDaniel Jurgens if (ibdev->port[port_num].mp.mpi) { 59532577188eSQing Huang mlx5_ib_dbg(ibdev, "port %d already affiliated.\n", 595432f69e4bSDaniel Jurgens port_num + 1); 595532f69e4bSDaniel Jurgens spin_unlock(&ibdev->port[port_num].mp.mpi_lock); 595632f69e4bSDaniel Jurgens return false; 595732f69e4bSDaniel Jurgens } 595832f69e4bSDaniel Jurgens 595932f69e4bSDaniel Jurgens ibdev->port[port_num].mp.mpi = mpi; 596032f69e4bSDaniel Jurgens mpi->ibdev = ibdev; 5961df097a27SSaeed Mahameed mpi->mdev_events.notifier_call = NULL; 596232f69e4bSDaniel Jurgens spin_unlock(&ibdev->port[port_num].mp.mpi_lock); 596332f69e4bSDaniel Jurgens 596432f69e4bSDaniel Jurgens err = mlx5_nic_vport_affiliate_multiport(ibdev->mdev, mpi->mdev); 596532f69e4bSDaniel Jurgens if (err) 596632f69e4bSDaniel Jurgens goto unbind; 596732f69e4bSDaniel Jurgens 596832f69e4bSDaniel Jurgens err = get_port_caps(ibdev, mlx5_core_native_port_num(mpi->mdev)); 596932f69e4bSDaniel Jurgens if (err) 597032f69e4bSDaniel Jurgens goto unbind; 597132f69e4bSDaniel Jurgens 597232f69e4bSDaniel Jurgens err = mlx5_add_netdev_notifier(ibdev, port_num); 597332f69e4bSDaniel Jurgens if (err) { 597432f69e4bSDaniel Jurgens mlx5_ib_err(ibdev, "failed adding netdev notifier for port %u\n", 597532f69e4bSDaniel Jurgens port_num + 1); 597632f69e4bSDaniel Jurgens goto unbind; 597732f69e4bSDaniel Jurgens } 597832f69e4bSDaniel Jurgens 5979df097a27SSaeed Mahameed mpi->mdev_events.notifier_call = mlx5_ib_event_slave_port; 5980df097a27SSaeed Mahameed mlx5_notifier_register(mpi->mdev, &mpi->mdev_events); 5981df097a27SSaeed Mahameed 598273eb8f03SGreg Kroah-Hartman mlx5_ib_init_cong_debugfs(ibdev, port_num); 5983a9e546e7SParav Pandit 598432f69e4bSDaniel Jurgens return true; 598532f69e4bSDaniel Jurgens 598632f69e4bSDaniel Jurgens unbind: 598732f69e4bSDaniel Jurgens mlx5_ib_unbind_slave_port(ibdev, mpi); 598832f69e4bSDaniel Jurgens return false; 598932f69e4bSDaniel Jurgens } 599032f69e4bSDaniel Jurgens 599132f69e4bSDaniel Jurgens static int mlx5_ib_init_multiport_master(struct mlx5_ib_dev *dev) 599232f69e4bSDaniel Jurgens { 599332f69e4bSDaniel Jurgens int port_num = mlx5_core_native_port_num(dev->mdev) - 1; 599432f69e4bSDaniel Jurgens enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev, 599532f69e4bSDaniel Jurgens port_num + 1); 599632f69e4bSDaniel Jurgens struct mlx5_ib_multiport_info *mpi; 599732f69e4bSDaniel Jurgens int err; 599832f69e4bSDaniel Jurgens int i; 599932f69e4bSDaniel Jurgens 600032f69e4bSDaniel Jurgens if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET) 600132f69e4bSDaniel Jurgens return 0; 600232f69e4bSDaniel Jurgens 600332f69e4bSDaniel Jurgens err = mlx5_query_nic_vport_system_image_guid(dev->mdev, 600432f69e4bSDaniel Jurgens &dev->sys_image_guid); 600532f69e4bSDaniel Jurgens if (err) 600632f69e4bSDaniel Jurgens return err; 600732f69e4bSDaniel Jurgens 600832f69e4bSDaniel Jurgens err = mlx5_nic_vport_enable_roce(dev->mdev); 600932f69e4bSDaniel Jurgens if (err) 601032f69e4bSDaniel Jurgens return err; 601132f69e4bSDaniel Jurgens 601232f69e4bSDaniel Jurgens mutex_lock(&mlx5_ib_multiport_mutex); 601332f69e4bSDaniel Jurgens for (i = 0; i < dev->num_ports; i++) { 601432f69e4bSDaniel Jurgens bool bound = false; 601532f69e4bSDaniel Jurgens 601632f69e4bSDaniel Jurgens /* build a stub multiport info struct for the native port. */ 601732f69e4bSDaniel Jurgens if (i == port_num) { 601832f69e4bSDaniel Jurgens mpi = kzalloc(sizeof(*mpi), GFP_KERNEL); 601932f69e4bSDaniel Jurgens if (!mpi) { 602032f69e4bSDaniel Jurgens mutex_unlock(&mlx5_ib_multiport_mutex); 602132f69e4bSDaniel Jurgens mlx5_nic_vport_disable_roce(dev->mdev); 602232f69e4bSDaniel Jurgens return -ENOMEM; 602332f69e4bSDaniel Jurgens } 602432f69e4bSDaniel Jurgens 602532f69e4bSDaniel Jurgens mpi->is_master = true; 602632f69e4bSDaniel Jurgens mpi->mdev = dev->mdev; 602732f69e4bSDaniel Jurgens mpi->sys_image_guid = dev->sys_image_guid; 602832f69e4bSDaniel Jurgens dev->port[i].mp.mpi = mpi; 602932f69e4bSDaniel Jurgens mpi->ibdev = dev; 603032f69e4bSDaniel Jurgens mpi = NULL; 603132f69e4bSDaniel Jurgens continue; 603232f69e4bSDaniel Jurgens } 603332f69e4bSDaniel Jurgens 603432f69e4bSDaniel Jurgens list_for_each_entry(mpi, &mlx5_ib_unaffiliated_port_list, 603532f69e4bSDaniel Jurgens list) { 603632f69e4bSDaniel Jurgens if (dev->sys_image_guid == mpi->sys_image_guid && 603732f69e4bSDaniel Jurgens (mlx5_core_native_port_num(mpi->mdev) - 1) == i) { 603832f69e4bSDaniel Jurgens bound = mlx5_ib_bind_slave_port(dev, mpi); 603932f69e4bSDaniel Jurgens } 604032f69e4bSDaniel Jurgens 604132f69e4bSDaniel Jurgens if (bound) { 6042c42260f1SVu Pham dev_dbg(mpi->mdev->device, 6043c42260f1SVu Pham "removing port from unaffiliated list.\n"); 604432f69e4bSDaniel Jurgens mlx5_ib_dbg(dev, "port %d bound\n", i + 1); 604532f69e4bSDaniel Jurgens list_del(&mpi->list); 604632f69e4bSDaniel Jurgens break; 604732f69e4bSDaniel Jurgens } 604832f69e4bSDaniel Jurgens } 604932f69e4bSDaniel Jurgens if (!bound) { 605032f69e4bSDaniel Jurgens get_port_caps(dev, i + 1); 605132f69e4bSDaniel Jurgens mlx5_ib_dbg(dev, "no free port found for port %d\n", 605232f69e4bSDaniel Jurgens i + 1); 605332f69e4bSDaniel Jurgens } 605432f69e4bSDaniel Jurgens } 605532f69e4bSDaniel Jurgens 605632f69e4bSDaniel Jurgens list_add_tail(&dev->ib_dev_list, &mlx5_ib_dev_list); 605732f69e4bSDaniel Jurgens mutex_unlock(&mlx5_ib_multiport_mutex); 605832f69e4bSDaniel Jurgens return err; 605932f69e4bSDaniel Jurgens } 606032f69e4bSDaniel Jurgens 606132f69e4bSDaniel Jurgens static void mlx5_ib_cleanup_multiport_master(struct mlx5_ib_dev *dev) 606232f69e4bSDaniel Jurgens { 606332f69e4bSDaniel Jurgens int port_num = mlx5_core_native_port_num(dev->mdev) - 1; 606432f69e4bSDaniel Jurgens enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev, 606532f69e4bSDaniel Jurgens port_num + 1); 606632f69e4bSDaniel Jurgens int i; 606732f69e4bSDaniel Jurgens 606832f69e4bSDaniel Jurgens if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET) 606932f69e4bSDaniel Jurgens return; 607032f69e4bSDaniel Jurgens 607132f69e4bSDaniel Jurgens mutex_lock(&mlx5_ib_multiport_mutex); 607232f69e4bSDaniel Jurgens for (i = 0; i < dev->num_ports; i++) { 607332f69e4bSDaniel Jurgens if (dev->port[i].mp.mpi) { 607432f69e4bSDaniel Jurgens /* Destroy the native port stub */ 607532f69e4bSDaniel Jurgens if (i == port_num) { 607632f69e4bSDaniel Jurgens kfree(dev->port[i].mp.mpi); 607732f69e4bSDaniel Jurgens dev->port[i].mp.mpi = NULL; 607832f69e4bSDaniel Jurgens } else { 607932f69e4bSDaniel Jurgens mlx5_ib_dbg(dev, "unbinding port_num: %d\n", i + 1); 608032f69e4bSDaniel Jurgens mlx5_ib_unbind_slave_port(dev, dev->port[i].mp.mpi); 608132f69e4bSDaniel Jurgens } 608232f69e4bSDaniel Jurgens } 608332f69e4bSDaniel Jurgens } 608432f69e4bSDaniel Jurgens 608532f69e4bSDaniel Jurgens mlx5_ib_dbg(dev, "removing from devlist\n"); 608632f69e4bSDaniel Jurgens list_del(&dev->ib_dev_list); 608732f69e4bSDaniel Jurgens mutex_unlock(&mlx5_ib_multiport_mutex); 608832f69e4bSDaniel Jurgens 608932f69e4bSDaniel Jurgens mlx5_nic_vport_disable_roce(dev->mdev); 609032f69e4bSDaniel Jurgens } 609132f69e4bSDaniel Jurgens 60927be76befSYishai Hadas static int var_obj_cleanup(struct ib_uobject *uobject, 60937be76befSYishai Hadas enum rdma_remove_reason why, 60947be76befSYishai Hadas struct uverbs_attr_bundle *attrs) 60957be76befSYishai Hadas { 60967be76befSYishai Hadas struct mlx5_user_mmap_entry *obj = uobject->object; 60977be76befSYishai Hadas 60987be76befSYishai Hadas rdma_user_mmap_entry_remove(&obj->rdma_entry); 60997be76befSYishai Hadas return 0; 61007be76befSYishai Hadas } 61017be76befSYishai Hadas 61027be76befSYishai Hadas static struct mlx5_user_mmap_entry * 61037be76befSYishai Hadas alloc_var_entry(struct mlx5_ib_ucontext *c) 61047be76befSYishai Hadas { 61057be76befSYishai Hadas struct mlx5_user_mmap_entry *entry; 61067be76befSYishai Hadas struct mlx5_var_table *var_table; 61077be76befSYishai Hadas u32 page_idx; 61087be76befSYishai Hadas int err; 61097be76befSYishai Hadas 61107be76befSYishai Hadas var_table = &to_mdev(c->ibucontext.device)->var_table; 61117be76befSYishai Hadas entry = kzalloc(sizeof(*entry), GFP_KERNEL); 61127be76befSYishai Hadas if (!entry) 61137be76befSYishai Hadas return ERR_PTR(-ENOMEM); 61147be76befSYishai Hadas 61157be76befSYishai Hadas mutex_lock(&var_table->bitmap_lock); 61167be76befSYishai Hadas page_idx = find_first_zero_bit(var_table->bitmap, 61177be76befSYishai Hadas var_table->num_var_hw_entries); 61187be76befSYishai Hadas if (page_idx >= var_table->num_var_hw_entries) { 61197be76befSYishai Hadas err = -ENOSPC; 61207be76befSYishai Hadas mutex_unlock(&var_table->bitmap_lock); 61217be76befSYishai Hadas goto end; 61227be76befSYishai Hadas } 61237be76befSYishai Hadas 61247be76befSYishai Hadas set_bit(page_idx, var_table->bitmap); 61257be76befSYishai Hadas mutex_unlock(&var_table->bitmap_lock); 61267be76befSYishai Hadas 61277be76befSYishai Hadas entry->address = var_table->hw_start_addr + 61287be76befSYishai Hadas (page_idx * var_table->stride_size); 61297be76befSYishai Hadas entry->page_idx = page_idx; 61307be76befSYishai Hadas entry->mmap_flag = MLX5_IB_MMAP_TYPE_VAR; 61317be76befSYishai Hadas 61327be76befSYishai Hadas err = rdma_user_mmap_entry_insert_range( 61337be76befSYishai Hadas &c->ibucontext, &entry->rdma_entry, var_table->stride_size, 61347be76befSYishai Hadas MLX5_IB_MMAP_OFFSET_START << 16, 61357be76befSYishai Hadas (MLX5_IB_MMAP_OFFSET_END << 16) + (1UL << 16) - 1); 61367be76befSYishai Hadas if (err) 61377be76befSYishai Hadas goto err_insert; 61387be76befSYishai Hadas 61397be76befSYishai Hadas return entry; 61407be76befSYishai Hadas 61417be76befSYishai Hadas err_insert: 61427be76befSYishai Hadas mutex_lock(&var_table->bitmap_lock); 61437be76befSYishai Hadas clear_bit(page_idx, var_table->bitmap); 61447be76befSYishai Hadas mutex_unlock(&var_table->bitmap_lock); 61457be76befSYishai Hadas end: 61467be76befSYishai Hadas kfree(entry); 61477be76befSYishai Hadas return ERR_PTR(err); 61487be76befSYishai Hadas } 61497be76befSYishai Hadas 61507be76befSYishai Hadas static int UVERBS_HANDLER(MLX5_IB_METHOD_VAR_OBJ_ALLOC)( 61517be76befSYishai Hadas struct uverbs_attr_bundle *attrs) 61527be76befSYishai Hadas { 61537be76befSYishai Hadas struct ib_uobject *uobj = uverbs_attr_get_uobject( 61547be76befSYishai Hadas attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_HANDLE); 61557be76befSYishai Hadas struct mlx5_ib_ucontext *c; 61567be76befSYishai Hadas struct mlx5_user_mmap_entry *entry; 61577be76befSYishai Hadas u64 mmap_offset; 61587be76befSYishai Hadas u32 length; 61597be76befSYishai Hadas int err; 61607be76befSYishai Hadas 61617be76befSYishai Hadas c = to_mucontext(ib_uverbs_get_ucontext(attrs)); 61627be76befSYishai Hadas if (IS_ERR(c)) 61637be76befSYishai Hadas return PTR_ERR(c); 61647be76befSYishai Hadas 61657be76befSYishai Hadas entry = alloc_var_entry(c); 61667be76befSYishai Hadas if (IS_ERR(entry)) 61677be76befSYishai Hadas return PTR_ERR(entry); 61687be76befSYishai Hadas 61697be76befSYishai Hadas mmap_offset = mlx5_entry_to_mmap_offset(entry); 61707be76befSYishai Hadas length = entry->rdma_entry.npages * PAGE_SIZE; 61717be76befSYishai Hadas uobj->object = entry; 61727be76befSYishai Hadas 61737be76befSYishai Hadas err = uverbs_copy_to(attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_MMAP_OFFSET, 61747be76befSYishai Hadas &mmap_offset, sizeof(mmap_offset)); 61757be76befSYishai Hadas if (err) 61767be76befSYishai Hadas goto err; 61777be76befSYishai Hadas 61787be76befSYishai Hadas err = uverbs_copy_to(attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_PAGE_ID, 61797be76befSYishai Hadas &entry->page_idx, sizeof(entry->page_idx)); 61807be76befSYishai Hadas if (err) 61817be76befSYishai Hadas goto err; 61827be76befSYishai Hadas 61837be76befSYishai Hadas err = uverbs_copy_to(attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_MMAP_LENGTH, 61847be76befSYishai Hadas &length, sizeof(length)); 61857be76befSYishai Hadas if (err) 61867be76befSYishai Hadas goto err; 61877be76befSYishai Hadas 61887be76befSYishai Hadas return 0; 61897be76befSYishai Hadas 61907be76befSYishai Hadas err: 61917be76befSYishai Hadas rdma_user_mmap_entry_remove(&entry->rdma_entry); 61927be76befSYishai Hadas return err; 61937be76befSYishai Hadas } 61947be76befSYishai Hadas 61957be76befSYishai Hadas DECLARE_UVERBS_NAMED_METHOD( 61967be76befSYishai Hadas MLX5_IB_METHOD_VAR_OBJ_ALLOC, 61977be76befSYishai Hadas UVERBS_ATTR_IDR(MLX5_IB_ATTR_VAR_OBJ_ALLOC_HANDLE, 61987be76befSYishai Hadas MLX5_IB_OBJECT_VAR, 61997be76befSYishai Hadas UVERBS_ACCESS_NEW, 62007be76befSYishai Hadas UA_MANDATORY), 62017be76befSYishai Hadas UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_VAR_OBJ_ALLOC_PAGE_ID, 62027be76befSYishai Hadas UVERBS_ATTR_TYPE(u32), 62037be76befSYishai Hadas UA_MANDATORY), 62047be76befSYishai Hadas UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_VAR_OBJ_ALLOC_MMAP_LENGTH, 62057be76befSYishai Hadas UVERBS_ATTR_TYPE(u32), 62067be76befSYishai Hadas UA_MANDATORY), 62077be76befSYishai Hadas UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_VAR_OBJ_ALLOC_MMAP_OFFSET, 62087be76befSYishai Hadas UVERBS_ATTR_TYPE(u64), 62097be76befSYishai Hadas UA_MANDATORY)); 62107be76befSYishai Hadas 62117be76befSYishai Hadas DECLARE_UVERBS_NAMED_METHOD_DESTROY( 62127be76befSYishai Hadas MLX5_IB_METHOD_VAR_OBJ_DESTROY, 62137be76befSYishai Hadas UVERBS_ATTR_IDR(MLX5_IB_ATTR_VAR_OBJ_DESTROY_HANDLE, 62147be76befSYishai Hadas MLX5_IB_OBJECT_VAR, 62157be76befSYishai Hadas UVERBS_ACCESS_DESTROY, 62167be76befSYishai Hadas UA_MANDATORY)); 62177be76befSYishai Hadas 62187be76befSYishai Hadas DECLARE_UVERBS_NAMED_OBJECT(MLX5_IB_OBJECT_VAR, 62197be76befSYishai Hadas UVERBS_TYPE_ALLOC_IDR(var_obj_cleanup), 62207be76befSYishai Hadas &UVERBS_METHOD(MLX5_IB_METHOD_VAR_OBJ_ALLOC), 62217be76befSYishai Hadas &UVERBS_METHOD(MLX5_IB_METHOD_VAR_OBJ_DESTROY)); 62227be76befSYishai Hadas 62237be76befSYishai Hadas static bool var_is_supported(struct ib_device *device) 62247be76befSYishai Hadas { 62257be76befSYishai Hadas struct mlx5_ib_dev *dev = to_mdev(device); 62267be76befSYishai Hadas 62277be76befSYishai Hadas return (MLX5_CAP_GEN_64(dev->mdev, general_obj_types) & 62287be76befSYishai Hadas MLX5_GENERAL_OBJ_TYPES_CAP_VIRTIO_NET_Q); 62297be76befSYishai Hadas } 62307be76befSYishai Hadas 62319a119cd5SJason Gunthorpe ADD_UVERBS_ATTRIBUTES_SIMPLE( 62329a119cd5SJason Gunthorpe mlx5_ib_dm, 62339a119cd5SJason Gunthorpe UVERBS_OBJECT_DM, 623424da0016SAriel Levkovich UVERBS_METHOD_DM_ALLOC, 62359a119cd5SJason Gunthorpe UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_ALLOC_DM_RESP_START_OFFSET, 623624da0016SAriel Levkovich UVERBS_ATTR_TYPE(u64), 623783bb4442SJason Gunthorpe UA_MANDATORY), 62389a119cd5SJason Gunthorpe UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_ALLOC_DM_RESP_PAGE_INDEX, 623924da0016SAriel Levkovich UVERBS_ATTR_TYPE(u16), 62403b113a1eSAriel Levkovich UA_OPTIONAL), 62413b113a1eSAriel Levkovich UVERBS_ATTR_CONST_IN(MLX5_IB_ATTR_ALLOC_DM_REQ_TYPE, 62423b113a1eSAriel Levkovich enum mlx5_ib_uapi_dm_type, 62433b113a1eSAriel Levkovich UA_OPTIONAL)); 624424da0016SAriel Levkovich 62459a119cd5SJason Gunthorpe ADD_UVERBS_ATTRIBUTES_SIMPLE( 62469a119cd5SJason Gunthorpe mlx5_ib_flow_action, 62479a119cd5SJason Gunthorpe UVERBS_OBJECT_FLOW_ACTION, 6248c6475a0bSAviad Yehezkel UVERBS_METHOD_FLOW_ACTION_ESP_CREATE, 6249bccd0622SJason Gunthorpe UVERBS_ATTR_FLAGS_IN(MLX5_IB_ATTR_CREATE_FLOW_ACTION_FLAGS, 6250bccd0622SJason Gunthorpe enum mlx5_ib_uapi_flow_action_flags)); 6251c6475a0bSAviad Yehezkel 62520cbf432dSJason Gunthorpe static const struct uapi_definition mlx5_ib_defs[] = { 625336e235c8SJason Gunthorpe UAPI_DEF_CHAIN(mlx5_ib_devx_defs), 62540cbf432dSJason Gunthorpe UAPI_DEF_CHAIN(mlx5_ib_flow_defs), 62558c84660bSMatan Barak 62560cbf432dSJason Gunthorpe UAPI_DEF_CHAIN_OBJ_TREE(UVERBS_OBJECT_FLOW_ACTION, 62570cbf432dSJason Gunthorpe &mlx5_ib_flow_action), 62580cbf432dSJason Gunthorpe UAPI_DEF_CHAIN_OBJ_TREE(UVERBS_OBJECT_DM, &mlx5_ib_dm), 62597be76befSYishai Hadas UAPI_DEF_CHAIN_OBJ_TREE_NAMED(MLX5_IB_OBJECT_VAR, 62607be76befSYishai Hadas UAPI_DEF_IS_OBJ_SUPPORTED(var_is_supported)), 62610cbf432dSJason Gunthorpe {} 62620cbf432dSJason Gunthorpe }; 62638c84660bSMatan Barak 62641a1e03dcSRaed Salem static int mlx5_ib_read_counters(struct ib_counters *counters, 62651a1e03dcSRaed Salem struct ib_counters_read_attr *read_attr, 62661a1e03dcSRaed Salem struct uverbs_attr_bundle *attrs) 62671a1e03dcSRaed Salem { 62681a1e03dcSRaed Salem struct mlx5_ib_mcounters *mcounters = to_mcounters(counters); 62691a1e03dcSRaed Salem struct mlx5_read_counters_attr mread_attr = {}; 62701a1e03dcSRaed Salem struct mlx5_ib_flow_counters_desc *desc; 62711a1e03dcSRaed Salem int ret, i; 62721a1e03dcSRaed Salem 62731a1e03dcSRaed Salem mutex_lock(&mcounters->mcntrs_mutex); 62741a1e03dcSRaed Salem if (mcounters->cntrs_max_index > read_attr->ncounters) { 62751a1e03dcSRaed Salem ret = -EINVAL; 62761a1e03dcSRaed Salem goto err_bound; 62771a1e03dcSRaed Salem } 62781a1e03dcSRaed Salem 62791a1e03dcSRaed Salem mread_attr.out = kcalloc(mcounters->counters_num, sizeof(u64), 62801a1e03dcSRaed Salem GFP_KERNEL); 62811a1e03dcSRaed Salem if (!mread_attr.out) { 62821a1e03dcSRaed Salem ret = -ENOMEM; 62831a1e03dcSRaed Salem goto err_bound; 62841a1e03dcSRaed Salem } 62851a1e03dcSRaed Salem 62861a1e03dcSRaed Salem mread_attr.hw_cntrs_hndl = mcounters->hw_cntrs_hndl; 62871a1e03dcSRaed Salem mread_attr.flags = read_attr->flags; 62881a1e03dcSRaed Salem ret = mcounters->read_counters(counters->device, &mread_attr); 62891a1e03dcSRaed Salem if (ret) 62901a1e03dcSRaed Salem goto err_read; 62911a1e03dcSRaed Salem 62921a1e03dcSRaed Salem /* do the pass over the counters data array to assign according to the 62931a1e03dcSRaed Salem * descriptions and indexing pairs 62941a1e03dcSRaed Salem */ 62951a1e03dcSRaed Salem desc = mcounters->counters_data; 62961a1e03dcSRaed Salem for (i = 0; i < mcounters->ncounters; i++) 62971a1e03dcSRaed Salem read_attr->counters_buff[desc[i].index] += mread_attr.out[desc[i].description]; 62981a1e03dcSRaed Salem 62991a1e03dcSRaed Salem err_read: 63001a1e03dcSRaed Salem kfree(mread_attr.out); 63011a1e03dcSRaed Salem err_bound: 63021a1e03dcSRaed Salem mutex_unlock(&mcounters->mcntrs_mutex); 63031a1e03dcSRaed Salem return ret; 63041a1e03dcSRaed Salem } 63051a1e03dcSRaed Salem 6306b29e2a13SRaed Salem static int mlx5_ib_destroy_counters(struct ib_counters *counters) 6307b29e2a13SRaed Salem { 6308b29e2a13SRaed Salem struct mlx5_ib_mcounters *mcounters = to_mcounters(counters); 6309b29e2a13SRaed Salem 63103b3233fbSRaed Salem counters_clear_description(counters); 63113b3233fbSRaed Salem if (mcounters->hw_cntrs_hndl) 63123b3233fbSRaed Salem mlx5_fc_destroy(to_mdev(counters->device)->mdev, 63133b3233fbSRaed Salem mcounters->hw_cntrs_hndl); 63143b3233fbSRaed Salem 6315b29e2a13SRaed Salem kfree(mcounters); 6316b29e2a13SRaed Salem 6317b29e2a13SRaed Salem return 0; 6318b29e2a13SRaed Salem } 6319b29e2a13SRaed Salem 6320b29e2a13SRaed Salem static struct ib_counters *mlx5_ib_create_counters(struct ib_device *device, 6321b29e2a13SRaed Salem struct uverbs_attr_bundle *attrs) 6322b29e2a13SRaed Salem { 6323b29e2a13SRaed Salem struct mlx5_ib_mcounters *mcounters; 6324b29e2a13SRaed Salem 6325b29e2a13SRaed Salem mcounters = kzalloc(sizeof(*mcounters), GFP_KERNEL); 6326b29e2a13SRaed Salem if (!mcounters) 6327b29e2a13SRaed Salem return ERR_PTR(-ENOMEM); 6328b29e2a13SRaed Salem 63293b3233fbSRaed Salem mutex_init(&mcounters->mcntrs_mutex); 63303b3233fbSRaed Salem 6331b29e2a13SRaed Salem return &mcounters->ibcntrs; 6332b29e2a13SRaed Salem } 6333b29e2a13SRaed Salem 6334fb652d32SMark Bloch static void mlx5_ib_stage_init_cleanup(struct mlx5_ib_dev *dev) 6335e126ba97SEli Cohen { 633632f69e4bSDaniel Jurgens mlx5_ib_cleanup_multiport_master(dev); 6337806b101bSJason Gunthorpe WARN_ON(!xa_empty(&dev->odp_mkeys)); 6338806b101bSJason Gunthorpe cleanup_srcu_struct(&dev->odp_srcu); 63394056b12eSAriel Levkovich 634050211ec9SJason Gunthorpe WARN_ON(!xa_empty(&dev->sig_mrs)); 63414056b12eSAriel Levkovich WARN_ON(!bitmap_empty(dev->dm.memic_alloc_pages, MLX5_MAX_MEMIC_PAGES)); 634216c1975fSMark Bloch } 634316c1975fSMark Bloch 6344fb652d32SMark Bloch static int mlx5_ib_stage_init_init(struct mlx5_ib_dev *dev) 634516c1975fSMark Bloch { 634616c1975fSMark Bloch struct mlx5_core_dev *mdev = dev->mdev; 6347e126ba97SEli Cohen int err; 634832f69e4bSDaniel Jurgens int i; 6349e126ba97SEli Cohen 635032f69e4bSDaniel Jurgens for (i = 0; i < dev->num_ports; i++) { 635132f69e4bSDaniel Jurgens spin_lock_init(&dev->port[i].mp.mpi_lock); 635295579e78SMark Bloch rwlock_init(&dev->port[i].roce.netdev_lock); 6353d3b5cc1cSMark Bloch dev->port[i].roce.dev = dev; 6354d3b5cc1cSMark Bloch dev->port[i].roce.native_port_num = i + 1; 6355d3b5cc1cSMark Bloch dev->port[i].roce.last_port_state = IB_PORT_DOWN; 635632f69e4bSDaniel Jurgens } 635732f69e4bSDaniel Jurgens 635800815752SMoni Shoua mlx5_ib_internal_fill_odp_caps(dev); 635900815752SMoni Shoua 636032f69e4bSDaniel Jurgens err = mlx5_ib_init_multiport_master(dev); 6361e126ba97SEli Cohen if (err) 6362da796ccbSMark Bloch return err; 6363e126ba97SEli Cohen 6364a989ea01SMark Bloch err = set_has_smi_cap(dev); 6365a989ea01SMark Bloch if (err) 6366a989ea01SMark Bloch return err; 6367e126ba97SEli Cohen 636832f69e4bSDaniel Jurgens if (!mlx5_core_mp_enabled(mdev)) { 636932f69e4bSDaniel Jurgens for (i = 1; i <= dev->num_ports; i++) { 637032f69e4bSDaniel Jurgens err = get_port_caps(dev, i); 637132f69e4bSDaniel Jurgens if (err) 637232f69e4bSDaniel Jurgens break; 637332f69e4bSDaniel Jurgens } 637432f69e4bSDaniel Jurgens } else { 637532f69e4bSDaniel Jurgens err = get_port_caps(dev, mlx5_core_native_port_num(mdev)); 637632f69e4bSDaniel Jurgens } 637732f69e4bSDaniel Jurgens if (err) 637832f69e4bSDaniel Jurgens goto err_mp; 637932f69e4bSDaniel Jurgens 63801b5daf11SMajd Dibbiny if (mlx5_use_mad_ifc(dev)) 6381e126ba97SEli Cohen get_ext_port_caps(dev); 6382e126ba97SEli Cohen 6383e126ba97SEli Cohen dev->ib_dev.node_type = RDMA_NODE_IB_CA; 6384c6790aa9SSagi Grimberg dev->ib_dev.local_dma_lkey = 0 /* not supported for now */; 6385e126ba97SEli Cohen dev->ib_dev.phys_port_cnt = dev->num_ports; 6386f2f3df55SSaeed Mahameed dev->ib_dev.num_comp_vectors = mlx5_comp_vectors_count(mdev); 6387c42260f1SVu Pham dev->ib_dev.dev.parent = mdev->device; 6388e126ba97SEli Cohen 63893cc297dbSMark Bloch mutex_init(&dev->cap_mask_mutex); 63903cc297dbSMark Bloch INIT_LIST_HEAD(&dev->qp_list); 63913cc297dbSMark Bloch spin_lock_init(&dev->reset_flow_resource_lock); 6392806b101bSJason Gunthorpe xa_init(&dev->odp_mkeys); 639350211ec9SJason Gunthorpe xa_init(&dev->sig_mrs); 63943cc297dbSMark Bloch 63953b113a1eSAriel Levkovich spin_lock_init(&dev->dm.lock); 63963b113a1eSAriel Levkovich dev->dm.dev = mdev; 639724da0016SAriel Levkovich 6398806b101bSJason Gunthorpe err = init_srcu_struct(&dev->odp_srcu); 6399a6bc3875SMoni Shoua if (err) 6400c9b9dcb4SAriel Levkovich goto err_mp; 64013cc297dbSMark Bloch 640216c1975fSMark Bloch return 0; 640325c13324SAriel Levkovich 640432f69e4bSDaniel Jurgens err_mp: 640532f69e4bSDaniel Jurgens mlx5_ib_cleanup_multiport_master(dev); 640616c1975fSMark Bloch 640716c1975fSMark Bloch return -ENOMEM; 640816c1975fSMark Bloch } 640916c1975fSMark Bloch 64109a4ca38dSMark Bloch static int mlx5_ib_stage_flow_db_init(struct mlx5_ib_dev *dev) 64119a4ca38dSMark Bloch { 64129a4ca38dSMark Bloch dev->flow_db = kzalloc(sizeof(*dev->flow_db), GFP_KERNEL); 64139a4ca38dSMark Bloch 64149a4ca38dSMark Bloch if (!dev->flow_db) 64159a4ca38dSMark Bloch return -ENOMEM; 64169a4ca38dSMark Bloch 64179a4ca38dSMark Bloch mutex_init(&dev->flow_db->lock); 64189a4ca38dSMark Bloch 64199a4ca38dSMark Bloch return 0; 64209a4ca38dSMark Bloch } 64219a4ca38dSMark Bloch 64229a4ca38dSMark Bloch static void mlx5_ib_stage_flow_db_cleanup(struct mlx5_ib_dev *dev) 64239a4ca38dSMark Bloch { 64249a4ca38dSMark Bloch kfree(dev->flow_db); 64259a4ca38dSMark Bloch } 64269a4ca38dSMark Bloch 642796458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_ops = { 64287a154142SJason Gunthorpe .owner = THIS_MODULE, 6429b9560a41SJason Gunthorpe .driver_id = RDMA_DRIVER_MLX5, 643072c6ec18SJason Gunthorpe .uverbs_abi_ver = MLX5_IB_UVERBS_ABI_VERSION, 6431b9560a41SJason Gunthorpe 643296458233SKamal Heib .add_gid = mlx5_ib_add_gid, 643396458233SKamal Heib .alloc_mr = mlx5_ib_alloc_mr, 64346c984472SMax Gurtovoy .alloc_mr_integrity = mlx5_ib_alloc_mr_integrity, 643596458233SKamal Heib .alloc_pd = mlx5_ib_alloc_pd, 643696458233SKamal Heib .alloc_ucontext = mlx5_ib_alloc_ucontext, 643796458233SKamal Heib .attach_mcast = mlx5_ib_mcg_attach, 643896458233SKamal Heib .check_mr_status = mlx5_ib_check_mr_status, 643996458233SKamal Heib .create_ah = mlx5_ib_create_ah, 644096458233SKamal Heib .create_counters = mlx5_ib_create_counters, 644196458233SKamal Heib .create_cq = mlx5_ib_create_cq, 644296458233SKamal Heib .create_flow = mlx5_ib_create_flow, 644396458233SKamal Heib .create_qp = mlx5_ib_create_qp, 644496458233SKamal Heib .create_srq = mlx5_ib_create_srq, 644596458233SKamal Heib .dealloc_pd = mlx5_ib_dealloc_pd, 644696458233SKamal Heib .dealloc_ucontext = mlx5_ib_dealloc_ucontext, 644796458233SKamal Heib .del_gid = mlx5_ib_del_gid, 644896458233SKamal Heib .dereg_mr = mlx5_ib_dereg_mr, 644996458233SKamal Heib .destroy_ah = mlx5_ib_destroy_ah, 645096458233SKamal Heib .destroy_counters = mlx5_ib_destroy_counters, 645196458233SKamal Heib .destroy_cq = mlx5_ib_destroy_cq, 645296458233SKamal Heib .destroy_flow = mlx5_ib_destroy_flow, 645396458233SKamal Heib .destroy_flow_action = mlx5_ib_destroy_flow_action, 645496458233SKamal Heib .destroy_qp = mlx5_ib_destroy_qp, 645596458233SKamal Heib .destroy_srq = mlx5_ib_destroy_srq, 645696458233SKamal Heib .detach_mcast = mlx5_ib_mcg_detach, 645796458233SKamal Heib .disassociate_ucontext = mlx5_ib_disassociate_ucontext, 645896458233SKamal Heib .drain_rq = mlx5_ib_drain_rq, 645996458233SKamal Heib .drain_sq = mlx5_ib_drain_sq, 646011f552e2SMichael Guralnik .enable_driver = mlx5_ib_enable_driver, 6461e1b95ae0SErez Alfasi .fill_res_entry = mlx5_ib_fill_res_entry, 64624061ff7aSErez Alfasi .fill_stat_entry = mlx5_ib_fill_stat_entry, 646396458233SKamal Heib .get_dev_fw_str = get_dev_fw_str, 646496458233SKamal Heib .get_dma_mr = mlx5_ib_get_dma_mr, 646596458233SKamal Heib .get_link_layer = mlx5_ib_port_link_layer, 646696458233SKamal Heib .map_mr_sg = mlx5_ib_map_mr_sg, 64676c984472SMax Gurtovoy .map_mr_sg_pi = mlx5_ib_map_mr_sg_pi, 646896458233SKamal Heib .mmap = mlx5_ib_mmap, 6469dc2316ebSYishai Hadas .mmap_free = mlx5_ib_mmap_free, 647096458233SKamal Heib .modify_cq = mlx5_ib_modify_cq, 647196458233SKamal Heib .modify_device = mlx5_ib_modify_device, 647296458233SKamal Heib .modify_port = mlx5_ib_modify_port, 647396458233SKamal Heib .modify_qp = mlx5_ib_modify_qp, 647496458233SKamal Heib .modify_srq = mlx5_ib_modify_srq, 647596458233SKamal Heib .poll_cq = mlx5_ib_poll_cq, 647696458233SKamal Heib .post_recv = mlx5_ib_post_recv, 647796458233SKamal Heib .post_send = mlx5_ib_post_send, 647896458233SKamal Heib .post_srq_recv = mlx5_ib_post_srq_recv, 647996458233SKamal Heib .process_mad = mlx5_ib_process_mad, 648096458233SKamal Heib .query_ah = mlx5_ib_query_ah, 648196458233SKamal Heib .query_device = mlx5_ib_query_device, 648296458233SKamal Heib .query_gid = mlx5_ib_query_gid, 648396458233SKamal Heib .query_pkey = mlx5_ib_query_pkey, 648496458233SKamal Heib .query_qp = mlx5_ib_query_qp, 648596458233SKamal Heib .query_srq = mlx5_ib_query_srq, 648696458233SKamal Heib .read_counters = mlx5_ib_read_counters, 648796458233SKamal Heib .reg_user_mr = mlx5_ib_reg_user_mr, 648896458233SKamal Heib .req_notify_cq = mlx5_ib_arm_cq, 648996458233SKamal Heib .rereg_user_mr = mlx5_ib_rereg_user_mr, 649096458233SKamal Heib .resize_cq = mlx5_ib_resize_cq, 6491d3456914SLeon Romanovsky 6492d3456914SLeon Romanovsky INIT_RDMA_OBJ_SIZE(ib_ah, mlx5_ib_ah, ibah), 6493e39afe3dSLeon Romanovsky INIT_RDMA_OBJ_SIZE(ib_cq, mlx5_ib_cq, ibcq), 649421a428a0SLeon Romanovsky INIT_RDMA_OBJ_SIZE(ib_pd, mlx5_ib_pd, ibpd), 649568e326deSLeon Romanovsky INIT_RDMA_OBJ_SIZE(ib_srq, mlx5_ib_srq, ibsrq), 6496a2a074efSLeon Romanovsky INIT_RDMA_OBJ_SIZE(ib_ucontext, mlx5_ib_ucontext, ibucontext), 649796458233SKamal Heib }; 649896458233SKamal Heib 649996458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_flow_ipsec_ops = { 650096458233SKamal Heib .create_flow_action_esp = mlx5_ib_create_flow_action_esp, 650196458233SKamal Heib .modify_flow_action_esp = mlx5_ib_modify_flow_action_esp, 650296458233SKamal Heib }; 650396458233SKamal Heib 650496458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_ipoib_enhanced_ops = { 650596458233SKamal Heib .rdma_netdev_get_params = mlx5_ib_rn_get_params, 650696458233SKamal Heib }; 650796458233SKamal Heib 650896458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_sriov_ops = { 650996458233SKamal Heib .get_vf_config = mlx5_ib_get_vf_config, 65109c0015efSDanit Goldberg .get_vf_guid = mlx5_ib_get_vf_guid, 651196458233SKamal Heib .get_vf_stats = mlx5_ib_get_vf_stats, 651296458233SKamal Heib .set_vf_guid = mlx5_ib_set_vf_guid, 651396458233SKamal Heib .set_vf_link_state = mlx5_ib_set_vf_link_state, 651496458233SKamal Heib }; 651596458233SKamal Heib 651696458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_mw_ops = { 651796458233SKamal Heib .alloc_mw = mlx5_ib_alloc_mw, 651896458233SKamal Heib .dealloc_mw = mlx5_ib_dealloc_mw, 651996458233SKamal Heib }; 652096458233SKamal Heib 652196458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_xrc_ops = { 652296458233SKamal Heib .alloc_xrcd = mlx5_ib_alloc_xrcd, 652396458233SKamal Heib .dealloc_xrcd = mlx5_ib_dealloc_xrcd, 652496458233SKamal Heib }; 652596458233SKamal Heib 652696458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_dm_ops = { 652796458233SKamal Heib .alloc_dm = mlx5_ib_alloc_dm, 652896458233SKamal Heib .dealloc_dm = mlx5_ib_dealloc_dm, 652996458233SKamal Heib .reg_dm_mr = mlx5_ib_reg_dm_mr, 653096458233SKamal Heib }; 653196458233SKamal Heib 6532f164be8cSYishai Hadas static int mlx5_ib_init_var_table(struct mlx5_ib_dev *dev) 6533f164be8cSYishai Hadas { 6534f164be8cSYishai Hadas struct mlx5_core_dev *mdev = dev->mdev; 6535f164be8cSYishai Hadas struct mlx5_var_table *var_table = &dev->var_table; 6536f164be8cSYishai Hadas u8 log_doorbell_bar_size; 6537f164be8cSYishai Hadas u8 log_doorbell_stride; 6538f164be8cSYishai Hadas u64 bar_size; 6539f164be8cSYishai Hadas 6540f164be8cSYishai Hadas log_doorbell_bar_size = MLX5_CAP_DEV_VDPA_EMULATION(mdev, 6541f164be8cSYishai Hadas log_doorbell_bar_size); 6542f164be8cSYishai Hadas log_doorbell_stride = MLX5_CAP_DEV_VDPA_EMULATION(mdev, 6543f164be8cSYishai Hadas log_doorbell_stride); 6544f164be8cSYishai Hadas var_table->hw_start_addr = dev->mdev->bar_addr + 6545f164be8cSYishai Hadas MLX5_CAP64_DEV_VDPA_EMULATION(mdev, 6546f164be8cSYishai Hadas doorbell_bar_offset); 6547f164be8cSYishai Hadas bar_size = (1ULL << log_doorbell_bar_size) * 4096; 6548f164be8cSYishai Hadas var_table->stride_size = 1ULL << log_doorbell_stride; 6549f164be8cSYishai Hadas var_table->num_var_hw_entries = bar_size / var_table->stride_size; 6550f164be8cSYishai Hadas mutex_init(&var_table->bitmap_lock); 6551f164be8cSYishai Hadas var_table->bitmap = bitmap_zalloc(var_table->num_var_hw_entries, 6552f164be8cSYishai Hadas GFP_KERNEL); 6553f164be8cSYishai Hadas return (var_table->bitmap) ? 0 : -ENOMEM; 6554f164be8cSYishai Hadas } 6555f164be8cSYishai Hadas 6556f164be8cSYishai Hadas static void mlx5_ib_stage_caps_cleanup(struct mlx5_ib_dev *dev) 6557f164be8cSYishai Hadas { 6558f164be8cSYishai Hadas bitmap_free(dev->var_table.bitmap); 6559f164be8cSYishai Hadas } 6560f164be8cSYishai Hadas 6561fb652d32SMark Bloch static int mlx5_ib_stage_caps_init(struct mlx5_ib_dev *dev) 656216c1975fSMark Bloch { 656316c1975fSMark Bloch struct mlx5_core_dev *mdev = dev->mdev; 656416c1975fSMark Bloch int err; 656516c1975fSMark Bloch 6566e126ba97SEli Cohen dev->ib_dev.uverbs_cmd_mask = 6567e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) | 6568e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) | 6569e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_QUERY_PORT) | 6570e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_ALLOC_PD) | 6571e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) | 657241c450fdSMoni Shoua (1ull << IB_USER_VERBS_CMD_CREATE_AH) | 657341c450fdSMoni Shoua (1ull << IB_USER_VERBS_CMD_DESTROY_AH) | 6574e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_REG_MR) | 657556e11d62SNoa Osherovich (1ull << IB_USER_VERBS_CMD_REREG_MR) | 6576e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_DEREG_MR) | 6577e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) | 6578e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_CREATE_CQ) | 6579e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_RESIZE_CQ) | 6580e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) | 6581e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_CREATE_QP) | 6582e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_MODIFY_QP) | 6583e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_QUERY_QP) | 6584e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_DESTROY_QP) | 6585e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST) | 6586e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_DETACH_MCAST) | 6587e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_CREATE_SRQ) | 6588e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ) | 6589e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_QUERY_SRQ) | 6590e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ) | 6591e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_CREATE_XSRQ) | 6592e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_OPEN_QP); 65931707cb4aSHaggai Eran dev->ib_dev.uverbs_ex_cmd_mask = 6594d4584ddfSMatan Barak (1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE) | 6595d4584ddfSMatan Barak (1ull << IB_USER_VERBS_EX_CMD_CREATE_CQ) | 65967d29f349SBodong Wang (1ull << IB_USER_VERBS_EX_CMD_CREATE_QP) | 6597b0e9df6dSYonatan Cohen (1ull << IB_USER_VERBS_EX_CMD_MODIFY_QP) | 659896458233SKamal Heib (1ull << IB_USER_VERBS_EX_CMD_MODIFY_CQ) | 659996458233SKamal Heib (1ull << IB_USER_VERBS_EX_CMD_CREATE_FLOW) | 660096458233SKamal Heib (1ull << IB_USER_VERBS_EX_CMD_DESTROY_FLOW); 6601e126ba97SEli Cohen 6602f6a8a19bSDenis Drozdov if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads) && 6603f6a8a19bSDenis Drozdov IS_ENABLED(CONFIG_MLX5_CORE_IPOIB)) 660496458233SKamal Heib ib_set_device_ops(&dev->ib_dev, 660596458233SKamal Heib &mlx5_ib_dev_ipoib_enhanced_ops); 66068e959601SNiranjana Vishwanathapura 660796458233SKamal Heib if (mlx5_core_is_pf(mdev)) 660896458233SKamal Heib ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_sriov_ops); 66097c2344c3SMaor Gottlieb 66106e8484c5SMax Gurtovoy dev->umr_fence = mlx5_get_umr_fence(MLX5_CAP_GEN(mdev, umr_fence)); 66116e8484c5SMax Gurtovoy 6612d2370e0aSMatan Barak if (MLX5_CAP_GEN(mdev, imaicl)) { 6613d2370e0aSMatan Barak dev->ib_dev.uverbs_cmd_mask |= 6614d2370e0aSMatan Barak (1ull << IB_USER_VERBS_CMD_ALLOC_MW) | 6615d2370e0aSMatan Barak (1ull << IB_USER_VERBS_CMD_DEALLOC_MW); 661696458233SKamal Heib ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_mw_ops); 6617d2370e0aSMatan Barak } 6618d2370e0aSMatan Barak 6619938fe83cSSaeed Mahameed if (MLX5_CAP_GEN(mdev, xrc)) { 6620e126ba97SEli Cohen dev->ib_dev.uverbs_cmd_mask |= 6621e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_OPEN_XRCD) | 6622e126ba97SEli Cohen (1ull << IB_USER_VERBS_CMD_CLOSE_XRCD); 662396458233SKamal Heib ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_xrc_ops); 6624e126ba97SEli Cohen } 6625e126ba97SEli Cohen 662625c13324SAriel Levkovich if (MLX5_CAP_DEV_MEM(mdev, memic) || 662725c13324SAriel Levkovich MLX5_CAP_GEN_64(dev->mdev, general_obj_types) & 662825c13324SAriel Levkovich MLX5_GENERAL_OBJ_TYPES_CAP_SW_ICM) 662996458233SKamal Heib ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_dm_ops); 663024da0016SAriel Levkovich 6631dfb631a1SJason Gunthorpe if (mlx5_accel_ipsec_device_caps(dev->mdev) & 663296458233SKamal Heib MLX5_ACCEL_IPSEC_CAP_DEVICE) 663396458233SKamal Heib ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_flow_ipsec_ops); 663496458233SKamal Heib ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_ops); 663581e30880SYishai Hadas 663636e235c8SJason Gunthorpe if (IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS)) 663736e235c8SJason Gunthorpe dev->ib_dev.driver_def = mlx5_ib_defs; 663881e30880SYishai Hadas 6639e126ba97SEli Cohen err = init_node_data(dev); 6640e126ba97SEli Cohen if (err) 664116c1975fSMark Bloch return err; 6642e126ba97SEli Cohen 6643c8b89924SMark Bloch if ((MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_ETH) && 6644e7996a9aSJason Gunthorpe (MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) || 6645e7996a9aSJason Gunthorpe MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc))) 6646a560f1d9SMark Bloch mutex_init(&dev->lb.mutex); 6647c8b89924SMark Bloch 6648f164be8cSYishai Hadas if (MLX5_CAP_GEN_64(dev->mdev, general_obj_types) & 6649f164be8cSYishai Hadas MLX5_GENERAL_OBJ_TYPES_CAP_VIRTIO_NET_Q) { 6650f164be8cSYishai Hadas err = mlx5_ib_init_var_table(dev); 6651f164be8cSYishai Hadas if (err) 6652f164be8cSYishai Hadas return err; 6653f164be8cSYishai Hadas } 6654f164be8cSYishai Hadas 665596e2fd73SLeon Romanovsky dev->ib_dev.use_cq_dim = true; 665696e2fd73SLeon Romanovsky 665716c1975fSMark Bloch return 0; 665816c1975fSMark Bloch } 665916c1975fSMark Bloch 666096458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_port_ops = { 666196458233SKamal Heib .get_port_immutable = mlx5_port_immutable, 666296458233SKamal Heib .query_port = mlx5_ib_query_port, 666396458233SKamal Heib }; 666496458233SKamal Heib 66658e6efa3aSMark Bloch static int mlx5_ib_stage_non_default_cb(struct mlx5_ib_dev *dev) 666616c1975fSMark Bloch { 666796458233SKamal Heib ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_port_ops); 66688e6efa3aSMark Bloch return 0; 66698e6efa3aSMark Bloch } 66708e6efa3aSMark Bloch 667196458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_port_rep_ops = { 667296458233SKamal Heib .get_port_immutable = mlx5_port_rep_immutable, 667396458233SKamal Heib .query_port = mlx5_ib_rep_query_port, 667496458233SKamal Heib }; 667596458233SKamal Heib 6676b5a498baSMichael Guralnik static int mlx5_ib_stage_raw_eth_non_default_cb(struct mlx5_ib_dev *dev) 66778e6efa3aSMark Bloch { 667896458233SKamal Heib ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_port_rep_ops); 66798e6efa3aSMark Bloch return 0; 66808e6efa3aSMark Bloch } 66818e6efa3aSMark Bloch 668296458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_common_roce_ops = { 668396458233SKamal Heib .create_rwq_ind_table = mlx5_ib_create_rwq_ind_table, 668496458233SKamal Heib .create_wq = mlx5_ib_create_wq, 668596458233SKamal Heib .destroy_rwq_ind_table = mlx5_ib_destroy_rwq_ind_table, 668696458233SKamal Heib .destroy_wq = mlx5_ib_destroy_wq, 668796458233SKamal Heib .get_netdev = mlx5_ib_get_netdev, 668896458233SKamal Heib .modify_wq = mlx5_ib_modify_wq, 668996458233SKamal Heib }; 669096458233SKamal Heib 6691e3f1ed1fSLeon Romanovsky static int mlx5_ib_stage_common_roce_init(struct mlx5_ib_dev *dev) 66928e6efa3aSMark Bloch { 6693e3f1ed1fSLeon Romanovsky u8 port_num; 66947fd8aefbSDaniel Jurgens 6695c11a226aSMark Bloch dev->ib_dev.uverbs_ex_cmd_mask |= 6696c11a226aSMark Bloch (1ull << IB_USER_VERBS_EX_CMD_CREATE_WQ) | 6697c11a226aSMark Bloch (1ull << IB_USER_VERBS_EX_CMD_MODIFY_WQ) | 6698c11a226aSMark Bloch (1ull << IB_USER_VERBS_EX_CMD_DESTROY_WQ) | 6699c11a226aSMark Bloch (1ull << IB_USER_VERBS_EX_CMD_CREATE_RWQ_IND_TBL) | 6700c11a226aSMark Bloch (1ull << IB_USER_VERBS_EX_CMD_DESTROY_RWQ_IND_TBL); 670196458233SKamal Heib ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_common_roce_ops); 67028e6efa3aSMark Bloch 6703e3f1ed1fSLeon Romanovsky port_num = mlx5_core_native_port_num(dev->mdev) - 1; 6704e3f1ed1fSLeon Romanovsky 670526628e2dSMark Bloch /* Register only for native ports */ 67068e6efa3aSMark Bloch return mlx5_add_netdev_notifier(dev, port_num); 67078e6efa3aSMark Bloch } 67088e6efa3aSMark Bloch 67098e6efa3aSMark Bloch static void mlx5_ib_stage_common_roce_cleanup(struct mlx5_ib_dev *dev) 67108e6efa3aSMark Bloch { 67118e6efa3aSMark Bloch u8 port_num = mlx5_core_native_port_num(dev->mdev) - 1; 67128e6efa3aSMark Bloch 67138e6efa3aSMark Bloch mlx5_remove_netdev_notifier(dev, port_num); 67148e6efa3aSMark Bloch } 67158e6efa3aSMark Bloch 6716b5a498baSMichael Guralnik static int mlx5_ib_stage_raw_eth_roce_init(struct mlx5_ib_dev *dev) 67178e6efa3aSMark Bloch { 67188e6efa3aSMark Bloch struct mlx5_core_dev *mdev = dev->mdev; 67198e6efa3aSMark Bloch enum rdma_link_layer ll; 67208e6efa3aSMark Bloch int port_type_cap; 67218e6efa3aSMark Bloch int err = 0; 67228e6efa3aSMark Bloch 67238e6efa3aSMark Bloch port_type_cap = MLX5_CAP_GEN(mdev, port_type); 67248e6efa3aSMark Bloch ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap); 67258e6efa3aSMark Bloch 67268e6efa3aSMark Bloch if (ll == IB_LINK_LAYER_ETHERNET) 6727e3f1ed1fSLeon Romanovsky err = mlx5_ib_stage_common_roce_init(dev); 67288e6efa3aSMark Bloch 672916c1975fSMark Bloch return err; 6730fc24fc5eSAchiad Shochat } 6731fc24fc5eSAchiad Shochat 6732b5a498baSMichael Guralnik static void mlx5_ib_stage_raw_eth_roce_cleanup(struct mlx5_ib_dev *dev) 67338e6efa3aSMark Bloch { 67348e6efa3aSMark Bloch mlx5_ib_stage_common_roce_cleanup(dev); 67358e6efa3aSMark Bloch } 67368e6efa3aSMark Bloch 67378e6efa3aSMark Bloch static int mlx5_ib_stage_roce_init(struct mlx5_ib_dev *dev) 67388e6efa3aSMark Bloch { 67398e6efa3aSMark Bloch struct mlx5_core_dev *mdev = dev->mdev; 67408e6efa3aSMark Bloch enum rdma_link_layer ll; 67418e6efa3aSMark Bloch int port_type_cap; 67428e6efa3aSMark Bloch int err; 67438e6efa3aSMark Bloch 67448e6efa3aSMark Bloch port_type_cap = MLX5_CAP_GEN(mdev, port_type); 67458e6efa3aSMark Bloch ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap); 67468e6efa3aSMark Bloch 67478e6efa3aSMark Bloch if (ll == IB_LINK_LAYER_ETHERNET) { 6748e3f1ed1fSLeon Romanovsky err = mlx5_ib_stage_common_roce_init(dev); 67498e6efa3aSMark Bloch if (err) 67508e6efa3aSMark Bloch return err; 67518e6efa3aSMark Bloch 6752e3f1ed1fSLeon Romanovsky err = mlx5_enable_eth(dev); 67538e6efa3aSMark Bloch if (err) 67548e6efa3aSMark Bloch goto cleanup; 67558e6efa3aSMark Bloch } 67568e6efa3aSMark Bloch 675716c1975fSMark Bloch return 0; 67588e6efa3aSMark Bloch cleanup: 67598e6efa3aSMark Bloch mlx5_ib_stage_common_roce_cleanup(dev); 67608e6efa3aSMark Bloch 67618e6efa3aSMark Bloch return err; 676245bded2cSKamal Heib } 67636aec21f6SHaggai Eran 676416c1975fSMark Bloch static void mlx5_ib_stage_roce_cleanup(struct mlx5_ib_dev *dev) 676516c1975fSMark Bloch { 676616c1975fSMark Bloch struct mlx5_core_dev *mdev = dev->mdev; 676716c1975fSMark Bloch enum rdma_link_layer ll; 676816c1975fSMark Bloch int port_type_cap; 67694a2da0b8SParav Pandit 677016c1975fSMark Bloch port_type_cap = MLX5_CAP_GEN(mdev, port_type); 677116c1975fSMark Bloch ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap); 677216c1975fSMark Bloch 677316c1975fSMark Bloch if (ll == IB_LINK_LAYER_ETHERNET) { 677416c1975fSMark Bloch mlx5_disable_eth(dev); 67758e6efa3aSMark Bloch mlx5_ib_stage_common_roce_cleanup(dev); 677616c1975fSMark Bloch } 677716c1975fSMark Bloch } 677816c1975fSMark Bloch 6779fb652d32SMark Bloch static int mlx5_ib_stage_dev_res_init(struct mlx5_ib_dev *dev) 678016c1975fSMark Bloch { 678116c1975fSMark Bloch return create_dev_resources(&dev->devr); 678216c1975fSMark Bloch } 678316c1975fSMark Bloch 6784fb652d32SMark Bloch static void mlx5_ib_stage_dev_res_cleanup(struct mlx5_ib_dev *dev) 678516c1975fSMark Bloch { 678616c1975fSMark Bloch destroy_dev_resources(&dev->devr); 678716c1975fSMark Bloch } 678816c1975fSMark Bloch 678916c1975fSMark Bloch static int mlx5_ib_stage_odp_init(struct mlx5_ib_dev *dev) 679016c1975fSMark Bloch { 679116c1975fSMark Bloch return mlx5_ib_odp_init_one(dev); 679216c1975fSMark Bloch } 679316c1975fSMark Bloch 6794f3ffed0cSKamal Heib static void mlx5_ib_stage_odp_cleanup(struct mlx5_ib_dev *dev) 6795d5d284b8SSaeed Mahameed { 6796d5d284b8SSaeed Mahameed mlx5_ib_odp_cleanup_one(dev); 6797d5d284b8SSaeed Mahameed } 6798d5d284b8SSaeed Mahameed 679996458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_hw_stats_ops = { 680096458233SKamal Heib .alloc_hw_stats = mlx5_ib_alloc_hw_stats, 680196458233SKamal Heib .get_hw_stats = mlx5_ib_get_hw_stats, 680245842fc6SMark Zhang .counter_bind_qp = mlx5_ib_counter_bind_qp, 680345842fc6SMark Zhang .counter_unbind_qp = mlx5_ib_counter_unbind_qp, 680445842fc6SMark Zhang .counter_dealloc = mlx5_ib_counter_dealloc, 680518d422ceSMark Zhang .counter_alloc_stats = mlx5_ib_counter_alloc_stats, 680618d422ceSMark Zhang .counter_update_stats = mlx5_ib_counter_update_stats, 680796458233SKamal Heib }; 680896458233SKamal Heib 6809fb652d32SMark Bloch static int mlx5_ib_stage_counters_init(struct mlx5_ib_dev *dev) 681016c1975fSMark Bloch { 68115e1e7612SMark Bloch if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt)) { 681296458233SKamal Heib ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_hw_stats_ops); 68135e1e7612SMark Bloch 681416c1975fSMark Bloch return mlx5_ib_alloc_counters(dev); 68155e1e7612SMark Bloch } 681616c1975fSMark Bloch 681716c1975fSMark Bloch return 0; 681816c1975fSMark Bloch } 681916c1975fSMark Bloch 6820fb652d32SMark Bloch static void mlx5_ib_stage_counters_cleanup(struct mlx5_ib_dev *dev) 682116c1975fSMark Bloch { 682216c1975fSMark Bloch if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt)) 682316c1975fSMark Bloch mlx5_ib_dealloc_counters(dev); 682416c1975fSMark Bloch } 682516c1975fSMark Bloch 682616c1975fSMark Bloch static int mlx5_ib_stage_cong_debugfs_init(struct mlx5_ib_dev *dev) 682716c1975fSMark Bloch { 682873eb8f03SGreg Kroah-Hartman mlx5_ib_init_cong_debugfs(dev, 6829a9e546e7SParav Pandit mlx5_core_native_port_num(dev->mdev) - 1); 683073eb8f03SGreg Kroah-Hartman return 0; 683116c1975fSMark Bloch } 683216c1975fSMark Bloch 683316c1975fSMark Bloch static void mlx5_ib_stage_cong_debugfs_cleanup(struct mlx5_ib_dev *dev) 683416c1975fSMark Bloch { 6835a9e546e7SParav Pandit mlx5_ib_cleanup_cong_debugfs(dev, 6836a9e546e7SParav Pandit mlx5_core_native_port_num(dev->mdev) - 1); 683716c1975fSMark Bloch } 683816c1975fSMark Bloch 683916c1975fSMark Bloch static int mlx5_ib_stage_uar_init(struct mlx5_ib_dev *dev) 684016c1975fSMark Bloch { 68415fe9dec0SEli Cohen dev->mdev->priv.uar = mlx5_get_uars_page(dev->mdev); 6842444261caSLeon Romanovsky return PTR_ERR_OR_ZERO(dev->mdev->priv.uar); 684316c1975fSMark Bloch } 684416c1975fSMark Bloch 684516c1975fSMark Bloch static void mlx5_ib_stage_uar_cleanup(struct mlx5_ib_dev *dev) 684616c1975fSMark Bloch { 684716c1975fSMark Bloch mlx5_put_uars_page(dev->mdev, dev->mdev->priv.uar); 684816c1975fSMark Bloch } 684916c1975fSMark Bloch 6850fb652d32SMark Bloch static int mlx5_ib_stage_bfrag_init(struct mlx5_ib_dev *dev) 685116c1975fSMark Bloch { 685216c1975fSMark Bloch int err; 68535fe9dec0SEli Cohen 68545fe9dec0SEli Cohen err = mlx5_alloc_bfreg(dev->mdev, &dev->bfreg, false, false); 68555fe9dec0SEli Cohen if (err) 685616c1975fSMark Bloch return err; 68575fe9dec0SEli Cohen 68585fe9dec0SEli Cohen err = mlx5_alloc_bfreg(dev->mdev, &dev->fp_bfreg, false, true); 68595fe9dec0SEli Cohen if (err) 686016c1975fSMark Bloch mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg); 68615fe9dec0SEli Cohen 686216c1975fSMark Bloch return err; 686316c1975fSMark Bloch } 68640837e86aSMark Bloch 6865fb652d32SMark Bloch static void mlx5_ib_stage_bfrag_cleanup(struct mlx5_ib_dev *dev) 686616c1975fSMark Bloch { 686716c1975fSMark Bloch mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg); 686816c1975fSMark Bloch mlx5_free_bfreg(dev->mdev, &dev->bfreg); 686916c1975fSMark Bloch } 6870e126ba97SEli Cohen 6871fb652d32SMark Bloch static int mlx5_ib_stage_ib_reg_init(struct mlx5_ib_dev *dev) 687216c1975fSMark Bloch { 6873e349f858SJason Gunthorpe const char *name; 6874e349f858SJason Gunthorpe 6875508a523fSParav Pandit rdma_set_device_sysfs_group(&dev->ib_dev, &mlx5_attr_group); 68767c34ec19SAviv Heller if (!mlx5_lag_is_roce(dev->mdev)) 6877e349f858SJason Gunthorpe name = "mlx5_%d"; 6878e349f858SJason Gunthorpe else 6879e349f858SJason Gunthorpe name = "mlx5_bond_%d"; 6880ea4baf7fSParav Pandit return ib_register_device(&dev->ib_dev, name); 688116c1975fSMark Bloch } 688216c1975fSMark Bloch 6883fb652d32SMark Bloch static void mlx5_ib_stage_pre_ib_reg_umr_cleanup(struct mlx5_ib_dev *dev) 688442cea83fSMark Bloch { 688542cea83fSMark Bloch destroy_umrc_res(dev); 688642cea83fSMark Bloch } 688742cea83fSMark Bloch 6888fb652d32SMark Bloch static void mlx5_ib_stage_ib_reg_cleanup(struct mlx5_ib_dev *dev) 688916c1975fSMark Bloch { 689016c1975fSMark Bloch ib_unregister_device(&dev->ib_dev); 689116c1975fSMark Bloch } 689216c1975fSMark Bloch 6893fb652d32SMark Bloch static int mlx5_ib_stage_post_ib_reg_umr_init(struct mlx5_ib_dev *dev) 689416c1975fSMark Bloch { 689516c1975fSMark Bloch return create_umr_res(dev); 689616c1975fSMark Bloch } 689716c1975fSMark Bloch 689816c1975fSMark Bloch static int mlx5_ib_stage_delay_drop_init(struct mlx5_ib_dev *dev) 689916c1975fSMark Bloch { 690003404e8aSMaor Gottlieb init_delay_drop(dev); 690103404e8aSMaor Gottlieb 690216c1975fSMark Bloch return 0; 690316c1975fSMark Bloch } 690416c1975fSMark Bloch 690516c1975fSMark Bloch static void mlx5_ib_stage_delay_drop_cleanup(struct mlx5_ib_dev *dev) 690616c1975fSMark Bloch { 690716c1975fSMark Bloch cancel_delay_drop(dev); 690816c1975fSMark Bloch } 690916c1975fSMark Bloch 6910df097a27SSaeed Mahameed static int mlx5_ib_stage_dev_notifier_init(struct mlx5_ib_dev *dev) 6911df097a27SSaeed Mahameed { 6912df097a27SSaeed Mahameed dev->mdev_events.notifier_call = mlx5_ib_event; 6913df097a27SSaeed Mahameed mlx5_notifier_register(dev->mdev, &dev->mdev_events); 6914df097a27SSaeed Mahameed return 0; 6915df097a27SSaeed Mahameed } 6916df097a27SSaeed Mahameed 6917df097a27SSaeed Mahameed static void mlx5_ib_stage_dev_notifier_cleanup(struct mlx5_ib_dev *dev) 6918df097a27SSaeed Mahameed { 6919df097a27SSaeed Mahameed mlx5_notifier_unregister(dev->mdev, &dev->mdev_events); 6920df097a27SSaeed Mahameed } 6921df097a27SSaeed Mahameed 692281773ce5SLeon Romanovsky static int mlx5_ib_stage_devx_init(struct mlx5_ib_dev *dev) 692381773ce5SLeon Romanovsky { 692481773ce5SLeon Romanovsky int uid; 692581773ce5SLeon Romanovsky 6926fb98153bSYishai Hadas uid = mlx5_ib_devx_create(dev, false); 6927e337dd53SYishai Hadas if (uid > 0) { 692881773ce5SLeon Romanovsky dev->devx_whitelist_uid = uid; 6929e337dd53SYishai Hadas mlx5_ib_devx_init_event_table(dev); 6930e337dd53SYishai Hadas } 693181773ce5SLeon Romanovsky 693281773ce5SLeon Romanovsky return 0; 693381773ce5SLeon Romanovsky } 693481773ce5SLeon Romanovsky static void mlx5_ib_stage_devx_cleanup(struct mlx5_ib_dev *dev) 693581773ce5SLeon Romanovsky { 6936e337dd53SYishai Hadas if (dev->devx_whitelist_uid) { 6937e337dd53SYishai Hadas mlx5_ib_devx_cleanup_event_table(dev); 693881773ce5SLeon Romanovsky mlx5_ib_devx_destroy(dev, dev->devx_whitelist_uid); 693981773ce5SLeon Romanovsky } 6940e337dd53SYishai Hadas } 694181773ce5SLeon Romanovsky 694211f552e2SMichael Guralnik int mlx5_ib_enable_driver(struct ib_device *dev) 694311f552e2SMichael Guralnik { 694411f552e2SMichael Guralnik struct mlx5_ib_dev *mdev = to_mdev(dev); 694511f552e2SMichael Guralnik int ret; 694611f552e2SMichael Guralnik 694711f552e2SMichael Guralnik ret = mlx5_ib_test_wc(mdev); 694811f552e2SMichael Guralnik mlx5_ib_dbg(mdev, "Write-Combining %s", 694911f552e2SMichael Guralnik mdev->wc_support ? "supported" : "not supported"); 695011f552e2SMichael Guralnik 695111f552e2SMichael Guralnik return ret; 695211f552e2SMichael Guralnik } 695311f552e2SMichael Guralnik 6954b5ca15adSMark Bloch void __mlx5_ib_remove(struct mlx5_ib_dev *dev, 695516c1975fSMark Bloch const struct mlx5_ib_profile *profile, 695616c1975fSMark Bloch int stage) 695716c1975fSMark Bloch { 69584cca96a8SParav Pandit dev->ib_active = false; 69594cca96a8SParav Pandit 696016c1975fSMark Bloch /* Number of stages to cleanup */ 696116c1975fSMark Bloch while (stage) { 696216c1975fSMark Bloch stage--; 696316c1975fSMark Bloch if (profile->stage[stage].cleanup) 696416c1975fSMark Bloch profile->stage[stage].cleanup(dev); 696516c1975fSMark Bloch } 69664a6dc855SMark Bloch 6967da796ccbSMark Bloch kfree(dev->port); 69684a6dc855SMark Bloch ib_dealloc_device(&dev->ib_dev); 696916c1975fSMark Bloch } 697016c1975fSMark Bloch 6971b5ca15adSMark Bloch void *__mlx5_ib_add(struct mlx5_ib_dev *dev, 697216c1975fSMark Bloch const struct mlx5_ib_profile *profile) 697316c1975fSMark Bloch { 697416c1975fSMark Bloch int err; 697516c1975fSMark Bloch int i; 697616c1975fSMark Bloch 697716c1975fSMark Bloch for (i = 0; i < MLX5_IB_STAGE_MAX; i++) { 697816c1975fSMark Bloch if (profile->stage[i].init) { 697916c1975fSMark Bloch err = profile->stage[i].init(dev); 698016c1975fSMark Bloch if (err) 698116c1975fSMark Bloch goto err_out; 698216c1975fSMark Bloch } 698316c1975fSMark Bloch } 698416c1975fSMark Bloch 698516c1975fSMark Bloch dev->profile = profile; 6986e126ba97SEli Cohen dev->ib_active = true; 6987e126ba97SEli Cohen 69889603b61dSJack Morgenstein return dev; 6989e126ba97SEli Cohen 699016c1975fSMark Bloch err_out: 699116c1975fSMark Bloch __mlx5_ib_remove(dev, profile, i); 6992e126ba97SEli Cohen 69939603b61dSJack Morgenstein return NULL; 6994e126ba97SEli Cohen } 6995e126ba97SEli Cohen 699616c1975fSMark Bloch static const struct mlx5_ib_profile pf_profile = { 699716c1975fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_INIT, 699816c1975fSMark Bloch mlx5_ib_stage_init_init, 699916c1975fSMark Bloch mlx5_ib_stage_init_cleanup), 70009a4ca38dSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_FLOW_DB, 70019a4ca38dSMark Bloch mlx5_ib_stage_flow_db_init, 70029a4ca38dSMark Bloch mlx5_ib_stage_flow_db_cleanup), 700316c1975fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_CAPS, 700416c1975fSMark Bloch mlx5_ib_stage_caps_init, 7005f164be8cSYishai Hadas mlx5_ib_stage_caps_cleanup), 70068e6efa3aSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB, 70078e6efa3aSMark Bloch mlx5_ib_stage_non_default_cb, 70088e6efa3aSMark Bloch NULL), 700916c1975fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_ROCE, 701016c1975fSMark Bloch mlx5_ib_stage_roce_init, 701116c1975fSMark Bloch mlx5_ib_stage_roce_cleanup), 7012f3da6577SLeon Romanovsky STAGE_CREATE(MLX5_IB_STAGE_SRQ, 7013f3da6577SLeon Romanovsky mlx5_init_srq_table, 7014f3da6577SLeon Romanovsky mlx5_cleanup_srq_table), 701516c1975fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES, 701616c1975fSMark Bloch mlx5_ib_stage_dev_res_init, 701716c1975fSMark Bloch mlx5_ib_stage_dev_res_cleanup), 7018df097a27SSaeed Mahameed STAGE_CREATE(MLX5_IB_STAGE_DEVICE_NOTIFIER, 7019df097a27SSaeed Mahameed mlx5_ib_stage_dev_notifier_init, 7020df097a27SSaeed Mahameed mlx5_ib_stage_dev_notifier_cleanup), 702116c1975fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_ODP, 702216c1975fSMark Bloch mlx5_ib_stage_odp_init, 7023d5d284b8SSaeed Mahameed mlx5_ib_stage_odp_cleanup), 702416c1975fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_COUNTERS, 702516c1975fSMark Bloch mlx5_ib_stage_counters_init, 702616c1975fSMark Bloch mlx5_ib_stage_counters_cleanup), 702716c1975fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_CONG_DEBUGFS, 702816c1975fSMark Bloch mlx5_ib_stage_cong_debugfs_init, 702916c1975fSMark Bloch mlx5_ib_stage_cong_debugfs_cleanup), 703016c1975fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_UAR, 703116c1975fSMark Bloch mlx5_ib_stage_uar_init, 703216c1975fSMark Bloch mlx5_ib_stage_uar_cleanup), 703316c1975fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_BFREG, 703416c1975fSMark Bloch mlx5_ib_stage_bfrag_init, 703516c1975fSMark Bloch mlx5_ib_stage_bfrag_cleanup), 703642cea83fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR, 703742cea83fSMark Bloch NULL, 703842cea83fSMark Bloch mlx5_ib_stage_pre_ib_reg_umr_cleanup), 703981773ce5SLeon Romanovsky STAGE_CREATE(MLX5_IB_STAGE_WHITELIST_UID, 704081773ce5SLeon Romanovsky mlx5_ib_stage_devx_init, 704181773ce5SLeon Romanovsky mlx5_ib_stage_devx_cleanup), 704216c1975fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_IB_REG, 704316c1975fSMark Bloch mlx5_ib_stage_ib_reg_init, 704416c1975fSMark Bloch mlx5_ib_stage_ib_reg_cleanup), 704542cea83fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR, 704642cea83fSMark Bloch mlx5_ib_stage_post_ib_reg_umr_init, 704742cea83fSMark Bloch NULL), 704816c1975fSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_DELAY_DROP, 704916c1975fSMark Bloch mlx5_ib_stage_delay_drop_init, 705016c1975fSMark Bloch mlx5_ib_stage_delay_drop_cleanup), 705116c1975fSMark Bloch }; 705216c1975fSMark Bloch 7053b5a498baSMichael Guralnik const struct mlx5_ib_profile raw_eth_profile = { 7054b5ca15adSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_INIT, 7055b5ca15adSMark Bloch mlx5_ib_stage_init_init, 7056b5ca15adSMark Bloch mlx5_ib_stage_init_cleanup), 7057b5ca15adSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_FLOW_DB, 7058b5ca15adSMark Bloch mlx5_ib_stage_flow_db_init, 7059b5ca15adSMark Bloch mlx5_ib_stage_flow_db_cleanup), 7060b5ca15adSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_CAPS, 7061b5ca15adSMark Bloch mlx5_ib_stage_caps_init, 7062f164be8cSYishai Hadas mlx5_ib_stage_caps_cleanup), 7063b5ca15adSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB, 7064b5a498baSMichael Guralnik mlx5_ib_stage_raw_eth_non_default_cb, 7065b5ca15adSMark Bloch NULL), 7066b5ca15adSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_ROCE, 7067b5a498baSMichael Guralnik mlx5_ib_stage_raw_eth_roce_init, 7068b5a498baSMichael Guralnik mlx5_ib_stage_raw_eth_roce_cleanup), 7069f3da6577SLeon Romanovsky STAGE_CREATE(MLX5_IB_STAGE_SRQ, 7070f3da6577SLeon Romanovsky mlx5_init_srq_table, 7071f3da6577SLeon Romanovsky mlx5_cleanup_srq_table), 7072b5ca15adSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES, 7073b5ca15adSMark Bloch mlx5_ib_stage_dev_res_init, 7074b5ca15adSMark Bloch mlx5_ib_stage_dev_res_cleanup), 7075df097a27SSaeed Mahameed STAGE_CREATE(MLX5_IB_STAGE_DEVICE_NOTIFIER, 7076df097a27SSaeed Mahameed mlx5_ib_stage_dev_notifier_init, 7077df097a27SSaeed Mahameed mlx5_ib_stage_dev_notifier_cleanup), 7078b5ca15adSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_COUNTERS, 7079b5ca15adSMark Bloch mlx5_ib_stage_counters_init, 7080b5ca15adSMark Bloch mlx5_ib_stage_counters_cleanup), 7081b5ca15adSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_UAR, 7082b5ca15adSMark Bloch mlx5_ib_stage_uar_init, 7083b5ca15adSMark Bloch mlx5_ib_stage_uar_cleanup), 7084b5ca15adSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_BFREG, 7085b5ca15adSMark Bloch mlx5_ib_stage_bfrag_init, 7086b5ca15adSMark Bloch mlx5_ib_stage_bfrag_cleanup), 708703fe2debSDavid S. Miller STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR, 708803fe2debSDavid S. Miller NULL, 708903fe2debSDavid S. Miller mlx5_ib_stage_pre_ib_reg_umr_cleanup), 70907f575103SMark Bloch STAGE_CREATE(MLX5_IB_STAGE_WHITELIST_UID, 70917f575103SMark Bloch mlx5_ib_stage_devx_init, 70927f575103SMark Bloch mlx5_ib_stage_devx_cleanup), 7093b5ca15adSMark Bloch STAGE_CREATE(MLX5_IB_STAGE_IB_REG, 7094b5ca15adSMark Bloch mlx5_ib_stage_ib_reg_init, 7095b5ca15adSMark Bloch mlx5_ib_stage_ib_reg_cleanup), 709603fe2debSDavid S. Miller STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR, 709703fe2debSDavid S. Miller mlx5_ib_stage_post_ib_reg_umr_init, 709803fe2debSDavid S. Miller NULL), 7099b5ca15adSMark Bloch }; 7100b5ca15adSMark Bloch 7101e3f1ed1fSLeon Romanovsky static void *mlx5_ib_add_slave_port(struct mlx5_core_dev *mdev) 710232f69e4bSDaniel Jurgens { 710332f69e4bSDaniel Jurgens struct mlx5_ib_multiport_info *mpi; 710432f69e4bSDaniel Jurgens struct mlx5_ib_dev *dev; 710532f69e4bSDaniel Jurgens bool bound = false; 710632f69e4bSDaniel Jurgens int err; 710732f69e4bSDaniel Jurgens 710832f69e4bSDaniel Jurgens mpi = kzalloc(sizeof(*mpi), GFP_KERNEL); 710932f69e4bSDaniel Jurgens if (!mpi) 711032f69e4bSDaniel Jurgens return NULL; 711132f69e4bSDaniel Jurgens 711232f69e4bSDaniel Jurgens mpi->mdev = mdev; 711332f69e4bSDaniel Jurgens 711432f69e4bSDaniel Jurgens err = mlx5_query_nic_vport_system_image_guid(mdev, 711532f69e4bSDaniel Jurgens &mpi->sys_image_guid); 711632f69e4bSDaniel Jurgens if (err) { 711732f69e4bSDaniel Jurgens kfree(mpi); 711832f69e4bSDaniel Jurgens return NULL; 711932f69e4bSDaniel Jurgens } 712032f69e4bSDaniel Jurgens 712132f69e4bSDaniel Jurgens mutex_lock(&mlx5_ib_multiport_mutex); 712232f69e4bSDaniel Jurgens list_for_each_entry(dev, &mlx5_ib_dev_list, ib_dev_list) { 712332f69e4bSDaniel Jurgens if (dev->sys_image_guid == mpi->sys_image_guid) 712432f69e4bSDaniel Jurgens bound = mlx5_ib_bind_slave_port(dev, mpi); 712532f69e4bSDaniel Jurgens 712632f69e4bSDaniel Jurgens if (bound) { 712732f69e4bSDaniel Jurgens rdma_roce_rescan_device(&dev->ib_dev); 712832f69e4bSDaniel Jurgens break; 712932f69e4bSDaniel Jurgens } 713032f69e4bSDaniel Jurgens } 713132f69e4bSDaniel Jurgens 713232f69e4bSDaniel Jurgens if (!bound) { 713332f69e4bSDaniel Jurgens list_add_tail(&mpi->list, &mlx5_ib_unaffiliated_port_list); 7134c42260f1SVu Pham dev_dbg(mdev->device, 7135c42260f1SVu Pham "no suitable IB device found to bind to, added to unaffiliated list.\n"); 713632f69e4bSDaniel Jurgens } 713732f69e4bSDaniel Jurgens mutex_unlock(&mlx5_ib_multiport_mutex); 713832f69e4bSDaniel Jurgens 713932f69e4bSDaniel Jurgens return mpi; 714032f69e4bSDaniel Jurgens } 714132f69e4bSDaniel Jurgens 714216c1975fSMark Bloch static void *mlx5_ib_add(struct mlx5_core_dev *mdev) 714316c1975fSMark Bloch { 714494de879cSMichael Guralnik const struct mlx5_ib_profile *profile; 714532f69e4bSDaniel Jurgens enum rdma_link_layer ll; 7146b5ca15adSMark Bloch struct mlx5_ib_dev *dev; 714732f69e4bSDaniel Jurgens int port_type_cap; 7148da796ccbSMark Bloch int num_ports; 714932f69e4bSDaniel Jurgens 7150b5ca15adSMark Bloch printk_once(KERN_INFO "%s", mlx5_version); 7151b5ca15adSMark Bloch 7152f0666f1fSBodong Wang if (MLX5_ESWITCH_MANAGER(mdev) && 7153f6455de0SBodong Wang mlx5_ib_eswitch_mode(mdev->priv.eswitch) == MLX5_ESWITCH_OFFLOADS) { 71545fb58c9eSMark Bloch if (!mlx5_core_mp_enabled(mdev)) 7155f0666f1fSBodong Wang mlx5_ib_register_vport_reps(mdev); 7156f0666f1fSBodong Wang return mdev; 7157f0666f1fSBodong Wang } 7158f0666f1fSBodong Wang 715932f69e4bSDaniel Jurgens port_type_cap = MLX5_CAP_GEN(mdev, port_type); 716032f69e4bSDaniel Jurgens ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap); 716132f69e4bSDaniel Jurgens 7162e3f1ed1fSLeon Romanovsky if (mlx5_core_is_mp_slave(mdev) && ll == IB_LINK_LAYER_ETHERNET) 7163e3f1ed1fSLeon Romanovsky return mlx5_ib_add_slave_port(mdev); 716432f69e4bSDaniel Jurgens 7165da796ccbSMark Bloch num_ports = max(MLX5_CAP_GEN(mdev, num_ports), 7166da796ccbSMark Bloch MLX5_CAP_GEN(mdev, num_vhca_ports)); 7167459cc69fSLeon Romanovsky dev = ib_alloc_device(mlx5_ib_dev, ib_dev); 7168b5ca15adSMark Bloch if (!dev) 7169b5ca15adSMark Bloch return NULL; 7170da796ccbSMark Bloch dev->port = kcalloc(num_ports, sizeof(*dev->port), 7171da796ccbSMark Bloch GFP_KERNEL); 7172da796ccbSMark Bloch if (!dev->port) { 7173a5c9c299SParav Pandit ib_dealloc_device(&dev->ib_dev); 7174da796ccbSMark Bloch return NULL; 7175da796ccbSMark Bloch } 7176b5ca15adSMark Bloch 7177b5ca15adSMark Bloch dev->mdev = mdev; 7178da796ccbSMark Bloch dev->num_ports = num_ports; 7179b5ca15adSMark Bloch 718094de879cSMichael Guralnik if (ll == IB_LINK_LAYER_ETHERNET && !mlx5_is_roce_enabled(mdev)) 718194de879cSMichael Guralnik profile = &raw_eth_profile; 718294de879cSMichael Guralnik else 718394de879cSMichael Guralnik profile = &pf_profile; 718494de879cSMichael Guralnik 718594de879cSMichael Guralnik return __mlx5_ib_add(dev, profile); 718616c1975fSMark Bloch } 718716c1975fSMark Bloch 71889603b61dSJack Morgenstein static void mlx5_ib_remove(struct mlx5_core_dev *mdev, void *context) 7189e126ba97SEli Cohen { 719032f69e4bSDaniel Jurgens struct mlx5_ib_multiport_info *mpi; 719132f69e4bSDaniel Jurgens struct mlx5_ib_dev *dev; 71926aec21f6SHaggai Eran 7193f0666f1fSBodong Wang if (MLX5_ESWITCH_MANAGER(mdev) && context == mdev) { 7194f0666f1fSBodong Wang mlx5_ib_unregister_vport_reps(mdev); 7195f0666f1fSBodong Wang return; 7196f0666f1fSBodong Wang } 7197f0666f1fSBodong Wang 719832f69e4bSDaniel Jurgens if (mlx5_core_is_mp_slave(mdev)) { 719932f69e4bSDaniel Jurgens mpi = context; 720032f69e4bSDaniel Jurgens mutex_lock(&mlx5_ib_multiport_mutex); 720132f69e4bSDaniel Jurgens if (mpi->ibdev) 720232f69e4bSDaniel Jurgens mlx5_ib_unbind_slave_port(mpi->ibdev, mpi); 720332f69e4bSDaniel Jurgens list_del(&mpi->list); 720432f69e4bSDaniel Jurgens mutex_unlock(&mlx5_ib_multiport_mutex); 72055d44adebSDanit Goldberg kfree(mpi); 720632f69e4bSDaniel Jurgens return; 720732f69e4bSDaniel Jurgens } 720832f69e4bSDaniel Jurgens 720932f69e4bSDaniel Jurgens dev = context; 721016c1975fSMark Bloch __mlx5_ib_remove(dev, dev->profile, MLX5_IB_STAGE_MAX); 7211e126ba97SEli Cohen } 7212e126ba97SEli Cohen 72139603b61dSJack Morgenstein static struct mlx5_interface mlx5_ib_interface = { 72149603b61dSJack Morgenstein .add = mlx5_ib_add, 72159603b61dSJack Morgenstein .remove = mlx5_ib_remove, 721664613d94SSaeed Mahameed .protocol = MLX5_INTERFACE_PROTOCOL_IB, 7217e126ba97SEli Cohen }; 7218e126ba97SEli Cohen 7219c44ef998SIlya Lesokhin unsigned long mlx5_ib_get_xlt_emergency_page(void) 7220c44ef998SIlya Lesokhin { 7221c44ef998SIlya Lesokhin mutex_lock(&xlt_emergency_page_mutex); 7222c44ef998SIlya Lesokhin return xlt_emergency_page; 7223c44ef998SIlya Lesokhin } 7224c44ef998SIlya Lesokhin 7225c44ef998SIlya Lesokhin void mlx5_ib_put_xlt_emergency_page(void) 7226c44ef998SIlya Lesokhin { 7227c44ef998SIlya Lesokhin mutex_unlock(&xlt_emergency_page_mutex); 7228c44ef998SIlya Lesokhin } 7229c44ef998SIlya Lesokhin 7230e126ba97SEli Cohen static int __init mlx5_ib_init(void) 7231e126ba97SEli Cohen { 72326aec21f6SHaggai Eran int err; 72336aec21f6SHaggai Eran 7234c44ef998SIlya Lesokhin xlt_emergency_page = __get_free_page(GFP_KERNEL); 7235c44ef998SIlya Lesokhin if (!xlt_emergency_page) 7236d69a24e0SDaniel Jurgens return -ENOMEM; 7237d69a24e0SDaniel Jurgens 7238c44ef998SIlya Lesokhin mutex_init(&xlt_emergency_page_mutex); 7239c44ef998SIlya Lesokhin 7240c44ef998SIlya Lesokhin mlx5_ib_event_wq = alloc_ordered_workqueue("mlx5_ib_event_wq", 0); 7241c44ef998SIlya Lesokhin if (!mlx5_ib_event_wq) { 7242c44ef998SIlya Lesokhin free_page(xlt_emergency_page); 7243c44ef998SIlya Lesokhin return -ENOMEM; 7244c44ef998SIlya Lesokhin } 7245c44ef998SIlya Lesokhin 724681713d37SArtemy Kovalyov mlx5_ib_odp_init(); 72479603b61dSJack Morgenstein 72486aec21f6SHaggai Eran err = mlx5_register_interface(&mlx5_ib_interface); 72496aec21f6SHaggai Eran 72506aec21f6SHaggai Eran return err; 7251e126ba97SEli Cohen } 7252e126ba97SEli Cohen 7253e126ba97SEli Cohen static void __exit mlx5_ib_cleanup(void) 7254e126ba97SEli Cohen { 72559603b61dSJack Morgenstein mlx5_unregister_interface(&mlx5_ib_interface); 7256d69a24e0SDaniel Jurgens destroy_workqueue(mlx5_ib_event_wq); 7257c44ef998SIlya Lesokhin mutex_destroy(&xlt_emergency_page_mutex); 7258c44ef998SIlya Lesokhin free_page(xlt_emergency_page); 7259e126ba97SEli Cohen } 7260e126ba97SEli Cohen 7261e126ba97SEli Cohen module_init(mlx5_ib_init); 7262e126ba97SEli Cohen module_exit(mlx5_ib_cleanup); 7263