blob: d40c394346371d616fb5f3ae05592422de6e7db0 [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.
Mark Bloch26628e22019-03-28 15:27:41 +0200509 * Use native port in case of reps
Noa Osherovich50f22fd2017-04-20 20:53:32 +0300510 */
Mark Bloch26628e22019-03-28 15:27:41 +0200511 if (dev->is_rep)
512 err = mlx5_query_port_ptys(mdev, out, sizeof(out), MLX5_PTYS_EN,
513 1);
514 else
515 err = mlx5_query_port_ptys(mdev, out, sizeof(out), MLX5_PTYS_EN,
516 mdev_port_num);
Ilan Tayari095b0922017-05-14 16:04:30 +0300517 if (err)
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200518 goto out;
Aya Levin08e86762019-02-12 22:55:46 -0800519 ext = MLX5_CAP_PCAM_FEATURE(dev->mdev, ptys_extended_ethernet);
520 eth_prot_oper = MLX5_GET_ETH_PROTO(ptys_reg, out, ext, eth_proto_oper);
Noa Osherovichf1b65df2017-04-20 20:53:33 +0300521
Honggang Li7672ed32018-03-16 10:37:13 +0800522 props->active_width = IB_WIDTH_4X;
523 props->active_speed = IB_SPEED_QDR;
524
Noa Osherovichf1b65df2017-04-20 20:53:33 +0300525 translate_eth_proto_oper(eth_prot_oper, &props->active_speed,
Aya Levin08e86762019-02-12 22:55:46 -0800526 &props->active_width, ext);
Achiad Shochat3f89a642015-12-23 18:47:21 +0200527
Jason Gunthorpe2f944c02018-07-04 15:57:48 +0300528 props->port_cap_flags |= IB_PORT_CM_SUP;
529 props->ip_gids = true;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200530
531 props->gid_tbl_len = MLX5_CAP_ROCE(dev->mdev,
532 roce_address_table_size);
533 props->max_mtu = IB_MTU_4096;
534 props->max_msg_sz = 1 << MLX5_CAP_GEN(dev->mdev, log_max_msg);
535 props->pkey_tbl_len = 1;
536 props->state = IB_PORT_DOWN;
537 props->phys_state = 3;
538
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200539 mlx5_query_nic_vport_qkey_viol_cntr(mdev, &qkey_viol_cntr);
Leon Romanovskyc876a1b2016-01-09 13:06:25 +0200540 props->qkey_viol_cntr = qkey_viol_cntr;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200541
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200542 /* If this is a stub query for an unaffiliated port stop here */
543 if (!put_mdev)
544 goto out;
545
Achiad Shochat3f89a642015-12-23 18:47:21 +0200546 ndev = mlx5_ib_get_netdev(device, port_num);
547 if (!ndev)
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200548 goto out;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200549
Aviv Heller7c34ec12018-08-23 13:47:53 +0300550 if (dev->lag_active) {
Aviv Heller88621df2016-09-18 20:48:02 +0300551 rcu_read_lock();
552 upper = netdev_master_upper_dev_get_rcu(ndev);
553 if (upper) {
554 dev_put(ndev);
555 ndev = upper;
556 dev_hold(ndev);
557 }
558 rcu_read_unlock();
559 }
560
Achiad Shochat3f89a642015-12-23 18:47:21 +0200561 if (netif_running(ndev) && netif_carrier_ok(ndev)) {
562 props->state = IB_PORT_ACTIVE;
563 props->phys_state = 5;
564 }
565
566 ndev_ib_mtu = iboe_get_mtu(ndev->mtu);
567
568 dev_put(ndev);
569
570 props->active_mtu = min(props->max_mtu, ndev_ib_mtu);
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200571out:
572 if (put_mdev)
573 mlx5_ib_put_native_port_mdev(dev, port_num);
574 return err;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200575}
576
Parav Panditcf34e1f2019-01-27 20:35:50 +0200577struct mlx5_ib_vlan_info {
578 u16 vlan_id;
579 bool vlan;
580};
581
582static int get_lower_dev_vlan(struct net_device *lower_dev, void *data)
583{
584 struct mlx5_ib_vlan_info *vlan_info = data;
585
586 if (is_vlan_dev(lower_dev)) {
587 vlan_info->vlan = true;
588 vlan_info->vlan_id = vlan_dev_vlan_id(lower_dev);
589 }
590 /* We are interested only in first level vlan device, so
591 * always return 1 to stop iterating over next level devices.
592 */
593 return 1;
594}
595
Ilan Tayari095b0922017-05-14 16:04:30 +0300596static int set_roce_addr(struct mlx5_ib_dev *dev, u8 port_num,
597 unsigned int index, const union ib_gid *gid,
598 const struct ib_gid_attr *attr)
Achiad Shochat3cca2602015-12-23 18:47:23 +0200599{
Ilan Tayari095b0922017-05-14 16:04:30 +0300600 enum ib_gid_type gid_type = IB_GID_TYPE_IB;
Parav Panditcf34e1f2019-01-27 20:35:50 +0200601 struct mlx5_ib_vlan_info vlan_info = { };
Ilan Tayari095b0922017-05-14 16:04:30 +0300602 u8 roce_version = 0;
603 u8 roce_l3_type = 0;
Ilan Tayari095b0922017-05-14 16:04:30 +0300604 u8 mac[ETH_ALEN];
Achiad Shochat3cca2602015-12-23 18:47:23 +0200605
Ilan Tayari095b0922017-05-14 16:04:30 +0300606 if (gid) {
607 gid_type = attr->gid_type;
608 ether_addr_copy(mac, attr->ndev->dev_addr);
Achiad Shochat3cca2602015-12-23 18:47:23 +0200609
Ilan Tayari095b0922017-05-14 16:04:30 +0300610 if (is_vlan_dev(attr->ndev)) {
Parav Panditcf34e1f2019-01-27 20:35:50 +0200611 vlan_info.vlan = true;
612 vlan_info.vlan_id = vlan_dev_vlan_id(attr->ndev);
613 } else {
614 /* If the netdev is upper device and if it's lower
615 * lower device is vlan device, consider vlan id of
616 * the lower vlan device for this gid entry.
617 */
618 rcu_read_lock();
619 netdev_walk_all_lower_dev_rcu(attr->ndev,
620 get_lower_dev_vlan, &vlan_info);
621 rcu_read_unlock();
Ilan Tayari095b0922017-05-14 16:04:30 +0300622 }
Achiad Shochat3cca2602015-12-23 18:47:23 +0200623 }
624
Ilan Tayari095b0922017-05-14 16:04:30 +0300625 switch (gid_type) {
Achiad Shochat3cca2602015-12-23 18:47:23 +0200626 case IB_GID_TYPE_IB:
Ilan Tayari095b0922017-05-14 16:04:30 +0300627 roce_version = MLX5_ROCE_VERSION_1;
Achiad Shochat3cca2602015-12-23 18:47:23 +0200628 break;
629 case IB_GID_TYPE_ROCE_UDP_ENCAP:
Ilan Tayari095b0922017-05-14 16:04:30 +0300630 roce_version = MLX5_ROCE_VERSION_2;
631 if (ipv6_addr_v4mapped((void *)gid))
632 roce_l3_type = MLX5_ROCE_L3_TYPE_IPV4;
633 else
634 roce_l3_type = MLX5_ROCE_L3_TYPE_IPV6;
Achiad Shochat3cca2602015-12-23 18:47:23 +0200635 break;
636
637 default:
Ilan Tayari095b0922017-05-14 16:04:30 +0300638 mlx5_ib_warn(dev, "Unexpected GID type %u\n", gid_type);
Achiad Shochat3cca2602015-12-23 18:47:23 +0200639 }
640
Ilan Tayari095b0922017-05-14 16:04:30 +0300641 return mlx5_core_roce_gid_set(dev->mdev, index, roce_version,
Parav Panditcf34e1f2019-01-27 20:35:50 +0200642 roce_l3_type, gid->raw, mac,
643 vlan_info.vlan, vlan_info.vlan_id,
644 port_num);
Achiad Shochat3cca2602015-12-23 18:47:23 +0200645}
646
Parav Panditf4df9a72018-06-05 08:40:16 +0300647static int mlx5_ib_add_gid(const struct ib_gid_attr *attr,
Achiad Shochat3cca2602015-12-23 18:47:23 +0200648 __always_unused void **context)
649{
Parav Pandit414448d2018-04-01 15:08:24 +0300650 return set_roce_addr(to_mdev(attr->device), attr->port_num,
Parav Panditf4df9a72018-06-05 08:40:16 +0300651 attr->index, &attr->gid, attr);
Achiad Shochat3cca2602015-12-23 18:47:23 +0200652}
653
Parav Pandit414448d2018-04-01 15:08:24 +0300654static int mlx5_ib_del_gid(const struct ib_gid_attr *attr,
655 __always_unused void **context)
Achiad Shochat3cca2602015-12-23 18:47:23 +0200656{
Parav Pandit414448d2018-04-01 15:08:24 +0300657 return set_roce_addr(to_mdev(attr->device), attr->port_num,
658 attr->index, NULL, NULL);
Achiad Shochat3cca2602015-12-23 18:47:23 +0200659}
660
Parav Pandit47ec3862018-06-13 10:22:06 +0300661__be16 mlx5_get_roce_udp_sport(struct mlx5_ib_dev *dev,
662 const struct ib_gid_attr *attr)
Achiad Shochat2811ba52015-12-23 18:47:24 +0200663{
Parav Pandit47ec3862018-06-13 10:22:06 +0300664 if (attr->gid_type != IB_GID_TYPE_ROCE_UDP_ENCAP)
Achiad Shochat2811ba52015-12-23 18:47:24 +0200665 return 0;
666
667 return cpu_to_be16(MLX5_CAP_ROCE(dev->mdev, r_roce_min_src_udp_port));
668}
669
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300670static int mlx5_use_mad_ifc(struct mlx5_ib_dev *dev)
671{
Noa Osherovich7fae6652016-09-12 19:16:23 +0300672 if (MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_IB)
673 return !MLX5_CAP_GEN(dev->mdev, ib_virt);
674 return 0;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300675}
676
677enum {
678 MLX5_VPORT_ACCESS_METHOD_MAD,
679 MLX5_VPORT_ACCESS_METHOD_HCA,
680 MLX5_VPORT_ACCESS_METHOD_NIC,
681};
682
683static int mlx5_get_vport_access_method(struct ib_device *ibdev)
684{
685 if (mlx5_use_mad_ifc(to_mdev(ibdev)))
686 return MLX5_VPORT_ACCESS_METHOD_MAD;
687
Achiad Shochatebd61f62015-12-23 18:47:16 +0200688 if (mlx5_ib_port_link_layer(ibdev, 1) ==
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300689 IB_LINK_LAYER_ETHERNET)
690 return MLX5_VPORT_ACCESS_METHOD_NIC;
691
692 return MLX5_VPORT_ACCESS_METHOD_HCA;
693}
694
Eran Ben Elishada7525d2015-12-14 16:34:10 +0200695static void get_atomic_caps(struct mlx5_ib_dev *dev,
Moni Shoua776a3902018-01-02 16:19:33 +0200696 u8 atomic_size_qp,
Eran Ben Elishada7525d2015-12-14 16:34:10 +0200697 struct ib_device_attr *props)
698{
699 u8 tmp;
700 u8 atomic_operations = MLX5_CAP_ATOMIC(dev->mdev, atomic_operations);
Eran Ben Elishada7525d2015-12-14 16:34:10 +0200701 u8 atomic_req_8B_endianness_mode =
Or Gerlitzbd108382017-05-28 15:24:17 +0300702 MLX5_CAP_ATOMIC(dev->mdev, atomic_req_8B_endianness_mode);
Eran Ben Elishada7525d2015-12-14 16:34:10 +0200703
704 /* Check if HW supports 8 bytes standard atomic operations and capable
705 * of host endianness respond
706 */
707 tmp = MLX5_ATOMIC_OPS_CMP_SWAP | MLX5_ATOMIC_OPS_FETCH_ADD;
708 if (((atomic_operations & tmp) == tmp) &&
709 (atomic_size_qp & MLX5_ATOMIC_SIZE_QP_8BYTES) &&
710 (atomic_req_8B_endianness_mode)) {
711 props->atomic_cap = IB_ATOMIC_HCA;
712 } else {
713 props->atomic_cap = IB_ATOMIC_NONE;
714 }
715}
716
Moni Shoua776a3902018-01-02 16:19:33 +0200717static void get_atomic_caps_qp(struct mlx5_ib_dev *dev,
718 struct ib_device_attr *props)
719{
720 u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_qp);
721
722 get_atomic_caps(dev, atomic_size_qp, props);
723}
724
725static void get_atomic_caps_dc(struct mlx5_ib_dev *dev,
726 struct ib_device_attr *props)
727{
728 u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_dc);
729
730 get_atomic_caps(dev, atomic_size_qp, props);
731}
732
733bool mlx5_ib_dc_atomic_is_supported(struct mlx5_ib_dev *dev)
734{
735 struct ib_device_attr props = {};
736
737 get_atomic_caps_dc(dev, &props);
738 return (props.atomic_cap == IB_ATOMIC_HCA) ? true : false;
739}
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300740static int mlx5_query_system_image_guid(struct ib_device *ibdev,
741 __be64 *sys_image_guid)
742{
743 struct mlx5_ib_dev *dev = to_mdev(ibdev);
744 struct mlx5_core_dev *mdev = dev->mdev;
745 u64 tmp;
746 int err;
747
748 switch (mlx5_get_vport_access_method(ibdev)) {
749 case MLX5_VPORT_ACCESS_METHOD_MAD:
750 return mlx5_query_mad_ifc_system_image_guid(ibdev,
751 sys_image_guid);
752
753 case MLX5_VPORT_ACCESS_METHOD_HCA:
754 err = mlx5_query_hca_vport_system_image_guid(mdev, &tmp);
Achiad Shochat3f89a642015-12-23 18:47:21 +0200755 break;
756
757 case MLX5_VPORT_ACCESS_METHOD_NIC:
758 err = mlx5_query_nic_vport_system_image_guid(mdev, &tmp);
759 break;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300760
761 default:
762 return -EINVAL;
763 }
Achiad Shochat3f89a642015-12-23 18:47:21 +0200764
765 if (!err)
766 *sys_image_guid = cpu_to_be64(tmp);
767
768 return err;
769
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300770}
771
772static int mlx5_query_max_pkeys(struct ib_device *ibdev,
773 u16 *max_pkeys)
774{
775 struct mlx5_ib_dev *dev = to_mdev(ibdev);
776 struct mlx5_core_dev *mdev = dev->mdev;
777
778 switch (mlx5_get_vport_access_method(ibdev)) {
779 case MLX5_VPORT_ACCESS_METHOD_MAD:
780 return mlx5_query_mad_ifc_max_pkeys(ibdev, max_pkeys);
781
782 case MLX5_VPORT_ACCESS_METHOD_HCA:
783 case MLX5_VPORT_ACCESS_METHOD_NIC:
784 *max_pkeys = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev,
785 pkey_table_size));
786 return 0;
787
788 default:
789 return -EINVAL;
790 }
791}
792
793static int mlx5_query_vendor_id(struct ib_device *ibdev,
794 u32 *vendor_id)
795{
796 struct mlx5_ib_dev *dev = to_mdev(ibdev);
797
798 switch (mlx5_get_vport_access_method(ibdev)) {
799 case MLX5_VPORT_ACCESS_METHOD_MAD:
800 return mlx5_query_mad_ifc_vendor_id(ibdev, vendor_id);
801
802 case MLX5_VPORT_ACCESS_METHOD_HCA:
803 case MLX5_VPORT_ACCESS_METHOD_NIC:
804 return mlx5_core_query_vendor_id(dev->mdev, vendor_id);
805
806 default:
807 return -EINVAL;
808 }
809}
810
811static int mlx5_query_node_guid(struct mlx5_ib_dev *dev,
812 __be64 *node_guid)
813{
814 u64 tmp;
815 int err;
816
817 switch (mlx5_get_vport_access_method(&dev->ib_dev)) {
818 case MLX5_VPORT_ACCESS_METHOD_MAD:
819 return mlx5_query_mad_ifc_node_guid(dev, node_guid);
820
821 case MLX5_VPORT_ACCESS_METHOD_HCA:
822 err = mlx5_query_hca_vport_node_guid(dev->mdev, &tmp);
Achiad Shochat3f89a642015-12-23 18:47:21 +0200823 break;
824
825 case MLX5_VPORT_ACCESS_METHOD_NIC:
826 err = mlx5_query_nic_vport_node_guid(dev->mdev, &tmp);
827 break;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300828
829 default:
830 return -EINVAL;
831 }
Achiad Shochat3f89a642015-12-23 18:47:21 +0200832
833 if (!err)
834 *node_guid = cpu_to_be64(tmp);
835
836 return err;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300837}
838
839struct mlx5_reg_node_desc {
Yuval Shaiabd99fde2016-08-25 10:57:07 -0700840 u8 desc[IB_DEVICE_NODE_DESC_MAX];
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300841};
842
843static int mlx5_query_node_desc(struct mlx5_ib_dev *dev, char *node_desc)
844{
845 struct mlx5_reg_node_desc in;
846
847 if (mlx5_use_mad_ifc(dev))
848 return mlx5_query_mad_ifc_node_desc(dev, node_desc);
849
850 memset(&in, 0, sizeof(in));
851
852 return mlx5_core_access_reg(dev->mdev, &in, sizeof(in), node_desc,
853 sizeof(struct mlx5_reg_node_desc),
854 MLX5_REG_NODE_DESC, 0, 0);
855}
856
Eli Cohene126ba92013-07-07 17:25:49 +0300857static int mlx5_ib_query_device(struct ib_device *ibdev,
Matan Barak2528e332015-06-11 16:35:25 +0300858 struct ib_device_attr *props,
859 struct ib_udata *uhw)
Eli Cohene126ba92013-07-07 17:25:49 +0300860{
861 struct mlx5_ib_dev *dev = to_mdev(ibdev);
Saeed Mahameed938fe832015-05-28 22:28:41 +0300862 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +0300863 int err = -ENOMEM;
Eli Cohen288c01b2016-10-27 16:36:45 +0300864 int max_sq_desc;
Eli Cohene126ba92013-07-07 17:25:49 +0300865 int max_rq_sg;
866 int max_sq_sg;
Sagi Grimberge0238a62015-07-21 14:40:12 +0300867 u64 min_page_size = 1ull << MLX5_CAP_GEN(mdev, log_pg_sz);
Daniel Jurgens85c7c012018-01-04 17:25:43 +0200868 bool raw_support = !mlx5_core_mp_enabled(mdev);
Bodong Wang402ca532016-06-17 15:02:20 +0300869 struct mlx5_ib_query_device_resp resp = {};
870 size_t resp_len;
871 u64 max_tso;
Eli Cohene126ba92013-07-07 17:25:49 +0300872
Bodong Wang402ca532016-06-17 15:02:20 +0300873 resp_len = sizeof(resp.comp_mask) + sizeof(resp.response_length);
874 if (uhw->outlen && uhw->outlen < resp_len)
875 return -EINVAL;
876 else
877 resp.response_length = resp_len;
878
879 if (uhw->inlen && !ib_is_udata_cleared(uhw, 0, uhw->inlen))
Matan Barak2528e332015-06-11 16:35:25 +0300880 return -EINVAL;
881
Eli Cohene126ba92013-07-07 17:25:49 +0300882 memset(props, 0, sizeof(*props));
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300883 err = mlx5_query_system_image_guid(ibdev,
884 &props->sys_image_guid);
885 if (err)
886 return err;
887
888 err = mlx5_query_max_pkeys(ibdev, &props->max_pkeys);
889 if (err)
890 return err;
891
892 err = mlx5_query_vendor_id(ibdev, &props->vendor_id);
893 if (err)
894 return err;
Eli Cohene126ba92013-07-07 17:25:49 +0300895
Jack Morgenstein9603b612014-07-28 23:30:22 +0300896 props->fw_ver = ((u64)fw_rev_maj(dev->mdev) << 32) |
897 (fw_rev_min(dev->mdev) << 16) |
898 fw_rev_sub(dev->mdev);
Eli Cohene126ba92013-07-07 17:25:49 +0300899 props->device_cap_flags = IB_DEVICE_CHANGE_PHY_PORT |
900 IB_DEVICE_PORT_ACTIVE_EVENT |
901 IB_DEVICE_SYS_IMAGE_GUID |
Eli Cohen1a4c3a32014-02-06 17:41:25 +0200902 IB_DEVICE_RC_RNR_NAK_GEN;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300903
904 if (MLX5_CAP_GEN(mdev, pkv))
Eli Cohene126ba92013-07-07 17:25:49 +0300905 props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300906 if (MLX5_CAP_GEN(mdev, qkv))
Eli Cohene126ba92013-07-07 17:25:49 +0300907 props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300908 if (MLX5_CAP_GEN(mdev, apm))
Eli Cohene126ba92013-07-07 17:25:49 +0300909 props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300910 if (MLX5_CAP_GEN(mdev, xrc))
Eli Cohene126ba92013-07-07 17:25:49 +0300911 props->device_cap_flags |= IB_DEVICE_XRC;
Matan Barakd2370e02016-02-29 18:05:30 +0200912 if (MLX5_CAP_GEN(mdev, imaicl)) {
913 props->device_cap_flags |= IB_DEVICE_MEM_WINDOW |
914 IB_DEVICE_MEM_WINDOW_TYPE_2B;
915 props->max_mw = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
Sagi Grimbergb005d312016-02-29 19:07:33 +0200916 /* We support 'Gappy' memory registration too */
917 props->device_cap_flags |= IB_DEVICE_SG_GAPS_REG;
Matan Barakd2370e02016-02-29 18:05:30 +0200918 }
Eli Cohene126ba92013-07-07 17:25:49 +0300919 props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300920 if (MLX5_CAP_GEN(mdev, sho)) {
Sagi Grimberg2dea9092014-02-23 14:19:13 +0200921 props->device_cap_flags |= IB_DEVICE_SIGNATURE_HANDOVER;
922 /* At this stage no support for signature handover */
923 props->sig_prot_cap = IB_PROT_T10DIF_TYPE_1 |
924 IB_PROT_T10DIF_TYPE_2 |
925 IB_PROT_T10DIF_TYPE_3;
926 props->sig_guard_cap = IB_GUARD_T10DIF_CRC |
927 IB_GUARD_T10DIF_CSUM;
928 }
Saeed Mahameed938fe832015-05-28 22:28:41 +0300929 if (MLX5_CAP_GEN(mdev, block_lb_mc))
Eli Cohenf360d882014-04-02 00:10:16 +0300930 props->device_cap_flags |= IB_DEVICE_BLOCK_MULTICAST_LOOPBACK;
Eli Cohene126ba92013-07-07 17:25:49 +0300931
Daniel Jurgens85c7c012018-01-04 17:25:43 +0200932 if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) && raw_support) {
Noa Osheroviche8161332017-01-18 15:40:01 +0200933 if (MLX5_CAP_ETH(mdev, csum_cap)) {
934 /* Legacy bit to support old userspace libraries */
Bodong Wang88115fe2015-12-18 13:53:20 +0200935 props->device_cap_flags |= IB_DEVICE_RAW_IP_CSUM;
Noa Osheroviche8161332017-01-18 15:40:01 +0200936 props->raw_packet_caps |= IB_RAW_PACKET_CAP_IP_CSUM;
937 }
938
939 if (MLX5_CAP_ETH(dev->mdev, vlan_cap))
940 props->raw_packet_caps |=
941 IB_RAW_PACKET_CAP_CVLAN_STRIPPING;
Bodong Wang88115fe2015-12-18 13:53:20 +0200942
Bodong Wang402ca532016-06-17 15:02:20 +0300943 if (field_avail(typeof(resp), tso_caps, uhw->outlen)) {
944 max_tso = MLX5_CAP_ETH(mdev, max_lso_cap);
945 if (max_tso) {
946 resp.tso_caps.max_tso = 1 << max_tso;
947 resp.tso_caps.supported_qpts |=
948 1 << IB_QPT_RAW_PACKET;
949 resp.response_length += sizeof(resp.tso_caps);
950 }
951 }
Yishai Hadas31f69a82016-08-28 11:28:45 +0300952
953 if (field_avail(typeof(resp), rss_caps, uhw->outlen)) {
954 resp.rss_caps.rx_hash_function =
955 MLX5_RX_HASH_FUNC_TOEPLITZ;
956 resp.rss_caps.rx_hash_fields_mask =
957 MLX5_RX_HASH_SRC_IPV4 |
958 MLX5_RX_HASH_DST_IPV4 |
959 MLX5_RX_HASH_SRC_IPV6 |
960 MLX5_RX_HASH_DST_IPV6 |
961 MLX5_RX_HASH_SRC_PORT_TCP |
962 MLX5_RX_HASH_DST_PORT_TCP |
963 MLX5_RX_HASH_SRC_PORT_UDP |
Maor Gottlieb4e2b53a2017-12-24 14:51:25 +0200964 MLX5_RX_HASH_DST_PORT_UDP |
965 MLX5_RX_HASH_INNER;
Matan Barak2d93fc82018-03-28 09:27:55 +0300966 if (mlx5_accel_ipsec_device_caps(dev->mdev) &
967 MLX5_ACCEL_IPSEC_CAP_DEVICE)
968 resp.rss_caps.rx_hash_fields_mask |=
969 MLX5_RX_HASH_IPSEC_SPI;
Yishai Hadas31f69a82016-08-28 11:28:45 +0300970 resp.response_length += sizeof(resp.rss_caps);
971 }
972 } else {
973 if (field_avail(typeof(resp), tso_caps, uhw->outlen))
974 resp.response_length += sizeof(resp.tso_caps);
975 if (field_avail(typeof(resp), rss_caps, uhw->outlen))
976 resp.response_length += sizeof(resp.rss_caps);
Bodong Wang402ca532016-06-17 15:02:20 +0300977 }
978
Erez Shitritf0313962016-02-21 16:27:17 +0200979 if (MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) {
980 props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
981 props->device_cap_flags |= IB_DEVICE_UD_TSO;
982 }
983
Maor Gottlieb03404e82017-05-30 10:29:13 +0300984 if (MLX5_CAP_GEN(dev->mdev, rq_delay_drop) &&
Daniel Jurgens85c7c012018-01-04 17:25:43 +0200985 MLX5_CAP_GEN(dev->mdev, general_notification_event) &&
986 raw_support)
Maor Gottlieb03404e82017-05-30 10:29:13 +0300987 props->raw_packet_caps |= IB_RAW_PACKET_CAP_DELAY_DROP;
988
Yishai Hadas1d54f892017-06-08 16:15:11 +0300989 if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads) &&
990 MLX5_CAP_IPOIB_ENHANCED(mdev, csum_cap))
991 props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
992
Majd Dibbinycff5a0f2016-04-17 17:19:38 +0300993 if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
Daniel Jurgens85c7c012018-01-04 17:25:43 +0200994 MLX5_CAP_ETH(dev->mdev, scatter_fcs) &&
995 raw_support) {
Noa Osheroviche8161332017-01-18 15:40:01 +0200996 /* Legacy bit to support old userspace libraries */
Majd Dibbinycff5a0f2016-04-17 17:19:38 +0300997 props->device_cap_flags |= IB_DEVICE_RAW_SCATTER_FCS;
Noa Osheroviche8161332017-01-18 15:40:01 +0200998 props->raw_packet_caps |= IB_RAW_PACKET_CAP_SCATTER_FCS;
999 }
Majd Dibbinycff5a0f2016-04-17 17:19:38 +03001000
Ariel Levkovich24da0012018-04-05 18:53:27 +03001001 if (MLX5_CAP_DEV_MEM(mdev, memic)) {
1002 props->max_dm_size =
1003 MLX5_CAP_DEV_MEM(mdev, max_memic_size);
1004 }
1005
Maor Gottliebda6d6ba32016-06-04 15:15:28 +03001006 if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS))
1007 props->device_cap_flags |= IB_DEVICE_MANAGED_FLOW_STEERING;
1008
Noa Osherovichb1383aa2017-10-29 13:59:45 +02001009 if (MLX5_CAP_GEN(mdev, end_pad))
1010 props->device_cap_flags |= IB_DEVICE_PCI_WRITE_END_PADDING;
1011
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001012 props->vendor_part_id = mdev->pdev->device;
1013 props->hw_ver = mdev->pdev->revision;
Eli Cohene126ba92013-07-07 17:25:49 +03001014
1015 props->max_mr_size = ~0ull;
Sagi Grimberge0238a62015-07-21 14:40:12 +03001016 props->page_size_cap = ~(min_page_size - 1);
Saeed Mahameed938fe832015-05-28 22:28:41 +03001017 props->max_qp = 1 << MLX5_CAP_GEN(mdev, log_max_qp);
1018 props->max_qp_wr = 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
1019 max_rq_sg = MLX5_CAP_GEN(mdev, max_wqe_sz_rq) /
1020 sizeof(struct mlx5_wqe_data_seg);
Eli Cohen288c01b2016-10-27 16:36:45 +03001021 max_sq_desc = min_t(int, MLX5_CAP_GEN(mdev, max_wqe_sz_sq), 512);
1022 max_sq_sg = (max_sq_desc - sizeof(struct mlx5_wqe_ctrl_seg) -
1023 sizeof(struct mlx5_wqe_raddr_seg)) /
1024 sizeof(struct mlx5_wqe_data_seg);
Steve Wise33023fb2018-06-18 08:05:26 -07001025 props->max_send_sge = max_sq_sg;
1026 props->max_recv_sge = max_rq_sg;
Sagi Grimberg986ef952016-03-31 19:03:25 +03001027 props->max_sge_rd = MLX5_MAX_SGE_RD;
Saeed Mahameed938fe832015-05-28 22:28:41 +03001028 props->max_cq = 1 << MLX5_CAP_GEN(mdev, log_max_cq);
Leon Romanovsky9f177682016-01-14 08:11:40 +02001029 props->max_cqe = (1 << MLX5_CAP_GEN(mdev, log_max_cq_sz)) - 1;
Saeed Mahameed938fe832015-05-28 22:28:41 +03001030 props->max_mr = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
1031 props->max_pd = 1 << MLX5_CAP_GEN(mdev, log_max_pd);
1032 props->max_qp_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_req_qp);
1033 props->max_qp_init_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_res_qp);
1034 props->max_srq = 1 << MLX5_CAP_GEN(mdev, log_max_srq);
1035 props->max_srq_wr = (1 << MLX5_CAP_GEN(mdev, log_max_srq_sz)) - 1;
1036 props->local_ca_ack_delay = MLX5_CAP_GEN(mdev, local_ca_ack_delay);
Eli Cohene126ba92013-07-07 17:25:49 +03001037 props->max_res_rd_atom = props->max_qp_rd_atom * props->max_qp;
Eli Cohene126ba92013-07-07 17:25:49 +03001038 props->max_srq_sge = max_rq_sg - 1;
Sagi Grimberg911f4332016-03-03 13:37:51 +02001039 props->max_fast_reg_page_list_len =
1040 1 << MLX5_CAP_GEN(mdev, log_max_klm_list_size);
Moni Shoua776a3902018-01-02 16:19:33 +02001041 get_atomic_caps_qp(dev, props);
Eli Cohen81bea282013-09-11 16:35:30 +03001042 props->masked_atomic_cap = IB_ATOMIC_NONE;
Saeed Mahameed938fe832015-05-28 22:28:41 +03001043 props->max_mcast_grp = 1 << MLX5_CAP_GEN(mdev, log_max_mcg);
1044 props->max_mcast_qp_attach = MLX5_CAP_GEN(mdev, max_qp_mcg);
Eli Cohene126ba92013-07-07 17:25:49 +03001045 props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
1046 props->max_mcast_grp;
1047 props->max_map_per_fmr = INT_MAX; /* no limit in ConnectIB */
Maor Gottlieb86695a62016-10-27 16:36:38 +03001048 props->max_ah = INT_MAX;
Matan Barak7c60bcb2015-12-15 20:30:11 +02001049 props->hca_core_clock = MLX5_CAP_GEN(mdev, device_frequency_khz);
1050 props->timestamp_mask = 0x7FFFFFFFFFFFFFFFULL;
Eli Cohene126ba92013-07-07 17:25:49 +03001051
Leon Romanovskye502b8b2019-01-08 16:07:24 +02001052 if (IS_ENABLED(CONFIG_INFINIBAND_ON_DEMAND_PAGING)) {
1053 if (MLX5_CAP_GEN(mdev, pg))
1054 props->device_cap_flags |= IB_DEVICE_ON_DEMAND_PAGING;
1055 props->odp_caps = dev->odp_caps;
1056 }
Haggai Eran8cdd3122014-12-11 17:04:20 +02001057
Leon Romanovsky051f2632015-12-20 12:16:11 +02001058 if (MLX5_CAP_GEN(mdev, cd))
1059 props->device_cap_flags |= IB_DEVICE_CROSS_CHANNEL;
1060
Eli Coheneff901d2016-03-11 22:58:42 +02001061 if (!mlx5_core_is_pf(mdev))
1062 props->device_cap_flags |= IB_DEVICE_VIRTUAL_FUNCTION;
1063
Yishai Hadas31f69a82016-08-28 11:28:45 +03001064 if (mlx5_ib_port_link_layer(ibdev, 1) ==
Daniel Jurgens85c7c012018-01-04 17:25:43 +02001065 IB_LINK_LAYER_ETHERNET && raw_support) {
Yishai Hadas31f69a82016-08-28 11:28:45 +03001066 props->rss_caps.max_rwq_indirection_tables =
1067 1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt);
1068 props->rss_caps.max_rwq_indirection_table_size =
1069 1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt_size);
1070 props->rss_caps.supported_qpts = 1 << IB_QPT_RAW_PACKET;
1071 props->max_wq_type_rq =
1072 1 << MLX5_CAP_GEN(dev->mdev, log_max_rq);
1073 }
1074
Artemy Kovalyoveb761892017-08-17 15:52:09 +03001075 if (MLX5_CAP_GEN(mdev, tag_matching)) {
Leon Romanovsky78b1beb2017-09-24 21:46:29 +03001076 props->tm_caps.max_rndv_hdr_size = MLX5_TM_MAX_RNDV_MSG_SIZE;
1077 props->tm_caps.max_num_tags =
Artemy Kovalyoveb761892017-08-17 15:52:09 +03001078 (1 << MLX5_CAP_GEN(mdev, log_tag_matching_list_sz)) - 1;
Leon Romanovsky78b1beb2017-09-24 21:46:29 +03001079 props->tm_caps.flags = IB_TM_CAP_RC;
1080 props->tm_caps.max_ops =
Artemy Kovalyoveb761892017-08-17 15:52:09 +03001081 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
Leon Romanovsky78b1beb2017-09-24 21:46:29 +03001082 props->tm_caps.max_sge = MLX5_TM_MAX_SGE;
Artemy Kovalyoveb761892017-08-17 15:52:09 +03001083 }
1084
Yonatan Cohen87ab3f52017-11-13 10:51:18 +02001085 if (MLX5_CAP_GEN(dev->mdev, cq_moderation)) {
1086 props->cq_caps.max_cq_moderation_count =
1087 MLX5_MAX_CQ_COUNT;
1088 props->cq_caps.max_cq_moderation_period =
1089 MLX5_MAX_CQ_PERIOD;
1090 }
1091
Bodong Wang7e43a2a2016-10-31 12:16:44 +02001092 if (field_avail(typeof(resp), cqe_comp_caps, uhw->outlen)) {
Bodong Wang7e43a2a2016-10-31 12:16:44 +02001093 resp.response_length += sizeof(resp.cqe_comp_caps);
Yonatan Cohen572f46b2018-05-27 13:42:33 +03001094
1095 if (MLX5_CAP_GEN(dev->mdev, cqe_compression)) {
1096 resp.cqe_comp_caps.max_num =
1097 MLX5_CAP_GEN(dev->mdev,
1098 cqe_compression_max_num);
1099
1100 resp.cqe_comp_caps.supported_format =
1101 MLX5_IB_CQE_RES_FORMAT_HASH |
1102 MLX5_IB_CQE_RES_FORMAT_CSUM;
Yonatan Cohen6f1006a2018-05-27 13:42:34 +03001103
1104 if (MLX5_CAP_GEN(dev->mdev, mini_cqe_resp_stride_index))
1105 resp.cqe_comp_caps.supported_format |=
1106 MLX5_IB_CQE_RES_FORMAT_CSUM_STRIDX;
Yonatan Cohen572f46b2018-05-27 13:42:33 +03001107 }
Bodong Wang7e43a2a2016-10-31 12:16:44 +02001108 }
1109
Daniel Jurgens85c7c012018-01-04 17:25:43 +02001110 if (field_avail(typeof(resp), packet_pacing_caps, uhw->outlen) &&
1111 raw_support) {
Bodong Wangd9491672016-12-01 13:43:13 +02001112 if (MLX5_CAP_QOS(mdev, packet_pacing) &&
1113 MLX5_CAP_GEN(mdev, qos)) {
1114 resp.packet_pacing_caps.qp_rate_limit_max =
1115 MLX5_CAP_QOS(mdev, packet_pacing_max_rate);
1116 resp.packet_pacing_caps.qp_rate_limit_min =
1117 MLX5_CAP_QOS(mdev, packet_pacing_min_rate);
1118 resp.packet_pacing_caps.supported_qpts |=
1119 1 << IB_QPT_RAW_PACKET;
Bodong Wang61147f32018-03-19 15:10:30 +02001120 if (MLX5_CAP_QOS(mdev, packet_pacing_burst_bound) &&
1121 MLX5_CAP_QOS(mdev, packet_pacing_typical_size))
1122 resp.packet_pacing_caps.cap_flags |=
1123 MLX5_IB_PP_SUPPORT_BURST;
Bodong Wangd9491672016-12-01 13:43:13 +02001124 }
1125 resp.response_length += sizeof(resp.packet_pacing_caps);
1126 }
1127
Leon Romanovsky9f885202017-01-02 11:37:39 +02001128 if (field_avail(typeof(resp), mlx5_ib_support_multi_pkt_send_wqes,
1129 uhw->outlen)) {
Bodong Wang795b6092017-08-17 15:52:34 +03001130 if (MLX5_CAP_ETH(mdev, multi_pkt_send_wqe))
1131 resp.mlx5_ib_support_multi_pkt_send_wqes =
1132 MLX5_IB_ALLOW_MPW;
Bodong Wang050da902017-08-17 15:52:35 +03001133
1134 if (MLX5_CAP_ETH(mdev, enhanced_multi_pkt_send_wqe))
1135 resp.mlx5_ib_support_multi_pkt_send_wqes |=
1136 MLX5_IB_SUPPORT_EMPW;
1137
Leon Romanovsky9f885202017-01-02 11:37:39 +02001138 resp.response_length +=
1139 sizeof(resp.mlx5_ib_support_multi_pkt_send_wqes);
1140 }
1141
Guy Levide57f2a2017-10-19 08:25:52 +03001142 if (field_avail(typeof(resp), flags, uhw->outlen)) {
1143 resp.response_length += sizeof(resp.flags);
Guy Levi7a0c8f42017-10-19 08:25:53 +03001144
Guy Levide57f2a2017-10-19 08:25:52 +03001145 if (MLX5_CAP_GEN(mdev, cqe_compression_128))
1146 resp.flags |=
1147 MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_COMP;
Guy Levi7a0c8f42017-10-19 08:25:53 +03001148
1149 if (MLX5_CAP_GEN(mdev, cqe_128_always))
1150 resp.flags |= MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_PAD;
Danit Goldberg7e11b912018-11-30 13:22:06 +02001151 if (MLX5_CAP_GEN(mdev, qp_packet_based))
1152 resp.flags |=
1153 MLX5_IB_QUERY_DEV_RESP_PACKET_BASED_CREDIT_MODE;
Guy Levide57f2a2017-10-19 08:25:52 +03001154 }
Leon Romanovsky9f885202017-01-02 11:37:39 +02001155
Noa Osherovich96dc3fc2017-08-17 15:52:28 +03001156 if (field_avail(typeof(resp), sw_parsing_caps,
1157 uhw->outlen)) {
1158 resp.response_length += sizeof(resp.sw_parsing_caps);
1159 if (MLX5_CAP_ETH(mdev, swp)) {
1160 resp.sw_parsing_caps.sw_parsing_offloads |=
1161 MLX5_IB_SW_PARSING;
1162
1163 if (MLX5_CAP_ETH(mdev, swp_csum))
1164 resp.sw_parsing_caps.sw_parsing_offloads |=
1165 MLX5_IB_SW_PARSING_CSUM;
1166
1167 if (MLX5_CAP_ETH(mdev, swp_lso))
1168 resp.sw_parsing_caps.sw_parsing_offloads |=
1169 MLX5_IB_SW_PARSING_LSO;
1170
1171 if (resp.sw_parsing_caps.sw_parsing_offloads)
1172 resp.sw_parsing_caps.supported_qpts =
1173 BIT(IB_QPT_RAW_PACKET);
1174 }
1175 }
1176
Daniel Jurgens85c7c012018-01-04 17:25:43 +02001177 if (field_avail(typeof(resp), striding_rq_caps, uhw->outlen) &&
1178 raw_support) {
Noa Osherovichb4f34592017-10-17 18:01:12 +03001179 resp.response_length += sizeof(resp.striding_rq_caps);
1180 if (MLX5_CAP_GEN(mdev, striding_rq)) {
1181 resp.striding_rq_caps.min_single_stride_log_num_of_bytes =
1182 MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES;
1183 resp.striding_rq_caps.max_single_stride_log_num_of_bytes =
1184 MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES;
1185 resp.striding_rq_caps.min_single_wqe_log_num_of_strides =
1186 MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES;
1187 resp.striding_rq_caps.max_single_wqe_log_num_of_strides =
1188 MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES;
1189 resp.striding_rq_caps.supported_qpts =
1190 BIT(IB_QPT_RAW_PACKET);
1191 }
1192 }
1193
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001194 if (field_avail(typeof(resp), tunnel_offloads_caps,
1195 uhw->outlen)) {
1196 resp.response_length += sizeof(resp.tunnel_offloads_caps);
1197 if (MLX5_CAP_ETH(mdev, tunnel_stateless_vxlan))
1198 resp.tunnel_offloads_caps |=
1199 MLX5_IB_TUNNELED_OFFLOADS_VXLAN;
1200 if (MLX5_CAP_ETH(mdev, tunnel_stateless_geneve_rx))
1201 resp.tunnel_offloads_caps |=
1202 MLX5_IB_TUNNELED_OFFLOADS_GENEVE;
1203 if (MLX5_CAP_ETH(mdev, tunnel_stateless_gre))
1204 resp.tunnel_offloads_caps |=
1205 MLX5_IB_TUNNELED_OFFLOADS_GRE;
Ariel Levkoviche818e252018-05-13 14:33:35 +03001206 if (MLX5_CAP_GEN(mdev, flex_parser_protocols) &
1207 MLX5_FLEX_PROTO_CW_MPLS_GRE)
1208 resp.tunnel_offloads_caps |=
1209 MLX5_IB_TUNNELED_OFFLOADS_MPLS_GRE;
1210 if (MLX5_CAP_GEN(mdev, flex_parser_protocols) &
1211 MLX5_FLEX_PROTO_CW_MPLS_UDP)
1212 resp.tunnel_offloads_caps |=
1213 MLX5_IB_TUNNELED_OFFLOADS_MPLS_UDP;
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001214 }
1215
Bodong Wang402ca532016-06-17 15:02:20 +03001216 if (uhw->outlen) {
1217 err = ib_copy_to_udata(uhw, &resp, resp.response_length);
1218
1219 if (err)
1220 return err;
1221 }
1222
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001223 return 0;
1224}
Eli Cohene126ba92013-07-07 17:25:49 +03001225
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001226enum mlx5_ib_width {
1227 MLX5_IB_WIDTH_1X = 1 << 0,
1228 MLX5_IB_WIDTH_2X = 1 << 1,
1229 MLX5_IB_WIDTH_4X = 1 << 2,
1230 MLX5_IB_WIDTH_8X = 1 << 3,
1231 MLX5_IB_WIDTH_12X = 1 << 4
1232};
1233
Michael Guralnikdb7a6912018-11-21 15:03:54 +02001234static void translate_active_width(struct ib_device *ibdev, u8 active_width,
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001235 u8 *ib_width)
1236{
1237 struct mlx5_ib_dev *dev = to_mdev(ibdev);
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001238
Michael Guralnikdb7a6912018-11-21 15:03:54 +02001239 if (active_width & MLX5_IB_WIDTH_1X)
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001240 *ib_width = IB_WIDTH_1X;
Michael Guralnikd7649702018-12-09 11:49:54 +02001241 else if (active_width & MLX5_IB_WIDTH_2X)
1242 *ib_width = IB_WIDTH_2X;
Michael Guralnikdb7a6912018-11-21 15:03:54 +02001243 else if (active_width & MLX5_IB_WIDTH_4X)
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001244 *ib_width = IB_WIDTH_4X;
Michael Guralnikdb7a6912018-11-21 15:03:54 +02001245 else if (active_width & MLX5_IB_WIDTH_8X)
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001246 *ib_width = IB_WIDTH_8X;
Michael Guralnikdb7a6912018-11-21 15:03:54 +02001247 else if (active_width & MLX5_IB_WIDTH_12X)
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001248 *ib_width = IB_WIDTH_12X;
Michael Guralnikdb7a6912018-11-21 15:03:54 +02001249 else {
1250 mlx5_ib_dbg(dev, "Invalid active_width %d, setting width to default value: 4x\n",
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001251 (int)active_width);
Michael Guralnikdb7a6912018-11-21 15:03:54 +02001252 *ib_width = IB_WIDTH_4X;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001253 }
1254
Michael Guralnikdb7a6912018-11-21 15:03:54 +02001255 return;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001256}
1257
1258static int mlx5_mtu_to_ib_mtu(int mtu)
1259{
1260 switch (mtu) {
1261 case 256: return 1;
1262 case 512: return 2;
1263 case 1024: return 3;
1264 case 2048: return 4;
1265 case 4096: return 5;
1266 default:
1267 pr_warn("invalid mtu\n");
1268 return -1;
1269 }
1270}
1271
1272enum ib_max_vl_num {
1273 __IB_MAX_VL_0 = 1,
1274 __IB_MAX_VL_0_1 = 2,
1275 __IB_MAX_VL_0_3 = 3,
1276 __IB_MAX_VL_0_7 = 4,
1277 __IB_MAX_VL_0_14 = 5,
1278};
1279
1280enum mlx5_vl_hw_cap {
1281 MLX5_VL_HW_0 = 1,
1282 MLX5_VL_HW_0_1 = 2,
1283 MLX5_VL_HW_0_2 = 3,
1284 MLX5_VL_HW_0_3 = 4,
1285 MLX5_VL_HW_0_4 = 5,
1286 MLX5_VL_HW_0_5 = 6,
1287 MLX5_VL_HW_0_6 = 7,
1288 MLX5_VL_HW_0_7 = 8,
1289 MLX5_VL_HW_0_14 = 15
1290};
1291
1292static int translate_max_vl_num(struct ib_device *ibdev, u8 vl_hw_cap,
1293 u8 *max_vl_num)
1294{
1295 switch (vl_hw_cap) {
1296 case MLX5_VL_HW_0:
1297 *max_vl_num = __IB_MAX_VL_0;
1298 break;
1299 case MLX5_VL_HW_0_1:
1300 *max_vl_num = __IB_MAX_VL_0_1;
1301 break;
1302 case MLX5_VL_HW_0_3:
1303 *max_vl_num = __IB_MAX_VL_0_3;
1304 break;
1305 case MLX5_VL_HW_0_7:
1306 *max_vl_num = __IB_MAX_VL_0_7;
1307 break;
1308 case MLX5_VL_HW_0_14:
1309 *max_vl_num = __IB_MAX_VL_0_14;
1310 break;
1311
1312 default:
1313 return -EINVAL;
1314 }
1315
1316 return 0;
1317}
1318
1319static int mlx5_query_hca_port(struct ib_device *ibdev, u8 port,
1320 struct ib_port_attr *props)
1321{
1322 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1323 struct mlx5_core_dev *mdev = dev->mdev;
1324 struct mlx5_hca_vport_context *rep;
Saeed Mahameed046339e2016-04-22 00:33:03 +03001325 u16 max_mtu;
1326 u16 oper_mtu;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001327 int err;
1328 u8 ib_link_width_oper;
1329 u8 vl_hw_cap;
1330
1331 rep = kzalloc(sizeof(*rep), GFP_KERNEL);
1332 if (!rep) {
1333 err = -ENOMEM;
1334 goto out;
1335 }
1336
Or Gerlitzc4550c62017-01-24 13:02:39 +02001337 /* props being zeroed by the caller, avoid zeroing it here */
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001338
1339 err = mlx5_query_hca_vport_context(mdev, 0, port, 0, rep);
1340 if (err)
1341 goto out;
1342
1343 props->lid = rep->lid;
1344 props->lmc = rep->lmc;
1345 props->sm_lid = rep->sm_lid;
1346 props->sm_sl = rep->sm_sl;
1347 props->state = rep->vport_state;
1348 props->phys_state = rep->port_physical_state;
1349 props->port_cap_flags = rep->cap_mask1;
1350 props->gid_tbl_len = mlx5_get_gid_table_len(MLX5_CAP_GEN(mdev, gid_table_size));
1351 props->max_msg_sz = 1 << MLX5_CAP_GEN(mdev, log_max_msg);
1352 props->pkey_tbl_len = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, pkey_table_size));
1353 props->bad_pkey_cntr = rep->pkey_violation_counter;
1354 props->qkey_viol_cntr = rep->qkey_violation_counter;
1355 props->subnet_timeout = rep->subnet_timeout;
1356 props->init_type_reply = rep->init_type_reply;
1357
Michael Guralnik4106a752018-12-09 11:49:51 +02001358 if (props->port_cap_flags & IB_PORT_CAP_MASK2_SUP)
1359 props->port_cap_flags2 = rep->cap_mask2;
1360
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001361 err = mlx5_query_port_link_width_oper(mdev, &ib_link_width_oper, port);
1362 if (err)
1363 goto out;
1364
Michael Guralnikdb7a6912018-11-21 15:03:54 +02001365 translate_active_width(ibdev, ib_link_width_oper, &props->active_width);
1366
Noa Osherovichd5beb7f2016-06-02 10:47:53 +03001367 err = mlx5_query_port_ib_proto_oper(mdev, &props->active_speed, port);
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001368 if (err)
1369 goto out;
1370
Saeed Mahameedfacc9692015-06-11 14:47:27 +03001371 mlx5_query_port_max_mtu(mdev, &max_mtu, port);
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001372
1373 props->max_mtu = mlx5_mtu_to_ib_mtu(max_mtu);
1374
Saeed Mahameedfacc9692015-06-11 14:47:27 +03001375 mlx5_query_port_oper_mtu(mdev, &oper_mtu, port);
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001376
1377 props->active_mtu = mlx5_mtu_to_ib_mtu(oper_mtu);
1378
1379 err = mlx5_query_port_vl_hw_cap(mdev, &vl_hw_cap, port);
1380 if (err)
1381 goto out;
1382
1383 err = translate_max_vl_num(ibdev, vl_hw_cap,
1384 &props->max_vl_num);
1385out:
1386 kfree(rep);
Eli Cohene126ba92013-07-07 17:25:49 +03001387 return err;
1388}
1389
1390int mlx5_ib_query_port(struct ib_device *ibdev, u8 port,
1391 struct ib_port_attr *props)
1392{
Ilan Tayari095b0922017-05-14 16:04:30 +03001393 unsigned int count;
1394 int ret;
1395
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001396 switch (mlx5_get_vport_access_method(ibdev)) {
1397 case MLX5_VPORT_ACCESS_METHOD_MAD:
Ilan Tayari095b0922017-05-14 16:04:30 +03001398 ret = mlx5_query_mad_ifc_port(ibdev, port, props);
1399 break;
Eli Cohene126ba92013-07-07 17:25:49 +03001400
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001401 case MLX5_VPORT_ACCESS_METHOD_HCA:
Ilan Tayari095b0922017-05-14 16:04:30 +03001402 ret = mlx5_query_hca_port(ibdev, port, props);
1403 break;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001404
Achiad Shochat3f89a642015-12-23 18:47:21 +02001405 case MLX5_VPORT_ACCESS_METHOD_NIC:
Ilan Tayari095b0922017-05-14 16:04:30 +03001406 ret = mlx5_query_port_roce(ibdev, port, props);
1407 break;
Achiad Shochat3f89a642015-12-23 18:47:21 +02001408
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001409 default:
Ilan Tayari095b0922017-05-14 16:04:30 +03001410 ret = -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +03001411 }
Ilan Tayari095b0922017-05-14 16:04:30 +03001412
1413 if (!ret && props) {
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001414 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1415 struct mlx5_core_dev *mdev;
1416 bool put_mdev = true;
1417
1418 mdev = mlx5_ib_get_native_port_mdev(dev, port, NULL);
1419 if (!mdev) {
1420 /* If the port isn't affiliated yet query the master.
1421 * The master and slave will have the same values.
1422 */
1423 mdev = dev->mdev;
1424 port = 1;
1425 put_mdev = false;
1426 }
1427 count = mlx5_core_reserved_gids_count(mdev);
1428 if (put_mdev)
1429 mlx5_ib_put_native_port_mdev(dev, port);
Ilan Tayari095b0922017-05-14 16:04:30 +03001430 props->gid_tbl_len -= count;
1431 }
1432 return ret;
Eli Cohene126ba92013-07-07 17:25:49 +03001433}
1434
Mark Bloch8e6efa32017-11-06 12:22:13 +00001435static int mlx5_ib_rep_query_port(struct ib_device *ibdev, u8 port,
1436 struct ib_port_attr *props)
1437{
1438 int ret;
1439
Mark Bloch26628e22019-03-28 15:27:41 +02001440 /* Only link layer == ethernet is valid for representors
1441 * and we always use port 1
1442 */
Mark Bloch8e6efa32017-11-06 12:22:13 +00001443 ret = mlx5_query_port_roce(ibdev, port, props);
1444 if (ret || !props)
1445 return ret;
1446
1447 /* We don't support GIDS */
1448 props->gid_tbl_len = 0;
1449
1450 return ret;
1451}
1452
Eli Cohene126ba92013-07-07 17:25:49 +03001453static int mlx5_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
1454 union ib_gid *gid)
1455{
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001456 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1457 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03001458
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001459 switch (mlx5_get_vport_access_method(ibdev)) {
1460 case MLX5_VPORT_ACCESS_METHOD_MAD:
1461 return mlx5_query_mad_ifc_gids(ibdev, port, index, gid);
Eli Cohene126ba92013-07-07 17:25:49 +03001462
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001463 case MLX5_VPORT_ACCESS_METHOD_HCA:
1464 return mlx5_query_hca_vport_gid(mdev, 0, port, 0, index, gid);
Eli Cohene126ba92013-07-07 17:25:49 +03001465
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001466 default:
1467 return -EINVAL;
1468 }
Eli Cohene126ba92013-07-07 17:25:49 +03001469
Eli Cohene126ba92013-07-07 17:25:49 +03001470}
1471
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001472static int mlx5_query_hca_nic_pkey(struct ib_device *ibdev, u8 port,
1473 u16 index, u16 *pkey)
1474{
1475 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1476 struct mlx5_core_dev *mdev;
1477 bool put_mdev = true;
1478 u8 mdev_port_num;
1479 int err;
1480
1481 mdev = mlx5_ib_get_native_port_mdev(dev, port, &mdev_port_num);
1482 if (!mdev) {
1483 /* The port isn't affiliated yet, get the PKey from the master
1484 * port. For RoCE the PKey tables will be the same.
1485 */
1486 put_mdev = false;
1487 mdev = dev->mdev;
1488 mdev_port_num = 1;
1489 }
1490
1491 err = mlx5_query_hca_vport_pkey(mdev, 0, mdev_port_num, 0,
1492 index, pkey);
1493 if (put_mdev)
1494 mlx5_ib_put_native_port_mdev(dev, port);
1495
1496 return err;
1497}
1498
Eli Cohene126ba92013-07-07 17:25:49 +03001499static int mlx5_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
1500 u16 *pkey)
1501{
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001502 switch (mlx5_get_vport_access_method(ibdev)) {
1503 case MLX5_VPORT_ACCESS_METHOD_MAD:
1504 return mlx5_query_mad_ifc_pkey(ibdev, port, index, pkey);
Eli Cohene126ba92013-07-07 17:25:49 +03001505
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001506 case MLX5_VPORT_ACCESS_METHOD_HCA:
1507 case MLX5_VPORT_ACCESS_METHOD_NIC:
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001508 return mlx5_query_hca_nic_pkey(ibdev, port, index, pkey);
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001509 default:
1510 return -EINVAL;
1511 }
Eli Cohene126ba92013-07-07 17:25:49 +03001512}
1513
Eli Cohene126ba92013-07-07 17:25:49 +03001514static int mlx5_ib_modify_device(struct ib_device *ibdev, int mask,
1515 struct ib_device_modify *props)
1516{
1517 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1518 struct mlx5_reg_node_desc in;
1519 struct mlx5_reg_node_desc out;
1520 int err;
1521
1522 if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
1523 return -EOPNOTSUPP;
1524
1525 if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
1526 return 0;
1527
1528 /*
1529 * If possible, pass node desc to FW, so it can generate
1530 * a 144 trap. If cmd fails, just ignore.
1531 */
Yuval Shaiabd99fde2016-08-25 10:57:07 -07001532 memcpy(&in, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
Jack Morgenstein9603b612014-07-28 23:30:22 +03001533 err = mlx5_core_access_reg(dev->mdev, &in, sizeof(in), &out,
Eli Cohene126ba92013-07-07 17:25:49 +03001534 sizeof(out), MLX5_REG_NODE_DESC, 0, 1);
1535 if (err)
1536 return err;
1537
Yuval Shaiabd99fde2016-08-25 10:57:07 -07001538 memcpy(ibdev->node_desc, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
Eli Cohene126ba92013-07-07 17:25:49 +03001539
1540 return err;
1541}
1542
Eli Cohencdbe33d2017-02-14 07:25:38 +02001543static int set_port_caps_atomic(struct mlx5_ib_dev *dev, u8 port_num, u32 mask,
1544 u32 value)
1545{
1546 struct mlx5_hca_vport_context ctx = {};
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001547 struct mlx5_core_dev *mdev;
1548 u8 mdev_port_num;
Eli Cohencdbe33d2017-02-14 07:25:38 +02001549 int err;
1550
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001551 mdev = mlx5_ib_get_native_port_mdev(dev, port_num, &mdev_port_num);
1552 if (!mdev)
1553 return -ENODEV;
1554
1555 err = mlx5_query_hca_vport_context(mdev, 0, mdev_port_num, 0, &ctx);
Eli Cohencdbe33d2017-02-14 07:25:38 +02001556 if (err)
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001557 goto out;
Eli Cohencdbe33d2017-02-14 07:25:38 +02001558
1559 if (~ctx.cap_mask1_perm & mask) {
1560 mlx5_ib_warn(dev, "trying to change bitmask 0x%X but change supported 0x%X\n",
1561 mask, ctx.cap_mask1_perm);
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001562 err = -EINVAL;
1563 goto out;
Eli Cohencdbe33d2017-02-14 07:25:38 +02001564 }
1565
1566 ctx.cap_mask1 = value;
1567 ctx.cap_mask1_perm = mask;
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001568 err = mlx5_core_modify_hca_vport_context(mdev, 0, mdev_port_num,
1569 0, &ctx);
1570
1571out:
1572 mlx5_ib_put_native_port_mdev(dev, port_num);
Eli Cohencdbe33d2017-02-14 07:25:38 +02001573
1574 return err;
1575}
1576
Eli Cohene126ba92013-07-07 17:25:49 +03001577static int mlx5_ib_modify_port(struct ib_device *ibdev, u8 port, int mask,
1578 struct ib_port_modify *props)
1579{
1580 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1581 struct ib_port_attr attr;
1582 u32 tmp;
1583 int err;
Eli Cohencdbe33d2017-02-14 07:25:38 +02001584 u32 change_mask;
1585 u32 value;
1586 bool is_ib = (mlx5_ib_port_link_layer(ibdev, port) ==
1587 IB_LINK_LAYER_INFINIBAND);
1588
Majd Dibbinyec255872017-08-23 08:35:42 +03001589 /* CM layer calls ib_modify_port() regardless of the link layer. For
1590 * Ethernet ports, qkey violation and Port capabilities are meaningless.
1591 */
1592 if (!is_ib)
1593 return 0;
1594
Eli Cohencdbe33d2017-02-14 07:25:38 +02001595 if (MLX5_CAP_GEN(dev->mdev, ib_virt) && is_ib) {
1596 change_mask = props->clr_port_cap_mask | props->set_port_cap_mask;
1597 value = ~props->clr_port_cap_mask | props->set_port_cap_mask;
1598 return set_port_caps_atomic(dev, port, change_mask, value);
1599 }
Eli Cohene126ba92013-07-07 17:25:49 +03001600
1601 mutex_lock(&dev->cap_mask_mutex);
1602
Or Gerlitzc4550c62017-01-24 13:02:39 +02001603 err = ib_query_port(ibdev, port, &attr);
Eli Cohene126ba92013-07-07 17:25:49 +03001604 if (err)
1605 goto out;
1606
1607 tmp = (attr.port_cap_flags | props->set_port_cap_mask) &
1608 ~props->clr_port_cap_mask;
1609
Jack Morgenstein9603b612014-07-28 23:30:22 +03001610 err = mlx5_set_port_caps(dev->mdev, port, tmp);
Eli Cohene126ba92013-07-07 17:25:49 +03001611
1612out:
1613 mutex_unlock(&dev->cap_mask_mutex);
1614 return err;
1615}
1616
Eli Cohen30aa60b2017-01-03 23:55:27 +02001617static void print_lib_caps(struct mlx5_ib_dev *dev, u64 caps)
1618{
1619 mlx5_ib_dbg(dev, "MLX5_LIB_CAP_4K_UAR = %s\n",
1620 caps & MLX5_LIB_CAP_4K_UAR ? "y" : "n");
1621}
1622
Yishai Hadas31a78a52017-12-24 16:31:34 +02001623static u16 calc_dynamic_bfregs(int uars_per_sys_page)
1624{
1625 /* Large page with non 4k uar support might limit the dynamic size */
1626 if (uars_per_sys_page == 1 && PAGE_SIZE > 4096)
1627 return MLX5_MIN_DYN_BFREGS;
1628
1629 return MLX5_MAX_DYN_BFREGS;
1630}
1631
Eli Cohenb037c292017-01-03 23:55:26 +02001632static int calc_total_bfregs(struct mlx5_ib_dev *dev, bool lib_uar_4k,
1633 struct mlx5_ib_alloc_ucontext_req_v2 *req,
Yishai Hadas31a78a52017-12-24 16:31:34 +02001634 struct mlx5_bfreg_info *bfregi)
Eli Cohenb037c292017-01-03 23:55:26 +02001635{
1636 int uars_per_sys_page;
1637 int bfregs_per_sys_page;
1638 int ref_bfregs = req->total_num_bfregs;
1639
1640 if (req->total_num_bfregs == 0)
1641 return -EINVAL;
1642
1643 BUILD_BUG_ON(MLX5_MAX_BFREGS % MLX5_NON_FP_BFREGS_IN_PAGE);
1644 BUILD_BUG_ON(MLX5_MAX_BFREGS < MLX5_NON_FP_BFREGS_IN_PAGE);
1645
1646 if (req->total_num_bfregs > MLX5_MAX_BFREGS)
1647 return -ENOMEM;
1648
1649 uars_per_sys_page = get_uars_per_sys_page(dev, lib_uar_4k);
1650 bfregs_per_sys_page = uars_per_sys_page * MLX5_NON_FP_BFREGS_PER_UAR;
Yishai Hadas31a78a52017-12-24 16:31:34 +02001651 /* This holds the required static allocation asked by the user */
Eli Cohenb037c292017-01-03 23:55:26 +02001652 req->total_num_bfregs = ALIGN(req->total_num_bfregs, bfregs_per_sys_page);
Eli Cohenb037c292017-01-03 23:55:26 +02001653 if (req->num_low_latency_bfregs > req->total_num_bfregs - 1)
1654 return -EINVAL;
1655
Yishai Hadas31a78a52017-12-24 16:31:34 +02001656 bfregi->num_static_sys_pages = req->total_num_bfregs / bfregs_per_sys_page;
1657 bfregi->num_dyn_bfregs = ALIGN(calc_dynamic_bfregs(uars_per_sys_page), bfregs_per_sys_page);
1658 bfregi->total_num_bfregs = req->total_num_bfregs + bfregi->num_dyn_bfregs;
1659 bfregi->num_sys_pages = bfregi->total_num_bfregs / bfregs_per_sys_page;
1660
1661 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 +02001662 MLX5_CAP_GEN(dev->mdev, uar_4k) ? "yes" : "no",
1663 lib_uar_4k ? "yes" : "no", ref_bfregs,
Yishai Hadas31a78a52017-12-24 16:31:34 +02001664 req->total_num_bfregs, bfregi->total_num_bfregs,
1665 bfregi->num_sys_pages);
Eli Cohenb037c292017-01-03 23:55:26 +02001666
1667 return 0;
1668}
1669
1670static int allocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
1671{
1672 struct mlx5_bfreg_info *bfregi;
1673 int err;
1674 int i;
1675
1676 bfregi = &context->bfregi;
Yishai Hadas31a78a52017-12-24 16:31:34 +02001677 for (i = 0; i < bfregi->num_static_sys_pages; i++) {
Eli Cohenb037c292017-01-03 23:55:26 +02001678 err = mlx5_cmd_alloc_uar(dev->mdev, &bfregi->sys_pages[i]);
1679 if (err)
1680 goto error;
1681
1682 mlx5_ib_dbg(dev, "allocated uar %d\n", bfregi->sys_pages[i]);
1683 }
Yishai Hadas4ed131d2017-12-24 16:31:35 +02001684
1685 for (i = bfregi->num_static_sys_pages; i < bfregi->num_sys_pages; i++)
1686 bfregi->sys_pages[i] = MLX5_IB_INVALID_UAR_INDEX;
1687
Eli Cohenb037c292017-01-03 23:55:26 +02001688 return 0;
1689
1690error:
1691 for (--i; i >= 0; i--)
1692 if (mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]))
1693 mlx5_ib_warn(dev, "failed to free uar %d\n", i);
1694
1695 return err;
1696}
1697
Leon Romanovsky15177992018-06-27 10:44:24 +03001698static void deallocate_uars(struct mlx5_ib_dev *dev,
1699 struct mlx5_ib_ucontext *context)
Eli Cohenb037c292017-01-03 23:55:26 +02001700{
1701 struct mlx5_bfreg_info *bfregi;
Eli Cohenb037c292017-01-03 23:55:26 +02001702 int i;
1703
1704 bfregi = &context->bfregi;
Leon Romanovsky15177992018-06-27 10:44:24 +03001705 for (i = 0; i < bfregi->num_sys_pages; i++)
Yishai Hadas4ed131d2017-12-24 16:31:35 +02001706 if (i < bfregi->num_static_sys_pages ||
Leon Romanovsky15177992018-06-27 10:44:24 +03001707 bfregi->sys_pages[i] != MLX5_IB_INVALID_UAR_INDEX)
1708 mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]);
Eli Cohenb037c292017-01-03 23:55:26 +02001709}
1710
Mark Bloch0042f9e2018-09-17 13:30:49 +03001711int mlx5_ib_enable_lb(struct mlx5_ib_dev *dev, bool td, bool qp)
Mark Blocha560f1d2018-09-17 13:30:47 +03001712{
1713 int err = 0;
1714
1715 mutex_lock(&dev->lb.mutex);
Mark Bloch0042f9e2018-09-17 13:30:49 +03001716 if (td)
1717 dev->lb.user_td++;
1718 if (qp)
1719 dev->lb.qps++;
Mark Blocha560f1d2018-09-17 13:30:47 +03001720
Mark Bloch0042f9e2018-09-17 13:30:49 +03001721 if (dev->lb.user_td == 2 ||
1722 dev->lb.qps == 1) {
1723 if (!dev->lb.enabled) {
1724 err = mlx5_nic_vport_update_local_lb(dev->mdev, true);
1725 dev->lb.enabled = true;
1726 }
1727 }
Mark Blocha560f1d2018-09-17 13:30:47 +03001728
1729 mutex_unlock(&dev->lb.mutex);
1730
1731 return err;
1732}
1733
Mark Bloch0042f9e2018-09-17 13:30:49 +03001734void mlx5_ib_disable_lb(struct mlx5_ib_dev *dev, bool td, bool qp)
Mark Blocha560f1d2018-09-17 13:30:47 +03001735{
1736 mutex_lock(&dev->lb.mutex);
Mark Bloch0042f9e2018-09-17 13:30:49 +03001737 if (td)
1738 dev->lb.user_td--;
1739 if (qp)
1740 dev->lb.qps--;
Mark Blocha560f1d2018-09-17 13:30:47 +03001741
Mark Bloch0042f9e2018-09-17 13:30:49 +03001742 if (dev->lb.user_td == 1 &&
1743 dev->lb.qps == 0) {
1744 if (dev->lb.enabled) {
1745 mlx5_nic_vport_update_local_lb(dev->mdev, false);
1746 dev->lb.enabled = false;
1747 }
1748 }
Mark Blocha560f1d2018-09-17 13:30:47 +03001749
1750 mutex_unlock(&dev->lb.mutex);
1751}
1752
Yishai Hadasd2d19122018-09-20 21:39:32 +03001753static int mlx5_ib_alloc_transport_domain(struct mlx5_ib_dev *dev, u32 *tdn,
1754 u16 uid)
Huy Nguyenc85023e2017-05-30 09:42:54 +03001755{
1756 int err;
1757
Leon Romanovskycfdeb892018-06-19 10:39:06 +03001758 if (!MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1759 return 0;
1760
Yishai Hadasd2d19122018-09-20 21:39:32 +03001761 err = mlx5_cmd_alloc_transport_domain(dev->mdev, tdn, uid);
Huy Nguyenc85023e2017-05-30 09:42:54 +03001762 if (err)
1763 return err;
1764
1765 if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
Eran Ben Elisha8978cc92018-01-09 11:41:10 +02001766 (!MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) &&
1767 !MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
Huy Nguyenc85023e2017-05-30 09:42:54 +03001768 return err;
1769
Mark Bloch0042f9e2018-09-17 13:30:49 +03001770 return mlx5_ib_enable_lb(dev, true, false);
Huy Nguyenc85023e2017-05-30 09:42:54 +03001771}
1772
Yishai Hadasd2d19122018-09-20 21:39:32 +03001773static void mlx5_ib_dealloc_transport_domain(struct mlx5_ib_dev *dev, u32 tdn,
1774 u16 uid)
Huy Nguyenc85023e2017-05-30 09:42:54 +03001775{
Leon Romanovskycfdeb892018-06-19 10:39:06 +03001776 if (!MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1777 return;
1778
Yishai Hadasd2d19122018-09-20 21:39:32 +03001779 mlx5_cmd_dealloc_transport_domain(dev->mdev, tdn, uid);
Huy Nguyenc85023e2017-05-30 09:42:54 +03001780
1781 if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
Eran Ben Elisha8978cc92018-01-09 11:41:10 +02001782 (!MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) &&
1783 !MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
Huy Nguyenc85023e2017-05-30 09:42:54 +03001784 return;
1785
Mark Bloch0042f9e2018-09-17 13:30:49 +03001786 mlx5_ib_disable_lb(dev, true, false);
Huy Nguyenc85023e2017-05-30 09:42:54 +03001787}
1788
Leon Romanovskya2a074e2019-02-12 20:39:16 +02001789static int mlx5_ib_alloc_ucontext(struct ib_ucontext *uctx,
1790 struct ib_udata *udata)
Eli Cohene126ba92013-07-07 17:25:49 +03001791{
Leon Romanovskya2a074e2019-02-12 20:39:16 +02001792 struct ib_device *ibdev = uctx->device;
Eli Cohene126ba92013-07-07 17:25:49 +03001793 struct mlx5_ib_dev *dev = to_mdev(ibdev);
Matan Barakb368d7c2015-12-15 20:30:12 +02001794 struct mlx5_ib_alloc_ucontext_req_v2 req = {};
1795 struct mlx5_ib_alloc_ucontext_resp resp = {};
Feras Daoud5c99eae2018-01-16 20:08:41 +02001796 struct mlx5_core_dev *mdev = dev->mdev;
Leon Romanovskya2a074e2019-02-12 20:39:16 +02001797 struct mlx5_ib_ucontext *context = to_mucontext(uctx);
Eli Cohen2f5ff262017-01-03 23:55:21 +02001798 struct mlx5_bfreg_info *bfregi;
Eli Cohen78c0f982014-01-30 13:49:48 +02001799 int ver;
Eli Cohene126ba92013-07-07 17:25:49 +03001800 int err;
Majd Dibbinya168a41c2016-01-28 17:51:47 +02001801 size_t min_req_v2 = offsetof(struct mlx5_ib_alloc_ucontext_req_v2,
1802 max_cqe_version);
Yonatan Cohen25bb36e2018-06-19 08:47:24 +03001803 u32 dump_fill_mkey;
Eli Cohenb037c292017-01-03 23:55:26 +02001804 bool lib_uar_4k;
Eli Cohene126ba92013-07-07 17:25:49 +03001805
1806 if (!dev->ib_active)
Leon Romanovskya2a074e2019-02-12 20:39:16 +02001807 return -EAGAIN;
Eli Cohene126ba92013-07-07 17:25:49 +03001808
Amrani, Rame0931112017-06-27 17:04:42 +03001809 if (udata->inlen == sizeof(struct mlx5_ib_alloc_ucontext_req))
Eli Cohen78c0f982014-01-30 13:49:48 +02001810 ver = 0;
Amrani, Rame0931112017-06-27 17:04:42 +03001811 else if (udata->inlen >= min_req_v2)
Eli Cohen78c0f982014-01-30 13:49:48 +02001812 ver = 2;
1813 else
Leon Romanovskya2a074e2019-02-12 20:39:16 +02001814 return -EINVAL;
Eli Cohen78c0f982014-01-30 13:49:48 +02001815
Amrani, Rame0931112017-06-27 17:04:42 +03001816 err = ib_copy_from_udata(&req, udata, min(udata->inlen, sizeof(req)));
Eli Cohene126ba92013-07-07 17:25:49 +03001817 if (err)
Leon Romanovskya2a074e2019-02-12 20:39:16 +02001818 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03001819
Yishai Hadasa8b92ca2018-06-17 12:59:57 +03001820 if (req.flags & ~MLX5_IB_ALLOC_UCTX_DEVX)
Leon Romanovskya2a074e2019-02-12 20:39:16 +02001821 return -EOPNOTSUPP;
Eli Cohen78c0f982014-01-30 13:49:48 +02001822
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001823 if (req.comp_mask || req.reserved0 || req.reserved1 || req.reserved2)
Leon Romanovskya2a074e2019-02-12 20:39:16 +02001824 return -EOPNOTSUPP;
Matan Barakb368d7c2015-12-15 20:30:12 +02001825
Eli Cohen2f5ff262017-01-03 23:55:21 +02001826 req.total_num_bfregs = ALIGN(req.total_num_bfregs,
1827 MLX5_NON_FP_BFREGS_PER_UAR);
1828 if (req.num_low_latency_bfregs > req.total_num_bfregs - 1)
Leon Romanovskya2a074e2019-02-12 20:39:16 +02001829 return -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +03001830
Saeed Mahameed938fe832015-05-28 22:28:41 +03001831 resp.qp_tab_size = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp);
Noa Osherovich2cc6ad52016-06-04 15:15:33 +03001832 if (mlx5_core_is_pf(dev->mdev) && MLX5_CAP_GEN(dev->mdev, bf))
1833 resp.bf_reg_size = 1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size);
Daniel Jurgensb47bd6e2016-10-25 18:36:24 +03001834 resp.cache_line_size = cache_line_size();
Saeed Mahameed938fe832015-05-28 22:28:41 +03001835 resp.max_sq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq);
1836 resp.max_rq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq);
1837 resp.max_send_wqebb = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1838 resp.max_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1839 resp.max_srq_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz);
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001840 resp.cqe_version = min_t(__u8,
1841 (__u8)MLX5_CAP_GEN(dev->mdev, cqe_version),
1842 req.max_cqe_version);
Eli Cohen30aa60b2017-01-03 23:55:27 +02001843 resp.log_uar_size = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
1844 MLX5_ADAPTER_PAGE_SHIFT : PAGE_SHIFT;
1845 resp.num_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
1846 MLX5_CAP_GEN(dev->mdev, num_of_uars_per_page) : 1;
Matan Barakb368d7c2015-12-15 20:30:12 +02001847 resp.response_length = min(offsetof(typeof(resp), response_length) +
1848 sizeof(resp.response_length), udata->outlen);
Eli Cohene126ba92013-07-07 17:25:49 +03001849
Matan Barakc03faa52018-03-28 09:27:54 +03001850 if (mlx5_accel_ipsec_device_caps(dev->mdev) & MLX5_ACCEL_IPSEC_CAP_DEVICE) {
1851 if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_EGRESS))
1852 resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM;
1853 if (mlx5_accel_ipsec_device_caps(dev->mdev) & MLX5_ACCEL_IPSEC_CAP_REQUIRED_METADATA)
1854 resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_REQ_METADATA;
1855 if (MLX5_CAP_FLOWTABLE(dev->mdev, flow_table_properties_nic_receive.ft_field_support.outer_esp_spi))
1856 resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_SPI_STEERING;
1857 if (mlx5_accel_ipsec_device_caps(dev->mdev) & MLX5_ACCEL_IPSEC_CAP_TX_IV_IS_ESN)
1858 resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_TX_IV_IS_ESN;
1859 /* MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_FULL_OFFLOAD is currently always 0 */
1860 }
1861
Eli Cohen30aa60b2017-01-03 23:55:27 +02001862 lib_uar_4k = req.lib_caps & MLX5_LIB_CAP_4K_UAR;
Eli Cohen2f5ff262017-01-03 23:55:21 +02001863 bfregi = &context->bfregi;
Eli Cohenb037c292017-01-03 23:55:26 +02001864
1865 /* updates req->total_num_bfregs */
Yishai Hadas31a78a52017-12-24 16:31:34 +02001866 err = calc_total_bfregs(dev, lib_uar_4k, &req, bfregi);
Eli Cohenb037c292017-01-03 23:55:26 +02001867 if (err)
1868 goto out_ctx;
1869
Eli Cohen2f5ff262017-01-03 23:55:21 +02001870 mutex_init(&bfregi->lock);
Eli Cohenb037c292017-01-03 23:55:26 +02001871 bfregi->lib_uar_4k = lib_uar_4k;
Yishai Hadas31a78a52017-12-24 16:31:34 +02001872 bfregi->count = kcalloc(bfregi->total_num_bfregs, sizeof(*bfregi->count),
Eli Cohenb037c292017-01-03 23:55:26 +02001873 GFP_KERNEL);
1874 if (!bfregi->count) {
Eli Cohene126ba92013-07-07 17:25:49 +03001875 err = -ENOMEM;
1876 goto out_ctx;
1877 }
1878
Eli Cohenb037c292017-01-03 23:55:26 +02001879 bfregi->sys_pages = kcalloc(bfregi->num_sys_pages,
1880 sizeof(*bfregi->sys_pages),
1881 GFP_KERNEL);
1882 if (!bfregi->sys_pages) {
Eli Cohene126ba92013-07-07 17:25:49 +03001883 err = -ENOMEM;
Eli Cohenb037c292017-01-03 23:55:26 +02001884 goto out_count;
Eli Cohene126ba92013-07-07 17:25:49 +03001885 }
1886
Eli Cohenb037c292017-01-03 23:55:26 +02001887 err = allocate_uars(dev, context);
1888 if (err)
1889 goto out_sys_pages;
Eli Cohene126ba92013-07-07 17:25:49 +03001890
Leon Romanovsky13859d5d2019-01-08 16:07:26 +02001891 if (ibdev->attrs.device_cap_flags & IB_DEVICE_ON_DEMAND_PAGING)
1892 context->ibucontext.invalidate_range =
1893 &mlx5_ib_invalidate_range;
Haggai Eranb4cfe442014-12-11 17:04:26 +02001894
Yishai Hadasa8b92ca2018-06-17 12:59:57 +03001895 if (req.flags & MLX5_IB_ALLOC_UCTX_DEVX) {
Yishai Hadasfb981532018-11-26 08:28:36 +02001896 err = mlx5_ib_devx_create(dev, true);
Yishai Hadas76dc5a82018-09-20 21:45:19 +03001897 if (err < 0)
Yishai Hadasd2d19122018-09-20 21:39:32 +03001898 goto out_uars;
Yishai Hadas76dc5a82018-09-20 21:45:19 +03001899 context->devx_uid = err;
Yishai Hadasa8b92ca2018-06-17 12:59:57 +03001900 }
1901
Yishai Hadasd2d19122018-09-20 21:39:32 +03001902 err = mlx5_ib_alloc_transport_domain(dev, &context->tdn,
1903 context->devx_uid);
1904 if (err)
1905 goto out_devx;
1906
Yonatan Cohen25bb36e2018-06-19 08:47:24 +03001907 if (MLX5_CAP_GEN(dev->mdev, dump_fill_mkey)) {
1908 err = mlx5_cmd_dump_fill_mkey(dev->mdev, &dump_fill_mkey);
1909 if (err)
Jason Gunthorpe8193abb2018-07-04 13:19:46 -06001910 goto out_mdev;
Yonatan Cohen25bb36e2018-06-19 08:47:24 +03001911 }
1912
Eli Cohene126ba92013-07-07 17:25:49 +03001913 INIT_LIST_HEAD(&context->db_page_list);
1914 mutex_init(&context->db_page_mutex);
1915
Eli Cohen2f5ff262017-01-03 23:55:21 +02001916 resp.tot_bfregs = req.total_num_bfregs;
Daniel Jurgens508562d2018-01-04 17:25:34 +02001917 resp.num_ports = dev->num_ports;
Matan Barakb368d7c2015-12-15 20:30:12 +02001918
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001919 if (field_avail(typeof(resp), cqe_version, udata->outlen))
1920 resp.response_length += sizeof(resp.cqe_version);
Matan Barakb368d7c2015-12-15 20:30:12 +02001921
Bodong Wang402ca532016-06-17 15:02:20 +03001922 if (field_avail(typeof(resp), cmds_supp_uhw, udata->outlen)) {
Moni Shoua6ad279c52016-11-23 08:23:23 +02001923 resp.cmds_supp_uhw |= MLX5_USER_CMDS_SUPP_UHW_QUERY_DEVICE |
1924 MLX5_USER_CMDS_SUPP_UHW_CREATE_AH;
Bodong Wang402ca532016-06-17 15:02:20 +03001925 resp.response_length += sizeof(resp.cmds_supp_uhw);
1926 }
1927
Or Gerlitz78984892016-11-30 20:33:33 +02001928 if (field_avail(typeof(resp), eth_min_inline, udata->outlen)) {
1929 if (mlx5_ib_port_link_layer(ibdev, 1) == IB_LINK_LAYER_ETHERNET) {
1930 mlx5_query_min_inline(dev->mdev, &resp.eth_min_inline);
1931 resp.eth_min_inline++;
1932 }
1933 resp.response_length += sizeof(resp.eth_min_inline);
1934 }
1935
Feras Daoud5c99eae2018-01-16 20:08:41 +02001936 if (field_avail(typeof(resp), clock_info_versions, udata->outlen)) {
1937 if (mdev->clock_info)
1938 resp.clock_info_versions = BIT(MLX5_IB_CLOCK_INFO_V1);
1939 resp.response_length += sizeof(resp.clock_info_versions);
1940 }
1941
Noa Osherovichbc5c6ee2016-06-04 15:15:31 +03001942 /*
1943 * We don't want to expose information from the PCI bar that is located
1944 * after 4096 bytes, so if the arch only supports larger pages, let's
1945 * pretend we don't support reading the HCA's core clock. This is also
1946 * forced by mmap function.
1947 */
Eli Cohende8d6e02017-01-03 23:55:19 +02001948 if (field_avail(typeof(resp), hca_core_clock_offset, udata->outlen)) {
1949 if (PAGE_SIZE <= 4096) {
1950 resp.comp_mask |=
1951 MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_CORE_CLOCK_OFFSET;
1952 resp.hca_core_clock_offset =
1953 offsetof(struct mlx5_init_seg, internal_timer_h) % PAGE_SIZE;
1954 }
Feras Daoud5c99eae2018-01-16 20:08:41 +02001955 resp.response_length += sizeof(resp.hca_core_clock_offset);
Matan Barakb368d7c2015-12-15 20:30:12 +02001956 }
1957
Eli Cohen30aa60b2017-01-03 23:55:27 +02001958 if (field_avail(typeof(resp), log_uar_size, udata->outlen))
1959 resp.response_length += sizeof(resp.log_uar_size);
1960
1961 if (field_avail(typeof(resp), num_uars_per_page, udata->outlen))
1962 resp.response_length += sizeof(resp.num_uars_per_page);
1963
Yishai Hadas31a78a52017-12-24 16:31:34 +02001964 if (field_avail(typeof(resp), num_dyn_bfregs, udata->outlen)) {
1965 resp.num_dyn_bfregs = bfregi->num_dyn_bfregs;
1966 resp.response_length += sizeof(resp.num_dyn_bfregs);
1967 }
1968
Yonatan Cohen25bb36e2018-06-19 08:47:24 +03001969 if (field_avail(typeof(resp), dump_fill_mkey, udata->outlen)) {
1970 if (MLX5_CAP_GEN(dev->mdev, dump_fill_mkey)) {
1971 resp.dump_fill_mkey = dump_fill_mkey;
1972 resp.comp_mask |=
1973 MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_DUMP_FILL_MKEY;
1974 }
1975 resp.response_length += sizeof(resp.dump_fill_mkey);
1976 }
1977
Matan Barakb368d7c2015-12-15 20:30:12 +02001978 err = ib_copy_to_udata(udata, &resp, resp.response_length);
Eli Cohene126ba92013-07-07 17:25:49 +03001979 if (err)
Yishai Hadasa8b92ca2018-06-17 12:59:57 +03001980 goto out_mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03001981
Eli Cohen2f5ff262017-01-03 23:55:21 +02001982 bfregi->ver = ver;
1983 bfregi->num_low_latency_bfregs = req.num_low_latency_bfregs;
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001984 context->cqe_version = resp.cqe_version;
Eli Cohen30aa60b2017-01-03 23:55:27 +02001985 context->lib_caps = req.lib_caps;
1986 print_lib_caps(dev, context->lib_caps);
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001987
Aviv Heller7c34ec12018-08-23 13:47:53 +03001988 if (dev->lag_active) {
Mark Bloch95579e72019-03-28 15:27:33 +02001989 u8 port = mlx5_core_native_port_num(dev->mdev) - 1;
Majd Dibbinyc6a21c32018-08-28 14:29:05 +03001990
1991 atomic_set(&context->tx_port_affinity,
1992 atomic_add_return(
Mark Bloch95579e72019-03-28 15:27:33 +02001993 1, &dev->port[port].roce.tx_port_affinity));
Majd Dibbinyc6a21c32018-08-28 14:29:05 +03001994 }
1995
Leon Romanovskya2a074e2019-02-12 20:39:16 +02001996 return 0;
Eli Cohene126ba92013-07-07 17:25:49 +03001997
Yishai Hadasa8b92ca2018-06-17 12:59:57 +03001998out_mdev:
Yishai Hadasd2d19122018-09-20 21:39:32 +03001999 mlx5_ib_dealloc_transport_domain(dev, context->tdn, context->devx_uid);
2000out_devx:
Yishai Hadasa8b92ca2018-06-17 12:59:57 +03002001 if (req.flags & MLX5_IB_ALLOC_UCTX_DEVX)
Yishai Hadas76dc5a82018-09-20 21:45:19 +03002002 mlx5_ib_devx_destroy(dev, context->devx_uid);
majd@mellanox.com146d2f12016-01-14 19:13:02 +02002003
Eli Cohene126ba92013-07-07 17:25:49 +03002004out_uars:
Eli Cohenb037c292017-01-03 23:55:26 +02002005 deallocate_uars(dev, context);
2006
2007out_sys_pages:
2008 kfree(bfregi->sys_pages);
2009
Eli Cohene126ba92013-07-07 17:25:49 +03002010out_count:
Eli Cohen2f5ff262017-01-03 23:55:21 +02002011 kfree(bfregi->count);
Eli Cohene126ba92013-07-07 17:25:49 +03002012
Eli Cohene126ba92013-07-07 17:25:49 +03002013out_ctx:
Leon Romanovskya2a074e2019-02-12 20:39:16 +02002014 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03002015}
2016
Leon Romanovskya2a074e2019-02-12 20:39:16 +02002017static void mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
Eli Cohene126ba92013-07-07 17:25:49 +03002018{
2019 struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
2020 struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
Eli Cohenb037c292017-01-03 23:55:26 +02002021 struct mlx5_bfreg_info *bfregi;
Eli Cohene126ba92013-07-07 17:25:49 +03002022
Jason Gunthorpef27a0d52018-09-16 20:48:08 +03002023 /* All umem's must be destroyed before destroying the ucontext. */
2024 mutex_lock(&ibcontext->per_mm_list_lock);
2025 WARN_ON(!list_empty(&ibcontext->per_mm_list));
2026 mutex_unlock(&ibcontext->per_mm_list_lock);
Yishai Hadasa8b92ca2018-06-17 12:59:57 +03002027
Eli Cohenb037c292017-01-03 23:55:26 +02002028 bfregi = &context->bfregi;
Yishai Hadasd2d19122018-09-20 21:39:32 +03002029 mlx5_ib_dealloc_transport_domain(dev, context->tdn, context->devx_uid);
2030
Eli Cohenb037c292017-01-03 23:55:26 +02002031 if (context->devx_uid)
Yishai Hadas76dc5a82018-09-20 21:45:19 +03002032 mlx5_ib_devx_destroy(dev, context->devx_uid);
Eli Cohene126ba92013-07-07 17:25:49 +03002033
2034 deallocate_uars(dev, context);
Eli Cohen2f5ff262017-01-03 23:55:21 +02002035 kfree(bfregi->sys_pages);
2036 kfree(bfregi->count);
Eli Cohene126ba92013-07-07 17:25:49 +03002037}
2038
2039static phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev,
2040 int uar_idx)
2041{
Eli Cohenb037c292017-01-03 23:55:26 +02002042 int fw_uars_per_page;
2043
2044 fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ? MLX5_UARS_IN_PAGE : 1;
2045
Huy Nguyenaa8106f2019-03-29 15:38:01 -07002046 return (dev->mdev->bar_addr >> PAGE_SHIFT) + uar_idx / fw_uars_per_page;
Eli Cohene126ba92013-07-07 17:25:49 +03002047}
2048
2049static int get_command(unsigned long offset)
2050{
2051 return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK;
2052}
2053
2054static int get_arg(unsigned long offset)
2055{
2056 return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1);
2057}
2058
2059static int get_index(unsigned long offset)
2060{
2061 return get_arg(offset);
2062}
2063
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002064/* Index resides in an extra byte to enable larger values than 255 */
2065static int get_extended_index(unsigned long offset)
2066{
2067 return get_arg(offset) | ((offset >> 16) & 0xff) << 8;
2068}
2069
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03002070
2071static void mlx5_ib_disassociate_ucontext(struct ib_ucontext *ibcontext)
2072{
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03002073}
2074
Guy Levi37aa5c32016-04-27 16:49:50 +03002075static inline char *mmap_cmd2str(enum mlx5_ib_mmap_cmd cmd)
2076{
2077 switch (cmd) {
2078 case MLX5_IB_MMAP_WC_PAGE:
2079 return "WC";
2080 case MLX5_IB_MMAP_REGULAR_PAGE:
2081 return "best effort WC";
2082 case MLX5_IB_MMAP_NC_PAGE:
2083 return "NC";
Ariel Levkovich24da0012018-04-05 18:53:27 +03002084 case MLX5_IB_MMAP_DEVICE_MEM:
2085 return "Device Memory";
Guy Levi37aa5c32016-04-27 16:49:50 +03002086 default:
2087 return NULL;
2088 }
2089}
2090
Feras Daoud5c99eae2018-01-16 20:08:41 +02002091static int mlx5_ib_mmap_clock_info_page(struct mlx5_ib_dev *dev,
2092 struct vm_area_struct *vma,
2093 struct mlx5_ib_ucontext *context)
2094{
Feras Daoud5c99eae2018-01-16 20:08:41 +02002095 if (vma->vm_end - vma->vm_start != PAGE_SIZE)
2096 return -EINVAL;
2097
2098 if (get_index(vma->vm_pgoff) != MLX5_IB_CLOCK_INFO_V1)
2099 return -EOPNOTSUPP;
2100
2101 if (vma->vm_flags & VM_WRITE)
2102 return -EPERM;
2103
2104 if (!dev->mdev->clock_info_page)
2105 return -EOPNOTSUPP;
2106
Jason Gunthorpee2cd1d12018-09-16 20:43:10 +03002107 return rdma_user_mmap_page(&context->ibucontext, vma,
2108 dev->mdev->clock_info_page, PAGE_SIZE);
Feras Daoud5c99eae2018-01-16 20:08:41 +02002109}
2110
Guy Levi37aa5c32016-04-27 16:49:50 +03002111static int uar_mmap(struct mlx5_ib_dev *dev, enum mlx5_ib_mmap_cmd cmd,
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03002112 struct vm_area_struct *vma,
2113 struct mlx5_ib_ucontext *context)
Guy Levi37aa5c32016-04-27 16:49:50 +03002114{
Eli Cohen2f5ff262017-01-03 23:55:21 +02002115 struct mlx5_bfreg_info *bfregi = &context->bfregi;
Guy Levi37aa5c32016-04-27 16:49:50 +03002116 int err;
2117 unsigned long idx;
Kamal Heibaa09ea62018-07-19 00:05:32 +03002118 phys_addr_t pfn;
Guy Levi37aa5c32016-04-27 16:49:50 +03002119 pgprot_t prot;
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002120 u32 bfreg_dyn_idx = 0;
2121 u32 uar_index;
2122 int dyn_uar = (cmd == MLX5_IB_MMAP_ALLOC_WC);
2123 int max_valid_idx = dyn_uar ? bfregi->num_sys_pages :
2124 bfregi->num_static_sys_pages;
Eli Cohenb037c292017-01-03 23:55:26 +02002125
2126 if (vma->vm_end - vma->vm_start != PAGE_SIZE)
2127 return -EINVAL;
2128
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002129 if (dyn_uar)
2130 idx = get_extended_index(vma->vm_pgoff) + bfregi->num_static_sys_pages;
2131 else
2132 idx = get_index(vma->vm_pgoff);
2133
2134 if (idx >= max_valid_idx) {
2135 mlx5_ib_warn(dev, "invalid uar index %lu, max=%d\n",
2136 idx, max_valid_idx);
Eli Cohenb037c292017-01-03 23:55:26 +02002137 return -EINVAL;
2138 }
Guy Levi37aa5c32016-04-27 16:49:50 +03002139
2140 switch (cmd) {
2141 case MLX5_IB_MMAP_WC_PAGE:
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002142 case MLX5_IB_MMAP_ALLOC_WC:
Guy Levi37aa5c32016-04-27 16:49:50 +03002143/* Some architectures don't support WC memory */
2144#if defined(CONFIG_X86)
2145 if (!pat_enabled())
2146 return -EPERM;
2147#elif !(defined(CONFIG_PPC) || (defined(CONFIG_ARM) && defined(CONFIG_MMU)))
2148 return -EPERM;
2149#endif
2150 /* fall through */
2151 case MLX5_IB_MMAP_REGULAR_PAGE:
2152 /* For MLX5_IB_MMAP_REGULAR_PAGE do the best effort to get WC */
2153 prot = pgprot_writecombine(vma->vm_page_prot);
2154 break;
2155 case MLX5_IB_MMAP_NC_PAGE:
2156 prot = pgprot_noncached(vma->vm_page_prot);
2157 break;
2158 default:
2159 return -EINVAL;
2160 }
2161
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002162 if (dyn_uar) {
2163 int uars_per_page;
2164
2165 uars_per_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k);
2166 bfreg_dyn_idx = idx * (uars_per_page * MLX5_NON_FP_BFREGS_PER_UAR);
2167 if (bfreg_dyn_idx >= bfregi->total_num_bfregs) {
2168 mlx5_ib_warn(dev, "invalid bfreg_dyn_idx %u, max=%u\n",
2169 bfreg_dyn_idx, bfregi->total_num_bfregs);
2170 return -EINVAL;
2171 }
2172
2173 mutex_lock(&bfregi->lock);
2174 /* Fail if uar already allocated, first bfreg index of each
2175 * page holds its count.
2176 */
2177 if (bfregi->count[bfreg_dyn_idx]) {
2178 mlx5_ib_warn(dev, "wrong offset, idx %lu is busy, bfregn=%u\n", idx, bfreg_dyn_idx);
2179 mutex_unlock(&bfregi->lock);
2180 return -EINVAL;
2181 }
2182
2183 bfregi->count[bfreg_dyn_idx]++;
2184 mutex_unlock(&bfregi->lock);
2185
2186 err = mlx5_cmd_alloc_uar(dev->mdev, &uar_index);
2187 if (err) {
2188 mlx5_ib_warn(dev, "UAR alloc failed\n");
2189 goto free_bfreg;
2190 }
2191 } else {
2192 uar_index = bfregi->sys_pages[idx];
2193 }
2194
2195 pfn = uar_index2pfn(dev, uar_index);
Guy Levi37aa5c32016-04-27 16:49:50 +03002196 mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn %pa\n", idx, &pfn);
2197
Jason Gunthorpee2cd1d12018-09-16 20:43:10 +03002198 err = rdma_user_mmap_io(&context->ibucontext, vma, pfn, PAGE_SIZE,
2199 prot);
Guy Levi37aa5c32016-04-27 16:49:50 +03002200 if (err) {
Leon Romanovsky8f062282018-05-22 08:31:03 +03002201 mlx5_ib_err(dev,
Jason Gunthorpee2cd1d12018-09-16 20:43:10 +03002202 "rdma_user_mmap_io failed with error=%d, mmap_cmd=%s\n",
Leon Romanovsky8f062282018-05-22 08:31:03 +03002203 err, mmap_cmd2str(cmd));
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002204 goto err;
Guy Levi37aa5c32016-04-27 16:49:50 +03002205 }
2206
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002207 if (dyn_uar)
2208 bfregi->sys_pages[idx] = uar_index;
2209 return 0;
2210
2211err:
2212 if (!dyn_uar)
2213 return err;
2214
2215 mlx5_cmd_free_uar(dev->mdev, idx);
2216
2217free_bfreg:
2218 mlx5_ib_free_bfreg(dev, bfregi, bfreg_dyn_idx);
2219
2220 return err;
Guy Levi37aa5c32016-04-27 16:49:50 +03002221}
2222
Ariel Levkovich24da0012018-04-05 18:53:27 +03002223static int dm_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
2224{
2225 struct mlx5_ib_ucontext *mctx = to_mucontext(context);
2226 struct mlx5_ib_dev *dev = to_mdev(context->device);
2227 u16 page_idx = get_extended_index(vma->vm_pgoff);
2228 size_t map_size = vma->vm_end - vma->vm_start;
2229 u32 npages = map_size >> PAGE_SHIFT;
2230 phys_addr_t pfn;
Ariel Levkovich24da0012018-04-05 18:53:27 +03002231
2232 if (find_next_zero_bit(mctx->dm_pages, page_idx + npages, page_idx) !=
2233 page_idx + npages)
2234 return -EINVAL;
2235
Huy Nguyenaa8106f2019-03-29 15:38:01 -07002236 pfn = ((dev->mdev->bar_addr +
Ariel Levkovich24da0012018-04-05 18:53:27 +03002237 MLX5_CAP64_DEV_MEM(dev->mdev, memic_bar_start_addr)) >>
2238 PAGE_SHIFT) +
2239 page_idx;
Jason Gunthorpee2cd1d12018-09-16 20:43:10 +03002240 return rdma_user_mmap_io(context, vma, pfn, map_size,
2241 pgprot_writecombine(vma->vm_page_prot));
Ariel Levkovich24da0012018-04-05 18:53:27 +03002242}
2243
Eli Cohene126ba92013-07-07 17:25:49 +03002244static int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma)
2245{
2246 struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
2247 struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
Eli Cohene126ba92013-07-07 17:25:49 +03002248 unsigned long command;
Eli Cohene126ba92013-07-07 17:25:49 +03002249 phys_addr_t pfn;
2250
2251 command = get_command(vma->vm_pgoff);
2252 switch (command) {
Guy Levi37aa5c32016-04-27 16:49:50 +03002253 case MLX5_IB_MMAP_WC_PAGE:
2254 case MLX5_IB_MMAP_NC_PAGE:
Eli Cohene126ba92013-07-07 17:25:49 +03002255 case MLX5_IB_MMAP_REGULAR_PAGE:
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002256 case MLX5_IB_MMAP_ALLOC_WC:
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03002257 return uar_mmap(dev, command, vma, context);
Eli Cohene126ba92013-07-07 17:25:49 +03002258
2259 case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES:
2260 return -ENOSYS;
2261
Matan Barakd69e3bc2015-12-15 20:30:13 +02002262 case MLX5_IB_MMAP_CORE_CLOCK:
Matan Barakd69e3bc2015-12-15 20:30:13 +02002263 if (vma->vm_end - vma->vm_start != PAGE_SIZE)
2264 return -EINVAL;
2265
Matan Barak6cbac1e2016-04-14 16:52:10 +03002266 if (vma->vm_flags & VM_WRITE)
Matan Barakd69e3bc2015-12-15 20:30:13 +02002267 return -EPERM;
2268
2269 /* Don't expose to user-space information it shouldn't have */
2270 if (PAGE_SIZE > 4096)
2271 return -EOPNOTSUPP;
2272
2273 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
2274 pfn = (dev->mdev->iseg_base +
2275 offsetof(struct mlx5_init_seg, internal_timer_h)) >>
2276 PAGE_SHIFT;
2277 if (io_remap_pfn_range(vma, vma->vm_start, pfn,
2278 PAGE_SIZE, vma->vm_page_prot))
2279 return -EAGAIN;
Matan Barakd69e3bc2015-12-15 20:30:13 +02002280 break;
Feras Daoud5c99eae2018-01-16 20:08:41 +02002281 case MLX5_IB_MMAP_CLOCK_INFO:
2282 return mlx5_ib_mmap_clock_info_page(dev, vma, context);
Matan Barakd69e3bc2015-12-15 20:30:13 +02002283
Ariel Levkovich24da0012018-04-05 18:53:27 +03002284 case MLX5_IB_MMAP_DEVICE_MEM:
2285 return dm_mmap(ibcontext, vma);
2286
Eli Cohene126ba92013-07-07 17:25:49 +03002287 default:
2288 return -EINVAL;
2289 }
2290
2291 return 0;
2292}
2293
Ariel Levkovich24da0012018-04-05 18:53:27 +03002294struct ib_dm *mlx5_ib_alloc_dm(struct ib_device *ibdev,
2295 struct ib_ucontext *context,
2296 struct ib_dm_alloc_attr *attr,
2297 struct uverbs_attr_bundle *attrs)
2298{
2299 u64 act_size = roundup(attr->length, MLX5_MEMIC_BASE_SIZE);
2300 struct mlx5_memic *memic = &to_mdev(ibdev)->memic;
2301 phys_addr_t memic_addr;
2302 struct mlx5_ib_dm *dm;
2303 u64 start_offset;
2304 u32 page_idx;
2305 int err;
2306
2307 dm = kzalloc(sizeof(*dm), GFP_KERNEL);
2308 if (!dm)
2309 return ERR_PTR(-ENOMEM);
2310
2311 mlx5_ib_dbg(to_mdev(ibdev), "alloc_memic req: user_length=0x%llx act_length=0x%llx log_alignment=%d\n",
2312 attr->length, act_size, attr->alignment);
2313
2314 err = mlx5_cmd_alloc_memic(memic, &memic_addr,
2315 act_size, attr->alignment);
2316 if (err)
2317 goto err_free;
2318
2319 start_offset = memic_addr & ~PAGE_MASK;
Huy Nguyenaa8106f2019-03-29 15:38:01 -07002320 page_idx = (memic_addr - memic->dev->bar_addr -
Ariel Levkovich24da0012018-04-05 18:53:27 +03002321 MLX5_CAP64_DEV_MEM(memic->dev, memic_bar_start_addr)) >>
2322 PAGE_SHIFT;
2323
2324 err = uverbs_copy_to(attrs,
2325 MLX5_IB_ATTR_ALLOC_DM_RESP_START_OFFSET,
2326 &start_offset, sizeof(start_offset));
2327 if (err)
2328 goto err_dealloc;
2329
2330 err = uverbs_copy_to(attrs,
2331 MLX5_IB_ATTR_ALLOC_DM_RESP_PAGE_INDEX,
2332 &page_idx, sizeof(page_idx));
2333 if (err)
2334 goto err_dealloc;
2335
2336 bitmap_set(to_mucontext(context)->dm_pages, page_idx,
2337 DIV_ROUND_UP(act_size, PAGE_SIZE));
2338
2339 dm->dev_addr = memic_addr;
2340
2341 return &dm->ibdm;
2342
2343err_dealloc:
2344 mlx5_cmd_dealloc_memic(memic, memic_addr,
2345 act_size);
2346err_free:
2347 kfree(dm);
2348 return ERR_PTR(err);
2349}
2350
Shamir Rabinovitchc4367a22019-03-31 19:10:05 +03002351int mlx5_ib_dealloc_dm(struct ib_dm *ibdm, struct uverbs_attr_bundle *attrs)
Ariel Levkovich24da0012018-04-05 18:53:27 +03002352{
2353 struct mlx5_memic *memic = &to_mdev(ibdm->device)->memic;
2354 struct mlx5_ib_dm *dm = to_mdm(ibdm);
2355 u64 act_size = roundup(dm->ibdm.length, MLX5_MEMIC_BASE_SIZE);
2356 u32 page_idx;
2357 int ret;
2358
2359 ret = mlx5_cmd_dealloc_memic(memic, dm->dev_addr, act_size);
2360 if (ret)
2361 return ret;
2362
Huy Nguyenaa8106f2019-03-29 15:38:01 -07002363 page_idx = (dm->dev_addr - memic->dev->bar_addr -
Ariel Levkovich24da0012018-04-05 18:53:27 +03002364 MLX5_CAP64_DEV_MEM(memic->dev, memic_bar_start_addr)) >>
2365 PAGE_SHIFT;
Shamir Rabinovitchbdeacab2019-03-31 19:10:06 +03002366 bitmap_clear(rdma_udata_to_drv_context(
2367 &attrs->driver_udata,
2368 struct mlx5_ib_ucontext,
2369 ibucontext)->dm_pages,
Ariel Levkovich24da0012018-04-05 18:53:27 +03002370 page_idx,
2371 DIV_ROUND_UP(act_size, PAGE_SIZE));
2372
2373 kfree(dm);
2374
2375 return 0;
2376}
2377
Shamir Rabinovitchff23dfa2019-03-31 19:10:07 +03002378static int mlx5_ib_alloc_pd(struct ib_pd *ibpd, struct ib_udata *udata)
Eli Cohene126ba92013-07-07 17:25:49 +03002379{
Leon Romanovsky21a428a2019-02-03 14:55:51 +02002380 struct mlx5_ib_pd *pd = to_mpd(ibpd);
2381 struct ib_device *ibdev = ibpd->device;
Eli Cohene126ba92013-07-07 17:25:49 +03002382 struct mlx5_ib_alloc_pd_resp resp;
Eli Cohene126ba92013-07-07 17:25:49 +03002383 int err;
Yishai Hadasa1069c12018-09-20 21:39:19 +03002384 u32 out[MLX5_ST_SZ_DW(alloc_pd_out)] = {};
2385 u32 in[MLX5_ST_SZ_DW(alloc_pd_in)] = {};
2386 u16 uid = 0;
Shamir Rabinovitchff23dfa2019-03-31 19:10:07 +03002387 struct mlx5_ib_ucontext *context = rdma_udata_to_drv_context(
2388 udata, struct mlx5_ib_ucontext, ibucontext);
Eli Cohene126ba92013-07-07 17:25:49 +03002389
Shamir Rabinovitchff23dfa2019-03-31 19:10:07 +03002390 uid = context ? context->devx_uid : 0;
Yishai Hadasa1069c12018-09-20 21:39:19 +03002391 MLX5_SET(alloc_pd_in, in, opcode, MLX5_CMD_OP_ALLOC_PD);
2392 MLX5_SET(alloc_pd_in, in, uid, uid);
2393 err = mlx5_cmd_exec(to_mdev(ibdev)->mdev, in, sizeof(in),
2394 out, sizeof(out));
Leon Romanovsky21a428a2019-02-03 14:55:51 +02002395 if (err)
2396 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03002397
Yishai Hadasa1069c12018-09-20 21:39:19 +03002398 pd->pdn = MLX5_GET(alloc_pd_out, out, pd);
2399 pd->uid = uid;
Shamir Rabinovitchff23dfa2019-03-31 19:10:07 +03002400 if (udata) {
Eli Cohene126ba92013-07-07 17:25:49 +03002401 resp.pdn = pd->pdn;
2402 if (ib_copy_to_udata(udata, &resp, sizeof(resp))) {
Yishai Hadasa1069c12018-09-20 21:39:19 +03002403 mlx5_cmd_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn, uid);
Leon Romanovsky21a428a2019-02-03 14:55:51 +02002404 return -EFAULT;
Eli Cohene126ba92013-07-07 17:25:49 +03002405 }
Eli Cohene126ba92013-07-07 17:25:49 +03002406 }
2407
Leon Romanovsky21a428a2019-02-03 14:55:51 +02002408 return 0;
Eli Cohene126ba92013-07-07 17:25:49 +03002409}
2410
Shamir Rabinovitchc4367a22019-03-31 19:10:05 +03002411static void mlx5_ib_dealloc_pd(struct ib_pd *pd, struct ib_udata *udata)
Eli Cohene126ba92013-07-07 17:25:49 +03002412{
2413 struct mlx5_ib_dev *mdev = to_mdev(pd->device);
2414 struct mlx5_ib_pd *mpd = to_mpd(pd);
2415
Yishai Hadasa1069c12018-09-20 21:39:19 +03002416 mlx5_cmd_dealloc_pd(mdev->mdev, mpd->pdn, mpd->uid);
Eli Cohene126ba92013-07-07 17:25:49 +03002417}
2418
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002419enum {
2420 MATCH_CRITERIA_ENABLE_OUTER_BIT,
2421 MATCH_CRITERIA_ENABLE_MISC_BIT,
Ariel Levkovich71c6e862018-05-13 14:33:34 +03002422 MATCH_CRITERIA_ENABLE_INNER_BIT,
2423 MATCH_CRITERIA_ENABLE_MISC2_BIT
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002424};
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002425
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002426#define HEADER_IS_ZERO(match_criteria, headers) \
2427 !(memchr_inv(MLX5_ADDR_OF(fte_match_param, match_criteria, headers), \
2428 0, MLX5_FLD_SZ_BYTES(fte_match_param, headers))) \
2429
2430static u8 get_match_criteria_enable(u32 *match_criteria)
2431{
2432 u8 match_criteria_enable;
2433
2434 match_criteria_enable =
2435 (!HEADER_IS_ZERO(match_criteria, outer_headers)) <<
2436 MATCH_CRITERIA_ENABLE_OUTER_BIT;
2437 match_criteria_enable |=
2438 (!HEADER_IS_ZERO(match_criteria, misc_parameters)) <<
2439 MATCH_CRITERIA_ENABLE_MISC_BIT;
2440 match_criteria_enable |=
2441 (!HEADER_IS_ZERO(match_criteria, inner_headers)) <<
2442 MATCH_CRITERIA_ENABLE_INNER_BIT;
Ariel Levkovich71c6e862018-05-13 14:33:34 +03002443 match_criteria_enable |=
2444 (!HEADER_IS_ZERO(match_criteria, misc_parameters_2)) <<
2445 MATCH_CRITERIA_ENABLE_MISC2_BIT;
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002446
2447 return match_criteria_enable;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002448}
2449
Maor Gottlieb6113cc42019-01-17 20:08:15 +02002450static int set_proto(void *outer_c, void *outer_v, u8 mask, u8 val)
Maor Gottliebca0d4752016-08-30 16:58:35 +03002451{
Maor Gottlieb6113cc42019-01-17 20:08:15 +02002452 u8 entry_mask;
2453 u8 entry_val;
2454 int err = 0;
2455
2456 if (!mask)
2457 goto out;
2458
2459 entry_mask = MLX5_GET(fte_match_set_lyr_2_4, outer_c,
2460 ip_protocol);
2461 entry_val = MLX5_GET(fte_match_set_lyr_2_4, outer_v,
2462 ip_protocol);
2463 if (!entry_mask) {
2464 MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_protocol, mask);
2465 MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_protocol, val);
2466 goto out;
2467 }
2468 /* Don't override existing ip protocol */
2469 if (mask != entry_mask || val != entry_val)
2470 err = -EINVAL;
2471out:
2472 return err;
Maor Gottliebca0d4752016-08-30 16:58:35 +03002473}
2474
Daria Velikovsky37da2a02018-05-07 10:20:02 +03002475static void set_flow_label(void *misc_c, void *misc_v, u32 mask, u32 val,
Moses Reuben2d1e6972016-11-14 19:04:52 +02002476 bool inner)
2477{
2478 if (inner) {
2479 MLX5_SET(fte_match_set_misc,
2480 misc_c, inner_ipv6_flow_label, mask);
2481 MLX5_SET(fte_match_set_misc,
2482 misc_v, inner_ipv6_flow_label, val);
2483 } else {
2484 MLX5_SET(fte_match_set_misc,
2485 misc_c, outer_ipv6_flow_label, mask);
2486 MLX5_SET(fte_match_set_misc,
2487 misc_v, outer_ipv6_flow_label, val);
2488 }
2489}
2490
Maor Gottliebca0d4752016-08-30 16:58:35 +03002491static void set_tos(void *outer_c, void *outer_v, u8 mask, u8 val)
2492{
2493 MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_ecn, mask);
2494 MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_ecn, val);
2495 MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_dscp, mask >> 2);
2496 MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_dscp, val >> 2);
2497}
2498
Ariel Levkovich71c6e862018-05-13 14:33:34 +03002499static int check_mpls_supp_fields(u32 field_support, const __be32 *set_mask)
2500{
2501 if (MLX5_GET(fte_match_mpls, set_mask, mpls_label) &&
2502 !(field_support & MLX5_FIELD_SUPPORT_MPLS_LABEL))
2503 return -EOPNOTSUPP;
2504
2505 if (MLX5_GET(fte_match_mpls, set_mask, mpls_exp) &&
2506 !(field_support & MLX5_FIELD_SUPPORT_MPLS_EXP))
2507 return -EOPNOTSUPP;
2508
2509 if (MLX5_GET(fte_match_mpls, set_mask, mpls_s_bos) &&
2510 !(field_support & MLX5_FIELD_SUPPORT_MPLS_S_BOS))
2511 return -EOPNOTSUPP;
2512
2513 if (MLX5_GET(fte_match_mpls, set_mask, mpls_ttl) &&
2514 !(field_support & MLX5_FIELD_SUPPORT_MPLS_TTL))
2515 return -EOPNOTSUPP;
2516
2517 return 0;
2518}
2519
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002520#define LAST_ETH_FIELD vlan_tag
2521#define LAST_IB_FIELD sl
Maor Gottliebca0d4752016-08-30 16:58:35 +03002522#define LAST_IPV4_FIELD tos
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002523#define LAST_IPV6_FIELD traffic_class
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002524#define LAST_TCP_UDP_FIELD src_port
Moses Reubenffb30d82016-11-14 19:04:50 +02002525#define LAST_TUNNEL_FIELD tunnel_id
Moses Reuben2ac693f2017-01-18 14:59:50 +02002526#define LAST_FLOW_TAG_FIELD tag_id
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002527#define LAST_DROP_FIELD size
Raed Salem3b3233f2018-05-31 16:43:39 +03002528#define LAST_COUNTERS_FIELD counters
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002529
2530/* Field is the last supported field */
2531#define FIELDS_NOT_SUPPORTED(filter, field)\
2532 memchr_inv((void *)&filter.field +\
2533 sizeof(filter.field), 0,\
2534 sizeof(filter) -\
2535 offsetof(typeof(filter), field) -\
2536 sizeof(filter.field))
2537
Mark Bloch2ea26202018-09-06 17:27:03 +03002538int parse_flow_flow_action(struct mlx5_ib_flow_action *maction,
2539 bool is_egress,
2540 struct mlx5_flow_act *action)
Aviad Yehezkel802c2122018-03-28 09:27:53 +03002541{
Aviad Yehezkel802c2122018-03-28 09:27:53 +03002542
2543 switch (maction->ib_action.type) {
2544 case IB_FLOW_ACTION_ESP:
Mark Bloch501f14e2018-09-06 17:27:04 +03002545 if (action->action & (MLX5_FLOW_CONTEXT_ACTION_ENCRYPT |
2546 MLX5_FLOW_CONTEXT_ACTION_DECRYPT))
2547 return -EINVAL;
Aviad Yehezkel802c2122018-03-28 09:27:53 +03002548 /* Currently only AES_GCM keymat is supported by the driver */
2549 action->esp_id = (uintptr_t)maction->esp_aes_gcm.ctx;
Mark Bloch2ea26202018-09-06 17:27:03 +03002550 action->action |= is_egress ?
Aviad Yehezkel802c2122018-03-28 09:27:53 +03002551 MLX5_FLOW_CONTEXT_ACTION_ENCRYPT :
2552 MLX5_FLOW_CONTEXT_ACTION_DECRYPT;
2553 return 0;
Mark Blochb1085be2018-09-02 12:51:32 +03002554 case IB_FLOW_ACTION_UNSPECIFIED:
2555 if (maction->flow_action_raw.sub_type ==
2556 MLX5_IB_FLOW_ACTION_MODIFY_HEADER) {
Mark Bloch501f14e2018-09-06 17:27:04 +03002557 if (action->action & MLX5_FLOW_CONTEXT_ACTION_MOD_HDR)
2558 return -EINVAL;
Mark Blochb1085be2018-09-02 12:51:32 +03002559 action->action |= MLX5_FLOW_CONTEXT_ACTION_MOD_HDR;
2560 action->modify_id = maction->flow_action_raw.action_id;
2561 return 0;
2562 }
Mark Bloch10a30892018-09-02 12:51:34 +03002563 if (maction->flow_action_raw.sub_type ==
2564 MLX5_IB_FLOW_ACTION_DECAP) {
Mark Bloch501f14e2018-09-06 17:27:04 +03002565 if (action->action & MLX5_FLOW_CONTEXT_ACTION_DECAP)
2566 return -EINVAL;
Mark Bloch10a30892018-09-02 12:51:34 +03002567 action->action |= MLX5_FLOW_CONTEXT_ACTION_DECAP;
2568 return 0;
2569 }
Mark Bloche806f932018-09-02 12:51:36 +03002570 if (maction->flow_action_raw.sub_type ==
2571 MLX5_IB_FLOW_ACTION_PACKET_REFORMAT) {
Mark Bloch501f14e2018-09-06 17:27:04 +03002572 if (action->action &
2573 MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT)
2574 return -EINVAL;
Mark Bloche806f932018-09-02 12:51:36 +03002575 action->action |=
2576 MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT;
2577 action->reformat_id =
2578 maction->flow_action_raw.action_id;
2579 return 0;
2580 }
Mark Blochb1085be2018-09-02 12:51:32 +03002581 /* fall through */
Aviad Yehezkel802c2122018-03-28 09:27:53 +03002582 default:
2583 return -EOPNOTSUPP;
2584 }
2585}
2586
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002587static int parse_flow_attr(struct mlx5_core_dev *mdev, u32 *match_c,
2588 u32 *match_v, const union ib_flow_spec *ib_spec,
Aviad Yehezkel802c2122018-03-28 09:27:53 +03002589 const struct ib_flow_attr *flow_attr,
Ariel Levkovich71c6e862018-05-13 14:33:34 +03002590 struct mlx5_flow_act *action, u32 prev_type)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002591{
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002592 void *misc_params_c = MLX5_ADDR_OF(fte_match_param, match_c,
2593 misc_parameters);
2594 void *misc_params_v = MLX5_ADDR_OF(fte_match_param, match_v,
2595 misc_parameters);
Ariel Levkovich71c6e862018-05-13 14:33:34 +03002596 void *misc_params2_c = MLX5_ADDR_OF(fte_match_param, match_c,
2597 misc_parameters_2);
2598 void *misc_params2_v = MLX5_ADDR_OF(fte_match_param, match_v,
2599 misc_parameters_2);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002600 void *headers_c;
2601 void *headers_v;
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002602 int match_ipv;
Aviad Yehezkel802c2122018-03-28 09:27:53 +03002603 int ret;
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002604
Moses Reuben2d1e6972016-11-14 19:04:52 +02002605 if (ib_spec->type & IB_FLOW_SPEC_INNER) {
2606 headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
2607 inner_headers);
2608 headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
2609 inner_headers);
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002610 match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2611 ft_field_support.inner_ip_version);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002612 } else {
2613 headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
2614 outer_headers);
2615 headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
2616 outer_headers);
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002617 match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2618 ft_field_support.outer_ip_version);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002619 }
2620
2621 switch (ib_spec->type & ~IB_FLOW_SPEC_INNER) {
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002622 case IB_FLOW_SPEC_ETH:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002623 if (FIELDS_NOT_SUPPORTED(ib_spec->eth.mask, LAST_ETH_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002624 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002625
Moses Reuben2d1e6972016-11-14 19:04:52 +02002626 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002627 dmac_47_16),
2628 ib_spec->eth.mask.dst_mac);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002629 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002630 dmac_47_16),
2631 ib_spec->eth.val.dst_mac);
2632
Moses Reuben2d1e6972016-11-14 19:04:52 +02002633 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottliebee3da802016-09-12 19:16:24 +03002634 smac_47_16),
2635 ib_spec->eth.mask.src_mac);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002636 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottliebee3da802016-09-12 19:16:24 +03002637 smac_47_16),
2638 ib_spec->eth.val.src_mac);
2639
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002640 if (ib_spec->eth.mask.vlan_tag) {
Moses Reuben2d1e6972016-11-14 19:04:52 +02002641 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Mohamad Haj Yahia10543362016-10-09 16:25:43 +03002642 cvlan_tag, 1);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002643 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Mohamad Haj Yahia10543362016-10-09 16:25:43 +03002644 cvlan_tag, 1);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002645
Moses Reuben2d1e6972016-11-14 19:04:52 +02002646 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002647 first_vid, ntohs(ib_spec->eth.mask.vlan_tag));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002648 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002649 first_vid, ntohs(ib_spec->eth.val.vlan_tag));
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_cfi,
2653 ntohs(ib_spec->eth.mask.vlan_tag) >> 12);
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_cfi,
2656 ntohs(ib_spec->eth.val.vlan_tag) >> 12);
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 first_prio,
2660 ntohs(ib_spec->eth.mask.vlan_tag) >> 13);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002661 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002662 first_prio,
2663 ntohs(ib_spec->eth.val.vlan_tag) >> 13);
2664 }
Moses Reuben2d1e6972016-11-14 19:04:52 +02002665 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002666 ethertype, ntohs(ib_spec->eth.mask.ether_type));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002667 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002668 ethertype, ntohs(ib_spec->eth.val.ether_type));
2669 break;
2670 case IB_FLOW_SPEC_IPV4:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002671 if (FIELDS_NOT_SUPPORTED(ib_spec->ipv4.mask, LAST_IPV4_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002672 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002673
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002674 if (match_ipv) {
2675 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2676 ip_version, 0xf);
2677 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Boris Pismenny3346c482017-08-20 15:13:08 +03002678 ip_version, MLX5_FS_IPV4_VERSION);
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002679 } else {
2680 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2681 ethertype, 0xffff);
2682 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2683 ethertype, ETH_P_IP);
2684 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002685
Moses Reuben2d1e6972016-11-14 19:04:52 +02002686 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002687 src_ipv4_src_ipv6.ipv4_layout.ipv4),
2688 &ib_spec->ipv4.mask.src_ip,
2689 sizeof(ib_spec->ipv4.mask.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002690 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002691 src_ipv4_src_ipv6.ipv4_layout.ipv4),
2692 &ib_spec->ipv4.val.src_ip,
2693 sizeof(ib_spec->ipv4.val.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002694 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002695 dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2696 &ib_spec->ipv4.mask.dst_ip,
2697 sizeof(ib_spec->ipv4.mask.dst_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002698 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002699 dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2700 &ib_spec->ipv4.val.dst_ip,
2701 sizeof(ib_spec->ipv4.val.dst_ip));
Maor Gottliebca0d4752016-08-30 16:58:35 +03002702
Moses Reuben2d1e6972016-11-14 19:04:52 +02002703 set_tos(headers_c, headers_v,
Maor Gottliebca0d4752016-08-30 16:58:35 +03002704 ib_spec->ipv4.mask.tos, ib_spec->ipv4.val.tos);
2705
Maor Gottlieb6113cc42019-01-17 20:08:15 +02002706 if (set_proto(headers_c, headers_v,
2707 ib_spec->ipv4.mask.proto,
2708 ib_spec->ipv4.val.proto))
2709 return -EINVAL;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002710 break;
Maor Gottlieb026bae02016-06-17 15:14:51 +03002711 case IB_FLOW_SPEC_IPV6:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002712 if (FIELDS_NOT_SUPPORTED(ib_spec->ipv6.mask, LAST_IPV6_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002713 return -EOPNOTSUPP;
Maor Gottlieb026bae02016-06-17 15:14:51 +03002714
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002715 if (match_ipv) {
2716 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2717 ip_version, 0xf);
2718 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Boris Pismenny3346c482017-08-20 15:13:08 +03002719 ip_version, MLX5_FS_IPV6_VERSION);
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002720 } else {
2721 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2722 ethertype, 0xffff);
2723 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2724 ethertype, ETH_P_IPV6);
2725 }
Maor Gottlieb026bae02016-06-17 15:14:51 +03002726
Moses Reuben2d1e6972016-11-14 19:04:52 +02002727 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb026bae02016-06-17 15:14:51 +03002728 src_ipv4_src_ipv6.ipv6_layout.ipv6),
2729 &ib_spec->ipv6.mask.src_ip,
2730 sizeof(ib_spec->ipv6.mask.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002731 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb026bae02016-06-17 15:14:51 +03002732 src_ipv4_src_ipv6.ipv6_layout.ipv6),
2733 &ib_spec->ipv6.val.src_ip,
2734 sizeof(ib_spec->ipv6.val.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002735 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb026bae02016-06-17 15:14:51 +03002736 dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2737 &ib_spec->ipv6.mask.dst_ip,
2738 sizeof(ib_spec->ipv6.mask.dst_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002739 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb026bae02016-06-17 15:14:51 +03002740 dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2741 &ib_spec->ipv6.val.dst_ip,
2742 sizeof(ib_spec->ipv6.val.dst_ip));
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002743
Moses Reuben2d1e6972016-11-14 19:04:52 +02002744 set_tos(headers_c, headers_v,
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002745 ib_spec->ipv6.mask.traffic_class,
2746 ib_spec->ipv6.val.traffic_class);
2747
Maor Gottlieb6113cc42019-01-17 20:08:15 +02002748 if (set_proto(headers_c, headers_v,
2749 ib_spec->ipv6.mask.next_hdr,
2750 ib_spec->ipv6.val.next_hdr))
2751 return -EINVAL;
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002752
Moses Reuben2d1e6972016-11-14 19:04:52 +02002753 set_flow_label(misc_params_c, misc_params_v,
2754 ntohl(ib_spec->ipv6.mask.flow_label),
2755 ntohl(ib_spec->ipv6.val.flow_label),
2756 ib_spec->type & IB_FLOW_SPEC_INNER);
Aviad Yehezkel802c2122018-03-28 09:27:53 +03002757 break;
2758 case IB_FLOW_SPEC_ESP:
2759 if (ib_spec->esp.mask.seq)
2760 return -EOPNOTSUPP;
Moses Reuben2d1e6972016-11-14 19:04:52 +02002761
Aviad Yehezkel802c2122018-03-28 09:27:53 +03002762 MLX5_SET(fte_match_set_misc, misc_params_c, outer_esp_spi,
2763 ntohl(ib_spec->esp.mask.spi));
2764 MLX5_SET(fte_match_set_misc, misc_params_v, outer_esp_spi,
2765 ntohl(ib_spec->esp.val.spi));
Maor Gottlieb026bae02016-06-17 15:14:51 +03002766 break;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002767 case IB_FLOW_SPEC_TCP:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002768 if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
2769 LAST_TCP_UDP_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002770 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002771
Maor Gottlieb6113cc42019-01-17 20:08:15 +02002772 if (set_proto(headers_c, headers_v, 0xff, IPPROTO_TCP))
2773 return -EINVAL;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002774
Moses Reuben2d1e6972016-11-14 19:04:52 +02002775 MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002776 ntohs(ib_spec->tcp_udp.mask.src_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002777 MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002778 ntohs(ib_spec->tcp_udp.val.src_port));
2779
Moses Reuben2d1e6972016-11-14 19:04:52 +02002780 MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002781 ntohs(ib_spec->tcp_udp.mask.dst_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002782 MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002783 ntohs(ib_spec->tcp_udp.val.dst_port));
2784 break;
2785 case IB_FLOW_SPEC_UDP:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002786 if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
2787 LAST_TCP_UDP_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002788 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002789
Maor Gottlieb6113cc42019-01-17 20:08:15 +02002790 if (set_proto(headers_c, headers_v, 0xff, IPPROTO_UDP))
2791 return -EINVAL;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002792
Moses Reuben2d1e6972016-11-14 19:04:52 +02002793 MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002794 ntohs(ib_spec->tcp_udp.mask.src_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002795 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002796 ntohs(ib_spec->tcp_udp.val.src_port));
2797
Moses Reuben2d1e6972016-11-14 19:04:52 +02002798 MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002799 ntohs(ib_spec->tcp_udp.mask.dst_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002800 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002801 ntohs(ib_spec->tcp_udp.val.dst_port));
2802 break;
Ariel Levkovichda2f22a2018-05-13 14:33:33 +03002803 case IB_FLOW_SPEC_GRE:
2804 if (ib_spec->gre.mask.c_ks_res0_ver)
2805 return -EOPNOTSUPP;
2806
Maor Gottlieb6113cc42019-01-17 20:08:15 +02002807 if (set_proto(headers_c, headers_v, 0xff, IPPROTO_GRE))
2808 return -EINVAL;
2809
Ariel Levkovichda2f22a2018-05-13 14:33:33 +03002810 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
2811 0xff);
2812 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
2813 IPPROTO_GRE);
2814
2815 MLX5_SET(fte_match_set_misc, misc_params_c, gre_protocol,
Maor Gottlieba93b6322018-07-01 15:50:17 +03002816 ntohs(ib_spec->gre.mask.protocol));
Ariel Levkovichda2f22a2018-05-13 14:33:33 +03002817 MLX5_SET(fte_match_set_misc, misc_params_v, gre_protocol,
2818 ntohs(ib_spec->gre.val.protocol));
2819
2820 memcpy(MLX5_ADDR_OF(fte_match_set_misc, misc_params_c,
Oz Shlomo5886a962018-12-10 13:15:13 -08002821 gre_key.nvgre.hi),
Ariel Levkovichda2f22a2018-05-13 14:33:33 +03002822 &ib_spec->gre.mask.key,
2823 sizeof(ib_spec->gre.mask.key));
2824 memcpy(MLX5_ADDR_OF(fte_match_set_misc, misc_params_v,
Oz Shlomo5886a962018-12-10 13:15:13 -08002825 gre_key.nvgre.hi),
Ariel Levkovichda2f22a2018-05-13 14:33:33 +03002826 &ib_spec->gre.val.key,
2827 sizeof(ib_spec->gre.val.key));
2828 break;
Ariel Levkovich71c6e862018-05-13 14:33:34 +03002829 case IB_FLOW_SPEC_MPLS:
2830 switch (prev_type) {
2831 case IB_FLOW_SPEC_UDP:
2832 if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2833 ft_field_support.outer_first_mpls_over_udp),
2834 &ib_spec->mpls.mask.tag))
2835 return -EOPNOTSUPP;
2836
2837 memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
2838 outer_first_mpls_over_udp),
2839 &ib_spec->mpls.val.tag,
2840 sizeof(ib_spec->mpls.val.tag));
2841 memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
2842 outer_first_mpls_over_udp),
2843 &ib_spec->mpls.mask.tag,
2844 sizeof(ib_spec->mpls.mask.tag));
2845 break;
2846 case IB_FLOW_SPEC_GRE:
2847 if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2848 ft_field_support.outer_first_mpls_over_gre),
2849 &ib_spec->mpls.mask.tag))
2850 return -EOPNOTSUPP;
2851
2852 memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
2853 outer_first_mpls_over_gre),
2854 &ib_spec->mpls.val.tag,
2855 sizeof(ib_spec->mpls.val.tag));
2856 memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
2857 outer_first_mpls_over_gre),
2858 &ib_spec->mpls.mask.tag,
2859 sizeof(ib_spec->mpls.mask.tag));
2860 break;
2861 default:
2862 if (ib_spec->type & IB_FLOW_SPEC_INNER) {
2863 if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2864 ft_field_support.inner_first_mpls),
2865 &ib_spec->mpls.mask.tag))
2866 return -EOPNOTSUPP;
2867
2868 memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
2869 inner_first_mpls),
2870 &ib_spec->mpls.val.tag,
2871 sizeof(ib_spec->mpls.val.tag));
2872 memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
2873 inner_first_mpls),
2874 &ib_spec->mpls.mask.tag,
2875 sizeof(ib_spec->mpls.mask.tag));
2876 } else {
2877 if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2878 ft_field_support.outer_first_mpls),
2879 &ib_spec->mpls.mask.tag))
2880 return -EOPNOTSUPP;
2881
2882 memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
2883 outer_first_mpls),
2884 &ib_spec->mpls.val.tag,
2885 sizeof(ib_spec->mpls.val.tag));
2886 memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
2887 outer_first_mpls),
2888 &ib_spec->mpls.mask.tag,
2889 sizeof(ib_spec->mpls.mask.tag));
2890 }
2891 }
2892 break;
Moses Reubenffb30d82016-11-14 19:04:50 +02002893 case IB_FLOW_SPEC_VXLAN_TUNNEL:
2894 if (FIELDS_NOT_SUPPORTED(ib_spec->tunnel.mask,
2895 LAST_TUNNEL_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002896 return -EOPNOTSUPP;
Moses Reubenffb30d82016-11-14 19:04:50 +02002897
2898 MLX5_SET(fte_match_set_misc, misc_params_c, vxlan_vni,
2899 ntohl(ib_spec->tunnel.mask.tunnel_id));
2900 MLX5_SET(fte_match_set_misc, misc_params_v, vxlan_vni,
2901 ntohl(ib_spec->tunnel.val.tunnel_id));
2902 break;
Moses Reuben2ac693f2017-01-18 14:59:50 +02002903 case IB_FLOW_SPEC_ACTION_TAG:
2904 if (FIELDS_NOT_SUPPORTED(ib_spec->flow_tag,
2905 LAST_FLOW_TAG_FIELD))
2906 return -EOPNOTSUPP;
2907 if (ib_spec->flow_tag.tag_id >= BIT(24))
2908 return -EINVAL;
2909
Boris Pismenny075572d2017-08-16 09:33:30 +03002910 action->flow_tag = ib_spec->flow_tag.tag_id;
Paul Blakeyd5634fe2018-09-20 12:17:48 +02002911 action->flags |= FLOW_ACT_HAS_TAG;
Moses Reuben2ac693f2017-01-18 14:59:50 +02002912 break;
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002913 case IB_FLOW_SPEC_ACTION_DROP:
2914 if (FIELDS_NOT_SUPPORTED(ib_spec->drop,
2915 LAST_DROP_FIELD))
2916 return -EOPNOTSUPP;
Boris Pismenny075572d2017-08-16 09:33:30 +03002917 action->action |= MLX5_FLOW_CONTEXT_ACTION_DROP;
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002918 break;
Aviad Yehezkel802c2122018-03-28 09:27:53 +03002919 case IB_FLOW_SPEC_ACTION_HANDLE:
Mark Bloch2ea26202018-09-06 17:27:03 +03002920 ret = parse_flow_flow_action(to_mflow_act(ib_spec->action.act),
2921 flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS, action);
Aviad Yehezkel802c2122018-03-28 09:27:53 +03002922 if (ret)
2923 return ret;
2924 break;
Raed Salem3b3233f2018-05-31 16:43:39 +03002925 case IB_FLOW_SPEC_ACTION_COUNT:
2926 if (FIELDS_NOT_SUPPORTED(ib_spec->flow_count,
2927 LAST_COUNTERS_FIELD))
2928 return -EOPNOTSUPP;
2929
2930 /* for now support only one counters spec per flow */
2931 if (action->action & MLX5_FLOW_CONTEXT_ACTION_COUNT)
2932 return -EINVAL;
2933
2934 action->counters = ib_spec->flow_count.counters;
2935 action->action |= MLX5_FLOW_CONTEXT_ACTION_COUNT;
2936 break;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002937 default:
2938 return -EINVAL;
2939 }
2940
2941 return 0;
2942}
2943
2944/* If a flow could catch both multicast and unicast packets,
2945 * it won't fall into the multicast flow steering table and this rule
2946 * could steal other multicast packets.
2947 */
Yishai Hadasa550ddf2017-08-17 15:52:33 +03002948static bool flow_is_multicast_only(const struct ib_flow_attr *ib_attr)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002949{
Yishai Hadas81e30882017-06-08 16:15:09 +03002950 union ib_flow_spec *flow_spec;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002951
2952 if (ib_attr->type != IB_FLOW_ATTR_NORMAL ||
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002953 ib_attr->num_of_specs < 1)
2954 return false;
2955
Yishai Hadas81e30882017-06-08 16:15:09 +03002956 flow_spec = (union ib_flow_spec *)(ib_attr + 1);
2957 if (flow_spec->type == IB_FLOW_SPEC_IPV4) {
2958 struct ib_flow_spec_ipv4 *ipv4_spec;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002959
Yishai Hadas81e30882017-06-08 16:15:09 +03002960 ipv4_spec = (struct ib_flow_spec_ipv4 *)flow_spec;
2961 if (ipv4_is_multicast(ipv4_spec->val.dst_ip))
2962 return true;
2963
2964 return false;
2965 }
2966
2967 if (flow_spec->type == IB_FLOW_SPEC_ETH) {
2968 struct ib_flow_spec_eth *eth_spec;
2969
2970 eth_spec = (struct ib_flow_spec_eth *)flow_spec;
2971 return is_multicast_ether_addr(eth_spec->mask.dst_mac) &&
2972 is_multicast_ether_addr(eth_spec->val.dst_mac);
2973 }
2974
2975 return false;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002976}
2977
Aviad Yehezkel802c2122018-03-28 09:27:53 +03002978enum valid_spec {
2979 VALID_SPEC_INVALID,
2980 VALID_SPEC_VALID,
2981 VALID_SPEC_NA,
2982};
2983
2984static enum valid_spec
2985is_valid_esp_aes_gcm(struct mlx5_core_dev *mdev,
2986 const struct mlx5_flow_spec *spec,
2987 const struct mlx5_flow_act *flow_act,
2988 bool egress)
2989{
2990 const u32 *match_c = spec->match_criteria;
2991 bool is_crypto =
2992 (flow_act->action & (MLX5_FLOW_CONTEXT_ACTION_ENCRYPT |
2993 MLX5_FLOW_CONTEXT_ACTION_DECRYPT));
2994 bool is_ipsec = mlx5_fs_is_ipsec_flow(match_c);
2995 bool is_drop = flow_act->action & MLX5_FLOW_CONTEXT_ACTION_DROP;
2996
2997 /*
2998 * Currently only crypto is supported in egress, when regular egress
2999 * rules would be supported, always return VALID_SPEC_NA.
3000 */
3001 if (!is_crypto)
Mark Bloch78dd0c42018-09-02 12:51:31 +03003002 return VALID_SPEC_NA;
Aviad Yehezkel802c2122018-03-28 09:27:53 +03003003
3004 return is_crypto && is_ipsec &&
Paul Blakeyd5634fe2018-09-20 12:17:48 +02003005 (!egress || (!is_drop && !(flow_act->flags & FLOW_ACT_HAS_TAG))) ?
Aviad Yehezkel802c2122018-03-28 09:27:53 +03003006 VALID_SPEC_VALID : VALID_SPEC_INVALID;
3007}
3008
3009static bool is_valid_spec(struct mlx5_core_dev *mdev,
3010 const struct mlx5_flow_spec *spec,
3011 const struct mlx5_flow_act *flow_act,
3012 bool egress)
3013{
3014 /* We curretly only support ipsec egress flow */
3015 return is_valid_esp_aes_gcm(mdev, spec, flow_act, egress) != VALID_SPEC_INVALID;
3016}
3017
Ariel Levkovich19cc7522017-04-03 13:11:03 +03003018static bool is_valid_ethertype(struct mlx5_core_dev *mdev,
3019 const struct ib_flow_attr *flow_attr,
Ariel Levkovich0f750962017-04-03 13:11:02 +03003020 bool check_inner)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003021{
3022 union ib_flow_spec *ib_spec = (union ib_flow_spec *)(flow_attr + 1);
Ariel Levkovich19cc7522017-04-03 13:11:03 +03003023 int match_ipv = check_inner ?
3024 MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
3025 ft_field_support.inner_ip_version) :
3026 MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
3027 ft_field_support.outer_ip_version);
Ariel Levkovich0f750962017-04-03 13:11:02 +03003028 int inner_bit = check_inner ? IB_FLOW_SPEC_INNER : 0;
3029 bool ipv4_spec_valid, ipv6_spec_valid;
3030 unsigned int ip_spec_type = 0;
3031 bool has_ethertype = false;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003032 unsigned int spec_index;
Ariel Levkovich0f750962017-04-03 13:11:02 +03003033 bool mask_valid = true;
3034 u16 eth_type = 0;
3035 bool type_valid;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003036
3037 /* Validate that ethertype is correct */
3038 for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
Ariel Levkovich0f750962017-04-03 13:11:02 +03003039 if ((ib_spec->type == (IB_FLOW_SPEC_ETH | inner_bit)) &&
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003040 ib_spec->eth.mask.ether_type) {
Ariel Levkovich0f750962017-04-03 13:11:02 +03003041 mask_valid = (ib_spec->eth.mask.ether_type ==
3042 htons(0xffff));
3043 has_ethertype = true;
3044 eth_type = ntohs(ib_spec->eth.val.ether_type);
3045 } else if ((ib_spec->type == (IB_FLOW_SPEC_IPV4 | inner_bit)) ||
3046 (ib_spec->type == (IB_FLOW_SPEC_IPV6 | inner_bit))) {
3047 ip_spec_type = ib_spec->type;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003048 }
3049 ib_spec = (void *)ib_spec + ib_spec->size;
3050 }
Ariel Levkovich0f750962017-04-03 13:11:02 +03003051
3052 type_valid = (!has_ethertype) || (!ip_spec_type);
3053 if (!type_valid && mask_valid) {
3054 ipv4_spec_valid = (eth_type == ETH_P_IP) &&
3055 (ip_spec_type == (IB_FLOW_SPEC_IPV4 | inner_bit));
3056 ipv6_spec_valid = (eth_type == ETH_P_IPV6) &&
3057 (ip_spec_type == (IB_FLOW_SPEC_IPV6 | inner_bit));
Ariel Levkovich19cc7522017-04-03 13:11:03 +03003058
3059 type_valid = (ipv4_spec_valid) || (ipv6_spec_valid) ||
3060 (((eth_type == ETH_P_MPLS_UC) ||
3061 (eth_type == ETH_P_MPLS_MC)) && match_ipv);
Ariel Levkovich0f750962017-04-03 13:11:02 +03003062 }
3063
3064 return type_valid;
3065}
3066
Ariel Levkovich19cc7522017-04-03 13:11:03 +03003067static bool is_valid_attr(struct mlx5_core_dev *mdev,
3068 const struct ib_flow_attr *flow_attr)
Ariel Levkovich0f750962017-04-03 13:11:02 +03003069{
Ariel Levkovich19cc7522017-04-03 13:11:03 +03003070 return is_valid_ethertype(mdev, flow_attr, false) &&
3071 is_valid_ethertype(mdev, flow_attr, true);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003072}
3073
3074static void put_flow_table(struct mlx5_ib_dev *dev,
3075 struct mlx5_ib_flow_prio *prio, bool ft_added)
3076{
3077 prio->refcount -= !!ft_added;
3078 if (!prio->refcount) {
3079 mlx5_destroy_flow_table(prio->flow_table);
3080 prio->flow_table = NULL;
3081 }
3082}
3083
Raed Salem3b3233f2018-05-31 16:43:39 +03003084static void counters_clear_description(struct ib_counters *counters)
3085{
3086 struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
3087
3088 mutex_lock(&mcounters->mcntrs_mutex);
3089 kfree(mcounters->counters_data);
3090 mcounters->counters_data = NULL;
3091 mcounters->cntrs_max_index = 0;
3092 mutex_unlock(&mcounters->mcntrs_mutex);
3093}
3094
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003095static int mlx5_ib_destroy_flow(struct ib_flow *flow_id)
3096{
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003097 struct mlx5_ib_flow_handler *handler = container_of(flow_id,
3098 struct mlx5_ib_flow_handler,
3099 ibflow);
3100 struct mlx5_ib_flow_handler *iter, *tmp;
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003101 struct mlx5_ib_dev *dev = handler->dev;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003102
Mark Bloch9a4ca382018-01-16 14:42:35 +00003103 mutex_lock(&dev->flow_db->lock);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003104
3105 list_for_each_entry_safe(iter, tmp, &handler->list, list) {
Mark Bloch74491de2016-08-31 11:24:25 +00003106 mlx5_del_flow_rules(iter->rule);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003107 put_flow_table(dev, iter->prio, true);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003108 list_del(&iter->list);
3109 kfree(iter);
3110 }
3111
Mark Bloch74491de2016-08-31 11:24:25 +00003112 mlx5_del_flow_rules(handler->rule);
Maor Gottlieb5497adc2016-08-28 14:16:31 +03003113 put_flow_table(dev, handler->prio, true);
Raed Salem3b3233f2018-05-31 16:43:39 +03003114 if (handler->ibcounters &&
3115 atomic_read(&handler->ibcounters->usecnt) == 1)
3116 counters_clear_description(handler->ibcounters);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003117
Raed Salem3b3233f2018-05-31 16:43:39 +03003118 mutex_unlock(&dev->flow_db->lock);
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003119 if (handler->flow_matcher)
3120 atomic_dec(&handler->flow_matcher->usecnt);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003121 kfree(handler);
3122
3123 return 0;
3124}
3125
Maor Gottlieb35d190112016-03-07 18:51:47 +02003126static int ib_prio_to_core_prio(unsigned int priority, bool dont_trap)
3127{
3128 priority *= 2;
3129 if (!dont_trap)
3130 priority++;
3131 return priority;
3132}
3133
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003134enum flow_table_type {
3135 MLX5_IB_FT_RX,
3136 MLX5_IB_FT_TX
3137};
3138
Maor Gottlieb00b7c2a2017-03-29 06:09:01 +03003139#define MLX5_FS_MAX_TYPES 6
3140#define MLX5_FS_MAX_ENTRIES BIT(16)
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003141
3142static struct mlx5_ib_flow_prio *_get_prio(struct mlx5_flow_namespace *ns,
3143 struct mlx5_ib_flow_prio *prio,
3144 int priority,
Mark Bloch4adda112018-09-02 12:51:33 +03003145 int num_entries, int num_groups,
3146 u32 flags)
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003147{
3148 struct mlx5_flow_table *ft;
3149
3150 ft = mlx5_create_auto_grouped_flow_table(ns, priority,
3151 num_entries,
3152 num_groups,
Mark Bloch4adda112018-09-02 12:51:33 +03003153 0, flags);
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003154 if (IS_ERR(ft))
3155 return ERR_CAST(ft);
3156
3157 prio->flow_table = ft;
3158 prio->refcount = 0;
3159 return prio;
3160}
3161
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003162static struct mlx5_ib_flow_prio *get_flow_table(struct mlx5_ib_dev *dev,
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003163 struct ib_flow_attr *flow_attr,
3164 enum flow_table_type ft_type)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003165{
Maor Gottlieb35d190112016-03-07 18:51:47 +02003166 bool dont_trap = flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003167 struct mlx5_flow_namespace *ns = NULL;
3168 struct mlx5_ib_flow_prio *prio;
3169 struct mlx5_flow_table *ft;
Maor Gottliebdac388e2017-03-29 06:09:00 +03003170 int max_table_size;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003171 int num_entries;
3172 int num_groups;
Mark Bloch4adda112018-09-02 12:51:33 +03003173 u32 flags = 0;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003174 int priority;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003175
Maor Gottliebdac388e2017-03-29 06:09:00 +03003176 max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
3177 log_max_ft_size));
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003178 if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
Mark Bloch78dd0c42018-09-02 12:51:31 +03003179 enum mlx5_flow_namespace_type fn_type;
3180
3181 if (flow_is_multicast_only(flow_attr) &&
3182 !dont_trap)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003183 priority = MLX5_IB_FLOW_MCAST_PRIO;
3184 else
Maor Gottlieb35d190112016-03-07 18:51:47 +02003185 priority = ib_prio_to_core_prio(flow_attr->priority,
3186 dont_trap);
Mark Bloch78dd0c42018-09-02 12:51:31 +03003187 if (ft_type == MLX5_IB_FT_RX) {
3188 fn_type = MLX5_FLOW_NAMESPACE_BYPASS;
3189 prio = &dev->flow_db->prios[priority];
Mark Bloch6a4d00b2019-03-28 15:27:37 +02003190 if (!dev->is_rep &&
Mark Bloch4adda112018-09-02 12:51:33 +03003191 MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, decap))
3192 flags |= MLX5_FLOW_TABLE_TUNNEL_EN_DECAP;
Mark Bloch6a4d00b2019-03-28 15:27:37 +02003193 if (!dev->is_rep &&
Mark Bloch5c2db532018-09-02 12:51:35 +03003194 MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
3195 reformat_l3_tunnel_to_l2))
3196 flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
Mark Bloch78dd0c42018-09-02 12:51:31 +03003197 } else {
3198 max_table_size =
3199 BIT(MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev,
3200 log_max_ft_size));
3201 fn_type = MLX5_FLOW_NAMESPACE_EGRESS;
3202 prio = &dev->flow_db->egress_prios[priority];
Mark Bloch6a4d00b2019-03-28 15:27:37 +02003203 if (!dev->is_rep &&
Mark Bloch4adda112018-09-02 12:51:33 +03003204 MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev, reformat))
3205 flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
Mark Bloch78dd0c42018-09-02 12:51:31 +03003206 }
3207 ns = mlx5_get_flow_namespace(dev->mdev, fn_type);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003208 num_entries = MLX5_FS_MAX_ENTRIES;
3209 num_groups = MLX5_FS_MAX_TYPES;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003210 } else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
3211 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
3212 ns = mlx5_get_flow_namespace(dev->mdev,
3213 MLX5_FLOW_NAMESPACE_LEFTOVERS);
3214 build_leftovers_ft_param(&priority,
3215 &num_entries,
3216 &num_groups);
Mark Bloch9a4ca382018-01-16 14:42:35 +00003217 prio = &dev->flow_db->prios[MLX5_IB_FLOW_LEFTOVERS_PRIO];
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003218 } else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
3219 if (!MLX5_CAP_FLOWTABLE(dev->mdev,
3220 allow_sniffer_and_nic_rx_shared_tir))
3221 return ERR_PTR(-ENOTSUPP);
3222
3223 ns = mlx5_get_flow_namespace(dev->mdev, ft_type == MLX5_IB_FT_RX ?
3224 MLX5_FLOW_NAMESPACE_SNIFFER_RX :
3225 MLX5_FLOW_NAMESPACE_SNIFFER_TX);
3226
Mark Bloch9a4ca382018-01-16 14:42:35 +00003227 prio = &dev->flow_db->sniffer[ft_type];
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003228 priority = 0;
3229 num_entries = 1;
3230 num_groups = 1;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003231 }
3232
3233 if (!ns)
3234 return ERR_PTR(-ENOTSUPP);
3235
Mark Bloch3b705082019-03-28 15:46:22 +02003236 max_table_size = min_t(int, num_entries, max_table_size);
Maor Gottliebdac388e2017-03-29 06:09:00 +03003237
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003238 ft = prio->flow_table;
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003239 if (!ft)
Mark Bloch3b705082019-03-28 15:46:22 +02003240 return _get_prio(ns, prio, priority, max_table_size, num_groups,
Mark Bloch4adda112018-09-02 12:51:33 +03003241 flags);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003242
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003243 return prio;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003244}
3245
Yishai Hadasa550ddf2017-08-17 15:52:33 +03003246static void set_underlay_qp(struct mlx5_ib_dev *dev,
3247 struct mlx5_flow_spec *spec,
3248 u32 underlay_qpn)
3249{
3250 void *misc_params_c = MLX5_ADDR_OF(fte_match_param,
3251 spec->match_criteria,
3252 misc_parameters);
3253 void *misc_params_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
3254 misc_parameters);
3255
3256 if (underlay_qpn &&
3257 MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
3258 ft_field_support.bth_dst_qp)) {
3259 MLX5_SET(fte_match_set_misc,
3260 misc_params_v, bth_dst_qp, underlay_qpn);
3261 MLX5_SET(fte_match_set_misc,
3262 misc_params_c, bth_dst_qp, 0xffffff);
3263 }
3264}
3265
Raed Salem5e95af52018-05-31 16:43:40 +03003266static int read_flow_counters(struct ib_device *ibdev,
3267 struct mlx5_read_counters_attr *read_attr)
3268{
3269 struct mlx5_fc *fc = read_attr->hw_cntrs_hndl;
3270 struct mlx5_ib_dev *dev = to_mdev(ibdev);
3271
3272 return mlx5_fc_query(dev->mdev, fc,
3273 &read_attr->out[IB_COUNTER_PACKETS],
3274 &read_attr->out[IB_COUNTER_BYTES]);
3275}
3276
3277/* flow counters currently expose two counters packets and bytes */
3278#define FLOW_COUNTERS_NUM 2
Raed Salem3b3233f2018-05-31 16:43:39 +03003279static int counters_set_description(struct ib_counters *counters,
3280 enum mlx5_ib_counters_type counters_type,
3281 struct mlx5_ib_flow_counters_desc *desc_data,
3282 u32 ncounters)
3283{
3284 struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
3285 u32 cntrs_max_index = 0;
3286 int i;
3287
3288 if (counters_type != MLX5_IB_COUNTERS_FLOW)
3289 return -EINVAL;
3290
3291 /* init the fields for the object */
3292 mcounters->type = counters_type;
Raed Salem5e95af52018-05-31 16:43:40 +03003293 mcounters->read_counters = read_flow_counters;
3294 mcounters->counters_num = FLOW_COUNTERS_NUM;
Raed Salem3b3233f2018-05-31 16:43:39 +03003295 mcounters->ncounters = ncounters;
3296 /* each counter entry have both description and index pair */
3297 for (i = 0; i < ncounters; i++) {
3298 if (desc_data[i].description > IB_COUNTER_BYTES)
3299 return -EINVAL;
3300
3301 if (cntrs_max_index <= desc_data[i].index)
3302 cntrs_max_index = desc_data[i].index + 1;
3303 }
3304
3305 mutex_lock(&mcounters->mcntrs_mutex);
3306 mcounters->counters_data = desc_data;
3307 mcounters->cntrs_max_index = cntrs_max_index;
3308 mutex_unlock(&mcounters->mcntrs_mutex);
3309
3310 return 0;
3311}
3312
3313#define MAX_COUNTERS_NUM (USHRT_MAX / (sizeof(u32) * 2))
3314static int flow_counters_set_data(struct ib_counters *ibcounters,
3315 struct mlx5_ib_create_flow *ucmd)
3316{
3317 struct mlx5_ib_mcounters *mcounters = to_mcounters(ibcounters);
3318 struct mlx5_ib_flow_counters_data *cntrs_data = NULL;
3319 struct mlx5_ib_flow_counters_desc *desc_data = NULL;
3320 bool hw_hndl = false;
3321 int ret = 0;
3322
3323 if (ucmd && ucmd->ncounters_data != 0) {
3324 cntrs_data = ucmd->data;
3325 if (cntrs_data->ncounters > MAX_COUNTERS_NUM)
3326 return -EINVAL;
3327
3328 desc_data = kcalloc(cntrs_data->ncounters,
3329 sizeof(*desc_data),
3330 GFP_KERNEL);
3331 if (!desc_data)
3332 return -ENOMEM;
3333
3334 if (copy_from_user(desc_data,
3335 u64_to_user_ptr(cntrs_data->counters_data),
3336 sizeof(*desc_data) * cntrs_data->ncounters)) {
3337 ret = -EFAULT;
3338 goto free;
3339 }
3340 }
3341
3342 if (!mcounters->hw_cntrs_hndl) {
3343 mcounters->hw_cntrs_hndl = mlx5_fc_create(
3344 to_mdev(ibcounters->device)->mdev, false);
weiyongjun (A)e31abf72018-06-07 01:47:41 +00003345 if (IS_ERR(mcounters->hw_cntrs_hndl)) {
3346 ret = PTR_ERR(mcounters->hw_cntrs_hndl);
Raed Salem3b3233f2018-05-31 16:43:39 +03003347 goto free;
3348 }
3349 hw_hndl = true;
3350 }
3351
3352 if (desc_data) {
3353 /* counters already bound to at least one flow */
3354 if (mcounters->cntrs_max_index) {
3355 ret = -EINVAL;
3356 goto free_hndl;
3357 }
3358
3359 ret = counters_set_description(ibcounters,
3360 MLX5_IB_COUNTERS_FLOW,
3361 desc_data,
3362 cntrs_data->ncounters);
3363 if (ret)
3364 goto free_hndl;
3365
3366 } else if (!mcounters->cntrs_max_index) {
3367 /* counters not bound yet, must have udata passed */
3368 ret = -EINVAL;
3369 goto free_hndl;
3370 }
3371
3372 return 0;
3373
3374free_hndl:
3375 if (hw_hndl) {
3376 mlx5_fc_destroy(to_mdev(ibcounters->device)->mdev,
3377 mcounters->hw_cntrs_hndl);
3378 mcounters->hw_cntrs_hndl = NULL;
3379 }
3380free:
3381 kfree(desc_data);
3382 return ret;
3383}
3384
Yishai Hadasa550ddf2017-08-17 15:52:33 +03003385static struct mlx5_ib_flow_handler *_create_flow_rule(struct mlx5_ib_dev *dev,
3386 struct mlx5_ib_flow_prio *ft_prio,
3387 const struct ib_flow_attr *flow_attr,
3388 struct mlx5_flow_destination *dst,
Raed Salem3b3233f2018-05-31 16:43:39 +03003389 u32 underlay_qpn,
3390 struct mlx5_ib_create_flow *ucmd)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003391{
3392 struct mlx5_flow_table *ft = ft_prio->flow_table;
3393 struct mlx5_ib_flow_handler *handler;
Boris Pismenny075572d2017-08-16 09:33:30 +03003394 struct mlx5_flow_act flow_act = {.flow_tag = MLX5_FS_DEFAULT_FLOW_TAG};
Maor Gottliebc5bb1732016-07-04 17:23:05 +03003395 struct mlx5_flow_spec *spec;
Raed Salem3b3233f2018-05-31 16:43:39 +03003396 struct mlx5_flow_destination dest_arr[2] = {};
3397 struct mlx5_flow_destination *rule_dst = dest_arr;
Maor Gottliebdd063d02016-08-28 14:16:32 +03003398 const void *ib_flow = (const void *)flow_attr + sizeof(*flow_attr);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003399 unsigned int spec_index;
Ariel Levkovich71c6e862018-05-13 14:33:34 +03003400 u32 prev_type = 0;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003401 int err = 0;
Raed Salem3b3233f2018-05-31 16:43:39 +03003402 int dest_num = 0;
Aviad Yehezkel802c2122018-03-28 09:27:53 +03003403 bool is_egress = flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003404
Ariel Levkovich19cc7522017-04-03 13:11:03 +03003405 if (!is_valid_attr(dev->mdev, flow_attr))
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003406 return ERR_PTR(-EINVAL);
3407
Mark Bloch6a4d00b2019-03-28 15:27:37 +02003408 if (dev->is_rep && is_egress)
Mark Bloch78dd0c42018-09-02 12:51:31 +03003409 return ERR_PTR(-EINVAL);
3410
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03003411 spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003412 handler = kzalloc(sizeof(*handler), GFP_KERNEL);
Maor Gottliebc5bb1732016-07-04 17:23:05 +03003413 if (!handler || !spec) {
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003414 err = -ENOMEM;
3415 goto free;
3416 }
3417
3418 INIT_LIST_HEAD(&handler->list);
Raed Salem3b3233f2018-05-31 16:43:39 +03003419 if (dst) {
3420 memcpy(&dest_arr[0], dst, sizeof(*dst));
3421 dest_num++;
3422 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003423
3424 for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
Ariel Levkovich19cc7522017-04-03 13:11:03 +03003425 err = parse_flow_attr(dev->mdev, spec->match_criteria,
Slava Shwartsmana22ed862017-04-03 13:13:52 +03003426 spec->match_value,
Ariel Levkovich71c6e862018-05-13 14:33:34 +03003427 ib_flow, flow_attr, &flow_act,
3428 prev_type);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003429 if (err < 0)
3430 goto free;
3431
Ariel Levkovich71c6e862018-05-13 14:33:34 +03003432 prev_type = ((union ib_flow_spec *)ib_flow)->type;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003433 ib_flow += ((union ib_flow_spec *)ib_flow)->size;
3434 }
3435
Yishai Hadasa550ddf2017-08-17 15:52:33 +03003436 if (!flow_is_multicast_only(flow_attr))
3437 set_underlay_qp(dev, spec, underlay_qpn);
3438
Mark Bloch6a4d00b2019-03-28 15:27:37 +02003439 if (dev->is_rep) {
Mark Bloch018a94e2018-01-16 14:44:29 +00003440 void *misc;
3441
Mark Bloch6a4d00b2019-03-28 15:27:37 +02003442 if (!dev->port[flow_attr->port - 1].rep) {
3443 err = -EINVAL;
3444 goto free;
3445 }
Mark Bloch018a94e2018-01-16 14:44:29 +00003446 misc = MLX5_ADDR_OF(fte_match_param, spec->match_value,
3447 misc_parameters);
3448 MLX5_SET(fte_match_set_misc, misc, source_port,
Mark Bloch6a4d00b2019-03-28 15:27:37 +02003449 dev->port[flow_attr->port - 1].rep->vport);
Mark Bloch018a94e2018-01-16 14:44:29 +00003450 misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
3451 misc_parameters);
3452 MLX5_SET_TO_ONES(fte_match_set_misc, misc, source_port);
3453 }
3454
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03003455 spec->match_criteria_enable = get_match_criteria_enable(spec->match_criteria);
Aviad Yehezkel802c2122018-03-28 09:27:53 +03003456
3457 if (is_egress &&
3458 !is_valid_spec(dev->mdev, spec, &flow_act, is_egress)) {
3459 err = -EINVAL;
3460 goto free;
3461 }
3462
Raed Salem3b3233f2018-05-31 16:43:39 +03003463 if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_COUNT) {
Mark Bloch171c7625b2018-10-03 00:03:35 +00003464 struct mlx5_ib_mcounters *mcounters;
3465
Raed Salem3b3233f2018-05-31 16:43:39 +03003466 err = flow_counters_set_data(flow_act.counters, ucmd);
3467 if (err)
3468 goto free;
3469
Mark Bloch171c7625b2018-10-03 00:03:35 +00003470 mcounters = to_mcounters(flow_act.counters);
Raed Salem3b3233f2018-05-31 16:43:39 +03003471 handler->ibcounters = flow_act.counters;
3472 dest_arr[dest_num].type =
3473 MLX5_FLOW_DESTINATION_TYPE_COUNTER;
Mark Bloch171c7625b2018-10-03 00:03:35 +00003474 dest_arr[dest_num].counter_id =
3475 mlx5_fc_id(mcounters->hw_cntrs_hndl);
Raed Salem3b3233f2018-05-31 16:43:39 +03003476 dest_num++;
3477 }
3478
Boris Pismenny075572d2017-08-16 09:33:30 +03003479 if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_DROP) {
Raed Salem3b3233f2018-05-31 16:43:39 +03003480 if (!(flow_act.action & MLX5_FLOW_CONTEXT_ACTION_COUNT)) {
3481 rule_dst = NULL;
3482 dest_num = 0;
3483 }
Slava Shwartsmana22ed862017-04-03 13:13:52 +03003484 } else {
Aviad Yehezkel802c2122018-03-28 09:27:53 +03003485 if (is_egress)
3486 flow_act.action |= MLX5_FLOW_CONTEXT_ACTION_ALLOW;
3487 else
3488 flow_act.action |=
Raed Salem3b3233f2018-05-31 16:43:39 +03003489 dest_num ? MLX5_FLOW_CONTEXT_ACTION_FWD_DEST :
Aviad Yehezkel802c2122018-03-28 09:27:53 +03003490 MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO;
Slava Shwartsmana22ed862017-04-03 13:13:52 +03003491 }
Moses Reuben2ac693f2017-01-18 14:59:50 +02003492
Paul Blakeyd5634fe2018-09-20 12:17:48 +02003493 if ((flow_act.flags & FLOW_ACT_HAS_TAG) &&
Moses Reuben2ac693f2017-01-18 14:59:50 +02003494 (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
3495 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT)) {
3496 mlx5_ib_warn(dev, "Flow tag %u and attribute type %x isn't allowed in leftovers\n",
Boris Pismenny075572d2017-08-16 09:33:30 +03003497 flow_act.flow_tag, flow_attr->type);
Moses Reuben2ac693f2017-01-18 14:59:50 +02003498 err = -EINVAL;
3499 goto free;
3500 }
Mark Bloch74491de2016-08-31 11:24:25 +00003501 handler->rule = mlx5_add_flow_rules(ft, spec,
Hadar Hen Zion66958ed2016-11-07 15:14:45 +02003502 &flow_act,
Slava Shwartsmana22ed862017-04-03 13:13:52 +03003503 rule_dst, dest_num);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003504
3505 if (IS_ERR(handler->rule)) {
3506 err = PTR_ERR(handler->rule);
3507 goto free;
3508 }
3509
Maor Gottliebd9d49802016-08-28 14:16:33 +03003510 ft_prio->refcount++;
Maor Gottlieb5497adc2016-08-28 14:16:31 +03003511 handler->prio = ft_prio;
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003512 handler->dev = dev;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003513
3514 ft_prio->flow_table = ft;
3515free:
Raed Salem3b3233f2018-05-31 16:43:39 +03003516 if (err && handler) {
3517 if (handler->ibcounters &&
3518 atomic_read(&handler->ibcounters->usecnt) == 1)
3519 counters_clear_description(handler->ibcounters);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003520 kfree(handler);
Raed Salem3b3233f2018-05-31 16:43:39 +03003521 }
Maor Gottliebc5bb1732016-07-04 17:23:05 +03003522 kvfree(spec);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003523 return err ? ERR_PTR(err) : handler;
3524}
3525
Yishai Hadasa550ddf2017-08-17 15:52:33 +03003526static struct mlx5_ib_flow_handler *create_flow_rule(struct mlx5_ib_dev *dev,
3527 struct mlx5_ib_flow_prio *ft_prio,
3528 const struct ib_flow_attr *flow_attr,
3529 struct mlx5_flow_destination *dst)
3530{
Raed Salem3b3233f2018-05-31 16:43:39 +03003531 return _create_flow_rule(dev, ft_prio, flow_attr, dst, 0, NULL);
Yishai Hadasa550ddf2017-08-17 15:52:33 +03003532}
3533
Maor Gottlieb35d190112016-03-07 18:51:47 +02003534static struct mlx5_ib_flow_handler *create_dont_trap_rule(struct mlx5_ib_dev *dev,
3535 struct mlx5_ib_flow_prio *ft_prio,
3536 struct ib_flow_attr *flow_attr,
3537 struct mlx5_flow_destination *dst)
3538{
3539 struct mlx5_ib_flow_handler *handler_dst = NULL;
3540 struct mlx5_ib_flow_handler *handler = NULL;
3541
3542 handler = create_flow_rule(dev, ft_prio, flow_attr, NULL);
3543 if (!IS_ERR(handler)) {
3544 handler_dst = create_flow_rule(dev, ft_prio,
3545 flow_attr, dst);
3546 if (IS_ERR(handler_dst)) {
Mark Bloch74491de2016-08-31 11:24:25 +00003547 mlx5_del_flow_rules(handler->rule);
Maor Gottliebd9d49802016-08-28 14:16:33 +03003548 ft_prio->refcount--;
Maor Gottlieb35d190112016-03-07 18:51:47 +02003549 kfree(handler);
3550 handler = handler_dst;
3551 } else {
3552 list_add(&handler_dst->list, &handler->list);
3553 }
3554 }
3555
3556 return handler;
3557}
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003558enum {
3559 LEFTOVERS_MC,
3560 LEFTOVERS_UC,
3561};
3562
3563static struct mlx5_ib_flow_handler *create_leftovers_rule(struct mlx5_ib_dev *dev,
3564 struct mlx5_ib_flow_prio *ft_prio,
3565 struct ib_flow_attr *flow_attr,
3566 struct mlx5_flow_destination *dst)
3567{
3568 struct mlx5_ib_flow_handler *handler_ucast = NULL;
3569 struct mlx5_ib_flow_handler *handler = NULL;
3570
3571 static struct {
3572 struct ib_flow_attr flow_attr;
3573 struct ib_flow_spec_eth eth_flow;
3574 } leftovers_specs[] = {
3575 [LEFTOVERS_MC] = {
3576 .flow_attr = {
3577 .num_of_specs = 1,
3578 .size = sizeof(leftovers_specs[0])
3579 },
3580 .eth_flow = {
3581 .type = IB_FLOW_SPEC_ETH,
3582 .size = sizeof(struct ib_flow_spec_eth),
3583 .mask = {.dst_mac = {0x1} },
3584 .val = {.dst_mac = {0x1} }
3585 }
3586 },
3587 [LEFTOVERS_UC] = {
3588 .flow_attr = {
3589 .num_of_specs = 1,
3590 .size = sizeof(leftovers_specs[0])
3591 },
3592 .eth_flow = {
3593 .type = IB_FLOW_SPEC_ETH,
3594 .size = sizeof(struct ib_flow_spec_eth),
3595 .mask = {.dst_mac = {0x1} },
3596 .val = {.dst_mac = {} }
3597 }
3598 }
3599 };
3600
3601 handler = create_flow_rule(dev, ft_prio,
3602 &leftovers_specs[LEFTOVERS_MC].flow_attr,
3603 dst);
3604 if (!IS_ERR(handler) &&
3605 flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT) {
3606 handler_ucast = create_flow_rule(dev, ft_prio,
3607 &leftovers_specs[LEFTOVERS_UC].flow_attr,
3608 dst);
3609 if (IS_ERR(handler_ucast)) {
Mark Bloch74491de2016-08-31 11:24:25 +00003610 mlx5_del_flow_rules(handler->rule);
Maor Gottliebd9d49802016-08-28 14:16:33 +03003611 ft_prio->refcount--;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003612 kfree(handler);
3613 handler = handler_ucast;
3614 } else {
3615 list_add(&handler_ucast->list, &handler->list);
3616 }
3617 }
3618
3619 return handler;
3620}
3621
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003622static struct mlx5_ib_flow_handler *create_sniffer_rule(struct mlx5_ib_dev *dev,
3623 struct mlx5_ib_flow_prio *ft_rx,
3624 struct mlx5_ib_flow_prio *ft_tx,
3625 struct mlx5_flow_destination *dst)
3626{
3627 struct mlx5_ib_flow_handler *handler_rx;
3628 struct mlx5_ib_flow_handler *handler_tx;
3629 int err;
3630 static const struct ib_flow_attr flow_attr = {
3631 .num_of_specs = 0,
3632 .size = sizeof(flow_attr)
3633 };
3634
3635 handler_rx = create_flow_rule(dev, ft_rx, &flow_attr, dst);
3636 if (IS_ERR(handler_rx)) {
3637 err = PTR_ERR(handler_rx);
3638 goto err;
3639 }
3640
3641 handler_tx = create_flow_rule(dev, ft_tx, &flow_attr, dst);
3642 if (IS_ERR(handler_tx)) {
3643 err = PTR_ERR(handler_tx);
3644 goto err_tx;
3645 }
3646
3647 list_add(&handler_tx->list, &handler_rx->list);
3648
3649 return handler_rx;
3650
3651err_tx:
Mark Bloch74491de2016-08-31 11:24:25 +00003652 mlx5_del_flow_rules(handler_rx->rule);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003653 ft_rx->refcount--;
3654 kfree(handler_rx);
3655err:
3656 return ERR_PTR(err);
3657}
3658
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003659static struct ib_flow *mlx5_ib_create_flow(struct ib_qp *qp,
3660 struct ib_flow_attr *flow_attr,
Matan Barak59082a32018-05-31 16:43:35 +03003661 int domain,
3662 struct ib_udata *udata)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003663{
3664 struct mlx5_ib_dev *dev = to_mdev(qp->device);
Yishai Hadasd9f88e52016-08-28 10:58:37 +03003665 struct mlx5_ib_qp *mqp = to_mqp(qp);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003666 struct mlx5_ib_flow_handler *handler = NULL;
3667 struct mlx5_flow_destination *dst = NULL;
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003668 struct mlx5_ib_flow_prio *ft_prio_tx = NULL;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003669 struct mlx5_ib_flow_prio *ft_prio;
Aviad Yehezkel802c2122018-03-28 09:27:53 +03003670 bool is_egress = flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS;
Raed Salem3b3233f2018-05-31 16:43:39 +03003671 struct mlx5_ib_create_flow *ucmd = NULL, ucmd_hdr;
3672 size_t min_ucmd_sz, required_ucmd_sz;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003673 int err;
Yishai Hadasa550ddf2017-08-17 15:52:33 +03003674 int underlay_qpn;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003675
Raed Salem3b3233f2018-05-31 16:43:39 +03003676 if (udata && udata->inlen) {
3677 min_ucmd_sz = offsetof(typeof(ucmd_hdr), reserved) +
3678 sizeof(ucmd_hdr.reserved);
3679 if (udata->inlen < min_ucmd_sz)
3680 return ERR_PTR(-EOPNOTSUPP);
3681
3682 err = ib_copy_from_udata(&ucmd_hdr, udata, min_ucmd_sz);
3683 if (err)
3684 return ERR_PTR(err);
3685
3686 /* currently supports only one counters data */
3687 if (ucmd_hdr.ncounters_data > 1)
3688 return ERR_PTR(-EINVAL);
3689
3690 required_ucmd_sz = min_ucmd_sz +
3691 sizeof(struct mlx5_ib_flow_counters_data) *
3692 ucmd_hdr.ncounters_data;
3693 if (udata->inlen > required_ucmd_sz &&
3694 !ib_is_udata_cleared(udata, required_ucmd_sz,
3695 udata->inlen - required_ucmd_sz))
3696 return ERR_PTR(-EOPNOTSUPP);
3697
3698 ucmd = kzalloc(required_ucmd_sz, GFP_KERNEL);
3699 if (!ucmd)
3700 return ERR_PTR(-ENOMEM);
3701
3702 err = ib_copy_from_udata(ucmd, udata, required_ucmd_sz);
Gustavo A. R. Silva299eafe2018-06-07 14:19:15 -05003703 if (err)
3704 goto free_ucmd;
Raed Salem3b3233f2018-05-31 16:43:39 +03003705 }
Matan Barak59082a32018-05-31 16:43:35 +03003706
Gustavo A. R. Silva299eafe2018-06-07 14:19:15 -05003707 if (flow_attr->priority > MLX5_IB_FLOW_LAST_PRIO) {
3708 err = -ENOMEM;
3709 goto free_ucmd;
3710 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003711
3712 if (domain != IB_FLOW_DOMAIN_USER ||
Daniel Jurgens508562d2018-01-04 17:25:34 +02003713 flow_attr->port > dev->num_ports ||
Aviad Yehezkel802c2122018-03-28 09:27:53 +03003714 (flow_attr->flags & ~(IB_FLOW_ATTR_FLAGS_DONT_TRAP |
Gustavo A. R. Silva299eafe2018-06-07 14:19:15 -05003715 IB_FLOW_ATTR_FLAGS_EGRESS))) {
3716 err = -EINVAL;
3717 goto free_ucmd;
3718 }
Aviad Yehezkel802c2122018-03-28 09:27:53 +03003719
3720 if (is_egress &&
3721 (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
Gustavo A. R. Silva299eafe2018-06-07 14:19:15 -05003722 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT)) {
3723 err = -EINVAL;
3724 goto free_ucmd;
3725 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003726
3727 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
Gustavo A. R. Silva299eafe2018-06-07 14:19:15 -05003728 if (!dst) {
3729 err = -ENOMEM;
3730 goto free_ucmd;
3731 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003732
Mark Bloch9a4ca382018-01-16 14:42:35 +00003733 mutex_lock(&dev->flow_db->lock);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003734
Aviad Yehezkel802c2122018-03-28 09:27:53 +03003735 ft_prio = get_flow_table(dev, flow_attr,
3736 is_egress ? MLX5_IB_FT_TX : MLX5_IB_FT_RX);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003737 if (IS_ERR(ft_prio)) {
3738 err = PTR_ERR(ft_prio);
3739 goto unlock;
3740 }
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003741 if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
3742 ft_prio_tx = get_flow_table(dev, flow_attr, MLX5_IB_FT_TX);
3743 if (IS_ERR(ft_prio_tx)) {
3744 err = PTR_ERR(ft_prio_tx);
3745 ft_prio_tx = NULL;
3746 goto destroy_ft;
3747 }
3748 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003749
Aviad Yehezkel802c2122018-03-28 09:27:53 +03003750 if (is_egress) {
3751 dst->type = MLX5_FLOW_DESTINATION_TYPE_PORT;
3752 } else {
3753 dst->type = MLX5_FLOW_DESTINATION_TYPE_TIR;
3754 if (mqp->flags & MLX5_IB_QP_RSS)
3755 dst->tir_num = mqp->rss_qp.tirn;
3756 else
3757 dst->tir_num = mqp->raw_packet_qp.rq.tirn;
3758 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003759
3760 if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
Maor Gottlieb35d190112016-03-07 18:51:47 +02003761 if (flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP) {
3762 handler = create_dont_trap_rule(dev, ft_prio,
3763 flow_attr, dst);
3764 } else {
Yishai Hadasa550ddf2017-08-17 15:52:33 +03003765 underlay_qpn = (mqp->flags & MLX5_IB_QP_UNDERLAY) ?
3766 mqp->underlay_qpn : 0;
3767 handler = _create_flow_rule(dev, ft_prio, flow_attr,
Raed Salem3b3233f2018-05-31 16:43:39 +03003768 dst, underlay_qpn, ucmd);
Maor Gottlieb35d190112016-03-07 18:51:47 +02003769 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003770 } else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
3771 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
3772 handler = create_leftovers_rule(dev, ft_prio, flow_attr,
3773 dst);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003774 } else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
3775 handler = create_sniffer_rule(dev, ft_prio, ft_prio_tx, dst);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003776 } else {
3777 err = -EINVAL;
3778 goto destroy_ft;
3779 }
3780
3781 if (IS_ERR(handler)) {
3782 err = PTR_ERR(handler);
3783 handler = NULL;
3784 goto destroy_ft;
3785 }
3786
Mark Bloch9a4ca382018-01-16 14:42:35 +00003787 mutex_unlock(&dev->flow_db->lock);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003788 kfree(dst);
Raed Salem3b3233f2018-05-31 16:43:39 +03003789 kfree(ucmd);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003790
3791 return &handler->ibflow;
3792
3793destroy_ft:
3794 put_flow_table(dev, ft_prio, false);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003795 if (ft_prio_tx)
3796 put_flow_table(dev, ft_prio_tx, false);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003797unlock:
Mark Bloch9a4ca382018-01-16 14:42:35 +00003798 mutex_unlock(&dev->flow_db->lock);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003799 kfree(dst);
Gustavo A. R. Silva299eafe2018-06-07 14:19:15 -05003800free_ucmd:
Raed Salem3b3233f2018-05-31 16:43:39 +03003801 kfree(ucmd);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003802 return ERR_PTR(err);
3803}
3804
Mark Blochb47fd4f2018-09-06 17:27:07 +03003805static struct mlx5_ib_flow_prio *
3806_get_flow_table(struct mlx5_ib_dev *dev,
3807 struct mlx5_ib_flow_matcher *fs_matcher,
3808 bool mcast)
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003809{
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003810 struct mlx5_flow_namespace *ns = NULL;
Mark Bloch13a43762019-03-28 15:46:21 +02003811 struct mlx5_ib_flow_prio *prio = NULL;
3812 int max_table_size = 0;
Mark Blochb47fd4f2018-09-06 17:27:07 +03003813 u32 flags = 0;
3814 int priority;
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003815
Mark Bloch13a43762019-03-28 15:46:21 +02003816 if (mcast)
3817 priority = MLX5_IB_FLOW_MCAST_PRIO;
3818 else
3819 priority = ib_prio_to_core_prio(fs_matcher->priority, false);
3820
Mark Blochb47fd4f2018-09-06 17:27:07 +03003821 if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_BYPASS) {
3822 max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
3823 log_max_ft_size));
3824 if (MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, decap))
3825 flags |= MLX5_FLOW_TABLE_TUNNEL_EN_DECAP;
3826 if (MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
3827 reformat_l3_tunnel_to_l2))
3828 flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
Mark Bloch13a43762019-03-28 15:46:21 +02003829 } else if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_EGRESS) {
3830 max_table_size = BIT(
3831 MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev, log_max_ft_size));
Mark Blochb47fd4f2018-09-06 17:27:07 +03003832 if (MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev, reformat))
3833 flags |= MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
Mark Bloch13a43762019-03-28 15:46:21 +02003834 } else if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_FDB) {
3835 max_table_size = BIT(
3836 MLX5_CAP_ESW_FLOWTABLE_FDB(dev->mdev, log_max_ft_size));
3837 priority = FDB_BYPASS_PATH;
Mark Blochb47fd4f2018-09-06 17:27:07 +03003838 }
3839
Mark Bloch3b705082019-03-28 15:46:22 +02003840 max_table_size = min_t(int, max_table_size, MLX5_FS_MAX_ENTRIES);
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003841
Mark Blochb47fd4f2018-09-06 17:27:07 +03003842 ns = mlx5_get_flow_namespace(dev->mdev, fs_matcher->ns_type);
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003843 if (!ns)
3844 return ERR_PTR(-ENOTSUPP);
3845
Mark Blochb47fd4f2018-09-06 17:27:07 +03003846 if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_BYPASS)
3847 prio = &dev->flow_db->prios[priority];
Mark Bloch13a43762019-03-28 15:46:21 +02003848 else if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_EGRESS)
Mark Blochb47fd4f2018-09-06 17:27:07 +03003849 prio = &dev->flow_db->egress_prios[priority];
Mark Bloch13a43762019-03-28 15:46:21 +02003850 else if (fs_matcher->ns_type == MLX5_FLOW_NAMESPACE_FDB)
3851 prio = &dev->flow_db->fdb;
3852
3853 if (!prio)
3854 return ERR_PTR(-EINVAL);
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003855
3856 if (prio->flow_table)
3857 return prio;
3858
Mark Bloch3b705082019-03-28 15:46:22 +02003859 return _get_prio(ns, prio, priority, max_table_size,
Mark Blochb47fd4f2018-09-06 17:27:07 +03003860 MLX5_FS_MAX_TYPES, flags);
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003861}
3862
3863static struct mlx5_ib_flow_handler *
3864_create_raw_flow_rule(struct mlx5_ib_dev *dev,
3865 struct mlx5_ib_flow_prio *ft_prio,
3866 struct mlx5_flow_destination *dst,
3867 struct mlx5_ib_flow_matcher *fs_matcher,
Mark Blochb823dd62018-09-06 17:27:05 +03003868 struct mlx5_flow_act *flow_act,
Mark Blochbfc5d832018-11-20 20:31:08 +02003869 void *cmd_in, int inlen,
3870 int dst_num)
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003871{
3872 struct mlx5_ib_flow_handler *handler;
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003873 struct mlx5_flow_spec *spec;
3874 struct mlx5_flow_table *ft = ft_prio->flow_table;
3875 int err = 0;
3876
3877 spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
3878 handler = kzalloc(sizeof(*handler), GFP_KERNEL);
3879 if (!handler || !spec) {
3880 err = -ENOMEM;
3881 goto free;
3882 }
3883
3884 INIT_LIST_HEAD(&handler->list);
3885
3886 memcpy(spec->match_value, cmd_in, inlen);
3887 memcpy(spec->match_criteria, fs_matcher->matcher_mask.match_params,
3888 fs_matcher->mask_len);
3889 spec->match_criteria_enable = fs_matcher->match_criteria_enable;
3890
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003891 handler->rule = mlx5_add_flow_rules(ft, spec,
Mark Blochbfc5d832018-11-20 20:31:08 +02003892 flow_act, dst, dst_num);
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003893
3894 if (IS_ERR(handler->rule)) {
3895 err = PTR_ERR(handler->rule);
3896 goto free;
3897 }
3898
3899 ft_prio->refcount++;
3900 handler->prio = ft_prio;
3901 handler->dev = dev;
3902 ft_prio->flow_table = ft;
3903
3904free:
3905 if (err)
3906 kfree(handler);
3907 kvfree(spec);
3908 return err ? ERR_PTR(err) : handler;
3909}
3910
3911static bool raw_fs_is_multicast(struct mlx5_ib_flow_matcher *fs_matcher,
3912 void *match_v)
3913{
3914 void *match_c;
3915 void *match_v_set_lyr_2_4, *match_c_set_lyr_2_4;
3916 void *dmac, *dmac_mask;
3917 void *ipv4, *ipv4_mask;
3918
3919 if (!(fs_matcher->match_criteria_enable &
3920 (1 << MATCH_CRITERIA_ENABLE_OUTER_BIT)))
3921 return false;
3922
3923 match_c = fs_matcher->matcher_mask.match_params;
3924 match_v_set_lyr_2_4 = MLX5_ADDR_OF(fte_match_param, match_v,
3925 outer_headers);
3926 match_c_set_lyr_2_4 = MLX5_ADDR_OF(fte_match_param, match_c,
3927 outer_headers);
3928
3929 dmac = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_v_set_lyr_2_4,
3930 dmac_47_16);
3931 dmac_mask = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_c_set_lyr_2_4,
3932 dmac_47_16);
3933
3934 if (is_multicast_ether_addr(dmac) &&
3935 is_multicast_ether_addr(dmac_mask))
3936 return true;
3937
3938 ipv4 = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_v_set_lyr_2_4,
3939 dst_ipv4_dst_ipv6.ipv4_layout.ipv4);
3940
3941 ipv4_mask = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_c_set_lyr_2_4,
3942 dst_ipv4_dst_ipv6.ipv4_layout.ipv4);
3943
3944 if (ipv4_is_multicast(*(__be32 *)(ipv4)) &&
3945 ipv4_is_multicast(*(__be32 *)(ipv4_mask)))
3946 return true;
3947
3948 return false;
3949}
3950
Yishai Hadas32269442018-07-23 15:25:09 +03003951struct mlx5_ib_flow_handler *
3952mlx5_ib_raw_fs_rule_add(struct mlx5_ib_dev *dev,
3953 struct mlx5_ib_flow_matcher *fs_matcher,
Mark Blochb823dd62018-09-06 17:27:05 +03003954 struct mlx5_flow_act *flow_act,
Mark Blochbfc5d832018-11-20 20:31:08 +02003955 u32 counter_id,
Yishai Hadas32269442018-07-23 15:25:09 +03003956 void *cmd_in, int inlen, int dest_id,
3957 int dest_type)
3958{
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003959 struct mlx5_flow_destination *dst;
3960 struct mlx5_ib_flow_prio *ft_prio;
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003961 struct mlx5_ib_flow_handler *handler;
Mark Blochbfc5d832018-11-20 20:31:08 +02003962 int dst_num = 0;
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003963 bool mcast;
3964 int err;
3965
3966 if (fs_matcher->flow_type != MLX5_IB_FLOW_TYPE_NORMAL)
3967 return ERR_PTR(-EOPNOTSUPP);
3968
3969 if (fs_matcher->priority > MLX5_IB_FLOW_LAST_PRIO)
3970 return ERR_PTR(-ENOMEM);
3971
Gustavo A. R. Silva8e8aa142019-01-15 00:00:48 -06003972 dst = kcalloc(2, sizeof(*dst), GFP_KERNEL);
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003973 if (!dst)
3974 return ERR_PTR(-ENOMEM);
3975
3976 mcast = raw_fs_is_multicast(fs_matcher, cmd_in);
3977 mutex_lock(&dev->flow_db->lock);
3978
Mark Blochb47fd4f2018-09-06 17:27:07 +03003979 ft_prio = _get_flow_table(dev, fs_matcher, mcast);
Yishai Hadasd4be3f42018-07-23 15:25:10 +03003980 if (IS_ERR(ft_prio)) {
3981 err = PTR_ERR(ft_prio);
3982 goto unlock;
3983 }
3984
Yishai Hadas6346f0b2018-07-23 15:25:11 +03003985 if (dest_type == MLX5_FLOW_DESTINATION_TYPE_TIR) {
Mark Blochbfc5d832018-11-20 20:31:08 +02003986 dst[dst_num].type = dest_type;
3987 dst[dst_num].tir_num = dest_id;
Mark Blochb823dd62018-09-06 17:27:05 +03003988 flow_act->action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
Mark Blocha7ee18b2018-09-06 17:27:08 +03003989 } else if (dest_type == MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE) {
Mark Blochbfc5d832018-11-20 20:31:08 +02003990 dst[dst_num].type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE_NUM;
3991 dst[dst_num].ft_num = dest_id;
Mark Blochb823dd62018-09-06 17:27:05 +03003992 flow_act->action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
Mark Blocha7ee18b2018-09-06 17:27:08 +03003993 } else {
Mark Blochbfc5d832018-11-20 20:31:08 +02003994 dst[dst_num].type = MLX5_FLOW_DESTINATION_TYPE_PORT;
Mark Blocha7ee18b2018-09-06 17:27:08 +03003995 flow_act->action |= MLX5_FLOW_CONTEXT_ACTION_ALLOW;
Yishai Hadas6346f0b2018-07-23 15:25:11 +03003996 }
3997
Mark Blochbfc5d832018-11-20 20:31:08 +02003998 dst_num++;
3999
4000 if (flow_act->action & MLX5_FLOW_CONTEXT_ACTION_COUNT) {
4001 dst[dst_num].type = MLX5_FLOW_DESTINATION_TYPE_COUNTER;
4002 dst[dst_num].counter_id = counter_id;
4003 dst_num++;
4004 }
4005
Mark Blochb823dd62018-09-06 17:27:05 +03004006 handler = _create_raw_flow_rule(dev, ft_prio, dst, fs_matcher, flow_act,
Mark Blochbfc5d832018-11-20 20:31:08 +02004007 cmd_in, inlen, dst_num);
Yishai Hadasd4be3f42018-07-23 15:25:10 +03004008
4009 if (IS_ERR(handler)) {
4010 err = PTR_ERR(handler);
4011 goto destroy_ft;
4012 }
4013
4014 mutex_unlock(&dev->flow_db->lock);
4015 atomic_inc(&fs_matcher->usecnt);
4016 handler->flow_matcher = fs_matcher;
4017
4018 kfree(dst);
4019
4020 return handler;
4021
4022destroy_ft:
4023 put_flow_table(dev, ft_prio, false);
4024unlock:
4025 mutex_unlock(&dev->flow_db->lock);
4026 kfree(dst);
4027
4028 return ERR_PTR(err);
Yishai Hadas32269442018-07-23 15:25:09 +03004029}
4030
Aviad Yehezkelc6475a02018-03-28 09:27:50 +03004031static u32 mlx5_ib_flow_action_flags_to_accel_xfrm_flags(u32 mlx5_flags)
4032{
4033 u32 flags = 0;
4034
4035 if (mlx5_flags & MLX5_IB_UAPI_FLOW_ACTION_FLAGS_REQUIRE_METADATA)
4036 flags |= MLX5_ACCEL_XFRM_FLAG_REQUIRE_METADATA;
4037
4038 return flags;
4039}
4040
4041#define MLX5_FLOW_ACTION_ESP_CREATE_LAST_SUPPORTED MLX5_IB_UAPI_FLOW_ACTION_FLAGS_REQUIRE_METADATA
4042static struct ib_flow_action *
4043mlx5_ib_create_flow_action_esp(struct ib_device *device,
4044 const struct ib_flow_action_attrs_esp *attr,
4045 struct uverbs_attr_bundle *attrs)
4046{
4047 struct mlx5_ib_dev *mdev = to_mdev(device);
4048 struct ib_uverbs_flow_action_esp_keymat_aes_gcm *aes_gcm;
4049 struct mlx5_accel_esp_xfrm_attrs accel_attrs = {};
4050 struct mlx5_ib_flow_action *action;
4051 u64 action_flags;
4052 u64 flags;
4053 int err = 0;
4054
Jason Gunthorpebccd0622018-07-26 16:37:14 -06004055 err = uverbs_get_flags64(
4056 &action_flags, attrs, MLX5_IB_ATTR_CREATE_FLOW_ACTION_FLAGS,
4057 ((MLX5_FLOW_ACTION_ESP_CREATE_LAST_SUPPORTED << 1) - 1));
4058 if (err)
4059 return ERR_PTR(err);
Aviad Yehezkelc6475a02018-03-28 09:27:50 +03004060
4061 flags = mlx5_ib_flow_action_flags_to_accel_xfrm_flags(action_flags);
4062
4063 /* We current only support a subset of the standard features. Only a
4064 * keymat of type AES_GCM, with icv_len == 16, iv_algo == SEQ and esn
4065 * (with overlap). Full offload mode isn't supported.
4066 */
4067 if (!attr->keymat || attr->replay || attr->encap ||
4068 attr->spi || attr->seq || attr->tfc_pad ||
4069 attr->hard_limit_pkts ||
4070 (attr->flags & ~(IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED |
4071 IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ENCRYPT)))
4072 return ERR_PTR(-EOPNOTSUPP);
4073
4074 if (attr->keymat->protocol !=
4075 IB_UVERBS_FLOW_ACTION_ESP_KEYMAT_AES_GCM)
4076 return ERR_PTR(-EOPNOTSUPP);
4077
4078 aes_gcm = &attr->keymat->keymat.aes_gcm;
4079
4080 if (aes_gcm->icv_len != 16 ||
4081 aes_gcm->iv_algo != IB_UVERBS_FLOW_ACTION_IV_ALGO_SEQ)
4082 return ERR_PTR(-EOPNOTSUPP);
4083
4084 action = kmalloc(sizeof(*action), GFP_KERNEL);
4085 if (!action)
4086 return ERR_PTR(-ENOMEM);
4087
4088 action->esp_aes_gcm.ib_flags = attr->flags;
4089 memcpy(&accel_attrs.keymat.aes_gcm.aes_key, &aes_gcm->aes_key,
4090 sizeof(accel_attrs.keymat.aes_gcm.aes_key));
4091 accel_attrs.keymat.aes_gcm.key_len = aes_gcm->key_len * 8;
4092 memcpy(&accel_attrs.keymat.aes_gcm.salt, &aes_gcm->salt,
4093 sizeof(accel_attrs.keymat.aes_gcm.salt));
4094 memcpy(&accel_attrs.keymat.aes_gcm.seq_iv, &aes_gcm->iv,
4095 sizeof(accel_attrs.keymat.aes_gcm.seq_iv));
4096 accel_attrs.keymat.aes_gcm.icv_len = aes_gcm->icv_len * 8;
4097 accel_attrs.keymat.aes_gcm.iv_algo = MLX5_ACCEL_ESP_AES_GCM_IV_ALGO_SEQ;
4098 accel_attrs.keymat_type = MLX5_ACCEL_ESP_KEYMAT_AES_GCM;
4099
4100 accel_attrs.esn = attr->esn;
4101 if (attr->flags & IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED)
4102 accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_TRIGGERED;
4103 if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW)
4104 accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP;
4105
4106 if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ENCRYPT)
4107 accel_attrs.action |= MLX5_ACCEL_ESP_ACTION_ENCRYPT;
4108
4109 action->esp_aes_gcm.ctx =
4110 mlx5_accel_esp_create_xfrm(mdev->mdev, &accel_attrs, flags);
4111 if (IS_ERR(action->esp_aes_gcm.ctx)) {
4112 err = PTR_ERR(action->esp_aes_gcm.ctx);
4113 goto err_parse;
4114 }
4115
4116 action->esp_aes_gcm.ib_flags = attr->flags;
4117
4118 return &action->ib_action;
4119
4120err_parse:
4121 kfree(action);
4122 return ERR_PTR(err);
4123}
4124
Matan Barak349705c2018-03-28 09:27:51 +03004125static int
4126mlx5_ib_modify_flow_action_esp(struct ib_flow_action *action,
4127 const struct ib_flow_action_attrs_esp *attr,
4128 struct uverbs_attr_bundle *attrs)
4129{
4130 struct mlx5_ib_flow_action *maction = to_mflow_act(action);
4131 struct mlx5_accel_esp_xfrm_attrs accel_attrs;
4132 int err = 0;
4133
4134 if (attr->keymat || attr->replay || attr->encap ||
4135 attr->spi || attr->seq || attr->tfc_pad ||
4136 attr->hard_limit_pkts ||
4137 (attr->flags & ~(IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED |
4138 IB_FLOW_ACTION_ESP_FLAGS_MOD_ESP_ATTRS |
4139 IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW)))
4140 return -EOPNOTSUPP;
4141
4142 /* Only the ESN value or the MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP can
4143 * be modified.
4144 */
4145 if (!(maction->esp_aes_gcm.ib_flags &
4146 IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED) &&
4147 attr->flags & (IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED |
4148 IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW))
4149 return -EINVAL;
4150
4151 memcpy(&accel_attrs, &maction->esp_aes_gcm.ctx->attrs,
4152 sizeof(accel_attrs));
4153
4154 accel_attrs.esn = attr->esn;
4155 if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW)
4156 accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP;
4157 else
4158 accel_attrs.flags &= ~MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP;
4159
4160 err = mlx5_accel_esp_modify_xfrm(maction->esp_aes_gcm.ctx,
4161 &accel_attrs);
4162 if (err)
4163 return err;
4164
4165 maction->esp_aes_gcm.ib_flags &=
4166 ~IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW;
4167 maction->esp_aes_gcm.ib_flags |=
4168 attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW;
4169
4170 return 0;
4171}
4172
Aviad Yehezkelc6475a02018-03-28 09:27:50 +03004173static int mlx5_ib_destroy_flow_action(struct ib_flow_action *action)
4174{
4175 struct mlx5_ib_flow_action *maction = to_mflow_act(action);
4176
4177 switch (action->type) {
4178 case IB_FLOW_ACTION_ESP:
4179 /*
4180 * We only support aes_gcm by now, so we implicitly know this is
4181 * the underline crypto.
4182 */
4183 mlx5_accel_esp_destroy_xfrm(maction->esp_aes_gcm.ctx);
4184 break;
Mark Blochb4749bf2018-08-28 14:18:51 +03004185 case IB_FLOW_ACTION_UNSPECIFIED:
4186 mlx5_ib_destroy_flow_action_raw(maction);
4187 break;
Aviad Yehezkelc6475a02018-03-28 09:27:50 +03004188 default:
4189 WARN_ON(true);
4190 break;
4191 }
4192
4193 kfree(maction);
4194 return 0;
4195}
4196
Eli Cohene126ba92013-07-07 17:25:49 +03004197static int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
4198{
4199 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
Yishai Hadas81e30882017-06-08 16:15:09 +03004200 struct mlx5_ib_qp *mqp = to_mqp(ibqp);
Eli Cohene126ba92013-07-07 17:25:49 +03004201 int err;
Yishai Hadas539ec982018-09-20 21:39:25 +03004202 u16 uid;
4203
4204 uid = ibqp->pd ?
4205 to_mpd(ibqp->pd)->uid : 0;
Eli Cohene126ba92013-07-07 17:25:49 +03004206
Yishai Hadas81e30882017-06-08 16:15:09 +03004207 if (mqp->flags & MLX5_IB_QP_UNDERLAY) {
4208 mlx5_ib_dbg(dev, "Attaching a multi cast group to underlay QP is not supported\n");
4209 return -EOPNOTSUPP;
4210 }
4211
Yishai Hadas539ec982018-09-20 21:39:25 +03004212 err = mlx5_cmd_attach_mcg(dev->mdev, gid, ibqp->qp_num, uid);
Eli Cohene126ba92013-07-07 17:25:49 +03004213 if (err)
4214 mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n",
4215 ibqp->qp_num, gid->raw);
4216
4217 return err;
4218}
4219
4220static int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
4221{
4222 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
4223 int err;
Yishai Hadas539ec982018-09-20 21:39:25 +03004224 u16 uid;
Eli Cohene126ba92013-07-07 17:25:49 +03004225
Yishai Hadas539ec982018-09-20 21:39:25 +03004226 uid = ibqp->pd ?
4227 to_mpd(ibqp->pd)->uid : 0;
4228 err = mlx5_cmd_detach_mcg(dev->mdev, gid, ibqp->qp_num, uid);
Eli Cohene126ba92013-07-07 17:25:49 +03004229 if (err)
4230 mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n",
4231 ibqp->qp_num, gid->raw);
4232
4233 return err;
4234}
4235
4236static int init_node_data(struct mlx5_ib_dev *dev)
4237{
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03004238 int err;
Eli Cohene126ba92013-07-07 17:25:49 +03004239
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03004240 err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc);
Eli Cohene126ba92013-07-07 17:25:49 +03004241 if (err)
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03004242 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03004243
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03004244 dev->mdev->rev_id = dev->mdev->pdev->revision;
Eli Cohene126ba92013-07-07 17:25:49 +03004245
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03004246 return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid);
Eli Cohene126ba92013-07-07 17:25:49 +03004247}
4248
Parav Pandit508a5232018-10-11 22:31:54 +03004249static ssize_t fw_pages_show(struct device *device,
4250 struct device_attribute *attr, char *buf)
Eli Cohene126ba92013-07-07 17:25:49 +03004251{
4252 struct mlx5_ib_dev *dev =
Parav Pandit54747232018-12-18 14:15:56 +02004253 rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
Eli Cohene126ba92013-07-07 17:25:49 +03004254
Jack Morgenstein9603b612014-07-28 23:30:22 +03004255 return sprintf(buf, "%d\n", dev->mdev->priv.fw_pages);
Eli Cohene126ba92013-07-07 17:25:49 +03004256}
Parav Pandit508a5232018-10-11 22:31:54 +03004257static DEVICE_ATTR_RO(fw_pages);
Eli Cohene126ba92013-07-07 17:25:49 +03004258
Parav Pandit508a5232018-10-11 22:31:54 +03004259static ssize_t reg_pages_show(struct device *device,
Eli Cohene126ba92013-07-07 17:25:49 +03004260 struct device_attribute *attr, char *buf)
4261{
4262 struct mlx5_ib_dev *dev =
Parav Pandit54747232018-12-18 14:15:56 +02004263 rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
Eli Cohene126ba92013-07-07 17:25:49 +03004264
Haggai Eran6aec21f2014-12-11 17:04:23 +02004265 return sprintf(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages));
Eli Cohene126ba92013-07-07 17:25:49 +03004266}
Parav Pandit508a5232018-10-11 22:31:54 +03004267static DEVICE_ATTR_RO(reg_pages);
Eli Cohene126ba92013-07-07 17:25:49 +03004268
Parav Pandit508a5232018-10-11 22:31:54 +03004269static ssize_t hca_type_show(struct device *device,
4270 struct device_attribute *attr, char *buf)
Eli Cohene126ba92013-07-07 17:25:49 +03004271{
4272 struct mlx5_ib_dev *dev =
Parav Pandit54747232018-12-18 14:15:56 +02004273 rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
4274
Jack Morgenstein9603b612014-07-28 23:30:22 +03004275 return sprintf(buf, "MT%d\n", dev->mdev->pdev->device);
Eli Cohene126ba92013-07-07 17:25:49 +03004276}
Parav Pandit508a5232018-10-11 22:31:54 +03004277static DEVICE_ATTR_RO(hca_type);
Eli Cohene126ba92013-07-07 17:25:49 +03004278
Parav Pandit508a5232018-10-11 22:31:54 +03004279static ssize_t hw_rev_show(struct device *device,
4280 struct device_attribute *attr, char *buf)
Eli Cohene126ba92013-07-07 17:25:49 +03004281{
4282 struct mlx5_ib_dev *dev =
Parav Pandit54747232018-12-18 14:15:56 +02004283 rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
4284
Jack Morgenstein9603b612014-07-28 23:30:22 +03004285 return sprintf(buf, "%x\n", dev->mdev->rev_id);
Eli Cohene126ba92013-07-07 17:25:49 +03004286}
Parav Pandit508a5232018-10-11 22:31:54 +03004287static DEVICE_ATTR_RO(hw_rev);
Eli Cohene126ba92013-07-07 17:25:49 +03004288
Parav Pandit508a5232018-10-11 22:31:54 +03004289static ssize_t board_id_show(struct device *device,
4290 struct device_attribute *attr, char *buf)
Eli Cohene126ba92013-07-07 17:25:49 +03004291{
4292 struct mlx5_ib_dev *dev =
Parav Pandit54747232018-12-18 14:15:56 +02004293 rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
4294
Eli Cohene126ba92013-07-07 17:25:49 +03004295 return sprintf(buf, "%.*s\n", MLX5_BOARD_ID_LEN,
Jack Morgenstein9603b612014-07-28 23:30:22 +03004296 dev->mdev->board_id);
Eli Cohene126ba92013-07-07 17:25:49 +03004297}
Parav Pandit508a5232018-10-11 22:31:54 +03004298static DEVICE_ATTR_RO(board_id);
Eli Cohene126ba92013-07-07 17:25:49 +03004299
Parav Pandit508a5232018-10-11 22:31:54 +03004300static struct attribute *mlx5_class_attributes[] = {
4301 &dev_attr_hw_rev.attr,
4302 &dev_attr_hca_type.attr,
4303 &dev_attr_board_id.attr,
4304 &dev_attr_fw_pages.attr,
4305 &dev_attr_reg_pages.attr,
4306 NULL,
4307};
Eli Cohene126ba92013-07-07 17:25:49 +03004308
Parav Pandit508a5232018-10-11 22:31:54 +03004309static const struct attribute_group mlx5_attr_group = {
4310 .attrs = mlx5_class_attributes,
Eli Cohene126ba92013-07-07 17:25:49 +03004311};
4312
Haggai Eran7722f472016-02-29 15:45:07 +02004313static void pkey_change_handler(struct work_struct *work)
4314{
4315 struct mlx5_ib_port_resources *ports =
4316 container_of(work, struct mlx5_ib_port_resources,
4317 pkey_change_work);
4318
4319 mutex_lock(&ports->devr->mutex);
4320 mlx5_ib_gsi_pkey_change(ports->gsi);
4321 mutex_unlock(&ports->devr->mutex);
4322}
4323
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03004324static void mlx5_ib_handle_internal_error(struct mlx5_ib_dev *ibdev)
4325{
4326 struct mlx5_ib_qp *mqp;
4327 struct mlx5_ib_cq *send_mcq, *recv_mcq;
4328 struct mlx5_core_cq *mcq;
4329 struct list_head cq_armed_list;
4330 unsigned long flags_qp;
4331 unsigned long flags_cq;
4332 unsigned long flags;
4333
4334 INIT_LIST_HEAD(&cq_armed_list);
4335
4336 /* Go over qp list reside on that ibdev, sync with create/destroy qp.*/
4337 spin_lock_irqsave(&ibdev->reset_flow_resource_lock, flags);
4338 list_for_each_entry(mqp, &ibdev->qp_list, qps_list) {
4339 spin_lock_irqsave(&mqp->sq.lock, flags_qp);
4340 if (mqp->sq.tail != mqp->sq.head) {
4341 send_mcq = to_mcq(mqp->ibqp.send_cq);
4342 spin_lock_irqsave(&send_mcq->lock, flags_cq);
4343 if (send_mcq->mcq.comp &&
4344 mqp->ibqp.send_cq->comp_handler) {
4345 if (!send_mcq->mcq.reset_notify_added) {
4346 send_mcq->mcq.reset_notify_added = 1;
4347 list_add_tail(&send_mcq->mcq.reset_notify,
4348 &cq_armed_list);
4349 }
4350 }
4351 spin_unlock_irqrestore(&send_mcq->lock, flags_cq);
4352 }
4353 spin_unlock_irqrestore(&mqp->sq.lock, flags_qp);
4354 spin_lock_irqsave(&mqp->rq.lock, flags_qp);
4355 /* no handling is needed for SRQ */
4356 if (!mqp->ibqp.srq) {
4357 if (mqp->rq.tail != mqp->rq.head) {
4358 recv_mcq = to_mcq(mqp->ibqp.recv_cq);
4359 spin_lock_irqsave(&recv_mcq->lock, flags_cq);
4360 if (recv_mcq->mcq.comp &&
4361 mqp->ibqp.recv_cq->comp_handler) {
4362 if (!recv_mcq->mcq.reset_notify_added) {
4363 recv_mcq->mcq.reset_notify_added = 1;
4364 list_add_tail(&recv_mcq->mcq.reset_notify,
4365 &cq_armed_list);
4366 }
4367 }
4368 spin_unlock_irqrestore(&recv_mcq->lock,
4369 flags_cq);
4370 }
4371 }
4372 spin_unlock_irqrestore(&mqp->rq.lock, flags_qp);
4373 }
4374 /*At that point all inflight post send were put to be executed as of we
4375 * lock/unlock above locks Now need to arm all involved CQs.
4376 */
4377 list_for_each_entry(mcq, &cq_armed_list, reset_notify) {
4378 mcq->comp(mcq);
4379 }
4380 spin_unlock_irqrestore(&ibdev->reset_flow_resource_lock, flags);
4381}
4382
Maor Gottlieb03404e82017-05-30 10:29:13 +03004383static void delay_drop_handler(struct work_struct *work)
4384{
4385 int err;
4386 struct mlx5_ib_delay_drop *delay_drop =
4387 container_of(work, struct mlx5_ib_delay_drop,
4388 delay_drop_work);
4389
Maor Gottliebfe248c32017-05-30 10:29:14 +03004390 atomic_inc(&delay_drop->events_cnt);
4391
Maor Gottlieb03404e82017-05-30 10:29:13 +03004392 mutex_lock(&delay_drop->lock);
4393 err = mlx5_core_set_delay_drop(delay_drop->dev->mdev,
4394 delay_drop->timeout);
4395 if (err) {
4396 mlx5_ib_warn(delay_drop->dev, "Failed to set delay drop, timeout=%u\n",
4397 delay_drop->timeout);
4398 delay_drop->activate = false;
4399 }
4400 mutex_unlock(&delay_drop->lock);
4401}
4402
Saeed Mahameed09e574f2018-11-26 14:39:04 -08004403static void handle_general_event(struct mlx5_ib_dev *ibdev, struct mlx5_eqe *eqe,
4404 struct ib_event *ibev)
4405{
4406 switch (eqe->sub_type) {
4407 case MLX5_GENERAL_SUBTYPE_DELAY_DROP_TIMEOUT:
4408 schedule_work(&ibdev->delay_drop.delay_drop_work);
4409 break;
4410 default: /* do nothing */
4411 return;
4412 }
4413}
4414
Saeed Mahameed134e9342018-11-26 14:39:02 -08004415static int handle_port_change(struct mlx5_ib_dev *ibdev, struct mlx5_eqe *eqe,
4416 struct ib_event *ibev)
4417{
4418 u8 port = (eqe->data.port.port >> 4) & 0xf;
4419
4420 ibev->element.port_num = port;
4421
4422 switch (eqe->sub_type) {
4423 case MLX5_PORT_CHANGE_SUBTYPE_ACTIVE:
4424 case MLX5_PORT_CHANGE_SUBTYPE_DOWN:
4425 case MLX5_PORT_CHANGE_SUBTYPE_INITIALIZED:
4426 /* In RoCE, port up/down events are handled in
4427 * mlx5_netdev_event().
4428 */
4429 if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) ==
4430 IB_LINK_LAYER_ETHERNET)
4431 return -EINVAL;
4432
4433 ibev->event = (eqe->sub_type == MLX5_PORT_CHANGE_SUBTYPE_ACTIVE) ?
4434 IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
4435 break;
4436
4437 case MLX5_PORT_CHANGE_SUBTYPE_LID:
4438 ibev->event = IB_EVENT_LID_CHANGE;
4439 break;
4440
4441 case MLX5_PORT_CHANGE_SUBTYPE_PKEY:
4442 ibev->event = IB_EVENT_PKEY_CHANGE;
4443 schedule_work(&ibdev->devr.ports[port - 1].pkey_change_work);
4444 break;
4445
4446 case MLX5_PORT_CHANGE_SUBTYPE_GUID:
4447 ibev->event = IB_EVENT_GID_CHANGE;
4448 break;
4449
4450 case MLX5_PORT_CHANGE_SUBTYPE_CLIENT_REREG:
4451 ibev->event = IB_EVENT_CLIENT_REREGISTER;
4452 break;
4453 default:
4454 return -EINVAL;
4455 }
4456
4457 return 0;
4458}
4459
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02004460static void mlx5_ib_handle_event(struct work_struct *_work)
Eli Cohene126ba92013-07-07 17:25:49 +03004461{
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02004462 struct mlx5_ib_event_work *work =
4463 container_of(_work, struct mlx5_ib_event_work, work);
4464 struct mlx5_ib_dev *ibdev;
Eli Cohene126ba92013-07-07 17:25:49 +03004465 struct ib_event ibev;
Eli Cohendbaaff22016-10-27 16:36:44 +03004466 bool fatal = false;
Eli Cohene126ba92013-07-07 17:25:49 +03004467
Saeed Mahameeddf097a22018-11-26 14:39:00 -08004468 if (work->is_slave) {
4469 ibdev = mlx5_ib_get_ibdev_from_mpi(work->mpi);
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02004470 if (!ibdev)
4471 goto out;
4472 } else {
Saeed Mahameeddf097a22018-11-26 14:39:00 -08004473 ibdev = work->dev;
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02004474 }
4475
4476 switch (work->event) {
Eli Cohene126ba92013-07-07 17:25:49 +03004477 case MLX5_DEV_EVENT_SYS_ERROR:
Eli Cohene126ba92013-07-07 17:25:49 +03004478 ibev.event = IB_EVENT_DEVICE_FATAL;
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03004479 mlx5_ib_handle_internal_error(ibdev);
Saeed Mahameed134e9342018-11-26 14:39:02 -08004480 ibev.element.port_num = (u8)(unsigned long)work->param;
Eli Cohendbaaff22016-10-27 16:36:44 +03004481 fatal = true;
Eli Cohene126ba92013-07-07 17:25:49 +03004482 break;
Saeed Mahameed134e9342018-11-26 14:39:02 -08004483 case MLX5_EVENT_TYPE_PORT_CHANGE:
4484 if (handle_port_change(ibdev, work->param, &ibev))
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02004485 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03004486 break;
Saeed Mahameed09e574f2018-11-26 14:39:04 -08004487 case MLX5_EVENT_TYPE_GENERAL_EVENT:
4488 handle_general_event(ibdev, work->param, &ibev);
4489 /* fall through */
Saeed Mahameedbdc37922016-09-29 19:35:38 +03004490 default:
Maor Gottlieb03404e82017-05-30 10:29:13 +03004491 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03004492 }
4493
Saeed Mahameed134e9342018-11-26 14:39:02 -08004494 ibev.device = &ibdev->ib_dev;
Eli Cohene126ba92013-07-07 17:25:49 +03004495
Saeed Mahameed134e9342018-11-26 14:39:02 -08004496 if (!rdma_is_port_valid(&ibdev->ib_dev, ibev.element.port_num)) {
4497 mlx5_ib_warn(ibdev, "warning: event on port %d\n", ibev.element.port_num);
Maor Gottlieb03404e82017-05-30 10:29:13 +03004498 goto out;
Eli Cohena0c84c32013-09-11 16:35:27 +03004499 }
4500
Eli Cohene126ba92013-07-07 17:25:49 +03004501 if (ibdev->ib_active)
4502 ib_dispatch_event(&ibev);
Eli Cohendbaaff22016-10-27 16:36:44 +03004503
4504 if (fatal)
4505 ibdev->ib_active = false;
Maor Gottlieb03404e82017-05-30 10:29:13 +03004506out:
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02004507 kfree(work);
4508}
4509
Saeed Mahameeddf097a22018-11-26 14:39:00 -08004510static int mlx5_ib_event(struct notifier_block *nb,
4511 unsigned long event, void *param)
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02004512{
4513 struct mlx5_ib_event_work *work;
4514
4515 work = kmalloc(sizeof(*work), GFP_ATOMIC);
Leon Romanovsky10bea9c2018-01-19 13:07:11 +02004516 if (!work)
Saeed Mahameeddf097a22018-11-26 14:39:00 -08004517 return NOTIFY_DONE;
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02004518
Leon Romanovsky10bea9c2018-01-19 13:07:11 +02004519 INIT_WORK(&work->work, mlx5_ib_handle_event);
Saeed Mahameeddf097a22018-11-26 14:39:00 -08004520 work->dev = container_of(nb, struct mlx5_ib_dev, mdev_events);
4521 work->is_slave = false;
Leon Romanovsky10bea9c2018-01-19 13:07:11 +02004522 work->param = param;
Leon Romanovsky10bea9c2018-01-19 13:07:11 +02004523 work->event = event;
4524
4525 queue_work(mlx5_ib_event_wq, &work->work);
Saeed Mahameeddf097a22018-11-26 14:39:00 -08004526
4527 return NOTIFY_OK;
4528}
4529
4530static int mlx5_ib_event_slave_port(struct notifier_block *nb,
4531 unsigned long event, void *param)
4532{
4533 struct mlx5_ib_event_work *work;
4534
4535 work = kmalloc(sizeof(*work), GFP_ATOMIC);
4536 if (!work)
4537 return NOTIFY_DONE;
4538
4539 INIT_WORK(&work->work, mlx5_ib_handle_event);
4540 work->mpi = container_of(nb, struct mlx5_ib_multiport_info, mdev_events);
4541 work->is_slave = true;
4542 work->param = param;
4543 work->event = event;
4544 queue_work(mlx5_ib_event_wq, &work->work);
4545
4546 return NOTIFY_OK;
Eli Cohene126ba92013-07-07 17:25:49 +03004547}
4548
Maor Gottliebc43f1112017-01-18 14:10:33 +02004549static int set_has_smi_cap(struct mlx5_ib_dev *dev)
4550{
4551 struct mlx5_hca_vport_context vport_ctx;
4552 int err;
4553 int port;
4554
Mark Blocha989ea02019-03-28 15:27:40 +02004555 for (port = 1; port <= ARRAY_SIZE(dev->mdev->port_caps); port++) {
Maor Gottliebc43f1112017-01-18 14:10:33 +02004556 dev->mdev->port_caps[port - 1].has_smi = false;
4557 if (MLX5_CAP_GEN(dev->mdev, port_type) ==
4558 MLX5_CAP_PORT_TYPE_IB) {
4559 if (MLX5_CAP_GEN(dev->mdev, ib_virt)) {
4560 err = mlx5_query_hca_vport_context(dev->mdev, 0,
4561 port, 0,
4562 &vport_ctx);
4563 if (err) {
4564 mlx5_ib_err(dev, "query_hca_vport_context for port=%d failed %d\n",
4565 port, err);
4566 return err;
4567 }
4568 dev->mdev->port_caps[port - 1].has_smi =
4569 vport_ctx.has_smi;
4570 } else {
4571 dev->mdev->port_caps[port - 1].has_smi = true;
4572 }
4573 }
4574 }
4575 return 0;
4576}
4577
Eli Cohene126ba92013-07-07 17:25:49 +03004578static void get_ext_port_caps(struct mlx5_ib_dev *dev)
4579{
4580 int port;
4581
Daniel Jurgens508562d2018-01-04 17:25:34 +02004582 for (port = 1; port <= dev->num_ports; port++)
Eli Cohene126ba92013-07-07 17:25:49 +03004583 mlx5_query_ext_port_caps(dev, port);
4584}
4585
Mark Bloch26628e22019-03-28 15:27:41 +02004586static int __get_port_caps(struct mlx5_ib_dev *dev, u8 port)
Eli Cohene126ba92013-07-07 17:25:49 +03004587{
4588 struct ib_device_attr *dprops = NULL;
4589 struct ib_port_attr *pprops = NULL;
Dan Carpenterf614fc12015-01-12 11:56:58 +03004590 int err = -ENOMEM;
Matan Barak2528e332015-06-11 16:35:25 +03004591 struct ib_udata uhw = {.inlen = 0, .outlen = 0};
Eli Cohene126ba92013-07-07 17:25:49 +03004592
4593 pprops = kmalloc(sizeof(*pprops), GFP_KERNEL);
4594 if (!pprops)
4595 goto out;
4596
4597 dprops = kmalloc(sizeof(*dprops), GFP_KERNEL);
4598 if (!dprops)
4599 goto out;
4600
Matan Barak2528e332015-06-11 16:35:25 +03004601 err = mlx5_ib_query_device(&dev->ib_dev, dprops, &uhw);
Eli Cohene126ba92013-07-07 17:25:49 +03004602 if (err) {
4603 mlx5_ib_warn(dev, "query_device failed %d\n", err);
4604 goto out;
4605 }
4606
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004607 memset(pprops, 0, sizeof(*pprops));
4608 err = mlx5_ib_query_port(&dev->ib_dev, port, pprops);
4609 if (err) {
4610 mlx5_ib_warn(dev, "query_port %d failed %d\n",
4611 port, err);
4612 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03004613 }
4614
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004615 dev->mdev->port_caps[port - 1].pkey_table_len =
4616 dprops->max_pkeys;
4617 dev->mdev->port_caps[port - 1].gid_table_len =
4618 pprops->gid_tbl_len;
4619 mlx5_ib_dbg(dev, "port %d: pkey_table_len %d, gid_table_len %d\n",
4620 port, dprops->max_pkeys, pprops->gid_tbl_len);
4621
Eli Cohene126ba92013-07-07 17:25:49 +03004622out:
4623 kfree(pprops);
4624 kfree(dprops);
4625
4626 return err;
4627}
4628
Mark Bloch26628e22019-03-28 15:27:41 +02004629static int get_port_caps(struct mlx5_ib_dev *dev, u8 port)
4630{
4631 /* For representors use port 1, is this is the only native
4632 * port
4633 */
4634 if (dev->is_rep)
4635 return __get_port_caps(dev, 1);
4636 return __get_port_caps(dev, port);
4637}
4638
Eli Cohene126ba92013-07-07 17:25:49 +03004639static void destroy_umrc_res(struct mlx5_ib_dev *dev)
4640{
4641 int err;
4642
4643 err = mlx5_mr_cache_cleanup(dev);
4644 if (err)
4645 mlx5_ib_warn(dev, "mr cache cleanup failed\n");
4646
Mark Bloch32927e22018-03-20 15:45:37 +02004647 if (dev->umrc.qp)
Shamir Rabinovitchc4367a22019-03-31 19:10:05 +03004648 mlx5_ib_destroy_qp(dev->umrc.qp, NULL);
Mark Bloch32927e22018-03-20 15:45:37 +02004649 if (dev->umrc.cq)
4650 ib_free_cq(dev->umrc.cq);
4651 if (dev->umrc.pd)
4652 ib_dealloc_pd(dev->umrc.pd);
Eli Cohene126ba92013-07-07 17:25:49 +03004653}
4654
4655enum {
4656 MAX_UMR_WR = 128,
4657};
4658
4659static int create_umr_res(struct mlx5_ib_dev *dev)
4660{
4661 struct ib_qp_init_attr *init_attr = NULL;
4662 struct ib_qp_attr *attr = NULL;
4663 struct ib_pd *pd;
4664 struct ib_cq *cq;
4665 struct ib_qp *qp;
Eli Cohene126ba92013-07-07 17:25:49 +03004666 int ret;
4667
4668 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
4669 init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL);
4670 if (!attr || !init_attr) {
4671 ret = -ENOMEM;
4672 goto error_0;
4673 }
4674
Christoph Hellwiged082d32016-09-05 12:56:17 +02004675 pd = ib_alloc_pd(&dev->ib_dev, 0);
Eli Cohene126ba92013-07-07 17:25:49 +03004676 if (IS_ERR(pd)) {
4677 mlx5_ib_dbg(dev, "Couldn't create PD for sync UMR QP\n");
4678 ret = PTR_ERR(pd);
4679 goto error_0;
4680 }
4681
Christoph Hellwigadd08d72016-03-03 09:38:22 +01004682 cq = ib_alloc_cq(&dev->ib_dev, NULL, 128, 0, IB_POLL_SOFTIRQ);
Eli Cohene126ba92013-07-07 17:25:49 +03004683 if (IS_ERR(cq)) {
4684 mlx5_ib_dbg(dev, "Couldn't create CQ for sync UMR QP\n");
4685 ret = PTR_ERR(cq);
4686 goto error_2;
4687 }
Eli Cohene126ba92013-07-07 17:25:49 +03004688
4689 init_attr->send_cq = cq;
4690 init_attr->recv_cq = cq;
4691 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
4692 init_attr->cap.max_send_wr = MAX_UMR_WR;
4693 init_attr->cap.max_send_sge = 1;
4694 init_attr->qp_type = MLX5_IB_QPT_REG_UMR;
4695 init_attr->port_num = 1;
4696 qp = mlx5_ib_create_qp(pd, init_attr, NULL);
4697 if (IS_ERR(qp)) {
4698 mlx5_ib_dbg(dev, "Couldn't create sync UMR QP\n");
4699 ret = PTR_ERR(qp);
4700 goto error_3;
4701 }
4702 qp->device = &dev->ib_dev;
4703 qp->real_qp = qp;
4704 qp->uobject = NULL;
4705 qp->qp_type = MLX5_IB_QPT_REG_UMR;
Majd Dibbiny31fde032017-10-30 14:23:13 +02004706 qp->send_cq = init_attr->send_cq;
4707 qp->recv_cq = init_attr->recv_cq;
Eli Cohene126ba92013-07-07 17:25:49 +03004708
4709 attr->qp_state = IB_QPS_INIT;
4710 attr->port_num = 1;
4711 ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_PKEY_INDEX |
4712 IB_QP_PORT, NULL);
4713 if (ret) {
4714 mlx5_ib_dbg(dev, "Couldn't modify UMR QP\n");
4715 goto error_4;
4716 }
4717
4718 memset(attr, 0, sizeof(*attr));
4719 attr->qp_state = IB_QPS_RTR;
4720 attr->path_mtu = IB_MTU_256;
4721
4722 ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
4723 if (ret) {
4724 mlx5_ib_dbg(dev, "Couldn't modify umr QP to rtr\n");
4725 goto error_4;
4726 }
4727
4728 memset(attr, 0, sizeof(*attr));
4729 attr->qp_state = IB_QPS_RTS;
4730 ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
4731 if (ret) {
4732 mlx5_ib_dbg(dev, "Couldn't modify umr QP to rts\n");
4733 goto error_4;
4734 }
4735
4736 dev->umrc.qp = qp;
4737 dev->umrc.cq = cq;
Eli Cohene126ba92013-07-07 17:25:49 +03004738 dev->umrc.pd = pd;
4739
4740 sema_init(&dev->umrc.sem, MAX_UMR_WR);
4741 ret = mlx5_mr_cache_init(dev);
4742 if (ret) {
4743 mlx5_ib_warn(dev, "mr cache init failed %d\n", ret);
4744 goto error_4;
4745 }
4746
4747 kfree(attr);
4748 kfree(init_attr);
4749
4750 return 0;
4751
4752error_4:
Shamir Rabinovitchc4367a22019-03-31 19:10:05 +03004753 mlx5_ib_destroy_qp(qp, NULL);
Mark Bloch32927e22018-03-20 15:45:37 +02004754 dev->umrc.qp = NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03004755
4756error_3:
Christoph Hellwigadd08d72016-03-03 09:38:22 +01004757 ib_free_cq(cq);
Mark Bloch32927e22018-03-20 15:45:37 +02004758 dev->umrc.cq = NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03004759
4760error_2:
Eli Cohene126ba92013-07-07 17:25:49 +03004761 ib_dealloc_pd(pd);
Mark Bloch32927e22018-03-20 15:45:37 +02004762 dev->umrc.pd = NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03004763
4764error_0:
4765 kfree(attr);
4766 kfree(init_attr);
4767 return ret;
4768}
4769
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03004770static u8 mlx5_get_umr_fence(u8 umr_fence_cap)
4771{
4772 switch (umr_fence_cap) {
4773 case MLX5_CAP_UMR_FENCE_NONE:
4774 return MLX5_FENCE_MODE_NONE;
4775 case MLX5_CAP_UMR_FENCE_SMALL:
4776 return MLX5_FENCE_MODE_INITIATOR_SMALL;
4777 default:
4778 return MLX5_FENCE_MODE_STRONG_ORDERING;
4779 }
4780}
4781
Eli Cohene126ba92013-07-07 17:25:49 +03004782static int create_dev_resources(struct mlx5_ib_resources *devr)
4783{
4784 struct ib_srq_init_attr attr;
4785 struct mlx5_ib_dev *dev;
Leon Romanovsky21a428a2019-02-03 14:55:51 +02004786 struct ib_device *ibdev;
Matan Barakbcf4c1e2015-06-11 16:35:20 +03004787 struct ib_cq_init_attr cq_attr = {.cqe = 1};
Haggai Eran7722f472016-02-29 15:45:07 +02004788 int port;
Eli Cohene126ba92013-07-07 17:25:49 +03004789 int ret = 0;
4790
4791 dev = container_of(devr, struct mlx5_ib_dev, devr);
Leon Romanovsky21a428a2019-02-03 14:55:51 +02004792 ibdev = &dev->ib_dev;
Eli Cohene126ba92013-07-07 17:25:49 +03004793
Haggai Erand16e91d2016-02-29 15:45:05 +02004794 mutex_init(&devr->mutex);
4795
Leon Romanovsky21a428a2019-02-03 14:55:51 +02004796 devr->p0 = rdma_zalloc_drv_obj(ibdev, ib_pd);
4797 if (!devr->p0)
4798 return -ENOMEM;
4799
4800 devr->p0->device = ibdev;
Eli Cohene126ba92013-07-07 17:25:49 +03004801 devr->p0->uobject = NULL;
4802 atomic_set(&devr->p0->usecnt, 0);
4803
Shamir Rabinovitchff23dfa2019-03-31 19:10:07 +03004804 ret = mlx5_ib_alloc_pd(devr->p0, NULL);
Leon Romanovsky21a428a2019-02-03 14:55:51 +02004805 if (ret)
4806 goto error0;
4807
Shamir Rabinovitchff23dfa2019-03-31 19:10:07 +03004808 devr->c0 = mlx5_ib_create_cq(&dev->ib_dev, &cq_attr, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03004809 if (IS_ERR(devr->c0)) {
4810 ret = PTR_ERR(devr->c0);
4811 goto error1;
4812 }
4813 devr->c0->device = &dev->ib_dev;
4814 devr->c0->uobject = NULL;
4815 devr->c0->comp_handler = NULL;
4816 devr->c0->event_handler = NULL;
4817 devr->c0->cq_context = NULL;
4818 atomic_set(&devr->c0->usecnt, 0);
4819
Shamir Rabinovitchff23dfa2019-03-31 19:10:07 +03004820 devr->x0 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03004821 if (IS_ERR(devr->x0)) {
4822 ret = PTR_ERR(devr->x0);
4823 goto error2;
4824 }
4825 devr->x0->device = &dev->ib_dev;
4826 devr->x0->inode = NULL;
4827 atomic_set(&devr->x0->usecnt, 0);
4828 mutex_init(&devr->x0->tgt_qp_mutex);
4829 INIT_LIST_HEAD(&devr->x0->tgt_qp_list);
4830
Shamir Rabinovitchff23dfa2019-03-31 19:10:07 +03004831 devr->x1 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03004832 if (IS_ERR(devr->x1)) {
4833 ret = PTR_ERR(devr->x1);
4834 goto error3;
4835 }
4836 devr->x1->device = &dev->ib_dev;
4837 devr->x1->inode = NULL;
4838 atomic_set(&devr->x1->usecnt, 0);
4839 mutex_init(&devr->x1->tgt_qp_mutex);
4840 INIT_LIST_HEAD(&devr->x1->tgt_qp_list);
4841
4842 memset(&attr, 0, sizeof(attr));
4843 attr.attr.max_sge = 1;
4844 attr.attr.max_wr = 1;
4845 attr.srq_type = IB_SRQT_XRC;
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03004846 attr.ext.cq = devr->c0;
Eli Cohene126ba92013-07-07 17:25:49 +03004847 attr.ext.xrc.xrcd = devr->x0;
4848
Leon Romanovsky68e326d2019-04-03 16:42:43 +03004849 devr->s0 = rdma_zalloc_drv_obj(ibdev, ib_srq);
4850 if (!devr->s0) {
4851 ret = -ENOMEM;
Eli Cohene126ba92013-07-07 17:25:49 +03004852 goto error4;
4853 }
Leon Romanovsky68e326d2019-04-03 16:42:43 +03004854
Eli Cohene126ba92013-07-07 17:25:49 +03004855 devr->s0->device = &dev->ib_dev;
4856 devr->s0->pd = devr->p0;
Eli Cohene126ba92013-07-07 17:25:49 +03004857 devr->s0->srq_type = IB_SRQT_XRC;
4858 devr->s0->ext.xrc.xrcd = devr->x0;
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03004859 devr->s0->ext.cq = devr->c0;
Leon Romanovsky68e326d2019-04-03 16:42:43 +03004860 ret = mlx5_ib_create_srq(devr->s0, &attr, NULL);
4861 if (ret)
4862 goto err_create;
4863
Eli Cohene126ba92013-07-07 17:25:49 +03004864 atomic_inc(&devr->s0->ext.xrc.xrcd->usecnt);
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03004865 atomic_inc(&devr->s0->ext.cq->usecnt);
Eli Cohene126ba92013-07-07 17:25:49 +03004866 atomic_inc(&devr->p0->usecnt);
4867 atomic_set(&devr->s0->usecnt, 0);
4868
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03004869 memset(&attr, 0, sizeof(attr));
4870 attr.attr.max_sge = 1;
4871 attr.attr.max_wr = 1;
4872 attr.srq_type = IB_SRQT_BASIC;
Leon Romanovsky68e326d2019-04-03 16:42:43 +03004873 devr->s1 = rdma_zalloc_drv_obj(ibdev, ib_srq);
4874 if (!devr->s1) {
4875 ret = -ENOMEM;
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03004876 goto error5;
4877 }
Leon Romanovsky68e326d2019-04-03 16:42:43 +03004878
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03004879 devr->s1->device = &dev->ib_dev;
4880 devr->s1->pd = devr->p0;
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03004881 devr->s1->srq_type = IB_SRQT_BASIC;
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03004882 devr->s1->ext.cq = devr->c0;
Leon Romanovsky68e326d2019-04-03 16:42:43 +03004883
4884 ret = mlx5_ib_create_srq(devr->s1, &attr, NULL);
4885 if (ret)
4886 goto error6;
4887
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03004888 atomic_inc(&devr->p0->usecnt);
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03004889 atomic_set(&devr->s1->usecnt, 0);
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03004890
Haggai Eran7722f472016-02-29 15:45:07 +02004891 for (port = 0; port < ARRAY_SIZE(devr->ports); ++port) {
4892 INIT_WORK(&devr->ports[port].pkey_change_work,
4893 pkey_change_handler);
4894 devr->ports[port].devr = devr;
4895 }
4896
Eli Cohene126ba92013-07-07 17:25:49 +03004897 return 0;
4898
Leon Romanovsky68e326d2019-04-03 16:42:43 +03004899error6:
4900 kfree(devr->s1);
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03004901error5:
Shamir Rabinovitchc4367a22019-03-31 19:10:05 +03004902 mlx5_ib_destroy_srq(devr->s0, NULL);
Leon Romanovsky68e326d2019-04-03 16:42:43 +03004903err_create:
4904 kfree(devr->s0);
Eli Cohene126ba92013-07-07 17:25:49 +03004905error4:
Shamir Rabinovitchc4367a22019-03-31 19:10:05 +03004906 mlx5_ib_dealloc_xrcd(devr->x1, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03004907error3:
Shamir Rabinovitchc4367a22019-03-31 19:10:05 +03004908 mlx5_ib_dealloc_xrcd(devr->x0, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03004909error2:
Shamir Rabinovitchc4367a22019-03-31 19:10:05 +03004910 mlx5_ib_destroy_cq(devr->c0, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03004911error1:
Shamir Rabinovitchc4367a22019-03-31 19:10:05 +03004912 mlx5_ib_dealloc_pd(devr->p0, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03004913error0:
Leon Romanovsky21a428a2019-02-03 14:55:51 +02004914 kfree(devr->p0);
Eli Cohene126ba92013-07-07 17:25:49 +03004915 return ret;
4916}
4917
4918static void destroy_dev_resources(struct mlx5_ib_resources *devr)
4919{
Haggai Eran7722f472016-02-29 15:45:07 +02004920 int port;
4921
Shamir Rabinovitchc4367a22019-03-31 19:10:05 +03004922 mlx5_ib_destroy_srq(devr->s1, NULL);
Leon Romanovsky68e326d2019-04-03 16:42:43 +03004923 kfree(devr->s1);
Shamir Rabinovitchc4367a22019-03-31 19:10:05 +03004924 mlx5_ib_destroy_srq(devr->s0, NULL);
Leon Romanovsky68e326d2019-04-03 16:42:43 +03004925 kfree(devr->s0);
Shamir Rabinovitchc4367a22019-03-31 19:10:05 +03004926 mlx5_ib_dealloc_xrcd(devr->x0, NULL);
4927 mlx5_ib_dealloc_xrcd(devr->x1, NULL);
4928 mlx5_ib_destroy_cq(devr->c0, NULL);
4929 mlx5_ib_dealloc_pd(devr->p0, NULL);
Leon Romanovsky21a428a2019-02-03 14:55:51 +02004930 kfree(devr->p0);
Haggai Eran7722f472016-02-29 15:45:07 +02004931
4932 /* Make sure no change P_Key work items are still executing */
Mark Bloch5d8f6a02019-03-28 15:27:36 +02004933 for (port = 0; port < ARRAY_SIZE(devr->ports); ++port)
Haggai Eran7722f472016-02-29 15:45:07 +02004934 cancel_work_sync(&devr->ports[port].pkey_change_work);
Eli Cohene126ba92013-07-07 17:25:49 +03004935}
4936
Artemy Kovalyovb02289b2018-07-04 15:57:50 +03004937static u32 get_core_cap_flags(struct ib_device *ibdev,
4938 struct mlx5_hca_vport_context *rep)
Achiad Shochate53505a2015-12-23 18:47:25 +02004939{
4940 struct mlx5_ib_dev *dev = to_mdev(ibdev);
4941 enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, 1);
4942 u8 l3_type_cap = MLX5_CAP_ROCE(dev->mdev, l3_type);
4943 u8 roce_version_cap = MLX5_CAP_ROCE(dev->mdev, roce_version);
Daniel Jurgens85c7c012018-01-04 17:25:43 +02004944 bool raw_support = !mlx5_core_mp_enabled(dev->mdev);
Achiad Shochate53505a2015-12-23 18:47:25 +02004945 u32 ret = 0;
4946
Artemy Kovalyovb02289b2018-07-04 15:57:50 +03004947 if (rep->grh_required)
4948 ret |= RDMA_CORE_CAP_IB_GRH_REQUIRED;
4949
Achiad Shochate53505a2015-12-23 18:47:25 +02004950 if (ll == IB_LINK_LAYER_INFINIBAND)
Artemy Kovalyovb02289b2018-07-04 15:57:50 +03004951 return ret | RDMA_CORE_PORT_IBA_IB;
Achiad Shochate53505a2015-12-23 18:47:25 +02004952
Daniel Jurgens85c7c012018-01-04 17:25:43 +02004953 if (raw_support)
Artemy Kovalyovb02289b2018-07-04 15:57:50 +03004954 ret |= RDMA_CORE_PORT_RAW_PACKET;
Or Gerlitz72cd5712017-01-24 13:02:36 +02004955
Achiad Shochate53505a2015-12-23 18:47:25 +02004956 if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV4_CAP))
Or Gerlitz72cd5712017-01-24 13:02:36 +02004957 return ret;
Achiad Shochate53505a2015-12-23 18:47:25 +02004958
4959 if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV6_CAP))
Or Gerlitz72cd5712017-01-24 13:02:36 +02004960 return ret;
Achiad Shochate53505a2015-12-23 18:47:25 +02004961
4962 if (roce_version_cap & MLX5_ROCE_VERSION_1_CAP)
4963 ret |= RDMA_CORE_PORT_IBA_ROCE;
4964
4965 if (roce_version_cap & MLX5_ROCE_VERSION_2_CAP)
4966 ret |= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
4967
4968 return ret;
4969}
4970
Ira Weiny77386132015-05-13 20:02:58 -04004971static int mlx5_port_immutable(struct ib_device *ibdev, u8 port_num,
4972 struct ib_port_immutable *immutable)
4973{
4974 struct ib_port_attr attr;
Or Gerlitzca5b91d2016-11-27 16:51:36 +02004975 struct mlx5_ib_dev *dev = to_mdev(ibdev);
4976 enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, port_num);
Artemy Kovalyovb02289b2018-07-04 15:57:50 +03004977 struct mlx5_hca_vport_context rep = {0};
Ira Weiny77386132015-05-13 20:02:58 -04004978 int err;
4979
Or Gerlitzc4550c62017-01-24 13:02:39 +02004980 err = ib_query_port(ibdev, port_num, &attr);
Ira Weiny77386132015-05-13 20:02:58 -04004981 if (err)
4982 return err;
4983
Artemy Kovalyovb02289b2018-07-04 15:57:50 +03004984 if (ll == IB_LINK_LAYER_INFINIBAND) {
4985 err = mlx5_query_hca_vport_context(dev->mdev, 0, port_num, 0,
4986 &rep);
4987 if (err)
4988 return err;
4989 }
4990
Ira Weiny77386132015-05-13 20:02:58 -04004991 immutable->pkey_tbl_len = attr.pkey_tbl_len;
4992 immutable->gid_tbl_len = attr.gid_tbl_len;
Artemy Kovalyovb02289b2018-07-04 15:57:50 +03004993 immutable->core_cap_flags = get_core_cap_flags(ibdev, &rep);
Or Gerlitzca5b91d2016-11-27 16:51:36 +02004994 if ((ll == IB_LINK_LAYER_INFINIBAND) || MLX5_CAP_GEN(dev->mdev, roce))
4995 immutable->max_mad_size = IB_MGMT_MAD_SIZE;
Ira Weiny77386132015-05-13 20:02:58 -04004996
4997 return 0;
4998}
4999
Mark Bloch8e6efa32017-11-06 12:22:13 +00005000static int mlx5_port_rep_immutable(struct ib_device *ibdev, u8 port_num,
5001 struct ib_port_immutable *immutable)
5002{
5003 struct ib_port_attr attr;
5004 int err;
5005
5006 immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET;
5007
5008 err = ib_query_port(ibdev, port_num, &attr);
5009 if (err)
5010 return err;
5011
5012 immutable->pkey_tbl_len = attr.pkey_tbl_len;
5013 immutable->gid_tbl_len = attr.gid_tbl_len;
5014 immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET;
5015
5016 return 0;
5017}
5018
Leon Romanovsky9abb0d12017-06-27 16:49:53 +03005019static void get_dev_fw_str(struct ib_device *ibdev, char *str)
Ira Weinyc7342822016-06-15 02:22:01 -04005020{
5021 struct mlx5_ib_dev *dev =
5022 container_of(ibdev, struct mlx5_ib_dev, ib_dev);
Leon Romanovsky9abb0d12017-06-27 16:49:53 +03005023 snprintf(str, IB_FW_VERSION_NAME_MAX, "%d.%d.%04d",
5024 fw_rev_maj(dev->mdev), fw_rev_min(dev->mdev),
5025 fw_rev_sub(dev->mdev));
Ira Weinyc7342822016-06-15 02:22:01 -04005026}
5027
Or Gerlitz45f95ac2016-11-27 16:51:35 +02005028static int mlx5_eth_lag_init(struct mlx5_ib_dev *dev)
Aviv Heller9ef9c642016-09-18 20:48:01 +03005029{
5030 struct mlx5_core_dev *mdev = dev->mdev;
5031 struct mlx5_flow_namespace *ns = mlx5_get_flow_namespace(mdev,
5032 MLX5_FLOW_NAMESPACE_LAG);
5033 struct mlx5_flow_table *ft;
5034 int err;
5035
Aviv Heller7c34ec12018-08-23 13:47:53 +03005036 if (!ns || !mlx5_lag_is_roce(mdev))
Aviv Heller9ef9c642016-09-18 20:48:01 +03005037 return 0;
5038
5039 err = mlx5_cmd_create_vport_lag(mdev);
5040 if (err)
5041 return err;
5042
5043 ft = mlx5_create_lag_demux_flow_table(ns, 0, 0);
5044 if (IS_ERR(ft)) {
5045 err = PTR_ERR(ft);
5046 goto err_destroy_vport_lag;
5047 }
5048
Mark Bloch9a4ca382018-01-16 14:42:35 +00005049 dev->flow_db->lag_demux_ft = ft;
Aviv Heller7c34ec12018-08-23 13:47:53 +03005050 dev->lag_active = true;
Aviv Heller9ef9c642016-09-18 20:48:01 +03005051 return 0;
5052
5053err_destroy_vport_lag:
5054 mlx5_cmd_destroy_vport_lag(mdev);
5055 return err;
5056}
5057
Or Gerlitz45f95ac2016-11-27 16:51:35 +02005058static void mlx5_eth_lag_cleanup(struct mlx5_ib_dev *dev)
Aviv Heller9ef9c642016-09-18 20:48:01 +03005059{
5060 struct mlx5_core_dev *mdev = dev->mdev;
5061
Aviv Heller7c34ec12018-08-23 13:47:53 +03005062 if (dev->lag_active) {
5063 dev->lag_active = false;
5064
Mark Bloch9a4ca382018-01-16 14:42:35 +00005065 mlx5_destroy_flow_table(dev->flow_db->lag_demux_ft);
5066 dev->flow_db->lag_demux_ft = NULL;
Aviv Heller9ef9c642016-09-18 20:48:01 +03005067
5068 mlx5_cmd_destroy_vport_lag(mdev);
5069 }
5070}
5071
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +02005072static int mlx5_add_netdev_notifier(struct mlx5_ib_dev *dev, u8 port_num)
Achiad Shochatfc24fc52015-12-23 18:47:17 +02005073{
Achiad Shochate53505a2015-12-23 18:47:25 +02005074 int err;
5075
Mark Bloch95579e72019-03-28 15:27:33 +02005076 dev->port[port_num].roce.nb.notifier_call = mlx5_netdev_event;
5077 err = register_netdevice_notifier(&dev->port[port_num].roce.nb);
Aviv Heller5ec8c832016-09-18 20:48:00 +03005078 if (err) {
Mark Bloch95579e72019-03-28 15:27:33 +02005079 dev->port[port_num].roce.nb.notifier_call = NULL;
Achiad Shochate53505a2015-12-23 18:47:25 +02005080 return err;
Aviv Heller5ec8c832016-09-18 20:48:00 +03005081 }
Achiad Shochate53505a2015-12-23 18:47:25 +02005082
Or Gerlitzd012f5d2016-11-27 16:51:34 +02005083 return 0;
5084}
Achiad Shochate53505a2015-12-23 18:47:25 +02005085
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +02005086static void mlx5_remove_netdev_notifier(struct mlx5_ib_dev *dev, u8 port_num)
Eli Cohene126ba92013-07-07 17:25:49 +03005087{
Mark Bloch95579e72019-03-28 15:27:33 +02005088 if (dev->port[port_num].roce.nb.notifier_call) {
5089 unregister_netdevice_notifier(&dev->port[port_num].roce.nb);
5090 dev->port[port_num].roce.nb.notifier_call = NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03005091 }
5092}
5093
Leon Romanovskye3f1ed12018-07-08 12:55:43 +03005094static int mlx5_enable_eth(struct mlx5_ib_dev *dev)
Eli Cohene126ba92013-07-07 17:25:49 +03005095{
Eli Cohene126ba92013-07-07 17:25:49 +03005096 int err;
5097
Or Gerlitzca5b91d2016-11-27 16:51:36 +02005098 if (MLX5_CAP_GEN(dev->mdev, roce)) {
5099 err = mlx5_nic_vport_enable_roce(dev->mdev);
5100 if (err)
Mark Bloch8e6efa32017-11-06 12:22:13 +00005101 return err;
Or Gerlitzca5b91d2016-11-27 16:51:36 +02005102 }
Achiad Shochate53505a2015-12-23 18:47:25 +02005103
Or Gerlitz45f95ac2016-11-27 16:51:35 +02005104 err = mlx5_eth_lag_init(dev);
Aviv Heller9ef9c642016-09-18 20:48:01 +03005105 if (err)
5106 goto err_disable_roce;
5107
Achiad Shochate53505a2015-12-23 18:47:25 +02005108 return 0;
5109
Aviv Heller9ef9c642016-09-18 20:48:01 +03005110err_disable_roce:
Or Gerlitzca5b91d2016-11-27 16:51:36 +02005111 if (MLX5_CAP_GEN(dev->mdev, roce))
5112 mlx5_nic_vport_disable_roce(dev->mdev);
Aviv Heller9ef9c642016-09-18 20:48:01 +03005113
Achiad Shochate53505a2015-12-23 18:47:25 +02005114 return err;
Achiad Shochatfc24fc52015-12-23 18:47:17 +02005115}
5116
Or Gerlitz45f95ac2016-11-27 16:51:35 +02005117static void mlx5_disable_eth(struct mlx5_ib_dev *dev)
Achiad Shochatfc24fc52015-12-23 18:47:17 +02005118{
Or Gerlitz45f95ac2016-11-27 16:51:35 +02005119 mlx5_eth_lag_cleanup(dev);
Or Gerlitzca5b91d2016-11-27 16:51:36 +02005120 if (MLX5_CAP_GEN(dev->mdev, roce))
5121 mlx5_nic_vport_disable_roce(dev->mdev);
Achiad Shochatfc24fc52015-12-23 18:47:17 +02005122}
5123
Parav Pandite1f24a72017-04-16 07:29:29 +03005124struct mlx5_ib_counter {
Kamal Heib7c16f472017-01-18 15:25:09 +02005125 const char *name;
5126 size_t offset;
5127};
5128
5129#define INIT_Q_COUNTER(_name) \
5130 { .name = #_name, .offset = MLX5_BYTE_OFF(query_q_counter_out, _name)}
5131
Parav Pandite1f24a72017-04-16 07:29:29 +03005132static const struct mlx5_ib_counter basic_q_cnts[] = {
Kamal Heib7c16f472017-01-18 15:25:09 +02005133 INIT_Q_COUNTER(rx_write_requests),
5134 INIT_Q_COUNTER(rx_read_requests),
5135 INIT_Q_COUNTER(rx_atomic_requests),
5136 INIT_Q_COUNTER(out_of_buffer),
5137};
5138
Parav Pandite1f24a72017-04-16 07:29:29 +03005139static const struct mlx5_ib_counter out_of_seq_q_cnts[] = {
Kamal Heib7c16f472017-01-18 15:25:09 +02005140 INIT_Q_COUNTER(out_of_sequence),
5141};
5142
Parav Pandite1f24a72017-04-16 07:29:29 +03005143static const struct mlx5_ib_counter retrans_q_cnts[] = {
Kamal Heib7c16f472017-01-18 15:25:09 +02005144 INIT_Q_COUNTER(duplicate_request),
5145 INIT_Q_COUNTER(rnr_nak_retry_err),
5146 INIT_Q_COUNTER(packet_seq_err),
5147 INIT_Q_COUNTER(implied_nak_seq_err),
5148 INIT_Q_COUNTER(local_ack_timeout_err),
5149};
5150
Parav Pandite1f24a72017-04-16 07:29:29 +03005151#define INIT_CONG_COUNTER(_name) \
5152 { .name = #_name, .offset = \
5153 MLX5_BYTE_OFF(query_cong_statistics_out, _name ## _high)}
5154
5155static const struct mlx5_ib_counter cong_cnts[] = {
5156 INIT_CONG_COUNTER(rp_cnp_ignored),
5157 INIT_CONG_COUNTER(rp_cnp_handled),
5158 INIT_CONG_COUNTER(np_ecn_marked_roce_packets),
5159 INIT_CONG_COUNTER(np_cnp_sent),
5160};
5161
Parav Pandit58dcb602017-06-19 07:19:37 +03005162static const struct mlx5_ib_counter extended_err_cnts[] = {
5163 INIT_Q_COUNTER(resp_local_length_error),
5164 INIT_Q_COUNTER(resp_cqe_error),
5165 INIT_Q_COUNTER(req_cqe_error),
5166 INIT_Q_COUNTER(req_remote_invalid_request),
5167 INIT_Q_COUNTER(req_remote_access_errors),
5168 INIT_Q_COUNTER(resp_remote_access_errors),
5169 INIT_Q_COUNTER(resp_cqe_flush_error),
5170 INIT_Q_COUNTER(req_cqe_flush_error),
5171};
5172
Talat Batheesh9f876f32018-06-21 15:37:56 +03005173#define INIT_EXT_PPCNT_COUNTER(_name) \
5174 { .name = #_name, .offset = \
5175 MLX5_BYTE_OFF(ppcnt_reg, \
5176 counter_set.eth_extended_cntrs_grp_data_layout._name##_high)}
5177
5178static const struct mlx5_ib_counter ext_ppcnt_cnts[] = {
5179 INIT_EXT_PPCNT_COUNTER(rx_icrc_encapsulated),
5180};
5181
Parav Pandite1f24a72017-04-16 07:29:29 +03005182static void mlx5_ib_dealloc_counters(struct mlx5_ib_dev *dev)
Mark Bloch0837e862016-06-17 15:10:55 +03005183{
Daniel Jurgensaac44922018-01-04 17:25:40 +02005184 int i;
Mark Bloch0837e862016-06-17 15:10:55 +03005185
Kamal Heib7c16f472017-01-18 15:25:09 +02005186 for (i = 0; i < dev->num_ports; i++) {
Parav Pandit921c0f52018-07-08 13:40:30 +03005187 if (dev->port[i].cnts.set_id_valid)
Daniel Jurgensaac44922018-01-04 17:25:40 +02005188 mlx5_core_dealloc_q_counter(dev->mdev,
5189 dev->port[i].cnts.set_id);
Parav Pandite1f24a72017-04-16 07:29:29 +03005190 kfree(dev->port[i].cnts.names);
5191 kfree(dev->port[i].cnts.offsets);
Kamal Heib7c16f472017-01-18 15:25:09 +02005192 }
5193}
5194
Parav Pandite1f24a72017-04-16 07:29:29 +03005195static int __mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev,
5196 struct mlx5_ib_counters *cnts)
Kamal Heib7c16f472017-01-18 15:25:09 +02005197{
5198 u32 num_counters;
5199
5200 num_counters = ARRAY_SIZE(basic_q_cnts);
5201
5202 if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt))
5203 num_counters += ARRAY_SIZE(out_of_seq_q_cnts);
5204
5205 if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters))
5206 num_counters += ARRAY_SIZE(retrans_q_cnts);
Parav Pandit58dcb602017-06-19 07:19:37 +03005207
5208 if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters))
5209 num_counters += ARRAY_SIZE(extended_err_cnts);
5210
Parav Pandite1f24a72017-04-16 07:29:29 +03005211 cnts->num_q_counters = num_counters;
Kamal Heib7c16f472017-01-18 15:25:09 +02005212
Parav Pandite1f24a72017-04-16 07:29:29 +03005213 if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
5214 cnts->num_cong_counters = ARRAY_SIZE(cong_cnts);
5215 num_counters += ARRAY_SIZE(cong_cnts);
5216 }
Talat Batheesh9f876f32018-06-21 15:37:56 +03005217 if (MLX5_CAP_PCAM_FEATURE(dev->mdev, rx_icrc_encapsulated_counter)) {
5218 cnts->num_ext_ppcnt_counters = ARRAY_SIZE(ext_ppcnt_cnts);
5219 num_counters += ARRAY_SIZE(ext_ppcnt_cnts);
5220 }
Parav Pandite1f24a72017-04-16 07:29:29 +03005221 cnts->names = kcalloc(num_counters, sizeof(cnts->names), GFP_KERNEL);
5222 if (!cnts->names)
Kamal Heib7c16f472017-01-18 15:25:09 +02005223 return -ENOMEM;
5224
Parav Pandite1f24a72017-04-16 07:29:29 +03005225 cnts->offsets = kcalloc(num_counters,
5226 sizeof(cnts->offsets), GFP_KERNEL);
5227 if (!cnts->offsets)
Kamal Heib7c16f472017-01-18 15:25:09 +02005228 goto err_names;
5229
Kamal Heib7c16f472017-01-18 15:25:09 +02005230 return 0;
5231
5232err_names:
Parav Pandite1f24a72017-04-16 07:29:29 +03005233 kfree(cnts->names);
Daniel Jurgensaac44922018-01-04 17:25:40 +02005234 cnts->names = NULL;
Kamal Heib7c16f472017-01-18 15:25:09 +02005235 return -ENOMEM;
5236}
5237
Parav Pandite1f24a72017-04-16 07:29:29 +03005238static void mlx5_ib_fill_counters(struct mlx5_ib_dev *dev,
5239 const char **names,
5240 size_t *offsets)
Kamal Heib7c16f472017-01-18 15:25:09 +02005241{
5242 int i;
5243 int j = 0;
5244
5245 for (i = 0; i < ARRAY_SIZE(basic_q_cnts); i++, j++) {
5246 names[j] = basic_q_cnts[i].name;
5247 offsets[j] = basic_q_cnts[i].offset;
5248 }
5249
5250 if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt)) {
5251 for (i = 0; i < ARRAY_SIZE(out_of_seq_q_cnts); i++, j++) {
5252 names[j] = out_of_seq_q_cnts[i].name;
5253 offsets[j] = out_of_seq_q_cnts[i].offset;
5254 }
5255 }
5256
5257 if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters)) {
5258 for (i = 0; i < ARRAY_SIZE(retrans_q_cnts); i++, j++) {
5259 names[j] = retrans_q_cnts[i].name;
5260 offsets[j] = retrans_q_cnts[i].offset;
5261 }
5262 }
Parav Pandite1f24a72017-04-16 07:29:29 +03005263
Parav Pandit58dcb602017-06-19 07:19:37 +03005264 if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters)) {
5265 for (i = 0; i < ARRAY_SIZE(extended_err_cnts); i++, j++) {
5266 names[j] = extended_err_cnts[i].name;
5267 offsets[j] = extended_err_cnts[i].offset;
5268 }
5269 }
5270
Parav Pandite1f24a72017-04-16 07:29:29 +03005271 if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
5272 for (i = 0; i < ARRAY_SIZE(cong_cnts); i++, j++) {
5273 names[j] = cong_cnts[i].name;
5274 offsets[j] = cong_cnts[i].offset;
5275 }
5276 }
Talat Batheesh9f876f32018-06-21 15:37:56 +03005277
5278 if (MLX5_CAP_PCAM_FEATURE(dev->mdev, rx_icrc_encapsulated_counter)) {
5279 for (i = 0; i < ARRAY_SIZE(ext_ppcnt_cnts); i++, j++) {
5280 names[j] = ext_ppcnt_cnts[i].name;
5281 offsets[j] = ext_ppcnt_cnts[i].offset;
5282 }
5283 }
Mark Bloch0837e862016-06-17 15:10:55 +03005284}
5285
Parav Pandite1f24a72017-04-16 07:29:29 +03005286static int mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev)
Mark Bloch0837e862016-06-17 15:10:55 +03005287{
Daniel Jurgensaac44922018-01-04 17:25:40 +02005288 int err = 0;
Mark Bloch0837e862016-06-17 15:10:55 +03005289 int i;
Yishai Hadasaa74be62018-12-09 12:52:36 +02005290 bool is_shared;
5291
5292 is_shared = MLX5_CAP_GEN(dev->mdev, log_max_uctx) != 0;
Mark Bloch0837e862016-06-17 15:10:55 +03005293
5294 for (i = 0; i < dev->num_ports; i++) {
Daniel Jurgensaac44922018-01-04 17:25:40 +02005295 err = __mlx5_ib_alloc_counters(dev, &dev->port[i].cnts);
5296 if (err)
5297 goto err_alloc;
Kamal Heib7c16f472017-01-18 15:25:09 +02005298
Daniel Jurgensaac44922018-01-04 17:25:40 +02005299 mlx5_ib_fill_counters(dev, dev->port[i].cnts.names,
5300 dev->port[i].cnts.offsets);
5301
Yishai Hadasaa74be62018-12-09 12:52:36 +02005302 err = mlx5_cmd_alloc_q_counter(dev->mdev,
5303 &dev->port[i].cnts.set_id,
5304 is_shared ?
5305 MLX5_SHARED_RESOURCE_UID : 0);
Daniel Jurgensaac44922018-01-04 17:25:40 +02005306 if (err) {
Mark Bloch0837e862016-06-17 15:10:55 +03005307 mlx5_ib_warn(dev,
5308 "couldn't allocate queue counter for port %d, err %d\n",
Daniel Jurgensaac44922018-01-04 17:25:40 +02005309 i + 1, err);
5310 goto err_alloc;
Mark Bloch0837e862016-06-17 15:10:55 +03005311 }
Daniel Jurgensaac44922018-01-04 17:25:40 +02005312 dev->port[i].cnts.set_id_valid = true;
Mark Bloch0837e862016-06-17 15:10:55 +03005313 }
5314
5315 return 0;
5316
Daniel Jurgensaac44922018-01-04 17:25:40 +02005317err_alloc:
5318 mlx5_ib_dealloc_counters(dev);
5319 return err;
Mark Bloch0837e862016-06-17 15:10:55 +03005320}
5321
Mark Bloch0ad17a82016-06-17 15:10:56 +03005322static struct rdma_hw_stats *mlx5_ib_alloc_hw_stats(struct ib_device *ibdev,
5323 u8 port_num)
5324{
Kamal Heib7c16f472017-01-18 15:25:09 +02005325 struct mlx5_ib_dev *dev = to_mdev(ibdev);
5326 struct mlx5_ib_port *port = &dev->port[port_num - 1];
Mark Bloch0ad17a82016-06-17 15:10:56 +03005327
5328 /* We support only per port stats */
5329 if (port_num == 0)
5330 return NULL;
5331
Parav Pandite1f24a72017-04-16 07:29:29 +03005332 return rdma_alloc_hw_stats_struct(port->cnts.names,
5333 port->cnts.num_q_counters +
Talat Batheesh9f876f32018-06-21 15:37:56 +03005334 port->cnts.num_cong_counters +
5335 port->cnts.num_ext_ppcnt_counters,
Mark Bloch0ad17a82016-06-17 15:10:56 +03005336 RDMA_HW_STATS_DEFAULT_LIFESPAN);
5337}
5338
Daniel Jurgensaac44922018-01-04 17:25:40 +02005339static int mlx5_ib_query_q_counters(struct mlx5_core_dev *mdev,
Parav Pandite1f24a72017-04-16 07:29:29 +03005340 struct mlx5_ib_port *port,
5341 struct rdma_hw_stats *stats)
5342{
5343 int outlen = MLX5_ST_SZ_BYTES(query_q_counter_out);
5344 void *out;
5345 __be32 val;
5346 int ret, i;
5347
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03005348 out = kvzalloc(outlen, GFP_KERNEL);
Parav Pandite1f24a72017-04-16 07:29:29 +03005349 if (!out)
5350 return -ENOMEM;
5351
Daniel Jurgensaac44922018-01-04 17:25:40 +02005352 ret = mlx5_core_query_q_counter(mdev,
Parav Pandite1f24a72017-04-16 07:29:29 +03005353 port->cnts.set_id, 0,
5354 out, outlen);
5355 if (ret)
5356 goto free;
5357
5358 for (i = 0; i < port->cnts.num_q_counters; i++) {
5359 val = *(__be32 *)(out + port->cnts.offsets[i]);
5360 stats->value[i] = (u64)be32_to_cpu(val);
5361 }
5362
5363free:
5364 kvfree(out);
5365 return ret;
5366}
5367
Talat Batheesh9f876f32018-06-21 15:37:56 +03005368static int mlx5_ib_query_ext_ppcnt_counters(struct mlx5_ib_dev *dev,
5369 struct mlx5_ib_port *port,
5370 struct rdma_hw_stats *stats)
5371{
5372 int offset = port->cnts.num_q_counters + port->cnts.num_cong_counters;
5373 int sz = MLX5_ST_SZ_BYTES(ppcnt_reg);
5374 int ret, i;
5375 void *out;
5376
5377 out = kvzalloc(sz, GFP_KERNEL);
5378 if (!out)
5379 return -ENOMEM;
5380
5381 ret = mlx5_cmd_query_ext_ppcnt_counters(dev->mdev, out);
5382 if (ret)
5383 goto free;
5384
5385 for (i = 0; i < port->cnts.num_ext_ppcnt_counters; i++) {
5386 stats->value[i + offset] =
5387 be64_to_cpup((__be64 *)(out +
5388 port->cnts.offsets[i + offset]));
5389 }
5390
5391free:
5392 kvfree(out);
5393 return ret;
5394}
5395
Mark Bloch0ad17a82016-06-17 15:10:56 +03005396static int mlx5_ib_get_hw_stats(struct ib_device *ibdev,
5397 struct rdma_hw_stats *stats,
Kamal Heib7c16f472017-01-18 15:25:09 +02005398 u8 port_num, int index)
Mark Bloch0ad17a82016-06-17 15:10:56 +03005399{
5400 struct mlx5_ib_dev *dev = to_mdev(ibdev);
Kamal Heib7c16f472017-01-18 15:25:09 +02005401 struct mlx5_ib_port *port = &dev->port[port_num - 1];
Daniel Jurgensaac44922018-01-04 17:25:40 +02005402 struct mlx5_core_dev *mdev;
Parav Pandite1f24a72017-04-16 07:29:29 +03005403 int ret, num_counters;
Daniel Jurgensaac44922018-01-04 17:25:40 +02005404 u8 mdev_port_num;
Mark Bloch0ad17a82016-06-17 15:10:56 +03005405
Kamal Heib7c16f472017-01-18 15:25:09 +02005406 if (!stats)
Parav Pandite1f24a72017-04-16 07:29:29 +03005407 return -EINVAL;
Mark Bloch0ad17a82016-06-17 15:10:56 +03005408
Talat Batheesh9f876f32018-06-21 15:37:56 +03005409 num_counters = port->cnts.num_q_counters +
5410 port->cnts.num_cong_counters +
5411 port->cnts.num_ext_ppcnt_counters;
Daniel Jurgensaac44922018-01-04 17:25:40 +02005412
5413 /* q_counters are per IB device, query the master mdev */
5414 ret = mlx5_ib_query_q_counters(dev->mdev, port, stats);
Mark Bloch0ad17a82016-06-17 15:10:56 +03005415 if (ret)
Parav Pandite1f24a72017-04-16 07:29:29 +03005416 return ret;
Mark Bloch0ad17a82016-06-17 15:10:56 +03005417
Talat Batheesh9f876f32018-06-21 15:37:56 +03005418 if (MLX5_CAP_PCAM_FEATURE(dev->mdev, rx_icrc_encapsulated_counter)) {
5419 ret = mlx5_ib_query_ext_ppcnt_counters(dev, port, stats);
5420 if (ret)
5421 return ret;
5422 }
5423
Parav Pandite1f24a72017-04-16 07:29:29 +03005424 if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
Daniel Jurgensaac44922018-01-04 17:25:40 +02005425 mdev = mlx5_ib_get_native_port_mdev(dev, port_num,
5426 &mdev_port_num);
5427 if (!mdev) {
5428 /* If port is not affiliated yet, its in down state
5429 * which doesn't have any counters yet, so it would be
5430 * zero. So no need to read from the HCA.
5431 */
5432 goto done;
5433 }
Majd Dibbiny71a0ff62017-12-21 17:38:26 +02005434 ret = mlx5_lag_query_cong_counters(dev->mdev,
5435 stats->value +
5436 port->cnts.num_q_counters,
5437 port->cnts.num_cong_counters,
5438 port->cnts.offsets +
5439 port->cnts.num_q_counters);
Daniel Jurgensaac44922018-01-04 17:25:40 +02005440
5441 mlx5_ib_put_native_port_mdev(dev, port_num);
Parav Pandite1f24a72017-04-16 07:29:29 +03005442 if (ret)
5443 return ret;
Mark Bloch0ad17a82016-06-17 15:10:56 +03005444 }
Kamal Heib7c16f472017-01-18 15:25:09 +02005445
Daniel Jurgensaac44922018-01-04 17:25:40 +02005446done:
Parav Pandite1f24a72017-04-16 07:29:29 +03005447 return num_counters;
Mark Bloch0ad17a82016-06-17 15:10:56 +03005448}
5449
Denis Drozdovf6a8a192018-08-14 14:08:51 +03005450static int mlx5_ib_rn_get_params(struct ib_device *device, u8 port_num,
5451 enum rdma_netdev_t type,
5452 struct rdma_netdev_alloc_params *params)
Erez Shitrit693dfd52017-04-27 17:01:34 +03005453{
5454 if (type != RDMA_NETDEV_IPOIB)
Denis Drozdovf6a8a192018-08-14 14:08:51 +03005455 return -EOPNOTSUPP;
Erez Shitrit693dfd52017-04-27 17:01:34 +03005456
Denis Drozdovf6a8a192018-08-14 14:08:51 +03005457 return mlx5_rdma_rn_get_params(to_mdev(device)->mdev, device, params);
Erez Shitrit693dfd52017-04-27 17:01:34 +03005458}
5459
Maor Gottliebfe248c32017-05-30 10:29:14 +03005460static void delay_drop_debugfs_cleanup(struct mlx5_ib_dev *dev)
5461{
5462 if (!dev->delay_drop.dbg)
5463 return;
5464 debugfs_remove_recursive(dev->delay_drop.dbg->dir_debugfs);
5465 kfree(dev->delay_drop.dbg);
5466 dev->delay_drop.dbg = NULL;
5467}
5468
Maor Gottlieb03404e82017-05-30 10:29:13 +03005469static void cancel_delay_drop(struct mlx5_ib_dev *dev)
5470{
5471 if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
5472 return;
5473
5474 cancel_work_sync(&dev->delay_drop.delay_drop_work);
Maor Gottliebfe248c32017-05-30 10:29:14 +03005475 delay_drop_debugfs_cleanup(dev);
5476}
5477
5478static ssize_t delay_drop_timeout_read(struct file *filp, char __user *buf,
5479 size_t count, loff_t *pos)
5480{
5481 struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
5482 char lbuf[20];
5483 int len;
5484
5485 len = snprintf(lbuf, sizeof(lbuf), "%u\n", delay_drop->timeout);
5486 return simple_read_from_buffer(buf, count, pos, lbuf, len);
5487}
5488
5489static ssize_t delay_drop_timeout_write(struct file *filp, const char __user *buf,
5490 size_t count, loff_t *pos)
5491{
5492 struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
5493 u32 timeout;
5494 u32 var;
5495
5496 if (kstrtouint_from_user(buf, count, 0, &var))
5497 return -EFAULT;
5498
5499 timeout = min_t(u32, roundup(var, 100), MLX5_MAX_DELAY_DROP_TIMEOUT_MS *
5500 1000);
5501 if (timeout != var)
5502 mlx5_ib_dbg(delay_drop->dev, "Round delay drop timeout to %u usec\n",
5503 timeout);
5504
5505 delay_drop->timeout = timeout;
5506
5507 return count;
5508}
5509
5510static const struct file_operations fops_delay_drop_timeout = {
5511 .owner = THIS_MODULE,
5512 .open = simple_open,
5513 .write = delay_drop_timeout_write,
5514 .read = delay_drop_timeout_read,
5515};
5516
5517static int delay_drop_debugfs_init(struct mlx5_ib_dev *dev)
5518{
5519 struct mlx5_ib_dbg_delay_drop *dbg;
5520
5521 if (!mlx5_debugfs_root)
5522 return 0;
5523
5524 dbg = kzalloc(sizeof(*dbg), GFP_KERNEL);
5525 if (!dbg)
5526 return -ENOMEM;
5527
Sudip Mukherjeecbafad82017-09-18 12:28:48 +01005528 dev->delay_drop.dbg = dbg;
5529
Maor Gottliebfe248c32017-05-30 10:29:14 +03005530 dbg->dir_debugfs =
5531 debugfs_create_dir("delay_drop",
5532 dev->mdev->priv.dbg_root);
5533 if (!dbg->dir_debugfs)
Sudip Mukherjeecbafad82017-09-18 12:28:48 +01005534 goto out_debugfs;
Maor Gottliebfe248c32017-05-30 10:29:14 +03005535
5536 dbg->events_cnt_debugfs =
5537 debugfs_create_atomic_t("num_timeout_events", 0400,
5538 dbg->dir_debugfs,
5539 &dev->delay_drop.events_cnt);
5540 if (!dbg->events_cnt_debugfs)
5541 goto out_debugfs;
5542
5543 dbg->rqs_cnt_debugfs =
5544 debugfs_create_atomic_t("num_rqs", 0400,
5545 dbg->dir_debugfs,
5546 &dev->delay_drop.rqs_cnt);
5547 if (!dbg->rqs_cnt_debugfs)
5548 goto out_debugfs;
5549
5550 dbg->timeout_debugfs =
5551 debugfs_create_file("timeout", 0600,
5552 dbg->dir_debugfs,
5553 &dev->delay_drop,
5554 &fops_delay_drop_timeout);
5555 if (!dbg->timeout_debugfs)
5556 goto out_debugfs;
5557
5558 return 0;
5559
5560out_debugfs:
5561 delay_drop_debugfs_cleanup(dev);
5562 return -ENOMEM;
Maor Gottlieb03404e82017-05-30 10:29:13 +03005563}
5564
5565static void init_delay_drop(struct mlx5_ib_dev *dev)
5566{
5567 if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
5568 return;
5569
5570 mutex_init(&dev->delay_drop.lock);
5571 dev->delay_drop.dev = dev;
5572 dev->delay_drop.activate = false;
5573 dev->delay_drop.timeout = MLX5_MAX_DELAY_DROP_TIMEOUT_MS * 1000;
5574 INIT_WORK(&dev->delay_drop.delay_drop_work, delay_drop_handler);
Maor Gottliebfe248c32017-05-30 10:29:14 +03005575 atomic_set(&dev->delay_drop.rqs_cnt, 0);
5576 atomic_set(&dev->delay_drop.events_cnt, 0);
5577
5578 if (delay_drop_debugfs_init(dev))
5579 mlx5_ib_warn(dev, "Failed to init delay drop debugfs\n");
Maor Gottlieb03404e82017-05-30 10:29:13 +03005580}
5581
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005582/* The mlx5_ib_multiport_mutex should be held when calling this function */
5583static void mlx5_ib_unbind_slave_port(struct mlx5_ib_dev *ibdev,
5584 struct mlx5_ib_multiport_info *mpi)
5585{
5586 u8 port_num = mlx5_core_native_port_num(mpi->mdev) - 1;
5587 struct mlx5_ib_port *port = &ibdev->port[port_num];
5588 int comps;
5589 int err;
5590 int i;
5591
Parav Pandita9e546e2018-01-04 17:25:39 +02005592 mlx5_ib_cleanup_cong_debugfs(ibdev, port_num);
5593
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005594 spin_lock(&port->mp.mpi_lock);
5595 if (!mpi->ibdev) {
5596 spin_unlock(&port->mp.mpi_lock);
5597 return;
5598 }
Saeed Mahameeddf097a22018-11-26 14:39:00 -08005599
5600 if (mpi->mdev_events.notifier_call)
5601 mlx5_notifier_unregister(mpi->mdev, &mpi->mdev_events);
5602 mpi->mdev_events.notifier_call = NULL;
5603
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005604 mpi->ibdev = NULL;
5605
5606 spin_unlock(&port->mp.mpi_lock);
5607 mlx5_remove_netdev_notifier(ibdev, port_num);
5608 spin_lock(&port->mp.mpi_lock);
5609
5610 comps = mpi->mdev_refcnt;
5611 if (comps) {
5612 mpi->unaffiliate = true;
5613 init_completion(&mpi->unref_comp);
5614 spin_unlock(&port->mp.mpi_lock);
5615
5616 for (i = 0; i < comps; i++)
5617 wait_for_completion(&mpi->unref_comp);
5618
5619 spin_lock(&port->mp.mpi_lock);
5620 mpi->unaffiliate = false;
5621 }
5622
5623 port->mp.mpi = NULL;
5624
5625 list_add_tail(&mpi->list, &mlx5_ib_unaffiliated_port_list);
5626
5627 spin_unlock(&port->mp.mpi_lock);
5628
5629 err = mlx5_nic_vport_unaffiliate_multiport(mpi->mdev);
5630
5631 mlx5_ib_dbg(ibdev, "unaffiliated port %d\n", port_num + 1);
5632 /* Log an error, still needed to cleanup the pointers and add
5633 * it back to the list.
5634 */
5635 if (err)
5636 mlx5_ib_err(ibdev, "Failed to unaffiliate port %u\n",
5637 port_num + 1);
5638
Mark Bloch95579e72019-03-28 15:27:33 +02005639 ibdev->port[port_num].roce.last_port_state = IB_PORT_DOWN;
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005640}
5641
5642/* The mlx5_ib_multiport_mutex should be held when calling this function */
5643static bool mlx5_ib_bind_slave_port(struct mlx5_ib_dev *ibdev,
5644 struct mlx5_ib_multiport_info *mpi)
5645{
5646 u8 port_num = mlx5_core_native_port_num(mpi->mdev) - 1;
5647 int err;
5648
5649 spin_lock(&ibdev->port[port_num].mp.mpi_lock);
5650 if (ibdev->port[port_num].mp.mpi) {
Qing Huang25771882018-07-23 14:15:08 -07005651 mlx5_ib_dbg(ibdev, "port %d already affiliated.\n",
5652 port_num + 1);
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005653 spin_unlock(&ibdev->port[port_num].mp.mpi_lock);
5654 return false;
5655 }
5656
5657 ibdev->port[port_num].mp.mpi = mpi;
5658 mpi->ibdev = ibdev;
Saeed Mahameeddf097a22018-11-26 14:39:00 -08005659 mpi->mdev_events.notifier_call = NULL;
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005660 spin_unlock(&ibdev->port[port_num].mp.mpi_lock);
5661
5662 err = mlx5_nic_vport_affiliate_multiport(ibdev->mdev, mpi->mdev);
5663 if (err)
5664 goto unbind;
5665
5666 err = get_port_caps(ibdev, mlx5_core_native_port_num(mpi->mdev));
5667 if (err)
5668 goto unbind;
5669
5670 err = mlx5_add_netdev_notifier(ibdev, port_num);
5671 if (err) {
5672 mlx5_ib_err(ibdev, "failed adding netdev notifier for port %u\n",
5673 port_num + 1);
5674 goto unbind;
5675 }
5676
Saeed Mahameeddf097a22018-11-26 14:39:00 -08005677 mpi->mdev_events.notifier_call = mlx5_ib_event_slave_port;
5678 mlx5_notifier_register(mpi->mdev, &mpi->mdev_events);
5679
Greg Kroah-Hartman73eb8f02019-01-22 16:17:57 +01005680 mlx5_ib_init_cong_debugfs(ibdev, port_num);
Parav Pandita9e546e2018-01-04 17:25:39 +02005681
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005682 return true;
5683
5684unbind:
5685 mlx5_ib_unbind_slave_port(ibdev, mpi);
5686 return false;
5687}
5688
5689static int mlx5_ib_init_multiport_master(struct mlx5_ib_dev *dev)
5690{
5691 int port_num = mlx5_core_native_port_num(dev->mdev) - 1;
5692 enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev,
5693 port_num + 1);
5694 struct mlx5_ib_multiport_info *mpi;
5695 int err;
5696 int i;
5697
5698 if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
5699 return 0;
5700
5701 err = mlx5_query_nic_vport_system_image_guid(dev->mdev,
5702 &dev->sys_image_guid);
5703 if (err)
5704 return err;
5705
5706 err = mlx5_nic_vport_enable_roce(dev->mdev);
5707 if (err)
5708 return err;
5709
5710 mutex_lock(&mlx5_ib_multiport_mutex);
5711 for (i = 0; i < dev->num_ports; i++) {
5712 bool bound = false;
5713
5714 /* build a stub multiport info struct for the native port. */
5715 if (i == port_num) {
5716 mpi = kzalloc(sizeof(*mpi), GFP_KERNEL);
5717 if (!mpi) {
5718 mutex_unlock(&mlx5_ib_multiport_mutex);
5719 mlx5_nic_vport_disable_roce(dev->mdev);
5720 return -ENOMEM;
5721 }
5722
5723 mpi->is_master = true;
5724 mpi->mdev = dev->mdev;
5725 mpi->sys_image_guid = dev->sys_image_guid;
5726 dev->port[i].mp.mpi = mpi;
5727 mpi->ibdev = dev;
5728 mpi = NULL;
5729 continue;
5730 }
5731
5732 list_for_each_entry(mpi, &mlx5_ib_unaffiliated_port_list,
5733 list) {
5734 if (dev->sys_image_guid == mpi->sys_image_guid &&
5735 (mlx5_core_native_port_num(mpi->mdev) - 1) == i) {
5736 bound = mlx5_ib_bind_slave_port(dev, mpi);
5737 }
5738
5739 if (bound) {
5740 dev_dbg(&mpi->mdev->pdev->dev, "removing port from unaffiliated list.\n");
5741 mlx5_ib_dbg(dev, "port %d bound\n", i + 1);
5742 list_del(&mpi->list);
5743 break;
5744 }
5745 }
5746 if (!bound) {
5747 get_port_caps(dev, i + 1);
5748 mlx5_ib_dbg(dev, "no free port found for port %d\n",
5749 i + 1);
5750 }
5751 }
5752
5753 list_add_tail(&dev->ib_dev_list, &mlx5_ib_dev_list);
5754 mutex_unlock(&mlx5_ib_multiport_mutex);
5755 return err;
5756}
5757
5758static void mlx5_ib_cleanup_multiport_master(struct mlx5_ib_dev *dev)
5759{
5760 int port_num = mlx5_core_native_port_num(dev->mdev) - 1;
5761 enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev,
5762 port_num + 1);
5763 int i;
5764
5765 if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
5766 return;
5767
5768 mutex_lock(&mlx5_ib_multiport_mutex);
5769 for (i = 0; i < dev->num_ports; i++) {
5770 if (dev->port[i].mp.mpi) {
5771 /* Destroy the native port stub */
5772 if (i == port_num) {
5773 kfree(dev->port[i].mp.mpi);
5774 dev->port[i].mp.mpi = NULL;
5775 } else {
5776 mlx5_ib_dbg(dev, "unbinding port_num: %d\n", i + 1);
5777 mlx5_ib_unbind_slave_port(dev, dev->port[i].mp.mpi);
5778 }
5779 }
5780 }
5781
5782 mlx5_ib_dbg(dev, "removing from devlist\n");
5783 list_del(&dev->ib_dev_list);
5784 mutex_unlock(&mlx5_ib_multiport_mutex);
5785
5786 mlx5_nic_vport_disable_roce(dev->mdev);
5787}
5788
Jason Gunthorpe9a119cd2018-07-04 08:50:28 +03005789ADD_UVERBS_ATTRIBUTES_SIMPLE(
5790 mlx5_ib_dm,
5791 UVERBS_OBJECT_DM,
5792 UVERBS_METHOD_DM_ALLOC,
5793 UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_ALLOC_DM_RESP_START_OFFSET,
5794 UVERBS_ATTR_TYPE(u64),
Jason Gunthorpe83bb4442018-07-04 08:50:29 +03005795 UA_MANDATORY),
Jason Gunthorpe9a119cd2018-07-04 08:50:28 +03005796 UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_ALLOC_DM_RESP_PAGE_INDEX,
5797 UVERBS_ATTR_TYPE(u16),
Jason Gunthorpe83bb4442018-07-04 08:50:29 +03005798 UA_MANDATORY));
Ariel Levkovich24da0012018-04-05 18:53:27 +03005799
Jason Gunthorpe9a119cd2018-07-04 08:50:28 +03005800ADD_UVERBS_ATTRIBUTES_SIMPLE(
5801 mlx5_ib_flow_action,
5802 UVERBS_OBJECT_FLOW_ACTION,
5803 UVERBS_METHOD_FLOW_ACTION_ESP_CREATE,
Jason Gunthorpebccd0622018-07-26 16:37:14 -06005804 UVERBS_ATTR_FLAGS_IN(MLX5_IB_ATTR_CREATE_FLOW_ACTION_FLAGS,
5805 enum mlx5_ib_uapi_flow_action_flags));
Aviad Yehezkelc6475a02018-03-28 09:27:50 +03005806
Jason Gunthorpe0cbf4322018-11-12 22:59:50 +02005807static const struct uapi_definition mlx5_ib_defs[] = {
5808#if IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS)
Jason Gunthorpe36e235c2018-11-12 22:59:53 +02005809 UAPI_DEF_CHAIN(mlx5_ib_devx_defs),
Jason Gunthorpe0cbf4322018-11-12 22:59:50 +02005810 UAPI_DEF_CHAIN(mlx5_ib_flow_defs),
5811#endif
Matan Barak8c846602018-03-28 09:27:41 +03005812
Jason Gunthorpe0cbf4322018-11-12 22:59:50 +02005813 UAPI_DEF_CHAIN_OBJ_TREE(UVERBS_OBJECT_FLOW_ACTION,
5814 &mlx5_ib_flow_action),
5815 UAPI_DEF_CHAIN_OBJ_TREE(UVERBS_OBJECT_DM, &mlx5_ib_dm),
5816 {}
5817};
Matan Barak8c846602018-03-28 09:27:41 +03005818
Raed Salem1a1e03d2018-05-31 16:43:41 +03005819static int mlx5_ib_read_counters(struct ib_counters *counters,
5820 struct ib_counters_read_attr *read_attr,
5821 struct uverbs_attr_bundle *attrs)
5822{
5823 struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
5824 struct mlx5_read_counters_attr mread_attr = {};
5825 struct mlx5_ib_flow_counters_desc *desc;
5826 int ret, i;
5827
5828 mutex_lock(&mcounters->mcntrs_mutex);
5829 if (mcounters->cntrs_max_index > read_attr->ncounters) {
5830 ret = -EINVAL;
5831 goto err_bound;
5832 }
5833
5834 mread_attr.out = kcalloc(mcounters->counters_num, sizeof(u64),
5835 GFP_KERNEL);
5836 if (!mread_attr.out) {
5837 ret = -ENOMEM;
5838 goto err_bound;
5839 }
5840
5841 mread_attr.hw_cntrs_hndl = mcounters->hw_cntrs_hndl;
5842 mread_attr.flags = read_attr->flags;
5843 ret = mcounters->read_counters(counters->device, &mread_attr);
5844 if (ret)
5845 goto err_read;
5846
5847 /* do the pass over the counters data array to assign according to the
5848 * descriptions and indexing pairs
5849 */
5850 desc = mcounters->counters_data;
5851 for (i = 0; i < mcounters->ncounters; i++)
5852 read_attr->counters_buff[desc[i].index] += mread_attr.out[desc[i].description];
5853
5854err_read:
5855 kfree(mread_attr.out);
5856err_bound:
5857 mutex_unlock(&mcounters->mcntrs_mutex);
5858 return ret;
5859}
5860
Raed Salemb29e2a12018-05-31 16:43:38 +03005861static int mlx5_ib_destroy_counters(struct ib_counters *counters)
5862{
5863 struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
5864
Raed Salem3b3233f2018-05-31 16:43:39 +03005865 counters_clear_description(counters);
5866 if (mcounters->hw_cntrs_hndl)
5867 mlx5_fc_destroy(to_mdev(counters->device)->mdev,
5868 mcounters->hw_cntrs_hndl);
5869
Raed Salemb29e2a12018-05-31 16:43:38 +03005870 kfree(mcounters);
5871
5872 return 0;
5873}
5874
5875static struct ib_counters *mlx5_ib_create_counters(struct ib_device *device,
5876 struct uverbs_attr_bundle *attrs)
5877{
5878 struct mlx5_ib_mcounters *mcounters;
5879
5880 mcounters = kzalloc(sizeof(*mcounters), GFP_KERNEL);
5881 if (!mcounters)
5882 return ERR_PTR(-ENOMEM);
5883
Raed Salem3b3233f2018-05-31 16:43:39 +03005884 mutex_init(&mcounters->mcntrs_mutex);
5885
Raed Salemb29e2a12018-05-31 16:43:38 +03005886 return &mcounters->ibcntrs;
5887}
5888
Mark Blochfb652d32019-03-28 15:27:42 +02005889static void mlx5_ib_stage_init_cleanup(struct mlx5_ib_dev *dev)
Eli Cohene126ba92013-07-07 17:25:49 +03005890{
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005891 mlx5_ib_cleanup_multiport_master(dev);
Leon Romanovsky13859d5d2019-01-08 16:07:26 +02005892 if (IS_ENABLED(CONFIG_INFINIBAND_ON_DEMAND_PAGING)) {
Yishai Hadas534fd7a2019-01-13 16:01:17 +02005893 srcu_barrier(&dev->mr_srcu);
Leon Romanovsky13859d5d2019-01-08 16:07:26 +02005894 cleanup_srcu_struct(&dev->mr_srcu);
Leon Romanovsky13859d5d2019-01-08 16:07:26 +02005895 }
Mark Bloch16c19752018-01-01 13:06:58 +02005896}
5897
Mark Blochfb652d32019-03-28 15:27:42 +02005898static int mlx5_ib_stage_init_init(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02005899{
5900 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03005901 int err;
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005902 int i;
Eli Cohene126ba92013-07-07 17:25:49 +03005903
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005904 for (i = 0; i < dev->num_ports; i++) {
5905 spin_lock_init(&dev->port[i].mp.mpi_lock);
Mark Bloch95579e72019-03-28 15:27:33 +02005906 rwlock_init(&dev->port[i].roce.netdev_lock);
Mark Blochd3b5cc12019-03-28 15:46:26 +02005907 dev->port[i].roce.dev = dev;
5908 dev->port[i].roce.native_port_num = i + 1;
5909 dev->port[i].roce.last_port_state = IB_PORT_DOWN;
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005910 }
5911
5912 err = mlx5_ib_init_multiport_master(dev);
Eli Cohene126ba92013-07-07 17:25:49 +03005913 if (err)
Mark Blochda796cc2019-03-28 15:27:35 +02005914 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03005915
Mark Blocha989ea02019-03-28 15:27:40 +02005916 err = set_has_smi_cap(dev);
5917 if (err)
5918 return err;
5919
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005920 if (!mlx5_core_mp_enabled(mdev)) {
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005921 for (i = 1; i <= dev->num_ports; i++) {
5922 err = get_port_caps(dev, i);
5923 if (err)
5924 break;
5925 }
5926 } else {
5927 err = get_port_caps(dev, mlx5_core_native_port_num(mdev));
5928 }
5929 if (err)
5930 goto err_mp;
5931
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03005932 if (mlx5_use_mad_ifc(dev))
5933 get_ext_port_caps(dev);
Eli Cohene126ba92013-07-07 17:25:49 +03005934
Eli Cohene126ba92013-07-07 17:25:49 +03005935 dev->ib_dev.owner = THIS_MODULE;
5936 dev->ib_dev.node_type = RDMA_NODE_IB_CA;
Sagi Grimbergc6790aa2015-09-24 10:34:23 +03005937 dev->ib_dev.local_dma_lkey = 0 /* not supported for now */;
Daniel Jurgens508562d2018-01-04 17:25:34 +02005938 dev->ib_dev.phys_port_cnt = dev->num_ports;
Saeed Mahameedf2f3df52018-11-19 10:52:38 -08005939 dev->ib_dev.num_comp_vectors = mlx5_comp_vectors_count(mdev);
Bart Van Assche9b0c2892017-01-20 13:04:21 -08005940 dev->ib_dev.dev.parent = &mdev->pdev->dev;
Eli Cohene126ba92013-07-07 17:25:49 +03005941
Mark Bloch3cc297d2018-01-01 13:07:03 +02005942 mutex_init(&dev->cap_mask_mutex);
5943 INIT_LIST_HEAD(&dev->qp_list);
5944 spin_lock_init(&dev->reset_flow_resource_lock);
5945
Ariel Levkovich24da0012018-04-05 18:53:27 +03005946 spin_lock_init(&dev->memic.memic_lock);
5947 dev->memic.dev = mdev;
5948
Leon Romanovsky13859d5d2019-01-08 16:07:26 +02005949 if (IS_ENABLED(CONFIG_INFINIBAND_ON_DEMAND_PAGING)) {
Leon Romanovsky13859d5d2019-01-08 16:07:26 +02005950 err = init_srcu_struct(&dev->mr_srcu);
Moni Shouaa6bc3872019-02-17 16:08:22 +02005951 if (err)
Leon Romanovsky13859d5d2019-01-08 16:07:26 +02005952 goto err_mp;
Jason Gunthorpe623d1542018-12-20 16:39:26 -07005953 }
Mark Bloch3cc297d2018-01-01 13:07:03 +02005954
Mark Bloch16c19752018-01-01 13:06:58 +02005955 return 0;
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005956err_mp:
5957 mlx5_ib_cleanup_multiport_master(dev);
Mark Bloch16c19752018-01-01 13:06:58 +02005958
Mark Bloch16c19752018-01-01 13:06:58 +02005959 return -ENOMEM;
5960}
5961
Mark Bloch9a4ca382018-01-16 14:42:35 +00005962static int mlx5_ib_stage_flow_db_init(struct mlx5_ib_dev *dev)
5963{
5964 dev->flow_db = kzalloc(sizeof(*dev->flow_db), GFP_KERNEL);
5965
5966 if (!dev->flow_db)
5967 return -ENOMEM;
5968
5969 mutex_init(&dev->flow_db->lock);
5970
5971 return 0;
5972}
5973
5974static void mlx5_ib_stage_flow_db_cleanup(struct mlx5_ib_dev *dev)
5975{
5976 kfree(dev->flow_db);
5977}
5978
Kamal Heib96458233e2018-12-10 21:09:38 +02005979static const struct ib_device_ops mlx5_ib_dev_ops = {
5980 .add_gid = mlx5_ib_add_gid,
5981 .alloc_mr = mlx5_ib_alloc_mr,
5982 .alloc_pd = mlx5_ib_alloc_pd,
5983 .alloc_ucontext = mlx5_ib_alloc_ucontext,
5984 .attach_mcast = mlx5_ib_mcg_attach,
5985 .check_mr_status = mlx5_ib_check_mr_status,
5986 .create_ah = mlx5_ib_create_ah,
5987 .create_counters = mlx5_ib_create_counters,
5988 .create_cq = mlx5_ib_create_cq,
5989 .create_flow = mlx5_ib_create_flow,
5990 .create_qp = mlx5_ib_create_qp,
5991 .create_srq = mlx5_ib_create_srq,
5992 .dealloc_pd = mlx5_ib_dealloc_pd,
5993 .dealloc_ucontext = mlx5_ib_dealloc_ucontext,
5994 .del_gid = mlx5_ib_del_gid,
5995 .dereg_mr = mlx5_ib_dereg_mr,
5996 .destroy_ah = mlx5_ib_destroy_ah,
5997 .destroy_counters = mlx5_ib_destroy_counters,
5998 .destroy_cq = mlx5_ib_destroy_cq,
5999 .destroy_flow = mlx5_ib_destroy_flow,
6000 .destroy_flow_action = mlx5_ib_destroy_flow_action,
6001 .destroy_qp = mlx5_ib_destroy_qp,
6002 .destroy_srq = mlx5_ib_destroy_srq,
6003 .detach_mcast = mlx5_ib_mcg_detach,
6004 .disassociate_ucontext = mlx5_ib_disassociate_ucontext,
6005 .drain_rq = mlx5_ib_drain_rq,
6006 .drain_sq = mlx5_ib_drain_sq,
6007 .get_dev_fw_str = get_dev_fw_str,
6008 .get_dma_mr = mlx5_ib_get_dma_mr,
6009 .get_link_layer = mlx5_ib_port_link_layer,
6010 .map_mr_sg = mlx5_ib_map_mr_sg,
6011 .mmap = mlx5_ib_mmap,
6012 .modify_cq = mlx5_ib_modify_cq,
6013 .modify_device = mlx5_ib_modify_device,
6014 .modify_port = mlx5_ib_modify_port,
6015 .modify_qp = mlx5_ib_modify_qp,
6016 .modify_srq = mlx5_ib_modify_srq,
6017 .poll_cq = mlx5_ib_poll_cq,
6018 .post_recv = mlx5_ib_post_recv,
6019 .post_send = mlx5_ib_post_send,
6020 .post_srq_recv = mlx5_ib_post_srq_recv,
6021 .process_mad = mlx5_ib_process_mad,
6022 .query_ah = mlx5_ib_query_ah,
6023 .query_device = mlx5_ib_query_device,
6024 .query_gid = mlx5_ib_query_gid,
6025 .query_pkey = mlx5_ib_query_pkey,
6026 .query_qp = mlx5_ib_query_qp,
6027 .query_srq = mlx5_ib_query_srq,
6028 .read_counters = mlx5_ib_read_counters,
6029 .reg_user_mr = mlx5_ib_reg_user_mr,
6030 .req_notify_cq = mlx5_ib_arm_cq,
6031 .rereg_user_mr = mlx5_ib_rereg_user_mr,
6032 .resize_cq = mlx5_ib_resize_cq,
Leon Romanovskyd3456912019-04-03 16:42:42 +03006033
6034 INIT_RDMA_OBJ_SIZE(ib_ah, mlx5_ib_ah, ibah),
Leon Romanovsky21a428a2019-02-03 14:55:51 +02006035 INIT_RDMA_OBJ_SIZE(ib_pd, mlx5_ib_pd, ibpd),
Leon Romanovsky68e326d2019-04-03 16:42:43 +03006036 INIT_RDMA_OBJ_SIZE(ib_srq, mlx5_ib_srq, ibsrq),
Leon Romanovskya2a074e2019-02-12 20:39:16 +02006037 INIT_RDMA_OBJ_SIZE(ib_ucontext, mlx5_ib_ucontext, ibucontext),
Kamal Heib96458233e2018-12-10 21:09:38 +02006038};
6039
6040static const struct ib_device_ops mlx5_ib_dev_flow_ipsec_ops = {
6041 .create_flow_action_esp = mlx5_ib_create_flow_action_esp,
6042 .modify_flow_action_esp = mlx5_ib_modify_flow_action_esp,
6043};
6044
6045static const struct ib_device_ops mlx5_ib_dev_ipoib_enhanced_ops = {
6046 .rdma_netdev_get_params = mlx5_ib_rn_get_params,
6047};
6048
6049static const struct ib_device_ops mlx5_ib_dev_sriov_ops = {
6050 .get_vf_config = mlx5_ib_get_vf_config,
6051 .get_vf_stats = mlx5_ib_get_vf_stats,
6052 .set_vf_guid = mlx5_ib_set_vf_guid,
6053 .set_vf_link_state = mlx5_ib_set_vf_link_state,
6054};
6055
6056static const struct ib_device_ops mlx5_ib_dev_mw_ops = {
6057 .alloc_mw = mlx5_ib_alloc_mw,
6058 .dealloc_mw = mlx5_ib_dealloc_mw,
6059};
6060
6061static const struct ib_device_ops mlx5_ib_dev_xrc_ops = {
6062 .alloc_xrcd = mlx5_ib_alloc_xrcd,
6063 .dealloc_xrcd = mlx5_ib_dealloc_xrcd,
6064};
6065
6066static const struct ib_device_ops mlx5_ib_dev_dm_ops = {
6067 .alloc_dm = mlx5_ib_alloc_dm,
6068 .dealloc_dm = mlx5_ib_dealloc_dm,
6069 .reg_dm_mr = mlx5_ib_reg_dm_mr,
6070};
6071
Mark Blochfb652d32019-03-28 15:27:42 +02006072static int mlx5_ib_stage_caps_init(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02006073{
6074 struct mlx5_core_dev *mdev = dev->mdev;
Mark Bloch16c19752018-01-01 13:06:58 +02006075 int err;
6076
Eli Cohene126ba92013-07-07 17:25:49 +03006077 dev->ib_dev.uverbs_abi_ver = MLX5_IB_UVERBS_ABI_VERSION;
6078 dev->ib_dev.uverbs_cmd_mask =
6079 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
6080 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
6081 (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
6082 (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
6083 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
Moni Shoua41c450f2016-11-23 08:23:26 +02006084 (1ull << IB_USER_VERBS_CMD_CREATE_AH) |
6085 (1ull << IB_USER_VERBS_CMD_DESTROY_AH) |
Eli Cohene126ba92013-07-07 17:25:49 +03006086 (1ull << IB_USER_VERBS_CMD_REG_MR) |
Noa Osherovich56e11d62016-02-29 16:46:51 +02006087 (1ull << IB_USER_VERBS_CMD_REREG_MR) |
Eli Cohene126ba92013-07-07 17:25:49 +03006088 (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
6089 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
6090 (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
6091 (1ull << IB_USER_VERBS_CMD_RESIZE_CQ) |
6092 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
6093 (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
6094 (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
6095 (1ull << IB_USER_VERBS_CMD_QUERY_QP) |
6096 (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
6097 (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST) |
6098 (1ull << IB_USER_VERBS_CMD_DETACH_MCAST) |
6099 (1ull << IB_USER_VERBS_CMD_CREATE_SRQ) |
6100 (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ) |
6101 (1ull << IB_USER_VERBS_CMD_QUERY_SRQ) |
6102 (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ) |
6103 (1ull << IB_USER_VERBS_CMD_CREATE_XSRQ) |
6104 (1ull << IB_USER_VERBS_CMD_OPEN_QP);
Haggai Eran1707cb42015-02-08 13:28:52 +02006105 dev->ib_dev.uverbs_ex_cmd_mask =
Matan Barakd4584dd2016-01-28 17:51:46 +02006106 (1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE) |
6107 (1ull << IB_USER_VERBS_EX_CMD_CREATE_CQ) |
Bodong Wang7d29f342016-12-01 13:43:16 +02006108 (1ull << IB_USER_VERBS_EX_CMD_CREATE_QP) |
Yonatan Cohenb0e9df62017-11-13 10:51:15 +02006109 (1ull << IB_USER_VERBS_EX_CMD_MODIFY_QP) |
Kamal Heib96458233e2018-12-10 21:09:38 +02006110 (1ull << IB_USER_VERBS_EX_CMD_MODIFY_CQ) |
6111 (1ull << IB_USER_VERBS_EX_CMD_CREATE_FLOW) |
6112 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_FLOW);
Eli Cohene126ba92013-07-07 17:25:49 +03006113
Denis Drozdovf6a8a192018-08-14 14:08:51 +03006114 if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads) &&
6115 IS_ENABLED(CONFIG_MLX5_CORE_IPOIB))
Kamal Heib96458233e2018-12-10 21:09:38 +02006116 ib_set_device_ops(&dev->ib_dev,
6117 &mlx5_ib_dev_ipoib_enhanced_ops);
Niranjana Vishwanathapura8e959602017-06-30 13:14:46 -07006118
Kamal Heib96458233e2018-12-10 21:09:38 +02006119 if (mlx5_core_is_pf(mdev))
6120 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_sriov_ops);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03006121
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03006122 dev->umr_fence = mlx5_get_umr_fence(MLX5_CAP_GEN(mdev, umr_fence));
6123
Matan Barakd2370e02016-02-29 18:05:30 +02006124 if (MLX5_CAP_GEN(mdev, imaicl)) {
Matan Barakd2370e02016-02-29 18:05:30 +02006125 dev->ib_dev.uverbs_cmd_mask |=
6126 (1ull << IB_USER_VERBS_CMD_ALLOC_MW) |
6127 (1ull << IB_USER_VERBS_CMD_DEALLOC_MW);
Kamal Heib96458233e2018-12-10 21:09:38 +02006128 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_mw_ops);
Matan Barakd2370e02016-02-29 18:05:30 +02006129 }
6130
Saeed Mahameed938fe832015-05-28 22:28:41 +03006131 if (MLX5_CAP_GEN(mdev, xrc)) {
Eli Cohene126ba92013-07-07 17:25:49 +03006132 dev->ib_dev.uverbs_cmd_mask |=
6133 (1ull << IB_USER_VERBS_CMD_OPEN_XRCD) |
6134 (1ull << IB_USER_VERBS_CMD_CLOSE_XRCD);
Kamal Heib96458233e2018-12-10 21:09:38 +02006135 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_xrc_ops);
Eli Cohene126ba92013-07-07 17:25:49 +03006136 }
6137
Kamal Heib96458233e2018-12-10 21:09:38 +02006138 if (MLX5_CAP_DEV_MEM(mdev, memic))
6139 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_dm_ops);
Ariel Levkovich24da0012018-04-05 18:53:27 +03006140
Jason Gunthorpedfb631a2018-11-12 22:59:49 +02006141 if (mlx5_accel_ipsec_device_caps(dev->mdev) &
Kamal Heib96458233e2018-12-10 21:09:38 +02006142 MLX5_ACCEL_IPSEC_CAP_DEVICE)
6143 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_flow_ipsec_ops);
Matan Barak0ede73b2018-03-19 15:02:34 +02006144 dev->ib_dev.driver_id = RDMA_DRIVER_MLX5;
Kamal Heib96458233e2018-12-10 21:09:38 +02006145 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_ops);
Yishai Hadas81e30882017-06-08 16:15:09 +03006146
Jason Gunthorpe36e235c2018-11-12 22:59:53 +02006147 if (IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS))
6148 dev->ib_dev.driver_def = mlx5_ib_defs;
Eli Cohene126ba92013-07-07 17:25:49 +03006149
6150 err = init_node_data(dev);
6151 if (err)
Mark Bloch16c19752018-01-01 13:06:58 +02006152 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03006153
Mark Blochc8b89922018-01-01 13:07:02 +02006154 if ((MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_ETH) &&
Jason Gunthorpee7996a92018-01-29 13:26:40 -07006155 (MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) ||
6156 MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
Mark Blocha560f1d2018-09-17 13:30:47 +03006157 mutex_init(&dev->lb.mutex);
Mark Blochc8b89922018-01-01 13:07:02 +02006158
Mark Bloch16c19752018-01-01 13:06:58 +02006159 return 0;
6160}
6161
Kamal Heib96458233e2018-12-10 21:09:38 +02006162static const struct ib_device_ops mlx5_ib_dev_port_ops = {
6163 .get_port_immutable = mlx5_port_immutable,
6164 .query_port = mlx5_ib_query_port,
6165};
6166
Mark Bloch8e6efa32017-11-06 12:22:13 +00006167static int mlx5_ib_stage_non_default_cb(struct mlx5_ib_dev *dev)
6168{
Kamal Heib96458233e2018-12-10 21:09:38 +02006169 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_port_ops);
Mark Bloch8e6efa32017-11-06 12:22:13 +00006170 return 0;
6171}
6172
Kamal Heib96458233e2018-12-10 21:09:38 +02006173static const struct ib_device_ops mlx5_ib_dev_port_rep_ops = {
6174 .get_port_immutable = mlx5_port_rep_immutable,
6175 .query_port = mlx5_ib_rep_query_port,
6176};
6177
Mark Blochfb652d32019-03-28 15:27:42 +02006178static int mlx5_ib_stage_rep_non_default_cb(struct mlx5_ib_dev *dev)
Mark Bloch8e6efa32017-11-06 12:22:13 +00006179{
Kamal Heib96458233e2018-12-10 21:09:38 +02006180 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_port_rep_ops);
Mark Bloch8e6efa32017-11-06 12:22:13 +00006181 return 0;
6182}
6183
Kamal Heib96458233e2018-12-10 21:09:38 +02006184static const struct ib_device_ops mlx5_ib_dev_common_roce_ops = {
6185 .create_rwq_ind_table = mlx5_ib_create_rwq_ind_table,
6186 .create_wq = mlx5_ib_create_wq,
6187 .destroy_rwq_ind_table = mlx5_ib_destroy_rwq_ind_table,
6188 .destroy_wq = mlx5_ib_destroy_wq,
6189 .get_netdev = mlx5_ib_get_netdev,
6190 .modify_wq = mlx5_ib_modify_wq,
6191};
6192
Leon Romanovskye3f1ed12018-07-08 12:55:43 +03006193static int mlx5_ib_stage_common_roce_init(struct mlx5_ib_dev *dev)
Mark Bloch8e6efa32017-11-06 12:22:13 +00006194{
Leon Romanovskye3f1ed12018-07-08 12:55:43 +03006195 u8 port_num;
Mark Bloch8e6efa32017-11-06 12:22:13 +00006196
Mark Bloch8e6efa32017-11-06 12:22:13 +00006197 dev->ib_dev.uverbs_ex_cmd_mask |=
6198 (1ull << IB_USER_VERBS_EX_CMD_CREATE_WQ) |
6199 (1ull << IB_USER_VERBS_EX_CMD_MODIFY_WQ) |
6200 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_WQ) |
6201 (1ull << IB_USER_VERBS_EX_CMD_CREATE_RWQ_IND_TBL) |
6202 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_RWQ_IND_TBL);
Kamal Heib96458233e2018-12-10 21:09:38 +02006203 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_common_roce_ops);
Mark Bloch8e6efa32017-11-06 12:22:13 +00006204
Leon Romanovskye3f1ed12018-07-08 12:55:43 +03006205 port_num = mlx5_core_native_port_num(dev->mdev) - 1;
6206
Mark Bloch26628e22019-03-28 15:27:41 +02006207 /* Register only for native ports */
Mark Bloch8e6efa32017-11-06 12:22:13 +00006208 return mlx5_add_netdev_notifier(dev, port_num);
6209}
6210
6211static void mlx5_ib_stage_common_roce_cleanup(struct mlx5_ib_dev *dev)
6212{
6213 u8 port_num = mlx5_core_native_port_num(dev->mdev) - 1;
6214
6215 mlx5_remove_netdev_notifier(dev, port_num);
6216}
6217
Mark Blochfb652d32019-03-28 15:27:42 +02006218static int mlx5_ib_stage_rep_roce_init(struct mlx5_ib_dev *dev)
Mark Bloch8e6efa32017-11-06 12:22:13 +00006219{
6220 struct mlx5_core_dev *mdev = dev->mdev;
6221 enum rdma_link_layer ll;
6222 int port_type_cap;
6223 int err = 0;
Mark Bloch8e6efa32017-11-06 12:22:13 +00006224
Mark Bloch8e6efa32017-11-06 12:22:13 +00006225 port_type_cap = MLX5_CAP_GEN(mdev, port_type);
6226 ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
6227
6228 if (ll == IB_LINK_LAYER_ETHERNET)
Leon Romanovskye3f1ed12018-07-08 12:55:43 +03006229 err = mlx5_ib_stage_common_roce_init(dev);
Mark Bloch8e6efa32017-11-06 12:22:13 +00006230
6231 return err;
6232}
6233
Mark Blochfb652d32019-03-28 15:27:42 +02006234static void mlx5_ib_stage_rep_roce_cleanup(struct mlx5_ib_dev *dev)
Mark Bloch8e6efa32017-11-06 12:22:13 +00006235{
6236 mlx5_ib_stage_common_roce_cleanup(dev);
6237}
6238
Mark Bloch16c19752018-01-01 13:06:58 +02006239static int mlx5_ib_stage_roce_init(struct mlx5_ib_dev *dev)
6240{
6241 struct mlx5_core_dev *mdev = dev->mdev;
6242 enum rdma_link_layer ll;
6243 int port_type_cap;
6244 int err;
6245
6246 port_type_cap = MLX5_CAP_GEN(mdev, port_type);
6247 ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
6248
Achiad Shochatfc24fc52015-12-23 18:47:17 +02006249 if (ll == IB_LINK_LAYER_ETHERNET) {
Leon Romanovskye3f1ed12018-07-08 12:55:43 +03006250 err = mlx5_ib_stage_common_roce_init(dev);
Achiad Shochatfc24fc52015-12-23 18:47:17 +02006251 if (err)
Mark Bloch16c19752018-01-01 13:06:58 +02006252 return err;
Mark Bloch8e6efa32017-11-06 12:22:13 +00006253
Leon Romanovskye3f1ed12018-07-08 12:55:43 +03006254 err = mlx5_enable_eth(dev);
Mark Bloch8e6efa32017-11-06 12:22:13 +00006255 if (err)
6256 goto cleanup;
Achiad Shochatfc24fc52015-12-23 18:47:17 +02006257 }
6258
Mark Bloch16c19752018-01-01 13:06:58 +02006259 return 0;
Mark Bloch8e6efa32017-11-06 12:22:13 +00006260cleanup:
6261 mlx5_ib_stage_common_roce_cleanup(dev);
6262
6263 return err;
Mark Bloch16c19752018-01-01 13:06:58 +02006264}
Eli Cohene126ba92013-07-07 17:25:49 +03006265
Mark Bloch16c19752018-01-01 13:06:58 +02006266static void mlx5_ib_stage_roce_cleanup(struct mlx5_ib_dev *dev)
6267{
6268 struct mlx5_core_dev *mdev = dev->mdev;
6269 enum rdma_link_layer ll;
6270 int port_type_cap;
Eli Cohene126ba92013-07-07 17:25:49 +03006271
Mark Bloch16c19752018-01-01 13:06:58 +02006272 port_type_cap = MLX5_CAP_GEN(mdev, port_type);
6273 ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
6274
6275 if (ll == IB_LINK_LAYER_ETHERNET) {
6276 mlx5_disable_eth(dev);
Mark Bloch8e6efa32017-11-06 12:22:13 +00006277 mlx5_ib_stage_common_roce_cleanup(dev);
Kamal Heib45bded22017-01-18 14:10:32 +02006278 }
Mark Bloch16c19752018-01-01 13:06:58 +02006279}
Haggai Eran6aec21f2014-12-11 17:04:23 +02006280
Mark Blochfb652d32019-03-28 15:27:42 +02006281static int mlx5_ib_stage_dev_res_init(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02006282{
6283 return create_dev_resources(&dev->devr);
6284}
Parav Pandit4a2da0b2017-05-30 10:05:15 +03006285
Mark Blochfb652d32019-03-28 15:27:42 +02006286static void mlx5_ib_stage_dev_res_cleanup(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02006287{
6288 destroy_dev_resources(&dev->devr);
6289}
6290
6291static int mlx5_ib_stage_odp_init(struct mlx5_ib_dev *dev)
6292{
Mark Bloch07321b32018-01-01 13:07:00 +02006293 mlx5_ib_internal_fill_odp_caps(dev);
6294
Mark Bloch16c19752018-01-01 13:06:58 +02006295 return mlx5_ib_odp_init_one(dev);
6296}
6297
Kamal Heibf3ffed02019-01-30 16:13:42 +02006298static void mlx5_ib_stage_odp_cleanup(struct mlx5_ib_dev *dev)
Saeed Mahameedd5d284b2018-11-19 10:52:41 -08006299{
6300 mlx5_ib_odp_cleanup_one(dev);
6301}
6302
Kamal Heib96458233e2018-12-10 21:09:38 +02006303static const struct ib_device_ops mlx5_ib_dev_hw_stats_ops = {
6304 .alloc_hw_stats = mlx5_ib_alloc_hw_stats,
6305 .get_hw_stats = mlx5_ib_get_hw_stats,
6306};
6307
Mark Blochfb652d32019-03-28 15:27:42 +02006308static int mlx5_ib_stage_counters_init(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02006309{
Mark Bloch5e1e7612018-01-01 13:07:01 +02006310 if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt)) {
Kamal Heib96458233e2018-12-10 21:09:38 +02006311 ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_hw_stats_ops);
Mark Bloch5e1e7612018-01-01 13:07:01 +02006312
6313 return mlx5_ib_alloc_counters(dev);
6314 }
Mark Bloch16c19752018-01-01 13:06:58 +02006315
6316 return 0;
6317}
6318
Mark Blochfb652d32019-03-28 15:27:42 +02006319static void mlx5_ib_stage_counters_cleanup(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02006320{
6321 if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt))
6322 mlx5_ib_dealloc_counters(dev);
6323}
6324
6325static int mlx5_ib_stage_cong_debugfs_init(struct mlx5_ib_dev *dev)
6326{
Greg Kroah-Hartman73eb8f02019-01-22 16:17:57 +01006327 mlx5_ib_init_cong_debugfs(dev,
6328 mlx5_core_native_port_num(dev->mdev) - 1);
6329 return 0;
Mark Bloch16c19752018-01-01 13:06:58 +02006330}
6331
6332static void mlx5_ib_stage_cong_debugfs_cleanup(struct mlx5_ib_dev *dev)
6333{
Parav Pandita9e546e2018-01-04 17:25:39 +02006334 mlx5_ib_cleanup_cong_debugfs(dev,
6335 mlx5_core_native_port_num(dev->mdev) - 1);
Mark Bloch16c19752018-01-01 13:06:58 +02006336}
6337
6338static int mlx5_ib_stage_uar_init(struct mlx5_ib_dev *dev)
6339{
Eli Cohen5fe9dec2017-01-03 23:55:25 +02006340 dev->mdev->priv.uar = mlx5_get_uars_page(dev->mdev);
Leon Romanovsky444261c2018-04-23 17:01:56 +03006341 return PTR_ERR_OR_ZERO(dev->mdev->priv.uar);
Mark Bloch16c19752018-01-01 13:06:58 +02006342}
6343
6344static void mlx5_ib_stage_uar_cleanup(struct mlx5_ib_dev *dev)
6345{
6346 mlx5_put_uars_page(dev->mdev, dev->mdev->priv.uar);
6347}
6348
Mark Blochfb652d32019-03-28 15:27:42 +02006349static int mlx5_ib_stage_bfrag_init(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02006350{
6351 int err;
Eli Cohen5fe9dec2017-01-03 23:55:25 +02006352
6353 err = mlx5_alloc_bfreg(dev->mdev, &dev->bfreg, false, false);
6354 if (err)
Mark Bloch16c19752018-01-01 13:06:58 +02006355 return err;
Eli Cohen5fe9dec2017-01-03 23:55:25 +02006356
6357 err = mlx5_alloc_bfreg(dev->mdev, &dev->fp_bfreg, false, true);
6358 if (err)
Mark Bloch16c19752018-01-01 13:06:58 +02006359 mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
Eli Cohen5fe9dec2017-01-03 23:55:25 +02006360
Mark Bloch16c19752018-01-01 13:06:58 +02006361 return err;
6362}
Mark Bloch0837e862016-06-17 15:10:55 +03006363
Mark Blochfb652d32019-03-28 15:27:42 +02006364static void mlx5_ib_stage_bfrag_cleanup(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02006365{
6366 mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
6367 mlx5_free_bfreg(dev->mdev, &dev->bfreg);
6368}
Eli Cohene126ba92013-07-07 17:25:49 +03006369
Mark Blochfb652d32019-03-28 15:27:42 +02006370static int mlx5_ib_stage_ib_reg_init(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02006371{
Jason Gunthorpee349f852018-09-25 16:58:09 -06006372 const char *name;
6373
Parav Pandit508a5232018-10-11 22:31:54 +03006374 rdma_set_device_sysfs_group(&dev->ib_dev, &mlx5_attr_group);
Aviv Heller7c34ec12018-08-23 13:47:53 +03006375 if (!mlx5_lag_is_roce(dev->mdev))
Jason Gunthorpee349f852018-09-25 16:58:09 -06006376 name = "mlx5_%d";
6377 else
6378 name = "mlx5_bond_%d";
Parav Panditea4baf72018-12-18 14:28:30 +02006379 return ib_register_device(&dev->ib_dev, name);
Mark Bloch16c19752018-01-01 13:06:58 +02006380}
6381
Mark Blochfb652d32019-03-28 15:27:42 +02006382static void mlx5_ib_stage_pre_ib_reg_umr_cleanup(struct mlx5_ib_dev *dev)
Mark Bloch42cea832018-03-14 09:14:15 +02006383{
6384 destroy_umrc_res(dev);
6385}
6386
Mark Blochfb652d32019-03-28 15:27:42 +02006387static void mlx5_ib_stage_ib_reg_cleanup(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02006388{
6389 ib_unregister_device(&dev->ib_dev);
6390}
6391
Mark Blochfb652d32019-03-28 15:27:42 +02006392static int mlx5_ib_stage_post_ib_reg_umr_init(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02006393{
6394 return create_umr_res(dev);
6395}
6396
Mark Bloch16c19752018-01-01 13:06:58 +02006397static int mlx5_ib_stage_delay_drop_init(struct mlx5_ib_dev *dev)
6398{
Maor Gottlieb03404e82017-05-30 10:29:13 +03006399 init_delay_drop(dev);
6400
Mark Bloch16c19752018-01-01 13:06:58 +02006401 return 0;
6402}
6403
6404static void mlx5_ib_stage_delay_drop_cleanup(struct mlx5_ib_dev *dev)
6405{
6406 cancel_delay_drop(dev);
6407}
6408
Saeed Mahameeddf097a22018-11-26 14:39:00 -08006409static int mlx5_ib_stage_dev_notifier_init(struct mlx5_ib_dev *dev)
6410{
6411 dev->mdev_events.notifier_call = mlx5_ib_event;
6412 mlx5_notifier_register(dev->mdev, &dev->mdev_events);
6413 return 0;
6414}
6415
6416static void mlx5_ib_stage_dev_notifier_cleanup(struct mlx5_ib_dev *dev)
6417{
6418 mlx5_notifier_unregister(dev->mdev, &dev->mdev_events);
6419}
6420
Leon Romanovsky81773ce2018-11-28 20:53:39 +02006421static int mlx5_ib_stage_devx_init(struct mlx5_ib_dev *dev)
6422{
6423 int uid;
6424
Yishai Hadasfb981532018-11-26 08:28:36 +02006425 uid = mlx5_ib_devx_create(dev, false);
Leon Romanovsky81773ce2018-11-28 20:53:39 +02006426 if (uid > 0)
6427 dev->devx_whitelist_uid = uid;
6428
6429 return 0;
6430}
6431static void mlx5_ib_stage_devx_cleanup(struct mlx5_ib_dev *dev)
6432{
6433 if (dev->devx_whitelist_uid)
6434 mlx5_ib_devx_destroy(dev, dev->devx_whitelist_uid);
6435}
6436
Mark Blochb5ca15a2018-01-23 11:16:30 +00006437void __mlx5_ib_remove(struct mlx5_ib_dev *dev,
6438 const struct mlx5_ib_profile *profile,
6439 int stage)
Mark Bloch16c19752018-01-01 13:06:58 +02006440{
6441 /* Number of stages to cleanup */
6442 while (stage) {
6443 stage--;
6444 if (profile->stage[stage].cleanup)
6445 profile->stage[stage].cleanup(dev);
6446 }
Mark Bloch4a6dc852019-03-28 15:27:34 +02006447
Mark Blochda796cc2019-03-28 15:27:35 +02006448 kfree(dev->port);
Mark Bloch4a6dc852019-03-28 15:27:34 +02006449 ib_dealloc_device(&dev->ib_dev);
Mark Bloch16c19752018-01-01 13:06:58 +02006450}
6451
Mark Blochb5ca15a2018-01-23 11:16:30 +00006452void *__mlx5_ib_add(struct mlx5_ib_dev *dev,
6453 const struct mlx5_ib_profile *profile)
Mark Bloch16c19752018-01-01 13:06:58 +02006454{
Mark Bloch16c19752018-01-01 13:06:58 +02006455 int err;
6456 int i;
Mark Bloch16c19752018-01-01 13:06:58 +02006457
Mark Bloch16c19752018-01-01 13:06:58 +02006458 for (i = 0; i < MLX5_IB_STAGE_MAX; i++) {
6459 if (profile->stage[i].init) {
6460 err = profile->stage[i].init(dev);
6461 if (err)
6462 goto err_out;
6463 }
6464 }
6465
6466 dev->profile = profile;
Eli Cohene126ba92013-07-07 17:25:49 +03006467 dev->ib_active = true;
6468
Jack Morgenstein9603b612014-07-28 23:30:22 +03006469 return dev;
Eli Cohene126ba92013-07-07 17:25:49 +03006470
Mark Bloch16c19752018-01-01 13:06:58 +02006471err_out:
6472 __mlx5_ib_remove(dev, profile, i);
Eli Cohene126ba92013-07-07 17:25:49 +03006473
Jack Morgenstein9603b612014-07-28 23:30:22 +03006474 return NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03006475}
6476
Mark Bloch16c19752018-01-01 13:06:58 +02006477static const struct mlx5_ib_profile pf_profile = {
6478 STAGE_CREATE(MLX5_IB_STAGE_INIT,
6479 mlx5_ib_stage_init_init,
6480 mlx5_ib_stage_init_cleanup),
Mark Bloch9a4ca382018-01-16 14:42:35 +00006481 STAGE_CREATE(MLX5_IB_STAGE_FLOW_DB,
6482 mlx5_ib_stage_flow_db_init,
6483 mlx5_ib_stage_flow_db_cleanup),
Mark Bloch16c19752018-01-01 13:06:58 +02006484 STAGE_CREATE(MLX5_IB_STAGE_CAPS,
6485 mlx5_ib_stage_caps_init,
6486 NULL),
Mark Bloch8e6efa32017-11-06 12:22:13 +00006487 STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB,
6488 mlx5_ib_stage_non_default_cb,
6489 NULL),
Mark Bloch16c19752018-01-01 13:06:58 +02006490 STAGE_CREATE(MLX5_IB_STAGE_ROCE,
6491 mlx5_ib_stage_roce_init,
6492 mlx5_ib_stage_roce_cleanup),
Leon Romanovskyf3da6572018-11-28 20:53:41 +02006493 STAGE_CREATE(MLX5_IB_STAGE_SRQ,
6494 mlx5_init_srq_table,
6495 mlx5_cleanup_srq_table),
Mark Bloch16c19752018-01-01 13:06:58 +02006496 STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES,
6497 mlx5_ib_stage_dev_res_init,
6498 mlx5_ib_stage_dev_res_cleanup),
Saeed Mahameeddf097a22018-11-26 14:39:00 -08006499 STAGE_CREATE(MLX5_IB_STAGE_DEVICE_NOTIFIER,
6500 mlx5_ib_stage_dev_notifier_init,
6501 mlx5_ib_stage_dev_notifier_cleanup),
Mark Bloch16c19752018-01-01 13:06:58 +02006502 STAGE_CREATE(MLX5_IB_STAGE_ODP,
6503 mlx5_ib_stage_odp_init,
Saeed Mahameedd5d284b2018-11-19 10:52:41 -08006504 mlx5_ib_stage_odp_cleanup),
Mark Bloch16c19752018-01-01 13:06:58 +02006505 STAGE_CREATE(MLX5_IB_STAGE_COUNTERS,
6506 mlx5_ib_stage_counters_init,
6507 mlx5_ib_stage_counters_cleanup),
6508 STAGE_CREATE(MLX5_IB_STAGE_CONG_DEBUGFS,
6509 mlx5_ib_stage_cong_debugfs_init,
6510 mlx5_ib_stage_cong_debugfs_cleanup),
6511 STAGE_CREATE(MLX5_IB_STAGE_UAR,
6512 mlx5_ib_stage_uar_init,
6513 mlx5_ib_stage_uar_cleanup),
6514 STAGE_CREATE(MLX5_IB_STAGE_BFREG,
6515 mlx5_ib_stage_bfrag_init,
6516 mlx5_ib_stage_bfrag_cleanup),
Mark Bloch42cea832018-03-14 09:14:15 +02006517 STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR,
6518 NULL,
6519 mlx5_ib_stage_pre_ib_reg_umr_cleanup),
Leon Romanovsky81773ce2018-11-28 20:53:39 +02006520 STAGE_CREATE(MLX5_IB_STAGE_WHITELIST_UID,
6521 mlx5_ib_stage_devx_init,
6522 mlx5_ib_stage_devx_cleanup),
Mark Bloch16c19752018-01-01 13:06:58 +02006523 STAGE_CREATE(MLX5_IB_STAGE_IB_REG,
6524 mlx5_ib_stage_ib_reg_init,
6525 mlx5_ib_stage_ib_reg_cleanup),
Mark Bloch42cea832018-03-14 09:14:15 +02006526 STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR,
6527 mlx5_ib_stage_post_ib_reg_umr_init,
6528 NULL),
Mark Bloch16c19752018-01-01 13:06:58 +02006529 STAGE_CREATE(MLX5_IB_STAGE_DELAY_DROP,
6530 mlx5_ib_stage_delay_drop_init,
6531 mlx5_ib_stage_delay_drop_cleanup),
Mark Bloch16c19752018-01-01 13:06:58 +02006532};
6533
Bodong Wangf0666f12019-02-12 22:55:34 -08006534const struct mlx5_ib_profile uplink_rep_profile = {
Mark Blochb5ca15a2018-01-23 11:16:30 +00006535 STAGE_CREATE(MLX5_IB_STAGE_INIT,
6536 mlx5_ib_stage_init_init,
6537 mlx5_ib_stage_init_cleanup),
6538 STAGE_CREATE(MLX5_IB_STAGE_FLOW_DB,
6539 mlx5_ib_stage_flow_db_init,
6540 mlx5_ib_stage_flow_db_cleanup),
6541 STAGE_CREATE(MLX5_IB_STAGE_CAPS,
6542 mlx5_ib_stage_caps_init,
6543 NULL),
6544 STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB,
6545 mlx5_ib_stage_rep_non_default_cb,
6546 NULL),
6547 STAGE_CREATE(MLX5_IB_STAGE_ROCE,
6548 mlx5_ib_stage_rep_roce_init,
6549 mlx5_ib_stage_rep_roce_cleanup),
Leon Romanovskyf3da6572018-11-28 20:53:41 +02006550 STAGE_CREATE(MLX5_IB_STAGE_SRQ,
6551 mlx5_init_srq_table,
6552 mlx5_cleanup_srq_table),
Mark Blochb5ca15a2018-01-23 11:16:30 +00006553 STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES,
6554 mlx5_ib_stage_dev_res_init,
6555 mlx5_ib_stage_dev_res_cleanup),
Saeed Mahameeddf097a22018-11-26 14:39:00 -08006556 STAGE_CREATE(MLX5_IB_STAGE_DEVICE_NOTIFIER,
6557 mlx5_ib_stage_dev_notifier_init,
6558 mlx5_ib_stage_dev_notifier_cleanup),
Mark Blochb5ca15a2018-01-23 11:16:30 +00006559 STAGE_CREATE(MLX5_IB_STAGE_COUNTERS,
6560 mlx5_ib_stage_counters_init,
6561 mlx5_ib_stage_counters_cleanup),
6562 STAGE_CREATE(MLX5_IB_STAGE_UAR,
6563 mlx5_ib_stage_uar_init,
6564 mlx5_ib_stage_uar_cleanup),
6565 STAGE_CREATE(MLX5_IB_STAGE_BFREG,
6566 mlx5_ib_stage_bfrag_init,
6567 mlx5_ib_stage_bfrag_cleanup),
David S. Miller03fe2de2018-03-23 11:24:57 -04006568 STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR,
6569 NULL,
6570 mlx5_ib_stage_pre_ib_reg_umr_cleanup),
Mark Bloch7f575102019-03-28 15:46:25 +02006571 STAGE_CREATE(MLX5_IB_STAGE_WHITELIST_UID,
6572 mlx5_ib_stage_devx_init,
6573 mlx5_ib_stage_devx_cleanup),
Mark Blochb5ca15a2018-01-23 11:16:30 +00006574 STAGE_CREATE(MLX5_IB_STAGE_IB_REG,
6575 mlx5_ib_stage_ib_reg_init,
6576 mlx5_ib_stage_ib_reg_cleanup),
David S. Miller03fe2de2018-03-23 11:24:57 -04006577 STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR,
6578 mlx5_ib_stage_post_ib_reg_umr_init,
6579 NULL),
Mark Blochb5ca15a2018-01-23 11:16:30 +00006580};
6581
Leon Romanovskye3f1ed12018-07-08 12:55:43 +03006582static void *mlx5_ib_add_slave_port(struct mlx5_core_dev *mdev)
Daniel Jurgens32f69e42018-01-04 17:25:36 +02006583{
6584 struct mlx5_ib_multiport_info *mpi;
6585 struct mlx5_ib_dev *dev;
6586 bool bound = false;
6587 int err;
6588
6589 mpi = kzalloc(sizeof(*mpi), GFP_KERNEL);
6590 if (!mpi)
6591 return NULL;
6592
6593 mpi->mdev = mdev;
6594
6595 err = mlx5_query_nic_vport_system_image_guid(mdev,
6596 &mpi->sys_image_guid);
6597 if (err) {
6598 kfree(mpi);
6599 return NULL;
6600 }
6601
6602 mutex_lock(&mlx5_ib_multiport_mutex);
6603 list_for_each_entry(dev, &mlx5_ib_dev_list, ib_dev_list) {
6604 if (dev->sys_image_guid == mpi->sys_image_guid)
6605 bound = mlx5_ib_bind_slave_port(dev, mpi);
6606
6607 if (bound) {
6608 rdma_roce_rescan_device(&dev->ib_dev);
6609 break;
6610 }
6611 }
6612
6613 if (!bound) {
6614 list_add_tail(&mpi->list, &mlx5_ib_unaffiliated_port_list);
6615 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 +02006616 }
6617 mutex_unlock(&mlx5_ib_multiport_mutex);
6618
6619 return mpi;
6620}
6621
Mark Bloch16c19752018-01-01 13:06:58 +02006622static void *mlx5_ib_add(struct mlx5_core_dev *mdev)
6623{
Daniel Jurgens32f69e42018-01-04 17:25:36 +02006624 enum rdma_link_layer ll;
Mark Blochb5ca15a2018-01-23 11:16:30 +00006625 struct mlx5_ib_dev *dev;
Daniel Jurgens32f69e42018-01-04 17:25:36 +02006626 int port_type_cap;
Mark Blochda796cc2019-03-28 15:27:35 +02006627 int num_ports;
Daniel Jurgens32f69e42018-01-04 17:25:36 +02006628
Mark Blochb5ca15a2018-01-23 11:16:30 +00006629 printk_once(KERN_INFO "%s", mlx5_version);
6630
Bodong Wangf0666f12019-02-12 22:55:34 -08006631 if (MLX5_ESWITCH_MANAGER(mdev) &&
6632 mlx5_ib_eswitch_mode(mdev->priv.eswitch) == SRIOV_OFFLOADS) {
Mark Bloch5fb58c92019-03-28 15:46:27 +02006633 if (!mlx5_core_mp_enabled(mdev))
6634 mlx5_ib_register_vport_reps(mdev);
Bodong Wangf0666f12019-02-12 22:55:34 -08006635 return mdev;
6636 }
6637
Daniel Jurgens32f69e42018-01-04 17:25:36 +02006638 port_type_cap = MLX5_CAP_GEN(mdev, port_type);
6639 ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
6640
Leon Romanovskye3f1ed12018-07-08 12:55:43 +03006641 if (mlx5_core_is_mp_slave(mdev) && ll == IB_LINK_LAYER_ETHERNET)
6642 return mlx5_ib_add_slave_port(mdev);
Daniel Jurgens32f69e42018-01-04 17:25:36 +02006643
Mark Blochda796cc2019-03-28 15:27:35 +02006644 num_ports = max(MLX5_CAP_GEN(mdev, num_ports),
6645 MLX5_CAP_GEN(mdev, num_vhca_ports));
Leon Romanovsky459cc692019-01-30 12:49:11 +02006646 dev = ib_alloc_device(mlx5_ib_dev, ib_dev);
Mark Blochb5ca15a2018-01-23 11:16:30 +00006647 if (!dev)
6648 return NULL;
Mark Blochda796cc2019-03-28 15:27:35 +02006649 dev->port = kcalloc(num_ports, sizeof(*dev->port),
6650 GFP_KERNEL);
6651 if (!dev->port) {
6652 ib_dealloc_device((struct ib_device *)dev);
6653 return NULL;
6654 }
Mark Blochb5ca15a2018-01-23 11:16:30 +00006655
6656 dev->mdev = mdev;
Mark Blochda796cc2019-03-28 15:27:35 +02006657 dev->num_ports = num_ports;
Mark Blochb5ca15a2018-01-23 11:16:30 +00006658
Mark Blochb5ca15a2018-01-23 11:16:30 +00006659 return __mlx5_ib_add(dev, &pf_profile);
Mark Bloch16c19752018-01-01 13:06:58 +02006660}
6661
Jack Morgenstein9603b612014-07-28 23:30:22 +03006662static void mlx5_ib_remove(struct mlx5_core_dev *mdev, void *context)
Eli Cohene126ba92013-07-07 17:25:49 +03006663{
Daniel Jurgens32f69e42018-01-04 17:25:36 +02006664 struct mlx5_ib_multiport_info *mpi;
6665 struct mlx5_ib_dev *dev;
Haggai Eran6aec21f2014-12-11 17:04:23 +02006666
Bodong Wangf0666f12019-02-12 22:55:34 -08006667 if (MLX5_ESWITCH_MANAGER(mdev) && context == mdev) {
6668 mlx5_ib_unregister_vport_reps(mdev);
6669 return;
6670 }
6671
Daniel Jurgens32f69e42018-01-04 17:25:36 +02006672 if (mlx5_core_is_mp_slave(mdev)) {
6673 mpi = context;
6674 mutex_lock(&mlx5_ib_multiport_mutex);
6675 if (mpi->ibdev)
6676 mlx5_ib_unbind_slave_port(mpi->ibdev, mpi);
6677 list_del(&mpi->list);
6678 mutex_unlock(&mlx5_ib_multiport_mutex);
6679 return;
6680 }
6681
6682 dev = context;
Bodong Wangf0666f12019-02-12 22:55:34 -08006683 __mlx5_ib_remove(dev, dev->profile, MLX5_IB_STAGE_MAX);
Eli Cohene126ba92013-07-07 17:25:49 +03006684}
6685
Jack Morgenstein9603b612014-07-28 23:30:22 +03006686static struct mlx5_interface mlx5_ib_interface = {
6687 .add = mlx5_ib_add,
6688 .remove = mlx5_ib_remove,
Saeed Mahameed64613d942015-04-02 17:07:34 +03006689 .protocol = MLX5_INTERFACE_PROTOCOL_IB,
Eli Cohene126ba92013-07-07 17:25:49 +03006690};
6691
Ilya Lesokhinc44ef992018-03-13 15:18:48 +02006692unsigned long mlx5_ib_get_xlt_emergency_page(void)
6693{
6694 mutex_lock(&xlt_emergency_page_mutex);
6695 return xlt_emergency_page;
6696}
6697
6698void mlx5_ib_put_xlt_emergency_page(void)
6699{
6700 mutex_unlock(&xlt_emergency_page_mutex);
6701}
6702
Eli Cohene126ba92013-07-07 17:25:49 +03006703static int __init mlx5_ib_init(void)
6704{
Haggai Eran6aec21f2014-12-11 17:04:23 +02006705 int err;
6706
Ilya Lesokhinc44ef992018-03-13 15:18:48 +02006707 xlt_emergency_page = __get_free_page(GFP_KERNEL);
6708 if (!xlt_emergency_page)
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02006709 return -ENOMEM;
6710
Ilya Lesokhinc44ef992018-03-13 15:18:48 +02006711 mutex_init(&xlt_emergency_page_mutex);
6712
6713 mlx5_ib_event_wq = alloc_ordered_workqueue("mlx5_ib_event_wq", 0);
6714 if (!mlx5_ib_event_wq) {
6715 free_page(xlt_emergency_page);
6716 return -ENOMEM;
6717 }
6718
Artemy Kovalyov81713d32017-01-18 16:58:11 +02006719 mlx5_ib_odp_init();
Jack Morgenstein9603b612014-07-28 23:30:22 +03006720
Haggai Eran6aec21f2014-12-11 17:04:23 +02006721 err = mlx5_register_interface(&mlx5_ib_interface);
Haggai Eran6aec21f2014-12-11 17:04:23 +02006722
6723 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03006724}
6725
6726static void __exit mlx5_ib_cleanup(void)
6727{
Jack Morgenstein9603b612014-07-28 23:30:22 +03006728 mlx5_unregister_interface(&mlx5_ib_interface);
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02006729 destroy_workqueue(mlx5_ib_event_wq);
Ilya Lesokhinc44ef992018-03-13 15:18:48 +02006730 mutex_destroy(&xlt_emergency_page_mutex);
6731 free_page(xlt_emergency_page);
Eli Cohene126ba92013-07-07 17:25:49 +03006732}
6733
6734module_init(mlx5_ib_init);
6735module_exit(mlx5_ib_cleanup);