blob: 82ad0faf8007340a652f3b9e421f1fae34e5279f [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>
Guy Levi37aa5c32016-04-27 16:49:50 +030041#if defined(CONFIG_X86)
42#include <asm/pat.h>
43#endif
Eli Cohene126ba92013-07-07 17:25:49 +030044#include <linux/sched.h>
Ingo Molnar6e84f312017-02-08 18:51:29 +010045#include <linux/sched/mm.h>
Ingo Molnar0881e7b2017-02-05 15:30:50 +010046#include <linux/sched/task.h>
Maor Gottlieb7c2344c2016-06-17 14:56:44 +030047#include <linux/delay.h>
Eli Cohene126ba92013-07-07 17:25:49 +030048#include <rdma/ib_user_verbs.h>
Achiad Shochat3f89a642015-12-23 18:47:21 +020049#include <rdma/ib_addr.h>
Achiad Shochat2811ba52015-12-23 18:47:24 +020050#include <rdma/ib_cache.h>
Achiad Shochatada68c32016-02-22 18:17:23 +020051#include <linux/mlx5/port.h>
Majd Dibbiny1b5daf12015-06-04 19:30:46 +030052#include <linux/mlx5/vport.h>
Pravin Shedge72c7fe92017-12-06 22:19:39 +053053#include <linux/mlx5/fs.h>
Maor Gottlieb7c2344c2016-06-17 14:56:44 +030054#include <linux/list.h>
Eli Cohene126ba92013-07-07 17:25:49 +030055#include <rdma/ib_smi.h>
56#include <rdma/ib_umem.h>
Maor Gottlieb038d2ef2016-01-11 10:26:07 +020057#include <linux/in.h>
58#include <linux/etherdevice.h>
Eli Cohene126ba92013-07-07 17:25:49 +030059#include "mlx5_ib.h"
Mark Blochfc385b72018-01-16 14:34:48 +000060#include "ib_rep.h"
Parav Pandite1f24a72017-04-16 07:29:29 +030061#include "cmd.h"
Boris Pismenny3346c482017-08-20 15:13:08 +030062#include <linux/mlx5/fs_helpers.h>
Matan Barak8c846602018-03-28 09:27:41 +030063#include <rdma/uverbs_std_types.h>
64
65#define UVERBS_MODULE_NAME mlx5_ib
66#include <rdma/uverbs_named_ioctl.h>
Eli Cohene126ba92013-07-07 17:25:49 +030067
68#define DRIVER_NAME "mlx5_ib"
Tariq Toukanb3599112017-02-22 17:45:46 +020069#define DRIVER_VERSION "5.0-0"
Eli Cohene126ba92013-07-07 17:25:49 +030070
71MODULE_AUTHOR("Eli Cohen <eli@mellanox.com>");
72MODULE_DESCRIPTION("Mellanox Connect-IB HCA IB driver");
73MODULE_LICENSE("Dual BSD/GPL");
Eli Cohene126ba92013-07-07 17:25:49 +030074
Eli Cohene126ba92013-07-07 17:25:49 +030075static char mlx5_version[] =
76 DRIVER_NAME ": Mellanox Connect-IB Infiniband driver v"
Tariq Toukanb3599112017-02-22 17:45:46 +020077 DRIVER_VERSION "\n";
Eli Cohene126ba92013-07-07 17:25:49 +030078
Daniel Jurgensd69a24e2018-01-04 17:25:37 +020079struct mlx5_ib_event_work {
80 struct work_struct work;
81 struct mlx5_core_dev *dev;
82 void *context;
83 enum mlx5_dev_event event;
84 unsigned long param;
85};
86
Eran Ben Elishada7525d2015-12-14 16:34:10 +020087enum {
88 MLX5_ATOMIC_SIZE_QP_8BYTES = 1 << 3,
89};
Majd Dibbiny1b5daf12015-06-04 19:30:46 +030090
Daniel Jurgensd69a24e2018-01-04 17:25:37 +020091static struct workqueue_struct *mlx5_ib_event_wq;
Daniel Jurgens32f69e42018-01-04 17:25:36 +020092static LIST_HEAD(mlx5_ib_unaffiliated_port_list);
93static LIST_HEAD(mlx5_ib_dev_list);
94/*
95 * This mutex should be held when accessing either of the above lists
96 */
97static DEFINE_MUTEX(mlx5_ib_multiport_mutex);
98
Ilya Lesokhinc44ef992018-03-13 15:18:48 +020099/* We can't use an array for xlt_emergency_page because dma_map_single
100 * doesn't work on kernel modules memory
101 */
102static unsigned long xlt_emergency_page;
103static struct mutex xlt_emergency_page_mutex;
104
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200105struct mlx5_ib_dev *mlx5_ib_get_ibdev_from_mpi(struct mlx5_ib_multiport_info *mpi)
106{
107 struct mlx5_ib_dev *dev;
108
109 mutex_lock(&mlx5_ib_multiport_mutex);
110 dev = mpi->ibdev;
111 mutex_unlock(&mlx5_ib_multiport_mutex);
112 return dev;
113}
114
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300115static enum rdma_link_layer
Achiad Shochatebd61f62015-12-23 18:47:16 +0200116mlx5_port_type_cap_to_rdma_ll(int port_type_cap)
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300117{
Achiad Shochatebd61f62015-12-23 18:47:16 +0200118 switch (port_type_cap) {
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300119 case MLX5_CAP_PORT_TYPE_IB:
120 return IB_LINK_LAYER_INFINIBAND;
121 case MLX5_CAP_PORT_TYPE_ETH:
122 return IB_LINK_LAYER_ETHERNET;
123 default:
124 return IB_LINK_LAYER_UNSPECIFIED;
125 }
126}
127
Achiad Shochatebd61f62015-12-23 18:47:16 +0200128static enum rdma_link_layer
129mlx5_ib_port_link_layer(struct ib_device *device, u8 port_num)
130{
131 struct mlx5_ib_dev *dev = to_mdev(device);
132 int port_type_cap = MLX5_CAP_GEN(dev->mdev, port_type);
133
134 return mlx5_port_type_cap_to_rdma_ll(port_type_cap);
135}
136
Moni Shouafd65f1b2017-05-30 09:56:05 +0300137static int get_port_state(struct ib_device *ibdev,
138 u8 port_num,
139 enum ib_port_state *state)
140{
141 struct ib_port_attr attr;
142 int ret;
143
144 memset(&attr, 0, sizeof(attr));
Mark Bloch8e6efa32017-11-06 12:22:13 +0000145 ret = ibdev->query_port(ibdev, port_num, &attr);
Moni Shouafd65f1b2017-05-30 09:56:05 +0300146 if (!ret)
147 *state = attr.state;
148 return ret;
149}
150
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200151static int mlx5_netdev_event(struct notifier_block *this,
152 unsigned long event, void *ptr)
153{
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200154 struct mlx5_roce *roce = container_of(this, struct mlx5_roce, nb);
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200155 struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200156 u8 port_num = roce->native_port_num;
157 struct mlx5_core_dev *mdev;
158 struct mlx5_ib_dev *ibdev;
159
160 ibdev = roce->dev;
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200161 mdev = mlx5_ib_get_native_port_mdev(ibdev, port_num, NULL);
162 if (!mdev)
163 return NOTIFY_DONE;
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200164
Aviv Heller5ec8c832016-09-18 20:48:00 +0300165 switch (event) {
166 case NETDEV_REGISTER:
167 case NETDEV_UNREGISTER:
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200168 write_lock(&roce->netdev_lock);
Mark Blochbcf87f12018-01-16 15:02:36 +0000169 if (ibdev->rep) {
170 struct mlx5_eswitch *esw = ibdev->mdev->priv.eswitch;
171 struct net_device *rep_ndev;
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200172
Mark Blochbcf87f12018-01-16 15:02:36 +0000173 rep_ndev = mlx5_ib_get_rep_netdev(esw,
174 ibdev->rep->vport);
175 if (rep_ndev == ndev)
176 roce->netdev = (event == NETDEV_UNREGISTER) ?
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200177 NULL : ndev;
Mark Blochbcf87f12018-01-16 15:02:36 +0000178 } else if (ndev->dev.parent == &ibdev->mdev->pdev->dev) {
179 roce->netdev = (event == NETDEV_UNREGISTER) ?
180 NULL : ndev;
181 }
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200182 write_unlock(&roce->netdev_lock);
Aviv Heller5ec8c832016-09-18 20:48:00 +0300183 break;
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200184
Moni Shouafd65f1b2017-05-30 09:56:05 +0300185 case NETDEV_CHANGE:
Aviv Heller5ec8c832016-09-18 20:48:00 +0300186 case NETDEV_UP:
Aviv Heller88621df2016-09-18 20:48:02 +0300187 case NETDEV_DOWN: {
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200188 struct net_device *lag_ndev = mlx5_lag_get_roce_netdev(mdev);
Aviv Heller88621df2016-09-18 20:48:02 +0300189 struct net_device *upper = NULL;
190
191 if (lag_ndev) {
192 upper = netdev_master_upper_dev_get(lag_ndev);
193 dev_put(lag_ndev);
194 }
195
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200196 if ((upper == ndev || (!upper && ndev == roce->netdev))
Aviv Heller88621df2016-09-18 20:48:02 +0300197 && ibdev->ib_active) {
Bart Van Assche626bc022016-12-05 17:18:08 -0800198 struct ib_event ibev = { };
Moni Shouafd65f1b2017-05-30 09:56:05 +0300199 enum ib_port_state port_state;
Aviv Heller5ec8c832016-09-18 20:48:00 +0300200
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200201 if (get_port_state(&ibdev->ib_dev, port_num,
202 &port_state))
203 goto done;
Moni Shouafd65f1b2017-05-30 09:56:05 +0300204
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200205 if (roce->last_port_state == port_state)
206 goto done;
Moni Shouafd65f1b2017-05-30 09:56:05 +0300207
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200208 roce->last_port_state = port_state;
Aviv Heller5ec8c832016-09-18 20:48:00 +0300209 ibev.device = &ibdev->ib_dev;
Moni Shouafd65f1b2017-05-30 09:56:05 +0300210 if (port_state == IB_PORT_DOWN)
211 ibev.event = IB_EVENT_PORT_ERR;
212 else if (port_state == IB_PORT_ACTIVE)
213 ibev.event = IB_EVENT_PORT_ACTIVE;
214 else
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200215 goto done;
Moni Shouafd65f1b2017-05-30 09:56:05 +0300216
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200217 ibev.element.port_num = port_num;
Aviv Heller5ec8c832016-09-18 20:48:00 +0300218 ib_dispatch_event(&ibev);
219 }
220 break;
Aviv Heller88621df2016-09-18 20:48:02 +0300221 }
Aviv Heller5ec8c832016-09-18 20:48:00 +0300222
223 default:
224 break;
225 }
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200226done:
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200227 mlx5_ib_put_native_port_mdev(ibdev, port_num);
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200228 return NOTIFY_DONE;
229}
230
231static struct net_device *mlx5_ib_get_netdev(struct ib_device *device,
232 u8 port_num)
233{
234 struct mlx5_ib_dev *ibdev = to_mdev(device);
235 struct net_device *ndev;
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200236 struct mlx5_core_dev *mdev;
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200237
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200238 mdev = mlx5_ib_get_native_port_mdev(ibdev, port_num, NULL);
239 if (!mdev)
240 return NULL;
241
242 ndev = mlx5_lag_get_roce_netdev(mdev);
Aviv Heller88621df2016-09-18 20:48:02 +0300243 if (ndev)
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200244 goto out;
Aviv Heller88621df2016-09-18 20:48:02 +0300245
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200246 /* Ensure ndev does not disappear before we invoke dev_hold()
247 */
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200248 read_lock(&ibdev->roce[port_num - 1].netdev_lock);
249 ndev = ibdev->roce[port_num - 1].netdev;
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200250 if (ndev)
251 dev_hold(ndev);
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200252 read_unlock(&ibdev->roce[port_num - 1].netdev_lock);
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200253
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200254out:
255 mlx5_ib_put_native_port_mdev(ibdev, port_num);
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200256 return ndev;
257}
258
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200259struct mlx5_core_dev *mlx5_ib_get_native_port_mdev(struct mlx5_ib_dev *ibdev,
260 u8 ib_port_num,
261 u8 *native_port_num)
262{
263 enum rdma_link_layer ll = mlx5_ib_port_link_layer(&ibdev->ib_dev,
264 ib_port_num);
265 struct mlx5_core_dev *mdev = NULL;
266 struct mlx5_ib_multiport_info *mpi;
267 struct mlx5_ib_port *port;
268
Mark Bloch210b1f72018-03-05 20:09:47 +0200269 if (!mlx5_core_mp_enabled(ibdev->mdev) ||
270 ll != IB_LINK_LAYER_ETHERNET) {
271 if (native_port_num)
272 *native_port_num = ib_port_num;
273 return ibdev->mdev;
274 }
275
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200276 if (native_port_num)
277 *native_port_num = 1;
278
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200279 port = &ibdev->port[ib_port_num - 1];
280 if (!port)
281 return NULL;
282
283 spin_lock(&port->mp.mpi_lock);
284 mpi = ibdev->port[ib_port_num - 1].mp.mpi;
285 if (mpi && !mpi->unaffiliate) {
286 mdev = mpi->mdev;
287 /* If it's the master no need to refcount, it'll exist
288 * as long as the ib_dev exists.
289 */
290 if (!mpi->is_master)
291 mpi->mdev_refcnt++;
292 }
293 spin_unlock(&port->mp.mpi_lock);
294
295 return mdev;
296}
297
298void mlx5_ib_put_native_port_mdev(struct mlx5_ib_dev *ibdev, u8 port_num)
299{
300 enum rdma_link_layer ll = mlx5_ib_port_link_layer(&ibdev->ib_dev,
301 port_num);
302 struct mlx5_ib_multiport_info *mpi;
303 struct mlx5_ib_port *port;
304
305 if (!mlx5_core_mp_enabled(ibdev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
306 return;
307
308 port = &ibdev->port[port_num - 1];
309
310 spin_lock(&port->mp.mpi_lock);
311 mpi = ibdev->port[port_num - 1].mp.mpi;
312 if (mpi->is_master)
313 goto out;
314
315 mpi->mdev_refcnt--;
316 if (mpi->unaffiliate)
317 complete(&mpi->unref_comp);
318out:
319 spin_unlock(&port->mp.mpi_lock);
320}
321
Noa Osherovichf1b65df2017-04-20 20:53:33 +0300322static int translate_eth_proto_oper(u32 eth_proto_oper, u8 *active_speed,
323 u8 *active_width)
324{
325 switch (eth_proto_oper) {
326 case MLX5E_PROT_MASK(MLX5E_1000BASE_CX_SGMII):
327 case MLX5E_PROT_MASK(MLX5E_1000BASE_KX):
328 case MLX5E_PROT_MASK(MLX5E_100BASE_TX):
329 case MLX5E_PROT_MASK(MLX5E_1000BASE_T):
330 *active_width = IB_WIDTH_1X;
331 *active_speed = IB_SPEED_SDR;
332 break;
333 case MLX5E_PROT_MASK(MLX5E_10GBASE_T):
334 case MLX5E_PROT_MASK(MLX5E_10GBASE_CX4):
335 case MLX5E_PROT_MASK(MLX5E_10GBASE_KX4):
336 case MLX5E_PROT_MASK(MLX5E_10GBASE_KR):
337 case MLX5E_PROT_MASK(MLX5E_10GBASE_CR):
338 case MLX5E_PROT_MASK(MLX5E_10GBASE_SR):
339 case MLX5E_PROT_MASK(MLX5E_10GBASE_ER):
340 *active_width = IB_WIDTH_1X;
341 *active_speed = IB_SPEED_QDR;
342 break;
343 case MLX5E_PROT_MASK(MLX5E_25GBASE_CR):
344 case MLX5E_PROT_MASK(MLX5E_25GBASE_KR):
345 case MLX5E_PROT_MASK(MLX5E_25GBASE_SR):
346 *active_width = IB_WIDTH_1X;
347 *active_speed = IB_SPEED_EDR;
348 break;
349 case MLX5E_PROT_MASK(MLX5E_40GBASE_CR4):
350 case MLX5E_PROT_MASK(MLX5E_40GBASE_KR4):
351 case MLX5E_PROT_MASK(MLX5E_40GBASE_SR4):
352 case MLX5E_PROT_MASK(MLX5E_40GBASE_LR4):
353 *active_width = IB_WIDTH_4X;
354 *active_speed = IB_SPEED_QDR;
355 break;
356 case MLX5E_PROT_MASK(MLX5E_50GBASE_CR2):
357 case MLX5E_PROT_MASK(MLX5E_50GBASE_KR2):
358 case MLX5E_PROT_MASK(MLX5E_50GBASE_SR2):
359 *active_width = IB_WIDTH_1X;
360 *active_speed = IB_SPEED_HDR;
361 break;
362 case MLX5E_PROT_MASK(MLX5E_56GBASE_R4):
363 *active_width = IB_WIDTH_4X;
364 *active_speed = IB_SPEED_FDR;
365 break;
366 case MLX5E_PROT_MASK(MLX5E_100GBASE_CR4):
367 case MLX5E_PROT_MASK(MLX5E_100GBASE_SR4):
368 case MLX5E_PROT_MASK(MLX5E_100GBASE_KR4):
369 case MLX5E_PROT_MASK(MLX5E_100GBASE_LR4):
370 *active_width = IB_WIDTH_4X;
371 *active_speed = IB_SPEED_EDR;
372 break;
373 default:
374 return -EINVAL;
375 }
376
377 return 0;
378}
379
Ilan Tayari095b0922017-05-14 16:04:30 +0300380static int mlx5_query_port_roce(struct ib_device *device, u8 port_num,
381 struct ib_port_attr *props)
Achiad Shochat3f89a642015-12-23 18:47:21 +0200382{
383 struct mlx5_ib_dev *dev = to_mdev(device);
Colin Ian Kingda005f92018-01-09 15:55:43 +0000384 struct mlx5_core_dev *mdev;
Aviv Heller88621df2016-09-18 20:48:02 +0300385 struct net_device *ndev, *upper;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200386 enum ib_mtu ndev_ib_mtu;
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200387 bool put_mdev = true;
Leon Romanovskyc876a1b2016-01-09 13:06:25 +0200388 u16 qkey_viol_cntr;
Noa Osherovichf1b65df2017-04-20 20:53:33 +0300389 u32 eth_prot_oper;
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200390 u8 mdev_port_num;
Ilan Tayari095b0922017-05-14 16:04:30 +0300391 int err;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200392
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200393 mdev = mlx5_ib_get_native_port_mdev(dev, port_num, &mdev_port_num);
394 if (!mdev) {
395 /* This means the port isn't affiliated yet. Get the
396 * info for the master port instead.
397 */
398 put_mdev = false;
399 mdev = dev->mdev;
400 mdev_port_num = 1;
401 port_num = 1;
402 }
403
Noa Osherovichf1b65df2017-04-20 20:53:33 +0300404 /* Possible bad flows are checked before filling out props so in case
405 * of an error it will still be zeroed out.
Noa Osherovich50f22fd2017-04-20 20:53:32 +0300406 */
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200407 err = mlx5_query_port_eth_proto_oper(mdev, &eth_prot_oper,
408 mdev_port_num);
Ilan Tayari095b0922017-05-14 16:04:30 +0300409 if (err)
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200410 goto out;
Noa Osherovichf1b65df2017-04-20 20:53:33 +0300411
Honggang Li7672ed32018-03-16 10:37:13 +0800412 props->active_width = IB_WIDTH_4X;
413 props->active_speed = IB_SPEED_QDR;
414
Noa Osherovichf1b65df2017-04-20 20:53:33 +0300415 translate_eth_proto_oper(eth_prot_oper, &props->active_speed,
416 &props->active_width);
Achiad Shochat3f89a642015-12-23 18:47:21 +0200417
418 props->port_cap_flags |= IB_PORT_CM_SUP;
419 props->port_cap_flags |= IB_PORT_IP_BASED_GIDS;
420
421 props->gid_tbl_len = MLX5_CAP_ROCE(dev->mdev,
422 roce_address_table_size);
423 props->max_mtu = IB_MTU_4096;
424 props->max_msg_sz = 1 << MLX5_CAP_GEN(dev->mdev, log_max_msg);
425 props->pkey_tbl_len = 1;
426 props->state = IB_PORT_DOWN;
427 props->phys_state = 3;
428
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200429 mlx5_query_nic_vport_qkey_viol_cntr(mdev, &qkey_viol_cntr);
Leon Romanovskyc876a1b2016-01-09 13:06:25 +0200430 props->qkey_viol_cntr = qkey_viol_cntr;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200431
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200432 /* If this is a stub query for an unaffiliated port stop here */
433 if (!put_mdev)
434 goto out;
435
Achiad Shochat3f89a642015-12-23 18:47:21 +0200436 ndev = mlx5_ib_get_netdev(device, port_num);
437 if (!ndev)
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200438 goto out;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200439
Aviv Heller88621df2016-09-18 20:48:02 +0300440 if (mlx5_lag_is_active(dev->mdev)) {
441 rcu_read_lock();
442 upper = netdev_master_upper_dev_get_rcu(ndev);
443 if (upper) {
444 dev_put(ndev);
445 ndev = upper;
446 dev_hold(ndev);
447 }
448 rcu_read_unlock();
449 }
450
Achiad Shochat3f89a642015-12-23 18:47:21 +0200451 if (netif_running(ndev) && netif_carrier_ok(ndev)) {
452 props->state = IB_PORT_ACTIVE;
453 props->phys_state = 5;
454 }
455
456 ndev_ib_mtu = iboe_get_mtu(ndev->mtu);
457
458 dev_put(ndev);
459
460 props->active_mtu = min(props->max_mtu, ndev_ib_mtu);
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200461out:
462 if (put_mdev)
463 mlx5_ib_put_native_port_mdev(dev, port_num);
464 return err;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200465}
466
Ilan Tayari095b0922017-05-14 16:04:30 +0300467static int set_roce_addr(struct mlx5_ib_dev *dev, u8 port_num,
468 unsigned int index, const union ib_gid *gid,
469 const struct ib_gid_attr *attr)
Achiad Shochat3cca2602015-12-23 18:47:23 +0200470{
Ilan Tayari095b0922017-05-14 16:04:30 +0300471 enum ib_gid_type gid_type = IB_GID_TYPE_IB;
472 u8 roce_version = 0;
473 u8 roce_l3_type = 0;
474 bool vlan = false;
475 u8 mac[ETH_ALEN];
476 u16 vlan_id = 0;
Achiad Shochat3cca2602015-12-23 18:47:23 +0200477
Ilan Tayari095b0922017-05-14 16:04:30 +0300478 if (gid) {
479 gid_type = attr->gid_type;
480 ether_addr_copy(mac, attr->ndev->dev_addr);
Achiad Shochat3cca2602015-12-23 18:47:23 +0200481
Ilan Tayari095b0922017-05-14 16:04:30 +0300482 if (is_vlan_dev(attr->ndev)) {
483 vlan = true;
484 vlan_id = vlan_dev_vlan_id(attr->ndev);
485 }
Achiad Shochat3cca2602015-12-23 18:47:23 +0200486 }
487
Ilan Tayari095b0922017-05-14 16:04:30 +0300488 switch (gid_type) {
Achiad Shochat3cca2602015-12-23 18:47:23 +0200489 case IB_GID_TYPE_IB:
Ilan Tayari095b0922017-05-14 16:04:30 +0300490 roce_version = MLX5_ROCE_VERSION_1;
Achiad Shochat3cca2602015-12-23 18:47:23 +0200491 break;
492 case IB_GID_TYPE_ROCE_UDP_ENCAP:
Ilan Tayari095b0922017-05-14 16:04:30 +0300493 roce_version = MLX5_ROCE_VERSION_2;
494 if (ipv6_addr_v4mapped((void *)gid))
495 roce_l3_type = MLX5_ROCE_L3_TYPE_IPV4;
496 else
497 roce_l3_type = MLX5_ROCE_L3_TYPE_IPV6;
Achiad Shochat3cca2602015-12-23 18:47:23 +0200498 break;
499
500 default:
Ilan Tayari095b0922017-05-14 16:04:30 +0300501 mlx5_ib_warn(dev, "Unexpected GID type %u\n", gid_type);
Achiad Shochat3cca2602015-12-23 18:47:23 +0200502 }
503
Ilan Tayari095b0922017-05-14 16:04:30 +0300504 return mlx5_core_roce_gid_set(dev->mdev, index, roce_version,
505 roce_l3_type, gid->raw, mac, vlan,
Daniel Jurgenscfe4e372018-01-04 17:25:41 +0200506 vlan_id, port_num);
Achiad Shochat3cca2602015-12-23 18:47:23 +0200507}
508
Parav Pandit414448d2018-04-01 15:08:24 +0300509static int mlx5_ib_add_gid(const union ib_gid *gid,
Achiad Shochat3cca2602015-12-23 18:47:23 +0200510 const struct ib_gid_attr *attr,
511 __always_unused void **context)
512{
Parav Pandit414448d2018-04-01 15:08:24 +0300513 return set_roce_addr(to_mdev(attr->device), attr->port_num,
514 attr->index, gid, attr);
Achiad Shochat3cca2602015-12-23 18:47:23 +0200515}
516
Parav Pandit414448d2018-04-01 15:08:24 +0300517static int mlx5_ib_del_gid(const struct ib_gid_attr *attr,
518 __always_unused void **context)
Achiad Shochat3cca2602015-12-23 18:47:23 +0200519{
Parav Pandit414448d2018-04-01 15:08:24 +0300520 return set_roce_addr(to_mdev(attr->device), attr->port_num,
521 attr->index, NULL, NULL);
Achiad Shochat3cca2602015-12-23 18:47:23 +0200522}
523
Achiad Shochat2811ba52015-12-23 18:47:24 +0200524__be16 mlx5_get_roce_udp_sport(struct mlx5_ib_dev *dev, u8 port_num,
525 int index)
526{
527 struct ib_gid_attr attr;
528 union ib_gid gid;
529
530 if (ib_get_cached_gid(&dev->ib_dev, port_num, index, &gid, &attr))
531 return 0;
532
Achiad Shochat2811ba52015-12-23 18:47:24 +0200533 dev_put(attr.ndev);
534
535 if (attr.gid_type != IB_GID_TYPE_ROCE_UDP_ENCAP)
536 return 0;
537
538 return cpu_to_be16(MLX5_CAP_ROCE(dev->mdev, r_roce_min_src_udp_port));
539}
540
Majd Dibbinyed884512017-01-18 14:10:35 +0200541int mlx5_get_roce_gid_type(struct mlx5_ib_dev *dev, u8 port_num,
542 int index, enum ib_gid_type *gid_type)
543{
544 struct ib_gid_attr attr;
545 union ib_gid gid;
546 int ret;
547
548 ret = ib_get_cached_gid(&dev->ib_dev, port_num, index, &gid, &attr);
549 if (ret)
550 return ret;
551
Majd Dibbinyed884512017-01-18 14:10:35 +0200552 dev_put(attr.ndev);
553
554 *gid_type = attr.gid_type;
555
556 return 0;
557}
558
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300559static int mlx5_use_mad_ifc(struct mlx5_ib_dev *dev)
560{
Noa Osherovich7fae6652016-09-12 19:16:23 +0300561 if (MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_IB)
562 return !MLX5_CAP_GEN(dev->mdev, ib_virt);
563 return 0;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300564}
565
566enum {
567 MLX5_VPORT_ACCESS_METHOD_MAD,
568 MLX5_VPORT_ACCESS_METHOD_HCA,
569 MLX5_VPORT_ACCESS_METHOD_NIC,
570};
571
572static int mlx5_get_vport_access_method(struct ib_device *ibdev)
573{
574 if (mlx5_use_mad_ifc(to_mdev(ibdev)))
575 return MLX5_VPORT_ACCESS_METHOD_MAD;
576
Achiad Shochatebd61f62015-12-23 18:47:16 +0200577 if (mlx5_ib_port_link_layer(ibdev, 1) ==
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300578 IB_LINK_LAYER_ETHERNET)
579 return MLX5_VPORT_ACCESS_METHOD_NIC;
580
581 return MLX5_VPORT_ACCESS_METHOD_HCA;
582}
583
Eran Ben Elishada7525d2015-12-14 16:34:10 +0200584static void get_atomic_caps(struct mlx5_ib_dev *dev,
Moni Shoua776a3902018-01-02 16:19:33 +0200585 u8 atomic_size_qp,
Eran Ben Elishada7525d2015-12-14 16:34:10 +0200586 struct ib_device_attr *props)
587{
588 u8 tmp;
589 u8 atomic_operations = MLX5_CAP_ATOMIC(dev->mdev, atomic_operations);
Eran Ben Elishada7525d2015-12-14 16:34:10 +0200590 u8 atomic_req_8B_endianness_mode =
Or Gerlitzbd108382017-05-28 15:24:17 +0300591 MLX5_CAP_ATOMIC(dev->mdev, atomic_req_8B_endianness_mode);
Eran Ben Elishada7525d2015-12-14 16:34:10 +0200592
593 /* Check if HW supports 8 bytes standard atomic operations and capable
594 * of host endianness respond
595 */
596 tmp = MLX5_ATOMIC_OPS_CMP_SWAP | MLX5_ATOMIC_OPS_FETCH_ADD;
597 if (((atomic_operations & tmp) == tmp) &&
598 (atomic_size_qp & MLX5_ATOMIC_SIZE_QP_8BYTES) &&
599 (atomic_req_8B_endianness_mode)) {
600 props->atomic_cap = IB_ATOMIC_HCA;
601 } else {
602 props->atomic_cap = IB_ATOMIC_NONE;
603 }
604}
605
Moni Shoua776a3902018-01-02 16:19:33 +0200606static void get_atomic_caps_qp(struct mlx5_ib_dev *dev,
607 struct ib_device_attr *props)
608{
609 u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_qp);
610
611 get_atomic_caps(dev, atomic_size_qp, props);
612}
613
614static void get_atomic_caps_dc(struct mlx5_ib_dev *dev,
615 struct ib_device_attr *props)
616{
617 u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_dc);
618
619 get_atomic_caps(dev, atomic_size_qp, props);
620}
621
622bool mlx5_ib_dc_atomic_is_supported(struct mlx5_ib_dev *dev)
623{
624 struct ib_device_attr props = {};
625
626 get_atomic_caps_dc(dev, &props);
627 return (props.atomic_cap == IB_ATOMIC_HCA) ? true : false;
628}
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300629static int mlx5_query_system_image_guid(struct ib_device *ibdev,
630 __be64 *sys_image_guid)
631{
632 struct mlx5_ib_dev *dev = to_mdev(ibdev);
633 struct mlx5_core_dev *mdev = dev->mdev;
634 u64 tmp;
635 int err;
636
637 switch (mlx5_get_vport_access_method(ibdev)) {
638 case MLX5_VPORT_ACCESS_METHOD_MAD:
639 return mlx5_query_mad_ifc_system_image_guid(ibdev,
640 sys_image_guid);
641
642 case MLX5_VPORT_ACCESS_METHOD_HCA:
643 err = mlx5_query_hca_vport_system_image_guid(mdev, &tmp);
Achiad Shochat3f89a642015-12-23 18:47:21 +0200644 break;
645
646 case MLX5_VPORT_ACCESS_METHOD_NIC:
647 err = mlx5_query_nic_vport_system_image_guid(mdev, &tmp);
648 break;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300649
650 default:
651 return -EINVAL;
652 }
Achiad Shochat3f89a642015-12-23 18:47:21 +0200653
654 if (!err)
655 *sys_image_guid = cpu_to_be64(tmp);
656
657 return err;
658
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300659}
660
661static int mlx5_query_max_pkeys(struct ib_device *ibdev,
662 u16 *max_pkeys)
663{
664 struct mlx5_ib_dev *dev = to_mdev(ibdev);
665 struct mlx5_core_dev *mdev = dev->mdev;
666
667 switch (mlx5_get_vport_access_method(ibdev)) {
668 case MLX5_VPORT_ACCESS_METHOD_MAD:
669 return mlx5_query_mad_ifc_max_pkeys(ibdev, max_pkeys);
670
671 case MLX5_VPORT_ACCESS_METHOD_HCA:
672 case MLX5_VPORT_ACCESS_METHOD_NIC:
673 *max_pkeys = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev,
674 pkey_table_size));
675 return 0;
676
677 default:
678 return -EINVAL;
679 }
680}
681
682static int mlx5_query_vendor_id(struct ib_device *ibdev,
683 u32 *vendor_id)
684{
685 struct mlx5_ib_dev *dev = to_mdev(ibdev);
686
687 switch (mlx5_get_vport_access_method(ibdev)) {
688 case MLX5_VPORT_ACCESS_METHOD_MAD:
689 return mlx5_query_mad_ifc_vendor_id(ibdev, vendor_id);
690
691 case MLX5_VPORT_ACCESS_METHOD_HCA:
692 case MLX5_VPORT_ACCESS_METHOD_NIC:
693 return mlx5_core_query_vendor_id(dev->mdev, vendor_id);
694
695 default:
696 return -EINVAL;
697 }
698}
699
700static int mlx5_query_node_guid(struct mlx5_ib_dev *dev,
701 __be64 *node_guid)
702{
703 u64 tmp;
704 int err;
705
706 switch (mlx5_get_vport_access_method(&dev->ib_dev)) {
707 case MLX5_VPORT_ACCESS_METHOD_MAD:
708 return mlx5_query_mad_ifc_node_guid(dev, node_guid);
709
710 case MLX5_VPORT_ACCESS_METHOD_HCA:
711 err = mlx5_query_hca_vport_node_guid(dev->mdev, &tmp);
Achiad Shochat3f89a642015-12-23 18:47:21 +0200712 break;
713
714 case MLX5_VPORT_ACCESS_METHOD_NIC:
715 err = mlx5_query_nic_vport_node_guid(dev->mdev, &tmp);
716 break;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300717
718 default:
719 return -EINVAL;
720 }
Achiad Shochat3f89a642015-12-23 18:47:21 +0200721
722 if (!err)
723 *node_guid = cpu_to_be64(tmp);
724
725 return err;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300726}
727
728struct mlx5_reg_node_desc {
Yuval Shaiabd99fde2016-08-25 10:57:07 -0700729 u8 desc[IB_DEVICE_NODE_DESC_MAX];
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300730};
731
732static int mlx5_query_node_desc(struct mlx5_ib_dev *dev, char *node_desc)
733{
734 struct mlx5_reg_node_desc in;
735
736 if (mlx5_use_mad_ifc(dev))
737 return mlx5_query_mad_ifc_node_desc(dev, node_desc);
738
739 memset(&in, 0, sizeof(in));
740
741 return mlx5_core_access_reg(dev->mdev, &in, sizeof(in), node_desc,
742 sizeof(struct mlx5_reg_node_desc),
743 MLX5_REG_NODE_DESC, 0, 0);
744}
745
Eli Cohene126ba92013-07-07 17:25:49 +0300746static int mlx5_ib_query_device(struct ib_device *ibdev,
Matan Barak2528e332015-06-11 16:35:25 +0300747 struct ib_device_attr *props,
748 struct ib_udata *uhw)
Eli Cohene126ba92013-07-07 17:25:49 +0300749{
750 struct mlx5_ib_dev *dev = to_mdev(ibdev);
Saeed Mahameed938fe832015-05-28 22:28:41 +0300751 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +0300752 int err = -ENOMEM;
Eli Cohen288c01b2016-10-27 16:36:45 +0300753 int max_sq_desc;
Eli Cohene126ba92013-07-07 17:25:49 +0300754 int max_rq_sg;
755 int max_sq_sg;
Sagi Grimberge0238a62015-07-21 14:40:12 +0300756 u64 min_page_size = 1ull << MLX5_CAP_GEN(mdev, log_pg_sz);
Daniel Jurgens85c7c012018-01-04 17:25:43 +0200757 bool raw_support = !mlx5_core_mp_enabled(mdev);
Bodong Wang402ca532016-06-17 15:02:20 +0300758 struct mlx5_ib_query_device_resp resp = {};
759 size_t resp_len;
760 u64 max_tso;
Eli Cohene126ba92013-07-07 17:25:49 +0300761
Bodong Wang402ca532016-06-17 15:02:20 +0300762 resp_len = sizeof(resp.comp_mask) + sizeof(resp.response_length);
763 if (uhw->outlen && uhw->outlen < resp_len)
764 return -EINVAL;
765 else
766 resp.response_length = resp_len;
767
768 if (uhw->inlen && !ib_is_udata_cleared(uhw, 0, uhw->inlen))
Matan Barak2528e332015-06-11 16:35:25 +0300769 return -EINVAL;
770
Eli Cohene126ba92013-07-07 17:25:49 +0300771 memset(props, 0, sizeof(*props));
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300772 err = mlx5_query_system_image_guid(ibdev,
773 &props->sys_image_guid);
774 if (err)
775 return err;
776
777 err = mlx5_query_max_pkeys(ibdev, &props->max_pkeys);
778 if (err)
779 return err;
780
781 err = mlx5_query_vendor_id(ibdev, &props->vendor_id);
782 if (err)
783 return err;
Eli Cohene126ba92013-07-07 17:25:49 +0300784
Jack Morgenstein9603b612014-07-28 23:30:22 +0300785 props->fw_ver = ((u64)fw_rev_maj(dev->mdev) << 32) |
786 (fw_rev_min(dev->mdev) << 16) |
787 fw_rev_sub(dev->mdev);
Eli Cohene126ba92013-07-07 17:25:49 +0300788 props->device_cap_flags = IB_DEVICE_CHANGE_PHY_PORT |
789 IB_DEVICE_PORT_ACTIVE_EVENT |
790 IB_DEVICE_SYS_IMAGE_GUID |
Eli Cohen1a4c3a32014-02-06 17:41:25 +0200791 IB_DEVICE_RC_RNR_NAK_GEN;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300792
793 if (MLX5_CAP_GEN(mdev, pkv))
Eli Cohene126ba92013-07-07 17:25:49 +0300794 props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300795 if (MLX5_CAP_GEN(mdev, qkv))
Eli Cohene126ba92013-07-07 17:25:49 +0300796 props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300797 if (MLX5_CAP_GEN(mdev, apm))
Eli Cohene126ba92013-07-07 17:25:49 +0300798 props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300799 if (MLX5_CAP_GEN(mdev, xrc))
Eli Cohene126ba92013-07-07 17:25:49 +0300800 props->device_cap_flags |= IB_DEVICE_XRC;
Matan Barakd2370e02016-02-29 18:05:30 +0200801 if (MLX5_CAP_GEN(mdev, imaicl)) {
802 props->device_cap_flags |= IB_DEVICE_MEM_WINDOW |
803 IB_DEVICE_MEM_WINDOW_TYPE_2B;
804 props->max_mw = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
Sagi Grimbergb005d312016-02-29 19:07:33 +0200805 /* We support 'Gappy' memory registration too */
806 props->device_cap_flags |= IB_DEVICE_SG_GAPS_REG;
Matan Barakd2370e02016-02-29 18:05:30 +0200807 }
Eli Cohene126ba92013-07-07 17:25:49 +0300808 props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300809 if (MLX5_CAP_GEN(mdev, sho)) {
Sagi Grimberg2dea9092014-02-23 14:19:13 +0200810 props->device_cap_flags |= IB_DEVICE_SIGNATURE_HANDOVER;
811 /* At this stage no support for signature handover */
812 props->sig_prot_cap = IB_PROT_T10DIF_TYPE_1 |
813 IB_PROT_T10DIF_TYPE_2 |
814 IB_PROT_T10DIF_TYPE_3;
815 props->sig_guard_cap = IB_GUARD_T10DIF_CRC |
816 IB_GUARD_T10DIF_CSUM;
817 }
Saeed Mahameed938fe832015-05-28 22:28:41 +0300818 if (MLX5_CAP_GEN(mdev, block_lb_mc))
Eli Cohenf360d882014-04-02 00:10:16 +0300819 props->device_cap_flags |= IB_DEVICE_BLOCK_MULTICAST_LOOPBACK;
Eli Cohene126ba92013-07-07 17:25:49 +0300820
Daniel Jurgens85c7c012018-01-04 17:25:43 +0200821 if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) && raw_support) {
Noa Osheroviche8161332017-01-18 15:40:01 +0200822 if (MLX5_CAP_ETH(mdev, csum_cap)) {
823 /* Legacy bit to support old userspace libraries */
Bodong Wang88115fe2015-12-18 13:53:20 +0200824 props->device_cap_flags |= IB_DEVICE_RAW_IP_CSUM;
Noa Osheroviche8161332017-01-18 15:40:01 +0200825 props->raw_packet_caps |= IB_RAW_PACKET_CAP_IP_CSUM;
826 }
827
828 if (MLX5_CAP_ETH(dev->mdev, vlan_cap))
829 props->raw_packet_caps |=
830 IB_RAW_PACKET_CAP_CVLAN_STRIPPING;
Bodong Wang88115fe2015-12-18 13:53:20 +0200831
Bodong Wang402ca532016-06-17 15:02:20 +0300832 if (field_avail(typeof(resp), tso_caps, uhw->outlen)) {
833 max_tso = MLX5_CAP_ETH(mdev, max_lso_cap);
834 if (max_tso) {
835 resp.tso_caps.max_tso = 1 << max_tso;
836 resp.tso_caps.supported_qpts |=
837 1 << IB_QPT_RAW_PACKET;
838 resp.response_length += sizeof(resp.tso_caps);
839 }
840 }
Yishai Hadas31f69a82016-08-28 11:28:45 +0300841
842 if (field_avail(typeof(resp), rss_caps, uhw->outlen)) {
843 resp.rss_caps.rx_hash_function =
844 MLX5_RX_HASH_FUNC_TOEPLITZ;
845 resp.rss_caps.rx_hash_fields_mask =
846 MLX5_RX_HASH_SRC_IPV4 |
847 MLX5_RX_HASH_DST_IPV4 |
848 MLX5_RX_HASH_SRC_IPV6 |
849 MLX5_RX_HASH_DST_IPV6 |
850 MLX5_RX_HASH_SRC_PORT_TCP |
851 MLX5_RX_HASH_DST_PORT_TCP |
852 MLX5_RX_HASH_SRC_PORT_UDP |
Maor Gottlieb4e2b53a2017-12-24 14:51:25 +0200853 MLX5_RX_HASH_DST_PORT_UDP |
854 MLX5_RX_HASH_INNER;
Yishai Hadas31f69a82016-08-28 11:28:45 +0300855 resp.response_length += sizeof(resp.rss_caps);
856 }
857 } else {
858 if (field_avail(typeof(resp), tso_caps, uhw->outlen))
859 resp.response_length += sizeof(resp.tso_caps);
860 if (field_avail(typeof(resp), rss_caps, uhw->outlen))
861 resp.response_length += sizeof(resp.rss_caps);
Bodong Wang402ca532016-06-17 15:02:20 +0300862 }
863
Erez Shitritf0313962016-02-21 16:27:17 +0200864 if (MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) {
865 props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
866 props->device_cap_flags |= IB_DEVICE_UD_TSO;
867 }
868
Maor Gottlieb03404e82017-05-30 10:29:13 +0300869 if (MLX5_CAP_GEN(dev->mdev, rq_delay_drop) &&
Daniel Jurgens85c7c012018-01-04 17:25:43 +0200870 MLX5_CAP_GEN(dev->mdev, general_notification_event) &&
871 raw_support)
Maor Gottlieb03404e82017-05-30 10:29:13 +0300872 props->raw_packet_caps |= IB_RAW_PACKET_CAP_DELAY_DROP;
873
Yishai Hadas1d54f892017-06-08 16:15:11 +0300874 if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads) &&
875 MLX5_CAP_IPOIB_ENHANCED(mdev, csum_cap))
876 props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
877
Majd Dibbinycff5a0f2016-04-17 17:19:38 +0300878 if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
Daniel Jurgens85c7c012018-01-04 17:25:43 +0200879 MLX5_CAP_ETH(dev->mdev, scatter_fcs) &&
880 raw_support) {
Noa Osheroviche8161332017-01-18 15:40:01 +0200881 /* Legacy bit to support old userspace libraries */
Majd Dibbinycff5a0f2016-04-17 17:19:38 +0300882 props->device_cap_flags |= IB_DEVICE_RAW_SCATTER_FCS;
Noa Osheroviche8161332017-01-18 15:40:01 +0200883 props->raw_packet_caps |= IB_RAW_PACKET_CAP_SCATTER_FCS;
884 }
Majd Dibbinycff5a0f2016-04-17 17:19:38 +0300885
Maor Gottliebda6d6ba32016-06-04 15:15:28 +0300886 if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS))
887 props->device_cap_flags |= IB_DEVICE_MANAGED_FLOW_STEERING;
888
Noa Osherovichb1383aa2017-10-29 13:59:45 +0200889 if (MLX5_CAP_GEN(mdev, end_pad))
890 props->device_cap_flags |= IB_DEVICE_PCI_WRITE_END_PADDING;
891
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300892 props->vendor_part_id = mdev->pdev->device;
893 props->hw_ver = mdev->pdev->revision;
Eli Cohene126ba92013-07-07 17:25:49 +0300894
895 props->max_mr_size = ~0ull;
Sagi Grimberge0238a62015-07-21 14:40:12 +0300896 props->page_size_cap = ~(min_page_size - 1);
Saeed Mahameed938fe832015-05-28 22:28:41 +0300897 props->max_qp = 1 << MLX5_CAP_GEN(mdev, log_max_qp);
898 props->max_qp_wr = 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
899 max_rq_sg = MLX5_CAP_GEN(mdev, max_wqe_sz_rq) /
900 sizeof(struct mlx5_wqe_data_seg);
Eli Cohen288c01b2016-10-27 16:36:45 +0300901 max_sq_desc = min_t(int, MLX5_CAP_GEN(mdev, max_wqe_sz_sq), 512);
902 max_sq_sg = (max_sq_desc - sizeof(struct mlx5_wqe_ctrl_seg) -
903 sizeof(struct mlx5_wqe_raddr_seg)) /
904 sizeof(struct mlx5_wqe_data_seg);
Eli Cohene126ba92013-07-07 17:25:49 +0300905 props->max_sge = min(max_rq_sg, max_sq_sg);
Sagi Grimberg986ef952016-03-31 19:03:25 +0300906 props->max_sge_rd = MLX5_MAX_SGE_RD;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300907 props->max_cq = 1 << MLX5_CAP_GEN(mdev, log_max_cq);
Leon Romanovsky9f177682016-01-14 08:11:40 +0200908 props->max_cqe = (1 << MLX5_CAP_GEN(mdev, log_max_cq_sz)) - 1;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300909 props->max_mr = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
910 props->max_pd = 1 << MLX5_CAP_GEN(mdev, log_max_pd);
911 props->max_qp_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_req_qp);
912 props->max_qp_init_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_res_qp);
913 props->max_srq = 1 << MLX5_CAP_GEN(mdev, log_max_srq);
914 props->max_srq_wr = (1 << MLX5_CAP_GEN(mdev, log_max_srq_sz)) - 1;
915 props->local_ca_ack_delay = MLX5_CAP_GEN(mdev, local_ca_ack_delay);
Eli Cohene126ba92013-07-07 17:25:49 +0300916 props->max_res_rd_atom = props->max_qp_rd_atom * props->max_qp;
Eli Cohene126ba92013-07-07 17:25:49 +0300917 props->max_srq_sge = max_rq_sg - 1;
Sagi Grimberg911f4332016-03-03 13:37:51 +0200918 props->max_fast_reg_page_list_len =
919 1 << MLX5_CAP_GEN(mdev, log_max_klm_list_size);
Moni Shoua776a3902018-01-02 16:19:33 +0200920 get_atomic_caps_qp(dev, props);
Eli Cohen81bea282013-09-11 16:35:30 +0300921 props->masked_atomic_cap = IB_ATOMIC_NONE;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300922 props->max_mcast_grp = 1 << MLX5_CAP_GEN(mdev, log_max_mcg);
923 props->max_mcast_qp_attach = MLX5_CAP_GEN(mdev, max_qp_mcg);
Eli Cohene126ba92013-07-07 17:25:49 +0300924 props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
925 props->max_mcast_grp;
926 props->max_map_per_fmr = INT_MAX; /* no limit in ConnectIB */
Maor Gottlieb86695a62016-10-27 16:36:38 +0300927 props->max_ah = INT_MAX;
Matan Barak7c60bcb2015-12-15 20:30:11 +0200928 props->hca_core_clock = MLX5_CAP_GEN(mdev, device_frequency_khz);
929 props->timestamp_mask = 0x7FFFFFFFFFFFFFFFULL;
Eli Cohene126ba92013-07-07 17:25:49 +0300930
Haggai Eran8cdd3122014-12-11 17:04:20 +0200931#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
Saeed Mahameed938fe832015-05-28 22:28:41 +0300932 if (MLX5_CAP_GEN(mdev, pg))
Haggai Eran8cdd3122014-12-11 17:04:20 +0200933 props->device_cap_flags |= IB_DEVICE_ON_DEMAND_PAGING;
934 props->odp_caps = dev->odp_caps;
935#endif
936
Leon Romanovsky051f2632015-12-20 12:16:11 +0200937 if (MLX5_CAP_GEN(mdev, cd))
938 props->device_cap_flags |= IB_DEVICE_CROSS_CHANNEL;
939
Eli Coheneff901d2016-03-11 22:58:42 +0200940 if (!mlx5_core_is_pf(mdev))
941 props->device_cap_flags |= IB_DEVICE_VIRTUAL_FUNCTION;
942
Yishai Hadas31f69a82016-08-28 11:28:45 +0300943 if (mlx5_ib_port_link_layer(ibdev, 1) ==
Daniel Jurgens85c7c012018-01-04 17:25:43 +0200944 IB_LINK_LAYER_ETHERNET && raw_support) {
Yishai Hadas31f69a82016-08-28 11:28:45 +0300945 props->rss_caps.max_rwq_indirection_tables =
946 1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt);
947 props->rss_caps.max_rwq_indirection_table_size =
948 1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt_size);
949 props->rss_caps.supported_qpts = 1 << IB_QPT_RAW_PACKET;
950 props->max_wq_type_rq =
951 1 << MLX5_CAP_GEN(dev->mdev, log_max_rq);
952 }
953
Artemy Kovalyoveb761892017-08-17 15:52:09 +0300954 if (MLX5_CAP_GEN(mdev, tag_matching)) {
Leon Romanovsky78b1beb2017-09-24 21:46:29 +0300955 props->tm_caps.max_rndv_hdr_size = MLX5_TM_MAX_RNDV_MSG_SIZE;
956 props->tm_caps.max_num_tags =
Artemy Kovalyoveb761892017-08-17 15:52:09 +0300957 (1 << MLX5_CAP_GEN(mdev, log_tag_matching_list_sz)) - 1;
Leon Romanovsky78b1beb2017-09-24 21:46:29 +0300958 props->tm_caps.flags = IB_TM_CAP_RC;
959 props->tm_caps.max_ops =
Artemy Kovalyoveb761892017-08-17 15:52:09 +0300960 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
Leon Romanovsky78b1beb2017-09-24 21:46:29 +0300961 props->tm_caps.max_sge = MLX5_TM_MAX_SGE;
Artemy Kovalyoveb761892017-08-17 15:52:09 +0300962 }
963
Yonatan Cohen87ab3f52017-11-13 10:51:18 +0200964 if (MLX5_CAP_GEN(dev->mdev, cq_moderation)) {
965 props->cq_caps.max_cq_moderation_count =
966 MLX5_MAX_CQ_COUNT;
967 props->cq_caps.max_cq_moderation_period =
968 MLX5_MAX_CQ_PERIOD;
969 }
970
Bodong Wang7e43a2a2016-10-31 12:16:44 +0200971 if (field_avail(typeof(resp), cqe_comp_caps, uhw->outlen)) {
972 resp.cqe_comp_caps.max_num =
973 MLX5_CAP_GEN(dev->mdev, cqe_compression) ?
974 MLX5_CAP_GEN(dev->mdev, cqe_compression_max_num) : 0;
975 resp.cqe_comp_caps.supported_format =
976 MLX5_IB_CQE_RES_FORMAT_HASH |
977 MLX5_IB_CQE_RES_FORMAT_CSUM;
978 resp.response_length += sizeof(resp.cqe_comp_caps);
979 }
980
Daniel Jurgens85c7c012018-01-04 17:25:43 +0200981 if (field_avail(typeof(resp), packet_pacing_caps, uhw->outlen) &&
982 raw_support) {
Bodong Wangd9491672016-12-01 13:43:13 +0200983 if (MLX5_CAP_QOS(mdev, packet_pacing) &&
984 MLX5_CAP_GEN(mdev, qos)) {
985 resp.packet_pacing_caps.qp_rate_limit_max =
986 MLX5_CAP_QOS(mdev, packet_pacing_max_rate);
987 resp.packet_pacing_caps.qp_rate_limit_min =
988 MLX5_CAP_QOS(mdev, packet_pacing_min_rate);
989 resp.packet_pacing_caps.supported_qpts |=
990 1 << IB_QPT_RAW_PACKET;
Bodong Wang61147f32018-03-19 15:10:30 +0200991 if (MLX5_CAP_QOS(mdev, packet_pacing_burst_bound) &&
992 MLX5_CAP_QOS(mdev, packet_pacing_typical_size))
993 resp.packet_pacing_caps.cap_flags |=
994 MLX5_IB_PP_SUPPORT_BURST;
Bodong Wangd9491672016-12-01 13:43:13 +0200995 }
996 resp.response_length += sizeof(resp.packet_pacing_caps);
997 }
998
Leon Romanovsky9f885202017-01-02 11:37:39 +0200999 if (field_avail(typeof(resp), mlx5_ib_support_multi_pkt_send_wqes,
1000 uhw->outlen)) {
Bodong Wang795b6092017-08-17 15:52:34 +03001001 if (MLX5_CAP_ETH(mdev, multi_pkt_send_wqe))
1002 resp.mlx5_ib_support_multi_pkt_send_wqes =
1003 MLX5_IB_ALLOW_MPW;
Bodong Wang050da902017-08-17 15:52:35 +03001004
1005 if (MLX5_CAP_ETH(mdev, enhanced_multi_pkt_send_wqe))
1006 resp.mlx5_ib_support_multi_pkt_send_wqes |=
1007 MLX5_IB_SUPPORT_EMPW;
1008
Leon Romanovsky9f885202017-01-02 11:37:39 +02001009 resp.response_length +=
1010 sizeof(resp.mlx5_ib_support_multi_pkt_send_wqes);
1011 }
1012
Guy Levide57f2a2017-10-19 08:25:52 +03001013 if (field_avail(typeof(resp), flags, uhw->outlen)) {
1014 resp.response_length += sizeof(resp.flags);
Guy Levi7a0c8f42017-10-19 08:25:53 +03001015
Guy Levide57f2a2017-10-19 08:25:52 +03001016 if (MLX5_CAP_GEN(mdev, cqe_compression_128))
1017 resp.flags |=
1018 MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_COMP;
Guy Levi7a0c8f42017-10-19 08:25:53 +03001019
1020 if (MLX5_CAP_GEN(mdev, cqe_128_always))
1021 resp.flags |= MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_PAD;
Guy Levide57f2a2017-10-19 08:25:52 +03001022 }
Leon Romanovsky9f885202017-01-02 11:37:39 +02001023
Noa Osherovich96dc3fc2017-08-17 15:52:28 +03001024 if (field_avail(typeof(resp), sw_parsing_caps,
1025 uhw->outlen)) {
1026 resp.response_length += sizeof(resp.sw_parsing_caps);
1027 if (MLX5_CAP_ETH(mdev, swp)) {
1028 resp.sw_parsing_caps.sw_parsing_offloads |=
1029 MLX5_IB_SW_PARSING;
1030
1031 if (MLX5_CAP_ETH(mdev, swp_csum))
1032 resp.sw_parsing_caps.sw_parsing_offloads |=
1033 MLX5_IB_SW_PARSING_CSUM;
1034
1035 if (MLX5_CAP_ETH(mdev, swp_lso))
1036 resp.sw_parsing_caps.sw_parsing_offloads |=
1037 MLX5_IB_SW_PARSING_LSO;
1038
1039 if (resp.sw_parsing_caps.sw_parsing_offloads)
1040 resp.sw_parsing_caps.supported_qpts =
1041 BIT(IB_QPT_RAW_PACKET);
1042 }
1043 }
1044
Daniel Jurgens85c7c012018-01-04 17:25:43 +02001045 if (field_avail(typeof(resp), striding_rq_caps, uhw->outlen) &&
1046 raw_support) {
Noa Osherovichb4f34592017-10-17 18:01:12 +03001047 resp.response_length += sizeof(resp.striding_rq_caps);
1048 if (MLX5_CAP_GEN(mdev, striding_rq)) {
1049 resp.striding_rq_caps.min_single_stride_log_num_of_bytes =
1050 MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES;
1051 resp.striding_rq_caps.max_single_stride_log_num_of_bytes =
1052 MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES;
1053 resp.striding_rq_caps.min_single_wqe_log_num_of_strides =
1054 MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES;
1055 resp.striding_rq_caps.max_single_wqe_log_num_of_strides =
1056 MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES;
1057 resp.striding_rq_caps.supported_qpts =
1058 BIT(IB_QPT_RAW_PACKET);
1059 }
1060 }
1061
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001062 if (field_avail(typeof(resp), tunnel_offloads_caps,
1063 uhw->outlen)) {
1064 resp.response_length += sizeof(resp.tunnel_offloads_caps);
1065 if (MLX5_CAP_ETH(mdev, tunnel_stateless_vxlan))
1066 resp.tunnel_offloads_caps |=
1067 MLX5_IB_TUNNELED_OFFLOADS_VXLAN;
1068 if (MLX5_CAP_ETH(mdev, tunnel_stateless_geneve_rx))
1069 resp.tunnel_offloads_caps |=
1070 MLX5_IB_TUNNELED_OFFLOADS_GENEVE;
1071 if (MLX5_CAP_ETH(mdev, tunnel_stateless_gre))
1072 resp.tunnel_offloads_caps |=
1073 MLX5_IB_TUNNELED_OFFLOADS_GRE;
1074 }
1075
Bodong Wang402ca532016-06-17 15:02:20 +03001076 if (uhw->outlen) {
1077 err = ib_copy_to_udata(uhw, &resp, resp.response_length);
1078
1079 if (err)
1080 return err;
1081 }
1082
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001083 return 0;
1084}
Eli Cohene126ba92013-07-07 17:25:49 +03001085
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001086enum mlx5_ib_width {
1087 MLX5_IB_WIDTH_1X = 1 << 0,
1088 MLX5_IB_WIDTH_2X = 1 << 1,
1089 MLX5_IB_WIDTH_4X = 1 << 2,
1090 MLX5_IB_WIDTH_8X = 1 << 3,
1091 MLX5_IB_WIDTH_12X = 1 << 4
1092};
1093
1094static int translate_active_width(struct ib_device *ibdev, u8 active_width,
1095 u8 *ib_width)
1096{
1097 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1098 int err = 0;
1099
1100 if (active_width & MLX5_IB_WIDTH_1X) {
1101 *ib_width = IB_WIDTH_1X;
1102 } else if (active_width & MLX5_IB_WIDTH_2X) {
1103 mlx5_ib_dbg(dev, "active_width %d is not supported by IB spec\n",
1104 (int)active_width);
1105 err = -EINVAL;
1106 } else if (active_width & MLX5_IB_WIDTH_4X) {
1107 *ib_width = IB_WIDTH_4X;
1108 } else if (active_width & MLX5_IB_WIDTH_8X) {
1109 *ib_width = IB_WIDTH_8X;
1110 } else if (active_width & MLX5_IB_WIDTH_12X) {
1111 *ib_width = IB_WIDTH_12X;
1112 } else {
1113 mlx5_ib_dbg(dev, "Invalid active_width %d\n",
1114 (int)active_width);
1115 err = -EINVAL;
1116 }
1117
1118 return err;
1119}
1120
1121static int mlx5_mtu_to_ib_mtu(int mtu)
1122{
1123 switch (mtu) {
1124 case 256: return 1;
1125 case 512: return 2;
1126 case 1024: return 3;
1127 case 2048: return 4;
1128 case 4096: return 5;
1129 default:
1130 pr_warn("invalid mtu\n");
1131 return -1;
1132 }
1133}
1134
1135enum ib_max_vl_num {
1136 __IB_MAX_VL_0 = 1,
1137 __IB_MAX_VL_0_1 = 2,
1138 __IB_MAX_VL_0_3 = 3,
1139 __IB_MAX_VL_0_7 = 4,
1140 __IB_MAX_VL_0_14 = 5,
1141};
1142
1143enum mlx5_vl_hw_cap {
1144 MLX5_VL_HW_0 = 1,
1145 MLX5_VL_HW_0_1 = 2,
1146 MLX5_VL_HW_0_2 = 3,
1147 MLX5_VL_HW_0_3 = 4,
1148 MLX5_VL_HW_0_4 = 5,
1149 MLX5_VL_HW_0_5 = 6,
1150 MLX5_VL_HW_0_6 = 7,
1151 MLX5_VL_HW_0_7 = 8,
1152 MLX5_VL_HW_0_14 = 15
1153};
1154
1155static int translate_max_vl_num(struct ib_device *ibdev, u8 vl_hw_cap,
1156 u8 *max_vl_num)
1157{
1158 switch (vl_hw_cap) {
1159 case MLX5_VL_HW_0:
1160 *max_vl_num = __IB_MAX_VL_0;
1161 break;
1162 case MLX5_VL_HW_0_1:
1163 *max_vl_num = __IB_MAX_VL_0_1;
1164 break;
1165 case MLX5_VL_HW_0_3:
1166 *max_vl_num = __IB_MAX_VL_0_3;
1167 break;
1168 case MLX5_VL_HW_0_7:
1169 *max_vl_num = __IB_MAX_VL_0_7;
1170 break;
1171 case MLX5_VL_HW_0_14:
1172 *max_vl_num = __IB_MAX_VL_0_14;
1173 break;
1174
1175 default:
1176 return -EINVAL;
1177 }
1178
1179 return 0;
1180}
1181
1182static int mlx5_query_hca_port(struct ib_device *ibdev, u8 port,
1183 struct ib_port_attr *props)
1184{
1185 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1186 struct mlx5_core_dev *mdev = dev->mdev;
1187 struct mlx5_hca_vport_context *rep;
Saeed Mahameed046339e2016-04-22 00:33:03 +03001188 u16 max_mtu;
1189 u16 oper_mtu;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001190 int err;
1191 u8 ib_link_width_oper;
1192 u8 vl_hw_cap;
1193
1194 rep = kzalloc(sizeof(*rep), GFP_KERNEL);
1195 if (!rep) {
1196 err = -ENOMEM;
1197 goto out;
1198 }
1199
Or Gerlitzc4550c62017-01-24 13:02:39 +02001200 /* props being zeroed by the caller, avoid zeroing it here */
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001201
1202 err = mlx5_query_hca_vport_context(mdev, 0, port, 0, rep);
1203 if (err)
1204 goto out;
1205
1206 props->lid = rep->lid;
1207 props->lmc = rep->lmc;
1208 props->sm_lid = rep->sm_lid;
1209 props->sm_sl = rep->sm_sl;
1210 props->state = rep->vport_state;
1211 props->phys_state = rep->port_physical_state;
1212 props->port_cap_flags = rep->cap_mask1;
1213 props->gid_tbl_len = mlx5_get_gid_table_len(MLX5_CAP_GEN(mdev, gid_table_size));
1214 props->max_msg_sz = 1 << MLX5_CAP_GEN(mdev, log_max_msg);
1215 props->pkey_tbl_len = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, pkey_table_size));
1216 props->bad_pkey_cntr = rep->pkey_violation_counter;
1217 props->qkey_viol_cntr = rep->qkey_violation_counter;
1218 props->subnet_timeout = rep->subnet_timeout;
1219 props->init_type_reply = rep->init_type_reply;
Eli Coheneff901d2016-03-11 22:58:42 +02001220 props->grh_required = rep->grh_required;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001221
1222 err = mlx5_query_port_link_width_oper(mdev, &ib_link_width_oper, port);
1223 if (err)
1224 goto out;
1225
1226 err = translate_active_width(ibdev, ib_link_width_oper,
1227 &props->active_width);
1228 if (err)
1229 goto out;
Noa Osherovichd5beb7f2016-06-02 10:47:53 +03001230 err = mlx5_query_port_ib_proto_oper(mdev, &props->active_speed, port);
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001231 if (err)
1232 goto out;
1233
Saeed Mahameedfacc9692015-06-11 14:47:27 +03001234 mlx5_query_port_max_mtu(mdev, &max_mtu, port);
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001235
1236 props->max_mtu = mlx5_mtu_to_ib_mtu(max_mtu);
1237
Saeed Mahameedfacc9692015-06-11 14:47:27 +03001238 mlx5_query_port_oper_mtu(mdev, &oper_mtu, port);
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001239
1240 props->active_mtu = mlx5_mtu_to_ib_mtu(oper_mtu);
1241
1242 err = mlx5_query_port_vl_hw_cap(mdev, &vl_hw_cap, port);
1243 if (err)
1244 goto out;
1245
1246 err = translate_max_vl_num(ibdev, vl_hw_cap,
1247 &props->max_vl_num);
1248out:
1249 kfree(rep);
Eli Cohene126ba92013-07-07 17:25:49 +03001250 return err;
1251}
1252
1253int mlx5_ib_query_port(struct ib_device *ibdev, u8 port,
1254 struct ib_port_attr *props)
1255{
Ilan Tayari095b0922017-05-14 16:04:30 +03001256 unsigned int count;
1257 int ret;
1258
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001259 switch (mlx5_get_vport_access_method(ibdev)) {
1260 case MLX5_VPORT_ACCESS_METHOD_MAD:
Ilan Tayari095b0922017-05-14 16:04:30 +03001261 ret = mlx5_query_mad_ifc_port(ibdev, port, props);
1262 break;
Eli Cohene126ba92013-07-07 17:25:49 +03001263
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001264 case MLX5_VPORT_ACCESS_METHOD_HCA:
Ilan Tayari095b0922017-05-14 16:04:30 +03001265 ret = mlx5_query_hca_port(ibdev, port, props);
1266 break;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001267
Achiad Shochat3f89a642015-12-23 18:47:21 +02001268 case MLX5_VPORT_ACCESS_METHOD_NIC:
Ilan Tayari095b0922017-05-14 16:04:30 +03001269 ret = mlx5_query_port_roce(ibdev, port, props);
1270 break;
Achiad Shochat3f89a642015-12-23 18:47:21 +02001271
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001272 default:
Ilan Tayari095b0922017-05-14 16:04:30 +03001273 ret = -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +03001274 }
Ilan Tayari095b0922017-05-14 16:04:30 +03001275
1276 if (!ret && props) {
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001277 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1278 struct mlx5_core_dev *mdev;
1279 bool put_mdev = true;
1280
1281 mdev = mlx5_ib_get_native_port_mdev(dev, port, NULL);
1282 if (!mdev) {
1283 /* If the port isn't affiliated yet query the master.
1284 * The master and slave will have the same values.
1285 */
1286 mdev = dev->mdev;
1287 port = 1;
1288 put_mdev = false;
1289 }
1290 count = mlx5_core_reserved_gids_count(mdev);
1291 if (put_mdev)
1292 mlx5_ib_put_native_port_mdev(dev, port);
Ilan Tayari095b0922017-05-14 16:04:30 +03001293 props->gid_tbl_len -= count;
1294 }
1295 return ret;
Eli Cohene126ba92013-07-07 17:25:49 +03001296}
1297
Mark Bloch8e6efa32017-11-06 12:22:13 +00001298static int mlx5_ib_rep_query_port(struct ib_device *ibdev, u8 port,
1299 struct ib_port_attr *props)
1300{
1301 int ret;
1302
1303 /* Only link layer == ethernet is valid for representors */
1304 ret = mlx5_query_port_roce(ibdev, port, props);
1305 if (ret || !props)
1306 return ret;
1307
1308 /* We don't support GIDS */
1309 props->gid_tbl_len = 0;
1310
1311 return ret;
1312}
1313
Eli Cohene126ba92013-07-07 17:25:49 +03001314static int mlx5_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
1315 union ib_gid *gid)
1316{
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001317 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1318 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03001319
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001320 switch (mlx5_get_vport_access_method(ibdev)) {
1321 case MLX5_VPORT_ACCESS_METHOD_MAD:
1322 return mlx5_query_mad_ifc_gids(ibdev, port, index, gid);
Eli Cohene126ba92013-07-07 17:25:49 +03001323
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001324 case MLX5_VPORT_ACCESS_METHOD_HCA:
1325 return mlx5_query_hca_vport_gid(mdev, 0, port, 0, index, gid);
Eli Cohene126ba92013-07-07 17:25:49 +03001326
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001327 default:
1328 return -EINVAL;
1329 }
Eli Cohene126ba92013-07-07 17:25:49 +03001330
Eli Cohene126ba92013-07-07 17:25:49 +03001331}
1332
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001333static int mlx5_query_hca_nic_pkey(struct ib_device *ibdev, u8 port,
1334 u16 index, u16 *pkey)
1335{
1336 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1337 struct mlx5_core_dev *mdev;
1338 bool put_mdev = true;
1339 u8 mdev_port_num;
1340 int err;
1341
1342 mdev = mlx5_ib_get_native_port_mdev(dev, port, &mdev_port_num);
1343 if (!mdev) {
1344 /* The port isn't affiliated yet, get the PKey from the master
1345 * port. For RoCE the PKey tables will be the same.
1346 */
1347 put_mdev = false;
1348 mdev = dev->mdev;
1349 mdev_port_num = 1;
1350 }
1351
1352 err = mlx5_query_hca_vport_pkey(mdev, 0, mdev_port_num, 0,
1353 index, pkey);
1354 if (put_mdev)
1355 mlx5_ib_put_native_port_mdev(dev, port);
1356
1357 return err;
1358}
1359
Eli Cohene126ba92013-07-07 17:25:49 +03001360static int mlx5_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
1361 u16 *pkey)
1362{
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001363 switch (mlx5_get_vport_access_method(ibdev)) {
1364 case MLX5_VPORT_ACCESS_METHOD_MAD:
1365 return mlx5_query_mad_ifc_pkey(ibdev, port, index, pkey);
Eli Cohene126ba92013-07-07 17:25:49 +03001366
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001367 case MLX5_VPORT_ACCESS_METHOD_HCA:
1368 case MLX5_VPORT_ACCESS_METHOD_NIC:
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001369 return mlx5_query_hca_nic_pkey(ibdev, port, index, pkey);
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001370 default:
1371 return -EINVAL;
1372 }
Eli Cohene126ba92013-07-07 17:25:49 +03001373}
1374
Eli Cohene126ba92013-07-07 17:25:49 +03001375static int mlx5_ib_modify_device(struct ib_device *ibdev, int mask,
1376 struct ib_device_modify *props)
1377{
1378 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1379 struct mlx5_reg_node_desc in;
1380 struct mlx5_reg_node_desc out;
1381 int err;
1382
1383 if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
1384 return -EOPNOTSUPP;
1385
1386 if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
1387 return 0;
1388
1389 /*
1390 * If possible, pass node desc to FW, so it can generate
1391 * a 144 trap. If cmd fails, just ignore.
1392 */
Yuval Shaiabd99fde2016-08-25 10:57:07 -07001393 memcpy(&in, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
Jack Morgenstein9603b612014-07-28 23:30:22 +03001394 err = mlx5_core_access_reg(dev->mdev, &in, sizeof(in), &out,
Eli Cohene126ba92013-07-07 17:25:49 +03001395 sizeof(out), MLX5_REG_NODE_DESC, 0, 1);
1396 if (err)
1397 return err;
1398
Yuval Shaiabd99fde2016-08-25 10:57:07 -07001399 memcpy(ibdev->node_desc, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
Eli Cohene126ba92013-07-07 17:25:49 +03001400
1401 return err;
1402}
1403
Eli Cohencdbe33d2017-02-14 07:25:38 +02001404static int set_port_caps_atomic(struct mlx5_ib_dev *dev, u8 port_num, u32 mask,
1405 u32 value)
1406{
1407 struct mlx5_hca_vport_context ctx = {};
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001408 struct mlx5_core_dev *mdev;
1409 u8 mdev_port_num;
Eli Cohencdbe33d2017-02-14 07:25:38 +02001410 int err;
1411
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001412 mdev = mlx5_ib_get_native_port_mdev(dev, port_num, &mdev_port_num);
1413 if (!mdev)
1414 return -ENODEV;
1415
1416 err = mlx5_query_hca_vport_context(mdev, 0, mdev_port_num, 0, &ctx);
Eli Cohencdbe33d2017-02-14 07:25:38 +02001417 if (err)
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001418 goto out;
Eli Cohencdbe33d2017-02-14 07:25:38 +02001419
1420 if (~ctx.cap_mask1_perm & mask) {
1421 mlx5_ib_warn(dev, "trying to change bitmask 0x%X but change supported 0x%X\n",
1422 mask, ctx.cap_mask1_perm);
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001423 err = -EINVAL;
1424 goto out;
Eli Cohencdbe33d2017-02-14 07:25:38 +02001425 }
1426
1427 ctx.cap_mask1 = value;
1428 ctx.cap_mask1_perm = mask;
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001429 err = mlx5_core_modify_hca_vport_context(mdev, 0, mdev_port_num,
1430 0, &ctx);
1431
1432out:
1433 mlx5_ib_put_native_port_mdev(dev, port_num);
Eli Cohencdbe33d2017-02-14 07:25:38 +02001434
1435 return err;
1436}
1437
Eli Cohene126ba92013-07-07 17:25:49 +03001438static int mlx5_ib_modify_port(struct ib_device *ibdev, u8 port, int mask,
1439 struct ib_port_modify *props)
1440{
1441 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1442 struct ib_port_attr attr;
1443 u32 tmp;
1444 int err;
Eli Cohencdbe33d2017-02-14 07:25:38 +02001445 u32 change_mask;
1446 u32 value;
1447 bool is_ib = (mlx5_ib_port_link_layer(ibdev, port) ==
1448 IB_LINK_LAYER_INFINIBAND);
1449
Majd Dibbinyec255872017-08-23 08:35:42 +03001450 /* CM layer calls ib_modify_port() regardless of the link layer. For
1451 * Ethernet ports, qkey violation and Port capabilities are meaningless.
1452 */
1453 if (!is_ib)
1454 return 0;
1455
Eli Cohencdbe33d2017-02-14 07:25:38 +02001456 if (MLX5_CAP_GEN(dev->mdev, ib_virt) && is_ib) {
1457 change_mask = props->clr_port_cap_mask | props->set_port_cap_mask;
1458 value = ~props->clr_port_cap_mask | props->set_port_cap_mask;
1459 return set_port_caps_atomic(dev, port, change_mask, value);
1460 }
Eli Cohene126ba92013-07-07 17:25:49 +03001461
1462 mutex_lock(&dev->cap_mask_mutex);
1463
Or Gerlitzc4550c62017-01-24 13:02:39 +02001464 err = ib_query_port(ibdev, port, &attr);
Eli Cohene126ba92013-07-07 17:25:49 +03001465 if (err)
1466 goto out;
1467
1468 tmp = (attr.port_cap_flags | props->set_port_cap_mask) &
1469 ~props->clr_port_cap_mask;
1470
Jack Morgenstein9603b612014-07-28 23:30:22 +03001471 err = mlx5_set_port_caps(dev->mdev, port, tmp);
Eli Cohene126ba92013-07-07 17:25:49 +03001472
1473out:
1474 mutex_unlock(&dev->cap_mask_mutex);
1475 return err;
1476}
1477
Eli Cohen30aa60b2017-01-03 23:55:27 +02001478static void print_lib_caps(struct mlx5_ib_dev *dev, u64 caps)
1479{
1480 mlx5_ib_dbg(dev, "MLX5_LIB_CAP_4K_UAR = %s\n",
1481 caps & MLX5_LIB_CAP_4K_UAR ? "y" : "n");
1482}
1483
Yishai Hadas31a78a52017-12-24 16:31:34 +02001484static u16 calc_dynamic_bfregs(int uars_per_sys_page)
1485{
1486 /* Large page with non 4k uar support might limit the dynamic size */
1487 if (uars_per_sys_page == 1 && PAGE_SIZE > 4096)
1488 return MLX5_MIN_DYN_BFREGS;
1489
1490 return MLX5_MAX_DYN_BFREGS;
1491}
1492
Eli Cohenb037c292017-01-03 23:55:26 +02001493static int calc_total_bfregs(struct mlx5_ib_dev *dev, bool lib_uar_4k,
1494 struct mlx5_ib_alloc_ucontext_req_v2 *req,
Yishai Hadas31a78a52017-12-24 16:31:34 +02001495 struct mlx5_bfreg_info *bfregi)
Eli Cohenb037c292017-01-03 23:55:26 +02001496{
1497 int uars_per_sys_page;
1498 int bfregs_per_sys_page;
1499 int ref_bfregs = req->total_num_bfregs;
1500
1501 if (req->total_num_bfregs == 0)
1502 return -EINVAL;
1503
1504 BUILD_BUG_ON(MLX5_MAX_BFREGS % MLX5_NON_FP_BFREGS_IN_PAGE);
1505 BUILD_BUG_ON(MLX5_MAX_BFREGS < MLX5_NON_FP_BFREGS_IN_PAGE);
1506
1507 if (req->total_num_bfregs > MLX5_MAX_BFREGS)
1508 return -ENOMEM;
1509
1510 uars_per_sys_page = get_uars_per_sys_page(dev, lib_uar_4k);
1511 bfregs_per_sys_page = uars_per_sys_page * MLX5_NON_FP_BFREGS_PER_UAR;
Yishai Hadas31a78a52017-12-24 16:31:34 +02001512 /* This holds the required static allocation asked by the user */
Eli Cohenb037c292017-01-03 23:55:26 +02001513 req->total_num_bfregs = ALIGN(req->total_num_bfregs, bfregs_per_sys_page);
Eli Cohenb037c292017-01-03 23:55:26 +02001514 if (req->num_low_latency_bfregs > req->total_num_bfregs - 1)
1515 return -EINVAL;
1516
Yishai Hadas31a78a52017-12-24 16:31:34 +02001517 bfregi->num_static_sys_pages = req->total_num_bfregs / bfregs_per_sys_page;
1518 bfregi->num_dyn_bfregs = ALIGN(calc_dynamic_bfregs(uars_per_sys_page), bfregs_per_sys_page);
1519 bfregi->total_num_bfregs = req->total_num_bfregs + bfregi->num_dyn_bfregs;
1520 bfregi->num_sys_pages = bfregi->total_num_bfregs / bfregs_per_sys_page;
1521
1522 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 +02001523 MLX5_CAP_GEN(dev->mdev, uar_4k) ? "yes" : "no",
1524 lib_uar_4k ? "yes" : "no", ref_bfregs,
Yishai Hadas31a78a52017-12-24 16:31:34 +02001525 req->total_num_bfregs, bfregi->total_num_bfregs,
1526 bfregi->num_sys_pages);
Eli Cohenb037c292017-01-03 23:55:26 +02001527
1528 return 0;
1529}
1530
1531static int allocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
1532{
1533 struct mlx5_bfreg_info *bfregi;
1534 int err;
1535 int i;
1536
1537 bfregi = &context->bfregi;
Yishai Hadas31a78a52017-12-24 16:31:34 +02001538 for (i = 0; i < bfregi->num_static_sys_pages; i++) {
Eli Cohenb037c292017-01-03 23:55:26 +02001539 err = mlx5_cmd_alloc_uar(dev->mdev, &bfregi->sys_pages[i]);
1540 if (err)
1541 goto error;
1542
1543 mlx5_ib_dbg(dev, "allocated uar %d\n", bfregi->sys_pages[i]);
1544 }
Yishai Hadas4ed131d2017-12-24 16:31:35 +02001545
1546 for (i = bfregi->num_static_sys_pages; i < bfregi->num_sys_pages; i++)
1547 bfregi->sys_pages[i] = MLX5_IB_INVALID_UAR_INDEX;
1548
Eli Cohenb037c292017-01-03 23:55:26 +02001549 return 0;
1550
1551error:
1552 for (--i; i >= 0; i--)
1553 if (mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]))
1554 mlx5_ib_warn(dev, "failed to free uar %d\n", i);
1555
1556 return err;
1557}
1558
1559static int deallocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
1560{
1561 struct mlx5_bfreg_info *bfregi;
1562 int err;
1563 int i;
1564
1565 bfregi = &context->bfregi;
Yishai Hadas4ed131d2017-12-24 16:31:35 +02001566 for (i = 0; i < bfregi->num_sys_pages; i++) {
1567 if (i < bfregi->num_static_sys_pages ||
1568 bfregi->sys_pages[i] != MLX5_IB_INVALID_UAR_INDEX) {
1569 err = mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]);
1570 if (err) {
1571 mlx5_ib_warn(dev, "failed to free uar %d, err=%d\n", i, err);
1572 return err;
1573 }
Eli Cohenb037c292017-01-03 23:55:26 +02001574 }
1575 }
Yishai Hadas4ed131d2017-12-24 16:31:35 +02001576
Eli Cohenb037c292017-01-03 23:55:26 +02001577 return 0;
1578}
1579
Huy Nguyenc85023e2017-05-30 09:42:54 +03001580static int mlx5_ib_alloc_transport_domain(struct mlx5_ib_dev *dev, u32 *tdn)
1581{
1582 int err;
1583
1584 err = mlx5_core_alloc_transport_domain(dev->mdev, tdn);
1585 if (err)
1586 return err;
1587
1588 if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
Eran Ben Elisha8978cc92018-01-09 11:41:10 +02001589 (!MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) &&
1590 !MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
Huy Nguyenc85023e2017-05-30 09:42:54 +03001591 return err;
1592
1593 mutex_lock(&dev->lb_mutex);
1594 dev->user_td++;
1595
1596 if (dev->user_td == 2)
1597 err = mlx5_nic_vport_update_local_lb(dev->mdev, true);
1598
1599 mutex_unlock(&dev->lb_mutex);
1600 return err;
1601}
1602
1603static void mlx5_ib_dealloc_transport_domain(struct mlx5_ib_dev *dev, u32 tdn)
1604{
1605 mlx5_core_dealloc_transport_domain(dev->mdev, tdn);
1606
1607 if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
Eran Ben Elisha8978cc92018-01-09 11:41:10 +02001608 (!MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) &&
1609 !MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
Huy Nguyenc85023e2017-05-30 09:42:54 +03001610 return;
1611
1612 mutex_lock(&dev->lb_mutex);
1613 dev->user_td--;
1614
1615 if (dev->user_td < 2)
1616 mlx5_nic_vport_update_local_lb(dev->mdev, false);
1617
1618 mutex_unlock(&dev->lb_mutex);
1619}
1620
Eli Cohene126ba92013-07-07 17:25:49 +03001621static struct ib_ucontext *mlx5_ib_alloc_ucontext(struct ib_device *ibdev,
1622 struct ib_udata *udata)
1623{
1624 struct mlx5_ib_dev *dev = to_mdev(ibdev);
Matan Barakb368d7c2015-12-15 20:30:12 +02001625 struct mlx5_ib_alloc_ucontext_req_v2 req = {};
1626 struct mlx5_ib_alloc_ucontext_resp resp = {};
Feras Daoud5c99eae2018-01-16 20:08:41 +02001627 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03001628 struct mlx5_ib_ucontext *context;
Eli Cohen2f5ff262017-01-03 23:55:21 +02001629 struct mlx5_bfreg_info *bfregi;
Eli Cohen78c0f982014-01-30 13:49:48 +02001630 int ver;
Eli Cohene126ba92013-07-07 17:25:49 +03001631 int err;
Majd Dibbinya168a41c2016-01-28 17:51:47 +02001632 size_t min_req_v2 = offsetof(struct mlx5_ib_alloc_ucontext_req_v2,
1633 max_cqe_version);
Eli Cohenb037c292017-01-03 23:55:26 +02001634 bool lib_uar_4k;
Eli Cohene126ba92013-07-07 17:25:49 +03001635
1636 if (!dev->ib_active)
1637 return ERR_PTR(-EAGAIN);
1638
Amrani, Rame0931112017-06-27 17:04:42 +03001639 if (udata->inlen == sizeof(struct mlx5_ib_alloc_ucontext_req))
Eli Cohen78c0f982014-01-30 13:49:48 +02001640 ver = 0;
Amrani, Rame0931112017-06-27 17:04:42 +03001641 else if (udata->inlen >= min_req_v2)
Eli Cohen78c0f982014-01-30 13:49:48 +02001642 ver = 2;
1643 else
1644 return ERR_PTR(-EINVAL);
1645
Amrani, Rame0931112017-06-27 17:04:42 +03001646 err = ib_copy_from_udata(&req, udata, min(udata->inlen, sizeof(req)));
Eli Cohene126ba92013-07-07 17:25:49 +03001647 if (err)
1648 return ERR_PTR(err);
1649
Matan Barakb368d7c2015-12-15 20:30:12 +02001650 if (req.flags)
Eli Cohen78c0f982014-01-30 13:49:48 +02001651 return ERR_PTR(-EINVAL);
1652
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001653 if (req.comp_mask || req.reserved0 || req.reserved1 || req.reserved2)
Matan Barakb368d7c2015-12-15 20:30:12 +02001654 return ERR_PTR(-EOPNOTSUPP);
1655
Eli Cohen2f5ff262017-01-03 23:55:21 +02001656 req.total_num_bfregs = ALIGN(req.total_num_bfregs,
1657 MLX5_NON_FP_BFREGS_PER_UAR);
1658 if (req.num_low_latency_bfregs > req.total_num_bfregs - 1)
Eli Cohene126ba92013-07-07 17:25:49 +03001659 return ERR_PTR(-EINVAL);
1660
Saeed Mahameed938fe832015-05-28 22:28:41 +03001661 resp.qp_tab_size = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp);
Noa Osherovich2cc6ad52016-06-04 15:15:33 +03001662 if (mlx5_core_is_pf(dev->mdev) && MLX5_CAP_GEN(dev->mdev, bf))
1663 resp.bf_reg_size = 1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size);
Daniel Jurgensb47bd6e2016-10-25 18:36:24 +03001664 resp.cache_line_size = cache_line_size();
Saeed Mahameed938fe832015-05-28 22:28:41 +03001665 resp.max_sq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq);
1666 resp.max_rq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq);
1667 resp.max_send_wqebb = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1668 resp.max_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1669 resp.max_srq_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz);
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001670 resp.cqe_version = min_t(__u8,
1671 (__u8)MLX5_CAP_GEN(dev->mdev, cqe_version),
1672 req.max_cqe_version);
Eli Cohen30aa60b2017-01-03 23:55:27 +02001673 resp.log_uar_size = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
1674 MLX5_ADAPTER_PAGE_SHIFT : PAGE_SHIFT;
1675 resp.num_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
1676 MLX5_CAP_GEN(dev->mdev, num_of_uars_per_page) : 1;
Matan Barakb368d7c2015-12-15 20:30:12 +02001677 resp.response_length = min(offsetof(typeof(resp), response_length) +
1678 sizeof(resp.response_length), udata->outlen);
Eli Cohene126ba92013-07-07 17:25:49 +03001679
1680 context = kzalloc(sizeof(*context), GFP_KERNEL);
1681 if (!context)
1682 return ERR_PTR(-ENOMEM);
1683
Eli Cohen30aa60b2017-01-03 23:55:27 +02001684 lib_uar_4k = req.lib_caps & MLX5_LIB_CAP_4K_UAR;
Eli Cohen2f5ff262017-01-03 23:55:21 +02001685 bfregi = &context->bfregi;
Eli Cohenb037c292017-01-03 23:55:26 +02001686
1687 /* updates req->total_num_bfregs */
Yishai Hadas31a78a52017-12-24 16:31:34 +02001688 err = calc_total_bfregs(dev, lib_uar_4k, &req, bfregi);
Eli Cohenb037c292017-01-03 23:55:26 +02001689 if (err)
1690 goto out_ctx;
1691
Eli Cohen2f5ff262017-01-03 23:55:21 +02001692 mutex_init(&bfregi->lock);
Eli Cohenb037c292017-01-03 23:55:26 +02001693 bfregi->lib_uar_4k = lib_uar_4k;
Yishai Hadas31a78a52017-12-24 16:31:34 +02001694 bfregi->count = kcalloc(bfregi->total_num_bfregs, sizeof(*bfregi->count),
Eli Cohenb037c292017-01-03 23:55:26 +02001695 GFP_KERNEL);
1696 if (!bfregi->count) {
Eli Cohene126ba92013-07-07 17:25:49 +03001697 err = -ENOMEM;
1698 goto out_ctx;
1699 }
1700
Eli Cohenb037c292017-01-03 23:55:26 +02001701 bfregi->sys_pages = kcalloc(bfregi->num_sys_pages,
1702 sizeof(*bfregi->sys_pages),
1703 GFP_KERNEL);
1704 if (!bfregi->sys_pages) {
Eli Cohene126ba92013-07-07 17:25:49 +03001705 err = -ENOMEM;
Eli Cohenb037c292017-01-03 23:55:26 +02001706 goto out_count;
Eli Cohene126ba92013-07-07 17:25:49 +03001707 }
1708
Eli Cohenb037c292017-01-03 23:55:26 +02001709 err = allocate_uars(dev, context);
1710 if (err)
1711 goto out_sys_pages;
Eli Cohene126ba92013-07-07 17:25:49 +03001712
Haggai Eranb4cfe442014-12-11 17:04:26 +02001713#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
1714 context->ibucontext.invalidate_range = &mlx5_ib_invalidate_range;
1715#endif
1716
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001717 if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain)) {
Huy Nguyenc85023e2017-05-30 09:42:54 +03001718 err = mlx5_ib_alloc_transport_domain(dev, &context->tdn);
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001719 if (err)
Ilya Lesokhinc44ef992018-03-13 15:18:48 +02001720 goto out_uars;
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001721 }
1722
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001723 INIT_LIST_HEAD(&context->vma_private_list);
Majd Dibbinyad9a3662017-12-24 13:54:56 +02001724 mutex_init(&context->vma_private_list_mutex);
Eli Cohene126ba92013-07-07 17:25:49 +03001725 INIT_LIST_HEAD(&context->db_page_list);
1726 mutex_init(&context->db_page_mutex);
1727
Eli Cohen2f5ff262017-01-03 23:55:21 +02001728 resp.tot_bfregs = req.total_num_bfregs;
Daniel Jurgens508562d2018-01-04 17:25:34 +02001729 resp.num_ports = dev->num_ports;
Matan Barakb368d7c2015-12-15 20:30:12 +02001730
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001731 if (field_avail(typeof(resp), cqe_version, udata->outlen))
1732 resp.response_length += sizeof(resp.cqe_version);
Matan Barakb368d7c2015-12-15 20:30:12 +02001733
Bodong Wang402ca532016-06-17 15:02:20 +03001734 if (field_avail(typeof(resp), cmds_supp_uhw, udata->outlen)) {
Moni Shoua6ad279c52016-11-23 08:23:23 +02001735 resp.cmds_supp_uhw |= MLX5_USER_CMDS_SUPP_UHW_QUERY_DEVICE |
1736 MLX5_USER_CMDS_SUPP_UHW_CREATE_AH;
Bodong Wang402ca532016-06-17 15:02:20 +03001737 resp.response_length += sizeof(resp.cmds_supp_uhw);
1738 }
1739
Or Gerlitz78984892016-11-30 20:33:33 +02001740 if (field_avail(typeof(resp), eth_min_inline, udata->outlen)) {
1741 if (mlx5_ib_port_link_layer(ibdev, 1) == IB_LINK_LAYER_ETHERNET) {
1742 mlx5_query_min_inline(dev->mdev, &resp.eth_min_inline);
1743 resp.eth_min_inline++;
1744 }
1745 resp.response_length += sizeof(resp.eth_min_inline);
1746 }
1747
Feras Daoud5c99eae2018-01-16 20:08:41 +02001748 if (field_avail(typeof(resp), clock_info_versions, udata->outlen)) {
1749 if (mdev->clock_info)
1750 resp.clock_info_versions = BIT(MLX5_IB_CLOCK_INFO_V1);
1751 resp.response_length += sizeof(resp.clock_info_versions);
1752 }
1753
Noa Osherovichbc5c6ee2016-06-04 15:15:31 +03001754 /*
1755 * We don't want to expose information from the PCI bar that is located
1756 * after 4096 bytes, so if the arch only supports larger pages, let's
1757 * pretend we don't support reading the HCA's core clock. This is also
1758 * forced by mmap function.
1759 */
Eli Cohende8d6e02017-01-03 23:55:19 +02001760 if (field_avail(typeof(resp), hca_core_clock_offset, udata->outlen)) {
1761 if (PAGE_SIZE <= 4096) {
1762 resp.comp_mask |=
1763 MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_CORE_CLOCK_OFFSET;
1764 resp.hca_core_clock_offset =
1765 offsetof(struct mlx5_init_seg, internal_timer_h) % PAGE_SIZE;
1766 }
Feras Daoud5c99eae2018-01-16 20:08:41 +02001767 resp.response_length += sizeof(resp.hca_core_clock_offset);
Matan Barakb368d7c2015-12-15 20:30:12 +02001768 }
1769
Eli Cohen30aa60b2017-01-03 23:55:27 +02001770 if (field_avail(typeof(resp), log_uar_size, udata->outlen))
1771 resp.response_length += sizeof(resp.log_uar_size);
1772
1773 if (field_avail(typeof(resp), num_uars_per_page, udata->outlen))
1774 resp.response_length += sizeof(resp.num_uars_per_page);
1775
Yishai Hadas31a78a52017-12-24 16:31:34 +02001776 if (field_avail(typeof(resp), num_dyn_bfregs, udata->outlen)) {
1777 resp.num_dyn_bfregs = bfregi->num_dyn_bfregs;
1778 resp.response_length += sizeof(resp.num_dyn_bfregs);
1779 }
1780
Matan Barakb368d7c2015-12-15 20:30:12 +02001781 err = ib_copy_to_udata(udata, &resp, resp.response_length);
Eli Cohene126ba92013-07-07 17:25:49 +03001782 if (err)
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001783 goto out_td;
Eli Cohene126ba92013-07-07 17:25:49 +03001784
Eli Cohen2f5ff262017-01-03 23:55:21 +02001785 bfregi->ver = ver;
1786 bfregi->num_low_latency_bfregs = req.num_low_latency_bfregs;
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001787 context->cqe_version = resp.cqe_version;
Eli Cohen30aa60b2017-01-03 23:55:27 +02001788 context->lib_caps = req.lib_caps;
1789 print_lib_caps(dev, context->lib_caps);
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001790
Eli Cohene126ba92013-07-07 17:25:49 +03001791 return &context->ibucontext;
1792
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001793out_td:
1794 if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
Huy Nguyenc85023e2017-05-30 09:42:54 +03001795 mlx5_ib_dealloc_transport_domain(dev, context->tdn);
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001796
Eli Cohene126ba92013-07-07 17:25:49 +03001797out_uars:
Eli Cohenb037c292017-01-03 23:55:26 +02001798 deallocate_uars(dev, context);
1799
1800out_sys_pages:
1801 kfree(bfregi->sys_pages);
1802
Eli Cohene126ba92013-07-07 17:25:49 +03001803out_count:
Eli Cohen2f5ff262017-01-03 23:55:21 +02001804 kfree(bfregi->count);
Eli Cohene126ba92013-07-07 17:25:49 +03001805
Eli Cohene126ba92013-07-07 17:25:49 +03001806out_ctx:
1807 kfree(context);
Eli Cohenb037c292017-01-03 23:55:26 +02001808
Eli Cohene126ba92013-07-07 17:25:49 +03001809 return ERR_PTR(err);
1810}
1811
1812static int mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
1813{
1814 struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1815 struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
Eli Cohenb037c292017-01-03 23:55:26 +02001816 struct mlx5_bfreg_info *bfregi;
Eli Cohene126ba92013-07-07 17:25:49 +03001817
Eli Cohenb037c292017-01-03 23:55:26 +02001818 bfregi = &context->bfregi;
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001819 if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
Huy Nguyenc85023e2017-05-30 09:42:54 +03001820 mlx5_ib_dealloc_transport_domain(dev, context->tdn);
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001821
Eli Cohenb037c292017-01-03 23:55:26 +02001822 deallocate_uars(dev, context);
1823 kfree(bfregi->sys_pages);
Eli Cohen2f5ff262017-01-03 23:55:21 +02001824 kfree(bfregi->count);
Eli Cohene126ba92013-07-07 17:25:49 +03001825 kfree(context);
1826
1827 return 0;
1828}
1829
Eli Cohenb037c292017-01-03 23:55:26 +02001830static phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev,
Yishai Hadas4ed131d2017-12-24 16:31:35 +02001831 int uar_idx)
Eli Cohene126ba92013-07-07 17:25:49 +03001832{
Eli Cohenb037c292017-01-03 23:55:26 +02001833 int fw_uars_per_page;
1834
1835 fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ? MLX5_UARS_IN_PAGE : 1;
1836
Yishai Hadas4ed131d2017-12-24 16:31:35 +02001837 return (pci_resource_start(dev->mdev->pdev, 0) >> PAGE_SHIFT) + uar_idx / fw_uars_per_page;
Eli Cohene126ba92013-07-07 17:25:49 +03001838}
1839
1840static int get_command(unsigned long offset)
1841{
1842 return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK;
1843}
1844
1845static int get_arg(unsigned long offset)
1846{
1847 return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1);
1848}
1849
1850static int get_index(unsigned long offset)
1851{
1852 return get_arg(offset);
1853}
1854
Yishai Hadas4ed131d2017-12-24 16:31:35 +02001855/* Index resides in an extra byte to enable larger values than 255 */
1856static int get_extended_index(unsigned long offset)
1857{
1858 return get_arg(offset) | ((offset >> 16) & 0xff) << 8;
1859}
1860
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001861static void mlx5_ib_vma_open(struct vm_area_struct *area)
1862{
1863 /* vma_open is called when a new VMA is created on top of our VMA. This
1864 * is done through either mremap flow or split_vma (usually due to
1865 * mlock, madvise, munmap, etc.) We do not support a clone of the VMA,
1866 * as this VMA is strongly hardware related. Therefore we set the
1867 * vm_ops of the newly created/cloned VMA to NULL, to prevent it from
1868 * calling us again and trying to do incorrect actions. We assume that
1869 * the original VMA size is exactly a single page, and therefore all
1870 * "splitting" operation will not happen to it.
1871 */
1872 area->vm_ops = NULL;
1873}
1874
1875static void mlx5_ib_vma_close(struct vm_area_struct *area)
1876{
1877 struct mlx5_ib_vma_private_data *mlx5_ib_vma_priv_data;
1878
1879 /* It's guaranteed that all VMAs opened on a FD are closed before the
1880 * file itself is closed, therefore no sync is needed with the regular
1881 * closing flow. (e.g. mlx5 ib_dealloc_ucontext)
1882 * However need a sync with accessing the vma as part of
1883 * mlx5_ib_disassociate_ucontext.
1884 * The close operation is usually called under mm->mmap_sem except when
1885 * process is exiting.
1886 * The exiting case is handled explicitly as part of
1887 * mlx5_ib_disassociate_ucontext.
1888 */
1889 mlx5_ib_vma_priv_data = (struct mlx5_ib_vma_private_data *)area->vm_private_data;
1890
1891 /* setting the vma context pointer to null in the mlx5_ib driver's
1892 * private data, to protect a race condition in
1893 * mlx5_ib_disassociate_ucontext().
1894 */
1895 mlx5_ib_vma_priv_data->vma = NULL;
Majd Dibbinyad9a3662017-12-24 13:54:56 +02001896 mutex_lock(mlx5_ib_vma_priv_data->vma_private_list_mutex);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001897 list_del(&mlx5_ib_vma_priv_data->list);
Majd Dibbinyad9a3662017-12-24 13:54:56 +02001898 mutex_unlock(mlx5_ib_vma_priv_data->vma_private_list_mutex);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001899 kfree(mlx5_ib_vma_priv_data);
1900}
1901
1902static const struct vm_operations_struct mlx5_ib_vm_ops = {
1903 .open = mlx5_ib_vma_open,
1904 .close = mlx5_ib_vma_close
1905};
1906
1907static int mlx5_ib_set_vma_data(struct vm_area_struct *vma,
1908 struct mlx5_ib_ucontext *ctx)
1909{
1910 struct mlx5_ib_vma_private_data *vma_prv;
1911 struct list_head *vma_head = &ctx->vma_private_list;
1912
1913 vma_prv = kzalloc(sizeof(*vma_prv), GFP_KERNEL);
1914 if (!vma_prv)
1915 return -ENOMEM;
1916
1917 vma_prv->vma = vma;
Majd Dibbinyad9a3662017-12-24 13:54:56 +02001918 vma_prv->vma_private_list_mutex = &ctx->vma_private_list_mutex;
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001919 vma->vm_private_data = vma_prv;
1920 vma->vm_ops = &mlx5_ib_vm_ops;
1921
Majd Dibbinyad9a3662017-12-24 13:54:56 +02001922 mutex_lock(&ctx->vma_private_list_mutex);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001923 list_add(&vma_prv->list, vma_head);
Majd Dibbinyad9a3662017-12-24 13:54:56 +02001924 mutex_unlock(&ctx->vma_private_list_mutex);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001925
1926 return 0;
1927}
1928
1929static void mlx5_ib_disassociate_ucontext(struct ib_ucontext *ibcontext)
1930{
1931 int ret;
1932 struct vm_area_struct *vma;
1933 struct mlx5_ib_vma_private_data *vma_private, *n;
1934 struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1935 struct task_struct *owning_process = NULL;
1936 struct mm_struct *owning_mm = NULL;
1937
1938 owning_process = get_pid_task(ibcontext->tgid, PIDTYPE_PID);
1939 if (!owning_process)
1940 return;
1941
1942 owning_mm = get_task_mm(owning_process);
1943 if (!owning_mm) {
1944 pr_info("no mm, disassociate ucontext is pending task termination\n");
1945 while (1) {
1946 put_task_struct(owning_process);
1947 usleep_range(1000, 2000);
1948 owning_process = get_pid_task(ibcontext->tgid,
1949 PIDTYPE_PID);
1950 if (!owning_process ||
1951 owning_process->state == TASK_DEAD) {
1952 pr_info("disassociate ucontext done, task was terminated\n");
1953 /* in case task was dead need to release the
1954 * task struct.
1955 */
1956 if (owning_process)
1957 put_task_struct(owning_process);
1958 return;
1959 }
1960 }
1961 }
1962
1963 /* need to protect from a race on closing the vma as part of
1964 * mlx5_ib_vma_close.
1965 */
Maor Gottliebecc7d832017-03-29 06:03:02 +03001966 down_write(&owning_mm->mmap_sem);
Majd Dibbinyad9a3662017-12-24 13:54:56 +02001967 mutex_lock(&context->vma_private_list_mutex);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001968 list_for_each_entry_safe(vma_private, n, &context->vma_private_list,
1969 list) {
1970 vma = vma_private->vma;
1971 ret = zap_vma_ptes(vma, vma->vm_start,
1972 PAGE_SIZE);
1973 WARN_ONCE(ret, "%s: zap_vma_ptes failed", __func__);
1974 /* context going to be destroyed, should
1975 * not access ops any more.
1976 */
Maor Gottlieb13776612017-03-29 06:03:03 +03001977 vma->vm_flags &= ~(VM_SHARED | VM_MAYSHARE);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001978 vma->vm_ops = NULL;
1979 list_del(&vma_private->list);
1980 kfree(vma_private);
1981 }
Majd Dibbinyad9a3662017-12-24 13:54:56 +02001982 mutex_unlock(&context->vma_private_list_mutex);
Maor Gottliebecc7d832017-03-29 06:03:02 +03001983 up_write(&owning_mm->mmap_sem);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001984 mmput(owning_mm);
1985 put_task_struct(owning_process);
1986}
1987
Guy Levi37aa5c32016-04-27 16:49:50 +03001988static inline char *mmap_cmd2str(enum mlx5_ib_mmap_cmd cmd)
1989{
1990 switch (cmd) {
1991 case MLX5_IB_MMAP_WC_PAGE:
1992 return "WC";
1993 case MLX5_IB_MMAP_REGULAR_PAGE:
1994 return "best effort WC";
1995 case MLX5_IB_MMAP_NC_PAGE:
1996 return "NC";
1997 default:
1998 return NULL;
1999 }
2000}
2001
Feras Daoud5c99eae2018-01-16 20:08:41 +02002002static int mlx5_ib_mmap_clock_info_page(struct mlx5_ib_dev *dev,
2003 struct vm_area_struct *vma,
2004 struct mlx5_ib_ucontext *context)
2005{
2006 phys_addr_t pfn;
2007 int err;
2008
2009 if (vma->vm_end - vma->vm_start != PAGE_SIZE)
2010 return -EINVAL;
2011
2012 if (get_index(vma->vm_pgoff) != MLX5_IB_CLOCK_INFO_V1)
2013 return -EOPNOTSUPP;
2014
2015 if (vma->vm_flags & VM_WRITE)
2016 return -EPERM;
2017
2018 if (!dev->mdev->clock_info_page)
2019 return -EOPNOTSUPP;
2020
2021 pfn = page_to_pfn(dev->mdev->clock_info_page);
2022 err = remap_pfn_range(vma, vma->vm_start, pfn, PAGE_SIZE,
2023 vma->vm_page_prot);
2024 if (err)
2025 return err;
2026
2027 mlx5_ib_dbg(dev, "mapped clock info at 0x%lx, PA 0x%llx\n",
2028 vma->vm_start,
2029 (unsigned long long)pfn << PAGE_SHIFT);
2030
2031 return mlx5_ib_set_vma_data(vma, context);
2032}
2033
Guy Levi37aa5c32016-04-27 16:49:50 +03002034static int uar_mmap(struct mlx5_ib_dev *dev, enum mlx5_ib_mmap_cmd cmd,
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03002035 struct vm_area_struct *vma,
2036 struct mlx5_ib_ucontext *context)
Guy Levi37aa5c32016-04-27 16:49:50 +03002037{
Eli Cohen2f5ff262017-01-03 23:55:21 +02002038 struct mlx5_bfreg_info *bfregi = &context->bfregi;
Guy Levi37aa5c32016-04-27 16:49:50 +03002039 int err;
2040 unsigned long idx;
2041 phys_addr_t pfn, pa;
2042 pgprot_t prot;
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002043 u32 bfreg_dyn_idx = 0;
2044 u32 uar_index;
2045 int dyn_uar = (cmd == MLX5_IB_MMAP_ALLOC_WC);
2046 int max_valid_idx = dyn_uar ? bfregi->num_sys_pages :
2047 bfregi->num_static_sys_pages;
Eli Cohenb037c292017-01-03 23:55:26 +02002048
2049 if (vma->vm_end - vma->vm_start != PAGE_SIZE)
2050 return -EINVAL;
2051
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002052 if (dyn_uar)
2053 idx = get_extended_index(vma->vm_pgoff) + bfregi->num_static_sys_pages;
2054 else
2055 idx = get_index(vma->vm_pgoff);
2056
2057 if (idx >= max_valid_idx) {
2058 mlx5_ib_warn(dev, "invalid uar index %lu, max=%d\n",
2059 idx, max_valid_idx);
Eli Cohenb037c292017-01-03 23:55:26 +02002060 return -EINVAL;
2061 }
Guy Levi37aa5c32016-04-27 16:49:50 +03002062
2063 switch (cmd) {
2064 case MLX5_IB_MMAP_WC_PAGE:
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002065 case MLX5_IB_MMAP_ALLOC_WC:
Guy Levi37aa5c32016-04-27 16:49:50 +03002066/* Some architectures don't support WC memory */
2067#if defined(CONFIG_X86)
2068 if (!pat_enabled())
2069 return -EPERM;
2070#elif !(defined(CONFIG_PPC) || (defined(CONFIG_ARM) && defined(CONFIG_MMU)))
2071 return -EPERM;
2072#endif
2073 /* fall through */
2074 case MLX5_IB_MMAP_REGULAR_PAGE:
2075 /* For MLX5_IB_MMAP_REGULAR_PAGE do the best effort to get WC */
2076 prot = pgprot_writecombine(vma->vm_page_prot);
2077 break;
2078 case MLX5_IB_MMAP_NC_PAGE:
2079 prot = pgprot_noncached(vma->vm_page_prot);
2080 break;
2081 default:
2082 return -EINVAL;
2083 }
2084
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002085 if (dyn_uar) {
2086 int uars_per_page;
2087
2088 uars_per_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k);
2089 bfreg_dyn_idx = idx * (uars_per_page * MLX5_NON_FP_BFREGS_PER_UAR);
2090 if (bfreg_dyn_idx >= bfregi->total_num_bfregs) {
2091 mlx5_ib_warn(dev, "invalid bfreg_dyn_idx %u, max=%u\n",
2092 bfreg_dyn_idx, bfregi->total_num_bfregs);
2093 return -EINVAL;
2094 }
2095
2096 mutex_lock(&bfregi->lock);
2097 /* Fail if uar already allocated, first bfreg index of each
2098 * page holds its count.
2099 */
2100 if (bfregi->count[bfreg_dyn_idx]) {
2101 mlx5_ib_warn(dev, "wrong offset, idx %lu is busy, bfregn=%u\n", idx, bfreg_dyn_idx);
2102 mutex_unlock(&bfregi->lock);
2103 return -EINVAL;
2104 }
2105
2106 bfregi->count[bfreg_dyn_idx]++;
2107 mutex_unlock(&bfregi->lock);
2108
2109 err = mlx5_cmd_alloc_uar(dev->mdev, &uar_index);
2110 if (err) {
2111 mlx5_ib_warn(dev, "UAR alloc failed\n");
2112 goto free_bfreg;
2113 }
2114 } else {
2115 uar_index = bfregi->sys_pages[idx];
2116 }
2117
2118 pfn = uar_index2pfn(dev, uar_index);
Guy Levi37aa5c32016-04-27 16:49:50 +03002119 mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn %pa\n", idx, &pfn);
2120
2121 vma->vm_page_prot = prot;
2122 err = io_remap_pfn_range(vma, vma->vm_start, pfn,
2123 PAGE_SIZE, vma->vm_page_prot);
2124 if (err) {
2125 mlx5_ib_err(dev, "io_remap_pfn_range failed with error=%d, vm_start=0x%lx, pfn=%pa, mmap_cmd=%s\n",
2126 err, vma->vm_start, &pfn, mmap_cmd2str(cmd));
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002127 err = -EAGAIN;
2128 goto err;
Guy Levi37aa5c32016-04-27 16:49:50 +03002129 }
2130
2131 pa = pfn << PAGE_SHIFT;
2132 mlx5_ib_dbg(dev, "mapped %s at 0x%lx, PA %pa\n", mmap_cmd2str(cmd),
2133 vma->vm_start, &pa);
2134
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002135 err = mlx5_ib_set_vma_data(vma, context);
2136 if (err)
2137 goto err;
2138
2139 if (dyn_uar)
2140 bfregi->sys_pages[idx] = uar_index;
2141 return 0;
2142
2143err:
2144 if (!dyn_uar)
2145 return err;
2146
2147 mlx5_cmd_free_uar(dev->mdev, idx);
2148
2149free_bfreg:
2150 mlx5_ib_free_bfreg(dev, bfregi, bfreg_dyn_idx);
2151
2152 return err;
Guy Levi37aa5c32016-04-27 16:49:50 +03002153}
2154
Eli Cohene126ba92013-07-07 17:25:49 +03002155static int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma)
2156{
2157 struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
2158 struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
Eli Cohene126ba92013-07-07 17:25:49 +03002159 unsigned long command;
Eli Cohene126ba92013-07-07 17:25:49 +03002160 phys_addr_t pfn;
2161
2162 command = get_command(vma->vm_pgoff);
2163 switch (command) {
Guy Levi37aa5c32016-04-27 16:49:50 +03002164 case MLX5_IB_MMAP_WC_PAGE:
2165 case MLX5_IB_MMAP_NC_PAGE:
Eli Cohene126ba92013-07-07 17:25:49 +03002166 case MLX5_IB_MMAP_REGULAR_PAGE:
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002167 case MLX5_IB_MMAP_ALLOC_WC:
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03002168 return uar_mmap(dev, command, vma, context);
Eli Cohene126ba92013-07-07 17:25:49 +03002169
2170 case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES:
2171 return -ENOSYS;
2172
Matan Barakd69e3bc2015-12-15 20:30:13 +02002173 case MLX5_IB_MMAP_CORE_CLOCK:
Matan Barakd69e3bc2015-12-15 20:30:13 +02002174 if (vma->vm_end - vma->vm_start != PAGE_SIZE)
2175 return -EINVAL;
2176
Matan Barak6cbac1e2016-04-14 16:52:10 +03002177 if (vma->vm_flags & VM_WRITE)
Matan Barakd69e3bc2015-12-15 20:30:13 +02002178 return -EPERM;
2179
2180 /* Don't expose to user-space information it shouldn't have */
2181 if (PAGE_SIZE > 4096)
2182 return -EOPNOTSUPP;
2183
2184 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
2185 pfn = (dev->mdev->iseg_base +
2186 offsetof(struct mlx5_init_seg, internal_timer_h)) >>
2187 PAGE_SHIFT;
2188 if (io_remap_pfn_range(vma, vma->vm_start, pfn,
2189 PAGE_SIZE, vma->vm_page_prot))
2190 return -EAGAIN;
2191
2192 mlx5_ib_dbg(dev, "mapped internal timer at 0x%lx, PA 0x%llx\n",
2193 vma->vm_start,
2194 (unsigned long long)pfn << PAGE_SHIFT);
2195 break;
Feras Daoud5c99eae2018-01-16 20:08:41 +02002196 case MLX5_IB_MMAP_CLOCK_INFO:
2197 return mlx5_ib_mmap_clock_info_page(dev, vma, context);
Matan Barakd69e3bc2015-12-15 20:30:13 +02002198
Eli Cohene126ba92013-07-07 17:25:49 +03002199 default:
2200 return -EINVAL;
2201 }
2202
2203 return 0;
2204}
2205
Eli Cohene126ba92013-07-07 17:25:49 +03002206static struct ib_pd *mlx5_ib_alloc_pd(struct ib_device *ibdev,
2207 struct ib_ucontext *context,
2208 struct ib_udata *udata)
2209{
2210 struct mlx5_ib_alloc_pd_resp resp;
2211 struct mlx5_ib_pd *pd;
2212 int err;
2213
2214 pd = kmalloc(sizeof(*pd), GFP_KERNEL);
2215 if (!pd)
2216 return ERR_PTR(-ENOMEM);
2217
Jack Morgenstein9603b612014-07-28 23:30:22 +03002218 err = mlx5_core_alloc_pd(to_mdev(ibdev)->mdev, &pd->pdn);
Eli Cohene126ba92013-07-07 17:25:49 +03002219 if (err) {
2220 kfree(pd);
2221 return ERR_PTR(err);
2222 }
2223
2224 if (context) {
2225 resp.pdn = pd->pdn;
2226 if (ib_copy_to_udata(udata, &resp, sizeof(resp))) {
Jack Morgenstein9603b612014-07-28 23:30:22 +03002227 mlx5_core_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn);
Eli Cohene126ba92013-07-07 17:25:49 +03002228 kfree(pd);
2229 return ERR_PTR(-EFAULT);
2230 }
Eli Cohene126ba92013-07-07 17:25:49 +03002231 }
2232
2233 return &pd->ibpd;
2234}
2235
2236static int mlx5_ib_dealloc_pd(struct ib_pd *pd)
2237{
2238 struct mlx5_ib_dev *mdev = to_mdev(pd->device);
2239 struct mlx5_ib_pd *mpd = to_mpd(pd);
2240
Jack Morgenstein9603b612014-07-28 23:30:22 +03002241 mlx5_core_dealloc_pd(mdev->mdev, mpd->pdn);
Eli Cohene126ba92013-07-07 17:25:49 +03002242 kfree(mpd);
2243
2244 return 0;
2245}
2246
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002247enum {
2248 MATCH_CRITERIA_ENABLE_OUTER_BIT,
2249 MATCH_CRITERIA_ENABLE_MISC_BIT,
2250 MATCH_CRITERIA_ENABLE_INNER_BIT
2251};
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002252
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002253#define HEADER_IS_ZERO(match_criteria, headers) \
2254 !(memchr_inv(MLX5_ADDR_OF(fte_match_param, match_criteria, headers), \
2255 0, MLX5_FLD_SZ_BYTES(fte_match_param, headers))) \
2256
2257static u8 get_match_criteria_enable(u32 *match_criteria)
2258{
2259 u8 match_criteria_enable;
2260
2261 match_criteria_enable =
2262 (!HEADER_IS_ZERO(match_criteria, outer_headers)) <<
2263 MATCH_CRITERIA_ENABLE_OUTER_BIT;
2264 match_criteria_enable |=
2265 (!HEADER_IS_ZERO(match_criteria, misc_parameters)) <<
2266 MATCH_CRITERIA_ENABLE_MISC_BIT;
2267 match_criteria_enable |=
2268 (!HEADER_IS_ZERO(match_criteria, inner_headers)) <<
2269 MATCH_CRITERIA_ENABLE_INNER_BIT;
2270
2271 return match_criteria_enable;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002272}
2273
Maor Gottliebca0d4752016-08-30 16:58:35 +03002274static void set_proto(void *outer_c, void *outer_v, u8 mask, u8 val)
2275{
2276 MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_protocol, mask);
2277 MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_protocol, val);
2278}
2279
Moses Reuben2d1e6972016-11-14 19:04:52 +02002280static void set_flow_label(void *misc_c, void *misc_v, u8 mask, u8 val,
2281 bool inner)
2282{
2283 if (inner) {
2284 MLX5_SET(fte_match_set_misc,
2285 misc_c, inner_ipv6_flow_label, mask);
2286 MLX5_SET(fte_match_set_misc,
2287 misc_v, inner_ipv6_flow_label, val);
2288 } else {
2289 MLX5_SET(fte_match_set_misc,
2290 misc_c, outer_ipv6_flow_label, mask);
2291 MLX5_SET(fte_match_set_misc,
2292 misc_v, outer_ipv6_flow_label, val);
2293 }
2294}
2295
Maor Gottliebca0d4752016-08-30 16:58:35 +03002296static void set_tos(void *outer_c, void *outer_v, u8 mask, u8 val)
2297{
2298 MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_ecn, mask);
2299 MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_ecn, val);
2300 MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_dscp, mask >> 2);
2301 MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_dscp, val >> 2);
2302}
2303
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002304#define LAST_ETH_FIELD vlan_tag
2305#define LAST_IB_FIELD sl
Maor Gottliebca0d4752016-08-30 16:58:35 +03002306#define LAST_IPV4_FIELD tos
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002307#define LAST_IPV6_FIELD traffic_class
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002308#define LAST_TCP_UDP_FIELD src_port
Moses Reubenffb30d82016-11-14 19:04:50 +02002309#define LAST_TUNNEL_FIELD tunnel_id
Moses Reuben2ac693f2017-01-18 14:59:50 +02002310#define LAST_FLOW_TAG_FIELD tag_id
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002311#define LAST_DROP_FIELD size
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002312
2313/* Field is the last supported field */
2314#define FIELDS_NOT_SUPPORTED(filter, field)\
2315 memchr_inv((void *)&filter.field +\
2316 sizeof(filter.field), 0,\
2317 sizeof(filter) -\
2318 offsetof(typeof(filter), field) -\
2319 sizeof(filter.field))
2320
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002321static int parse_flow_attr(struct mlx5_core_dev *mdev, u32 *match_c,
2322 u32 *match_v, const union ib_flow_spec *ib_spec,
Boris Pismenny075572d2017-08-16 09:33:30 +03002323 struct mlx5_flow_act *action)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002324{
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002325 void *misc_params_c = MLX5_ADDR_OF(fte_match_param, match_c,
2326 misc_parameters);
2327 void *misc_params_v = MLX5_ADDR_OF(fte_match_param, match_v,
2328 misc_parameters);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002329 void *headers_c;
2330 void *headers_v;
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002331 int match_ipv;
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002332
Moses Reuben2d1e6972016-11-14 19:04:52 +02002333 if (ib_spec->type & IB_FLOW_SPEC_INNER) {
2334 headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
2335 inner_headers);
2336 headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
2337 inner_headers);
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002338 match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2339 ft_field_support.inner_ip_version);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002340 } else {
2341 headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
2342 outer_headers);
2343 headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
2344 outer_headers);
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002345 match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2346 ft_field_support.outer_ip_version);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002347 }
2348
2349 switch (ib_spec->type & ~IB_FLOW_SPEC_INNER) {
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002350 case IB_FLOW_SPEC_ETH:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002351 if (FIELDS_NOT_SUPPORTED(ib_spec->eth.mask, LAST_ETH_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002352 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002353
Moses Reuben2d1e6972016-11-14 19:04:52 +02002354 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002355 dmac_47_16),
2356 ib_spec->eth.mask.dst_mac);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002357 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002358 dmac_47_16),
2359 ib_spec->eth.val.dst_mac);
2360
Moses Reuben2d1e6972016-11-14 19:04:52 +02002361 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottliebee3da802016-09-12 19:16:24 +03002362 smac_47_16),
2363 ib_spec->eth.mask.src_mac);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002364 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottliebee3da802016-09-12 19:16:24 +03002365 smac_47_16),
2366 ib_spec->eth.val.src_mac);
2367
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002368 if (ib_spec->eth.mask.vlan_tag) {
Moses Reuben2d1e6972016-11-14 19:04:52 +02002369 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Mohamad Haj Yahia10543362016-10-09 16:25:43 +03002370 cvlan_tag, 1);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002371 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Mohamad Haj Yahia10543362016-10-09 16:25:43 +03002372 cvlan_tag, 1);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002373
Moses Reuben2d1e6972016-11-14 19:04:52 +02002374 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002375 first_vid, ntohs(ib_spec->eth.mask.vlan_tag));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002376 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002377 first_vid, ntohs(ib_spec->eth.val.vlan_tag));
2378
Moses Reuben2d1e6972016-11-14 19:04:52 +02002379 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002380 first_cfi,
2381 ntohs(ib_spec->eth.mask.vlan_tag) >> 12);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002382 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002383 first_cfi,
2384 ntohs(ib_spec->eth.val.vlan_tag) >> 12);
2385
Moses Reuben2d1e6972016-11-14 19:04:52 +02002386 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002387 first_prio,
2388 ntohs(ib_spec->eth.mask.vlan_tag) >> 13);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002389 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002390 first_prio,
2391 ntohs(ib_spec->eth.val.vlan_tag) >> 13);
2392 }
Moses Reuben2d1e6972016-11-14 19:04:52 +02002393 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002394 ethertype, ntohs(ib_spec->eth.mask.ether_type));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002395 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002396 ethertype, ntohs(ib_spec->eth.val.ether_type));
2397 break;
2398 case IB_FLOW_SPEC_IPV4:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002399 if (FIELDS_NOT_SUPPORTED(ib_spec->ipv4.mask, LAST_IPV4_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002400 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002401
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002402 if (match_ipv) {
2403 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2404 ip_version, 0xf);
2405 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Boris Pismenny3346c482017-08-20 15:13:08 +03002406 ip_version, MLX5_FS_IPV4_VERSION);
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002407 } else {
2408 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2409 ethertype, 0xffff);
2410 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2411 ethertype, ETH_P_IP);
2412 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002413
Moses Reuben2d1e6972016-11-14 19:04:52 +02002414 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002415 src_ipv4_src_ipv6.ipv4_layout.ipv4),
2416 &ib_spec->ipv4.mask.src_ip,
2417 sizeof(ib_spec->ipv4.mask.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002418 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002419 src_ipv4_src_ipv6.ipv4_layout.ipv4),
2420 &ib_spec->ipv4.val.src_ip,
2421 sizeof(ib_spec->ipv4.val.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002422 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002423 dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2424 &ib_spec->ipv4.mask.dst_ip,
2425 sizeof(ib_spec->ipv4.mask.dst_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002426 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002427 dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2428 &ib_spec->ipv4.val.dst_ip,
2429 sizeof(ib_spec->ipv4.val.dst_ip));
Maor Gottliebca0d4752016-08-30 16:58:35 +03002430
Moses Reuben2d1e6972016-11-14 19:04:52 +02002431 set_tos(headers_c, headers_v,
Maor Gottliebca0d4752016-08-30 16:58:35 +03002432 ib_spec->ipv4.mask.tos, ib_spec->ipv4.val.tos);
2433
Moses Reuben2d1e6972016-11-14 19:04:52 +02002434 set_proto(headers_c, headers_v,
Maor Gottliebca0d4752016-08-30 16:58:35 +03002435 ib_spec->ipv4.mask.proto, ib_spec->ipv4.val.proto);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002436 break;
Maor Gottlieb026bae02016-06-17 15:14:51 +03002437 case IB_FLOW_SPEC_IPV6:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002438 if (FIELDS_NOT_SUPPORTED(ib_spec->ipv6.mask, LAST_IPV6_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002439 return -EOPNOTSUPP;
Maor Gottlieb026bae02016-06-17 15:14:51 +03002440
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002441 if (match_ipv) {
2442 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2443 ip_version, 0xf);
2444 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Boris Pismenny3346c482017-08-20 15:13:08 +03002445 ip_version, MLX5_FS_IPV6_VERSION);
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002446 } else {
2447 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2448 ethertype, 0xffff);
2449 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2450 ethertype, ETH_P_IPV6);
2451 }
Maor Gottlieb026bae02016-06-17 15:14:51 +03002452
Moses Reuben2d1e6972016-11-14 19:04:52 +02002453 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb026bae02016-06-17 15:14:51 +03002454 src_ipv4_src_ipv6.ipv6_layout.ipv6),
2455 &ib_spec->ipv6.mask.src_ip,
2456 sizeof(ib_spec->ipv6.mask.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002457 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb026bae02016-06-17 15:14:51 +03002458 src_ipv4_src_ipv6.ipv6_layout.ipv6),
2459 &ib_spec->ipv6.val.src_ip,
2460 sizeof(ib_spec->ipv6.val.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002461 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb026bae02016-06-17 15:14:51 +03002462 dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2463 &ib_spec->ipv6.mask.dst_ip,
2464 sizeof(ib_spec->ipv6.mask.dst_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002465 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb026bae02016-06-17 15:14:51 +03002466 dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2467 &ib_spec->ipv6.val.dst_ip,
2468 sizeof(ib_spec->ipv6.val.dst_ip));
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002469
Moses Reuben2d1e6972016-11-14 19:04:52 +02002470 set_tos(headers_c, headers_v,
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002471 ib_spec->ipv6.mask.traffic_class,
2472 ib_spec->ipv6.val.traffic_class);
2473
Moses Reuben2d1e6972016-11-14 19:04:52 +02002474 set_proto(headers_c, headers_v,
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002475 ib_spec->ipv6.mask.next_hdr,
2476 ib_spec->ipv6.val.next_hdr);
2477
Moses Reuben2d1e6972016-11-14 19:04:52 +02002478 set_flow_label(misc_params_c, misc_params_v,
2479 ntohl(ib_spec->ipv6.mask.flow_label),
2480 ntohl(ib_spec->ipv6.val.flow_label),
2481 ib_spec->type & IB_FLOW_SPEC_INNER);
2482
Maor Gottlieb026bae02016-06-17 15:14:51 +03002483 break;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002484 case IB_FLOW_SPEC_TCP:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002485 if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
2486 LAST_TCP_UDP_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002487 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002488
Moses Reuben2d1e6972016-11-14 19:04:52 +02002489 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002490 0xff);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002491 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002492 IPPROTO_TCP);
2493
Moses Reuben2d1e6972016-11-14 19:04:52 +02002494 MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002495 ntohs(ib_spec->tcp_udp.mask.src_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002496 MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002497 ntohs(ib_spec->tcp_udp.val.src_port));
2498
Moses Reuben2d1e6972016-11-14 19:04:52 +02002499 MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002500 ntohs(ib_spec->tcp_udp.mask.dst_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002501 MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002502 ntohs(ib_spec->tcp_udp.val.dst_port));
2503 break;
2504 case IB_FLOW_SPEC_UDP:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002505 if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
2506 LAST_TCP_UDP_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002507 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002508
Moses Reuben2d1e6972016-11-14 19:04:52 +02002509 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002510 0xff);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002511 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002512 IPPROTO_UDP);
2513
Moses Reuben2d1e6972016-11-14 19:04:52 +02002514 MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002515 ntohs(ib_spec->tcp_udp.mask.src_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002516 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002517 ntohs(ib_spec->tcp_udp.val.src_port));
2518
Moses Reuben2d1e6972016-11-14 19:04:52 +02002519 MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002520 ntohs(ib_spec->tcp_udp.mask.dst_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002521 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002522 ntohs(ib_spec->tcp_udp.val.dst_port));
2523 break;
Moses Reubenffb30d82016-11-14 19:04:50 +02002524 case IB_FLOW_SPEC_VXLAN_TUNNEL:
2525 if (FIELDS_NOT_SUPPORTED(ib_spec->tunnel.mask,
2526 LAST_TUNNEL_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002527 return -EOPNOTSUPP;
Moses Reubenffb30d82016-11-14 19:04:50 +02002528
2529 MLX5_SET(fte_match_set_misc, misc_params_c, vxlan_vni,
2530 ntohl(ib_spec->tunnel.mask.tunnel_id));
2531 MLX5_SET(fte_match_set_misc, misc_params_v, vxlan_vni,
2532 ntohl(ib_spec->tunnel.val.tunnel_id));
2533 break;
Moses Reuben2ac693f2017-01-18 14:59:50 +02002534 case IB_FLOW_SPEC_ACTION_TAG:
2535 if (FIELDS_NOT_SUPPORTED(ib_spec->flow_tag,
2536 LAST_FLOW_TAG_FIELD))
2537 return -EOPNOTSUPP;
2538 if (ib_spec->flow_tag.tag_id >= BIT(24))
2539 return -EINVAL;
2540
Boris Pismenny075572d2017-08-16 09:33:30 +03002541 action->flow_tag = ib_spec->flow_tag.tag_id;
Matan Baraka9db0ec2017-08-16 09:43:48 +03002542 action->has_flow_tag = true;
Moses Reuben2ac693f2017-01-18 14:59:50 +02002543 break;
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002544 case IB_FLOW_SPEC_ACTION_DROP:
2545 if (FIELDS_NOT_SUPPORTED(ib_spec->drop,
2546 LAST_DROP_FIELD))
2547 return -EOPNOTSUPP;
Boris Pismenny075572d2017-08-16 09:33:30 +03002548 action->action |= MLX5_FLOW_CONTEXT_ACTION_DROP;
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002549 break;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002550 default:
2551 return -EINVAL;
2552 }
2553
2554 return 0;
2555}
2556
2557/* If a flow could catch both multicast and unicast packets,
2558 * it won't fall into the multicast flow steering table and this rule
2559 * could steal other multicast packets.
2560 */
Yishai Hadasa550ddf2017-08-17 15:52:33 +03002561static bool flow_is_multicast_only(const struct ib_flow_attr *ib_attr)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002562{
Yishai Hadas81e30882017-06-08 16:15:09 +03002563 union ib_flow_spec *flow_spec;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002564
2565 if (ib_attr->type != IB_FLOW_ATTR_NORMAL ||
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002566 ib_attr->num_of_specs < 1)
2567 return false;
2568
Yishai Hadas81e30882017-06-08 16:15:09 +03002569 flow_spec = (union ib_flow_spec *)(ib_attr + 1);
2570 if (flow_spec->type == IB_FLOW_SPEC_IPV4) {
2571 struct ib_flow_spec_ipv4 *ipv4_spec;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002572
Yishai Hadas81e30882017-06-08 16:15:09 +03002573 ipv4_spec = (struct ib_flow_spec_ipv4 *)flow_spec;
2574 if (ipv4_is_multicast(ipv4_spec->val.dst_ip))
2575 return true;
2576
2577 return false;
2578 }
2579
2580 if (flow_spec->type == IB_FLOW_SPEC_ETH) {
2581 struct ib_flow_spec_eth *eth_spec;
2582
2583 eth_spec = (struct ib_flow_spec_eth *)flow_spec;
2584 return is_multicast_ether_addr(eth_spec->mask.dst_mac) &&
2585 is_multicast_ether_addr(eth_spec->val.dst_mac);
2586 }
2587
2588 return false;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002589}
2590
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002591static bool is_valid_ethertype(struct mlx5_core_dev *mdev,
2592 const struct ib_flow_attr *flow_attr,
Ariel Levkovich0f750962017-04-03 13:11:02 +03002593 bool check_inner)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002594{
2595 union ib_flow_spec *ib_spec = (union ib_flow_spec *)(flow_attr + 1);
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002596 int match_ipv = check_inner ?
2597 MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2598 ft_field_support.inner_ip_version) :
2599 MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2600 ft_field_support.outer_ip_version);
Ariel Levkovich0f750962017-04-03 13:11:02 +03002601 int inner_bit = check_inner ? IB_FLOW_SPEC_INNER : 0;
2602 bool ipv4_spec_valid, ipv6_spec_valid;
2603 unsigned int ip_spec_type = 0;
2604 bool has_ethertype = false;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002605 unsigned int spec_index;
Ariel Levkovich0f750962017-04-03 13:11:02 +03002606 bool mask_valid = true;
2607 u16 eth_type = 0;
2608 bool type_valid;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002609
2610 /* Validate that ethertype is correct */
2611 for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
Ariel Levkovich0f750962017-04-03 13:11:02 +03002612 if ((ib_spec->type == (IB_FLOW_SPEC_ETH | inner_bit)) &&
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002613 ib_spec->eth.mask.ether_type) {
Ariel Levkovich0f750962017-04-03 13:11:02 +03002614 mask_valid = (ib_spec->eth.mask.ether_type ==
2615 htons(0xffff));
2616 has_ethertype = true;
2617 eth_type = ntohs(ib_spec->eth.val.ether_type);
2618 } else if ((ib_spec->type == (IB_FLOW_SPEC_IPV4 | inner_bit)) ||
2619 (ib_spec->type == (IB_FLOW_SPEC_IPV6 | inner_bit))) {
2620 ip_spec_type = ib_spec->type;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002621 }
2622 ib_spec = (void *)ib_spec + ib_spec->size;
2623 }
Ariel Levkovich0f750962017-04-03 13:11:02 +03002624
2625 type_valid = (!has_ethertype) || (!ip_spec_type);
2626 if (!type_valid && mask_valid) {
2627 ipv4_spec_valid = (eth_type == ETH_P_IP) &&
2628 (ip_spec_type == (IB_FLOW_SPEC_IPV4 | inner_bit));
2629 ipv6_spec_valid = (eth_type == ETH_P_IPV6) &&
2630 (ip_spec_type == (IB_FLOW_SPEC_IPV6 | inner_bit));
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002631
2632 type_valid = (ipv4_spec_valid) || (ipv6_spec_valid) ||
2633 (((eth_type == ETH_P_MPLS_UC) ||
2634 (eth_type == ETH_P_MPLS_MC)) && match_ipv);
Ariel Levkovich0f750962017-04-03 13:11:02 +03002635 }
2636
2637 return type_valid;
2638}
2639
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002640static bool is_valid_attr(struct mlx5_core_dev *mdev,
2641 const struct ib_flow_attr *flow_attr)
Ariel Levkovich0f750962017-04-03 13:11:02 +03002642{
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002643 return is_valid_ethertype(mdev, flow_attr, false) &&
2644 is_valid_ethertype(mdev, flow_attr, true);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002645}
2646
2647static void put_flow_table(struct mlx5_ib_dev *dev,
2648 struct mlx5_ib_flow_prio *prio, bool ft_added)
2649{
2650 prio->refcount -= !!ft_added;
2651 if (!prio->refcount) {
2652 mlx5_destroy_flow_table(prio->flow_table);
2653 prio->flow_table = NULL;
2654 }
2655}
2656
2657static int mlx5_ib_destroy_flow(struct ib_flow *flow_id)
2658{
2659 struct mlx5_ib_dev *dev = to_mdev(flow_id->qp->device);
2660 struct mlx5_ib_flow_handler *handler = container_of(flow_id,
2661 struct mlx5_ib_flow_handler,
2662 ibflow);
2663 struct mlx5_ib_flow_handler *iter, *tmp;
2664
Mark Bloch9a4ca382018-01-16 14:42:35 +00002665 mutex_lock(&dev->flow_db->lock);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002666
2667 list_for_each_entry_safe(iter, tmp, &handler->list, list) {
Mark Bloch74491de2016-08-31 11:24:25 +00002668 mlx5_del_flow_rules(iter->rule);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002669 put_flow_table(dev, iter->prio, true);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002670 list_del(&iter->list);
2671 kfree(iter);
2672 }
2673
Mark Bloch74491de2016-08-31 11:24:25 +00002674 mlx5_del_flow_rules(handler->rule);
Maor Gottlieb5497adc2016-08-28 14:16:31 +03002675 put_flow_table(dev, handler->prio, true);
Mark Bloch9a4ca382018-01-16 14:42:35 +00002676 mutex_unlock(&dev->flow_db->lock);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002677
2678 kfree(handler);
2679
2680 return 0;
2681}
2682
Maor Gottlieb35d190112016-03-07 18:51:47 +02002683static int ib_prio_to_core_prio(unsigned int priority, bool dont_trap)
2684{
2685 priority *= 2;
2686 if (!dont_trap)
2687 priority++;
2688 return priority;
2689}
2690
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002691enum flow_table_type {
2692 MLX5_IB_FT_RX,
2693 MLX5_IB_FT_TX
2694};
2695
Maor Gottlieb00b7c2a2017-03-29 06:09:01 +03002696#define MLX5_FS_MAX_TYPES 6
2697#define MLX5_FS_MAX_ENTRIES BIT(16)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002698static struct mlx5_ib_flow_prio *get_flow_table(struct mlx5_ib_dev *dev,
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002699 struct ib_flow_attr *flow_attr,
2700 enum flow_table_type ft_type)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002701{
Maor Gottlieb35d190112016-03-07 18:51:47 +02002702 bool dont_trap = flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002703 struct mlx5_flow_namespace *ns = NULL;
2704 struct mlx5_ib_flow_prio *prio;
2705 struct mlx5_flow_table *ft;
Maor Gottliebdac388e2017-03-29 06:09:00 +03002706 int max_table_size;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002707 int num_entries;
2708 int num_groups;
2709 int priority;
2710 int err = 0;
2711
Maor Gottliebdac388e2017-03-29 06:09:00 +03002712 max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
2713 log_max_ft_size));
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002714 if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
Maor Gottlieb35d190112016-03-07 18:51:47 +02002715 if (flow_is_multicast_only(flow_attr) &&
2716 !dont_trap)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002717 priority = MLX5_IB_FLOW_MCAST_PRIO;
2718 else
Maor Gottlieb35d190112016-03-07 18:51:47 +02002719 priority = ib_prio_to_core_prio(flow_attr->priority,
2720 dont_trap);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002721 ns = mlx5_get_flow_namespace(dev->mdev,
2722 MLX5_FLOW_NAMESPACE_BYPASS);
2723 num_entries = MLX5_FS_MAX_ENTRIES;
2724 num_groups = MLX5_FS_MAX_TYPES;
Mark Bloch9a4ca382018-01-16 14:42:35 +00002725 prio = &dev->flow_db->prios[priority];
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002726 } else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
2727 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
2728 ns = mlx5_get_flow_namespace(dev->mdev,
2729 MLX5_FLOW_NAMESPACE_LEFTOVERS);
2730 build_leftovers_ft_param(&priority,
2731 &num_entries,
2732 &num_groups);
Mark Bloch9a4ca382018-01-16 14:42:35 +00002733 prio = &dev->flow_db->prios[MLX5_IB_FLOW_LEFTOVERS_PRIO];
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002734 } else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
2735 if (!MLX5_CAP_FLOWTABLE(dev->mdev,
2736 allow_sniffer_and_nic_rx_shared_tir))
2737 return ERR_PTR(-ENOTSUPP);
2738
2739 ns = mlx5_get_flow_namespace(dev->mdev, ft_type == MLX5_IB_FT_RX ?
2740 MLX5_FLOW_NAMESPACE_SNIFFER_RX :
2741 MLX5_FLOW_NAMESPACE_SNIFFER_TX);
2742
Mark Bloch9a4ca382018-01-16 14:42:35 +00002743 prio = &dev->flow_db->sniffer[ft_type];
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002744 priority = 0;
2745 num_entries = 1;
2746 num_groups = 1;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002747 }
2748
2749 if (!ns)
2750 return ERR_PTR(-ENOTSUPP);
2751
Maor Gottliebdac388e2017-03-29 06:09:00 +03002752 if (num_entries > max_table_size)
2753 return ERR_PTR(-ENOMEM);
2754
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002755 ft = prio->flow_table;
2756 if (!ft) {
2757 ft = mlx5_create_auto_grouped_flow_table(ns, priority,
2758 num_entries,
Maor Gottliebd63cd282016-04-29 01:36:35 +03002759 num_groups,
Hadar Hen Zionc9f1b072016-11-07 15:14:44 +02002760 0, 0);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002761
2762 if (!IS_ERR(ft)) {
2763 prio->refcount = 0;
2764 prio->flow_table = ft;
2765 } else {
2766 err = PTR_ERR(ft);
2767 }
2768 }
2769
2770 return err ? ERR_PTR(err) : prio;
2771}
2772
Yishai Hadasa550ddf2017-08-17 15:52:33 +03002773static void set_underlay_qp(struct mlx5_ib_dev *dev,
2774 struct mlx5_flow_spec *spec,
2775 u32 underlay_qpn)
2776{
2777 void *misc_params_c = MLX5_ADDR_OF(fte_match_param,
2778 spec->match_criteria,
2779 misc_parameters);
2780 void *misc_params_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
2781 misc_parameters);
2782
2783 if (underlay_qpn &&
2784 MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
2785 ft_field_support.bth_dst_qp)) {
2786 MLX5_SET(fte_match_set_misc,
2787 misc_params_v, bth_dst_qp, underlay_qpn);
2788 MLX5_SET(fte_match_set_misc,
2789 misc_params_c, bth_dst_qp, 0xffffff);
2790 }
2791}
2792
2793static struct mlx5_ib_flow_handler *_create_flow_rule(struct mlx5_ib_dev *dev,
2794 struct mlx5_ib_flow_prio *ft_prio,
2795 const struct ib_flow_attr *flow_attr,
2796 struct mlx5_flow_destination *dst,
2797 u32 underlay_qpn)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002798{
2799 struct mlx5_flow_table *ft = ft_prio->flow_table;
2800 struct mlx5_ib_flow_handler *handler;
Boris Pismenny075572d2017-08-16 09:33:30 +03002801 struct mlx5_flow_act flow_act = {.flow_tag = MLX5_FS_DEFAULT_FLOW_TAG};
Maor Gottliebc5bb1732016-07-04 17:23:05 +03002802 struct mlx5_flow_spec *spec;
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002803 struct mlx5_flow_destination *rule_dst = dst;
Maor Gottliebdd063d02016-08-28 14:16:32 +03002804 const void *ib_flow = (const void *)flow_attr + sizeof(*flow_attr);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002805 unsigned int spec_index;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002806 int err = 0;
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002807 int dest_num = 1;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002808
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002809 if (!is_valid_attr(dev->mdev, flow_attr))
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002810 return ERR_PTR(-EINVAL);
2811
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03002812 spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002813 handler = kzalloc(sizeof(*handler), GFP_KERNEL);
Maor Gottliebc5bb1732016-07-04 17:23:05 +03002814 if (!handler || !spec) {
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002815 err = -ENOMEM;
2816 goto free;
2817 }
2818
2819 INIT_LIST_HEAD(&handler->list);
2820
2821 for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002822 err = parse_flow_attr(dev->mdev, spec->match_criteria,
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002823 spec->match_value,
Boris Pismenny075572d2017-08-16 09:33:30 +03002824 ib_flow, &flow_act);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002825 if (err < 0)
2826 goto free;
2827
2828 ib_flow += ((union ib_flow_spec *)ib_flow)->size;
2829 }
2830
Yishai Hadasa550ddf2017-08-17 15:52:33 +03002831 if (!flow_is_multicast_only(flow_attr))
2832 set_underlay_qp(dev, spec, underlay_qpn);
2833
Mark Bloch018a94e2018-01-16 14:44:29 +00002834 if (dev->rep) {
2835 void *misc;
2836
2837 misc = MLX5_ADDR_OF(fte_match_param, spec->match_value,
2838 misc_parameters);
2839 MLX5_SET(fte_match_set_misc, misc, source_port,
2840 dev->rep->vport);
2841 misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
2842 misc_parameters);
2843 MLX5_SET_TO_ONES(fte_match_set_misc, misc, source_port);
2844 }
2845
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002846 spec->match_criteria_enable = get_match_criteria_enable(spec->match_criteria);
Boris Pismenny075572d2017-08-16 09:33:30 +03002847 if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_DROP) {
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002848 rule_dst = NULL;
2849 dest_num = 0;
2850 } else {
2851 flow_act.action = dst ? MLX5_FLOW_CONTEXT_ACTION_FWD_DEST :
2852 MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO;
2853 }
Moses Reuben2ac693f2017-01-18 14:59:50 +02002854
Matan Baraka9db0ec2017-08-16 09:43:48 +03002855 if (flow_act.has_flow_tag &&
Moses Reuben2ac693f2017-01-18 14:59:50 +02002856 (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
2857 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT)) {
2858 mlx5_ib_warn(dev, "Flow tag %u and attribute type %x isn't allowed in leftovers\n",
Boris Pismenny075572d2017-08-16 09:33:30 +03002859 flow_act.flow_tag, flow_attr->type);
Moses Reuben2ac693f2017-01-18 14:59:50 +02002860 err = -EINVAL;
2861 goto free;
2862 }
Mark Bloch74491de2016-08-31 11:24:25 +00002863 handler->rule = mlx5_add_flow_rules(ft, spec,
Hadar Hen Zion66958ed2016-11-07 15:14:45 +02002864 &flow_act,
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002865 rule_dst, dest_num);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002866
2867 if (IS_ERR(handler->rule)) {
2868 err = PTR_ERR(handler->rule);
2869 goto free;
2870 }
2871
Maor Gottliebd9d49802016-08-28 14:16:33 +03002872 ft_prio->refcount++;
Maor Gottlieb5497adc2016-08-28 14:16:31 +03002873 handler->prio = ft_prio;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002874
2875 ft_prio->flow_table = ft;
2876free:
2877 if (err)
2878 kfree(handler);
Maor Gottliebc5bb1732016-07-04 17:23:05 +03002879 kvfree(spec);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002880 return err ? ERR_PTR(err) : handler;
2881}
2882
Yishai Hadasa550ddf2017-08-17 15:52:33 +03002883static struct mlx5_ib_flow_handler *create_flow_rule(struct mlx5_ib_dev *dev,
2884 struct mlx5_ib_flow_prio *ft_prio,
2885 const struct ib_flow_attr *flow_attr,
2886 struct mlx5_flow_destination *dst)
2887{
2888 return _create_flow_rule(dev, ft_prio, flow_attr, dst, 0);
2889}
2890
Maor Gottlieb35d190112016-03-07 18:51:47 +02002891static struct mlx5_ib_flow_handler *create_dont_trap_rule(struct mlx5_ib_dev *dev,
2892 struct mlx5_ib_flow_prio *ft_prio,
2893 struct ib_flow_attr *flow_attr,
2894 struct mlx5_flow_destination *dst)
2895{
2896 struct mlx5_ib_flow_handler *handler_dst = NULL;
2897 struct mlx5_ib_flow_handler *handler = NULL;
2898
2899 handler = create_flow_rule(dev, ft_prio, flow_attr, NULL);
2900 if (!IS_ERR(handler)) {
2901 handler_dst = create_flow_rule(dev, ft_prio,
2902 flow_attr, dst);
2903 if (IS_ERR(handler_dst)) {
Mark Bloch74491de2016-08-31 11:24:25 +00002904 mlx5_del_flow_rules(handler->rule);
Maor Gottliebd9d49802016-08-28 14:16:33 +03002905 ft_prio->refcount--;
Maor Gottlieb35d190112016-03-07 18:51:47 +02002906 kfree(handler);
2907 handler = handler_dst;
2908 } else {
2909 list_add(&handler_dst->list, &handler->list);
2910 }
2911 }
2912
2913 return handler;
2914}
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002915enum {
2916 LEFTOVERS_MC,
2917 LEFTOVERS_UC,
2918};
2919
2920static struct mlx5_ib_flow_handler *create_leftovers_rule(struct mlx5_ib_dev *dev,
2921 struct mlx5_ib_flow_prio *ft_prio,
2922 struct ib_flow_attr *flow_attr,
2923 struct mlx5_flow_destination *dst)
2924{
2925 struct mlx5_ib_flow_handler *handler_ucast = NULL;
2926 struct mlx5_ib_flow_handler *handler = NULL;
2927
2928 static struct {
2929 struct ib_flow_attr flow_attr;
2930 struct ib_flow_spec_eth eth_flow;
2931 } leftovers_specs[] = {
2932 [LEFTOVERS_MC] = {
2933 .flow_attr = {
2934 .num_of_specs = 1,
2935 .size = sizeof(leftovers_specs[0])
2936 },
2937 .eth_flow = {
2938 .type = IB_FLOW_SPEC_ETH,
2939 .size = sizeof(struct ib_flow_spec_eth),
2940 .mask = {.dst_mac = {0x1} },
2941 .val = {.dst_mac = {0x1} }
2942 }
2943 },
2944 [LEFTOVERS_UC] = {
2945 .flow_attr = {
2946 .num_of_specs = 1,
2947 .size = sizeof(leftovers_specs[0])
2948 },
2949 .eth_flow = {
2950 .type = IB_FLOW_SPEC_ETH,
2951 .size = sizeof(struct ib_flow_spec_eth),
2952 .mask = {.dst_mac = {0x1} },
2953 .val = {.dst_mac = {} }
2954 }
2955 }
2956 };
2957
2958 handler = create_flow_rule(dev, ft_prio,
2959 &leftovers_specs[LEFTOVERS_MC].flow_attr,
2960 dst);
2961 if (!IS_ERR(handler) &&
2962 flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT) {
2963 handler_ucast = create_flow_rule(dev, ft_prio,
2964 &leftovers_specs[LEFTOVERS_UC].flow_attr,
2965 dst);
2966 if (IS_ERR(handler_ucast)) {
Mark Bloch74491de2016-08-31 11:24:25 +00002967 mlx5_del_flow_rules(handler->rule);
Maor Gottliebd9d49802016-08-28 14:16:33 +03002968 ft_prio->refcount--;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002969 kfree(handler);
2970 handler = handler_ucast;
2971 } else {
2972 list_add(&handler_ucast->list, &handler->list);
2973 }
2974 }
2975
2976 return handler;
2977}
2978
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002979static struct mlx5_ib_flow_handler *create_sniffer_rule(struct mlx5_ib_dev *dev,
2980 struct mlx5_ib_flow_prio *ft_rx,
2981 struct mlx5_ib_flow_prio *ft_tx,
2982 struct mlx5_flow_destination *dst)
2983{
2984 struct mlx5_ib_flow_handler *handler_rx;
2985 struct mlx5_ib_flow_handler *handler_tx;
2986 int err;
2987 static const struct ib_flow_attr flow_attr = {
2988 .num_of_specs = 0,
2989 .size = sizeof(flow_attr)
2990 };
2991
2992 handler_rx = create_flow_rule(dev, ft_rx, &flow_attr, dst);
2993 if (IS_ERR(handler_rx)) {
2994 err = PTR_ERR(handler_rx);
2995 goto err;
2996 }
2997
2998 handler_tx = create_flow_rule(dev, ft_tx, &flow_attr, dst);
2999 if (IS_ERR(handler_tx)) {
3000 err = PTR_ERR(handler_tx);
3001 goto err_tx;
3002 }
3003
3004 list_add(&handler_tx->list, &handler_rx->list);
3005
3006 return handler_rx;
3007
3008err_tx:
Mark Bloch74491de2016-08-31 11:24:25 +00003009 mlx5_del_flow_rules(handler_rx->rule);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003010 ft_rx->refcount--;
3011 kfree(handler_rx);
3012err:
3013 return ERR_PTR(err);
3014}
3015
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003016static struct ib_flow *mlx5_ib_create_flow(struct ib_qp *qp,
3017 struct ib_flow_attr *flow_attr,
3018 int domain)
3019{
3020 struct mlx5_ib_dev *dev = to_mdev(qp->device);
Yishai Hadasd9f88e52016-08-28 10:58:37 +03003021 struct mlx5_ib_qp *mqp = to_mqp(qp);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003022 struct mlx5_ib_flow_handler *handler = NULL;
3023 struct mlx5_flow_destination *dst = NULL;
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003024 struct mlx5_ib_flow_prio *ft_prio_tx = NULL;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003025 struct mlx5_ib_flow_prio *ft_prio;
3026 int err;
Yishai Hadasa550ddf2017-08-17 15:52:33 +03003027 int underlay_qpn;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003028
3029 if (flow_attr->priority > MLX5_IB_FLOW_LAST_PRIO)
Maor Gottliebdac388e2017-03-29 06:09:00 +03003030 return ERR_PTR(-ENOMEM);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003031
3032 if (domain != IB_FLOW_DOMAIN_USER ||
Daniel Jurgens508562d2018-01-04 17:25:34 +02003033 flow_attr->port > dev->num_ports ||
Maor Gottlieb35d190112016-03-07 18:51:47 +02003034 (flow_attr->flags & ~IB_FLOW_ATTR_FLAGS_DONT_TRAP))
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003035 return ERR_PTR(-EINVAL);
3036
3037 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
3038 if (!dst)
3039 return ERR_PTR(-ENOMEM);
3040
Mark Bloch9a4ca382018-01-16 14:42:35 +00003041 mutex_lock(&dev->flow_db->lock);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003042
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003043 ft_prio = get_flow_table(dev, flow_attr, MLX5_IB_FT_RX);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003044 if (IS_ERR(ft_prio)) {
3045 err = PTR_ERR(ft_prio);
3046 goto unlock;
3047 }
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003048 if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
3049 ft_prio_tx = get_flow_table(dev, flow_attr, MLX5_IB_FT_TX);
3050 if (IS_ERR(ft_prio_tx)) {
3051 err = PTR_ERR(ft_prio_tx);
3052 ft_prio_tx = NULL;
3053 goto destroy_ft;
3054 }
3055 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003056
3057 dst->type = MLX5_FLOW_DESTINATION_TYPE_TIR;
Yishai Hadasd9f88e52016-08-28 10:58:37 +03003058 if (mqp->flags & MLX5_IB_QP_RSS)
3059 dst->tir_num = mqp->rss_qp.tirn;
3060 else
3061 dst->tir_num = mqp->raw_packet_qp.rq.tirn;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003062
3063 if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
Maor Gottlieb35d190112016-03-07 18:51:47 +02003064 if (flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP) {
3065 handler = create_dont_trap_rule(dev, ft_prio,
3066 flow_attr, dst);
3067 } else {
Yishai Hadasa550ddf2017-08-17 15:52:33 +03003068 underlay_qpn = (mqp->flags & MLX5_IB_QP_UNDERLAY) ?
3069 mqp->underlay_qpn : 0;
3070 handler = _create_flow_rule(dev, ft_prio, flow_attr,
3071 dst, underlay_qpn);
Maor Gottlieb35d190112016-03-07 18:51:47 +02003072 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003073 } else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
3074 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
3075 handler = create_leftovers_rule(dev, ft_prio, flow_attr,
3076 dst);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003077 } else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
3078 handler = create_sniffer_rule(dev, ft_prio, ft_prio_tx, dst);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003079 } else {
3080 err = -EINVAL;
3081 goto destroy_ft;
3082 }
3083
3084 if (IS_ERR(handler)) {
3085 err = PTR_ERR(handler);
3086 handler = NULL;
3087 goto destroy_ft;
3088 }
3089
Mark Bloch9a4ca382018-01-16 14:42:35 +00003090 mutex_unlock(&dev->flow_db->lock);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003091 kfree(dst);
3092
3093 return &handler->ibflow;
3094
3095destroy_ft:
3096 put_flow_table(dev, ft_prio, false);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003097 if (ft_prio_tx)
3098 put_flow_table(dev, ft_prio_tx, false);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003099unlock:
Mark Bloch9a4ca382018-01-16 14:42:35 +00003100 mutex_unlock(&dev->flow_db->lock);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003101 kfree(dst);
3102 kfree(handler);
3103 return ERR_PTR(err);
3104}
3105
Eli Cohene126ba92013-07-07 17:25:49 +03003106static int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
3107{
3108 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
Yishai Hadas81e30882017-06-08 16:15:09 +03003109 struct mlx5_ib_qp *mqp = to_mqp(ibqp);
Eli Cohene126ba92013-07-07 17:25:49 +03003110 int err;
3111
Yishai Hadas81e30882017-06-08 16:15:09 +03003112 if (mqp->flags & MLX5_IB_QP_UNDERLAY) {
3113 mlx5_ib_dbg(dev, "Attaching a multi cast group to underlay QP is not supported\n");
3114 return -EOPNOTSUPP;
3115 }
3116
Jack Morgenstein9603b612014-07-28 23:30:22 +03003117 err = mlx5_core_attach_mcg(dev->mdev, gid, ibqp->qp_num);
Eli Cohene126ba92013-07-07 17:25:49 +03003118 if (err)
3119 mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n",
3120 ibqp->qp_num, gid->raw);
3121
3122 return err;
3123}
3124
3125static int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
3126{
3127 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
3128 int err;
3129
Jack Morgenstein9603b612014-07-28 23:30:22 +03003130 err = mlx5_core_detach_mcg(dev->mdev, gid, ibqp->qp_num);
Eli Cohene126ba92013-07-07 17:25:49 +03003131 if (err)
3132 mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n",
3133 ibqp->qp_num, gid->raw);
3134
3135 return err;
3136}
3137
3138static int init_node_data(struct mlx5_ib_dev *dev)
3139{
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03003140 int err;
Eli Cohene126ba92013-07-07 17:25:49 +03003141
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03003142 err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc);
Eli Cohene126ba92013-07-07 17:25:49 +03003143 if (err)
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03003144 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03003145
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03003146 dev->mdev->rev_id = dev->mdev->pdev->revision;
Eli Cohene126ba92013-07-07 17:25:49 +03003147
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03003148 return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid);
Eli Cohene126ba92013-07-07 17:25:49 +03003149}
3150
3151static ssize_t show_fw_pages(struct device *device, struct device_attribute *attr,
3152 char *buf)
3153{
3154 struct mlx5_ib_dev *dev =
3155 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
3156
Jack Morgenstein9603b612014-07-28 23:30:22 +03003157 return sprintf(buf, "%d\n", dev->mdev->priv.fw_pages);
Eli Cohene126ba92013-07-07 17:25:49 +03003158}
3159
3160static ssize_t show_reg_pages(struct device *device,
3161 struct device_attribute *attr, char *buf)
3162{
3163 struct mlx5_ib_dev *dev =
3164 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
3165
Haggai Eran6aec21f2014-12-11 17:04:23 +02003166 return sprintf(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages));
Eli Cohene126ba92013-07-07 17:25:49 +03003167}
3168
3169static ssize_t show_hca(struct device *device, struct device_attribute *attr,
3170 char *buf)
3171{
3172 struct mlx5_ib_dev *dev =
3173 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
Jack Morgenstein9603b612014-07-28 23:30:22 +03003174 return sprintf(buf, "MT%d\n", dev->mdev->pdev->device);
Eli Cohene126ba92013-07-07 17:25:49 +03003175}
3176
Eli Cohene126ba92013-07-07 17:25:49 +03003177static ssize_t show_rev(struct device *device, struct device_attribute *attr,
3178 char *buf)
3179{
3180 struct mlx5_ib_dev *dev =
3181 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
Jack Morgenstein9603b612014-07-28 23:30:22 +03003182 return sprintf(buf, "%x\n", dev->mdev->rev_id);
Eli Cohene126ba92013-07-07 17:25:49 +03003183}
3184
3185static ssize_t show_board(struct device *device, struct device_attribute *attr,
3186 char *buf)
3187{
3188 struct mlx5_ib_dev *dev =
3189 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
3190 return sprintf(buf, "%.*s\n", MLX5_BOARD_ID_LEN,
Jack Morgenstein9603b612014-07-28 23:30:22 +03003191 dev->mdev->board_id);
Eli Cohene126ba92013-07-07 17:25:49 +03003192}
3193
3194static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03003195static DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL);
3196static DEVICE_ATTR(board_id, S_IRUGO, show_board, NULL);
3197static DEVICE_ATTR(fw_pages, S_IRUGO, show_fw_pages, NULL);
3198static DEVICE_ATTR(reg_pages, S_IRUGO, show_reg_pages, NULL);
3199
3200static struct device_attribute *mlx5_class_attributes[] = {
3201 &dev_attr_hw_rev,
Eli Cohene126ba92013-07-07 17:25:49 +03003202 &dev_attr_hca_type,
3203 &dev_attr_board_id,
3204 &dev_attr_fw_pages,
3205 &dev_attr_reg_pages,
3206};
3207
Haggai Eran7722f472016-02-29 15:45:07 +02003208static void pkey_change_handler(struct work_struct *work)
3209{
3210 struct mlx5_ib_port_resources *ports =
3211 container_of(work, struct mlx5_ib_port_resources,
3212 pkey_change_work);
3213
3214 mutex_lock(&ports->devr->mutex);
3215 mlx5_ib_gsi_pkey_change(ports->gsi);
3216 mutex_unlock(&ports->devr->mutex);
3217}
3218
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03003219static void mlx5_ib_handle_internal_error(struct mlx5_ib_dev *ibdev)
3220{
3221 struct mlx5_ib_qp *mqp;
3222 struct mlx5_ib_cq *send_mcq, *recv_mcq;
3223 struct mlx5_core_cq *mcq;
3224 struct list_head cq_armed_list;
3225 unsigned long flags_qp;
3226 unsigned long flags_cq;
3227 unsigned long flags;
3228
3229 INIT_LIST_HEAD(&cq_armed_list);
3230
3231 /* Go over qp list reside on that ibdev, sync with create/destroy qp.*/
3232 spin_lock_irqsave(&ibdev->reset_flow_resource_lock, flags);
3233 list_for_each_entry(mqp, &ibdev->qp_list, qps_list) {
3234 spin_lock_irqsave(&mqp->sq.lock, flags_qp);
3235 if (mqp->sq.tail != mqp->sq.head) {
3236 send_mcq = to_mcq(mqp->ibqp.send_cq);
3237 spin_lock_irqsave(&send_mcq->lock, flags_cq);
3238 if (send_mcq->mcq.comp &&
3239 mqp->ibqp.send_cq->comp_handler) {
3240 if (!send_mcq->mcq.reset_notify_added) {
3241 send_mcq->mcq.reset_notify_added = 1;
3242 list_add_tail(&send_mcq->mcq.reset_notify,
3243 &cq_armed_list);
3244 }
3245 }
3246 spin_unlock_irqrestore(&send_mcq->lock, flags_cq);
3247 }
3248 spin_unlock_irqrestore(&mqp->sq.lock, flags_qp);
3249 spin_lock_irqsave(&mqp->rq.lock, flags_qp);
3250 /* no handling is needed for SRQ */
3251 if (!mqp->ibqp.srq) {
3252 if (mqp->rq.tail != mqp->rq.head) {
3253 recv_mcq = to_mcq(mqp->ibqp.recv_cq);
3254 spin_lock_irqsave(&recv_mcq->lock, flags_cq);
3255 if (recv_mcq->mcq.comp &&
3256 mqp->ibqp.recv_cq->comp_handler) {
3257 if (!recv_mcq->mcq.reset_notify_added) {
3258 recv_mcq->mcq.reset_notify_added = 1;
3259 list_add_tail(&recv_mcq->mcq.reset_notify,
3260 &cq_armed_list);
3261 }
3262 }
3263 spin_unlock_irqrestore(&recv_mcq->lock,
3264 flags_cq);
3265 }
3266 }
3267 spin_unlock_irqrestore(&mqp->rq.lock, flags_qp);
3268 }
3269 /*At that point all inflight post send were put to be executed as of we
3270 * lock/unlock above locks Now need to arm all involved CQs.
3271 */
3272 list_for_each_entry(mcq, &cq_armed_list, reset_notify) {
3273 mcq->comp(mcq);
3274 }
3275 spin_unlock_irqrestore(&ibdev->reset_flow_resource_lock, flags);
3276}
3277
Maor Gottlieb03404e82017-05-30 10:29:13 +03003278static void delay_drop_handler(struct work_struct *work)
3279{
3280 int err;
3281 struct mlx5_ib_delay_drop *delay_drop =
3282 container_of(work, struct mlx5_ib_delay_drop,
3283 delay_drop_work);
3284
Maor Gottliebfe248c32017-05-30 10:29:14 +03003285 atomic_inc(&delay_drop->events_cnt);
3286
Maor Gottlieb03404e82017-05-30 10:29:13 +03003287 mutex_lock(&delay_drop->lock);
3288 err = mlx5_core_set_delay_drop(delay_drop->dev->mdev,
3289 delay_drop->timeout);
3290 if (err) {
3291 mlx5_ib_warn(delay_drop->dev, "Failed to set delay drop, timeout=%u\n",
3292 delay_drop->timeout);
3293 delay_drop->activate = false;
3294 }
3295 mutex_unlock(&delay_drop->lock);
3296}
3297
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02003298static void mlx5_ib_handle_event(struct work_struct *_work)
Eli Cohene126ba92013-07-07 17:25:49 +03003299{
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02003300 struct mlx5_ib_event_work *work =
3301 container_of(_work, struct mlx5_ib_event_work, work);
3302 struct mlx5_ib_dev *ibdev;
Eli Cohene126ba92013-07-07 17:25:49 +03003303 struct ib_event ibev;
Eli Cohendbaaff22016-10-27 16:36:44 +03003304 bool fatal = false;
Daniel Jurgensaba46212018-02-25 13:39:53 +02003305 u8 port = (u8)work->param;
Eli Cohene126ba92013-07-07 17:25:49 +03003306
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02003307 if (mlx5_core_is_mp_slave(work->dev)) {
3308 ibdev = mlx5_ib_get_ibdev_from_mpi(work->context);
3309 if (!ibdev)
3310 goto out;
3311 } else {
3312 ibdev = work->context;
3313 }
3314
3315 switch (work->event) {
Eli Cohene126ba92013-07-07 17:25:49 +03003316 case MLX5_DEV_EVENT_SYS_ERROR:
Eli Cohene126ba92013-07-07 17:25:49 +03003317 ibev.event = IB_EVENT_DEVICE_FATAL;
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03003318 mlx5_ib_handle_internal_error(ibdev);
Eli Cohendbaaff22016-10-27 16:36:44 +03003319 fatal = true;
Eli Cohene126ba92013-07-07 17:25:49 +03003320 break;
3321
3322 case MLX5_DEV_EVENT_PORT_UP:
Eli Cohene126ba92013-07-07 17:25:49 +03003323 case MLX5_DEV_EVENT_PORT_DOWN:
Noa Osherovich2788cf32016-06-04 15:15:29 +03003324 case MLX5_DEV_EVENT_PORT_INITIALIZED:
Aviv Heller5ec8c832016-09-18 20:48:00 +03003325 /* In RoCE, port up/down events are handled in
3326 * mlx5_netdev_event().
3327 */
3328 if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) ==
3329 IB_LINK_LAYER_ETHERNET)
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02003330 goto out;
Aviv Heller5ec8c832016-09-18 20:48:00 +03003331
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02003332 ibev.event = (work->event == MLX5_DEV_EVENT_PORT_UP) ?
Aviv Heller5ec8c832016-09-18 20:48:00 +03003333 IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
Eli Cohene126ba92013-07-07 17:25:49 +03003334 break;
3335
Eli Cohene126ba92013-07-07 17:25:49 +03003336 case MLX5_DEV_EVENT_LID_CHANGE:
3337 ibev.event = IB_EVENT_LID_CHANGE;
Eli Cohene126ba92013-07-07 17:25:49 +03003338 break;
3339
3340 case MLX5_DEV_EVENT_PKEY_CHANGE:
3341 ibev.event = IB_EVENT_PKEY_CHANGE;
Haggai Eran7722f472016-02-29 15:45:07 +02003342 schedule_work(&ibdev->devr.ports[port - 1].pkey_change_work);
Eli Cohene126ba92013-07-07 17:25:49 +03003343 break;
3344
3345 case MLX5_DEV_EVENT_GUID_CHANGE:
3346 ibev.event = IB_EVENT_GID_CHANGE;
Eli Cohene126ba92013-07-07 17:25:49 +03003347 break;
3348
3349 case MLX5_DEV_EVENT_CLIENT_REREG:
3350 ibev.event = IB_EVENT_CLIENT_REREGISTER;
Eli Cohene126ba92013-07-07 17:25:49 +03003351 break;
Maor Gottlieb03404e82017-05-30 10:29:13 +03003352 case MLX5_DEV_EVENT_DELAY_DROP_TIMEOUT:
3353 schedule_work(&ibdev->delay_drop.delay_drop_work);
3354 goto out;
Saeed Mahameedbdc37922016-09-29 19:35:38 +03003355 default:
Maor Gottlieb03404e82017-05-30 10:29:13 +03003356 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03003357 }
3358
3359 ibev.device = &ibdev->ib_dev;
3360 ibev.element.port_num = port;
3361
Daniel Jurgensaba46212018-02-25 13:39:53 +02003362 if (!rdma_is_port_valid(&ibdev->ib_dev, port)) {
Eli Cohena0c84c32013-09-11 16:35:27 +03003363 mlx5_ib_warn(ibdev, "warning: event on port %d\n", port);
Maor Gottlieb03404e82017-05-30 10:29:13 +03003364 goto out;
Eli Cohena0c84c32013-09-11 16:35:27 +03003365 }
3366
Eli Cohene126ba92013-07-07 17:25:49 +03003367 if (ibdev->ib_active)
3368 ib_dispatch_event(&ibev);
Eli Cohendbaaff22016-10-27 16:36:44 +03003369
3370 if (fatal)
3371 ibdev->ib_active = false;
Maor Gottlieb03404e82017-05-30 10:29:13 +03003372out:
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02003373 kfree(work);
3374}
3375
3376static void mlx5_ib_event(struct mlx5_core_dev *dev, void *context,
3377 enum mlx5_dev_event event, unsigned long param)
3378{
3379 struct mlx5_ib_event_work *work;
3380
3381 work = kmalloc(sizeof(*work), GFP_ATOMIC);
Leon Romanovsky10bea9c2018-01-19 13:07:11 +02003382 if (!work)
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02003383 return;
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02003384
Leon Romanovsky10bea9c2018-01-19 13:07:11 +02003385 INIT_WORK(&work->work, mlx5_ib_handle_event);
3386 work->dev = dev;
3387 work->param = param;
3388 work->context = context;
3389 work->event = event;
3390
3391 queue_work(mlx5_ib_event_wq, &work->work);
Eli Cohene126ba92013-07-07 17:25:49 +03003392}
3393
Maor Gottliebc43f1112017-01-18 14:10:33 +02003394static int set_has_smi_cap(struct mlx5_ib_dev *dev)
3395{
3396 struct mlx5_hca_vport_context vport_ctx;
3397 int err;
3398 int port;
3399
Daniel Jurgens508562d2018-01-04 17:25:34 +02003400 for (port = 1; port <= dev->num_ports; port++) {
Maor Gottliebc43f1112017-01-18 14:10:33 +02003401 dev->mdev->port_caps[port - 1].has_smi = false;
3402 if (MLX5_CAP_GEN(dev->mdev, port_type) ==
3403 MLX5_CAP_PORT_TYPE_IB) {
3404 if (MLX5_CAP_GEN(dev->mdev, ib_virt)) {
3405 err = mlx5_query_hca_vport_context(dev->mdev, 0,
3406 port, 0,
3407 &vport_ctx);
3408 if (err) {
3409 mlx5_ib_err(dev, "query_hca_vport_context for port=%d failed %d\n",
3410 port, err);
3411 return err;
3412 }
3413 dev->mdev->port_caps[port - 1].has_smi =
3414 vport_ctx.has_smi;
3415 } else {
3416 dev->mdev->port_caps[port - 1].has_smi = true;
3417 }
3418 }
3419 }
3420 return 0;
3421}
3422
Eli Cohene126ba92013-07-07 17:25:49 +03003423static void get_ext_port_caps(struct mlx5_ib_dev *dev)
3424{
3425 int port;
3426
Daniel Jurgens508562d2018-01-04 17:25:34 +02003427 for (port = 1; port <= dev->num_ports; port++)
Eli Cohene126ba92013-07-07 17:25:49 +03003428 mlx5_query_ext_port_caps(dev, port);
3429}
3430
Daniel Jurgens32f69e42018-01-04 17:25:36 +02003431static int get_port_caps(struct mlx5_ib_dev *dev, u8 port)
Eli Cohene126ba92013-07-07 17:25:49 +03003432{
3433 struct ib_device_attr *dprops = NULL;
3434 struct ib_port_attr *pprops = NULL;
Dan Carpenterf614fc12015-01-12 11:56:58 +03003435 int err = -ENOMEM;
Matan Barak2528e332015-06-11 16:35:25 +03003436 struct ib_udata uhw = {.inlen = 0, .outlen = 0};
Eli Cohene126ba92013-07-07 17:25:49 +03003437
3438 pprops = kmalloc(sizeof(*pprops), GFP_KERNEL);
3439 if (!pprops)
3440 goto out;
3441
3442 dprops = kmalloc(sizeof(*dprops), GFP_KERNEL);
3443 if (!dprops)
3444 goto out;
3445
Maor Gottliebc43f1112017-01-18 14:10:33 +02003446 err = set_has_smi_cap(dev);
3447 if (err)
3448 goto out;
3449
Matan Barak2528e332015-06-11 16:35:25 +03003450 err = mlx5_ib_query_device(&dev->ib_dev, dprops, &uhw);
Eli Cohene126ba92013-07-07 17:25:49 +03003451 if (err) {
3452 mlx5_ib_warn(dev, "query_device failed %d\n", err);
3453 goto out;
3454 }
3455
Daniel Jurgens32f69e42018-01-04 17:25:36 +02003456 memset(pprops, 0, sizeof(*pprops));
3457 err = mlx5_ib_query_port(&dev->ib_dev, port, pprops);
3458 if (err) {
3459 mlx5_ib_warn(dev, "query_port %d failed %d\n",
3460 port, err);
3461 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03003462 }
3463
Daniel Jurgens32f69e42018-01-04 17:25:36 +02003464 dev->mdev->port_caps[port - 1].pkey_table_len =
3465 dprops->max_pkeys;
3466 dev->mdev->port_caps[port - 1].gid_table_len =
3467 pprops->gid_tbl_len;
3468 mlx5_ib_dbg(dev, "port %d: pkey_table_len %d, gid_table_len %d\n",
3469 port, dprops->max_pkeys, pprops->gid_tbl_len);
3470
Eli Cohene126ba92013-07-07 17:25:49 +03003471out:
3472 kfree(pprops);
3473 kfree(dprops);
3474
3475 return err;
3476}
3477
3478static void destroy_umrc_res(struct mlx5_ib_dev *dev)
3479{
3480 int err;
3481
3482 err = mlx5_mr_cache_cleanup(dev);
3483 if (err)
3484 mlx5_ib_warn(dev, "mr cache cleanup failed\n");
3485
3486 mlx5_ib_destroy_qp(dev->umrc.qp);
Christoph Hellwigadd08d72016-03-03 09:38:22 +01003487 ib_free_cq(dev->umrc.cq);
Eli Cohene126ba92013-07-07 17:25:49 +03003488 ib_dealloc_pd(dev->umrc.pd);
3489}
3490
3491enum {
3492 MAX_UMR_WR = 128,
3493};
3494
3495static int create_umr_res(struct mlx5_ib_dev *dev)
3496{
3497 struct ib_qp_init_attr *init_attr = NULL;
3498 struct ib_qp_attr *attr = NULL;
3499 struct ib_pd *pd;
3500 struct ib_cq *cq;
3501 struct ib_qp *qp;
Eli Cohene126ba92013-07-07 17:25:49 +03003502 int ret;
3503
3504 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
3505 init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL);
3506 if (!attr || !init_attr) {
3507 ret = -ENOMEM;
3508 goto error_0;
3509 }
3510
Christoph Hellwiged082d32016-09-05 12:56:17 +02003511 pd = ib_alloc_pd(&dev->ib_dev, 0);
Eli Cohene126ba92013-07-07 17:25:49 +03003512 if (IS_ERR(pd)) {
3513 mlx5_ib_dbg(dev, "Couldn't create PD for sync UMR QP\n");
3514 ret = PTR_ERR(pd);
3515 goto error_0;
3516 }
3517
Christoph Hellwigadd08d72016-03-03 09:38:22 +01003518 cq = ib_alloc_cq(&dev->ib_dev, NULL, 128, 0, IB_POLL_SOFTIRQ);
Eli Cohene126ba92013-07-07 17:25:49 +03003519 if (IS_ERR(cq)) {
3520 mlx5_ib_dbg(dev, "Couldn't create CQ for sync UMR QP\n");
3521 ret = PTR_ERR(cq);
3522 goto error_2;
3523 }
Eli Cohene126ba92013-07-07 17:25:49 +03003524
3525 init_attr->send_cq = cq;
3526 init_attr->recv_cq = cq;
3527 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
3528 init_attr->cap.max_send_wr = MAX_UMR_WR;
3529 init_attr->cap.max_send_sge = 1;
3530 init_attr->qp_type = MLX5_IB_QPT_REG_UMR;
3531 init_attr->port_num = 1;
3532 qp = mlx5_ib_create_qp(pd, init_attr, NULL);
3533 if (IS_ERR(qp)) {
3534 mlx5_ib_dbg(dev, "Couldn't create sync UMR QP\n");
3535 ret = PTR_ERR(qp);
3536 goto error_3;
3537 }
3538 qp->device = &dev->ib_dev;
3539 qp->real_qp = qp;
3540 qp->uobject = NULL;
3541 qp->qp_type = MLX5_IB_QPT_REG_UMR;
Majd Dibbiny31fde032017-10-30 14:23:13 +02003542 qp->send_cq = init_attr->send_cq;
3543 qp->recv_cq = init_attr->recv_cq;
Eli Cohene126ba92013-07-07 17:25:49 +03003544
3545 attr->qp_state = IB_QPS_INIT;
3546 attr->port_num = 1;
3547 ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_PKEY_INDEX |
3548 IB_QP_PORT, NULL);
3549 if (ret) {
3550 mlx5_ib_dbg(dev, "Couldn't modify UMR QP\n");
3551 goto error_4;
3552 }
3553
3554 memset(attr, 0, sizeof(*attr));
3555 attr->qp_state = IB_QPS_RTR;
3556 attr->path_mtu = IB_MTU_256;
3557
3558 ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
3559 if (ret) {
3560 mlx5_ib_dbg(dev, "Couldn't modify umr QP to rtr\n");
3561 goto error_4;
3562 }
3563
3564 memset(attr, 0, sizeof(*attr));
3565 attr->qp_state = IB_QPS_RTS;
3566 ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
3567 if (ret) {
3568 mlx5_ib_dbg(dev, "Couldn't modify umr QP to rts\n");
3569 goto error_4;
3570 }
3571
3572 dev->umrc.qp = qp;
3573 dev->umrc.cq = cq;
Eli Cohene126ba92013-07-07 17:25:49 +03003574 dev->umrc.pd = pd;
3575
3576 sema_init(&dev->umrc.sem, MAX_UMR_WR);
3577 ret = mlx5_mr_cache_init(dev);
3578 if (ret) {
3579 mlx5_ib_warn(dev, "mr cache init failed %d\n", ret);
3580 goto error_4;
3581 }
3582
3583 kfree(attr);
3584 kfree(init_attr);
3585
3586 return 0;
3587
3588error_4:
3589 mlx5_ib_destroy_qp(qp);
3590
3591error_3:
Christoph Hellwigadd08d72016-03-03 09:38:22 +01003592 ib_free_cq(cq);
Eli Cohene126ba92013-07-07 17:25:49 +03003593
3594error_2:
Eli Cohene126ba92013-07-07 17:25:49 +03003595 ib_dealloc_pd(pd);
3596
3597error_0:
3598 kfree(attr);
3599 kfree(init_attr);
3600 return ret;
3601}
3602
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03003603static u8 mlx5_get_umr_fence(u8 umr_fence_cap)
3604{
3605 switch (umr_fence_cap) {
3606 case MLX5_CAP_UMR_FENCE_NONE:
3607 return MLX5_FENCE_MODE_NONE;
3608 case MLX5_CAP_UMR_FENCE_SMALL:
3609 return MLX5_FENCE_MODE_INITIATOR_SMALL;
3610 default:
3611 return MLX5_FENCE_MODE_STRONG_ORDERING;
3612 }
3613}
3614
Eli Cohene126ba92013-07-07 17:25:49 +03003615static int create_dev_resources(struct mlx5_ib_resources *devr)
3616{
3617 struct ib_srq_init_attr attr;
3618 struct mlx5_ib_dev *dev;
Matan Barakbcf4c1e2015-06-11 16:35:20 +03003619 struct ib_cq_init_attr cq_attr = {.cqe = 1};
Haggai Eran7722f472016-02-29 15:45:07 +02003620 int port;
Eli Cohene126ba92013-07-07 17:25:49 +03003621 int ret = 0;
3622
3623 dev = container_of(devr, struct mlx5_ib_dev, devr);
3624
Haggai Erand16e91d2016-02-29 15:45:05 +02003625 mutex_init(&devr->mutex);
3626
Eli Cohene126ba92013-07-07 17:25:49 +03003627 devr->p0 = mlx5_ib_alloc_pd(&dev->ib_dev, NULL, NULL);
3628 if (IS_ERR(devr->p0)) {
3629 ret = PTR_ERR(devr->p0);
3630 goto error0;
3631 }
3632 devr->p0->device = &dev->ib_dev;
3633 devr->p0->uobject = NULL;
3634 atomic_set(&devr->p0->usecnt, 0);
3635
Matan Barakbcf4c1e2015-06-11 16:35:20 +03003636 devr->c0 = mlx5_ib_create_cq(&dev->ib_dev, &cq_attr, NULL, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03003637 if (IS_ERR(devr->c0)) {
3638 ret = PTR_ERR(devr->c0);
3639 goto error1;
3640 }
3641 devr->c0->device = &dev->ib_dev;
3642 devr->c0->uobject = NULL;
3643 devr->c0->comp_handler = NULL;
3644 devr->c0->event_handler = NULL;
3645 devr->c0->cq_context = NULL;
3646 atomic_set(&devr->c0->usecnt, 0);
3647
3648 devr->x0 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
3649 if (IS_ERR(devr->x0)) {
3650 ret = PTR_ERR(devr->x0);
3651 goto error2;
3652 }
3653 devr->x0->device = &dev->ib_dev;
3654 devr->x0->inode = NULL;
3655 atomic_set(&devr->x0->usecnt, 0);
3656 mutex_init(&devr->x0->tgt_qp_mutex);
3657 INIT_LIST_HEAD(&devr->x0->tgt_qp_list);
3658
3659 devr->x1 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
3660 if (IS_ERR(devr->x1)) {
3661 ret = PTR_ERR(devr->x1);
3662 goto error3;
3663 }
3664 devr->x1->device = &dev->ib_dev;
3665 devr->x1->inode = NULL;
3666 atomic_set(&devr->x1->usecnt, 0);
3667 mutex_init(&devr->x1->tgt_qp_mutex);
3668 INIT_LIST_HEAD(&devr->x1->tgt_qp_list);
3669
3670 memset(&attr, 0, sizeof(attr));
3671 attr.attr.max_sge = 1;
3672 attr.attr.max_wr = 1;
3673 attr.srq_type = IB_SRQT_XRC;
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03003674 attr.ext.cq = devr->c0;
Eli Cohene126ba92013-07-07 17:25:49 +03003675 attr.ext.xrc.xrcd = devr->x0;
3676
3677 devr->s0 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
3678 if (IS_ERR(devr->s0)) {
3679 ret = PTR_ERR(devr->s0);
3680 goto error4;
3681 }
3682 devr->s0->device = &dev->ib_dev;
3683 devr->s0->pd = devr->p0;
3684 devr->s0->uobject = NULL;
3685 devr->s0->event_handler = NULL;
3686 devr->s0->srq_context = NULL;
3687 devr->s0->srq_type = IB_SRQT_XRC;
3688 devr->s0->ext.xrc.xrcd = devr->x0;
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03003689 devr->s0->ext.cq = devr->c0;
Eli Cohene126ba92013-07-07 17:25:49 +03003690 atomic_inc(&devr->s0->ext.xrc.xrcd->usecnt);
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03003691 atomic_inc(&devr->s0->ext.cq->usecnt);
Eli Cohene126ba92013-07-07 17:25:49 +03003692 atomic_inc(&devr->p0->usecnt);
3693 atomic_set(&devr->s0->usecnt, 0);
3694
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03003695 memset(&attr, 0, sizeof(attr));
3696 attr.attr.max_sge = 1;
3697 attr.attr.max_wr = 1;
3698 attr.srq_type = IB_SRQT_BASIC;
3699 devr->s1 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
3700 if (IS_ERR(devr->s1)) {
3701 ret = PTR_ERR(devr->s1);
3702 goto error5;
3703 }
3704 devr->s1->device = &dev->ib_dev;
3705 devr->s1->pd = devr->p0;
3706 devr->s1->uobject = NULL;
3707 devr->s1->event_handler = NULL;
3708 devr->s1->srq_context = NULL;
3709 devr->s1->srq_type = IB_SRQT_BASIC;
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03003710 devr->s1->ext.cq = devr->c0;
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03003711 atomic_inc(&devr->p0->usecnt);
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03003712 atomic_set(&devr->s1->usecnt, 0);
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03003713
Haggai Eran7722f472016-02-29 15:45:07 +02003714 for (port = 0; port < ARRAY_SIZE(devr->ports); ++port) {
3715 INIT_WORK(&devr->ports[port].pkey_change_work,
3716 pkey_change_handler);
3717 devr->ports[port].devr = devr;
3718 }
3719
Eli Cohene126ba92013-07-07 17:25:49 +03003720 return 0;
3721
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03003722error5:
3723 mlx5_ib_destroy_srq(devr->s0);
Eli Cohene126ba92013-07-07 17:25:49 +03003724error4:
3725 mlx5_ib_dealloc_xrcd(devr->x1);
3726error3:
3727 mlx5_ib_dealloc_xrcd(devr->x0);
3728error2:
3729 mlx5_ib_destroy_cq(devr->c0);
3730error1:
3731 mlx5_ib_dealloc_pd(devr->p0);
3732error0:
3733 return ret;
3734}
3735
3736static void destroy_dev_resources(struct mlx5_ib_resources *devr)
3737{
Haggai Eran7722f472016-02-29 15:45:07 +02003738 struct mlx5_ib_dev *dev =
3739 container_of(devr, struct mlx5_ib_dev, devr);
3740 int port;
3741
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03003742 mlx5_ib_destroy_srq(devr->s1);
Eli Cohene126ba92013-07-07 17:25:49 +03003743 mlx5_ib_destroy_srq(devr->s0);
3744 mlx5_ib_dealloc_xrcd(devr->x0);
3745 mlx5_ib_dealloc_xrcd(devr->x1);
3746 mlx5_ib_destroy_cq(devr->c0);
3747 mlx5_ib_dealloc_pd(devr->p0);
Haggai Eran7722f472016-02-29 15:45:07 +02003748
3749 /* Make sure no change P_Key work items are still executing */
3750 for (port = 0; port < dev->num_ports; ++port)
3751 cancel_work_sync(&devr->ports[port].pkey_change_work);
Eli Cohene126ba92013-07-07 17:25:49 +03003752}
3753
Achiad Shochate53505a2015-12-23 18:47:25 +02003754static u32 get_core_cap_flags(struct ib_device *ibdev)
3755{
3756 struct mlx5_ib_dev *dev = to_mdev(ibdev);
3757 enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, 1);
3758 u8 l3_type_cap = MLX5_CAP_ROCE(dev->mdev, l3_type);
3759 u8 roce_version_cap = MLX5_CAP_ROCE(dev->mdev, roce_version);
Daniel Jurgens85c7c012018-01-04 17:25:43 +02003760 bool raw_support = !mlx5_core_mp_enabled(dev->mdev);
Achiad Shochate53505a2015-12-23 18:47:25 +02003761 u32 ret = 0;
3762
3763 if (ll == IB_LINK_LAYER_INFINIBAND)
3764 return RDMA_CORE_PORT_IBA_IB;
3765
Daniel Jurgens85c7c012018-01-04 17:25:43 +02003766 if (raw_support)
3767 ret = RDMA_CORE_PORT_RAW_PACKET;
Or Gerlitz72cd5712017-01-24 13:02:36 +02003768
Achiad Shochate53505a2015-12-23 18:47:25 +02003769 if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV4_CAP))
Or Gerlitz72cd5712017-01-24 13:02:36 +02003770 return ret;
Achiad Shochate53505a2015-12-23 18:47:25 +02003771
3772 if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV6_CAP))
Or Gerlitz72cd5712017-01-24 13:02:36 +02003773 return ret;
Achiad Shochate53505a2015-12-23 18:47:25 +02003774
3775 if (roce_version_cap & MLX5_ROCE_VERSION_1_CAP)
3776 ret |= RDMA_CORE_PORT_IBA_ROCE;
3777
3778 if (roce_version_cap & MLX5_ROCE_VERSION_2_CAP)
3779 ret |= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
3780
3781 return ret;
3782}
3783
Ira Weiny77386132015-05-13 20:02:58 -04003784static int mlx5_port_immutable(struct ib_device *ibdev, u8 port_num,
3785 struct ib_port_immutable *immutable)
3786{
3787 struct ib_port_attr attr;
Or Gerlitzca5b91d2016-11-27 16:51:36 +02003788 struct mlx5_ib_dev *dev = to_mdev(ibdev);
3789 enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, port_num);
Ira Weiny77386132015-05-13 20:02:58 -04003790 int err;
3791
Or Gerlitzc4550c62017-01-24 13:02:39 +02003792 immutable->core_cap_flags = get_core_cap_flags(ibdev);
3793
3794 err = ib_query_port(ibdev, port_num, &attr);
Ira Weiny77386132015-05-13 20:02:58 -04003795 if (err)
3796 return err;
3797
3798 immutable->pkey_tbl_len = attr.pkey_tbl_len;
3799 immutable->gid_tbl_len = attr.gid_tbl_len;
Achiad Shochate53505a2015-12-23 18:47:25 +02003800 immutable->core_cap_flags = get_core_cap_flags(ibdev);
Or Gerlitzca5b91d2016-11-27 16:51:36 +02003801 if ((ll == IB_LINK_LAYER_INFINIBAND) || MLX5_CAP_GEN(dev->mdev, roce))
3802 immutable->max_mad_size = IB_MGMT_MAD_SIZE;
Ira Weiny77386132015-05-13 20:02:58 -04003803
3804 return 0;
3805}
3806
Mark Bloch8e6efa32017-11-06 12:22:13 +00003807static int mlx5_port_rep_immutable(struct ib_device *ibdev, u8 port_num,
3808 struct ib_port_immutable *immutable)
3809{
3810 struct ib_port_attr attr;
3811 int err;
3812
3813 immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET;
3814
3815 err = ib_query_port(ibdev, port_num, &attr);
3816 if (err)
3817 return err;
3818
3819 immutable->pkey_tbl_len = attr.pkey_tbl_len;
3820 immutable->gid_tbl_len = attr.gid_tbl_len;
3821 immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET;
3822
3823 return 0;
3824}
3825
Leon Romanovsky9abb0d12017-06-27 16:49:53 +03003826static void get_dev_fw_str(struct ib_device *ibdev, char *str)
Ira Weinyc7342822016-06-15 02:22:01 -04003827{
3828 struct mlx5_ib_dev *dev =
3829 container_of(ibdev, struct mlx5_ib_dev, ib_dev);
Leon Romanovsky9abb0d12017-06-27 16:49:53 +03003830 snprintf(str, IB_FW_VERSION_NAME_MAX, "%d.%d.%04d",
3831 fw_rev_maj(dev->mdev), fw_rev_min(dev->mdev),
3832 fw_rev_sub(dev->mdev));
Ira Weinyc7342822016-06-15 02:22:01 -04003833}
3834
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003835static int mlx5_eth_lag_init(struct mlx5_ib_dev *dev)
Aviv Heller9ef9c642016-09-18 20:48:01 +03003836{
3837 struct mlx5_core_dev *mdev = dev->mdev;
3838 struct mlx5_flow_namespace *ns = mlx5_get_flow_namespace(mdev,
3839 MLX5_FLOW_NAMESPACE_LAG);
3840 struct mlx5_flow_table *ft;
3841 int err;
3842
3843 if (!ns || !mlx5_lag_is_active(mdev))
3844 return 0;
3845
3846 err = mlx5_cmd_create_vport_lag(mdev);
3847 if (err)
3848 return err;
3849
3850 ft = mlx5_create_lag_demux_flow_table(ns, 0, 0);
3851 if (IS_ERR(ft)) {
3852 err = PTR_ERR(ft);
3853 goto err_destroy_vport_lag;
3854 }
3855
Mark Bloch9a4ca382018-01-16 14:42:35 +00003856 dev->flow_db->lag_demux_ft = ft;
Aviv Heller9ef9c642016-09-18 20:48:01 +03003857 return 0;
3858
3859err_destroy_vport_lag:
3860 mlx5_cmd_destroy_vport_lag(mdev);
3861 return err;
3862}
3863
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003864static void mlx5_eth_lag_cleanup(struct mlx5_ib_dev *dev)
Aviv Heller9ef9c642016-09-18 20:48:01 +03003865{
3866 struct mlx5_core_dev *mdev = dev->mdev;
3867
Mark Bloch9a4ca382018-01-16 14:42:35 +00003868 if (dev->flow_db->lag_demux_ft) {
3869 mlx5_destroy_flow_table(dev->flow_db->lag_demux_ft);
3870 dev->flow_db->lag_demux_ft = NULL;
Aviv Heller9ef9c642016-09-18 20:48:01 +03003871
3872 mlx5_cmd_destroy_vport_lag(mdev);
3873 }
3874}
3875
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +02003876static int mlx5_add_netdev_notifier(struct mlx5_ib_dev *dev, u8 port_num)
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003877{
Achiad Shochate53505a2015-12-23 18:47:25 +02003878 int err;
3879
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +02003880 dev->roce[port_num].nb.notifier_call = mlx5_netdev_event;
3881 err = register_netdevice_notifier(&dev->roce[port_num].nb);
Aviv Heller5ec8c832016-09-18 20:48:00 +03003882 if (err) {
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +02003883 dev->roce[port_num].nb.notifier_call = NULL;
Achiad Shochate53505a2015-12-23 18:47:25 +02003884 return err;
Aviv Heller5ec8c832016-09-18 20:48:00 +03003885 }
Achiad Shochate53505a2015-12-23 18:47:25 +02003886
Or Gerlitzd012f5d2016-11-27 16:51:34 +02003887 return 0;
3888}
Achiad Shochate53505a2015-12-23 18:47:25 +02003889
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +02003890static void mlx5_remove_netdev_notifier(struct mlx5_ib_dev *dev, u8 port_num)
Eli Cohene126ba92013-07-07 17:25:49 +03003891{
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +02003892 if (dev->roce[port_num].nb.notifier_call) {
3893 unregister_netdevice_notifier(&dev->roce[port_num].nb);
3894 dev->roce[port_num].nb.notifier_call = NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03003895 }
3896}
3897
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +02003898static int mlx5_enable_eth(struct mlx5_ib_dev *dev, u8 port_num)
Eli Cohene126ba92013-07-07 17:25:49 +03003899{
Eli Cohene126ba92013-07-07 17:25:49 +03003900 int err;
3901
Or Gerlitzca5b91d2016-11-27 16:51:36 +02003902 if (MLX5_CAP_GEN(dev->mdev, roce)) {
3903 err = mlx5_nic_vport_enable_roce(dev->mdev);
3904 if (err)
Mark Bloch8e6efa32017-11-06 12:22:13 +00003905 return err;
Or Gerlitzca5b91d2016-11-27 16:51:36 +02003906 }
Achiad Shochate53505a2015-12-23 18:47:25 +02003907
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003908 err = mlx5_eth_lag_init(dev);
Aviv Heller9ef9c642016-09-18 20:48:01 +03003909 if (err)
3910 goto err_disable_roce;
3911
Achiad Shochate53505a2015-12-23 18:47:25 +02003912 return 0;
3913
Aviv Heller9ef9c642016-09-18 20:48:01 +03003914err_disable_roce:
Or Gerlitzca5b91d2016-11-27 16:51:36 +02003915 if (MLX5_CAP_GEN(dev->mdev, roce))
3916 mlx5_nic_vport_disable_roce(dev->mdev);
Aviv Heller9ef9c642016-09-18 20:48:01 +03003917
Achiad Shochate53505a2015-12-23 18:47:25 +02003918 return err;
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003919}
3920
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003921static void mlx5_disable_eth(struct mlx5_ib_dev *dev)
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003922{
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003923 mlx5_eth_lag_cleanup(dev);
Or Gerlitzca5b91d2016-11-27 16:51:36 +02003924 if (MLX5_CAP_GEN(dev->mdev, roce))
3925 mlx5_nic_vport_disable_roce(dev->mdev);
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003926}
3927
Parav Pandite1f24a72017-04-16 07:29:29 +03003928struct mlx5_ib_counter {
Kamal Heib7c16f472017-01-18 15:25:09 +02003929 const char *name;
3930 size_t offset;
3931};
3932
3933#define INIT_Q_COUNTER(_name) \
3934 { .name = #_name, .offset = MLX5_BYTE_OFF(query_q_counter_out, _name)}
3935
Parav Pandite1f24a72017-04-16 07:29:29 +03003936static const struct mlx5_ib_counter basic_q_cnts[] = {
Kamal Heib7c16f472017-01-18 15:25:09 +02003937 INIT_Q_COUNTER(rx_write_requests),
3938 INIT_Q_COUNTER(rx_read_requests),
3939 INIT_Q_COUNTER(rx_atomic_requests),
3940 INIT_Q_COUNTER(out_of_buffer),
3941};
3942
Parav Pandite1f24a72017-04-16 07:29:29 +03003943static const struct mlx5_ib_counter out_of_seq_q_cnts[] = {
Kamal Heib7c16f472017-01-18 15:25:09 +02003944 INIT_Q_COUNTER(out_of_sequence),
3945};
3946
Parav Pandite1f24a72017-04-16 07:29:29 +03003947static const struct mlx5_ib_counter retrans_q_cnts[] = {
Kamal Heib7c16f472017-01-18 15:25:09 +02003948 INIT_Q_COUNTER(duplicate_request),
3949 INIT_Q_COUNTER(rnr_nak_retry_err),
3950 INIT_Q_COUNTER(packet_seq_err),
3951 INIT_Q_COUNTER(implied_nak_seq_err),
3952 INIT_Q_COUNTER(local_ack_timeout_err),
3953};
3954
Parav Pandite1f24a72017-04-16 07:29:29 +03003955#define INIT_CONG_COUNTER(_name) \
3956 { .name = #_name, .offset = \
3957 MLX5_BYTE_OFF(query_cong_statistics_out, _name ## _high)}
3958
3959static const struct mlx5_ib_counter cong_cnts[] = {
3960 INIT_CONG_COUNTER(rp_cnp_ignored),
3961 INIT_CONG_COUNTER(rp_cnp_handled),
3962 INIT_CONG_COUNTER(np_ecn_marked_roce_packets),
3963 INIT_CONG_COUNTER(np_cnp_sent),
3964};
3965
Parav Pandit58dcb602017-06-19 07:19:37 +03003966static const struct mlx5_ib_counter extended_err_cnts[] = {
3967 INIT_Q_COUNTER(resp_local_length_error),
3968 INIT_Q_COUNTER(resp_cqe_error),
3969 INIT_Q_COUNTER(req_cqe_error),
3970 INIT_Q_COUNTER(req_remote_invalid_request),
3971 INIT_Q_COUNTER(req_remote_access_errors),
3972 INIT_Q_COUNTER(resp_remote_access_errors),
3973 INIT_Q_COUNTER(resp_cqe_flush_error),
3974 INIT_Q_COUNTER(req_cqe_flush_error),
3975};
3976
Parav Pandite1f24a72017-04-16 07:29:29 +03003977static void mlx5_ib_dealloc_counters(struct mlx5_ib_dev *dev)
Mark Bloch0837e862016-06-17 15:10:55 +03003978{
Daniel Jurgensaac44922018-01-04 17:25:40 +02003979 int i;
Mark Bloch0837e862016-06-17 15:10:55 +03003980
Kamal Heib7c16f472017-01-18 15:25:09 +02003981 for (i = 0; i < dev->num_ports; i++) {
Daniel Jurgensaac44922018-01-04 17:25:40 +02003982 if (dev->port[i].cnts.set_id)
3983 mlx5_core_dealloc_q_counter(dev->mdev,
3984 dev->port[i].cnts.set_id);
Parav Pandite1f24a72017-04-16 07:29:29 +03003985 kfree(dev->port[i].cnts.names);
3986 kfree(dev->port[i].cnts.offsets);
Kamal Heib7c16f472017-01-18 15:25:09 +02003987 }
3988}
3989
Parav Pandite1f24a72017-04-16 07:29:29 +03003990static int __mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev,
3991 struct mlx5_ib_counters *cnts)
Kamal Heib7c16f472017-01-18 15:25:09 +02003992{
3993 u32 num_counters;
3994
3995 num_counters = ARRAY_SIZE(basic_q_cnts);
3996
3997 if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt))
3998 num_counters += ARRAY_SIZE(out_of_seq_q_cnts);
3999
4000 if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters))
4001 num_counters += ARRAY_SIZE(retrans_q_cnts);
Parav Pandit58dcb602017-06-19 07:19:37 +03004002
4003 if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters))
4004 num_counters += ARRAY_SIZE(extended_err_cnts);
4005
Parav Pandite1f24a72017-04-16 07:29:29 +03004006 cnts->num_q_counters = num_counters;
Kamal Heib7c16f472017-01-18 15:25:09 +02004007
Parav Pandite1f24a72017-04-16 07:29:29 +03004008 if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
4009 cnts->num_cong_counters = ARRAY_SIZE(cong_cnts);
4010 num_counters += ARRAY_SIZE(cong_cnts);
4011 }
4012
4013 cnts->names = kcalloc(num_counters, sizeof(cnts->names), GFP_KERNEL);
4014 if (!cnts->names)
Kamal Heib7c16f472017-01-18 15:25:09 +02004015 return -ENOMEM;
4016
Parav Pandite1f24a72017-04-16 07:29:29 +03004017 cnts->offsets = kcalloc(num_counters,
4018 sizeof(cnts->offsets), GFP_KERNEL);
4019 if (!cnts->offsets)
Kamal Heib7c16f472017-01-18 15:25:09 +02004020 goto err_names;
4021
Kamal Heib7c16f472017-01-18 15:25:09 +02004022 return 0;
4023
4024err_names:
Parav Pandite1f24a72017-04-16 07:29:29 +03004025 kfree(cnts->names);
Daniel Jurgensaac44922018-01-04 17:25:40 +02004026 cnts->names = NULL;
Kamal Heib7c16f472017-01-18 15:25:09 +02004027 return -ENOMEM;
4028}
4029
Parav Pandite1f24a72017-04-16 07:29:29 +03004030static void mlx5_ib_fill_counters(struct mlx5_ib_dev *dev,
4031 const char **names,
4032 size_t *offsets)
Kamal Heib7c16f472017-01-18 15:25:09 +02004033{
4034 int i;
4035 int j = 0;
4036
4037 for (i = 0; i < ARRAY_SIZE(basic_q_cnts); i++, j++) {
4038 names[j] = basic_q_cnts[i].name;
4039 offsets[j] = basic_q_cnts[i].offset;
4040 }
4041
4042 if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt)) {
4043 for (i = 0; i < ARRAY_SIZE(out_of_seq_q_cnts); i++, j++) {
4044 names[j] = out_of_seq_q_cnts[i].name;
4045 offsets[j] = out_of_seq_q_cnts[i].offset;
4046 }
4047 }
4048
4049 if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters)) {
4050 for (i = 0; i < ARRAY_SIZE(retrans_q_cnts); i++, j++) {
4051 names[j] = retrans_q_cnts[i].name;
4052 offsets[j] = retrans_q_cnts[i].offset;
4053 }
4054 }
Parav Pandite1f24a72017-04-16 07:29:29 +03004055
Parav Pandit58dcb602017-06-19 07:19:37 +03004056 if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters)) {
4057 for (i = 0; i < ARRAY_SIZE(extended_err_cnts); i++, j++) {
4058 names[j] = extended_err_cnts[i].name;
4059 offsets[j] = extended_err_cnts[i].offset;
4060 }
4061 }
4062
Parav Pandite1f24a72017-04-16 07:29:29 +03004063 if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
4064 for (i = 0; i < ARRAY_SIZE(cong_cnts); i++, j++) {
4065 names[j] = cong_cnts[i].name;
4066 offsets[j] = cong_cnts[i].offset;
4067 }
4068 }
Mark Bloch0837e862016-06-17 15:10:55 +03004069}
4070
Parav Pandite1f24a72017-04-16 07:29:29 +03004071static int mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev)
Mark Bloch0837e862016-06-17 15:10:55 +03004072{
Daniel Jurgensaac44922018-01-04 17:25:40 +02004073 int err = 0;
Mark Bloch0837e862016-06-17 15:10:55 +03004074 int i;
Mark Bloch0837e862016-06-17 15:10:55 +03004075
4076 for (i = 0; i < dev->num_ports; i++) {
Daniel Jurgensaac44922018-01-04 17:25:40 +02004077 err = __mlx5_ib_alloc_counters(dev, &dev->port[i].cnts);
4078 if (err)
4079 goto err_alloc;
Kamal Heib7c16f472017-01-18 15:25:09 +02004080
Daniel Jurgensaac44922018-01-04 17:25:40 +02004081 mlx5_ib_fill_counters(dev, dev->port[i].cnts.names,
4082 dev->port[i].cnts.offsets);
4083
4084 err = mlx5_core_alloc_q_counter(dev->mdev,
4085 &dev->port[i].cnts.set_id);
4086 if (err) {
Mark Bloch0837e862016-06-17 15:10:55 +03004087 mlx5_ib_warn(dev,
4088 "couldn't allocate queue counter for port %d, err %d\n",
Daniel Jurgensaac44922018-01-04 17:25:40 +02004089 i + 1, err);
4090 goto err_alloc;
Mark Bloch0837e862016-06-17 15:10:55 +03004091 }
Daniel Jurgensaac44922018-01-04 17:25:40 +02004092 dev->port[i].cnts.set_id_valid = true;
Mark Bloch0837e862016-06-17 15:10:55 +03004093 }
4094
4095 return 0;
4096
Daniel Jurgensaac44922018-01-04 17:25:40 +02004097err_alloc:
4098 mlx5_ib_dealloc_counters(dev);
4099 return err;
Mark Bloch0837e862016-06-17 15:10:55 +03004100}
4101
Mark Bloch0ad17a82016-06-17 15:10:56 +03004102static struct rdma_hw_stats *mlx5_ib_alloc_hw_stats(struct ib_device *ibdev,
4103 u8 port_num)
4104{
Kamal Heib7c16f472017-01-18 15:25:09 +02004105 struct mlx5_ib_dev *dev = to_mdev(ibdev);
4106 struct mlx5_ib_port *port = &dev->port[port_num - 1];
Mark Bloch0ad17a82016-06-17 15:10:56 +03004107
4108 /* We support only per port stats */
4109 if (port_num == 0)
4110 return NULL;
4111
Parav Pandite1f24a72017-04-16 07:29:29 +03004112 return rdma_alloc_hw_stats_struct(port->cnts.names,
4113 port->cnts.num_q_counters +
4114 port->cnts.num_cong_counters,
Mark Bloch0ad17a82016-06-17 15:10:56 +03004115 RDMA_HW_STATS_DEFAULT_LIFESPAN);
4116}
4117
Daniel Jurgensaac44922018-01-04 17:25:40 +02004118static int mlx5_ib_query_q_counters(struct mlx5_core_dev *mdev,
Parav Pandite1f24a72017-04-16 07:29:29 +03004119 struct mlx5_ib_port *port,
4120 struct rdma_hw_stats *stats)
4121{
4122 int outlen = MLX5_ST_SZ_BYTES(query_q_counter_out);
4123 void *out;
4124 __be32 val;
4125 int ret, i;
4126
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03004127 out = kvzalloc(outlen, GFP_KERNEL);
Parav Pandite1f24a72017-04-16 07:29:29 +03004128 if (!out)
4129 return -ENOMEM;
4130
Daniel Jurgensaac44922018-01-04 17:25:40 +02004131 ret = mlx5_core_query_q_counter(mdev,
Parav Pandite1f24a72017-04-16 07:29:29 +03004132 port->cnts.set_id, 0,
4133 out, outlen);
4134 if (ret)
4135 goto free;
4136
4137 for (i = 0; i < port->cnts.num_q_counters; i++) {
4138 val = *(__be32 *)(out + port->cnts.offsets[i]);
4139 stats->value[i] = (u64)be32_to_cpu(val);
4140 }
4141
4142free:
4143 kvfree(out);
4144 return ret;
4145}
4146
Mark Bloch0ad17a82016-06-17 15:10:56 +03004147static int mlx5_ib_get_hw_stats(struct ib_device *ibdev,
4148 struct rdma_hw_stats *stats,
Kamal Heib7c16f472017-01-18 15:25:09 +02004149 u8 port_num, int index)
Mark Bloch0ad17a82016-06-17 15:10:56 +03004150{
4151 struct mlx5_ib_dev *dev = to_mdev(ibdev);
Kamal Heib7c16f472017-01-18 15:25:09 +02004152 struct mlx5_ib_port *port = &dev->port[port_num - 1];
Daniel Jurgensaac44922018-01-04 17:25:40 +02004153 struct mlx5_core_dev *mdev;
Parav Pandite1f24a72017-04-16 07:29:29 +03004154 int ret, num_counters;
Daniel Jurgensaac44922018-01-04 17:25:40 +02004155 u8 mdev_port_num;
Mark Bloch0ad17a82016-06-17 15:10:56 +03004156
Kamal Heib7c16f472017-01-18 15:25:09 +02004157 if (!stats)
Parav Pandite1f24a72017-04-16 07:29:29 +03004158 return -EINVAL;
Mark Bloch0ad17a82016-06-17 15:10:56 +03004159
Daniel Jurgensaac44922018-01-04 17:25:40 +02004160 num_counters = port->cnts.num_q_counters + port->cnts.num_cong_counters;
4161
4162 /* q_counters are per IB device, query the master mdev */
4163 ret = mlx5_ib_query_q_counters(dev->mdev, port, stats);
Mark Bloch0ad17a82016-06-17 15:10:56 +03004164 if (ret)
Parav Pandite1f24a72017-04-16 07:29:29 +03004165 return ret;
Mark Bloch0ad17a82016-06-17 15:10:56 +03004166
Parav Pandite1f24a72017-04-16 07:29:29 +03004167 if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
Daniel Jurgensaac44922018-01-04 17:25:40 +02004168 mdev = mlx5_ib_get_native_port_mdev(dev, port_num,
4169 &mdev_port_num);
4170 if (!mdev) {
4171 /* If port is not affiliated yet, its in down state
4172 * which doesn't have any counters yet, so it would be
4173 * zero. So no need to read from the HCA.
4174 */
4175 goto done;
4176 }
Majd Dibbiny71a0ff62017-12-21 17:38:26 +02004177 ret = mlx5_lag_query_cong_counters(dev->mdev,
4178 stats->value +
4179 port->cnts.num_q_counters,
4180 port->cnts.num_cong_counters,
4181 port->cnts.offsets +
4182 port->cnts.num_q_counters);
Daniel Jurgensaac44922018-01-04 17:25:40 +02004183
4184 mlx5_ib_put_native_port_mdev(dev, port_num);
Parav Pandite1f24a72017-04-16 07:29:29 +03004185 if (ret)
4186 return ret;
Mark Bloch0ad17a82016-06-17 15:10:56 +03004187 }
Kamal Heib7c16f472017-01-18 15:25:09 +02004188
Daniel Jurgensaac44922018-01-04 17:25:40 +02004189done:
Parav Pandite1f24a72017-04-16 07:29:29 +03004190 return num_counters;
Mark Bloch0ad17a82016-06-17 15:10:56 +03004191}
4192
Niranjana Vishwanathapura8e959602017-06-30 13:14:46 -07004193static void mlx5_ib_free_rdma_netdev(struct net_device *netdev)
4194{
4195 return mlx5_rdma_netdev_free(netdev);
4196}
4197
Erez Shitrit693dfd52017-04-27 17:01:34 +03004198static struct net_device*
4199mlx5_ib_alloc_rdma_netdev(struct ib_device *hca,
4200 u8 port_num,
4201 enum rdma_netdev_t type,
4202 const char *name,
4203 unsigned char name_assign_type,
4204 void (*setup)(struct net_device *))
4205{
Niranjana Vishwanathapura8e959602017-06-30 13:14:46 -07004206 struct net_device *netdev;
4207 struct rdma_netdev *rn;
4208
Erez Shitrit693dfd52017-04-27 17:01:34 +03004209 if (type != RDMA_NETDEV_IPOIB)
4210 return ERR_PTR(-EOPNOTSUPP);
4211
Niranjana Vishwanathapura8e959602017-06-30 13:14:46 -07004212 netdev = mlx5_rdma_netdev_alloc(to_mdev(hca)->mdev, hca,
4213 name, setup);
4214 if (likely(!IS_ERR_OR_NULL(netdev))) {
4215 rn = netdev_priv(netdev);
4216 rn->free_rdma_netdev = mlx5_ib_free_rdma_netdev;
4217 }
4218 return netdev;
Erez Shitrit693dfd52017-04-27 17:01:34 +03004219}
4220
Maor Gottliebfe248c32017-05-30 10:29:14 +03004221static void delay_drop_debugfs_cleanup(struct mlx5_ib_dev *dev)
4222{
4223 if (!dev->delay_drop.dbg)
4224 return;
4225 debugfs_remove_recursive(dev->delay_drop.dbg->dir_debugfs);
4226 kfree(dev->delay_drop.dbg);
4227 dev->delay_drop.dbg = NULL;
4228}
4229
Maor Gottlieb03404e82017-05-30 10:29:13 +03004230static void cancel_delay_drop(struct mlx5_ib_dev *dev)
4231{
4232 if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
4233 return;
4234
4235 cancel_work_sync(&dev->delay_drop.delay_drop_work);
Maor Gottliebfe248c32017-05-30 10:29:14 +03004236 delay_drop_debugfs_cleanup(dev);
4237}
4238
4239static ssize_t delay_drop_timeout_read(struct file *filp, char __user *buf,
4240 size_t count, loff_t *pos)
4241{
4242 struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
4243 char lbuf[20];
4244 int len;
4245
4246 len = snprintf(lbuf, sizeof(lbuf), "%u\n", delay_drop->timeout);
4247 return simple_read_from_buffer(buf, count, pos, lbuf, len);
4248}
4249
4250static ssize_t delay_drop_timeout_write(struct file *filp, const char __user *buf,
4251 size_t count, loff_t *pos)
4252{
4253 struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
4254 u32 timeout;
4255 u32 var;
4256
4257 if (kstrtouint_from_user(buf, count, 0, &var))
4258 return -EFAULT;
4259
4260 timeout = min_t(u32, roundup(var, 100), MLX5_MAX_DELAY_DROP_TIMEOUT_MS *
4261 1000);
4262 if (timeout != var)
4263 mlx5_ib_dbg(delay_drop->dev, "Round delay drop timeout to %u usec\n",
4264 timeout);
4265
4266 delay_drop->timeout = timeout;
4267
4268 return count;
4269}
4270
4271static const struct file_operations fops_delay_drop_timeout = {
4272 .owner = THIS_MODULE,
4273 .open = simple_open,
4274 .write = delay_drop_timeout_write,
4275 .read = delay_drop_timeout_read,
4276};
4277
4278static int delay_drop_debugfs_init(struct mlx5_ib_dev *dev)
4279{
4280 struct mlx5_ib_dbg_delay_drop *dbg;
4281
4282 if (!mlx5_debugfs_root)
4283 return 0;
4284
4285 dbg = kzalloc(sizeof(*dbg), GFP_KERNEL);
4286 if (!dbg)
4287 return -ENOMEM;
4288
Sudip Mukherjeecbafad82017-09-18 12:28:48 +01004289 dev->delay_drop.dbg = dbg;
4290
Maor Gottliebfe248c32017-05-30 10:29:14 +03004291 dbg->dir_debugfs =
4292 debugfs_create_dir("delay_drop",
4293 dev->mdev->priv.dbg_root);
4294 if (!dbg->dir_debugfs)
Sudip Mukherjeecbafad82017-09-18 12:28:48 +01004295 goto out_debugfs;
Maor Gottliebfe248c32017-05-30 10:29:14 +03004296
4297 dbg->events_cnt_debugfs =
4298 debugfs_create_atomic_t("num_timeout_events", 0400,
4299 dbg->dir_debugfs,
4300 &dev->delay_drop.events_cnt);
4301 if (!dbg->events_cnt_debugfs)
4302 goto out_debugfs;
4303
4304 dbg->rqs_cnt_debugfs =
4305 debugfs_create_atomic_t("num_rqs", 0400,
4306 dbg->dir_debugfs,
4307 &dev->delay_drop.rqs_cnt);
4308 if (!dbg->rqs_cnt_debugfs)
4309 goto out_debugfs;
4310
4311 dbg->timeout_debugfs =
4312 debugfs_create_file("timeout", 0600,
4313 dbg->dir_debugfs,
4314 &dev->delay_drop,
4315 &fops_delay_drop_timeout);
4316 if (!dbg->timeout_debugfs)
4317 goto out_debugfs;
4318
4319 return 0;
4320
4321out_debugfs:
4322 delay_drop_debugfs_cleanup(dev);
4323 return -ENOMEM;
Maor Gottlieb03404e82017-05-30 10:29:13 +03004324}
4325
4326static void init_delay_drop(struct mlx5_ib_dev *dev)
4327{
4328 if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
4329 return;
4330
4331 mutex_init(&dev->delay_drop.lock);
4332 dev->delay_drop.dev = dev;
4333 dev->delay_drop.activate = false;
4334 dev->delay_drop.timeout = MLX5_MAX_DELAY_DROP_TIMEOUT_MS * 1000;
4335 INIT_WORK(&dev->delay_drop.delay_drop_work, delay_drop_handler);
Maor Gottliebfe248c32017-05-30 10:29:14 +03004336 atomic_set(&dev->delay_drop.rqs_cnt, 0);
4337 atomic_set(&dev->delay_drop.events_cnt, 0);
4338
4339 if (delay_drop_debugfs_init(dev))
4340 mlx5_ib_warn(dev, "Failed to init delay drop debugfs\n");
Maor Gottlieb03404e82017-05-30 10:29:13 +03004341}
4342
Leon Romanovsky84305d712017-08-17 15:50:53 +03004343static const struct cpumask *
4344mlx5_ib_get_vector_affinity(struct ib_device *ibdev, int comp_vector)
Sagi Grimberg40b24402017-07-13 11:09:42 +03004345{
4346 struct mlx5_ib_dev *dev = to_mdev(ibdev);
4347
4348 return mlx5_get_vector_affinity(dev->mdev, comp_vector);
4349}
4350
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004351/* The mlx5_ib_multiport_mutex should be held when calling this function */
4352static void mlx5_ib_unbind_slave_port(struct mlx5_ib_dev *ibdev,
4353 struct mlx5_ib_multiport_info *mpi)
4354{
4355 u8 port_num = mlx5_core_native_port_num(mpi->mdev) - 1;
4356 struct mlx5_ib_port *port = &ibdev->port[port_num];
4357 int comps;
4358 int err;
4359 int i;
4360
Parav Pandita9e546e2018-01-04 17:25:39 +02004361 mlx5_ib_cleanup_cong_debugfs(ibdev, port_num);
4362
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004363 spin_lock(&port->mp.mpi_lock);
4364 if (!mpi->ibdev) {
4365 spin_unlock(&port->mp.mpi_lock);
4366 return;
4367 }
4368 mpi->ibdev = NULL;
4369
4370 spin_unlock(&port->mp.mpi_lock);
4371 mlx5_remove_netdev_notifier(ibdev, port_num);
4372 spin_lock(&port->mp.mpi_lock);
4373
4374 comps = mpi->mdev_refcnt;
4375 if (comps) {
4376 mpi->unaffiliate = true;
4377 init_completion(&mpi->unref_comp);
4378 spin_unlock(&port->mp.mpi_lock);
4379
4380 for (i = 0; i < comps; i++)
4381 wait_for_completion(&mpi->unref_comp);
4382
4383 spin_lock(&port->mp.mpi_lock);
4384 mpi->unaffiliate = false;
4385 }
4386
4387 port->mp.mpi = NULL;
4388
4389 list_add_tail(&mpi->list, &mlx5_ib_unaffiliated_port_list);
4390
4391 spin_unlock(&port->mp.mpi_lock);
4392
4393 err = mlx5_nic_vport_unaffiliate_multiport(mpi->mdev);
4394
4395 mlx5_ib_dbg(ibdev, "unaffiliated port %d\n", port_num + 1);
4396 /* Log an error, still needed to cleanup the pointers and add
4397 * it back to the list.
4398 */
4399 if (err)
4400 mlx5_ib_err(ibdev, "Failed to unaffiliate port %u\n",
4401 port_num + 1);
4402
4403 ibdev->roce[port_num].last_port_state = IB_PORT_DOWN;
4404}
4405
4406/* The mlx5_ib_multiport_mutex should be held when calling this function */
4407static bool mlx5_ib_bind_slave_port(struct mlx5_ib_dev *ibdev,
4408 struct mlx5_ib_multiport_info *mpi)
4409{
4410 u8 port_num = mlx5_core_native_port_num(mpi->mdev) - 1;
4411 int err;
4412
4413 spin_lock(&ibdev->port[port_num].mp.mpi_lock);
4414 if (ibdev->port[port_num].mp.mpi) {
4415 mlx5_ib_warn(ibdev, "port %d already affiliated.\n",
4416 port_num + 1);
4417 spin_unlock(&ibdev->port[port_num].mp.mpi_lock);
4418 return false;
4419 }
4420
4421 ibdev->port[port_num].mp.mpi = mpi;
4422 mpi->ibdev = ibdev;
4423 spin_unlock(&ibdev->port[port_num].mp.mpi_lock);
4424
4425 err = mlx5_nic_vport_affiliate_multiport(ibdev->mdev, mpi->mdev);
4426 if (err)
4427 goto unbind;
4428
4429 err = get_port_caps(ibdev, mlx5_core_native_port_num(mpi->mdev));
4430 if (err)
4431 goto unbind;
4432
4433 err = mlx5_add_netdev_notifier(ibdev, port_num);
4434 if (err) {
4435 mlx5_ib_err(ibdev, "failed adding netdev notifier for port %u\n",
4436 port_num + 1);
4437 goto unbind;
4438 }
4439
Parav Pandita9e546e2018-01-04 17:25:39 +02004440 err = mlx5_ib_init_cong_debugfs(ibdev, port_num);
4441 if (err)
4442 goto unbind;
4443
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004444 return true;
4445
4446unbind:
4447 mlx5_ib_unbind_slave_port(ibdev, mpi);
4448 return false;
4449}
4450
4451static int mlx5_ib_init_multiport_master(struct mlx5_ib_dev *dev)
4452{
4453 int port_num = mlx5_core_native_port_num(dev->mdev) - 1;
4454 enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev,
4455 port_num + 1);
4456 struct mlx5_ib_multiport_info *mpi;
4457 int err;
4458 int i;
4459
4460 if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
4461 return 0;
4462
4463 err = mlx5_query_nic_vport_system_image_guid(dev->mdev,
4464 &dev->sys_image_guid);
4465 if (err)
4466 return err;
4467
4468 err = mlx5_nic_vport_enable_roce(dev->mdev);
4469 if (err)
4470 return err;
4471
4472 mutex_lock(&mlx5_ib_multiport_mutex);
4473 for (i = 0; i < dev->num_ports; i++) {
4474 bool bound = false;
4475
4476 /* build a stub multiport info struct for the native port. */
4477 if (i == port_num) {
4478 mpi = kzalloc(sizeof(*mpi), GFP_KERNEL);
4479 if (!mpi) {
4480 mutex_unlock(&mlx5_ib_multiport_mutex);
4481 mlx5_nic_vport_disable_roce(dev->mdev);
4482 return -ENOMEM;
4483 }
4484
4485 mpi->is_master = true;
4486 mpi->mdev = dev->mdev;
4487 mpi->sys_image_guid = dev->sys_image_guid;
4488 dev->port[i].mp.mpi = mpi;
4489 mpi->ibdev = dev;
4490 mpi = NULL;
4491 continue;
4492 }
4493
4494 list_for_each_entry(mpi, &mlx5_ib_unaffiliated_port_list,
4495 list) {
4496 if (dev->sys_image_guid == mpi->sys_image_guid &&
4497 (mlx5_core_native_port_num(mpi->mdev) - 1) == i) {
4498 bound = mlx5_ib_bind_slave_port(dev, mpi);
4499 }
4500
4501 if (bound) {
4502 dev_dbg(&mpi->mdev->pdev->dev, "removing port from unaffiliated list.\n");
4503 mlx5_ib_dbg(dev, "port %d bound\n", i + 1);
4504 list_del(&mpi->list);
4505 break;
4506 }
4507 }
4508 if (!bound) {
4509 get_port_caps(dev, i + 1);
4510 mlx5_ib_dbg(dev, "no free port found for port %d\n",
4511 i + 1);
4512 }
4513 }
4514
4515 list_add_tail(&dev->ib_dev_list, &mlx5_ib_dev_list);
4516 mutex_unlock(&mlx5_ib_multiport_mutex);
4517 return err;
4518}
4519
4520static void mlx5_ib_cleanup_multiport_master(struct mlx5_ib_dev *dev)
4521{
4522 int port_num = mlx5_core_native_port_num(dev->mdev) - 1;
4523 enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev,
4524 port_num + 1);
4525 int i;
4526
4527 if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
4528 return;
4529
4530 mutex_lock(&mlx5_ib_multiport_mutex);
4531 for (i = 0; i < dev->num_ports; i++) {
4532 if (dev->port[i].mp.mpi) {
4533 /* Destroy the native port stub */
4534 if (i == port_num) {
4535 kfree(dev->port[i].mp.mpi);
4536 dev->port[i].mp.mpi = NULL;
4537 } else {
4538 mlx5_ib_dbg(dev, "unbinding port_num: %d\n", i + 1);
4539 mlx5_ib_unbind_slave_port(dev, dev->port[i].mp.mpi);
4540 }
4541 }
4542 }
4543
4544 mlx5_ib_dbg(dev, "removing from devlist\n");
4545 list_del(&dev->ib_dev_list);
4546 mutex_unlock(&mlx5_ib_multiport_mutex);
4547
4548 mlx5_nic_vport_disable_roce(dev->mdev);
4549}
4550
Matan Barak8c846602018-03-28 09:27:41 +03004551#define NUM_TREES 0
4552static int populate_specs_root(struct mlx5_ib_dev *dev)
4553{
4554 const struct uverbs_object_tree_def *default_root[NUM_TREES + 1] = {
4555 uverbs_default_get_objects()};
4556 size_t num_trees = 1;
4557
4558 dev->ib_dev.specs_root =
4559 uverbs_alloc_spec_tree(num_trees, default_root);
4560
4561 return PTR_ERR_OR_ZERO(dev->ib_dev.specs_root);
4562}
4563
4564static void depopulate_specs_root(struct mlx5_ib_dev *dev)
4565{
4566 uverbs_free_spec_tree(dev->ib_dev.specs_root);
4567}
4568
Mark Blochb5ca15a2018-01-23 11:16:30 +00004569void mlx5_ib_stage_init_cleanup(struct mlx5_ib_dev *dev)
Eli Cohene126ba92013-07-07 17:25:49 +03004570{
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004571 mlx5_ib_cleanup_multiport_master(dev);
Mark Bloch3cc297d2018-01-01 13:07:03 +02004572#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
4573 cleanup_srcu_struct(&dev->mr_srcu);
4574#endif
Mark Bloch16c19752018-01-01 13:06:58 +02004575 kfree(dev->port);
4576}
4577
Mark Blochb5ca15a2018-01-23 11:16:30 +00004578int mlx5_ib_stage_init_init(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02004579{
4580 struct mlx5_core_dev *mdev = dev->mdev;
Aviv Heller4babcf92016-09-18 20:48:03 +03004581 const char *name;
Eli Cohene126ba92013-07-07 17:25:49 +03004582 int err;
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004583 int i;
Eli Cohene126ba92013-07-07 17:25:49 +03004584
Daniel Jurgens508562d2018-01-04 17:25:34 +02004585 dev->port = kcalloc(dev->num_ports, sizeof(*dev->port),
Mark Bloch0837e862016-06-17 15:10:55 +03004586 GFP_KERNEL);
4587 if (!dev->port)
Mark Bloch16c19752018-01-01 13:06:58 +02004588 return -ENOMEM;
Mark Bloch0837e862016-06-17 15:10:55 +03004589
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004590 for (i = 0; i < dev->num_ports; i++) {
4591 spin_lock_init(&dev->port[i].mp.mpi_lock);
4592 rwlock_init(&dev->roce[i].netdev_lock);
4593 }
4594
4595 err = mlx5_ib_init_multiport_master(dev);
Eli Cohene126ba92013-07-07 17:25:49 +03004596 if (err)
Mark Bloch0837e862016-06-17 15:10:55 +03004597 goto err_free_port;
Eli Cohene126ba92013-07-07 17:25:49 +03004598
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004599 if (!mlx5_core_mp_enabled(mdev)) {
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004600 for (i = 1; i <= dev->num_ports; i++) {
4601 err = get_port_caps(dev, i);
4602 if (err)
4603 break;
4604 }
4605 } else {
4606 err = get_port_caps(dev, mlx5_core_native_port_num(mdev));
4607 }
4608 if (err)
4609 goto err_mp;
4610
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03004611 if (mlx5_use_mad_ifc(dev))
4612 get_ext_port_caps(dev);
Eli Cohene126ba92013-07-07 17:25:49 +03004613
Aviv Heller4babcf92016-09-18 20:48:03 +03004614 if (!mlx5_lag_is_active(mdev))
4615 name = "mlx5_%d";
4616 else
4617 name = "mlx5_bond_%d";
4618
4619 strlcpy(dev->ib_dev.name, name, IB_DEVICE_NAME_MAX);
Eli Cohene126ba92013-07-07 17:25:49 +03004620 dev->ib_dev.owner = THIS_MODULE;
4621 dev->ib_dev.node_type = RDMA_NODE_IB_CA;
Sagi Grimbergc6790aa2015-09-24 10:34:23 +03004622 dev->ib_dev.local_dma_lkey = 0 /* not supported for now */;
Daniel Jurgens508562d2018-01-04 17:25:34 +02004623 dev->ib_dev.phys_port_cnt = dev->num_ports;
Saeed Mahameed233d05d2015-04-02 17:07:32 +03004624 dev->ib_dev.num_comp_vectors =
4625 dev->mdev->priv.eq_table.num_comp_vectors;
Bart Van Assche9b0c2892017-01-20 13:04:21 -08004626 dev->ib_dev.dev.parent = &mdev->pdev->dev;
Eli Cohene126ba92013-07-07 17:25:49 +03004627
Mark Bloch3cc297d2018-01-01 13:07:03 +02004628 mutex_init(&dev->cap_mask_mutex);
4629 INIT_LIST_HEAD(&dev->qp_list);
4630 spin_lock_init(&dev->reset_flow_resource_lock);
4631
4632#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
4633 err = init_srcu_struct(&dev->mr_srcu);
4634 if (err)
4635 goto err_free_port;
4636#endif
4637
Mark Bloch16c19752018-01-01 13:06:58 +02004638 return 0;
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004639err_mp:
4640 mlx5_ib_cleanup_multiport_master(dev);
Mark Bloch16c19752018-01-01 13:06:58 +02004641
4642err_free_port:
4643 kfree(dev->port);
4644
4645 return -ENOMEM;
4646}
4647
Mark Bloch9a4ca382018-01-16 14:42:35 +00004648static int mlx5_ib_stage_flow_db_init(struct mlx5_ib_dev *dev)
4649{
4650 dev->flow_db = kzalloc(sizeof(*dev->flow_db), GFP_KERNEL);
4651
4652 if (!dev->flow_db)
4653 return -ENOMEM;
4654
4655 mutex_init(&dev->flow_db->lock);
4656
4657 return 0;
4658}
4659
Mark Blochb5ca15a2018-01-23 11:16:30 +00004660int mlx5_ib_stage_rep_flow_db_init(struct mlx5_ib_dev *dev)
4661{
4662 struct mlx5_ib_dev *nic_dev;
4663
4664 nic_dev = mlx5_ib_get_uplink_ibdev(dev->mdev->priv.eswitch);
4665
4666 if (!nic_dev)
4667 return -EINVAL;
4668
4669 dev->flow_db = nic_dev->flow_db;
4670
4671 return 0;
4672}
4673
Mark Bloch9a4ca382018-01-16 14:42:35 +00004674static void mlx5_ib_stage_flow_db_cleanup(struct mlx5_ib_dev *dev)
4675{
4676 kfree(dev->flow_db);
4677}
4678
Mark Blochb5ca15a2018-01-23 11:16:30 +00004679int mlx5_ib_stage_caps_init(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02004680{
4681 struct mlx5_core_dev *mdev = dev->mdev;
Mark Bloch16c19752018-01-01 13:06:58 +02004682 int err;
4683
Eli Cohene126ba92013-07-07 17:25:49 +03004684 dev->ib_dev.uverbs_abi_ver = MLX5_IB_UVERBS_ABI_VERSION;
4685 dev->ib_dev.uverbs_cmd_mask =
4686 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
4687 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
4688 (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
4689 (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
4690 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
Moni Shoua41c450f2016-11-23 08:23:26 +02004691 (1ull << IB_USER_VERBS_CMD_CREATE_AH) |
4692 (1ull << IB_USER_VERBS_CMD_DESTROY_AH) |
Eli Cohene126ba92013-07-07 17:25:49 +03004693 (1ull << IB_USER_VERBS_CMD_REG_MR) |
Noa Osherovich56e11d62016-02-29 16:46:51 +02004694 (1ull << IB_USER_VERBS_CMD_REREG_MR) |
Eli Cohene126ba92013-07-07 17:25:49 +03004695 (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
4696 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
4697 (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
4698 (1ull << IB_USER_VERBS_CMD_RESIZE_CQ) |
4699 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
4700 (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
4701 (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
4702 (1ull << IB_USER_VERBS_CMD_QUERY_QP) |
4703 (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
4704 (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST) |
4705 (1ull << IB_USER_VERBS_CMD_DETACH_MCAST) |
4706 (1ull << IB_USER_VERBS_CMD_CREATE_SRQ) |
4707 (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ) |
4708 (1ull << IB_USER_VERBS_CMD_QUERY_SRQ) |
4709 (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ) |
4710 (1ull << IB_USER_VERBS_CMD_CREATE_XSRQ) |
4711 (1ull << IB_USER_VERBS_CMD_OPEN_QP);
Haggai Eran1707cb42015-02-08 13:28:52 +02004712 dev->ib_dev.uverbs_ex_cmd_mask =
Matan Barakd4584dd2016-01-28 17:51:46 +02004713 (1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE) |
4714 (1ull << IB_USER_VERBS_EX_CMD_CREATE_CQ) |
Bodong Wang7d29f342016-12-01 13:43:16 +02004715 (1ull << IB_USER_VERBS_EX_CMD_CREATE_QP) |
Yonatan Cohenb0e9df62017-11-13 10:51:15 +02004716 (1ull << IB_USER_VERBS_EX_CMD_MODIFY_QP) |
4717 (1ull << IB_USER_VERBS_EX_CMD_MODIFY_CQ);
Eli Cohene126ba92013-07-07 17:25:49 +03004718
4719 dev->ib_dev.query_device = mlx5_ib_query_device;
Achiad Shochatebd61f62015-12-23 18:47:16 +02004720 dev->ib_dev.get_link_layer = mlx5_ib_port_link_layer;
Eli Cohene126ba92013-07-07 17:25:49 +03004721 dev->ib_dev.query_gid = mlx5_ib_query_gid;
Achiad Shochat3cca2602015-12-23 18:47:23 +02004722 dev->ib_dev.add_gid = mlx5_ib_add_gid;
4723 dev->ib_dev.del_gid = mlx5_ib_del_gid;
Eli Cohene126ba92013-07-07 17:25:49 +03004724 dev->ib_dev.query_pkey = mlx5_ib_query_pkey;
4725 dev->ib_dev.modify_device = mlx5_ib_modify_device;
4726 dev->ib_dev.modify_port = mlx5_ib_modify_port;
4727 dev->ib_dev.alloc_ucontext = mlx5_ib_alloc_ucontext;
4728 dev->ib_dev.dealloc_ucontext = mlx5_ib_dealloc_ucontext;
4729 dev->ib_dev.mmap = mlx5_ib_mmap;
4730 dev->ib_dev.alloc_pd = mlx5_ib_alloc_pd;
4731 dev->ib_dev.dealloc_pd = mlx5_ib_dealloc_pd;
4732 dev->ib_dev.create_ah = mlx5_ib_create_ah;
4733 dev->ib_dev.query_ah = mlx5_ib_query_ah;
4734 dev->ib_dev.destroy_ah = mlx5_ib_destroy_ah;
4735 dev->ib_dev.create_srq = mlx5_ib_create_srq;
4736 dev->ib_dev.modify_srq = mlx5_ib_modify_srq;
4737 dev->ib_dev.query_srq = mlx5_ib_query_srq;
4738 dev->ib_dev.destroy_srq = mlx5_ib_destroy_srq;
4739 dev->ib_dev.post_srq_recv = mlx5_ib_post_srq_recv;
4740 dev->ib_dev.create_qp = mlx5_ib_create_qp;
4741 dev->ib_dev.modify_qp = mlx5_ib_modify_qp;
4742 dev->ib_dev.query_qp = mlx5_ib_query_qp;
4743 dev->ib_dev.destroy_qp = mlx5_ib_destroy_qp;
4744 dev->ib_dev.post_send = mlx5_ib_post_send;
4745 dev->ib_dev.post_recv = mlx5_ib_post_recv;
4746 dev->ib_dev.create_cq = mlx5_ib_create_cq;
4747 dev->ib_dev.modify_cq = mlx5_ib_modify_cq;
4748 dev->ib_dev.resize_cq = mlx5_ib_resize_cq;
4749 dev->ib_dev.destroy_cq = mlx5_ib_destroy_cq;
4750 dev->ib_dev.poll_cq = mlx5_ib_poll_cq;
4751 dev->ib_dev.req_notify_cq = mlx5_ib_arm_cq;
4752 dev->ib_dev.get_dma_mr = mlx5_ib_get_dma_mr;
4753 dev->ib_dev.reg_user_mr = mlx5_ib_reg_user_mr;
Noa Osherovich56e11d62016-02-29 16:46:51 +02004754 dev->ib_dev.rereg_user_mr = mlx5_ib_rereg_user_mr;
Eli Cohene126ba92013-07-07 17:25:49 +03004755 dev->ib_dev.dereg_mr = mlx5_ib_dereg_mr;
4756 dev->ib_dev.attach_mcast = mlx5_ib_mcg_attach;
4757 dev->ib_dev.detach_mcast = mlx5_ib_mcg_detach;
4758 dev->ib_dev.process_mad = mlx5_ib_process_mad;
Sagi Grimberg9bee1782015-07-30 10:32:35 +03004759 dev->ib_dev.alloc_mr = mlx5_ib_alloc_mr;
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004760 dev->ib_dev.map_mr_sg = mlx5_ib_map_mr_sg;
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02004761 dev->ib_dev.check_mr_status = mlx5_ib_check_mr_status;
Ira Weinyc7342822016-06-15 02:22:01 -04004762 dev->ib_dev.get_dev_fw_str = get_dev_fw_str;
Sagi Grimberg40b24402017-07-13 11:09:42 +03004763 dev->ib_dev.get_vector_affinity = mlx5_ib_get_vector_affinity;
Niranjana Vishwanathapura8e959602017-06-30 13:14:46 -07004764 if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads))
Alex Vesker022d0382017-06-14 09:59:06 +03004765 dev->ib_dev.alloc_rdma_netdev = mlx5_ib_alloc_rdma_netdev;
Niranjana Vishwanathapura8e959602017-06-30 13:14:46 -07004766
Eli Coheneff901d2016-03-11 22:58:42 +02004767 if (mlx5_core_is_pf(mdev)) {
4768 dev->ib_dev.get_vf_config = mlx5_ib_get_vf_config;
4769 dev->ib_dev.set_vf_link_state = mlx5_ib_set_vf_link_state;
4770 dev->ib_dev.get_vf_stats = mlx5_ib_get_vf_stats;
4771 dev->ib_dev.set_vf_guid = mlx5_ib_set_vf_guid;
4772 }
Eli Cohene126ba92013-07-07 17:25:49 +03004773
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03004774 dev->ib_dev.disassociate_ucontext = mlx5_ib_disassociate_ucontext;
4775
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03004776 dev->umr_fence = mlx5_get_umr_fence(MLX5_CAP_GEN(mdev, umr_fence));
4777
Matan Barakd2370e02016-02-29 18:05:30 +02004778 if (MLX5_CAP_GEN(mdev, imaicl)) {
4779 dev->ib_dev.alloc_mw = mlx5_ib_alloc_mw;
4780 dev->ib_dev.dealloc_mw = mlx5_ib_dealloc_mw;
4781 dev->ib_dev.uverbs_cmd_mask |=
4782 (1ull << IB_USER_VERBS_CMD_ALLOC_MW) |
4783 (1ull << IB_USER_VERBS_CMD_DEALLOC_MW);
4784 }
4785
Saeed Mahameed938fe832015-05-28 22:28:41 +03004786 if (MLX5_CAP_GEN(mdev, xrc)) {
Eli Cohene126ba92013-07-07 17:25:49 +03004787 dev->ib_dev.alloc_xrcd = mlx5_ib_alloc_xrcd;
4788 dev->ib_dev.dealloc_xrcd = mlx5_ib_dealloc_xrcd;
4789 dev->ib_dev.uverbs_cmd_mask |=
4790 (1ull << IB_USER_VERBS_CMD_OPEN_XRCD) |
4791 (1ull << IB_USER_VERBS_CMD_CLOSE_XRCD);
4792 }
4793
Yishai Hadas81e30882017-06-08 16:15:09 +03004794 dev->ib_dev.create_flow = mlx5_ib_create_flow;
4795 dev->ib_dev.destroy_flow = mlx5_ib_destroy_flow;
4796 dev->ib_dev.uverbs_ex_cmd_mask |=
4797 (1ull << IB_USER_VERBS_EX_CMD_CREATE_FLOW) |
4798 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_FLOW);
Matan Barak0ede73b2018-03-19 15:02:34 +02004799 dev->ib_dev.driver_id = RDMA_DRIVER_MLX5;
Yishai Hadas81e30882017-06-08 16:15:09 +03004800
Eli Cohene126ba92013-07-07 17:25:49 +03004801 err = init_node_data(dev);
4802 if (err)
Mark Bloch16c19752018-01-01 13:06:58 +02004803 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03004804
Mark Blochc8b89922018-01-01 13:07:02 +02004805 if ((MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_ETH) &&
Jason Gunthorpee7996a92018-01-29 13:26:40 -07004806 (MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) ||
4807 MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
Mark Blochc8b89922018-01-01 13:07:02 +02004808 mutex_init(&dev->lb_mutex);
4809
Mark Bloch16c19752018-01-01 13:06:58 +02004810 return 0;
4811}
4812
Mark Bloch8e6efa32017-11-06 12:22:13 +00004813static int mlx5_ib_stage_non_default_cb(struct mlx5_ib_dev *dev)
4814{
4815 dev->ib_dev.get_port_immutable = mlx5_port_immutable;
4816 dev->ib_dev.query_port = mlx5_ib_query_port;
4817
4818 return 0;
4819}
4820
Mark Blochb5ca15a2018-01-23 11:16:30 +00004821int mlx5_ib_stage_rep_non_default_cb(struct mlx5_ib_dev *dev)
Mark Bloch8e6efa32017-11-06 12:22:13 +00004822{
4823 dev->ib_dev.get_port_immutable = mlx5_port_rep_immutable;
4824 dev->ib_dev.query_port = mlx5_ib_rep_query_port;
4825
4826 return 0;
4827}
4828
4829static int mlx5_ib_stage_common_roce_init(struct mlx5_ib_dev *dev,
4830 u8 port_num)
4831{
4832 int i;
4833
4834 for (i = 0; i < dev->num_ports; i++) {
4835 dev->roce[i].dev = dev;
4836 dev->roce[i].native_port_num = i + 1;
4837 dev->roce[i].last_port_state = IB_PORT_DOWN;
4838 }
4839
4840 dev->ib_dev.get_netdev = mlx5_ib_get_netdev;
4841 dev->ib_dev.create_wq = mlx5_ib_create_wq;
4842 dev->ib_dev.modify_wq = mlx5_ib_modify_wq;
4843 dev->ib_dev.destroy_wq = mlx5_ib_destroy_wq;
4844 dev->ib_dev.create_rwq_ind_table = mlx5_ib_create_rwq_ind_table;
4845 dev->ib_dev.destroy_rwq_ind_table = mlx5_ib_destroy_rwq_ind_table;
4846
4847 dev->ib_dev.uverbs_ex_cmd_mask |=
4848 (1ull << IB_USER_VERBS_EX_CMD_CREATE_WQ) |
4849 (1ull << IB_USER_VERBS_EX_CMD_MODIFY_WQ) |
4850 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_WQ) |
4851 (1ull << IB_USER_VERBS_EX_CMD_CREATE_RWQ_IND_TBL) |
4852 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_RWQ_IND_TBL);
4853
4854 return mlx5_add_netdev_notifier(dev, port_num);
4855}
4856
4857static void mlx5_ib_stage_common_roce_cleanup(struct mlx5_ib_dev *dev)
4858{
4859 u8 port_num = mlx5_core_native_port_num(dev->mdev) - 1;
4860
4861 mlx5_remove_netdev_notifier(dev, port_num);
4862}
4863
4864int mlx5_ib_stage_rep_roce_init(struct mlx5_ib_dev *dev)
4865{
4866 struct mlx5_core_dev *mdev = dev->mdev;
4867 enum rdma_link_layer ll;
4868 int port_type_cap;
4869 int err = 0;
4870 u8 port_num;
4871
4872 port_num = mlx5_core_native_port_num(dev->mdev) - 1;
4873 port_type_cap = MLX5_CAP_GEN(mdev, port_type);
4874 ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
4875
4876 if (ll == IB_LINK_LAYER_ETHERNET)
4877 err = mlx5_ib_stage_common_roce_init(dev, port_num);
4878
4879 return err;
4880}
4881
4882void mlx5_ib_stage_rep_roce_cleanup(struct mlx5_ib_dev *dev)
4883{
4884 mlx5_ib_stage_common_roce_cleanup(dev);
4885}
4886
Mark Bloch16c19752018-01-01 13:06:58 +02004887static int mlx5_ib_stage_roce_init(struct mlx5_ib_dev *dev)
4888{
4889 struct mlx5_core_dev *mdev = dev->mdev;
4890 enum rdma_link_layer ll;
4891 int port_type_cap;
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004892 u8 port_num;
Mark Bloch16c19752018-01-01 13:06:58 +02004893 int err;
4894
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004895 port_num = mlx5_core_native_port_num(dev->mdev) - 1;
Mark Bloch16c19752018-01-01 13:06:58 +02004896 port_type_cap = MLX5_CAP_GEN(mdev, port_type);
4897 ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
4898
Achiad Shochatfc24fc52015-12-23 18:47:17 +02004899 if (ll == IB_LINK_LAYER_ETHERNET) {
Mark Bloch8e6efa32017-11-06 12:22:13 +00004900 err = mlx5_ib_stage_common_roce_init(dev, port_num);
Achiad Shochatfc24fc52015-12-23 18:47:17 +02004901 if (err)
Mark Bloch16c19752018-01-01 13:06:58 +02004902 return err;
Mark Bloch8e6efa32017-11-06 12:22:13 +00004903
4904 err = mlx5_enable_eth(dev, port_num);
4905 if (err)
4906 goto cleanup;
Achiad Shochatfc24fc52015-12-23 18:47:17 +02004907 }
4908
Mark Bloch16c19752018-01-01 13:06:58 +02004909 return 0;
Mark Bloch8e6efa32017-11-06 12:22:13 +00004910cleanup:
4911 mlx5_ib_stage_common_roce_cleanup(dev);
4912
4913 return err;
Mark Bloch16c19752018-01-01 13:06:58 +02004914}
Eli Cohene126ba92013-07-07 17:25:49 +03004915
Mark Bloch16c19752018-01-01 13:06:58 +02004916static void mlx5_ib_stage_roce_cleanup(struct mlx5_ib_dev *dev)
4917{
4918 struct mlx5_core_dev *mdev = dev->mdev;
4919 enum rdma_link_layer ll;
4920 int port_type_cap;
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004921 u8 port_num;
Eli Cohene126ba92013-07-07 17:25:49 +03004922
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004923 port_num = mlx5_core_native_port_num(dev->mdev) - 1;
Mark Bloch16c19752018-01-01 13:06:58 +02004924 port_type_cap = MLX5_CAP_GEN(mdev, port_type);
4925 ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
4926
4927 if (ll == IB_LINK_LAYER_ETHERNET) {
4928 mlx5_disable_eth(dev);
Mark Bloch8e6efa32017-11-06 12:22:13 +00004929 mlx5_ib_stage_common_roce_cleanup(dev);
Kamal Heib45bded22017-01-18 14:10:32 +02004930 }
Mark Bloch16c19752018-01-01 13:06:58 +02004931}
Haggai Eran6aec21f2014-12-11 17:04:23 +02004932
Mark Blochb5ca15a2018-01-23 11:16:30 +00004933int mlx5_ib_stage_dev_res_init(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02004934{
4935 return create_dev_resources(&dev->devr);
4936}
Parav Pandit4a2da0b2017-05-30 10:05:15 +03004937
Mark Blochb5ca15a2018-01-23 11:16:30 +00004938void mlx5_ib_stage_dev_res_cleanup(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02004939{
4940 destroy_dev_resources(&dev->devr);
4941}
4942
4943static int mlx5_ib_stage_odp_init(struct mlx5_ib_dev *dev)
4944{
Mark Bloch07321b32018-01-01 13:07:00 +02004945 mlx5_ib_internal_fill_odp_caps(dev);
4946
Mark Bloch16c19752018-01-01 13:06:58 +02004947 return mlx5_ib_odp_init_one(dev);
4948}
4949
Mark Blochb5ca15a2018-01-23 11:16:30 +00004950int mlx5_ib_stage_counters_init(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02004951{
Mark Bloch5e1e7612018-01-01 13:07:01 +02004952 if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt)) {
4953 dev->ib_dev.get_hw_stats = mlx5_ib_get_hw_stats;
4954 dev->ib_dev.alloc_hw_stats = mlx5_ib_alloc_hw_stats;
4955
4956 return mlx5_ib_alloc_counters(dev);
4957 }
Mark Bloch16c19752018-01-01 13:06:58 +02004958
4959 return 0;
4960}
4961
Mark Blochb5ca15a2018-01-23 11:16:30 +00004962void mlx5_ib_stage_counters_cleanup(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02004963{
4964 if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt))
4965 mlx5_ib_dealloc_counters(dev);
4966}
4967
4968static int mlx5_ib_stage_cong_debugfs_init(struct mlx5_ib_dev *dev)
4969{
Parav Pandita9e546e2018-01-04 17:25:39 +02004970 return mlx5_ib_init_cong_debugfs(dev,
4971 mlx5_core_native_port_num(dev->mdev) - 1);
Mark Bloch16c19752018-01-01 13:06:58 +02004972}
4973
4974static void mlx5_ib_stage_cong_debugfs_cleanup(struct mlx5_ib_dev *dev)
4975{
Parav Pandita9e546e2018-01-04 17:25:39 +02004976 mlx5_ib_cleanup_cong_debugfs(dev,
4977 mlx5_core_native_port_num(dev->mdev) - 1);
Mark Bloch16c19752018-01-01 13:06:58 +02004978}
4979
4980static int mlx5_ib_stage_uar_init(struct mlx5_ib_dev *dev)
4981{
Eli Cohen5fe9dec2017-01-03 23:55:25 +02004982 dev->mdev->priv.uar = mlx5_get_uars_page(dev->mdev);
4983 if (!dev->mdev->priv.uar)
Mark Bloch16c19752018-01-01 13:06:58 +02004984 return -ENOMEM;
4985 return 0;
4986}
4987
4988static void mlx5_ib_stage_uar_cleanup(struct mlx5_ib_dev *dev)
4989{
4990 mlx5_put_uars_page(dev->mdev, dev->mdev->priv.uar);
4991}
4992
Mark Blochb5ca15a2018-01-23 11:16:30 +00004993int mlx5_ib_stage_bfrag_init(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02004994{
4995 int err;
Eli Cohen5fe9dec2017-01-03 23:55:25 +02004996
4997 err = mlx5_alloc_bfreg(dev->mdev, &dev->bfreg, false, false);
4998 if (err)
Mark Bloch16c19752018-01-01 13:06:58 +02004999 return err;
Eli Cohen5fe9dec2017-01-03 23:55:25 +02005000
5001 err = mlx5_alloc_bfreg(dev->mdev, &dev->fp_bfreg, false, true);
5002 if (err)
Mark Bloch16c19752018-01-01 13:06:58 +02005003 mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
Eli Cohen5fe9dec2017-01-03 23:55:25 +02005004
Mark Bloch16c19752018-01-01 13:06:58 +02005005 return err;
5006}
Mark Bloch0837e862016-06-17 15:10:55 +03005007
Mark Blochb5ca15a2018-01-23 11:16:30 +00005008void mlx5_ib_stage_bfrag_cleanup(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02005009{
5010 mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
5011 mlx5_free_bfreg(dev->mdev, &dev->bfreg);
5012}
Eli Cohene126ba92013-07-07 17:25:49 +03005013
Matan Barak8c846602018-03-28 09:27:41 +03005014static int mlx5_ib_stage_populate_specs(struct mlx5_ib_dev *dev)
5015{
5016 return populate_specs_root(dev);
5017}
5018
Mark Blochb5ca15a2018-01-23 11:16:30 +00005019int mlx5_ib_stage_ib_reg_init(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02005020{
5021 return ib_register_device(&dev->ib_dev, NULL);
5022}
5023
Matan Barak8c846602018-03-28 09:27:41 +03005024static void mlx5_ib_stage_depopulate_specs(struct mlx5_ib_dev *dev)
5025{
5026 depopulate_specs_root(dev);
5027}
5028
Doug Ledford2d873442018-03-14 18:49:12 -04005029void mlx5_ib_stage_pre_ib_reg_umr_cleanup(struct mlx5_ib_dev *dev)
Mark Bloch42cea832018-03-14 09:14:15 +02005030{
5031 destroy_umrc_res(dev);
5032}
5033
Mark Blochb5ca15a2018-01-23 11:16:30 +00005034void mlx5_ib_stage_ib_reg_cleanup(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02005035{
5036 ib_unregister_device(&dev->ib_dev);
5037}
5038
Doug Ledford2d873442018-03-14 18:49:12 -04005039int mlx5_ib_stage_post_ib_reg_umr_init(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02005040{
5041 return create_umr_res(dev);
5042}
5043
Mark Bloch16c19752018-01-01 13:06:58 +02005044static int mlx5_ib_stage_delay_drop_init(struct mlx5_ib_dev *dev)
5045{
Maor Gottlieb03404e82017-05-30 10:29:13 +03005046 init_delay_drop(dev);
5047
Mark Bloch16c19752018-01-01 13:06:58 +02005048 return 0;
5049}
5050
5051static void mlx5_ib_stage_delay_drop_cleanup(struct mlx5_ib_dev *dev)
5052{
5053 cancel_delay_drop(dev);
5054}
5055
Mark Blochb5ca15a2018-01-23 11:16:30 +00005056int mlx5_ib_stage_class_attr_init(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02005057{
5058 int err;
5059 int i;
5060
Eli Cohene126ba92013-07-07 17:25:49 +03005061 for (i = 0; i < ARRAY_SIZE(mlx5_class_attributes); i++) {
Wei Yongjun281d1a92013-07-30 07:54:26 +08005062 err = device_create_file(&dev->ib_dev.dev,
5063 mlx5_class_attributes[i]);
5064 if (err)
Mark Bloch16c19752018-01-01 13:06:58 +02005065 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03005066 }
5067
Mark Bloch16c19752018-01-01 13:06:58 +02005068 return 0;
5069}
5070
Mark Blochfc385b72018-01-16 14:34:48 +00005071static int mlx5_ib_stage_rep_reg_init(struct mlx5_ib_dev *dev)
5072{
5073 mlx5_ib_register_vport_reps(dev);
5074
5075 return 0;
5076}
5077
5078static void mlx5_ib_stage_rep_reg_cleanup(struct mlx5_ib_dev *dev)
5079{
5080 mlx5_ib_unregister_vport_reps(dev);
5081}
5082
Mark Blochb5ca15a2018-01-23 11:16:30 +00005083void __mlx5_ib_remove(struct mlx5_ib_dev *dev,
5084 const struct mlx5_ib_profile *profile,
5085 int stage)
Mark Bloch16c19752018-01-01 13:06:58 +02005086{
5087 /* Number of stages to cleanup */
5088 while (stage) {
5089 stage--;
5090 if (profile->stage[stage].cleanup)
5091 profile->stage[stage].cleanup(dev);
5092 }
5093
5094 ib_dealloc_device((struct ib_device *)dev);
5095}
5096
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005097static void *mlx5_ib_add_slave_port(struct mlx5_core_dev *mdev, u8 port_num);
5098
Mark Blochb5ca15a2018-01-23 11:16:30 +00005099void *__mlx5_ib_add(struct mlx5_ib_dev *dev,
5100 const struct mlx5_ib_profile *profile)
Mark Bloch16c19752018-01-01 13:06:58 +02005101{
Mark Bloch16c19752018-01-01 13:06:58 +02005102 int err;
5103 int i;
5104
5105 printk_once(KERN_INFO "%s", mlx5_version);
5106
Mark Bloch16c19752018-01-01 13:06:58 +02005107 for (i = 0; i < MLX5_IB_STAGE_MAX; i++) {
5108 if (profile->stage[i].init) {
5109 err = profile->stage[i].init(dev);
5110 if (err)
5111 goto err_out;
5112 }
5113 }
5114
5115 dev->profile = profile;
Eli Cohene126ba92013-07-07 17:25:49 +03005116 dev->ib_active = true;
5117
Jack Morgenstein9603b612014-07-28 23:30:22 +03005118 return dev;
Eli Cohene126ba92013-07-07 17:25:49 +03005119
Mark Bloch16c19752018-01-01 13:06:58 +02005120err_out:
5121 __mlx5_ib_remove(dev, profile, i);
Eli Cohene126ba92013-07-07 17:25:49 +03005122
Jack Morgenstein9603b612014-07-28 23:30:22 +03005123 return NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03005124}
5125
Mark Bloch16c19752018-01-01 13:06:58 +02005126static const struct mlx5_ib_profile pf_profile = {
5127 STAGE_CREATE(MLX5_IB_STAGE_INIT,
5128 mlx5_ib_stage_init_init,
5129 mlx5_ib_stage_init_cleanup),
Mark Bloch9a4ca382018-01-16 14:42:35 +00005130 STAGE_CREATE(MLX5_IB_STAGE_FLOW_DB,
5131 mlx5_ib_stage_flow_db_init,
5132 mlx5_ib_stage_flow_db_cleanup),
Mark Bloch16c19752018-01-01 13:06:58 +02005133 STAGE_CREATE(MLX5_IB_STAGE_CAPS,
5134 mlx5_ib_stage_caps_init,
5135 NULL),
Mark Bloch8e6efa32017-11-06 12:22:13 +00005136 STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB,
5137 mlx5_ib_stage_non_default_cb,
5138 NULL),
Mark Bloch16c19752018-01-01 13:06:58 +02005139 STAGE_CREATE(MLX5_IB_STAGE_ROCE,
5140 mlx5_ib_stage_roce_init,
5141 mlx5_ib_stage_roce_cleanup),
5142 STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES,
5143 mlx5_ib_stage_dev_res_init,
5144 mlx5_ib_stage_dev_res_cleanup),
5145 STAGE_CREATE(MLX5_IB_STAGE_ODP,
5146 mlx5_ib_stage_odp_init,
Mark Bloch3cc297d2018-01-01 13:07:03 +02005147 NULL),
Mark Bloch16c19752018-01-01 13:06:58 +02005148 STAGE_CREATE(MLX5_IB_STAGE_COUNTERS,
5149 mlx5_ib_stage_counters_init,
5150 mlx5_ib_stage_counters_cleanup),
5151 STAGE_CREATE(MLX5_IB_STAGE_CONG_DEBUGFS,
5152 mlx5_ib_stage_cong_debugfs_init,
5153 mlx5_ib_stage_cong_debugfs_cleanup),
5154 STAGE_CREATE(MLX5_IB_STAGE_UAR,
5155 mlx5_ib_stage_uar_init,
5156 mlx5_ib_stage_uar_cleanup),
5157 STAGE_CREATE(MLX5_IB_STAGE_BFREG,
5158 mlx5_ib_stage_bfrag_init,
5159 mlx5_ib_stage_bfrag_cleanup),
Mark Bloch42cea832018-03-14 09:14:15 +02005160 STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR,
5161 NULL,
5162 mlx5_ib_stage_pre_ib_reg_umr_cleanup),
Matan Barak8c846602018-03-28 09:27:41 +03005163 STAGE_CREATE(MLX5_IB_STAGE_SPECS,
5164 mlx5_ib_stage_populate_specs,
5165 mlx5_ib_stage_depopulate_specs),
Mark Bloch16c19752018-01-01 13:06:58 +02005166 STAGE_CREATE(MLX5_IB_STAGE_IB_REG,
5167 mlx5_ib_stage_ib_reg_init,
5168 mlx5_ib_stage_ib_reg_cleanup),
Mark Bloch42cea832018-03-14 09:14:15 +02005169 STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR,
5170 mlx5_ib_stage_post_ib_reg_umr_init,
5171 NULL),
Mark Bloch16c19752018-01-01 13:06:58 +02005172 STAGE_CREATE(MLX5_IB_STAGE_DELAY_DROP,
5173 mlx5_ib_stage_delay_drop_init,
5174 mlx5_ib_stage_delay_drop_cleanup),
5175 STAGE_CREATE(MLX5_IB_STAGE_CLASS_ATTR,
5176 mlx5_ib_stage_class_attr_init,
5177 NULL),
Mark Bloch16c19752018-01-01 13:06:58 +02005178};
5179
Mark Blochb5ca15a2018-01-23 11:16:30 +00005180static const struct mlx5_ib_profile nic_rep_profile = {
5181 STAGE_CREATE(MLX5_IB_STAGE_INIT,
5182 mlx5_ib_stage_init_init,
5183 mlx5_ib_stage_init_cleanup),
5184 STAGE_CREATE(MLX5_IB_STAGE_FLOW_DB,
5185 mlx5_ib_stage_flow_db_init,
5186 mlx5_ib_stage_flow_db_cleanup),
5187 STAGE_CREATE(MLX5_IB_STAGE_CAPS,
5188 mlx5_ib_stage_caps_init,
5189 NULL),
5190 STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB,
5191 mlx5_ib_stage_rep_non_default_cb,
5192 NULL),
5193 STAGE_CREATE(MLX5_IB_STAGE_ROCE,
5194 mlx5_ib_stage_rep_roce_init,
5195 mlx5_ib_stage_rep_roce_cleanup),
5196 STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES,
5197 mlx5_ib_stage_dev_res_init,
5198 mlx5_ib_stage_dev_res_cleanup),
5199 STAGE_CREATE(MLX5_IB_STAGE_COUNTERS,
5200 mlx5_ib_stage_counters_init,
5201 mlx5_ib_stage_counters_cleanup),
5202 STAGE_CREATE(MLX5_IB_STAGE_UAR,
5203 mlx5_ib_stage_uar_init,
5204 mlx5_ib_stage_uar_cleanup),
5205 STAGE_CREATE(MLX5_IB_STAGE_BFREG,
5206 mlx5_ib_stage_bfrag_init,
5207 mlx5_ib_stage_bfrag_cleanup),
Doug Ledford2d873442018-03-14 18:49:12 -04005208 STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR,
5209 NULL,
5210 mlx5_ib_stage_pre_ib_reg_umr_cleanup),
Matan Barak8c846602018-03-28 09:27:41 +03005211 STAGE_CREATE(MLX5_IB_STAGE_SPECS,
5212 mlx5_ib_stage_populate_specs,
5213 mlx5_ib_stage_depopulate_specs),
Mark Blochb5ca15a2018-01-23 11:16:30 +00005214 STAGE_CREATE(MLX5_IB_STAGE_IB_REG,
5215 mlx5_ib_stage_ib_reg_init,
5216 mlx5_ib_stage_ib_reg_cleanup),
Doug Ledford2d873442018-03-14 18:49:12 -04005217 STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR,
5218 mlx5_ib_stage_post_ib_reg_umr_init,
5219 NULL),
Mark Blochb5ca15a2018-01-23 11:16:30 +00005220 STAGE_CREATE(MLX5_IB_STAGE_CLASS_ATTR,
5221 mlx5_ib_stage_class_attr_init,
5222 NULL),
5223 STAGE_CREATE(MLX5_IB_STAGE_REP_REG,
5224 mlx5_ib_stage_rep_reg_init,
5225 mlx5_ib_stage_rep_reg_cleanup),
5226};
5227
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005228static void *mlx5_ib_add_slave_port(struct mlx5_core_dev *mdev, u8 port_num)
5229{
5230 struct mlx5_ib_multiport_info *mpi;
5231 struct mlx5_ib_dev *dev;
5232 bool bound = false;
5233 int err;
5234
5235 mpi = kzalloc(sizeof(*mpi), GFP_KERNEL);
5236 if (!mpi)
5237 return NULL;
5238
5239 mpi->mdev = mdev;
5240
5241 err = mlx5_query_nic_vport_system_image_guid(mdev,
5242 &mpi->sys_image_guid);
5243 if (err) {
5244 kfree(mpi);
5245 return NULL;
5246 }
5247
5248 mutex_lock(&mlx5_ib_multiport_mutex);
5249 list_for_each_entry(dev, &mlx5_ib_dev_list, ib_dev_list) {
5250 if (dev->sys_image_guid == mpi->sys_image_guid)
5251 bound = mlx5_ib_bind_slave_port(dev, mpi);
5252
5253 if (bound) {
5254 rdma_roce_rescan_device(&dev->ib_dev);
5255 break;
5256 }
5257 }
5258
5259 if (!bound) {
5260 list_add_tail(&mpi->list, &mlx5_ib_unaffiliated_port_list);
5261 dev_dbg(&mdev->pdev->dev, "no suitable IB device found to bind to, added to unaffiliated list.\n");
5262 } else {
5263 mlx5_ib_dbg(dev, "bound port %u\n", port_num + 1);
5264 }
5265 mutex_unlock(&mlx5_ib_multiport_mutex);
5266
5267 return mpi;
5268}
5269
Mark Bloch16c19752018-01-01 13:06:58 +02005270static void *mlx5_ib_add(struct mlx5_core_dev *mdev)
5271{
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005272 enum rdma_link_layer ll;
Mark Blochb5ca15a2018-01-23 11:16:30 +00005273 struct mlx5_ib_dev *dev;
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005274 int port_type_cap;
5275
Mark Blochb5ca15a2018-01-23 11:16:30 +00005276 printk_once(KERN_INFO "%s", mlx5_version);
5277
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005278 port_type_cap = MLX5_CAP_GEN(mdev, port_type);
5279 ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
5280
5281 if (mlx5_core_is_mp_slave(mdev) && ll == IB_LINK_LAYER_ETHERNET) {
5282 u8 port_num = mlx5_core_native_port_num(mdev) - 1;
5283
5284 return mlx5_ib_add_slave_port(mdev, port_num);
5285 }
5286
Mark Blochb5ca15a2018-01-23 11:16:30 +00005287 dev = (struct mlx5_ib_dev *)ib_alloc_device(sizeof(*dev));
5288 if (!dev)
5289 return NULL;
5290
5291 dev->mdev = mdev;
5292 dev->num_ports = max(MLX5_CAP_GEN(mdev, num_ports),
5293 MLX5_CAP_GEN(mdev, num_vhca_ports));
5294
5295 if (MLX5_VPORT_MANAGER(mdev) &&
5296 mlx5_ib_eswitch_mode(mdev->priv.eswitch) == SRIOV_OFFLOADS) {
5297 dev->rep = mlx5_ib_vport_rep(mdev->priv.eswitch, 0);
5298
5299 return __mlx5_ib_add(dev, &nic_rep_profile);
5300 }
5301
5302 return __mlx5_ib_add(dev, &pf_profile);
Mark Bloch16c19752018-01-01 13:06:58 +02005303}
5304
Jack Morgenstein9603b612014-07-28 23:30:22 +03005305static void mlx5_ib_remove(struct mlx5_core_dev *mdev, void *context)
Eli Cohene126ba92013-07-07 17:25:49 +03005306{
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005307 struct mlx5_ib_multiport_info *mpi;
5308 struct mlx5_ib_dev *dev;
Haggai Eran6aec21f2014-12-11 17:04:23 +02005309
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005310 if (mlx5_core_is_mp_slave(mdev)) {
5311 mpi = context;
5312 mutex_lock(&mlx5_ib_multiport_mutex);
5313 if (mpi->ibdev)
5314 mlx5_ib_unbind_slave_port(mpi->ibdev, mpi);
5315 list_del(&mpi->list);
5316 mutex_unlock(&mlx5_ib_multiport_mutex);
5317 return;
5318 }
5319
5320 dev = context;
Mark Bloch16c19752018-01-01 13:06:58 +02005321 __mlx5_ib_remove(dev, dev->profile, MLX5_IB_STAGE_MAX);
Eli Cohene126ba92013-07-07 17:25:49 +03005322}
5323
Jack Morgenstein9603b612014-07-28 23:30:22 +03005324static struct mlx5_interface mlx5_ib_interface = {
5325 .add = mlx5_ib_add,
5326 .remove = mlx5_ib_remove,
5327 .event = mlx5_ib_event,
Artemy Kovalyovd9aaed82017-01-02 11:37:46 +02005328#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
5329 .pfault = mlx5_ib_pfault,
5330#endif
Saeed Mahameed64613d942015-04-02 17:07:34 +03005331 .protocol = MLX5_INTERFACE_PROTOCOL_IB,
Eli Cohene126ba92013-07-07 17:25:49 +03005332};
5333
Ilya Lesokhinc44ef992018-03-13 15:18:48 +02005334unsigned long mlx5_ib_get_xlt_emergency_page(void)
5335{
5336 mutex_lock(&xlt_emergency_page_mutex);
5337 return xlt_emergency_page;
5338}
5339
5340void mlx5_ib_put_xlt_emergency_page(void)
5341{
5342 mutex_unlock(&xlt_emergency_page_mutex);
5343}
5344
Eli Cohene126ba92013-07-07 17:25:49 +03005345static int __init mlx5_ib_init(void)
5346{
Haggai Eran6aec21f2014-12-11 17:04:23 +02005347 int err;
5348
Ilya Lesokhinc44ef992018-03-13 15:18:48 +02005349 xlt_emergency_page = __get_free_page(GFP_KERNEL);
5350 if (!xlt_emergency_page)
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02005351 return -ENOMEM;
5352
Ilya Lesokhinc44ef992018-03-13 15:18:48 +02005353 mutex_init(&xlt_emergency_page_mutex);
5354
5355 mlx5_ib_event_wq = alloc_ordered_workqueue("mlx5_ib_event_wq", 0);
5356 if (!mlx5_ib_event_wq) {
5357 free_page(xlt_emergency_page);
5358 return -ENOMEM;
5359 }
5360
Artemy Kovalyov81713d32017-01-18 16:58:11 +02005361 mlx5_ib_odp_init();
Jack Morgenstein9603b612014-07-28 23:30:22 +03005362
Haggai Eran6aec21f2014-12-11 17:04:23 +02005363 err = mlx5_register_interface(&mlx5_ib_interface);
Haggai Eran6aec21f2014-12-11 17:04:23 +02005364
5365 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03005366}
5367
5368static void __exit mlx5_ib_cleanup(void)
5369{
Jack Morgenstein9603b612014-07-28 23:30:22 +03005370 mlx5_unregister_interface(&mlx5_ib_interface);
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02005371 destroy_workqueue(mlx5_ib_event_wq);
Ilya Lesokhinc44ef992018-03-13 15:18:48 +02005372 mutex_destroy(&xlt_emergency_page_mutex);
5373 free_page(xlt_emergency_page);
Eli Cohene126ba92013-07-07 17:25:49 +03005374}
5375
5376module_init(mlx5_ib_init);
5377module_exit(mlx5_ib_cleanup);