blob: 11ed9416db4863e95ca3620235be2a1aa7b64dc3 [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
Eli Cohene126ba92013-07-07 17:25:49 +03001829 return &context->ibucontext;
1830
Yishai Hadasa8b92ca2018-06-17 12:59:57 +03001831out_mdev:
1832 if (req.flags & MLX5_IB_ALLOC_UCTX_DEVX)
1833 mlx5_ib_devx_destroy(dev, context);
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001834out_td:
Leon Romanovskycfdeb892018-06-19 10:39:06 +03001835 mlx5_ib_dealloc_transport_domain(dev, context->tdn);
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001836
Eli Cohene126ba92013-07-07 17:25:49 +03001837out_uars:
Eli Cohenb037c292017-01-03 23:55:26 +02001838 deallocate_uars(dev, context);
1839
1840out_sys_pages:
1841 kfree(bfregi->sys_pages);
1842
Eli Cohene126ba92013-07-07 17:25:49 +03001843out_count:
Eli Cohen2f5ff262017-01-03 23:55:21 +02001844 kfree(bfregi->count);
Eli Cohene126ba92013-07-07 17:25:49 +03001845
Eli Cohene126ba92013-07-07 17:25:49 +03001846out_ctx:
1847 kfree(context);
Eli Cohenb037c292017-01-03 23:55:26 +02001848
Eli Cohene126ba92013-07-07 17:25:49 +03001849 return ERR_PTR(err);
1850}
1851
1852static int mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
1853{
1854 struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1855 struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
Eli Cohenb037c292017-01-03 23:55:26 +02001856 struct mlx5_bfreg_info *bfregi;
Eli Cohene126ba92013-07-07 17:25:49 +03001857
Yishai Hadasa8b92ca2018-06-17 12:59:57 +03001858 if (context->devx_uid)
1859 mlx5_ib_devx_destroy(dev, context);
1860
Eli Cohenb037c292017-01-03 23:55:26 +02001861 bfregi = &context->bfregi;
Leon Romanovskycfdeb892018-06-19 10:39:06 +03001862 mlx5_ib_dealloc_transport_domain(dev, context->tdn);
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001863
Eli Cohenb037c292017-01-03 23:55:26 +02001864 deallocate_uars(dev, context);
1865 kfree(bfregi->sys_pages);
Eli Cohen2f5ff262017-01-03 23:55:21 +02001866 kfree(bfregi->count);
Eli Cohene126ba92013-07-07 17:25:49 +03001867 kfree(context);
1868
1869 return 0;
1870}
1871
Eli Cohenb037c292017-01-03 23:55:26 +02001872static phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev,
Yishai Hadas4ed131d2017-12-24 16:31:35 +02001873 int uar_idx)
Eli Cohene126ba92013-07-07 17:25:49 +03001874{
Eli Cohenb037c292017-01-03 23:55:26 +02001875 int fw_uars_per_page;
1876
1877 fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ? MLX5_UARS_IN_PAGE : 1;
1878
Yishai Hadas4ed131d2017-12-24 16:31:35 +02001879 return (pci_resource_start(dev->mdev->pdev, 0) >> PAGE_SHIFT) + uar_idx / fw_uars_per_page;
Eli Cohene126ba92013-07-07 17:25:49 +03001880}
1881
1882static int get_command(unsigned long offset)
1883{
1884 return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK;
1885}
1886
1887static int get_arg(unsigned long offset)
1888{
1889 return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1);
1890}
1891
1892static int get_index(unsigned long offset)
1893{
1894 return get_arg(offset);
1895}
1896
Yishai Hadas4ed131d2017-12-24 16:31:35 +02001897/* Index resides in an extra byte to enable larger values than 255 */
1898static int get_extended_index(unsigned long offset)
1899{
1900 return get_arg(offset) | ((offset >> 16) & 0xff) << 8;
1901}
1902
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001903static void mlx5_ib_vma_open(struct vm_area_struct *area)
1904{
1905 /* vma_open is called when a new VMA is created on top of our VMA. This
1906 * is done through either mremap flow or split_vma (usually due to
1907 * mlock, madvise, munmap, etc.) We do not support a clone of the VMA,
1908 * as this VMA is strongly hardware related. Therefore we set the
1909 * vm_ops of the newly created/cloned VMA to NULL, to prevent it from
1910 * calling us again and trying to do incorrect actions. We assume that
1911 * the original VMA size is exactly a single page, and therefore all
1912 * "splitting" operation will not happen to it.
1913 */
1914 area->vm_ops = NULL;
1915}
1916
1917static void mlx5_ib_vma_close(struct vm_area_struct *area)
1918{
1919 struct mlx5_ib_vma_private_data *mlx5_ib_vma_priv_data;
1920
1921 /* It's guaranteed that all VMAs opened on a FD are closed before the
1922 * file itself is closed, therefore no sync is needed with the regular
1923 * closing flow. (e.g. mlx5 ib_dealloc_ucontext)
1924 * However need a sync with accessing the vma as part of
1925 * mlx5_ib_disassociate_ucontext.
1926 * The close operation is usually called under mm->mmap_sem except when
1927 * process is exiting.
1928 * The exiting case is handled explicitly as part of
1929 * mlx5_ib_disassociate_ucontext.
1930 */
1931 mlx5_ib_vma_priv_data = (struct mlx5_ib_vma_private_data *)area->vm_private_data;
1932
1933 /* setting the vma context pointer to null in the mlx5_ib driver's
1934 * private data, to protect a race condition in
1935 * mlx5_ib_disassociate_ucontext().
1936 */
1937 mlx5_ib_vma_priv_data->vma = NULL;
Majd Dibbinyad9a3662017-12-24 13:54:56 +02001938 mutex_lock(mlx5_ib_vma_priv_data->vma_private_list_mutex);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001939 list_del(&mlx5_ib_vma_priv_data->list);
Majd Dibbinyad9a3662017-12-24 13:54:56 +02001940 mutex_unlock(mlx5_ib_vma_priv_data->vma_private_list_mutex);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001941 kfree(mlx5_ib_vma_priv_data);
1942}
1943
1944static const struct vm_operations_struct mlx5_ib_vm_ops = {
1945 .open = mlx5_ib_vma_open,
1946 .close = mlx5_ib_vma_close
1947};
1948
1949static int mlx5_ib_set_vma_data(struct vm_area_struct *vma,
1950 struct mlx5_ib_ucontext *ctx)
1951{
1952 struct mlx5_ib_vma_private_data *vma_prv;
1953 struct list_head *vma_head = &ctx->vma_private_list;
1954
1955 vma_prv = kzalloc(sizeof(*vma_prv), GFP_KERNEL);
1956 if (!vma_prv)
1957 return -ENOMEM;
1958
1959 vma_prv->vma = vma;
Majd Dibbinyad9a3662017-12-24 13:54:56 +02001960 vma_prv->vma_private_list_mutex = &ctx->vma_private_list_mutex;
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001961 vma->vm_private_data = vma_prv;
1962 vma->vm_ops = &mlx5_ib_vm_ops;
1963
Majd Dibbinyad9a3662017-12-24 13:54:56 +02001964 mutex_lock(&ctx->vma_private_list_mutex);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001965 list_add(&vma_prv->list, vma_head);
Majd Dibbinyad9a3662017-12-24 13:54:56 +02001966 mutex_unlock(&ctx->vma_private_list_mutex);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001967
1968 return 0;
1969}
1970
1971static void mlx5_ib_disassociate_ucontext(struct ib_ucontext *ibcontext)
1972{
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001973 struct vm_area_struct *vma;
1974 struct mlx5_ib_vma_private_data *vma_private, *n;
1975 struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001976
Majd Dibbinyad9a3662017-12-24 13:54:56 +02001977 mutex_lock(&context->vma_private_list_mutex);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001978 list_for_each_entry_safe(vma_private, n, &context->vma_private_list,
1979 list) {
1980 vma = vma_private->vma;
Leon Romanovsky2cb40792018-05-29 15:14:05 +03001981 zap_vma_ptes(vma, vma->vm_start, PAGE_SIZE);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001982 /* context going to be destroyed, should
1983 * not access ops any more.
1984 */
Maor Gottlieb13776612017-03-29 06:03:03 +03001985 vma->vm_flags &= ~(VM_SHARED | VM_MAYSHARE);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001986 vma->vm_ops = NULL;
1987 list_del(&vma_private->list);
1988 kfree(vma_private);
1989 }
Majd Dibbinyad9a3662017-12-24 13:54:56 +02001990 mutex_unlock(&context->vma_private_list_mutex);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001991}
1992
Guy Levi37aa5c32016-04-27 16:49:50 +03001993static inline char *mmap_cmd2str(enum mlx5_ib_mmap_cmd cmd)
1994{
1995 switch (cmd) {
1996 case MLX5_IB_MMAP_WC_PAGE:
1997 return "WC";
1998 case MLX5_IB_MMAP_REGULAR_PAGE:
1999 return "best effort WC";
2000 case MLX5_IB_MMAP_NC_PAGE:
2001 return "NC";
Ariel Levkovich24da0012018-04-05 18:53:27 +03002002 case MLX5_IB_MMAP_DEVICE_MEM:
2003 return "Device Memory";
Guy Levi37aa5c32016-04-27 16:49:50 +03002004 default:
2005 return NULL;
2006 }
2007}
2008
Feras Daoud5c99eae2018-01-16 20:08:41 +02002009static int mlx5_ib_mmap_clock_info_page(struct mlx5_ib_dev *dev,
2010 struct vm_area_struct *vma,
2011 struct mlx5_ib_ucontext *context)
2012{
2013 phys_addr_t pfn;
2014 int err;
2015
2016 if (vma->vm_end - vma->vm_start != PAGE_SIZE)
2017 return -EINVAL;
2018
2019 if (get_index(vma->vm_pgoff) != MLX5_IB_CLOCK_INFO_V1)
2020 return -EOPNOTSUPP;
2021
2022 if (vma->vm_flags & VM_WRITE)
2023 return -EPERM;
2024
2025 if (!dev->mdev->clock_info_page)
2026 return -EOPNOTSUPP;
2027
2028 pfn = page_to_pfn(dev->mdev->clock_info_page);
2029 err = remap_pfn_range(vma, vma->vm_start, pfn, PAGE_SIZE,
2030 vma->vm_page_prot);
2031 if (err)
2032 return err;
2033
Feras Daoud5c99eae2018-01-16 20:08:41 +02002034 return mlx5_ib_set_vma_data(vma, context);
2035}
2036
Guy Levi37aa5c32016-04-27 16:49:50 +03002037static int uar_mmap(struct mlx5_ib_dev *dev, enum mlx5_ib_mmap_cmd cmd,
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03002038 struct vm_area_struct *vma,
2039 struct mlx5_ib_ucontext *context)
Guy Levi37aa5c32016-04-27 16:49:50 +03002040{
Eli Cohen2f5ff262017-01-03 23:55:21 +02002041 struct mlx5_bfreg_info *bfregi = &context->bfregi;
Guy Levi37aa5c32016-04-27 16:49:50 +03002042 int err;
2043 unsigned long idx;
Kamal Heibaa09ea62018-07-19 00:05:32 +03002044 phys_addr_t pfn;
Guy Levi37aa5c32016-04-27 16:49:50 +03002045 pgprot_t prot;
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002046 u32 bfreg_dyn_idx = 0;
2047 u32 uar_index;
2048 int dyn_uar = (cmd == MLX5_IB_MMAP_ALLOC_WC);
2049 int max_valid_idx = dyn_uar ? bfregi->num_sys_pages :
2050 bfregi->num_static_sys_pages;
Eli Cohenb037c292017-01-03 23:55:26 +02002051
2052 if (vma->vm_end - vma->vm_start != PAGE_SIZE)
2053 return -EINVAL;
2054
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002055 if (dyn_uar)
2056 idx = get_extended_index(vma->vm_pgoff) + bfregi->num_static_sys_pages;
2057 else
2058 idx = get_index(vma->vm_pgoff);
2059
2060 if (idx >= max_valid_idx) {
2061 mlx5_ib_warn(dev, "invalid uar index %lu, max=%d\n",
2062 idx, max_valid_idx);
Eli Cohenb037c292017-01-03 23:55:26 +02002063 return -EINVAL;
2064 }
Guy Levi37aa5c32016-04-27 16:49:50 +03002065
2066 switch (cmd) {
2067 case MLX5_IB_MMAP_WC_PAGE:
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002068 case MLX5_IB_MMAP_ALLOC_WC:
Guy Levi37aa5c32016-04-27 16:49:50 +03002069/* Some architectures don't support WC memory */
2070#if defined(CONFIG_X86)
2071 if (!pat_enabled())
2072 return -EPERM;
2073#elif !(defined(CONFIG_PPC) || (defined(CONFIG_ARM) && defined(CONFIG_MMU)))
2074 return -EPERM;
2075#endif
2076 /* fall through */
2077 case MLX5_IB_MMAP_REGULAR_PAGE:
2078 /* For MLX5_IB_MMAP_REGULAR_PAGE do the best effort to get WC */
2079 prot = pgprot_writecombine(vma->vm_page_prot);
2080 break;
2081 case MLX5_IB_MMAP_NC_PAGE:
2082 prot = pgprot_noncached(vma->vm_page_prot);
2083 break;
2084 default:
2085 return -EINVAL;
2086 }
2087
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002088 if (dyn_uar) {
2089 int uars_per_page;
2090
2091 uars_per_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k);
2092 bfreg_dyn_idx = idx * (uars_per_page * MLX5_NON_FP_BFREGS_PER_UAR);
2093 if (bfreg_dyn_idx >= bfregi->total_num_bfregs) {
2094 mlx5_ib_warn(dev, "invalid bfreg_dyn_idx %u, max=%u\n",
2095 bfreg_dyn_idx, bfregi->total_num_bfregs);
2096 return -EINVAL;
2097 }
2098
2099 mutex_lock(&bfregi->lock);
2100 /* Fail if uar already allocated, first bfreg index of each
2101 * page holds its count.
2102 */
2103 if (bfregi->count[bfreg_dyn_idx]) {
2104 mlx5_ib_warn(dev, "wrong offset, idx %lu is busy, bfregn=%u\n", idx, bfreg_dyn_idx);
2105 mutex_unlock(&bfregi->lock);
2106 return -EINVAL;
2107 }
2108
2109 bfregi->count[bfreg_dyn_idx]++;
2110 mutex_unlock(&bfregi->lock);
2111
2112 err = mlx5_cmd_alloc_uar(dev->mdev, &uar_index);
2113 if (err) {
2114 mlx5_ib_warn(dev, "UAR alloc failed\n");
2115 goto free_bfreg;
2116 }
2117 } else {
2118 uar_index = bfregi->sys_pages[idx];
2119 }
2120
2121 pfn = uar_index2pfn(dev, uar_index);
Guy Levi37aa5c32016-04-27 16:49:50 +03002122 mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn %pa\n", idx, &pfn);
2123
2124 vma->vm_page_prot = prot;
2125 err = io_remap_pfn_range(vma, vma->vm_start, pfn,
2126 PAGE_SIZE, vma->vm_page_prot);
2127 if (err) {
Leon Romanovsky8f062282018-05-22 08:31:03 +03002128 mlx5_ib_err(dev,
2129 "io_remap_pfn_range failed with error=%d, mmap_cmd=%s\n",
2130 err, mmap_cmd2str(cmd));
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002131 err = -EAGAIN;
2132 goto err;
Guy Levi37aa5c32016-04-27 16:49:50 +03002133 }
2134
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002135 err = mlx5_ib_set_vma_data(vma, context);
2136 if (err)
2137 goto err;
2138
2139 if (dyn_uar)
2140 bfregi->sys_pages[idx] = uar_index;
2141 return 0;
2142
2143err:
2144 if (!dyn_uar)
2145 return err;
2146
2147 mlx5_cmd_free_uar(dev->mdev, idx);
2148
2149free_bfreg:
2150 mlx5_ib_free_bfreg(dev, bfregi, bfreg_dyn_idx);
2151
2152 return err;
Guy Levi37aa5c32016-04-27 16:49:50 +03002153}
2154
Ariel Levkovich24da0012018-04-05 18:53:27 +03002155static int dm_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
2156{
2157 struct mlx5_ib_ucontext *mctx = to_mucontext(context);
2158 struct mlx5_ib_dev *dev = to_mdev(context->device);
2159 u16 page_idx = get_extended_index(vma->vm_pgoff);
2160 size_t map_size = vma->vm_end - vma->vm_start;
2161 u32 npages = map_size >> PAGE_SHIFT;
2162 phys_addr_t pfn;
2163 pgprot_t prot;
2164
2165 if (find_next_zero_bit(mctx->dm_pages, page_idx + npages, page_idx) !=
2166 page_idx + npages)
2167 return -EINVAL;
2168
2169 pfn = ((pci_resource_start(dev->mdev->pdev, 0) +
2170 MLX5_CAP64_DEV_MEM(dev->mdev, memic_bar_start_addr)) >>
2171 PAGE_SHIFT) +
2172 page_idx;
2173 prot = pgprot_writecombine(vma->vm_page_prot);
2174 vma->vm_page_prot = prot;
2175
2176 if (io_remap_pfn_range(vma, vma->vm_start, pfn, map_size,
2177 vma->vm_page_prot))
2178 return -EAGAIN;
2179
2180 return mlx5_ib_set_vma_data(vma, mctx);
2181}
2182
Eli Cohene126ba92013-07-07 17:25:49 +03002183static int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma)
2184{
2185 struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
2186 struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
Eli Cohene126ba92013-07-07 17:25:49 +03002187 unsigned long command;
Eli Cohene126ba92013-07-07 17:25:49 +03002188 phys_addr_t pfn;
2189
2190 command = get_command(vma->vm_pgoff);
2191 switch (command) {
Guy Levi37aa5c32016-04-27 16:49:50 +03002192 case MLX5_IB_MMAP_WC_PAGE:
2193 case MLX5_IB_MMAP_NC_PAGE:
Eli Cohene126ba92013-07-07 17:25:49 +03002194 case MLX5_IB_MMAP_REGULAR_PAGE:
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002195 case MLX5_IB_MMAP_ALLOC_WC:
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03002196 return uar_mmap(dev, command, vma, context);
Eli Cohene126ba92013-07-07 17:25:49 +03002197
2198 case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES:
2199 return -ENOSYS;
2200
Matan Barakd69e3bc2015-12-15 20:30:13 +02002201 case MLX5_IB_MMAP_CORE_CLOCK:
Matan Barakd69e3bc2015-12-15 20:30:13 +02002202 if (vma->vm_end - vma->vm_start != PAGE_SIZE)
2203 return -EINVAL;
2204
Matan Barak6cbac1e2016-04-14 16:52:10 +03002205 if (vma->vm_flags & VM_WRITE)
Matan Barakd69e3bc2015-12-15 20:30:13 +02002206 return -EPERM;
2207
2208 /* Don't expose to user-space information it shouldn't have */
2209 if (PAGE_SIZE > 4096)
2210 return -EOPNOTSUPP;
2211
2212 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
2213 pfn = (dev->mdev->iseg_base +
2214 offsetof(struct mlx5_init_seg, internal_timer_h)) >>
2215 PAGE_SHIFT;
2216 if (io_remap_pfn_range(vma, vma->vm_start, pfn,
2217 PAGE_SIZE, vma->vm_page_prot))
2218 return -EAGAIN;
Matan Barakd69e3bc2015-12-15 20:30:13 +02002219 break;
Feras Daoud5c99eae2018-01-16 20:08:41 +02002220 case MLX5_IB_MMAP_CLOCK_INFO:
2221 return mlx5_ib_mmap_clock_info_page(dev, vma, context);
Matan Barakd69e3bc2015-12-15 20:30:13 +02002222
Ariel Levkovich24da0012018-04-05 18:53:27 +03002223 case MLX5_IB_MMAP_DEVICE_MEM:
2224 return dm_mmap(ibcontext, vma);
2225
Eli Cohene126ba92013-07-07 17:25:49 +03002226 default:
2227 return -EINVAL;
2228 }
2229
2230 return 0;
2231}
2232
Ariel Levkovich24da0012018-04-05 18:53:27 +03002233struct ib_dm *mlx5_ib_alloc_dm(struct ib_device *ibdev,
2234 struct ib_ucontext *context,
2235 struct ib_dm_alloc_attr *attr,
2236 struct uverbs_attr_bundle *attrs)
2237{
2238 u64 act_size = roundup(attr->length, MLX5_MEMIC_BASE_SIZE);
2239 struct mlx5_memic *memic = &to_mdev(ibdev)->memic;
2240 phys_addr_t memic_addr;
2241 struct mlx5_ib_dm *dm;
2242 u64 start_offset;
2243 u32 page_idx;
2244 int err;
2245
2246 dm = kzalloc(sizeof(*dm), GFP_KERNEL);
2247 if (!dm)
2248 return ERR_PTR(-ENOMEM);
2249
2250 mlx5_ib_dbg(to_mdev(ibdev), "alloc_memic req: user_length=0x%llx act_length=0x%llx log_alignment=%d\n",
2251 attr->length, act_size, attr->alignment);
2252
2253 err = mlx5_cmd_alloc_memic(memic, &memic_addr,
2254 act_size, attr->alignment);
2255 if (err)
2256 goto err_free;
2257
2258 start_offset = memic_addr & ~PAGE_MASK;
2259 page_idx = (memic_addr - pci_resource_start(memic->dev->pdev, 0) -
2260 MLX5_CAP64_DEV_MEM(memic->dev, memic_bar_start_addr)) >>
2261 PAGE_SHIFT;
2262
2263 err = uverbs_copy_to(attrs,
2264 MLX5_IB_ATTR_ALLOC_DM_RESP_START_OFFSET,
2265 &start_offset, sizeof(start_offset));
2266 if (err)
2267 goto err_dealloc;
2268
2269 err = uverbs_copy_to(attrs,
2270 MLX5_IB_ATTR_ALLOC_DM_RESP_PAGE_INDEX,
2271 &page_idx, sizeof(page_idx));
2272 if (err)
2273 goto err_dealloc;
2274
2275 bitmap_set(to_mucontext(context)->dm_pages, page_idx,
2276 DIV_ROUND_UP(act_size, PAGE_SIZE));
2277
2278 dm->dev_addr = memic_addr;
2279
2280 return &dm->ibdm;
2281
2282err_dealloc:
2283 mlx5_cmd_dealloc_memic(memic, memic_addr,
2284 act_size);
2285err_free:
2286 kfree(dm);
2287 return ERR_PTR(err);
2288}
2289
2290int mlx5_ib_dealloc_dm(struct ib_dm *ibdm)
2291{
2292 struct mlx5_memic *memic = &to_mdev(ibdm->device)->memic;
2293 struct mlx5_ib_dm *dm = to_mdm(ibdm);
2294 u64 act_size = roundup(dm->ibdm.length, MLX5_MEMIC_BASE_SIZE);
2295 u32 page_idx;
2296 int ret;
2297
2298 ret = mlx5_cmd_dealloc_memic(memic, dm->dev_addr, act_size);
2299 if (ret)
2300 return ret;
2301
2302 page_idx = (dm->dev_addr - pci_resource_start(memic->dev->pdev, 0) -
2303 MLX5_CAP64_DEV_MEM(memic->dev, memic_bar_start_addr)) >>
2304 PAGE_SHIFT;
2305 bitmap_clear(to_mucontext(ibdm->uobject->context)->dm_pages,
2306 page_idx,
2307 DIV_ROUND_UP(act_size, PAGE_SIZE));
2308
2309 kfree(dm);
2310
2311 return 0;
2312}
2313
Eli Cohene126ba92013-07-07 17:25:49 +03002314static struct ib_pd *mlx5_ib_alloc_pd(struct ib_device *ibdev,
2315 struct ib_ucontext *context,
2316 struct ib_udata *udata)
2317{
2318 struct mlx5_ib_alloc_pd_resp resp;
2319 struct mlx5_ib_pd *pd;
2320 int err;
2321
2322 pd = kmalloc(sizeof(*pd), GFP_KERNEL);
2323 if (!pd)
2324 return ERR_PTR(-ENOMEM);
2325
Jack Morgenstein9603b612014-07-28 23:30:22 +03002326 err = mlx5_core_alloc_pd(to_mdev(ibdev)->mdev, &pd->pdn);
Eli Cohene126ba92013-07-07 17:25:49 +03002327 if (err) {
2328 kfree(pd);
2329 return ERR_PTR(err);
2330 }
2331
2332 if (context) {
2333 resp.pdn = pd->pdn;
2334 if (ib_copy_to_udata(udata, &resp, sizeof(resp))) {
Jack Morgenstein9603b612014-07-28 23:30:22 +03002335 mlx5_core_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn);
Eli Cohene126ba92013-07-07 17:25:49 +03002336 kfree(pd);
2337 return ERR_PTR(-EFAULT);
2338 }
Eli Cohene126ba92013-07-07 17:25:49 +03002339 }
2340
2341 return &pd->ibpd;
2342}
2343
2344static int mlx5_ib_dealloc_pd(struct ib_pd *pd)
2345{
2346 struct mlx5_ib_dev *mdev = to_mdev(pd->device);
2347 struct mlx5_ib_pd *mpd = to_mpd(pd);
2348
Jack Morgenstein9603b612014-07-28 23:30:22 +03002349 mlx5_core_dealloc_pd(mdev->mdev, mpd->pdn);
Eli Cohene126ba92013-07-07 17:25:49 +03002350 kfree(mpd);
2351
2352 return 0;
2353}
2354
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002355enum {
2356 MATCH_CRITERIA_ENABLE_OUTER_BIT,
2357 MATCH_CRITERIA_ENABLE_MISC_BIT,
Ariel Levkovich71c6e862018-05-13 14:33:34 +03002358 MATCH_CRITERIA_ENABLE_INNER_BIT,
2359 MATCH_CRITERIA_ENABLE_MISC2_BIT
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002360};
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002361
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002362#define HEADER_IS_ZERO(match_criteria, headers) \
2363 !(memchr_inv(MLX5_ADDR_OF(fte_match_param, match_criteria, headers), \
2364 0, MLX5_FLD_SZ_BYTES(fte_match_param, headers))) \
2365
2366static u8 get_match_criteria_enable(u32 *match_criteria)
2367{
2368 u8 match_criteria_enable;
2369
2370 match_criteria_enable =
2371 (!HEADER_IS_ZERO(match_criteria, outer_headers)) <<
2372 MATCH_CRITERIA_ENABLE_OUTER_BIT;
2373 match_criteria_enable |=
2374 (!HEADER_IS_ZERO(match_criteria, misc_parameters)) <<
2375 MATCH_CRITERIA_ENABLE_MISC_BIT;
2376 match_criteria_enable |=
2377 (!HEADER_IS_ZERO(match_criteria, inner_headers)) <<
2378 MATCH_CRITERIA_ENABLE_INNER_BIT;
Ariel Levkovich71c6e862018-05-13 14:33:34 +03002379 match_criteria_enable |=
2380 (!HEADER_IS_ZERO(match_criteria, misc_parameters_2)) <<
2381 MATCH_CRITERIA_ENABLE_MISC2_BIT;
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002382
2383 return match_criteria_enable;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002384}
2385
Maor Gottliebca0d4752016-08-30 16:58:35 +03002386static void set_proto(void *outer_c, void *outer_v, u8 mask, u8 val)
2387{
2388 MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_protocol, mask);
2389 MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_protocol, val);
2390}
2391
Daria Velikovsky37da2a02018-05-07 10:20:02 +03002392static void set_flow_label(void *misc_c, void *misc_v, u32 mask, u32 val,
Moses Reuben2d1e6972016-11-14 19:04:52 +02002393 bool inner)
2394{
2395 if (inner) {
2396 MLX5_SET(fte_match_set_misc,
2397 misc_c, inner_ipv6_flow_label, mask);
2398 MLX5_SET(fte_match_set_misc,
2399 misc_v, inner_ipv6_flow_label, val);
2400 } else {
2401 MLX5_SET(fte_match_set_misc,
2402 misc_c, outer_ipv6_flow_label, mask);
2403 MLX5_SET(fte_match_set_misc,
2404 misc_v, outer_ipv6_flow_label, val);
2405 }
2406}
2407
Maor Gottliebca0d4752016-08-30 16:58:35 +03002408static void set_tos(void *outer_c, void *outer_v, u8 mask, u8 val)
2409{
2410 MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_ecn, mask);
2411 MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_ecn, val);
2412 MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_dscp, mask >> 2);
2413 MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_dscp, val >> 2);
2414}
2415
Ariel Levkovich71c6e862018-05-13 14:33:34 +03002416static int check_mpls_supp_fields(u32 field_support, const __be32 *set_mask)
2417{
2418 if (MLX5_GET(fte_match_mpls, set_mask, mpls_label) &&
2419 !(field_support & MLX5_FIELD_SUPPORT_MPLS_LABEL))
2420 return -EOPNOTSUPP;
2421
2422 if (MLX5_GET(fte_match_mpls, set_mask, mpls_exp) &&
2423 !(field_support & MLX5_FIELD_SUPPORT_MPLS_EXP))
2424 return -EOPNOTSUPP;
2425
2426 if (MLX5_GET(fte_match_mpls, set_mask, mpls_s_bos) &&
2427 !(field_support & MLX5_FIELD_SUPPORT_MPLS_S_BOS))
2428 return -EOPNOTSUPP;
2429
2430 if (MLX5_GET(fte_match_mpls, set_mask, mpls_ttl) &&
2431 !(field_support & MLX5_FIELD_SUPPORT_MPLS_TTL))
2432 return -EOPNOTSUPP;
2433
2434 return 0;
2435}
2436
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002437#define LAST_ETH_FIELD vlan_tag
2438#define LAST_IB_FIELD sl
Maor Gottliebca0d4752016-08-30 16:58:35 +03002439#define LAST_IPV4_FIELD tos
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002440#define LAST_IPV6_FIELD traffic_class
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002441#define LAST_TCP_UDP_FIELD src_port
Moses Reubenffb30d82016-11-14 19:04:50 +02002442#define LAST_TUNNEL_FIELD tunnel_id
Moses Reuben2ac693f2017-01-18 14:59:50 +02002443#define LAST_FLOW_TAG_FIELD tag_id
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002444#define LAST_DROP_FIELD size
Raed Salem3b3233f2018-05-31 16:43:39 +03002445#define LAST_COUNTERS_FIELD counters
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002446
2447/* Field is the last supported field */
2448#define FIELDS_NOT_SUPPORTED(filter, field)\
2449 memchr_inv((void *)&filter.field +\
2450 sizeof(filter.field), 0,\
2451 sizeof(filter) -\
2452 offsetof(typeof(filter), field) -\
2453 sizeof(filter.field))
2454
Aviad Yehezkel802c2122018-03-28 09:27:53 +03002455static int parse_flow_flow_action(const union ib_flow_spec *ib_spec,
2456 const struct ib_flow_attr *flow_attr,
2457 struct mlx5_flow_act *action)
2458{
2459 struct mlx5_ib_flow_action *maction = to_mflow_act(ib_spec->action.act);
2460
2461 switch (maction->ib_action.type) {
2462 case IB_FLOW_ACTION_ESP:
2463 /* Currently only AES_GCM keymat is supported by the driver */
2464 action->esp_id = (uintptr_t)maction->esp_aes_gcm.ctx;
2465 action->action |= flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS ?
2466 MLX5_FLOW_CONTEXT_ACTION_ENCRYPT :
2467 MLX5_FLOW_CONTEXT_ACTION_DECRYPT;
2468 return 0;
2469 default:
2470 return -EOPNOTSUPP;
2471 }
2472}
2473
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002474static int parse_flow_attr(struct mlx5_core_dev *mdev, u32 *match_c,
2475 u32 *match_v, const union ib_flow_spec *ib_spec,
Aviad Yehezkel802c2122018-03-28 09:27:53 +03002476 const struct ib_flow_attr *flow_attr,
Ariel Levkovich71c6e862018-05-13 14:33:34 +03002477 struct mlx5_flow_act *action, u32 prev_type)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002478{
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002479 void *misc_params_c = MLX5_ADDR_OF(fte_match_param, match_c,
2480 misc_parameters);
2481 void *misc_params_v = MLX5_ADDR_OF(fte_match_param, match_v,
2482 misc_parameters);
Ariel Levkovich71c6e862018-05-13 14:33:34 +03002483 void *misc_params2_c = MLX5_ADDR_OF(fte_match_param, match_c,
2484 misc_parameters_2);
2485 void *misc_params2_v = MLX5_ADDR_OF(fte_match_param, match_v,
2486 misc_parameters_2);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002487 void *headers_c;
2488 void *headers_v;
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002489 int match_ipv;
Aviad Yehezkel802c2122018-03-28 09:27:53 +03002490 int ret;
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002491
Moses Reuben2d1e6972016-11-14 19:04:52 +02002492 if (ib_spec->type & IB_FLOW_SPEC_INNER) {
2493 headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
2494 inner_headers);
2495 headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
2496 inner_headers);
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002497 match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2498 ft_field_support.inner_ip_version);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002499 } else {
2500 headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
2501 outer_headers);
2502 headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
2503 outer_headers);
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002504 match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2505 ft_field_support.outer_ip_version);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002506 }
2507
2508 switch (ib_spec->type & ~IB_FLOW_SPEC_INNER) {
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002509 case IB_FLOW_SPEC_ETH:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002510 if (FIELDS_NOT_SUPPORTED(ib_spec->eth.mask, LAST_ETH_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002511 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002512
Moses Reuben2d1e6972016-11-14 19:04:52 +02002513 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002514 dmac_47_16),
2515 ib_spec->eth.mask.dst_mac);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002516 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002517 dmac_47_16),
2518 ib_spec->eth.val.dst_mac);
2519
Moses Reuben2d1e6972016-11-14 19:04:52 +02002520 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottliebee3da802016-09-12 19:16:24 +03002521 smac_47_16),
2522 ib_spec->eth.mask.src_mac);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002523 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottliebee3da802016-09-12 19:16:24 +03002524 smac_47_16),
2525 ib_spec->eth.val.src_mac);
2526
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002527 if (ib_spec->eth.mask.vlan_tag) {
Moses Reuben2d1e6972016-11-14 19:04:52 +02002528 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Mohamad Haj Yahia10543362016-10-09 16:25:43 +03002529 cvlan_tag, 1);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002530 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Mohamad Haj Yahia10543362016-10-09 16:25:43 +03002531 cvlan_tag, 1);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002532
Moses Reuben2d1e6972016-11-14 19:04:52 +02002533 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002534 first_vid, ntohs(ib_spec->eth.mask.vlan_tag));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002535 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002536 first_vid, ntohs(ib_spec->eth.val.vlan_tag));
2537
Moses Reuben2d1e6972016-11-14 19:04:52 +02002538 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002539 first_cfi,
2540 ntohs(ib_spec->eth.mask.vlan_tag) >> 12);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002541 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002542 first_cfi,
2543 ntohs(ib_spec->eth.val.vlan_tag) >> 12);
2544
Moses Reuben2d1e6972016-11-14 19:04:52 +02002545 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002546 first_prio,
2547 ntohs(ib_spec->eth.mask.vlan_tag) >> 13);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002548 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002549 first_prio,
2550 ntohs(ib_spec->eth.val.vlan_tag) >> 13);
2551 }
Moses Reuben2d1e6972016-11-14 19:04:52 +02002552 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002553 ethertype, ntohs(ib_spec->eth.mask.ether_type));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002554 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002555 ethertype, ntohs(ib_spec->eth.val.ether_type));
2556 break;
2557 case IB_FLOW_SPEC_IPV4:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002558 if (FIELDS_NOT_SUPPORTED(ib_spec->ipv4.mask, LAST_IPV4_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002559 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002560
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002561 if (match_ipv) {
2562 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2563 ip_version, 0xf);
2564 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Boris Pismenny3346c482017-08-20 15:13:08 +03002565 ip_version, MLX5_FS_IPV4_VERSION);
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002566 } else {
2567 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2568 ethertype, 0xffff);
2569 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2570 ethertype, ETH_P_IP);
2571 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002572
Moses Reuben2d1e6972016-11-14 19:04:52 +02002573 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002574 src_ipv4_src_ipv6.ipv4_layout.ipv4),
2575 &ib_spec->ipv4.mask.src_ip,
2576 sizeof(ib_spec->ipv4.mask.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002577 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002578 src_ipv4_src_ipv6.ipv4_layout.ipv4),
2579 &ib_spec->ipv4.val.src_ip,
2580 sizeof(ib_spec->ipv4.val.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002581 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002582 dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2583 &ib_spec->ipv4.mask.dst_ip,
2584 sizeof(ib_spec->ipv4.mask.dst_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002585 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002586 dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2587 &ib_spec->ipv4.val.dst_ip,
2588 sizeof(ib_spec->ipv4.val.dst_ip));
Maor Gottliebca0d4752016-08-30 16:58:35 +03002589
Moses Reuben2d1e6972016-11-14 19:04:52 +02002590 set_tos(headers_c, headers_v,
Maor Gottliebca0d4752016-08-30 16:58:35 +03002591 ib_spec->ipv4.mask.tos, ib_spec->ipv4.val.tos);
2592
Moses Reuben2d1e6972016-11-14 19:04:52 +02002593 set_proto(headers_c, headers_v,
Maor Gottliebca0d4752016-08-30 16:58:35 +03002594 ib_spec->ipv4.mask.proto, ib_spec->ipv4.val.proto);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002595 break;
Maor Gottlieb026bae02016-06-17 15:14:51 +03002596 case IB_FLOW_SPEC_IPV6:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002597 if (FIELDS_NOT_SUPPORTED(ib_spec->ipv6.mask, LAST_IPV6_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002598 return -EOPNOTSUPP;
Maor Gottlieb026bae02016-06-17 15:14:51 +03002599
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002600 if (match_ipv) {
2601 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2602 ip_version, 0xf);
2603 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Boris Pismenny3346c482017-08-20 15:13:08 +03002604 ip_version, MLX5_FS_IPV6_VERSION);
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002605 } else {
2606 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2607 ethertype, 0xffff);
2608 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2609 ethertype, ETH_P_IPV6);
2610 }
Maor Gottlieb026bae02016-06-17 15:14:51 +03002611
Moses Reuben2d1e6972016-11-14 19:04:52 +02002612 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb026bae02016-06-17 15:14:51 +03002613 src_ipv4_src_ipv6.ipv6_layout.ipv6),
2614 &ib_spec->ipv6.mask.src_ip,
2615 sizeof(ib_spec->ipv6.mask.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002616 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb026bae02016-06-17 15:14:51 +03002617 src_ipv4_src_ipv6.ipv6_layout.ipv6),
2618 &ib_spec->ipv6.val.src_ip,
2619 sizeof(ib_spec->ipv6.val.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002620 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb026bae02016-06-17 15:14:51 +03002621 dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2622 &ib_spec->ipv6.mask.dst_ip,
2623 sizeof(ib_spec->ipv6.mask.dst_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002624 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb026bae02016-06-17 15:14:51 +03002625 dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2626 &ib_spec->ipv6.val.dst_ip,
2627 sizeof(ib_spec->ipv6.val.dst_ip));
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002628
Moses Reuben2d1e6972016-11-14 19:04:52 +02002629 set_tos(headers_c, headers_v,
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002630 ib_spec->ipv6.mask.traffic_class,
2631 ib_spec->ipv6.val.traffic_class);
2632
Moses Reuben2d1e6972016-11-14 19:04:52 +02002633 set_proto(headers_c, headers_v,
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002634 ib_spec->ipv6.mask.next_hdr,
2635 ib_spec->ipv6.val.next_hdr);
2636
Moses Reuben2d1e6972016-11-14 19:04:52 +02002637 set_flow_label(misc_params_c, misc_params_v,
2638 ntohl(ib_spec->ipv6.mask.flow_label),
2639 ntohl(ib_spec->ipv6.val.flow_label),
2640 ib_spec->type & IB_FLOW_SPEC_INNER);
Aviad Yehezkel802c2122018-03-28 09:27:53 +03002641 break;
2642 case IB_FLOW_SPEC_ESP:
2643 if (ib_spec->esp.mask.seq)
2644 return -EOPNOTSUPP;
Moses Reuben2d1e6972016-11-14 19:04:52 +02002645
Aviad Yehezkel802c2122018-03-28 09:27:53 +03002646 MLX5_SET(fte_match_set_misc, misc_params_c, outer_esp_spi,
2647 ntohl(ib_spec->esp.mask.spi));
2648 MLX5_SET(fte_match_set_misc, misc_params_v, outer_esp_spi,
2649 ntohl(ib_spec->esp.val.spi));
Maor Gottlieb026bae02016-06-17 15:14:51 +03002650 break;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002651 case IB_FLOW_SPEC_TCP:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002652 if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
2653 LAST_TCP_UDP_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002654 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002655
Moses Reuben2d1e6972016-11-14 19:04:52 +02002656 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002657 0xff);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002658 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002659 IPPROTO_TCP);
2660
Moses Reuben2d1e6972016-11-14 19:04:52 +02002661 MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002662 ntohs(ib_spec->tcp_udp.mask.src_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002663 MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002664 ntohs(ib_spec->tcp_udp.val.src_port));
2665
Moses Reuben2d1e6972016-11-14 19:04:52 +02002666 MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002667 ntohs(ib_spec->tcp_udp.mask.dst_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002668 MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002669 ntohs(ib_spec->tcp_udp.val.dst_port));
2670 break;
2671 case IB_FLOW_SPEC_UDP:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002672 if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
2673 LAST_TCP_UDP_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002674 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002675
Moses Reuben2d1e6972016-11-14 19:04:52 +02002676 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002677 0xff);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002678 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002679 IPPROTO_UDP);
2680
Moses Reuben2d1e6972016-11-14 19:04:52 +02002681 MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002682 ntohs(ib_spec->tcp_udp.mask.src_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002683 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002684 ntohs(ib_spec->tcp_udp.val.src_port));
2685
Moses Reuben2d1e6972016-11-14 19:04:52 +02002686 MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002687 ntohs(ib_spec->tcp_udp.mask.dst_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002688 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002689 ntohs(ib_spec->tcp_udp.val.dst_port));
2690 break;
Ariel Levkovichda2f22a2018-05-13 14:33:33 +03002691 case IB_FLOW_SPEC_GRE:
2692 if (ib_spec->gre.mask.c_ks_res0_ver)
2693 return -EOPNOTSUPP;
2694
2695 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
2696 0xff);
2697 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
2698 IPPROTO_GRE);
2699
2700 MLX5_SET(fte_match_set_misc, misc_params_c, gre_protocol,
Maor Gottlieba93b6322018-07-01 15:50:17 +03002701 ntohs(ib_spec->gre.mask.protocol));
Ariel Levkovichda2f22a2018-05-13 14:33:33 +03002702 MLX5_SET(fte_match_set_misc, misc_params_v, gre_protocol,
2703 ntohs(ib_spec->gre.val.protocol));
2704
2705 memcpy(MLX5_ADDR_OF(fte_match_set_misc, misc_params_c,
2706 gre_key_h),
2707 &ib_spec->gre.mask.key,
2708 sizeof(ib_spec->gre.mask.key));
2709 memcpy(MLX5_ADDR_OF(fte_match_set_misc, misc_params_v,
2710 gre_key_h),
2711 &ib_spec->gre.val.key,
2712 sizeof(ib_spec->gre.val.key));
2713 break;
Ariel Levkovich71c6e862018-05-13 14:33:34 +03002714 case IB_FLOW_SPEC_MPLS:
2715 switch (prev_type) {
2716 case IB_FLOW_SPEC_UDP:
2717 if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2718 ft_field_support.outer_first_mpls_over_udp),
2719 &ib_spec->mpls.mask.tag))
2720 return -EOPNOTSUPP;
2721
2722 memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
2723 outer_first_mpls_over_udp),
2724 &ib_spec->mpls.val.tag,
2725 sizeof(ib_spec->mpls.val.tag));
2726 memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
2727 outer_first_mpls_over_udp),
2728 &ib_spec->mpls.mask.tag,
2729 sizeof(ib_spec->mpls.mask.tag));
2730 break;
2731 case IB_FLOW_SPEC_GRE:
2732 if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2733 ft_field_support.outer_first_mpls_over_gre),
2734 &ib_spec->mpls.mask.tag))
2735 return -EOPNOTSUPP;
2736
2737 memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
2738 outer_first_mpls_over_gre),
2739 &ib_spec->mpls.val.tag,
2740 sizeof(ib_spec->mpls.val.tag));
2741 memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
2742 outer_first_mpls_over_gre),
2743 &ib_spec->mpls.mask.tag,
2744 sizeof(ib_spec->mpls.mask.tag));
2745 break;
2746 default:
2747 if (ib_spec->type & IB_FLOW_SPEC_INNER) {
2748 if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2749 ft_field_support.inner_first_mpls),
2750 &ib_spec->mpls.mask.tag))
2751 return -EOPNOTSUPP;
2752
2753 memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
2754 inner_first_mpls),
2755 &ib_spec->mpls.val.tag,
2756 sizeof(ib_spec->mpls.val.tag));
2757 memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
2758 inner_first_mpls),
2759 &ib_spec->mpls.mask.tag,
2760 sizeof(ib_spec->mpls.mask.tag));
2761 } else {
2762 if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2763 ft_field_support.outer_first_mpls),
2764 &ib_spec->mpls.mask.tag))
2765 return -EOPNOTSUPP;
2766
2767 memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
2768 outer_first_mpls),
2769 &ib_spec->mpls.val.tag,
2770 sizeof(ib_spec->mpls.val.tag));
2771 memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
2772 outer_first_mpls),
2773 &ib_spec->mpls.mask.tag,
2774 sizeof(ib_spec->mpls.mask.tag));
2775 }
2776 }
2777 break;
Moses Reubenffb30d82016-11-14 19:04:50 +02002778 case IB_FLOW_SPEC_VXLAN_TUNNEL:
2779 if (FIELDS_NOT_SUPPORTED(ib_spec->tunnel.mask,
2780 LAST_TUNNEL_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002781 return -EOPNOTSUPP;
Moses Reubenffb30d82016-11-14 19:04:50 +02002782
2783 MLX5_SET(fte_match_set_misc, misc_params_c, vxlan_vni,
2784 ntohl(ib_spec->tunnel.mask.tunnel_id));
2785 MLX5_SET(fte_match_set_misc, misc_params_v, vxlan_vni,
2786 ntohl(ib_spec->tunnel.val.tunnel_id));
2787 break;
Moses Reuben2ac693f2017-01-18 14:59:50 +02002788 case IB_FLOW_SPEC_ACTION_TAG:
2789 if (FIELDS_NOT_SUPPORTED(ib_spec->flow_tag,
2790 LAST_FLOW_TAG_FIELD))
2791 return -EOPNOTSUPP;
2792 if (ib_spec->flow_tag.tag_id >= BIT(24))
2793 return -EINVAL;
2794
Boris Pismenny075572d2017-08-16 09:33:30 +03002795 action->flow_tag = ib_spec->flow_tag.tag_id;
Matan Baraka9db0ec2017-08-16 09:43:48 +03002796 action->has_flow_tag = true;
Moses Reuben2ac693f2017-01-18 14:59:50 +02002797 break;
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002798 case IB_FLOW_SPEC_ACTION_DROP:
2799 if (FIELDS_NOT_SUPPORTED(ib_spec->drop,
2800 LAST_DROP_FIELD))
2801 return -EOPNOTSUPP;
Boris Pismenny075572d2017-08-16 09:33:30 +03002802 action->action |= MLX5_FLOW_CONTEXT_ACTION_DROP;
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002803 break;
Aviad Yehezkel802c2122018-03-28 09:27:53 +03002804 case IB_FLOW_SPEC_ACTION_HANDLE:
2805 ret = parse_flow_flow_action(ib_spec, flow_attr, action);
2806 if (ret)
2807 return ret;
2808 break;
Raed Salem3b3233f2018-05-31 16:43:39 +03002809 case IB_FLOW_SPEC_ACTION_COUNT:
2810 if (FIELDS_NOT_SUPPORTED(ib_spec->flow_count,
2811 LAST_COUNTERS_FIELD))
2812 return -EOPNOTSUPP;
2813
2814 /* for now support only one counters spec per flow */
2815 if (action->action & MLX5_FLOW_CONTEXT_ACTION_COUNT)
2816 return -EINVAL;
2817
2818 action->counters = ib_spec->flow_count.counters;
2819 action->action |= MLX5_FLOW_CONTEXT_ACTION_COUNT;
2820 break;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002821 default:
2822 return -EINVAL;
2823 }
2824
2825 return 0;
2826}
2827
2828/* If a flow could catch both multicast and unicast packets,
2829 * it won't fall into the multicast flow steering table and this rule
2830 * could steal other multicast packets.
2831 */
Yishai Hadasa550ddf2017-08-17 15:52:33 +03002832static bool flow_is_multicast_only(const struct ib_flow_attr *ib_attr)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002833{
Yishai Hadas81e30882017-06-08 16:15:09 +03002834 union ib_flow_spec *flow_spec;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002835
2836 if (ib_attr->type != IB_FLOW_ATTR_NORMAL ||
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002837 ib_attr->num_of_specs < 1)
2838 return false;
2839
Yishai Hadas81e30882017-06-08 16:15:09 +03002840 flow_spec = (union ib_flow_spec *)(ib_attr + 1);
2841 if (flow_spec->type == IB_FLOW_SPEC_IPV4) {
2842 struct ib_flow_spec_ipv4 *ipv4_spec;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002843
Yishai Hadas81e30882017-06-08 16:15:09 +03002844 ipv4_spec = (struct ib_flow_spec_ipv4 *)flow_spec;
2845 if (ipv4_is_multicast(ipv4_spec->val.dst_ip))
2846 return true;
2847
2848 return false;
2849 }
2850
2851 if (flow_spec->type == IB_FLOW_SPEC_ETH) {
2852 struct ib_flow_spec_eth *eth_spec;
2853
2854 eth_spec = (struct ib_flow_spec_eth *)flow_spec;
2855 return is_multicast_ether_addr(eth_spec->mask.dst_mac) &&
2856 is_multicast_ether_addr(eth_spec->val.dst_mac);
2857 }
2858
2859 return false;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002860}
2861
Aviad Yehezkel802c2122018-03-28 09:27:53 +03002862enum valid_spec {
2863 VALID_SPEC_INVALID,
2864 VALID_SPEC_VALID,
2865 VALID_SPEC_NA,
2866};
2867
2868static enum valid_spec
2869is_valid_esp_aes_gcm(struct mlx5_core_dev *mdev,
2870 const struct mlx5_flow_spec *spec,
2871 const struct mlx5_flow_act *flow_act,
2872 bool egress)
2873{
2874 const u32 *match_c = spec->match_criteria;
2875 bool is_crypto =
2876 (flow_act->action & (MLX5_FLOW_CONTEXT_ACTION_ENCRYPT |
2877 MLX5_FLOW_CONTEXT_ACTION_DECRYPT));
2878 bool is_ipsec = mlx5_fs_is_ipsec_flow(match_c);
2879 bool is_drop = flow_act->action & MLX5_FLOW_CONTEXT_ACTION_DROP;
2880
2881 /*
2882 * Currently only crypto is supported in egress, when regular egress
2883 * rules would be supported, always return VALID_SPEC_NA.
2884 */
2885 if (!is_crypto)
2886 return egress ? VALID_SPEC_INVALID : VALID_SPEC_NA;
2887
2888 return is_crypto && is_ipsec &&
2889 (!egress || (!is_drop && !flow_act->has_flow_tag)) ?
2890 VALID_SPEC_VALID : VALID_SPEC_INVALID;
2891}
2892
2893static bool is_valid_spec(struct mlx5_core_dev *mdev,
2894 const struct mlx5_flow_spec *spec,
2895 const struct mlx5_flow_act *flow_act,
2896 bool egress)
2897{
2898 /* We curretly only support ipsec egress flow */
2899 return is_valid_esp_aes_gcm(mdev, spec, flow_act, egress) != VALID_SPEC_INVALID;
2900}
2901
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002902static bool is_valid_ethertype(struct mlx5_core_dev *mdev,
2903 const struct ib_flow_attr *flow_attr,
Ariel Levkovich0f750962017-04-03 13:11:02 +03002904 bool check_inner)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002905{
2906 union ib_flow_spec *ib_spec = (union ib_flow_spec *)(flow_attr + 1);
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002907 int match_ipv = check_inner ?
2908 MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2909 ft_field_support.inner_ip_version) :
2910 MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2911 ft_field_support.outer_ip_version);
Ariel Levkovich0f750962017-04-03 13:11:02 +03002912 int inner_bit = check_inner ? IB_FLOW_SPEC_INNER : 0;
2913 bool ipv4_spec_valid, ipv6_spec_valid;
2914 unsigned int ip_spec_type = 0;
2915 bool has_ethertype = false;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002916 unsigned int spec_index;
Ariel Levkovich0f750962017-04-03 13:11:02 +03002917 bool mask_valid = true;
2918 u16 eth_type = 0;
2919 bool type_valid;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002920
2921 /* Validate that ethertype is correct */
2922 for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
Ariel Levkovich0f750962017-04-03 13:11:02 +03002923 if ((ib_spec->type == (IB_FLOW_SPEC_ETH | inner_bit)) &&
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002924 ib_spec->eth.mask.ether_type) {
Ariel Levkovich0f750962017-04-03 13:11:02 +03002925 mask_valid = (ib_spec->eth.mask.ether_type ==
2926 htons(0xffff));
2927 has_ethertype = true;
2928 eth_type = ntohs(ib_spec->eth.val.ether_type);
2929 } else if ((ib_spec->type == (IB_FLOW_SPEC_IPV4 | inner_bit)) ||
2930 (ib_spec->type == (IB_FLOW_SPEC_IPV6 | inner_bit))) {
2931 ip_spec_type = ib_spec->type;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002932 }
2933 ib_spec = (void *)ib_spec + ib_spec->size;
2934 }
Ariel Levkovich0f750962017-04-03 13:11:02 +03002935
2936 type_valid = (!has_ethertype) || (!ip_spec_type);
2937 if (!type_valid && mask_valid) {
2938 ipv4_spec_valid = (eth_type == ETH_P_IP) &&
2939 (ip_spec_type == (IB_FLOW_SPEC_IPV4 | inner_bit));
2940 ipv6_spec_valid = (eth_type == ETH_P_IPV6) &&
2941 (ip_spec_type == (IB_FLOW_SPEC_IPV6 | inner_bit));
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002942
2943 type_valid = (ipv4_spec_valid) || (ipv6_spec_valid) ||
2944 (((eth_type == ETH_P_MPLS_UC) ||
2945 (eth_type == ETH_P_MPLS_MC)) && match_ipv);
Ariel Levkovich0f750962017-04-03 13:11:02 +03002946 }
2947
2948 return type_valid;
2949}
2950
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002951static bool is_valid_attr(struct mlx5_core_dev *mdev,
2952 const struct ib_flow_attr *flow_attr)
Ariel Levkovich0f750962017-04-03 13:11:02 +03002953{
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002954 return is_valid_ethertype(mdev, flow_attr, false) &&
2955 is_valid_ethertype(mdev, flow_attr, true);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002956}
2957
2958static void put_flow_table(struct mlx5_ib_dev *dev,
2959 struct mlx5_ib_flow_prio *prio, bool ft_added)
2960{
2961 prio->refcount -= !!ft_added;
2962 if (!prio->refcount) {
2963 mlx5_destroy_flow_table(prio->flow_table);
2964 prio->flow_table = NULL;
2965 }
2966}
2967
Raed Salem3b3233f2018-05-31 16:43:39 +03002968static void counters_clear_description(struct ib_counters *counters)
2969{
2970 struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
2971
2972 mutex_lock(&mcounters->mcntrs_mutex);
2973 kfree(mcounters->counters_data);
2974 mcounters->counters_data = NULL;
2975 mcounters->cntrs_max_index = 0;
2976 mutex_unlock(&mcounters->mcntrs_mutex);
2977}
2978
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002979static int mlx5_ib_destroy_flow(struct ib_flow *flow_id)
2980{
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002981 struct mlx5_ib_flow_handler *handler = container_of(flow_id,
2982 struct mlx5_ib_flow_handler,
2983 ibflow);
2984 struct mlx5_ib_flow_handler *iter, *tmp;
Yishai Hadasd4be3f42018-07-23 15:25:10 +03002985 struct mlx5_ib_dev *dev = handler->dev;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002986
Mark Bloch9a4ca382018-01-16 14:42:35 +00002987 mutex_lock(&dev->flow_db->lock);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002988
2989 list_for_each_entry_safe(iter, tmp, &handler->list, list) {
Mark Bloch74491de2016-08-31 11:24:25 +00002990 mlx5_del_flow_rules(iter->rule);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002991 put_flow_table(dev, iter->prio, true);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002992 list_del(&iter->list);
2993 kfree(iter);
2994 }
2995
Mark Bloch74491de2016-08-31 11:24:25 +00002996 mlx5_del_flow_rules(handler->rule);
Maor Gottlieb5497adc2016-08-28 14:16:31 +03002997 put_flow_table(dev, handler->prio, true);
Raed Salem3b3233f2018-05-31 16:43:39 +03002998 if (handler->ibcounters &&
2999 atomic_read(&handler->ibcounters->usecnt) == 1)
3000 counters_clear_description(handler->ibcounters);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003001
Raed Salem3b3233f2018-05-31 16:43:39 +03003002 mutex_unlock(&dev->flow_db->lock);
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003003 if (handler->flow_matcher)
3004 atomic_dec(&handler->flow_matcher->usecnt);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003005 kfree(handler);
3006
3007 return 0;
3008}
3009
Maor Gottlieb35d190112016-03-07 18:51:47 +02003010static int ib_prio_to_core_prio(unsigned int priority, bool dont_trap)
3011{
3012 priority *= 2;
3013 if (!dont_trap)
3014 priority++;
3015 return priority;
3016}
3017
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003018enum flow_table_type {
3019 MLX5_IB_FT_RX,
3020 MLX5_IB_FT_TX
3021};
3022
Maor Gottlieb00b7c2a2017-03-29 06:09:01 +03003023#define MLX5_FS_MAX_TYPES 6
3024#define MLX5_FS_MAX_ENTRIES BIT(16)
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003025
3026static struct mlx5_ib_flow_prio *_get_prio(struct mlx5_flow_namespace *ns,
3027 struct mlx5_ib_flow_prio *prio,
3028 int priority,
3029 int num_entries, int num_groups)
3030{
3031 struct mlx5_flow_table *ft;
3032
3033 ft = mlx5_create_auto_grouped_flow_table(ns, priority,
3034 num_entries,
3035 num_groups,
3036 0, 0);
3037 if (IS_ERR(ft))
3038 return ERR_CAST(ft);
3039
3040 prio->flow_table = ft;
3041 prio->refcount = 0;
3042 return prio;
3043}
3044
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003045static struct mlx5_ib_flow_prio *get_flow_table(struct mlx5_ib_dev *dev,
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003046 struct ib_flow_attr *flow_attr,
3047 enum flow_table_type ft_type)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003048{
Maor Gottlieb35d190112016-03-07 18:51:47 +02003049 bool dont_trap = flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003050 struct mlx5_flow_namespace *ns = NULL;
3051 struct mlx5_ib_flow_prio *prio;
3052 struct mlx5_flow_table *ft;
Maor Gottliebdac388e2017-03-29 06:09:00 +03003053 int max_table_size;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003054 int num_entries;
3055 int num_groups;
3056 int priority;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003057
Maor Gottliebdac388e2017-03-29 06:09:00 +03003058 max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
3059 log_max_ft_size));
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003060 if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
Aviad Yehezkel802c2122018-03-28 09:27:53 +03003061 if (ft_type == MLX5_IB_FT_TX)
3062 priority = 0;
3063 else if (flow_is_multicast_only(flow_attr) &&
3064 !dont_trap)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003065 priority = MLX5_IB_FLOW_MCAST_PRIO;
3066 else
Maor Gottlieb35d190112016-03-07 18:51:47 +02003067 priority = ib_prio_to_core_prio(flow_attr->priority,
3068 dont_trap);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003069 ns = mlx5_get_flow_namespace(dev->mdev,
Aviad Yehezkel802c2122018-03-28 09:27:53 +03003070 ft_type == MLX5_IB_FT_TX ?
3071 MLX5_FLOW_NAMESPACE_EGRESS :
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003072 MLX5_FLOW_NAMESPACE_BYPASS);
3073 num_entries = MLX5_FS_MAX_ENTRIES;
3074 num_groups = MLX5_FS_MAX_TYPES;
Mark Bloch9a4ca382018-01-16 14:42:35 +00003075 prio = &dev->flow_db->prios[priority];
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003076 } else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
3077 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
3078 ns = mlx5_get_flow_namespace(dev->mdev,
3079 MLX5_FLOW_NAMESPACE_LEFTOVERS);
3080 build_leftovers_ft_param(&priority,
3081 &num_entries,
3082 &num_groups);
Mark Bloch9a4ca382018-01-16 14:42:35 +00003083 prio = &dev->flow_db->prios[MLX5_IB_FLOW_LEFTOVERS_PRIO];
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003084 } else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
3085 if (!MLX5_CAP_FLOWTABLE(dev->mdev,
3086 allow_sniffer_and_nic_rx_shared_tir))
3087 return ERR_PTR(-ENOTSUPP);
3088
3089 ns = mlx5_get_flow_namespace(dev->mdev, ft_type == MLX5_IB_FT_RX ?
3090 MLX5_FLOW_NAMESPACE_SNIFFER_RX :
3091 MLX5_FLOW_NAMESPACE_SNIFFER_TX);
3092
Mark Bloch9a4ca382018-01-16 14:42:35 +00003093 prio = &dev->flow_db->sniffer[ft_type];
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003094 priority = 0;
3095 num_entries = 1;
3096 num_groups = 1;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003097 }
3098
3099 if (!ns)
3100 return ERR_PTR(-ENOTSUPP);
3101
Maor Gottliebdac388e2017-03-29 06:09:00 +03003102 if (num_entries > max_table_size)
3103 return ERR_PTR(-ENOMEM);
3104
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003105 ft = prio->flow_table;
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003106 if (!ft)
3107 return _get_prio(ns, prio, priority, num_entries, num_groups);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003108
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003109 return prio;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003110}
3111
Yishai Hadasa550ddf2017-08-17 15:52:33 +03003112static void set_underlay_qp(struct mlx5_ib_dev *dev,
3113 struct mlx5_flow_spec *spec,
3114 u32 underlay_qpn)
3115{
3116 void *misc_params_c = MLX5_ADDR_OF(fte_match_param,
3117 spec->match_criteria,
3118 misc_parameters);
3119 void *misc_params_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
3120 misc_parameters);
3121
3122 if (underlay_qpn &&
3123 MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
3124 ft_field_support.bth_dst_qp)) {
3125 MLX5_SET(fte_match_set_misc,
3126 misc_params_v, bth_dst_qp, underlay_qpn);
3127 MLX5_SET(fte_match_set_misc,
3128 misc_params_c, bth_dst_qp, 0xffffff);
3129 }
3130}
3131
Raed Salem5e95af52018-05-31 16:43:40 +03003132static int read_flow_counters(struct ib_device *ibdev,
3133 struct mlx5_read_counters_attr *read_attr)
3134{
3135 struct mlx5_fc *fc = read_attr->hw_cntrs_hndl;
3136 struct mlx5_ib_dev *dev = to_mdev(ibdev);
3137
3138 return mlx5_fc_query(dev->mdev, fc,
3139 &read_attr->out[IB_COUNTER_PACKETS],
3140 &read_attr->out[IB_COUNTER_BYTES]);
3141}
3142
3143/* flow counters currently expose two counters packets and bytes */
3144#define FLOW_COUNTERS_NUM 2
Raed Salem3b3233f2018-05-31 16:43:39 +03003145static int counters_set_description(struct ib_counters *counters,
3146 enum mlx5_ib_counters_type counters_type,
3147 struct mlx5_ib_flow_counters_desc *desc_data,
3148 u32 ncounters)
3149{
3150 struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
3151 u32 cntrs_max_index = 0;
3152 int i;
3153
3154 if (counters_type != MLX5_IB_COUNTERS_FLOW)
3155 return -EINVAL;
3156
3157 /* init the fields for the object */
3158 mcounters->type = counters_type;
Raed Salem5e95af52018-05-31 16:43:40 +03003159 mcounters->read_counters = read_flow_counters;
3160 mcounters->counters_num = FLOW_COUNTERS_NUM;
Raed Salem3b3233f2018-05-31 16:43:39 +03003161 mcounters->ncounters = ncounters;
3162 /* each counter entry have both description and index pair */
3163 for (i = 0; i < ncounters; i++) {
3164 if (desc_data[i].description > IB_COUNTER_BYTES)
3165 return -EINVAL;
3166
3167 if (cntrs_max_index <= desc_data[i].index)
3168 cntrs_max_index = desc_data[i].index + 1;
3169 }
3170
3171 mutex_lock(&mcounters->mcntrs_mutex);
3172 mcounters->counters_data = desc_data;
3173 mcounters->cntrs_max_index = cntrs_max_index;
3174 mutex_unlock(&mcounters->mcntrs_mutex);
3175
3176 return 0;
3177}
3178
3179#define MAX_COUNTERS_NUM (USHRT_MAX / (sizeof(u32) * 2))
3180static int flow_counters_set_data(struct ib_counters *ibcounters,
3181 struct mlx5_ib_create_flow *ucmd)
3182{
3183 struct mlx5_ib_mcounters *mcounters = to_mcounters(ibcounters);
3184 struct mlx5_ib_flow_counters_data *cntrs_data = NULL;
3185 struct mlx5_ib_flow_counters_desc *desc_data = NULL;
3186 bool hw_hndl = false;
3187 int ret = 0;
3188
3189 if (ucmd && ucmd->ncounters_data != 0) {
3190 cntrs_data = ucmd->data;
3191 if (cntrs_data->ncounters > MAX_COUNTERS_NUM)
3192 return -EINVAL;
3193
3194 desc_data = kcalloc(cntrs_data->ncounters,
3195 sizeof(*desc_data),
3196 GFP_KERNEL);
3197 if (!desc_data)
3198 return -ENOMEM;
3199
3200 if (copy_from_user(desc_data,
3201 u64_to_user_ptr(cntrs_data->counters_data),
3202 sizeof(*desc_data) * cntrs_data->ncounters)) {
3203 ret = -EFAULT;
3204 goto free;
3205 }
3206 }
3207
3208 if (!mcounters->hw_cntrs_hndl) {
3209 mcounters->hw_cntrs_hndl = mlx5_fc_create(
3210 to_mdev(ibcounters->device)->mdev, false);
3211 if (!mcounters->hw_cntrs_hndl) {
3212 ret = -ENOMEM;
3213 goto free;
3214 }
3215 hw_hndl = true;
3216 }
3217
3218 if (desc_data) {
3219 /* counters already bound to at least one flow */
3220 if (mcounters->cntrs_max_index) {
3221 ret = -EINVAL;
3222 goto free_hndl;
3223 }
3224
3225 ret = counters_set_description(ibcounters,
3226 MLX5_IB_COUNTERS_FLOW,
3227 desc_data,
3228 cntrs_data->ncounters);
3229 if (ret)
3230 goto free_hndl;
3231
3232 } else if (!mcounters->cntrs_max_index) {
3233 /* counters not bound yet, must have udata passed */
3234 ret = -EINVAL;
3235 goto free_hndl;
3236 }
3237
3238 return 0;
3239
3240free_hndl:
3241 if (hw_hndl) {
3242 mlx5_fc_destroy(to_mdev(ibcounters->device)->mdev,
3243 mcounters->hw_cntrs_hndl);
3244 mcounters->hw_cntrs_hndl = NULL;
3245 }
3246free:
3247 kfree(desc_data);
3248 return ret;
3249}
3250
Yishai Hadasa550ddf2017-08-17 15:52:33 +03003251static struct mlx5_ib_flow_handler *_create_flow_rule(struct mlx5_ib_dev *dev,
3252 struct mlx5_ib_flow_prio *ft_prio,
3253 const struct ib_flow_attr *flow_attr,
3254 struct mlx5_flow_destination *dst,
Raed Salem3b3233f2018-05-31 16:43:39 +03003255 u32 underlay_qpn,
3256 struct mlx5_ib_create_flow *ucmd)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003257{
3258 struct mlx5_flow_table *ft = ft_prio->flow_table;
3259 struct mlx5_ib_flow_handler *handler;
Boris Pismenny075572d2017-08-16 09:33:30 +03003260 struct mlx5_flow_act flow_act = {.flow_tag = MLX5_FS_DEFAULT_FLOW_TAG};
Maor Gottliebc5bb1732016-07-04 17:23:05 +03003261 struct mlx5_flow_spec *spec;
Raed Salem3b3233f2018-05-31 16:43:39 +03003262 struct mlx5_flow_destination dest_arr[2] = {};
3263 struct mlx5_flow_destination *rule_dst = dest_arr;
Maor Gottliebdd063d02016-08-28 14:16:32 +03003264 const void *ib_flow = (const void *)flow_attr + sizeof(*flow_attr);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003265 unsigned int spec_index;
Ariel Levkovich71c6e862018-05-13 14:33:34 +03003266 u32 prev_type = 0;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003267 int err = 0;
Raed Salem3b3233f2018-05-31 16:43:39 +03003268 int dest_num = 0;
Aviad Yehezkel802c2122018-03-28 09:27:53 +03003269 bool is_egress = flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003270
Ariel Levkovich19cc7522017-04-03 13:11:03 +03003271 if (!is_valid_attr(dev->mdev, flow_attr))
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003272 return ERR_PTR(-EINVAL);
3273
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03003274 spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003275 handler = kzalloc(sizeof(*handler), GFP_KERNEL);
Maor Gottliebc5bb1732016-07-04 17:23:05 +03003276 if (!handler || !spec) {
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003277 err = -ENOMEM;
3278 goto free;
3279 }
3280
3281 INIT_LIST_HEAD(&handler->list);
Raed Salem3b3233f2018-05-31 16:43:39 +03003282 if (dst) {
3283 memcpy(&dest_arr[0], dst, sizeof(*dst));
3284 dest_num++;
3285 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003286
3287 for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
Ariel Levkovich19cc7522017-04-03 13:11:03 +03003288 err = parse_flow_attr(dev->mdev, spec->match_criteria,
Slava Shwartsmana22ed862017-04-03 13:13:52 +03003289 spec->match_value,
Ariel Levkovich71c6e862018-05-13 14:33:34 +03003290 ib_flow, flow_attr, &flow_act,
3291 prev_type);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003292 if (err < 0)
3293 goto free;
3294
Ariel Levkovich71c6e862018-05-13 14:33:34 +03003295 prev_type = ((union ib_flow_spec *)ib_flow)->type;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003296 ib_flow += ((union ib_flow_spec *)ib_flow)->size;
3297 }
3298
Yishai Hadasa550ddf2017-08-17 15:52:33 +03003299 if (!flow_is_multicast_only(flow_attr))
3300 set_underlay_qp(dev, spec, underlay_qpn);
3301
Mark Bloch018a94e2018-01-16 14:44:29 +00003302 if (dev->rep) {
3303 void *misc;
3304
3305 misc = MLX5_ADDR_OF(fte_match_param, spec->match_value,
3306 misc_parameters);
3307 MLX5_SET(fte_match_set_misc, misc, source_port,
3308 dev->rep->vport);
3309 misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
3310 misc_parameters);
3311 MLX5_SET_TO_ONES(fte_match_set_misc, misc, source_port);
3312 }
3313
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03003314 spec->match_criteria_enable = get_match_criteria_enable(spec->match_criteria);
Aviad Yehezkel802c2122018-03-28 09:27:53 +03003315
3316 if (is_egress &&
3317 !is_valid_spec(dev->mdev, spec, &flow_act, is_egress)) {
3318 err = -EINVAL;
3319 goto free;
3320 }
3321
Raed Salem3b3233f2018-05-31 16:43:39 +03003322 if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_COUNT) {
3323 err = flow_counters_set_data(flow_act.counters, ucmd);
3324 if (err)
3325 goto free;
3326
3327 handler->ibcounters = flow_act.counters;
3328 dest_arr[dest_num].type =
3329 MLX5_FLOW_DESTINATION_TYPE_COUNTER;
3330 dest_arr[dest_num].counter =
3331 to_mcounters(flow_act.counters)->hw_cntrs_hndl;
3332 dest_num++;
3333 }
3334
Boris Pismenny075572d2017-08-16 09:33:30 +03003335 if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_DROP) {
Raed Salem3b3233f2018-05-31 16:43:39 +03003336 if (!(flow_act.action & MLX5_FLOW_CONTEXT_ACTION_COUNT)) {
3337 rule_dst = NULL;
3338 dest_num = 0;
3339 }
Slava Shwartsmana22ed862017-04-03 13:13:52 +03003340 } else {
Aviad Yehezkel802c2122018-03-28 09:27:53 +03003341 if (is_egress)
3342 flow_act.action |= MLX5_FLOW_CONTEXT_ACTION_ALLOW;
3343 else
3344 flow_act.action |=
Raed Salem3b3233f2018-05-31 16:43:39 +03003345 dest_num ? MLX5_FLOW_CONTEXT_ACTION_FWD_DEST :
Aviad Yehezkel802c2122018-03-28 09:27:53 +03003346 MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO;
Slava Shwartsmana22ed862017-04-03 13:13:52 +03003347 }
Moses Reuben2ac693f2017-01-18 14:59:50 +02003348
Matan Baraka9db0ec2017-08-16 09:43:48 +03003349 if (flow_act.has_flow_tag &&
Moses Reuben2ac693f2017-01-18 14:59:50 +02003350 (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
3351 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT)) {
3352 mlx5_ib_warn(dev, "Flow tag %u and attribute type %x isn't allowed in leftovers\n",
Boris Pismenny075572d2017-08-16 09:33:30 +03003353 flow_act.flow_tag, flow_attr->type);
Moses Reuben2ac693f2017-01-18 14:59:50 +02003354 err = -EINVAL;
3355 goto free;
3356 }
Mark Bloch74491de2016-08-31 11:24:25 +00003357 handler->rule = mlx5_add_flow_rules(ft, spec,
Hadar Hen Zion66958ed2016-11-07 15:14:45 +02003358 &flow_act,
Slava Shwartsmana22ed862017-04-03 13:13:52 +03003359 rule_dst, dest_num);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003360
3361 if (IS_ERR(handler->rule)) {
3362 err = PTR_ERR(handler->rule);
3363 goto free;
3364 }
3365
Maor Gottliebd9d49802016-08-28 14:16:33 +03003366 ft_prio->refcount++;
Maor Gottlieb5497adc2016-08-28 14:16:31 +03003367 handler->prio = ft_prio;
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003368 handler->dev = dev;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003369
3370 ft_prio->flow_table = ft;
3371free:
Raed Salem3b3233f2018-05-31 16:43:39 +03003372 if (err && handler) {
3373 if (handler->ibcounters &&
3374 atomic_read(&handler->ibcounters->usecnt) == 1)
3375 counters_clear_description(handler->ibcounters);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003376 kfree(handler);
Raed Salem3b3233f2018-05-31 16:43:39 +03003377 }
Maor Gottliebc5bb1732016-07-04 17:23:05 +03003378 kvfree(spec);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003379 return err ? ERR_PTR(err) : handler;
3380}
3381
Yishai Hadasa550ddf2017-08-17 15:52:33 +03003382static struct mlx5_ib_flow_handler *create_flow_rule(struct mlx5_ib_dev *dev,
3383 struct mlx5_ib_flow_prio *ft_prio,
3384 const struct ib_flow_attr *flow_attr,
3385 struct mlx5_flow_destination *dst)
3386{
Raed Salem3b3233f2018-05-31 16:43:39 +03003387 return _create_flow_rule(dev, ft_prio, flow_attr, dst, 0, NULL);
Yishai Hadasa550ddf2017-08-17 15:52:33 +03003388}
3389
Maor Gottlieb35d190112016-03-07 18:51:47 +02003390static struct mlx5_ib_flow_handler *create_dont_trap_rule(struct mlx5_ib_dev *dev,
3391 struct mlx5_ib_flow_prio *ft_prio,
3392 struct ib_flow_attr *flow_attr,
3393 struct mlx5_flow_destination *dst)
3394{
3395 struct mlx5_ib_flow_handler *handler_dst = NULL;
3396 struct mlx5_ib_flow_handler *handler = NULL;
3397
3398 handler = create_flow_rule(dev, ft_prio, flow_attr, NULL);
3399 if (!IS_ERR(handler)) {
3400 handler_dst = create_flow_rule(dev, ft_prio,
3401 flow_attr, dst);
3402 if (IS_ERR(handler_dst)) {
Mark Bloch74491de2016-08-31 11:24:25 +00003403 mlx5_del_flow_rules(handler->rule);
Maor Gottliebd9d49802016-08-28 14:16:33 +03003404 ft_prio->refcount--;
Maor Gottlieb35d190112016-03-07 18:51:47 +02003405 kfree(handler);
3406 handler = handler_dst;
3407 } else {
3408 list_add(&handler_dst->list, &handler->list);
3409 }
3410 }
3411
3412 return handler;
3413}
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003414enum {
3415 LEFTOVERS_MC,
3416 LEFTOVERS_UC,
3417};
3418
3419static struct mlx5_ib_flow_handler *create_leftovers_rule(struct mlx5_ib_dev *dev,
3420 struct mlx5_ib_flow_prio *ft_prio,
3421 struct ib_flow_attr *flow_attr,
3422 struct mlx5_flow_destination *dst)
3423{
3424 struct mlx5_ib_flow_handler *handler_ucast = NULL;
3425 struct mlx5_ib_flow_handler *handler = NULL;
3426
3427 static struct {
3428 struct ib_flow_attr flow_attr;
3429 struct ib_flow_spec_eth eth_flow;
3430 } leftovers_specs[] = {
3431 [LEFTOVERS_MC] = {
3432 .flow_attr = {
3433 .num_of_specs = 1,
3434 .size = sizeof(leftovers_specs[0])
3435 },
3436 .eth_flow = {
3437 .type = IB_FLOW_SPEC_ETH,
3438 .size = sizeof(struct ib_flow_spec_eth),
3439 .mask = {.dst_mac = {0x1} },
3440 .val = {.dst_mac = {0x1} }
3441 }
3442 },
3443 [LEFTOVERS_UC] = {
3444 .flow_attr = {
3445 .num_of_specs = 1,
3446 .size = sizeof(leftovers_specs[0])
3447 },
3448 .eth_flow = {
3449 .type = IB_FLOW_SPEC_ETH,
3450 .size = sizeof(struct ib_flow_spec_eth),
3451 .mask = {.dst_mac = {0x1} },
3452 .val = {.dst_mac = {} }
3453 }
3454 }
3455 };
3456
3457 handler = create_flow_rule(dev, ft_prio,
3458 &leftovers_specs[LEFTOVERS_MC].flow_attr,
3459 dst);
3460 if (!IS_ERR(handler) &&
3461 flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT) {
3462 handler_ucast = create_flow_rule(dev, ft_prio,
3463 &leftovers_specs[LEFTOVERS_UC].flow_attr,
3464 dst);
3465 if (IS_ERR(handler_ucast)) {
Mark Bloch74491de2016-08-31 11:24:25 +00003466 mlx5_del_flow_rules(handler->rule);
Maor Gottliebd9d49802016-08-28 14:16:33 +03003467 ft_prio->refcount--;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003468 kfree(handler);
3469 handler = handler_ucast;
3470 } else {
3471 list_add(&handler_ucast->list, &handler->list);
3472 }
3473 }
3474
3475 return handler;
3476}
3477
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003478static struct mlx5_ib_flow_handler *create_sniffer_rule(struct mlx5_ib_dev *dev,
3479 struct mlx5_ib_flow_prio *ft_rx,
3480 struct mlx5_ib_flow_prio *ft_tx,
3481 struct mlx5_flow_destination *dst)
3482{
3483 struct mlx5_ib_flow_handler *handler_rx;
3484 struct mlx5_ib_flow_handler *handler_tx;
3485 int err;
3486 static const struct ib_flow_attr flow_attr = {
3487 .num_of_specs = 0,
3488 .size = sizeof(flow_attr)
3489 };
3490
3491 handler_rx = create_flow_rule(dev, ft_rx, &flow_attr, dst);
3492 if (IS_ERR(handler_rx)) {
3493 err = PTR_ERR(handler_rx);
3494 goto err;
3495 }
3496
3497 handler_tx = create_flow_rule(dev, ft_tx, &flow_attr, dst);
3498 if (IS_ERR(handler_tx)) {
3499 err = PTR_ERR(handler_tx);
3500 goto err_tx;
3501 }
3502
3503 list_add(&handler_tx->list, &handler_rx->list);
3504
3505 return handler_rx;
3506
3507err_tx:
Mark Bloch74491de2016-08-31 11:24:25 +00003508 mlx5_del_flow_rules(handler_rx->rule);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003509 ft_rx->refcount--;
3510 kfree(handler_rx);
3511err:
3512 return ERR_PTR(err);
3513}
3514
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003515static struct ib_flow *mlx5_ib_create_flow(struct ib_qp *qp,
3516 struct ib_flow_attr *flow_attr,
Matan Barak59082a32018-05-31 16:43:35 +03003517 int domain,
3518 struct ib_udata *udata)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003519{
3520 struct mlx5_ib_dev *dev = to_mdev(qp->device);
Yishai Hadasd9f88e52016-08-28 10:58:37 +03003521 struct mlx5_ib_qp *mqp = to_mqp(qp);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003522 struct mlx5_ib_flow_handler *handler = NULL;
3523 struct mlx5_flow_destination *dst = NULL;
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003524 struct mlx5_ib_flow_prio *ft_prio_tx = NULL;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003525 struct mlx5_ib_flow_prio *ft_prio;
Aviad Yehezkel802c2122018-03-28 09:27:53 +03003526 bool is_egress = flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS;
Raed Salem3b3233f2018-05-31 16:43:39 +03003527 struct mlx5_ib_create_flow *ucmd = NULL, ucmd_hdr;
3528 size_t min_ucmd_sz, required_ucmd_sz;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003529 int err;
Yishai Hadasa550ddf2017-08-17 15:52:33 +03003530 int underlay_qpn;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003531
Raed Salem3b3233f2018-05-31 16:43:39 +03003532 if (udata && udata->inlen) {
3533 min_ucmd_sz = offsetof(typeof(ucmd_hdr), reserved) +
3534 sizeof(ucmd_hdr.reserved);
3535 if (udata->inlen < min_ucmd_sz)
3536 return ERR_PTR(-EOPNOTSUPP);
3537
3538 err = ib_copy_from_udata(&ucmd_hdr, udata, min_ucmd_sz);
3539 if (err)
3540 return ERR_PTR(err);
3541
3542 /* currently supports only one counters data */
3543 if (ucmd_hdr.ncounters_data > 1)
3544 return ERR_PTR(-EINVAL);
3545
3546 required_ucmd_sz = min_ucmd_sz +
3547 sizeof(struct mlx5_ib_flow_counters_data) *
3548 ucmd_hdr.ncounters_data;
3549 if (udata->inlen > required_ucmd_sz &&
3550 !ib_is_udata_cleared(udata, required_ucmd_sz,
3551 udata->inlen - required_ucmd_sz))
3552 return ERR_PTR(-EOPNOTSUPP);
3553
3554 ucmd = kzalloc(required_ucmd_sz, GFP_KERNEL);
3555 if (!ucmd)
3556 return ERR_PTR(-ENOMEM);
3557
3558 err = ib_copy_from_udata(ucmd, udata, required_ucmd_sz);
3559 if (err) {
3560 kfree(ucmd);
3561 return ERR_PTR(err);
3562 }
3563 }
Matan Barak59082a32018-05-31 16:43:35 +03003564
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003565 if (flow_attr->priority > MLX5_IB_FLOW_LAST_PRIO)
Maor Gottliebdac388e2017-03-29 06:09:00 +03003566 return ERR_PTR(-ENOMEM);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003567
3568 if (domain != IB_FLOW_DOMAIN_USER ||
Daniel Jurgens508562d2018-01-04 17:25:34 +02003569 flow_attr->port > dev->num_ports ||
Aviad Yehezkel802c2122018-03-28 09:27:53 +03003570 (flow_attr->flags & ~(IB_FLOW_ATTR_FLAGS_DONT_TRAP |
3571 IB_FLOW_ATTR_FLAGS_EGRESS)))
3572 return ERR_PTR(-EINVAL);
3573
3574 if (is_egress &&
3575 (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
3576 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT))
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003577 return ERR_PTR(-EINVAL);
3578
3579 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
3580 if (!dst)
3581 return ERR_PTR(-ENOMEM);
3582
Mark Bloch9a4ca382018-01-16 14:42:35 +00003583 mutex_lock(&dev->flow_db->lock);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003584
Aviad Yehezkel802c2122018-03-28 09:27:53 +03003585 ft_prio = get_flow_table(dev, flow_attr,
3586 is_egress ? MLX5_IB_FT_TX : MLX5_IB_FT_RX);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003587 if (IS_ERR(ft_prio)) {
3588 err = PTR_ERR(ft_prio);
3589 goto unlock;
3590 }
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003591 if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
3592 ft_prio_tx = get_flow_table(dev, flow_attr, MLX5_IB_FT_TX);
3593 if (IS_ERR(ft_prio_tx)) {
3594 err = PTR_ERR(ft_prio_tx);
3595 ft_prio_tx = NULL;
3596 goto destroy_ft;
3597 }
3598 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003599
Aviad Yehezkel802c2122018-03-28 09:27:53 +03003600 if (is_egress) {
3601 dst->type = MLX5_FLOW_DESTINATION_TYPE_PORT;
3602 } else {
3603 dst->type = MLX5_FLOW_DESTINATION_TYPE_TIR;
3604 if (mqp->flags & MLX5_IB_QP_RSS)
3605 dst->tir_num = mqp->rss_qp.tirn;
3606 else
3607 dst->tir_num = mqp->raw_packet_qp.rq.tirn;
3608 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003609
3610 if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
Maor Gottlieb35d190112016-03-07 18:51:47 +02003611 if (flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP) {
3612 handler = create_dont_trap_rule(dev, ft_prio,
3613 flow_attr, dst);
3614 } else {
Yishai Hadasa550ddf2017-08-17 15:52:33 +03003615 underlay_qpn = (mqp->flags & MLX5_IB_QP_UNDERLAY) ?
3616 mqp->underlay_qpn : 0;
3617 handler = _create_flow_rule(dev, ft_prio, flow_attr,
Raed Salem3b3233f2018-05-31 16:43:39 +03003618 dst, underlay_qpn, ucmd);
Maor Gottlieb35d190112016-03-07 18:51:47 +02003619 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003620 } else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
3621 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
3622 handler = create_leftovers_rule(dev, ft_prio, flow_attr,
3623 dst);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003624 } else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
3625 handler = create_sniffer_rule(dev, ft_prio, ft_prio_tx, dst);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003626 } else {
3627 err = -EINVAL;
3628 goto destroy_ft;
3629 }
3630
3631 if (IS_ERR(handler)) {
3632 err = PTR_ERR(handler);
3633 handler = NULL;
3634 goto destroy_ft;
3635 }
3636
Mark Bloch9a4ca382018-01-16 14:42:35 +00003637 mutex_unlock(&dev->flow_db->lock);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003638 kfree(dst);
Raed Salem3b3233f2018-05-31 16:43:39 +03003639 kfree(ucmd);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003640
3641 return &handler->ibflow;
3642
3643destroy_ft:
3644 put_flow_table(dev, ft_prio, false);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003645 if (ft_prio_tx)
3646 put_flow_table(dev, ft_prio_tx, false);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003647unlock:
Mark Bloch9a4ca382018-01-16 14:42:35 +00003648 mutex_unlock(&dev->flow_db->lock);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003649 kfree(dst);
Raed Salem3b3233f2018-05-31 16:43:39 +03003650 kfree(ucmd);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003651 kfree(handler);
3652 return ERR_PTR(err);
3653}
3654
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003655static struct mlx5_ib_flow_prio *_get_flow_table(struct mlx5_ib_dev *dev,
3656 int priority, bool mcast)
3657{
3658 int max_table_size;
3659 struct mlx5_flow_namespace *ns = NULL;
3660 struct mlx5_ib_flow_prio *prio;
3661
3662 max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
3663 log_max_ft_size));
3664 if (max_table_size < MLX5_FS_MAX_ENTRIES)
3665 return ERR_PTR(-ENOMEM);
3666
3667 if (mcast)
3668 priority = MLX5_IB_FLOW_MCAST_PRIO;
3669 else
3670 priority = ib_prio_to_core_prio(priority, false);
3671
3672 ns = mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS);
3673 if (!ns)
3674 return ERR_PTR(-ENOTSUPP);
3675
3676 prio = &dev->flow_db->prios[priority];
3677
3678 if (prio->flow_table)
3679 return prio;
3680
3681 return _get_prio(ns, prio, priority, MLX5_FS_MAX_ENTRIES,
3682 MLX5_FS_MAX_TYPES);
3683}
3684
3685static struct mlx5_ib_flow_handler *
3686_create_raw_flow_rule(struct mlx5_ib_dev *dev,
3687 struct mlx5_ib_flow_prio *ft_prio,
3688 struct mlx5_flow_destination *dst,
3689 struct mlx5_ib_flow_matcher *fs_matcher,
3690 void *cmd_in, int inlen)
3691{
3692 struct mlx5_ib_flow_handler *handler;
3693 struct mlx5_flow_act flow_act = {.flow_tag = MLX5_FS_DEFAULT_FLOW_TAG};
3694 struct mlx5_flow_spec *spec;
3695 struct mlx5_flow_table *ft = ft_prio->flow_table;
3696 int err = 0;
3697
3698 spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
3699 handler = kzalloc(sizeof(*handler), GFP_KERNEL);
3700 if (!handler || !spec) {
3701 err = -ENOMEM;
3702 goto free;
3703 }
3704
3705 INIT_LIST_HEAD(&handler->list);
3706
3707 memcpy(spec->match_value, cmd_in, inlen);
3708 memcpy(spec->match_criteria, fs_matcher->matcher_mask.match_params,
3709 fs_matcher->mask_len);
3710 spec->match_criteria_enable = fs_matcher->match_criteria_enable;
3711
3712 flow_act.action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
3713 handler->rule = mlx5_add_flow_rules(ft, spec,
3714 &flow_act, dst, 1);
3715
3716 if (IS_ERR(handler->rule)) {
3717 err = PTR_ERR(handler->rule);
3718 goto free;
3719 }
3720
3721 ft_prio->refcount++;
3722 handler->prio = ft_prio;
3723 handler->dev = dev;
3724 ft_prio->flow_table = ft;
3725
3726free:
3727 if (err)
3728 kfree(handler);
3729 kvfree(spec);
3730 return err ? ERR_PTR(err) : handler;
3731}
3732
3733static bool raw_fs_is_multicast(struct mlx5_ib_flow_matcher *fs_matcher,
3734 void *match_v)
3735{
3736 void *match_c;
3737 void *match_v_set_lyr_2_4, *match_c_set_lyr_2_4;
3738 void *dmac, *dmac_mask;
3739 void *ipv4, *ipv4_mask;
3740
3741 if (!(fs_matcher->match_criteria_enable &
3742 (1 << MATCH_CRITERIA_ENABLE_OUTER_BIT)))
3743 return false;
3744
3745 match_c = fs_matcher->matcher_mask.match_params;
3746 match_v_set_lyr_2_4 = MLX5_ADDR_OF(fte_match_param, match_v,
3747 outer_headers);
3748 match_c_set_lyr_2_4 = MLX5_ADDR_OF(fte_match_param, match_c,
3749 outer_headers);
3750
3751 dmac = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_v_set_lyr_2_4,
3752 dmac_47_16);
3753 dmac_mask = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_c_set_lyr_2_4,
3754 dmac_47_16);
3755
3756 if (is_multicast_ether_addr(dmac) &&
3757 is_multicast_ether_addr(dmac_mask))
3758 return true;
3759
3760 ipv4 = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_v_set_lyr_2_4,
3761 dst_ipv4_dst_ipv6.ipv4_layout.ipv4);
3762
3763 ipv4_mask = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_c_set_lyr_2_4,
3764 dst_ipv4_dst_ipv6.ipv4_layout.ipv4);
3765
3766 if (ipv4_is_multicast(*(__be32 *)(ipv4)) &&
3767 ipv4_is_multicast(*(__be32 *)(ipv4_mask)))
3768 return true;
3769
3770 return false;
3771}
3772
Yishai Hadas32269442018-07-23 15:25:09 +03003773struct mlx5_ib_flow_handler *
3774mlx5_ib_raw_fs_rule_add(struct mlx5_ib_dev *dev,
3775 struct mlx5_ib_flow_matcher *fs_matcher,
3776 void *cmd_in, int inlen, int dest_id,
3777 int dest_type)
3778{
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003779 struct mlx5_flow_destination *dst;
3780 struct mlx5_ib_flow_prio *ft_prio;
3781 int priority = fs_matcher->priority;
3782 struct mlx5_ib_flow_handler *handler;
3783 bool mcast;
3784 int err;
3785
3786 if (fs_matcher->flow_type != MLX5_IB_FLOW_TYPE_NORMAL)
3787 return ERR_PTR(-EOPNOTSUPP);
3788
3789 if (fs_matcher->priority > MLX5_IB_FLOW_LAST_PRIO)
3790 return ERR_PTR(-ENOMEM);
3791
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003792 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
3793 if (!dst)
3794 return ERR_PTR(-ENOMEM);
3795
3796 mcast = raw_fs_is_multicast(fs_matcher, cmd_in);
3797 mutex_lock(&dev->flow_db->lock);
3798
3799 ft_prio = _get_flow_table(dev, priority, mcast);
3800 if (IS_ERR(ft_prio)) {
3801 err = PTR_ERR(ft_prio);
3802 goto unlock;
3803 }
3804
Yishai Hadas6346f0b2018-07-23 15:25:11 +03003805 if (dest_type == MLX5_FLOW_DESTINATION_TYPE_TIR) {
3806 dst->type = dest_type;
3807 dst->tir_num = dest_id;
3808 } else {
3809 dst->type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE_NUM;
3810 dst->ft_num = dest_id;
3811 }
3812
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003813 handler = _create_raw_flow_rule(dev, ft_prio, dst, fs_matcher, cmd_in,
3814 inlen);
3815
3816 if (IS_ERR(handler)) {
3817 err = PTR_ERR(handler);
3818 goto destroy_ft;
3819 }
3820
3821 mutex_unlock(&dev->flow_db->lock);
3822 atomic_inc(&fs_matcher->usecnt);
3823 handler->flow_matcher = fs_matcher;
3824
3825 kfree(dst);
3826
3827 return handler;
3828
3829destroy_ft:
3830 put_flow_table(dev, ft_prio, false);
3831unlock:
3832 mutex_unlock(&dev->flow_db->lock);
3833 kfree(dst);
3834
3835 return ERR_PTR(err);
Yishai Hadas32269442018-07-23 15:25:09 +03003836}
3837
Aviad Yehezkelc6475a02018-03-28 09:27:50 +03003838static u32 mlx5_ib_flow_action_flags_to_accel_xfrm_flags(u32 mlx5_flags)
3839{
3840 u32 flags = 0;
3841
3842 if (mlx5_flags & MLX5_IB_UAPI_FLOW_ACTION_FLAGS_REQUIRE_METADATA)
3843 flags |= MLX5_ACCEL_XFRM_FLAG_REQUIRE_METADATA;
3844
3845 return flags;
3846}
3847
3848#define MLX5_FLOW_ACTION_ESP_CREATE_LAST_SUPPORTED MLX5_IB_UAPI_FLOW_ACTION_FLAGS_REQUIRE_METADATA
3849static struct ib_flow_action *
3850mlx5_ib_create_flow_action_esp(struct ib_device *device,
3851 const struct ib_flow_action_attrs_esp *attr,
3852 struct uverbs_attr_bundle *attrs)
3853{
3854 struct mlx5_ib_dev *mdev = to_mdev(device);
3855 struct ib_uverbs_flow_action_esp_keymat_aes_gcm *aes_gcm;
3856 struct mlx5_accel_esp_xfrm_attrs accel_attrs = {};
3857 struct mlx5_ib_flow_action *action;
3858 u64 action_flags;
3859 u64 flags;
3860 int err = 0;
3861
3862 if (IS_UVERBS_COPY_ERR(uverbs_copy_from(&action_flags, attrs,
3863 MLX5_IB_ATTR_CREATE_FLOW_ACTION_FLAGS)))
3864 return ERR_PTR(-EFAULT);
3865
3866 if (action_flags >= (MLX5_FLOW_ACTION_ESP_CREATE_LAST_SUPPORTED << 1))
3867 return ERR_PTR(-EOPNOTSUPP);
3868
3869 flags = mlx5_ib_flow_action_flags_to_accel_xfrm_flags(action_flags);
3870
3871 /* We current only support a subset of the standard features. Only a
3872 * keymat of type AES_GCM, with icv_len == 16, iv_algo == SEQ and esn
3873 * (with overlap). Full offload mode isn't supported.
3874 */
3875 if (!attr->keymat || attr->replay || attr->encap ||
3876 attr->spi || attr->seq || attr->tfc_pad ||
3877 attr->hard_limit_pkts ||
3878 (attr->flags & ~(IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED |
3879 IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ENCRYPT)))
3880 return ERR_PTR(-EOPNOTSUPP);
3881
3882 if (attr->keymat->protocol !=
3883 IB_UVERBS_FLOW_ACTION_ESP_KEYMAT_AES_GCM)
3884 return ERR_PTR(-EOPNOTSUPP);
3885
3886 aes_gcm = &attr->keymat->keymat.aes_gcm;
3887
3888 if (aes_gcm->icv_len != 16 ||
3889 aes_gcm->iv_algo != IB_UVERBS_FLOW_ACTION_IV_ALGO_SEQ)
3890 return ERR_PTR(-EOPNOTSUPP);
3891
3892 action = kmalloc(sizeof(*action), GFP_KERNEL);
3893 if (!action)
3894 return ERR_PTR(-ENOMEM);
3895
3896 action->esp_aes_gcm.ib_flags = attr->flags;
3897 memcpy(&accel_attrs.keymat.aes_gcm.aes_key, &aes_gcm->aes_key,
3898 sizeof(accel_attrs.keymat.aes_gcm.aes_key));
3899 accel_attrs.keymat.aes_gcm.key_len = aes_gcm->key_len * 8;
3900 memcpy(&accel_attrs.keymat.aes_gcm.salt, &aes_gcm->salt,
3901 sizeof(accel_attrs.keymat.aes_gcm.salt));
3902 memcpy(&accel_attrs.keymat.aes_gcm.seq_iv, &aes_gcm->iv,
3903 sizeof(accel_attrs.keymat.aes_gcm.seq_iv));
3904 accel_attrs.keymat.aes_gcm.icv_len = aes_gcm->icv_len * 8;
3905 accel_attrs.keymat.aes_gcm.iv_algo = MLX5_ACCEL_ESP_AES_GCM_IV_ALGO_SEQ;
3906 accel_attrs.keymat_type = MLX5_ACCEL_ESP_KEYMAT_AES_GCM;
3907
3908 accel_attrs.esn = attr->esn;
3909 if (attr->flags & IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED)
3910 accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_TRIGGERED;
3911 if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW)
3912 accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP;
3913
3914 if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ENCRYPT)
3915 accel_attrs.action |= MLX5_ACCEL_ESP_ACTION_ENCRYPT;
3916
3917 action->esp_aes_gcm.ctx =
3918 mlx5_accel_esp_create_xfrm(mdev->mdev, &accel_attrs, flags);
3919 if (IS_ERR(action->esp_aes_gcm.ctx)) {
3920 err = PTR_ERR(action->esp_aes_gcm.ctx);
3921 goto err_parse;
3922 }
3923
3924 action->esp_aes_gcm.ib_flags = attr->flags;
3925
3926 return &action->ib_action;
3927
3928err_parse:
3929 kfree(action);
3930 return ERR_PTR(err);
3931}
3932
Matan Barak349705c2018-03-28 09:27:51 +03003933static int
3934mlx5_ib_modify_flow_action_esp(struct ib_flow_action *action,
3935 const struct ib_flow_action_attrs_esp *attr,
3936 struct uverbs_attr_bundle *attrs)
3937{
3938 struct mlx5_ib_flow_action *maction = to_mflow_act(action);
3939 struct mlx5_accel_esp_xfrm_attrs accel_attrs;
3940 int err = 0;
3941
3942 if (attr->keymat || attr->replay || attr->encap ||
3943 attr->spi || attr->seq || attr->tfc_pad ||
3944 attr->hard_limit_pkts ||
3945 (attr->flags & ~(IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED |
3946 IB_FLOW_ACTION_ESP_FLAGS_MOD_ESP_ATTRS |
3947 IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW)))
3948 return -EOPNOTSUPP;
3949
3950 /* Only the ESN value or the MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP can
3951 * be modified.
3952 */
3953 if (!(maction->esp_aes_gcm.ib_flags &
3954 IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED) &&
3955 attr->flags & (IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED |
3956 IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW))
3957 return -EINVAL;
3958
3959 memcpy(&accel_attrs, &maction->esp_aes_gcm.ctx->attrs,
3960 sizeof(accel_attrs));
3961
3962 accel_attrs.esn = attr->esn;
3963 if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW)
3964 accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP;
3965 else
3966 accel_attrs.flags &= ~MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP;
3967
3968 err = mlx5_accel_esp_modify_xfrm(maction->esp_aes_gcm.ctx,
3969 &accel_attrs);
3970 if (err)
3971 return err;
3972
3973 maction->esp_aes_gcm.ib_flags &=
3974 ~IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW;
3975 maction->esp_aes_gcm.ib_flags |=
3976 attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW;
3977
3978 return 0;
3979}
3980
Aviad Yehezkelc6475a02018-03-28 09:27:50 +03003981static int mlx5_ib_destroy_flow_action(struct ib_flow_action *action)
3982{
3983 struct mlx5_ib_flow_action *maction = to_mflow_act(action);
3984
3985 switch (action->type) {
3986 case IB_FLOW_ACTION_ESP:
3987 /*
3988 * We only support aes_gcm by now, so we implicitly know this is
3989 * the underline crypto.
3990 */
3991 mlx5_accel_esp_destroy_xfrm(maction->esp_aes_gcm.ctx);
3992 break;
3993 default:
3994 WARN_ON(true);
3995 break;
3996 }
3997
3998 kfree(maction);
3999 return 0;
4000}
4001
Eli Cohene126ba92013-07-07 17:25:49 +03004002static int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
4003{
4004 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
Yishai Hadas81e30882017-06-08 16:15:09 +03004005 struct mlx5_ib_qp *mqp = to_mqp(ibqp);
Eli Cohene126ba92013-07-07 17:25:49 +03004006 int err;
4007
Yishai Hadas81e30882017-06-08 16:15:09 +03004008 if (mqp->flags & MLX5_IB_QP_UNDERLAY) {
4009 mlx5_ib_dbg(dev, "Attaching a multi cast group to underlay QP is not supported\n");
4010 return -EOPNOTSUPP;
4011 }
4012
Jack Morgenstein9603b612014-07-28 23:30:22 +03004013 err = mlx5_core_attach_mcg(dev->mdev, gid, ibqp->qp_num);
Eli Cohene126ba92013-07-07 17:25:49 +03004014 if (err)
4015 mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n",
4016 ibqp->qp_num, gid->raw);
4017
4018 return err;
4019}
4020
4021static int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
4022{
4023 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
4024 int err;
4025
Jack Morgenstein9603b612014-07-28 23:30:22 +03004026 err = mlx5_core_detach_mcg(dev->mdev, gid, ibqp->qp_num);
Eli Cohene126ba92013-07-07 17:25:49 +03004027 if (err)
4028 mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n",
4029 ibqp->qp_num, gid->raw);
4030
4031 return err;
4032}
4033
4034static int init_node_data(struct mlx5_ib_dev *dev)
4035{
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03004036 int err;
Eli Cohene126ba92013-07-07 17:25:49 +03004037
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03004038 err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc);
Eli Cohene126ba92013-07-07 17:25:49 +03004039 if (err)
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03004040 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03004041
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03004042 dev->mdev->rev_id = dev->mdev->pdev->revision;
Eli Cohene126ba92013-07-07 17:25:49 +03004043
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03004044 return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid);
Eli Cohene126ba92013-07-07 17:25:49 +03004045}
4046
4047static ssize_t show_fw_pages(struct device *device, struct device_attribute *attr,
4048 char *buf)
4049{
4050 struct mlx5_ib_dev *dev =
4051 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
4052
Jack Morgenstein9603b612014-07-28 23:30:22 +03004053 return sprintf(buf, "%d\n", dev->mdev->priv.fw_pages);
Eli Cohene126ba92013-07-07 17:25:49 +03004054}
4055
4056static ssize_t show_reg_pages(struct device *device,
4057 struct device_attribute *attr, char *buf)
4058{
4059 struct mlx5_ib_dev *dev =
4060 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
4061
Haggai Eran6aec21f2014-12-11 17:04:23 +02004062 return sprintf(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages));
Eli Cohene126ba92013-07-07 17:25:49 +03004063}
4064
4065static ssize_t show_hca(struct device *device, struct device_attribute *attr,
4066 char *buf)
4067{
4068 struct mlx5_ib_dev *dev =
4069 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
Jack Morgenstein9603b612014-07-28 23:30:22 +03004070 return sprintf(buf, "MT%d\n", dev->mdev->pdev->device);
Eli Cohene126ba92013-07-07 17:25:49 +03004071}
4072
Eli Cohene126ba92013-07-07 17:25:49 +03004073static ssize_t show_rev(struct device *device, struct device_attribute *attr,
4074 char *buf)
4075{
4076 struct mlx5_ib_dev *dev =
4077 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
Jack Morgenstein9603b612014-07-28 23:30:22 +03004078 return sprintf(buf, "%x\n", dev->mdev->rev_id);
Eli Cohene126ba92013-07-07 17:25:49 +03004079}
4080
4081static ssize_t show_board(struct device *device, struct device_attribute *attr,
4082 char *buf)
4083{
4084 struct mlx5_ib_dev *dev =
4085 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
4086 return sprintf(buf, "%.*s\n", MLX5_BOARD_ID_LEN,
Jack Morgenstein9603b612014-07-28 23:30:22 +03004087 dev->mdev->board_id);
Eli Cohene126ba92013-07-07 17:25:49 +03004088}
4089
4090static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03004091static DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL);
4092static DEVICE_ATTR(board_id, S_IRUGO, show_board, NULL);
4093static DEVICE_ATTR(fw_pages, S_IRUGO, show_fw_pages, NULL);
4094static DEVICE_ATTR(reg_pages, S_IRUGO, show_reg_pages, NULL);
4095
4096static struct device_attribute *mlx5_class_attributes[] = {
4097 &dev_attr_hw_rev,
Eli Cohene126ba92013-07-07 17:25:49 +03004098 &dev_attr_hca_type,
4099 &dev_attr_board_id,
4100 &dev_attr_fw_pages,
4101 &dev_attr_reg_pages,
4102};
4103
Haggai Eran7722f472016-02-29 15:45:07 +02004104static void pkey_change_handler(struct work_struct *work)
4105{
4106 struct mlx5_ib_port_resources *ports =
4107 container_of(work, struct mlx5_ib_port_resources,
4108 pkey_change_work);
4109
4110 mutex_lock(&ports->devr->mutex);
4111 mlx5_ib_gsi_pkey_change(ports->gsi);
4112 mutex_unlock(&ports->devr->mutex);
4113}
4114
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03004115static void mlx5_ib_handle_internal_error(struct mlx5_ib_dev *ibdev)
4116{
4117 struct mlx5_ib_qp *mqp;
4118 struct mlx5_ib_cq *send_mcq, *recv_mcq;
4119 struct mlx5_core_cq *mcq;
4120 struct list_head cq_armed_list;
4121 unsigned long flags_qp;
4122 unsigned long flags_cq;
4123 unsigned long flags;
4124
4125 INIT_LIST_HEAD(&cq_armed_list);
4126
4127 /* Go over qp list reside on that ibdev, sync with create/destroy qp.*/
4128 spin_lock_irqsave(&ibdev->reset_flow_resource_lock, flags);
4129 list_for_each_entry(mqp, &ibdev->qp_list, qps_list) {
4130 spin_lock_irqsave(&mqp->sq.lock, flags_qp);
4131 if (mqp->sq.tail != mqp->sq.head) {
4132 send_mcq = to_mcq(mqp->ibqp.send_cq);
4133 spin_lock_irqsave(&send_mcq->lock, flags_cq);
4134 if (send_mcq->mcq.comp &&
4135 mqp->ibqp.send_cq->comp_handler) {
4136 if (!send_mcq->mcq.reset_notify_added) {
4137 send_mcq->mcq.reset_notify_added = 1;
4138 list_add_tail(&send_mcq->mcq.reset_notify,
4139 &cq_armed_list);
4140 }
4141 }
4142 spin_unlock_irqrestore(&send_mcq->lock, flags_cq);
4143 }
4144 spin_unlock_irqrestore(&mqp->sq.lock, flags_qp);
4145 spin_lock_irqsave(&mqp->rq.lock, flags_qp);
4146 /* no handling is needed for SRQ */
4147 if (!mqp->ibqp.srq) {
4148 if (mqp->rq.tail != mqp->rq.head) {
4149 recv_mcq = to_mcq(mqp->ibqp.recv_cq);
4150 spin_lock_irqsave(&recv_mcq->lock, flags_cq);
4151 if (recv_mcq->mcq.comp &&
4152 mqp->ibqp.recv_cq->comp_handler) {
4153 if (!recv_mcq->mcq.reset_notify_added) {
4154 recv_mcq->mcq.reset_notify_added = 1;
4155 list_add_tail(&recv_mcq->mcq.reset_notify,
4156 &cq_armed_list);
4157 }
4158 }
4159 spin_unlock_irqrestore(&recv_mcq->lock,
4160 flags_cq);
4161 }
4162 }
4163 spin_unlock_irqrestore(&mqp->rq.lock, flags_qp);
4164 }
4165 /*At that point all inflight post send were put to be executed as of we
4166 * lock/unlock above locks Now need to arm all involved CQs.
4167 */
4168 list_for_each_entry(mcq, &cq_armed_list, reset_notify) {
4169 mcq->comp(mcq);
4170 }
4171 spin_unlock_irqrestore(&ibdev->reset_flow_resource_lock, flags);
4172}
4173
Maor Gottlieb03404e82017-05-30 10:29:13 +03004174static void delay_drop_handler(struct work_struct *work)
4175{
4176 int err;
4177 struct mlx5_ib_delay_drop *delay_drop =
4178 container_of(work, struct mlx5_ib_delay_drop,
4179 delay_drop_work);
4180
Maor Gottliebfe248c32017-05-30 10:29:14 +03004181 atomic_inc(&delay_drop->events_cnt);
4182
Maor Gottlieb03404e82017-05-30 10:29:13 +03004183 mutex_lock(&delay_drop->lock);
4184 err = mlx5_core_set_delay_drop(delay_drop->dev->mdev,
4185 delay_drop->timeout);
4186 if (err) {
4187 mlx5_ib_warn(delay_drop->dev, "Failed to set delay drop, timeout=%u\n",
4188 delay_drop->timeout);
4189 delay_drop->activate = false;
4190 }
4191 mutex_unlock(&delay_drop->lock);
4192}
4193
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02004194static void mlx5_ib_handle_event(struct work_struct *_work)
Eli Cohene126ba92013-07-07 17:25:49 +03004195{
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02004196 struct mlx5_ib_event_work *work =
4197 container_of(_work, struct mlx5_ib_event_work, work);
4198 struct mlx5_ib_dev *ibdev;
Eli Cohene126ba92013-07-07 17:25:49 +03004199 struct ib_event ibev;
Eli Cohendbaaff22016-10-27 16:36:44 +03004200 bool fatal = false;
Daniel Jurgensaba46212018-02-25 13:39:53 +02004201 u8 port = (u8)work->param;
Eli Cohene126ba92013-07-07 17:25:49 +03004202
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02004203 if (mlx5_core_is_mp_slave(work->dev)) {
4204 ibdev = mlx5_ib_get_ibdev_from_mpi(work->context);
4205 if (!ibdev)
4206 goto out;
4207 } else {
4208 ibdev = work->context;
4209 }
4210
4211 switch (work->event) {
Eli Cohene126ba92013-07-07 17:25:49 +03004212 case MLX5_DEV_EVENT_SYS_ERROR:
Eli Cohene126ba92013-07-07 17:25:49 +03004213 ibev.event = IB_EVENT_DEVICE_FATAL;
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03004214 mlx5_ib_handle_internal_error(ibdev);
Eli Cohendbaaff22016-10-27 16:36:44 +03004215 fatal = true;
Eli Cohene126ba92013-07-07 17:25:49 +03004216 break;
4217
4218 case MLX5_DEV_EVENT_PORT_UP:
Eli Cohene126ba92013-07-07 17:25:49 +03004219 case MLX5_DEV_EVENT_PORT_DOWN:
Noa Osherovich2788cf32016-06-04 15:15:29 +03004220 case MLX5_DEV_EVENT_PORT_INITIALIZED:
Aviv Heller5ec8c832016-09-18 20:48:00 +03004221 /* In RoCE, port up/down events are handled in
4222 * mlx5_netdev_event().
4223 */
4224 if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) ==
4225 IB_LINK_LAYER_ETHERNET)
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02004226 goto out;
Aviv Heller5ec8c832016-09-18 20:48:00 +03004227
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02004228 ibev.event = (work->event == MLX5_DEV_EVENT_PORT_UP) ?
Aviv Heller5ec8c832016-09-18 20:48:00 +03004229 IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
Eli Cohene126ba92013-07-07 17:25:49 +03004230 break;
4231
Eli Cohene126ba92013-07-07 17:25:49 +03004232 case MLX5_DEV_EVENT_LID_CHANGE:
4233 ibev.event = IB_EVENT_LID_CHANGE;
Eli Cohene126ba92013-07-07 17:25:49 +03004234 break;
4235
4236 case MLX5_DEV_EVENT_PKEY_CHANGE:
4237 ibev.event = IB_EVENT_PKEY_CHANGE;
Haggai Eran7722f472016-02-29 15:45:07 +02004238 schedule_work(&ibdev->devr.ports[port - 1].pkey_change_work);
Eli Cohene126ba92013-07-07 17:25:49 +03004239 break;
4240
4241 case MLX5_DEV_EVENT_GUID_CHANGE:
4242 ibev.event = IB_EVENT_GID_CHANGE;
Eli Cohene126ba92013-07-07 17:25:49 +03004243 break;
4244
4245 case MLX5_DEV_EVENT_CLIENT_REREG:
4246 ibev.event = IB_EVENT_CLIENT_REREGISTER;
Eli Cohene126ba92013-07-07 17:25:49 +03004247 break;
Maor Gottlieb03404e82017-05-30 10:29:13 +03004248 case MLX5_DEV_EVENT_DELAY_DROP_TIMEOUT:
4249 schedule_work(&ibdev->delay_drop.delay_drop_work);
4250 goto out;
Saeed Mahameedbdc37922016-09-29 19:35:38 +03004251 default:
Maor Gottlieb03404e82017-05-30 10:29:13 +03004252 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03004253 }
4254
4255 ibev.device = &ibdev->ib_dev;
4256 ibev.element.port_num = port;
4257
Daniel Jurgensaba46212018-02-25 13:39:53 +02004258 if (!rdma_is_port_valid(&ibdev->ib_dev, port)) {
Eli Cohena0c84c32013-09-11 16:35:27 +03004259 mlx5_ib_warn(ibdev, "warning: event on port %d\n", port);
Maor Gottlieb03404e82017-05-30 10:29:13 +03004260 goto out;
Eli Cohena0c84c32013-09-11 16:35:27 +03004261 }
4262
Eli Cohene126ba92013-07-07 17:25:49 +03004263 if (ibdev->ib_active)
4264 ib_dispatch_event(&ibev);
Eli Cohendbaaff22016-10-27 16:36:44 +03004265
4266 if (fatal)
4267 ibdev->ib_active = false;
Maor Gottlieb03404e82017-05-30 10:29:13 +03004268out:
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02004269 kfree(work);
4270}
4271
4272static void mlx5_ib_event(struct mlx5_core_dev *dev, void *context,
4273 enum mlx5_dev_event event, unsigned long param)
4274{
4275 struct mlx5_ib_event_work *work;
4276
4277 work = kmalloc(sizeof(*work), GFP_ATOMIC);
Leon Romanovsky10bea9c2018-01-19 13:07:11 +02004278 if (!work)
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02004279 return;
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02004280
Leon Romanovsky10bea9c2018-01-19 13:07:11 +02004281 INIT_WORK(&work->work, mlx5_ib_handle_event);
4282 work->dev = dev;
4283 work->param = param;
4284 work->context = context;
4285 work->event = event;
4286
4287 queue_work(mlx5_ib_event_wq, &work->work);
Eli Cohene126ba92013-07-07 17:25:49 +03004288}
4289
Maor Gottliebc43f1112017-01-18 14:10:33 +02004290static int set_has_smi_cap(struct mlx5_ib_dev *dev)
4291{
4292 struct mlx5_hca_vport_context vport_ctx;
4293 int err;
4294 int port;
4295
Daniel Jurgens508562d2018-01-04 17:25:34 +02004296 for (port = 1; port <= dev->num_ports; port++) {
Maor Gottliebc43f1112017-01-18 14:10:33 +02004297 dev->mdev->port_caps[port - 1].has_smi = false;
4298 if (MLX5_CAP_GEN(dev->mdev, port_type) ==
4299 MLX5_CAP_PORT_TYPE_IB) {
4300 if (MLX5_CAP_GEN(dev->mdev, ib_virt)) {
4301 err = mlx5_query_hca_vport_context(dev->mdev, 0,
4302 port, 0,
4303 &vport_ctx);
4304 if (err) {
4305 mlx5_ib_err(dev, "query_hca_vport_context for port=%d failed %d\n",
4306 port, err);
4307 return err;
4308 }
4309 dev->mdev->port_caps[port - 1].has_smi =
4310 vport_ctx.has_smi;
4311 } else {
4312 dev->mdev->port_caps[port - 1].has_smi = true;
4313 }
4314 }
4315 }
4316 return 0;
4317}
4318
Eli Cohene126ba92013-07-07 17:25:49 +03004319static void get_ext_port_caps(struct mlx5_ib_dev *dev)
4320{
4321 int port;
4322
Daniel Jurgens508562d2018-01-04 17:25:34 +02004323 for (port = 1; port <= dev->num_ports; port++)
Eli Cohene126ba92013-07-07 17:25:49 +03004324 mlx5_query_ext_port_caps(dev, port);
4325}
4326
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004327static int get_port_caps(struct mlx5_ib_dev *dev, u8 port)
Eli Cohene126ba92013-07-07 17:25:49 +03004328{
4329 struct ib_device_attr *dprops = NULL;
4330 struct ib_port_attr *pprops = NULL;
Dan Carpenterf614fc12015-01-12 11:56:58 +03004331 int err = -ENOMEM;
Matan Barak2528e332015-06-11 16:35:25 +03004332 struct ib_udata uhw = {.inlen = 0, .outlen = 0};
Eli Cohene126ba92013-07-07 17:25:49 +03004333
4334 pprops = kmalloc(sizeof(*pprops), GFP_KERNEL);
4335 if (!pprops)
4336 goto out;
4337
4338 dprops = kmalloc(sizeof(*dprops), GFP_KERNEL);
4339 if (!dprops)
4340 goto out;
4341
Maor Gottliebc43f1112017-01-18 14:10:33 +02004342 err = set_has_smi_cap(dev);
4343 if (err)
4344 goto out;
4345
Matan Barak2528e332015-06-11 16:35:25 +03004346 err = mlx5_ib_query_device(&dev->ib_dev, dprops, &uhw);
Eli Cohene126ba92013-07-07 17:25:49 +03004347 if (err) {
4348 mlx5_ib_warn(dev, "query_device failed %d\n", err);
4349 goto out;
4350 }
4351
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004352 memset(pprops, 0, sizeof(*pprops));
4353 err = mlx5_ib_query_port(&dev->ib_dev, port, pprops);
4354 if (err) {
4355 mlx5_ib_warn(dev, "query_port %d failed %d\n",
4356 port, err);
4357 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03004358 }
4359
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004360 dev->mdev->port_caps[port - 1].pkey_table_len =
4361 dprops->max_pkeys;
4362 dev->mdev->port_caps[port - 1].gid_table_len =
4363 pprops->gid_tbl_len;
4364 mlx5_ib_dbg(dev, "port %d: pkey_table_len %d, gid_table_len %d\n",
4365 port, dprops->max_pkeys, pprops->gid_tbl_len);
4366
Eli Cohene126ba92013-07-07 17:25:49 +03004367out:
4368 kfree(pprops);
4369 kfree(dprops);
4370
4371 return err;
4372}
4373
4374static void destroy_umrc_res(struct mlx5_ib_dev *dev)
4375{
4376 int err;
4377
4378 err = mlx5_mr_cache_cleanup(dev);
4379 if (err)
4380 mlx5_ib_warn(dev, "mr cache cleanup failed\n");
4381
Mark Bloch32927e22018-03-20 15:45:37 +02004382 if (dev->umrc.qp)
4383 mlx5_ib_destroy_qp(dev->umrc.qp);
4384 if (dev->umrc.cq)
4385 ib_free_cq(dev->umrc.cq);
4386 if (dev->umrc.pd)
4387 ib_dealloc_pd(dev->umrc.pd);
Eli Cohene126ba92013-07-07 17:25:49 +03004388}
4389
4390enum {
4391 MAX_UMR_WR = 128,
4392};
4393
4394static int create_umr_res(struct mlx5_ib_dev *dev)
4395{
4396 struct ib_qp_init_attr *init_attr = NULL;
4397 struct ib_qp_attr *attr = NULL;
4398 struct ib_pd *pd;
4399 struct ib_cq *cq;
4400 struct ib_qp *qp;
Eli Cohene126ba92013-07-07 17:25:49 +03004401 int ret;
4402
4403 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
4404 init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL);
4405 if (!attr || !init_attr) {
4406 ret = -ENOMEM;
4407 goto error_0;
4408 }
4409
Christoph Hellwiged082d32016-09-05 12:56:17 +02004410 pd = ib_alloc_pd(&dev->ib_dev, 0);
Eli Cohene126ba92013-07-07 17:25:49 +03004411 if (IS_ERR(pd)) {
4412 mlx5_ib_dbg(dev, "Couldn't create PD for sync UMR QP\n");
4413 ret = PTR_ERR(pd);
4414 goto error_0;
4415 }
4416
Christoph Hellwigadd08d72016-03-03 09:38:22 +01004417 cq = ib_alloc_cq(&dev->ib_dev, NULL, 128, 0, IB_POLL_SOFTIRQ);
Eli Cohene126ba92013-07-07 17:25:49 +03004418 if (IS_ERR(cq)) {
4419 mlx5_ib_dbg(dev, "Couldn't create CQ for sync UMR QP\n");
4420 ret = PTR_ERR(cq);
4421 goto error_2;
4422 }
Eli Cohene126ba92013-07-07 17:25:49 +03004423
4424 init_attr->send_cq = cq;
4425 init_attr->recv_cq = cq;
4426 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
4427 init_attr->cap.max_send_wr = MAX_UMR_WR;
4428 init_attr->cap.max_send_sge = 1;
4429 init_attr->qp_type = MLX5_IB_QPT_REG_UMR;
4430 init_attr->port_num = 1;
4431 qp = mlx5_ib_create_qp(pd, init_attr, NULL);
4432 if (IS_ERR(qp)) {
4433 mlx5_ib_dbg(dev, "Couldn't create sync UMR QP\n");
4434 ret = PTR_ERR(qp);
4435 goto error_3;
4436 }
4437 qp->device = &dev->ib_dev;
4438 qp->real_qp = qp;
4439 qp->uobject = NULL;
4440 qp->qp_type = MLX5_IB_QPT_REG_UMR;
Majd Dibbiny31fde032017-10-30 14:23:13 +02004441 qp->send_cq = init_attr->send_cq;
4442 qp->recv_cq = init_attr->recv_cq;
Eli Cohene126ba92013-07-07 17:25:49 +03004443
4444 attr->qp_state = IB_QPS_INIT;
4445 attr->port_num = 1;
4446 ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_PKEY_INDEX |
4447 IB_QP_PORT, NULL);
4448 if (ret) {
4449 mlx5_ib_dbg(dev, "Couldn't modify UMR QP\n");
4450 goto error_4;
4451 }
4452
4453 memset(attr, 0, sizeof(*attr));
4454 attr->qp_state = IB_QPS_RTR;
4455 attr->path_mtu = IB_MTU_256;
4456
4457 ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
4458 if (ret) {
4459 mlx5_ib_dbg(dev, "Couldn't modify umr QP to rtr\n");
4460 goto error_4;
4461 }
4462
4463 memset(attr, 0, sizeof(*attr));
4464 attr->qp_state = IB_QPS_RTS;
4465 ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
4466 if (ret) {
4467 mlx5_ib_dbg(dev, "Couldn't modify umr QP to rts\n");
4468 goto error_4;
4469 }
4470
4471 dev->umrc.qp = qp;
4472 dev->umrc.cq = cq;
Eli Cohene126ba92013-07-07 17:25:49 +03004473 dev->umrc.pd = pd;
4474
4475 sema_init(&dev->umrc.sem, MAX_UMR_WR);
4476 ret = mlx5_mr_cache_init(dev);
4477 if (ret) {
4478 mlx5_ib_warn(dev, "mr cache init failed %d\n", ret);
4479 goto error_4;
4480 }
4481
4482 kfree(attr);
4483 kfree(init_attr);
4484
4485 return 0;
4486
4487error_4:
4488 mlx5_ib_destroy_qp(qp);
Mark Bloch32927e22018-03-20 15:45:37 +02004489 dev->umrc.qp = NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03004490
4491error_3:
Christoph Hellwigadd08d72016-03-03 09:38:22 +01004492 ib_free_cq(cq);
Mark Bloch32927e22018-03-20 15:45:37 +02004493 dev->umrc.cq = NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03004494
4495error_2:
Eli Cohene126ba92013-07-07 17:25:49 +03004496 ib_dealloc_pd(pd);
Mark Bloch32927e22018-03-20 15:45:37 +02004497 dev->umrc.pd = NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03004498
4499error_0:
4500 kfree(attr);
4501 kfree(init_attr);
4502 return ret;
4503}
4504
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03004505static u8 mlx5_get_umr_fence(u8 umr_fence_cap)
4506{
4507 switch (umr_fence_cap) {
4508 case MLX5_CAP_UMR_FENCE_NONE:
4509 return MLX5_FENCE_MODE_NONE;
4510 case MLX5_CAP_UMR_FENCE_SMALL:
4511 return MLX5_FENCE_MODE_INITIATOR_SMALL;
4512 default:
4513 return MLX5_FENCE_MODE_STRONG_ORDERING;
4514 }
4515}
4516
Eli Cohene126ba92013-07-07 17:25:49 +03004517static int create_dev_resources(struct mlx5_ib_resources *devr)
4518{
4519 struct ib_srq_init_attr attr;
4520 struct mlx5_ib_dev *dev;
Matan Barakbcf4c1e2015-06-11 16:35:20 +03004521 struct ib_cq_init_attr cq_attr = {.cqe = 1};
Haggai Eran7722f472016-02-29 15:45:07 +02004522 int port;
Eli Cohene126ba92013-07-07 17:25:49 +03004523 int ret = 0;
4524
4525 dev = container_of(devr, struct mlx5_ib_dev, devr);
4526
Haggai Erand16e91d2016-02-29 15:45:05 +02004527 mutex_init(&devr->mutex);
4528
Eli Cohene126ba92013-07-07 17:25:49 +03004529 devr->p0 = mlx5_ib_alloc_pd(&dev->ib_dev, NULL, NULL);
4530 if (IS_ERR(devr->p0)) {
4531 ret = PTR_ERR(devr->p0);
4532 goto error0;
4533 }
4534 devr->p0->device = &dev->ib_dev;
4535 devr->p0->uobject = NULL;
4536 atomic_set(&devr->p0->usecnt, 0);
4537
Matan Barakbcf4c1e2015-06-11 16:35:20 +03004538 devr->c0 = mlx5_ib_create_cq(&dev->ib_dev, &cq_attr, NULL, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03004539 if (IS_ERR(devr->c0)) {
4540 ret = PTR_ERR(devr->c0);
4541 goto error1;
4542 }
4543 devr->c0->device = &dev->ib_dev;
4544 devr->c0->uobject = NULL;
4545 devr->c0->comp_handler = NULL;
4546 devr->c0->event_handler = NULL;
4547 devr->c0->cq_context = NULL;
4548 atomic_set(&devr->c0->usecnt, 0);
4549
4550 devr->x0 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
4551 if (IS_ERR(devr->x0)) {
4552 ret = PTR_ERR(devr->x0);
4553 goto error2;
4554 }
4555 devr->x0->device = &dev->ib_dev;
4556 devr->x0->inode = NULL;
4557 atomic_set(&devr->x0->usecnt, 0);
4558 mutex_init(&devr->x0->tgt_qp_mutex);
4559 INIT_LIST_HEAD(&devr->x0->tgt_qp_list);
4560
4561 devr->x1 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
4562 if (IS_ERR(devr->x1)) {
4563 ret = PTR_ERR(devr->x1);
4564 goto error3;
4565 }
4566 devr->x1->device = &dev->ib_dev;
4567 devr->x1->inode = NULL;
4568 atomic_set(&devr->x1->usecnt, 0);
4569 mutex_init(&devr->x1->tgt_qp_mutex);
4570 INIT_LIST_HEAD(&devr->x1->tgt_qp_list);
4571
4572 memset(&attr, 0, sizeof(attr));
4573 attr.attr.max_sge = 1;
4574 attr.attr.max_wr = 1;
4575 attr.srq_type = IB_SRQT_XRC;
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03004576 attr.ext.cq = devr->c0;
Eli Cohene126ba92013-07-07 17:25:49 +03004577 attr.ext.xrc.xrcd = devr->x0;
4578
4579 devr->s0 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
4580 if (IS_ERR(devr->s0)) {
4581 ret = PTR_ERR(devr->s0);
4582 goto error4;
4583 }
4584 devr->s0->device = &dev->ib_dev;
4585 devr->s0->pd = devr->p0;
4586 devr->s0->uobject = NULL;
4587 devr->s0->event_handler = NULL;
4588 devr->s0->srq_context = NULL;
4589 devr->s0->srq_type = IB_SRQT_XRC;
4590 devr->s0->ext.xrc.xrcd = devr->x0;
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03004591 devr->s0->ext.cq = devr->c0;
Eli Cohene126ba92013-07-07 17:25:49 +03004592 atomic_inc(&devr->s0->ext.xrc.xrcd->usecnt);
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03004593 atomic_inc(&devr->s0->ext.cq->usecnt);
Eli Cohene126ba92013-07-07 17:25:49 +03004594 atomic_inc(&devr->p0->usecnt);
4595 atomic_set(&devr->s0->usecnt, 0);
4596
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03004597 memset(&attr, 0, sizeof(attr));
4598 attr.attr.max_sge = 1;
4599 attr.attr.max_wr = 1;
4600 attr.srq_type = IB_SRQT_BASIC;
4601 devr->s1 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
4602 if (IS_ERR(devr->s1)) {
4603 ret = PTR_ERR(devr->s1);
4604 goto error5;
4605 }
4606 devr->s1->device = &dev->ib_dev;
4607 devr->s1->pd = devr->p0;
4608 devr->s1->uobject = NULL;
4609 devr->s1->event_handler = NULL;
4610 devr->s1->srq_context = NULL;
4611 devr->s1->srq_type = IB_SRQT_BASIC;
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03004612 devr->s1->ext.cq = devr->c0;
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03004613 atomic_inc(&devr->p0->usecnt);
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03004614 atomic_set(&devr->s1->usecnt, 0);
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03004615
Haggai Eran7722f472016-02-29 15:45:07 +02004616 for (port = 0; port < ARRAY_SIZE(devr->ports); ++port) {
4617 INIT_WORK(&devr->ports[port].pkey_change_work,
4618 pkey_change_handler);
4619 devr->ports[port].devr = devr;
4620 }
4621
Eli Cohene126ba92013-07-07 17:25:49 +03004622 return 0;
4623
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03004624error5:
4625 mlx5_ib_destroy_srq(devr->s0);
Eli Cohene126ba92013-07-07 17:25:49 +03004626error4:
4627 mlx5_ib_dealloc_xrcd(devr->x1);
4628error3:
4629 mlx5_ib_dealloc_xrcd(devr->x0);
4630error2:
4631 mlx5_ib_destroy_cq(devr->c0);
4632error1:
4633 mlx5_ib_dealloc_pd(devr->p0);
4634error0:
4635 return ret;
4636}
4637
4638static void destroy_dev_resources(struct mlx5_ib_resources *devr)
4639{
Haggai Eran7722f472016-02-29 15:45:07 +02004640 struct mlx5_ib_dev *dev =
4641 container_of(devr, struct mlx5_ib_dev, devr);
4642 int port;
4643
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03004644 mlx5_ib_destroy_srq(devr->s1);
Eli Cohene126ba92013-07-07 17:25:49 +03004645 mlx5_ib_destroy_srq(devr->s0);
4646 mlx5_ib_dealloc_xrcd(devr->x0);
4647 mlx5_ib_dealloc_xrcd(devr->x1);
4648 mlx5_ib_destroy_cq(devr->c0);
4649 mlx5_ib_dealloc_pd(devr->p0);
Haggai Eran7722f472016-02-29 15:45:07 +02004650
4651 /* Make sure no change P_Key work items are still executing */
4652 for (port = 0; port < dev->num_ports; ++port)
4653 cancel_work_sync(&devr->ports[port].pkey_change_work);
Eli Cohene126ba92013-07-07 17:25:49 +03004654}
4655
Artemy Kovalyovb02289b2018-07-04 15:57:50 +03004656static u32 get_core_cap_flags(struct ib_device *ibdev,
4657 struct mlx5_hca_vport_context *rep)
Achiad Shochate53505a2015-12-23 18:47:25 +02004658{
4659 struct mlx5_ib_dev *dev = to_mdev(ibdev);
4660 enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, 1);
4661 u8 l3_type_cap = MLX5_CAP_ROCE(dev->mdev, l3_type);
4662 u8 roce_version_cap = MLX5_CAP_ROCE(dev->mdev, roce_version);
Daniel Jurgens85c7c012018-01-04 17:25:43 +02004663 bool raw_support = !mlx5_core_mp_enabled(dev->mdev);
Achiad Shochate53505a2015-12-23 18:47:25 +02004664 u32 ret = 0;
4665
Artemy Kovalyovb02289b2018-07-04 15:57:50 +03004666 if (rep->grh_required)
4667 ret |= RDMA_CORE_CAP_IB_GRH_REQUIRED;
4668
Achiad Shochate53505a2015-12-23 18:47:25 +02004669 if (ll == IB_LINK_LAYER_INFINIBAND)
Artemy Kovalyovb02289b2018-07-04 15:57:50 +03004670 return ret | RDMA_CORE_PORT_IBA_IB;
Achiad Shochate53505a2015-12-23 18:47:25 +02004671
Daniel Jurgens85c7c012018-01-04 17:25:43 +02004672 if (raw_support)
Artemy Kovalyovb02289b2018-07-04 15:57:50 +03004673 ret |= RDMA_CORE_PORT_RAW_PACKET;
Or Gerlitz72cd5712017-01-24 13:02:36 +02004674
Achiad Shochate53505a2015-12-23 18:47:25 +02004675 if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV4_CAP))
Or Gerlitz72cd5712017-01-24 13:02:36 +02004676 return ret;
Achiad Shochate53505a2015-12-23 18:47:25 +02004677
4678 if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV6_CAP))
Or Gerlitz72cd5712017-01-24 13:02:36 +02004679 return ret;
Achiad Shochate53505a2015-12-23 18:47:25 +02004680
4681 if (roce_version_cap & MLX5_ROCE_VERSION_1_CAP)
4682 ret |= RDMA_CORE_PORT_IBA_ROCE;
4683
4684 if (roce_version_cap & MLX5_ROCE_VERSION_2_CAP)
4685 ret |= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
4686
4687 return ret;
4688}
4689
Ira Weiny77386132015-05-13 20:02:58 -04004690static int mlx5_port_immutable(struct ib_device *ibdev, u8 port_num,
4691 struct ib_port_immutable *immutable)
4692{
4693 struct ib_port_attr attr;
Or Gerlitzca5b91d2016-11-27 16:51:36 +02004694 struct mlx5_ib_dev *dev = to_mdev(ibdev);
4695 enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, port_num);
Artemy Kovalyovb02289b2018-07-04 15:57:50 +03004696 struct mlx5_hca_vport_context rep = {0};
Ira Weiny77386132015-05-13 20:02:58 -04004697 int err;
4698
Or Gerlitzc4550c62017-01-24 13:02:39 +02004699 err = ib_query_port(ibdev, port_num, &attr);
Ira Weiny77386132015-05-13 20:02:58 -04004700 if (err)
4701 return err;
4702
Artemy Kovalyovb02289b2018-07-04 15:57:50 +03004703 if (ll == IB_LINK_LAYER_INFINIBAND) {
4704 err = mlx5_query_hca_vport_context(dev->mdev, 0, port_num, 0,
4705 &rep);
4706 if (err)
4707 return err;
4708 }
4709
Ira Weiny77386132015-05-13 20:02:58 -04004710 immutable->pkey_tbl_len = attr.pkey_tbl_len;
4711 immutable->gid_tbl_len = attr.gid_tbl_len;
Artemy Kovalyovb02289b2018-07-04 15:57:50 +03004712 immutable->core_cap_flags = get_core_cap_flags(ibdev, &rep);
Or Gerlitzca5b91d2016-11-27 16:51:36 +02004713 if ((ll == IB_LINK_LAYER_INFINIBAND) || MLX5_CAP_GEN(dev->mdev, roce))
4714 immutable->max_mad_size = IB_MGMT_MAD_SIZE;
Ira Weiny77386132015-05-13 20:02:58 -04004715
4716 return 0;
4717}
4718
Mark Bloch8e6efa32017-11-06 12:22:13 +00004719static int mlx5_port_rep_immutable(struct ib_device *ibdev, u8 port_num,
4720 struct ib_port_immutable *immutable)
4721{
4722 struct ib_port_attr attr;
4723 int err;
4724
4725 immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET;
4726
4727 err = ib_query_port(ibdev, port_num, &attr);
4728 if (err)
4729 return err;
4730
4731 immutable->pkey_tbl_len = attr.pkey_tbl_len;
4732 immutable->gid_tbl_len = attr.gid_tbl_len;
4733 immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET;
4734
4735 return 0;
4736}
4737
Leon Romanovsky9abb0d12017-06-27 16:49:53 +03004738static void get_dev_fw_str(struct ib_device *ibdev, char *str)
Ira Weinyc7342822016-06-15 02:22:01 -04004739{
4740 struct mlx5_ib_dev *dev =
4741 container_of(ibdev, struct mlx5_ib_dev, ib_dev);
Leon Romanovsky9abb0d12017-06-27 16:49:53 +03004742 snprintf(str, IB_FW_VERSION_NAME_MAX, "%d.%d.%04d",
4743 fw_rev_maj(dev->mdev), fw_rev_min(dev->mdev),
4744 fw_rev_sub(dev->mdev));
Ira Weinyc7342822016-06-15 02:22:01 -04004745}
4746
Or Gerlitz45f95ac2016-11-27 16:51:35 +02004747static int mlx5_eth_lag_init(struct mlx5_ib_dev *dev)
Aviv Heller9ef9c642016-09-18 20:48:01 +03004748{
4749 struct mlx5_core_dev *mdev = dev->mdev;
4750 struct mlx5_flow_namespace *ns = mlx5_get_flow_namespace(mdev,
4751 MLX5_FLOW_NAMESPACE_LAG);
4752 struct mlx5_flow_table *ft;
4753 int err;
4754
4755 if (!ns || !mlx5_lag_is_active(mdev))
4756 return 0;
4757
4758 err = mlx5_cmd_create_vport_lag(mdev);
4759 if (err)
4760 return err;
4761
4762 ft = mlx5_create_lag_demux_flow_table(ns, 0, 0);
4763 if (IS_ERR(ft)) {
4764 err = PTR_ERR(ft);
4765 goto err_destroy_vport_lag;
4766 }
4767
Mark Bloch9a4ca382018-01-16 14:42:35 +00004768 dev->flow_db->lag_demux_ft = ft;
Aviv Heller9ef9c642016-09-18 20:48:01 +03004769 return 0;
4770
4771err_destroy_vport_lag:
4772 mlx5_cmd_destroy_vport_lag(mdev);
4773 return err;
4774}
4775
Or Gerlitz45f95ac2016-11-27 16:51:35 +02004776static void mlx5_eth_lag_cleanup(struct mlx5_ib_dev *dev)
Aviv Heller9ef9c642016-09-18 20:48:01 +03004777{
4778 struct mlx5_core_dev *mdev = dev->mdev;
4779
Mark Bloch9a4ca382018-01-16 14:42:35 +00004780 if (dev->flow_db->lag_demux_ft) {
4781 mlx5_destroy_flow_table(dev->flow_db->lag_demux_ft);
4782 dev->flow_db->lag_demux_ft = NULL;
Aviv Heller9ef9c642016-09-18 20:48:01 +03004783
4784 mlx5_cmd_destroy_vport_lag(mdev);
4785 }
4786}
4787
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +02004788static int mlx5_add_netdev_notifier(struct mlx5_ib_dev *dev, u8 port_num)
Achiad Shochatfc24fc52015-12-23 18:47:17 +02004789{
Achiad Shochate53505a2015-12-23 18:47:25 +02004790 int err;
4791
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +02004792 dev->roce[port_num].nb.notifier_call = mlx5_netdev_event;
4793 err = register_netdevice_notifier(&dev->roce[port_num].nb);
Aviv Heller5ec8c832016-09-18 20:48:00 +03004794 if (err) {
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +02004795 dev->roce[port_num].nb.notifier_call = NULL;
Achiad Shochate53505a2015-12-23 18:47:25 +02004796 return err;
Aviv Heller5ec8c832016-09-18 20:48:00 +03004797 }
Achiad Shochate53505a2015-12-23 18:47:25 +02004798
Or Gerlitzd012f5d2016-11-27 16:51:34 +02004799 return 0;
4800}
Achiad Shochate53505a2015-12-23 18:47:25 +02004801
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +02004802static void mlx5_remove_netdev_notifier(struct mlx5_ib_dev *dev, u8 port_num)
Eli Cohene126ba92013-07-07 17:25:49 +03004803{
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +02004804 if (dev->roce[port_num].nb.notifier_call) {
4805 unregister_netdevice_notifier(&dev->roce[port_num].nb);
4806 dev->roce[port_num].nb.notifier_call = NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03004807 }
4808}
4809
Leon Romanovskye3f1ed12018-07-08 12:55:43 +03004810static int mlx5_enable_eth(struct mlx5_ib_dev *dev)
Eli Cohene126ba92013-07-07 17:25:49 +03004811{
Eli Cohene126ba92013-07-07 17:25:49 +03004812 int err;
4813
Or Gerlitzca5b91d2016-11-27 16:51:36 +02004814 if (MLX5_CAP_GEN(dev->mdev, roce)) {
4815 err = mlx5_nic_vport_enable_roce(dev->mdev);
4816 if (err)
Mark Bloch8e6efa32017-11-06 12:22:13 +00004817 return err;
Or Gerlitzca5b91d2016-11-27 16:51:36 +02004818 }
Achiad Shochate53505a2015-12-23 18:47:25 +02004819
Or Gerlitz45f95ac2016-11-27 16:51:35 +02004820 err = mlx5_eth_lag_init(dev);
Aviv Heller9ef9c642016-09-18 20:48:01 +03004821 if (err)
4822 goto err_disable_roce;
4823
Achiad Shochate53505a2015-12-23 18:47:25 +02004824 return 0;
4825
Aviv Heller9ef9c642016-09-18 20:48:01 +03004826err_disable_roce:
Or Gerlitzca5b91d2016-11-27 16:51:36 +02004827 if (MLX5_CAP_GEN(dev->mdev, roce))
4828 mlx5_nic_vport_disable_roce(dev->mdev);
Aviv Heller9ef9c642016-09-18 20:48:01 +03004829
Achiad Shochate53505a2015-12-23 18:47:25 +02004830 return err;
Achiad Shochatfc24fc52015-12-23 18:47:17 +02004831}
4832
Or Gerlitz45f95ac2016-11-27 16:51:35 +02004833static void mlx5_disable_eth(struct mlx5_ib_dev *dev)
Achiad Shochatfc24fc52015-12-23 18:47:17 +02004834{
Or Gerlitz45f95ac2016-11-27 16:51:35 +02004835 mlx5_eth_lag_cleanup(dev);
Or Gerlitzca5b91d2016-11-27 16:51:36 +02004836 if (MLX5_CAP_GEN(dev->mdev, roce))
4837 mlx5_nic_vport_disable_roce(dev->mdev);
Achiad Shochatfc24fc52015-12-23 18:47:17 +02004838}
4839
Parav Pandite1f24a72017-04-16 07:29:29 +03004840struct mlx5_ib_counter {
Kamal Heib7c16f472017-01-18 15:25:09 +02004841 const char *name;
4842 size_t offset;
4843};
4844
4845#define INIT_Q_COUNTER(_name) \
4846 { .name = #_name, .offset = MLX5_BYTE_OFF(query_q_counter_out, _name)}
4847
Parav Pandite1f24a72017-04-16 07:29:29 +03004848static const struct mlx5_ib_counter basic_q_cnts[] = {
Kamal Heib7c16f472017-01-18 15:25:09 +02004849 INIT_Q_COUNTER(rx_write_requests),
4850 INIT_Q_COUNTER(rx_read_requests),
4851 INIT_Q_COUNTER(rx_atomic_requests),
4852 INIT_Q_COUNTER(out_of_buffer),
4853};
4854
Parav Pandite1f24a72017-04-16 07:29:29 +03004855static const struct mlx5_ib_counter out_of_seq_q_cnts[] = {
Kamal Heib7c16f472017-01-18 15:25:09 +02004856 INIT_Q_COUNTER(out_of_sequence),
4857};
4858
Parav Pandite1f24a72017-04-16 07:29:29 +03004859static const struct mlx5_ib_counter retrans_q_cnts[] = {
Kamal Heib7c16f472017-01-18 15:25:09 +02004860 INIT_Q_COUNTER(duplicate_request),
4861 INIT_Q_COUNTER(rnr_nak_retry_err),
4862 INIT_Q_COUNTER(packet_seq_err),
4863 INIT_Q_COUNTER(implied_nak_seq_err),
4864 INIT_Q_COUNTER(local_ack_timeout_err),
4865};
4866
Parav Pandite1f24a72017-04-16 07:29:29 +03004867#define INIT_CONG_COUNTER(_name) \
4868 { .name = #_name, .offset = \
4869 MLX5_BYTE_OFF(query_cong_statistics_out, _name ## _high)}
4870
4871static const struct mlx5_ib_counter cong_cnts[] = {
4872 INIT_CONG_COUNTER(rp_cnp_ignored),
4873 INIT_CONG_COUNTER(rp_cnp_handled),
4874 INIT_CONG_COUNTER(np_ecn_marked_roce_packets),
4875 INIT_CONG_COUNTER(np_cnp_sent),
4876};
4877
Parav Pandit58dcb602017-06-19 07:19:37 +03004878static const struct mlx5_ib_counter extended_err_cnts[] = {
4879 INIT_Q_COUNTER(resp_local_length_error),
4880 INIT_Q_COUNTER(resp_cqe_error),
4881 INIT_Q_COUNTER(req_cqe_error),
4882 INIT_Q_COUNTER(req_remote_invalid_request),
4883 INIT_Q_COUNTER(req_remote_access_errors),
4884 INIT_Q_COUNTER(resp_remote_access_errors),
4885 INIT_Q_COUNTER(resp_cqe_flush_error),
4886 INIT_Q_COUNTER(req_cqe_flush_error),
4887};
4888
Talat Batheesh9f876f32018-06-21 15:37:56 +03004889#define INIT_EXT_PPCNT_COUNTER(_name) \
4890 { .name = #_name, .offset = \
4891 MLX5_BYTE_OFF(ppcnt_reg, \
4892 counter_set.eth_extended_cntrs_grp_data_layout._name##_high)}
4893
4894static const struct mlx5_ib_counter ext_ppcnt_cnts[] = {
4895 INIT_EXT_PPCNT_COUNTER(rx_icrc_encapsulated),
4896};
4897
Parav Pandite1f24a72017-04-16 07:29:29 +03004898static void mlx5_ib_dealloc_counters(struct mlx5_ib_dev *dev)
Mark Bloch0837e862016-06-17 15:10:55 +03004899{
Daniel Jurgensaac44922018-01-04 17:25:40 +02004900 int i;
Mark Bloch0837e862016-06-17 15:10:55 +03004901
Kamal Heib7c16f472017-01-18 15:25:09 +02004902 for (i = 0; i < dev->num_ports; i++) {
Parav Pandit921c0f52018-07-08 13:40:30 +03004903 if (dev->port[i].cnts.set_id_valid)
Daniel Jurgensaac44922018-01-04 17:25:40 +02004904 mlx5_core_dealloc_q_counter(dev->mdev,
4905 dev->port[i].cnts.set_id);
Parav Pandite1f24a72017-04-16 07:29:29 +03004906 kfree(dev->port[i].cnts.names);
4907 kfree(dev->port[i].cnts.offsets);
Kamal Heib7c16f472017-01-18 15:25:09 +02004908 }
4909}
4910
Parav Pandite1f24a72017-04-16 07:29:29 +03004911static int __mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev,
4912 struct mlx5_ib_counters *cnts)
Kamal Heib7c16f472017-01-18 15:25:09 +02004913{
4914 u32 num_counters;
4915
4916 num_counters = ARRAY_SIZE(basic_q_cnts);
4917
4918 if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt))
4919 num_counters += ARRAY_SIZE(out_of_seq_q_cnts);
4920
4921 if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters))
4922 num_counters += ARRAY_SIZE(retrans_q_cnts);
Parav Pandit58dcb602017-06-19 07:19:37 +03004923
4924 if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters))
4925 num_counters += ARRAY_SIZE(extended_err_cnts);
4926
Parav Pandite1f24a72017-04-16 07:29:29 +03004927 cnts->num_q_counters = num_counters;
Kamal Heib7c16f472017-01-18 15:25:09 +02004928
Parav Pandite1f24a72017-04-16 07:29:29 +03004929 if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
4930 cnts->num_cong_counters = ARRAY_SIZE(cong_cnts);
4931 num_counters += ARRAY_SIZE(cong_cnts);
4932 }
Talat Batheesh9f876f32018-06-21 15:37:56 +03004933 if (MLX5_CAP_PCAM_FEATURE(dev->mdev, rx_icrc_encapsulated_counter)) {
4934 cnts->num_ext_ppcnt_counters = ARRAY_SIZE(ext_ppcnt_cnts);
4935 num_counters += ARRAY_SIZE(ext_ppcnt_cnts);
4936 }
Parav Pandite1f24a72017-04-16 07:29:29 +03004937 cnts->names = kcalloc(num_counters, sizeof(cnts->names), GFP_KERNEL);
4938 if (!cnts->names)
Kamal Heib7c16f472017-01-18 15:25:09 +02004939 return -ENOMEM;
4940
Parav Pandite1f24a72017-04-16 07:29:29 +03004941 cnts->offsets = kcalloc(num_counters,
4942 sizeof(cnts->offsets), GFP_KERNEL);
4943 if (!cnts->offsets)
Kamal Heib7c16f472017-01-18 15:25:09 +02004944 goto err_names;
4945
Kamal Heib7c16f472017-01-18 15:25:09 +02004946 return 0;
4947
4948err_names:
Parav Pandite1f24a72017-04-16 07:29:29 +03004949 kfree(cnts->names);
Daniel Jurgensaac44922018-01-04 17:25:40 +02004950 cnts->names = NULL;
Kamal Heib7c16f472017-01-18 15:25:09 +02004951 return -ENOMEM;
4952}
4953
Parav Pandite1f24a72017-04-16 07:29:29 +03004954static void mlx5_ib_fill_counters(struct mlx5_ib_dev *dev,
4955 const char **names,
4956 size_t *offsets)
Kamal Heib7c16f472017-01-18 15:25:09 +02004957{
4958 int i;
4959 int j = 0;
4960
4961 for (i = 0; i < ARRAY_SIZE(basic_q_cnts); i++, j++) {
4962 names[j] = basic_q_cnts[i].name;
4963 offsets[j] = basic_q_cnts[i].offset;
4964 }
4965
4966 if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt)) {
4967 for (i = 0; i < ARRAY_SIZE(out_of_seq_q_cnts); i++, j++) {
4968 names[j] = out_of_seq_q_cnts[i].name;
4969 offsets[j] = out_of_seq_q_cnts[i].offset;
4970 }
4971 }
4972
4973 if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters)) {
4974 for (i = 0; i < ARRAY_SIZE(retrans_q_cnts); i++, j++) {
4975 names[j] = retrans_q_cnts[i].name;
4976 offsets[j] = retrans_q_cnts[i].offset;
4977 }
4978 }
Parav Pandite1f24a72017-04-16 07:29:29 +03004979
Parav Pandit58dcb602017-06-19 07:19:37 +03004980 if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters)) {
4981 for (i = 0; i < ARRAY_SIZE(extended_err_cnts); i++, j++) {
4982 names[j] = extended_err_cnts[i].name;
4983 offsets[j] = extended_err_cnts[i].offset;
4984 }
4985 }
4986
Parav Pandite1f24a72017-04-16 07:29:29 +03004987 if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
4988 for (i = 0; i < ARRAY_SIZE(cong_cnts); i++, j++) {
4989 names[j] = cong_cnts[i].name;
4990 offsets[j] = cong_cnts[i].offset;
4991 }
4992 }
Talat Batheesh9f876f32018-06-21 15:37:56 +03004993
4994 if (MLX5_CAP_PCAM_FEATURE(dev->mdev, rx_icrc_encapsulated_counter)) {
4995 for (i = 0; i < ARRAY_SIZE(ext_ppcnt_cnts); i++, j++) {
4996 names[j] = ext_ppcnt_cnts[i].name;
4997 offsets[j] = ext_ppcnt_cnts[i].offset;
4998 }
4999 }
Mark Bloch0837e862016-06-17 15:10:55 +03005000}
5001
Parav Pandite1f24a72017-04-16 07:29:29 +03005002static int mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev)
Mark Bloch0837e862016-06-17 15:10:55 +03005003{
Daniel Jurgensaac44922018-01-04 17:25:40 +02005004 int err = 0;
Mark Bloch0837e862016-06-17 15:10:55 +03005005 int i;
Mark Bloch0837e862016-06-17 15:10:55 +03005006
5007 for (i = 0; i < dev->num_ports; i++) {
Daniel Jurgensaac44922018-01-04 17:25:40 +02005008 err = __mlx5_ib_alloc_counters(dev, &dev->port[i].cnts);
5009 if (err)
5010 goto err_alloc;
Kamal Heib7c16f472017-01-18 15:25:09 +02005011
Daniel Jurgensaac44922018-01-04 17:25:40 +02005012 mlx5_ib_fill_counters(dev, dev->port[i].cnts.names,
5013 dev->port[i].cnts.offsets);
5014
5015 err = mlx5_core_alloc_q_counter(dev->mdev,
5016 &dev->port[i].cnts.set_id);
5017 if (err) {
Mark Bloch0837e862016-06-17 15:10:55 +03005018 mlx5_ib_warn(dev,
5019 "couldn't allocate queue counter for port %d, err %d\n",
Daniel Jurgensaac44922018-01-04 17:25:40 +02005020 i + 1, err);
5021 goto err_alloc;
Mark Bloch0837e862016-06-17 15:10:55 +03005022 }
Daniel Jurgensaac44922018-01-04 17:25:40 +02005023 dev->port[i].cnts.set_id_valid = true;
Mark Bloch0837e862016-06-17 15:10:55 +03005024 }
5025
5026 return 0;
5027
Daniel Jurgensaac44922018-01-04 17:25:40 +02005028err_alloc:
5029 mlx5_ib_dealloc_counters(dev);
5030 return err;
Mark Bloch0837e862016-06-17 15:10:55 +03005031}
5032
Mark Bloch0ad17a82016-06-17 15:10:56 +03005033static struct rdma_hw_stats *mlx5_ib_alloc_hw_stats(struct ib_device *ibdev,
5034 u8 port_num)
5035{
Kamal Heib7c16f472017-01-18 15:25:09 +02005036 struct mlx5_ib_dev *dev = to_mdev(ibdev);
5037 struct mlx5_ib_port *port = &dev->port[port_num - 1];
Mark Bloch0ad17a82016-06-17 15:10:56 +03005038
5039 /* We support only per port stats */
5040 if (port_num == 0)
5041 return NULL;
5042
Parav Pandite1f24a72017-04-16 07:29:29 +03005043 return rdma_alloc_hw_stats_struct(port->cnts.names,
5044 port->cnts.num_q_counters +
Talat Batheesh9f876f32018-06-21 15:37:56 +03005045 port->cnts.num_cong_counters +
5046 port->cnts.num_ext_ppcnt_counters,
Mark Bloch0ad17a82016-06-17 15:10:56 +03005047 RDMA_HW_STATS_DEFAULT_LIFESPAN);
5048}
5049
Daniel Jurgensaac44922018-01-04 17:25:40 +02005050static int mlx5_ib_query_q_counters(struct mlx5_core_dev *mdev,
Parav Pandite1f24a72017-04-16 07:29:29 +03005051 struct mlx5_ib_port *port,
5052 struct rdma_hw_stats *stats)
5053{
5054 int outlen = MLX5_ST_SZ_BYTES(query_q_counter_out);
5055 void *out;
5056 __be32 val;
5057 int ret, i;
5058
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03005059 out = kvzalloc(outlen, GFP_KERNEL);
Parav Pandite1f24a72017-04-16 07:29:29 +03005060 if (!out)
5061 return -ENOMEM;
5062
Daniel Jurgensaac44922018-01-04 17:25:40 +02005063 ret = mlx5_core_query_q_counter(mdev,
Parav Pandite1f24a72017-04-16 07:29:29 +03005064 port->cnts.set_id, 0,
5065 out, outlen);
5066 if (ret)
5067 goto free;
5068
5069 for (i = 0; i < port->cnts.num_q_counters; i++) {
5070 val = *(__be32 *)(out + port->cnts.offsets[i]);
5071 stats->value[i] = (u64)be32_to_cpu(val);
5072 }
5073
5074free:
5075 kvfree(out);
5076 return ret;
5077}
5078
Talat Batheesh9f876f32018-06-21 15:37:56 +03005079static int mlx5_ib_query_ext_ppcnt_counters(struct mlx5_ib_dev *dev,
5080 struct mlx5_ib_port *port,
5081 struct rdma_hw_stats *stats)
5082{
5083 int offset = port->cnts.num_q_counters + port->cnts.num_cong_counters;
5084 int sz = MLX5_ST_SZ_BYTES(ppcnt_reg);
5085 int ret, i;
5086 void *out;
5087
5088 out = kvzalloc(sz, GFP_KERNEL);
5089 if (!out)
5090 return -ENOMEM;
5091
5092 ret = mlx5_cmd_query_ext_ppcnt_counters(dev->mdev, out);
5093 if (ret)
5094 goto free;
5095
5096 for (i = 0; i < port->cnts.num_ext_ppcnt_counters; i++) {
5097 stats->value[i + offset] =
5098 be64_to_cpup((__be64 *)(out +
5099 port->cnts.offsets[i + offset]));
5100 }
5101
5102free:
5103 kvfree(out);
5104 return ret;
5105}
5106
Mark Bloch0ad17a82016-06-17 15:10:56 +03005107static int mlx5_ib_get_hw_stats(struct ib_device *ibdev,
5108 struct rdma_hw_stats *stats,
Kamal Heib7c16f472017-01-18 15:25:09 +02005109 u8 port_num, int index)
Mark Bloch0ad17a82016-06-17 15:10:56 +03005110{
5111 struct mlx5_ib_dev *dev = to_mdev(ibdev);
Kamal Heib7c16f472017-01-18 15:25:09 +02005112 struct mlx5_ib_port *port = &dev->port[port_num - 1];
Daniel Jurgensaac44922018-01-04 17:25:40 +02005113 struct mlx5_core_dev *mdev;
Parav Pandite1f24a72017-04-16 07:29:29 +03005114 int ret, num_counters;
Daniel Jurgensaac44922018-01-04 17:25:40 +02005115 u8 mdev_port_num;
Mark Bloch0ad17a82016-06-17 15:10:56 +03005116
Kamal Heib7c16f472017-01-18 15:25:09 +02005117 if (!stats)
Parav Pandite1f24a72017-04-16 07:29:29 +03005118 return -EINVAL;
Mark Bloch0ad17a82016-06-17 15:10:56 +03005119
Talat Batheesh9f876f32018-06-21 15:37:56 +03005120 num_counters = port->cnts.num_q_counters +
5121 port->cnts.num_cong_counters +
5122 port->cnts.num_ext_ppcnt_counters;
Daniel Jurgensaac44922018-01-04 17:25:40 +02005123
5124 /* q_counters are per IB device, query the master mdev */
5125 ret = mlx5_ib_query_q_counters(dev->mdev, port, stats);
Mark Bloch0ad17a82016-06-17 15:10:56 +03005126 if (ret)
Parav Pandite1f24a72017-04-16 07:29:29 +03005127 return ret;
Mark Bloch0ad17a82016-06-17 15:10:56 +03005128
Talat Batheesh9f876f32018-06-21 15:37:56 +03005129 if (MLX5_CAP_PCAM_FEATURE(dev->mdev, rx_icrc_encapsulated_counter)) {
5130 ret = mlx5_ib_query_ext_ppcnt_counters(dev, port, stats);
5131 if (ret)
5132 return ret;
5133 }
5134
Parav Pandite1f24a72017-04-16 07:29:29 +03005135 if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
Daniel Jurgensaac44922018-01-04 17:25:40 +02005136 mdev = mlx5_ib_get_native_port_mdev(dev, port_num,
5137 &mdev_port_num);
5138 if (!mdev) {
5139 /* If port is not affiliated yet, its in down state
5140 * which doesn't have any counters yet, so it would be
5141 * zero. So no need to read from the HCA.
5142 */
5143 goto done;
5144 }
Majd Dibbiny71a0ff62017-12-21 17:38:26 +02005145 ret = mlx5_lag_query_cong_counters(dev->mdev,
5146 stats->value +
5147 port->cnts.num_q_counters,
5148 port->cnts.num_cong_counters,
5149 port->cnts.offsets +
5150 port->cnts.num_q_counters);
Daniel Jurgensaac44922018-01-04 17:25:40 +02005151
5152 mlx5_ib_put_native_port_mdev(dev, port_num);
Parav Pandite1f24a72017-04-16 07:29:29 +03005153 if (ret)
5154 return ret;
Mark Bloch0ad17a82016-06-17 15:10:56 +03005155 }
Kamal Heib7c16f472017-01-18 15:25:09 +02005156
Daniel Jurgensaac44922018-01-04 17:25:40 +02005157done:
Parav Pandite1f24a72017-04-16 07:29:29 +03005158 return num_counters;
Mark Bloch0ad17a82016-06-17 15:10:56 +03005159}
5160
Niranjana Vishwanathapura8e959602017-06-30 13:14:46 -07005161static void mlx5_ib_free_rdma_netdev(struct net_device *netdev)
5162{
5163 return mlx5_rdma_netdev_free(netdev);
5164}
5165
Erez Shitrit693dfd52017-04-27 17:01:34 +03005166static struct net_device*
5167mlx5_ib_alloc_rdma_netdev(struct ib_device *hca,
5168 u8 port_num,
5169 enum rdma_netdev_t type,
5170 const char *name,
5171 unsigned char name_assign_type,
5172 void (*setup)(struct net_device *))
5173{
Niranjana Vishwanathapura8e959602017-06-30 13:14:46 -07005174 struct net_device *netdev;
5175 struct rdma_netdev *rn;
5176
Erez Shitrit693dfd52017-04-27 17:01:34 +03005177 if (type != RDMA_NETDEV_IPOIB)
5178 return ERR_PTR(-EOPNOTSUPP);
5179
Niranjana Vishwanathapura8e959602017-06-30 13:14:46 -07005180 netdev = mlx5_rdma_netdev_alloc(to_mdev(hca)->mdev, hca,
5181 name, setup);
5182 if (likely(!IS_ERR_OR_NULL(netdev))) {
5183 rn = netdev_priv(netdev);
5184 rn->free_rdma_netdev = mlx5_ib_free_rdma_netdev;
5185 }
5186 return netdev;
Erez Shitrit693dfd52017-04-27 17:01:34 +03005187}
5188
Maor Gottliebfe248c32017-05-30 10:29:14 +03005189static void delay_drop_debugfs_cleanup(struct mlx5_ib_dev *dev)
5190{
5191 if (!dev->delay_drop.dbg)
5192 return;
5193 debugfs_remove_recursive(dev->delay_drop.dbg->dir_debugfs);
5194 kfree(dev->delay_drop.dbg);
5195 dev->delay_drop.dbg = NULL;
5196}
5197
Maor Gottlieb03404e82017-05-30 10:29:13 +03005198static void cancel_delay_drop(struct mlx5_ib_dev *dev)
5199{
5200 if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
5201 return;
5202
5203 cancel_work_sync(&dev->delay_drop.delay_drop_work);
Maor Gottliebfe248c32017-05-30 10:29:14 +03005204 delay_drop_debugfs_cleanup(dev);
5205}
5206
5207static ssize_t delay_drop_timeout_read(struct file *filp, char __user *buf,
5208 size_t count, loff_t *pos)
5209{
5210 struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
5211 char lbuf[20];
5212 int len;
5213
5214 len = snprintf(lbuf, sizeof(lbuf), "%u\n", delay_drop->timeout);
5215 return simple_read_from_buffer(buf, count, pos, lbuf, len);
5216}
5217
5218static ssize_t delay_drop_timeout_write(struct file *filp, const char __user *buf,
5219 size_t count, loff_t *pos)
5220{
5221 struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
5222 u32 timeout;
5223 u32 var;
5224
5225 if (kstrtouint_from_user(buf, count, 0, &var))
5226 return -EFAULT;
5227
5228 timeout = min_t(u32, roundup(var, 100), MLX5_MAX_DELAY_DROP_TIMEOUT_MS *
5229 1000);
5230 if (timeout != var)
5231 mlx5_ib_dbg(delay_drop->dev, "Round delay drop timeout to %u usec\n",
5232 timeout);
5233
5234 delay_drop->timeout = timeout;
5235
5236 return count;
5237}
5238
5239static const struct file_operations fops_delay_drop_timeout = {
5240 .owner = THIS_MODULE,
5241 .open = simple_open,
5242 .write = delay_drop_timeout_write,
5243 .read = delay_drop_timeout_read,
5244};
5245
5246static int delay_drop_debugfs_init(struct mlx5_ib_dev *dev)
5247{
5248 struct mlx5_ib_dbg_delay_drop *dbg;
5249
5250 if (!mlx5_debugfs_root)
5251 return 0;
5252
5253 dbg = kzalloc(sizeof(*dbg), GFP_KERNEL);
5254 if (!dbg)
5255 return -ENOMEM;
5256
Sudip Mukherjeecbafad82017-09-18 12:28:48 +01005257 dev->delay_drop.dbg = dbg;
5258
Maor Gottliebfe248c32017-05-30 10:29:14 +03005259 dbg->dir_debugfs =
5260 debugfs_create_dir("delay_drop",
5261 dev->mdev->priv.dbg_root);
5262 if (!dbg->dir_debugfs)
Sudip Mukherjeecbafad82017-09-18 12:28:48 +01005263 goto out_debugfs;
Maor Gottliebfe248c32017-05-30 10:29:14 +03005264
5265 dbg->events_cnt_debugfs =
5266 debugfs_create_atomic_t("num_timeout_events", 0400,
5267 dbg->dir_debugfs,
5268 &dev->delay_drop.events_cnt);
5269 if (!dbg->events_cnt_debugfs)
5270 goto out_debugfs;
5271
5272 dbg->rqs_cnt_debugfs =
5273 debugfs_create_atomic_t("num_rqs", 0400,
5274 dbg->dir_debugfs,
5275 &dev->delay_drop.rqs_cnt);
5276 if (!dbg->rqs_cnt_debugfs)
5277 goto out_debugfs;
5278
5279 dbg->timeout_debugfs =
5280 debugfs_create_file("timeout", 0600,
5281 dbg->dir_debugfs,
5282 &dev->delay_drop,
5283 &fops_delay_drop_timeout);
5284 if (!dbg->timeout_debugfs)
5285 goto out_debugfs;
5286
5287 return 0;
5288
5289out_debugfs:
5290 delay_drop_debugfs_cleanup(dev);
5291 return -ENOMEM;
Maor Gottlieb03404e82017-05-30 10:29:13 +03005292}
5293
5294static void init_delay_drop(struct mlx5_ib_dev *dev)
5295{
5296 if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
5297 return;
5298
5299 mutex_init(&dev->delay_drop.lock);
5300 dev->delay_drop.dev = dev;
5301 dev->delay_drop.activate = false;
5302 dev->delay_drop.timeout = MLX5_MAX_DELAY_DROP_TIMEOUT_MS * 1000;
5303 INIT_WORK(&dev->delay_drop.delay_drop_work, delay_drop_handler);
Maor Gottliebfe248c32017-05-30 10:29:14 +03005304 atomic_set(&dev->delay_drop.rqs_cnt, 0);
5305 atomic_set(&dev->delay_drop.events_cnt, 0);
5306
5307 if (delay_drop_debugfs_init(dev))
5308 mlx5_ib_warn(dev, "Failed to init delay drop debugfs\n");
Maor Gottlieb03404e82017-05-30 10:29:13 +03005309}
5310
Leon Romanovsky84305d712017-08-17 15:50:53 +03005311static const struct cpumask *
5312mlx5_ib_get_vector_affinity(struct ib_device *ibdev, int comp_vector)
Sagi Grimberg40b24402017-07-13 11:09:42 +03005313{
5314 struct mlx5_ib_dev *dev = to_mdev(ibdev);
5315
Israel Rukshin6082d9c2018-04-12 09:49:11 +00005316 return mlx5_get_vector_affinity_hint(dev->mdev, comp_vector);
Sagi Grimberg40b24402017-07-13 11:09:42 +03005317}
5318
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005319/* The mlx5_ib_multiport_mutex should be held when calling this function */
5320static void mlx5_ib_unbind_slave_port(struct mlx5_ib_dev *ibdev,
5321 struct mlx5_ib_multiport_info *mpi)
5322{
5323 u8 port_num = mlx5_core_native_port_num(mpi->mdev) - 1;
5324 struct mlx5_ib_port *port = &ibdev->port[port_num];
5325 int comps;
5326 int err;
5327 int i;
5328
Parav Pandita9e546e2018-01-04 17:25:39 +02005329 mlx5_ib_cleanup_cong_debugfs(ibdev, port_num);
5330
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005331 spin_lock(&port->mp.mpi_lock);
5332 if (!mpi->ibdev) {
5333 spin_unlock(&port->mp.mpi_lock);
5334 return;
5335 }
5336 mpi->ibdev = NULL;
5337
5338 spin_unlock(&port->mp.mpi_lock);
5339 mlx5_remove_netdev_notifier(ibdev, port_num);
5340 spin_lock(&port->mp.mpi_lock);
5341
5342 comps = mpi->mdev_refcnt;
5343 if (comps) {
5344 mpi->unaffiliate = true;
5345 init_completion(&mpi->unref_comp);
5346 spin_unlock(&port->mp.mpi_lock);
5347
5348 for (i = 0; i < comps; i++)
5349 wait_for_completion(&mpi->unref_comp);
5350
5351 spin_lock(&port->mp.mpi_lock);
5352 mpi->unaffiliate = false;
5353 }
5354
5355 port->mp.mpi = NULL;
5356
5357 list_add_tail(&mpi->list, &mlx5_ib_unaffiliated_port_list);
5358
5359 spin_unlock(&port->mp.mpi_lock);
5360
5361 err = mlx5_nic_vport_unaffiliate_multiport(mpi->mdev);
5362
5363 mlx5_ib_dbg(ibdev, "unaffiliated port %d\n", port_num + 1);
5364 /* Log an error, still needed to cleanup the pointers and add
5365 * it back to the list.
5366 */
5367 if (err)
5368 mlx5_ib_err(ibdev, "Failed to unaffiliate port %u\n",
5369 port_num + 1);
5370
5371 ibdev->roce[port_num].last_port_state = IB_PORT_DOWN;
5372}
5373
5374/* The mlx5_ib_multiport_mutex should be held when calling this function */
5375static bool mlx5_ib_bind_slave_port(struct mlx5_ib_dev *ibdev,
5376 struct mlx5_ib_multiport_info *mpi)
5377{
5378 u8 port_num = mlx5_core_native_port_num(mpi->mdev) - 1;
5379 int err;
5380
5381 spin_lock(&ibdev->port[port_num].mp.mpi_lock);
5382 if (ibdev->port[port_num].mp.mpi) {
5383 mlx5_ib_warn(ibdev, "port %d already affiliated.\n",
5384 port_num + 1);
5385 spin_unlock(&ibdev->port[port_num].mp.mpi_lock);
5386 return false;
5387 }
5388
5389 ibdev->port[port_num].mp.mpi = mpi;
5390 mpi->ibdev = ibdev;
5391 spin_unlock(&ibdev->port[port_num].mp.mpi_lock);
5392
5393 err = mlx5_nic_vport_affiliate_multiport(ibdev->mdev, mpi->mdev);
5394 if (err)
5395 goto unbind;
5396
5397 err = get_port_caps(ibdev, mlx5_core_native_port_num(mpi->mdev));
5398 if (err)
5399 goto unbind;
5400
5401 err = mlx5_add_netdev_notifier(ibdev, port_num);
5402 if (err) {
5403 mlx5_ib_err(ibdev, "failed adding netdev notifier for port %u\n",
5404 port_num + 1);
5405 goto unbind;
5406 }
5407
Parav Pandita9e546e2018-01-04 17:25:39 +02005408 err = mlx5_ib_init_cong_debugfs(ibdev, port_num);
5409 if (err)
5410 goto unbind;
5411
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005412 return true;
5413
5414unbind:
5415 mlx5_ib_unbind_slave_port(ibdev, mpi);
5416 return false;
5417}
5418
5419static int mlx5_ib_init_multiport_master(struct mlx5_ib_dev *dev)
5420{
5421 int port_num = mlx5_core_native_port_num(dev->mdev) - 1;
5422 enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev,
5423 port_num + 1);
5424 struct mlx5_ib_multiport_info *mpi;
5425 int err;
5426 int i;
5427
5428 if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
5429 return 0;
5430
5431 err = mlx5_query_nic_vport_system_image_guid(dev->mdev,
5432 &dev->sys_image_guid);
5433 if (err)
5434 return err;
5435
5436 err = mlx5_nic_vport_enable_roce(dev->mdev);
5437 if (err)
5438 return err;
5439
5440 mutex_lock(&mlx5_ib_multiport_mutex);
5441 for (i = 0; i < dev->num_ports; i++) {
5442 bool bound = false;
5443
5444 /* build a stub multiport info struct for the native port. */
5445 if (i == port_num) {
5446 mpi = kzalloc(sizeof(*mpi), GFP_KERNEL);
5447 if (!mpi) {
5448 mutex_unlock(&mlx5_ib_multiport_mutex);
5449 mlx5_nic_vport_disable_roce(dev->mdev);
5450 return -ENOMEM;
5451 }
5452
5453 mpi->is_master = true;
5454 mpi->mdev = dev->mdev;
5455 mpi->sys_image_guid = dev->sys_image_guid;
5456 dev->port[i].mp.mpi = mpi;
5457 mpi->ibdev = dev;
5458 mpi = NULL;
5459 continue;
5460 }
5461
5462 list_for_each_entry(mpi, &mlx5_ib_unaffiliated_port_list,
5463 list) {
5464 if (dev->sys_image_guid == mpi->sys_image_guid &&
5465 (mlx5_core_native_port_num(mpi->mdev) - 1) == i) {
5466 bound = mlx5_ib_bind_slave_port(dev, mpi);
5467 }
5468
5469 if (bound) {
5470 dev_dbg(&mpi->mdev->pdev->dev, "removing port from unaffiliated list.\n");
5471 mlx5_ib_dbg(dev, "port %d bound\n", i + 1);
5472 list_del(&mpi->list);
5473 break;
5474 }
5475 }
5476 if (!bound) {
5477 get_port_caps(dev, i + 1);
5478 mlx5_ib_dbg(dev, "no free port found for port %d\n",
5479 i + 1);
5480 }
5481 }
5482
5483 list_add_tail(&dev->ib_dev_list, &mlx5_ib_dev_list);
5484 mutex_unlock(&mlx5_ib_multiport_mutex);
5485 return err;
5486}
5487
5488static void mlx5_ib_cleanup_multiport_master(struct mlx5_ib_dev *dev)
5489{
5490 int port_num = mlx5_core_native_port_num(dev->mdev) - 1;
5491 enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev,
5492 port_num + 1);
5493 int i;
5494
5495 if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
5496 return;
5497
5498 mutex_lock(&mlx5_ib_multiport_mutex);
5499 for (i = 0; i < dev->num_ports; i++) {
5500 if (dev->port[i].mp.mpi) {
5501 /* Destroy the native port stub */
5502 if (i == port_num) {
5503 kfree(dev->port[i].mp.mpi);
5504 dev->port[i].mp.mpi = NULL;
5505 } else {
5506 mlx5_ib_dbg(dev, "unbinding port_num: %d\n", i + 1);
5507 mlx5_ib_unbind_slave_port(dev, dev->port[i].mp.mpi);
5508 }
5509 }
5510 }
5511
5512 mlx5_ib_dbg(dev, "removing from devlist\n");
5513 list_del(&dev->ib_dev_list);
5514 mutex_unlock(&mlx5_ib_multiport_mutex);
5515
5516 mlx5_nic_vport_disable_roce(dev->mdev);
5517}
5518
Jason Gunthorpe9a119cd2018-07-04 08:50:28 +03005519ADD_UVERBS_ATTRIBUTES_SIMPLE(
5520 mlx5_ib_dm,
5521 UVERBS_OBJECT_DM,
5522 UVERBS_METHOD_DM_ALLOC,
5523 UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_ALLOC_DM_RESP_START_OFFSET,
5524 UVERBS_ATTR_TYPE(u64),
Jason Gunthorpe83bb4442018-07-04 08:50:29 +03005525 UA_MANDATORY),
Jason Gunthorpe9a119cd2018-07-04 08:50:28 +03005526 UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_ALLOC_DM_RESP_PAGE_INDEX,
5527 UVERBS_ATTR_TYPE(u16),
Jason Gunthorpe83bb4442018-07-04 08:50:29 +03005528 UA_MANDATORY));
Ariel Levkovich24da0012018-04-05 18:53:27 +03005529
Jason Gunthorpe9a119cd2018-07-04 08:50:28 +03005530ADD_UVERBS_ATTRIBUTES_SIMPLE(
5531 mlx5_ib_flow_action,
5532 UVERBS_OBJECT_FLOW_ACTION,
5533 UVERBS_METHOD_FLOW_ACTION_ESP_CREATE,
5534 UVERBS_ATTR_PTR_IN(MLX5_IB_ATTR_CREATE_FLOW_ACTION_FLAGS,
5535 UVERBS_ATTR_TYPE(u64),
Jason Gunthorpe83bb4442018-07-04 08:50:29 +03005536 UA_MANDATORY));
Aviad Yehezkelc6475a02018-03-28 09:27:50 +03005537
Yishai Hadasc59450c2018-06-17 13:00:06 +03005538#define NUM_TREES 3
Matan Barak8c846602018-03-28 09:27:41 +03005539static int populate_specs_root(struct mlx5_ib_dev *dev)
5540{
5541 const struct uverbs_object_tree_def *default_root[NUM_TREES + 1] = {
5542 uverbs_default_get_objects()};
5543 size_t num_trees = 1;
5544
Aviad Yehezkelc6475a02018-03-28 09:27:50 +03005545 if (mlx5_accel_ipsec_device_caps(dev->mdev) & MLX5_ACCEL_IPSEC_CAP_DEVICE &&
5546 !WARN_ON(num_trees >= ARRAY_SIZE(default_root)))
5547 default_root[num_trees++] = &mlx5_ib_flow_action;
5548
Ariel Levkovich24da0012018-04-05 18:53:27 +03005549 if (MLX5_CAP_DEV_MEM(dev->mdev, memic) &&
5550 !WARN_ON(num_trees >= ARRAY_SIZE(default_root)))
5551 default_root[num_trees++] = &mlx5_ib_dm;
5552
Yishai Hadasc59450c2018-06-17 13:00:06 +03005553 if (MLX5_CAP_GEN_64(dev->mdev, general_obj_types) &
5554 MLX5_GENERAL_OBJ_TYPES_CAP_UCTX &&
5555 !WARN_ON(num_trees >= ARRAY_SIZE(default_root)))
5556 default_root[num_trees++] = mlx5_ib_get_devx_tree();
5557
Jason Gunthorpe87fc2a62018-07-04 08:50:23 +03005558 dev->ib_dev.driver_specs_root =
Matan Barak8c846602018-03-28 09:27:41 +03005559 uverbs_alloc_spec_tree(num_trees, default_root);
5560
Jason Gunthorpe87fc2a62018-07-04 08:50:23 +03005561 return PTR_ERR_OR_ZERO(dev->ib_dev.driver_specs_root);
Matan Barak8c846602018-03-28 09:27:41 +03005562}
5563
5564static void depopulate_specs_root(struct mlx5_ib_dev *dev)
5565{
Jason Gunthorpe87fc2a62018-07-04 08:50:23 +03005566 uverbs_free_spec_tree(dev->ib_dev.driver_specs_root);
Matan Barak8c846602018-03-28 09:27:41 +03005567}
5568
Raed Salem1a1e03d2018-05-31 16:43:41 +03005569static int mlx5_ib_read_counters(struct ib_counters *counters,
5570 struct ib_counters_read_attr *read_attr,
5571 struct uverbs_attr_bundle *attrs)
5572{
5573 struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
5574 struct mlx5_read_counters_attr mread_attr = {};
5575 struct mlx5_ib_flow_counters_desc *desc;
5576 int ret, i;
5577
5578 mutex_lock(&mcounters->mcntrs_mutex);
5579 if (mcounters->cntrs_max_index > read_attr->ncounters) {
5580 ret = -EINVAL;
5581 goto err_bound;
5582 }
5583
5584 mread_attr.out = kcalloc(mcounters->counters_num, sizeof(u64),
5585 GFP_KERNEL);
5586 if (!mread_attr.out) {
5587 ret = -ENOMEM;
5588 goto err_bound;
5589 }
5590
5591 mread_attr.hw_cntrs_hndl = mcounters->hw_cntrs_hndl;
5592 mread_attr.flags = read_attr->flags;
5593 ret = mcounters->read_counters(counters->device, &mread_attr);
5594 if (ret)
5595 goto err_read;
5596
5597 /* do the pass over the counters data array to assign according to the
5598 * descriptions and indexing pairs
5599 */
5600 desc = mcounters->counters_data;
5601 for (i = 0; i < mcounters->ncounters; i++)
5602 read_attr->counters_buff[desc[i].index] += mread_attr.out[desc[i].description];
5603
5604err_read:
5605 kfree(mread_attr.out);
5606err_bound:
5607 mutex_unlock(&mcounters->mcntrs_mutex);
5608 return ret;
5609}
5610
Raed Salemb29e2a12018-05-31 16:43:38 +03005611static int mlx5_ib_destroy_counters(struct ib_counters *counters)
5612{
5613 struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
5614
Raed Salem3b3233f2018-05-31 16:43:39 +03005615 counters_clear_description(counters);
5616 if (mcounters->hw_cntrs_hndl)
5617 mlx5_fc_destroy(to_mdev(counters->device)->mdev,
5618 mcounters->hw_cntrs_hndl);
5619
Raed Salemb29e2a12018-05-31 16:43:38 +03005620 kfree(mcounters);
5621
5622 return 0;
5623}
5624
5625static struct ib_counters *mlx5_ib_create_counters(struct ib_device *device,
5626 struct uverbs_attr_bundle *attrs)
5627{
5628 struct mlx5_ib_mcounters *mcounters;
5629
5630 mcounters = kzalloc(sizeof(*mcounters), GFP_KERNEL);
5631 if (!mcounters)
5632 return ERR_PTR(-ENOMEM);
5633
Raed Salem3b3233f2018-05-31 16:43:39 +03005634 mutex_init(&mcounters->mcntrs_mutex);
5635
Raed Salemb29e2a12018-05-31 16:43:38 +03005636 return &mcounters->ibcntrs;
5637}
5638
Mark Blochb5ca15a2018-01-23 11:16:30 +00005639void mlx5_ib_stage_init_cleanup(struct mlx5_ib_dev *dev)
Eli Cohene126ba92013-07-07 17:25:49 +03005640{
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005641 mlx5_ib_cleanup_multiport_master(dev);
Mark Bloch3cc297d2018-01-01 13:07:03 +02005642#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
5643 cleanup_srcu_struct(&dev->mr_srcu);
5644#endif
Mark Bloch16c19752018-01-01 13:06:58 +02005645 kfree(dev->port);
5646}
5647
Mark Blochb5ca15a2018-01-23 11:16:30 +00005648int mlx5_ib_stage_init_init(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02005649{
5650 struct mlx5_core_dev *mdev = dev->mdev;
Aviv Heller4babcf92016-09-18 20:48:03 +03005651 const char *name;
Eli Cohene126ba92013-07-07 17:25:49 +03005652 int err;
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005653 int i;
Eli Cohene126ba92013-07-07 17:25:49 +03005654
Daniel Jurgens508562d2018-01-04 17:25:34 +02005655 dev->port = kcalloc(dev->num_ports, sizeof(*dev->port),
Mark Bloch0837e862016-06-17 15:10:55 +03005656 GFP_KERNEL);
5657 if (!dev->port)
Mark Bloch16c19752018-01-01 13:06:58 +02005658 return -ENOMEM;
Mark Bloch0837e862016-06-17 15:10:55 +03005659
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005660 for (i = 0; i < dev->num_ports; i++) {
5661 spin_lock_init(&dev->port[i].mp.mpi_lock);
5662 rwlock_init(&dev->roce[i].netdev_lock);
5663 }
5664
5665 err = mlx5_ib_init_multiport_master(dev);
Eli Cohene126ba92013-07-07 17:25:49 +03005666 if (err)
Mark Bloch0837e862016-06-17 15:10:55 +03005667 goto err_free_port;
Eli Cohene126ba92013-07-07 17:25:49 +03005668
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005669 if (!mlx5_core_mp_enabled(mdev)) {
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005670 for (i = 1; i <= dev->num_ports; i++) {
5671 err = get_port_caps(dev, i);
5672 if (err)
5673 break;
5674 }
5675 } else {
5676 err = get_port_caps(dev, mlx5_core_native_port_num(mdev));
5677 }
5678 if (err)
5679 goto err_mp;
5680
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03005681 if (mlx5_use_mad_ifc(dev))
5682 get_ext_port_caps(dev);
Eli Cohene126ba92013-07-07 17:25:49 +03005683
Aviv Heller4babcf92016-09-18 20:48:03 +03005684 if (!mlx5_lag_is_active(mdev))
5685 name = "mlx5_%d";
5686 else
5687 name = "mlx5_bond_%d";
5688
5689 strlcpy(dev->ib_dev.name, name, IB_DEVICE_NAME_MAX);
Eli Cohene126ba92013-07-07 17:25:49 +03005690 dev->ib_dev.owner = THIS_MODULE;
5691 dev->ib_dev.node_type = RDMA_NODE_IB_CA;
Sagi Grimbergc6790aa2015-09-24 10:34:23 +03005692 dev->ib_dev.local_dma_lkey = 0 /* not supported for now */;
Daniel Jurgens508562d2018-01-04 17:25:34 +02005693 dev->ib_dev.phys_port_cnt = dev->num_ports;
Saeed Mahameed233d05d2015-04-02 17:07:32 +03005694 dev->ib_dev.num_comp_vectors =
5695 dev->mdev->priv.eq_table.num_comp_vectors;
Bart Van Assche9b0c2892017-01-20 13:04:21 -08005696 dev->ib_dev.dev.parent = &mdev->pdev->dev;
Eli Cohene126ba92013-07-07 17:25:49 +03005697
Mark Bloch3cc297d2018-01-01 13:07:03 +02005698 mutex_init(&dev->cap_mask_mutex);
5699 INIT_LIST_HEAD(&dev->qp_list);
5700 spin_lock_init(&dev->reset_flow_resource_lock);
5701
Ariel Levkovich24da0012018-04-05 18:53:27 +03005702 spin_lock_init(&dev->memic.memic_lock);
5703 dev->memic.dev = mdev;
5704
Mark Bloch3cc297d2018-01-01 13:07:03 +02005705#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
5706 err = init_srcu_struct(&dev->mr_srcu);
5707 if (err)
5708 goto err_free_port;
5709#endif
5710
Mark Bloch16c19752018-01-01 13:06:58 +02005711 return 0;
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005712err_mp:
5713 mlx5_ib_cleanup_multiport_master(dev);
Mark Bloch16c19752018-01-01 13:06:58 +02005714
5715err_free_port:
5716 kfree(dev->port);
5717
5718 return -ENOMEM;
5719}
5720
Mark Bloch9a4ca382018-01-16 14:42:35 +00005721static int mlx5_ib_stage_flow_db_init(struct mlx5_ib_dev *dev)
5722{
5723 dev->flow_db = kzalloc(sizeof(*dev->flow_db), GFP_KERNEL);
5724
5725 if (!dev->flow_db)
5726 return -ENOMEM;
5727
5728 mutex_init(&dev->flow_db->lock);
5729
5730 return 0;
5731}
5732
Mark Blochb5ca15a2018-01-23 11:16:30 +00005733int mlx5_ib_stage_rep_flow_db_init(struct mlx5_ib_dev *dev)
5734{
5735 struct mlx5_ib_dev *nic_dev;
5736
5737 nic_dev = mlx5_ib_get_uplink_ibdev(dev->mdev->priv.eswitch);
5738
5739 if (!nic_dev)
5740 return -EINVAL;
5741
5742 dev->flow_db = nic_dev->flow_db;
5743
5744 return 0;
5745}
5746
Mark Bloch9a4ca382018-01-16 14:42:35 +00005747static void mlx5_ib_stage_flow_db_cleanup(struct mlx5_ib_dev *dev)
5748{
5749 kfree(dev->flow_db);
5750}
5751
Mark Blochb5ca15a2018-01-23 11:16:30 +00005752int mlx5_ib_stage_caps_init(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02005753{
5754 struct mlx5_core_dev *mdev = dev->mdev;
Mark Bloch16c19752018-01-01 13:06:58 +02005755 int err;
5756
Eli Cohene126ba92013-07-07 17:25:49 +03005757 dev->ib_dev.uverbs_abi_ver = MLX5_IB_UVERBS_ABI_VERSION;
5758 dev->ib_dev.uverbs_cmd_mask =
5759 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
5760 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
5761 (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
5762 (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
5763 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
Moni Shoua41c450f2016-11-23 08:23:26 +02005764 (1ull << IB_USER_VERBS_CMD_CREATE_AH) |
5765 (1ull << IB_USER_VERBS_CMD_DESTROY_AH) |
Eli Cohene126ba92013-07-07 17:25:49 +03005766 (1ull << IB_USER_VERBS_CMD_REG_MR) |
Noa Osherovich56e11d62016-02-29 16:46:51 +02005767 (1ull << IB_USER_VERBS_CMD_REREG_MR) |
Eli Cohene126ba92013-07-07 17:25:49 +03005768 (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
5769 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
5770 (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
5771 (1ull << IB_USER_VERBS_CMD_RESIZE_CQ) |
5772 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
5773 (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
5774 (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
5775 (1ull << IB_USER_VERBS_CMD_QUERY_QP) |
5776 (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
5777 (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST) |
5778 (1ull << IB_USER_VERBS_CMD_DETACH_MCAST) |
5779 (1ull << IB_USER_VERBS_CMD_CREATE_SRQ) |
5780 (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ) |
5781 (1ull << IB_USER_VERBS_CMD_QUERY_SRQ) |
5782 (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ) |
5783 (1ull << IB_USER_VERBS_CMD_CREATE_XSRQ) |
5784 (1ull << IB_USER_VERBS_CMD_OPEN_QP);
Haggai Eran1707cb42015-02-08 13:28:52 +02005785 dev->ib_dev.uverbs_ex_cmd_mask =
Matan Barakd4584dd2016-01-28 17:51:46 +02005786 (1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE) |
5787 (1ull << IB_USER_VERBS_EX_CMD_CREATE_CQ) |
Bodong Wang7d29f342016-12-01 13:43:16 +02005788 (1ull << IB_USER_VERBS_EX_CMD_CREATE_QP) |
Yonatan Cohenb0e9df62017-11-13 10:51:15 +02005789 (1ull << IB_USER_VERBS_EX_CMD_MODIFY_QP) |
5790 (1ull << IB_USER_VERBS_EX_CMD_MODIFY_CQ);
Eli Cohene126ba92013-07-07 17:25:49 +03005791
5792 dev->ib_dev.query_device = mlx5_ib_query_device;
Achiad Shochatebd61f62015-12-23 18:47:16 +02005793 dev->ib_dev.get_link_layer = mlx5_ib_port_link_layer;
Eli Cohene126ba92013-07-07 17:25:49 +03005794 dev->ib_dev.query_gid = mlx5_ib_query_gid;
Achiad Shochat3cca2602015-12-23 18:47:23 +02005795 dev->ib_dev.add_gid = mlx5_ib_add_gid;
5796 dev->ib_dev.del_gid = mlx5_ib_del_gid;
Eli Cohene126ba92013-07-07 17:25:49 +03005797 dev->ib_dev.query_pkey = mlx5_ib_query_pkey;
5798 dev->ib_dev.modify_device = mlx5_ib_modify_device;
5799 dev->ib_dev.modify_port = mlx5_ib_modify_port;
5800 dev->ib_dev.alloc_ucontext = mlx5_ib_alloc_ucontext;
5801 dev->ib_dev.dealloc_ucontext = mlx5_ib_dealloc_ucontext;
5802 dev->ib_dev.mmap = mlx5_ib_mmap;
5803 dev->ib_dev.alloc_pd = mlx5_ib_alloc_pd;
5804 dev->ib_dev.dealloc_pd = mlx5_ib_dealloc_pd;
5805 dev->ib_dev.create_ah = mlx5_ib_create_ah;
5806 dev->ib_dev.query_ah = mlx5_ib_query_ah;
5807 dev->ib_dev.destroy_ah = mlx5_ib_destroy_ah;
5808 dev->ib_dev.create_srq = mlx5_ib_create_srq;
5809 dev->ib_dev.modify_srq = mlx5_ib_modify_srq;
5810 dev->ib_dev.query_srq = mlx5_ib_query_srq;
5811 dev->ib_dev.destroy_srq = mlx5_ib_destroy_srq;
5812 dev->ib_dev.post_srq_recv = mlx5_ib_post_srq_recv;
5813 dev->ib_dev.create_qp = mlx5_ib_create_qp;
5814 dev->ib_dev.modify_qp = mlx5_ib_modify_qp;
5815 dev->ib_dev.query_qp = mlx5_ib_query_qp;
5816 dev->ib_dev.destroy_qp = mlx5_ib_destroy_qp;
Yishai Hadasd0e84c02018-06-19 10:43:55 +03005817 dev->ib_dev.drain_sq = mlx5_ib_drain_sq;
5818 dev->ib_dev.drain_rq = mlx5_ib_drain_rq;
Eli Cohene126ba92013-07-07 17:25:49 +03005819 dev->ib_dev.post_send = mlx5_ib_post_send;
5820 dev->ib_dev.post_recv = mlx5_ib_post_recv;
5821 dev->ib_dev.create_cq = mlx5_ib_create_cq;
5822 dev->ib_dev.modify_cq = mlx5_ib_modify_cq;
5823 dev->ib_dev.resize_cq = mlx5_ib_resize_cq;
5824 dev->ib_dev.destroy_cq = mlx5_ib_destroy_cq;
5825 dev->ib_dev.poll_cq = mlx5_ib_poll_cq;
5826 dev->ib_dev.req_notify_cq = mlx5_ib_arm_cq;
5827 dev->ib_dev.get_dma_mr = mlx5_ib_get_dma_mr;
5828 dev->ib_dev.reg_user_mr = mlx5_ib_reg_user_mr;
Noa Osherovich56e11d62016-02-29 16:46:51 +02005829 dev->ib_dev.rereg_user_mr = mlx5_ib_rereg_user_mr;
Eli Cohene126ba92013-07-07 17:25:49 +03005830 dev->ib_dev.dereg_mr = mlx5_ib_dereg_mr;
5831 dev->ib_dev.attach_mcast = mlx5_ib_mcg_attach;
5832 dev->ib_dev.detach_mcast = mlx5_ib_mcg_detach;
5833 dev->ib_dev.process_mad = mlx5_ib_process_mad;
Sagi Grimberg9bee1782015-07-30 10:32:35 +03005834 dev->ib_dev.alloc_mr = mlx5_ib_alloc_mr;
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03005835 dev->ib_dev.map_mr_sg = mlx5_ib_map_mr_sg;
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02005836 dev->ib_dev.check_mr_status = mlx5_ib_check_mr_status;
Ira Weinyc7342822016-06-15 02:22:01 -04005837 dev->ib_dev.get_dev_fw_str = get_dev_fw_str;
Sagi Grimberg40b24402017-07-13 11:09:42 +03005838 dev->ib_dev.get_vector_affinity = mlx5_ib_get_vector_affinity;
Niranjana Vishwanathapura8e959602017-06-30 13:14:46 -07005839 if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads))
Alex Vesker022d0382017-06-14 09:59:06 +03005840 dev->ib_dev.alloc_rdma_netdev = mlx5_ib_alloc_rdma_netdev;
Niranjana Vishwanathapura8e959602017-06-30 13:14:46 -07005841
Eli Coheneff901d2016-03-11 22:58:42 +02005842 if (mlx5_core_is_pf(mdev)) {
5843 dev->ib_dev.get_vf_config = mlx5_ib_get_vf_config;
5844 dev->ib_dev.set_vf_link_state = mlx5_ib_set_vf_link_state;
5845 dev->ib_dev.get_vf_stats = mlx5_ib_get_vf_stats;
5846 dev->ib_dev.set_vf_guid = mlx5_ib_set_vf_guid;
5847 }
Eli Cohene126ba92013-07-07 17:25:49 +03005848
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03005849 dev->ib_dev.disassociate_ucontext = mlx5_ib_disassociate_ucontext;
5850
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03005851 dev->umr_fence = mlx5_get_umr_fence(MLX5_CAP_GEN(mdev, umr_fence));
5852
Matan Barakd2370e02016-02-29 18:05:30 +02005853 if (MLX5_CAP_GEN(mdev, imaicl)) {
5854 dev->ib_dev.alloc_mw = mlx5_ib_alloc_mw;
5855 dev->ib_dev.dealloc_mw = mlx5_ib_dealloc_mw;
5856 dev->ib_dev.uverbs_cmd_mask |=
5857 (1ull << IB_USER_VERBS_CMD_ALLOC_MW) |
5858 (1ull << IB_USER_VERBS_CMD_DEALLOC_MW);
5859 }
5860
Saeed Mahameed938fe832015-05-28 22:28:41 +03005861 if (MLX5_CAP_GEN(mdev, xrc)) {
Eli Cohene126ba92013-07-07 17:25:49 +03005862 dev->ib_dev.alloc_xrcd = mlx5_ib_alloc_xrcd;
5863 dev->ib_dev.dealloc_xrcd = mlx5_ib_dealloc_xrcd;
5864 dev->ib_dev.uverbs_cmd_mask |=
5865 (1ull << IB_USER_VERBS_CMD_OPEN_XRCD) |
5866 (1ull << IB_USER_VERBS_CMD_CLOSE_XRCD);
5867 }
5868
Ariel Levkovich24da0012018-04-05 18:53:27 +03005869 if (MLX5_CAP_DEV_MEM(mdev, memic)) {
5870 dev->ib_dev.alloc_dm = mlx5_ib_alloc_dm;
5871 dev->ib_dev.dealloc_dm = mlx5_ib_dealloc_dm;
Ariel Levkovich6c29f572018-04-05 18:53:29 +03005872 dev->ib_dev.reg_dm_mr = mlx5_ib_reg_dm_mr;
Ariel Levkovich24da0012018-04-05 18:53:27 +03005873 }
5874
Yishai Hadas81e30882017-06-08 16:15:09 +03005875 dev->ib_dev.create_flow = mlx5_ib_create_flow;
5876 dev->ib_dev.destroy_flow = mlx5_ib_destroy_flow;
5877 dev->ib_dev.uverbs_ex_cmd_mask |=
5878 (1ull << IB_USER_VERBS_EX_CMD_CREATE_FLOW) |
5879 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_FLOW);
Aviad Yehezkelc6475a02018-03-28 09:27:50 +03005880 dev->ib_dev.create_flow_action_esp = mlx5_ib_create_flow_action_esp;
5881 dev->ib_dev.destroy_flow_action = mlx5_ib_destroy_flow_action;
Matan Barak349705c2018-03-28 09:27:51 +03005882 dev->ib_dev.modify_flow_action_esp = mlx5_ib_modify_flow_action_esp;
Matan Barak0ede73b2018-03-19 15:02:34 +02005883 dev->ib_dev.driver_id = RDMA_DRIVER_MLX5;
Raed Salemb29e2a12018-05-31 16:43:38 +03005884 dev->ib_dev.create_counters = mlx5_ib_create_counters;
5885 dev->ib_dev.destroy_counters = mlx5_ib_destroy_counters;
Raed Salem1a1e03d2018-05-31 16:43:41 +03005886 dev->ib_dev.read_counters = mlx5_ib_read_counters;
Yishai Hadas81e30882017-06-08 16:15:09 +03005887
Eli Cohene126ba92013-07-07 17:25:49 +03005888 err = init_node_data(dev);
5889 if (err)
Mark Bloch16c19752018-01-01 13:06:58 +02005890 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03005891
Mark Blochc8b89922018-01-01 13:07:02 +02005892 if ((MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_ETH) &&
Jason Gunthorpee7996a92018-01-29 13:26:40 -07005893 (MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) ||
5894 MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
Mark Blochc8b89922018-01-01 13:07:02 +02005895 mutex_init(&dev->lb_mutex);
5896
Mark Bloch16c19752018-01-01 13:06:58 +02005897 return 0;
5898}
5899
Mark Bloch8e6efa32017-11-06 12:22:13 +00005900static int mlx5_ib_stage_non_default_cb(struct mlx5_ib_dev *dev)
5901{
5902 dev->ib_dev.get_port_immutable = mlx5_port_immutable;
5903 dev->ib_dev.query_port = mlx5_ib_query_port;
5904
5905 return 0;
5906}
5907
Mark Blochb5ca15a2018-01-23 11:16:30 +00005908int mlx5_ib_stage_rep_non_default_cb(struct mlx5_ib_dev *dev)
Mark Bloch8e6efa32017-11-06 12:22:13 +00005909{
5910 dev->ib_dev.get_port_immutable = mlx5_port_rep_immutable;
5911 dev->ib_dev.query_port = mlx5_ib_rep_query_port;
5912
5913 return 0;
5914}
5915
Leon Romanovskye3f1ed12018-07-08 12:55:43 +03005916static int mlx5_ib_stage_common_roce_init(struct mlx5_ib_dev *dev)
Mark Bloch8e6efa32017-11-06 12:22:13 +00005917{
Leon Romanovskye3f1ed12018-07-08 12:55:43 +03005918 u8 port_num;
Mark Bloch8e6efa32017-11-06 12:22:13 +00005919 int i;
5920
5921 for (i = 0; i < dev->num_ports; i++) {
5922 dev->roce[i].dev = dev;
5923 dev->roce[i].native_port_num = i + 1;
5924 dev->roce[i].last_port_state = IB_PORT_DOWN;
5925 }
5926
5927 dev->ib_dev.get_netdev = mlx5_ib_get_netdev;
5928 dev->ib_dev.create_wq = mlx5_ib_create_wq;
5929 dev->ib_dev.modify_wq = mlx5_ib_modify_wq;
5930 dev->ib_dev.destroy_wq = mlx5_ib_destroy_wq;
5931 dev->ib_dev.create_rwq_ind_table = mlx5_ib_create_rwq_ind_table;
5932 dev->ib_dev.destroy_rwq_ind_table = mlx5_ib_destroy_rwq_ind_table;
5933
5934 dev->ib_dev.uverbs_ex_cmd_mask |=
5935 (1ull << IB_USER_VERBS_EX_CMD_CREATE_WQ) |
5936 (1ull << IB_USER_VERBS_EX_CMD_MODIFY_WQ) |
5937 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_WQ) |
5938 (1ull << IB_USER_VERBS_EX_CMD_CREATE_RWQ_IND_TBL) |
5939 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_RWQ_IND_TBL);
5940
Leon Romanovskye3f1ed12018-07-08 12:55:43 +03005941 port_num = mlx5_core_native_port_num(dev->mdev) - 1;
5942
Mark Bloch8e6efa32017-11-06 12:22:13 +00005943 return mlx5_add_netdev_notifier(dev, port_num);
5944}
5945
5946static void mlx5_ib_stage_common_roce_cleanup(struct mlx5_ib_dev *dev)
5947{
5948 u8 port_num = mlx5_core_native_port_num(dev->mdev) - 1;
5949
5950 mlx5_remove_netdev_notifier(dev, port_num);
5951}
5952
5953int mlx5_ib_stage_rep_roce_init(struct mlx5_ib_dev *dev)
5954{
5955 struct mlx5_core_dev *mdev = dev->mdev;
5956 enum rdma_link_layer ll;
5957 int port_type_cap;
5958 int err = 0;
Mark Bloch8e6efa32017-11-06 12:22:13 +00005959
Mark Bloch8e6efa32017-11-06 12:22:13 +00005960 port_type_cap = MLX5_CAP_GEN(mdev, port_type);
5961 ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
5962
5963 if (ll == IB_LINK_LAYER_ETHERNET)
Leon Romanovskye3f1ed12018-07-08 12:55:43 +03005964 err = mlx5_ib_stage_common_roce_init(dev);
Mark Bloch8e6efa32017-11-06 12:22:13 +00005965
5966 return err;
5967}
5968
5969void mlx5_ib_stage_rep_roce_cleanup(struct mlx5_ib_dev *dev)
5970{
5971 mlx5_ib_stage_common_roce_cleanup(dev);
5972}
5973
Mark Bloch16c19752018-01-01 13:06:58 +02005974static int mlx5_ib_stage_roce_init(struct mlx5_ib_dev *dev)
5975{
5976 struct mlx5_core_dev *mdev = dev->mdev;
5977 enum rdma_link_layer ll;
5978 int port_type_cap;
5979 int err;
5980
5981 port_type_cap = MLX5_CAP_GEN(mdev, port_type);
5982 ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
5983
Achiad Shochatfc24fc52015-12-23 18:47:17 +02005984 if (ll == IB_LINK_LAYER_ETHERNET) {
Leon Romanovskye3f1ed12018-07-08 12:55:43 +03005985 err = mlx5_ib_stage_common_roce_init(dev);
Achiad Shochatfc24fc52015-12-23 18:47:17 +02005986 if (err)
Mark Bloch16c19752018-01-01 13:06:58 +02005987 return err;
Mark Bloch8e6efa32017-11-06 12:22:13 +00005988
Leon Romanovskye3f1ed12018-07-08 12:55:43 +03005989 err = mlx5_enable_eth(dev);
Mark Bloch8e6efa32017-11-06 12:22:13 +00005990 if (err)
5991 goto cleanup;
Achiad Shochatfc24fc52015-12-23 18:47:17 +02005992 }
5993
Mark Bloch16c19752018-01-01 13:06:58 +02005994 return 0;
Mark Bloch8e6efa32017-11-06 12:22:13 +00005995cleanup:
5996 mlx5_ib_stage_common_roce_cleanup(dev);
5997
5998 return err;
Mark Bloch16c19752018-01-01 13:06:58 +02005999}
Eli Cohene126ba92013-07-07 17:25:49 +03006000
Mark Bloch16c19752018-01-01 13:06:58 +02006001static void mlx5_ib_stage_roce_cleanup(struct mlx5_ib_dev *dev)
6002{
6003 struct mlx5_core_dev *mdev = dev->mdev;
6004 enum rdma_link_layer ll;
6005 int port_type_cap;
Eli Cohene126ba92013-07-07 17:25:49 +03006006
Mark Bloch16c19752018-01-01 13:06:58 +02006007 port_type_cap = MLX5_CAP_GEN(mdev, port_type);
6008 ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
6009
6010 if (ll == IB_LINK_LAYER_ETHERNET) {
6011 mlx5_disable_eth(dev);
Mark Bloch8e6efa32017-11-06 12:22:13 +00006012 mlx5_ib_stage_common_roce_cleanup(dev);
Kamal Heib45bded22017-01-18 14:10:32 +02006013 }
Mark Bloch16c19752018-01-01 13:06:58 +02006014}
Haggai Eran6aec21f2014-12-11 17:04:23 +02006015
Mark Blochb5ca15a2018-01-23 11:16:30 +00006016int mlx5_ib_stage_dev_res_init(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02006017{
6018 return create_dev_resources(&dev->devr);
6019}
Parav Pandit4a2da0b2017-05-30 10:05:15 +03006020
Mark Blochb5ca15a2018-01-23 11:16:30 +00006021void mlx5_ib_stage_dev_res_cleanup(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02006022{
6023 destroy_dev_resources(&dev->devr);
6024}
6025
6026static int mlx5_ib_stage_odp_init(struct mlx5_ib_dev *dev)
6027{
Mark Bloch07321b32018-01-01 13:07:00 +02006028 mlx5_ib_internal_fill_odp_caps(dev);
6029
Mark Bloch16c19752018-01-01 13:06:58 +02006030 return mlx5_ib_odp_init_one(dev);
6031}
6032
Mark Blochb5ca15a2018-01-23 11:16:30 +00006033int mlx5_ib_stage_counters_init(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02006034{
Mark Bloch5e1e7612018-01-01 13:07:01 +02006035 if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt)) {
6036 dev->ib_dev.get_hw_stats = mlx5_ib_get_hw_stats;
6037 dev->ib_dev.alloc_hw_stats = mlx5_ib_alloc_hw_stats;
6038
6039 return mlx5_ib_alloc_counters(dev);
6040 }
Mark Bloch16c19752018-01-01 13:06:58 +02006041
6042 return 0;
6043}
6044
Mark Blochb5ca15a2018-01-23 11:16:30 +00006045void mlx5_ib_stage_counters_cleanup(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02006046{
6047 if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt))
6048 mlx5_ib_dealloc_counters(dev);
6049}
6050
6051static int mlx5_ib_stage_cong_debugfs_init(struct mlx5_ib_dev *dev)
6052{
Parav Pandita9e546e2018-01-04 17:25:39 +02006053 return mlx5_ib_init_cong_debugfs(dev,
6054 mlx5_core_native_port_num(dev->mdev) - 1);
Mark Bloch16c19752018-01-01 13:06:58 +02006055}
6056
6057static void mlx5_ib_stage_cong_debugfs_cleanup(struct mlx5_ib_dev *dev)
6058{
Parav Pandita9e546e2018-01-04 17:25:39 +02006059 mlx5_ib_cleanup_cong_debugfs(dev,
6060 mlx5_core_native_port_num(dev->mdev) - 1);
Mark Bloch16c19752018-01-01 13:06:58 +02006061}
6062
6063static int mlx5_ib_stage_uar_init(struct mlx5_ib_dev *dev)
6064{
Eli Cohen5fe9dec2017-01-03 23:55:25 +02006065 dev->mdev->priv.uar = mlx5_get_uars_page(dev->mdev);
Leon Romanovsky444261c2018-04-23 17:01:56 +03006066 return PTR_ERR_OR_ZERO(dev->mdev->priv.uar);
Mark Bloch16c19752018-01-01 13:06:58 +02006067}
6068
6069static void mlx5_ib_stage_uar_cleanup(struct mlx5_ib_dev *dev)
6070{
6071 mlx5_put_uars_page(dev->mdev, dev->mdev->priv.uar);
6072}
6073
Mark Blochb5ca15a2018-01-23 11:16:30 +00006074int mlx5_ib_stage_bfrag_init(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02006075{
6076 int err;
Eli Cohen5fe9dec2017-01-03 23:55:25 +02006077
6078 err = mlx5_alloc_bfreg(dev->mdev, &dev->bfreg, false, false);
6079 if (err)
Mark Bloch16c19752018-01-01 13:06:58 +02006080 return err;
Eli Cohen5fe9dec2017-01-03 23:55:25 +02006081
6082 err = mlx5_alloc_bfreg(dev->mdev, &dev->fp_bfreg, false, true);
6083 if (err)
Mark Bloch16c19752018-01-01 13:06:58 +02006084 mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
Eli Cohen5fe9dec2017-01-03 23:55:25 +02006085
Mark Bloch16c19752018-01-01 13:06:58 +02006086 return err;
6087}
Mark Bloch0837e862016-06-17 15:10:55 +03006088
Mark Blochb5ca15a2018-01-23 11:16:30 +00006089void mlx5_ib_stage_bfrag_cleanup(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02006090{
6091 mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
6092 mlx5_free_bfreg(dev->mdev, &dev->bfreg);
6093}
Eli Cohene126ba92013-07-07 17:25:49 +03006094
Matan Barak8c846602018-03-28 09:27:41 +03006095static int mlx5_ib_stage_populate_specs(struct mlx5_ib_dev *dev)
6096{
6097 return populate_specs_root(dev);
6098}
6099
Mark Blochb5ca15a2018-01-23 11:16:30 +00006100int mlx5_ib_stage_ib_reg_init(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02006101{
6102 return ib_register_device(&dev->ib_dev, NULL);
6103}
6104
Matan Barak8c846602018-03-28 09:27:41 +03006105static void mlx5_ib_stage_depopulate_specs(struct mlx5_ib_dev *dev)
6106{
6107 depopulate_specs_root(dev);
6108}
6109
David S. Miller03fe2de2018-03-23 11:24:57 -04006110void mlx5_ib_stage_pre_ib_reg_umr_cleanup(struct mlx5_ib_dev *dev)
Mark Bloch42cea832018-03-14 09:14:15 +02006111{
6112 destroy_umrc_res(dev);
6113}
6114
Mark Blochb5ca15a2018-01-23 11:16:30 +00006115void mlx5_ib_stage_ib_reg_cleanup(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02006116{
6117 ib_unregister_device(&dev->ib_dev);
6118}
6119
David S. Miller03fe2de2018-03-23 11:24:57 -04006120int mlx5_ib_stage_post_ib_reg_umr_init(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02006121{
6122 return create_umr_res(dev);
6123}
6124
Mark Bloch16c19752018-01-01 13:06:58 +02006125static int mlx5_ib_stage_delay_drop_init(struct mlx5_ib_dev *dev)
6126{
Maor Gottlieb03404e82017-05-30 10:29:13 +03006127 init_delay_drop(dev);
6128
Mark Bloch16c19752018-01-01 13:06:58 +02006129 return 0;
6130}
6131
6132static void mlx5_ib_stage_delay_drop_cleanup(struct mlx5_ib_dev *dev)
6133{
6134 cancel_delay_drop(dev);
6135}
6136
Mark Blochb5ca15a2018-01-23 11:16:30 +00006137int mlx5_ib_stage_class_attr_init(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02006138{
6139 int err;
6140 int i;
6141
Eli Cohene126ba92013-07-07 17:25:49 +03006142 for (i = 0; i < ARRAY_SIZE(mlx5_class_attributes); i++) {
Wei Yongjun281d1a92013-07-30 07:54:26 +08006143 err = device_create_file(&dev->ib_dev.dev,
6144 mlx5_class_attributes[i]);
6145 if (err)
Mark Bloch16c19752018-01-01 13:06:58 +02006146 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03006147 }
6148
Mark Bloch16c19752018-01-01 13:06:58 +02006149 return 0;
6150}
6151
Mark Blochfc385b72018-01-16 14:34:48 +00006152static int mlx5_ib_stage_rep_reg_init(struct mlx5_ib_dev *dev)
6153{
6154 mlx5_ib_register_vport_reps(dev);
6155
6156 return 0;
6157}
6158
6159static void mlx5_ib_stage_rep_reg_cleanup(struct mlx5_ib_dev *dev)
6160{
6161 mlx5_ib_unregister_vport_reps(dev);
6162}
6163
Mark Blochb5ca15a2018-01-23 11:16:30 +00006164void __mlx5_ib_remove(struct mlx5_ib_dev *dev,
6165 const struct mlx5_ib_profile *profile,
6166 int stage)
Mark Bloch16c19752018-01-01 13:06:58 +02006167{
6168 /* Number of stages to cleanup */
6169 while (stage) {
6170 stage--;
6171 if (profile->stage[stage].cleanup)
6172 profile->stage[stage].cleanup(dev);
6173 }
6174
6175 ib_dealloc_device((struct ib_device *)dev);
6176}
6177
Mark Blochb5ca15a2018-01-23 11:16:30 +00006178void *__mlx5_ib_add(struct mlx5_ib_dev *dev,
6179 const struct mlx5_ib_profile *profile)
Mark Bloch16c19752018-01-01 13:06:58 +02006180{
Mark Bloch16c19752018-01-01 13:06:58 +02006181 int err;
6182 int i;
6183
6184 printk_once(KERN_INFO "%s", mlx5_version);
6185
Mark Bloch16c19752018-01-01 13:06:58 +02006186 for (i = 0; i < MLX5_IB_STAGE_MAX; i++) {
6187 if (profile->stage[i].init) {
6188 err = profile->stage[i].init(dev);
6189 if (err)
6190 goto err_out;
6191 }
6192 }
6193
6194 dev->profile = profile;
Eli Cohene126ba92013-07-07 17:25:49 +03006195 dev->ib_active = true;
6196
Jack Morgenstein9603b612014-07-28 23:30:22 +03006197 return dev;
Eli Cohene126ba92013-07-07 17:25:49 +03006198
Mark Bloch16c19752018-01-01 13:06:58 +02006199err_out:
6200 __mlx5_ib_remove(dev, profile, i);
Eli Cohene126ba92013-07-07 17:25:49 +03006201
Jack Morgenstein9603b612014-07-28 23:30:22 +03006202 return NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03006203}
6204
Mark Bloch16c19752018-01-01 13:06:58 +02006205static const struct mlx5_ib_profile pf_profile = {
6206 STAGE_CREATE(MLX5_IB_STAGE_INIT,
6207 mlx5_ib_stage_init_init,
6208 mlx5_ib_stage_init_cleanup),
Mark Bloch9a4ca382018-01-16 14:42:35 +00006209 STAGE_CREATE(MLX5_IB_STAGE_FLOW_DB,
6210 mlx5_ib_stage_flow_db_init,
6211 mlx5_ib_stage_flow_db_cleanup),
Mark Bloch16c19752018-01-01 13:06:58 +02006212 STAGE_CREATE(MLX5_IB_STAGE_CAPS,
6213 mlx5_ib_stage_caps_init,
6214 NULL),
Mark Bloch8e6efa32017-11-06 12:22:13 +00006215 STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB,
6216 mlx5_ib_stage_non_default_cb,
6217 NULL),
Mark Bloch16c19752018-01-01 13:06:58 +02006218 STAGE_CREATE(MLX5_IB_STAGE_ROCE,
6219 mlx5_ib_stage_roce_init,
6220 mlx5_ib_stage_roce_cleanup),
6221 STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES,
6222 mlx5_ib_stage_dev_res_init,
6223 mlx5_ib_stage_dev_res_cleanup),
6224 STAGE_CREATE(MLX5_IB_STAGE_ODP,
6225 mlx5_ib_stage_odp_init,
Mark Bloch3cc297d2018-01-01 13:07:03 +02006226 NULL),
Mark Bloch16c19752018-01-01 13:06:58 +02006227 STAGE_CREATE(MLX5_IB_STAGE_COUNTERS,
6228 mlx5_ib_stage_counters_init,
6229 mlx5_ib_stage_counters_cleanup),
6230 STAGE_CREATE(MLX5_IB_STAGE_CONG_DEBUGFS,
6231 mlx5_ib_stage_cong_debugfs_init,
6232 mlx5_ib_stage_cong_debugfs_cleanup),
6233 STAGE_CREATE(MLX5_IB_STAGE_UAR,
6234 mlx5_ib_stage_uar_init,
6235 mlx5_ib_stage_uar_cleanup),
6236 STAGE_CREATE(MLX5_IB_STAGE_BFREG,
6237 mlx5_ib_stage_bfrag_init,
6238 mlx5_ib_stage_bfrag_cleanup),
Mark Bloch42cea832018-03-14 09:14:15 +02006239 STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR,
6240 NULL,
6241 mlx5_ib_stage_pre_ib_reg_umr_cleanup),
Matan Barak8c846602018-03-28 09:27:41 +03006242 STAGE_CREATE(MLX5_IB_STAGE_SPECS,
6243 mlx5_ib_stage_populate_specs,
6244 mlx5_ib_stage_depopulate_specs),
Mark Bloch16c19752018-01-01 13:06:58 +02006245 STAGE_CREATE(MLX5_IB_STAGE_IB_REG,
6246 mlx5_ib_stage_ib_reg_init,
6247 mlx5_ib_stage_ib_reg_cleanup),
Mark Bloch42cea832018-03-14 09:14:15 +02006248 STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR,
6249 mlx5_ib_stage_post_ib_reg_umr_init,
6250 NULL),
Mark Bloch16c19752018-01-01 13:06:58 +02006251 STAGE_CREATE(MLX5_IB_STAGE_DELAY_DROP,
6252 mlx5_ib_stage_delay_drop_init,
6253 mlx5_ib_stage_delay_drop_cleanup),
6254 STAGE_CREATE(MLX5_IB_STAGE_CLASS_ATTR,
6255 mlx5_ib_stage_class_attr_init,
6256 NULL),
Mark Bloch16c19752018-01-01 13:06:58 +02006257};
6258
Mark Blochb5ca15a2018-01-23 11:16:30 +00006259static const struct mlx5_ib_profile nic_rep_profile = {
6260 STAGE_CREATE(MLX5_IB_STAGE_INIT,
6261 mlx5_ib_stage_init_init,
6262 mlx5_ib_stage_init_cleanup),
6263 STAGE_CREATE(MLX5_IB_STAGE_FLOW_DB,
6264 mlx5_ib_stage_flow_db_init,
6265 mlx5_ib_stage_flow_db_cleanup),
6266 STAGE_CREATE(MLX5_IB_STAGE_CAPS,
6267 mlx5_ib_stage_caps_init,
6268 NULL),
6269 STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB,
6270 mlx5_ib_stage_rep_non_default_cb,
6271 NULL),
6272 STAGE_CREATE(MLX5_IB_STAGE_ROCE,
6273 mlx5_ib_stage_rep_roce_init,
6274 mlx5_ib_stage_rep_roce_cleanup),
6275 STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES,
6276 mlx5_ib_stage_dev_res_init,
6277 mlx5_ib_stage_dev_res_cleanup),
6278 STAGE_CREATE(MLX5_IB_STAGE_COUNTERS,
6279 mlx5_ib_stage_counters_init,
6280 mlx5_ib_stage_counters_cleanup),
6281 STAGE_CREATE(MLX5_IB_STAGE_UAR,
6282 mlx5_ib_stage_uar_init,
6283 mlx5_ib_stage_uar_cleanup),
6284 STAGE_CREATE(MLX5_IB_STAGE_BFREG,
6285 mlx5_ib_stage_bfrag_init,
6286 mlx5_ib_stage_bfrag_cleanup),
David S. Miller03fe2de2018-03-23 11:24:57 -04006287 STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR,
6288 NULL,
6289 mlx5_ib_stage_pre_ib_reg_umr_cleanup),
Matan Barak8c846602018-03-28 09:27:41 +03006290 STAGE_CREATE(MLX5_IB_STAGE_SPECS,
6291 mlx5_ib_stage_populate_specs,
6292 mlx5_ib_stage_depopulate_specs),
Mark Blochb5ca15a2018-01-23 11:16:30 +00006293 STAGE_CREATE(MLX5_IB_STAGE_IB_REG,
6294 mlx5_ib_stage_ib_reg_init,
6295 mlx5_ib_stage_ib_reg_cleanup),
David S. Miller03fe2de2018-03-23 11:24:57 -04006296 STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR,
6297 mlx5_ib_stage_post_ib_reg_umr_init,
6298 NULL),
Mark Blochb5ca15a2018-01-23 11:16:30 +00006299 STAGE_CREATE(MLX5_IB_STAGE_CLASS_ATTR,
6300 mlx5_ib_stage_class_attr_init,
6301 NULL),
6302 STAGE_CREATE(MLX5_IB_STAGE_REP_REG,
6303 mlx5_ib_stage_rep_reg_init,
6304 mlx5_ib_stage_rep_reg_cleanup),
6305};
6306
Leon Romanovskye3f1ed12018-07-08 12:55:43 +03006307static void *mlx5_ib_add_slave_port(struct mlx5_core_dev *mdev)
Daniel Jurgens32f69e42018-01-04 17:25:36 +02006308{
6309 struct mlx5_ib_multiport_info *mpi;
6310 struct mlx5_ib_dev *dev;
6311 bool bound = false;
6312 int err;
6313
6314 mpi = kzalloc(sizeof(*mpi), GFP_KERNEL);
6315 if (!mpi)
6316 return NULL;
6317
6318 mpi->mdev = mdev;
6319
6320 err = mlx5_query_nic_vport_system_image_guid(mdev,
6321 &mpi->sys_image_guid);
6322 if (err) {
6323 kfree(mpi);
6324 return NULL;
6325 }
6326
6327 mutex_lock(&mlx5_ib_multiport_mutex);
6328 list_for_each_entry(dev, &mlx5_ib_dev_list, ib_dev_list) {
6329 if (dev->sys_image_guid == mpi->sys_image_guid)
6330 bound = mlx5_ib_bind_slave_port(dev, mpi);
6331
6332 if (bound) {
6333 rdma_roce_rescan_device(&dev->ib_dev);
6334 break;
6335 }
6336 }
6337
6338 if (!bound) {
6339 list_add_tail(&mpi->list, &mlx5_ib_unaffiliated_port_list);
6340 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 +02006341 }
6342 mutex_unlock(&mlx5_ib_multiport_mutex);
6343
6344 return mpi;
6345}
6346
Mark Bloch16c19752018-01-01 13:06:58 +02006347static void *mlx5_ib_add(struct mlx5_core_dev *mdev)
6348{
Daniel Jurgens32f69e42018-01-04 17:25:36 +02006349 enum rdma_link_layer ll;
Mark Blochb5ca15a2018-01-23 11:16:30 +00006350 struct mlx5_ib_dev *dev;
Daniel Jurgens32f69e42018-01-04 17:25:36 +02006351 int port_type_cap;
6352
Mark Blochb5ca15a2018-01-23 11:16:30 +00006353 printk_once(KERN_INFO "%s", mlx5_version);
6354
Daniel Jurgens32f69e42018-01-04 17:25:36 +02006355 port_type_cap = MLX5_CAP_GEN(mdev, port_type);
6356 ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
6357
Leon Romanovskye3f1ed12018-07-08 12:55:43 +03006358 if (mlx5_core_is_mp_slave(mdev) && ll == IB_LINK_LAYER_ETHERNET)
6359 return mlx5_ib_add_slave_port(mdev);
Daniel Jurgens32f69e42018-01-04 17:25:36 +02006360
Mark Blochb5ca15a2018-01-23 11:16:30 +00006361 dev = (struct mlx5_ib_dev *)ib_alloc_device(sizeof(*dev));
6362 if (!dev)
6363 return NULL;
6364
6365 dev->mdev = mdev;
6366 dev->num_ports = max(MLX5_CAP_GEN(mdev, num_ports),
6367 MLX5_CAP_GEN(mdev, num_vhca_ports));
6368
6369 if (MLX5_VPORT_MANAGER(mdev) &&
6370 mlx5_ib_eswitch_mode(mdev->priv.eswitch) == SRIOV_OFFLOADS) {
6371 dev->rep = mlx5_ib_vport_rep(mdev->priv.eswitch, 0);
6372
6373 return __mlx5_ib_add(dev, &nic_rep_profile);
6374 }
6375
6376 return __mlx5_ib_add(dev, &pf_profile);
Mark Bloch16c19752018-01-01 13:06:58 +02006377}
6378
Jack Morgenstein9603b612014-07-28 23:30:22 +03006379static void mlx5_ib_remove(struct mlx5_core_dev *mdev, void *context)
Eli Cohene126ba92013-07-07 17:25:49 +03006380{
Daniel Jurgens32f69e42018-01-04 17:25:36 +02006381 struct mlx5_ib_multiport_info *mpi;
6382 struct mlx5_ib_dev *dev;
Haggai Eran6aec21f2014-12-11 17:04:23 +02006383
Daniel Jurgens32f69e42018-01-04 17:25:36 +02006384 if (mlx5_core_is_mp_slave(mdev)) {
6385 mpi = context;
6386 mutex_lock(&mlx5_ib_multiport_mutex);
6387 if (mpi->ibdev)
6388 mlx5_ib_unbind_slave_port(mpi->ibdev, mpi);
6389 list_del(&mpi->list);
6390 mutex_unlock(&mlx5_ib_multiport_mutex);
6391 return;
6392 }
6393
6394 dev = context;
Mark Bloch16c19752018-01-01 13:06:58 +02006395 __mlx5_ib_remove(dev, dev->profile, MLX5_IB_STAGE_MAX);
Eli Cohene126ba92013-07-07 17:25:49 +03006396}
6397
Jack Morgenstein9603b612014-07-28 23:30:22 +03006398static struct mlx5_interface mlx5_ib_interface = {
6399 .add = mlx5_ib_add,
6400 .remove = mlx5_ib_remove,
6401 .event = mlx5_ib_event,
Artemy Kovalyovd9aaed82017-01-02 11:37:46 +02006402#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
6403 .pfault = mlx5_ib_pfault,
6404#endif
Saeed Mahameed64613d942015-04-02 17:07:34 +03006405 .protocol = MLX5_INTERFACE_PROTOCOL_IB,
Eli Cohene126ba92013-07-07 17:25:49 +03006406};
6407
Ilya Lesokhinc44ef992018-03-13 15:18:48 +02006408unsigned long mlx5_ib_get_xlt_emergency_page(void)
6409{
6410 mutex_lock(&xlt_emergency_page_mutex);
6411 return xlt_emergency_page;
6412}
6413
6414void mlx5_ib_put_xlt_emergency_page(void)
6415{
6416 mutex_unlock(&xlt_emergency_page_mutex);
6417}
6418
Eli Cohene126ba92013-07-07 17:25:49 +03006419static int __init mlx5_ib_init(void)
6420{
Haggai Eran6aec21f2014-12-11 17:04:23 +02006421 int err;
6422
Ilya Lesokhinc44ef992018-03-13 15:18:48 +02006423 xlt_emergency_page = __get_free_page(GFP_KERNEL);
6424 if (!xlt_emergency_page)
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02006425 return -ENOMEM;
6426
Ilya Lesokhinc44ef992018-03-13 15:18:48 +02006427 mutex_init(&xlt_emergency_page_mutex);
6428
6429 mlx5_ib_event_wq = alloc_ordered_workqueue("mlx5_ib_event_wq", 0);
6430 if (!mlx5_ib_event_wq) {
6431 free_page(xlt_emergency_page);
6432 return -ENOMEM;
6433 }
6434
Artemy Kovalyov81713d32017-01-18 16:58:11 +02006435 mlx5_ib_odp_init();
Jack Morgenstein9603b612014-07-28 23:30:22 +03006436
Haggai Eran6aec21f2014-12-11 17:04:23 +02006437 err = mlx5_register_interface(&mlx5_ib_interface);
Haggai Eran6aec21f2014-12-11 17:04:23 +02006438
6439 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03006440}
6441
6442static void __exit mlx5_ib_cleanup(void)
6443{
Jack Morgenstein9603b612014-07-28 23:30:22 +03006444 mlx5_unregister_interface(&mlx5_ib_interface);
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02006445 destroy_workqueue(mlx5_ib_event_wq);
Ilya Lesokhinc44ef992018-03-13 15:18:48 +02006446 mutex_destroy(&xlt_emergency_page_mutex);
6447 free_page(xlt_emergency_page);
Eli Cohene126ba92013-07-07 17:25:49 +03006448}
6449
6450module_init(mlx5_ib_init);
6451module_exit(mlx5_ib_cleanup);