blob: c8cbfe2e964a83626e821266fd7c79bf29ddcbce [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
Mark Bloch35b0aa62019-03-28 15:27:39 +0200159static struct mlx5_roce *mlx5_get_rep_roce(struct mlx5_ib_dev *dev,
160 struct net_device *ndev,
161 u8 *port_num)
162{
163 struct mlx5_eswitch *esw = dev->mdev->priv.eswitch;
164 struct net_device *rep_ndev;
165 struct mlx5_ib_port *port;
166 int i;
167
168 for (i = 0; i < dev->num_ports; i++) {
169 port = &dev->port[i];
170 if (!port->rep)
171 continue;
172
173 read_lock(&port->roce.netdev_lock);
174 rep_ndev = mlx5_ib_get_rep_netdev(esw,
175 port->rep->vport);
176 if (rep_ndev == ndev) {
177 read_unlock(&port->roce.netdev_lock);
178 *port_num = i + 1;
179 return &port->roce;
180 }
181 read_unlock(&port->roce.netdev_lock);
182 }
183
184 return NULL;
185}
186
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200187static int mlx5_netdev_event(struct notifier_block *this,
188 unsigned long event, void *ptr)
189{
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200190 struct mlx5_roce *roce = container_of(this, struct mlx5_roce, nb);
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200191 struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200192 u8 port_num = roce->native_port_num;
193 struct mlx5_core_dev *mdev;
194 struct mlx5_ib_dev *ibdev;
195
196 ibdev = roce->dev;
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200197 mdev = mlx5_ib_get_native_port_mdev(ibdev, port_num, NULL);
198 if (!mdev)
199 return NOTIFY_DONE;
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200200
Aviv Heller5ec8c832016-09-18 20:48:00 +0300201 switch (event) {
202 case NETDEV_REGISTER:
Mark Bloch35b0aa62019-03-28 15:27:39 +0200203 /* Should already be registered during the load */
204 if (ibdev->is_rep)
205 break;
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200206 write_lock(&roce->netdev_lock);
Mark Bloch35b0aa62019-03-28 15:27:39 +0200207 if (ndev->dev.parent == &mdev->pdev->dev)
Or Gerlitz842a9c82018-12-11 18:10:43 +0200208 roce->netdev = ndev;
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200209 write_unlock(&roce->netdev_lock);
Aviv Heller5ec8c832016-09-18 20:48:00 +0300210 break;
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200211
Or Gerlitz842a9c82018-12-11 18:10:43 +0200212 case NETDEV_UNREGISTER:
Mark Bloch35b0aa62019-03-28 15:27:39 +0200213 /* In case of reps, ib device goes away before the netdevs */
Or Gerlitz842a9c82018-12-11 18:10:43 +0200214 write_lock(&roce->netdev_lock);
215 if (roce->netdev == ndev)
216 roce->netdev = NULL;
217 write_unlock(&roce->netdev_lock);
218 break;
219
Moni Shouafd65f1b2017-05-30 09:56:05 +0300220 case NETDEV_CHANGE:
Aviv Heller5ec8c832016-09-18 20:48:00 +0300221 case NETDEV_UP:
Aviv Heller88621df2016-09-18 20:48:02 +0300222 case NETDEV_DOWN: {
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200223 struct net_device *lag_ndev = mlx5_lag_get_roce_netdev(mdev);
Aviv Heller88621df2016-09-18 20:48:02 +0300224 struct net_device *upper = NULL;
225
226 if (lag_ndev) {
227 upper = netdev_master_upper_dev_get(lag_ndev);
228 dev_put(lag_ndev);
229 }
230
Mark Bloch35b0aa62019-03-28 15:27:39 +0200231 if (ibdev->is_rep)
232 roce = mlx5_get_rep_roce(ibdev, ndev, &port_num);
233 if (!roce)
234 return NOTIFY_DONE;
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200235 if ((upper == ndev || (!upper && ndev == roce->netdev))
Aviv Heller88621df2016-09-18 20:48:02 +0300236 && ibdev->ib_active) {
Bart Van Assche626bc022016-12-05 17:18:08 -0800237 struct ib_event ibev = { };
Moni Shouafd65f1b2017-05-30 09:56:05 +0300238 enum ib_port_state port_state;
Aviv Heller5ec8c832016-09-18 20:48:00 +0300239
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200240 if (get_port_state(&ibdev->ib_dev, port_num,
241 &port_state))
242 goto done;
Moni Shouafd65f1b2017-05-30 09:56:05 +0300243
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200244 if (roce->last_port_state == port_state)
245 goto done;
Moni Shouafd65f1b2017-05-30 09:56:05 +0300246
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200247 roce->last_port_state = port_state;
Aviv Heller5ec8c832016-09-18 20:48:00 +0300248 ibev.device = &ibdev->ib_dev;
Moni Shouafd65f1b2017-05-30 09:56:05 +0300249 if (port_state == IB_PORT_DOWN)
250 ibev.event = IB_EVENT_PORT_ERR;
251 else if (port_state == IB_PORT_ACTIVE)
252 ibev.event = IB_EVENT_PORT_ACTIVE;
253 else
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200254 goto done;
Moni Shouafd65f1b2017-05-30 09:56:05 +0300255
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200256 ibev.element.port_num = port_num;
Aviv Heller5ec8c832016-09-18 20:48:00 +0300257 ib_dispatch_event(&ibev);
258 }
259 break;
Aviv Heller88621df2016-09-18 20:48:02 +0300260 }
Aviv Heller5ec8c832016-09-18 20:48:00 +0300261
262 default:
263 break;
264 }
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200265done:
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200266 mlx5_ib_put_native_port_mdev(ibdev, port_num);
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200267 return NOTIFY_DONE;
268}
269
270static struct net_device *mlx5_ib_get_netdev(struct ib_device *device,
271 u8 port_num)
272{
273 struct mlx5_ib_dev *ibdev = to_mdev(device);
274 struct net_device *ndev;
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200275 struct mlx5_core_dev *mdev;
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200276
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200277 mdev = mlx5_ib_get_native_port_mdev(ibdev, port_num, NULL);
278 if (!mdev)
279 return NULL;
280
281 ndev = mlx5_lag_get_roce_netdev(mdev);
Aviv Heller88621df2016-09-18 20:48:02 +0300282 if (ndev)
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200283 goto out;
Aviv Heller88621df2016-09-18 20:48:02 +0300284
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200285 /* Ensure ndev does not disappear before we invoke dev_hold()
286 */
Mark Bloch95579e72019-03-28 15:27:33 +0200287 read_lock(&ibdev->port[port_num - 1].roce.netdev_lock);
288 ndev = ibdev->port[port_num - 1].roce.netdev;
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200289 if (ndev)
290 dev_hold(ndev);
Mark Bloch95579e72019-03-28 15:27:33 +0200291 read_unlock(&ibdev->port[port_num - 1].roce.netdev_lock);
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200292
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200293out:
294 mlx5_ib_put_native_port_mdev(ibdev, port_num);
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200295 return ndev;
296}
297
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200298struct mlx5_core_dev *mlx5_ib_get_native_port_mdev(struct mlx5_ib_dev *ibdev,
299 u8 ib_port_num,
300 u8 *native_port_num)
301{
302 enum rdma_link_layer ll = mlx5_ib_port_link_layer(&ibdev->ib_dev,
303 ib_port_num);
304 struct mlx5_core_dev *mdev = NULL;
305 struct mlx5_ib_multiport_info *mpi;
306 struct mlx5_ib_port *port;
307
Mark Bloch210b1f72018-03-05 20:09:47 +0200308 if (!mlx5_core_mp_enabled(ibdev->mdev) ||
309 ll != IB_LINK_LAYER_ETHERNET) {
310 if (native_port_num)
311 *native_port_num = ib_port_num;
312 return ibdev->mdev;
313 }
314
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200315 if (native_port_num)
316 *native_port_num = 1;
317
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200318 port = &ibdev->port[ib_port_num - 1];
319 if (!port)
320 return NULL;
321
322 spin_lock(&port->mp.mpi_lock);
323 mpi = ibdev->port[ib_port_num - 1].mp.mpi;
324 if (mpi && !mpi->unaffiliate) {
325 mdev = mpi->mdev;
326 /* If it's the master no need to refcount, it'll exist
327 * as long as the ib_dev exists.
328 */
329 if (!mpi->is_master)
330 mpi->mdev_refcnt++;
331 }
332 spin_unlock(&port->mp.mpi_lock);
333
334 return mdev;
335}
336
337void mlx5_ib_put_native_port_mdev(struct mlx5_ib_dev *ibdev, u8 port_num)
338{
339 enum rdma_link_layer ll = mlx5_ib_port_link_layer(&ibdev->ib_dev,
340 port_num);
341 struct mlx5_ib_multiport_info *mpi;
342 struct mlx5_ib_port *port;
343
344 if (!mlx5_core_mp_enabled(ibdev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
345 return;
346
347 port = &ibdev->port[port_num - 1];
348
349 spin_lock(&port->mp.mpi_lock);
350 mpi = ibdev->port[port_num - 1].mp.mpi;
351 if (mpi->is_master)
352 goto out;
353
354 mpi->mdev_refcnt--;
355 if (mpi->unaffiliate)
356 complete(&mpi->unref_comp);
357out:
358 spin_unlock(&port->mp.mpi_lock);
359}
360
Aya Levin08e86762019-02-12 22:55:46 -0800361static int translate_eth_legacy_proto_oper(u32 eth_proto_oper, u8 *active_speed,
362 u8 *active_width)
Noa Osherovichf1b65df2017-04-20 20:53:33 +0300363{
364 switch (eth_proto_oper) {
365 case MLX5E_PROT_MASK(MLX5E_1000BASE_CX_SGMII):
366 case MLX5E_PROT_MASK(MLX5E_1000BASE_KX):
367 case MLX5E_PROT_MASK(MLX5E_100BASE_TX):
368 case MLX5E_PROT_MASK(MLX5E_1000BASE_T):
369 *active_width = IB_WIDTH_1X;
370 *active_speed = IB_SPEED_SDR;
371 break;
372 case MLX5E_PROT_MASK(MLX5E_10GBASE_T):
373 case MLX5E_PROT_MASK(MLX5E_10GBASE_CX4):
374 case MLX5E_PROT_MASK(MLX5E_10GBASE_KX4):
375 case MLX5E_PROT_MASK(MLX5E_10GBASE_KR):
376 case MLX5E_PROT_MASK(MLX5E_10GBASE_CR):
377 case MLX5E_PROT_MASK(MLX5E_10GBASE_SR):
378 case MLX5E_PROT_MASK(MLX5E_10GBASE_ER):
379 *active_width = IB_WIDTH_1X;
380 *active_speed = IB_SPEED_QDR;
381 break;
382 case MLX5E_PROT_MASK(MLX5E_25GBASE_CR):
383 case MLX5E_PROT_MASK(MLX5E_25GBASE_KR):
384 case MLX5E_PROT_MASK(MLX5E_25GBASE_SR):
385 *active_width = IB_WIDTH_1X;
386 *active_speed = IB_SPEED_EDR;
387 break;
388 case MLX5E_PROT_MASK(MLX5E_40GBASE_CR4):
389 case MLX5E_PROT_MASK(MLX5E_40GBASE_KR4):
390 case MLX5E_PROT_MASK(MLX5E_40GBASE_SR4):
391 case MLX5E_PROT_MASK(MLX5E_40GBASE_LR4):
392 *active_width = IB_WIDTH_4X;
393 *active_speed = IB_SPEED_QDR;
394 break;
395 case MLX5E_PROT_MASK(MLX5E_50GBASE_CR2):
396 case MLX5E_PROT_MASK(MLX5E_50GBASE_KR2):
397 case MLX5E_PROT_MASK(MLX5E_50GBASE_SR2):
398 *active_width = IB_WIDTH_1X;
399 *active_speed = IB_SPEED_HDR;
400 break;
401 case MLX5E_PROT_MASK(MLX5E_56GBASE_R4):
402 *active_width = IB_WIDTH_4X;
403 *active_speed = IB_SPEED_FDR;
404 break;
405 case MLX5E_PROT_MASK(MLX5E_100GBASE_CR4):
406 case MLX5E_PROT_MASK(MLX5E_100GBASE_SR4):
407 case MLX5E_PROT_MASK(MLX5E_100GBASE_KR4):
408 case MLX5E_PROT_MASK(MLX5E_100GBASE_LR4):
409 *active_width = IB_WIDTH_4X;
410 *active_speed = IB_SPEED_EDR;
411 break;
412 default:
413 return -EINVAL;
414 }
415
416 return 0;
417}
418
Aya Levin08e86762019-02-12 22:55:46 -0800419static int translate_eth_ext_proto_oper(u32 eth_proto_oper, u8 *active_speed,
420 u8 *active_width)
421{
422 switch (eth_proto_oper) {
423 case MLX5E_PROT_MASK(MLX5E_SGMII_100M):
424 case MLX5E_PROT_MASK(MLX5E_1000BASE_X_SGMII):
425 *active_width = IB_WIDTH_1X;
426 *active_speed = IB_SPEED_SDR;
427 break;
428 case MLX5E_PROT_MASK(MLX5E_5GBASE_R):
429 *active_width = IB_WIDTH_1X;
430 *active_speed = IB_SPEED_DDR;
431 break;
432 case MLX5E_PROT_MASK(MLX5E_10GBASE_XFI_XAUI_1):
433 *active_width = IB_WIDTH_1X;
434 *active_speed = IB_SPEED_QDR;
435 break;
436 case MLX5E_PROT_MASK(MLX5E_40GBASE_XLAUI_4_XLPPI_4):
437 *active_width = IB_WIDTH_4X;
438 *active_speed = IB_SPEED_QDR;
439 break;
440 case MLX5E_PROT_MASK(MLX5E_25GAUI_1_25GBASE_CR_KR):
441 *active_width = IB_WIDTH_1X;
442 *active_speed = IB_SPEED_EDR;
443 break;
444 case MLX5E_PROT_MASK(MLX5E_50GAUI_2_LAUI_2_50GBASE_CR2_KR2):
Aya Levincd272872019-03-11 14:35:58 +0200445 *active_width = IB_WIDTH_2X;
446 *active_speed = IB_SPEED_EDR;
447 break;
Aya Levin08e86762019-02-12 22:55:46 -0800448 case MLX5E_PROT_MASK(MLX5E_50GAUI_1_LAUI_1_50GBASE_CR_KR):
449 *active_width = IB_WIDTH_1X;
450 *active_speed = IB_SPEED_HDR;
451 break;
Aya Levincd272872019-03-11 14:35:58 +0200452 case MLX5E_PROT_MASK(MLX5E_CAUI_4_100GBASE_CR4_KR4):
453 *active_width = IB_WIDTH_4X;
454 *active_speed = IB_SPEED_EDR;
455 break;
Aya Levin08e86762019-02-12 22:55:46 -0800456 case MLX5E_PROT_MASK(MLX5E_100GAUI_2_100GBASE_CR2_KR2):
457 *active_width = IB_WIDTH_2X;
458 *active_speed = IB_SPEED_HDR;
459 break;
460 case MLX5E_PROT_MASK(MLX5E_200GAUI_4_200GBASE_CR4_KR4):
461 *active_width = IB_WIDTH_4X;
462 *active_speed = IB_SPEED_HDR;
463 break;
464 default:
465 return -EINVAL;
466 }
467
468 return 0;
469}
470
471static int translate_eth_proto_oper(u32 eth_proto_oper, u8 *active_speed,
472 u8 *active_width, bool ext)
473{
474 return ext ?
475 translate_eth_ext_proto_oper(eth_proto_oper, active_speed,
476 active_width) :
477 translate_eth_legacy_proto_oper(eth_proto_oper, active_speed,
478 active_width);
479}
480
Ilan Tayari095b0922017-05-14 16:04:30 +0300481static int mlx5_query_port_roce(struct ib_device *device, u8 port_num,
482 struct ib_port_attr *props)
Achiad Shochat3f89a642015-12-23 18:47:21 +0200483{
484 struct mlx5_ib_dev *dev = to_mdev(device);
Aya Levinbc4e12f2019-02-12 22:55:43 -0800485 u32 out[MLX5_ST_SZ_DW(ptys_reg)] = {0};
Colin Ian Kingda005f92018-01-09 15:55:43 +0000486 struct mlx5_core_dev *mdev;
Aviv Heller88621df2016-09-18 20:48:02 +0300487 struct net_device *ndev, *upper;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200488 enum ib_mtu ndev_ib_mtu;
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200489 bool put_mdev = true;
Leon Romanovskyc876a1b2016-01-09 13:06:25 +0200490 u16 qkey_viol_cntr;
Noa Osherovichf1b65df2017-04-20 20:53:33 +0300491 u32 eth_prot_oper;
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200492 u8 mdev_port_num;
Aya Levin08e86762019-02-12 22:55:46 -0800493 bool ext;
Ilan Tayari095b0922017-05-14 16:04:30 +0300494 int err;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200495
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200496 mdev = mlx5_ib_get_native_port_mdev(dev, port_num, &mdev_port_num);
497 if (!mdev) {
498 /* This means the port isn't affiliated yet. Get the
499 * info for the master port instead.
500 */
501 put_mdev = false;
502 mdev = dev->mdev;
503 mdev_port_num = 1;
504 port_num = 1;
505 }
506
Noa Osherovichf1b65df2017-04-20 20:53:33 +0300507 /* Possible bad flows are checked before filling out props so in case
508 * of an error it will still be zeroed out.
Noa Osherovich50f22fd2017-04-20 20:53:32 +0300509 */
Aya Levinbc4e12f2019-02-12 22:55:43 -0800510 err = mlx5_query_port_ptys(mdev, out, sizeof(out), MLX5_PTYS_EN,
511 mdev_port_num);
Ilan Tayari095b0922017-05-14 16:04:30 +0300512 if (err)
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200513 goto out;
Aya Levin08e86762019-02-12 22:55:46 -0800514 ext = MLX5_CAP_PCAM_FEATURE(dev->mdev, ptys_extended_ethernet);
515 eth_prot_oper = MLX5_GET_ETH_PROTO(ptys_reg, out, ext, eth_proto_oper);
Noa Osherovichf1b65df2017-04-20 20:53:33 +0300516
Honggang Li7672ed32018-03-16 10:37:13 +0800517 props->active_width = IB_WIDTH_4X;
518 props->active_speed = IB_SPEED_QDR;
519
Noa Osherovichf1b65df2017-04-20 20:53:33 +0300520 translate_eth_proto_oper(eth_prot_oper, &props->active_speed,
Aya Levin08e86762019-02-12 22:55:46 -0800521 &props->active_width, ext);
Achiad Shochat3f89a642015-12-23 18:47:21 +0200522
Jason Gunthorpe2f944c02018-07-04 15:57:48 +0300523 props->port_cap_flags |= IB_PORT_CM_SUP;
524 props->ip_gids = true;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200525
526 props->gid_tbl_len = MLX5_CAP_ROCE(dev->mdev,
527 roce_address_table_size);
528 props->max_mtu = IB_MTU_4096;
529 props->max_msg_sz = 1 << MLX5_CAP_GEN(dev->mdev, log_max_msg);
530 props->pkey_tbl_len = 1;
531 props->state = IB_PORT_DOWN;
532 props->phys_state = 3;
533
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200534 mlx5_query_nic_vport_qkey_viol_cntr(mdev, &qkey_viol_cntr);
Leon Romanovskyc876a1b2016-01-09 13:06:25 +0200535 props->qkey_viol_cntr = qkey_viol_cntr;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200536
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200537 /* If this is a stub query for an unaffiliated port stop here */
538 if (!put_mdev)
539 goto out;
540
Achiad Shochat3f89a642015-12-23 18:47:21 +0200541 ndev = mlx5_ib_get_netdev(device, port_num);
542 if (!ndev)
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200543 goto out;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200544
Aviv Heller7c34ec12018-08-23 13:47:53 +0300545 if (dev->lag_active) {
Aviv Heller88621df2016-09-18 20:48:02 +0300546 rcu_read_lock();
547 upper = netdev_master_upper_dev_get_rcu(ndev);
548 if (upper) {
549 dev_put(ndev);
550 ndev = upper;
551 dev_hold(ndev);
552 }
553 rcu_read_unlock();
554 }
555
Achiad Shochat3f89a642015-12-23 18:47:21 +0200556 if (netif_running(ndev) && netif_carrier_ok(ndev)) {
557 props->state = IB_PORT_ACTIVE;
558 props->phys_state = 5;
559 }
560
561 ndev_ib_mtu = iboe_get_mtu(ndev->mtu);
562
563 dev_put(ndev);
564
565 props->active_mtu = min(props->max_mtu, ndev_ib_mtu);
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200566out:
567 if (put_mdev)
568 mlx5_ib_put_native_port_mdev(dev, port_num);
569 return err;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200570}
571
Parav Panditcf34e1f2019-01-27 20:35:50 +0200572struct mlx5_ib_vlan_info {
573 u16 vlan_id;
574 bool vlan;
575};
576
577static int get_lower_dev_vlan(struct net_device *lower_dev, void *data)
578{
579 struct mlx5_ib_vlan_info *vlan_info = data;
580
581 if (is_vlan_dev(lower_dev)) {
582 vlan_info->vlan = true;
583 vlan_info->vlan_id = vlan_dev_vlan_id(lower_dev);
584 }
585 /* We are interested only in first level vlan device, so
586 * always return 1 to stop iterating over next level devices.
587 */
588 return 1;
589}
590
Ilan Tayari095b0922017-05-14 16:04:30 +0300591static int set_roce_addr(struct mlx5_ib_dev *dev, u8 port_num,
592 unsigned int index, const union ib_gid *gid,
593 const struct ib_gid_attr *attr)
Achiad Shochat3cca2602015-12-23 18:47:23 +0200594{
Ilan Tayari095b0922017-05-14 16:04:30 +0300595 enum ib_gid_type gid_type = IB_GID_TYPE_IB;
Parav Panditcf34e1f2019-01-27 20:35:50 +0200596 struct mlx5_ib_vlan_info vlan_info = { };
Ilan Tayari095b0922017-05-14 16:04:30 +0300597 u8 roce_version = 0;
598 u8 roce_l3_type = 0;
Ilan Tayari095b0922017-05-14 16:04:30 +0300599 u8 mac[ETH_ALEN];
Achiad Shochat3cca2602015-12-23 18:47:23 +0200600
Ilan Tayari095b0922017-05-14 16:04:30 +0300601 if (gid) {
602 gid_type = attr->gid_type;
603 ether_addr_copy(mac, attr->ndev->dev_addr);
Achiad Shochat3cca2602015-12-23 18:47:23 +0200604
Ilan Tayari095b0922017-05-14 16:04:30 +0300605 if (is_vlan_dev(attr->ndev)) {
Parav Panditcf34e1f2019-01-27 20:35:50 +0200606 vlan_info.vlan = true;
607 vlan_info.vlan_id = vlan_dev_vlan_id(attr->ndev);
608 } else {
609 /* If the netdev is upper device and if it's lower
610 * lower device is vlan device, consider vlan id of
611 * the lower vlan device for this gid entry.
612 */
613 rcu_read_lock();
614 netdev_walk_all_lower_dev_rcu(attr->ndev,
615 get_lower_dev_vlan, &vlan_info);
616 rcu_read_unlock();
Ilan Tayari095b0922017-05-14 16:04:30 +0300617 }
Achiad Shochat3cca2602015-12-23 18:47:23 +0200618 }
619
Ilan Tayari095b0922017-05-14 16:04:30 +0300620 switch (gid_type) {
Achiad Shochat3cca2602015-12-23 18:47:23 +0200621 case IB_GID_TYPE_IB:
Ilan Tayari095b0922017-05-14 16:04:30 +0300622 roce_version = MLX5_ROCE_VERSION_1;
Achiad Shochat3cca2602015-12-23 18:47:23 +0200623 break;
624 case IB_GID_TYPE_ROCE_UDP_ENCAP:
Ilan Tayari095b0922017-05-14 16:04:30 +0300625 roce_version = MLX5_ROCE_VERSION_2;
626 if (ipv6_addr_v4mapped((void *)gid))
627 roce_l3_type = MLX5_ROCE_L3_TYPE_IPV4;
628 else
629 roce_l3_type = MLX5_ROCE_L3_TYPE_IPV6;
Achiad Shochat3cca2602015-12-23 18:47:23 +0200630 break;
631
632 default:
Ilan Tayari095b0922017-05-14 16:04:30 +0300633 mlx5_ib_warn(dev, "Unexpected GID type %u\n", gid_type);
Achiad Shochat3cca2602015-12-23 18:47:23 +0200634 }
635
Ilan Tayari095b0922017-05-14 16:04:30 +0300636 return mlx5_core_roce_gid_set(dev->mdev, index, roce_version,
Parav Panditcf34e1f2019-01-27 20:35:50 +0200637 roce_l3_type, gid->raw, mac,
638 vlan_info.vlan, vlan_info.vlan_id,
639 port_num);
Achiad Shochat3cca2602015-12-23 18:47:23 +0200640}
641
Parav Panditf4df9a72018-06-05 08:40:16 +0300642static int mlx5_ib_add_gid(const struct ib_gid_attr *attr,
Achiad Shochat3cca2602015-12-23 18:47:23 +0200643 __always_unused void **context)
644{
Parav Pandit414448d2018-04-01 15:08:24 +0300645 return set_roce_addr(to_mdev(attr->device), attr->port_num,
Parav Panditf4df9a72018-06-05 08:40:16 +0300646 attr->index, &attr->gid, attr);
Achiad Shochat3cca2602015-12-23 18:47:23 +0200647}
648
Parav Pandit414448d2018-04-01 15:08:24 +0300649static int mlx5_ib_del_gid(const struct ib_gid_attr *attr,
650 __always_unused void **context)
Achiad Shochat3cca2602015-12-23 18:47:23 +0200651{
Parav Pandit414448d2018-04-01 15:08:24 +0300652 return set_roce_addr(to_mdev(attr->device), attr->port_num,
653 attr->index, NULL, NULL);
Achiad Shochat3cca2602015-12-23 18:47:23 +0200654}
655
Parav Pandit47ec3862018-06-13 10:22:06 +0300656__be16 mlx5_get_roce_udp_sport(struct mlx5_ib_dev *dev,
657 const struct ib_gid_attr *attr)
Achiad Shochat2811ba52015-12-23 18:47:24 +0200658{
Parav Pandit47ec3862018-06-13 10:22:06 +0300659 if (attr->gid_type != IB_GID_TYPE_ROCE_UDP_ENCAP)
Achiad Shochat2811ba52015-12-23 18:47:24 +0200660 return 0;
661
662 return cpu_to_be16(MLX5_CAP_ROCE(dev->mdev, r_roce_min_src_udp_port));
663}
664
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300665static int mlx5_use_mad_ifc(struct mlx5_ib_dev *dev)
666{
Noa Osherovich7fae6652016-09-12 19:16:23 +0300667 if (MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_IB)
668 return !MLX5_CAP_GEN(dev->mdev, ib_virt);
669 return 0;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300670}
671
672enum {
673 MLX5_VPORT_ACCESS_METHOD_MAD,
674 MLX5_VPORT_ACCESS_METHOD_HCA,
675 MLX5_VPORT_ACCESS_METHOD_NIC,
676};
677
678static int mlx5_get_vport_access_method(struct ib_device *ibdev)
679{
680 if (mlx5_use_mad_ifc(to_mdev(ibdev)))
681 return MLX5_VPORT_ACCESS_METHOD_MAD;
682
Achiad Shochatebd61f62015-12-23 18:47:16 +0200683 if (mlx5_ib_port_link_layer(ibdev, 1) ==
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300684 IB_LINK_LAYER_ETHERNET)
685 return MLX5_VPORT_ACCESS_METHOD_NIC;
686
687 return MLX5_VPORT_ACCESS_METHOD_HCA;
688}
689
Eran Ben Elishada7525d2015-12-14 16:34:10 +0200690static void get_atomic_caps(struct mlx5_ib_dev *dev,
Moni Shoua776a3902018-01-02 16:19:33 +0200691 u8 atomic_size_qp,
Eran Ben Elishada7525d2015-12-14 16:34:10 +0200692 struct ib_device_attr *props)
693{
694 u8 tmp;
695 u8 atomic_operations = MLX5_CAP_ATOMIC(dev->mdev, atomic_operations);
Eran Ben Elishada7525d2015-12-14 16:34:10 +0200696 u8 atomic_req_8B_endianness_mode =
Or Gerlitzbd108382017-05-28 15:24:17 +0300697 MLX5_CAP_ATOMIC(dev->mdev, atomic_req_8B_endianness_mode);
Eran Ben Elishada7525d2015-12-14 16:34:10 +0200698
699 /* Check if HW supports 8 bytes standard atomic operations and capable
700 * of host endianness respond
701 */
702 tmp = MLX5_ATOMIC_OPS_CMP_SWAP | MLX5_ATOMIC_OPS_FETCH_ADD;
703 if (((atomic_operations & tmp) == tmp) &&
704 (atomic_size_qp & MLX5_ATOMIC_SIZE_QP_8BYTES) &&
705 (atomic_req_8B_endianness_mode)) {
706 props->atomic_cap = IB_ATOMIC_HCA;
707 } else {
708 props->atomic_cap = IB_ATOMIC_NONE;
709 }
710}
711
Moni Shoua776a3902018-01-02 16:19:33 +0200712static void get_atomic_caps_qp(struct mlx5_ib_dev *dev,
713 struct ib_device_attr *props)
714{
715 u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_qp);
716
717 get_atomic_caps(dev, atomic_size_qp, props);
718}
719
720static void get_atomic_caps_dc(struct mlx5_ib_dev *dev,
721 struct ib_device_attr *props)
722{
723 u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_dc);
724
725 get_atomic_caps(dev, atomic_size_qp, props);
726}
727
728bool mlx5_ib_dc_atomic_is_supported(struct mlx5_ib_dev *dev)
729{
730 struct ib_device_attr props = {};
731
732 get_atomic_caps_dc(dev, &props);
733 return (props.atomic_cap == IB_ATOMIC_HCA) ? true : false;
734}
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300735static int mlx5_query_system_image_guid(struct ib_device *ibdev,
736 __be64 *sys_image_guid)
737{
738 struct mlx5_ib_dev *dev = to_mdev(ibdev);
739 struct mlx5_core_dev *mdev = dev->mdev;
740 u64 tmp;
741 int err;
742
743 switch (mlx5_get_vport_access_method(ibdev)) {
744 case MLX5_VPORT_ACCESS_METHOD_MAD:
745 return mlx5_query_mad_ifc_system_image_guid(ibdev,
746 sys_image_guid);
747
748 case MLX5_VPORT_ACCESS_METHOD_HCA:
749 err = mlx5_query_hca_vport_system_image_guid(mdev, &tmp);
Achiad Shochat3f89a642015-12-23 18:47:21 +0200750 break;
751
752 case MLX5_VPORT_ACCESS_METHOD_NIC:
753 err = mlx5_query_nic_vport_system_image_guid(mdev, &tmp);
754 break;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300755
756 default:
757 return -EINVAL;
758 }
Achiad Shochat3f89a642015-12-23 18:47:21 +0200759
760 if (!err)
761 *sys_image_guid = cpu_to_be64(tmp);
762
763 return err;
764
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300765}
766
767static int mlx5_query_max_pkeys(struct ib_device *ibdev,
768 u16 *max_pkeys)
769{
770 struct mlx5_ib_dev *dev = to_mdev(ibdev);
771 struct mlx5_core_dev *mdev = dev->mdev;
772
773 switch (mlx5_get_vport_access_method(ibdev)) {
774 case MLX5_VPORT_ACCESS_METHOD_MAD:
775 return mlx5_query_mad_ifc_max_pkeys(ibdev, max_pkeys);
776
777 case MLX5_VPORT_ACCESS_METHOD_HCA:
778 case MLX5_VPORT_ACCESS_METHOD_NIC:
779 *max_pkeys = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev,
780 pkey_table_size));
781 return 0;
782
783 default:
784 return -EINVAL;
785 }
786}
787
788static int mlx5_query_vendor_id(struct ib_device *ibdev,
789 u32 *vendor_id)
790{
791 struct mlx5_ib_dev *dev = to_mdev(ibdev);
792
793 switch (mlx5_get_vport_access_method(ibdev)) {
794 case MLX5_VPORT_ACCESS_METHOD_MAD:
795 return mlx5_query_mad_ifc_vendor_id(ibdev, vendor_id);
796
797 case MLX5_VPORT_ACCESS_METHOD_HCA:
798 case MLX5_VPORT_ACCESS_METHOD_NIC:
799 return mlx5_core_query_vendor_id(dev->mdev, vendor_id);
800
801 default:
802 return -EINVAL;
803 }
804}
805
806static int mlx5_query_node_guid(struct mlx5_ib_dev *dev,
807 __be64 *node_guid)
808{
809 u64 tmp;
810 int err;
811
812 switch (mlx5_get_vport_access_method(&dev->ib_dev)) {
813 case MLX5_VPORT_ACCESS_METHOD_MAD:
814 return mlx5_query_mad_ifc_node_guid(dev, node_guid);
815
816 case MLX5_VPORT_ACCESS_METHOD_HCA:
817 err = mlx5_query_hca_vport_node_guid(dev->mdev, &tmp);
Achiad Shochat3f89a642015-12-23 18:47:21 +0200818 break;
819
820 case MLX5_VPORT_ACCESS_METHOD_NIC:
821 err = mlx5_query_nic_vport_node_guid(dev->mdev, &tmp);
822 break;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300823
824 default:
825 return -EINVAL;
826 }
Achiad Shochat3f89a642015-12-23 18:47:21 +0200827
828 if (!err)
829 *node_guid = cpu_to_be64(tmp);
830
831 return err;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300832}
833
834struct mlx5_reg_node_desc {
Yuval Shaiabd99fde2016-08-25 10:57:07 -0700835 u8 desc[IB_DEVICE_NODE_DESC_MAX];
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300836};
837
838static int mlx5_query_node_desc(struct mlx5_ib_dev *dev, char *node_desc)
839{
840 struct mlx5_reg_node_desc in;
841
842 if (mlx5_use_mad_ifc(dev))
843 return mlx5_query_mad_ifc_node_desc(dev, node_desc);
844
845 memset(&in, 0, sizeof(in));
846
847 return mlx5_core_access_reg(dev->mdev, &in, sizeof(in), node_desc,
848 sizeof(struct mlx5_reg_node_desc),
849 MLX5_REG_NODE_DESC, 0, 0);
850}
851
Eli Cohene126ba92013-07-07 17:25:49 +0300852static int mlx5_ib_query_device(struct ib_device *ibdev,
Matan Barak2528e332015-06-11 16:35:25 +0300853 struct ib_device_attr *props,
854 struct ib_udata *uhw)
Eli Cohene126ba92013-07-07 17:25:49 +0300855{
856 struct mlx5_ib_dev *dev = to_mdev(ibdev);
Saeed Mahameed938fe832015-05-28 22:28:41 +0300857 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +0300858 int err = -ENOMEM;
Eli Cohen288c01b2016-10-27 16:36:45 +0300859 int max_sq_desc;
Eli Cohene126ba92013-07-07 17:25:49 +0300860 int max_rq_sg;
861 int max_sq_sg;
Sagi Grimberge0238a62015-07-21 14:40:12 +0300862 u64 min_page_size = 1ull << MLX5_CAP_GEN(mdev, log_pg_sz);
Daniel Jurgens85c7c012018-01-04 17:25:43 +0200863 bool raw_support = !mlx5_core_mp_enabled(mdev);
Bodong Wang402ca532016-06-17 15:02:20 +0300864 struct mlx5_ib_query_device_resp resp = {};
865 size_t resp_len;
866 u64 max_tso;
Eli Cohene126ba92013-07-07 17:25:49 +0300867
Bodong Wang402ca532016-06-17 15:02:20 +0300868 resp_len = sizeof(resp.comp_mask) + sizeof(resp.response_length);
869 if (uhw->outlen && uhw->outlen < resp_len)
870 return -EINVAL;
871 else
872 resp.response_length = resp_len;
873
874 if (uhw->inlen && !ib_is_udata_cleared(uhw, 0, uhw->inlen))
Matan Barak2528e332015-06-11 16:35:25 +0300875 return -EINVAL;
876
Eli Cohene126ba92013-07-07 17:25:49 +0300877 memset(props, 0, sizeof(*props));
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300878 err = mlx5_query_system_image_guid(ibdev,
879 &props->sys_image_guid);
880 if (err)
881 return err;
882
883 err = mlx5_query_max_pkeys(ibdev, &props->max_pkeys);
884 if (err)
885 return err;
886
887 err = mlx5_query_vendor_id(ibdev, &props->vendor_id);
888 if (err)
889 return err;
Eli Cohene126ba92013-07-07 17:25:49 +0300890
Jack Morgenstein9603b612014-07-28 23:30:22 +0300891 props->fw_ver = ((u64)fw_rev_maj(dev->mdev) << 32) |
892 (fw_rev_min(dev->mdev) << 16) |
893 fw_rev_sub(dev->mdev);
Eli Cohene126ba92013-07-07 17:25:49 +0300894 props->device_cap_flags = IB_DEVICE_CHANGE_PHY_PORT |
895 IB_DEVICE_PORT_ACTIVE_EVENT |
896 IB_DEVICE_SYS_IMAGE_GUID |
Eli Cohen1a4c3a32014-02-06 17:41:25 +0200897 IB_DEVICE_RC_RNR_NAK_GEN;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300898
899 if (MLX5_CAP_GEN(mdev, pkv))
Eli Cohene126ba92013-07-07 17:25:49 +0300900 props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300901 if (MLX5_CAP_GEN(mdev, qkv))
Eli Cohene126ba92013-07-07 17:25:49 +0300902 props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300903 if (MLX5_CAP_GEN(mdev, apm))
Eli Cohene126ba92013-07-07 17:25:49 +0300904 props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300905 if (MLX5_CAP_GEN(mdev, xrc))
Eli Cohene126ba92013-07-07 17:25:49 +0300906 props->device_cap_flags |= IB_DEVICE_XRC;
Matan Barakd2370e02016-02-29 18:05:30 +0200907 if (MLX5_CAP_GEN(mdev, imaicl)) {
908 props->device_cap_flags |= IB_DEVICE_MEM_WINDOW |
909 IB_DEVICE_MEM_WINDOW_TYPE_2B;
910 props->max_mw = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
Sagi Grimbergb005d312016-02-29 19:07:33 +0200911 /* We support 'Gappy' memory registration too */
912 props->device_cap_flags |= IB_DEVICE_SG_GAPS_REG;
Matan Barakd2370e02016-02-29 18:05:30 +0200913 }
Eli Cohene126ba92013-07-07 17:25:49 +0300914 props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300915 if (MLX5_CAP_GEN(mdev, sho)) {
Sagi Grimberg2dea9092014-02-23 14:19:13 +0200916 props->device_cap_flags |= IB_DEVICE_SIGNATURE_HANDOVER;
917 /* At this stage no support for signature handover */
918 props->sig_prot_cap = IB_PROT_T10DIF_TYPE_1 |
919 IB_PROT_T10DIF_TYPE_2 |
920 IB_PROT_T10DIF_TYPE_3;
921 props->sig_guard_cap = IB_GUARD_T10DIF_CRC |
922 IB_GUARD_T10DIF_CSUM;
923 }
Saeed Mahameed938fe832015-05-28 22:28:41 +0300924 if (MLX5_CAP_GEN(mdev, block_lb_mc))
Eli Cohenf360d882014-04-02 00:10:16 +0300925 props->device_cap_flags |= IB_DEVICE_BLOCK_MULTICAST_LOOPBACK;
Eli Cohene126ba92013-07-07 17:25:49 +0300926
Daniel Jurgens85c7c012018-01-04 17:25:43 +0200927 if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) && raw_support) {
Noa Osheroviche8161332017-01-18 15:40:01 +0200928 if (MLX5_CAP_ETH(mdev, csum_cap)) {
929 /* Legacy bit to support old userspace libraries */
Bodong Wang88115fe2015-12-18 13:53:20 +0200930 props->device_cap_flags |= IB_DEVICE_RAW_IP_CSUM;
Noa Osheroviche8161332017-01-18 15:40:01 +0200931 props->raw_packet_caps |= IB_RAW_PACKET_CAP_IP_CSUM;
932 }
933
934 if (MLX5_CAP_ETH(dev->mdev, vlan_cap))
935 props->raw_packet_caps |=
936 IB_RAW_PACKET_CAP_CVLAN_STRIPPING;
Bodong Wang88115fe2015-12-18 13:53:20 +0200937
Bodong Wang402ca532016-06-17 15:02:20 +0300938 if (field_avail(typeof(resp), tso_caps, uhw->outlen)) {
939 max_tso = MLX5_CAP_ETH(mdev, max_lso_cap);
940 if (max_tso) {
941 resp.tso_caps.max_tso = 1 << max_tso;
942 resp.tso_caps.supported_qpts |=
943 1 << IB_QPT_RAW_PACKET;
944 resp.response_length += sizeof(resp.tso_caps);
945 }
946 }
Yishai Hadas31f69a82016-08-28 11:28:45 +0300947
948 if (field_avail(typeof(resp), rss_caps, uhw->outlen)) {
949 resp.rss_caps.rx_hash_function =
950 MLX5_RX_HASH_FUNC_TOEPLITZ;
951 resp.rss_caps.rx_hash_fields_mask =
952 MLX5_RX_HASH_SRC_IPV4 |
953 MLX5_RX_HASH_DST_IPV4 |
954 MLX5_RX_HASH_SRC_IPV6 |
955 MLX5_RX_HASH_DST_IPV6 |
956 MLX5_RX_HASH_SRC_PORT_TCP |
957 MLX5_RX_HASH_DST_PORT_TCP |
958 MLX5_RX_HASH_SRC_PORT_UDP |
Maor Gottlieb4e2b53a2017-12-24 14:51:25 +0200959 MLX5_RX_HASH_DST_PORT_UDP |
960 MLX5_RX_HASH_INNER;
Matan Barak2d93fc82018-03-28 09:27:55 +0300961 if (mlx5_accel_ipsec_device_caps(dev->mdev) &
962 MLX5_ACCEL_IPSEC_CAP_DEVICE)
963 resp.rss_caps.rx_hash_fields_mask |=
964 MLX5_RX_HASH_IPSEC_SPI;
Yishai Hadas31f69a82016-08-28 11:28:45 +0300965 resp.response_length += sizeof(resp.rss_caps);
966 }
967 } else {
968 if (field_avail(typeof(resp), tso_caps, uhw->outlen))
969 resp.response_length += sizeof(resp.tso_caps);
970 if (field_avail(typeof(resp), rss_caps, uhw->outlen))
971 resp.response_length += sizeof(resp.rss_caps);
Bodong Wang402ca532016-06-17 15:02:20 +0300972 }
973
Erez Shitritf0313962016-02-21 16:27:17 +0200974 if (MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) {
975 props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
976 props->device_cap_flags |= IB_DEVICE_UD_TSO;
977 }
978
Maor Gottlieb03404e82017-05-30 10:29:13 +0300979 if (MLX5_CAP_GEN(dev->mdev, rq_delay_drop) &&
Daniel Jurgens85c7c012018-01-04 17:25:43 +0200980 MLX5_CAP_GEN(dev->mdev, general_notification_event) &&
981 raw_support)
Maor Gottlieb03404e82017-05-30 10:29:13 +0300982 props->raw_packet_caps |= IB_RAW_PACKET_CAP_DELAY_DROP;
983
Yishai Hadas1d54f892017-06-08 16:15:11 +0300984 if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads) &&
985 MLX5_CAP_IPOIB_ENHANCED(mdev, csum_cap))
986 props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
987
Majd Dibbinycff5a0f2016-04-17 17:19:38 +0300988 if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
Daniel Jurgens85c7c012018-01-04 17:25:43 +0200989 MLX5_CAP_ETH(dev->mdev, scatter_fcs) &&
990 raw_support) {
Noa Osheroviche8161332017-01-18 15:40:01 +0200991 /* Legacy bit to support old userspace libraries */
Majd Dibbinycff5a0f2016-04-17 17:19:38 +0300992 props->device_cap_flags |= IB_DEVICE_RAW_SCATTER_FCS;
Noa Osheroviche8161332017-01-18 15:40:01 +0200993 props->raw_packet_caps |= IB_RAW_PACKET_CAP_SCATTER_FCS;
994 }
Majd Dibbinycff5a0f2016-04-17 17:19:38 +0300995
Ariel Levkovich24da0012018-04-05 18:53:27 +0300996 if (MLX5_CAP_DEV_MEM(mdev, memic)) {
997 props->max_dm_size =
998 MLX5_CAP_DEV_MEM(mdev, max_memic_size);
999 }
1000
Maor Gottliebda6d6ba32016-06-04 15:15:28 +03001001 if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS))
1002 props->device_cap_flags |= IB_DEVICE_MANAGED_FLOW_STEERING;
1003
Noa Osherovichb1383aa2017-10-29 13:59:45 +02001004 if (MLX5_CAP_GEN(mdev, end_pad))
1005 props->device_cap_flags |= IB_DEVICE_PCI_WRITE_END_PADDING;
1006
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001007 props->vendor_part_id = mdev->pdev->device;
1008 props->hw_ver = mdev->pdev->revision;
Eli Cohene126ba92013-07-07 17:25:49 +03001009
1010 props->max_mr_size = ~0ull;
Sagi Grimberge0238a62015-07-21 14:40:12 +03001011 props->page_size_cap = ~(min_page_size - 1);
Saeed Mahameed938fe832015-05-28 22:28:41 +03001012 props->max_qp = 1 << MLX5_CAP_GEN(mdev, log_max_qp);
1013 props->max_qp_wr = 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
1014 max_rq_sg = MLX5_CAP_GEN(mdev, max_wqe_sz_rq) /
1015 sizeof(struct mlx5_wqe_data_seg);
Eli Cohen288c01b2016-10-27 16:36:45 +03001016 max_sq_desc = min_t(int, MLX5_CAP_GEN(mdev, max_wqe_sz_sq), 512);
1017 max_sq_sg = (max_sq_desc - sizeof(struct mlx5_wqe_ctrl_seg) -
1018 sizeof(struct mlx5_wqe_raddr_seg)) /
1019 sizeof(struct mlx5_wqe_data_seg);
Steve Wise33023fb2018-06-18 08:05:26 -07001020 props->max_send_sge = max_sq_sg;
1021 props->max_recv_sge = max_rq_sg;
Sagi Grimberg986ef952016-03-31 19:03:25 +03001022 props->max_sge_rd = MLX5_MAX_SGE_RD;
Saeed Mahameed938fe832015-05-28 22:28:41 +03001023 props->max_cq = 1 << MLX5_CAP_GEN(mdev, log_max_cq);
Leon Romanovsky9f177682016-01-14 08:11:40 +02001024 props->max_cqe = (1 << MLX5_CAP_GEN(mdev, log_max_cq_sz)) - 1;
Saeed Mahameed938fe832015-05-28 22:28:41 +03001025 props->max_mr = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
1026 props->max_pd = 1 << MLX5_CAP_GEN(mdev, log_max_pd);
1027 props->max_qp_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_req_qp);
1028 props->max_qp_init_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_res_qp);
1029 props->max_srq = 1 << MLX5_CAP_GEN(mdev, log_max_srq);
1030 props->max_srq_wr = (1 << MLX5_CAP_GEN(mdev, log_max_srq_sz)) - 1;
1031 props->local_ca_ack_delay = MLX5_CAP_GEN(mdev, local_ca_ack_delay);
Eli Cohene126ba92013-07-07 17:25:49 +03001032 props->max_res_rd_atom = props->max_qp_rd_atom * props->max_qp;
Eli Cohene126ba92013-07-07 17:25:49 +03001033 props->max_srq_sge = max_rq_sg - 1;
Sagi Grimberg911f4332016-03-03 13:37:51 +02001034 props->max_fast_reg_page_list_len =
1035 1 << MLX5_CAP_GEN(mdev, log_max_klm_list_size);
Moni Shoua776a3902018-01-02 16:19:33 +02001036 get_atomic_caps_qp(dev, props);
Eli Cohen81bea282013-09-11 16:35:30 +03001037 props->masked_atomic_cap = IB_ATOMIC_NONE;
Saeed Mahameed938fe832015-05-28 22:28:41 +03001038 props->max_mcast_grp = 1 << MLX5_CAP_GEN(mdev, log_max_mcg);
1039 props->max_mcast_qp_attach = MLX5_CAP_GEN(mdev, max_qp_mcg);
Eli Cohene126ba92013-07-07 17:25:49 +03001040 props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
1041 props->max_mcast_grp;
1042 props->max_map_per_fmr = INT_MAX; /* no limit in ConnectIB */
Maor Gottlieb86695a62016-10-27 16:36:38 +03001043 props->max_ah = INT_MAX;
Matan Barak7c60bcb2015-12-15 20:30:11 +02001044 props->hca_core_clock = MLX5_CAP_GEN(mdev, device_frequency_khz);
1045 props->timestamp_mask = 0x7FFFFFFFFFFFFFFFULL;
Eli Cohene126ba92013-07-07 17:25:49 +03001046
Leon Romanovskye502b8b2019-01-08 16:07:24 +02001047 if (IS_ENABLED(CONFIG_INFINIBAND_ON_DEMAND_PAGING)) {
1048 if (MLX5_CAP_GEN(mdev, pg))
1049 props->device_cap_flags |= IB_DEVICE_ON_DEMAND_PAGING;
1050 props->odp_caps = dev->odp_caps;
1051 }
Haggai Eran8cdd3122014-12-11 17:04:20 +02001052
Leon Romanovsky051f2632015-12-20 12:16:11 +02001053 if (MLX5_CAP_GEN(mdev, cd))
1054 props->device_cap_flags |= IB_DEVICE_CROSS_CHANNEL;
1055
Eli Coheneff901d2016-03-11 22:58:42 +02001056 if (!mlx5_core_is_pf(mdev))
1057 props->device_cap_flags |= IB_DEVICE_VIRTUAL_FUNCTION;
1058
Yishai Hadas31f69a82016-08-28 11:28:45 +03001059 if (mlx5_ib_port_link_layer(ibdev, 1) ==
Daniel Jurgens85c7c012018-01-04 17:25:43 +02001060 IB_LINK_LAYER_ETHERNET && raw_support) {
Yishai Hadas31f69a82016-08-28 11:28:45 +03001061 props->rss_caps.max_rwq_indirection_tables =
1062 1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt);
1063 props->rss_caps.max_rwq_indirection_table_size =
1064 1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt_size);
1065 props->rss_caps.supported_qpts = 1 << IB_QPT_RAW_PACKET;
1066 props->max_wq_type_rq =
1067 1 << MLX5_CAP_GEN(dev->mdev, log_max_rq);
1068 }
1069
Artemy Kovalyoveb761892017-08-17 15:52:09 +03001070 if (MLX5_CAP_GEN(mdev, tag_matching)) {
Leon Romanovsky78b1beb2017-09-24 21:46:29 +03001071 props->tm_caps.max_rndv_hdr_size = MLX5_TM_MAX_RNDV_MSG_SIZE;
1072 props->tm_caps.max_num_tags =
Artemy Kovalyoveb761892017-08-17 15:52:09 +03001073 (1 << MLX5_CAP_GEN(mdev, log_tag_matching_list_sz)) - 1;
Leon Romanovsky78b1beb2017-09-24 21:46:29 +03001074 props->tm_caps.flags = IB_TM_CAP_RC;
1075 props->tm_caps.max_ops =
Artemy Kovalyoveb761892017-08-17 15:52:09 +03001076 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
Leon Romanovsky78b1beb2017-09-24 21:46:29 +03001077 props->tm_caps.max_sge = MLX5_TM_MAX_SGE;
Artemy Kovalyoveb761892017-08-17 15:52:09 +03001078 }
1079
Yonatan Cohen87ab3f52017-11-13 10:51:18 +02001080 if (MLX5_CAP_GEN(dev->mdev, cq_moderation)) {
1081 props->cq_caps.max_cq_moderation_count =
1082 MLX5_MAX_CQ_COUNT;
1083 props->cq_caps.max_cq_moderation_period =
1084 MLX5_MAX_CQ_PERIOD;
1085 }
1086
Bodong Wang7e43a2a2016-10-31 12:16:44 +02001087 if (field_avail(typeof(resp), cqe_comp_caps, uhw->outlen)) {
Bodong Wang7e43a2a2016-10-31 12:16:44 +02001088 resp.response_length += sizeof(resp.cqe_comp_caps);
Yonatan Cohen572f46b2018-05-27 13:42:33 +03001089
1090 if (MLX5_CAP_GEN(dev->mdev, cqe_compression)) {
1091 resp.cqe_comp_caps.max_num =
1092 MLX5_CAP_GEN(dev->mdev,
1093 cqe_compression_max_num);
1094
1095 resp.cqe_comp_caps.supported_format =
1096 MLX5_IB_CQE_RES_FORMAT_HASH |
1097 MLX5_IB_CQE_RES_FORMAT_CSUM;
Yonatan Cohen6f1006a2018-05-27 13:42:34 +03001098
1099 if (MLX5_CAP_GEN(dev->mdev, mini_cqe_resp_stride_index))
1100 resp.cqe_comp_caps.supported_format |=
1101 MLX5_IB_CQE_RES_FORMAT_CSUM_STRIDX;
Yonatan Cohen572f46b2018-05-27 13:42:33 +03001102 }
Bodong Wang7e43a2a2016-10-31 12:16:44 +02001103 }
1104
Daniel Jurgens85c7c012018-01-04 17:25:43 +02001105 if (field_avail(typeof(resp), packet_pacing_caps, uhw->outlen) &&
1106 raw_support) {
Bodong Wangd9491672016-12-01 13:43:13 +02001107 if (MLX5_CAP_QOS(mdev, packet_pacing) &&
1108 MLX5_CAP_GEN(mdev, qos)) {
1109 resp.packet_pacing_caps.qp_rate_limit_max =
1110 MLX5_CAP_QOS(mdev, packet_pacing_max_rate);
1111 resp.packet_pacing_caps.qp_rate_limit_min =
1112 MLX5_CAP_QOS(mdev, packet_pacing_min_rate);
1113 resp.packet_pacing_caps.supported_qpts |=
1114 1 << IB_QPT_RAW_PACKET;
Bodong Wang61147f32018-03-19 15:10:30 +02001115 if (MLX5_CAP_QOS(mdev, packet_pacing_burst_bound) &&
1116 MLX5_CAP_QOS(mdev, packet_pacing_typical_size))
1117 resp.packet_pacing_caps.cap_flags |=
1118 MLX5_IB_PP_SUPPORT_BURST;
Bodong Wangd9491672016-12-01 13:43:13 +02001119 }
1120 resp.response_length += sizeof(resp.packet_pacing_caps);
1121 }
1122
Leon Romanovsky9f885202017-01-02 11:37:39 +02001123 if (field_avail(typeof(resp), mlx5_ib_support_multi_pkt_send_wqes,
1124 uhw->outlen)) {
Bodong Wang795b6092017-08-17 15:52:34 +03001125 if (MLX5_CAP_ETH(mdev, multi_pkt_send_wqe))
1126 resp.mlx5_ib_support_multi_pkt_send_wqes =
1127 MLX5_IB_ALLOW_MPW;
Bodong Wang050da902017-08-17 15:52:35 +03001128
1129 if (MLX5_CAP_ETH(mdev, enhanced_multi_pkt_send_wqe))
1130 resp.mlx5_ib_support_multi_pkt_send_wqes |=
1131 MLX5_IB_SUPPORT_EMPW;
1132
Leon Romanovsky9f885202017-01-02 11:37:39 +02001133 resp.response_length +=
1134 sizeof(resp.mlx5_ib_support_multi_pkt_send_wqes);
1135 }
1136
Guy Levide57f2a2017-10-19 08:25:52 +03001137 if (field_avail(typeof(resp), flags, uhw->outlen)) {
1138 resp.response_length += sizeof(resp.flags);
Guy Levi7a0c8f42017-10-19 08:25:53 +03001139
Guy Levide57f2a2017-10-19 08:25:52 +03001140 if (MLX5_CAP_GEN(mdev, cqe_compression_128))
1141 resp.flags |=
1142 MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_COMP;
Guy Levi7a0c8f42017-10-19 08:25:53 +03001143
1144 if (MLX5_CAP_GEN(mdev, cqe_128_always))
1145 resp.flags |= MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_PAD;
Danit Goldberg7e11b912018-11-30 13:22:06 +02001146 if (MLX5_CAP_GEN(mdev, qp_packet_based))
1147 resp.flags |=
1148 MLX5_IB_QUERY_DEV_RESP_PACKET_BASED_CREDIT_MODE;
Guy Levide57f2a2017-10-19 08:25:52 +03001149 }
Leon Romanovsky9f885202017-01-02 11:37:39 +02001150
Noa Osherovich96dc3fc2017-08-17 15:52:28 +03001151 if (field_avail(typeof(resp), sw_parsing_caps,
1152 uhw->outlen)) {
1153 resp.response_length += sizeof(resp.sw_parsing_caps);
1154 if (MLX5_CAP_ETH(mdev, swp)) {
1155 resp.sw_parsing_caps.sw_parsing_offloads |=
1156 MLX5_IB_SW_PARSING;
1157
1158 if (MLX5_CAP_ETH(mdev, swp_csum))
1159 resp.sw_parsing_caps.sw_parsing_offloads |=
1160 MLX5_IB_SW_PARSING_CSUM;
1161
1162 if (MLX5_CAP_ETH(mdev, swp_lso))
1163 resp.sw_parsing_caps.sw_parsing_offloads |=
1164 MLX5_IB_SW_PARSING_LSO;
1165
1166 if (resp.sw_parsing_caps.sw_parsing_offloads)
1167 resp.sw_parsing_caps.supported_qpts =
1168 BIT(IB_QPT_RAW_PACKET);
1169 }
1170 }
1171
Daniel Jurgens85c7c012018-01-04 17:25:43 +02001172 if (field_avail(typeof(resp), striding_rq_caps, uhw->outlen) &&
1173 raw_support) {
Noa Osherovichb4f34592017-10-17 18:01:12 +03001174 resp.response_length += sizeof(resp.striding_rq_caps);
1175 if (MLX5_CAP_GEN(mdev, striding_rq)) {
1176 resp.striding_rq_caps.min_single_stride_log_num_of_bytes =
1177 MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES;
1178 resp.striding_rq_caps.max_single_stride_log_num_of_bytes =
1179 MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES;
1180 resp.striding_rq_caps.min_single_wqe_log_num_of_strides =
1181 MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES;
1182 resp.striding_rq_caps.max_single_wqe_log_num_of_strides =
1183 MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES;
1184 resp.striding_rq_caps.supported_qpts =
1185 BIT(IB_QPT_RAW_PACKET);
1186 }
1187 }
1188
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001189 if (field_avail(typeof(resp), tunnel_offloads_caps,
1190 uhw->outlen)) {
1191 resp.response_length += sizeof(resp.tunnel_offloads_caps);
1192 if (MLX5_CAP_ETH(mdev, tunnel_stateless_vxlan))
1193 resp.tunnel_offloads_caps |=
1194 MLX5_IB_TUNNELED_OFFLOADS_VXLAN;
1195 if (MLX5_CAP_ETH(mdev, tunnel_stateless_geneve_rx))
1196 resp.tunnel_offloads_caps |=
1197 MLX5_IB_TUNNELED_OFFLOADS_GENEVE;
1198 if (MLX5_CAP_ETH(mdev, tunnel_stateless_gre))
1199 resp.tunnel_offloads_caps |=
1200 MLX5_IB_TUNNELED_OFFLOADS_GRE;
Ariel Levkoviche818e252018-05-13 14:33:35 +03001201 if (MLX5_CAP_GEN(mdev, flex_parser_protocols) &
1202 MLX5_FLEX_PROTO_CW_MPLS_GRE)
1203 resp.tunnel_offloads_caps |=
1204 MLX5_IB_TUNNELED_OFFLOADS_MPLS_GRE;
1205 if (MLX5_CAP_GEN(mdev, flex_parser_protocols) &
1206 MLX5_FLEX_PROTO_CW_MPLS_UDP)
1207 resp.tunnel_offloads_caps |=
1208 MLX5_IB_TUNNELED_OFFLOADS_MPLS_UDP;
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001209 }
1210
Bodong Wang402ca532016-06-17 15:02:20 +03001211 if (uhw->outlen) {
1212 err = ib_copy_to_udata(uhw, &resp, resp.response_length);
1213
1214 if (err)
1215 return err;
1216 }
1217
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001218 return 0;
1219}
Eli Cohene126ba92013-07-07 17:25:49 +03001220
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001221enum mlx5_ib_width {
1222 MLX5_IB_WIDTH_1X = 1 << 0,
1223 MLX5_IB_WIDTH_2X = 1 << 1,
1224 MLX5_IB_WIDTH_4X = 1 << 2,
1225 MLX5_IB_WIDTH_8X = 1 << 3,
1226 MLX5_IB_WIDTH_12X = 1 << 4
1227};
1228
Michael Guralnikdb7a6912018-11-21 15:03:54 +02001229static void translate_active_width(struct ib_device *ibdev, u8 active_width,
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001230 u8 *ib_width)
1231{
1232 struct mlx5_ib_dev *dev = to_mdev(ibdev);
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001233
Michael Guralnikdb7a6912018-11-21 15:03:54 +02001234 if (active_width & MLX5_IB_WIDTH_1X)
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001235 *ib_width = IB_WIDTH_1X;
Michael Guralnikd7649702018-12-09 11:49:54 +02001236 else if (active_width & MLX5_IB_WIDTH_2X)
1237 *ib_width = IB_WIDTH_2X;
Michael Guralnikdb7a6912018-11-21 15:03:54 +02001238 else if (active_width & MLX5_IB_WIDTH_4X)
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001239 *ib_width = IB_WIDTH_4X;
Michael Guralnikdb7a6912018-11-21 15:03:54 +02001240 else if (active_width & MLX5_IB_WIDTH_8X)
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001241 *ib_width = IB_WIDTH_8X;
Michael Guralnikdb7a6912018-11-21 15:03:54 +02001242 else if (active_width & MLX5_IB_WIDTH_12X)
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001243 *ib_width = IB_WIDTH_12X;
Michael Guralnikdb7a6912018-11-21 15:03:54 +02001244 else {
1245 mlx5_ib_dbg(dev, "Invalid active_width %d, setting width to default value: 4x\n",
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001246 (int)active_width);
Michael Guralnikdb7a6912018-11-21 15:03:54 +02001247 *ib_width = IB_WIDTH_4X;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001248 }
1249
Michael Guralnikdb7a6912018-11-21 15:03:54 +02001250 return;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001251}
1252
1253static int mlx5_mtu_to_ib_mtu(int mtu)
1254{
1255 switch (mtu) {
1256 case 256: return 1;
1257 case 512: return 2;
1258 case 1024: return 3;
1259 case 2048: return 4;
1260 case 4096: return 5;
1261 default:
1262 pr_warn("invalid mtu\n");
1263 return -1;
1264 }
1265}
1266
1267enum ib_max_vl_num {
1268 __IB_MAX_VL_0 = 1,
1269 __IB_MAX_VL_0_1 = 2,
1270 __IB_MAX_VL_0_3 = 3,
1271 __IB_MAX_VL_0_7 = 4,
1272 __IB_MAX_VL_0_14 = 5,
1273};
1274
1275enum mlx5_vl_hw_cap {
1276 MLX5_VL_HW_0 = 1,
1277 MLX5_VL_HW_0_1 = 2,
1278 MLX5_VL_HW_0_2 = 3,
1279 MLX5_VL_HW_0_3 = 4,
1280 MLX5_VL_HW_0_4 = 5,
1281 MLX5_VL_HW_0_5 = 6,
1282 MLX5_VL_HW_0_6 = 7,
1283 MLX5_VL_HW_0_7 = 8,
1284 MLX5_VL_HW_0_14 = 15
1285};
1286
1287static int translate_max_vl_num(struct ib_device *ibdev, u8 vl_hw_cap,
1288 u8 *max_vl_num)
1289{
1290 switch (vl_hw_cap) {
1291 case MLX5_VL_HW_0:
1292 *max_vl_num = __IB_MAX_VL_0;
1293 break;
1294 case MLX5_VL_HW_0_1:
1295 *max_vl_num = __IB_MAX_VL_0_1;
1296 break;
1297 case MLX5_VL_HW_0_3:
1298 *max_vl_num = __IB_MAX_VL_0_3;
1299 break;
1300 case MLX5_VL_HW_0_7:
1301 *max_vl_num = __IB_MAX_VL_0_7;
1302 break;
1303 case MLX5_VL_HW_0_14:
1304 *max_vl_num = __IB_MAX_VL_0_14;
1305 break;
1306
1307 default:
1308 return -EINVAL;
1309 }
1310
1311 return 0;
1312}
1313
1314static int mlx5_query_hca_port(struct ib_device *ibdev, u8 port,
1315 struct ib_port_attr *props)
1316{
1317 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1318 struct mlx5_core_dev *mdev = dev->mdev;
1319 struct mlx5_hca_vport_context *rep;
Saeed Mahameed046339e2016-04-22 00:33:03 +03001320 u16 max_mtu;
1321 u16 oper_mtu;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001322 int err;
1323 u8 ib_link_width_oper;
1324 u8 vl_hw_cap;
1325
1326 rep = kzalloc(sizeof(*rep), GFP_KERNEL);
1327 if (!rep) {
1328 err = -ENOMEM;
1329 goto out;
1330 }
1331
Or Gerlitzc4550c62017-01-24 13:02:39 +02001332 /* props being zeroed by the caller, avoid zeroing it here */
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001333
1334 err = mlx5_query_hca_vport_context(mdev, 0, port, 0, rep);
1335 if (err)
1336 goto out;
1337
1338 props->lid = rep->lid;
1339 props->lmc = rep->lmc;
1340 props->sm_lid = rep->sm_lid;
1341 props->sm_sl = rep->sm_sl;
1342 props->state = rep->vport_state;
1343 props->phys_state = rep->port_physical_state;
1344 props->port_cap_flags = rep->cap_mask1;
1345 props->gid_tbl_len = mlx5_get_gid_table_len(MLX5_CAP_GEN(mdev, gid_table_size));
1346 props->max_msg_sz = 1 << MLX5_CAP_GEN(mdev, log_max_msg);
1347 props->pkey_tbl_len = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, pkey_table_size));
1348 props->bad_pkey_cntr = rep->pkey_violation_counter;
1349 props->qkey_viol_cntr = rep->qkey_violation_counter;
1350 props->subnet_timeout = rep->subnet_timeout;
1351 props->init_type_reply = rep->init_type_reply;
1352
Michael Guralnik4106a752018-12-09 11:49:51 +02001353 if (props->port_cap_flags & IB_PORT_CAP_MASK2_SUP)
1354 props->port_cap_flags2 = rep->cap_mask2;
1355
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001356 err = mlx5_query_port_link_width_oper(mdev, &ib_link_width_oper, port);
1357 if (err)
1358 goto out;
1359
Michael Guralnikdb7a6912018-11-21 15:03:54 +02001360 translate_active_width(ibdev, ib_link_width_oper, &props->active_width);
1361
Noa Osherovichd5beb7f2016-06-02 10:47:53 +03001362 err = mlx5_query_port_ib_proto_oper(mdev, &props->active_speed, port);
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001363 if (err)
1364 goto out;
1365
Saeed Mahameedfacc9692015-06-11 14:47:27 +03001366 mlx5_query_port_max_mtu(mdev, &max_mtu, port);
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001367
1368 props->max_mtu = mlx5_mtu_to_ib_mtu(max_mtu);
1369
Saeed Mahameedfacc9692015-06-11 14:47:27 +03001370 mlx5_query_port_oper_mtu(mdev, &oper_mtu, port);
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001371
1372 props->active_mtu = mlx5_mtu_to_ib_mtu(oper_mtu);
1373
1374 err = mlx5_query_port_vl_hw_cap(mdev, &vl_hw_cap, port);
1375 if (err)
1376 goto out;
1377
1378 err = translate_max_vl_num(ibdev, vl_hw_cap,
1379 &props->max_vl_num);
1380out:
1381 kfree(rep);
Eli Cohene126ba92013-07-07 17:25:49 +03001382 return err;
1383}
1384
1385int mlx5_ib_query_port(struct ib_device *ibdev, u8 port,
1386 struct ib_port_attr *props)
1387{
Ilan Tayari095b0922017-05-14 16:04:30 +03001388 unsigned int count;
1389 int ret;
1390
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001391 switch (mlx5_get_vport_access_method(ibdev)) {
1392 case MLX5_VPORT_ACCESS_METHOD_MAD:
Ilan Tayari095b0922017-05-14 16:04:30 +03001393 ret = mlx5_query_mad_ifc_port(ibdev, port, props);
1394 break;
Eli Cohene126ba92013-07-07 17:25:49 +03001395
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001396 case MLX5_VPORT_ACCESS_METHOD_HCA:
Ilan Tayari095b0922017-05-14 16:04:30 +03001397 ret = mlx5_query_hca_port(ibdev, port, props);
1398 break;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001399
Achiad Shochat3f89a642015-12-23 18:47:21 +02001400 case MLX5_VPORT_ACCESS_METHOD_NIC:
Ilan Tayari095b0922017-05-14 16:04:30 +03001401 ret = mlx5_query_port_roce(ibdev, port, props);
1402 break;
Achiad Shochat3f89a642015-12-23 18:47:21 +02001403
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001404 default:
Ilan Tayari095b0922017-05-14 16:04:30 +03001405 ret = -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +03001406 }
Ilan Tayari095b0922017-05-14 16:04:30 +03001407
1408 if (!ret && props) {
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001409 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1410 struct mlx5_core_dev *mdev;
1411 bool put_mdev = true;
1412
1413 mdev = mlx5_ib_get_native_port_mdev(dev, port, NULL);
1414 if (!mdev) {
1415 /* If the port isn't affiliated yet query the master.
1416 * The master and slave will have the same values.
1417 */
1418 mdev = dev->mdev;
1419 port = 1;
1420 put_mdev = false;
1421 }
1422 count = mlx5_core_reserved_gids_count(mdev);
1423 if (put_mdev)
1424 mlx5_ib_put_native_port_mdev(dev, port);
Ilan Tayari095b0922017-05-14 16:04:30 +03001425 props->gid_tbl_len -= count;
1426 }
1427 return ret;
Eli Cohene126ba92013-07-07 17:25:49 +03001428}
1429
Mark Bloch8e6efa32017-11-06 12:22:13 +00001430static int mlx5_ib_rep_query_port(struct ib_device *ibdev, u8 port,
1431 struct ib_port_attr *props)
1432{
1433 int ret;
1434
1435 /* Only link layer == ethernet is valid for representors */
1436 ret = mlx5_query_port_roce(ibdev, port, props);
1437 if (ret || !props)
1438 return ret;
1439
1440 /* We don't support GIDS */
1441 props->gid_tbl_len = 0;
1442
1443 return ret;
1444}
1445
Eli Cohene126ba92013-07-07 17:25:49 +03001446static int mlx5_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
1447 union ib_gid *gid)
1448{
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001449 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1450 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03001451
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001452 switch (mlx5_get_vport_access_method(ibdev)) {
1453 case MLX5_VPORT_ACCESS_METHOD_MAD:
1454 return mlx5_query_mad_ifc_gids(ibdev, port, index, gid);
Eli Cohene126ba92013-07-07 17:25:49 +03001455
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001456 case MLX5_VPORT_ACCESS_METHOD_HCA:
1457 return mlx5_query_hca_vport_gid(mdev, 0, port, 0, index, gid);
Eli Cohene126ba92013-07-07 17:25:49 +03001458
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001459 default:
1460 return -EINVAL;
1461 }
Eli Cohene126ba92013-07-07 17:25:49 +03001462
Eli Cohene126ba92013-07-07 17:25:49 +03001463}
1464
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001465static int mlx5_query_hca_nic_pkey(struct ib_device *ibdev, u8 port,
1466 u16 index, u16 *pkey)
1467{
1468 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1469 struct mlx5_core_dev *mdev;
1470 bool put_mdev = true;
1471 u8 mdev_port_num;
1472 int err;
1473
1474 mdev = mlx5_ib_get_native_port_mdev(dev, port, &mdev_port_num);
1475 if (!mdev) {
1476 /* The port isn't affiliated yet, get the PKey from the master
1477 * port. For RoCE the PKey tables will be the same.
1478 */
1479 put_mdev = false;
1480 mdev = dev->mdev;
1481 mdev_port_num = 1;
1482 }
1483
1484 err = mlx5_query_hca_vport_pkey(mdev, 0, mdev_port_num, 0,
1485 index, pkey);
1486 if (put_mdev)
1487 mlx5_ib_put_native_port_mdev(dev, port);
1488
1489 return err;
1490}
1491
Eli Cohene126ba92013-07-07 17:25:49 +03001492static int mlx5_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
1493 u16 *pkey)
1494{
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001495 switch (mlx5_get_vport_access_method(ibdev)) {
1496 case MLX5_VPORT_ACCESS_METHOD_MAD:
1497 return mlx5_query_mad_ifc_pkey(ibdev, port, index, pkey);
Eli Cohene126ba92013-07-07 17:25:49 +03001498
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001499 case MLX5_VPORT_ACCESS_METHOD_HCA:
1500 case MLX5_VPORT_ACCESS_METHOD_NIC:
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001501 return mlx5_query_hca_nic_pkey(ibdev, port, index, pkey);
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001502 default:
1503 return -EINVAL;
1504 }
Eli Cohene126ba92013-07-07 17:25:49 +03001505}
1506
Eli Cohene126ba92013-07-07 17:25:49 +03001507static int mlx5_ib_modify_device(struct ib_device *ibdev, int mask,
1508 struct ib_device_modify *props)
1509{
1510 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1511 struct mlx5_reg_node_desc in;
1512 struct mlx5_reg_node_desc out;
1513 int err;
1514
1515 if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
1516 return -EOPNOTSUPP;
1517
1518 if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
1519 return 0;
1520
1521 /*
1522 * If possible, pass node desc to FW, so it can generate
1523 * a 144 trap. If cmd fails, just ignore.
1524 */
Yuval Shaiabd99fde2016-08-25 10:57:07 -07001525 memcpy(&in, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
Jack Morgenstein9603b612014-07-28 23:30:22 +03001526 err = mlx5_core_access_reg(dev->mdev, &in, sizeof(in), &out,
Eli Cohene126ba92013-07-07 17:25:49 +03001527 sizeof(out), MLX5_REG_NODE_DESC, 0, 1);
1528 if (err)
1529 return err;
1530
Yuval Shaiabd99fde2016-08-25 10:57:07 -07001531 memcpy(ibdev->node_desc, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
Eli Cohene126ba92013-07-07 17:25:49 +03001532
1533 return err;
1534}
1535
Eli Cohencdbe33d2017-02-14 07:25:38 +02001536static int set_port_caps_atomic(struct mlx5_ib_dev *dev, u8 port_num, u32 mask,
1537 u32 value)
1538{
1539 struct mlx5_hca_vport_context ctx = {};
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001540 struct mlx5_core_dev *mdev;
1541 u8 mdev_port_num;
Eli Cohencdbe33d2017-02-14 07:25:38 +02001542 int err;
1543
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001544 mdev = mlx5_ib_get_native_port_mdev(dev, port_num, &mdev_port_num);
1545 if (!mdev)
1546 return -ENODEV;
1547
1548 err = mlx5_query_hca_vport_context(mdev, 0, mdev_port_num, 0, &ctx);
Eli Cohencdbe33d2017-02-14 07:25:38 +02001549 if (err)
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001550 goto out;
Eli Cohencdbe33d2017-02-14 07:25:38 +02001551
1552 if (~ctx.cap_mask1_perm & mask) {
1553 mlx5_ib_warn(dev, "trying to change bitmask 0x%X but change supported 0x%X\n",
1554 mask, ctx.cap_mask1_perm);
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001555 err = -EINVAL;
1556 goto out;
Eli Cohencdbe33d2017-02-14 07:25:38 +02001557 }
1558
1559 ctx.cap_mask1 = value;
1560 ctx.cap_mask1_perm = mask;
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001561 err = mlx5_core_modify_hca_vport_context(mdev, 0, mdev_port_num,
1562 0, &ctx);
1563
1564out:
1565 mlx5_ib_put_native_port_mdev(dev, port_num);
Eli Cohencdbe33d2017-02-14 07:25:38 +02001566
1567 return err;
1568}
1569
Eli Cohene126ba92013-07-07 17:25:49 +03001570static int mlx5_ib_modify_port(struct ib_device *ibdev, u8 port, int mask,
1571 struct ib_port_modify *props)
1572{
1573 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1574 struct ib_port_attr attr;
1575 u32 tmp;
1576 int err;
Eli Cohencdbe33d2017-02-14 07:25:38 +02001577 u32 change_mask;
1578 u32 value;
1579 bool is_ib = (mlx5_ib_port_link_layer(ibdev, port) ==
1580 IB_LINK_LAYER_INFINIBAND);
1581
Majd Dibbinyec255872017-08-23 08:35:42 +03001582 /* CM layer calls ib_modify_port() regardless of the link layer. For
1583 * Ethernet ports, qkey violation and Port capabilities are meaningless.
1584 */
1585 if (!is_ib)
1586 return 0;
1587
Eli Cohencdbe33d2017-02-14 07:25:38 +02001588 if (MLX5_CAP_GEN(dev->mdev, ib_virt) && is_ib) {
1589 change_mask = props->clr_port_cap_mask | props->set_port_cap_mask;
1590 value = ~props->clr_port_cap_mask | props->set_port_cap_mask;
1591 return set_port_caps_atomic(dev, port, change_mask, value);
1592 }
Eli Cohene126ba92013-07-07 17:25:49 +03001593
1594 mutex_lock(&dev->cap_mask_mutex);
1595
Or Gerlitzc4550c62017-01-24 13:02:39 +02001596 err = ib_query_port(ibdev, port, &attr);
Eli Cohene126ba92013-07-07 17:25:49 +03001597 if (err)
1598 goto out;
1599
1600 tmp = (attr.port_cap_flags | props->set_port_cap_mask) &
1601 ~props->clr_port_cap_mask;
1602
Jack Morgenstein9603b612014-07-28 23:30:22 +03001603 err = mlx5_set_port_caps(dev->mdev, port, tmp);
Eli Cohene126ba92013-07-07 17:25:49 +03001604
1605out:
1606 mutex_unlock(&dev->cap_mask_mutex);
1607 return err;
1608}
1609
Eli Cohen30aa60b2017-01-03 23:55:27 +02001610static void print_lib_caps(struct mlx5_ib_dev *dev, u64 caps)
1611{
1612 mlx5_ib_dbg(dev, "MLX5_LIB_CAP_4K_UAR = %s\n",
1613 caps & MLX5_LIB_CAP_4K_UAR ? "y" : "n");
1614}
1615
Yishai Hadas31a78a52017-12-24 16:31:34 +02001616static u16 calc_dynamic_bfregs(int uars_per_sys_page)
1617{
1618 /* Large page with non 4k uar support might limit the dynamic size */
1619 if (uars_per_sys_page == 1 && PAGE_SIZE > 4096)
1620 return MLX5_MIN_DYN_BFREGS;
1621
1622 return MLX5_MAX_DYN_BFREGS;
1623}
1624
Eli Cohenb037c292017-01-03 23:55:26 +02001625static int calc_total_bfregs(struct mlx5_ib_dev *dev, bool lib_uar_4k,
1626 struct mlx5_ib_alloc_ucontext_req_v2 *req,
Yishai Hadas31a78a52017-12-24 16:31:34 +02001627 struct mlx5_bfreg_info *bfregi)
Eli Cohenb037c292017-01-03 23:55:26 +02001628{
1629 int uars_per_sys_page;
1630 int bfregs_per_sys_page;
1631 int ref_bfregs = req->total_num_bfregs;
1632
1633 if (req->total_num_bfregs == 0)
1634 return -EINVAL;
1635
1636 BUILD_BUG_ON(MLX5_MAX_BFREGS % MLX5_NON_FP_BFREGS_IN_PAGE);
1637 BUILD_BUG_ON(MLX5_MAX_BFREGS < MLX5_NON_FP_BFREGS_IN_PAGE);
1638
1639 if (req->total_num_bfregs > MLX5_MAX_BFREGS)
1640 return -ENOMEM;
1641
1642 uars_per_sys_page = get_uars_per_sys_page(dev, lib_uar_4k);
1643 bfregs_per_sys_page = uars_per_sys_page * MLX5_NON_FP_BFREGS_PER_UAR;
Yishai Hadas31a78a52017-12-24 16:31:34 +02001644 /* This holds the required static allocation asked by the user */
Eli Cohenb037c292017-01-03 23:55:26 +02001645 req->total_num_bfregs = ALIGN(req->total_num_bfregs, bfregs_per_sys_page);
Eli Cohenb037c292017-01-03 23:55:26 +02001646 if (req->num_low_latency_bfregs > req->total_num_bfregs - 1)
1647 return -EINVAL;
1648
Yishai Hadas31a78a52017-12-24 16:31:34 +02001649 bfregi->num_static_sys_pages = req->total_num_bfregs / bfregs_per_sys_page;
1650 bfregi->num_dyn_bfregs = ALIGN(calc_dynamic_bfregs(uars_per_sys_page), bfregs_per_sys_page);
1651 bfregi->total_num_bfregs = req->total_num_bfregs + bfregi->num_dyn_bfregs;
1652 bfregi->num_sys_pages = bfregi->total_num_bfregs / bfregs_per_sys_page;
1653
1654 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 +02001655 MLX5_CAP_GEN(dev->mdev, uar_4k) ? "yes" : "no",
1656 lib_uar_4k ? "yes" : "no", ref_bfregs,
Yishai Hadas31a78a52017-12-24 16:31:34 +02001657 req->total_num_bfregs, bfregi->total_num_bfregs,
1658 bfregi->num_sys_pages);
Eli Cohenb037c292017-01-03 23:55:26 +02001659
1660 return 0;
1661}
1662
1663static int allocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
1664{
1665 struct mlx5_bfreg_info *bfregi;
1666 int err;
1667 int i;
1668
1669 bfregi = &context->bfregi;
Yishai Hadas31a78a52017-12-24 16:31:34 +02001670 for (i = 0; i < bfregi->num_static_sys_pages; i++) {
Eli Cohenb037c292017-01-03 23:55:26 +02001671 err = mlx5_cmd_alloc_uar(dev->mdev, &bfregi->sys_pages[i]);
1672 if (err)
1673 goto error;
1674
1675 mlx5_ib_dbg(dev, "allocated uar %d\n", bfregi->sys_pages[i]);
1676 }
Yishai Hadas4ed131d2017-12-24 16:31:35 +02001677
1678 for (i = bfregi->num_static_sys_pages; i < bfregi->num_sys_pages; i++)
1679 bfregi->sys_pages[i] = MLX5_IB_INVALID_UAR_INDEX;
1680
Eli Cohenb037c292017-01-03 23:55:26 +02001681 return 0;
1682
1683error:
1684 for (--i; i >= 0; i--)
1685 if (mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]))
1686 mlx5_ib_warn(dev, "failed to free uar %d\n", i);
1687
1688 return err;
1689}
1690
Leon Romanovsky15177992018-06-27 10:44:24 +03001691static void deallocate_uars(struct mlx5_ib_dev *dev,
1692 struct mlx5_ib_ucontext *context)
Eli Cohenb037c292017-01-03 23:55:26 +02001693{
1694 struct mlx5_bfreg_info *bfregi;
Eli Cohenb037c292017-01-03 23:55:26 +02001695 int i;
1696
1697 bfregi = &context->bfregi;
Leon Romanovsky15177992018-06-27 10:44:24 +03001698 for (i = 0; i < bfregi->num_sys_pages; i++)
Yishai Hadas4ed131d2017-12-24 16:31:35 +02001699 if (i < bfregi->num_static_sys_pages ||
Leon Romanovsky15177992018-06-27 10:44:24 +03001700 bfregi->sys_pages[i] != MLX5_IB_INVALID_UAR_INDEX)
1701 mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]);
Eli Cohenb037c292017-01-03 23:55:26 +02001702}
1703
Mark Bloch0042f9e2018-09-17 13:30:49 +03001704int mlx5_ib_enable_lb(struct mlx5_ib_dev *dev, bool td, bool qp)
Mark Blocha560f1d2018-09-17 13:30:47 +03001705{
1706 int err = 0;
1707
1708 mutex_lock(&dev->lb.mutex);
Mark Bloch0042f9e2018-09-17 13:30:49 +03001709 if (td)
1710 dev->lb.user_td++;
1711 if (qp)
1712 dev->lb.qps++;
Mark Blocha560f1d2018-09-17 13:30:47 +03001713
Mark Bloch0042f9e2018-09-17 13:30:49 +03001714 if (dev->lb.user_td == 2 ||
1715 dev->lb.qps == 1) {
1716 if (!dev->lb.enabled) {
1717 err = mlx5_nic_vport_update_local_lb(dev->mdev, true);
1718 dev->lb.enabled = true;
1719 }
1720 }
Mark Blocha560f1d2018-09-17 13:30:47 +03001721
1722 mutex_unlock(&dev->lb.mutex);
1723
1724 return err;
1725}
1726
Mark Bloch0042f9e2018-09-17 13:30:49 +03001727void mlx5_ib_disable_lb(struct mlx5_ib_dev *dev, bool td, bool qp)
Mark Blocha560f1d2018-09-17 13:30:47 +03001728{
1729 mutex_lock(&dev->lb.mutex);
Mark Bloch0042f9e2018-09-17 13:30:49 +03001730 if (td)
1731 dev->lb.user_td--;
1732 if (qp)
1733 dev->lb.qps--;
Mark Blocha560f1d2018-09-17 13:30:47 +03001734
Mark Bloch0042f9e2018-09-17 13:30:49 +03001735 if (dev->lb.user_td == 1 &&
1736 dev->lb.qps == 0) {
1737 if (dev->lb.enabled) {
1738 mlx5_nic_vport_update_local_lb(dev->mdev, false);
1739 dev->lb.enabled = false;
1740 }
1741 }
Mark Blocha560f1d2018-09-17 13:30:47 +03001742
1743 mutex_unlock(&dev->lb.mutex);
1744}
1745
Yishai Hadasd2d19122018-09-20 21:39:32 +03001746static int mlx5_ib_alloc_transport_domain(struct mlx5_ib_dev *dev, u32 *tdn,
1747 u16 uid)
Huy Nguyenc85023e2017-05-30 09:42:54 +03001748{
1749 int err;
1750
Leon Romanovskycfdeb892018-06-19 10:39:06 +03001751 if (!MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1752 return 0;
1753
Yishai Hadasd2d19122018-09-20 21:39:32 +03001754 err = mlx5_cmd_alloc_transport_domain(dev->mdev, tdn, uid);
Huy Nguyenc85023e2017-05-30 09:42:54 +03001755 if (err)
1756 return err;
1757
1758 if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
Eran Ben Elisha8978cc92018-01-09 11:41:10 +02001759 (!MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) &&
1760 !MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
Huy Nguyenc85023e2017-05-30 09:42:54 +03001761 return err;
1762
Mark Bloch0042f9e2018-09-17 13:30:49 +03001763 return mlx5_ib_enable_lb(dev, true, false);
Huy Nguyenc85023e2017-05-30 09:42:54 +03001764}
1765
Yishai Hadasd2d19122018-09-20 21:39:32 +03001766static void mlx5_ib_dealloc_transport_domain(struct mlx5_ib_dev *dev, u32 tdn,
1767 u16 uid)
Huy Nguyenc85023e2017-05-30 09:42:54 +03001768{
Leon Romanovskycfdeb892018-06-19 10:39:06 +03001769 if (!MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1770 return;
1771
Yishai Hadasd2d19122018-09-20 21:39:32 +03001772 mlx5_cmd_dealloc_transport_domain(dev->mdev, tdn, uid);
Huy Nguyenc85023e2017-05-30 09:42:54 +03001773
1774 if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
Eran Ben Elisha8978cc92018-01-09 11:41:10 +02001775 (!MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) &&
1776 !MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
Huy Nguyenc85023e2017-05-30 09:42:54 +03001777 return;
1778
Mark Bloch0042f9e2018-09-17 13:30:49 +03001779 mlx5_ib_disable_lb(dev, true, false);
Huy Nguyenc85023e2017-05-30 09:42:54 +03001780}
1781
Leon Romanovskya2a074e2019-02-12 20:39:16 +02001782static int mlx5_ib_alloc_ucontext(struct ib_ucontext *uctx,
1783 struct ib_udata *udata)
Eli Cohene126ba92013-07-07 17:25:49 +03001784{
Leon Romanovskya2a074e2019-02-12 20:39:16 +02001785 struct ib_device *ibdev = uctx->device;
Eli Cohene126ba92013-07-07 17:25:49 +03001786 struct mlx5_ib_dev *dev = to_mdev(ibdev);
Matan Barakb368d7c2015-12-15 20:30:12 +02001787 struct mlx5_ib_alloc_ucontext_req_v2 req = {};
1788 struct mlx5_ib_alloc_ucontext_resp resp = {};
Feras Daoud5c99eae2018-01-16 20:08:41 +02001789 struct mlx5_core_dev *mdev = dev->mdev;
Leon Romanovskya2a074e2019-02-12 20:39:16 +02001790 struct mlx5_ib_ucontext *context = to_mucontext(uctx);
Eli Cohen2f5ff262017-01-03 23:55:21 +02001791 struct mlx5_bfreg_info *bfregi;
Eli Cohen78c0f982014-01-30 13:49:48 +02001792 int ver;
Eli Cohene126ba92013-07-07 17:25:49 +03001793 int err;
Majd Dibbinya168a41c2016-01-28 17:51:47 +02001794 size_t min_req_v2 = offsetof(struct mlx5_ib_alloc_ucontext_req_v2,
1795 max_cqe_version);
Yonatan Cohen25bb36e2018-06-19 08:47:24 +03001796 u32 dump_fill_mkey;
Eli Cohenb037c292017-01-03 23:55:26 +02001797 bool lib_uar_4k;
Eli Cohene126ba92013-07-07 17:25:49 +03001798
1799 if (!dev->ib_active)
Leon Romanovskya2a074e2019-02-12 20:39:16 +02001800 return -EAGAIN;
Eli Cohene126ba92013-07-07 17:25:49 +03001801
Amrani, Rame0931112017-06-27 17:04:42 +03001802 if (udata->inlen == sizeof(struct mlx5_ib_alloc_ucontext_req))
Eli Cohen78c0f982014-01-30 13:49:48 +02001803 ver = 0;
Amrani, Rame0931112017-06-27 17:04:42 +03001804 else if (udata->inlen >= min_req_v2)
Eli Cohen78c0f982014-01-30 13:49:48 +02001805 ver = 2;
1806 else
Leon Romanovskya2a074e2019-02-12 20:39:16 +02001807 return -EINVAL;
Eli Cohen78c0f982014-01-30 13:49:48 +02001808
Amrani, Rame0931112017-06-27 17:04:42 +03001809 err = ib_copy_from_udata(&req, udata, min(udata->inlen, sizeof(req)));
Eli Cohene126ba92013-07-07 17:25:49 +03001810 if (err)
Leon Romanovskya2a074e2019-02-12 20:39:16 +02001811 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03001812
Yishai Hadasa8b92ca2018-06-17 12:59:57 +03001813 if (req.flags & ~MLX5_IB_ALLOC_UCTX_DEVX)
Leon Romanovskya2a074e2019-02-12 20:39:16 +02001814 return -EOPNOTSUPP;
Eli Cohen78c0f982014-01-30 13:49:48 +02001815
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001816 if (req.comp_mask || req.reserved0 || req.reserved1 || req.reserved2)
Leon Romanovskya2a074e2019-02-12 20:39:16 +02001817 return -EOPNOTSUPP;
Matan Barakb368d7c2015-12-15 20:30:12 +02001818
Eli Cohen2f5ff262017-01-03 23:55:21 +02001819 req.total_num_bfregs = ALIGN(req.total_num_bfregs,
1820 MLX5_NON_FP_BFREGS_PER_UAR);
1821 if (req.num_low_latency_bfregs > req.total_num_bfregs - 1)
Leon Romanovskya2a074e2019-02-12 20:39:16 +02001822 return -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +03001823
Saeed Mahameed938fe832015-05-28 22:28:41 +03001824 resp.qp_tab_size = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp);
Noa Osherovich2cc6ad52016-06-04 15:15:33 +03001825 if (mlx5_core_is_pf(dev->mdev) && MLX5_CAP_GEN(dev->mdev, bf))
1826 resp.bf_reg_size = 1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size);
Daniel Jurgensb47bd6e2016-10-25 18:36:24 +03001827 resp.cache_line_size = cache_line_size();
Saeed Mahameed938fe832015-05-28 22:28:41 +03001828 resp.max_sq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq);
1829 resp.max_rq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq);
1830 resp.max_send_wqebb = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1831 resp.max_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1832 resp.max_srq_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz);
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001833 resp.cqe_version = min_t(__u8,
1834 (__u8)MLX5_CAP_GEN(dev->mdev, cqe_version),
1835 req.max_cqe_version);
Eli Cohen30aa60b2017-01-03 23:55:27 +02001836 resp.log_uar_size = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
1837 MLX5_ADAPTER_PAGE_SHIFT : PAGE_SHIFT;
1838 resp.num_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
1839 MLX5_CAP_GEN(dev->mdev, num_of_uars_per_page) : 1;
Matan Barakb368d7c2015-12-15 20:30:12 +02001840 resp.response_length = min(offsetof(typeof(resp), response_length) +
1841 sizeof(resp.response_length), udata->outlen);
Eli Cohene126ba92013-07-07 17:25:49 +03001842
Matan Barakc03faa52018-03-28 09:27:54 +03001843 if (mlx5_accel_ipsec_device_caps(dev->mdev) & MLX5_ACCEL_IPSEC_CAP_DEVICE) {
1844 if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_EGRESS))
1845 resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM;
1846 if (mlx5_accel_ipsec_device_caps(dev->mdev) & MLX5_ACCEL_IPSEC_CAP_REQUIRED_METADATA)
1847 resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_REQ_METADATA;
1848 if (MLX5_CAP_FLOWTABLE(dev->mdev, flow_table_properties_nic_receive.ft_field_support.outer_esp_spi))
1849 resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_SPI_STEERING;
1850 if (mlx5_accel_ipsec_device_caps(dev->mdev) & MLX5_ACCEL_IPSEC_CAP_TX_IV_IS_ESN)
1851 resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_TX_IV_IS_ESN;
1852 /* MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_FULL_OFFLOAD is currently always 0 */
1853 }
1854
Eli Cohen30aa60b2017-01-03 23:55:27 +02001855 lib_uar_4k = req.lib_caps & MLX5_LIB_CAP_4K_UAR;
Eli Cohen2f5ff262017-01-03 23:55:21 +02001856 bfregi = &context->bfregi;
Eli Cohenb037c292017-01-03 23:55:26 +02001857
1858 /* updates req->total_num_bfregs */
Yishai Hadas31a78a52017-12-24 16:31:34 +02001859 err = calc_total_bfregs(dev, lib_uar_4k, &req, bfregi);
Eli Cohenb037c292017-01-03 23:55:26 +02001860 if (err)
1861 goto out_ctx;
1862
Eli Cohen2f5ff262017-01-03 23:55:21 +02001863 mutex_init(&bfregi->lock);
Eli Cohenb037c292017-01-03 23:55:26 +02001864 bfregi->lib_uar_4k = lib_uar_4k;
Yishai Hadas31a78a52017-12-24 16:31:34 +02001865 bfregi->count = kcalloc(bfregi->total_num_bfregs, sizeof(*bfregi->count),
Eli Cohenb037c292017-01-03 23:55:26 +02001866 GFP_KERNEL);
1867 if (!bfregi->count) {
Eli Cohene126ba92013-07-07 17:25:49 +03001868 err = -ENOMEM;
1869 goto out_ctx;
1870 }
1871
Eli Cohenb037c292017-01-03 23:55:26 +02001872 bfregi->sys_pages = kcalloc(bfregi->num_sys_pages,
1873 sizeof(*bfregi->sys_pages),
1874 GFP_KERNEL);
1875 if (!bfregi->sys_pages) {
Eli Cohene126ba92013-07-07 17:25:49 +03001876 err = -ENOMEM;
Eli Cohenb037c292017-01-03 23:55:26 +02001877 goto out_count;
Eli Cohene126ba92013-07-07 17:25:49 +03001878 }
1879
Eli Cohenb037c292017-01-03 23:55:26 +02001880 err = allocate_uars(dev, context);
1881 if (err)
1882 goto out_sys_pages;
Eli Cohene126ba92013-07-07 17:25:49 +03001883
Leon Romanovsky13859d5d2019-01-08 16:07:26 +02001884 if (ibdev->attrs.device_cap_flags & IB_DEVICE_ON_DEMAND_PAGING)
1885 context->ibucontext.invalidate_range =
1886 &mlx5_ib_invalidate_range;
Haggai Eranb4cfe442014-12-11 17:04:26 +02001887
Yishai Hadasa8b92ca2018-06-17 12:59:57 +03001888 if (req.flags & MLX5_IB_ALLOC_UCTX_DEVX) {
Yishai Hadasfb981532018-11-26 08:28:36 +02001889 err = mlx5_ib_devx_create(dev, true);
Yishai Hadas76dc5a82018-09-20 21:45:19 +03001890 if (err < 0)
Yishai Hadasd2d19122018-09-20 21:39:32 +03001891 goto out_uars;
Yishai Hadas76dc5a82018-09-20 21:45:19 +03001892 context->devx_uid = err;
Yishai Hadasa8b92ca2018-06-17 12:59:57 +03001893 }
1894
Yishai Hadasd2d19122018-09-20 21:39:32 +03001895 err = mlx5_ib_alloc_transport_domain(dev, &context->tdn,
1896 context->devx_uid);
1897 if (err)
1898 goto out_devx;
1899
Yonatan Cohen25bb36e2018-06-19 08:47:24 +03001900 if (MLX5_CAP_GEN(dev->mdev, dump_fill_mkey)) {
1901 err = mlx5_cmd_dump_fill_mkey(dev->mdev, &dump_fill_mkey);
1902 if (err)
Jason Gunthorpe8193abb2018-07-04 13:19:46 -06001903 goto out_mdev;
Yonatan Cohen25bb36e2018-06-19 08:47:24 +03001904 }
1905
Eli Cohene126ba92013-07-07 17:25:49 +03001906 INIT_LIST_HEAD(&context->db_page_list);
1907 mutex_init(&context->db_page_mutex);
1908
Eli Cohen2f5ff262017-01-03 23:55:21 +02001909 resp.tot_bfregs = req.total_num_bfregs;
Daniel Jurgens508562d2018-01-04 17:25:34 +02001910 resp.num_ports = dev->num_ports;
Matan Barakb368d7c2015-12-15 20:30:12 +02001911
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001912 if (field_avail(typeof(resp), cqe_version, udata->outlen))
1913 resp.response_length += sizeof(resp.cqe_version);
Matan Barakb368d7c2015-12-15 20:30:12 +02001914
Bodong Wang402ca532016-06-17 15:02:20 +03001915 if (field_avail(typeof(resp), cmds_supp_uhw, udata->outlen)) {
Moni Shoua6ad279c52016-11-23 08:23:23 +02001916 resp.cmds_supp_uhw |= MLX5_USER_CMDS_SUPP_UHW_QUERY_DEVICE |
1917 MLX5_USER_CMDS_SUPP_UHW_CREATE_AH;
Bodong Wang402ca532016-06-17 15:02:20 +03001918 resp.response_length += sizeof(resp.cmds_supp_uhw);
1919 }
1920
Or Gerlitz78984892016-11-30 20:33:33 +02001921 if (field_avail(typeof(resp), eth_min_inline, udata->outlen)) {
1922 if (mlx5_ib_port_link_layer(ibdev, 1) == IB_LINK_LAYER_ETHERNET) {
1923 mlx5_query_min_inline(dev->mdev, &resp.eth_min_inline);
1924 resp.eth_min_inline++;
1925 }
1926 resp.response_length += sizeof(resp.eth_min_inline);
1927 }
1928
Feras Daoud5c99eae2018-01-16 20:08:41 +02001929 if (field_avail(typeof(resp), clock_info_versions, udata->outlen)) {
1930 if (mdev->clock_info)
1931 resp.clock_info_versions = BIT(MLX5_IB_CLOCK_INFO_V1);
1932 resp.response_length += sizeof(resp.clock_info_versions);
1933 }
1934
Noa Osherovichbc5c6ee2016-06-04 15:15:31 +03001935 /*
1936 * We don't want to expose information from the PCI bar that is located
1937 * after 4096 bytes, so if the arch only supports larger pages, let's
1938 * pretend we don't support reading the HCA's core clock. This is also
1939 * forced by mmap function.
1940 */
Eli Cohende8d6e02017-01-03 23:55:19 +02001941 if (field_avail(typeof(resp), hca_core_clock_offset, udata->outlen)) {
1942 if (PAGE_SIZE <= 4096) {
1943 resp.comp_mask |=
1944 MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_CORE_CLOCK_OFFSET;
1945 resp.hca_core_clock_offset =
1946 offsetof(struct mlx5_init_seg, internal_timer_h) % PAGE_SIZE;
1947 }
Feras Daoud5c99eae2018-01-16 20:08:41 +02001948 resp.response_length += sizeof(resp.hca_core_clock_offset);
Matan Barakb368d7c2015-12-15 20:30:12 +02001949 }
1950
Eli Cohen30aa60b2017-01-03 23:55:27 +02001951 if (field_avail(typeof(resp), log_uar_size, udata->outlen))
1952 resp.response_length += sizeof(resp.log_uar_size);
1953
1954 if (field_avail(typeof(resp), num_uars_per_page, udata->outlen))
1955 resp.response_length += sizeof(resp.num_uars_per_page);
1956
Yishai Hadas31a78a52017-12-24 16:31:34 +02001957 if (field_avail(typeof(resp), num_dyn_bfregs, udata->outlen)) {
1958 resp.num_dyn_bfregs = bfregi->num_dyn_bfregs;
1959 resp.response_length += sizeof(resp.num_dyn_bfregs);
1960 }
1961
Yonatan Cohen25bb36e2018-06-19 08:47:24 +03001962 if (field_avail(typeof(resp), dump_fill_mkey, udata->outlen)) {
1963 if (MLX5_CAP_GEN(dev->mdev, dump_fill_mkey)) {
1964 resp.dump_fill_mkey = dump_fill_mkey;
1965 resp.comp_mask |=
1966 MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_DUMP_FILL_MKEY;
1967 }
1968 resp.response_length += sizeof(resp.dump_fill_mkey);
1969 }
1970
Matan Barakb368d7c2015-12-15 20:30:12 +02001971 err = ib_copy_to_udata(udata, &resp, resp.response_length);
Eli Cohene126ba92013-07-07 17:25:49 +03001972 if (err)
Yishai Hadasa8b92ca2018-06-17 12:59:57 +03001973 goto out_mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03001974
Eli Cohen2f5ff262017-01-03 23:55:21 +02001975 bfregi->ver = ver;
1976 bfregi->num_low_latency_bfregs = req.num_low_latency_bfregs;
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001977 context->cqe_version = resp.cqe_version;
Eli Cohen30aa60b2017-01-03 23:55:27 +02001978 context->lib_caps = req.lib_caps;
1979 print_lib_caps(dev, context->lib_caps);
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001980
Aviv Heller7c34ec12018-08-23 13:47:53 +03001981 if (dev->lag_active) {
Mark Bloch95579e72019-03-28 15:27:33 +02001982 u8 port = mlx5_core_native_port_num(dev->mdev) - 1;
Majd Dibbinyc6a21c32018-08-28 14:29:05 +03001983
1984 atomic_set(&context->tx_port_affinity,
1985 atomic_add_return(
Mark Bloch95579e72019-03-28 15:27:33 +02001986 1, &dev->port[port].roce.tx_port_affinity));
Majd Dibbinyc6a21c32018-08-28 14:29:05 +03001987 }
1988
Leon Romanovskya2a074e2019-02-12 20:39:16 +02001989 return 0;
Eli Cohene126ba92013-07-07 17:25:49 +03001990
Yishai Hadasa8b92ca2018-06-17 12:59:57 +03001991out_mdev:
Yishai Hadasd2d19122018-09-20 21:39:32 +03001992 mlx5_ib_dealloc_transport_domain(dev, context->tdn, context->devx_uid);
1993out_devx:
Yishai Hadasa8b92ca2018-06-17 12:59:57 +03001994 if (req.flags & MLX5_IB_ALLOC_UCTX_DEVX)
Yishai Hadas76dc5a82018-09-20 21:45:19 +03001995 mlx5_ib_devx_destroy(dev, context->devx_uid);
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001996
Eli Cohene126ba92013-07-07 17:25:49 +03001997out_uars:
Eli Cohenb037c292017-01-03 23:55:26 +02001998 deallocate_uars(dev, context);
1999
2000out_sys_pages:
2001 kfree(bfregi->sys_pages);
2002
Eli Cohene126ba92013-07-07 17:25:49 +03002003out_count:
Eli Cohen2f5ff262017-01-03 23:55:21 +02002004 kfree(bfregi->count);
Eli Cohene126ba92013-07-07 17:25:49 +03002005
Eli Cohene126ba92013-07-07 17:25:49 +03002006out_ctx:
Leon Romanovskya2a074e2019-02-12 20:39:16 +02002007 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03002008}
2009
Leon Romanovskya2a074e2019-02-12 20:39:16 +02002010static void mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
Eli Cohene126ba92013-07-07 17:25:49 +03002011{
2012 struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
2013 struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
Eli Cohenb037c292017-01-03 23:55:26 +02002014 struct mlx5_bfreg_info *bfregi;
Eli Cohene126ba92013-07-07 17:25:49 +03002015
Jason Gunthorpef27a0d52018-09-16 20:48:08 +03002016 /* All umem's must be destroyed before destroying the ucontext. */
2017 mutex_lock(&ibcontext->per_mm_list_lock);
2018 WARN_ON(!list_empty(&ibcontext->per_mm_list));
2019 mutex_unlock(&ibcontext->per_mm_list_lock);
Yishai Hadasa8b92ca2018-06-17 12:59:57 +03002020
Eli Cohenb037c292017-01-03 23:55:26 +02002021 bfregi = &context->bfregi;
Yishai Hadasd2d19122018-09-20 21:39:32 +03002022 mlx5_ib_dealloc_transport_domain(dev, context->tdn, context->devx_uid);
2023
Eli Cohenb037c292017-01-03 23:55:26 +02002024 if (context->devx_uid)
Yishai Hadas76dc5a82018-09-20 21:45:19 +03002025 mlx5_ib_devx_destroy(dev, context->devx_uid);
Eli Cohene126ba92013-07-07 17:25:49 +03002026
2027 deallocate_uars(dev, context);
Eli Cohen2f5ff262017-01-03 23:55:21 +02002028 kfree(bfregi->sys_pages);
2029 kfree(bfregi->count);
Eli Cohene126ba92013-07-07 17:25:49 +03002030}
2031
2032static phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev,
2033 int uar_idx)
2034{
Eli Cohenb037c292017-01-03 23:55:26 +02002035 int fw_uars_per_page;
2036
2037 fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ? MLX5_UARS_IN_PAGE : 1;
2038
Huy Nguyenaa8106f2019-03-29 15:38:01 -07002039 return (dev->mdev->bar_addr >> PAGE_SHIFT) + uar_idx / fw_uars_per_page;
Eli Cohene126ba92013-07-07 17:25:49 +03002040}
2041
2042static int get_command(unsigned long offset)
2043{
2044 return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK;
2045}
2046
2047static int get_arg(unsigned long offset)
2048{
2049 return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1);
2050}
2051
2052static int get_index(unsigned long offset)
2053{
2054 return get_arg(offset);
2055}
2056
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002057/* Index resides in an extra byte to enable larger values than 255 */
2058static int get_extended_index(unsigned long offset)
2059{
2060 return get_arg(offset) | ((offset >> 16) & 0xff) << 8;
2061}
2062
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03002063
2064static void mlx5_ib_disassociate_ucontext(struct ib_ucontext *ibcontext)
2065{
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03002066}
2067
Guy Levi37aa5c32016-04-27 16:49:50 +03002068static inline char *mmap_cmd2str(enum mlx5_ib_mmap_cmd cmd)
2069{
2070 switch (cmd) {
2071 case MLX5_IB_MMAP_WC_PAGE:
2072 return "WC";
2073 case MLX5_IB_MMAP_REGULAR_PAGE:
2074 return "best effort WC";
2075 case MLX5_IB_MMAP_NC_PAGE:
2076 return "NC";
Ariel Levkovich24da0012018-04-05 18:53:27 +03002077 case MLX5_IB_MMAP_DEVICE_MEM:
2078 return "Device Memory";
Guy Levi37aa5c32016-04-27 16:49:50 +03002079 default:
2080 return NULL;
2081 }
2082}
2083
Feras Daoud5c99eae2018-01-16 20:08:41 +02002084static int mlx5_ib_mmap_clock_info_page(struct mlx5_ib_dev *dev,
2085 struct vm_area_struct *vma,
2086 struct mlx5_ib_ucontext *context)
2087{
Feras Daoud5c99eae2018-01-16 20:08:41 +02002088 if (vma->vm_end - vma->vm_start != PAGE_SIZE)
2089 return -EINVAL;
2090
2091 if (get_index(vma->vm_pgoff) != MLX5_IB_CLOCK_INFO_V1)
2092 return -EOPNOTSUPP;
2093
2094 if (vma->vm_flags & VM_WRITE)
2095 return -EPERM;
2096
2097 if (!dev->mdev->clock_info_page)
2098 return -EOPNOTSUPP;
2099
Jason Gunthorpee2cd1d12018-09-16 20:43:10 +03002100 return rdma_user_mmap_page(&context->ibucontext, vma,
2101 dev->mdev->clock_info_page, PAGE_SIZE);
Feras Daoud5c99eae2018-01-16 20:08:41 +02002102}
2103
Guy Levi37aa5c32016-04-27 16:49:50 +03002104static int uar_mmap(struct mlx5_ib_dev *dev, enum mlx5_ib_mmap_cmd cmd,
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03002105 struct vm_area_struct *vma,
2106 struct mlx5_ib_ucontext *context)
Guy Levi37aa5c32016-04-27 16:49:50 +03002107{
Eli Cohen2f5ff262017-01-03 23:55:21 +02002108 struct mlx5_bfreg_info *bfregi = &context->bfregi;
Guy Levi37aa5c32016-04-27 16:49:50 +03002109 int err;
2110 unsigned long idx;
Kamal Heibaa09ea62018-07-19 00:05:32 +03002111 phys_addr_t pfn;
Guy Levi37aa5c32016-04-27 16:49:50 +03002112 pgprot_t prot;
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002113 u32 bfreg_dyn_idx = 0;
2114 u32 uar_index;
2115 int dyn_uar = (cmd == MLX5_IB_MMAP_ALLOC_WC);
2116 int max_valid_idx = dyn_uar ? bfregi->num_sys_pages :
2117 bfregi->num_static_sys_pages;
Eli Cohenb037c292017-01-03 23:55:26 +02002118
2119 if (vma->vm_end - vma->vm_start != PAGE_SIZE)
2120 return -EINVAL;
2121
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002122 if (dyn_uar)
2123 idx = get_extended_index(vma->vm_pgoff) + bfregi->num_static_sys_pages;
2124 else
2125 idx = get_index(vma->vm_pgoff);
2126
2127 if (idx >= max_valid_idx) {
2128 mlx5_ib_warn(dev, "invalid uar index %lu, max=%d\n",
2129 idx, max_valid_idx);
Eli Cohenb037c292017-01-03 23:55:26 +02002130 return -EINVAL;
2131 }
Guy Levi37aa5c32016-04-27 16:49:50 +03002132
2133 switch (cmd) {
2134 case MLX5_IB_MMAP_WC_PAGE:
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002135 case MLX5_IB_MMAP_ALLOC_WC:
Guy Levi37aa5c32016-04-27 16:49:50 +03002136/* Some architectures don't support WC memory */
2137#if defined(CONFIG_X86)
2138 if (!pat_enabled())
2139 return -EPERM;
2140#elif !(defined(CONFIG_PPC) || (defined(CONFIG_ARM) && defined(CONFIG_MMU)))
2141 return -EPERM;
2142#endif
2143 /* fall through */
2144 case MLX5_IB_MMAP_REGULAR_PAGE:
2145 /* For MLX5_IB_MMAP_REGULAR_PAGE do the best effort to get WC */
2146 prot = pgprot_writecombine(vma->vm_page_prot);
2147 break;
2148 case MLX5_IB_MMAP_NC_PAGE:
2149 prot = pgprot_noncached(vma->vm_page_prot);
2150 break;
2151 default:
2152 return -EINVAL;
2153 }
2154
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002155 if (dyn_uar) {
2156 int uars_per_page;
2157
2158 uars_per_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k);
2159 bfreg_dyn_idx = idx * (uars_per_page * MLX5_NON_FP_BFREGS_PER_UAR);
2160 if (bfreg_dyn_idx >= bfregi->total_num_bfregs) {
2161 mlx5_ib_warn(dev, "invalid bfreg_dyn_idx %u, max=%u\n",
2162 bfreg_dyn_idx, bfregi->total_num_bfregs);
2163 return -EINVAL;
2164 }
2165
2166 mutex_lock(&bfregi->lock);
2167 /* Fail if uar already allocated, first bfreg index of each
2168 * page holds its count.
2169 */
2170 if (bfregi->count[bfreg_dyn_idx]) {
2171 mlx5_ib_warn(dev, "wrong offset, idx %lu is busy, bfregn=%u\n", idx, bfreg_dyn_idx);
2172 mutex_unlock(&bfregi->lock);
2173 return -EINVAL;
2174 }
2175
2176 bfregi->count[bfreg_dyn_idx]++;
2177 mutex_unlock(&bfregi->lock);
2178
2179 err = mlx5_cmd_alloc_uar(dev->mdev, &uar_index);
2180 if (err) {
2181 mlx5_ib_warn(dev, "UAR alloc failed\n");
2182 goto free_bfreg;
2183 }
2184 } else {
2185 uar_index = bfregi->sys_pages[idx];
2186 }
2187
2188 pfn = uar_index2pfn(dev, uar_index);
Guy Levi37aa5c32016-04-27 16:49:50 +03002189 mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn %pa\n", idx, &pfn);
2190
Jason Gunthorpee2cd1d12018-09-16 20:43:10 +03002191 err = rdma_user_mmap_io(&context->ibucontext, vma, pfn, PAGE_SIZE,
2192 prot);
Guy Levi37aa5c32016-04-27 16:49:50 +03002193 if (err) {
Leon Romanovsky8f062282018-05-22 08:31:03 +03002194 mlx5_ib_err(dev,
Jason Gunthorpee2cd1d12018-09-16 20:43:10 +03002195 "rdma_user_mmap_io failed with error=%d, mmap_cmd=%s\n",
Leon Romanovsky8f062282018-05-22 08:31:03 +03002196 err, mmap_cmd2str(cmd));
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002197 goto err;
Guy Levi37aa5c32016-04-27 16:49:50 +03002198 }
2199
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002200 if (dyn_uar)
2201 bfregi->sys_pages[idx] = uar_index;
2202 return 0;
2203
2204err:
2205 if (!dyn_uar)
2206 return err;
2207
2208 mlx5_cmd_free_uar(dev->mdev, idx);
2209
2210free_bfreg:
2211 mlx5_ib_free_bfreg(dev, bfregi, bfreg_dyn_idx);
2212
2213 return err;
Guy Levi37aa5c32016-04-27 16:49:50 +03002214}
2215
Ariel Levkovich24da0012018-04-05 18:53:27 +03002216static int dm_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
2217{
2218 struct mlx5_ib_ucontext *mctx = to_mucontext(context);
2219 struct mlx5_ib_dev *dev = to_mdev(context->device);
2220 u16 page_idx = get_extended_index(vma->vm_pgoff);
2221 size_t map_size = vma->vm_end - vma->vm_start;
2222 u32 npages = map_size >> PAGE_SHIFT;
2223 phys_addr_t pfn;
Ariel Levkovich24da0012018-04-05 18:53:27 +03002224
2225 if (find_next_zero_bit(mctx->dm_pages, page_idx + npages, page_idx) !=
2226 page_idx + npages)
2227 return -EINVAL;
2228
Huy Nguyenaa8106f2019-03-29 15:38:01 -07002229 pfn = ((dev->mdev->bar_addr +
Ariel Levkovich24da0012018-04-05 18:53:27 +03002230 MLX5_CAP64_DEV_MEM(dev->mdev, memic_bar_start_addr)) >>
2231 PAGE_SHIFT) +
2232 page_idx;
Jason Gunthorpee2cd1d12018-09-16 20:43:10 +03002233 return rdma_user_mmap_io(context, vma, pfn, map_size,
2234 pgprot_writecombine(vma->vm_page_prot));
Ariel Levkovich24da0012018-04-05 18:53:27 +03002235}
2236
Eli Cohene126ba92013-07-07 17:25:49 +03002237static int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma)
2238{
2239 struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
2240 struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
Eli Cohene126ba92013-07-07 17:25:49 +03002241 unsigned long command;
Eli Cohene126ba92013-07-07 17:25:49 +03002242 phys_addr_t pfn;
2243
2244 command = get_command(vma->vm_pgoff);
2245 switch (command) {
Guy Levi37aa5c32016-04-27 16:49:50 +03002246 case MLX5_IB_MMAP_WC_PAGE:
2247 case MLX5_IB_MMAP_NC_PAGE:
Eli Cohene126ba92013-07-07 17:25:49 +03002248 case MLX5_IB_MMAP_REGULAR_PAGE:
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002249 case MLX5_IB_MMAP_ALLOC_WC:
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03002250 return uar_mmap(dev, command, vma, context);
Eli Cohene126ba92013-07-07 17:25:49 +03002251
2252 case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES:
2253 return -ENOSYS;
2254
Matan Barakd69e3bc2015-12-15 20:30:13 +02002255 case MLX5_IB_MMAP_CORE_CLOCK:
Matan Barakd69e3bc2015-12-15 20:30:13 +02002256 if (vma->vm_end - vma->vm_start != PAGE_SIZE)
2257 return -EINVAL;
2258
Matan Barak6cbac1e2016-04-14 16:52:10 +03002259 if (vma->vm_flags & VM_WRITE)
Matan Barakd69e3bc2015-12-15 20:30:13 +02002260 return -EPERM;
2261
2262 /* Don't expose to user-space information it shouldn't have */
2263 if (PAGE_SIZE > 4096)
2264 return -EOPNOTSUPP;
2265
2266 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
2267 pfn = (dev->mdev->iseg_base +
2268 offsetof(struct mlx5_init_seg, internal_timer_h)) >>
2269 PAGE_SHIFT;
2270 if (io_remap_pfn_range(vma, vma->vm_start, pfn,
2271 PAGE_SIZE, vma->vm_page_prot))
2272 return -EAGAIN;
Matan Barakd69e3bc2015-12-15 20:30:13 +02002273 break;
Feras Daoud5c99eae2018-01-16 20:08:41 +02002274 case MLX5_IB_MMAP_CLOCK_INFO:
2275 return mlx5_ib_mmap_clock_info_page(dev, vma, context);
Matan Barakd69e3bc2015-12-15 20:30:13 +02002276
Ariel Levkovich24da0012018-04-05 18:53:27 +03002277 case MLX5_IB_MMAP_DEVICE_MEM:
2278 return dm_mmap(ibcontext, vma);
2279
Eli Cohene126ba92013-07-07 17:25:49 +03002280 default:
2281 return -EINVAL;
2282 }
2283
2284 return 0;
2285}
2286
Ariel Levkovich24da0012018-04-05 18:53:27 +03002287struct ib_dm *mlx5_ib_alloc_dm(struct ib_device *ibdev,
2288 struct ib_ucontext *context,
2289 struct ib_dm_alloc_attr *attr,
2290 struct uverbs_attr_bundle *attrs)
2291{
2292 u64 act_size = roundup(attr->length, MLX5_MEMIC_BASE_SIZE);
2293 struct mlx5_memic *memic = &to_mdev(ibdev)->memic;
2294 phys_addr_t memic_addr;
2295 struct mlx5_ib_dm *dm;
2296 u64 start_offset;
2297 u32 page_idx;
2298 int err;
2299
2300 dm = kzalloc(sizeof(*dm), GFP_KERNEL);
2301 if (!dm)
2302 return ERR_PTR(-ENOMEM);
2303
2304 mlx5_ib_dbg(to_mdev(ibdev), "alloc_memic req: user_length=0x%llx act_length=0x%llx log_alignment=%d\n",
2305 attr->length, act_size, attr->alignment);
2306
2307 err = mlx5_cmd_alloc_memic(memic, &memic_addr,
2308 act_size, attr->alignment);
2309 if (err)
2310 goto err_free;
2311
2312 start_offset = memic_addr & ~PAGE_MASK;
Huy Nguyenaa8106f2019-03-29 15:38:01 -07002313 page_idx = (memic_addr - memic->dev->bar_addr -
Ariel Levkovich24da0012018-04-05 18:53:27 +03002314 MLX5_CAP64_DEV_MEM(memic->dev, memic_bar_start_addr)) >>
2315 PAGE_SHIFT;
2316
2317 err = uverbs_copy_to(attrs,
2318 MLX5_IB_ATTR_ALLOC_DM_RESP_START_OFFSET,
2319 &start_offset, sizeof(start_offset));
2320 if (err)
2321 goto err_dealloc;
2322
2323 err = uverbs_copy_to(attrs,
2324 MLX5_IB_ATTR_ALLOC_DM_RESP_PAGE_INDEX,
2325 &page_idx, sizeof(page_idx));
2326 if (err)
2327 goto err_dealloc;
2328
2329 bitmap_set(to_mucontext(context)->dm_pages, page_idx,
2330 DIV_ROUND_UP(act_size, PAGE_SIZE));
2331
2332 dm->dev_addr = memic_addr;
2333
2334 return &dm->ibdm;
2335
2336err_dealloc:
2337 mlx5_cmd_dealloc_memic(memic, memic_addr,
2338 act_size);
2339err_free:
2340 kfree(dm);
2341 return ERR_PTR(err);
2342}
2343
Shamir Rabinovitchc4367a22019-03-31 19:10:05 +03002344int mlx5_ib_dealloc_dm(struct ib_dm *ibdm, struct uverbs_attr_bundle *attrs)
Ariel Levkovich24da0012018-04-05 18:53:27 +03002345{
2346 struct mlx5_memic *memic = &to_mdev(ibdm->device)->memic;
2347 struct mlx5_ib_dm *dm = to_mdm(ibdm);
2348 u64 act_size = roundup(dm->ibdm.length, MLX5_MEMIC_BASE_SIZE);
2349 u32 page_idx;
2350 int ret;
2351
2352 ret = mlx5_cmd_dealloc_memic(memic, dm->dev_addr, act_size);
2353 if (ret)
2354 return ret;
2355
Huy Nguyenaa8106f2019-03-29 15:38:01 -07002356 page_idx = (dm->dev_addr - memic->dev->bar_addr -
Ariel Levkovich24da0012018-04-05 18:53:27 +03002357 MLX5_CAP64_DEV_MEM(memic->dev, memic_bar_start_addr)) >>
2358 PAGE_SHIFT;
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +03002359 bitmap_clear(rdma_udata_to_drv_context(
2360 &attrs->driver_udata,
2361 struct mlx5_ib_ucontext,
2362 ibucontext)->dm_pages,
Ariel Levkovich24da0012018-04-05 18:53:27 +03002363 page_idx,
2364 DIV_ROUND_UP(act_size, PAGE_SIZE));
2365
2366 kfree(dm);
2367
2368 return 0;
2369}
2370
Shamir Rabinovitchff23dfa2019-03-31 19:10:07 +03002371static int mlx5_ib_alloc_pd(struct ib_pd *ibpd, struct ib_udata *udata)
Eli Cohene126ba92013-07-07 17:25:49 +03002372{
Leon Romanovsky21a428a2019-02-03 14:55:51 +02002373 struct mlx5_ib_pd *pd = to_mpd(ibpd);
2374 struct ib_device *ibdev = ibpd->device;
Eli Cohene126ba92013-07-07 17:25:49 +03002375 struct mlx5_ib_alloc_pd_resp resp;
Eli Cohene126ba92013-07-07 17:25:49 +03002376 int err;
Yishai Hadasa1069c12018-09-20 21:39:19 +03002377 u32 out[MLX5_ST_SZ_DW(alloc_pd_out)] = {};
2378 u32 in[MLX5_ST_SZ_DW(alloc_pd_in)] = {};
2379 u16 uid = 0;
Shamir Rabinovitchff23dfa2019-03-31 19:10:07 +03002380 struct mlx5_ib_ucontext *context = rdma_udata_to_drv_context(
2381 udata, struct mlx5_ib_ucontext, ibucontext);
Eli Cohene126ba92013-07-07 17:25:49 +03002382
Shamir Rabinovitchff23dfa2019-03-31 19:10:07 +03002383 uid = context ? context->devx_uid : 0;
Yishai Hadasa1069c12018-09-20 21:39:19 +03002384 MLX5_SET(alloc_pd_in, in, opcode, MLX5_CMD_OP_ALLOC_PD);
2385 MLX5_SET(alloc_pd_in, in, uid, uid);
2386 err = mlx5_cmd_exec(to_mdev(ibdev)->mdev, in, sizeof(in),
2387 out, sizeof(out));
Leon Romanovsky21a428a2019-02-03 14:55:51 +02002388 if (err)
2389 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03002390
Yishai Hadasa1069c12018-09-20 21:39:19 +03002391 pd->pdn = MLX5_GET(alloc_pd_out, out, pd);
2392 pd->uid = uid;
Shamir Rabinovitchff23dfa2019-03-31 19:10:07 +03002393 if (udata) {
Eli Cohene126ba92013-07-07 17:25:49 +03002394 resp.pdn = pd->pdn;
2395 if (ib_copy_to_udata(udata, &resp, sizeof(resp))) {
Yishai Hadasa1069c12018-09-20 21:39:19 +03002396 mlx5_cmd_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn, uid);
Leon Romanovsky21a428a2019-02-03 14:55:51 +02002397 return -EFAULT;
Eli Cohene126ba92013-07-07 17:25:49 +03002398 }
Eli Cohene126ba92013-07-07 17:25:49 +03002399 }
2400
Leon Romanovsky21a428a2019-02-03 14:55:51 +02002401 return 0;
Eli Cohene126ba92013-07-07 17:25:49 +03002402}
2403
Shamir Rabinovitchc4367a22019-03-31 19:10:05 +03002404static void mlx5_ib_dealloc_pd(struct ib_pd *pd, struct ib_udata *udata)
Eli Cohene126ba92013-07-07 17:25:49 +03002405{
2406 struct mlx5_ib_dev *mdev = to_mdev(pd->device);
2407 struct mlx5_ib_pd *mpd = to_mpd(pd);
2408
Yishai Hadasa1069c12018-09-20 21:39:19 +03002409 mlx5_cmd_dealloc_pd(mdev->mdev, mpd->pdn, mpd->uid);
Eli Cohene126ba92013-07-07 17:25:49 +03002410}
2411
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002412enum {
2413 MATCH_CRITERIA_ENABLE_OUTER_BIT,
2414 MATCH_CRITERIA_ENABLE_MISC_BIT,
Ariel Levkovich71c6e862018-05-13 14:33:34 +03002415 MATCH_CRITERIA_ENABLE_INNER_BIT,
2416 MATCH_CRITERIA_ENABLE_MISC2_BIT
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002417};
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002418
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002419#define HEADER_IS_ZERO(match_criteria, headers) \
2420 !(memchr_inv(MLX5_ADDR_OF(fte_match_param, match_criteria, headers), \
2421 0, MLX5_FLD_SZ_BYTES(fte_match_param, headers))) \
2422
2423static u8 get_match_criteria_enable(u32 *match_criteria)
2424{
2425 u8 match_criteria_enable;
2426
2427 match_criteria_enable =
2428 (!HEADER_IS_ZERO(match_criteria, outer_headers)) <<
2429 MATCH_CRITERIA_ENABLE_OUTER_BIT;
2430 match_criteria_enable |=
2431 (!HEADER_IS_ZERO(match_criteria, misc_parameters)) <<
2432 MATCH_CRITERIA_ENABLE_MISC_BIT;
2433 match_criteria_enable |=
2434 (!HEADER_IS_ZERO(match_criteria, inner_headers)) <<
2435 MATCH_CRITERIA_ENABLE_INNER_BIT;
Ariel Levkovich71c6e862018-05-13 14:33:34 +03002436 match_criteria_enable |=
2437 (!HEADER_IS_ZERO(match_criteria, misc_parameters_2)) <<
2438 MATCH_CRITERIA_ENABLE_MISC2_BIT;
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002439
2440 return match_criteria_enable;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002441}
2442
Maor Gottlieb6113cc42019-01-17 20:08:15 +02002443static int set_proto(void *outer_c, void *outer_v, u8 mask, u8 val)
Maor Gottliebca0d4752016-08-30 16:58:35 +03002444{
Maor Gottlieb6113cc42019-01-17 20:08:15 +02002445 u8 entry_mask;
2446 u8 entry_val;
2447 int err = 0;
2448
2449 if (!mask)
2450 goto out;
2451
2452 entry_mask = MLX5_GET(fte_match_set_lyr_2_4, outer_c,
2453 ip_protocol);
2454 entry_val = MLX5_GET(fte_match_set_lyr_2_4, outer_v,
2455 ip_protocol);
2456 if (!entry_mask) {
2457 MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_protocol, mask);
2458 MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_protocol, val);
2459 goto out;
2460 }
2461 /* Don't override existing ip protocol */
2462 if (mask != entry_mask || val != entry_val)
2463 err = -EINVAL;
2464out:
2465 return err;
Maor Gottliebca0d4752016-08-30 16:58:35 +03002466}
2467
Daria Velikovsky37da2a02018-05-07 10:20:02 +03002468static void set_flow_label(void *misc_c, void *misc_v, u32 mask, u32 val,
Moses Reuben2d1e6972016-11-14 19:04:52 +02002469 bool inner)
2470{
2471 if (inner) {
2472 MLX5_SET(fte_match_set_misc,
2473 misc_c, inner_ipv6_flow_label, mask);
2474 MLX5_SET(fte_match_set_misc,
2475 misc_v, inner_ipv6_flow_label, val);
2476 } else {
2477 MLX5_SET(fte_match_set_misc,
2478 misc_c, outer_ipv6_flow_label, mask);
2479 MLX5_SET(fte_match_set_misc,
2480 misc_v, outer_ipv6_flow_label, val);
2481 }
2482}
2483
Maor Gottliebca0d4752016-08-30 16:58:35 +03002484static void set_tos(void *outer_c, void *outer_v, u8 mask, u8 val)
2485{
2486 MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_ecn, mask);
2487 MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_ecn, val);
2488 MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_dscp, mask >> 2);
2489 MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_dscp, val >> 2);
2490}
2491
Ariel Levkovich71c6e862018-05-13 14:33:34 +03002492static int check_mpls_supp_fields(u32 field_support, const __be32 *set_mask)
2493{
2494 if (MLX5_GET(fte_match_mpls, set_mask, mpls_label) &&
2495 !(field_support & MLX5_FIELD_SUPPORT_MPLS_LABEL))
2496 return -EOPNOTSUPP;
2497
2498 if (MLX5_GET(fte_match_mpls, set_mask, mpls_exp) &&
2499 !(field_support & MLX5_FIELD_SUPPORT_MPLS_EXP))
2500 return -EOPNOTSUPP;
2501
2502 if (MLX5_GET(fte_match_mpls, set_mask, mpls_s_bos) &&
2503 !(field_support & MLX5_FIELD_SUPPORT_MPLS_S_BOS))
2504 return -EOPNOTSUPP;
2505
2506 if (MLX5_GET(fte_match_mpls, set_mask, mpls_ttl) &&
2507 !(field_support & MLX5_FIELD_SUPPORT_MPLS_TTL))
2508 return -EOPNOTSUPP;
2509
2510 return 0;
2511}
2512
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002513#define LAST_ETH_FIELD vlan_tag
2514#define LAST_IB_FIELD sl
Maor Gottliebca0d4752016-08-30 16:58:35 +03002515#define LAST_IPV4_FIELD tos
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002516#define LAST_IPV6_FIELD traffic_class
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002517#define LAST_TCP_UDP_FIELD src_port
Moses Reubenffb30d82016-11-14 19:04:50 +02002518#define LAST_TUNNEL_FIELD tunnel_id
Moses Reuben2ac693f2017-01-18 14:59:50 +02002519#define LAST_FLOW_TAG_FIELD tag_id
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002520#define LAST_DROP_FIELD size
Raed Salem3b3233f2018-05-31 16:43:39 +03002521#define LAST_COUNTERS_FIELD counters
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002522
2523/* Field is the last supported field */
2524#define FIELDS_NOT_SUPPORTED(filter, field)\
2525 memchr_inv((void *)&filter.field +\
2526 sizeof(filter.field), 0,\
2527 sizeof(filter) -\
2528 offsetof(typeof(filter), field) -\
2529 sizeof(filter.field))
2530
Mark Bloch2ea26202018-09-06 17:27:03 +03002531int parse_flow_flow_action(struct mlx5_ib_flow_action *maction,
2532 bool is_egress,
2533 struct mlx5_flow_act *action)
Aviad Yehezkel802c2122018-03-28 09:27:53 +03002534{
Aviad Yehezkel802c2122018-03-28 09:27:53 +03002535
2536 switch (maction->ib_action.type) {
2537 case IB_FLOW_ACTION_ESP:
Mark Bloch501f14e2018-09-06 17:27:04 +03002538 if (action->action & (MLX5_FLOW_CONTEXT_ACTION_ENCRYPT |
2539 MLX5_FLOW_CONTEXT_ACTION_DECRYPT))
2540 return -EINVAL;
Aviad Yehezkel802c2122018-03-28 09:27:53 +03002541 /* Currently only AES_GCM keymat is supported by the driver */
2542 action->esp_id = (uintptr_t)maction->esp_aes_gcm.ctx;
Mark Bloch2ea26202018-09-06 17:27:03 +03002543 action->action |= is_egress ?
Aviad Yehezkel802c2122018-03-28 09:27:53 +03002544 MLX5_FLOW_CONTEXT_ACTION_ENCRYPT :
2545 MLX5_FLOW_CONTEXT_ACTION_DECRYPT;
2546 return 0;
Mark Blochb1085be2018-09-02 12:51:32 +03002547 case IB_FLOW_ACTION_UNSPECIFIED:
2548 if (maction->flow_action_raw.sub_type ==
2549 MLX5_IB_FLOW_ACTION_MODIFY_HEADER) {
Mark Bloch501f14e2018-09-06 17:27:04 +03002550 if (action->action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR)
2551 return -EINVAL;
Mark Blochb1085be2018-09-02 12:51:32 +03002552 action->action |= MLX5_FLOW_CONTEXT_ACTION_MOD_HDR;
2553 action->modify_id = maction->flow_action_raw.action_id;
2554 return 0;
2555 }
Mark Bloch10a30892018-09-02 12:51:34 +03002556 if (maction->flow_action_raw.sub_type ==
2557 MLX5_IB_FLOW_ACTION_DECAP) {
Mark Bloch501f14e2018-09-06 17:27:04 +03002558 if (action->action & MLX5_FLOW_CONTEXT_ACTION_DECAP)
2559 return -EINVAL;
Mark Bloch10a30892018-09-02 12:51:34 +03002560 action->action |= MLX5_FLOW_CONTEXT_ACTION_DECAP;
2561 return 0;
2562 }
Mark Bloche806f932018-09-02 12:51:36 +03002563 if (maction->flow_action_raw.sub_type ==
2564 MLX5_IB_FLOW_ACTION_PACKET_REFORMAT) {
Mark Bloch501f14e2018-09-06 17:27:04 +03002565 if (action->action &
2566 MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT)
2567 return -EINVAL;
Mark Bloche806f932018-09-02 12:51:36 +03002568 action->action |=
2569 MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT;
2570 action->reformat_id =
2571 maction->flow_action_raw.action_id;
2572 return 0;
2573 }
Mark Blochb1085be2018-09-02 12:51:32 +03002574 /* fall through */
Aviad Yehezkel802c2122018-03-28 09:27:53 +03002575 default:
2576 return -EOPNOTSUPP;
2577 }
2578}
2579
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002580static int parse_flow_attr(struct mlx5_core_dev *mdev, u32 *match_c,
2581 u32 *match_v, const union ib_flow_spec *ib_spec,
Aviad Yehezkel802c2122018-03-28 09:27:53 +03002582 const struct ib_flow_attr *flow_attr,
Ariel Levkovich71c6e862018-05-13 14:33:34 +03002583 struct mlx5_flow_act *action, u32 prev_type)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002584{
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002585 void *misc_params_c = MLX5_ADDR_OF(fte_match_param, match_c,
2586 misc_parameters);
2587 void *misc_params_v = MLX5_ADDR_OF(fte_match_param, match_v,
2588 misc_parameters);
Ariel Levkovich71c6e862018-05-13 14:33:34 +03002589 void *misc_params2_c = MLX5_ADDR_OF(fte_match_param, match_c,
2590 misc_parameters_2);
2591 void *misc_params2_v = MLX5_ADDR_OF(fte_match_param, match_v,
2592 misc_parameters_2);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002593 void *headers_c;
2594 void *headers_v;
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002595 int match_ipv;
Aviad Yehezkel802c2122018-03-28 09:27:53 +03002596 int ret;
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002597
Moses Reuben2d1e6972016-11-14 19:04:52 +02002598 if (ib_spec->type & IB_FLOW_SPEC_INNER) {
2599 headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
2600 inner_headers);
2601 headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
2602 inner_headers);
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002603 match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2604 ft_field_support.inner_ip_version);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002605 } else {
2606 headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
2607 outer_headers);
2608 headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
2609 outer_headers);
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002610 match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2611 ft_field_support.outer_ip_version);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002612 }
2613
2614 switch (ib_spec->type & ~IB_FLOW_SPEC_INNER) {
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002615 case IB_FLOW_SPEC_ETH:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002616 if (FIELDS_NOT_SUPPORTED(ib_spec->eth.mask, LAST_ETH_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002617 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002618
Moses Reuben2d1e6972016-11-14 19:04:52 +02002619 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002620 dmac_47_16),
2621 ib_spec->eth.mask.dst_mac);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002622 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002623 dmac_47_16),
2624 ib_spec->eth.val.dst_mac);
2625
Moses Reuben2d1e6972016-11-14 19:04:52 +02002626 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottliebee3da802016-09-12 19:16:24 +03002627 smac_47_16),
2628 ib_spec->eth.mask.src_mac);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002629 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottliebee3da802016-09-12 19:16:24 +03002630 smac_47_16),
2631 ib_spec->eth.val.src_mac);
2632
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002633 if (ib_spec->eth.mask.vlan_tag) {
Moses Reuben2d1e6972016-11-14 19:04:52 +02002634 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Mohamad Haj Yahia10543362016-10-09 16:25:43 +03002635 cvlan_tag, 1);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002636 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Mohamad Haj Yahia10543362016-10-09 16:25:43 +03002637 cvlan_tag, 1);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002638
Moses Reuben2d1e6972016-11-14 19:04:52 +02002639 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002640 first_vid, ntohs(ib_spec->eth.mask.vlan_tag));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002641 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002642 first_vid, ntohs(ib_spec->eth.val.vlan_tag));
2643
Moses Reuben2d1e6972016-11-14 19:04:52 +02002644 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002645 first_cfi,
2646 ntohs(ib_spec->eth.mask.vlan_tag) >> 12);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002647 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002648 first_cfi,
2649 ntohs(ib_spec->eth.val.vlan_tag) >> 12);
2650
Moses Reuben2d1e6972016-11-14 19:04:52 +02002651 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002652 first_prio,
2653 ntohs(ib_spec->eth.mask.vlan_tag) >> 13);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002654 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002655 first_prio,
2656 ntohs(ib_spec->eth.val.vlan_tag) >> 13);
2657 }
Moses Reuben2d1e6972016-11-14 19:04:52 +02002658 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002659 ethertype, ntohs(ib_spec->eth.mask.ether_type));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002660 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002661 ethertype, ntohs(ib_spec->eth.val.ether_type));
2662 break;
2663 case IB_FLOW_SPEC_IPV4:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002664 if (FIELDS_NOT_SUPPORTED(ib_spec->ipv4.mask, LAST_IPV4_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002665 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002666
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002667 if (match_ipv) {
2668 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2669 ip_version, 0xf);
2670 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Boris Pismenny3346c482017-08-20 15:13:08 +03002671 ip_version, MLX5_FS_IPV4_VERSION);
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002672 } else {
2673 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2674 ethertype, 0xffff);
2675 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2676 ethertype, ETH_P_IP);
2677 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002678
Moses Reuben2d1e6972016-11-14 19:04:52 +02002679 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002680 src_ipv4_src_ipv6.ipv4_layout.ipv4),
2681 &ib_spec->ipv4.mask.src_ip,
2682 sizeof(ib_spec->ipv4.mask.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002683 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002684 src_ipv4_src_ipv6.ipv4_layout.ipv4),
2685 &ib_spec->ipv4.val.src_ip,
2686 sizeof(ib_spec->ipv4.val.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002687 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002688 dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2689 &ib_spec->ipv4.mask.dst_ip,
2690 sizeof(ib_spec->ipv4.mask.dst_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002691 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002692 dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2693 &ib_spec->ipv4.val.dst_ip,
2694 sizeof(ib_spec->ipv4.val.dst_ip));
Maor Gottliebca0d4752016-08-30 16:58:35 +03002695
Moses Reuben2d1e6972016-11-14 19:04:52 +02002696 set_tos(headers_c, headers_v,
Maor Gottliebca0d4752016-08-30 16:58:35 +03002697 ib_spec->ipv4.mask.tos, ib_spec->ipv4.val.tos);
2698
Maor Gottlieb6113cc42019-01-17 20:08:15 +02002699 if (set_proto(headers_c, headers_v,
2700 ib_spec->ipv4.mask.proto,
2701 ib_spec->ipv4.val.proto))
2702 return -EINVAL;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002703 break;
Maor Gottlieb026bae02016-06-17 15:14:51 +03002704 case IB_FLOW_SPEC_IPV6:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002705 if (FIELDS_NOT_SUPPORTED(ib_spec->ipv6.mask, LAST_IPV6_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002706 return -EOPNOTSUPP;
Maor Gottlieb026bae02016-06-17 15:14:51 +03002707
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002708 if (match_ipv) {
2709 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2710 ip_version, 0xf);
2711 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Boris Pismenny3346c482017-08-20 15:13:08 +03002712 ip_version, MLX5_FS_IPV6_VERSION);
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002713 } else {
2714 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2715 ethertype, 0xffff);
2716 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2717 ethertype, ETH_P_IPV6);
2718 }
Maor Gottlieb026bae02016-06-17 15:14:51 +03002719
Moses Reuben2d1e6972016-11-14 19:04:52 +02002720 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb026bae02016-06-17 15:14:51 +03002721 src_ipv4_src_ipv6.ipv6_layout.ipv6),
2722 &ib_spec->ipv6.mask.src_ip,
2723 sizeof(ib_spec->ipv6.mask.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002724 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb026bae02016-06-17 15:14:51 +03002725 src_ipv4_src_ipv6.ipv6_layout.ipv6),
2726 &ib_spec->ipv6.val.src_ip,
2727 sizeof(ib_spec->ipv6.val.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002728 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb026bae02016-06-17 15:14:51 +03002729 dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2730 &ib_spec->ipv6.mask.dst_ip,
2731 sizeof(ib_spec->ipv6.mask.dst_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002732 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb026bae02016-06-17 15:14:51 +03002733 dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2734 &ib_spec->ipv6.val.dst_ip,
2735 sizeof(ib_spec->ipv6.val.dst_ip));
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002736
Moses Reuben2d1e6972016-11-14 19:04:52 +02002737 set_tos(headers_c, headers_v,
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002738 ib_spec->ipv6.mask.traffic_class,
2739 ib_spec->ipv6.val.traffic_class);
2740
Maor Gottlieb6113cc42019-01-17 20:08:15 +02002741 if (set_proto(headers_c, headers_v,
2742 ib_spec->ipv6.mask.next_hdr,
2743 ib_spec->ipv6.val.next_hdr))
2744 return -EINVAL;
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002745
Moses Reuben2d1e6972016-11-14 19:04:52 +02002746 set_flow_label(misc_params_c, misc_params_v,
2747 ntohl(ib_spec->ipv6.mask.flow_label),
2748 ntohl(ib_spec->ipv6.val.flow_label),
2749 ib_spec->type & IB_FLOW_SPEC_INNER);
Aviad Yehezkel802c2122018-03-28 09:27:53 +03002750 break;
2751 case IB_FLOW_SPEC_ESP:
2752 if (ib_spec->esp.mask.seq)
2753 return -EOPNOTSUPP;
Moses Reuben2d1e6972016-11-14 19:04:52 +02002754
Aviad Yehezkel802c2122018-03-28 09:27:53 +03002755 MLX5_SET(fte_match_set_misc, misc_params_c, outer_esp_spi,
2756 ntohl(ib_spec->esp.mask.spi));
2757 MLX5_SET(fte_match_set_misc, misc_params_v, outer_esp_spi,
2758 ntohl(ib_spec->esp.val.spi));
Maor Gottlieb026bae02016-06-17 15:14:51 +03002759 break;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002760 case IB_FLOW_SPEC_TCP:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002761 if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
2762 LAST_TCP_UDP_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002763 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002764
Maor Gottlieb6113cc42019-01-17 20:08:15 +02002765 if (set_proto(headers_c, headers_v, 0xff, IPPROTO_TCP))
2766 return -EINVAL;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002767
Moses Reuben2d1e6972016-11-14 19:04:52 +02002768 MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002769 ntohs(ib_spec->tcp_udp.mask.src_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002770 MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002771 ntohs(ib_spec->tcp_udp.val.src_port));
2772
Moses Reuben2d1e6972016-11-14 19:04:52 +02002773 MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002774 ntohs(ib_spec->tcp_udp.mask.dst_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002775 MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002776 ntohs(ib_spec->tcp_udp.val.dst_port));
2777 break;
2778 case IB_FLOW_SPEC_UDP:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002779 if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
2780 LAST_TCP_UDP_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002781 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002782
Maor Gottlieb6113cc42019-01-17 20:08:15 +02002783 if (set_proto(headers_c, headers_v, 0xff, IPPROTO_UDP))
2784 return -EINVAL;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002785
Moses Reuben2d1e6972016-11-14 19:04:52 +02002786 MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002787 ntohs(ib_spec->tcp_udp.mask.src_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002788 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002789 ntohs(ib_spec->tcp_udp.val.src_port));
2790
Moses Reuben2d1e6972016-11-14 19:04:52 +02002791 MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002792 ntohs(ib_spec->tcp_udp.mask.dst_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002793 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002794 ntohs(ib_spec->tcp_udp.val.dst_port));
2795 break;
Ariel Levkovichda2f22a2018-05-13 14:33:33 +03002796 case IB_FLOW_SPEC_GRE:
2797 if (ib_spec->gre.mask.c_ks_res0_ver)
2798 return -EOPNOTSUPP;
2799
Maor Gottlieb6113cc42019-01-17 20:08:15 +02002800 if (set_proto(headers_c, headers_v, 0xff, IPPROTO_GRE))
2801 return -EINVAL;
2802
Ariel Levkovichda2f22a2018-05-13 14:33:33 +03002803 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
2804 0xff);
2805 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
2806 IPPROTO_GRE);
2807
2808 MLX5_SET(fte_match_set_misc, misc_params_c, gre_protocol,
Maor Gottlieba93b6322018-07-01 15:50:17 +03002809 ntohs(ib_spec->gre.mask.protocol));
Ariel Levkovichda2f22a2018-05-13 14:33:33 +03002810 MLX5_SET(fte_match_set_misc, misc_params_v, gre_protocol,
2811 ntohs(ib_spec->gre.val.protocol));
2812
2813 memcpy(MLX5_ADDR_OF(fte_match_set_misc, misc_params_c,
Oz Shlomo5886a962018-12-10 13:15:13 -08002814 gre_key.nvgre.hi),
Ariel Levkovichda2f22a2018-05-13 14:33:33 +03002815 &ib_spec->gre.mask.key,
2816 sizeof(ib_spec->gre.mask.key));
2817 memcpy(MLX5_ADDR_OF(fte_match_set_misc, misc_params_v,
Oz Shlomo5886a962018-12-10 13:15:13 -08002818 gre_key.nvgre.hi),
Ariel Levkovichda2f22a2018-05-13 14:33:33 +03002819 &ib_spec->gre.val.key,
2820 sizeof(ib_spec->gre.val.key));
2821 break;
Ariel Levkovich71c6e862018-05-13 14:33:34 +03002822 case IB_FLOW_SPEC_MPLS:
2823 switch (prev_type) {
2824 case IB_FLOW_SPEC_UDP:
2825 if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2826 ft_field_support.outer_first_mpls_over_udp),
2827 &ib_spec->mpls.mask.tag))
2828 return -EOPNOTSUPP;
2829
2830 memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
2831 outer_first_mpls_over_udp),
2832 &ib_spec->mpls.val.tag,
2833 sizeof(ib_spec->mpls.val.tag));
2834 memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
2835 outer_first_mpls_over_udp),
2836 &ib_spec->mpls.mask.tag,
2837 sizeof(ib_spec->mpls.mask.tag));
2838 break;
2839 case IB_FLOW_SPEC_GRE:
2840 if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2841 ft_field_support.outer_first_mpls_over_gre),
2842 &ib_spec->mpls.mask.tag))
2843 return -EOPNOTSUPP;
2844
2845 memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
2846 outer_first_mpls_over_gre),
2847 &ib_spec->mpls.val.tag,
2848 sizeof(ib_spec->mpls.val.tag));
2849 memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
2850 outer_first_mpls_over_gre),
2851 &ib_spec->mpls.mask.tag,
2852 sizeof(ib_spec->mpls.mask.tag));
2853 break;
2854 default:
2855 if (ib_spec->type & IB_FLOW_SPEC_INNER) {
2856 if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2857 ft_field_support.inner_first_mpls),
2858 &ib_spec->mpls.mask.tag))
2859 return -EOPNOTSUPP;
2860
2861 memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
2862 inner_first_mpls),
2863 &ib_spec->mpls.val.tag,
2864 sizeof(ib_spec->mpls.val.tag));
2865 memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
2866 inner_first_mpls),
2867 &ib_spec->mpls.mask.tag,
2868 sizeof(ib_spec->mpls.mask.tag));
2869 } else {
2870 if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2871 ft_field_support.outer_first_mpls),
2872 &ib_spec->mpls.mask.tag))
2873 return -EOPNOTSUPP;
2874
2875 memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
2876 outer_first_mpls),
2877 &ib_spec->mpls.val.tag,
2878 sizeof(ib_spec->mpls.val.tag));
2879 memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
2880 outer_first_mpls),
2881 &ib_spec->mpls.mask.tag,
2882 sizeof(ib_spec->mpls.mask.tag));
2883 }
2884 }
2885 break;
Moses Reubenffb30d82016-11-14 19:04:50 +02002886 case IB_FLOW_SPEC_VXLAN_TUNNEL:
2887 if (FIELDS_NOT_SUPPORTED(ib_spec->tunnel.mask,
2888 LAST_TUNNEL_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002889 return -EOPNOTSUPP;
Moses Reubenffb30d82016-11-14 19:04:50 +02002890
2891 MLX5_SET(fte_match_set_misc, misc_params_c, vxlan_vni,
2892 ntohl(ib_spec->tunnel.mask.tunnel_id));
2893 MLX5_SET(fte_match_set_misc, misc_params_v, vxlan_vni,
2894 ntohl(ib_spec->tunnel.val.tunnel_id));
2895 break;
Moses Reuben2ac693f2017-01-18 14:59:50 +02002896 case IB_FLOW_SPEC_ACTION_TAG:
2897 if (FIELDS_NOT_SUPPORTED(ib_spec->flow_tag,
2898 LAST_FLOW_TAG_FIELD))
2899 return -EOPNOTSUPP;
2900 if (ib_spec->flow_tag.tag_id >= BIT(24))
2901 return -EINVAL;
2902
Boris Pismenny075572d2017-08-16 09:33:30 +03002903 action->flow_tag = ib_spec->flow_tag.tag_id;
Paul Blakeyd5634fe2018-09-20 12:17:48 +02002904 action->flags |= FLOW_ACT_HAS_TAG;
Moses Reuben2ac693f2017-01-18 14:59:50 +02002905 break;
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002906 case IB_FLOW_SPEC_ACTION_DROP:
2907 if (FIELDS_NOT_SUPPORTED(ib_spec->drop,
2908 LAST_DROP_FIELD))
2909 return -EOPNOTSUPP;
Boris Pismenny075572d2017-08-16 09:33:30 +03002910 action->action |= MLX5_FLOW_CONTEXT_ACTION_DROP;
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002911 break;
Aviad Yehezkel802c2122018-03-28 09:27:53 +03002912 case IB_FLOW_SPEC_ACTION_HANDLE:
Mark Bloch2ea26202018-09-06 17:27:03 +03002913 ret = parse_flow_flow_action(to_mflow_act(ib_spec->action.act),
2914 flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS, action);
Aviad Yehezkel802c2122018-03-28 09:27:53 +03002915 if (ret)
2916 return ret;
2917 break;
Raed Salem3b3233f2018-05-31 16:43:39 +03002918 case IB_FLOW_SPEC_ACTION_COUNT:
2919 if (FIELDS_NOT_SUPPORTED(ib_spec->flow_count,
2920 LAST_COUNTERS_FIELD))
2921 return -EOPNOTSUPP;
2922
2923 /* for now support only one counters spec per flow */
2924 if (action->action & MLX5_FLOW_CONTEXT_ACTION_COUNT)
2925 return -EINVAL;
2926
2927 action->counters = ib_spec->flow_count.counters;
2928 action->action |= MLX5_FLOW_CONTEXT_ACTION_COUNT;
2929 break;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002930 default:
2931 return -EINVAL;
2932 }
2933
2934 return 0;
2935}
2936
2937/* If a flow could catch both multicast and unicast packets,
2938 * it won't fall into the multicast flow steering table and this rule
2939 * could steal other multicast packets.
2940 */
Yishai Hadasa550ddf2017-08-17 15:52:33 +03002941static bool flow_is_multicast_only(const struct ib_flow_attr *ib_attr)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002942{
Yishai Hadas81e30882017-06-08 16:15:09 +03002943 union ib_flow_spec *flow_spec;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002944
2945 if (ib_attr->type != IB_FLOW_ATTR_NORMAL ||
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002946 ib_attr->num_of_specs < 1)
2947 return false;
2948
Yishai Hadas81e30882017-06-08 16:15:09 +03002949 flow_spec = (union ib_flow_spec *)(ib_attr + 1);
2950 if (flow_spec->type == IB_FLOW_SPEC_IPV4) {
2951 struct ib_flow_spec_ipv4 *ipv4_spec;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002952
Yishai Hadas81e30882017-06-08 16:15:09 +03002953 ipv4_spec = (struct ib_flow_spec_ipv4 *)flow_spec;
2954 if (ipv4_is_multicast(ipv4_spec->val.dst_ip))
2955 return true;
2956
2957 return false;
2958 }
2959
2960 if (flow_spec->type == IB_FLOW_SPEC_ETH) {
2961 struct ib_flow_spec_eth *eth_spec;
2962
2963 eth_spec = (struct ib_flow_spec_eth *)flow_spec;
2964 return is_multicast_ether_addr(eth_spec->mask.dst_mac) &&
2965 is_multicast_ether_addr(eth_spec->val.dst_mac);
2966 }
2967
2968 return false;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002969}
2970
Aviad Yehezkel802c2122018-03-28 09:27:53 +03002971enum valid_spec {
2972 VALID_SPEC_INVALID,
2973 VALID_SPEC_VALID,
2974 VALID_SPEC_NA,
2975};
2976
2977static enum valid_spec
2978is_valid_esp_aes_gcm(struct mlx5_core_dev *mdev,
2979 const struct mlx5_flow_spec *spec,
2980 const struct mlx5_flow_act *flow_act,
2981 bool egress)
2982{
2983 const u32 *match_c = spec->match_criteria;
2984 bool is_crypto =
2985 (flow_act->action & (MLX5_FLOW_CONTEXT_ACTION_ENCRYPT |
2986 MLX5_FLOW_CONTEXT_ACTION_DECRYPT));
2987 bool is_ipsec = mlx5_fs_is_ipsec_flow(match_c);
2988 bool is_drop = flow_act->action & MLX5_FLOW_CONTEXT_ACTION_DROP;
2989
2990 /*
2991 * Currently only crypto is supported in egress, when regular egress
2992 * rules would be supported, always return VALID_SPEC_NA.
2993 */
2994 if (!is_crypto)
Mark Bloch78dd0c42018-09-02 12:51:31 +03002995 return VALID_SPEC_NA;
Aviad Yehezkel802c2122018-03-28 09:27:53 +03002996
2997 return is_crypto && is_ipsec &&
Paul Blakeyd5634fe2018-09-20 12:17:48 +02002998 (!egress || (!is_drop && !(flow_act->flags & FLOW_ACT_HAS_TAG))) ?
Aviad Yehezkel802c2122018-03-28 09:27:53 +03002999 VALID_SPEC_VALID : VALID_SPEC_INVALID;
3000}
3001
3002static bool is_valid_spec(struct mlx5_core_dev *mdev,
3003 const struct mlx5_flow_spec *spec,
3004 const struct mlx5_flow_act *flow_act,
3005 bool egress)
3006{
3007 /* We curretly only support ipsec egress flow */
3008 return is_valid_esp_aes_gcm(mdev, spec, flow_act, egress) != VALID_SPEC_INVALID;
3009}
3010
Ariel Levkovich19cc7522017-04-03 13:11:03 +03003011static bool is_valid_ethertype(struct mlx5_core_dev *mdev,
3012 const struct ib_flow_attr *flow_attr,
Ariel Levkovich0f750962017-04-03 13:11:02 +03003013 bool check_inner)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003014{
3015 union ib_flow_spec *ib_spec = (union ib_flow_spec *)(flow_attr + 1);
Ariel Levkovich19cc7522017-04-03 13:11:03 +03003016 int match_ipv = check_inner ?
3017 MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
3018 ft_field_support.inner_ip_version) :
3019 MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
3020 ft_field_support.outer_ip_version);
Ariel Levkovich0f750962017-04-03 13:11:02 +03003021 int inner_bit = check_inner ? IB_FLOW_SPEC_INNER : 0;
3022 bool ipv4_spec_valid, ipv6_spec_valid;
3023 unsigned int ip_spec_type = 0;
3024 bool has_ethertype = false;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003025 unsigned int spec_index;
Ariel Levkovich0f750962017-04-03 13:11:02 +03003026 bool mask_valid = true;
3027 u16 eth_type = 0;
3028 bool type_valid;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003029
3030 /* Validate that ethertype is correct */
3031 for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
Ariel Levkovich0f750962017-04-03 13:11:02 +03003032 if ((ib_spec->type == (IB_FLOW_SPEC_ETH | inner_bit)) &&
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003033 ib_spec->eth.mask.ether_type) {
Ariel Levkovich0f750962017-04-03 13:11:02 +03003034 mask_valid = (ib_spec->eth.mask.ether_type ==
3035 htons(0xffff));
3036 has_ethertype = true;
3037 eth_type = ntohs(ib_spec->eth.val.ether_type);
3038 } else if ((ib_spec->type == (IB_FLOW_SPEC_IPV4 | inner_bit)) ||
3039 (ib_spec->type == (IB_FLOW_SPEC_IPV6 | inner_bit))) {
3040 ip_spec_type = ib_spec->type;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003041 }
3042 ib_spec = (void *)ib_spec + ib_spec->size;
3043 }
Ariel Levkovich0f750962017-04-03 13:11:02 +03003044
3045 type_valid = (!has_ethertype) || (!ip_spec_type);
3046 if (!type_valid && mask_valid) {
3047 ipv4_spec_valid = (eth_type == ETH_P_IP) &&
3048 (ip_spec_type == (IB_FLOW_SPEC_IPV4 | inner_bit));
3049 ipv6_spec_valid = (eth_type == ETH_P_IPV6) &&
3050 (ip_spec_type == (IB_FLOW_SPEC_IPV6 | inner_bit));
Ariel Levkovich19cc7522017-04-03 13:11:03 +03003051
3052 type_valid = (ipv4_spec_valid) || (ipv6_spec_valid) ||
3053 (((eth_type == ETH_P_MPLS_UC) ||
3054 (eth_type == ETH_P_MPLS_MC)) && match_ipv);
Ariel Levkovich0f750962017-04-03 13:11:02 +03003055 }
3056
3057 return type_valid;
3058}
3059
Ariel Levkovich19cc7522017-04-03 13:11:03 +03003060static bool is_valid_attr(struct mlx5_core_dev *mdev,
3061 const struct ib_flow_attr *flow_attr)
Ariel Levkovich0f750962017-04-03 13:11:02 +03003062{
Ariel Levkovich19cc7522017-04-03 13:11:03 +03003063 return is_valid_ethertype(mdev, flow_attr, false) &&
3064 is_valid_ethertype(mdev, flow_attr, true);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003065}
3066
3067static void put_flow_table(struct mlx5_ib_dev *dev,
3068 struct mlx5_ib_flow_prio *prio, bool ft_added)
3069{
3070 prio->refcount -= !!ft_added;
3071 if (!prio->refcount) {
3072 mlx5_destroy_flow_table(prio->flow_table);
3073 prio->flow_table = NULL;
3074 }
3075}
3076
Raed Salem3b3233f2018-05-31 16:43:39 +03003077static void counters_clear_description(struct ib_counters *counters)
3078{
3079 struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
3080
3081 mutex_lock(&mcounters->mcntrs_mutex);
3082 kfree(mcounters->counters_data);
3083 mcounters->counters_data = NULL;
3084 mcounters->cntrs_max_index = 0;
3085 mutex_unlock(&mcounters->mcntrs_mutex);
3086}
3087
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003088static int mlx5_ib_destroy_flow(struct ib_flow *flow_id)
3089{
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003090 struct mlx5_ib_flow_handler *handler = container_of(flow_id,
3091 struct mlx5_ib_flow_handler,
3092 ibflow);
3093 struct mlx5_ib_flow_handler *iter, *tmp;
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003094 struct mlx5_ib_dev *dev = handler->dev;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003095
Mark Bloch9a4ca382018-01-16 14:42:35 +00003096 mutex_lock(&dev->flow_db->lock);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003097
3098 list_for_each_entry_safe(iter, tmp, &handler->list, list) {
Mark Bloch74491de2016-08-31 11:24:25 +00003099 mlx5_del_flow_rules(iter->rule);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003100 put_flow_table(dev, iter->prio, true);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003101 list_del(&iter->list);
3102 kfree(iter);
3103 }
3104
Mark Bloch74491de2016-08-31 11:24:25 +00003105 mlx5_del_flow_rules(handler->rule);
Maor Gottlieb5497adc2016-08-28 14:16:31 +03003106 put_flow_table(dev, handler->prio, true);
Raed Salem3b3233f2018-05-31 16:43:39 +03003107 if (handler->ibcounters &&
3108 atomic_read(&handler->ibcounters->usecnt) == 1)
3109 counters_clear_description(handler->ibcounters);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003110
Raed Salem3b3233f2018-05-31 16:43:39 +03003111 mutex_unlock(&dev->flow_db->lock);
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003112 if (handler->flow_matcher)
3113 atomic_dec(&handler->flow_matcher->usecnt);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003114 kfree(handler);
3115
3116 return 0;
3117}
3118
Maor Gottlieb35d190112016-03-07 18:51:47 +02003119static int ib_prio_to_core_prio(unsigned int priority, bool dont_trap)
3120{
3121 priority *= 2;
3122 if (!dont_trap)
3123 priority++;
3124 return priority;
3125}
3126
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003127enum flow_table_type {
3128 MLX5_IB_FT_RX,
3129 MLX5_IB_FT_TX
3130};
3131
Maor Gottlieb00b7c2a2017-03-29 06:09:01 +03003132#define MLX5_FS_MAX_TYPES 6
3133#define MLX5_FS_MAX_ENTRIES BIT(16)
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003134
3135static struct mlx5_ib_flow_prio *_get_prio(struct mlx5_flow_namespace *ns,
3136 struct mlx5_ib_flow_prio *prio,
3137 int priority,
Mark Bloch4adda112018-09-02 12:51:33 +03003138 int num_entries, int num_groups,
3139 u32 flags)
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003140{
3141 struct mlx5_flow_table *ft;
3142
3143 ft = mlx5_create_auto_grouped_flow_table(ns, priority,
3144 num_entries,
3145 num_groups,
Mark Bloch4adda112018-09-02 12:51:33 +03003146 0, flags);
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003147 if (IS_ERR(ft))
3148 return ERR_CAST(ft);
3149
3150 prio->flow_table = ft;
3151 prio->refcount = 0;
3152 return prio;
3153}
3154
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003155static struct mlx5_ib_flow_prio *get_flow_table(struct mlx5_ib_dev *dev,
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003156 struct ib_flow_attr *flow_attr,
3157 enum flow_table_type ft_type)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003158{
Maor Gottlieb35d190112016-03-07 18:51:47 +02003159 bool dont_trap = flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003160 struct mlx5_flow_namespace *ns = NULL;
3161 struct mlx5_ib_flow_prio *prio;
3162 struct mlx5_flow_table *ft;
Maor Gottliebdac388e2017-03-29 06:09:00 +03003163 int max_table_size;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003164 int num_entries;
3165 int num_groups;
Mark Bloch4adda112018-09-02 12:51:33 +03003166 u32 flags = 0;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003167 int priority;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003168
Maor Gottliebdac388e2017-03-29 06:09:00 +03003169 max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
3170 log_max_ft_size));
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003171 if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
Mark Bloch78dd0c42018-09-02 12:51:31 +03003172 enum mlx5_flow_namespace_type fn_type;
3173
3174 if (flow_is_multicast_only(flow_attr) &&
3175 !dont_trap)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003176 priority = MLX5_IB_FLOW_MCAST_PRIO;
3177 else
Maor Gottlieb35d190112016-03-07 18:51:47 +02003178 priority = ib_prio_to_core_prio(flow_attr->priority,
3179 dont_trap);
Mark Bloch78dd0c42018-09-02 12:51:31 +03003180 if (ft_type == MLX5_IB_FT_RX) {
3181 fn_type = MLX5_FLOW_NAMESPACE_BYPASS;
3182 prio = &dev->flow_db->prios[priority];
Mark Bloch6a4d00b2019-03-28 15:27:37 +02003183 if (!dev->is_rep &&
Mark Bloch4adda112018-09-02 12:51:33 +03003184 MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, decap))
3185 flags |= MLX5_FLOW_TABLE_TUNNEL_EN_DECAP;
Mark Bloch6a4d00b2019-03-28 15:27:37 +02003186 if (!dev->is_rep &&
Mark Bloch5c2db532018-09-02 12:51:35 +03003187 MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
3188 reformat_l3_tunnel_to_l2))
3189 flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
Mark Bloch78dd0c42018-09-02 12:51:31 +03003190 } else {
3191 max_table_size =
3192 BIT(MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev,
3193 log_max_ft_size));
3194 fn_type = MLX5_FLOW_NAMESPACE_EGRESS;
3195 prio = &dev->flow_db->egress_prios[priority];
Mark Bloch6a4d00b2019-03-28 15:27:37 +02003196 if (!dev->is_rep &&
Mark Bloch4adda112018-09-02 12:51:33 +03003197 MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev, reformat))
3198 flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
Mark Bloch78dd0c42018-09-02 12:51:31 +03003199 }
3200 ns = mlx5_get_flow_namespace(dev->mdev, fn_type);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003201 num_entries = MLX5_FS_MAX_ENTRIES;
3202 num_groups = MLX5_FS_MAX_TYPES;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003203 } else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
3204 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
3205 ns = mlx5_get_flow_namespace(dev->mdev,
3206 MLX5_FLOW_NAMESPACE_LEFTOVERS);
3207 build_leftovers_ft_param(&priority,
3208 &num_entries,
3209 &num_groups);
Mark Bloch9a4ca382018-01-16 14:42:35 +00003210 prio = &dev->flow_db->prios[MLX5_IB_FLOW_LEFTOVERS_PRIO];
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003211 } else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
3212 if (!MLX5_CAP_FLOWTABLE(dev->mdev,
3213 allow_sniffer_and_nic_rx_shared_tir))
3214 return ERR_PTR(-ENOTSUPP);
3215
3216 ns = mlx5_get_flow_namespace(dev->mdev, ft_type == MLX5_IB_FT_RX ?
3217 MLX5_FLOW_NAMESPACE_SNIFFER_RX :
3218 MLX5_FLOW_NAMESPACE_SNIFFER_TX);
3219
Mark Bloch9a4ca382018-01-16 14:42:35 +00003220 prio = &dev->flow_db->sniffer[ft_type];
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003221 priority = 0;
3222 num_entries = 1;
3223 num_groups = 1;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003224 }
3225
3226 if (!ns)
3227 return ERR_PTR(-ENOTSUPP);
3228
Maor Gottliebdac388e2017-03-29 06:09:00 +03003229 if (num_entries > max_table_size)
3230 return ERR_PTR(-ENOMEM);
3231
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003232 ft = prio->flow_table;
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003233 if (!ft)
Mark Bloch4adda112018-09-02 12:51:33 +03003234 return _get_prio(ns, prio, priority, num_entries, num_groups,
3235 flags);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003236
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003237 return prio;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003238}
3239
Yishai Hadasa550ddf2017-08-17 15:52:33 +03003240static void set_underlay_qp(struct mlx5_ib_dev *dev,
3241 struct mlx5_flow_spec *spec,
3242 u32 underlay_qpn)
3243{
3244 void *misc_params_c = MLX5_ADDR_OF(fte_match_param,
3245 spec->match_criteria,
3246 misc_parameters);
3247 void *misc_params_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
3248 misc_parameters);
3249
3250 if (underlay_qpn &&
3251 MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
3252 ft_field_support.bth_dst_qp)) {
3253 MLX5_SET(fte_match_set_misc,
3254 misc_params_v, bth_dst_qp, underlay_qpn);
3255 MLX5_SET(fte_match_set_misc,
3256 misc_params_c, bth_dst_qp, 0xffffff);
3257 }
3258}
3259
Raed Salem5e95af52018-05-31 16:43:40 +03003260static int read_flow_counters(struct ib_device *ibdev,
3261 struct mlx5_read_counters_attr *read_attr)
3262{
3263 struct mlx5_fc *fc = read_attr->hw_cntrs_hndl;
3264 struct mlx5_ib_dev *dev = to_mdev(ibdev);
3265
3266 return mlx5_fc_query(dev->mdev, fc,
3267 &read_attr->out[IB_COUNTER_PACKETS],
3268 &read_attr->out[IB_COUNTER_BYTES]);
3269}
3270
3271/* flow counters currently expose two counters packets and bytes */
3272#define FLOW_COUNTERS_NUM 2
Raed Salem3b3233f2018-05-31 16:43:39 +03003273static int counters_set_description(struct ib_counters *counters,
3274 enum mlx5_ib_counters_type counters_type,
3275 struct mlx5_ib_flow_counters_desc *desc_data,
3276 u32 ncounters)
3277{
3278 struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
3279 u32 cntrs_max_index = 0;
3280 int i;
3281
3282 if (counters_type != MLX5_IB_COUNTERS_FLOW)
3283 return -EINVAL;
3284
3285 /* init the fields for the object */
3286 mcounters->type = counters_type;
Raed Salem5e95af52018-05-31 16:43:40 +03003287 mcounters->read_counters = read_flow_counters;
3288 mcounters->counters_num = FLOW_COUNTERS_NUM;
Raed Salem3b3233f2018-05-31 16:43:39 +03003289 mcounters->ncounters = ncounters;
3290 /* each counter entry have both description and index pair */
3291 for (i = 0; i < ncounters; i++) {
3292 if (desc_data[i].description > IB_COUNTER_BYTES)
3293 return -EINVAL;
3294
3295 if (cntrs_max_index <= desc_data[i].index)
3296 cntrs_max_index = desc_data[i].index + 1;
3297 }
3298
3299 mutex_lock(&mcounters->mcntrs_mutex);
3300 mcounters->counters_data = desc_data;
3301 mcounters->cntrs_max_index = cntrs_max_index;
3302 mutex_unlock(&mcounters->mcntrs_mutex);
3303
3304 return 0;
3305}
3306
3307#define MAX_COUNTERS_NUM (USHRT_MAX / (sizeof(u32) * 2))
3308static int flow_counters_set_data(struct ib_counters *ibcounters,
3309 struct mlx5_ib_create_flow *ucmd)
3310{
3311 struct mlx5_ib_mcounters *mcounters = to_mcounters(ibcounters);
3312 struct mlx5_ib_flow_counters_data *cntrs_data = NULL;
3313 struct mlx5_ib_flow_counters_desc *desc_data = NULL;
3314 bool hw_hndl = false;
3315 int ret = 0;
3316
3317 if (ucmd && ucmd->ncounters_data != 0) {
3318 cntrs_data = ucmd->data;
3319 if (cntrs_data->ncounters > MAX_COUNTERS_NUM)
3320 return -EINVAL;
3321
3322 desc_data = kcalloc(cntrs_data->ncounters,
3323 sizeof(*desc_data),
3324 GFP_KERNEL);
3325 if (!desc_data)
3326 return -ENOMEM;
3327
3328 if (copy_from_user(desc_data,
3329 u64_to_user_ptr(cntrs_data->counters_data),
3330 sizeof(*desc_data) * cntrs_data->ncounters)) {
3331 ret = -EFAULT;
3332 goto free;
3333 }
3334 }
3335
3336 if (!mcounters->hw_cntrs_hndl) {
3337 mcounters->hw_cntrs_hndl = mlx5_fc_create(
3338 to_mdev(ibcounters->device)->mdev, false);
weiyongjun (A)e31abf72018-06-07 01:47:41 +00003339 if (IS_ERR(mcounters->hw_cntrs_hndl)) {
3340 ret = PTR_ERR(mcounters->hw_cntrs_hndl);
Raed Salem3b3233f2018-05-31 16:43:39 +03003341 goto free;
3342 }
3343 hw_hndl = true;
3344 }
3345
3346 if (desc_data) {
3347 /* counters already bound to at least one flow */
3348 if (mcounters->cntrs_max_index) {
3349 ret = -EINVAL;
3350 goto free_hndl;
3351 }
3352
3353 ret = counters_set_description(ibcounters,
3354 MLX5_IB_COUNTERS_FLOW,
3355 desc_data,
3356 cntrs_data->ncounters);
3357 if (ret)
3358 goto free_hndl;
3359
3360 } else if (!mcounters->cntrs_max_index) {
3361 /* counters not bound yet, must have udata passed */
3362 ret = -EINVAL;
3363 goto free_hndl;
3364 }
3365
3366 return 0;
3367
3368free_hndl:
3369 if (hw_hndl) {
3370 mlx5_fc_destroy(to_mdev(ibcounters->device)->mdev,
3371 mcounters->hw_cntrs_hndl);
3372 mcounters->hw_cntrs_hndl = NULL;
3373 }
3374free:
3375 kfree(desc_data);
3376 return ret;
3377}
3378
Yishai Hadasa550ddf2017-08-17 15:52:33 +03003379static struct mlx5_ib_flow_handler *_create_flow_rule(struct mlx5_ib_dev *dev,
3380 struct mlx5_ib_flow_prio *ft_prio,
3381 const struct ib_flow_attr *flow_attr,
3382 struct mlx5_flow_destination *dst,
Raed Salem3b3233f2018-05-31 16:43:39 +03003383 u32 underlay_qpn,
3384 struct mlx5_ib_create_flow *ucmd)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003385{
3386 struct mlx5_flow_table *ft = ft_prio->flow_table;
3387 struct mlx5_ib_flow_handler *handler;
Boris Pismenny075572d2017-08-16 09:33:30 +03003388 struct mlx5_flow_act flow_act = {.flow_tag = MLX5_FS_DEFAULT_FLOW_TAG};
Maor Gottliebc5bb1732016-07-04 17:23:05 +03003389 struct mlx5_flow_spec *spec;
Raed Salem3b3233f2018-05-31 16:43:39 +03003390 struct mlx5_flow_destination dest_arr[2] = {};
3391 struct mlx5_flow_destination *rule_dst = dest_arr;
Maor Gottliebdd063d02016-08-28 14:16:32 +03003392 const void *ib_flow = (const void *)flow_attr + sizeof(*flow_attr);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003393 unsigned int spec_index;
Ariel Levkovich71c6e862018-05-13 14:33:34 +03003394 u32 prev_type = 0;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003395 int err = 0;
Raed Salem3b3233f2018-05-31 16:43:39 +03003396 int dest_num = 0;
Aviad Yehezkel802c2122018-03-28 09:27:53 +03003397 bool is_egress = flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003398
Ariel Levkovich19cc7522017-04-03 13:11:03 +03003399 if (!is_valid_attr(dev->mdev, flow_attr))
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003400 return ERR_PTR(-EINVAL);
3401
Mark Bloch6a4d00b2019-03-28 15:27:37 +02003402 if (dev->is_rep && is_egress)
Mark Bloch78dd0c42018-09-02 12:51:31 +03003403 return ERR_PTR(-EINVAL);
3404
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03003405 spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003406 handler = kzalloc(sizeof(*handler), GFP_KERNEL);
Maor Gottliebc5bb1732016-07-04 17:23:05 +03003407 if (!handler || !spec) {
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003408 err = -ENOMEM;
3409 goto free;
3410 }
3411
3412 INIT_LIST_HEAD(&handler->list);
Raed Salem3b3233f2018-05-31 16:43:39 +03003413 if (dst) {
3414 memcpy(&dest_arr[0], dst, sizeof(*dst));
3415 dest_num++;
3416 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003417
3418 for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
Ariel Levkovich19cc7522017-04-03 13:11:03 +03003419 err = parse_flow_attr(dev->mdev, spec->match_criteria,
Slava Shwartsmana22ed862017-04-03 13:13:52 +03003420 spec->match_value,
Ariel Levkovich71c6e862018-05-13 14:33:34 +03003421 ib_flow, flow_attr, &flow_act,
3422 prev_type);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003423 if (err < 0)
3424 goto free;
3425
Ariel Levkovich71c6e862018-05-13 14:33:34 +03003426 prev_type = ((union ib_flow_spec *)ib_flow)->type;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003427 ib_flow += ((union ib_flow_spec *)ib_flow)->size;
3428 }
3429
Yishai Hadasa550ddf2017-08-17 15:52:33 +03003430 if (!flow_is_multicast_only(flow_attr))
3431 set_underlay_qp(dev, spec, underlay_qpn);
3432
Mark Bloch6a4d00b2019-03-28 15:27:37 +02003433 if (dev->is_rep) {
Mark Bloch018a94e2018-01-16 14:44:29 +00003434 void *misc;
3435
Mark Bloch6a4d00b2019-03-28 15:27:37 +02003436 if (!dev->port[flow_attr->port - 1].rep) {
3437 err = -EINVAL;
3438 goto free;
3439 }
Mark Bloch018a94e2018-01-16 14:44:29 +00003440 misc = MLX5_ADDR_OF(fte_match_param, spec->match_value,
3441 misc_parameters);
3442 MLX5_SET(fte_match_set_misc, misc, source_port,
Mark Bloch6a4d00b2019-03-28 15:27:37 +02003443 dev->port[flow_attr->port - 1].rep->vport);
Mark Bloch018a94e2018-01-16 14:44:29 +00003444 misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
3445 misc_parameters);
3446 MLX5_SET_TO_ONES(fte_match_set_misc, misc, source_port);
3447 }
3448
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03003449 spec->match_criteria_enable = get_match_criteria_enable(spec->match_criteria);
Aviad Yehezkel802c2122018-03-28 09:27:53 +03003450
3451 if (is_egress &&
3452 !is_valid_spec(dev->mdev, spec, &flow_act, is_egress)) {
3453 err = -EINVAL;
3454 goto free;
3455 }
3456
Raed Salem3b3233f2018-05-31 16:43:39 +03003457 if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_COUNT) {
Mark Bloch171c7625b2018-10-03 00:03:35 +00003458 struct mlx5_ib_mcounters *mcounters;
3459
Raed Salem3b3233f2018-05-31 16:43:39 +03003460 err = flow_counters_set_data(flow_act.counters, ucmd);
3461 if (err)
3462 goto free;
3463
Mark Bloch171c7625b2018-10-03 00:03:35 +00003464 mcounters = to_mcounters(flow_act.counters);
Raed Salem3b3233f2018-05-31 16:43:39 +03003465 handler->ibcounters = flow_act.counters;
3466 dest_arr[dest_num].type =
3467 MLX5_FLOW_DESTINATION_TYPE_COUNTER;
Mark Bloch171c7625b2018-10-03 00:03:35 +00003468 dest_arr[dest_num].counter_id =
3469 mlx5_fc_id(mcounters->hw_cntrs_hndl);
Raed Salem3b3233f2018-05-31 16:43:39 +03003470 dest_num++;
3471 }
3472
Boris Pismenny075572d2017-08-16 09:33:30 +03003473 if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_DROP) {
Raed Salem3b3233f2018-05-31 16:43:39 +03003474 if (!(flow_act.action & MLX5_FLOW_CONTEXT_ACTION_COUNT)) {
3475 rule_dst = NULL;
3476 dest_num = 0;
3477 }
Slava Shwartsmana22ed862017-04-03 13:13:52 +03003478 } else {
Aviad Yehezkel802c2122018-03-28 09:27:53 +03003479 if (is_egress)
3480 flow_act.action |= MLX5_FLOW_CONTEXT_ACTION_ALLOW;
3481 else
3482 flow_act.action |=
Raed Salem3b3233f2018-05-31 16:43:39 +03003483 dest_num ? MLX5_FLOW_CONTEXT_ACTION_FWD_DEST :
Aviad Yehezkel802c2122018-03-28 09:27:53 +03003484 MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO;
Slava Shwartsmana22ed862017-04-03 13:13:52 +03003485 }
Moses Reuben2ac693f2017-01-18 14:59:50 +02003486
Paul Blakeyd5634fe2018-09-20 12:17:48 +02003487 if ((flow_act.flags & FLOW_ACT_HAS_TAG) &&
Moses Reuben2ac693f2017-01-18 14:59:50 +02003488 (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
3489 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT)) {
3490 mlx5_ib_warn(dev, "Flow tag %u and attribute type %x isn't allowed in leftovers\n",
Boris Pismenny075572d2017-08-16 09:33:30 +03003491 flow_act.flow_tag, flow_attr->type);
Moses Reuben2ac693f2017-01-18 14:59:50 +02003492 err = -EINVAL;
3493 goto free;
3494 }
Mark Bloch74491de2016-08-31 11:24:25 +00003495 handler->rule = mlx5_add_flow_rules(ft, spec,
Hadar Hen Zion66958ed2016-11-07 15:14:45 +02003496 &flow_act,
Slava Shwartsmana22ed862017-04-03 13:13:52 +03003497 rule_dst, dest_num);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003498
3499 if (IS_ERR(handler->rule)) {
3500 err = PTR_ERR(handler->rule);
3501 goto free;
3502 }
3503
Maor Gottliebd9d49802016-08-28 14:16:33 +03003504 ft_prio->refcount++;
Maor Gottlieb5497adc2016-08-28 14:16:31 +03003505 handler->prio = ft_prio;
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003506 handler->dev = dev;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003507
3508 ft_prio->flow_table = ft;
3509free:
Raed Salem3b3233f2018-05-31 16:43:39 +03003510 if (err && handler) {
3511 if (handler->ibcounters &&
3512 atomic_read(&handler->ibcounters->usecnt) == 1)
3513 counters_clear_description(handler->ibcounters);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003514 kfree(handler);
Raed Salem3b3233f2018-05-31 16:43:39 +03003515 }
Maor Gottliebc5bb1732016-07-04 17:23:05 +03003516 kvfree(spec);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003517 return err ? ERR_PTR(err) : handler;
3518}
3519
Yishai Hadasa550ddf2017-08-17 15:52:33 +03003520static struct mlx5_ib_flow_handler *create_flow_rule(struct mlx5_ib_dev *dev,
3521 struct mlx5_ib_flow_prio *ft_prio,
3522 const struct ib_flow_attr *flow_attr,
3523 struct mlx5_flow_destination *dst)
3524{
Raed Salem3b3233f2018-05-31 16:43:39 +03003525 return _create_flow_rule(dev, ft_prio, flow_attr, dst, 0, NULL);
Yishai Hadasa550ddf2017-08-17 15:52:33 +03003526}
3527
Maor Gottlieb35d190112016-03-07 18:51:47 +02003528static struct mlx5_ib_flow_handler *create_dont_trap_rule(struct mlx5_ib_dev *dev,
3529 struct mlx5_ib_flow_prio *ft_prio,
3530 struct ib_flow_attr *flow_attr,
3531 struct mlx5_flow_destination *dst)
3532{
3533 struct mlx5_ib_flow_handler *handler_dst = NULL;
3534 struct mlx5_ib_flow_handler *handler = NULL;
3535
3536 handler = create_flow_rule(dev, ft_prio, flow_attr, NULL);
3537 if (!IS_ERR(handler)) {
3538 handler_dst = create_flow_rule(dev, ft_prio,
3539 flow_attr, dst);
3540 if (IS_ERR(handler_dst)) {
Mark Bloch74491de2016-08-31 11:24:25 +00003541 mlx5_del_flow_rules(handler->rule);
Maor Gottliebd9d49802016-08-28 14:16:33 +03003542 ft_prio->refcount--;
Maor Gottlieb35d190112016-03-07 18:51:47 +02003543 kfree(handler);
3544 handler = handler_dst;
3545 } else {
3546 list_add(&handler_dst->list, &handler->list);
3547 }
3548 }
3549
3550 return handler;
3551}
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003552enum {
3553 LEFTOVERS_MC,
3554 LEFTOVERS_UC,
3555};
3556
3557static struct mlx5_ib_flow_handler *create_leftovers_rule(struct mlx5_ib_dev *dev,
3558 struct mlx5_ib_flow_prio *ft_prio,
3559 struct ib_flow_attr *flow_attr,
3560 struct mlx5_flow_destination *dst)
3561{
3562 struct mlx5_ib_flow_handler *handler_ucast = NULL;
3563 struct mlx5_ib_flow_handler *handler = NULL;
3564
3565 static struct {
3566 struct ib_flow_attr flow_attr;
3567 struct ib_flow_spec_eth eth_flow;
3568 } leftovers_specs[] = {
3569 [LEFTOVERS_MC] = {
3570 .flow_attr = {
3571 .num_of_specs = 1,
3572 .size = sizeof(leftovers_specs[0])
3573 },
3574 .eth_flow = {
3575 .type = IB_FLOW_SPEC_ETH,
3576 .size = sizeof(struct ib_flow_spec_eth),
3577 .mask = {.dst_mac = {0x1} },
3578 .val = {.dst_mac = {0x1} }
3579 }
3580 },
3581 [LEFTOVERS_UC] = {
3582 .flow_attr = {
3583 .num_of_specs = 1,
3584 .size = sizeof(leftovers_specs[0])
3585 },
3586 .eth_flow = {
3587 .type = IB_FLOW_SPEC_ETH,
3588 .size = sizeof(struct ib_flow_spec_eth),
3589 .mask = {.dst_mac = {0x1} },
3590 .val = {.dst_mac = {} }
3591 }
3592 }
3593 };
3594
3595 handler = create_flow_rule(dev, ft_prio,
3596 &leftovers_specs[LEFTOVERS_MC].flow_attr,
3597 dst);
3598 if (!IS_ERR(handler) &&
3599 flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT) {
3600 handler_ucast = create_flow_rule(dev, ft_prio,
3601 &leftovers_specs[LEFTOVERS_UC].flow_attr,
3602 dst);
3603 if (IS_ERR(handler_ucast)) {
Mark Bloch74491de2016-08-31 11:24:25 +00003604 mlx5_del_flow_rules(handler->rule);
Maor Gottliebd9d49802016-08-28 14:16:33 +03003605 ft_prio->refcount--;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003606 kfree(handler);
3607 handler = handler_ucast;
3608 } else {
3609 list_add(&handler_ucast->list, &handler->list);
3610 }
3611 }
3612
3613 return handler;
3614}
3615
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003616static struct mlx5_ib_flow_handler *create_sniffer_rule(struct mlx5_ib_dev *dev,
3617 struct mlx5_ib_flow_prio *ft_rx,
3618 struct mlx5_ib_flow_prio *ft_tx,
3619 struct mlx5_flow_destination *dst)
3620{
3621 struct mlx5_ib_flow_handler *handler_rx;
3622 struct mlx5_ib_flow_handler *handler_tx;
3623 int err;
3624 static const struct ib_flow_attr flow_attr = {
3625 .num_of_specs = 0,
3626 .size = sizeof(flow_attr)
3627 };
3628
3629 handler_rx = create_flow_rule(dev, ft_rx, &flow_attr, dst);
3630 if (IS_ERR(handler_rx)) {
3631 err = PTR_ERR(handler_rx);
3632 goto err;
3633 }
3634
3635 handler_tx = create_flow_rule(dev, ft_tx, &flow_attr, dst);
3636 if (IS_ERR(handler_tx)) {
3637 err = PTR_ERR(handler_tx);
3638 goto err_tx;
3639 }
3640
3641 list_add(&handler_tx->list, &handler_rx->list);
3642
3643 return handler_rx;
3644
3645err_tx:
Mark Bloch74491de2016-08-31 11:24:25 +00003646 mlx5_del_flow_rules(handler_rx->rule);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003647 ft_rx->refcount--;
3648 kfree(handler_rx);
3649err:
3650 return ERR_PTR(err);
3651}
3652
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003653static struct ib_flow *mlx5_ib_create_flow(struct ib_qp *qp,
3654 struct ib_flow_attr *flow_attr,
Matan Barak59082a32018-05-31 16:43:35 +03003655 int domain,
3656 struct ib_udata *udata)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003657{
3658 struct mlx5_ib_dev *dev = to_mdev(qp->device);
Yishai Hadasd9f88e52016-08-28 10:58:37 +03003659 struct mlx5_ib_qp *mqp = to_mqp(qp);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003660 struct mlx5_ib_flow_handler *handler = NULL;
3661 struct mlx5_flow_destination *dst = NULL;
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003662 struct mlx5_ib_flow_prio *ft_prio_tx = NULL;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003663 struct mlx5_ib_flow_prio *ft_prio;
Aviad Yehezkel802c2122018-03-28 09:27:53 +03003664 bool is_egress = flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS;
Raed Salem3b3233f2018-05-31 16:43:39 +03003665 struct mlx5_ib_create_flow *ucmd = NULL, ucmd_hdr;
3666 size_t min_ucmd_sz, required_ucmd_sz;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003667 int err;
Yishai Hadasa550ddf2017-08-17 15:52:33 +03003668 int underlay_qpn;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003669
Raed Salem3b3233f2018-05-31 16:43:39 +03003670 if (udata && udata->inlen) {
3671 min_ucmd_sz = offsetof(typeof(ucmd_hdr), reserved) +
3672 sizeof(ucmd_hdr.reserved);
3673 if (udata->inlen < min_ucmd_sz)
3674 return ERR_PTR(-EOPNOTSUPP);
3675
3676 err = ib_copy_from_udata(&ucmd_hdr, udata, min_ucmd_sz);
3677 if (err)
3678 return ERR_PTR(err);
3679
3680 /* currently supports only one counters data */
3681 if (ucmd_hdr.ncounters_data > 1)
3682 return ERR_PTR(-EINVAL);
3683
3684 required_ucmd_sz = min_ucmd_sz +
3685 sizeof(struct mlx5_ib_flow_counters_data) *
3686 ucmd_hdr.ncounters_data;
3687 if (udata->inlen > required_ucmd_sz &&
3688 !ib_is_udata_cleared(udata, required_ucmd_sz,
3689 udata->inlen - required_ucmd_sz))
3690 return ERR_PTR(-EOPNOTSUPP);
3691
3692 ucmd = kzalloc(required_ucmd_sz, GFP_KERNEL);
3693 if (!ucmd)
3694 return ERR_PTR(-ENOMEM);
3695
3696 err = ib_copy_from_udata(ucmd, udata, required_ucmd_sz);
Gustavo A. R. Silva299eafe2018-06-07 14:19:15 -05003697 if (err)
3698 goto free_ucmd;
Raed Salem3b3233f2018-05-31 16:43:39 +03003699 }
Matan Barak59082a32018-05-31 16:43:35 +03003700
Gustavo A. R. Silva299eafe2018-06-07 14:19:15 -05003701 if (flow_attr->priority > MLX5_IB_FLOW_LAST_PRIO) {
3702 err = -ENOMEM;
3703 goto free_ucmd;
3704 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003705
3706 if (domain != IB_FLOW_DOMAIN_USER ||
Daniel Jurgens508562d2018-01-04 17:25:34 +02003707 flow_attr->port > dev->num_ports ||
Aviad Yehezkel802c2122018-03-28 09:27:53 +03003708 (flow_attr->flags & ~(IB_FLOW_ATTR_FLAGS_DONT_TRAP |
Gustavo A. R. Silva299eafe2018-06-07 14:19:15 -05003709 IB_FLOW_ATTR_FLAGS_EGRESS))) {
3710 err = -EINVAL;
3711 goto free_ucmd;
3712 }
Aviad Yehezkel802c2122018-03-28 09:27:53 +03003713
3714 if (is_egress &&
3715 (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
Gustavo A. R. Silva299eafe2018-06-07 14:19:15 -05003716 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT)) {
3717 err = -EINVAL;
3718 goto free_ucmd;
3719 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003720
3721 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
Gustavo A. R. Silva299eafe2018-06-07 14:19:15 -05003722 if (!dst) {
3723 err = -ENOMEM;
3724 goto free_ucmd;
3725 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003726
Mark Bloch9a4ca382018-01-16 14:42:35 +00003727 mutex_lock(&dev->flow_db->lock);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003728
Aviad Yehezkel802c2122018-03-28 09:27:53 +03003729 ft_prio = get_flow_table(dev, flow_attr,
3730 is_egress ? MLX5_IB_FT_TX : MLX5_IB_FT_RX);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003731 if (IS_ERR(ft_prio)) {
3732 err = PTR_ERR(ft_prio);
3733 goto unlock;
3734 }
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003735 if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
3736 ft_prio_tx = get_flow_table(dev, flow_attr, MLX5_IB_FT_TX);
3737 if (IS_ERR(ft_prio_tx)) {
3738 err = PTR_ERR(ft_prio_tx);
3739 ft_prio_tx = NULL;
3740 goto destroy_ft;
3741 }
3742 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003743
Aviad Yehezkel802c2122018-03-28 09:27:53 +03003744 if (is_egress) {
3745 dst->type = MLX5_FLOW_DESTINATION_TYPE_PORT;
3746 } else {
3747 dst->type = MLX5_FLOW_DESTINATION_TYPE_TIR;
3748 if (mqp->flags & MLX5_IB_QP_RSS)
3749 dst->tir_num = mqp->rss_qp.tirn;
3750 else
3751 dst->tir_num = mqp->raw_packet_qp.rq.tirn;
3752 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003753
3754 if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
Maor Gottlieb35d190112016-03-07 18:51:47 +02003755 if (flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP) {
3756 handler = create_dont_trap_rule(dev, ft_prio,
3757 flow_attr, dst);
3758 } else {
Yishai Hadasa550ddf2017-08-17 15:52:33 +03003759 underlay_qpn = (mqp->flags & MLX5_IB_QP_UNDERLAY) ?
3760 mqp->underlay_qpn : 0;
3761 handler = _create_flow_rule(dev, ft_prio, flow_attr,
Raed Salem3b3233f2018-05-31 16:43:39 +03003762 dst, underlay_qpn, ucmd);
Maor Gottlieb35d190112016-03-07 18:51:47 +02003763 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003764 } else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
3765 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
3766 handler = create_leftovers_rule(dev, ft_prio, flow_attr,
3767 dst);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003768 } else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
3769 handler = create_sniffer_rule(dev, ft_prio, ft_prio_tx, dst);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003770 } else {
3771 err = -EINVAL;
3772 goto destroy_ft;
3773 }
3774
3775 if (IS_ERR(handler)) {
3776 err = PTR_ERR(handler);
3777 handler = NULL;
3778 goto destroy_ft;
3779 }
3780
Mark Bloch9a4ca382018-01-16 14:42:35 +00003781 mutex_unlock(&dev->flow_db->lock);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003782 kfree(dst);
Raed Salem3b3233f2018-05-31 16:43:39 +03003783 kfree(ucmd);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003784
3785 return &handler->ibflow;
3786
3787destroy_ft:
3788 put_flow_table(dev, ft_prio, false);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003789 if (ft_prio_tx)
3790 put_flow_table(dev, ft_prio_tx, false);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003791unlock:
Mark Bloch9a4ca382018-01-16 14:42:35 +00003792 mutex_unlock(&dev->flow_db->lock);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003793 kfree(dst);
Gustavo A. R. Silva299eafe2018-06-07 14:19:15 -05003794free_ucmd:
Raed Salem3b3233f2018-05-31 16:43:39 +03003795 kfree(ucmd);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003796 return ERR_PTR(err);
3797}
3798
Mark Blochb47fd4f2018-09-06 17:27:07 +03003799static struct mlx5_ib_flow_prio *
3800_get_flow_table(struct mlx5_ib_dev *dev,
3801 struct mlx5_ib_flow_matcher *fs_matcher,
3802 bool mcast)
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003803{
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003804 struct mlx5_flow_namespace *ns = NULL;
3805 struct mlx5_ib_flow_prio *prio;
Mark Blochb47fd4f2018-09-06 17:27:07 +03003806 int max_table_size;
3807 u32 flags = 0;
3808 int priority;
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003809
Mark Blochb47fd4f2018-09-06 17:27:07 +03003810 if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_BYPASS) {
3811 max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
3812 log_max_ft_size));
3813 if (MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, decap))
3814 flags |= MLX5_FLOW_TABLE_TUNNEL_EN_DECAP;
3815 if (MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
3816 reformat_l3_tunnel_to_l2))
3817 flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
3818 } else { /* Can only be MLX5_FLOW_NAMESPACE_EGRESS */
3819 max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev,
3820 log_max_ft_size));
3821 if (MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev, reformat))
3822 flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
3823 }
3824
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003825 if (max_table_size < MLX5_FS_MAX_ENTRIES)
3826 return ERR_PTR(-ENOMEM);
3827
3828 if (mcast)
3829 priority = MLX5_IB_FLOW_MCAST_PRIO;
3830 else
Mark Blochb47fd4f2018-09-06 17:27:07 +03003831 priority = ib_prio_to_core_prio(fs_matcher->priority, false);
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003832
Mark Blochb47fd4f2018-09-06 17:27:07 +03003833 ns = mlx5_get_flow_namespace(dev->mdev, fs_matcher->ns_type);
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003834 if (!ns)
3835 return ERR_PTR(-ENOTSUPP);
3836
Mark Blochb47fd4f2018-09-06 17:27:07 +03003837 if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_BYPASS)
3838 prio = &dev->flow_db->prios[priority];
3839 else
3840 prio = &dev->flow_db->egress_prios[priority];
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003841
3842 if (prio->flow_table)
3843 return prio;
3844
3845 return _get_prio(ns, prio, priority, MLX5_FS_MAX_ENTRIES,
Mark Blochb47fd4f2018-09-06 17:27:07 +03003846 MLX5_FS_MAX_TYPES, flags);
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003847}
3848
3849static struct mlx5_ib_flow_handler *
3850_create_raw_flow_rule(struct mlx5_ib_dev *dev,
3851 struct mlx5_ib_flow_prio *ft_prio,
3852 struct mlx5_flow_destination *dst,
3853 struct mlx5_ib_flow_matcher *fs_matcher,
Mark Blochb823dd62018-09-06 17:27:05 +03003854 struct mlx5_flow_act *flow_act,
Mark Blochbfc5d832018-11-20 20:31:08 +02003855 void *cmd_in, int inlen,
3856 int dst_num)
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003857{
3858 struct mlx5_ib_flow_handler *handler;
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003859 struct mlx5_flow_spec *spec;
3860 struct mlx5_flow_table *ft = ft_prio->flow_table;
3861 int err = 0;
3862
3863 spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
3864 handler = kzalloc(sizeof(*handler), GFP_KERNEL);
3865 if (!handler || !spec) {
3866 err = -ENOMEM;
3867 goto free;
3868 }
3869
3870 INIT_LIST_HEAD(&handler->list);
3871
3872 memcpy(spec->match_value, cmd_in, inlen);
3873 memcpy(spec->match_criteria, fs_matcher->matcher_mask.match_params,
3874 fs_matcher->mask_len);
3875 spec->match_criteria_enable = fs_matcher->match_criteria_enable;
3876
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003877 handler->rule = mlx5_add_flow_rules(ft, spec,
Mark Blochbfc5d832018-11-20 20:31:08 +02003878 flow_act, dst, dst_num);
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003879
3880 if (IS_ERR(handler->rule)) {
3881 err = PTR_ERR(handler->rule);
3882 goto free;
3883 }
3884
3885 ft_prio->refcount++;
3886 handler->prio = ft_prio;
3887 handler->dev = dev;
3888 ft_prio->flow_table = ft;
3889
3890free:
3891 if (err)
3892 kfree(handler);
3893 kvfree(spec);
3894 return err ? ERR_PTR(err) : handler;
3895}
3896
3897static bool raw_fs_is_multicast(struct mlx5_ib_flow_matcher *fs_matcher,
3898 void *match_v)
3899{
3900 void *match_c;
3901 void *match_v_set_lyr_2_4, *match_c_set_lyr_2_4;
3902 void *dmac, *dmac_mask;
3903 void *ipv4, *ipv4_mask;
3904
3905 if (!(fs_matcher->match_criteria_enable &
3906 (1 << MATCH_CRITERIA_ENABLE_OUTER_BIT)))
3907 return false;
3908
3909 match_c = fs_matcher->matcher_mask.match_params;
3910 match_v_set_lyr_2_4 = MLX5_ADDR_OF(fte_match_param, match_v,
3911 outer_headers);
3912 match_c_set_lyr_2_4 = MLX5_ADDR_OF(fte_match_param, match_c,
3913 outer_headers);
3914
3915 dmac = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_v_set_lyr_2_4,
3916 dmac_47_16);
3917 dmac_mask = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_c_set_lyr_2_4,
3918 dmac_47_16);
3919
3920 if (is_multicast_ether_addr(dmac) &&
3921 is_multicast_ether_addr(dmac_mask))
3922 return true;
3923
3924 ipv4 = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_v_set_lyr_2_4,
3925 dst_ipv4_dst_ipv6.ipv4_layout.ipv4);
3926
3927 ipv4_mask = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_c_set_lyr_2_4,
3928 dst_ipv4_dst_ipv6.ipv4_layout.ipv4);
3929
3930 if (ipv4_is_multicast(*(__be32 *)(ipv4)) &&
3931 ipv4_is_multicast(*(__be32 *)(ipv4_mask)))
3932 return true;
3933
3934 return false;
3935}
3936
Yishai Hadas32269442018-07-23 15:25:09 +03003937struct mlx5_ib_flow_handler *
3938mlx5_ib_raw_fs_rule_add(struct mlx5_ib_dev *dev,
3939 struct mlx5_ib_flow_matcher *fs_matcher,
Mark Blochb823dd62018-09-06 17:27:05 +03003940 struct mlx5_flow_act *flow_act,
Mark Blochbfc5d832018-11-20 20:31:08 +02003941 u32 counter_id,
Yishai Hadas32269442018-07-23 15:25:09 +03003942 void *cmd_in, int inlen, int dest_id,
3943 int dest_type)
3944{
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003945 struct mlx5_flow_destination *dst;
3946 struct mlx5_ib_flow_prio *ft_prio;
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003947 struct mlx5_ib_flow_handler *handler;
Mark Blochbfc5d832018-11-20 20:31:08 +02003948 int dst_num = 0;
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003949 bool mcast;
3950 int err;
3951
3952 if (fs_matcher->flow_type != MLX5_IB_FLOW_TYPE_NORMAL)
3953 return ERR_PTR(-EOPNOTSUPP);
3954
3955 if (fs_matcher->priority > MLX5_IB_FLOW_LAST_PRIO)
3956 return ERR_PTR(-ENOMEM);
3957
Gustavo A. R. Silva8e8aa142019-01-15 00:00:48 -06003958 dst = kcalloc(2, sizeof(*dst), GFP_KERNEL);
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003959 if (!dst)
3960 return ERR_PTR(-ENOMEM);
3961
3962 mcast = raw_fs_is_multicast(fs_matcher, cmd_in);
3963 mutex_lock(&dev->flow_db->lock);
3964
Mark Blochb47fd4f2018-09-06 17:27:07 +03003965 ft_prio = _get_flow_table(dev, fs_matcher, mcast);
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003966 if (IS_ERR(ft_prio)) {
3967 err = PTR_ERR(ft_prio);
3968 goto unlock;
3969 }
3970
Yishai Hadas6346f0b2018-07-23 15:25:11 +03003971 if (dest_type == MLX5_FLOW_DESTINATION_TYPE_TIR) {
Mark Blochbfc5d832018-11-20 20:31:08 +02003972 dst[dst_num].type = dest_type;
3973 dst[dst_num].tir_num = dest_id;
Mark Blochb823dd62018-09-06 17:27:05 +03003974 flow_act->action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
Mark Blocha7ee18b2018-09-06 17:27:08 +03003975 } else if (dest_type == MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE) {
Mark Blochbfc5d832018-11-20 20:31:08 +02003976 dst[dst_num].type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE_NUM;
3977 dst[dst_num].ft_num = dest_id;
Mark Blochb823dd62018-09-06 17:27:05 +03003978 flow_act->action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
Mark Blocha7ee18b2018-09-06 17:27:08 +03003979 } else {
Mark Blochbfc5d832018-11-20 20:31:08 +02003980 dst[dst_num].type = MLX5_FLOW_DESTINATION_TYPE_PORT;
Mark Blocha7ee18b2018-09-06 17:27:08 +03003981 flow_act->action |= MLX5_FLOW_CONTEXT_ACTION_ALLOW;
Yishai Hadas6346f0b2018-07-23 15:25:11 +03003982 }
3983
Mark Blochbfc5d832018-11-20 20:31:08 +02003984 dst_num++;
3985
3986 if (flow_act->action & MLX5_FLOW_CONTEXT_ACTION_COUNT) {
3987 dst[dst_num].type = MLX5_FLOW_DESTINATION_TYPE_COUNTER;
3988 dst[dst_num].counter_id = counter_id;
3989 dst_num++;
3990 }
3991
Mark Blochb823dd62018-09-06 17:27:05 +03003992 handler = _create_raw_flow_rule(dev, ft_prio, dst, fs_matcher, flow_act,
Mark Blochbfc5d832018-11-20 20:31:08 +02003993 cmd_in, inlen, dst_num);
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003994
3995 if (IS_ERR(handler)) {
3996 err = PTR_ERR(handler);
3997 goto destroy_ft;
3998 }
3999
4000 mutex_unlock(&dev->flow_db->lock);
4001 atomic_inc(&fs_matcher->usecnt);
4002 handler->flow_matcher = fs_matcher;
4003
4004 kfree(dst);
4005
4006 return handler;
4007
4008destroy_ft:
4009 put_flow_table(dev, ft_prio, false);
4010unlock:
4011 mutex_unlock(&dev->flow_db->lock);
4012 kfree(dst);
4013
4014 return ERR_PTR(err);
Yishai Hadas32269442018-07-23 15:25:09 +03004015}
4016
Aviad Yehezkelc6475a02018-03-28 09:27:50 +03004017static u32 mlx5_ib_flow_action_flags_to_accel_xfrm_flags(u32 mlx5_flags)
4018{
4019 u32 flags = 0;
4020
4021 if (mlx5_flags & MLX5_IB_UAPI_FLOW_ACTION_FLAGS_REQUIRE_METADATA)
4022 flags |= MLX5_ACCEL_XFRM_FLAG_REQUIRE_METADATA;
4023
4024 return flags;
4025}
4026
4027#define MLX5_FLOW_ACTION_ESP_CREATE_LAST_SUPPORTED MLX5_IB_UAPI_FLOW_ACTION_FLAGS_REQUIRE_METADATA
4028static struct ib_flow_action *
4029mlx5_ib_create_flow_action_esp(struct ib_device *device,
4030 const struct ib_flow_action_attrs_esp *attr,
4031 struct uverbs_attr_bundle *attrs)
4032{
4033 struct mlx5_ib_dev *mdev = to_mdev(device);
4034 struct ib_uverbs_flow_action_esp_keymat_aes_gcm *aes_gcm;
4035 struct mlx5_accel_esp_xfrm_attrs accel_attrs = {};
4036 struct mlx5_ib_flow_action *action;
4037 u64 action_flags;
4038 u64 flags;
4039 int err = 0;
4040
Jason Gunthorpebccd0622018-07-26 16:37:14 -06004041 err = uverbs_get_flags64(
4042 &action_flags, attrs, MLX5_IB_ATTR_CREATE_FLOW_ACTION_FLAGS,
4043 ((MLX5_FLOW_ACTION_ESP_CREATE_LAST_SUPPORTED << 1) - 1));
4044 if (err)
4045 return ERR_PTR(err);
Aviad Yehezkelc6475a02018-03-28 09:27:50 +03004046
4047 flags = mlx5_ib_flow_action_flags_to_accel_xfrm_flags(action_flags);
4048
4049 /* We current only support a subset of the standard features. Only a
4050 * keymat of type AES_GCM, with icv_len == 16, iv_algo == SEQ and esn
4051 * (with overlap). Full offload mode isn't supported.
4052 */
4053 if (!attr->keymat || attr->replay || attr->encap ||
4054 attr->spi || attr->seq || attr->tfc_pad ||
4055 attr->hard_limit_pkts ||
4056 (attr->flags & ~(IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED |
4057 IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ENCRYPT)))
4058 return ERR_PTR(-EOPNOTSUPP);
4059
4060 if (attr->keymat->protocol !=
4061 IB_UVERBS_FLOW_ACTION_ESP_KEYMAT_AES_GCM)
4062 return ERR_PTR(-EOPNOTSUPP);
4063
4064 aes_gcm = &attr->keymat->keymat.aes_gcm;
4065
4066 if (aes_gcm->icv_len != 16 ||
4067 aes_gcm->iv_algo != IB_UVERBS_FLOW_ACTION_IV_ALGO_SEQ)
4068 return ERR_PTR(-EOPNOTSUPP);
4069
4070 action = kmalloc(sizeof(*action), GFP_KERNEL);
4071 if (!action)
4072 return ERR_PTR(-ENOMEM);
4073
4074 action->esp_aes_gcm.ib_flags = attr->flags;
4075 memcpy(&accel_attrs.keymat.aes_gcm.aes_key, &aes_gcm->aes_key,
4076 sizeof(accel_attrs.keymat.aes_gcm.aes_key));
4077 accel_attrs.keymat.aes_gcm.key_len = aes_gcm->key_len * 8;
4078 memcpy(&accel_attrs.keymat.aes_gcm.salt, &aes_gcm->salt,
4079 sizeof(accel_attrs.keymat.aes_gcm.salt));
4080 memcpy(&accel_attrs.keymat.aes_gcm.seq_iv, &aes_gcm->iv,
4081 sizeof(accel_attrs.keymat.aes_gcm.seq_iv));
4082 accel_attrs.keymat.aes_gcm.icv_len = aes_gcm->icv_len * 8;
4083 accel_attrs.keymat.aes_gcm.iv_algo = MLX5_ACCEL_ESP_AES_GCM_IV_ALGO_SEQ;
4084 accel_attrs.keymat_type = MLX5_ACCEL_ESP_KEYMAT_AES_GCM;
4085
4086 accel_attrs.esn = attr->esn;
4087 if (attr->flags & IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED)
4088 accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_TRIGGERED;
4089 if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW)
4090 accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP;
4091
4092 if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ENCRYPT)
4093 accel_attrs.action |= MLX5_ACCEL_ESP_ACTION_ENCRYPT;
4094
4095 action->esp_aes_gcm.ctx =
4096 mlx5_accel_esp_create_xfrm(mdev->mdev, &accel_attrs, flags);
4097 if (IS_ERR(action->esp_aes_gcm.ctx)) {
4098 err = PTR_ERR(action->esp_aes_gcm.ctx);
4099 goto err_parse;
4100 }
4101
4102 action->esp_aes_gcm.ib_flags = attr->flags;
4103
4104 return &action->ib_action;
4105
4106err_parse:
4107 kfree(action);
4108 return ERR_PTR(err);
4109}
4110
Matan Barak349705c2018-03-28 09:27:51 +03004111static int
4112mlx5_ib_modify_flow_action_esp(struct ib_flow_action *action,
4113 const struct ib_flow_action_attrs_esp *attr,
4114 struct uverbs_attr_bundle *attrs)
4115{
4116 struct mlx5_ib_flow_action *maction = to_mflow_act(action);
4117 struct mlx5_accel_esp_xfrm_attrs accel_attrs;
4118 int err = 0;
4119
4120 if (attr->keymat || attr->replay || attr->encap ||
4121 attr->spi || attr->seq || attr->tfc_pad ||
4122 attr->hard_limit_pkts ||
4123 (attr->flags & ~(IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED |
4124 IB_FLOW_ACTION_ESP_FLAGS_MOD_ESP_ATTRS |
4125 IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW)))
4126 return -EOPNOTSUPP;
4127
4128 /* Only the ESN value or the MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP can
4129 * be modified.
4130 */
4131 if (!(maction->esp_aes_gcm.ib_flags &
4132 IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED) &&
4133 attr->flags & (IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED |
4134 IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW))
4135 return -EINVAL;
4136
4137 memcpy(&accel_attrs, &maction->esp_aes_gcm.ctx->attrs,
4138 sizeof(accel_attrs));
4139
4140 accel_attrs.esn = attr->esn;
4141 if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW)
4142 accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP;
4143 else
4144 accel_attrs.flags &= ~MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP;
4145
4146 err = mlx5_accel_esp_modify_xfrm(maction->esp_aes_gcm.ctx,
4147 &accel_attrs);
4148 if (err)
4149 return err;
4150
4151 maction->esp_aes_gcm.ib_flags &=
4152 ~IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW;
4153 maction->esp_aes_gcm.ib_flags |=
4154 attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW;
4155
4156 return 0;
4157}
4158
Aviad Yehezkelc6475a02018-03-28 09:27:50 +03004159static int mlx5_ib_destroy_flow_action(struct ib_flow_action *action)
4160{
4161 struct mlx5_ib_flow_action *maction = to_mflow_act(action);
4162
4163 switch (action->type) {
4164 case IB_FLOW_ACTION_ESP:
4165 /*
4166 * We only support aes_gcm by now, so we implicitly know this is
4167 * the underline crypto.
4168 */
4169 mlx5_accel_esp_destroy_xfrm(maction->esp_aes_gcm.ctx);
4170 break;
Mark Blochb4749bf2018-08-28 14:18:51 +03004171 case IB_FLOW_ACTION_UNSPECIFIED:
4172 mlx5_ib_destroy_flow_action_raw(maction);
4173 break;
Aviad Yehezkelc6475a02018-03-28 09:27:50 +03004174 default:
4175 WARN_ON(true);
4176 break;
4177 }
4178
4179 kfree(maction);
4180 return 0;
4181}
4182
Eli Cohene126ba92013-07-07 17:25:49 +03004183static int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
4184{
4185 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
Yishai Hadas81e30882017-06-08 16:15:09 +03004186 struct mlx5_ib_qp *mqp = to_mqp(ibqp);
Eli Cohene126ba92013-07-07 17:25:49 +03004187 int err;
Yishai Hadas539ec982018-09-20 21:39:25 +03004188 u16 uid;
4189
4190 uid = ibqp->pd ?
4191 to_mpd(ibqp->pd)->uid : 0;
Eli Cohene126ba92013-07-07 17:25:49 +03004192
Yishai Hadas81e30882017-06-08 16:15:09 +03004193 if (mqp->flags & MLX5_IB_QP_UNDERLAY) {
4194 mlx5_ib_dbg(dev, "Attaching a multi cast group to underlay QP is not supported\n");
4195 return -EOPNOTSUPP;
4196 }
4197
Yishai Hadas539ec982018-09-20 21:39:25 +03004198 err = mlx5_cmd_attach_mcg(dev->mdev, gid, ibqp->qp_num, uid);
Eli Cohene126ba92013-07-07 17:25:49 +03004199 if (err)
4200 mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n",
4201 ibqp->qp_num, gid->raw);
4202
4203 return err;
4204}
4205
4206static int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
4207{
4208 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
4209 int err;
Yishai Hadas539ec982018-09-20 21:39:25 +03004210 u16 uid;
Eli Cohene126ba92013-07-07 17:25:49 +03004211
Yishai Hadas539ec982018-09-20 21:39:25 +03004212 uid = ibqp->pd ?
4213 to_mpd(ibqp->pd)->uid : 0;
4214 err = mlx5_cmd_detach_mcg(dev->mdev, gid, ibqp->qp_num, uid);
Eli Cohene126ba92013-07-07 17:25:49 +03004215 if (err)
4216 mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n",
4217 ibqp->qp_num, gid->raw);
4218
4219 return err;
4220}
4221
4222static int init_node_data(struct mlx5_ib_dev *dev)
4223{
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03004224 int err;
Eli Cohene126ba92013-07-07 17:25:49 +03004225
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03004226 err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc);
Eli Cohene126ba92013-07-07 17:25:49 +03004227 if (err)
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03004228 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03004229
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03004230 dev->mdev->rev_id = dev->mdev->pdev->revision;
Eli Cohene126ba92013-07-07 17:25:49 +03004231
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03004232 return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid);
Eli Cohene126ba92013-07-07 17:25:49 +03004233}
4234
Parav Pandit508a5232018-10-11 22:31:54 +03004235static ssize_t fw_pages_show(struct device *device,
4236 struct device_attribute *attr, char *buf)
Eli Cohene126ba92013-07-07 17:25:49 +03004237{
4238 struct mlx5_ib_dev *dev =
Parav Pandit54747232018-12-18 14:15:56 +02004239 rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
Eli Cohene126ba92013-07-07 17:25:49 +03004240
Jack Morgenstein9603b612014-07-28 23:30:22 +03004241 return sprintf(buf, "%d\n", dev->mdev->priv.fw_pages);
Eli Cohene126ba92013-07-07 17:25:49 +03004242}
Parav Pandit508a5232018-10-11 22:31:54 +03004243static DEVICE_ATTR_RO(fw_pages);
Eli Cohene126ba92013-07-07 17:25:49 +03004244
Parav Pandit508a5232018-10-11 22:31:54 +03004245static ssize_t reg_pages_show(struct device *device,
Eli Cohene126ba92013-07-07 17:25:49 +03004246 struct device_attribute *attr, char *buf)
4247{
4248 struct mlx5_ib_dev *dev =
Parav Pandit54747232018-12-18 14:15:56 +02004249 rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
Eli Cohene126ba92013-07-07 17:25:49 +03004250
Haggai Eran6aec21f2014-12-11 17:04:23 +02004251 return sprintf(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages));
Eli Cohene126ba92013-07-07 17:25:49 +03004252}
Parav Pandit508a5232018-10-11 22:31:54 +03004253static DEVICE_ATTR_RO(reg_pages);
Eli Cohene126ba92013-07-07 17:25:49 +03004254
Parav Pandit508a5232018-10-11 22:31:54 +03004255static ssize_t hca_type_show(struct device *device,
4256 struct device_attribute *attr, char *buf)
Eli Cohene126ba92013-07-07 17:25:49 +03004257{
4258 struct mlx5_ib_dev *dev =
Parav Pandit54747232018-12-18 14:15:56 +02004259 rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
4260
Jack Morgenstein9603b612014-07-28 23:30:22 +03004261 return sprintf(buf, "MT%d\n", dev->mdev->pdev->device);
Eli Cohene126ba92013-07-07 17:25:49 +03004262}
Parav Pandit508a5232018-10-11 22:31:54 +03004263static DEVICE_ATTR_RO(hca_type);
Eli Cohene126ba92013-07-07 17:25:49 +03004264
Parav Pandit508a5232018-10-11 22:31:54 +03004265static ssize_t hw_rev_show(struct device *device,
4266 struct device_attribute *attr, char *buf)
Eli Cohene126ba92013-07-07 17:25:49 +03004267{
4268 struct mlx5_ib_dev *dev =
Parav Pandit54747232018-12-18 14:15:56 +02004269 rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
4270
Jack Morgenstein9603b612014-07-28 23:30:22 +03004271 return sprintf(buf, "%x\n", dev->mdev->rev_id);
Eli Cohene126ba92013-07-07 17:25:49 +03004272}
Parav Pandit508a5232018-10-11 22:31:54 +03004273static DEVICE_ATTR_RO(hw_rev);
Eli Cohene126ba92013-07-07 17:25:49 +03004274
Parav Pandit508a5232018-10-11 22:31:54 +03004275static ssize_t board_id_show(struct device *device,
4276 struct device_attribute *attr, char *buf)
Eli Cohene126ba92013-07-07 17:25:49 +03004277{
4278 struct mlx5_ib_dev *dev =
Parav Pandit54747232018-12-18 14:15:56 +02004279 rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
4280
Eli Cohene126ba92013-07-07 17:25:49 +03004281 return sprintf(buf, "%.*s\n", MLX5_BOARD_ID_LEN,
Jack Morgenstein9603b612014-07-28 23:30:22 +03004282 dev->mdev->board_id);
Eli Cohene126ba92013-07-07 17:25:49 +03004283}
Parav Pandit508a5232018-10-11 22:31:54 +03004284static DEVICE_ATTR_RO(board_id);
Eli Cohene126ba92013-07-07 17:25:49 +03004285
Parav Pandit508a5232018-10-11 22:31:54 +03004286static struct attribute *mlx5_class_attributes[] = {
4287 &dev_attr_hw_rev.attr,
4288 &dev_attr_hca_type.attr,
4289 &dev_attr_board_id.attr,
4290 &dev_attr_fw_pages.attr,
4291 &dev_attr_reg_pages.attr,
4292 NULL,
4293};
Eli Cohene126ba92013-07-07 17:25:49 +03004294
Parav Pandit508a5232018-10-11 22:31:54 +03004295static const struct attribute_group mlx5_attr_group = {
4296 .attrs = mlx5_class_attributes,
Eli Cohene126ba92013-07-07 17:25:49 +03004297};
4298
Haggai Eran7722f472016-02-29 15:45:07 +02004299static void pkey_change_handler(struct work_struct *work)
4300{
4301 struct mlx5_ib_port_resources *ports =
4302 container_of(work, struct mlx5_ib_port_resources,
4303 pkey_change_work);
4304
4305 mutex_lock(&ports->devr->mutex);
4306 mlx5_ib_gsi_pkey_change(ports->gsi);
4307 mutex_unlock(&ports->devr->mutex);
4308}
4309
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03004310static void mlx5_ib_handle_internal_error(struct mlx5_ib_dev *ibdev)
4311{
4312 struct mlx5_ib_qp *mqp;
4313 struct mlx5_ib_cq *send_mcq, *recv_mcq;
4314 struct mlx5_core_cq *mcq;
4315 struct list_head cq_armed_list;
4316 unsigned long flags_qp;
4317 unsigned long flags_cq;
4318 unsigned long flags;
4319
4320 INIT_LIST_HEAD(&cq_armed_list);
4321
4322 /* Go over qp list reside on that ibdev, sync with create/destroy qp.*/
4323 spin_lock_irqsave(&ibdev->reset_flow_resource_lock, flags);
4324 list_for_each_entry(mqp, &ibdev->qp_list, qps_list) {
4325 spin_lock_irqsave(&mqp->sq.lock, flags_qp);
4326 if (mqp->sq.tail != mqp->sq.head) {
4327 send_mcq = to_mcq(mqp->ibqp.send_cq);
4328 spin_lock_irqsave(&send_mcq->lock, flags_cq);
4329 if (send_mcq->mcq.comp &&
4330 mqp->ibqp.send_cq->comp_handler) {
4331 if (!send_mcq->mcq.reset_notify_added) {
4332 send_mcq->mcq.reset_notify_added = 1;
4333 list_add_tail(&send_mcq->mcq.reset_notify,
4334 &cq_armed_list);
4335 }
4336 }
4337 spin_unlock_irqrestore(&send_mcq->lock, flags_cq);
4338 }
4339 spin_unlock_irqrestore(&mqp->sq.lock, flags_qp);
4340 spin_lock_irqsave(&mqp->rq.lock, flags_qp);
4341 /* no handling is needed for SRQ */
4342 if (!mqp->ibqp.srq) {
4343 if (mqp->rq.tail != mqp->rq.head) {
4344 recv_mcq = to_mcq(mqp->ibqp.recv_cq);
4345 spin_lock_irqsave(&recv_mcq->lock, flags_cq);
4346 if (recv_mcq->mcq.comp &&
4347 mqp->ibqp.recv_cq->comp_handler) {
4348 if (!recv_mcq->mcq.reset_notify_added) {
4349 recv_mcq->mcq.reset_notify_added = 1;
4350 list_add_tail(&recv_mcq->mcq.reset_notify,
4351 &cq_armed_list);
4352 }
4353 }
4354 spin_unlock_irqrestore(&recv_mcq->lock,
4355 flags_cq);
4356 }
4357 }
4358 spin_unlock_irqrestore(&mqp->rq.lock, flags_qp);
4359 }
4360 /*At that point all inflight post send were put to be executed as of we
4361 * lock/unlock above locks Now need to arm all involved CQs.
4362 */
4363 list_for_each_entry(mcq, &cq_armed_list, reset_notify) {
4364 mcq->comp(mcq);
4365 }
4366 spin_unlock_irqrestore(&ibdev->reset_flow_resource_lock, flags);
4367}
4368
Maor Gottlieb03404e82017-05-30 10:29:13 +03004369static void delay_drop_handler(struct work_struct *work)
4370{
4371 int err;
4372 struct mlx5_ib_delay_drop *delay_drop =
4373 container_of(work, struct mlx5_ib_delay_drop,
4374 delay_drop_work);
4375
Maor Gottliebfe248c32017-05-30 10:29:14 +03004376 atomic_inc(&delay_drop->events_cnt);
4377
Maor Gottlieb03404e82017-05-30 10:29:13 +03004378 mutex_lock(&delay_drop->lock);
4379 err = mlx5_core_set_delay_drop(delay_drop->dev->mdev,
4380 delay_drop->timeout);
4381 if (err) {
4382 mlx5_ib_warn(delay_drop->dev, "Failed to set delay drop, timeout=%u\n",
4383 delay_drop->timeout);
4384 delay_drop->activate = false;
4385 }
4386 mutex_unlock(&delay_drop->lock);
4387}
4388
Saeed Mahameed09e574f2018-11-26 14:39:04 -08004389static void handle_general_event(struct mlx5_ib_dev *ibdev, struct mlx5_eqe *eqe,
4390 struct ib_event *ibev)
4391{
4392 switch (eqe->sub_type) {
4393 case MLX5_GENERAL_SUBTYPE_DELAY_DROP_TIMEOUT:
4394 schedule_work(&ibdev->delay_drop.delay_drop_work);
4395 break;
4396 default: /* do nothing */
4397 return;
4398 }
4399}
4400
Saeed Mahameed134e9342018-11-26 14:39:02 -08004401static int handle_port_change(struct mlx5_ib_dev *ibdev, struct mlx5_eqe *eqe,
4402 struct ib_event *ibev)
4403{
4404 u8 port = (eqe->data.port.port >> 4) & 0xf;
4405
4406 ibev->element.port_num = port;
4407
4408 switch (eqe->sub_type) {
4409 case MLX5_PORT_CHANGE_SUBTYPE_ACTIVE:
4410 case MLX5_PORT_CHANGE_SUBTYPE_DOWN:
4411 case MLX5_PORT_CHANGE_SUBTYPE_INITIALIZED:
4412 /* In RoCE, port up/down events are handled in
4413 * mlx5_netdev_event().
4414 */
4415 if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) ==
4416 IB_LINK_LAYER_ETHERNET)
4417 return -EINVAL;
4418
4419 ibev->event = (eqe->sub_type == MLX5_PORT_CHANGE_SUBTYPE_ACTIVE) ?
4420 IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
4421 break;
4422
4423 case MLX5_PORT_CHANGE_SUBTYPE_LID:
4424 ibev->event = IB_EVENT_LID_CHANGE;
4425 break;
4426
4427 case MLX5_PORT_CHANGE_SUBTYPE_PKEY:
4428 ibev->event = IB_EVENT_PKEY_CHANGE;
4429 schedule_work(&ibdev->devr.ports[port - 1].pkey_change_work);
4430 break;
4431
4432 case MLX5_PORT_CHANGE_SUBTYPE_GUID:
4433 ibev->event = IB_EVENT_GID_CHANGE;
4434 break;
4435
4436 case MLX5_PORT_CHANGE_SUBTYPE_CLIENT_REREG:
4437 ibev->event = IB_EVENT_CLIENT_REREGISTER;
4438 break;
4439 default:
4440 return -EINVAL;
4441 }
4442
4443 return 0;
4444}
4445
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02004446static void mlx5_ib_handle_event(struct work_struct *_work)
Eli Cohene126ba92013-07-07 17:25:49 +03004447{
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02004448 struct mlx5_ib_event_work *work =
4449 container_of(_work, struct mlx5_ib_event_work, work);
4450 struct mlx5_ib_dev *ibdev;
Eli Cohene126ba92013-07-07 17:25:49 +03004451 struct ib_event ibev;
Eli Cohendbaaff22016-10-27 16:36:44 +03004452 bool fatal = false;
Eli Cohene126ba92013-07-07 17:25:49 +03004453
Saeed Mahameeddf097a22018-11-26 14:39:00 -08004454 if (work->is_slave) {
4455 ibdev = mlx5_ib_get_ibdev_from_mpi(work->mpi);
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02004456 if (!ibdev)
4457 goto out;
4458 } else {
Saeed Mahameeddf097a22018-11-26 14:39:00 -08004459 ibdev = work->dev;
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02004460 }
4461
4462 switch (work->event) {
Eli Cohene126ba92013-07-07 17:25:49 +03004463 case MLX5_DEV_EVENT_SYS_ERROR:
Eli Cohene126ba92013-07-07 17:25:49 +03004464 ibev.event = IB_EVENT_DEVICE_FATAL;
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03004465 mlx5_ib_handle_internal_error(ibdev);
Saeed Mahameed134e9342018-11-26 14:39:02 -08004466 ibev.element.port_num = (u8)(unsigned long)work->param;
Eli Cohendbaaff22016-10-27 16:36:44 +03004467 fatal = true;
Eli Cohene126ba92013-07-07 17:25:49 +03004468 break;
Saeed Mahameed134e9342018-11-26 14:39:02 -08004469 case MLX5_EVENT_TYPE_PORT_CHANGE:
4470 if (handle_port_change(ibdev, work->param, &ibev))
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02004471 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03004472 break;
Saeed Mahameed09e574f2018-11-26 14:39:04 -08004473 case MLX5_EVENT_TYPE_GENERAL_EVENT:
4474 handle_general_event(ibdev, work->param, &ibev);
4475 /* fall through */
Saeed Mahameedbdc37922016-09-29 19:35:38 +03004476 default:
Maor Gottlieb03404e82017-05-30 10:29:13 +03004477 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03004478 }
4479
Saeed Mahameed134e9342018-11-26 14:39:02 -08004480 ibev.device = &ibdev->ib_dev;
Eli Cohene126ba92013-07-07 17:25:49 +03004481
Saeed Mahameed134e9342018-11-26 14:39:02 -08004482 if (!rdma_is_port_valid(&ibdev->ib_dev, ibev.element.port_num)) {
4483 mlx5_ib_warn(ibdev, "warning: event on port %d\n", ibev.element.port_num);
Maor Gottlieb03404e82017-05-30 10:29:13 +03004484 goto out;
Eli Cohena0c84c32013-09-11 16:35:27 +03004485 }
4486
Eli Cohene126ba92013-07-07 17:25:49 +03004487 if (ibdev->ib_active)
4488 ib_dispatch_event(&ibev);
Eli Cohendbaaff22016-10-27 16:36:44 +03004489
4490 if (fatal)
4491 ibdev->ib_active = false;
Maor Gottlieb03404e82017-05-30 10:29:13 +03004492out:
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02004493 kfree(work);
4494}
4495
Saeed Mahameeddf097a22018-11-26 14:39:00 -08004496static int mlx5_ib_event(struct notifier_block *nb,
4497 unsigned long event, void *param)
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02004498{
4499 struct mlx5_ib_event_work *work;
4500
4501 work = kmalloc(sizeof(*work), GFP_ATOMIC);
Leon Romanovsky10bea9c2018-01-19 13:07:11 +02004502 if (!work)
Saeed Mahameeddf097a22018-11-26 14:39:00 -08004503 return NOTIFY_DONE;
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02004504
Leon Romanovsky10bea9c2018-01-19 13:07:11 +02004505 INIT_WORK(&work->work, mlx5_ib_handle_event);
Saeed Mahameeddf097a22018-11-26 14:39:00 -08004506 work->dev = container_of(nb, struct mlx5_ib_dev, mdev_events);
4507 work->is_slave = false;
Leon Romanovsky10bea9c2018-01-19 13:07:11 +02004508 work->param = param;
Leon Romanovsky10bea9c2018-01-19 13:07:11 +02004509 work->event = event;
4510
4511 queue_work(mlx5_ib_event_wq, &work->work);
Saeed Mahameeddf097a22018-11-26 14:39:00 -08004512
4513 return NOTIFY_OK;
4514}
4515
4516static int mlx5_ib_event_slave_port(struct notifier_block *nb,
4517 unsigned long event, void *param)
4518{
4519 struct mlx5_ib_event_work *work;
4520
4521 work = kmalloc(sizeof(*work), GFP_ATOMIC);
4522 if (!work)
4523 return NOTIFY_DONE;
4524
4525 INIT_WORK(&work->work, mlx5_ib_handle_event);
4526 work->mpi = container_of(nb, struct mlx5_ib_multiport_info, mdev_events);
4527 work->is_slave = true;
4528 work->param = param;
4529 work->event = event;
4530 queue_work(mlx5_ib_event_wq, &work->work);
4531
4532 return NOTIFY_OK;
Eli Cohene126ba92013-07-07 17:25:49 +03004533}
4534
Maor Gottliebc43f1112017-01-18 14:10:33 +02004535static int set_has_smi_cap(struct mlx5_ib_dev *dev)
4536{
4537 struct mlx5_hca_vport_context vport_ctx;
4538 int err;
4539 int port;
4540
Mark Blocha989ea02019-03-28 15:27:40 +02004541 for (port = 1; port <= ARRAY_SIZE(dev->mdev->port_caps); port++) {
Maor Gottliebc43f1112017-01-18 14:10:33 +02004542 dev->mdev->port_caps[port - 1].has_smi = false;
4543 if (MLX5_CAP_GEN(dev->mdev, port_type) ==
4544 MLX5_CAP_PORT_TYPE_IB) {
4545 if (MLX5_CAP_GEN(dev->mdev, ib_virt)) {
4546 err = mlx5_query_hca_vport_context(dev->mdev, 0,
4547 port, 0,
4548 &vport_ctx);
4549 if (err) {
4550 mlx5_ib_err(dev, "query_hca_vport_context for port=%d failed %d\n",
4551 port, err);
4552 return err;
4553 }
4554 dev->mdev->port_caps[port - 1].has_smi =
4555 vport_ctx.has_smi;
4556 } else {
4557 dev->mdev->port_caps[port - 1].has_smi = true;
4558 }
4559 }
4560 }
4561 return 0;
4562}
4563
Eli Cohene126ba92013-07-07 17:25:49 +03004564static void get_ext_port_caps(struct mlx5_ib_dev *dev)
4565{
4566 int port;
4567
Daniel Jurgens508562d2018-01-04 17:25:34 +02004568 for (port = 1; port <= dev->num_ports; port++)
Eli Cohene126ba92013-07-07 17:25:49 +03004569 mlx5_query_ext_port_caps(dev, port);
4570}
4571
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004572static int get_port_caps(struct mlx5_ib_dev *dev, u8 port)
Eli Cohene126ba92013-07-07 17:25:49 +03004573{
4574 struct ib_device_attr *dprops = NULL;
4575 struct ib_port_attr *pprops = NULL;
Dan Carpenterf614fc12015-01-12 11:56:58 +03004576 int err = -ENOMEM;
Matan Barak2528e332015-06-11 16:35:25 +03004577 struct ib_udata uhw = {.inlen = 0, .outlen = 0};
Eli Cohene126ba92013-07-07 17:25:49 +03004578
4579 pprops = kmalloc(sizeof(*pprops), GFP_KERNEL);
4580 if (!pprops)
4581 goto out;
4582
4583 dprops = kmalloc(sizeof(*dprops), GFP_KERNEL);
4584 if (!dprops)
4585 goto out;
4586
Matan Barak2528e332015-06-11 16:35:25 +03004587 err = mlx5_ib_query_device(&dev->ib_dev, dprops, &uhw);
Eli Cohene126ba92013-07-07 17:25:49 +03004588 if (err) {
4589 mlx5_ib_warn(dev, "query_device failed %d\n", err);
4590 goto out;
4591 }
4592
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004593 memset(pprops, 0, sizeof(*pprops));
4594 err = mlx5_ib_query_port(&dev->ib_dev, port, pprops);
4595 if (err) {
4596 mlx5_ib_warn(dev, "query_port %d failed %d\n",
4597 port, err);
4598 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03004599 }
4600
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004601 dev->mdev->port_caps[port - 1].pkey_table_len =
4602 dprops->max_pkeys;
4603 dev->mdev->port_caps[port - 1].gid_table_len =
4604 pprops->gid_tbl_len;
4605 mlx5_ib_dbg(dev, "port %d: pkey_table_len %d, gid_table_len %d\n",
4606 port, dprops->max_pkeys, pprops->gid_tbl_len);
4607
Eli Cohene126ba92013-07-07 17:25:49 +03004608out:
4609 kfree(pprops);
4610 kfree(dprops);
4611
4612 return err;
4613}
4614
4615static void destroy_umrc_res(struct mlx5_ib_dev *dev)
4616{
4617 int err;
4618
4619 err = mlx5_mr_cache_cleanup(dev);
4620 if (err)
4621 mlx5_ib_warn(dev, "mr cache cleanup failed\n");
4622
Mark Bloch32927e22018-03-20 15:45:37 +02004623 if (dev->umrc.qp)
Shamir Rabinovitchc4367a22019-03-31 19:10:05 +03004624 mlx5_ib_destroy_qp(dev->umrc.qp, NULL);
Mark Bloch32927e22018-03-20 15:45:37 +02004625 if (dev->umrc.cq)
4626 ib_free_cq(dev->umrc.cq);
4627 if (dev->umrc.pd)
4628 ib_dealloc_pd(dev->umrc.pd);
Eli Cohene126ba92013-07-07 17:25:49 +03004629}
4630
4631enum {
4632 MAX_UMR_WR = 128,
4633};
4634
4635static int create_umr_res(struct mlx5_ib_dev *dev)
4636{
4637 struct ib_qp_init_attr *init_attr = NULL;
4638 struct ib_qp_attr *attr = NULL;
4639 struct ib_pd *pd;
4640 struct ib_cq *cq;
4641 struct ib_qp *qp;
Eli Cohene126ba92013-07-07 17:25:49 +03004642 int ret;
4643
4644 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
4645 init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL);
4646 if (!attr || !init_attr) {
4647 ret = -ENOMEM;
4648 goto error_0;
4649 }
4650
Christoph Hellwiged082d32016-09-05 12:56:17 +02004651 pd = ib_alloc_pd(&dev->ib_dev, 0);
Eli Cohene126ba92013-07-07 17:25:49 +03004652 if (IS_ERR(pd)) {
4653 mlx5_ib_dbg(dev, "Couldn't create PD for sync UMR QP\n");
4654 ret = PTR_ERR(pd);
4655 goto error_0;
4656 }
4657
Christoph Hellwigadd08d72016-03-03 09:38:22 +01004658 cq = ib_alloc_cq(&dev->ib_dev, NULL, 128, 0, IB_POLL_SOFTIRQ);
Eli Cohene126ba92013-07-07 17:25:49 +03004659 if (IS_ERR(cq)) {
4660 mlx5_ib_dbg(dev, "Couldn't create CQ for sync UMR QP\n");
4661 ret = PTR_ERR(cq);
4662 goto error_2;
4663 }
Eli Cohene126ba92013-07-07 17:25:49 +03004664
4665 init_attr->send_cq = cq;
4666 init_attr->recv_cq = cq;
4667 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
4668 init_attr->cap.max_send_wr = MAX_UMR_WR;
4669 init_attr->cap.max_send_sge = 1;
4670 init_attr->qp_type = MLX5_IB_QPT_REG_UMR;
4671 init_attr->port_num = 1;
4672 qp = mlx5_ib_create_qp(pd, init_attr, NULL);
4673 if (IS_ERR(qp)) {
4674 mlx5_ib_dbg(dev, "Couldn't create sync UMR QP\n");
4675 ret = PTR_ERR(qp);
4676 goto error_3;
4677 }
4678 qp->device = &dev->ib_dev;
4679 qp->real_qp = qp;
4680 qp->uobject = NULL;
4681 qp->qp_type = MLX5_IB_QPT_REG_UMR;
Majd Dibbiny31fde032017-10-30 14:23:13 +02004682 qp->send_cq = init_attr->send_cq;
4683 qp->recv_cq = init_attr->recv_cq;
Eli Cohene126ba92013-07-07 17:25:49 +03004684
4685 attr->qp_state = IB_QPS_INIT;
4686 attr->port_num = 1;
4687 ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_PKEY_INDEX |
4688 IB_QP_PORT, NULL);
4689 if (ret) {
4690 mlx5_ib_dbg(dev, "Couldn't modify UMR QP\n");
4691 goto error_4;
4692 }
4693
4694 memset(attr, 0, sizeof(*attr));
4695 attr->qp_state = IB_QPS_RTR;
4696 attr->path_mtu = IB_MTU_256;
4697
4698 ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
4699 if (ret) {
4700 mlx5_ib_dbg(dev, "Couldn't modify umr QP to rtr\n");
4701 goto error_4;
4702 }
4703
4704 memset(attr, 0, sizeof(*attr));
4705 attr->qp_state = IB_QPS_RTS;
4706 ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
4707 if (ret) {
4708 mlx5_ib_dbg(dev, "Couldn't modify umr QP to rts\n");
4709 goto error_4;
4710 }
4711
4712 dev->umrc.qp = qp;
4713 dev->umrc.cq = cq;
Eli Cohene126ba92013-07-07 17:25:49 +03004714 dev->umrc.pd = pd;
4715
4716 sema_init(&dev->umrc.sem, MAX_UMR_WR);
4717 ret = mlx5_mr_cache_init(dev);
4718 if (ret) {
4719 mlx5_ib_warn(dev, "mr cache init failed %d\n", ret);
4720 goto error_4;
4721 }
4722
4723 kfree(attr);
4724 kfree(init_attr);
4725
4726 return 0;
4727
4728error_4:
Shamir Rabinovitchc4367a22019-03-31 19:10:05 +03004729 mlx5_ib_destroy_qp(qp, NULL);
Mark Bloch32927e22018-03-20 15:45:37 +02004730 dev->umrc.qp = NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03004731
4732error_3:
Christoph Hellwigadd08d72016-03-03 09:38:22 +01004733 ib_free_cq(cq);
Mark Bloch32927e22018-03-20 15:45:37 +02004734 dev->umrc.cq = NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03004735
4736error_2:
Eli Cohene126ba92013-07-07 17:25:49 +03004737 ib_dealloc_pd(pd);
Mark Bloch32927e22018-03-20 15:45:37 +02004738 dev->umrc.pd = NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03004739
4740error_0:
4741 kfree(attr);
4742 kfree(init_attr);
4743 return ret;
4744}
4745
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03004746static u8 mlx5_get_umr_fence(u8 umr_fence_cap)
4747{
4748 switch (umr_fence_cap) {
4749 case MLX5_CAP_UMR_FENCE_NONE:
4750 return MLX5_FENCE_MODE_NONE;
4751 case MLX5_CAP_UMR_FENCE_SMALL:
4752 return MLX5_FENCE_MODE_INITIATOR_SMALL;
4753 default:
4754 return MLX5_FENCE_MODE_STRONG_ORDERING;
4755 }
4756}
4757
Eli Cohene126ba92013-07-07 17:25:49 +03004758static int create_dev_resources(struct mlx5_ib_resources *devr)
4759{
4760 struct ib_srq_init_attr attr;
4761 struct mlx5_ib_dev *dev;
Leon Romanovsky21a428a2019-02-03 14:55:51 +02004762 struct ib_device *ibdev;
Matan Barakbcf4c1e2015-06-11 16:35:20 +03004763 struct ib_cq_init_attr cq_attr = {.cqe = 1};
Haggai Eran7722f472016-02-29 15:45:07 +02004764 int port;
Eli Cohene126ba92013-07-07 17:25:49 +03004765 int ret = 0;
4766
4767 dev = container_of(devr, struct mlx5_ib_dev, devr);
Leon Romanovsky21a428a2019-02-03 14:55:51 +02004768 ibdev = &dev->ib_dev;
Eli Cohene126ba92013-07-07 17:25:49 +03004769
Haggai Erand16e91d2016-02-29 15:45:05 +02004770 mutex_init(&devr->mutex);
4771
Leon Romanovsky21a428a2019-02-03 14:55:51 +02004772 devr->p0 = rdma_zalloc_drv_obj(ibdev, ib_pd);
4773 if (!devr->p0)
4774 return -ENOMEM;
4775
4776 devr->p0->device = ibdev;
Eli Cohene126ba92013-07-07 17:25:49 +03004777 devr->p0->uobject = NULL;
4778 atomic_set(&devr->p0->usecnt, 0);
4779
Shamir Rabinovitchff23dfa2019-03-31 19:10:07 +03004780 ret = mlx5_ib_alloc_pd(devr->p0, NULL);
Leon Romanovsky21a428a2019-02-03 14:55:51 +02004781 if (ret)
4782 goto error0;
4783
Shamir Rabinovitchff23dfa2019-03-31 19:10:07 +03004784 devr->c0 = mlx5_ib_create_cq(&dev->ib_dev, &cq_attr, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03004785 if (IS_ERR(devr->c0)) {
4786 ret = PTR_ERR(devr->c0);
4787 goto error1;
4788 }
4789 devr->c0->device = &dev->ib_dev;
4790 devr->c0->uobject = NULL;
4791 devr->c0->comp_handler = NULL;
4792 devr->c0->event_handler = NULL;
4793 devr->c0->cq_context = NULL;
4794 atomic_set(&devr->c0->usecnt, 0);
4795
Shamir Rabinovitchff23dfa2019-03-31 19:10:07 +03004796 devr->x0 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03004797 if (IS_ERR(devr->x0)) {
4798 ret = PTR_ERR(devr->x0);
4799 goto error2;
4800 }
4801 devr->x0->device = &dev->ib_dev;
4802 devr->x0->inode = NULL;
4803 atomic_set(&devr->x0->usecnt, 0);
4804 mutex_init(&devr->x0->tgt_qp_mutex);
4805 INIT_LIST_HEAD(&devr->x0->tgt_qp_list);
4806
Shamir Rabinovitchff23dfa2019-03-31 19:10:07 +03004807 devr->x1 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03004808 if (IS_ERR(devr->x1)) {
4809 ret = PTR_ERR(devr->x1);
4810 goto error3;
4811 }
4812 devr->x1->device = &dev->ib_dev;
4813 devr->x1->inode = NULL;
4814 atomic_set(&devr->x1->usecnt, 0);
4815 mutex_init(&devr->x1->tgt_qp_mutex);
4816 INIT_LIST_HEAD(&devr->x1->tgt_qp_list);
4817
4818 memset(&attr, 0, sizeof(attr));
4819 attr.attr.max_sge = 1;
4820 attr.attr.max_wr = 1;
4821 attr.srq_type = IB_SRQT_XRC;
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03004822 attr.ext.cq = devr->c0;
Eli Cohene126ba92013-07-07 17:25:49 +03004823 attr.ext.xrc.xrcd = devr->x0;
4824
Leon Romanovsky68e326d2019-04-03 16:42:43 +03004825 devr->s0 = rdma_zalloc_drv_obj(ibdev, ib_srq);
4826 if (!devr->s0) {
4827 ret = -ENOMEM;
Eli Cohene126ba92013-07-07 17:25:49 +03004828 goto error4;
4829 }
Leon Romanovsky68e326d2019-04-03 16:42:43 +03004830
Eli Cohene126ba92013-07-07 17:25:49 +03004831 devr->s0->device = &dev->ib_dev;
4832 devr->s0->pd = devr->p0;
Eli Cohene126ba92013-07-07 17:25:49 +03004833 devr->s0->srq_type = IB_SRQT_XRC;
4834 devr->s0->ext.xrc.xrcd = devr->x0;
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03004835 devr->s0->ext.cq = devr->c0;
Leon Romanovsky68e326d2019-04-03 16:42:43 +03004836 ret = mlx5_ib_create_srq(devr->s0, &attr, NULL);
4837 if (ret)
4838 goto err_create;
4839
Eli Cohene126ba92013-07-07 17:25:49 +03004840 atomic_inc(&devr->s0->ext.xrc.xrcd->usecnt);
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03004841 atomic_inc(&devr->s0->ext.cq->usecnt);
Eli Cohene126ba92013-07-07 17:25:49 +03004842 atomic_inc(&devr->p0->usecnt);
4843 atomic_set(&devr->s0->usecnt, 0);
4844
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03004845 memset(&attr, 0, sizeof(attr));
4846 attr.attr.max_sge = 1;
4847 attr.attr.max_wr = 1;
4848 attr.srq_type = IB_SRQT_BASIC;
Leon Romanovsky68e326d2019-04-03 16:42:43 +03004849 devr->s1 = rdma_zalloc_drv_obj(ibdev, ib_srq);
4850 if (!devr->s1) {
4851 ret = -ENOMEM;
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03004852 goto error5;
4853 }
Leon Romanovsky68e326d2019-04-03 16:42:43 +03004854
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03004855 devr->s1->device = &dev->ib_dev;
4856 devr->s1->pd = devr->p0;
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03004857 devr->s1->srq_type = IB_SRQT_BASIC;
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03004858 devr->s1->ext.cq = devr->c0;
Leon Romanovsky68e326d2019-04-03 16:42:43 +03004859
4860 ret = mlx5_ib_create_srq(devr->s1, &attr, NULL);
4861 if (ret)
4862 goto error6;
4863
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03004864 atomic_inc(&devr->p0->usecnt);
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03004865 atomic_set(&devr->s1->usecnt, 0);
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03004866
Haggai Eran7722f472016-02-29 15:45:07 +02004867 for (port = 0; port < ARRAY_SIZE(devr->ports); ++port) {
4868 INIT_WORK(&devr->ports[port].pkey_change_work,
4869 pkey_change_handler);
4870 devr->ports[port].devr = devr;
4871 }
4872
Eli Cohene126ba92013-07-07 17:25:49 +03004873 return 0;
4874
Leon Romanovsky68e326d2019-04-03 16:42:43 +03004875error6:
4876 kfree(devr->s1);
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03004877error5:
Shamir Rabinovitchc4367a22019-03-31 19:10:05 +03004878 mlx5_ib_destroy_srq(devr->s0, NULL);
Leon Romanovsky68e326d2019-04-03 16:42:43 +03004879err_create:
4880 kfree(devr->s0);
Eli Cohene126ba92013-07-07 17:25:49 +03004881error4:
Shamir Rabinovitchc4367a22019-03-31 19:10:05 +03004882 mlx5_ib_dealloc_xrcd(devr->x1, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03004883error3:
Shamir Rabinovitchc4367a22019-03-31 19:10:05 +03004884 mlx5_ib_dealloc_xrcd(devr->x0, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03004885error2:
Shamir Rabinovitchc4367a22019-03-31 19:10:05 +03004886 mlx5_ib_destroy_cq(devr->c0, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03004887error1:
Shamir Rabinovitchc4367a22019-03-31 19:10:05 +03004888 mlx5_ib_dealloc_pd(devr->p0, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03004889error0:
Leon Romanovsky21a428a2019-02-03 14:55:51 +02004890 kfree(devr->p0);
Eli Cohene126ba92013-07-07 17:25:49 +03004891 return ret;
4892}
4893
4894static void destroy_dev_resources(struct mlx5_ib_resources *devr)
4895{
Haggai Eran7722f472016-02-29 15:45:07 +02004896 int port;
4897
Shamir Rabinovitchc4367a22019-03-31 19:10:05 +03004898 mlx5_ib_destroy_srq(devr->s1, NULL);
Leon Romanovsky68e326d2019-04-03 16:42:43 +03004899 kfree(devr->s1);
Shamir Rabinovitchc4367a22019-03-31 19:10:05 +03004900 mlx5_ib_destroy_srq(devr->s0, NULL);
Leon Romanovsky68e326d2019-04-03 16:42:43 +03004901 kfree(devr->s0);
Shamir Rabinovitchc4367a22019-03-31 19:10:05 +03004902 mlx5_ib_dealloc_xrcd(devr->x0, NULL);
4903 mlx5_ib_dealloc_xrcd(devr->x1, NULL);
4904 mlx5_ib_destroy_cq(devr->c0, NULL);
4905 mlx5_ib_dealloc_pd(devr->p0, NULL);
Leon Romanovsky21a428a2019-02-03 14:55:51 +02004906 kfree(devr->p0);
Haggai Eran7722f472016-02-29 15:45:07 +02004907
4908 /* Make sure no change P_Key work items are still executing */
Mark Bloch5d8f6a02019-03-28 15:27:36 +02004909 for (port = 0; port < ARRAY_SIZE(devr->ports); ++port)
Haggai Eran7722f472016-02-29 15:45:07 +02004910 cancel_work_sync(&devr->ports[port].pkey_change_work);
Eli Cohene126ba92013-07-07 17:25:49 +03004911}
4912
Artemy Kovalyovb02289b2018-07-04 15:57:50 +03004913static u32 get_core_cap_flags(struct ib_device *ibdev,
4914 struct mlx5_hca_vport_context *rep)
Achiad Shochate53505a2015-12-23 18:47:25 +02004915{
4916 struct mlx5_ib_dev *dev = to_mdev(ibdev);
4917 enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, 1);
4918 u8 l3_type_cap = MLX5_CAP_ROCE(dev->mdev, l3_type);
4919 u8 roce_version_cap = MLX5_CAP_ROCE(dev->mdev, roce_version);
Daniel Jurgens85c7c012018-01-04 17:25:43 +02004920 bool raw_support = !mlx5_core_mp_enabled(dev->mdev);
Achiad Shochate53505a2015-12-23 18:47:25 +02004921 u32 ret = 0;
4922
Artemy Kovalyovb02289b2018-07-04 15:57:50 +03004923 if (rep->grh_required)
4924 ret |= RDMA_CORE_CAP_IB_GRH_REQUIRED;
4925
Achiad Shochate53505a2015-12-23 18:47:25 +02004926 if (ll == IB_LINK_LAYER_INFINIBAND)
Artemy Kovalyovb02289b2018-07-04 15:57:50 +03004927 return ret | RDMA_CORE_PORT_IBA_IB;
Achiad Shochate53505a2015-12-23 18:47:25 +02004928
Daniel Jurgens85c7c012018-01-04 17:25:43 +02004929 if (raw_support)
Artemy Kovalyovb02289b2018-07-04 15:57:50 +03004930 ret |= RDMA_CORE_PORT_RAW_PACKET;
Or Gerlitz72cd5712017-01-24 13:02:36 +02004931
Achiad Shochate53505a2015-12-23 18:47:25 +02004932 if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV4_CAP))
Or Gerlitz72cd5712017-01-24 13:02:36 +02004933 return ret;
Achiad Shochate53505a2015-12-23 18:47:25 +02004934
4935 if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV6_CAP))
Or Gerlitz72cd5712017-01-24 13:02:36 +02004936 return ret;
Achiad Shochate53505a2015-12-23 18:47:25 +02004937
4938 if (roce_version_cap & MLX5_ROCE_VERSION_1_CAP)
4939 ret |= RDMA_CORE_PORT_IBA_ROCE;
4940
4941 if (roce_version_cap & MLX5_ROCE_VERSION_2_CAP)
4942 ret |= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
4943
4944 return ret;
4945}
4946
Ira Weiny77386132015-05-13 20:02:58 -04004947static int mlx5_port_immutable(struct ib_device *ibdev, u8 port_num,
4948 struct ib_port_immutable *immutable)
4949{
4950 struct ib_port_attr attr;
Or Gerlitzca5b91d2016-11-27 16:51:36 +02004951 struct mlx5_ib_dev *dev = to_mdev(ibdev);
4952 enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, port_num);
Artemy Kovalyovb02289b2018-07-04 15:57:50 +03004953 struct mlx5_hca_vport_context rep = {0};
Ira Weiny77386132015-05-13 20:02:58 -04004954 int err;
4955
Or Gerlitzc4550c62017-01-24 13:02:39 +02004956 err = ib_query_port(ibdev, port_num, &attr);
Ira Weiny77386132015-05-13 20:02:58 -04004957 if (err)
4958 return err;
4959
Artemy Kovalyovb02289b2018-07-04 15:57:50 +03004960 if (ll == IB_LINK_LAYER_INFINIBAND) {
4961 err = mlx5_query_hca_vport_context(dev->mdev, 0, port_num, 0,
4962 &rep);
4963 if (err)
4964 return err;
4965 }
4966
Ira Weiny77386132015-05-13 20:02:58 -04004967 immutable->pkey_tbl_len = attr.pkey_tbl_len;
4968 immutable->gid_tbl_len = attr.gid_tbl_len;
Artemy Kovalyovb02289b2018-07-04 15:57:50 +03004969 immutable->core_cap_flags = get_core_cap_flags(ibdev, &rep);
Or Gerlitzca5b91d2016-11-27 16:51:36 +02004970 if ((ll == IB_LINK_LAYER_INFINIBAND) || MLX5_CAP_GEN(dev->mdev, roce))
4971 immutable->max_mad_size = IB_MGMT_MAD_SIZE;
Ira Weiny77386132015-05-13 20:02:58 -04004972
4973 return 0;
4974}
4975
Mark Bloch8e6efa32017-11-06 12:22:13 +00004976static int mlx5_port_rep_immutable(struct ib_device *ibdev, u8 port_num,
4977 struct ib_port_immutable *immutable)
4978{
4979 struct ib_port_attr attr;
4980 int err;
4981
4982 immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET;
4983
4984 err = ib_query_port(ibdev, port_num, &attr);
4985 if (err)
4986 return err;
4987
4988 immutable->pkey_tbl_len = attr.pkey_tbl_len;
4989 immutable->gid_tbl_len = attr.gid_tbl_len;
4990 immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET;
4991
4992 return 0;
4993}
4994
Leon Romanovsky9abb0d12017-06-27 16:49:53 +03004995static void get_dev_fw_str(struct ib_device *ibdev, char *str)
Ira Weinyc7342822016-06-15 02:22:01 -04004996{
4997 struct mlx5_ib_dev *dev =
4998 container_of(ibdev, struct mlx5_ib_dev, ib_dev);
Leon Romanovsky9abb0d12017-06-27 16:49:53 +03004999 snprintf(str, IB_FW_VERSION_NAME_MAX, "%d.%d.%04d",
5000 fw_rev_maj(dev->mdev), fw_rev_min(dev->mdev),
5001 fw_rev_sub(dev->mdev));
Ira Weinyc7342822016-06-15 02:22:01 -04005002}
5003
Or Gerlitz45f95ac2016-11-27 16:51:35 +02005004static int mlx5_eth_lag_init(struct mlx5_ib_dev *dev)
Aviv Heller9ef9c642016-09-18 20:48:01 +03005005{
5006 struct mlx5_core_dev *mdev = dev->mdev;
5007 struct mlx5_flow_namespace *ns = mlx5_get_flow_namespace(mdev,
5008 MLX5_FLOW_NAMESPACE_LAG);
5009 struct mlx5_flow_table *ft;
5010 int err;
5011
Aviv Heller7c34ec12018-08-23 13:47:53 +03005012 if (!ns || !mlx5_lag_is_roce(mdev))
Aviv Heller9ef9c642016-09-18 20:48:01 +03005013 return 0;
5014
5015 err = mlx5_cmd_create_vport_lag(mdev);
5016 if (err)
5017 return err;
5018
5019 ft = mlx5_create_lag_demux_flow_table(ns, 0, 0);
5020 if (IS_ERR(ft)) {
5021 err = PTR_ERR(ft);
5022 goto err_destroy_vport_lag;
5023 }
5024
Mark Bloch9a4ca382018-01-16 14:42:35 +00005025 dev->flow_db->lag_demux_ft = ft;
Aviv Heller7c34ec12018-08-23 13:47:53 +03005026 dev->lag_active = true;
Aviv Heller9ef9c642016-09-18 20:48:01 +03005027 return 0;
5028
5029err_destroy_vport_lag:
5030 mlx5_cmd_destroy_vport_lag(mdev);
5031 return err;
5032}
5033
Or Gerlitz45f95ac2016-11-27 16:51:35 +02005034static void mlx5_eth_lag_cleanup(struct mlx5_ib_dev *dev)
Aviv Heller9ef9c642016-09-18 20:48:01 +03005035{
5036 struct mlx5_core_dev *mdev = dev->mdev;
5037
Aviv Heller7c34ec12018-08-23 13:47:53 +03005038 if (dev->lag_active) {
5039 dev->lag_active = false;
5040
Mark Bloch9a4ca382018-01-16 14:42:35 +00005041 mlx5_destroy_flow_table(dev->flow_db->lag_demux_ft);
5042 dev->flow_db->lag_demux_ft = NULL;
Aviv Heller9ef9c642016-09-18 20:48:01 +03005043
5044 mlx5_cmd_destroy_vport_lag(mdev);
5045 }
5046}
5047
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +02005048static int mlx5_add_netdev_notifier(struct mlx5_ib_dev *dev, u8 port_num)
Achiad Shochatfc24fc52015-12-23 18:47:17 +02005049{
Achiad Shochate53505a2015-12-23 18:47:25 +02005050 int err;
5051
Mark Bloch95579e72019-03-28 15:27:33 +02005052 dev->port[port_num].roce.nb.notifier_call = mlx5_netdev_event;
5053 err = register_netdevice_notifier(&dev->port[port_num].roce.nb);
Aviv Heller5ec8c832016-09-18 20:48:00 +03005054 if (err) {
Mark Bloch95579e72019-03-28 15:27:33 +02005055 dev->port[port_num].roce.nb.notifier_call = NULL;
Achiad Shochate53505a2015-12-23 18:47:25 +02005056 return err;
Aviv Heller5ec8c832016-09-18 20:48:00 +03005057 }
Achiad Shochate53505a2015-12-23 18:47:25 +02005058
Or Gerlitzd012f5d2016-11-27 16:51:34 +02005059 return 0;
5060}
Achiad Shochate53505a2015-12-23 18:47:25 +02005061
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +02005062static void mlx5_remove_netdev_notifier(struct mlx5_ib_dev *dev, u8 port_num)
Eli Cohene126ba92013-07-07 17:25:49 +03005063{
Mark Bloch95579e72019-03-28 15:27:33 +02005064 if (dev->port[port_num].roce.nb.notifier_call) {
5065 unregister_netdevice_notifier(&dev->port[port_num].roce.nb);
5066 dev->port[port_num].roce.nb.notifier_call = NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03005067 }
5068}
5069
Leon Romanovskye3f1ed12018-07-08 12:55:43 +03005070static int mlx5_enable_eth(struct mlx5_ib_dev *dev)
Eli Cohene126ba92013-07-07 17:25:49 +03005071{
Eli Cohene126ba92013-07-07 17:25:49 +03005072 int err;
5073
Or Gerlitzca5b91d2016-11-27 16:51:36 +02005074 if (MLX5_CAP_GEN(dev->mdev, roce)) {
5075 err = mlx5_nic_vport_enable_roce(dev->mdev);
5076 if (err)
Mark Bloch8e6efa32017-11-06 12:22:13 +00005077 return err;
Or Gerlitzca5b91d2016-11-27 16:51:36 +02005078 }
Achiad Shochate53505a2015-12-23 18:47:25 +02005079
Or Gerlitz45f95ac2016-11-27 16:51:35 +02005080 err = mlx5_eth_lag_init(dev);
Aviv Heller9ef9c642016-09-18 20:48:01 +03005081 if (err)
5082 goto err_disable_roce;
5083
Achiad Shochate53505a2015-12-23 18:47:25 +02005084 return 0;
5085
Aviv Heller9ef9c642016-09-18 20:48:01 +03005086err_disable_roce:
Or Gerlitzca5b91d2016-11-27 16:51:36 +02005087 if (MLX5_CAP_GEN(dev->mdev, roce))
5088 mlx5_nic_vport_disable_roce(dev->mdev);
Aviv Heller9ef9c642016-09-18 20:48:01 +03005089
Achiad Shochate53505a2015-12-23 18:47:25 +02005090 return err;
Achiad Shochatfc24fc52015-12-23 18:47:17 +02005091}
5092
Or Gerlitz45f95ac2016-11-27 16:51:35 +02005093static void mlx5_disable_eth(struct mlx5_ib_dev *dev)
Achiad Shochatfc24fc52015-12-23 18:47:17 +02005094{
Or Gerlitz45f95ac2016-11-27 16:51:35 +02005095 mlx5_eth_lag_cleanup(dev);
Or Gerlitzca5b91d2016-11-27 16:51:36 +02005096 if (MLX5_CAP_GEN(dev->mdev, roce))
5097 mlx5_nic_vport_disable_roce(dev->mdev);
Achiad Shochatfc24fc52015-12-23 18:47:17 +02005098}
5099
Parav Pandite1f24a72017-04-16 07:29:29 +03005100struct mlx5_ib_counter {
Kamal Heib7c16f472017-01-18 15:25:09 +02005101 const char *name;
5102 size_t offset;
5103};
5104
5105#define INIT_Q_COUNTER(_name) \
5106 { .name = #_name, .offset = MLX5_BYTE_OFF(query_q_counter_out, _name)}
5107
Parav Pandite1f24a72017-04-16 07:29:29 +03005108static const struct mlx5_ib_counter basic_q_cnts[] = {
Kamal Heib7c16f472017-01-18 15:25:09 +02005109 INIT_Q_COUNTER(rx_write_requests),
5110 INIT_Q_COUNTER(rx_read_requests),
5111 INIT_Q_COUNTER(rx_atomic_requests),
5112 INIT_Q_COUNTER(out_of_buffer),
5113};
5114
Parav Pandite1f24a72017-04-16 07:29:29 +03005115static const struct mlx5_ib_counter out_of_seq_q_cnts[] = {
Kamal Heib7c16f472017-01-18 15:25:09 +02005116 INIT_Q_COUNTER(out_of_sequence),
5117};
5118
Parav Pandite1f24a72017-04-16 07:29:29 +03005119static const struct mlx5_ib_counter retrans_q_cnts[] = {
Kamal Heib7c16f472017-01-18 15:25:09 +02005120 INIT_Q_COUNTER(duplicate_request),
5121 INIT_Q_COUNTER(rnr_nak_retry_err),
5122 INIT_Q_COUNTER(packet_seq_err),
5123 INIT_Q_COUNTER(implied_nak_seq_err),
5124 INIT_Q_COUNTER(local_ack_timeout_err),
5125};
5126
Parav Pandite1f24a72017-04-16 07:29:29 +03005127#define INIT_CONG_COUNTER(_name) \
5128 { .name = #_name, .offset = \
5129 MLX5_BYTE_OFF(query_cong_statistics_out, _name ## _high)}
5130
5131static const struct mlx5_ib_counter cong_cnts[] = {
5132 INIT_CONG_COUNTER(rp_cnp_ignored),
5133 INIT_CONG_COUNTER(rp_cnp_handled),
5134 INIT_CONG_COUNTER(np_ecn_marked_roce_packets),
5135 INIT_CONG_COUNTER(np_cnp_sent),
5136};
5137
Parav Pandit58dcb602017-06-19 07:19:37 +03005138static const struct mlx5_ib_counter extended_err_cnts[] = {
5139 INIT_Q_COUNTER(resp_local_length_error),
5140 INIT_Q_COUNTER(resp_cqe_error),
5141 INIT_Q_COUNTER(req_cqe_error),
5142 INIT_Q_COUNTER(req_remote_invalid_request),
5143 INIT_Q_COUNTER(req_remote_access_errors),
5144 INIT_Q_COUNTER(resp_remote_access_errors),
5145 INIT_Q_COUNTER(resp_cqe_flush_error),
5146 INIT_Q_COUNTER(req_cqe_flush_error),
5147};
5148
Talat Batheesh9f876f32018-06-21 15:37:56 +03005149#define INIT_EXT_PPCNT_COUNTER(_name) \
5150 { .name = #_name, .offset = \
5151 MLX5_BYTE_OFF(ppcnt_reg, \
5152 counter_set.eth_extended_cntrs_grp_data_layout._name##_high)}
5153
5154static const struct mlx5_ib_counter ext_ppcnt_cnts[] = {
5155 INIT_EXT_PPCNT_COUNTER(rx_icrc_encapsulated),
5156};
5157
Parav Pandite1f24a72017-04-16 07:29:29 +03005158static void mlx5_ib_dealloc_counters(struct mlx5_ib_dev *dev)
Mark Bloch0837e862016-06-17 15:10:55 +03005159{
Daniel Jurgensaac44922018-01-04 17:25:40 +02005160 int i;
Mark Bloch0837e862016-06-17 15:10:55 +03005161
Kamal Heib7c16f472017-01-18 15:25:09 +02005162 for (i = 0; i < dev->num_ports; i++) {
Parav Pandit921c0f52018-07-08 13:40:30 +03005163 if (dev->port[i].cnts.set_id_valid)
Daniel Jurgensaac44922018-01-04 17:25:40 +02005164 mlx5_core_dealloc_q_counter(dev->mdev,
5165 dev->port[i].cnts.set_id);
Parav Pandite1f24a72017-04-16 07:29:29 +03005166 kfree(dev->port[i].cnts.names);
5167 kfree(dev->port[i].cnts.offsets);
Kamal Heib7c16f472017-01-18 15:25:09 +02005168 }
5169}
5170
Parav Pandite1f24a72017-04-16 07:29:29 +03005171static int __mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev,
5172 struct mlx5_ib_counters *cnts)
Kamal Heib7c16f472017-01-18 15:25:09 +02005173{
5174 u32 num_counters;
5175
5176 num_counters = ARRAY_SIZE(basic_q_cnts);
5177
5178 if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt))
5179 num_counters += ARRAY_SIZE(out_of_seq_q_cnts);
5180
5181 if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters))
5182 num_counters += ARRAY_SIZE(retrans_q_cnts);
Parav Pandit58dcb602017-06-19 07:19:37 +03005183
5184 if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters))
5185 num_counters += ARRAY_SIZE(extended_err_cnts);
5186
Parav Pandite1f24a72017-04-16 07:29:29 +03005187 cnts->num_q_counters = num_counters;
Kamal Heib7c16f472017-01-18 15:25:09 +02005188
Parav Pandite1f24a72017-04-16 07:29:29 +03005189 if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
5190 cnts->num_cong_counters = ARRAY_SIZE(cong_cnts);
5191 num_counters += ARRAY_SIZE(cong_cnts);
5192 }
Talat Batheesh9f876f32018-06-21 15:37:56 +03005193 if (MLX5_CAP_PCAM_FEATURE(dev->mdev, rx_icrc_encapsulated_counter)) {
5194 cnts->num_ext_ppcnt_counters = ARRAY_SIZE(ext_ppcnt_cnts);
5195 num_counters += ARRAY_SIZE(ext_ppcnt_cnts);
5196 }
Parav Pandite1f24a72017-04-16 07:29:29 +03005197 cnts->names = kcalloc(num_counters, sizeof(cnts->names), GFP_KERNEL);
5198 if (!cnts->names)
Kamal Heib7c16f472017-01-18 15:25:09 +02005199 return -ENOMEM;
5200
Parav Pandite1f24a72017-04-16 07:29:29 +03005201 cnts->offsets = kcalloc(num_counters,
5202 sizeof(cnts->offsets), GFP_KERNEL);
5203 if (!cnts->offsets)
Kamal Heib7c16f472017-01-18 15:25:09 +02005204 goto err_names;
5205
Kamal Heib7c16f472017-01-18 15:25:09 +02005206 return 0;
5207
5208err_names:
Parav Pandite1f24a72017-04-16 07:29:29 +03005209 kfree(cnts->names);
Daniel Jurgensaac44922018-01-04 17:25:40 +02005210 cnts->names = NULL;
Kamal Heib7c16f472017-01-18 15:25:09 +02005211 return -ENOMEM;
5212}
5213
Parav Pandite1f24a72017-04-16 07:29:29 +03005214static void mlx5_ib_fill_counters(struct mlx5_ib_dev *dev,
5215 const char **names,
5216 size_t *offsets)
Kamal Heib7c16f472017-01-18 15:25:09 +02005217{
5218 int i;
5219 int j = 0;
5220
5221 for (i = 0; i < ARRAY_SIZE(basic_q_cnts); i++, j++) {
5222 names[j] = basic_q_cnts[i].name;
5223 offsets[j] = basic_q_cnts[i].offset;
5224 }
5225
5226 if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt)) {
5227 for (i = 0; i < ARRAY_SIZE(out_of_seq_q_cnts); i++, j++) {
5228 names[j] = out_of_seq_q_cnts[i].name;
5229 offsets[j] = out_of_seq_q_cnts[i].offset;
5230 }
5231 }
5232
5233 if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters)) {
5234 for (i = 0; i < ARRAY_SIZE(retrans_q_cnts); i++, j++) {
5235 names[j] = retrans_q_cnts[i].name;
5236 offsets[j] = retrans_q_cnts[i].offset;
5237 }
5238 }
Parav Pandite1f24a72017-04-16 07:29:29 +03005239
Parav Pandit58dcb602017-06-19 07:19:37 +03005240 if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters)) {
5241 for (i = 0; i < ARRAY_SIZE(extended_err_cnts); i++, j++) {
5242 names[j] = extended_err_cnts[i].name;
5243 offsets[j] = extended_err_cnts[i].offset;
5244 }
5245 }
5246
Parav Pandite1f24a72017-04-16 07:29:29 +03005247 if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
5248 for (i = 0; i < ARRAY_SIZE(cong_cnts); i++, j++) {
5249 names[j] = cong_cnts[i].name;
5250 offsets[j] = cong_cnts[i].offset;
5251 }
5252 }
Talat Batheesh9f876f32018-06-21 15:37:56 +03005253
5254 if (MLX5_CAP_PCAM_FEATURE(dev->mdev, rx_icrc_encapsulated_counter)) {
5255 for (i = 0; i < ARRAY_SIZE(ext_ppcnt_cnts); i++, j++) {
5256 names[j] = ext_ppcnt_cnts[i].name;
5257 offsets[j] = ext_ppcnt_cnts[i].offset;
5258 }
5259 }
Mark Bloch0837e862016-06-17 15:10:55 +03005260}
5261
Parav Pandite1f24a72017-04-16 07:29:29 +03005262static int mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev)
Mark Bloch0837e862016-06-17 15:10:55 +03005263{
Daniel Jurgensaac44922018-01-04 17:25:40 +02005264 int err = 0;
Mark Bloch0837e862016-06-17 15:10:55 +03005265 int i;
Yishai Hadasaa74be62018-12-09 12:52:36 +02005266 bool is_shared;
5267
5268 is_shared = MLX5_CAP_GEN(dev->mdev, log_max_uctx) != 0;
Mark Bloch0837e862016-06-17 15:10:55 +03005269
5270 for (i = 0; i < dev->num_ports; i++) {
Daniel Jurgensaac44922018-01-04 17:25:40 +02005271 err = __mlx5_ib_alloc_counters(dev, &dev->port[i].cnts);
5272 if (err)
5273 goto err_alloc;
Kamal Heib7c16f472017-01-18 15:25:09 +02005274
Daniel Jurgensaac44922018-01-04 17:25:40 +02005275 mlx5_ib_fill_counters(dev, dev->port[i].cnts.names,
5276 dev->port[i].cnts.offsets);
5277
Yishai Hadasaa74be62018-12-09 12:52:36 +02005278 err = mlx5_cmd_alloc_q_counter(dev->mdev,
5279 &dev->port[i].cnts.set_id,
5280 is_shared ?
5281 MLX5_SHARED_RESOURCE_UID : 0);
Daniel Jurgensaac44922018-01-04 17:25:40 +02005282 if (err) {
Mark Bloch0837e862016-06-17 15:10:55 +03005283 mlx5_ib_warn(dev,
5284 "couldn't allocate queue counter for port %d, err %d\n",
Daniel Jurgensaac44922018-01-04 17:25:40 +02005285 i + 1, err);
5286 goto err_alloc;
Mark Bloch0837e862016-06-17 15:10:55 +03005287 }
Daniel Jurgensaac44922018-01-04 17:25:40 +02005288 dev->port[i].cnts.set_id_valid = true;
Mark Bloch0837e862016-06-17 15:10:55 +03005289 }
5290
5291 return 0;
5292
Daniel Jurgensaac44922018-01-04 17:25:40 +02005293err_alloc:
5294 mlx5_ib_dealloc_counters(dev);
5295 return err;
Mark Bloch0837e862016-06-17 15:10:55 +03005296}
5297
Mark Bloch0ad17a82016-06-17 15:10:56 +03005298static struct rdma_hw_stats *mlx5_ib_alloc_hw_stats(struct ib_device *ibdev,
5299 u8 port_num)
5300{
Kamal Heib7c16f472017-01-18 15:25:09 +02005301 struct mlx5_ib_dev *dev = to_mdev(ibdev);
5302 struct mlx5_ib_port *port = &dev->port[port_num - 1];
Mark Bloch0ad17a82016-06-17 15:10:56 +03005303
5304 /* We support only per port stats */
5305 if (port_num == 0)
5306 return NULL;
5307
Parav Pandite1f24a72017-04-16 07:29:29 +03005308 return rdma_alloc_hw_stats_struct(port->cnts.names,
5309 port->cnts.num_q_counters +
Talat Batheesh9f876f32018-06-21 15:37:56 +03005310 port->cnts.num_cong_counters +
5311 port->cnts.num_ext_ppcnt_counters,
Mark Bloch0ad17a82016-06-17 15:10:56 +03005312 RDMA_HW_STATS_DEFAULT_LIFESPAN);
5313}
5314
Daniel Jurgensaac44922018-01-04 17:25:40 +02005315static int mlx5_ib_query_q_counters(struct mlx5_core_dev *mdev,
Parav Pandite1f24a72017-04-16 07:29:29 +03005316 struct mlx5_ib_port *port,
5317 struct rdma_hw_stats *stats)
5318{
5319 int outlen = MLX5_ST_SZ_BYTES(query_q_counter_out);
5320 void *out;
5321 __be32 val;
5322 int ret, i;
5323
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03005324 out = kvzalloc(outlen, GFP_KERNEL);
Parav Pandite1f24a72017-04-16 07:29:29 +03005325 if (!out)
5326 return -ENOMEM;
5327
Daniel Jurgensaac44922018-01-04 17:25:40 +02005328 ret = mlx5_core_query_q_counter(mdev,
Parav Pandite1f24a72017-04-16 07:29:29 +03005329 port->cnts.set_id, 0,
5330 out, outlen);
5331 if (ret)
5332 goto free;
5333
5334 for (i = 0; i < port->cnts.num_q_counters; i++) {
5335 val = *(__be32 *)(out + port->cnts.offsets[i]);
5336 stats->value[i] = (u64)be32_to_cpu(val);
5337 }
5338
5339free:
5340 kvfree(out);
5341 return ret;
5342}
5343
Talat Batheesh9f876f32018-06-21 15:37:56 +03005344static int mlx5_ib_query_ext_ppcnt_counters(struct mlx5_ib_dev *dev,
5345 struct mlx5_ib_port *port,
5346 struct rdma_hw_stats *stats)
5347{
5348 int offset = port->cnts.num_q_counters + port->cnts.num_cong_counters;
5349 int sz = MLX5_ST_SZ_BYTES(ppcnt_reg);
5350 int ret, i;
5351 void *out;
5352
5353 out = kvzalloc(sz, GFP_KERNEL);
5354 if (!out)
5355 return -ENOMEM;
5356
5357 ret = mlx5_cmd_query_ext_ppcnt_counters(dev->mdev, out);
5358 if (ret)
5359 goto free;
5360
5361 for (i = 0; i < port->cnts.num_ext_ppcnt_counters; i++) {
5362 stats->value[i + offset] =
5363 be64_to_cpup((__be64 *)(out +
5364 port->cnts.offsets[i + offset]));
5365 }
5366
5367free:
5368 kvfree(out);
5369 return ret;
5370}
5371
Mark Bloch0ad17a82016-06-17 15:10:56 +03005372static int mlx5_ib_get_hw_stats(struct ib_device *ibdev,
5373 struct rdma_hw_stats *stats,
Kamal Heib7c16f472017-01-18 15:25:09 +02005374 u8 port_num, int index)
Mark Bloch0ad17a82016-06-17 15:10:56 +03005375{
5376 struct mlx5_ib_dev *dev = to_mdev(ibdev);
Kamal Heib7c16f472017-01-18 15:25:09 +02005377 struct mlx5_ib_port *port = &dev->port[port_num - 1];
Daniel Jurgensaac44922018-01-04 17:25:40 +02005378 struct mlx5_core_dev *mdev;
Parav Pandite1f24a72017-04-16 07:29:29 +03005379 int ret, num_counters;
Daniel Jurgensaac44922018-01-04 17:25:40 +02005380 u8 mdev_port_num;
Mark Bloch0ad17a82016-06-17 15:10:56 +03005381
Kamal Heib7c16f472017-01-18 15:25:09 +02005382 if (!stats)
Parav Pandite1f24a72017-04-16 07:29:29 +03005383 return -EINVAL;
Mark Bloch0ad17a82016-06-17 15:10:56 +03005384
Talat Batheesh9f876f32018-06-21 15:37:56 +03005385 num_counters = port->cnts.num_q_counters +
5386 port->cnts.num_cong_counters +
5387 port->cnts.num_ext_ppcnt_counters;
Daniel Jurgensaac44922018-01-04 17:25:40 +02005388
5389 /* q_counters are per IB device, query the master mdev */
5390 ret = mlx5_ib_query_q_counters(dev->mdev, port, stats);
Mark Bloch0ad17a82016-06-17 15:10:56 +03005391 if (ret)
Parav Pandite1f24a72017-04-16 07:29:29 +03005392 return ret;
Mark Bloch0ad17a82016-06-17 15:10:56 +03005393
Talat Batheesh9f876f32018-06-21 15:37:56 +03005394 if (MLX5_CAP_PCAM_FEATURE(dev->mdev, rx_icrc_encapsulated_counter)) {
5395 ret = mlx5_ib_query_ext_ppcnt_counters(dev, port, stats);
5396 if (ret)
5397 return ret;
5398 }
5399
Parav Pandite1f24a72017-04-16 07:29:29 +03005400 if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
Daniel Jurgensaac44922018-01-04 17:25:40 +02005401 mdev = mlx5_ib_get_native_port_mdev(dev, port_num,
5402 &mdev_port_num);
5403 if (!mdev) {
5404 /* If port is not affiliated yet, its in down state
5405 * which doesn't have any counters yet, so it would be
5406 * zero. So no need to read from the HCA.
5407 */
5408 goto done;
5409 }
Majd Dibbiny71a0ff62017-12-21 17:38:26 +02005410 ret = mlx5_lag_query_cong_counters(dev->mdev,
5411 stats->value +
5412 port->cnts.num_q_counters,
5413 port->cnts.num_cong_counters,
5414 port->cnts.offsets +
5415 port->cnts.num_q_counters);
Daniel Jurgensaac44922018-01-04 17:25:40 +02005416
5417 mlx5_ib_put_native_port_mdev(dev, port_num);
Parav Pandite1f24a72017-04-16 07:29:29 +03005418 if (ret)
5419 return ret;
Mark Bloch0ad17a82016-06-17 15:10:56 +03005420 }
Kamal Heib7c16f472017-01-18 15:25:09 +02005421
Daniel Jurgensaac44922018-01-04 17:25:40 +02005422done:
Parav Pandite1f24a72017-04-16 07:29:29 +03005423 return num_counters;
Mark Bloch0ad17a82016-06-17 15:10:56 +03005424}
5425
Denis Drozdovf6a8a192018-08-14 14:08:51 +03005426static int mlx5_ib_rn_get_params(struct ib_device *device, u8 port_num,
5427 enum rdma_netdev_t type,
5428 struct rdma_netdev_alloc_params *params)
Erez Shitrit693dfd52017-04-27 17:01:34 +03005429{
5430 if (type != RDMA_NETDEV_IPOIB)
Denis Drozdovf6a8a192018-08-14 14:08:51 +03005431 return -EOPNOTSUPP;
Erez Shitrit693dfd52017-04-27 17:01:34 +03005432
Denis Drozdovf6a8a192018-08-14 14:08:51 +03005433 return mlx5_rdma_rn_get_params(to_mdev(device)->mdev, device, params);
Erez Shitrit693dfd52017-04-27 17:01:34 +03005434}
5435
Maor Gottliebfe248c32017-05-30 10:29:14 +03005436static void delay_drop_debugfs_cleanup(struct mlx5_ib_dev *dev)
5437{
5438 if (!dev->delay_drop.dbg)
5439 return;
5440 debugfs_remove_recursive(dev->delay_drop.dbg->dir_debugfs);
5441 kfree(dev->delay_drop.dbg);
5442 dev->delay_drop.dbg = NULL;
5443}
5444
Maor Gottlieb03404e82017-05-30 10:29:13 +03005445static void cancel_delay_drop(struct mlx5_ib_dev *dev)
5446{
5447 if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
5448 return;
5449
5450 cancel_work_sync(&dev->delay_drop.delay_drop_work);
Maor Gottliebfe248c32017-05-30 10:29:14 +03005451 delay_drop_debugfs_cleanup(dev);
5452}
5453
5454static ssize_t delay_drop_timeout_read(struct file *filp, char __user *buf,
5455 size_t count, loff_t *pos)
5456{
5457 struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
5458 char lbuf[20];
5459 int len;
5460
5461 len = snprintf(lbuf, sizeof(lbuf), "%u\n", delay_drop->timeout);
5462 return simple_read_from_buffer(buf, count, pos, lbuf, len);
5463}
5464
5465static ssize_t delay_drop_timeout_write(struct file *filp, const char __user *buf,
5466 size_t count, loff_t *pos)
5467{
5468 struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
5469 u32 timeout;
5470 u32 var;
5471
5472 if (kstrtouint_from_user(buf, count, 0, &var))
5473 return -EFAULT;
5474
5475 timeout = min_t(u32, roundup(var, 100), MLX5_MAX_DELAY_DROP_TIMEOUT_MS *
5476 1000);
5477 if (timeout != var)
5478 mlx5_ib_dbg(delay_drop->dev, "Round delay drop timeout to %u usec\n",
5479 timeout);
5480
5481 delay_drop->timeout = timeout;
5482
5483 return count;
5484}
5485
5486static const struct file_operations fops_delay_drop_timeout = {
5487 .owner = THIS_MODULE,
5488 .open = simple_open,
5489 .write = delay_drop_timeout_write,
5490 .read = delay_drop_timeout_read,
5491};
5492
5493static int delay_drop_debugfs_init(struct mlx5_ib_dev *dev)
5494{
5495 struct mlx5_ib_dbg_delay_drop *dbg;
5496
5497 if (!mlx5_debugfs_root)
5498 return 0;
5499
5500 dbg = kzalloc(sizeof(*dbg), GFP_KERNEL);
5501 if (!dbg)
5502 return -ENOMEM;
5503
Sudip Mukherjeecbafad82017-09-18 12:28:48 +01005504 dev->delay_drop.dbg = dbg;
5505
Maor Gottliebfe248c32017-05-30 10:29:14 +03005506 dbg->dir_debugfs =
5507 debugfs_create_dir("delay_drop",
5508 dev->mdev->priv.dbg_root);
5509 if (!dbg->dir_debugfs)
Sudip Mukherjeecbafad82017-09-18 12:28:48 +01005510 goto out_debugfs;
Maor Gottliebfe248c32017-05-30 10:29:14 +03005511
5512 dbg->events_cnt_debugfs =
5513 debugfs_create_atomic_t("num_timeout_events", 0400,
5514 dbg->dir_debugfs,
5515 &dev->delay_drop.events_cnt);
5516 if (!dbg->events_cnt_debugfs)
5517 goto out_debugfs;
5518
5519 dbg->rqs_cnt_debugfs =
5520 debugfs_create_atomic_t("num_rqs", 0400,
5521 dbg->dir_debugfs,
5522 &dev->delay_drop.rqs_cnt);
5523 if (!dbg->rqs_cnt_debugfs)
5524 goto out_debugfs;
5525
5526 dbg->timeout_debugfs =
5527 debugfs_create_file("timeout", 0600,
5528 dbg->dir_debugfs,
5529 &dev->delay_drop,
5530 &fops_delay_drop_timeout);
5531 if (!dbg->timeout_debugfs)
5532 goto out_debugfs;
5533
5534 return 0;
5535
5536out_debugfs:
5537 delay_drop_debugfs_cleanup(dev);
5538 return -ENOMEM;
Maor Gottlieb03404e82017-05-30 10:29:13 +03005539}
5540
5541static void init_delay_drop(struct mlx5_ib_dev *dev)
5542{
5543 if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
5544 return;
5545
5546 mutex_init(&dev->delay_drop.lock);
5547 dev->delay_drop.dev = dev;
5548 dev->delay_drop.activate = false;
5549 dev->delay_drop.timeout = MLX5_MAX_DELAY_DROP_TIMEOUT_MS * 1000;
5550 INIT_WORK(&dev->delay_drop.delay_drop_work, delay_drop_handler);
Maor Gottliebfe248c32017-05-30 10:29:14 +03005551 atomic_set(&dev->delay_drop.rqs_cnt, 0);
5552 atomic_set(&dev->delay_drop.events_cnt, 0);
5553
5554 if (delay_drop_debugfs_init(dev))
5555 mlx5_ib_warn(dev, "Failed to init delay drop debugfs\n");
Maor Gottlieb03404e82017-05-30 10:29:13 +03005556}
5557
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005558/* The mlx5_ib_multiport_mutex should be held when calling this function */
5559static void mlx5_ib_unbind_slave_port(struct mlx5_ib_dev *ibdev,
5560 struct mlx5_ib_multiport_info *mpi)
5561{
5562 u8 port_num = mlx5_core_native_port_num(mpi->mdev) - 1;
5563 struct mlx5_ib_port *port = &ibdev->port[port_num];
5564 int comps;
5565 int err;
5566 int i;
5567
Parav Pandita9e546e2018-01-04 17:25:39 +02005568 mlx5_ib_cleanup_cong_debugfs(ibdev, port_num);
5569
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005570 spin_lock(&port->mp.mpi_lock);
5571 if (!mpi->ibdev) {
5572 spin_unlock(&port->mp.mpi_lock);
5573 return;
5574 }
Saeed Mahameeddf097a22018-11-26 14:39:00 -08005575
5576 if (mpi->mdev_events.notifier_call)
5577 mlx5_notifier_unregister(mpi->mdev, &mpi->mdev_events);
5578 mpi->mdev_events.notifier_call = NULL;
5579
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005580 mpi->ibdev = NULL;
5581
5582 spin_unlock(&port->mp.mpi_lock);
5583 mlx5_remove_netdev_notifier(ibdev, port_num);
5584 spin_lock(&port->mp.mpi_lock);
5585
5586 comps = mpi->mdev_refcnt;
5587 if (comps) {
5588 mpi->unaffiliate = true;
5589 init_completion(&mpi->unref_comp);
5590 spin_unlock(&port->mp.mpi_lock);
5591
5592 for (i = 0; i < comps; i++)
5593 wait_for_completion(&mpi->unref_comp);
5594
5595 spin_lock(&port->mp.mpi_lock);
5596 mpi->unaffiliate = false;
5597 }
5598
5599 port->mp.mpi = NULL;
5600
5601 list_add_tail(&mpi->list, &mlx5_ib_unaffiliated_port_list);
5602
5603 spin_unlock(&port->mp.mpi_lock);
5604
5605 err = mlx5_nic_vport_unaffiliate_multiport(mpi->mdev);
5606
5607 mlx5_ib_dbg(ibdev, "unaffiliated port %d\n", port_num + 1);
5608 /* Log an error, still needed to cleanup the pointers and add
5609 * it back to the list.
5610 */
5611 if (err)
5612 mlx5_ib_err(ibdev, "Failed to unaffiliate port %u\n",
5613 port_num + 1);
5614
Mark Bloch95579e72019-03-28 15:27:33 +02005615 ibdev->port[port_num].roce.last_port_state = IB_PORT_DOWN;
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005616}
5617
5618/* The mlx5_ib_multiport_mutex should be held when calling this function */
5619static bool mlx5_ib_bind_slave_port(struct mlx5_ib_dev *ibdev,
5620 struct mlx5_ib_multiport_info *mpi)
5621{
5622 u8 port_num = mlx5_core_native_port_num(mpi->mdev) - 1;
5623 int err;
5624
5625 spin_lock(&ibdev->port[port_num].mp.mpi_lock);
5626 if (ibdev->port[port_num].mp.mpi) {
Qing Huang25771882018-07-23 14:15:08 -07005627 mlx5_ib_dbg(ibdev, "port %d already affiliated.\n",
5628 port_num + 1);
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005629 spin_unlock(&ibdev->port[port_num].mp.mpi_lock);
5630 return false;
5631 }
5632
5633 ibdev->port[port_num].mp.mpi = mpi;
5634 mpi->ibdev = ibdev;
Saeed Mahameeddf097a22018-11-26 14:39:00 -08005635 mpi->mdev_events.notifier_call = NULL;
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005636 spin_unlock(&ibdev->port[port_num].mp.mpi_lock);
5637
5638 err = mlx5_nic_vport_affiliate_multiport(ibdev->mdev, mpi->mdev);
5639 if (err)
5640 goto unbind;
5641
5642 err = get_port_caps(ibdev, mlx5_core_native_port_num(mpi->mdev));
5643 if (err)
5644 goto unbind;
5645
5646 err = mlx5_add_netdev_notifier(ibdev, port_num);
5647 if (err) {
5648 mlx5_ib_err(ibdev, "failed adding netdev notifier for port %u\n",
5649 port_num + 1);
5650 goto unbind;
5651 }
5652
Saeed Mahameeddf097a22018-11-26 14:39:00 -08005653 mpi->mdev_events.notifier_call = mlx5_ib_event_slave_port;
5654 mlx5_notifier_register(mpi->mdev, &mpi->mdev_events);
5655
Greg Kroah-Hartman73eb8f02019-01-22 16:17:57 +01005656 mlx5_ib_init_cong_debugfs(ibdev, port_num);
Parav Pandita9e546e2018-01-04 17:25:39 +02005657
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005658 return true;
5659
5660unbind:
5661 mlx5_ib_unbind_slave_port(ibdev, mpi);
5662 return false;
5663}
5664
5665static int mlx5_ib_init_multiport_master(struct mlx5_ib_dev *dev)
5666{
5667 int port_num = mlx5_core_native_port_num(dev->mdev) - 1;
5668 enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev,
5669 port_num + 1);
5670 struct mlx5_ib_multiport_info *mpi;
5671 int err;
5672 int i;
5673
5674 if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
5675 return 0;
5676
5677 err = mlx5_query_nic_vport_system_image_guid(dev->mdev,
5678 &dev->sys_image_guid);
5679 if (err)
5680 return err;
5681
5682 err = mlx5_nic_vport_enable_roce(dev->mdev);
5683 if (err)
5684 return err;
5685
5686 mutex_lock(&mlx5_ib_multiport_mutex);
5687 for (i = 0; i < dev->num_ports; i++) {
5688 bool bound = false;
5689
5690 /* build a stub multiport info struct for the native port. */
5691 if (i == port_num) {
5692 mpi = kzalloc(sizeof(*mpi), GFP_KERNEL);
5693 if (!mpi) {
5694 mutex_unlock(&mlx5_ib_multiport_mutex);
5695 mlx5_nic_vport_disable_roce(dev->mdev);
5696 return -ENOMEM;
5697 }
5698
5699 mpi->is_master = true;
5700 mpi->mdev = dev->mdev;
5701 mpi->sys_image_guid = dev->sys_image_guid;
5702 dev->port[i].mp.mpi = mpi;
5703 mpi->ibdev = dev;
5704 mpi = NULL;
5705 continue;
5706 }
5707
5708 list_for_each_entry(mpi, &mlx5_ib_unaffiliated_port_list,
5709 list) {
5710 if (dev->sys_image_guid == mpi->sys_image_guid &&
5711 (mlx5_core_native_port_num(mpi->mdev) - 1) == i) {
5712 bound = mlx5_ib_bind_slave_port(dev, mpi);
5713 }
5714
5715 if (bound) {
5716 dev_dbg(&mpi->mdev->pdev->dev, "removing port from unaffiliated list.\n");
5717 mlx5_ib_dbg(dev, "port %d bound\n", i + 1);
5718 list_del(&mpi->list);
5719 break;
5720 }
5721 }
5722 if (!bound) {
5723 get_port_caps(dev, i + 1);
5724 mlx5_ib_dbg(dev, "no free port found for port %d\n",
5725 i + 1);
5726 }
5727 }
5728
5729 list_add_tail(&dev->ib_dev_list, &mlx5_ib_dev_list);
5730 mutex_unlock(&mlx5_ib_multiport_mutex);
5731 return err;
5732}
5733
5734static void mlx5_ib_cleanup_multiport_master(struct mlx5_ib_dev *dev)
5735{
5736 int port_num = mlx5_core_native_port_num(dev->mdev) - 1;
5737 enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev,
5738 port_num + 1);
5739 int i;
5740
5741 if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
5742 return;
5743
5744 mutex_lock(&mlx5_ib_multiport_mutex);
5745 for (i = 0; i < dev->num_ports; i++) {
5746 if (dev->port[i].mp.mpi) {
5747 /* Destroy the native port stub */
5748 if (i == port_num) {
5749 kfree(dev->port[i].mp.mpi);
5750 dev->port[i].mp.mpi = NULL;
5751 } else {
5752 mlx5_ib_dbg(dev, "unbinding port_num: %d\n", i + 1);
5753 mlx5_ib_unbind_slave_port(dev, dev->port[i].mp.mpi);
5754 }
5755 }
5756 }
5757
5758 mlx5_ib_dbg(dev, "removing from devlist\n");
5759 list_del(&dev->ib_dev_list);
5760 mutex_unlock(&mlx5_ib_multiport_mutex);
5761
5762 mlx5_nic_vport_disable_roce(dev->mdev);
5763}
5764
Jason Gunthorpe9a119cd2018-07-04 08:50:28 +03005765ADD_UVERBS_ATTRIBUTES_SIMPLE(
5766 mlx5_ib_dm,
5767 UVERBS_OBJECT_DM,
5768 UVERBS_METHOD_DM_ALLOC,
5769 UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_ALLOC_DM_RESP_START_OFFSET,
5770 UVERBS_ATTR_TYPE(u64),
Jason Gunthorpe83bb4442018-07-04 08:50:29 +03005771 UA_MANDATORY),
Jason Gunthorpe9a119cd2018-07-04 08:50:28 +03005772 UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_ALLOC_DM_RESP_PAGE_INDEX,
5773 UVERBS_ATTR_TYPE(u16),
Jason Gunthorpe83bb4442018-07-04 08:50:29 +03005774 UA_MANDATORY));
Ariel Levkovich24da0012018-04-05 18:53:27 +03005775
Jason Gunthorpe9a119cd2018-07-04 08:50:28 +03005776ADD_UVERBS_ATTRIBUTES_SIMPLE(
5777 mlx5_ib_flow_action,
5778 UVERBS_OBJECT_FLOW_ACTION,
5779 UVERBS_METHOD_FLOW_ACTION_ESP_CREATE,
Jason Gunthorpebccd0622018-07-26 16:37:14 -06005780 UVERBS_ATTR_FLAGS_IN(MLX5_IB_ATTR_CREATE_FLOW_ACTION_FLAGS,
5781 enum mlx5_ib_uapi_flow_action_flags));
Aviad Yehezkelc6475a02018-03-28 09:27:50 +03005782
Jason Gunthorpe0cbf4322018-11-12 22:59:50 +02005783static const struct uapi_definition mlx5_ib_defs[] = {
5784#if IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS)
Jason Gunthorpe36e235c2018-11-12 22:59:53 +02005785 UAPI_DEF_CHAIN(mlx5_ib_devx_defs),
Jason Gunthorpe0cbf4322018-11-12 22:59:50 +02005786 UAPI_DEF_CHAIN(mlx5_ib_flow_defs),
5787#endif
Matan Barak8c846602018-03-28 09:27:41 +03005788
Jason Gunthorpe0cbf4322018-11-12 22:59:50 +02005789 UAPI_DEF_CHAIN_OBJ_TREE(UVERBS_OBJECT_FLOW_ACTION,
5790 &mlx5_ib_flow_action),
5791 UAPI_DEF_CHAIN_OBJ_TREE(UVERBS_OBJECT_DM, &mlx5_ib_dm),
5792 {}
5793};
Matan Barak8c846602018-03-28 09:27:41 +03005794
Raed Salem1a1e03d2018-05-31 16:43:41 +03005795static int mlx5_ib_read_counters(struct ib_counters *counters,
5796 struct ib_counters_read_attr *read_attr,
5797 struct uverbs_attr_bundle *attrs)
5798{
5799 struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
5800 struct mlx5_read_counters_attr mread_attr = {};
5801 struct mlx5_ib_flow_counters_desc *desc;
5802 int ret, i;
5803
5804 mutex_lock(&mcounters->mcntrs_mutex);
5805 if (mcounters->cntrs_max_index > read_attr->ncounters) {
5806 ret = -EINVAL;
5807 goto err_bound;
5808 }
5809
5810 mread_attr.out = kcalloc(mcounters->counters_num, sizeof(u64),
5811 GFP_KERNEL);
5812 if (!mread_attr.out) {
5813 ret = -ENOMEM;
5814 goto err_bound;
5815 }
5816
5817 mread_attr.hw_cntrs_hndl = mcounters->hw_cntrs_hndl;
5818 mread_attr.flags = read_attr->flags;
5819 ret = mcounters->read_counters(counters->device, &mread_attr);
5820 if (ret)
5821 goto err_read;
5822
5823 /* do the pass over the counters data array to assign according to the
5824 * descriptions and indexing pairs
5825 */
5826 desc = mcounters->counters_data;
5827 for (i = 0; i < mcounters->ncounters; i++)
5828 read_attr->counters_buff[desc[i].index] += mread_attr.out[desc[i].description];
5829
5830err_read:
5831 kfree(mread_attr.out);
5832err_bound:
5833 mutex_unlock(&mcounters->mcntrs_mutex);
5834 return ret;
5835}
5836
Raed Salemb29e2a12018-05-31 16:43:38 +03005837static int mlx5_ib_destroy_counters(struct ib_counters *counters)
5838{
5839 struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
5840
Raed Salem3b3233f2018-05-31 16:43:39 +03005841 counters_clear_description(counters);
5842 if (mcounters->hw_cntrs_hndl)
5843 mlx5_fc_destroy(to_mdev(counters->device)->mdev,
5844 mcounters->hw_cntrs_hndl);
5845
Raed Salemb29e2a12018-05-31 16:43:38 +03005846 kfree(mcounters);
5847
5848 return 0;
5849}
5850
5851static struct ib_counters *mlx5_ib_create_counters(struct ib_device *device,
5852 struct uverbs_attr_bundle *attrs)
5853{
5854 struct mlx5_ib_mcounters *mcounters;
5855
5856 mcounters = kzalloc(sizeof(*mcounters), GFP_KERNEL);
5857 if (!mcounters)
5858 return ERR_PTR(-ENOMEM);
5859
Raed Salem3b3233f2018-05-31 16:43:39 +03005860 mutex_init(&mcounters->mcntrs_mutex);
5861
Raed Salemb29e2a12018-05-31 16:43:38 +03005862 return &mcounters->ibcntrs;
5863}
5864
Mark Blochb5ca15a2018-01-23 11:16:30 +00005865void mlx5_ib_stage_init_cleanup(struct mlx5_ib_dev *dev)
Eli Cohene126ba92013-07-07 17:25:49 +03005866{
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005867 mlx5_ib_cleanup_multiport_master(dev);
Leon Romanovsky13859d5d2019-01-08 16:07:26 +02005868 if (IS_ENABLED(CONFIG_INFINIBAND_ON_DEMAND_PAGING)) {
Yishai Hadas534fd7a2019-01-13 16:01:17 +02005869 srcu_barrier(&dev->mr_srcu);
Leon Romanovsky13859d5d2019-01-08 16:07:26 +02005870 cleanup_srcu_struct(&dev->mr_srcu);
Leon Romanovsky13859d5d2019-01-08 16:07:26 +02005871 }
Mark Bloch16c19752018-01-01 13:06:58 +02005872}
5873
Mark Blochb5ca15a2018-01-23 11:16:30 +00005874int mlx5_ib_stage_init_init(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02005875{
5876 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03005877 int err;
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005878 int i;
Eli Cohene126ba92013-07-07 17:25:49 +03005879
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005880 for (i = 0; i < dev->num_ports; i++) {
5881 spin_lock_init(&dev->port[i].mp.mpi_lock);
Mark Bloch95579e72019-03-28 15:27:33 +02005882 rwlock_init(&dev->port[i].roce.netdev_lock);
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005883 }
5884
5885 err = mlx5_ib_init_multiport_master(dev);
Eli Cohene126ba92013-07-07 17:25:49 +03005886 if (err)
Mark Blochda796cc2019-03-28 15:27:35 +02005887 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03005888
Mark Blocha989ea02019-03-28 15:27:40 +02005889 err = set_has_smi_cap(dev);
5890 if (err)
5891 return err;
5892
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005893 if (!mlx5_core_mp_enabled(mdev)) {
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005894 for (i = 1; i <= dev->num_ports; i++) {
5895 err = get_port_caps(dev, i);
5896 if (err)
5897 break;
5898 }
5899 } else {
5900 err = get_port_caps(dev, mlx5_core_native_port_num(mdev));
5901 }
5902 if (err)
5903 goto err_mp;
5904
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03005905 if (mlx5_use_mad_ifc(dev))
5906 get_ext_port_caps(dev);
Eli Cohene126ba92013-07-07 17:25:49 +03005907
Eli Cohene126ba92013-07-07 17:25:49 +03005908 dev->ib_dev.owner = THIS_MODULE;
5909 dev->ib_dev.node_type = RDMA_NODE_IB_CA;
Sagi Grimbergc6790aa2015-09-24 10:34:23 +03005910 dev->ib_dev.local_dma_lkey = 0 /* not supported for now */;
Daniel Jurgens508562d2018-01-04 17:25:34 +02005911 dev->ib_dev.phys_port_cnt = dev->num_ports;
Saeed Mahameedf2f3df52018-11-19 10:52:38 -08005912 dev->ib_dev.num_comp_vectors = mlx5_comp_vectors_count(mdev);
Bart Van Assche9b0c2892017-01-20 13:04:21 -08005913 dev->ib_dev.dev.parent = &mdev->pdev->dev;
Eli Cohene126ba92013-07-07 17:25:49 +03005914
Mark Bloch3cc297d2018-01-01 13:07:03 +02005915 mutex_init(&dev->cap_mask_mutex);
5916 INIT_LIST_HEAD(&dev->qp_list);
5917 spin_lock_init(&dev->reset_flow_resource_lock);
5918
Ariel Levkovich24da0012018-04-05 18:53:27 +03005919 spin_lock_init(&dev->memic.memic_lock);
5920 dev->memic.dev = mdev;
5921
Leon Romanovsky13859d5d2019-01-08 16:07:26 +02005922 if (IS_ENABLED(CONFIG_INFINIBAND_ON_DEMAND_PAGING)) {
Leon Romanovsky13859d5d2019-01-08 16:07:26 +02005923 err = init_srcu_struct(&dev->mr_srcu);
Moni Shouaa6bc3872019-02-17 16:08:22 +02005924 if (err)
Leon Romanovsky13859d5d2019-01-08 16:07:26 +02005925 goto err_mp;
Jason Gunthorpe623d1542018-12-20 16:39:26 -07005926 }
Mark Bloch3cc297d2018-01-01 13:07:03 +02005927
Mark Bloch16c19752018-01-01 13:06:58 +02005928 return 0;
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005929err_mp:
5930 mlx5_ib_cleanup_multiport_master(dev);
Mark Bloch16c19752018-01-01 13:06:58 +02005931
Mark Bloch16c19752018-01-01 13:06:58 +02005932 return -ENOMEM;
5933}
5934
Mark Bloch9a4ca382018-01-16 14:42:35 +00005935static int mlx5_ib_stage_flow_db_init(struct mlx5_ib_dev *dev)
5936{
5937 dev->flow_db = kzalloc(sizeof(*dev->flow_db), GFP_KERNEL);
5938
5939 if (!dev->flow_db)
5940 return -ENOMEM;
5941
5942 mutex_init(&dev->flow_db->lock);
5943
5944 return 0;
5945}
5946
Mark Blochb5ca15a2018-01-23 11:16:30 +00005947int mlx5_ib_stage_rep_flow_db_init(struct mlx5_ib_dev *dev)
5948{
5949 struct mlx5_ib_dev *nic_dev;
5950
5951 nic_dev = mlx5_ib_get_uplink_ibdev(dev->mdev->priv.eswitch);
5952
5953 if (!nic_dev)
5954 return -EINVAL;
5955
5956 dev->flow_db = nic_dev->flow_db;
5957
5958 return 0;
5959}
5960
Mark Bloch9a4ca382018-01-16 14:42:35 +00005961static void mlx5_ib_stage_flow_db_cleanup(struct mlx5_ib_dev *dev)
5962{
5963 kfree(dev->flow_db);
5964}
5965
Kamal Heib96458233e2018-12-10 21:09:38 +02005966static const struct ib_device_ops mlx5_ib_dev_ops = {
5967 .add_gid = mlx5_ib_add_gid,
5968 .alloc_mr = mlx5_ib_alloc_mr,
5969 .alloc_pd = mlx5_ib_alloc_pd,
5970 .alloc_ucontext = mlx5_ib_alloc_ucontext,
5971 .attach_mcast = mlx5_ib_mcg_attach,
5972 .check_mr_status = mlx5_ib_check_mr_status,
5973 .create_ah = mlx5_ib_create_ah,
5974 .create_counters = mlx5_ib_create_counters,
5975 .create_cq = mlx5_ib_create_cq,
5976 .create_flow = mlx5_ib_create_flow,
5977 .create_qp = mlx5_ib_create_qp,
5978 .create_srq = mlx5_ib_create_srq,
5979 .dealloc_pd = mlx5_ib_dealloc_pd,
5980 .dealloc_ucontext = mlx5_ib_dealloc_ucontext,
5981 .del_gid = mlx5_ib_del_gid,
5982 .dereg_mr = mlx5_ib_dereg_mr,
5983 .destroy_ah = mlx5_ib_destroy_ah,
5984 .destroy_counters = mlx5_ib_destroy_counters,
5985 .destroy_cq = mlx5_ib_destroy_cq,
5986 .destroy_flow = mlx5_ib_destroy_flow,
5987 .destroy_flow_action = mlx5_ib_destroy_flow_action,
5988 .destroy_qp = mlx5_ib_destroy_qp,
5989 .destroy_srq = mlx5_ib_destroy_srq,
5990 .detach_mcast = mlx5_ib_mcg_detach,
5991 .disassociate_ucontext = mlx5_ib_disassociate_ucontext,
5992 .drain_rq = mlx5_ib_drain_rq,
5993 .drain_sq = mlx5_ib_drain_sq,
5994 .get_dev_fw_str = get_dev_fw_str,
5995 .get_dma_mr = mlx5_ib_get_dma_mr,
5996 .get_link_layer = mlx5_ib_port_link_layer,
5997 .map_mr_sg = mlx5_ib_map_mr_sg,
5998 .mmap = mlx5_ib_mmap,
5999 .modify_cq = mlx5_ib_modify_cq,
6000 .modify_device = mlx5_ib_modify_device,
6001 .modify_port = mlx5_ib_modify_port,
6002 .modify_qp = mlx5_ib_modify_qp,
6003 .modify_srq = mlx5_ib_modify_srq,
6004 .poll_cq = mlx5_ib_poll_cq,
6005 .post_recv = mlx5_ib_post_recv,
6006 .post_send = mlx5_ib_post_send,
6007 .post_srq_recv = mlx5_ib_post_srq_recv,
6008 .process_mad = mlx5_ib_process_mad,
6009 .query_ah = mlx5_ib_query_ah,
6010 .query_device = mlx5_ib_query_device,
6011 .query_gid = mlx5_ib_query_gid,
6012 .query_pkey = mlx5_ib_query_pkey,
6013 .query_qp = mlx5_ib_query_qp,
6014 .query_srq = mlx5_ib_query_srq,
6015 .read_counters = mlx5_ib_read_counters,
6016 .reg_user_mr = mlx5_ib_reg_user_mr,
6017 .req_notify_cq = mlx5_ib_arm_cq,
6018 .rereg_user_mr = mlx5_ib_rereg_user_mr,
6019 .resize_cq = mlx5_ib_resize_cq,
Leon Romanovskyd3456912019-04-03 16:42:42 +03006020
6021 INIT_RDMA_OBJ_SIZE(ib_ah, mlx5_ib_ah, ibah),
Leon Romanovsky21a428a2019-02-03 14:55:51 +02006022 INIT_RDMA_OBJ_SIZE(ib_pd, mlx5_ib_pd, ibpd),
Leon Romanovsky68e326d2019-04-03 16:42:43 +03006023 INIT_RDMA_OBJ_SIZE(ib_srq, mlx5_ib_srq, ibsrq),
Leon Romanovskya2a074e2019-02-12 20:39:16 +02006024 INIT_RDMA_OBJ_SIZE(ib_ucontext, mlx5_ib_ucontext, ibucontext),
Kamal Heib96458233e2018-12-10 21:09:38 +02006025};
6026
6027static const struct ib_device_ops mlx5_ib_dev_flow_ipsec_ops = {
6028 .create_flow_action_esp = mlx5_ib_create_flow_action_esp,
6029 .modify_flow_action_esp = mlx5_ib_modify_flow_action_esp,
6030};
6031
6032static const struct ib_device_ops mlx5_ib_dev_ipoib_enhanced_ops = {
6033 .rdma_netdev_get_params = mlx5_ib_rn_get_params,
6034};
6035
6036static const struct ib_device_ops mlx5_ib_dev_sriov_ops = {
6037 .get_vf_config = mlx5_ib_get_vf_config,
6038 .get_vf_stats = mlx5_ib_get_vf_stats,
6039 .set_vf_guid = mlx5_ib_set_vf_guid,
6040 .set_vf_link_state = mlx5_ib_set_vf_link_state,
6041};
6042
6043static const struct ib_device_ops mlx5_ib_dev_mw_ops = {
6044 .alloc_mw = mlx5_ib_alloc_mw,
6045 .dealloc_mw = mlx5_ib_dealloc_mw,
6046};
6047
6048static const struct ib_device_ops mlx5_ib_dev_xrc_ops = {
6049 .alloc_xrcd = mlx5_ib_alloc_xrcd,
6050 .dealloc_xrcd = mlx5_ib_dealloc_xrcd,
6051};
6052
6053static const struct ib_device_ops mlx5_ib_dev_dm_ops = {
6054 .alloc_dm = mlx5_ib_alloc_dm,
6055 .dealloc_dm = mlx5_ib_dealloc_dm,
6056 .reg_dm_mr = mlx5_ib_reg_dm_mr,
6057};
6058
Mark Blochb5ca15a2018-01-23 11:16:30 +00006059int mlx5_ib_stage_caps_init(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02006060{
6061 struct mlx5_core_dev *mdev = dev->mdev;
Mark Bloch16c19752018-01-01 13:06:58 +02006062 int err;
6063
Eli Cohene126ba92013-07-07 17:25:49 +03006064 dev->ib_dev.uverbs_abi_ver = MLX5_IB_UVERBS_ABI_VERSION;
6065 dev->ib_dev.uverbs_cmd_mask =
6066 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
6067 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
6068 (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
6069 (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
6070 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
Moni Shoua41c450f2016-11-23 08:23:26 +02006071 (1ull << IB_USER_VERBS_CMD_CREATE_AH) |
6072 (1ull << IB_USER_VERBS_CMD_DESTROY_AH) |
Eli Cohene126ba92013-07-07 17:25:49 +03006073 (1ull << IB_USER_VERBS_CMD_REG_MR) |
Noa Osherovich56e11d62016-02-29 16:46:51 +02006074 (1ull << IB_USER_VERBS_CMD_REREG_MR) |
Eli Cohene126ba92013-07-07 17:25:49 +03006075 (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
6076 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
6077 (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
6078 (1ull << IB_USER_VERBS_CMD_RESIZE_CQ) |
6079 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
6080 (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
6081 (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
6082 (1ull << IB_USER_VERBS_CMD_QUERY_QP) |
6083 (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
6084 (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST) |
6085 (1ull << IB_USER_VERBS_CMD_DETACH_MCAST) |
6086 (1ull << IB_USER_VERBS_CMD_CREATE_SRQ) |
6087 (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ) |
6088 (1ull << IB_USER_VERBS_CMD_QUERY_SRQ) |
6089 (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ) |
6090 (1ull << IB_USER_VERBS_CMD_CREATE_XSRQ) |
6091 (1ull << IB_USER_VERBS_CMD_OPEN_QP);
Haggai Eran1707cb42015-02-08 13:28:52 +02006092 dev->ib_dev.uverbs_ex_cmd_mask =
Matan Barakd4584dd2016-01-28 17:51:46 +02006093 (1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE) |
6094 (1ull << IB_USER_VERBS_EX_CMD_CREATE_CQ) |
Bodong Wang7d29f342016-12-01 13:43:16 +02006095 (1ull << IB_USER_VERBS_EX_CMD_CREATE_QP) |
Yonatan Cohenb0e9df62017-11-13 10:51:15 +02006096 (1ull << IB_USER_VERBS_EX_CMD_MODIFY_QP) |
Kamal Heib96458233e2018-12-10 21:09:38 +02006097 (1ull << IB_USER_VERBS_EX_CMD_MODIFY_CQ) |
6098 (1ull << IB_USER_VERBS_EX_CMD_CREATE_FLOW) |
6099 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_FLOW);
Eli Cohene126ba92013-07-07 17:25:49 +03006100
Denis Drozdovf6a8a192018-08-14 14:08:51 +03006101 if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads) &&
6102 IS_ENABLED(CONFIG_MLX5_CORE_IPOIB))
Kamal Heib96458233e2018-12-10 21:09:38 +02006103 ib_set_device_ops(&dev->ib_dev,
6104 &mlx5_ib_dev_ipoib_enhanced_ops);
Niranjana Vishwanathapura8e959602017-06-30 13:14:46 -07006105
Kamal Heib96458233e2018-12-10 21:09:38 +02006106 if (mlx5_core_is_pf(mdev))
6107 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_sriov_ops);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03006108
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03006109 dev->umr_fence = mlx5_get_umr_fence(MLX5_CAP_GEN(mdev, umr_fence));
6110
Matan Barakd2370e02016-02-29 18:05:30 +02006111 if (MLX5_CAP_GEN(mdev, imaicl)) {
Matan Barakd2370e02016-02-29 18:05:30 +02006112 dev->ib_dev.uverbs_cmd_mask |=
6113 (1ull << IB_USER_VERBS_CMD_ALLOC_MW) |
6114 (1ull << IB_USER_VERBS_CMD_DEALLOC_MW);
Kamal Heib96458233e2018-12-10 21:09:38 +02006115 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_mw_ops);
Matan Barakd2370e02016-02-29 18:05:30 +02006116 }
6117
Saeed Mahameed938fe832015-05-28 22:28:41 +03006118 if (MLX5_CAP_GEN(mdev, xrc)) {
Eli Cohene126ba92013-07-07 17:25:49 +03006119 dev->ib_dev.uverbs_cmd_mask |=
6120 (1ull << IB_USER_VERBS_CMD_OPEN_XRCD) |
6121 (1ull << IB_USER_VERBS_CMD_CLOSE_XRCD);
Kamal Heib96458233e2018-12-10 21:09:38 +02006122 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_xrc_ops);
Eli Cohene126ba92013-07-07 17:25:49 +03006123 }
6124
Kamal Heib96458233e2018-12-10 21:09:38 +02006125 if (MLX5_CAP_DEV_MEM(mdev, memic))
6126 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_dm_ops);
Ariel Levkovich24da0012018-04-05 18:53:27 +03006127
Jason Gunthorpedfb631a2018-11-12 22:59:49 +02006128 if (mlx5_accel_ipsec_device_caps(dev->mdev) &
Kamal Heib96458233e2018-12-10 21:09:38 +02006129 MLX5_ACCEL_IPSEC_CAP_DEVICE)
6130 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_flow_ipsec_ops);
Matan Barak0ede73b2018-03-19 15:02:34 +02006131 dev->ib_dev.driver_id = RDMA_DRIVER_MLX5;
Kamal Heib96458233e2018-12-10 21:09:38 +02006132 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_ops);
Yishai Hadas81e30882017-06-08 16:15:09 +03006133
Jason Gunthorpe36e235c2018-11-12 22:59:53 +02006134 if (IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS))
6135 dev->ib_dev.driver_def = mlx5_ib_defs;
Eli Cohene126ba92013-07-07 17:25:49 +03006136
6137 err = init_node_data(dev);
6138 if (err)
Mark Bloch16c19752018-01-01 13:06:58 +02006139 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03006140
Mark Blochc8b89922018-01-01 13:07:02 +02006141 if ((MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_ETH) &&
Jason Gunthorpee7996a92018-01-29 13:26:40 -07006142 (MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) ||
6143 MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
Mark Blocha560f1d2018-09-17 13:30:47 +03006144 mutex_init(&dev->lb.mutex);
Mark Blochc8b89922018-01-01 13:07:02 +02006145
Mark Bloch16c19752018-01-01 13:06:58 +02006146 return 0;
6147}
6148
Kamal Heib96458233e2018-12-10 21:09:38 +02006149static const struct ib_device_ops mlx5_ib_dev_port_ops = {
6150 .get_port_immutable = mlx5_port_immutable,
6151 .query_port = mlx5_ib_query_port,
6152};
6153
Mark Bloch8e6efa32017-11-06 12:22:13 +00006154static int mlx5_ib_stage_non_default_cb(struct mlx5_ib_dev *dev)
6155{
Kamal Heib96458233e2018-12-10 21:09:38 +02006156 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_port_ops);
Mark Bloch8e6efa32017-11-06 12:22:13 +00006157 return 0;
6158}
6159
Kamal Heib96458233e2018-12-10 21:09:38 +02006160static const struct ib_device_ops mlx5_ib_dev_port_rep_ops = {
6161 .get_port_immutable = mlx5_port_rep_immutable,
6162 .query_port = mlx5_ib_rep_query_port,
6163};
6164
Mark Blochb5ca15a2018-01-23 11:16:30 +00006165int mlx5_ib_stage_rep_non_default_cb(struct mlx5_ib_dev *dev)
Mark Bloch8e6efa32017-11-06 12:22:13 +00006166{
Kamal Heib96458233e2018-12-10 21:09:38 +02006167 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_port_rep_ops);
Mark Bloch8e6efa32017-11-06 12:22:13 +00006168 return 0;
6169}
6170
Kamal Heib96458233e2018-12-10 21:09:38 +02006171static const struct ib_device_ops mlx5_ib_dev_common_roce_ops = {
6172 .create_rwq_ind_table = mlx5_ib_create_rwq_ind_table,
6173 .create_wq = mlx5_ib_create_wq,
6174 .destroy_rwq_ind_table = mlx5_ib_destroy_rwq_ind_table,
6175 .destroy_wq = mlx5_ib_destroy_wq,
6176 .get_netdev = mlx5_ib_get_netdev,
6177 .modify_wq = mlx5_ib_modify_wq,
6178};
6179
Leon Romanovskye3f1ed12018-07-08 12:55:43 +03006180static int mlx5_ib_stage_common_roce_init(struct mlx5_ib_dev *dev)
Mark Bloch8e6efa32017-11-06 12:22:13 +00006181{
Leon Romanovskye3f1ed12018-07-08 12:55:43 +03006182 u8 port_num;
Mark Bloch8e6efa32017-11-06 12:22:13 +00006183 int i;
6184
6185 for (i = 0; i < dev->num_ports; i++) {
Mark Bloch95579e72019-03-28 15:27:33 +02006186 dev->port[i].roce.dev = dev;
6187 dev->port[i].roce.native_port_num = i + 1;
6188 dev->port[i].roce.last_port_state = IB_PORT_DOWN;
Mark Bloch8e6efa32017-11-06 12:22:13 +00006189 }
6190
Mark Bloch8e6efa32017-11-06 12:22:13 +00006191 dev->ib_dev.uverbs_ex_cmd_mask |=
6192 (1ull << IB_USER_VERBS_EX_CMD_CREATE_WQ) |
6193 (1ull << IB_USER_VERBS_EX_CMD_MODIFY_WQ) |
6194 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_WQ) |
6195 (1ull << IB_USER_VERBS_EX_CMD_CREATE_RWQ_IND_TBL) |
6196 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_RWQ_IND_TBL);
Kamal Heib96458233e2018-12-10 21:09:38 +02006197 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_common_roce_ops);
Mark Bloch8e6efa32017-11-06 12:22:13 +00006198
Leon Romanovskye3f1ed12018-07-08 12:55:43 +03006199 port_num = mlx5_core_native_port_num(dev->mdev) - 1;
6200
Mark Bloch8e6efa32017-11-06 12:22:13 +00006201 return mlx5_add_netdev_notifier(dev, port_num);
6202}
6203
6204static void mlx5_ib_stage_common_roce_cleanup(struct mlx5_ib_dev *dev)
6205{
6206 u8 port_num = mlx5_core_native_port_num(dev->mdev) - 1;
6207
6208 mlx5_remove_netdev_notifier(dev, port_num);
6209}
6210
6211int mlx5_ib_stage_rep_roce_init(struct mlx5_ib_dev *dev)
6212{
6213 struct mlx5_core_dev *mdev = dev->mdev;
6214 enum rdma_link_layer ll;
6215 int port_type_cap;
6216 int err = 0;
Mark Bloch8e6efa32017-11-06 12:22:13 +00006217
Mark Bloch8e6efa32017-11-06 12:22:13 +00006218 port_type_cap = MLX5_CAP_GEN(mdev, port_type);
6219 ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
6220
6221 if (ll == IB_LINK_LAYER_ETHERNET)
Leon Romanovskye3f1ed12018-07-08 12:55:43 +03006222 err = mlx5_ib_stage_common_roce_init(dev);
Mark Bloch8e6efa32017-11-06 12:22:13 +00006223
6224 return err;
6225}
6226
6227void mlx5_ib_stage_rep_roce_cleanup(struct mlx5_ib_dev *dev)
6228{
6229 mlx5_ib_stage_common_roce_cleanup(dev);
6230}
6231
Mark Bloch16c19752018-01-01 13:06:58 +02006232static int mlx5_ib_stage_roce_init(struct mlx5_ib_dev *dev)
6233{
6234 struct mlx5_core_dev *mdev = dev->mdev;
6235 enum rdma_link_layer ll;
6236 int port_type_cap;
6237 int err;
6238
6239 port_type_cap = MLX5_CAP_GEN(mdev, port_type);
6240 ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
6241
Achiad Shochatfc24fc52015-12-23 18:47:17 +02006242 if (ll == IB_LINK_LAYER_ETHERNET) {
Leon Romanovskye3f1ed12018-07-08 12:55:43 +03006243 err = mlx5_ib_stage_common_roce_init(dev);
Achiad Shochatfc24fc52015-12-23 18:47:17 +02006244 if (err)
Mark Bloch16c19752018-01-01 13:06:58 +02006245 return err;
Mark Bloch8e6efa32017-11-06 12:22:13 +00006246
Leon Romanovskye3f1ed12018-07-08 12:55:43 +03006247 err = mlx5_enable_eth(dev);
Mark Bloch8e6efa32017-11-06 12:22:13 +00006248 if (err)
6249 goto cleanup;
Achiad Shochatfc24fc52015-12-23 18:47:17 +02006250 }
6251
Mark Bloch16c19752018-01-01 13:06:58 +02006252 return 0;
Mark Bloch8e6efa32017-11-06 12:22:13 +00006253cleanup:
6254 mlx5_ib_stage_common_roce_cleanup(dev);
6255
6256 return err;
Mark Bloch16c19752018-01-01 13:06:58 +02006257}
Eli Cohene126ba92013-07-07 17:25:49 +03006258
Mark Bloch16c19752018-01-01 13:06:58 +02006259static void mlx5_ib_stage_roce_cleanup(struct mlx5_ib_dev *dev)
6260{
6261 struct mlx5_core_dev *mdev = dev->mdev;
6262 enum rdma_link_layer ll;
6263 int port_type_cap;
Eli Cohene126ba92013-07-07 17:25:49 +03006264
Mark Bloch16c19752018-01-01 13:06:58 +02006265 port_type_cap = MLX5_CAP_GEN(mdev, port_type);
6266 ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
6267
6268 if (ll == IB_LINK_LAYER_ETHERNET) {
6269 mlx5_disable_eth(dev);
Mark Bloch8e6efa32017-11-06 12:22:13 +00006270 mlx5_ib_stage_common_roce_cleanup(dev);
Kamal Heib45bded22017-01-18 14:10:32 +02006271 }
Mark Bloch16c19752018-01-01 13:06:58 +02006272}
Haggai Eran6aec21f2014-12-11 17:04:23 +02006273
Mark Blochb5ca15a2018-01-23 11:16:30 +00006274int mlx5_ib_stage_dev_res_init(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02006275{
6276 return create_dev_resources(&dev->devr);
6277}
Parav Pandit4a2da0b2017-05-30 10:05:15 +03006278
Mark Blochb5ca15a2018-01-23 11:16:30 +00006279void mlx5_ib_stage_dev_res_cleanup(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02006280{
6281 destroy_dev_resources(&dev->devr);
6282}
6283
6284static int mlx5_ib_stage_odp_init(struct mlx5_ib_dev *dev)
6285{
Mark Bloch07321b32018-01-01 13:07:00 +02006286 mlx5_ib_internal_fill_odp_caps(dev);
6287
Mark Bloch16c19752018-01-01 13:06:58 +02006288 return mlx5_ib_odp_init_one(dev);
6289}
6290
Kamal Heibf3ffed02019-01-30 16:13:42 +02006291static void mlx5_ib_stage_odp_cleanup(struct mlx5_ib_dev *dev)
Saeed Mahameedd5d284b2018-11-19 10:52:41 -08006292{
6293 mlx5_ib_odp_cleanup_one(dev);
6294}
6295
Kamal Heib96458233e2018-12-10 21:09:38 +02006296static const struct ib_device_ops mlx5_ib_dev_hw_stats_ops = {
6297 .alloc_hw_stats = mlx5_ib_alloc_hw_stats,
6298 .get_hw_stats = mlx5_ib_get_hw_stats,
6299};
6300
Mark Blochb5ca15a2018-01-23 11:16:30 +00006301int mlx5_ib_stage_counters_init(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02006302{
Mark Bloch5e1e7612018-01-01 13:07:01 +02006303 if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt)) {
Kamal Heib96458233e2018-12-10 21:09:38 +02006304 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_hw_stats_ops);
Mark Bloch5e1e7612018-01-01 13:07:01 +02006305
6306 return mlx5_ib_alloc_counters(dev);
6307 }
Mark Bloch16c19752018-01-01 13:06:58 +02006308
6309 return 0;
6310}
6311
Mark Blochb5ca15a2018-01-23 11:16:30 +00006312void mlx5_ib_stage_counters_cleanup(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02006313{
6314 if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt))
6315 mlx5_ib_dealloc_counters(dev);
6316}
6317
6318static int mlx5_ib_stage_cong_debugfs_init(struct mlx5_ib_dev *dev)
6319{
Greg Kroah-Hartman73eb8f02019-01-22 16:17:57 +01006320 mlx5_ib_init_cong_debugfs(dev,
6321 mlx5_core_native_port_num(dev->mdev) - 1);
6322 return 0;
Mark Bloch16c19752018-01-01 13:06:58 +02006323}
6324
6325static void mlx5_ib_stage_cong_debugfs_cleanup(struct mlx5_ib_dev *dev)
6326{
Parav Pandita9e546e2018-01-04 17:25:39 +02006327 mlx5_ib_cleanup_cong_debugfs(dev,
6328 mlx5_core_native_port_num(dev->mdev) - 1);
Mark Bloch16c19752018-01-01 13:06:58 +02006329}
6330
6331static int mlx5_ib_stage_uar_init(struct mlx5_ib_dev *dev)
6332{
Eli Cohen5fe9dec2017-01-03 23:55:25 +02006333 dev->mdev->priv.uar = mlx5_get_uars_page(dev->mdev);
Leon Romanovsky444261c2018-04-23 17:01:56 +03006334 return PTR_ERR_OR_ZERO(dev->mdev->priv.uar);
Mark Bloch16c19752018-01-01 13:06:58 +02006335}
6336
6337static void mlx5_ib_stage_uar_cleanup(struct mlx5_ib_dev *dev)
6338{
6339 mlx5_put_uars_page(dev->mdev, dev->mdev->priv.uar);
6340}
6341
Mark Blochb5ca15a2018-01-23 11:16:30 +00006342int mlx5_ib_stage_bfrag_init(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02006343{
6344 int err;
Eli Cohen5fe9dec2017-01-03 23:55:25 +02006345
6346 err = mlx5_alloc_bfreg(dev->mdev, &dev->bfreg, false, false);
6347 if (err)
Mark Bloch16c19752018-01-01 13:06:58 +02006348 return err;
Eli Cohen5fe9dec2017-01-03 23:55:25 +02006349
6350 err = mlx5_alloc_bfreg(dev->mdev, &dev->fp_bfreg, false, true);
6351 if (err)
Mark Bloch16c19752018-01-01 13:06:58 +02006352 mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
Eli Cohen5fe9dec2017-01-03 23:55:25 +02006353
Mark Bloch16c19752018-01-01 13:06:58 +02006354 return err;
6355}
Mark Bloch0837e862016-06-17 15:10:55 +03006356
Mark Blochb5ca15a2018-01-23 11:16:30 +00006357void mlx5_ib_stage_bfrag_cleanup(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02006358{
6359 mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
6360 mlx5_free_bfreg(dev->mdev, &dev->bfreg);
6361}
Eli Cohene126ba92013-07-07 17:25:49 +03006362
Mark Blochb5ca15a2018-01-23 11:16:30 +00006363int mlx5_ib_stage_ib_reg_init(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02006364{
Jason Gunthorpee349f852018-09-25 16:58:09 -06006365 const char *name;
6366
Parav Pandit508a5232018-10-11 22:31:54 +03006367 rdma_set_device_sysfs_group(&dev->ib_dev, &mlx5_attr_group);
Aviv Heller7c34ec12018-08-23 13:47:53 +03006368 if (!mlx5_lag_is_roce(dev->mdev))
Jason Gunthorpee349f852018-09-25 16:58:09 -06006369 name = "mlx5_%d";
6370 else
6371 name = "mlx5_bond_%d";
Parav Panditea4baf72018-12-18 14:28:30 +02006372 return ib_register_device(&dev->ib_dev, name);
Mark Bloch16c19752018-01-01 13:06:58 +02006373}
6374
David S. Miller03fe2de2018-03-23 11:24:57 -04006375void mlx5_ib_stage_pre_ib_reg_umr_cleanup(struct mlx5_ib_dev *dev)
Mark Bloch42cea832018-03-14 09:14:15 +02006376{
6377 destroy_umrc_res(dev);
6378}
6379
Mark Blochb5ca15a2018-01-23 11:16:30 +00006380void mlx5_ib_stage_ib_reg_cleanup(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02006381{
6382 ib_unregister_device(&dev->ib_dev);
6383}
6384
David S. Miller03fe2de2018-03-23 11:24:57 -04006385int mlx5_ib_stage_post_ib_reg_umr_init(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02006386{
6387 return create_umr_res(dev);
6388}
6389
Mark Bloch16c19752018-01-01 13:06:58 +02006390static int mlx5_ib_stage_delay_drop_init(struct mlx5_ib_dev *dev)
6391{
Maor Gottlieb03404e82017-05-30 10:29:13 +03006392 init_delay_drop(dev);
6393
Mark Bloch16c19752018-01-01 13:06:58 +02006394 return 0;
6395}
6396
6397static void mlx5_ib_stage_delay_drop_cleanup(struct mlx5_ib_dev *dev)
6398{
6399 cancel_delay_drop(dev);
6400}
6401
Saeed Mahameeddf097a22018-11-26 14:39:00 -08006402static int mlx5_ib_stage_dev_notifier_init(struct mlx5_ib_dev *dev)
6403{
6404 dev->mdev_events.notifier_call = mlx5_ib_event;
6405 mlx5_notifier_register(dev->mdev, &dev->mdev_events);
6406 return 0;
6407}
6408
6409static void mlx5_ib_stage_dev_notifier_cleanup(struct mlx5_ib_dev *dev)
6410{
6411 mlx5_notifier_unregister(dev->mdev, &dev->mdev_events);
6412}
6413
Leon Romanovsky81773ce2018-11-28 20:53:39 +02006414static int mlx5_ib_stage_devx_init(struct mlx5_ib_dev *dev)
6415{
6416 int uid;
6417
Yishai Hadasfb981532018-11-26 08:28:36 +02006418 uid = mlx5_ib_devx_create(dev, false);
Leon Romanovsky81773ce2018-11-28 20:53:39 +02006419 if (uid > 0)
6420 dev->devx_whitelist_uid = uid;
6421
6422 return 0;
6423}
6424static void mlx5_ib_stage_devx_cleanup(struct mlx5_ib_dev *dev)
6425{
6426 if (dev->devx_whitelist_uid)
6427 mlx5_ib_devx_destroy(dev, dev->devx_whitelist_uid);
6428}
6429
Mark Blochb5ca15a2018-01-23 11:16:30 +00006430void __mlx5_ib_remove(struct mlx5_ib_dev *dev,
6431 const struct mlx5_ib_profile *profile,
6432 int stage)
Mark Bloch16c19752018-01-01 13:06:58 +02006433{
6434 /* Number of stages to cleanup */
6435 while (stage) {
6436 stage--;
6437 if (profile->stage[stage].cleanup)
6438 profile->stage[stage].cleanup(dev);
6439 }
Mark Bloch4a6dc852019-03-28 15:27:34 +02006440
Mark Blochda796cc2019-03-28 15:27:35 +02006441 kfree(dev->port);
Mark Bloch4a6dc852019-03-28 15:27:34 +02006442 ib_dealloc_device(&dev->ib_dev);
Mark Bloch16c19752018-01-01 13:06:58 +02006443}
6444
Mark Blochb5ca15a2018-01-23 11:16:30 +00006445void *__mlx5_ib_add(struct mlx5_ib_dev *dev,
6446 const struct mlx5_ib_profile *profile)
Mark Bloch16c19752018-01-01 13:06:58 +02006447{
Mark Bloch16c19752018-01-01 13:06:58 +02006448 int err;
6449 int i;
Mark Bloch16c19752018-01-01 13:06:58 +02006450
Mark Bloch16c19752018-01-01 13:06:58 +02006451 for (i = 0; i < MLX5_IB_STAGE_MAX; i++) {
6452 if (profile->stage[i].init) {
6453 err = profile->stage[i].init(dev);
6454 if (err)
6455 goto err_out;
6456 }
6457 }
6458
6459 dev->profile = profile;
Eli Cohene126ba92013-07-07 17:25:49 +03006460 dev->ib_active = true;
6461
Jack Morgenstein9603b612014-07-28 23:30:22 +03006462 return dev;
Eli Cohene126ba92013-07-07 17:25:49 +03006463
Mark Bloch16c19752018-01-01 13:06:58 +02006464err_out:
6465 __mlx5_ib_remove(dev, profile, i);
Eli Cohene126ba92013-07-07 17:25:49 +03006466
Jack Morgenstein9603b612014-07-28 23:30:22 +03006467 return NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03006468}
6469
Mark Bloch16c19752018-01-01 13:06:58 +02006470static const struct mlx5_ib_profile pf_profile = {
6471 STAGE_CREATE(MLX5_IB_STAGE_INIT,
6472 mlx5_ib_stage_init_init,
6473 mlx5_ib_stage_init_cleanup),
Mark Bloch9a4ca382018-01-16 14:42:35 +00006474 STAGE_CREATE(MLX5_IB_STAGE_FLOW_DB,
6475 mlx5_ib_stage_flow_db_init,
6476 mlx5_ib_stage_flow_db_cleanup),
Mark Bloch16c19752018-01-01 13:06:58 +02006477 STAGE_CREATE(MLX5_IB_STAGE_CAPS,
6478 mlx5_ib_stage_caps_init,
6479 NULL),
Mark Bloch8e6efa32017-11-06 12:22:13 +00006480 STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB,
6481 mlx5_ib_stage_non_default_cb,
6482 NULL),
Mark Bloch16c19752018-01-01 13:06:58 +02006483 STAGE_CREATE(MLX5_IB_STAGE_ROCE,
6484 mlx5_ib_stage_roce_init,
6485 mlx5_ib_stage_roce_cleanup),
Leon Romanovskyf3da6572018-11-28 20:53:41 +02006486 STAGE_CREATE(MLX5_IB_STAGE_SRQ,
6487 mlx5_init_srq_table,
6488 mlx5_cleanup_srq_table),
Mark Bloch16c19752018-01-01 13:06:58 +02006489 STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES,
6490 mlx5_ib_stage_dev_res_init,
6491 mlx5_ib_stage_dev_res_cleanup),
Saeed Mahameeddf097a22018-11-26 14:39:00 -08006492 STAGE_CREATE(MLX5_IB_STAGE_DEVICE_NOTIFIER,
6493 mlx5_ib_stage_dev_notifier_init,
6494 mlx5_ib_stage_dev_notifier_cleanup),
Mark Bloch16c19752018-01-01 13:06:58 +02006495 STAGE_CREATE(MLX5_IB_STAGE_ODP,
6496 mlx5_ib_stage_odp_init,
Saeed Mahameedd5d284b2018-11-19 10:52:41 -08006497 mlx5_ib_stage_odp_cleanup),
Mark Bloch16c19752018-01-01 13:06:58 +02006498 STAGE_CREATE(MLX5_IB_STAGE_COUNTERS,
6499 mlx5_ib_stage_counters_init,
6500 mlx5_ib_stage_counters_cleanup),
6501 STAGE_CREATE(MLX5_IB_STAGE_CONG_DEBUGFS,
6502 mlx5_ib_stage_cong_debugfs_init,
6503 mlx5_ib_stage_cong_debugfs_cleanup),
6504 STAGE_CREATE(MLX5_IB_STAGE_UAR,
6505 mlx5_ib_stage_uar_init,
6506 mlx5_ib_stage_uar_cleanup),
6507 STAGE_CREATE(MLX5_IB_STAGE_BFREG,
6508 mlx5_ib_stage_bfrag_init,
6509 mlx5_ib_stage_bfrag_cleanup),
Mark Bloch42cea832018-03-14 09:14:15 +02006510 STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR,
6511 NULL,
6512 mlx5_ib_stage_pre_ib_reg_umr_cleanup),
Leon Romanovsky81773ce2018-11-28 20:53:39 +02006513 STAGE_CREATE(MLX5_IB_STAGE_WHITELIST_UID,
6514 mlx5_ib_stage_devx_init,
6515 mlx5_ib_stage_devx_cleanup),
Mark Bloch16c19752018-01-01 13:06:58 +02006516 STAGE_CREATE(MLX5_IB_STAGE_IB_REG,
6517 mlx5_ib_stage_ib_reg_init,
6518 mlx5_ib_stage_ib_reg_cleanup),
Mark Bloch42cea832018-03-14 09:14:15 +02006519 STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR,
6520 mlx5_ib_stage_post_ib_reg_umr_init,
6521 NULL),
Mark Bloch16c19752018-01-01 13:06:58 +02006522 STAGE_CREATE(MLX5_IB_STAGE_DELAY_DROP,
6523 mlx5_ib_stage_delay_drop_init,
6524 mlx5_ib_stage_delay_drop_cleanup),
Mark Bloch16c19752018-01-01 13:06:58 +02006525};
6526
Bodong Wangf0666f12019-02-12 22:55:34 -08006527const struct mlx5_ib_profile uplink_rep_profile = {
Mark Blochb5ca15a2018-01-23 11:16:30 +00006528 STAGE_CREATE(MLX5_IB_STAGE_INIT,
6529 mlx5_ib_stage_init_init,
6530 mlx5_ib_stage_init_cleanup),
6531 STAGE_CREATE(MLX5_IB_STAGE_FLOW_DB,
6532 mlx5_ib_stage_flow_db_init,
6533 mlx5_ib_stage_flow_db_cleanup),
6534 STAGE_CREATE(MLX5_IB_STAGE_CAPS,
6535 mlx5_ib_stage_caps_init,
6536 NULL),
6537 STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB,
6538 mlx5_ib_stage_rep_non_default_cb,
6539 NULL),
6540 STAGE_CREATE(MLX5_IB_STAGE_ROCE,
6541 mlx5_ib_stage_rep_roce_init,
6542 mlx5_ib_stage_rep_roce_cleanup),
Leon Romanovskyf3da6572018-11-28 20:53:41 +02006543 STAGE_CREATE(MLX5_IB_STAGE_SRQ,
6544 mlx5_init_srq_table,
6545 mlx5_cleanup_srq_table),
Mark Blochb5ca15a2018-01-23 11:16:30 +00006546 STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES,
6547 mlx5_ib_stage_dev_res_init,
6548 mlx5_ib_stage_dev_res_cleanup),
Saeed Mahameeddf097a22018-11-26 14:39:00 -08006549 STAGE_CREATE(MLX5_IB_STAGE_DEVICE_NOTIFIER,
6550 mlx5_ib_stage_dev_notifier_init,
6551 mlx5_ib_stage_dev_notifier_cleanup),
Mark Blochb5ca15a2018-01-23 11:16:30 +00006552 STAGE_CREATE(MLX5_IB_STAGE_COUNTERS,
6553 mlx5_ib_stage_counters_init,
6554 mlx5_ib_stage_counters_cleanup),
6555 STAGE_CREATE(MLX5_IB_STAGE_UAR,
6556 mlx5_ib_stage_uar_init,
6557 mlx5_ib_stage_uar_cleanup),
6558 STAGE_CREATE(MLX5_IB_STAGE_BFREG,
6559 mlx5_ib_stage_bfrag_init,
6560 mlx5_ib_stage_bfrag_cleanup),
David S. Miller03fe2de2018-03-23 11:24:57 -04006561 STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR,
6562 NULL,
6563 mlx5_ib_stage_pre_ib_reg_umr_cleanup),
Mark Blochb5ca15a2018-01-23 11:16:30 +00006564 STAGE_CREATE(MLX5_IB_STAGE_IB_REG,
6565 mlx5_ib_stage_ib_reg_init,
6566 mlx5_ib_stage_ib_reg_cleanup),
David S. Miller03fe2de2018-03-23 11:24:57 -04006567 STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR,
6568 mlx5_ib_stage_post_ib_reg_umr_init,
6569 NULL),
Mark Blochb5ca15a2018-01-23 11:16:30 +00006570};
6571
Leon Romanovskye3f1ed12018-07-08 12:55:43 +03006572static void *mlx5_ib_add_slave_port(struct mlx5_core_dev *mdev)
Daniel Jurgens32f69e42018-01-04 17:25:36 +02006573{
6574 struct mlx5_ib_multiport_info *mpi;
6575 struct mlx5_ib_dev *dev;
6576 bool bound = false;
6577 int err;
6578
6579 mpi = kzalloc(sizeof(*mpi), GFP_KERNEL);
6580 if (!mpi)
6581 return NULL;
6582
6583 mpi->mdev = mdev;
6584
6585 err = mlx5_query_nic_vport_system_image_guid(mdev,
6586 &mpi->sys_image_guid);
6587 if (err) {
6588 kfree(mpi);
6589 return NULL;
6590 }
6591
6592 mutex_lock(&mlx5_ib_multiport_mutex);
6593 list_for_each_entry(dev, &mlx5_ib_dev_list, ib_dev_list) {
6594 if (dev->sys_image_guid == mpi->sys_image_guid)
6595 bound = mlx5_ib_bind_slave_port(dev, mpi);
6596
6597 if (bound) {
6598 rdma_roce_rescan_device(&dev->ib_dev);
6599 break;
6600 }
6601 }
6602
6603 if (!bound) {
6604 list_add_tail(&mpi->list, &mlx5_ib_unaffiliated_port_list);
6605 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 +02006606 }
6607 mutex_unlock(&mlx5_ib_multiport_mutex);
6608
6609 return mpi;
6610}
6611
Mark Bloch16c19752018-01-01 13:06:58 +02006612static void *mlx5_ib_add(struct mlx5_core_dev *mdev)
6613{
Daniel Jurgens32f69e42018-01-04 17:25:36 +02006614 enum rdma_link_layer ll;
Mark Blochb5ca15a2018-01-23 11:16:30 +00006615 struct mlx5_ib_dev *dev;
Daniel Jurgens32f69e42018-01-04 17:25:36 +02006616 int port_type_cap;
Mark Blochda796cc2019-03-28 15:27:35 +02006617 int num_ports;
Daniel Jurgens32f69e42018-01-04 17:25:36 +02006618
Mark Blochb5ca15a2018-01-23 11:16:30 +00006619 printk_once(KERN_INFO "%s", mlx5_version);
6620
Bodong Wangf0666f12019-02-12 22:55:34 -08006621 if (MLX5_ESWITCH_MANAGER(mdev) &&
6622 mlx5_ib_eswitch_mode(mdev->priv.eswitch) == SRIOV_OFFLOADS) {
6623 mlx5_ib_register_vport_reps(mdev);
6624 return mdev;
6625 }
6626
Daniel Jurgens32f69e42018-01-04 17:25:36 +02006627 port_type_cap = MLX5_CAP_GEN(mdev, port_type);
6628 ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
6629
Leon Romanovskye3f1ed12018-07-08 12:55:43 +03006630 if (mlx5_core_is_mp_slave(mdev) && ll == IB_LINK_LAYER_ETHERNET)
6631 return mlx5_ib_add_slave_port(mdev);
Daniel Jurgens32f69e42018-01-04 17:25:36 +02006632
Mark Blochda796cc2019-03-28 15:27:35 +02006633 num_ports = max(MLX5_CAP_GEN(mdev, num_ports),
6634 MLX5_CAP_GEN(mdev, num_vhca_ports));
Leon Romanovsky459cc692019-01-30 12:49:11 +02006635 dev = ib_alloc_device(mlx5_ib_dev, ib_dev);
Mark Blochb5ca15a2018-01-23 11:16:30 +00006636 if (!dev)
6637 return NULL;
Mark Blochda796cc2019-03-28 15:27:35 +02006638 dev->port = kcalloc(num_ports, sizeof(*dev->port),
6639 GFP_KERNEL);
6640 if (!dev->port) {
6641 ib_dealloc_device((struct ib_device *)dev);
6642 return NULL;
6643 }
Mark Blochb5ca15a2018-01-23 11:16:30 +00006644
6645 dev->mdev = mdev;
Mark Blochda796cc2019-03-28 15:27:35 +02006646 dev->num_ports = num_ports;
Mark Blochb5ca15a2018-01-23 11:16:30 +00006647
Mark Blochb5ca15a2018-01-23 11:16:30 +00006648 return __mlx5_ib_add(dev, &pf_profile);
Mark Bloch16c19752018-01-01 13:06:58 +02006649}
6650
Jack Morgenstein9603b612014-07-28 23:30:22 +03006651static void mlx5_ib_remove(struct mlx5_core_dev *mdev, void *context)
Eli Cohene126ba92013-07-07 17:25:49 +03006652{
Daniel Jurgens32f69e42018-01-04 17:25:36 +02006653 struct mlx5_ib_multiport_info *mpi;
6654 struct mlx5_ib_dev *dev;
Haggai Eran6aec21f2014-12-11 17:04:23 +02006655
Bodong Wangf0666f12019-02-12 22:55:34 -08006656 if (MLX5_ESWITCH_MANAGER(mdev) && context == mdev) {
6657 mlx5_ib_unregister_vport_reps(mdev);
6658 return;
6659 }
6660
Daniel Jurgens32f69e42018-01-04 17:25:36 +02006661 if (mlx5_core_is_mp_slave(mdev)) {
6662 mpi = context;
6663 mutex_lock(&mlx5_ib_multiport_mutex);
6664 if (mpi->ibdev)
6665 mlx5_ib_unbind_slave_port(mpi->ibdev, mpi);
6666 list_del(&mpi->list);
6667 mutex_unlock(&mlx5_ib_multiport_mutex);
6668 return;
6669 }
6670
6671 dev = context;
Bodong Wangf0666f12019-02-12 22:55:34 -08006672 __mlx5_ib_remove(dev, dev->profile, MLX5_IB_STAGE_MAX);
Eli Cohene126ba92013-07-07 17:25:49 +03006673}
6674
Jack Morgenstein9603b612014-07-28 23:30:22 +03006675static struct mlx5_interface mlx5_ib_interface = {
6676 .add = mlx5_ib_add,
6677 .remove = mlx5_ib_remove,
Saeed Mahameed64613d942015-04-02 17:07:34 +03006678 .protocol = MLX5_INTERFACE_PROTOCOL_IB,
Eli Cohene126ba92013-07-07 17:25:49 +03006679};
6680
Ilya Lesokhinc44ef992018-03-13 15:18:48 +02006681unsigned long mlx5_ib_get_xlt_emergency_page(void)
6682{
6683 mutex_lock(&xlt_emergency_page_mutex);
6684 return xlt_emergency_page;
6685}
6686
6687void mlx5_ib_put_xlt_emergency_page(void)
6688{
6689 mutex_unlock(&xlt_emergency_page_mutex);
6690}
6691
Eli Cohene126ba92013-07-07 17:25:49 +03006692static int __init mlx5_ib_init(void)
6693{
Haggai Eran6aec21f2014-12-11 17:04:23 +02006694 int err;
6695
Ilya Lesokhinc44ef992018-03-13 15:18:48 +02006696 xlt_emergency_page = __get_free_page(GFP_KERNEL);
6697 if (!xlt_emergency_page)
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02006698 return -ENOMEM;
6699
Ilya Lesokhinc44ef992018-03-13 15:18:48 +02006700 mutex_init(&xlt_emergency_page_mutex);
6701
6702 mlx5_ib_event_wq = alloc_ordered_workqueue("mlx5_ib_event_wq", 0);
6703 if (!mlx5_ib_event_wq) {
6704 free_page(xlt_emergency_page);
6705 return -ENOMEM;
6706 }
6707
Artemy Kovalyov81713d32017-01-18 16:58:11 +02006708 mlx5_ib_odp_init();
Jack Morgenstein9603b612014-07-28 23:30:22 +03006709
Haggai Eran6aec21f2014-12-11 17:04:23 +02006710 err = mlx5_register_interface(&mlx5_ib_interface);
Haggai Eran6aec21f2014-12-11 17:04:23 +02006711
6712 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03006713}
6714
6715static void __exit mlx5_ib_cleanup(void)
6716{
Jack Morgenstein9603b612014-07-28 23:30:22 +03006717 mlx5_unregister_interface(&mlx5_ib_interface);
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02006718 destroy_workqueue(mlx5_ib_event_wq);
Ilya Lesokhinc44ef992018-03-13 15:18:48 +02006719 mutex_destroy(&xlt_emergency_page_mutex);
6720 free_page(xlt_emergency_page);
Eli Cohene126ba92013-07-07 17:25:49 +03006721}
6722
6723module_init(mlx5_ib_init);
6724module_exit(mlx5_ib_cleanup);