blob: 2be6a43775581979169978c104f6480bc1d6be27 [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 Gottlieb7c2344c2016-06-17 14:56:44 +030055#include <linux/list.h>
Eli Cohene126ba92013-07-07 17:25:49 +030056#include <rdma/ib_smi.h>
57#include <rdma/ib_umem.h>
Maor Gottlieb038d2ef2016-01-11 10:26:07 +020058#include <linux/in.h>
59#include <linux/etherdevice.h>
Eli Cohene126ba92013-07-07 17:25:49 +030060#include "mlx5_ib.h"
Mark Blochfc385b72018-01-16 14:34:48 +000061#include "ib_rep.h"
Parav Pandite1f24a72017-04-16 07:29:29 +030062#include "cmd.h"
Boris Pismenny3346c482017-08-20 15:13:08 +030063#include <linux/mlx5/fs_helpers.h>
Aviad Yehezkelc6475a02018-03-28 09:27:50 +030064#include <linux/mlx5/accel.h>
Matan Barak8c846602018-03-28 09:27:41 +030065#include <rdma/uverbs_std_types.h>
Aviad Yehezkelc6475a02018-03-28 09:27:50 +030066#include <rdma/mlx5_user_ioctl_verbs.h>
67#include <rdma/mlx5_user_ioctl_cmds.h>
Matan Barak8c846602018-03-28 09:27:41 +030068
69#define UVERBS_MODULE_NAME mlx5_ib
70#include <rdma/uverbs_named_ioctl.h>
Eli Cohene126ba92013-07-07 17:25:49 +030071
72#define DRIVER_NAME "mlx5_ib"
Tariq Toukanb3599112017-02-22 17:45:46 +020073#define DRIVER_VERSION "5.0-0"
Eli Cohene126ba92013-07-07 17:25:49 +030074
75MODULE_AUTHOR("Eli Cohen <eli@mellanox.com>");
76MODULE_DESCRIPTION("Mellanox Connect-IB HCA IB driver");
77MODULE_LICENSE("Dual BSD/GPL");
Eli Cohene126ba92013-07-07 17:25:49 +030078
Eli Cohene126ba92013-07-07 17:25:49 +030079static char mlx5_version[] =
80 DRIVER_NAME ": Mellanox Connect-IB Infiniband driver v"
Tariq Toukanb3599112017-02-22 17:45:46 +020081 DRIVER_VERSION "\n";
Eli Cohene126ba92013-07-07 17:25:49 +030082
Daniel Jurgensd69a24e2018-01-04 17:25:37 +020083struct mlx5_ib_event_work {
84 struct work_struct work;
85 struct mlx5_core_dev *dev;
86 void *context;
87 enum mlx5_dev_event event;
88 unsigned long param;
89};
90
Eran Ben Elishada7525d2015-12-14 16:34:10 +020091enum {
92 MLX5_ATOMIC_SIZE_QP_8BYTES = 1 << 3,
93};
Majd Dibbiny1b5daf12015-06-04 19:30:46 +030094
Daniel Jurgensd69a24e2018-01-04 17:25:37 +020095static struct workqueue_struct *mlx5_ib_event_wq;
Daniel Jurgens32f69e42018-01-04 17:25:36 +020096static LIST_HEAD(mlx5_ib_unaffiliated_port_list);
97static LIST_HEAD(mlx5_ib_dev_list);
98/*
99 * This mutex should be held when accessing either of the above lists
100 */
101static DEFINE_MUTEX(mlx5_ib_multiport_mutex);
102
Ilya Lesokhinc44ef992018-03-13 15:18:48 +0200103/* We can't use an array for xlt_emergency_page because dma_map_single
104 * doesn't work on kernel modules memory
105 */
106static unsigned long xlt_emergency_page;
107static struct mutex xlt_emergency_page_mutex;
108
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200109struct mlx5_ib_dev *mlx5_ib_get_ibdev_from_mpi(struct mlx5_ib_multiport_info *mpi)
110{
111 struct mlx5_ib_dev *dev;
112
113 mutex_lock(&mlx5_ib_multiport_mutex);
114 dev = mpi->ibdev;
115 mutex_unlock(&mlx5_ib_multiport_mutex);
116 return dev;
117}
118
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300119static enum rdma_link_layer
Achiad Shochatebd61f62015-12-23 18:47:16 +0200120mlx5_port_type_cap_to_rdma_ll(int port_type_cap)
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300121{
Achiad Shochatebd61f62015-12-23 18:47:16 +0200122 switch (port_type_cap) {
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300123 case MLX5_CAP_PORT_TYPE_IB:
124 return IB_LINK_LAYER_INFINIBAND;
125 case MLX5_CAP_PORT_TYPE_ETH:
126 return IB_LINK_LAYER_ETHERNET;
127 default:
128 return IB_LINK_LAYER_UNSPECIFIED;
129 }
130}
131
Achiad Shochatebd61f62015-12-23 18:47:16 +0200132static enum rdma_link_layer
133mlx5_ib_port_link_layer(struct ib_device *device, u8 port_num)
134{
135 struct mlx5_ib_dev *dev = to_mdev(device);
136 int port_type_cap = MLX5_CAP_GEN(dev->mdev, port_type);
137
138 return mlx5_port_type_cap_to_rdma_ll(port_type_cap);
139}
140
Moni Shouafd65f1b2017-05-30 09:56:05 +0300141static int get_port_state(struct ib_device *ibdev,
142 u8 port_num,
143 enum ib_port_state *state)
144{
145 struct ib_port_attr attr;
146 int ret;
147
148 memset(&attr, 0, sizeof(attr));
Mark Bloch8e6efa32017-11-06 12:22:13 +0000149 ret = ibdev->query_port(ibdev, port_num, &attr);
Moni Shouafd65f1b2017-05-30 09:56:05 +0300150 if (!ret)
151 *state = attr.state;
152 return ret;
153}
154
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200155static int mlx5_netdev_event(struct notifier_block *this,
156 unsigned long event, void *ptr)
157{
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200158 struct mlx5_roce *roce = container_of(this, struct mlx5_roce, nb);
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200159 struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200160 u8 port_num = roce->native_port_num;
161 struct mlx5_core_dev *mdev;
162 struct mlx5_ib_dev *ibdev;
163
164 ibdev = roce->dev;
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200165 mdev = mlx5_ib_get_native_port_mdev(ibdev, port_num, NULL);
166 if (!mdev)
167 return NOTIFY_DONE;
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200168
Aviv Heller5ec8c832016-09-18 20:48:00 +0300169 switch (event) {
170 case NETDEV_REGISTER:
171 case NETDEV_UNREGISTER:
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200172 write_lock(&roce->netdev_lock);
Mark Blochbcf87f12018-01-16 15:02:36 +0000173 if (ibdev->rep) {
174 struct mlx5_eswitch *esw = ibdev->mdev->priv.eswitch;
175 struct net_device *rep_ndev;
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200176
Mark Blochbcf87f12018-01-16 15:02:36 +0000177 rep_ndev = mlx5_ib_get_rep_netdev(esw,
178 ibdev->rep->vport);
179 if (rep_ndev == ndev)
180 roce->netdev = (event == NETDEV_UNREGISTER) ?
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200181 NULL : ndev;
Parav Pandit84a6a7a2018-04-23 17:01:55 +0300182 } else if (ndev->dev.parent == &mdev->pdev->dev) {
Mark Blochbcf87f12018-01-16 15:02:36 +0000183 roce->netdev = (event == NETDEV_UNREGISTER) ?
184 NULL : ndev;
185 }
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200186 write_unlock(&roce->netdev_lock);
Aviv Heller5ec8c832016-09-18 20:48:00 +0300187 break;
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200188
Moni Shouafd65f1b2017-05-30 09:56:05 +0300189 case NETDEV_CHANGE:
Aviv Heller5ec8c832016-09-18 20:48:00 +0300190 case NETDEV_UP:
Aviv Heller88621df2016-09-18 20:48:02 +0300191 case NETDEV_DOWN: {
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200192 struct net_device *lag_ndev = mlx5_lag_get_roce_netdev(mdev);
Aviv Heller88621df2016-09-18 20:48:02 +0300193 struct net_device *upper = NULL;
194
195 if (lag_ndev) {
196 upper = netdev_master_upper_dev_get(lag_ndev);
197 dev_put(lag_ndev);
198 }
199
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200200 if ((upper == ndev || (!upper && ndev == roce->netdev))
Aviv Heller88621df2016-09-18 20:48:02 +0300201 && ibdev->ib_active) {
Bart Van Assche626bc022016-12-05 17:18:08 -0800202 struct ib_event ibev = { };
Moni Shouafd65f1b2017-05-30 09:56:05 +0300203 enum ib_port_state port_state;
Aviv Heller5ec8c832016-09-18 20:48:00 +0300204
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200205 if (get_port_state(&ibdev->ib_dev, port_num,
206 &port_state))
207 goto done;
Moni Shouafd65f1b2017-05-30 09:56:05 +0300208
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200209 if (roce->last_port_state == port_state)
210 goto done;
Moni Shouafd65f1b2017-05-30 09:56:05 +0300211
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200212 roce->last_port_state = port_state;
Aviv Heller5ec8c832016-09-18 20:48:00 +0300213 ibev.device = &ibdev->ib_dev;
Moni Shouafd65f1b2017-05-30 09:56:05 +0300214 if (port_state == IB_PORT_DOWN)
215 ibev.event = IB_EVENT_PORT_ERR;
216 else if (port_state == IB_PORT_ACTIVE)
217 ibev.event = IB_EVENT_PORT_ACTIVE;
218 else
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200219 goto done;
Moni Shouafd65f1b2017-05-30 09:56:05 +0300220
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200221 ibev.element.port_num = port_num;
Aviv Heller5ec8c832016-09-18 20:48:00 +0300222 ib_dispatch_event(&ibev);
223 }
224 break;
Aviv Heller88621df2016-09-18 20:48:02 +0300225 }
Aviv Heller5ec8c832016-09-18 20:48:00 +0300226
227 default:
228 break;
229 }
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200230done:
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200231 mlx5_ib_put_native_port_mdev(ibdev, port_num);
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200232 return NOTIFY_DONE;
233}
234
235static struct net_device *mlx5_ib_get_netdev(struct ib_device *device,
236 u8 port_num)
237{
238 struct mlx5_ib_dev *ibdev = to_mdev(device);
239 struct net_device *ndev;
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200240 struct mlx5_core_dev *mdev;
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200241
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200242 mdev = mlx5_ib_get_native_port_mdev(ibdev, port_num, NULL);
243 if (!mdev)
244 return NULL;
245
246 ndev = mlx5_lag_get_roce_netdev(mdev);
Aviv Heller88621df2016-09-18 20:48:02 +0300247 if (ndev)
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200248 goto out;
Aviv Heller88621df2016-09-18 20:48:02 +0300249
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200250 /* Ensure ndev does not disappear before we invoke dev_hold()
251 */
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200252 read_lock(&ibdev->roce[port_num - 1].netdev_lock);
253 ndev = ibdev->roce[port_num - 1].netdev;
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200254 if (ndev)
255 dev_hold(ndev);
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200256 read_unlock(&ibdev->roce[port_num - 1].netdev_lock);
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200257
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200258out:
259 mlx5_ib_put_native_port_mdev(ibdev, port_num);
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200260 return ndev;
261}
262
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200263struct mlx5_core_dev *mlx5_ib_get_native_port_mdev(struct mlx5_ib_dev *ibdev,
264 u8 ib_port_num,
265 u8 *native_port_num)
266{
267 enum rdma_link_layer ll = mlx5_ib_port_link_layer(&ibdev->ib_dev,
268 ib_port_num);
269 struct mlx5_core_dev *mdev = NULL;
270 struct mlx5_ib_multiport_info *mpi;
271 struct mlx5_ib_port *port;
272
Mark Bloch210b1f72018-03-05 20:09:47 +0200273 if (!mlx5_core_mp_enabled(ibdev->mdev) ||
274 ll != IB_LINK_LAYER_ETHERNET) {
275 if (native_port_num)
276 *native_port_num = ib_port_num;
277 return ibdev->mdev;
278 }
279
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200280 if (native_port_num)
281 *native_port_num = 1;
282
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200283 port = &ibdev->port[ib_port_num - 1];
284 if (!port)
285 return NULL;
286
287 spin_lock(&port->mp.mpi_lock);
288 mpi = ibdev->port[ib_port_num - 1].mp.mpi;
289 if (mpi && !mpi->unaffiliate) {
290 mdev = mpi->mdev;
291 /* If it's the master no need to refcount, it'll exist
292 * as long as the ib_dev exists.
293 */
294 if (!mpi->is_master)
295 mpi->mdev_refcnt++;
296 }
297 spin_unlock(&port->mp.mpi_lock);
298
299 return mdev;
300}
301
302void mlx5_ib_put_native_port_mdev(struct mlx5_ib_dev *ibdev, u8 port_num)
303{
304 enum rdma_link_layer ll = mlx5_ib_port_link_layer(&ibdev->ib_dev,
305 port_num);
306 struct mlx5_ib_multiport_info *mpi;
307 struct mlx5_ib_port *port;
308
309 if (!mlx5_core_mp_enabled(ibdev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
310 return;
311
312 port = &ibdev->port[port_num - 1];
313
314 spin_lock(&port->mp.mpi_lock);
315 mpi = ibdev->port[port_num - 1].mp.mpi;
316 if (mpi->is_master)
317 goto out;
318
319 mpi->mdev_refcnt--;
320 if (mpi->unaffiliate)
321 complete(&mpi->unref_comp);
322out:
323 spin_unlock(&port->mp.mpi_lock);
324}
325
Noa Osherovichf1b65df2017-04-20 20:53:33 +0300326static int translate_eth_proto_oper(u32 eth_proto_oper, u8 *active_speed,
327 u8 *active_width)
328{
329 switch (eth_proto_oper) {
330 case MLX5E_PROT_MASK(MLX5E_1000BASE_CX_SGMII):
331 case MLX5E_PROT_MASK(MLX5E_1000BASE_KX):
332 case MLX5E_PROT_MASK(MLX5E_100BASE_TX):
333 case MLX5E_PROT_MASK(MLX5E_1000BASE_T):
334 *active_width = IB_WIDTH_1X;
335 *active_speed = IB_SPEED_SDR;
336 break;
337 case MLX5E_PROT_MASK(MLX5E_10GBASE_T):
338 case MLX5E_PROT_MASK(MLX5E_10GBASE_CX4):
339 case MLX5E_PROT_MASK(MLX5E_10GBASE_KX4):
340 case MLX5E_PROT_MASK(MLX5E_10GBASE_KR):
341 case MLX5E_PROT_MASK(MLX5E_10GBASE_CR):
342 case MLX5E_PROT_MASK(MLX5E_10GBASE_SR):
343 case MLX5E_PROT_MASK(MLX5E_10GBASE_ER):
344 *active_width = IB_WIDTH_1X;
345 *active_speed = IB_SPEED_QDR;
346 break;
347 case MLX5E_PROT_MASK(MLX5E_25GBASE_CR):
348 case MLX5E_PROT_MASK(MLX5E_25GBASE_KR):
349 case MLX5E_PROT_MASK(MLX5E_25GBASE_SR):
350 *active_width = IB_WIDTH_1X;
351 *active_speed = IB_SPEED_EDR;
352 break;
353 case MLX5E_PROT_MASK(MLX5E_40GBASE_CR4):
354 case MLX5E_PROT_MASK(MLX5E_40GBASE_KR4):
355 case MLX5E_PROT_MASK(MLX5E_40GBASE_SR4):
356 case MLX5E_PROT_MASK(MLX5E_40GBASE_LR4):
357 *active_width = IB_WIDTH_4X;
358 *active_speed = IB_SPEED_QDR;
359 break;
360 case MLX5E_PROT_MASK(MLX5E_50GBASE_CR2):
361 case MLX5E_PROT_MASK(MLX5E_50GBASE_KR2):
362 case MLX5E_PROT_MASK(MLX5E_50GBASE_SR2):
363 *active_width = IB_WIDTH_1X;
364 *active_speed = IB_SPEED_HDR;
365 break;
366 case MLX5E_PROT_MASK(MLX5E_56GBASE_R4):
367 *active_width = IB_WIDTH_4X;
368 *active_speed = IB_SPEED_FDR;
369 break;
370 case MLX5E_PROT_MASK(MLX5E_100GBASE_CR4):
371 case MLX5E_PROT_MASK(MLX5E_100GBASE_SR4):
372 case MLX5E_PROT_MASK(MLX5E_100GBASE_KR4):
373 case MLX5E_PROT_MASK(MLX5E_100GBASE_LR4):
374 *active_width = IB_WIDTH_4X;
375 *active_speed = IB_SPEED_EDR;
376 break;
377 default:
378 return -EINVAL;
379 }
380
381 return 0;
382}
383
Ilan Tayari095b0922017-05-14 16:04:30 +0300384static int mlx5_query_port_roce(struct ib_device *device, u8 port_num,
385 struct ib_port_attr *props)
Achiad Shochat3f89a642015-12-23 18:47:21 +0200386{
387 struct mlx5_ib_dev *dev = to_mdev(device);
Colin Ian Kingda005f92018-01-09 15:55:43 +0000388 struct mlx5_core_dev *mdev;
Aviv Heller88621df2016-09-18 20:48:02 +0300389 struct net_device *ndev, *upper;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200390 enum ib_mtu ndev_ib_mtu;
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200391 bool put_mdev = true;
Leon Romanovskyc876a1b2016-01-09 13:06:25 +0200392 u16 qkey_viol_cntr;
Noa Osherovichf1b65df2017-04-20 20:53:33 +0300393 u32 eth_prot_oper;
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200394 u8 mdev_port_num;
Ilan Tayari095b0922017-05-14 16:04:30 +0300395 int err;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200396
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200397 mdev = mlx5_ib_get_native_port_mdev(dev, port_num, &mdev_port_num);
398 if (!mdev) {
399 /* This means the port isn't affiliated yet. Get the
400 * info for the master port instead.
401 */
402 put_mdev = false;
403 mdev = dev->mdev;
404 mdev_port_num = 1;
405 port_num = 1;
406 }
407
Noa Osherovichf1b65df2017-04-20 20:53:33 +0300408 /* Possible bad flows are checked before filling out props so in case
409 * of an error it will still be zeroed out.
Noa Osherovich50f22fd2017-04-20 20:53:32 +0300410 */
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200411 err = mlx5_query_port_eth_proto_oper(mdev, &eth_prot_oper,
412 mdev_port_num);
Ilan Tayari095b0922017-05-14 16:04:30 +0300413 if (err)
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200414 goto out;
Noa Osherovichf1b65df2017-04-20 20:53:33 +0300415
Honggang Li7672ed32018-03-16 10:37:13 +0800416 props->active_width = IB_WIDTH_4X;
417 props->active_speed = IB_SPEED_QDR;
418
Noa Osherovichf1b65df2017-04-20 20:53:33 +0300419 translate_eth_proto_oper(eth_prot_oper, &props->active_speed,
420 &props->active_width);
Achiad Shochat3f89a642015-12-23 18:47:21 +0200421
Jason Gunthorpe2f944c02018-07-04 15:57:48 +0300422 props->port_cap_flags |= IB_PORT_CM_SUP;
423 props->ip_gids = true;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200424
425 props->gid_tbl_len = MLX5_CAP_ROCE(dev->mdev,
426 roce_address_table_size);
427 props->max_mtu = IB_MTU_4096;
428 props->max_msg_sz = 1 << MLX5_CAP_GEN(dev->mdev, log_max_msg);
429 props->pkey_tbl_len = 1;
430 props->state = IB_PORT_DOWN;
431 props->phys_state = 3;
432
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200433 mlx5_query_nic_vport_qkey_viol_cntr(mdev, &qkey_viol_cntr);
Leon Romanovskyc876a1b2016-01-09 13:06:25 +0200434 props->qkey_viol_cntr = qkey_viol_cntr;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200435
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200436 /* If this is a stub query for an unaffiliated port stop here */
437 if (!put_mdev)
438 goto out;
439
Achiad Shochat3f89a642015-12-23 18:47:21 +0200440 ndev = mlx5_ib_get_netdev(device, port_num);
441 if (!ndev)
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200442 goto out;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200443
Aviv Heller88621df2016-09-18 20:48:02 +0300444 if (mlx5_lag_is_active(dev->mdev)) {
445 rcu_read_lock();
446 upper = netdev_master_upper_dev_get_rcu(ndev);
447 if (upper) {
448 dev_put(ndev);
449 ndev = upper;
450 dev_hold(ndev);
451 }
452 rcu_read_unlock();
453 }
454
Achiad Shochat3f89a642015-12-23 18:47:21 +0200455 if (netif_running(ndev) && netif_carrier_ok(ndev)) {
456 props->state = IB_PORT_ACTIVE;
457 props->phys_state = 5;
458 }
459
460 ndev_ib_mtu = iboe_get_mtu(ndev->mtu);
461
462 dev_put(ndev);
463
464 props->active_mtu = min(props->max_mtu, ndev_ib_mtu);
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200465out:
466 if (put_mdev)
467 mlx5_ib_put_native_port_mdev(dev, port_num);
468 return err;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200469}
470
Ilan Tayari095b0922017-05-14 16:04:30 +0300471static int set_roce_addr(struct mlx5_ib_dev *dev, u8 port_num,
472 unsigned int index, const union ib_gid *gid,
473 const struct ib_gid_attr *attr)
Achiad Shochat3cca2602015-12-23 18:47:23 +0200474{
Ilan Tayari095b0922017-05-14 16:04:30 +0300475 enum ib_gid_type gid_type = IB_GID_TYPE_IB;
476 u8 roce_version = 0;
477 u8 roce_l3_type = 0;
478 bool vlan = false;
479 u8 mac[ETH_ALEN];
480 u16 vlan_id = 0;
Achiad Shochat3cca2602015-12-23 18:47:23 +0200481
Ilan Tayari095b0922017-05-14 16:04:30 +0300482 if (gid) {
483 gid_type = attr->gid_type;
484 ether_addr_copy(mac, attr->ndev->dev_addr);
Achiad Shochat3cca2602015-12-23 18:47:23 +0200485
Ilan Tayari095b0922017-05-14 16:04:30 +0300486 if (is_vlan_dev(attr->ndev)) {
487 vlan = true;
488 vlan_id = vlan_dev_vlan_id(attr->ndev);
489 }
Achiad Shochat3cca2602015-12-23 18:47:23 +0200490 }
491
Ilan Tayari095b0922017-05-14 16:04:30 +0300492 switch (gid_type) {
Achiad Shochat3cca2602015-12-23 18:47:23 +0200493 case IB_GID_TYPE_IB:
Ilan Tayari095b0922017-05-14 16:04:30 +0300494 roce_version = MLX5_ROCE_VERSION_1;
Achiad Shochat3cca2602015-12-23 18:47:23 +0200495 break;
496 case IB_GID_TYPE_ROCE_UDP_ENCAP:
Ilan Tayari095b0922017-05-14 16:04:30 +0300497 roce_version = MLX5_ROCE_VERSION_2;
498 if (ipv6_addr_v4mapped((void *)gid))
499 roce_l3_type = MLX5_ROCE_L3_TYPE_IPV4;
500 else
501 roce_l3_type = MLX5_ROCE_L3_TYPE_IPV6;
Achiad Shochat3cca2602015-12-23 18:47:23 +0200502 break;
503
504 default:
Ilan Tayari095b0922017-05-14 16:04:30 +0300505 mlx5_ib_warn(dev, "Unexpected GID type %u\n", gid_type);
Achiad Shochat3cca2602015-12-23 18:47:23 +0200506 }
507
Ilan Tayari095b0922017-05-14 16:04:30 +0300508 return mlx5_core_roce_gid_set(dev->mdev, index, roce_version,
509 roce_l3_type, gid->raw, mac, vlan,
Daniel Jurgenscfe4e372018-01-04 17:25:41 +0200510 vlan_id, port_num);
Achiad Shochat3cca2602015-12-23 18:47:23 +0200511}
512
Parav Panditf4df9a72018-06-05 08:40:16 +0300513static int mlx5_ib_add_gid(const struct ib_gid_attr *attr,
Achiad Shochat3cca2602015-12-23 18:47:23 +0200514 __always_unused void **context)
515{
Parav Pandit414448d2018-04-01 15:08:24 +0300516 return set_roce_addr(to_mdev(attr->device), attr->port_num,
Parav Panditf4df9a72018-06-05 08:40:16 +0300517 attr->index, &attr->gid, attr);
Achiad Shochat3cca2602015-12-23 18:47:23 +0200518}
519
Parav Pandit414448d2018-04-01 15:08:24 +0300520static int mlx5_ib_del_gid(const struct ib_gid_attr *attr,
521 __always_unused void **context)
Achiad Shochat3cca2602015-12-23 18:47:23 +0200522{
Parav Pandit414448d2018-04-01 15:08:24 +0300523 return set_roce_addr(to_mdev(attr->device), attr->port_num,
524 attr->index, NULL, NULL);
Achiad Shochat3cca2602015-12-23 18:47:23 +0200525}
526
Parav Pandit47ec3862018-06-13 10:22:06 +0300527__be16 mlx5_get_roce_udp_sport(struct mlx5_ib_dev *dev,
528 const struct ib_gid_attr *attr)
Achiad Shochat2811ba52015-12-23 18:47:24 +0200529{
Parav Pandit47ec3862018-06-13 10:22:06 +0300530 if (attr->gid_type != IB_GID_TYPE_ROCE_UDP_ENCAP)
Achiad Shochat2811ba52015-12-23 18:47:24 +0200531 return 0;
532
533 return cpu_to_be16(MLX5_CAP_ROCE(dev->mdev, r_roce_min_src_udp_port));
534}
535
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300536static int mlx5_use_mad_ifc(struct mlx5_ib_dev *dev)
537{
Noa Osherovich7fae6652016-09-12 19:16:23 +0300538 if (MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_IB)
539 return !MLX5_CAP_GEN(dev->mdev, ib_virt);
540 return 0;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300541}
542
543enum {
544 MLX5_VPORT_ACCESS_METHOD_MAD,
545 MLX5_VPORT_ACCESS_METHOD_HCA,
546 MLX5_VPORT_ACCESS_METHOD_NIC,
547};
548
549static int mlx5_get_vport_access_method(struct ib_device *ibdev)
550{
551 if (mlx5_use_mad_ifc(to_mdev(ibdev)))
552 return MLX5_VPORT_ACCESS_METHOD_MAD;
553
Achiad Shochatebd61f62015-12-23 18:47:16 +0200554 if (mlx5_ib_port_link_layer(ibdev, 1) ==
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300555 IB_LINK_LAYER_ETHERNET)
556 return MLX5_VPORT_ACCESS_METHOD_NIC;
557
558 return MLX5_VPORT_ACCESS_METHOD_HCA;
559}
560
Eran Ben Elishada7525d2015-12-14 16:34:10 +0200561static void get_atomic_caps(struct mlx5_ib_dev *dev,
Moni Shoua776a3902018-01-02 16:19:33 +0200562 u8 atomic_size_qp,
Eran Ben Elishada7525d2015-12-14 16:34:10 +0200563 struct ib_device_attr *props)
564{
565 u8 tmp;
566 u8 atomic_operations = MLX5_CAP_ATOMIC(dev->mdev, atomic_operations);
Eran Ben Elishada7525d2015-12-14 16:34:10 +0200567 u8 atomic_req_8B_endianness_mode =
Or Gerlitzbd108382017-05-28 15:24:17 +0300568 MLX5_CAP_ATOMIC(dev->mdev, atomic_req_8B_endianness_mode);
Eran Ben Elishada7525d2015-12-14 16:34:10 +0200569
570 /* Check if HW supports 8 bytes standard atomic operations and capable
571 * of host endianness respond
572 */
573 tmp = MLX5_ATOMIC_OPS_CMP_SWAP | MLX5_ATOMIC_OPS_FETCH_ADD;
574 if (((atomic_operations & tmp) == tmp) &&
575 (atomic_size_qp & MLX5_ATOMIC_SIZE_QP_8BYTES) &&
576 (atomic_req_8B_endianness_mode)) {
577 props->atomic_cap = IB_ATOMIC_HCA;
578 } else {
579 props->atomic_cap = IB_ATOMIC_NONE;
580 }
581}
582
Moni Shoua776a3902018-01-02 16:19:33 +0200583static void get_atomic_caps_qp(struct mlx5_ib_dev *dev,
584 struct ib_device_attr *props)
585{
586 u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_qp);
587
588 get_atomic_caps(dev, atomic_size_qp, props);
589}
590
591static void get_atomic_caps_dc(struct mlx5_ib_dev *dev,
592 struct ib_device_attr *props)
593{
594 u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_dc);
595
596 get_atomic_caps(dev, atomic_size_qp, props);
597}
598
599bool mlx5_ib_dc_atomic_is_supported(struct mlx5_ib_dev *dev)
600{
601 struct ib_device_attr props = {};
602
603 get_atomic_caps_dc(dev, &props);
604 return (props.atomic_cap == IB_ATOMIC_HCA) ? true : false;
605}
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300606static int mlx5_query_system_image_guid(struct ib_device *ibdev,
607 __be64 *sys_image_guid)
608{
609 struct mlx5_ib_dev *dev = to_mdev(ibdev);
610 struct mlx5_core_dev *mdev = dev->mdev;
611 u64 tmp;
612 int err;
613
614 switch (mlx5_get_vport_access_method(ibdev)) {
615 case MLX5_VPORT_ACCESS_METHOD_MAD:
616 return mlx5_query_mad_ifc_system_image_guid(ibdev,
617 sys_image_guid);
618
619 case MLX5_VPORT_ACCESS_METHOD_HCA:
620 err = mlx5_query_hca_vport_system_image_guid(mdev, &tmp);
Achiad Shochat3f89a642015-12-23 18:47:21 +0200621 break;
622
623 case MLX5_VPORT_ACCESS_METHOD_NIC:
624 err = mlx5_query_nic_vport_system_image_guid(mdev, &tmp);
625 break;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300626
627 default:
628 return -EINVAL;
629 }
Achiad Shochat3f89a642015-12-23 18:47:21 +0200630
631 if (!err)
632 *sys_image_guid = cpu_to_be64(tmp);
633
634 return err;
635
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300636}
637
638static int mlx5_query_max_pkeys(struct ib_device *ibdev,
639 u16 *max_pkeys)
640{
641 struct mlx5_ib_dev *dev = to_mdev(ibdev);
642 struct mlx5_core_dev *mdev = dev->mdev;
643
644 switch (mlx5_get_vport_access_method(ibdev)) {
645 case MLX5_VPORT_ACCESS_METHOD_MAD:
646 return mlx5_query_mad_ifc_max_pkeys(ibdev, max_pkeys);
647
648 case MLX5_VPORT_ACCESS_METHOD_HCA:
649 case MLX5_VPORT_ACCESS_METHOD_NIC:
650 *max_pkeys = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev,
651 pkey_table_size));
652 return 0;
653
654 default:
655 return -EINVAL;
656 }
657}
658
659static int mlx5_query_vendor_id(struct ib_device *ibdev,
660 u32 *vendor_id)
661{
662 struct mlx5_ib_dev *dev = to_mdev(ibdev);
663
664 switch (mlx5_get_vport_access_method(ibdev)) {
665 case MLX5_VPORT_ACCESS_METHOD_MAD:
666 return mlx5_query_mad_ifc_vendor_id(ibdev, vendor_id);
667
668 case MLX5_VPORT_ACCESS_METHOD_HCA:
669 case MLX5_VPORT_ACCESS_METHOD_NIC:
670 return mlx5_core_query_vendor_id(dev->mdev, vendor_id);
671
672 default:
673 return -EINVAL;
674 }
675}
676
677static int mlx5_query_node_guid(struct mlx5_ib_dev *dev,
678 __be64 *node_guid)
679{
680 u64 tmp;
681 int err;
682
683 switch (mlx5_get_vport_access_method(&dev->ib_dev)) {
684 case MLX5_VPORT_ACCESS_METHOD_MAD:
685 return mlx5_query_mad_ifc_node_guid(dev, node_guid);
686
687 case MLX5_VPORT_ACCESS_METHOD_HCA:
688 err = mlx5_query_hca_vport_node_guid(dev->mdev, &tmp);
Achiad Shochat3f89a642015-12-23 18:47:21 +0200689 break;
690
691 case MLX5_VPORT_ACCESS_METHOD_NIC:
692 err = mlx5_query_nic_vport_node_guid(dev->mdev, &tmp);
693 break;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300694
695 default:
696 return -EINVAL;
697 }
Achiad Shochat3f89a642015-12-23 18:47:21 +0200698
699 if (!err)
700 *node_guid = cpu_to_be64(tmp);
701
702 return err;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300703}
704
705struct mlx5_reg_node_desc {
Yuval Shaiabd99fde2016-08-25 10:57:07 -0700706 u8 desc[IB_DEVICE_NODE_DESC_MAX];
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300707};
708
709static int mlx5_query_node_desc(struct mlx5_ib_dev *dev, char *node_desc)
710{
711 struct mlx5_reg_node_desc in;
712
713 if (mlx5_use_mad_ifc(dev))
714 return mlx5_query_mad_ifc_node_desc(dev, node_desc);
715
716 memset(&in, 0, sizeof(in));
717
718 return mlx5_core_access_reg(dev->mdev, &in, sizeof(in), node_desc,
719 sizeof(struct mlx5_reg_node_desc),
720 MLX5_REG_NODE_DESC, 0, 0);
721}
722
Eli Cohene126ba92013-07-07 17:25:49 +0300723static int mlx5_ib_query_device(struct ib_device *ibdev,
Matan Barak2528e332015-06-11 16:35:25 +0300724 struct ib_device_attr *props,
725 struct ib_udata *uhw)
Eli Cohene126ba92013-07-07 17:25:49 +0300726{
727 struct mlx5_ib_dev *dev = to_mdev(ibdev);
Saeed Mahameed938fe832015-05-28 22:28:41 +0300728 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +0300729 int err = -ENOMEM;
Eli Cohen288c01b2016-10-27 16:36:45 +0300730 int max_sq_desc;
Eli Cohene126ba92013-07-07 17:25:49 +0300731 int max_rq_sg;
732 int max_sq_sg;
Sagi Grimberge0238a62015-07-21 14:40:12 +0300733 u64 min_page_size = 1ull << MLX5_CAP_GEN(mdev, log_pg_sz);
Daniel Jurgens85c7c012018-01-04 17:25:43 +0200734 bool raw_support = !mlx5_core_mp_enabled(mdev);
Bodong Wang402ca532016-06-17 15:02:20 +0300735 struct mlx5_ib_query_device_resp resp = {};
736 size_t resp_len;
737 u64 max_tso;
Eli Cohene126ba92013-07-07 17:25:49 +0300738
Bodong Wang402ca532016-06-17 15:02:20 +0300739 resp_len = sizeof(resp.comp_mask) + sizeof(resp.response_length);
740 if (uhw->outlen && uhw->outlen < resp_len)
741 return -EINVAL;
742 else
743 resp.response_length = resp_len;
744
745 if (uhw->inlen && !ib_is_udata_cleared(uhw, 0, uhw->inlen))
Matan Barak2528e332015-06-11 16:35:25 +0300746 return -EINVAL;
747
Eli Cohene126ba92013-07-07 17:25:49 +0300748 memset(props, 0, sizeof(*props));
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300749 err = mlx5_query_system_image_guid(ibdev,
750 &props->sys_image_guid);
751 if (err)
752 return err;
753
754 err = mlx5_query_max_pkeys(ibdev, &props->max_pkeys);
755 if (err)
756 return err;
757
758 err = mlx5_query_vendor_id(ibdev, &props->vendor_id);
759 if (err)
760 return err;
Eli Cohene126ba92013-07-07 17:25:49 +0300761
Jack Morgenstein9603b612014-07-28 23:30:22 +0300762 props->fw_ver = ((u64)fw_rev_maj(dev->mdev) << 32) |
763 (fw_rev_min(dev->mdev) << 16) |
764 fw_rev_sub(dev->mdev);
Eli Cohene126ba92013-07-07 17:25:49 +0300765 props->device_cap_flags = IB_DEVICE_CHANGE_PHY_PORT |
766 IB_DEVICE_PORT_ACTIVE_EVENT |
767 IB_DEVICE_SYS_IMAGE_GUID |
Eli Cohen1a4c3a32014-02-06 17:41:25 +0200768 IB_DEVICE_RC_RNR_NAK_GEN;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300769
770 if (MLX5_CAP_GEN(mdev, pkv))
Eli Cohene126ba92013-07-07 17:25:49 +0300771 props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300772 if (MLX5_CAP_GEN(mdev, qkv))
Eli Cohene126ba92013-07-07 17:25:49 +0300773 props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300774 if (MLX5_CAP_GEN(mdev, apm))
Eli Cohene126ba92013-07-07 17:25:49 +0300775 props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300776 if (MLX5_CAP_GEN(mdev, xrc))
Eli Cohene126ba92013-07-07 17:25:49 +0300777 props->device_cap_flags |= IB_DEVICE_XRC;
Matan Barakd2370e02016-02-29 18:05:30 +0200778 if (MLX5_CAP_GEN(mdev, imaicl)) {
779 props->device_cap_flags |= IB_DEVICE_MEM_WINDOW |
780 IB_DEVICE_MEM_WINDOW_TYPE_2B;
781 props->max_mw = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
Sagi Grimbergb005d312016-02-29 19:07:33 +0200782 /* We support 'Gappy' memory registration too */
783 props->device_cap_flags |= IB_DEVICE_SG_GAPS_REG;
Matan Barakd2370e02016-02-29 18:05:30 +0200784 }
Eli Cohene126ba92013-07-07 17:25:49 +0300785 props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300786 if (MLX5_CAP_GEN(mdev, sho)) {
Sagi Grimberg2dea9092014-02-23 14:19:13 +0200787 props->device_cap_flags |= IB_DEVICE_SIGNATURE_HANDOVER;
788 /* At this stage no support for signature handover */
789 props->sig_prot_cap = IB_PROT_T10DIF_TYPE_1 |
790 IB_PROT_T10DIF_TYPE_2 |
791 IB_PROT_T10DIF_TYPE_3;
792 props->sig_guard_cap = IB_GUARD_T10DIF_CRC |
793 IB_GUARD_T10DIF_CSUM;
794 }
Saeed Mahameed938fe832015-05-28 22:28:41 +0300795 if (MLX5_CAP_GEN(mdev, block_lb_mc))
Eli Cohenf360d882014-04-02 00:10:16 +0300796 props->device_cap_flags |= IB_DEVICE_BLOCK_MULTICAST_LOOPBACK;
Eli Cohene126ba92013-07-07 17:25:49 +0300797
Daniel Jurgens85c7c012018-01-04 17:25:43 +0200798 if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) && raw_support) {
Noa Osheroviche8161332017-01-18 15:40:01 +0200799 if (MLX5_CAP_ETH(mdev, csum_cap)) {
800 /* Legacy bit to support old userspace libraries */
Bodong Wang88115fe2015-12-18 13:53:20 +0200801 props->device_cap_flags |= IB_DEVICE_RAW_IP_CSUM;
Noa Osheroviche8161332017-01-18 15:40:01 +0200802 props->raw_packet_caps |= IB_RAW_PACKET_CAP_IP_CSUM;
803 }
804
805 if (MLX5_CAP_ETH(dev->mdev, vlan_cap))
806 props->raw_packet_caps |=
807 IB_RAW_PACKET_CAP_CVLAN_STRIPPING;
Bodong Wang88115fe2015-12-18 13:53:20 +0200808
Bodong Wang402ca532016-06-17 15:02:20 +0300809 if (field_avail(typeof(resp), tso_caps, uhw->outlen)) {
810 max_tso = MLX5_CAP_ETH(mdev, max_lso_cap);
811 if (max_tso) {
812 resp.tso_caps.max_tso = 1 << max_tso;
813 resp.tso_caps.supported_qpts |=
814 1 << IB_QPT_RAW_PACKET;
815 resp.response_length += sizeof(resp.tso_caps);
816 }
817 }
Yishai Hadas31f69a82016-08-28 11:28:45 +0300818
819 if (field_avail(typeof(resp), rss_caps, uhw->outlen)) {
820 resp.rss_caps.rx_hash_function =
821 MLX5_RX_HASH_FUNC_TOEPLITZ;
822 resp.rss_caps.rx_hash_fields_mask =
823 MLX5_RX_HASH_SRC_IPV4 |
824 MLX5_RX_HASH_DST_IPV4 |
825 MLX5_RX_HASH_SRC_IPV6 |
826 MLX5_RX_HASH_DST_IPV6 |
827 MLX5_RX_HASH_SRC_PORT_TCP |
828 MLX5_RX_HASH_DST_PORT_TCP |
829 MLX5_RX_HASH_SRC_PORT_UDP |
Maor Gottlieb4e2b53a2017-12-24 14:51:25 +0200830 MLX5_RX_HASH_DST_PORT_UDP |
831 MLX5_RX_HASH_INNER;
Matan Barak2d93fc82018-03-28 09:27:55 +0300832 if (mlx5_accel_ipsec_device_caps(dev->mdev) &
833 MLX5_ACCEL_IPSEC_CAP_DEVICE)
834 resp.rss_caps.rx_hash_fields_mask |=
835 MLX5_RX_HASH_IPSEC_SPI;
Yishai Hadas31f69a82016-08-28 11:28:45 +0300836 resp.response_length += sizeof(resp.rss_caps);
837 }
838 } else {
839 if (field_avail(typeof(resp), tso_caps, uhw->outlen))
840 resp.response_length += sizeof(resp.tso_caps);
841 if (field_avail(typeof(resp), rss_caps, uhw->outlen))
842 resp.response_length += sizeof(resp.rss_caps);
Bodong Wang402ca532016-06-17 15:02:20 +0300843 }
844
Erez Shitritf0313962016-02-21 16:27:17 +0200845 if (MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) {
846 props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
847 props->device_cap_flags |= IB_DEVICE_UD_TSO;
848 }
849
Maor Gottlieb03404e82017-05-30 10:29:13 +0300850 if (MLX5_CAP_GEN(dev->mdev, rq_delay_drop) &&
Daniel Jurgens85c7c012018-01-04 17:25:43 +0200851 MLX5_CAP_GEN(dev->mdev, general_notification_event) &&
852 raw_support)
Maor Gottlieb03404e82017-05-30 10:29:13 +0300853 props->raw_packet_caps |= IB_RAW_PACKET_CAP_DELAY_DROP;
854
Yishai Hadas1d54f892017-06-08 16:15:11 +0300855 if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads) &&
856 MLX5_CAP_IPOIB_ENHANCED(mdev, csum_cap))
857 props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
858
Majd Dibbinycff5a0f2016-04-17 17:19:38 +0300859 if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
Daniel Jurgens85c7c012018-01-04 17:25:43 +0200860 MLX5_CAP_ETH(dev->mdev, scatter_fcs) &&
861 raw_support) {
Noa Osheroviche8161332017-01-18 15:40:01 +0200862 /* Legacy bit to support old userspace libraries */
Majd Dibbinycff5a0f2016-04-17 17:19:38 +0300863 props->device_cap_flags |= IB_DEVICE_RAW_SCATTER_FCS;
Noa Osheroviche8161332017-01-18 15:40:01 +0200864 props->raw_packet_caps |= IB_RAW_PACKET_CAP_SCATTER_FCS;
865 }
Majd Dibbinycff5a0f2016-04-17 17:19:38 +0300866
Ariel Levkovich24da0012018-04-05 18:53:27 +0300867 if (MLX5_CAP_DEV_MEM(mdev, memic)) {
868 props->max_dm_size =
869 MLX5_CAP_DEV_MEM(mdev, max_memic_size);
870 }
871
Maor Gottliebda6d6ba32016-06-04 15:15:28 +0300872 if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS))
873 props->device_cap_flags |= IB_DEVICE_MANAGED_FLOW_STEERING;
874
Noa Osherovichb1383aa2017-10-29 13:59:45 +0200875 if (MLX5_CAP_GEN(mdev, end_pad))
876 props->device_cap_flags |= IB_DEVICE_PCI_WRITE_END_PADDING;
877
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300878 props->vendor_part_id = mdev->pdev->device;
879 props->hw_ver = mdev->pdev->revision;
Eli Cohene126ba92013-07-07 17:25:49 +0300880
881 props->max_mr_size = ~0ull;
Sagi Grimberge0238a62015-07-21 14:40:12 +0300882 props->page_size_cap = ~(min_page_size - 1);
Saeed Mahameed938fe832015-05-28 22:28:41 +0300883 props->max_qp = 1 << MLX5_CAP_GEN(mdev, log_max_qp);
884 props->max_qp_wr = 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
885 max_rq_sg = MLX5_CAP_GEN(mdev, max_wqe_sz_rq) /
886 sizeof(struct mlx5_wqe_data_seg);
Eli Cohen288c01b2016-10-27 16:36:45 +0300887 max_sq_desc = min_t(int, MLX5_CAP_GEN(mdev, max_wqe_sz_sq), 512);
888 max_sq_sg = (max_sq_desc - sizeof(struct mlx5_wqe_ctrl_seg) -
889 sizeof(struct mlx5_wqe_raddr_seg)) /
890 sizeof(struct mlx5_wqe_data_seg);
Steve Wise33023fb2018-06-18 08:05:26 -0700891 props->max_send_sge = max_sq_sg;
892 props->max_recv_sge = max_rq_sg;
Sagi Grimberg986ef952016-03-31 19:03:25 +0300893 props->max_sge_rd = MLX5_MAX_SGE_RD;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300894 props->max_cq = 1 << MLX5_CAP_GEN(mdev, log_max_cq);
Leon Romanovsky9f177682016-01-14 08:11:40 +0200895 props->max_cqe = (1 << MLX5_CAP_GEN(mdev, log_max_cq_sz)) - 1;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300896 props->max_mr = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
897 props->max_pd = 1 << MLX5_CAP_GEN(mdev, log_max_pd);
898 props->max_qp_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_req_qp);
899 props->max_qp_init_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_res_qp);
900 props->max_srq = 1 << MLX5_CAP_GEN(mdev, log_max_srq);
901 props->max_srq_wr = (1 << MLX5_CAP_GEN(mdev, log_max_srq_sz)) - 1;
902 props->local_ca_ack_delay = MLX5_CAP_GEN(mdev, local_ca_ack_delay);
Eli Cohene126ba92013-07-07 17:25:49 +0300903 props->max_res_rd_atom = props->max_qp_rd_atom * props->max_qp;
Eli Cohene126ba92013-07-07 17:25:49 +0300904 props->max_srq_sge = max_rq_sg - 1;
Sagi Grimberg911f4332016-03-03 13:37:51 +0200905 props->max_fast_reg_page_list_len =
906 1 << MLX5_CAP_GEN(mdev, log_max_klm_list_size);
Moni Shoua776a3902018-01-02 16:19:33 +0200907 get_atomic_caps_qp(dev, props);
Eli Cohen81bea282013-09-11 16:35:30 +0300908 props->masked_atomic_cap = IB_ATOMIC_NONE;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300909 props->max_mcast_grp = 1 << MLX5_CAP_GEN(mdev, log_max_mcg);
910 props->max_mcast_qp_attach = MLX5_CAP_GEN(mdev, max_qp_mcg);
Eli Cohene126ba92013-07-07 17:25:49 +0300911 props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
912 props->max_mcast_grp;
913 props->max_map_per_fmr = INT_MAX; /* no limit in ConnectIB */
Maor Gottlieb86695a62016-10-27 16:36:38 +0300914 props->max_ah = INT_MAX;
Matan Barak7c60bcb2015-12-15 20:30:11 +0200915 props->hca_core_clock = MLX5_CAP_GEN(mdev, device_frequency_khz);
916 props->timestamp_mask = 0x7FFFFFFFFFFFFFFFULL;
Eli Cohene126ba92013-07-07 17:25:49 +0300917
Haggai Eran8cdd3122014-12-11 17:04:20 +0200918#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
Saeed Mahameed938fe832015-05-28 22:28:41 +0300919 if (MLX5_CAP_GEN(mdev, pg))
Haggai Eran8cdd3122014-12-11 17:04:20 +0200920 props->device_cap_flags |= IB_DEVICE_ON_DEMAND_PAGING;
921 props->odp_caps = dev->odp_caps;
922#endif
923
Leon Romanovsky051f2632015-12-20 12:16:11 +0200924 if (MLX5_CAP_GEN(mdev, cd))
925 props->device_cap_flags |= IB_DEVICE_CROSS_CHANNEL;
926
Eli Coheneff901d2016-03-11 22:58:42 +0200927 if (!mlx5_core_is_pf(mdev))
928 props->device_cap_flags |= IB_DEVICE_VIRTUAL_FUNCTION;
929
Yishai Hadas31f69a82016-08-28 11:28:45 +0300930 if (mlx5_ib_port_link_layer(ibdev, 1) ==
Daniel Jurgens85c7c012018-01-04 17:25:43 +0200931 IB_LINK_LAYER_ETHERNET && raw_support) {
Yishai Hadas31f69a82016-08-28 11:28:45 +0300932 props->rss_caps.max_rwq_indirection_tables =
933 1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt);
934 props->rss_caps.max_rwq_indirection_table_size =
935 1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt_size);
936 props->rss_caps.supported_qpts = 1 << IB_QPT_RAW_PACKET;
937 props->max_wq_type_rq =
938 1 << MLX5_CAP_GEN(dev->mdev, log_max_rq);
939 }
940
Artemy Kovalyoveb761892017-08-17 15:52:09 +0300941 if (MLX5_CAP_GEN(mdev, tag_matching)) {
Leon Romanovsky78b1beb2017-09-24 21:46:29 +0300942 props->tm_caps.max_rndv_hdr_size = MLX5_TM_MAX_RNDV_MSG_SIZE;
943 props->tm_caps.max_num_tags =
Artemy Kovalyoveb761892017-08-17 15:52:09 +0300944 (1 << MLX5_CAP_GEN(mdev, log_tag_matching_list_sz)) - 1;
Leon Romanovsky78b1beb2017-09-24 21:46:29 +0300945 props->tm_caps.flags = IB_TM_CAP_RC;
946 props->tm_caps.max_ops =
Artemy Kovalyoveb761892017-08-17 15:52:09 +0300947 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
Leon Romanovsky78b1beb2017-09-24 21:46:29 +0300948 props->tm_caps.max_sge = MLX5_TM_MAX_SGE;
Artemy Kovalyoveb761892017-08-17 15:52:09 +0300949 }
950
Yonatan Cohen87ab3f52017-11-13 10:51:18 +0200951 if (MLX5_CAP_GEN(dev->mdev, cq_moderation)) {
952 props->cq_caps.max_cq_moderation_count =
953 MLX5_MAX_CQ_COUNT;
954 props->cq_caps.max_cq_moderation_period =
955 MLX5_MAX_CQ_PERIOD;
956 }
957
Bodong Wang7e43a2a2016-10-31 12:16:44 +0200958 if (field_avail(typeof(resp), cqe_comp_caps, uhw->outlen)) {
Bodong Wang7e43a2a2016-10-31 12:16:44 +0200959 resp.response_length += sizeof(resp.cqe_comp_caps);
Yonatan Cohen572f46b2018-05-27 13:42:33 +0300960
961 if (MLX5_CAP_GEN(dev->mdev, cqe_compression)) {
962 resp.cqe_comp_caps.max_num =
963 MLX5_CAP_GEN(dev->mdev,
964 cqe_compression_max_num);
965
966 resp.cqe_comp_caps.supported_format =
967 MLX5_IB_CQE_RES_FORMAT_HASH |
968 MLX5_IB_CQE_RES_FORMAT_CSUM;
Yonatan Cohen6f1006a2018-05-27 13:42:34 +0300969
970 if (MLX5_CAP_GEN(dev->mdev, mini_cqe_resp_stride_index))
971 resp.cqe_comp_caps.supported_format |=
972 MLX5_IB_CQE_RES_FORMAT_CSUM_STRIDX;
Yonatan Cohen572f46b2018-05-27 13:42:33 +0300973 }
Bodong Wang7e43a2a2016-10-31 12:16:44 +0200974 }
975
Daniel Jurgens85c7c012018-01-04 17:25:43 +0200976 if (field_avail(typeof(resp), packet_pacing_caps, uhw->outlen) &&
977 raw_support) {
Bodong Wangd9491672016-12-01 13:43:13 +0200978 if (MLX5_CAP_QOS(mdev, packet_pacing) &&
979 MLX5_CAP_GEN(mdev, qos)) {
980 resp.packet_pacing_caps.qp_rate_limit_max =
981 MLX5_CAP_QOS(mdev, packet_pacing_max_rate);
982 resp.packet_pacing_caps.qp_rate_limit_min =
983 MLX5_CAP_QOS(mdev, packet_pacing_min_rate);
984 resp.packet_pacing_caps.supported_qpts |=
985 1 << IB_QPT_RAW_PACKET;
Bodong Wang61147f32018-03-19 15:10:30 +0200986 if (MLX5_CAP_QOS(mdev, packet_pacing_burst_bound) &&
987 MLX5_CAP_QOS(mdev, packet_pacing_typical_size))
988 resp.packet_pacing_caps.cap_flags |=
989 MLX5_IB_PP_SUPPORT_BURST;
Bodong Wangd9491672016-12-01 13:43:13 +0200990 }
991 resp.response_length += sizeof(resp.packet_pacing_caps);
992 }
993
Leon Romanovsky9f885202017-01-02 11:37:39 +0200994 if (field_avail(typeof(resp), mlx5_ib_support_multi_pkt_send_wqes,
995 uhw->outlen)) {
Bodong Wang795b6092017-08-17 15:52:34 +0300996 if (MLX5_CAP_ETH(mdev, multi_pkt_send_wqe))
997 resp.mlx5_ib_support_multi_pkt_send_wqes =
998 MLX5_IB_ALLOW_MPW;
Bodong Wang050da902017-08-17 15:52:35 +0300999
1000 if (MLX5_CAP_ETH(mdev, enhanced_multi_pkt_send_wqe))
1001 resp.mlx5_ib_support_multi_pkt_send_wqes |=
1002 MLX5_IB_SUPPORT_EMPW;
1003
Leon Romanovsky9f885202017-01-02 11:37:39 +02001004 resp.response_length +=
1005 sizeof(resp.mlx5_ib_support_multi_pkt_send_wqes);
1006 }
1007
Guy Levide57f2a2017-10-19 08:25:52 +03001008 if (field_avail(typeof(resp), flags, uhw->outlen)) {
1009 resp.response_length += sizeof(resp.flags);
Guy Levi7a0c8f42017-10-19 08:25:53 +03001010
Guy Levide57f2a2017-10-19 08:25:52 +03001011 if (MLX5_CAP_GEN(mdev, cqe_compression_128))
1012 resp.flags |=
1013 MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_COMP;
Guy Levi7a0c8f42017-10-19 08:25:53 +03001014
1015 if (MLX5_CAP_GEN(mdev, cqe_128_always))
1016 resp.flags |= MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_PAD;
Guy Levide57f2a2017-10-19 08:25:52 +03001017 }
Leon Romanovsky9f885202017-01-02 11:37:39 +02001018
Noa Osherovich96dc3fc2017-08-17 15:52:28 +03001019 if (field_avail(typeof(resp), sw_parsing_caps,
1020 uhw->outlen)) {
1021 resp.response_length += sizeof(resp.sw_parsing_caps);
1022 if (MLX5_CAP_ETH(mdev, swp)) {
1023 resp.sw_parsing_caps.sw_parsing_offloads |=
1024 MLX5_IB_SW_PARSING;
1025
1026 if (MLX5_CAP_ETH(mdev, swp_csum))
1027 resp.sw_parsing_caps.sw_parsing_offloads |=
1028 MLX5_IB_SW_PARSING_CSUM;
1029
1030 if (MLX5_CAP_ETH(mdev, swp_lso))
1031 resp.sw_parsing_caps.sw_parsing_offloads |=
1032 MLX5_IB_SW_PARSING_LSO;
1033
1034 if (resp.sw_parsing_caps.sw_parsing_offloads)
1035 resp.sw_parsing_caps.supported_qpts =
1036 BIT(IB_QPT_RAW_PACKET);
1037 }
1038 }
1039
Daniel Jurgens85c7c012018-01-04 17:25:43 +02001040 if (field_avail(typeof(resp), striding_rq_caps, uhw->outlen) &&
1041 raw_support) {
Noa Osherovichb4f34592017-10-17 18:01:12 +03001042 resp.response_length += sizeof(resp.striding_rq_caps);
1043 if (MLX5_CAP_GEN(mdev, striding_rq)) {
1044 resp.striding_rq_caps.min_single_stride_log_num_of_bytes =
1045 MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES;
1046 resp.striding_rq_caps.max_single_stride_log_num_of_bytes =
1047 MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES;
1048 resp.striding_rq_caps.min_single_wqe_log_num_of_strides =
1049 MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES;
1050 resp.striding_rq_caps.max_single_wqe_log_num_of_strides =
1051 MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES;
1052 resp.striding_rq_caps.supported_qpts =
1053 BIT(IB_QPT_RAW_PACKET);
1054 }
1055 }
1056
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001057 if (field_avail(typeof(resp), tunnel_offloads_caps,
1058 uhw->outlen)) {
1059 resp.response_length += sizeof(resp.tunnel_offloads_caps);
1060 if (MLX5_CAP_ETH(mdev, tunnel_stateless_vxlan))
1061 resp.tunnel_offloads_caps |=
1062 MLX5_IB_TUNNELED_OFFLOADS_VXLAN;
1063 if (MLX5_CAP_ETH(mdev, tunnel_stateless_geneve_rx))
1064 resp.tunnel_offloads_caps |=
1065 MLX5_IB_TUNNELED_OFFLOADS_GENEVE;
1066 if (MLX5_CAP_ETH(mdev, tunnel_stateless_gre))
1067 resp.tunnel_offloads_caps |=
1068 MLX5_IB_TUNNELED_OFFLOADS_GRE;
Ariel Levkoviche818e252018-05-13 14:33:35 +03001069 if (MLX5_CAP_GEN(mdev, flex_parser_protocols) &
1070 MLX5_FLEX_PROTO_CW_MPLS_GRE)
1071 resp.tunnel_offloads_caps |=
1072 MLX5_IB_TUNNELED_OFFLOADS_MPLS_GRE;
1073 if (MLX5_CAP_GEN(mdev, flex_parser_protocols) &
1074 MLX5_FLEX_PROTO_CW_MPLS_UDP)
1075 resp.tunnel_offloads_caps |=
1076 MLX5_IB_TUNNELED_OFFLOADS_MPLS_UDP;
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001077 }
1078
Bodong Wang402ca532016-06-17 15:02:20 +03001079 if (uhw->outlen) {
1080 err = ib_copy_to_udata(uhw, &resp, resp.response_length);
1081
1082 if (err)
1083 return err;
1084 }
1085
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001086 return 0;
1087}
Eli Cohene126ba92013-07-07 17:25:49 +03001088
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001089enum mlx5_ib_width {
1090 MLX5_IB_WIDTH_1X = 1 << 0,
1091 MLX5_IB_WIDTH_2X = 1 << 1,
1092 MLX5_IB_WIDTH_4X = 1 << 2,
1093 MLX5_IB_WIDTH_8X = 1 << 3,
1094 MLX5_IB_WIDTH_12X = 1 << 4
1095};
1096
1097static int translate_active_width(struct ib_device *ibdev, u8 active_width,
1098 u8 *ib_width)
1099{
1100 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1101 int err = 0;
1102
1103 if (active_width & MLX5_IB_WIDTH_1X) {
1104 *ib_width = IB_WIDTH_1X;
1105 } else if (active_width & MLX5_IB_WIDTH_2X) {
1106 mlx5_ib_dbg(dev, "active_width %d is not supported by IB spec\n",
1107 (int)active_width);
1108 err = -EINVAL;
1109 } else if (active_width & MLX5_IB_WIDTH_4X) {
1110 *ib_width = IB_WIDTH_4X;
1111 } else if (active_width & MLX5_IB_WIDTH_8X) {
1112 *ib_width = IB_WIDTH_8X;
1113 } else if (active_width & MLX5_IB_WIDTH_12X) {
1114 *ib_width = IB_WIDTH_12X;
1115 } else {
1116 mlx5_ib_dbg(dev, "Invalid active_width %d\n",
1117 (int)active_width);
1118 err = -EINVAL;
1119 }
1120
1121 return err;
1122}
1123
1124static int mlx5_mtu_to_ib_mtu(int mtu)
1125{
1126 switch (mtu) {
1127 case 256: return 1;
1128 case 512: return 2;
1129 case 1024: return 3;
1130 case 2048: return 4;
1131 case 4096: return 5;
1132 default:
1133 pr_warn("invalid mtu\n");
1134 return -1;
1135 }
1136}
1137
1138enum ib_max_vl_num {
1139 __IB_MAX_VL_0 = 1,
1140 __IB_MAX_VL_0_1 = 2,
1141 __IB_MAX_VL_0_3 = 3,
1142 __IB_MAX_VL_0_7 = 4,
1143 __IB_MAX_VL_0_14 = 5,
1144};
1145
1146enum mlx5_vl_hw_cap {
1147 MLX5_VL_HW_0 = 1,
1148 MLX5_VL_HW_0_1 = 2,
1149 MLX5_VL_HW_0_2 = 3,
1150 MLX5_VL_HW_0_3 = 4,
1151 MLX5_VL_HW_0_4 = 5,
1152 MLX5_VL_HW_0_5 = 6,
1153 MLX5_VL_HW_0_6 = 7,
1154 MLX5_VL_HW_0_7 = 8,
1155 MLX5_VL_HW_0_14 = 15
1156};
1157
1158static int translate_max_vl_num(struct ib_device *ibdev, u8 vl_hw_cap,
1159 u8 *max_vl_num)
1160{
1161 switch (vl_hw_cap) {
1162 case MLX5_VL_HW_0:
1163 *max_vl_num = __IB_MAX_VL_0;
1164 break;
1165 case MLX5_VL_HW_0_1:
1166 *max_vl_num = __IB_MAX_VL_0_1;
1167 break;
1168 case MLX5_VL_HW_0_3:
1169 *max_vl_num = __IB_MAX_VL_0_3;
1170 break;
1171 case MLX5_VL_HW_0_7:
1172 *max_vl_num = __IB_MAX_VL_0_7;
1173 break;
1174 case MLX5_VL_HW_0_14:
1175 *max_vl_num = __IB_MAX_VL_0_14;
1176 break;
1177
1178 default:
1179 return -EINVAL;
1180 }
1181
1182 return 0;
1183}
1184
1185static int mlx5_query_hca_port(struct ib_device *ibdev, u8 port,
1186 struct ib_port_attr *props)
1187{
1188 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1189 struct mlx5_core_dev *mdev = dev->mdev;
1190 struct mlx5_hca_vport_context *rep;
Saeed Mahameed046339e2016-04-22 00:33:03 +03001191 u16 max_mtu;
1192 u16 oper_mtu;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001193 int err;
1194 u8 ib_link_width_oper;
1195 u8 vl_hw_cap;
1196
1197 rep = kzalloc(sizeof(*rep), GFP_KERNEL);
1198 if (!rep) {
1199 err = -ENOMEM;
1200 goto out;
1201 }
1202
Or Gerlitzc4550c62017-01-24 13:02:39 +02001203 /* props being zeroed by the caller, avoid zeroing it here */
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001204
1205 err = mlx5_query_hca_vport_context(mdev, 0, port, 0, rep);
1206 if (err)
1207 goto out;
1208
1209 props->lid = rep->lid;
1210 props->lmc = rep->lmc;
1211 props->sm_lid = rep->sm_lid;
1212 props->sm_sl = rep->sm_sl;
1213 props->state = rep->vport_state;
1214 props->phys_state = rep->port_physical_state;
1215 props->port_cap_flags = rep->cap_mask1;
1216 props->gid_tbl_len = mlx5_get_gid_table_len(MLX5_CAP_GEN(mdev, gid_table_size));
1217 props->max_msg_sz = 1 << MLX5_CAP_GEN(mdev, log_max_msg);
1218 props->pkey_tbl_len = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, pkey_table_size));
1219 props->bad_pkey_cntr = rep->pkey_violation_counter;
1220 props->qkey_viol_cntr = rep->qkey_violation_counter;
1221 props->subnet_timeout = rep->subnet_timeout;
1222 props->init_type_reply = rep->init_type_reply;
1223
1224 err = mlx5_query_port_link_width_oper(mdev, &ib_link_width_oper, port);
1225 if (err)
1226 goto out;
1227
1228 err = translate_active_width(ibdev, ib_link_width_oper,
1229 &props->active_width);
1230 if (err)
1231 goto out;
Noa Osherovichd5beb7f2016-06-02 10:47:53 +03001232 err = mlx5_query_port_ib_proto_oper(mdev, &props->active_speed, port);
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001233 if (err)
1234 goto out;
1235
Saeed Mahameedfacc9692015-06-11 14:47:27 +03001236 mlx5_query_port_max_mtu(mdev, &max_mtu, port);
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001237
1238 props->max_mtu = mlx5_mtu_to_ib_mtu(max_mtu);
1239
Saeed Mahameedfacc9692015-06-11 14:47:27 +03001240 mlx5_query_port_oper_mtu(mdev, &oper_mtu, port);
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001241
1242 props->active_mtu = mlx5_mtu_to_ib_mtu(oper_mtu);
1243
1244 err = mlx5_query_port_vl_hw_cap(mdev, &vl_hw_cap, port);
1245 if (err)
1246 goto out;
1247
1248 err = translate_max_vl_num(ibdev, vl_hw_cap,
1249 &props->max_vl_num);
1250out:
1251 kfree(rep);
Eli Cohene126ba92013-07-07 17:25:49 +03001252 return err;
1253}
1254
1255int mlx5_ib_query_port(struct ib_device *ibdev, u8 port,
1256 struct ib_port_attr *props)
1257{
Ilan Tayari095b0922017-05-14 16:04:30 +03001258 unsigned int count;
1259 int ret;
1260
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001261 switch (mlx5_get_vport_access_method(ibdev)) {
1262 case MLX5_VPORT_ACCESS_METHOD_MAD:
Ilan Tayari095b0922017-05-14 16:04:30 +03001263 ret = mlx5_query_mad_ifc_port(ibdev, port, props);
1264 break;
Eli Cohene126ba92013-07-07 17:25:49 +03001265
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001266 case MLX5_VPORT_ACCESS_METHOD_HCA:
Ilan Tayari095b0922017-05-14 16:04:30 +03001267 ret = mlx5_query_hca_port(ibdev, port, props);
1268 break;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001269
Achiad Shochat3f89a642015-12-23 18:47:21 +02001270 case MLX5_VPORT_ACCESS_METHOD_NIC:
Ilan Tayari095b0922017-05-14 16:04:30 +03001271 ret = mlx5_query_port_roce(ibdev, port, props);
1272 break;
Achiad Shochat3f89a642015-12-23 18:47:21 +02001273
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001274 default:
Ilan Tayari095b0922017-05-14 16:04:30 +03001275 ret = -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +03001276 }
Ilan Tayari095b0922017-05-14 16:04:30 +03001277
1278 if (!ret && props) {
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001279 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1280 struct mlx5_core_dev *mdev;
1281 bool put_mdev = true;
1282
1283 mdev = mlx5_ib_get_native_port_mdev(dev, port, NULL);
1284 if (!mdev) {
1285 /* If the port isn't affiliated yet query the master.
1286 * The master and slave will have the same values.
1287 */
1288 mdev = dev->mdev;
1289 port = 1;
1290 put_mdev = false;
1291 }
1292 count = mlx5_core_reserved_gids_count(mdev);
1293 if (put_mdev)
1294 mlx5_ib_put_native_port_mdev(dev, port);
Ilan Tayari095b0922017-05-14 16:04:30 +03001295 props->gid_tbl_len -= count;
1296 }
1297 return ret;
Eli Cohene126ba92013-07-07 17:25:49 +03001298}
1299
Mark Bloch8e6efa32017-11-06 12:22:13 +00001300static int mlx5_ib_rep_query_port(struct ib_device *ibdev, u8 port,
1301 struct ib_port_attr *props)
1302{
1303 int ret;
1304
1305 /* Only link layer == ethernet is valid for representors */
1306 ret = mlx5_query_port_roce(ibdev, port, props);
1307 if (ret || !props)
1308 return ret;
1309
1310 /* We don't support GIDS */
1311 props->gid_tbl_len = 0;
1312
1313 return ret;
1314}
1315
Eli Cohene126ba92013-07-07 17:25:49 +03001316static int mlx5_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
1317 union ib_gid *gid)
1318{
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001319 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1320 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03001321
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001322 switch (mlx5_get_vport_access_method(ibdev)) {
1323 case MLX5_VPORT_ACCESS_METHOD_MAD:
1324 return mlx5_query_mad_ifc_gids(ibdev, port, index, gid);
Eli Cohene126ba92013-07-07 17:25:49 +03001325
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001326 case MLX5_VPORT_ACCESS_METHOD_HCA:
1327 return mlx5_query_hca_vport_gid(mdev, 0, port, 0, index, gid);
Eli Cohene126ba92013-07-07 17:25:49 +03001328
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001329 default:
1330 return -EINVAL;
1331 }
Eli Cohene126ba92013-07-07 17:25:49 +03001332
Eli Cohene126ba92013-07-07 17:25:49 +03001333}
1334
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001335static int mlx5_query_hca_nic_pkey(struct ib_device *ibdev, u8 port,
1336 u16 index, u16 *pkey)
1337{
1338 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1339 struct mlx5_core_dev *mdev;
1340 bool put_mdev = true;
1341 u8 mdev_port_num;
1342 int err;
1343
1344 mdev = mlx5_ib_get_native_port_mdev(dev, port, &mdev_port_num);
1345 if (!mdev) {
1346 /* The port isn't affiliated yet, get the PKey from the master
1347 * port. For RoCE the PKey tables will be the same.
1348 */
1349 put_mdev = false;
1350 mdev = dev->mdev;
1351 mdev_port_num = 1;
1352 }
1353
1354 err = mlx5_query_hca_vport_pkey(mdev, 0, mdev_port_num, 0,
1355 index, pkey);
1356 if (put_mdev)
1357 mlx5_ib_put_native_port_mdev(dev, port);
1358
1359 return err;
1360}
1361
Eli Cohene126ba92013-07-07 17:25:49 +03001362static int mlx5_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
1363 u16 *pkey)
1364{
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001365 switch (mlx5_get_vport_access_method(ibdev)) {
1366 case MLX5_VPORT_ACCESS_METHOD_MAD:
1367 return mlx5_query_mad_ifc_pkey(ibdev, port, index, pkey);
Eli Cohene126ba92013-07-07 17:25:49 +03001368
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001369 case MLX5_VPORT_ACCESS_METHOD_HCA:
1370 case MLX5_VPORT_ACCESS_METHOD_NIC:
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001371 return mlx5_query_hca_nic_pkey(ibdev, port, index, pkey);
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001372 default:
1373 return -EINVAL;
1374 }
Eli Cohene126ba92013-07-07 17:25:49 +03001375}
1376
Eli Cohene126ba92013-07-07 17:25:49 +03001377static int mlx5_ib_modify_device(struct ib_device *ibdev, int mask,
1378 struct ib_device_modify *props)
1379{
1380 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1381 struct mlx5_reg_node_desc in;
1382 struct mlx5_reg_node_desc out;
1383 int err;
1384
1385 if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
1386 return -EOPNOTSUPP;
1387
1388 if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
1389 return 0;
1390
1391 /*
1392 * If possible, pass node desc to FW, so it can generate
1393 * a 144 trap. If cmd fails, just ignore.
1394 */
Yuval Shaiabd99fde2016-08-25 10:57:07 -07001395 memcpy(&in, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
Jack Morgenstein9603b612014-07-28 23:30:22 +03001396 err = mlx5_core_access_reg(dev->mdev, &in, sizeof(in), &out,
Eli Cohene126ba92013-07-07 17:25:49 +03001397 sizeof(out), MLX5_REG_NODE_DESC, 0, 1);
1398 if (err)
1399 return err;
1400
Yuval Shaiabd99fde2016-08-25 10:57:07 -07001401 memcpy(ibdev->node_desc, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
Eli Cohene126ba92013-07-07 17:25:49 +03001402
1403 return err;
1404}
1405
Eli Cohencdbe33d2017-02-14 07:25:38 +02001406static int set_port_caps_atomic(struct mlx5_ib_dev *dev, u8 port_num, u32 mask,
1407 u32 value)
1408{
1409 struct mlx5_hca_vport_context ctx = {};
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001410 struct mlx5_core_dev *mdev;
1411 u8 mdev_port_num;
Eli Cohencdbe33d2017-02-14 07:25:38 +02001412 int err;
1413
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001414 mdev = mlx5_ib_get_native_port_mdev(dev, port_num, &mdev_port_num);
1415 if (!mdev)
1416 return -ENODEV;
1417
1418 err = mlx5_query_hca_vport_context(mdev, 0, mdev_port_num, 0, &ctx);
Eli Cohencdbe33d2017-02-14 07:25:38 +02001419 if (err)
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001420 goto out;
Eli Cohencdbe33d2017-02-14 07:25:38 +02001421
1422 if (~ctx.cap_mask1_perm & mask) {
1423 mlx5_ib_warn(dev, "trying to change bitmask 0x%X but change supported 0x%X\n",
1424 mask, ctx.cap_mask1_perm);
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001425 err = -EINVAL;
1426 goto out;
Eli Cohencdbe33d2017-02-14 07:25:38 +02001427 }
1428
1429 ctx.cap_mask1 = value;
1430 ctx.cap_mask1_perm = mask;
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001431 err = mlx5_core_modify_hca_vport_context(mdev, 0, mdev_port_num,
1432 0, &ctx);
1433
1434out:
1435 mlx5_ib_put_native_port_mdev(dev, port_num);
Eli Cohencdbe33d2017-02-14 07:25:38 +02001436
1437 return err;
1438}
1439
Eli Cohene126ba92013-07-07 17:25:49 +03001440static int mlx5_ib_modify_port(struct ib_device *ibdev, u8 port, int mask,
1441 struct ib_port_modify *props)
1442{
1443 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1444 struct ib_port_attr attr;
1445 u32 tmp;
1446 int err;
Eli Cohencdbe33d2017-02-14 07:25:38 +02001447 u32 change_mask;
1448 u32 value;
1449 bool is_ib = (mlx5_ib_port_link_layer(ibdev, port) ==
1450 IB_LINK_LAYER_INFINIBAND);
1451
Majd Dibbinyec255872017-08-23 08:35:42 +03001452 /* CM layer calls ib_modify_port() regardless of the link layer. For
1453 * Ethernet ports, qkey violation and Port capabilities are meaningless.
1454 */
1455 if (!is_ib)
1456 return 0;
1457
Eli Cohencdbe33d2017-02-14 07:25:38 +02001458 if (MLX5_CAP_GEN(dev->mdev, ib_virt) && is_ib) {
1459 change_mask = props->clr_port_cap_mask | props->set_port_cap_mask;
1460 value = ~props->clr_port_cap_mask | props->set_port_cap_mask;
1461 return set_port_caps_atomic(dev, port, change_mask, value);
1462 }
Eli Cohene126ba92013-07-07 17:25:49 +03001463
1464 mutex_lock(&dev->cap_mask_mutex);
1465
Or Gerlitzc4550c62017-01-24 13:02:39 +02001466 err = ib_query_port(ibdev, port, &attr);
Eli Cohene126ba92013-07-07 17:25:49 +03001467 if (err)
1468 goto out;
1469
1470 tmp = (attr.port_cap_flags | props->set_port_cap_mask) &
1471 ~props->clr_port_cap_mask;
1472
Jack Morgenstein9603b612014-07-28 23:30:22 +03001473 err = mlx5_set_port_caps(dev->mdev, port, tmp);
Eli Cohene126ba92013-07-07 17:25:49 +03001474
1475out:
1476 mutex_unlock(&dev->cap_mask_mutex);
1477 return err;
1478}
1479
Eli Cohen30aa60b2017-01-03 23:55:27 +02001480static void print_lib_caps(struct mlx5_ib_dev *dev, u64 caps)
1481{
1482 mlx5_ib_dbg(dev, "MLX5_LIB_CAP_4K_UAR = %s\n",
1483 caps & MLX5_LIB_CAP_4K_UAR ? "y" : "n");
1484}
1485
Yishai Hadas31a78a52017-12-24 16:31:34 +02001486static u16 calc_dynamic_bfregs(int uars_per_sys_page)
1487{
1488 /* Large page with non 4k uar support might limit the dynamic size */
1489 if (uars_per_sys_page == 1 && PAGE_SIZE > 4096)
1490 return MLX5_MIN_DYN_BFREGS;
1491
1492 return MLX5_MAX_DYN_BFREGS;
1493}
1494
Eli Cohenb037c292017-01-03 23:55:26 +02001495static int calc_total_bfregs(struct mlx5_ib_dev *dev, bool lib_uar_4k,
1496 struct mlx5_ib_alloc_ucontext_req_v2 *req,
Yishai Hadas31a78a52017-12-24 16:31:34 +02001497 struct mlx5_bfreg_info *bfregi)
Eli Cohenb037c292017-01-03 23:55:26 +02001498{
1499 int uars_per_sys_page;
1500 int bfregs_per_sys_page;
1501 int ref_bfregs = req->total_num_bfregs;
1502
1503 if (req->total_num_bfregs == 0)
1504 return -EINVAL;
1505
1506 BUILD_BUG_ON(MLX5_MAX_BFREGS % MLX5_NON_FP_BFREGS_IN_PAGE);
1507 BUILD_BUG_ON(MLX5_MAX_BFREGS < MLX5_NON_FP_BFREGS_IN_PAGE);
1508
1509 if (req->total_num_bfregs > MLX5_MAX_BFREGS)
1510 return -ENOMEM;
1511
1512 uars_per_sys_page = get_uars_per_sys_page(dev, lib_uar_4k);
1513 bfregs_per_sys_page = uars_per_sys_page * MLX5_NON_FP_BFREGS_PER_UAR;
Yishai Hadas31a78a52017-12-24 16:31:34 +02001514 /* This holds the required static allocation asked by the user */
Eli Cohenb037c292017-01-03 23:55:26 +02001515 req->total_num_bfregs = ALIGN(req->total_num_bfregs, bfregs_per_sys_page);
Eli Cohenb037c292017-01-03 23:55:26 +02001516 if (req->num_low_latency_bfregs > req->total_num_bfregs - 1)
1517 return -EINVAL;
1518
Yishai Hadas31a78a52017-12-24 16:31:34 +02001519 bfregi->num_static_sys_pages = req->total_num_bfregs / bfregs_per_sys_page;
1520 bfregi->num_dyn_bfregs = ALIGN(calc_dynamic_bfregs(uars_per_sys_page), bfregs_per_sys_page);
1521 bfregi->total_num_bfregs = req->total_num_bfregs + bfregi->num_dyn_bfregs;
1522 bfregi->num_sys_pages = bfregi->total_num_bfregs / bfregs_per_sys_page;
1523
1524 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 +02001525 MLX5_CAP_GEN(dev->mdev, uar_4k) ? "yes" : "no",
1526 lib_uar_4k ? "yes" : "no", ref_bfregs,
Yishai Hadas31a78a52017-12-24 16:31:34 +02001527 req->total_num_bfregs, bfregi->total_num_bfregs,
1528 bfregi->num_sys_pages);
Eli Cohenb037c292017-01-03 23:55:26 +02001529
1530 return 0;
1531}
1532
1533static int allocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
1534{
1535 struct mlx5_bfreg_info *bfregi;
1536 int err;
1537 int i;
1538
1539 bfregi = &context->bfregi;
Yishai Hadas31a78a52017-12-24 16:31:34 +02001540 for (i = 0; i < bfregi->num_static_sys_pages; i++) {
Eli Cohenb037c292017-01-03 23:55:26 +02001541 err = mlx5_cmd_alloc_uar(dev->mdev, &bfregi->sys_pages[i]);
1542 if (err)
1543 goto error;
1544
1545 mlx5_ib_dbg(dev, "allocated uar %d\n", bfregi->sys_pages[i]);
1546 }
Yishai Hadas4ed131d2017-12-24 16:31:35 +02001547
1548 for (i = bfregi->num_static_sys_pages; i < bfregi->num_sys_pages; i++)
1549 bfregi->sys_pages[i] = MLX5_IB_INVALID_UAR_INDEX;
1550
Eli Cohenb037c292017-01-03 23:55:26 +02001551 return 0;
1552
1553error:
1554 for (--i; i >= 0; i--)
1555 if (mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]))
1556 mlx5_ib_warn(dev, "failed to free uar %d\n", i);
1557
1558 return err;
1559}
1560
Leon Romanovsky15177992018-06-27 10:44:24 +03001561static void deallocate_uars(struct mlx5_ib_dev *dev,
1562 struct mlx5_ib_ucontext *context)
Eli Cohenb037c292017-01-03 23:55:26 +02001563{
1564 struct mlx5_bfreg_info *bfregi;
Eli Cohenb037c292017-01-03 23:55:26 +02001565 int i;
1566
1567 bfregi = &context->bfregi;
Leon Romanovsky15177992018-06-27 10:44:24 +03001568 for (i = 0; i < bfregi->num_sys_pages; i++)
Yishai Hadas4ed131d2017-12-24 16:31:35 +02001569 if (i < bfregi->num_static_sys_pages ||
Leon Romanovsky15177992018-06-27 10:44:24 +03001570 bfregi->sys_pages[i] != MLX5_IB_INVALID_UAR_INDEX)
1571 mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]);
Eli Cohenb037c292017-01-03 23:55:26 +02001572}
1573
Huy Nguyenc85023e2017-05-30 09:42:54 +03001574static int mlx5_ib_alloc_transport_domain(struct mlx5_ib_dev *dev, u32 *tdn)
1575{
1576 int err;
1577
Leon Romanovskycfdeb892018-06-19 10:39:06 +03001578 if (!MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1579 return 0;
1580
Huy Nguyenc85023e2017-05-30 09:42:54 +03001581 err = mlx5_core_alloc_transport_domain(dev->mdev, tdn);
1582 if (err)
1583 return err;
1584
1585 if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
Eran Ben Elisha8978cc92018-01-09 11:41:10 +02001586 (!MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) &&
1587 !MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
Huy Nguyenc85023e2017-05-30 09:42:54 +03001588 return err;
1589
1590 mutex_lock(&dev->lb_mutex);
1591 dev->user_td++;
1592
1593 if (dev->user_td == 2)
1594 err = mlx5_nic_vport_update_local_lb(dev->mdev, true);
1595
1596 mutex_unlock(&dev->lb_mutex);
1597 return err;
1598}
1599
1600static void mlx5_ib_dealloc_transport_domain(struct mlx5_ib_dev *dev, u32 tdn)
1601{
Leon Romanovskycfdeb892018-06-19 10:39:06 +03001602 if (!MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1603 return;
1604
Huy Nguyenc85023e2017-05-30 09:42:54 +03001605 mlx5_core_dealloc_transport_domain(dev->mdev, tdn);
1606
1607 if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
Eran Ben Elisha8978cc92018-01-09 11:41:10 +02001608 (!MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) &&
1609 !MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
Huy Nguyenc85023e2017-05-30 09:42:54 +03001610 return;
1611
1612 mutex_lock(&dev->lb_mutex);
1613 dev->user_td--;
1614
1615 if (dev->user_td < 2)
1616 mlx5_nic_vport_update_local_lb(dev->mdev, false);
1617
1618 mutex_unlock(&dev->lb_mutex);
1619}
1620
Eli Cohene126ba92013-07-07 17:25:49 +03001621static struct ib_ucontext *mlx5_ib_alloc_ucontext(struct ib_device *ibdev,
1622 struct ib_udata *udata)
1623{
1624 struct mlx5_ib_dev *dev = to_mdev(ibdev);
Matan Barakb368d7c2015-12-15 20:30:12 +02001625 struct mlx5_ib_alloc_ucontext_req_v2 req = {};
1626 struct mlx5_ib_alloc_ucontext_resp resp = {};
Feras Daoud5c99eae2018-01-16 20:08:41 +02001627 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03001628 struct mlx5_ib_ucontext *context;
Eli Cohen2f5ff262017-01-03 23:55:21 +02001629 struct mlx5_bfreg_info *bfregi;
Eli Cohen78c0f982014-01-30 13:49:48 +02001630 int ver;
Eli Cohene126ba92013-07-07 17:25:49 +03001631 int err;
Majd Dibbinya168a41c2016-01-28 17:51:47 +02001632 size_t min_req_v2 = offsetof(struct mlx5_ib_alloc_ucontext_req_v2,
1633 max_cqe_version);
Yonatan Cohen25bb36e2018-06-19 08:47:24 +03001634 u32 dump_fill_mkey;
Eli Cohenb037c292017-01-03 23:55:26 +02001635 bool lib_uar_4k;
Eli Cohene126ba92013-07-07 17:25:49 +03001636
1637 if (!dev->ib_active)
1638 return ERR_PTR(-EAGAIN);
1639
Amrani, Rame0931112017-06-27 17:04:42 +03001640 if (udata->inlen == sizeof(struct mlx5_ib_alloc_ucontext_req))
Eli Cohen78c0f982014-01-30 13:49:48 +02001641 ver = 0;
Amrani, Rame0931112017-06-27 17:04:42 +03001642 else if (udata->inlen >= min_req_v2)
Eli Cohen78c0f982014-01-30 13:49:48 +02001643 ver = 2;
1644 else
1645 return ERR_PTR(-EINVAL);
1646
Amrani, Rame0931112017-06-27 17:04:42 +03001647 err = ib_copy_from_udata(&req, udata, min(udata->inlen, sizeof(req)));
Eli Cohene126ba92013-07-07 17:25:49 +03001648 if (err)
1649 return ERR_PTR(err);
1650
Yishai Hadasa8b92ca2018-06-17 12:59:57 +03001651 if (req.flags & ~MLX5_IB_ALLOC_UCTX_DEVX)
1652 return ERR_PTR(-EOPNOTSUPP);
Eli Cohen78c0f982014-01-30 13:49:48 +02001653
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001654 if (req.comp_mask || req.reserved0 || req.reserved1 || req.reserved2)
Matan Barakb368d7c2015-12-15 20:30:12 +02001655 return ERR_PTR(-EOPNOTSUPP);
1656
Eli Cohen2f5ff262017-01-03 23:55:21 +02001657 req.total_num_bfregs = ALIGN(req.total_num_bfregs,
1658 MLX5_NON_FP_BFREGS_PER_UAR);
1659 if (req.num_low_latency_bfregs > req.total_num_bfregs - 1)
Eli Cohene126ba92013-07-07 17:25:49 +03001660 return ERR_PTR(-EINVAL);
1661
Saeed Mahameed938fe832015-05-28 22:28:41 +03001662 resp.qp_tab_size = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp);
Noa Osherovich2cc6ad52016-06-04 15:15:33 +03001663 if (mlx5_core_is_pf(dev->mdev) && MLX5_CAP_GEN(dev->mdev, bf))
1664 resp.bf_reg_size = 1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size);
Daniel Jurgensb47bd6e2016-10-25 18:36:24 +03001665 resp.cache_line_size = cache_line_size();
Saeed Mahameed938fe832015-05-28 22:28:41 +03001666 resp.max_sq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq);
1667 resp.max_rq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq);
1668 resp.max_send_wqebb = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1669 resp.max_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1670 resp.max_srq_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz);
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001671 resp.cqe_version = min_t(__u8,
1672 (__u8)MLX5_CAP_GEN(dev->mdev, cqe_version),
1673 req.max_cqe_version);
Eli Cohen30aa60b2017-01-03 23:55:27 +02001674 resp.log_uar_size = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
1675 MLX5_ADAPTER_PAGE_SHIFT : PAGE_SHIFT;
1676 resp.num_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
1677 MLX5_CAP_GEN(dev->mdev, num_of_uars_per_page) : 1;
Matan Barakb368d7c2015-12-15 20:30:12 +02001678 resp.response_length = min(offsetof(typeof(resp), response_length) +
1679 sizeof(resp.response_length), udata->outlen);
Eli Cohene126ba92013-07-07 17:25:49 +03001680
Matan Barakc03faa52018-03-28 09:27:54 +03001681 if (mlx5_accel_ipsec_device_caps(dev->mdev) & MLX5_ACCEL_IPSEC_CAP_DEVICE) {
1682 if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_EGRESS))
1683 resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM;
1684 if (mlx5_accel_ipsec_device_caps(dev->mdev) & MLX5_ACCEL_IPSEC_CAP_REQUIRED_METADATA)
1685 resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_REQ_METADATA;
1686 if (MLX5_CAP_FLOWTABLE(dev->mdev, flow_table_properties_nic_receive.ft_field_support.outer_esp_spi))
1687 resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_SPI_STEERING;
1688 if (mlx5_accel_ipsec_device_caps(dev->mdev) & MLX5_ACCEL_IPSEC_CAP_TX_IV_IS_ESN)
1689 resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_TX_IV_IS_ESN;
1690 /* MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_FULL_OFFLOAD is currently always 0 */
1691 }
1692
Eli Cohene126ba92013-07-07 17:25:49 +03001693 context = kzalloc(sizeof(*context), GFP_KERNEL);
1694 if (!context)
1695 return ERR_PTR(-ENOMEM);
1696
Eli Cohen30aa60b2017-01-03 23:55:27 +02001697 lib_uar_4k = req.lib_caps & MLX5_LIB_CAP_4K_UAR;
Eli Cohen2f5ff262017-01-03 23:55:21 +02001698 bfregi = &context->bfregi;
Eli Cohenb037c292017-01-03 23:55:26 +02001699
1700 /* updates req->total_num_bfregs */
Yishai Hadas31a78a52017-12-24 16:31:34 +02001701 err = calc_total_bfregs(dev, lib_uar_4k, &req, bfregi);
Eli Cohenb037c292017-01-03 23:55:26 +02001702 if (err)
1703 goto out_ctx;
1704
Eli Cohen2f5ff262017-01-03 23:55:21 +02001705 mutex_init(&bfregi->lock);
Eli Cohenb037c292017-01-03 23:55:26 +02001706 bfregi->lib_uar_4k = lib_uar_4k;
Yishai Hadas31a78a52017-12-24 16:31:34 +02001707 bfregi->count = kcalloc(bfregi->total_num_bfregs, sizeof(*bfregi->count),
Eli Cohenb037c292017-01-03 23:55:26 +02001708 GFP_KERNEL);
1709 if (!bfregi->count) {
Eli Cohene126ba92013-07-07 17:25:49 +03001710 err = -ENOMEM;
1711 goto out_ctx;
1712 }
1713
Eli Cohenb037c292017-01-03 23:55:26 +02001714 bfregi->sys_pages = kcalloc(bfregi->num_sys_pages,
1715 sizeof(*bfregi->sys_pages),
1716 GFP_KERNEL);
1717 if (!bfregi->sys_pages) {
Eli Cohene126ba92013-07-07 17:25:49 +03001718 err = -ENOMEM;
Eli Cohenb037c292017-01-03 23:55:26 +02001719 goto out_count;
Eli Cohene126ba92013-07-07 17:25:49 +03001720 }
1721
Eli Cohenb037c292017-01-03 23:55:26 +02001722 err = allocate_uars(dev, context);
1723 if (err)
1724 goto out_sys_pages;
Eli Cohene126ba92013-07-07 17:25:49 +03001725
Haggai Eranb4cfe442014-12-11 17:04:26 +02001726#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
1727 context->ibucontext.invalidate_range = &mlx5_ib_invalidate_range;
1728#endif
1729
Leon Romanovskycfdeb892018-06-19 10:39:06 +03001730 err = mlx5_ib_alloc_transport_domain(dev, &context->tdn);
1731 if (err)
1732 goto out_uars;
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001733
Yishai Hadasa8b92ca2018-06-17 12:59:57 +03001734 if (req.flags & MLX5_IB_ALLOC_UCTX_DEVX) {
1735 /* Block DEVX on Infiniband as of SELinux */
1736 if (mlx5_ib_port_link_layer(ibdev, 1) != IB_LINK_LAYER_ETHERNET) {
1737 err = -EPERM;
1738 goto out_td;
1739 }
1740
1741 err = mlx5_ib_devx_create(dev, context);
1742 if (err)
1743 goto out_td;
1744 }
1745
Yonatan Cohen25bb36e2018-06-19 08:47:24 +03001746 if (MLX5_CAP_GEN(dev->mdev, dump_fill_mkey)) {
1747 err = mlx5_cmd_dump_fill_mkey(dev->mdev, &dump_fill_mkey);
1748 if (err)
Jason Gunthorpe8193abb2018-07-04 13:19:46 -06001749 goto out_mdev;
Yonatan Cohen25bb36e2018-06-19 08:47:24 +03001750 }
1751
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001752 INIT_LIST_HEAD(&context->vma_private_list);
Majd Dibbinyad9a3662017-12-24 13:54:56 +02001753 mutex_init(&context->vma_private_list_mutex);
Eli Cohene126ba92013-07-07 17:25:49 +03001754 INIT_LIST_HEAD(&context->db_page_list);
1755 mutex_init(&context->db_page_mutex);
1756
Eli Cohen2f5ff262017-01-03 23:55:21 +02001757 resp.tot_bfregs = req.total_num_bfregs;
Daniel Jurgens508562d2018-01-04 17:25:34 +02001758 resp.num_ports = dev->num_ports;
Matan Barakb368d7c2015-12-15 20:30:12 +02001759
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001760 if (field_avail(typeof(resp), cqe_version, udata->outlen))
1761 resp.response_length += sizeof(resp.cqe_version);
Matan Barakb368d7c2015-12-15 20:30:12 +02001762
Bodong Wang402ca532016-06-17 15:02:20 +03001763 if (field_avail(typeof(resp), cmds_supp_uhw, udata->outlen)) {
Moni Shoua6ad279c52016-11-23 08:23:23 +02001764 resp.cmds_supp_uhw |= MLX5_USER_CMDS_SUPP_UHW_QUERY_DEVICE |
1765 MLX5_USER_CMDS_SUPP_UHW_CREATE_AH;
Bodong Wang402ca532016-06-17 15:02:20 +03001766 resp.response_length += sizeof(resp.cmds_supp_uhw);
1767 }
1768
Or Gerlitz78984892016-11-30 20:33:33 +02001769 if (field_avail(typeof(resp), eth_min_inline, udata->outlen)) {
1770 if (mlx5_ib_port_link_layer(ibdev, 1) == IB_LINK_LAYER_ETHERNET) {
1771 mlx5_query_min_inline(dev->mdev, &resp.eth_min_inline);
1772 resp.eth_min_inline++;
1773 }
1774 resp.response_length += sizeof(resp.eth_min_inline);
1775 }
1776
Feras Daoud5c99eae2018-01-16 20:08:41 +02001777 if (field_avail(typeof(resp), clock_info_versions, udata->outlen)) {
1778 if (mdev->clock_info)
1779 resp.clock_info_versions = BIT(MLX5_IB_CLOCK_INFO_V1);
1780 resp.response_length += sizeof(resp.clock_info_versions);
1781 }
1782
Noa Osherovichbc5c6ee2016-06-04 15:15:31 +03001783 /*
1784 * We don't want to expose information from the PCI bar that is located
1785 * after 4096 bytes, so if the arch only supports larger pages, let's
1786 * pretend we don't support reading the HCA's core clock. This is also
1787 * forced by mmap function.
1788 */
Eli Cohende8d6e02017-01-03 23:55:19 +02001789 if (field_avail(typeof(resp), hca_core_clock_offset, udata->outlen)) {
1790 if (PAGE_SIZE <= 4096) {
1791 resp.comp_mask |=
1792 MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_CORE_CLOCK_OFFSET;
1793 resp.hca_core_clock_offset =
1794 offsetof(struct mlx5_init_seg, internal_timer_h) % PAGE_SIZE;
1795 }
Feras Daoud5c99eae2018-01-16 20:08:41 +02001796 resp.response_length += sizeof(resp.hca_core_clock_offset);
Matan Barakb368d7c2015-12-15 20:30:12 +02001797 }
1798
Eli Cohen30aa60b2017-01-03 23:55:27 +02001799 if (field_avail(typeof(resp), log_uar_size, udata->outlen))
1800 resp.response_length += sizeof(resp.log_uar_size);
1801
1802 if (field_avail(typeof(resp), num_uars_per_page, udata->outlen))
1803 resp.response_length += sizeof(resp.num_uars_per_page);
1804
Yishai Hadas31a78a52017-12-24 16:31:34 +02001805 if (field_avail(typeof(resp), num_dyn_bfregs, udata->outlen)) {
1806 resp.num_dyn_bfregs = bfregi->num_dyn_bfregs;
1807 resp.response_length += sizeof(resp.num_dyn_bfregs);
1808 }
1809
Yonatan Cohen25bb36e2018-06-19 08:47:24 +03001810 if (field_avail(typeof(resp), dump_fill_mkey, udata->outlen)) {
1811 if (MLX5_CAP_GEN(dev->mdev, dump_fill_mkey)) {
1812 resp.dump_fill_mkey = dump_fill_mkey;
1813 resp.comp_mask |=
1814 MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_DUMP_FILL_MKEY;
1815 }
1816 resp.response_length += sizeof(resp.dump_fill_mkey);
1817 }
1818
Matan Barakb368d7c2015-12-15 20:30:12 +02001819 err = ib_copy_to_udata(udata, &resp, resp.response_length);
Eli Cohene126ba92013-07-07 17:25:49 +03001820 if (err)
Yishai Hadasa8b92ca2018-06-17 12:59:57 +03001821 goto out_mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03001822
Eli Cohen2f5ff262017-01-03 23:55:21 +02001823 bfregi->ver = ver;
1824 bfregi->num_low_latency_bfregs = req.num_low_latency_bfregs;
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001825 context->cqe_version = resp.cqe_version;
Eli Cohen30aa60b2017-01-03 23:55:27 +02001826 context->lib_caps = req.lib_caps;
1827 print_lib_caps(dev, context->lib_caps);
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001828
Majd Dibbinyc6a21c32018-08-28 14:29:05 +03001829 if (mlx5_lag_is_active(dev->mdev)) {
1830 u8 port = mlx5_core_native_port_num(dev->mdev);
1831
1832 atomic_set(&context->tx_port_affinity,
1833 atomic_add_return(
1834 1, &dev->roce[port].tx_port_affinity));
1835 }
1836
Eli Cohene126ba92013-07-07 17:25:49 +03001837 return &context->ibucontext;
1838
Yishai Hadasa8b92ca2018-06-17 12:59:57 +03001839out_mdev:
1840 if (req.flags & MLX5_IB_ALLOC_UCTX_DEVX)
1841 mlx5_ib_devx_destroy(dev, context);
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001842out_td:
Leon Romanovskycfdeb892018-06-19 10:39:06 +03001843 mlx5_ib_dealloc_transport_domain(dev, context->tdn);
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001844
Eli Cohene126ba92013-07-07 17:25:49 +03001845out_uars:
Eli Cohenb037c292017-01-03 23:55:26 +02001846 deallocate_uars(dev, context);
1847
1848out_sys_pages:
1849 kfree(bfregi->sys_pages);
1850
Eli Cohene126ba92013-07-07 17:25:49 +03001851out_count:
Eli Cohen2f5ff262017-01-03 23:55:21 +02001852 kfree(bfregi->count);
Eli Cohene126ba92013-07-07 17:25:49 +03001853
Eli Cohene126ba92013-07-07 17:25:49 +03001854out_ctx:
1855 kfree(context);
Eli Cohenb037c292017-01-03 23:55:26 +02001856
Eli Cohene126ba92013-07-07 17:25:49 +03001857 return ERR_PTR(err);
1858}
1859
1860static int mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
1861{
1862 struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1863 struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
Eli Cohenb037c292017-01-03 23:55:26 +02001864 struct mlx5_bfreg_info *bfregi;
Eli Cohene126ba92013-07-07 17:25:49 +03001865
Yishai Hadasa8b92ca2018-06-17 12:59:57 +03001866 if (context->devx_uid)
1867 mlx5_ib_devx_destroy(dev, context);
1868
Eli Cohenb037c292017-01-03 23:55:26 +02001869 bfregi = &context->bfregi;
Leon Romanovskycfdeb892018-06-19 10:39:06 +03001870 mlx5_ib_dealloc_transport_domain(dev, context->tdn);
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001871
Eli Cohenb037c292017-01-03 23:55:26 +02001872 deallocate_uars(dev, context);
1873 kfree(bfregi->sys_pages);
Eli Cohen2f5ff262017-01-03 23:55:21 +02001874 kfree(bfregi->count);
Eli Cohene126ba92013-07-07 17:25:49 +03001875 kfree(context);
1876
1877 return 0;
1878}
1879
Eli Cohenb037c292017-01-03 23:55:26 +02001880static phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev,
Yishai Hadas4ed131d2017-12-24 16:31:35 +02001881 int uar_idx)
Eli Cohene126ba92013-07-07 17:25:49 +03001882{
Eli Cohenb037c292017-01-03 23:55:26 +02001883 int fw_uars_per_page;
1884
1885 fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ? MLX5_UARS_IN_PAGE : 1;
1886
Yishai Hadas4ed131d2017-12-24 16:31:35 +02001887 return (pci_resource_start(dev->mdev->pdev, 0) >> PAGE_SHIFT) + uar_idx / fw_uars_per_page;
Eli Cohene126ba92013-07-07 17:25:49 +03001888}
1889
1890static int get_command(unsigned long offset)
1891{
1892 return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK;
1893}
1894
1895static int get_arg(unsigned long offset)
1896{
1897 return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1);
1898}
1899
1900static int get_index(unsigned long offset)
1901{
1902 return get_arg(offset);
1903}
1904
Yishai Hadas4ed131d2017-12-24 16:31:35 +02001905/* Index resides in an extra byte to enable larger values than 255 */
1906static int get_extended_index(unsigned long offset)
1907{
1908 return get_arg(offset) | ((offset >> 16) & 0xff) << 8;
1909}
1910
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001911static void mlx5_ib_vma_open(struct vm_area_struct *area)
1912{
1913 /* vma_open is called when a new VMA is created on top of our VMA. This
1914 * is done through either mremap flow or split_vma (usually due to
1915 * mlock, madvise, munmap, etc.) We do not support a clone of the VMA,
1916 * as this VMA is strongly hardware related. Therefore we set the
1917 * vm_ops of the newly created/cloned VMA to NULL, to prevent it from
1918 * calling us again and trying to do incorrect actions. We assume that
1919 * the original VMA size is exactly a single page, and therefore all
1920 * "splitting" operation will not happen to it.
1921 */
1922 area->vm_ops = NULL;
1923}
1924
1925static void mlx5_ib_vma_close(struct vm_area_struct *area)
1926{
1927 struct mlx5_ib_vma_private_data *mlx5_ib_vma_priv_data;
1928
1929 /* It's guaranteed that all VMAs opened on a FD are closed before the
1930 * file itself is closed, therefore no sync is needed with the regular
1931 * closing flow. (e.g. mlx5 ib_dealloc_ucontext)
1932 * However need a sync with accessing the vma as part of
1933 * mlx5_ib_disassociate_ucontext.
1934 * The close operation is usually called under mm->mmap_sem except when
1935 * process is exiting.
1936 * The exiting case is handled explicitly as part of
1937 * mlx5_ib_disassociate_ucontext.
1938 */
1939 mlx5_ib_vma_priv_data = (struct mlx5_ib_vma_private_data *)area->vm_private_data;
1940
1941 /* setting the vma context pointer to null in the mlx5_ib driver's
1942 * private data, to protect a race condition in
1943 * mlx5_ib_disassociate_ucontext().
1944 */
1945 mlx5_ib_vma_priv_data->vma = NULL;
Majd Dibbinyad9a3662017-12-24 13:54:56 +02001946 mutex_lock(mlx5_ib_vma_priv_data->vma_private_list_mutex);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001947 list_del(&mlx5_ib_vma_priv_data->list);
Majd Dibbinyad9a3662017-12-24 13:54:56 +02001948 mutex_unlock(mlx5_ib_vma_priv_data->vma_private_list_mutex);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001949 kfree(mlx5_ib_vma_priv_data);
1950}
1951
1952static const struct vm_operations_struct mlx5_ib_vm_ops = {
1953 .open = mlx5_ib_vma_open,
1954 .close = mlx5_ib_vma_close
1955};
1956
1957static int mlx5_ib_set_vma_data(struct vm_area_struct *vma,
1958 struct mlx5_ib_ucontext *ctx)
1959{
1960 struct mlx5_ib_vma_private_data *vma_prv;
1961 struct list_head *vma_head = &ctx->vma_private_list;
1962
1963 vma_prv = kzalloc(sizeof(*vma_prv), GFP_KERNEL);
1964 if (!vma_prv)
1965 return -ENOMEM;
1966
1967 vma_prv->vma = vma;
Majd Dibbinyad9a3662017-12-24 13:54:56 +02001968 vma_prv->vma_private_list_mutex = &ctx->vma_private_list_mutex;
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001969 vma->vm_private_data = vma_prv;
1970 vma->vm_ops = &mlx5_ib_vm_ops;
1971
Majd Dibbinyad9a3662017-12-24 13:54:56 +02001972 mutex_lock(&ctx->vma_private_list_mutex);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001973 list_add(&vma_prv->list, vma_head);
Majd Dibbinyad9a3662017-12-24 13:54:56 +02001974 mutex_unlock(&ctx->vma_private_list_mutex);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001975
1976 return 0;
1977}
1978
1979static void mlx5_ib_disassociate_ucontext(struct ib_ucontext *ibcontext)
1980{
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001981 struct vm_area_struct *vma;
1982 struct mlx5_ib_vma_private_data *vma_private, *n;
1983 struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001984
Majd Dibbinyad9a3662017-12-24 13:54:56 +02001985 mutex_lock(&context->vma_private_list_mutex);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001986 list_for_each_entry_safe(vma_private, n, &context->vma_private_list,
1987 list) {
1988 vma = vma_private->vma;
Leon Romanovsky2cb40792018-05-29 15:14:05 +03001989 zap_vma_ptes(vma, vma->vm_start, PAGE_SIZE);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001990 /* context going to be destroyed, should
1991 * not access ops any more.
1992 */
Maor Gottlieb13776612017-03-29 06:03:03 +03001993 vma->vm_flags &= ~(VM_SHARED | VM_MAYSHARE);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001994 vma->vm_ops = NULL;
1995 list_del(&vma_private->list);
1996 kfree(vma_private);
1997 }
Majd Dibbinyad9a3662017-12-24 13:54:56 +02001998 mutex_unlock(&context->vma_private_list_mutex);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001999}
2000
Guy Levi37aa5c32016-04-27 16:49:50 +03002001static inline char *mmap_cmd2str(enum mlx5_ib_mmap_cmd cmd)
2002{
2003 switch (cmd) {
2004 case MLX5_IB_MMAP_WC_PAGE:
2005 return "WC";
2006 case MLX5_IB_MMAP_REGULAR_PAGE:
2007 return "best effort WC";
2008 case MLX5_IB_MMAP_NC_PAGE:
2009 return "NC";
Ariel Levkovich24da0012018-04-05 18:53:27 +03002010 case MLX5_IB_MMAP_DEVICE_MEM:
2011 return "Device Memory";
Guy Levi37aa5c32016-04-27 16:49:50 +03002012 default:
2013 return NULL;
2014 }
2015}
2016
Feras Daoud5c99eae2018-01-16 20:08:41 +02002017static int mlx5_ib_mmap_clock_info_page(struct mlx5_ib_dev *dev,
2018 struct vm_area_struct *vma,
2019 struct mlx5_ib_ucontext *context)
2020{
2021 phys_addr_t pfn;
2022 int err;
2023
2024 if (vma->vm_end - vma->vm_start != PAGE_SIZE)
2025 return -EINVAL;
2026
2027 if (get_index(vma->vm_pgoff) != MLX5_IB_CLOCK_INFO_V1)
2028 return -EOPNOTSUPP;
2029
2030 if (vma->vm_flags & VM_WRITE)
2031 return -EPERM;
2032
2033 if (!dev->mdev->clock_info_page)
2034 return -EOPNOTSUPP;
2035
2036 pfn = page_to_pfn(dev->mdev->clock_info_page);
2037 err = remap_pfn_range(vma, vma->vm_start, pfn, PAGE_SIZE,
2038 vma->vm_page_prot);
2039 if (err)
2040 return err;
2041
Feras Daoud5c99eae2018-01-16 20:08:41 +02002042 return mlx5_ib_set_vma_data(vma, context);
2043}
2044
Guy Levi37aa5c32016-04-27 16:49:50 +03002045static int uar_mmap(struct mlx5_ib_dev *dev, enum mlx5_ib_mmap_cmd cmd,
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03002046 struct vm_area_struct *vma,
2047 struct mlx5_ib_ucontext *context)
Guy Levi37aa5c32016-04-27 16:49:50 +03002048{
Eli Cohen2f5ff262017-01-03 23:55:21 +02002049 struct mlx5_bfreg_info *bfregi = &context->bfregi;
Guy Levi37aa5c32016-04-27 16:49:50 +03002050 int err;
2051 unsigned long idx;
Kamal Heibaa09ea62018-07-19 00:05:32 +03002052 phys_addr_t pfn;
Guy Levi37aa5c32016-04-27 16:49:50 +03002053 pgprot_t prot;
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002054 u32 bfreg_dyn_idx = 0;
2055 u32 uar_index;
2056 int dyn_uar = (cmd == MLX5_IB_MMAP_ALLOC_WC);
2057 int max_valid_idx = dyn_uar ? bfregi->num_sys_pages :
2058 bfregi->num_static_sys_pages;
Eli Cohenb037c292017-01-03 23:55:26 +02002059
2060 if (vma->vm_end - vma->vm_start != PAGE_SIZE)
2061 return -EINVAL;
2062
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002063 if (dyn_uar)
2064 idx = get_extended_index(vma->vm_pgoff) + bfregi->num_static_sys_pages;
2065 else
2066 idx = get_index(vma->vm_pgoff);
2067
2068 if (idx >= max_valid_idx) {
2069 mlx5_ib_warn(dev, "invalid uar index %lu, max=%d\n",
2070 idx, max_valid_idx);
Eli Cohenb037c292017-01-03 23:55:26 +02002071 return -EINVAL;
2072 }
Guy Levi37aa5c32016-04-27 16:49:50 +03002073
2074 switch (cmd) {
2075 case MLX5_IB_MMAP_WC_PAGE:
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002076 case MLX5_IB_MMAP_ALLOC_WC:
Guy Levi37aa5c32016-04-27 16:49:50 +03002077/* Some architectures don't support WC memory */
2078#if defined(CONFIG_X86)
2079 if (!pat_enabled())
2080 return -EPERM;
2081#elif !(defined(CONFIG_PPC) || (defined(CONFIG_ARM) && defined(CONFIG_MMU)))
2082 return -EPERM;
2083#endif
2084 /* fall through */
2085 case MLX5_IB_MMAP_REGULAR_PAGE:
2086 /* For MLX5_IB_MMAP_REGULAR_PAGE do the best effort to get WC */
2087 prot = pgprot_writecombine(vma->vm_page_prot);
2088 break;
2089 case MLX5_IB_MMAP_NC_PAGE:
2090 prot = pgprot_noncached(vma->vm_page_prot);
2091 break;
2092 default:
2093 return -EINVAL;
2094 }
2095
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002096 if (dyn_uar) {
2097 int uars_per_page;
2098
2099 uars_per_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k);
2100 bfreg_dyn_idx = idx * (uars_per_page * MLX5_NON_FP_BFREGS_PER_UAR);
2101 if (bfreg_dyn_idx >= bfregi->total_num_bfregs) {
2102 mlx5_ib_warn(dev, "invalid bfreg_dyn_idx %u, max=%u\n",
2103 bfreg_dyn_idx, bfregi->total_num_bfregs);
2104 return -EINVAL;
2105 }
2106
2107 mutex_lock(&bfregi->lock);
2108 /* Fail if uar already allocated, first bfreg index of each
2109 * page holds its count.
2110 */
2111 if (bfregi->count[bfreg_dyn_idx]) {
2112 mlx5_ib_warn(dev, "wrong offset, idx %lu is busy, bfregn=%u\n", idx, bfreg_dyn_idx);
2113 mutex_unlock(&bfregi->lock);
2114 return -EINVAL;
2115 }
2116
2117 bfregi->count[bfreg_dyn_idx]++;
2118 mutex_unlock(&bfregi->lock);
2119
2120 err = mlx5_cmd_alloc_uar(dev->mdev, &uar_index);
2121 if (err) {
2122 mlx5_ib_warn(dev, "UAR alloc failed\n");
2123 goto free_bfreg;
2124 }
2125 } else {
2126 uar_index = bfregi->sys_pages[idx];
2127 }
2128
2129 pfn = uar_index2pfn(dev, uar_index);
Guy Levi37aa5c32016-04-27 16:49:50 +03002130 mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn %pa\n", idx, &pfn);
2131
2132 vma->vm_page_prot = prot;
2133 err = io_remap_pfn_range(vma, vma->vm_start, pfn,
2134 PAGE_SIZE, vma->vm_page_prot);
2135 if (err) {
Leon Romanovsky8f062282018-05-22 08:31:03 +03002136 mlx5_ib_err(dev,
2137 "io_remap_pfn_range failed with error=%d, mmap_cmd=%s\n",
2138 err, mmap_cmd2str(cmd));
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002139 err = -EAGAIN;
2140 goto err;
Guy Levi37aa5c32016-04-27 16:49:50 +03002141 }
2142
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002143 err = mlx5_ib_set_vma_data(vma, context);
2144 if (err)
2145 goto err;
2146
2147 if (dyn_uar)
2148 bfregi->sys_pages[idx] = uar_index;
2149 return 0;
2150
2151err:
2152 if (!dyn_uar)
2153 return err;
2154
2155 mlx5_cmd_free_uar(dev->mdev, idx);
2156
2157free_bfreg:
2158 mlx5_ib_free_bfreg(dev, bfregi, bfreg_dyn_idx);
2159
2160 return err;
Guy Levi37aa5c32016-04-27 16:49:50 +03002161}
2162
Ariel Levkovich24da0012018-04-05 18:53:27 +03002163static int dm_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
2164{
2165 struct mlx5_ib_ucontext *mctx = to_mucontext(context);
2166 struct mlx5_ib_dev *dev = to_mdev(context->device);
2167 u16 page_idx = get_extended_index(vma->vm_pgoff);
2168 size_t map_size = vma->vm_end - vma->vm_start;
2169 u32 npages = map_size >> PAGE_SHIFT;
2170 phys_addr_t pfn;
2171 pgprot_t prot;
2172
2173 if (find_next_zero_bit(mctx->dm_pages, page_idx + npages, page_idx) !=
2174 page_idx + npages)
2175 return -EINVAL;
2176
2177 pfn = ((pci_resource_start(dev->mdev->pdev, 0) +
2178 MLX5_CAP64_DEV_MEM(dev->mdev, memic_bar_start_addr)) >>
2179 PAGE_SHIFT) +
2180 page_idx;
2181 prot = pgprot_writecombine(vma->vm_page_prot);
2182 vma->vm_page_prot = prot;
2183
2184 if (io_remap_pfn_range(vma, vma->vm_start, pfn, map_size,
2185 vma->vm_page_prot))
2186 return -EAGAIN;
2187
2188 return mlx5_ib_set_vma_data(vma, mctx);
2189}
2190
Eli Cohene126ba92013-07-07 17:25:49 +03002191static int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma)
2192{
2193 struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
2194 struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
Eli Cohene126ba92013-07-07 17:25:49 +03002195 unsigned long command;
Eli Cohene126ba92013-07-07 17:25:49 +03002196 phys_addr_t pfn;
2197
2198 command = get_command(vma->vm_pgoff);
2199 switch (command) {
Guy Levi37aa5c32016-04-27 16:49:50 +03002200 case MLX5_IB_MMAP_WC_PAGE:
2201 case MLX5_IB_MMAP_NC_PAGE:
Eli Cohene126ba92013-07-07 17:25:49 +03002202 case MLX5_IB_MMAP_REGULAR_PAGE:
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002203 case MLX5_IB_MMAP_ALLOC_WC:
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03002204 return uar_mmap(dev, command, vma, context);
Eli Cohene126ba92013-07-07 17:25:49 +03002205
2206 case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES:
2207 return -ENOSYS;
2208
Matan Barakd69e3bc2015-12-15 20:30:13 +02002209 case MLX5_IB_MMAP_CORE_CLOCK:
Matan Barakd69e3bc2015-12-15 20:30:13 +02002210 if (vma->vm_end - vma->vm_start != PAGE_SIZE)
2211 return -EINVAL;
2212
Matan Barak6cbac1e2016-04-14 16:52:10 +03002213 if (vma->vm_flags & VM_WRITE)
Matan Barakd69e3bc2015-12-15 20:30:13 +02002214 return -EPERM;
2215
2216 /* Don't expose to user-space information it shouldn't have */
2217 if (PAGE_SIZE > 4096)
2218 return -EOPNOTSUPP;
2219
2220 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
2221 pfn = (dev->mdev->iseg_base +
2222 offsetof(struct mlx5_init_seg, internal_timer_h)) >>
2223 PAGE_SHIFT;
2224 if (io_remap_pfn_range(vma, vma->vm_start, pfn,
2225 PAGE_SIZE, vma->vm_page_prot))
2226 return -EAGAIN;
Matan Barakd69e3bc2015-12-15 20:30:13 +02002227 break;
Feras Daoud5c99eae2018-01-16 20:08:41 +02002228 case MLX5_IB_MMAP_CLOCK_INFO:
2229 return mlx5_ib_mmap_clock_info_page(dev, vma, context);
Matan Barakd69e3bc2015-12-15 20:30:13 +02002230
Ariel Levkovich24da0012018-04-05 18:53:27 +03002231 case MLX5_IB_MMAP_DEVICE_MEM:
2232 return dm_mmap(ibcontext, vma);
2233
Eli Cohene126ba92013-07-07 17:25:49 +03002234 default:
2235 return -EINVAL;
2236 }
2237
2238 return 0;
2239}
2240
Ariel Levkovich24da0012018-04-05 18:53:27 +03002241struct ib_dm *mlx5_ib_alloc_dm(struct ib_device *ibdev,
2242 struct ib_ucontext *context,
2243 struct ib_dm_alloc_attr *attr,
2244 struct uverbs_attr_bundle *attrs)
2245{
2246 u64 act_size = roundup(attr->length, MLX5_MEMIC_BASE_SIZE);
2247 struct mlx5_memic *memic = &to_mdev(ibdev)->memic;
2248 phys_addr_t memic_addr;
2249 struct mlx5_ib_dm *dm;
2250 u64 start_offset;
2251 u32 page_idx;
2252 int err;
2253
2254 dm = kzalloc(sizeof(*dm), GFP_KERNEL);
2255 if (!dm)
2256 return ERR_PTR(-ENOMEM);
2257
2258 mlx5_ib_dbg(to_mdev(ibdev), "alloc_memic req: user_length=0x%llx act_length=0x%llx log_alignment=%d\n",
2259 attr->length, act_size, attr->alignment);
2260
2261 err = mlx5_cmd_alloc_memic(memic, &memic_addr,
2262 act_size, attr->alignment);
2263 if (err)
2264 goto err_free;
2265
2266 start_offset = memic_addr & ~PAGE_MASK;
2267 page_idx = (memic_addr - pci_resource_start(memic->dev->pdev, 0) -
2268 MLX5_CAP64_DEV_MEM(memic->dev, memic_bar_start_addr)) >>
2269 PAGE_SHIFT;
2270
2271 err = uverbs_copy_to(attrs,
2272 MLX5_IB_ATTR_ALLOC_DM_RESP_START_OFFSET,
2273 &start_offset, sizeof(start_offset));
2274 if (err)
2275 goto err_dealloc;
2276
2277 err = uverbs_copy_to(attrs,
2278 MLX5_IB_ATTR_ALLOC_DM_RESP_PAGE_INDEX,
2279 &page_idx, sizeof(page_idx));
2280 if (err)
2281 goto err_dealloc;
2282
2283 bitmap_set(to_mucontext(context)->dm_pages, page_idx,
2284 DIV_ROUND_UP(act_size, PAGE_SIZE));
2285
2286 dm->dev_addr = memic_addr;
2287
2288 return &dm->ibdm;
2289
2290err_dealloc:
2291 mlx5_cmd_dealloc_memic(memic, memic_addr,
2292 act_size);
2293err_free:
2294 kfree(dm);
2295 return ERR_PTR(err);
2296}
2297
2298int mlx5_ib_dealloc_dm(struct ib_dm *ibdm)
2299{
2300 struct mlx5_memic *memic = &to_mdev(ibdm->device)->memic;
2301 struct mlx5_ib_dm *dm = to_mdm(ibdm);
2302 u64 act_size = roundup(dm->ibdm.length, MLX5_MEMIC_BASE_SIZE);
2303 u32 page_idx;
2304 int ret;
2305
2306 ret = mlx5_cmd_dealloc_memic(memic, dm->dev_addr, act_size);
2307 if (ret)
2308 return ret;
2309
2310 page_idx = (dm->dev_addr - pci_resource_start(memic->dev->pdev, 0) -
2311 MLX5_CAP64_DEV_MEM(memic->dev, memic_bar_start_addr)) >>
2312 PAGE_SHIFT;
2313 bitmap_clear(to_mucontext(ibdm->uobject->context)->dm_pages,
2314 page_idx,
2315 DIV_ROUND_UP(act_size, PAGE_SIZE));
2316
2317 kfree(dm);
2318
2319 return 0;
2320}
2321
Eli Cohene126ba92013-07-07 17:25:49 +03002322static struct ib_pd *mlx5_ib_alloc_pd(struct ib_device *ibdev,
2323 struct ib_ucontext *context,
2324 struct ib_udata *udata)
2325{
2326 struct mlx5_ib_alloc_pd_resp resp;
2327 struct mlx5_ib_pd *pd;
2328 int err;
2329
2330 pd = kmalloc(sizeof(*pd), GFP_KERNEL);
2331 if (!pd)
2332 return ERR_PTR(-ENOMEM);
2333
Jack Morgenstein9603b612014-07-28 23:30:22 +03002334 err = mlx5_core_alloc_pd(to_mdev(ibdev)->mdev, &pd->pdn);
Eli Cohene126ba92013-07-07 17:25:49 +03002335 if (err) {
2336 kfree(pd);
2337 return ERR_PTR(err);
2338 }
2339
2340 if (context) {
2341 resp.pdn = pd->pdn;
2342 if (ib_copy_to_udata(udata, &resp, sizeof(resp))) {
Jack Morgenstein9603b612014-07-28 23:30:22 +03002343 mlx5_core_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn);
Eli Cohene126ba92013-07-07 17:25:49 +03002344 kfree(pd);
2345 return ERR_PTR(-EFAULT);
2346 }
Eli Cohene126ba92013-07-07 17:25:49 +03002347 }
2348
2349 return &pd->ibpd;
2350}
2351
2352static int mlx5_ib_dealloc_pd(struct ib_pd *pd)
2353{
2354 struct mlx5_ib_dev *mdev = to_mdev(pd->device);
2355 struct mlx5_ib_pd *mpd = to_mpd(pd);
2356
Jack Morgenstein9603b612014-07-28 23:30:22 +03002357 mlx5_core_dealloc_pd(mdev->mdev, mpd->pdn);
Eli Cohene126ba92013-07-07 17:25:49 +03002358 kfree(mpd);
2359
2360 return 0;
2361}
2362
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002363enum {
2364 MATCH_CRITERIA_ENABLE_OUTER_BIT,
2365 MATCH_CRITERIA_ENABLE_MISC_BIT,
Ariel Levkovich71c6e862018-05-13 14:33:34 +03002366 MATCH_CRITERIA_ENABLE_INNER_BIT,
2367 MATCH_CRITERIA_ENABLE_MISC2_BIT
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002368};
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002369
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002370#define HEADER_IS_ZERO(match_criteria, headers) \
2371 !(memchr_inv(MLX5_ADDR_OF(fte_match_param, match_criteria, headers), \
2372 0, MLX5_FLD_SZ_BYTES(fte_match_param, headers))) \
2373
2374static u8 get_match_criteria_enable(u32 *match_criteria)
2375{
2376 u8 match_criteria_enable;
2377
2378 match_criteria_enable =
2379 (!HEADER_IS_ZERO(match_criteria, outer_headers)) <<
2380 MATCH_CRITERIA_ENABLE_OUTER_BIT;
2381 match_criteria_enable |=
2382 (!HEADER_IS_ZERO(match_criteria, misc_parameters)) <<
2383 MATCH_CRITERIA_ENABLE_MISC_BIT;
2384 match_criteria_enable |=
2385 (!HEADER_IS_ZERO(match_criteria, inner_headers)) <<
2386 MATCH_CRITERIA_ENABLE_INNER_BIT;
Ariel Levkovich71c6e862018-05-13 14:33:34 +03002387 match_criteria_enable |=
2388 (!HEADER_IS_ZERO(match_criteria, misc_parameters_2)) <<
2389 MATCH_CRITERIA_ENABLE_MISC2_BIT;
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002390
2391 return match_criteria_enable;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002392}
2393
Maor Gottliebca0d4752016-08-30 16:58:35 +03002394static void set_proto(void *outer_c, void *outer_v, u8 mask, u8 val)
2395{
2396 MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_protocol, mask);
2397 MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_protocol, val);
2398}
2399
Daria Velikovsky37da2a02018-05-07 10:20:02 +03002400static void set_flow_label(void *misc_c, void *misc_v, u32 mask, u32 val,
Moses Reuben2d1e6972016-11-14 19:04:52 +02002401 bool inner)
2402{
2403 if (inner) {
2404 MLX5_SET(fte_match_set_misc,
2405 misc_c, inner_ipv6_flow_label, mask);
2406 MLX5_SET(fte_match_set_misc,
2407 misc_v, inner_ipv6_flow_label, val);
2408 } else {
2409 MLX5_SET(fte_match_set_misc,
2410 misc_c, outer_ipv6_flow_label, mask);
2411 MLX5_SET(fte_match_set_misc,
2412 misc_v, outer_ipv6_flow_label, val);
2413 }
2414}
2415
Maor Gottliebca0d4752016-08-30 16:58:35 +03002416static void set_tos(void *outer_c, void *outer_v, u8 mask, u8 val)
2417{
2418 MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_ecn, mask);
2419 MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_ecn, val);
2420 MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_dscp, mask >> 2);
2421 MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_dscp, val >> 2);
2422}
2423
Ariel Levkovich71c6e862018-05-13 14:33:34 +03002424static int check_mpls_supp_fields(u32 field_support, const __be32 *set_mask)
2425{
2426 if (MLX5_GET(fte_match_mpls, set_mask, mpls_label) &&
2427 !(field_support & MLX5_FIELD_SUPPORT_MPLS_LABEL))
2428 return -EOPNOTSUPP;
2429
2430 if (MLX5_GET(fte_match_mpls, set_mask, mpls_exp) &&
2431 !(field_support & MLX5_FIELD_SUPPORT_MPLS_EXP))
2432 return -EOPNOTSUPP;
2433
2434 if (MLX5_GET(fte_match_mpls, set_mask, mpls_s_bos) &&
2435 !(field_support & MLX5_FIELD_SUPPORT_MPLS_S_BOS))
2436 return -EOPNOTSUPP;
2437
2438 if (MLX5_GET(fte_match_mpls, set_mask, mpls_ttl) &&
2439 !(field_support & MLX5_FIELD_SUPPORT_MPLS_TTL))
2440 return -EOPNOTSUPP;
2441
2442 return 0;
2443}
2444
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002445#define LAST_ETH_FIELD vlan_tag
2446#define LAST_IB_FIELD sl
Maor Gottliebca0d4752016-08-30 16:58:35 +03002447#define LAST_IPV4_FIELD tos
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002448#define LAST_IPV6_FIELD traffic_class
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002449#define LAST_TCP_UDP_FIELD src_port
Moses Reubenffb30d82016-11-14 19:04:50 +02002450#define LAST_TUNNEL_FIELD tunnel_id
Moses Reuben2ac693f2017-01-18 14:59:50 +02002451#define LAST_FLOW_TAG_FIELD tag_id
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002452#define LAST_DROP_FIELD size
Raed Salem3b3233f2018-05-31 16:43:39 +03002453#define LAST_COUNTERS_FIELD counters
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002454
2455/* Field is the last supported field */
2456#define FIELDS_NOT_SUPPORTED(filter, field)\
2457 memchr_inv((void *)&filter.field +\
2458 sizeof(filter.field), 0,\
2459 sizeof(filter) -\
2460 offsetof(typeof(filter), field) -\
2461 sizeof(filter.field))
2462
Mark Bloch2ea26202018-09-06 17:27:03 +03002463int parse_flow_flow_action(struct mlx5_ib_flow_action *maction,
2464 bool is_egress,
2465 struct mlx5_flow_act *action)
Aviad Yehezkel802c2122018-03-28 09:27:53 +03002466{
Aviad Yehezkel802c2122018-03-28 09:27:53 +03002467
2468 switch (maction->ib_action.type) {
2469 case IB_FLOW_ACTION_ESP:
Mark Bloch501f14e2018-09-06 17:27:04 +03002470 if (action->action & (MLX5_FLOW_CONTEXT_ACTION_ENCRYPT |
2471 MLX5_FLOW_CONTEXT_ACTION_DECRYPT))
2472 return -EINVAL;
Aviad Yehezkel802c2122018-03-28 09:27:53 +03002473 /* Currently only AES_GCM keymat is supported by the driver */
2474 action->esp_id = (uintptr_t)maction->esp_aes_gcm.ctx;
Mark Bloch2ea26202018-09-06 17:27:03 +03002475 action->action |= is_egress ?
Aviad Yehezkel802c2122018-03-28 09:27:53 +03002476 MLX5_FLOW_CONTEXT_ACTION_ENCRYPT :
2477 MLX5_FLOW_CONTEXT_ACTION_DECRYPT;
2478 return 0;
Mark Blochb1085be2018-09-02 12:51:32 +03002479 case IB_FLOW_ACTION_UNSPECIFIED:
2480 if (maction->flow_action_raw.sub_type ==
2481 MLX5_IB_FLOW_ACTION_MODIFY_HEADER) {
Mark Bloch501f14e2018-09-06 17:27:04 +03002482 if (action->action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR)
2483 return -EINVAL;
Mark Blochb1085be2018-09-02 12:51:32 +03002484 action->action |= MLX5_FLOW_CONTEXT_ACTION_MOD_HDR;
2485 action->modify_id = maction->flow_action_raw.action_id;
2486 return 0;
2487 }
Mark Bloch10a30892018-09-02 12:51:34 +03002488 if (maction->flow_action_raw.sub_type ==
2489 MLX5_IB_FLOW_ACTION_DECAP) {
Mark Bloch501f14e2018-09-06 17:27:04 +03002490 if (action->action & MLX5_FLOW_CONTEXT_ACTION_DECAP)
2491 return -EINVAL;
Mark Bloch10a30892018-09-02 12:51:34 +03002492 action->action |= MLX5_FLOW_CONTEXT_ACTION_DECAP;
2493 return 0;
2494 }
Mark Bloche806f932018-09-02 12:51:36 +03002495 if (maction->flow_action_raw.sub_type ==
2496 MLX5_IB_FLOW_ACTION_PACKET_REFORMAT) {
Mark Bloch501f14e2018-09-06 17:27:04 +03002497 if (action->action &
2498 MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT)
2499 return -EINVAL;
Mark Bloche806f932018-09-02 12:51:36 +03002500 action->action |=
2501 MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT;
2502 action->reformat_id =
2503 maction->flow_action_raw.action_id;
2504 return 0;
2505 }
Mark Blochb1085be2018-09-02 12:51:32 +03002506 /* fall through */
Aviad Yehezkel802c2122018-03-28 09:27:53 +03002507 default:
2508 return -EOPNOTSUPP;
2509 }
2510}
2511
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002512static int parse_flow_attr(struct mlx5_core_dev *mdev, u32 *match_c,
2513 u32 *match_v, const union ib_flow_spec *ib_spec,
Aviad Yehezkel802c2122018-03-28 09:27:53 +03002514 const struct ib_flow_attr *flow_attr,
Ariel Levkovich71c6e862018-05-13 14:33:34 +03002515 struct mlx5_flow_act *action, u32 prev_type)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002516{
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002517 void *misc_params_c = MLX5_ADDR_OF(fte_match_param, match_c,
2518 misc_parameters);
2519 void *misc_params_v = MLX5_ADDR_OF(fte_match_param, match_v,
2520 misc_parameters);
Ariel Levkovich71c6e862018-05-13 14:33:34 +03002521 void *misc_params2_c = MLX5_ADDR_OF(fte_match_param, match_c,
2522 misc_parameters_2);
2523 void *misc_params2_v = MLX5_ADDR_OF(fte_match_param, match_v,
2524 misc_parameters_2);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002525 void *headers_c;
2526 void *headers_v;
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002527 int match_ipv;
Aviad Yehezkel802c2122018-03-28 09:27:53 +03002528 int ret;
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002529
Moses Reuben2d1e6972016-11-14 19:04:52 +02002530 if (ib_spec->type & IB_FLOW_SPEC_INNER) {
2531 headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
2532 inner_headers);
2533 headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
2534 inner_headers);
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002535 match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2536 ft_field_support.inner_ip_version);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002537 } else {
2538 headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
2539 outer_headers);
2540 headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
2541 outer_headers);
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002542 match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2543 ft_field_support.outer_ip_version);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002544 }
2545
2546 switch (ib_spec->type & ~IB_FLOW_SPEC_INNER) {
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002547 case IB_FLOW_SPEC_ETH:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002548 if (FIELDS_NOT_SUPPORTED(ib_spec->eth.mask, LAST_ETH_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002549 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002550
Moses Reuben2d1e6972016-11-14 19:04:52 +02002551 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002552 dmac_47_16),
2553 ib_spec->eth.mask.dst_mac);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002554 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002555 dmac_47_16),
2556 ib_spec->eth.val.dst_mac);
2557
Moses Reuben2d1e6972016-11-14 19:04:52 +02002558 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottliebee3da802016-09-12 19:16:24 +03002559 smac_47_16),
2560 ib_spec->eth.mask.src_mac);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002561 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottliebee3da802016-09-12 19:16:24 +03002562 smac_47_16),
2563 ib_spec->eth.val.src_mac);
2564
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002565 if (ib_spec->eth.mask.vlan_tag) {
Moses Reuben2d1e6972016-11-14 19:04:52 +02002566 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Mohamad Haj Yahia10543362016-10-09 16:25:43 +03002567 cvlan_tag, 1);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002568 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Mohamad Haj Yahia10543362016-10-09 16:25:43 +03002569 cvlan_tag, 1);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002570
Moses Reuben2d1e6972016-11-14 19:04:52 +02002571 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002572 first_vid, ntohs(ib_spec->eth.mask.vlan_tag));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002573 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002574 first_vid, ntohs(ib_spec->eth.val.vlan_tag));
2575
Moses Reuben2d1e6972016-11-14 19:04:52 +02002576 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002577 first_cfi,
2578 ntohs(ib_spec->eth.mask.vlan_tag) >> 12);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002579 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002580 first_cfi,
2581 ntohs(ib_spec->eth.val.vlan_tag) >> 12);
2582
Moses Reuben2d1e6972016-11-14 19:04:52 +02002583 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002584 first_prio,
2585 ntohs(ib_spec->eth.mask.vlan_tag) >> 13);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002586 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002587 first_prio,
2588 ntohs(ib_spec->eth.val.vlan_tag) >> 13);
2589 }
Moses Reuben2d1e6972016-11-14 19:04:52 +02002590 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002591 ethertype, ntohs(ib_spec->eth.mask.ether_type));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002592 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002593 ethertype, ntohs(ib_spec->eth.val.ether_type));
2594 break;
2595 case IB_FLOW_SPEC_IPV4:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002596 if (FIELDS_NOT_SUPPORTED(ib_spec->ipv4.mask, LAST_IPV4_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002597 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002598
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002599 if (match_ipv) {
2600 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2601 ip_version, 0xf);
2602 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Boris Pismenny3346c482017-08-20 15:13:08 +03002603 ip_version, MLX5_FS_IPV4_VERSION);
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002604 } else {
2605 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2606 ethertype, 0xffff);
2607 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2608 ethertype, ETH_P_IP);
2609 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002610
Moses Reuben2d1e6972016-11-14 19:04:52 +02002611 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002612 src_ipv4_src_ipv6.ipv4_layout.ipv4),
2613 &ib_spec->ipv4.mask.src_ip,
2614 sizeof(ib_spec->ipv4.mask.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002615 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002616 src_ipv4_src_ipv6.ipv4_layout.ipv4),
2617 &ib_spec->ipv4.val.src_ip,
2618 sizeof(ib_spec->ipv4.val.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002619 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002620 dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2621 &ib_spec->ipv4.mask.dst_ip,
2622 sizeof(ib_spec->ipv4.mask.dst_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002623 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002624 dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2625 &ib_spec->ipv4.val.dst_ip,
2626 sizeof(ib_spec->ipv4.val.dst_ip));
Maor Gottliebca0d4752016-08-30 16:58:35 +03002627
Moses Reuben2d1e6972016-11-14 19:04:52 +02002628 set_tos(headers_c, headers_v,
Maor Gottliebca0d4752016-08-30 16:58:35 +03002629 ib_spec->ipv4.mask.tos, ib_spec->ipv4.val.tos);
2630
Moses Reuben2d1e6972016-11-14 19:04:52 +02002631 set_proto(headers_c, headers_v,
Maor Gottliebca0d4752016-08-30 16:58:35 +03002632 ib_spec->ipv4.mask.proto, ib_spec->ipv4.val.proto);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002633 break;
Maor Gottlieb026bae02016-06-17 15:14:51 +03002634 case IB_FLOW_SPEC_IPV6:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002635 if (FIELDS_NOT_SUPPORTED(ib_spec->ipv6.mask, LAST_IPV6_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002636 return -EOPNOTSUPP;
Maor Gottlieb026bae02016-06-17 15:14:51 +03002637
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002638 if (match_ipv) {
2639 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2640 ip_version, 0xf);
2641 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Boris Pismenny3346c482017-08-20 15:13:08 +03002642 ip_version, MLX5_FS_IPV6_VERSION);
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002643 } else {
2644 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2645 ethertype, 0xffff);
2646 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2647 ethertype, ETH_P_IPV6);
2648 }
Maor Gottlieb026bae02016-06-17 15:14:51 +03002649
Moses Reuben2d1e6972016-11-14 19:04:52 +02002650 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb026bae02016-06-17 15:14:51 +03002651 src_ipv4_src_ipv6.ipv6_layout.ipv6),
2652 &ib_spec->ipv6.mask.src_ip,
2653 sizeof(ib_spec->ipv6.mask.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002654 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb026bae02016-06-17 15:14:51 +03002655 src_ipv4_src_ipv6.ipv6_layout.ipv6),
2656 &ib_spec->ipv6.val.src_ip,
2657 sizeof(ib_spec->ipv6.val.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002658 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb026bae02016-06-17 15:14:51 +03002659 dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2660 &ib_spec->ipv6.mask.dst_ip,
2661 sizeof(ib_spec->ipv6.mask.dst_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002662 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb026bae02016-06-17 15:14:51 +03002663 dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2664 &ib_spec->ipv6.val.dst_ip,
2665 sizeof(ib_spec->ipv6.val.dst_ip));
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002666
Moses Reuben2d1e6972016-11-14 19:04:52 +02002667 set_tos(headers_c, headers_v,
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002668 ib_spec->ipv6.mask.traffic_class,
2669 ib_spec->ipv6.val.traffic_class);
2670
Moses Reuben2d1e6972016-11-14 19:04:52 +02002671 set_proto(headers_c, headers_v,
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002672 ib_spec->ipv6.mask.next_hdr,
2673 ib_spec->ipv6.val.next_hdr);
2674
Moses Reuben2d1e6972016-11-14 19:04:52 +02002675 set_flow_label(misc_params_c, misc_params_v,
2676 ntohl(ib_spec->ipv6.mask.flow_label),
2677 ntohl(ib_spec->ipv6.val.flow_label),
2678 ib_spec->type & IB_FLOW_SPEC_INNER);
Aviad Yehezkel802c2122018-03-28 09:27:53 +03002679 break;
2680 case IB_FLOW_SPEC_ESP:
2681 if (ib_spec->esp.mask.seq)
2682 return -EOPNOTSUPP;
Moses Reuben2d1e6972016-11-14 19:04:52 +02002683
Aviad Yehezkel802c2122018-03-28 09:27:53 +03002684 MLX5_SET(fte_match_set_misc, misc_params_c, outer_esp_spi,
2685 ntohl(ib_spec->esp.mask.spi));
2686 MLX5_SET(fte_match_set_misc, misc_params_v, outer_esp_spi,
2687 ntohl(ib_spec->esp.val.spi));
Maor Gottlieb026bae02016-06-17 15:14:51 +03002688 break;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002689 case IB_FLOW_SPEC_TCP:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002690 if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
2691 LAST_TCP_UDP_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002692 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002693
Moses Reuben2d1e6972016-11-14 19:04:52 +02002694 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002695 0xff);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002696 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002697 IPPROTO_TCP);
2698
Moses Reuben2d1e6972016-11-14 19:04:52 +02002699 MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002700 ntohs(ib_spec->tcp_udp.mask.src_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002701 MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002702 ntohs(ib_spec->tcp_udp.val.src_port));
2703
Moses Reuben2d1e6972016-11-14 19:04:52 +02002704 MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002705 ntohs(ib_spec->tcp_udp.mask.dst_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002706 MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002707 ntohs(ib_spec->tcp_udp.val.dst_port));
2708 break;
2709 case IB_FLOW_SPEC_UDP:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002710 if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
2711 LAST_TCP_UDP_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002712 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002713
Moses Reuben2d1e6972016-11-14 19:04:52 +02002714 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002715 0xff);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002716 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002717 IPPROTO_UDP);
2718
Moses Reuben2d1e6972016-11-14 19:04:52 +02002719 MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002720 ntohs(ib_spec->tcp_udp.mask.src_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002721 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002722 ntohs(ib_spec->tcp_udp.val.src_port));
2723
Moses Reuben2d1e6972016-11-14 19:04:52 +02002724 MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002725 ntohs(ib_spec->tcp_udp.mask.dst_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002726 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002727 ntohs(ib_spec->tcp_udp.val.dst_port));
2728 break;
Ariel Levkovichda2f22a2018-05-13 14:33:33 +03002729 case IB_FLOW_SPEC_GRE:
2730 if (ib_spec->gre.mask.c_ks_res0_ver)
2731 return -EOPNOTSUPP;
2732
2733 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
2734 0xff);
2735 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
2736 IPPROTO_GRE);
2737
2738 MLX5_SET(fte_match_set_misc, misc_params_c, gre_protocol,
Maor Gottlieba93b6322018-07-01 15:50:17 +03002739 ntohs(ib_spec->gre.mask.protocol));
Ariel Levkovichda2f22a2018-05-13 14:33:33 +03002740 MLX5_SET(fte_match_set_misc, misc_params_v, gre_protocol,
2741 ntohs(ib_spec->gre.val.protocol));
2742
2743 memcpy(MLX5_ADDR_OF(fte_match_set_misc, misc_params_c,
2744 gre_key_h),
2745 &ib_spec->gre.mask.key,
2746 sizeof(ib_spec->gre.mask.key));
2747 memcpy(MLX5_ADDR_OF(fte_match_set_misc, misc_params_v,
2748 gre_key_h),
2749 &ib_spec->gre.val.key,
2750 sizeof(ib_spec->gre.val.key));
2751 break;
Ariel Levkovich71c6e862018-05-13 14:33:34 +03002752 case IB_FLOW_SPEC_MPLS:
2753 switch (prev_type) {
2754 case IB_FLOW_SPEC_UDP:
2755 if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2756 ft_field_support.outer_first_mpls_over_udp),
2757 &ib_spec->mpls.mask.tag))
2758 return -EOPNOTSUPP;
2759
2760 memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
2761 outer_first_mpls_over_udp),
2762 &ib_spec->mpls.val.tag,
2763 sizeof(ib_spec->mpls.val.tag));
2764 memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
2765 outer_first_mpls_over_udp),
2766 &ib_spec->mpls.mask.tag,
2767 sizeof(ib_spec->mpls.mask.tag));
2768 break;
2769 case IB_FLOW_SPEC_GRE:
2770 if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2771 ft_field_support.outer_first_mpls_over_gre),
2772 &ib_spec->mpls.mask.tag))
2773 return -EOPNOTSUPP;
2774
2775 memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
2776 outer_first_mpls_over_gre),
2777 &ib_spec->mpls.val.tag,
2778 sizeof(ib_spec->mpls.val.tag));
2779 memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
2780 outer_first_mpls_over_gre),
2781 &ib_spec->mpls.mask.tag,
2782 sizeof(ib_spec->mpls.mask.tag));
2783 break;
2784 default:
2785 if (ib_spec->type & IB_FLOW_SPEC_INNER) {
2786 if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2787 ft_field_support.inner_first_mpls),
2788 &ib_spec->mpls.mask.tag))
2789 return -EOPNOTSUPP;
2790
2791 memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
2792 inner_first_mpls),
2793 &ib_spec->mpls.val.tag,
2794 sizeof(ib_spec->mpls.val.tag));
2795 memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
2796 inner_first_mpls),
2797 &ib_spec->mpls.mask.tag,
2798 sizeof(ib_spec->mpls.mask.tag));
2799 } else {
2800 if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2801 ft_field_support.outer_first_mpls),
2802 &ib_spec->mpls.mask.tag))
2803 return -EOPNOTSUPP;
2804
2805 memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
2806 outer_first_mpls),
2807 &ib_spec->mpls.val.tag,
2808 sizeof(ib_spec->mpls.val.tag));
2809 memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
2810 outer_first_mpls),
2811 &ib_spec->mpls.mask.tag,
2812 sizeof(ib_spec->mpls.mask.tag));
2813 }
2814 }
2815 break;
Moses Reubenffb30d82016-11-14 19:04:50 +02002816 case IB_FLOW_SPEC_VXLAN_TUNNEL:
2817 if (FIELDS_NOT_SUPPORTED(ib_spec->tunnel.mask,
2818 LAST_TUNNEL_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002819 return -EOPNOTSUPP;
Moses Reubenffb30d82016-11-14 19:04:50 +02002820
2821 MLX5_SET(fte_match_set_misc, misc_params_c, vxlan_vni,
2822 ntohl(ib_spec->tunnel.mask.tunnel_id));
2823 MLX5_SET(fte_match_set_misc, misc_params_v, vxlan_vni,
2824 ntohl(ib_spec->tunnel.val.tunnel_id));
2825 break;
Moses Reuben2ac693f2017-01-18 14:59:50 +02002826 case IB_FLOW_SPEC_ACTION_TAG:
2827 if (FIELDS_NOT_SUPPORTED(ib_spec->flow_tag,
2828 LAST_FLOW_TAG_FIELD))
2829 return -EOPNOTSUPP;
2830 if (ib_spec->flow_tag.tag_id >= BIT(24))
2831 return -EINVAL;
2832
Boris Pismenny075572d2017-08-16 09:33:30 +03002833 action->flow_tag = ib_spec->flow_tag.tag_id;
Matan Baraka9db0ec2017-08-16 09:43:48 +03002834 action->has_flow_tag = true;
Moses Reuben2ac693f2017-01-18 14:59:50 +02002835 break;
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002836 case IB_FLOW_SPEC_ACTION_DROP:
2837 if (FIELDS_NOT_SUPPORTED(ib_spec->drop,
2838 LAST_DROP_FIELD))
2839 return -EOPNOTSUPP;
Boris Pismenny075572d2017-08-16 09:33:30 +03002840 action->action |= MLX5_FLOW_CONTEXT_ACTION_DROP;
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002841 break;
Aviad Yehezkel802c2122018-03-28 09:27:53 +03002842 case IB_FLOW_SPEC_ACTION_HANDLE:
Mark Bloch2ea26202018-09-06 17:27:03 +03002843 ret = parse_flow_flow_action(to_mflow_act(ib_spec->action.act),
2844 flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS, action);
Aviad Yehezkel802c2122018-03-28 09:27:53 +03002845 if (ret)
2846 return ret;
2847 break;
Raed Salem3b3233f2018-05-31 16:43:39 +03002848 case IB_FLOW_SPEC_ACTION_COUNT:
2849 if (FIELDS_NOT_SUPPORTED(ib_spec->flow_count,
2850 LAST_COUNTERS_FIELD))
2851 return -EOPNOTSUPP;
2852
2853 /* for now support only one counters spec per flow */
2854 if (action->action & MLX5_FLOW_CONTEXT_ACTION_COUNT)
2855 return -EINVAL;
2856
2857 action->counters = ib_spec->flow_count.counters;
2858 action->action |= MLX5_FLOW_CONTEXT_ACTION_COUNT;
2859 break;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002860 default:
2861 return -EINVAL;
2862 }
2863
2864 return 0;
2865}
2866
2867/* If a flow could catch both multicast and unicast packets,
2868 * it won't fall into the multicast flow steering table and this rule
2869 * could steal other multicast packets.
2870 */
Yishai Hadasa550ddf2017-08-17 15:52:33 +03002871static bool flow_is_multicast_only(const struct ib_flow_attr *ib_attr)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002872{
Yishai Hadas81e30882017-06-08 16:15:09 +03002873 union ib_flow_spec *flow_spec;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002874
2875 if (ib_attr->type != IB_FLOW_ATTR_NORMAL ||
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002876 ib_attr->num_of_specs < 1)
2877 return false;
2878
Yishai Hadas81e30882017-06-08 16:15:09 +03002879 flow_spec = (union ib_flow_spec *)(ib_attr + 1);
2880 if (flow_spec->type == IB_FLOW_SPEC_IPV4) {
2881 struct ib_flow_spec_ipv4 *ipv4_spec;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002882
Yishai Hadas81e30882017-06-08 16:15:09 +03002883 ipv4_spec = (struct ib_flow_spec_ipv4 *)flow_spec;
2884 if (ipv4_is_multicast(ipv4_spec->val.dst_ip))
2885 return true;
2886
2887 return false;
2888 }
2889
2890 if (flow_spec->type == IB_FLOW_SPEC_ETH) {
2891 struct ib_flow_spec_eth *eth_spec;
2892
2893 eth_spec = (struct ib_flow_spec_eth *)flow_spec;
2894 return is_multicast_ether_addr(eth_spec->mask.dst_mac) &&
2895 is_multicast_ether_addr(eth_spec->val.dst_mac);
2896 }
2897
2898 return false;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002899}
2900
Aviad Yehezkel802c2122018-03-28 09:27:53 +03002901enum valid_spec {
2902 VALID_SPEC_INVALID,
2903 VALID_SPEC_VALID,
2904 VALID_SPEC_NA,
2905};
2906
2907static enum valid_spec
2908is_valid_esp_aes_gcm(struct mlx5_core_dev *mdev,
2909 const struct mlx5_flow_spec *spec,
2910 const struct mlx5_flow_act *flow_act,
2911 bool egress)
2912{
2913 const u32 *match_c = spec->match_criteria;
2914 bool is_crypto =
2915 (flow_act->action & (MLX5_FLOW_CONTEXT_ACTION_ENCRYPT |
2916 MLX5_FLOW_CONTEXT_ACTION_DECRYPT));
2917 bool is_ipsec = mlx5_fs_is_ipsec_flow(match_c);
2918 bool is_drop = flow_act->action & MLX5_FLOW_CONTEXT_ACTION_DROP;
2919
2920 /*
2921 * Currently only crypto is supported in egress, when regular egress
2922 * rules would be supported, always return VALID_SPEC_NA.
2923 */
2924 if (!is_crypto)
Mark Bloch78dd0c42018-09-02 12:51:31 +03002925 return VALID_SPEC_NA;
Aviad Yehezkel802c2122018-03-28 09:27:53 +03002926
2927 return is_crypto && is_ipsec &&
2928 (!egress || (!is_drop && !flow_act->has_flow_tag)) ?
2929 VALID_SPEC_VALID : VALID_SPEC_INVALID;
2930}
2931
2932static bool is_valid_spec(struct mlx5_core_dev *mdev,
2933 const struct mlx5_flow_spec *spec,
2934 const struct mlx5_flow_act *flow_act,
2935 bool egress)
2936{
2937 /* We curretly only support ipsec egress flow */
2938 return is_valid_esp_aes_gcm(mdev, spec, flow_act, egress) != VALID_SPEC_INVALID;
2939}
2940
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002941static bool is_valid_ethertype(struct mlx5_core_dev *mdev,
2942 const struct ib_flow_attr *flow_attr,
Ariel Levkovich0f750962017-04-03 13:11:02 +03002943 bool check_inner)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002944{
2945 union ib_flow_spec *ib_spec = (union ib_flow_spec *)(flow_attr + 1);
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002946 int match_ipv = check_inner ?
2947 MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2948 ft_field_support.inner_ip_version) :
2949 MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2950 ft_field_support.outer_ip_version);
Ariel Levkovich0f750962017-04-03 13:11:02 +03002951 int inner_bit = check_inner ? IB_FLOW_SPEC_INNER : 0;
2952 bool ipv4_spec_valid, ipv6_spec_valid;
2953 unsigned int ip_spec_type = 0;
2954 bool has_ethertype = false;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002955 unsigned int spec_index;
Ariel Levkovich0f750962017-04-03 13:11:02 +03002956 bool mask_valid = true;
2957 u16 eth_type = 0;
2958 bool type_valid;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002959
2960 /* Validate that ethertype is correct */
2961 for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
Ariel Levkovich0f750962017-04-03 13:11:02 +03002962 if ((ib_spec->type == (IB_FLOW_SPEC_ETH | inner_bit)) &&
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002963 ib_spec->eth.mask.ether_type) {
Ariel Levkovich0f750962017-04-03 13:11:02 +03002964 mask_valid = (ib_spec->eth.mask.ether_type ==
2965 htons(0xffff));
2966 has_ethertype = true;
2967 eth_type = ntohs(ib_spec->eth.val.ether_type);
2968 } else if ((ib_spec->type == (IB_FLOW_SPEC_IPV4 | inner_bit)) ||
2969 (ib_spec->type == (IB_FLOW_SPEC_IPV6 | inner_bit))) {
2970 ip_spec_type = ib_spec->type;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002971 }
2972 ib_spec = (void *)ib_spec + ib_spec->size;
2973 }
Ariel Levkovich0f750962017-04-03 13:11:02 +03002974
2975 type_valid = (!has_ethertype) || (!ip_spec_type);
2976 if (!type_valid && mask_valid) {
2977 ipv4_spec_valid = (eth_type == ETH_P_IP) &&
2978 (ip_spec_type == (IB_FLOW_SPEC_IPV4 | inner_bit));
2979 ipv6_spec_valid = (eth_type == ETH_P_IPV6) &&
2980 (ip_spec_type == (IB_FLOW_SPEC_IPV6 | inner_bit));
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002981
2982 type_valid = (ipv4_spec_valid) || (ipv6_spec_valid) ||
2983 (((eth_type == ETH_P_MPLS_UC) ||
2984 (eth_type == ETH_P_MPLS_MC)) && match_ipv);
Ariel Levkovich0f750962017-04-03 13:11:02 +03002985 }
2986
2987 return type_valid;
2988}
2989
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002990static bool is_valid_attr(struct mlx5_core_dev *mdev,
2991 const struct ib_flow_attr *flow_attr)
Ariel Levkovich0f750962017-04-03 13:11:02 +03002992{
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002993 return is_valid_ethertype(mdev, flow_attr, false) &&
2994 is_valid_ethertype(mdev, flow_attr, true);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002995}
2996
2997static void put_flow_table(struct mlx5_ib_dev *dev,
2998 struct mlx5_ib_flow_prio *prio, bool ft_added)
2999{
3000 prio->refcount -= !!ft_added;
3001 if (!prio->refcount) {
3002 mlx5_destroy_flow_table(prio->flow_table);
3003 prio->flow_table = NULL;
3004 }
3005}
3006
Raed Salem3b3233f2018-05-31 16:43:39 +03003007static void counters_clear_description(struct ib_counters *counters)
3008{
3009 struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
3010
3011 mutex_lock(&mcounters->mcntrs_mutex);
3012 kfree(mcounters->counters_data);
3013 mcounters->counters_data = NULL;
3014 mcounters->cntrs_max_index = 0;
3015 mutex_unlock(&mcounters->mcntrs_mutex);
3016}
3017
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003018static int mlx5_ib_destroy_flow(struct ib_flow *flow_id)
3019{
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003020 struct mlx5_ib_flow_handler *handler = container_of(flow_id,
3021 struct mlx5_ib_flow_handler,
3022 ibflow);
3023 struct mlx5_ib_flow_handler *iter, *tmp;
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003024 struct mlx5_ib_dev *dev = handler->dev;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003025
Mark Bloch9a4ca382018-01-16 14:42:35 +00003026 mutex_lock(&dev->flow_db->lock);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003027
3028 list_for_each_entry_safe(iter, tmp, &handler->list, list) {
Mark Bloch74491de2016-08-31 11:24:25 +00003029 mlx5_del_flow_rules(iter->rule);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003030 put_flow_table(dev, iter->prio, true);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003031 list_del(&iter->list);
3032 kfree(iter);
3033 }
3034
Mark Bloch74491de2016-08-31 11:24:25 +00003035 mlx5_del_flow_rules(handler->rule);
Maor Gottlieb5497adc2016-08-28 14:16:31 +03003036 put_flow_table(dev, handler->prio, true);
Raed Salem3b3233f2018-05-31 16:43:39 +03003037 if (handler->ibcounters &&
3038 atomic_read(&handler->ibcounters->usecnt) == 1)
3039 counters_clear_description(handler->ibcounters);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003040
Raed Salem3b3233f2018-05-31 16:43:39 +03003041 mutex_unlock(&dev->flow_db->lock);
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003042 if (handler->flow_matcher)
3043 atomic_dec(&handler->flow_matcher->usecnt);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003044 kfree(handler);
3045
3046 return 0;
3047}
3048
Maor Gottlieb35d190112016-03-07 18:51:47 +02003049static int ib_prio_to_core_prio(unsigned int priority, bool dont_trap)
3050{
3051 priority *= 2;
3052 if (!dont_trap)
3053 priority++;
3054 return priority;
3055}
3056
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003057enum flow_table_type {
3058 MLX5_IB_FT_RX,
3059 MLX5_IB_FT_TX
3060};
3061
Maor Gottlieb00b7c2a2017-03-29 06:09:01 +03003062#define MLX5_FS_MAX_TYPES 6
3063#define MLX5_FS_MAX_ENTRIES BIT(16)
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003064
3065static struct mlx5_ib_flow_prio *_get_prio(struct mlx5_flow_namespace *ns,
3066 struct mlx5_ib_flow_prio *prio,
3067 int priority,
Mark Bloch4adda112018-09-02 12:51:33 +03003068 int num_entries, int num_groups,
3069 u32 flags)
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003070{
3071 struct mlx5_flow_table *ft;
3072
3073 ft = mlx5_create_auto_grouped_flow_table(ns, priority,
3074 num_entries,
3075 num_groups,
Mark Bloch4adda112018-09-02 12:51:33 +03003076 0, flags);
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003077 if (IS_ERR(ft))
3078 return ERR_CAST(ft);
3079
3080 prio->flow_table = ft;
3081 prio->refcount = 0;
3082 return prio;
3083}
3084
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003085static struct mlx5_ib_flow_prio *get_flow_table(struct mlx5_ib_dev *dev,
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003086 struct ib_flow_attr *flow_attr,
3087 enum flow_table_type ft_type)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003088{
Maor Gottlieb35d190112016-03-07 18:51:47 +02003089 bool dont_trap = flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003090 struct mlx5_flow_namespace *ns = NULL;
3091 struct mlx5_ib_flow_prio *prio;
3092 struct mlx5_flow_table *ft;
Maor Gottliebdac388e2017-03-29 06:09:00 +03003093 int max_table_size;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003094 int num_entries;
3095 int num_groups;
Mark Bloch4adda112018-09-02 12:51:33 +03003096 u32 flags = 0;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003097 int priority;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003098
Maor Gottliebdac388e2017-03-29 06:09:00 +03003099 max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
3100 log_max_ft_size));
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003101 if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
Mark Bloch78dd0c42018-09-02 12:51:31 +03003102 enum mlx5_flow_namespace_type fn_type;
3103
3104 if (flow_is_multicast_only(flow_attr) &&
3105 !dont_trap)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003106 priority = MLX5_IB_FLOW_MCAST_PRIO;
3107 else
Maor Gottlieb35d190112016-03-07 18:51:47 +02003108 priority = ib_prio_to_core_prio(flow_attr->priority,
3109 dont_trap);
Mark Bloch78dd0c42018-09-02 12:51:31 +03003110 if (ft_type == MLX5_IB_FT_RX) {
3111 fn_type = MLX5_FLOW_NAMESPACE_BYPASS;
3112 prio = &dev->flow_db->prios[priority];
Mark Bloch4adda112018-09-02 12:51:33 +03003113 if (!dev->rep &&
3114 MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, decap))
3115 flags |= MLX5_FLOW_TABLE_TUNNEL_EN_DECAP;
Mark Bloch5c2db532018-09-02 12:51:35 +03003116 if (!dev->rep &&
3117 MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
3118 reformat_l3_tunnel_to_l2))
3119 flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
Mark Bloch78dd0c42018-09-02 12:51:31 +03003120 } else {
3121 max_table_size =
3122 BIT(MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev,
3123 log_max_ft_size));
3124 fn_type = MLX5_FLOW_NAMESPACE_EGRESS;
3125 prio = &dev->flow_db->egress_prios[priority];
Mark Bloch4adda112018-09-02 12:51:33 +03003126 if (!dev->rep &&
3127 MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev, reformat))
3128 flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
Mark Bloch78dd0c42018-09-02 12:51:31 +03003129 }
3130 ns = mlx5_get_flow_namespace(dev->mdev, fn_type);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003131 num_entries = MLX5_FS_MAX_ENTRIES;
3132 num_groups = MLX5_FS_MAX_TYPES;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003133 } else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
3134 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
3135 ns = mlx5_get_flow_namespace(dev->mdev,
3136 MLX5_FLOW_NAMESPACE_LEFTOVERS);
3137 build_leftovers_ft_param(&priority,
3138 &num_entries,
3139 &num_groups);
Mark Bloch9a4ca382018-01-16 14:42:35 +00003140 prio = &dev->flow_db->prios[MLX5_IB_FLOW_LEFTOVERS_PRIO];
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003141 } else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
3142 if (!MLX5_CAP_FLOWTABLE(dev->mdev,
3143 allow_sniffer_and_nic_rx_shared_tir))
3144 return ERR_PTR(-ENOTSUPP);
3145
3146 ns = mlx5_get_flow_namespace(dev->mdev, ft_type == MLX5_IB_FT_RX ?
3147 MLX5_FLOW_NAMESPACE_SNIFFER_RX :
3148 MLX5_FLOW_NAMESPACE_SNIFFER_TX);
3149
Mark Bloch9a4ca382018-01-16 14:42:35 +00003150 prio = &dev->flow_db->sniffer[ft_type];
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003151 priority = 0;
3152 num_entries = 1;
3153 num_groups = 1;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003154 }
3155
3156 if (!ns)
3157 return ERR_PTR(-ENOTSUPP);
3158
Maor Gottliebdac388e2017-03-29 06:09:00 +03003159 if (num_entries > max_table_size)
3160 return ERR_PTR(-ENOMEM);
3161
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003162 ft = prio->flow_table;
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003163 if (!ft)
Mark Bloch4adda112018-09-02 12:51:33 +03003164 return _get_prio(ns, prio, priority, num_entries, num_groups,
3165 flags);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003166
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003167 return prio;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003168}
3169
Yishai Hadasa550ddf2017-08-17 15:52:33 +03003170static void set_underlay_qp(struct mlx5_ib_dev *dev,
3171 struct mlx5_flow_spec *spec,
3172 u32 underlay_qpn)
3173{
3174 void *misc_params_c = MLX5_ADDR_OF(fte_match_param,
3175 spec->match_criteria,
3176 misc_parameters);
3177 void *misc_params_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
3178 misc_parameters);
3179
3180 if (underlay_qpn &&
3181 MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
3182 ft_field_support.bth_dst_qp)) {
3183 MLX5_SET(fte_match_set_misc,
3184 misc_params_v, bth_dst_qp, underlay_qpn);
3185 MLX5_SET(fte_match_set_misc,
3186 misc_params_c, bth_dst_qp, 0xffffff);
3187 }
3188}
3189
Raed Salem5e95af52018-05-31 16:43:40 +03003190static int read_flow_counters(struct ib_device *ibdev,
3191 struct mlx5_read_counters_attr *read_attr)
3192{
3193 struct mlx5_fc *fc = read_attr->hw_cntrs_hndl;
3194 struct mlx5_ib_dev *dev = to_mdev(ibdev);
3195
3196 return mlx5_fc_query(dev->mdev, fc,
3197 &read_attr->out[IB_COUNTER_PACKETS],
3198 &read_attr->out[IB_COUNTER_BYTES]);
3199}
3200
3201/* flow counters currently expose two counters packets and bytes */
3202#define FLOW_COUNTERS_NUM 2
Raed Salem3b3233f2018-05-31 16:43:39 +03003203static int counters_set_description(struct ib_counters *counters,
3204 enum mlx5_ib_counters_type counters_type,
3205 struct mlx5_ib_flow_counters_desc *desc_data,
3206 u32 ncounters)
3207{
3208 struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
3209 u32 cntrs_max_index = 0;
3210 int i;
3211
3212 if (counters_type != MLX5_IB_COUNTERS_FLOW)
3213 return -EINVAL;
3214
3215 /* init the fields for the object */
3216 mcounters->type = counters_type;
Raed Salem5e95af52018-05-31 16:43:40 +03003217 mcounters->read_counters = read_flow_counters;
3218 mcounters->counters_num = FLOW_COUNTERS_NUM;
Raed Salem3b3233f2018-05-31 16:43:39 +03003219 mcounters->ncounters = ncounters;
3220 /* each counter entry have both description and index pair */
3221 for (i = 0; i < ncounters; i++) {
3222 if (desc_data[i].description > IB_COUNTER_BYTES)
3223 return -EINVAL;
3224
3225 if (cntrs_max_index <= desc_data[i].index)
3226 cntrs_max_index = desc_data[i].index + 1;
3227 }
3228
3229 mutex_lock(&mcounters->mcntrs_mutex);
3230 mcounters->counters_data = desc_data;
3231 mcounters->cntrs_max_index = cntrs_max_index;
3232 mutex_unlock(&mcounters->mcntrs_mutex);
3233
3234 return 0;
3235}
3236
3237#define MAX_COUNTERS_NUM (USHRT_MAX / (sizeof(u32) * 2))
3238static int flow_counters_set_data(struct ib_counters *ibcounters,
3239 struct mlx5_ib_create_flow *ucmd)
3240{
3241 struct mlx5_ib_mcounters *mcounters = to_mcounters(ibcounters);
3242 struct mlx5_ib_flow_counters_data *cntrs_data = NULL;
3243 struct mlx5_ib_flow_counters_desc *desc_data = NULL;
3244 bool hw_hndl = false;
3245 int ret = 0;
3246
3247 if (ucmd && ucmd->ncounters_data != 0) {
3248 cntrs_data = ucmd->data;
3249 if (cntrs_data->ncounters > MAX_COUNTERS_NUM)
3250 return -EINVAL;
3251
3252 desc_data = kcalloc(cntrs_data->ncounters,
3253 sizeof(*desc_data),
3254 GFP_KERNEL);
3255 if (!desc_data)
3256 return -ENOMEM;
3257
3258 if (copy_from_user(desc_data,
3259 u64_to_user_ptr(cntrs_data->counters_data),
3260 sizeof(*desc_data) * cntrs_data->ncounters)) {
3261 ret = -EFAULT;
3262 goto free;
3263 }
3264 }
3265
3266 if (!mcounters->hw_cntrs_hndl) {
3267 mcounters->hw_cntrs_hndl = mlx5_fc_create(
3268 to_mdev(ibcounters->device)->mdev, false);
weiyongjun (A)e31abf72018-06-07 01:47:41 +00003269 if (IS_ERR(mcounters->hw_cntrs_hndl)) {
3270 ret = PTR_ERR(mcounters->hw_cntrs_hndl);
Raed Salem3b3233f2018-05-31 16:43:39 +03003271 goto free;
3272 }
3273 hw_hndl = true;
3274 }
3275
3276 if (desc_data) {
3277 /* counters already bound to at least one flow */
3278 if (mcounters->cntrs_max_index) {
3279 ret = -EINVAL;
3280 goto free_hndl;
3281 }
3282
3283 ret = counters_set_description(ibcounters,
3284 MLX5_IB_COUNTERS_FLOW,
3285 desc_data,
3286 cntrs_data->ncounters);
3287 if (ret)
3288 goto free_hndl;
3289
3290 } else if (!mcounters->cntrs_max_index) {
3291 /* counters not bound yet, must have udata passed */
3292 ret = -EINVAL;
3293 goto free_hndl;
3294 }
3295
3296 return 0;
3297
3298free_hndl:
3299 if (hw_hndl) {
3300 mlx5_fc_destroy(to_mdev(ibcounters->device)->mdev,
3301 mcounters->hw_cntrs_hndl);
3302 mcounters->hw_cntrs_hndl = NULL;
3303 }
3304free:
3305 kfree(desc_data);
3306 return ret;
3307}
3308
Yishai Hadasa550ddf2017-08-17 15:52:33 +03003309static struct mlx5_ib_flow_handler *_create_flow_rule(struct mlx5_ib_dev *dev,
3310 struct mlx5_ib_flow_prio *ft_prio,
3311 const struct ib_flow_attr *flow_attr,
3312 struct mlx5_flow_destination *dst,
Raed Salem3b3233f2018-05-31 16:43:39 +03003313 u32 underlay_qpn,
3314 struct mlx5_ib_create_flow *ucmd)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003315{
3316 struct mlx5_flow_table *ft = ft_prio->flow_table;
3317 struct mlx5_ib_flow_handler *handler;
Boris Pismenny075572d2017-08-16 09:33:30 +03003318 struct mlx5_flow_act flow_act = {.flow_tag = MLX5_FS_DEFAULT_FLOW_TAG};
Maor Gottliebc5bb1732016-07-04 17:23:05 +03003319 struct mlx5_flow_spec *spec;
Raed Salem3b3233f2018-05-31 16:43:39 +03003320 struct mlx5_flow_destination dest_arr[2] = {};
3321 struct mlx5_flow_destination *rule_dst = dest_arr;
Maor Gottliebdd063d02016-08-28 14:16:32 +03003322 const void *ib_flow = (const void *)flow_attr + sizeof(*flow_attr);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003323 unsigned int spec_index;
Ariel Levkovich71c6e862018-05-13 14:33:34 +03003324 u32 prev_type = 0;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003325 int err = 0;
Raed Salem3b3233f2018-05-31 16:43:39 +03003326 int dest_num = 0;
Aviad Yehezkel802c2122018-03-28 09:27:53 +03003327 bool is_egress = flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003328
Ariel Levkovich19cc7522017-04-03 13:11:03 +03003329 if (!is_valid_attr(dev->mdev, flow_attr))
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003330 return ERR_PTR(-EINVAL);
3331
Mark Bloch78dd0c42018-09-02 12:51:31 +03003332 if (dev->rep && is_egress)
3333 return ERR_PTR(-EINVAL);
3334
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03003335 spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003336 handler = kzalloc(sizeof(*handler), GFP_KERNEL);
Maor Gottliebc5bb1732016-07-04 17:23:05 +03003337 if (!handler || !spec) {
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003338 err = -ENOMEM;
3339 goto free;
3340 }
3341
3342 INIT_LIST_HEAD(&handler->list);
Raed Salem3b3233f2018-05-31 16:43:39 +03003343 if (dst) {
3344 memcpy(&dest_arr[0], dst, sizeof(*dst));
3345 dest_num++;
3346 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003347
3348 for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
Ariel Levkovich19cc7522017-04-03 13:11:03 +03003349 err = parse_flow_attr(dev->mdev, spec->match_criteria,
Slava Shwartsmana22ed862017-04-03 13:13:52 +03003350 spec->match_value,
Ariel Levkovich71c6e862018-05-13 14:33:34 +03003351 ib_flow, flow_attr, &flow_act,
3352 prev_type);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003353 if (err < 0)
3354 goto free;
3355
Ariel Levkovich71c6e862018-05-13 14:33:34 +03003356 prev_type = ((union ib_flow_spec *)ib_flow)->type;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003357 ib_flow += ((union ib_flow_spec *)ib_flow)->size;
3358 }
3359
Yishai Hadasa550ddf2017-08-17 15:52:33 +03003360 if (!flow_is_multicast_only(flow_attr))
3361 set_underlay_qp(dev, spec, underlay_qpn);
3362
Mark Bloch018a94e2018-01-16 14:44:29 +00003363 if (dev->rep) {
3364 void *misc;
3365
3366 misc = MLX5_ADDR_OF(fte_match_param, spec->match_value,
3367 misc_parameters);
3368 MLX5_SET(fte_match_set_misc, misc, source_port,
3369 dev->rep->vport);
3370 misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
3371 misc_parameters);
3372 MLX5_SET_TO_ONES(fte_match_set_misc, misc, source_port);
3373 }
3374
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03003375 spec->match_criteria_enable = get_match_criteria_enable(spec->match_criteria);
Aviad Yehezkel802c2122018-03-28 09:27:53 +03003376
3377 if (is_egress &&
3378 !is_valid_spec(dev->mdev, spec, &flow_act, is_egress)) {
3379 err = -EINVAL;
3380 goto free;
3381 }
3382
Raed Salem3b3233f2018-05-31 16:43:39 +03003383 if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_COUNT) {
3384 err = flow_counters_set_data(flow_act.counters, ucmd);
3385 if (err)
3386 goto free;
3387
3388 handler->ibcounters = flow_act.counters;
3389 dest_arr[dest_num].type =
3390 MLX5_FLOW_DESTINATION_TYPE_COUNTER;
3391 dest_arr[dest_num].counter =
3392 to_mcounters(flow_act.counters)->hw_cntrs_hndl;
3393 dest_num++;
3394 }
3395
Boris Pismenny075572d2017-08-16 09:33:30 +03003396 if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_DROP) {
Raed Salem3b3233f2018-05-31 16:43:39 +03003397 if (!(flow_act.action & MLX5_FLOW_CONTEXT_ACTION_COUNT)) {
3398 rule_dst = NULL;
3399 dest_num = 0;
3400 }
Slava Shwartsmana22ed862017-04-03 13:13:52 +03003401 } else {
Aviad Yehezkel802c2122018-03-28 09:27:53 +03003402 if (is_egress)
3403 flow_act.action |= MLX5_FLOW_CONTEXT_ACTION_ALLOW;
3404 else
3405 flow_act.action |=
Raed Salem3b3233f2018-05-31 16:43:39 +03003406 dest_num ? MLX5_FLOW_CONTEXT_ACTION_FWD_DEST :
Aviad Yehezkel802c2122018-03-28 09:27:53 +03003407 MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO;
Slava Shwartsmana22ed862017-04-03 13:13:52 +03003408 }
Moses Reuben2ac693f2017-01-18 14:59:50 +02003409
Matan Baraka9db0ec2017-08-16 09:43:48 +03003410 if (flow_act.has_flow_tag &&
Moses Reuben2ac693f2017-01-18 14:59:50 +02003411 (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
3412 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT)) {
3413 mlx5_ib_warn(dev, "Flow tag %u and attribute type %x isn't allowed in leftovers\n",
Boris Pismenny075572d2017-08-16 09:33:30 +03003414 flow_act.flow_tag, flow_attr->type);
Moses Reuben2ac693f2017-01-18 14:59:50 +02003415 err = -EINVAL;
3416 goto free;
3417 }
Mark Bloch74491de2016-08-31 11:24:25 +00003418 handler->rule = mlx5_add_flow_rules(ft, spec,
Hadar Hen Zion66958ed2016-11-07 15:14:45 +02003419 &flow_act,
Slava Shwartsmana22ed862017-04-03 13:13:52 +03003420 rule_dst, dest_num);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003421
3422 if (IS_ERR(handler->rule)) {
3423 err = PTR_ERR(handler->rule);
3424 goto free;
3425 }
3426
Maor Gottliebd9d49802016-08-28 14:16:33 +03003427 ft_prio->refcount++;
Maor Gottlieb5497adc2016-08-28 14:16:31 +03003428 handler->prio = ft_prio;
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003429 handler->dev = dev;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003430
3431 ft_prio->flow_table = ft;
3432free:
Raed Salem3b3233f2018-05-31 16:43:39 +03003433 if (err && handler) {
3434 if (handler->ibcounters &&
3435 atomic_read(&handler->ibcounters->usecnt) == 1)
3436 counters_clear_description(handler->ibcounters);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003437 kfree(handler);
Raed Salem3b3233f2018-05-31 16:43:39 +03003438 }
Maor Gottliebc5bb1732016-07-04 17:23:05 +03003439 kvfree(spec);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003440 return err ? ERR_PTR(err) : handler;
3441}
3442
Yishai Hadasa550ddf2017-08-17 15:52:33 +03003443static struct mlx5_ib_flow_handler *create_flow_rule(struct mlx5_ib_dev *dev,
3444 struct mlx5_ib_flow_prio *ft_prio,
3445 const struct ib_flow_attr *flow_attr,
3446 struct mlx5_flow_destination *dst)
3447{
Raed Salem3b3233f2018-05-31 16:43:39 +03003448 return _create_flow_rule(dev, ft_prio, flow_attr, dst, 0, NULL);
Yishai Hadasa550ddf2017-08-17 15:52:33 +03003449}
3450
Maor Gottlieb35d190112016-03-07 18:51:47 +02003451static struct mlx5_ib_flow_handler *create_dont_trap_rule(struct mlx5_ib_dev *dev,
3452 struct mlx5_ib_flow_prio *ft_prio,
3453 struct ib_flow_attr *flow_attr,
3454 struct mlx5_flow_destination *dst)
3455{
3456 struct mlx5_ib_flow_handler *handler_dst = NULL;
3457 struct mlx5_ib_flow_handler *handler = NULL;
3458
3459 handler = create_flow_rule(dev, ft_prio, flow_attr, NULL);
3460 if (!IS_ERR(handler)) {
3461 handler_dst = create_flow_rule(dev, ft_prio,
3462 flow_attr, dst);
3463 if (IS_ERR(handler_dst)) {
Mark Bloch74491de2016-08-31 11:24:25 +00003464 mlx5_del_flow_rules(handler->rule);
Maor Gottliebd9d49802016-08-28 14:16:33 +03003465 ft_prio->refcount--;
Maor Gottlieb35d190112016-03-07 18:51:47 +02003466 kfree(handler);
3467 handler = handler_dst;
3468 } else {
3469 list_add(&handler_dst->list, &handler->list);
3470 }
3471 }
3472
3473 return handler;
3474}
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003475enum {
3476 LEFTOVERS_MC,
3477 LEFTOVERS_UC,
3478};
3479
3480static struct mlx5_ib_flow_handler *create_leftovers_rule(struct mlx5_ib_dev *dev,
3481 struct mlx5_ib_flow_prio *ft_prio,
3482 struct ib_flow_attr *flow_attr,
3483 struct mlx5_flow_destination *dst)
3484{
3485 struct mlx5_ib_flow_handler *handler_ucast = NULL;
3486 struct mlx5_ib_flow_handler *handler = NULL;
3487
3488 static struct {
3489 struct ib_flow_attr flow_attr;
3490 struct ib_flow_spec_eth eth_flow;
3491 } leftovers_specs[] = {
3492 [LEFTOVERS_MC] = {
3493 .flow_attr = {
3494 .num_of_specs = 1,
3495 .size = sizeof(leftovers_specs[0])
3496 },
3497 .eth_flow = {
3498 .type = IB_FLOW_SPEC_ETH,
3499 .size = sizeof(struct ib_flow_spec_eth),
3500 .mask = {.dst_mac = {0x1} },
3501 .val = {.dst_mac = {0x1} }
3502 }
3503 },
3504 [LEFTOVERS_UC] = {
3505 .flow_attr = {
3506 .num_of_specs = 1,
3507 .size = sizeof(leftovers_specs[0])
3508 },
3509 .eth_flow = {
3510 .type = IB_FLOW_SPEC_ETH,
3511 .size = sizeof(struct ib_flow_spec_eth),
3512 .mask = {.dst_mac = {0x1} },
3513 .val = {.dst_mac = {} }
3514 }
3515 }
3516 };
3517
3518 handler = create_flow_rule(dev, ft_prio,
3519 &leftovers_specs[LEFTOVERS_MC].flow_attr,
3520 dst);
3521 if (!IS_ERR(handler) &&
3522 flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT) {
3523 handler_ucast = create_flow_rule(dev, ft_prio,
3524 &leftovers_specs[LEFTOVERS_UC].flow_attr,
3525 dst);
3526 if (IS_ERR(handler_ucast)) {
Mark Bloch74491de2016-08-31 11:24:25 +00003527 mlx5_del_flow_rules(handler->rule);
Maor Gottliebd9d49802016-08-28 14:16:33 +03003528 ft_prio->refcount--;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003529 kfree(handler);
3530 handler = handler_ucast;
3531 } else {
3532 list_add(&handler_ucast->list, &handler->list);
3533 }
3534 }
3535
3536 return handler;
3537}
3538
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003539static struct mlx5_ib_flow_handler *create_sniffer_rule(struct mlx5_ib_dev *dev,
3540 struct mlx5_ib_flow_prio *ft_rx,
3541 struct mlx5_ib_flow_prio *ft_tx,
3542 struct mlx5_flow_destination *dst)
3543{
3544 struct mlx5_ib_flow_handler *handler_rx;
3545 struct mlx5_ib_flow_handler *handler_tx;
3546 int err;
3547 static const struct ib_flow_attr flow_attr = {
3548 .num_of_specs = 0,
3549 .size = sizeof(flow_attr)
3550 };
3551
3552 handler_rx = create_flow_rule(dev, ft_rx, &flow_attr, dst);
3553 if (IS_ERR(handler_rx)) {
3554 err = PTR_ERR(handler_rx);
3555 goto err;
3556 }
3557
3558 handler_tx = create_flow_rule(dev, ft_tx, &flow_attr, dst);
3559 if (IS_ERR(handler_tx)) {
3560 err = PTR_ERR(handler_tx);
3561 goto err_tx;
3562 }
3563
3564 list_add(&handler_tx->list, &handler_rx->list);
3565
3566 return handler_rx;
3567
3568err_tx:
Mark Bloch74491de2016-08-31 11:24:25 +00003569 mlx5_del_flow_rules(handler_rx->rule);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003570 ft_rx->refcount--;
3571 kfree(handler_rx);
3572err:
3573 return ERR_PTR(err);
3574}
3575
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003576static struct ib_flow *mlx5_ib_create_flow(struct ib_qp *qp,
3577 struct ib_flow_attr *flow_attr,
Matan Barak59082a32018-05-31 16:43:35 +03003578 int domain,
3579 struct ib_udata *udata)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003580{
3581 struct mlx5_ib_dev *dev = to_mdev(qp->device);
Yishai Hadasd9f88e52016-08-28 10:58:37 +03003582 struct mlx5_ib_qp *mqp = to_mqp(qp);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003583 struct mlx5_ib_flow_handler *handler = NULL;
3584 struct mlx5_flow_destination *dst = NULL;
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003585 struct mlx5_ib_flow_prio *ft_prio_tx = NULL;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003586 struct mlx5_ib_flow_prio *ft_prio;
Aviad Yehezkel802c2122018-03-28 09:27:53 +03003587 bool is_egress = flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS;
Raed Salem3b3233f2018-05-31 16:43:39 +03003588 struct mlx5_ib_create_flow *ucmd = NULL, ucmd_hdr;
3589 size_t min_ucmd_sz, required_ucmd_sz;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003590 int err;
Yishai Hadasa550ddf2017-08-17 15:52:33 +03003591 int underlay_qpn;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003592
Raed Salem3b3233f2018-05-31 16:43:39 +03003593 if (udata && udata->inlen) {
3594 min_ucmd_sz = offsetof(typeof(ucmd_hdr), reserved) +
3595 sizeof(ucmd_hdr.reserved);
3596 if (udata->inlen < min_ucmd_sz)
3597 return ERR_PTR(-EOPNOTSUPP);
3598
3599 err = ib_copy_from_udata(&ucmd_hdr, udata, min_ucmd_sz);
3600 if (err)
3601 return ERR_PTR(err);
3602
3603 /* currently supports only one counters data */
3604 if (ucmd_hdr.ncounters_data > 1)
3605 return ERR_PTR(-EINVAL);
3606
3607 required_ucmd_sz = min_ucmd_sz +
3608 sizeof(struct mlx5_ib_flow_counters_data) *
3609 ucmd_hdr.ncounters_data;
3610 if (udata->inlen > required_ucmd_sz &&
3611 !ib_is_udata_cleared(udata, required_ucmd_sz,
3612 udata->inlen - required_ucmd_sz))
3613 return ERR_PTR(-EOPNOTSUPP);
3614
3615 ucmd = kzalloc(required_ucmd_sz, GFP_KERNEL);
3616 if (!ucmd)
3617 return ERR_PTR(-ENOMEM);
3618
3619 err = ib_copy_from_udata(ucmd, udata, required_ucmd_sz);
Gustavo A. R. Silva299eafe2018-06-07 14:19:15 -05003620 if (err)
3621 goto free_ucmd;
Raed Salem3b3233f2018-05-31 16:43:39 +03003622 }
Matan Barak59082a32018-05-31 16:43:35 +03003623
Gustavo A. R. Silva299eafe2018-06-07 14:19:15 -05003624 if (flow_attr->priority > MLX5_IB_FLOW_LAST_PRIO) {
3625 err = -ENOMEM;
3626 goto free_ucmd;
3627 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003628
3629 if (domain != IB_FLOW_DOMAIN_USER ||
Daniel Jurgens508562d2018-01-04 17:25:34 +02003630 flow_attr->port > dev->num_ports ||
Aviad Yehezkel802c2122018-03-28 09:27:53 +03003631 (flow_attr->flags & ~(IB_FLOW_ATTR_FLAGS_DONT_TRAP |
Gustavo A. R. Silva299eafe2018-06-07 14:19:15 -05003632 IB_FLOW_ATTR_FLAGS_EGRESS))) {
3633 err = -EINVAL;
3634 goto free_ucmd;
3635 }
Aviad Yehezkel802c2122018-03-28 09:27:53 +03003636
3637 if (is_egress &&
3638 (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
Gustavo A. R. Silva299eafe2018-06-07 14:19:15 -05003639 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT)) {
3640 err = -EINVAL;
3641 goto free_ucmd;
3642 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003643
3644 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
Gustavo A. R. Silva299eafe2018-06-07 14:19:15 -05003645 if (!dst) {
3646 err = -ENOMEM;
3647 goto free_ucmd;
3648 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003649
Mark Bloch9a4ca382018-01-16 14:42:35 +00003650 mutex_lock(&dev->flow_db->lock);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003651
Aviad Yehezkel802c2122018-03-28 09:27:53 +03003652 ft_prio = get_flow_table(dev, flow_attr,
3653 is_egress ? MLX5_IB_FT_TX : MLX5_IB_FT_RX);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003654 if (IS_ERR(ft_prio)) {
3655 err = PTR_ERR(ft_prio);
3656 goto unlock;
3657 }
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003658 if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
3659 ft_prio_tx = get_flow_table(dev, flow_attr, MLX5_IB_FT_TX);
3660 if (IS_ERR(ft_prio_tx)) {
3661 err = PTR_ERR(ft_prio_tx);
3662 ft_prio_tx = NULL;
3663 goto destroy_ft;
3664 }
3665 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003666
Aviad Yehezkel802c2122018-03-28 09:27:53 +03003667 if (is_egress) {
3668 dst->type = MLX5_FLOW_DESTINATION_TYPE_PORT;
3669 } else {
3670 dst->type = MLX5_FLOW_DESTINATION_TYPE_TIR;
3671 if (mqp->flags & MLX5_IB_QP_RSS)
3672 dst->tir_num = mqp->rss_qp.tirn;
3673 else
3674 dst->tir_num = mqp->raw_packet_qp.rq.tirn;
3675 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003676
3677 if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
Maor Gottlieb35d190112016-03-07 18:51:47 +02003678 if (flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP) {
3679 handler = create_dont_trap_rule(dev, ft_prio,
3680 flow_attr, dst);
3681 } else {
Yishai Hadasa550ddf2017-08-17 15:52:33 +03003682 underlay_qpn = (mqp->flags & MLX5_IB_QP_UNDERLAY) ?
3683 mqp->underlay_qpn : 0;
3684 handler = _create_flow_rule(dev, ft_prio, flow_attr,
Raed Salem3b3233f2018-05-31 16:43:39 +03003685 dst, underlay_qpn, ucmd);
Maor Gottlieb35d190112016-03-07 18:51:47 +02003686 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003687 } else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
3688 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
3689 handler = create_leftovers_rule(dev, ft_prio, flow_attr,
3690 dst);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003691 } else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
3692 handler = create_sniffer_rule(dev, ft_prio, ft_prio_tx, dst);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003693 } else {
3694 err = -EINVAL;
3695 goto destroy_ft;
3696 }
3697
3698 if (IS_ERR(handler)) {
3699 err = PTR_ERR(handler);
3700 handler = NULL;
3701 goto destroy_ft;
3702 }
3703
Mark Bloch9a4ca382018-01-16 14:42:35 +00003704 mutex_unlock(&dev->flow_db->lock);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003705 kfree(dst);
Raed Salem3b3233f2018-05-31 16:43:39 +03003706 kfree(ucmd);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003707
3708 return &handler->ibflow;
3709
3710destroy_ft:
3711 put_flow_table(dev, ft_prio, false);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003712 if (ft_prio_tx)
3713 put_flow_table(dev, ft_prio_tx, false);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003714unlock:
Mark Bloch9a4ca382018-01-16 14:42:35 +00003715 mutex_unlock(&dev->flow_db->lock);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003716 kfree(dst);
Gustavo A. R. Silva299eafe2018-06-07 14:19:15 -05003717free_ucmd:
Raed Salem3b3233f2018-05-31 16:43:39 +03003718 kfree(ucmd);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003719 return ERR_PTR(err);
3720}
3721
Mark Blochb47fd4f2018-09-06 17:27:07 +03003722static struct mlx5_ib_flow_prio *
3723_get_flow_table(struct mlx5_ib_dev *dev,
3724 struct mlx5_ib_flow_matcher *fs_matcher,
3725 bool mcast)
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003726{
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003727 struct mlx5_flow_namespace *ns = NULL;
3728 struct mlx5_ib_flow_prio *prio;
Mark Blochb47fd4f2018-09-06 17:27:07 +03003729 int max_table_size;
3730 u32 flags = 0;
3731 int priority;
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003732
Mark Blochb47fd4f2018-09-06 17:27:07 +03003733 if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_BYPASS) {
3734 max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
3735 log_max_ft_size));
3736 if (MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, decap))
3737 flags |= MLX5_FLOW_TABLE_TUNNEL_EN_DECAP;
3738 if (MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
3739 reformat_l3_tunnel_to_l2))
3740 flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
3741 } else { /* Can only be MLX5_FLOW_NAMESPACE_EGRESS */
3742 max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev,
3743 log_max_ft_size));
3744 if (MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev, reformat))
3745 flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
3746 }
3747
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003748 if (max_table_size < MLX5_FS_MAX_ENTRIES)
3749 return ERR_PTR(-ENOMEM);
3750
3751 if (mcast)
3752 priority = MLX5_IB_FLOW_MCAST_PRIO;
3753 else
Mark Blochb47fd4f2018-09-06 17:27:07 +03003754 priority = ib_prio_to_core_prio(fs_matcher->priority, false);
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003755
Mark Blochb47fd4f2018-09-06 17:27:07 +03003756 ns = mlx5_get_flow_namespace(dev->mdev, fs_matcher->ns_type);
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003757 if (!ns)
3758 return ERR_PTR(-ENOTSUPP);
3759
Mark Blochb47fd4f2018-09-06 17:27:07 +03003760 if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_BYPASS)
3761 prio = &dev->flow_db->prios[priority];
3762 else
3763 prio = &dev->flow_db->egress_prios[priority];
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003764
3765 if (prio->flow_table)
3766 return prio;
3767
3768 return _get_prio(ns, prio, priority, MLX5_FS_MAX_ENTRIES,
Mark Blochb47fd4f2018-09-06 17:27:07 +03003769 MLX5_FS_MAX_TYPES, flags);
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003770}
3771
3772static struct mlx5_ib_flow_handler *
3773_create_raw_flow_rule(struct mlx5_ib_dev *dev,
3774 struct mlx5_ib_flow_prio *ft_prio,
3775 struct mlx5_flow_destination *dst,
3776 struct mlx5_ib_flow_matcher *fs_matcher,
Mark Blochb823dd62018-09-06 17:27:05 +03003777 struct mlx5_flow_act *flow_act,
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003778 void *cmd_in, int inlen)
3779{
3780 struct mlx5_ib_flow_handler *handler;
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003781 struct mlx5_flow_spec *spec;
3782 struct mlx5_flow_table *ft = ft_prio->flow_table;
3783 int err = 0;
3784
3785 spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
3786 handler = kzalloc(sizeof(*handler), GFP_KERNEL);
3787 if (!handler || !spec) {
3788 err = -ENOMEM;
3789 goto free;
3790 }
3791
3792 INIT_LIST_HEAD(&handler->list);
3793
3794 memcpy(spec->match_value, cmd_in, inlen);
3795 memcpy(spec->match_criteria, fs_matcher->matcher_mask.match_params,
3796 fs_matcher->mask_len);
3797 spec->match_criteria_enable = fs_matcher->match_criteria_enable;
3798
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003799 handler->rule = mlx5_add_flow_rules(ft, spec,
Mark Blochb823dd62018-09-06 17:27:05 +03003800 flow_act, dst, 1);
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003801
3802 if (IS_ERR(handler->rule)) {
3803 err = PTR_ERR(handler->rule);
3804 goto free;
3805 }
3806
3807 ft_prio->refcount++;
3808 handler->prio = ft_prio;
3809 handler->dev = dev;
3810 ft_prio->flow_table = ft;
3811
3812free:
3813 if (err)
3814 kfree(handler);
3815 kvfree(spec);
3816 return err ? ERR_PTR(err) : handler;
3817}
3818
3819static bool raw_fs_is_multicast(struct mlx5_ib_flow_matcher *fs_matcher,
3820 void *match_v)
3821{
3822 void *match_c;
3823 void *match_v_set_lyr_2_4, *match_c_set_lyr_2_4;
3824 void *dmac, *dmac_mask;
3825 void *ipv4, *ipv4_mask;
3826
3827 if (!(fs_matcher->match_criteria_enable &
3828 (1 << MATCH_CRITERIA_ENABLE_OUTER_BIT)))
3829 return false;
3830
3831 match_c = fs_matcher->matcher_mask.match_params;
3832 match_v_set_lyr_2_4 = MLX5_ADDR_OF(fte_match_param, match_v,
3833 outer_headers);
3834 match_c_set_lyr_2_4 = MLX5_ADDR_OF(fte_match_param, match_c,
3835 outer_headers);
3836
3837 dmac = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_v_set_lyr_2_4,
3838 dmac_47_16);
3839 dmac_mask = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_c_set_lyr_2_4,
3840 dmac_47_16);
3841
3842 if (is_multicast_ether_addr(dmac) &&
3843 is_multicast_ether_addr(dmac_mask))
3844 return true;
3845
3846 ipv4 = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_v_set_lyr_2_4,
3847 dst_ipv4_dst_ipv6.ipv4_layout.ipv4);
3848
3849 ipv4_mask = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_c_set_lyr_2_4,
3850 dst_ipv4_dst_ipv6.ipv4_layout.ipv4);
3851
3852 if (ipv4_is_multicast(*(__be32 *)(ipv4)) &&
3853 ipv4_is_multicast(*(__be32 *)(ipv4_mask)))
3854 return true;
3855
3856 return false;
3857}
3858
Yishai Hadas32269442018-07-23 15:25:09 +03003859struct mlx5_ib_flow_handler *
3860mlx5_ib_raw_fs_rule_add(struct mlx5_ib_dev *dev,
3861 struct mlx5_ib_flow_matcher *fs_matcher,
Mark Blochb823dd62018-09-06 17:27:05 +03003862 struct mlx5_flow_act *flow_act,
Yishai Hadas32269442018-07-23 15:25:09 +03003863 void *cmd_in, int inlen, int dest_id,
3864 int dest_type)
3865{
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003866 struct mlx5_flow_destination *dst;
3867 struct mlx5_ib_flow_prio *ft_prio;
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003868 struct mlx5_ib_flow_handler *handler;
3869 bool mcast;
3870 int err;
3871
3872 if (fs_matcher->flow_type != MLX5_IB_FLOW_TYPE_NORMAL)
3873 return ERR_PTR(-EOPNOTSUPP);
3874
3875 if (fs_matcher->priority > MLX5_IB_FLOW_LAST_PRIO)
3876 return ERR_PTR(-ENOMEM);
3877
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003878 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
3879 if (!dst)
3880 return ERR_PTR(-ENOMEM);
3881
3882 mcast = raw_fs_is_multicast(fs_matcher, cmd_in);
3883 mutex_lock(&dev->flow_db->lock);
3884
Mark Blochb47fd4f2018-09-06 17:27:07 +03003885 ft_prio = _get_flow_table(dev, fs_matcher, mcast);
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003886 if (IS_ERR(ft_prio)) {
3887 err = PTR_ERR(ft_prio);
3888 goto unlock;
3889 }
3890
Yishai Hadas6346f0b2018-07-23 15:25:11 +03003891 if (dest_type == MLX5_FLOW_DESTINATION_TYPE_TIR) {
3892 dst->type = dest_type;
3893 dst->tir_num = dest_id;
Mark Blochb823dd62018-09-06 17:27:05 +03003894 flow_act->action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
Mark Blocha7ee18b2018-09-06 17:27:08 +03003895 } else if (dest_type == MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE) {
Yishai Hadas6346f0b2018-07-23 15:25:11 +03003896 dst->type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE_NUM;
3897 dst->ft_num = dest_id;
Mark Blochb823dd62018-09-06 17:27:05 +03003898 flow_act->action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
Mark Blocha7ee18b2018-09-06 17:27:08 +03003899 } else {
3900 dst->type = MLX5_FLOW_DESTINATION_TYPE_PORT;
3901 flow_act->action |= MLX5_FLOW_CONTEXT_ACTION_ALLOW;
Yishai Hadas6346f0b2018-07-23 15:25:11 +03003902 }
3903
Mark Blochb823dd62018-09-06 17:27:05 +03003904 handler = _create_raw_flow_rule(dev, ft_prio, dst, fs_matcher, flow_act,
3905 cmd_in, inlen);
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003906
3907 if (IS_ERR(handler)) {
3908 err = PTR_ERR(handler);
3909 goto destroy_ft;
3910 }
3911
3912 mutex_unlock(&dev->flow_db->lock);
3913 atomic_inc(&fs_matcher->usecnt);
3914 handler->flow_matcher = fs_matcher;
3915
3916 kfree(dst);
3917
3918 return handler;
3919
3920destroy_ft:
3921 put_flow_table(dev, ft_prio, false);
3922unlock:
3923 mutex_unlock(&dev->flow_db->lock);
3924 kfree(dst);
3925
3926 return ERR_PTR(err);
Yishai Hadas32269442018-07-23 15:25:09 +03003927}
3928
Aviad Yehezkelc6475a02018-03-28 09:27:50 +03003929static u32 mlx5_ib_flow_action_flags_to_accel_xfrm_flags(u32 mlx5_flags)
3930{
3931 u32 flags = 0;
3932
3933 if (mlx5_flags & MLX5_IB_UAPI_FLOW_ACTION_FLAGS_REQUIRE_METADATA)
3934 flags |= MLX5_ACCEL_XFRM_FLAG_REQUIRE_METADATA;
3935
3936 return flags;
3937}
3938
3939#define MLX5_FLOW_ACTION_ESP_CREATE_LAST_SUPPORTED MLX5_IB_UAPI_FLOW_ACTION_FLAGS_REQUIRE_METADATA
3940static struct ib_flow_action *
3941mlx5_ib_create_flow_action_esp(struct ib_device *device,
3942 const struct ib_flow_action_attrs_esp *attr,
3943 struct uverbs_attr_bundle *attrs)
3944{
3945 struct mlx5_ib_dev *mdev = to_mdev(device);
3946 struct ib_uverbs_flow_action_esp_keymat_aes_gcm *aes_gcm;
3947 struct mlx5_accel_esp_xfrm_attrs accel_attrs = {};
3948 struct mlx5_ib_flow_action *action;
3949 u64 action_flags;
3950 u64 flags;
3951 int err = 0;
3952
Jason Gunthorpebccd0622018-07-26 16:37:14 -06003953 err = uverbs_get_flags64(
3954 &action_flags, attrs, MLX5_IB_ATTR_CREATE_FLOW_ACTION_FLAGS,
3955 ((MLX5_FLOW_ACTION_ESP_CREATE_LAST_SUPPORTED << 1) - 1));
3956 if (err)
3957 return ERR_PTR(err);
Aviad Yehezkelc6475a02018-03-28 09:27:50 +03003958
3959 flags = mlx5_ib_flow_action_flags_to_accel_xfrm_flags(action_flags);
3960
3961 /* We current only support a subset of the standard features. Only a
3962 * keymat of type AES_GCM, with icv_len == 16, iv_algo == SEQ and esn
3963 * (with overlap). Full offload mode isn't supported.
3964 */
3965 if (!attr->keymat || attr->replay || attr->encap ||
3966 attr->spi || attr->seq || attr->tfc_pad ||
3967 attr->hard_limit_pkts ||
3968 (attr->flags & ~(IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED |
3969 IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ENCRYPT)))
3970 return ERR_PTR(-EOPNOTSUPP);
3971
3972 if (attr->keymat->protocol !=
3973 IB_UVERBS_FLOW_ACTION_ESP_KEYMAT_AES_GCM)
3974 return ERR_PTR(-EOPNOTSUPP);
3975
3976 aes_gcm = &attr->keymat->keymat.aes_gcm;
3977
3978 if (aes_gcm->icv_len != 16 ||
3979 aes_gcm->iv_algo != IB_UVERBS_FLOW_ACTION_IV_ALGO_SEQ)
3980 return ERR_PTR(-EOPNOTSUPP);
3981
3982 action = kmalloc(sizeof(*action), GFP_KERNEL);
3983 if (!action)
3984 return ERR_PTR(-ENOMEM);
3985
3986 action->esp_aes_gcm.ib_flags = attr->flags;
3987 memcpy(&accel_attrs.keymat.aes_gcm.aes_key, &aes_gcm->aes_key,
3988 sizeof(accel_attrs.keymat.aes_gcm.aes_key));
3989 accel_attrs.keymat.aes_gcm.key_len = aes_gcm->key_len * 8;
3990 memcpy(&accel_attrs.keymat.aes_gcm.salt, &aes_gcm->salt,
3991 sizeof(accel_attrs.keymat.aes_gcm.salt));
3992 memcpy(&accel_attrs.keymat.aes_gcm.seq_iv, &aes_gcm->iv,
3993 sizeof(accel_attrs.keymat.aes_gcm.seq_iv));
3994 accel_attrs.keymat.aes_gcm.icv_len = aes_gcm->icv_len * 8;
3995 accel_attrs.keymat.aes_gcm.iv_algo = MLX5_ACCEL_ESP_AES_GCM_IV_ALGO_SEQ;
3996 accel_attrs.keymat_type = MLX5_ACCEL_ESP_KEYMAT_AES_GCM;
3997
3998 accel_attrs.esn = attr->esn;
3999 if (attr->flags & IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED)
4000 accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_TRIGGERED;
4001 if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW)
4002 accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP;
4003
4004 if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ENCRYPT)
4005 accel_attrs.action |= MLX5_ACCEL_ESP_ACTION_ENCRYPT;
4006
4007 action->esp_aes_gcm.ctx =
4008 mlx5_accel_esp_create_xfrm(mdev->mdev, &accel_attrs, flags);
4009 if (IS_ERR(action->esp_aes_gcm.ctx)) {
4010 err = PTR_ERR(action->esp_aes_gcm.ctx);
4011 goto err_parse;
4012 }
4013
4014 action->esp_aes_gcm.ib_flags = attr->flags;
4015
4016 return &action->ib_action;
4017
4018err_parse:
4019 kfree(action);
4020 return ERR_PTR(err);
4021}
4022
Matan Barak349705c2018-03-28 09:27:51 +03004023static int
4024mlx5_ib_modify_flow_action_esp(struct ib_flow_action *action,
4025 const struct ib_flow_action_attrs_esp *attr,
4026 struct uverbs_attr_bundle *attrs)
4027{
4028 struct mlx5_ib_flow_action *maction = to_mflow_act(action);
4029 struct mlx5_accel_esp_xfrm_attrs accel_attrs;
4030 int err = 0;
4031
4032 if (attr->keymat || attr->replay || attr->encap ||
4033 attr->spi || attr->seq || attr->tfc_pad ||
4034 attr->hard_limit_pkts ||
4035 (attr->flags & ~(IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED |
4036 IB_FLOW_ACTION_ESP_FLAGS_MOD_ESP_ATTRS |
4037 IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW)))
4038 return -EOPNOTSUPP;
4039
4040 /* Only the ESN value or the MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP can
4041 * be modified.
4042 */
4043 if (!(maction->esp_aes_gcm.ib_flags &
4044 IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED) &&
4045 attr->flags & (IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED |
4046 IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW))
4047 return -EINVAL;
4048
4049 memcpy(&accel_attrs, &maction->esp_aes_gcm.ctx->attrs,
4050 sizeof(accel_attrs));
4051
4052 accel_attrs.esn = attr->esn;
4053 if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW)
4054 accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP;
4055 else
4056 accel_attrs.flags &= ~MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP;
4057
4058 err = mlx5_accel_esp_modify_xfrm(maction->esp_aes_gcm.ctx,
4059 &accel_attrs);
4060 if (err)
4061 return err;
4062
4063 maction->esp_aes_gcm.ib_flags &=
4064 ~IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW;
4065 maction->esp_aes_gcm.ib_flags |=
4066 attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW;
4067
4068 return 0;
4069}
4070
Aviad Yehezkelc6475a02018-03-28 09:27:50 +03004071static int mlx5_ib_destroy_flow_action(struct ib_flow_action *action)
4072{
4073 struct mlx5_ib_flow_action *maction = to_mflow_act(action);
4074
4075 switch (action->type) {
4076 case IB_FLOW_ACTION_ESP:
4077 /*
4078 * We only support aes_gcm by now, so we implicitly know this is
4079 * the underline crypto.
4080 */
4081 mlx5_accel_esp_destroy_xfrm(maction->esp_aes_gcm.ctx);
4082 break;
Mark Blochb4749bf2018-08-28 14:18:51 +03004083 case IB_FLOW_ACTION_UNSPECIFIED:
4084 mlx5_ib_destroy_flow_action_raw(maction);
4085 break;
Aviad Yehezkelc6475a02018-03-28 09:27:50 +03004086 default:
4087 WARN_ON(true);
4088 break;
4089 }
4090
4091 kfree(maction);
4092 return 0;
4093}
4094
Eli Cohene126ba92013-07-07 17:25:49 +03004095static int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
4096{
4097 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
Yishai Hadas81e30882017-06-08 16:15:09 +03004098 struct mlx5_ib_qp *mqp = to_mqp(ibqp);
Eli Cohene126ba92013-07-07 17:25:49 +03004099 int err;
4100
Yishai Hadas81e30882017-06-08 16:15:09 +03004101 if (mqp->flags & MLX5_IB_QP_UNDERLAY) {
4102 mlx5_ib_dbg(dev, "Attaching a multi cast group to underlay QP is not supported\n");
4103 return -EOPNOTSUPP;
4104 }
4105
Jack Morgenstein9603b612014-07-28 23:30:22 +03004106 err = mlx5_core_attach_mcg(dev->mdev, gid, ibqp->qp_num);
Eli Cohene126ba92013-07-07 17:25:49 +03004107 if (err)
4108 mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n",
4109 ibqp->qp_num, gid->raw);
4110
4111 return err;
4112}
4113
4114static int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
4115{
4116 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
4117 int err;
4118
Jack Morgenstein9603b612014-07-28 23:30:22 +03004119 err = mlx5_core_detach_mcg(dev->mdev, gid, ibqp->qp_num);
Eli Cohene126ba92013-07-07 17:25:49 +03004120 if (err)
4121 mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n",
4122 ibqp->qp_num, gid->raw);
4123
4124 return err;
4125}
4126
4127static int init_node_data(struct mlx5_ib_dev *dev)
4128{
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03004129 int err;
Eli Cohene126ba92013-07-07 17:25:49 +03004130
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03004131 err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc);
Eli Cohene126ba92013-07-07 17:25:49 +03004132 if (err)
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03004133 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03004134
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03004135 dev->mdev->rev_id = dev->mdev->pdev->revision;
Eli Cohene126ba92013-07-07 17:25:49 +03004136
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03004137 return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid);
Eli Cohene126ba92013-07-07 17:25:49 +03004138}
4139
4140static ssize_t show_fw_pages(struct device *device, struct device_attribute *attr,
4141 char *buf)
4142{
4143 struct mlx5_ib_dev *dev =
4144 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
4145
Jack Morgenstein9603b612014-07-28 23:30:22 +03004146 return sprintf(buf, "%d\n", dev->mdev->priv.fw_pages);
Eli Cohene126ba92013-07-07 17:25:49 +03004147}
4148
4149static ssize_t show_reg_pages(struct device *device,
4150 struct device_attribute *attr, char *buf)
4151{
4152 struct mlx5_ib_dev *dev =
4153 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
4154
Haggai Eran6aec21f2014-12-11 17:04:23 +02004155 return sprintf(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages));
Eli Cohene126ba92013-07-07 17:25:49 +03004156}
4157
4158static ssize_t show_hca(struct device *device, struct device_attribute *attr,
4159 char *buf)
4160{
4161 struct mlx5_ib_dev *dev =
4162 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
Jack Morgenstein9603b612014-07-28 23:30:22 +03004163 return sprintf(buf, "MT%d\n", dev->mdev->pdev->device);
Eli Cohene126ba92013-07-07 17:25:49 +03004164}
4165
Eli Cohene126ba92013-07-07 17:25:49 +03004166static ssize_t show_rev(struct device *device, struct device_attribute *attr,
4167 char *buf)
4168{
4169 struct mlx5_ib_dev *dev =
4170 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
Jack Morgenstein9603b612014-07-28 23:30:22 +03004171 return sprintf(buf, "%x\n", dev->mdev->rev_id);
Eli Cohene126ba92013-07-07 17:25:49 +03004172}
4173
4174static ssize_t show_board(struct device *device, struct device_attribute *attr,
4175 char *buf)
4176{
4177 struct mlx5_ib_dev *dev =
4178 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
4179 return sprintf(buf, "%.*s\n", MLX5_BOARD_ID_LEN,
Jack Morgenstein9603b612014-07-28 23:30:22 +03004180 dev->mdev->board_id);
Eli Cohene126ba92013-07-07 17:25:49 +03004181}
4182
4183static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03004184static DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL);
4185static DEVICE_ATTR(board_id, S_IRUGO, show_board, NULL);
4186static DEVICE_ATTR(fw_pages, S_IRUGO, show_fw_pages, NULL);
4187static DEVICE_ATTR(reg_pages, S_IRUGO, show_reg_pages, NULL);
4188
4189static struct device_attribute *mlx5_class_attributes[] = {
4190 &dev_attr_hw_rev,
Eli Cohene126ba92013-07-07 17:25:49 +03004191 &dev_attr_hca_type,
4192 &dev_attr_board_id,
4193 &dev_attr_fw_pages,
4194 &dev_attr_reg_pages,
4195};
4196
Haggai Eran7722f472016-02-29 15:45:07 +02004197static void pkey_change_handler(struct work_struct *work)
4198{
4199 struct mlx5_ib_port_resources *ports =
4200 container_of(work, struct mlx5_ib_port_resources,
4201 pkey_change_work);
4202
4203 mutex_lock(&ports->devr->mutex);
4204 mlx5_ib_gsi_pkey_change(ports->gsi);
4205 mutex_unlock(&ports->devr->mutex);
4206}
4207
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03004208static void mlx5_ib_handle_internal_error(struct mlx5_ib_dev *ibdev)
4209{
4210 struct mlx5_ib_qp *mqp;
4211 struct mlx5_ib_cq *send_mcq, *recv_mcq;
4212 struct mlx5_core_cq *mcq;
4213 struct list_head cq_armed_list;
4214 unsigned long flags_qp;
4215 unsigned long flags_cq;
4216 unsigned long flags;
4217
4218 INIT_LIST_HEAD(&cq_armed_list);
4219
4220 /* Go over qp list reside on that ibdev, sync with create/destroy qp.*/
4221 spin_lock_irqsave(&ibdev->reset_flow_resource_lock, flags);
4222 list_for_each_entry(mqp, &ibdev->qp_list, qps_list) {
4223 spin_lock_irqsave(&mqp->sq.lock, flags_qp);
4224 if (mqp->sq.tail != mqp->sq.head) {
4225 send_mcq = to_mcq(mqp->ibqp.send_cq);
4226 spin_lock_irqsave(&send_mcq->lock, flags_cq);
4227 if (send_mcq->mcq.comp &&
4228 mqp->ibqp.send_cq->comp_handler) {
4229 if (!send_mcq->mcq.reset_notify_added) {
4230 send_mcq->mcq.reset_notify_added = 1;
4231 list_add_tail(&send_mcq->mcq.reset_notify,
4232 &cq_armed_list);
4233 }
4234 }
4235 spin_unlock_irqrestore(&send_mcq->lock, flags_cq);
4236 }
4237 spin_unlock_irqrestore(&mqp->sq.lock, flags_qp);
4238 spin_lock_irqsave(&mqp->rq.lock, flags_qp);
4239 /* no handling is needed for SRQ */
4240 if (!mqp->ibqp.srq) {
4241 if (mqp->rq.tail != mqp->rq.head) {
4242 recv_mcq = to_mcq(mqp->ibqp.recv_cq);
4243 spin_lock_irqsave(&recv_mcq->lock, flags_cq);
4244 if (recv_mcq->mcq.comp &&
4245 mqp->ibqp.recv_cq->comp_handler) {
4246 if (!recv_mcq->mcq.reset_notify_added) {
4247 recv_mcq->mcq.reset_notify_added = 1;
4248 list_add_tail(&recv_mcq->mcq.reset_notify,
4249 &cq_armed_list);
4250 }
4251 }
4252 spin_unlock_irqrestore(&recv_mcq->lock,
4253 flags_cq);
4254 }
4255 }
4256 spin_unlock_irqrestore(&mqp->rq.lock, flags_qp);
4257 }
4258 /*At that point all inflight post send were put to be executed as of we
4259 * lock/unlock above locks Now need to arm all involved CQs.
4260 */
4261 list_for_each_entry(mcq, &cq_armed_list, reset_notify) {
4262 mcq->comp(mcq);
4263 }
4264 spin_unlock_irqrestore(&ibdev->reset_flow_resource_lock, flags);
4265}
4266
Maor Gottlieb03404e82017-05-30 10:29:13 +03004267static void delay_drop_handler(struct work_struct *work)
4268{
4269 int err;
4270 struct mlx5_ib_delay_drop *delay_drop =
4271 container_of(work, struct mlx5_ib_delay_drop,
4272 delay_drop_work);
4273
Maor Gottliebfe248c32017-05-30 10:29:14 +03004274 atomic_inc(&delay_drop->events_cnt);
4275
Maor Gottlieb03404e82017-05-30 10:29:13 +03004276 mutex_lock(&delay_drop->lock);
4277 err = mlx5_core_set_delay_drop(delay_drop->dev->mdev,
4278 delay_drop->timeout);
4279 if (err) {
4280 mlx5_ib_warn(delay_drop->dev, "Failed to set delay drop, timeout=%u\n",
4281 delay_drop->timeout);
4282 delay_drop->activate = false;
4283 }
4284 mutex_unlock(&delay_drop->lock);
4285}
4286
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02004287static void mlx5_ib_handle_event(struct work_struct *_work)
Eli Cohene126ba92013-07-07 17:25:49 +03004288{
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02004289 struct mlx5_ib_event_work *work =
4290 container_of(_work, struct mlx5_ib_event_work, work);
4291 struct mlx5_ib_dev *ibdev;
Eli Cohene126ba92013-07-07 17:25:49 +03004292 struct ib_event ibev;
Eli Cohendbaaff22016-10-27 16:36:44 +03004293 bool fatal = false;
Daniel Jurgensaba46212018-02-25 13:39:53 +02004294 u8 port = (u8)work->param;
Eli Cohene126ba92013-07-07 17:25:49 +03004295
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02004296 if (mlx5_core_is_mp_slave(work->dev)) {
4297 ibdev = mlx5_ib_get_ibdev_from_mpi(work->context);
4298 if (!ibdev)
4299 goto out;
4300 } else {
4301 ibdev = work->context;
4302 }
4303
4304 switch (work->event) {
Eli Cohene126ba92013-07-07 17:25:49 +03004305 case MLX5_DEV_EVENT_SYS_ERROR:
Eli Cohene126ba92013-07-07 17:25:49 +03004306 ibev.event = IB_EVENT_DEVICE_FATAL;
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03004307 mlx5_ib_handle_internal_error(ibdev);
Eli Cohendbaaff22016-10-27 16:36:44 +03004308 fatal = true;
Eli Cohene126ba92013-07-07 17:25:49 +03004309 break;
4310
4311 case MLX5_DEV_EVENT_PORT_UP:
Eli Cohene126ba92013-07-07 17:25:49 +03004312 case MLX5_DEV_EVENT_PORT_DOWN:
Noa Osherovich2788cf32016-06-04 15:15:29 +03004313 case MLX5_DEV_EVENT_PORT_INITIALIZED:
Aviv Heller5ec8c832016-09-18 20:48:00 +03004314 /* In RoCE, port up/down events are handled in
4315 * mlx5_netdev_event().
4316 */
4317 if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) ==
4318 IB_LINK_LAYER_ETHERNET)
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02004319 goto out;
Aviv Heller5ec8c832016-09-18 20:48:00 +03004320
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02004321 ibev.event = (work->event == MLX5_DEV_EVENT_PORT_UP) ?
Aviv Heller5ec8c832016-09-18 20:48:00 +03004322 IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
Eli Cohene126ba92013-07-07 17:25:49 +03004323 break;
4324
Eli Cohene126ba92013-07-07 17:25:49 +03004325 case MLX5_DEV_EVENT_LID_CHANGE:
4326 ibev.event = IB_EVENT_LID_CHANGE;
Eli Cohene126ba92013-07-07 17:25:49 +03004327 break;
4328
4329 case MLX5_DEV_EVENT_PKEY_CHANGE:
4330 ibev.event = IB_EVENT_PKEY_CHANGE;
Haggai Eran7722f472016-02-29 15:45:07 +02004331 schedule_work(&ibdev->devr.ports[port - 1].pkey_change_work);
Eli Cohene126ba92013-07-07 17:25:49 +03004332 break;
4333
4334 case MLX5_DEV_EVENT_GUID_CHANGE:
4335 ibev.event = IB_EVENT_GID_CHANGE;
Eli Cohene126ba92013-07-07 17:25:49 +03004336 break;
4337
4338 case MLX5_DEV_EVENT_CLIENT_REREG:
4339 ibev.event = IB_EVENT_CLIENT_REREGISTER;
Eli Cohene126ba92013-07-07 17:25:49 +03004340 break;
Maor Gottlieb03404e82017-05-30 10:29:13 +03004341 case MLX5_DEV_EVENT_DELAY_DROP_TIMEOUT:
4342 schedule_work(&ibdev->delay_drop.delay_drop_work);
4343 goto out;
Saeed Mahameedbdc37922016-09-29 19:35:38 +03004344 default:
Maor Gottlieb03404e82017-05-30 10:29:13 +03004345 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03004346 }
4347
4348 ibev.device = &ibdev->ib_dev;
4349 ibev.element.port_num = port;
4350
Daniel Jurgensaba46212018-02-25 13:39:53 +02004351 if (!rdma_is_port_valid(&ibdev->ib_dev, port)) {
Eli Cohena0c84c32013-09-11 16:35:27 +03004352 mlx5_ib_warn(ibdev, "warning: event on port %d\n", port);
Maor Gottlieb03404e82017-05-30 10:29:13 +03004353 goto out;
Eli Cohena0c84c32013-09-11 16:35:27 +03004354 }
4355
Eli Cohene126ba92013-07-07 17:25:49 +03004356 if (ibdev->ib_active)
4357 ib_dispatch_event(&ibev);
Eli Cohendbaaff22016-10-27 16:36:44 +03004358
4359 if (fatal)
4360 ibdev->ib_active = false;
Maor Gottlieb03404e82017-05-30 10:29:13 +03004361out:
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02004362 kfree(work);
4363}
4364
4365static void mlx5_ib_event(struct mlx5_core_dev *dev, void *context,
4366 enum mlx5_dev_event event, unsigned long param)
4367{
4368 struct mlx5_ib_event_work *work;
4369
4370 work = kmalloc(sizeof(*work), GFP_ATOMIC);
Leon Romanovsky10bea9c2018-01-19 13:07:11 +02004371 if (!work)
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02004372 return;
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02004373
Leon Romanovsky10bea9c2018-01-19 13:07:11 +02004374 INIT_WORK(&work->work, mlx5_ib_handle_event);
4375 work->dev = dev;
4376 work->param = param;
4377 work->context = context;
4378 work->event = event;
4379
4380 queue_work(mlx5_ib_event_wq, &work->work);
Eli Cohene126ba92013-07-07 17:25:49 +03004381}
4382
Maor Gottliebc43f1112017-01-18 14:10:33 +02004383static int set_has_smi_cap(struct mlx5_ib_dev *dev)
4384{
4385 struct mlx5_hca_vport_context vport_ctx;
4386 int err;
4387 int port;
4388
Daniel Jurgens508562d2018-01-04 17:25:34 +02004389 for (port = 1; port <= dev->num_ports; port++) {
Maor Gottliebc43f1112017-01-18 14:10:33 +02004390 dev->mdev->port_caps[port - 1].has_smi = false;
4391 if (MLX5_CAP_GEN(dev->mdev, port_type) ==
4392 MLX5_CAP_PORT_TYPE_IB) {
4393 if (MLX5_CAP_GEN(dev->mdev, ib_virt)) {
4394 err = mlx5_query_hca_vport_context(dev->mdev, 0,
4395 port, 0,
4396 &vport_ctx);
4397 if (err) {
4398 mlx5_ib_err(dev, "query_hca_vport_context for port=%d failed %d\n",
4399 port, err);
4400 return err;
4401 }
4402 dev->mdev->port_caps[port - 1].has_smi =
4403 vport_ctx.has_smi;
4404 } else {
4405 dev->mdev->port_caps[port - 1].has_smi = true;
4406 }
4407 }
4408 }
4409 return 0;
4410}
4411
Eli Cohene126ba92013-07-07 17:25:49 +03004412static void get_ext_port_caps(struct mlx5_ib_dev *dev)
4413{
4414 int port;
4415
Daniel Jurgens508562d2018-01-04 17:25:34 +02004416 for (port = 1; port <= dev->num_ports; port++)
Eli Cohene126ba92013-07-07 17:25:49 +03004417 mlx5_query_ext_port_caps(dev, port);
4418}
4419
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004420static int get_port_caps(struct mlx5_ib_dev *dev, u8 port)
Eli Cohene126ba92013-07-07 17:25:49 +03004421{
4422 struct ib_device_attr *dprops = NULL;
4423 struct ib_port_attr *pprops = NULL;
Dan Carpenterf614fc12015-01-12 11:56:58 +03004424 int err = -ENOMEM;
Matan Barak2528e332015-06-11 16:35:25 +03004425 struct ib_udata uhw = {.inlen = 0, .outlen = 0};
Eli Cohene126ba92013-07-07 17:25:49 +03004426
4427 pprops = kmalloc(sizeof(*pprops), GFP_KERNEL);
4428 if (!pprops)
4429 goto out;
4430
4431 dprops = kmalloc(sizeof(*dprops), GFP_KERNEL);
4432 if (!dprops)
4433 goto out;
4434
Maor Gottliebc43f1112017-01-18 14:10:33 +02004435 err = set_has_smi_cap(dev);
4436 if (err)
4437 goto out;
4438
Matan Barak2528e332015-06-11 16:35:25 +03004439 err = mlx5_ib_query_device(&dev->ib_dev, dprops, &uhw);
Eli Cohene126ba92013-07-07 17:25:49 +03004440 if (err) {
4441 mlx5_ib_warn(dev, "query_device failed %d\n", err);
4442 goto out;
4443 }
4444
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004445 memset(pprops, 0, sizeof(*pprops));
4446 err = mlx5_ib_query_port(&dev->ib_dev, port, pprops);
4447 if (err) {
4448 mlx5_ib_warn(dev, "query_port %d failed %d\n",
4449 port, err);
4450 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03004451 }
4452
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004453 dev->mdev->port_caps[port - 1].pkey_table_len =
4454 dprops->max_pkeys;
4455 dev->mdev->port_caps[port - 1].gid_table_len =
4456 pprops->gid_tbl_len;
4457 mlx5_ib_dbg(dev, "port %d: pkey_table_len %d, gid_table_len %d\n",
4458 port, dprops->max_pkeys, pprops->gid_tbl_len);
4459
Eli Cohene126ba92013-07-07 17:25:49 +03004460out:
4461 kfree(pprops);
4462 kfree(dprops);
4463
4464 return err;
4465}
4466
4467static void destroy_umrc_res(struct mlx5_ib_dev *dev)
4468{
4469 int err;
4470
4471 err = mlx5_mr_cache_cleanup(dev);
4472 if (err)
4473 mlx5_ib_warn(dev, "mr cache cleanup failed\n");
4474
Mark Bloch32927e22018-03-20 15:45:37 +02004475 if (dev->umrc.qp)
4476 mlx5_ib_destroy_qp(dev->umrc.qp);
4477 if (dev->umrc.cq)
4478 ib_free_cq(dev->umrc.cq);
4479 if (dev->umrc.pd)
4480 ib_dealloc_pd(dev->umrc.pd);
Eli Cohene126ba92013-07-07 17:25:49 +03004481}
4482
4483enum {
4484 MAX_UMR_WR = 128,
4485};
4486
4487static int create_umr_res(struct mlx5_ib_dev *dev)
4488{
4489 struct ib_qp_init_attr *init_attr = NULL;
4490 struct ib_qp_attr *attr = NULL;
4491 struct ib_pd *pd;
4492 struct ib_cq *cq;
4493 struct ib_qp *qp;
Eli Cohene126ba92013-07-07 17:25:49 +03004494 int ret;
4495
4496 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
4497 init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL);
4498 if (!attr || !init_attr) {
4499 ret = -ENOMEM;
4500 goto error_0;
4501 }
4502
Christoph Hellwiged082d32016-09-05 12:56:17 +02004503 pd = ib_alloc_pd(&dev->ib_dev, 0);
Eli Cohene126ba92013-07-07 17:25:49 +03004504 if (IS_ERR(pd)) {
4505 mlx5_ib_dbg(dev, "Couldn't create PD for sync UMR QP\n");
4506 ret = PTR_ERR(pd);
4507 goto error_0;
4508 }
4509
Christoph Hellwigadd08d72016-03-03 09:38:22 +01004510 cq = ib_alloc_cq(&dev->ib_dev, NULL, 128, 0, IB_POLL_SOFTIRQ);
Eli Cohene126ba92013-07-07 17:25:49 +03004511 if (IS_ERR(cq)) {
4512 mlx5_ib_dbg(dev, "Couldn't create CQ for sync UMR QP\n");
4513 ret = PTR_ERR(cq);
4514 goto error_2;
4515 }
Eli Cohene126ba92013-07-07 17:25:49 +03004516
4517 init_attr->send_cq = cq;
4518 init_attr->recv_cq = cq;
4519 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
4520 init_attr->cap.max_send_wr = MAX_UMR_WR;
4521 init_attr->cap.max_send_sge = 1;
4522 init_attr->qp_type = MLX5_IB_QPT_REG_UMR;
4523 init_attr->port_num = 1;
4524 qp = mlx5_ib_create_qp(pd, init_attr, NULL);
4525 if (IS_ERR(qp)) {
4526 mlx5_ib_dbg(dev, "Couldn't create sync UMR QP\n");
4527 ret = PTR_ERR(qp);
4528 goto error_3;
4529 }
4530 qp->device = &dev->ib_dev;
4531 qp->real_qp = qp;
4532 qp->uobject = NULL;
4533 qp->qp_type = MLX5_IB_QPT_REG_UMR;
Majd Dibbiny31fde032017-10-30 14:23:13 +02004534 qp->send_cq = init_attr->send_cq;
4535 qp->recv_cq = init_attr->recv_cq;
Eli Cohene126ba92013-07-07 17:25:49 +03004536
4537 attr->qp_state = IB_QPS_INIT;
4538 attr->port_num = 1;
4539 ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_PKEY_INDEX |
4540 IB_QP_PORT, NULL);
4541 if (ret) {
4542 mlx5_ib_dbg(dev, "Couldn't modify UMR QP\n");
4543 goto error_4;
4544 }
4545
4546 memset(attr, 0, sizeof(*attr));
4547 attr->qp_state = IB_QPS_RTR;
4548 attr->path_mtu = IB_MTU_256;
4549
4550 ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
4551 if (ret) {
4552 mlx5_ib_dbg(dev, "Couldn't modify umr QP to rtr\n");
4553 goto error_4;
4554 }
4555
4556 memset(attr, 0, sizeof(*attr));
4557 attr->qp_state = IB_QPS_RTS;
4558 ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
4559 if (ret) {
4560 mlx5_ib_dbg(dev, "Couldn't modify umr QP to rts\n");
4561 goto error_4;
4562 }
4563
4564 dev->umrc.qp = qp;
4565 dev->umrc.cq = cq;
Eli Cohene126ba92013-07-07 17:25:49 +03004566 dev->umrc.pd = pd;
4567
4568 sema_init(&dev->umrc.sem, MAX_UMR_WR);
4569 ret = mlx5_mr_cache_init(dev);
4570 if (ret) {
4571 mlx5_ib_warn(dev, "mr cache init failed %d\n", ret);
4572 goto error_4;
4573 }
4574
4575 kfree(attr);
4576 kfree(init_attr);
4577
4578 return 0;
4579
4580error_4:
4581 mlx5_ib_destroy_qp(qp);
Mark Bloch32927e22018-03-20 15:45:37 +02004582 dev->umrc.qp = NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03004583
4584error_3:
Christoph Hellwigadd08d72016-03-03 09:38:22 +01004585 ib_free_cq(cq);
Mark Bloch32927e22018-03-20 15:45:37 +02004586 dev->umrc.cq = NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03004587
4588error_2:
Eli Cohene126ba92013-07-07 17:25:49 +03004589 ib_dealloc_pd(pd);
Mark Bloch32927e22018-03-20 15:45:37 +02004590 dev->umrc.pd = NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03004591
4592error_0:
4593 kfree(attr);
4594 kfree(init_attr);
4595 return ret;
4596}
4597
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03004598static u8 mlx5_get_umr_fence(u8 umr_fence_cap)
4599{
4600 switch (umr_fence_cap) {
4601 case MLX5_CAP_UMR_FENCE_NONE:
4602 return MLX5_FENCE_MODE_NONE;
4603 case MLX5_CAP_UMR_FENCE_SMALL:
4604 return MLX5_FENCE_MODE_INITIATOR_SMALL;
4605 default:
4606 return MLX5_FENCE_MODE_STRONG_ORDERING;
4607 }
4608}
4609
Eli Cohene126ba92013-07-07 17:25:49 +03004610static int create_dev_resources(struct mlx5_ib_resources *devr)
4611{
4612 struct ib_srq_init_attr attr;
4613 struct mlx5_ib_dev *dev;
Matan Barakbcf4c1e2015-06-11 16:35:20 +03004614 struct ib_cq_init_attr cq_attr = {.cqe = 1};
Haggai Eran7722f472016-02-29 15:45:07 +02004615 int port;
Eli Cohene126ba92013-07-07 17:25:49 +03004616 int ret = 0;
4617
4618 dev = container_of(devr, struct mlx5_ib_dev, devr);
4619
Haggai Erand16e91d2016-02-29 15:45:05 +02004620 mutex_init(&devr->mutex);
4621
Eli Cohene126ba92013-07-07 17:25:49 +03004622 devr->p0 = mlx5_ib_alloc_pd(&dev->ib_dev, NULL, NULL);
4623 if (IS_ERR(devr->p0)) {
4624 ret = PTR_ERR(devr->p0);
4625 goto error0;
4626 }
4627 devr->p0->device = &dev->ib_dev;
4628 devr->p0->uobject = NULL;
4629 atomic_set(&devr->p0->usecnt, 0);
4630
Matan Barakbcf4c1e2015-06-11 16:35:20 +03004631 devr->c0 = mlx5_ib_create_cq(&dev->ib_dev, &cq_attr, NULL, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03004632 if (IS_ERR(devr->c0)) {
4633 ret = PTR_ERR(devr->c0);
4634 goto error1;
4635 }
4636 devr->c0->device = &dev->ib_dev;
4637 devr->c0->uobject = NULL;
4638 devr->c0->comp_handler = NULL;
4639 devr->c0->event_handler = NULL;
4640 devr->c0->cq_context = NULL;
4641 atomic_set(&devr->c0->usecnt, 0);
4642
4643 devr->x0 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
4644 if (IS_ERR(devr->x0)) {
4645 ret = PTR_ERR(devr->x0);
4646 goto error2;
4647 }
4648 devr->x0->device = &dev->ib_dev;
4649 devr->x0->inode = NULL;
4650 atomic_set(&devr->x0->usecnt, 0);
4651 mutex_init(&devr->x0->tgt_qp_mutex);
4652 INIT_LIST_HEAD(&devr->x0->tgt_qp_list);
4653
4654 devr->x1 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
4655 if (IS_ERR(devr->x1)) {
4656 ret = PTR_ERR(devr->x1);
4657 goto error3;
4658 }
4659 devr->x1->device = &dev->ib_dev;
4660 devr->x1->inode = NULL;
4661 atomic_set(&devr->x1->usecnt, 0);
4662 mutex_init(&devr->x1->tgt_qp_mutex);
4663 INIT_LIST_HEAD(&devr->x1->tgt_qp_list);
4664
4665 memset(&attr, 0, sizeof(attr));
4666 attr.attr.max_sge = 1;
4667 attr.attr.max_wr = 1;
4668 attr.srq_type = IB_SRQT_XRC;
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03004669 attr.ext.cq = devr->c0;
Eli Cohene126ba92013-07-07 17:25:49 +03004670 attr.ext.xrc.xrcd = devr->x0;
4671
4672 devr->s0 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
4673 if (IS_ERR(devr->s0)) {
4674 ret = PTR_ERR(devr->s0);
4675 goto error4;
4676 }
4677 devr->s0->device = &dev->ib_dev;
4678 devr->s0->pd = devr->p0;
4679 devr->s0->uobject = NULL;
4680 devr->s0->event_handler = NULL;
4681 devr->s0->srq_context = NULL;
4682 devr->s0->srq_type = IB_SRQT_XRC;
4683 devr->s0->ext.xrc.xrcd = devr->x0;
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03004684 devr->s0->ext.cq = devr->c0;
Eli Cohene126ba92013-07-07 17:25:49 +03004685 atomic_inc(&devr->s0->ext.xrc.xrcd->usecnt);
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03004686 atomic_inc(&devr->s0->ext.cq->usecnt);
Eli Cohene126ba92013-07-07 17:25:49 +03004687 atomic_inc(&devr->p0->usecnt);
4688 atomic_set(&devr->s0->usecnt, 0);
4689
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03004690 memset(&attr, 0, sizeof(attr));
4691 attr.attr.max_sge = 1;
4692 attr.attr.max_wr = 1;
4693 attr.srq_type = IB_SRQT_BASIC;
4694 devr->s1 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
4695 if (IS_ERR(devr->s1)) {
4696 ret = PTR_ERR(devr->s1);
4697 goto error5;
4698 }
4699 devr->s1->device = &dev->ib_dev;
4700 devr->s1->pd = devr->p0;
4701 devr->s1->uobject = NULL;
4702 devr->s1->event_handler = NULL;
4703 devr->s1->srq_context = NULL;
4704 devr->s1->srq_type = IB_SRQT_BASIC;
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03004705 devr->s1->ext.cq = devr->c0;
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03004706 atomic_inc(&devr->p0->usecnt);
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03004707 atomic_set(&devr->s1->usecnt, 0);
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03004708
Haggai Eran7722f472016-02-29 15:45:07 +02004709 for (port = 0; port < ARRAY_SIZE(devr->ports); ++port) {
4710 INIT_WORK(&devr->ports[port].pkey_change_work,
4711 pkey_change_handler);
4712 devr->ports[port].devr = devr;
4713 }
4714
Eli Cohene126ba92013-07-07 17:25:49 +03004715 return 0;
4716
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03004717error5:
4718 mlx5_ib_destroy_srq(devr->s0);
Eli Cohene126ba92013-07-07 17:25:49 +03004719error4:
4720 mlx5_ib_dealloc_xrcd(devr->x1);
4721error3:
4722 mlx5_ib_dealloc_xrcd(devr->x0);
4723error2:
4724 mlx5_ib_destroy_cq(devr->c0);
4725error1:
4726 mlx5_ib_dealloc_pd(devr->p0);
4727error0:
4728 return ret;
4729}
4730
4731static void destroy_dev_resources(struct mlx5_ib_resources *devr)
4732{
Haggai Eran7722f472016-02-29 15:45:07 +02004733 struct mlx5_ib_dev *dev =
4734 container_of(devr, struct mlx5_ib_dev, devr);
4735 int port;
4736
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03004737 mlx5_ib_destroy_srq(devr->s1);
Eli Cohene126ba92013-07-07 17:25:49 +03004738 mlx5_ib_destroy_srq(devr->s0);
4739 mlx5_ib_dealloc_xrcd(devr->x0);
4740 mlx5_ib_dealloc_xrcd(devr->x1);
4741 mlx5_ib_destroy_cq(devr->c0);
4742 mlx5_ib_dealloc_pd(devr->p0);
Haggai Eran7722f472016-02-29 15:45:07 +02004743
4744 /* Make sure no change P_Key work items are still executing */
4745 for (port = 0; port < dev->num_ports; ++port)
4746 cancel_work_sync(&devr->ports[port].pkey_change_work);
Eli Cohene126ba92013-07-07 17:25:49 +03004747}
4748
Artemy Kovalyovb02289b2018-07-04 15:57:50 +03004749static u32 get_core_cap_flags(struct ib_device *ibdev,
4750 struct mlx5_hca_vport_context *rep)
Achiad Shochate53505a2015-12-23 18:47:25 +02004751{
4752 struct mlx5_ib_dev *dev = to_mdev(ibdev);
4753 enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, 1);
4754 u8 l3_type_cap = MLX5_CAP_ROCE(dev->mdev, l3_type);
4755 u8 roce_version_cap = MLX5_CAP_ROCE(dev->mdev, roce_version);
Daniel Jurgens85c7c012018-01-04 17:25:43 +02004756 bool raw_support = !mlx5_core_mp_enabled(dev->mdev);
Achiad Shochate53505a2015-12-23 18:47:25 +02004757 u32 ret = 0;
4758
Artemy Kovalyovb02289b2018-07-04 15:57:50 +03004759 if (rep->grh_required)
4760 ret |= RDMA_CORE_CAP_IB_GRH_REQUIRED;
4761
Achiad Shochate53505a2015-12-23 18:47:25 +02004762 if (ll == IB_LINK_LAYER_INFINIBAND)
Artemy Kovalyovb02289b2018-07-04 15:57:50 +03004763 return ret | RDMA_CORE_PORT_IBA_IB;
Achiad Shochate53505a2015-12-23 18:47:25 +02004764
Daniel Jurgens85c7c012018-01-04 17:25:43 +02004765 if (raw_support)
Artemy Kovalyovb02289b2018-07-04 15:57:50 +03004766 ret |= RDMA_CORE_PORT_RAW_PACKET;
Or Gerlitz72cd5712017-01-24 13:02:36 +02004767
Achiad Shochate53505a2015-12-23 18:47:25 +02004768 if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV4_CAP))
Or Gerlitz72cd5712017-01-24 13:02:36 +02004769 return ret;
Achiad Shochate53505a2015-12-23 18:47:25 +02004770
4771 if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV6_CAP))
Or Gerlitz72cd5712017-01-24 13:02:36 +02004772 return ret;
Achiad Shochate53505a2015-12-23 18:47:25 +02004773
4774 if (roce_version_cap & MLX5_ROCE_VERSION_1_CAP)
4775 ret |= RDMA_CORE_PORT_IBA_ROCE;
4776
4777 if (roce_version_cap & MLX5_ROCE_VERSION_2_CAP)
4778 ret |= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
4779
4780 return ret;
4781}
4782
Ira Weiny77386132015-05-13 20:02:58 -04004783static int mlx5_port_immutable(struct ib_device *ibdev, u8 port_num,
4784 struct ib_port_immutable *immutable)
4785{
4786 struct ib_port_attr attr;
Or Gerlitzca5b91d2016-11-27 16:51:36 +02004787 struct mlx5_ib_dev *dev = to_mdev(ibdev);
4788 enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, port_num);
Artemy Kovalyovb02289b2018-07-04 15:57:50 +03004789 struct mlx5_hca_vport_context rep = {0};
Ira Weiny77386132015-05-13 20:02:58 -04004790 int err;
4791
Or Gerlitzc4550c62017-01-24 13:02:39 +02004792 err = ib_query_port(ibdev, port_num, &attr);
Ira Weiny77386132015-05-13 20:02:58 -04004793 if (err)
4794 return err;
4795
Artemy Kovalyovb02289b2018-07-04 15:57:50 +03004796 if (ll == IB_LINK_LAYER_INFINIBAND) {
4797 err = mlx5_query_hca_vport_context(dev->mdev, 0, port_num, 0,
4798 &rep);
4799 if (err)
4800 return err;
4801 }
4802
Ira Weiny77386132015-05-13 20:02:58 -04004803 immutable->pkey_tbl_len = attr.pkey_tbl_len;
4804 immutable->gid_tbl_len = attr.gid_tbl_len;
Artemy Kovalyovb02289b2018-07-04 15:57:50 +03004805 immutable->core_cap_flags = get_core_cap_flags(ibdev, &rep);
Or Gerlitzca5b91d2016-11-27 16:51:36 +02004806 if ((ll == IB_LINK_LAYER_INFINIBAND) || MLX5_CAP_GEN(dev->mdev, roce))
4807 immutable->max_mad_size = IB_MGMT_MAD_SIZE;
Ira Weiny77386132015-05-13 20:02:58 -04004808
4809 return 0;
4810}
4811
Mark Bloch8e6efa32017-11-06 12:22:13 +00004812static int mlx5_port_rep_immutable(struct ib_device *ibdev, u8 port_num,
4813 struct ib_port_immutable *immutable)
4814{
4815 struct ib_port_attr attr;
4816 int err;
4817
4818 immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET;
4819
4820 err = ib_query_port(ibdev, port_num, &attr);
4821 if (err)
4822 return err;
4823
4824 immutable->pkey_tbl_len = attr.pkey_tbl_len;
4825 immutable->gid_tbl_len = attr.gid_tbl_len;
4826 immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET;
4827
4828 return 0;
4829}
4830
Leon Romanovsky9abb0d12017-06-27 16:49:53 +03004831static void get_dev_fw_str(struct ib_device *ibdev, char *str)
Ira Weinyc7342822016-06-15 02:22:01 -04004832{
4833 struct mlx5_ib_dev *dev =
4834 container_of(ibdev, struct mlx5_ib_dev, ib_dev);
Leon Romanovsky9abb0d12017-06-27 16:49:53 +03004835 snprintf(str, IB_FW_VERSION_NAME_MAX, "%d.%d.%04d",
4836 fw_rev_maj(dev->mdev), fw_rev_min(dev->mdev),
4837 fw_rev_sub(dev->mdev));
Ira Weinyc7342822016-06-15 02:22:01 -04004838}
4839
Or Gerlitz45f95ac2016-11-27 16:51:35 +02004840static int mlx5_eth_lag_init(struct mlx5_ib_dev *dev)
Aviv Heller9ef9c642016-09-18 20:48:01 +03004841{
4842 struct mlx5_core_dev *mdev = dev->mdev;
4843 struct mlx5_flow_namespace *ns = mlx5_get_flow_namespace(mdev,
4844 MLX5_FLOW_NAMESPACE_LAG);
4845 struct mlx5_flow_table *ft;
4846 int err;
4847
4848 if (!ns || !mlx5_lag_is_active(mdev))
4849 return 0;
4850
4851 err = mlx5_cmd_create_vport_lag(mdev);
4852 if (err)
4853 return err;
4854
4855 ft = mlx5_create_lag_demux_flow_table(ns, 0, 0);
4856 if (IS_ERR(ft)) {
4857 err = PTR_ERR(ft);
4858 goto err_destroy_vport_lag;
4859 }
4860
Mark Bloch9a4ca382018-01-16 14:42:35 +00004861 dev->flow_db->lag_demux_ft = ft;
Aviv Heller9ef9c642016-09-18 20:48:01 +03004862 return 0;
4863
4864err_destroy_vport_lag:
4865 mlx5_cmd_destroy_vport_lag(mdev);
4866 return err;
4867}
4868
Or Gerlitz45f95ac2016-11-27 16:51:35 +02004869static void mlx5_eth_lag_cleanup(struct mlx5_ib_dev *dev)
Aviv Heller9ef9c642016-09-18 20:48:01 +03004870{
4871 struct mlx5_core_dev *mdev = dev->mdev;
4872
Mark Bloch9a4ca382018-01-16 14:42:35 +00004873 if (dev->flow_db->lag_demux_ft) {
4874 mlx5_destroy_flow_table(dev->flow_db->lag_demux_ft);
4875 dev->flow_db->lag_demux_ft = NULL;
Aviv Heller9ef9c642016-09-18 20:48:01 +03004876
4877 mlx5_cmd_destroy_vport_lag(mdev);
4878 }
4879}
4880
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +02004881static int mlx5_add_netdev_notifier(struct mlx5_ib_dev *dev, u8 port_num)
Achiad Shochatfc24fc52015-12-23 18:47:17 +02004882{
Achiad Shochate53505a2015-12-23 18:47:25 +02004883 int err;
4884
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +02004885 dev->roce[port_num].nb.notifier_call = mlx5_netdev_event;
4886 err = register_netdevice_notifier(&dev->roce[port_num].nb);
Aviv Heller5ec8c832016-09-18 20:48:00 +03004887 if (err) {
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +02004888 dev->roce[port_num].nb.notifier_call = NULL;
Achiad Shochate53505a2015-12-23 18:47:25 +02004889 return err;
Aviv Heller5ec8c832016-09-18 20:48:00 +03004890 }
Achiad Shochate53505a2015-12-23 18:47:25 +02004891
Or Gerlitzd012f5d2016-11-27 16:51:34 +02004892 return 0;
4893}
Achiad Shochate53505a2015-12-23 18:47:25 +02004894
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +02004895static void mlx5_remove_netdev_notifier(struct mlx5_ib_dev *dev, u8 port_num)
Eli Cohene126ba92013-07-07 17:25:49 +03004896{
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +02004897 if (dev->roce[port_num].nb.notifier_call) {
4898 unregister_netdevice_notifier(&dev->roce[port_num].nb);
4899 dev->roce[port_num].nb.notifier_call = NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03004900 }
4901}
4902
Leon Romanovskye3f1ed12018-07-08 12:55:43 +03004903static int mlx5_enable_eth(struct mlx5_ib_dev *dev)
Eli Cohene126ba92013-07-07 17:25:49 +03004904{
Eli Cohene126ba92013-07-07 17:25:49 +03004905 int err;
4906
Or Gerlitzca5b91d2016-11-27 16:51:36 +02004907 if (MLX5_CAP_GEN(dev->mdev, roce)) {
4908 err = mlx5_nic_vport_enable_roce(dev->mdev);
4909 if (err)
Mark Bloch8e6efa32017-11-06 12:22:13 +00004910 return err;
Or Gerlitzca5b91d2016-11-27 16:51:36 +02004911 }
Achiad Shochate53505a2015-12-23 18:47:25 +02004912
Or Gerlitz45f95ac2016-11-27 16:51:35 +02004913 err = mlx5_eth_lag_init(dev);
Aviv Heller9ef9c642016-09-18 20:48:01 +03004914 if (err)
4915 goto err_disable_roce;
4916
Achiad Shochate53505a2015-12-23 18:47:25 +02004917 return 0;
4918
Aviv Heller9ef9c642016-09-18 20:48:01 +03004919err_disable_roce:
Or Gerlitzca5b91d2016-11-27 16:51:36 +02004920 if (MLX5_CAP_GEN(dev->mdev, roce))
4921 mlx5_nic_vport_disable_roce(dev->mdev);
Aviv Heller9ef9c642016-09-18 20:48:01 +03004922
Achiad Shochate53505a2015-12-23 18:47:25 +02004923 return err;
Achiad Shochatfc24fc52015-12-23 18:47:17 +02004924}
4925
Or Gerlitz45f95ac2016-11-27 16:51:35 +02004926static void mlx5_disable_eth(struct mlx5_ib_dev *dev)
Achiad Shochatfc24fc52015-12-23 18:47:17 +02004927{
Or Gerlitz45f95ac2016-11-27 16:51:35 +02004928 mlx5_eth_lag_cleanup(dev);
Or Gerlitzca5b91d2016-11-27 16:51:36 +02004929 if (MLX5_CAP_GEN(dev->mdev, roce))
4930 mlx5_nic_vport_disable_roce(dev->mdev);
Achiad Shochatfc24fc52015-12-23 18:47:17 +02004931}
4932
Parav Pandite1f24a72017-04-16 07:29:29 +03004933struct mlx5_ib_counter {
Kamal Heib7c16f472017-01-18 15:25:09 +02004934 const char *name;
4935 size_t offset;
4936};
4937
4938#define INIT_Q_COUNTER(_name) \
4939 { .name = #_name, .offset = MLX5_BYTE_OFF(query_q_counter_out, _name)}
4940
Parav Pandite1f24a72017-04-16 07:29:29 +03004941static const struct mlx5_ib_counter basic_q_cnts[] = {
Kamal Heib7c16f472017-01-18 15:25:09 +02004942 INIT_Q_COUNTER(rx_write_requests),
4943 INIT_Q_COUNTER(rx_read_requests),
4944 INIT_Q_COUNTER(rx_atomic_requests),
4945 INIT_Q_COUNTER(out_of_buffer),
4946};
4947
Parav Pandite1f24a72017-04-16 07:29:29 +03004948static const struct mlx5_ib_counter out_of_seq_q_cnts[] = {
Kamal Heib7c16f472017-01-18 15:25:09 +02004949 INIT_Q_COUNTER(out_of_sequence),
4950};
4951
Parav Pandite1f24a72017-04-16 07:29:29 +03004952static const struct mlx5_ib_counter retrans_q_cnts[] = {
Kamal Heib7c16f472017-01-18 15:25:09 +02004953 INIT_Q_COUNTER(duplicate_request),
4954 INIT_Q_COUNTER(rnr_nak_retry_err),
4955 INIT_Q_COUNTER(packet_seq_err),
4956 INIT_Q_COUNTER(implied_nak_seq_err),
4957 INIT_Q_COUNTER(local_ack_timeout_err),
4958};
4959
Parav Pandite1f24a72017-04-16 07:29:29 +03004960#define INIT_CONG_COUNTER(_name) \
4961 { .name = #_name, .offset = \
4962 MLX5_BYTE_OFF(query_cong_statistics_out, _name ## _high)}
4963
4964static const struct mlx5_ib_counter cong_cnts[] = {
4965 INIT_CONG_COUNTER(rp_cnp_ignored),
4966 INIT_CONG_COUNTER(rp_cnp_handled),
4967 INIT_CONG_COUNTER(np_ecn_marked_roce_packets),
4968 INIT_CONG_COUNTER(np_cnp_sent),
4969};
4970
Parav Pandit58dcb602017-06-19 07:19:37 +03004971static const struct mlx5_ib_counter extended_err_cnts[] = {
4972 INIT_Q_COUNTER(resp_local_length_error),
4973 INIT_Q_COUNTER(resp_cqe_error),
4974 INIT_Q_COUNTER(req_cqe_error),
4975 INIT_Q_COUNTER(req_remote_invalid_request),
4976 INIT_Q_COUNTER(req_remote_access_errors),
4977 INIT_Q_COUNTER(resp_remote_access_errors),
4978 INIT_Q_COUNTER(resp_cqe_flush_error),
4979 INIT_Q_COUNTER(req_cqe_flush_error),
4980};
4981
Talat Batheesh9f876f32018-06-21 15:37:56 +03004982#define INIT_EXT_PPCNT_COUNTER(_name) \
4983 { .name = #_name, .offset = \
4984 MLX5_BYTE_OFF(ppcnt_reg, \
4985 counter_set.eth_extended_cntrs_grp_data_layout._name##_high)}
4986
4987static const struct mlx5_ib_counter ext_ppcnt_cnts[] = {
4988 INIT_EXT_PPCNT_COUNTER(rx_icrc_encapsulated),
4989};
4990
Parav Pandite1f24a72017-04-16 07:29:29 +03004991static void mlx5_ib_dealloc_counters(struct mlx5_ib_dev *dev)
Mark Bloch0837e862016-06-17 15:10:55 +03004992{
Daniel Jurgensaac44922018-01-04 17:25:40 +02004993 int i;
Mark Bloch0837e862016-06-17 15:10:55 +03004994
Kamal Heib7c16f472017-01-18 15:25:09 +02004995 for (i = 0; i < dev->num_ports; i++) {
Parav Pandit921c0f52018-07-08 13:40:30 +03004996 if (dev->port[i].cnts.set_id_valid)
Daniel Jurgensaac44922018-01-04 17:25:40 +02004997 mlx5_core_dealloc_q_counter(dev->mdev,
4998 dev->port[i].cnts.set_id);
Parav Pandite1f24a72017-04-16 07:29:29 +03004999 kfree(dev->port[i].cnts.names);
5000 kfree(dev->port[i].cnts.offsets);
Kamal Heib7c16f472017-01-18 15:25:09 +02005001 }
5002}
5003
Parav Pandite1f24a72017-04-16 07:29:29 +03005004static int __mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev,
5005 struct mlx5_ib_counters *cnts)
Kamal Heib7c16f472017-01-18 15:25:09 +02005006{
5007 u32 num_counters;
5008
5009 num_counters = ARRAY_SIZE(basic_q_cnts);
5010
5011 if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt))
5012 num_counters += ARRAY_SIZE(out_of_seq_q_cnts);
5013
5014 if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters))
5015 num_counters += ARRAY_SIZE(retrans_q_cnts);
Parav Pandit58dcb602017-06-19 07:19:37 +03005016
5017 if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters))
5018 num_counters += ARRAY_SIZE(extended_err_cnts);
5019
Parav Pandite1f24a72017-04-16 07:29:29 +03005020 cnts->num_q_counters = num_counters;
Kamal Heib7c16f472017-01-18 15:25:09 +02005021
Parav Pandite1f24a72017-04-16 07:29:29 +03005022 if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
5023 cnts->num_cong_counters = ARRAY_SIZE(cong_cnts);
5024 num_counters += ARRAY_SIZE(cong_cnts);
5025 }
Talat Batheesh9f876f32018-06-21 15:37:56 +03005026 if (MLX5_CAP_PCAM_FEATURE(dev->mdev, rx_icrc_encapsulated_counter)) {
5027 cnts->num_ext_ppcnt_counters = ARRAY_SIZE(ext_ppcnt_cnts);
5028 num_counters += ARRAY_SIZE(ext_ppcnt_cnts);
5029 }
Parav Pandite1f24a72017-04-16 07:29:29 +03005030 cnts->names = kcalloc(num_counters, sizeof(cnts->names), GFP_KERNEL);
5031 if (!cnts->names)
Kamal Heib7c16f472017-01-18 15:25:09 +02005032 return -ENOMEM;
5033
Parav Pandite1f24a72017-04-16 07:29:29 +03005034 cnts->offsets = kcalloc(num_counters,
5035 sizeof(cnts->offsets), GFP_KERNEL);
5036 if (!cnts->offsets)
Kamal Heib7c16f472017-01-18 15:25:09 +02005037 goto err_names;
5038
Kamal Heib7c16f472017-01-18 15:25:09 +02005039 return 0;
5040
5041err_names:
Parav Pandite1f24a72017-04-16 07:29:29 +03005042 kfree(cnts->names);
Daniel Jurgensaac44922018-01-04 17:25:40 +02005043 cnts->names = NULL;
Kamal Heib7c16f472017-01-18 15:25:09 +02005044 return -ENOMEM;
5045}
5046
Parav Pandite1f24a72017-04-16 07:29:29 +03005047static void mlx5_ib_fill_counters(struct mlx5_ib_dev *dev,
5048 const char **names,
5049 size_t *offsets)
Kamal Heib7c16f472017-01-18 15:25:09 +02005050{
5051 int i;
5052 int j = 0;
5053
5054 for (i = 0; i < ARRAY_SIZE(basic_q_cnts); i++, j++) {
5055 names[j] = basic_q_cnts[i].name;
5056 offsets[j] = basic_q_cnts[i].offset;
5057 }
5058
5059 if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt)) {
5060 for (i = 0; i < ARRAY_SIZE(out_of_seq_q_cnts); i++, j++) {
5061 names[j] = out_of_seq_q_cnts[i].name;
5062 offsets[j] = out_of_seq_q_cnts[i].offset;
5063 }
5064 }
5065
5066 if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters)) {
5067 for (i = 0; i < ARRAY_SIZE(retrans_q_cnts); i++, j++) {
5068 names[j] = retrans_q_cnts[i].name;
5069 offsets[j] = retrans_q_cnts[i].offset;
5070 }
5071 }
Parav Pandite1f24a72017-04-16 07:29:29 +03005072
Parav Pandit58dcb602017-06-19 07:19:37 +03005073 if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters)) {
5074 for (i = 0; i < ARRAY_SIZE(extended_err_cnts); i++, j++) {
5075 names[j] = extended_err_cnts[i].name;
5076 offsets[j] = extended_err_cnts[i].offset;
5077 }
5078 }
5079
Parav Pandite1f24a72017-04-16 07:29:29 +03005080 if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
5081 for (i = 0; i < ARRAY_SIZE(cong_cnts); i++, j++) {
5082 names[j] = cong_cnts[i].name;
5083 offsets[j] = cong_cnts[i].offset;
5084 }
5085 }
Talat Batheesh9f876f32018-06-21 15:37:56 +03005086
5087 if (MLX5_CAP_PCAM_FEATURE(dev->mdev, rx_icrc_encapsulated_counter)) {
5088 for (i = 0; i < ARRAY_SIZE(ext_ppcnt_cnts); i++, j++) {
5089 names[j] = ext_ppcnt_cnts[i].name;
5090 offsets[j] = ext_ppcnt_cnts[i].offset;
5091 }
5092 }
Mark Bloch0837e862016-06-17 15:10:55 +03005093}
5094
Parav Pandite1f24a72017-04-16 07:29:29 +03005095static int mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev)
Mark Bloch0837e862016-06-17 15:10:55 +03005096{
Daniel Jurgensaac44922018-01-04 17:25:40 +02005097 int err = 0;
Mark Bloch0837e862016-06-17 15:10:55 +03005098 int i;
Mark Bloch0837e862016-06-17 15:10:55 +03005099
5100 for (i = 0; i < dev->num_ports; i++) {
Daniel Jurgensaac44922018-01-04 17:25:40 +02005101 err = __mlx5_ib_alloc_counters(dev, &dev->port[i].cnts);
5102 if (err)
5103 goto err_alloc;
Kamal Heib7c16f472017-01-18 15:25:09 +02005104
Daniel Jurgensaac44922018-01-04 17:25:40 +02005105 mlx5_ib_fill_counters(dev, dev->port[i].cnts.names,
5106 dev->port[i].cnts.offsets);
5107
5108 err = mlx5_core_alloc_q_counter(dev->mdev,
5109 &dev->port[i].cnts.set_id);
5110 if (err) {
Mark Bloch0837e862016-06-17 15:10:55 +03005111 mlx5_ib_warn(dev,
5112 "couldn't allocate queue counter for port %d, err %d\n",
Daniel Jurgensaac44922018-01-04 17:25:40 +02005113 i + 1, err);
5114 goto err_alloc;
Mark Bloch0837e862016-06-17 15:10:55 +03005115 }
Daniel Jurgensaac44922018-01-04 17:25:40 +02005116 dev->port[i].cnts.set_id_valid = true;
Mark Bloch0837e862016-06-17 15:10:55 +03005117 }
5118
5119 return 0;
5120
Daniel Jurgensaac44922018-01-04 17:25:40 +02005121err_alloc:
5122 mlx5_ib_dealloc_counters(dev);
5123 return err;
Mark Bloch0837e862016-06-17 15:10:55 +03005124}
5125
Mark Bloch0ad17a82016-06-17 15:10:56 +03005126static struct rdma_hw_stats *mlx5_ib_alloc_hw_stats(struct ib_device *ibdev,
5127 u8 port_num)
5128{
Kamal Heib7c16f472017-01-18 15:25:09 +02005129 struct mlx5_ib_dev *dev = to_mdev(ibdev);
5130 struct mlx5_ib_port *port = &dev->port[port_num - 1];
Mark Bloch0ad17a82016-06-17 15:10:56 +03005131
5132 /* We support only per port stats */
5133 if (port_num == 0)
5134 return NULL;
5135
Parav Pandite1f24a72017-04-16 07:29:29 +03005136 return rdma_alloc_hw_stats_struct(port->cnts.names,
5137 port->cnts.num_q_counters +
Talat Batheesh9f876f32018-06-21 15:37:56 +03005138 port->cnts.num_cong_counters +
5139 port->cnts.num_ext_ppcnt_counters,
Mark Bloch0ad17a82016-06-17 15:10:56 +03005140 RDMA_HW_STATS_DEFAULT_LIFESPAN);
5141}
5142
Daniel Jurgensaac44922018-01-04 17:25:40 +02005143static int mlx5_ib_query_q_counters(struct mlx5_core_dev *mdev,
Parav Pandite1f24a72017-04-16 07:29:29 +03005144 struct mlx5_ib_port *port,
5145 struct rdma_hw_stats *stats)
5146{
5147 int outlen = MLX5_ST_SZ_BYTES(query_q_counter_out);
5148 void *out;
5149 __be32 val;
5150 int ret, i;
5151
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03005152 out = kvzalloc(outlen, GFP_KERNEL);
Parav Pandite1f24a72017-04-16 07:29:29 +03005153 if (!out)
5154 return -ENOMEM;
5155
Daniel Jurgensaac44922018-01-04 17:25:40 +02005156 ret = mlx5_core_query_q_counter(mdev,
Parav Pandite1f24a72017-04-16 07:29:29 +03005157 port->cnts.set_id, 0,
5158 out, outlen);
5159 if (ret)
5160 goto free;
5161
5162 for (i = 0; i < port->cnts.num_q_counters; i++) {
5163 val = *(__be32 *)(out + port->cnts.offsets[i]);
5164 stats->value[i] = (u64)be32_to_cpu(val);
5165 }
5166
5167free:
5168 kvfree(out);
5169 return ret;
5170}
5171
Talat Batheesh9f876f32018-06-21 15:37:56 +03005172static int mlx5_ib_query_ext_ppcnt_counters(struct mlx5_ib_dev *dev,
5173 struct mlx5_ib_port *port,
5174 struct rdma_hw_stats *stats)
5175{
5176 int offset = port->cnts.num_q_counters + port->cnts.num_cong_counters;
5177 int sz = MLX5_ST_SZ_BYTES(ppcnt_reg);
5178 int ret, i;
5179 void *out;
5180
5181 out = kvzalloc(sz, GFP_KERNEL);
5182 if (!out)
5183 return -ENOMEM;
5184
5185 ret = mlx5_cmd_query_ext_ppcnt_counters(dev->mdev, out);
5186 if (ret)
5187 goto free;
5188
5189 for (i = 0; i < port->cnts.num_ext_ppcnt_counters; i++) {
5190 stats->value[i + offset] =
5191 be64_to_cpup((__be64 *)(out +
5192 port->cnts.offsets[i + offset]));
5193 }
5194
5195free:
5196 kvfree(out);
5197 return ret;
5198}
5199
Mark Bloch0ad17a82016-06-17 15:10:56 +03005200static int mlx5_ib_get_hw_stats(struct ib_device *ibdev,
5201 struct rdma_hw_stats *stats,
Kamal Heib7c16f472017-01-18 15:25:09 +02005202 u8 port_num, int index)
Mark Bloch0ad17a82016-06-17 15:10:56 +03005203{
5204 struct mlx5_ib_dev *dev = to_mdev(ibdev);
Kamal Heib7c16f472017-01-18 15:25:09 +02005205 struct mlx5_ib_port *port = &dev->port[port_num - 1];
Daniel Jurgensaac44922018-01-04 17:25:40 +02005206 struct mlx5_core_dev *mdev;
Parav Pandite1f24a72017-04-16 07:29:29 +03005207 int ret, num_counters;
Daniel Jurgensaac44922018-01-04 17:25:40 +02005208 u8 mdev_port_num;
Mark Bloch0ad17a82016-06-17 15:10:56 +03005209
Kamal Heib7c16f472017-01-18 15:25:09 +02005210 if (!stats)
Parav Pandite1f24a72017-04-16 07:29:29 +03005211 return -EINVAL;
Mark Bloch0ad17a82016-06-17 15:10:56 +03005212
Talat Batheesh9f876f32018-06-21 15:37:56 +03005213 num_counters = port->cnts.num_q_counters +
5214 port->cnts.num_cong_counters +
5215 port->cnts.num_ext_ppcnt_counters;
Daniel Jurgensaac44922018-01-04 17:25:40 +02005216
5217 /* q_counters are per IB device, query the master mdev */
5218 ret = mlx5_ib_query_q_counters(dev->mdev, port, stats);
Mark Bloch0ad17a82016-06-17 15:10:56 +03005219 if (ret)
Parav Pandite1f24a72017-04-16 07:29:29 +03005220 return ret;
Mark Bloch0ad17a82016-06-17 15:10:56 +03005221
Talat Batheesh9f876f32018-06-21 15:37:56 +03005222 if (MLX5_CAP_PCAM_FEATURE(dev->mdev, rx_icrc_encapsulated_counter)) {
5223 ret = mlx5_ib_query_ext_ppcnt_counters(dev, port, stats);
5224 if (ret)
5225 return ret;
5226 }
5227
Parav Pandite1f24a72017-04-16 07:29:29 +03005228 if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
Daniel Jurgensaac44922018-01-04 17:25:40 +02005229 mdev = mlx5_ib_get_native_port_mdev(dev, port_num,
5230 &mdev_port_num);
5231 if (!mdev) {
5232 /* If port is not affiliated yet, its in down state
5233 * which doesn't have any counters yet, so it would be
5234 * zero. So no need to read from the HCA.
5235 */
5236 goto done;
5237 }
Majd Dibbiny71a0ff62017-12-21 17:38:26 +02005238 ret = mlx5_lag_query_cong_counters(dev->mdev,
5239 stats->value +
5240 port->cnts.num_q_counters,
5241 port->cnts.num_cong_counters,
5242 port->cnts.offsets +
5243 port->cnts.num_q_counters);
Daniel Jurgensaac44922018-01-04 17:25:40 +02005244
5245 mlx5_ib_put_native_port_mdev(dev, port_num);
Parav Pandite1f24a72017-04-16 07:29:29 +03005246 if (ret)
5247 return ret;
Mark Bloch0ad17a82016-06-17 15:10:56 +03005248 }
Kamal Heib7c16f472017-01-18 15:25:09 +02005249
Daniel Jurgensaac44922018-01-04 17:25:40 +02005250done:
Parav Pandite1f24a72017-04-16 07:29:29 +03005251 return num_counters;
Mark Bloch0ad17a82016-06-17 15:10:56 +03005252}
5253
Erez Shitrit693dfd52017-04-27 17:01:34 +03005254static struct net_device*
5255mlx5_ib_alloc_rdma_netdev(struct ib_device *hca,
5256 u8 port_num,
5257 enum rdma_netdev_t type,
5258 const char *name,
5259 unsigned char name_assign_type,
5260 void (*setup)(struct net_device *))
5261{
Niranjana Vishwanathapura8e959602017-06-30 13:14:46 -07005262 struct net_device *netdev;
Niranjana Vishwanathapura8e959602017-06-30 13:14:46 -07005263
Erez Shitrit693dfd52017-04-27 17:01:34 +03005264 if (type != RDMA_NETDEV_IPOIB)
5265 return ERR_PTR(-EOPNOTSUPP);
5266
Niranjana Vishwanathapura8e959602017-06-30 13:14:46 -07005267 netdev = mlx5_rdma_netdev_alloc(to_mdev(hca)->mdev, hca,
5268 name, setup);
Niranjana Vishwanathapura8e959602017-06-30 13:14:46 -07005269 return netdev;
Erez Shitrit693dfd52017-04-27 17:01:34 +03005270}
5271
Maor Gottliebfe248c32017-05-30 10:29:14 +03005272static void delay_drop_debugfs_cleanup(struct mlx5_ib_dev *dev)
5273{
5274 if (!dev->delay_drop.dbg)
5275 return;
5276 debugfs_remove_recursive(dev->delay_drop.dbg->dir_debugfs);
5277 kfree(dev->delay_drop.dbg);
5278 dev->delay_drop.dbg = NULL;
5279}
5280
Maor Gottlieb03404e82017-05-30 10:29:13 +03005281static void cancel_delay_drop(struct mlx5_ib_dev *dev)
5282{
5283 if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
5284 return;
5285
5286 cancel_work_sync(&dev->delay_drop.delay_drop_work);
Maor Gottliebfe248c32017-05-30 10:29:14 +03005287 delay_drop_debugfs_cleanup(dev);
5288}
5289
5290static ssize_t delay_drop_timeout_read(struct file *filp, char __user *buf,
5291 size_t count, loff_t *pos)
5292{
5293 struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
5294 char lbuf[20];
5295 int len;
5296
5297 len = snprintf(lbuf, sizeof(lbuf), "%u\n", delay_drop->timeout);
5298 return simple_read_from_buffer(buf, count, pos, lbuf, len);
5299}
5300
5301static ssize_t delay_drop_timeout_write(struct file *filp, const char __user *buf,
5302 size_t count, loff_t *pos)
5303{
5304 struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
5305 u32 timeout;
5306 u32 var;
5307
5308 if (kstrtouint_from_user(buf, count, 0, &var))
5309 return -EFAULT;
5310
5311 timeout = min_t(u32, roundup(var, 100), MLX5_MAX_DELAY_DROP_TIMEOUT_MS *
5312 1000);
5313 if (timeout != var)
5314 mlx5_ib_dbg(delay_drop->dev, "Round delay drop timeout to %u usec\n",
5315 timeout);
5316
5317 delay_drop->timeout = timeout;
5318
5319 return count;
5320}
5321
5322static const struct file_operations fops_delay_drop_timeout = {
5323 .owner = THIS_MODULE,
5324 .open = simple_open,
5325 .write = delay_drop_timeout_write,
5326 .read = delay_drop_timeout_read,
5327};
5328
5329static int delay_drop_debugfs_init(struct mlx5_ib_dev *dev)
5330{
5331 struct mlx5_ib_dbg_delay_drop *dbg;
5332
5333 if (!mlx5_debugfs_root)
5334 return 0;
5335
5336 dbg = kzalloc(sizeof(*dbg), GFP_KERNEL);
5337 if (!dbg)
5338 return -ENOMEM;
5339
Sudip Mukherjeecbafad82017-09-18 12:28:48 +01005340 dev->delay_drop.dbg = dbg;
5341
Maor Gottliebfe248c32017-05-30 10:29:14 +03005342 dbg->dir_debugfs =
5343 debugfs_create_dir("delay_drop",
5344 dev->mdev->priv.dbg_root);
5345 if (!dbg->dir_debugfs)
Sudip Mukherjeecbafad82017-09-18 12:28:48 +01005346 goto out_debugfs;
Maor Gottliebfe248c32017-05-30 10:29:14 +03005347
5348 dbg->events_cnt_debugfs =
5349 debugfs_create_atomic_t("num_timeout_events", 0400,
5350 dbg->dir_debugfs,
5351 &dev->delay_drop.events_cnt);
5352 if (!dbg->events_cnt_debugfs)
5353 goto out_debugfs;
5354
5355 dbg->rqs_cnt_debugfs =
5356 debugfs_create_atomic_t("num_rqs", 0400,
5357 dbg->dir_debugfs,
5358 &dev->delay_drop.rqs_cnt);
5359 if (!dbg->rqs_cnt_debugfs)
5360 goto out_debugfs;
5361
5362 dbg->timeout_debugfs =
5363 debugfs_create_file("timeout", 0600,
5364 dbg->dir_debugfs,
5365 &dev->delay_drop,
5366 &fops_delay_drop_timeout);
5367 if (!dbg->timeout_debugfs)
5368 goto out_debugfs;
5369
5370 return 0;
5371
5372out_debugfs:
5373 delay_drop_debugfs_cleanup(dev);
5374 return -ENOMEM;
Maor Gottlieb03404e82017-05-30 10:29:13 +03005375}
5376
5377static void init_delay_drop(struct mlx5_ib_dev *dev)
5378{
5379 if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
5380 return;
5381
5382 mutex_init(&dev->delay_drop.lock);
5383 dev->delay_drop.dev = dev;
5384 dev->delay_drop.activate = false;
5385 dev->delay_drop.timeout = MLX5_MAX_DELAY_DROP_TIMEOUT_MS * 1000;
5386 INIT_WORK(&dev->delay_drop.delay_drop_work, delay_drop_handler);
Maor Gottliebfe248c32017-05-30 10:29:14 +03005387 atomic_set(&dev->delay_drop.rqs_cnt, 0);
5388 atomic_set(&dev->delay_drop.events_cnt, 0);
5389
5390 if (delay_drop_debugfs_init(dev))
5391 mlx5_ib_warn(dev, "Failed to init delay drop debugfs\n");
Maor Gottlieb03404e82017-05-30 10:29:13 +03005392}
5393
Leon Romanovsky84305d712017-08-17 15:50:53 +03005394static const struct cpumask *
5395mlx5_ib_get_vector_affinity(struct ib_device *ibdev, int comp_vector)
Sagi Grimberg40b24402017-07-13 11:09:42 +03005396{
5397 struct mlx5_ib_dev *dev = to_mdev(ibdev);
5398
Israel Rukshin6082d9c2018-04-12 09:49:11 +00005399 return mlx5_get_vector_affinity_hint(dev->mdev, comp_vector);
Sagi Grimberg40b24402017-07-13 11:09:42 +03005400}
5401
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005402/* The mlx5_ib_multiport_mutex should be held when calling this function */
5403static void mlx5_ib_unbind_slave_port(struct mlx5_ib_dev *ibdev,
5404 struct mlx5_ib_multiport_info *mpi)
5405{
5406 u8 port_num = mlx5_core_native_port_num(mpi->mdev) - 1;
5407 struct mlx5_ib_port *port = &ibdev->port[port_num];
5408 int comps;
5409 int err;
5410 int i;
5411
Parav Pandita9e546e2018-01-04 17:25:39 +02005412 mlx5_ib_cleanup_cong_debugfs(ibdev, port_num);
5413
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005414 spin_lock(&port->mp.mpi_lock);
5415 if (!mpi->ibdev) {
5416 spin_unlock(&port->mp.mpi_lock);
5417 return;
5418 }
5419 mpi->ibdev = NULL;
5420
5421 spin_unlock(&port->mp.mpi_lock);
5422 mlx5_remove_netdev_notifier(ibdev, port_num);
5423 spin_lock(&port->mp.mpi_lock);
5424
5425 comps = mpi->mdev_refcnt;
5426 if (comps) {
5427 mpi->unaffiliate = true;
5428 init_completion(&mpi->unref_comp);
5429 spin_unlock(&port->mp.mpi_lock);
5430
5431 for (i = 0; i < comps; i++)
5432 wait_for_completion(&mpi->unref_comp);
5433
5434 spin_lock(&port->mp.mpi_lock);
5435 mpi->unaffiliate = false;
5436 }
5437
5438 port->mp.mpi = NULL;
5439
5440 list_add_tail(&mpi->list, &mlx5_ib_unaffiliated_port_list);
5441
5442 spin_unlock(&port->mp.mpi_lock);
5443
5444 err = mlx5_nic_vport_unaffiliate_multiport(mpi->mdev);
5445
5446 mlx5_ib_dbg(ibdev, "unaffiliated port %d\n", port_num + 1);
5447 /* Log an error, still needed to cleanup the pointers and add
5448 * it back to the list.
5449 */
5450 if (err)
5451 mlx5_ib_err(ibdev, "Failed to unaffiliate port %u\n",
5452 port_num + 1);
5453
5454 ibdev->roce[port_num].last_port_state = IB_PORT_DOWN;
5455}
5456
5457/* The mlx5_ib_multiport_mutex should be held when calling this function */
5458static bool mlx5_ib_bind_slave_port(struct mlx5_ib_dev *ibdev,
5459 struct mlx5_ib_multiport_info *mpi)
5460{
5461 u8 port_num = mlx5_core_native_port_num(mpi->mdev) - 1;
5462 int err;
5463
5464 spin_lock(&ibdev->port[port_num].mp.mpi_lock);
5465 if (ibdev->port[port_num].mp.mpi) {
Qing Huang25771882018-07-23 14:15:08 -07005466 mlx5_ib_dbg(ibdev, "port %d already affiliated.\n",
5467 port_num + 1);
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005468 spin_unlock(&ibdev->port[port_num].mp.mpi_lock);
5469 return false;
5470 }
5471
5472 ibdev->port[port_num].mp.mpi = mpi;
5473 mpi->ibdev = ibdev;
5474 spin_unlock(&ibdev->port[port_num].mp.mpi_lock);
5475
5476 err = mlx5_nic_vport_affiliate_multiport(ibdev->mdev, mpi->mdev);
5477 if (err)
5478 goto unbind;
5479
5480 err = get_port_caps(ibdev, mlx5_core_native_port_num(mpi->mdev));
5481 if (err)
5482 goto unbind;
5483
5484 err = mlx5_add_netdev_notifier(ibdev, port_num);
5485 if (err) {
5486 mlx5_ib_err(ibdev, "failed adding netdev notifier for port %u\n",
5487 port_num + 1);
5488 goto unbind;
5489 }
5490
Parav Pandita9e546e2018-01-04 17:25:39 +02005491 err = mlx5_ib_init_cong_debugfs(ibdev, port_num);
5492 if (err)
5493 goto unbind;
5494
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005495 return true;
5496
5497unbind:
5498 mlx5_ib_unbind_slave_port(ibdev, mpi);
5499 return false;
5500}
5501
5502static int mlx5_ib_init_multiport_master(struct mlx5_ib_dev *dev)
5503{
5504 int port_num = mlx5_core_native_port_num(dev->mdev) - 1;
5505 enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev,
5506 port_num + 1);
5507 struct mlx5_ib_multiport_info *mpi;
5508 int err;
5509 int i;
5510
5511 if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
5512 return 0;
5513
5514 err = mlx5_query_nic_vport_system_image_guid(dev->mdev,
5515 &dev->sys_image_guid);
5516 if (err)
5517 return err;
5518
5519 err = mlx5_nic_vport_enable_roce(dev->mdev);
5520 if (err)
5521 return err;
5522
5523 mutex_lock(&mlx5_ib_multiport_mutex);
5524 for (i = 0; i < dev->num_ports; i++) {
5525 bool bound = false;
5526
5527 /* build a stub multiport info struct for the native port. */
5528 if (i == port_num) {
5529 mpi = kzalloc(sizeof(*mpi), GFP_KERNEL);
5530 if (!mpi) {
5531 mutex_unlock(&mlx5_ib_multiport_mutex);
5532 mlx5_nic_vport_disable_roce(dev->mdev);
5533 return -ENOMEM;
5534 }
5535
5536 mpi->is_master = true;
5537 mpi->mdev = dev->mdev;
5538 mpi->sys_image_guid = dev->sys_image_guid;
5539 dev->port[i].mp.mpi = mpi;
5540 mpi->ibdev = dev;
5541 mpi = NULL;
5542 continue;
5543 }
5544
5545 list_for_each_entry(mpi, &mlx5_ib_unaffiliated_port_list,
5546 list) {
5547 if (dev->sys_image_guid == mpi->sys_image_guid &&
5548 (mlx5_core_native_port_num(mpi->mdev) - 1) == i) {
5549 bound = mlx5_ib_bind_slave_port(dev, mpi);
5550 }
5551
5552 if (bound) {
5553 dev_dbg(&mpi->mdev->pdev->dev, "removing port from unaffiliated list.\n");
5554 mlx5_ib_dbg(dev, "port %d bound\n", i + 1);
5555 list_del(&mpi->list);
5556 break;
5557 }
5558 }
5559 if (!bound) {
5560 get_port_caps(dev, i + 1);
5561 mlx5_ib_dbg(dev, "no free port found for port %d\n",
5562 i + 1);
5563 }
5564 }
5565
5566 list_add_tail(&dev->ib_dev_list, &mlx5_ib_dev_list);
5567 mutex_unlock(&mlx5_ib_multiport_mutex);
5568 return err;
5569}
5570
5571static void mlx5_ib_cleanup_multiport_master(struct mlx5_ib_dev *dev)
5572{
5573 int port_num = mlx5_core_native_port_num(dev->mdev) - 1;
5574 enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev,
5575 port_num + 1);
5576 int i;
5577
5578 if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
5579 return;
5580
5581 mutex_lock(&mlx5_ib_multiport_mutex);
5582 for (i = 0; i < dev->num_ports; i++) {
5583 if (dev->port[i].mp.mpi) {
5584 /* Destroy the native port stub */
5585 if (i == port_num) {
5586 kfree(dev->port[i].mp.mpi);
5587 dev->port[i].mp.mpi = NULL;
5588 } else {
5589 mlx5_ib_dbg(dev, "unbinding port_num: %d\n", i + 1);
5590 mlx5_ib_unbind_slave_port(dev, dev->port[i].mp.mpi);
5591 }
5592 }
5593 }
5594
5595 mlx5_ib_dbg(dev, "removing from devlist\n");
5596 list_del(&dev->ib_dev_list);
5597 mutex_unlock(&mlx5_ib_multiport_mutex);
5598
5599 mlx5_nic_vport_disable_roce(dev->mdev);
5600}
5601
Jason Gunthorpe9a119cd2018-07-04 08:50:28 +03005602ADD_UVERBS_ATTRIBUTES_SIMPLE(
5603 mlx5_ib_dm,
5604 UVERBS_OBJECT_DM,
5605 UVERBS_METHOD_DM_ALLOC,
5606 UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_ALLOC_DM_RESP_START_OFFSET,
5607 UVERBS_ATTR_TYPE(u64),
Jason Gunthorpe83bb4442018-07-04 08:50:29 +03005608 UA_MANDATORY),
Jason Gunthorpe9a119cd2018-07-04 08:50:28 +03005609 UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_ALLOC_DM_RESP_PAGE_INDEX,
5610 UVERBS_ATTR_TYPE(u16),
Jason Gunthorpe83bb4442018-07-04 08:50:29 +03005611 UA_MANDATORY));
Ariel Levkovich24da0012018-04-05 18:53:27 +03005612
Jason Gunthorpe9a119cd2018-07-04 08:50:28 +03005613ADD_UVERBS_ATTRIBUTES_SIMPLE(
5614 mlx5_ib_flow_action,
5615 UVERBS_OBJECT_FLOW_ACTION,
5616 UVERBS_METHOD_FLOW_ACTION_ESP_CREATE,
Jason Gunthorpebccd0622018-07-26 16:37:14 -06005617 UVERBS_ATTR_FLAGS_IN(MLX5_IB_ATTR_CREATE_FLOW_ACTION_FLAGS,
5618 enum mlx5_ib_uapi_flow_action_flags));
Aviad Yehezkelc6475a02018-03-28 09:27:50 +03005619
Matan Barak8c846602018-03-28 09:27:41 +03005620static int populate_specs_root(struct mlx5_ib_dev *dev)
5621{
Jason Gunthorpe7d96c9b2018-08-09 20:14:35 -06005622 const struct uverbs_object_tree_def **trees = dev->driver_trees;
5623 size_t num_trees = 0;
Matan Barak8c846602018-03-28 09:27:41 +03005624
Jason Gunthorpe7d96c9b2018-08-09 20:14:35 -06005625 if (mlx5_accel_ipsec_device_caps(dev->mdev) &
5626 MLX5_ACCEL_IPSEC_CAP_DEVICE)
5627 trees[num_trees++] = &mlx5_ib_flow_action;
Aviad Yehezkelc6475a02018-03-28 09:27:50 +03005628
Jason Gunthorpe7d96c9b2018-08-09 20:14:35 -06005629 if (MLX5_CAP_DEV_MEM(dev->mdev, memic))
5630 trees[num_trees++] = &mlx5_ib_dm;
Ariel Levkovich24da0012018-04-05 18:53:27 +03005631
Yishai Hadasc59450c2018-06-17 13:00:06 +03005632 if (MLX5_CAP_GEN_64(dev->mdev, general_obj_types) &
Jason Gunthorpe7d96c9b2018-08-09 20:14:35 -06005633 MLX5_GENERAL_OBJ_TYPES_CAP_UCTX)
5634 trees[num_trees++] = mlx5_ib_get_devx_tree();
Yishai Hadasc59450c2018-06-17 13:00:06 +03005635
Jason Gunthorpe7d96c9b2018-08-09 20:14:35 -06005636 num_trees += mlx5_ib_get_flow_trees(trees + num_trees);
Yishai Hadascb80fb12018-07-23 15:25:12 +03005637
Jason Gunthorpe7d96c9b2018-08-09 20:14:35 -06005638 WARN_ON(num_trees >= ARRAY_SIZE(dev->driver_trees));
5639 trees[num_trees] = NULL;
5640 dev->ib_dev.driver_specs = trees;
Matan Barak8c846602018-03-28 09:27:41 +03005641
Jason Gunthorpe7d96c9b2018-08-09 20:14:35 -06005642 return 0;
Matan Barak8c846602018-03-28 09:27:41 +03005643}
5644
Raed Salem1a1e03d2018-05-31 16:43:41 +03005645static int mlx5_ib_read_counters(struct ib_counters *counters,
5646 struct ib_counters_read_attr *read_attr,
5647 struct uverbs_attr_bundle *attrs)
5648{
5649 struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
5650 struct mlx5_read_counters_attr mread_attr = {};
5651 struct mlx5_ib_flow_counters_desc *desc;
5652 int ret, i;
5653
5654 mutex_lock(&mcounters->mcntrs_mutex);
5655 if (mcounters->cntrs_max_index > read_attr->ncounters) {
5656 ret = -EINVAL;
5657 goto err_bound;
5658 }
5659
5660 mread_attr.out = kcalloc(mcounters->counters_num, sizeof(u64),
5661 GFP_KERNEL);
5662 if (!mread_attr.out) {
5663 ret = -ENOMEM;
5664 goto err_bound;
5665 }
5666
5667 mread_attr.hw_cntrs_hndl = mcounters->hw_cntrs_hndl;
5668 mread_attr.flags = read_attr->flags;
5669 ret = mcounters->read_counters(counters->device, &mread_attr);
5670 if (ret)
5671 goto err_read;
5672
5673 /* do the pass over the counters data array to assign according to the
5674 * descriptions and indexing pairs
5675 */
5676 desc = mcounters->counters_data;
5677 for (i = 0; i < mcounters->ncounters; i++)
5678 read_attr->counters_buff[desc[i].index] += mread_attr.out[desc[i].description];
5679
5680err_read:
5681 kfree(mread_attr.out);
5682err_bound:
5683 mutex_unlock(&mcounters->mcntrs_mutex);
5684 return ret;
5685}
5686
Raed Salemb29e2a12018-05-31 16:43:38 +03005687static int mlx5_ib_destroy_counters(struct ib_counters *counters)
5688{
5689 struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
5690
Raed Salem3b3233f2018-05-31 16:43:39 +03005691 counters_clear_description(counters);
5692 if (mcounters->hw_cntrs_hndl)
5693 mlx5_fc_destroy(to_mdev(counters->device)->mdev,
5694 mcounters->hw_cntrs_hndl);
5695
Raed Salemb29e2a12018-05-31 16:43:38 +03005696 kfree(mcounters);
5697
5698 return 0;
5699}
5700
5701static struct ib_counters *mlx5_ib_create_counters(struct ib_device *device,
5702 struct uverbs_attr_bundle *attrs)
5703{
5704 struct mlx5_ib_mcounters *mcounters;
5705
5706 mcounters = kzalloc(sizeof(*mcounters), GFP_KERNEL);
5707 if (!mcounters)
5708 return ERR_PTR(-ENOMEM);
5709
Raed Salem3b3233f2018-05-31 16:43:39 +03005710 mutex_init(&mcounters->mcntrs_mutex);
5711
Raed Salemb29e2a12018-05-31 16:43:38 +03005712 return &mcounters->ibcntrs;
5713}
5714
Mark Blochb5ca15a2018-01-23 11:16:30 +00005715void mlx5_ib_stage_init_cleanup(struct mlx5_ib_dev *dev)
Eli Cohene126ba92013-07-07 17:25:49 +03005716{
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005717 mlx5_ib_cleanup_multiport_master(dev);
Mark Bloch3cc297d2018-01-01 13:07:03 +02005718#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
5719 cleanup_srcu_struct(&dev->mr_srcu);
5720#endif
Mark Bloch16c19752018-01-01 13:06:58 +02005721 kfree(dev->port);
5722}
5723
Mark Blochb5ca15a2018-01-23 11:16:30 +00005724int mlx5_ib_stage_init_init(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02005725{
5726 struct mlx5_core_dev *mdev = dev->mdev;
Aviv Heller4babcf92016-09-18 20:48:03 +03005727 const char *name;
Eli Cohene126ba92013-07-07 17:25:49 +03005728 int err;
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005729 int i;
Eli Cohene126ba92013-07-07 17:25:49 +03005730
Daniel Jurgens508562d2018-01-04 17:25:34 +02005731 dev->port = kcalloc(dev->num_ports, sizeof(*dev->port),
Mark Bloch0837e862016-06-17 15:10:55 +03005732 GFP_KERNEL);
5733 if (!dev->port)
Mark Bloch16c19752018-01-01 13:06:58 +02005734 return -ENOMEM;
Mark Bloch0837e862016-06-17 15:10:55 +03005735
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005736 for (i = 0; i < dev->num_ports; i++) {
5737 spin_lock_init(&dev->port[i].mp.mpi_lock);
5738 rwlock_init(&dev->roce[i].netdev_lock);
5739 }
5740
5741 err = mlx5_ib_init_multiport_master(dev);
Eli Cohene126ba92013-07-07 17:25:49 +03005742 if (err)
Mark Bloch0837e862016-06-17 15:10:55 +03005743 goto err_free_port;
Eli Cohene126ba92013-07-07 17:25:49 +03005744
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005745 if (!mlx5_core_mp_enabled(mdev)) {
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005746 for (i = 1; i <= dev->num_ports; i++) {
5747 err = get_port_caps(dev, i);
5748 if (err)
5749 break;
5750 }
5751 } else {
5752 err = get_port_caps(dev, mlx5_core_native_port_num(mdev));
5753 }
5754 if (err)
5755 goto err_mp;
5756
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03005757 if (mlx5_use_mad_ifc(dev))
5758 get_ext_port_caps(dev);
Eli Cohene126ba92013-07-07 17:25:49 +03005759
Aviv Heller4babcf92016-09-18 20:48:03 +03005760 if (!mlx5_lag_is_active(mdev))
5761 name = "mlx5_%d";
5762 else
5763 name = "mlx5_bond_%d";
5764
5765 strlcpy(dev->ib_dev.name, name, IB_DEVICE_NAME_MAX);
Eli Cohene126ba92013-07-07 17:25:49 +03005766 dev->ib_dev.owner = THIS_MODULE;
5767 dev->ib_dev.node_type = RDMA_NODE_IB_CA;
Sagi Grimbergc6790aa2015-09-24 10:34:23 +03005768 dev->ib_dev.local_dma_lkey = 0 /* not supported for now */;
Daniel Jurgens508562d2018-01-04 17:25:34 +02005769 dev->ib_dev.phys_port_cnt = dev->num_ports;
Saeed Mahameed233d05d2015-04-02 17:07:32 +03005770 dev->ib_dev.num_comp_vectors =
5771 dev->mdev->priv.eq_table.num_comp_vectors;
Bart Van Assche9b0c2892017-01-20 13:04:21 -08005772 dev->ib_dev.dev.parent = &mdev->pdev->dev;
Eli Cohene126ba92013-07-07 17:25:49 +03005773
Mark Bloch3cc297d2018-01-01 13:07:03 +02005774 mutex_init(&dev->cap_mask_mutex);
5775 INIT_LIST_HEAD(&dev->qp_list);
5776 spin_lock_init(&dev->reset_flow_resource_lock);
5777
Ariel Levkovich24da0012018-04-05 18:53:27 +03005778 spin_lock_init(&dev->memic.memic_lock);
5779 dev->memic.dev = mdev;
5780
Mark Bloch3cc297d2018-01-01 13:07:03 +02005781#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
5782 err = init_srcu_struct(&dev->mr_srcu);
5783 if (err)
5784 goto err_free_port;
5785#endif
5786
Mark Bloch16c19752018-01-01 13:06:58 +02005787 return 0;
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005788err_mp:
5789 mlx5_ib_cleanup_multiport_master(dev);
Mark Bloch16c19752018-01-01 13:06:58 +02005790
5791err_free_port:
5792 kfree(dev->port);
5793
5794 return -ENOMEM;
5795}
5796
Mark Bloch9a4ca382018-01-16 14:42:35 +00005797static int mlx5_ib_stage_flow_db_init(struct mlx5_ib_dev *dev)
5798{
5799 dev->flow_db = kzalloc(sizeof(*dev->flow_db), GFP_KERNEL);
5800
5801 if (!dev->flow_db)
5802 return -ENOMEM;
5803
5804 mutex_init(&dev->flow_db->lock);
5805
5806 return 0;
5807}
5808
Mark Blochb5ca15a2018-01-23 11:16:30 +00005809int mlx5_ib_stage_rep_flow_db_init(struct mlx5_ib_dev *dev)
5810{
5811 struct mlx5_ib_dev *nic_dev;
5812
5813 nic_dev = mlx5_ib_get_uplink_ibdev(dev->mdev->priv.eswitch);
5814
5815 if (!nic_dev)
5816 return -EINVAL;
5817
5818 dev->flow_db = nic_dev->flow_db;
5819
5820 return 0;
5821}
5822
Mark Bloch9a4ca382018-01-16 14:42:35 +00005823static void mlx5_ib_stage_flow_db_cleanup(struct mlx5_ib_dev *dev)
5824{
5825 kfree(dev->flow_db);
5826}
5827
Mark Blochb5ca15a2018-01-23 11:16:30 +00005828int mlx5_ib_stage_caps_init(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02005829{
5830 struct mlx5_core_dev *mdev = dev->mdev;
Mark Bloch16c19752018-01-01 13:06:58 +02005831 int err;
5832
Eli Cohene126ba92013-07-07 17:25:49 +03005833 dev->ib_dev.uverbs_abi_ver = MLX5_IB_UVERBS_ABI_VERSION;
5834 dev->ib_dev.uverbs_cmd_mask =
5835 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
5836 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
5837 (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
5838 (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
5839 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
Moni Shoua41c450f2016-11-23 08:23:26 +02005840 (1ull << IB_USER_VERBS_CMD_CREATE_AH) |
5841 (1ull << IB_USER_VERBS_CMD_DESTROY_AH) |
Eli Cohene126ba92013-07-07 17:25:49 +03005842 (1ull << IB_USER_VERBS_CMD_REG_MR) |
Noa Osherovich56e11d62016-02-29 16:46:51 +02005843 (1ull << IB_USER_VERBS_CMD_REREG_MR) |
Eli Cohene126ba92013-07-07 17:25:49 +03005844 (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
5845 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
5846 (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
5847 (1ull << IB_USER_VERBS_CMD_RESIZE_CQ) |
5848 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
5849 (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
5850 (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
5851 (1ull << IB_USER_VERBS_CMD_QUERY_QP) |
5852 (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
5853 (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST) |
5854 (1ull << IB_USER_VERBS_CMD_DETACH_MCAST) |
5855 (1ull << IB_USER_VERBS_CMD_CREATE_SRQ) |
5856 (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ) |
5857 (1ull << IB_USER_VERBS_CMD_QUERY_SRQ) |
5858 (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ) |
5859 (1ull << IB_USER_VERBS_CMD_CREATE_XSRQ) |
5860 (1ull << IB_USER_VERBS_CMD_OPEN_QP);
Haggai Eran1707cb42015-02-08 13:28:52 +02005861 dev->ib_dev.uverbs_ex_cmd_mask =
Matan Barakd4584dd2016-01-28 17:51:46 +02005862 (1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE) |
5863 (1ull << IB_USER_VERBS_EX_CMD_CREATE_CQ) |
Bodong Wang7d29f342016-12-01 13:43:16 +02005864 (1ull << IB_USER_VERBS_EX_CMD_CREATE_QP) |
Yonatan Cohenb0e9df62017-11-13 10:51:15 +02005865 (1ull << IB_USER_VERBS_EX_CMD_MODIFY_QP) |
5866 (1ull << IB_USER_VERBS_EX_CMD_MODIFY_CQ);
Eli Cohene126ba92013-07-07 17:25:49 +03005867
5868 dev->ib_dev.query_device = mlx5_ib_query_device;
Achiad Shochatebd61f62015-12-23 18:47:16 +02005869 dev->ib_dev.get_link_layer = mlx5_ib_port_link_layer;
Eli Cohene126ba92013-07-07 17:25:49 +03005870 dev->ib_dev.query_gid = mlx5_ib_query_gid;
Achiad Shochat3cca2602015-12-23 18:47:23 +02005871 dev->ib_dev.add_gid = mlx5_ib_add_gid;
5872 dev->ib_dev.del_gid = mlx5_ib_del_gid;
Eli Cohene126ba92013-07-07 17:25:49 +03005873 dev->ib_dev.query_pkey = mlx5_ib_query_pkey;
5874 dev->ib_dev.modify_device = mlx5_ib_modify_device;
5875 dev->ib_dev.modify_port = mlx5_ib_modify_port;
5876 dev->ib_dev.alloc_ucontext = mlx5_ib_alloc_ucontext;
5877 dev->ib_dev.dealloc_ucontext = mlx5_ib_dealloc_ucontext;
5878 dev->ib_dev.mmap = mlx5_ib_mmap;
5879 dev->ib_dev.alloc_pd = mlx5_ib_alloc_pd;
5880 dev->ib_dev.dealloc_pd = mlx5_ib_dealloc_pd;
5881 dev->ib_dev.create_ah = mlx5_ib_create_ah;
5882 dev->ib_dev.query_ah = mlx5_ib_query_ah;
5883 dev->ib_dev.destroy_ah = mlx5_ib_destroy_ah;
5884 dev->ib_dev.create_srq = mlx5_ib_create_srq;
5885 dev->ib_dev.modify_srq = mlx5_ib_modify_srq;
5886 dev->ib_dev.query_srq = mlx5_ib_query_srq;
5887 dev->ib_dev.destroy_srq = mlx5_ib_destroy_srq;
5888 dev->ib_dev.post_srq_recv = mlx5_ib_post_srq_recv;
5889 dev->ib_dev.create_qp = mlx5_ib_create_qp;
5890 dev->ib_dev.modify_qp = mlx5_ib_modify_qp;
5891 dev->ib_dev.query_qp = mlx5_ib_query_qp;
5892 dev->ib_dev.destroy_qp = mlx5_ib_destroy_qp;
Yishai Hadasd0e84c02018-06-19 10:43:55 +03005893 dev->ib_dev.drain_sq = mlx5_ib_drain_sq;
5894 dev->ib_dev.drain_rq = mlx5_ib_drain_rq;
Eli Cohene126ba92013-07-07 17:25:49 +03005895 dev->ib_dev.post_send = mlx5_ib_post_send;
5896 dev->ib_dev.post_recv = mlx5_ib_post_recv;
5897 dev->ib_dev.create_cq = mlx5_ib_create_cq;
5898 dev->ib_dev.modify_cq = mlx5_ib_modify_cq;
5899 dev->ib_dev.resize_cq = mlx5_ib_resize_cq;
5900 dev->ib_dev.destroy_cq = mlx5_ib_destroy_cq;
5901 dev->ib_dev.poll_cq = mlx5_ib_poll_cq;
5902 dev->ib_dev.req_notify_cq = mlx5_ib_arm_cq;
5903 dev->ib_dev.get_dma_mr = mlx5_ib_get_dma_mr;
5904 dev->ib_dev.reg_user_mr = mlx5_ib_reg_user_mr;
Noa Osherovich56e11d62016-02-29 16:46:51 +02005905 dev->ib_dev.rereg_user_mr = mlx5_ib_rereg_user_mr;
Eli Cohene126ba92013-07-07 17:25:49 +03005906 dev->ib_dev.dereg_mr = mlx5_ib_dereg_mr;
5907 dev->ib_dev.attach_mcast = mlx5_ib_mcg_attach;
5908 dev->ib_dev.detach_mcast = mlx5_ib_mcg_detach;
5909 dev->ib_dev.process_mad = mlx5_ib_process_mad;
Sagi Grimberg9bee1782015-07-30 10:32:35 +03005910 dev->ib_dev.alloc_mr = mlx5_ib_alloc_mr;
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03005911 dev->ib_dev.map_mr_sg = mlx5_ib_map_mr_sg;
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02005912 dev->ib_dev.check_mr_status = mlx5_ib_check_mr_status;
Ira Weinyc7342822016-06-15 02:22:01 -04005913 dev->ib_dev.get_dev_fw_str = get_dev_fw_str;
Sagi Grimberg40b24402017-07-13 11:09:42 +03005914 dev->ib_dev.get_vector_affinity = mlx5_ib_get_vector_affinity;
Niranjana Vishwanathapura8e959602017-06-30 13:14:46 -07005915 if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads))
Alex Vesker022d0382017-06-14 09:59:06 +03005916 dev->ib_dev.alloc_rdma_netdev = mlx5_ib_alloc_rdma_netdev;
Niranjana Vishwanathapura8e959602017-06-30 13:14:46 -07005917
Eli Coheneff901d2016-03-11 22:58:42 +02005918 if (mlx5_core_is_pf(mdev)) {
5919 dev->ib_dev.get_vf_config = mlx5_ib_get_vf_config;
5920 dev->ib_dev.set_vf_link_state = mlx5_ib_set_vf_link_state;
5921 dev->ib_dev.get_vf_stats = mlx5_ib_get_vf_stats;
5922 dev->ib_dev.set_vf_guid = mlx5_ib_set_vf_guid;
5923 }
Eli Cohene126ba92013-07-07 17:25:49 +03005924
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03005925 dev->ib_dev.disassociate_ucontext = mlx5_ib_disassociate_ucontext;
5926
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03005927 dev->umr_fence = mlx5_get_umr_fence(MLX5_CAP_GEN(mdev, umr_fence));
5928
Matan Barakd2370e02016-02-29 18:05:30 +02005929 if (MLX5_CAP_GEN(mdev, imaicl)) {
5930 dev->ib_dev.alloc_mw = mlx5_ib_alloc_mw;
5931 dev->ib_dev.dealloc_mw = mlx5_ib_dealloc_mw;
5932 dev->ib_dev.uverbs_cmd_mask |=
5933 (1ull << IB_USER_VERBS_CMD_ALLOC_MW) |
5934 (1ull << IB_USER_VERBS_CMD_DEALLOC_MW);
5935 }
5936
Saeed Mahameed938fe832015-05-28 22:28:41 +03005937 if (MLX5_CAP_GEN(mdev, xrc)) {
Eli Cohene126ba92013-07-07 17:25:49 +03005938 dev->ib_dev.alloc_xrcd = mlx5_ib_alloc_xrcd;
5939 dev->ib_dev.dealloc_xrcd = mlx5_ib_dealloc_xrcd;
5940 dev->ib_dev.uverbs_cmd_mask |=
5941 (1ull << IB_USER_VERBS_CMD_OPEN_XRCD) |
5942 (1ull << IB_USER_VERBS_CMD_CLOSE_XRCD);
5943 }
5944
Ariel Levkovich24da0012018-04-05 18:53:27 +03005945 if (MLX5_CAP_DEV_MEM(mdev, memic)) {
5946 dev->ib_dev.alloc_dm = mlx5_ib_alloc_dm;
5947 dev->ib_dev.dealloc_dm = mlx5_ib_dealloc_dm;
Ariel Levkovich6c29f572018-04-05 18:53:29 +03005948 dev->ib_dev.reg_dm_mr = mlx5_ib_reg_dm_mr;
Ariel Levkovich24da0012018-04-05 18:53:27 +03005949 }
5950
Yishai Hadas81e30882017-06-08 16:15:09 +03005951 dev->ib_dev.create_flow = mlx5_ib_create_flow;
5952 dev->ib_dev.destroy_flow = mlx5_ib_destroy_flow;
5953 dev->ib_dev.uverbs_ex_cmd_mask |=
5954 (1ull << IB_USER_VERBS_EX_CMD_CREATE_FLOW) |
5955 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_FLOW);
Aviad Yehezkelc6475a02018-03-28 09:27:50 +03005956 dev->ib_dev.create_flow_action_esp = mlx5_ib_create_flow_action_esp;
5957 dev->ib_dev.destroy_flow_action = mlx5_ib_destroy_flow_action;
Matan Barak349705c2018-03-28 09:27:51 +03005958 dev->ib_dev.modify_flow_action_esp = mlx5_ib_modify_flow_action_esp;
Matan Barak0ede73b2018-03-19 15:02:34 +02005959 dev->ib_dev.driver_id = RDMA_DRIVER_MLX5;
Raed Salemb29e2a12018-05-31 16:43:38 +03005960 dev->ib_dev.create_counters = mlx5_ib_create_counters;
5961 dev->ib_dev.destroy_counters = mlx5_ib_destroy_counters;
Raed Salem1a1e03d2018-05-31 16:43:41 +03005962 dev->ib_dev.read_counters = mlx5_ib_read_counters;
Yishai Hadas81e30882017-06-08 16:15:09 +03005963
Eli Cohene126ba92013-07-07 17:25:49 +03005964 err = init_node_data(dev);
5965 if (err)
Mark Bloch16c19752018-01-01 13:06:58 +02005966 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03005967
Mark Blochc8b89922018-01-01 13:07:02 +02005968 if ((MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_ETH) &&
Jason Gunthorpee7996a92018-01-29 13:26:40 -07005969 (MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) ||
5970 MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
Mark Blochc8b89922018-01-01 13:07:02 +02005971 mutex_init(&dev->lb_mutex);
5972
Mark Bloch16c19752018-01-01 13:06:58 +02005973 return 0;
5974}
5975
Mark Bloch8e6efa32017-11-06 12:22:13 +00005976static int mlx5_ib_stage_non_default_cb(struct mlx5_ib_dev *dev)
5977{
5978 dev->ib_dev.get_port_immutable = mlx5_port_immutable;
5979 dev->ib_dev.query_port = mlx5_ib_query_port;
5980
5981 return 0;
5982}
5983
Mark Blochb5ca15a2018-01-23 11:16:30 +00005984int mlx5_ib_stage_rep_non_default_cb(struct mlx5_ib_dev *dev)
Mark Bloch8e6efa32017-11-06 12:22:13 +00005985{
5986 dev->ib_dev.get_port_immutable = mlx5_port_rep_immutable;
5987 dev->ib_dev.query_port = mlx5_ib_rep_query_port;
5988
5989 return 0;
5990}
5991
Leon Romanovskye3f1ed12018-07-08 12:55:43 +03005992static int mlx5_ib_stage_common_roce_init(struct mlx5_ib_dev *dev)
Mark Bloch8e6efa32017-11-06 12:22:13 +00005993{
Leon Romanovskye3f1ed12018-07-08 12:55:43 +03005994 u8 port_num;
Mark Bloch8e6efa32017-11-06 12:22:13 +00005995 int i;
5996
5997 for (i = 0; i < dev->num_ports; i++) {
5998 dev->roce[i].dev = dev;
5999 dev->roce[i].native_port_num = i + 1;
6000 dev->roce[i].last_port_state = IB_PORT_DOWN;
6001 }
6002
6003 dev->ib_dev.get_netdev = mlx5_ib_get_netdev;
6004 dev->ib_dev.create_wq = mlx5_ib_create_wq;
6005 dev->ib_dev.modify_wq = mlx5_ib_modify_wq;
6006 dev->ib_dev.destroy_wq = mlx5_ib_destroy_wq;
6007 dev->ib_dev.create_rwq_ind_table = mlx5_ib_create_rwq_ind_table;
6008 dev->ib_dev.destroy_rwq_ind_table = mlx5_ib_destroy_rwq_ind_table;
6009
6010 dev->ib_dev.uverbs_ex_cmd_mask |=
6011 (1ull << IB_USER_VERBS_EX_CMD_CREATE_WQ) |
6012 (1ull << IB_USER_VERBS_EX_CMD_MODIFY_WQ) |
6013 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_WQ) |
6014 (1ull << IB_USER_VERBS_EX_CMD_CREATE_RWQ_IND_TBL) |
6015 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_RWQ_IND_TBL);
6016
Leon Romanovskye3f1ed12018-07-08 12:55:43 +03006017 port_num = mlx5_core_native_port_num(dev->mdev) - 1;
6018
Mark Bloch8e6efa32017-11-06 12:22:13 +00006019 return mlx5_add_netdev_notifier(dev, port_num);
6020}
6021
6022static void mlx5_ib_stage_common_roce_cleanup(struct mlx5_ib_dev *dev)
6023{
6024 u8 port_num = mlx5_core_native_port_num(dev->mdev) - 1;
6025
6026 mlx5_remove_netdev_notifier(dev, port_num);
6027}
6028
6029int mlx5_ib_stage_rep_roce_init(struct mlx5_ib_dev *dev)
6030{
6031 struct mlx5_core_dev *mdev = dev->mdev;
6032 enum rdma_link_layer ll;
6033 int port_type_cap;
6034 int err = 0;
Mark Bloch8e6efa32017-11-06 12:22:13 +00006035
Mark Bloch8e6efa32017-11-06 12:22:13 +00006036 port_type_cap = MLX5_CAP_GEN(mdev, port_type);
6037 ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
6038
6039 if (ll == IB_LINK_LAYER_ETHERNET)
Leon Romanovskye3f1ed12018-07-08 12:55:43 +03006040 err = mlx5_ib_stage_common_roce_init(dev);
Mark Bloch8e6efa32017-11-06 12:22:13 +00006041
6042 return err;
6043}
6044
6045void mlx5_ib_stage_rep_roce_cleanup(struct mlx5_ib_dev *dev)
6046{
6047 mlx5_ib_stage_common_roce_cleanup(dev);
6048}
6049
Mark Bloch16c19752018-01-01 13:06:58 +02006050static int mlx5_ib_stage_roce_init(struct mlx5_ib_dev *dev)
6051{
6052 struct mlx5_core_dev *mdev = dev->mdev;
6053 enum rdma_link_layer ll;
6054 int port_type_cap;
6055 int err;
6056
6057 port_type_cap = MLX5_CAP_GEN(mdev, port_type);
6058 ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
6059
Achiad Shochatfc24fc52015-12-23 18:47:17 +02006060 if (ll == IB_LINK_LAYER_ETHERNET) {
Leon Romanovskye3f1ed12018-07-08 12:55:43 +03006061 err = mlx5_ib_stage_common_roce_init(dev);
Achiad Shochatfc24fc52015-12-23 18:47:17 +02006062 if (err)
Mark Bloch16c19752018-01-01 13:06:58 +02006063 return err;
Mark Bloch8e6efa32017-11-06 12:22:13 +00006064
Leon Romanovskye3f1ed12018-07-08 12:55:43 +03006065 err = mlx5_enable_eth(dev);
Mark Bloch8e6efa32017-11-06 12:22:13 +00006066 if (err)
6067 goto cleanup;
Achiad Shochatfc24fc52015-12-23 18:47:17 +02006068 }
6069
Mark Bloch16c19752018-01-01 13:06:58 +02006070 return 0;
Mark Bloch8e6efa32017-11-06 12:22:13 +00006071cleanup:
6072 mlx5_ib_stage_common_roce_cleanup(dev);
6073
6074 return err;
Mark Bloch16c19752018-01-01 13:06:58 +02006075}
Eli Cohene126ba92013-07-07 17:25:49 +03006076
Mark Bloch16c19752018-01-01 13:06:58 +02006077static void mlx5_ib_stage_roce_cleanup(struct mlx5_ib_dev *dev)
6078{
6079 struct mlx5_core_dev *mdev = dev->mdev;
6080 enum rdma_link_layer ll;
6081 int port_type_cap;
Eli Cohene126ba92013-07-07 17:25:49 +03006082
Mark Bloch16c19752018-01-01 13:06:58 +02006083 port_type_cap = MLX5_CAP_GEN(mdev, port_type);
6084 ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
6085
6086 if (ll == IB_LINK_LAYER_ETHERNET) {
6087 mlx5_disable_eth(dev);
Mark Bloch8e6efa32017-11-06 12:22:13 +00006088 mlx5_ib_stage_common_roce_cleanup(dev);
Kamal Heib45bded22017-01-18 14:10:32 +02006089 }
Mark Bloch16c19752018-01-01 13:06:58 +02006090}
Haggai Eran6aec21f2014-12-11 17:04:23 +02006091
Mark Blochb5ca15a2018-01-23 11:16:30 +00006092int mlx5_ib_stage_dev_res_init(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02006093{
6094 return create_dev_resources(&dev->devr);
6095}
Parav Pandit4a2da0b2017-05-30 10:05:15 +03006096
Mark Blochb5ca15a2018-01-23 11:16:30 +00006097void mlx5_ib_stage_dev_res_cleanup(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02006098{
6099 destroy_dev_resources(&dev->devr);
6100}
6101
6102static int mlx5_ib_stage_odp_init(struct mlx5_ib_dev *dev)
6103{
Mark Bloch07321b32018-01-01 13:07:00 +02006104 mlx5_ib_internal_fill_odp_caps(dev);
6105
Mark Bloch16c19752018-01-01 13:06:58 +02006106 return mlx5_ib_odp_init_one(dev);
6107}
6108
Mark Blochb5ca15a2018-01-23 11:16:30 +00006109int mlx5_ib_stage_counters_init(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02006110{
Mark Bloch5e1e7612018-01-01 13:07:01 +02006111 if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt)) {
6112 dev->ib_dev.get_hw_stats = mlx5_ib_get_hw_stats;
6113 dev->ib_dev.alloc_hw_stats = mlx5_ib_alloc_hw_stats;
6114
6115 return mlx5_ib_alloc_counters(dev);
6116 }
Mark Bloch16c19752018-01-01 13:06:58 +02006117
6118 return 0;
6119}
6120
Mark Blochb5ca15a2018-01-23 11:16:30 +00006121void mlx5_ib_stage_counters_cleanup(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02006122{
6123 if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt))
6124 mlx5_ib_dealloc_counters(dev);
6125}
6126
6127static int mlx5_ib_stage_cong_debugfs_init(struct mlx5_ib_dev *dev)
6128{
Parav Pandita9e546e2018-01-04 17:25:39 +02006129 return mlx5_ib_init_cong_debugfs(dev,
6130 mlx5_core_native_port_num(dev->mdev) - 1);
Mark Bloch16c19752018-01-01 13:06:58 +02006131}
6132
6133static void mlx5_ib_stage_cong_debugfs_cleanup(struct mlx5_ib_dev *dev)
6134{
Parav Pandita9e546e2018-01-04 17:25:39 +02006135 mlx5_ib_cleanup_cong_debugfs(dev,
6136 mlx5_core_native_port_num(dev->mdev) - 1);
Mark Bloch16c19752018-01-01 13:06:58 +02006137}
6138
6139static int mlx5_ib_stage_uar_init(struct mlx5_ib_dev *dev)
6140{
Eli Cohen5fe9dec2017-01-03 23:55:25 +02006141 dev->mdev->priv.uar = mlx5_get_uars_page(dev->mdev);
Leon Romanovsky444261c2018-04-23 17:01:56 +03006142 return PTR_ERR_OR_ZERO(dev->mdev->priv.uar);
Mark Bloch16c19752018-01-01 13:06:58 +02006143}
6144
6145static void mlx5_ib_stage_uar_cleanup(struct mlx5_ib_dev *dev)
6146{
6147 mlx5_put_uars_page(dev->mdev, dev->mdev->priv.uar);
6148}
6149
Mark Blochb5ca15a2018-01-23 11:16:30 +00006150int mlx5_ib_stage_bfrag_init(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02006151{
6152 int err;
Eli Cohen5fe9dec2017-01-03 23:55:25 +02006153
6154 err = mlx5_alloc_bfreg(dev->mdev, &dev->bfreg, false, false);
6155 if (err)
Mark Bloch16c19752018-01-01 13:06:58 +02006156 return err;
Eli Cohen5fe9dec2017-01-03 23:55:25 +02006157
6158 err = mlx5_alloc_bfreg(dev->mdev, &dev->fp_bfreg, false, true);
6159 if (err)
Mark Bloch16c19752018-01-01 13:06:58 +02006160 mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
Eli Cohen5fe9dec2017-01-03 23:55:25 +02006161
Mark Bloch16c19752018-01-01 13:06:58 +02006162 return err;
6163}
Mark Bloch0837e862016-06-17 15:10:55 +03006164
Mark Blochb5ca15a2018-01-23 11:16:30 +00006165void mlx5_ib_stage_bfrag_cleanup(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02006166{
6167 mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
6168 mlx5_free_bfreg(dev->mdev, &dev->bfreg);
6169}
Eli Cohene126ba92013-07-07 17:25:49 +03006170
Matan Barak8c846602018-03-28 09:27:41 +03006171static int mlx5_ib_stage_populate_specs(struct mlx5_ib_dev *dev)
6172{
6173 return populate_specs_root(dev);
6174}
6175
Mark Blochb5ca15a2018-01-23 11:16:30 +00006176int mlx5_ib_stage_ib_reg_init(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02006177{
6178 return ib_register_device(&dev->ib_dev, NULL);
6179}
6180
David S. Miller03fe2de2018-03-23 11:24:57 -04006181void mlx5_ib_stage_pre_ib_reg_umr_cleanup(struct mlx5_ib_dev *dev)
Mark Bloch42cea832018-03-14 09:14:15 +02006182{
6183 destroy_umrc_res(dev);
6184}
6185
Mark Blochb5ca15a2018-01-23 11:16:30 +00006186void mlx5_ib_stage_ib_reg_cleanup(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02006187{
6188 ib_unregister_device(&dev->ib_dev);
6189}
6190
David S. Miller03fe2de2018-03-23 11:24:57 -04006191int mlx5_ib_stage_post_ib_reg_umr_init(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02006192{
6193 return create_umr_res(dev);
6194}
6195
Mark Bloch16c19752018-01-01 13:06:58 +02006196static int mlx5_ib_stage_delay_drop_init(struct mlx5_ib_dev *dev)
6197{
Maor Gottlieb03404e82017-05-30 10:29:13 +03006198 init_delay_drop(dev);
6199
Mark Bloch16c19752018-01-01 13:06:58 +02006200 return 0;
6201}
6202
6203static void mlx5_ib_stage_delay_drop_cleanup(struct mlx5_ib_dev *dev)
6204{
6205 cancel_delay_drop(dev);
6206}
6207
Mark Blochb5ca15a2018-01-23 11:16:30 +00006208int mlx5_ib_stage_class_attr_init(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02006209{
6210 int err;
6211 int i;
6212
Eli Cohene126ba92013-07-07 17:25:49 +03006213 for (i = 0; i < ARRAY_SIZE(mlx5_class_attributes); i++) {
Wei Yongjun281d1a92013-07-30 07:54:26 +08006214 err = device_create_file(&dev->ib_dev.dev,
6215 mlx5_class_attributes[i]);
6216 if (err)
Mark Bloch16c19752018-01-01 13:06:58 +02006217 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03006218 }
6219
Mark Bloch16c19752018-01-01 13:06:58 +02006220 return 0;
6221}
6222
Mark Blochfc385b72018-01-16 14:34:48 +00006223static int mlx5_ib_stage_rep_reg_init(struct mlx5_ib_dev *dev)
6224{
6225 mlx5_ib_register_vport_reps(dev);
6226
6227 return 0;
6228}
6229
6230static void mlx5_ib_stage_rep_reg_cleanup(struct mlx5_ib_dev *dev)
6231{
6232 mlx5_ib_unregister_vport_reps(dev);
6233}
6234
Mark Blochb5ca15a2018-01-23 11:16:30 +00006235void __mlx5_ib_remove(struct mlx5_ib_dev *dev,
6236 const struct mlx5_ib_profile *profile,
6237 int stage)
Mark Bloch16c19752018-01-01 13:06:58 +02006238{
6239 /* Number of stages to cleanup */
6240 while (stage) {
6241 stage--;
6242 if (profile->stage[stage].cleanup)
6243 profile->stage[stage].cleanup(dev);
6244 }
6245
6246 ib_dealloc_device((struct ib_device *)dev);
6247}
6248
Mark Blochb5ca15a2018-01-23 11:16:30 +00006249void *__mlx5_ib_add(struct mlx5_ib_dev *dev,
6250 const struct mlx5_ib_profile *profile)
Mark Bloch16c19752018-01-01 13:06:58 +02006251{
Mark Bloch16c19752018-01-01 13:06:58 +02006252 int err;
6253 int i;
6254
6255 printk_once(KERN_INFO "%s", mlx5_version);
6256
Mark Bloch16c19752018-01-01 13:06:58 +02006257 for (i = 0; i < MLX5_IB_STAGE_MAX; i++) {
6258 if (profile->stage[i].init) {
6259 err = profile->stage[i].init(dev);
6260 if (err)
6261 goto err_out;
6262 }
6263 }
6264
6265 dev->profile = profile;
Eli Cohene126ba92013-07-07 17:25:49 +03006266 dev->ib_active = true;
6267
Jack Morgenstein9603b612014-07-28 23:30:22 +03006268 return dev;
Eli Cohene126ba92013-07-07 17:25:49 +03006269
Mark Bloch16c19752018-01-01 13:06:58 +02006270err_out:
6271 __mlx5_ib_remove(dev, profile, i);
Eli Cohene126ba92013-07-07 17:25:49 +03006272
Jack Morgenstein9603b612014-07-28 23:30:22 +03006273 return NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03006274}
6275
Mark Bloch16c19752018-01-01 13:06:58 +02006276static const struct mlx5_ib_profile pf_profile = {
6277 STAGE_CREATE(MLX5_IB_STAGE_INIT,
6278 mlx5_ib_stage_init_init,
6279 mlx5_ib_stage_init_cleanup),
Mark Bloch9a4ca382018-01-16 14:42:35 +00006280 STAGE_CREATE(MLX5_IB_STAGE_FLOW_DB,
6281 mlx5_ib_stage_flow_db_init,
6282 mlx5_ib_stage_flow_db_cleanup),
Mark Bloch16c19752018-01-01 13:06:58 +02006283 STAGE_CREATE(MLX5_IB_STAGE_CAPS,
6284 mlx5_ib_stage_caps_init,
6285 NULL),
Mark Bloch8e6efa32017-11-06 12:22:13 +00006286 STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB,
6287 mlx5_ib_stage_non_default_cb,
6288 NULL),
Mark Bloch16c19752018-01-01 13:06:58 +02006289 STAGE_CREATE(MLX5_IB_STAGE_ROCE,
6290 mlx5_ib_stage_roce_init,
6291 mlx5_ib_stage_roce_cleanup),
6292 STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES,
6293 mlx5_ib_stage_dev_res_init,
6294 mlx5_ib_stage_dev_res_cleanup),
6295 STAGE_CREATE(MLX5_IB_STAGE_ODP,
6296 mlx5_ib_stage_odp_init,
Mark Bloch3cc297d2018-01-01 13:07:03 +02006297 NULL),
Mark Bloch16c19752018-01-01 13:06:58 +02006298 STAGE_CREATE(MLX5_IB_STAGE_COUNTERS,
6299 mlx5_ib_stage_counters_init,
6300 mlx5_ib_stage_counters_cleanup),
6301 STAGE_CREATE(MLX5_IB_STAGE_CONG_DEBUGFS,
6302 mlx5_ib_stage_cong_debugfs_init,
6303 mlx5_ib_stage_cong_debugfs_cleanup),
6304 STAGE_CREATE(MLX5_IB_STAGE_UAR,
6305 mlx5_ib_stage_uar_init,
6306 mlx5_ib_stage_uar_cleanup),
6307 STAGE_CREATE(MLX5_IB_STAGE_BFREG,
6308 mlx5_ib_stage_bfrag_init,
6309 mlx5_ib_stage_bfrag_cleanup),
Mark Bloch42cea832018-03-14 09:14:15 +02006310 STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR,
6311 NULL,
6312 mlx5_ib_stage_pre_ib_reg_umr_cleanup),
Matan Barak8c846602018-03-28 09:27:41 +03006313 STAGE_CREATE(MLX5_IB_STAGE_SPECS,
6314 mlx5_ib_stage_populate_specs,
Jason Gunthorpe7d96c9b2018-08-09 20:14:35 -06006315 NULL),
Mark Bloch16c19752018-01-01 13:06:58 +02006316 STAGE_CREATE(MLX5_IB_STAGE_IB_REG,
6317 mlx5_ib_stage_ib_reg_init,
6318 mlx5_ib_stage_ib_reg_cleanup),
Mark Bloch42cea832018-03-14 09:14:15 +02006319 STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR,
6320 mlx5_ib_stage_post_ib_reg_umr_init,
6321 NULL),
Mark Bloch16c19752018-01-01 13:06:58 +02006322 STAGE_CREATE(MLX5_IB_STAGE_DELAY_DROP,
6323 mlx5_ib_stage_delay_drop_init,
6324 mlx5_ib_stage_delay_drop_cleanup),
6325 STAGE_CREATE(MLX5_IB_STAGE_CLASS_ATTR,
6326 mlx5_ib_stage_class_attr_init,
6327 NULL),
Mark Bloch16c19752018-01-01 13:06:58 +02006328};
6329
Mark Blochb5ca15a2018-01-23 11:16:30 +00006330static const struct mlx5_ib_profile nic_rep_profile = {
6331 STAGE_CREATE(MLX5_IB_STAGE_INIT,
6332 mlx5_ib_stage_init_init,
6333 mlx5_ib_stage_init_cleanup),
6334 STAGE_CREATE(MLX5_IB_STAGE_FLOW_DB,
6335 mlx5_ib_stage_flow_db_init,
6336 mlx5_ib_stage_flow_db_cleanup),
6337 STAGE_CREATE(MLX5_IB_STAGE_CAPS,
6338 mlx5_ib_stage_caps_init,
6339 NULL),
6340 STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB,
6341 mlx5_ib_stage_rep_non_default_cb,
6342 NULL),
6343 STAGE_CREATE(MLX5_IB_STAGE_ROCE,
6344 mlx5_ib_stage_rep_roce_init,
6345 mlx5_ib_stage_rep_roce_cleanup),
6346 STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES,
6347 mlx5_ib_stage_dev_res_init,
6348 mlx5_ib_stage_dev_res_cleanup),
6349 STAGE_CREATE(MLX5_IB_STAGE_COUNTERS,
6350 mlx5_ib_stage_counters_init,
6351 mlx5_ib_stage_counters_cleanup),
6352 STAGE_CREATE(MLX5_IB_STAGE_UAR,
6353 mlx5_ib_stage_uar_init,
6354 mlx5_ib_stage_uar_cleanup),
6355 STAGE_CREATE(MLX5_IB_STAGE_BFREG,
6356 mlx5_ib_stage_bfrag_init,
6357 mlx5_ib_stage_bfrag_cleanup),
David S. Miller03fe2de2018-03-23 11:24:57 -04006358 STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR,
6359 NULL,
6360 mlx5_ib_stage_pre_ib_reg_umr_cleanup),
Matan Barak8c846602018-03-28 09:27:41 +03006361 STAGE_CREATE(MLX5_IB_STAGE_SPECS,
6362 mlx5_ib_stage_populate_specs,
Jason Gunthorpe7d96c9b2018-08-09 20:14:35 -06006363 NULL),
Mark Blochb5ca15a2018-01-23 11:16:30 +00006364 STAGE_CREATE(MLX5_IB_STAGE_IB_REG,
6365 mlx5_ib_stage_ib_reg_init,
6366 mlx5_ib_stage_ib_reg_cleanup),
David S. Miller03fe2de2018-03-23 11:24:57 -04006367 STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR,
6368 mlx5_ib_stage_post_ib_reg_umr_init,
6369 NULL),
Mark Blochb5ca15a2018-01-23 11:16:30 +00006370 STAGE_CREATE(MLX5_IB_STAGE_CLASS_ATTR,
6371 mlx5_ib_stage_class_attr_init,
6372 NULL),
6373 STAGE_CREATE(MLX5_IB_STAGE_REP_REG,
6374 mlx5_ib_stage_rep_reg_init,
6375 mlx5_ib_stage_rep_reg_cleanup),
6376};
6377
Leon Romanovskye3f1ed12018-07-08 12:55:43 +03006378static void *mlx5_ib_add_slave_port(struct mlx5_core_dev *mdev)
Daniel Jurgens32f69e42018-01-04 17:25:36 +02006379{
6380 struct mlx5_ib_multiport_info *mpi;
6381 struct mlx5_ib_dev *dev;
6382 bool bound = false;
6383 int err;
6384
6385 mpi = kzalloc(sizeof(*mpi), GFP_KERNEL);
6386 if (!mpi)
6387 return NULL;
6388
6389 mpi->mdev = mdev;
6390
6391 err = mlx5_query_nic_vport_system_image_guid(mdev,
6392 &mpi->sys_image_guid);
6393 if (err) {
6394 kfree(mpi);
6395 return NULL;
6396 }
6397
6398 mutex_lock(&mlx5_ib_multiport_mutex);
6399 list_for_each_entry(dev, &mlx5_ib_dev_list, ib_dev_list) {
6400 if (dev->sys_image_guid == mpi->sys_image_guid)
6401 bound = mlx5_ib_bind_slave_port(dev, mpi);
6402
6403 if (bound) {
6404 rdma_roce_rescan_device(&dev->ib_dev);
6405 break;
6406 }
6407 }
6408
6409 if (!bound) {
6410 list_add_tail(&mpi->list, &mlx5_ib_unaffiliated_port_list);
6411 dev_dbg(&mdev->pdev->dev, "no suitable IB device found to bind to, added to unaffiliated list.\n");
Daniel Jurgens32f69e42018-01-04 17:25:36 +02006412 }
6413 mutex_unlock(&mlx5_ib_multiport_mutex);
6414
6415 return mpi;
6416}
6417
Mark Bloch16c19752018-01-01 13:06:58 +02006418static void *mlx5_ib_add(struct mlx5_core_dev *mdev)
6419{
Daniel Jurgens32f69e42018-01-04 17:25:36 +02006420 enum rdma_link_layer ll;
Mark Blochb5ca15a2018-01-23 11:16:30 +00006421 struct mlx5_ib_dev *dev;
Daniel Jurgens32f69e42018-01-04 17:25:36 +02006422 int port_type_cap;
6423
Mark Blochb5ca15a2018-01-23 11:16:30 +00006424 printk_once(KERN_INFO "%s", mlx5_version);
6425
Daniel Jurgens32f69e42018-01-04 17:25:36 +02006426 port_type_cap = MLX5_CAP_GEN(mdev, port_type);
6427 ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
6428
Leon Romanovskye3f1ed12018-07-08 12:55:43 +03006429 if (mlx5_core_is_mp_slave(mdev) && ll == IB_LINK_LAYER_ETHERNET)
6430 return mlx5_ib_add_slave_port(mdev);
Daniel Jurgens32f69e42018-01-04 17:25:36 +02006431
Mark Blochb5ca15a2018-01-23 11:16:30 +00006432 dev = (struct mlx5_ib_dev *)ib_alloc_device(sizeof(*dev));
6433 if (!dev)
6434 return NULL;
6435
6436 dev->mdev = mdev;
6437 dev->num_ports = max(MLX5_CAP_GEN(mdev, num_ports),
6438 MLX5_CAP_GEN(mdev, num_vhca_ports));
6439
Or Gerlitzaff22522018-05-31 11:40:17 +03006440 if (MLX5_ESWITCH_MANAGER(mdev) &&
Mark Blochb5ca15a2018-01-23 11:16:30 +00006441 mlx5_ib_eswitch_mode(mdev->priv.eswitch) == SRIOV_OFFLOADS) {
6442 dev->rep = mlx5_ib_vport_rep(mdev->priv.eswitch, 0);
6443
6444 return __mlx5_ib_add(dev, &nic_rep_profile);
6445 }
6446
6447 return __mlx5_ib_add(dev, &pf_profile);
Mark Bloch16c19752018-01-01 13:06:58 +02006448}
6449
Jack Morgenstein9603b612014-07-28 23:30:22 +03006450static void mlx5_ib_remove(struct mlx5_core_dev *mdev, void *context)
Eli Cohene126ba92013-07-07 17:25:49 +03006451{
Daniel Jurgens32f69e42018-01-04 17:25:36 +02006452 struct mlx5_ib_multiport_info *mpi;
6453 struct mlx5_ib_dev *dev;
Haggai Eran6aec21f2014-12-11 17:04:23 +02006454
Daniel Jurgens32f69e42018-01-04 17:25:36 +02006455 if (mlx5_core_is_mp_slave(mdev)) {
6456 mpi = context;
6457 mutex_lock(&mlx5_ib_multiport_mutex);
6458 if (mpi->ibdev)
6459 mlx5_ib_unbind_slave_port(mpi->ibdev, mpi);
6460 list_del(&mpi->list);
6461 mutex_unlock(&mlx5_ib_multiport_mutex);
6462 return;
6463 }
6464
6465 dev = context;
Mark Bloch16c19752018-01-01 13:06:58 +02006466 __mlx5_ib_remove(dev, dev->profile, MLX5_IB_STAGE_MAX);
Eli Cohene126ba92013-07-07 17:25:49 +03006467}
6468
Jack Morgenstein9603b612014-07-28 23:30:22 +03006469static struct mlx5_interface mlx5_ib_interface = {
6470 .add = mlx5_ib_add,
6471 .remove = mlx5_ib_remove,
6472 .event = mlx5_ib_event,
Artemy Kovalyovd9aaed82017-01-02 11:37:46 +02006473#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
6474 .pfault = mlx5_ib_pfault,
6475#endif
Saeed Mahameed64613d942015-04-02 17:07:34 +03006476 .protocol = MLX5_INTERFACE_PROTOCOL_IB,
Eli Cohene126ba92013-07-07 17:25:49 +03006477};
6478
Ilya Lesokhinc44ef992018-03-13 15:18:48 +02006479unsigned long mlx5_ib_get_xlt_emergency_page(void)
6480{
6481 mutex_lock(&xlt_emergency_page_mutex);
6482 return xlt_emergency_page;
6483}
6484
6485void mlx5_ib_put_xlt_emergency_page(void)
6486{
6487 mutex_unlock(&xlt_emergency_page_mutex);
6488}
6489
Eli Cohene126ba92013-07-07 17:25:49 +03006490static int __init mlx5_ib_init(void)
6491{
Haggai Eran6aec21f2014-12-11 17:04:23 +02006492 int err;
6493
Ilya Lesokhinc44ef992018-03-13 15:18:48 +02006494 xlt_emergency_page = __get_free_page(GFP_KERNEL);
6495 if (!xlt_emergency_page)
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02006496 return -ENOMEM;
6497
Ilya Lesokhinc44ef992018-03-13 15:18:48 +02006498 mutex_init(&xlt_emergency_page_mutex);
6499
6500 mlx5_ib_event_wq = alloc_ordered_workqueue("mlx5_ib_event_wq", 0);
6501 if (!mlx5_ib_event_wq) {
6502 free_page(xlt_emergency_page);
6503 return -ENOMEM;
6504 }
6505
Artemy Kovalyov81713d32017-01-18 16:58:11 +02006506 mlx5_ib_odp_init();
Jack Morgenstein9603b612014-07-28 23:30:22 +03006507
Haggai Eran6aec21f2014-12-11 17:04:23 +02006508 err = mlx5_register_interface(&mlx5_ib_interface);
Haggai Eran6aec21f2014-12-11 17:04:23 +02006509
6510 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03006511}
6512
6513static void __exit mlx5_ib_cleanup(void)
6514{
Jack Morgenstein9603b612014-07-28 23:30:22 +03006515 mlx5_unregister_interface(&mlx5_ib_interface);
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02006516 destroy_workqueue(mlx5_ib_event_wq);
Ilya Lesokhinc44ef992018-03-13 15:18:48 +02006517 mutex_destroy(&xlt_emergency_page_mutex);
6518 free_page(xlt_emergency_page);
Eli Cohene126ba92013-07-07 17:25:49 +03006519}
6520
6521module_init(mlx5_ib_init);
6522module_exit(mlx5_ib_cleanup);