blob: 239d70833afc85e89d0aa72d285022d196beaa31 [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"
Leon Romanovskyf3da6572018-11-28 20:53:41 +020063#include "srq.h"
Boris Pismenny3346c482017-08-20 15:13:08 +030064#include <linux/mlx5/fs_helpers.h>
Aviad Yehezkelc6475a02018-03-28 09:27:50 +030065#include <linux/mlx5/accel.h>
Matan Barak8c846602018-03-28 09:27:41 +030066#include <rdma/uverbs_std_types.h>
Aviad Yehezkelc6475a02018-03-28 09:27:50 +030067#include <rdma/mlx5_user_ioctl_verbs.h>
68#include <rdma/mlx5_user_ioctl_cmds.h>
Matan Barak8c846602018-03-28 09:27:41 +030069
70#define UVERBS_MODULE_NAME mlx5_ib
71#include <rdma/uverbs_named_ioctl.h>
Eli Cohene126ba92013-07-07 17:25:49 +030072
73#define DRIVER_NAME "mlx5_ib"
Tariq Toukanb3599112017-02-22 17:45:46 +020074#define DRIVER_VERSION "5.0-0"
Eli Cohene126ba92013-07-07 17:25:49 +030075
76MODULE_AUTHOR("Eli Cohen <eli@mellanox.com>");
77MODULE_DESCRIPTION("Mellanox Connect-IB HCA IB driver");
78MODULE_LICENSE("Dual BSD/GPL");
Eli Cohene126ba92013-07-07 17:25:49 +030079
Eli Cohene126ba92013-07-07 17:25:49 +030080static char mlx5_version[] =
81 DRIVER_NAME ": Mellanox Connect-IB Infiniband driver v"
Tariq Toukanb3599112017-02-22 17:45:46 +020082 DRIVER_VERSION "\n";
Eli Cohene126ba92013-07-07 17:25:49 +030083
Daniel Jurgensd69a24e2018-01-04 17:25:37 +020084struct mlx5_ib_event_work {
85 struct work_struct work;
Saeed Mahameeddf097a22018-11-26 14:39:00 -080086 union {
87 struct mlx5_ib_dev *dev;
88 struct mlx5_ib_multiport_info *mpi;
89 };
90 bool is_slave;
Saeed Mahameed134e9342018-11-26 14:39:02 -080091 unsigned int event;
Saeed Mahameeddf097a22018-11-26 14:39:00 -080092 void *param;
Daniel Jurgensd69a24e2018-01-04 17:25:37 +020093};
94
Eran Ben Elishada7525d2015-12-14 16:34:10 +020095enum {
96 MLX5_ATOMIC_SIZE_QP_8BYTES = 1 << 3,
97};
Majd Dibbiny1b5daf12015-06-04 19:30:46 +030098
Daniel Jurgensd69a24e2018-01-04 17:25:37 +020099static struct workqueue_struct *mlx5_ib_event_wq;
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200100static LIST_HEAD(mlx5_ib_unaffiliated_port_list);
101static LIST_HEAD(mlx5_ib_dev_list);
102/*
103 * This mutex should be held when accessing either of the above lists
104 */
105static DEFINE_MUTEX(mlx5_ib_multiport_mutex);
106
Ilya Lesokhinc44ef992018-03-13 15:18:48 +0200107/* We can't use an array for xlt_emergency_page because dma_map_single
108 * doesn't work on kernel modules memory
109 */
110static unsigned long xlt_emergency_page;
111static struct mutex xlt_emergency_page_mutex;
112
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200113struct mlx5_ib_dev *mlx5_ib_get_ibdev_from_mpi(struct mlx5_ib_multiport_info *mpi)
114{
115 struct mlx5_ib_dev *dev;
116
117 mutex_lock(&mlx5_ib_multiport_mutex);
118 dev = mpi->ibdev;
119 mutex_unlock(&mlx5_ib_multiport_mutex);
120 return dev;
121}
122
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300123static enum rdma_link_layer
Achiad Shochatebd61f62015-12-23 18:47:16 +0200124mlx5_port_type_cap_to_rdma_ll(int port_type_cap)
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300125{
Achiad Shochatebd61f62015-12-23 18:47:16 +0200126 switch (port_type_cap) {
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300127 case MLX5_CAP_PORT_TYPE_IB:
128 return IB_LINK_LAYER_INFINIBAND;
129 case MLX5_CAP_PORT_TYPE_ETH:
130 return IB_LINK_LAYER_ETHERNET;
131 default:
132 return IB_LINK_LAYER_UNSPECIFIED;
133 }
134}
135
Achiad Shochatebd61f62015-12-23 18:47:16 +0200136static enum rdma_link_layer
137mlx5_ib_port_link_layer(struct ib_device *device, u8 port_num)
138{
139 struct mlx5_ib_dev *dev = to_mdev(device);
140 int port_type_cap = MLX5_CAP_GEN(dev->mdev, port_type);
141
142 return mlx5_port_type_cap_to_rdma_ll(port_type_cap);
143}
144
Moni Shouafd65f1b2017-05-30 09:56:05 +0300145static int get_port_state(struct ib_device *ibdev,
146 u8 port_num,
147 enum ib_port_state *state)
148{
149 struct ib_port_attr attr;
150 int ret;
151
152 memset(&attr, 0, sizeof(attr));
Kamal Heib3023a1e2018-12-10 21:09:48 +0200153 ret = ibdev->ops.query_port(ibdev, port_num, &attr);
Moni Shouafd65f1b2017-05-30 09:56:05 +0300154 if (!ret)
155 *state = attr.state;
156 return ret;
157}
158
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200159static int mlx5_netdev_event(struct notifier_block *this,
160 unsigned long event, void *ptr)
161{
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200162 struct mlx5_roce *roce = container_of(this, struct mlx5_roce, nb);
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200163 struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200164 u8 port_num = roce->native_port_num;
165 struct mlx5_core_dev *mdev;
166 struct mlx5_ib_dev *ibdev;
167
168 ibdev = roce->dev;
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200169 mdev = mlx5_ib_get_native_port_mdev(ibdev, port_num, NULL);
170 if (!mdev)
171 return NOTIFY_DONE;
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200172
Aviv Heller5ec8c832016-09-18 20:48:00 +0300173 switch (event) {
174 case NETDEV_REGISTER:
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200175 write_lock(&roce->netdev_lock);
Mark Blochbcf87f12018-01-16 15:02:36 +0000176 if (ibdev->rep) {
177 struct mlx5_eswitch *esw = ibdev->mdev->priv.eswitch;
178 struct net_device *rep_ndev;
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200179
Mark Blochbcf87f12018-01-16 15:02:36 +0000180 rep_ndev = mlx5_ib_get_rep_netdev(esw,
181 ibdev->rep->vport);
182 if (rep_ndev == ndev)
Or Gerlitz842a9c82018-12-11 18:10:43 +0200183 roce->netdev = ndev;
Parav Pandit84a6a7a2018-04-23 17:01:55 +0300184 } else if (ndev->dev.parent == &mdev->pdev->dev) {
Or Gerlitz842a9c82018-12-11 18:10:43 +0200185 roce->netdev = ndev;
Mark Blochbcf87f12018-01-16 15:02:36 +0000186 }
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200187 write_unlock(&roce->netdev_lock);
Aviv Heller5ec8c832016-09-18 20:48:00 +0300188 break;
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200189
Or Gerlitz842a9c82018-12-11 18:10:43 +0200190 case NETDEV_UNREGISTER:
191 write_lock(&roce->netdev_lock);
192 if (roce->netdev == ndev)
193 roce->netdev = NULL;
194 write_unlock(&roce->netdev_lock);
195 break;
196
Moni Shouafd65f1b2017-05-30 09:56:05 +0300197 case NETDEV_CHANGE:
Aviv Heller5ec8c832016-09-18 20:48:00 +0300198 case NETDEV_UP:
Aviv Heller88621df2016-09-18 20:48:02 +0300199 case NETDEV_DOWN: {
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200200 struct net_device *lag_ndev = mlx5_lag_get_roce_netdev(mdev);
Aviv Heller88621df2016-09-18 20:48:02 +0300201 struct net_device *upper = NULL;
202
203 if (lag_ndev) {
204 upper = netdev_master_upper_dev_get(lag_ndev);
205 dev_put(lag_ndev);
206 }
207
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200208 if ((upper == ndev || (!upper && ndev == roce->netdev))
Aviv Heller88621df2016-09-18 20:48:02 +0300209 && ibdev->ib_active) {
Bart Van Assche626bc022016-12-05 17:18:08 -0800210 struct ib_event ibev = { };
Moni Shouafd65f1b2017-05-30 09:56:05 +0300211 enum ib_port_state port_state;
Aviv Heller5ec8c832016-09-18 20:48:00 +0300212
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200213 if (get_port_state(&ibdev->ib_dev, port_num,
214 &port_state))
215 goto done;
Moni Shouafd65f1b2017-05-30 09:56:05 +0300216
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200217 if (roce->last_port_state == port_state)
218 goto done;
Moni Shouafd65f1b2017-05-30 09:56:05 +0300219
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200220 roce->last_port_state = port_state;
Aviv Heller5ec8c832016-09-18 20:48:00 +0300221 ibev.device = &ibdev->ib_dev;
Moni Shouafd65f1b2017-05-30 09:56:05 +0300222 if (port_state == IB_PORT_DOWN)
223 ibev.event = IB_EVENT_PORT_ERR;
224 else if (port_state == IB_PORT_ACTIVE)
225 ibev.event = IB_EVENT_PORT_ACTIVE;
226 else
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200227 goto done;
Moni Shouafd65f1b2017-05-30 09:56:05 +0300228
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200229 ibev.element.port_num = port_num;
Aviv Heller5ec8c832016-09-18 20:48:00 +0300230 ib_dispatch_event(&ibev);
231 }
232 break;
Aviv Heller88621df2016-09-18 20:48:02 +0300233 }
Aviv Heller5ec8c832016-09-18 20:48:00 +0300234
235 default:
236 break;
237 }
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200238done:
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200239 mlx5_ib_put_native_port_mdev(ibdev, port_num);
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200240 return NOTIFY_DONE;
241}
242
243static struct net_device *mlx5_ib_get_netdev(struct ib_device *device,
244 u8 port_num)
245{
246 struct mlx5_ib_dev *ibdev = to_mdev(device);
247 struct net_device *ndev;
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200248 struct mlx5_core_dev *mdev;
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200249
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200250 mdev = mlx5_ib_get_native_port_mdev(ibdev, port_num, NULL);
251 if (!mdev)
252 return NULL;
253
254 ndev = mlx5_lag_get_roce_netdev(mdev);
Aviv Heller88621df2016-09-18 20:48:02 +0300255 if (ndev)
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200256 goto out;
Aviv Heller88621df2016-09-18 20:48:02 +0300257
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200258 /* Ensure ndev does not disappear before we invoke dev_hold()
259 */
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200260 read_lock(&ibdev->roce[port_num - 1].netdev_lock);
261 ndev = ibdev->roce[port_num - 1].netdev;
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200262 if (ndev)
263 dev_hold(ndev);
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200264 read_unlock(&ibdev->roce[port_num - 1].netdev_lock);
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200265
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200266out:
267 mlx5_ib_put_native_port_mdev(ibdev, port_num);
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200268 return ndev;
269}
270
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200271struct mlx5_core_dev *mlx5_ib_get_native_port_mdev(struct mlx5_ib_dev *ibdev,
272 u8 ib_port_num,
273 u8 *native_port_num)
274{
275 enum rdma_link_layer ll = mlx5_ib_port_link_layer(&ibdev->ib_dev,
276 ib_port_num);
277 struct mlx5_core_dev *mdev = NULL;
278 struct mlx5_ib_multiport_info *mpi;
279 struct mlx5_ib_port *port;
280
Mark Bloch210b1f72018-03-05 20:09:47 +0200281 if (!mlx5_core_mp_enabled(ibdev->mdev) ||
282 ll != IB_LINK_LAYER_ETHERNET) {
283 if (native_port_num)
284 *native_port_num = ib_port_num;
285 return ibdev->mdev;
286 }
287
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200288 if (native_port_num)
289 *native_port_num = 1;
290
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200291 port = &ibdev->port[ib_port_num - 1];
292 if (!port)
293 return NULL;
294
295 spin_lock(&port->mp.mpi_lock);
296 mpi = ibdev->port[ib_port_num - 1].mp.mpi;
297 if (mpi && !mpi->unaffiliate) {
298 mdev = mpi->mdev;
299 /* If it's the master no need to refcount, it'll exist
300 * as long as the ib_dev exists.
301 */
302 if (!mpi->is_master)
303 mpi->mdev_refcnt++;
304 }
305 spin_unlock(&port->mp.mpi_lock);
306
307 return mdev;
308}
309
310void mlx5_ib_put_native_port_mdev(struct mlx5_ib_dev *ibdev, u8 port_num)
311{
312 enum rdma_link_layer ll = mlx5_ib_port_link_layer(&ibdev->ib_dev,
313 port_num);
314 struct mlx5_ib_multiport_info *mpi;
315 struct mlx5_ib_port *port;
316
317 if (!mlx5_core_mp_enabled(ibdev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
318 return;
319
320 port = &ibdev->port[port_num - 1];
321
322 spin_lock(&port->mp.mpi_lock);
323 mpi = ibdev->port[port_num - 1].mp.mpi;
324 if (mpi->is_master)
325 goto out;
326
327 mpi->mdev_refcnt--;
328 if (mpi->unaffiliate)
329 complete(&mpi->unref_comp);
330out:
331 spin_unlock(&port->mp.mpi_lock);
332}
333
Aya Levin08e86762019-02-12 22:55:46 -0800334static int translate_eth_legacy_proto_oper(u32 eth_proto_oper, u8 *active_speed,
335 u8 *active_width)
Noa Osherovichf1b65df2017-04-20 20:53:33 +0300336{
337 switch (eth_proto_oper) {
338 case MLX5E_PROT_MASK(MLX5E_1000BASE_CX_SGMII):
339 case MLX5E_PROT_MASK(MLX5E_1000BASE_KX):
340 case MLX5E_PROT_MASK(MLX5E_100BASE_TX):
341 case MLX5E_PROT_MASK(MLX5E_1000BASE_T):
342 *active_width = IB_WIDTH_1X;
343 *active_speed = IB_SPEED_SDR;
344 break;
345 case MLX5E_PROT_MASK(MLX5E_10GBASE_T):
346 case MLX5E_PROT_MASK(MLX5E_10GBASE_CX4):
347 case MLX5E_PROT_MASK(MLX5E_10GBASE_KX4):
348 case MLX5E_PROT_MASK(MLX5E_10GBASE_KR):
349 case MLX5E_PROT_MASK(MLX5E_10GBASE_CR):
350 case MLX5E_PROT_MASK(MLX5E_10GBASE_SR):
351 case MLX5E_PROT_MASK(MLX5E_10GBASE_ER):
352 *active_width = IB_WIDTH_1X;
353 *active_speed = IB_SPEED_QDR;
354 break;
355 case MLX5E_PROT_MASK(MLX5E_25GBASE_CR):
356 case MLX5E_PROT_MASK(MLX5E_25GBASE_KR):
357 case MLX5E_PROT_MASK(MLX5E_25GBASE_SR):
358 *active_width = IB_WIDTH_1X;
359 *active_speed = IB_SPEED_EDR;
360 break;
361 case MLX5E_PROT_MASK(MLX5E_40GBASE_CR4):
362 case MLX5E_PROT_MASK(MLX5E_40GBASE_KR4):
363 case MLX5E_PROT_MASK(MLX5E_40GBASE_SR4):
364 case MLX5E_PROT_MASK(MLX5E_40GBASE_LR4):
365 *active_width = IB_WIDTH_4X;
366 *active_speed = IB_SPEED_QDR;
367 break;
368 case MLX5E_PROT_MASK(MLX5E_50GBASE_CR2):
369 case MLX5E_PROT_MASK(MLX5E_50GBASE_KR2):
370 case MLX5E_PROT_MASK(MLX5E_50GBASE_SR2):
371 *active_width = IB_WIDTH_1X;
372 *active_speed = IB_SPEED_HDR;
373 break;
374 case MLX5E_PROT_MASK(MLX5E_56GBASE_R4):
375 *active_width = IB_WIDTH_4X;
376 *active_speed = IB_SPEED_FDR;
377 break;
378 case MLX5E_PROT_MASK(MLX5E_100GBASE_CR4):
379 case MLX5E_PROT_MASK(MLX5E_100GBASE_SR4):
380 case MLX5E_PROT_MASK(MLX5E_100GBASE_KR4):
381 case MLX5E_PROT_MASK(MLX5E_100GBASE_LR4):
382 *active_width = IB_WIDTH_4X;
383 *active_speed = IB_SPEED_EDR;
384 break;
385 default:
386 return -EINVAL;
387 }
388
389 return 0;
390}
391
Aya Levin08e86762019-02-12 22:55:46 -0800392static int translate_eth_ext_proto_oper(u32 eth_proto_oper, u8 *active_speed,
393 u8 *active_width)
394{
395 switch (eth_proto_oper) {
396 case MLX5E_PROT_MASK(MLX5E_SGMII_100M):
397 case MLX5E_PROT_MASK(MLX5E_1000BASE_X_SGMII):
398 *active_width = IB_WIDTH_1X;
399 *active_speed = IB_SPEED_SDR;
400 break;
401 case MLX5E_PROT_MASK(MLX5E_5GBASE_R):
402 *active_width = IB_WIDTH_1X;
403 *active_speed = IB_SPEED_DDR;
404 break;
405 case MLX5E_PROT_MASK(MLX5E_10GBASE_XFI_XAUI_1):
406 *active_width = IB_WIDTH_1X;
407 *active_speed = IB_SPEED_QDR;
408 break;
409 case MLX5E_PROT_MASK(MLX5E_40GBASE_XLAUI_4_XLPPI_4):
410 *active_width = IB_WIDTH_4X;
411 *active_speed = IB_SPEED_QDR;
412 break;
413 case MLX5E_PROT_MASK(MLX5E_25GAUI_1_25GBASE_CR_KR):
414 *active_width = IB_WIDTH_1X;
415 *active_speed = IB_SPEED_EDR;
416 break;
417 case MLX5E_PROT_MASK(MLX5E_50GAUI_2_LAUI_2_50GBASE_CR2_KR2):
Aya Levincd272872019-03-11 14:35:58 +0200418 *active_width = IB_WIDTH_2X;
419 *active_speed = IB_SPEED_EDR;
420 break;
Aya Levin08e86762019-02-12 22:55:46 -0800421 case MLX5E_PROT_MASK(MLX5E_50GAUI_1_LAUI_1_50GBASE_CR_KR):
422 *active_width = IB_WIDTH_1X;
423 *active_speed = IB_SPEED_HDR;
424 break;
Aya Levincd272872019-03-11 14:35:58 +0200425 case MLX5E_PROT_MASK(MLX5E_CAUI_4_100GBASE_CR4_KR4):
426 *active_width = IB_WIDTH_4X;
427 *active_speed = IB_SPEED_EDR;
428 break;
Aya Levin08e86762019-02-12 22:55:46 -0800429 case MLX5E_PROT_MASK(MLX5E_100GAUI_2_100GBASE_CR2_KR2):
430 *active_width = IB_WIDTH_2X;
431 *active_speed = IB_SPEED_HDR;
432 break;
433 case MLX5E_PROT_MASK(MLX5E_200GAUI_4_200GBASE_CR4_KR4):
434 *active_width = IB_WIDTH_4X;
435 *active_speed = IB_SPEED_HDR;
436 break;
437 default:
438 return -EINVAL;
439 }
440
441 return 0;
442}
443
444static int translate_eth_proto_oper(u32 eth_proto_oper, u8 *active_speed,
445 u8 *active_width, bool ext)
446{
447 return ext ?
448 translate_eth_ext_proto_oper(eth_proto_oper, active_speed,
449 active_width) :
450 translate_eth_legacy_proto_oper(eth_proto_oper, active_speed,
451 active_width);
452}
453
Ilan Tayari095b0922017-05-14 16:04:30 +0300454static int mlx5_query_port_roce(struct ib_device *device, u8 port_num,
455 struct ib_port_attr *props)
Achiad Shochat3f89a642015-12-23 18:47:21 +0200456{
457 struct mlx5_ib_dev *dev = to_mdev(device);
Aya Levinbc4e12f2019-02-12 22:55:43 -0800458 u32 out[MLX5_ST_SZ_DW(ptys_reg)] = {0};
Colin Ian Kingda005f92018-01-09 15:55:43 +0000459 struct mlx5_core_dev *mdev;
Aviv Heller88621df2016-09-18 20:48:02 +0300460 struct net_device *ndev, *upper;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200461 enum ib_mtu ndev_ib_mtu;
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200462 bool put_mdev = true;
Leon Romanovskyc876a1b2016-01-09 13:06:25 +0200463 u16 qkey_viol_cntr;
Noa Osherovichf1b65df2017-04-20 20:53:33 +0300464 u32 eth_prot_oper;
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200465 u8 mdev_port_num;
Aya Levin08e86762019-02-12 22:55:46 -0800466 bool ext;
Ilan Tayari095b0922017-05-14 16:04:30 +0300467 int err;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200468
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200469 mdev = mlx5_ib_get_native_port_mdev(dev, port_num, &mdev_port_num);
470 if (!mdev) {
471 /* This means the port isn't affiliated yet. Get the
472 * info for the master port instead.
473 */
474 put_mdev = false;
475 mdev = dev->mdev;
476 mdev_port_num = 1;
477 port_num = 1;
478 }
479
Noa Osherovichf1b65df2017-04-20 20:53:33 +0300480 /* Possible bad flows are checked before filling out props so in case
481 * of an error it will still be zeroed out.
Noa Osherovich50f22fd2017-04-20 20:53:32 +0300482 */
Aya Levinbc4e12f2019-02-12 22:55:43 -0800483 err = mlx5_query_port_ptys(mdev, out, sizeof(out), MLX5_PTYS_EN,
484 mdev_port_num);
Ilan Tayari095b0922017-05-14 16:04:30 +0300485 if (err)
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200486 goto out;
Aya Levin08e86762019-02-12 22:55:46 -0800487 ext = MLX5_CAP_PCAM_FEATURE(dev->mdev, ptys_extended_ethernet);
488 eth_prot_oper = MLX5_GET_ETH_PROTO(ptys_reg, out, ext, eth_proto_oper);
Noa Osherovichf1b65df2017-04-20 20:53:33 +0300489
Honggang Li7672ed32018-03-16 10:37:13 +0800490 props->active_width = IB_WIDTH_4X;
491 props->active_speed = IB_SPEED_QDR;
492
Noa Osherovichf1b65df2017-04-20 20:53:33 +0300493 translate_eth_proto_oper(eth_prot_oper, &props->active_speed,
Aya Levin08e86762019-02-12 22:55:46 -0800494 &props->active_width, ext);
Achiad Shochat3f89a642015-12-23 18:47:21 +0200495
Jason Gunthorpe2f944c02018-07-04 15:57:48 +0300496 props->port_cap_flags |= IB_PORT_CM_SUP;
497 props->ip_gids = true;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200498
499 props->gid_tbl_len = MLX5_CAP_ROCE(dev->mdev,
500 roce_address_table_size);
501 props->max_mtu = IB_MTU_4096;
502 props->max_msg_sz = 1 << MLX5_CAP_GEN(dev->mdev, log_max_msg);
503 props->pkey_tbl_len = 1;
504 props->state = IB_PORT_DOWN;
505 props->phys_state = 3;
506
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200507 mlx5_query_nic_vport_qkey_viol_cntr(mdev, &qkey_viol_cntr);
Leon Romanovskyc876a1b2016-01-09 13:06:25 +0200508 props->qkey_viol_cntr = qkey_viol_cntr;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200509
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200510 /* If this is a stub query for an unaffiliated port stop here */
511 if (!put_mdev)
512 goto out;
513
Achiad Shochat3f89a642015-12-23 18:47:21 +0200514 ndev = mlx5_ib_get_netdev(device, port_num);
515 if (!ndev)
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200516 goto out;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200517
Aviv Heller7c34ec12018-08-23 13:47:53 +0300518 if (dev->lag_active) {
Aviv Heller88621df2016-09-18 20:48:02 +0300519 rcu_read_lock();
520 upper = netdev_master_upper_dev_get_rcu(ndev);
521 if (upper) {
522 dev_put(ndev);
523 ndev = upper;
524 dev_hold(ndev);
525 }
526 rcu_read_unlock();
527 }
528
Achiad Shochat3f89a642015-12-23 18:47:21 +0200529 if (netif_running(ndev) && netif_carrier_ok(ndev)) {
530 props->state = IB_PORT_ACTIVE;
531 props->phys_state = 5;
532 }
533
534 ndev_ib_mtu = iboe_get_mtu(ndev->mtu);
535
536 dev_put(ndev);
537
538 props->active_mtu = min(props->max_mtu, ndev_ib_mtu);
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200539out:
540 if (put_mdev)
541 mlx5_ib_put_native_port_mdev(dev, port_num);
542 return err;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200543}
544
Parav Panditcf34e1f2019-01-27 20:35:50 +0200545struct mlx5_ib_vlan_info {
546 u16 vlan_id;
547 bool vlan;
548};
549
550static int get_lower_dev_vlan(struct net_device *lower_dev, void *data)
551{
552 struct mlx5_ib_vlan_info *vlan_info = data;
553
554 if (is_vlan_dev(lower_dev)) {
555 vlan_info->vlan = true;
556 vlan_info->vlan_id = vlan_dev_vlan_id(lower_dev);
557 }
558 /* We are interested only in first level vlan device, so
559 * always return 1 to stop iterating over next level devices.
560 */
561 return 1;
562}
563
Ilan Tayari095b0922017-05-14 16:04:30 +0300564static int set_roce_addr(struct mlx5_ib_dev *dev, u8 port_num,
565 unsigned int index, const union ib_gid *gid,
566 const struct ib_gid_attr *attr)
Achiad Shochat3cca2602015-12-23 18:47:23 +0200567{
Ilan Tayari095b0922017-05-14 16:04:30 +0300568 enum ib_gid_type gid_type = IB_GID_TYPE_IB;
Parav Panditcf34e1f2019-01-27 20:35:50 +0200569 struct mlx5_ib_vlan_info vlan_info = { };
Ilan Tayari095b0922017-05-14 16:04:30 +0300570 u8 roce_version = 0;
571 u8 roce_l3_type = 0;
Ilan Tayari095b0922017-05-14 16:04:30 +0300572 u8 mac[ETH_ALEN];
Achiad Shochat3cca2602015-12-23 18:47:23 +0200573
Ilan Tayari095b0922017-05-14 16:04:30 +0300574 if (gid) {
575 gid_type = attr->gid_type;
576 ether_addr_copy(mac, attr->ndev->dev_addr);
Achiad Shochat3cca2602015-12-23 18:47:23 +0200577
Ilan Tayari095b0922017-05-14 16:04:30 +0300578 if (is_vlan_dev(attr->ndev)) {
Parav Panditcf34e1f2019-01-27 20:35:50 +0200579 vlan_info.vlan = true;
580 vlan_info.vlan_id = vlan_dev_vlan_id(attr->ndev);
581 } else {
582 /* If the netdev is upper device and if it's lower
583 * lower device is vlan device, consider vlan id of
584 * the lower vlan device for this gid entry.
585 */
586 rcu_read_lock();
587 netdev_walk_all_lower_dev_rcu(attr->ndev,
588 get_lower_dev_vlan, &vlan_info);
589 rcu_read_unlock();
Ilan Tayari095b0922017-05-14 16:04:30 +0300590 }
Achiad Shochat3cca2602015-12-23 18:47:23 +0200591 }
592
Ilan Tayari095b0922017-05-14 16:04:30 +0300593 switch (gid_type) {
Achiad Shochat3cca2602015-12-23 18:47:23 +0200594 case IB_GID_TYPE_IB:
Ilan Tayari095b0922017-05-14 16:04:30 +0300595 roce_version = MLX5_ROCE_VERSION_1;
Achiad Shochat3cca2602015-12-23 18:47:23 +0200596 break;
597 case IB_GID_TYPE_ROCE_UDP_ENCAP:
Ilan Tayari095b0922017-05-14 16:04:30 +0300598 roce_version = MLX5_ROCE_VERSION_2;
599 if (ipv6_addr_v4mapped((void *)gid))
600 roce_l3_type = MLX5_ROCE_L3_TYPE_IPV4;
601 else
602 roce_l3_type = MLX5_ROCE_L3_TYPE_IPV6;
Achiad Shochat3cca2602015-12-23 18:47:23 +0200603 break;
604
605 default:
Ilan Tayari095b0922017-05-14 16:04:30 +0300606 mlx5_ib_warn(dev, "Unexpected GID type %u\n", gid_type);
Achiad Shochat3cca2602015-12-23 18:47:23 +0200607 }
608
Ilan Tayari095b0922017-05-14 16:04:30 +0300609 return mlx5_core_roce_gid_set(dev->mdev, index, roce_version,
Parav Panditcf34e1f2019-01-27 20:35:50 +0200610 roce_l3_type, gid->raw, mac,
611 vlan_info.vlan, vlan_info.vlan_id,
612 port_num);
Achiad Shochat3cca2602015-12-23 18:47:23 +0200613}
614
Parav Panditf4df9a72018-06-05 08:40:16 +0300615static int mlx5_ib_add_gid(const struct ib_gid_attr *attr,
Achiad Shochat3cca2602015-12-23 18:47:23 +0200616 __always_unused void **context)
617{
Parav Pandit414448d2018-04-01 15:08:24 +0300618 return set_roce_addr(to_mdev(attr->device), attr->port_num,
Parav Panditf4df9a72018-06-05 08:40:16 +0300619 attr->index, &attr->gid, attr);
Achiad Shochat3cca2602015-12-23 18:47:23 +0200620}
621
Parav Pandit414448d2018-04-01 15:08:24 +0300622static int mlx5_ib_del_gid(const struct ib_gid_attr *attr,
623 __always_unused void **context)
Achiad Shochat3cca2602015-12-23 18:47:23 +0200624{
Parav Pandit414448d2018-04-01 15:08:24 +0300625 return set_roce_addr(to_mdev(attr->device), attr->port_num,
626 attr->index, NULL, NULL);
Achiad Shochat3cca2602015-12-23 18:47:23 +0200627}
628
Parav Pandit47ec3862018-06-13 10:22:06 +0300629__be16 mlx5_get_roce_udp_sport(struct mlx5_ib_dev *dev,
630 const struct ib_gid_attr *attr)
Achiad Shochat2811ba52015-12-23 18:47:24 +0200631{
Parav Pandit47ec3862018-06-13 10:22:06 +0300632 if (attr->gid_type != IB_GID_TYPE_ROCE_UDP_ENCAP)
Achiad Shochat2811ba52015-12-23 18:47:24 +0200633 return 0;
634
635 return cpu_to_be16(MLX5_CAP_ROCE(dev->mdev, r_roce_min_src_udp_port));
636}
637
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300638static int mlx5_use_mad_ifc(struct mlx5_ib_dev *dev)
639{
Noa Osherovich7fae6652016-09-12 19:16:23 +0300640 if (MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_IB)
641 return !MLX5_CAP_GEN(dev->mdev, ib_virt);
642 return 0;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300643}
644
645enum {
646 MLX5_VPORT_ACCESS_METHOD_MAD,
647 MLX5_VPORT_ACCESS_METHOD_HCA,
648 MLX5_VPORT_ACCESS_METHOD_NIC,
649};
650
651static int mlx5_get_vport_access_method(struct ib_device *ibdev)
652{
653 if (mlx5_use_mad_ifc(to_mdev(ibdev)))
654 return MLX5_VPORT_ACCESS_METHOD_MAD;
655
Achiad Shochatebd61f62015-12-23 18:47:16 +0200656 if (mlx5_ib_port_link_layer(ibdev, 1) ==
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300657 IB_LINK_LAYER_ETHERNET)
658 return MLX5_VPORT_ACCESS_METHOD_NIC;
659
660 return MLX5_VPORT_ACCESS_METHOD_HCA;
661}
662
Eran Ben Elishada7525d2015-12-14 16:34:10 +0200663static void get_atomic_caps(struct mlx5_ib_dev *dev,
Moni Shoua776a3902018-01-02 16:19:33 +0200664 u8 atomic_size_qp,
Eran Ben Elishada7525d2015-12-14 16:34:10 +0200665 struct ib_device_attr *props)
666{
667 u8 tmp;
668 u8 atomic_operations = MLX5_CAP_ATOMIC(dev->mdev, atomic_operations);
Eran Ben Elishada7525d2015-12-14 16:34:10 +0200669 u8 atomic_req_8B_endianness_mode =
Or Gerlitzbd108382017-05-28 15:24:17 +0300670 MLX5_CAP_ATOMIC(dev->mdev, atomic_req_8B_endianness_mode);
Eran Ben Elishada7525d2015-12-14 16:34:10 +0200671
672 /* Check if HW supports 8 bytes standard atomic operations and capable
673 * of host endianness respond
674 */
675 tmp = MLX5_ATOMIC_OPS_CMP_SWAP | MLX5_ATOMIC_OPS_FETCH_ADD;
676 if (((atomic_operations & tmp) == tmp) &&
677 (atomic_size_qp & MLX5_ATOMIC_SIZE_QP_8BYTES) &&
678 (atomic_req_8B_endianness_mode)) {
679 props->atomic_cap = IB_ATOMIC_HCA;
680 } else {
681 props->atomic_cap = IB_ATOMIC_NONE;
682 }
683}
684
Moni Shoua776a3902018-01-02 16:19:33 +0200685static void get_atomic_caps_qp(struct mlx5_ib_dev *dev,
686 struct ib_device_attr *props)
687{
688 u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_qp);
689
690 get_atomic_caps(dev, atomic_size_qp, props);
691}
692
693static void get_atomic_caps_dc(struct mlx5_ib_dev *dev,
694 struct ib_device_attr *props)
695{
696 u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_dc);
697
698 get_atomic_caps(dev, atomic_size_qp, props);
699}
700
701bool mlx5_ib_dc_atomic_is_supported(struct mlx5_ib_dev *dev)
702{
703 struct ib_device_attr props = {};
704
705 get_atomic_caps_dc(dev, &props);
706 return (props.atomic_cap == IB_ATOMIC_HCA) ? true : false;
707}
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300708static int mlx5_query_system_image_guid(struct ib_device *ibdev,
709 __be64 *sys_image_guid)
710{
711 struct mlx5_ib_dev *dev = to_mdev(ibdev);
712 struct mlx5_core_dev *mdev = dev->mdev;
713 u64 tmp;
714 int err;
715
716 switch (mlx5_get_vport_access_method(ibdev)) {
717 case MLX5_VPORT_ACCESS_METHOD_MAD:
718 return mlx5_query_mad_ifc_system_image_guid(ibdev,
719 sys_image_guid);
720
721 case MLX5_VPORT_ACCESS_METHOD_HCA:
722 err = mlx5_query_hca_vport_system_image_guid(mdev, &tmp);
Achiad Shochat3f89a642015-12-23 18:47:21 +0200723 break;
724
725 case MLX5_VPORT_ACCESS_METHOD_NIC:
726 err = mlx5_query_nic_vport_system_image_guid(mdev, &tmp);
727 break;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300728
729 default:
730 return -EINVAL;
731 }
Achiad Shochat3f89a642015-12-23 18:47:21 +0200732
733 if (!err)
734 *sys_image_guid = cpu_to_be64(tmp);
735
736 return err;
737
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300738}
739
740static int mlx5_query_max_pkeys(struct ib_device *ibdev,
741 u16 *max_pkeys)
742{
743 struct mlx5_ib_dev *dev = to_mdev(ibdev);
744 struct mlx5_core_dev *mdev = dev->mdev;
745
746 switch (mlx5_get_vport_access_method(ibdev)) {
747 case MLX5_VPORT_ACCESS_METHOD_MAD:
748 return mlx5_query_mad_ifc_max_pkeys(ibdev, max_pkeys);
749
750 case MLX5_VPORT_ACCESS_METHOD_HCA:
751 case MLX5_VPORT_ACCESS_METHOD_NIC:
752 *max_pkeys = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev,
753 pkey_table_size));
754 return 0;
755
756 default:
757 return -EINVAL;
758 }
759}
760
761static int mlx5_query_vendor_id(struct ib_device *ibdev,
762 u32 *vendor_id)
763{
764 struct mlx5_ib_dev *dev = to_mdev(ibdev);
765
766 switch (mlx5_get_vport_access_method(ibdev)) {
767 case MLX5_VPORT_ACCESS_METHOD_MAD:
768 return mlx5_query_mad_ifc_vendor_id(ibdev, vendor_id);
769
770 case MLX5_VPORT_ACCESS_METHOD_HCA:
771 case MLX5_VPORT_ACCESS_METHOD_NIC:
772 return mlx5_core_query_vendor_id(dev->mdev, vendor_id);
773
774 default:
775 return -EINVAL;
776 }
777}
778
779static int mlx5_query_node_guid(struct mlx5_ib_dev *dev,
780 __be64 *node_guid)
781{
782 u64 tmp;
783 int err;
784
785 switch (mlx5_get_vport_access_method(&dev->ib_dev)) {
786 case MLX5_VPORT_ACCESS_METHOD_MAD:
787 return mlx5_query_mad_ifc_node_guid(dev, node_guid);
788
789 case MLX5_VPORT_ACCESS_METHOD_HCA:
790 err = mlx5_query_hca_vport_node_guid(dev->mdev, &tmp);
Achiad Shochat3f89a642015-12-23 18:47:21 +0200791 break;
792
793 case MLX5_VPORT_ACCESS_METHOD_NIC:
794 err = mlx5_query_nic_vport_node_guid(dev->mdev, &tmp);
795 break;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300796
797 default:
798 return -EINVAL;
799 }
Achiad Shochat3f89a642015-12-23 18:47:21 +0200800
801 if (!err)
802 *node_guid = cpu_to_be64(tmp);
803
804 return err;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300805}
806
807struct mlx5_reg_node_desc {
Yuval Shaiabd99fde2016-08-25 10:57:07 -0700808 u8 desc[IB_DEVICE_NODE_DESC_MAX];
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300809};
810
811static int mlx5_query_node_desc(struct mlx5_ib_dev *dev, char *node_desc)
812{
813 struct mlx5_reg_node_desc in;
814
815 if (mlx5_use_mad_ifc(dev))
816 return mlx5_query_mad_ifc_node_desc(dev, node_desc);
817
818 memset(&in, 0, sizeof(in));
819
820 return mlx5_core_access_reg(dev->mdev, &in, sizeof(in), node_desc,
821 sizeof(struct mlx5_reg_node_desc),
822 MLX5_REG_NODE_DESC, 0, 0);
823}
824
Eli Cohene126ba92013-07-07 17:25:49 +0300825static int mlx5_ib_query_device(struct ib_device *ibdev,
Matan Barak2528e332015-06-11 16:35:25 +0300826 struct ib_device_attr *props,
827 struct ib_udata *uhw)
Eli Cohene126ba92013-07-07 17:25:49 +0300828{
829 struct mlx5_ib_dev *dev = to_mdev(ibdev);
Saeed Mahameed938fe832015-05-28 22:28:41 +0300830 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +0300831 int err = -ENOMEM;
Eli Cohen288c01b2016-10-27 16:36:45 +0300832 int max_sq_desc;
Eli Cohene126ba92013-07-07 17:25:49 +0300833 int max_rq_sg;
834 int max_sq_sg;
Sagi Grimberge0238a62015-07-21 14:40:12 +0300835 u64 min_page_size = 1ull << MLX5_CAP_GEN(mdev, log_pg_sz);
Daniel Jurgens85c7c012018-01-04 17:25:43 +0200836 bool raw_support = !mlx5_core_mp_enabled(mdev);
Bodong Wang402ca532016-06-17 15:02:20 +0300837 struct mlx5_ib_query_device_resp resp = {};
838 size_t resp_len;
839 u64 max_tso;
Eli Cohene126ba92013-07-07 17:25:49 +0300840
Bodong Wang402ca532016-06-17 15:02:20 +0300841 resp_len = sizeof(resp.comp_mask) + sizeof(resp.response_length);
842 if (uhw->outlen && uhw->outlen < resp_len)
843 return -EINVAL;
844 else
845 resp.response_length = resp_len;
846
847 if (uhw->inlen && !ib_is_udata_cleared(uhw, 0, uhw->inlen))
Matan Barak2528e332015-06-11 16:35:25 +0300848 return -EINVAL;
849
Eli Cohene126ba92013-07-07 17:25:49 +0300850 memset(props, 0, sizeof(*props));
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300851 err = mlx5_query_system_image_guid(ibdev,
852 &props->sys_image_guid);
853 if (err)
854 return err;
855
856 err = mlx5_query_max_pkeys(ibdev, &props->max_pkeys);
857 if (err)
858 return err;
859
860 err = mlx5_query_vendor_id(ibdev, &props->vendor_id);
861 if (err)
862 return err;
Eli Cohene126ba92013-07-07 17:25:49 +0300863
Jack Morgenstein9603b612014-07-28 23:30:22 +0300864 props->fw_ver = ((u64)fw_rev_maj(dev->mdev) << 32) |
865 (fw_rev_min(dev->mdev) << 16) |
866 fw_rev_sub(dev->mdev);
Eli Cohene126ba92013-07-07 17:25:49 +0300867 props->device_cap_flags = IB_DEVICE_CHANGE_PHY_PORT |
868 IB_DEVICE_PORT_ACTIVE_EVENT |
869 IB_DEVICE_SYS_IMAGE_GUID |
Eli Cohen1a4c3a32014-02-06 17:41:25 +0200870 IB_DEVICE_RC_RNR_NAK_GEN;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300871
872 if (MLX5_CAP_GEN(mdev, pkv))
Eli Cohene126ba92013-07-07 17:25:49 +0300873 props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300874 if (MLX5_CAP_GEN(mdev, qkv))
Eli Cohene126ba92013-07-07 17:25:49 +0300875 props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300876 if (MLX5_CAP_GEN(mdev, apm))
Eli Cohene126ba92013-07-07 17:25:49 +0300877 props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300878 if (MLX5_CAP_GEN(mdev, xrc))
Eli Cohene126ba92013-07-07 17:25:49 +0300879 props->device_cap_flags |= IB_DEVICE_XRC;
Matan Barakd2370e02016-02-29 18:05:30 +0200880 if (MLX5_CAP_GEN(mdev, imaicl)) {
881 props->device_cap_flags |= IB_DEVICE_MEM_WINDOW |
882 IB_DEVICE_MEM_WINDOW_TYPE_2B;
883 props->max_mw = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
Sagi Grimbergb005d312016-02-29 19:07:33 +0200884 /* We support 'Gappy' memory registration too */
885 props->device_cap_flags |= IB_DEVICE_SG_GAPS_REG;
Matan Barakd2370e02016-02-29 18:05:30 +0200886 }
Eli Cohene126ba92013-07-07 17:25:49 +0300887 props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300888 if (MLX5_CAP_GEN(mdev, sho)) {
Sagi Grimberg2dea9092014-02-23 14:19:13 +0200889 props->device_cap_flags |= IB_DEVICE_SIGNATURE_HANDOVER;
890 /* At this stage no support for signature handover */
891 props->sig_prot_cap = IB_PROT_T10DIF_TYPE_1 |
892 IB_PROT_T10DIF_TYPE_2 |
893 IB_PROT_T10DIF_TYPE_3;
894 props->sig_guard_cap = IB_GUARD_T10DIF_CRC |
895 IB_GUARD_T10DIF_CSUM;
896 }
Saeed Mahameed938fe832015-05-28 22:28:41 +0300897 if (MLX5_CAP_GEN(mdev, block_lb_mc))
Eli Cohenf360d882014-04-02 00:10:16 +0300898 props->device_cap_flags |= IB_DEVICE_BLOCK_MULTICAST_LOOPBACK;
Eli Cohene126ba92013-07-07 17:25:49 +0300899
Daniel Jurgens85c7c012018-01-04 17:25:43 +0200900 if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) && raw_support) {
Noa Osheroviche8161332017-01-18 15:40:01 +0200901 if (MLX5_CAP_ETH(mdev, csum_cap)) {
902 /* Legacy bit to support old userspace libraries */
Bodong Wang88115fe2015-12-18 13:53:20 +0200903 props->device_cap_flags |= IB_DEVICE_RAW_IP_CSUM;
Noa Osheroviche8161332017-01-18 15:40:01 +0200904 props->raw_packet_caps |= IB_RAW_PACKET_CAP_IP_CSUM;
905 }
906
907 if (MLX5_CAP_ETH(dev->mdev, vlan_cap))
908 props->raw_packet_caps |=
909 IB_RAW_PACKET_CAP_CVLAN_STRIPPING;
Bodong Wang88115fe2015-12-18 13:53:20 +0200910
Bodong Wang402ca532016-06-17 15:02:20 +0300911 if (field_avail(typeof(resp), tso_caps, uhw->outlen)) {
912 max_tso = MLX5_CAP_ETH(mdev, max_lso_cap);
913 if (max_tso) {
914 resp.tso_caps.max_tso = 1 << max_tso;
915 resp.tso_caps.supported_qpts |=
916 1 << IB_QPT_RAW_PACKET;
917 resp.response_length += sizeof(resp.tso_caps);
918 }
919 }
Yishai Hadas31f69a82016-08-28 11:28:45 +0300920
921 if (field_avail(typeof(resp), rss_caps, uhw->outlen)) {
922 resp.rss_caps.rx_hash_function =
923 MLX5_RX_HASH_FUNC_TOEPLITZ;
924 resp.rss_caps.rx_hash_fields_mask =
925 MLX5_RX_HASH_SRC_IPV4 |
926 MLX5_RX_HASH_DST_IPV4 |
927 MLX5_RX_HASH_SRC_IPV6 |
928 MLX5_RX_HASH_DST_IPV6 |
929 MLX5_RX_HASH_SRC_PORT_TCP |
930 MLX5_RX_HASH_DST_PORT_TCP |
931 MLX5_RX_HASH_SRC_PORT_UDP |
Maor Gottlieb4e2b53a2017-12-24 14:51:25 +0200932 MLX5_RX_HASH_DST_PORT_UDP |
933 MLX5_RX_HASH_INNER;
Matan Barak2d93fc82018-03-28 09:27:55 +0300934 if (mlx5_accel_ipsec_device_caps(dev->mdev) &
935 MLX5_ACCEL_IPSEC_CAP_DEVICE)
936 resp.rss_caps.rx_hash_fields_mask |=
937 MLX5_RX_HASH_IPSEC_SPI;
Yishai Hadas31f69a82016-08-28 11:28:45 +0300938 resp.response_length += sizeof(resp.rss_caps);
939 }
940 } else {
941 if (field_avail(typeof(resp), tso_caps, uhw->outlen))
942 resp.response_length += sizeof(resp.tso_caps);
943 if (field_avail(typeof(resp), rss_caps, uhw->outlen))
944 resp.response_length += sizeof(resp.rss_caps);
Bodong Wang402ca532016-06-17 15:02:20 +0300945 }
946
Erez Shitritf0313962016-02-21 16:27:17 +0200947 if (MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) {
948 props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
949 props->device_cap_flags |= IB_DEVICE_UD_TSO;
950 }
951
Maor Gottlieb03404e82017-05-30 10:29:13 +0300952 if (MLX5_CAP_GEN(dev->mdev, rq_delay_drop) &&
Daniel Jurgens85c7c012018-01-04 17:25:43 +0200953 MLX5_CAP_GEN(dev->mdev, general_notification_event) &&
954 raw_support)
Maor Gottlieb03404e82017-05-30 10:29:13 +0300955 props->raw_packet_caps |= IB_RAW_PACKET_CAP_DELAY_DROP;
956
Yishai Hadas1d54f892017-06-08 16:15:11 +0300957 if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads) &&
958 MLX5_CAP_IPOIB_ENHANCED(mdev, csum_cap))
959 props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
960
Majd Dibbinycff5a0f2016-04-17 17:19:38 +0300961 if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
Daniel Jurgens85c7c012018-01-04 17:25:43 +0200962 MLX5_CAP_ETH(dev->mdev, scatter_fcs) &&
963 raw_support) {
Noa Osheroviche8161332017-01-18 15:40:01 +0200964 /* Legacy bit to support old userspace libraries */
Majd Dibbinycff5a0f2016-04-17 17:19:38 +0300965 props->device_cap_flags |= IB_DEVICE_RAW_SCATTER_FCS;
Noa Osheroviche8161332017-01-18 15:40:01 +0200966 props->raw_packet_caps |= IB_RAW_PACKET_CAP_SCATTER_FCS;
967 }
Majd Dibbinycff5a0f2016-04-17 17:19:38 +0300968
Ariel Levkovich24da0012018-04-05 18:53:27 +0300969 if (MLX5_CAP_DEV_MEM(mdev, memic)) {
970 props->max_dm_size =
971 MLX5_CAP_DEV_MEM(mdev, max_memic_size);
972 }
973
Maor Gottliebda6d6ba32016-06-04 15:15:28 +0300974 if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS))
975 props->device_cap_flags |= IB_DEVICE_MANAGED_FLOW_STEERING;
976
Noa Osherovichb1383aa2017-10-29 13:59:45 +0200977 if (MLX5_CAP_GEN(mdev, end_pad))
978 props->device_cap_flags |= IB_DEVICE_PCI_WRITE_END_PADDING;
979
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300980 props->vendor_part_id = mdev->pdev->device;
981 props->hw_ver = mdev->pdev->revision;
Eli Cohene126ba92013-07-07 17:25:49 +0300982
983 props->max_mr_size = ~0ull;
Sagi Grimberge0238a62015-07-21 14:40:12 +0300984 props->page_size_cap = ~(min_page_size - 1);
Saeed Mahameed938fe832015-05-28 22:28:41 +0300985 props->max_qp = 1 << MLX5_CAP_GEN(mdev, log_max_qp);
986 props->max_qp_wr = 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
987 max_rq_sg = MLX5_CAP_GEN(mdev, max_wqe_sz_rq) /
988 sizeof(struct mlx5_wqe_data_seg);
Eli Cohen288c01b2016-10-27 16:36:45 +0300989 max_sq_desc = min_t(int, MLX5_CAP_GEN(mdev, max_wqe_sz_sq), 512);
990 max_sq_sg = (max_sq_desc - sizeof(struct mlx5_wqe_ctrl_seg) -
991 sizeof(struct mlx5_wqe_raddr_seg)) /
992 sizeof(struct mlx5_wqe_data_seg);
Steve Wise33023fb2018-06-18 08:05:26 -0700993 props->max_send_sge = max_sq_sg;
994 props->max_recv_sge = max_rq_sg;
Sagi Grimberg986ef952016-03-31 19:03:25 +0300995 props->max_sge_rd = MLX5_MAX_SGE_RD;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300996 props->max_cq = 1 << MLX5_CAP_GEN(mdev, log_max_cq);
Leon Romanovsky9f177682016-01-14 08:11:40 +0200997 props->max_cqe = (1 << MLX5_CAP_GEN(mdev, log_max_cq_sz)) - 1;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300998 props->max_mr = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
999 props->max_pd = 1 << MLX5_CAP_GEN(mdev, log_max_pd);
1000 props->max_qp_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_req_qp);
1001 props->max_qp_init_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_res_qp);
1002 props->max_srq = 1 << MLX5_CAP_GEN(mdev, log_max_srq);
1003 props->max_srq_wr = (1 << MLX5_CAP_GEN(mdev, log_max_srq_sz)) - 1;
1004 props->local_ca_ack_delay = MLX5_CAP_GEN(mdev, local_ca_ack_delay);
Eli Cohene126ba92013-07-07 17:25:49 +03001005 props->max_res_rd_atom = props->max_qp_rd_atom * props->max_qp;
Eli Cohene126ba92013-07-07 17:25:49 +03001006 props->max_srq_sge = max_rq_sg - 1;
Sagi Grimberg911f4332016-03-03 13:37:51 +02001007 props->max_fast_reg_page_list_len =
1008 1 << MLX5_CAP_GEN(mdev, log_max_klm_list_size);
Moni Shoua776a3902018-01-02 16:19:33 +02001009 get_atomic_caps_qp(dev, props);
Eli Cohen81bea282013-09-11 16:35:30 +03001010 props->masked_atomic_cap = IB_ATOMIC_NONE;
Saeed Mahameed938fe832015-05-28 22:28:41 +03001011 props->max_mcast_grp = 1 << MLX5_CAP_GEN(mdev, log_max_mcg);
1012 props->max_mcast_qp_attach = MLX5_CAP_GEN(mdev, max_qp_mcg);
Eli Cohene126ba92013-07-07 17:25:49 +03001013 props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
1014 props->max_mcast_grp;
1015 props->max_map_per_fmr = INT_MAX; /* no limit in ConnectIB */
Maor Gottlieb86695a62016-10-27 16:36:38 +03001016 props->max_ah = INT_MAX;
Matan Barak7c60bcb2015-12-15 20:30:11 +02001017 props->hca_core_clock = MLX5_CAP_GEN(mdev, device_frequency_khz);
1018 props->timestamp_mask = 0x7FFFFFFFFFFFFFFFULL;
Eli Cohene126ba92013-07-07 17:25:49 +03001019
Leon Romanovskye502b8b2019-01-08 16:07:24 +02001020 if (IS_ENABLED(CONFIG_INFINIBAND_ON_DEMAND_PAGING)) {
1021 if (MLX5_CAP_GEN(mdev, pg))
1022 props->device_cap_flags |= IB_DEVICE_ON_DEMAND_PAGING;
1023 props->odp_caps = dev->odp_caps;
1024 }
Haggai Eran8cdd3122014-12-11 17:04:20 +02001025
Leon Romanovsky051f2632015-12-20 12:16:11 +02001026 if (MLX5_CAP_GEN(mdev, cd))
1027 props->device_cap_flags |= IB_DEVICE_CROSS_CHANNEL;
1028
Eli Coheneff901d2016-03-11 22:58:42 +02001029 if (!mlx5_core_is_pf(mdev))
1030 props->device_cap_flags |= IB_DEVICE_VIRTUAL_FUNCTION;
1031
Yishai Hadas31f69a82016-08-28 11:28:45 +03001032 if (mlx5_ib_port_link_layer(ibdev, 1) ==
Daniel Jurgens85c7c012018-01-04 17:25:43 +02001033 IB_LINK_LAYER_ETHERNET && raw_support) {
Yishai Hadas31f69a82016-08-28 11:28:45 +03001034 props->rss_caps.max_rwq_indirection_tables =
1035 1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt);
1036 props->rss_caps.max_rwq_indirection_table_size =
1037 1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt_size);
1038 props->rss_caps.supported_qpts = 1 << IB_QPT_RAW_PACKET;
1039 props->max_wq_type_rq =
1040 1 << MLX5_CAP_GEN(dev->mdev, log_max_rq);
1041 }
1042
Artemy Kovalyoveb761892017-08-17 15:52:09 +03001043 if (MLX5_CAP_GEN(mdev, tag_matching)) {
Leon Romanovsky78b1beb2017-09-24 21:46:29 +03001044 props->tm_caps.max_rndv_hdr_size = MLX5_TM_MAX_RNDV_MSG_SIZE;
1045 props->tm_caps.max_num_tags =
Artemy Kovalyoveb761892017-08-17 15:52:09 +03001046 (1 << MLX5_CAP_GEN(mdev, log_tag_matching_list_sz)) - 1;
Leon Romanovsky78b1beb2017-09-24 21:46:29 +03001047 props->tm_caps.flags = IB_TM_CAP_RC;
1048 props->tm_caps.max_ops =
Artemy Kovalyoveb761892017-08-17 15:52:09 +03001049 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
Leon Romanovsky78b1beb2017-09-24 21:46:29 +03001050 props->tm_caps.max_sge = MLX5_TM_MAX_SGE;
Artemy Kovalyoveb761892017-08-17 15:52:09 +03001051 }
1052
Yonatan Cohen87ab3f52017-11-13 10:51:18 +02001053 if (MLX5_CAP_GEN(dev->mdev, cq_moderation)) {
1054 props->cq_caps.max_cq_moderation_count =
1055 MLX5_MAX_CQ_COUNT;
1056 props->cq_caps.max_cq_moderation_period =
1057 MLX5_MAX_CQ_PERIOD;
1058 }
1059
Bodong Wang7e43a2a2016-10-31 12:16:44 +02001060 if (field_avail(typeof(resp), cqe_comp_caps, uhw->outlen)) {
Bodong Wang7e43a2a2016-10-31 12:16:44 +02001061 resp.response_length += sizeof(resp.cqe_comp_caps);
Yonatan Cohen572f46b2018-05-27 13:42:33 +03001062
1063 if (MLX5_CAP_GEN(dev->mdev, cqe_compression)) {
1064 resp.cqe_comp_caps.max_num =
1065 MLX5_CAP_GEN(dev->mdev,
1066 cqe_compression_max_num);
1067
1068 resp.cqe_comp_caps.supported_format =
1069 MLX5_IB_CQE_RES_FORMAT_HASH |
1070 MLX5_IB_CQE_RES_FORMAT_CSUM;
Yonatan Cohen6f1006a2018-05-27 13:42:34 +03001071
1072 if (MLX5_CAP_GEN(dev->mdev, mini_cqe_resp_stride_index))
1073 resp.cqe_comp_caps.supported_format |=
1074 MLX5_IB_CQE_RES_FORMAT_CSUM_STRIDX;
Yonatan Cohen572f46b2018-05-27 13:42:33 +03001075 }
Bodong Wang7e43a2a2016-10-31 12:16:44 +02001076 }
1077
Daniel Jurgens85c7c012018-01-04 17:25:43 +02001078 if (field_avail(typeof(resp), packet_pacing_caps, uhw->outlen) &&
1079 raw_support) {
Bodong Wangd9491672016-12-01 13:43:13 +02001080 if (MLX5_CAP_QOS(mdev, packet_pacing) &&
1081 MLX5_CAP_GEN(mdev, qos)) {
1082 resp.packet_pacing_caps.qp_rate_limit_max =
1083 MLX5_CAP_QOS(mdev, packet_pacing_max_rate);
1084 resp.packet_pacing_caps.qp_rate_limit_min =
1085 MLX5_CAP_QOS(mdev, packet_pacing_min_rate);
1086 resp.packet_pacing_caps.supported_qpts |=
1087 1 << IB_QPT_RAW_PACKET;
Bodong Wang61147f32018-03-19 15:10:30 +02001088 if (MLX5_CAP_QOS(mdev, packet_pacing_burst_bound) &&
1089 MLX5_CAP_QOS(mdev, packet_pacing_typical_size))
1090 resp.packet_pacing_caps.cap_flags |=
1091 MLX5_IB_PP_SUPPORT_BURST;
Bodong Wangd9491672016-12-01 13:43:13 +02001092 }
1093 resp.response_length += sizeof(resp.packet_pacing_caps);
1094 }
1095
Leon Romanovsky9f885202017-01-02 11:37:39 +02001096 if (field_avail(typeof(resp), mlx5_ib_support_multi_pkt_send_wqes,
1097 uhw->outlen)) {
Bodong Wang795b6092017-08-17 15:52:34 +03001098 if (MLX5_CAP_ETH(mdev, multi_pkt_send_wqe))
1099 resp.mlx5_ib_support_multi_pkt_send_wqes =
1100 MLX5_IB_ALLOW_MPW;
Bodong Wang050da902017-08-17 15:52:35 +03001101
1102 if (MLX5_CAP_ETH(mdev, enhanced_multi_pkt_send_wqe))
1103 resp.mlx5_ib_support_multi_pkt_send_wqes |=
1104 MLX5_IB_SUPPORT_EMPW;
1105
Leon Romanovsky9f885202017-01-02 11:37:39 +02001106 resp.response_length +=
1107 sizeof(resp.mlx5_ib_support_multi_pkt_send_wqes);
1108 }
1109
Guy Levide57f2a2017-10-19 08:25:52 +03001110 if (field_avail(typeof(resp), flags, uhw->outlen)) {
1111 resp.response_length += sizeof(resp.flags);
Guy Levi7a0c8f42017-10-19 08:25:53 +03001112
Guy Levide57f2a2017-10-19 08:25:52 +03001113 if (MLX5_CAP_GEN(mdev, cqe_compression_128))
1114 resp.flags |=
1115 MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_COMP;
Guy Levi7a0c8f42017-10-19 08:25:53 +03001116
1117 if (MLX5_CAP_GEN(mdev, cqe_128_always))
1118 resp.flags |= MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_PAD;
Danit Goldberg7e11b912018-11-30 13:22:06 +02001119 if (MLX5_CAP_GEN(mdev, qp_packet_based))
1120 resp.flags |=
1121 MLX5_IB_QUERY_DEV_RESP_PACKET_BASED_CREDIT_MODE;
Guy Levi7249c8e2019-04-10 10:59:45 +03001122
1123 resp.flags |= MLX5_IB_QUERY_DEV_RESP_FLAGS_SCAT2CQE_DCT;
Guy Levide57f2a2017-10-19 08:25:52 +03001124 }
Leon Romanovsky9f885202017-01-02 11:37:39 +02001125
Noa Osherovich96dc3fc2017-08-17 15:52:28 +03001126 if (field_avail(typeof(resp), sw_parsing_caps,
1127 uhw->outlen)) {
1128 resp.response_length += sizeof(resp.sw_parsing_caps);
1129 if (MLX5_CAP_ETH(mdev, swp)) {
1130 resp.sw_parsing_caps.sw_parsing_offloads |=
1131 MLX5_IB_SW_PARSING;
1132
1133 if (MLX5_CAP_ETH(mdev, swp_csum))
1134 resp.sw_parsing_caps.sw_parsing_offloads |=
1135 MLX5_IB_SW_PARSING_CSUM;
1136
1137 if (MLX5_CAP_ETH(mdev, swp_lso))
1138 resp.sw_parsing_caps.sw_parsing_offloads |=
1139 MLX5_IB_SW_PARSING_LSO;
1140
1141 if (resp.sw_parsing_caps.sw_parsing_offloads)
1142 resp.sw_parsing_caps.supported_qpts =
1143 BIT(IB_QPT_RAW_PACKET);
1144 }
1145 }
1146
Daniel Jurgens85c7c012018-01-04 17:25:43 +02001147 if (field_avail(typeof(resp), striding_rq_caps, uhw->outlen) &&
1148 raw_support) {
Noa Osherovichb4f34592017-10-17 18:01:12 +03001149 resp.response_length += sizeof(resp.striding_rq_caps);
1150 if (MLX5_CAP_GEN(mdev, striding_rq)) {
1151 resp.striding_rq_caps.min_single_stride_log_num_of_bytes =
1152 MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES;
1153 resp.striding_rq_caps.max_single_stride_log_num_of_bytes =
1154 MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES;
1155 resp.striding_rq_caps.min_single_wqe_log_num_of_strides =
1156 MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES;
1157 resp.striding_rq_caps.max_single_wqe_log_num_of_strides =
1158 MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES;
1159 resp.striding_rq_caps.supported_qpts =
1160 BIT(IB_QPT_RAW_PACKET);
1161 }
1162 }
1163
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001164 if (field_avail(typeof(resp), tunnel_offloads_caps,
1165 uhw->outlen)) {
1166 resp.response_length += sizeof(resp.tunnel_offloads_caps);
1167 if (MLX5_CAP_ETH(mdev, tunnel_stateless_vxlan))
1168 resp.tunnel_offloads_caps |=
1169 MLX5_IB_TUNNELED_OFFLOADS_VXLAN;
1170 if (MLX5_CAP_ETH(mdev, tunnel_stateless_geneve_rx))
1171 resp.tunnel_offloads_caps |=
1172 MLX5_IB_TUNNELED_OFFLOADS_GENEVE;
1173 if (MLX5_CAP_ETH(mdev, tunnel_stateless_gre))
1174 resp.tunnel_offloads_caps |=
1175 MLX5_IB_TUNNELED_OFFLOADS_GRE;
Ariel Levkoviche818e252018-05-13 14:33:35 +03001176 if (MLX5_CAP_GEN(mdev, flex_parser_protocols) &
1177 MLX5_FLEX_PROTO_CW_MPLS_GRE)
1178 resp.tunnel_offloads_caps |=
1179 MLX5_IB_TUNNELED_OFFLOADS_MPLS_GRE;
1180 if (MLX5_CAP_GEN(mdev, flex_parser_protocols) &
1181 MLX5_FLEX_PROTO_CW_MPLS_UDP)
1182 resp.tunnel_offloads_caps |=
1183 MLX5_IB_TUNNELED_OFFLOADS_MPLS_UDP;
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001184 }
1185
Bodong Wang402ca532016-06-17 15:02:20 +03001186 if (uhw->outlen) {
1187 err = ib_copy_to_udata(uhw, &resp, resp.response_length);
1188
1189 if (err)
1190 return err;
1191 }
1192
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001193 return 0;
1194}
Eli Cohene126ba92013-07-07 17:25:49 +03001195
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001196enum mlx5_ib_width {
1197 MLX5_IB_WIDTH_1X = 1 << 0,
1198 MLX5_IB_WIDTH_2X = 1 << 1,
1199 MLX5_IB_WIDTH_4X = 1 << 2,
1200 MLX5_IB_WIDTH_8X = 1 << 3,
1201 MLX5_IB_WIDTH_12X = 1 << 4
1202};
1203
Michael Guralnikdb7a6912018-11-21 15:03:54 +02001204static void translate_active_width(struct ib_device *ibdev, u8 active_width,
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001205 u8 *ib_width)
1206{
1207 struct mlx5_ib_dev *dev = to_mdev(ibdev);
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001208
Michael Guralnikdb7a6912018-11-21 15:03:54 +02001209 if (active_width & MLX5_IB_WIDTH_1X)
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001210 *ib_width = IB_WIDTH_1X;
Michael Guralnikd7649702018-12-09 11:49:54 +02001211 else if (active_width & MLX5_IB_WIDTH_2X)
1212 *ib_width = IB_WIDTH_2X;
Michael Guralnikdb7a6912018-11-21 15:03:54 +02001213 else if (active_width & MLX5_IB_WIDTH_4X)
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001214 *ib_width = IB_WIDTH_4X;
Michael Guralnikdb7a6912018-11-21 15:03:54 +02001215 else if (active_width & MLX5_IB_WIDTH_8X)
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001216 *ib_width = IB_WIDTH_8X;
Michael Guralnikdb7a6912018-11-21 15:03:54 +02001217 else if (active_width & MLX5_IB_WIDTH_12X)
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001218 *ib_width = IB_WIDTH_12X;
Michael Guralnikdb7a6912018-11-21 15:03:54 +02001219 else {
1220 mlx5_ib_dbg(dev, "Invalid active_width %d, setting width to default value: 4x\n",
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001221 (int)active_width);
Michael Guralnikdb7a6912018-11-21 15:03:54 +02001222 *ib_width = IB_WIDTH_4X;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001223 }
1224
Michael Guralnikdb7a6912018-11-21 15:03:54 +02001225 return;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001226}
1227
1228static int mlx5_mtu_to_ib_mtu(int mtu)
1229{
1230 switch (mtu) {
1231 case 256: return 1;
1232 case 512: return 2;
1233 case 1024: return 3;
1234 case 2048: return 4;
1235 case 4096: return 5;
1236 default:
1237 pr_warn("invalid mtu\n");
1238 return -1;
1239 }
1240}
1241
1242enum ib_max_vl_num {
1243 __IB_MAX_VL_0 = 1,
1244 __IB_MAX_VL_0_1 = 2,
1245 __IB_MAX_VL_0_3 = 3,
1246 __IB_MAX_VL_0_7 = 4,
1247 __IB_MAX_VL_0_14 = 5,
1248};
1249
1250enum mlx5_vl_hw_cap {
1251 MLX5_VL_HW_0 = 1,
1252 MLX5_VL_HW_0_1 = 2,
1253 MLX5_VL_HW_0_2 = 3,
1254 MLX5_VL_HW_0_3 = 4,
1255 MLX5_VL_HW_0_4 = 5,
1256 MLX5_VL_HW_0_5 = 6,
1257 MLX5_VL_HW_0_6 = 7,
1258 MLX5_VL_HW_0_7 = 8,
1259 MLX5_VL_HW_0_14 = 15
1260};
1261
1262static int translate_max_vl_num(struct ib_device *ibdev, u8 vl_hw_cap,
1263 u8 *max_vl_num)
1264{
1265 switch (vl_hw_cap) {
1266 case MLX5_VL_HW_0:
1267 *max_vl_num = __IB_MAX_VL_0;
1268 break;
1269 case MLX5_VL_HW_0_1:
1270 *max_vl_num = __IB_MAX_VL_0_1;
1271 break;
1272 case MLX5_VL_HW_0_3:
1273 *max_vl_num = __IB_MAX_VL_0_3;
1274 break;
1275 case MLX5_VL_HW_0_7:
1276 *max_vl_num = __IB_MAX_VL_0_7;
1277 break;
1278 case MLX5_VL_HW_0_14:
1279 *max_vl_num = __IB_MAX_VL_0_14;
1280 break;
1281
1282 default:
1283 return -EINVAL;
1284 }
1285
1286 return 0;
1287}
1288
1289static int mlx5_query_hca_port(struct ib_device *ibdev, u8 port,
1290 struct ib_port_attr *props)
1291{
1292 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1293 struct mlx5_core_dev *mdev = dev->mdev;
1294 struct mlx5_hca_vport_context *rep;
Saeed Mahameed046339e2016-04-22 00:33:03 +03001295 u16 max_mtu;
1296 u16 oper_mtu;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001297 int err;
1298 u8 ib_link_width_oper;
1299 u8 vl_hw_cap;
1300
1301 rep = kzalloc(sizeof(*rep), GFP_KERNEL);
1302 if (!rep) {
1303 err = -ENOMEM;
1304 goto out;
1305 }
1306
Or Gerlitzc4550c62017-01-24 13:02:39 +02001307 /* props being zeroed by the caller, avoid zeroing it here */
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001308
1309 err = mlx5_query_hca_vport_context(mdev, 0, port, 0, rep);
1310 if (err)
1311 goto out;
1312
1313 props->lid = rep->lid;
1314 props->lmc = rep->lmc;
1315 props->sm_lid = rep->sm_lid;
1316 props->sm_sl = rep->sm_sl;
1317 props->state = rep->vport_state;
1318 props->phys_state = rep->port_physical_state;
1319 props->port_cap_flags = rep->cap_mask1;
1320 props->gid_tbl_len = mlx5_get_gid_table_len(MLX5_CAP_GEN(mdev, gid_table_size));
1321 props->max_msg_sz = 1 << MLX5_CAP_GEN(mdev, log_max_msg);
1322 props->pkey_tbl_len = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, pkey_table_size));
1323 props->bad_pkey_cntr = rep->pkey_violation_counter;
1324 props->qkey_viol_cntr = rep->qkey_violation_counter;
1325 props->subnet_timeout = rep->subnet_timeout;
1326 props->init_type_reply = rep->init_type_reply;
1327
Michael Guralnik4106a752018-12-09 11:49:51 +02001328 if (props->port_cap_flags & IB_PORT_CAP_MASK2_SUP)
1329 props->port_cap_flags2 = rep->cap_mask2;
1330
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001331 err = mlx5_query_port_link_width_oper(mdev, &ib_link_width_oper, port);
1332 if (err)
1333 goto out;
1334
Michael Guralnikdb7a6912018-11-21 15:03:54 +02001335 translate_active_width(ibdev, ib_link_width_oper, &props->active_width);
1336
Noa Osherovichd5beb7f2016-06-02 10:47:53 +03001337 err = mlx5_query_port_ib_proto_oper(mdev, &props->active_speed, port);
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001338 if (err)
1339 goto out;
1340
Saeed Mahameedfacc9692015-06-11 14:47:27 +03001341 mlx5_query_port_max_mtu(mdev, &max_mtu, port);
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001342
1343 props->max_mtu = mlx5_mtu_to_ib_mtu(max_mtu);
1344
Saeed Mahameedfacc9692015-06-11 14:47:27 +03001345 mlx5_query_port_oper_mtu(mdev, &oper_mtu, port);
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001346
1347 props->active_mtu = mlx5_mtu_to_ib_mtu(oper_mtu);
1348
1349 err = mlx5_query_port_vl_hw_cap(mdev, &vl_hw_cap, port);
1350 if (err)
1351 goto out;
1352
1353 err = translate_max_vl_num(ibdev, vl_hw_cap,
1354 &props->max_vl_num);
1355out:
1356 kfree(rep);
Eli Cohene126ba92013-07-07 17:25:49 +03001357 return err;
1358}
1359
1360int mlx5_ib_query_port(struct ib_device *ibdev, u8 port,
1361 struct ib_port_attr *props)
1362{
Ilan Tayari095b0922017-05-14 16:04:30 +03001363 unsigned int count;
1364 int ret;
1365
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001366 switch (mlx5_get_vport_access_method(ibdev)) {
1367 case MLX5_VPORT_ACCESS_METHOD_MAD:
Ilan Tayari095b0922017-05-14 16:04:30 +03001368 ret = mlx5_query_mad_ifc_port(ibdev, port, props);
1369 break;
Eli Cohene126ba92013-07-07 17:25:49 +03001370
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001371 case MLX5_VPORT_ACCESS_METHOD_HCA:
Ilan Tayari095b0922017-05-14 16:04:30 +03001372 ret = mlx5_query_hca_port(ibdev, port, props);
1373 break;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001374
Achiad Shochat3f89a642015-12-23 18:47:21 +02001375 case MLX5_VPORT_ACCESS_METHOD_NIC:
Ilan Tayari095b0922017-05-14 16:04:30 +03001376 ret = mlx5_query_port_roce(ibdev, port, props);
1377 break;
Achiad Shochat3f89a642015-12-23 18:47:21 +02001378
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001379 default:
Ilan Tayari095b0922017-05-14 16:04:30 +03001380 ret = -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +03001381 }
Ilan Tayari095b0922017-05-14 16:04:30 +03001382
1383 if (!ret && props) {
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001384 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1385 struct mlx5_core_dev *mdev;
1386 bool put_mdev = true;
1387
1388 mdev = mlx5_ib_get_native_port_mdev(dev, port, NULL);
1389 if (!mdev) {
1390 /* If the port isn't affiliated yet query the master.
1391 * The master and slave will have the same values.
1392 */
1393 mdev = dev->mdev;
1394 port = 1;
1395 put_mdev = false;
1396 }
1397 count = mlx5_core_reserved_gids_count(mdev);
1398 if (put_mdev)
1399 mlx5_ib_put_native_port_mdev(dev, port);
Ilan Tayari095b0922017-05-14 16:04:30 +03001400 props->gid_tbl_len -= count;
1401 }
1402 return ret;
Eli Cohene126ba92013-07-07 17:25:49 +03001403}
1404
Mark Bloch8e6efa32017-11-06 12:22:13 +00001405static int mlx5_ib_rep_query_port(struct ib_device *ibdev, u8 port,
1406 struct ib_port_attr *props)
1407{
1408 int ret;
1409
1410 /* Only link layer == ethernet is valid for representors */
1411 ret = mlx5_query_port_roce(ibdev, port, props);
1412 if (ret || !props)
1413 return ret;
1414
1415 /* We don't support GIDS */
1416 props->gid_tbl_len = 0;
1417
1418 return ret;
1419}
1420
Eli Cohene126ba92013-07-07 17:25:49 +03001421static int mlx5_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
1422 union ib_gid *gid)
1423{
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001424 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1425 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03001426
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001427 switch (mlx5_get_vport_access_method(ibdev)) {
1428 case MLX5_VPORT_ACCESS_METHOD_MAD:
1429 return mlx5_query_mad_ifc_gids(ibdev, port, index, gid);
Eli Cohene126ba92013-07-07 17:25:49 +03001430
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001431 case MLX5_VPORT_ACCESS_METHOD_HCA:
1432 return mlx5_query_hca_vport_gid(mdev, 0, port, 0, index, gid);
Eli Cohene126ba92013-07-07 17:25:49 +03001433
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001434 default:
1435 return -EINVAL;
1436 }
Eli Cohene126ba92013-07-07 17:25:49 +03001437
Eli Cohene126ba92013-07-07 17:25:49 +03001438}
1439
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001440static int mlx5_query_hca_nic_pkey(struct ib_device *ibdev, u8 port,
1441 u16 index, u16 *pkey)
1442{
1443 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1444 struct mlx5_core_dev *mdev;
1445 bool put_mdev = true;
1446 u8 mdev_port_num;
1447 int err;
1448
1449 mdev = mlx5_ib_get_native_port_mdev(dev, port, &mdev_port_num);
1450 if (!mdev) {
1451 /* The port isn't affiliated yet, get the PKey from the master
1452 * port. For RoCE the PKey tables will be the same.
1453 */
1454 put_mdev = false;
1455 mdev = dev->mdev;
1456 mdev_port_num = 1;
1457 }
1458
1459 err = mlx5_query_hca_vport_pkey(mdev, 0, mdev_port_num, 0,
1460 index, pkey);
1461 if (put_mdev)
1462 mlx5_ib_put_native_port_mdev(dev, port);
1463
1464 return err;
1465}
1466
Eli Cohene126ba92013-07-07 17:25:49 +03001467static int mlx5_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
1468 u16 *pkey)
1469{
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001470 switch (mlx5_get_vport_access_method(ibdev)) {
1471 case MLX5_VPORT_ACCESS_METHOD_MAD:
1472 return mlx5_query_mad_ifc_pkey(ibdev, port, index, pkey);
Eli Cohene126ba92013-07-07 17:25:49 +03001473
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001474 case MLX5_VPORT_ACCESS_METHOD_HCA:
1475 case MLX5_VPORT_ACCESS_METHOD_NIC:
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001476 return mlx5_query_hca_nic_pkey(ibdev, port, index, pkey);
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001477 default:
1478 return -EINVAL;
1479 }
Eli Cohene126ba92013-07-07 17:25:49 +03001480}
1481
Eli Cohene126ba92013-07-07 17:25:49 +03001482static int mlx5_ib_modify_device(struct ib_device *ibdev, int mask,
1483 struct ib_device_modify *props)
1484{
1485 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1486 struct mlx5_reg_node_desc in;
1487 struct mlx5_reg_node_desc out;
1488 int err;
1489
1490 if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
1491 return -EOPNOTSUPP;
1492
1493 if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
1494 return 0;
1495
1496 /*
1497 * If possible, pass node desc to FW, so it can generate
1498 * a 144 trap. If cmd fails, just ignore.
1499 */
Yuval Shaiabd99fde2016-08-25 10:57:07 -07001500 memcpy(&in, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
Jack Morgenstein9603b612014-07-28 23:30:22 +03001501 err = mlx5_core_access_reg(dev->mdev, &in, sizeof(in), &out,
Eli Cohene126ba92013-07-07 17:25:49 +03001502 sizeof(out), MLX5_REG_NODE_DESC, 0, 1);
1503 if (err)
1504 return err;
1505
Yuval Shaiabd99fde2016-08-25 10:57:07 -07001506 memcpy(ibdev->node_desc, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
Eli Cohene126ba92013-07-07 17:25:49 +03001507
1508 return err;
1509}
1510
Eli Cohencdbe33d2017-02-14 07:25:38 +02001511static int set_port_caps_atomic(struct mlx5_ib_dev *dev, u8 port_num, u32 mask,
1512 u32 value)
1513{
1514 struct mlx5_hca_vport_context ctx = {};
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001515 struct mlx5_core_dev *mdev;
1516 u8 mdev_port_num;
Eli Cohencdbe33d2017-02-14 07:25:38 +02001517 int err;
1518
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001519 mdev = mlx5_ib_get_native_port_mdev(dev, port_num, &mdev_port_num);
1520 if (!mdev)
1521 return -ENODEV;
1522
1523 err = mlx5_query_hca_vport_context(mdev, 0, mdev_port_num, 0, &ctx);
Eli Cohencdbe33d2017-02-14 07:25:38 +02001524 if (err)
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001525 goto out;
Eli Cohencdbe33d2017-02-14 07:25:38 +02001526
1527 if (~ctx.cap_mask1_perm & mask) {
1528 mlx5_ib_warn(dev, "trying to change bitmask 0x%X but change supported 0x%X\n",
1529 mask, ctx.cap_mask1_perm);
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001530 err = -EINVAL;
1531 goto out;
Eli Cohencdbe33d2017-02-14 07:25:38 +02001532 }
1533
1534 ctx.cap_mask1 = value;
1535 ctx.cap_mask1_perm = mask;
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001536 err = mlx5_core_modify_hca_vport_context(mdev, 0, mdev_port_num,
1537 0, &ctx);
1538
1539out:
1540 mlx5_ib_put_native_port_mdev(dev, port_num);
Eli Cohencdbe33d2017-02-14 07:25:38 +02001541
1542 return err;
1543}
1544
Eli Cohene126ba92013-07-07 17:25:49 +03001545static int mlx5_ib_modify_port(struct ib_device *ibdev, u8 port, int mask,
1546 struct ib_port_modify *props)
1547{
1548 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1549 struct ib_port_attr attr;
1550 u32 tmp;
1551 int err;
Eli Cohencdbe33d2017-02-14 07:25:38 +02001552 u32 change_mask;
1553 u32 value;
1554 bool is_ib = (mlx5_ib_port_link_layer(ibdev, port) ==
1555 IB_LINK_LAYER_INFINIBAND);
1556
Majd Dibbinyec255872017-08-23 08:35:42 +03001557 /* CM layer calls ib_modify_port() regardless of the link layer. For
1558 * Ethernet ports, qkey violation and Port capabilities are meaningless.
1559 */
1560 if (!is_ib)
1561 return 0;
1562
Eli Cohencdbe33d2017-02-14 07:25:38 +02001563 if (MLX5_CAP_GEN(dev->mdev, ib_virt) && is_ib) {
1564 change_mask = props->clr_port_cap_mask | props->set_port_cap_mask;
1565 value = ~props->clr_port_cap_mask | props->set_port_cap_mask;
1566 return set_port_caps_atomic(dev, port, change_mask, value);
1567 }
Eli Cohene126ba92013-07-07 17:25:49 +03001568
1569 mutex_lock(&dev->cap_mask_mutex);
1570
Or Gerlitzc4550c62017-01-24 13:02:39 +02001571 err = ib_query_port(ibdev, port, &attr);
Eli Cohene126ba92013-07-07 17:25:49 +03001572 if (err)
1573 goto out;
1574
1575 tmp = (attr.port_cap_flags | props->set_port_cap_mask) &
1576 ~props->clr_port_cap_mask;
1577
Jack Morgenstein9603b612014-07-28 23:30:22 +03001578 err = mlx5_set_port_caps(dev->mdev, port, tmp);
Eli Cohene126ba92013-07-07 17:25:49 +03001579
1580out:
1581 mutex_unlock(&dev->cap_mask_mutex);
1582 return err;
1583}
1584
Eli Cohen30aa60b2017-01-03 23:55:27 +02001585static void print_lib_caps(struct mlx5_ib_dev *dev, u64 caps)
1586{
1587 mlx5_ib_dbg(dev, "MLX5_LIB_CAP_4K_UAR = %s\n",
1588 caps & MLX5_LIB_CAP_4K_UAR ? "y" : "n");
1589}
1590
Yishai Hadas31a78a52017-12-24 16:31:34 +02001591static u16 calc_dynamic_bfregs(int uars_per_sys_page)
1592{
1593 /* Large page with non 4k uar support might limit the dynamic size */
1594 if (uars_per_sys_page == 1 && PAGE_SIZE > 4096)
1595 return MLX5_MIN_DYN_BFREGS;
1596
1597 return MLX5_MAX_DYN_BFREGS;
1598}
1599
Eli Cohenb037c292017-01-03 23:55:26 +02001600static int calc_total_bfregs(struct mlx5_ib_dev *dev, bool lib_uar_4k,
1601 struct mlx5_ib_alloc_ucontext_req_v2 *req,
Yishai Hadas31a78a52017-12-24 16:31:34 +02001602 struct mlx5_bfreg_info *bfregi)
Eli Cohenb037c292017-01-03 23:55:26 +02001603{
1604 int uars_per_sys_page;
1605 int bfregs_per_sys_page;
1606 int ref_bfregs = req->total_num_bfregs;
1607
1608 if (req->total_num_bfregs == 0)
1609 return -EINVAL;
1610
1611 BUILD_BUG_ON(MLX5_MAX_BFREGS % MLX5_NON_FP_BFREGS_IN_PAGE);
1612 BUILD_BUG_ON(MLX5_MAX_BFREGS < MLX5_NON_FP_BFREGS_IN_PAGE);
1613
1614 if (req->total_num_bfregs > MLX5_MAX_BFREGS)
1615 return -ENOMEM;
1616
1617 uars_per_sys_page = get_uars_per_sys_page(dev, lib_uar_4k);
1618 bfregs_per_sys_page = uars_per_sys_page * MLX5_NON_FP_BFREGS_PER_UAR;
Yishai Hadas31a78a52017-12-24 16:31:34 +02001619 /* This holds the required static allocation asked by the user */
Eli Cohenb037c292017-01-03 23:55:26 +02001620 req->total_num_bfregs = ALIGN(req->total_num_bfregs, bfregs_per_sys_page);
Eli Cohenb037c292017-01-03 23:55:26 +02001621 if (req->num_low_latency_bfregs > req->total_num_bfregs - 1)
1622 return -EINVAL;
1623
Yishai Hadas31a78a52017-12-24 16:31:34 +02001624 bfregi->num_static_sys_pages = req->total_num_bfregs / bfregs_per_sys_page;
1625 bfregi->num_dyn_bfregs = ALIGN(calc_dynamic_bfregs(uars_per_sys_page), bfregs_per_sys_page);
1626 bfregi->total_num_bfregs = req->total_num_bfregs + bfregi->num_dyn_bfregs;
1627 bfregi->num_sys_pages = bfregi->total_num_bfregs / bfregs_per_sys_page;
1628
1629 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 +02001630 MLX5_CAP_GEN(dev->mdev, uar_4k) ? "yes" : "no",
1631 lib_uar_4k ? "yes" : "no", ref_bfregs,
Yishai Hadas31a78a52017-12-24 16:31:34 +02001632 req->total_num_bfregs, bfregi->total_num_bfregs,
1633 bfregi->num_sys_pages);
Eli Cohenb037c292017-01-03 23:55:26 +02001634
1635 return 0;
1636}
1637
1638static int allocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
1639{
1640 struct mlx5_bfreg_info *bfregi;
1641 int err;
1642 int i;
1643
1644 bfregi = &context->bfregi;
Yishai Hadas31a78a52017-12-24 16:31:34 +02001645 for (i = 0; i < bfregi->num_static_sys_pages; i++) {
Eli Cohenb037c292017-01-03 23:55:26 +02001646 err = mlx5_cmd_alloc_uar(dev->mdev, &bfregi->sys_pages[i]);
1647 if (err)
1648 goto error;
1649
1650 mlx5_ib_dbg(dev, "allocated uar %d\n", bfregi->sys_pages[i]);
1651 }
Yishai Hadas4ed131d2017-12-24 16:31:35 +02001652
1653 for (i = bfregi->num_static_sys_pages; i < bfregi->num_sys_pages; i++)
1654 bfregi->sys_pages[i] = MLX5_IB_INVALID_UAR_INDEX;
1655
Eli Cohenb037c292017-01-03 23:55:26 +02001656 return 0;
1657
1658error:
1659 for (--i; i >= 0; i--)
1660 if (mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]))
1661 mlx5_ib_warn(dev, "failed to free uar %d\n", i);
1662
1663 return err;
1664}
1665
Leon Romanovsky15177992018-06-27 10:44:24 +03001666static void deallocate_uars(struct mlx5_ib_dev *dev,
1667 struct mlx5_ib_ucontext *context)
Eli Cohenb037c292017-01-03 23:55:26 +02001668{
1669 struct mlx5_bfreg_info *bfregi;
Eli Cohenb037c292017-01-03 23:55:26 +02001670 int i;
1671
1672 bfregi = &context->bfregi;
Leon Romanovsky15177992018-06-27 10:44:24 +03001673 for (i = 0; i < bfregi->num_sys_pages; i++)
Yishai Hadas4ed131d2017-12-24 16:31:35 +02001674 if (i < bfregi->num_static_sys_pages ||
Leon Romanovsky15177992018-06-27 10:44:24 +03001675 bfregi->sys_pages[i] != MLX5_IB_INVALID_UAR_INDEX)
1676 mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]);
Eli Cohenb037c292017-01-03 23:55:26 +02001677}
1678
Mark Bloch0042f9e2018-09-17 13:30:49 +03001679int mlx5_ib_enable_lb(struct mlx5_ib_dev *dev, bool td, bool qp)
Mark Blocha560f1d2018-09-17 13:30:47 +03001680{
1681 int err = 0;
1682
1683 mutex_lock(&dev->lb.mutex);
Mark Bloch0042f9e2018-09-17 13:30:49 +03001684 if (td)
1685 dev->lb.user_td++;
1686 if (qp)
1687 dev->lb.qps++;
Mark Blocha560f1d2018-09-17 13:30:47 +03001688
Mark Bloch0042f9e2018-09-17 13:30:49 +03001689 if (dev->lb.user_td == 2 ||
1690 dev->lb.qps == 1) {
1691 if (!dev->lb.enabled) {
1692 err = mlx5_nic_vport_update_local_lb(dev->mdev, true);
1693 dev->lb.enabled = true;
1694 }
1695 }
Mark Blocha560f1d2018-09-17 13:30:47 +03001696
1697 mutex_unlock(&dev->lb.mutex);
1698
1699 return err;
1700}
1701
Mark Bloch0042f9e2018-09-17 13:30:49 +03001702void mlx5_ib_disable_lb(struct mlx5_ib_dev *dev, bool td, bool qp)
Mark Blocha560f1d2018-09-17 13:30:47 +03001703{
1704 mutex_lock(&dev->lb.mutex);
Mark Bloch0042f9e2018-09-17 13:30:49 +03001705 if (td)
1706 dev->lb.user_td--;
1707 if (qp)
1708 dev->lb.qps--;
Mark Blocha560f1d2018-09-17 13:30:47 +03001709
Mark Bloch0042f9e2018-09-17 13:30:49 +03001710 if (dev->lb.user_td == 1 &&
1711 dev->lb.qps == 0) {
1712 if (dev->lb.enabled) {
1713 mlx5_nic_vport_update_local_lb(dev->mdev, false);
1714 dev->lb.enabled = false;
1715 }
1716 }
Mark Blocha560f1d2018-09-17 13:30:47 +03001717
1718 mutex_unlock(&dev->lb.mutex);
1719}
1720
Yishai Hadasd2d19122018-09-20 21:39:32 +03001721static int mlx5_ib_alloc_transport_domain(struct mlx5_ib_dev *dev, u32 *tdn,
1722 u16 uid)
Huy Nguyenc85023e2017-05-30 09:42:54 +03001723{
1724 int err;
1725
Leon Romanovskycfdeb892018-06-19 10:39:06 +03001726 if (!MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1727 return 0;
1728
Yishai Hadasd2d19122018-09-20 21:39:32 +03001729 err = mlx5_cmd_alloc_transport_domain(dev->mdev, tdn, uid);
Huy Nguyenc85023e2017-05-30 09:42:54 +03001730 if (err)
1731 return err;
1732
1733 if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
Eran Ben Elisha8978cc92018-01-09 11:41:10 +02001734 (!MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) &&
1735 !MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
Huy Nguyenc85023e2017-05-30 09:42:54 +03001736 return err;
1737
Mark Bloch0042f9e2018-09-17 13:30:49 +03001738 return mlx5_ib_enable_lb(dev, true, false);
Huy Nguyenc85023e2017-05-30 09:42:54 +03001739}
1740
Yishai Hadasd2d19122018-09-20 21:39:32 +03001741static void mlx5_ib_dealloc_transport_domain(struct mlx5_ib_dev *dev, u32 tdn,
1742 u16 uid)
Huy Nguyenc85023e2017-05-30 09:42:54 +03001743{
Leon Romanovskycfdeb892018-06-19 10:39:06 +03001744 if (!MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1745 return;
1746
Yishai Hadasd2d19122018-09-20 21:39:32 +03001747 mlx5_cmd_dealloc_transport_domain(dev->mdev, tdn, uid);
Huy Nguyenc85023e2017-05-30 09:42:54 +03001748
1749 if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
Eran Ben Elisha8978cc92018-01-09 11:41:10 +02001750 (!MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) &&
1751 !MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
Huy Nguyenc85023e2017-05-30 09:42:54 +03001752 return;
1753
Mark Bloch0042f9e2018-09-17 13:30:49 +03001754 mlx5_ib_disable_lb(dev, true, false);
Huy Nguyenc85023e2017-05-30 09:42:54 +03001755}
1756
Leon Romanovskya2a074e2019-02-12 20:39:16 +02001757static int mlx5_ib_alloc_ucontext(struct ib_ucontext *uctx,
1758 struct ib_udata *udata)
Eli Cohene126ba92013-07-07 17:25:49 +03001759{
Leon Romanovskya2a074e2019-02-12 20:39:16 +02001760 struct ib_device *ibdev = uctx->device;
Eli Cohene126ba92013-07-07 17:25:49 +03001761 struct mlx5_ib_dev *dev = to_mdev(ibdev);
Matan Barakb368d7c2015-12-15 20:30:12 +02001762 struct mlx5_ib_alloc_ucontext_req_v2 req = {};
1763 struct mlx5_ib_alloc_ucontext_resp resp = {};
Feras Daoud5c99eae2018-01-16 20:08:41 +02001764 struct mlx5_core_dev *mdev = dev->mdev;
Leon Romanovskya2a074e2019-02-12 20:39:16 +02001765 struct mlx5_ib_ucontext *context = to_mucontext(uctx);
Eli Cohen2f5ff262017-01-03 23:55:21 +02001766 struct mlx5_bfreg_info *bfregi;
Eli Cohen78c0f982014-01-30 13:49:48 +02001767 int ver;
Eli Cohene126ba92013-07-07 17:25:49 +03001768 int err;
Majd Dibbinya168a41c2016-01-28 17:51:47 +02001769 size_t min_req_v2 = offsetof(struct mlx5_ib_alloc_ucontext_req_v2,
1770 max_cqe_version);
Yonatan Cohen25bb36e2018-06-19 08:47:24 +03001771 u32 dump_fill_mkey;
Eli Cohenb037c292017-01-03 23:55:26 +02001772 bool lib_uar_4k;
Eli Cohene126ba92013-07-07 17:25:49 +03001773
1774 if (!dev->ib_active)
Leon Romanovskya2a074e2019-02-12 20:39:16 +02001775 return -EAGAIN;
Eli Cohene126ba92013-07-07 17:25:49 +03001776
Amrani, Rame0931112017-06-27 17:04:42 +03001777 if (udata->inlen == sizeof(struct mlx5_ib_alloc_ucontext_req))
Eli Cohen78c0f982014-01-30 13:49:48 +02001778 ver = 0;
Amrani, Rame0931112017-06-27 17:04:42 +03001779 else if (udata->inlen >= min_req_v2)
Eli Cohen78c0f982014-01-30 13:49:48 +02001780 ver = 2;
1781 else
Leon Romanovskya2a074e2019-02-12 20:39:16 +02001782 return -EINVAL;
Eli Cohen78c0f982014-01-30 13:49:48 +02001783
Amrani, Rame0931112017-06-27 17:04:42 +03001784 err = ib_copy_from_udata(&req, udata, min(udata->inlen, sizeof(req)));
Eli Cohene126ba92013-07-07 17:25:49 +03001785 if (err)
Leon Romanovskya2a074e2019-02-12 20:39:16 +02001786 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03001787
Yishai Hadasa8b92ca2018-06-17 12:59:57 +03001788 if (req.flags & ~MLX5_IB_ALLOC_UCTX_DEVX)
Leon Romanovskya2a074e2019-02-12 20:39:16 +02001789 return -EOPNOTSUPP;
Eli Cohen78c0f982014-01-30 13:49:48 +02001790
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001791 if (req.comp_mask || req.reserved0 || req.reserved1 || req.reserved2)
Leon Romanovskya2a074e2019-02-12 20:39:16 +02001792 return -EOPNOTSUPP;
Matan Barakb368d7c2015-12-15 20:30:12 +02001793
Eli Cohen2f5ff262017-01-03 23:55:21 +02001794 req.total_num_bfregs = ALIGN(req.total_num_bfregs,
1795 MLX5_NON_FP_BFREGS_PER_UAR);
1796 if (req.num_low_latency_bfregs > req.total_num_bfregs - 1)
Leon Romanovskya2a074e2019-02-12 20:39:16 +02001797 return -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +03001798
Saeed Mahameed938fe832015-05-28 22:28:41 +03001799 resp.qp_tab_size = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp);
Noa Osherovich2cc6ad52016-06-04 15:15:33 +03001800 if (mlx5_core_is_pf(dev->mdev) && MLX5_CAP_GEN(dev->mdev, bf))
1801 resp.bf_reg_size = 1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size);
Daniel Jurgensb47bd6e2016-10-25 18:36:24 +03001802 resp.cache_line_size = cache_line_size();
Saeed Mahameed938fe832015-05-28 22:28:41 +03001803 resp.max_sq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq);
1804 resp.max_rq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq);
1805 resp.max_send_wqebb = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1806 resp.max_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1807 resp.max_srq_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz);
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001808 resp.cqe_version = min_t(__u8,
1809 (__u8)MLX5_CAP_GEN(dev->mdev, cqe_version),
1810 req.max_cqe_version);
Eli Cohen30aa60b2017-01-03 23:55:27 +02001811 resp.log_uar_size = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
1812 MLX5_ADAPTER_PAGE_SHIFT : PAGE_SHIFT;
1813 resp.num_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
1814 MLX5_CAP_GEN(dev->mdev, num_of_uars_per_page) : 1;
Matan Barakb368d7c2015-12-15 20:30:12 +02001815 resp.response_length = min(offsetof(typeof(resp), response_length) +
1816 sizeof(resp.response_length), udata->outlen);
Eli Cohene126ba92013-07-07 17:25:49 +03001817
Matan Barakc03faa52018-03-28 09:27:54 +03001818 if (mlx5_accel_ipsec_device_caps(dev->mdev) & MLX5_ACCEL_IPSEC_CAP_DEVICE) {
1819 if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_EGRESS))
1820 resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM;
1821 if (mlx5_accel_ipsec_device_caps(dev->mdev) & MLX5_ACCEL_IPSEC_CAP_REQUIRED_METADATA)
1822 resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_REQ_METADATA;
1823 if (MLX5_CAP_FLOWTABLE(dev->mdev, flow_table_properties_nic_receive.ft_field_support.outer_esp_spi))
1824 resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_SPI_STEERING;
1825 if (mlx5_accel_ipsec_device_caps(dev->mdev) & MLX5_ACCEL_IPSEC_CAP_TX_IV_IS_ESN)
1826 resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_TX_IV_IS_ESN;
1827 /* MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_FULL_OFFLOAD is currently always 0 */
1828 }
1829
Eli Cohen30aa60b2017-01-03 23:55:27 +02001830 lib_uar_4k = req.lib_caps & MLX5_LIB_CAP_4K_UAR;
Eli Cohen2f5ff262017-01-03 23:55:21 +02001831 bfregi = &context->bfregi;
Eli Cohenb037c292017-01-03 23:55:26 +02001832
1833 /* updates req->total_num_bfregs */
Yishai Hadas31a78a52017-12-24 16:31:34 +02001834 err = calc_total_bfregs(dev, lib_uar_4k, &req, bfregi);
Eli Cohenb037c292017-01-03 23:55:26 +02001835 if (err)
1836 goto out_ctx;
1837
Eli Cohen2f5ff262017-01-03 23:55:21 +02001838 mutex_init(&bfregi->lock);
Eli Cohenb037c292017-01-03 23:55:26 +02001839 bfregi->lib_uar_4k = lib_uar_4k;
Yishai Hadas31a78a52017-12-24 16:31:34 +02001840 bfregi->count = kcalloc(bfregi->total_num_bfregs, sizeof(*bfregi->count),
Eli Cohenb037c292017-01-03 23:55:26 +02001841 GFP_KERNEL);
1842 if (!bfregi->count) {
Eli Cohene126ba92013-07-07 17:25:49 +03001843 err = -ENOMEM;
1844 goto out_ctx;
1845 }
1846
Eli Cohenb037c292017-01-03 23:55:26 +02001847 bfregi->sys_pages = kcalloc(bfregi->num_sys_pages,
1848 sizeof(*bfregi->sys_pages),
1849 GFP_KERNEL);
1850 if (!bfregi->sys_pages) {
Eli Cohene126ba92013-07-07 17:25:49 +03001851 err = -ENOMEM;
Eli Cohenb037c292017-01-03 23:55:26 +02001852 goto out_count;
Eli Cohene126ba92013-07-07 17:25:49 +03001853 }
1854
Eli Cohenb037c292017-01-03 23:55:26 +02001855 err = allocate_uars(dev, context);
1856 if (err)
1857 goto out_sys_pages;
Eli Cohene126ba92013-07-07 17:25:49 +03001858
Leon Romanovsky13859d5d2019-01-08 16:07:26 +02001859 if (ibdev->attrs.device_cap_flags & IB_DEVICE_ON_DEMAND_PAGING)
1860 context->ibucontext.invalidate_range =
1861 &mlx5_ib_invalidate_range;
Haggai Eranb4cfe442014-12-11 17:04:26 +02001862
Yishai Hadasa8b92ca2018-06-17 12:59:57 +03001863 if (req.flags & MLX5_IB_ALLOC_UCTX_DEVX) {
Yishai Hadasfb981532018-11-26 08:28:36 +02001864 err = mlx5_ib_devx_create(dev, true);
Yishai Hadas76dc5a82018-09-20 21:45:19 +03001865 if (err < 0)
Yishai Hadasd2d19122018-09-20 21:39:32 +03001866 goto out_uars;
Yishai Hadas76dc5a82018-09-20 21:45:19 +03001867 context->devx_uid = err;
Yishai Hadasa8b92ca2018-06-17 12:59:57 +03001868 }
1869
Yishai Hadasd2d19122018-09-20 21:39:32 +03001870 err = mlx5_ib_alloc_transport_domain(dev, &context->tdn,
1871 context->devx_uid);
1872 if (err)
1873 goto out_devx;
1874
Yonatan Cohen25bb36e2018-06-19 08:47:24 +03001875 if (MLX5_CAP_GEN(dev->mdev, dump_fill_mkey)) {
1876 err = mlx5_cmd_dump_fill_mkey(dev->mdev, &dump_fill_mkey);
1877 if (err)
Jason Gunthorpe8193abb2018-07-04 13:19:46 -06001878 goto out_mdev;
Yonatan Cohen25bb36e2018-06-19 08:47:24 +03001879 }
1880
Eli Cohene126ba92013-07-07 17:25:49 +03001881 INIT_LIST_HEAD(&context->db_page_list);
1882 mutex_init(&context->db_page_mutex);
1883
Eli Cohen2f5ff262017-01-03 23:55:21 +02001884 resp.tot_bfregs = req.total_num_bfregs;
Daniel Jurgens508562d2018-01-04 17:25:34 +02001885 resp.num_ports = dev->num_ports;
Matan Barakb368d7c2015-12-15 20:30:12 +02001886
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001887 if (field_avail(typeof(resp), cqe_version, udata->outlen))
1888 resp.response_length += sizeof(resp.cqe_version);
Matan Barakb368d7c2015-12-15 20:30:12 +02001889
Bodong Wang402ca532016-06-17 15:02:20 +03001890 if (field_avail(typeof(resp), cmds_supp_uhw, udata->outlen)) {
Moni Shoua6ad279c52016-11-23 08:23:23 +02001891 resp.cmds_supp_uhw |= MLX5_USER_CMDS_SUPP_UHW_QUERY_DEVICE |
1892 MLX5_USER_CMDS_SUPP_UHW_CREATE_AH;
Bodong Wang402ca532016-06-17 15:02:20 +03001893 resp.response_length += sizeof(resp.cmds_supp_uhw);
1894 }
1895
Or Gerlitz78984892016-11-30 20:33:33 +02001896 if (field_avail(typeof(resp), eth_min_inline, udata->outlen)) {
1897 if (mlx5_ib_port_link_layer(ibdev, 1) == IB_LINK_LAYER_ETHERNET) {
1898 mlx5_query_min_inline(dev->mdev, &resp.eth_min_inline);
1899 resp.eth_min_inline++;
1900 }
1901 resp.response_length += sizeof(resp.eth_min_inline);
1902 }
1903
Feras Daoud5c99eae2018-01-16 20:08:41 +02001904 if (field_avail(typeof(resp), clock_info_versions, udata->outlen)) {
1905 if (mdev->clock_info)
1906 resp.clock_info_versions = BIT(MLX5_IB_CLOCK_INFO_V1);
1907 resp.response_length += sizeof(resp.clock_info_versions);
1908 }
1909
Noa Osherovichbc5c6ee2016-06-04 15:15:31 +03001910 /*
1911 * We don't want to expose information from the PCI bar that is located
1912 * after 4096 bytes, so if the arch only supports larger pages, let's
1913 * pretend we don't support reading the HCA's core clock. This is also
1914 * forced by mmap function.
1915 */
Eli Cohende8d6e02017-01-03 23:55:19 +02001916 if (field_avail(typeof(resp), hca_core_clock_offset, udata->outlen)) {
1917 if (PAGE_SIZE <= 4096) {
1918 resp.comp_mask |=
1919 MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_CORE_CLOCK_OFFSET;
1920 resp.hca_core_clock_offset =
1921 offsetof(struct mlx5_init_seg, internal_timer_h) % PAGE_SIZE;
1922 }
Feras Daoud5c99eae2018-01-16 20:08:41 +02001923 resp.response_length += sizeof(resp.hca_core_clock_offset);
Matan Barakb368d7c2015-12-15 20:30:12 +02001924 }
1925
Eli Cohen30aa60b2017-01-03 23:55:27 +02001926 if (field_avail(typeof(resp), log_uar_size, udata->outlen))
1927 resp.response_length += sizeof(resp.log_uar_size);
1928
1929 if (field_avail(typeof(resp), num_uars_per_page, udata->outlen))
1930 resp.response_length += sizeof(resp.num_uars_per_page);
1931
Yishai Hadas31a78a52017-12-24 16:31:34 +02001932 if (field_avail(typeof(resp), num_dyn_bfregs, udata->outlen)) {
1933 resp.num_dyn_bfregs = bfregi->num_dyn_bfregs;
1934 resp.response_length += sizeof(resp.num_dyn_bfregs);
1935 }
1936
Yonatan Cohen25bb36e2018-06-19 08:47:24 +03001937 if (field_avail(typeof(resp), dump_fill_mkey, udata->outlen)) {
1938 if (MLX5_CAP_GEN(dev->mdev, dump_fill_mkey)) {
1939 resp.dump_fill_mkey = dump_fill_mkey;
1940 resp.comp_mask |=
1941 MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_DUMP_FILL_MKEY;
1942 }
1943 resp.response_length += sizeof(resp.dump_fill_mkey);
1944 }
1945
Matan Barakb368d7c2015-12-15 20:30:12 +02001946 err = ib_copy_to_udata(udata, &resp, resp.response_length);
Eli Cohene126ba92013-07-07 17:25:49 +03001947 if (err)
Yishai Hadasa8b92ca2018-06-17 12:59:57 +03001948 goto out_mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03001949
Eli Cohen2f5ff262017-01-03 23:55:21 +02001950 bfregi->ver = ver;
1951 bfregi->num_low_latency_bfregs = req.num_low_latency_bfregs;
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001952 context->cqe_version = resp.cqe_version;
Eli Cohen30aa60b2017-01-03 23:55:27 +02001953 context->lib_caps = req.lib_caps;
1954 print_lib_caps(dev, context->lib_caps);
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001955
Aviv Heller7c34ec12018-08-23 13:47:53 +03001956 if (dev->lag_active) {
Majd Dibbinyc6a21c32018-08-28 14:29:05 +03001957 u8 port = mlx5_core_native_port_num(dev->mdev);
1958
1959 atomic_set(&context->tx_port_affinity,
1960 atomic_add_return(
1961 1, &dev->roce[port].tx_port_affinity));
1962 }
1963
Leon Romanovskya2a074e2019-02-12 20:39:16 +02001964 return 0;
Eli Cohene126ba92013-07-07 17:25:49 +03001965
Yishai Hadasa8b92ca2018-06-17 12:59:57 +03001966out_mdev:
Yishai Hadasd2d19122018-09-20 21:39:32 +03001967 mlx5_ib_dealloc_transport_domain(dev, context->tdn, context->devx_uid);
1968out_devx:
Yishai Hadasa8b92ca2018-06-17 12:59:57 +03001969 if (req.flags & MLX5_IB_ALLOC_UCTX_DEVX)
Yishai Hadas76dc5a82018-09-20 21:45:19 +03001970 mlx5_ib_devx_destroy(dev, context->devx_uid);
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001971
Eli Cohene126ba92013-07-07 17:25:49 +03001972out_uars:
Eli Cohenb037c292017-01-03 23:55:26 +02001973 deallocate_uars(dev, context);
1974
1975out_sys_pages:
1976 kfree(bfregi->sys_pages);
1977
Eli Cohene126ba92013-07-07 17:25:49 +03001978out_count:
Eli Cohen2f5ff262017-01-03 23:55:21 +02001979 kfree(bfregi->count);
Eli Cohene126ba92013-07-07 17:25:49 +03001980
Eli Cohene126ba92013-07-07 17:25:49 +03001981out_ctx:
Leon Romanovskya2a074e2019-02-12 20:39:16 +02001982 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03001983}
1984
Leon Romanovskya2a074e2019-02-12 20:39:16 +02001985static void mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
Eli Cohene126ba92013-07-07 17:25:49 +03001986{
1987 struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1988 struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
Eli Cohenb037c292017-01-03 23:55:26 +02001989 struct mlx5_bfreg_info *bfregi;
Eli Cohene126ba92013-07-07 17:25:49 +03001990
Jason Gunthorpef27a0d52018-09-16 20:48:08 +03001991 /* All umem's must be destroyed before destroying the ucontext. */
1992 mutex_lock(&ibcontext->per_mm_list_lock);
1993 WARN_ON(!list_empty(&ibcontext->per_mm_list));
1994 mutex_unlock(&ibcontext->per_mm_list_lock);
Yishai Hadasa8b92ca2018-06-17 12:59:57 +03001995
Eli Cohenb037c292017-01-03 23:55:26 +02001996 bfregi = &context->bfregi;
Yishai Hadasd2d19122018-09-20 21:39:32 +03001997 mlx5_ib_dealloc_transport_domain(dev, context->tdn, context->devx_uid);
1998
Eli Cohenb037c292017-01-03 23:55:26 +02001999 if (context->devx_uid)
Yishai Hadas76dc5a82018-09-20 21:45:19 +03002000 mlx5_ib_devx_destroy(dev, context->devx_uid);
Eli Cohene126ba92013-07-07 17:25:49 +03002001
2002 deallocate_uars(dev, context);
Eli Cohen2f5ff262017-01-03 23:55:21 +02002003 kfree(bfregi->sys_pages);
2004 kfree(bfregi->count);
Eli Cohene126ba92013-07-07 17:25:49 +03002005}
2006
2007static phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev,
2008 int uar_idx)
2009{
Eli Cohenb037c292017-01-03 23:55:26 +02002010 int fw_uars_per_page;
2011
2012 fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ? MLX5_UARS_IN_PAGE : 1;
2013
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002014 return (pci_resource_start(dev->mdev->pdev, 0) >> PAGE_SHIFT) + uar_idx / fw_uars_per_page;
Eli Cohene126ba92013-07-07 17:25:49 +03002015}
2016
2017static int get_command(unsigned long offset)
2018{
2019 return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK;
2020}
2021
2022static int get_arg(unsigned long offset)
2023{
2024 return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1);
2025}
2026
2027static int get_index(unsigned long offset)
2028{
2029 return get_arg(offset);
2030}
2031
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002032/* Index resides in an extra byte to enable larger values than 255 */
2033static int get_extended_index(unsigned long offset)
2034{
2035 return get_arg(offset) | ((offset >> 16) & 0xff) << 8;
2036}
2037
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03002038
2039static void mlx5_ib_disassociate_ucontext(struct ib_ucontext *ibcontext)
2040{
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03002041}
2042
Guy Levi37aa5c32016-04-27 16:49:50 +03002043static inline char *mmap_cmd2str(enum mlx5_ib_mmap_cmd cmd)
2044{
2045 switch (cmd) {
2046 case MLX5_IB_MMAP_WC_PAGE:
2047 return "WC";
2048 case MLX5_IB_MMAP_REGULAR_PAGE:
2049 return "best effort WC";
2050 case MLX5_IB_MMAP_NC_PAGE:
2051 return "NC";
Ariel Levkovich24da0012018-04-05 18:53:27 +03002052 case MLX5_IB_MMAP_DEVICE_MEM:
2053 return "Device Memory";
Guy Levi37aa5c32016-04-27 16:49:50 +03002054 default:
2055 return NULL;
2056 }
2057}
2058
Feras Daoud5c99eae2018-01-16 20:08:41 +02002059static int mlx5_ib_mmap_clock_info_page(struct mlx5_ib_dev *dev,
2060 struct vm_area_struct *vma,
2061 struct mlx5_ib_ucontext *context)
2062{
Jason Gunthorpe4eb6ab12019-04-16 14:07:30 +03002063 if ((vma->vm_end - vma->vm_start != PAGE_SIZE) ||
2064 !(vma->vm_flags & VM_SHARED))
Feras Daoud5c99eae2018-01-16 20:08:41 +02002065 return -EINVAL;
2066
2067 if (get_index(vma->vm_pgoff) != MLX5_IB_CLOCK_INFO_V1)
2068 return -EOPNOTSUPP;
2069
Jason Gunthorpe4eb6ab12019-04-16 14:07:30 +03002070 if (vma->vm_flags & (VM_WRITE | VM_EXEC))
Feras Daoud5c99eae2018-01-16 20:08:41 +02002071 return -EPERM;
Jason Gunthorpe4eb6ab12019-04-16 14:07:30 +03002072 vma->vm_flags &= ~(VM_MAYWRITE | VM_MAYEXEC);
Feras Daoud5c99eae2018-01-16 20:08:41 +02002073
Jason Gunthorpeddcdc362019-04-16 14:07:29 +03002074 if (!dev->mdev->clock_info)
Feras Daoud5c99eae2018-01-16 20:08:41 +02002075 return -EOPNOTSUPP;
2076
Jason Gunthorpe4eb6ab12019-04-16 14:07:30 +03002077 return vm_insert_page(vma, vma->vm_start,
2078 virt_to_page(dev->mdev->clock_info));
Feras Daoud5c99eae2018-01-16 20:08:41 +02002079}
2080
Guy Levi37aa5c32016-04-27 16:49:50 +03002081static int uar_mmap(struct mlx5_ib_dev *dev, enum mlx5_ib_mmap_cmd cmd,
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03002082 struct vm_area_struct *vma,
2083 struct mlx5_ib_ucontext *context)
Guy Levi37aa5c32016-04-27 16:49:50 +03002084{
Eli Cohen2f5ff262017-01-03 23:55:21 +02002085 struct mlx5_bfreg_info *bfregi = &context->bfregi;
Guy Levi37aa5c32016-04-27 16:49:50 +03002086 int err;
2087 unsigned long idx;
Kamal Heibaa09ea62018-07-19 00:05:32 +03002088 phys_addr_t pfn;
Guy Levi37aa5c32016-04-27 16:49:50 +03002089 pgprot_t prot;
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002090 u32 bfreg_dyn_idx = 0;
2091 u32 uar_index;
2092 int dyn_uar = (cmd == MLX5_IB_MMAP_ALLOC_WC);
2093 int max_valid_idx = dyn_uar ? bfregi->num_sys_pages :
2094 bfregi->num_static_sys_pages;
Eli Cohenb037c292017-01-03 23:55:26 +02002095
2096 if (vma->vm_end - vma->vm_start != PAGE_SIZE)
2097 return -EINVAL;
2098
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002099 if (dyn_uar)
2100 idx = get_extended_index(vma->vm_pgoff) + bfregi->num_static_sys_pages;
2101 else
2102 idx = get_index(vma->vm_pgoff);
2103
2104 if (idx >= max_valid_idx) {
2105 mlx5_ib_warn(dev, "invalid uar index %lu, max=%d\n",
2106 idx, max_valid_idx);
Eli Cohenb037c292017-01-03 23:55:26 +02002107 return -EINVAL;
2108 }
Guy Levi37aa5c32016-04-27 16:49:50 +03002109
2110 switch (cmd) {
2111 case MLX5_IB_MMAP_WC_PAGE:
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002112 case MLX5_IB_MMAP_ALLOC_WC:
Guy Levi37aa5c32016-04-27 16:49:50 +03002113/* Some architectures don't support WC memory */
2114#if defined(CONFIG_X86)
2115 if (!pat_enabled())
2116 return -EPERM;
2117#elif !(defined(CONFIG_PPC) || (defined(CONFIG_ARM) && defined(CONFIG_MMU)))
2118 return -EPERM;
2119#endif
2120 /* fall through */
2121 case MLX5_IB_MMAP_REGULAR_PAGE:
2122 /* For MLX5_IB_MMAP_REGULAR_PAGE do the best effort to get WC */
2123 prot = pgprot_writecombine(vma->vm_page_prot);
2124 break;
2125 case MLX5_IB_MMAP_NC_PAGE:
2126 prot = pgprot_noncached(vma->vm_page_prot);
2127 break;
2128 default:
2129 return -EINVAL;
2130 }
2131
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002132 if (dyn_uar) {
2133 int uars_per_page;
2134
2135 uars_per_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k);
2136 bfreg_dyn_idx = idx * (uars_per_page * MLX5_NON_FP_BFREGS_PER_UAR);
2137 if (bfreg_dyn_idx >= bfregi->total_num_bfregs) {
2138 mlx5_ib_warn(dev, "invalid bfreg_dyn_idx %u, max=%u\n",
2139 bfreg_dyn_idx, bfregi->total_num_bfregs);
2140 return -EINVAL;
2141 }
2142
2143 mutex_lock(&bfregi->lock);
2144 /* Fail if uar already allocated, first bfreg index of each
2145 * page holds its count.
2146 */
2147 if (bfregi->count[bfreg_dyn_idx]) {
2148 mlx5_ib_warn(dev, "wrong offset, idx %lu is busy, bfregn=%u\n", idx, bfreg_dyn_idx);
2149 mutex_unlock(&bfregi->lock);
2150 return -EINVAL;
2151 }
2152
2153 bfregi->count[bfreg_dyn_idx]++;
2154 mutex_unlock(&bfregi->lock);
2155
2156 err = mlx5_cmd_alloc_uar(dev->mdev, &uar_index);
2157 if (err) {
2158 mlx5_ib_warn(dev, "UAR alloc failed\n");
2159 goto free_bfreg;
2160 }
2161 } else {
2162 uar_index = bfregi->sys_pages[idx];
2163 }
2164
2165 pfn = uar_index2pfn(dev, uar_index);
Guy Levi37aa5c32016-04-27 16:49:50 +03002166 mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn %pa\n", idx, &pfn);
2167
Jason Gunthorpee2cd1d12018-09-16 20:43:10 +03002168 err = rdma_user_mmap_io(&context->ibucontext, vma, pfn, PAGE_SIZE,
2169 prot);
Guy Levi37aa5c32016-04-27 16:49:50 +03002170 if (err) {
Leon Romanovsky8f062282018-05-22 08:31:03 +03002171 mlx5_ib_err(dev,
Jason Gunthorpee2cd1d12018-09-16 20:43:10 +03002172 "rdma_user_mmap_io failed with error=%d, mmap_cmd=%s\n",
Leon Romanovsky8f062282018-05-22 08:31:03 +03002173 err, mmap_cmd2str(cmd));
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002174 goto err;
Guy Levi37aa5c32016-04-27 16:49:50 +03002175 }
2176
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002177 if (dyn_uar)
2178 bfregi->sys_pages[idx] = uar_index;
2179 return 0;
2180
2181err:
2182 if (!dyn_uar)
2183 return err;
2184
2185 mlx5_cmd_free_uar(dev->mdev, idx);
2186
2187free_bfreg:
2188 mlx5_ib_free_bfreg(dev, bfregi, bfreg_dyn_idx);
2189
2190 return err;
Guy Levi37aa5c32016-04-27 16:49:50 +03002191}
2192
Ariel Levkovich24da0012018-04-05 18:53:27 +03002193static int dm_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
2194{
2195 struct mlx5_ib_ucontext *mctx = to_mucontext(context);
2196 struct mlx5_ib_dev *dev = to_mdev(context->device);
2197 u16 page_idx = get_extended_index(vma->vm_pgoff);
2198 size_t map_size = vma->vm_end - vma->vm_start;
2199 u32 npages = map_size >> PAGE_SHIFT;
2200 phys_addr_t pfn;
Ariel Levkovich24da0012018-04-05 18:53:27 +03002201
2202 if (find_next_zero_bit(mctx->dm_pages, page_idx + npages, page_idx) !=
2203 page_idx + npages)
2204 return -EINVAL;
2205
2206 pfn = ((pci_resource_start(dev->mdev->pdev, 0) +
2207 MLX5_CAP64_DEV_MEM(dev->mdev, memic_bar_start_addr)) >>
2208 PAGE_SHIFT) +
2209 page_idx;
Jason Gunthorpee2cd1d12018-09-16 20:43:10 +03002210 return rdma_user_mmap_io(context, vma, pfn, map_size,
2211 pgprot_writecombine(vma->vm_page_prot));
Ariel Levkovich24da0012018-04-05 18:53:27 +03002212}
2213
Eli Cohene126ba92013-07-07 17:25:49 +03002214static int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma)
2215{
2216 struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
2217 struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
Eli Cohene126ba92013-07-07 17:25:49 +03002218 unsigned long command;
Eli Cohene126ba92013-07-07 17:25:49 +03002219 phys_addr_t pfn;
2220
2221 command = get_command(vma->vm_pgoff);
2222 switch (command) {
Guy Levi37aa5c32016-04-27 16:49:50 +03002223 case MLX5_IB_MMAP_WC_PAGE:
2224 case MLX5_IB_MMAP_NC_PAGE:
Eli Cohene126ba92013-07-07 17:25:49 +03002225 case MLX5_IB_MMAP_REGULAR_PAGE:
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002226 case MLX5_IB_MMAP_ALLOC_WC:
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03002227 return uar_mmap(dev, command, vma, context);
Eli Cohene126ba92013-07-07 17:25:49 +03002228
2229 case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES:
2230 return -ENOSYS;
2231
Matan Barakd69e3bc2015-12-15 20:30:13 +02002232 case MLX5_IB_MMAP_CORE_CLOCK:
Matan Barakd69e3bc2015-12-15 20:30:13 +02002233 if (vma->vm_end - vma->vm_start != PAGE_SIZE)
2234 return -EINVAL;
2235
Matan Barak6cbac1e2016-04-14 16:52:10 +03002236 if (vma->vm_flags & VM_WRITE)
Matan Barakd69e3bc2015-12-15 20:30:13 +02002237 return -EPERM;
Jason Gunthorpec6601332019-04-16 14:07:25 +03002238 vma->vm_flags &= ~VM_MAYWRITE;
Matan Barakd69e3bc2015-12-15 20:30:13 +02002239
2240 /* Don't expose to user-space information it shouldn't have */
2241 if (PAGE_SIZE > 4096)
2242 return -EOPNOTSUPP;
2243
Matan Barakd69e3bc2015-12-15 20:30:13 +02002244 pfn = (dev->mdev->iseg_base +
2245 offsetof(struct mlx5_init_seg, internal_timer_h)) >>
2246 PAGE_SHIFT;
Jason Gunthorped5e560d2019-04-16 14:07:26 +03002247 return rdma_user_mmap_io(&context->ibucontext, vma, pfn,
2248 PAGE_SIZE,
2249 pgprot_noncached(vma->vm_page_prot));
Feras Daoud5c99eae2018-01-16 20:08:41 +02002250 case MLX5_IB_MMAP_CLOCK_INFO:
2251 return mlx5_ib_mmap_clock_info_page(dev, vma, context);
Matan Barakd69e3bc2015-12-15 20:30:13 +02002252
Ariel Levkovich24da0012018-04-05 18:53:27 +03002253 case MLX5_IB_MMAP_DEVICE_MEM:
2254 return dm_mmap(ibcontext, vma);
2255
Eli Cohene126ba92013-07-07 17:25:49 +03002256 default:
2257 return -EINVAL;
2258 }
2259
2260 return 0;
2261}
2262
Ariel Levkovich24da0012018-04-05 18:53:27 +03002263struct ib_dm *mlx5_ib_alloc_dm(struct ib_device *ibdev,
2264 struct ib_ucontext *context,
2265 struct ib_dm_alloc_attr *attr,
2266 struct uverbs_attr_bundle *attrs)
2267{
2268 u64 act_size = roundup(attr->length, MLX5_MEMIC_BASE_SIZE);
2269 struct mlx5_memic *memic = &to_mdev(ibdev)->memic;
2270 phys_addr_t memic_addr;
2271 struct mlx5_ib_dm *dm;
2272 u64 start_offset;
2273 u32 page_idx;
2274 int err;
2275
2276 dm = kzalloc(sizeof(*dm), GFP_KERNEL);
2277 if (!dm)
2278 return ERR_PTR(-ENOMEM);
2279
2280 mlx5_ib_dbg(to_mdev(ibdev), "alloc_memic req: user_length=0x%llx act_length=0x%llx log_alignment=%d\n",
2281 attr->length, act_size, attr->alignment);
2282
2283 err = mlx5_cmd_alloc_memic(memic, &memic_addr,
2284 act_size, attr->alignment);
2285 if (err)
2286 goto err_free;
2287
2288 start_offset = memic_addr & ~PAGE_MASK;
2289 page_idx = (memic_addr - pci_resource_start(memic->dev->pdev, 0) -
2290 MLX5_CAP64_DEV_MEM(memic->dev, memic_bar_start_addr)) >>
2291 PAGE_SHIFT;
2292
2293 err = uverbs_copy_to(attrs,
2294 MLX5_IB_ATTR_ALLOC_DM_RESP_START_OFFSET,
2295 &start_offset, sizeof(start_offset));
2296 if (err)
2297 goto err_dealloc;
2298
2299 err = uverbs_copy_to(attrs,
2300 MLX5_IB_ATTR_ALLOC_DM_RESP_PAGE_INDEX,
2301 &page_idx, sizeof(page_idx));
2302 if (err)
2303 goto err_dealloc;
2304
2305 bitmap_set(to_mucontext(context)->dm_pages, page_idx,
2306 DIV_ROUND_UP(act_size, PAGE_SIZE));
2307
2308 dm->dev_addr = memic_addr;
2309
2310 return &dm->ibdm;
2311
2312err_dealloc:
2313 mlx5_cmd_dealloc_memic(memic, memic_addr,
2314 act_size);
2315err_free:
2316 kfree(dm);
2317 return ERR_PTR(err);
2318}
2319
2320int mlx5_ib_dealloc_dm(struct ib_dm *ibdm)
2321{
2322 struct mlx5_memic *memic = &to_mdev(ibdm->device)->memic;
2323 struct mlx5_ib_dm *dm = to_mdm(ibdm);
2324 u64 act_size = roundup(dm->ibdm.length, MLX5_MEMIC_BASE_SIZE);
2325 u32 page_idx;
2326 int ret;
2327
2328 ret = mlx5_cmd_dealloc_memic(memic, dm->dev_addr, act_size);
2329 if (ret)
2330 return ret;
2331
2332 page_idx = (dm->dev_addr - pci_resource_start(memic->dev->pdev, 0) -
2333 MLX5_CAP64_DEV_MEM(memic->dev, memic_bar_start_addr)) >>
2334 PAGE_SHIFT;
2335 bitmap_clear(to_mucontext(ibdm->uobject->context)->dm_pages,
2336 page_idx,
2337 DIV_ROUND_UP(act_size, PAGE_SIZE));
2338
2339 kfree(dm);
2340
2341 return 0;
2342}
2343
Leon Romanovsky21a428a2019-02-03 14:55:51 +02002344static int mlx5_ib_alloc_pd(struct ib_pd *ibpd, struct ib_ucontext *context,
2345 struct ib_udata *udata)
Eli Cohene126ba92013-07-07 17:25:49 +03002346{
Leon Romanovsky21a428a2019-02-03 14:55:51 +02002347 struct mlx5_ib_pd *pd = to_mpd(ibpd);
2348 struct ib_device *ibdev = ibpd->device;
Eli Cohene126ba92013-07-07 17:25:49 +03002349 struct mlx5_ib_alloc_pd_resp resp;
Eli Cohene126ba92013-07-07 17:25:49 +03002350 int err;
Yishai Hadasa1069c12018-09-20 21:39:19 +03002351 u32 out[MLX5_ST_SZ_DW(alloc_pd_out)] = {};
2352 u32 in[MLX5_ST_SZ_DW(alloc_pd_in)] = {};
2353 u16 uid = 0;
Eli Cohene126ba92013-07-07 17:25:49 +03002354
Yishai Hadas58895f02018-09-20 21:39:29 +03002355 uid = context ? to_mucontext(context)->devx_uid : 0;
Yishai Hadasa1069c12018-09-20 21:39:19 +03002356 MLX5_SET(alloc_pd_in, in, opcode, MLX5_CMD_OP_ALLOC_PD);
2357 MLX5_SET(alloc_pd_in, in, uid, uid);
2358 err = mlx5_cmd_exec(to_mdev(ibdev)->mdev, in, sizeof(in),
2359 out, sizeof(out));
Leon Romanovsky21a428a2019-02-03 14:55:51 +02002360 if (err)
2361 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03002362
Yishai Hadasa1069c12018-09-20 21:39:19 +03002363 pd->pdn = MLX5_GET(alloc_pd_out, out, pd);
2364 pd->uid = uid;
Eli Cohene126ba92013-07-07 17:25:49 +03002365 if (context) {
2366 resp.pdn = pd->pdn;
2367 if (ib_copy_to_udata(udata, &resp, sizeof(resp))) {
Yishai Hadasa1069c12018-09-20 21:39:19 +03002368 mlx5_cmd_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn, uid);
Leon Romanovsky21a428a2019-02-03 14:55:51 +02002369 return -EFAULT;
Eli Cohene126ba92013-07-07 17:25:49 +03002370 }
Eli Cohene126ba92013-07-07 17:25:49 +03002371 }
2372
Leon Romanovsky21a428a2019-02-03 14:55:51 +02002373 return 0;
Eli Cohene126ba92013-07-07 17:25:49 +03002374}
2375
Leon Romanovsky21a428a2019-02-03 14:55:51 +02002376static void mlx5_ib_dealloc_pd(struct ib_pd *pd)
Eli Cohene126ba92013-07-07 17:25:49 +03002377{
2378 struct mlx5_ib_dev *mdev = to_mdev(pd->device);
2379 struct mlx5_ib_pd *mpd = to_mpd(pd);
2380
Yishai Hadasa1069c12018-09-20 21:39:19 +03002381 mlx5_cmd_dealloc_pd(mdev->mdev, mpd->pdn, mpd->uid);
Eli Cohene126ba92013-07-07 17:25:49 +03002382}
2383
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002384enum {
2385 MATCH_CRITERIA_ENABLE_OUTER_BIT,
2386 MATCH_CRITERIA_ENABLE_MISC_BIT,
Ariel Levkovich71c6e862018-05-13 14:33:34 +03002387 MATCH_CRITERIA_ENABLE_INNER_BIT,
2388 MATCH_CRITERIA_ENABLE_MISC2_BIT
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002389};
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002390
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002391#define HEADER_IS_ZERO(match_criteria, headers) \
2392 !(memchr_inv(MLX5_ADDR_OF(fte_match_param, match_criteria, headers), \
2393 0, MLX5_FLD_SZ_BYTES(fte_match_param, headers))) \
2394
2395static u8 get_match_criteria_enable(u32 *match_criteria)
2396{
2397 u8 match_criteria_enable;
2398
2399 match_criteria_enable =
2400 (!HEADER_IS_ZERO(match_criteria, outer_headers)) <<
2401 MATCH_CRITERIA_ENABLE_OUTER_BIT;
2402 match_criteria_enable |=
2403 (!HEADER_IS_ZERO(match_criteria, misc_parameters)) <<
2404 MATCH_CRITERIA_ENABLE_MISC_BIT;
2405 match_criteria_enable |=
2406 (!HEADER_IS_ZERO(match_criteria, inner_headers)) <<
2407 MATCH_CRITERIA_ENABLE_INNER_BIT;
Ariel Levkovich71c6e862018-05-13 14:33:34 +03002408 match_criteria_enable |=
2409 (!HEADER_IS_ZERO(match_criteria, misc_parameters_2)) <<
2410 MATCH_CRITERIA_ENABLE_MISC2_BIT;
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002411
2412 return match_criteria_enable;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002413}
2414
Maor Gottlieb6113cc42019-01-17 20:08:15 +02002415static int set_proto(void *outer_c, void *outer_v, u8 mask, u8 val)
Maor Gottliebca0d4752016-08-30 16:58:35 +03002416{
Maor Gottlieb6113cc42019-01-17 20:08:15 +02002417 u8 entry_mask;
2418 u8 entry_val;
2419 int err = 0;
2420
2421 if (!mask)
2422 goto out;
2423
2424 entry_mask = MLX5_GET(fte_match_set_lyr_2_4, outer_c,
2425 ip_protocol);
2426 entry_val = MLX5_GET(fte_match_set_lyr_2_4, outer_v,
2427 ip_protocol);
2428 if (!entry_mask) {
2429 MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_protocol, mask);
2430 MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_protocol, val);
2431 goto out;
2432 }
2433 /* Don't override existing ip protocol */
2434 if (mask != entry_mask || val != entry_val)
2435 err = -EINVAL;
2436out:
2437 return err;
Maor Gottliebca0d4752016-08-30 16:58:35 +03002438}
2439
Daria Velikovsky37da2a02018-05-07 10:20:02 +03002440static void set_flow_label(void *misc_c, void *misc_v, u32 mask, u32 val,
Moses Reuben2d1e6972016-11-14 19:04:52 +02002441 bool inner)
2442{
2443 if (inner) {
2444 MLX5_SET(fte_match_set_misc,
2445 misc_c, inner_ipv6_flow_label, mask);
2446 MLX5_SET(fte_match_set_misc,
2447 misc_v, inner_ipv6_flow_label, val);
2448 } else {
2449 MLX5_SET(fte_match_set_misc,
2450 misc_c, outer_ipv6_flow_label, mask);
2451 MLX5_SET(fte_match_set_misc,
2452 misc_v, outer_ipv6_flow_label, val);
2453 }
2454}
2455
Maor Gottliebca0d4752016-08-30 16:58:35 +03002456static void set_tos(void *outer_c, void *outer_v, u8 mask, u8 val)
2457{
2458 MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_ecn, mask);
2459 MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_ecn, val);
2460 MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_dscp, mask >> 2);
2461 MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_dscp, val >> 2);
2462}
2463
Ariel Levkovich71c6e862018-05-13 14:33:34 +03002464static int check_mpls_supp_fields(u32 field_support, const __be32 *set_mask)
2465{
2466 if (MLX5_GET(fte_match_mpls, set_mask, mpls_label) &&
2467 !(field_support & MLX5_FIELD_SUPPORT_MPLS_LABEL))
2468 return -EOPNOTSUPP;
2469
2470 if (MLX5_GET(fte_match_mpls, set_mask, mpls_exp) &&
2471 !(field_support & MLX5_FIELD_SUPPORT_MPLS_EXP))
2472 return -EOPNOTSUPP;
2473
2474 if (MLX5_GET(fte_match_mpls, set_mask, mpls_s_bos) &&
2475 !(field_support & MLX5_FIELD_SUPPORT_MPLS_S_BOS))
2476 return -EOPNOTSUPP;
2477
2478 if (MLX5_GET(fte_match_mpls, set_mask, mpls_ttl) &&
2479 !(field_support & MLX5_FIELD_SUPPORT_MPLS_TTL))
2480 return -EOPNOTSUPP;
2481
2482 return 0;
2483}
2484
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002485#define LAST_ETH_FIELD vlan_tag
2486#define LAST_IB_FIELD sl
Maor Gottliebca0d4752016-08-30 16:58:35 +03002487#define LAST_IPV4_FIELD tos
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002488#define LAST_IPV6_FIELD traffic_class
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002489#define LAST_TCP_UDP_FIELD src_port
Moses Reubenffb30d82016-11-14 19:04:50 +02002490#define LAST_TUNNEL_FIELD tunnel_id
Moses Reuben2ac693f2017-01-18 14:59:50 +02002491#define LAST_FLOW_TAG_FIELD tag_id
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002492#define LAST_DROP_FIELD size
Raed Salem3b3233f2018-05-31 16:43:39 +03002493#define LAST_COUNTERS_FIELD counters
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002494
2495/* Field is the last supported field */
2496#define FIELDS_NOT_SUPPORTED(filter, field)\
2497 memchr_inv((void *)&filter.field +\
2498 sizeof(filter.field), 0,\
2499 sizeof(filter) -\
2500 offsetof(typeof(filter), field) -\
2501 sizeof(filter.field))
2502
Mark Bloch2ea26202018-09-06 17:27:03 +03002503int parse_flow_flow_action(struct mlx5_ib_flow_action *maction,
2504 bool is_egress,
2505 struct mlx5_flow_act *action)
Aviad Yehezkel802c2122018-03-28 09:27:53 +03002506{
Aviad Yehezkel802c2122018-03-28 09:27:53 +03002507
2508 switch (maction->ib_action.type) {
2509 case IB_FLOW_ACTION_ESP:
Mark Bloch501f14e2018-09-06 17:27:04 +03002510 if (action->action & (MLX5_FLOW_CONTEXT_ACTION_ENCRYPT |
2511 MLX5_FLOW_CONTEXT_ACTION_DECRYPT))
2512 return -EINVAL;
Aviad Yehezkel802c2122018-03-28 09:27:53 +03002513 /* Currently only AES_GCM keymat is supported by the driver */
2514 action->esp_id = (uintptr_t)maction->esp_aes_gcm.ctx;
Mark Bloch2ea26202018-09-06 17:27:03 +03002515 action->action |= is_egress ?
Aviad Yehezkel802c2122018-03-28 09:27:53 +03002516 MLX5_FLOW_CONTEXT_ACTION_ENCRYPT :
2517 MLX5_FLOW_CONTEXT_ACTION_DECRYPT;
2518 return 0;
Mark Blochb1085be2018-09-02 12:51:32 +03002519 case IB_FLOW_ACTION_UNSPECIFIED:
2520 if (maction->flow_action_raw.sub_type ==
2521 MLX5_IB_FLOW_ACTION_MODIFY_HEADER) {
Mark Bloch501f14e2018-09-06 17:27:04 +03002522 if (action->action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR)
2523 return -EINVAL;
Mark Blochb1085be2018-09-02 12:51:32 +03002524 action->action |= MLX5_FLOW_CONTEXT_ACTION_MOD_HDR;
2525 action->modify_id = maction->flow_action_raw.action_id;
2526 return 0;
2527 }
Mark Bloch10a30892018-09-02 12:51:34 +03002528 if (maction->flow_action_raw.sub_type ==
2529 MLX5_IB_FLOW_ACTION_DECAP) {
Mark Bloch501f14e2018-09-06 17:27:04 +03002530 if (action->action & MLX5_FLOW_CONTEXT_ACTION_DECAP)
2531 return -EINVAL;
Mark Bloch10a30892018-09-02 12:51:34 +03002532 action->action |= MLX5_FLOW_CONTEXT_ACTION_DECAP;
2533 return 0;
2534 }
Mark Bloche806f932018-09-02 12:51:36 +03002535 if (maction->flow_action_raw.sub_type ==
2536 MLX5_IB_FLOW_ACTION_PACKET_REFORMAT) {
Mark Bloch501f14e2018-09-06 17:27:04 +03002537 if (action->action &
2538 MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT)
2539 return -EINVAL;
Mark Bloche806f932018-09-02 12:51:36 +03002540 action->action |=
2541 MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT;
2542 action->reformat_id =
2543 maction->flow_action_raw.action_id;
2544 return 0;
2545 }
Mark Blochb1085be2018-09-02 12:51:32 +03002546 /* fall through */
Aviad Yehezkel802c2122018-03-28 09:27:53 +03002547 default:
2548 return -EOPNOTSUPP;
2549 }
2550}
2551
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002552static int parse_flow_attr(struct mlx5_core_dev *mdev, u32 *match_c,
2553 u32 *match_v, const union ib_flow_spec *ib_spec,
Aviad Yehezkel802c2122018-03-28 09:27:53 +03002554 const struct ib_flow_attr *flow_attr,
Ariel Levkovich71c6e862018-05-13 14:33:34 +03002555 struct mlx5_flow_act *action, u32 prev_type)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002556{
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002557 void *misc_params_c = MLX5_ADDR_OF(fte_match_param, match_c,
2558 misc_parameters);
2559 void *misc_params_v = MLX5_ADDR_OF(fte_match_param, match_v,
2560 misc_parameters);
Ariel Levkovich71c6e862018-05-13 14:33:34 +03002561 void *misc_params2_c = MLX5_ADDR_OF(fte_match_param, match_c,
2562 misc_parameters_2);
2563 void *misc_params2_v = MLX5_ADDR_OF(fte_match_param, match_v,
2564 misc_parameters_2);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002565 void *headers_c;
2566 void *headers_v;
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002567 int match_ipv;
Aviad Yehezkel802c2122018-03-28 09:27:53 +03002568 int ret;
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002569
Moses Reuben2d1e6972016-11-14 19:04:52 +02002570 if (ib_spec->type & IB_FLOW_SPEC_INNER) {
2571 headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
2572 inner_headers);
2573 headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
2574 inner_headers);
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002575 match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2576 ft_field_support.inner_ip_version);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002577 } else {
2578 headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
2579 outer_headers);
2580 headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
2581 outer_headers);
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002582 match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2583 ft_field_support.outer_ip_version);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002584 }
2585
2586 switch (ib_spec->type & ~IB_FLOW_SPEC_INNER) {
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002587 case IB_FLOW_SPEC_ETH:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002588 if (FIELDS_NOT_SUPPORTED(ib_spec->eth.mask, LAST_ETH_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002589 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002590
Moses Reuben2d1e6972016-11-14 19:04:52 +02002591 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002592 dmac_47_16),
2593 ib_spec->eth.mask.dst_mac);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002594 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002595 dmac_47_16),
2596 ib_spec->eth.val.dst_mac);
2597
Moses Reuben2d1e6972016-11-14 19:04:52 +02002598 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottliebee3da802016-09-12 19:16:24 +03002599 smac_47_16),
2600 ib_spec->eth.mask.src_mac);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002601 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottliebee3da802016-09-12 19:16:24 +03002602 smac_47_16),
2603 ib_spec->eth.val.src_mac);
2604
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002605 if (ib_spec->eth.mask.vlan_tag) {
Moses Reuben2d1e6972016-11-14 19:04:52 +02002606 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Mohamad Haj Yahia10543362016-10-09 16:25:43 +03002607 cvlan_tag, 1);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002608 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Mohamad Haj Yahia10543362016-10-09 16:25:43 +03002609 cvlan_tag, 1);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002610
Moses Reuben2d1e6972016-11-14 19:04:52 +02002611 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002612 first_vid, ntohs(ib_spec->eth.mask.vlan_tag));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002613 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002614 first_vid, ntohs(ib_spec->eth.val.vlan_tag));
2615
Moses Reuben2d1e6972016-11-14 19:04:52 +02002616 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002617 first_cfi,
2618 ntohs(ib_spec->eth.mask.vlan_tag) >> 12);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002619 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002620 first_cfi,
2621 ntohs(ib_spec->eth.val.vlan_tag) >> 12);
2622
Moses Reuben2d1e6972016-11-14 19:04:52 +02002623 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002624 first_prio,
2625 ntohs(ib_spec->eth.mask.vlan_tag) >> 13);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002626 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002627 first_prio,
2628 ntohs(ib_spec->eth.val.vlan_tag) >> 13);
2629 }
Moses Reuben2d1e6972016-11-14 19:04:52 +02002630 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002631 ethertype, ntohs(ib_spec->eth.mask.ether_type));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002632 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002633 ethertype, ntohs(ib_spec->eth.val.ether_type));
2634 break;
2635 case IB_FLOW_SPEC_IPV4:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002636 if (FIELDS_NOT_SUPPORTED(ib_spec->ipv4.mask, LAST_IPV4_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002637 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002638
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002639 if (match_ipv) {
2640 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2641 ip_version, 0xf);
2642 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Boris Pismenny3346c482017-08-20 15:13:08 +03002643 ip_version, MLX5_FS_IPV4_VERSION);
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002644 } else {
2645 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2646 ethertype, 0xffff);
2647 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2648 ethertype, ETH_P_IP);
2649 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002650
Moses Reuben2d1e6972016-11-14 19:04:52 +02002651 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002652 src_ipv4_src_ipv6.ipv4_layout.ipv4),
2653 &ib_spec->ipv4.mask.src_ip,
2654 sizeof(ib_spec->ipv4.mask.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002655 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002656 src_ipv4_src_ipv6.ipv4_layout.ipv4),
2657 &ib_spec->ipv4.val.src_ip,
2658 sizeof(ib_spec->ipv4.val.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002659 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002660 dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2661 &ib_spec->ipv4.mask.dst_ip,
2662 sizeof(ib_spec->ipv4.mask.dst_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002663 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002664 dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2665 &ib_spec->ipv4.val.dst_ip,
2666 sizeof(ib_spec->ipv4.val.dst_ip));
Maor Gottliebca0d4752016-08-30 16:58:35 +03002667
Moses Reuben2d1e6972016-11-14 19:04:52 +02002668 set_tos(headers_c, headers_v,
Maor Gottliebca0d4752016-08-30 16:58:35 +03002669 ib_spec->ipv4.mask.tos, ib_spec->ipv4.val.tos);
2670
Maor Gottlieb6113cc42019-01-17 20:08:15 +02002671 if (set_proto(headers_c, headers_v,
2672 ib_spec->ipv4.mask.proto,
2673 ib_spec->ipv4.val.proto))
2674 return -EINVAL;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002675 break;
Maor Gottlieb026bae02016-06-17 15:14:51 +03002676 case IB_FLOW_SPEC_IPV6:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002677 if (FIELDS_NOT_SUPPORTED(ib_spec->ipv6.mask, LAST_IPV6_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002678 return -EOPNOTSUPP;
Maor Gottlieb026bae02016-06-17 15:14:51 +03002679
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002680 if (match_ipv) {
2681 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2682 ip_version, 0xf);
2683 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Boris Pismenny3346c482017-08-20 15:13:08 +03002684 ip_version, MLX5_FS_IPV6_VERSION);
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002685 } else {
2686 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2687 ethertype, 0xffff);
2688 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2689 ethertype, ETH_P_IPV6);
2690 }
Maor Gottlieb026bae02016-06-17 15:14:51 +03002691
Moses Reuben2d1e6972016-11-14 19:04:52 +02002692 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb026bae02016-06-17 15:14:51 +03002693 src_ipv4_src_ipv6.ipv6_layout.ipv6),
2694 &ib_spec->ipv6.mask.src_ip,
2695 sizeof(ib_spec->ipv6.mask.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002696 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb026bae02016-06-17 15:14:51 +03002697 src_ipv4_src_ipv6.ipv6_layout.ipv6),
2698 &ib_spec->ipv6.val.src_ip,
2699 sizeof(ib_spec->ipv6.val.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002700 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb026bae02016-06-17 15:14:51 +03002701 dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2702 &ib_spec->ipv6.mask.dst_ip,
2703 sizeof(ib_spec->ipv6.mask.dst_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002704 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb026bae02016-06-17 15:14:51 +03002705 dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2706 &ib_spec->ipv6.val.dst_ip,
2707 sizeof(ib_spec->ipv6.val.dst_ip));
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002708
Moses Reuben2d1e6972016-11-14 19:04:52 +02002709 set_tos(headers_c, headers_v,
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002710 ib_spec->ipv6.mask.traffic_class,
2711 ib_spec->ipv6.val.traffic_class);
2712
Maor Gottlieb6113cc42019-01-17 20:08:15 +02002713 if (set_proto(headers_c, headers_v,
2714 ib_spec->ipv6.mask.next_hdr,
2715 ib_spec->ipv6.val.next_hdr))
2716 return -EINVAL;
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002717
Moses Reuben2d1e6972016-11-14 19:04:52 +02002718 set_flow_label(misc_params_c, misc_params_v,
2719 ntohl(ib_spec->ipv6.mask.flow_label),
2720 ntohl(ib_spec->ipv6.val.flow_label),
2721 ib_spec->type & IB_FLOW_SPEC_INNER);
Aviad Yehezkel802c2122018-03-28 09:27:53 +03002722 break;
2723 case IB_FLOW_SPEC_ESP:
2724 if (ib_spec->esp.mask.seq)
2725 return -EOPNOTSUPP;
Moses Reuben2d1e6972016-11-14 19:04:52 +02002726
Aviad Yehezkel802c2122018-03-28 09:27:53 +03002727 MLX5_SET(fte_match_set_misc, misc_params_c, outer_esp_spi,
2728 ntohl(ib_spec->esp.mask.spi));
2729 MLX5_SET(fte_match_set_misc, misc_params_v, outer_esp_spi,
2730 ntohl(ib_spec->esp.val.spi));
Maor Gottlieb026bae02016-06-17 15:14:51 +03002731 break;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002732 case IB_FLOW_SPEC_TCP:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002733 if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
2734 LAST_TCP_UDP_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002735 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002736
Maor Gottlieb6113cc42019-01-17 20:08:15 +02002737 if (set_proto(headers_c, headers_v, 0xff, IPPROTO_TCP))
2738 return -EINVAL;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002739
Moses Reuben2d1e6972016-11-14 19:04:52 +02002740 MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002741 ntohs(ib_spec->tcp_udp.mask.src_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002742 MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002743 ntohs(ib_spec->tcp_udp.val.src_port));
2744
Moses Reuben2d1e6972016-11-14 19:04:52 +02002745 MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002746 ntohs(ib_spec->tcp_udp.mask.dst_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002747 MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002748 ntohs(ib_spec->tcp_udp.val.dst_port));
2749 break;
2750 case IB_FLOW_SPEC_UDP:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002751 if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
2752 LAST_TCP_UDP_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002753 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002754
Maor Gottlieb6113cc42019-01-17 20:08:15 +02002755 if (set_proto(headers_c, headers_v, 0xff, IPPROTO_UDP))
2756 return -EINVAL;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002757
Moses Reuben2d1e6972016-11-14 19:04:52 +02002758 MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002759 ntohs(ib_spec->tcp_udp.mask.src_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002760 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002761 ntohs(ib_spec->tcp_udp.val.src_port));
2762
Moses Reuben2d1e6972016-11-14 19:04:52 +02002763 MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002764 ntohs(ib_spec->tcp_udp.mask.dst_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002765 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002766 ntohs(ib_spec->tcp_udp.val.dst_port));
2767 break;
Ariel Levkovichda2f22a2018-05-13 14:33:33 +03002768 case IB_FLOW_SPEC_GRE:
2769 if (ib_spec->gre.mask.c_ks_res0_ver)
2770 return -EOPNOTSUPP;
2771
Maor Gottlieb6113cc42019-01-17 20:08:15 +02002772 if (set_proto(headers_c, headers_v, 0xff, IPPROTO_GRE))
2773 return -EINVAL;
2774
Ariel Levkovichda2f22a2018-05-13 14:33:33 +03002775 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
2776 0xff);
2777 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
2778 IPPROTO_GRE);
2779
2780 MLX5_SET(fte_match_set_misc, misc_params_c, gre_protocol,
Maor Gottlieba93b6322018-07-01 15:50:17 +03002781 ntohs(ib_spec->gre.mask.protocol));
Ariel Levkovichda2f22a2018-05-13 14:33:33 +03002782 MLX5_SET(fte_match_set_misc, misc_params_v, gre_protocol,
2783 ntohs(ib_spec->gre.val.protocol));
2784
2785 memcpy(MLX5_ADDR_OF(fte_match_set_misc, misc_params_c,
Oz Shlomo5886a962018-12-10 13:15:13 -08002786 gre_key.nvgre.hi),
Ariel Levkovichda2f22a2018-05-13 14:33:33 +03002787 &ib_spec->gre.mask.key,
2788 sizeof(ib_spec->gre.mask.key));
2789 memcpy(MLX5_ADDR_OF(fte_match_set_misc, misc_params_v,
Oz Shlomo5886a962018-12-10 13:15:13 -08002790 gre_key.nvgre.hi),
Ariel Levkovichda2f22a2018-05-13 14:33:33 +03002791 &ib_spec->gre.val.key,
2792 sizeof(ib_spec->gre.val.key));
2793 break;
Ariel Levkovich71c6e862018-05-13 14:33:34 +03002794 case IB_FLOW_SPEC_MPLS:
2795 switch (prev_type) {
2796 case IB_FLOW_SPEC_UDP:
2797 if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2798 ft_field_support.outer_first_mpls_over_udp),
2799 &ib_spec->mpls.mask.tag))
2800 return -EOPNOTSUPP;
2801
2802 memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
2803 outer_first_mpls_over_udp),
2804 &ib_spec->mpls.val.tag,
2805 sizeof(ib_spec->mpls.val.tag));
2806 memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
2807 outer_first_mpls_over_udp),
2808 &ib_spec->mpls.mask.tag,
2809 sizeof(ib_spec->mpls.mask.tag));
2810 break;
2811 case IB_FLOW_SPEC_GRE:
2812 if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2813 ft_field_support.outer_first_mpls_over_gre),
2814 &ib_spec->mpls.mask.tag))
2815 return -EOPNOTSUPP;
2816
2817 memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
2818 outer_first_mpls_over_gre),
2819 &ib_spec->mpls.val.tag,
2820 sizeof(ib_spec->mpls.val.tag));
2821 memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
2822 outer_first_mpls_over_gre),
2823 &ib_spec->mpls.mask.tag,
2824 sizeof(ib_spec->mpls.mask.tag));
2825 break;
2826 default:
2827 if (ib_spec->type & IB_FLOW_SPEC_INNER) {
2828 if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2829 ft_field_support.inner_first_mpls),
2830 &ib_spec->mpls.mask.tag))
2831 return -EOPNOTSUPP;
2832
2833 memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
2834 inner_first_mpls),
2835 &ib_spec->mpls.val.tag,
2836 sizeof(ib_spec->mpls.val.tag));
2837 memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
2838 inner_first_mpls),
2839 &ib_spec->mpls.mask.tag,
2840 sizeof(ib_spec->mpls.mask.tag));
2841 } else {
2842 if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2843 ft_field_support.outer_first_mpls),
2844 &ib_spec->mpls.mask.tag))
2845 return -EOPNOTSUPP;
2846
2847 memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
2848 outer_first_mpls),
2849 &ib_spec->mpls.val.tag,
2850 sizeof(ib_spec->mpls.val.tag));
2851 memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
2852 outer_first_mpls),
2853 &ib_spec->mpls.mask.tag,
2854 sizeof(ib_spec->mpls.mask.tag));
2855 }
2856 }
2857 break;
Moses Reubenffb30d82016-11-14 19:04:50 +02002858 case IB_FLOW_SPEC_VXLAN_TUNNEL:
2859 if (FIELDS_NOT_SUPPORTED(ib_spec->tunnel.mask,
2860 LAST_TUNNEL_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002861 return -EOPNOTSUPP;
Moses Reubenffb30d82016-11-14 19:04:50 +02002862
2863 MLX5_SET(fte_match_set_misc, misc_params_c, vxlan_vni,
2864 ntohl(ib_spec->tunnel.mask.tunnel_id));
2865 MLX5_SET(fte_match_set_misc, misc_params_v, vxlan_vni,
2866 ntohl(ib_spec->tunnel.val.tunnel_id));
2867 break;
Moses Reuben2ac693f2017-01-18 14:59:50 +02002868 case IB_FLOW_SPEC_ACTION_TAG:
2869 if (FIELDS_NOT_SUPPORTED(ib_spec->flow_tag,
2870 LAST_FLOW_TAG_FIELD))
2871 return -EOPNOTSUPP;
2872 if (ib_spec->flow_tag.tag_id >= BIT(24))
2873 return -EINVAL;
2874
Boris Pismenny075572d2017-08-16 09:33:30 +03002875 action->flow_tag = ib_spec->flow_tag.tag_id;
Paul Blakeyd5634fe2018-09-20 12:17:48 +02002876 action->flags |= FLOW_ACT_HAS_TAG;
Moses Reuben2ac693f2017-01-18 14:59:50 +02002877 break;
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002878 case IB_FLOW_SPEC_ACTION_DROP:
2879 if (FIELDS_NOT_SUPPORTED(ib_spec->drop,
2880 LAST_DROP_FIELD))
2881 return -EOPNOTSUPP;
Boris Pismenny075572d2017-08-16 09:33:30 +03002882 action->action |= MLX5_FLOW_CONTEXT_ACTION_DROP;
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002883 break;
Aviad Yehezkel802c2122018-03-28 09:27:53 +03002884 case IB_FLOW_SPEC_ACTION_HANDLE:
Mark Bloch2ea26202018-09-06 17:27:03 +03002885 ret = parse_flow_flow_action(to_mflow_act(ib_spec->action.act),
2886 flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS, action);
Aviad Yehezkel802c2122018-03-28 09:27:53 +03002887 if (ret)
2888 return ret;
2889 break;
Raed Salem3b3233f2018-05-31 16:43:39 +03002890 case IB_FLOW_SPEC_ACTION_COUNT:
2891 if (FIELDS_NOT_SUPPORTED(ib_spec->flow_count,
2892 LAST_COUNTERS_FIELD))
2893 return -EOPNOTSUPP;
2894
2895 /* for now support only one counters spec per flow */
2896 if (action->action & MLX5_FLOW_CONTEXT_ACTION_COUNT)
2897 return -EINVAL;
2898
2899 action->counters = ib_spec->flow_count.counters;
2900 action->action |= MLX5_FLOW_CONTEXT_ACTION_COUNT;
2901 break;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002902 default:
2903 return -EINVAL;
2904 }
2905
2906 return 0;
2907}
2908
2909/* If a flow could catch both multicast and unicast packets,
2910 * it won't fall into the multicast flow steering table and this rule
2911 * could steal other multicast packets.
2912 */
Yishai Hadasa550ddf2017-08-17 15:52:33 +03002913static bool flow_is_multicast_only(const struct ib_flow_attr *ib_attr)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002914{
Yishai Hadas81e30882017-06-08 16:15:09 +03002915 union ib_flow_spec *flow_spec;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002916
2917 if (ib_attr->type != IB_FLOW_ATTR_NORMAL ||
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002918 ib_attr->num_of_specs < 1)
2919 return false;
2920
Yishai Hadas81e30882017-06-08 16:15:09 +03002921 flow_spec = (union ib_flow_spec *)(ib_attr + 1);
2922 if (flow_spec->type == IB_FLOW_SPEC_IPV4) {
2923 struct ib_flow_spec_ipv4 *ipv4_spec;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002924
Yishai Hadas81e30882017-06-08 16:15:09 +03002925 ipv4_spec = (struct ib_flow_spec_ipv4 *)flow_spec;
2926 if (ipv4_is_multicast(ipv4_spec->val.dst_ip))
2927 return true;
2928
2929 return false;
2930 }
2931
2932 if (flow_spec->type == IB_FLOW_SPEC_ETH) {
2933 struct ib_flow_spec_eth *eth_spec;
2934
2935 eth_spec = (struct ib_flow_spec_eth *)flow_spec;
2936 return is_multicast_ether_addr(eth_spec->mask.dst_mac) &&
2937 is_multicast_ether_addr(eth_spec->val.dst_mac);
2938 }
2939
2940 return false;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002941}
2942
Aviad Yehezkel802c2122018-03-28 09:27:53 +03002943enum valid_spec {
2944 VALID_SPEC_INVALID,
2945 VALID_SPEC_VALID,
2946 VALID_SPEC_NA,
2947};
2948
2949static enum valid_spec
2950is_valid_esp_aes_gcm(struct mlx5_core_dev *mdev,
2951 const struct mlx5_flow_spec *spec,
2952 const struct mlx5_flow_act *flow_act,
2953 bool egress)
2954{
2955 const u32 *match_c = spec->match_criteria;
2956 bool is_crypto =
2957 (flow_act->action & (MLX5_FLOW_CONTEXT_ACTION_ENCRYPT |
2958 MLX5_FLOW_CONTEXT_ACTION_DECRYPT));
2959 bool is_ipsec = mlx5_fs_is_ipsec_flow(match_c);
2960 bool is_drop = flow_act->action & MLX5_FLOW_CONTEXT_ACTION_DROP;
2961
2962 /*
2963 * Currently only crypto is supported in egress, when regular egress
2964 * rules would be supported, always return VALID_SPEC_NA.
2965 */
2966 if (!is_crypto)
Mark Bloch78dd0c42018-09-02 12:51:31 +03002967 return VALID_SPEC_NA;
Aviad Yehezkel802c2122018-03-28 09:27:53 +03002968
2969 return is_crypto && is_ipsec &&
Paul Blakeyd5634fe2018-09-20 12:17:48 +02002970 (!egress || (!is_drop && !(flow_act->flags & FLOW_ACT_HAS_TAG))) ?
Aviad Yehezkel802c2122018-03-28 09:27:53 +03002971 VALID_SPEC_VALID : VALID_SPEC_INVALID;
2972}
2973
2974static bool is_valid_spec(struct mlx5_core_dev *mdev,
2975 const struct mlx5_flow_spec *spec,
2976 const struct mlx5_flow_act *flow_act,
2977 bool egress)
2978{
2979 /* We curretly only support ipsec egress flow */
2980 return is_valid_esp_aes_gcm(mdev, spec, flow_act, egress) != VALID_SPEC_INVALID;
2981}
2982
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002983static bool is_valid_ethertype(struct mlx5_core_dev *mdev,
2984 const struct ib_flow_attr *flow_attr,
Ariel Levkovich0f750962017-04-03 13:11:02 +03002985 bool check_inner)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002986{
2987 union ib_flow_spec *ib_spec = (union ib_flow_spec *)(flow_attr + 1);
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002988 int match_ipv = check_inner ?
2989 MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2990 ft_field_support.inner_ip_version) :
2991 MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2992 ft_field_support.outer_ip_version);
Ariel Levkovich0f750962017-04-03 13:11:02 +03002993 int inner_bit = check_inner ? IB_FLOW_SPEC_INNER : 0;
2994 bool ipv4_spec_valid, ipv6_spec_valid;
2995 unsigned int ip_spec_type = 0;
2996 bool has_ethertype = false;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002997 unsigned int spec_index;
Ariel Levkovich0f750962017-04-03 13:11:02 +03002998 bool mask_valid = true;
2999 u16 eth_type = 0;
3000 bool type_valid;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003001
3002 /* Validate that ethertype is correct */
3003 for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
Ariel Levkovich0f750962017-04-03 13:11:02 +03003004 if ((ib_spec->type == (IB_FLOW_SPEC_ETH | inner_bit)) &&
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003005 ib_spec->eth.mask.ether_type) {
Ariel Levkovich0f750962017-04-03 13:11:02 +03003006 mask_valid = (ib_spec->eth.mask.ether_type ==
3007 htons(0xffff));
3008 has_ethertype = true;
3009 eth_type = ntohs(ib_spec->eth.val.ether_type);
3010 } else if ((ib_spec->type == (IB_FLOW_SPEC_IPV4 | inner_bit)) ||
3011 (ib_spec->type == (IB_FLOW_SPEC_IPV6 | inner_bit))) {
3012 ip_spec_type = ib_spec->type;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003013 }
3014 ib_spec = (void *)ib_spec + ib_spec->size;
3015 }
Ariel Levkovich0f750962017-04-03 13:11:02 +03003016
3017 type_valid = (!has_ethertype) || (!ip_spec_type);
3018 if (!type_valid && mask_valid) {
3019 ipv4_spec_valid = (eth_type == ETH_P_IP) &&
3020 (ip_spec_type == (IB_FLOW_SPEC_IPV4 | inner_bit));
3021 ipv6_spec_valid = (eth_type == ETH_P_IPV6) &&
3022 (ip_spec_type == (IB_FLOW_SPEC_IPV6 | inner_bit));
Ariel Levkovich19cc7522017-04-03 13:11:03 +03003023
3024 type_valid = (ipv4_spec_valid) || (ipv6_spec_valid) ||
3025 (((eth_type == ETH_P_MPLS_UC) ||
3026 (eth_type == ETH_P_MPLS_MC)) && match_ipv);
Ariel Levkovich0f750962017-04-03 13:11:02 +03003027 }
3028
3029 return type_valid;
3030}
3031
Ariel Levkovich19cc7522017-04-03 13:11:03 +03003032static bool is_valid_attr(struct mlx5_core_dev *mdev,
3033 const struct ib_flow_attr *flow_attr)
Ariel Levkovich0f750962017-04-03 13:11:02 +03003034{
Ariel Levkovich19cc7522017-04-03 13:11:03 +03003035 return is_valid_ethertype(mdev, flow_attr, false) &&
3036 is_valid_ethertype(mdev, flow_attr, true);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003037}
3038
3039static void put_flow_table(struct mlx5_ib_dev *dev,
3040 struct mlx5_ib_flow_prio *prio, bool ft_added)
3041{
3042 prio->refcount -= !!ft_added;
3043 if (!prio->refcount) {
3044 mlx5_destroy_flow_table(prio->flow_table);
3045 prio->flow_table = NULL;
3046 }
3047}
3048
Raed Salem3b3233f2018-05-31 16:43:39 +03003049static void counters_clear_description(struct ib_counters *counters)
3050{
3051 struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
3052
3053 mutex_lock(&mcounters->mcntrs_mutex);
3054 kfree(mcounters->counters_data);
3055 mcounters->counters_data = NULL;
3056 mcounters->cntrs_max_index = 0;
3057 mutex_unlock(&mcounters->mcntrs_mutex);
3058}
3059
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003060static int mlx5_ib_destroy_flow(struct ib_flow *flow_id)
3061{
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003062 struct mlx5_ib_flow_handler *handler = container_of(flow_id,
3063 struct mlx5_ib_flow_handler,
3064 ibflow);
3065 struct mlx5_ib_flow_handler *iter, *tmp;
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003066 struct mlx5_ib_dev *dev = handler->dev;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003067
Mark Bloch9a4ca382018-01-16 14:42:35 +00003068 mutex_lock(&dev->flow_db->lock);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003069
3070 list_for_each_entry_safe(iter, tmp, &handler->list, list) {
Mark Bloch74491de2016-08-31 11:24:25 +00003071 mlx5_del_flow_rules(iter->rule);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003072 put_flow_table(dev, iter->prio, true);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003073 list_del(&iter->list);
3074 kfree(iter);
3075 }
3076
Mark Bloch74491de2016-08-31 11:24:25 +00003077 mlx5_del_flow_rules(handler->rule);
Maor Gottlieb5497adc2016-08-28 14:16:31 +03003078 put_flow_table(dev, handler->prio, true);
Raed Salem3b3233f2018-05-31 16:43:39 +03003079 if (handler->ibcounters &&
3080 atomic_read(&handler->ibcounters->usecnt) == 1)
3081 counters_clear_description(handler->ibcounters);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003082
Raed Salem3b3233f2018-05-31 16:43:39 +03003083 mutex_unlock(&dev->flow_db->lock);
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003084 if (handler->flow_matcher)
3085 atomic_dec(&handler->flow_matcher->usecnt);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003086 kfree(handler);
3087
3088 return 0;
3089}
3090
Maor Gottlieb35d190112016-03-07 18:51:47 +02003091static int ib_prio_to_core_prio(unsigned int priority, bool dont_trap)
3092{
3093 priority *= 2;
3094 if (!dont_trap)
3095 priority++;
3096 return priority;
3097}
3098
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003099enum flow_table_type {
3100 MLX5_IB_FT_RX,
3101 MLX5_IB_FT_TX
3102};
3103
Maor Gottlieb00b7c2a2017-03-29 06:09:01 +03003104#define MLX5_FS_MAX_TYPES 6
3105#define MLX5_FS_MAX_ENTRIES BIT(16)
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003106
3107static struct mlx5_ib_flow_prio *_get_prio(struct mlx5_flow_namespace *ns,
3108 struct mlx5_ib_flow_prio *prio,
3109 int priority,
Mark Bloch4adda112018-09-02 12:51:33 +03003110 int num_entries, int num_groups,
3111 u32 flags)
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003112{
3113 struct mlx5_flow_table *ft;
3114
3115 ft = mlx5_create_auto_grouped_flow_table(ns, priority,
3116 num_entries,
3117 num_groups,
Mark Bloch4adda112018-09-02 12:51:33 +03003118 0, flags);
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003119 if (IS_ERR(ft))
3120 return ERR_CAST(ft);
3121
3122 prio->flow_table = ft;
3123 prio->refcount = 0;
3124 return prio;
3125}
3126
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003127static struct mlx5_ib_flow_prio *get_flow_table(struct mlx5_ib_dev *dev,
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003128 struct ib_flow_attr *flow_attr,
3129 enum flow_table_type ft_type)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003130{
Maor Gottlieb35d190112016-03-07 18:51:47 +02003131 bool dont_trap = flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003132 struct mlx5_flow_namespace *ns = NULL;
3133 struct mlx5_ib_flow_prio *prio;
3134 struct mlx5_flow_table *ft;
Maor Gottliebdac388e2017-03-29 06:09:00 +03003135 int max_table_size;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003136 int num_entries;
3137 int num_groups;
Mark Bloch4adda112018-09-02 12:51:33 +03003138 u32 flags = 0;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003139 int priority;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003140
Maor Gottliebdac388e2017-03-29 06:09:00 +03003141 max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
3142 log_max_ft_size));
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003143 if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
Mark Bloch78dd0c42018-09-02 12:51:31 +03003144 enum mlx5_flow_namespace_type fn_type;
3145
3146 if (flow_is_multicast_only(flow_attr) &&
3147 !dont_trap)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003148 priority = MLX5_IB_FLOW_MCAST_PRIO;
3149 else
Maor Gottlieb35d190112016-03-07 18:51:47 +02003150 priority = ib_prio_to_core_prio(flow_attr->priority,
3151 dont_trap);
Mark Bloch78dd0c42018-09-02 12:51:31 +03003152 if (ft_type == MLX5_IB_FT_RX) {
3153 fn_type = MLX5_FLOW_NAMESPACE_BYPASS;
3154 prio = &dev->flow_db->prios[priority];
Mark Bloch4adda112018-09-02 12:51:33 +03003155 if (!dev->rep &&
3156 MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, decap))
3157 flags |= MLX5_FLOW_TABLE_TUNNEL_EN_DECAP;
Mark Bloch5c2db532018-09-02 12:51:35 +03003158 if (!dev->rep &&
3159 MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
3160 reformat_l3_tunnel_to_l2))
3161 flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
Mark Bloch78dd0c42018-09-02 12:51:31 +03003162 } else {
3163 max_table_size =
3164 BIT(MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev,
3165 log_max_ft_size));
3166 fn_type = MLX5_FLOW_NAMESPACE_EGRESS;
3167 prio = &dev->flow_db->egress_prios[priority];
Mark Bloch4adda112018-09-02 12:51:33 +03003168 if (!dev->rep &&
3169 MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev, reformat))
3170 flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
Mark Bloch78dd0c42018-09-02 12:51:31 +03003171 }
3172 ns = mlx5_get_flow_namespace(dev->mdev, fn_type);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003173 num_entries = MLX5_FS_MAX_ENTRIES;
3174 num_groups = MLX5_FS_MAX_TYPES;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003175 } else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
3176 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
3177 ns = mlx5_get_flow_namespace(dev->mdev,
3178 MLX5_FLOW_NAMESPACE_LEFTOVERS);
3179 build_leftovers_ft_param(&priority,
3180 &num_entries,
3181 &num_groups);
Mark Bloch9a4ca382018-01-16 14:42:35 +00003182 prio = &dev->flow_db->prios[MLX5_IB_FLOW_LEFTOVERS_PRIO];
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003183 } else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
3184 if (!MLX5_CAP_FLOWTABLE(dev->mdev,
3185 allow_sniffer_and_nic_rx_shared_tir))
3186 return ERR_PTR(-ENOTSUPP);
3187
3188 ns = mlx5_get_flow_namespace(dev->mdev, ft_type == MLX5_IB_FT_RX ?
3189 MLX5_FLOW_NAMESPACE_SNIFFER_RX :
3190 MLX5_FLOW_NAMESPACE_SNIFFER_TX);
3191
Mark Bloch9a4ca382018-01-16 14:42:35 +00003192 prio = &dev->flow_db->sniffer[ft_type];
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003193 priority = 0;
3194 num_entries = 1;
3195 num_groups = 1;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003196 }
3197
3198 if (!ns)
3199 return ERR_PTR(-ENOTSUPP);
3200
Maor Gottliebdac388e2017-03-29 06:09:00 +03003201 if (num_entries > max_table_size)
3202 return ERR_PTR(-ENOMEM);
3203
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003204 ft = prio->flow_table;
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003205 if (!ft)
Mark Bloch4adda112018-09-02 12:51:33 +03003206 return _get_prio(ns, prio, priority, num_entries, num_groups,
3207 flags);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003208
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003209 return prio;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003210}
3211
Yishai Hadasa550ddf2017-08-17 15:52:33 +03003212static void set_underlay_qp(struct mlx5_ib_dev *dev,
3213 struct mlx5_flow_spec *spec,
3214 u32 underlay_qpn)
3215{
3216 void *misc_params_c = MLX5_ADDR_OF(fte_match_param,
3217 spec->match_criteria,
3218 misc_parameters);
3219 void *misc_params_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
3220 misc_parameters);
3221
3222 if (underlay_qpn &&
3223 MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
3224 ft_field_support.bth_dst_qp)) {
3225 MLX5_SET(fte_match_set_misc,
3226 misc_params_v, bth_dst_qp, underlay_qpn);
3227 MLX5_SET(fte_match_set_misc,
3228 misc_params_c, bth_dst_qp, 0xffffff);
3229 }
3230}
3231
Raed Salem5e95af52018-05-31 16:43:40 +03003232static int read_flow_counters(struct ib_device *ibdev,
3233 struct mlx5_read_counters_attr *read_attr)
3234{
3235 struct mlx5_fc *fc = read_attr->hw_cntrs_hndl;
3236 struct mlx5_ib_dev *dev = to_mdev(ibdev);
3237
3238 return mlx5_fc_query(dev->mdev, fc,
3239 &read_attr->out[IB_COUNTER_PACKETS],
3240 &read_attr->out[IB_COUNTER_BYTES]);
3241}
3242
3243/* flow counters currently expose two counters packets and bytes */
3244#define FLOW_COUNTERS_NUM 2
Raed Salem3b3233f2018-05-31 16:43:39 +03003245static int counters_set_description(struct ib_counters *counters,
3246 enum mlx5_ib_counters_type counters_type,
3247 struct mlx5_ib_flow_counters_desc *desc_data,
3248 u32 ncounters)
3249{
3250 struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
3251 u32 cntrs_max_index = 0;
3252 int i;
3253
3254 if (counters_type != MLX5_IB_COUNTERS_FLOW)
3255 return -EINVAL;
3256
3257 /* init the fields for the object */
3258 mcounters->type = counters_type;
Raed Salem5e95af52018-05-31 16:43:40 +03003259 mcounters->read_counters = read_flow_counters;
3260 mcounters->counters_num = FLOW_COUNTERS_NUM;
Raed Salem3b3233f2018-05-31 16:43:39 +03003261 mcounters->ncounters = ncounters;
3262 /* each counter entry have both description and index pair */
3263 for (i = 0; i < ncounters; i++) {
3264 if (desc_data[i].description > IB_COUNTER_BYTES)
3265 return -EINVAL;
3266
3267 if (cntrs_max_index <= desc_data[i].index)
3268 cntrs_max_index = desc_data[i].index + 1;
3269 }
3270
3271 mutex_lock(&mcounters->mcntrs_mutex);
3272 mcounters->counters_data = desc_data;
3273 mcounters->cntrs_max_index = cntrs_max_index;
3274 mutex_unlock(&mcounters->mcntrs_mutex);
3275
3276 return 0;
3277}
3278
3279#define MAX_COUNTERS_NUM (USHRT_MAX / (sizeof(u32) * 2))
3280static int flow_counters_set_data(struct ib_counters *ibcounters,
3281 struct mlx5_ib_create_flow *ucmd)
3282{
3283 struct mlx5_ib_mcounters *mcounters = to_mcounters(ibcounters);
3284 struct mlx5_ib_flow_counters_data *cntrs_data = NULL;
3285 struct mlx5_ib_flow_counters_desc *desc_data = NULL;
3286 bool hw_hndl = false;
3287 int ret = 0;
3288
3289 if (ucmd && ucmd->ncounters_data != 0) {
3290 cntrs_data = ucmd->data;
3291 if (cntrs_data->ncounters > MAX_COUNTERS_NUM)
3292 return -EINVAL;
3293
3294 desc_data = kcalloc(cntrs_data->ncounters,
3295 sizeof(*desc_data),
3296 GFP_KERNEL);
3297 if (!desc_data)
3298 return -ENOMEM;
3299
3300 if (copy_from_user(desc_data,
3301 u64_to_user_ptr(cntrs_data->counters_data),
3302 sizeof(*desc_data) * cntrs_data->ncounters)) {
3303 ret = -EFAULT;
3304 goto free;
3305 }
3306 }
3307
3308 if (!mcounters->hw_cntrs_hndl) {
3309 mcounters->hw_cntrs_hndl = mlx5_fc_create(
3310 to_mdev(ibcounters->device)->mdev, false);
weiyongjun (A)e31abf72018-06-07 01:47:41 +00003311 if (IS_ERR(mcounters->hw_cntrs_hndl)) {
3312 ret = PTR_ERR(mcounters->hw_cntrs_hndl);
Raed Salem3b3233f2018-05-31 16:43:39 +03003313 goto free;
3314 }
3315 hw_hndl = true;
3316 }
3317
3318 if (desc_data) {
3319 /* counters already bound to at least one flow */
3320 if (mcounters->cntrs_max_index) {
3321 ret = -EINVAL;
3322 goto free_hndl;
3323 }
3324
3325 ret = counters_set_description(ibcounters,
3326 MLX5_IB_COUNTERS_FLOW,
3327 desc_data,
3328 cntrs_data->ncounters);
3329 if (ret)
3330 goto free_hndl;
3331
3332 } else if (!mcounters->cntrs_max_index) {
3333 /* counters not bound yet, must have udata passed */
3334 ret = -EINVAL;
3335 goto free_hndl;
3336 }
3337
3338 return 0;
3339
3340free_hndl:
3341 if (hw_hndl) {
3342 mlx5_fc_destroy(to_mdev(ibcounters->device)->mdev,
3343 mcounters->hw_cntrs_hndl);
3344 mcounters->hw_cntrs_hndl = NULL;
3345 }
3346free:
3347 kfree(desc_data);
3348 return ret;
3349}
3350
Yishai Hadasa550ddf2017-08-17 15:52:33 +03003351static struct mlx5_ib_flow_handler *_create_flow_rule(struct mlx5_ib_dev *dev,
3352 struct mlx5_ib_flow_prio *ft_prio,
3353 const struct ib_flow_attr *flow_attr,
3354 struct mlx5_flow_destination *dst,
Raed Salem3b3233f2018-05-31 16:43:39 +03003355 u32 underlay_qpn,
3356 struct mlx5_ib_create_flow *ucmd)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003357{
3358 struct mlx5_flow_table *ft = ft_prio->flow_table;
3359 struct mlx5_ib_flow_handler *handler;
Boris Pismenny075572d2017-08-16 09:33:30 +03003360 struct mlx5_flow_act flow_act = {.flow_tag = MLX5_FS_DEFAULT_FLOW_TAG};
Maor Gottliebc5bb1732016-07-04 17:23:05 +03003361 struct mlx5_flow_spec *spec;
Raed Salem3b3233f2018-05-31 16:43:39 +03003362 struct mlx5_flow_destination dest_arr[2] = {};
3363 struct mlx5_flow_destination *rule_dst = dest_arr;
Maor Gottliebdd063d02016-08-28 14:16:32 +03003364 const void *ib_flow = (const void *)flow_attr + sizeof(*flow_attr);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003365 unsigned int spec_index;
Ariel Levkovich71c6e862018-05-13 14:33:34 +03003366 u32 prev_type = 0;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003367 int err = 0;
Raed Salem3b3233f2018-05-31 16:43:39 +03003368 int dest_num = 0;
Aviad Yehezkel802c2122018-03-28 09:27:53 +03003369 bool is_egress = flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003370
Ariel Levkovich19cc7522017-04-03 13:11:03 +03003371 if (!is_valid_attr(dev->mdev, flow_attr))
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003372 return ERR_PTR(-EINVAL);
3373
Mark Bloch78dd0c42018-09-02 12:51:31 +03003374 if (dev->rep && is_egress)
3375 return ERR_PTR(-EINVAL);
3376
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03003377 spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003378 handler = kzalloc(sizeof(*handler), GFP_KERNEL);
Maor Gottliebc5bb1732016-07-04 17:23:05 +03003379 if (!handler || !spec) {
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003380 err = -ENOMEM;
3381 goto free;
3382 }
3383
3384 INIT_LIST_HEAD(&handler->list);
Raed Salem3b3233f2018-05-31 16:43:39 +03003385 if (dst) {
3386 memcpy(&dest_arr[0], dst, sizeof(*dst));
3387 dest_num++;
3388 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003389
3390 for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
Ariel Levkovich19cc7522017-04-03 13:11:03 +03003391 err = parse_flow_attr(dev->mdev, spec->match_criteria,
Slava Shwartsmana22ed862017-04-03 13:13:52 +03003392 spec->match_value,
Ariel Levkovich71c6e862018-05-13 14:33:34 +03003393 ib_flow, flow_attr, &flow_act,
3394 prev_type);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003395 if (err < 0)
3396 goto free;
3397
Ariel Levkovich71c6e862018-05-13 14:33:34 +03003398 prev_type = ((union ib_flow_spec *)ib_flow)->type;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003399 ib_flow += ((union ib_flow_spec *)ib_flow)->size;
3400 }
3401
Yishai Hadasa550ddf2017-08-17 15:52:33 +03003402 if (!flow_is_multicast_only(flow_attr))
3403 set_underlay_qp(dev, spec, underlay_qpn);
3404
Mark Bloch018a94e2018-01-16 14:44:29 +00003405 if (dev->rep) {
3406 void *misc;
3407
3408 misc = MLX5_ADDR_OF(fte_match_param, spec->match_value,
3409 misc_parameters);
3410 MLX5_SET(fte_match_set_misc, misc, source_port,
3411 dev->rep->vport);
3412 misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
3413 misc_parameters);
3414 MLX5_SET_TO_ONES(fte_match_set_misc, misc, source_port);
3415 }
3416
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03003417 spec->match_criteria_enable = get_match_criteria_enable(spec->match_criteria);
Aviad Yehezkel802c2122018-03-28 09:27:53 +03003418
3419 if (is_egress &&
3420 !is_valid_spec(dev->mdev, spec, &flow_act, is_egress)) {
3421 err = -EINVAL;
3422 goto free;
3423 }
3424
Raed Salem3b3233f2018-05-31 16:43:39 +03003425 if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_COUNT) {
Mark Bloch171c7625b2018-10-03 00:03:35 +00003426 struct mlx5_ib_mcounters *mcounters;
3427
Raed Salem3b3233f2018-05-31 16:43:39 +03003428 err = flow_counters_set_data(flow_act.counters, ucmd);
3429 if (err)
3430 goto free;
3431
Mark Bloch171c7625b2018-10-03 00:03:35 +00003432 mcounters = to_mcounters(flow_act.counters);
Raed Salem3b3233f2018-05-31 16:43:39 +03003433 handler->ibcounters = flow_act.counters;
3434 dest_arr[dest_num].type =
3435 MLX5_FLOW_DESTINATION_TYPE_COUNTER;
Mark Bloch171c7625b2018-10-03 00:03:35 +00003436 dest_arr[dest_num].counter_id =
3437 mlx5_fc_id(mcounters->hw_cntrs_hndl);
Raed Salem3b3233f2018-05-31 16:43:39 +03003438 dest_num++;
3439 }
3440
Boris Pismenny075572d2017-08-16 09:33:30 +03003441 if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_DROP) {
Raed Salem3b3233f2018-05-31 16:43:39 +03003442 if (!(flow_act.action & MLX5_FLOW_CONTEXT_ACTION_COUNT)) {
3443 rule_dst = NULL;
3444 dest_num = 0;
3445 }
Slava Shwartsmana22ed862017-04-03 13:13:52 +03003446 } else {
Aviad Yehezkel802c2122018-03-28 09:27:53 +03003447 if (is_egress)
3448 flow_act.action |= MLX5_FLOW_CONTEXT_ACTION_ALLOW;
3449 else
3450 flow_act.action |=
Raed Salem3b3233f2018-05-31 16:43:39 +03003451 dest_num ? MLX5_FLOW_CONTEXT_ACTION_FWD_DEST :
Aviad Yehezkel802c2122018-03-28 09:27:53 +03003452 MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO;
Slava Shwartsmana22ed862017-04-03 13:13:52 +03003453 }
Moses Reuben2ac693f2017-01-18 14:59:50 +02003454
Paul Blakeyd5634fe2018-09-20 12:17:48 +02003455 if ((flow_act.flags & FLOW_ACT_HAS_TAG) &&
Moses Reuben2ac693f2017-01-18 14:59:50 +02003456 (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
3457 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT)) {
3458 mlx5_ib_warn(dev, "Flow tag %u and attribute type %x isn't allowed in leftovers\n",
Boris Pismenny075572d2017-08-16 09:33:30 +03003459 flow_act.flow_tag, flow_attr->type);
Moses Reuben2ac693f2017-01-18 14:59:50 +02003460 err = -EINVAL;
3461 goto free;
3462 }
Mark Bloch74491de2016-08-31 11:24:25 +00003463 handler->rule = mlx5_add_flow_rules(ft, spec,
Hadar Hen Zion66958ed2016-11-07 15:14:45 +02003464 &flow_act,
Slava Shwartsmana22ed862017-04-03 13:13:52 +03003465 rule_dst, dest_num);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003466
3467 if (IS_ERR(handler->rule)) {
3468 err = PTR_ERR(handler->rule);
3469 goto free;
3470 }
3471
Maor Gottliebd9d49802016-08-28 14:16:33 +03003472 ft_prio->refcount++;
Maor Gottlieb5497adc2016-08-28 14:16:31 +03003473 handler->prio = ft_prio;
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003474 handler->dev = dev;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003475
3476 ft_prio->flow_table = ft;
3477free:
Raed Salem3b3233f2018-05-31 16:43:39 +03003478 if (err && handler) {
3479 if (handler->ibcounters &&
3480 atomic_read(&handler->ibcounters->usecnt) == 1)
3481 counters_clear_description(handler->ibcounters);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003482 kfree(handler);
Raed Salem3b3233f2018-05-31 16:43:39 +03003483 }
Maor Gottliebc5bb1732016-07-04 17:23:05 +03003484 kvfree(spec);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003485 return err ? ERR_PTR(err) : handler;
3486}
3487
Yishai Hadasa550ddf2017-08-17 15:52:33 +03003488static struct mlx5_ib_flow_handler *create_flow_rule(struct mlx5_ib_dev *dev,
3489 struct mlx5_ib_flow_prio *ft_prio,
3490 const struct ib_flow_attr *flow_attr,
3491 struct mlx5_flow_destination *dst)
3492{
Raed Salem3b3233f2018-05-31 16:43:39 +03003493 return _create_flow_rule(dev, ft_prio, flow_attr, dst, 0, NULL);
Yishai Hadasa550ddf2017-08-17 15:52:33 +03003494}
3495
Maor Gottlieb35d190112016-03-07 18:51:47 +02003496static struct mlx5_ib_flow_handler *create_dont_trap_rule(struct mlx5_ib_dev *dev,
3497 struct mlx5_ib_flow_prio *ft_prio,
3498 struct ib_flow_attr *flow_attr,
3499 struct mlx5_flow_destination *dst)
3500{
3501 struct mlx5_ib_flow_handler *handler_dst = NULL;
3502 struct mlx5_ib_flow_handler *handler = NULL;
3503
3504 handler = create_flow_rule(dev, ft_prio, flow_attr, NULL);
3505 if (!IS_ERR(handler)) {
3506 handler_dst = create_flow_rule(dev, ft_prio,
3507 flow_attr, dst);
3508 if (IS_ERR(handler_dst)) {
Mark Bloch74491de2016-08-31 11:24:25 +00003509 mlx5_del_flow_rules(handler->rule);
Maor Gottliebd9d49802016-08-28 14:16:33 +03003510 ft_prio->refcount--;
Maor Gottlieb35d190112016-03-07 18:51:47 +02003511 kfree(handler);
3512 handler = handler_dst;
3513 } else {
3514 list_add(&handler_dst->list, &handler->list);
3515 }
3516 }
3517
3518 return handler;
3519}
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003520enum {
3521 LEFTOVERS_MC,
3522 LEFTOVERS_UC,
3523};
3524
3525static struct mlx5_ib_flow_handler *create_leftovers_rule(struct mlx5_ib_dev *dev,
3526 struct mlx5_ib_flow_prio *ft_prio,
3527 struct ib_flow_attr *flow_attr,
3528 struct mlx5_flow_destination *dst)
3529{
3530 struct mlx5_ib_flow_handler *handler_ucast = NULL;
3531 struct mlx5_ib_flow_handler *handler = NULL;
3532
3533 static struct {
3534 struct ib_flow_attr flow_attr;
3535 struct ib_flow_spec_eth eth_flow;
3536 } leftovers_specs[] = {
3537 [LEFTOVERS_MC] = {
3538 .flow_attr = {
3539 .num_of_specs = 1,
3540 .size = sizeof(leftovers_specs[0])
3541 },
3542 .eth_flow = {
3543 .type = IB_FLOW_SPEC_ETH,
3544 .size = sizeof(struct ib_flow_spec_eth),
3545 .mask = {.dst_mac = {0x1} },
3546 .val = {.dst_mac = {0x1} }
3547 }
3548 },
3549 [LEFTOVERS_UC] = {
3550 .flow_attr = {
3551 .num_of_specs = 1,
3552 .size = sizeof(leftovers_specs[0])
3553 },
3554 .eth_flow = {
3555 .type = IB_FLOW_SPEC_ETH,
3556 .size = sizeof(struct ib_flow_spec_eth),
3557 .mask = {.dst_mac = {0x1} },
3558 .val = {.dst_mac = {} }
3559 }
3560 }
3561 };
3562
3563 handler = create_flow_rule(dev, ft_prio,
3564 &leftovers_specs[LEFTOVERS_MC].flow_attr,
3565 dst);
3566 if (!IS_ERR(handler) &&
3567 flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT) {
3568 handler_ucast = create_flow_rule(dev, ft_prio,
3569 &leftovers_specs[LEFTOVERS_UC].flow_attr,
3570 dst);
3571 if (IS_ERR(handler_ucast)) {
Mark Bloch74491de2016-08-31 11:24:25 +00003572 mlx5_del_flow_rules(handler->rule);
Maor Gottliebd9d49802016-08-28 14:16:33 +03003573 ft_prio->refcount--;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003574 kfree(handler);
3575 handler = handler_ucast;
3576 } else {
3577 list_add(&handler_ucast->list, &handler->list);
3578 }
3579 }
3580
3581 return handler;
3582}
3583
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003584static struct mlx5_ib_flow_handler *create_sniffer_rule(struct mlx5_ib_dev *dev,
3585 struct mlx5_ib_flow_prio *ft_rx,
3586 struct mlx5_ib_flow_prio *ft_tx,
3587 struct mlx5_flow_destination *dst)
3588{
3589 struct mlx5_ib_flow_handler *handler_rx;
3590 struct mlx5_ib_flow_handler *handler_tx;
3591 int err;
3592 static const struct ib_flow_attr flow_attr = {
3593 .num_of_specs = 0,
3594 .size = sizeof(flow_attr)
3595 };
3596
3597 handler_rx = create_flow_rule(dev, ft_rx, &flow_attr, dst);
3598 if (IS_ERR(handler_rx)) {
3599 err = PTR_ERR(handler_rx);
3600 goto err;
3601 }
3602
3603 handler_tx = create_flow_rule(dev, ft_tx, &flow_attr, dst);
3604 if (IS_ERR(handler_tx)) {
3605 err = PTR_ERR(handler_tx);
3606 goto err_tx;
3607 }
3608
3609 list_add(&handler_tx->list, &handler_rx->list);
3610
3611 return handler_rx;
3612
3613err_tx:
Mark Bloch74491de2016-08-31 11:24:25 +00003614 mlx5_del_flow_rules(handler_rx->rule);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003615 ft_rx->refcount--;
3616 kfree(handler_rx);
3617err:
3618 return ERR_PTR(err);
3619}
3620
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003621static struct ib_flow *mlx5_ib_create_flow(struct ib_qp *qp,
3622 struct ib_flow_attr *flow_attr,
Matan Barak59082a32018-05-31 16:43:35 +03003623 int domain,
3624 struct ib_udata *udata)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003625{
3626 struct mlx5_ib_dev *dev = to_mdev(qp->device);
Yishai Hadasd9f88e52016-08-28 10:58:37 +03003627 struct mlx5_ib_qp *mqp = to_mqp(qp);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003628 struct mlx5_ib_flow_handler *handler = NULL;
3629 struct mlx5_flow_destination *dst = NULL;
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003630 struct mlx5_ib_flow_prio *ft_prio_tx = NULL;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003631 struct mlx5_ib_flow_prio *ft_prio;
Aviad Yehezkel802c2122018-03-28 09:27:53 +03003632 bool is_egress = flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS;
Raed Salem3b3233f2018-05-31 16:43:39 +03003633 struct mlx5_ib_create_flow *ucmd = NULL, ucmd_hdr;
3634 size_t min_ucmd_sz, required_ucmd_sz;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003635 int err;
Yishai Hadasa550ddf2017-08-17 15:52:33 +03003636 int underlay_qpn;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003637
Raed Salem3b3233f2018-05-31 16:43:39 +03003638 if (udata && udata->inlen) {
3639 min_ucmd_sz = offsetof(typeof(ucmd_hdr), reserved) +
3640 sizeof(ucmd_hdr.reserved);
3641 if (udata->inlen < min_ucmd_sz)
3642 return ERR_PTR(-EOPNOTSUPP);
3643
3644 err = ib_copy_from_udata(&ucmd_hdr, udata, min_ucmd_sz);
3645 if (err)
3646 return ERR_PTR(err);
3647
3648 /* currently supports only one counters data */
3649 if (ucmd_hdr.ncounters_data > 1)
3650 return ERR_PTR(-EINVAL);
3651
3652 required_ucmd_sz = min_ucmd_sz +
3653 sizeof(struct mlx5_ib_flow_counters_data) *
3654 ucmd_hdr.ncounters_data;
3655 if (udata->inlen > required_ucmd_sz &&
3656 !ib_is_udata_cleared(udata, required_ucmd_sz,
3657 udata->inlen - required_ucmd_sz))
3658 return ERR_PTR(-EOPNOTSUPP);
3659
3660 ucmd = kzalloc(required_ucmd_sz, GFP_KERNEL);
3661 if (!ucmd)
3662 return ERR_PTR(-ENOMEM);
3663
3664 err = ib_copy_from_udata(ucmd, udata, required_ucmd_sz);
Gustavo A. R. Silva299eafe2018-06-07 14:19:15 -05003665 if (err)
3666 goto free_ucmd;
Raed Salem3b3233f2018-05-31 16:43:39 +03003667 }
Matan Barak59082a32018-05-31 16:43:35 +03003668
Gustavo A. R. Silva299eafe2018-06-07 14:19:15 -05003669 if (flow_attr->priority > MLX5_IB_FLOW_LAST_PRIO) {
3670 err = -ENOMEM;
3671 goto free_ucmd;
3672 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003673
3674 if (domain != IB_FLOW_DOMAIN_USER ||
Daniel Jurgens508562d2018-01-04 17:25:34 +02003675 flow_attr->port > dev->num_ports ||
Aviad Yehezkel802c2122018-03-28 09:27:53 +03003676 (flow_attr->flags & ~(IB_FLOW_ATTR_FLAGS_DONT_TRAP |
Gustavo A. R. Silva299eafe2018-06-07 14:19:15 -05003677 IB_FLOW_ATTR_FLAGS_EGRESS))) {
3678 err = -EINVAL;
3679 goto free_ucmd;
3680 }
Aviad Yehezkel802c2122018-03-28 09:27:53 +03003681
3682 if (is_egress &&
3683 (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
Gustavo A. R. Silva299eafe2018-06-07 14:19:15 -05003684 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT)) {
3685 err = -EINVAL;
3686 goto free_ucmd;
3687 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003688
3689 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
Gustavo A. R. Silva299eafe2018-06-07 14:19:15 -05003690 if (!dst) {
3691 err = -ENOMEM;
3692 goto free_ucmd;
3693 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003694
Mark Bloch9a4ca382018-01-16 14:42:35 +00003695 mutex_lock(&dev->flow_db->lock);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003696
Aviad Yehezkel802c2122018-03-28 09:27:53 +03003697 ft_prio = get_flow_table(dev, flow_attr,
3698 is_egress ? MLX5_IB_FT_TX : MLX5_IB_FT_RX);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003699 if (IS_ERR(ft_prio)) {
3700 err = PTR_ERR(ft_prio);
3701 goto unlock;
3702 }
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003703 if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
3704 ft_prio_tx = get_flow_table(dev, flow_attr, MLX5_IB_FT_TX);
3705 if (IS_ERR(ft_prio_tx)) {
3706 err = PTR_ERR(ft_prio_tx);
3707 ft_prio_tx = NULL;
3708 goto destroy_ft;
3709 }
3710 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003711
Aviad Yehezkel802c2122018-03-28 09:27:53 +03003712 if (is_egress) {
3713 dst->type = MLX5_FLOW_DESTINATION_TYPE_PORT;
3714 } else {
3715 dst->type = MLX5_FLOW_DESTINATION_TYPE_TIR;
3716 if (mqp->flags & MLX5_IB_QP_RSS)
3717 dst->tir_num = mqp->rss_qp.tirn;
3718 else
3719 dst->tir_num = mqp->raw_packet_qp.rq.tirn;
3720 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003721
3722 if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
Maor Gottlieb35d190112016-03-07 18:51:47 +02003723 if (flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP) {
3724 handler = create_dont_trap_rule(dev, ft_prio,
3725 flow_attr, dst);
3726 } else {
Yishai Hadasa550ddf2017-08-17 15:52:33 +03003727 underlay_qpn = (mqp->flags & MLX5_IB_QP_UNDERLAY) ?
3728 mqp->underlay_qpn : 0;
3729 handler = _create_flow_rule(dev, ft_prio, flow_attr,
Raed Salem3b3233f2018-05-31 16:43:39 +03003730 dst, underlay_qpn, ucmd);
Maor Gottlieb35d190112016-03-07 18:51:47 +02003731 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003732 } else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
3733 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
3734 handler = create_leftovers_rule(dev, ft_prio, flow_attr,
3735 dst);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003736 } else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
3737 handler = create_sniffer_rule(dev, ft_prio, ft_prio_tx, dst);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003738 } else {
3739 err = -EINVAL;
3740 goto destroy_ft;
3741 }
3742
3743 if (IS_ERR(handler)) {
3744 err = PTR_ERR(handler);
3745 handler = NULL;
3746 goto destroy_ft;
3747 }
3748
Mark Bloch9a4ca382018-01-16 14:42:35 +00003749 mutex_unlock(&dev->flow_db->lock);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003750 kfree(dst);
Raed Salem3b3233f2018-05-31 16:43:39 +03003751 kfree(ucmd);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003752
3753 return &handler->ibflow;
3754
3755destroy_ft:
3756 put_flow_table(dev, ft_prio, false);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003757 if (ft_prio_tx)
3758 put_flow_table(dev, ft_prio_tx, false);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003759unlock:
Mark Bloch9a4ca382018-01-16 14:42:35 +00003760 mutex_unlock(&dev->flow_db->lock);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003761 kfree(dst);
Gustavo A. R. Silva299eafe2018-06-07 14:19:15 -05003762free_ucmd:
Raed Salem3b3233f2018-05-31 16:43:39 +03003763 kfree(ucmd);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003764 return ERR_PTR(err);
3765}
3766
Mark Blochb47fd4f2018-09-06 17:27:07 +03003767static struct mlx5_ib_flow_prio *
3768_get_flow_table(struct mlx5_ib_dev *dev,
3769 struct mlx5_ib_flow_matcher *fs_matcher,
3770 bool mcast)
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003771{
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003772 struct mlx5_flow_namespace *ns = NULL;
3773 struct mlx5_ib_flow_prio *prio;
Mark Blochb47fd4f2018-09-06 17:27:07 +03003774 int max_table_size;
3775 u32 flags = 0;
3776 int priority;
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003777
Mark Blochb47fd4f2018-09-06 17:27:07 +03003778 if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_BYPASS) {
3779 max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
3780 log_max_ft_size));
3781 if (MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, decap))
3782 flags |= MLX5_FLOW_TABLE_TUNNEL_EN_DECAP;
3783 if (MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
3784 reformat_l3_tunnel_to_l2))
3785 flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
3786 } else { /* Can only be MLX5_FLOW_NAMESPACE_EGRESS */
3787 max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev,
3788 log_max_ft_size));
3789 if (MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev, reformat))
3790 flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
3791 }
3792
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003793 if (max_table_size < MLX5_FS_MAX_ENTRIES)
3794 return ERR_PTR(-ENOMEM);
3795
3796 if (mcast)
3797 priority = MLX5_IB_FLOW_MCAST_PRIO;
3798 else
Mark Blochb47fd4f2018-09-06 17:27:07 +03003799 priority = ib_prio_to_core_prio(fs_matcher->priority, false);
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003800
Mark Blochb47fd4f2018-09-06 17:27:07 +03003801 ns = mlx5_get_flow_namespace(dev->mdev, fs_matcher->ns_type);
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003802 if (!ns)
3803 return ERR_PTR(-ENOTSUPP);
3804
Mark Blochb47fd4f2018-09-06 17:27:07 +03003805 if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_BYPASS)
3806 prio = &dev->flow_db->prios[priority];
3807 else
3808 prio = &dev->flow_db->egress_prios[priority];
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003809
3810 if (prio->flow_table)
3811 return prio;
3812
3813 return _get_prio(ns, prio, priority, MLX5_FS_MAX_ENTRIES,
Mark Blochb47fd4f2018-09-06 17:27:07 +03003814 MLX5_FS_MAX_TYPES, flags);
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003815}
3816
3817static struct mlx5_ib_flow_handler *
3818_create_raw_flow_rule(struct mlx5_ib_dev *dev,
3819 struct mlx5_ib_flow_prio *ft_prio,
3820 struct mlx5_flow_destination *dst,
3821 struct mlx5_ib_flow_matcher *fs_matcher,
Mark Blochb823dd62018-09-06 17:27:05 +03003822 struct mlx5_flow_act *flow_act,
Mark Blochbfc5d832018-11-20 20:31:08 +02003823 void *cmd_in, int inlen,
3824 int dst_num)
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003825{
3826 struct mlx5_ib_flow_handler *handler;
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003827 struct mlx5_flow_spec *spec;
3828 struct mlx5_flow_table *ft = ft_prio->flow_table;
3829 int err = 0;
3830
3831 spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
3832 handler = kzalloc(sizeof(*handler), GFP_KERNEL);
3833 if (!handler || !spec) {
3834 err = -ENOMEM;
3835 goto free;
3836 }
3837
3838 INIT_LIST_HEAD(&handler->list);
3839
3840 memcpy(spec->match_value, cmd_in, inlen);
3841 memcpy(spec->match_criteria, fs_matcher->matcher_mask.match_params,
3842 fs_matcher->mask_len);
3843 spec->match_criteria_enable = fs_matcher->match_criteria_enable;
3844
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003845 handler->rule = mlx5_add_flow_rules(ft, spec,
Mark Blochbfc5d832018-11-20 20:31:08 +02003846 flow_act, dst, dst_num);
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003847
3848 if (IS_ERR(handler->rule)) {
3849 err = PTR_ERR(handler->rule);
3850 goto free;
3851 }
3852
3853 ft_prio->refcount++;
3854 handler->prio = ft_prio;
3855 handler->dev = dev;
3856 ft_prio->flow_table = ft;
3857
3858free:
3859 if (err)
3860 kfree(handler);
3861 kvfree(spec);
3862 return err ? ERR_PTR(err) : handler;
3863}
3864
3865static bool raw_fs_is_multicast(struct mlx5_ib_flow_matcher *fs_matcher,
3866 void *match_v)
3867{
3868 void *match_c;
3869 void *match_v_set_lyr_2_4, *match_c_set_lyr_2_4;
3870 void *dmac, *dmac_mask;
3871 void *ipv4, *ipv4_mask;
3872
3873 if (!(fs_matcher->match_criteria_enable &
3874 (1 << MATCH_CRITERIA_ENABLE_OUTER_BIT)))
3875 return false;
3876
3877 match_c = fs_matcher->matcher_mask.match_params;
3878 match_v_set_lyr_2_4 = MLX5_ADDR_OF(fte_match_param, match_v,
3879 outer_headers);
3880 match_c_set_lyr_2_4 = MLX5_ADDR_OF(fte_match_param, match_c,
3881 outer_headers);
3882
3883 dmac = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_v_set_lyr_2_4,
3884 dmac_47_16);
3885 dmac_mask = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_c_set_lyr_2_4,
3886 dmac_47_16);
3887
3888 if (is_multicast_ether_addr(dmac) &&
3889 is_multicast_ether_addr(dmac_mask))
3890 return true;
3891
3892 ipv4 = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_v_set_lyr_2_4,
3893 dst_ipv4_dst_ipv6.ipv4_layout.ipv4);
3894
3895 ipv4_mask = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_c_set_lyr_2_4,
3896 dst_ipv4_dst_ipv6.ipv4_layout.ipv4);
3897
3898 if (ipv4_is_multicast(*(__be32 *)(ipv4)) &&
3899 ipv4_is_multicast(*(__be32 *)(ipv4_mask)))
3900 return true;
3901
3902 return false;
3903}
3904
Yishai Hadas32269442018-07-23 15:25:09 +03003905struct mlx5_ib_flow_handler *
3906mlx5_ib_raw_fs_rule_add(struct mlx5_ib_dev *dev,
3907 struct mlx5_ib_flow_matcher *fs_matcher,
Mark Blochb823dd62018-09-06 17:27:05 +03003908 struct mlx5_flow_act *flow_act,
Mark Blochbfc5d832018-11-20 20:31:08 +02003909 u32 counter_id,
Yishai Hadas32269442018-07-23 15:25:09 +03003910 void *cmd_in, int inlen, int dest_id,
3911 int dest_type)
3912{
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003913 struct mlx5_flow_destination *dst;
3914 struct mlx5_ib_flow_prio *ft_prio;
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003915 struct mlx5_ib_flow_handler *handler;
Mark Blochbfc5d832018-11-20 20:31:08 +02003916 int dst_num = 0;
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003917 bool mcast;
3918 int err;
3919
3920 if (fs_matcher->flow_type != MLX5_IB_FLOW_TYPE_NORMAL)
3921 return ERR_PTR(-EOPNOTSUPP);
3922
3923 if (fs_matcher->priority > MLX5_IB_FLOW_LAST_PRIO)
3924 return ERR_PTR(-ENOMEM);
3925
Gustavo A. R. Silva8e8aa142019-01-15 00:00:48 -06003926 dst = kcalloc(2, sizeof(*dst), GFP_KERNEL);
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003927 if (!dst)
3928 return ERR_PTR(-ENOMEM);
3929
3930 mcast = raw_fs_is_multicast(fs_matcher, cmd_in);
3931 mutex_lock(&dev->flow_db->lock);
3932
Mark Blochb47fd4f2018-09-06 17:27:07 +03003933 ft_prio = _get_flow_table(dev, fs_matcher, mcast);
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003934 if (IS_ERR(ft_prio)) {
3935 err = PTR_ERR(ft_prio);
3936 goto unlock;
3937 }
3938
Yishai Hadas6346f0b2018-07-23 15:25:11 +03003939 if (dest_type == MLX5_FLOW_DESTINATION_TYPE_TIR) {
Mark Blochbfc5d832018-11-20 20:31:08 +02003940 dst[dst_num].type = dest_type;
3941 dst[dst_num].tir_num = dest_id;
Mark Blochb823dd62018-09-06 17:27:05 +03003942 flow_act->action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
Mark Blocha7ee18b2018-09-06 17:27:08 +03003943 } else if (dest_type == MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE) {
Mark Blochbfc5d832018-11-20 20:31:08 +02003944 dst[dst_num].type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE_NUM;
3945 dst[dst_num].ft_num = dest_id;
Mark Blochb823dd62018-09-06 17:27:05 +03003946 flow_act->action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
Mark Blocha7ee18b2018-09-06 17:27:08 +03003947 } else {
Mark Blochbfc5d832018-11-20 20:31:08 +02003948 dst[dst_num].type = MLX5_FLOW_DESTINATION_TYPE_PORT;
Mark Blocha7ee18b2018-09-06 17:27:08 +03003949 flow_act->action |= MLX5_FLOW_CONTEXT_ACTION_ALLOW;
Yishai Hadas6346f0b2018-07-23 15:25:11 +03003950 }
3951
Mark Blochbfc5d832018-11-20 20:31:08 +02003952 dst_num++;
3953
3954 if (flow_act->action & MLX5_FLOW_CONTEXT_ACTION_COUNT) {
3955 dst[dst_num].type = MLX5_FLOW_DESTINATION_TYPE_COUNTER;
3956 dst[dst_num].counter_id = counter_id;
3957 dst_num++;
3958 }
3959
Mark Blochb823dd62018-09-06 17:27:05 +03003960 handler = _create_raw_flow_rule(dev, ft_prio, dst, fs_matcher, flow_act,
Mark Blochbfc5d832018-11-20 20:31:08 +02003961 cmd_in, inlen, dst_num);
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003962
3963 if (IS_ERR(handler)) {
3964 err = PTR_ERR(handler);
3965 goto destroy_ft;
3966 }
3967
3968 mutex_unlock(&dev->flow_db->lock);
3969 atomic_inc(&fs_matcher->usecnt);
3970 handler->flow_matcher = fs_matcher;
3971
3972 kfree(dst);
3973
3974 return handler;
3975
3976destroy_ft:
3977 put_flow_table(dev, ft_prio, false);
3978unlock:
3979 mutex_unlock(&dev->flow_db->lock);
3980 kfree(dst);
3981
3982 return ERR_PTR(err);
Yishai Hadas32269442018-07-23 15:25:09 +03003983}
3984
Aviad Yehezkelc6475a02018-03-28 09:27:50 +03003985static u32 mlx5_ib_flow_action_flags_to_accel_xfrm_flags(u32 mlx5_flags)
3986{
3987 u32 flags = 0;
3988
3989 if (mlx5_flags & MLX5_IB_UAPI_FLOW_ACTION_FLAGS_REQUIRE_METADATA)
3990 flags |= MLX5_ACCEL_XFRM_FLAG_REQUIRE_METADATA;
3991
3992 return flags;
3993}
3994
3995#define MLX5_FLOW_ACTION_ESP_CREATE_LAST_SUPPORTED MLX5_IB_UAPI_FLOW_ACTION_FLAGS_REQUIRE_METADATA
3996static struct ib_flow_action *
3997mlx5_ib_create_flow_action_esp(struct ib_device *device,
3998 const struct ib_flow_action_attrs_esp *attr,
3999 struct uverbs_attr_bundle *attrs)
4000{
4001 struct mlx5_ib_dev *mdev = to_mdev(device);
4002 struct ib_uverbs_flow_action_esp_keymat_aes_gcm *aes_gcm;
4003 struct mlx5_accel_esp_xfrm_attrs accel_attrs = {};
4004 struct mlx5_ib_flow_action *action;
4005 u64 action_flags;
4006 u64 flags;
4007 int err = 0;
4008
Jason Gunthorpebccd0622018-07-26 16:37:14 -06004009 err = uverbs_get_flags64(
4010 &action_flags, attrs, MLX5_IB_ATTR_CREATE_FLOW_ACTION_FLAGS,
4011 ((MLX5_FLOW_ACTION_ESP_CREATE_LAST_SUPPORTED << 1) - 1));
4012 if (err)
4013 return ERR_PTR(err);
Aviad Yehezkelc6475a02018-03-28 09:27:50 +03004014
4015 flags = mlx5_ib_flow_action_flags_to_accel_xfrm_flags(action_flags);
4016
4017 /* We current only support a subset of the standard features. Only a
4018 * keymat of type AES_GCM, with icv_len == 16, iv_algo == SEQ and esn
4019 * (with overlap). Full offload mode isn't supported.
4020 */
4021 if (!attr->keymat || attr->replay || attr->encap ||
4022 attr->spi || attr->seq || attr->tfc_pad ||
4023 attr->hard_limit_pkts ||
4024 (attr->flags & ~(IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED |
4025 IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ENCRYPT)))
4026 return ERR_PTR(-EOPNOTSUPP);
4027
4028 if (attr->keymat->protocol !=
4029 IB_UVERBS_FLOW_ACTION_ESP_KEYMAT_AES_GCM)
4030 return ERR_PTR(-EOPNOTSUPP);
4031
4032 aes_gcm = &attr->keymat->keymat.aes_gcm;
4033
4034 if (aes_gcm->icv_len != 16 ||
4035 aes_gcm->iv_algo != IB_UVERBS_FLOW_ACTION_IV_ALGO_SEQ)
4036 return ERR_PTR(-EOPNOTSUPP);
4037
4038 action = kmalloc(sizeof(*action), GFP_KERNEL);
4039 if (!action)
4040 return ERR_PTR(-ENOMEM);
4041
4042 action->esp_aes_gcm.ib_flags = attr->flags;
4043 memcpy(&accel_attrs.keymat.aes_gcm.aes_key, &aes_gcm->aes_key,
4044 sizeof(accel_attrs.keymat.aes_gcm.aes_key));
4045 accel_attrs.keymat.aes_gcm.key_len = aes_gcm->key_len * 8;
4046 memcpy(&accel_attrs.keymat.aes_gcm.salt, &aes_gcm->salt,
4047 sizeof(accel_attrs.keymat.aes_gcm.salt));
4048 memcpy(&accel_attrs.keymat.aes_gcm.seq_iv, &aes_gcm->iv,
4049 sizeof(accel_attrs.keymat.aes_gcm.seq_iv));
4050 accel_attrs.keymat.aes_gcm.icv_len = aes_gcm->icv_len * 8;
4051 accel_attrs.keymat.aes_gcm.iv_algo = MLX5_ACCEL_ESP_AES_GCM_IV_ALGO_SEQ;
4052 accel_attrs.keymat_type = MLX5_ACCEL_ESP_KEYMAT_AES_GCM;
4053
4054 accel_attrs.esn = attr->esn;
4055 if (attr->flags & IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED)
4056 accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_TRIGGERED;
4057 if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW)
4058 accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP;
4059
4060 if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ENCRYPT)
4061 accel_attrs.action |= MLX5_ACCEL_ESP_ACTION_ENCRYPT;
4062
4063 action->esp_aes_gcm.ctx =
4064 mlx5_accel_esp_create_xfrm(mdev->mdev, &accel_attrs, flags);
4065 if (IS_ERR(action->esp_aes_gcm.ctx)) {
4066 err = PTR_ERR(action->esp_aes_gcm.ctx);
4067 goto err_parse;
4068 }
4069
4070 action->esp_aes_gcm.ib_flags = attr->flags;
4071
4072 return &action->ib_action;
4073
4074err_parse:
4075 kfree(action);
4076 return ERR_PTR(err);
4077}
4078
Matan Barak349705c2018-03-28 09:27:51 +03004079static int
4080mlx5_ib_modify_flow_action_esp(struct ib_flow_action *action,
4081 const struct ib_flow_action_attrs_esp *attr,
4082 struct uverbs_attr_bundle *attrs)
4083{
4084 struct mlx5_ib_flow_action *maction = to_mflow_act(action);
4085 struct mlx5_accel_esp_xfrm_attrs accel_attrs;
4086 int err = 0;
4087
4088 if (attr->keymat || attr->replay || attr->encap ||
4089 attr->spi || attr->seq || attr->tfc_pad ||
4090 attr->hard_limit_pkts ||
4091 (attr->flags & ~(IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED |
4092 IB_FLOW_ACTION_ESP_FLAGS_MOD_ESP_ATTRS |
4093 IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW)))
4094 return -EOPNOTSUPP;
4095
4096 /* Only the ESN value or the MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP can
4097 * be modified.
4098 */
4099 if (!(maction->esp_aes_gcm.ib_flags &
4100 IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED) &&
4101 attr->flags & (IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED |
4102 IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW))
4103 return -EINVAL;
4104
4105 memcpy(&accel_attrs, &maction->esp_aes_gcm.ctx->attrs,
4106 sizeof(accel_attrs));
4107
4108 accel_attrs.esn = attr->esn;
4109 if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW)
4110 accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP;
4111 else
4112 accel_attrs.flags &= ~MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP;
4113
4114 err = mlx5_accel_esp_modify_xfrm(maction->esp_aes_gcm.ctx,
4115 &accel_attrs);
4116 if (err)
4117 return err;
4118
4119 maction->esp_aes_gcm.ib_flags &=
4120 ~IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW;
4121 maction->esp_aes_gcm.ib_flags |=
4122 attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW;
4123
4124 return 0;
4125}
4126
Aviad Yehezkelc6475a02018-03-28 09:27:50 +03004127static int mlx5_ib_destroy_flow_action(struct ib_flow_action *action)
4128{
4129 struct mlx5_ib_flow_action *maction = to_mflow_act(action);
4130
4131 switch (action->type) {
4132 case IB_FLOW_ACTION_ESP:
4133 /*
4134 * We only support aes_gcm by now, so we implicitly know this is
4135 * the underline crypto.
4136 */
4137 mlx5_accel_esp_destroy_xfrm(maction->esp_aes_gcm.ctx);
4138 break;
Mark Blochb4749bf2018-08-28 14:18:51 +03004139 case IB_FLOW_ACTION_UNSPECIFIED:
4140 mlx5_ib_destroy_flow_action_raw(maction);
4141 break;
Aviad Yehezkelc6475a02018-03-28 09:27:50 +03004142 default:
4143 WARN_ON(true);
4144 break;
4145 }
4146
4147 kfree(maction);
4148 return 0;
4149}
4150
Eli Cohene126ba92013-07-07 17:25:49 +03004151static int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
4152{
4153 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
Yishai Hadas81e30882017-06-08 16:15:09 +03004154 struct mlx5_ib_qp *mqp = to_mqp(ibqp);
Eli Cohene126ba92013-07-07 17:25:49 +03004155 int err;
Yishai Hadas539ec982018-09-20 21:39:25 +03004156 u16 uid;
4157
4158 uid = ibqp->pd ?
4159 to_mpd(ibqp->pd)->uid : 0;
Eli Cohene126ba92013-07-07 17:25:49 +03004160
Yishai Hadas81e30882017-06-08 16:15:09 +03004161 if (mqp->flags & MLX5_IB_QP_UNDERLAY) {
4162 mlx5_ib_dbg(dev, "Attaching a multi cast group to underlay QP is not supported\n");
4163 return -EOPNOTSUPP;
4164 }
4165
Yishai Hadas539ec982018-09-20 21:39:25 +03004166 err = mlx5_cmd_attach_mcg(dev->mdev, gid, ibqp->qp_num, uid);
Eli Cohene126ba92013-07-07 17:25:49 +03004167 if (err)
4168 mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n",
4169 ibqp->qp_num, gid->raw);
4170
4171 return err;
4172}
4173
4174static int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
4175{
4176 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
4177 int err;
Yishai Hadas539ec982018-09-20 21:39:25 +03004178 u16 uid;
Eli Cohene126ba92013-07-07 17:25:49 +03004179
Yishai Hadas539ec982018-09-20 21:39:25 +03004180 uid = ibqp->pd ?
4181 to_mpd(ibqp->pd)->uid : 0;
4182 err = mlx5_cmd_detach_mcg(dev->mdev, gid, ibqp->qp_num, uid);
Eli Cohene126ba92013-07-07 17:25:49 +03004183 if (err)
4184 mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n",
4185 ibqp->qp_num, gid->raw);
4186
4187 return err;
4188}
4189
4190static int init_node_data(struct mlx5_ib_dev *dev)
4191{
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03004192 int err;
Eli Cohene126ba92013-07-07 17:25:49 +03004193
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03004194 err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc);
Eli Cohene126ba92013-07-07 17:25:49 +03004195 if (err)
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03004196 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03004197
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03004198 dev->mdev->rev_id = dev->mdev->pdev->revision;
Eli Cohene126ba92013-07-07 17:25:49 +03004199
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03004200 return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid);
Eli Cohene126ba92013-07-07 17:25:49 +03004201}
4202
Parav Pandit508a5232018-10-11 22:31:54 +03004203static ssize_t fw_pages_show(struct device *device,
4204 struct device_attribute *attr, char *buf)
Eli Cohene126ba92013-07-07 17:25:49 +03004205{
4206 struct mlx5_ib_dev *dev =
Parav Pandit54747232018-12-18 14:15:56 +02004207 rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
Eli Cohene126ba92013-07-07 17:25:49 +03004208
Jack Morgenstein9603b612014-07-28 23:30:22 +03004209 return sprintf(buf, "%d\n", dev->mdev->priv.fw_pages);
Eli Cohene126ba92013-07-07 17:25:49 +03004210}
Parav Pandit508a5232018-10-11 22:31:54 +03004211static DEVICE_ATTR_RO(fw_pages);
Eli Cohene126ba92013-07-07 17:25:49 +03004212
Parav Pandit508a5232018-10-11 22:31:54 +03004213static ssize_t reg_pages_show(struct device *device,
Eli Cohene126ba92013-07-07 17:25:49 +03004214 struct device_attribute *attr, char *buf)
4215{
4216 struct mlx5_ib_dev *dev =
Parav Pandit54747232018-12-18 14:15:56 +02004217 rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
Eli Cohene126ba92013-07-07 17:25:49 +03004218
Haggai Eran6aec21f2014-12-11 17:04:23 +02004219 return sprintf(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages));
Eli Cohene126ba92013-07-07 17:25:49 +03004220}
Parav Pandit508a5232018-10-11 22:31:54 +03004221static DEVICE_ATTR_RO(reg_pages);
Eli Cohene126ba92013-07-07 17:25:49 +03004222
Parav Pandit508a5232018-10-11 22:31:54 +03004223static ssize_t hca_type_show(struct device *device,
4224 struct device_attribute *attr, char *buf)
Eli Cohene126ba92013-07-07 17:25:49 +03004225{
4226 struct mlx5_ib_dev *dev =
Parav Pandit54747232018-12-18 14:15:56 +02004227 rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
4228
Jack Morgenstein9603b612014-07-28 23:30:22 +03004229 return sprintf(buf, "MT%d\n", dev->mdev->pdev->device);
Eli Cohene126ba92013-07-07 17:25:49 +03004230}
Parav Pandit508a5232018-10-11 22:31:54 +03004231static DEVICE_ATTR_RO(hca_type);
Eli Cohene126ba92013-07-07 17:25:49 +03004232
Parav Pandit508a5232018-10-11 22:31:54 +03004233static ssize_t hw_rev_show(struct device *device,
4234 struct device_attribute *attr, char *buf)
Eli Cohene126ba92013-07-07 17:25:49 +03004235{
4236 struct mlx5_ib_dev *dev =
Parav Pandit54747232018-12-18 14:15:56 +02004237 rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
4238
Jack Morgenstein9603b612014-07-28 23:30:22 +03004239 return sprintf(buf, "%x\n", dev->mdev->rev_id);
Eli Cohene126ba92013-07-07 17:25:49 +03004240}
Parav Pandit508a5232018-10-11 22:31:54 +03004241static DEVICE_ATTR_RO(hw_rev);
Eli Cohene126ba92013-07-07 17:25:49 +03004242
Parav Pandit508a5232018-10-11 22:31:54 +03004243static ssize_t board_id_show(struct device *device,
4244 struct device_attribute *attr, char *buf)
Eli Cohene126ba92013-07-07 17:25:49 +03004245{
4246 struct mlx5_ib_dev *dev =
Parav Pandit54747232018-12-18 14:15:56 +02004247 rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
4248
Eli Cohene126ba92013-07-07 17:25:49 +03004249 return sprintf(buf, "%.*s\n", MLX5_BOARD_ID_LEN,
Jack Morgenstein9603b612014-07-28 23:30:22 +03004250 dev->mdev->board_id);
Eli Cohene126ba92013-07-07 17:25:49 +03004251}
Parav Pandit508a5232018-10-11 22:31:54 +03004252static DEVICE_ATTR_RO(board_id);
Eli Cohene126ba92013-07-07 17:25:49 +03004253
Parav Pandit508a5232018-10-11 22:31:54 +03004254static struct attribute *mlx5_class_attributes[] = {
4255 &dev_attr_hw_rev.attr,
4256 &dev_attr_hca_type.attr,
4257 &dev_attr_board_id.attr,
4258 &dev_attr_fw_pages.attr,
4259 &dev_attr_reg_pages.attr,
4260 NULL,
4261};
Eli Cohene126ba92013-07-07 17:25:49 +03004262
Parav Pandit508a5232018-10-11 22:31:54 +03004263static const struct attribute_group mlx5_attr_group = {
4264 .attrs = mlx5_class_attributes,
Eli Cohene126ba92013-07-07 17:25:49 +03004265};
4266
Haggai Eran7722f472016-02-29 15:45:07 +02004267static void pkey_change_handler(struct work_struct *work)
4268{
4269 struct mlx5_ib_port_resources *ports =
4270 container_of(work, struct mlx5_ib_port_resources,
4271 pkey_change_work);
4272
4273 mutex_lock(&ports->devr->mutex);
4274 mlx5_ib_gsi_pkey_change(ports->gsi);
4275 mutex_unlock(&ports->devr->mutex);
4276}
4277
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03004278static void mlx5_ib_handle_internal_error(struct mlx5_ib_dev *ibdev)
4279{
4280 struct mlx5_ib_qp *mqp;
4281 struct mlx5_ib_cq *send_mcq, *recv_mcq;
4282 struct mlx5_core_cq *mcq;
4283 struct list_head cq_armed_list;
4284 unsigned long flags_qp;
4285 unsigned long flags_cq;
4286 unsigned long flags;
4287
4288 INIT_LIST_HEAD(&cq_armed_list);
4289
4290 /* Go over qp list reside on that ibdev, sync with create/destroy qp.*/
4291 spin_lock_irqsave(&ibdev->reset_flow_resource_lock, flags);
4292 list_for_each_entry(mqp, &ibdev->qp_list, qps_list) {
4293 spin_lock_irqsave(&mqp->sq.lock, flags_qp);
4294 if (mqp->sq.tail != mqp->sq.head) {
4295 send_mcq = to_mcq(mqp->ibqp.send_cq);
4296 spin_lock_irqsave(&send_mcq->lock, flags_cq);
4297 if (send_mcq->mcq.comp &&
4298 mqp->ibqp.send_cq->comp_handler) {
4299 if (!send_mcq->mcq.reset_notify_added) {
4300 send_mcq->mcq.reset_notify_added = 1;
4301 list_add_tail(&send_mcq->mcq.reset_notify,
4302 &cq_armed_list);
4303 }
4304 }
4305 spin_unlock_irqrestore(&send_mcq->lock, flags_cq);
4306 }
4307 spin_unlock_irqrestore(&mqp->sq.lock, flags_qp);
4308 spin_lock_irqsave(&mqp->rq.lock, flags_qp);
4309 /* no handling is needed for SRQ */
4310 if (!mqp->ibqp.srq) {
4311 if (mqp->rq.tail != mqp->rq.head) {
4312 recv_mcq = to_mcq(mqp->ibqp.recv_cq);
4313 spin_lock_irqsave(&recv_mcq->lock, flags_cq);
4314 if (recv_mcq->mcq.comp &&
4315 mqp->ibqp.recv_cq->comp_handler) {
4316 if (!recv_mcq->mcq.reset_notify_added) {
4317 recv_mcq->mcq.reset_notify_added = 1;
4318 list_add_tail(&recv_mcq->mcq.reset_notify,
4319 &cq_armed_list);
4320 }
4321 }
4322 spin_unlock_irqrestore(&recv_mcq->lock,
4323 flags_cq);
4324 }
4325 }
4326 spin_unlock_irqrestore(&mqp->rq.lock, flags_qp);
4327 }
4328 /*At that point all inflight post send were put to be executed as of we
4329 * lock/unlock above locks Now need to arm all involved CQs.
4330 */
4331 list_for_each_entry(mcq, &cq_armed_list, reset_notify) {
4332 mcq->comp(mcq);
4333 }
4334 spin_unlock_irqrestore(&ibdev->reset_flow_resource_lock, flags);
4335}
4336
Maor Gottlieb03404e82017-05-30 10:29:13 +03004337static void delay_drop_handler(struct work_struct *work)
4338{
4339 int err;
4340 struct mlx5_ib_delay_drop *delay_drop =
4341 container_of(work, struct mlx5_ib_delay_drop,
4342 delay_drop_work);
4343
Maor Gottliebfe248c32017-05-30 10:29:14 +03004344 atomic_inc(&delay_drop->events_cnt);
4345
Maor Gottlieb03404e82017-05-30 10:29:13 +03004346 mutex_lock(&delay_drop->lock);
4347 err = mlx5_core_set_delay_drop(delay_drop->dev->mdev,
4348 delay_drop->timeout);
4349 if (err) {
4350 mlx5_ib_warn(delay_drop->dev, "Failed to set delay drop, timeout=%u\n",
4351 delay_drop->timeout);
4352 delay_drop->activate = false;
4353 }
4354 mutex_unlock(&delay_drop->lock);
4355}
4356
Saeed Mahameed09e574f2018-11-26 14:39:04 -08004357static void handle_general_event(struct mlx5_ib_dev *ibdev, struct mlx5_eqe *eqe,
4358 struct ib_event *ibev)
4359{
4360 switch (eqe->sub_type) {
4361 case MLX5_GENERAL_SUBTYPE_DELAY_DROP_TIMEOUT:
4362 schedule_work(&ibdev->delay_drop.delay_drop_work);
4363 break;
4364 default: /* do nothing */
4365 return;
4366 }
4367}
4368
Saeed Mahameed134e9342018-11-26 14:39:02 -08004369static int handle_port_change(struct mlx5_ib_dev *ibdev, struct mlx5_eqe *eqe,
4370 struct ib_event *ibev)
4371{
4372 u8 port = (eqe->data.port.port >> 4) & 0xf;
4373
4374 ibev->element.port_num = port;
4375
4376 switch (eqe->sub_type) {
4377 case MLX5_PORT_CHANGE_SUBTYPE_ACTIVE:
4378 case MLX5_PORT_CHANGE_SUBTYPE_DOWN:
4379 case MLX5_PORT_CHANGE_SUBTYPE_INITIALIZED:
4380 /* In RoCE, port up/down events are handled in
4381 * mlx5_netdev_event().
4382 */
4383 if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) ==
4384 IB_LINK_LAYER_ETHERNET)
4385 return -EINVAL;
4386
4387 ibev->event = (eqe->sub_type == MLX5_PORT_CHANGE_SUBTYPE_ACTIVE) ?
4388 IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
4389 break;
4390
4391 case MLX5_PORT_CHANGE_SUBTYPE_LID:
4392 ibev->event = IB_EVENT_LID_CHANGE;
4393 break;
4394
4395 case MLX5_PORT_CHANGE_SUBTYPE_PKEY:
4396 ibev->event = IB_EVENT_PKEY_CHANGE;
4397 schedule_work(&ibdev->devr.ports[port - 1].pkey_change_work);
4398 break;
4399
4400 case MLX5_PORT_CHANGE_SUBTYPE_GUID:
4401 ibev->event = IB_EVENT_GID_CHANGE;
4402 break;
4403
4404 case MLX5_PORT_CHANGE_SUBTYPE_CLIENT_REREG:
4405 ibev->event = IB_EVENT_CLIENT_REREGISTER;
4406 break;
4407 default:
4408 return -EINVAL;
4409 }
4410
4411 return 0;
4412}
4413
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02004414static void mlx5_ib_handle_event(struct work_struct *_work)
Eli Cohene126ba92013-07-07 17:25:49 +03004415{
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02004416 struct mlx5_ib_event_work *work =
4417 container_of(_work, struct mlx5_ib_event_work, work);
4418 struct mlx5_ib_dev *ibdev;
Eli Cohene126ba92013-07-07 17:25:49 +03004419 struct ib_event ibev;
Eli Cohendbaaff22016-10-27 16:36:44 +03004420 bool fatal = false;
Eli Cohene126ba92013-07-07 17:25:49 +03004421
Saeed Mahameeddf097a22018-11-26 14:39:00 -08004422 if (work->is_slave) {
4423 ibdev = mlx5_ib_get_ibdev_from_mpi(work->mpi);
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02004424 if (!ibdev)
4425 goto out;
4426 } else {
Saeed Mahameeddf097a22018-11-26 14:39:00 -08004427 ibdev = work->dev;
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02004428 }
4429
4430 switch (work->event) {
Eli Cohene126ba92013-07-07 17:25:49 +03004431 case MLX5_DEV_EVENT_SYS_ERROR:
Eli Cohene126ba92013-07-07 17:25:49 +03004432 ibev.event = IB_EVENT_DEVICE_FATAL;
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03004433 mlx5_ib_handle_internal_error(ibdev);
Saeed Mahameed134e9342018-11-26 14:39:02 -08004434 ibev.element.port_num = (u8)(unsigned long)work->param;
Eli Cohendbaaff22016-10-27 16:36:44 +03004435 fatal = true;
Eli Cohene126ba92013-07-07 17:25:49 +03004436 break;
Saeed Mahameed134e9342018-11-26 14:39:02 -08004437 case MLX5_EVENT_TYPE_PORT_CHANGE:
4438 if (handle_port_change(ibdev, work->param, &ibev))
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02004439 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03004440 break;
Saeed Mahameed09e574f2018-11-26 14:39:04 -08004441 case MLX5_EVENT_TYPE_GENERAL_EVENT:
4442 handle_general_event(ibdev, work->param, &ibev);
4443 /* fall through */
Saeed Mahameedbdc37922016-09-29 19:35:38 +03004444 default:
Maor Gottlieb03404e82017-05-30 10:29:13 +03004445 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03004446 }
4447
Saeed Mahameed134e9342018-11-26 14:39:02 -08004448 ibev.device = &ibdev->ib_dev;
Eli Cohene126ba92013-07-07 17:25:49 +03004449
Saeed Mahameed134e9342018-11-26 14:39:02 -08004450 if (!rdma_is_port_valid(&ibdev->ib_dev, ibev.element.port_num)) {
4451 mlx5_ib_warn(ibdev, "warning: event on port %d\n", ibev.element.port_num);
Maor Gottlieb03404e82017-05-30 10:29:13 +03004452 goto out;
Eli Cohena0c84c32013-09-11 16:35:27 +03004453 }
4454
Eli Cohene126ba92013-07-07 17:25:49 +03004455 if (ibdev->ib_active)
4456 ib_dispatch_event(&ibev);
Eli Cohendbaaff22016-10-27 16:36:44 +03004457
4458 if (fatal)
4459 ibdev->ib_active = false;
Maor Gottlieb03404e82017-05-30 10:29:13 +03004460out:
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02004461 kfree(work);
4462}
4463
Saeed Mahameeddf097a22018-11-26 14:39:00 -08004464static int mlx5_ib_event(struct notifier_block *nb,
4465 unsigned long event, void *param)
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02004466{
4467 struct mlx5_ib_event_work *work;
4468
4469 work = kmalloc(sizeof(*work), GFP_ATOMIC);
Leon Romanovsky10bea9c2018-01-19 13:07:11 +02004470 if (!work)
Saeed Mahameeddf097a22018-11-26 14:39:00 -08004471 return NOTIFY_DONE;
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02004472
Leon Romanovsky10bea9c2018-01-19 13:07:11 +02004473 INIT_WORK(&work->work, mlx5_ib_handle_event);
Saeed Mahameeddf097a22018-11-26 14:39:00 -08004474 work->dev = container_of(nb, struct mlx5_ib_dev, mdev_events);
4475 work->is_slave = false;
Leon Romanovsky10bea9c2018-01-19 13:07:11 +02004476 work->param = param;
Leon Romanovsky10bea9c2018-01-19 13:07:11 +02004477 work->event = event;
4478
4479 queue_work(mlx5_ib_event_wq, &work->work);
Saeed Mahameeddf097a22018-11-26 14:39:00 -08004480
4481 return NOTIFY_OK;
4482}
4483
4484static int mlx5_ib_event_slave_port(struct notifier_block *nb,
4485 unsigned long event, void *param)
4486{
4487 struct mlx5_ib_event_work *work;
4488
4489 work = kmalloc(sizeof(*work), GFP_ATOMIC);
4490 if (!work)
4491 return NOTIFY_DONE;
4492
4493 INIT_WORK(&work->work, mlx5_ib_handle_event);
4494 work->mpi = container_of(nb, struct mlx5_ib_multiport_info, mdev_events);
4495 work->is_slave = true;
4496 work->param = param;
4497 work->event = event;
4498 queue_work(mlx5_ib_event_wq, &work->work);
4499
4500 return NOTIFY_OK;
Eli Cohene126ba92013-07-07 17:25:49 +03004501}
4502
Maor Gottliebc43f1112017-01-18 14:10:33 +02004503static int set_has_smi_cap(struct mlx5_ib_dev *dev)
4504{
4505 struct mlx5_hca_vport_context vport_ctx;
4506 int err;
4507 int port;
4508
Daniel Jurgens508562d2018-01-04 17:25:34 +02004509 for (port = 1; port <= dev->num_ports; port++) {
Maor Gottliebc43f1112017-01-18 14:10:33 +02004510 dev->mdev->port_caps[port - 1].has_smi = false;
4511 if (MLX5_CAP_GEN(dev->mdev, port_type) ==
4512 MLX5_CAP_PORT_TYPE_IB) {
4513 if (MLX5_CAP_GEN(dev->mdev, ib_virt)) {
4514 err = mlx5_query_hca_vport_context(dev->mdev, 0,
4515 port, 0,
4516 &vport_ctx);
4517 if (err) {
4518 mlx5_ib_err(dev, "query_hca_vport_context for port=%d failed %d\n",
4519 port, err);
4520 return err;
4521 }
4522 dev->mdev->port_caps[port - 1].has_smi =
4523 vport_ctx.has_smi;
4524 } else {
4525 dev->mdev->port_caps[port - 1].has_smi = true;
4526 }
4527 }
4528 }
4529 return 0;
4530}
4531
Eli Cohene126ba92013-07-07 17:25:49 +03004532static void get_ext_port_caps(struct mlx5_ib_dev *dev)
4533{
4534 int port;
4535
Daniel Jurgens508562d2018-01-04 17:25:34 +02004536 for (port = 1; port <= dev->num_ports; port++)
Eli Cohene126ba92013-07-07 17:25:49 +03004537 mlx5_query_ext_port_caps(dev, port);
4538}
4539
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004540static int get_port_caps(struct mlx5_ib_dev *dev, u8 port)
Eli Cohene126ba92013-07-07 17:25:49 +03004541{
4542 struct ib_device_attr *dprops = NULL;
4543 struct ib_port_attr *pprops = NULL;
Dan Carpenterf614fc12015-01-12 11:56:58 +03004544 int err = -ENOMEM;
Matan Barak2528e332015-06-11 16:35:25 +03004545 struct ib_udata uhw = {.inlen = 0, .outlen = 0};
Eli Cohene126ba92013-07-07 17:25:49 +03004546
4547 pprops = kmalloc(sizeof(*pprops), GFP_KERNEL);
4548 if (!pprops)
4549 goto out;
4550
4551 dprops = kmalloc(sizeof(*dprops), GFP_KERNEL);
4552 if (!dprops)
4553 goto out;
4554
Maor Gottliebc43f1112017-01-18 14:10:33 +02004555 err = set_has_smi_cap(dev);
4556 if (err)
4557 goto out;
4558
Matan Barak2528e332015-06-11 16:35:25 +03004559 err = mlx5_ib_query_device(&dev->ib_dev, dprops, &uhw);
Eli Cohene126ba92013-07-07 17:25:49 +03004560 if (err) {
4561 mlx5_ib_warn(dev, "query_device failed %d\n", err);
4562 goto out;
4563 }
4564
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004565 memset(pprops, 0, sizeof(*pprops));
4566 err = mlx5_ib_query_port(&dev->ib_dev, port, pprops);
4567 if (err) {
4568 mlx5_ib_warn(dev, "query_port %d failed %d\n",
4569 port, err);
4570 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03004571 }
4572
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004573 dev->mdev->port_caps[port - 1].pkey_table_len =
4574 dprops->max_pkeys;
4575 dev->mdev->port_caps[port - 1].gid_table_len =
4576 pprops->gid_tbl_len;
4577 mlx5_ib_dbg(dev, "port %d: pkey_table_len %d, gid_table_len %d\n",
4578 port, dprops->max_pkeys, pprops->gid_tbl_len);
4579
Eli Cohene126ba92013-07-07 17:25:49 +03004580out:
4581 kfree(pprops);
4582 kfree(dprops);
4583
4584 return err;
4585}
4586
4587static void destroy_umrc_res(struct mlx5_ib_dev *dev)
4588{
4589 int err;
4590
4591 err = mlx5_mr_cache_cleanup(dev);
4592 if (err)
4593 mlx5_ib_warn(dev, "mr cache cleanup failed\n");
4594
Mark Bloch32927e22018-03-20 15:45:37 +02004595 if (dev->umrc.qp)
4596 mlx5_ib_destroy_qp(dev->umrc.qp);
4597 if (dev->umrc.cq)
4598 ib_free_cq(dev->umrc.cq);
4599 if (dev->umrc.pd)
4600 ib_dealloc_pd(dev->umrc.pd);
Eli Cohene126ba92013-07-07 17:25:49 +03004601}
4602
4603enum {
4604 MAX_UMR_WR = 128,
4605};
4606
4607static int create_umr_res(struct mlx5_ib_dev *dev)
4608{
4609 struct ib_qp_init_attr *init_attr = NULL;
4610 struct ib_qp_attr *attr = NULL;
4611 struct ib_pd *pd;
4612 struct ib_cq *cq;
4613 struct ib_qp *qp;
Eli Cohene126ba92013-07-07 17:25:49 +03004614 int ret;
4615
4616 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
4617 init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL);
4618 if (!attr || !init_attr) {
4619 ret = -ENOMEM;
4620 goto error_0;
4621 }
4622
Christoph Hellwiged082d32016-09-05 12:56:17 +02004623 pd = ib_alloc_pd(&dev->ib_dev, 0);
Eli Cohene126ba92013-07-07 17:25:49 +03004624 if (IS_ERR(pd)) {
4625 mlx5_ib_dbg(dev, "Couldn't create PD for sync UMR QP\n");
4626 ret = PTR_ERR(pd);
4627 goto error_0;
4628 }
4629
Christoph Hellwigadd08d72016-03-03 09:38:22 +01004630 cq = ib_alloc_cq(&dev->ib_dev, NULL, 128, 0, IB_POLL_SOFTIRQ);
Eli Cohene126ba92013-07-07 17:25:49 +03004631 if (IS_ERR(cq)) {
4632 mlx5_ib_dbg(dev, "Couldn't create CQ for sync UMR QP\n");
4633 ret = PTR_ERR(cq);
4634 goto error_2;
4635 }
Eli Cohene126ba92013-07-07 17:25:49 +03004636
4637 init_attr->send_cq = cq;
4638 init_attr->recv_cq = cq;
4639 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
4640 init_attr->cap.max_send_wr = MAX_UMR_WR;
4641 init_attr->cap.max_send_sge = 1;
4642 init_attr->qp_type = MLX5_IB_QPT_REG_UMR;
4643 init_attr->port_num = 1;
4644 qp = mlx5_ib_create_qp(pd, init_attr, NULL);
4645 if (IS_ERR(qp)) {
4646 mlx5_ib_dbg(dev, "Couldn't create sync UMR QP\n");
4647 ret = PTR_ERR(qp);
4648 goto error_3;
4649 }
4650 qp->device = &dev->ib_dev;
4651 qp->real_qp = qp;
4652 qp->uobject = NULL;
4653 qp->qp_type = MLX5_IB_QPT_REG_UMR;
Majd Dibbiny31fde032017-10-30 14:23:13 +02004654 qp->send_cq = init_attr->send_cq;
4655 qp->recv_cq = init_attr->recv_cq;
Eli Cohene126ba92013-07-07 17:25:49 +03004656
4657 attr->qp_state = IB_QPS_INIT;
4658 attr->port_num = 1;
4659 ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_PKEY_INDEX |
4660 IB_QP_PORT, NULL);
4661 if (ret) {
4662 mlx5_ib_dbg(dev, "Couldn't modify UMR QP\n");
4663 goto error_4;
4664 }
4665
4666 memset(attr, 0, sizeof(*attr));
4667 attr->qp_state = IB_QPS_RTR;
4668 attr->path_mtu = IB_MTU_256;
4669
4670 ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
4671 if (ret) {
4672 mlx5_ib_dbg(dev, "Couldn't modify umr QP to rtr\n");
4673 goto error_4;
4674 }
4675
4676 memset(attr, 0, sizeof(*attr));
4677 attr->qp_state = IB_QPS_RTS;
4678 ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
4679 if (ret) {
4680 mlx5_ib_dbg(dev, "Couldn't modify umr QP to rts\n");
4681 goto error_4;
4682 }
4683
4684 dev->umrc.qp = qp;
4685 dev->umrc.cq = cq;
Eli Cohene126ba92013-07-07 17:25:49 +03004686 dev->umrc.pd = pd;
4687
4688 sema_init(&dev->umrc.sem, MAX_UMR_WR);
4689 ret = mlx5_mr_cache_init(dev);
4690 if (ret) {
4691 mlx5_ib_warn(dev, "mr cache init failed %d\n", ret);
4692 goto error_4;
4693 }
4694
4695 kfree(attr);
4696 kfree(init_attr);
4697
4698 return 0;
4699
4700error_4:
4701 mlx5_ib_destroy_qp(qp);
Mark Bloch32927e22018-03-20 15:45:37 +02004702 dev->umrc.qp = NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03004703
4704error_3:
Christoph Hellwigadd08d72016-03-03 09:38:22 +01004705 ib_free_cq(cq);
Mark Bloch32927e22018-03-20 15:45:37 +02004706 dev->umrc.cq = NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03004707
4708error_2:
Eli Cohene126ba92013-07-07 17:25:49 +03004709 ib_dealloc_pd(pd);
Mark Bloch32927e22018-03-20 15:45:37 +02004710 dev->umrc.pd = NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03004711
4712error_0:
4713 kfree(attr);
4714 kfree(init_attr);
4715 return ret;
4716}
4717
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03004718static u8 mlx5_get_umr_fence(u8 umr_fence_cap)
4719{
4720 switch (umr_fence_cap) {
4721 case MLX5_CAP_UMR_FENCE_NONE:
4722 return MLX5_FENCE_MODE_NONE;
4723 case MLX5_CAP_UMR_FENCE_SMALL:
4724 return MLX5_FENCE_MODE_INITIATOR_SMALL;
4725 default:
4726 return MLX5_FENCE_MODE_STRONG_ORDERING;
4727 }
4728}
4729
Eli Cohene126ba92013-07-07 17:25:49 +03004730static int create_dev_resources(struct mlx5_ib_resources *devr)
4731{
4732 struct ib_srq_init_attr attr;
4733 struct mlx5_ib_dev *dev;
Leon Romanovsky21a428a2019-02-03 14:55:51 +02004734 struct ib_device *ibdev;
Matan Barakbcf4c1e2015-06-11 16:35:20 +03004735 struct ib_cq_init_attr cq_attr = {.cqe = 1};
Haggai Eran7722f472016-02-29 15:45:07 +02004736 int port;
Eli Cohene126ba92013-07-07 17:25:49 +03004737 int ret = 0;
4738
4739 dev = container_of(devr, struct mlx5_ib_dev, devr);
Leon Romanovsky21a428a2019-02-03 14:55:51 +02004740 ibdev = &dev->ib_dev;
Eli Cohene126ba92013-07-07 17:25:49 +03004741
Haggai Erand16e91d2016-02-29 15:45:05 +02004742 mutex_init(&devr->mutex);
4743
Leon Romanovsky21a428a2019-02-03 14:55:51 +02004744 devr->p0 = rdma_zalloc_drv_obj(ibdev, ib_pd);
4745 if (!devr->p0)
4746 return -ENOMEM;
4747
4748 devr->p0->device = ibdev;
Eli Cohene126ba92013-07-07 17:25:49 +03004749 devr->p0->uobject = NULL;
4750 atomic_set(&devr->p0->usecnt, 0);
4751
Leon Romanovsky21a428a2019-02-03 14:55:51 +02004752 ret = mlx5_ib_alloc_pd(devr->p0, NULL, NULL);
4753 if (ret)
4754 goto error0;
4755
Matan Barakbcf4c1e2015-06-11 16:35:20 +03004756 devr->c0 = mlx5_ib_create_cq(&dev->ib_dev, &cq_attr, NULL, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03004757 if (IS_ERR(devr->c0)) {
4758 ret = PTR_ERR(devr->c0);
4759 goto error1;
4760 }
4761 devr->c0->device = &dev->ib_dev;
4762 devr->c0->uobject = NULL;
4763 devr->c0->comp_handler = NULL;
4764 devr->c0->event_handler = NULL;
4765 devr->c0->cq_context = NULL;
4766 atomic_set(&devr->c0->usecnt, 0);
4767
4768 devr->x0 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
4769 if (IS_ERR(devr->x0)) {
4770 ret = PTR_ERR(devr->x0);
4771 goto error2;
4772 }
4773 devr->x0->device = &dev->ib_dev;
4774 devr->x0->inode = NULL;
4775 atomic_set(&devr->x0->usecnt, 0);
4776 mutex_init(&devr->x0->tgt_qp_mutex);
4777 INIT_LIST_HEAD(&devr->x0->tgt_qp_list);
4778
4779 devr->x1 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
4780 if (IS_ERR(devr->x1)) {
4781 ret = PTR_ERR(devr->x1);
4782 goto error3;
4783 }
4784 devr->x1->device = &dev->ib_dev;
4785 devr->x1->inode = NULL;
4786 atomic_set(&devr->x1->usecnt, 0);
4787 mutex_init(&devr->x1->tgt_qp_mutex);
4788 INIT_LIST_HEAD(&devr->x1->tgt_qp_list);
4789
4790 memset(&attr, 0, sizeof(attr));
4791 attr.attr.max_sge = 1;
4792 attr.attr.max_wr = 1;
4793 attr.srq_type = IB_SRQT_XRC;
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03004794 attr.ext.cq = devr->c0;
Eli Cohene126ba92013-07-07 17:25:49 +03004795 attr.ext.xrc.xrcd = devr->x0;
4796
4797 devr->s0 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
4798 if (IS_ERR(devr->s0)) {
4799 ret = PTR_ERR(devr->s0);
4800 goto error4;
4801 }
4802 devr->s0->device = &dev->ib_dev;
4803 devr->s0->pd = devr->p0;
4804 devr->s0->uobject = NULL;
4805 devr->s0->event_handler = NULL;
4806 devr->s0->srq_context = NULL;
4807 devr->s0->srq_type = IB_SRQT_XRC;
4808 devr->s0->ext.xrc.xrcd = devr->x0;
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03004809 devr->s0->ext.cq = devr->c0;
Eli Cohene126ba92013-07-07 17:25:49 +03004810 atomic_inc(&devr->s0->ext.xrc.xrcd->usecnt);
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03004811 atomic_inc(&devr->s0->ext.cq->usecnt);
Eli Cohene126ba92013-07-07 17:25:49 +03004812 atomic_inc(&devr->p0->usecnt);
4813 atomic_set(&devr->s0->usecnt, 0);
4814
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03004815 memset(&attr, 0, sizeof(attr));
4816 attr.attr.max_sge = 1;
4817 attr.attr.max_wr = 1;
4818 attr.srq_type = IB_SRQT_BASIC;
4819 devr->s1 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
4820 if (IS_ERR(devr->s1)) {
4821 ret = PTR_ERR(devr->s1);
4822 goto error5;
4823 }
4824 devr->s1->device = &dev->ib_dev;
4825 devr->s1->pd = devr->p0;
4826 devr->s1->uobject = NULL;
4827 devr->s1->event_handler = NULL;
4828 devr->s1->srq_context = NULL;
4829 devr->s1->srq_type = IB_SRQT_BASIC;
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03004830 devr->s1->ext.cq = devr->c0;
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03004831 atomic_inc(&devr->p0->usecnt);
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03004832 atomic_set(&devr->s1->usecnt, 0);
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03004833
Haggai Eran7722f472016-02-29 15:45:07 +02004834 for (port = 0; port < ARRAY_SIZE(devr->ports); ++port) {
4835 INIT_WORK(&devr->ports[port].pkey_change_work,
4836 pkey_change_handler);
4837 devr->ports[port].devr = devr;
4838 }
4839
Eli Cohene126ba92013-07-07 17:25:49 +03004840 return 0;
4841
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03004842error5:
4843 mlx5_ib_destroy_srq(devr->s0);
Eli Cohene126ba92013-07-07 17:25:49 +03004844error4:
4845 mlx5_ib_dealloc_xrcd(devr->x1);
4846error3:
4847 mlx5_ib_dealloc_xrcd(devr->x0);
4848error2:
4849 mlx5_ib_destroy_cq(devr->c0);
4850error1:
4851 mlx5_ib_dealloc_pd(devr->p0);
4852error0:
Leon Romanovsky21a428a2019-02-03 14:55:51 +02004853 kfree(devr->p0);
Eli Cohene126ba92013-07-07 17:25:49 +03004854 return ret;
4855}
4856
4857static void destroy_dev_resources(struct mlx5_ib_resources *devr)
4858{
Haggai Eran7722f472016-02-29 15:45:07 +02004859 struct mlx5_ib_dev *dev =
4860 container_of(devr, struct mlx5_ib_dev, devr);
4861 int port;
4862
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03004863 mlx5_ib_destroy_srq(devr->s1);
Eli Cohene126ba92013-07-07 17:25:49 +03004864 mlx5_ib_destroy_srq(devr->s0);
4865 mlx5_ib_dealloc_xrcd(devr->x0);
4866 mlx5_ib_dealloc_xrcd(devr->x1);
4867 mlx5_ib_destroy_cq(devr->c0);
4868 mlx5_ib_dealloc_pd(devr->p0);
Leon Romanovsky21a428a2019-02-03 14:55:51 +02004869 kfree(devr->p0);
Haggai Eran7722f472016-02-29 15:45:07 +02004870
4871 /* Make sure no change P_Key work items are still executing */
4872 for (port = 0; port < dev->num_ports; ++port)
4873 cancel_work_sync(&devr->ports[port].pkey_change_work);
Eli Cohene126ba92013-07-07 17:25:49 +03004874}
4875
Artemy Kovalyovb02289b2018-07-04 15:57:50 +03004876static u32 get_core_cap_flags(struct ib_device *ibdev,
4877 struct mlx5_hca_vport_context *rep)
Achiad Shochate53505a2015-12-23 18:47:25 +02004878{
4879 struct mlx5_ib_dev *dev = to_mdev(ibdev);
4880 enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, 1);
4881 u8 l3_type_cap = MLX5_CAP_ROCE(dev->mdev, l3_type);
4882 u8 roce_version_cap = MLX5_CAP_ROCE(dev->mdev, roce_version);
Daniel Jurgens85c7c012018-01-04 17:25:43 +02004883 bool raw_support = !mlx5_core_mp_enabled(dev->mdev);
Achiad Shochate53505a2015-12-23 18:47:25 +02004884 u32 ret = 0;
4885
Artemy Kovalyovb02289b2018-07-04 15:57:50 +03004886 if (rep->grh_required)
4887 ret |= RDMA_CORE_CAP_IB_GRH_REQUIRED;
4888
Achiad Shochate53505a2015-12-23 18:47:25 +02004889 if (ll == IB_LINK_LAYER_INFINIBAND)
Artemy Kovalyovb02289b2018-07-04 15:57:50 +03004890 return ret | RDMA_CORE_PORT_IBA_IB;
Achiad Shochate53505a2015-12-23 18:47:25 +02004891
Daniel Jurgens85c7c012018-01-04 17:25:43 +02004892 if (raw_support)
Artemy Kovalyovb02289b2018-07-04 15:57:50 +03004893 ret |= RDMA_CORE_PORT_RAW_PACKET;
Or Gerlitz72cd5712017-01-24 13:02:36 +02004894
Achiad Shochate53505a2015-12-23 18:47:25 +02004895 if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV4_CAP))
Or Gerlitz72cd5712017-01-24 13:02:36 +02004896 return ret;
Achiad Shochate53505a2015-12-23 18:47:25 +02004897
4898 if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV6_CAP))
Or Gerlitz72cd5712017-01-24 13:02:36 +02004899 return ret;
Achiad Shochate53505a2015-12-23 18:47:25 +02004900
4901 if (roce_version_cap & MLX5_ROCE_VERSION_1_CAP)
4902 ret |= RDMA_CORE_PORT_IBA_ROCE;
4903
4904 if (roce_version_cap & MLX5_ROCE_VERSION_2_CAP)
4905 ret |= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
4906
4907 return ret;
4908}
4909
Ira Weiny77386132015-05-13 20:02:58 -04004910static int mlx5_port_immutable(struct ib_device *ibdev, u8 port_num,
4911 struct ib_port_immutable *immutable)
4912{
4913 struct ib_port_attr attr;
Or Gerlitzca5b91d2016-11-27 16:51:36 +02004914 struct mlx5_ib_dev *dev = to_mdev(ibdev);
4915 enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, port_num);
Artemy Kovalyovb02289b2018-07-04 15:57:50 +03004916 struct mlx5_hca_vport_context rep = {0};
Ira Weiny77386132015-05-13 20:02:58 -04004917 int err;
4918
Or Gerlitzc4550c62017-01-24 13:02:39 +02004919 err = ib_query_port(ibdev, port_num, &attr);
Ira Weiny77386132015-05-13 20:02:58 -04004920 if (err)
4921 return err;
4922
Artemy Kovalyovb02289b2018-07-04 15:57:50 +03004923 if (ll == IB_LINK_LAYER_INFINIBAND) {
4924 err = mlx5_query_hca_vport_context(dev->mdev, 0, port_num, 0,
4925 &rep);
4926 if (err)
4927 return err;
4928 }
4929
Ira Weiny77386132015-05-13 20:02:58 -04004930 immutable->pkey_tbl_len = attr.pkey_tbl_len;
4931 immutable->gid_tbl_len = attr.gid_tbl_len;
Artemy Kovalyovb02289b2018-07-04 15:57:50 +03004932 immutable->core_cap_flags = get_core_cap_flags(ibdev, &rep);
Or Gerlitzca5b91d2016-11-27 16:51:36 +02004933 if ((ll == IB_LINK_LAYER_INFINIBAND) || MLX5_CAP_GEN(dev->mdev, roce))
4934 immutable->max_mad_size = IB_MGMT_MAD_SIZE;
Ira Weiny77386132015-05-13 20:02:58 -04004935
4936 return 0;
4937}
4938
Mark Bloch8e6efa32017-11-06 12:22:13 +00004939static int mlx5_port_rep_immutable(struct ib_device *ibdev, u8 port_num,
4940 struct ib_port_immutable *immutable)
4941{
4942 struct ib_port_attr attr;
4943 int err;
4944
4945 immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET;
4946
4947 err = ib_query_port(ibdev, port_num, &attr);
4948 if (err)
4949 return err;
4950
4951 immutable->pkey_tbl_len = attr.pkey_tbl_len;
4952 immutable->gid_tbl_len = attr.gid_tbl_len;
4953 immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET;
4954
4955 return 0;
4956}
4957
Leon Romanovsky9abb0d12017-06-27 16:49:53 +03004958static void get_dev_fw_str(struct ib_device *ibdev, char *str)
Ira Weinyc7342822016-06-15 02:22:01 -04004959{
4960 struct mlx5_ib_dev *dev =
4961 container_of(ibdev, struct mlx5_ib_dev, ib_dev);
Leon Romanovsky9abb0d12017-06-27 16:49:53 +03004962 snprintf(str, IB_FW_VERSION_NAME_MAX, "%d.%d.%04d",
4963 fw_rev_maj(dev->mdev), fw_rev_min(dev->mdev),
4964 fw_rev_sub(dev->mdev));
Ira Weinyc7342822016-06-15 02:22:01 -04004965}
4966
Or Gerlitz45f95ac2016-11-27 16:51:35 +02004967static int mlx5_eth_lag_init(struct mlx5_ib_dev *dev)
Aviv Heller9ef9c642016-09-18 20:48:01 +03004968{
4969 struct mlx5_core_dev *mdev = dev->mdev;
4970 struct mlx5_flow_namespace *ns = mlx5_get_flow_namespace(mdev,
4971 MLX5_FLOW_NAMESPACE_LAG);
4972 struct mlx5_flow_table *ft;
4973 int err;
4974
Aviv Heller7c34ec12018-08-23 13:47:53 +03004975 if (!ns || !mlx5_lag_is_roce(mdev))
Aviv Heller9ef9c642016-09-18 20:48:01 +03004976 return 0;
4977
4978 err = mlx5_cmd_create_vport_lag(mdev);
4979 if (err)
4980 return err;
4981
4982 ft = mlx5_create_lag_demux_flow_table(ns, 0, 0);
4983 if (IS_ERR(ft)) {
4984 err = PTR_ERR(ft);
4985 goto err_destroy_vport_lag;
4986 }
4987
Mark Bloch9a4ca382018-01-16 14:42:35 +00004988 dev->flow_db->lag_demux_ft = ft;
Aviv Heller7c34ec12018-08-23 13:47:53 +03004989 dev->lag_active = true;
Aviv Heller9ef9c642016-09-18 20:48:01 +03004990 return 0;
4991
4992err_destroy_vport_lag:
4993 mlx5_cmd_destroy_vport_lag(mdev);
4994 return err;
4995}
4996
Or Gerlitz45f95ac2016-11-27 16:51:35 +02004997static void mlx5_eth_lag_cleanup(struct mlx5_ib_dev *dev)
Aviv Heller9ef9c642016-09-18 20:48:01 +03004998{
4999 struct mlx5_core_dev *mdev = dev->mdev;
5000
Aviv Heller7c34ec12018-08-23 13:47:53 +03005001 if (dev->lag_active) {
5002 dev->lag_active = false;
5003
Mark Bloch9a4ca382018-01-16 14:42:35 +00005004 mlx5_destroy_flow_table(dev->flow_db->lag_demux_ft);
5005 dev->flow_db->lag_demux_ft = NULL;
Aviv Heller9ef9c642016-09-18 20:48:01 +03005006
5007 mlx5_cmd_destroy_vport_lag(mdev);
5008 }
5009}
5010
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +02005011static int mlx5_add_netdev_notifier(struct mlx5_ib_dev *dev, u8 port_num)
Achiad Shochatfc24fc52015-12-23 18:47:17 +02005012{
Achiad Shochate53505a2015-12-23 18:47:25 +02005013 int err;
5014
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +02005015 dev->roce[port_num].nb.notifier_call = mlx5_netdev_event;
5016 err = register_netdevice_notifier(&dev->roce[port_num].nb);
Aviv Heller5ec8c832016-09-18 20:48:00 +03005017 if (err) {
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +02005018 dev->roce[port_num].nb.notifier_call = NULL;
Achiad Shochate53505a2015-12-23 18:47:25 +02005019 return err;
Aviv Heller5ec8c832016-09-18 20:48:00 +03005020 }
Achiad Shochate53505a2015-12-23 18:47:25 +02005021
Or Gerlitzd012f5d2016-11-27 16:51:34 +02005022 return 0;
5023}
Achiad Shochate53505a2015-12-23 18:47:25 +02005024
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +02005025static void mlx5_remove_netdev_notifier(struct mlx5_ib_dev *dev, u8 port_num)
Eli Cohene126ba92013-07-07 17:25:49 +03005026{
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +02005027 if (dev->roce[port_num].nb.notifier_call) {
5028 unregister_netdevice_notifier(&dev->roce[port_num].nb);
5029 dev->roce[port_num].nb.notifier_call = NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03005030 }
5031}
5032
Leon Romanovskye3f1ed12018-07-08 12:55:43 +03005033static int mlx5_enable_eth(struct mlx5_ib_dev *dev)
Eli Cohene126ba92013-07-07 17:25:49 +03005034{
Eli Cohene126ba92013-07-07 17:25:49 +03005035 int err;
5036
Or Gerlitzca5b91d2016-11-27 16:51:36 +02005037 if (MLX5_CAP_GEN(dev->mdev, roce)) {
5038 err = mlx5_nic_vport_enable_roce(dev->mdev);
5039 if (err)
Mark Bloch8e6efa32017-11-06 12:22:13 +00005040 return err;
Or Gerlitzca5b91d2016-11-27 16:51:36 +02005041 }
Achiad Shochate53505a2015-12-23 18:47:25 +02005042
Or Gerlitz45f95ac2016-11-27 16:51:35 +02005043 err = mlx5_eth_lag_init(dev);
Aviv Heller9ef9c642016-09-18 20:48:01 +03005044 if (err)
5045 goto err_disable_roce;
5046
Achiad Shochate53505a2015-12-23 18:47:25 +02005047 return 0;
5048
Aviv Heller9ef9c642016-09-18 20:48:01 +03005049err_disable_roce:
Or Gerlitzca5b91d2016-11-27 16:51:36 +02005050 if (MLX5_CAP_GEN(dev->mdev, roce))
5051 mlx5_nic_vport_disable_roce(dev->mdev);
Aviv Heller9ef9c642016-09-18 20:48:01 +03005052
Achiad Shochate53505a2015-12-23 18:47:25 +02005053 return err;
Achiad Shochatfc24fc52015-12-23 18:47:17 +02005054}
5055
Or Gerlitz45f95ac2016-11-27 16:51:35 +02005056static void mlx5_disable_eth(struct mlx5_ib_dev *dev)
Achiad Shochatfc24fc52015-12-23 18:47:17 +02005057{
Or Gerlitz45f95ac2016-11-27 16:51:35 +02005058 mlx5_eth_lag_cleanup(dev);
Or Gerlitzca5b91d2016-11-27 16:51:36 +02005059 if (MLX5_CAP_GEN(dev->mdev, roce))
5060 mlx5_nic_vport_disable_roce(dev->mdev);
Achiad Shochatfc24fc52015-12-23 18:47:17 +02005061}
5062
Parav Pandite1f24a72017-04-16 07:29:29 +03005063struct mlx5_ib_counter {
Kamal Heib7c16f472017-01-18 15:25:09 +02005064 const char *name;
5065 size_t offset;
5066};
5067
5068#define INIT_Q_COUNTER(_name) \
5069 { .name = #_name, .offset = MLX5_BYTE_OFF(query_q_counter_out, _name)}
5070
Parav Pandite1f24a72017-04-16 07:29:29 +03005071static const struct mlx5_ib_counter basic_q_cnts[] = {
Kamal Heib7c16f472017-01-18 15:25:09 +02005072 INIT_Q_COUNTER(rx_write_requests),
5073 INIT_Q_COUNTER(rx_read_requests),
5074 INIT_Q_COUNTER(rx_atomic_requests),
5075 INIT_Q_COUNTER(out_of_buffer),
5076};
5077
Parav Pandite1f24a72017-04-16 07:29:29 +03005078static const struct mlx5_ib_counter out_of_seq_q_cnts[] = {
Kamal Heib7c16f472017-01-18 15:25:09 +02005079 INIT_Q_COUNTER(out_of_sequence),
5080};
5081
Parav Pandite1f24a72017-04-16 07:29:29 +03005082static const struct mlx5_ib_counter retrans_q_cnts[] = {
Kamal Heib7c16f472017-01-18 15:25:09 +02005083 INIT_Q_COUNTER(duplicate_request),
5084 INIT_Q_COUNTER(rnr_nak_retry_err),
5085 INIT_Q_COUNTER(packet_seq_err),
5086 INIT_Q_COUNTER(implied_nak_seq_err),
5087 INIT_Q_COUNTER(local_ack_timeout_err),
5088};
5089
Parav Pandite1f24a72017-04-16 07:29:29 +03005090#define INIT_CONG_COUNTER(_name) \
5091 { .name = #_name, .offset = \
5092 MLX5_BYTE_OFF(query_cong_statistics_out, _name ## _high)}
5093
5094static const struct mlx5_ib_counter cong_cnts[] = {
5095 INIT_CONG_COUNTER(rp_cnp_ignored),
5096 INIT_CONG_COUNTER(rp_cnp_handled),
5097 INIT_CONG_COUNTER(np_ecn_marked_roce_packets),
5098 INIT_CONG_COUNTER(np_cnp_sent),
5099};
5100
Parav Pandit58dcb602017-06-19 07:19:37 +03005101static const struct mlx5_ib_counter extended_err_cnts[] = {
5102 INIT_Q_COUNTER(resp_local_length_error),
5103 INIT_Q_COUNTER(resp_cqe_error),
5104 INIT_Q_COUNTER(req_cqe_error),
5105 INIT_Q_COUNTER(req_remote_invalid_request),
5106 INIT_Q_COUNTER(req_remote_access_errors),
5107 INIT_Q_COUNTER(resp_remote_access_errors),
5108 INIT_Q_COUNTER(resp_cqe_flush_error),
5109 INIT_Q_COUNTER(req_cqe_flush_error),
5110};
5111
Talat Batheesh9f876f32018-06-21 15:37:56 +03005112#define INIT_EXT_PPCNT_COUNTER(_name) \
5113 { .name = #_name, .offset = \
5114 MLX5_BYTE_OFF(ppcnt_reg, \
5115 counter_set.eth_extended_cntrs_grp_data_layout._name##_high)}
5116
5117static const struct mlx5_ib_counter ext_ppcnt_cnts[] = {
5118 INIT_EXT_PPCNT_COUNTER(rx_icrc_encapsulated),
5119};
5120
Parav Pandite1f24a72017-04-16 07:29:29 +03005121static void mlx5_ib_dealloc_counters(struct mlx5_ib_dev *dev)
Mark Bloch0837e862016-06-17 15:10:55 +03005122{
Daniel Jurgensaac44922018-01-04 17:25:40 +02005123 int i;
Mark Bloch0837e862016-06-17 15:10:55 +03005124
Kamal Heib7c16f472017-01-18 15:25:09 +02005125 for (i = 0; i < dev->num_ports; i++) {
Parav Pandit921c0f52018-07-08 13:40:30 +03005126 if (dev->port[i].cnts.set_id_valid)
Daniel Jurgensaac44922018-01-04 17:25:40 +02005127 mlx5_core_dealloc_q_counter(dev->mdev,
5128 dev->port[i].cnts.set_id);
Parav Pandite1f24a72017-04-16 07:29:29 +03005129 kfree(dev->port[i].cnts.names);
5130 kfree(dev->port[i].cnts.offsets);
Kamal Heib7c16f472017-01-18 15:25:09 +02005131 }
5132}
5133
Parav Pandite1f24a72017-04-16 07:29:29 +03005134static int __mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev,
5135 struct mlx5_ib_counters *cnts)
Kamal Heib7c16f472017-01-18 15:25:09 +02005136{
5137 u32 num_counters;
5138
5139 num_counters = ARRAY_SIZE(basic_q_cnts);
5140
5141 if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt))
5142 num_counters += ARRAY_SIZE(out_of_seq_q_cnts);
5143
5144 if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters))
5145 num_counters += ARRAY_SIZE(retrans_q_cnts);
Parav Pandit58dcb602017-06-19 07:19:37 +03005146
5147 if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters))
5148 num_counters += ARRAY_SIZE(extended_err_cnts);
5149
Parav Pandite1f24a72017-04-16 07:29:29 +03005150 cnts->num_q_counters = num_counters;
Kamal Heib7c16f472017-01-18 15:25:09 +02005151
Parav Pandite1f24a72017-04-16 07:29:29 +03005152 if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
5153 cnts->num_cong_counters = ARRAY_SIZE(cong_cnts);
5154 num_counters += ARRAY_SIZE(cong_cnts);
5155 }
Talat Batheesh9f876f32018-06-21 15:37:56 +03005156 if (MLX5_CAP_PCAM_FEATURE(dev->mdev, rx_icrc_encapsulated_counter)) {
5157 cnts->num_ext_ppcnt_counters = ARRAY_SIZE(ext_ppcnt_cnts);
5158 num_counters += ARRAY_SIZE(ext_ppcnt_cnts);
5159 }
Parav Pandite1f24a72017-04-16 07:29:29 +03005160 cnts->names = kcalloc(num_counters, sizeof(cnts->names), GFP_KERNEL);
5161 if (!cnts->names)
Kamal Heib7c16f472017-01-18 15:25:09 +02005162 return -ENOMEM;
5163
Parav Pandite1f24a72017-04-16 07:29:29 +03005164 cnts->offsets = kcalloc(num_counters,
5165 sizeof(cnts->offsets), GFP_KERNEL);
5166 if (!cnts->offsets)
Kamal Heib7c16f472017-01-18 15:25:09 +02005167 goto err_names;
5168
Kamal Heib7c16f472017-01-18 15:25:09 +02005169 return 0;
5170
5171err_names:
Parav Pandite1f24a72017-04-16 07:29:29 +03005172 kfree(cnts->names);
Daniel Jurgensaac44922018-01-04 17:25:40 +02005173 cnts->names = NULL;
Kamal Heib7c16f472017-01-18 15:25:09 +02005174 return -ENOMEM;
5175}
5176
Parav Pandite1f24a72017-04-16 07:29:29 +03005177static void mlx5_ib_fill_counters(struct mlx5_ib_dev *dev,
5178 const char **names,
5179 size_t *offsets)
Kamal Heib7c16f472017-01-18 15:25:09 +02005180{
5181 int i;
5182 int j = 0;
5183
5184 for (i = 0; i < ARRAY_SIZE(basic_q_cnts); i++, j++) {
5185 names[j] = basic_q_cnts[i].name;
5186 offsets[j] = basic_q_cnts[i].offset;
5187 }
5188
5189 if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt)) {
5190 for (i = 0; i < ARRAY_SIZE(out_of_seq_q_cnts); i++, j++) {
5191 names[j] = out_of_seq_q_cnts[i].name;
5192 offsets[j] = out_of_seq_q_cnts[i].offset;
5193 }
5194 }
5195
5196 if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters)) {
5197 for (i = 0; i < ARRAY_SIZE(retrans_q_cnts); i++, j++) {
5198 names[j] = retrans_q_cnts[i].name;
5199 offsets[j] = retrans_q_cnts[i].offset;
5200 }
5201 }
Parav Pandite1f24a72017-04-16 07:29:29 +03005202
Parav Pandit58dcb602017-06-19 07:19:37 +03005203 if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters)) {
5204 for (i = 0; i < ARRAY_SIZE(extended_err_cnts); i++, j++) {
5205 names[j] = extended_err_cnts[i].name;
5206 offsets[j] = extended_err_cnts[i].offset;
5207 }
5208 }
5209
Parav Pandite1f24a72017-04-16 07:29:29 +03005210 if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
5211 for (i = 0; i < ARRAY_SIZE(cong_cnts); i++, j++) {
5212 names[j] = cong_cnts[i].name;
5213 offsets[j] = cong_cnts[i].offset;
5214 }
5215 }
Talat Batheesh9f876f32018-06-21 15:37:56 +03005216
5217 if (MLX5_CAP_PCAM_FEATURE(dev->mdev, rx_icrc_encapsulated_counter)) {
5218 for (i = 0; i < ARRAY_SIZE(ext_ppcnt_cnts); i++, j++) {
5219 names[j] = ext_ppcnt_cnts[i].name;
5220 offsets[j] = ext_ppcnt_cnts[i].offset;
5221 }
5222 }
Mark Bloch0837e862016-06-17 15:10:55 +03005223}
5224
Parav Pandite1f24a72017-04-16 07:29:29 +03005225static int mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev)
Mark Bloch0837e862016-06-17 15:10:55 +03005226{
Daniel Jurgensaac44922018-01-04 17:25:40 +02005227 int err = 0;
Mark Bloch0837e862016-06-17 15:10:55 +03005228 int i;
Yishai Hadasaa74be62018-12-09 12:52:36 +02005229 bool is_shared;
5230
5231 is_shared = MLX5_CAP_GEN(dev->mdev, log_max_uctx) != 0;
Mark Bloch0837e862016-06-17 15:10:55 +03005232
5233 for (i = 0; i < dev->num_ports; i++) {
Daniel Jurgensaac44922018-01-04 17:25:40 +02005234 err = __mlx5_ib_alloc_counters(dev, &dev->port[i].cnts);
5235 if (err)
5236 goto err_alloc;
Kamal Heib7c16f472017-01-18 15:25:09 +02005237
Daniel Jurgensaac44922018-01-04 17:25:40 +02005238 mlx5_ib_fill_counters(dev, dev->port[i].cnts.names,
5239 dev->port[i].cnts.offsets);
5240
Yishai Hadasaa74be62018-12-09 12:52:36 +02005241 err = mlx5_cmd_alloc_q_counter(dev->mdev,
5242 &dev->port[i].cnts.set_id,
5243 is_shared ?
5244 MLX5_SHARED_RESOURCE_UID : 0);
Daniel Jurgensaac44922018-01-04 17:25:40 +02005245 if (err) {
Mark Bloch0837e862016-06-17 15:10:55 +03005246 mlx5_ib_warn(dev,
5247 "couldn't allocate queue counter for port %d, err %d\n",
Daniel Jurgensaac44922018-01-04 17:25:40 +02005248 i + 1, err);
5249 goto err_alloc;
Mark Bloch0837e862016-06-17 15:10:55 +03005250 }
Daniel Jurgensaac44922018-01-04 17:25:40 +02005251 dev->port[i].cnts.set_id_valid = true;
Mark Bloch0837e862016-06-17 15:10:55 +03005252 }
5253
5254 return 0;
5255
Daniel Jurgensaac44922018-01-04 17:25:40 +02005256err_alloc:
5257 mlx5_ib_dealloc_counters(dev);
5258 return err;
Mark Bloch0837e862016-06-17 15:10:55 +03005259}
5260
Mark Bloch0ad17a82016-06-17 15:10:56 +03005261static struct rdma_hw_stats *mlx5_ib_alloc_hw_stats(struct ib_device *ibdev,
5262 u8 port_num)
5263{
Kamal Heib7c16f472017-01-18 15:25:09 +02005264 struct mlx5_ib_dev *dev = to_mdev(ibdev);
5265 struct mlx5_ib_port *port = &dev->port[port_num - 1];
Mark Bloch0ad17a82016-06-17 15:10:56 +03005266
5267 /* We support only per port stats */
5268 if (port_num == 0)
5269 return NULL;
5270
Parav Pandite1f24a72017-04-16 07:29:29 +03005271 return rdma_alloc_hw_stats_struct(port->cnts.names,
5272 port->cnts.num_q_counters +
Talat Batheesh9f876f32018-06-21 15:37:56 +03005273 port->cnts.num_cong_counters +
5274 port->cnts.num_ext_ppcnt_counters,
Mark Bloch0ad17a82016-06-17 15:10:56 +03005275 RDMA_HW_STATS_DEFAULT_LIFESPAN);
5276}
5277
Daniel Jurgensaac44922018-01-04 17:25:40 +02005278static int mlx5_ib_query_q_counters(struct mlx5_core_dev *mdev,
Parav Pandite1f24a72017-04-16 07:29:29 +03005279 struct mlx5_ib_port *port,
5280 struct rdma_hw_stats *stats)
5281{
5282 int outlen = MLX5_ST_SZ_BYTES(query_q_counter_out);
5283 void *out;
5284 __be32 val;
5285 int ret, i;
5286
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03005287 out = kvzalloc(outlen, GFP_KERNEL);
Parav Pandite1f24a72017-04-16 07:29:29 +03005288 if (!out)
5289 return -ENOMEM;
5290
Daniel Jurgensaac44922018-01-04 17:25:40 +02005291 ret = mlx5_core_query_q_counter(mdev,
Parav Pandite1f24a72017-04-16 07:29:29 +03005292 port->cnts.set_id, 0,
5293 out, outlen);
5294 if (ret)
5295 goto free;
5296
5297 for (i = 0; i < port->cnts.num_q_counters; i++) {
5298 val = *(__be32 *)(out + port->cnts.offsets[i]);
5299 stats->value[i] = (u64)be32_to_cpu(val);
5300 }
5301
5302free:
5303 kvfree(out);
5304 return ret;
5305}
5306
Talat Batheesh9f876f32018-06-21 15:37:56 +03005307static int mlx5_ib_query_ext_ppcnt_counters(struct mlx5_ib_dev *dev,
5308 struct mlx5_ib_port *port,
5309 struct rdma_hw_stats *stats)
5310{
5311 int offset = port->cnts.num_q_counters + port->cnts.num_cong_counters;
5312 int sz = MLX5_ST_SZ_BYTES(ppcnt_reg);
5313 int ret, i;
5314 void *out;
5315
5316 out = kvzalloc(sz, GFP_KERNEL);
5317 if (!out)
5318 return -ENOMEM;
5319
5320 ret = mlx5_cmd_query_ext_ppcnt_counters(dev->mdev, out);
5321 if (ret)
5322 goto free;
5323
5324 for (i = 0; i < port->cnts.num_ext_ppcnt_counters; i++) {
5325 stats->value[i + offset] =
5326 be64_to_cpup((__be64 *)(out +
5327 port->cnts.offsets[i + offset]));
5328 }
5329
5330free:
5331 kvfree(out);
5332 return ret;
5333}
5334
Mark Bloch0ad17a82016-06-17 15:10:56 +03005335static int mlx5_ib_get_hw_stats(struct ib_device *ibdev,
5336 struct rdma_hw_stats *stats,
Kamal Heib7c16f472017-01-18 15:25:09 +02005337 u8 port_num, int index)
Mark Bloch0ad17a82016-06-17 15:10:56 +03005338{
5339 struct mlx5_ib_dev *dev = to_mdev(ibdev);
Kamal Heib7c16f472017-01-18 15:25:09 +02005340 struct mlx5_ib_port *port = &dev->port[port_num - 1];
Daniel Jurgensaac44922018-01-04 17:25:40 +02005341 struct mlx5_core_dev *mdev;
Parav Pandite1f24a72017-04-16 07:29:29 +03005342 int ret, num_counters;
Daniel Jurgensaac44922018-01-04 17:25:40 +02005343 u8 mdev_port_num;
Mark Bloch0ad17a82016-06-17 15:10:56 +03005344
Kamal Heib7c16f472017-01-18 15:25:09 +02005345 if (!stats)
Parav Pandite1f24a72017-04-16 07:29:29 +03005346 return -EINVAL;
Mark Bloch0ad17a82016-06-17 15:10:56 +03005347
Talat Batheesh9f876f32018-06-21 15:37:56 +03005348 num_counters = port->cnts.num_q_counters +
5349 port->cnts.num_cong_counters +
5350 port->cnts.num_ext_ppcnt_counters;
Daniel Jurgensaac44922018-01-04 17:25:40 +02005351
5352 /* q_counters are per IB device, query the master mdev */
5353 ret = mlx5_ib_query_q_counters(dev->mdev, port, stats);
Mark Bloch0ad17a82016-06-17 15:10:56 +03005354 if (ret)
Parav Pandite1f24a72017-04-16 07:29:29 +03005355 return ret;
Mark Bloch0ad17a82016-06-17 15:10:56 +03005356
Talat Batheesh9f876f32018-06-21 15:37:56 +03005357 if (MLX5_CAP_PCAM_FEATURE(dev->mdev, rx_icrc_encapsulated_counter)) {
5358 ret = mlx5_ib_query_ext_ppcnt_counters(dev, port, stats);
5359 if (ret)
5360 return ret;
5361 }
5362
Parav Pandite1f24a72017-04-16 07:29:29 +03005363 if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
Daniel Jurgensaac44922018-01-04 17:25:40 +02005364 mdev = mlx5_ib_get_native_port_mdev(dev, port_num,
5365 &mdev_port_num);
5366 if (!mdev) {
5367 /* If port is not affiliated yet, its in down state
5368 * which doesn't have any counters yet, so it would be
5369 * zero. So no need to read from the HCA.
5370 */
5371 goto done;
5372 }
Majd Dibbiny71a0ff62017-12-21 17:38:26 +02005373 ret = mlx5_lag_query_cong_counters(dev->mdev,
5374 stats->value +
5375 port->cnts.num_q_counters,
5376 port->cnts.num_cong_counters,
5377 port->cnts.offsets +
5378 port->cnts.num_q_counters);
Daniel Jurgensaac44922018-01-04 17:25:40 +02005379
5380 mlx5_ib_put_native_port_mdev(dev, port_num);
Parav Pandite1f24a72017-04-16 07:29:29 +03005381 if (ret)
5382 return ret;
Mark Bloch0ad17a82016-06-17 15:10:56 +03005383 }
Kamal Heib7c16f472017-01-18 15:25:09 +02005384
Daniel Jurgensaac44922018-01-04 17:25:40 +02005385done:
Parav Pandite1f24a72017-04-16 07:29:29 +03005386 return num_counters;
Mark Bloch0ad17a82016-06-17 15:10:56 +03005387}
5388
Denis Drozdovf6a8a192018-08-14 14:08:51 +03005389static int mlx5_ib_rn_get_params(struct ib_device *device, u8 port_num,
5390 enum rdma_netdev_t type,
5391 struct rdma_netdev_alloc_params *params)
Erez Shitrit693dfd52017-04-27 17:01:34 +03005392{
5393 if (type != RDMA_NETDEV_IPOIB)
Denis Drozdovf6a8a192018-08-14 14:08:51 +03005394 return -EOPNOTSUPP;
Erez Shitrit693dfd52017-04-27 17:01:34 +03005395
Denis Drozdovf6a8a192018-08-14 14:08:51 +03005396 return mlx5_rdma_rn_get_params(to_mdev(device)->mdev, device, params);
Erez Shitrit693dfd52017-04-27 17:01:34 +03005397}
5398
Maor Gottliebfe248c32017-05-30 10:29:14 +03005399static void delay_drop_debugfs_cleanup(struct mlx5_ib_dev *dev)
5400{
5401 if (!dev->delay_drop.dbg)
5402 return;
5403 debugfs_remove_recursive(dev->delay_drop.dbg->dir_debugfs);
5404 kfree(dev->delay_drop.dbg);
5405 dev->delay_drop.dbg = NULL;
5406}
5407
Maor Gottlieb03404e82017-05-30 10:29:13 +03005408static void cancel_delay_drop(struct mlx5_ib_dev *dev)
5409{
5410 if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
5411 return;
5412
5413 cancel_work_sync(&dev->delay_drop.delay_drop_work);
Maor Gottliebfe248c32017-05-30 10:29:14 +03005414 delay_drop_debugfs_cleanup(dev);
5415}
5416
5417static ssize_t delay_drop_timeout_read(struct file *filp, char __user *buf,
5418 size_t count, loff_t *pos)
5419{
5420 struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
5421 char lbuf[20];
5422 int len;
5423
5424 len = snprintf(lbuf, sizeof(lbuf), "%u\n", delay_drop->timeout);
5425 return simple_read_from_buffer(buf, count, pos, lbuf, len);
5426}
5427
5428static ssize_t delay_drop_timeout_write(struct file *filp, const char __user *buf,
5429 size_t count, loff_t *pos)
5430{
5431 struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
5432 u32 timeout;
5433 u32 var;
5434
5435 if (kstrtouint_from_user(buf, count, 0, &var))
5436 return -EFAULT;
5437
5438 timeout = min_t(u32, roundup(var, 100), MLX5_MAX_DELAY_DROP_TIMEOUT_MS *
5439 1000);
5440 if (timeout != var)
5441 mlx5_ib_dbg(delay_drop->dev, "Round delay drop timeout to %u usec\n",
5442 timeout);
5443
5444 delay_drop->timeout = timeout;
5445
5446 return count;
5447}
5448
5449static const struct file_operations fops_delay_drop_timeout = {
5450 .owner = THIS_MODULE,
5451 .open = simple_open,
5452 .write = delay_drop_timeout_write,
5453 .read = delay_drop_timeout_read,
5454};
5455
5456static int delay_drop_debugfs_init(struct mlx5_ib_dev *dev)
5457{
5458 struct mlx5_ib_dbg_delay_drop *dbg;
5459
5460 if (!mlx5_debugfs_root)
5461 return 0;
5462
5463 dbg = kzalloc(sizeof(*dbg), GFP_KERNEL);
5464 if (!dbg)
5465 return -ENOMEM;
5466
Sudip Mukherjeecbafad82017-09-18 12:28:48 +01005467 dev->delay_drop.dbg = dbg;
5468
Maor Gottliebfe248c32017-05-30 10:29:14 +03005469 dbg->dir_debugfs =
5470 debugfs_create_dir("delay_drop",
5471 dev->mdev->priv.dbg_root);
5472 if (!dbg->dir_debugfs)
Sudip Mukherjeecbafad82017-09-18 12:28:48 +01005473 goto out_debugfs;
Maor Gottliebfe248c32017-05-30 10:29:14 +03005474
5475 dbg->events_cnt_debugfs =
5476 debugfs_create_atomic_t("num_timeout_events", 0400,
5477 dbg->dir_debugfs,
5478 &dev->delay_drop.events_cnt);
5479 if (!dbg->events_cnt_debugfs)
5480 goto out_debugfs;
5481
5482 dbg->rqs_cnt_debugfs =
5483 debugfs_create_atomic_t("num_rqs", 0400,
5484 dbg->dir_debugfs,
5485 &dev->delay_drop.rqs_cnt);
5486 if (!dbg->rqs_cnt_debugfs)
5487 goto out_debugfs;
5488
5489 dbg->timeout_debugfs =
5490 debugfs_create_file("timeout", 0600,
5491 dbg->dir_debugfs,
5492 &dev->delay_drop,
5493 &fops_delay_drop_timeout);
5494 if (!dbg->timeout_debugfs)
5495 goto out_debugfs;
5496
5497 return 0;
5498
5499out_debugfs:
5500 delay_drop_debugfs_cleanup(dev);
5501 return -ENOMEM;
Maor Gottlieb03404e82017-05-30 10:29:13 +03005502}
5503
5504static void init_delay_drop(struct mlx5_ib_dev *dev)
5505{
5506 if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
5507 return;
5508
5509 mutex_init(&dev->delay_drop.lock);
5510 dev->delay_drop.dev = dev;
5511 dev->delay_drop.activate = false;
5512 dev->delay_drop.timeout = MLX5_MAX_DELAY_DROP_TIMEOUT_MS * 1000;
5513 INIT_WORK(&dev->delay_drop.delay_drop_work, delay_drop_handler);
Maor Gottliebfe248c32017-05-30 10:29:14 +03005514 atomic_set(&dev->delay_drop.rqs_cnt, 0);
5515 atomic_set(&dev->delay_drop.events_cnt, 0);
5516
5517 if (delay_drop_debugfs_init(dev))
5518 mlx5_ib_warn(dev, "Failed to init delay drop debugfs\n");
Maor Gottlieb03404e82017-05-30 10:29:13 +03005519}
5520
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005521/* The mlx5_ib_multiport_mutex should be held when calling this function */
5522static void mlx5_ib_unbind_slave_port(struct mlx5_ib_dev *ibdev,
5523 struct mlx5_ib_multiport_info *mpi)
5524{
5525 u8 port_num = mlx5_core_native_port_num(mpi->mdev) - 1;
5526 struct mlx5_ib_port *port = &ibdev->port[port_num];
5527 int comps;
5528 int err;
5529 int i;
5530
Parav Pandita9e546e2018-01-04 17:25:39 +02005531 mlx5_ib_cleanup_cong_debugfs(ibdev, port_num);
5532
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005533 spin_lock(&port->mp.mpi_lock);
5534 if (!mpi->ibdev) {
5535 spin_unlock(&port->mp.mpi_lock);
5536 return;
5537 }
Saeed Mahameeddf097a22018-11-26 14:39:00 -08005538
5539 if (mpi->mdev_events.notifier_call)
5540 mlx5_notifier_unregister(mpi->mdev, &mpi->mdev_events);
5541 mpi->mdev_events.notifier_call = NULL;
5542
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005543 mpi->ibdev = NULL;
5544
5545 spin_unlock(&port->mp.mpi_lock);
5546 mlx5_remove_netdev_notifier(ibdev, port_num);
5547 spin_lock(&port->mp.mpi_lock);
5548
5549 comps = mpi->mdev_refcnt;
5550 if (comps) {
5551 mpi->unaffiliate = true;
5552 init_completion(&mpi->unref_comp);
5553 spin_unlock(&port->mp.mpi_lock);
5554
5555 for (i = 0; i < comps; i++)
5556 wait_for_completion(&mpi->unref_comp);
5557
5558 spin_lock(&port->mp.mpi_lock);
5559 mpi->unaffiliate = false;
5560 }
5561
5562 port->mp.mpi = NULL;
5563
5564 list_add_tail(&mpi->list, &mlx5_ib_unaffiliated_port_list);
5565
5566 spin_unlock(&port->mp.mpi_lock);
5567
5568 err = mlx5_nic_vport_unaffiliate_multiport(mpi->mdev);
5569
5570 mlx5_ib_dbg(ibdev, "unaffiliated port %d\n", port_num + 1);
5571 /* Log an error, still needed to cleanup the pointers and add
5572 * it back to the list.
5573 */
5574 if (err)
5575 mlx5_ib_err(ibdev, "Failed to unaffiliate port %u\n",
5576 port_num + 1);
5577
5578 ibdev->roce[port_num].last_port_state = IB_PORT_DOWN;
5579}
5580
5581/* The mlx5_ib_multiport_mutex should be held when calling this function */
5582static bool mlx5_ib_bind_slave_port(struct mlx5_ib_dev *ibdev,
5583 struct mlx5_ib_multiport_info *mpi)
5584{
5585 u8 port_num = mlx5_core_native_port_num(mpi->mdev) - 1;
5586 int err;
5587
5588 spin_lock(&ibdev->port[port_num].mp.mpi_lock);
5589 if (ibdev->port[port_num].mp.mpi) {
Qing Huang25771882018-07-23 14:15:08 -07005590 mlx5_ib_dbg(ibdev, "port %d already affiliated.\n",
5591 port_num + 1);
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005592 spin_unlock(&ibdev->port[port_num].mp.mpi_lock);
5593 return false;
5594 }
5595
5596 ibdev->port[port_num].mp.mpi = mpi;
5597 mpi->ibdev = ibdev;
Saeed Mahameeddf097a22018-11-26 14:39:00 -08005598 mpi->mdev_events.notifier_call = NULL;
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005599 spin_unlock(&ibdev->port[port_num].mp.mpi_lock);
5600
5601 err = mlx5_nic_vport_affiliate_multiport(ibdev->mdev, mpi->mdev);
5602 if (err)
5603 goto unbind;
5604
5605 err = get_port_caps(ibdev, mlx5_core_native_port_num(mpi->mdev));
5606 if (err)
5607 goto unbind;
5608
5609 err = mlx5_add_netdev_notifier(ibdev, port_num);
5610 if (err) {
5611 mlx5_ib_err(ibdev, "failed adding netdev notifier for port %u\n",
5612 port_num + 1);
5613 goto unbind;
5614 }
5615
Saeed Mahameeddf097a22018-11-26 14:39:00 -08005616 mpi->mdev_events.notifier_call = mlx5_ib_event_slave_port;
5617 mlx5_notifier_register(mpi->mdev, &mpi->mdev_events);
5618
Greg Kroah-Hartman73eb8f02019-01-22 16:17:57 +01005619 mlx5_ib_init_cong_debugfs(ibdev, port_num);
Parav Pandita9e546e2018-01-04 17:25:39 +02005620
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005621 return true;
5622
5623unbind:
5624 mlx5_ib_unbind_slave_port(ibdev, mpi);
5625 return false;
5626}
5627
5628static int mlx5_ib_init_multiport_master(struct mlx5_ib_dev *dev)
5629{
5630 int port_num = mlx5_core_native_port_num(dev->mdev) - 1;
5631 enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev,
5632 port_num + 1);
5633 struct mlx5_ib_multiport_info *mpi;
5634 int err;
5635 int i;
5636
5637 if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
5638 return 0;
5639
5640 err = mlx5_query_nic_vport_system_image_guid(dev->mdev,
5641 &dev->sys_image_guid);
5642 if (err)
5643 return err;
5644
5645 err = mlx5_nic_vport_enable_roce(dev->mdev);
5646 if (err)
5647 return err;
5648
5649 mutex_lock(&mlx5_ib_multiport_mutex);
5650 for (i = 0; i < dev->num_ports; i++) {
5651 bool bound = false;
5652
5653 /* build a stub multiport info struct for the native port. */
5654 if (i == port_num) {
5655 mpi = kzalloc(sizeof(*mpi), GFP_KERNEL);
5656 if (!mpi) {
5657 mutex_unlock(&mlx5_ib_multiport_mutex);
5658 mlx5_nic_vport_disable_roce(dev->mdev);
5659 return -ENOMEM;
5660 }
5661
5662 mpi->is_master = true;
5663 mpi->mdev = dev->mdev;
5664 mpi->sys_image_guid = dev->sys_image_guid;
5665 dev->port[i].mp.mpi = mpi;
5666 mpi->ibdev = dev;
5667 mpi = NULL;
5668 continue;
5669 }
5670
5671 list_for_each_entry(mpi, &mlx5_ib_unaffiliated_port_list,
5672 list) {
5673 if (dev->sys_image_guid == mpi->sys_image_guid &&
5674 (mlx5_core_native_port_num(mpi->mdev) - 1) == i) {
5675 bound = mlx5_ib_bind_slave_port(dev, mpi);
5676 }
5677
5678 if (bound) {
5679 dev_dbg(&mpi->mdev->pdev->dev, "removing port from unaffiliated list.\n");
5680 mlx5_ib_dbg(dev, "port %d bound\n", i + 1);
5681 list_del(&mpi->list);
5682 break;
5683 }
5684 }
5685 if (!bound) {
5686 get_port_caps(dev, i + 1);
5687 mlx5_ib_dbg(dev, "no free port found for port %d\n",
5688 i + 1);
5689 }
5690 }
5691
5692 list_add_tail(&dev->ib_dev_list, &mlx5_ib_dev_list);
5693 mutex_unlock(&mlx5_ib_multiport_mutex);
5694 return err;
5695}
5696
5697static void mlx5_ib_cleanup_multiport_master(struct mlx5_ib_dev *dev)
5698{
5699 int port_num = mlx5_core_native_port_num(dev->mdev) - 1;
5700 enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev,
5701 port_num + 1);
5702 int i;
5703
5704 if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
5705 return;
5706
5707 mutex_lock(&mlx5_ib_multiport_mutex);
5708 for (i = 0; i < dev->num_ports; i++) {
5709 if (dev->port[i].mp.mpi) {
5710 /* Destroy the native port stub */
5711 if (i == port_num) {
5712 kfree(dev->port[i].mp.mpi);
5713 dev->port[i].mp.mpi = NULL;
5714 } else {
5715 mlx5_ib_dbg(dev, "unbinding port_num: %d\n", i + 1);
5716 mlx5_ib_unbind_slave_port(dev, dev->port[i].mp.mpi);
5717 }
5718 }
5719 }
5720
5721 mlx5_ib_dbg(dev, "removing from devlist\n");
5722 list_del(&dev->ib_dev_list);
5723 mutex_unlock(&mlx5_ib_multiport_mutex);
5724
5725 mlx5_nic_vport_disable_roce(dev->mdev);
5726}
5727
Jason Gunthorpe9a119cd2018-07-04 08:50:28 +03005728ADD_UVERBS_ATTRIBUTES_SIMPLE(
5729 mlx5_ib_dm,
5730 UVERBS_OBJECT_DM,
5731 UVERBS_METHOD_DM_ALLOC,
5732 UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_ALLOC_DM_RESP_START_OFFSET,
5733 UVERBS_ATTR_TYPE(u64),
Jason Gunthorpe83bb4442018-07-04 08:50:29 +03005734 UA_MANDATORY),
Jason Gunthorpe9a119cd2018-07-04 08:50:28 +03005735 UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_ALLOC_DM_RESP_PAGE_INDEX,
5736 UVERBS_ATTR_TYPE(u16),
Jason Gunthorpe83bb4442018-07-04 08:50:29 +03005737 UA_MANDATORY));
Ariel Levkovich24da0012018-04-05 18:53:27 +03005738
Jason Gunthorpe9a119cd2018-07-04 08:50:28 +03005739ADD_UVERBS_ATTRIBUTES_SIMPLE(
5740 mlx5_ib_flow_action,
5741 UVERBS_OBJECT_FLOW_ACTION,
5742 UVERBS_METHOD_FLOW_ACTION_ESP_CREATE,
Jason Gunthorpebccd0622018-07-26 16:37:14 -06005743 UVERBS_ATTR_FLAGS_IN(MLX5_IB_ATTR_CREATE_FLOW_ACTION_FLAGS,
5744 enum mlx5_ib_uapi_flow_action_flags));
Aviad Yehezkelc6475a02018-03-28 09:27:50 +03005745
Jason Gunthorpe0cbf4322018-11-12 22:59:50 +02005746static const struct uapi_definition mlx5_ib_defs[] = {
5747#if IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS)
Jason Gunthorpe36e235c2018-11-12 22:59:53 +02005748 UAPI_DEF_CHAIN(mlx5_ib_devx_defs),
Jason Gunthorpe0cbf4322018-11-12 22:59:50 +02005749 UAPI_DEF_CHAIN(mlx5_ib_flow_defs),
5750#endif
Matan Barak8c846602018-03-28 09:27:41 +03005751
Jason Gunthorpe0cbf4322018-11-12 22:59:50 +02005752 UAPI_DEF_CHAIN_OBJ_TREE(UVERBS_OBJECT_FLOW_ACTION,
5753 &mlx5_ib_flow_action),
5754 UAPI_DEF_CHAIN_OBJ_TREE(UVERBS_OBJECT_DM, &mlx5_ib_dm),
5755 {}
5756};
Matan Barak8c846602018-03-28 09:27:41 +03005757
Raed Salem1a1e03d2018-05-31 16:43:41 +03005758static int mlx5_ib_read_counters(struct ib_counters *counters,
5759 struct ib_counters_read_attr *read_attr,
5760 struct uverbs_attr_bundle *attrs)
5761{
5762 struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
5763 struct mlx5_read_counters_attr mread_attr = {};
5764 struct mlx5_ib_flow_counters_desc *desc;
5765 int ret, i;
5766
5767 mutex_lock(&mcounters->mcntrs_mutex);
5768 if (mcounters->cntrs_max_index > read_attr->ncounters) {
5769 ret = -EINVAL;
5770 goto err_bound;
5771 }
5772
5773 mread_attr.out = kcalloc(mcounters->counters_num, sizeof(u64),
5774 GFP_KERNEL);
5775 if (!mread_attr.out) {
5776 ret = -ENOMEM;
5777 goto err_bound;
5778 }
5779
5780 mread_attr.hw_cntrs_hndl = mcounters->hw_cntrs_hndl;
5781 mread_attr.flags = read_attr->flags;
5782 ret = mcounters->read_counters(counters->device, &mread_attr);
5783 if (ret)
5784 goto err_read;
5785
5786 /* do the pass over the counters data array to assign according to the
5787 * descriptions and indexing pairs
5788 */
5789 desc = mcounters->counters_data;
5790 for (i = 0; i < mcounters->ncounters; i++)
5791 read_attr->counters_buff[desc[i].index] += mread_attr.out[desc[i].description];
5792
5793err_read:
5794 kfree(mread_attr.out);
5795err_bound:
5796 mutex_unlock(&mcounters->mcntrs_mutex);
5797 return ret;
5798}
5799
Raed Salemb29e2a12018-05-31 16:43:38 +03005800static int mlx5_ib_destroy_counters(struct ib_counters *counters)
5801{
5802 struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
5803
Raed Salem3b3233f2018-05-31 16:43:39 +03005804 counters_clear_description(counters);
5805 if (mcounters->hw_cntrs_hndl)
5806 mlx5_fc_destroy(to_mdev(counters->device)->mdev,
5807 mcounters->hw_cntrs_hndl);
5808
Raed Salemb29e2a12018-05-31 16:43:38 +03005809 kfree(mcounters);
5810
5811 return 0;
5812}
5813
5814static struct ib_counters *mlx5_ib_create_counters(struct ib_device *device,
5815 struct uverbs_attr_bundle *attrs)
5816{
5817 struct mlx5_ib_mcounters *mcounters;
5818
5819 mcounters = kzalloc(sizeof(*mcounters), GFP_KERNEL);
5820 if (!mcounters)
5821 return ERR_PTR(-ENOMEM);
5822
Raed Salem3b3233f2018-05-31 16:43:39 +03005823 mutex_init(&mcounters->mcntrs_mutex);
5824
Raed Salemb29e2a12018-05-31 16:43:38 +03005825 return &mcounters->ibcntrs;
5826}
5827
Mark Blochb5ca15a2018-01-23 11:16:30 +00005828void mlx5_ib_stage_init_cleanup(struct mlx5_ib_dev *dev)
Eli Cohene126ba92013-07-07 17:25:49 +03005829{
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005830 mlx5_ib_cleanup_multiport_master(dev);
Leon Romanovsky13859d5d2019-01-08 16:07:26 +02005831 if (IS_ENABLED(CONFIG_INFINIBAND_ON_DEMAND_PAGING)) {
Yishai Hadas534fd7a2019-01-13 16:01:17 +02005832 srcu_barrier(&dev->mr_srcu);
Leon Romanovsky13859d5d2019-01-08 16:07:26 +02005833 cleanup_srcu_struct(&dev->mr_srcu);
Leon Romanovsky13859d5d2019-01-08 16:07:26 +02005834 }
Mark Bloch16c19752018-01-01 13:06:58 +02005835 kfree(dev->port);
5836}
5837
Mark Blochb5ca15a2018-01-23 11:16:30 +00005838int mlx5_ib_stage_init_init(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02005839{
5840 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03005841 int err;
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005842 int i;
Eli Cohene126ba92013-07-07 17:25:49 +03005843
Daniel Jurgens508562d2018-01-04 17:25:34 +02005844 dev->port = kcalloc(dev->num_ports, sizeof(*dev->port),
Mark Bloch0837e862016-06-17 15:10:55 +03005845 GFP_KERNEL);
5846 if (!dev->port)
Mark Bloch16c19752018-01-01 13:06:58 +02005847 return -ENOMEM;
Mark Bloch0837e862016-06-17 15:10:55 +03005848
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005849 for (i = 0; i < dev->num_ports; i++) {
5850 spin_lock_init(&dev->port[i].mp.mpi_lock);
5851 rwlock_init(&dev->roce[i].netdev_lock);
5852 }
5853
5854 err = mlx5_ib_init_multiport_master(dev);
Eli Cohene126ba92013-07-07 17:25:49 +03005855 if (err)
Mark Bloch0837e862016-06-17 15:10:55 +03005856 goto err_free_port;
Eli Cohene126ba92013-07-07 17:25:49 +03005857
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005858 if (!mlx5_core_mp_enabled(mdev)) {
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005859 for (i = 1; i <= dev->num_ports; i++) {
5860 err = get_port_caps(dev, i);
5861 if (err)
5862 break;
5863 }
5864 } else {
5865 err = get_port_caps(dev, mlx5_core_native_port_num(mdev));
5866 }
5867 if (err)
5868 goto err_mp;
5869
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03005870 if (mlx5_use_mad_ifc(dev))
5871 get_ext_port_caps(dev);
Eli Cohene126ba92013-07-07 17:25:49 +03005872
Eli Cohene126ba92013-07-07 17:25:49 +03005873 dev->ib_dev.owner = THIS_MODULE;
5874 dev->ib_dev.node_type = RDMA_NODE_IB_CA;
Sagi Grimbergc6790aa2015-09-24 10:34:23 +03005875 dev->ib_dev.local_dma_lkey = 0 /* not supported for now */;
Daniel Jurgens508562d2018-01-04 17:25:34 +02005876 dev->ib_dev.phys_port_cnt = dev->num_ports;
Saeed Mahameedf2f3df52018-11-19 10:52:38 -08005877 dev->ib_dev.num_comp_vectors = mlx5_comp_vectors_count(mdev);
Bart Van Assche9b0c2892017-01-20 13:04:21 -08005878 dev->ib_dev.dev.parent = &mdev->pdev->dev;
Eli Cohene126ba92013-07-07 17:25:49 +03005879
Mark Bloch3cc297d2018-01-01 13:07:03 +02005880 mutex_init(&dev->cap_mask_mutex);
5881 INIT_LIST_HEAD(&dev->qp_list);
5882 spin_lock_init(&dev->reset_flow_resource_lock);
5883
Ariel Levkovich24da0012018-04-05 18:53:27 +03005884 spin_lock_init(&dev->memic.memic_lock);
5885 dev->memic.dev = mdev;
5886
Leon Romanovsky13859d5d2019-01-08 16:07:26 +02005887 if (IS_ENABLED(CONFIG_INFINIBAND_ON_DEMAND_PAGING)) {
Leon Romanovsky13859d5d2019-01-08 16:07:26 +02005888 err = init_srcu_struct(&dev->mr_srcu);
Moni Shouaa6bc3872019-02-17 16:08:22 +02005889 if (err)
Leon Romanovsky13859d5d2019-01-08 16:07:26 +02005890 goto err_mp;
Jason Gunthorpe623d1542018-12-20 16:39:26 -07005891 }
Mark Bloch3cc297d2018-01-01 13:07:03 +02005892
Mark Bloch16c19752018-01-01 13:06:58 +02005893 return 0;
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005894err_mp:
5895 mlx5_ib_cleanup_multiport_master(dev);
Mark Bloch16c19752018-01-01 13:06:58 +02005896
5897err_free_port:
5898 kfree(dev->port);
5899
5900 return -ENOMEM;
5901}
5902
Mark Bloch9a4ca382018-01-16 14:42:35 +00005903static int mlx5_ib_stage_flow_db_init(struct mlx5_ib_dev *dev)
5904{
5905 dev->flow_db = kzalloc(sizeof(*dev->flow_db), GFP_KERNEL);
5906
5907 if (!dev->flow_db)
5908 return -ENOMEM;
5909
5910 mutex_init(&dev->flow_db->lock);
5911
5912 return 0;
5913}
5914
Mark Blochb5ca15a2018-01-23 11:16:30 +00005915int mlx5_ib_stage_rep_flow_db_init(struct mlx5_ib_dev *dev)
5916{
5917 struct mlx5_ib_dev *nic_dev;
5918
5919 nic_dev = mlx5_ib_get_uplink_ibdev(dev->mdev->priv.eswitch);
5920
5921 if (!nic_dev)
5922 return -EINVAL;
5923
5924 dev->flow_db = nic_dev->flow_db;
5925
5926 return 0;
5927}
5928
Mark Bloch9a4ca382018-01-16 14:42:35 +00005929static void mlx5_ib_stage_flow_db_cleanup(struct mlx5_ib_dev *dev)
5930{
5931 kfree(dev->flow_db);
5932}
5933
Kamal Heib96458233e2018-12-10 21:09:38 +02005934static const struct ib_device_ops mlx5_ib_dev_ops = {
5935 .add_gid = mlx5_ib_add_gid,
5936 .alloc_mr = mlx5_ib_alloc_mr,
5937 .alloc_pd = mlx5_ib_alloc_pd,
5938 .alloc_ucontext = mlx5_ib_alloc_ucontext,
5939 .attach_mcast = mlx5_ib_mcg_attach,
5940 .check_mr_status = mlx5_ib_check_mr_status,
5941 .create_ah = mlx5_ib_create_ah,
5942 .create_counters = mlx5_ib_create_counters,
5943 .create_cq = mlx5_ib_create_cq,
5944 .create_flow = mlx5_ib_create_flow,
5945 .create_qp = mlx5_ib_create_qp,
5946 .create_srq = mlx5_ib_create_srq,
5947 .dealloc_pd = mlx5_ib_dealloc_pd,
5948 .dealloc_ucontext = mlx5_ib_dealloc_ucontext,
5949 .del_gid = mlx5_ib_del_gid,
5950 .dereg_mr = mlx5_ib_dereg_mr,
5951 .destroy_ah = mlx5_ib_destroy_ah,
5952 .destroy_counters = mlx5_ib_destroy_counters,
5953 .destroy_cq = mlx5_ib_destroy_cq,
5954 .destroy_flow = mlx5_ib_destroy_flow,
5955 .destroy_flow_action = mlx5_ib_destroy_flow_action,
5956 .destroy_qp = mlx5_ib_destroy_qp,
5957 .destroy_srq = mlx5_ib_destroy_srq,
5958 .detach_mcast = mlx5_ib_mcg_detach,
5959 .disassociate_ucontext = mlx5_ib_disassociate_ucontext,
5960 .drain_rq = mlx5_ib_drain_rq,
5961 .drain_sq = mlx5_ib_drain_sq,
5962 .get_dev_fw_str = get_dev_fw_str,
5963 .get_dma_mr = mlx5_ib_get_dma_mr,
5964 .get_link_layer = mlx5_ib_port_link_layer,
5965 .map_mr_sg = mlx5_ib_map_mr_sg,
5966 .mmap = mlx5_ib_mmap,
5967 .modify_cq = mlx5_ib_modify_cq,
5968 .modify_device = mlx5_ib_modify_device,
5969 .modify_port = mlx5_ib_modify_port,
5970 .modify_qp = mlx5_ib_modify_qp,
5971 .modify_srq = mlx5_ib_modify_srq,
5972 .poll_cq = mlx5_ib_poll_cq,
5973 .post_recv = mlx5_ib_post_recv,
5974 .post_send = mlx5_ib_post_send,
5975 .post_srq_recv = mlx5_ib_post_srq_recv,
5976 .process_mad = mlx5_ib_process_mad,
5977 .query_ah = mlx5_ib_query_ah,
5978 .query_device = mlx5_ib_query_device,
5979 .query_gid = mlx5_ib_query_gid,
5980 .query_pkey = mlx5_ib_query_pkey,
5981 .query_qp = mlx5_ib_query_qp,
5982 .query_srq = mlx5_ib_query_srq,
5983 .read_counters = mlx5_ib_read_counters,
5984 .reg_user_mr = mlx5_ib_reg_user_mr,
5985 .req_notify_cq = mlx5_ib_arm_cq,
5986 .rereg_user_mr = mlx5_ib_rereg_user_mr,
5987 .resize_cq = mlx5_ib_resize_cq,
Leon Romanovsky21a428a2019-02-03 14:55:51 +02005988 INIT_RDMA_OBJ_SIZE(ib_pd, mlx5_ib_pd, ibpd),
Leon Romanovskya2a074e2019-02-12 20:39:16 +02005989 INIT_RDMA_OBJ_SIZE(ib_ucontext, mlx5_ib_ucontext, ibucontext),
Kamal Heib96458233e2018-12-10 21:09:38 +02005990};
5991
5992static const struct ib_device_ops mlx5_ib_dev_flow_ipsec_ops = {
5993 .create_flow_action_esp = mlx5_ib_create_flow_action_esp,
5994 .modify_flow_action_esp = mlx5_ib_modify_flow_action_esp,
5995};
5996
5997static const struct ib_device_ops mlx5_ib_dev_ipoib_enhanced_ops = {
5998 .rdma_netdev_get_params = mlx5_ib_rn_get_params,
5999};
6000
6001static const struct ib_device_ops mlx5_ib_dev_sriov_ops = {
6002 .get_vf_config = mlx5_ib_get_vf_config,
6003 .get_vf_stats = mlx5_ib_get_vf_stats,
6004 .set_vf_guid = mlx5_ib_set_vf_guid,
6005 .set_vf_link_state = mlx5_ib_set_vf_link_state,
6006};
6007
6008static const struct ib_device_ops mlx5_ib_dev_mw_ops = {
6009 .alloc_mw = mlx5_ib_alloc_mw,
6010 .dealloc_mw = mlx5_ib_dealloc_mw,
6011};
6012
6013static const struct ib_device_ops mlx5_ib_dev_xrc_ops = {
6014 .alloc_xrcd = mlx5_ib_alloc_xrcd,
6015 .dealloc_xrcd = mlx5_ib_dealloc_xrcd,
6016};
6017
6018static const struct ib_device_ops mlx5_ib_dev_dm_ops = {
6019 .alloc_dm = mlx5_ib_alloc_dm,
6020 .dealloc_dm = mlx5_ib_dealloc_dm,
6021 .reg_dm_mr = mlx5_ib_reg_dm_mr,
6022};
6023
Mark Blochb5ca15a2018-01-23 11:16:30 +00006024int mlx5_ib_stage_caps_init(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02006025{
6026 struct mlx5_core_dev *mdev = dev->mdev;
Mark Bloch16c19752018-01-01 13:06:58 +02006027 int err;
6028
Eli Cohene126ba92013-07-07 17:25:49 +03006029 dev->ib_dev.uverbs_abi_ver = MLX5_IB_UVERBS_ABI_VERSION;
6030 dev->ib_dev.uverbs_cmd_mask =
6031 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
6032 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
6033 (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
6034 (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
6035 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
Moni Shoua41c450f2016-11-23 08:23:26 +02006036 (1ull << IB_USER_VERBS_CMD_CREATE_AH) |
6037 (1ull << IB_USER_VERBS_CMD_DESTROY_AH) |
Eli Cohene126ba92013-07-07 17:25:49 +03006038 (1ull << IB_USER_VERBS_CMD_REG_MR) |
Noa Osherovich56e11d62016-02-29 16:46:51 +02006039 (1ull << IB_USER_VERBS_CMD_REREG_MR) |
Eli Cohene126ba92013-07-07 17:25:49 +03006040 (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
6041 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
6042 (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
6043 (1ull << IB_USER_VERBS_CMD_RESIZE_CQ) |
6044 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
6045 (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
6046 (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
6047 (1ull << IB_USER_VERBS_CMD_QUERY_QP) |
6048 (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
6049 (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST) |
6050 (1ull << IB_USER_VERBS_CMD_DETACH_MCAST) |
6051 (1ull << IB_USER_VERBS_CMD_CREATE_SRQ) |
6052 (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ) |
6053 (1ull << IB_USER_VERBS_CMD_QUERY_SRQ) |
6054 (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ) |
6055 (1ull << IB_USER_VERBS_CMD_CREATE_XSRQ) |
6056 (1ull << IB_USER_VERBS_CMD_OPEN_QP);
Haggai Eran1707cb42015-02-08 13:28:52 +02006057 dev->ib_dev.uverbs_ex_cmd_mask =
Matan Barakd4584dd2016-01-28 17:51:46 +02006058 (1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE) |
6059 (1ull << IB_USER_VERBS_EX_CMD_CREATE_CQ) |
Bodong Wang7d29f342016-12-01 13:43:16 +02006060 (1ull << IB_USER_VERBS_EX_CMD_CREATE_QP) |
Yonatan Cohenb0e9df62017-11-13 10:51:15 +02006061 (1ull << IB_USER_VERBS_EX_CMD_MODIFY_QP) |
Kamal Heib96458233e2018-12-10 21:09:38 +02006062 (1ull << IB_USER_VERBS_EX_CMD_MODIFY_CQ) |
6063 (1ull << IB_USER_VERBS_EX_CMD_CREATE_FLOW) |
6064 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_FLOW);
Eli Cohene126ba92013-07-07 17:25:49 +03006065
Denis Drozdovf6a8a192018-08-14 14:08:51 +03006066 if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads) &&
6067 IS_ENABLED(CONFIG_MLX5_CORE_IPOIB))
Kamal Heib96458233e2018-12-10 21:09:38 +02006068 ib_set_device_ops(&dev->ib_dev,
6069 &mlx5_ib_dev_ipoib_enhanced_ops);
Niranjana Vishwanathapura8e959602017-06-30 13:14:46 -07006070
Kamal Heib96458233e2018-12-10 21:09:38 +02006071 if (mlx5_core_is_pf(mdev))
6072 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_sriov_ops);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03006073
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03006074 dev->umr_fence = mlx5_get_umr_fence(MLX5_CAP_GEN(mdev, umr_fence));
6075
Matan Barakd2370e02016-02-29 18:05:30 +02006076 if (MLX5_CAP_GEN(mdev, imaicl)) {
Matan Barakd2370e02016-02-29 18:05:30 +02006077 dev->ib_dev.uverbs_cmd_mask |=
6078 (1ull << IB_USER_VERBS_CMD_ALLOC_MW) |
6079 (1ull << IB_USER_VERBS_CMD_DEALLOC_MW);
Kamal Heib96458233e2018-12-10 21:09:38 +02006080 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_mw_ops);
Matan Barakd2370e02016-02-29 18:05:30 +02006081 }
6082
Saeed Mahameed938fe832015-05-28 22:28:41 +03006083 if (MLX5_CAP_GEN(mdev, xrc)) {
Eli Cohene126ba92013-07-07 17:25:49 +03006084 dev->ib_dev.uverbs_cmd_mask |=
6085 (1ull << IB_USER_VERBS_CMD_OPEN_XRCD) |
6086 (1ull << IB_USER_VERBS_CMD_CLOSE_XRCD);
Kamal Heib96458233e2018-12-10 21:09:38 +02006087 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_xrc_ops);
Eli Cohene126ba92013-07-07 17:25:49 +03006088 }
6089
Kamal Heib96458233e2018-12-10 21:09:38 +02006090 if (MLX5_CAP_DEV_MEM(mdev, memic))
6091 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_dm_ops);
Ariel Levkovich24da0012018-04-05 18:53:27 +03006092
Jason Gunthorpedfb631a2018-11-12 22:59:49 +02006093 if (mlx5_accel_ipsec_device_caps(dev->mdev) &
Kamal Heib96458233e2018-12-10 21:09:38 +02006094 MLX5_ACCEL_IPSEC_CAP_DEVICE)
6095 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_flow_ipsec_ops);
Matan Barak0ede73b2018-03-19 15:02:34 +02006096 dev->ib_dev.driver_id = RDMA_DRIVER_MLX5;
Kamal Heib96458233e2018-12-10 21:09:38 +02006097 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_ops);
Yishai Hadas81e30882017-06-08 16:15:09 +03006098
Jason Gunthorpe36e235c2018-11-12 22:59:53 +02006099 if (IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS))
6100 dev->ib_dev.driver_def = mlx5_ib_defs;
Eli Cohene126ba92013-07-07 17:25:49 +03006101
6102 err = init_node_data(dev);
6103 if (err)
Mark Bloch16c19752018-01-01 13:06:58 +02006104 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03006105
Mark Blochc8b89922018-01-01 13:07:02 +02006106 if ((MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_ETH) &&
Jason Gunthorpee7996a92018-01-29 13:26:40 -07006107 (MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) ||
6108 MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
Mark Blocha560f1d2018-09-17 13:30:47 +03006109 mutex_init(&dev->lb.mutex);
Mark Blochc8b89922018-01-01 13:07:02 +02006110
Mark Bloch16c19752018-01-01 13:06:58 +02006111 return 0;
6112}
6113
Kamal Heib96458233e2018-12-10 21:09:38 +02006114static const struct ib_device_ops mlx5_ib_dev_port_ops = {
6115 .get_port_immutable = mlx5_port_immutable,
6116 .query_port = mlx5_ib_query_port,
6117};
6118
Mark Bloch8e6efa32017-11-06 12:22:13 +00006119static int mlx5_ib_stage_non_default_cb(struct mlx5_ib_dev *dev)
6120{
Kamal Heib96458233e2018-12-10 21:09:38 +02006121 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_port_ops);
Mark Bloch8e6efa32017-11-06 12:22:13 +00006122 return 0;
6123}
6124
Kamal Heib96458233e2018-12-10 21:09:38 +02006125static const struct ib_device_ops mlx5_ib_dev_port_rep_ops = {
6126 .get_port_immutable = mlx5_port_rep_immutable,
6127 .query_port = mlx5_ib_rep_query_port,
6128};
6129
Mark Blochb5ca15a2018-01-23 11:16:30 +00006130int mlx5_ib_stage_rep_non_default_cb(struct mlx5_ib_dev *dev)
Mark Bloch8e6efa32017-11-06 12:22:13 +00006131{
Kamal Heib96458233e2018-12-10 21:09:38 +02006132 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_port_rep_ops);
Mark Bloch8e6efa32017-11-06 12:22:13 +00006133 return 0;
6134}
6135
Kamal Heib96458233e2018-12-10 21:09:38 +02006136static const struct ib_device_ops mlx5_ib_dev_common_roce_ops = {
6137 .create_rwq_ind_table = mlx5_ib_create_rwq_ind_table,
6138 .create_wq = mlx5_ib_create_wq,
6139 .destroy_rwq_ind_table = mlx5_ib_destroy_rwq_ind_table,
6140 .destroy_wq = mlx5_ib_destroy_wq,
6141 .get_netdev = mlx5_ib_get_netdev,
6142 .modify_wq = mlx5_ib_modify_wq,
6143};
6144
Leon Romanovskye3f1ed12018-07-08 12:55:43 +03006145static int mlx5_ib_stage_common_roce_init(struct mlx5_ib_dev *dev)
Mark Bloch8e6efa32017-11-06 12:22:13 +00006146{
Leon Romanovskye3f1ed12018-07-08 12:55:43 +03006147 u8 port_num;
Mark Bloch8e6efa32017-11-06 12:22:13 +00006148 int i;
6149
6150 for (i = 0; i < dev->num_ports; i++) {
6151 dev->roce[i].dev = dev;
6152 dev->roce[i].native_port_num = i + 1;
6153 dev->roce[i].last_port_state = IB_PORT_DOWN;
6154 }
6155
Mark Bloch8e6efa32017-11-06 12:22:13 +00006156 dev->ib_dev.uverbs_ex_cmd_mask |=
6157 (1ull << IB_USER_VERBS_EX_CMD_CREATE_WQ) |
6158 (1ull << IB_USER_VERBS_EX_CMD_MODIFY_WQ) |
6159 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_WQ) |
6160 (1ull << IB_USER_VERBS_EX_CMD_CREATE_RWQ_IND_TBL) |
6161 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_RWQ_IND_TBL);
Kamal Heib96458233e2018-12-10 21:09:38 +02006162 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_common_roce_ops);
Mark Bloch8e6efa32017-11-06 12:22:13 +00006163
Leon Romanovskye3f1ed12018-07-08 12:55:43 +03006164 port_num = mlx5_core_native_port_num(dev->mdev) - 1;
6165
Mark Bloch8e6efa32017-11-06 12:22:13 +00006166 return mlx5_add_netdev_notifier(dev, port_num);
6167}
6168
6169static void mlx5_ib_stage_common_roce_cleanup(struct mlx5_ib_dev *dev)
6170{
6171 u8 port_num = mlx5_core_native_port_num(dev->mdev) - 1;
6172
6173 mlx5_remove_netdev_notifier(dev, port_num);
6174}
6175
6176int mlx5_ib_stage_rep_roce_init(struct mlx5_ib_dev *dev)
6177{
6178 struct mlx5_core_dev *mdev = dev->mdev;
6179 enum rdma_link_layer ll;
6180 int port_type_cap;
6181 int err = 0;
Mark Bloch8e6efa32017-11-06 12:22:13 +00006182
Mark Bloch8e6efa32017-11-06 12:22:13 +00006183 port_type_cap = MLX5_CAP_GEN(mdev, port_type);
6184 ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
6185
6186 if (ll == IB_LINK_LAYER_ETHERNET)
Leon Romanovskye3f1ed12018-07-08 12:55:43 +03006187 err = mlx5_ib_stage_common_roce_init(dev);
Mark Bloch8e6efa32017-11-06 12:22:13 +00006188
6189 return err;
6190}
6191
6192void mlx5_ib_stage_rep_roce_cleanup(struct mlx5_ib_dev *dev)
6193{
6194 mlx5_ib_stage_common_roce_cleanup(dev);
6195}
6196
Mark Bloch16c19752018-01-01 13:06:58 +02006197static int mlx5_ib_stage_roce_init(struct mlx5_ib_dev *dev)
6198{
6199 struct mlx5_core_dev *mdev = dev->mdev;
6200 enum rdma_link_layer ll;
6201 int port_type_cap;
6202 int err;
6203
6204 port_type_cap = MLX5_CAP_GEN(mdev, port_type);
6205 ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
6206
Achiad Shochatfc24fc52015-12-23 18:47:17 +02006207 if (ll == IB_LINK_LAYER_ETHERNET) {
Leon Romanovskye3f1ed12018-07-08 12:55:43 +03006208 err = mlx5_ib_stage_common_roce_init(dev);
Achiad Shochatfc24fc52015-12-23 18:47:17 +02006209 if (err)
Mark Bloch16c19752018-01-01 13:06:58 +02006210 return err;
Mark Bloch8e6efa32017-11-06 12:22:13 +00006211
Leon Romanovskye3f1ed12018-07-08 12:55:43 +03006212 err = mlx5_enable_eth(dev);
Mark Bloch8e6efa32017-11-06 12:22:13 +00006213 if (err)
6214 goto cleanup;
Achiad Shochatfc24fc52015-12-23 18:47:17 +02006215 }
6216
Mark Bloch16c19752018-01-01 13:06:58 +02006217 return 0;
Mark Bloch8e6efa32017-11-06 12:22:13 +00006218cleanup:
6219 mlx5_ib_stage_common_roce_cleanup(dev);
6220
6221 return err;
Mark Bloch16c19752018-01-01 13:06:58 +02006222}
Eli Cohene126ba92013-07-07 17:25:49 +03006223
Mark Bloch16c19752018-01-01 13:06:58 +02006224static void mlx5_ib_stage_roce_cleanup(struct mlx5_ib_dev *dev)
6225{
6226 struct mlx5_core_dev *mdev = dev->mdev;
6227 enum rdma_link_layer ll;
6228 int port_type_cap;
Eli Cohene126ba92013-07-07 17:25:49 +03006229
Mark Bloch16c19752018-01-01 13:06:58 +02006230 port_type_cap = MLX5_CAP_GEN(mdev, port_type);
6231 ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
6232
6233 if (ll == IB_LINK_LAYER_ETHERNET) {
6234 mlx5_disable_eth(dev);
Mark Bloch8e6efa32017-11-06 12:22:13 +00006235 mlx5_ib_stage_common_roce_cleanup(dev);
Kamal Heib45bded22017-01-18 14:10:32 +02006236 }
Mark Bloch16c19752018-01-01 13:06:58 +02006237}
Haggai Eran6aec21f2014-12-11 17:04:23 +02006238
Mark Blochb5ca15a2018-01-23 11:16:30 +00006239int mlx5_ib_stage_dev_res_init(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02006240{
6241 return create_dev_resources(&dev->devr);
6242}
Parav Pandit4a2da0b2017-05-30 10:05:15 +03006243
Mark Blochb5ca15a2018-01-23 11:16:30 +00006244void mlx5_ib_stage_dev_res_cleanup(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02006245{
6246 destroy_dev_resources(&dev->devr);
6247}
6248
6249static int mlx5_ib_stage_odp_init(struct mlx5_ib_dev *dev)
6250{
Mark Bloch07321b32018-01-01 13:07:00 +02006251 mlx5_ib_internal_fill_odp_caps(dev);
6252
Mark Bloch16c19752018-01-01 13:06:58 +02006253 return mlx5_ib_odp_init_one(dev);
6254}
6255
Kamal Heibf3ffed02019-01-30 16:13:42 +02006256static void mlx5_ib_stage_odp_cleanup(struct mlx5_ib_dev *dev)
Saeed Mahameedd5d284b2018-11-19 10:52:41 -08006257{
6258 mlx5_ib_odp_cleanup_one(dev);
6259}
6260
Kamal Heib96458233e2018-12-10 21:09:38 +02006261static const struct ib_device_ops mlx5_ib_dev_hw_stats_ops = {
6262 .alloc_hw_stats = mlx5_ib_alloc_hw_stats,
6263 .get_hw_stats = mlx5_ib_get_hw_stats,
6264};
6265
Mark Blochb5ca15a2018-01-23 11:16:30 +00006266int mlx5_ib_stage_counters_init(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02006267{
Mark Bloch5e1e7612018-01-01 13:07:01 +02006268 if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt)) {
Kamal Heib96458233e2018-12-10 21:09:38 +02006269 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_hw_stats_ops);
Mark Bloch5e1e7612018-01-01 13:07:01 +02006270
6271 return mlx5_ib_alloc_counters(dev);
6272 }
Mark Bloch16c19752018-01-01 13:06:58 +02006273
6274 return 0;
6275}
6276
Mark Blochb5ca15a2018-01-23 11:16:30 +00006277void mlx5_ib_stage_counters_cleanup(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02006278{
6279 if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt))
6280 mlx5_ib_dealloc_counters(dev);
6281}
6282
6283static int mlx5_ib_stage_cong_debugfs_init(struct mlx5_ib_dev *dev)
6284{
Greg Kroah-Hartman73eb8f02019-01-22 16:17:57 +01006285 mlx5_ib_init_cong_debugfs(dev,
6286 mlx5_core_native_port_num(dev->mdev) - 1);
6287 return 0;
Mark Bloch16c19752018-01-01 13:06:58 +02006288}
6289
6290static void mlx5_ib_stage_cong_debugfs_cleanup(struct mlx5_ib_dev *dev)
6291{
Parav Pandita9e546e2018-01-04 17:25:39 +02006292 mlx5_ib_cleanup_cong_debugfs(dev,
6293 mlx5_core_native_port_num(dev->mdev) - 1);
Mark Bloch16c19752018-01-01 13:06:58 +02006294}
6295
6296static int mlx5_ib_stage_uar_init(struct mlx5_ib_dev *dev)
6297{
Eli Cohen5fe9dec2017-01-03 23:55:25 +02006298 dev->mdev->priv.uar = mlx5_get_uars_page(dev->mdev);
Leon Romanovsky444261c2018-04-23 17:01:56 +03006299 return PTR_ERR_OR_ZERO(dev->mdev->priv.uar);
Mark Bloch16c19752018-01-01 13:06:58 +02006300}
6301
6302static void mlx5_ib_stage_uar_cleanup(struct mlx5_ib_dev *dev)
6303{
6304 mlx5_put_uars_page(dev->mdev, dev->mdev->priv.uar);
6305}
6306
Mark Blochb5ca15a2018-01-23 11:16:30 +00006307int mlx5_ib_stage_bfrag_init(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02006308{
6309 int err;
Eli Cohen5fe9dec2017-01-03 23:55:25 +02006310
6311 err = mlx5_alloc_bfreg(dev->mdev, &dev->bfreg, false, false);
6312 if (err)
Mark Bloch16c19752018-01-01 13:06:58 +02006313 return err;
Eli Cohen5fe9dec2017-01-03 23:55:25 +02006314
6315 err = mlx5_alloc_bfreg(dev->mdev, &dev->fp_bfreg, false, true);
6316 if (err)
Mark Bloch16c19752018-01-01 13:06:58 +02006317 mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
Eli Cohen5fe9dec2017-01-03 23:55:25 +02006318
Mark Bloch16c19752018-01-01 13:06:58 +02006319 return err;
6320}
Mark Bloch0837e862016-06-17 15:10:55 +03006321
Mark Blochb5ca15a2018-01-23 11:16:30 +00006322void mlx5_ib_stage_bfrag_cleanup(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02006323{
6324 mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
6325 mlx5_free_bfreg(dev->mdev, &dev->bfreg);
6326}
Eli Cohene126ba92013-07-07 17:25:49 +03006327
Mark Blochb5ca15a2018-01-23 11:16:30 +00006328int mlx5_ib_stage_ib_reg_init(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02006329{
Jason Gunthorpee349f852018-09-25 16:58:09 -06006330 const char *name;
6331
Parav Pandit508a5232018-10-11 22:31:54 +03006332 rdma_set_device_sysfs_group(&dev->ib_dev, &mlx5_attr_group);
Aviv Heller7c34ec12018-08-23 13:47:53 +03006333 if (!mlx5_lag_is_roce(dev->mdev))
Jason Gunthorpee349f852018-09-25 16:58:09 -06006334 name = "mlx5_%d";
6335 else
6336 name = "mlx5_bond_%d";
Parav Panditea4baf72018-12-18 14:28:30 +02006337 return ib_register_device(&dev->ib_dev, name);
Mark Bloch16c19752018-01-01 13:06:58 +02006338}
6339
David S. Miller03fe2de2018-03-23 11:24:57 -04006340void mlx5_ib_stage_pre_ib_reg_umr_cleanup(struct mlx5_ib_dev *dev)
Mark Bloch42cea832018-03-14 09:14:15 +02006341{
6342 destroy_umrc_res(dev);
6343}
6344
Mark Blochb5ca15a2018-01-23 11:16:30 +00006345void mlx5_ib_stage_ib_reg_cleanup(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02006346{
6347 ib_unregister_device(&dev->ib_dev);
6348}
6349
David S. Miller03fe2de2018-03-23 11:24:57 -04006350int mlx5_ib_stage_post_ib_reg_umr_init(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02006351{
6352 return create_umr_res(dev);
6353}
6354
Mark Bloch16c19752018-01-01 13:06:58 +02006355static int mlx5_ib_stage_delay_drop_init(struct mlx5_ib_dev *dev)
6356{
Maor Gottlieb03404e82017-05-30 10:29:13 +03006357 init_delay_drop(dev);
6358
Mark Bloch16c19752018-01-01 13:06:58 +02006359 return 0;
6360}
6361
6362static void mlx5_ib_stage_delay_drop_cleanup(struct mlx5_ib_dev *dev)
6363{
6364 cancel_delay_drop(dev);
6365}
6366
Saeed Mahameeddf097a22018-11-26 14:39:00 -08006367static int mlx5_ib_stage_dev_notifier_init(struct mlx5_ib_dev *dev)
6368{
6369 dev->mdev_events.notifier_call = mlx5_ib_event;
6370 mlx5_notifier_register(dev->mdev, &dev->mdev_events);
6371 return 0;
6372}
6373
6374static void mlx5_ib_stage_dev_notifier_cleanup(struct mlx5_ib_dev *dev)
6375{
6376 mlx5_notifier_unregister(dev->mdev, &dev->mdev_events);
6377}
6378
Leon Romanovsky81773ce2018-11-28 20:53:39 +02006379static int mlx5_ib_stage_devx_init(struct mlx5_ib_dev *dev)
6380{
6381 int uid;
6382
Yishai Hadasfb981532018-11-26 08:28:36 +02006383 uid = mlx5_ib_devx_create(dev, false);
Leon Romanovsky81773ce2018-11-28 20:53:39 +02006384 if (uid > 0)
6385 dev->devx_whitelist_uid = uid;
6386
6387 return 0;
6388}
6389static void mlx5_ib_stage_devx_cleanup(struct mlx5_ib_dev *dev)
6390{
6391 if (dev->devx_whitelist_uid)
6392 mlx5_ib_devx_destroy(dev, dev->devx_whitelist_uid);
6393}
6394
Mark Blochb5ca15a2018-01-23 11:16:30 +00006395void __mlx5_ib_remove(struct mlx5_ib_dev *dev,
6396 const struct mlx5_ib_profile *profile,
6397 int stage)
Mark Bloch16c19752018-01-01 13:06:58 +02006398{
6399 /* Number of stages to cleanup */
6400 while (stage) {
6401 stage--;
6402 if (profile->stage[stage].cleanup)
6403 profile->stage[stage].cleanup(dev);
6404 }
Mark Bloch16c19752018-01-01 13:06:58 +02006405}
6406
Mark Blochb5ca15a2018-01-23 11:16:30 +00006407void *__mlx5_ib_add(struct mlx5_ib_dev *dev,
6408 const struct mlx5_ib_profile *profile)
Mark Bloch16c19752018-01-01 13:06:58 +02006409{
Mark Bloch16c19752018-01-01 13:06:58 +02006410 int err;
6411 int i;
Mark Bloch16c19752018-01-01 13:06:58 +02006412
Mark Bloch16c19752018-01-01 13:06:58 +02006413 for (i = 0; i < MLX5_IB_STAGE_MAX; i++) {
6414 if (profile->stage[i].init) {
6415 err = profile->stage[i].init(dev);
6416 if (err)
6417 goto err_out;
6418 }
6419 }
6420
6421 dev->profile = profile;
Eli Cohene126ba92013-07-07 17:25:49 +03006422 dev->ib_active = true;
6423
Jack Morgenstein9603b612014-07-28 23:30:22 +03006424 return dev;
Eli Cohene126ba92013-07-07 17:25:49 +03006425
Mark Bloch16c19752018-01-01 13:06:58 +02006426err_out:
6427 __mlx5_ib_remove(dev, profile, i);
Eli Cohene126ba92013-07-07 17:25:49 +03006428
Jack Morgenstein9603b612014-07-28 23:30:22 +03006429 return NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03006430}
6431
Mark Bloch16c19752018-01-01 13:06:58 +02006432static const struct mlx5_ib_profile pf_profile = {
6433 STAGE_CREATE(MLX5_IB_STAGE_INIT,
6434 mlx5_ib_stage_init_init,
6435 mlx5_ib_stage_init_cleanup),
Mark Bloch9a4ca382018-01-16 14:42:35 +00006436 STAGE_CREATE(MLX5_IB_STAGE_FLOW_DB,
6437 mlx5_ib_stage_flow_db_init,
6438 mlx5_ib_stage_flow_db_cleanup),
Mark Bloch16c19752018-01-01 13:06:58 +02006439 STAGE_CREATE(MLX5_IB_STAGE_CAPS,
6440 mlx5_ib_stage_caps_init,
6441 NULL),
Mark Bloch8e6efa32017-11-06 12:22:13 +00006442 STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB,
6443 mlx5_ib_stage_non_default_cb,
6444 NULL),
Mark Bloch16c19752018-01-01 13:06:58 +02006445 STAGE_CREATE(MLX5_IB_STAGE_ROCE,
6446 mlx5_ib_stage_roce_init,
6447 mlx5_ib_stage_roce_cleanup),
Leon Romanovskyf3da6572018-11-28 20:53:41 +02006448 STAGE_CREATE(MLX5_IB_STAGE_SRQ,
6449 mlx5_init_srq_table,
6450 mlx5_cleanup_srq_table),
Mark Bloch16c19752018-01-01 13:06:58 +02006451 STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES,
6452 mlx5_ib_stage_dev_res_init,
6453 mlx5_ib_stage_dev_res_cleanup),
Saeed Mahameeddf097a22018-11-26 14:39:00 -08006454 STAGE_CREATE(MLX5_IB_STAGE_DEVICE_NOTIFIER,
6455 mlx5_ib_stage_dev_notifier_init,
6456 mlx5_ib_stage_dev_notifier_cleanup),
Mark Bloch16c19752018-01-01 13:06:58 +02006457 STAGE_CREATE(MLX5_IB_STAGE_ODP,
6458 mlx5_ib_stage_odp_init,
Saeed Mahameedd5d284b2018-11-19 10:52:41 -08006459 mlx5_ib_stage_odp_cleanup),
Mark Bloch16c19752018-01-01 13:06:58 +02006460 STAGE_CREATE(MLX5_IB_STAGE_COUNTERS,
6461 mlx5_ib_stage_counters_init,
6462 mlx5_ib_stage_counters_cleanup),
6463 STAGE_CREATE(MLX5_IB_STAGE_CONG_DEBUGFS,
6464 mlx5_ib_stage_cong_debugfs_init,
6465 mlx5_ib_stage_cong_debugfs_cleanup),
6466 STAGE_CREATE(MLX5_IB_STAGE_UAR,
6467 mlx5_ib_stage_uar_init,
6468 mlx5_ib_stage_uar_cleanup),
6469 STAGE_CREATE(MLX5_IB_STAGE_BFREG,
6470 mlx5_ib_stage_bfrag_init,
6471 mlx5_ib_stage_bfrag_cleanup),
Mark Bloch42cea832018-03-14 09:14:15 +02006472 STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR,
6473 NULL,
6474 mlx5_ib_stage_pre_ib_reg_umr_cleanup),
Leon Romanovsky81773ce2018-11-28 20:53:39 +02006475 STAGE_CREATE(MLX5_IB_STAGE_WHITELIST_UID,
6476 mlx5_ib_stage_devx_init,
6477 mlx5_ib_stage_devx_cleanup),
Mark Bloch16c19752018-01-01 13:06:58 +02006478 STAGE_CREATE(MLX5_IB_STAGE_IB_REG,
6479 mlx5_ib_stage_ib_reg_init,
6480 mlx5_ib_stage_ib_reg_cleanup),
Mark Bloch42cea832018-03-14 09:14:15 +02006481 STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR,
6482 mlx5_ib_stage_post_ib_reg_umr_init,
6483 NULL),
Mark Bloch16c19752018-01-01 13:06:58 +02006484 STAGE_CREATE(MLX5_IB_STAGE_DELAY_DROP,
6485 mlx5_ib_stage_delay_drop_init,
6486 mlx5_ib_stage_delay_drop_cleanup),
Mark Bloch16c19752018-01-01 13:06:58 +02006487};
6488
Bodong Wangf0666f12019-02-12 22:55:34 -08006489const struct mlx5_ib_profile uplink_rep_profile = {
Mark Blochb5ca15a2018-01-23 11:16:30 +00006490 STAGE_CREATE(MLX5_IB_STAGE_INIT,
6491 mlx5_ib_stage_init_init,
6492 mlx5_ib_stage_init_cleanup),
6493 STAGE_CREATE(MLX5_IB_STAGE_FLOW_DB,
6494 mlx5_ib_stage_flow_db_init,
6495 mlx5_ib_stage_flow_db_cleanup),
6496 STAGE_CREATE(MLX5_IB_STAGE_CAPS,
6497 mlx5_ib_stage_caps_init,
6498 NULL),
6499 STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB,
6500 mlx5_ib_stage_rep_non_default_cb,
6501 NULL),
6502 STAGE_CREATE(MLX5_IB_STAGE_ROCE,
6503 mlx5_ib_stage_rep_roce_init,
6504 mlx5_ib_stage_rep_roce_cleanup),
Leon Romanovskyf3da6572018-11-28 20:53:41 +02006505 STAGE_CREATE(MLX5_IB_STAGE_SRQ,
6506 mlx5_init_srq_table,
6507 mlx5_cleanup_srq_table),
Mark Blochb5ca15a2018-01-23 11:16:30 +00006508 STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES,
6509 mlx5_ib_stage_dev_res_init,
6510 mlx5_ib_stage_dev_res_cleanup),
Saeed Mahameeddf097a22018-11-26 14:39:00 -08006511 STAGE_CREATE(MLX5_IB_STAGE_DEVICE_NOTIFIER,
6512 mlx5_ib_stage_dev_notifier_init,
6513 mlx5_ib_stage_dev_notifier_cleanup),
Mark Blochb5ca15a2018-01-23 11:16:30 +00006514 STAGE_CREATE(MLX5_IB_STAGE_COUNTERS,
6515 mlx5_ib_stage_counters_init,
6516 mlx5_ib_stage_counters_cleanup),
6517 STAGE_CREATE(MLX5_IB_STAGE_UAR,
6518 mlx5_ib_stage_uar_init,
6519 mlx5_ib_stage_uar_cleanup),
6520 STAGE_CREATE(MLX5_IB_STAGE_BFREG,
6521 mlx5_ib_stage_bfrag_init,
6522 mlx5_ib_stage_bfrag_cleanup),
David S. Miller03fe2de2018-03-23 11:24:57 -04006523 STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR,
6524 NULL,
6525 mlx5_ib_stage_pre_ib_reg_umr_cleanup),
Mark Blochb5ca15a2018-01-23 11:16:30 +00006526 STAGE_CREATE(MLX5_IB_STAGE_IB_REG,
6527 mlx5_ib_stage_ib_reg_init,
6528 mlx5_ib_stage_ib_reg_cleanup),
David S. Miller03fe2de2018-03-23 11:24:57 -04006529 STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR,
6530 mlx5_ib_stage_post_ib_reg_umr_init,
6531 NULL),
Mark Blochb5ca15a2018-01-23 11:16:30 +00006532};
6533
Leon Romanovskye3f1ed12018-07-08 12:55:43 +03006534static void *mlx5_ib_add_slave_port(struct mlx5_core_dev *mdev)
Daniel Jurgens32f69e42018-01-04 17:25:36 +02006535{
6536 struct mlx5_ib_multiport_info *mpi;
6537 struct mlx5_ib_dev *dev;
6538 bool bound = false;
6539 int err;
6540
6541 mpi = kzalloc(sizeof(*mpi), GFP_KERNEL);
6542 if (!mpi)
6543 return NULL;
6544
6545 mpi->mdev = mdev;
6546
6547 err = mlx5_query_nic_vport_system_image_guid(mdev,
6548 &mpi->sys_image_guid);
6549 if (err) {
6550 kfree(mpi);
6551 return NULL;
6552 }
6553
6554 mutex_lock(&mlx5_ib_multiport_mutex);
6555 list_for_each_entry(dev, &mlx5_ib_dev_list, ib_dev_list) {
6556 if (dev->sys_image_guid == mpi->sys_image_guid)
6557 bound = mlx5_ib_bind_slave_port(dev, mpi);
6558
6559 if (bound) {
6560 rdma_roce_rescan_device(&dev->ib_dev);
6561 break;
6562 }
6563 }
6564
6565 if (!bound) {
6566 list_add_tail(&mpi->list, &mlx5_ib_unaffiliated_port_list);
6567 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 +02006568 }
6569 mutex_unlock(&mlx5_ib_multiport_mutex);
6570
6571 return mpi;
6572}
6573
Mark Bloch16c19752018-01-01 13:06:58 +02006574static void *mlx5_ib_add(struct mlx5_core_dev *mdev)
6575{
Daniel Jurgens32f69e42018-01-04 17:25:36 +02006576 enum rdma_link_layer ll;
Mark Blochb5ca15a2018-01-23 11:16:30 +00006577 struct mlx5_ib_dev *dev;
Daniel Jurgens32f69e42018-01-04 17:25:36 +02006578 int port_type_cap;
6579
Mark Blochb5ca15a2018-01-23 11:16:30 +00006580 printk_once(KERN_INFO "%s", mlx5_version);
6581
Bodong Wangf0666f12019-02-12 22:55:34 -08006582 if (MLX5_ESWITCH_MANAGER(mdev) &&
6583 mlx5_ib_eswitch_mode(mdev->priv.eswitch) == SRIOV_OFFLOADS) {
6584 mlx5_ib_register_vport_reps(mdev);
6585 return mdev;
6586 }
6587
Daniel Jurgens32f69e42018-01-04 17:25:36 +02006588 port_type_cap = MLX5_CAP_GEN(mdev, port_type);
6589 ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
6590
Leon Romanovskye3f1ed12018-07-08 12:55:43 +03006591 if (mlx5_core_is_mp_slave(mdev) && ll == IB_LINK_LAYER_ETHERNET)
6592 return mlx5_ib_add_slave_port(mdev);
Daniel Jurgens32f69e42018-01-04 17:25:36 +02006593
Leon Romanovsky459cc692019-01-30 12:49:11 +02006594 dev = ib_alloc_device(mlx5_ib_dev, ib_dev);
Mark Blochb5ca15a2018-01-23 11:16:30 +00006595 if (!dev)
6596 return NULL;
6597
6598 dev->mdev = mdev;
6599 dev->num_ports = max(MLX5_CAP_GEN(mdev, num_ports),
6600 MLX5_CAP_GEN(mdev, num_vhca_ports));
6601
Mark Blochb5ca15a2018-01-23 11:16:30 +00006602 return __mlx5_ib_add(dev, &pf_profile);
Mark Bloch16c19752018-01-01 13:06:58 +02006603}
6604
Jack Morgenstein9603b612014-07-28 23:30:22 +03006605static void mlx5_ib_remove(struct mlx5_core_dev *mdev, void *context)
Eli Cohene126ba92013-07-07 17:25:49 +03006606{
Daniel Jurgens32f69e42018-01-04 17:25:36 +02006607 struct mlx5_ib_multiport_info *mpi;
6608 struct mlx5_ib_dev *dev;
Haggai Eran6aec21f2014-12-11 17:04:23 +02006609
Bodong Wangf0666f12019-02-12 22:55:34 -08006610 if (MLX5_ESWITCH_MANAGER(mdev) && context == mdev) {
6611 mlx5_ib_unregister_vport_reps(mdev);
6612 return;
6613 }
6614
Daniel Jurgens32f69e42018-01-04 17:25:36 +02006615 if (mlx5_core_is_mp_slave(mdev)) {
6616 mpi = context;
6617 mutex_lock(&mlx5_ib_multiport_mutex);
6618 if (mpi->ibdev)
6619 mlx5_ib_unbind_slave_port(mpi->ibdev, mpi);
6620 list_del(&mpi->list);
6621 mutex_unlock(&mlx5_ib_multiport_mutex);
6622 return;
6623 }
6624
6625 dev = context;
Bodong Wangf0666f12019-02-12 22:55:34 -08006626 __mlx5_ib_remove(dev, dev->profile, MLX5_IB_STAGE_MAX);
Mark Bloch06cc74a2018-12-12 19:11:37 -08006627
6628 ib_dealloc_device((struct ib_device *)dev);
Eli Cohene126ba92013-07-07 17:25:49 +03006629}
6630
Jack Morgenstein9603b612014-07-28 23:30:22 +03006631static struct mlx5_interface mlx5_ib_interface = {
6632 .add = mlx5_ib_add,
6633 .remove = mlx5_ib_remove,
Saeed Mahameed64613d942015-04-02 17:07:34 +03006634 .protocol = MLX5_INTERFACE_PROTOCOL_IB,
Eli Cohene126ba92013-07-07 17:25:49 +03006635};
6636
Ilya Lesokhinc44ef992018-03-13 15:18:48 +02006637unsigned long mlx5_ib_get_xlt_emergency_page(void)
6638{
6639 mutex_lock(&xlt_emergency_page_mutex);
6640 return xlt_emergency_page;
6641}
6642
6643void mlx5_ib_put_xlt_emergency_page(void)
6644{
6645 mutex_unlock(&xlt_emergency_page_mutex);
6646}
6647
Eli Cohene126ba92013-07-07 17:25:49 +03006648static int __init mlx5_ib_init(void)
6649{
Haggai Eran6aec21f2014-12-11 17:04:23 +02006650 int err;
6651
Ilya Lesokhinc44ef992018-03-13 15:18:48 +02006652 xlt_emergency_page = __get_free_page(GFP_KERNEL);
6653 if (!xlt_emergency_page)
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02006654 return -ENOMEM;
6655
Ilya Lesokhinc44ef992018-03-13 15:18:48 +02006656 mutex_init(&xlt_emergency_page_mutex);
6657
6658 mlx5_ib_event_wq = alloc_ordered_workqueue("mlx5_ib_event_wq", 0);
6659 if (!mlx5_ib_event_wq) {
6660 free_page(xlt_emergency_page);
6661 return -ENOMEM;
6662 }
6663
Artemy Kovalyov81713d32017-01-18 16:58:11 +02006664 mlx5_ib_odp_init();
Jack Morgenstein9603b612014-07-28 23:30:22 +03006665
Haggai Eran6aec21f2014-12-11 17:04:23 +02006666 err = mlx5_register_interface(&mlx5_ib_interface);
Haggai Eran6aec21f2014-12-11 17:04:23 +02006667
6668 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03006669}
6670
6671static void __exit mlx5_ib_cleanup(void)
6672{
Jack Morgenstein9603b612014-07-28 23:30:22 +03006673 mlx5_unregister_interface(&mlx5_ib_interface);
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02006674 destroy_workqueue(mlx5_ib_event_wq);
Ilya Lesokhinc44ef992018-03-13 15:18:48 +02006675 mutex_destroy(&xlt_emergency_page_mutex);
6676 free_page(xlt_emergency_page);
Eli Cohene126ba92013-07-07 17:25:49 +03006677}
6678
6679module_init(mlx5_ib_init);
6680module_exit(mlx5_ib_cleanup);