blob: 56d4b1e9dd2366cc2c1718e316f26dba8ce8571a [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>
Matan Barak8c846602018-03-28 09:27:41 +030070
71#define UVERBS_MODULE_NAME mlx5_ib
72#include <rdma/uverbs_named_ioctl.h>
Eli Cohene126ba92013-07-07 17:25:49 +030073
74#define DRIVER_NAME "mlx5_ib"
Tariq Toukanb3599112017-02-22 17:45:46 +020075#define DRIVER_VERSION "5.0-0"
Eli Cohene126ba92013-07-07 17:25:49 +030076
77MODULE_AUTHOR("Eli Cohen <eli@mellanox.com>");
78MODULE_DESCRIPTION("Mellanox Connect-IB HCA IB driver");
79MODULE_LICENSE("Dual BSD/GPL");
Eli Cohene126ba92013-07-07 17:25:49 +030080
Eli Cohene126ba92013-07-07 17:25:49 +030081static char mlx5_version[] =
82 DRIVER_NAME ": Mellanox Connect-IB Infiniband driver v"
Tariq Toukanb3599112017-02-22 17:45:46 +020083 DRIVER_VERSION "\n";
Eli Cohene126ba92013-07-07 17:25:49 +030084
Daniel Jurgensd69a24e2018-01-04 17:25:37 +020085struct mlx5_ib_event_work {
86 struct work_struct work;
Saeed Mahameeddf097a22018-11-26 14:39:00 -080087 union {
88 struct mlx5_ib_dev *dev;
89 struct mlx5_ib_multiport_info *mpi;
90 };
91 bool is_slave;
Saeed Mahameed134e9342018-11-26 14:39:02 -080092 unsigned int event;
Saeed Mahameeddf097a22018-11-26 14:39:00 -080093 void *param;
Daniel Jurgensd69a24e2018-01-04 17:25:37 +020094};
95
Eran Ben Elishada7525d2015-12-14 16:34:10 +020096enum {
97 MLX5_ATOMIC_SIZE_QP_8BYTES = 1 << 3,
98};
Majd Dibbiny1b5daf12015-06-04 19:30:46 +030099
Daniel Jurgensd69a24e2018-01-04 17:25:37 +0200100static struct workqueue_struct *mlx5_ib_event_wq;
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200101static LIST_HEAD(mlx5_ib_unaffiliated_port_list);
102static LIST_HEAD(mlx5_ib_dev_list);
103/*
104 * This mutex should be held when accessing either of the above lists
105 */
106static DEFINE_MUTEX(mlx5_ib_multiport_mutex);
107
Ilya Lesokhinc44ef992018-03-13 15:18:48 +0200108/* We can't use an array for xlt_emergency_page because dma_map_single
109 * doesn't work on kernel modules memory
110 */
111static unsigned long xlt_emergency_page;
112static struct mutex xlt_emergency_page_mutex;
113
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200114struct mlx5_ib_dev *mlx5_ib_get_ibdev_from_mpi(struct mlx5_ib_multiport_info *mpi)
115{
116 struct mlx5_ib_dev *dev;
117
118 mutex_lock(&mlx5_ib_multiport_mutex);
119 dev = mpi->ibdev;
120 mutex_unlock(&mlx5_ib_multiport_mutex);
121 return dev;
122}
123
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300124static enum rdma_link_layer
Achiad Shochatebd61f62015-12-23 18:47:16 +0200125mlx5_port_type_cap_to_rdma_ll(int port_type_cap)
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300126{
Achiad Shochatebd61f62015-12-23 18:47:16 +0200127 switch (port_type_cap) {
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300128 case MLX5_CAP_PORT_TYPE_IB:
129 return IB_LINK_LAYER_INFINIBAND;
130 case MLX5_CAP_PORT_TYPE_ETH:
131 return IB_LINK_LAYER_ETHERNET;
132 default:
133 return IB_LINK_LAYER_UNSPECIFIED;
134 }
135}
136
Achiad Shochatebd61f62015-12-23 18:47:16 +0200137static enum rdma_link_layer
138mlx5_ib_port_link_layer(struct ib_device *device, u8 port_num)
139{
140 struct mlx5_ib_dev *dev = to_mdev(device);
141 int port_type_cap = MLX5_CAP_GEN(dev->mdev, port_type);
142
143 return mlx5_port_type_cap_to_rdma_ll(port_type_cap);
144}
145
Moni Shouafd65f1b2017-05-30 09:56:05 +0300146static int get_port_state(struct ib_device *ibdev,
147 u8 port_num,
148 enum ib_port_state *state)
149{
150 struct ib_port_attr attr;
151 int ret;
152
153 memset(&attr, 0, sizeof(attr));
Kamal Heib3023a1e2018-12-10 21:09:48 +0200154 ret = ibdev->ops.query_port(ibdev, port_num, &attr);
Moni Shouafd65f1b2017-05-30 09:56:05 +0300155 if (!ret)
156 *state = attr.state;
157 return ret;
158}
159
Mark Bloch35b0aa62019-03-28 15:27:39 +0200160static struct mlx5_roce *mlx5_get_rep_roce(struct mlx5_ib_dev *dev,
161 struct net_device *ndev,
162 u8 *port_num)
163{
164 struct mlx5_eswitch *esw = dev->mdev->priv.eswitch;
165 struct net_device *rep_ndev;
166 struct mlx5_ib_port *port;
167 int i;
168
169 for (i = 0; i < dev->num_ports; i++) {
170 port = &dev->port[i];
171 if (!port->rep)
172 continue;
173
174 read_lock(&port->roce.netdev_lock);
175 rep_ndev = mlx5_ib_get_rep_netdev(esw,
176 port->rep->vport);
177 if (rep_ndev == ndev) {
178 read_unlock(&port->roce.netdev_lock);
179 *port_num = i + 1;
180 return &port->roce;
181 }
182 read_unlock(&port->roce.netdev_lock);
183 }
184
185 return NULL;
186}
187
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200188static int mlx5_netdev_event(struct notifier_block *this,
189 unsigned long event, void *ptr)
190{
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200191 struct mlx5_roce *roce = container_of(this, struct mlx5_roce, nb);
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200192 struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200193 u8 port_num = roce->native_port_num;
194 struct mlx5_core_dev *mdev;
195 struct mlx5_ib_dev *ibdev;
196
197 ibdev = roce->dev;
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200198 mdev = mlx5_ib_get_native_port_mdev(ibdev, port_num, NULL);
199 if (!mdev)
200 return NOTIFY_DONE;
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200201
Aviv Heller5ec8c832016-09-18 20:48:00 +0300202 switch (event) {
203 case NETDEV_REGISTER:
Mark Bloch35b0aa62019-03-28 15:27:39 +0200204 /* Should already be registered during the load */
205 if (ibdev->is_rep)
206 break;
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200207 write_lock(&roce->netdev_lock);
Linus Torvaldsdce45af2019-05-09 09:02:46 -0700208 if (ndev->dev.parent == mdev->device)
Or Gerlitz842a9c82018-12-11 18:10:43 +0200209 roce->netdev = ndev;
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200210 write_unlock(&roce->netdev_lock);
Aviv Heller5ec8c832016-09-18 20:48:00 +0300211 break;
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200212
Or Gerlitz842a9c82018-12-11 18:10:43 +0200213 case NETDEV_UNREGISTER:
Mark Bloch35b0aa62019-03-28 15:27:39 +0200214 /* In case of reps, ib device goes away before the netdevs */
Or Gerlitz842a9c82018-12-11 18:10:43 +0200215 write_lock(&roce->netdev_lock);
216 if (roce->netdev == ndev)
217 roce->netdev = NULL;
218 write_unlock(&roce->netdev_lock);
219 break;
220
Moni Shouafd65f1b2017-05-30 09:56:05 +0300221 case NETDEV_CHANGE:
Aviv Heller5ec8c832016-09-18 20:48:00 +0300222 case NETDEV_UP:
Aviv Heller88621df2016-09-18 20:48:02 +0300223 case NETDEV_DOWN: {
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200224 struct net_device *lag_ndev = mlx5_lag_get_roce_netdev(mdev);
Aviv Heller88621df2016-09-18 20:48:02 +0300225 struct net_device *upper = NULL;
226
227 if (lag_ndev) {
228 upper = netdev_master_upper_dev_get(lag_ndev);
229 dev_put(lag_ndev);
230 }
231
Mark Bloch35b0aa62019-03-28 15:27:39 +0200232 if (ibdev->is_rep)
233 roce = mlx5_get_rep_roce(ibdev, ndev, &port_num);
234 if (!roce)
235 return NOTIFY_DONE;
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200236 if ((upper == ndev || (!upper && ndev == roce->netdev))
Aviv Heller88621df2016-09-18 20:48:02 +0300237 && ibdev->ib_active) {
Bart Van Assche626bc022016-12-05 17:18:08 -0800238 struct ib_event ibev = { };
Moni Shouafd65f1b2017-05-30 09:56:05 +0300239 enum ib_port_state port_state;
Aviv Heller5ec8c832016-09-18 20:48:00 +0300240
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200241 if (get_port_state(&ibdev->ib_dev, port_num,
242 &port_state))
243 goto done;
Moni Shouafd65f1b2017-05-30 09:56:05 +0300244
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200245 if (roce->last_port_state == port_state)
246 goto done;
Moni Shouafd65f1b2017-05-30 09:56:05 +0300247
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200248 roce->last_port_state = port_state;
Aviv Heller5ec8c832016-09-18 20:48:00 +0300249 ibev.device = &ibdev->ib_dev;
Moni Shouafd65f1b2017-05-30 09:56:05 +0300250 if (port_state == IB_PORT_DOWN)
251 ibev.event = IB_EVENT_PORT_ERR;
252 else if (port_state == IB_PORT_ACTIVE)
253 ibev.event = IB_EVENT_PORT_ACTIVE;
254 else
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200255 goto done;
Moni Shouafd65f1b2017-05-30 09:56:05 +0300256
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200257 ibev.element.port_num = port_num;
Aviv Heller5ec8c832016-09-18 20:48:00 +0300258 ib_dispatch_event(&ibev);
259 }
260 break;
Aviv Heller88621df2016-09-18 20:48:02 +0300261 }
Aviv Heller5ec8c832016-09-18 20:48:00 +0300262
263 default:
264 break;
265 }
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200266done:
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200267 mlx5_ib_put_native_port_mdev(ibdev, port_num);
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200268 return NOTIFY_DONE;
269}
270
271static struct net_device *mlx5_ib_get_netdev(struct ib_device *device,
272 u8 port_num)
273{
274 struct mlx5_ib_dev *ibdev = to_mdev(device);
275 struct net_device *ndev;
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200276 struct mlx5_core_dev *mdev;
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200277
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200278 mdev = mlx5_ib_get_native_port_mdev(ibdev, port_num, NULL);
279 if (!mdev)
280 return NULL;
281
282 ndev = mlx5_lag_get_roce_netdev(mdev);
Aviv Heller88621df2016-09-18 20:48:02 +0300283 if (ndev)
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200284 goto out;
Aviv Heller88621df2016-09-18 20:48:02 +0300285
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200286 /* Ensure ndev does not disappear before we invoke dev_hold()
287 */
Mark Bloch95579e72019-03-28 15:27:33 +0200288 read_lock(&ibdev->port[port_num - 1].roce.netdev_lock);
289 ndev = ibdev->port[port_num - 1].roce.netdev;
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200290 if (ndev)
291 dev_hold(ndev);
Mark Bloch95579e72019-03-28 15:27:33 +0200292 read_unlock(&ibdev->port[port_num - 1].roce.netdev_lock);
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200293
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200294out:
295 mlx5_ib_put_native_port_mdev(ibdev, port_num);
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200296 return ndev;
297}
298
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200299struct mlx5_core_dev *mlx5_ib_get_native_port_mdev(struct mlx5_ib_dev *ibdev,
300 u8 ib_port_num,
301 u8 *native_port_num)
302{
303 enum rdma_link_layer ll = mlx5_ib_port_link_layer(&ibdev->ib_dev,
304 ib_port_num);
305 struct mlx5_core_dev *mdev = NULL;
306 struct mlx5_ib_multiport_info *mpi;
307 struct mlx5_ib_port *port;
308
Mark Bloch210b1f72018-03-05 20:09:47 +0200309 if (!mlx5_core_mp_enabled(ibdev->mdev) ||
310 ll != IB_LINK_LAYER_ETHERNET) {
311 if (native_port_num)
312 *native_port_num = ib_port_num;
313 return ibdev->mdev;
314 }
315
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200316 if (native_port_num)
317 *native_port_num = 1;
318
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200319 port = &ibdev->port[ib_port_num - 1];
320 if (!port)
321 return NULL;
322
323 spin_lock(&port->mp.mpi_lock);
324 mpi = ibdev->port[ib_port_num - 1].mp.mpi;
325 if (mpi && !mpi->unaffiliate) {
326 mdev = mpi->mdev;
327 /* If it's the master no need to refcount, it'll exist
328 * as long as the ib_dev exists.
329 */
330 if (!mpi->is_master)
331 mpi->mdev_refcnt++;
332 }
333 spin_unlock(&port->mp.mpi_lock);
334
335 return mdev;
336}
337
338void mlx5_ib_put_native_port_mdev(struct mlx5_ib_dev *ibdev, u8 port_num)
339{
340 enum rdma_link_layer ll = mlx5_ib_port_link_layer(&ibdev->ib_dev,
341 port_num);
342 struct mlx5_ib_multiport_info *mpi;
343 struct mlx5_ib_port *port;
344
345 if (!mlx5_core_mp_enabled(ibdev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
346 return;
347
348 port = &ibdev->port[port_num - 1];
349
350 spin_lock(&port->mp.mpi_lock);
351 mpi = ibdev->port[port_num - 1].mp.mpi;
352 if (mpi->is_master)
353 goto out;
354
355 mpi->mdev_refcnt--;
356 if (mpi->unaffiliate)
357 complete(&mpi->unref_comp);
358out:
359 spin_unlock(&port->mp.mpi_lock);
360}
361
Aya Levin08e86762019-02-12 22:55:46 -0800362static int translate_eth_legacy_proto_oper(u32 eth_proto_oper, u8 *active_speed,
363 u8 *active_width)
Noa Osherovichf1b65df2017-04-20 20:53:33 +0300364{
365 switch (eth_proto_oper) {
366 case MLX5E_PROT_MASK(MLX5E_1000BASE_CX_SGMII):
367 case MLX5E_PROT_MASK(MLX5E_1000BASE_KX):
368 case MLX5E_PROT_MASK(MLX5E_100BASE_TX):
369 case MLX5E_PROT_MASK(MLX5E_1000BASE_T):
370 *active_width = IB_WIDTH_1X;
371 *active_speed = IB_SPEED_SDR;
372 break;
373 case MLX5E_PROT_MASK(MLX5E_10GBASE_T):
374 case MLX5E_PROT_MASK(MLX5E_10GBASE_CX4):
375 case MLX5E_PROT_MASK(MLX5E_10GBASE_KX4):
376 case MLX5E_PROT_MASK(MLX5E_10GBASE_KR):
377 case MLX5E_PROT_MASK(MLX5E_10GBASE_CR):
378 case MLX5E_PROT_MASK(MLX5E_10GBASE_SR):
379 case MLX5E_PROT_MASK(MLX5E_10GBASE_ER):
380 *active_width = IB_WIDTH_1X;
381 *active_speed = IB_SPEED_QDR;
382 break;
383 case MLX5E_PROT_MASK(MLX5E_25GBASE_CR):
384 case MLX5E_PROT_MASK(MLX5E_25GBASE_KR):
385 case MLX5E_PROT_MASK(MLX5E_25GBASE_SR):
386 *active_width = IB_WIDTH_1X;
387 *active_speed = IB_SPEED_EDR;
388 break;
389 case MLX5E_PROT_MASK(MLX5E_40GBASE_CR4):
390 case MLX5E_PROT_MASK(MLX5E_40GBASE_KR4):
391 case MLX5E_PROT_MASK(MLX5E_40GBASE_SR4):
392 case MLX5E_PROT_MASK(MLX5E_40GBASE_LR4):
393 *active_width = IB_WIDTH_4X;
394 *active_speed = IB_SPEED_QDR;
395 break;
396 case MLX5E_PROT_MASK(MLX5E_50GBASE_CR2):
397 case MLX5E_PROT_MASK(MLX5E_50GBASE_KR2):
398 case MLX5E_PROT_MASK(MLX5E_50GBASE_SR2):
399 *active_width = IB_WIDTH_1X;
400 *active_speed = IB_SPEED_HDR;
401 break;
402 case MLX5E_PROT_MASK(MLX5E_56GBASE_R4):
403 *active_width = IB_WIDTH_4X;
404 *active_speed = IB_SPEED_FDR;
405 break;
406 case MLX5E_PROT_MASK(MLX5E_100GBASE_CR4):
407 case MLX5E_PROT_MASK(MLX5E_100GBASE_SR4):
408 case MLX5E_PROT_MASK(MLX5E_100GBASE_KR4):
409 case MLX5E_PROT_MASK(MLX5E_100GBASE_LR4):
410 *active_width = IB_WIDTH_4X;
411 *active_speed = IB_SPEED_EDR;
412 break;
413 default:
414 return -EINVAL;
415 }
416
417 return 0;
418}
419
Aya Levin08e86762019-02-12 22:55:46 -0800420static int translate_eth_ext_proto_oper(u32 eth_proto_oper, u8 *active_speed,
421 u8 *active_width)
422{
423 switch (eth_proto_oper) {
424 case MLX5E_PROT_MASK(MLX5E_SGMII_100M):
425 case MLX5E_PROT_MASK(MLX5E_1000BASE_X_SGMII):
426 *active_width = IB_WIDTH_1X;
427 *active_speed = IB_SPEED_SDR;
428 break;
429 case MLX5E_PROT_MASK(MLX5E_5GBASE_R):
430 *active_width = IB_WIDTH_1X;
431 *active_speed = IB_SPEED_DDR;
432 break;
433 case MLX5E_PROT_MASK(MLX5E_10GBASE_XFI_XAUI_1):
434 *active_width = IB_WIDTH_1X;
435 *active_speed = IB_SPEED_QDR;
436 break;
437 case MLX5E_PROT_MASK(MLX5E_40GBASE_XLAUI_4_XLPPI_4):
438 *active_width = IB_WIDTH_4X;
439 *active_speed = IB_SPEED_QDR;
440 break;
441 case MLX5E_PROT_MASK(MLX5E_25GAUI_1_25GBASE_CR_KR):
442 *active_width = IB_WIDTH_1X;
443 *active_speed = IB_SPEED_EDR;
444 break;
445 case MLX5E_PROT_MASK(MLX5E_50GAUI_2_LAUI_2_50GBASE_CR2_KR2):
Aya Levincd272872019-03-11 14:35:58 +0200446 *active_width = IB_WIDTH_2X;
447 *active_speed = IB_SPEED_EDR;
448 break;
Aya Levin08e86762019-02-12 22:55:46 -0800449 case MLX5E_PROT_MASK(MLX5E_50GAUI_1_LAUI_1_50GBASE_CR_KR):
450 *active_width = IB_WIDTH_1X;
451 *active_speed = IB_SPEED_HDR;
452 break;
Aya Levincd272872019-03-11 14:35:58 +0200453 case MLX5E_PROT_MASK(MLX5E_CAUI_4_100GBASE_CR4_KR4):
454 *active_width = IB_WIDTH_4X;
455 *active_speed = IB_SPEED_EDR;
456 break;
Aya Levin08e86762019-02-12 22:55:46 -0800457 case MLX5E_PROT_MASK(MLX5E_100GAUI_2_100GBASE_CR2_KR2):
458 *active_width = IB_WIDTH_2X;
459 *active_speed = IB_SPEED_HDR;
460 break;
461 case MLX5E_PROT_MASK(MLX5E_200GAUI_4_200GBASE_CR4_KR4):
462 *active_width = IB_WIDTH_4X;
463 *active_speed = IB_SPEED_HDR;
464 break;
465 default:
466 return -EINVAL;
467 }
468
469 return 0;
470}
471
472static int translate_eth_proto_oper(u32 eth_proto_oper, u8 *active_speed,
473 u8 *active_width, bool ext)
474{
475 return ext ?
476 translate_eth_ext_proto_oper(eth_proto_oper, active_speed,
477 active_width) :
478 translate_eth_legacy_proto_oper(eth_proto_oper, active_speed,
479 active_width);
480}
481
Ilan Tayari095b0922017-05-14 16:04:30 +0300482static int mlx5_query_port_roce(struct ib_device *device, u8 port_num,
483 struct ib_port_attr *props)
Achiad Shochat3f89a642015-12-23 18:47:21 +0200484{
485 struct mlx5_ib_dev *dev = to_mdev(device);
Aya Levinbc4e12f2019-02-12 22:55:43 -0800486 u32 out[MLX5_ST_SZ_DW(ptys_reg)] = {0};
Colin Ian Kingda005f92018-01-09 15:55:43 +0000487 struct mlx5_core_dev *mdev;
Aviv Heller88621df2016-09-18 20:48:02 +0300488 struct net_device *ndev, *upper;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200489 enum ib_mtu ndev_ib_mtu;
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200490 bool put_mdev = true;
Leon Romanovskyc876a1b2016-01-09 13:06:25 +0200491 u16 qkey_viol_cntr;
Noa Osherovichf1b65df2017-04-20 20:53:33 +0300492 u32 eth_prot_oper;
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200493 u8 mdev_port_num;
Aya Levin08e86762019-02-12 22:55:46 -0800494 bool ext;
Ilan Tayari095b0922017-05-14 16:04:30 +0300495 int err;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200496
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200497 mdev = mlx5_ib_get_native_port_mdev(dev, port_num, &mdev_port_num);
498 if (!mdev) {
499 /* This means the port isn't affiliated yet. Get the
500 * info for the master port instead.
501 */
502 put_mdev = false;
503 mdev = dev->mdev;
504 mdev_port_num = 1;
505 port_num = 1;
506 }
507
Noa Osherovichf1b65df2017-04-20 20:53:33 +0300508 /* Possible bad flows are checked before filling out props so in case
509 * of an error it will still be zeroed out.
Mark Bloch26628e22019-03-28 15:27:41 +0200510 * Use native port in case of reps
Noa Osherovich50f22fd2017-04-20 20:53:32 +0300511 */
Mark Bloch26628e22019-03-28 15:27:41 +0200512 if (dev->is_rep)
513 err = mlx5_query_port_ptys(mdev, out, sizeof(out), MLX5_PTYS_EN,
514 1);
515 else
516 err = mlx5_query_port_ptys(mdev, out, sizeof(out), MLX5_PTYS_EN,
517 mdev_port_num);
Ilan Tayari095b0922017-05-14 16:04:30 +0300518 if (err)
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200519 goto out;
Aya Levin08e86762019-02-12 22:55:46 -0800520 ext = MLX5_CAP_PCAM_FEATURE(dev->mdev, ptys_extended_ethernet);
521 eth_prot_oper = MLX5_GET_ETH_PROTO(ptys_reg, out, ext, eth_proto_oper);
Noa Osherovichf1b65df2017-04-20 20:53:33 +0300522
Honggang Li7672ed32018-03-16 10:37:13 +0800523 props->active_width = IB_WIDTH_4X;
524 props->active_speed = IB_SPEED_QDR;
525
Noa Osherovichf1b65df2017-04-20 20:53:33 +0300526 translate_eth_proto_oper(eth_prot_oper, &props->active_speed,
Aya Levin08e86762019-02-12 22:55:46 -0800527 &props->active_width, ext);
Achiad Shochat3f89a642015-12-23 18:47:21 +0200528
Jason Gunthorpe2f944c02018-07-04 15:57:48 +0300529 props->port_cap_flags |= IB_PORT_CM_SUP;
530 props->ip_gids = true;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200531
532 props->gid_tbl_len = MLX5_CAP_ROCE(dev->mdev,
533 roce_address_table_size);
534 props->max_mtu = IB_MTU_4096;
535 props->max_msg_sz = 1 << MLX5_CAP_GEN(dev->mdev, log_max_msg);
536 props->pkey_tbl_len = 1;
537 props->state = IB_PORT_DOWN;
538 props->phys_state = 3;
539
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200540 mlx5_query_nic_vport_qkey_viol_cntr(mdev, &qkey_viol_cntr);
Leon Romanovskyc876a1b2016-01-09 13:06:25 +0200541 props->qkey_viol_cntr = qkey_viol_cntr;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200542
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200543 /* If this is a stub query for an unaffiliated port stop here */
544 if (!put_mdev)
545 goto out;
546
Achiad Shochat3f89a642015-12-23 18:47:21 +0200547 ndev = mlx5_ib_get_netdev(device, port_num);
548 if (!ndev)
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200549 goto out;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200550
Aviv Heller7c34ec12018-08-23 13:47:53 +0300551 if (dev->lag_active) {
Aviv Heller88621df2016-09-18 20:48:02 +0300552 rcu_read_lock();
553 upper = netdev_master_upper_dev_get_rcu(ndev);
554 if (upper) {
555 dev_put(ndev);
556 ndev = upper;
557 dev_hold(ndev);
558 }
559 rcu_read_unlock();
560 }
561
Achiad Shochat3f89a642015-12-23 18:47:21 +0200562 if (netif_running(ndev) && netif_carrier_ok(ndev)) {
563 props->state = IB_PORT_ACTIVE;
564 props->phys_state = 5;
565 }
566
567 ndev_ib_mtu = iboe_get_mtu(ndev->mtu);
568
569 dev_put(ndev);
570
571 props->active_mtu = min(props->max_mtu, ndev_ib_mtu);
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200572out:
573 if (put_mdev)
574 mlx5_ib_put_native_port_mdev(dev, port_num);
575 return err;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200576}
577
Ilan Tayari095b0922017-05-14 16:04:30 +0300578static int set_roce_addr(struct mlx5_ib_dev *dev, u8 port_num,
579 unsigned int index, const union ib_gid *gid,
580 const struct ib_gid_attr *attr)
Achiad Shochat3cca2602015-12-23 18:47:23 +0200581{
Ilan Tayari095b0922017-05-14 16:04:30 +0300582 enum ib_gid_type gid_type = IB_GID_TYPE_IB;
Parav Pandita70c0732019-05-02 10:48:03 +0300583 u16 vlan_id = 0xffff;
Ilan Tayari095b0922017-05-14 16:04:30 +0300584 u8 roce_version = 0;
585 u8 roce_l3_type = 0;
Ilan Tayari095b0922017-05-14 16:04:30 +0300586 u8 mac[ETH_ALEN];
Parav Pandita70c0732019-05-02 10:48:03 +0300587 int ret;
Achiad Shochat3cca2602015-12-23 18:47:23 +0200588
Ilan Tayari095b0922017-05-14 16:04:30 +0300589 if (gid) {
590 gid_type = attr->gid_type;
Parav Pandita70c0732019-05-02 10:48:03 +0300591 ret = rdma_read_gid_l2_fields(attr, &vlan_id, &mac[0]);
592 if (ret)
593 return ret;
Achiad Shochat3cca2602015-12-23 18:47:23 +0200594 }
595
Ilan Tayari095b0922017-05-14 16:04:30 +0300596 switch (gid_type) {
Achiad Shochat3cca2602015-12-23 18:47:23 +0200597 case IB_GID_TYPE_IB:
Ilan Tayari095b0922017-05-14 16:04:30 +0300598 roce_version = MLX5_ROCE_VERSION_1;
Achiad Shochat3cca2602015-12-23 18:47:23 +0200599 break;
600 case IB_GID_TYPE_ROCE_UDP_ENCAP:
Ilan Tayari095b0922017-05-14 16:04:30 +0300601 roce_version = MLX5_ROCE_VERSION_2;
602 if (ipv6_addr_v4mapped((void *)gid))
603 roce_l3_type = MLX5_ROCE_L3_TYPE_IPV4;
604 else
605 roce_l3_type = MLX5_ROCE_L3_TYPE_IPV6;
Achiad Shochat3cca2602015-12-23 18:47:23 +0200606 break;
607
608 default:
Ilan Tayari095b0922017-05-14 16:04:30 +0300609 mlx5_ib_warn(dev, "Unexpected GID type %u\n", gid_type);
Achiad Shochat3cca2602015-12-23 18:47:23 +0200610 }
611
Ilan Tayari095b0922017-05-14 16:04:30 +0300612 return mlx5_core_roce_gid_set(dev->mdev, index, roce_version,
Parav Panditcf34e1f2019-01-27 20:35:50 +0200613 roce_l3_type, gid->raw, mac,
Parav Pandita70c0732019-05-02 10:48:03 +0300614 vlan_id < VLAN_CFI_MASK, vlan_id,
Parav Panditcf34e1f2019-01-27 20:35:50 +0200615 port_num);
Achiad Shochat3cca2602015-12-23 18:47:23 +0200616}
617
Parav Panditf4df9a72018-06-05 08:40:16 +0300618static int mlx5_ib_add_gid(const struct ib_gid_attr *attr,
Achiad Shochat3cca2602015-12-23 18:47:23 +0200619 __always_unused void **context)
620{
Parav Pandit414448d2018-04-01 15:08:24 +0300621 return set_roce_addr(to_mdev(attr->device), attr->port_num,
Parav Panditf4df9a72018-06-05 08:40:16 +0300622 attr->index, &attr->gid, attr);
Achiad Shochat3cca2602015-12-23 18:47:23 +0200623}
624
Parav Pandit414448d2018-04-01 15:08:24 +0300625static int mlx5_ib_del_gid(const struct ib_gid_attr *attr,
626 __always_unused void **context)
Achiad Shochat3cca2602015-12-23 18:47:23 +0200627{
Parav Pandit414448d2018-04-01 15:08:24 +0300628 return set_roce_addr(to_mdev(attr->device), attr->port_num,
629 attr->index, NULL, NULL);
Achiad Shochat3cca2602015-12-23 18:47:23 +0200630}
631
Parav Pandit47ec3862018-06-13 10:22:06 +0300632__be16 mlx5_get_roce_udp_sport(struct mlx5_ib_dev *dev,
633 const struct ib_gid_attr *attr)
Achiad Shochat2811ba52015-12-23 18:47:24 +0200634{
Parav Pandit47ec3862018-06-13 10:22:06 +0300635 if (attr->gid_type != IB_GID_TYPE_ROCE_UDP_ENCAP)
Achiad Shochat2811ba52015-12-23 18:47:24 +0200636 return 0;
637
638 return cpu_to_be16(MLX5_CAP_ROCE(dev->mdev, r_roce_min_src_udp_port));
639}
640
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300641static int mlx5_use_mad_ifc(struct mlx5_ib_dev *dev)
642{
Noa Osherovich7fae6652016-09-12 19:16:23 +0300643 if (MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_IB)
644 return !MLX5_CAP_GEN(dev->mdev, ib_virt);
645 return 0;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300646}
647
648enum {
649 MLX5_VPORT_ACCESS_METHOD_MAD,
650 MLX5_VPORT_ACCESS_METHOD_HCA,
651 MLX5_VPORT_ACCESS_METHOD_NIC,
652};
653
654static int mlx5_get_vport_access_method(struct ib_device *ibdev)
655{
656 if (mlx5_use_mad_ifc(to_mdev(ibdev)))
657 return MLX5_VPORT_ACCESS_METHOD_MAD;
658
Achiad Shochatebd61f62015-12-23 18:47:16 +0200659 if (mlx5_ib_port_link_layer(ibdev, 1) ==
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300660 IB_LINK_LAYER_ETHERNET)
661 return MLX5_VPORT_ACCESS_METHOD_NIC;
662
663 return MLX5_VPORT_ACCESS_METHOD_HCA;
664}
665
Eran Ben Elishada7525d2015-12-14 16:34:10 +0200666static void get_atomic_caps(struct mlx5_ib_dev *dev,
Moni Shoua776a3902018-01-02 16:19:33 +0200667 u8 atomic_size_qp,
Eran Ben Elishada7525d2015-12-14 16:34:10 +0200668 struct ib_device_attr *props)
669{
670 u8 tmp;
671 u8 atomic_operations = MLX5_CAP_ATOMIC(dev->mdev, atomic_operations);
Eran Ben Elishada7525d2015-12-14 16:34:10 +0200672 u8 atomic_req_8B_endianness_mode =
Or Gerlitzbd108382017-05-28 15:24:17 +0300673 MLX5_CAP_ATOMIC(dev->mdev, atomic_req_8B_endianness_mode);
Eran Ben Elishada7525d2015-12-14 16:34:10 +0200674
675 /* Check if HW supports 8 bytes standard atomic operations and capable
676 * of host endianness respond
677 */
678 tmp = MLX5_ATOMIC_OPS_CMP_SWAP | MLX5_ATOMIC_OPS_FETCH_ADD;
679 if (((atomic_operations & tmp) == tmp) &&
680 (atomic_size_qp & MLX5_ATOMIC_SIZE_QP_8BYTES) &&
681 (atomic_req_8B_endianness_mode)) {
682 props->atomic_cap = IB_ATOMIC_HCA;
683 } else {
684 props->atomic_cap = IB_ATOMIC_NONE;
685 }
686}
687
Moni Shoua776a3902018-01-02 16:19:33 +0200688static void get_atomic_caps_qp(struct mlx5_ib_dev *dev,
689 struct ib_device_attr *props)
690{
691 u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_qp);
692
693 get_atomic_caps(dev, atomic_size_qp, props);
694}
695
696static void get_atomic_caps_dc(struct mlx5_ib_dev *dev,
697 struct ib_device_attr *props)
698{
699 u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_dc);
700
701 get_atomic_caps(dev, atomic_size_qp, props);
702}
703
704bool mlx5_ib_dc_atomic_is_supported(struct mlx5_ib_dev *dev)
705{
706 struct ib_device_attr props = {};
707
708 get_atomic_caps_dc(dev, &props);
709 return (props.atomic_cap == IB_ATOMIC_HCA) ? true : false;
710}
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300711static int mlx5_query_system_image_guid(struct ib_device *ibdev,
712 __be64 *sys_image_guid)
713{
714 struct mlx5_ib_dev *dev = to_mdev(ibdev);
715 struct mlx5_core_dev *mdev = dev->mdev;
716 u64 tmp;
717 int err;
718
719 switch (mlx5_get_vport_access_method(ibdev)) {
720 case MLX5_VPORT_ACCESS_METHOD_MAD:
721 return mlx5_query_mad_ifc_system_image_guid(ibdev,
722 sys_image_guid);
723
724 case MLX5_VPORT_ACCESS_METHOD_HCA:
725 err = mlx5_query_hca_vport_system_image_guid(mdev, &tmp);
Achiad Shochat3f89a642015-12-23 18:47:21 +0200726 break;
727
728 case MLX5_VPORT_ACCESS_METHOD_NIC:
729 err = mlx5_query_nic_vport_system_image_guid(mdev, &tmp);
730 break;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300731
732 default:
733 return -EINVAL;
734 }
Achiad Shochat3f89a642015-12-23 18:47:21 +0200735
736 if (!err)
737 *sys_image_guid = cpu_to_be64(tmp);
738
739 return err;
740
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300741}
742
743static int mlx5_query_max_pkeys(struct ib_device *ibdev,
744 u16 *max_pkeys)
745{
746 struct mlx5_ib_dev *dev = to_mdev(ibdev);
747 struct mlx5_core_dev *mdev = dev->mdev;
748
749 switch (mlx5_get_vport_access_method(ibdev)) {
750 case MLX5_VPORT_ACCESS_METHOD_MAD:
751 return mlx5_query_mad_ifc_max_pkeys(ibdev, max_pkeys);
752
753 case MLX5_VPORT_ACCESS_METHOD_HCA:
754 case MLX5_VPORT_ACCESS_METHOD_NIC:
755 *max_pkeys = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev,
756 pkey_table_size));
757 return 0;
758
759 default:
760 return -EINVAL;
761 }
762}
763
764static int mlx5_query_vendor_id(struct ib_device *ibdev,
765 u32 *vendor_id)
766{
767 struct mlx5_ib_dev *dev = to_mdev(ibdev);
768
769 switch (mlx5_get_vport_access_method(ibdev)) {
770 case MLX5_VPORT_ACCESS_METHOD_MAD:
771 return mlx5_query_mad_ifc_vendor_id(ibdev, vendor_id);
772
773 case MLX5_VPORT_ACCESS_METHOD_HCA:
774 case MLX5_VPORT_ACCESS_METHOD_NIC:
775 return mlx5_core_query_vendor_id(dev->mdev, vendor_id);
776
777 default:
778 return -EINVAL;
779 }
780}
781
782static int mlx5_query_node_guid(struct mlx5_ib_dev *dev,
783 __be64 *node_guid)
784{
785 u64 tmp;
786 int err;
787
788 switch (mlx5_get_vport_access_method(&dev->ib_dev)) {
789 case MLX5_VPORT_ACCESS_METHOD_MAD:
790 return mlx5_query_mad_ifc_node_guid(dev, node_guid);
791
792 case MLX5_VPORT_ACCESS_METHOD_HCA:
793 err = mlx5_query_hca_vport_node_guid(dev->mdev, &tmp);
Achiad Shochat3f89a642015-12-23 18:47:21 +0200794 break;
795
796 case MLX5_VPORT_ACCESS_METHOD_NIC:
797 err = mlx5_query_nic_vport_node_guid(dev->mdev, &tmp);
798 break;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300799
800 default:
801 return -EINVAL;
802 }
Achiad Shochat3f89a642015-12-23 18:47:21 +0200803
804 if (!err)
805 *node_guid = cpu_to_be64(tmp);
806
807 return err;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300808}
809
810struct mlx5_reg_node_desc {
Yuval Shaiabd99fde2016-08-25 10:57:07 -0700811 u8 desc[IB_DEVICE_NODE_DESC_MAX];
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300812};
813
814static int mlx5_query_node_desc(struct mlx5_ib_dev *dev, char *node_desc)
815{
816 struct mlx5_reg_node_desc in;
817
818 if (mlx5_use_mad_ifc(dev))
819 return mlx5_query_mad_ifc_node_desc(dev, node_desc);
820
821 memset(&in, 0, sizeof(in));
822
823 return mlx5_core_access_reg(dev->mdev, &in, sizeof(in), node_desc,
824 sizeof(struct mlx5_reg_node_desc),
825 MLX5_REG_NODE_DESC, 0, 0);
826}
827
Eli Cohene126ba92013-07-07 17:25:49 +0300828static int mlx5_ib_query_device(struct ib_device *ibdev,
Matan Barak2528e332015-06-11 16:35:25 +0300829 struct ib_device_attr *props,
830 struct ib_udata *uhw)
Eli Cohene126ba92013-07-07 17:25:49 +0300831{
832 struct mlx5_ib_dev *dev = to_mdev(ibdev);
Saeed Mahameed938fe832015-05-28 22:28:41 +0300833 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +0300834 int err = -ENOMEM;
Eli Cohen288c01b2016-10-27 16:36:45 +0300835 int max_sq_desc;
Eli Cohene126ba92013-07-07 17:25:49 +0300836 int max_rq_sg;
837 int max_sq_sg;
Sagi Grimberge0238a62015-07-21 14:40:12 +0300838 u64 min_page_size = 1ull << MLX5_CAP_GEN(mdev, log_pg_sz);
Daniel Jurgens85c7c012018-01-04 17:25:43 +0200839 bool raw_support = !mlx5_core_mp_enabled(mdev);
Bodong Wang402ca532016-06-17 15:02:20 +0300840 struct mlx5_ib_query_device_resp resp = {};
841 size_t resp_len;
842 u64 max_tso;
Eli Cohene126ba92013-07-07 17:25:49 +0300843
Bodong Wang402ca532016-06-17 15:02:20 +0300844 resp_len = sizeof(resp.comp_mask) + sizeof(resp.response_length);
845 if (uhw->outlen && uhw->outlen < resp_len)
846 return -EINVAL;
847 else
848 resp.response_length = resp_len;
849
850 if (uhw->inlen && !ib_is_udata_cleared(uhw, 0, uhw->inlen))
Matan Barak2528e332015-06-11 16:35:25 +0300851 return -EINVAL;
852
Eli Cohene126ba92013-07-07 17:25:49 +0300853 memset(props, 0, sizeof(*props));
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300854 err = mlx5_query_system_image_guid(ibdev,
855 &props->sys_image_guid);
856 if (err)
857 return err;
858
859 err = mlx5_query_max_pkeys(ibdev, &props->max_pkeys);
860 if (err)
861 return err;
862
863 err = mlx5_query_vendor_id(ibdev, &props->vendor_id);
864 if (err)
865 return err;
Eli Cohene126ba92013-07-07 17:25:49 +0300866
Jack Morgenstein9603b612014-07-28 23:30:22 +0300867 props->fw_ver = ((u64)fw_rev_maj(dev->mdev) << 32) |
868 (fw_rev_min(dev->mdev) << 16) |
869 fw_rev_sub(dev->mdev);
Eli Cohene126ba92013-07-07 17:25:49 +0300870 props->device_cap_flags = IB_DEVICE_CHANGE_PHY_PORT |
871 IB_DEVICE_PORT_ACTIVE_EVENT |
872 IB_DEVICE_SYS_IMAGE_GUID |
Eli Cohen1a4c3a32014-02-06 17:41:25 +0200873 IB_DEVICE_RC_RNR_NAK_GEN;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300874
875 if (MLX5_CAP_GEN(mdev, pkv))
Eli Cohene126ba92013-07-07 17:25:49 +0300876 props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300877 if (MLX5_CAP_GEN(mdev, qkv))
Eli Cohene126ba92013-07-07 17:25:49 +0300878 props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300879 if (MLX5_CAP_GEN(mdev, apm))
Eli Cohene126ba92013-07-07 17:25:49 +0300880 props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300881 if (MLX5_CAP_GEN(mdev, xrc))
Eli Cohene126ba92013-07-07 17:25:49 +0300882 props->device_cap_flags |= IB_DEVICE_XRC;
Matan Barakd2370e02016-02-29 18:05:30 +0200883 if (MLX5_CAP_GEN(mdev, imaicl)) {
884 props->device_cap_flags |= IB_DEVICE_MEM_WINDOW |
885 IB_DEVICE_MEM_WINDOW_TYPE_2B;
886 props->max_mw = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
Sagi Grimbergb005d312016-02-29 19:07:33 +0200887 /* We support 'Gappy' memory registration too */
888 props->device_cap_flags |= IB_DEVICE_SG_GAPS_REG;
Matan Barakd2370e02016-02-29 18:05:30 +0200889 }
Eli Cohene126ba92013-07-07 17:25:49 +0300890 props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300891 if (MLX5_CAP_GEN(mdev, sho)) {
Israel Rukshinc0a6cbb2019-06-11 18:52:50 +0300892 props->device_cap_flags |= IB_DEVICE_INTEGRITY_HANDOVER;
Sagi Grimberg2dea9092014-02-23 14:19:13 +0200893 /* At this stage no support for signature handover */
894 props->sig_prot_cap = IB_PROT_T10DIF_TYPE_1 |
895 IB_PROT_T10DIF_TYPE_2 |
896 IB_PROT_T10DIF_TYPE_3;
897 props->sig_guard_cap = IB_GUARD_T10DIF_CRC |
898 IB_GUARD_T10DIF_CSUM;
899 }
Saeed Mahameed938fe832015-05-28 22:28:41 +0300900 if (MLX5_CAP_GEN(mdev, block_lb_mc))
Eli Cohenf360d882014-04-02 00:10:16 +0300901 props->device_cap_flags |= IB_DEVICE_BLOCK_MULTICAST_LOOPBACK;
Eli Cohene126ba92013-07-07 17:25:49 +0300902
Daniel Jurgens85c7c012018-01-04 17:25:43 +0200903 if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) && raw_support) {
Noa Osheroviche8161332017-01-18 15:40:01 +0200904 if (MLX5_CAP_ETH(mdev, csum_cap)) {
905 /* Legacy bit to support old userspace libraries */
Bodong Wang88115fe2015-12-18 13:53:20 +0200906 props->device_cap_flags |= IB_DEVICE_RAW_IP_CSUM;
Noa Osheroviche8161332017-01-18 15:40:01 +0200907 props->raw_packet_caps |= IB_RAW_PACKET_CAP_IP_CSUM;
908 }
909
910 if (MLX5_CAP_ETH(dev->mdev, vlan_cap))
911 props->raw_packet_caps |=
912 IB_RAW_PACKET_CAP_CVLAN_STRIPPING;
Bodong Wang88115fe2015-12-18 13:53:20 +0200913
Bodong Wang402ca532016-06-17 15:02:20 +0300914 if (field_avail(typeof(resp), tso_caps, uhw->outlen)) {
915 max_tso = MLX5_CAP_ETH(mdev, max_lso_cap);
916 if (max_tso) {
917 resp.tso_caps.max_tso = 1 << max_tso;
918 resp.tso_caps.supported_qpts |=
919 1 << IB_QPT_RAW_PACKET;
920 resp.response_length += sizeof(resp.tso_caps);
921 }
922 }
Yishai Hadas31f69a82016-08-28 11:28:45 +0300923
924 if (field_avail(typeof(resp), rss_caps, uhw->outlen)) {
925 resp.rss_caps.rx_hash_function =
926 MLX5_RX_HASH_FUNC_TOEPLITZ;
927 resp.rss_caps.rx_hash_fields_mask =
928 MLX5_RX_HASH_SRC_IPV4 |
929 MLX5_RX_HASH_DST_IPV4 |
930 MLX5_RX_HASH_SRC_IPV6 |
931 MLX5_RX_HASH_DST_IPV6 |
932 MLX5_RX_HASH_SRC_PORT_TCP |
933 MLX5_RX_HASH_DST_PORT_TCP |
934 MLX5_RX_HASH_SRC_PORT_UDP |
Maor Gottlieb4e2b53a2017-12-24 14:51:25 +0200935 MLX5_RX_HASH_DST_PORT_UDP |
936 MLX5_RX_HASH_INNER;
Matan Barak2d93fc82018-03-28 09:27:55 +0300937 if (mlx5_accel_ipsec_device_caps(dev->mdev) &
938 MLX5_ACCEL_IPSEC_CAP_DEVICE)
939 resp.rss_caps.rx_hash_fields_mask |=
940 MLX5_RX_HASH_IPSEC_SPI;
Yishai Hadas31f69a82016-08-28 11:28:45 +0300941 resp.response_length += sizeof(resp.rss_caps);
942 }
943 } else {
944 if (field_avail(typeof(resp), tso_caps, uhw->outlen))
945 resp.response_length += sizeof(resp.tso_caps);
946 if (field_avail(typeof(resp), rss_caps, uhw->outlen))
947 resp.response_length += sizeof(resp.rss_caps);
Bodong Wang402ca532016-06-17 15:02:20 +0300948 }
949
Erez Shitritf0313962016-02-21 16:27:17 +0200950 if (MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) {
951 props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
952 props->device_cap_flags |= IB_DEVICE_UD_TSO;
953 }
954
Maor Gottlieb03404e82017-05-30 10:29:13 +0300955 if (MLX5_CAP_GEN(dev->mdev, rq_delay_drop) &&
Daniel Jurgens85c7c012018-01-04 17:25:43 +0200956 MLX5_CAP_GEN(dev->mdev, general_notification_event) &&
957 raw_support)
Maor Gottlieb03404e82017-05-30 10:29:13 +0300958 props->raw_packet_caps |= IB_RAW_PACKET_CAP_DELAY_DROP;
959
Yishai Hadas1d54f892017-06-08 16:15:11 +0300960 if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads) &&
961 MLX5_CAP_IPOIB_ENHANCED(mdev, csum_cap))
962 props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
963
Majd Dibbinycff5a0f2016-04-17 17:19:38 +0300964 if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
Daniel Jurgens85c7c012018-01-04 17:25:43 +0200965 MLX5_CAP_ETH(dev->mdev, scatter_fcs) &&
966 raw_support) {
Noa Osheroviche8161332017-01-18 15:40:01 +0200967 /* Legacy bit to support old userspace libraries */
Majd Dibbinycff5a0f2016-04-17 17:19:38 +0300968 props->device_cap_flags |= IB_DEVICE_RAW_SCATTER_FCS;
Noa Osheroviche8161332017-01-18 15:40:01 +0200969 props->raw_packet_caps |= IB_RAW_PACKET_CAP_SCATTER_FCS;
970 }
Majd Dibbinycff5a0f2016-04-17 17:19:38 +0300971
Ariel Levkovich24da0012018-04-05 18:53:27 +0300972 if (MLX5_CAP_DEV_MEM(mdev, memic)) {
973 props->max_dm_size =
974 MLX5_CAP_DEV_MEM(mdev, max_memic_size);
975 }
976
Maor Gottliebda6d6ba32016-06-04 15:15:28 +0300977 if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS))
978 props->device_cap_flags |= IB_DEVICE_MANAGED_FLOW_STEERING;
979
Noa Osherovichb1383aa2017-10-29 13:59:45 +0200980 if (MLX5_CAP_GEN(mdev, end_pad))
981 props->device_cap_flags |= IB_DEVICE_PCI_WRITE_END_PADDING;
982
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300983 props->vendor_part_id = mdev->pdev->device;
984 props->hw_ver = mdev->pdev->revision;
Eli Cohene126ba92013-07-07 17:25:49 +0300985
986 props->max_mr_size = ~0ull;
Sagi Grimberge0238a62015-07-21 14:40:12 +0300987 props->page_size_cap = ~(min_page_size - 1);
Saeed Mahameed938fe832015-05-28 22:28:41 +0300988 props->max_qp = 1 << MLX5_CAP_GEN(mdev, log_max_qp);
989 props->max_qp_wr = 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
990 max_rq_sg = MLX5_CAP_GEN(mdev, max_wqe_sz_rq) /
991 sizeof(struct mlx5_wqe_data_seg);
Eli Cohen288c01b2016-10-27 16:36:45 +0300992 max_sq_desc = min_t(int, MLX5_CAP_GEN(mdev, max_wqe_sz_sq), 512);
993 max_sq_sg = (max_sq_desc - sizeof(struct mlx5_wqe_ctrl_seg) -
994 sizeof(struct mlx5_wqe_raddr_seg)) /
995 sizeof(struct mlx5_wqe_data_seg);
Steve Wise33023fb2018-06-18 08:05:26 -0700996 props->max_send_sge = max_sq_sg;
997 props->max_recv_sge = max_rq_sg;
Sagi Grimberg986ef952016-03-31 19:03:25 +0300998 props->max_sge_rd = MLX5_MAX_SGE_RD;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300999 props->max_cq = 1 << MLX5_CAP_GEN(mdev, log_max_cq);
Leon Romanovsky9f177682016-01-14 08:11:40 +02001000 props->max_cqe = (1 << MLX5_CAP_GEN(mdev, log_max_cq_sz)) - 1;
Saeed Mahameed938fe832015-05-28 22:28:41 +03001001 props->max_mr = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
1002 props->max_pd = 1 << MLX5_CAP_GEN(mdev, log_max_pd);
1003 props->max_qp_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_req_qp);
1004 props->max_qp_init_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_res_qp);
1005 props->max_srq = 1 << MLX5_CAP_GEN(mdev, log_max_srq);
1006 props->max_srq_wr = (1 << MLX5_CAP_GEN(mdev, log_max_srq_sz)) - 1;
1007 props->local_ca_ack_delay = MLX5_CAP_GEN(mdev, local_ca_ack_delay);
Eli Cohene126ba92013-07-07 17:25:49 +03001008 props->max_res_rd_atom = props->max_qp_rd_atom * props->max_qp;
Eli Cohene126ba92013-07-07 17:25:49 +03001009 props->max_srq_sge = max_rq_sg - 1;
Sagi Grimberg911f4332016-03-03 13:37:51 +02001010 props->max_fast_reg_page_list_len =
1011 1 << MLX5_CAP_GEN(mdev, log_max_klm_list_size);
Max Gurtovoy62e3c372019-06-11 18:52:43 +03001012 props->max_pi_fast_reg_page_list_len =
1013 props->max_fast_reg_page_list_len / 2;
Moni Shoua776a3902018-01-02 16:19:33 +02001014 get_atomic_caps_qp(dev, props);
Eli Cohen81bea282013-09-11 16:35:30 +03001015 props->masked_atomic_cap = IB_ATOMIC_NONE;
Saeed Mahameed938fe832015-05-28 22:28:41 +03001016 props->max_mcast_grp = 1 << MLX5_CAP_GEN(mdev, log_max_mcg);
1017 props->max_mcast_qp_attach = MLX5_CAP_GEN(mdev, max_qp_mcg);
Eli Cohene126ba92013-07-07 17:25:49 +03001018 props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
1019 props->max_mcast_grp;
1020 props->max_map_per_fmr = INT_MAX; /* no limit in ConnectIB */
Maor Gottlieb86695a62016-10-27 16:36:38 +03001021 props->max_ah = INT_MAX;
Matan Barak7c60bcb2015-12-15 20:30:11 +02001022 props->hca_core_clock = MLX5_CAP_GEN(mdev, device_frequency_khz);
1023 props->timestamp_mask = 0x7FFFFFFFFFFFFFFFULL;
Eli Cohene126ba92013-07-07 17:25:49 +03001024
Leon Romanovskye502b8b2019-01-08 16:07:24 +02001025 if (IS_ENABLED(CONFIG_INFINIBAND_ON_DEMAND_PAGING)) {
1026 if (MLX5_CAP_GEN(mdev, pg))
1027 props->device_cap_flags |= IB_DEVICE_ON_DEMAND_PAGING;
1028 props->odp_caps = dev->odp_caps;
1029 }
Haggai Eran8cdd3122014-12-11 17:04:20 +02001030
Leon Romanovsky051f2632015-12-20 12:16:11 +02001031 if (MLX5_CAP_GEN(mdev, cd))
1032 props->device_cap_flags |= IB_DEVICE_CROSS_CHANNEL;
1033
Eli Coheneff901d2016-03-11 22:58:42 +02001034 if (!mlx5_core_is_pf(mdev))
1035 props->device_cap_flags |= IB_DEVICE_VIRTUAL_FUNCTION;
1036
Yishai Hadas31f69a82016-08-28 11:28:45 +03001037 if (mlx5_ib_port_link_layer(ibdev, 1) ==
Daniel Jurgens85c7c012018-01-04 17:25:43 +02001038 IB_LINK_LAYER_ETHERNET && raw_support) {
Yishai Hadas31f69a82016-08-28 11:28:45 +03001039 props->rss_caps.max_rwq_indirection_tables =
1040 1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt);
1041 props->rss_caps.max_rwq_indirection_table_size =
1042 1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt_size);
1043 props->rss_caps.supported_qpts = 1 << IB_QPT_RAW_PACKET;
1044 props->max_wq_type_rq =
1045 1 << MLX5_CAP_GEN(dev->mdev, log_max_rq);
1046 }
1047
Artemy Kovalyoveb761892017-08-17 15:52:09 +03001048 if (MLX5_CAP_GEN(mdev, tag_matching)) {
Leon Romanovsky78b1beb2017-09-24 21:46:29 +03001049 props->tm_caps.max_num_tags =
Artemy Kovalyoveb761892017-08-17 15:52:09 +03001050 (1 << MLX5_CAP_GEN(mdev, log_tag_matching_list_sz)) - 1;
Leon Romanovsky78b1beb2017-09-24 21:46:29 +03001051 props->tm_caps.max_ops =
Artemy Kovalyoveb761892017-08-17 15:52:09 +03001052 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
Leon Romanovsky78b1beb2017-09-24 21:46:29 +03001053 props->tm_caps.max_sge = MLX5_TM_MAX_SGE;
Artemy Kovalyoveb761892017-08-17 15:52:09 +03001054 }
1055
Danit Goldberg89705e92019-07-05 19:21:57 +03001056 if (MLX5_CAP_GEN(mdev, tag_matching) &&
1057 MLX5_CAP_GEN(mdev, rndv_offload_rc)) {
1058 props->tm_caps.flags = IB_TM_CAP_RNDV_RC;
1059 props->tm_caps.max_rndv_hdr_size = MLX5_TM_MAX_RNDV_MSG_SIZE;
1060 }
1061
Yonatan Cohen87ab3f52017-11-13 10:51:18 +02001062 if (MLX5_CAP_GEN(dev->mdev, cq_moderation)) {
1063 props->cq_caps.max_cq_moderation_count =
1064 MLX5_MAX_CQ_COUNT;
1065 props->cq_caps.max_cq_moderation_period =
1066 MLX5_MAX_CQ_PERIOD;
1067 }
1068
Bodong Wang7e43a2a2016-10-31 12:16:44 +02001069 if (field_avail(typeof(resp), cqe_comp_caps, uhw->outlen)) {
Bodong Wang7e43a2a2016-10-31 12:16:44 +02001070 resp.response_length += sizeof(resp.cqe_comp_caps);
Yonatan Cohen572f46b2018-05-27 13:42:33 +03001071
1072 if (MLX5_CAP_GEN(dev->mdev, cqe_compression)) {
1073 resp.cqe_comp_caps.max_num =
1074 MLX5_CAP_GEN(dev->mdev,
1075 cqe_compression_max_num);
1076
1077 resp.cqe_comp_caps.supported_format =
1078 MLX5_IB_CQE_RES_FORMAT_HASH |
1079 MLX5_IB_CQE_RES_FORMAT_CSUM;
Yonatan Cohen6f1006a2018-05-27 13:42:34 +03001080
1081 if (MLX5_CAP_GEN(dev->mdev, mini_cqe_resp_stride_index))
1082 resp.cqe_comp_caps.supported_format |=
1083 MLX5_IB_CQE_RES_FORMAT_CSUM_STRIDX;
Yonatan Cohen572f46b2018-05-27 13:42:33 +03001084 }
Bodong Wang7e43a2a2016-10-31 12:16:44 +02001085 }
1086
Daniel Jurgens85c7c012018-01-04 17:25:43 +02001087 if (field_avail(typeof(resp), packet_pacing_caps, uhw->outlen) &&
1088 raw_support) {
Bodong Wangd9491672016-12-01 13:43:13 +02001089 if (MLX5_CAP_QOS(mdev, packet_pacing) &&
1090 MLX5_CAP_GEN(mdev, qos)) {
1091 resp.packet_pacing_caps.qp_rate_limit_max =
1092 MLX5_CAP_QOS(mdev, packet_pacing_max_rate);
1093 resp.packet_pacing_caps.qp_rate_limit_min =
1094 MLX5_CAP_QOS(mdev, packet_pacing_min_rate);
1095 resp.packet_pacing_caps.supported_qpts |=
1096 1 << IB_QPT_RAW_PACKET;
Bodong Wang61147f32018-03-19 15:10:30 +02001097 if (MLX5_CAP_QOS(mdev, packet_pacing_burst_bound) &&
1098 MLX5_CAP_QOS(mdev, packet_pacing_typical_size))
1099 resp.packet_pacing_caps.cap_flags |=
1100 MLX5_IB_PP_SUPPORT_BURST;
Bodong Wangd9491672016-12-01 13:43:13 +02001101 }
1102 resp.response_length += sizeof(resp.packet_pacing_caps);
1103 }
1104
Leon Romanovsky9f885202017-01-02 11:37:39 +02001105 if (field_avail(typeof(resp), mlx5_ib_support_multi_pkt_send_wqes,
1106 uhw->outlen)) {
Bodong Wang795b6092017-08-17 15:52:34 +03001107 if (MLX5_CAP_ETH(mdev, multi_pkt_send_wqe))
1108 resp.mlx5_ib_support_multi_pkt_send_wqes =
1109 MLX5_IB_ALLOW_MPW;
Bodong Wang050da902017-08-17 15:52:35 +03001110
1111 if (MLX5_CAP_ETH(mdev, enhanced_multi_pkt_send_wqe))
1112 resp.mlx5_ib_support_multi_pkt_send_wqes |=
1113 MLX5_IB_SUPPORT_EMPW;
1114
Leon Romanovsky9f885202017-01-02 11:37:39 +02001115 resp.response_length +=
1116 sizeof(resp.mlx5_ib_support_multi_pkt_send_wqes);
1117 }
1118
Guy Levide57f2a2017-10-19 08:25:52 +03001119 if (field_avail(typeof(resp), flags, uhw->outlen)) {
1120 resp.response_length += sizeof(resp.flags);
Guy Levi7a0c8f42017-10-19 08:25:53 +03001121
Guy Levide57f2a2017-10-19 08:25:52 +03001122 if (MLX5_CAP_GEN(mdev, cqe_compression_128))
1123 resp.flags |=
1124 MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_COMP;
Guy Levi7a0c8f42017-10-19 08:25:53 +03001125
1126 if (MLX5_CAP_GEN(mdev, cqe_128_always))
1127 resp.flags |= MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_PAD;
Danit Goldberg7e11b912018-11-30 13:22:06 +02001128 if (MLX5_CAP_GEN(mdev, qp_packet_based))
1129 resp.flags |=
1130 MLX5_IB_QUERY_DEV_RESP_PACKET_BASED_CREDIT_MODE;
Guy Levi7249c8e2019-04-10 10:59:45 +03001131
1132 resp.flags |= MLX5_IB_QUERY_DEV_RESP_FLAGS_SCAT2CQE_DCT;
Guy Levide57f2a2017-10-19 08:25:52 +03001133 }
Leon Romanovsky9f885202017-01-02 11:37:39 +02001134
Noa Osherovich96dc3fc2017-08-17 15:52:28 +03001135 if (field_avail(typeof(resp), sw_parsing_caps,
1136 uhw->outlen)) {
1137 resp.response_length += sizeof(resp.sw_parsing_caps);
1138 if (MLX5_CAP_ETH(mdev, swp)) {
1139 resp.sw_parsing_caps.sw_parsing_offloads |=
1140 MLX5_IB_SW_PARSING;
1141
1142 if (MLX5_CAP_ETH(mdev, swp_csum))
1143 resp.sw_parsing_caps.sw_parsing_offloads |=
1144 MLX5_IB_SW_PARSING_CSUM;
1145
1146 if (MLX5_CAP_ETH(mdev, swp_lso))
1147 resp.sw_parsing_caps.sw_parsing_offloads |=
1148 MLX5_IB_SW_PARSING_LSO;
1149
1150 if (resp.sw_parsing_caps.sw_parsing_offloads)
1151 resp.sw_parsing_caps.supported_qpts =
1152 BIT(IB_QPT_RAW_PACKET);
1153 }
1154 }
1155
Daniel Jurgens85c7c012018-01-04 17:25:43 +02001156 if (field_avail(typeof(resp), striding_rq_caps, uhw->outlen) &&
1157 raw_support) {
Noa Osherovichb4f34592017-10-17 18:01:12 +03001158 resp.response_length += sizeof(resp.striding_rq_caps);
1159 if (MLX5_CAP_GEN(mdev, striding_rq)) {
1160 resp.striding_rq_caps.min_single_stride_log_num_of_bytes =
1161 MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES;
1162 resp.striding_rq_caps.max_single_stride_log_num_of_bytes =
1163 MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES;
1164 resp.striding_rq_caps.min_single_wqe_log_num_of_strides =
1165 MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES;
1166 resp.striding_rq_caps.max_single_wqe_log_num_of_strides =
1167 MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES;
1168 resp.striding_rq_caps.supported_qpts =
1169 BIT(IB_QPT_RAW_PACKET);
1170 }
1171 }
1172
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001173 if (field_avail(typeof(resp), tunnel_offloads_caps,
1174 uhw->outlen)) {
1175 resp.response_length += sizeof(resp.tunnel_offloads_caps);
1176 if (MLX5_CAP_ETH(mdev, tunnel_stateless_vxlan))
1177 resp.tunnel_offloads_caps |=
1178 MLX5_IB_TUNNELED_OFFLOADS_VXLAN;
1179 if (MLX5_CAP_ETH(mdev, tunnel_stateless_geneve_rx))
1180 resp.tunnel_offloads_caps |=
1181 MLX5_IB_TUNNELED_OFFLOADS_GENEVE;
1182 if (MLX5_CAP_ETH(mdev, tunnel_stateless_gre))
1183 resp.tunnel_offloads_caps |=
1184 MLX5_IB_TUNNELED_OFFLOADS_GRE;
Ariel Levkoviche818e252018-05-13 14:33:35 +03001185 if (MLX5_CAP_GEN(mdev, flex_parser_protocols) &
1186 MLX5_FLEX_PROTO_CW_MPLS_GRE)
1187 resp.tunnel_offloads_caps |=
1188 MLX5_IB_TUNNELED_OFFLOADS_MPLS_GRE;
1189 if (MLX5_CAP_GEN(mdev, flex_parser_protocols) &
1190 MLX5_FLEX_PROTO_CW_MPLS_UDP)
1191 resp.tunnel_offloads_caps |=
1192 MLX5_IB_TUNNELED_OFFLOADS_MPLS_UDP;
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001193 }
1194
Bodong Wang402ca532016-06-17 15:02:20 +03001195 if (uhw->outlen) {
1196 err = ib_copy_to_udata(uhw, &resp, resp.response_length);
1197
1198 if (err)
1199 return err;
1200 }
1201
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001202 return 0;
1203}
Eli Cohene126ba92013-07-07 17:25:49 +03001204
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001205enum mlx5_ib_width {
1206 MLX5_IB_WIDTH_1X = 1 << 0,
1207 MLX5_IB_WIDTH_2X = 1 << 1,
1208 MLX5_IB_WIDTH_4X = 1 << 2,
1209 MLX5_IB_WIDTH_8X = 1 << 3,
1210 MLX5_IB_WIDTH_12X = 1 << 4
1211};
1212
Michael Guralnikdb7a6912018-11-21 15:03:54 +02001213static void translate_active_width(struct ib_device *ibdev, u8 active_width,
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001214 u8 *ib_width)
1215{
1216 struct mlx5_ib_dev *dev = to_mdev(ibdev);
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001217
Michael Guralnikdb7a6912018-11-21 15:03:54 +02001218 if (active_width & MLX5_IB_WIDTH_1X)
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001219 *ib_width = IB_WIDTH_1X;
Michael Guralnikd7649702018-12-09 11:49:54 +02001220 else if (active_width & MLX5_IB_WIDTH_2X)
1221 *ib_width = IB_WIDTH_2X;
Michael Guralnikdb7a6912018-11-21 15:03:54 +02001222 else if (active_width & MLX5_IB_WIDTH_4X)
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001223 *ib_width = IB_WIDTH_4X;
Michael Guralnikdb7a6912018-11-21 15:03:54 +02001224 else if (active_width & MLX5_IB_WIDTH_8X)
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001225 *ib_width = IB_WIDTH_8X;
Michael Guralnikdb7a6912018-11-21 15:03:54 +02001226 else if (active_width & MLX5_IB_WIDTH_12X)
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001227 *ib_width = IB_WIDTH_12X;
Michael Guralnikdb7a6912018-11-21 15:03:54 +02001228 else {
1229 mlx5_ib_dbg(dev, "Invalid active_width %d, setting width to default value: 4x\n",
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001230 (int)active_width);
Michael Guralnikdb7a6912018-11-21 15:03:54 +02001231 *ib_width = IB_WIDTH_4X;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001232 }
1233
Michael Guralnikdb7a6912018-11-21 15:03:54 +02001234 return;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001235}
1236
1237static int mlx5_mtu_to_ib_mtu(int mtu)
1238{
1239 switch (mtu) {
1240 case 256: return 1;
1241 case 512: return 2;
1242 case 1024: return 3;
1243 case 2048: return 4;
1244 case 4096: return 5;
1245 default:
1246 pr_warn("invalid mtu\n");
1247 return -1;
1248 }
1249}
1250
1251enum ib_max_vl_num {
1252 __IB_MAX_VL_0 = 1,
1253 __IB_MAX_VL_0_1 = 2,
1254 __IB_MAX_VL_0_3 = 3,
1255 __IB_MAX_VL_0_7 = 4,
1256 __IB_MAX_VL_0_14 = 5,
1257};
1258
1259enum mlx5_vl_hw_cap {
1260 MLX5_VL_HW_0 = 1,
1261 MLX5_VL_HW_0_1 = 2,
1262 MLX5_VL_HW_0_2 = 3,
1263 MLX5_VL_HW_0_3 = 4,
1264 MLX5_VL_HW_0_4 = 5,
1265 MLX5_VL_HW_0_5 = 6,
1266 MLX5_VL_HW_0_6 = 7,
1267 MLX5_VL_HW_0_7 = 8,
1268 MLX5_VL_HW_0_14 = 15
1269};
1270
1271static int translate_max_vl_num(struct ib_device *ibdev, u8 vl_hw_cap,
1272 u8 *max_vl_num)
1273{
1274 switch (vl_hw_cap) {
1275 case MLX5_VL_HW_0:
1276 *max_vl_num = __IB_MAX_VL_0;
1277 break;
1278 case MLX5_VL_HW_0_1:
1279 *max_vl_num = __IB_MAX_VL_0_1;
1280 break;
1281 case MLX5_VL_HW_0_3:
1282 *max_vl_num = __IB_MAX_VL_0_3;
1283 break;
1284 case MLX5_VL_HW_0_7:
1285 *max_vl_num = __IB_MAX_VL_0_7;
1286 break;
1287 case MLX5_VL_HW_0_14:
1288 *max_vl_num = __IB_MAX_VL_0_14;
1289 break;
1290
1291 default:
1292 return -EINVAL;
1293 }
1294
1295 return 0;
1296}
1297
1298static int mlx5_query_hca_port(struct ib_device *ibdev, u8 port,
1299 struct ib_port_attr *props)
1300{
1301 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1302 struct mlx5_core_dev *mdev = dev->mdev;
1303 struct mlx5_hca_vport_context *rep;
Saeed Mahameed046339e2016-04-22 00:33:03 +03001304 u16 max_mtu;
1305 u16 oper_mtu;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001306 int err;
1307 u8 ib_link_width_oper;
1308 u8 vl_hw_cap;
1309
1310 rep = kzalloc(sizeof(*rep), GFP_KERNEL);
1311 if (!rep) {
1312 err = -ENOMEM;
1313 goto out;
1314 }
1315
Or Gerlitzc4550c62017-01-24 13:02:39 +02001316 /* props being zeroed by the caller, avoid zeroing it here */
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001317
1318 err = mlx5_query_hca_vport_context(mdev, 0, port, 0, rep);
1319 if (err)
1320 goto out;
1321
1322 props->lid = rep->lid;
1323 props->lmc = rep->lmc;
1324 props->sm_lid = rep->sm_lid;
1325 props->sm_sl = rep->sm_sl;
1326 props->state = rep->vport_state;
1327 props->phys_state = rep->port_physical_state;
1328 props->port_cap_flags = rep->cap_mask1;
1329 props->gid_tbl_len = mlx5_get_gid_table_len(MLX5_CAP_GEN(mdev, gid_table_size));
1330 props->max_msg_sz = 1 << MLX5_CAP_GEN(mdev, log_max_msg);
1331 props->pkey_tbl_len = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, pkey_table_size));
1332 props->bad_pkey_cntr = rep->pkey_violation_counter;
1333 props->qkey_viol_cntr = rep->qkey_violation_counter;
1334 props->subnet_timeout = rep->subnet_timeout;
1335 props->init_type_reply = rep->init_type_reply;
1336
Michael Guralnik4106a752018-12-09 11:49:51 +02001337 if (props->port_cap_flags & IB_PORT_CAP_MASK2_SUP)
1338 props->port_cap_flags2 = rep->cap_mask2;
1339
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001340 err = mlx5_query_port_link_width_oper(mdev, &ib_link_width_oper, port);
1341 if (err)
1342 goto out;
1343
Michael Guralnikdb7a6912018-11-21 15:03:54 +02001344 translate_active_width(ibdev, ib_link_width_oper, &props->active_width);
1345
Noa Osherovichd5beb7f2016-06-02 10:47:53 +03001346 err = mlx5_query_port_ib_proto_oper(mdev, &props->active_speed, port);
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001347 if (err)
1348 goto out;
1349
Saeed Mahameedfacc9692015-06-11 14:47:27 +03001350 mlx5_query_port_max_mtu(mdev, &max_mtu, port);
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001351
1352 props->max_mtu = mlx5_mtu_to_ib_mtu(max_mtu);
1353
Saeed Mahameedfacc9692015-06-11 14:47:27 +03001354 mlx5_query_port_oper_mtu(mdev, &oper_mtu, port);
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001355
1356 props->active_mtu = mlx5_mtu_to_ib_mtu(oper_mtu);
1357
1358 err = mlx5_query_port_vl_hw_cap(mdev, &vl_hw_cap, port);
1359 if (err)
1360 goto out;
1361
1362 err = translate_max_vl_num(ibdev, vl_hw_cap,
1363 &props->max_vl_num);
1364out:
1365 kfree(rep);
Eli Cohene126ba92013-07-07 17:25:49 +03001366 return err;
1367}
1368
1369int mlx5_ib_query_port(struct ib_device *ibdev, u8 port,
1370 struct ib_port_attr *props)
1371{
Ilan Tayari095b0922017-05-14 16:04:30 +03001372 unsigned int count;
1373 int ret;
1374
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001375 switch (mlx5_get_vport_access_method(ibdev)) {
1376 case MLX5_VPORT_ACCESS_METHOD_MAD:
Ilan Tayari095b0922017-05-14 16:04:30 +03001377 ret = mlx5_query_mad_ifc_port(ibdev, port, props);
1378 break;
Eli Cohene126ba92013-07-07 17:25:49 +03001379
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001380 case MLX5_VPORT_ACCESS_METHOD_HCA:
Ilan Tayari095b0922017-05-14 16:04:30 +03001381 ret = mlx5_query_hca_port(ibdev, port, props);
1382 break;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001383
Achiad Shochat3f89a642015-12-23 18:47:21 +02001384 case MLX5_VPORT_ACCESS_METHOD_NIC:
Ilan Tayari095b0922017-05-14 16:04:30 +03001385 ret = mlx5_query_port_roce(ibdev, port, props);
1386 break;
Achiad Shochat3f89a642015-12-23 18:47:21 +02001387
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001388 default:
Ilan Tayari095b0922017-05-14 16:04:30 +03001389 ret = -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +03001390 }
Ilan Tayari095b0922017-05-14 16:04:30 +03001391
1392 if (!ret && props) {
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001393 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1394 struct mlx5_core_dev *mdev;
1395 bool put_mdev = true;
1396
1397 mdev = mlx5_ib_get_native_port_mdev(dev, port, NULL);
1398 if (!mdev) {
1399 /* If the port isn't affiliated yet query the master.
1400 * The master and slave will have the same values.
1401 */
1402 mdev = dev->mdev;
1403 port = 1;
1404 put_mdev = false;
1405 }
1406 count = mlx5_core_reserved_gids_count(mdev);
1407 if (put_mdev)
1408 mlx5_ib_put_native_port_mdev(dev, port);
Ilan Tayari095b0922017-05-14 16:04:30 +03001409 props->gid_tbl_len -= count;
1410 }
1411 return ret;
Eli Cohene126ba92013-07-07 17:25:49 +03001412}
1413
Mark Bloch8e6efa32017-11-06 12:22:13 +00001414static int mlx5_ib_rep_query_port(struct ib_device *ibdev, u8 port,
1415 struct ib_port_attr *props)
1416{
1417 int ret;
1418
Mark Bloch26628e22019-03-28 15:27:41 +02001419 /* Only link layer == ethernet is valid for representors
1420 * and we always use port 1
1421 */
Mark Bloch8e6efa32017-11-06 12:22:13 +00001422 ret = mlx5_query_port_roce(ibdev, port, props);
1423 if (ret || !props)
1424 return ret;
1425
1426 /* We don't support GIDS */
1427 props->gid_tbl_len = 0;
1428
1429 return ret;
1430}
1431
Eli Cohene126ba92013-07-07 17:25:49 +03001432static int mlx5_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
1433 union ib_gid *gid)
1434{
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001435 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1436 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03001437
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001438 switch (mlx5_get_vport_access_method(ibdev)) {
1439 case MLX5_VPORT_ACCESS_METHOD_MAD:
1440 return mlx5_query_mad_ifc_gids(ibdev, port, index, gid);
Eli Cohene126ba92013-07-07 17:25:49 +03001441
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001442 case MLX5_VPORT_ACCESS_METHOD_HCA:
1443 return mlx5_query_hca_vport_gid(mdev, 0, port, 0, index, gid);
Eli Cohene126ba92013-07-07 17:25:49 +03001444
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001445 default:
1446 return -EINVAL;
1447 }
Eli Cohene126ba92013-07-07 17:25:49 +03001448
Eli Cohene126ba92013-07-07 17:25:49 +03001449}
1450
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001451static int mlx5_query_hca_nic_pkey(struct ib_device *ibdev, u8 port,
1452 u16 index, u16 *pkey)
1453{
1454 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1455 struct mlx5_core_dev *mdev;
1456 bool put_mdev = true;
1457 u8 mdev_port_num;
1458 int err;
1459
1460 mdev = mlx5_ib_get_native_port_mdev(dev, port, &mdev_port_num);
1461 if (!mdev) {
1462 /* The port isn't affiliated yet, get the PKey from the master
1463 * port. For RoCE the PKey tables will be the same.
1464 */
1465 put_mdev = false;
1466 mdev = dev->mdev;
1467 mdev_port_num = 1;
1468 }
1469
1470 err = mlx5_query_hca_vport_pkey(mdev, 0, mdev_port_num, 0,
1471 index, pkey);
1472 if (put_mdev)
1473 mlx5_ib_put_native_port_mdev(dev, port);
1474
1475 return err;
1476}
1477
Eli Cohene126ba92013-07-07 17:25:49 +03001478static int mlx5_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
1479 u16 *pkey)
1480{
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001481 switch (mlx5_get_vport_access_method(ibdev)) {
1482 case MLX5_VPORT_ACCESS_METHOD_MAD:
1483 return mlx5_query_mad_ifc_pkey(ibdev, port, index, pkey);
Eli Cohene126ba92013-07-07 17:25:49 +03001484
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001485 case MLX5_VPORT_ACCESS_METHOD_HCA:
1486 case MLX5_VPORT_ACCESS_METHOD_NIC:
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001487 return mlx5_query_hca_nic_pkey(ibdev, port, index, pkey);
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001488 default:
1489 return -EINVAL;
1490 }
Eli Cohene126ba92013-07-07 17:25:49 +03001491}
1492
Eli Cohene126ba92013-07-07 17:25:49 +03001493static int mlx5_ib_modify_device(struct ib_device *ibdev, int mask,
1494 struct ib_device_modify *props)
1495{
1496 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1497 struct mlx5_reg_node_desc in;
1498 struct mlx5_reg_node_desc out;
1499 int err;
1500
1501 if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
1502 return -EOPNOTSUPP;
1503
1504 if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
1505 return 0;
1506
1507 /*
1508 * If possible, pass node desc to FW, so it can generate
1509 * a 144 trap. If cmd fails, just ignore.
1510 */
Yuval Shaiabd99fde2016-08-25 10:57:07 -07001511 memcpy(&in, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
Jack Morgenstein9603b612014-07-28 23:30:22 +03001512 err = mlx5_core_access_reg(dev->mdev, &in, sizeof(in), &out,
Eli Cohene126ba92013-07-07 17:25:49 +03001513 sizeof(out), MLX5_REG_NODE_DESC, 0, 1);
1514 if (err)
1515 return err;
1516
Yuval Shaiabd99fde2016-08-25 10:57:07 -07001517 memcpy(ibdev->node_desc, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
Eli Cohene126ba92013-07-07 17:25:49 +03001518
1519 return err;
1520}
1521
Eli Cohencdbe33d2017-02-14 07:25:38 +02001522static int set_port_caps_atomic(struct mlx5_ib_dev *dev, u8 port_num, u32 mask,
1523 u32 value)
1524{
1525 struct mlx5_hca_vport_context ctx = {};
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001526 struct mlx5_core_dev *mdev;
1527 u8 mdev_port_num;
Eli Cohencdbe33d2017-02-14 07:25:38 +02001528 int err;
1529
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001530 mdev = mlx5_ib_get_native_port_mdev(dev, port_num, &mdev_port_num);
1531 if (!mdev)
1532 return -ENODEV;
1533
1534 err = mlx5_query_hca_vport_context(mdev, 0, mdev_port_num, 0, &ctx);
Eli Cohencdbe33d2017-02-14 07:25:38 +02001535 if (err)
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001536 goto out;
Eli Cohencdbe33d2017-02-14 07:25:38 +02001537
1538 if (~ctx.cap_mask1_perm & mask) {
1539 mlx5_ib_warn(dev, "trying to change bitmask 0x%X but change supported 0x%X\n",
1540 mask, ctx.cap_mask1_perm);
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001541 err = -EINVAL;
1542 goto out;
Eli Cohencdbe33d2017-02-14 07:25:38 +02001543 }
1544
1545 ctx.cap_mask1 = value;
1546 ctx.cap_mask1_perm = mask;
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001547 err = mlx5_core_modify_hca_vport_context(mdev, 0, mdev_port_num,
1548 0, &ctx);
1549
1550out:
1551 mlx5_ib_put_native_port_mdev(dev, port_num);
Eli Cohencdbe33d2017-02-14 07:25:38 +02001552
1553 return err;
1554}
1555
Eli Cohene126ba92013-07-07 17:25:49 +03001556static int mlx5_ib_modify_port(struct ib_device *ibdev, u8 port, int mask,
1557 struct ib_port_modify *props)
1558{
1559 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1560 struct ib_port_attr attr;
1561 u32 tmp;
1562 int err;
Eli Cohencdbe33d2017-02-14 07:25:38 +02001563 u32 change_mask;
1564 u32 value;
1565 bool is_ib = (mlx5_ib_port_link_layer(ibdev, port) ==
1566 IB_LINK_LAYER_INFINIBAND);
1567
Majd Dibbinyec255872017-08-23 08:35:42 +03001568 /* CM layer calls ib_modify_port() regardless of the link layer. For
1569 * Ethernet ports, qkey violation and Port capabilities are meaningless.
1570 */
1571 if (!is_ib)
1572 return 0;
1573
Eli Cohencdbe33d2017-02-14 07:25:38 +02001574 if (MLX5_CAP_GEN(dev->mdev, ib_virt) && is_ib) {
1575 change_mask = props->clr_port_cap_mask | props->set_port_cap_mask;
1576 value = ~props->clr_port_cap_mask | props->set_port_cap_mask;
1577 return set_port_caps_atomic(dev, port, change_mask, value);
1578 }
Eli Cohene126ba92013-07-07 17:25:49 +03001579
1580 mutex_lock(&dev->cap_mask_mutex);
1581
Or Gerlitzc4550c62017-01-24 13:02:39 +02001582 err = ib_query_port(ibdev, port, &attr);
Eli Cohene126ba92013-07-07 17:25:49 +03001583 if (err)
1584 goto out;
1585
1586 tmp = (attr.port_cap_flags | props->set_port_cap_mask) &
1587 ~props->clr_port_cap_mask;
1588
Jack Morgenstein9603b612014-07-28 23:30:22 +03001589 err = mlx5_set_port_caps(dev->mdev, port, tmp);
Eli Cohene126ba92013-07-07 17:25:49 +03001590
1591out:
1592 mutex_unlock(&dev->cap_mask_mutex);
1593 return err;
1594}
1595
Eli Cohen30aa60b2017-01-03 23:55:27 +02001596static void print_lib_caps(struct mlx5_ib_dev *dev, u64 caps)
1597{
1598 mlx5_ib_dbg(dev, "MLX5_LIB_CAP_4K_UAR = %s\n",
1599 caps & MLX5_LIB_CAP_4K_UAR ? "y" : "n");
1600}
1601
Yishai Hadas31a78a52017-12-24 16:31:34 +02001602static u16 calc_dynamic_bfregs(int uars_per_sys_page)
1603{
1604 /* Large page with non 4k uar support might limit the dynamic size */
1605 if (uars_per_sys_page == 1 && PAGE_SIZE > 4096)
1606 return MLX5_MIN_DYN_BFREGS;
1607
1608 return MLX5_MAX_DYN_BFREGS;
1609}
1610
Eli Cohenb037c292017-01-03 23:55:26 +02001611static int calc_total_bfregs(struct mlx5_ib_dev *dev, bool lib_uar_4k,
1612 struct mlx5_ib_alloc_ucontext_req_v2 *req,
Yishai Hadas31a78a52017-12-24 16:31:34 +02001613 struct mlx5_bfreg_info *bfregi)
Eli Cohenb037c292017-01-03 23:55:26 +02001614{
1615 int uars_per_sys_page;
1616 int bfregs_per_sys_page;
1617 int ref_bfregs = req->total_num_bfregs;
1618
1619 if (req->total_num_bfregs == 0)
1620 return -EINVAL;
1621
1622 BUILD_BUG_ON(MLX5_MAX_BFREGS % MLX5_NON_FP_BFREGS_IN_PAGE);
1623 BUILD_BUG_ON(MLX5_MAX_BFREGS < MLX5_NON_FP_BFREGS_IN_PAGE);
1624
1625 if (req->total_num_bfregs > MLX5_MAX_BFREGS)
1626 return -ENOMEM;
1627
1628 uars_per_sys_page = get_uars_per_sys_page(dev, lib_uar_4k);
1629 bfregs_per_sys_page = uars_per_sys_page * MLX5_NON_FP_BFREGS_PER_UAR;
Yishai Hadas31a78a52017-12-24 16:31:34 +02001630 /* This holds the required static allocation asked by the user */
Eli Cohenb037c292017-01-03 23:55:26 +02001631 req->total_num_bfregs = ALIGN(req->total_num_bfregs, bfregs_per_sys_page);
Eli Cohenb037c292017-01-03 23:55:26 +02001632 if (req->num_low_latency_bfregs > req->total_num_bfregs - 1)
1633 return -EINVAL;
1634
Yishai Hadas31a78a52017-12-24 16:31:34 +02001635 bfregi->num_static_sys_pages = req->total_num_bfregs / bfregs_per_sys_page;
1636 bfregi->num_dyn_bfregs = ALIGN(calc_dynamic_bfregs(uars_per_sys_page), bfregs_per_sys_page);
1637 bfregi->total_num_bfregs = req->total_num_bfregs + bfregi->num_dyn_bfregs;
1638 bfregi->num_sys_pages = bfregi->total_num_bfregs / bfregs_per_sys_page;
1639
1640 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 +02001641 MLX5_CAP_GEN(dev->mdev, uar_4k) ? "yes" : "no",
1642 lib_uar_4k ? "yes" : "no", ref_bfregs,
Yishai Hadas31a78a52017-12-24 16:31:34 +02001643 req->total_num_bfregs, bfregi->total_num_bfregs,
1644 bfregi->num_sys_pages);
Eli Cohenb037c292017-01-03 23:55:26 +02001645
1646 return 0;
1647}
1648
1649static int allocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
1650{
1651 struct mlx5_bfreg_info *bfregi;
1652 int err;
1653 int i;
1654
1655 bfregi = &context->bfregi;
Yishai Hadas31a78a52017-12-24 16:31:34 +02001656 for (i = 0; i < bfregi->num_static_sys_pages; i++) {
Eli Cohenb037c292017-01-03 23:55:26 +02001657 err = mlx5_cmd_alloc_uar(dev->mdev, &bfregi->sys_pages[i]);
1658 if (err)
1659 goto error;
1660
1661 mlx5_ib_dbg(dev, "allocated uar %d\n", bfregi->sys_pages[i]);
1662 }
Yishai Hadas4ed131d2017-12-24 16:31:35 +02001663
1664 for (i = bfregi->num_static_sys_pages; i < bfregi->num_sys_pages; i++)
1665 bfregi->sys_pages[i] = MLX5_IB_INVALID_UAR_INDEX;
1666
Eli Cohenb037c292017-01-03 23:55:26 +02001667 return 0;
1668
1669error:
1670 for (--i; i >= 0; i--)
1671 if (mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]))
1672 mlx5_ib_warn(dev, "failed to free uar %d\n", i);
1673
1674 return err;
1675}
1676
Leon Romanovsky15177992018-06-27 10:44:24 +03001677static void deallocate_uars(struct mlx5_ib_dev *dev,
1678 struct mlx5_ib_ucontext *context)
Eli Cohenb037c292017-01-03 23:55:26 +02001679{
1680 struct mlx5_bfreg_info *bfregi;
Eli Cohenb037c292017-01-03 23:55:26 +02001681 int i;
1682
1683 bfregi = &context->bfregi;
Leon Romanovsky15177992018-06-27 10:44:24 +03001684 for (i = 0; i < bfregi->num_sys_pages; i++)
Yishai Hadas4ed131d2017-12-24 16:31:35 +02001685 if (i < bfregi->num_static_sys_pages ||
Leon Romanovsky15177992018-06-27 10:44:24 +03001686 bfregi->sys_pages[i] != MLX5_IB_INVALID_UAR_INDEX)
1687 mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]);
Eli Cohenb037c292017-01-03 23:55:26 +02001688}
1689
Mark Bloch0042f9e2018-09-17 13:30:49 +03001690int mlx5_ib_enable_lb(struct mlx5_ib_dev *dev, bool td, bool qp)
Mark Blocha560f1d2018-09-17 13:30:47 +03001691{
1692 int err = 0;
1693
1694 mutex_lock(&dev->lb.mutex);
Mark Bloch0042f9e2018-09-17 13:30:49 +03001695 if (td)
1696 dev->lb.user_td++;
1697 if (qp)
1698 dev->lb.qps++;
Mark Blocha560f1d2018-09-17 13:30:47 +03001699
Mark Bloch0042f9e2018-09-17 13:30:49 +03001700 if (dev->lb.user_td == 2 ||
1701 dev->lb.qps == 1) {
1702 if (!dev->lb.enabled) {
1703 err = mlx5_nic_vport_update_local_lb(dev->mdev, true);
1704 dev->lb.enabled = true;
1705 }
1706 }
Mark Blocha560f1d2018-09-17 13:30:47 +03001707
1708 mutex_unlock(&dev->lb.mutex);
1709
1710 return err;
1711}
1712
Mark Bloch0042f9e2018-09-17 13:30:49 +03001713void mlx5_ib_disable_lb(struct mlx5_ib_dev *dev, bool td, bool qp)
Mark Blocha560f1d2018-09-17 13:30:47 +03001714{
1715 mutex_lock(&dev->lb.mutex);
Mark Bloch0042f9e2018-09-17 13:30:49 +03001716 if (td)
1717 dev->lb.user_td--;
1718 if (qp)
1719 dev->lb.qps--;
Mark Blocha560f1d2018-09-17 13:30:47 +03001720
Mark Bloch0042f9e2018-09-17 13:30:49 +03001721 if (dev->lb.user_td == 1 &&
1722 dev->lb.qps == 0) {
1723 if (dev->lb.enabled) {
1724 mlx5_nic_vport_update_local_lb(dev->mdev, false);
1725 dev->lb.enabled = false;
1726 }
1727 }
Mark Blocha560f1d2018-09-17 13:30:47 +03001728
1729 mutex_unlock(&dev->lb.mutex);
1730}
1731
Yishai Hadasd2d19122018-09-20 21:39:32 +03001732static int mlx5_ib_alloc_transport_domain(struct mlx5_ib_dev *dev, u32 *tdn,
1733 u16 uid)
Huy Nguyenc85023e2017-05-30 09:42:54 +03001734{
1735 int err;
1736
Leon Romanovskycfdeb892018-06-19 10:39:06 +03001737 if (!MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1738 return 0;
1739
Yishai Hadasd2d19122018-09-20 21:39:32 +03001740 err = mlx5_cmd_alloc_transport_domain(dev->mdev, tdn, uid);
Huy Nguyenc85023e2017-05-30 09:42:54 +03001741 if (err)
1742 return err;
1743
1744 if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
Eran Ben Elisha8978cc92018-01-09 11:41:10 +02001745 (!MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) &&
1746 !MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
Huy Nguyenc85023e2017-05-30 09:42:54 +03001747 return err;
1748
Mark Bloch0042f9e2018-09-17 13:30:49 +03001749 return mlx5_ib_enable_lb(dev, true, false);
Huy Nguyenc85023e2017-05-30 09:42:54 +03001750}
1751
Yishai Hadasd2d19122018-09-20 21:39:32 +03001752static void mlx5_ib_dealloc_transport_domain(struct mlx5_ib_dev *dev, u32 tdn,
1753 u16 uid)
Huy Nguyenc85023e2017-05-30 09:42:54 +03001754{
Leon Romanovskycfdeb892018-06-19 10:39:06 +03001755 if (!MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1756 return;
1757
Yishai Hadasd2d19122018-09-20 21:39:32 +03001758 mlx5_cmd_dealloc_transport_domain(dev->mdev, tdn, uid);
Huy Nguyenc85023e2017-05-30 09:42:54 +03001759
1760 if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
Eran Ben Elisha8978cc92018-01-09 11:41:10 +02001761 (!MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) &&
1762 !MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
Huy Nguyenc85023e2017-05-30 09:42:54 +03001763 return;
1764
Mark Bloch0042f9e2018-09-17 13:30:49 +03001765 mlx5_ib_disable_lb(dev, true, false);
Huy Nguyenc85023e2017-05-30 09:42:54 +03001766}
1767
Leon Romanovskya2a074e2019-02-12 20:39:16 +02001768static int mlx5_ib_alloc_ucontext(struct ib_ucontext *uctx,
1769 struct ib_udata *udata)
Eli Cohene126ba92013-07-07 17:25:49 +03001770{
Leon Romanovskya2a074e2019-02-12 20:39:16 +02001771 struct ib_device *ibdev = uctx->device;
Eli Cohene126ba92013-07-07 17:25:49 +03001772 struct mlx5_ib_dev *dev = to_mdev(ibdev);
Matan Barakb368d7c2015-12-15 20:30:12 +02001773 struct mlx5_ib_alloc_ucontext_req_v2 req = {};
1774 struct mlx5_ib_alloc_ucontext_resp resp = {};
Feras Daoud5c99eae2018-01-16 20:08:41 +02001775 struct mlx5_core_dev *mdev = dev->mdev;
Leon Romanovskya2a074e2019-02-12 20:39:16 +02001776 struct mlx5_ib_ucontext *context = to_mucontext(uctx);
Eli Cohen2f5ff262017-01-03 23:55:21 +02001777 struct mlx5_bfreg_info *bfregi;
Eli Cohen78c0f982014-01-30 13:49:48 +02001778 int ver;
Eli Cohene126ba92013-07-07 17:25:49 +03001779 int err;
Majd Dibbinya168a41c2016-01-28 17:51:47 +02001780 size_t min_req_v2 = offsetof(struct mlx5_ib_alloc_ucontext_req_v2,
1781 max_cqe_version);
Yonatan Cohen25bb36e2018-06-19 08:47:24 +03001782 u32 dump_fill_mkey;
Eli Cohenb037c292017-01-03 23:55:26 +02001783 bool lib_uar_4k;
Eli Cohene126ba92013-07-07 17:25:49 +03001784
1785 if (!dev->ib_active)
Leon Romanovskya2a074e2019-02-12 20:39:16 +02001786 return -EAGAIN;
Eli Cohene126ba92013-07-07 17:25:49 +03001787
Amrani, Rame0931112017-06-27 17:04:42 +03001788 if (udata->inlen == sizeof(struct mlx5_ib_alloc_ucontext_req))
Eli Cohen78c0f982014-01-30 13:49:48 +02001789 ver = 0;
Amrani, Rame0931112017-06-27 17:04:42 +03001790 else if (udata->inlen >= min_req_v2)
Eli Cohen78c0f982014-01-30 13:49:48 +02001791 ver = 2;
1792 else
Leon Romanovskya2a074e2019-02-12 20:39:16 +02001793 return -EINVAL;
Eli Cohen78c0f982014-01-30 13:49:48 +02001794
Amrani, Rame0931112017-06-27 17:04:42 +03001795 err = ib_copy_from_udata(&req, udata, min(udata->inlen, sizeof(req)));
Eli Cohene126ba92013-07-07 17:25:49 +03001796 if (err)
Leon Romanovskya2a074e2019-02-12 20:39:16 +02001797 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03001798
Yishai Hadasa8b92ca2018-06-17 12:59:57 +03001799 if (req.flags & ~MLX5_IB_ALLOC_UCTX_DEVX)
Leon Romanovskya2a074e2019-02-12 20:39:16 +02001800 return -EOPNOTSUPP;
Eli Cohen78c0f982014-01-30 13:49:48 +02001801
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001802 if (req.comp_mask || req.reserved0 || req.reserved1 || req.reserved2)
Leon Romanovskya2a074e2019-02-12 20:39:16 +02001803 return -EOPNOTSUPP;
Matan Barakb368d7c2015-12-15 20:30:12 +02001804
Eli Cohen2f5ff262017-01-03 23:55:21 +02001805 req.total_num_bfregs = ALIGN(req.total_num_bfregs,
1806 MLX5_NON_FP_BFREGS_PER_UAR);
1807 if (req.num_low_latency_bfregs > req.total_num_bfregs - 1)
Leon Romanovskya2a074e2019-02-12 20:39:16 +02001808 return -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +03001809
Saeed Mahameed938fe832015-05-28 22:28:41 +03001810 resp.qp_tab_size = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp);
Noa Osherovich2cc6ad52016-06-04 15:15:33 +03001811 if (mlx5_core_is_pf(dev->mdev) && MLX5_CAP_GEN(dev->mdev, bf))
1812 resp.bf_reg_size = 1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size);
Daniel Jurgensb47bd6e2016-10-25 18:36:24 +03001813 resp.cache_line_size = cache_line_size();
Saeed Mahameed938fe832015-05-28 22:28:41 +03001814 resp.max_sq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq);
1815 resp.max_rq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq);
1816 resp.max_send_wqebb = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1817 resp.max_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1818 resp.max_srq_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz);
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001819 resp.cqe_version = min_t(__u8,
1820 (__u8)MLX5_CAP_GEN(dev->mdev, cqe_version),
1821 req.max_cqe_version);
Eli Cohen30aa60b2017-01-03 23:55:27 +02001822 resp.log_uar_size = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
1823 MLX5_ADAPTER_PAGE_SHIFT : PAGE_SHIFT;
1824 resp.num_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
1825 MLX5_CAP_GEN(dev->mdev, num_of_uars_per_page) : 1;
Matan Barakb368d7c2015-12-15 20:30:12 +02001826 resp.response_length = min(offsetof(typeof(resp), response_length) +
1827 sizeof(resp.response_length), udata->outlen);
Eli Cohene126ba92013-07-07 17:25:49 +03001828
Matan Barakc03faa52018-03-28 09:27:54 +03001829 if (mlx5_accel_ipsec_device_caps(dev->mdev) & MLX5_ACCEL_IPSEC_CAP_DEVICE) {
1830 if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_EGRESS))
1831 resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM;
1832 if (mlx5_accel_ipsec_device_caps(dev->mdev) & MLX5_ACCEL_IPSEC_CAP_REQUIRED_METADATA)
1833 resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_REQ_METADATA;
1834 if (MLX5_CAP_FLOWTABLE(dev->mdev, flow_table_properties_nic_receive.ft_field_support.outer_esp_spi))
1835 resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_SPI_STEERING;
1836 if (mlx5_accel_ipsec_device_caps(dev->mdev) & MLX5_ACCEL_IPSEC_CAP_TX_IV_IS_ESN)
1837 resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_TX_IV_IS_ESN;
1838 /* MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_FULL_OFFLOAD is currently always 0 */
1839 }
1840
Eli Cohen30aa60b2017-01-03 23:55:27 +02001841 lib_uar_4k = req.lib_caps & MLX5_LIB_CAP_4K_UAR;
Eli Cohen2f5ff262017-01-03 23:55:21 +02001842 bfregi = &context->bfregi;
Eli Cohenb037c292017-01-03 23:55:26 +02001843
1844 /* updates req->total_num_bfregs */
Yishai Hadas31a78a52017-12-24 16:31:34 +02001845 err = calc_total_bfregs(dev, lib_uar_4k, &req, bfregi);
Eli Cohenb037c292017-01-03 23:55:26 +02001846 if (err)
1847 goto out_ctx;
1848
Eli Cohen2f5ff262017-01-03 23:55:21 +02001849 mutex_init(&bfregi->lock);
Eli Cohenb037c292017-01-03 23:55:26 +02001850 bfregi->lib_uar_4k = lib_uar_4k;
Yishai Hadas31a78a52017-12-24 16:31:34 +02001851 bfregi->count = kcalloc(bfregi->total_num_bfregs, sizeof(*bfregi->count),
Eli Cohenb037c292017-01-03 23:55:26 +02001852 GFP_KERNEL);
1853 if (!bfregi->count) {
Eli Cohene126ba92013-07-07 17:25:49 +03001854 err = -ENOMEM;
1855 goto out_ctx;
1856 }
1857
Eli Cohenb037c292017-01-03 23:55:26 +02001858 bfregi->sys_pages = kcalloc(bfregi->num_sys_pages,
1859 sizeof(*bfregi->sys_pages),
1860 GFP_KERNEL);
1861 if (!bfregi->sys_pages) {
Eli Cohene126ba92013-07-07 17:25:49 +03001862 err = -ENOMEM;
Eli Cohenb037c292017-01-03 23:55:26 +02001863 goto out_count;
Eli Cohene126ba92013-07-07 17:25:49 +03001864 }
1865
Eli Cohenb037c292017-01-03 23:55:26 +02001866 err = allocate_uars(dev, context);
1867 if (err)
1868 goto out_sys_pages;
Eli Cohene126ba92013-07-07 17:25:49 +03001869
Leon Romanovsky13859d5d2019-01-08 16:07:26 +02001870 if (ibdev->attrs.device_cap_flags & IB_DEVICE_ON_DEMAND_PAGING)
1871 context->ibucontext.invalidate_range =
1872 &mlx5_ib_invalidate_range;
Haggai Eranb4cfe442014-12-11 17:04:26 +02001873
Yishai Hadasa8b92ca2018-06-17 12:59:57 +03001874 if (req.flags & MLX5_IB_ALLOC_UCTX_DEVX) {
Yishai Hadasfb981532018-11-26 08:28:36 +02001875 err = mlx5_ib_devx_create(dev, true);
Yishai Hadas76dc5a82018-09-20 21:45:19 +03001876 if (err < 0)
Yishai Hadasd2d19122018-09-20 21:39:32 +03001877 goto out_uars;
Yishai Hadas76dc5a82018-09-20 21:45:19 +03001878 context->devx_uid = err;
Yishai Hadasa8b92ca2018-06-17 12:59:57 +03001879 }
1880
Yishai Hadasd2d19122018-09-20 21:39:32 +03001881 err = mlx5_ib_alloc_transport_domain(dev, &context->tdn,
1882 context->devx_uid);
1883 if (err)
1884 goto out_devx;
1885
Yonatan Cohen25bb36e2018-06-19 08:47:24 +03001886 if (MLX5_CAP_GEN(dev->mdev, dump_fill_mkey)) {
1887 err = mlx5_cmd_dump_fill_mkey(dev->mdev, &dump_fill_mkey);
1888 if (err)
Jason Gunthorpe8193abb2018-07-04 13:19:46 -06001889 goto out_mdev;
Yonatan Cohen25bb36e2018-06-19 08:47:24 +03001890 }
1891
Eli Cohene126ba92013-07-07 17:25:49 +03001892 INIT_LIST_HEAD(&context->db_page_list);
1893 mutex_init(&context->db_page_mutex);
1894
Eli Cohen2f5ff262017-01-03 23:55:21 +02001895 resp.tot_bfregs = req.total_num_bfregs;
Daniel Jurgens508562d2018-01-04 17:25:34 +02001896 resp.num_ports = dev->num_ports;
Matan Barakb368d7c2015-12-15 20:30:12 +02001897
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001898 if (field_avail(typeof(resp), cqe_version, udata->outlen))
1899 resp.response_length += sizeof(resp.cqe_version);
Matan Barakb368d7c2015-12-15 20:30:12 +02001900
Bodong Wang402ca532016-06-17 15:02:20 +03001901 if (field_avail(typeof(resp), cmds_supp_uhw, udata->outlen)) {
Moni Shoua6ad279c52016-11-23 08:23:23 +02001902 resp.cmds_supp_uhw |= MLX5_USER_CMDS_SUPP_UHW_QUERY_DEVICE |
1903 MLX5_USER_CMDS_SUPP_UHW_CREATE_AH;
Bodong Wang402ca532016-06-17 15:02:20 +03001904 resp.response_length += sizeof(resp.cmds_supp_uhw);
1905 }
1906
Or Gerlitz78984892016-11-30 20:33:33 +02001907 if (field_avail(typeof(resp), eth_min_inline, udata->outlen)) {
1908 if (mlx5_ib_port_link_layer(ibdev, 1) == IB_LINK_LAYER_ETHERNET) {
1909 mlx5_query_min_inline(dev->mdev, &resp.eth_min_inline);
1910 resp.eth_min_inline++;
1911 }
1912 resp.response_length += sizeof(resp.eth_min_inline);
1913 }
1914
Feras Daoud5c99eae2018-01-16 20:08:41 +02001915 if (field_avail(typeof(resp), clock_info_versions, udata->outlen)) {
1916 if (mdev->clock_info)
1917 resp.clock_info_versions = BIT(MLX5_IB_CLOCK_INFO_V1);
1918 resp.response_length += sizeof(resp.clock_info_versions);
1919 }
1920
Noa Osherovichbc5c6ee2016-06-04 15:15:31 +03001921 /*
1922 * We don't want to expose information from the PCI bar that is located
1923 * after 4096 bytes, so if the arch only supports larger pages, let's
1924 * pretend we don't support reading the HCA's core clock. This is also
1925 * forced by mmap function.
1926 */
Eli Cohende8d6e02017-01-03 23:55:19 +02001927 if (field_avail(typeof(resp), hca_core_clock_offset, udata->outlen)) {
1928 if (PAGE_SIZE <= 4096) {
1929 resp.comp_mask |=
1930 MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_CORE_CLOCK_OFFSET;
1931 resp.hca_core_clock_offset =
1932 offsetof(struct mlx5_init_seg, internal_timer_h) % PAGE_SIZE;
1933 }
Feras Daoud5c99eae2018-01-16 20:08:41 +02001934 resp.response_length += sizeof(resp.hca_core_clock_offset);
Matan Barakb368d7c2015-12-15 20:30:12 +02001935 }
1936
Eli Cohen30aa60b2017-01-03 23:55:27 +02001937 if (field_avail(typeof(resp), log_uar_size, udata->outlen))
1938 resp.response_length += sizeof(resp.log_uar_size);
1939
1940 if (field_avail(typeof(resp), num_uars_per_page, udata->outlen))
1941 resp.response_length += sizeof(resp.num_uars_per_page);
1942
Yishai Hadas31a78a52017-12-24 16:31:34 +02001943 if (field_avail(typeof(resp), num_dyn_bfregs, udata->outlen)) {
1944 resp.num_dyn_bfregs = bfregi->num_dyn_bfregs;
1945 resp.response_length += sizeof(resp.num_dyn_bfregs);
1946 }
1947
Yonatan Cohen25bb36e2018-06-19 08:47:24 +03001948 if (field_avail(typeof(resp), dump_fill_mkey, udata->outlen)) {
1949 if (MLX5_CAP_GEN(dev->mdev, dump_fill_mkey)) {
1950 resp.dump_fill_mkey = dump_fill_mkey;
1951 resp.comp_mask |=
1952 MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_DUMP_FILL_MKEY;
1953 }
1954 resp.response_length += sizeof(resp.dump_fill_mkey);
1955 }
1956
Matan Barakb368d7c2015-12-15 20:30:12 +02001957 err = ib_copy_to_udata(udata, &resp, resp.response_length);
Eli Cohene126ba92013-07-07 17:25:49 +03001958 if (err)
Yishai Hadasa8b92ca2018-06-17 12:59:57 +03001959 goto out_mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03001960
Eli Cohen2f5ff262017-01-03 23:55:21 +02001961 bfregi->ver = ver;
1962 bfregi->num_low_latency_bfregs = req.num_low_latency_bfregs;
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001963 context->cqe_version = resp.cqe_version;
Eli Cohen30aa60b2017-01-03 23:55:27 +02001964 context->lib_caps = req.lib_caps;
1965 print_lib_caps(dev, context->lib_caps);
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001966
Aviv Heller7c34ec12018-08-23 13:47:53 +03001967 if (dev->lag_active) {
Mark Bloch95579e72019-03-28 15:27:33 +02001968 u8 port = mlx5_core_native_port_num(dev->mdev) - 1;
Majd Dibbinyc6a21c32018-08-28 14:29:05 +03001969
1970 atomic_set(&context->tx_port_affinity,
1971 atomic_add_return(
Mark Bloch95579e72019-03-28 15:27:33 +02001972 1, &dev->port[port].roce.tx_port_affinity));
Majd Dibbinyc6a21c32018-08-28 14:29:05 +03001973 }
1974
Leon Romanovskya2a074e2019-02-12 20:39:16 +02001975 return 0;
Eli Cohene126ba92013-07-07 17:25:49 +03001976
Yishai Hadasa8b92ca2018-06-17 12:59:57 +03001977out_mdev:
Yishai Hadasd2d19122018-09-20 21:39:32 +03001978 mlx5_ib_dealloc_transport_domain(dev, context->tdn, context->devx_uid);
1979out_devx:
Yishai Hadasa8b92ca2018-06-17 12:59:57 +03001980 if (req.flags & MLX5_IB_ALLOC_UCTX_DEVX)
Yishai Hadas76dc5a82018-09-20 21:45:19 +03001981 mlx5_ib_devx_destroy(dev, context->devx_uid);
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001982
Eli Cohene126ba92013-07-07 17:25:49 +03001983out_uars:
Eli Cohenb037c292017-01-03 23:55:26 +02001984 deallocate_uars(dev, context);
1985
1986out_sys_pages:
1987 kfree(bfregi->sys_pages);
1988
Eli Cohene126ba92013-07-07 17:25:49 +03001989out_count:
Eli Cohen2f5ff262017-01-03 23:55:21 +02001990 kfree(bfregi->count);
Eli Cohene126ba92013-07-07 17:25:49 +03001991
Eli Cohene126ba92013-07-07 17:25:49 +03001992out_ctx:
Leon Romanovskya2a074e2019-02-12 20:39:16 +02001993 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03001994}
1995
Leon Romanovskya2a074e2019-02-12 20:39:16 +02001996static void mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
Eli Cohene126ba92013-07-07 17:25:49 +03001997{
1998 struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1999 struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
Eli Cohenb037c292017-01-03 23:55:26 +02002000 struct mlx5_bfreg_info *bfregi;
Eli Cohene126ba92013-07-07 17:25:49 +03002001
Jason Gunthorpef27a0d52018-09-16 20:48:08 +03002002 /* All umem's must be destroyed before destroying the ucontext. */
2003 mutex_lock(&ibcontext->per_mm_list_lock);
2004 WARN_ON(!list_empty(&ibcontext->per_mm_list));
2005 mutex_unlock(&ibcontext->per_mm_list_lock);
Yishai Hadasa8b92ca2018-06-17 12:59:57 +03002006
Eli Cohenb037c292017-01-03 23:55:26 +02002007 bfregi = &context->bfregi;
Yishai Hadasd2d19122018-09-20 21:39:32 +03002008 mlx5_ib_dealloc_transport_domain(dev, context->tdn, context->devx_uid);
2009
Eli Cohenb037c292017-01-03 23:55:26 +02002010 if (context->devx_uid)
Yishai Hadas76dc5a82018-09-20 21:45:19 +03002011 mlx5_ib_devx_destroy(dev, context->devx_uid);
Eli Cohene126ba92013-07-07 17:25:49 +03002012
2013 deallocate_uars(dev, context);
Eli Cohen2f5ff262017-01-03 23:55:21 +02002014 kfree(bfregi->sys_pages);
2015 kfree(bfregi->count);
Eli Cohene126ba92013-07-07 17:25:49 +03002016}
2017
2018static phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev,
2019 int uar_idx)
2020{
Eli Cohenb037c292017-01-03 23:55:26 +02002021 int fw_uars_per_page;
2022
2023 fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ? MLX5_UARS_IN_PAGE : 1;
2024
Huy Nguyenaa8106f2019-03-29 15:38:01 -07002025 return (dev->mdev->bar_addr >> PAGE_SHIFT) + uar_idx / fw_uars_per_page;
Eli Cohene126ba92013-07-07 17:25:49 +03002026}
2027
2028static int get_command(unsigned long offset)
2029{
2030 return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK;
2031}
2032
2033static int get_arg(unsigned long offset)
2034{
2035 return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1);
2036}
2037
2038static int get_index(unsigned long offset)
2039{
2040 return get_arg(offset);
2041}
2042
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002043/* Index resides in an extra byte to enable larger values than 255 */
2044static int get_extended_index(unsigned long offset)
2045{
2046 return get_arg(offset) | ((offset >> 16) & 0xff) << 8;
2047}
2048
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03002049
2050static void mlx5_ib_disassociate_ucontext(struct ib_ucontext *ibcontext)
2051{
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03002052}
2053
Guy Levi37aa5c32016-04-27 16:49:50 +03002054static inline char *mmap_cmd2str(enum mlx5_ib_mmap_cmd cmd)
2055{
2056 switch (cmd) {
2057 case MLX5_IB_MMAP_WC_PAGE:
2058 return "WC";
2059 case MLX5_IB_MMAP_REGULAR_PAGE:
2060 return "best effort WC";
2061 case MLX5_IB_MMAP_NC_PAGE:
2062 return "NC";
Ariel Levkovich24da0012018-04-05 18:53:27 +03002063 case MLX5_IB_MMAP_DEVICE_MEM:
2064 return "Device Memory";
Guy Levi37aa5c32016-04-27 16:49:50 +03002065 default:
2066 return NULL;
2067 }
2068}
2069
Feras Daoud5c99eae2018-01-16 20:08:41 +02002070static int mlx5_ib_mmap_clock_info_page(struct mlx5_ib_dev *dev,
2071 struct vm_area_struct *vma,
2072 struct mlx5_ib_ucontext *context)
2073{
Jason Gunthorpe4eb6ab12019-04-16 14:07:30 +03002074 if ((vma->vm_end - vma->vm_start != PAGE_SIZE) ||
2075 !(vma->vm_flags & VM_SHARED))
Feras Daoud5c99eae2018-01-16 20:08:41 +02002076 return -EINVAL;
2077
2078 if (get_index(vma->vm_pgoff) != MLX5_IB_CLOCK_INFO_V1)
2079 return -EOPNOTSUPP;
2080
Jason Gunthorpe4eb6ab12019-04-16 14:07:30 +03002081 if (vma->vm_flags & (VM_WRITE | VM_EXEC))
Feras Daoud5c99eae2018-01-16 20:08:41 +02002082 return -EPERM;
Jason Gunthorpec6601332019-04-16 14:07:25 +03002083 vma->vm_flags &= ~VM_MAYWRITE;
Feras Daoud5c99eae2018-01-16 20:08:41 +02002084
Jason Gunthorpeddcdc362019-04-16 14:07:29 +03002085 if (!dev->mdev->clock_info)
Feras Daoud5c99eae2018-01-16 20:08:41 +02002086 return -EOPNOTSUPP;
2087
Jason Gunthorpe4eb6ab12019-04-16 14:07:30 +03002088 return vm_insert_page(vma, vma->vm_start,
2089 virt_to_page(dev->mdev->clock_info));
Feras Daoud5c99eae2018-01-16 20:08:41 +02002090}
2091
Guy Levi37aa5c32016-04-27 16:49:50 +03002092static int uar_mmap(struct mlx5_ib_dev *dev, enum mlx5_ib_mmap_cmd cmd,
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03002093 struct vm_area_struct *vma,
2094 struct mlx5_ib_ucontext *context)
Guy Levi37aa5c32016-04-27 16:49:50 +03002095{
Eli Cohen2f5ff262017-01-03 23:55:21 +02002096 struct mlx5_bfreg_info *bfregi = &context->bfregi;
Guy Levi37aa5c32016-04-27 16:49:50 +03002097 int err;
2098 unsigned long idx;
Kamal Heibaa09ea62018-07-19 00:05:32 +03002099 phys_addr_t pfn;
Guy Levi37aa5c32016-04-27 16:49:50 +03002100 pgprot_t prot;
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002101 u32 bfreg_dyn_idx = 0;
2102 u32 uar_index;
2103 int dyn_uar = (cmd == MLX5_IB_MMAP_ALLOC_WC);
2104 int max_valid_idx = dyn_uar ? bfregi->num_sys_pages :
2105 bfregi->num_static_sys_pages;
Eli Cohenb037c292017-01-03 23:55:26 +02002106
2107 if (vma->vm_end - vma->vm_start != PAGE_SIZE)
2108 return -EINVAL;
2109
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002110 if (dyn_uar)
2111 idx = get_extended_index(vma->vm_pgoff) + bfregi->num_static_sys_pages;
2112 else
2113 idx = get_index(vma->vm_pgoff);
2114
2115 if (idx >= max_valid_idx) {
2116 mlx5_ib_warn(dev, "invalid uar index %lu, max=%d\n",
2117 idx, max_valid_idx);
Eli Cohenb037c292017-01-03 23:55:26 +02002118 return -EINVAL;
2119 }
Guy Levi37aa5c32016-04-27 16:49:50 +03002120
2121 switch (cmd) {
2122 case MLX5_IB_MMAP_WC_PAGE:
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002123 case MLX5_IB_MMAP_ALLOC_WC:
Guy Levi37aa5c32016-04-27 16:49:50 +03002124/* Some architectures don't support WC memory */
2125#if defined(CONFIG_X86)
2126 if (!pat_enabled())
2127 return -EPERM;
2128#elif !(defined(CONFIG_PPC) || (defined(CONFIG_ARM) && defined(CONFIG_MMU)))
2129 return -EPERM;
2130#endif
2131 /* fall through */
2132 case MLX5_IB_MMAP_REGULAR_PAGE:
2133 /* For MLX5_IB_MMAP_REGULAR_PAGE do the best effort to get WC */
2134 prot = pgprot_writecombine(vma->vm_page_prot);
2135 break;
2136 case MLX5_IB_MMAP_NC_PAGE:
2137 prot = pgprot_noncached(vma->vm_page_prot);
2138 break;
2139 default:
2140 return -EINVAL;
2141 }
2142
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002143 if (dyn_uar) {
2144 int uars_per_page;
2145
2146 uars_per_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k);
2147 bfreg_dyn_idx = idx * (uars_per_page * MLX5_NON_FP_BFREGS_PER_UAR);
2148 if (bfreg_dyn_idx >= bfregi->total_num_bfregs) {
2149 mlx5_ib_warn(dev, "invalid bfreg_dyn_idx %u, max=%u\n",
2150 bfreg_dyn_idx, bfregi->total_num_bfregs);
2151 return -EINVAL;
2152 }
2153
2154 mutex_lock(&bfregi->lock);
2155 /* Fail if uar already allocated, first bfreg index of each
2156 * page holds its count.
2157 */
2158 if (bfregi->count[bfreg_dyn_idx]) {
2159 mlx5_ib_warn(dev, "wrong offset, idx %lu is busy, bfregn=%u\n", idx, bfreg_dyn_idx);
2160 mutex_unlock(&bfregi->lock);
2161 return -EINVAL;
2162 }
2163
2164 bfregi->count[bfreg_dyn_idx]++;
2165 mutex_unlock(&bfregi->lock);
2166
2167 err = mlx5_cmd_alloc_uar(dev->mdev, &uar_index);
2168 if (err) {
2169 mlx5_ib_warn(dev, "UAR alloc failed\n");
2170 goto free_bfreg;
2171 }
2172 } else {
2173 uar_index = bfregi->sys_pages[idx];
2174 }
2175
2176 pfn = uar_index2pfn(dev, uar_index);
Guy Levi37aa5c32016-04-27 16:49:50 +03002177 mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn %pa\n", idx, &pfn);
2178
Jason Gunthorpee2cd1d12018-09-16 20:43:10 +03002179 err = rdma_user_mmap_io(&context->ibucontext, vma, pfn, PAGE_SIZE,
2180 prot);
Guy Levi37aa5c32016-04-27 16:49:50 +03002181 if (err) {
Leon Romanovsky8f062282018-05-22 08:31:03 +03002182 mlx5_ib_err(dev,
Jason Gunthorpee2cd1d12018-09-16 20:43:10 +03002183 "rdma_user_mmap_io failed with error=%d, mmap_cmd=%s\n",
Leon Romanovsky8f062282018-05-22 08:31:03 +03002184 err, mmap_cmd2str(cmd));
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002185 goto err;
Guy Levi37aa5c32016-04-27 16:49:50 +03002186 }
2187
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002188 if (dyn_uar)
2189 bfregi->sys_pages[idx] = uar_index;
2190 return 0;
2191
2192err:
2193 if (!dyn_uar)
2194 return err;
2195
2196 mlx5_cmd_free_uar(dev->mdev, idx);
2197
2198free_bfreg:
2199 mlx5_ib_free_bfreg(dev, bfregi, bfreg_dyn_idx);
2200
2201 return err;
Guy Levi37aa5c32016-04-27 16:49:50 +03002202}
2203
Ariel Levkovich24da0012018-04-05 18:53:27 +03002204static int dm_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
2205{
2206 struct mlx5_ib_ucontext *mctx = to_mucontext(context);
2207 struct mlx5_ib_dev *dev = to_mdev(context->device);
2208 u16 page_idx = get_extended_index(vma->vm_pgoff);
2209 size_t map_size = vma->vm_end - vma->vm_start;
2210 u32 npages = map_size >> PAGE_SHIFT;
2211 phys_addr_t pfn;
Ariel Levkovich24da0012018-04-05 18:53:27 +03002212
2213 if (find_next_zero_bit(mctx->dm_pages, page_idx + npages, page_idx) !=
2214 page_idx + npages)
2215 return -EINVAL;
2216
Huy Nguyenaa8106f2019-03-29 15:38:01 -07002217 pfn = ((dev->mdev->bar_addr +
Ariel Levkovich24da0012018-04-05 18:53:27 +03002218 MLX5_CAP64_DEV_MEM(dev->mdev, memic_bar_start_addr)) >>
2219 PAGE_SHIFT) +
2220 page_idx;
Jason Gunthorpee2cd1d12018-09-16 20:43:10 +03002221 return rdma_user_mmap_io(context, vma, pfn, map_size,
2222 pgprot_writecombine(vma->vm_page_prot));
Ariel Levkovich24da0012018-04-05 18:53:27 +03002223}
2224
Eli Cohene126ba92013-07-07 17:25:49 +03002225static int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma)
2226{
2227 struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
2228 struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
Eli Cohene126ba92013-07-07 17:25:49 +03002229 unsigned long command;
Eli Cohene126ba92013-07-07 17:25:49 +03002230 phys_addr_t pfn;
2231
2232 command = get_command(vma->vm_pgoff);
2233 switch (command) {
Guy Levi37aa5c32016-04-27 16:49:50 +03002234 case MLX5_IB_MMAP_WC_PAGE:
2235 case MLX5_IB_MMAP_NC_PAGE:
Eli Cohene126ba92013-07-07 17:25:49 +03002236 case MLX5_IB_MMAP_REGULAR_PAGE:
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002237 case MLX5_IB_MMAP_ALLOC_WC:
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03002238 return uar_mmap(dev, command, vma, context);
Eli Cohene126ba92013-07-07 17:25:49 +03002239
2240 case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES:
2241 return -ENOSYS;
2242
Matan Barakd69e3bc2015-12-15 20:30:13 +02002243 case MLX5_IB_MMAP_CORE_CLOCK:
Matan Barakd69e3bc2015-12-15 20:30:13 +02002244 if (vma->vm_end - vma->vm_start != PAGE_SIZE)
2245 return -EINVAL;
2246
Matan Barak6cbac1e2016-04-14 16:52:10 +03002247 if (vma->vm_flags & VM_WRITE)
Matan Barakd69e3bc2015-12-15 20:30:13 +02002248 return -EPERM;
Jason Gunthorpec6601332019-04-16 14:07:25 +03002249 vma->vm_flags &= ~VM_MAYWRITE;
Matan Barakd69e3bc2015-12-15 20:30:13 +02002250
2251 /* Don't expose to user-space information it shouldn't have */
2252 if (PAGE_SIZE > 4096)
2253 return -EOPNOTSUPP;
2254
Matan Barakd69e3bc2015-12-15 20:30:13 +02002255 pfn = (dev->mdev->iseg_base +
2256 offsetof(struct mlx5_init_seg, internal_timer_h)) >>
2257 PAGE_SHIFT;
Jason Gunthorped5e560d2019-04-16 14:07:26 +03002258 return rdma_user_mmap_io(&context->ibucontext, vma, pfn,
2259 PAGE_SIZE,
2260 pgprot_noncached(vma->vm_page_prot));
Feras Daoud5c99eae2018-01-16 20:08:41 +02002261 case MLX5_IB_MMAP_CLOCK_INFO:
2262 return mlx5_ib_mmap_clock_info_page(dev, vma, context);
Matan Barakd69e3bc2015-12-15 20:30:13 +02002263
Ariel Levkovich24da0012018-04-05 18:53:27 +03002264 case MLX5_IB_MMAP_DEVICE_MEM:
2265 return dm_mmap(ibcontext, vma);
2266
Eli Cohene126ba92013-07-07 17:25:49 +03002267 default:
2268 return -EINVAL;
2269 }
2270
2271 return 0;
2272}
2273
Ariel Levkovich25c13322019-05-05 17:07:13 +03002274static inline int check_dm_type_support(struct mlx5_ib_dev *dev,
2275 u32 type)
Ariel Levkovich24da0012018-04-05 18:53:27 +03002276{
Ariel Levkovich25c13322019-05-05 17:07:13 +03002277 switch (type) {
2278 case MLX5_IB_UAPI_DM_TYPE_MEMIC:
2279 if (!MLX5_CAP_DEV_MEM(dev->mdev, memic))
2280 return -EOPNOTSUPP;
2281 break;
2282 case MLX5_IB_UAPI_DM_TYPE_STEERING_SW_ICM:
2283 if (!capable(CAP_SYS_RAWIO) ||
2284 !capable(CAP_NET_RAW))
2285 return -EPERM;
2286
2287 if (!(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, sw_owner) ||
2288 MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev, sw_owner)))
2289 return -EOPNOTSUPP;
2290 break;
2291 }
2292
2293 return 0;
2294}
2295
Ariel Levkovich3b113a12019-05-05 17:07:11 +03002296static int handle_alloc_dm_memic(struct ib_ucontext *ctx,
2297 struct mlx5_ib_dm *dm,
2298 struct ib_dm_alloc_attr *attr,
2299 struct uverbs_attr_bundle *attrs)
Ariel Levkovich24da0012018-04-05 18:53:27 +03002300{
Ariel Levkovich3b113a12019-05-05 17:07:11 +03002301 struct mlx5_dm *dm_db = &to_mdev(ctx->device)->dm;
Ariel Levkovich24da0012018-04-05 18:53:27 +03002302 u64 start_offset;
2303 u32 page_idx;
2304 int err;
2305
Ariel Levkovich3b113a12019-05-05 17:07:11 +03002306 dm->size = roundup(attr->length, MLX5_MEMIC_BASE_SIZE);
Ariel Levkovich24da0012018-04-05 18:53:27 +03002307
Ariel Levkovich3b113a12019-05-05 17:07:11 +03002308 err = mlx5_cmd_alloc_memic(dm_db, &dm->dev_addr,
2309 dm->size, attr->alignment);
Ariel Levkovich24da0012018-04-05 18:53:27 +03002310 if (err)
Ariel Levkovich3b113a12019-05-05 17:07:11 +03002311 return err;
Ariel Levkovich24da0012018-04-05 18:53:27 +03002312
Ariel Levkovich3b113a12019-05-05 17:07:11 +03002313 page_idx = (dm->dev_addr - pci_resource_start(dm_db->dev->pdev, 0) -
2314 MLX5_CAP64_DEV_MEM(dm_db->dev, memic_bar_start_addr)) >>
Ariel Levkovich24da0012018-04-05 18:53:27 +03002315 PAGE_SHIFT;
2316
2317 err = uverbs_copy_to(attrs,
Ariel Levkovich24da0012018-04-05 18:53:27 +03002318 MLX5_IB_ATTR_ALLOC_DM_RESP_PAGE_INDEX,
2319 &page_idx, sizeof(page_idx));
2320 if (err)
2321 goto err_dealloc;
2322
Ariel Levkovich3b113a12019-05-05 17:07:11 +03002323 start_offset = dm->dev_addr & ~PAGE_MASK;
2324 err = uverbs_copy_to(attrs,
2325 MLX5_IB_ATTR_ALLOC_DM_RESP_START_OFFSET,
2326 &start_offset, sizeof(start_offset));
2327 if (err)
2328 goto err_dealloc;
Ariel Levkovich24da0012018-04-05 18:53:27 +03002329
Ariel Levkovich3b113a12019-05-05 17:07:11 +03002330 bitmap_set(to_mucontext(ctx)->dm_pages, page_idx,
2331 DIV_ROUND_UP(dm->size, PAGE_SIZE));
2332
2333 return 0;
2334
2335err_dealloc:
2336 mlx5_cmd_dealloc_memic(dm_db, dm->dev_addr, dm->size);
2337
2338 return err;
2339}
2340
Ariel Levkovich25c13322019-05-05 17:07:13 +03002341static int handle_alloc_dm_sw_icm(struct ib_ucontext *ctx,
2342 struct mlx5_ib_dm *dm,
2343 struct ib_dm_alloc_attr *attr,
2344 struct uverbs_attr_bundle *attrs,
2345 int type)
2346{
2347 struct mlx5_dm *dm_db = &to_mdev(ctx->device)->dm;
2348 u64 act_size;
2349 int err;
2350
2351 /* Allocation size must a multiple of the basic block size
2352 * and a power of 2.
2353 */
Michal Kubecek37eb86c2019-05-20 13:19:02 +02002354 act_size = round_up(attr->length, MLX5_SW_ICM_BLOCK_SIZE(dm_db->dev));
Ariel Levkovich25c13322019-05-05 17:07:13 +03002355 act_size = roundup_pow_of_two(act_size);
2356
2357 dm->size = act_size;
2358 err = mlx5_cmd_alloc_sw_icm(dm_db, type, act_size,
2359 to_mucontext(ctx)->devx_uid, &dm->dev_addr,
2360 &dm->icm_dm.obj_id);
2361 if (err)
2362 return err;
2363
2364 err = uverbs_copy_to(attrs,
2365 MLX5_IB_ATTR_ALLOC_DM_RESP_START_OFFSET,
2366 &dm->dev_addr, sizeof(dm->dev_addr));
2367 if (err)
2368 mlx5_cmd_dealloc_sw_icm(dm_db, type, dm->size,
2369 to_mucontext(ctx)->devx_uid,
2370 dm->dev_addr, dm->icm_dm.obj_id);
2371
2372 return err;
2373}
2374
Ariel Levkovich3b113a12019-05-05 17:07:11 +03002375struct ib_dm *mlx5_ib_alloc_dm(struct ib_device *ibdev,
2376 struct ib_ucontext *context,
2377 struct ib_dm_alloc_attr *attr,
2378 struct uverbs_attr_bundle *attrs)
2379{
2380 struct mlx5_ib_dm *dm;
2381 enum mlx5_ib_uapi_dm_type type;
2382 int err;
2383
2384 err = uverbs_get_const_default(&type, attrs,
2385 MLX5_IB_ATTR_ALLOC_DM_REQ_TYPE,
2386 MLX5_IB_UAPI_DM_TYPE_MEMIC);
2387 if (err)
2388 return ERR_PTR(err);
2389
2390 mlx5_ib_dbg(to_mdev(ibdev), "alloc_dm req: dm_type=%d user_length=0x%llx log_alignment=%d\n",
2391 type, attr->length, attr->alignment);
2392
Ariel Levkovich25c13322019-05-05 17:07:13 +03002393 err = check_dm_type_support(to_mdev(ibdev), type);
2394 if (err)
2395 return ERR_PTR(err);
2396
Ariel Levkovich3b113a12019-05-05 17:07:11 +03002397 dm = kzalloc(sizeof(*dm), GFP_KERNEL);
2398 if (!dm)
2399 return ERR_PTR(-ENOMEM);
2400
2401 dm->type = type;
2402
2403 switch (type) {
2404 case MLX5_IB_UAPI_DM_TYPE_MEMIC:
2405 err = handle_alloc_dm_memic(context, dm,
2406 attr,
2407 attrs);
2408 break;
Ariel Levkovich25c13322019-05-05 17:07:13 +03002409 case MLX5_IB_UAPI_DM_TYPE_STEERING_SW_ICM:
2410 case MLX5_IB_UAPI_DM_TYPE_HEADER_MODIFY_SW_ICM:
2411 err = handle_alloc_dm_sw_icm(context, dm, attr, attrs, type);
2412 break;
Ariel Levkovich3b113a12019-05-05 17:07:11 +03002413 default:
2414 err = -EOPNOTSUPP;
2415 }
2416
2417 if (err)
2418 goto err_free;
Ariel Levkovich24da0012018-04-05 18:53:27 +03002419
2420 return &dm->ibdm;
2421
Ariel Levkovich24da0012018-04-05 18:53:27 +03002422err_free:
2423 kfree(dm);
2424 return ERR_PTR(err);
2425}
2426
Shamir Rabinovitchc4367a22019-03-31 19:10:05 +03002427int mlx5_ib_dealloc_dm(struct ib_dm *ibdm, struct uverbs_attr_bundle *attrs)
Ariel Levkovich24da0012018-04-05 18:53:27 +03002428{
Ariel Levkovich25c13322019-05-05 17:07:13 +03002429 struct mlx5_ib_ucontext *ctx = rdma_udata_to_drv_context(
2430 &attrs->driver_udata, struct mlx5_ib_ucontext, ibucontext);
Ariel Levkovich3b113a12019-05-05 17:07:11 +03002431 struct mlx5_dm *dm_db = &to_mdev(ibdm->device)->dm;
Ariel Levkovich24da0012018-04-05 18:53:27 +03002432 struct mlx5_ib_dm *dm = to_mdm(ibdm);
Ariel Levkovich24da0012018-04-05 18:53:27 +03002433 u32 page_idx;
2434 int ret;
2435
Ariel Levkovich3b113a12019-05-05 17:07:11 +03002436 switch (dm->type) {
2437 case MLX5_IB_UAPI_DM_TYPE_MEMIC:
2438 ret = mlx5_cmd_dealloc_memic(dm_db, dm->dev_addr, dm->size);
2439 if (ret)
2440 return ret;
Ariel Levkovich24da0012018-04-05 18:53:27 +03002441
Ariel Levkovich3b113a12019-05-05 17:07:11 +03002442 page_idx = (dm->dev_addr -
2443 pci_resource_start(dm_db->dev->pdev, 0) -
2444 MLX5_CAP64_DEV_MEM(dm_db->dev,
2445 memic_bar_start_addr)) >>
2446 PAGE_SHIFT;
Ariel Levkovich25c13322019-05-05 17:07:13 +03002447 bitmap_clear(ctx->dm_pages, page_idx,
2448 DIV_ROUND_UP(dm->size, PAGE_SIZE));
2449 break;
2450 case MLX5_IB_UAPI_DM_TYPE_STEERING_SW_ICM:
2451 case MLX5_IB_UAPI_DM_TYPE_HEADER_MODIFY_SW_ICM:
2452 ret = mlx5_cmd_dealloc_sw_icm(dm_db, dm->type, dm->size,
2453 ctx->devx_uid, dm->dev_addr,
2454 dm->icm_dm.obj_id);
2455 if (ret)
2456 return ret;
Ariel Levkovich3b113a12019-05-05 17:07:11 +03002457 break;
2458 default:
2459 return -EOPNOTSUPP;
2460 }
Ariel Levkovich24da0012018-04-05 18:53:27 +03002461
2462 kfree(dm);
2463
2464 return 0;
2465}
2466
Shamir Rabinovitchff23dfa2019-03-31 19:10:07 +03002467static int mlx5_ib_alloc_pd(struct ib_pd *ibpd, struct ib_udata *udata)
Eli Cohene126ba92013-07-07 17:25:49 +03002468{
Leon Romanovsky21a428a2019-02-03 14:55:51 +02002469 struct mlx5_ib_pd *pd = to_mpd(ibpd);
2470 struct ib_device *ibdev = ibpd->device;
Eli Cohene126ba92013-07-07 17:25:49 +03002471 struct mlx5_ib_alloc_pd_resp resp;
Eli Cohene126ba92013-07-07 17:25:49 +03002472 int err;
Yishai Hadasa1069c12018-09-20 21:39:19 +03002473 u32 out[MLX5_ST_SZ_DW(alloc_pd_out)] = {};
2474 u32 in[MLX5_ST_SZ_DW(alloc_pd_in)] = {};
2475 u16 uid = 0;
Shamir Rabinovitchff23dfa2019-03-31 19:10:07 +03002476 struct mlx5_ib_ucontext *context = rdma_udata_to_drv_context(
2477 udata, struct mlx5_ib_ucontext, ibucontext);
Eli Cohene126ba92013-07-07 17:25:49 +03002478
Shamir Rabinovitchff23dfa2019-03-31 19:10:07 +03002479 uid = context ? context->devx_uid : 0;
Yishai Hadasa1069c12018-09-20 21:39:19 +03002480 MLX5_SET(alloc_pd_in, in, opcode, MLX5_CMD_OP_ALLOC_PD);
2481 MLX5_SET(alloc_pd_in, in, uid, uid);
2482 err = mlx5_cmd_exec(to_mdev(ibdev)->mdev, in, sizeof(in),
2483 out, sizeof(out));
Leon Romanovsky21a428a2019-02-03 14:55:51 +02002484 if (err)
2485 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03002486
Yishai Hadasa1069c12018-09-20 21:39:19 +03002487 pd->pdn = MLX5_GET(alloc_pd_out, out, pd);
2488 pd->uid = uid;
Shamir Rabinovitchff23dfa2019-03-31 19:10:07 +03002489 if (udata) {
Eli Cohene126ba92013-07-07 17:25:49 +03002490 resp.pdn = pd->pdn;
2491 if (ib_copy_to_udata(udata, &resp, sizeof(resp))) {
Yishai Hadasa1069c12018-09-20 21:39:19 +03002492 mlx5_cmd_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn, uid);
Leon Romanovsky21a428a2019-02-03 14:55:51 +02002493 return -EFAULT;
Eli Cohene126ba92013-07-07 17:25:49 +03002494 }
Eli Cohene126ba92013-07-07 17:25:49 +03002495 }
2496
Leon Romanovsky21a428a2019-02-03 14:55:51 +02002497 return 0;
Eli Cohene126ba92013-07-07 17:25:49 +03002498}
2499
Shamir Rabinovitchc4367a22019-03-31 19:10:05 +03002500static void mlx5_ib_dealloc_pd(struct ib_pd *pd, struct ib_udata *udata)
Eli Cohene126ba92013-07-07 17:25:49 +03002501{
2502 struct mlx5_ib_dev *mdev = to_mdev(pd->device);
2503 struct mlx5_ib_pd *mpd = to_mpd(pd);
2504
Yishai Hadasa1069c12018-09-20 21:39:19 +03002505 mlx5_cmd_dealloc_pd(mdev->mdev, mpd->pdn, mpd->uid);
Eli Cohene126ba92013-07-07 17:25:49 +03002506}
2507
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002508enum {
2509 MATCH_CRITERIA_ENABLE_OUTER_BIT,
2510 MATCH_CRITERIA_ENABLE_MISC_BIT,
Ariel Levkovich71c6e862018-05-13 14:33:34 +03002511 MATCH_CRITERIA_ENABLE_INNER_BIT,
2512 MATCH_CRITERIA_ENABLE_MISC2_BIT
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002513};
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002514
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002515#define HEADER_IS_ZERO(match_criteria, headers) \
2516 !(memchr_inv(MLX5_ADDR_OF(fte_match_param, match_criteria, headers), \
2517 0, MLX5_FLD_SZ_BYTES(fte_match_param, headers))) \
2518
2519static u8 get_match_criteria_enable(u32 *match_criteria)
2520{
2521 u8 match_criteria_enable;
2522
2523 match_criteria_enable =
2524 (!HEADER_IS_ZERO(match_criteria, outer_headers)) <<
2525 MATCH_CRITERIA_ENABLE_OUTER_BIT;
2526 match_criteria_enable |=
2527 (!HEADER_IS_ZERO(match_criteria, misc_parameters)) <<
2528 MATCH_CRITERIA_ENABLE_MISC_BIT;
2529 match_criteria_enable |=
2530 (!HEADER_IS_ZERO(match_criteria, inner_headers)) <<
2531 MATCH_CRITERIA_ENABLE_INNER_BIT;
Ariel Levkovich71c6e862018-05-13 14:33:34 +03002532 match_criteria_enable |=
2533 (!HEADER_IS_ZERO(match_criteria, misc_parameters_2)) <<
2534 MATCH_CRITERIA_ENABLE_MISC2_BIT;
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002535
2536 return match_criteria_enable;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002537}
2538
Maor Gottlieb6113cc42019-01-17 20:08:15 +02002539static int set_proto(void *outer_c, void *outer_v, u8 mask, u8 val)
Maor Gottliebca0d4752016-08-30 16:58:35 +03002540{
Maor Gottlieb6113cc42019-01-17 20:08:15 +02002541 u8 entry_mask;
2542 u8 entry_val;
2543 int err = 0;
2544
2545 if (!mask)
2546 goto out;
2547
2548 entry_mask = MLX5_GET(fte_match_set_lyr_2_4, outer_c,
2549 ip_protocol);
2550 entry_val = MLX5_GET(fte_match_set_lyr_2_4, outer_v,
2551 ip_protocol);
2552 if (!entry_mask) {
2553 MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_protocol, mask);
2554 MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_protocol, val);
2555 goto out;
2556 }
2557 /* Don't override existing ip protocol */
2558 if (mask != entry_mask || val != entry_val)
2559 err = -EINVAL;
2560out:
2561 return err;
Maor Gottliebca0d4752016-08-30 16:58:35 +03002562}
2563
Daria Velikovsky37da2a02018-05-07 10:20:02 +03002564static void set_flow_label(void *misc_c, void *misc_v, u32 mask, u32 val,
Moses Reuben2d1e6972016-11-14 19:04:52 +02002565 bool inner)
2566{
2567 if (inner) {
2568 MLX5_SET(fte_match_set_misc,
2569 misc_c, inner_ipv6_flow_label, mask);
2570 MLX5_SET(fte_match_set_misc,
2571 misc_v, inner_ipv6_flow_label, val);
2572 } else {
2573 MLX5_SET(fte_match_set_misc,
2574 misc_c, outer_ipv6_flow_label, mask);
2575 MLX5_SET(fte_match_set_misc,
2576 misc_v, outer_ipv6_flow_label, val);
2577 }
2578}
2579
Maor Gottliebca0d4752016-08-30 16:58:35 +03002580static void set_tos(void *outer_c, void *outer_v, u8 mask, u8 val)
2581{
2582 MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_ecn, mask);
2583 MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_ecn, val);
2584 MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_dscp, mask >> 2);
2585 MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_dscp, val >> 2);
2586}
2587
Ariel Levkovich71c6e862018-05-13 14:33:34 +03002588static int check_mpls_supp_fields(u32 field_support, const __be32 *set_mask)
2589{
2590 if (MLX5_GET(fte_match_mpls, set_mask, mpls_label) &&
2591 !(field_support & MLX5_FIELD_SUPPORT_MPLS_LABEL))
2592 return -EOPNOTSUPP;
2593
2594 if (MLX5_GET(fte_match_mpls, set_mask, mpls_exp) &&
2595 !(field_support & MLX5_FIELD_SUPPORT_MPLS_EXP))
2596 return -EOPNOTSUPP;
2597
2598 if (MLX5_GET(fte_match_mpls, set_mask, mpls_s_bos) &&
2599 !(field_support & MLX5_FIELD_SUPPORT_MPLS_S_BOS))
2600 return -EOPNOTSUPP;
2601
2602 if (MLX5_GET(fte_match_mpls, set_mask, mpls_ttl) &&
2603 !(field_support & MLX5_FIELD_SUPPORT_MPLS_TTL))
2604 return -EOPNOTSUPP;
2605
2606 return 0;
2607}
2608
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002609#define LAST_ETH_FIELD vlan_tag
2610#define LAST_IB_FIELD sl
Maor Gottliebca0d4752016-08-30 16:58:35 +03002611#define LAST_IPV4_FIELD tos
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002612#define LAST_IPV6_FIELD traffic_class
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002613#define LAST_TCP_UDP_FIELD src_port
Moses Reubenffb30d82016-11-14 19:04:50 +02002614#define LAST_TUNNEL_FIELD tunnel_id
Moses Reuben2ac693f2017-01-18 14:59:50 +02002615#define LAST_FLOW_TAG_FIELD tag_id
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002616#define LAST_DROP_FIELD size
Raed Salem3b3233f2018-05-31 16:43:39 +03002617#define LAST_COUNTERS_FIELD counters
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002618
2619/* Field is the last supported field */
2620#define FIELDS_NOT_SUPPORTED(filter, field)\
2621 memchr_inv((void *)&filter.field +\
2622 sizeof(filter.field), 0,\
2623 sizeof(filter) -\
2624 offsetof(typeof(filter), field) -\
2625 sizeof(filter.field))
2626
Mark Bloch2ea26202018-09-06 17:27:03 +03002627int parse_flow_flow_action(struct mlx5_ib_flow_action *maction,
2628 bool is_egress,
2629 struct mlx5_flow_act *action)
Aviad Yehezkel802c2122018-03-28 09:27:53 +03002630{
Aviad Yehezkel802c2122018-03-28 09:27:53 +03002631
2632 switch (maction->ib_action.type) {
2633 case IB_FLOW_ACTION_ESP:
Mark Bloch501f14e2018-09-06 17:27:04 +03002634 if (action->action & (MLX5_FLOW_CONTEXT_ACTION_ENCRYPT |
2635 MLX5_FLOW_CONTEXT_ACTION_DECRYPT))
2636 return -EINVAL;
Aviad Yehezkel802c2122018-03-28 09:27:53 +03002637 /* Currently only AES_GCM keymat is supported by the driver */
2638 action->esp_id = (uintptr_t)maction->esp_aes_gcm.ctx;
Mark Bloch2ea26202018-09-06 17:27:03 +03002639 action->action |= is_egress ?
Aviad Yehezkel802c2122018-03-28 09:27:53 +03002640 MLX5_FLOW_CONTEXT_ACTION_ENCRYPT :
2641 MLX5_FLOW_CONTEXT_ACTION_DECRYPT;
2642 return 0;
Mark Blochb1085be2018-09-02 12:51:32 +03002643 case IB_FLOW_ACTION_UNSPECIFIED:
2644 if (maction->flow_action_raw.sub_type ==
2645 MLX5_IB_FLOW_ACTION_MODIFY_HEADER) {
Mark Bloch501f14e2018-09-06 17:27:04 +03002646 if (action->action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR)
2647 return -EINVAL;
Mark Blochb1085be2018-09-02 12:51:32 +03002648 action->action |= MLX5_FLOW_CONTEXT_ACTION_MOD_HDR;
2649 action->modify_id = maction->flow_action_raw.action_id;
2650 return 0;
2651 }
Mark Bloch10a30892018-09-02 12:51:34 +03002652 if (maction->flow_action_raw.sub_type ==
2653 MLX5_IB_FLOW_ACTION_DECAP) {
Mark Bloch501f14e2018-09-06 17:27:04 +03002654 if (action->action & MLX5_FLOW_CONTEXT_ACTION_DECAP)
2655 return -EINVAL;
Mark Bloch10a30892018-09-02 12:51:34 +03002656 action->action |= MLX5_FLOW_CONTEXT_ACTION_DECAP;
2657 return 0;
2658 }
Mark Bloche806f932018-09-02 12:51:36 +03002659 if (maction->flow_action_raw.sub_type ==
2660 MLX5_IB_FLOW_ACTION_PACKET_REFORMAT) {
Mark Bloch501f14e2018-09-06 17:27:04 +03002661 if (action->action &
2662 MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT)
2663 return -EINVAL;
Mark Bloche806f932018-09-02 12:51:36 +03002664 action->action |=
2665 MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT;
2666 action->reformat_id =
2667 maction->flow_action_raw.action_id;
2668 return 0;
2669 }
Mark Blochb1085be2018-09-02 12:51:32 +03002670 /* fall through */
Aviad Yehezkel802c2122018-03-28 09:27:53 +03002671 default:
2672 return -EOPNOTSUPP;
2673 }
2674}
2675
Jianbo Liubb0ee7d2019-06-25 17:47:58 +00002676static int parse_flow_attr(struct mlx5_core_dev *mdev,
2677 struct mlx5_flow_spec *spec,
2678 const union ib_flow_spec *ib_spec,
Aviad Yehezkel802c2122018-03-28 09:27:53 +03002679 const struct ib_flow_attr *flow_attr,
Ariel Levkovich71c6e862018-05-13 14:33:34 +03002680 struct mlx5_flow_act *action, u32 prev_type)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002681{
Jianbo Liubb0ee7d2019-06-25 17:47:58 +00002682 struct mlx5_flow_context *flow_context = &spec->flow_context;
2683 u32 *match_c = spec->match_criteria;
2684 u32 *match_v = spec->match_value;
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002685 void *misc_params_c = MLX5_ADDR_OF(fte_match_param, match_c,
2686 misc_parameters);
2687 void *misc_params_v = MLX5_ADDR_OF(fte_match_param, match_v,
2688 misc_parameters);
Ariel Levkovich71c6e862018-05-13 14:33:34 +03002689 void *misc_params2_c = MLX5_ADDR_OF(fte_match_param, match_c,
2690 misc_parameters_2);
2691 void *misc_params2_v = MLX5_ADDR_OF(fte_match_param, match_v,
2692 misc_parameters_2);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002693 void *headers_c;
2694 void *headers_v;
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002695 int match_ipv;
Aviad Yehezkel802c2122018-03-28 09:27:53 +03002696 int ret;
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002697
Moses Reuben2d1e6972016-11-14 19:04:52 +02002698 if (ib_spec->type & IB_FLOW_SPEC_INNER) {
2699 headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
2700 inner_headers);
2701 headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
2702 inner_headers);
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002703 match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2704 ft_field_support.inner_ip_version);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002705 } else {
2706 headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
2707 outer_headers);
2708 headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
2709 outer_headers);
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002710 match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2711 ft_field_support.outer_ip_version);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002712 }
2713
2714 switch (ib_spec->type & ~IB_FLOW_SPEC_INNER) {
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002715 case IB_FLOW_SPEC_ETH:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002716 if (FIELDS_NOT_SUPPORTED(ib_spec->eth.mask, LAST_ETH_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002717 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002718
Moses Reuben2d1e6972016-11-14 19:04:52 +02002719 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002720 dmac_47_16),
2721 ib_spec->eth.mask.dst_mac);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002722 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002723 dmac_47_16),
2724 ib_spec->eth.val.dst_mac);
2725
Moses Reuben2d1e6972016-11-14 19:04:52 +02002726 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottliebee3da802016-09-12 19:16:24 +03002727 smac_47_16),
2728 ib_spec->eth.mask.src_mac);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002729 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottliebee3da802016-09-12 19:16:24 +03002730 smac_47_16),
2731 ib_spec->eth.val.src_mac);
2732
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002733 if (ib_spec->eth.mask.vlan_tag) {
Moses Reuben2d1e6972016-11-14 19:04:52 +02002734 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Mohamad Haj Yahia10543362016-10-09 16:25:43 +03002735 cvlan_tag, 1);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002736 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Mohamad Haj Yahia10543362016-10-09 16:25:43 +03002737 cvlan_tag, 1);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002738
Moses Reuben2d1e6972016-11-14 19:04:52 +02002739 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002740 first_vid, ntohs(ib_spec->eth.mask.vlan_tag));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002741 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002742 first_vid, ntohs(ib_spec->eth.val.vlan_tag));
2743
Moses Reuben2d1e6972016-11-14 19:04:52 +02002744 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002745 first_cfi,
2746 ntohs(ib_spec->eth.mask.vlan_tag) >> 12);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002747 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002748 first_cfi,
2749 ntohs(ib_spec->eth.val.vlan_tag) >> 12);
2750
Moses Reuben2d1e6972016-11-14 19:04:52 +02002751 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002752 first_prio,
2753 ntohs(ib_spec->eth.mask.vlan_tag) >> 13);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002754 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002755 first_prio,
2756 ntohs(ib_spec->eth.val.vlan_tag) >> 13);
2757 }
Moses Reuben2d1e6972016-11-14 19:04:52 +02002758 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002759 ethertype, ntohs(ib_spec->eth.mask.ether_type));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002760 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002761 ethertype, ntohs(ib_spec->eth.val.ether_type));
2762 break;
2763 case IB_FLOW_SPEC_IPV4:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002764 if (FIELDS_NOT_SUPPORTED(ib_spec->ipv4.mask, LAST_IPV4_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002765 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002766
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002767 if (match_ipv) {
2768 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2769 ip_version, 0xf);
2770 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Boris Pismenny3346c482017-08-20 15:13:08 +03002771 ip_version, MLX5_FS_IPV4_VERSION);
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002772 } else {
2773 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2774 ethertype, 0xffff);
2775 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2776 ethertype, ETH_P_IP);
2777 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002778
Moses Reuben2d1e6972016-11-14 19:04:52 +02002779 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002780 src_ipv4_src_ipv6.ipv4_layout.ipv4),
2781 &ib_spec->ipv4.mask.src_ip,
2782 sizeof(ib_spec->ipv4.mask.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002783 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002784 src_ipv4_src_ipv6.ipv4_layout.ipv4),
2785 &ib_spec->ipv4.val.src_ip,
2786 sizeof(ib_spec->ipv4.val.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002787 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002788 dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2789 &ib_spec->ipv4.mask.dst_ip,
2790 sizeof(ib_spec->ipv4.mask.dst_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002791 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002792 dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2793 &ib_spec->ipv4.val.dst_ip,
2794 sizeof(ib_spec->ipv4.val.dst_ip));
Maor Gottliebca0d4752016-08-30 16:58:35 +03002795
Moses Reuben2d1e6972016-11-14 19:04:52 +02002796 set_tos(headers_c, headers_v,
Maor Gottliebca0d4752016-08-30 16:58:35 +03002797 ib_spec->ipv4.mask.tos, ib_spec->ipv4.val.tos);
2798
Maor Gottlieb6113cc42019-01-17 20:08:15 +02002799 if (set_proto(headers_c, headers_v,
2800 ib_spec->ipv4.mask.proto,
2801 ib_spec->ipv4.val.proto))
2802 return -EINVAL;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002803 break;
Maor Gottlieb026bae02016-06-17 15:14:51 +03002804 case IB_FLOW_SPEC_IPV6:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002805 if (FIELDS_NOT_SUPPORTED(ib_spec->ipv6.mask, LAST_IPV6_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002806 return -EOPNOTSUPP;
Maor Gottlieb026bae02016-06-17 15:14:51 +03002807
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002808 if (match_ipv) {
2809 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2810 ip_version, 0xf);
2811 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Boris Pismenny3346c482017-08-20 15:13:08 +03002812 ip_version, MLX5_FS_IPV6_VERSION);
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002813 } else {
2814 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2815 ethertype, 0xffff);
2816 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2817 ethertype, ETH_P_IPV6);
2818 }
Maor Gottlieb026bae02016-06-17 15:14:51 +03002819
Moses Reuben2d1e6972016-11-14 19:04:52 +02002820 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb026bae02016-06-17 15:14:51 +03002821 src_ipv4_src_ipv6.ipv6_layout.ipv6),
2822 &ib_spec->ipv6.mask.src_ip,
2823 sizeof(ib_spec->ipv6.mask.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002824 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb026bae02016-06-17 15:14:51 +03002825 src_ipv4_src_ipv6.ipv6_layout.ipv6),
2826 &ib_spec->ipv6.val.src_ip,
2827 sizeof(ib_spec->ipv6.val.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002828 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb026bae02016-06-17 15:14:51 +03002829 dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2830 &ib_spec->ipv6.mask.dst_ip,
2831 sizeof(ib_spec->ipv6.mask.dst_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002832 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb026bae02016-06-17 15:14:51 +03002833 dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2834 &ib_spec->ipv6.val.dst_ip,
2835 sizeof(ib_spec->ipv6.val.dst_ip));
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002836
Moses Reuben2d1e6972016-11-14 19:04:52 +02002837 set_tos(headers_c, headers_v,
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002838 ib_spec->ipv6.mask.traffic_class,
2839 ib_spec->ipv6.val.traffic_class);
2840
Maor Gottlieb6113cc42019-01-17 20:08:15 +02002841 if (set_proto(headers_c, headers_v,
2842 ib_spec->ipv6.mask.next_hdr,
2843 ib_spec->ipv6.val.next_hdr))
2844 return -EINVAL;
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002845
Moses Reuben2d1e6972016-11-14 19:04:52 +02002846 set_flow_label(misc_params_c, misc_params_v,
2847 ntohl(ib_spec->ipv6.mask.flow_label),
2848 ntohl(ib_spec->ipv6.val.flow_label),
2849 ib_spec->type & IB_FLOW_SPEC_INNER);
Aviad Yehezkel802c2122018-03-28 09:27:53 +03002850 break;
2851 case IB_FLOW_SPEC_ESP:
2852 if (ib_spec->esp.mask.seq)
2853 return -EOPNOTSUPP;
Moses Reuben2d1e6972016-11-14 19:04:52 +02002854
Aviad Yehezkel802c2122018-03-28 09:27:53 +03002855 MLX5_SET(fte_match_set_misc, misc_params_c, outer_esp_spi,
2856 ntohl(ib_spec->esp.mask.spi));
2857 MLX5_SET(fte_match_set_misc, misc_params_v, outer_esp_spi,
2858 ntohl(ib_spec->esp.val.spi));
Maor Gottlieb026bae02016-06-17 15:14:51 +03002859 break;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002860 case IB_FLOW_SPEC_TCP:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002861 if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
2862 LAST_TCP_UDP_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002863 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002864
Maor Gottlieb6113cc42019-01-17 20:08:15 +02002865 if (set_proto(headers_c, headers_v, 0xff, IPPROTO_TCP))
2866 return -EINVAL;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002867
Moses Reuben2d1e6972016-11-14 19:04:52 +02002868 MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002869 ntohs(ib_spec->tcp_udp.mask.src_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002870 MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002871 ntohs(ib_spec->tcp_udp.val.src_port));
2872
Moses Reuben2d1e6972016-11-14 19:04:52 +02002873 MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002874 ntohs(ib_spec->tcp_udp.mask.dst_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002875 MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002876 ntohs(ib_spec->tcp_udp.val.dst_port));
2877 break;
2878 case IB_FLOW_SPEC_UDP:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002879 if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
2880 LAST_TCP_UDP_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002881 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002882
Maor Gottlieb6113cc42019-01-17 20:08:15 +02002883 if (set_proto(headers_c, headers_v, 0xff, IPPROTO_UDP))
2884 return -EINVAL;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002885
Moses Reuben2d1e6972016-11-14 19:04:52 +02002886 MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002887 ntohs(ib_spec->tcp_udp.mask.src_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002888 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002889 ntohs(ib_spec->tcp_udp.val.src_port));
2890
Moses Reuben2d1e6972016-11-14 19:04:52 +02002891 MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002892 ntohs(ib_spec->tcp_udp.mask.dst_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002893 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002894 ntohs(ib_spec->tcp_udp.val.dst_port));
2895 break;
Ariel Levkovichda2f22a2018-05-13 14:33:33 +03002896 case IB_FLOW_SPEC_GRE:
2897 if (ib_spec->gre.mask.c_ks_res0_ver)
2898 return -EOPNOTSUPP;
2899
Maor Gottlieb6113cc42019-01-17 20:08:15 +02002900 if (set_proto(headers_c, headers_v, 0xff, IPPROTO_GRE))
2901 return -EINVAL;
2902
Ariel Levkovichda2f22a2018-05-13 14:33:33 +03002903 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
2904 0xff);
2905 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
2906 IPPROTO_GRE);
2907
2908 MLX5_SET(fte_match_set_misc, misc_params_c, gre_protocol,
Maor Gottlieba93b6322018-07-01 15:50:17 +03002909 ntohs(ib_spec->gre.mask.protocol));
Ariel Levkovichda2f22a2018-05-13 14:33:33 +03002910 MLX5_SET(fte_match_set_misc, misc_params_v, gre_protocol,
2911 ntohs(ib_spec->gre.val.protocol));
2912
2913 memcpy(MLX5_ADDR_OF(fte_match_set_misc, misc_params_c,
Oz Shlomo5886a962018-12-10 13:15:13 -08002914 gre_key.nvgre.hi),
Ariel Levkovichda2f22a2018-05-13 14:33:33 +03002915 &ib_spec->gre.mask.key,
2916 sizeof(ib_spec->gre.mask.key));
2917 memcpy(MLX5_ADDR_OF(fte_match_set_misc, misc_params_v,
Oz Shlomo5886a962018-12-10 13:15:13 -08002918 gre_key.nvgre.hi),
Ariel Levkovichda2f22a2018-05-13 14:33:33 +03002919 &ib_spec->gre.val.key,
2920 sizeof(ib_spec->gre.val.key));
2921 break;
Ariel Levkovich71c6e862018-05-13 14:33:34 +03002922 case IB_FLOW_SPEC_MPLS:
2923 switch (prev_type) {
2924 case IB_FLOW_SPEC_UDP:
2925 if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2926 ft_field_support.outer_first_mpls_over_udp),
2927 &ib_spec->mpls.mask.tag))
2928 return -EOPNOTSUPP;
2929
2930 memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
2931 outer_first_mpls_over_udp),
2932 &ib_spec->mpls.val.tag,
2933 sizeof(ib_spec->mpls.val.tag));
2934 memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
2935 outer_first_mpls_over_udp),
2936 &ib_spec->mpls.mask.tag,
2937 sizeof(ib_spec->mpls.mask.tag));
2938 break;
2939 case IB_FLOW_SPEC_GRE:
2940 if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2941 ft_field_support.outer_first_mpls_over_gre),
2942 &ib_spec->mpls.mask.tag))
2943 return -EOPNOTSUPP;
2944
2945 memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
2946 outer_first_mpls_over_gre),
2947 &ib_spec->mpls.val.tag,
2948 sizeof(ib_spec->mpls.val.tag));
2949 memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
2950 outer_first_mpls_over_gre),
2951 &ib_spec->mpls.mask.tag,
2952 sizeof(ib_spec->mpls.mask.tag));
2953 break;
2954 default:
2955 if (ib_spec->type & IB_FLOW_SPEC_INNER) {
2956 if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2957 ft_field_support.inner_first_mpls),
2958 &ib_spec->mpls.mask.tag))
2959 return -EOPNOTSUPP;
2960
2961 memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
2962 inner_first_mpls),
2963 &ib_spec->mpls.val.tag,
2964 sizeof(ib_spec->mpls.val.tag));
2965 memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
2966 inner_first_mpls),
2967 &ib_spec->mpls.mask.tag,
2968 sizeof(ib_spec->mpls.mask.tag));
2969 } else {
2970 if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2971 ft_field_support.outer_first_mpls),
2972 &ib_spec->mpls.mask.tag))
2973 return -EOPNOTSUPP;
2974
2975 memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
2976 outer_first_mpls),
2977 &ib_spec->mpls.val.tag,
2978 sizeof(ib_spec->mpls.val.tag));
2979 memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
2980 outer_first_mpls),
2981 &ib_spec->mpls.mask.tag,
2982 sizeof(ib_spec->mpls.mask.tag));
2983 }
2984 }
2985 break;
Moses Reubenffb30d82016-11-14 19:04:50 +02002986 case IB_FLOW_SPEC_VXLAN_TUNNEL:
2987 if (FIELDS_NOT_SUPPORTED(ib_spec->tunnel.mask,
2988 LAST_TUNNEL_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002989 return -EOPNOTSUPP;
Moses Reubenffb30d82016-11-14 19:04:50 +02002990
2991 MLX5_SET(fte_match_set_misc, misc_params_c, vxlan_vni,
2992 ntohl(ib_spec->tunnel.mask.tunnel_id));
2993 MLX5_SET(fte_match_set_misc, misc_params_v, vxlan_vni,
2994 ntohl(ib_spec->tunnel.val.tunnel_id));
2995 break;
Moses Reuben2ac693f2017-01-18 14:59:50 +02002996 case IB_FLOW_SPEC_ACTION_TAG:
2997 if (FIELDS_NOT_SUPPORTED(ib_spec->flow_tag,
2998 LAST_FLOW_TAG_FIELD))
2999 return -EOPNOTSUPP;
3000 if (ib_spec->flow_tag.tag_id >= BIT(24))
3001 return -EINVAL;
3002
Jianbo Liubb0ee7d2019-06-25 17:47:58 +00003003 flow_context->flow_tag = ib_spec->flow_tag.tag_id;
3004 flow_context->flags |= FLOW_CONTEXT_HAS_TAG;
Moses Reuben2ac693f2017-01-18 14:59:50 +02003005 break;
Slava Shwartsmana22ed862017-04-03 13:13:52 +03003006 case IB_FLOW_SPEC_ACTION_DROP:
3007 if (FIELDS_NOT_SUPPORTED(ib_spec->drop,
3008 LAST_DROP_FIELD))
3009 return -EOPNOTSUPP;
Boris Pismenny075572d2017-08-16 09:33:30 +03003010 action->action |= MLX5_FLOW_CONTEXT_ACTION_DROP;
Slava Shwartsmana22ed862017-04-03 13:13:52 +03003011 break;
Aviad Yehezkel802c2122018-03-28 09:27:53 +03003012 case IB_FLOW_SPEC_ACTION_HANDLE:
Mark Bloch2ea26202018-09-06 17:27:03 +03003013 ret = parse_flow_flow_action(to_mflow_act(ib_spec->action.act),
3014 flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS, action);
Aviad Yehezkel802c2122018-03-28 09:27:53 +03003015 if (ret)
3016 return ret;
3017 break;
Raed Salem3b3233f2018-05-31 16:43:39 +03003018 case IB_FLOW_SPEC_ACTION_COUNT:
3019 if (FIELDS_NOT_SUPPORTED(ib_spec->flow_count,
3020 LAST_COUNTERS_FIELD))
3021 return -EOPNOTSUPP;
3022
3023 /* for now support only one counters spec per flow */
3024 if (action->action & MLX5_FLOW_CONTEXT_ACTION_COUNT)
3025 return -EINVAL;
3026
3027 action->counters = ib_spec->flow_count.counters;
3028 action->action |= MLX5_FLOW_CONTEXT_ACTION_COUNT;
3029 break;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003030 default:
3031 return -EINVAL;
3032 }
3033
3034 return 0;
3035}
3036
3037/* If a flow could catch both multicast and unicast packets,
3038 * it won't fall into the multicast flow steering table and this rule
3039 * could steal other multicast packets.
3040 */
Yishai Hadasa550ddf2017-08-17 15:52:33 +03003041static bool flow_is_multicast_only(const struct ib_flow_attr *ib_attr)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003042{
Yishai Hadas81e30882017-06-08 16:15:09 +03003043 union ib_flow_spec *flow_spec;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003044
3045 if (ib_attr->type != IB_FLOW_ATTR_NORMAL ||
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003046 ib_attr->num_of_specs < 1)
3047 return false;
3048
Yishai Hadas81e30882017-06-08 16:15:09 +03003049 flow_spec = (union ib_flow_spec *)(ib_attr + 1);
3050 if (flow_spec->type == IB_FLOW_SPEC_IPV4) {
3051 struct ib_flow_spec_ipv4 *ipv4_spec;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003052
Yishai Hadas81e30882017-06-08 16:15:09 +03003053 ipv4_spec = (struct ib_flow_spec_ipv4 *)flow_spec;
3054 if (ipv4_is_multicast(ipv4_spec->val.dst_ip))
3055 return true;
3056
3057 return false;
3058 }
3059
3060 if (flow_spec->type == IB_FLOW_SPEC_ETH) {
3061 struct ib_flow_spec_eth *eth_spec;
3062
3063 eth_spec = (struct ib_flow_spec_eth *)flow_spec;
3064 return is_multicast_ether_addr(eth_spec->mask.dst_mac) &&
3065 is_multicast_ether_addr(eth_spec->val.dst_mac);
3066 }
3067
3068 return false;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003069}
3070
Aviad Yehezkel802c2122018-03-28 09:27:53 +03003071enum valid_spec {
3072 VALID_SPEC_INVALID,
3073 VALID_SPEC_VALID,
3074 VALID_SPEC_NA,
3075};
3076
3077static enum valid_spec
3078is_valid_esp_aes_gcm(struct mlx5_core_dev *mdev,
3079 const struct mlx5_flow_spec *spec,
3080 const struct mlx5_flow_act *flow_act,
3081 bool egress)
3082{
3083 const u32 *match_c = spec->match_criteria;
3084 bool is_crypto =
3085 (flow_act->action & (MLX5_FLOW_CONTEXT_ACTION_ENCRYPT |
3086 MLX5_FLOW_CONTEXT_ACTION_DECRYPT));
3087 bool is_ipsec = mlx5_fs_is_ipsec_flow(match_c);
3088 bool is_drop = flow_act->action & MLX5_FLOW_CONTEXT_ACTION_DROP;
3089
3090 /*
3091 * Currently only crypto is supported in egress, when regular egress
3092 * rules would be supported, always return VALID_SPEC_NA.
3093 */
3094 if (!is_crypto)
Mark Bloch78dd0c42018-09-02 12:51:31 +03003095 return VALID_SPEC_NA;
Aviad Yehezkel802c2122018-03-28 09:27:53 +03003096
3097 return is_crypto && is_ipsec &&
Jianbo Liubb0ee7d2019-06-25 17:47:58 +00003098 (!egress || (!is_drop &&
3099 !(spec->flow_context.flags & FLOW_CONTEXT_HAS_TAG))) ?
Aviad Yehezkel802c2122018-03-28 09:27:53 +03003100 VALID_SPEC_VALID : VALID_SPEC_INVALID;
3101}
3102
3103static bool is_valid_spec(struct mlx5_core_dev *mdev,
3104 const struct mlx5_flow_spec *spec,
3105 const struct mlx5_flow_act *flow_act,
3106 bool egress)
3107{
3108 /* We curretly only support ipsec egress flow */
3109 return is_valid_esp_aes_gcm(mdev, spec, flow_act, egress) != VALID_SPEC_INVALID;
3110}
3111
Ariel Levkovich19cc7522017-04-03 13:11:03 +03003112static bool is_valid_ethertype(struct mlx5_core_dev *mdev,
3113 const struct ib_flow_attr *flow_attr,
Ariel Levkovich0f750962017-04-03 13:11:02 +03003114 bool check_inner)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003115{
3116 union ib_flow_spec *ib_spec = (union ib_flow_spec *)(flow_attr + 1);
Ariel Levkovich19cc7522017-04-03 13:11:03 +03003117 int match_ipv = check_inner ?
3118 MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
3119 ft_field_support.inner_ip_version) :
3120 MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
3121 ft_field_support.outer_ip_version);
Ariel Levkovich0f750962017-04-03 13:11:02 +03003122 int inner_bit = check_inner ? IB_FLOW_SPEC_INNER : 0;
3123 bool ipv4_spec_valid, ipv6_spec_valid;
3124 unsigned int ip_spec_type = 0;
3125 bool has_ethertype = false;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003126 unsigned int spec_index;
Ariel Levkovich0f750962017-04-03 13:11:02 +03003127 bool mask_valid = true;
3128 u16 eth_type = 0;
3129 bool type_valid;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003130
3131 /* Validate that ethertype is correct */
3132 for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
Ariel Levkovich0f750962017-04-03 13:11:02 +03003133 if ((ib_spec->type == (IB_FLOW_SPEC_ETH | inner_bit)) &&
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003134 ib_spec->eth.mask.ether_type) {
Ariel Levkovich0f750962017-04-03 13:11:02 +03003135 mask_valid = (ib_spec->eth.mask.ether_type ==
3136 htons(0xffff));
3137 has_ethertype = true;
3138 eth_type = ntohs(ib_spec->eth.val.ether_type);
3139 } else if ((ib_spec->type == (IB_FLOW_SPEC_IPV4 | inner_bit)) ||
3140 (ib_spec->type == (IB_FLOW_SPEC_IPV6 | inner_bit))) {
3141 ip_spec_type = ib_spec->type;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003142 }
3143 ib_spec = (void *)ib_spec + ib_spec->size;
3144 }
Ariel Levkovich0f750962017-04-03 13:11:02 +03003145
3146 type_valid = (!has_ethertype) || (!ip_spec_type);
3147 if (!type_valid && mask_valid) {
3148 ipv4_spec_valid = (eth_type == ETH_P_IP) &&
3149 (ip_spec_type == (IB_FLOW_SPEC_IPV4 | inner_bit));
3150 ipv6_spec_valid = (eth_type == ETH_P_IPV6) &&
3151 (ip_spec_type == (IB_FLOW_SPEC_IPV6 | inner_bit));
Ariel Levkovich19cc7522017-04-03 13:11:03 +03003152
3153 type_valid = (ipv4_spec_valid) || (ipv6_spec_valid) ||
3154 (((eth_type == ETH_P_MPLS_UC) ||
3155 (eth_type == ETH_P_MPLS_MC)) && match_ipv);
Ariel Levkovich0f750962017-04-03 13:11:02 +03003156 }
3157
3158 return type_valid;
3159}
3160
Ariel Levkovich19cc7522017-04-03 13:11:03 +03003161static bool is_valid_attr(struct mlx5_core_dev *mdev,
3162 const struct ib_flow_attr *flow_attr)
Ariel Levkovich0f750962017-04-03 13:11:02 +03003163{
Ariel Levkovich19cc7522017-04-03 13:11:03 +03003164 return is_valid_ethertype(mdev, flow_attr, false) &&
3165 is_valid_ethertype(mdev, flow_attr, true);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003166}
3167
3168static void put_flow_table(struct mlx5_ib_dev *dev,
3169 struct mlx5_ib_flow_prio *prio, bool ft_added)
3170{
3171 prio->refcount -= !!ft_added;
3172 if (!prio->refcount) {
3173 mlx5_destroy_flow_table(prio->flow_table);
3174 prio->flow_table = NULL;
3175 }
3176}
3177
Raed Salem3b3233f2018-05-31 16:43:39 +03003178static void counters_clear_description(struct ib_counters *counters)
3179{
3180 struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
3181
3182 mutex_lock(&mcounters->mcntrs_mutex);
3183 kfree(mcounters->counters_data);
3184 mcounters->counters_data = NULL;
3185 mcounters->cntrs_max_index = 0;
3186 mutex_unlock(&mcounters->mcntrs_mutex);
3187}
3188
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003189static int mlx5_ib_destroy_flow(struct ib_flow *flow_id)
3190{
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003191 struct mlx5_ib_flow_handler *handler = container_of(flow_id,
3192 struct mlx5_ib_flow_handler,
3193 ibflow);
3194 struct mlx5_ib_flow_handler *iter, *tmp;
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003195 struct mlx5_ib_dev *dev = handler->dev;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003196
Mark Bloch9a4ca382018-01-16 14:42:35 +00003197 mutex_lock(&dev->flow_db->lock);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003198
3199 list_for_each_entry_safe(iter, tmp, &handler->list, list) {
Mark Bloch74491de2016-08-31 11:24:25 +00003200 mlx5_del_flow_rules(iter->rule);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003201 put_flow_table(dev, iter->prio, true);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003202 list_del(&iter->list);
3203 kfree(iter);
3204 }
3205
Mark Bloch74491de2016-08-31 11:24:25 +00003206 mlx5_del_flow_rules(handler->rule);
Maor Gottlieb5497adc2016-08-28 14:16:31 +03003207 put_flow_table(dev, handler->prio, true);
Raed Salem3b3233f2018-05-31 16:43:39 +03003208 if (handler->ibcounters &&
3209 atomic_read(&handler->ibcounters->usecnt) == 1)
3210 counters_clear_description(handler->ibcounters);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003211
Raed Salem3b3233f2018-05-31 16:43:39 +03003212 mutex_unlock(&dev->flow_db->lock);
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003213 if (handler->flow_matcher)
3214 atomic_dec(&handler->flow_matcher->usecnt);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003215 kfree(handler);
3216
3217 return 0;
3218}
3219
Maor Gottlieb35d190112016-03-07 18:51:47 +02003220static int ib_prio_to_core_prio(unsigned int priority, bool dont_trap)
3221{
3222 priority *= 2;
3223 if (!dont_trap)
3224 priority++;
3225 return priority;
3226}
3227
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003228enum flow_table_type {
3229 MLX5_IB_FT_RX,
3230 MLX5_IB_FT_TX
3231};
3232
Maor Gottlieb00b7c2a2017-03-29 06:09:01 +03003233#define MLX5_FS_MAX_TYPES 6
3234#define MLX5_FS_MAX_ENTRIES BIT(16)
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003235
3236static struct mlx5_ib_flow_prio *_get_prio(struct mlx5_flow_namespace *ns,
3237 struct mlx5_ib_flow_prio *prio,
3238 int priority,
Mark Bloch4adda112018-09-02 12:51:33 +03003239 int num_entries, int num_groups,
3240 u32 flags)
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003241{
3242 struct mlx5_flow_table *ft;
3243
3244 ft = mlx5_create_auto_grouped_flow_table(ns, priority,
3245 num_entries,
3246 num_groups,
Mark Bloch4adda112018-09-02 12:51:33 +03003247 0, flags);
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003248 if (IS_ERR(ft))
3249 return ERR_CAST(ft);
3250
3251 prio->flow_table = ft;
3252 prio->refcount = 0;
3253 return prio;
3254}
3255
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003256static struct mlx5_ib_flow_prio *get_flow_table(struct mlx5_ib_dev *dev,
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003257 struct ib_flow_attr *flow_attr,
3258 enum flow_table_type ft_type)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003259{
Maor Gottlieb35d190112016-03-07 18:51:47 +02003260 bool dont_trap = flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003261 struct mlx5_flow_namespace *ns = NULL;
3262 struct mlx5_ib_flow_prio *prio;
3263 struct mlx5_flow_table *ft;
Maor Gottliebdac388e2017-03-29 06:09:00 +03003264 int max_table_size;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003265 int num_entries;
3266 int num_groups;
Maor Gottliebcecae742019-06-12 15:20:13 +03003267 bool esw_encap;
Mark Bloch4adda112018-09-02 12:51:33 +03003268 u32 flags = 0;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003269 int priority;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003270
Maor Gottliebdac388e2017-03-29 06:09:00 +03003271 max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
3272 log_max_ft_size));
Maor Gottliebcecae742019-06-12 15:20:13 +03003273 esw_encap = mlx5_eswitch_get_encap_mode(dev->mdev) !=
3274 DEVLINK_ESWITCH_ENCAP_MODE_NONE;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003275 if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
Mark Bloch78dd0c42018-09-02 12:51:31 +03003276 enum mlx5_flow_namespace_type fn_type;
3277
3278 if (flow_is_multicast_only(flow_attr) &&
3279 !dont_trap)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003280 priority = MLX5_IB_FLOW_MCAST_PRIO;
3281 else
Maor Gottlieb35d190112016-03-07 18:51:47 +02003282 priority = ib_prio_to_core_prio(flow_attr->priority,
3283 dont_trap);
Mark Bloch78dd0c42018-09-02 12:51:31 +03003284 if (ft_type == MLX5_IB_FT_RX) {
3285 fn_type = MLX5_FLOW_NAMESPACE_BYPASS;
3286 prio = &dev->flow_db->prios[priority];
Maor Gottliebcecae742019-06-12 15:20:13 +03003287 if (!dev->is_rep && !esw_encap &&
Mark Bloch4adda112018-09-02 12:51:33 +03003288 MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, decap))
3289 flags |= MLX5_FLOW_TABLE_TUNNEL_EN_DECAP;
Maor Gottliebcecae742019-06-12 15:20:13 +03003290 if (!dev->is_rep && !esw_encap &&
Mark Bloch5c2db532018-09-02 12:51:35 +03003291 MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
3292 reformat_l3_tunnel_to_l2))
3293 flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
Mark Bloch78dd0c42018-09-02 12:51:31 +03003294 } else {
3295 max_table_size =
3296 BIT(MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev,
3297 log_max_ft_size));
3298 fn_type = MLX5_FLOW_NAMESPACE_EGRESS;
3299 prio = &dev->flow_db->egress_prios[priority];
Maor Gottliebcecae742019-06-12 15:20:13 +03003300 if (!dev->is_rep && !esw_encap &&
Mark Bloch4adda112018-09-02 12:51:33 +03003301 MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev, reformat))
3302 flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
Mark Bloch78dd0c42018-09-02 12:51:31 +03003303 }
3304 ns = mlx5_get_flow_namespace(dev->mdev, fn_type);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003305 num_entries = MLX5_FS_MAX_ENTRIES;
3306 num_groups = MLX5_FS_MAX_TYPES;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003307 } else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
3308 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
3309 ns = mlx5_get_flow_namespace(dev->mdev,
3310 MLX5_FLOW_NAMESPACE_LEFTOVERS);
3311 build_leftovers_ft_param(&priority,
3312 &num_entries,
3313 &num_groups);
Mark Bloch9a4ca382018-01-16 14:42:35 +00003314 prio = &dev->flow_db->prios[MLX5_IB_FLOW_LEFTOVERS_PRIO];
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003315 } else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
3316 if (!MLX5_CAP_FLOWTABLE(dev->mdev,
3317 allow_sniffer_and_nic_rx_shared_tir))
3318 return ERR_PTR(-ENOTSUPP);
3319
3320 ns = mlx5_get_flow_namespace(dev->mdev, ft_type == MLX5_IB_FT_RX ?
3321 MLX5_FLOW_NAMESPACE_SNIFFER_RX :
3322 MLX5_FLOW_NAMESPACE_SNIFFER_TX);
3323
Mark Bloch9a4ca382018-01-16 14:42:35 +00003324 prio = &dev->flow_db->sniffer[ft_type];
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003325 priority = 0;
3326 num_entries = 1;
3327 num_groups = 1;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003328 }
3329
3330 if (!ns)
3331 return ERR_PTR(-ENOTSUPP);
3332
Mark Bloch3b705082019-03-28 15:46:22 +02003333 max_table_size = min_t(int, num_entries, max_table_size);
Maor Gottliebdac388e2017-03-29 06:09:00 +03003334
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003335 ft = prio->flow_table;
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003336 if (!ft)
Mark Bloch3b705082019-03-28 15:46:22 +02003337 return _get_prio(ns, prio, priority, max_table_size, num_groups,
Mark Bloch4adda112018-09-02 12:51:33 +03003338 flags);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003339
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003340 return prio;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003341}
3342
Yishai Hadasa550ddf2017-08-17 15:52:33 +03003343static void set_underlay_qp(struct mlx5_ib_dev *dev,
3344 struct mlx5_flow_spec *spec,
3345 u32 underlay_qpn)
3346{
3347 void *misc_params_c = MLX5_ADDR_OF(fte_match_param,
3348 spec->match_criteria,
3349 misc_parameters);
3350 void *misc_params_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
3351 misc_parameters);
3352
3353 if (underlay_qpn &&
3354 MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
3355 ft_field_support.bth_dst_qp)) {
3356 MLX5_SET(fte_match_set_misc,
3357 misc_params_v, bth_dst_qp, underlay_qpn);
3358 MLX5_SET(fte_match_set_misc,
3359 misc_params_c, bth_dst_qp, 0xffffff);
3360 }
3361}
3362
Raed Salem5e95af52018-05-31 16:43:40 +03003363static int read_flow_counters(struct ib_device *ibdev,
3364 struct mlx5_read_counters_attr *read_attr)
3365{
3366 struct mlx5_fc *fc = read_attr->hw_cntrs_hndl;
3367 struct mlx5_ib_dev *dev = to_mdev(ibdev);
3368
3369 return mlx5_fc_query(dev->mdev, fc,
3370 &read_attr->out[IB_COUNTER_PACKETS],
3371 &read_attr->out[IB_COUNTER_BYTES]);
3372}
3373
3374/* flow counters currently expose two counters packets and bytes */
3375#define FLOW_COUNTERS_NUM 2
Raed Salem3b3233f2018-05-31 16:43:39 +03003376static int counters_set_description(struct ib_counters *counters,
3377 enum mlx5_ib_counters_type counters_type,
3378 struct mlx5_ib_flow_counters_desc *desc_data,
3379 u32 ncounters)
3380{
3381 struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
3382 u32 cntrs_max_index = 0;
3383 int i;
3384
3385 if (counters_type != MLX5_IB_COUNTERS_FLOW)
3386 return -EINVAL;
3387
3388 /* init the fields for the object */
3389 mcounters->type = counters_type;
Raed Salem5e95af52018-05-31 16:43:40 +03003390 mcounters->read_counters = read_flow_counters;
3391 mcounters->counters_num = FLOW_COUNTERS_NUM;
Raed Salem3b3233f2018-05-31 16:43:39 +03003392 mcounters->ncounters = ncounters;
3393 /* each counter entry have both description and index pair */
3394 for (i = 0; i < ncounters; i++) {
3395 if (desc_data[i].description > IB_COUNTER_BYTES)
3396 return -EINVAL;
3397
3398 if (cntrs_max_index <= desc_data[i].index)
3399 cntrs_max_index = desc_data[i].index + 1;
3400 }
3401
3402 mutex_lock(&mcounters->mcntrs_mutex);
3403 mcounters->counters_data = desc_data;
3404 mcounters->cntrs_max_index = cntrs_max_index;
3405 mutex_unlock(&mcounters->mcntrs_mutex);
3406
3407 return 0;
3408}
3409
3410#define MAX_COUNTERS_NUM (USHRT_MAX / (sizeof(u32) * 2))
3411static int flow_counters_set_data(struct ib_counters *ibcounters,
3412 struct mlx5_ib_create_flow *ucmd)
3413{
3414 struct mlx5_ib_mcounters *mcounters = to_mcounters(ibcounters);
3415 struct mlx5_ib_flow_counters_data *cntrs_data = NULL;
3416 struct mlx5_ib_flow_counters_desc *desc_data = NULL;
3417 bool hw_hndl = false;
3418 int ret = 0;
3419
3420 if (ucmd && ucmd->ncounters_data != 0) {
3421 cntrs_data = ucmd->data;
3422 if (cntrs_data->ncounters > MAX_COUNTERS_NUM)
3423 return -EINVAL;
3424
3425 desc_data = kcalloc(cntrs_data->ncounters,
3426 sizeof(*desc_data),
3427 GFP_KERNEL);
3428 if (!desc_data)
3429 return -ENOMEM;
3430
3431 if (copy_from_user(desc_data,
3432 u64_to_user_ptr(cntrs_data->counters_data),
3433 sizeof(*desc_data) * cntrs_data->ncounters)) {
3434 ret = -EFAULT;
3435 goto free;
3436 }
3437 }
3438
3439 if (!mcounters->hw_cntrs_hndl) {
3440 mcounters->hw_cntrs_hndl = mlx5_fc_create(
3441 to_mdev(ibcounters->device)->mdev, false);
weiyongjun (A)e31abf72018-06-07 01:47:41 +00003442 if (IS_ERR(mcounters->hw_cntrs_hndl)) {
3443 ret = PTR_ERR(mcounters->hw_cntrs_hndl);
Raed Salem3b3233f2018-05-31 16:43:39 +03003444 goto free;
3445 }
3446 hw_hndl = true;
3447 }
3448
3449 if (desc_data) {
3450 /* counters already bound to at least one flow */
3451 if (mcounters->cntrs_max_index) {
3452 ret = -EINVAL;
3453 goto free_hndl;
3454 }
3455
3456 ret = counters_set_description(ibcounters,
3457 MLX5_IB_COUNTERS_FLOW,
3458 desc_data,
3459 cntrs_data->ncounters);
3460 if (ret)
3461 goto free_hndl;
3462
3463 } else if (!mcounters->cntrs_max_index) {
3464 /* counters not bound yet, must have udata passed */
3465 ret = -EINVAL;
3466 goto free_hndl;
3467 }
3468
3469 return 0;
3470
3471free_hndl:
3472 if (hw_hndl) {
3473 mlx5_fc_destroy(to_mdev(ibcounters->device)->mdev,
3474 mcounters->hw_cntrs_hndl);
3475 mcounters->hw_cntrs_hndl = NULL;
3476 }
3477free:
3478 kfree(desc_data);
3479 return ret;
3480}
3481
Jianbo Liu669ff1e2019-06-25 17:48:12 +00003482static void mlx5_ib_set_rule_source_port(struct mlx5_ib_dev *dev,
3483 struct mlx5_flow_spec *spec,
3484 struct mlx5_eswitch_rep *rep)
3485{
3486 struct mlx5_eswitch *esw = dev->mdev->priv.eswitch;
3487 void *misc;
3488
3489 if (mlx5_eswitch_vport_match_metadata_enabled(esw)) {
3490 misc = MLX5_ADDR_OF(fte_match_param, spec->match_value,
3491 misc_parameters_2);
3492
3493 MLX5_SET(fte_match_set_misc2, misc, metadata_reg_c_0,
3494 mlx5_eswitch_get_vport_metadata_for_match(esw,
3495 rep->vport));
3496 misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
3497 misc_parameters_2);
3498
3499 MLX5_SET_TO_ONES(fte_match_set_misc2, misc, metadata_reg_c_0);
3500 } else {
3501 misc = MLX5_ADDR_OF(fte_match_param, spec->match_value,
3502 misc_parameters);
3503
3504 MLX5_SET(fte_match_set_misc, misc, source_port, rep->vport);
3505
3506 misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
3507 misc_parameters);
3508
3509 MLX5_SET_TO_ONES(fte_match_set_misc, misc, source_port);
3510 }
3511}
3512
Yishai Hadasa550ddf2017-08-17 15:52:33 +03003513static struct mlx5_ib_flow_handler *_create_flow_rule(struct mlx5_ib_dev *dev,
3514 struct mlx5_ib_flow_prio *ft_prio,
3515 const struct ib_flow_attr *flow_attr,
3516 struct mlx5_flow_destination *dst,
Raed Salem3b3233f2018-05-31 16:43:39 +03003517 u32 underlay_qpn,
3518 struct mlx5_ib_create_flow *ucmd)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003519{
3520 struct mlx5_flow_table *ft = ft_prio->flow_table;
3521 struct mlx5_ib_flow_handler *handler;
Jianbo Liubb0ee7d2019-06-25 17:47:58 +00003522 struct mlx5_flow_act flow_act = {};
Maor Gottliebc5bb1732016-07-04 17:23:05 +03003523 struct mlx5_flow_spec *spec;
Raed Salem3b3233f2018-05-31 16:43:39 +03003524 struct mlx5_flow_destination dest_arr[2] = {};
3525 struct mlx5_flow_destination *rule_dst = dest_arr;
Maor Gottliebdd063d02016-08-28 14:16:32 +03003526 const void *ib_flow = (const void *)flow_attr + sizeof(*flow_attr);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003527 unsigned int spec_index;
Ariel Levkovich71c6e862018-05-13 14:33:34 +03003528 u32 prev_type = 0;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003529 int err = 0;
Raed Salem3b3233f2018-05-31 16:43:39 +03003530 int dest_num = 0;
Aviad Yehezkel802c2122018-03-28 09:27:53 +03003531 bool is_egress = flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003532
Ariel Levkovich19cc7522017-04-03 13:11:03 +03003533 if (!is_valid_attr(dev->mdev, flow_attr))
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003534 return ERR_PTR(-EINVAL);
3535
Mark Bloch6a4d00b2019-03-28 15:27:37 +02003536 if (dev->is_rep && is_egress)
Mark Bloch78dd0c42018-09-02 12:51:31 +03003537 return ERR_PTR(-EINVAL);
3538
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03003539 spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003540 handler = kzalloc(sizeof(*handler), GFP_KERNEL);
Maor Gottliebc5bb1732016-07-04 17:23:05 +03003541 if (!handler || !spec) {
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003542 err = -ENOMEM;
3543 goto free;
3544 }
3545
3546 INIT_LIST_HEAD(&handler->list);
Raed Salem3b3233f2018-05-31 16:43:39 +03003547 if (dst) {
3548 memcpy(&dest_arr[0], dst, sizeof(*dst));
3549 dest_num++;
3550 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003551
3552 for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
Jianbo Liubb0ee7d2019-06-25 17:47:58 +00003553 err = parse_flow_attr(dev->mdev, spec,
Ariel Levkovich71c6e862018-05-13 14:33:34 +03003554 ib_flow, flow_attr, &flow_act,
3555 prev_type);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003556 if (err < 0)
3557 goto free;
3558
Ariel Levkovich71c6e862018-05-13 14:33:34 +03003559 prev_type = ((union ib_flow_spec *)ib_flow)->type;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003560 ib_flow += ((union ib_flow_spec *)ib_flow)->size;
3561 }
3562
Yishai Hadasa550ddf2017-08-17 15:52:33 +03003563 if (!flow_is_multicast_only(flow_attr))
3564 set_underlay_qp(dev, spec, underlay_qpn);
3565
Mark Bloch6a4d00b2019-03-28 15:27:37 +02003566 if (dev->is_rep) {
Jianbo Liu669ff1e2019-06-25 17:48:12 +00003567 struct mlx5_eswitch_rep *rep;
Mark Bloch018a94e2018-01-16 14:44:29 +00003568
Jianbo Liu669ff1e2019-06-25 17:48:12 +00003569 rep = dev->port[flow_attr->port - 1].rep;
3570 if (!rep) {
Mark Bloch6a4d00b2019-03-28 15:27:37 +02003571 err = -EINVAL;
3572 goto free;
3573 }
Jianbo Liu669ff1e2019-06-25 17:48:12 +00003574
3575 mlx5_ib_set_rule_source_port(dev, spec, rep);
Mark Bloch018a94e2018-01-16 14:44:29 +00003576 }
3577
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03003578 spec->match_criteria_enable = get_match_criteria_enable(spec->match_criteria);
Aviad Yehezkel802c2122018-03-28 09:27:53 +03003579
3580 if (is_egress &&
3581 !is_valid_spec(dev->mdev, spec, &flow_act, is_egress)) {
3582 err = -EINVAL;
3583 goto free;
3584 }
3585
Raed Salem3b3233f2018-05-31 16:43:39 +03003586 if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_COUNT) {
Mark Bloch171c7625b2018-10-03 00:03:35 +00003587 struct mlx5_ib_mcounters *mcounters;
3588
Raed Salem3b3233f2018-05-31 16:43:39 +03003589 err = flow_counters_set_data(flow_act.counters, ucmd);
3590 if (err)
3591 goto free;
3592
Mark Bloch171c7625b2018-10-03 00:03:35 +00003593 mcounters = to_mcounters(flow_act.counters);
Raed Salem3b3233f2018-05-31 16:43:39 +03003594 handler->ibcounters = flow_act.counters;
3595 dest_arr[dest_num].type =
3596 MLX5_FLOW_DESTINATION_TYPE_COUNTER;
Mark Bloch171c7625b2018-10-03 00:03:35 +00003597 dest_arr[dest_num].counter_id =
3598 mlx5_fc_id(mcounters->hw_cntrs_hndl);
Raed Salem3b3233f2018-05-31 16:43:39 +03003599 dest_num++;
3600 }
3601
Boris Pismenny075572d2017-08-16 09:33:30 +03003602 if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_DROP) {
Raed Salem3b3233f2018-05-31 16:43:39 +03003603 if (!(flow_act.action & MLX5_FLOW_CONTEXT_ACTION_COUNT)) {
3604 rule_dst = NULL;
3605 dest_num = 0;
3606 }
Slava Shwartsmana22ed862017-04-03 13:13:52 +03003607 } else {
Aviad Yehezkel802c2122018-03-28 09:27:53 +03003608 if (is_egress)
3609 flow_act.action |= MLX5_FLOW_CONTEXT_ACTION_ALLOW;
3610 else
3611 flow_act.action |=
Raed Salem3b3233f2018-05-31 16:43:39 +03003612 dest_num ? MLX5_FLOW_CONTEXT_ACTION_FWD_DEST :
Aviad Yehezkel802c2122018-03-28 09:27:53 +03003613 MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO;
Slava Shwartsmana22ed862017-04-03 13:13:52 +03003614 }
Moses Reuben2ac693f2017-01-18 14:59:50 +02003615
Jianbo Liubb0ee7d2019-06-25 17:47:58 +00003616 if ((spec->flow_context.flags & FLOW_CONTEXT_HAS_TAG) &&
Moses Reuben2ac693f2017-01-18 14:59:50 +02003617 (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
3618 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT)) {
3619 mlx5_ib_warn(dev, "Flow tag %u and attribute type %x isn't allowed in leftovers\n",
Jianbo Liubb0ee7d2019-06-25 17:47:58 +00003620 spec->flow_context.flow_tag, flow_attr->type);
Moses Reuben2ac693f2017-01-18 14:59:50 +02003621 err = -EINVAL;
3622 goto free;
3623 }
Mark Bloch74491de2016-08-31 11:24:25 +00003624 handler->rule = mlx5_add_flow_rules(ft, spec,
Hadar Hen Zion66958ed2016-11-07 15:14:45 +02003625 &flow_act,
Slava Shwartsmana22ed862017-04-03 13:13:52 +03003626 rule_dst, dest_num);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003627
3628 if (IS_ERR(handler->rule)) {
3629 err = PTR_ERR(handler->rule);
3630 goto free;
3631 }
3632
Maor Gottliebd9d49802016-08-28 14:16:33 +03003633 ft_prio->refcount++;
Maor Gottlieb5497adc2016-08-28 14:16:31 +03003634 handler->prio = ft_prio;
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003635 handler->dev = dev;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003636
3637 ft_prio->flow_table = ft;
3638free:
Raed Salem3b3233f2018-05-31 16:43:39 +03003639 if (err && handler) {
3640 if (handler->ibcounters &&
3641 atomic_read(&handler->ibcounters->usecnt) == 1)
3642 counters_clear_description(handler->ibcounters);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003643 kfree(handler);
Raed Salem3b3233f2018-05-31 16:43:39 +03003644 }
Maor Gottliebc5bb1732016-07-04 17:23:05 +03003645 kvfree(spec);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003646 return err ? ERR_PTR(err) : handler;
3647}
3648
Yishai Hadasa550ddf2017-08-17 15:52:33 +03003649static struct mlx5_ib_flow_handler *create_flow_rule(struct mlx5_ib_dev *dev,
3650 struct mlx5_ib_flow_prio *ft_prio,
3651 const struct ib_flow_attr *flow_attr,
3652 struct mlx5_flow_destination *dst)
3653{
Raed Salem3b3233f2018-05-31 16:43:39 +03003654 return _create_flow_rule(dev, ft_prio, flow_attr, dst, 0, NULL);
Yishai Hadasa550ddf2017-08-17 15:52:33 +03003655}
3656
Maor Gottlieb35d190112016-03-07 18:51:47 +02003657static struct mlx5_ib_flow_handler *create_dont_trap_rule(struct mlx5_ib_dev *dev,
3658 struct mlx5_ib_flow_prio *ft_prio,
3659 struct ib_flow_attr *flow_attr,
3660 struct mlx5_flow_destination *dst)
3661{
3662 struct mlx5_ib_flow_handler *handler_dst = NULL;
3663 struct mlx5_ib_flow_handler *handler = NULL;
3664
3665 handler = create_flow_rule(dev, ft_prio, flow_attr, NULL);
3666 if (!IS_ERR(handler)) {
3667 handler_dst = create_flow_rule(dev, ft_prio,
3668 flow_attr, dst);
3669 if (IS_ERR(handler_dst)) {
Mark Bloch74491de2016-08-31 11:24:25 +00003670 mlx5_del_flow_rules(handler->rule);
Maor Gottliebd9d49802016-08-28 14:16:33 +03003671 ft_prio->refcount--;
Maor Gottlieb35d190112016-03-07 18:51:47 +02003672 kfree(handler);
3673 handler = handler_dst;
3674 } else {
3675 list_add(&handler_dst->list, &handler->list);
3676 }
3677 }
3678
3679 return handler;
3680}
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003681enum {
3682 LEFTOVERS_MC,
3683 LEFTOVERS_UC,
3684};
3685
3686static struct mlx5_ib_flow_handler *create_leftovers_rule(struct mlx5_ib_dev *dev,
3687 struct mlx5_ib_flow_prio *ft_prio,
3688 struct ib_flow_attr *flow_attr,
3689 struct mlx5_flow_destination *dst)
3690{
3691 struct mlx5_ib_flow_handler *handler_ucast = NULL;
3692 struct mlx5_ib_flow_handler *handler = NULL;
3693
3694 static struct {
3695 struct ib_flow_attr flow_attr;
3696 struct ib_flow_spec_eth eth_flow;
3697 } leftovers_specs[] = {
3698 [LEFTOVERS_MC] = {
3699 .flow_attr = {
3700 .num_of_specs = 1,
3701 .size = sizeof(leftovers_specs[0])
3702 },
3703 .eth_flow = {
3704 .type = IB_FLOW_SPEC_ETH,
3705 .size = sizeof(struct ib_flow_spec_eth),
3706 .mask = {.dst_mac = {0x1} },
3707 .val = {.dst_mac = {0x1} }
3708 }
3709 },
3710 [LEFTOVERS_UC] = {
3711 .flow_attr = {
3712 .num_of_specs = 1,
3713 .size = sizeof(leftovers_specs[0])
3714 },
3715 .eth_flow = {
3716 .type = IB_FLOW_SPEC_ETH,
3717 .size = sizeof(struct ib_flow_spec_eth),
3718 .mask = {.dst_mac = {0x1} },
3719 .val = {.dst_mac = {} }
3720 }
3721 }
3722 };
3723
3724 handler = create_flow_rule(dev, ft_prio,
3725 &leftovers_specs[LEFTOVERS_MC].flow_attr,
3726 dst);
3727 if (!IS_ERR(handler) &&
3728 flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT) {
3729 handler_ucast = create_flow_rule(dev, ft_prio,
3730 &leftovers_specs[LEFTOVERS_UC].flow_attr,
3731 dst);
3732 if (IS_ERR(handler_ucast)) {
Mark Bloch74491de2016-08-31 11:24:25 +00003733 mlx5_del_flow_rules(handler->rule);
Maor Gottliebd9d49802016-08-28 14:16:33 +03003734 ft_prio->refcount--;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003735 kfree(handler);
3736 handler = handler_ucast;
3737 } else {
3738 list_add(&handler_ucast->list, &handler->list);
3739 }
3740 }
3741
3742 return handler;
3743}
3744
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003745static struct mlx5_ib_flow_handler *create_sniffer_rule(struct mlx5_ib_dev *dev,
3746 struct mlx5_ib_flow_prio *ft_rx,
3747 struct mlx5_ib_flow_prio *ft_tx,
3748 struct mlx5_flow_destination *dst)
3749{
3750 struct mlx5_ib_flow_handler *handler_rx;
3751 struct mlx5_ib_flow_handler *handler_tx;
3752 int err;
3753 static const struct ib_flow_attr flow_attr = {
3754 .num_of_specs = 0,
3755 .size = sizeof(flow_attr)
3756 };
3757
3758 handler_rx = create_flow_rule(dev, ft_rx, &flow_attr, dst);
3759 if (IS_ERR(handler_rx)) {
3760 err = PTR_ERR(handler_rx);
3761 goto err;
3762 }
3763
3764 handler_tx = create_flow_rule(dev, ft_tx, &flow_attr, dst);
3765 if (IS_ERR(handler_tx)) {
3766 err = PTR_ERR(handler_tx);
3767 goto err_tx;
3768 }
3769
3770 list_add(&handler_tx->list, &handler_rx->list);
3771
3772 return handler_rx;
3773
3774err_tx:
Mark Bloch74491de2016-08-31 11:24:25 +00003775 mlx5_del_flow_rules(handler_rx->rule);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003776 ft_rx->refcount--;
3777 kfree(handler_rx);
3778err:
3779 return ERR_PTR(err);
3780}
3781
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003782static struct ib_flow *mlx5_ib_create_flow(struct ib_qp *qp,
3783 struct ib_flow_attr *flow_attr,
Matan Barak59082a32018-05-31 16:43:35 +03003784 int domain,
3785 struct ib_udata *udata)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003786{
3787 struct mlx5_ib_dev *dev = to_mdev(qp->device);
Yishai Hadasd9f88e52016-08-28 10:58:37 +03003788 struct mlx5_ib_qp *mqp = to_mqp(qp);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003789 struct mlx5_ib_flow_handler *handler = NULL;
3790 struct mlx5_flow_destination *dst = NULL;
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003791 struct mlx5_ib_flow_prio *ft_prio_tx = NULL;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003792 struct mlx5_ib_flow_prio *ft_prio;
Aviad Yehezkel802c2122018-03-28 09:27:53 +03003793 bool is_egress = flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS;
Raed Salem3b3233f2018-05-31 16:43:39 +03003794 struct mlx5_ib_create_flow *ucmd = NULL, ucmd_hdr;
3795 size_t min_ucmd_sz, required_ucmd_sz;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003796 int err;
Yishai Hadasa550ddf2017-08-17 15:52:33 +03003797 int underlay_qpn;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003798
Raed Salem3b3233f2018-05-31 16:43:39 +03003799 if (udata && udata->inlen) {
3800 min_ucmd_sz = offsetof(typeof(ucmd_hdr), reserved) +
3801 sizeof(ucmd_hdr.reserved);
3802 if (udata->inlen < min_ucmd_sz)
3803 return ERR_PTR(-EOPNOTSUPP);
3804
3805 err = ib_copy_from_udata(&ucmd_hdr, udata, min_ucmd_sz);
3806 if (err)
3807 return ERR_PTR(err);
3808
3809 /* currently supports only one counters data */
3810 if (ucmd_hdr.ncounters_data > 1)
3811 return ERR_PTR(-EINVAL);
3812
3813 required_ucmd_sz = min_ucmd_sz +
3814 sizeof(struct mlx5_ib_flow_counters_data) *
3815 ucmd_hdr.ncounters_data;
3816 if (udata->inlen > required_ucmd_sz &&
3817 !ib_is_udata_cleared(udata, required_ucmd_sz,
3818 udata->inlen - required_ucmd_sz))
3819 return ERR_PTR(-EOPNOTSUPP);
3820
3821 ucmd = kzalloc(required_ucmd_sz, GFP_KERNEL);
3822 if (!ucmd)
3823 return ERR_PTR(-ENOMEM);
3824
3825 err = ib_copy_from_udata(ucmd, udata, required_ucmd_sz);
Gustavo A. R. Silva299eafe2018-06-07 14:19:15 -05003826 if (err)
3827 goto free_ucmd;
Raed Salem3b3233f2018-05-31 16:43:39 +03003828 }
Matan Barak59082a32018-05-31 16:43:35 +03003829
Gustavo A. R. Silva299eafe2018-06-07 14:19:15 -05003830 if (flow_attr->priority > MLX5_IB_FLOW_LAST_PRIO) {
3831 err = -ENOMEM;
3832 goto free_ucmd;
3833 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003834
3835 if (domain != IB_FLOW_DOMAIN_USER ||
Daniel Jurgens508562d2018-01-04 17:25:34 +02003836 flow_attr->port > dev->num_ports ||
Aviad Yehezkel802c2122018-03-28 09:27:53 +03003837 (flow_attr->flags & ~(IB_FLOW_ATTR_FLAGS_DONT_TRAP |
Gustavo A. R. Silva299eafe2018-06-07 14:19:15 -05003838 IB_FLOW_ATTR_FLAGS_EGRESS))) {
3839 err = -EINVAL;
3840 goto free_ucmd;
3841 }
Aviad Yehezkel802c2122018-03-28 09:27:53 +03003842
3843 if (is_egress &&
3844 (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
Gustavo A. R. Silva299eafe2018-06-07 14:19:15 -05003845 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT)) {
3846 err = -EINVAL;
3847 goto free_ucmd;
3848 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003849
3850 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
Gustavo A. R. Silva299eafe2018-06-07 14:19:15 -05003851 if (!dst) {
3852 err = -ENOMEM;
3853 goto free_ucmd;
3854 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003855
Mark Bloch9a4ca382018-01-16 14:42:35 +00003856 mutex_lock(&dev->flow_db->lock);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003857
Aviad Yehezkel802c2122018-03-28 09:27:53 +03003858 ft_prio = get_flow_table(dev, flow_attr,
3859 is_egress ? MLX5_IB_FT_TX : MLX5_IB_FT_RX);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003860 if (IS_ERR(ft_prio)) {
3861 err = PTR_ERR(ft_prio);
3862 goto unlock;
3863 }
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003864 if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
3865 ft_prio_tx = get_flow_table(dev, flow_attr, MLX5_IB_FT_TX);
3866 if (IS_ERR(ft_prio_tx)) {
3867 err = PTR_ERR(ft_prio_tx);
3868 ft_prio_tx = NULL;
3869 goto destroy_ft;
3870 }
3871 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003872
Aviad Yehezkel802c2122018-03-28 09:27:53 +03003873 if (is_egress) {
3874 dst->type = MLX5_FLOW_DESTINATION_TYPE_PORT;
3875 } else {
3876 dst->type = MLX5_FLOW_DESTINATION_TYPE_TIR;
3877 if (mqp->flags & MLX5_IB_QP_RSS)
3878 dst->tir_num = mqp->rss_qp.tirn;
3879 else
3880 dst->tir_num = mqp->raw_packet_qp.rq.tirn;
3881 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003882
3883 if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
Maor Gottlieb35d190112016-03-07 18:51:47 +02003884 if (flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP) {
3885 handler = create_dont_trap_rule(dev, ft_prio,
3886 flow_attr, dst);
3887 } else {
Yishai Hadasa550ddf2017-08-17 15:52:33 +03003888 underlay_qpn = (mqp->flags & MLX5_IB_QP_UNDERLAY) ?
3889 mqp->underlay_qpn : 0;
3890 handler = _create_flow_rule(dev, ft_prio, flow_attr,
Raed Salem3b3233f2018-05-31 16:43:39 +03003891 dst, underlay_qpn, ucmd);
Maor Gottlieb35d190112016-03-07 18:51:47 +02003892 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003893 } else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
3894 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
3895 handler = create_leftovers_rule(dev, ft_prio, flow_attr,
3896 dst);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003897 } else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
3898 handler = create_sniffer_rule(dev, ft_prio, ft_prio_tx, dst);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003899 } else {
3900 err = -EINVAL;
3901 goto destroy_ft;
3902 }
3903
3904 if (IS_ERR(handler)) {
3905 err = PTR_ERR(handler);
3906 handler = NULL;
3907 goto destroy_ft;
3908 }
3909
Mark Bloch9a4ca382018-01-16 14:42:35 +00003910 mutex_unlock(&dev->flow_db->lock);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003911 kfree(dst);
Raed Salem3b3233f2018-05-31 16:43:39 +03003912 kfree(ucmd);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003913
3914 return &handler->ibflow;
3915
3916destroy_ft:
3917 put_flow_table(dev, ft_prio, false);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003918 if (ft_prio_tx)
3919 put_flow_table(dev, ft_prio_tx, false);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003920unlock:
Mark Bloch9a4ca382018-01-16 14:42:35 +00003921 mutex_unlock(&dev->flow_db->lock);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003922 kfree(dst);
Gustavo A. R. Silva299eafe2018-06-07 14:19:15 -05003923free_ucmd:
Raed Salem3b3233f2018-05-31 16:43:39 +03003924 kfree(ucmd);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003925 return ERR_PTR(err);
3926}
3927
Mark Blochb47fd4f2018-09-06 17:27:07 +03003928static struct mlx5_ib_flow_prio *
3929_get_flow_table(struct mlx5_ib_dev *dev,
3930 struct mlx5_ib_flow_matcher *fs_matcher,
3931 bool mcast)
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003932{
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003933 struct mlx5_flow_namespace *ns = NULL;
Mark Bloch13a43762019-03-28 15:46:21 +02003934 struct mlx5_ib_flow_prio *prio = NULL;
3935 int max_table_size = 0;
Maor Gottliebcecae742019-06-12 15:20:13 +03003936 bool esw_encap;
Mark Blochb47fd4f2018-09-06 17:27:07 +03003937 u32 flags = 0;
3938 int priority;
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003939
Mark Bloch13a43762019-03-28 15:46:21 +02003940 if (mcast)
3941 priority = MLX5_IB_FLOW_MCAST_PRIO;
3942 else
3943 priority = ib_prio_to_core_prio(fs_matcher->priority, false);
3944
Maor Gottliebcecae742019-06-12 15:20:13 +03003945 esw_encap = mlx5_eswitch_get_encap_mode(dev->mdev) !=
3946 DEVLINK_ESWITCH_ENCAP_MODE_NONE;
Mark Blochb47fd4f2018-09-06 17:27:07 +03003947 if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_BYPASS) {
3948 max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
3949 log_max_ft_size));
Maor Gottliebcecae742019-06-12 15:20:13 +03003950 if (MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, decap) && !esw_encap)
Mark Blochb47fd4f2018-09-06 17:27:07 +03003951 flags |= MLX5_FLOW_TABLE_TUNNEL_EN_DECAP;
3952 if (MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
Maor Gottliebcecae742019-06-12 15:20:13 +03003953 reformat_l3_tunnel_to_l2) &&
3954 !esw_encap)
Mark Blochb47fd4f2018-09-06 17:27:07 +03003955 flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
Mark Bloch13a43762019-03-28 15:46:21 +02003956 } else if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_EGRESS) {
3957 max_table_size = BIT(
3958 MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev, log_max_ft_size));
Maor Gottliebcecae742019-06-12 15:20:13 +03003959 if (MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev, reformat) && !esw_encap)
Mark Blochb47fd4f2018-09-06 17:27:07 +03003960 flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
Mark Bloch13a43762019-03-28 15:46:21 +02003961 } else if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_FDB) {
3962 max_table_size = BIT(
3963 MLX5_CAP_ESW_FLOWTABLE_FDB(dev->mdev, log_max_ft_size));
Maor Gottlieb09d985b2019-06-12 15:20:14 +03003964 if (MLX5_CAP_ESW_FLOWTABLE_FDB(dev->mdev, decap) && esw_encap)
3965 flags |= MLX5_FLOW_TABLE_TUNNEL_EN_DECAP;
3966 if (MLX5_CAP_ESW_FLOWTABLE_FDB(dev->mdev, reformat_l3_tunnel_to_l2) &&
3967 esw_encap)
3968 flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
Mark Bloch13a43762019-03-28 15:46:21 +02003969 priority = FDB_BYPASS_PATH;
Mark Blochb47fd4f2018-09-06 17:27:07 +03003970 }
3971
Mark Bloch3b705082019-03-28 15:46:22 +02003972 max_table_size = min_t(int, max_table_size, MLX5_FS_MAX_ENTRIES);
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003973
Mark Blochb47fd4f2018-09-06 17:27:07 +03003974 ns = mlx5_get_flow_namespace(dev->mdev, fs_matcher->ns_type);
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003975 if (!ns)
3976 return ERR_PTR(-ENOTSUPP);
3977
Mark Blochb47fd4f2018-09-06 17:27:07 +03003978 if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_BYPASS)
3979 prio = &dev->flow_db->prios[priority];
Mark Bloch13a43762019-03-28 15:46:21 +02003980 else if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_EGRESS)
Mark Blochb47fd4f2018-09-06 17:27:07 +03003981 prio = &dev->flow_db->egress_prios[priority];
Mark Bloch13a43762019-03-28 15:46:21 +02003982 else if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_FDB)
3983 prio = &dev->flow_db->fdb;
3984
3985 if (!prio)
3986 return ERR_PTR(-EINVAL);
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003987
3988 if (prio->flow_table)
3989 return prio;
3990
Mark Bloch3b705082019-03-28 15:46:22 +02003991 return _get_prio(ns, prio, priority, max_table_size,
Mark Blochb47fd4f2018-09-06 17:27:07 +03003992 MLX5_FS_MAX_TYPES, flags);
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003993}
3994
3995static struct mlx5_ib_flow_handler *
3996_create_raw_flow_rule(struct mlx5_ib_dev *dev,
3997 struct mlx5_ib_flow_prio *ft_prio,
3998 struct mlx5_flow_destination *dst,
3999 struct mlx5_ib_flow_matcher *fs_matcher,
Jianbo Liubb0ee7d2019-06-25 17:47:58 +00004000 struct mlx5_flow_context *flow_context,
Mark Blochb823dd62018-09-06 17:27:05 +03004001 struct mlx5_flow_act *flow_act,
Mark Blochbfc5d832018-11-20 20:31:08 +02004002 void *cmd_in, int inlen,
4003 int dst_num)
Yishai Hadasd4be3f42018-07-23 15:25:10 +03004004{
4005 struct mlx5_ib_flow_handler *handler;
Yishai Hadasd4be3f42018-07-23 15:25:10 +03004006 struct mlx5_flow_spec *spec;
4007 struct mlx5_flow_table *ft = ft_prio->flow_table;
4008 int err = 0;
4009
4010 spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
4011 handler = kzalloc(sizeof(*handler), GFP_KERNEL);
4012 if (!handler || !spec) {
4013 err = -ENOMEM;
4014 goto free;
4015 }
4016
4017 INIT_LIST_HEAD(&handler->list);
4018
4019 memcpy(spec->match_value, cmd_in, inlen);
4020 memcpy(spec->match_criteria, fs_matcher->matcher_mask.match_params,
4021 fs_matcher->mask_len);
4022 spec->match_criteria_enable = fs_matcher->match_criteria_enable;
Jianbo Liubb0ee7d2019-06-25 17:47:58 +00004023 spec->flow_context = *flow_context;
Yishai Hadasd4be3f42018-07-23 15:25:10 +03004024
Yishai Hadasd4be3f42018-07-23 15:25:10 +03004025 handler->rule = mlx5_add_flow_rules(ft, spec,
Mark Blochbfc5d832018-11-20 20:31:08 +02004026 flow_act, dst, dst_num);
Yishai Hadasd4be3f42018-07-23 15:25:10 +03004027
4028 if (IS_ERR(handler->rule)) {
4029 err = PTR_ERR(handler->rule);
4030 goto free;
4031 }
4032
4033 ft_prio->refcount++;
4034 handler->prio = ft_prio;
4035 handler->dev = dev;
4036 ft_prio->flow_table = ft;
4037
4038free:
4039 if (err)
4040 kfree(handler);
4041 kvfree(spec);
4042 return err ? ERR_PTR(err) : handler;
4043}
4044
4045static bool raw_fs_is_multicast(struct mlx5_ib_flow_matcher *fs_matcher,
4046 void *match_v)
4047{
4048 void *match_c;
4049 void *match_v_set_lyr_2_4, *match_c_set_lyr_2_4;
4050 void *dmac, *dmac_mask;
4051 void *ipv4, *ipv4_mask;
4052
4053 if (!(fs_matcher->match_criteria_enable &
4054 (1 << MATCH_CRITERIA_ENABLE_OUTER_BIT)))
4055 return false;
4056
4057 match_c = fs_matcher->matcher_mask.match_params;
4058 match_v_set_lyr_2_4 = MLX5_ADDR_OF(fte_match_param, match_v,
4059 outer_headers);
4060 match_c_set_lyr_2_4 = MLX5_ADDR_OF(fte_match_param, match_c,
4061 outer_headers);
4062
4063 dmac = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_v_set_lyr_2_4,
4064 dmac_47_16);
4065 dmac_mask = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_c_set_lyr_2_4,
4066 dmac_47_16);
4067
4068 if (is_multicast_ether_addr(dmac) &&
4069 is_multicast_ether_addr(dmac_mask))
4070 return true;
4071
4072 ipv4 = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_v_set_lyr_2_4,
4073 dst_ipv4_dst_ipv6.ipv4_layout.ipv4);
4074
4075 ipv4_mask = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_c_set_lyr_2_4,
4076 dst_ipv4_dst_ipv6.ipv4_layout.ipv4);
4077
4078 if (ipv4_is_multicast(*(__be32 *)(ipv4)) &&
4079 ipv4_is_multicast(*(__be32 *)(ipv4_mask)))
4080 return true;
4081
4082 return false;
4083}
4084
Yishai Hadas32269442018-07-23 15:25:09 +03004085struct mlx5_ib_flow_handler *
4086mlx5_ib_raw_fs_rule_add(struct mlx5_ib_dev *dev,
4087 struct mlx5_ib_flow_matcher *fs_matcher,
Jianbo Liubb0ee7d2019-06-25 17:47:58 +00004088 struct mlx5_flow_context *flow_context,
Mark Blochb823dd62018-09-06 17:27:05 +03004089 struct mlx5_flow_act *flow_act,
Mark Blochbfc5d832018-11-20 20:31:08 +02004090 u32 counter_id,
Yishai Hadas32269442018-07-23 15:25:09 +03004091 void *cmd_in, int inlen, int dest_id,
4092 int dest_type)
4093{
Yishai Hadasd4be3f42018-07-23 15:25:10 +03004094 struct mlx5_flow_destination *dst;
4095 struct mlx5_ib_flow_prio *ft_prio;
Yishai Hadasd4be3f42018-07-23 15:25:10 +03004096 struct mlx5_ib_flow_handler *handler;
Mark Blochbfc5d832018-11-20 20:31:08 +02004097 int dst_num = 0;
Yishai Hadasd4be3f42018-07-23 15:25:10 +03004098 bool mcast;
4099 int err;
4100
4101 if (fs_matcher->flow_type != MLX5_IB_FLOW_TYPE_NORMAL)
4102 return ERR_PTR(-EOPNOTSUPP);
4103
4104 if (fs_matcher->priority > MLX5_IB_FLOW_LAST_PRIO)
4105 return ERR_PTR(-ENOMEM);
4106
Gustavo A. R. Silva8e8aa142019-01-15 00:00:48 -06004107 dst = kcalloc(2, sizeof(*dst), GFP_KERNEL);
Yishai Hadasd4be3f42018-07-23 15:25:10 +03004108 if (!dst)
4109 return ERR_PTR(-ENOMEM);
4110
4111 mcast = raw_fs_is_multicast(fs_matcher, cmd_in);
4112 mutex_lock(&dev->flow_db->lock);
4113
Mark Blochb47fd4f2018-09-06 17:27:07 +03004114 ft_prio = _get_flow_table(dev, fs_matcher, mcast);
Yishai Hadasd4be3f42018-07-23 15:25:10 +03004115 if (IS_ERR(ft_prio)) {
4116 err = PTR_ERR(ft_prio);
4117 goto unlock;
4118 }
4119
Yishai Hadas6346f0b2018-07-23 15:25:11 +03004120 if (dest_type == MLX5_FLOW_DESTINATION_TYPE_TIR) {
Mark Blochbfc5d832018-11-20 20:31:08 +02004121 dst[dst_num].type = dest_type;
4122 dst[dst_num].tir_num = dest_id;
Mark Blochb823dd62018-09-06 17:27:05 +03004123 flow_act->action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
Mark Blocha7ee18b2018-09-06 17:27:08 +03004124 } else if (dest_type == MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE) {
Mark Blochbfc5d832018-11-20 20:31:08 +02004125 dst[dst_num].type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE_NUM;
4126 dst[dst_num].ft_num = dest_id;
Mark Blochb823dd62018-09-06 17:27:05 +03004127 flow_act->action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
Mark Blocha7ee18b2018-09-06 17:27:08 +03004128 } else {
Mark Blochbfc5d832018-11-20 20:31:08 +02004129 dst[dst_num].type = MLX5_FLOW_DESTINATION_TYPE_PORT;
Mark Blocha7ee18b2018-09-06 17:27:08 +03004130 flow_act->action |= MLX5_FLOW_CONTEXT_ACTION_ALLOW;
Yishai Hadas6346f0b2018-07-23 15:25:11 +03004131 }
4132
Mark Blochbfc5d832018-11-20 20:31:08 +02004133 dst_num++;
4134
4135 if (flow_act->action & MLX5_FLOW_CONTEXT_ACTION_COUNT) {
4136 dst[dst_num].type = MLX5_FLOW_DESTINATION_TYPE_COUNTER;
4137 dst[dst_num].counter_id = counter_id;
4138 dst_num++;
4139 }
4140
Jianbo Liubb0ee7d2019-06-25 17:47:58 +00004141 handler = _create_raw_flow_rule(dev, ft_prio, dst, fs_matcher,
4142 flow_context, flow_act,
Mark Blochbfc5d832018-11-20 20:31:08 +02004143 cmd_in, inlen, dst_num);
Yishai Hadasd4be3f42018-07-23 15:25:10 +03004144
4145 if (IS_ERR(handler)) {
4146 err = PTR_ERR(handler);
4147 goto destroy_ft;
4148 }
4149
4150 mutex_unlock(&dev->flow_db->lock);
4151 atomic_inc(&fs_matcher->usecnt);
4152 handler->flow_matcher = fs_matcher;
4153
4154 kfree(dst);
4155
4156 return handler;
4157
4158destroy_ft:
4159 put_flow_table(dev, ft_prio, false);
4160unlock:
4161 mutex_unlock(&dev->flow_db->lock);
4162 kfree(dst);
4163
4164 return ERR_PTR(err);
Yishai Hadas32269442018-07-23 15:25:09 +03004165}
4166
Aviad Yehezkelc6475a02018-03-28 09:27:50 +03004167static u32 mlx5_ib_flow_action_flags_to_accel_xfrm_flags(u32 mlx5_flags)
4168{
4169 u32 flags = 0;
4170
4171 if (mlx5_flags & MLX5_IB_UAPI_FLOW_ACTION_FLAGS_REQUIRE_METADATA)
4172 flags |= MLX5_ACCEL_XFRM_FLAG_REQUIRE_METADATA;
4173
4174 return flags;
4175}
4176
4177#define MLX5_FLOW_ACTION_ESP_CREATE_LAST_SUPPORTED MLX5_IB_UAPI_FLOW_ACTION_FLAGS_REQUIRE_METADATA
4178static struct ib_flow_action *
4179mlx5_ib_create_flow_action_esp(struct ib_device *device,
4180 const struct ib_flow_action_attrs_esp *attr,
4181 struct uverbs_attr_bundle *attrs)
4182{
4183 struct mlx5_ib_dev *mdev = to_mdev(device);
4184 struct ib_uverbs_flow_action_esp_keymat_aes_gcm *aes_gcm;
4185 struct mlx5_accel_esp_xfrm_attrs accel_attrs = {};
4186 struct mlx5_ib_flow_action *action;
4187 u64 action_flags;
4188 u64 flags;
4189 int err = 0;
4190
Jason Gunthorpebccd0622018-07-26 16:37:14 -06004191 err = uverbs_get_flags64(
4192 &action_flags, attrs, MLX5_IB_ATTR_CREATE_FLOW_ACTION_FLAGS,
4193 ((MLX5_FLOW_ACTION_ESP_CREATE_LAST_SUPPORTED << 1) - 1));
4194 if (err)
4195 return ERR_PTR(err);
Aviad Yehezkelc6475a02018-03-28 09:27:50 +03004196
4197 flags = mlx5_ib_flow_action_flags_to_accel_xfrm_flags(action_flags);
4198
4199 /* We current only support a subset of the standard features. Only a
4200 * keymat of type AES_GCM, with icv_len == 16, iv_algo == SEQ and esn
4201 * (with overlap). Full offload mode isn't supported.
4202 */
4203 if (!attr->keymat || attr->replay || attr->encap ||
4204 attr->spi || attr->seq || attr->tfc_pad ||
4205 attr->hard_limit_pkts ||
4206 (attr->flags & ~(IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED |
4207 IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ENCRYPT)))
4208 return ERR_PTR(-EOPNOTSUPP);
4209
4210 if (attr->keymat->protocol !=
4211 IB_UVERBS_FLOW_ACTION_ESP_KEYMAT_AES_GCM)
4212 return ERR_PTR(-EOPNOTSUPP);
4213
4214 aes_gcm = &attr->keymat->keymat.aes_gcm;
4215
4216 if (aes_gcm->icv_len != 16 ||
4217 aes_gcm->iv_algo != IB_UVERBS_FLOW_ACTION_IV_ALGO_SEQ)
4218 return ERR_PTR(-EOPNOTSUPP);
4219
4220 action = kmalloc(sizeof(*action), GFP_KERNEL);
4221 if (!action)
4222 return ERR_PTR(-ENOMEM);
4223
4224 action->esp_aes_gcm.ib_flags = attr->flags;
4225 memcpy(&accel_attrs.keymat.aes_gcm.aes_key, &aes_gcm->aes_key,
4226 sizeof(accel_attrs.keymat.aes_gcm.aes_key));
4227 accel_attrs.keymat.aes_gcm.key_len = aes_gcm->key_len * 8;
4228 memcpy(&accel_attrs.keymat.aes_gcm.salt, &aes_gcm->salt,
4229 sizeof(accel_attrs.keymat.aes_gcm.salt));
4230 memcpy(&accel_attrs.keymat.aes_gcm.seq_iv, &aes_gcm->iv,
4231 sizeof(accel_attrs.keymat.aes_gcm.seq_iv));
4232 accel_attrs.keymat.aes_gcm.icv_len = aes_gcm->icv_len * 8;
4233 accel_attrs.keymat.aes_gcm.iv_algo = MLX5_ACCEL_ESP_AES_GCM_IV_ALGO_SEQ;
4234 accel_attrs.keymat_type = MLX5_ACCEL_ESP_KEYMAT_AES_GCM;
4235
4236 accel_attrs.esn = attr->esn;
4237 if (attr->flags & IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED)
4238 accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_TRIGGERED;
4239 if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW)
4240 accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP;
4241
4242 if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ENCRYPT)
4243 accel_attrs.action |= MLX5_ACCEL_ESP_ACTION_ENCRYPT;
4244
4245 action->esp_aes_gcm.ctx =
4246 mlx5_accel_esp_create_xfrm(mdev->mdev, &accel_attrs, flags);
4247 if (IS_ERR(action->esp_aes_gcm.ctx)) {
4248 err = PTR_ERR(action->esp_aes_gcm.ctx);
4249 goto err_parse;
4250 }
4251
4252 action->esp_aes_gcm.ib_flags = attr->flags;
4253
4254 return &action->ib_action;
4255
4256err_parse:
4257 kfree(action);
4258 return ERR_PTR(err);
4259}
4260
Matan Barak349705c2018-03-28 09:27:51 +03004261static int
4262mlx5_ib_modify_flow_action_esp(struct ib_flow_action *action,
4263 const struct ib_flow_action_attrs_esp *attr,
4264 struct uverbs_attr_bundle *attrs)
4265{
4266 struct mlx5_ib_flow_action *maction = to_mflow_act(action);
4267 struct mlx5_accel_esp_xfrm_attrs accel_attrs;
4268 int err = 0;
4269
4270 if (attr->keymat || attr->replay || attr->encap ||
4271 attr->spi || attr->seq || attr->tfc_pad ||
4272 attr->hard_limit_pkts ||
4273 (attr->flags & ~(IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED |
4274 IB_FLOW_ACTION_ESP_FLAGS_MOD_ESP_ATTRS |
4275 IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW)))
4276 return -EOPNOTSUPP;
4277
4278 /* Only the ESN value or the MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP can
4279 * be modified.
4280 */
4281 if (!(maction->esp_aes_gcm.ib_flags &
4282 IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED) &&
4283 attr->flags & (IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED |
4284 IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW))
4285 return -EINVAL;
4286
4287 memcpy(&accel_attrs, &maction->esp_aes_gcm.ctx->attrs,
4288 sizeof(accel_attrs));
4289
4290 accel_attrs.esn = attr->esn;
4291 if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW)
4292 accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP;
4293 else
4294 accel_attrs.flags &= ~MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP;
4295
4296 err = mlx5_accel_esp_modify_xfrm(maction->esp_aes_gcm.ctx,
4297 &accel_attrs);
4298 if (err)
4299 return err;
4300
4301 maction->esp_aes_gcm.ib_flags &=
4302 ~IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW;
4303 maction->esp_aes_gcm.ib_flags |=
4304 attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW;
4305
4306 return 0;
4307}
4308
Aviad Yehezkelc6475a02018-03-28 09:27:50 +03004309static int mlx5_ib_destroy_flow_action(struct ib_flow_action *action)
4310{
4311 struct mlx5_ib_flow_action *maction = to_mflow_act(action);
4312
4313 switch (action->type) {
4314 case IB_FLOW_ACTION_ESP:
4315 /*
4316 * We only support aes_gcm by now, so we implicitly know this is
4317 * the underline crypto.
4318 */
4319 mlx5_accel_esp_destroy_xfrm(maction->esp_aes_gcm.ctx);
4320 break;
Mark Blochb4749bf2018-08-28 14:18:51 +03004321 case IB_FLOW_ACTION_UNSPECIFIED:
4322 mlx5_ib_destroy_flow_action_raw(maction);
4323 break;
Aviad Yehezkelc6475a02018-03-28 09:27:50 +03004324 default:
4325 WARN_ON(true);
4326 break;
4327 }
4328
4329 kfree(maction);
4330 return 0;
4331}
4332
Eli Cohene126ba92013-07-07 17:25:49 +03004333static int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
4334{
4335 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
Yishai Hadas81e30882017-06-08 16:15:09 +03004336 struct mlx5_ib_qp *mqp = to_mqp(ibqp);
Eli Cohene126ba92013-07-07 17:25:49 +03004337 int err;
Yishai Hadas539ec982018-09-20 21:39:25 +03004338 u16 uid;
4339
4340 uid = ibqp->pd ?
4341 to_mpd(ibqp->pd)->uid : 0;
Eli Cohene126ba92013-07-07 17:25:49 +03004342
Yishai Hadas81e30882017-06-08 16:15:09 +03004343 if (mqp->flags & MLX5_IB_QP_UNDERLAY) {
4344 mlx5_ib_dbg(dev, "Attaching a multi cast group to underlay QP is not supported\n");
4345 return -EOPNOTSUPP;
4346 }
4347
Yishai Hadas539ec982018-09-20 21:39:25 +03004348 err = mlx5_cmd_attach_mcg(dev->mdev, gid, ibqp->qp_num, uid);
Eli Cohene126ba92013-07-07 17:25:49 +03004349 if (err)
4350 mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n",
4351 ibqp->qp_num, gid->raw);
4352
4353 return err;
4354}
4355
4356static int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
4357{
4358 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
4359 int err;
Yishai Hadas539ec982018-09-20 21:39:25 +03004360 u16 uid;
Eli Cohene126ba92013-07-07 17:25:49 +03004361
Yishai Hadas539ec982018-09-20 21:39:25 +03004362 uid = ibqp->pd ?
4363 to_mpd(ibqp->pd)->uid : 0;
4364 err = mlx5_cmd_detach_mcg(dev->mdev, gid, ibqp->qp_num, uid);
Eli Cohene126ba92013-07-07 17:25:49 +03004365 if (err)
4366 mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n",
4367 ibqp->qp_num, gid->raw);
4368
4369 return err;
4370}
4371
4372static int init_node_data(struct mlx5_ib_dev *dev)
4373{
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03004374 int err;
Eli Cohene126ba92013-07-07 17:25:49 +03004375
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03004376 err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc);
Eli Cohene126ba92013-07-07 17:25:49 +03004377 if (err)
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03004378 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03004379
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03004380 dev->mdev->rev_id = dev->mdev->pdev->revision;
Eli Cohene126ba92013-07-07 17:25:49 +03004381
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03004382 return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid);
Eli Cohene126ba92013-07-07 17:25:49 +03004383}
4384
Parav Pandit508a5232018-10-11 22:31:54 +03004385static ssize_t fw_pages_show(struct device *device,
4386 struct device_attribute *attr, char *buf)
Eli Cohene126ba92013-07-07 17:25:49 +03004387{
4388 struct mlx5_ib_dev *dev =
Parav Pandit54747232018-12-18 14:15:56 +02004389 rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
Eli Cohene126ba92013-07-07 17:25:49 +03004390
Jack Morgenstein9603b612014-07-28 23:30:22 +03004391 return sprintf(buf, "%d\n", dev->mdev->priv.fw_pages);
Eli Cohene126ba92013-07-07 17:25:49 +03004392}
Parav Pandit508a5232018-10-11 22:31:54 +03004393static DEVICE_ATTR_RO(fw_pages);
Eli Cohene126ba92013-07-07 17:25:49 +03004394
Parav Pandit508a5232018-10-11 22:31:54 +03004395static ssize_t reg_pages_show(struct device *device,
Eli Cohene126ba92013-07-07 17:25:49 +03004396 struct device_attribute *attr, char *buf)
4397{
4398 struct mlx5_ib_dev *dev =
Parav Pandit54747232018-12-18 14:15:56 +02004399 rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
Eli Cohene126ba92013-07-07 17:25:49 +03004400
Haggai Eran6aec21f2014-12-11 17:04:23 +02004401 return sprintf(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages));
Eli Cohene126ba92013-07-07 17:25:49 +03004402}
Parav Pandit508a5232018-10-11 22:31:54 +03004403static DEVICE_ATTR_RO(reg_pages);
Eli Cohene126ba92013-07-07 17:25:49 +03004404
Parav Pandit508a5232018-10-11 22:31:54 +03004405static ssize_t hca_type_show(struct device *device,
4406 struct device_attribute *attr, char *buf)
Eli Cohene126ba92013-07-07 17:25:49 +03004407{
4408 struct mlx5_ib_dev *dev =
Parav Pandit54747232018-12-18 14:15:56 +02004409 rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
4410
Jack Morgenstein9603b612014-07-28 23:30:22 +03004411 return sprintf(buf, "MT%d\n", dev->mdev->pdev->device);
Eli Cohene126ba92013-07-07 17:25:49 +03004412}
Parav Pandit508a5232018-10-11 22:31:54 +03004413static DEVICE_ATTR_RO(hca_type);
Eli Cohene126ba92013-07-07 17:25:49 +03004414
Parav Pandit508a5232018-10-11 22:31:54 +03004415static ssize_t hw_rev_show(struct device *device,
4416 struct device_attribute *attr, char *buf)
Eli Cohene126ba92013-07-07 17:25:49 +03004417{
4418 struct mlx5_ib_dev *dev =
Parav Pandit54747232018-12-18 14:15:56 +02004419 rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
4420
Jack Morgenstein9603b612014-07-28 23:30:22 +03004421 return sprintf(buf, "%x\n", dev->mdev->rev_id);
Eli Cohene126ba92013-07-07 17:25:49 +03004422}
Parav Pandit508a5232018-10-11 22:31:54 +03004423static DEVICE_ATTR_RO(hw_rev);
Eli Cohene126ba92013-07-07 17:25:49 +03004424
Parav Pandit508a5232018-10-11 22:31:54 +03004425static ssize_t board_id_show(struct device *device,
4426 struct device_attribute *attr, char *buf)
Eli Cohene126ba92013-07-07 17:25:49 +03004427{
4428 struct mlx5_ib_dev *dev =
Parav Pandit54747232018-12-18 14:15:56 +02004429 rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
4430
Eli Cohene126ba92013-07-07 17:25:49 +03004431 return sprintf(buf, "%.*s\n", MLX5_BOARD_ID_LEN,
Jack Morgenstein9603b612014-07-28 23:30:22 +03004432 dev->mdev->board_id);
Eli Cohene126ba92013-07-07 17:25:49 +03004433}
Parav Pandit508a5232018-10-11 22:31:54 +03004434static DEVICE_ATTR_RO(board_id);
Eli Cohene126ba92013-07-07 17:25:49 +03004435
Parav Pandit508a5232018-10-11 22:31:54 +03004436static struct attribute *mlx5_class_attributes[] = {
4437 &dev_attr_hw_rev.attr,
4438 &dev_attr_hca_type.attr,
4439 &dev_attr_board_id.attr,
4440 &dev_attr_fw_pages.attr,
4441 &dev_attr_reg_pages.attr,
4442 NULL,
4443};
Eli Cohene126ba92013-07-07 17:25:49 +03004444
Parav Pandit508a5232018-10-11 22:31:54 +03004445static const struct attribute_group mlx5_attr_group = {
4446 .attrs = mlx5_class_attributes,
Eli Cohene126ba92013-07-07 17:25:49 +03004447};
4448
Haggai Eran7722f472016-02-29 15:45:07 +02004449static void pkey_change_handler(struct work_struct *work)
4450{
4451 struct mlx5_ib_port_resources *ports =
4452 container_of(work, struct mlx5_ib_port_resources,
4453 pkey_change_work);
4454
4455 mutex_lock(&ports->devr->mutex);
4456 mlx5_ib_gsi_pkey_change(ports->gsi);
4457 mutex_unlock(&ports->devr->mutex);
4458}
4459
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03004460static void mlx5_ib_handle_internal_error(struct mlx5_ib_dev *ibdev)
4461{
4462 struct mlx5_ib_qp *mqp;
4463 struct mlx5_ib_cq *send_mcq, *recv_mcq;
4464 struct mlx5_core_cq *mcq;
4465 struct list_head cq_armed_list;
4466 unsigned long flags_qp;
4467 unsigned long flags_cq;
4468 unsigned long flags;
4469
4470 INIT_LIST_HEAD(&cq_armed_list);
4471
4472 /* Go over qp list reside on that ibdev, sync with create/destroy qp.*/
4473 spin_lock_irqsave(&ibdev->reset_flow_resource_lock, flags);
4474 list_for_each_entry(mqp, &ibdev->qp_list, qps_list) {
4475 spin_lock_irqsave(&mqp->sq.lock, flags_qp);
4476 if (mqp->sq.tail != mqp->sq.head) {
4477 send_mcq = to_mcq(mqp->ibqp.send_cq);
4478 spin_lock_irqsave(&send_mcq->lock, flags_cq);
4479 if (send_mcq->mcq.comp &&
4480 mqp->ibqp.send_cq->comp_handler) {
4481 if (!send_mcq->mcq.reset_notify_added) {
4482 send_mcq->mcq.reset_notify_added = 1;
4483 list_add_tail(&send_mcq->mcq.reset_notify,
4484 &cq_armed_list);
4485 }
4486 }
4487 spin_unlock_irqrestore(&send_mcq->lock, flags_cq);
4488 }
4489 spin_unlock_irqrestore(&mqp->sq.lock, flags_qp);
4490 spin_lock_irqsave(&mqp->rq.lock, flags_qp);
4491 /* no handling is needed for SRQ */
4492 if (!mqp->ibqp.srq) {
4493 if (mqp->rq.tail != mqp->rq.head) {
4494 recv_mcq = to_mcq(mqp->ibqp.recv_cq);
4495 spin_lock_irqsave(&recv_mcq->lock, flags_cq);
4496 if (recv_mcq->mcq.comp &&
4497 mqp->ibqp.recv_cq->comp_handler) {
4498 if (!recv_mcq->mcq.reset_notify_added) {
4499 recv_mcq->mcq.reset_notify_added = 1;
4500 list_add_tail(&recv_mcq->mcq.reset_notify,
4501 &cq_armed_list);
4502 }
4503 }
4504 spin_unlock_irqrestore(&recv_mcq->lock,
4505 flags_cq);
4506 }
4507 }
4508 spin_unlock_irqrestore(&mqp->rq.lock, flags_qp);
4509 }
4510 /*At that point all inflight post send were put to be executed as of we
4511 * lock/unlock above locks Now need to arm all involved CQs.
4512 */
4513 list_for_each_entry(mcq, &cq_armed_list, reset_notify) {
Yishai Hadas4e0e2ea2019-06-30 19:23:27 +03004514 mcq->comp(mcq, NULL);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03004515 }
4516 spin_unlock_irqrestore(&ibdev->reset_flow_resource_lock, flags);
4517}
4518
Maor Gottlieb03404e82017-05-30 10:29:13 +03004519static void delay_drop_handler(struct work_struct *work)
4520{
4521 int err;
4522 struct mlx5_ib_delay_drop *delay_drop =
4523 container_of(work, struct mlx5_ib_delay_drop,
4524 delay_drop_work);
4525
Maor Gottliebfe248c32017-05-30 10:29:14 +03004526 atomic_inc(&delay_drop->events_cnt);
4527
Maor Gottlieb03404e82017-05-30 10:29:13 +03004528 mutex_lock(&delay_drop->lock);
4529 err = mlx5_core_set_delay_drop(delay_drop->dev->mdev,
4530 delay_drop->timeout);
4531 if (err) {
4532 mlx5_ib_warn(delay_drop->dev, "Failed to set delay drop, timeout=%u\n",
4533 delay_drop->timeout);
4534 delay_drop->activate = false;
4535 }
4536 mutex_unlock(&delay_drop->lock);
4537}
4538
Saeed Mahameed09e574f2018-11-26 14:39:04 -08004539static void handle_general_event(struct mlx5_ib_dev *ibdev, struct mlx5_eqe *eqe,
4540 struct ib_event *ibev)
4541{
Aya Levin6cfdc7e2019-04-29 18:14:07 +00004542 u8 port = (eqe->data.port.port >> 4) & 0xf;
4543
Saeed Mahameed09e574f2018-11-26 14:39:04 -08004544 switch (eqe->sub_type) {
4545 case MLX5_GENERAL_SUBTYPE_DELAY_DROP_TIMEOUT:
Aya Levin6cfdc7e2019-04-29 18:14:07 +00004546 if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) ==
4547 IB_LINK_LAYER_ETHERNET)
4548 schedule_work(&ibdev->delay_drop.delay_drop_work);
Saeed Mahameed09e574f2018-11-26 14:39:04 -08004549 break;
4550 default: /* do nothing */
4551 return;
4552 }
4553}
4554
Saeed Mahameed134e9342018-11-26 14:39:02 -08004555static int handle_port_change(struct mlx5_ib_dev *ibdev, struct mlx5_eqe *eqe,
4556 struct ib_event *ibev)
4557{
4558 u8 port = (eqe->data.port.port >> 4) & 0xf;
4559
4560 ibev->element.port_num = port;
4561
4562 switch (eqe->sub_type) {
4563 case MLX5_PORT_CHANGE_SUBTYPE_ACTIVE:
4564 case MLX5_PORT_CHANGE_SUBTYPE_DOWN:
4565 case MLX5_PORT_CHANGE_SUBTYPE_INITIALIZED:
4566 /* In RoCE, port up/down events are handled in
4567 * mlx5_netdev_event().
4568 */
4569 if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) ==
4570 IB_LINK_LAYER_ETHERNET)
4571 return -EINVAL;
4572
4573 ibev->event = (eqe->sub_type == MLX5_PORT_CHANGE_SUBTYPE_ACTIVE) ?
4574 IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
4575 break;
4576
4577 case MLX5_PORT_CHANGE_SUBTYPE_LID:
4578 ibev->event = IB_EVENT_LID_CHANGE;
4579 break;
4580
4581 case MLX5_PORT_CHANGE_SUBTYPE_PKEY:
4582 ibev->event = IB_EVENT_PKEY_CHANGE;
4583 schedule_work(&ibdev->devr.ports[port - 1].pkey_change_work);
4584 break;
4585
4586 case MLX5_PORT_CHANGE_SUBTYPE_GUID:
4587 ibev->event = IB_EVENT_GID_CHANGE;
4588 break;
4589
4590 case MLX5_PORT_CHANGE_SUBTYPE_CLIENT_REREG:
4591 ibev->event = IB_EVENT_CLIENT_REREGISTER;
4592 break;
4593 default:
4594 return -EINVAL;
4595 }
4596
4597 return 0;
4598}
4599
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02004600static void mlx5_ib_handle_event(struct work_struct *_work)
Eli Cohene126ba92013-07-07 17:25:49 +03004601{
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02004602 struct mlx5_ib_event_work *work =
4603 container_of(_work, struct mlx5_ib_event_work, work);
4604 struct mlx5_ib_dev *ibdev;
Eli Cohene126ba92013-07-07 17:25:49 +03004605 struct ib_event ibev;
Eli Cohendbaaff22016-10-27 16:36:44 +03004606 bool fatal = false;
Eli Cohene126ba92013-07-07 17:25:49 +03004607
Saeed Mahameeddf097a22018-11-26 14:39:00 -08004608 if (work->is_slave) {
4609 ibdev = mlx5_ib_get_ibdev_from_mpi(work->mpi);
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02004610 if (!ibdev)
4611 goto out;
4612 } else {
Saeed Mahameeddf097a22018-11-26 14:39:00 -08004613 ibdev = work->dev;
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02004614 }
4615
4616 switch (work->event) {
Eli Cohene126ba92013-07-07 17:25:49 +03004617 case MLX5_DEV_EVENT_SYS_ERROR:
Eli Cohene126ba92013-07-07 17:25:49 +03004618 ibev.event = IB_EVENT_DEVICE_FATAL;
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03004619 mlx5_ib_handle_internal_error(ibdev);
Saeed Mahameed134e9342018-11-26 14:39:02 -08004620 ibev.element.port_num = (u8)(unsigned long)work->param;
Eli Cohendbaaff22016-10-27 16:36:44 +03004621 fatal = true;
Eli Cohene126ba92013-07-07 17:25:49 +03004622 break;
Saeed Mahameed134e9342018-11-26 14:39:02 -08004623 case MLX5_EVENT_TYPE_PORT_CHANGE:
4624 if (handle_port_change(ibdev, work->param, &ibev))
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02004625 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03004626 break;
Saeed Mahameed09e574f2018-11-26 14:39:04 -08004627 case MLX5_EVENT_TYPE_GENERAL_EVENT:
4628 handle_general_event(ibdev, work->param, &ibev);
4629 /* fall through */
Saeed Mahameedbdc37922016-09-29 19:35:38 +03004630 default:
Maor Gottlieb03404e82017-05-30 10:29:13 +03004631 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03004632 }
4633
Saeed Mahameed134e9342018-11-26 14:39:02 -08004634 ibev.device = &ibdev->ib_dev;
Eli Cohene126ba92013-07-07 17:25:49 +03004635
Saeed Mahameed134e9342018-11-26 14:39:02 -08004636 if (!rdma_is_port_valid(&ibdev->ib_dev, ibev.element.port_num)) {
4637 mlx5_ib_warn(ibdev, "warning: event on port %d\n", ibev.element.port_num);
Maor Gottlieb03404e82017-05-30 10:29:13 +03004638 goto out;
Eli Cohena0c84c32013-09-11 16:35:27 +03004639 }
4640
Eli Cohene126ba92013-07-07 17:25:49 +03004641 if (ibdev->ib_active)
4642 ib_dispatch_event(&ibev);
Eli Cohendbaaff22016-10-27 16:36:44 +03004643
4644 if (fatal)
4645 ibdev->ib_active = false;
Maor Gottlieb03404e82017-05-30 10:29:13 +03004646out:
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02004647 kfree(work);
4648}
4649
Saeed Mahameeddf097a22018-11-26 14:39:00 -08004650static int mlx5_ib_event(struct notifier_block *nb,
4651 unsigned long event, void *param)
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02004652{
4653 struct mlx5_ib_event_work *work;
4654
4655 work = kmalloc(sizeof(*work), GFP_ATOMIC);
Leon Romanovsky10bea9c2018-01-19 13:07:11 +02004656 if (!work)
Saeed Mahameeddf097a22018-11-26 14:39:00 -08004657 return NOTIFY_DONE;
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02004658
Leon Romanovsky10bea9c2018-01-19 13:07:11 +02004659 INIT_WORK(&work->work, mlx5_ib_handle_event);
Saeed Mahameeddf097a22018-11-26 14:39:00 -08004660 work->dev = container_of(nb, struct mlx5_ib_dev, mdev_events);
4661 work->is_slave = false;
Leon Romanovsky10bea9c2018-01-19 13:07:11 +02004662 work->param = param;
Leon Romanovsky10bea9c2018-01-19 13:07:11 +02004663 work->event = event;
4664
4665 queue_work(mlx5_ib_event_wq, &work->work);
Saeed Mahameeddf097a22018-11-26 14:39:00 -08004666
4667 return NOTIFY_OK;
4668}
4669
4670static int mlx5_ib_event_slave_port(struct notifier_block *nb,
4671 unsigned long event, void *param)
4672{
4673 struct mlx5_ib_event_work *work;
4674
4675 work = kmalloc(sizeof(*work), GFP_ATOMIC);
4676 if (!work)
4677 return NOTIFY_DONE;
4678
4679 INIT_WORK(&work->work, mlx5_ib_handle_event);
4680 work->mpi = container_of(nb, struct mlx5_ib_multiport_info, mdev_events);
4681 work->is_slave = true;
4682 work->param = param;
4683 work->event = event;
4684 queue_work(mlx5_ib_event_wq, &work->work);
4685
4686 return NOTIFY_OK;
Eli Cohene126ba92013-07-07 17:25:49 +03004687}
4688
Maor Gottliebc43f1112017-01-18 14:10:33 +02004689static int set_has_smi_cap(struct mlx5_ib_dev *dev)
4690{
4691 struct mlx5_hca_vport_context vport_ctx;
4692 int err;
4693 int port;
4694
Mark Blocha989ea02019-03-28 15:27:40 +02004695 for (port = 1; port <= ARRAY_SIZE(dev->mdev->port_caps); port++) {
Maor Gottliebc43f1112017-01-18 14:10:33 +02004696 dev->mdev->port_caps[port - 1].has_smi = false;
4697 if (MLX5_CAP_GEN(dev->mdev, port_type) ==
4698 MLX5_CAP_PORT_TYPE_IB) {
4699 if (MLX5_CAP_GEN(dev->mdev, ib_virt)) {
4700 err = mlx5_query_hca_vport_context(dev->mdev, 0,
4701 port, 0,
4702 &vport_ctx);
4703 if (err) {
4704 mlx5_ib_err(dev, "query_hca_vport_context for port=%d failed %d\n",
4705 port, err);
4706 return err;
4707 }
4708 dev->mdev->port_caps[port - 1].has_smi =
4709 vport_ctx.has_smi;
4710 } else {
4711 dev->mdev->port_caps[port - 1].has_smi = true;
4712 }
4713 }
4714 }
4715 return 0;
4716}
4717
Eli Cohene126ba92013-07-07 17:25:49 +03004718static void get_ext_port_caps(struct mlx5_ib_dev *dev)
4719{
4720 int port;
4721
Daniel Jurgens508562d2018-01-04 17:25:34 +02004722 for (port = 1; port <= dev->num_ports; port++)
Eli Cohene126ba92013-07-07 17:25:49 +03004723 mlx5_query_ext_port_caps(dev, port);
4724}
4725
Mark Bloch26628e22019-03-28 15:27:41 +02004726static int __get_port_caps(struct mlx5_ib_dev *dev, u8 port)
Eli Cohene126ba92013-07-07 17:25:49 +03004727{
4728 struct ib_device_attr *dprops = NULL;
4729 struct ib_port_attr *pprops = NULL;
Dan Carpenterf614fc12015-01-12 11:56:58 +03004730 int err = -ENOMEM;
Matan Barak2528e332015-06-11 16:35:25 +03004731 struct ib_udata uhw = {.inlen = 0, .outlen = 0};
Eli Cohene126ba92013-07-07 17:25:49 +03004732
Leon Romanovsky50ba3c12019-06-30 18:48:32 +03004733 pprops = kzalloc(sizeof(*pprops), GFP_KERNEL);
Eli Cohene126ba92013-07-07 17:25:49 +03004734 if (!pprops)
4735 goto out;
4736
4737 dprops = kmalloc(sizeof(*dprops), GFP_KERNEL);
4738 if (!dprops)
4739 goto out;
4740
Matan Barak2528e332015-06-11 16:35:25 +03004741 err = mlx5_ib_query_device(&dev->ib_dev, dprops, &uhw);
Eli Cohene126ba92013-07-07 17:25:49 +03004742 if (err) {
4743 mlx5_ib_warn(dev, "query_device failed %d\n", err);
4744 goto out;
4745 }
4746
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004747 err = mlx5_ib_query_port(&dev->ib_dev, port, pprops);
4748 if (err) {
4749 mlx5_ib_warn(dev, "query_port %d failed %d\n",
4750 port, err);
4751 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03004752 }
4753
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004754 dev->mdev->port_caps[port - 1].pkey_table_len =
4755 dprops->max_pkeys;
4756 dev->mdev->port_caps[port - 1].gid_table_len =
4757 pprops->gid_tbl_len;
4758 mlx5_ib_dbg(dev, "port %d: pkey_table_len %d, gid_table_len %d\n",
4759 port, dprops->max_pkeys, pprops->gid_tbl_len);
4760
Eli Cohene126ba92013-07-07 17:25:49 +03004761out:
4762 kfree(pprops);
4763 kfree(dprops);
4764
4765 return err;
4766}
4767
Mark Bloch26628e22019-03-28 15:27:41 +02004768static int get_port_caps(struct mlx5_ib_dev *dev, u8 port)
4769{
4770 /* For representors use port 1, is this is the only native
4771 * port
4772 */
4773 if (dev->is_rep)
4774 return __get_port_caps(dev, 1);
4775 return __get_port_caps(dev, port);
4776}
4777
Eli Cohene126ba92013-07-07 17:25:49 +03004778static void destroy_umrc_res(struct mlx5_ib_dev *dev)
4779{
4780 int err;
4781
4782 err = mlx5_mr_cache_cleanup(dev);
4783 if (err)
4784 mlx5_ib_warn(dev, "mr cache cleanup failed\n");
4785
Mark Bloch32927e22018-03-20 15:45:37 +02004786 if (dev->umrc.qp)
Shamir Rabinovitchc4367a22019-03-31 19:10:05 +03004787 mlx5_ib_destroy_qp(dev->umrc.qp, NULL);
Mark Bloch32927e22018-03-20 15:45:37 +02004788 if (dev->umrc.cq)
4789 ib_free_cq(dev->umrc.cq);
4790 if (dev->umrc.pd)
4791 ib_dealloc_pd(dev->umrc.pd);
Eli Cohene126ba92013-07-07 17:25:49 +03004792}
4793
4794enum {
4795 MAX_UMR_WR = 128,
4796};
4797
4798static int create_umr_res(struct mlx5_ib_dev *dev)
4799{
4800 struct ib_qp_init_attr *init_attr = NULL;
4801 struct ib_qp_attr *attr = NULL;
4802 struct ib_pd *pd;
4803 struct ib_cq *cq;
4804 struct ib_qp *qp;
Eli Cohene126ba92013-07-07 17:25:49 +03004805 int ret;
4806
4807 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
4808 init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL);
4809 if (!attr || !init_attr) {
4810 ret = -ENOMEM;
4811 goto error_0;
4812 }
4813
Christoph Hellwiged082d32016-09-05 12:56:17 +02004814 pd = ib_alloc_pd(&dev->ib_dev, 0);
Eli Cohene126ba92013-07-07 17:25:49 +03004815 if (IS_ERR(pd)) {
4816 mlx5_ib_dbg(dev, "Couldn't create PD for sync UMR QP\n");
4817 ret = PTR_ERR(pd);
4818 goto error_0;
4819 }
4820
Christoph Hellwigadd08d72016-03-03 09:38:22 +01004821 cq = ib_alloc_cq(&dev->ib_dev, NULL, 128, 0, IB_POLL_SOFTIRQ);
Eli Cohene126ba92013-07-07 17:25:49 +03004822 if (IS_ERR(cq)) {
4823 mlx5_ib_dbg(dev, "Couldn't create CQ for sync UMR QP\n");
4824 ret = PTR_ERR(cq);
4825 goto error_2;
4826 }
Eli Cohene126ba92013-07-07 17:25:49 +03004827
4828 init_attr->send_cq = cq;
4829 init_attr->recv_cq = cq;
4830 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
4831 init_attr->cap.max_send_wr = MAX_UMR_WR;
4832 init_attr->cap.max_send_sge = 1;
4833 init_attr->qp_type = MLX5_IB_QPT_REG_UMR;
4834 init_attr->port_num = 1;
4835 qp = mlx5_ib_create_qp(pd, init_attr, NULL);
4836 if (IS_ERR(qp)) {
4837 mlx5_ib_dbg(dev, "Couldn't create sync UMR QP\n");
4838 ret = PTR_ERR(qp);
4839 goto error_3;
4840 }
4841 qp->device = &dev->ib_dev;
4842 qp->real_qp = qp;
4843 qp->uobject = NULL;
4844 qp->qp_type = MLX5_IB_QPT_REG_UMR;
Majd Dibbiny31fde032017-10-30 14:23:13 +02004845 qp->send_cq = init_attr->send_cq;
4846 qp->recv_cq = init_attr->recv_cq;
Eli Cohene126ba92013-07-07 17:25:49 +03004847
4848 attr->qp_state = IB_QPS_INIT;
4849 attr->port_num = 1;
4850 ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_PKEY_INDEX |
4851 IB_QP_PORT, NULL);
4852 if (ret) {
4853 mlx5_ib_dbg(dev, "Couldn't modify UMR QP\n");
4854 goto error_4;
4855 }
4856
4857 memset(attr, 0, sizeof(*attr));
4858 attr->qp_state = IB_QPS_RTR;
4859 attr->path_mtu = IB_MTU_256;
4860
4861 ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
4862 if (ret) {
4863 mlx5_ib_dbg(dev, "Couldn't modify umr QP to rtr\n");
4864 goto error_4;
4865 }
4866
4867 memset(attr, 0, sizeof(*attr));
4868 attr->qp_state = IB_QPS_RTS;
4869 ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
4870 if (ret) {
4871 mlx5_ib_dbg(dev, "Couldn't modify umr QP to rts\n");
4872 goto error_4;
4873 }
4874
4875 dev->umrc.qp = qp;
4876 dev->umrc.cq = cq;
Eli Cohene126ba92013-07-07 17:25:49 +03004877 dev->umrc.pd = pd;
4878
4879 sema_init(&dev->umrc.sem, MAX_UMR_WR);
4880 ret = mlx5_mr_cache_init(dev);
4881 if (ret) {
4882 mlx5_ib_warn(dev, "mr cache init failed %d\n", ret);
4883 goto error_4;
4884 }
4885
4886 kfree(attr);
4887 kfree(init_attr);
4888
4889 return 0;
4890
4891error_4:
Shamir Rabinovitchc4367a22019-03-31 19:10:05 +03004892 mlx5_ib_destroy_qp(qp, NULL);
Mark Bloch32927e22018-03-20 15:45:37 +02004893 dev->umrc.qp = NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03004894
4895error_3:
Christoph Hellwigadd08d72016-03-03 09:38:22 +01004896 ib_free_cq(cq);
Mark Bloch32927e22018-03-20 15:45:37 +02004897 dev->umrc.cq = NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03004898
4899error_2:
Eli Cohene126ba92013-07-07 17:25:49 +03004900 ib_dealloc_pd(pd);
Mark Bloch32927e22018-03-20 15:45:37 +02004901 dev->umrc.pd = NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03004902
4903error_0:
4904 kfree(attr);
4905 kfree(init_attr);
4906 return ret;
4907}
4908
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03004909static u8 mlx5_get_umr_fence(u8 umr_fence_cap)
4910{
4911 switch (umr_fence_cap) {
4912 case MLX5_CAP_UMR_FENCE_NONE:
4913 return MLX5_FENCE_MODE_NONE;
4914 case MLX5_CAP_UMR_FENCE_SMALL:
4915 return MLX5_FENCE_MODE_INITIATOR_SMALL;
4916 default:
4917 return MLX5_FENCE_MODE_STRONG_ORDERING;
4918 }
4919}
4920
Eli Cohene126ba92013-07-07 17:25:49 +03004921static int create_dev_resources(struct mlx5_ib_resources *devr)
4922{
4923 struct ib_srq_init_attr attr;
4924 struct mlx5_ib_dev *dev;
Leon Romanovsky21a428a2019-02-03 14:55:51 +02004925 struct ib_device *ibdev;
Matan Barakbcf4c1e2015-06-11 16:35:20 +03004926 struct ib_cq_init_attr cq_attr = {.cqe = 1};
Haggai Eran7722f472016-02-29 15:45:07 +02004927 int port;
Eli Cohene126ba92013-07-07 17:25:49 +03004928 int ret = 0;
4929
4930 dev = container_of(devr, struct mlx5_ib_dev, devr);
Leon Romanovsky21a428a2019-02-03 14:55:51 +02004931 ibdev = &dev->ib_dev;
Eli Cohene126ba92013-07-07 17:25:49 +03004932
Haggai Erand16e91d2016-02-29 15:45:05 +02004933 mutex_init(&devr->mutex);
4934
Leon Romanovsky21a428a2019-02-03 14:55:51 +02004935 devr->p0 = rdma_zalloc_drv_obj(ibdev, ib_pd);
4936 if (!devr->p0)
4937 return -ENOMEM;
4938
4939 devr->p0->device = ibdev;
Eli Cohene126ba92013-07-07 17:25:49 +03004940 devr->p0->uobject = NULL;
4941 atomic_set(&devr->p0->usecnt, 0);
4942
Shamir Rabinovitchff23dfa2019-03-31 19:10:07 +03004943 ret = mlx5_ib_alloc_pd(devr->p0, NULL);
Leon Romanovsky21a428a2019-02-03 14:55:51 +02004944 if (ret)
4945 goto error0;
4946
Leon Romanovskye39afe32019-05-28 14:37:29 +03004947 devr->c0 = rdma_zalloc_drv_obj(ibdev, ib_cq);
4948 if (!devr->c0) {
4949 ret = -ENOMEM;
Eli Cohene126ba92013-07-07 17:25:49 +03004950 goto error1;
4951 }
Leon Romanovskye39afe32019-05-28 14:37:29 +03004952
4953 devr->c0->device = &dev->ib_dev;
Eli Cohene126ba92013-07-07 17:25:49 +03004954 atomic_set(&devr->c0->usecnt, 0);
4955
Leon Romanovskye39afe32019-05-28 14:37:29 +03004956 ret = mlx5_ib_create_cq(devr->c0, &cq_attr, NULL);
4957 if (ret)
4958 goto err_create_cq;
4959
Shamir Rabinovitchff23dfa2019-03-31 19:10:07 +03004960 devr->x0 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03004961 if (IS_ERR(devr->x0)) {
4962 ret = PTR_ERR(devr->x0);
4963 goto error2;
4964 }
4965 devr->x0->device = &dev->ib_dev;
4966 devr->x0->inode = NULL;
4967 atomic_set(&devr->x0->usecnt, 0);
4968 mutex_init(&devr->x0->tgt_qp_mutex);
4969 INIT_LIST_HEAD(&devr->x0->tgt_qp_list);
4970
Shamir Rabinovitchff23dfa2019-03-31 19:10:07 +03004971 devr->x1 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03004972 if (IS_ERR(devr->x1)) {
4973 ret = PTR_ERR(devr->x1);
4974 goto error3;
4975 }
4976 devr->x1->device = &dev->ib_dev;
4977 devr->x1->inode = NULL;
4978 atomic_set(&devr->x1->usecnt, 0);
4979 mutex_init(&devr->x1->tgt_qp_mutex);
4980 INIT_LIST_HEAD(&devr->x1->tgt_qp_list);
4981
4982 memset(&attr, 0, sizeof(attr));
4983 attr.attr.max_sge = 1;
4984 attr.attr.max_wr = 1;
4985 attr.srq_type = IB_SRQT_XRC;
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03004986 attr.ext.cq = devr->c0;
Eli Cohene126ba92013-07-07 17:25:49 +03004987 attr.ext.xrc.xrcd = devr->x0;
4988
Leon Romanovsky68e326d2019-04-03 16:42:43 +03004989 devr->s0 = rdma_zalloc_drv_obj(ibdev, ib_srq);
4990 if (!devr->s0) {
4991 ret = -ENOMEM;
Eli Cohene126ba92013-07-07 17:25:49 +03004992 goto error4;
4993 }
Leon Romanovsky68e326d2019-04-03 16:42:43 +03004994
Eli Cohene126ba92013-07-07 17:25:49 +03004995 devr->s0->device = &dev->ib_dev;
4996 devr->s0->pd = devr->p0;
Eli Cohene126ba92013-07-07 17:25:49 +03004997 devr->s0->srq_type = IB_SRQT_XRC;
4998 devr->s0->ext.xrc.xrcd = devr->x0;
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03004999 devr->s0->ext.cq = devr->c0;
Leon Romanovsky68e326d2019-04-03 16:42:43 +03005000 ret = mlx5_ib_create_srq(devr->s0, &attr, NULL);
5001 if (ret)
5002 goto err_create;
5003
Eli Cohene126ba92013-07-07 17:25:49 +03005004 atomic_inc(&devr->s0->ext.xrc.xrcd->usecnt);
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03005005 atomic_inc(&devr->s0->ext.cq->usecnt);
Eli Cohene126ba92013-07-07 17:25:49 +03005006 atomic_inc(&devr->p0->usecnt);
5007 atomic_set(&devr->s0->usecnt, 0);
5008
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03005009 memset(&attr, 0, sizeof(attr));
5010 attr.attr.max_sge = 1;
5011 attr.attr.max_wr = 1;
5012 attr.srq_type = IB_SRQT_BASIC;
Leon Romanovsky68e326d2019-04-03 16:42:43 +03005013 devr->s1 = rdma_zalloc_drv_obj(ibdev, ib_srq);
5014 if (!devr->s1) {
5015 ret = -ENOMEM;
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03005016 goto error5;
5017 }
Leon Romanovsky68e326d2019-04-03 16:42:43 +03005018
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03005019 devr->s1->device = &dev->ib_dev;
5020 devr->s1->pd = devr->p0;
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03005021 devr->s1->srq_type = IB_SRQT_BASIC;
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03005022 devr->s1->ext.cq = devr->c0;
Leon Romanovsky68e326d2019-04-03 16:42:43 +03005023
5024 ret = mlx5_ib_create_srq(devr->s1, &attr, NULL);
5025 if (ret)
5026 goto error6;
5027
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03005028 atomic_inc(&devr->p0->usecnt);
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03005029 atomic_set(&devr->s1->usecnt, 0);
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03005030
Haggai Eran7722f472016-02-29 15:45:07 +02005031 for (port = 0; port < ARRAY_SIZE(devr->ports); ++port) {
5032 INIT_WORK(&devr->ports[port].pkey_change_work,
5033 pkey_change_handler);
5034 devr->ports[port].devr = devr;
5035 }
5036
Eli Cohene126ba92013-07-07 17:25:49 +03005037 return 0;
5038
Leon Romanovsky68e326d2019-04-03 16:42:43 +03005039error6:
5040 kfree(devr->s1);
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03005041error5:
Shamir Rabinovitchc4367a22019-03-31 19:10:05 +03005042 mlx5_ib_destroy_srq(devr->s0, NULL);
Leon Romanovsky68e326d2019-04-03 16:42:43 +03005043err_create:
5044 kfree(devr->s0);
Eli Cohene126ba92013-07-07 17:25:49 +03005045error4:
Shamir Rabinovitchc4367a22019-03-31 19:10:05 +03005046 mlx5_ib_dealloc_xrcd(devr->x1, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03005047error3:
Shamir Rabinovitchc4367a22019-03-31 19:10:05 +03005048 mlx5_ib_dealloc_xrcd(devr->x0, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03005049error2:
Shamir Rabinovitchc4367a22019-03-31 19:10:05 +03005050 mlx5_ib_destroy_cq(devr->c0, NULL);
Leon Romanovskye39afe32019-05-28 14:37:29 +03005051err_create_cq:
5052 kfree(devr->c0);
Eli Cohene126ba92013-07-07 17:25:49 +03005053error1:
Shamir Rabinovitchc4367a22019-03-31 19:10:05 +03005054 mlx5_ib_dealloc_pd(devr->p0, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03005055error0:
Leon Romanovsky21a428a2019-02-03 14:55:51 +02005056 kfree(devr->p0);
Eli Cohene126ba92013-07-07 17:25:49 +03005057 return ret;
5058}
5059
5060static void destroy_dev_resources(struct mlx5_ib_resources *devr)
5061{
Haggai Eran7722f472016-02-29 15:45:07 +02005062 int port;
5063
Shamir Rabinovitchc4367a22019-03-31 19:10:05 +03005064 mlx5_ib_destroy_srq(devr->s1, NULL);
Leon Romanovsky68e326d2019-04-03 16:42:43 +03005065 kfree(devr->s1);
Shamir Rabinovitchc4367a22019-03-31 19:10:05 +03005066 mlx5_ib_destroy_srq(devr->s0, NULL);
Leon Romanovsky68e326d2019-04-03 16:42:43 +03005067 kfree(devr->s0);
Shamir Rabinovitchc4367a22019-03-31 19:10:05 +03005068 mlx5_ib_dealloc_xrcd(devr->x0, NULL);
5069 mlx5_ib_dealloc_xrcd(devr->x1, NULL);
5070 mlx5_ib_destroy_cq(devr->c0, NULL);
Leon Romanovskye39afe32019-05-28 14:37:29 +03005071 kfree(devr->c0);
Shamir Rabinovitchc4367a22019-03-31 19:10:05 +03005072 mlx5_ib_dealloc_pd(devr->p0, NULL);
Leon Romanovsky21a428a2019-02-03 14:55:51 +02005073 kfree(devr->p0);
Haggai Eran7722f472016-02-29 15:45:07 +02005074
5075 /* Make sure no change P_Key work items are still executing */
Mark Bloch5d8f6a02019-03-28 15:27:36 +02005076 for (port = 0; port < ARRAY_SIZE(devr->ports); ++port)
Haggai Eran7722f472016-02-29 15:45:07 +02005077 cancel_work_sync(&devr->ports[port].pkey_change_work);
Eli Cohene126ba92013-07-07 17:25:49 +03005078}
5079
Artemy Kovalyovb02289b2018-07-04 15:57:50 +03005080static u32 get_core_cap_flags(struct ib_device *ibdev,
5081 struct mlx5_hca_vport_context *rep)
Achiad Shochate53505a2015-12-23 18:47:25 +02005082{
5083 struct mlx5_ib_dev *dev = to_mdev(ibdev);
5084 enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, 1);
5085 u8 l3_type_cap = MLX5_CAP_ROCE(dev->mdev, l3_type);
5086 u8 roce_version_cap = MLX5_CAP_ROCE(dev->mdev, roce_version);
Daniel Jurgens85c7c012018-01-04 17:25:43 +02005087 bool raw_support = !mlx5_core_mp_enabled(dev->mdev);
Achiad Shochate53505a2015-12-23 18:47:25 +02005088 u32 ret = 0;
5089
Artemy Kovalyovb02289b2018-07-04 15:57:50 +03005090 if (rep->grh_required)
5091 ret |= RDMA_CORE_CAP_IB_GRH_REQUIRED;
5092
Achiad Shochate53505a2015-12-23 18:47:25 +02005093 if (ll == IB_LINK_LAYER_INFINIBAND)
Artemy Kovalyovb02289b2018-07-04 15:57:50 +03005094 return ret | RDMA_CORE_PORT_IBA_IB;
Achiad Shochate53505a2015-12-23 18:47:25 +02005095
Daniel Jurgens85c7c012018-01-04 17:25:43 +02005096 if (raw_support)
Artemy Kovalyovb02289b2018-07-04 15:57:50 +03005097 ret |= RDMA_CORE_PORT_RAW_PACKET;
Or Gerlitz72cd5712017-01-24 13:02:36 +02005098
Achiad Shochate53505a2015-12-23 18:47:25 +02005099 if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV4_CAP))
Or Gerlitz72cd5712017-01-24 13:02:36 +02005100 return ret;
Achiad Shochate53505a2015-12-23 18:47:25 +02005101
5102 if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV6_CAP))
Or Gerlitz72cd5712017-01-24 13:02:36 +02005103 return ret;
Achiad Shochate53505a2015-12-23 18:47:25 +02005104
5105 if (roce_version_cap & MLX5_ROCE_VERSION_1_CAP)
5106 ret |= RDMA_CORE_PORT_IBA_ROCE;
5107
5108 if (roce_version_cap & MLX5_ROCE_VERSION_2_CAP)
5109 ret |= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
5110
5111 return ret;
5112}
5113
Ira Weiny77386132015-05-13 20:02:58 -04005114static int mlx5_port_immutable(struct ib_device *ibdev, u8 port_num,
5115 struct ib_port_immutable *immutable)
5116{
5117 struct ib_port_attr attr;
Or Gerlitzca5b91d2016-11-27 16:51:36 +02005118 struct mlx5_ib_dev *dev = to_mdev(ibdev);
5119 enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, port_num);
Artemy Kovalyovb02289b2018-07-04 15:57:50 +03005120 struct mlx5_hca_vport_context rep = {0};
Ira Weiny77386132015-05-13 20:02:58 -04005121 int err;
5122
Or Gerlitzc4550c62017-01-24 13:02:39 +02005123 err = ib_query_port(ibdev, port_num, &attr);
Ira Weiny77386132015-05-13 20:02:58 -04005124 if (err)
5125 return err;
5126
Artemy Kovalyovb02289b2018-07-04 15:57:50 +03005127 if (ll == IB_LINK_LAYER_INFINIBAND) {
5128 err = mlx5_query_hca_vport_context(dev->mdev, 0, port_num, 0,
5129 &rep);
5130 if (err)
5131 return err;
5132 }
5133
Ira Weiny77386132015-05-13 20:02:58 -04005134 immutable->pkey_tbl_len = attr.pkey_tbl_len;
5135 immutable->gid_tbl_len = attr.gid_tbl_len;
Artemy Kovalyovb02289b2018-07-04 15:57:50 +03005136 immutable->core_cap_flags = get_core_cap_flags(ibdev, &rep);
Or Gerlitzca5b91d2016-11-27 16:51:36 +02005137 if ((ll == IB_LINK_LAYER_INFINIBAND) || MLX5_CAP_GEN(dev->mdev, roce))
5138 immutable->max_mad_size = IB_MGMT_MAD_SIZE;
Ira Weiny77386132015-05-13 20:02:58 -04005139
5140 return 0;
5141}
5142
Mark Bloch8e6efa32017-11-06 12:22:13 +00005143static int mlx5_port_rep_immutable(struct ib_device *ibdev, u8 port_num,
5144 struct ib_port_immutable *immutable)
5145{
5146 struct ib_port_attr attr;
5147 int err;
5148
5149 immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET;
5150
5151 err = ib_query_port(ibdev, port_num, &attr);
5152 if (err)
5153 return err;
5154
5155 immutable->pkey_tbl_len = attr.pkey_tbl_len;
5156 immutable->gid_tbl_len = attr.gid_tbl_len;
5157 immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET;
5158
5159 return 0;
5160}
5161
Leon Romanovsky9abb0d12017-06-27 16:49:53 +03005162static void get_dev_fw_str(struct ib_device *ibdev, char *str)
Ira Weinyc7342822016-06-15 02:22:01 -04005163{
5164 struct mlx5_ib_dev *dev =
5165 container_of(ibdev, struct mlx5_ib_dev, ib_dev);
Leon Romanovsky9abb0d12017-06-27 16:49:53 +03005166 snprintf(str, IB_FW_VERSION_NAME_MAX, "%d.%d.%04d",
5167 fw_rev_maj(dev->mdev), fw_rev_min(dev->mdev),
5168 fw_rev_sub(dev->mdev));
Ira Weinyc7342822016-06-15 02:22:01 -04005169}
5170
Or Gerlitz45f95ac2016-11-27 16:51:35 +02005171static int mlx5_eth_lag_init(struct mlx5_ib_dev *dev)
Aviv Heller9ef9c642016-09-18 20:48:01 +03005172{
5173 struct mlx5_core_dev *mdev = dev->mdev;
5174 struct mlx5_flow_namespace *ns = mlx5_get_flow_namespace(mdev,
5175 MLX5_FLOW_NAMESPACE_LAG);
5176 struct mlx5_flow_table *ft;
5177 int err;
5178
Aviv Heller7c34ec12018-08-23 13:47:53 +03005179 if (!ns || !mlx5_lag_is_roce(mdev))
Aviv Heller9ef9c642016-09-18 20:48:01 +03005180 return 0;
5181
5182 err = mlx5_cmd_create_vport_lag(mdev);
5183 if (err)
5184 return err;
5185
5186 ft = mlx5_create_lag_demux_flow_table(ns, 0, 0);
5187 if (IS_ERR(ft)) {
5188 err = PTR_ERR(ft);
5189 goto err_destroy_vport_lag;
5190 }
5191
Mark Bloch9a4ca382018-01-16 14:42:35 +00005192 dev->flow_db->lag_demux_ft = ft;
Aviv Heller7c34ec12018-08-23 13:47:53 +03005193 dev->lag_active = true;
Aviv Heller9ef9c642016-09-18 20:48:01 +03005194 return 0;
5195
5196err_destroy_vport_lag:
5197 mlx5_cmd_destroy_vport_lag(mdev);
5198 return err;
5199}
5200
Or Gerlitz45f95ac2016-11-27 16:51:35 +02005201static void mlx5_eth_lag_cleanup(struct mlx5_ib_dev *dev)
Aviv Heller9ef9c642016-09-18 20:48:01 +03005202{
5203 struct mlx5_core_dev *mdev = dev->mdev;
5204
Aviv Heller7c34ec12018-08-23 13:47:53 +03005205 if (dev->lag_active) {
5206 dev->lag_active = false;
5207
Mark Bloch9a4ca382018-01-16 14:42:35 +00005208 mlx5_destroy_flow_table(dev->flow_db->lag_demux_ft);
5209 dev->flow_db->lag_demux_ft = NULL;
Aviv Heller9ef9c642016-09-18 20:48:01 +03005210
5211 mlx5_cmd_destroy_vport_lag(mdev);
5212 }
5213}
5214
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +02005215static int mlx5_add_netdev_notifier(struct mlx5_ib_dev *dev, u8 port_num)
Achiad Shochatfc24fc52015-12-23 18:47:17 +02005216{
Achiad Shochate53505a2015-12-23 18:47:25 +02005217 int err;
5218
Mark Bloch95579e72019-03-28 15:27:33 +02005219 dev->port[port_num].roce.nb.notifier_call = mlx5_netdev_event;
5220 err = register_netdevice_notifier(&dev->port[port_num].roce.nb);
Aviv Heller5ec8c832016-09-18 20:48:00 +03005221 if (err) {
Mark Bloch95579e72019-03-28 15:27:33 +02005222 dev->port[port_num].roce.nb.notifier_call = NULL;
Achiad Shochate53505a2015-12-23 18:47:25 +02005223 return err;
Aviv Heller5ec8c832016-09-18 20:48:00 +03005224 }
Achiad Shochate53505a2015-12-23 18:47:25 +02005225
Or Gerlitzd012f5d2016-11-27 16:51:34 +02005226 return 0;
5227}
Achiad Shochate53505a2015-12-23 18:47:25 +02005228
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +02005229static void mlx5_remove_netdev_notifier(struct mlx5_ib_dev *dev, u8 port_num)
Eli Cohene126ba92013-07-07 17:25:49 +03005230{
Mark Bloch95579e72019-03-28 15:27:33 +02005231 if (dev->port[port_num].roce.nb.notifier_call) {
5232 unregister_netdevice_notifier(&dev->port[port_num].roce.nb);
5233 dev->port[port_num].roce.nb.notifier_call = NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03005234 }
5235}
5236
Leon Romanovskye3f1ed12018-07-08 12:55:43 +03005237static int mlx5_enable_eth(struct mlx5_ib_dev *dev)
Eli Cohene126ba92013-07-07 17:25:49 +03005238{
Eli Cohene126ba92013-07-07 17:25:49 +03005239 int err;
5240
Or Gerlitzca5b91d2016-11-27 16:51:36 +02005241 if (MLX5_CAP_GEN(dev->mdev, roce)) {
5242 err = mlx5_nic_vport_enable_roce(dev->mdev);
5243 if (err)
Mark Bloch8e6efa32017-11-06 12:22:13 +00005244 return err;
Or Gerlitzca5b91d2016-11-27 16:51:36 +02005245 }
Achiad Shochate53505a2015-12-23 18:47:25 +02005246
Or Gerlitz45f95ac2016-11-27 16:51:35 +02005247 err = mlx5_eth_lag_init(dev);
Aviv Heller9ef9c642016-09-18 20:48:01 +03005248 if (err)
5249 goto err_disable_roce;
5250
Achiad Shochate53505a2015-12-23 18:47:25 +02005251 return 0;
5252
Aviv Heller9ef9c642016-09-18 20:48:01 +03005253err_disable_roce:
Or Gerlitzca5b91d2016-11-27 16:51:36 +02005254 if (MLX5_CAP_GEN(dev->mdev, roce))
5255 mlx5_nic_vport_disable_roce(dev->mdev);
Aviv Heller9ef9c642016-09-18 20:48:01 +03005256
Achiad Shochate53505a2015-12-23 18:47:25 +02005257 return err;
Achiad Shochatfc24fc52015-12-23 18:47:17 +02005258}
5259
Or Gerlitz45f95ac2016-11-27 16:51:35 +02005260static void mlx5_disable_eth(struct mlx5_ib_dev *dev)
Achiad Shochatfc24fc52015-12-23 18:47:17 +02005261{
Or Gerlitz45f95ac2016-11-27 16:51:35 +02005262 mlx5_eth_lag_cleanup(dev);
Or Gerlitzca5b91d2016-11-27 16:51:36 +02005263 if (MLX5_CAP_GEN(dev->mdev, roce))
5264 mlx5_nic_vport_disable_roce(dev->mdev);
Achiad Shochatfc24fc52015-12-23 18:47:17 +02005265}
5266
Parav Pandite1f24a72017-04-16 07:29:29 +03005267struct mlx5_ib_counter {
Kamal Heib7c16f472017-01-18 15:25:09 +02005268 const char *name;
5269 size_t offset;
5270};
5271
5272#define INIT_Q_COUNTER(_name) \
5273 { .name = #_name, .offset = MLX5_BYTE_OFF(query_q_counter_out, _name)}
5274
Parav Pandite1f24a72017-04-16 07:29:29 +03005275static const struct mlx5_ib_counter basic_q_cnts[] = {
Kamal Heib7c16f472017-01-18 15:25:09 +02005276 INIT_Q_COUNTER(rx_write_requests),
5277 INIT_Q_COUNTER(rx_read_requests),
5278 INIT_Q_COUNTER(rx_atomic_requests),
5279 INIT_Q_COUNTER(out_of_buffer),
5280};
5281
Parav Pandite1f24a72017-04-16 07:29:29 +03005282static const struct mlx5_ib_counter out_of_seq_q_cnts[] = {
Kamal Heib7c16f472017-01-18 15:25:09 +02005283 INIT_Q_COUNTER(out_of_sequence),
5284};
5285
Parav Pandite1f24a72017-04-16 07:29:29 +03005286static const struct mlx5_ib_counter retrans_q_cnts[] = {
Kamal Heib7c16f472017-01-18 15:25:09 +02005287 INIT_Q_COUNTER(duplicate_request),
5288 INIT_Q_COUNTER(rnr_nak_retry_err),
5289 INIT_Q_COUNTER(packet_seq_err),
5290 INIT_Q_COUNTER(implied_nak_seq_err),
5291 INIT_Q_COUNTER(local_ack_timeout_err),
5292};
5293
Parav Pandite1f24a72017-04-16 07:29:29 +03005294#define INIT_CONG_COUNTER(_name) \
5295 { .name = #_name, .offset = \
5296 MLX5_BYTE_OFF(query_cong_statistics_out, _name ## _high)}
5297
5298static const struct mlx5_ib_counter cong_cnts[] = {
5299 INIT_CONG_COUNTER(rp_cnp_ignored),
5300 INIT_CONG_COUNTER(rp_cnp_handled),
5301 INIT_CONG_COUNTER(np_ecn_marked_roce_packets),
5302 INIT_CONG_COUNTER(np_cnp_sent),
5303};
5304
Parav Pandit58dcb602017-06-19 07:19:37 +03005305static const struct mlx5_ib_counter extended_err_cnts[] = {
5306 INIT_Q_COUNTER(resp_local_length_error),
5307 INIT_Q_COUNTER(resp_cqe_error),
5308 INIT_Q_COUNTER(req_cqe_error),
5309 INIT_Q_COUNTER(req_remote_invalid_request),
5310 INIT_Q_COUNTER(req_remote_access_errors),
5311 INIT_Q_COUNTER(resp_remote_access_errors),
5312 INIT_Q_COUNTER(resp_cqe_flush_error),
5313 INIT_Q_COUNTER(req_cqe_flush_error),
5314};
5315
Talat Batheesh9f876f32018-06-21 15:37:56 +03005316#define INIT_EXT_PPCNT_COUNTER(_name) \
5317 { .name = #_name, .offset = \
5318 MLX5_BYTE_OFF(ppcnt_reg, \
5319 counter_set.eth_extended_cntrs_grp_data_layout._name##_high)}
5320
5321static const struct mlx5_ib_counter ext_ppcnt_cnts[] = {
5322 INIT_EXT_PPCNT_COUNTER(rx_icrc_encapsulated),
5323};
5324
Parav Pandite1f24a72017-04-16 07:29:29 +03005325static void mlx5_ib_dealloc_counters(struct mlx5_ib_dev *dev)
Mark Bloch0837e862016-06-17 15:10:55 +03005326{
Daniel Jurgensaac44922018-01-04 17:25:40 +02005327 int i;
Mark Bloch0837e862016-06-17 15:10:55 +03005328
Kamal Heib7c16f472017-01-18 15:25:09 +02005329 for (i = 0; i < dev->num_ports; i++) {
Parav Pandit921c0f52018-07-08 13:40:30 +03005330 if (dev->port[i].cnts.set_id_valid)
Daniel Jurgensaac44922018-01-04 17:25:40 +02005331 mlx5_core_dealloc_q_counter(dev->mdev,
5332 dev->port[i].cnts.set_id);
Parav Pandite1f24a72017-04-16 07:29:29 +03005333 kfree(dev->port[i].cnts.names);
5334 kfree(dev->port[i].cnts.offsets);
Kamal Heib7c16f472017-01-18 15:25:09 +02005335 }
5336}
5337
Parav Pandite1f24a72017-04-16 07:29:29 +03005338static int __mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev,
5339 struct mlx5_ib_counters *cnts)
Kamal Heib7c16f472017-01-18 15:25:09 +02005340{
5341 u32 num_counters;
5342
5343 num_counters = ARRAY_SIZE(basic_q_cnts);
5344
5345 if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt))
5346 num_counters += ARRAY_SIZE(out_of_seq_q_cnts);
5347
5348 if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters))
5349 num_counters += ARRAY_SIZE(retrans_q_cnts);
Parav Pandit58dcb602017-06-19 07:19:37 +03005350
5351 if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters))
5352 num_counters += ARRAY_SIZE(extended_err_cnts);
5353
Parav Pandite1f24a72017-04-16 07:29:29 +03005354 cnts->num_q_counters = num_counters;
Kamal Heib7c16f472017-01-18 15:25:09 +02005355
Parav Pandite1f24a72017-04-16 07:29:29 +03005356 if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
5357 cnts->num_cong_counters = ARRAY_SIZE(cong_cnts);
5358 num_counters += ARRAY_SIZE(cong_cnts);
5359 }
Talat Batheesh9f876f32018-06-21 15:37:56 +03005360 if (MLX5_CAP_PCAM_FEATURE(dev->mdev, rx_icrc_encapsulated_counter)) {
5361 cnts->num_ext_ppcnt_counters = ARRAY_SIZE(ext_ppcnt_cnts);
5362 num_counters += ARRAY_SIZE(ext_ppcnt_cnts);
5363 }
Parav Pandite1f24a72017-04-16 07:29:29 +03005364 cnts->names = kcalloc(num_counters, sizeof(cnts->names), GFP_KERNEL);
5365 if (!cnts->names)
Kamal Heib7c16f472017-01-18 15:25:09 +02005366 return -ENOMEM;
5367
Parav Pandite1f24a72017-04-16 07:29:29 +03005368 cnts->offsets = kcalloc(num_counters,
5369 sizeof(cnts->offsets), GFP_KERNEL);
5370 if (!cnts->offsets)
Kamal Heib7c16f472017-01-18 15:25:09 +02005371 goto err_names;
5372
Kamal Heib7c16f472017-01-18 15:25:09 +02005373 return 0;
5374
5375err_names:
Parav Pandite1f24a72017-04-16 07:29:29 +03005376 kfree(cnts->names);
Daniel Jurgensaac44922018-01-04 17:25:40 +02005377 cnts->names = NULL;
Kamal Heib7c16f472017-01-18 15:25:09 +02005378 return -ENOMEM;
5379}
5380
Parav Pandite1f24a72017-04-16 07:29:29 +03005381static void mlx5_ib_fill_counters(struct mlx5_ib_dev *dev,
5382 const char **names,
5383 size_t *offsets)
Kamal Heib7c16f472017-01-18 15:25:09 +02005384{
5385 int i;
5386 int j = 0;
5387
5388 for (i = 0; i < ARRAY_SIZE(basic_q_cnts); i++, j++) {
5389 names[j] = basic_q_cnts[i].name;
5390 offsets[j] = basic_q_cnts[i].offset;
5391 }
5392
5393 if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt)) {
5394 for (i = 0; i < ARRAY_SIZE(out_of_seq_q_cnts); i++, j++) {
5395 names[j] = out_of_seq_q_cnts[i].name;
5396 offsets[j] = out_of_seq_q_cnts[i].offset;
5397 }
5398 }
5399
5400 if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters)) {
5401 for (i = 0; i < ARRAY_SIZE(retrans_q_cnts); i++, j++) {
5402 names[j] = retrans_q_cnts[i].name;
5403 offsets[j] = retrans_q_cnts[i].offset;
5404 }
5405 }
Parav Pandite1f24a72017-04-16 07:29:29 +03005406
Parav Pandit58dcb602017-06-19 07:19:37 +03005407 if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters)) {
5408 for (i = 0; i < ARRAY_SIZE(extended_err_cnts); i++, j++) {
5409 names[j] = extended_err_cnts[i].name;
5410 offsets[j] = extended_err_cnts[i].offset;
5411 }
5412 }
5413
Parav Pandite1f24a72017-04-16 07:29:29 +03005414 if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
5415 for (i = 0; i < ARRAY_SIZE(cong_cnts); i++, j++) {
5416 names[j] = cong_cnts[i].name;
5417 offsets[j] = cong_cnts[i].offset;
5418 }
5419 }
Talat Batheesh9f876f32018-06-21 15:37:56 +03005420
5421 if (MLX5_CAP_PCAM_FEATURE(dev->mdev, rx_icrc_encapsulated_counter)) {
5422 for (i = 0; i < ARRAY_SIZE(ext_ppcnt_cnts); i++, j++) {
5423 names[j] = ext_ppcnt_cnts[i].name;
5424 offsets[j] = ext_ppcnt_cnts[i].offset;
5425 }
5426 }
Mark Bloch0837e862016-06-17 15:10:55 +03005427}
5428
Parav Pandite1f24a72017-04-16 07:29:29 +03005429static int mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev)
Mark Bloch0837e862016-06-17 15:10:55 +03005430{
Daniel Jurgensaac44922018-01-04 17:25:40 +02005431 int err = 0;
Mark Bloch0837e862016-06-17 15:10:55 +03005432 int i;
Yishai Hadasaa74be62018-12-09 12:52:36 +02005433 bool is_shared;
5434
5435 is_shared = MLX5_CAP_GEN(dev->mdev, log_max_uctx) != 0;
Mark Bloch0837e862016-06-17 15:10:55 +03005436
5437 for (i = 0; i < dev->num_ports; i++) {
Daniel Jurgensaac44922018-01-04 17:25:40 +02005438 err = __mlx5_ib_alloc_counters(dev, &dev->port[i].cnts);
5439 if (err)
5440 goto err_alloc;
Kamal Heib7c16f472017-01-18 15:25:09 +02005441
Daniel Jurgensaac44922018-01-04 17:25:40 +02005442 mlx5_ib_fill_counters(dev, dev->port[i].cnts.names,
5443 dev->port[i].cnts.offsets);
5444
Yishai Hadasaa74be62018-12-09 12:52:36 +02005445 err = mlx5_cmd_alloc_q_counter(dev->mdev,
5446 &dev->port[i].cnts.set_id,
5447 is_shared ?
5448 MLX5_SHARED_RESOURCE_UID : 0);
Daniel Jurgensaac44922018-01-04 17:25:40 +02005449 if (err) {
Mark Bloch0837e862016-06-17 15:10:55 +03005450 mlx5_ib_warn(dev,
5451 "couldn't allocate queue counter for port %d, err %d\n",
Daniel Jurgensaac44922018-01-04 17:25:40 +02005452 i + 1, err);
5453 goto err_alloc;
Mark Bloch0837e862016-06-17 15:10:55 +03005454 }
Daniel Jurgensaac44922018-01-04 17:25:40 +02005455 dev->port[i].cnts.set_id_valid = true;
Mark Bloch0837e862016-06-17 15:10:55 +03005456 }
5457
5458 return 0;
5459
Daniel Jurgensaac44922018-01-04 17:25:40 +02005460err_alloc:
5461 mlx5_ib_dealloc_counters(dev);
5462 return err;
Mark Bloch0837e862016-06-17 15:10:55 +03005463}
5464
Mark Bloch0ad17a82016-06-17 15:10:56 +03005465static struct rdma_hw_stats *mlx5_ib_alloc_hw_stats(struct ib_device *ibdev,
5466 u8 port_num)
5467{
Kamal Heib7c16f472017-01-18 15:25:09 +02005468 struct mlx5_ib_dev *dev = to_mdev(ibdev);
5469 struct mlx5_ib_port *port = &dev->port[port_num - 1];
Mark Bloch0ad17a82016-06-17 15:10:56 +03005470
5471 /* We support only per port stats */
5472 if (port_num == 0)
5473 return NULL;
5474
Parav Pandite1f24a72017-04-16 07:29:29 +03005475 return rdma_alloc_hw_stats_struct(port->cnts.names,
5476 port->cnts.num_q_counters +
Talat Batheesh9f876f32018-06-21 15:37:56 +03005477 port->cnts.num_cong_counters +
5478 port->cnts.num_ext_ppcnt_counters,
Mark Bloch0ad17a82016-06-17 15:10:56 +03005479 RDMA_HW_STATS_DEFAULT_LIFESPAN);
5480}
5481
Daniel Jurgensaac44922018-01-04 17:25:40 +02005482static int mlx5_ib_query_q_counters(struct mlx5_core_dev *mdev,
Parav Pandite1f24a72017-04-16 07:29:29 +03005483 struct mlx5_ib_port *port,
Mark Zhang318d5352019-07-02 13:02:37 +03005484 struct rdma_hw_stats *stats,
5485 u16 set_id)
Parav Pandite1f24a72017-04-16 07:29:29 +03005486{
5487 int outlen = MLX5_ST_SZ_BYTES(query_q_counter_out);
5488 void *out;
5489 __be32 val;
5490 int ret, i;
5491
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03005492 out = kvzalloc(outlen, GFP_KERNEL);
Parav Pandite1f24a72017-04-16 07:29:29 +03005493 if (!out)
5494 return -ENOMEM;
5495
Mark Zhang318d5352019-07-02 13:02:37 +03005496 ret = mlx5_core_query_q_counter(mdev, set_id, 0, out, outlen);
Parav Pandite1f24a72017-04-16 07:29:29 +03005497 if (ret)
5498 goto free;
5499
5500 for (i = 0; i < port->cnts.num_q_counters; i++) {
5501 val = *(__be32 *)(out + port->cnts.offsets[i]);
5502 stats->value[i] = (u64)be32_to_cpu(val);
5503 }
5504
5505free:
5506 kvfree(out);
5507 return ret;
5508}
5509
Talat Batheesh9f876f32018-06-21 15:37:56 +03005510static int mlx5_ib_query_ext_ppcnt_counters(struct mlx5_ib_dev *dev,
5511 struct mlx5_ib_port *port,
5512 struct rdma_hw_stats *stats)
5513{
5514 int offset = port->cnts.num_q_counters + port->cnts.num_cong_counters;
5515 int sz = MLX5_ST_SZ_BYTES(ppcnt_reg);
5516 int ret, i;
5517 void *out;
5518
5519 out = kvzalloc(sz, GFP_KERNEL);
5520 if (!out)
5521 return -ENOMEM;
5522
5523 ret = mlx5_cmd_query_ext_ppcnt_counters(dev->mdev, out);
5524 if (ret)
5525 goto free;
5526
5527 for (i = 0; i < port->cnts.num_ext_ppcnt_counters; i++) {
5528 stats->value[i + offset] =
5529 be64_to_cpup((__be64 *)(out +
5530 port->cnts.offsets[i + offset]));
5531 }
5532
5533free:
5534 kvfree(out);
5535 return ret;
5536}
5537
Mark Bloch0ad17a82016-06-17 15:10:56 +03005538static int mlx5_ib_get_hw_stats(struct ib_device *ibdev,
5539 struct rdma_hw_stats *stats,
Kamal Heib7c16f472017-01-18 15:25:09 +02005540 u8 port_num, int index)
Mark Bloch0ad17a82016-06-17 15:10:56 +03005541{
5542 struct mlx5_ib_dev *dev = to_mdev(ibdev);
Kamal Heib7c16f472017-01-18 15:25:09 +02005543 struct mlx5_ib_port *port = &dev->port[port_num - 1];
Daniel Jurgensaac44922018-01-04 17:25:40 +02005544 struct mlx5_core_dev *mdev;
Parav Pandite1f24a72017-04-16 07:29:29 +03005545 int ret, num_counters;
Daniel Jurgensaac44922018-01-04 17:25:40 +02005546 u8 mdev_port_num;
Mark Bloch0ad17a82016-06-17 15:10:56 +03005547
Kamal Heib7c16f472017-01-18 15:25:09 +02005548 if (!stats)
Parav Pandite1f24a72017-04-16 07:29:29 +03005549 return -EINVAL;
Mark Bloch0ad17a82016-06-17 15:10:56 +03005550
Talat Batheesh9f876f32018-06-21 15:37:56 +03005551 num_counters = port->cnts.num_q_counters +
5552 port->cnts.num_cong_counters +
5553 port->cnts.num_ext_ppcnt_counters;
Daniel Jurgensaac44922018-01-04 17:25:40 +02005554
5555 /* q_counters are per IB device, query the master mdev */
Mark Zhang318d5352019-07-02 13:02:37 +03005556 ret = mlx5_ib_query_q_counters(dev->mdev, port, stats,
5557 port->cnts.set_id);
Mark Bloch0ad17a82016-06-17 15:10:56 +03005558 if (ret)
Parav Pandite1f24a72017-04-16 07:29:29 +03005559 return ret;
Mark Bloch0ad17a82016-06-17 15:10:56 +03005560
Talat Batheesh9f876f32018-06-21 15:37:56 +03005561 if (MLX5_CAP_PCAM_FEATURE(dev->mdev, rx_icrc_encapsulated_counter)) {
5562 ret = mlx5_ib_query_ext_ppcnt_counters(dev, port, stats);
5563 if (ret)
5564 return ret;
5565 }
5566
Parav Pandite1f24a72017-04-16 07:29:29 +03005567 if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
Daniel Jurgensaac44922018-01-04 17:25:40 +02005568 mdev = mlx5_ib_get_native_port_mdev(dev, port_num,
5569 &mdev_port_num);
5570 if (!mdev) {
5571 /* If port is not affiliated yet, its in down state
5572 * which doesn't have any counters yet, so it would be
5573 * zero. So no need to read from the HCA.
5574 */
5575 goto done;
5576 }
Majd Dibbiny71a0ff62017-12-21 17:38:26 +02005577 ret = mlx5_lag_query_cong_counters(dev->mdev,
5578 stats->value +
5579 port->cnts.num_q_counters,
5580 port->cnts.num_cong_counters,
5581 port->cnts.offsets +
5582 port->cnts.num_q_counters);
Daniel Jurgensaac44922018-01-04 17:25:40 +02005583
5584 mlx5_ib_put_native_port_mdev(dev, port_num);
Parav Pandite1f24a72017-04-16 07:29:29 +03005585 if (ret)
5586 return ret;
Mark Bloch0ad17a82016-06-17 15:10:56 +03005587 }
Kamal Heib7c16f472017-01-18 15:25:09 +02005588
Daniel Jurgensaac44922018-01-04 17:25:40 +02005589done:
Parav Pandite1f24a72017-04-16 07:29:29 +03005590 return num_counters;
Mark Bloch0ad17a82016-06-17 15:10:56 +03005591}
5592
Mark Zhang18d422c2019-07-02 13:02:41 +03005593static struct rdma_hw_stats *
5594mlx5_ib_counter_alloc_stats(struct rdma_counter *counter)
5595{
5596 struct mlx5_ib_dev *dev = to_mdev(counter->device);
5597 struct mlx5_ib_port *port = &dev->port[counter->port - 1];
5598
5599 /* Q counters are in the beginning of all counters */
5600 return rdma_alloc_hw_stats_struct(port->cnts.names,
5601 port->cnts.num_q_counters,
5602 RDMA_HW_STATS_DEFAULT_LIFESPAN);
5603}
5604
5605static int mlx5_ib_counter_update_stats(struct rdma_counter *counter)
5606{
5607 struct mlx5_ib_dev *dev = to_mdev(counter->device);
5608 struct mlx5_ib_port *port = &dev->port[counter->port - 1];
5609
5610 return mlx5_ib_query_q_counters(dev->mdev, port,
5611 counter->stats, counter->id);
5612}
5613
Mark Zhang45842fc2019-07-02 13:02:38 +03005614static int mlx5_ib_counter_bind_qp(struct rdma_counter *counter,
5615 struct ib_qp *qp)
5616{
5617 struct mlx5_ib_dev *dev = to_mdev(qp->device);
5618 u16 cnt_set_id = 0;
5619 int err;
5620
5621 if (!counter->id) {
5622 err = mlx5_cmd_alloc_q_counter(dev->mdev,
5623 &cnt_set_id,
5624 MLX5_SHARED_RESOURCE_UID);
5625 if (err)
5626 return err;
5627 counter->id = cnt_set_id;
5628 }
5629
5630 err = mlx5_ib_qp_set_counter(qp, counter);
5631 if (err)
5632 goto fail_set_counter;
5633
5634 return 0;
5635
5636fail_set_counter:
5637 mlx5_core_dealloc_q_counter(dev->mdev, cnt_set_id);
5638 counter->id = 0;
5639
5640 return err;
5641}
5642
5643static int mlx5_ib_counter_unbind_qp(struct ib_qp *qp)
5644{
5645 return mlx5_ib_qp_set_counter(qp, NULL);
5646}
5647
5648static int mlx5_ib_counter_dealloc(struct rdma_counter *counter)
5649{
5650 struct mlx5_ib_dev *dev = to_mdev(counter->device);
5651
5652 return mlx5_core_dealloc_q_counter(dev->mdev, counter->id);
5653}
5654
Denis Drozdovf6a8a192018-08-14 14:08:51 +03005655static int mlx5_ib_rn_get_params(struct ib_device *device, u8 port_num,
5656 enum rdma_netdev_t type,
5657 struct rdma_netdev_alloc_params *params)
Erez Shitrit693dfd52017-04-27 17:01:34 +03005658{
5659 if (type != RDMA_NETDEV_IPOIB)
Denis Drozdovf6a8a192018-08-14 14:08:51 +03005660 return -EOPNOTSUPP;
Erez Shitrit693dfd52017-04-27 17:01:34 +03005661
Denis Drozdovf6a8a192018-08-14 14:08:51 +03005662 return mlx5_rdma_rn_get_params(to_mdev(device)->mdev, device, params);
Erez Shitrit693dfd52017-04-27 17:01:34 +03005663}
5664
Maor Gottliebfe248c32017-05-30 10:29:14 +03005665static void delay_drop_debugfs_cleanup(struct mlx5_ib_dev *dev)
5666{
5667 if (!dev->delay_drop.dbg)
5668 return;
5669 debugfs_remove_recursive(dev->delay_drop.dbg->dir_debugfs);
5670 kfree(dev->delay_drop.dbg);
5671 dev->delay_drop.dbg = NULL;
5672}
5673
Maor Gottlieb03404e82017-05-30 10:29:13 +03005674static void cancel_delay_drop(struct mlx5_ib_dev *dev)
5675{
5676 if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
5677 return;
5678
5679 cancel_work_sync(&dev->delay_drop.delay_drop_work);
Maor Gottliebfe248c32017-05-30 10:29:14 +03005680 delay_drop_debugfs_cleanup(dev);
5681}
5682
5683static ssize_t delay_drop_timeout_read(struct file *filp, char __user *buf,
5684 size_t count, loff_t *pos)
5685{
5686 struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
5687 char lbuf[20];
5688 int len;
5689
5690 len = snprintf(lbuf, sizeof(lbuf), "%u\n", delay_drop->timeout);
5691 return simple_read_from_buffer(buf, count, pos, lbuf, len);
5692}
5693
5694static ssize_t delay_drop_timeout_write(struct file *filp, const char __user *buf,
5695 size_t count, loff_t *pos)
5696{
5697 struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
5698 u32 timeout;
5699 u32 var;
5700
5701 if (kstrtouint_from_user(buf, count, 0, &var))
5702 return -EFAULT;
5703
5704 timeout = min_t(u32, roundup(var, 100), MLX5_MAX_DELAY_DROP_TIMEOUT_MS *
5705 1000);
5706 if (timeout != var)
5707 mlx5_ib_dbg(delay_drop->dev, "Round delay drop timeout to %u usec\n",
5708 timeout);
5709
5710 delay_drop->timeout = timeout;
5711
5712 return count;
5713}
5714
5715static const struct file_operations fops_delay_drop_timeout = {
5716 .owner = THIS_MODULE,
5717 .open = simple_open,
5718 .write = delay_drop_timeout_write,
5719 .read = delay_drop_timeout_read,
5720};
5721
5722static int delay_drop_debugfs_init(struct mlx5_ib_dev *dev)
5723{
5724 struct mlx5_ib_dbg_delay_drop *dbg;
5725
5726 if (!mlx5_debugfs_root)
5727 return 0;
5728
5729 dbg = kzalloc(sizeof(*dbg), GFP_KERNEL);
5730 if (!dbg)
5731 return -ENOMEM;
5732
Sudip Mukherjeecbafad82017-09-18 12:28:48 +01005733 dev->delay_drop.dbg = dbg;
5734
Maor Gottliebfe248c32017-05-30 10:29:14 +03005735 dbg->dir_debugfs =
5736 debugfs_create_dir("delay_drop",
5737 dev->mdev->priv.dbg_root);
5738 if (!dbg->dir_debugfs)
Sudip Mukherjeecbafad82017-09-18 12:28:48 +01005739 goto out_debugfs;
Maor Gottliebfe248c32017-05-30 10:29:14 +03005740
5741 dbg->events_cnt_debugfs =
5742 debugfs_create_atomic_t("num_timeout_events", 0400,
5743 dbg->dir_debugfs,
5744 &dev->delay_drop.events_cnt);
5745 if (!dbg->events_cnt_debugfs)
5746 goto out_debugfs;
5747
5748 dbg->rqs_cnt_debugfs =
5749 debugfs_create_atomic_t("num_rqs", 0400,
5750 dbg->dir_debugfs,
5751 &dev->delay_drop.rqs_cnt);
5752 if (!dbg->rqs_cnt_debugfs)
5753 goto out_debugfs;
5754
5755 dbg->timeout_debugfs =
5756 debugfs_create_file("timeout", 0600,
5757 dbg->dir_debugfs,
5758 &dev->delay_drop,
5759 &fops_delay_drop_timeout);
5760 if (!dbg->timeout_debugfs)
5761 goto out_debugfs;
5762
5763 return 0;
5764
5765out_debugfs:
5766 delay_drop_debugfs_cleanup(dev);
5767 return -ENOMEM;
Maor Gottlieb03404e82017-05-30 10:29:13 +03005768}
5769
5770static void init_delay_drop(struct mlx5_ib_dev *dev)
5771{
5772 if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
5773 return;
5774
5775 mutex_init(&dev->delay_drop.lock);
5776 dev->delay_drop.dev = dev;
5777 dev->delay_drop.activate = false;
5778 dev->delay_drop.timeout = MLX5_MAX_DELAY_DROP_TIMEOUT_MS * 1000;
5779 INIT_WORK(&dev->delay_drop.delay_drop_work, delay_drop_handler);
Maor Gottliebfe248c32017-05-30 10:29:14 +03005780 atomic_set(&dev->delay_drop.rqs_cnt, 0);
5781 atomic_set(&dev->delay_drop.events_cnt, 0);
5782
5783 if (delay_drop_debugfs_init(dev))
5784 mlx5_ib_warn(dev, "Failed to init delay drop debugfs\n");
Maor Gottlieb03404e82017-05-30 10:29:13 +03005785}
5786
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005787/* The mlx5_ib_multiport_mutex should be held when calling this function */
5788static void mlx5_ib_unbind_slave_port(struct mlx5_ib_dev *ibdev,
5789 struct mlx5_ib_multiport_info *mpi)
5790{
5791 u8 port_num = mlx5_core_native_port_num(mpi->mdev) - 1;
5792 struct mlx5_ib_port *port = &ibdev->port[port_num];
5793 int comps;
5794 int err;
5795 int i;
5796
Parav Pandita9e546e2018-01-04 17:25:39 +02005797 mlx5_ib_cleanup_cong_debugfs(ibdev, port_num);
5798
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005799 spin_lock(&port->mp.mpi_lock);
5800 if (!mpi->ibdev) {
5801 spin_unlock(&port->mp.mpi_lock);
5802 return;
5803 }
Saeed Mahameeddf097a22018-11-26 14:39:00 -08005804
5805 if (mpi->mdev_events.notifier_call)
5806 mlx5_notifier_unregister(mpi->mdev, &mpi->mdev_events);
5807 mpi->mdev_events.notifier_call = NULL;
5808
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005809 mpi->ibdev = NULL;
5810
5811 spin_unlock(&port->mp.mpi_lock);
5812 mlx5_remove_netdev_notifier(ibdev, port_num);
5813 spin_lock(&port->mp.mpi_lock);
5814
5815 comps = mpi->mdev_refcnt;
5816 if (comps) {
5817 mpi->unaffiliate = true;
5818 init_completion(&mpi->unref_comp);
5819 spin_unlock(&port->mp.mpi_lock);
5820
5821 for (i = 0; i < comps; i++)
5822 wait_for_completion(&mpi->unref_comp);
5823
5824 spin_lock(&port->mp.mpi_lock);
5825 mpi->unaffiliate = false;
5826 }
5827
5828 port->mp.mpi = NULL;
5829
5830 list_add_tail(&mpi->list, &mlx5_ib_unaffiliated_port_list);
5831
5832 spin_unlock(&port->mp.mpi_lock);
5833
5834 err = mlx5_nic_vport_unaffiliate_multiport(mpi->mdev);
5835
5836 mlx5_ib_dbg(ibdev, "unaffiliated port %d\n", port_num + 1);
5837 /* Log an error, still needed to cleanup the pointers and add
5838 * it back to the list.
5839 */
5840 if (err)
5841 mlx5_ib_err(ibdev, "Failed to unaffiliate port %u\n",
5842 port_num + 1);
5843
Mark Bloch95579e72019-03-28 15:27:33 +02005844 ibdev->port[port_num].roce.last_port_state = IB_PORT_DOWN;
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005845}
5846
5847/* The mlx5_ib_multiport_mutex should be held when calling this function */
5848static bool mlx5_ib_bind_slave_port(struct mlx5_ib_dev *ibdev,
5849 struct mlx5_ib_multiport_info *mpi)
5850{
5851 u8 port_num = mlx5_core_native_port_num(mpi->mdev) - 1;
5852 int err;
5853
5854 spin_lock(&ibdev->port[port_num].mp.mpi_lock);
5855 if (ibdev->port[port_num].mp.mpi) {
Qing Huang25771882018-07-23 14:15:08 -07005856 mlx5_ib_dbg(ibdev, "port %d already affiliated.\n",
5857 port_num + 1);
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005858 spin_unlock(&ibdev->port[port_num].mp.mpi_lock);
5859 return false;
5860 }
5861
5862 ibdev->port[port_num].mp.mpi = mpi;
5863 mpi->ibdev = ibdev;
Saeed Mahameeddf097a22018-11-26 14:39:00 -08005864 mpi->mdev_events.notifier_call = NULL;
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005865 spin_unlock(&ibdev->port[port_num].mp.mpi_lock);
5866
5867 err = mlx5_nic_vport_affiliate_multiport(ibdev->mdev, mpi->mdev);
5868 if (err)
5869 goto unbind;
5870
5871 err = get_port_caps(ibdev, mlx5_core_native_port_num(mpi->mdev));
5872 if (err)
5873 goto unbind;
5874
5875 err = mlx5_add_netdev_notifier(ibdev, port_num);
5876 if (err) {
5877 mlx5_ib_err(ibdev, "failed adding netdev notifier for port %u\n",
5878 port_num + 1);
5879 goto unbind;
5880 }
5881
Saeed Mahameeddf097a22018-11-26 14:39:00 -08005882 mpi->mdev_events.notifier_call = mlx5_ib_event_slave_port;
5883 mlx5_notifier_register(mpi->mdev, &mpi->mdev_events);
5884
Greg Kroah-Hartman73eb8f02019-01-22 16:17:57 +01005885 mlx5_ib_init_cong_debugfs(ibdev, port_num);
Parav Pandita9e546e2018-01-04 17:25:39 +02005886
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005887 return true;
5888
5889unbind:
5890 mlx5_ib_unbind_slave_port(ibdev, mpi);
5891 return false;
5892}
5893
5894static int mlx5_ib_init_multiport_master(struct mlx5_ib_dev *dev)
5895{
5896 int port_num = mlx5_core_native_port_num(dev->mdev) - 1;
5897 enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev,
5898 port_num + 1);
5899 struct mlx5_ib_multiport_info *mpi;
5900 int err;
5901 int i;
5902
5903 if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
5904 return 0;
5905
5906 err = mlx5_query_nic_vport_system_image_guid(dev->mdev,
5907 &dev->sys_image_guid);
5908 if (err)
5909 return err;
5910
5911 err = mlx5_nic_vport_enable_roce(dev->mdev);
5912 if (err)
5913 return err;
5914
5915 mutex_lock(&mlx5_ib_multiport_mutex);
5916 for (i = 0; i < dev->num_ports; i++) {
5917 bool bound = false;
5918
5919 /* build a stub multiport info struct for the native port. */
5920 if (i == port_num) {
5921 mpi = kzalloc(sizeof(*mpi), GFP_KERNEL);
5922 if (!mpi) {
5923 mutex_unlock(&mlx5_ib_multiport_mutex);
5924 mlx5_nic_vport_disable_roce(dev->mdev);
5925 return -ENOMEM;
5926 }
5927
5928 mpi->is_master = true;
5929 mpi->mdev = dev->mdev;
5930 mpi->sys_image_guid = dev->sys_image_guid;
5931 dev->port[i].mp.mpi = mpi;
5932 mpi->ibdev = dev;
5933 mpi = NULL;
5934 continue;
5935 }
5936
5937 list_for_each_entry(mpi, &mlx5_ib_unaffiliated_port_list,
5938 list) {
5939 if (dev->sys_image_guid == mpi->sys_image_guid &&
5940 (mlx5_core_native_port_num(mpi->mdev) - 1) == i) {
5941 bound = mlx5_ib_bind_slave_port(dev, mpi);
5942 }
5943
5944 if (bound) {
Vu Phamc42260f12019-04-29 18:14:05 +00005945 dev_dbg(mpi->mdev->device,
5946 "removing port from unaffiliated list.\n");
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005947 mlx5_ib_dbg(dev, "port %d bound\n", i + 1);
5948 list_del(&mpi->list);
5949 break;
5950 }
5951 }
5952 if (!bound) {
5953 get_port_caps(dev, i + 1);
5954 mlx5_ib_dbg(dev, "no free port found for port %d\n",
5955 i + 1);
5956 }
5957 }
5958
5959 list_add_tail(&dev->ib_dev_list, &mlx5_ib_dev_list);
5960 mutex_unlock(&mlx5_ib_multiport_mutex);
5961 return err;
5962}
5963
5964static void mlx5_ib_cleanup_multiport_master(struct mlx5_ib_dev *dev)
5965{
5966 int port_num = mlx5_core_native_port_num(dev->mdev) - 1;
5967 enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev,
5968 port_num + 1);
5969 int i;
5970
5971 if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
5972 return;
5973
5974 mutex_lock(&mlx5_ib_multiport_mutex);
5975 for (i = 0; i < dev->num_ports; i++) {
5976 if (dev->port[i].mp.mpi) {
5977 /* Destroy the native port stub */
5978 if (i == port_num) {
5979 kfree(dev->port[i].mp.mpi);
5980 dev->port[i].mp.mpi = NULL;
5981 } else {
5982 mlx5_ib_dbg(dev, "unbinding port_num: %d\n", i + 1);
5983 mlx5_ib_unbind_slave_port(dev, dev->port[i].mp.mpi);
5984 }
5985 }
5986 }
5987
5988 mlx5_ib_dbg(dev, "removing from devlist\n");
5989 list_del(&dev->ib_dev_list);
5990 mutex_unlock(&mlx5_ib_multiport_mutex);
5991
5992 mlx5_nic_vport_disable_roce(dev->mdev);
5993}
5994
Jason Gunthorpe9a119cd2018-07-04 08:50:28 +03005995ADD_UVERBS_ATTRIBUTES_SIMPLE(
5996 mlx5_ib_dm,
5997 UVERBS_OBJECT_DM,
5998 UVERBS_METHOD_DM_ALLOC,
5999 UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_ALLOC_DM_RESP_START_OFFSET,
6000 UVERBS_ATTR_TYPE(u64),
Jason Gunthorpe83bb4442018-07-04 08:50:29 +03006001 UA_MANDATORY),
Jason Gunthorpe9a119cd2018-07-04 08:50:28 +03006002 UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_ALLOC_DM_RESP_PAGE_INDEX,
6003 UVERBS_ATTR_TYPE(u16),
Ariel Levkovich3b113a12019-05-05 17:07:11 +03006004 UA_OPTIONAL),
6005 UVERBS_ATTR_CONST_IN(MLX5_IB_ATTR_ALLOC_DM_REQ_TYPE,
6006 enum mlx5_ib_uapi_dm_type,
6007 UA_OPTIONAL));
Ariel Levkovich24da0012018-04-05 18:53:27 +03006008
Jason Gunthorpe9a119cd2018-07-04 08:50:28 +03006009ADD_UVERBS_ATTRIBUTES_SIMPLE(
6010 mlx5_ib_flow_action,
6011 UVERBS_OBJECT_FLOW_ACTION,
6012 UVERBS_METHOD_FLOW_ACTION_ESP_CREATE,
Jason Gunthorpebccd0622018-07-26 16:37:14 -06006013 UVERBS_ATTR_FLAGS_IN(MLX5_IB_ATTR_CREATE_FLOW_ACTION_FLAGS,
6014 enum mlx5_ib_uapi_flow_action_flags));
Aviad Yehezkelc6475a02018-03-28 09:27:50 +03006015
Jason Gunthorpe0cbf4322018-11-12 22:59:50 +02006016static const struct uapi_definition mlx5_ib_defs[] = {
6017#if IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS)
Jason Gunthorpe36e235c2018-11-12 22:59:53 +02006018 UAPI_DEF_CHAIN(mlx5_ib_devx_defs),
Jason Gunthorpe0cbf4322018-11-12 22:59:50 +02006019 UAPI_DEF_CHAIN(mlx5_ib_flow_defs),
6020#endif
Matan Barak8c846602018-03-28 09:27:41 +03006021
Jason Gunthorpe0cbf4322018-11-12 22:59:50 +02006022 UAPI_DEF_CHAIN_OBJ_TREE(UVERBS_OBJECT_FLOW_ACTION,
6023 &mlx5_ib_flow_action),
6024 UAPI_DEF_CHAIN_OBJ_TREE(UVERBS_OBJECT_DM, &mlx5_ib_dm),
6025 {}
6026};
Matan Barak8c846602018-03-28 09:27:41 +03006027
Raed Salem1a1e03d2018-05-31 16:43:41 +03006028static int mlx5_ib_read_counters(struct ib_counters *counters,
6029 struct ib_counters_read_attr *read_attr,
6030 struct uverbs_attr_bundle *attrs)
6031{
6032 struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
6033 struct mlx5_read_counters_attr mread_attr = {};
6034 struct mlx5_ib_flow_counters_desc *desc;
6035 int ret, i;
6036
6037 mutex_lock(&mcounters->mcntrs_mutex);
6038 if (mcounters->cntrs_max_index > read_attr->ncounters) {
6039 ret = -EINVAL;
6040 goto err_bound;
6041 }
6042
6043 mread_attr.out = kcalloc(mcounters->counters_num, sizeof(u64),
6044 GFP_KERNEL);
6045 if (!mread_attr.out) {
6046 ret = -ENOMEM;
6047 goto err_bound;
6048 }
6049
6050 mread_attr.hw_cntrs_hndl = mcounters->hw_cntrs_hndl;
6051 mread_attr.flags = read_attr->flags;
6052 ret = mcounters->read_counters(counters->device, &mread_attr);
6053 if (ret)
6054 goto err_read;
6055
6056 /* do the pass over the counters data array to assign according to the
6057 * descriptions and indexing pairs
6058 */
6059 desc = mcounters->counters_data;
6060 for (i = 0; i < mcounters->ncounters; i++)
6061 read_attr->counters_buff[desc[i].index] += mread_attr.out[desc[i].description];
6062
6063err_read:
6064 kfree(mread_attr.out);
6065err_bound:
6066 mutex_unlock(&mcounters->mcntrs_mutex);
6067 return ret;
6068}
6069
Raed Salemb29e2a12018-05-31 16:43:38 +03006070static int mlx5_ib_destroy_counters(struct ib_counters *counters)
6071{
6072 struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
6073
Raed Salem3b3233f2018-05-31 16:43:39 +03006074 counters_clear_description(counters);
6075 if (mcounters->hw_cntrs_hndl)
6076 mlx5_fc_destroy(to_mdev(counters->device)->mdev,
6077 mcounters->hw_cntrs_hndl);
6078
Raed Salemb29e2a12018-05-31 16:43:38 +03006079 kfree(mcounters);
6080
6081 return 0;
6082}
6083
6084static struct ib_counters *mlx5_ib_create_counters(struct ib_device *device,
6085 struct uverbs_attr_bundle *attrs)
6086{
6087 struct mlx5_ib_mcounters *mcounters;
6088
6089 mcounters = kzalloc(sizeof(*mcounters), GFP_KERNEL);
6090 if (!mcounters)
6091 return ERR_PTR(-ENOMEM);
6092
Raed Salem3b3233f2018-05-31 16:43:39 +03006093 mutex_init(&mcounters->mcntrs_mutex);
6094
Raed Salemb29e2a12018-05-31 16:43:38 +03006095 return &mcounters->ibcntrs;
6096}
6097
Mark Blochfb652d32019-03-28 15:27:42 +02006098static void mlx5_ib_stage_init_cleanup(struct mlx5_ib_dev *dev)
Eli Cohene126ba92013-07-07 17:25:49 +03006099{
Ariel Levkovich25c13322019-05-05 17:07:13 +03006100 struct mlx5_core_dev *mdev = dev->mdev;
6101
Daniel Jurgens32f69e42018-01-04 17:25:36 +02006102 mlx5_ib_cleanup_multiport_master(dev);
Leon Romanovsky13859d5d2019-01-08 16:07:26 +02006103 if (IS_ENABLED(CONFIG_INFINIBAND_ON_DEMAND_PAGING)) {
Yishai Hadas534fd7a2019-01-13 16:01:17 +02006104 srcu_barrier(&dev->mr_srcu);
Leon Romanovsky13859d5d2019-01-08 16:07:26 +02006105 cleanup_srcu_struct(&dev->mr_srcu);
Leon Romanovsky13859d5d2019-01-08 16:07:26 +02006106 }
Ariel Levkovich4056b122019-05-05 17:07:12 +03006107
6108 WARN_ON(!bitmap_empty(dev->dm.memic_alloc_pages, MLX5_MAX_MEMIC_PAGES));
Ariel Levkovich25c13322019-05-05 17:07:13 +03006109
6110 WARN_ON(dev->dm.steering_sw_icm_alloc_blocks &&
6111 !bitmap_empty(
6112 dev->dm.steering_sw_icm_alloc_blocks,
6113 BIT(MLX5_CAP_DEV_MEM(mdev, log_steering_sw_icm_size) -
6114 MLX5_LOG_SW_ICM_BLOCK_SIZE(mdev))));
6115
6116 kfree(dev->dm.steering_sw_icm_alloc_blocks);
6117
6118 WARN_ON(dev->dm.header_modify_sw_icm_alloc_blocks &&
6119 !bitmap_empty(dev->dm.header_modify_sw_icm_alloc_blocks,
6120 BIT(MLX5_CAP_DEV_MEM(
6121 mdev, log_header_modify_sw_icm_size) -
6122 MLX5_LOG_SW_ICM_BLOCK_SIZE(mdev))));
6123
6124 kfree(dev->dm.header_modify_sw_icm_alloc_blocks);
Mark Bloch16c19752018-01-01 13:06:58 +02006125}
6126
Mark Blochfb652d32019-03-28 15:27:42 +02006127static int mlx5_ib_stage_init_init(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02006128{
6129 struct mlx5_core_dev *mdev = dev->mdev;
Ariel Levkovich25c13322019-05-05 17:07:13 +03006130 u64 header_modify_icm_blocks = 0;
6131 u64 steering_icm_blocks = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03006132 int err;
Daniel Jurgens32f69e42018-01-04 17:25:36 +02006133 int i;
Eli Cohene126ba92013-07-07 17:25:49 +03006134
Daniel Jurgens32f69e42018-01-04 17:25:36 +02006135 for (i = 0; i < dev->num_ports; i++) {
6136 spin_lock_init(&dev->port[i].mp.mpi_lock);
Mark Bloch95579e72019-03-28 15:27:33 +02006137 rwlock_init(&dev->port[i].roce.netdev_lock);
Mark Blochd3b5cc12019-03-28 15:46:26 +02006138 dev->port[i].roce.dev = dev;
6139 dev->port[i].roce.native_port_num = i + 1;
6140 dev->port[i].roce.last_port_state = IB_PORT_DOWN;
Daniel Jurgens32f69e42018-01-04 17:25:36 +02006141 }
6142
6143 err = mlx5_ib_init_multiport_master(dev);
Eli Cohene126ba92013-07-07 17:25:49 +03006144 if (err)
Mark Blochda796cc2019-03-28 15:27:35 +02006145 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03006146
Mark Blocha989ea02019-03-28 15:27:40 +02006147 err = set_has_smi_cap(dev);
6148 if (err)
6149 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03006150
Daniel Jurgens32f69e42018-01-04 17:25:36 +02006151 if (!mlx5_core_mp_enabled(mdev)) {
Daniel Jurgens32f69e42018-01-04 17:25:36 +02006152 for (i = 1; i <= dev->num_ports; i++) {
6153 err = get_port_caps(dev, i);
6154 if (err)
6155 break;
6156 }
6157 } else {
6158 err = get_port_caps(dev, mlx5_core_native_port_num(mdev));
6159 }
6160 if (err)
6161 goto err_mp;
6162
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03006163 if (mlx5_use_mad_ifc(dev))
6164 get_ext_port_caps(dev);
Eli Cohene126ba92013-07-07 17:25:49 +03006165
Eli Cohene126ba92013-07-07 17:25:49 +03006166 dev->ib_dev.node_type = RDMA_NODE_IB_CA;
Sagi Grimbergc6790aa2015-09-24 10:34:23 +03006167 dev->ib_dev.local_dma_lkey = 0 /* not supported for now */;
Daniel Jurgens508562d2018-01-04 17:25:34 +02006168 dev->ib_dev.phys_port_cnt = dev->num_ports;
Saeed Mahameedf2f3df52018-11-19 10:52:38 -08006169 dev->ib_dev.num_comp_vectors = mlx5_comp_vectors_count(mdev);
Vu Phamc42260f12019-04-29 18:14:05 +00006170 dev->ib_dev.dev.parent = mdev->device;
Eli Cohene126ba92013-07-07 17:25:49 +03006171
Mark Bloch3cc297d2018-01-01 13:07:03 +02006172 mutex_init(&dev->cap_mask_mutex);
6173 INIT_LIST_HEAD(&dev->qp_list);
6174 spin_lock_init(&dev->reset_flow_resource_lock);
6175
Ariel Levkovich25c13322019-05-05 17:07:13 +03006176 if (MLX5_CAP_GEN_64(mdev, general_obj_types) &
6177 MLX5_GENERAL_OBJ_TYPES_CAP_SW_ICM) {
6178 if (MLX5_CAP64_DEV_MEM(mdev, steering_sw_icm_start_address)) {
6179 steering_icm_blocks =
6180 BIT(MLX5_CAP_DEV_MEM(mdev,
6181 log_steering_sw_icm_size) -
6182 MLX5_LOG_SW_ICM_BLOCK_SIZE(mdev));
6183
6184 dev->dm.steering_sw_icm_alloc_blocks =
6185 kcalloc(BITS_TO_LONGS(steering_icm_blocks),
6186 sizeof(unsigned long), GFP_KERNEL);
6187 if (!dev->dm.steering_sw_icm_alloc_blocks)
6188 goto err_mp;
6189 }
6190
6191 if (MLX5_CAP64_DEV_MEM(mdev,
6192 header_modify_sw_icm_start_address)) {
6193 header_modify_icm_blocks = BIT(
6194 MLX5_CAP_DEV_MEM(
6195 mdev, log_header_modify_sw_icm_size) -
6196 MLX5_LOG_SW_ICM_BLOCK_SIZE(mdev));
6197
6198 dev->dm.header_modify_sw_icm_alloc_blocks =
6199 kcalloc(BITS_TO_LONGS(header_modify_icm_blocks),
6200 sizeof(unsigned long), GFP_KERNEL);
6201 if (!dev->dm.header_modify_sw_icm_alloc_blocks)
6202 goto err_dm;
6203 }
6204 }
6205
Ariel Levkovich3b113a12019-05-05 17:07:11 +03006206 spin_lock_init(&dev->dm.lock);
6207 dev->dm.dev = mdev;
Ariel Levkovich24da0012018-04-05 18:53:27 +03006208
Leon Romanovsky13859d5d2019-01-08 16:07:26 +02006209 if (IS_ENABLED(CONFIG_INFINIBAND_ON_DEMAND_PAGING)) {
Leon Romanovsky13859d5d2019-01-08 16:07:26 +02006210 err = init_srcu_struct(&dev->mr_srcu);
Moni Shouaa6bc3872019-02-17 16:08:22 +02006211 if (err)
Ariel Levkovich25c13322019-05-05 17:07:13 +03006212 goto err_dm;
Jason Gunthorpe623d1542018-12-20 16:39:26 -07006213 }
Mark Bloch3cc297d2018-01-01 13:07:03 +02006214
Mark Bloch16c19752018-01-01 13:06:58 +02006215 return 0;
Ariel Levkovich25c13322019-05-05 17:07:13 +03006216
6217err_dm:
6218 kfree(dev->dm.steering_sw_icm_alloc_blocks);
6219 kfree(dev->dm.header_modify_sw_icm_alloc_blocks);
6220
Daniel Jurgens32f69e42018-01-04 17:25:36 +02006221err_mp:
6222 mlx5_ib_cleanup_multiport_master(dev);
Mark Bloch16c19752018-01-01 13:06:58 +02006223
Mark Bloch16c19752018-01-01 13:06:58 +02006224 return -ENOMEM;
6225}
6226
Mark Bloch9a4ca382018-01-16 14:42:35 +00006227static int mlx5_ib_stage_flow_db_init(struct mlx5_ib_dev *dev)
6228{
6229 dev->flow_db = kzalloc(sizeof(*dev->flow_db), GFP_KERNEL);
6230
6231 if (!dev->flow_db)
6232 return -ENOMEM;
6233
6234 mutex_init(&dev->flow_db->lock);
6235
6236 return 0;
6237}
6238
6239static void mlx5_ib_stage_flow_db_cleanup(struct mlx5_ib_dev *dev)
6240{
6241 kfree(dev->flow_db);
6242}
6243
Kamal Heib96458233e2018-12-10 21:09:38 +02006244static const struct ib_device_ops mlx5_ib_dev_ops = {
Jason Gunthorpe7a154142019-06-05 14:39:26 -03006245 .owner = THIS_MODULE,
Jason Gunthorpeb9560a42019-06-05 14:39:24 -03006246 .driver_id = RDMA_DRIVER_MLX5,
Jason Gunthorpe72c6ec12019-06-05 14:39:25 -03006247 .uverbs_abi_ver = MLX5_IB_UVERBS_ABI_VERSION,
Jason Gunthorpeb9560a42019-06-05 14:39:24 -03006248
Kamal Heib96458233e2018-12-10 21:09:38 +02006249 .add_gid = mlx5_ib_add_gid,
6250 .alloc_mr = mlx5_ib_alloc_mr,
Max Gurtovoy6c984472019-06-11 18:52:42 +03006251 .alloc_mr_integrity = mlx5_ib_alloc_mr_integrity,
Kamal Heib96458233e2018-12-10 21:09:38 +02006252 .alloc_pd = mlx5_ib_alloc_pd,
6253 .alloc_ucontext = mlx5_ib_alloc_ucontext,
6254 .attach_mcast = mlx5_ib_mcg_attach,
6255 .check_mr_status = mlx5_ib_check_mr_status,
6256 .create_ah = mlx5_ib_create_ah,
6257 .create_counters = mlx5_ib_create_counters,
6258 .create_cq = mlx5_ib_create_cq,
6259 .create_flow = mlx5_ib_create_flow,
6260 .create_qp = mlx5_ib_create_qp,
6261 .create_srq = mlx5_ib_create_srq,
6262 .dealloc_pd = mlx5_ib_dealloc_pd,
6263 .dealloc_ucontext = mlx5_ib_dealloc_ucontext,
6264 .del_gid = mlx5_ib_del_gid,
6265 .dereg_mr = mlx5_ib_dereg_mr,
6266 .destroy_ah = mlx5_ib_destroy_ah,
6267 .destroy_counters = mlx5_ib_destroy_counters,
6268 .destroy_cq = mlx5_ib_destroy_cq,
6269 .destroy_flow = mlx5_ib_destroy_flow,
6270 .destroy_flow_action = mlx5_ib_destroy_flow_action,
6271 .destroy_qp = mlx5_ib_destroy_qp,
6272 .destroy_srq = mlx5_ib_destroy_srq,
6273 .detach_mcast = mlx5_ib_mcg_detach,
6274 .disassociate_ucontext = mlx5_ib_disassociate_ucontext,
6275 .drain_rq = mlx5_ib_drain_rq,
6276 .drain_sq = mlx5_ib_drain_sq,
6277 .get_dev_fw_str = get_dev_fw_str,
6278 .get_dma_mr = mlx5_ib_get_dma_mr,
6279 .get_link_layer = mlx5_ib_port_link_layer,
6280 .map_mr_sg = mlx5_ib_map_mr_sg,
Max Gurtovoy6c984472019-06-11 18:52:42 +03006281 .map_mr_sg_pi = mlx5_ib_map_mr_sg_pi,
Kamal Heib96458233e2018-12-10 21:09:38 +02006282 .mmap = mlx5_ib_mmap,
6283 .modify_cq = mlx5_ib_modify_cq,
6284 .modify_device = mlx5_ib_modify_device,
6285 .modify_port = mlx5_ib_modify_port,
6286 .modify_qp = mlx5_ib_modify_qp,
6287 .modify_srq = mlx5_ib_modify_srq,
6288 .poll_cq = mlx5_ib_poll_cq,
6289 .post_recv = mlx5_ib_post_recv,
6290 .post_send = mlx5_ib_post_send,
6291 .post_srq_recv = mlx5_ib_post_srq_recv,
6292 .process_mad = mlx5_ib_process_mad,
6293 .query_ah = mlx5_ib_query_ah,
6294 .query_device = mlx5_ib_query_device,
6295 .query_gid = mlx5_ib_query_gid,
6296 .query_pkey = mlx5_ib_query_pkey,
6297 .query_qp = mlx5_ib_query_qp,
6298 .query_srq = mlx5_ib_query_srq,
6299 .read_counters = mlx5_ib_read_counters,
6300 .reg_user_mr = mlx5_ib_reg_user_mr,
6301 .req_notify_cq = mlx5_ib_arm_cq,
6302 .rereg_user_mr = mlx5_ib_rereg_user_mr,
6303 .resize_cq = mlx5_ib_resize_cq,
Leon Romanovskyd3456912019-04-03 16:42:42 +03006304
6305 INIT_RDMA_OBJ_SIZE(ib_ah, mlx5_ib_ah, ibah),
Leon Romanovskye39afe32019-05-28 14:37:29 +03006306 INIT_RDMA_OBJ_SIZE(ib_cq, mlx5_ib_cq, ibcq),
Leon Romanovsky21a428a2019-02-03 14:55:51 +02006307 INIT_RDMA_OBJ_SIZE(ib_pd, mlx5_ib_pd, ibpd),
Leon Romanovsky68e326d2019-04-03 16:42:43 +03006308 INIT_RDMA_OBJ_SIZE(ib_srq, mlx5_ib_srq, ibsrq),
Leon Romanovskya2a074e2019-02-12 20:39:16 +02006309 INIT_RDMA_OBJ_SIZE(ib_ucontext, mlx5_ib_ucontext, ibucontext),
Kamal Heib96458233e2018-12-10 21:09:38 +02006310};
6311
6312static const struct ib_device_ops mlx5_ib_dev_flow_ipsec_ops = {
6313 .create_flow_action_esp = mlx5_ib_create_flow_action_esp,
6314 .modify_flow_action_esp = mlx5_ib_modify_flow_action_esp,
6315};
6316
6317static const struct ib_device_ops mlx5_ib_dev_ipoib_enhanced_ops = {
6318 .rdma_netdev_get_params = mlx5_ib_rn_get_params,
6319};
6320
6321static const struct ib_device_ops mlx5_ib_dev_sriov_ops = {
6322 .get_vf_config = mlx5_ib_get_vf_config,
6323 .get_vf_stats = mlx5_ib_get_vf_stats,
6324 .set_vf_guid = mlx5_ib_set_vf_guid,
6325 .set_vf_link_state = mlx5_ib_set_vf_link_state,
6326};
6327
6328static const struct ib_device_ops mlx5_ib_dev_mw_ops = {
6329 .alloc_mw = mlx5_ib_alloc_mw,
6330 .dealloc_mw = mlx5_ib_dealloc_mw,
6331};
6332
6333static const struct ib_device_ops mlx5_ib_dev_xrc_ops = {
6334 .alloc_xrcd = mlx5_ib_alloc_xrcd,
6335 .dealloc_xrcd = mlx5_ib_dealloc_xrcd,
6336};
6337
6338static const struct ib_device_ops mlx5_ib_dev_dm_ops = {
6339 .alloc_dm = mlx5_ib_alloc_dm,
6340 .dealloc_dm = mlx5_ib_dealloc_dm,
6341 .reg_dm_mr = mlx5_ib_reg_dm_mr,
6342};
6343
Mark Blochfb652d32019-03-28 15:27:42 +02006344static int mlx5_ib_stage_caps_init(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02006345{
6346 struct mlx5_core_dev *mdev = dev->mdev;
Mark Bloch16c19752018-01-01 13:06:58 +02006347 int err;
6348
Eli Cohene126ba92013-07-07 17:25:49 +03006349 dev->ib_dev.uverbs_cmd_mask =
6350 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
6351 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
6352 (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
6353 (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
6354 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
Moni Shoua41c450f2016-11-23 08:23:26 +02006355 (1ull << IB_USER_VERBS_CMD_CREATE_AH) |
6356 (1ull << IB_USER_VERBS_CMD_DESTROY_AH) |
Eli Cohene126ba92013-07-07 17:25:49 +03006357 (1ull << IB_USER_VERBS_CMD_REG_MR) |
Noa Osherovich56e11d62016-02-29 16:46:51 +02006358 (1ull << IB_USER_VERBS_CMD_REREG_MR) |
Eli Cohene126ba92013-07-07 17:25:49 +03006359 (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
6360 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
6361 (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
6362 (1ull << IB_USER_VERBS_CMD_RESIZE_CQ) |
6363 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
6364 (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
6365 (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
6366 (1ull << IB_USER_VERBS_CMD_QUERY_QP) |
6367 (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
6368 (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST) |
6369 (1ull << IB_USER_VERBS_CMD_DETACH_MCAST) |
6370 (1ull << IB_USER_VERBS_CMD_CREATE_SRQ) |
6371 (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ) |
6372 (1ull << IB_USER_VERBS_CMD_QUERY_SRQ) |
6373 (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ) |
6374 (1ull << IB_USER_VERBS_CMD_CREATE_XSRQ) |
6375 (1ull << IB_USER_VERBS_CMD_OPEN_QP);
Haggai Eran1707cb42015-02-08 13:28:52 +02006376 dev->ib_dev.uverbs_ex_cmd_mask =
Matan Barakd4584dd2016-01-28 17:51:46 +02006377 (1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE) |
6378 (1ull << IB_USER_VERBS_EX_CMD_CREATE_CQ) |
Bodong Wang7d29f342016-12-01 13:43:16 +02006379 (1ull << IB_USER_VERBS_EX_CMD_CREATE_QP) |
Yonatan Cohenb0e9df62017-11-13 10:51:15 +02006380 (1ull << IB_USER_VERBS_EX_CMD_MODIFY_QP) |
Kamal Heib96458233e2018-12-10 21:09:38 +02006381 (1ull << IB_USER_VERBS_EX_CMD_MODIFY_CQ) |
6382 (1ull << IB_USER_VERBS_EX_CMD_CREATE_FLOW) |
6383 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_FLOW);
Eli Cohene126ba92013-07-07 17:25:49 +03006384
Denis Drozdovf6a8a192018-08-14 14:08:51 +03006385 if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads) &&
6386 IS_ENABLED(CONFIG_MLX5_CORE_IPOIB))
Kamal Heib96458233e2018-12-10 21:09:38 +02006387 ib_set_device_ops(&dev->ib_dev,
6388 &mlx5_ib_dev_ipoib_enhanced_ops);
Niranjana Vishwanathapura8e959602017-06-30 13:14:46 -07006389
Kamal Heib96458233e2018-12-10 21:09:38 +02006390 if (mlx5_core_is_pf(mdev))
6391 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_sriov_ops);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03006392
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03006393 dev->umr_fence = mlx5_get_umr_fence(MLX5_CAP_GEN(mdev, umr_fence));
6394
Matan Barakd2370e02016-02-29 18:05:30 +02006395 if (MLX5_CAP_GEN(mdev, imaicl)) {
Matan Barakd2370e02016-02-29 18:05:30 +02006396 dev->ib_dev.uverbs_cmd_mask |=
6397 (1ull << IB_USER_VERBS_CMD_ALLOC_MW) |
6398 (1ull << IB_USER_VERBS_CMD_DEALLOC_MW);
Kamal Heib96458233e2018-12-10 21:09:38 +02006399 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_mw_ops);
Matan Barakd2370e02016-02-29 18:05:30 +02006400 }
6401
Saeed Mahameed938fe832015-05-28 22:28:41 +03006402 if (MLX5_CAP_GEN(mdev, xrc)) {
Eli Cohene126ba92013-07-07 17:25:49 +03006403 dev->ib_dev.uverbs_cmd_mask |=
6404 (1ull << IB_USER_VERBS_CMD_OPEN_XRCD) |
6405 (1ull << IB_USER_VERBS_CMD_CLOSE_XRCD);
Kamal Heib96458233e2018-12-10 21:09:38 +02006406 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_xrc_ops);
Eli Cohene126ba92013-07-07 17:25:49 +03006407 }
6408
Ariel Levkovich25c13322019-05-05 17:07:13 +03006409 if (MLX5_CAP_DEV_MEM(mdev, memic) ||
6410 MLX5_CAP_GEN_64(dev->mdev, general_obj_types) &
6411 MLX5_GENERAL_OBJ_TYPES_CAP_SW_ICM)
Kamal Heib96458233e2018-12-10 21:09:38 +02006412 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_dm_ops);
Ariel Levkovich24da0012018-04-05 18:53:27 +03006413
Jason Gunthorpedfb631a2018-11-12 22:59:49 +02006414 if (mlx5_accel_ipsec_device_caps(dev->mdev) &
Kamal Heib96458233e2018-12-10 21:09:38 +02006415 MLX5_ACCEL_IPSEC_CAP_DEVICE)
6416 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_flow_ipsec_ops);
Kamal Heib96458233e2018-12-10 21:09:38 +02006417 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_ops);
Yishai Hadas81e30882017-06-08 16:15:09 +03006418
Jason Gunthorpe36e235c2018-11-12 22:59:53 +02006419 if (IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS))
6420 dev->ib_dev.driver_def = mlx5_ib_defs;
Eli Cohene126ba92013-07-07 17:25:49 +03006421
6422 err = init_node_data(dev);
6423 if (err)
Mark Bloch16c19752018-01-01 13:06:58 +02006424 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03006425
Mark Blochc8b89922018-01-01 13:07:02 +02006426 if ((MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_ETH) &&
Jason Gunthorpee7996a92018-01-29 13:26:40 -07006427 (MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) ||
6428 MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
Mark Blocha560f1d2018-09-17 13:30:47 +03006429 mutex_init(&dev->lb.mutex);
Mark Blochc8b89922018-01-01 13:07:02 +02006430
Mark Bloch16c19752018-01-01 13:06:58 +02006431 return 0;
6432}
6433
Kamal Heib96458233e2018-12-10 21:09:38 +02006434static const struct ib_device_ops mlx5_ib_dev_port_ops = {
6435 .get_port_immutable = mlx5_port_immutable,
6436 .query_port = mlx5_ib_query_port,
6437};
6438
Mark Bloch8e6efa32017-11-06 12:22:13 +00006439static int mlx5_ib_stage_non_default_cb(struct mlx5_ib_dev *dev)
6440{
Kamal Heib96458233e2018-12-10 21:09:38 +02006441 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_port_ops);
Mark Bloch8e6efa32017-11-06 12:22:13 +00006442 return 0;
6443}
6444
Kamal Heib96458233e2018-12-10 21:09:38 +02006445static const struct ib_device_ops mlx5_ib_dev_port_rep_ops = {
6446 .get_port_immutable = mlx5_port_rep_immutable,
6447 .query_port = mlx5_ib_rep_query_port,
6448};
6449
Mark Blochfb652d32019-03-28 15:27:42 +02006450static int mlx5_ib_stage_rep_non_default_cb(struct mlx5_ib_dev *dev)
Mark Bloch8e6efa32017-11-06 12:22:13 +00006451{
Kamal Heib96458233e2018-12-10 21:09:38 +02006452 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_port_rep_ops);
Mark Bloch8e6efa32017-11-06 12:22:13 +00006453 return 0;
6454}
6455
Kamal Heib96458233e2018-12-10 21:09:38 +02006456static const struct ib_device_ops mlx5_ib_dev_common_roce_ops = {
6457 .create_rwq_ind_table = mlx5_ib_create_rwq_ind_table,
6458 .create_wq = mlx5_ib_create_wq,
6459 .destroy_rwq_ind_table = mlx5_ib_destroy_rwq_ind_table,
6460 .destroy_wq = mlx5_ib_destroy_wq,
6461 .get_netdev = mlx5_ib_get_netdev,
6462 .modify_wq = mlx5_ib_modify_wq,
6463};
6464
Leon Romanovskye3f1ed12018-07-08 12:55:43 +03006465static int mlx5_ib_stage_common_roce_init(struct mlx5_ib_dev *dev)
Mark Bloch8e6efa32017-11-06 12:22:13 +00006466{
Leon Romanovskye3f1ed12018-07-08 12:55:43 +03006467 u8 port_num;
Mark Bloch8e6efa32017-11-06 12:22:13 +00006468
Mark Bloch8e6efa32017-11-06 12:22:13 +00006469 dev->ib_dev.uverbs_ex_cmd_mask |=
6470 (1ull << IB_USER_VERBS_EX_CMD_CREATE_WQ) |
6471 (1ull << IB_USER_VERBS_EX_CMD_MODIFY_WQ) |
6472 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_WQ) |
6473 (1ull << IB_USER_VERBS_EX_CMD_CREATE_RWQ_IND_TBL) |
6474 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_RWQ_IND_TBL);
Kamal Heib96458233e2018-12-10 21:09:38 +02006475 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_common_roce_ops);
Mark Bloch8e6efa32017-11-06 12:22:13 +00006476
Leon Romanovskye3f1ed12018-07-08 12:55:43 +03006477 port_num = mlx5_core_native_port_num(dev->mdev) - 1;
6478
Mark Bloch26628e22019-03-28 15:27:41 +02006479 /* Register only for native ports */
Mark Bloch8e6efa32017-11-06 12:22:13 +00006480 return mlx5_add_netdev_notifier(dev, port_num);
6481}
6482
6483static void mlx5_ib_stage_common_roce_cleanup(struct mlx5_ib_dev *dev)
6484{
6485 u8 port_num = mlx5_core_native_port_num(dev->mdev) - 1;
6486
6487 mlx5_remove_netdev_notifier(dev, port_num);
6488}
6489
Mark Blochfb652d32019-03-28 15:27:42 +02006490static int mlx5_ib_stage_rep_roce_init(struct mlx5_ib_dev *dev)
Mark Bloch8e6efa32017-11-06 12:22:13 +00006491{
6492 struct mlx5_core_dev *mdev = dev->mdev;
6493 enum rdma_link_layer ll;
6494 int port_type_cap;
6495 int err = 0;
Mark Bloch8e6efa32017-11-06 12:22:13 +00006496
Mark Bloch8e6efa32017-11-06 12:22:13 +00006497 port_type_cap = MLX5_CAP_GEN(mdev, port_type);
6498 ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
6499
6500 if (ll == IB_LINK_LAYER_ETHERNET)
Leon Romanovskye3f1ed12018-07-08 12:55:43 +03006501 err = mlx5_ib_stage_common_roce_init(dev);
Mark Bloch8e6efa32017-11-06 12:22:13 +00006502
6503 return err;
6504}
6505
Mark Blochfb652d32019-03-28 15:27:42 +02006506static void mlx5_ib_stage_rep_roce_cleanup(struct mlx5_ib_dev *dev)
Mark Bloch8e6efa32017-11-06 12:22:13 +00006507{
6508 mlx5_ib_stage_common_roce_cleanup(dev);
6509}
6510
Mark Bloch16c19752018-01-01 13:06:58 +02006511static int mlx5_ib_stage_roce_init(struct mlx5_ib_dev *dev)
6512{
6513 struct mlx5_core_dev *mdev = dev->mdev;
6514 enum rdma_link_layer ll;
6515 int port_type_cap;
6516 int err;
6517
6518 port_type_cap = MLX5_CAP_GEN(mdev, port_type);
6519 ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
6520
Achiad Shochatfc24fc52015-12-23 18:47:17 +02006521 if (ll == IB_LINK_LAYER_ETHERNET) {
Leon Romanovskye3f1ed12018-07-08 12:55:43 +03006522 err = mlx5_ib_stage_common_roce_init(dev);
Achiad Shochatfc24fc52015-12-23 18:47:17 +02006523 if (err)
Mark Bloch16c19752018-01-01 13:06:58 +02006524 return err;
Mark Bloch8e6efa32017-11-06 12:22:13 +00006525
Leon Romanovskye3f1ed12018-07-08 12:55:43 +03006526 err = mlx5_enable_eth(dev);
Mark Bloch8e6efa32017-11-06 12:22:13 +00006527 if (err)
6528 goto cleanup;
Achiad Shochatfc24fc52015-12-23 18:47:17 +02006529 }
6530
Mark Bloch16c19752018-01-01 13:06:58 +02006531 return 0;
Mark Bloch8e6efa32017-11-06 12:22:13 +00006532cleanup:
6533 mlx5_ib_stage_common_roce_cleanup(dev);
6534
6535 return err;
Mark Bloch16c19752018-01-01 13:06:58 +02006536}
Eli Cohene126ba92013-07-07 17:25:49 +03006537
Mark Bloch16c19752018-01-01 13:06:58 +02006538static void mlx5_ib_stage_roce_cleanup(struct mlx5_ib_dev *dev)
6539{
6540 struct mlx5_core_dev *mdev = dev->mdev;
6541 enum rdma_link_layer ll;
6542 int port_type_cap;
Eli Cohene126ba92013-07-07 17:25:49 +03006543
Mark Bloch16c19752018-01-01 13:06:58 +02006544 port_type_cap = MLX5_CAP_GEN(mdev, port_type);
6545 ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
6546
6547 if (ll == IB_LINK_LAYER_ETHERNET) {
6548 mlx5_disable_eth(dev);
Mark Bloch8e6efa32017-11-06 12:22:13 +00006549 mlx5_ib_stage_common_roce_cleanup(dev);
Kamal Heib45bded22017-01-18 14:10:32 +02006550 }
Mark Bloch16c19752018-01-01 13:06:58 +02006551}
Haggai Eran6aec21f2014-12-11 17:04:23 +02006552
Mark Blochfb652d32019-03-28 15:27:42 +02006553static int mlx5_ib_stage_dev_res_init(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02006554{
6555 return create_dev_resources(&dev->devr);
6556}
Parav Pandit4a2da0b2017-05-30 10:05:15 +03006557
Mark Blochfb652d32019-03-28 15:27:42 +02006558static void mlx5_ib_stage_dev_res_cleanup(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02006559{
6560 destroy_dev_resources(&dev->devr);
6561}
6562
6563static int mlx5_ib_stage_odp_init(struct mlx5_ib_dev *dev)
6564{
Mark Bloch07321b32018-01-01 13:07:00 +02006565 mlx5_ib_internal_fill_odp_caps(dev);
6566
Mark Bloch16c19752018-01-01 13:06:58 +02006567 return mlx5_ib_odp_init_one(dev);
6568}
6569
Kamal Heibf3ffed02019-01-30 16:13:42 +02006570static void mlx5_ib_stage_odp_cleanup(struct mlx5_ib_dev *dev)
Saeed Mahameedd5d284b2018-11-19 10:52:41 -08006571{
6572 mlx5_ib_odp_cleanup_one(dev);
6573}
6574
Kamal Heib96458233e2018-12-10 21:09:38 +02006575static const struct ib_device_ops mlx5_ib_dev_hw_stats_ops = {
6576 .alloc_hw_stats = mlx5_ib_alloc_hw_stats,
6577 .get_hw_stats = mlx5_ib_get_hw_stats,
Mark Zhang45842fc2019-07-02 13:02:38 +03006578 .counter_bind_qp = mlx5_ib_counter_bind_qp,
6579 .counter_unbind_qp = mlx5_ib_counter_unbind_qp,
6580 .counter_dealloc = mlx5_ib_counter_dealloc,
Mark Zhang18d422c2019-07-02 13:02:41 +03006581 .counter_alloc_stats = mlx5_ib_counter_alloc_stats,
6582 .counter_update_stats = mlx5_ib_counter_update_stats,
Kamal Heib96458233e2018-12-10 21:09:38 +02006583};
6584
Mark Blochfb652d32019-03-28 15:27:42 +02006585static int mlx5_ib_stage_counters_init(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02006586{
Mark Bloch5e1e7612018-01-01 13:07:01 +02006587 if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt)) {
Kamal Heib96458233e2018-12-10 21:09:38 +02006588 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_hw_stats_ops);
Mark Bloch5e1e7612018-01-01 13:07:01 +02006589
6590 return mlx5_ib_alloc_counters(dev);
6591 }
Mark Bloch16c19752018-01-01 13:06:58 +02006592
6593 return 0;
6594}
6595
Mark Blochfb652d32019-03-28 15:27:42 +02006596static void mlx5_ib_stage_counters_cleanup(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02006597{
6598 if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt))
6599 mlx5_ib_dealloc_counters(dev);
6600}
6601
6602static int mlx5_ib_stage_cong_debugfs_init(struct mlx5_ib_dev *dev)
6603{
Greg Kroah-Hartman73eb8f02019-01-22 16:17:57 +01006604 mlx5_ib_init_cong_debugfs(dev,
6605 mlx5_core_native_port_num(dev->mdev) - 1);
6606 return 0;
Mark Bloch16c19752018-01-01 13:06:58 +02006607}
6608
6609static void mlx5_ib_stage_cong_debugfs_cleanup(struct mlx5_ib_dev *dev)
6610{
Parav Pandita9e546e2018-01-04 17:25:39 +02006611 mlx5_ib_cleanup_cong_debugfs(dev,
6612 mlx5_core_native_port_num(dev->mdev) - 1);
Mark Bloch16c19752018-01-01 13:06:58 +02006613}
6614
6615static int mlx5_ib_stage_uar_init(struct mlx5_ib_dev *dev)
6616{
Eli Cohen5fe9dec2017-01-03 23:55:25 +02006617 dev->mdev->priv.uar = mlx5_get_uars_page(dev->mdev);
Leon Romanovsky444261c2018-04-23 17:01:56 +03006618 return PTR_ERR_OR_ZERO(dev->mdev->priv.uar);
Mark Bloch16c19752018-01-01 13:06:58 +02006619}
6620
6621static void mlx5_ib_stage_uar_cleanup(struct mlx5_ib_dev *dev)
6622{
6623 mlx5_put_uars_page(dev->mdev, dev->mdev->priv.uar);
6624}
6625
Mark Blochfb652d32019-03-28 15:27:42 +02006626static int mlx5_ib_stage_bfrag_init(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02006627{
6628 int err;
Eli Cohen5fe9dec2017-01-03 23:55:25 +02006629
6630 err = mlx5_alloc_bfreg(dev->mdev, &dev->bfreg, false, false);
6631 if (err)
Mark Bloch16c19752018-01-01 13:06:58 +02006632 return err;
Eli Cohen5fe9dec2017-01-03 23:55:25 +02006633
6634 err = mlx5_alloc_bfreg(dev->mdev, &dev->fp_bfreg, false, true);
6635 if (err)
Mark Bloch16c19752018-01-01 13:06:58 +02006636 mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
Eli Cohen5fe9dec2017-01-03 23:55:25 +02006637
Mark Bloch16c19752018-01-01 13:06:58 +02006638 return err;
6639}
Mark Bloch0837e862016-06-17 15:10:55 +03006640
Mark Blochfb652d32019-03-28 15:27:42 +02006641static void mlx5_ib_stage_bfrag_cleanup(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02006642{
6643 mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
6644 mlx5_free_bfreg(dev->mdev, &dev->bfreg);
6645}
Eli Cohene126ba92013-07-07 17:25:49 +03006646
Mark Blochfb652d32019-03-28 15:27:42 +02006647static int mlx5_ib_stage_ib_reg_init(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02006648{
Jason Gunthorpee349f852018-09-25 16:58:09 -06006649 const char *name;
6650
Parav Pandit508a5232018-10-11 22:31:54 +03006651 rdma_set_device_sysfs_group(&dev->ib_dev, &mlx5_attr_group);
Aviv Heller7c34ec12018-08-23 13:47:53 +03006652 if (!mlx5_lag_is_roce(dev->mdev))
Jason Gunthorpee349f852018-09-25 16:58:09 -06006653 name = "mlx5_%d";
6654 else
6655 name = "mlx5_bond_%d";
Parav Panditea4baf72018-12-18 14:28:30 +02006656 return ib_register_device(&dev->ib_dev, name);
Mark Bloch16c19752018-01-01 13:06:58 +02006657}
6658
Mark Blochfb652d32019-03-28 15:27:42 +02006659static void mlx5_ib_stage_pre_ib_reg_umr_cleanup(struct mlx5_ib_dev *dev)
Mark Bloch42cea832018-03-14 09:14:15 +02006660{
6661 destroy_umrc_res(dev);
6662}
6663
Mark Blochfb652d32019-03-28 15:27:42 +02006664static void mlx5_ib_stage_ib_reg_cleanup(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02006665{
6666 ib_unregister_device(&dev->ib_dev);
6667}
6668
Mark Blochfb652d32019-03-28 15:27:42 +02006669static int mlx5_ib_stage_post_ib_reg_umr_init(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02006670{
6671 return create_umr_res(dev);
6672}
6673
Mark Bloch16c19752018-01-01 13:06:58 +02006674static int mlx5_ib_stage_delay_drop_init(struct mlx5_ib_dev *dev)
6675{
Maor Gottlieb03404e82017-05-30 10:29:13 +03006676 init_delay_drop(dev);
6677
Mark Bloch16c19752018-01-01 13:06:58 +02006678 return 0;
6679}
6680
6681static void mlx5_ib_stage_delay_drop_cleanup(struct mlx5_ib_dev *dev)
6682{
6683 cancel_delay_drop(dev);
6684}
6685
Saeed Mahameeddf097a22018-11-26 14:39:00 -08006686static int mlx5_ib_stage_dev_notifier_init(struct mlx5_ib_dev *dev)
6687{
6688 dev->mdev_events.notifier_call = mlx5_ib_event;
6689 mlx5_notifier_register(dev->mdev, &dev->mdev_events);
6690 return 0;
6691}
6692
6693static void mlx5_ib_stage_dev_notifier_cleanup(struct mlx5_ib_dev *dev)
6694{
6695 mlx5_notifier_unregister(dev->mdev, &dev->mdev_events);
6696}
6697
Leon Romanovsky81773ce2018-11-28 20:53:39 +02006698static int mlx5_ib_stage_devx_init(struct mlx5_ib_dev *dev)
6699{
6700 int uid;
6701
Yishai Hadasfb981532018-11-26 08:28:36 +02006702 uid = mlx5_ib_devx_create(dev, false);
Yishai Hadase337dd52019-06-30 19:23:30 +03006703 if (uid > 0) {
Leon Romanovsky81773ce2018-11-28 20:53:39 +02006704 dev->devx_whitelist_uid = uid;
Yishai Hadase337dd52019-06-30 19:23:30 +03006705 mlx5_ib_devx_init_event_table(dev);
6706 }
Leon Romanovsky81773ce2018-11-28 20:53:39 +02006707
6708 return 0;
6709}
6710static void mlx5_ib_stage_devx_cleanup(struct mlx5_ib_dev *dev)
6711{
Yishai Hadase337dd52019-06-30 19:23:30 +03006712 if (dev->devx_whitelist_uid) {
6713 mlx5_ib_devx_cleanup_event_table(dev);
Leon Romanovsky81773ce2018-11-28 20:53:39 +02006714 mlx5_ib_devx_destroy(dev, dev->devx_whitelist_uid);
Yishai Hadase337dd52019-06-30 19:23:30 +03006715 }
Leon Romanovsky81773ce2018-11-28 20:53:39 +02006716}
6717
Mark Blochb5ca15a2018-01-23 11:16:30 +00006718void __mlx5_ib_remove(struct mlx5_ib_dev *dev,
6719 const struct mlx5_ib_profile *profile,
6720 int stage)
Mark Bloch16c19752018-01-01 13:06:58 +02006721{
6722 /* Number of stages to cleanup */
6723 while (stage) {
6724 stage--;
6725 if (profile->stage[stage].cleanup)
6726 profile->stage[stage].cleanup(dev);
6727 }
Mark Bloch4a6dc852019-03-28 15:27:34 +02006728
Mark Blochda796cc2019-03-28 15:27:35 +02006729 kfree(dev->port);
Mark Bloch4a6dc852019-03-28 15:27:34 +02006730 ib_dealloc_device(&dev->ib_dev);
Mark Bloch16c19752018-01-01 13:06:58 +02006731}
6732
Mark Blochb5ca15a2018-01-23 11:16:30 +00006733void *__mlx5_ib_add(struct mlx5_ib_dev *dev,
6734 const struct mlx5_ib_profile *profile)
Mark Bloch16c19752018-01-01 13:06:58 +02006735{
Mark Bloch16c19752018-01-01 13:06:58 +02006736 int err;
6737 int i;
Mark Bloch16c19752018-01-01 13:06:58 +02006738
Mark Bloch16c19752018-01-01 13:06:58 +02006739 for (i = 0; i < MLX5_IB_STAGE_MAX; i++) {
6740 if (profile->stage[i].init) {
6741 err = profile->stage[i].init(dev);
6742 if (err)
6743 goto err_out;
6744 }
6745 }
6746
6747 dev->profile = profile;
Eli Cohene126ba92013-07-07 17:25:49 +03006748 dev->ib_active = true;
6749
Jack Morgenstein9603b612014-07-28 23:30:22 +03006750 return dev;
Eli Cohene126ba92013-07-07 17:25:49 +03006751
Mark Bloch16c19752018-01-01 13:06:58 +02006752err_out:
6753 __mlx5_ib_remove(dev, profile, i);
Eli Cohene126ba92013-07-07 17:25:49 +03006754
Jack Morgenstein9603b612014-07-28 23:30:22 +03006755 return NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03006756}
6757
Mark Bloch16c19752018-01-01 13:06:58 +02006758static const struct mlx5_ib_profile pf_profile = {
6759 STAGE_CREATE(MLX5_IB_STAGE_INIT,
6760 mlx5_ib_stage_init_init,
6761 mlx5_ib_stage_init_cleanup),
Mark Bloch9a4ca382018-01-16 14:42:35 +00006762 STAGE_CREATE(MLX5_IB_STAGE_FLOW_DB,
6763 mlx5_ib_stage_flow_db_init,
6764 mlx5_ib_stage_flow_db_cleanup),
Mark Bloch16c19752018-01-01 13:06:58 +02006765 STAGE_CREATE(MLX5_IB_STAGE_CAPS,
6766 mlx5_ib_stage_caps_init,
6767 NULL),
Mark Bloch8e6efa32017-11-06 12:22:13 +00006768 STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB,
6769 mlx5_ib_stage_non_default_cb,
6770 NULL),
Mark Bloch16c19752018-01-01 13:06:58 +02006771 STAGE_CREATE(MLX5_IB_STAGE_ROCE,
6772 mlx5_ib_stage_roce_init,
6773 mlx5_ib_stage_roce_cleanup),
Leon Romanovskyf3da6572018-11-28 20:53:41 +02006774 STAGE_CREATE(MLX5_IB_STAGE_SRQ,
6775 mlx5_init_srq_table,
6776 mlx5_cleanup_srq_table),
Mark Bloch16c19752018-01-01 13:06:58 +02006777 STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES,
6778 mlx5_ib_stage_dev_res_init,
6779 mlx5_ib_stage_dev_res_cleanup),
Saeed Mahameeddf097a22018-11-26 14:39:00 -08006780 STAGE_CREATE(MLX5_IB_STAGE_DEVICE_NOTIFIER,
6781 mlx5_ib_stage_dev_notifier_init,
6782 mlx5_ib_stage_dev_notifier_cleanup),
Mark Bloch16c19752018-01-01 13:06:58 +02006783 STAGE_CREATE(MLX5_IB_STAGE_ODP,
6784 mlx5_ib_stage_odp_init,
Saeed Mahameedd5d284b2018-11-19 10:52:41 -08006785 mlx5_ib_stage_odp_cleanup),
Mark Bloch16c19752018-01-01 13:06:58 +02006786 STAGE_CREATE(MLX5_IB_STAGE_COUNTERS,
6787 mlx5_ib_stage_counters_init,
6788 mlx5_ib_stage_counters_cleanup),
6789 STAGE_CREATE(MLX5_IB_STAGE_CONG_DEBUGFS,
6790 mlx5_ib_stage_cong_debugfs_init,
6791 mlx5_ib_stage_cong_debugfs_cleanup),
6792 STAGE_CREATE(MLX5_IB_STAGE_UAR,
6793 mlx5_ib_stage_uar_init,
6794 mlx5_ib_stage_uar_cleanup),
6795 STAGE_CREATE(MLX5_IB_STAGE_BFREG,
6796 mlx5_ib_stage_bfrag_init,
6797 mlx5_ib_stage_bfrag_cleanup),
Mark Bloch42cea832018-03-14 09:14:15 +02006798 STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR,
6799 NULL,
6800 mlx5_ib_stage_pre_ib_reg_umr_cleanup),
Leon Romanovsky81773ce2018-11-28 20:53:39 +02006801 STAGE_CREATE(MLX5_IB_STAGE_WHITELIST_UID,
6802 mlx5_ib_stage_devx_init,
6803 mlx5_ib_stage_devx_cleanup),
Mark Bloch16c19752018-01-01 13:06:58 +02006804 STAGE_CREATE(MLX5_IB_STAGE_IB_REG,
6805 mlx5_ib_stage_ib_reg_init,
6806 mlx5_ib_stage_ib_reg_cleanup),
Mark Bloch42cea832018-03-14 09:14:15 +02006807 STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR,
6808 mlx5_ib_stage_post_ib_reg_umr_init,
6809 NULL),
Mark Bloch16c19752018-01-01 13:06:58 +02006810 STAGE_CREATE(MLX5_IB_STAGE_DELAY_DROP,
6811 mlx5_ib_stage_delay_drop_init,
6812 mlx5_ib_stage_delay_drop_cleanup),
Mark Bloch16c19752018-01-01 13:06:58 +02006813};
6814
Bodong Wangf0666f12019-02-12 22:55:34 -08006815const struct mlx5_ib_profile uplink_rep_profile = {
Mark Blochb5ca15a2018-01-23 11:16:30 +00006816 STAGE_CREATE(MLX5_IB_STAGE_INIT,
6817 mlx5_ib_stage_init_init,
6818 mlx5_ib_stage_init_cleanup),
6819 STAGE_CREATE(MLX5_IB_STAGE_FLOW_DB,
6820 mlx5_ib_stage_flow_db_init,
6821 mlx5_ib_stage_flow_db_cleanup),
6822 STAGE_CREATE(MLX5_IB_STAGE_CAPS,
6823 mlx5_ib_stage_caps_init,
6824 NULL),
6825 STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB,
6826 mlx5_ib_stage_rep_non_default_cb,
6827 NULL),
6828 STAGE_CREATE(MLX5_IB_STAGE_ROCE,
6829 mlx5_ib_stage_rep_roce_init,
6830 mlx5_ib_stage_rep_roce_cleanup),
Leon Romanovskyf3da6572018-11-28 20:53:41 +02006831 STAGE_CREATE(MLX5_IB_STAGE_SRQ,
6832 mlx5_init_srq_table,
6833 mlx5_cleanup_srq_table),
Mark Blochb5ca15a2018-01-23 11:16:30 +00006834 STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES,
6835 mlx5_ib_stage_dev_res_init,
6836 mlx5_ib_stage_dev_res_cleanup),
Saeed Mahameeddf097a22018-11-26 14:39:00 -08006837 STAGE_CREATE(MLX5_IB_STAGE_DEVICE_NOTIFIER,
6838 mlx5_ib_stage_dev_notifier_init,
6839 mlx5_ib_stage_dev_notifier_cleanup),
Mark Blochb5ca15a2018-01-23 11:16:30 +00006840 STAGE_CREATE(MLX5_IB_STAGE_COUNTERS,
6841 mlx5_ib_stage_counters_init,
6842 mlx5_ib_stage_counters_cleanup),
6843 STAGE_CREATE(MLX5_IB_STAGE_UAR,
6844 mlx5_ib_stage_uar_init,
6845 mlx5_ib_stage_uar_cleanup),
6846 STAGE_CREATE(MLX5_IB_STAGE_BFREG,
6847 mlx5_ib_stage_bfrag_init,
6848 mlx5_ib_stage_bfrag_cleanup),
David S. Miller03fe2de2018-03-23 11:24:57 -04006849 STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR,
6850 NULL,
6851 mlx5_ib_stage_pre_ib_reg_umr_cleanup),
Mark Bloch7f575102019-03-28 15:46:25 +02006852 STAGE_CREATE(MLX5_IB_STAGE_WHITELIST_UID,
6853 mlx5_ib_stage_devx_init,
6854 mlx5_ib_stage_devx_cleanup),
Mark Blochb5ca15a2018-01-23 11:16:30 +00006855 STAGE_CREATE(MLX5_IB_STAGE_IB_REG,
6856 mlx5_ib_stage_ib_reg_init,
6857 mlx5_ib_stage_ib_reg_cleanup),
David S. Miller03fe2de2018-03-23 11:24:57 -04006858 STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR,
6859 mlx5_ib_stage_post_ib_reg_umr_init,
6860 NULL),
Mark Blochb5ca15a2018-01-23 11:16:30 +00006861};
6862
Leon Romanovskye3f1ed12018-07-08 12:55:43 +03006863static void *mlx5_ib_add_slave_port(struct mlx5_core_dev *mdev)
Daniel Jurgens32f69e42018-01-04 17:25:36 +02006864{
6865 struct mlx5_ib_multiport_info *mpi;
6866 struct mlx5_ib_dev *dev;
6867 bool bound = false;
6868 int err;
6869
6870 mpi = kzalloc(sizeof(*mpi), GFP_KERNEL);
6871 if (!mpi)
6872 return NULL;
6873
6874 mpi->mdev = mdev;
6875
6876 err = mlx5_query_nic_vport_system_image_guid(mdev,
6877 &mpi->sys_image_guid);
6878 if (err) {
6879 kfree(mpi);
6880 return NULL;
6881 }
6882
6883 mutex_lock(&mlx5_ib_multiport_mutex);
6884 list_for_each_entry(dev, &mlx5_ib_dev_list, ib_dev_list) {
6885 if (dev->sys_image_guid == mpi->sys_image_guid)
6886 bound = mlx5_ib_bind_slave_port(dev, mpi);
6887
6888 if (bound) {
6889 rdma_roce_rescan_device(&dev->ib_dev);
6890 break;
6891 }
6892 }
6893
6894 if (!bound) {
6895 list_add_tail(&mpi->list, &mlx5_ib_unaffiliated_port_list);
Vu Phamc42260f12019-04-29 18:14:05 +00006896 dev_dbg(mdev->device,
6897 "no suitable IB device found to bind to, added to unaffiliated list.\n");
Daniel Jurgens32f69e42018-01-04 17:25:36 +02006898 }
6899 mutex_unlock(&mlx5_ib_multiport_mutex);
6900
6901 return mpi;
6902}
6903
Mark Bloch16c19752018-01-01 13:06:58 +02006904static void *mlx5_ib_add(struct mlx5_core_dev *mdev)
6905{
Daniel Jurgens32f69e42018-01-04 17:25:36 +02006906 enum rdma_link_layer ll;
Mark Blochb5ca15a2018-01-23 11:16:30 +00006907 struct mlx5_ib_dev *dev;
Daniel Jurgens32f69e42018-01-04 17:25:36 +02006908 int port_type_cap;
Mark Blochda796cc2019-03-28 15:27:35 +02006909 int num_ports;
Daniel Jurgens32f69e42018-01-04 17:25:36 +02006910
Mark Blochb5ca15a2018-01-23 11:16:30 +00006911 printk_once(KERN_INFO "%s", mlx5_version);
6912
Bodong Wangf0666f12019-02-12 22:55:34 -08006913 if (MLX5_ESWITCH_MANAGER(mdev) &&
Bodong Wangf6455de2019-06-28 22:36:15 +00006914 mlx5_ib_eswitch_mode(mdev->priv.eswitch) == MLX5_ESWITCH_OFFLOADS) {
Mark Bloch5fb58c92019-03-28 15:46:27 +02006915 if (!mlx5_core_mp_enabled(mdev))
6916 mlx5_ib_register_vport_reps(mdev);
Bodong Wangf0666f12019-02-12 22:55:34 -08006917 return mdev;
6918 }
6919
Daniel Jurgens32f69e42018-01-04 17:25:36 +02006920 port_type_cap = MLX5_CAP_GEN(mdev, port_type);
6921 ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
6922
Leon Romanovskye3f1ed12018-07-08 12:55:43 +03006923 if (mlx5_core_is_mp_slave(mdev) && ll == IB_LINK_LAYER_ETHERNET)
6924 return mlx5_ib_add_slave_port(mdev);
Daniel Jurgens32f69e42018-01-04 17:25:36 +02006925
Mark Blochda796cc2019-03-28 15:27:35 +02006926 num_ports = max(MLX5_CAP_GEN(mdev, num_ports),
6927 MLX5_CAP_GEN(mdev, num_vhca_ports));
Leon Romanovsky459cc692019-01-30 12:49:11 +02006928 dev = ib_alloc_device(mlx5_ib_dev, ib_dev);
Mark Blochb5ca15a2018-01-23 11:16:30 +00006929 if (!dev)
6930 return NULL;
Mark Blochda796cc2019-03-28 15:27:35 +02006931 dev->port = kcalloc(num_ports, sizeof(*dev->port),
6932 GFP_KERNEL);
6933 if (!dev->port) {
6934 ib_dealloc_device((struct ib_device *)dev);
6935 return NULL;
6936 }
Mark Blochb5ca15a2018-01-23 11:16:30 +00006937
6938 dev->mdev = mdev;
Mark Blochda796cc2019-03-28 15:27:35 +02006939 dev->num_ports = num_ports;
Mark Blochb5ca15a2018-01-23 11:16:30 +00006940
Mark Blochb5ca15a2018-01-23 11:16:30 +00006941 return __mlx5_ib_add(dev, &pf_profile);
Mark Bloch16c19752018-01-01 13:06:58 +02006942}
6943
Jack Morgenstein9603b612014-07-28 23:30:22 +03006944static void mlx5_ib_remove(struct mlx5_core_dev *mdev, void *context)
Eli Cohene126ba92013-07-07 17:25:49 +03006945{
Daniel Jurgens32f69e42018-01-04 17:25:36 +02006946 struct mlx5_ib_multiport_info *mpi;
6947 struct mlx5_ib_dev *dev;
Haggai Eran6aec21f2014-12-11 17:04:23 +02006948
Bodong Wangf0666f12019-02-12 22:55:34 -08006949 if (MLX5_ESWITCH_MANAGER(mdev) && context == mdev) {
6950 mlx5_ib_unregister_vport_reps(mdev);
6951 return;
6952 }
6953
Daniel Jurgens32f69e42018-01-04 17:25:36 +02006954 if (mlx5_core_is_mp_slave(mdev)) {
6955 mpi = context;
6956 mutex_lock(&mlx5_ib_multiport_mutex);
6957 if (mpi->ibdev)
6958 mlx5_ib_unbind_slave_port(mpi->ibdev, mpi);
6959 list_del(&mpi->list);
6960 mutex_unlock(&mlx5_ib_multiport_mutex);
6961 return;
6962 }
6963
6964 dev = context;
Bodong Wangf0666f12019-02-12 22:55:34 -08006965 __mlx5_ib_remove(dev, dev->profile, MLX5_IB_STAGE_MAX);
Eli Cohene126ba92013-07-07 17:25:49 +03006966}
6967
Jack Morgenstein9603b612014-07-28 23:30:22 +03006968static struct mlx5_interface mlx5_ib_interface = {
6969 .add = mlx5_ib_add,
6970 .remove = mlx5_ib_remove,
Saeed Mahameed64613d942015-04-02 17:07:34 +03006971 .protocol = MLX5_INTERFACE_PROTOCOL_IB,
Eli Cohene126ba92013-07-07 17:25:49 +03006972};
6973
Ilya Lesokhinc44ef992018-03-13 15:18:48 +02006974unsigned long mlx5_ib_get_xlt_emergency_page(void)
6975{
6976 mutex_lock(&xlt_emergency_page_mutex);
6977 return xlt_emergency_page;
6978}
6979
6980void mlx5_ib_put_xlt_emergency_page(void)
6981{
6982 mutex_unlock(&xlt_emergency_page_mutex);
6983}
6984
Eli Cohene126ba92013-07-07 17:25:49 +03006985static int __init mlx5_ib_init(void)
6986{
Haggai Eran6aec21f2014-12-11 17:04:23 +02006987 int err;
6988
Ilya Lesokhinc44ef992018-03-13 15:18:48 +02006989 xlt_emergency_page = __get_free_page(GFP_KERNEL);
6990 if (!xlt_emergency_page)
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02006991 return -ENOMEM;
6992
Ilya Lesokhinc44ef992018-03-13 15:18:48 +02006993 mutex_init(&xlt_emergency_page_mutex);
6994
6995 mlx5_ib_event_wq = alloc_ordered_workqueue("mlx5_ib_event_wq", 0);
6996 if (!mlx5_ib_event_wq) {
6997 free_page(xlt_emergency_page);
6998 return -ENOMEM;
6999 }
7000
Artemy Kovalyov81713d32017-01-18 16:58:11 +02007001 mlx5_ib_odp_init();
Jack Morgenstein9603b612014-07-28 23:30:22 +03007002
Haggai Eran6aec21f2014-12-11 17:04:23 +02007003 err = mlx5_register_interface(&mlx5_ib_interface);
Haggai Eran6aec21f2014-12-11 17:04:23 +02007004
7005 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03007006}
7007
7008static void __exit mlx5_ib_cleanup(void)
7009{
Jack Morgenstein9603b612014-07-28 23:30:22 +03007010 mlx5_unregister_interface(&mlx5_ib_interface);
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02007011 destroy_workqueue(mlx5_ib_event_wq);
Ilya Lesokhinc44ef992018-03-13 15:18:48 +02007012 mutex_destroy(&xlt_emergency_page_mutex);
7013 free_page(xlt_emergency_page);
Eli Cohene126ba92013-07-07 17:25:49 +03007014}
7015
7016module_init(mlx5_ib_init);
7017module_exit(mlx5_ib_cleanup);