blob: 97bcf01960aeec4c1cfb8d1c57299a146edef92d [file] [log] [blame]
Eli Cohene126ba92013-07-07 17:25:49 +03001/*
Saeed Mahameed6cf0a152015-04-02 17:07:30 +03002 * Copyright (c) 2013-2015, Mellanox Technologies. All rights reserved.
Eli Cohene126ba92013-07-07 17:25:49 +03003 *
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
9 *
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
13 *
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
17 *
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE.
31 */
32
Maor Gottliebfe248c32017-05-30 10:29:14 +030033#include <linux/debugfs.h>
Christoph Hellwigadec6402015-08-28 09:27:19 +020034#include <linux/highmem.h>
Eli Cohene126ba92013-07-07 17:25:49 +030035#include <linux/module.h>
36#include <linux/init.h>
37#include <linux/errno.h>
38#include <linux/pci.h>
39#include <linux/dma-mapping.h>
40#include <linux/slab.h>
Ariel Levkovich24da0012018-04-05 18:53:27 +030041#include <linux/bitmap.h>
Guy Levi37aa5c32016-04-27 16:49:50 +030042#if defined(CONFIG_X86)
43#include <asm/pat.h>
44#endif
Eli Cohene126ba92013-07-07 17:25:49 +030045#include <linux/sched.h>
Ingo Molnar6e84f312017-02-08 18:51:29 +010046#include <linux/sched/mm.h>
Ingo Molnar0881e7b2017-02-05 15:30:50 +010047#include <linux/sched/task.h>
Maor Gottlieb7c2344c2016-06-17 14:56:44 +030048#include <linux/delay.h>
Eli Cohene126ba92013-07-07 17:25:49 +030049#include <rdma/ib_user_verbs.h>
Achiad Shochat3f89a642015-12-23 18:47:21 +020050#include <rdma/ib_addr.h>
Achiad Shochat2811ba52015-12-23 18:47:24 +020051#include <rdma/ib_cache.h>
Achiad Shochatada68c32016-02-22 18:17:23 +020052#include <linux/mlx5/port.h>
Majd Dibbiny1b5daf12015-06-04 19:30:46 +030053#include <linux/mlx5/vport.h>
Pravin Shedge72c7fe92017-12-06 22:19:39 +053054#include <linux/mlx5/fs.h>
Maor Gottliebcecae742019-06-12 15:20:13 +030055#include <linux/mlx5/eswitch.h>
Maor Gottlieb7c2344c2016-06-17 14:56:44 +030056#include <linux/list.h>
Eli Cohene126ba92013-07-07 17:25:49 +030057#include <rdma/ib_smi.h>
58#include <rdma/ib_umem.h>
Maor Gottlieb038d2ef2016-01-11 10:26:07 +020059#include <linux/in.h>
60#include <linux/etherdevice.h>
Eli Cohene126ba92013-07-07 17:25:49 +030061#include "mlx5_ib.h"
Mark Blochfc385b72018-01-16 14:34:48 +000062#include "ib_rep.h"
Parav Pandite1f24a72017-04-16 07:29:29 +030063#include "cmd.h"
Leon Romanovskyf3da6572018-11-28 20:53:41 +020064#include "srq.h"
Boris Pismenny3346c482017-08-20 15:13:08 +030065#include <linux/mlx5/fs_helpers.h>
Aviad Yehezkelc6475a02018-03-28 09:27:50 +030066#include <linux/mlx5/accel.h>
Matan Barak8c846602018-03-28 09:27:41 +030067#include <rdma/uverbs_std_types.h>
Aviad Yehezkelc6475a02018-03-28 09:27:50 +030068#include <rdma/mlx5_user_ioctl_verbs.h>
69#include <rdma/mlx5_user_ioctl_cmds.h>
Erez Alfasi4061ff72019-10-16 09:23:08 +030070#include <rdma/ib_umem_odp.h>
Matan Barak8c846602018-03-28 09:27:41 +030071
72#define UVERBS_MODULE_NAME mlx5_ib
73#include <rdma/uverbs_named_ioctl.h>
Eli Cohene126ba92013-07-07 17:25:49 +030074
75#define DRIVER_NAME "mlx5_ib"
Tariq Toukanb3599112017-02-22 17:45:46 +020076#define DRIVER_VERSION "5.0-0"
Eli Cohene126ba92013-07-07 17:25:49 +030077
78MODULE_AUTHOR("Eli Cohen <eli@mellanox.com>");
79MODULE_DESCRIPTION("Mellanox Connect-IB HCA IB driver");
80MODULE_LICENSE("Dual BSD/GPL");
Eli Cohene126ba92013-07-07 17:25:49 +030081
Eli Cohene126ba92013-07-07 17:25:49 +030082static char mlx5_version[] =
83 DRIVER_NAME ": Mellanox Connect-IB Infiniband driver v"
Tariq Toukanb3599112017-02-22 17:45:46 +020084 DRIVER_VERSION "\n";
Eli Cohene126ba92013-07-07 17:25:49 +030085
Daniel Jurgensd69a24e2018-01-04 17:25:37 +020086struct mlx5_ib_event_work {
87 struct work_struct work;
Saeed Mahameeddf097a22018-11-26 14:39:00 -080088 union {
89 struct mlx5_ib_dev *dev;
90 struct mlx5_ib_multiport_info *mpi;
91 };
92 bool is_slave;
Saeed Mahameed134e9342018-11-26 14:39:02 -080093 unsigned int event;
Saeed Mahameeddf097a22018-11-26 14:39:00 -080094 void *param;
Daniel Jurgensd69a24e2018-01-04 17:25:37 +020095};
96
Eran Ben Elishada7525d2015-12-14 16:34:10 +020097enum {
98 MLX5_ATOMIC_SIZE_QP_8BYTES = 1 << 3,
99};
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300100
Daniel Jurgensd69a24e2018-01-04 17:25:37 +0200101static struct workqueue_struct *mlx5_ib_event_wq;
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200102static LIST_HEAD(mlx5_ib_unaffiliated_port_list);
103static LIST_HEAD(mlx5_ib_dev_list);
104/*
105 * This mutex should be held when accessing either of the above lists
106 */
107static DEFINE_MUTEX(mlx5_ib_multiport_mutex);
108
Ilya Lesokhinc44ef992018-03-13 15:18:48 +0200109/* We can't use an array for xlt_emergency_page because dma_map_single
110 * doesn't work on kernel modules memory
111 */
112static unsigned long xlt_emergency_page;
113static struct mutex xlt_emergency_page_mutex;
114
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200115struct mlx5_ib_dev *mlx5_ib_get_ibdev_from_mpi(struct mlx5_ib_multiport_info *mpi)
116{
117 struct mlx5_ib_dev *dev;
118
119 mutex_lock(&mlx5_ib_multiport_mutex);
120 dev = mpi->ibdev;
121 mutex_unlock(&mlx5_ib_multiport_mutex);
122 return dev;
123}
124
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300125static enum rdma_link_layer
Achiad Shochatebd61f62015-12-23 18:47:16 +0200126mlx5_port_type_cap_to_rdma_ll(int port_type_cap)
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300127{
Achiad Shochatebd61f62015-12-23 18:47:16 +0200128 switch (port_type_cap) {
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300129 case MLX5_CAP_PORT_TYPE_IB:
130 return IB_LINK_LAYER_INFINIBAND;
131 case MLX5_CAP_PORT_TYPE_ETH:
132 return IB_LINK_LAYER_ETHERNET;
133 default:
134 return IB_LINK_LAYER_UNSPECIFIED;
135 }
136}
137
Achiad Shochatebd61f62015-12-23 18:47:16 +0200138static enum rdma_link_layer
139mlx5_ib_port_link_layer(struct ib_device *device, u8 port_num)
140{
141 struct mlx5_ib_dev *dev = to_mdev(device);
142 int port_type_cap = MLX5_CAP_GEN(dev->mdev, port_type);
143
144 return mlx5_port_type_cap_to_rdma_ll(port_type_cap);
145}
146
Moni Shouafd65f1b2017-05-30 09:56:05 +0300147static int get_port_state(struct ib_device *ibdev,
148 u8 port_num,
149 enum ib_port_state *state)
150{
151 struct ib_port_attr attr;
152 int ret;
153
154 memset(&attr, 0, sizeof(attr));
Kamal Heib3023a1e2018-12-10 21:09:48 +0200155 ret = ibdev->ops.query_port(ibdev, port_num, &attr);
Moni Shouafd65f1b2017-05-30 09:56:05 +0300156 if (!ret)
157 *state = attr.state;
158 return ret;
159}
160
Mark Bloch35b0aa62019-03-28 15:27:39 +0200161static struct mlx5_roce *mlx5_get_rep_roce(struct mlx5_ib_dev *dev,
162 struct net_device *ndev,
163 u8 *port_num)
164{
165 struct mlx5_eswitch *esw = dev->mdev->priv.eswitch;
166 struct net_device *rep_ndev;
167 struct mlx5_ib_port *port;
168 int i;
169
170 for (i = 0; i < dev->num_ports; i++) {
171 port = &dev->port[i];
172 if (!port->rep)
173 continue;
174
175 read_lock(&port->roce.netdev_lock);
176 rep_ndev = mlx5_ib_get_rep_netdev(esw,
177 port->rep->vport);
178 if (rep_ndev == ndev) {
179 read_unlock(&port->roce.netdev_lock);
180 *port_num = i + 1;
181 return &port->roce;
182 }
183 read_unlock(&port->roce.netdev_lock);
184 }
185
186 return NULL;
187}
188
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200189static int mlx5_netdev_event(struct notifier_block *this,
190 unsigned long event, void *ptr)
191{
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200192 struct mlx5_roce *roce = container_of(this, struct mlx5_roce, nb);
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200193 struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200194 u8 port_num = roce->native_port_num;
195 struct mlx5_core_dev *mdev;
196 struct mlx5_ib_dev *ibdev;
197
198 ibdev = roce->dev;
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200199 mdev = mlx5_ib_get_native_port_mdev(ibdev, port_num, NULL);
200 if (!mdev)
201 return NOTIFY_DONE;
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200202
Aviv Heller5ec8c832016-09-18 20:48:00 +0300203 switch (event) {
204 case NETDEV_REGISTER:
Mark Bloch35b0aa62019-03-28 15:27:39 +0200205 /* Should already be registered during the load */
206 if (ibdev->is_rep)
207 break;
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200208 write_lock(&roce->netdev_lock);
Linus Torvaldsdce45af2019-05-09 09:02:46 -0700209 if (ndev->dev.parent == mdev->device)
Or Gerlitz842a9c82018-12-11 18:10:43 +0200210 roce->netdev = ndev;
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200211 write_unlock(&roce->netdev_lock);
Aviv Heller5ec8c832016-09-18 20:48:00 +0300212 break;
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200213
Or Gerlitz842a9c82018-12-11 18:10:43 +0200214 case NETDEV_UNREGISTER:
Mark Bloch35b0aa62019-03-28 15:27:39 +0200215 /* In case of reps, ib device goes away before the netdevs */
Or Gerlitz842a9c82018-12-11 18:10:43 +0200216 write_lock(&roce->netdev_lock);
217 if (roce->netdev == ndev)
218 roce->netdev = NULL;
219 write_unlock(&roce->netdev_lock);
220 break;
221
Moni Shouafd65f1b2017-05-30 09:56:05 +0300222 case NETDEV_CHANGE:
Aviv Heller5ec8c832016-09-18 20:48:00 +0300223 case NETDEV_UP:
Aviv Heller88621df2016-09-18 20:48:02 +0300224 case NETDEV_DOWN: {
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200225 struct net_device *lag_ndev = mlx5_lag_get_roce_netdev(mdev);
Aviv Heller88621df2016-09-18 20:48:02 +0300226 struct net_device *upper = NULL;
227
228 if (lag_ndev) {
229 upper = netdev_master_upper_dev_get(lag_ndev);
230 dev_put(lag_ndev);
231 }
232
Mark Bloch35b0aa62019-03-28 15:27:39 +0200233 if (ibdev->is_rep)
234 roce = mlx5_get_rep_roce(ibdev, ndev, &port_num);
235 if (!roce)
236 return NOTIFY_DONE;
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200237 if ((upper == ndev || (!upper && ndev == roce->netdev))
Aviv Heller88621df2016-09-18 20:48:02 +0300238 && ibdev->ib_active) {
Bart Van Assche626bc022016-12-05 17:18:08 -0800239 struct ib_event ibev = { };
Moni Shouafd65f1b2017-05-30 09:56:05 +0300240 enum ib_port_state port_state;
Aviv Heller5ec8c832016-09-18 20:48:00 +0300241
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200242 if (get_port_state(&ibdev->ib_dev, port_num,
243 &port_state))
244 goto done;
Moni Shouafd65f1b2017-05-30 09:56:05 +0300245
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200246 if (roce->last_port_state == port_state)
247 goto done;
Moni Shouafd65f1b2017-05-30 09:56:05 +0300248
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200249 roce->last_port_state = port_state;
Aviv Heller5ec8c832016-09-18 20:48:00 +0300250 ibev.device = &ibdev->ib_dev;
Moni Shouafd65f1b2017-05-30 09:56:05 +0300251 if (port_state == IB_PORT_DOWN)
252 ibev.event = IB_EVENT_PORT_ERR;
253 else if (port_state == IB_PORT_ACTIVE)
254 ibev.event = IB_EVENT_PORT_ACTIVE;
255 else
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200256 goto done;
Moni Shouafd65f1b2017-05-30 09:56:05 +0300257
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200258 ibev.element.port_num = port_num;
Aviv Heller5ec8c832016-09-18 20:48:00 +0300259 ib_dispatch_event(&ibev);
260 }
261 break;
Aviv Heller88621df2016-09-18 20:48:02 +0300262 }
Aviv Heller5ec8c832016-09-18 20:48:00 +0300263
264 default:
265 break;
266 }
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200267done:
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200268 mlx5_ib_put_native_port_mdev(ibdev, port_num);
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200269 return NOTIFY_DONE;
270}
271
272static struct net_device *mlx5_ib_get_netdev(struct ib_device *device,
273 u8 port_num)
274{
275 struct mlx5_ib_dev *ibdev = to_mdev(device);
276 struct net_device *ndev;
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200277 struct mlx5_core_dev *mdev;
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200278
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200279 mdev = mlx5_ib_get_native_port_mdev(ibdev, port_num, NULL);
280 if (!mdev)
281 return NULL;
282
283 ndev = mlx5_lag_get_roce_netdev(mdev);
Aviv Heller88621df2016-09-18 20:48:02 +0300284 if (ndev)
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200285 goto out;
Aviv Heller88621df2016-09-18 20:48:02 +0300286
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200287 /* Ensure ndev does not disappear before we invoke dev_hold()
288 */
Mark Bloch95579e72019-03-28 15:27:33 +0200289 read_lock(&ibdev->port[port_num - 1].roce.netdev_lock);
290 ndev = ibdev->port[port_num - 1].roce.netdev;
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200291 if (ndev)
292 dev_hold(ndev);
Mark Bloch95579e72019-03-28 15:27:33 +0200293 read_unlock(&ibdev->port[port_num - 1].roce.netdev_lock);
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200294
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200295out:
296 mlx5_ib_put_native_port_mdev(ibdev, port_num);
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200297 return ndev;
298}
299
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200300struct mlx5_core_dev *mlx5_ib_get_native_port_mdev(struct mlx5_ib_dev *ibdev,
301 u8 ib_port_num,
302 u8 *native_port_num)
303{
304 enum rdma_link_layer ll = mlx5_ib_port_link_layer(&ibdev->ib_dev,
305 ib_port_num);
306 struct mlx5_core_dev *mdev = NULL;
307 struct mlx5_ib_multiport_info *mpi;
308 struct mlx5_ib_port *port;
309
Mark Bloch210b1f72018-03-05 20:09:47 +0200310 if (!mlx5_core_mp_enabled(ibdev->mdev) ||
311 ll != IB_LINK_LAYER_ETHERNET) {
312 if (native_port_num)
313 *native_port_num = ib_port_num;
314 return ibdev->mdev;
315 }
316
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200317 if (native_port_num)
318 *native_port_num = 1;
319
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200320 port = &ibdev->port[ib_port_num - 1];
321 if (!port)
322 return NULL;
323
324 spin_lock(&port->mp.mpi_lock);
325 mpi = ibdev->port[ib_port_num - 1].mp.mpi;
326 if (mpi && !mpi->unaffiliate) {
327 mdev = mpi->mdev;
328 /* If it's the master no need to refcount, it'll exist
329 * as long as the ib_dev exists.
330 */
331 if (!mpi->is_master)
332 mpi->mdev_refcnt++;
333 }
334 spin_unlock(&port->mp.mpi_lock);
335
336 return mdev;
337}
338
339void mlx5_ib_put_native_port_mdev(struct mlx5_ib_dev *ibdev, u8 port_num)
340{
341 enum rdma_link_layer ll = mlx5_ib_port_link_layer(&ibdev->ib_dev,
342 port_num);
343 struct mlx5_ib_multiport_info *mpi;
344 struct mlx5_ib_port *port;
345
346 if (!mlx5_core_mp_enabled(ibdev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
347 return;
348
349 port = &ibdev->port[port_num - 1];
350
351 spin_lock(&port->mp.mpi_lock);
352 mpi = ibdev->port[port_num - 1].mp.mpi;
353 if (mpi->is_master)
354 goto out;
355
356 mpi->mdev_refcnt--;
357 if (mpi->unaffiliate)
358 complete(&mpi->unref_comp);
359out:
360 spin_unlock(&port->mp.mpi_lock);
361}
362
Aya Levin08e86762019-02-12 22:55:46 -0800363static int translate_eth_legacy_proto_oper(u32 eth_proto_oper, u8 *active_speed,
364 u8 *active_width)
Noa Osherovichf1b65df2017-04-20 20:53:33 +0300365{
366 switch (eth_proto_oper) {
367 case MLX5E_PROT_MASK(MLX5E_1000BASE_CX_SGMII):
368 case MLX5E_PROT_MASK(MLX5E_1000BASE_KX):
369 case MLX5E_PROT_MASK(MLX5E_100BASE_TX):
370 case MLX5E_PROT_MASK(MLX5E_1000BASE_T):
371 *active_width = IB_WIDTH_1X;
372 *active_speed = IB_SPEED_SDR;
373 break;
374 case MLX5E_PROT_MASK(MLX5E_10GBASE_T):
375 case MLX5E_PROT_MASK(MLX5E_10GBASE_CX4):
376 case MLX5E_PROT_MASK(MLX5E_10GBASE_KX4):
377 case MLX5E_PROT_MASK(MLX5E_10GBASE_KR):
378 case MLX5E_PROT_MASK(MLX5E_10GBASE_CR):
379 case MLX5E_PROT_MASK(MLX5E_10GBASE_SR):
380 case MLX5E_PROT_MASK(MLX5E_10GBASE_ER):
381 *active_width = IB_WIDTH_1X;
382 *active_speed = IB_SPEED_QDR;
383 break;
384 case MLX5E_PROT_MASK(MLX5E_25GBASE_CR):
385 case MLX5E_PROT_MASK(MLX5E_25GBASE_KR):
386 case MLX5E_PROT_MASK(MLX5E_25GBASE_SR):
387 *active_width = IB_WIDTH_1X;
388 *active_speed = IB_SPEED_EDR;
389 break;
390 case MLX5E_PROT_MASK(MLX5E_40GBASE_CR4):
391 case MLX5E_PROT_MASK(MLX5E_40GBASE_KR4):
392 case MLX5E_PROT_MASK(MLX5E_40GBASE_SR4):
393 case MLX5E_PROT_MASK(MLX5E_40GBASE_LR4):
394 *active_width = IB_WIDTH_4X;
395 *active_speed = IB_SPEED_QDR;
396 break;
397 case MLX5E_PROT_MASK(MLX5E_50GBASE_CR2):
398 case MLX5E_PROT_MASK(MLX5E_50GBASE_KR2):
399 case MLX5E_PROT_MASK(MLX5E_50GBASE_SR2):
400 *active_width = IB_WIDTH_1X;
401 *active_speed = IB_SPEED_HDR;
402 break;
403 case MLX5E_PROT_MASK(MLX5E_56GBASE_R4):
404 *active_width = IB_WIDTH_4X;
405 *active_speed = IB_SPEED_FDR;
406 break;
407 case MLX5E_PROT_MASK(MLX5E_100GBASE_CR4):
408 case MLX5E_PROT_MASK(MLX5E_100GBASE_SR4):
409 case MLX5E_PROT_MASK(MLX5E_100GBASE_KR4):
410 case MLX5E_PROT_MASK(MLX5E_100GBASE_LR4):
411 *active_width = IB_WIDTH_4X;
412 *active_speed = IB_SPEED_EDR;
413 break;
414 default:
415 return -EINVAL;
416 }
417
418 return 0;
419}
420
Aya Levin08e86762019-02-12 22:55:46 -0800421static int translate_eth_ext_proto_oper(u32 eth_proto_oper, u8 *active_speed,
422 u8 *active_width)
423{
424 switch (eth_proto_oper) {
425 case MLX5E_PROT_MASK(MLX5E_SGMII_100M):
426 case MLX5E_PROT_MASK(MLX5E_1000BASE_X_SGMII):
427 *active_width = IB_WIDTH_1X;
428 *active_speed = IB_SPEED_SDR;
429 break;
430 case MLX5E_PROT_MASK(MLX5E_5GBASE_R):
431 *active_width = IB_WIDTH_1X;
432 *active_speed = IB_SPEED_DDR;
433 break;
434 case MLX5E_PROT_MASK(MLX5E_10GBASE_XFI_XAUI_1):
435 *active_width = IB_WIDTH_1X;
436 *active_speed = IB_SPEED_QDR;
437 break;
438 case MLX5E_PROT_MASK(MLX5E_40GBASE_XLAUI_4_XLPPI_4):
439 *active_width = IB_WIDTH_4X;
440 *active_speed = IB_SPEED_QDR;
441 break;
442 case MLX5E_PROT_MASK(MLX5E_25GAUI_1_25GBASE_CR_KR):
443 *active_width = IB_WIDTH_1X;
444 *active_speed = IB_SPEED_EDR;
445 break;
446 case MLX5E_PROT_MASK(MLX5E_50GAUI_2_LAUI_2_50GBASE_CR2_KR2):
Aya Levincd272872019-03-11 14:35:58 +0200447 *active_width = IB_WIDTH_2X;
448 *active_speed = IB_SPEED_EDR;
449 break;
Aya Levin08e86762019-02-12 22:55:46 -0800450 case MLX5E_PROT_MASK(MLX5E_50GAUI_1_LAUI_1_50GBASE_CR_KR):
451 *active_width = IB_WIDTH_1X;
452 *active_speed = IB_SPEED_HDR;
453 break;
Aya Levincd272872019-03-11 14:35:58 +0200454 case MLX5E_PROT_MASK(MLX5E_CAUI_4_100GBASE_CR4_KR4):
455 *active_width = IB_WIDTH_4X;
456 *active_speed = IB_SPEED_EDR;
457 break;
Aya Levin08e86762019-02-12 22:55:46 -0800458 case MLX5E_PROT_MASK(MLX5E_100GAUI_2_100GBASE_CR2_KR2):
459 *active_width = IB_WIDTH_2X;
460 *active_speed = IB_SPEED_HDR;
461 break;
462 case MLX5E_PROT_MASK(MLX5E_200GAUI_4_200GBASE_CR4_KR4):
463 *active_width = IB_WIDTH_4X;
464 *active_speed = IB_SPEED_HDR;
465 break;
466 default:
467 return -EINVAL;
468 }
469
470 return 0;
471}
472
473static int translate_eth_proto_oper(u32 eth_proto_oper, u8 *active_speed,
474 u8 *active_width, bool ext)
475{
476 return ext ?
477 translate_eth_ext_proto_oper(eth_proto_oper, active_speed,
478 active_width) :
479 translate_eth_legacy_proto_oper(eth_proto_oper, active_speed,
480 active_width);
481}
482
Ilan Tayari095b0922017-05-14 16:04:30 +0300483static int mlx5_query_port_roce(struct ib_device *device, u8 port_num,
484 struct ib_port_attr *props)
Achiad Shochat3f89a642015-12-23 18:47:21 +0200485{
486 struct mlx5_ib_dev *dev = to_mdev(device);
Aya Levinbc4e12f2019-02-12 22:55:43 -0800487 u32 out[MLX5_ST_SZ_DW(ptys_reg)] = {0};
Colin Ian Kingda005f92018-01-09 15:55:43 +0000488 struct mlx5_core_dev *mdev;
Aviv Heller88621df2016-09-18 20:48:02 +0300489 struct net_device *ndev, *upper;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200490 enum ib_mtu ndev_ib_mtu;
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200491 bool put_mdev = true;
Leon Romanovskyc876a1b2016-01-09 13:06:25 +0200492 u16 qkey_viol_cntr;
Noa Osherovichf1b65df2017-04-20 20:53:33 +0300493 u32 eth_prot_oper;
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200494 u8 mdev_port_num;
Aya Levin08e86762019-02-12 22:55:46 -0800495 bool ext;
Ilan Tayari095b0922017-05-14 16:04:30 +0300496 int err;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200497
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200498 mdev = mlx5_ib_get_native_port_mdev(dev, port_num, &mdev_port_num);
499 if (!mdev) {
500 /* This means the port isn't affiliated yet. Get the
501 * info for the master port instead.
502 */
503 put_mdev = false;
504 mdev = dev->mdev;
505 mdev_port_num = 1;
506 port_num = 1;
507 }
508
Noa Osherovichf1b65df2017-04-20 20:53:33 +0300509 /* Possible bad flows are checked before filling out props so in case
510 * of an error it will still be zeroed out.
Mark Bloch26628e22019-03-28 15:27:41 +0200511 * Use native port in case of reps
Noa Osherovich50f22fd2017-04-20 20:53:32 +0300512 */
Mark Bloch26628e22019-03-28 15:27:41 +0200513 if (dev->is_rep)
514 err = mlx5_query_port_ptys(mdev, out, sizeof(out), MLX5_PTYS_EN,
515 1);
516 else
517 err = mlx5_query_port_ptys(mdev, out, sizeof(out), MLX5_PTYS_EN,
518 mdev_port_num);
Ilan Tayari095b0922017-05-14 16:04:30 +0300519 if (err)
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200520 goto out;
Aya Levin08e86762019-02-12 22:55:46 -0800521 ext = MLX5_CAP_PCAM_FEATURE(dev->mdev, ptys_extended_ethernet);
522 eth_prot_oper = MLX5_GET_ETH_PROTO(ptys_reg, out, ext, eth_proto_oper);
Noa Osherovichf1b65df2017-04-20 20:53:33 +0300523
Honggang Li7672ed32018-03-16 10:37:13 +0800524 props->active_width = IB_WIDTH_4X;
525 props->active_speed = IB_SPEED_QDR;
526
Noa Osherovichf1b65df2017-04-20 20:53:33 +0300527 translate_eth_proto_oper(eth_prot_oper, &props->active_speed,
Aya Levin08e86762019-02-12 22:55:46 -0800528 &props->active_width, ext);
Achiad Shochat3f89a642015-12-23 18:47:21 +0200529
Jason Gunthorpe2f944c02018-07-04 15:57:48 +0300530 props->port_cap_flags |= IB_PORT_CM_SUP;
531 props->ip_gids = true;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200532
533 props->gid_tbl_len = MLX5_CAP_ROCE(dev->mdev,
534 roce_address_table_size);
535 props->max_mtu = IB_MTU_4096;
536 props->max_msg_sz = 1 << MLX5_CAP_GEN(dev->mdev, log_max_msg);
537 props->pkey_tbl_len = 1;
538 props->state = IB_PORT_DOWN;
Kamal Heib72a77202019-08-07 13:31:35 +0300539 props->phys_state = IB_PORT_PHYS_STATE_DISABLED;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200540
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200541 mlx5_query_nic_vport_qkey_viol_cntr(mdev, &qkey_viol_cntr);
Leon Romanovskyc876a1b2016-01-09 13:06:25 +0200542 props->qkey_viol_cntr = qkey_viol_cntr;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200543
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200544 /* If this is a stub query for an unaffiliated port stop here */
545 if (!put_mdev)
546 goto out;
547
Achiad Shochat3f89a642015-12-23 18:47:21 +0200548 ndev = mlx5_ib_get_netdev(device, port_num);
549 if (!ndev)
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200550 goto out;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200551
Aviv Heller7c34ec12018-08-23 13:47:53 +0300552 if (dev->lag_active) {
Aviv Heller88621df2016-09-18 20:48:02 +0300553 rcu_read_lock();
554 upper = netdev_master_upper_dev_get_rcu(ndev);
555 if (upper) {
556 dev_put(ndev);
557 ndev = upper;
558 dev_hold(ndev);
559 }
560 rcu_read_unlock();
561 }
562
Achiad Shochat3f89a642015-12-23 18:47:21 +0200563 if (netif_running(ndev) && netif_carrier_ok(ndev)) {
564 props->state = IB_PORT_ACTIVE;
Kamal Heib72a77202019-08-07 13:31:35 +0300565 props->phys_state = IB_PORT_PHYS_STATE_LINK_UP;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200566 }
567
568 ndev_ib_mtu = iboe_get_mtu(ndev->mtu);
569
570 dev_put(ndev);
571
572 props->active_mtu = min(props->max_mtu, ndev_ib_mtu);
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200573out:
574 if (put_mdev)
575 mlx5_ib_put_native_port_mdev(dev, port_num);
576 return err;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200577}
578
Ilan Tayari095b0922017-05-14 16:04:30 +0300579static int set_roce_addr(struct mlx5_ib_dev *dev, u8 port_num,
580 unsigned int index, const union ib_gid *gid,
581 const struct ib_gid_attr *attr)
Achiad Shochat3cca2602015-12-23 18:47:23 +0200582{
Ilan Tayari095b0922017-05-14 16:04:30 +0300583 enum ib_gid_type gid_type = IB_GID_TYPE_IB;
Parav Pandita70c0732019-05-02 10:48:03 +0300584 u16 vlan_id = 0xffff;
Ilan Tayari095b0922017-05-14 16:04:30 +0300585 u8 roce_version = 0;
586 u8 roce_l3_type = 0;
Ilan Tayari095b0922017-05-14 16:04:30 +0300587 u8 mac[ETH_ALEN];
Parav Pandita70c0732019-05-02 10:48:03 +0300588 int ret;
Achiad Shochat3cca2602015-12-23 18:47:23 +0200589
Ilan Tayari095b0922017-05-14 16:04:30 +0300590 if (gid) {
591 gid_type = attr->gid_type;
Parav Pandita70c0732019-05-02 10:48:03 +0300592 ret = rdma_read_gid_l2_fields(attr, &vlan_id, &mac[0]);
593 if (ret)
594 return ret;
Achiad Shochat3cca2602015-12-23 18:47:23 +0200595 }
596
Ilan Tayari095b0922017-05-14 16:04:30 +0300597 switch (gid_type) {
Achiad Shochat3cca2602015-12-23 18:47:23 +0200598 case IB_GID_TYPE_IB:
Ilan Tayari095b0922017-05-14 16:04:30 +0300599 roce_version = MLX5_ROCE_VERSION_1;
Achiad Shochat3cca2602015-12-23 18:47:23 +0200600 break;
601 case IB_GID_TYPE_ROCE_UDP_ENCAP:
Ilan Tayari095b0922017-05-14 16:04:30 +0300602 roce_version = MLX5_ROCE_VERSION_2;
603 if (ipv6_addr_v4mapped((void *)gid))
604 roce_l3_type = MLX5_ROCE_L3_TYPE_IPV4;
605 else
606 roce_l3_type = MLX5_ROCE_L3_TYPE_IPV6;
Achiad Shochat3cca2602015-12-23 18:47:23 +0200607 break;
608
609 default:
Ilan Tayari095b0922017-05-14 16:04:30 +0300610 mlx5_ib_warn(dev, "Unexpected GID type %u\n", gid_type);
Achiad Shochat3cca2602015-12-23 18:47:23 +0200611 }
612
Ilan Tayari095b0922017-05-14 16:04:30 +0300613 return mlx5_core_roce_gid_set(dev->mdev, index, roce_version,
Parav Panditcf34e1f2019-01-27 20:35:50 +0200614 roce_l3_type, gid->raw, mac,
Parav Pandita70c0732019-05-02 10:48:03 +0300615 vlan_id < VLAN_CFI_MASK, vlan_id,
Parav Panditcf34e1f2019-01-27 20:35:50 +0200616 port_num);
Achiad Shochat3cca2602015-12-23 18:47:23 +0200617}
618
Parav Panditf4df9a72018-06-05 08:40:16 +0300619static int mlx5_ib_add_gid(const struct ib_gid_attr *attr,
Achiad Shochat3cca2602015-12-23 18:47:23 +0200620 __always_unused void **context)
621{
Parav Pandit414448d2018-04-01 15:08:24 +0300622 return set_roce_addr(to_mdev(attr->device), attr->port_num,
Parav Panditf4df9a72018-06-05 08:40:16 +0300623 attr->index, &attr->gid, attr);
Achiad Shochat3cca2602015-12-23 18:47:23 +0200624}
625
Parav Pandit414448d2018-04-01 15:08:24 +0300626static int mlx5_ib_del_gid(const struct ib_gid_attr *attr,
627 __always_unused void **context)
Achiad Shochat3cca2602015-12-23 18:47:23 +0200628{
Parav Pandit414448d2018-04-01 15:08:24 +0300629 return set_roce_addr(to_mdev(attr->device), attr->port_num,
630 attr->index, NULL, NULL);
Achiad Shochat3cca2602015-12-23 18:47:23 +0200631}
632
Parav Pandit47ec3862018-06-13 10:22:06 +0300633__be16 mlx5_get_roce_udp_sport(struct mlx5_ib_dev *dev,
634 const struct ib_gid_attr *attr)
Achiad Shochat2811ba52015-12-23 18:47:24 +0200635{
Parav Pandit47ec3862018-06-13 10:22:06 +0300636 if (attr->gid_type != IB_GID_TYPE_ROCE_UDP_ENCAP)
Achiad Shochat2811ba52015-12-23 18:47:24 +0200637 return 0;
638
639 return cpu_to_be16(MLX5_CAP_ROCE(dev->mdev, r_roce_min_src_udp_port));
640}
641
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300642static int mlx5_use_mad_ifc(struct mlx5_ib_dev *dev)
643{
Noa Osherovich7fae6652016-09-12 19:16:23 +0300644 if (MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_IB)
645 return !MLX5_CAP_GEN(dev->mdev, ib_virt);
646 return 0;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300647}
648
649enum {
650 MLX5_VPORT_ACCESS_METHOD_MAD,
651 MLX5_VPORT_ACCESS_METHOD_HCA,
652 MLX5_VPORT_ACCESS_METHOD_NIC,
653};
654
655static int mlx5_get_vport_access_method(struct ib_device *ibdev)
656{
657 if (mlx5_use_mad_ifc(to_mdev(ibdev)))
658 return MLX5_VPORT_ACCESS_METHOD_MAD;
659
Achiad Shochatebd61f62015-12-23 18:47:16 +0200660 if (mlx5_ib_port_link_layer(ibdev, 1) ==
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300661 IB_LINK_LAYER_ETHERNET)
662 return MLX5_VPORT_ACCESS_METHOD_NIC;
663
664 return MLX5_VPORT_ACCESS_METHOD_HCA;
665}
666
Eran Ben Elishada7525d2015-12-14 16:34:10 +0200667static void get_atomic_caps(struct mlx5_ib_dev *dev,
Moni Shoua776a3902018-01-02 16:19:33 +0200668 u8 atomic_size_qp,
Eran Ben Elishada7525d2015-12-14 16:34:10 +0200669 struct ib_device_attr *props)
670{
671 u8 tmp;
672 u8 atomic_operations = MLX5_CAP_ATOMIC(dev->mdev, atomic_operations);
Eran Ben Elishada7525d2015-12-14 16:34:10 +0200673 u8 atomic_req_8B_endianness_mode =
Or Gerlitzbd108382017-05-28 15:24:17 +0300674 MLX5_CAP_ATOMIC(dev->mdev, atomic_req_8B_endianness_mode);
Eran Ben Elishada7525d2015-12-14 16:34:10 +0200675
676 /* Check if HW supports 8 bytes standard atomic operations and capable
677 * of host endianness respond
678 */
679 tmp = MLX5_ATOMIC_OPS_CMP_SWAP | MLX5_ATOMIC_OPS_FETCH_ADD;
680 if (((atomic_operations & tmp) == tmp) &&
681 (atomic_size_qp & MLX5_ATOMIC_SIZE_QP_8BYTES) &&
682 (atomic_req_8B_endianness_mode)) {
683 props->atomic_cap = IB_ATOMIC_HCA;
684 } else {
685 props->atomic_cap = IB_ATOMIC_NONE;
686 }
687}
688
Moni Shoua776a3902018-01-02 16:19:33 +0200689static void get_atomic_caps_qp(struct mlx5_ib_dev *dev,
690 struct ib_device_attr *props)
691{
692 u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_qp);
693
694 get_atomic_caps(dev, atomic_size_qp, props);
695}
696
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300697static int mlx5_query_system_image_guid(struct ib_device *ibdev,
698 __be64 *sys_image_guid)
699{
700 struct mlx5_ib_dev *dev = to_mdev(ibdev);
701 struct mlx5_core_dev *mdev = dev->mdev;
702 u64 tmp;
703 int err;
704
705 switch (mlx5_get_vport_access_method(ibdev)) {
706 case MLX5_VPORT_ACCESS_METHOD_MAD:
707 return mlx5_query_mad_ifc_system_image_guid(ibdev,
708 sys_image_guid);
709
710 case MLX5_VPORT_ACCESS_METHOD_HCA:
711 err = mlx5_query_hca_vport_system_image_guid(mdev, &tmp);
Achiad Shochat3f89a642015-12-23 18:47:21 +0200712 break;
713
714 case MLX5_VPORT_ACCESS_METHOD_NIC:
715 err = mlx5_query_nic_vport_system_image_guid(mdev, &tmp);
716 break;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300717
718 default:
719 return -EINVAL;
720 }
Achiad Shochat3f89a642015-12-23 18:47:21 +0200721
722 if (!err)
723 *sys_image_guid = cpu_to_be64(tmp);
724
725 return err;
726
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300727}
728
729static int mlx5_query_max_pkeys(struct ib_device *ibdev,
730 u16 *max_pkeys)
731{
732 struct mlx5_ib_dev *dev = to_mdev(ibdev);
733 struct mlx5_core_dev *mdev = dev->mdev;
734
735 switch (mlx5_get_vport_access_method(ibdev)) {
736 case MLX5_VPORT_ACCESS_METHOD_MAD:
737 return mlx5_query_mad_ifc_max_pkeys(ibdev, max_pkeys);
738
739 case MLX5_VPORT_ACCESS_METHOD_HCA:
740 case MLX5_VPORT_ACCESS_METHOD_NIC:
741 *max_pkeys = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev,
742 pkey_table_size));
743 return 0;
744
745 default:
746 return -EINVAL;
747 }
748}
749
750static int mlx5_query_vendor_id(struct ib_device *ibdev,
751 u32 *vendor_id)
752{
753 struct mlx5_ib_dev *dev = to_mdev(ibdev);
754
755 switch (mlx5_get_vport_access_method(ibdev)) {
756 case MLX5_VPORT_ACCESS_METHOD_MAD:
757 return mlx5_query_mad_ifc_vendor_id(ibdev, vendor_id);
758
759 case MLX5_VPORT_ACCESS_METHOD_HCA:
760 case MLX5_VPORT_ACCESS_METHOD_NIC:
761 return mlx5_core_query_vendor_id(dev->mdev, vendor_id);
762
763 default:
764 return -EINVAL;
765 }
766}
767
768static int mlx5_query_node_guid(struct mlx5_ib_dev *dev,
769 __be64 *node_guid)
770{
771 u64 tmp;
772 int err;
773
774 switch (mlx5_get_vport_access_method(&dev->ib_dev)) {
775 case MLX5_VPORT_ACCESS_METHOD_MAD:
776 return mlx5_query_mad_ifc_node_guid(dev, node_guid);
777
778 case MLX5_VPORT_ACCESS_METHOD_HCA:
779 err = mlx5_query_hca_vport_node_guid(dev->mdev, &tmp);
Achiad Shochat3f89a642015-12-23 18:47:21 +0200780 break;
781
782 case MLX5_VPORT_ACCESS_METHOD_NIC:
783 err = mlx5_query_nic_vport_node_guid(dev->mdev, &tmp);
784 break;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300785
786 default:
787 return -EINVAL;
788 }
Achiad Shochat3f89a642015-12-23 18:47:21 +0200789
790 if (!err)
791 *node_guid = cpu_to_be64(tmp);
792
793 return err;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300794}
795
796struct mlx5_reg_node_desc {
Yuval Shaiabd99fde2016-08-25 10:57:07 -0700797 u8 desc[IB_DEVICE_NODE_DESC_MAX];
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300798};
799
800static int mlx5_query_node_desc(struct mlx5_ib_dev *dev, char *node_desc)
801{
802 struct mlx5_reg_node_desc in;
803
804 if (mlx5_use_mad_ifc(dev))
805 return mlx5_query_mad_ifc_node_desc(dev, node_desc);
806
807 memset(&in, 0, sizeof(in));
808
809 return mlx5_core_access_reg(dev->mdev, &in, sizeof(in), node_desc,
810 sizeof(struct mlx5_reg_node_desc),
811 MLX5_REG_NODE_DESC, 0, 0);
812}
813
Eli Cohene126ba92013-07-07 17:25:49 +0300814static int mlx5_ib_query_device(struct ib_device *ibdev,
Matan Barak2528e332015-06-11 16:35:25 +0300815 struct ib_device_attr *props,
816 struct ib_udata *uhw)
Eli Cohene126ba92013-07-07 17:25:49 +0300817{
818 struct mlx5_ib_dev *dev = to_mdev(ibdev);
Saeed Mahameed938fe832015-05-28 22:28:41 +0300819 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +0300820 int err = -ENOMEM;
Eli Cohen288c01b2016-10-27 16:36:45 +0300821 int max_sq_desc;
Eli Cohene126ba92013-07-07 17:25:49 +0300822 int max_rq_sg;
823 int max_sq_sg;
Sagi Grimberge0238a62015-07-21 14:40:12 +0300824 u64 min_page_size = 1ull << MLX5_CAP_GEN(mdev, log_pg_sz);
Daniel Jurgens85c7c012018-01-04 17:25:43 +0200825 bool raw_support = !mlx5_core_mp_enabled(mdev);
Bodong Wang402ca532016-06-17 15:02:20 +0300826 struct mlx5_ib_query_device_resp resp = {};
827 size_t resp_len;
828 u64 max_tso;
Eli Cohene126ba92013-07-07 17:25:49 +0300829
Bodong Wang402ca532016-06-17 15:02:20 +0300830 resp_len = sizeof(resp.comp_mask) + sizeof(resp.response_length);
831 if (uhw->outlen && uhw->outlen < resp_len)
832 return -EINVAL;
Erez Alfasi6f26b2a2019-10-02 15:25:16 +0300833
834 resp.response_length = resp_len;
Bodong Wang402ca532016-06-17 15:02:20 +0300835
836 if (uhw->inlen && !ib_is_udata_cleared(uhw, 0, uhw->inlen))
Matan Barak2528e332015-06-11 16:35:25 +0300837 return -EINVAL;
838
Eli Cohene126ba92013-07-07 17:25:49 +0300839 memset(props, 0, sizeof(*props));
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300840 err = mlx5_query_system_image_guid(ibdev,
841 &props->sys_image_guid);
842 if (err)
843 return err;
844
845 err = mlx5_query_max_pkeys(ibdev, &props->max_pkeys);
846 if (err)
847 return err;
848
849 err = mlx5_query_vendor_id(ibdev, &props->vendor_id);
850 if (err)
851 return err;
Eli Cohene126ba92013-07-07 17:25:49 +0300852
Jack Morgenstein9603b612014-07-28 23:30:22 +0300853 props->fw_ver = ((u64)fw_rev_maj(dev->mdev) << 32) |
854 (fw_rev_min(dev->mdev) << 16) |
855 fw_rev_sub(dev->mdev);
Eli Cohene126ba92013-07-07 17:25:49 +0300856 props->device_cap_flags = IB_DEVICE_CHANGE_PHY_PORT |
857 IB_DEVICE_PORT_ACTIVE_EVENT |
858 IB_DEVICE_SYS_IMAGE_GUID |
Eli Cohen1a4c3a32014-02-06 17:41:25 +0200859 IB_DEVICE_RC_RNR_NAK_GEN;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300860
861 if (MLX5_CAP_GEN(mdev, pkv))
Eli Cohene126ba92013-07-07 17:25:49 +0300862 props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300863 if (MLX5_CAP_GEN(mdev, qkv))
Eli Cohene126ba92013-07-07 17:25:49 +0300864 props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300865 if (MLX5_CAP_GEN(mdev, apm))
Eli Cohene126ba92013-07-07 17:25:49 +0300866 props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300867 if (MLX5_CAP_GEN(mdev, xrc))
Eli Cohene126ba92013-07-07 17:25:49 +0300868 props->device_cap_flags |= IB_DEVICE_XRC;
Matan Barakd2370e02016-02-29 18:05:30 +0200869 if (MLX5_CAP_GEN(mdev, imaicl)) {
870 props->device_cap_flags |= IB_DEVICE_MEM_WINDOW |
871 IB_DEVICE_MEM_WINDOW_TYPE_2B;
872 props->max_mw = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
Sagi Grimbergb005d312016-02-29 19:07:33 +0200873 /* We support 'Gappy' memory registration too */
874 props->device_cap_flags |= IB_DEVICE_SG_GAPS_REG;
Matan Barakd2370e02016-02-29 18:05:30 +0200875 }
Eli Cohene126ba92013-07-07 17:25:49 +0300876 props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300877 if (MLX5_CAP_GEN(mdev, sho)) {
Israel Rukshinc0a6cbb2019-06-11 18:52:50 +0300878 props->device_cap_flags |= IB_DEVICE_INTEGRITY_HANDOVER;
Sagi Grimberg2dea9092014-02-23 14:19:13 +0200879 /* At this stage no support for signature handover */
880 props->sig_prot_cap = IB_PROT_T10DIF_TYPE_1 |
881 IB_PROT_T10DIF_TYPE_2 |
882 IB_PROT_T10DIF_TYPE_3;
883 props->sig_guard_cap = IB_GUARD_T10DIF_CRC |
884 IB_GUARD_T10DIF_CSUM;
885 }
Saeed Mahameed938fe832015-05-28 22:28:41 +0300886 if (MLX5_CAP_GEN(mdev, block_lb_mc))
Eli Cohenf360d882014-04-02 00:10:16 +0300887 props->device_cap_flags |= IB_DEVICE_BLOCK_MULTICAST_LOOPBACK;
Eli Cohene126ba92013-07-07 17:25:49 +0300888
Daniel Jurgens85c7c012018-01-04 17:25:43 +0200889 if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) && raw_support) {
Noa Osheroviche8161332017-01-18 15:40:01 +0200890 if (MLX5_CAP_ETH(mdev, csum_cap)) {
891 /* Legacy bit to support old userspace libraries */
Bodong Wang88115fe2015-12-18 13:53:20 +0200892 props->device_cap_flags |= IB_DEVICE_RAW_IP_CSUM;
Noa Osheroviche8161332017-01-18 15:40:01 +0200893 props->raw_packet_caps |= IB_RAW_PACKET_CAP_IP_CSUM;
894 }
895
896 if (MLX5_CAP_ETH(dev->mdev, vlan_cap))
897 props->raw_packet_caps |=
898 IB_RAW_PACKET_CAP_CVLAN_STRIPPING;
Bodong Wang88115fe2015-12-18 13:53:20 +0200899
Bodong Wang402ca532016-06-17 15:02:20 +0300900 if (field_avail(typeof(resp), tso_caps, uhw->outlen)) {
901 max_tso = MLX5_CAP_ETH(mdev, max_lso_cap);
902 if (max_tso) {
903 resp.tso_caps.max_tso = 1 << max_tso;
904 resp.tso_caps.supported_qpts |=
905 1 << IB_QPT_RAW_PACKET;
906 resp.response_length += sizeof(resp.tso_caps);
907 }
908 }
Yishai Hadas31f69a82016-08-28 11:28:45 +0300909
910 if (field_avail(typeof(resp), rss_caps, uhw->outlen)) {
911 resp.rss_caps.rx_hash_function =
912 MLX5_RX_HASH_FUNC_TOEPLITZ;
913 resp.rss_caps.rx_hash_fields_mask =
914 MLX5_RX_HASH_SRC_IPV4 |
915 MLX5_RX_HASH_DST_IPV4 |
916 MLX5_RX_HASH_SRC_IPV6 |
917 MLX5_RX_HASH_DST_IPV6 |
918 MLX5_RX_HASH_SRC_PORT_TCP |
919 MLX5_RX_HASH_DST_PORT_TCP |
920 MLX5_RX_HASH_SRC_PORT_UDP |
Maor Gottlieb4e2b53a2017-12-24 14:51:25 +0200921 MLX5_RX_HASH_DST_PORT_UDP |
922 MLX5_RX_HASH_INNER;
Matan Barak2d93fc82018-03-28 09:27:55 +0300923 if (mlx5_accel_ipsec_device_caps(dev->mdev) &
924 MLX5_ACCEL_IPSEC_CAP_DEVICE)
925 resp.rss_caps.rx_hash_fields_mask |=
926 MLX5_RX_HASH_IPSEC_SPI;
Yishai Hadas31f69a82016-08-28 11:28:45 +0300927 resp.response_length += sizeof(resp.rss_caps);
928 }
929 } else {
930 if (field_avail(typeof(resp), tso_caps, uhw->outlen))
931 resp.response_length += sizeof(resp.tso_caps);
932 if (field_avail(typeof(resp), rss_caps, uhw->outlen))
933 resp.response_length += sizeof(resp.rss_caps);
Bodong Wang402ca532016-06-17 15:02:20 +0300934 }
935
Erez Shitritf0313962016-02-21 16:27:17 +0200936 if (MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) {
937 props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
938 props->device_cap_flags |= IB_DEVICE_UD_TSO;
939 }
940
Maor Gottlieb03404e82017-05-30 10:29:13 +0300941 if (MLX5_CAP_GEN(dev->mdev, rq_delay_drop) &&
Daniel Jurgens85c7c012018-01-04 17:25:43 +0200942 MLX5_CAP_GEN(dev->mdev, general_notification_event) &&
943 raw_support)
Maor Gottlieb03404e82017-05-30 10:29:13 +0300944 props->raw_packet_caps |= IB_RAW_PACKET_CAP_DELAY_DROP;
945
Yishai Hadas1d54f892017-06-08 16:15:11 +0300946 if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads) &&
947 MLX5_CAP_IPOIB_ENHANCED(mdev, csum_cap))
948 props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
949
Majd Dibbinycff5a0f2016-04-17 17:19:38 +0300950 if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
Daniel Jurgens85c7c012018-01-04 17:25:43 +0200951 MLX5_CAP_ETH(dev->mdev, scatter_fcs) &&
952 raw_support) {
Noa Osheroviche8161332017-01-18 15:40:01 +0200953 /* Legacy bit to support old userspace libraries */
Majd Dibbinycff5a0f2016-04-17 17:19:38 +0300954 props->device_cap_flags |= IB_DEVICE_RAW_SCATTER_FCS;
Noa Osheroviche8161332017-01-18 15:40:01 +0200955 props->raw_packet_caps |= IB_RAW_PACKET_CAP_SCATTER_FCS;
956 }
Majd Dibbinycff5a0f2016-04-17 17:19:38 +0300957
Ariel Levkovich24da0012018-04-05 18:53:27 +0300958 if (MLX5_CAP_DEV_MEM(mdev, memic)) {
959 props->max_dm_size =
960 MLX5_CAP_DEV_MEM(mdev, max_memic_size);
961 }
962
Maor Gottliebda6d6ba32016-06-04 15:15:28 +0300963 if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS))
964 props->device_cap_flags |= IB_DEVICE_MANAGED_FLOW_STEERING;
965
Noa Osherovichb1383aa2017-10-29 13:59:45 +0200966 if (MLX5_CAP_GEN(mdev, end_pad))
967 props->device_cap_flags |= IB_DEVICE_PCI_WRITE_END_PADDING;
968
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300969 props->vendor_part_id = mdev->pdev->device;
970 props->hw_ver = mdev->pdev->revision;
Eli Cohene126ba92013-07-07 17:25:49 +0300971
972 props->max_mr_size = ~0ull;
Sagi Grimberge0238a62015-07-21 14:40:12 +0300973 props->page_size_cap = ~(min_page_size - 1);
Saeed Mahameed938fe832015-05-28 22:28:41 +0300974 props->max_qp = 1 << MLX5_CAP_GEN(mdev, log_max_qp);
975 props->max_qp_wr = 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
976 max_rq_sg = MLX5_CAP_GEN(mdev, max_wqe_sz_rq) /
977 sizeof(struct mlx5_wqe_data_seg);
Eli Cohen288c01b2016-10-27 16:36:45 +0300978 max_sq_desc = min_t(int, MLX5_CAP_GEN(mdev, max_wqe_sz_sq), 512);
979 max_sq_sg = (max_sq_desc - sizeof(struct mlx5_wqe_ctrl_seg) -
980 sizeof(struct mlx5_wqe_raddr_seg)) /
981 sizeof(struct mlx5_wqe_data_seg);
Steve Wise33023fb2018-06-18 08:05:26 -0700982 props->max_send_sge = max_sq_sg;
983 props->max_recv_sge = max_rq_sg;
Sagi Grimberg986ef952016-03-31 19:03:25 +0300984 props->max_sge_rd = MLX5_MAX_SGE_RD;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300985 props->max_cq = 1 << MLX5_CAP_GEN(mdev, log_max_cq);
Leon Romanovsky9f177682016-01-14 08:11:40 +0200986 props->max_cqe = (1 << MLX5_CAP_GEN(mdev, log_max_cq_sz)) - 1;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300987 props->max_mr = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
988 props->max_pd = 1 << MLX5_CAP_GEN(mdev, log_max_pd);
989 props->max_qp_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_req_qp);
990 props->max_qp_init_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_res_qp);
991 props->max_srq = 1 << MLX5_CAP_GEN(mdev, log_max_srq);
992 props->max_srq_wr = (1 << MLX5_CAP_GEN(mdev, log_max_srq_sz)) - 1;
993 props->local_ca_ack_delay = MLX5_CAP_GEN(mdev, local_ca_ack_delay);
Eli Cohene126ba92013-07-07 17:25:49 +0300994 props->max_res_rd_atom = props->max_qp_rd_atom * props->max_qp;
Eli Cohene126ba92013-07-07 17:25:49 +0300995 props->max_srq_sge = max_rq_sg - 1;
Sagi Grimberg911f4332016-03-03 13:37:51 +0200996 props->max_fast_reg_page_list_len =
997 1 << MLX5_CAP_GEN(mdev, log_max_klm_list_size);
Max Gurtovoy62e3c372019-06-11 18:52:43 +0300998 props->max_pi_fast_reg_page_list_len =
999 props->max_fast_reg_page_list_len / 2;
Yamin Friedman36609052019-10-07 16:59:33 +03001000 props->max_sgl_rd =
1001 MLX5_CAP_GEN(mdev, max_sgl_for_optimized_performance);
Moni Shoua776a3902018-01-02 16:19:33 +02001002 get_atomic_caps_qp(dev, props);
Eli Cohen81bea282013-09-11 16:35:30 +03001003 props->masked_atomic_cap = IB_ATOMIC_NONE;
Saeed Mahameed938fe832015-05-28 22:28:41 +03001004 props->max_mcast_grp = 1 << MLX5_CAP_GEN(mdev, log_max_mcg);
1005 props->max_mcast_qp_attach = MLX5_CAP_GEN(mdev, max_qp_mcg);
Eli Cohene126ba92013-07-07 17:25:49 +03001006 props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
1007 props->max_mcast_grp;
1008 props->max_map_per_fmr = INT_MAX; /* no limit in ConnectIB */
Maor Gottlieb86695a62016-10-27 16:36:38 +03001009 props->max_ah = INT_MAX;
Matan Barak7c60bcb2015-12-15 20:30:11 +02001010 props->hca_core_clock = MLX5_CAP_GEN(mdev, device_frequency_khz);
1011 props->timestamp_mask = 0x7FFFFFFFFFFFFFFFULL;
Eli Cohene126ba92013-07-07 17:25:49 +03001012
Leon Romanovskye502b8b2019-01-08 16:07:24 +02001013 if (IS_ENABLED(CONFIG_INFINIBAND_ON_DEMAND_PAGING)) {
Moni Shoua00815752019-08-15 11:38:32 +03001014 if (dev->odp_caps.general_caps & IB_ODP_SUPPORT)
Leon Romanovskye502b8b2019-01-08 16:07:24 +02001015 props->device_cap_flags |= IB_DEVICE_ON_DEMAND_PAGING;
1016 props->odp_caps = dev->odp_caps;
1017 }
Haggai Eran8cdd3122014-12-11 17:04:20 +02001018
Leon Romanovsky051f2632015-12-20 12:16:11 +02001019 if (MLX5_CAP_GEN(mdev, cd))
1020 props->device_cap_flags |= IB_DEVICE_CROSS_CHANNEL;
1021
Parav Pandite53a9d22019-10-28 23:35:30 +00001022 if (mlx5_core_is_vf(mdev))
Eli Coheneff901d2016-03-11 22:58:42 +02001023 props->device_cap_flags |= IB_DEVICE_VIRTUAL_FUNCTION;
1024
Yishai Hadas31f69a82016-08-28 11:28:45 +03001025 if (mlx5_ib_port_link_layer(ibdev, 1) ==
Daniel Jurgens85c7c012018-01-04 17:25:43 +02001026 IB_LINK_LAYER_ETHERNET && raw_support) {
Yishai Hadas31f69a82016-08-28 11:28:45 +03001027 props->rss_caps.max_rwq_indirection_tables =
1028 1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt);
1029 props->rss_caps.max_rwq_indirection_table_size =
1030 1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt_size);
1031 props->rss_caps.supported_qpts = 1 << IB_QPT_RAW_PACKET;
1032 props->max_wq_type_rq =
1033 1 << MLX5_CAP_GEN(dev->mdev, log_max_rq);
1034 }
1035
Artemy Kovalyoveb761892017-08-17 15:52:09 +03001036 if (MLX5_CAP_GEN(mdev, tag_matching)) {
Leon Romanovsky78b1beb2017-09-24 21:46:29 +03001037 props->tm_caps.max_num_tags =
Artemy Kovalyoveb761892017-08-17 15:52:09 +03001038 (1 << MLX5_CAP_GEN(mdev, log_tag_matching_list_sz)) - 1;
Leon Romanovsky78b1beb2017-09-24 21:46:29 +03001039 props->tm_caps.max_ops =
Artemy Kovalyoveb761892017-08-17 15:52:09 +03001040 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
Leon Romanovsky78b1beb2017-09-24 21:46:29 +03001041 props->tm_caps.max_sge = MLX5_TM_MAX_SGE;
Artemy Kovalyoveb761892017-08-17 15:52:09 +03001042 }
1043
Danit Goldberg89705e92019-07-05 19:21:57 +03001044 if (MLX5_CAP_GEN(mdev, tag_matching) &&
1045 MLX5_CAP_GEN(mdev, rndv_offload_rc)) {
1046 props->tm_caps.flags = IB_TM_CAP_RNDV_RC;
1047 props->tm_caps.max_rndv_hdr_size = MLX5_TM_MAX_RNDV_MSG_SIZE;
1048 }
1049
Yonatan Cohen87ab3f52017-11-13 10:51:18 +02001050 if (MLX5_CAP_GEN(dev->mdev, cq_moderation)) {
1051 props->cq_caps.max_cq_moderation_count =
1052 MLX5_MAX_CQ_COUNT;
1053 props->cq_caps.max_cq_moderation_period =
1054 MLX5_MAX_CQ_PERIOD;
1055 }
1056
Bodong Wang7e43a2a2016-10-31 12:16:44 +02001057 if (field_avail(typeof(resp), cqe_comp_caps, uhw->outlen)) {
Bodong Wang7e43a2a2016-10-31 12:16:44 +02001058 resp.response_length += sizeof(resp.cqe_comp_caps);
Yonatan Cohen572f46b2018-05-27 13:42:33 +03001059
1060 if (MLX5_CAP_GEN(dev->mdev, cqe_compression)) {
1061 resp.cqe_comp_caps.max_num =
1062 MLX5_CAP_GEN(dev->mdev,
1063 cqe_compression_max_num);
1064
1065 resp.cqe_comp_caps.supported_format =
1066 MLX5_IB_CQE_RES_FORMAT_HASH |
1067 MLX5_IB_CQE_RES_FORMAT_CSUM;
Yonatan Cohen6f1006a2018-05-27 13:42:34 +03001068
1069 if (MLX5_CAP_GEN(dev->mdev, mini_cqe_resp_stride_index))
1070 resp.cqe_comp_caps.supported_format |=
1071 MLX5_IB_CQE_RES_FORMAT_CSUM_STRIDX;
Yonatan Cohen572f46b2018-05-27 13:42:33 +03001072 }
Bodong Wang7e43a2a2016-10-31 12:16:44 +02001073 }
1074
Daniel Jurgens85c7c012018-01-04 17:25:43 +02001075 if (field_avail(typeof(resp), packet_pacing_caps, uhw->outlen) &&
1076 raw_support) {
Bodong Wangd9491672016-12-01 13:43:13 +02001077 if (MLX5_CAP_QOS(mdev, packet_pacing) &&
1078 MLX5_CAP_GEN(mdev, qos)) {
1079 resp.packet_pacing_caps.qp_rate_limit_max =
1080 MLX5_CAP_QOS(mdev, packet_pacing_max_rate);
1081 resp.packet_pacing_caps.qp_rate_limit_min =
1082 MLX5_CAP_QOS(mdev, packet_pacing_min_rate);
1083 resp.packet_pacing_caps.supported_qpts |=
1084 1 << IB_QPT_RAW_PACKET;
Bodong Wang61147f32018-03-19 15:10:30 +02001085 if (MLX5_CAP_QOS(mdev, packet_pacing_burst_bound) &&
1086 MLX5_CAP_QOS(mdev, packet_pacing_typical_size))
1087 resp.packet_pacing_caps.cap_flags |=
1088 MLX5_IB_PP_SUPPORT_BURST;
Bodong Wangd9491672016-12-01 13:43:13 +02001089 }
1090 resp.response_length += sizeof(resp.packet_pacing_caps);
1091 }
1092
Leon Romanovsky9f885202017-01-02 11:37:39 +02001093 if (field_avail(typeof(resp), mlx5_ib_support_multi_pkt_send_wqes,
1094 uhw->outlen)) {
Bodong Wang795b6092017-08-17 15:52:34 +03001095 if (MLX5_CAP_ETH(mdev, multi_pkt_send_wqe))
1096 resp.mlx5_ib_support_multi_pkt_send_wqes =
1097 MLX5_IB_ALLOW_MPW;
Bodong Wang050da902017-08-17 15:52:35 +03001098
1099 if (MLX5_CAP_ETH(mdev, enhanced_multi_pkt_send_wqe))
1100 resp.mlx5_ib_support_multi_pkt_send_wqes |=
1101 MLX5_IB_SUPPORT_EMPW;
1102
Leon Romanovsky9f885202017-01-02 11:37:39 +02001103 resp.response_length +=
1104 sizeof(resp.mlx5_ib_support_multi_pkt_send_wqes);
1105 }
1106
Guy Levide57f2a2017-10-19 08:25:52 +03001107 if (field_avail(typeof(resp), flags, uhw->outlen)) {
1108 resp.response_length += sizeof(resp.flags);
Guy Levi7a0c8f42017-10-19 08:25:53 +03001109
Guy Levide57f2a2017-10-19 08:25:52 +03001110 if (MLX5_CAP_GEN(mdev, cqe_compression_128))
1111 resp.flags |=
1112 MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_COMP;
Guy Levi7a0c8f42017-10-19 08:25:53 +03001113
1114 if (MLX5_CAP_GEN(mdev, cqe_128_always))
1115 resp.flags |= MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_PAD;
Danit Goldberg7e11b912018-11-30 13:22:06 +02001116 if (MLX5_CAP_GEN(mdev, qp_packet_based))
1117 resp.flags |=
1118 MLX5_IB_QUERY_DEV_RESP_PACKET_BASED_CREDIT_MODE;
Guy Levi7249c8e2019-04-10 10:59:45 +03001119
1120 resp.flags |= MLX5_IB_QUERY_DEV_RESP_FLAGS_SCAT2CQE_DCT;
Guy Levide57f2a2017-10-19 08:25:52 +03001121 }
Leon Romanovsky9f885202017-01-02 11:37:39 +02001122
Noa Osherovich96dc3fc2017-08-17 15:52:28 +03001123 if (field_avail(typeof(resp), sw_parsing_caps,
1124 uhw->outlen)) {
1125 resp.response_length += sizeof(resp.sw_parsing_caps);
1126 if (MLX5_CAP_ETH(mdev, swp)) {
1127 resp.sw_parsing_caps.sw_parsing_offloads |=
1128 MLX5_IB_SW_PARSING;
1129
1130 if (MLX5_CAP_ETH(mdev, swp_csum))
1131 resp.sw_parsing_caps.sw_parsing_offloads |=
1132 MLX5_IB_SW_PARSING_CSUM;
1133
1134 if (MLX5_CAP_ETH(mdev, swp_lso))
1135 resp.sw_parsing_caps.sw_parsing_offloads |=
1136 MLX5_IB_SW_PARSING_LSO;
1137
1138 if (resp.sw_parsing_caps.sw_parsing_offloads)
1139 resp.sw_parsing_caps.supported_qpts =
1140 BIT(IB_QPT_RAW_PACKET);
1141 }
1142 }
1143
Daniel Jurgens85c7c012018-01-04 17:25:43 +02001144 if (field_avail(typeof(resp), striding_rq_caps, uhw->outlen) &&
1145 raw_support) {
Noa Osherovichb4f34592017-10-17 18:01:12 +03001146 resp.response_length += sizeof(resp.striding_rq_caps);
1147 if (MLX5_CAP_GEN(mdev, striding_rq)) {
1148 resp.striding_rq_caps.min_single_stride_log_num_of_bytes =
1149 MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES;
1150 resp.striding_rq_caps.max_single_stride_log_num_of_bytes =
1151 MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES;
Mark Zhangc16339b2019-11-15 17:45:55 +02001152 if (MLX5_CAP_GEN(dev->mdev, ext_stride_num_range))
1153 resp.striding_rq_caps
1154 .min_single_wqe_log_num_of_strides =
1155 MLX5_EXT_MIN_SINGLE_WQE_LOG_NUM_STRIDES;
1156 else
1157 resp.striding_rq_caps
1158 .min_single_wqe_log_num_of_strides =
1159 MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES;
Noa Osherovichb4f34592017-10-17 18:01:12 +03001160 resp.striding_rq_caps.max_single_wqe_log_num_of_strides =
1161 MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES;
1162 resp.striding_rq_caps.supported_qpts =
1163 BIT(IB_QPT_RAW_PACKET);
1164 }
1165 }
1166
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001167 if (field_avail(typeof(resp), tunnel_offloads_caps,
1168 uhw->outlen)) {
1169 resp.response_length += sizeof(resp.tunnel_offloads_caps);
1170 if (MLX5_CAP_ETH(mdev, tunnel_stateless_vxlan))
1171 resp.tunnel_offloads_caps |=
1172 MLX5_IB_TUNNELED_OFFLOADS_VXLAN;
1173 if (MLX5_CAP_ETH(mdev, tunnel_stateless_geneve_rx))
1174 resp.tunnel_offloads_caps |=
1175 MLX5_IB_TUNNELED_OFFLOADS_GENEVE;
1176 if (MLX5_CAP_ETH(mdev, tunnel_stateless_gre))
1177 resp.tunnel_offloads_caps |=
1178 MLX5_IB_TUNNELED_OFFLOADS_GRE;
Ariel Levkoviche818e252018-05-13 14:33:35 +03001179 if (MLX5_CAP_GEN(mdev, flex_parser_protocols) &
1180 MLX5_FLEX_PROTO_CW_MPLS_GRE)
1181 resp.tunnel_offloads_caps |=
1182 MLX5_IB_TUNNELED_OFFLOADS_MPLS_GRE;
1183 if (MLX5_CAP_GEN(mdev, flex_parser_protocols) &
1184 MLX5_FLEX_PROTO_CW_MPLS_UDP)
1185 resp.tunnel_offloads_caps |=
1186 MLX5_IB_TUNNELED_OFFLOADS_MPLS_UDP;
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001187 }
1188
Bodong Wang402ca532016-06-17 15:02:20 +03001189 if (uhw->outlen) {
1190 err = ib_copy_to_udata(uhw, &resp, resp.response_length);
1191
1192 if (err)
1193 return err;
1194 }
1195
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001196 return 0;
1197}
Eli Cohene126ba92013-07-07 17:25:49 +03001198
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001199enum mlx5_ib_width {
1200 MLX5_IB_WIDTH_1X = 1 << 0,
1201 MLX5_IB_WIDTH_2X = 1 << 1,
1202 MLX5_IB_WIDTH_4X = 1 << 2,
1203 MLX5_IB_WIDTH_8X = 1 << 3,
1204 MLX5_IB_WIDTH_12X = 1 << 4
1205};
1206
Michael Guralnikdb7a6912018-11-21 15:03:54 +02001207static void translate_active_width(struct ib_device *ibdev, u8 active_width,
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001208 u8 *ib_width)
1209{
1210 struct mlx5_ib_dev *dev = to_mdev(ibdev);
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001211
Michael Guralnikdb7a6912018-11-21 15:03:54 +02001212 if (active_width & MLX5_IB_WIDTH_1X)
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001213 *ib_width = IB_WIDTH_1X;
Michael Guralnikd7649702018-12-09 11:49:54 +02001214 else if (active_width & MLX5_IB_WIDTH_2X)
1215 *ib_width = IB_WIDTH_2X;
Michael Guralnikdb7a6912018-11-21 15:03:54 +02001216 else if (active_width & MLX5_IB_WIDTH_4X)
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001217 *ib_width = IB_WIDTH_4X;
Michael Guralnikdb7a6912018-11-21 15:03:54 +02001218 else if (active_width & MLX5_IB_WIDTH_8X)
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001219 *ib_width = IB_WIDTH_8X;
Michael Guralnikdb7a6912018-11-21 15:03:54 +02001220 else if (active_width & MLX5_IB_WIDTH_12X)
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001221 *ib_width = IB_WIDTH_12X;
Michael Guralnikdb7a6912018-11-21 15:03:54 +02001222 else {
1223 mlx5_ib_dbg(dev, "Invalid active_width %d, setting width to default value: 4x\n",
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001224 (int)active_width);
Michael Guralnikdb7a6912018-11-21 15:03:54 +02001225 *ib_width = IB_WIDTH_4X;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001226 }
1227
Michael Guralnikdb7a6912018-11-21 15:03:54 +02001228 return;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001229}
1230
1231static int mlx5_mtu_to_ib_mtu(int mtu)
1232{
1233 switch (mtu) {
1234 case 256: return 1;
1235 case 512: return 2;
1236 case 1024: return 3;
1237 case 2048: return 4;
1238 case 4096: return 5;
1239 default:
1240 pr_warn("invalid mtu\n");
1241 return -1;
1242 }
1243}
1244
1245enum ib_max_vl_num {
1246 __IB_MAX_VL_0 = 1,
1247 __IB_MAX_VL_0_1 = 2,
1248 __IB_MAX_VL_0_3 = 3,
1249 __IB_MAX_VL_0_7 = 4,
1250 __IB_MAX_VL_0_14 = 5,
1251};
1252
1253enum mlx5_vl_hw_cap {
1254 MLX5_VL_HW_0 = 1,
1255 MLX5_VL_HW_0_1 = 2,
1256 MLX5_VL_HW_0_2 = 3,
1257 MLX5_VL_HW_0_3 = 4,
1258 MLX5_VL_HW_0_4 = 5,
1259 MLX5_VL_HW_0_5 = 6,
1260 MLX5_VL_HW_0_6 = 7,
1261 MLX5_VL_HW_0_7 = 8,
1262 MLX5_VL_HW_0_14 = 15
1263};
1264
1265static int translate_max_vl_num(struct ib_device *ibdev, u8 vl_hw_cap,
1266 u8 *max_vl_num)
1267{
1268 switch (vl_hw_cap) {
1269 case MLX5_VL_HW_0:
1270 *max_vl_num = __IB_MAX_VL_0;
1271 break;
1272 case MLX5_VL_HW_0_1:
1273 *max_vl_num = __IB_MAX_VL_0_1;
1274 break;
1275 case MLX5_VL_HW_0_3:
1276 *max_vl_num = __IB_MAX_VL_0_3;
1277 break;
1278 case MLX5_VL_HW_0_7:
1279 *max_vl_num = __IB_MAX_VL_0_7;
1280 break;
1281 case MLX5_VL_HW_0_14:
1282 *max_vl_num = __IB_MAX_VL_0_14;
1283 break;
1284
1285 default:
1286 return -EINVAL;
1287 }
1288
1289 return 0;
1290}
1291
1292static int mlx5_query_hca_port(struct ib_device *ibdev, u8 port,
1293 struct ib_port_attr *props)
1294{
1295 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1296 struct mlx5_core_dev *mdev = dev->mdev;
1297 struct mlx5_hca_vport_context *rep;
Saeed Mahameed046339e2016-04-22 00:33:03 +03001298 u16 max_mtu;
1299 u16 oper_mtu;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001300 int err;
1301 u8 ib_link_width_oper;
1302 u8 vl_hw_cap;
1303
1304 rep = kzalloc(sizeof(*rep), GFP_KERNEL);
1305 if (!rep) {
1306 err = -ENOMEM;
1307 goto out;
1308 }
1309
Or Gerlitzc4550c62017-01-24 13:02:39 +02001310 /* props being zeroed by the caller, avoid zeroing it here */
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001311
1312 err = mlx5_query_hca_vport_context(mdev, 0, port, 0, rep);
1313 if (err)
1314 goto out;
1315
1316 props->lid = rep->lid;
1317 props->lmc = rep->lmc;
1318 props->sm_lid = rep->sm_lid;
1319 props->sm_sl = rep->sm_sl;
1320 props->state = rep->vport_state;
1321 props->phys_state = rep->port_physical_state;
1322 props->port_cap_flags = rep->cap_mask1;
1323 props->gid_tbl_len = mlx5_get_gid_table_len(MLX5_CAP_GEN(mdev, gid_table_size));
1324 props->max_msg_sz = 1 << MLX5_CAP_GEN(mdev, log_max_msg);
1325 props->pkey_tbl_len = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, pkey_table_size));
1326 props->bad_pkey_cntr = rep->pkey_violation_counter;
1327 props->qkey_viol_cntr = rep->qkey_violation_counter;
1328 props->subnet_timeout = rep->subnet_timeout;
1329 props->init_type_reply = rep->init_type_reply;
1330
Michael Guralnik4106a752018-12-09 11:49:51 +02001331 if (props->port_cap_flags & IB_PORT_CAP_MASK2_SUP)
1332 props->port_cap_flags2 = rep->cap_mask2;
1333
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001334 err = mlx5_query_port_link_width_oper(mdev, &ib_link_width_oper, port);
1335 if (err)
1336 goto out;
1337
Michael Guralnikdb7a6912018-11-21 15:03:54 +02001338 translate_active_width(ibdev, ib_link_width_oper, &props->active_width);
1339
Noa Osherovichd5beb7f2016-06-02 10:47:53 +03001340 err = mlx5_query_port_ib_proto_oper(mdev, &props->active_speed, port);
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001341 if (err)
1342 goto out;
1343
Saeed Mahameedfacc9692015-06-11 14:47:27 +03001344 mlx5_query_port_max_mtu(mdev, &max_mtu, port);
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001345
1346 props->max_mtu = mlx5_mtu_to_ib_mtu(max_mtu);
1347
Saeed Mahameedfacc9692015-06-11 14:47:27 +03001348 mlx5_query_port_oper_mtu(mdev, &oper_mtu, port);
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001349
1350 props->active_mtu = mlx5_mtu_to_ib_mtu(oper_mtu);
1351
1352 err = mlx5_query_port_vl_hw_cap(mdev, &vl_hw_cap, port);
1353 if (err)
1354 goto out;
1355
1356 err = translate_max_vl_num(ibdev, vl_hw_cap,
1357 &props->max_vl_num);
1358out:
1359 kfree(rep);
Eli Cohene126ba92013-07-07 17:25:49 +03001360 return err;
1361}
1362
1363int mlx5_ib_query_port(struct ib_device *ibdev, u8 port,
1364 struct ib_port_attr *props)
1365{
Ilan Tayari095b0922017-05-14 16:04:30 +03001366 unsigned int count;
1367 int ret;
1368
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001369 switch (mlx5_get_vport_access_method(ibdev)) {
1370 case MLX5_VPORT_ACCESS_METHOD_MAD:
Ilan Tayari095b0922017-05-14 16:04:30 +03001371 ret = mlx5_query_mad_ifc_port(ibdev, port, props);
1372 break;
Eli Cohene126ba92013-07-07 17:25:49 +03001373
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001374 case MLX5_VPORT_ACCESS_METHOD_HCA:
Ilan Tayari095b0922017-05-14 16:04:30 +03001375 ret = mlx5_query_hca_port(ibdev, port, props);
1376 break;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001377
Achiad Shochat3f89a642015-12-23 18:47:21 +02001378 case MLX5_VPORT_ACCESS_METHOD_NIC:
Ilan Tayari095b0922017-05-14 16:04:30 +03001379 ret = mlx5_query_port_roce(ibdev, port, props);
1380 break;
Achiad Shochat3f89a642015-12-23 18:47:21 +02001381
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001382 default:
Ilan Tayari095b0922017-05-14 16:04:30 +03001383 ret = -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +03001384 }
Ilan Tayari095b0922017-05-14 16:04:30 +03001385
1386 if (!ret && props) {
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001387 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1388 struct mlx5_core_dev *mdev;
1389 bool put_mdev = true;
1390
1391 mdev = mlx5_ib_get_native_port_mdev(dev, port, NULL);
1392 if (!mdev) {
1393 /* If the port isn't affiliated yet query the master.
1394 * The master and slave will have the same values.
1395 */
1396 mdev = dev->mdev;
1397 port = 1;
1398 put_mdev = false;
1399 }
1400 count = mlx5_core_reserved_gids_count(mdev);
1401 if (put_mdev)
1402 mlx5_ib_put_native_port_mdev(dev, port);
Ilan Tayari095b0922017-05-14 16:04:30 +03001403 props->gid_tbl_len -= count;
1404 }
1405 return ret;
Eli Cohene126ba92013-07-07 17:25:49 +03001406}
1407
Mark Bloch8e6efa32017-11-06 12:22:13 +00001408static int mlx5_ib_rep_query_port(struct ib_device *ibdev, u8 port,
1409 struct ib_port_attr *props)
1410{
1411 int ret;
1412
Mark Bloch26628e22019-03-28 15:27:41 +02001413 /* Only link layer == ethernet is valid for representors
1414 * and we always use port 1
1415 */
Mark Bloch8e6efa32017-11-06 12:22:13 +00001416 ret = mlx5_query_port_roce(ibdev, port, props);
1417 if (ret || !props)
1418 return ret;
1419
1420 /* We don't support GIDS */
1421 props->gid_tbl_len = 0;
1422
1423 return ret;
1424}
1425
Eli Cohene126ba92013-07-07 17:25:49 +03001426static int mlx5_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
1427 union ib_gid *gid)
1428{
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001429 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1430 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03001431
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001432 switch (mlx5_get_vport_access_method(ibdev)) {
1433 case MLX5_VPORT_ACCESS_METHOD_MAD:
1434 return mlx5_query_mad_ifc_gids(ibdev, port, index, gid);
Eli Cohene126ba92013-07-07 17:25:49 +03001435
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001436 case MLX5_VPORT_ACCESS_METHOD_HCA:
1437 return mlx5_query_hca_vport_gid(mdev, 0, port, 0, index, gid);
Eli Cohene126ba92013-07-07 17:25:49 +03001438
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001439 default:
1440 return -EINVAL;
1441 }
Eli Cohene126ba92013-07-07 17:25:49 +03001442
Eli Cohene126ba92013-07-07 17:25:49 +03001443}
1444
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001445static int mlx5_query_hca_nic_pkey(struct ib_device *ibdev, u8 port,
1446 u16 index, u16 *pkey)
1447{
1448 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1449 struct mlx5_core_dev *mdev;
1450 bool put_mdev = true;
1451 u8 mdev_port_num;
1452 int err;
1453
1454 mdev = mlx5_ib_get_native_port_mdev(dev, port, &mdev_port_num);
1455 if (!mdev) {
1456 /* The port isn't affiliated yet, get the PKey from the master
1457 * port. For RoCE the PKey tables will be the same.
1458 */
1459 put_mdev = false;
1460 mdev = dev->mdev;
1461 mdev_port_num = 1;
1462 }
1463
1464 err = mlx5_query_hca_vport_pkey(mdev, 0, mdev_port_num, 0,
1465 index, pkey);
1466 if (put_mdev)
1467 mlx5_ib_put_native_port_mdev(dev, port);
1468
1469 return err;
1470}
1471
Eli Cohene126ba92013-07-07 17:25:49 +03001472static int mlx5_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
1473 u16 *pkey)
1474{
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001475 switch (mlx5_get_vport_access_method(ibdev)) {
1476 case MLX5_VPORT_ACCESS_METHOD_MAD:
1477 return mlx5_query_mad_ifc_pkey(ibdev, port, index, pkey);
Eli Cohene126ba92013-07-07 17:25:49 +03001478
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001479 case MLX5_VPORT_ACCESS_METHOD_HCA:
1480 case MLX5_VPORT_ACCESS_METHOD_NIC:
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001481 return mlx5_query_hca_nic_pkey(ibdev, port, index, pkey);
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001482 default:
1483 return -EINVAL;
1484 }
Eli Cohene126ba92013-07-07 17:25:49 +03001485}
1486
Eli Cohene126ba92013-07-07 17:25:49 +03001487static int mlx5_ib_modify_device(struct ib_device *ibdev, int mask,
1488 struct ib_device_modify *props)
1489{
1490 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1491 struct mlx5_reg_node_desc in;
1492 struct mlx5_reg_node_desc out;
1493 int err;
1494
1495 if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
1496 return -EOPNOTSUPP;
1497
1498 if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
1499 return 0;
1500
1501 /*
1502 * If possible, pass node desc to FW, so it can generate
1503 * a 144 trap. If cmd fails, just ignore.
1504 */
Yuval Shaiabd99fde2016-08-25 10:57:07 -07001505 memcpy(&in, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
Jack Morgenstein9603b612014-07-28 23:30:22 +03001506 err = mlx5_core_access_reg(dev->mdev, &in, sizeof(in), &out,
Eli Cohene126ba92013-07-07 17:25:49 +03001507 sizeof(out), MLX5_REG_NODE_DESC, 0, 1);
1508 if (err)
1509 return err;
1510
Yuval Shaiabd99fde2016-08-25 10:57:07 -07001511 memcpy(ibdev->node_desc, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
Eli Cohene126ba92013-07-07 17:25:49 +03001512
1513 return err;
1514}
1515
Eli Cohencdbe33d2017-02-14 07:25:38 +02001516static int set_port_caps_atomic(struct mlx5_ib_dev *dev, u8 port_num, u32 mask,
1517 u32 value)
1518{
1519 struct mlx5_hca_vport_context ctx = {};
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001520 struct mlx5_core_dev *mdev;
1521 u8 mdev_port_num;
Eli Cohencdbe33d2017-02-14 07:25:38 +02001522 int err;
1523
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001524 mdev = mlx5_ib_get_native_port_mdev(dev, port_num, &mdev_port_num);
1525 if (!mdev)
1526 return -ENODEV;
1527
1528 err = mlx5_query_hca_vport_context(mdev, 0, mdev_port_num, 0, &ctx);
Eli Cohencdbe33d2017-02-14 07:25:38 +02001529 if (err)
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001530 goto out;
Eli Cohencdbe33d2017-02-14 07:25:38 +02001531
1532 if (~ctx.cap_mask1_perm & mask) {
1533 mlx5_ib_warn(dev, "trying to change bitmask 0x%X but change supported 0x%X\n",
1534 mask, ctx.cap_mask1_perm);
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001535 err = -EINVAL;
1536 goto out;
Eli Cohencdbe33d2017-02-14 07:25:38 +02001537 }
1538
1539 ctx.cap_mask1 = value;
1540 ctx.cap_mask1_perm = mask;
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001541 err = mlx5_core_modify_hca_vport_context(mdev, 0, mdev_port_num,
1542 0, &ctx);
1543
1544out:
1545 mlx5_ib_put_native_port_mdev(dev, port_num);
Eli Cohencdbe33d2017-02-14 07:25:38 +02001546
1547 return err;
1548}
1549
Eli Cohene126ba92013-07-07 17:25:49 +03001550static int mlx5_ib_modify_port(struct ib_device *ibdev, u8 port, int mask,
1551 struct ib_port_modify *props)
1552{
1553 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1554 struct ib_port_attr attr;
1555 u32 tmp;
1556 int err;
Eli Cohencdbe33d2017-02-14 07:25:38 +02001557 u32 change_mask;
1558 u32 value;
1559 bool is_ib = (mlx5_ib_port_link_layer(ibdev, port) ==
1560 IB_LINK_LAYER_INFINIBAND);
1561
Majd Dibbinyec255872017-08-23 08:35:42 +03001562 /* CM layer calls ib_modify_port() regardless of the link layer. For
1563 * Ethernet ports, qkey violation and Port capabilities are meaningless.
1564 */
1565 if (!is_ib)
1566 return 0;
1567
Eli Cohencdbe33d2017-02-14 07:25:38 +02001568 if (MLX5_CAP_GEN(dev->mdev, ib_virt) && is_ib) {
1569 change_mask = props->clr_port_cap_mask | props->set_port_cap_mask;
1570 value = ~props->clr_port_cap_mask | props->set_port_cap_mask;
1571 return set_port_caps_atomic(dev, port, change_mask, value);
1572 }
Eli Cohene126ba92013-07-07 17:25:49 +03001573
1574 mutex_lock(&dev->cap_mask_mutex);
1575
Or Gerlitzc4550c62017-01-24 13:02:39 +02001576 err = ib_query_port(ibdev, port, &attr);
Eli Cohene126ba92013-07-07 17:25:49 +03001577 if (err)
1578 goto out;
1579
1580 tmp = (attr.port_cap_flags | props->set_port_cap_mask) &
1581 ~props->clr_port_cap_mask;
1582
Jack Morgenstein9603b612014-07-28 23:30:22 +03001583 err = mlx5_set_port_caps(dev->mdev, port, tmp);
Eli Cohene126ba92013-07-07 17:25:49 +03001584
1585out:
1586 mutex_unlock(&dev->cap_mask_mutex);
1587 return err;
1588}
1589
Eli Cohen30aa60b2017-01-03 23:55:27 +02001590static void print_lib_caps(struct mlx5_ib_dev *dev, u64 caps)
1591{
1592 mlx5_ib_dbg(dev, "MLX5_LIB_CAP_4K_UAR = %s\n",
1593 caps & MLX5_LIB_CAP_4K_UAR ? "y" : "n");
1594}
1595
Yishai Hadas31a78a52017-12-24 16:31:34 +02001596static u16 calc_dynamic_bfregs(int uars_per_sys_page)
1597{
1598 /* Large page with non 4k uar support might limit the dynamic size */
1599 if (uars_per_sys_page == 1 && PAGE_SIZE > 4096)
1600 return MLX5_MIN_DYN_BFREGS;
1601
1602 return MLX5_MAX_DYN_BFREGS;
1603}
1604
Eli Cohenb037c292017-01-03 23:55:26 +02001605static int calc_total_bfregs(struct mlx5_ib_dev *dev, bool lib_uar_4k,
1606 struct mlx5_ib_alloc_ucontext_req_v2 *req,
Yishai Hadas31a78a52017-12-24 16:31:34 +02001607 struct mlx5_bfreg_info *bfregi)
Eli Cohenb037c292017-01-03 23:55:26 +02001608{
1609 int uars_per_sys_page;
1610 int bfregs_per_sys_page;
1611 int ref_bfregs = req->total_num_bfregs;
1612
1613 if (req->total_num_bfregs == 0)
1614 return -EINVAL;
1615
1616 BUILD_BUG_ON(MLX5_MAX_BFREGS % MLX5_NON_FP_BFREGS_IN_PAGE);
1617 BUILD_BUG_ON(MLX5_MAX_BFREGS < MLX5_NON_FP_BFREGS_IN_PAGE);
1618
1619 if (req->total_num_bfregs > MLX5_MAX_BFREGS)
1620 return -ENOMEM;
1621
1622 uars_per_sys_page = get_uars_per_sys_page(dev, lib_uar_4k);
1623 bfregs_per_sys_page = uars_per_sys_page * MLX5_NON_FP_BFREGS_PER_UAR;
Yishai Hadas31a78a52017-12-24 16:31:34 +02001624 /* This holds the required static allocation asked by the user */
Eli Cohenb037c292017-01-03 23:55:26 +02001625 req->total_num_bfregs = ALIGN(req->total_num_bfregs, bfregs_per_sys_page);
Eli Cohenb037c292017-01-03 23:55:26 +02001626 if (req->num_low_latency_bfregs > req->total_num_bfregs - 1)
1627 return -EINVAL;
1628
Yishai Hadas31a78a52017-12-24 16:31:34 +02001629 bfregi->num_static_sys_pages = req->total_num_bfregs / bfregs_per_sys_page;
1630 bfregi->num_dyn_bfregs = ALIGN(calc_dynamic_bfregs(uars_per_sys_page), bfregs_per_sys_page);
1631 bfregi->total_num_bfregs = req->total_num_bfregs + bfregi->num_dyn_bfregs;
1632 bfregi->num_sys_pages = bfregi->total_num_bfregs / bfregs_per_sys_page;
1633
1634 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",
Eli Cohenb037c292017-01-03 23:55:26 +02001635 MLX5_CAP_GEN(dev->mdev, uar_4k) ? "yes" : "no",
1636 lib_uar_4k ? "yes" : "no", ref_bfregs,
Yishai Hadas31a78a52017-12-24 16:31:34 +02001637 req->total_num_bfregs, bfregi->total_num_bfregs,
1638 bfregi->num_sys_pages);
Eli Cohenb037c292017-01-03 23:55:26 +02001639
1640 return 0;
1641}
1642
1643static int allocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
1644{
1645 struct mlx5_bfreg_info *bfregi;
1646 int err;
1647 int i;
1648
1649 bfregi = &context->bfregi;
Yishai Hadas31a78a52017-12-24 16:31:34 +02001650 for (i = 0; i < bfregi->num_static_sys_pages; i++) {
Eli Cohenb037c292017-01-03 23:55:26 +02001651 err = mlx5_cmd_alloc_uar(dev->mdev, &bfregi->sys_pages[i]);
1652 if (err)
1653 goto error;
1654
1655 mlx5_ib_dbg(dev, "allocated uar %d\n", bfregi->sys_pages[i]);
1656 }
Yishai Hadas4ed131d2017-12-24 16:31:35 +02001657
1658 for (i = bfregi->num_static_sys_pages; i < bfregi->num_sys_pages; i++)
1659 bfregi->sys_pages[i] = MLX5_IB_INVALID_UAR_INDEX;
1660
Eli Cohenb037c292017-01-03 23:55:26 +02001661 return 0;
1662
1663error:
1664 for (--i; i >= 0; i--)
1665 if (mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]))
1666 mlx5_ib_warn(dev, "failed to free uar %d\n", i);
1667
1668 return err;
1669}
1670
Leon Romanovsky15177992018-06-27 10:44:24 +03001671static void deallocate_uars(struct mlx5_ib_dev *dev,
1672 struct mlx5_ib_ucontext *context)
Eli Cohenb037c292017-01-03 23:55:26 +02001673{
1674 struct mlx5_bfreg_info *bfregi;
Eli Cohenb037c292017-01-03 23:55:26 +02001675 int i;
1676
1677 bfregi = &context->bfregi;
Leon Romanovsky15177992018-06-27 10:44:24 +03001678 for (i = 0; i < bfregi->num_sys_pages; i++)
Yishai Hadas4ed131d2017-12-24 16:31:35 +02001679 if (i < bfregi->num_static_sys_pages ||
Leon Romanovsky15177992018-06-27 10:44:24 +03001680 bfregi->sys_pages[i] != MLX5_IB_INVALID_UAR_INDEX)
1681 mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]);
Eli Cohenb037c292017-01-03 23:55:26 +02001682}
1683
Mark Bloch0042f9e2018-09-17 13:30:49 +03001684int mlx5_ib_enable_lb(struct mlx5_ib_dev *dev, bool td, bool qp)
Mark Blocha560f1d2018-09-17 13:30:47 +03001685{
1686 int err = 0;
1687
1688 mutex_lock(&dev->lb.mutex);
Mark Bloch0042f9e2018-09-17 13:30:49 +03001689 if (td)
1690 dev->lb.user_td++;
1691 if (qp)
1692 dev->lb.qps++;
Mark Blocha560f1d2018-09-17 13:30:47 +03001693
Mark Bloch0042f9e2018-09-17 13:30:49 +03001694 if (dev->lb.user_td == 2 ||
1695 dev->lb.qps == 1) {
1696 if (!dev->lb.enabled) {
1697 err = mlx5_nic_vport_update_local_lb(dev->mdev, true);
1698 dev->lb.enabled = true;
1699 }
1700 }
Mark Blocha560f1d2018-09-17 13:30:47 +03001701
1702 mutex_unlock(&dev->lb.mutex);
1703
1704 return err;
1705}
1706
Mark Bloch0042f9e2018-09-17 13:30:49 +03001707void mlx5_ib_disable_lb(struct mlx5_ib_dev *dev, bool td, bool qp)
Mark Blocha560f1d2018-09-17 13:30:47 +03001708{
1709 mutex_lock(&dev->lb.mutex);
Mark Bloch0042f9e2018-09-17 13:30:49 +03001710 if (td)
1711 dev->lb.user_td--;
1712 if (qp)
1713 dev->lb.qps--;
Mark Blocha560f1d2018-09-17 13:30:47 +03001714
Mark Bloch0042f9e2018-09-17 13:30:49 +03001715 if (dev->lb.user_td == 1 &&
1716 dev->lb.qps == 0) {
1717 if (dev->lb.enabled) {
1718 mlx5_nic_vport_update_local_lb(dev->mdev, false);
1719 dev->lb.enabled = false;
1720 }
1721 }
Mark Blocha560f1d2018-09-17 13:30:47 +03001722
1723 mutex_unlock(&dev->lb.mutex);
1724}
1725
Yishai Hadasd2d19122018-09-20 21:39:32 +03001726static int mlx5_ib_alloc_transport_domain(struct mlx5_ib_dev *dev, u32 *tdn,
1727 u16 uid)
Huy Nguyenc85023e2017-05-30 09:42:54 +03001728{
1729 int err;
1730
Leon Romanovskycfdeb892018-06-19 10:39:06 +03001731 if (!MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1732 return 0;
1733
Yishai Hadasd2d19122018-09-20 21:39:32 +03001734 err = mlx5_cmd_alloc_transport_domain(dev->mdev, tdn, uid);
Huy Nguyenc85023e2017-05-30 09:42:54 +03001735 if (err)
1736 return err;
1737
1738 if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
Eran Ben Elisha8978cc92018-01-09 11:41:10 +02001739 (!MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) &&
1740 !MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
Huy Nguyenc85023e2017-05-30 09:42:54 +03001741 return err;
1742
Mark Bloch0042f9e2018-09-17 13:30:49 +03001743 return mlx5_ib_enable_lb(dev, true, false);
Huy Nguyenc85023e2017-05-30 09:42:54 +03001744}
1745
Yishai Hadasd2d19122018-09-20 21:39:32 +03001746static void mlx5_ib_dealloc_transport_domain(struct mlx5_ib_dev *dev, u32 tdn,
1747 u16 uid)
Huy Nguyenc85023e2017-05-30 09:42:54 +03001748{
Leon Romanovskycfdeb892018-06-19 10:39:06 +03001749 if (!MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1750 return;
1751
Yishai Hadasd2d19122018-09-20 21:39:32 +03001752 mlx5_cmd_dealloc_transport_domain(dev->mdev, tdn, uid);
Huy Nguyenc85023e2017-05-30 09:42:54 +03001753
1754 if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
Eran Ben Elisha8978cc92018-01-09 11:41:10 +02001755 (!MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) &&
1756 !MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
Huy Nguyenc85023e2017-05-30 09:42:54 +03001757 return;
1758
Mark Bloch0042f9e2018-09-17 13:30:49 +03001759 mlx5_ib_disable_lb(dev, true, false);
Huy Nguyenc85023e2017-05-30 09:42:54 +03001760}
1761
Leon Romanovskya2a074e2019-02-12 20:39:16 +02001762static int mlx5_ib_alloc_ucontext(struct ib_ucontext *uctx,
1763 struct ib_udata *udata)
Eli Cohene126ba92013-07-07 17:25:49 +03001764{
Leon Romanovskya2a074e2019-02-12 20:39:16 +02001765 struct ib_device *ibdev = uctx->device;
Eli Cohene126ba92013-07-07 17:25:49 +03001766 struct mlx5_ib_dev *dev = to_mdev(ibdev);
Matan Barakb368d7c2015-12-15 20:30:12 +02001767 struct mlx5_ib_alloc_ucontext_req_v2 req = {};
1768 struct mlx5_ib_alloc_ucontext_resp resp = {};
Feras Daoud5c99eae2018-01-16 20:08:41 +02001769 struct mlx5_core_dev *mdev = dev->mdev;
Leon Romanovskya2a074e2019-02-12 20:39:16 +02001770 struct mlx5_ib_ucontext *context = to_mucontext(uctx);
Eli Cohen2f5ff262017-01-03 23:55:21 +02001771 struct mlx5_bfreg_info *bfregi;
Eli Cohen78c0f982014-01-30 13:49:48 +02001772 int ver;
Eli Cohene126ba92013-07-07 17:25:49 +03001773 int err;
Majd Dibbinya168a41c2016-01-28 17:51:47 +02001774 size_t min_req_v2 = offsetof(struct mlx5_ib_alloc_ucontext_req_v2,
1775 max_cqe_version);
Yonatan Cohen25bb36e2018-06-19 08:47:24 +03001776 u32 dump_fill_mkey;
Eli Cohenb037c292017-01-03 23:55:26 +02001777 bool lib_uar_4k;
Eli Cohene126ba92013-07-07 17:25:49 +03001778
1779 if (!dev->ib_active)
Leon Romanovskya2a074e2019-02-12 20:39:16 +02001780 return -EAGAIN;
Eli Cohene126ba92013-07-07 17:25:49 +03001781
Amrani, Rame0931112017-06-27 17:04:42 +03001782 if (udata->inlen == sizeof(struct mlx5_ib_alloc_ucontext_req))
Eli Cohen78c0f982014-01-30 13:49:48 +02001783 ver = 0;
Amrani, Rame0931112017-06-27 17:04:42 +03001784 else if (udata->inlen >= min_req_v2)
Eli Cohen78c0f982014-01-30 13:49:48 +02001785 ver = 2;
1786 else
Leon Romanovskya2a074e2019-02-12 20:39:16 +02001787 return -EINVAL;
Eli Cohen78c0f982014-01-30 13:49:48 +02001788
Amrani, Rame0931112017-06-27 17:04:42 +03001789 err = ib_copy_from_udata(&req, udata, min(udata->inlen, sizeof(req)));
Eli Cohene126ba92013-07-07 17:25:49 +03001790 if (err)
Leon Romanovskya2a074e2019-02-12 20:39:16 +02001791 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03001792
Yishai Hadasa8b92ca2018-06-17 12:59:57 +03001793 if (req.flags & ~MLX5_IB_ALLOC_UCTX_DEVX)
Leon Romanovskya2a074e2019-02-12 20:39:16 +02001794 return -EOPNOTSUPP;
Eli Cohen78c0f982014-01-30 13:49:48 +02001795
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001796 if (req.comp_mask || req.reserved0 || req.reserved1 || req.reserved2)
Leon Romanovskya2a074e2019-02-12 20:39:16 +02001797 return -EOPNOTSUPP;
Matan Barakb368d7c2015-12-15 20:30:12 +02001798
Eli Cohen2f5ff262017-01-03 23:55:21 +02001799 req.total_num_bfregs = ALIGN(req.total_num_bfregs,
1800 MLX5_NON_FP_BFREGS_PER_UAR);
1801 if (req.num_low_latency_bfregs > req.total_num_bfregs - 1)
Leon Romanovskya2a074e2019-02-12 20:39:16 +02001802 return -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +03001803
Saeed Mahameed938fe832015-05-28 22:28:41 +03001804 resp.qp_tab_size = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp);
Michael Guralnik11f552e2019-06-10 15:21:24 +03001805 if (dev->wc_support)
Noa Osherovich2cc6ad52016-06-04 15:15:33 +03001806 resp.bf_reg_size = 1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size);
Daniel Jurgensb47bd6e2016-10-25 18:36:24 +03001807 resp.cache_line_size = cache_line_size();
Saeed Mahameed938fe832015-05-28 22:28:41 +03001808 resp.max_sq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq);
1809 resp.max_rq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq);
1810 resp.max_send_wqebb = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1811 resp.max_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1812 resp.max_srq_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz);
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001813 resp.cqe_version = min_t(__u8,
1814 (__u8)MLX5_CAP_GEN(dev->mdev, cqe_version),
1815 req.max_cqe_version);
Eli Cohen30aa60b2017-01-03 23:55:27 +02001816 resp.log_uar_size = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
1817 MLX5_ADAPTER_PAGE_SHIFT : PAGE_SHIFT;
1818 resp.num_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
1819 MLX5_CAP_GEN(dev->mdev, num_of_uars_per_page) : 1;
Matan Barakb368d7c2015-12-15 20:30:12 +02001820 resp.response_length = min(offsetof(typeof(resp), response_length) +
1821 sizeof(resp.response_length), udata->outlen);
Eli Cohene126ba92013-07-07 17:25:49 +03001822
Matan Barakc03faa52018-03-28 09:27:54 +03001823 if (mlx5_accel_ipsec_device_caps(dev->mdev) & MLX5_ACCEL_IPSEC_CAP_DEVICE) {
1824 if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_EGRESS))
1825 resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM;
1826 if (mlx5_accel_ipsec_device_caps(dev->mdev) & MLX5_ACCEL_IPSEC_CAP_REQUIRED_METADATA)
1827 resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_REQ_METADATA;
1828 if (MLX5_CAP_FLOWTABLE(dev->mdev, flow_table_properties_nic_receive.ft_field_support.outer_esp_spi))
1829 resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_SPI_STEERING;
1830 if (mlx5_accel_ipsec_device_caps(dev->mdev) & MLX5_ACCEL_IPSEC_CAP_TX_IV_IS_ESN)
1831 resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_TX_IV_IS_ESN;
1832 /* MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_FULL_OFFLOAD is currently always 0 */
1833 }
1834
Eli Cohen30aa60b2017-01-03 23:55:27 +02001835 lib_uar_4k = req.lib_caps & MLX5_LIB_CAP_4K_UAR;
Eli Cohen2f5ff262017-01-03 23:55:21 +02001836 bfregi = &context->bfregi;
Eli Cohenb037c292017-01-03 23:55:26 +02001837
1838 /* updates req->total_num_bfregs */
Yishai Hadas31a78a52017-12-24 16:31:34 +02001839 err = calc_total_bfregs(dev, lib_uar_4k, &req, bfregi);
Eli Cohenb037c292017-01-03 23:55:26 +02001840 if (err)
1841 goto out_ctx;
1842
Eli Cohen2f5ff262017-01-03 23:55:21 +02001843 mutex_init(&bfregi->lock);
Eli Cohenb037c292017-01-03 23:55:26 +02001844 bfregi->lib_uar_4k = lib_uar_4k;
Yishai Hadas31a78a52017-12-24 16:31:34 +02001845 bfregi->count = kcalloc(bfregi->total_num_bfregs, sizeof(*bfregi->count),
Eli Cohenb037c292017-01-03 23:55:26 +02001846 GFP_KERNEL);
1847 if (!bfregi->count) {
Eli Cohene126ba92013-07-07 17:25:49 +03001848 err = -ENOMEM;
1849 goto out_ctx;
1850 }
1851
Eli Cohenb037c292017-01-03 23:55:26 +02001852 bfregi->sys_pages = kcalloc(bfregi->num_sys_pages,
1853 sizeof(*bfregi->sys_pages),
1854 GFP_KERNEL);
1855 if (!bfregi->sys_pages) {
Eli Cohene126ba92013-07-07 17:25:49 +03001856 err = -ENOMEM;
Eli Cohenb037c292017-01-03 23:55:26 +02001857 goto out_count;
Eli Cohene126ba92013-07-07 17:25:49 +03001858 }
1859
Eli Cohenb037c292017-01-03 23:55:26 +02001860 err = allocate_uars(dev, context);
1861 if (err)
1862 goto out_sys_pages;
Eli Cohene126ba92013-07-07 17:25:49 +03001863
Yishai Hadasa8b92ca2018-06-17 12:59:57 +03001864 if (req.flags & MLX5_IB_ALLOC_UCTX_DEVX) {
Yishai Hadasfb981532018-11-26 08:28:36 +02001865 err = mlx5_ib_devx_create(dev, true);
Yishai Hadas76dc5a82018-09-20 21:45:19 +03001866 if (err < 0)
Yishai Hadasd2d19122018-09-20 21:39:32 +03001867 goto out_uars;
Yishai Hadas76dc5a82018-09-20 21:45:19 +03001868 context->devx_uid = err;
Yishai Hadasa8b92ca2018-06-17 12:59:57 +03001869 }
1870
Yishai Hadasd2d19122018-09-20 21:39:32 +03001871 err = mlx5_ib_alloc_transport_domain(dev, &context->tdn,
1872 context->devx_uid);
1873 if (err)
1874 goto out_devx;
1875
Yonatan Cohen25bb36e2018-06-19 08:47:24 +03001876 if (MLX5_CAP_GEN(dev->mdev, dump_fill_mkey)) {
1877 err = mlx5_cmd_dump_fill_mkey(dev->mdev, &dump_fill_mkey);
1878 if (err)
Jason Gunthorpe8193abb2018-07-04 13:19:46 -06001879 goto out_mdev;
Yonatan Cohen25bb36e2018-06-19 08:47:24 +03001880 }
1881
Eli Cohene126ba92013-07-07 17:25:49 +03001882 INIT_LIST_HEAD(&context->db_page_list);
1883 mutex_init(&context->db_page_mutex);
1884
Eli Cohen2f5ff262017-01-03 23:55:21 +02001885 resp.tot_bfregs = req.total_num_bfregs;
Daniel Jurgens508562d2018-01-04 17:25:34 +02001886 resp.num_ports = dev->num_ports;
Matan Barakb368d7c2015-12-15 20:30:12 +02001887
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001888 if (field_avail(typeof(resp), cqe_version, udata->outlen))
1889 resp.response_length += sizeof(resp.cqe_version);
Matan Barakb368d7c2015-12-15 20:30:12 +02001890
Bodong Wang402ca532016-06-17 15:02:20 +03001891 if (field_avail(typeof(resp), cmds_supp_uhw, udata->outlen)) {
Moni Shoua6ad279c52016-11-23 08:23:23 +02001892 resp.cmds_supp_uhw |= MLX5_USER_CMDS_SUPP_UHW_QUERY_DEVICE |
1893 MLX5_USER_CMDS_SUPP_UHW_CREATE_AH;
Bodong Wang402ca532016-06-17 15:02:20 +03001894 resp.response_length += sizeof(resp.cmds_supp_uhw);
1895 }
1896
Or Gerlitz78984892016-11-30 20:33:33 +02001897 if (field_avail(typeof(resp), eth_min_inline, udata->outlen)) {
1898 if (mlx5_ib_port_link_layer(ibdev, 1) == IB_LINK_LAYER_ETHERNET) {
1899 mlx5_query_min_inline(dev->mdev, &resp.eth_min_inline);
1900 resp.eth_min_inline++;
1901 }
1902 resp.response_length += sizeof(resp.eth_min_inline);
1903 }
1904
Feras Daoud5c99eae2018-01-16 20:08:41 +02001905 if (field_avail(typeof(resp), clock_info_versions, udata->outlen)) {
1906 if (mdev->clock_info)
1907 resp.clock_info_versions = BIT(MLX5_IB_CLOCK_INFO_V1);
1908 resp.response_length += sizeof(resp.clock_info_versions);
1909 }
1910
Noa Osherovichbc5c6ee2016-06-04 15:15:31 +03001911 /*
1912 * We don't want to expose information from the PCI bar that is located
1913 * after 4096 bytes, so if the arch only supports larger pages, let's
1914 * pretend we don't support reading the HCA's core clock. This is also
1915 * forced by mmap function.
1916 */
Eli Cohende8d6e02017-01-03 23:55:19 +02001917 if (field_avail(typeof(resp), hca_core_clock_offset, udata->outlen)) {
1918 if (PAGE_SIZE <= 4096) {
1919 resp.comp_mask |=
1920 MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_CORE_CLOCK_OFFSET;
1921 resp.hca_core_clock_offset =
1922 offsetof(struct mlx5_init_seg, internal_timer_h) % PAGE_SIZE;
1923 }
Feras Daoud5c99eae2018-01-16 20:08:41 +02001924 resp.response_length += sizeof(resp.hca_core_clock_offset);
Matan Barakb368d7c2015-12-15 20:30:12 +02001925 }
1926
Eli Cohen30aa60b2017-01-03 23:55:27 +02001927 if (field_avail(typeof(resp), log_uar_size, udata->outlen))
1928 resp.response_length += sizeof(resp.log_uar_size);
1929
1930 if (field_avail(typeof(resp), num_uars_per_page, udata->outlen))
1931 resp.response_length += sizeof(resp.num_uars_per_page);
1932
Yishai Hadas31a78a52017-12-24 16:31:34 +02001933 if (field_avail(typeof(resp), num_dyn_bfregs, udata->outlen)) {
1934 resp.num_dyn_bfregs = bfregi->num_dyn_bfregs;
1935 resp.response_length += sizeof(resp.num_dyn_bfregs);
1936 }
1937
Yonatan Cohen25bb36e2018-06-19 08:47:24 +03001938 if (field_avail(typeof(resp), dump_fill_mkey, udata->outlen)) {
1939 if (MLX5_CAP_GEN(dev->mdev, dump_fill_mkey)) {
1940 resp.dump_fill_mkey = dump_fill_mkey;
1941 resp.comp_mask |=
1942 MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_DUMP_FILL_MKEY;
1943 }
1944 resp.response_length += sizeof(resp.dump_fill_mkey);
1945 }
1946
Matan Barakb368d7c2015-12-15 20:30:12 +02001947 err = ib_copy_to_udata(udata, &resp, resp.response_length);
Eli Cohene126ba92013-07-07 17:25:49 +03001948 if (err)
Yishai Hadasa8b92ca2018-06-17 12:59:57 +03001949 goto out_mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03001950
Eli Cohen2f5ff262017-01-03 23:55:21 +02001951 bfregi->ver = ver;
1952 bfregi->num_low_latency_bfregs = req.num_low_latency_bfregs;
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001953 context->cqe_version = resp.cqe_version;
Eli Cohen30aa60b2017-01-03 23:55:27 +02001954 context->lib_caps = req.lib_caps;
1955 print_lib_caps(dev, context->lib_caps);
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001956
Aviv Heller7c34ec12018-08-23 13:47:53 +03001957 if (dev->lag_active) {
Mark Bloch95579e72019-03-28 15:27:33 +02001958 u8 port = mlx5_core_native_port_num(dev->mdev) - 1;
Majd Dibbinyc6a21c32018-08-28 14:29:05 +03001959
1960 atomic_set(&context->tx_port_affinity,
1961 atomic_add_return(
Mark Bloch95579e72019-03-28 15:27:33 +02001962 1, &dev->port[port].roce.tx_port_affinity));
Majd Dibbinyc6a21c32018-08-28 14:29:05 +03001963 }
1964
Leon Romanovskya2a074e2019-02-12 20:39:16 +02001965 return 0;
Eli Cohene126ba92013-07-07 17:25:49 +03001966
Yishai Hadasa8b92ca2018-06-17 12:59:57 +03001967out_mdev:
Yishai Hadasd2d19122018-09-20 21:39:32 +03001968 mlx5_ib_dealloc_transport_domain(dev, context->tdn, context->devx_uid);
1969out_devx:
Yishai Hadasa8b92ca2018-06-17 12:59:57 +03001970 if (req.flags & MLX5_IB_ALLOC_UCTX_DEVX)
Yishai Hadas76dc5a82018-09-20 21:45:19 +03001971 mlx5_ib_devx_destroy(dev, context->devx_uid);
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001972
Eli Cohene126ba92013-07-07 17:25:49 +03001973out_uars:
Eli Cohenb037c292017-01-03 23:55:26 +02001974 deallocate_uars(dev, context);
1975
1976out_sys_pages:
1977 kfree(bfregi->sys_pages);
1978
Eli Cohene126ba92013-07-07 17:25:49 +03001979out_count:
Eli Cohen2f5ff262017-01-03 23:55:21 +02001980 kfree(bfregi->count);
Eli Cohene126ba92013-07-07 17:25:49 +03001981
Eli Cohene126ba92013-07-07 17:25:49 +03001982out_ctx:
Leon Romanovskya2a074e2019-02-12 20:39:16 +02001983 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03001984}
1985
Leon Romanovskya2a074e2019-02-12 20:39:16 +02001986static void mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
Eli Cohene126ba92013-07-07 17:25:49 +03001987{
1988 struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1989 struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
Eli Cohenb037c292017-01-03 23:55:26 +02001990 struct mlx5_bfreg_info *bfregi;
Eli Cohene126ba92013-07-07 17:25:49 +03001991
Eli Cohenb037c292017-01-03 23:55:26 +02001992 bfregi = &context->bfregi;
Yishai Hadasd2d19122018-09-20 21:39:32 +03001993 mlx5_ib_dealloc_transport_domain(dev, context->tdn, context->devx_uid);
1994
Eli Cohenb037c292017-01-03 23:55:26 +02001995 if (context->devx_uid)
Yishai Hadas76dc5a82018-09-20 21:45:19 +03001996 mlx5_ib_devx_destroy(dev, context->devx_uid);
Eli Cohene126ba92013-07-07 17:25:49 +03001997
1998 deallocate_uars(dev, context);
Eli Cohen2f5ff262017-01-03 23:55:21 +02001999 kfree(bfregi->sys_pages);
2000 kfree(bfregi->count);
Eli Cohene126ba92013-07-07 17:25:49 +03002001}
2002
2003static phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev,
2004 int uar_idx)
2005{
Eli Cohenb037c292017-01-03 23:55:26 +02002006 int fw_uars_per_page;
2007
2008 fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ? MLX5_UARS_IN_PAGE : 1;
2009
Huy Nguyenaa8106f2019-03-29 15:38:01 -07002010 return (dev->mdev->bar_addr >> PAGE_SHIFT) + uar_idx / fw_uars_per_page;
Eli Cohene126ba92013-07-07 17:25:49 +03002011}
2012
2013static int get_command(unsigned long offset)
2014{
2015 return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK;
2016}
2017
2018static int get_arg(unsigned long offset)
2019{
2020 return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1);
2021}
2022
2023static int get_index(unsigned long offset)
2024{
2025 return get_arg(offset);
2026}
2027
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002028/* Index resides in an extra byte to enable larger values than 255 */
2029static int get_extended_index(unsigned long offset)
2030{
2031 return get_arg(offset) | ((offset >> 16) & 0xff) << 8;
2032}
2033
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03002034
2035static void mlx5_ib_disassociate_ucontext(struct ib_ucontext *ibcontext)
2036{
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03002037}
2038
Guy Levi37aa5c32016-04-27 16:49:50 +03002039static inline char *mmap_cmd2str(enum mlx5_ib_mmap_cmd cmd)
2040{
2041 switch (cmd) {
2042 case MLX5_IB_MMAP_WC_PAGE:
2043 return "WC";
2044 case MLX5_IB_MMAP_REGULAR_PAGE:
2045 return "best effort WC";
2046 case MLX5_IB_MMAP_NC_PAGE:
2047 return "NC";
Ariel Levkovich24da0012018-04-05 18:53:27 +03002048 case MLX5_IB_MMAP_DEVICE_MEM:
2049 return "Device Memory";
Guy Levi37aa5c32016-04-27 16:49:50 +03002050 default:
2051 return NULL;
2052 }
2053}
2054
Feras Daoud5c99eae2018-01-16 20:08:41 +02002055static int mlx5_ib_mmap_clock_info_page(struct mlx5_ib_dev *dev,
2056 struct vm_area_struct *vma,
2057 struct mlx5_ib_ucontext *context)
2058{
Jason Gunthorpe4eb6ab12019-04-16 14:07:30 +03002059 if ((vma->vm_end - vma->vm_start != PAGE_SIZE) ||
2060 !(vma->vm_flags & VM_SHARED))
Feras Daoud5c99eae2018-01-16 20:08:41 +02002061 return -EINVAL;
2062
2063 if (get_index(vma->vm_pgoff) != MLX5_IB_CLOCK_INFO_V1)
2064 return -EOPNOTSUPP;
2065
Jason Gunthorpe4eb6ab12019-04-16 14:07:30 +03002066 if (vma->vm_flags & (VM_WRITE | VM_EXEC))
Feras Daoud5c99eae2018-01-16 20:08:41 +02002067 return -EPERM;
Jason Gunthorpec6601332019-04-16 14:07:25 +03002068 vma->vm_flags &= ~VM_MAYWRITE;
Feras Daoud5c99eae2018-01-16 20:08:41 +02002069
Jason Gunthorpeddcdc362019-04-16 14:07:29 +03002070 if (!dev->mdev->clock_info)
Feras Daoud5c99eae2018-01-16 20:08:41 +02002071 return -EOPNOTSUPP;
2072
Jason Gunthorpe4eb6ab12019-04-16 14:07:30 +03002073 return vm_insert_page(vma, vma->vm_start,
2074 virt_to_page(dev->mdev->clock_info));
Feras Daoud5c99eae2018-01-16 20:08:41 +02002075}
2076
Yishai Hadasdc2316e2019-12-12 12:02:37 +02002077static void mlx5_ib_mmap_free(struct rdma_user_mmap_entry *entry)
2078{
2079 struct mlx5_user_mmap_entry *mentry = to_mmmap(entry);
2080 struct mlx5_ib_dev *dev = to_mdev(entry->ucontext->device);
Yishai Hadas7be76be2019-12-12 13:09:27 +02002081 struct mlx5_var_table *var_table = &dev->var_table;
Yishai Hadasdc2316e2019-12-12 12:02:37 +02002082 struct mlx5_ib_dm *mdm;
2083
2084 switch (mentry->mmap_flag) {
2085 case MLX5_IB_MMAP_TYPE_MEMIC:
2086 mdm = container_of(mentry, struct mlx5_ib_dm, mentry);
2087 mlx5_cmd_dealloc_memic(&dev->dm, mdm->dev_addr,
2088 mdm->size);
2089 kfree(mdm);
2090 break;
Yishai Hadas7be76be2019-12-12 13:09:27 +02002091 case MLX5_IB_MMAP_TYPE_VAR:
2092 mutex_lock(&var_table->bitmap_lock);
2093 clear_bit(mentry->page_idx, var_table->bitmap);
2094 mutex_unlock(&var_table->bitmap_lock);
2095 kfree(mentry);
2096 break;
Yishai Hadasdc2316e2019-12-12 12:02:37 +02002097 default:
2098 WARN_ON(true);
2099 }
2100}
2101
Guy Levi37aa5c32016-04-27 16:49:50 +03002102static int uar_mmap(struct mlx5_ib_dev *dev, enum mlx5_ib_mmap_cmd cmd,
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03002103 struct vm_area_struct *vma,
2104 struct mlx5_ib_ucontext *context)
Guy Levi37aa5c32016-04-27 16:49:50 +03002105{
Eli Cohen2f5ff262017-01-03 23:55:21 +02002106 struct mlx5_bfreg_info *bfregi = &context->bfregi;
Guy Levi37aa5c32016-04-27 16:49:50 +03002107 int err;
2108 unsigned long idx;
Kamal Heibaa09ea62018-07-19 00:05:32 +03002109 phys_addr_t pfn;
Guy Levi37aa5c32016-04-27 16:49:50 +03002110 pgprot_t prot;
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002111 u32 bfreg_dyn_idx = 0;
2112 u32 uar_index;
2113 int dyn_uar = (cmd == MLX5_IB_MMAP_ALLOC_WC);
2114 int max_valid_idx = dyn_uar ? bfregi->num_sys_pages :
2115 bfregi->num_static_sys_pages;
Eli Cohenb037c292017-01-03 23:55:26 +02002116
2117 if (vma->vm_end - vma->vm_start != PAGE_SIZE)
2118 return -EINVAL;
2119
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002120 if (dyn_uar)
2121 idx = get_extended_index(vma->vm_pgoff) + bfregi->num_static_sys_pages;
2122 else
2123 idx = get_index(vma->vm_pgoff);
2124
2125 if (idx >= max_valid_idx) {
2126 mlx5_ib_warn(dev, "invalid uar index %lu, max=%d\n",
2127 idx, max_valid_idx);
Eli Cohenb037c292017-01-03 23:55:26 +02002128 return -EINVAL;
2129 }
Guy Levi37aa5c32016-04-27 16:49:50 +03002130
2131 switch (cmd) {
2132 case MLX5_IB_MMAP_WC_PAGE:
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002133 case MLX5_IB_MMAP_ALLOC_WC:
Guy Levi37aa5c32016-04-27 16:49:50 +03002134/* Some architectures don't support WC memory */
2135#if defined(CONFIG_X86)
2136 if (!pat_enabled())
2137 return -EPERM;
2138#elif !(defined(CONFIG_PPC) || (defined(CONFIG_ARM) && defined(CONFIG_MMU)))
2139 return -EPERM;
2140#endif
2141 /* fall through */
2142 case MLX5_IB_MMAP_REGULAR_PAGE:
2143 /* For MLX5_IB_MMAP_REGULAR_PAGE do the best effort to get WC */
2144 prot = pgprot_writecombine(vma->vm_page_prot);
2145 break;
2146 case MLX5_IB_MMAP_NC_PAGE:
2147 prot = pgprot_noncached(vma->vm_page_prot);
2148 break;
2149 default:
2150 return -EINVAL;
2151 }
2152
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002153 if (dyn_uar) {
2154 int uars_per_page;
2155
2156 uars_per_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k);
2157 bfreg_dyn_idx = idx * (uars_per_page * MLX5_NON_FP_BFREGS_PER_UAR);
2158 if (bfreg_dyn_idx >= bfregi->total_num_bfregs) {
2159 mlx5_ib_warn(dev, "invalid bfreg_dyn_idx %u, max=%u\n",
2160 bfreg_dyn_idx, bfregi->total_num_bfregs);
2161 return -EINVAL;
2162 }
2163
2164 mutex_lock(&bfregi->lock);
2165 /* Fail if uar already allocated, first bfreg index of each
2166 * page holds its count.
2167 */
2168 if (bfregi->count[bfreg_dyn_idx]) {
2169 mlx5_ib_warn(dev, "wrong offset, idx %lu is busy, bfregn=%u\n", idx, bfreg_dyn_idx);
2170 mutex_unlock(&bfregi->lock);
2171 return -EINVAL;
2172 }
2173
2174 bfregi->count[bfreg_dyn_idx]++;
2175 mutex_unlock(&bfregi->lock);
2176
2177 err = mlx5_cmd_alloc_uar(dev->mdev, &uar_index);
2178 if (err) {
2179 mlx5_ib_warn(dev, "UAR alloc failed\n");
2180 goto free_bfreg;
2181 }
2182 } else {
2183 uar_index = bfregi->sys_pages[idx];
2184 }
2185
2186 pfn = uar_index2pfn(dev, uar_index);
Guy Levi37aa5c32016-04-27 16:49:50 +03002187 mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn %pa\n", idx, &pfn);
2188
Jason Gunthorpee2cd1d12018-09-16 20:43:10 +03002189 err = rdma_user_mmap_io(&context->ibucontext, vma, pfn, PAGE_SIZE,
Michal Kalderonc043ff22019-10-30 11:44:12 +02002190 prot, NULL);
Guy Levi37aa5c32016-04-27 16:49:50 +03002191 if (err) {
Leon Romanovsky8f062282018-05-22 08:31:03 +03002192 mlx5_ib_err(dev,
Jason Gunthorpee2cd1d12018-09-16 20:43:10 +03002193 "rdma_user_mmap_io failed with error=%d, mmap_cmd=%s\n",
Leon Romanovsky8f062282018-05-22 08:31:03 +03002194 err, mmap_cmd2str(cmd));
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002195 goto err;
Guy Levi37aa5c32016-04-27 16:49:50 +03002196 }
2197
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002198 if (dyn_uar)
2199 bfregi->sys_pages[idx] = uar_index;
2200 return 0;
2201
2202err:
2203 if (!dyn_uar)
2204 return err;
2205
2206 mlx5_cmd_free_uar(dev->mdev, idx);
2207
2208free_bfreg:
2209 mlx5_ib_free_bfreg(dev, bfregi, bfreg_dyn_idx);
2210
2211 return err;
Guy Levi37aa5c32016-04-27 16:49:50 +03002212}
2213
Yishai Hadasdc2316e2019-12-12 12:02:37 +02002214static int add_dm_mmap_entry(struct ib_ucontext *context,
2215 struct mlx5_ib_dm *mdm,
2216 u64 address)
Ariel Levkovich24da0012018-04-05 18:53:27 +03002217{
Yishai Hadasdc2316e2019-12-12 12:02:37 +02002218 mdm->mentry.mmap_flag = MLX5_IB_MMAP_TYPE_MEMIC;
2219 mdm->mentry.address = address;
2220 return rdma_user_mmap_entry_insert_range(
2221 context, &mdm->mentry.rdma_entry,
2222 mdm->size,
2223 MLX5_IB_MMAP_DEVICE_MEM << 16,
2224 (MLX5_IB_MMAP_DEVICE_MEM << 16) + (1UL << 16) - 1);
2225}
Ariel Levkovich24da0012018-04-05 18:53:27 +03002226
Yishai Hadasdc2316e2019-12-12 12:02:37 +02002227static unsigned long mlx5_vma_to_pgoff(struct vm_area_struct *vma)
2228{
2229 unsigned long idx;
2230 u8 command;
2231
2232 command = get_command(vma->vm_pgoff);
2233 idx = get_extended_index(vma->vm_pgoff);
2234
2235 return (command << 16 | idx);
2236}
2237
2238static int mlx5_ib_mmap_offset(struct mlx5_ib_dev *dev,
2239 struct vm_area_struct *vma,
2240 struct ib_ucontext *ucontext)
2241{
2242 struct mlx5_user_mmap_entry *mentry;
2243 struct rdma_user_mmap_entry *entry;
2244 unsigned long pgoff;
2245 pgprot_t prot;
2246 phys_addr_t pfn;
2247 int ret;
2248
2249 pgoff = mlx5_vma_to_pgoff(vma);
2250 entry = rdma_user_mmap_entry_get_pgoff(ucontext, pgoff);
2251 if (!entry)
Ariel Levkovich24da0012018-04-05 18:53:27 +03002252 return -EINVAL;
2253
Yishai Hadasdc2316e2019-12-12 12:02:37 +02002254 mentry = to_mmmap(entry);
2255 pfn = (mentry->address >> PAGE_SHIFT);
Yishai Hadas3f59b6c2019-12-12 13:09:28 +02002256 if (mentry->mmap_flag == MLX5_IB_MMAP_TYPE_VAR)
2257 prot = pgprot_noncached(vma->vm_page_prot);
2258 else
2259 prot = pgprot_writecombine(vma->vm_page_prot);
Yishai Hadasdc2316e2019-12-12 12:02:37 +02002260 ret = rdma_user_mmap_io(ucontext, vma, pfn,
2261 entry->npages * PAGE_SIZE,
2262 prot,
2263 entry);
2264 rdma_user_mmap_entry_put(&mentry->rdma_entry);
2265 return ret;
Ariel Levkovich24da0012018-04-05 18:53:27 +03002266}
2267
Yishai Hadas7be76be2019-12-12 13:09:27 +02002268static u64 mlx5_entry_to_mmap_offset(struct mlx5_user_mmap_entry *entry)
2269{
2270 u16 cmd = entry->rdma_entry.start_pgoff >> 16;
2271 u16 index = entry->rdma_entry.start_pgoff & 0xFFFF;
2272
2273 return (((index >> 8) << 16) | (cmd << MLX5_IB_MMAP_CMD_SHIFT) |
2274 (index & 0xFF)) << PAGE_SHIFT;
2275}
2276
Eli Cohene126ba92013-07-07 17:25:49 +03002277static int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma)
2278{
2279 struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
2280 struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
Eli Cohene126ba92013-07-07 17:25:49 +03002281 unsigned long command;
Eli Cohene126ba92013-07-07 17:25:49 +03002282 phys_addr_t pfn;
2283
2284 command = get_command(vma->vm_pgoff);
2285 switch (command) {
Guy Levi37aa5c32016-04-27 16:49:50 +03002286 case MLX5_IB_MMAP_WC_PAGE:
2287 case MLX5_IB_MMAP_NC_PAGE:
Eli Cohene126ba92013-07-07 17:25:49 +03002288 case MLX5_IB_MMAP_REGULAR_PAGE:
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002289 case MLX5_IB_MMAP_ALLOC_WC:
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03002290 return uar_mmap(dev, command, vma, context);
Eli Cohene126ba92013-07-07 17:25:49 +03002291
2292 case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES:
2293 return -ENOSYS;
2294
Matan Barakd69e3bc2015-12-15 20:30:13 +02002295 case MLX5_IB_MMAP_CORE_CLOCK:
Matan Barakd69e3bc2015-12-15 20:30:13 +02002296 if (vma->vm_end - vma->vm_start != PAGE_SIZE)
2297 return -EINVAL;
2298
Matan Barak6cbac1e2016-04-14 16:52:10 +03002299 if (vma->vm_flags & VM_WRITE)
Matan Barakd69e3bc2015-12-15 20:30:13 +02002300 return -EPERM;
Jason Gunthorpec6601332019-04-16 14:07:25 +03002301 vma->vm_flags &= ~VM_MAYWRITE;
Matan Barakd69e3bc2015-12-15 20:30:13 +02002302
2303 /* Don't expose to user-space information it shouldn't have */
2304 if (PAGE_SIZE > 4096)
2305 return -EOPNOTSUPP;
2306
Matan Barakd69e3bc2015-12-15 20:30:13 +02002307 pfn = (dev->mdev->iseg_base +
2308 offsetof(struct mlx5_init_seg, internal_timer_h)) >>
2309 PAGE_SHIFT;
Jason Gunthorped5e560d2019-04-16 14:07:26 +03002310 return rdma_user_mmap_io(&context->ibucontext, vma, pfn,
2311 PAGE_SIZE,
Michal Kalderonc043ff22019-10-30 11:44:12 +02002312 pgprot_noncached(vma->vm_page_prot),
2313 NULL);
Feras Daoud5c99eae2018-01-16 20:08:41 +02002314 case MLX5_IB_MMAP_CLOCK_INFO:
2315 return mlx5_ib_mmap_clock_info_page(dev, vma, context);
Matan Barakd69e3bc2015-12-15 20:30:13 +02002316
Eli Cohene126ba92013-07-07 17:25:49 +03002317 default:
Yishai Hadasdc2316e2019-12-12 12:02:37 +02002318 return mlx5_ib_mmap_offset(dev, vma, ibcontext);
Eli Cohene126ba92013-07-07 17:25:49 +03002319 }
2320
2321 return 0;
2322}
2323
Ariel Levkovich25c13322019-05-05 17:07:13 +03002324static inline int check_dm_type_support(struct mlx5_ib_dev *dev,
2325 u32 type)
Ariel Levkovich24da0012018-04-05 18:53:27 +03002326{
Ariel Levkovich25c13322019-05-05 17:07:13 +03002327 switch (type) {
2328 case MLX5_IB_UAPI_DM_TYPE_MEMIC:
2329 if (!MLX5_CAP_DEV_MEM(dev->mdev, memic))
2330 return -EOPNOTSUPP;
2331 break;
2332 case MLX5_IB_UAPI_DM_TYPE_STEERING_SW_ICM:
Ariel Levkovichc9b9dcb2019-08-29 23:42:30 +00002333 case MLX5_IB_UAPI_DM_TYPE_HEADER_MODIFY_SW_ICM:
Ariel Levkovich25c13322019-05-05 17:07:13 +03002334 if (!capable(CAP_SYS_RAWIO) ||
2335 !capable(CAP_NET_RAW))
2336 return -EPERM;
2337
2338 if (!(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, sw_owner) ||
2339 MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev, sw_owner)))
2340 return -EOPNOTSUPP;
2341 break;
2342 }
2343
2344 return 0;
2345}
2346
Ariel Levkovich3b113a12019-05-05 17:07:11 +03002347static int handle_alloc_dm_memic(struct ib_ucontext *ctx,
2348 struct mlx5_ib_dm *dm,
2349 struct ib_dm_alloc_attr *attr,
2350 struct uverbs_attr_bundle *attrs)
Ariel Levkovich24da0012018-04-05 18:53:27 +03002351{
Ariel Levkovich3b113a12019-05-05 17:07:11 +03002352 struct mlx5_dm *dm_db = &to_mdev(ctx->device)->dm;
Ariel Levkovich24da0012018-04-05 18:53:27 +03002353 u64 start_offset;
Yishai Hadasdc2316e2019-12-12 12:02:37 +02002354 u16 page_idx;
Ariel Levkovich24da0012018-04-05 18:53:27 +03002355 int err;
Yishai Hadasdc2316e2019-12-12 12:02:37 +02002356 u64 address;
Ariel Levkovich24da0012018-04-05 18:53:27 +03002357
Ariel Levkovich3b113a12019-05-05 17:07:11 +03002358 dm->size = roundup(attr->length, MLX5_MEMIC_BASE_SIZE);
Ariel Levkovich24da0012018-04-05 18:53:27 +03002359
Ariel Levkovich3b113a12019-05-05 17:07:11 +03002360 err = mlx5_cmd_alloc_memic(dm_db, &dm->dev_addr,
2361 dm->size, attr->alignment);
Ariel Levkovich24da0012018-04-05 18:53:27 +03002362 if (err)
Ariel Levkovich3b113a12019-05-05 17:07:11 +03002363 return err;
Ariel Levkovich24da0012018-04-05 18:53:27 +03002364
Yishai Hadasdc2316e2019-12-12 12:02:37 +02002365 address = dm->dev_addr & PAGE_MASK;
2366 err = add_dm_mmap_entry(ctx, dm, address);
Ariel Levkovich24da0012018-04-05 18:53:27 +03002367 if (err)
2368 goto err_dealloc;
2369
Yishai Hadasdc2316e2019-12-12 12:02:37 +02002370 page_idx = dm->mentry.rdma_entry.start_pgoff & 0xFFFF;
2371 err = uverbs_copy_to(attrs,
2372 MLX5_IB_ATTR_ALLOC_DM_RESP_PAGE_INDEX,
2373 &page_idx,
2374 sizeof(page_idx));
2375 if (err)
2376 goto err_copy;
2377
Ariel Levkovich3b113a12019-05-05 17:07:11 +03002378 start_offset = dm->dev_addr & ~PAGE_MASK;
2379 err = uverbs_copy_to(attrs,
2380 MLX5_IB_ATTR_ALLOC_DM_RESP_START_OFFSET,
2381 &start_offset, sizeof(start_offset));
2382 if (err)
Yishai Hadasdc2316e2019-12-12 12:02:37 +02002383 goto err_copy;
Ariel Levkovich3b113a12019-05-05 17:07:11 +03002384
2385 return 0;
2386
Yishai Hadasdc2316e2019-12-12 12:02:37 +02002387err_copy:
2388 rdma_user_mmap_entry_remove(&dm->mentry.rdma_entry);
Ariel Levkovich3b113a12019-05-05 17:07:11 +03002389err_dealloc:
2390 mlx5_cmd_dealloc_memic(dm_db, dm->dev_addr, dm->size);
2391
2392 return err;
2393}
2394
Ariel Levkovich25c13322019-05-05 17:07:13 +03002395static int handle_alloc_dm_sw_icm(struct ib_ucontext *ctx,
2396 struct mlx5_ib_dm *dm,
2397 struct ib_dm_alloc_attr *attr,
2398 struct uverbs_attr_bundle *attrs,
2399 int type)
2400{
Ariel Levkovichc9b9dcb2019-08-29 23:42:30 +00002401 struct mlx5_core_dev *dev = to_mdev(ctx->device)->mdev;
Ariel Levkovich25c13322019-05-05 17:07:13 +03002402 u64 act_size;
2403 int err;
2404
2405 /* Allocation size must a multiple of the basic block size
2406 * and a power of 2.
2407 */
Ariel Levkovichc9b9dcb2019-08-29 23:42:30 +00002408 act_size = round_up(attr->length, MLX5_SW_ICM_BLOCK_SIZE(dev));
Ariel Levkovich25c13322019-05-05 17:07:13 +03002409 act_size = roundup_pow_of_two(act_size);
2410
2411 dm->size = act_size;
Ariel Levkovichc9b9dcb2019-08-29 23:42:30 +00002412 err = mlx5_dm_sw_icm_alloc(dev, type, act_size,
2413 to_mucontext(ctx)->devx_uid, &dm->dev_addr,
2414 &dm->icm_dm.obj_id);
Ariel Levkovich25c13322019-05-05 17:07:13 +03002415 if (err)
2416 return err;
2417
2418 err = uverbs_copy_to(attrs,
2419 MLX5_IB_ATTR_ALLOC_DM_RESP_START_OFFSET,
2420 &dm->dev_addr, sizeof(dm->dev_addr));
2421 if (err)
Ariel Levkovichc9b9dcb2019-08-29 23:42:30 +00002422 mlx5_dm_sw_icm_dealloc(dev, type, dm->size,
2423 to_mucontext(ctx)->devx_uid, dm->dev_addr,
2424 dm->icm_dm.obj_id);
Ariel Levkovich25c13322019-05-05 17:07:13 +03002425
2426 return err;
2427}
2428
Ariel Levkovich3b113a12019-05-05 17:07:11 +03002429struct ib_dm *mlx5_ib_alloc_dm(struct ib_device *ibdev,
2430 struct ib_ucontext *context,
2431 struct ib_dm_alloc_attr *attr,
2432 struct uverbs_attr_bundle *attrs)
2433{
2434 struct mlx5_ib_dm *dm;
2435 enum mlx5_ib_uapi_dm_type type;
2436 int err;
2437
2438 err = uverbs_get_const_default(&type, attrs,
2439 MLX5_IB_ATTR_ALLOC_DM_REQ_TYPE,
2440 MLX5_IB_UAPI_DM_TYPE_MEMIC);
2441 if (err)
2442 return ERR_PTR(err);
2443
2444 mlx5_ib_dbg(to_mdev(ibdev), "alloc_dm req: dm_type=%d user_length=0x%llx log_alignment=%d\n",
2445 type, attr->length, attr->alignment);
2446
Ariel Levkovich25c13322019-05-05 17:07:13 +03002447 err = check_dm_type_support(to_mdev(ibdev), type);
2448 if (err)
2449 return ERR_PTR(err);
2450
Ariel Levkovich3b113a12019-05-05 17:07:11 +03002451 dm = kzalloc(sizeof(*dm), GFP_KERNEL);
2452 if (!dm)
2453 return ERR_PTR(-ENOMEM);
2454
2455 dm->type = type;
2456
2457 switch (type) {
2458 case MLX5_IB_UAPI_DM_TYPE_MEMIC:
2459 err = handle_alloc_dm_memic(context, dm,
2460 attr,
2461 attrs);
2462 break;
Ariel Levkovich25c13322019-05-05 17:07:13 +03002463 case MLX5_IB_UAPI_DM_TYPE_STEERING_SW_ICM:
Ariel Levkovichc9b9dcb2019-08-29 23:42:30 +00002464 err = handle_alloc_dm_sw_icm(context, dm,
2465 attr, attrs,
2466 MLX5_SW_ICM_TYPE_STEERING);
2467 break;
Ariel Levkovich25c13322019-05-05 17:07:13 +03002468 case MLX5_IB_UAPI_DM_TYPE_HEADER_MODIFY_SW_ICM:
Ariel Levkovichc9b9dcb2019-08-29 23:42:30 +00002469 err = handle_alloc_dm_sw_icm(context, dm,
2470 attr, attrs,
2471 MLX5_SW_ICM_TYPE_HEADER_MODIFY);
Ariel Levkovich25c13322019-05-05 17:07:13 +03002472 break;
Ariel Levkovich3b113a12019-05-05 17:07:11 +03002473 default:
2474 err = -EOPNOTSUPP;
2475 }
2476
2477 if (err)
2478 goto err_free;
Ariel Levkovich24da0012018-04-05 18:53:27 +03002479
2480 return &dm->ibdm;
2481
Ariel Levkovich24da0012018-04-05 18:53:27 +03002482err_free:
2483 kfree(dm);
2484 return ERR_PTR(err);
2485}
2486
Shamir Rabinovitchc4367a22019-03-31 19:10:05 +03002487int mlx5_ib_dealloc_dm(struct ib_dm *ibdm, struct uverbs_attr_bundle *attrs)
Ariel Levkovich24da0012018-04-05 18:53:27 +03002488{
Ariel Levkovich25c13322019-05-05 17:07:13 +03002489 struct mlx5_ib_ucontext *ctx = rdma_udata_to_drv_context(
2490 &attrs->driver_udata, struct mlx5_ib_ucontext, ibucontext);
Ariel Levkovichc9b9dcb2019-08-29 23:42:30 +00002491 struct mlx5_core_dev *dev = to_mdev(ibdm->device)->mdev;
Ariel Levkovich24da0012018-04-05 18:53:27 +03002492 struct mlx5_ib_dm *dm = to_mdm(ibdm);
Ariel Levkovich24da0012018-04-05 18:53:27 +03002493 int ret;
2494
Ariel Levkovich3b113a12019-05-05 17:07:11 +03002495 switch (dm->type) {
2496 case MLX5_IB_UAPI_DM_TYPE_MEMIC:
Yishai Hadasdc2316e2019-12-12 12:02:37 +02002497 rdma_user_mmap_entry_remove(&dm->mentry.rdma_entry);
2498 return 0;
Ariel Levkovich25c13322019-05-05 17:07:13 +03002499 case MLX5_IB_UAPI_DM_TYPE_STEERING_SW_ICM:
Ariel Levkovichc9b9dcb2019-08-29 23:42:30 +00002500 ret = mlx5_dm_sw_icm_dealloc(dev, MLX5_SW_ICM_TYPE_STEERING,
2501 dm->size, ctx->devx_uid, dm->dev_addr,
2502 dm->icm_dm.obj_id);
2503 if (ret)
2504 return ret;
2505 break;
Ariel Levkovich25c13322019-05-05 17:07:13 +03002506 case MLX5_IB_UAPI_DM_TYPE_HEADER_MODIFY_SW_ICM:
Ariel Levkovichc9b9dcb2019-08-29 23:42:30 +00002507 ret = mlx5_dm_sw_icm_dealloc(dev, MLX5_SW_ICM_TYPE_HEADER_MODIFY,
2508 dm->size, ctx->devx_uid, dm->dev_addr,
2509 dm->icm_dm.obj_id);
Ariel Levkovich25c13322019-05-05 17:07:13 +03002510 if (ret)
2511 return ret;
Ariel Levkovich3b113a12019-05-05 17:07:11 +03002512 break;
2513 default:
2514 return -EOPNOTSUPP;
2515 }
Ariel Levkovich24da0012018-04-05 18:53:27 +03002516
2517 kfree(dm);
2518
2519 return 0;
2520}
2521
Shamir Rabinovitchff23dfa2019-03-31 19:10:07 +03002522static int mlx5_ib_alloc_pd(struct ib_pd *ibpd, struct ib_udata *udata)
Eli Cohene126ba92013-07-07 17:25:49 +03002523{
Leon Romanovsky21a428a2019-02-03 14:55:51 +02002524 struct mlx5_ib_pd *pd = to_mpd(ibpd);
2525 struct ib_device *ibdev = ibpd->device;
Eli Cohene126ba92013-07-07 17:25:49 +03002526 struct mlx5_ib_alloc_pd_resp resp;
Eli Cohene126ba92013-07-07 17:25:49 +03002527 int err;
Yishai Hadasa1069c12018-09-20 21:39:19 +03002528 u32 out[MLX5_ST_SZ_DW(alloc_pd_out)] = {};
2529 u32 in[MLX5_ST_SZ_DW(alloc_pd_in)] = {};
2530 u16 uid = 0;
Shamir Rabinovitchff23dfa2019-03-31 19:10:07 +03002531 struct mlx5_ib_ucontext *context = rdma_udata_to_drv_context(
2532 udata, struct mlx5_ib_ucontext, ibucontext);
Eli Cohene126ba92013-07-07 17:25:49 +03002533
Shamir Rabinovitchff23dfa2019-03-31 19:10:07 +03002534 uid = context ? context->devx_uid : 0;
Yishai Hadasa1069c12018-09-20 21:39:19 +03002535 MLX5_SET(alloc_pd_in, in, opcode, MLX5_CMD_OP_ALLOC_PD);
2536 MLX5_SET(alloc_pd_in, in, uid, uid);
2537 err = mlx5_cmd_exec(to_mdev(ibdev)->mdev, in, sizeof(in),
2538 out, sizeof(out));
Leon Romanovsky21a428a2019-02-03 14:55:51 +02002539 if (err)
2540 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03002541
Yishai Hadasa1069c12018-09-20 21:39:19 +03002542 pd->pdn = MLX5_GET(alloc_pd_out, out, pd);
2543 pd->uid = uid;
Shamir Rabinovitchff23dfa2019-03-31 19:10:07 +03002544 if (udata) {
Eli Cohene126ba92013-07-07 17:25:49 +03002545 resp.pdn = pd->pdn;
2546 if (ib_copy_to_udata(udata, &resp, sizeof(resp))) {
Yishai Hadasa1069c12018-09-20 21:39:19 +03002547 mlx5_cmd_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn, uid);
Leon Romanovsky21a428a2019-02-03 14:55:51 +02002548 return -EFAULT;
Eli Cohene126ba92013-07-07 17:25:49 +03002549 }
Eli Cohene126ba92013-07-07 17:25:49 +03002550 }
2551
Leon Romanovsky21a428a2019-02-03 14:55:51 +02002552 return 0;
Eli Cohene126ba92013-07-07 17:25:49 +03002553}
2554
Shamir Rabinovitchc4367a22019-03-31 19:10:05 +03002555static void mlx5_ib_dealloc_pd(struct ib_pd *pd, struct ib_udata *udata)
Eli Cohene126ba92013-07-07 17:25:49 +03002556{
2557 struct mlx5_ib_dev *mdev = to_mdev(pd->device);
2558 struct mlx5_ib_pd *mpd = to_mpd(pd);
2559
Yishai Hadasa1069c12018-09-20 21:39:19 +03002560 mlx5_cmd_dealloc_pd(mdev->mdev, mpd->pdn, mpd->uid);
Eli Cohene126ba92013-07-07 17:25:49 +03002561}
2562
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002563enum {
2564 MATCH_CRITERIA_ENABLE_OUTER_BIT,
2565 MATCH_CRITERIA_ENABLE_MISC_BIT,
Ariel Levkovich71c6e862018-05-13 14:33:34 +03002566 MATCH_CRITERIA_ENABLE_INNER_BIT,
2567 MATCH_CRITERIA_ENABLE_MISC2_BIT
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002568};
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002569
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002570#define HEADER_IS_ZERO(match_criteria, headers) \
2571 !(memchr_inv(MLX5_ADDR_OF(fte_match_param, match_criteria, headers), \
2572 0, MLX5_FLD_SZ_BYTES(fte_match_param, headers))) \
2573
2574static u8 get_match_criteria_enable(u32 *match_criteria)
2575{
2576 u8 match_criteria_enable;
2577
2578 match_criteria_enable =
2579 (!HEADER_IS_ZERO(match_criteria, outer_headers)) <<
2580 MATCH_CRITERIA_ENABLE_OUTER_BIT;
2581 match_criteria_enable |=
2582 (!HEADER_IS_ZERO(match_criteria, misc_parameters)) <<
2583 MATCH_CRITERIA_ENABLE_MISC_BIT;
2584 match_criteria_enable |=
2585 (!HEADER_IS_ZERO(match_criteria, inner_headers)) <<
2586 MATCH_CRITERIA_ENABLE_INNER_BIT;
Ariel Levkovich71c6e862018-05-13 14:33:34 +03002587 match_criteria_enable |=
2588 (!HEADER_IS_ZERO(match_criteria, misc_parameters_2)) <<
2589 MATCH_CRITERIA_ENABLE_MISC2_BIT;
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002590
2591 return match_criteria_enable;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002592}
2593
Maor Gottlieb6113cc42019-01-17 20:08:15 +02002594static int set_proto(void *outer_c, void *outer_v, u8 mask, u8 val)
Maor Gottliebca0d4752016-08-30 16:58:35 +03002595{
Maor Gottlieb6113cc42019-01-17 20:08:15 +02002596 u8 entry_mask;
2597 u8 entry_val;
2598 int err = 0;
2599
2600 if (!mask)
2601 goto out;
2602
2603 entry_mask = MLX5_GET(fte_match_set_lyr_2_4, outer_c,
2604 ip_protocol);
2605 entry_val = MLX5_GET(fte_match_set_lyr_2_4, outer_v,
2606 ip_protocol);
2607 if (!entry_mask) {
2608 MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_protocol, mask);
2609 MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_protocol, val);
2610 goto out;
2611 }
2612 /* Don't override existing ip protocol */
2613 if (mask != entry_mask || val != entry_val)
2614 err = -EINVAL;
2615out:
2616 return err;
Maor Gottliebca0d4752016-08-30 16:58:35 +03002617}
2618
Daria Velikovsky37da2a02018-05-07 10:20:02 +03002619static void set_flow_label(void *misc_c, void *misc_v, u32 mask, u32 val,
Moses Reuben2d1e6972016-11-14 19:04:52 +02002620 bool inner)
2621{
2622 if (inner) {
2623 MLX5_SET(fte_match_set_misc,
2624 misc_c, inner_ipv6_flow_label, mask);
2625 MLX5_SET(fte_match_set_misc,
2626 misc_v, inner_ipv6_flow_label, val);
2627 } else {
2628 MLX5_SET(fte_match_set_misc,
2629 misc_c, outer_ipv6_flow_label, mask);
2630 MLX5_SET(fte_match_set_misc,
2631 misc_v, outer_ipv6_flow_label, val);
2632 }
2633}
2634
Maor Gottliebca0d4752016-08-30 16:58:35 +03002635static void set_tos(void *outer_c, void *outer_v, u8 mask, u8 val)
2636{
2637 MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_ecn, mask);
2638 MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_ecn, val);
2639 MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_dscp, mask >> 2);
2640 MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_dscp, val >> 2);
2641}
2642
Ariel Levkovich71c6e862018-05-13 14:33:34 +03002643static int check_mpls_supp_fields(u32 field_support, const __be32 *set_mask)
2644{
2645 if (MLX5_GET(fte_match_mpls, set_mask, mpls_label) &&
2646 !(field_support & MLX5_FIELD_SUPPORT_MPLS_LABEL))
2647 return -EOPNOTSUPP;
2648
2649 if (MLX5_GET(fte_match_mpls, set_mask, mpls_exp) &&
2650 !(field_support & MLX5_FIELD_SUPPORT_MPLS_EXP))
2651 return -EOPNOTSUPP;
2652
2653 if (MLX5_GET(fte_match_mpls, set_mask, mpls_s_bos) &&
2654 !(field_support & MLX5_FIELD_SUPPORT_MPLS_S_BOS))
2655 return -EOPNOTSUPP;
2656
2657 if (MLX5_GET(fte_match_mpls, set_mask, mpls_ttl) &&
2658 !(field_support & MLX5_FIELD_SUPPORT_MPLS_TTL))
2659 return -EOPNOTSUPP;
2660
2661 return 0;
2662}
2663
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002664#define LAST_ETH_FIELD vlan_tag
2665#define LAST_IB_FIELD sl
Maor Gottliebca0d4752016-08-30 16:58:35 +03002666#define LAST_IPV4_FIELD tos
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002667#define LAST_IPV6_FIELD traffic_class
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002668#define LAST_TCP_UDP_FIELD src_port
Moses Reubenffb30d82016-11-14 19:04:50 +02002669#define LAST_TUNNEL_FIELD tunnel_id
Moses Reuben2ac693f2017-01-18 14:59:50 +02002670#define LAST_FLOW_TAG_FIELD tag_id
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002671#define LAST_DROP_FIELD size
Raed Salem3b3233f2018-05-31 16:43:39 +03002672#define LAST_COUNTERS_FIELD counters
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002673
2674/* Field is the last supported field */
2675#define FIELDS_NOT_SUPPORTED(filter, field)\
2676 memchr_inv((void *)&filter.field +\
2677 sizeof(filter.field), 0,\
2678 sizeof(filter) -\
2679 offsetof(typeof(filter), field) -\
2680 sizeof(filter.field))
2681
Mark Bloch2ea26202018-09-06 17:27:03 +03002682int parse_flow_flow_action(struct mlx5_ib_flow_action *maction,
2683 bool is_egress,
2684 struct mlx5_flow_act *action)
Aviad Yehezkel802c2122018-03-28 09:27:53 +03002685{
Aviad Yehezkel802c2122018-03-28 09:27:53 +03002686
2687 switch (maction->ib_action.type) {
2688 case IB_FLOW_ACTION_ESP:
Mark Bloch501f14e2018-09-06 17:27:04 +03002689 if (action->action & (MLX5_FLOW_CONTEXT_ACTION_ENCRYPT |
2690 MLX5_FLOW_CONTEXT_ACTION_DECRYPT))
2691 return -EINVAL;
Aviad Yehezkel802c2122018-03-28 09:27:53 +03002692 /* Currently only AES_GCM keymat is supported by the driver */
2693 action->esp_id = (uintptr_t)maction->esp_aes_gcm.ctx;
Mark Bloch2ea26202018-09-06 17:27:03 +03002694 action->action |= is_egress ?
Aviad Yehezkel802c2122018-03-28 09:27:53 +03002695 MLX5_FLOW_CONTEXT_ACTION_ENCRYPT :
2696 MLX5_FLOW_CONTEXT_ACTION_DECRYPT;
2697 return 0;
Mark Blochb1085be2018-09-02 12:51:32 +03002698 case IB_FLOW_ACTION_UNSPECIFIED:
2699 if (maction->flow_action_raw.sub_type ==
2700 MLX5_IB_FLOW_ACTION_MODIFY_HEADER) {
Mark Bloch501f14e2018-09-06 17:27:04 +03002701 if (action->action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR)
2702 return -EINVAL;
Mark Blochb1085be2018-09-02 12:51:32 +03002703 action->action |= MLX5_FLOW_CONTEXT_ACTION_MOD_HDR;
Maor Gottlieb2b688ea2019-08-15 13:54:17 +03002704 action->modify_hdr =
2705 maction->flow_action_raw.modify_hdr;
Mark Blochb1085be2018-09-02 12:51:32 +03002706 return 0;
2707 }
Mark Bloch10a30892018-09-02 12:51:34 +03002708 if (maction->flow_action_raw.sub_type ==
2709 MLX5_IB_FLOW_ACTION_DECAP) {
Mark Bloch501f14e2018-09-06 17:27:04 +03002710 if (action->action & MLX5_FLOW_CONTEXT_ACTION_DECAP)
2711 return -EINVAL;
Mark Bloch10a30892018-09-02 12:51:34 +03002712 action->action |= MLX5_FLOW_CONTEXT_ACTION_DECAP;
2713 return 0;
2714 }
Mark Bloche806f932018-09-02 12:51:36 +03002715 if (maction->flow_action_raw.sub_type ==
2716 MLX5_IB_FLOW_ACTION_PACKET_REFORMAT) {
Mark Bloch501f14e2018-09-06 17:27:04 +03002717 if (action->action &
2718 MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT)
2719 return -EINVAL;
Mark Bloche806f932018-09-02 12:51:36 +03002720 action->action |=
2721 MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT;
Maor Gottlieb2b688ea2019-08-15 13:54:17 +03002722 action->pkt_reformat =
2723 maction->flow_action_raw.pkt_reformat;
Mark Bloche806f932018-09-02 12:51:36 +03002724 return 0;
2725 }
Mark Blochb1085be2018-09-02 12:51:32 +03002726 /* fall through */
Aviad Yehezkel802c2122018-03-28 09:27:53 +03002727 default:
2728 return -EOPNOTSUPP;
2729 }
2730}
2731
Jianbo Liubb0ee7d2019-06-25 17:47:58 +00002732static int parse_flow_attr(struct mlx5_core_dev *mdev,
2733 struct mlx5_flow_spec *spec,
2734 const union ib_flow_spec *ib_spec,
Aviad Yehezkel802c2122018-03-28 09:27:53 +03002735 const struct ib_flow_attr *flow_attr,
Ariel Levkovich71c6e862018-05-13 14:33:34 +03002736 struct mlx5_flow_act *action, u32 prev_type)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002737{
Jianbo Liubb0ee7d2019-06-25 17:47:58 +00002738 struct mlx5_flow_context *flow_context = &spec->flow_context;
2739 u32 *match_c = spec->match_criteria;
2740 u32 *match_v = spec->match_value;
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002741 void *misc_params_c = MLX5_ADDR_OF(fte_match_param, match_c,
2742 misc_parameters);
2743 void *misc_params_v = MLX5_ADDR_OF(fte_match_param, match_v,
2744 misc_parameters);
Ariel Levkovich71c6e862018-05-13 14:33:34 +03002745 void *misc_params2_c = MLX5_ADDR_OF(fte_match_param, match_c,
2746 misc_parameters_2);
2747 void *misc_params2_v = MLX5_ADDR_OF(fte_match_param, match_v,
2748 misc_parameters_2);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002749 void *headers_c;
2750 void *headers_v;
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002751 int match_ipv;
Aviad Yehezkel802c2122018-03-28 09:27:53 +03002752 int ret;
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002753
Moses Reuben2d1e6972016-11-14 19:04:52 +02002754 if (ib_spec->type & IB_FLOW_SPEC_INNER) {
2755 headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
2756 inner_headers);
2757 headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
2758 inner_headers);
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002759 match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2760 ft_field_support.inner_ip_version);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002761 } else {
2762 headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
2763 outer_headers);
2764 headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
2765 outer_headers);
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002766 match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2767 ft_field_support.outer_ip_version);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002768 }
2769
2770 switch (ib_spec->type & ~IB_FLOW_SPEC_INNER) {
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002771 case IB_FLOW_SPEC_ETH:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002772 if (FIELDS_NOT_SUPPORTED(ib_spec->eth.mask, LAST_ETH_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002773 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002774
Moses Reuben2d1e6972016-11-14 19:04:52 +02002775 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002776 dmac_47_16),
2777 ib_spec->eth.mask.dst_mac);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002778 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002779 dmac_47_16),
2780 ib_spec->eth.val.dst_mac);
2781
Moses Reuben2d1e6972016-11-14 19:04:52 +02002782 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottliebee3da802016-09-12 19:16:24 +03002783 smac_47_16),
2784 ib_spec->eth.mask.src_mac);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002785 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottliebee3da802016-09-12 19:16:24 +03002786 smac_47_16),
2787 ib_spec->eth.val.src_mac);
2788
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002789 if (ib_spec->eth.mask.vlan_tag) {
Moses Reuben2d1e6972016-11-14 19:04:52 +02002790 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Mohamad Haj Yahia10543362016-10-09 16:25:43 +03002791 cvlan_tag, 1);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002792 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Mohamad Haj Yahia10543362016-10-09 16:25:43 +03002793 cvlan_tag, 1);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002794
Moses Reuben2d1e6972016-11-14 19:04:52 +02002795 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002796 first_vid, ntohs(ib_spec->eth.mask.vlan_tag));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002797 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002798 first_vid, ntohs(ib_spec->eth.val.vlan_tag));
2799
Moses Reuben2d1e6972016-11-14 19:04:52 +02002800 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002801 first_cfi,
2802 ntohs(ib_spec->eth.mask.vlan_tag) >> 12);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002803 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002804 first_cfi,
2805 ntohs(ib_spec->eth.val.vlan_tag) >> 12);
2806
Moses Reuben2d1e6972016-11-14 19:04:52 +02002807 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002808 first_prio,
2809 ntohs(ib_spec->eth.mask.vlan_tag) >> 13);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002810 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002811 first_prio,
2812 ntohs(ib_spec->eth.val.vlan_tag) >> 13);
2813 }
Moses Reuben2d1e6972016-11-14 19:04:52 +02002814 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002815 ethertype, ntohs(ib_spec->eth.mask.ether_type));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002816 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002817 ethertype, ntohs(ib_spec->eth.val.ether_type));
2818 break;
2819 case IB_FLOW_SPEC_IPV4:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002820 if (FIELDS_NOT_SUPPORTED(ib_spec->ipv4.mask, LAST_IPV4_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002821 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002822
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002823 if (match_ipv) {
2824 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2825 ip_version, 0xf);
2826 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Boris Pismenny3346c482017-08-20 15:13:08 +03002827 ip_version, MLX5_FS_IPV4_VERSION);
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002828 } else {
2829 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2830 ethertype, 0xffff);
2831 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2832 ethertype, ETH_P_IP);
2833 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002834
Moses Reuben2d1e6972016-11-14 19:04:52 +02002835 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002836 src_ipv4_src_ipv6.ipv4_layout.ipv4),
2837 &ib_spec->ipv4.mask.src_ip,
2838 sizeof(ib_spec->ipv4.mask.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002839 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002840 src_ipv4_src_ipv6.ipv4_layout.ipv4),
2841 &ib_spec->ipv4.val.src_ip,
2842 sizeof(ib_spec->ipv4.val.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002843 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002844 dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2845 &ib_spec->ipv4.mask.dst_ip,
2846 sizeof(ib_spec->ipv4.mask.dst_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002847 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002848 dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2849 &ib_spec->ipv4.val.dst_ip,
2850 sizeof(ib_spec->ipv4.val.dst_ip));
Maor Gottliebca0d4752016-08-30 16:58:35 +03002851
Moses Reuben2d1e6972016-11-14 19:04:52 +02002852 set_tos(headers_c, headers_v,
Maor Gottliebca0d4752016-08-30 16:58:35 +03002853 ib_spec->ipv4.mask.tos, ib_spec->ipv4.val.tos);
2854
Maor Gottlieb6113cc42019-01-17 20:08:15 +02002855 if (set_proto(headers_c, headers_v,
2856 ib_spec->ipv4.mask.proto,
2857 ib_spec->ipv4.val.proto))
2858 return -EINVAL;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002859 break;
Maor Gottlieb026bae02016-06-17 15:14:51 +03002860 case IB_FLOW_SPEC_IPV6:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002861 if (FIELDS_NOT_SUPPORTED(ib_spec->ipv6.mask, LAST_IPV6_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002862 return -EOPNOTSUPP;
Maor Gottlieb026bae02016-06-17 15:14:51 +03002863
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002864 if (match_ipv) {
2865 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2866 ip_version, 0xf);
2867 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Boris Pismenny3346c482017-08-20 15:13:08 +03002868 ip_version, MLX5_FS_IPV6_VERSION);
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002869 } else {
2870 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2871 ethertype, 0xffff);
2872 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2873 ethertype, ETH_P_IPV6);
2874 }
Maor Gottlieb026bae02016-06-17 15:14:51 +03002875
Moses Reuben2d1e6972016-11-14 19:04:52 +02002876 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb026bae02016-06-17 15:14:51 +03002877 src_ipv4_src_ipv6.ipv6_layout.ipv6),
2878 &ib_spec->ipv6.mask.src_ip,
2879 sizeof(ib_spec->ipv6.mask.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002880 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb026bae02016-06-17 15:14:51 +03002881 src_ipv4_src_ipv6.ipv6_layout.ipv6),
2882 &ib_spec->ipv6.val.src_ip,
2883 sizeof(ib_spec->ipv6.val.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002884 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb026bae02016-06-17 15:14:51 +03002885 dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2886 &ib_spec->ipv6.mask.dst_ip,
2887 sizeof(ib_spec->ipv6.mask.dst_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002888 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb026bae02016-06-17 15:14:51 +03002889 dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2890 &ib_spec->ipv6.val.dst_ip,
2891 sizeof(ib_spec->ipv6.val.dst_ip));
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002892
Moses Reuben2d1e6972016-11-14 19:04:52 +02002893 set_tos(headers_c, headers_v,
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002894 ib_spec->ipv6.mask.traffic_class,
2895 ib_spec->ipv6.val.traffic_class);
2896
Maor Gottlieb6113cc42019-01-17 20:08:15 +02002897 if (set_proto(headers_c, headers_v,
2898 ib_spec->ipv6.mask.next_hdr,
2899 ib_spec->ipv6.val.next_hdr))
2900 return -EINVAL;
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002901
Moses Reuben2d1e6972016-11-14 19:04:52 +02002902 set_flow_label(misc_params_c, misc_params_v,
2903 ntohl(ib_spec->ipv6.mask.flow_label),
2904 ntohl(ib_spec->ipv6.val.flow_label),
2905 ib_spec->type & IB_FLOW_SPEC_INNER);
Aviad Yehezkel802c2122018-03-28 09:27:53 +03002906 break;
2907 case IB_FLOW_SPEC_ESP:
2908 if (ib_spec->esp.mask.seq)
2909 return -EOPNOTSUPP;
Moses Reuben2d1e6972016-11-14 19:04:52 +02002910
Aviad Yehezkel802c2122018-03-28 09:27:53 +03002911 MLX5_SET(fte_match_set_misc, misc_params_c, outer_esp_spi,
2912 ntohl(ib_spec->esp.mask.spi));
2913 MLX5_SET(fte_match_set_misc, misc_params_v, outer_esp_spi,
2914 ntohl(ib_spec->esp.val.spi));
Maor Gottlieb026bae02016-06-17 15:14:51 +03002915 break;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002916 case IB_FLOW_SPEC_TCP:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002917 if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
2918 LAST_TCP_UDP_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002919 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002920
Maor Gottlieb6113cc42019-01-17 20:08:15 +02002921 if (set_proto(headers_c, headers_v, 0xff, IPPROTO_TCP))
2922 return -EINVAL;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002923
Moses Reuben2d1e6972016-11-14 19:04:52 +02002924 MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002925 ntohs(ib_spec->tcp_udp.mask.src_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002926 MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002927 ntohs(ib_spec->tcp_udp.val.src_port));
2928
Moses Reuben2d1e6972016-11-14 19:04:52 +02002929 MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002930 ntohs(ib_spec->tcp_udp.mask.dst_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002931 MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002932 ntohs(ib_spec->tcp_udp.val.dst_port));
2933 break;
2934 case IB_FLOW_SPEC_UDP:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002935 if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
2936 LAST_TCP_UDP_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002937 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002938
Maor Gottlieb6113cc42019-01-17 20:08:15 +02002939 if (set_proto(headers_c, headers_v, 0xff, IPPROTO_UDP))
2940 return -EINVAL;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002941
Moses Reuben2d1e6972016-11-14 19:04:52 +02002942 MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002943 ntohs(ib_spec->tcp_udp.mask.src_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002944 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002945 ntohs(ib_spec->tcp_udp.val.src_port));
2946
Moses Reuben2d1e6972016-11-14 19:04:52 +02002947 MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002948 ntohs(ib_spec->tcp_udp.mask.dst_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002949 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002950 ntohs(ib_spec->tcp_udp.val.dst_port));
2951 break;
Ariel Levkovichda2f22a2018-05-13 14:33:33 +03002952 case IB_FLOW_SPEC_GRE:
2953 if (ib_spec->gre.mask.c_ks_res0_ver)
2954 return -EOPNOTSUPP;
2955
Maor Gottlieb6113cc42019-01-17 20:08:15 +02002956 if (set_proto(headers_c, headers_v, 0xff, IPPROTO_GRE))
2957 return -EINVAL;
2958
Ariel Levkovichda2f22a2018-05-13 14:33:33 +03002959 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
2960 0xff);
2961 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
2962 IPPROTO_GRE);
2963
2964 MLX5_SET(fte_match_set_misc, misc_params_c, gre_protocol,
Maor Gottlieba93b6322018-07-01 15:50:17 +03002965 ntohs(ib_spec->gre.mask.protocol));
Ariel Levkovichda2f22a2018-05-13 14:33:33 +03002966 MLX5_SET(fte_match_set_misc, misc_params_v, gre_protocol,
2967 ntohs(ib_spec->gre.val.protocol));
2968
2969 memcpy(MLX5_ADDR_OF(fte_match_set_misc, misc_params_c,
Oz Shlomo5886a962018-12-10 13:15:13 -08002970 gre_key.nvgre.hi),
Ariel Levkovichda2f22a2018-05-13 14:33:33 +03002971 &ib_spec->gre.mask.key,
2972 sizeof(ib_spec->gre.mask.key));
2973 memcpy(MLX5_ADDR_OF(fte_match_set_misc, misc_params_v,
Oz Shlomo5886a962018-12-10 13:15:13 -08002974 gre_key.nvgre.hi),
Ariel Levkovichda2f22a2018-05-13 14:33:33 +03002975 &ib_spec->gre.val.key,
2976 sizeof(ib_spec->gre.val.key));
2977 break;
Ariel Levkovich71c6e862018-05-13 14:33:34 +03002978 case IB_FLOW_SPEC_MPLS:
2979 switch (prev_type) {
2980 case IB_FLOW_SPEC_UDP:
2981 if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2982 ft_field_support.outer_first_mpls_over_udp),
2983 &ib_spec->mpls.mask.tag))
2984 return -EOPNOTSUPP;
2985
2986 memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
2987 outer_first_mpls_over_udp),
2988 &ib_spec->mpls.val.tag,
2989 sizeof(ib_spec->mpls.val.tag));
2990 memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
2991 outer_first_mpls_over_udp),
2992 &ib_spec->mpls.mask.tag,
2993 sizeof(ib_spec->mpls.mask.tag));
2994 break;
2995 case IB_FLOW_SPEC_GRE:
2996 if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2997 ft_field_support.outer_first_mpls_over_gre),
2998 &ib_spec->mpls.mask.tag))
2999 return -EOPNOTSUPP;
3000
3001 memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
3002 outer_first_mpls_over_gre),
3003 &ib_spec->mpls.val.tag,
3004 sizeof(ib_spec->mpls.val.tag));
3005 memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
3006 outer_first_mpls_over_gre),
3007 &ib_spec->mpls.mask.tag,
3008 sizeof(ib_spec->mpls.mask.tag));
3009 break;
3010 default:
3011 if (ib_spec->type & IB_FLOW_SPEC_INNER) {
3012 if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
3013 ft_field_support.inner_first_mpls),
3014 &ib_spec->mpls.mask.tag))
3015 return -EOPNOTSUPP;
3016
3017 memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
3018 inner_first_mpls),
3019 &ib_spec->mpls.val.tag,
3020 sizeof(ib_spec->mpls.val.tag));
3021 memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
3022 inner_first_mpls),
3023 &ib_spec->mpls.mask.tag,
3024 sizeof(ib_spec->mpls.mask.tag));
3025 } else {
3026 if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
3027 ft_field_support.outer_first_mpls),
3028 &ib_spec->mpls.mask.tag))
3029 return -EOPNOTSUPP;
3030
3031 memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
3032 outer_first_mpls),
3033 &ib_spec->mpls.val.tag,
3034 sizeof(ib_spec->mpls.val.tag));
3035 memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
3036 outer_first_mpls),
3037 &ib_spec->mpls.mask.tag,
3038 sizeof(ib_spec->mpls.mask.tag));
3039 }
3040 }
3041 break;
Moses Reubenffb30d82016-11-14 19:04:50 +02003042 case IB_FLOW_SPEC_VXLAN_TUNNEL:
3043 if (FIELDS_NOT_SUPPORTED(ib_spec->tunnel.mask,
3044 LAST_TUNNEL_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02003045 return -EOPNOTSUPP;
Moses Reubenffb30d82016-11-14 19:04:50 +02003046
3047 MLX5_SET(fte_match_set_misc, misc_params_c, vxlan_vni,
3048 ntohl(ib_spec->tunnel.mask.tunnel_id));
3049 MLX5_SET(fte_match_set_misc, misc_params_v, vxlan_vni,
3050 ntohl(ib_spec->tunnel.val.tunnel_id));
3051 break;
Moses Reuben2ac693f2017-01-18 14:59:50 +02003052 case IB_FLOW_SPEC_ACTION_TAG:
3053 if (FIELDS_NOT_SUPPORTED(ib_spec->flow_tag,
3054 LAST_FLOW_TAG_FIELD))
3055 return -EOPNOTSUPP;
3056 if (ib_spec->flow_tag.tag_id >= BIT(24))
3057 return -EINVAL;
3058
Jianbo Liubb0ee7d2019-06-25 17:47:58 +00003059 flow_context->flow_tag = ib_spec->flow_tag.tag_id;
3060 flow_context->flags |= FLOW_CONTEXT_HAS_TAG;
Moses Reuben2ac693f2017-01-18 14:59:50 +02003061 break;
Slava Shwartsmana22ed862017-04-03 13:13:52 +03003062 case IB_FLOW_SPEC_ACTION_DROP:
3063 if (FIELDS_NOT_SUPPORTED(ib_spec->drop,
3064 LAST_DROP_FIELD))
3065 return -EOPNOTSUPP;
Boris Pismenny075572d2017-08-16 09:33:30 +03003066 action->action |= MLX5_FLOW_CONTEXT_ACTION_DROP;
Slava Shwartsmana22ed862017-04-03 13:13:52 +03003067 break;
Aviad Yehezkel802c2122018-03-28 09:27:53 +03003068 case IB_FLOW_SPEC_ACTION_HANDLE:
Mark Bloch2ea26202018-09-06 17:27:03 +03003069 ret = parse_flow_flow_action(to_mflow_act(ib_spec->action.act),
3070 flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS, action);
Aviad Yehezkel802c2122018-03-28 09:27:53 +03003071 if (ret)
3072 return ret;
3073 break;
Raed Salem3b3233f2018-05-31 16:43:39 +03003074 case IB_FLOW_SPEC_ACTION_COUNT:
3075 if (FIELDS_NOT_SUPPORTED(ib_spec->flow_count,
3076 LAST_COUNTERS_FIELD))
3077 return -EOPNOTSUPP;
3078
3079 /* for now support only one counters spec per flow */
3080 if (action->action & MLX5_FLOW_CONTEXT_ACTION_COUNT)
3081 return -EINVAL;
3082
3083 action->counters = ib_spec->flow_count.counters;
3084 action->action |= MLX5_FLOW_CONTEXT_ACTION_COUNT;
3085 break;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003086 default:
3087 return -EINVAL;
3088 }
3089
3090 return 0;
3091}
3092
3093/* If a flow could catch both multicast and unicast packets,
3094 * it won't fall into the multicast flow steering table and this rule
3095 * could steal other multicast packets.
3096 */
Yishai Hadasa550ddf2017-08-17 15:52:33 +03003097static bool flow_is_multicast_only(const struct ib_flow_attr *ib_attr)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003098{
Yishai Hadas81e30882017-06-08 16:15:09 +03003099 union ib_flow_spec *flow_spec;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003100
3101 if (ib_attr->type != IB_FLOW_ATTR_NORMAL ||
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003102 ib_attr->num_of_specs < 1)
3103 return false;
3104
Yishai Hadas81e30882017-06-08 16:15:09 +03003105 flow_spec = (union ib_flow_spec *)(ib_attr + 1);
3106 if (flow_spec->type == IB_FLOW_SPEC_IPV4) {
3107 struct ib_flow_spec_ipv4 *ipv4_spec;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003108
Yishai Hadas81e30882017-06-08 16:15:09 +03003109 ipv4_spec = (struct ib_flow_spec_ipv4 *)flow_spec;
3110 if (ipv4_is_multicast(ipv4_spec->val.dst_ip))
3111 return true;
3112
3113 return false;
3114 }
3115
3116 if (flow_spec->type == IB_FLOW_SPEC_ETH) {
3117 struct ib_flow_spec_eth *eth_spec;
3118
3119 eth_spec = (struct ib_flow_spec_eth *)flow_spec;
3120 return is_multicast_ether_addr(eth_spec->mask.dst_mac) &&
3121 is_multicast_ether_addr(eth_spec->val.dst_mac);
3122 }
3123
3124 return false;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003125}
3126
Aviad Yehezkel802c2122018-03-28 09:27:53 +03003127enum valid_spec {
3128 VALID_SPEC_INVALID,
3129 VALID_SPEC_VALID,
3130 VALID_SPEC_NA,
3131};
3132
3133static enum valid_spec
3134is_valid_esp_aes_gcm(struct mlx5_core_dev *mdev,
3135 const struct mlx5_flow_spec *spec,
3136 const struct mlx5_flow_act *flow_act,
3137 bool egress)
3138{
3139 const u32 *match_c = spec->match_criteria;
3140 bool is_crypto =
3141 (flow_act->action & (MLX5_FLOW_CONTEXT_ACTION_ENCRYPT |
3142 MLX5_FLOW_CONTEXT_ACTION_DECRYPT));
3143 bool is_ipsec = mlx5_fs_is_ipsec_flow(match_c);
3144 bool is_drop = flow_act->action & MLX5_FLOW_CONTEXT_ACTION_DROP;
3145
3146 /*
3147 * Currently only crypto is supported in egress, when regular egress
3148 * rules would be supported, always return VALID_SPEC_NA.
3149 */
3150 if (!is_crypto)
Mark Bloch78dd0c42018-09-02 12:51:31 +03003151 return VALID_SPEC_NA;
Aviad Yehezkel802c2122018-03-28 09:27:53 +03003152
3153 return is_crypto && is_ipsec &&
Jianbo Liubb0ee7d2019-06-25 17:47:58 +00003154 (!egress || (!is_drop &&
3155 !(spec->flow_context.flags & FLOW_CONTEXT_HAS_TAG))) ?
Aviad Yehezkel802c2122018-03-28 09:27:53 +03003156 VALID_SPEC_VALID : VALID_SPEC_INVALID;
3157}
3158
3159static bool is_valid_spec(struct mlx5_core_dev *mdev,
3160 const struct mlx5_flow_spec *spec,
3161 const struct mlx5_flow_act *flow_act,
3162 bool egress)
3163{
3164 /* We curretly only support ipsec egress flow */
3165 return is_valid_esp_aes_gcm(mdev, spec, flow_act, egress) != VALID_SPEC_INVALID;
3166}
3167
Ariel Levkovich19cc7522017-04-03 13:11:03 +03003168static bool is_valid_ethertype(struct mlx5_core_dev *mdev,
3169 const struct ib_flow_attr *flow_attr,
Ariel Levkovich0f750962017-04-03 13:11:02 +03003170 bool check_inner)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003171{
3172 union ib_flow_spec *ib_spec = (union ib_flow_spec *)(flow_attr + 1);
Ariel Levkovich19cc7522017-04-03 13:11:03 +03003173 int match_ipv = check_inner ?
3174 MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
3175 ft_field_support.inner_ip_version) :
3176 MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
3177 ft_field_support.outer_ip_version);
Ariel Levkovich0f750962017-04-03 13:11:02 +03003178 int inner_bit = check_inner ? IB_FLOW_SPEC_INNER : 0;
3179 bool ipv4_spec_valid, ipv6_spec_valid;
3180 unsigned int ip_spec_type = 0;
3181 bool has_ethertype = false;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003182 unsigned int spec_index;
Ariel Levkovich0f750962017-04-03 13:11:02 +03003183 bool mask_valid = true;
3184 u16 eth_type = 0;
3185 bool type_valid;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003186
3187 /* Validate that ethertype is correct */
3188 for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
Ariel Levkovich0f750962017-04-03 13:11:02 +03003189 if ((ib_spec->type == (IB_FLOW_SPEC_ETH | inner_bit)) &&
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003190 ib_spec->eth.mask.ether_type) {
Ariel Levkovich0f750962017-04-03 13:11:02 +03003191 mask_valid = (ib_spec->eth.mask.ether_type ==
3192 htons(0xffff));
3193 has_ethertype = true;
3194 eth_type = ntohs(ib_spec->eth.val.ether_type);
3195 } else if ((ib_spec->type == (IB_FLOW_SPEC_IPV4 | inner_bit)) ||
3196 (ib_spec->type == (IB_FLOW_SPEC_IPV6 | inner_bit))) {
3197 ip_spec_type = ib_spec->type;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003198 }
3199 ib_spec = (void *)ib_spec + ib_spec->size;
3200 }
Ariel Levkovich0f750962017-04-03 13:11:02 +03003201
3202 type_valid = (!has_ethertype) || (!ip_spec_type);
3203 if (!type_valid && mask_valid) {
3204 ipv4_spec_valid = (eth_type == ETH_P_IP) &&
3205 (ip_spec_type == (IB_FLOW_SPEC_IPV4 | inner_bit));
3206 ipv6_spec_valid = (eth_type == ETH_P_IPV6) &&
3207 (ip_spec_type == (IB_FLOW_SPEC_IPV6 | inner_bit));
Ariel Levkovich19cc7522017-04-03 13:11:03 +03003208
3209 type_valid = (ipv4_spec_valid) || (ipv6_spec_valid) ||
3210 (((eth_type == ETH_P_MPLS_UC) ||
3211 (eth_type == ETH_P_MPLS_MC)) && match_ipv);
Ariel Levkovich0f750962017-04-03 13:11:02 +03003212 }
3213
3214 return type_valid;
3215}
3216
Ariel Levkovich19cc7522017-04-03 13:11:03 +03003217static bool is_valid_attr(struct mlx5_core_dev *mdev,
3218 const struct ib_flow_attr *flow_attr)
Ariel Levkovich0f750962017-04-03 13:11:02 +03003219{
Ariel Levkovich19cc7522017-04-03 13:11:03 +03003220 return is_valid_ethertype(mdev, flow_attr, false) &&
3221 is_valid_ethertype(mdev, flow_attr, true);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003222}
3223
3224static void put_flow_table(struct mlx5_ib_dev *dev,
3225 struct mlx5_ib_flow_prio *prio, bool ft_added)
3226{
3227 prio->refcount -= !!ft_added;
3228 if (!prio->refcount) {
3229 mlx5_destroy_flow_table(prio->flow_table);
3230 prio->flow_table = NULL;
3231 }
3232}
3233
Raed Salem3b3233f2018-05-31 16:43:39 +03003234static void counters_clear_description(struct ib_counters *counters)
3235{
3236 struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
3237
3238 mutex_lock(&mcounters->mcntrs_mutex);
3239 kfree(mcounters->counters_data);
3240 mcounters->counters_data = NULL;
3241 mcounters->cntrs_max_index = 0;
3242 mutex_unlock(&mcounters->mcntrs_mutex);
3243}
3244
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003245static int mlx5_ib_destroy_flow(struct ib_flow *flow_id)
3246{
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003247 struct mlx5_ib_flow_handler *handler = container_of(flow_id,
3248 struct mlx5_ib_flow_handler,
3249 ibflow);
3250 struct mlx5_ib_flow_handler *iter, *tmp;
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003251 struct mlx5_ib_dev *dev = handler->dev;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003252
Mark Bloch9a4ca382018-01-16 14:42:35 +00003253 mutex_lock(&dev->flow_db->lock);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003254
3255 list_for_each_entry_safe(iter, tmp, &handler->list, list) {
Mark Bloch74491de2016-08-31 11:24:25 +00003256 mlx5_del_flow_rules(iter->rule);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003257 put_flow_table(dev, iter->prio, true);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003258 list_del(&iter->list);
3259 kfree(iter);
3260 }
3261
Mark Bloch74491de2016-08-31 11:24:25 +00003262 mlx5_del_flow_rules(handler->rule);
Maor Gottlieb5497adc2016-08-28 14:16:31 +03003263 put_flow_table(dev, handler->prio, true);
Raed Salem3b3233f2018-05-31 16:43:39 +03003264 if (handler->ibcounters &&
3265 atomic_read(&handler->ibcounters->usecnt) == 1)
3266 counters_clear_description(handler->ibcounters);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003267
Raed Salem3b3233f2018-05-31 16:43:39 +03003268 mutex_unlock(&dev->flow_db->lock);
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003269 if (handler->flow_matcher)
3270 atomic_dec(&handler->flow_matcher->usecnt);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003271 kfree(handler);
3272
3273 return 0;
3274}
3275
Maor Gottlieb35d190112016-03-07 18:51:47 +02003276static int ib_prio_to_core_prio(unsigned int priority, bool dont_trap)
3277{
3278 priority *= 2;
3279 if (!dont_trap)
3280 priority++;
3281 return priority;
3282}
3283
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003284enum flow_table_type {
3285 MLX5_IB_FT_RX,
3286 MLX5_IB_FT_TX
3287};
3288
Maor Gottlieb00b7c2a2017-03-29 06:09:01 +03003289#define MLX5_FS_MAX_TYPES 6
3290#define MLX5_FS_MAX_ENTRIES BIT(16)
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003291
3292static struct mlx5_ib_flow_prio *_get_prio(struct mlx5_flow_namespace *ns,
3293 struct mlx5_ib_flow_prio *prio,
3294 int priority,
Mark Bloch4adda112018-09-02 12:51:33 +03003295 int num_entries, int num_groups,
3296 u32 flags)
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003297{
3298 struct mlx5_flow_table *ft;
3299
3300 ft = mlx5_create_auto_grouped_flow_table(ns, priority,
3301 num_entries,
3302 num_groups,
Mark Bloch4adda112018-09-02 12:51:33 +03003303 0, flags);
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003304 if (IS_ERR(ft))
3305 return ERR_CAST(ft);
3306
3307 prio->flow_table = ft;
3308 prio->refcount = 0;
3309 return prio;
3310}
3311
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003312static struct mlx5_ib_flow_prio *get_flow_table(struct mlx5_ib_dev *dev,
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003313 struct ib_flow_attr *flow_attr,
3314 enum flow_table_type ft_type)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003315{
Maor Gottlieb35d190112016-03-07 18:51:47 +02003316 bool dont_trap = flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003317 struct mlx5_flow_namespace *ns = NULL;
3318 struct mlx5_ib_flow_prio *prio;
3319 struct mlx5_flow_table *ft;
Maor Gottliebdac388e2017-03-29 06:09:00 +03003320 int max_table_size;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003321 int num_entries;
3322 int num_groups;
Maor Gottliebcecae742019-06-12 15:20:13 +03003323 bool esw_encap;
Mark Bloch4adda112018-09-02 12:51:33 +03003324 u32 flags = 0;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003325 int priority;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003326
Maor Gottliebdac388e2017-03-29 06:09:00 +03003327 max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
3328 log_max_ft_size));
Maor Gottliebcecae742019-06-12 15:20:13 +03003329 esw_encap = mlx5_eswitch_get_encap_mode(dev->mdev) !=
3330 DEVLINK_ESWITCH_ENCAP_MODE_NONE;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003331 if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
Mark Bloch78dd0c42018-09-02 12:51:31 +03003332 enum mlx5_flow_namespace_type fn_type;
3333
3334 if (flow_is_multicast_only(flow_attr) &&
3335 !dont_trap)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003336 priority = MLX5_IB_FLOW_MCAST_PRIO;
3337 else
Maor Gottlieb35d190112016-03-07 18:51:47 +02003338 priority = ib_prio_to_core_prio(flow_attr->priority,
3339 dont_trap);
Mark Bloch78dd0c42018-09-02 12:51:31 +03003340 if (ft_type == MLX5_IB_FT_RX) {
3341 fn_type = MLX5_FLOW_NAMESPACE_BYPASS;
3342 prio = &dev->flow_db->prios[priority];
Maor Gottliebcecae742019-06-12 15:20:13 +03003343 if (!dev->is_rep && !esw_encap &&
Mark Bloch4adda112018-09-02 12:51:33 +03003344 MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, decap))
3345 flags |= MLX5_FLOW_TABLE_TUNNEL_EN_DECAP;
Maor Gottliebcecae742019-06-12 15:20:13 +03003346 if (!dev->is_rep && !esw_encap &&
Mark Bloch5c2db532018-09-02 12:51:35 +03003347 MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
3348 reformat_l3_tunnel_to_l2))
3349 flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
Mark Bloch78dd0c42018-09-02 12:51:31 +03003350 } else {
3351 max_table_size =
3352 BIT(MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev,
3353 log_max_ft_size));
3354 fn_type = MLX5_FLOW_NAMESPACE_EGRESS;
3355 prio = &dev->flow_db->egress_prios[priority];
Maor Gottliebcecae742019-06-12 15:20:13 +03003356 if (!dev->is_rep && !esw_encap &&
Mark Bloch4adda112018-09-02 12:51:33 +03003357 MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev, reformat))
3358 flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
Mark Bloch78dd0c42018-09-02 12:51:31 +03003359 }
3360 ns = mlx5_get_flow_namespace(dev->mdev, fn_type);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003361 num_entries = MLX5_FS_MAX_ENTRIES;
3362 num_groups = MLX5_FS_MAX_TYPES;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003363 } else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
3364 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
3365 ns = mlx5_get_flow_namespace(dev->mdev,
3366 MLX5_FLOW_NAMESPACE_LEFTOVERS);
3367 build_leftovers_ft_param(&priority,
3368 &num_entries,
3369 &num_groups);
Mark Bloch9a4ca382018-01-16 14:42:35 +00003370 prio = &dev->flow_db->prios[MLX5_IB_FLOW_LEFTOVERS_PRIO];
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003371 } else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
3372 if (!MLX5_CAP_FLOWTABLE(dev->mdev,
3373 allow_sniffer_and_nic_rx_shared_tir))
3374 return ERR_PTR(-ENOTSUPP);
3375
3376 ns = mlx5_get_flow_namespace(dev->mdev, ft_type == MLX5_IB_FT_RX ?
3377 MLX5_FLOW_NAMESPACE_SNIFFER_RX :
3378 MLX5_FLOW_NAMESPACE_SNIFFER_TX);
3379
Mark Bloch9a4ca382018-01-16 14:42:35 +00003380 prio = &dev->flow_db->sniffer[ft_type];
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003381 priority = 0;
3382 num_entries = 1;
3383 num_groups = 1;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003384 }
3385
3386 if (!ns)
3387 return ERR_PTR(-ENOTSUPP);
3388
Mark Bloch3b705082019-03-28 15:46:22 +02003389 max_table_size = min_t(int, num_entries, max_table_size);
Maor Gottliebdac388e2017-03-29 06:09:00 +03003390
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003391 ft = prio->flow_table;
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003392 if (!ft)
Mark Bloch3b705082019-03-28 15:46:22 +02003393 return _get_prio(ns, prio, priority, max_table_size, num_groups,
Mark Bloch4adda112018-09-02 12:51:33 +03003394 flags);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003395
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003396 return prio;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003397}
3398
Yishai Hadasa550ddf2017-08-17 15:52:33 +03003399static void set_underlay_qp(struct mlx5_ib_dev *dev,
3400 struct mlx5_flow_spec *spec,
3401 u32 underlay_qpn)
3402{
3403 void *misc_params_c = MLX5_ADDR_OF(fte_match_param,
3404 spec->match_criteria,
3405 misc_parameters);
3406 void *misc_params_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
3407 misc_parameters);
3408
3409 if (underlay_qpn &&
3410 MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
3411 ft_field_support.bth_dst_qp)) {
3412 MLX5_SET(fte_match_set_misc,
3413 misc_params_v, bth_dst_qp, underlay_qpn);
3414 MLX5_SET(fte_match_set_misc,
3415 misc_params_c, bth_dst_qp, 0xffffff);
3416 }
3417}
3418
Raed Salem5e95af52018-05-31 16:43:40 +03003419static int read_flow_counters(struct ib_device *ibdev,
3420 struct mlx5_read_counters_attr *read_attr)
3421{
3422 struct mlx5_fc *fc = read_attr->hw_cntrs_hndl;
3423 struct mlx5_ib_dev *dev = to_mdev(ibdev);
3424
3425 return mlx5_fc_query(dev->mdev, fc,
3426 &read_attr->out[IB_COUNTER_PACKETS],
3427 &read_attr->out[IB_COUNTER_BYTES]);
3428}
3429
3430/* flow counters currently expose two counters packets and bytes */
3431#define FLOW_COUNTERS_NUM 2
Raed Salem3b3233f2018-05-31 16:43:39 +03003432static int counters_set_description(struct ib_counters *counters,
3433 enum mlx5_ib_counters_type counters_type,
3434 struct mlx5_ib_flow_counters_desc *desc_data,
3435 u32 ncounters)
3436{
3437 struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
3438 u32 cntrs_max_index = 0;
3439 int i;
3440
3441 if (counters_type != MLX5_IB_COUNTERS_FLOW)
3442 return -EINVAL;
3443
3444 /* init the fields for the object */
3445 mcounters->type = counters_type;
Raed Salem5e95af52018-05-31 16:43:40 +03003446 mcounters->read_counters = read_flow_counters;
3447 mcounters->counters_num = FLOW_COUNTERS_NUM;
Raed Salem3b3233f2018-05-31 16:43:39 +03003448 mcounters->ncounters = ncounters;
3449 /* each counter entry have both description and index pair */
3450 for (i = 0; i < ncounters; i++) {
3451 if (desc_data[i].description > IB_COUNTER_BYTES)
3452 return -EINVAL;
3453
3454 if (cntrs_max_index <= desc_data[i].index)
3455 cntrs_max_index = desc_data[i].index + 1;
3456 }
3457
3458 mutex_lock(&mcounters->mcntrs_mutex);
3459 mcounters->counters_data = desc_data;
3460 mcounters->cntrs_max_index = cntrs_max_index;
3461 mutex_unlock(&mcounters->mcntrs_mutex);
3462
3463 return 0;
3464}
3465
3466#define MAX_COUNTERS_NUM (USHRT_MAX / (sizeof(u32) * 2))
3467static int flow_counters_set_data(struct ib_counters *ibcounters,
3468 struct mlx5_ib_create_flow *ucmd)
3469{
3470 struct mlx5_ib_mcounters *mcounters = to_mcounters(ibcounters);
3471 struct mlx5_ib_flow_counters_data *cntrs_data = NULL;
3472 struct mlx5_ib_flow_counters_desc *desc_data = NULL;
3473 bool hw_hndl = false;
3474 int ret = 0;
3475
3476 if (ucmd && ucmd->ncounters_data != 0) {
3477 cntrs_data = ucmd->data;
3478 if (cntrs_data->ncounters > MAX_COUNTERS_NUM)
3479 return -EINVAL;
3480
3481 desc_data = kcalloc(cntrs_data->ncounters,
3482 sizeof(*desc_data),
3483 GFP_KERNEL);
3484 if (!desc_data)
3485 return -ENOMEM;
3486
3487 if (copy_from_user(desc_data,
3488 u64_to_user_ptr(cntrs_data->counters_data),
3489 sizeof(*desc_data) * cntrs_data->ncounters)) {
3490 ret = -EFAULT;
3491 goto free;
3492 }
3493 }
3494
3495 if (!mcounters->hw_cntrs_hndl) {
3496 mcounters->hw_cntrs_hndl = mlx5_fc_create(
3497 to_mdev(ibcounters->device)->mdev, false);
weiyongjun (A)e31abf72018-06-07 01:47:41 +00003498 if (IS_ERR(mcounters->hw_cntrs_hndl)) {
3499 ret = PTR_ERR(mcounters->hw_cntrs_hndl);
Raed Salem3b3233f2018-05-31 16:43:39 +03003500 goto free;
3501 }
3502 hw_hndl = true;
3503 }
3504
3505 if (desc_data) {
3506 /* counters already bound to at least one flow */
3507 if (mcounters->cntrs_max_index) {
3508 ret = -EINVAL;
3509 goto free_hndl;
3510 }
3511
3512 ret = counters_set_description(ibcounters,
3513 MLX5_IB_COUNTERS_FLOW,
3514 desc_data,
3515 cntrs_data->ncounters);
3516 if (ret)
3517 goto free_hndl;
3518
3519 } else if (!mcounters->cntrs_max_index) {
3520 /* counters not bound yet, must have udata passed */
3521 ret = -EINVAL;
3522 goto free_hndl;
3523 }
3524
3525 return 0;
3526
3527free_hndl:
3528 if (hw_hndl) {
3529 mlx5_fc_destroy(to_mdev(ibcounters->device)->mdev,
3530 mcounters->hw_cntrs_hndl);
3531 mcounters->hw_cntrs_hndl = NULL;
3532 }
3533free:
3534 kfree(desc_data);
3535 return ret;
3536}
3537
Jianbo Liu669ff1e2019-06-25 17:48:12 +00003538static void mlx5_ib_set_rule_source_port(struct mlx5_ib_dev *dev,
3539 struct mlx5_flow_spec *spec,
3540 struct mlx5_eswitch_rep *rep)
3541{
3542 struct mlx5_eswitch *esw = dev->mdev->priv.eswitch;
3543 void *misc;
3544
3545 if (mlx5_eswitch_vport_match_metadata_enabled(esw)) {
3546 misc = MLX5_ADDR_OF(fte_match_param, spec->match_value,
3547 misc_parameters_2);
3548
3549 MLX5_SET(fte_match_set_misc2, misc, metadata_reg_c_0,
3550 mlx5_eswitch_get_vport_metadata_for_match(esw,
3551 rep->vport));
3552 misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
3553 misc_parameters_2);
3554
3555 MLX5_SET_TO_ONES(fte_match_set_misc2, misc, metadata_reg_c_0);
3556 } else {
3557 misc = MLX5_ADDR_OF(fte_match_param, spec->match_value,
3558 misc_parameters);
3559
3560 MLX5_SET(fte_match_set_misc, misc, source_port, rep->vport);
3561
3562 misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
3563 misc_parameters);
3564
3565 MLX5_SET_TO_ONES(fte_match_set_misc, misc, source_port);
3566 }
3567}
3568
Yishai Hadasa550ddf2017-08-17 15:52:33 +03003569static struct mlx5_ib_flow_handler *_create_flow_rule(struct mlx5_ib_dev *dev,
3570 struct mlx5_ib_flow_prio *ft_prio,
3571 const struct ib_flow_attr *flow_attr,
3572 struct mlx5_flow_destination *dst,
Raed Salem3b3233f2018-05-31 16:43:39 +03003573 u32 underlay_qpn,
3574 struct mlx5_ib_create_flow *ucmd)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003575{
3576 struct mlx5_flow_table *ft = ft_prio->flow_table;
3577 struct mlx5_ib_flow_handler *handler;
Jianbo Liubb0ee7d2019-06-25 17:47:58 +00003578 struct mlx5_flow_act flow_act = {};
Maor Gottliebc5bb1732016-07-04 17:23:05 +03003579 struct mlx5_flow_spec *spec;
Raed Salem3b3233f2018-05-31 16:43:39 +03003580 struct mlx5_flow_destination dest_arr[2] = {};
3581 struct mlx5_flow_destination *rule_dst = dest_arr;
Maor Gottliebdd063d02016-08-28 14:16:32 +03003582 const void *ib_flow = (const void *)flow_attr + sizeof(*flow_attr);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003583 unsigned int spec_index;
Ariel Levkovich71c6e862018-05-13 14:33:34 +03003584 u32 prev_type = 0;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003585 int err = 0;
Raed Salem3b3233f2018-05-31 16:43:39 +03003586 int dest_num = 0;
Aviad Yehezkel802c2122018-03-28 09:27:53 +03003587 bool is_egress = flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003588
Ariel Levkovich19cc7522017-04-03 13:11:03 +03003589 if (!is_valid_attr(dev->mdev, flow_attr))
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003590 return ERR_PTR(-EINVAL);
3591
Mark Bloch6a4d00b2019-03-28 15:27:37 +02003592 if (dev->is_rep && is_egress)
Mark Bloch78dd0c42018-09-02 12:51:31 +03003593 return ERR_PTR(-EINVAL);
3594
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03003595 spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003596 handler = kzalloc(sizeof(*handler), GFP_KERNEL);
Maor Gottliebc5bb1732016-07-04 17:23:05 +03003597 if (!handler || !spec) {
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003598 err = -ENOMEM;
3599 goto free;
3600 }
3601
3602 INIT_LIST_HEAD(&handler->list);
3603
3604 for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
Jianbo Liubb0ee7d2019-06-25 17:47:58 +00003605 err = parse_flow_attr(dev->mdev, spec,
Ariel Levkovich71c6e862018-05-13 14:33:34 +03003606 ib_flow, flow_attr, &flow_act,
3607 prev_type);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003608 if (err < 0)
3609 goto free;
3610
Ariel Levkovich71c6e862018-05-13 14:33:34 +03003611 prev_type = ((union ib_flow_spec *)ib_flow)->type;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003612 ib_flow += ((union ib_flow_spec *)ib_flow)->size;
3613 }
3614
Maor Gottliebed9085f2019-12-12 11:12:14 +02003615 if (dst && !(flow_act.action & MLX5_FLOW_CONTEXT_ACTION_DROP)) {
3616 memcpy(&dest_arr[0], dst, sizeof(*dst));
3617 dest_num++;
3618 }
3619
Yishai Hadasa550ddf2017-08-17 15:52:33 +03003620 if (!flow_is_multicast_only(flow_attr))
3621 set_underlay_qp(dev, spec, underlay_qpn);
3622
Mark Bloch6a4d00b2019-03-28 15:27:37 +02003623 if (dev->is_rep) {
Jianbo Liu669ff1e2019-06-25 17:48:12 +00003624 struct mlx5_eswitch_rep *rep;
Mark Bloch018a94e2018-01-16 14:44:29 +00003625
Jianbo Liu669ff1e2019-06-25 17:48:12 +00003626 rep = dev->port[flow_attr->port - 1].rep;
3627 if (!rep) {
Mark Bloch6a4d00b2019-03-28 15:27:37 +02003628 err = -EINVAL;
3629 goto free;
3630 }
Jianbo Liu669ff1e2019-06-25 17:48:12 +00003631
3632 mlx5_ib_set_rule_source_port(dev, spec, rep);
Mark Bloch018a94e2018-01-16 14:44:29 +00003633 }
3634
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03003635 spec->match_criteria_enable = get_match_criteria_enable(spec->match_criteria);
Aviad Yehezkel802c2122018-03-28 09:27:53 +03003636
3637 if (is_egress &&
3638 !is_valid_spec(dev->mdev, spec, &flow_act, is_egress)) {
3639 err = -EINVAL;
3640 goto free;
3641 }
3642
Raed Salem3b3233f2018-05-31 16:43:39 +03003643 if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_COUNT) {
Mark Bloch171c7625b2018-10-03 00:03:35 +00003644 struct mlx5_ib_mcounters *mcounters;
3645
Raed Salem3b3233f2018-05-31 16:43:39 +03003646 err = flow_counters_set_data(flow_act.counters, ucmd);
3647 if (err)
3648 goto free;
3649
Mark Bloch171c7625b2018-10-03 00:03:35 +00003650 mcounters = to_mcounters(flow_act.counters);
Raed Salem3b3233f2018-05-31 16:43:39 +03003651 handler->ibcounters = flow_act.counters;
3652 dest_arr[dest_num].type =
3653 MLX5_FLOW_DESTINATION_TYPE_COUNTER;
Mark Bloch171c7625b2018-10-03 00:03:35 +00003654 dest_arr[dest_num].counter_id =
3655 mlx5_fc_id(mcounters->hw_cntrs_hndl);
Raed Salem3b3233f2018-05-31 16:43:39 +03003656 dest_num++;
3657 }
3658
Boris Pismenny075572d2017-08-16 09:33:30 +03003659 if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_DROP) {
Maor Gottliebed9085f2019-12-12 11:12:14 +02003660 if (!dest_num)
Raed Salem3b3233f2018-05-31 16:43:39 +03003661 rule_dst = NULL;
Slava Shwartsmana22ed862017-04-03 13:13:52 +03003662 } else {
Aviad Yehezkel802c2122018-03-28 09:27:53 +03003663 if (is_egress)
3664 flow_act.action |= MLX5_FLOW_CONTEXT_ACTION_ALLOW;
3665 else
3666 flow_act.action |=
Raed Salem3b3233f2018-05-31 16:43:39 +03003667 dest_num ? MLX5_FLOW_CONTEXT_ACTION_FWD_DEST :
Aviad Yehezkel802c2122018-03-28 09:27:53 +03003668 MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO;
Slava Shwartsmana22ed862017-04-03 13:13:52 +03003669 }
Moses Reuben2ac693f2017-01-18 14:59:50 +02003670
Jianbo Liubb0ee7d2019-06-25 17:47:58 +00003671 if ((spec->flow_context.flags & FLOW_CONTEXT_HAS_TAG) &&
Moses Reuben2ac693f2017-01-18 14:59:50 +02003672 (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
3673 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT)) {
3674 mlx5_ib_warn(dev, "Flow tag %u and attribute type %x isn't allowed in leftovers\n",
Jianbo Liubb0ee7d2019-06-25 17:47:58 +00003675 spec->flow_context.flow_tag, flow_attr->type);
Moses Reuben2ac693f2017-01-18 14:59:50 +02003676 err = -EINVAL;
3677 goto free;
3678 }
Mark Bloch74491de2016-08-31 11:24:25 +00003679 handler->rule = mlx5_add_flow_rules(ft, spec,
Hadar Hen Zion66958ed2016-11-07 15:14:45 +02003680 &flow_act,
Slava Shwartsmana22ed862017-04-03 13:13:52 +03003681 rule_dst, dest_num);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003682
3683 if (IS_ERR(handler->rule)) {
3684 err = PTR_ERR(handler->rule);
3685 goto free;
3686 }
3687
Maor Gottliebd9d49802016-08-28 14:16:33 +03003688 ft_prio->refcount++;
Maor Gottlieb5497adc2016-08-28 14:16:31 +03003689 handler->prio = ft_prio;
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003690 handler->dev = dev;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003691
3692 ft_prio->flow_table = ft;
3693free:
Raed Salem3b3233f2018-05-31 16:43:39 +03003694 if (err && handler) {
3695 if (handler->ibcounters &&
3696 atomic_read(&handler->ibcounters->usecnt) == 1)
3697 counters_clear_description(handler->ibcounters);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003698 kfree(handler);
Raed Salem3b3233f2018-05-31 16:43:39 +03003699 }
Maor Gottliebc5bb1732016-07-04 17:23:05 +03003700 kvfree(spec);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003701 return err ? ERR_PTR(err) : handler;
3702}
3703
Yishai Hadasa550ddf2017-08-17 15:52:33 +03003704static struct mlx5_ib_flow_handler *create_flow_rule(struct mlx5_ib_dev *dev,
3705 struct mlx5_ib_flow_prio *ft_prio,
3706 const struct ib_flow_attr *flow_attr,
3707 struct mlx5_flow_destination *dst)
3708{
Raed Salem3b3233f2018-05-31 16:43:39 +03003709 return _create_flow_rule(dev, ft_prio, flow_attr, dst, 0, NULL);
Yishai Hadasa550ddf2017-08-17 15:52:33 +03003710}
3711
Maor Gottlieb35d190112016-03-07 18:51:47 +02003712static struct mlx5_ib_flow_handler *create_dont_trap_rule(struct mlx5_ib_dev *dev,
3713 struct mlx5_ib_flow_prio *ft_prio,
3714 struct ib_flow_attr *flow_attr,
3715 struct mlx5_flow_destination *dst)
3716{
3717 struct mlx5_ib_flow_handler *handler_dst = NULL;
3718 struct mlx5_ib_flow_handler *handler = NULL;
3719
3720 handler = create_flow_rule(dev, ft_prio, flow_attr, NULL);
3721 if (!IS_ERR(handler)) {
3722 handler_dst = create_flow_rule(dev, ft_prio,
3723 flow_attr, dst);
3724 if (IS_ERR(handler_dst)) {
Mark Bloch74491de2016-08-31 11:24:25 +00003725 mlx5_del_flow_rules(handler->rule);
Maor Gottliebd9d49802016-08-28 14:16:33 +03003726 ft_prio->refcount--;
Maor Gottlieb35d190112016-03-07 18:51:47 +02003727 kfree(handler);
3728 handler = handler_dst;
3729 } else {
3730 list_add(&handler_dst->list, &handler->list);
3731 }
3732 }
3733
3734 return handler;
3735}
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003736enum {
3737 LEFTOVERS_MC,
3738 LEFTOVERS_UC,
3739};
3740
3741static struct mlx5_ib_flow_handler *create_leftovers_rule(struct mlx5_ib_dev *dev,
3742 struct mlx5_ib_flow_prio *ft_prio,
3743 struct ib_flow_attr *flow_attr,
3744 struct mlx5_flow_destination *dst)
3745{
3746 struct mlx5_ib_flow_handler *handler_ucast = NULL;
3747 struct mlx5_ib_flow_handler *handler = NULL;
3748
3749 static struct {
3750 struct ib_flow_attr flow_attr;
3751 struct ib_flow_spec_eth eth_flow;
3752 } leftovers_specs[] = {
3753 [LEFTOVERS_MC] = {
3754 .flow_attr = {
3755 .num_of_specs = 1,
3756 .size = sizeof(leftovers_specs[0])
3757 },
3758 .eth_flow = {
3759 .type = IB_FLOW_SPEC_ETH,
3760 .size = sizeof(struct ib_flow_spec_eth),
3761 .mask = {.dst_mac = {0x1} },
3762 .val = {.dst_mac = {0x1} }
3763 }
3764 },
3765 [LEFTOVERS_UC] = {
3766 .flow_attr = {
3767 .num_of_specs = 1,
3768 .size = sizeof(leftovers_specs[0])
3769 },
3770 .eth_flow = {
3771 .type = IB_FLOW_SPEC_ETH,
3772 .size = sizeof(struct ib_flow_spec_eth),
3773 .mask = {.dst_mac = {0x1} },
3774 .val = {.dst_mac = {} }
3775 }
3776 }
3777 };
3778
3779 handler = create_flow_rule(dev, ft_prio,
3780 &leftovers_specs[LEFTOVERS_MC].flow_attr,
3781 dst);
3782 if (!IS_ERR(handler) &&
3783 flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT) {
3784 handler_ucast = create_flow_rule(dev, ft_prio,
3785 &leftovers_specs[LEFTOVERS_UC].flow_attr,
3786 dst);
3787 if (IS_ERR(handler_ucast)) {
Mark Bloch74491de2016-08-31 11:24:25 +00003788 mlx5_del_flow_rules(handler->rule);
Maor Gottliebd9d49802016-08-28 14:16:33 +03003789 ft_prio->refcount--;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003790 kfree(handler);
3791 handler = handler_ucast;
3792 } else {
3793 list_add(&handler_ucast->list, &handler->list);
3794 }
3795 }
3796
3797 return handler;
3798}
3799
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003800static struct mlx5_ib_flow_handler *create_sniffer_rule(struct mlx5_ib_dev *dev,
3801 struct mlx5_ib_flow_prio *ft_rx,
3802 struct mlx5_ib_flow_prio *ft_tx,
3803 struct mlx5_flow_destination *dst)
3804{
3805 struct mlx5_ib_flow_handler *handler_rx;
3806 struct mlx5_ib_flow_handler *handler_tx;
3807 int err;
3808 static const struct ib_flow_attr flow_attr = {
3809 .num_of_specs = 0,
3810 .size = sizeof(flow_attr)
3811 };
3812
3813 handler_rx = create_flow_rule(dev, ft_rx, &flow_attr, dst);
3814 if (IS_ERR(handler_rx)) {
3815 err = PTR_ERR(handler_rx);
3816 goto err;
3817 }
3818
3819 handler_tx = create_flow_rule(dev, ft_tx, &flow_attr, dst);
3820 if (IS_ERR(handler_tx)) {
3821 err = PTR_ERR(handler_tx);
3822 goto err_tx;
3823 }
3824
3825 list_add(&handler_tx->list, &handler_rx->list);
3826
3827 return handler_rx;
3828
3829err_tx:
Mark Bloch74491de2016-08-31 11:24:25 +00003830 mlx5_del_flow_rules(handler_rx->rule);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003831 ft_rx->refcount--;
3832 kfree(handler_rx);
3833err:
3834 return ERR_PTR(err);
3835}
3836
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003837static struct ib_flow *mlx5_ib_create_flow(struct ib_qp *qp,
3838 struct ib_flow_attr *flow_attr,
Matan Barak59082a32018-05-31 16:43:35 +03003839 int domain,
3840 struct ib_udata *udata)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003841{
3842 struct mlx5_ib_dev *dev = to_mdev(qp->device);
Yishai Hadasd9f88e52016-08-28 10:58:37 +03003843 struct mlx5_ib_qp *mqp = to_mqp(qp);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003844 struct mlx5_ib_flow_handler *handler = NULL;
3845 struct mlx5_flow_destination *dst = NULL;
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003846 struct mlx5_ib_flow_prio *ft_prio_tx = NULL;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003847 struct mlx5_ib_flow_prio *ft_prio;
Aviad Yehezkel802c2122018-03-28 09:27:53 +03003848 bool is_egress = flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS;
Raed Salem3b3233f2018-05-31 16:43:39 +03003849 struct mlx5_ib_create_flow *ucmd = NULL, ucmd_hdr;
3850 size_t min_ucmd_sz, required_ucmd_sz;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003851 int err;
Yishai Hadasa550ddf2017-08-17 15:52:33 +03003852 int underlay_qpn;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003853
Raed Salem3b3233f2018-05-31 16:43:39 +03003854 if (udata && udata->inlen) {
3855 min_ucmd_sz = offsetof(typeof(ucmd_hdr), reserved) +
3856 sizeof(ucmd_hdr.reserved);
3857 if (udata->inlen < min_ucmd_sz)
3858 return ERR_PTR(-EOPNOTSUPP);
3859
3860 err = ib_copy_from_udata(&ucmd_hdr, udata, min_ucmd_sz);
3861 if (err)
3862 return ERR_PTR(err);
3863
3864 /* currently supports only one counters data */
3865 if (ucmd_hdr.ncounters_data > 1)
3866 return ERR_PTR(-EINVAL);
3867
3868 required_ucmd_sz = min_ucmd_sz +
3869 sizeof(struct mlx5_ib_flow_counters_data) *
3870 ucmd_hdr.ncounters_data;
3871 if (udata->inlen > required_ucmd_sz &&
3872 !ib_is_udata_cleared(udata, required_ucmd_sz,
3873 udata->inlen - required_ucmd_sz))
3874 return ERR_PTR(-EOPNOTSUPP);
3875
3876 ucmd = kzalloc(required_ucmd_sz, GFP_KERNEL);
3877 if (!ucmd)
3878 return ERR_PTR(-ENOMEM);
3879
3880 err = ib_copy_from_udata(ucmd, udata, required_ucmd_sz);
Gustavo A. R. Silva299eafe2018-06-07 14:19:15 -05003881 if (err)
3882 goto free_ucmd;
Raed Salem3b3233f2018-05-31 16:43:39 +03003883 }
Matan Barak59082a32018-05-31 16:43:35 +03003884
Gustavo A. R. Silva299eafe2018-06-07 14:19:15 -05003885 if (flow_attr->priority > MLX5_IB_FLOW_LAST_PRIO) {
3886 err = -ENOMEM;
3887 goto free_ucmd;
3888 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003889
3890 if (domain != IB_FLOW_DOMAIN_USER ||
Daniel Jurgens508562d2018-01-04 17:25:34 +02003891 flow_attr->port > dev->num_ports ||
Aviad Yehezkel802c2122018-03-28 09:27:53 +03003892 (flow_attr->flags & ~(IB_FLOW_ATTR_FLAGS_DONT_TRAP |
Gustavo A. R. Silva299eafe2018-06-07 14:19:15 -05003893 IB_FLOW_ATTR_FLAGS_EGRESS))) {
3894 err = -EINVAL;
3895 goto free_ucmd;
3896 }
Aviad Yehezkel802c2122018-03-28 09:27:53 +03003897
3898 if (is_egress &&
3899 (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
Gustavo A. R. Silva299eafe2018-06-07 14:19:15 -05003900 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT)) {
3901 err = -EINVAL;
3902 goto free_ucmd;
3903 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003904
3905 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
Gustavo A. R. Silva299eafe2018-06-07 14:19:15 -05003906 if (!dst) {
3907 err = -ENOMEM;
3908 goto free_ucmd;
3909 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003910
Mark Bloch9a4ca382018-01-16 14:42:35 +00003911 mutex_lock(&dev->flow_db->lock);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003912
Aviad Yehezkel802c2122018-03-28 09:27:53 +03003913 ft_prio = get_flow_table(dev, flow_attr,
3914 is_egress ? MLX5_IB_FT_TX : MLX5_IB_FT_RX);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003915 if (IS_ERR(ft_prio)) {
3916 err = PTR_ERR(ft_prio);
3917 goto unlock;
3918 }
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003919 if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
3920 ft_prio_tx = get_flow_table(dev, flow_attr, MLX5_IB_FT_TX);
3921 if (IS_ERR(ft_prio_tx)) {
3922 err = PTR_ERR(ft_prio_tx);
3923 ft_prio_tx = NULL;
3924 goto destroy_ft;
3925 }
3926 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003927
Aviad Yehezkel802c2122018-03-28 09:27:53 +03003928 if (is_egress) {
3929 dst->type = MLX5_FLOW_DESTINATION_TYPE_PORT;
3930 } else {
3931 dst->type = MLX5_FLOW_DESTINATION_TYPE_TIR;
3932 if (mqp->flags & MLX5_IB_QP_RSS)
3933 dst->tir_num = mqp->rss_qp.tirn;
3934 else
3935 dst->tir_num = mqp->raw_packet_qp.rq.tirn;
3936 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003937
3938 if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
Maor Gottlieb35d190112016-03-07 18:51:47 +02003939 if (flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP) {
3940 handler = create_dont_trap_rule(dev, ft_prio,
3941 flow_attr, dst);
3942 } else {
Yishai Hadasa550ddf2017-08-17 15:52:33 +03003943 underlay_qpn = (mqp->flags & MLX5_IB_QP_UNDERLAY) ?
3944 mqp->underlay_qpn : 0;
3945 handler = _create_flow_rule(dev, ft_prio, flow_attr,
Raed Salem3b3233f2018-05-31 16:43:39 +03003946 dst, underlay_qpn, ucmd);
Maor Gottlieb35d190112016-03-07 18:51:47 +02003947 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003948 } else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
3949 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
3950 handler = create_leftovers_rule(dev, ft_prio, flow_attr,
3951 dst);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003952 } else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
3953 handler = create_sniffer_rule(dev, ft_prio, ft_prio_tx, dst);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003954 } else {
3955 err = -EINVAL;
3956 goto destroy_ft;
3957 }
3958
3959 if (IS_ERR(handler)) {
3960 err = PTR_ERR(handler);
3961 handler = NULL;
3962 goto destroy_ft;
3963 }
3964
Mark Bloch9a4ca382018-01-16 14:42:35 +00003965 mutex_unlock(&dev->flow_db->lock);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003966 kfree(dst);
Raed Salem3b3233f2018-05-31 16:43:39 +03003967 kfree(ucmd);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003968
3969 return &handler->ibflow;
3970
3971destroy_ft:
3972 put_flow_table(dev, ft_prio, false);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003973 if (ft_prio_tx)
3974 put_flow_table(dev, ft_prio_tx, false);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003975unlock:
Mark Bloch9a4ca382018-01-16 14:42:35 +00003976 mutex_unlock(&dev->flow_db->lock);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003977 kfree(dst);
Gustavo A. R. Silva299eafe2018-06-07 14:19:15 -05003978free_ucmd:
Raed Salem3b3233f2018-05-31 16:43:39 +03003979 kfree(ucmd);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003980 return ERR_PTR(err);
3981}
3982
Mark Blochb47fd4f2018-09-06 17:27:07 +03003983static struct mlx5_ib_flow_prio *
3984_get_flow_table(struct mlx5_ib_dev *dev,
3985 struct mlx5_ib_flow_matcher *fs_matcher,
3986 bool mcast)
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003987{
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003988 struct mlx5_flow_namespace *ns = NULL;
Mark Bloch13a43762019-03-28 15:46:21 +02003989 struct mlx5_ib_flow_prio *prio = NULL;
3990 int max_table_size = 0;
Maor Gottliebcecae742019-06-12 15:20:13 +03003991 bool esw_encap;
Mark Blochb47fd4f2018-09-06 17:27:07 +03003992 u32 flags = 0;
3993 int priority;
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003994
Mark Bloch13a43762019-03-28 15:46:21 +02003995 if (mcast)
3996 priority = MLX5_IB_FLOW_MCAST_PRIO;
3997 else
3998 priority = ib_prio_to_core_prio(fs_matcher->priority, false);
3999
Maor Gottliebcecae742019-06-12 15:20:13 +03004000 esw_encap = mlx5_eswitch_get_encap_mode(dev->mdev) !=
4001 DEVLINK_ESWITCH_ENCAP_MODE_NONE;
Mark Blochb47fd4f2018-09-06 17:27:07 +03004002 if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_BYPASS) {
4003 max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
4004 log_max_ft_size));
Maor Gottliebcecae742019-06-12 15:20:13 +03004005 if (MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, decap) && !esw_encap)
Mark Blochb47fd4f2018-09-06 17:27:07 +03004006 flags |= MLX5_FLOW_TABLE_TUNNEL_EN_DECAP;
4007 if (MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
Maor Gottliebcecae742019-06-12 15:20:13 +03004008 reformat_l3_tunnel_to_l2) &&
4009 !esw_encap)
Mark Blochb47fd4f2018-09-06 17:27:07 +03004010 flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
Mark Bloch13a43762019-03-28 15:46:21 +02004011 } else if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_EGRESS) {
4012 max_table_size = BIT(
4013 MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev, log_max_ft_size));
Maor Gottliebcecae742019-06-12 15:20:13 +03004014 if (MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev, reformat) && !esw_encap)
Mark Blochb47fd4f2018-09-06 17:27:07 +03004015 flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
Mark Bloch13a43762019-03-28 15:46:21 +02004016 } else if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_FDB) {
4017 max_table_size = BIT(
4018 MLX5_CAP_ESW_FLOWTABLE_FDB(dev->mdev, log_max_ft_size));
Maor Gottlieb09d985b2019-06-12 15:20:14 +03004019 if (MLX5_CAP_ESW_FLOWTABLE_FDB(dev->mdev, decap) && esw_encap)
4020 flags |= MLX5_FLOW_TABLE_TUNNEL_EN_DECAP;
4021 if (MLX5_CAP_ESW_FLOWTABLE_FDB(dev->mdev, reformat_l3_tunnel_to_l2) &&
4022 esw_encap)
4023 flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
Mark Bloch13a43762019-03-28 15:46:21 +02004024 priority = FDB_BYPASS_PATH;
Mark Zhangd8abe882019-08-19 14:36:26 +03004025 } else if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_RDMA_RX) {
4026 max_table_size =
4027 BIT(MLX5_CAP_FLOWTABLE_RDMA_RX(dev->mdev,
4028 log_max_ft_size));
4029 priority = fs_matcher->priority;
Mark Blochb47fd4f2018-09-06 17:27:07 +03004030 }
4031
Mark Bloch3b705082019-03-28 15:46:22 +02004032 max_table_size = min_t(int, max_table_size, MLX5_FS_MAX_ENTRIES);
Yishai Hadasd4be3f42018-07-23 15:25:10 +03004033
Mark Blochb47fd4f2018-09-06 17:27:07 +03004034 ns = mlx5_get_flow_namespace(dev->mdev, fs_matcher->ns_type);
Yishai Hadasd4be3f42018-07-23 15:25:10 +03004035 if (!ns)
4036 return ERR_PTR(-ENOTSUPP);
4037
Mark Blochb47fd4f2018-09-06 17:27:07 +03004038 if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_BYPASS)
4039 prio = &dev->flow_db->prios[priority];
Mark Bloch13a43762019-03-28 15:46:21 +02004040 else if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_EGRESS)
Mark Blochb47fd4f2018-09-06 17:27:07 +03004041 prio = &dev->flow_db->egress_prios[priority];
Mark Bloch13a43762019-03-28 15:46:21 +02004042 else if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_FDB)
4043 prio = &dev->flow_db->fdb;
Mark Zhangd8abe882019-08-19 14:36:26 +03004044 else if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_RDMA_RX)
4045 prio = &dev->flow_db->rdma_rx[priority];
Mark Bloch13a43762019-03-28 15:46:21 +02004046
4047 if (!prio)
4048 return ERR_PTR(-EINVAL);
Yishai Hadasd4be3f42018-07-23 15:25:10 +03004049
4050 if (prio->flow_table)
4051 return prio;
4052
Mark Bloch3b705082019-03-28 15:46:22 +02004053 return _get_prio(ns, prio, priority, max_table_size,
Mark Blochb47fd4f2018-09-06 17:27:07 +03004054 MLX5_FS_MAX_TYPES, flags);
Yishai Hadasd4be3f42018-07-23 15:25:10 +03004055}
4056
4057static struct mlx5_ib_flow_handler *
4058_create_raw_flow_rule(struct mlx5_ib_dev *dev,
4059 struct mlx5_ib_flow_prio *ft_prio,
4060 struct mlx5_flow_destination *dst,
4061 struct mlx5_ib_flow_matcher *fs_matcher,
Jianbo Liubb0ee7d2019-06-25 17:47:58 +00004062 struct mlx5_flow_context *flow_context,
Mark Blochb823dd62018-09-06 17:27:05 +03004063 struct mlx5_flow_act *flow_act,
Mark Blochbfc5d832018-11-20 20:31:08 +02004064 void *cmd_in, int inlen,
4065 int dst_num)
Yishai Hadasd4be3f42018-07-23 15:25:10 +03004066{
4067 struct mlx5_ib_flow_handler *handler;
Yishai Hadasd4be3f42018-07-23 15:25:10 +03004068 struct mlx5_flow_spec *spec;
4069 struct mlx5_flow_table *ft = ft_prio->flow_table;
4070 int err = 0;
4071
4072 spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
4073 handler = kzalloc(sizeof(*handler), GFP_KERNEL);
4074 if (!handler || !spec) {
4075 err = -ENOMEM;
4076 goto free;
4077 }
4078
4079 INIT_LIST_HEAD(&handler->list);
4080
4081 memcpy(spec->match_value, cmd_in, inlen);
4082 memcpy(spec->match_criteria, fs_matcher->matcher_mask.match_params,
4083 fs_matcher->mask_len);
4084 spec->match_criteria_enable = fs_matcher->match_criteria_enable;
Jianbo Liubb0ee7d2019-06-25 17:47:58 +00004085 spec->flow_context = *flow_context;
Yishai Hadasd4be3f42018-07-23 15:25:10 +03004086
Yishai Hadasd4be3f42018-07-23 15:25:10 +03004087 handler->rule = mlx5_add_flow_rules(ft, spec,
Mark Blochbfc5d832018-11-20 20:31:08 +02004088 flow_act, dst, dst_num);
Yishai Hadasd4be3f42018-07-23 15:25:10 +03004089
4090 if (IS_ERR(handler->rule)) {
4091 err = PTR_ERR(handler->rule);
4092 goto free;
4093 }
4094
4095 ft_prio->refcount++;
4096 handler->prio = ft_prio;
4097 handler->dev = dev;
4098 ft_prio->flow_table = ft;
4099
4100free:
4101 if (err)
4102 kfree(handler);
4103 kvfree(spec);
4104 return err ? ERR_PTR(err) : handler;
4105}
4106
4107static bool raw_fs_is_multicast(struct mlx5_ib_flow_matcher *fs_matcher,
4108 void *match_v)
4109{
4110 void *match_c;
4111 void *match_v_set_lyr_2_4, *match_c_set_lyr_2_4;
4112 void *dmac, *dmac_mask;
4113 void *ipv4, *ipv4_mask;
4114
4115 if (!(fs_matcher->match_criteria_enable &
4116 (1 << MATCH_CRITERIA_ENABLE_OUTER_BIT)))
4117 return false;
4118
4119 match_c = fs_matcher->matcher_mask.match_params;
4120 match_v_set_lyr_2_4 = MLX5_ADDR_OF(fte_match_param, match_v,
4121 outer_headers);
4122 match_c_set_lyr_2_4 = MLX5_ADDR_OF(fte_match_param, match_c,
4123 outer_headers);
4124
4125 dmac = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_v_set_lyr_2_4,
4126 dmac_47_16);
4127 dmac_mask = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_c_set_lyr_2_4,
4128 dmac_47_16);
4129
4130 if (is_multicast_ether_addr(dmac) &&
4131 is_multicast_ether_addr(dmac_mask))
4132 return true;
4133
4134 ipv4 = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_v_set_lyr_2_4,
4135 dst_ipv4_dst_ipv6.ipv4_layout.ipv4);
4136
4137 ipv4_mask = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_c_set_lyr_2_4,
4138 dst_ipv4_dst_ipv6.ipv4_layout.ipv4);
4139
4140 if (ipv4_is_multicast(*(__be32 *)(ipv4)) &&
4141 ipv4_is_multicast(*(__be32 *)(ipv4_mask)))
4142 return true;
4143
4144 return false;
4145}
4146
Yishai Hadas32269442018-07-23 15:25:09 +03004147struct mlx5_ib_flow_handler *
4148mlx5_ib_raw_fs_rule_add(struct mlx5_ib_dev *dev,
4149 struct mlx5_ib_flow_matcher *fs_matcher,
Jianbo Liubb0ee7d2019-06-25 17:47:58 +00004150 struct mlx5_flow_context *flow_context,
Mark Blochb823dd62018-09-06 17:27:05 +03004151 struct mlx5_flow_act *flow_act,
Mark Blochbfc5d832018-11-20 20:31:08 +02004152 u32 counter_id,
Yishai Hadas32269442018-07-23 15:25:09 +03004153 void *cmd_in, int inlen, int dest_id,
4154 int dest_type)
4155{
Yishai Hadasd4be3f42018-07-23 15:25:10 +03004156 struct mlx5_flow_destination *dst;
4157 struct mlx5_ib_flow_prio *ft_prio;
Yishai Hadasd4be3f42018-07-23 15:25:10 +03004158 struct mlx5_ib_flow_handler *handler;
Mark Blochbfc5d832018-11-20 20:31:08 +02004159 int dst_num = 0;
Yishai Hadasd4be3f42018-07-23 15:25:10 +03004160 bool mcast;
4161 int err;
4162
4163 if (fs_matcher->flow_type != MLX5_IB_FLOW_TYPE_NORMAL)
4164 return ERR_PTR(-EOPNOTSUPP);
4165
4166 if (fs_matcher->priority > MLX5_IB_FLOW_LAST_PRIO)
4167 return ERR_PTR(-ENOMEM);
4168
Gustavo A. R. Silva8e8aa142019-01-15 00:00:48 -06004169 dst = kcalloc(2, sizeof(*dst), GFP_KERNEL);
Yishai Hadasd4be3f42018-07-23 15:25:10 +03004170 if (!dst)
4171 return ERR_PTR(-ENOMEM);
4172
4173 mcast = raw_fs_is_multicast(fs_matcher, cmd_in);
4174 mutex_lock(&dev->flow_db->lock);
4175
Mark Blochb47fd4f2018-09-06 17:27:07 +03004176 ft_prio = _get_flow_table(dev, fs_matcher, mcast);
Yishai Hadasd4be3f42018-07-23 15:25:10 +03004177 if (IS_ERR(ft_prio)) {
4178 err = PTR_ERR(ft_prio);
4179 goto unlock;
4180 }
4181
Yishai Hadas6346f0b2018-07-23 15:25:11 +03004182 if (dest_type == MLX5_FLOW_DESTINATION_TYPE_TIR) {
Mark Blochbfc5d832018-11-20 20:31:08 +02004183 dst[dst_num].type = dest_type;
4184 dst[dst_num].tir_num = dest_id;
Mark Blochb823dd62018-09-06 17:27:05 +03004185 flow_act->action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
Mark Blocha7ee18b2018-09-06 17:27:08 +03004186 } else if (dest_type == MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE) {
Mark Blochbfc5d832018-11-20 20:31:08 +02004187 dst[dst_num].type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE_NUM;
4188 dst[dst_num].ft_num = dest_id;
Mark Blochb823dd62018-09-06 17:27:05 +03004189 flow_act->action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
Mark Blocha7ee18b2018-09-06 17:27:08 +03004190 } else {
Mark Blochbfc5d832018-11-20 20:31:08 +02004191 dst[dst_num].type = MLX5_FLOW_DESTINATION_TYPE_PORT;
Mark Blocha7ee18b2018-09-06 17:27:08 +03004192 flow_act->action |= MLX5_FLOW_CONTEXT_ACTION_ALLOW;
Yishai Hadas6346f0b2018-07-23 15:25:11 +03004193 }
4194
Mark Blochbfc5d832018-11-20 20:31:08 +02004195 dst_num++;
4196
4197 if (flow_act->action & MLX5_FLOW_CONTEXT_ACTION_COUNT) {
4198 dst[dst_num].type = MLX5_FLOW_DESTINATION_TYPE_COUNTER;
4199 dst[dst_num].counter_id = counter_id;
4200 dst_num++;
4201 }
4202
Jianbo Liubb0ee7d2019-06-25 17:47:58 +00004203 handler = _create_raw_flow_rule(dev, ft_prio, dst, fs_matcher,
4204 flow_context, flow_act,
Mark Blochbfc5d832018-11-20 20:31:08 +02004205 cmd_in, inlen, dst_num);
Yishai Hadasd4be3f42018-07-23 15:25:10 +03004206
4207 if (IS_ERR(handler)) {
4208 err = PTR_ERR(handler);
4209 goto destroy_ft;
4210 }
4211
4212 mutex_unlock(&dev->flow_db->lock);
4213 atomic_inc(&fs_matcher->usecnt);
4214 handler->flow_matcher = fs_matcher;
4215
4216 kfree(dst);
4217
4218 return handler;
4219
4220destroy_ft:
4221 put_flow_table(dev, ft_prio, false);
4222unlock:
4223 mutex_unlock(&dev->flow_db->lock);
4224 kfree(dst);
4225
4226 return ERR_PTR(err);
Yishai Hadas32269442018-07-23 15:25:09 +03004227}
4228
Aviad Yehezkelc6475a02018-03-28 09:27:50 +03004229static u32 mlx5_ib_flow_action_flags_to_accel_xfrm_flags(u32 mlx5_flags)
4230{
4231 u32 flags = 0;
4232
4233 if (mlx5_flags & MLX5_IB_UAPI_FLOW_ACTION_FLAGS_REQUIRE_METADATA)
4234 flags |= MLX5_ACCEL_XFRM_FLAG_REQUIRE_METADATA;
4235
4236 return flags;
4237}
4238
4239#define MLX5_FLOW_ACTION_ESP_CREATE_LAST_SUPPORTED MLX5_IB_UAPI_FLOW_ACTION_FLAGS_REQUIRE_METADATA
4240static struct ib_flow_action *
4241mlx5_ib_create_flow_action_esp(struct ib_device *device,
4242 const struct ib_flow_action_attrs_esp *attr,
4243 struct uverbs_attr_bundle *attrs)
4244{
4245 struct mlx5_ib_dev *mdev = to_mdev(device);
4246 struct ib_uverbs_flow_action_esp_keymat_aes_gcm *aes_gcm;
4247 struct mlx5_accel_esp_xfrm_attrs accel_attrs = {};
4248 struct mlx5_ib_flow_action *action;
4249 u64 action_flags;
4250 u64 flags;
4251 int err = 0;
4252
Jason Gunthorpebccd0622018-07-26 16:37:14 -06004253 err = uverbs_get_flags64(
4254 &action_flags, attrs, MLX5_IB_ATTR_CREATE_FLOW_ACTION_FLAGS,
4255 ((MLX5_FLOW_ACTION_ESP_CREATE_LAST_SUPPORTED << 1) - 1));
4256 if (err)
4257 return ERR_PTR(err);
Aviad Yehezkelc6475a02018-03-28 09:27:50 +03004258
4259 flags = mlx5_ib_flow_action_flags_to_accel_xfrm_flags(action_flags);
4260
4261 /* We current only support a subset of the standard features. Only a
4262 * keymat of type AES_GCM, with icv_len == 16, iv_algo == SEQ and esn
4263 * (with overlap). Full offload mode isn't supported.
4264 */
4265 if (!attr->keymat || attr->replay || attr->encap ||
4266 attr->spi || attr->seq || attr->tfc_pad ||
4267 attr->hard_limit_pkts ||
4268 (attr->flags & ~(IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED |
4269 IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ENCRYPT)))
4270 return ERR_PTR(-EOPNOTSUPP);
4271
4272 if (attr->keymat->protocol !=
4273 IB_UVERBS_FLOW_ACTION_ESP_KEYMAT_AES_GCM)
4274 return ERR_PTR(-EOPNOTSUPP);
4275
4276 aes_gcm = &attr->keymat->keymat.aes_gcm;
4277
4278 if (aes_gcm->icv_len != 16 ||
4279 aes_gcm->iv_algo != IB_UVERBS_FLOW_ACTION_IV_ALGO_SEQ)
4280 return ERR_PTR(-EOPNOTSUPP);
4281
4282 action = kmalloc(sizeof(*action), GFP_KERNEL);
4283 if (!action)
4284 return ERR_PTR(-ENOMEM);
4285
4286 action->esp_aes_gcm.ib_flags = attr->flags;
4287 memcpy(&accel_attrs.keymat.aes_gcm.aes_key, &aes_gcm->aes_key,
4288 sizeof(accel_attrs.keymat.aes_gcm.aes_key));
4289 accel_attrs.keymat.aes_gcm.key_len = aes_gcm->key_len * 8;
4290 memcpy(&accel_attrs.keymat.aes_gcm.salt, &aes_gcm->salt,
4291 sizeof(accel_attrs.keymat.aes_gcm.salt));
4292 memcpy(&accel_attrs.keymat.aes_gcm.seq_iv, &aes_gcm->iv,
4293 sizeof(accel_attrs.keymat.aes_gcm.seq_iv));
4294 accel_attrs.keymat.aes_gcm.icv_len = aes_gcm->icv_len * 8;
4295 accel_attrs.keymat.aes_gcm.iv_algo = MLX5_ACCEL_ESP_AES_GCM_IV_ALGO_SEQ;
4296 accel_attrs.keymat_type = MLX5_ACCEL_ESP_KEYMAT_AES_GCM;
4297
4298 accel_attrs.esn = attr->esn;
4299 if (attr->flags & IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED)
4300 accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_TRIGGERED;
4301 if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW)
4302 accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP;
4303
4304 if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ENCRYPT)
4305 accel_attrs.action |= MLX5_ACCEL_ESP_ACTION_ENCRYPT;
4306
4307 action->esp_aes_gcm.ctx =
4308 mlx5_accel_esp_create_xfrm(mdev->mdev, &accel_attrs, flags);
4309 if (IS_ERR(action->esp_aes_gcm.ctx)) {
4310 err = PTR_ERR(action->esp_aes_gcm.ctx);
4311 goto err_parse;
4312 }
4313
4314 action->esp_aes_gcm.ib_flags = attr->flags;
4315
4316 return &action->ib_action;
4317
4318err_parse:
4319 kfree(action);
4320 return ERR_PTR(err);
4321}
4322
Matan Barak349705c2018-03-28 09:27:51 +03004323static int
4324mlx5_ib_modify_flow_action_esp(struct ib_flow_action *action,
4325 const struct ib_flow_action_attrs_esp *attr,
4326 struct uverbs_attr_bundle *attrs)
4327{
4328 struct mlx5_ib_flow_action *maction = to_mflow_act(action);
4329 struct mlx5_accel_esp_xfrm_attrs accel_attrs;
4330 int err = 0;
4331
4332 if (attr->keymat || attr->replay || attr->encap ||
4333 attr->spi || attr->seq || attr->tfc_pad ||
4334 attr->hard_limit_pkts ||
4335 (attr->flags & ~(IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED |
4336 IB_FLOW_ACTION_ESP_FLAGS_MOD_ESP_ATTRS |
4337 IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW)))
4338 return -EOPNOTSUPP;
4339
4340 /* Only the ESN value or the MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP can
4341 * be modified.
4342 */
4343 if (!(maction->esp_aes_gcm.ib_flags &
4344 IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED) &&
4345 attr->flags & (IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED |
4346 IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW))
4347 return -EINVAL;
4348
4349 memcpy(&accel_attrs, &maction->esp_aes_gcm.ctx->attrs,
4350 sizeof(accel_attrs));
4351
4352 accel_attrs.esn = attr->esn;
4353 if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW)
4354 accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP;
4355 else
4356 accel_attrs.flags &= ~MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP;
4357
4358 err = mlx5_accel_esp_modify_xfrm(maction->esp_aes_gcm.ctx,
4359 &accel_attrs);
4360 if (err)
4361 return err;
4362
4363 maction->esp_aes_gcm.ib_flags &=
4364 ~IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW;
4365 maction->esp_aes_gcm.ib_flags |=
4366 attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW;
4367
4368 return 0;
4369}
4370
Aviad Yehezkelc6475a02018-03-28 09:27:50 +03004371static int mlx5_ib_destroy_flow_action(struct ib_flow_action *action)
4372{
4373 struct mlx5_ib_flow_action *maction = to_mflow_act(action);
4374
4375 switch (action->type) {
4376 case IB_FLOW_ACTION_ESP:
4377 /*
4378 * We only support aes_gcm by now, so we implicitly know this is
4379 * the underline crypto.
4380 */
4381 mlx5_accel_esp_destroy_xfrm(maction->esp_aes_gcm.ctx);
4382 break;
Mark Blochb4749bf2018-08-28 14:18:51 +03004383 case IB_FLOW_ACTION_UNSPECIFIED:
4384 mlx5_ib_destroy_flow_action_raw(maction);
4385 break;
Aviad Yehezkelc6475a02018-03-28 09:27:50 +03004386 default:
4387 WARN_ON(true);
4388 break;
4389 }
4390
4391 kfree(maction);
4392 return 0;
4393}
4394
Eli Cohene126ba92013-07-07 17:25:49 +03004395static int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
4396{
4397 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
Yishai Hadas81e30882017-06-08 16:15:09 +03004398 struct mlx5_ib_qp *mqp = to_mqp(ibqp);
Eli Cohene126ba92013-07-07 17:25:49 +03004399 int err;
Yishai Hadas539ec982018-09-20 21:39:25 +03004400 u16 uid;
4401
4402 uid = ibqp->pd ?
4403 to_mpd(ibqp->pd)->uid : 0;
Eli Cohene126ba92013-07-07 17:25:49 +03004404
Yishai Hadas81e30882017-06-08 16:15:09 +03004405 if (mqp->flags & MLX5_IB_QP_UNDERLAY) {
4406 mlx5_ib_dbg(dev, "Attaching a multi cast group to underlay QP is not supported\n");
4407 return -EOPNOTSUPP;
4408 }
4409
Yishai Hadas539ec982018-09-20 21:39:25 +03004410 err = mlx5_cmd_attach_mcg(dev->mdev, gid, ibqp->qp_num, uid);
Eli Cohene126ba92013-07-07 17:25:49 +03004411 if (err)
4412 mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n",
4413 ibqp->qp_num, gid->raw);
4414
4415 return err;
4416}
4417
4418static int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
4419{
4420 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
4421 int err;
Yishai Hadas539ec982018-09-20 21:39:25 +03004422 u16 uid;
Eli Cohene126ba92013-07-07 17:25:49 +03004423
Yishai Hadas539ec982018-09-20 21:39:25 +03004424 uid = ibqp->pd ?
4425 to_mpd(ibqp->pd)->uid : 0;
4426 err = mlx5_cmd_detach_mcg(dev->mdev, gid, ibqp->qp_num, uid);
Eli Cohene126ba92013-07-07 17:25:49 +03004427 if (err)
4428 mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n",
4429 ibqp->qp_num, gid->raw);
4430
4431 return err;
4432}
4433
4434static int init_node_data(struct mlx5_ib_dev *dev)
4435{
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03004436 int err;
Eli Cohene126ba92013-07-07 17:25:49 +03004437
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03004438 err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc);
Eli Cohene126ba92013-07-07 17:25:49 +03004439 if (err)
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03004440 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03004441
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03004442 dev->mdev->rev_id = dev->mdev->pdev->revision;
Eli Cohene126ba92013-07-07 17:25:49 +03004443
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03004444 return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid);
Eli Cohene126ba92013-07-07 17:25:49 +03004445}
4446
Parav Pandit508a5232018-10-11 22:31:54 +03004447static ssize_t fw_pages_show(struct device *device,
4448 struct device_attribute *attr, char *buf)
Eli Cohene126ba92013-07-07 17:25:49 +03004449{
4450 struct mlx5_ib_dev *dev =
Parav Pandit54747232018-12-18 14:15:56 +02004451 rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
Eli Cohene126ba92013-07-07 17:25:49 +03004452
Jack Morgenstein9603b612014-07-28 23:30:22 +03004453 return sprintf(buf, "%d\n", dev->mdev->priv.fw_pages);
Eli Cohene126ba92013-07-07 17:25:49 +03004454}
Parav Pandit508a5232018-10-11 22:31:54 +03004455static DEVICE_ATTR_RO(fw_pages);
Eli Cohene126ba92013-07-07 17:25:49 +03004456
Parav Pandit508a5232018-10-11 22:31:54 +03004457static ssize_t reg_pages_show(struct device *device,
Eli Cohene126ba92013-07-07 17:25:49 +03004458 struct device_attribute *attr, char *buf)
4459{
4460 struct mlx5_ib_dev *dev =
Parav Pandit54747232018-12-18 14:15:56 +02004461 rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
Eli Cohene126ba92013-07-07 17:25:49 +03004462
Haggai Eran6aec21f2014-12-11 17:04:23 +02004463 return sprintf(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages));
Eli Cohene126ba92013-07-07 17:25:49 +03004464}
Parav Pandit508a5232018-10-11 22:31:54 +03004465static DEVICE_ATTR_RO(reg_pages);
Eli Cohene126ba92013-07-07 17:25:49 +03004466
Parav Pandit508a5232018-10-11 22:31:54 +03004467static ssize_t hca_type_show(struct device *device,
4468 struct device_attribute *attr, char *buf)
Eli Cohene126ba92013-07-07 17:25:49 +03004469{
4470 struct mlx5_ib_dev *dev =
Parav Pandit54747232018-12-18 14:15:56 +02004471 rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
4472
Jack Morgenstein9603b612014-07-28 23:30:22 +03004473 return sprintf(buf, "MT%d\n", dev->mdev->pdev->device);
Eli Cohene126ba92013-07-07 17:25:49 +03004474}
Parav Pandit508a5232018-10-11 22:31:54 +03004475static DEVICE_ATTR_RO(hca_type);
Eli Cohene126ba92013-07-07 17:25:49 +03004476
Parav Pandit508a5232018-10-11 22:31:54 +03004477static ssize_t hw_rev_show(struct device *device,
4478 struct device_attribute *attr, char *buf)
Eli Cohene126ba92013-07-07 17:25:49 +03004479{
4480 struct mlx5_ib_dev *dev =
Parav Pandit54747232018-12-18 14:15:56 +02004481 rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
4482
Jack Morgenstein9603b612014-07-28 23:30:22 +03004483 return sprintf(buf, "%x\n", dev->mdev->rev_id);
Eli Cohene126ba92013-07-07 17:25:49 +03004484}
Parav Pandit508a5232018-10-11 22:31:54 +03004485static DEVICE_ATTR_RO(hw_rev);
Eli Cohene126ba92013-07-07 17:25:49 +03004486
Parav Pandit508a5232018-10-11 22:31:54 +03004487static ssize_t board_id_show(struct device *device,
4488 struct device_attribute *attr, char *buf)
Eli Cohene126ba92013-07-07 17:25:49 +03004489{
4490 struct mlx5_ib_dev *dev =
Parav Pandit54747232018-12-18 14:15:56 +02004491 rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
4492
Eli Cohene126ba92013-07-07 17:25:49 +03004493 return sprintf(buf, "%.*s\n", MLX5_BOARD_ID_LEN,
Jack Morgenstein9603b612014-07-28 23:30:22 +03004494 dev->mdev->board_id);
Eli Cohene126ba92013-07-07 17:25:49 +03004495}
Parav Pandit508a5232018-10-11 22:31:54 +03004496static DEVICE_ATTR_RO(board_id);
Eli Cohene126ba92013-07-07 17:25:49 +03004497
Parav Pandit508a5232018-10-11 22:31:54 +03004498static struct attribute *mlx5_class_attributes[] = {
4499 &dev_attr_hw_rev.attr,
4500 &dev_attr_hca_type.attr,
4501 &dev_attr_board_id.attr,
4502 &dev_attr_fw_pages.attr,
4503 &dev_attr_reg_pages.attr,
4504 NULL,
4505};
Eli Cohene126ba92013-07-07 17:25:49 +03004506
Parav Pandit508a5232018-10-11 22:31:54 +03004507static const struct attribute_group mlx5_attr_group = {
4508 .attrs = mlx5_class_attributes,
Eli Cohene126ba92013-07-07 17:25:49 +03004509};
4510
Haggai Eran7722f472016-02-29 15:45:07 +02004511static void pkey_change_handler(struct work_struct *work)
4512{
4513 struct mlx5_ib_port_resources *ports =
4514 container_of(work, struct mlx5_ib_port_resources,
4515 pkey_change_work);
4516
4517 mutex_lock(&ports->devr->mutex);
4518 mlx5_ib_gsi_pkey_change(ports->gsi);
4519 mutex_unlock(&ports->devr->mutex);
4520}
4521
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03004522static void mlx5_ib_handle_internal_error(struct mlx5_ib_dev *ibdev)
4523{
4524 struct mlx5_ib_qp *mqp;
4525 struct mlx5_ib_cq *send_mcq, *recv_mcq;
4526 struct mlx5_core_cq *mcq;
4527 struct list_head cq_armed_list;
4528 unsigned long flags_qp;
4529 unsigned long flags_cq;
4530 unsigned long flags;
4531
4532 INIT_LIST_HEAD(&cq_armed_list);
4533
4534 /* Go over qp list reside on that ibdev, sync with create/destroy qp.*/
4535 spin_lock_irqsave(&ibdev->reset_flow_resource_lock, flags);
4536 list_for_each_entry(mqp, &ibdev->qp_list, qps_list) {
4537 spin_lock_irqsave(&mqp->sq.lock, flags_qp);
4538 if (mqp->sq.tail != mqp->sq.head) {
4539 send_mcq = to_mcq(mqp->ibqp.send_cq);
4540 spin_lock_irqsave(&send_mcq->lock, flags_cq);
4541 if (send_mcq->mcq.comp &&
4542 mqp->ibqp.send_cq->comp_handler) {
4543 if (!send_mcq->mcq.reset_notify_added) {
4544 send_mcq->mcq.reset_notify_added = 1;
4545 list_add_tail(&send_mcq->mcq.reset_notify,
4546 &cq_armed_list);
4547 }
4548 }
4549 spin_unlock_irqrestore(&send_mcq->lock, flags_cq);
4550 }
4551 spin_unlock_irqrestore(&mqp->sq.lock, flags_qp);
4552 spin_lock_irqsave(&mqp->rq.lock, flags_qp);
4553 /* no handling is needed for SRQ */
4554 if (!mqp->ibqp.srq) {
4555 if (mqp->rq.tail != mqp->rq.head) {
4556 recv_mcq = to_mcq(mqp->ibqp.recv_cq);
4557 spin_lock_irqsave(&recv_mcq->lock, flags_cq);
4558 if (recv_mcq->mcq.comp &&
4559 mqp->ibqp.recv_cq->comp_handler) {
4560 if (!recv_mcq->mcq.reset_notify_added) {
4561 recv_mcq->mcq.reset_notify_added = 1;
4562 list_add_tail(&recv_mcq->mcq.reset_notify,
4563 &cq_armed_list);
4564 }
4565 }
4566 spin_unlock_irqrestore(&recv_mcq->lock,
4567 flags_cq);
4568 }
4569 }
4570 spin_unlock_irqrestore(&mqp->rq.lock, flags_qp);
4571 }
4572 /*At that point all inflight post send were put to be executed as of we
4573 * lock/unlock above locks Now need to arm all involved CQs.
4574 */
4575 list_for_each_entry(mcq, &cq_armed_list, reset_notify) {
Yishai Hadas4e0e2ea2019-06-30 19:23:27 +03004576 mcq->comp(mcq, NULL);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03004577 }
4578 spin_unlock_irqrestore(&ibdev->reset_flow_resource_lock, flags);
4579}
4580
Maor Gottlieb03404e82017-05-30 10:29:13 +03004581static void delay_drop_handler(struct work_struct *work)
4582{
4583 int err;
4584 struct mlx5_ib_delay_drop *delay_drop =
4585 container_of(work, struct mlx5_ib_delay_drop,
4586 delay_drop_work);
4587
Maor Gottliebfe248c32017-05-30 10:29:14 +03004588 atomic_inc(&delay_drop->events_cnt);
4589
Maor Gottlieb03404e82017-05-30 10:29:13 +03004590 mutex_lock(&delay_drop->lock);
4591 err = mlx5_core_set_delay_drop(delay_drop->dev->mdev,
4592 delay_drop->timeout);
4593 if (err) {
4594 mlx5_ib_warn(delay_drop->dev, "Failed to set delay drop, timeout=%u\n",
4595 delay_drop->timeout);
4596 delay_drop->activate = false;
4597 }
4598 mutex_unlock(&delay_drop->lock);
4599}
4600
Saeed Mahameed09e574f2018-11-26 14:39:04 -08004601static void handle_general_event(struct mlx5_ib_dev *ibdev, struct mlx5_eqe *eqe,
4602 struct ib_event *ibev)
4603{
Aya Levin6cfdc7e2019-04-29 18:14:07 +00004604 u8 port = (eqe->data.port.port >> 4) & 0xf;
4605
Saeed Mahameed09e574f2018-11-26 14:39:04 -08004606 switch (eqe->sub_type) {
4607 case MLX5_GENERAL_SUBTYPE_DELAY_DROP_TIMEOUT:
Aya Levin6cfdc7e2019-04-29 18:14:07 +00004608 if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) ==
4609 IB_LINK_LAYER_ETHERNET)
4610 schedule_work(&ibdev->delay_drop.delay_drop_work);
Saeed Mahameed09e574f2018-11-26 14:39:04 -08004611 break;
4612 default: /* do nothing */
4613 return;
4614 }
4615}
4616
Saeed Mahameed134e9342018-11-26 14:39:02 -08004617static int handle_port_change(struct mlx5_ib_dev *ibdev, struct mlx5_eqe *eqe,
4618 struct ib_event *ibev)
4619{
4620 u8 port = (eqe->data.port.port >> 4) & 0xf;
4621
4622 ibev->element.port_num = port;
4623
4624 switch (eqe->sub_type) {
4625 case MLX5_PORT_CHANGE_SUBTYPE_ACTIVE:
4626 case MLX5_PORT_CHANGE_SUBTYPE_DOWN:
4627 case MLX5_PORT_CHANGE_SUBTYPE_INITIALIZED:
4628 /* In RoCE, port up/down events are handled in
4629 * mlx5_netdev_event().
4630 */
4631 if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) ==
4632 IB_LINK_LAYER_ETHERNET)
4633 return -EINVAL;
4634
4635 ibev->event = (eqe->sub_type == MLX5_PORT_CHANGE_SUBTYPE_ACTIVE) ?
4636 IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
4637 break;
4638
4639 case MLX5_PORT_CHANGE_SUBTYPE_LID:
4640 ibev->event = IB_EVENT_LID_CHANGE;
4641 break;
4642
4643 case MLX5_PORT_CHANGE_SUBTYPE_PKEY:
4644 ibev->event = IB_EVENT_PKEY_CHANGE;
4645 schedule_work(&ibdev->devr.ports[port - 1].pkey_change_work);
4646 break;
4647
4648 case MLX5_PORT_CHANGE_SUBTYPE_GUID:
4649 ibev->event = IB_EVENT_GID_CHANGE;
4650 break;
4651
4652 case MLX5_PORT_CHANGE_SUBTYPE_CLIENT_REREG:
4653 ibev->event = IB_EVENT_CLIENT_REREGISTER;
4654 break;
4655 default:
4656 return -EINVAL;
4657 }
4658
4659 return 0;
4660}
4661
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02004662static void mlx5_ib_handle_event(struct work_struct *_work)
Eli Cohene126ba92013-07-07 17:25:49 +03004663{
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02004664 struct mlx5_ib_event_work *work =
4665 container_of(_work, struct mlx5_ib_event_work, work);
4666 struct mlx5_ib_dev *ibdev;
Eli Cohene126ba92013-07-07 17:25:49 +03004667 struct ib_event ibev;
Eli Cohendbaaff22016-10-27 16:36:44 +03004668 bool fatal = false;
Eli Cohene126ba92013-07-07 17:25:49 +03004669
Saeed Mahameeddf097a22018-11-26 14:39:00 -08004670 if (work->is_slave) {
4671 ibdev = mlx5_ib_get_ibdev_from_mpi(work->mpi);
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02004672 if (!ibdev)
4673 goto out;
4674 } else {
Saeed Mahameeddf097a22018-11-26 14:39:00 -08004675 ibdev = work->dev;
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02004676 }
4677
4678 switch (work->event) {
Eli Cohene126ba92013-07-07 17:25:49 +03004679 case MLX5_DEV_EVENT_SYS_ERROR:
Eli Cohene126ba92013-07-07 17:25:49 +03004680 ibev.event = IB_EVENT_DEVICE_FATAL;
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03004681 mlx5_ib_handle_internal_error(ibdev);
Saeed Mahameed134e9342018-11-26 14:39:02 -08004682 ibev.element.port_num = (u8)(unsigned long)work->param;
Eli Cohendbaaff22016-10-27 16:36:44 +03004683 fatal = true;
Eli Cohene126ba92013-07-07 17:25:49 +03004684 break;
Saeed Mahameed134e9342018-11-26 14:39:02 -08004685 case MLX5_EVENT_TYPE_PORT_CHANGE:
4686 if (handle_port_change(ibdev, work->param, &ibev))
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02004687 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03004688 break;
Saeed Mahameed09e574f2018-11-26 14:39:04 -08004689 case MLX5_EVENT_TYPE_GENERAL_EVENT:
4690 handle_general_event(ibdev, work->param, &ibev);
4691 /* fall through */
Saeed Mahameedbdc37922016-09-29 19:35:38 +03004692 default:
Maor Gottlieb03404e82017-05-30 10:29:13 +03004693 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03004694 }
4695
Saeed Mahameed134e9342018-11-26 14:39:02 -08004696 ibev.device = &ibdev->ib_dev;
Eli Cohene126ba92013-07-07 17:25:49 +03004697
Saeed Mahameed134e9342018-11-26 14:39:02 -08004698 if (!rdma_is_port_valid(&ibdev->ib_dev, ibev.element.port_num)) {
4699 mlx5_ib_warn(ibdev, "warning: event on port %d\n", ibev.element.port_num);
Maor Gottlieb03404e82017-05-30 10:29:13 +03004700 goto out;
Eli Cohena0c84c32013-09-11 16:35:27 +03004701 }
4702
Eli Cohene126ba92013-07-07 17:25:49 +03004703 if (ibdev->ib_active)
4704 ib_dispatch_event(&ibev);
Eli Cohendbaaff22016-10-27 16:36:44 +03004705
4706 if (fatal)
4707 ibdev->ib_active = false;
Maor Gottlieb03404e82017-05-30 10:29:13 +03004708out:
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02004709 kfree(work);
4710}
4711
Saeed Mahameeddf097a22018-11-26 14:39:00 -08004712static int mlx5_ib_event(struct notifier_block *nb,
4713 unsigned long event, void *param)
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02004714{
4715 struct mlx5_ib_event_work *work;
4716
4717 work = kmalloc(sizeof(*work), GFP_ATOMIC);
Leon Romanovsky10bea9c2018-01-19 13:07:11 +02004718 if (!work)
Saeed Mahameeddf097a22018-11-26 14:39:00 -08004719 return NOTIFY_DONE;
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02004720
Leon Romanovsky10bea9c2018-01-19 13:07:11 +02004721 INIT_WORK(&work->work, mlx5_ib_handle_event);
Saeed Mahameeddf097a22018-11-26 14:39:00 -08004722 work->dev = container_of(nb, struct mlx5_ib_dev, mdev_events);
4723 work->is_slave = false;
Leon Romanovsky10bea9c2018-01-19 13:07:11 +02004724 work->param = param;
Leon Romanovsky10bea9c2018-01-19 13:07:11 +02004725 work->event = event;
4726
4727 queue_work(mlx5_ib_event_wq, &work->work);
Saeed Mahameeddf097a22018-11-26 14:39:00 -08004728
4729 return NOTIFY_OK;
4730}
4731
4732static int mlx5_ib_event_slave_port(struct notifier_block *nb,
4733 unsigned long event, void *param)
4734{
4735 struct mlx5_ib_event_work *work;
4736
4737 work = kmalloc(sizeof(*work), GFP_ATOMIC);
4738 if (!work)
4739 return NOTIFY_DONE;
4740
4741 INIT_WORK(&work->work, mlx5_ib_handle_event);
4742 work->mpi = container_of(nb, struct mlx5_ib_multiport_info, mdev_events);
4743 work->is_slave = true;
4744 work->param = param;
4745 work->event = event;
4746 queue_work(mlx5_ib_event_wq, &work->work);
4747
4748 return NOTIFY_OK;
Eli Cohene126ba92013-07-07 17:25:49 +03004749}
4750
Maor Gottliebc43f1112017-01-18 14:10:33 +02004751static int set_has_smi_cap(struct mlx5_ib_dev *dev)
4752{
4753 struct mlx5_hca_vport_context vport_ctx;
4754 int err;
4755 int port;
4756
Mark Blocha989ea02019-03-28 15:27:40 +02004757 for (port = 1; port <= ARRAY_SIZE(dev->mdev->port_caps); port++) {
Maor Gottliebc43f1112017-01-18 14:10:33 +02004758 dev->mdev->port_caps[port - 1].has_smi = false;
4759 if (MLX5_CAP_GEN(dev->mdev, port_type) ==
4760 MLX5_CAP_PORT_TYPE_IB) {
4761 if (MLX5_CAP_GEN(dev->mdev, ib_virt)) {
4762 err = mlx5_query_hca_vport_context(dev->mdev, 0,
4763 port, 0,
4764 &vport_ctx);
4765 if (err) {
4766 mlx5_ib_err(dev, "query_hca_vport_context for port=%d failed %d\n",
4767 port, err);
4768 return err;
4769 }
4770 dev->mdev->port_caps[port - 1].has_smi =
4771 vport_ctx.has_smi;
4772 } else {
4773 dev->mdev->port_caps[port - 1].has_smi = true;
4774 }
4775 }
4776 }
4777 return 0;
4778}
4779
Eli Cohene126ba92013-07-07 17:25:49 +03004780static void get_ext_port_caps(struct mlx5_ib_dev *dev)
4781{
4782 int port;
4783
Daniel Jurgens508562d2018-01-04 17:25:34 +02004784 for (port = 1; port <= dev->num_ports; port++)
Eli Cohene126ba92013-07-07 17:25:49 +03004785 mlx5_query_ext_port_caps(dev, port);
4786}
4787
Mark Bloch26628e22019-03-28 15:27:41 +02004788static int __get_port_caps(struct mlx5_ib_dev *dev, u8 port)
Eli Cohene126ba92013-07-07 17:25:49 +03004789{
4790 struct ib_device_attr *dprops = NULL;
4791 struct ib_port_attr *pprops = NULL;
Dan Carpenterf614fc12015-01-12 11:56:58 +03004792 int err = -ENOMEM;
Matan Barak2528e332015-06-11 16:35:25 +03004793 struct ib_udata uhw = {.inlen = 0, .outlen = 0};
Eli Cohene126ba92013-07-07 17:25:49 +03004794
Leon Romanovsky50ba3c12019-06-30 18:48:32 +03004795 pprops = kzalloc(sizeof(*pprops), GFP_KERNEL);
Eli Cohene126ba92013-07-07 17:25:49 +03004796 if (!pprops)
4797 goto out;
4798
4799 dprops = kmalloc(sizeof(*dprops), GFP_KERNEL);
4800 if (!dprops)
4801 goto out;
4802
Matan Barak2528e332015-06-11 16:35:25 +03004803 err = mlx5_ib_query_device(&dev->ib_dev, dprops, &uhw);
Eli Cohene126ba92013-07-07 17:25:49 +03004804 if (err) {
4805 mlx5_ib_warn(dev, "query_device failed %d\n", err);
4806 goto out;
4807 }
4808
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004809 err = mlx5_ib_query_port(&dev->ib_dev, port, pprops);
4810 if (err) {
4811 mlx5_ib_warn(dev, "query_port %d failed %d\n",
4812 port, err);
4813 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03004814 }
4815
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004816 dev->mdev->port_caps[port - 1].pkey_table_len =
4817 dprops->max_pkeys;
4818 dev->mdev->port_caps[port - 1].gid_table_len =
4819 pprops->gid_tbl_len;
4820 mlx5_ib_dbg(dev, "port %d: pkey_table_len %d, gid_table_len %d\n",
4821 port, dprops->max_pkeys, pprops->gid_tbl_len);
4822
Eli Cohene126ba92013-07-07 17:25:49 +03004823out:
4824 kfree(pprops);
4825 kfree(dprops);
4826
4827 return err;
4828}
4829
Mark Bloch26628e22019-03-28 15:27:41 +02004830static int get_port_caps(struct mlx5_ib_dev *dev, u8 port)
4831{
4832 /* For representors use port 1, is this is the only native
4833 * port
4834 */
4835 if (dev->is_rep)
4836 return __get_port_caps(dev, 1);
4837 return __get_port_caps(dev, port);
4838}
4839
Eli Cohene126ba92013-07-07 17:25:49 +03004840static void destroy_umrc_res(struct mlx5_ib_dev *dev)
4841{
4842 int err;
4843
4844 err = mlx5_mr_cache_cleanup(dev);
4845 if (err)
4846 mlx5_ib_warn(dev, "mr cache cleanup failed\n");
4847
Mark Bloch32927e22018-03-20 15:45:37 +02004848 if (dev->umrc.qp)
Shamir Rabinovitchc4367a22019-03-31 19:10:05 +03004849 mlx5_ib_destroy_qp(dev->umrc.qp, NULL);
Mark Bloch32927e22018-03-20 15:45:37 +02004850 if (dev->umrc.cq)
4851 ib_free_cq(dev->umrc.cq);
4852 if (dev->umrc.pd)
4853 ib_dealloc_pd(dev->umrc.pd);
Eli Cohene126ba92013-07-07 17:25:49 +03004854}
4855
4856enum {
4857 MAX_UMR_WR = 128,
4858};
4859
4860static int create_umr_res(struct mlx5_ib_dev *dev)
4861{
4862 struct ib_qp_init_attr *init_attr = NULL;
4863 struct ib_qp_attr *attr = NULL;
4864 struct ib_pd *pd;
4865 struct ib_cq *cq;
4866 struct ib_qp *qp;
Eli Cohene126ba92013-07-07 17:25:49 +03004867 int ret;
4868
4869 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
4870 init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL);
4871 if (!attr || !init_attr) {
4872 ret = -ENOMEM;
4873 goto error_0;
4874 }
4875
Christoph Hellwiged082d32016-09-05 12:56:17 +02004876 pd = ib_alloc_pd(&dev->ib_dev, 0);
Eli Cohene126ba92013-07-07 17:25:49 +03004877 if (IS_ERR(pd)) {
4878 mlx5_ib_dbg(dev, "Couldn't create PD for sync UMR QP\n");
4879 ret = PTR_ERR(pd);
4880 goto error_0;
4881 }
4882
Christoph Hellwigadd08d72016-03-03 09:38:22 +01004883 cq = ib_alloc_cq(&dev->ib_dev, NULL, 128, 0, IB_POLL_SOFTIRQ);
Eli Cohene126ba92013-07-07 17:25:49 +03004884 if (IS_ERR(cq)) {
4885 mlx5_ib_dbg(dev, "Couldn't create CQ for sync UMR QP\n");
4886 ret = PTR_ERR(cq);
4887 goto error_2;
4888 }
Eli Cohene126ba92013-07-07 17:25:49 +03004889
4890 init_attr->send_cq = cq;
4891 init_attr->recv_cq = cq;
4892 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
4893 init_attr->cap.max_send_wr = MAX_UMR_WR;
4894 init_attr->cap.max_send_sge = 1;
4895 init_attr->qp_type = MLX5_IB_QPT_REG_UMR;
4896 init_attr->port_num = 1;
4897 qp = mlx5_ib_create_qp(pd, init_attr, NULL);
4898 if (IS_ERR(qp)) {
4899 mlx5_ib_dbg(dev, "Couldn't create sync UMR QP\n");
4900 ret = PTR_ERR(qp);
4901 goto error_3;
4902 }
4903 qp->device = &dev->ib_dev;
4904 qp->real_qp = qp;
4905 qp->uobject = NULL;
4906 qp->qp_type = MLX5_IB_QPT_REG_UMR;
Majd Dibbiny31fde032017-10-30 14:23:13 +02004907 qp->send_cq = init_attr->send_cq;
4908 qp->recv_cq = init_attr->recv_cq;
Eli Cohene126ba92013-07-07 17:25:49 +03004909
4910 attr->qp_state = IB_QPS_INIT;
4911 attr->port_num = 1;
4912 ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_PKEY_INDEX |
4913 IB_QP_PORT, NULL);
4914 if (ret) {
4915 mlx5_ib_dbg(dev, "Couldn't modify UMR QP\n");
4916 goto error_4;
4917 }
4918
4919 memset(attr, 0, sizeof(*attr));
4920 attr->qp_state = IB_QPS_RTR;
4921 attr->path_mtu = IB_MTU_256;
4922
4923 ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
4924 if (ret) {
4925 mlx5_ib_dbg(dev, "Couldn't modify umr QP to rtr\n");
4926 goto error_4;
4927 }
4928
4929 memset(attr, 0, sizeof(*attr));
4930 attr->qp_state = IB_QPS_RTS;
4931 ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
4932 if (ret) {
4933 mlx5_ib_dbg(dev, "Couldn't modify umr QP to rts\n");
4934 goto error_4;
4935 }
4936
4937 dev->umrc.qp = qp;
4938 dev->umrc.cq = cq;
Eli Cohene126ba92013-07-07 17:25:49 +03004939 dev->umrc.pd = pd;
4940
4941 sema_init(&dev->umrc.sem, MAX_UMR_WR);
4942 ret = mlx5_mr_cache_init(dev);
4943 if (ret) {
4944 mlx5_ib_warn(dev, "mr cache init failed %d\n", ret);
4945 goto error_4;
4946 }
4947
4948 kfree(attr);
4949 kfree(init_attr);
4950
4951 return 0;
4952
4953error_4:
Shamir Rabinovitchc4367a22019-03-31 19:10:05 +03004954 mlx5_ib_destroy_qp(qp, NULL);
Mark Bloch32927e22018-03-20 15:45:37 +02004955 dev->umrc.qp = NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03004956
4957error_3:
Christoph Hellwigadd08d72016-03-03 09:38:22 +01004958 ib_free_cq(cq);
Mark Bloch32927e22018-03-20 15:45:37 +02004959 dev->umrc.cq = NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03004960
4961error_2:
Eli Cohene126ba92013-07-07 17:25:49 +03004962 ib_dealloc_pd(pd);
Mark Bloch32927e22018-03-20 15:45:37 +02004963 dev->umrc.pd = NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03004964
4965error_0:
4966 kfree(attr);
4967 kfree(init_attr);
4968 return ret;
4969}
4970
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03004971static u8 mlx5_get_umr_fence(u8 umr_fence_cap)
4972{
4973 switch (umr_fence_cap) {
4974 case MLX5_CAP_UMR_FENCE_NONE:
4975 return MLX5_FENCE_MODE_NONE;
4976 case MLX5_CAP_UMR_FENCE_SMALL:
4977 return MLX5_FENCE_MODE_INITIATOR_SMALL;
4978 default:
4979 return MLX5_FENCE_MODE_STRONG_ORDERING;
4980 }
4981}
4982
Eli Cohene126ba92013-07-07 17:25:49 +03004983static int create_dev_resources(struct mlx5_ib_resources *devr)
4984{
4985 struct ib_srq_init_attr attr;
4986 struct mlx5_ib_dev *dev;
Leon Romanovsky21a428a2019-02-03 14:55:51 +02004987 struct ib_device *ibdev;
Matan Barakbcf4c1e2015-06-11 16:35:20 +03004988 struct ib_cq_init_attr cq_attr = {.cqe = 1};
Haggai Eran7722f472016-02-29 15:45:07 +02004989 int port;
Eli Cohene126ba92013-07-07 17:25:49 +03004990 int ret = 0;
4991
4992 dev = container_of(devr, struct mlx5_ib_dev, devr);
Leon Romanovsky21a428a2019-02-03 14:55:51 +02004993 ibdev = &dev->ib_dev;
Eli Cohene126ba92013-07-07 17:25:49 +03004994
Haggai Erand16e91d2016-02-29 15:45:05 +02004995 mutex_init(&devr->mutex);
4996
Leon Romanovsky21a428a2019-02-03 14:55:51 +02004997 devr->p0 = rdma_zalloc_drv_obj(ibdev, ib_pd);
4998 if (!devr->p0)
4999 return -ENOMEM;
5000
5001 devr->p0->device = ibdev;
Eli Cohene126ba92013-07-07 17:25:49 +03005002 devr->p0->uobject = NULL;
5003 atomic_set(&devr->p0->usecnt, 0);
5004
Shamir Rabinovitchff23dfa2019-03-31 19:10:07 +03005005 ret = mlx5_ib_alloc_pd(devr->p0, NULL);
Leon Romanovsky21a428a2019-02-03 14:55:51 +02005006 if (ret)
5007 goto error0;
5008
Leon Romanovskye39afe32019-05-28 14:37:29 +03005009 devr->c0 = rdma_zalloc_drv_obj(ibdev, ib_cq);
5010 if (!devr->c0) {
5011 ret = -ENOMEM;
Eli Cohene126ba92013-07-07 17:25:49 +03005012 goto error1;
5013 }
Leon Romanovskye39afe32019-05-28 14:37:29 +03005014
5015 devr->c0->device = &dev->ib_dev;
Eli Cohene126ba92013-07-07 17:25:49 +03005016 atomic_set(&devr->c0->usecnt, 0);
5017
Leon Romanovskye39afe32019-05-28 14:37:29 +03005018 ret = mlx5_ib_create_cq(devr->c0, &cq_attr, NULL);
5019 if (ret)
5020 goto err_create_cq;
5021
Shamir Rabinovitchff23dfa2019-03-31 19:10:07 +03005022 devr->x0 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03005023 if (IS_ERR(devr->x0)) {
5024 ret = PTR_ERR(devr->x0);
5025 goto error2;
5026 }
5027 devr->x0->device = &dev->ib_dev;
5028 devr->x0->inode = NULL;
5029 atomic_set(&devr->x0->usecnt, 0);
5030 mutex_init(&devr->x0->tgt_qp_mutex);
5031 INIT_LIST_HEAD(&devr->x0->tgt_qp_list);
5032
Shamir Rabinovitchff23dfa2019-03-31 19:10:07 +03005033 devr->x1 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03005034 if (IS_ERR(devr->x1)) {
5035 ret = PTR_ERR(devr->x1);
5036 goto error3;
5037 }
5038 devr->x1->device = &dev->ib_dev;
5039 devr->x1->inode = NULL;
5040 atomic_set(&devr->x1->usecnt, 0);
5041 mutex_init(&devr->x1->tgt_qp_mutex);
5042 INIT_LIST_HEAD(&devr->x1->tgt_qp_list);
5043
5044 memset(&attr, 0, sizeof(attr));
5045 attr.attr.max_sge = 1;
5046 attr.attr.max_wr = 1;
5047 attr.srq_type = IB_SRQT_XRC;
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03005048 attr.ext.cq = devr->c0;
Eli Cohene126ba92013-07-07 17:25:49 +03005049 attr.ext.xrc.xrcd = devr->x0;
5050
Leon Romanovsky68e326d2019-04-03 16:42:43 +03005051 devr->s0 = rdma_zalloc_drv_obj(ibdev, ib_srq);
5052 if (!devr->s0) {
5053 ret = -ENOMEM;
Eli Cohene126ba92013-07-07 17:25:49 +03005054 goto error4;
5055 }
Leon Romanovsky68e326d2019-04-03 16:42:43 +03005056
Eli Cohene126ba92013-07-07 17:25:49 +03005057 devr->s0->device = &dev->ib_dev;
5058 devr->s0->pd = devr->p0;
Eli Cohene126ba92013-07-07 17:25:49 +03005059 devr->s0->srq_type = IB_SRQT_XRC;
5060 devr->s0->ext.xrc.xrcd = devr->x0;
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03005061 devr->s0->ext.cq = devr->c0;
Leon Romanovsky68e326d2019-04-03 16:42:43 +03005062 ret = mlx5_ib_create_srq(devr->s0, &attr, NULL);
5063 if (ret)
5064 goto err_create;
5065
Eli Cohene126ba92013-07-07 17:25:49 +03005066 atomic_inc(&devr->s0->ext.xrc.xrcd->usecnt);
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03005067 atomic_inc(&devr->s0->ext.cq->usecnt);
Eli Cohene126ba92013-07-07 17:25:49 +03005068 atomic_inc(&devr->p0->usecnt);
5069 atomic_set(&devr->s0->usecnt, 0);
5070
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03005071 memset(&attr, 0, sizeof(attr));
5072 attr.attr.max_sge = 1;
5073 attr.attr.max_wr = 1;
5074 attr.srq_type = IB_SRQT_BASIC;
Leon Romanovsky68e326d2019-04-03 16:42:43 +03005075 devr->s1 = rdma_zalloc_drv_obj(ibdev, ib_srq);
5076 if (!devr->s1) {
5077 ret = -ENOMEM;
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03005078 goto error5;
5079 }
Leon Romanovsky68e326d2019-04-03 16:42:43 +03005080
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03005081 devr->s1->device = &dev->ib_dev;
5082 devr->s1->pd = devr->p0;
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03005083 devr->s1->srq_type = IB_SRQT_BASIC;
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03005084 devr->s1->ext.cq = devr->c0;
Leon Romanovsky68e326d2019-04-03 16:42:43 +03005085
5086 ret = mlx5_ib_create_srq(devr->s1, &attr, NULL);
5087 if (ret)
5088 goto error6;
5089
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03005090 atomic_inc(&devr->p0->usecnt);
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03005091 atomic_set(&devr->s1->usecnt, 0);
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03005092
Haggai Eran7722f472016-02-29 15:45:07 +02005093 for (port = 0; port < ARRAY_SIZE(devr->ports); ++port) {
5094 INIT_WORK(&devr->ports[port].pkey_change_work,
5095 pkey_change_handler);
5096 devr->ports[port].devr = devr;
5097 }
5098
Eli Cohene126ba92013-07-07 17:25:49 +03005099 return 0;
5100
Leon Romanovsky68e326d2019-04-03 16:42:43 +03005101error6:
5102 kfree(devr->s1);
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03005103error5:
Shamir Rabinovitchc4367a22019-03-31 19:10:05 +03005104 mlx5_ib_destroy_srq(devr->s0, NULL);
Leon Romanovsky68e326d2019-04-03 16:42:43 +03005105err_create:
5106 kfree(devr->s0);
Eli Cohene126ba92013-07-07 17:25:49 +03005107error4:
Shamir Rabinovitchc4367a22019-03-31 19:10:05 +03005108 mlx5_ib_dealloc_xrcd(devr->x1, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03005109error3:
Shamir Rabinovitchc4367a22019-03-31 19:10:05 +03005110 mlx5_ib_dealloc_xrcd(devr->x0, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03005111error2:
Shamir Rabinovitchc4367a22019-03-31 19:10:05 +03005112 mlx5_ib_destroy_cq(devr->c0, NULL);
Leon Romanovskye39afe32019-05-28 14:37:29 +03005113err_create_cq:
5114 kfree(devr->c0);
Eli Cohene126ba92013-07-07 17:25:49 +03005115error1:
Shamir Rabinovitchc4367a22019-03-31 19:10:05 +03005116 mlx5_ib_dealloc_pd(devr->p0, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03005117error0:
Leon Romanovsky21a428a2019-02-03 14:55:51 +02005118 kfree(devr->p0);
Eli Cohene126ba92013-07-07 17:25:49 +03005119 return ret;
5120}
5121
5122static void destroy_dev_resources(struct mlx5_ib_resources *devr)
5123{
Haggai Eran7722f472016-02-29 15:45:07 +02005124 int port;
5125
Shamir Rabinovitchc4367a22019-03-31 19:10:05 +03005126 mlx5_ib_destroy_srq(devr->s1, NULL);
Leon Romanovsky68e326d2019-04-03 16:42:43 +03005127 kfree(devr->s1);
Shamir Rabinovitchc4367a22019-03-31 19:10:05 +03005128 mlx5_ib_destroy_srq(devr->s0, NULL);
Leon Romanovsky68e326d2019-04-03 16:42:43 +03005129 kfree(devr->s0);
Shamir Rabinovitchc4367a22019-03-31 19:10:05 +03005130 mlx5_ib_dealloc_xrcd(devr->x0, NULL);
5131 mlx5_ib_dealloc_xrcd(devr->x1, NULL);
5132 mlx5_ib_destroy_cq(devr->c0, NULL);
Leon Romanovskye39afe32019-05-28 14:37:29 +03005133 kfree(devr->c0);
Shamir Rabinovitchc4367a22019-03-31 19:10:05 +03005134 mlx5_ib_dealloc_pd(devr->p0, NULL);
Leon Romanovsky21a428a2019-02-03 14:55:51 +02005135 kfree(devr->p0);
Haggai Eran7722f472016-02-29 15:45:07 +02005136
5137 /* Make sure no change P_Key work items are still executing */
Mark Bloch5d8f6a02019-03-28 15:27:36 +02005138 for (port = 0; port < ARRAY_SIZE(devr->ports); ++port)
Haggai Eran7722f472016-02-29 15:45:07 +02005139 cancel_work_sync(&devr->ports[port].pkey_change_work);
Eli Cohene126ba92013-07-07 17:25:49 +03005140}
5141
Artemy Kovalyovb02289b2018-07-04 15:57:50 +03005142static u32 get_core_cap_flags(struct ib_device *ibdev,
5143 struct mlx5_hca_vport_context *rep)
Achiad Shochate53505a2015-12-23 18:47:25 +02005144{
5145 struct mlx5_ib_dev *dev = to_mdev(ibdev);
5146 enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, 1);
5147 u8 l3_type_cap = MLX5_CAP_ROCE(dev->mdev, l3_type);
5148 u8 roce_version_cap = MLX5_CAP_ROCE(dev->mdev, roce_version);
Daniel Jurgens85c7c012018-01-04 17:25:43 +02005149 bool raw_support = !mlx5_core_mp_enabled(dev->mdev);
Achiad Shochate53505a2015-12-23 18:47:25 +02005150 u32 ret = 0;
5151
Artemy Kovalyovb02289b2018-07-04 15:57:50 +03005152 if (rep->grh_required)
5153 ret |= RDMA_CORE_CAP_IB_GRH_REQUIRED;
5154
Achiad Shochate53505a2015-12-23 18:47:25 +02005155 if (ll == IB_LINK_LAYER_INFINIBAND)
Artemy Kovalyovb02289b2018-07-04 15:57:50 +03005156 return ret | RDMA_CORE_PORT_IBA_IB;
Achiad Shochate53505a2015-12-23 18:47:25 +02005157
Daniel Jurgens85c7c012018-01-04 17:25:43 +02005158 if (raw_support)
Artemy Kovalyovb02289b2018-07-04 15:57:50 +03005159 ret |= RDMA_CORE_PORT_RAW_PACKET;
Or Gerlitz72cd5712017-01-24 13:02:36 +02005160
Achiad Shochate53505a2015-12-23 18:47:25 +02005161 if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV4_CAP))
Or Gerlitz72cd5712017-01-24 13:02:36 +02005162 return ret;
Achiad Shochate53505a2015-12-23 18:47:25 +02005163
5164 if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV6_CAP))
Or Gerlitz72cd5712017-01-24 13:02:36 +02005165 return ret;
Achiad Shochate53505a2015-12-23 18:47:25 +02005166
5167 if (roce_version_cap & MLX5_ROCE_VERSION_1_CAP)
5168 ret |= RDMA_CORE_PORT_IBA_ROCE;
5169
5170 if (roce_version_cap & MLX5_ROCE_VERSION_2_CAP)
5171 ret |= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
5172
5173 return ret;
5174}
5175
Ira Weiny77386132015-05-13 20:02:58 -04005176static int mlx5_port_immutable(struct ib_device *ibdev, u8 port_num,
5177 struct ib_port_immutable *immutable)
5178{
5179 struct ib_port_attr attr;
Or Gerlitzca5b91d2016-11-27 16:51:36 +02005180 struct mlx5_ib_dev *dev = to_mdev(ibdev);
5181 enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, port_num);
Artemy Kovalyovb02289b2018-07-04 15:57:50 +03005182 struct mlx5_hca_vport_context rep = {0};
Ira Weiny77386132015-05-13 20:02:58 -04005183 int err;
5184
Or Gerlitzc4550c62017-01-24 13:02:39 +02005185 err = ib_query_port(ibdev, port_num, &attr);
Ira Weiny77386132015-05-13 20:02:58 -04005186 if (err)
5187 return err;
5188
Artemy Kovalyovb02289b2018-07-04 15:57:50 +03005189 if (ll == IB_LINK_LAYER_INFINIBAND) {
5190 err = mlx5_query_hca_vport_context(dev->mdev, 0, port_num, 0,
5191 &rep);
5192 if (err)
5193 return err;
5194 }
5195
Ira Weiny77386132015-05-13 20:02:58 -04005196 immutable->pkey_tbl_len = attr.pkey_tbl_len;
5197 immutable->gid_tbl_len = attr.gid_tbl_len;
Artemy Kovalyovb02289b2018-07-04 15:57:50 +03005198 immutable->core_cap_flags = get_core_cap_flags(ibdev, &rep);
Michael Guralnik94de8792019-11-08 23:45:28 +00005199 immutable->max_mad_size = IB_MGMT_MAD_SIZE;
Ira Weiny77386132015-05-13 20:02:58 -04005200
5201 return 0;
5202}
5203
Mark Bloch8e6efa32017-11-06 12:22:13 +00005204static int mlx5_port_rep_immutable(struct ib_device *ibdev, u8 port_num,
5205 struct ib_port_immutable *immutable)
5206{
5207 struct ib_port_attr attr;
5208 int err;
5209
5210 immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET;
5211
5212 err = ib_query_port(ibdev, port_num, &attr);
5213 if (err)
5214 return err;
5215
5216 immutable->pkey_tbl_len = attr.pkey_tbl_len;
5217 immutable->gid_tbl_len = attr.gid_tbl_len;
5218 immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET;
5219
5220 return 0;
5221}
5222
Leon Romanovsky9abb0d12017-06-27 16:49:53 +03005223static void get_dev_fw_str(struct ib_device *ibdev, char *str)
Ira Weinyc7342822016-06-15 02:22:01 -04005224{
5225 struct mlx5_ib_dev *dev =
5226 container_of(ibdev, struct mlx5_ib_dev, ib_dev);
Leon Romanovsky9abb0d12017-06-27 16:49:53 +03005227 snprintf(str, IB_FW_VERSION_NAME_MAX, "%d.%d.%04d",
5228 fw_rev_maj(dev->mdev), fw_rev_min(dev->mdev),
5229 fw_rev_sub(dev->mdev));
Ira Weinyc7342822016-06-15 02:22:01 -04005230}
5231
Or Gerlitz45f95ac2016-11-27 16:51:35 +02005232static int mlx5_eth_lag_init(struct mlx5_ib_dev *dev)
Aviv Heller9ef9c642016-09-18 20:48:01 +03005233{
5234 struct mlx5_core_dev *mdev = dev->mdev;
5235 struct mlx5_flow_namespace *ns = mlx5_get_flow_namespace(mdev,
5236 MLX5_FLOW_NAMESPACE_LAG);
5237 struct mlx5_flow_table *ft;
5238 int err;
5239
Aviv Heller7c34ec12018-08-23 13:47:53 +03005240 if (!ns || !mlx5_lag_is_roce(mdev))
Aviv Heller9ef9c642016-09-18 20:48:01 +03005241 return 0;
5242
5243 err = mlx5_cmd_create_vport_lag(mdev);
5244 if (err)
5245 return err;
5246
5247 ft = mlx5_create_lag_demux_flow_table(ns, 0, 0);
5248 if (IS_ERR(ft)) {
5249 err = PTR_ERR(ft);
5250 goto err_destroy_vport_lag;
5251 }
5252
Mark Bloch9a4ca382018-01-16 14:42:35 +00005253 dev->flow_db->lag_demux_ft = ft;
Aviv Heller7c34ec12018-08-23 13:47:53 +03005254 dev->lag_active = true;
Aviv Heller9ef9c642016-09-18 20:48:01 +03005255 return 0;
5256
5257err_destroy_vport_lag:
5258 mlx5_cmd_destroy_vport_lag(mdev);
5259 return err;
5260}
5261
Or Gerlitz45f95ac2016-11-27 16:51:35 +02005262static void mlx5_eth_lag_cleanup(struct mlx5_ib_dev *dev)
Aviv Heller9ef9c642016-09-18 20:48:01 +03005263{
5264 struct mlx5_core_dev *mdev = dev->mdev;
5265
Aviv Heller7c34ec12018-08-23 13:47:53 +03005266 if (dev->lag_active) {
5267 dev->lag_active = false;
5268
Mark Bloch9a4ca382018-01-16 14:42:35 +00005269 mlx5_destroy_flow_table(dev->flow_db->lag_demux_ft);
5270 dev->flow_db->lag_demux_ft = NULL;
Aviv Heller9ef9c642016-09-18 20:48:01 +03005271
5272 mlx5_cmd_destroy_vport_lag(mdev);
5273 }
5274}
5275
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +02005276static int mlx5_add_netdev_notifier(struct mlx5_ib_dev *dev, u8 port_num)
Achiad Shochatfc24fc52015-12-23 18:47:17 +02005277{
Achiad Shochate53505a2015-12-23 18:47:25 +02005278 int err;
5279
Mark Bloch95579e72019-03-28 15:27:33 +02005280 dev->port[port_num].roce.nb.notifier_call = mlx5_netdev_event;
5281 err = register_netdevice_notifier(&dev->port[port_num].roce.nb);
Aviv Heller5ec8c832016-09-18 20:48:00 +03005282 if (err) {
Mark Bloch95579e72019-03-28 15:27:33 +02005283 dev->port[port_num].roce.nb.notifier_call = NULL;
Achiad Shochate53505a2015-12-23 18:47:25 +02005284 return err;
Aviv Heller5ec8c832016-09-18 20:48:00 +03005285 }
Achiad Shochate53505a2015-12-23 18:47:25 +02005286
Or Gerlitzd012f5d2016-11-27 16:51:34 +02005287 return 0;
5288}
Achiad Shochate53505a2015-12-23 18:47:25 +02005289
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +02005290static void mlx5_remove_netdev_notifier(struct mlx5_ib_dev *dev, u8 port_num)
Eli Cohene126ba92013-07-07 17:25:49 +03005291{
Mark Bloch95579e72019-03-28 15:27:33 +02005292 if (dev->port[port_num].roce.nb.notifier_call) {
5293 unregister_netdevice_notifier(&dev->port[port_num].roce.nb);
5294 dev->port[port_num].roce.nb.notifier_call = NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03005295 }
5296}
5297
Leon Romanovskye3f1ed12018-07-08 12:55:43 +03005298static int mlx5_enable_eth(struct mlx5_ib_dev *dev)
Eli Cohene126ba92013-07-07 17:25:49 +03005299{
Eli Cohene126ba92013-07-07 17:25:49 +03005300 int err;
5301
Michael Guralnik94de8792019-11-08 23:45:28 +00005302 err = mlx5_nic_vport_enable_roce(dev->mdev);
5303 if (err)
5304 return err;
Achiad Shochate53505a2015-12-23 18:47:25 +02005305
Or Gerlitz45f95ac2016-11-27 16:51:35 +02005306 err = mlx5_eth_lag_init(dev);
Aviv Heller9ef9c642016-09-18 20:48:01 +03005307 if (err)
5308 goto err_disable_roce;
5309
Achiad Shochate53505a2015-12-23 18:47:25 +02005310 return 0;
5311
Aviv Heller9ef9c642016-09-18 20:48:01 +03005312err_disable_roce:
Michael Guralnik94de8792019-11-08 23:45:28 +00005313 mlx5_nic_vport_disable_roce(dev->mdev);
Aviv Heller9ef9c642016-09-18 20:48:01 +03005314
Achiad Shochate53505a2015-12-23 18:47:25 +02005315 return err;
Achiad Shochatfc24fc52015-12-23 18:47:17 +02005316}
5317
Or Gerlitz45f95ac2016-11-27 16:51:35 +02005318static void mlx5_disable_eth(struct mlx5_ib_dev *dev)
Achiad Shochatfc24fc52015-12-23 18:47:17 +02005319{
Or Gerlitz45f95ac2016-11-27 16:51:35 +02005320 mlx5_eth_lag_cleanup(dev);
Michael Guralnik94de8792019-11-08 23:45:28 +00005321 mlx5_nic_vport_disable_roce(dev->mdev);
Achiad Shochatfc24fc52015-12-23 18:47:17 +02005322}
5323
Parav Pandite1f24a72017-04-16 07:29:29 +03005324struct mlx5_ib_counter {
Kamal Heib7c16f472017-01-18 15:25:09 +02005325 const char *name;
5326 size_t offset;
5327};
5328
5329#define INIT_Q_COUNTER(_name) \
5330 { .name = #_name, .offset = MLX5_BYTE_OFF(query_q_counter_out, _name)}
5331
Parav Pandite1f24a72017-04-16 07:29:29 +03005332static const struct mlx5_ib_counter basic_q_cnts[] = {
Kamal Heib7c16f472017-01-18 15:25:09 +02005333 INIT_Q_COUNTER(rx_write_requests),
5334 INIT_Q_COUNTER(rx_read_requests),
5335 INIT_Q_COUNTER(rx_atomic_requests),
5336 INIT_Q_COUNTER(out_of_buffer),
5337};
5338
Parav Pandite1f24a72017-04-16 07:29:29 +03005339static const struct mlx5_ib_counter out_of_seq_q_cnts[] = {
Kamal Heib7c16f472017-01-18 15:25:09 +02005340 INIT_Q_COUNTER(out_of_sequence),
5341};
5342
Parav Pandite1f24a72017-04-16 07:29:29 +03005343static const struct mlx5_ib_counter retrans_q_cnts[] = {
Kamal Heib7c16f472017-01-18 15:25:09 +02005344 INIT_Q_COUNTER(duplicate_request),
5345 INIT_Q_COUNTER(rnr_nak_retry_err),
5346 INIT_Q_COUNTER(packet_seq_err),
5347 INIT_Q_COUNTER(implied_nak_seq_err),
5348 INIT_Q_COUNTER(local_ack_timeout_err),
5349};
5350
Parav Pandite1f24a72017-04-16 07:29:29 +03005351#define INIT_CONG_COUNTER(_name) \
5352 { .name = #_name, .offset = \
5353 MLX5_BYTE_OFF(query_cong_statistics_out, _name ## _high)}
5354
5355static const struct mlx5_ib_counter cong_cnts[] = {
5356 INIT_CONG_COUNTER(rp_cnp_ignored),
5357 INIT_CONG_COUNTER(rp_cnp_handled),
5358 INIT_CONG_COUNTER(np_ecn_marked_roce_packets),
5359 INIT_CONG_COUNTER(np_cnp_sent),
5360};
5361
Parav Pandit58dcb602017-06-19 07:19:37 +03005362static const struct mlx5_ib_counter extended_err_cnts[] = {
5363 INIT_Q_COUNTER(resp_local_length_error),
5364 INIT_Q_COUNTER(resp_cqe_error),
5365 INIT_Q_COUNTER(req_cqe_error),
5366 INIT_Q_COUNTER(req_remote_invalid_request),
5367 INIT_Q_COUNTER(req_remote_access_errors),
5368 INIT_Q_COUNTER(resp_remote_access_errors),
5369 INIT_Q_COUNTER(resp_cqe_flush_error),
5370 INIT_Q_COUNTER(req_cqe_flush_error),
5371};
5372
Talat Batheesh9f876f32018-06-21 15:37:56 +03005373#define INIT_EXT_PPCNT_COUNTER(_name) \
5374 { .name = #_name, .offset = \
5375 MLX5_BYTE_OFF(ppcnt_reg, \
5376 counter_set.eth_extended_cntrs_grp_data_layout._name##_high)}
5377
5378static const struct mlx5_ib_counter ext_ppcnt_cnts[] = {
5379 INIT_EXT_PPCNT_COUNTER(rx_icrc_encapsulated),
5380};
5381
Parav Pandit3e1f0002019-07-23 10:31:17 +03005382static bool is_mdev_switchdev_mode(const struct mlx5_core_dev *mdev)
5383{
5384 return MLX5_ESWITCH_MANAGER(mdev) &&
5385 mlx5_ib_eswitch_mode(mdev->priv.eswitch) ==
5386 MLX5_ESWITCH_OFFLOADS;
5387}
5388
Parav Pandite1f24a72017-04-16 07:29:29 +03005389static void mlx5_ib_dealloc_counters(struct mlx5_ib_dev *dev)
Mark Bloch0837e862016-06-17 15:10:55 +03005390{
Parav Pandit3e1f0002019-07-23 10:31:17 +03005391 int num_cnt_ports;
Daniel Jurgensaac44922018-01-04 17:25:40 +02005392 int i;
Mark Bloch0837e862016-06-17 15:10:55 +03005393
Parav Pandit3e1f0002019-07-23 10:31:17 +03005394 num_cnt_ports = is_mdev_switchdev_mode(dev->mdev) ? 1 : dev->num_ports;
5395
5396 for (i = 0; i < num_cnt_ports; i++) {
Parav Pandit921c0f52018-07-08 13:40:30 +03005397 if (dev->port[i].cnts.set_id_valid)
Daniel Jurgensaac44922018-01-04 17:25:40 +02005398 mlx5_core_dealloc_q_counter(dev->mdev,
5399 dev->port[i].cnts.set_id);
Parav Pandite1f24a72017-04-16 07:29:29 +03005400 kfree(dev->port[i].cnts.names);
5401 kfree(dev->port[i].cnts.offsets);
Kamal Heib7c16f472017-01-18 15:25:09 +02005402 }
5403}
5404
Parav Pandite1f24a72017-04-16 07:29:29 +03005405static int __mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev,
5406 struct mlx5_ib_counters *cnts)
Kamal Heib7c16f472017-01-18 15:25:09 +02005407{
5408 u32 num_counters;
5409
5410 num_counters = ARRAY_SIZE(basic_q_cnts);
5411
5412 if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt))
5413 num_counters += ARRAY_SIZE(out_of_seq_q_cnts);
5414
5415 if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters))
5416 num_counters += ARRAY_SIZE(retrans_q_cnts);
Parav Pandit58dcb602017-06-19 07:19:37 +03005417
5418 if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters))
5419 num_counters += ARRAY_SIZE(extended_err_cnts);
5420
Parav Pandite1f24a72017-04-16 07:29:29 +03005421 cnts->num_q_counters = num_counters;
Kamal Heib7c16f472017-01-18 15:25:09 +02005422
Parav Pandite1f24a72017-04-16 07:29:29 +03005423 if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
5424 cnts->num_cong_counters = ARRAY_SIZE(cong_cnts);
5425 num_counters += ARRAY_SIZE(cong_cnts);
5426 }
Talat Batheesh9f876f32018-06-21 15:37:56 +03005427 if (MLX5_CAP_PCAM_FEATURE(dev->mdev, rx_icrc_encapsulated_counter)) {
5428 cnts->num_ext_ppcnt_counters = ARRAY_SIZE(ext_ppcnt_cnts);
5429 num_counters += ARRAY_SIZE(ext_ppcnt_cnts);
5430 }
Parav Pandite1f24a72017-04-16 07:29:29 +03005431 cnts->names = kcalloc(num_counters, sizeof(cnts->names), GFP_KERNEL);
5432 if (!cnts->names)
Kamal Heib7c16f472017-01-18 15:25:09 +02005433 return -ENOMEM;
5434
Parav Pandite1f24a72017-04-16 07:29:29 +03005435 cnts->offsets = kcalloc(num_counters,
5436 sizeof(cnts->offsets), GFP_KERNEL);
5437 if (!cnts->offsets)
Kamal Heib7c16f472017-01-18 15:25:09 +02005438 goto err_names;
5439
Kamal Heib7c16f472017-01-18 15:25:09 +02005440 return 0;
5441
5442err_names:
Parav Pandite1f24a72017-04-16 07:29:29 +03005443 kfree(cnts->names);
Daniel Jurgensaac44922018-01-04 17:25:40 +02005444 cnts->names = NULL;
Kamal Heib7c16f472017-01-18 15:25:09 +02005445 return -ENOMEM;
5446}
5447
Parav Pandite1f24a72017-04-16 07:29:29 +03005448static void mlx5_ib_fill_counters(struct mlx5_ib_dev *dev,
5449 const char **names,
5450 size_t *offsets)
Kamal Heib7c16f472017-01-18 15:25:09 +02005451{
5452 int i;
5453 int j = 0;
5454
5455 for (i = 0; i < ARRAY_SIZE(basic_q_cnts); i++, j++) {
5456 names[j] = basic_q_cnts[i].name;
5457 offsets[j] = basic_q_cnts[i].offset;
5458 }
5459
5460 if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt)) {
5461 for (i = 0; i < ARRAY_SIZE(out_of_seq_q_cnts); i++, j++) {
5462 names[j] = out_of_seq_q_cnts[i].name;
5463 offsets[j] = out_of_seq_q_cnts[i].offset;
5464 }
5465 }
5466
5467 if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters)) {
5468 for (i = 0; i < ARRAY_SIZE(retrans_q_cnts); i++, j++) {
5469 names[j] = retrans_q_cnts[i].name;
5470 offsets[j] = retrans_q_cnts[i].offset;
5471 }
5472 }
Parav Pandite1f24a72017-04-16 07:29:29 +03005473
Parav Pandit58dcb602017-06-19 07:19:37 +03005474 if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters)) {
5475 for (i = 0; i < ARRAY_SIZE(extended_err_cnts); i++, j++) {
5476 names[j] = extended_err_cnts[i].name;
5477 offsets[j] = extended_err_cnts[i].offset;
5478 }
5479 }
5480
Parav Pandite1f24a72017-04-16 07:29:29 +03005481 if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
5482 for (i = 0; i < ARRAY_SIZE(cong_cnts); i++, j++) {
5483 names[j] = cong_cnts[i].name;
5484 offsets[j] = cong_cnts[i].offset;
5485 }
5486 }
Talat Batheesh9f876f32018-06-21 15:37:56 +03005487
5488 if (MLX5_CAP_PCAM_FEATURE(dev->mdev, rx_icrc_encapsulated_counter)) {
5489 for (i = 0; i < ARRAY_SIZE(ext_ppcnt_cnts); i++, j++) {
5490 names[j] = ext_ppcnt_cnts[i].name;
5491 offsets[j] = ext_ppcnt_cnts[i].offset;
5492 }
5493 }
Mark Bloch0837e862016-06-17 15:10:55 +03005494}
5495
Parav Pandite1f24a72017-04-16 07:29:29 +03005496static int mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev)
Mark Bloch0837e862016-06-17 15:10:55 +03005497{
Parav Pandit3e1f0002019-07-23 10:31:17 +03005498 int num_cnt_ports;
Daniel Jurgensaac44922018-01-04 17:25:40 +02005499 int err = 0;
Mark Bloch0837e862016-06-17 15:10:55 +03005500 int i;
Yishai Hadasaa74be62018-12-09 12:52:36 +02005501 bool is_shared;
5502
5503 is_shared = MLX5_CAP_GEN(dev->mdev, log_max_uctx) != 0;
Parav Pandit3e1f0002019-07-23 10:31:17 +03005504 num_cnt_ports = is_mdev_switchdev_mode(dev->mdev) ? 1 : dev->num_ports;
Mark Bloch0837e862016-06-17 15:10:55 +03005505
Parav Pandit3e1f0002019-07-23 10:31:17 +03005506 for (i = 0; i < num_cnt_ports; i++) {
Daniel Jurgensaac44922018-01-04 17:25:40 +02005507 err = __mlx5_ib_alloc_counters(dev, &dev->port[i].cnts);
5508 if (err)
5509 goto err_alloc;
Kamal Heib7c16f472017-01-18 15:25:09 +02005510
Daniel Jurgensaac44922018-01-04 17:25:40 +02005511 mlx5_ib_fill_counters(dev, dev->port[i].cnts.names,
5512 dev->port[i].cnts.offsets);
5513
Yishai Hadasaa74be62018-12-09 12:52:36 +02005514 err = mlx5_cmd_alloc_q_counter(dev->mdev,
5515 &dev->port[i].cnts.set_id,
5516 is_shared ?
5517 MLX5_SHARED_RESOURCE_UID : 0);
Daniel Jurgensaac44922018-01-04 17:25:40 +02005518 if (err) {
Mark Bloch0837e862016-06-17 15:10:55 +03005519 mlx5_ib_warn(dev,
5520 "couldn't allocate queue counter for port %d, err %d\n",
Daniel Jurgensaac44922018-01-04 17:25:40 +02005521 i + 1, err);
5522 goto err_alloc;
Mark Bloch0837e862016-06-17 15:10:55 +03005523 }
Daniel Jurgensaac44922018-01-04 17:25:40 +02005524 dev->port[i].cnts.set_id_valid = true;
Mark Bloch0837e862016-06-17 15:10:55 +03005525 }
Mark Bloch0837e862016-06-17 15:10:55 +03005526 return 0;
5527
Daniel Jurgensaac44922018-01-04 17:25:40 +02005528err_alloc:
5529 mlx5_ib_dealloc_counters(dev);
5530 return err;
Mark Bloch0837e862016-06-17 15:10:55 +03005531}
5532
Parav Pandit3e1f0002019-07-23 10:31:17 +03005533static const struct mlx5_ib_counters *get_counters(struct mlx5_ib_dev *dev,
5534 u8 port_num)
5535{
5536 return is_mdev_switchdev_mode(dev->mdev) ? &dev->port[0].cnts :
5537 &dev->port[port_num].cnts;
5538}
5539
5540/**
5541 * mlx5_ib_get_counters_id - Returns counters id to use for device+port
5542 * @dev: Pointer to mlx5 IB device
5543 * @port_num: Zero based port number
5544 *
5545 * mlx5_ib_get_counters_id() Returns counters set id to use for given
5546 * device port combination in switchdev and non switchdev mode of the
5547 * parent device.
5548 */
5549u16 mlx5_ib_get_counters_id(struct mlx5_ib_dev *dev, u8 port_num)
5550{
5551 const struct mlx5_ib_counters *cnts = get_counters(dev, port_num);
5552
5553 return cnts->set_id;
5554}
5555
Mark Bloch0ad17a82016-06-17 15:10:56 +03005556static struct rdma_hw_stats *mlx5_ib_alloc_hw_stats(struct ib_device *ibdev,
5557 u8 port_num)
5558{
Kamal Heib7c16f472017-01-18 15:25:09 +02005559 struct mlx5_ib_dev *dev = to_mdev(ibdev);
Parav Pandit3e1f0002019-07-23 10:31:17 +03005560 const struct mlx5_ib_counters *cnts;
5561 bool is_switchdev = is_mdev_switchdev_mode(dev->mdev);
Mark Bloch0ad17a82016-06-17 15:10:56 +03005562
Parav Pandit3e1f0002019-07-23 10:31:17 +03005563 if ((is_switchdev && port_num) || (!is_switchdev && !port_num))
Mark Bloch0ad17a82016-06-17 15:10:56 +03005564 return NULL;
5565
Parav Pandit3e1f0002019-07-23 10:31:17 +03005566 cnts = get_counters(dev, port_num - 1);
5567
Parav Pandit5dcecbc2019-07-23 10:31:16 +03005568 return rdma_alloc_hw_stats_struct(cnts->names,
5569 cnts->num_q_counters +
5570 cnts->num_cong_counters +
5571 cnts->num_ext_ppcnt_counters,
Mark Bloch0ad17a82016-06-17 15:10:56 +03005572 RDMA_HW_STATS_DEFAULT_LIFESPAN);
5573}
5574
Daniel Jurgensaac44922018-01-04 17:25:40 +02005575static int mlx5_ib_query_q_counters(struct mlx5_core_dev *mdev,
Parav Pandit5dcecbc2019-07-23 10:31:16 +03005576 const struct mlx5_ib_counters *cnts,
Mark Zhang318d5352019-07-02 13:02:37 +03005577 struct rdma_hw_stats *stats,
5578 u16 set_id)
Parav Pandite1f24a72017-04-16 07:29:29 +03005579{
5580 int outlen = MLX5_ST_SZ_BYTES(query_q_counter_out);
5581 void *out;
5582 __be32 val;
5583 int ret, i;
5584
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03005585 out = kvzalloc(outlen, GFP_KERNEL);
Parav Pandite1f24a72017-04-16 07:29:29 +03005586 if (!out)
5587 return -ENOMEM;
5588
Mark Zhang318d5352019-07-02 13:02:37 +03005589 ret = mlx5_core_query_q_counter(mdev, set_id, 0, out, outlen);
Parav Pandite1f24a72017-04-16 07:29:29 +03005590 if (ret)
5591 goto free;
5592
Parav Pandit5dcecbc2019-07-23 10:31:16 +03005593 for (i = 0; i < cnts->num_q_counters; i++) {
5594 val = *(__be32 *)(out + cnts->offsets[i]);
Parav Pandite1f24a72017-04-16 07:29:29 +03005595 stats->value[i] = (u64)be32_to_cpu(val);
5596 }
5597
5598free:
5599 kvfree(out);
5600 return ret;
5601}
5602
Talat Batheesh9f876f32018-06-21 15:37:56 +03005603static int mlx5_ib_query_ext_ppcnt_counters(struct mlx5_ib_dev *dev,
Parav Pandit5dcecbc2019-07-23 10:31:16 +03005604 const struct mlx5_ib_counters *cnts,
5605 struct rdma_hw_stats *stats)
Talat Batheesh9f876f32018-06-21 15:37:56 +03005606{
Parav Pandit5dcecbc2019-07-23 10:31:16 +03005607 int offset = cnts->num_q_counters + cnts->num_cong_counters;
Talat Batheesh9f876f32018-06-21 15:37:56 +03005608 int sz = MLX5_ST_SZ_BYTES(ppcnt_reg);
5609 int ret, i;
5610 void *out;
5611
5612 out = kvzalloc(sz, GFP_KERNEL);
5613 if (!out)
5614 return -ENOMEM;
5615
5616 ret = mlx5_cmd_query_ext_ppcnt_counters(dev->mdev, out);
5617 if (ret)
5618 goto free;
5619
Parav Pandit5dcecbc2019-07-23 10:31:16 +03005620 for (i = 0; i < cnts->num_ext_ppcnt_counters; i++)
Talat Batheesh9f876f32018-06-21 15:37:56 +03005621 stats->value[i + offset] =
5622 be64_to_cpup((__be64 *)(out +
Parav Pandit5dcecbc2019-07-23 10:31:16 +03005623 cnts->offsets[i + offset]));
Talat Batheesh9f876f32018-06-21 15:37:56 +03005624free:
5625 kvfree(out);
5626 return ret;
5627}
5628
Mark Bloch0ad17a82016-06-17 15:10:56 +03005629static int mlx5_ib_get_hw_stats(struct ib_device *ibdev,
5630 struct rdma_hw_stats *stats,
Kamal Heib7c16f472017-01-18 15:25:09 +02005631 u8 port_num, int index)
Mark Bloch0ad17a82016-06-17 15:10:56 +03005632{
5633 struct mlx5_ib_dev *dev = to_mdev(ibdev);
Parav Pandit3e1f0002019-07-23 10:31:17 +03005634 const struct mlx5_ib_counters *cnts = get_counters(dev, port_num - 1);
Daniel Jurgensaac44922018-01-04 17:25:40 +02005635 struct mlx5_core_dev *mdev;
Parav Pandite1f24a72017-04-16 07:29:29 +03005636 int ret, num_counters;
Daniel Jurgensaac44922018-01-04 17:25:40 +02005637 u8 mdev_port_num;
Mark Bloch0ad17a82016-06-17 15:10:56 +03005638
Kamal Heib7c16f472017-01-18 15:25:09 +02005639 if (!stats)
Parav Pandite1f24a72017-04-16 07:29:29 +03005640 return -EINVAL;
Mark Bloch0ad17a82016-06-17 15:10:56 +03005641
Parav Pandit5dcecbc2019-07-23 10:31:16 +03005642 num_counters = cnts->num_q_counters +
5643 cnts->num_cong_counters +
5644 cnts->num_ext_ppcnt_counters;
Daniel Jurgensaac44922018-01-04 17:25:40 +02005645
5646 /* q_counters are per IB device, query the master mdev */
Parav Pandit5dcecbc2019-07-23 10:31:16 +03005647 ret = mlx5_ib_query_q_counters(dev->mdev, cnts, stats, cnts->set_id);
Mark Bloch0ad17a82016-06-17 15:10:56 +03005648 if (ret)
Parav Pandite1f24a72017-04-16 07:29:29 +03005649 return ret;
Mark Bloch0ad17a82016-06-17 15:10:56 +03005650
Talat Batheesh9f876f32018-06-21 15:37:56 +03005651 if (MLX5_CAP_PCAM_FEATURE(dev->mdev, rx_icrc_encapsulated_counter)) {
Parav Pandit5dcecbc2019-07-23 10:31:16 +03005652 ret = mlx5_ib_query_ext_ppcnt_counters(dev, cnts, stats);
Talat Batheesh9f876f32018-06-21 15:37:56 +03005653 if (ret)
5654 return ret;
5655 }
5656
Parav Pandite1f24a72017-04-16 07:29:29 +03005657 if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
Daniel Jurgensaac44922018-01-04 17:25:40 +02005658 mdev = mlx5_ib_get_native_port_mdev(dev, port_num,
5659 &mdev_port_num);
5660 if (!mdev) {
5661 /* If port is not affiliated yet, its in down state
5662 * which doesn't have any counters yet, so it would be
5663 * zero. So no need to read from the HCA.
5664 */
5665 goto done;
5666 }
Majd Dibbiny71a0ff62017-12-21 17:38:26 +02005667 ret = mlx5_lag_query_cong_counters(dev->mdev,
5668 stats->value +
Parav Pandit5dcecbc2019-07-23 10:31:16 +03005669 cnts->num_q_counters,
5670 cnts->num_cong_counters,
5671 cnts->offsets +
5672 cnts->num_q_counters);
Daniel Jurgensaac44922018-01-04 17:25:40 +02005673
5674 mlx5_ib_put_native_port_mdev(dev, port_num);
Parav Pandite1f24a72017-04-16 07:29:29 +03005675 if (ret)
5676 return ret;
Mark Bloch0ad17a82016-06-17 15:10:56 +03005677 }
Kamal Heib7c16f472017-01-18 15:25:09 +02005678
Daniel Jurgensaac44922018-01-04 17:25:40 +02005679done:
Parav Pandite1f24a72017-04-16 07:29:29 +03005680 return num_counters;
Mark Bloch0ad17a82016-06-17 15:10:56 +03005681}
5682
Mark Zhang18d422c2019-07-02 13:02:41 +03005683static struct rdma_hw_stats *
5684mlx5_ib_counter_alloc_stats(struct rdma_counter *counter)
5685{
5686 struct mlx5_ib_dev *dev = to_mdev(counter->device);
Parav Pandit5dcecbc2019-07-23 10:31:16 +03005687 const struct mlx5_ib_counters *cnts =
Parav Pandit3e1f0002019-07-23 10:31:17 +03005688 get_counters(dev, counter->port - 1);
Mark Zhang18d422c2019-07-02 13:02:41 +03005689
5690 /* Q counters are in the beginning of all counters */
Parav Pandit5dcecbc2019-07-23 10:31:16 +03005691 return rdma_alloc_hw_stats_struct(cnts->names,
5692 cnts->num_q_counters,
Mark Zhang18d422c2019-07-02 13:02:41 +03005693 RDMA_HW_STATS_DEFAULT_LIFESPAN);
5694}
5695
5696static int mlx5_ib_counter_update_stats(struct rdma_counter *counter)
5697{
5698 struct mlx5_ib_dev *dev = to_mdev(counter->device);
Parav Pandit3e1f0002019-07-23 10:31:17 +03005699 const struct mlx5_ib_counters *cnts =
5700 get_counters(dev, counter->port - 1);
Mark Zhang18d422c2019-07-02 13:02:41 +03005701
Parav Pandit5dcecbc2019-07-23 10:31:16 +03005702 return mlx5_ib_query_q_counters(dev->mdev, cnts,
Mark Zhang18d422c2019-07-02 13:02:41 +03005703 counter->stats, counter->id);
5704}
5705
Mark Zhang45842fc2019-07-02 13:02:38 +03005706static int mlx5_ib_counter_bind_qp(struct rdma_counter *counter,
5707 struct ib_qp *qp)
5708{
5709 struct mlx5_ib_dev *dev = to_mdev(qp->device);
5710 u16 cnt_set_id = 0;
5711 int err;
5712
5713 if (!counter->id) {
5714 err = mlx5_cmd_alloc_q_counter(dev->mdev,
5715 &cnt_set_id,
5716 MLX5_SHARED_RESOURCE_UID);
5717 if (err)
5718 return err;
5719 counter->id = cnt_set_id;
5720 }
5721
5722 err = mlx5_ib_qp_set_counter(qp, counter);
5723 if (err)
5724 goto fail_set_counter;
5725
5726 return 0;
5727
5728fail_set_counter:
5729 mlx5_core_dealloc_q_counter(dev->mdev, cnt_set_id);
5730 counter->id = 0;
5731
5732 return err;
5733}
5734
5735static int mlx5_ib_counter_unbind_qp(struct ib_qp *qp)
5736{
5737 return mlx5_ib_qp_set_counter(qp, NULL);
5738}
5739
5740static int mlx5_ib_counter_dealloc(struct rdma_counter *counter)
5741{
5742 struct mlx5_ib_dev *dev = to_mdev(counter->device);
5743
5744 return mlx5_core_dealloc_q_counter(dev->mdev, counter->id);
5745}
5746
Denis Drozdovf6a8a192018-08-14 14:08:51 +03005747static int mlx5_ib_rn_get_params(struct ib_device *device, u8 port_num,
5748 enum rdma_netdev_t type,
5749 struct rdma_netdev_alloc_params *params)
Erez Shitrit693dfd52017-04-27 17:01:34 +03005750{
5751 if (type != RDMA_NETDEV_IPOIB)
Denis Drozdovf6a8a192018-08-14 14:08:51 +03005752 return -EOPNOTSUPP;
Erez Shitrit693dfd52017-04-27 17:01:34 +03005753
Denis Drozdovf6a8a192018-08-14 14:08:51 +03005754 return mlx5_rdma_rn_get_params(to_mdev(device)->mdev, device, params);
Erez Shitrit693dfd52017-04-27 17:01:34 +03005755}
5756
Maor Gottliebfe248c32017-05-30 10:29:14 +03005757static void delay_drop_debugfs_cleanup(struct mlx5_ib_dev *dev)
5758{
Greg Kroah-Hartman09b09652019-11-04 08:38:07 +01005759 if (!dev->delay_drop.dir_debugfs)
Maor Gottliebfe248c32017-05-30 10:29:14 +03005760 return;
Greg Kroah-Hartman09b09652019-11-04 08:38:07 +01005761 debugfs_remove_recursive(dev->delay_drop.dir_debugfs);
5762 dev->delay_drop.dir_debugfs = NULL;
Maor Gottliebfe248c32017-05-30 10:29:14 +03005763}
5764
Maor Gottlieb03404e82017-05-30 10:29:13 +03005765static void cancel_delay_drop(struct mlx5_ib_dev *dev)
5766{
5767 if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
5768 return;
5769
5770 cancel_work_sync(&dev->delay_drop.delay_drop_work);
Maor Gottliebfe248c32017-05-30 10:29:14 +03005771 delay_drop_debugfs_cleanup(dev);
5772}
5773
5774static ssize_t delay_drop_timeout_read(struct file *filp, char __user *buf,
5775 size_t count, loff_t *pos)
5776{
5777 struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
5778 char lbuf[20];
5779 int len;
5780
5781 len = snprintf(lbuf, sizeof(lbuf), "%u\n", delay_drop->timeout);
5782 return simple_read_from_buffer(buf, count, pos, lbuf, len);
5783}
5784
5785static ssize_t delay_drop_timeout_write(struct file *filp, const char __user *buf,
5786 size_t count, loff_t *pos)
5787{
5788 struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
5789 u32 timeout;
5790 u32 var;
5791
5792 if (kstrtouint_from_user(buf, count, 0, &var))
5793 return -EFAULT;
5794
5795 timeout = min_t(u32, roundup(var, 100), MLX5_MAX_DELAY_DROP_TIMEOUT_MS *
5796 1000);
5797 if (timeout != var)
5798 mlx5_ib_dbg(delay_drop->dev, "Round delay drop timeout to %u usec\n",
5799 timeout);
5800
5801 delay_drop->timeout = timeout;
5802
5803 return count;
5804}
5805
5806static const struct file_operations fops_delay_drop_timeout = {
5807 .owner = THIS_MODULE,
5808 .open = simple_open,
5809 .write = delay_drop_timeout_write,
5810 .read = delay_drop_timeout_read,
5811};
5812
Greg Kroah-Hartman09b09652019-11-04 08:38:07 +01005813static void delay_drop_debugfs_init(struct mlx5_ib_dev *dev)
Maor Gottliebfe248c32017-05-30 10:29:14 +03005814{
Greg Kroah-Hartman09b09652019-11-04 08:38:07 +01005815 struct dentry *root;
Maor Gottliebfe248c32017-05-30 10:29:14 +03005816
5817 if (!mlx5_debugfs_root)
Greg Kroah-Hartman09b09652019-11-04 08:38:07 +01005818 return;
Maor Gottliebfe248c32017-05-30 10:29:14 +03005819
Greg Kroah-Hartman09b09652019-11-04 08:38:07 +01005820 root = debugfs_create_dir("delay_drop", dev->mdev->priv.dbg_root);
5821 dev->delay_drop.dir_debugfs = root;
Maor Gottliebfe248c32017-05-30 10:29:14 +03005822
Greg Kroah-Hartman09b09652019-11-04 08:38:07 +01005823 debugfs_create_atomic_t("num_timeout_events", 0400, root,
5824 &dev->delay_drop.events_cnt);
5825 debugfs_create_atomic_t("num_rqs", 0400, root,
5826 &dev->delay_drop.rqs_cnt);
5827 debugfs_create_file("timeout", 0600, root, &dev->delay_drop,
5828 &fops_delay_drop_timeout);
Maor Gottlieb03404e82017-05-30 10:29:13 +03005829}
5830
5831static void init_delay_drop(struct mlx5_ib_dev *dev)
5832{
5833 if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
5834 return;
5835
5836 mutex_init(&dev->delay_drop.lock);
5837 dev->delay_drop.dev = dev;
5838 dev->delay_drop.activate = false;
5839 dev->delay_drop.timeout = MLX5_MAX_DELAY_DROP_TIMEOUT_MS * 1000;
5840 INIT_WORK(&dev->delay_drop.delay_drop_work, delay_drop_handler);
Maor Gottliebfe248c32017-05-30 10:29:14 +03005841 atomic_set(&dev->delay_drop.rqs_cnt, 0);
5842 atomic_set(&dev->delay_drop.events_cnt, 0);
5843
Greg Kroah-Hartman09b09652019-11-04 08:38:07 +01005844 delay_drop_debugfs_init(dev);
Maor Gottlieb03404e82017-05-30 10:29:13 +03005845}
5846
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005847static void mlx5_ib_unbind_slave_port(struct mlx5_ib_dev *ibdev,
5848 struct mlx5_ib_multiport_info *mpi)
5849{
5850 u8 port_num = mlx5_core_native_port_num(mpi->mdev) - 1;
5851 struct mlx5_ib_port *port = &ibdev->port[port_num];
5852 int comps;
5853 int err;
5854 int i;
5855
Leon Romanovsky9dc4cff2019-08-13 13:28:14 +03005856 lockdep_assert_held(&mlx5_ib_multiport_mutex);
5857
Parav Pandita9e546e2018-01-04 17:25:39 +02005858 mlx5_ib_cleanup_cong_debugfs(ibdev, port_num);
5859
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005860 spin_lock(&port->mp.mpi_lock);
5861 if (!mpi->ibdev) {
5862 spin_unlock(&port->mp.mpi_lock);
5863 return;
5864 }
Saeed Mahameeddf097a22018-11-26 14:39:00 -08005865
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005866 mpi->ibdev = NULL;
5867
5868 spin_unlock(&port->mp.mpi_lock);
Leon Romanovsky23eaf3b2019-07-31 11:38:52 +03005869 if (mpi->mdev_events.notifier_call)
5870 mlx5_notifier_unregister(mpi->mdev, &mpi->mdev_events);
5871 mpi->mdev_events.notifier_call = NULL;
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005872 mlx5_remove_netdev_notifier(ibdev, port_num);
5873 spin_lock(&port->mp.mpi_lock);
5874
5875 comps = mpi->mdev_refcnt;
5876 if (comps) {
5877 mpi->unaffiliate = true;
5878 init_completion(&mpi->unref_comp);
5879 spin_unlock(&port->mp.mpi_lock);
5880
5881 for (i = 0; i < comps; i++)
5882 wait_for_completion(&mpi->unref_comp);
5883
5884 spin_lock(&port->mp.mpi_lock);
5885 mpi->unaffiliate = false;
5886 }
5887
5888 port->mp.mpi = NULL;
5889
5890 list_add_tail(&mpi->list, &mlx5_ib_unaffiliated_port_list);
5891
5892 spin_unlock(&port->mp.mpi_lock);
5893
5894 err = mlx5_nic_vport_unaffiliate_multiport(mpi->mdev);
5895
5896 mlx5_ib_dbg(ibdev, "unaffiliated port %d\n", port_num + 1);
5897 /* Log an error, still needed to cleanup the pointers and add
5898 * it back to the list.
5899 */
5900 if (err)
5901 mlx5_ib_err(ibdev, "Failed to unaffiliate port %u\n",
5902 port_num + 1);
5903
Mark Bloch95579e72019-03-28 15:27:33 +02005904 ibdev->port[port_num].roce.last_port_state = IB_PORT_DOWN;
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005905}
5906
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005907static bool mlx5_ib_bind_slave_port(struct mlx5_ib_dev *ibdev,
5908 struct mlx5_ib_multiport_info *mpi)
5909{
5910 u8 port_num = mlx5_core_native_port_num(mpi->mdev) - 1;
5911 int err;
5912
Leon Romanovsky9dc4cff2019-08-13 13:28:14 +03005913 lockdep_assert_held(&mlx5_ib_multiport_mutex);
5914
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005915 spin_lock(&ibdev->port[port_num].mp.mpi_lock);
5916 if (ibdev->port[port_num].mp.mpi) {
Qing Huang25771882018-07-23 14:15:08 -07005917 mlx5_ib_dbg(ibdev, "port %d already affiliated.\n",
5918 port_num + 1);
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005919 spin_unlock(&ibdev->port[port_num].mp.mpi_lock);
5920 return false;
5921 }
5922
5923 ibdev->port[port_num].mp.mpi = mpi;
5924 mpi->ibdev = ibdev;
Saeed Mahameeddf097a22018-11-26 14:39:00 -08005925 mpi->mdev_events.notifier_call = NULL;
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005926 spin_unlock(&ibdev->port[port_num].mp.mpi_lock);
5927
5928 err = mlx5_nic_vport_affiliate_multiport(ibdev->mdev, mpi->mdev);
5929 if (err)
5930 goto unbind;
5931
5932 err = get_port_caps(ibdev, mlx5_core_native_port_num(mpi->mdev));
5933 if (err)
5934 goto unbind;
5935
5936 err = mlx5_add_netdev_notifier(ibdev, port_num);
5937 if (err) {
5938 mlx5_ib_err(ibdev, "failed adding netdev notifier for port %u\n",
5939 port_num + 1);
5940 goto unbind;
5941 }
5942
Saeed Mahameeddf097a22018-11-26 14:39:00 -08005943 mpi->mdev_events.notifier_call = mlx5_ib_event_slave_port;
5944 mlx5_notifier_register(mpi->mdev, &mpi->mdev_events);
5945
Greg Kroah-Hartman73eb8f02019-01-22 16:17:57 +01005946 mlx5_ib_init_cong_debugfs(ibdev, port_num);
Parav Pandita9e546e2018-01-04 17:25:39 +02005947
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005948 return true;
5949
5950unbind:
5951 mlx5_ib_unbind_slave_port(ibdev, mpi);
5952 return false;
5953}
5954
5955static int mlx5_ib_init_multiport_master(struct mlx5_ib_dev *dev)
5956{
5957 int port_num = mlx5_core_native_port_num(dev->mdev) - 1;
5958 enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev,
5959 port_num + 1);
5960 struct mlx5_ib_multiport_info *mpi;
5961 int err;
5962 int i;
5963
5964 if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
5965 return 0;
5966
5967 err = mlx5_query_nic_vport_system_image_guid(dev->mdev,
5968 &dev->sys_image_guid);
5969 if (err)
5970 return err;
5971
5972 err = mlx5_nic_vport_enable_roce(dev->mdev);
5973 if (err)
5974 return err;
5975
5976 mutex_lock(&mlx5_ib_multiport_mutex);
5977 for (i = 0; i < dev->num_ports; i++) {
5978 bool bound = false;
5979
5980 /* build a stub multiport info struct for the native port. */
5981 if (i == port_num) {
5982 mpi = kzalloc(sizeof(*mpi), GFP_KERNEL);
5983 if (!mpi) {
5984 mutex_unlock(&mlx5_ib_multiport_mutex);
5985 mlx5_nic_vport_disable_roce(dev->mdev);
5986 return -ENOMEM;
5987 }
5988
5989 mpi->is_master = true;
5990 mpi->mdev = dev->mdev;
5991 mpi->sys_image_guid = dev->sys_image_guid;
5992 dev->port[i].mp.mpi = mpi;
5993 mpi->ibdev = dev;
5994 mpi = NULL;
5995 continue;
5996 }
5997
5998 list_for_each_entry(mpi, &mlx5_ib_unaffiliated_port_list,
5999 list) {
6000 if (dev->sys_image_guid == mpi->sys_image_guid &&
6001 (mlx5_core_native_port_num(mpi->mdev) - 1) == i) {
6002 bound = mlx5_ib_bind_slave_port(dev, mpi);
6003 }
6004
6005 if (bound) {
Vu Phamc42260f12019-04-29 18:14:05 +00006006 dev_dbg(mpi->mdev->device,
6007 "removing port from unaffiliated list.\n");
Daniel Jurgens32f69e42018-01-04 17:25:36 +02006008 mlx5_ib_dbg(dev, "port %d bound\n", i + 1);
6009 list_del(&mpi->list);
6010 break;
6011 }
6012 }
6013 if (!bound) {
6014 get_port_caps(dev, i + 1);
6015 mlx5_ib_dbg(dev, "no free port found for port %d\n",
6016 i + 1);
6017 }
6018 }
6019
6020 list_add_tail(&dev->ib_dev_list, &mlx5_ib_dev_list);
6021 mutex_unlock(&mlx5_ib_multiport_mutex);
6022 return err;
6023}
6024
6025static void mlx5_ib_cleanup_multiport_master(struct mlx5_ib_dev *dev)
6026{
6027 int port_num = mlx5_core_native_port_num(dev->mdev) - 1;
6028 enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev,
6029 port_num + 1);
6030 int i;
6031
6032 if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
6033 return;
6034
6035 mutex_lock(&mlx5_ib_multiport_mutex);
6036 for (i = 0; i < dev->num_ports; i++) {
6037 if (dev->port[i].mp.mpi) {
6038 /* Destroy the native port stub */
6039 if (i == port_num) {
6040 kfree(dev->port[i].mp.mpi);
6041 dev->port[i].mp.mpi = NULL;
6042 } else {
6043 mlx5_ib_dbg(dev, "unbinding port_num: %d\n", i + 1);
6044 mlx5_ib_unbind_slave_port(dev, dev->port[i].mp.mpi);
6045 }
6046 }
6047 }
6048
6049 mlx5_ib_dbg(dev, "removing from devlist\n");
6050 list_del(&dev->ib_dev_list);
6051 mutex_unlock(&mlx5_ib_multiport_mutex);
6052
6053 mlx5_nic_vport_disable_roce(dev->mdev);
6054}
6055
Yishai Hadas7be76be2019-12-12 13:09:27 +02006056static int var_obj_cleanup(struct ib_uobject *uobject,
6057 enum rdma_remove_reason why,
6058 struct uverbs_attr_bundle *attrs)
6059{
6060 struct mlx5_user_mmap_entry *obj = uobject->object;
6061
6062 rdma_user_mmap_entry_remove(&obj->rdma_entry);
6063 return 0;
6064}
6065
6066static struct mlx5_user_mmap_entry *
6067alloc_var_entry(struct mlx5_ib_ucontext *c)
6068{
6069 struct mlx5_user_mmap_entry *entry;
6070 struct mlx5_var_table *var_table;
6071 u32 page_idx;
6072 int err;
6073
6074 var_table = &to_mdev(c->ibucontext.device)->var_table;
6075 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
6076 if (!entry)
6077 return ERR_PTR(-ENOMEM);
6078
6079 mutex_lock(&var_table->bitmap_lock);
6080 page_idx = find_first_zero_bit(var_table->bitmap,
6081 var_table->num_var_hw_entries);
6082 if (page_idx >= var_table->num_var_hw_entries) {
6083 err = -ENOSPC;
6084 mutex_unlock(&var_table->bitmap_lock);
6085 goto end;
6086 }
6087
6088 set_bit(page_idx, var_table->bitmap);
6089 mutex_unlock(&var_table->bitmap_lock);
6090
6091 entry->address = var_table->hw_start_addr +
6092 (page_idx * var_table->stride_size);
6093 entry->page_idx = page_idx;
6094 entry->mmap_flag = MLX5_IB_MMAP_TYPE_VAR;
6095
6096 err = rdma_user_mmap_entry_insert_range(
6097 &c->ibucontext, &entry->rdma_entry, var_table->stride_size,
6098 MLX5_IB_MMAP_OFFSET_START << 16,
6099 (MLX5_IB_MMAP_OFFSET_END << 16) + (1UL << 16) - 1);
6100 if (err)
6101 goto err_insert;
6102
6103 return entry;
6104
6105err_insert:
6106 mutex_lock(&var_table->bitmap_lock);
6107 clear_bit(page_idx, var_table->bitmap);
6108 mutex_unlock(&var_table->bitmap_lock);
6109end:
6110 kfree(entry);
6111 return ERR_PTR(err);
6112}
6113
6114static int UVERBS_HANDLER(MLX5_IB_METHOD_VAR_OBJ_ALLOC)(
6115 struct uverbs_attr_bundle *attrs)
6116{
6117 struct ib_uobject *uobj = uverbs_attr_get_uobject(
6118 attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_HANDLE);
6119 struct mlx5_ib_ucontext *c;
6120 struct mlx5_user_mmap_entry *entry;
6121 u64 mmap_offset;
6122 u32 length;
6123 int err;
6124
6125 c = to_mucontext(ib_uverbs_get_ucontext(attrs));
6126 if (IS_ERR(c))
6127 return PTR_ERR(c);
6128
6129 entry = alloc_var_entry(c);
6130 if (IS_ERR(entry))
6131 return PTR_ERR(entry);
6132
6133 mmap_offset = mlx5_entry_to_mmap_offset(entry);
6134 length = entry->rdma_entry.npages * PAGE_SIZE;
6135 uobj->object = entry;
6136
6137 err = uverbs_copy_to(attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_MMAP_OFFSET,
6138 &mmap_offset, sizeof(mmap_offset));
6139 if (err)
6140 goto err;
6141
6142 err = uverbs_copy_to(attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_PAGE_ID,
6143 &entry->page_idx, sizeof(entry->page_idx));
6144 if (err)
6145 goto err;
6146
6147 err = uverbs_copy_to(attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_MMAP_LENGTH,
6148 &length, sizeof(length));
6149 if (err)
6150 goto err;
6151
6152 return 0;
6153
6154err:
6155 rdma_user_mmap_entry_remove(&entry->rdma_entry);
6156 return err;
6157}
6158
6159DECLARE_UVERBS_NAMED_METHOD(
6160 MLX5_IB_METHOD_VAR_OBJ_ALLOC,
6161 UVERBS_ATTR_IDR(MLX5_IB_ATTR_VAR_OBJ_ALLOC_HANDLE,
6162 MLX5_IB_OBJECT_VAR,
6163 UVERBS_ACCESS_NEW,
6164 UA_MANDATORY),
6165 UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_VAR_OBJ_ALLOC_PAGE_ID,
6166 UVERBS_ATTR_TYPE(u32),
6167 UA_MANDATORY),
6168 UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_VAR_OBJ_ALLOC_MMAP_LENGTH,
6169 UVERBS_ATTR_TYPE(u32),
6170 UA_MANDATORY),
6171 UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_VAR_OBJ_ALLOC_MMAP_OFFSET,
6172 UVERBS_ATTR_TYPE(u64),
6173 UA_MANDATORY));
6174
6175DECLARE_UVERBS_NAMED_METHOD_DESTROY(
6176 MLX5_IB_METHOD_VAR_OBJ_DESTROY,
6177 UVERBS_ATTR_IDR(MLX5_IB_ATTR_VAR_OBJ_DESTROY_HANDLE,
6178 MLX5_IB_OBJECT_VAR,
6179 UVERBS_ACCESS_DESTROY,
6180 UA_MANDATORY));
6181
6182DECLARE_UVERBS_NAMED_OBJECT(MLX5_IB_OBJECT_VAR,
6183 UVERBS_TYPE_ALLOC_IDR(var_obj_cleanup),
6184 &UVERBS_METHOD(MLX5_IB_METHOD_VAR_OBJ_ALLOC),
6185 &UVERBS_METHOD(MLX5_IB_METHOD_VAR_OBJ_DESTROY));
6186
6187static bool var_is_supported(struct ib_device *device)
6188{
6189 struct mlx5_ib_dev *dev = to_mdev(device);
6190
6191 return (MLX5_CAP_GEN_64(dev->mdev, general_obj_types) &
6192 MLX5_GENERAL_OBJ_TYPES_CAP_VIRTIO_NET_Q);
6193}
6194
Jason Gunthorpe9a119cd2018-07-04 08:50:28 +03006195ADD_UVERBS_ATTRIBUTES_SIMPLE(
6196 mlx5_ib_dm,
6197 UVERBS_OBJECT_DM,
6198 UVERBS_METHOD_DM_ALLOC,
6199 UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_ALLOC_DM_RESP_START_OFFSET,
6200 UVERBS_ATTR_TYPE(u64),
Jason Gunthorpe83bb4442018-07-04 08:50:29 +03006201 UA_MANDATORY),
Jason Gunthorpe9a119cd2018-07-04 08:50:28 +03006202 UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_ALLOC_DM_RESP_PAGE_INDEX,
6203 UVERBS_ATTR_TYPE(u16),
Ariel Levkovich3b113a12019-05-05 17:07:11 +03006204 UA_OPTIONAL),
6205 UVERBS_ATTR_CONST_IN(MLX5_IB_ATTR_ALLOC_DM_REQ_TYPE,
6206 enum mlx5_ib_uapi_dm_type,
6207 UA_OPTIONAL));
Ariel Levkovich24da0012018-04-05 18:53:27 +03006208
Jason Gunthorpe9a119cd2018-07-04 08:50:28 +03006209ADD_UVERBS_ATTRIBUTES_SIMPLE(
6210 mlx5_ib_flow_action,
6211 UVERBS_OBJECT_FLOW_ACTION,
6212 UVERBS_METHOD_FLOW_ACTION_ESP_CREATE,
Jason Gunthorpebccd0622018-07-26 16:37:14 -06006213 UVERBS_ATTR_FLAGS_IN(MLX5_IB_ATTR_CREATE_FLOW_ACTION_FLAGS,
6214 enum mlx5_ib_uapi_flow_action_flags));
Aviad Yehezkelc6475a02018-03-28 09:27:50 +03006215
Jason Gunthorpe0cbf4322018-11-12 22:59:50 +02006216static const struct uapi_definition mlx5_ib_defs[] = {
6217#if IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS)
Jason Gunthorpe36e235c2018-11-12 22:59:53 +02006218 UAPI_DEF_CHAIN(mlx5_ib_devx_defs),
Jason Gunthorpe0cbf4322018-11-12 22:59:50 +02006219 UAPI_DEF_CHAIN(mlx5_ib_flow_defs),
6220#endif
Matan Barak8c846602018-03-28 09:27:41 +03006221
Jason Gunthorpe0cbf4322018-11-12 22:59:50 +02006222 UAPI_DEF_CHAIN_OBJ_TREE(UVERBS_OBJECT_FLOW_ACTION,
6223 &mlx5_ib_flow_action),
6224 UAPI_DEF_CHAIN_OBJ_TREE(UVERBS_OBJECT_DM, &mlx5_ib_dm),
Yishai Hadas7be76be2019-12-12 13:09:27 +02006225 UAPI_DEF_CHAIN_OBJ_TREE_NAMED(MLX5_IB_OBJECT_VAR,
6226 UAPI_DEF_IS_OBJ_SUPPORTED(var_is_supported)),
Jason Gunthorpe0cbf4322018-11-12 22:59:50 +02006227 {}
6228};
Matan Barak8c846602018-03-28 09:27:41 +03006229
Raed Salem1a1e03d2018-05-31 16:43:41 +03006230static int mlx5_ib_read_counters(struct ib_counters *counters,
6231 struct ib_counters_read_attr *read_attr,
6232 struct uverbs_attr_bundle *attrs)
6233{
6234 struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
6235 struct mlx5_read_counters_attr mread_attr = {};
6236 struct mlx5_ib_flow_counters_desc *desc;
6237 int ret, i;
6238
6239 mutex_lock(&mcounters->mcntrs_mutex);
6240 if (mcounters->cntrs_max_index > read_attr->ncounters) {
6241 ret = -EINVAL;
6242 goto err_bound;
6243 }
6244
6245 mread_attr.out = kcalloc(mcounters->counters_num, sizeof(u64),
6246 GFP_KERNEL);
6247 if (!mread_attr.out) {
6248 ret = -ENOMEM;
6249 goto err_bound;
6250 }
6251
6252 mread_attr.hw_cntrs_hndl = mcounters->hw_cntrs_hndl;
6253 mread_attr.flags = read_attr->flags;
6254 ret = mcounters->read_counters(counters->device, &mread_attr);
6255 if (ret)
6256 goto err_read;
6257
6258 /* do the pass over the counters data array to assign according to the
6259 * descriptions and indexing pairs
6260 */
6261 desc = mcounters->counters_data;
6262 for (i = 0; i < mcounters->ncounters; i++)
6263 read_attr->counters_buff[desc[i].index] += mread_attr.out[desc[i].description];
6264
6265err_read:
6266 kfree(mread_attr.out);
6267err_bound:
6268 mutex_unlock(&mcounters->mcntrs_mutex);
6269 return ret;
6270}
6271
Raed Salemb29e2a12018-05-31 16:43:38 +03006272static int mlx5_ib_destroy_counters(struct ib_counters *counters)
6273{
6274 struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
6275
Raed Salem3b3233f2018-05-31 16:43:39 +03006276 counters_clear_description(counters);
6277 if (mcounters->hw_cntrs_hndl)
6278 mlx5_fc_destroy(to_mdev(counters->device)->mdev,
6279 mcounters->hw_cntrs_hndl);
6280
Raed Salemb29e2a12018-05-31 16:43:38 +03006281 kfree(mcounters);
6282
6283 return 0;
6284}
6285
6286static struct ib_counters *mlx5_ib_create_counters(struct ib_device *device,
6287 struct uverbs_attr_bundle *attrs)
6288{
6289 struct mlx5_ib_mcounters *mcounters;
6290
6291 mcounters = kzalloc(sizeof(*mcounters), GFP_KERNEL);
6292 if (!mcounters)
6293 return ERR_PTR(-ENOMEM);
6294
Raed Salem3b3233f2018-05-31 16:43:39 +03006295 mutex_init(&mcounters->mcntrs_mutex);
6296
Raed Salemb29e2a12018-05-31 16:43:38 +03006297 return &mcounters->ibcntrs;
6298}
6299
Mark Blochfb652d32019-03-28 15:27:42 +02006300static void mlx5_ib_stage_init_cleanup(struct mlx5_ib_dev *dev)
Eli Cohene126ba92013-07-07 17:25:49 +03006301{
Daniel Jurgens32f69e42018-01-04 17:25:36 +02006302 mlx5_ib_cleanup_multiport_master(dev);
Jason Gunthorpe806b1012019-10-09 13:09:23 -03006303 WARN_ON(!xa_empty(&dev->odp_mkeys));
Jason Gunthorpe806b1012019-10-09 13:09:23 -03006304 cleanup_srcu_struct(&dev->odp_srcu);
Ariel Levkovich4056b122019-05-05 17:07:12 +03006305
Jason Gunthorpe50211ec2019-10-09 13:09:22 -03006306 WARN_ON(!xa_empty(&dev->sig_mrs));
Ariel Levkovich4056b122019-05-05 17:07:12 +03006307 WARN_ON(!bitmap_empty(dev->dm.memic_alloc_pages, MLX5_MAX_MEMIC_PAGES));
Mark Bloch16c19752018-01-01 13:06:58 +02006308}
6309
Mark Blochfb652d32019-03-28 15:27:42 +02006310static int mlx5_ib_stage_init_init(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02006311{
6312 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03006313 int err;
Daniel Jurgens32f69e42018-01-04 17:25:36 +02006314 int i;
Eli Cohene126ba92013-07-07 17:25:49 +03006315
Daniel Jurgens32f69e42018-01-04 17:25:36 +02006316 for (i = 0; i < dev->num_ports; i++) {
6317 spin_lock_init(&dev->port[i].mp.mpi_lock);
Mark Bloch95579e72019-03-28 15:27:33 +02006318 rwlock_init(&dev->port[i].roce.netdev_lock);
Mark Blochd3b5cc12019-03-28 15:46:26 +02006319 dev->port[i].roce.dev = dev;
6320 dev->port[i].roce.native_port_num = i + 1;
6321 dev->port[i].roce.last_port_state = IB_PORT_DOWN;
Daniel Jurgens32f69e42018-01-04 17:25:36 +02006322 }
6323
Moni Shoua00815752019-08-15 11:38:32 +03006324 mlx5_ib_internal_fill_odp_caps(dev);
6325
Daniel Jurgens32f69e42018-01-04 17:25:36 +02006326 err = mlx5_ib_init_multiport_master(dev);
Eli Cohene126ba92013-07-07 17:25:49 +03006327 if (err)
Mark Blochda796cc2019-03-28 15:27:35 +02006328 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03006329
Mark Blocha989ea02019-03-28 15:27:40 +02006330 err = set_has_smi_cap(dev);
6331 if (err)
6332 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03006333
Daniel Jurgens32f69e42018-01-04 17:25:36 +02006334 if (!mlx5_core_mp_enabled(mdev)) {
Daniel Jurgens32f69e42018-01-04 17:25:36 +02006335 for (i = 1; i <= dev->num_ports; i++) {
6336 err = get_port_caps(dev, i);
6337 if (err)
6338 break;
6339 }
6340 } else {
6341 err = get_port_caps(dev, mlx5_core_native_port_num(mdev));
6342 }
6343 if (err)
6344 goto err_mp;
6345
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03006346 if (mlx5_use_mad_ifc(dev))
6347 get_ext_port_caps(dev);
Eli Cohene126ba92013-07-07 17:25:49 +03006348
Eli Cohene126ba92013-07-07 17:25:49 +03006349 dev->ib_dev.node_type = RDMA_NODE_IB_CA;
Sagi Grimbergc6790aa2015-09-24 10:34:23 +03006350 dev->ib_dev.local_dma_lkey = 0 /* not supported for now */;
Daniel Jurgens508562d2018-01-04 17:25:34 +02006351 dev->ib_dev.phys_port_cnt = dev->num_ports;
Saeed Mahameedf2f3df52018-11-19 10:52:38 -08006352 dev->ib_dev.num_comp_vectors = mlx5_comp_vectors_count(mdev);
Vu Phamc42260f12019-04-29 18:14:05 +00006353 dev->ib_dev.dev.parent = mdev->device;
Eli Cohene126ba92013-07-07 17:25:49 +03006354
Mark Bloch3cc297d2018-01-01 13:07:03 +02006355 mutex_init(&dev->cap_mask_mutex);
6356 INIT_LIST_HEAD(&dev->qp_list);
6357 spin_lock_init(&dev->reset_flow_resource_lock);
Jason Gunthorpe806b1012019-10-09 13:09:23 -03006358 xa_init(&dev->odp_mkeys);
Jason Gunthorpe50211ec2019-10-09 13:09:22 -03006359 xa_init(&dev->sig_mrs);
Mark Bloch3cc297d2018-01-01 13:07:03 +02006360
Ariel Levkovich3b113a12019-05-05 17:07:11 +03006361 spin_lock_init(&dev->dm.lock);
6362 dev->dm.dev = mdev;
Ariel Levkovich24da0012018-04-05 18:53:27 +03006363
Jason Gunthorpe806b1012019-10-09 13:09:23 -03006364 err = init_srcu_struct(&dev->odp_srcu);
6365 if (err)
6366 goto err_mp;
Mark Bloch3cc297d2018-01-01 13:07:03 +02006367
Mark Bloch16c19752018-01-01 13:06:58 +02006368 return 0;
Ariel Levkovich25c13322019-05-05 17:07:13 +03006369
Daniel Jurgens32f69e42018-01-04 17:25:36 +02006370err_mp:
6371 mlx5_ib_cleanup_multiport_master(dev);
Mark Bloch16c19752018-01-01 13:06:58 +02006372
Mark Bloch16c19752018-01-01 13:06:58 +02006373 return -ENOMEM;
6374}
6375
Mark Bloch9a4ca382018-01-16 14:42:35 +00006376static int mlx5_ib_stage_flow_db_init(struct mlx5_ib_dev *dev)
6377{
6378 dev->flow_db = kzalloc(sizeof(*dev->flow_db), GFP_KERNEL);
6379
6380 if (!dev->flow_db)
6381 return -ENOMEM;
6382
6383 mutex_init(&dev->flow_db->lock);
6384
6385 return 0;
6386}
6387
6388static void mlx5_ib_stage_flow_db_cleanup(struct mlx5_ib_dev *dev)
6389{
6390 kfree(dev->flow_db);
6391}
6392
Kamal Heib96458233e2018-12-10 21:09:38 +02006393static const struct ib_device_ops mlx5_ib_dev_ops = {
Jason Gunthorpe7a154142019-06-05 14:39:26 -03006394 .owner = THIS_MODULE,
Jason Gunthorpeb9560a42019-06-05 14:39:24 -03006395 .driver_id = RDMA_DRIVER_MLX5,
Jason Gunthorpe72c6ec12019-06-05 14:39:25 -03006396 .uverbs_abi_ver = MLX5_IB_UVERBS_ABI_VERSION,
Jason Gunthorpeb9560a42019-06-05 14:39:24 -03006397
Kamal Heib96458233e2018-12-10 21:09:38 +02006398 .add_gid = mlx5_ib_add_gid,
6399 .alloc_mr = mlx5_ib_alloc_mr,
Max Gurtovoy6c984472019-06-11 18:52:42 +03006400 .alloc_mr_integrity = mlx5_ib_alloc_mr_integrity,
Kamal Heib96458233e2018-12-10 21:09:38 +02006401 .alloc_pd = mlx5_ib_alloc_pd,
6402 .alloc_ucontext = mlx5_ib_alloc_ucontext,
6403 .attach_mcast = mlx5_ib_mcg_attach,
6404 .check_mr_status = mlx5_ib_check_mr_status,
6405 .create_ah = mlx5_ib_create_ah,
6406 .create_counters = mlx5_ib_create_counters,
6407 .create_cq = mlx5_ib_create_cq,
6408 .create_flow = mlx5_ib_create_flow,
6409 .create_qp = mlx5_ib_create_qp,
6410 .create_srq = mlx5_ib_create_srq,
6411 .dealloc_pd = mlx5_ib_dealloc_pd,
6412 .dealloc_ucontext = mlx5_ib_dealloc_ucontext,
6413 .del_gid = mlx5_ib_del_gid,
6414 .dereg_mr = mlx5_ib_dereg_mr,
6415 .destroy_ah = mlx5_ib_destroy_ah,
6416 .destroy_counters = mlx5_ib_destroy_counters,
6417 .destroy_cq = mlx5_ib_destroy_cq,
6418 .destroy_flow = mlx5_ib_destroy_flow,
6419 .destroy_flow_action = mlx5_ib_destroy_flow_action,
6420 .destroy_qp = mlx5_ib_destroy_qp,
6421 .destroy_srq = mlx5_ib_destroy_srq,
6422 .detach_mcast = mlx5_ib_mcg_detach,
6423 .disassociate_ucontext = mlx5_ib_disassociate_ucontext,
6424 .drain_rq = mlx5_ib_drain_rq,
6425 .drain_sq = mlx5_ib_drain_sq,
Michael Guralnik11f552e2019-06-10 15:21:24 +03006426 .enable_driver = mlx5_ib_enable_driver,
Erez Alfasie1b95ae2019-10-16 09:23:07 +03006427 .fill_res_entry = mlx5_ib_fill_res_entry,
Erez Alfasi4061ff72019-10-16 09:23:08 +03006428 .fill_stat_entry = mlx5_ib_fill_stat_entry,
Kamal Heib96458233e2018-12-10 21:09:38 +02006429 .get_dev_fw_str = get_dev_fw_str,
6430 .get_dma_mr = mlx5_ib_get_dma_mr,
6431 .get_link_layer = mlx5_ib_port_link_layer,
6432 .map_mr_sg = mlx5_ib_map_mr_sg,
Max Gurtovoy6c984472019-06-11 18:52:42 +03006433 .map_mr_sg_pi = mlx5_ib_map_mr_sg_pi,
Kamal Heib96458233e2018-12-10 21:09:38 +02006434 .mmap = mlx5_ib_mmap,
Yishai Hadasdc2316e2019-12-12 12:02:37 +02006435 .mmap_free = mlx5_ib_mmap_free,
Kamal Heib96458233e2018-12-10 21:09:38 +02006436 .modify_cq = mlx5_ib_modify_cq,
6437 .modify_device = mlx5_ib_modify_device,
6438 .modify_port = mlx5_ib_modify_port,
6439 .modify_qp = mlx5_ib_modify_qp,
6440 .modify_srq = mlx5_ib_modify_srq,
6441 .poll_cq = mlx5_ib_poll_cq,
6442 .post_recv = mlx5_ib_post_recv,
6443 .post_send = mlx5_ib_post_send,
6444 .post_srq_recv = mlx5_ib_post_srq_recv,
6445 .process_mad = mlx5_ib_process_mad,
6446 .query_ah = mlx5_ib_query_ah,
6447 .query_device = mlx5_ib_query_device,
6448 .query_gid = mlx5_ib_query_gid,
6449 .query_pkey = mlx5_ib_query_pkey,
6450 .query_qp = mlx5_ib_query_qp,
6451 .query_srq = mlx5_ib_query_srq,
6452 .read_counters = mlx5_ib_read_counters,
6453 .reg_user_mr = mlx5_ib_reg_user_mr,
6454 .req_notify_cq = mlx5_ib_arm_cq,
6455 .rereg_user_mr = mlx5_ib_rereg_user_mr,
6456 .resize_cq = mlx5_ib_resize_cq,
Leon Romanovskyd3456912019-04-03 16:42:42 +03006457
6458 INIT_RDMA_OBJ_SIZE(ib_ah, mlx5_ib_ah, ibah),
Leon Romanovskye39afe32019-05-28 14:37:29 +03006459 INIT_RDMA_OBJ_SIZE(ib_cq, mlx5_ib_cq, ibcq),
Leon Romanovsky21a428a2019-02-03 14:55:51 +02006460 INIT_RDMA_OBJ_SIZE(ib_pd, mlx5_ib_pd, ibpd),
Leon Romanovsky68e326d2019-04-03 16:42:43 +03006461 INIT_RDMA_OBJ_SIZE(ib_srq, mlx5_ib_srq, ibsrq),
Leon Romanovskya2a074e2019-02-12 20:39:16 +02006462 INIT_RDMA_OBJ_SIZE(ib_ucontext, mlx5_ib_ucontext, ibucontext),
Kamal Heib96458233e2018-12-10 21:09:38 +02006463};
6464
6465static const struct ib_device_ops mlx5_ib_dev_flow_ipsec_ops = {
6466 .create_flow_action_esp = mlx5_ib_create_flow_action_esp,
6467 .modify_flow_action_esp = mlx5_ib_modify_flow_action_esp,
6468};
6469
6470static const struct ib_device_ops mlx5_ib_dev_ipoib_enhanced_ops = {
6471 .rdma_netdev_get_params = mlx5_ib_rn_get_params,
6472};
6473
6474static const struct ib_device_ops mlx5_ib_dev_sriov_ops = {
6475 .get_vf_config = mlx5_ib_get_vf_config,
Danit Goldberg9c0015e2019-11-06 15:18:12 +02006476 .get_vf_guid = mlx5_ib_get_vf_guid,
Kamal Heib96458233e2018-12-10 21:09:38 +02006477 .get_vf_stats = mlx5_ib_get_vf_stats,
6478 .set_vf_guid = mlx5_ib_set_vf_guid,
6479 .set_vf_link_state = mlx5_ib_set_vf_link_state,
6480};
6481
6482static const struct ib_device_ops mlx5_ib_dev_mw_ops = {
6483 .alloc_mw = mlx5_ib_alloc_mw,
6484 .dealloc_mw = mlx5_ib_dealloc_mw,
6485};
6486
6487static const struct ib_device_ops mlx5_ib_dev_xrc_ops = {
6488 .alloc_xrcd = mlx5_ib_alloc_xrcd,
6489 .dealloc_xrcd = mlx5_ib_dealloc_xrcd,
6490};
6491
6492static const struct ib_device_ops mlx5_ib_dev_dm_ops = {
6493 .alloc_dm = mlx5_ib_alloc_dm,
6494 .dealloc_dm = mlx5_ib_dealloc_dm,
6495 .reg_dm_mr = mlx5_ib_reg_dm_mr,
6496};
6497
Yishai Hadasf164be82019-12-12 13:09:26 +02006498static int mlx5_ib_init_var_table(struct mlx5_ib_dev *dev)
6499{
6500 struct mlx5_core_dev *mdev = dev->mdev;
6501 struct mlx5_var_table *var_table = &dev->var_table;
6502 u8 log_doorbell_bar_size;
6503 u8 log_doorbell_stride;
6504 u64 bar_size;
6505
6506 log_doorbell_bar_size = MLX5_CAP_DEV_VDPA_EMULATION(mdev,
6507 log_doorbell_bar_size);
6508 log_doorbell_stride = MLX5_CAP_DEV_VDPA_EMULATION(mdev,
6509 log_doorbell_stride);
6510 var_table->hw_start_addr = dev->mdev->bar_addr +
6511 MLX5_CAP64_DEV_VDPA_EMULATION(mdev,
6512 doorbell_bar_offset);
6513 bar_size = (1ULL << log_doorbell_bar_size) * 4096;
6514 var_table->stride_size = 1ULL << log_doorbell_stride;
6515 var_table->num_var_hw_entries = bar_size / var_table->stride_size;
6516 mutex_init(&var_table->bitmap_lock);
6517 var_table->bitmap = bitmap_zalloc(var_table->num_var_hw_entries,
6518 GFP_KERNEL);
6519 return (var_table->bitmap) ? 0 : -ENOMEM;
6520}
6521
6522static void mlx5_ib_stage_caps_cleanup(struct mlx5_ib_dev *dev)
6523{
6524 bitmap_free(dev->var_table.bitmap);
6525}
6526
Mark Blochfb652d32019-03-28 15:27:42 +02006527static int mlx5_ib_stage_caps_init(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02006528{
6529 struct mlx5_core_dev *mdev = dev->mdev;
Mark Bloch16c19752018-01-01 13:06:58 +02006530 int err;
6531
Eli Cohene126ba92013-07-07 17:25:49 +03006532 dev->ib_dev.uverbs_cmd_mask =
6533 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
6534 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
6535 (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
6536 (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
6537 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
Moni Shoua41c450f2016-11-23 08:23:26 +02006538 (1ull << IB_USER_VERBS_CMD_CREATE_AH) |
6539 (1ull << IB_USER_VERBS_CMD_DESTROY_AH) |
Eli Cohene126ba92013-07-07 17:25:49 +03006540 (1ull << IB_USER_VERBS_CMD_REG_MR) |
Noa Osherovich56e11d62016-02-29 16:46:51 +02006541 (1ull << IB_USER_VERBS_CMD_REREG_MR) |
Eli Cohene126ba92013-07-07 17:25:49 +03006542 (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
6543 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
6544 (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
6545 (1ull << IB_USER_VERBS_CMD_RESIZE_CQ) |
6546 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
6547 (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
6548 (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
6549 (1ull << IB_USER_VERBS_CMD_QUERY_QP) |
6550 (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
6551 (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST) |
6552 (1ull << IB_USER_VERBS_CMD_DETACH_MCAST) |
6553 (1ull << IB_USER_VERBS_CMD_CREATE_SRQ) |
6554 (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ) |
6555 (1ull << IB_USER_VERBS_CMD_QUERY_SRQ) |
6556 (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ) |
6557 (1ull << IB_USER_VERBS_CMD_CREATE_XSRQ) |
6558 (1ull << IB_USER_VERBS_CMD_OPEN_QP);
Haggai Eran1707cb42015-02-08 13:28:52 +02006559 dev->ib_dev.uverbs_ex_cmd_mask =
Matan Barakd4584dd2016-01-28 17:51:46 +02006560 (1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE) |
6561 (1ull << IB_USER_VERBS_EX_CMD_CREATE_CQ) |
Bodong Wang7d29f342016-12-01 13:43:16 +02006562 (1ull << IB_USER_VERBS_EX_CMD_CREATE_QP) |
Yonatan Cohenb0e9df62017-11-13 10:51:15 +02006563 (1ull << IB_USER_VERBS_EX_CMD_MODIFY_QP) |
Kamal Heib96458233e2018-12-10 21:09:38 +02006564 (1ull << IB_USER_VERBS_EX_CMD_MODIFY_CQ) |
6565 (1ull << IB_USER_VERBS_EX_CMD_CREATE_FLOW) |
6566 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_FLOW);
Eli Cohene126ba92013-07-07 17:25:49 +03006567
Denis Drozdovf6a8a192018-08-14 14:08:51 +03006568 if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads) &&
6569 IS_ENABLED(CONFIG_MLX5_CORE_IPOIB))
Kamal Heib96458233e2018-12-10 21:09:38 +02006570 ib_set_device_ops(&dev->ib_dev,
6571 &mlx5_ib_dev_ipoib_enhanced_ops);
Niranjana Vishwanathapura8e959602017-06-30 13:14:46 -07006572
Kamal Heib96458233e2018-12-10 21:09:38 +02006573 if (mlx5_core_is_pf(mdev))
6574 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_sriov_ops);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03006575
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03006576 dev->umr_fence = mlx5_get_umr_fence(MLX5_CAP_GEN(mdev, umr_fence));
6577
Matan Barakd2370e02016-02-29 18:05:30 +02006578 if (MLX5_CAP_GEN(mdev, imaicl)) {
Matan Barakd2370e02016-02-29 18:05:30 +02006579 dev->ib_dev.uverbs_cmd_mask |=
6580 (1ull << IB_USER_VERBS_CMD_ALLOC_MW) |
6581 (1ull << IB_USER_VERBS_CMD_DEALLOC_MW);
Kamal Heib96458233e2018-12-10 21:09:38 +02006582 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_mw_ops);
Matan Barakd2370e02016-02-29 18:05:30 +02006583 }
6584
Saeed Mahameed938fe832015-05-28 22:28:41 +03006585 if (MLX5_CAP_GEN(mdev, xrc)) {
Eli Cohene126ba92013-07-07 17:25:49 +03006586 dev->ib_dev.uverbs_cmd_mask |=
6587 (1ull << IB_USER_VERBS_CMD_OPEN_XRCD) |
6588 (1ull << IB_USER_VERBS_CMD_CLOSE_XRCD);
Kamal Heib96458233e2018-12-10 21:09:38 +02006589 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_xrc_ops);
Eli Cohene126ba92013-07-07 17:25:49 +03006590 }
6591
Ariel Levkovich25c13322019-05-05 17:07:13 +03006592 if (MLX5_CAP_DEV_MEM(mdev, memic) ||
6593 MLX5_CAP_GEN_64(dev->mdev, general_obj_types) &
6594 MLX5_GENERAL_OBJ_TYPES_CAP_SW_ICM)
Kamal Heib96458233e2018-12-10 21:09:38 +02006595 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_dm_ops);
Ariel Levkovich24da0012018-04-05 18:53:27 +03006596
Jason Gunthorpedfb631a2018-11-12 22:59:49 +02006597 if (mlx5_accel_ipsec_device_caps(dev->mdev) &
Kamal Heib96458233e2018-12-10 21:09:38 +02006598 MLX5_ACCEL_IPSEC_CAP_DEVICE)
6599 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_flow_ipsec_ops);
Kamal Heib96458233e2018-12-10 21:09:38 +02006600 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_ops);
Yishai Hadas81e30882017-06-08 16:15:09 +03006601
Jason Gunthorpe36e235c2018-11-12 22:59:53 +02006602 if (IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS))
6603 dev->ib_dev.driver_def = mlx5_ib_defs;
Eli Cohene126ba92013-07-07 17:25:49 +03006604
6605 err = init_node_data(dev);
6606 if (err)
Mark Bloch16c19752018-01-01 13:06:58 +02006607 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03006608
Mark Blochc8b89922018-01-01 13:07:02 +02006609 if ((MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_ETH) &&
Jason Gunthorpee7996a92018-01-29 13:26:40 -07006610 (MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) ||
6611 MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
Mark Blocha560f1d2018-09-17 13:30:47 +03006612 mutex_init(&dev->lb.mutex);
Mark Blochc8b89922018-01-01 13:07:02 +02006613
Yishai Hadasf164be82019-12-12 13:09:26 +02006614 if (MLX5_CAP_GEN_64(dev->mdev, general_obj_types) &
6615 MLX5_GENERAL_OBJ_TYPES_CAP_VIRTIO_NET_Q) {
6616 err = mlx5_ib_init_var_table(dev);
6617 if (err)
6618 return err;
6619 }
6620
Leon Romanovsky96e2fd72019-07-08 13:59:05 +03006621 dev->ib_dev.use_cq_dim = true;
6622
Mark Bloch16c19752018-01-01 13:06:58 +02006623 return 0;
6624}
6625
Kamal Heib96458233e2018-12-10 21:09:38 +02006626static const struct ib_device_ops mlx5_ib_dev_port_ops = {
6627 .get_port_immutable = mlx5_port_immutable,
6628 .query_port = mlx5_ib_query_port,
6629};
6630
Mark Bloch8e6efa32017-11-06 12:22:13 +00006631static int mlx5_ib_stage_non_default_cb(struct mlx5_ib_dev *dev)
6632{
Kamal Heib96458233e2018-12-10 21:09:38 +02006633 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_port_ops);
Mark Bloch8e6efa32017-11-06 12:22:13 +00006634 return 0;
6635}
6636
Kamal Heib96458233e2018-12-10 21:09:38 +02006637static const struct ib_device_ops mlx5_ib_dev_port_rep_ops = {
6638 .get_port_immutable = mlx5_port_rep_immutable,
6639 .query_port = mlx5_ib_rep_query_port,
6640};
6641
Michael Guralnikb5a498b2019-11-08 23:45:26 +00006642static int mlx5_ib_stage_raw_eth_non_default_cb(struct mlx5_ib_dev *dev)
Mark Bloch8e6efa32017-11-06 12:22:13 +00006643{
Kamal Heib96458233e2018-12-10 21:09:38 +02006644 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_port_rep_ops);
Mark Bloch8e6efa32017-11-06 12:22:13 +00006645 return 0;
6646}
6647
Kamal Heib96458233e2018-12-10 21:09:38 +02006648static const struct ib_device_ops mlx5_ib_dev_common_roce_ops = {
6649 .create_rwq_ind_table = mlx5_ib_create_rwq_ind_table,
6650 .create_wq = mlx5_ib_create_wq,
6651 .destroy_rwq_ind_table = mlx5_ib_destroy_rwq_ind_table,
6652 .destroy_wq = mlx5_ib_destroy_wq,
6653 .get_netdev = mlx5_ib_get_netdev,
6654 .modify_wq = mlx5_ib_modify_wq,
6655};
6656
Leon Romanovskye3f1ed12018-07-08 12:55:43 +03006657static int mlx5_ib_stage_common_roce_init(struct mlx5_ib_dev *dev)
Mark Bloch8e6efa32017-11-06 12:22:13 +00006658{
Leon Romanovskye3f1ed12018-07-08 12:55:43 +03006659 u8 port_num;
Mark Bloch8e6efa32017-11-06 12:22:13 +00006660
Mark Bloch8e6efa32017-11-06 12:22:13 +00006661 dev->ib_dev.uverbs_ex_cmd_mask |=
6662 (1ull << IB_USER_VERBS_EX_CMD_CREATE_WQ) |
6663 (1ull << IB_USER_VERBS_EX_CMD_MODIFY_WQ) |
6664 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_WQ) |
6665 (1ull << IB_USER_VERBS_EX_CMD_CREATE_RWQ_IND_TBL) |
6666 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_RWQ_IND_TBL);
Kamal Heib96458233e2018-12-10 21:09:38 +02006667 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_common_roce_ops);
Mark Bloch8e6efa32017-11-06 12:22:13 +00006668
Leon Romanovskye3f1ed12018-07-08 12:55:43 +03006669 port_num = mlx5_core_native_port_num(dev->mdev) - 1;
6670
Mark Bloch26628e22019-03-28 15:27:41 +02006671 /* Register only for native ports */
Mark Bloch8e6efa32017-11-06 12:22:13 +00006672 return mlx5_add_netdev_notifier(dev, port_num);
6673}
6674
6675static void mlx5_ib_stage_common_roce_cleanup(struct mlx5_ib_dev *dev)
6676{
6677 u8 port_num = mlx5_core_native_port_num(dev->mdev) - 1;
6678
6679 mlx5_remove_netdev_notifier(dev, port_num);
6680}
6681
Michael Guralnikb5a498b2019-11-08 23:45:26 +00006682static int mlx5_ib_stage_raw_eth_roce_init(struct mlx5_ib_dev *dev)
Mark Bloch8e6efa32017-11-06 12:22:13 +00006683{
6684 struct mlx5_core_dev *mdev = dev->mdev;
6685 enum rdma_link_layer ll;
6686 int port_type_cap;
6687 int err = 0;
Mark Bloch8e6efa32017-11-06 12:22:13 +00006688
Mark Bloch8e6efa32017-11-06 12:22:13 +00006689 port_type_cap = MLX5_CAP_GEN(mdev, port_type);
6690 ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
6691
6692 if (ll == IB_LINK_LAYER_ETHERNET)
Leon Romanovskye3f1ed12018-07-08 12:55:43 +03006693 err = mlx5_ib_stage_common_roce_init(dev);
Mark Bloch8e6efa32017-11-06 12:22:13 +00006694
6695 return err;
6696}
6697
Michael Guralnikb5a498b2019-11-08 23:45:26 +00006698static void mlx5_ib_stage_raw_eth_roce_cleanup(struct mlx5_ib_dev *dev)
Mark Bloch8e6efa32017-11-06 12:22:13 +00006699{
6700 mlx5_ib_stage_common_roce_cleanup(dev);
6701}
6702
Mark Bloch16c19752018-01-01 13:06:58 +02006703static int mlx5_ib_stage_roce_init(struct mlx5_ib_dev *dev)
6704{
6705 struct mlx5_core_dev *mdev = dev->mdev;
6706 enum rdma_link_layer ll;
6707 int port_type_cap;
6708 int err;
6709
6710 port_type_cap = MLX5_CAP_GEN(mdev, port_type);
6711 ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
6712
Achiad Shochatfc24fc52015-12-23 18:47:17 +02006713 if (ll == IB_LINK_LAYER_ETHERNET) {
Leon Romanovskye3f1ed12018-07-08 12:55:43 +03006714 err = mlx5_ib_stage_common_roce_init(dev);
Achiad Shochatfc24fc52015-12-23 18:47:17 +02006715 if (err)
Mark Bloch16c19752018-01-01 13:06:58 +02006716 return err;
Mark Bloch8e6efa32017-11-06 12:22:13 +00006717
Leon Romanovskye3f1ed12018-07-08 12:55:43 +03006718 err = mlx5_enable_eth(dev);
Mark Bloch8e6efa32017-11-06 12:22:13 +00006719 if (err)
6720 goto cleanup;
Achiad Shochatfc24fc52015-12-23 18:47:17 +02006721 }
6722
Mark Bloch16c19752018-01-01 13:06:58 +02006723 return 0;
Mark Bloch8e6efa32017-11-06 12:22:13 +00006724cleanup:
6725 mlx5_ib_stage_common_roce_cleanup(dev);
6726
6727 return err;
Mark Bloch16c19752018-01-01 13:06:58 +02006728}
Eli Cohene126ba92013-07-07 17:25:49 +03006729
Mark Bloch16c19752018-01-01 13:06:58 +02006730static void mlx5_ib_stage_roce_cleanup(struct mlx5_ib_dev *dev)
6731{
6732 struct mlx5_core_dev *mdev = dev->mdev;
6733 enum rdma_link_layer ll;
6734 int port_type_cap;
Eli Cohene126ba92013-07-07 17:25:49 +03006735
Mark Bloch16c19752018-01-01 13:06:58 +02006736 port_type_cap = MLX5_CAP_GEN(mdev, port_type);
6737 ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
6738
6739 if (ll == IB_LINK_LAYER_ETHERNET) {
6740 mlx5_disable_eth(dev);
Mark Bloch8e6efa32017-11-06 12:22:13 +00006741 mlx5_ib_stage_common_roce_cleanup(dev);
Kamal Heib45bded22017-01-18 14:10:32 +02006742 }
Mark Bloch16c19752018-01-01 13:06:58 +02006743}
Haggai Eran6aec21f2014-12-11 17:04:23 +02006744
Mark Blochfb652d32019-03-28 15:27:42 +02006745static int mlx5_ib_stage_dev_res_init(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02006746{
6747 return create_dev_resources(&dev->devr);
6748}
Parav Pandit4a2da0b2017-05-30 10:05:15 +03006749
Mark Blochfb652d32019-03-28 15:27:42 +02006750static void mlx5_ib_stage_dev_res_cleanup(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02006751{
6752 destroy_dev_resources(&dev->devr);
6753}
6754
6755static int mlx5_ib_stage_odp_init(struct mlx5_ib_dev *dev)
6756{
6757 return mlx5_ib_odp_init_one(dev);
6758}
6759
Kamal Heibf3ffed02019-01-30 16:13:42 +02006760static void mlx5_ib_stage_odp_cleanup(struct mlx5_ib_dev *dev)
Saeed Mahameedd5d284b2018-11-19 10:52:41 -08006761{
6762 mlx5_ib_odp_cleanup_one(dev);
6763}
6764
Kamal Heib96458233e2018-12-10 21:09:38 +02006765static const struct ib_device_ops mlx5_ib_dev_hw_stats_ops = {
6766 .alloc_hw_stats = mlx5_ib_alloc_hw_stats,
6767 .get_hw_stats = mlx5_ib_get_hw_stats,
Mark Zhang45842fc2019-07-02 13:02:38 +03006768 .counter_bind_qp = mlx5_ib_counter_bind_qp,
6769 .counter_unbind_qp = mlx5_ib_counter_unbind_qp,
6770 .counter_dealloc = mlx5_ib_counter_dealloc,
Mark Zhang18d422c2019-07-02 13:02:41 +03006771 .counter_alloc_stats = mlx5_ib_counter_alloc_stats,
6772 .counter_update_stats = mlx5_ib_counter_update_stats,
Kamal Heib96458233e2018-12-10 21:09:38 +02006773};
6774
Mark Blochfb652d32019-03-28 15:27:42 +02006775static int mlx5_ib_stage_counters_init(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02006776{
Mark Bloch5e1e7612018-01-01 13:07:01 +02006777 if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt)) {
Kamal Heib96458233e2018-12-10 21:09:38 +02006778 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_hw_stats_ops);
Mark Bloch5e1e7612018-01-01 13:07:01 +02006779
6780 return mlx5_ib_alloc_counters(dev);
6781 }
Mark Bloch16c19752018-01-01 13:06:58 +02006782
6783 return 0;
6784}
6785
Mark Blochfb652d32019-03-28 15:27:42 +02006786static void mlx5_ib_stage_counters_cleanup(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02006787{
6788 if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt))
6789 mlx5_ib_dealloc_counters(dev);
6790}
6791
6792static int mlx5_ib_stage_cong_debugfs_init(struct mlx5_ib_dev *dev)
6793{
Greg Kroah-Hartman73eb8f02019-01-22 16:17:57 +01006794 mlx5_ib_init_cong_debugfs(dev,
6795 mlx5_core_native_port_num(dev->mdev) - 1);
6796 return 0;
Mark Bloch16c19752018-01-01 13:06:58 +02006797}
6798
6799static void mlx5_ib_stage_cong_debugfs_cleanup(struct mlx5_ib_dev *dev)
6800{
Parav Pandita9e546e2018-01-04 17:25:39 +02006801 mlx5_ib_cleanup_cong_debugfs(dev,
6802 mlx5_core_native_port_num(dev->mdev) - 1);
Mark Bloch16c19752018-01-01 13:06:58 +02006803}
6804
6805static int mlx5_ib_stage_uar_init(struct mlx5_ib_dev *dev)
6806{
Eli Cohen5fe9dec2017-01-03 23:55:25 +02006807 dev->mdev->priv.uar = mlx5_get_uars_page(dev->mdev);
Leon Romanovsky444261c2018-04-23 17:01:56 +03006808 return PTR_ERR_OR_ZERO(dev->mdev->priv.uar);
Mark Bloch16c19752018-01-01 13:06:58 +02006809}
6810
6811static void mlx5_ib_stage_uar_cleanup(struct mlx5_ib_dev *dev)
6812{
6813 mlx5_put_uars_page(dev->mdev, dev->mdev->priv.uar);
6814}
6815
Mark Blochfb652d32019-03-28 15:27:42 +02006816static int mlx5_ib_stage_bfrag_init(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02006817{
6818 int err;
Eli Cohen5fe9dec2017-01-03 23:55:25 +02006819
6820 err = mlx5_alloc_bfreg(dev->mdev, &dev->bfreg, false, false);
6821 if (err)
Mark Bloch16c19752018-01-01 13:06:58 +02006822 return err;
Eli Cohen5fe9dec2017-01-03 23:55:25 +02006823
6824 err = mlx5_alloc_bfreg(dev->mdev, &dev->fp_bfreg, false, true);
6825 if (err)
Mark Bloch16c19752018-01-01 13:06:58 +02006826 mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
Eli Cohen5fe9dec2017-01-03 23:55:25 +02006827
Mark Bloch16c19752018-01-01 13:06:58 +02006828 return err;
6829}
Mark Bloch0837e862016-06-17 15:10:55 +03006830
Mark Blochfb652d32019-03-28 15:27:42 +02006831static void mlx5_ib_stage_bfrag_cleanup(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02006832{
6833 mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
6834 mlx5_free_bfreg(dev->mdev, &dev->bfreg);
6835}
Eli Cohene126ba92013-07-07 17:25:49 +03006836
Mark Blochfb652d32019-03-28 15:27:42 +02006837static int mlx5_ib_stage_ib_reg_init(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02006838{
Jason Gunthorpee349f852018-09-25 16:58:09 -06006839 const char *name;
6840
Parav Pandit508a5232018-10-11 22:31:54 +03006841 rdma_set_device_sysfs_group(&dev->ib_dev, &mlx5_attr_group);
Aviv Heller7c34ec12018-08-23 13:47:53 +03006842 if (!mlx5_lag_is_roce(dev->mdev))
Jason Gunthorpee349f852018-09-25 16:58:09 -06006843 name = "mlx5_%d";
6844 else
6845 name = "mlx5_bond_%d";
Parav Panditea4baf72018-12-18 14:28:30 +02006846 return ib_register_device(&dev->ib_dev, name);
Mark Bloch16c19752018-01-01 13:06:58 +02006847}
6848
Mark Blochfb652d32019-03-28 15:27:42 +02006849static void mlx5_ib_stage_pre_ib_reg_umr_cleanup(struct mlx5_ib_dev *dev)
Mark Bloch42cea832018-03-14 09:14:15 +02006850{
6851 destroy_umrc_res(dev);
6852}
6853
Mark Blochfb652d32019-03-28 15:27:42 +02006854static void mlx5_ib_stage_ib_reg_cleanup(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02006855{
6856 ib_unregister_device(&dev->ib_dev);
6857}
6858
Mark Blochfb652d32019-03-28 15:27:42 +02006859static int mlx5_ib_stage_post_ib_reg_umr_init(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02006860{
6861 return create_umr_res(dev);
6862}
6863
Mark Bloch16c19752018-01-01 13:06:58 +02006864static int mlx5_ib_stage_delay_drop_init(struct mlx5_ib_dev *dev)
6865{
Maor Gottlieb03404e82017-05-30 10:29:13 +03006866 init_delay_drop(dev);
6867
Mark Bloch16c19752018-01-01 13:06:58 +02006868 return 0;
6869}
6870
6871static void mlx5_ib_stage_delay_drop_cleanup(struct mlx5_ib_dev *dev)
6872{
6873 cancel_delay_drop(dev);
6874}
6875
Saeed Mahameeddf097a22018-11-26 14:39:00 -08006876static int mlx5_ib_stage_dev_notifier_init(struct mlx5_ib_dev *dev)
6877{
6878 dev->mdev_events.notifier_call = mlx5_ib_event;
6879 mlx5_notifier_register(dev->mdev, &dev->mdev_events);
6880 return 0;
6881}
6882
6883static void mlx5_ib_stage_dev_notifier_cleanup(struct mlx5_ib_dev *dev)
6884{
6885 mlx5_notifier_unregister(dev->mdev, &dev->mdev_events);
6886}
6887
Leon Romanovsky81773ce2018-11-28 20:53:39 +02006888static int mlx5_ib_stage_devx_init(struct mlx5_ib_dev *dev)
6889{
6890 int uid;
6891
Yishai Hadasfb981532018-11-26 08:28:36 +02006892 uid = mlx5_ib_devx_create(dev, false);
Yishai Hadase337dd52019-06-30 19:23:30 +03006893 if (uid > 0) {
Leon Romanovsky81773ce2018-11-28 20:53:39 +02006894 dev->devx_whitelist_uid = uid;
Yishai Hadase337dd52019-06-30 19:23:30 +03006895 mlx5_ib_devx_init_event_table(dev);
6896 }
Leon Romanovsky81773ce2018-11-28 20:53:39 +02006897
6898 return 0;
6899}
6900static void mlx5_ib_stage_devx_cleanup(struct mlx5_ib_dev *dev)
6901{
Yishai Hadase337dd52019-06-30 19:23:30 +03006902 if (dev->devx_whitelist_uid) {
6903 mlx5_ib_devx_cleanup_event_table(dev);
Leon Romanovsky81773ce2018-11-28 20:53:39 +02006904 mlx5_ib_devx_destroy(dev, dev->devx_whitelist_uid);
Yishai Hadase337dd52019-06-30 19:23:30 +03006905 }
Leon Romanovsky81773ce2018-11-28 20:53:39 +02006906}
6907
Michael Guralnik11f552e2019-06-10 15:21:24 +03006908int mlx5_ib_enable_driver(struct ib_device *dev)
6909{
6910 struct mlx5_ib_dev *mdev = to_mdev(dev);
6911 int ret;
6912
6913 ret = mlx5_ib_test_wc(mdev);
6914 mlx5_ib_dbg(mdev, "Write-Combining %s",
6915 mdev->wc_support ? "supported" : "not supported");
6916
6917 return ret;
6918}
6919
Mark Blochb5ca15a2018-01-23 11:16:30 +00006920void __mlx5_ib_remove(struct mlx5_ib_dev *dev,
6921 const struct mlx5_ib_profile *profile,
6922 int stage)
Mark Bloch16c19752018-01-01 13:06:58 +02006923{
Parav Pandit4cca96a2019-12-12 13:30:21 +02006924 dev->ib_active = false;
6925
Mark Bloch16c19752018-01-01 13:06:58 +02006926 /* Number of stages to cleanup */
6927 while (stage) {
6928 stage--;
6929 if (profile->stage[stage].cleanup)
6930 profile->stage[stage].cleanup(dev);
6931 }
Mark Bloch4a6dc852019-03-28 15:27:34 +02006932
Mark Blochda796cc2019-03-28 15:27:35 +02006933 kfree(dev->port);
Mark Bloch4a6dc852019-03-28 15:27:34 +02006934 ib_dealloc_device(&dev->ib_dev);
Mark Bloch16c19752018-01-01 13:06:58 +02006935}
6936
Mark Blochb5ca15a2018-01-23 11:16:30 +00006937void *__mlx5_ib_add(struct mlx5_ib_dev *dev,
6938 const struct mlx5_ib_profile *profile)
Mark Bloch16c19752018-01-01 13:06:58 +02006939{
Mark Bloch16c19752018-01-01 13:06:58 +02006940 int err;
6941 int i;
Mark Bloch16c19752018-01-01 13:06:58 +02006942
Mark Bloch16c19752018-01-01 13:06:58 +02006943 for (i = 0; i < MLX5_IB_STAGE_MAX; i++) {
6944 if (profile->stage[i].init) {
6945 err = profile->stage[i].init(dev);
6946 if (err)
6947 goto err_out;
6948 }
6949 }
6950
6951 dev->profile = profile;
Eli Cohene126ba92013-07-07 17:25:49 +03006952 dev->ib_active = true;
6953
Jack Morgenstein9603b612014-07-28 23:30:22 +03006954 return dev;
Eli Cohene126ba92013-07-07 17:25:49 +03006955
Mark Bloch16c19752018-01-01 13:06:58 +02006956err_out:
6957 __mlx5_ib_remove(dev, profile, i);
Eli Cohene126ba92013-07-07 17:25:49 +03006958
Jack Morgenstein9603b612014-07-28 23:30:22 +03006959 return NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03006960}
6961
Mark Bloch16c19752018-01-01 13:06:58 +02006962static const struct mlx5_ib_profile pf_profile = {
6963 STAGE_CREATE(MLX5_IB_STAGE_INIT,
6964 mlx5_ib_stage_init_init,
6965 mlx5_ib_stage_init_cleanup),
Mark Bloch9a4ca382018-01-16 14:42:35 +00006966 STAGE_CREATE(MLX5_IB_STAGE_FLOW_DB,
6967 mlx5_ib_stage_flow_db_init,
6968 mlx5_ib_stage_flow_db_cleanup),
Mark Bloch16c19752018-01-01 13:06:58 +02006969 STAGE_CREATE(MLX5_IB_STAGE_CAPS,
6970 mlx5_ib_stage_caps_init,
Yishai Hadasf164be82019-12-12 13:09:26 +02006971 mlx5_ib_stage_caps_cleanup),
Mark Bloch8e6efa32017-11-06 12:22:13 +00006972 STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB,
6973 mlx5_ib_stage_non_default_cb,
6974 NULL),
Mark Bloch16c19752018-01-01 13:06:58 +02006975 STAGE_CREATE(MLX5_IB_STAGE_ROCE,
6976 mlx5_ib_stage_roce_init,
6977 mlx5_ib_stage_roce_cleanup),
Leon Romanovskyf3da6572018-11-28 20:53:41 +02006978 STAGE_CREATE(MLX5_IB_STAGE_SRQ,
6979 mlx5_init_srq_table,
6980 mlx5_cleanup_srq_table),
Mark Bloch16c19752018-01-01 13:06:58 +02006981 STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES,
6982 mlx5_ib_stage_dev_res_init,
6983 mlx5_ib_stage_dev_res_cleanup),
Saeed Mahameeddf097a22018-11-26 14:39:00 -08006984 STAGE_CREATE(MLX5_IB_STAGE_DEVICE_NOTIFIER,
6985 mlx5_ib_stage_dev_notifier_init,
6986 mlx5_ib_stage_dev_notifier_cleanup),
Mark Bloch16c19752018-01-01 13:06:58 +02006987 STAGE_CREATE(MLX5_IB_STAGE_ODP,
6988 mlx5_ib_stage_odp_init,
Saeed Mahameedd5d284b2018-11-19 10:52:41 -08006989 mlx5_ib_stage_odp_cleanup),
Mark Bloch16c19752018-01-01 13:06:58 +02006990 STAGE_CREATE(MLX5_IB_STAGE_COUNTERS,
6991 mlx5_ib_stage_counters_init,
6992 mlx5_ib_stage_counters_cleanup),
6993 STAGE_CREATE(MLX5_IB_STAGE_CONG_DEBUGFS,
6994 mlx5_ib_stage_cong_debugfs_init,
6995 mlx5_ib_stage_cong_debugfs_cleanup),
6996 STAGE_CREATE(MLX5_IB_STAGE_UAR,
6997 mlx5_ib_stage_uar_init,
6998 mlx5_ib_stage_uar_cleanup),
6999 STAGE_CREATE(MLX5_IB_STAGE_BFREG,
7000 mlx5_ib_stage_bfrag_init,
7001 mlx5_ib_stage_bfrag_cleanup),
Mark Bloch42cea832018-03-14 09:14:15 +02007002 STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR,
7003 NULL,
7004 mlx5_ib_stage_pre_ib_reg_umr_cleanup),
Leon Romanovsky81773ce2018-11-28 20:53:39 +02007005 STAGE_CREATE(MLX5_IB_STAGE_WHITELIST_UID,
7006 mlx5_ib_stage_devx_init,
7007 mlx5_ib_stage_devx_cleanup),
Mark Bloch16c19752018-01-01 13:06:58 +02007008 STAGE_CREATE(MLX5_IB_STAGE_IB_REG,
7009 mlx5_ib_stage_ib_reg_init,
7010 mlx5_ib_stage_ib_reg_cleanup),
Mark Bloch42cea832018-03-14 09:14:15 +02007011 STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR,
7012 mlx5_ib_stage_post_ib_reg_umr_init,
7013 NULL),
Mark Bloch16c19752018-01-01 13:06:58 +02007014 STAGE_CREATE(MLX5_IB_STAGE_DELAY_DROP,
7015 mlx5_ib_stage_delay_drop_init,
7016 mlx5_ib_stage_delay_drop_cleanup),
Mark Bloch16c19752018-01-01 13:06:58 +02007017};
7018
Michael Guralnikb5a498b2019-11-08 23:45:26 +00007019const struct mlx5_ib_profile raw_eth_profile = {
Mark Blochb5ca15a2018-01-23 11:16:30 +00007020 STAGE_CREATE(MLX5_IB_STAGE_INIT,
7021 mlx5_ib_stage_init_init,
7022 mlx5_ib_stage_init_cleanup),
7023 STAGE_CREATE(MLX5_IB_STAGE_FLOW_DB,
7024 mlx5_ib_stage_flow_db_init,
7025 mlx5_ib_stage_flow_db_cleanup),
7026 STAGE_CREATE(MLX5_IB_STAGE_CAPS,
7027 mlx5_ib_stage_caps_init,
Yishai Hadasf164be82019-12-12 13:09:26 +02007028 mlx5_ib_stage_caps_cleanup),
Mark Blochb5ca15a2018-01-23 11:16:30 +00007029 STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB,
Michael Guralnikb5a498b2019-11-08 23:45:26 +00007030 mlx5_ib_stage_raw_eth_non_default_cb,
Mark Blochb5ca15a2018-01-23 11:16:30 +00007031 NULL),
7032 STAGE_CREATE(MLX5_IB_STAGE_ROCE,
Michael Guralnikb5a498b2019-11-08 23:45:26 +00007033 mlx5_ib_stage_raw_eth_roce_init,
7034 mlx5_ib_stage_raw_eth_roce_cleanup),
Leon Romanovskyf3da6572018-11-28 20:53:41 +02007035 STAGE_CREATE(MLX5_IB_STAGE_SRQ,
7036 mlx5_init_srq_table,
7037 mlx5_cleanup_srq_table),
Mark Blochb5ca15a2018-01-23 11:16:30 +00007038 STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES,
7039 mlx5_ib_stage_dev_res_init,
7040 mlx5_ib_stage_dev_res_cleanup),
Saeed Mahameeddf097a22018-11-26 14:39:00 -08007041 STAGE_CREATE(MLX5_IB_STAGE_DEVICE_NOTIFIER,
7042 mlx5_ib_stage_dev_notifier_init,
7043 mlx5_ib_stage_dev_notifier_cleanup),
Mark Blochb5ca15a2018-01-23 11:16:30 +00007044 STAGE_CREATE(MLX5_IB_STAGE_COUNTERS,
7045 mlx5_ib_stage_counters_init,
7046 mlx5_ib_stage_counters_cleanup),
7047 STAGE_CREATE(MLX5_IB_STAGE_UAR,
7048 mlx5_ib_stage_uar_init,
7049 mlx5_ib_stage_uar_cleanup),
7050 STAGE_CREATE(MLX5_IB_STAGE_BFREG,
7051 mlx5_ib_stage_bfrag_init,
7052 mlx5_ib_stage_bfrag_cleanup),
David S. Miller03fe2de2018-03-23 11:24:57 -04007053 STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR,
7054 NULL,
7055 mlx5_ib_stage_pre_ib_reg_umr_cleanup),
Mark Bloch7f575102019-03-28 15:46:25 +02007056 STAGE_CREATE(MLX5_IB_STAGE_WHITELIST_UID,
7057 mlx5_ib_stage_devx_init,
7058 mlx5_ib_stage_devx_cleanup),
Mark Blochb5ca15a2018-01-23 11:16:30 +00007059 STAGE_CREATE(MLX5_IB_STAGE_IB_REG,
7060 mlx5_ib_stage_ib_reg_init,
7061 mlx5_ib_stage_ib_reg_cleanup),
David S. Miller03fe2de2018-03-23 11:24:57 -04007062 STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR,
7063 mlx5_ib_stage_post_ib_reg_umr_init,
7064 NULL),
Mark Blochb5ca15a2018-01-23 11:16:30 +00007065};
7066
Leon Romanovskye3f1ed12018-07-08 12:55:43 +03007067static void *mlx5_ib_add_slave_port(struct mlx5_core_dev *mdev)
Daniel Jurgens32f69e42018-01-04 17:25:36 +02007068{
7069 struct mlx5_ib_multiport_info *mpi;
7070 struct mlx5_ib_dev *dev;
7071 bool bound = false;
7072 int err;
7073
7074 mpi = kzalloc(sizeof(*mpi), GFP_KERNEL);
7075 if (!mpi)
7076 return NULL;
7077
7078 mpi->mdev = mdev;
7079
7080 err = mlx5_query_nic_vport_system_image_guid(mdev,
7081 &mpi->sys_image_guid);
7082 if (err) {
7083 kfree(mpi);
7084 return NULL;
7085 }
7086
7087 mutex_lock(&mlx5_ib_multiport_mutex);
7088 list_for_each_entry(dev, &mlx5_ib_dev_list, ib_dev_list) {
7089 if (dev->sys_image_guid == mpi->sys_image_guid)
7090 bound = mlx5_ib_bind_slave_port(dev, mpi);
7091
7092 if (bound) {
7093 rdma_roce_rescan_device(&dev->ib_dev);
7094 break;
7095 }
7096 }
7097
7098 if (!bound) {
7099 list_add_tail(&mpi->list, &mlx5_ib_unaffiliated_port_list);
Vu Phamc42260f12019-04-29 18:14:05 +00007100 dev_dbg(mdev->device,
7101 "no suitable IB device found to bind to, added to unaffiliated list.\n");
Daniel Jurgens32f69e42018-01-04 17:25:36 +02007102 }
7103 mutex_unlock(&mlx5_ib_multiport_mutex);
7104
7105 return mpi;
7106}
7107
Mark Bloch16c19752018-01-01 13:06:58 +02007108static void *mlx5_ib_add(struct mlx5_core_dev *mdev)
7109{
Michael Guralnik94de8792019-11-08 23:45:28 +00007110 const struct mlx5_ib_profile *profile;
Daniel Jurgens32f69e42018-01-04 17:25:36 +02007111 enum rdma_link_layer ll;
Mark Blochb5ca15a2018-01-23 11:16:30 +00007112 struct mlx5_ib_dev *dev;
Daniel Jurgens32f69e42018-01-04 17:25:36 +02007113 int port_type_cap;
Mark Blochda796cc2019-03-28 15:27:35 +02007114 int num_ports;
Daniel Jurgens32f69e42018-01-04 17:25:36 +02007115
Mark Blochb5ca15a2018-01-23 11:16:30 +00007116 printk_once(KERN_INFO "%s", mlx5_version);
7117
Bodong Wangf0666f12019-02-12 22:55:34 -08007118 if (MLX5_ESWITCH_MANAGER(mdev) &&
Bodong Wangf6455de2019-06-28 22:36:15 +00007119 mlx5_ib_eswitch_mode(mdev->priv.eswitch) == MLX5_ESWITCH_OFFLOADS) {
Mark Bloch5fb58c92019-03-28 15:46:27 +02007120 if (!mlx5_core_mp_enabled(mdev))
7121 mlx5_ib_register_vport_reps(mdev);
Bodong Wangf0666f12019-02-12 22:55:34 -08007122 return mdev;
7123 }
7124
Daniel Jurgens32f69e42018-01-04 17:25:36 +02007125 port_type_cap = MLX5_CAP_GEN(mdev, port_type);
7126 ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
7127
Leon Romanovskye3f1ed12018-07-08 12:55:43 +03007128 if (mlx5_core_is_mp_slave(mdev) && ll == IB_LINK_LAYER_ETHERNET)
7129 return mlx5_ib_add_slave_port(mdev);
Daniel Jurgens32f69e42018-01-04 17:25:36 +02007130
Mark Blochda796cc2019-03-28 15:27:35 +02007131 num_ports = max(MLX5_CAP_GEN(mdev, num_ports),
7132 MLX5_CAP_GEN(mdev, num_vhca_ports));
Leon Romanovsky459cc692019-01-30 12:49:11 +02007133 dev = ib_alloc_device(mlx5_ib_dev, ib_dev);
Mark Blochb5ca15a2018-01-23 11:16:30 +00007134 if (!dev)
7135 return NULL;
Mark Blochda796cc2019-03-28 15:27:35 +02007136 dev->port = kcalloc(num_ports, sizeof(*dev->port),
7137 GFP_KERNEL);
7138 if (!dev->port) {
Parav Pandita5c9c292019-07-23 09:57:31 +03007139 ib_dealloc_device(&dev->ib_dev);
Mark Blochda796cc2019-03-28 15:27:35 +02007140 return NULL;
7141 }
Mark Blochb5ca15a2018-01-23 11:16:30 +00007142
7143 dev->mdev = mdev;
Mark Blochda796cc2019-03-28 15:27:35 +02007144 dev->num_ports = num_ports;
Mark Blochb5ca15a2018-01-23 11:16:30 +00007145
Michael Guralnik94de8792019-11-08 23:45:28 +00007146 if (ll == IB_LINK_LAYER_ETHERNET && !mlx5_is_roce_enabled(mdev))
7147 profile = &raw_eth_profile;
7148 else
7149 profile = &pf_profile;
7150
7151 return __mlx5_ib_add(dev, profile);
Mark Bloch16c19752018-01-01 13:06:58 +02007152}
7153
Jack Morgenstein9603b612014-07-28 23:30:22 +03007154static void mlx5_ib_remove(struct mlx5_core_dev *mdev, void *context)
Eli Cohene126ba92013-07-07 17:25:49 +03007155{
Daniel Jurgens32f69e42018-01-04 17:25:36 +02007156 struct mlx5_ib_multiport_info *mpi;
7157 struct mlx5_ib_dev *dev;
Haggai Eran6aec21f2014-12-11 17:04:23 +02007158
Bodong Wangf0666f12019-02-12 22:55:34 -08007159 if (MLX5_ESWITCH_MANAGER(mdev) && context == mdev) {
7160 mlx5_ib_unregister_vport_reps(mdev);
7161 return;
7162 }
7163
Daniel Jurgens32f69e42018-01-04 17:25:36 +02007164 if (mlx5_core_is_mp_slave(mdev)) {
7165 mpi = context;
7166 mutex_lock(&mlx5_ib_multiport_mutex);
7167 if (mpi->ibdev)
7168 mlx5_ib_unbind_slave_port(mpi->ibdev, mpi);
7169 list_del(&mpi->list);
7170 mutex_unlock(&mlx5_ib_multiport_mutex);
Danit Goldberg5d44ade2019-09-16 09:48:18 +03007171 kfree(mpi);
Daniel Jurgens32f69e42018-01-04 17:25:36 +02007172 return;
7173 }
7174
7175 dev = context;
Bodong Wangf0666f12019-02-12 22:55:34 -08007176 __mlx5_ib_remove(dev, dev->profile, MLX5_IB_STAGE_MAX);
Eli Cohene126ba92013-07-07 17:25:49 +03007177}
7178
Jack Morgenstein9603b612014-07-28 23:30:22 +03007179static struct mlx5_interface mlx5_ib_interface = {
7180 .add = mlx5_ib_add,
7181 .remove = mlx5_ib_remove,
Saeed Mahameed64613d942015-04-02 17:07:34 +03007182 .protocol = MLX5_INTERFACE_PROTOCOL_IB,
Eli Cohene126ba92013-07-07 17:25:49 +03007183};
7184
Ilya Lesokhinc44ef992018-03-13 15:18:48 +02007185unsigned long mlx5_ib_get_xlt_emergency_page(void)
7186{
7187 mutex_lock(&xlt_emergency_page_mutex);
7188 return xlt_emergency_page;
7189}
7190
7191void mlx5_ib_put_xlt_emergency_page(void)
7192{
7193 mutex_unlock(&xlt_emergency_page_mutex);
7194}
7195
Eli Cohene126ba92013-07-07 17:25:49 +03007196static int __init mlx5_ib_init(void)
7197{
Haggai Eran6aec21f2014-12-11 17:04:23 +02007198 int err;
7199
Ilya Lesokhinc44ef992018-03-13 15:18:48 +02007200 xlt_emergency_page = __get_free_page(GFP_KERNEL);
7201 if (!xlt_emergency_page)
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02007202 return -ENOMEM;
7203
Ilya Lesokhinc44ef992018-03-13 15:18:48 +02007204 mutex_init(&xlt_emergency_page_mutex);
7205
7206 mlx5_ib_event_wq = alloc_ordered_workqueue("mlx5_ib_event_wq", 0);
7207 if (!mlx5_ib_event_wq) {
7208 free_page(xlt_emergency_page);
7209 return -ENOMEM;
7210 }
7211
Artemy Kovalyov81713d32017-01-18 16:58:11 +02007212 mlx5_ib_odp_init();
Jack Morgenstein9603b612014-07-28 23:30:22 +03007213
Haggai Eran6aec21f2014-12-11 17:04:23 +02007214 err = mlx5_register_interface(&mlx5_ib_interface);
Haggai Eran6aec21f2014-12-11 17:04:23 +02007215
7216 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03007217}
7218
7219static void __exit mlx5_ib_cleanup(void)
7220{
Jack Morgenstein9603b612014-07-28 23:30:22 +03007221 mlx5_unregister_interface(&mlx5_ib_interface);
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02007222 destroy_workqueue(mlx5_ib_event_wq);
Ilya Lesokhinc44ef992018-03-13 15:18:48 +02007223 mutex_destroy(&xlt_emergency_page_mutex);
7224 free_page(xlt_emergency_page);
Eli Cohene126ba92013-07-07 17:25:49 +03007225}
7226
7227module_init(mlx5_ib_init);
7228module_exit(mlx5_ib_cleanup);