blob: bc46589a904dc83b874e78cbe38ffdf47372a415 [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>
Aviad Yehezkelc6475a02018-03-28 09:27:50 +030063#include <linux/mlx5/accel.h>
Matan Barak8c846602018-03-28 09:27:41 +030064#include <rdma/uverbs_std_types.h>
Aviad Yehezkelc6475a02018-03-28 09:27:50 +030065#include <rdma/mlx5_user_ioctl_verbs.h>
66#include <rdma/mlx5_user_ioctl_cmds.h>
Matan Barak8c846602018-03-28 09:27:41 +030067
68#define UVERBS_MODULE_NAME mlx5_ib
69#include <rdma/uverbs_named_ioctl.h>
Eli Cohene126ba92013-07-07 17:25:49 +030070
71#define DRIVER_NAME "mlx5_ib"
Tariq Toukanb3599112017-02-22 17:45:46 +020072#define DRIVER_VERSION "5.0-0"
Eli Cohene126ba92013-07-07 17:25:49 +030073
74MODULE_AUTHOR("Eli Cohen <eli@mellanox.com>");
75MODULE_DESCRIPTION("Mellanox Connect-IB HCA IB driver");
76MODULE_LICENSE("Dual BSD/GPL");
Eli Cohene126ba92013-07-07 17:25:49 +030077
Eli Cohene126ba92013-07-07 17:25:49 +030078static char mlx5_version[] =
79 DRIVER_NAME ": Mellanox Connect-IB Infiniband driver v"
Tariq Toukanb3599112017-02-22 17:45:46 +020080 DRIVER_VERSION "\n";
Eli Cohene126ba92013-07-07 17:25:49 +030081
Daniel Jurgensd69a24e2018-01-04 17:25:37 +020082struct mlx5_ib_event_work {
83 struct work_struct work;
84 struct mlx5_core_dev *dev;
85 void *context;
86 enum mlx5_dev_event event;
87 unsigned long param;
88};
89
Eran Ben Elishada7525d2015-12-14 16:34:10 +020090enum {
91 MLX5_ATOMIC_SIZE_QP_8BYTES = 1 << 3,
92};
Majd Dibbiny1b5daf12015-06-04 19:30:46 +030093
Daniel Jurgensd69a24e2018-01-04 17:25:37 +020094static struct workqueue_struct *mlx5_ib_event_wq;
Daniel Jurgens32f69e42018-01-04 17:25:36 +020095static LIST_HEAD(mlx5_ib_unaffiliated_port_list);
96static LIST_HEAD(mlx5_ib_dev_list);
97/*
98 * This mutex should be held when accessing either of the above lists
99 */
100static DEFINE_MUTEX(mlx5_ib_multiport_mutex);
101
Ilya Lesokhinc44ef992018-03-13 15:18:48 +0200102/* We can't use an array for xlt_emergency_page because dma_map_single
103 * doesn't work on kernel modules memory
104 */
105static unsigned long xlt_emergency_page;
106static struct mutex xlt_emergency_page_mutex;
107
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200108struct mlx5_ib_dev *mlx5_ib_get_ibdev_from_mpi(struct mlx5_ib_multiport_info *mpi)
109{
110 struct mlx5_ib_dev *dev;
111
112 mutex_lock(&mlx5_ib_multiport_mutex);
113 dev = mpi->ibdev;
114 mutex_unlock(&mlx5_ib_multiport_mutex);
115 return dev;
116}
117
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300118static enum rdma_link_layer
Achiad Shochatebd61f62015-12-23 18:47:16 +0200119mlx5_port_type_cap_to_rdma_ll(int port_type_cap)
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300120{
Achiad Shochatebd61f62015-12-23 18:47:16 +0200121 switch (port_type_cap) {
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300122 case MLX5_CAP_PORT_TYPE_IB:
123 return IB_LINK_LAYER_INFINIBAND;
124 case MLX5_CAP_PORT_TYPE_ETH:
125 return IB_LINK_LAYER_ETHERNET;
126 default:
127 return IB_LINK_LAYER_UNSPECIFIED;
128 }
129}
130
Achiad Shochatebd61f62015-12-23 18:47:16 +0200131static enum rdma_link_layer
132mlx5_ib_port_link_layer(struct ib_device *device, u8 port_num)
133{
134 struct mlx5_ib_dev *dev = to_mdev(device);
135 int port_type_cap = MLX5_CAP_GEN(dev->mdev, port_type);
136
137 return mlx5_port_type_cap_to_rdma_ll(port_type_cap);
138}
139
Moni Shouafd65f1b2017-05-30 09:56:05 +0300140static int get_port_state(struct ib_device *ibdev,
141 u8 port_num,
142 enum ib_port_state *state)
143{
144 struct ib_port_attr attr;
145 int ret;
146
147 memset(&attr, 0, sizeof(attr));
Mark Bloch8e6efa32017-11-06 12:22:13 +0000148 ret = ibdev->query_port(ibdev, port_num, &attr);
Moni Shouafd65f1b2017-05-30 09:56:05 +0300149 if (!ret)
150 *state = attr.state;
151 return ret;
152}
153
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200154static int mlx5_netdev_event(struct notifier_block *this,
155 unsigned long event, void *ptr)
156{
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200157 struct mlx5_roce *roce = container_of(this, struct mlx5_roce, nb);
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200158 struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200159 u8 port_num = roce->native_port_num;
160 struct mlx5_core_dev *mdev;
161 struct mlx5_ib_dev *ibdev;
162
163 ibdev = roce->dev;
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200164 mdev = mlx5_ib_get_native_port_mdev(ibdev, port_num, NULL);
165 if (!mdev)
166 return NOTIFY_DONE;
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200167
Aviv Heller5ec8c832016-09-18 20:48:00 +0300168 switch (event) {
169 case NETDEV_REGISTER:
170 case NETDEV_UNREGISTER:
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200171 write_lock(&roce->netdev_lock);
Mark Blochbcf87f12018-01-16 15:02:36 +0000172 if (ibdev->rep) {
173 struct mlx5_eswitch *esw = ibdev->mdev->priv.eswitch;
174 struct net_device *rep_ndev;
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200175
Mark Blochbcf87f12018-01-16 15:02:36 +0000176 rep_ndev = mlx5_ib_get_rep_netdev(esw,
177 ibdev->rep->vport);
178 if (rep_ndev == ndev)
179 roce->netdev = (event == NETDEV_UNREGISTER) ?
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200180 NULL : ndev;
Mark Blochbcf87f12018-01-16 15:02:36 +0000181 } else if (ndev->dev.parent == &ibdev->mdev->pdev->dev) {
182 roce->netdev = (event == NETDEV_UNREGISTER) ?
183 NULL : ndev;
184 }
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200185 write_unlock(&roce->netdev_lock);
Aviv Heller5ec8c832016-09-18 20:48:00 +0300186 break;
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200187
Moni Shouafd65f1b2017-05-30 09:56:05 +0300188 case NETDEV_CHANGE:
Aviv Heller5ec8c832016-09-18 20:48:00 +0300189 case NETDEV_UP:
Aviv Heller88621df2016-09-18 20:48:02 +0300190 case NETDEV_DOWN: {
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200191 struct net_device *lag_ndev = mlx5_lag_get_roce_netdev(mdev);
Aviv Heller88621df2016-09-18 20:48:02 +0300192 struct net_device *upper = NULL;
193
194 if (lag_ndev) {
195 upper = netdev_master_upper_dev_get(lag_ndev);
196 dev_put(lag_ndev);
197 }
198
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200199 if ((upper == ndev || (!upper && ndev == roce->netdev))
Aviv Heller88621df2016-09-18 20:48:02 +0300200 && ibdev->ib_active) {
Bart Van Assche626bc022016-12-05 17:18:08 -0800201 struct ib_event ibev = { };
Moni Shouafd65f1b2017-05-30 09:56:05 +0300202 enum ib_port_state port_state;
Aviv Heller5ec8c832016-09-18 20:48:00 +0300203
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200204 if (get_port_state(&ibdev->ib_dev, port_num,
205 &port_state))
206 goto done;
Moni Shouafd65f1b2017-05-30 09:56:05 +0300207
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200208 if (roce->last_port_state == port_state)
209 goto done;
Moni Shouafd65f1b2017-05-30 09:56:05 +0300210
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200211 roce->last_port_state = port_state;
Aviv Heller5ec8c832016-09-18 20:48:00 +0300212 ibev.device = &ibdev->ib_dev;
Moni Shouafd65f1b2017-05-30 09:56:05 +0300213 if (port_state == IB_PORT_DOWN)
214 ibev.event = IB_EVENT_PORT_ERR;
215 else if (port_state == IB_PORT_ACTIVE)
216 ibev.event = IB_EVENT_PORT_ACTIVE;
217 else
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200218 goto done;
Moni Shouafd65f1b2017-05-30 09:56:05 +0300219
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200220 ibev.element.port_num = port_num;
Aviv Heller5ec8c832016-09-18 20:48:00 +0300221 ib_dispatch_event(&ibev);
222 }
223 break;
Aviv Heller88621df2016-09-18 20:48:02 +0300224 }
Aviv Heller5ec8c832016-09-18 20:48:00 +0300225
226 default:
227 break;
228 }
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200229done:
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200230 mlx5_ib_put_native_port_mdev(ibdev, port_num);
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200231 return NOTIFY_DONE;
232}
233
234static struct net_device *mlx5_ib_get_netdev(struct ib_device *device,
235 u8 port_num)
236{
237 struct mlx5_ib_dev *ibdev = to_mdev(device);
238 struct net_device *ndev;
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200239 struct mlx5_core_dev *mdev;
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200240
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200241 mdev = mlx5_ib_get_native_port_mdev(ibdev, port_num, NULL);
242 if (!mdev)
243 return NULL;
244
245 ndev = mlx5_lag_get_roce_netdev(mdev);
Aviv Heller88621df2016-09-18 20:48:02 +0300246 if (ndev)
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200247 goto out;
Aviv Heller88621df2016-09-18 20:48:02 +0300248
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200249 /* Ensure ndev does not disappear before we invoke dev_hold()
250 */
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200251 read_lock(&ibdev->roce[port_num - 1].netdev_lock);
252 ndev = ibdev->roce[port_num - 1].netdev;
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200253 if (ndev)
254 dev_hold(ndev);
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200255 read_unlock(&ibdev->roce[port_num - 1].netdev_lock);
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200256
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200257out:
258 mlx5_ib_put_native_port_mdev(ibdev, port_num);
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200259 return ndev;
260}
261
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200262struct mlx5_core_dev *mlx5_ib_get_native_port_mdev(struct mlx5_ib_dev *ibdev,
263 u8 ib_port_num,
264 u8 *native_port_num)
265{
266 enum rdma_link_layer ll = mlx5_ib_port_link_layer(&ibdev->ib_dev,
267 ib_port_num);
268 struct mlx5_core_dev *mdev = NULL;
269 struct mlx5_ib_multiport_info *mpi;
270 struct mlx5_ib_port *port;
271
Mark Bloch210b1f72018-03-05 20:09:47 +0200272 if (!mlx5_core_mp_enabled(ibdev->mdev) ||
273 ll != IB_LINK_LAYER_ETHERNET) {
274 if (native_port_num)
275 *native_port_num = ib_port_num;
276 return ibdev->mdev;
277 }
278
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200279 if (native_port_num)
280 *native_port_num = 1;
281
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200282 port = &ibdev->port[ib_port_num - 1];
283 if (!port)
284 return NULL;
285
286 spin_lock(&port->mp.mpi_lock);
287 mpi = ibdev->port[ib_port_num - 1].mp.mpi;
288 if (mpi && !mpi->unaffiliate) {
289 mdev = mpi->mdev;
290 /* If it's the master no need to refcount, it'll exist
291 * as long as the ib_dev exists.
292 */
293 if (!mpi->is_master)
294 mpi->mdev_refcnt++;
295 }
296 spin_unlock(&port->mp.mpi_lock);
297
298 return mdev;
299}
300
301void mlx5_ib_put_native_port_mdev(struct mlx5_ib_dev *ibdev, u8 port_num)
302{
303 enum rdma_link_layer ll = mlx5_ib_port_link_layer(&ibdev->ib_dev,
304 port_num);
305 struct mlx5_ib_multiport_info *mpi;
306 struct mlx5_ib_port *port;
307
308 if (!mlx5_core_mp_enabled(ibdev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
309 return;
310
311 port = &ibdev->port[port_num - 1];
312
313 spin_lock(&port->mp.mpi_lock);
314 mpi = ibdev->port[port_num - 1].mp.mpi;
315 if (mpi->is_master)
316 goto out;
317
318 mpi->mdev_refcnt--;
319 if (mpi->unaffiliate)
320 complete(&mpi->unref_comp);
321out:
322 spin_unlock(&port->mp.mpi_lock);
323}
324
Noa Osherovichf1b65df2017-04-20 20:53:33 +0300325static int translate_eth_proto_oper(u32 eth_proto_oper, u8 *active_speed,
326 u8 *active_width)
327{
328 switch (eth_proto_oper) {
329 case MLX5E_PROT_MASK(MLX5E_1000BASE_CX_SGMII):
330 case MLX5E_PROT_MASK(MLX5E_1000BASE_KX):
331 case MLX5E_PROT_MASK(MLX5E_100BASE_TX):
332 case MLX5E_PROT_MASK(MLX5E_1000BASE_T):
333 *active_width = IB_WIDTH_1X;
334 *active_speed = IB_SPEED_SDR;
335 break;
336 case MLX5E_PROT_MASK(MLX5E_10GBASE_T):
337 case MLX5E_PROT_MASK(MLX5E_10GBASE_CX4):
338 case MLX5E_PROT_MASK(MLX5E_10GBASE_KX4):
339 case MLX5E_PROT_MASK(MLX5E_10GBASE_KR):
340 case MLX5E_PROT_MASK(MLX5E_10GBASE_CR):
341 case MLX5E_PROT_MASK(MLX5E_10GBASE_SR):
342 case MLX5E_PROT_MASK(MLX5E_10GBASE_ER):
343 *active_width = IB_WIDTH_1X;
344 *active_speed = IB_SPEED_QDR;
345 break;
346 case MLX5E_PROT_MASK(MLX5E_25GBASE_CR):
347 case MLX5E_PROT_MASK(MLX5E_25GBASE_KR):
348 case MLX5E_PROT_MASK(MLX5E_25GBASE_SR):
349 *active_width = IB_WIDTH_1X;
350 *active_speed = IB_SPEED_EDR;
351 break;
352 case MLX5E_PROT_MASK(MLX5E_40GBASE_CR4):
353 case MLX5E_PROT_MASK(MLX5E_40GBASE_KR4):
354 case MLX5E_PROT_MASK(MLX5E_40GBASE_SR4):
355 case MLX5E_PROT_MASK(MLX5E_40GBASE_LR4):
356 *active_width = IB_WIDTH_4X;
357 *active_speed = IB_SPEED_QDR;
358 break;
359 case MLX5E_PROT_MASK(MLX5E_50GBASE_CR2):
360 case MLX5E_PROT_MASK(MLX5E_50GBASE_KR2):
361 case MLX5E_PROT_MASK(MLX5E_50GBASE_SR2):
362 *active_width = IB_WIDTH_1X;
363 *active_speed = IB_SPEED_HDR;
364 break;
365 case MLX5E_PROT_MASK(MLX5E_56GBASE_R4):
366 *active_width = IB_WIDTH_4X;
367 *active_speed = IB_SPEED_FDR;
368 break;
369 case MLX5E_PROT_MASK(MLX5E_100GBASE_CR4):
370 case MLX5E_PROT_MASK(MLX5E_100GBASE_SR4):
371 case MLX5E_PROT_MASK(MLX5E_100GBASE_KR4):
372 case MLX5E_PROT_MASK(MLX5E_100GBASE_LR4):
373 *active_width = IB_WIDTH_4X;
374 *active_speed = IB_SPEED_EDR;
375 break;
376 default:
377 return -EINVAL;
378 }
379
380 return 0;
381}
382
Ilan Tayari095b0922017-05-14 16:04:30 +0300383static int mlx5_query_port_roce(struct ib_device *device, u8 port_num,
384 struct ib_port_attr *props)
Achiad Shochat3f89a642015-12-23 18:47:21 +0200385{
386 struct mlx5_ib_dev *dev = to_mdev(device);
Colin Ian Kingda005f92018-01-09 15:55:43 +0000387 struct mlx5_core_dev *mdev;
Aviv Heller88621df2016-09-18 20:48:02 +0300388 struct net_device *ndev, *upper;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200389 enum ib_mtu ndev_ib_mtu;
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200390 bool put_mdev = true;
Leon Romanovskyc876a1b2016-01-09 13:06:25 +0200391 u16 qkey_viol_cntr;
Noa Osherovichf1b65df2017-04-20 20:53:33 +0300392 u32 eth_prot_oper;
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200393 u8 mdev_port_num;
Ilan Tayari095b0922017-05-14 16:04:30 +0300394 int err;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200395
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200396 mdev = mlx5_ib_get_native_port_mdev(dev, port_num, &mdev_port_num);
397 if (!mdev) {
398 /* This means the port isn't affiliated yet. Get the
399 * info for the master port instead.
400 */
401 put_mdev = false;
402 mdev = dev->mdev;
403 mdev_port_num = 1;
404 port_num = 1;
405 }
406
Noa Osherovichf1b65df2017-04-20 20:53:33 +0300407 /* Possible bad flows are checked before filling out props so in case
408 * of an error it will still be zeroed out.
Noa Osherovich50f22fd2017-04-20 20:53:32 +0300409 */
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200410 err = mlx5_query_port_eth_proto_oper(mdev, &eth_prot_oper,
411 mdev_port_num);
Ilan Tayari095b0922017-05-14 16:04:30 +0300412 if (err)
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200413 goto out;
Noa Osherovichf1b65df2017-04-20 20:53:33 +0300414
Honggang Li7672ed32018-03-16 10:37:13 +0800415 props->active_width = IB_WIDTH_4X;
416 props->active_speed = IB_SPEED_QDR;
417
Noa Osherovichf1b65df2017-04-20 20:53:33 +0300418 translate_eth_proto_oper(eth_prot_oper, &props->active_speed,
419 &props->active_width);
Achiad Shochat3f89a642015-12-23 18:47:21 +0200420
421 props->port_cap_flags |= IB_PORT_CM_SUP;
422 props->port_cap_flags |= IB_PORT_IP_BASED_GIDS;
423
424 props->gid_tbl_len = MLX5_CAP_ROCE(dev->mdev,
425 roce_address_table_size);
426 props->max_mtu = IB_MTU_4096;
427 props->max_msg_sz = 1 << MLX5_CAP_GEN(dev->mdev, log_max_msg);
428 props->pkey_tbl_len = 1;
429 props->state = IB_PORT_DOWN;
430 props->phys_state = 3;
431
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200432 mlx5_query_nic_vport_qkey_viol_cntr(mdev, &qkey_viol_cntr);
Leon Romanovskyc876a1b2016-01-09 13:06:25 +0200433 props->qkey_viol_cntr = qkey_viol_cntr;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200434
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200435 /* If this is a stub query for an unaffiliated port stop here */
436 if (!put_mdev)
437 goto out;
438
Achiad Shochat3f89a642015-12-23 18:47:21 +0200439 ndev = mlx5_ib_get_netdev(device, port_num);
440 if (!ndev)
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200441 goto out;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200442
Aviv Heller88621df2016-09-18 20:48:02 +0300443 if (mlx5_lag_is_active(dev->mdev)) {
444 rcu_read_lock();
445 upper = netdev_master_upper_dev_get_rcu(ndev);
446 if (upper) {
447 dev_put(ndev);
448 ndev = upper;
449 dev_hold(ndev);
450 }
451 rcu_read_unlock();
452 }
453
Achiad Shochat3f89a642015-12-23 18:47:21 +0200454 if (netif_running(ndev) && netif_carrier_ok(ndev)) {
455 props->state = IB_PORT_ACTIVE;
456 props->phys_state = 5;
457 }
458
459 ndev_ib_mtu = iboe_get_mtu(ndev->mtu);
460
461 dev_put(ndev);
462
463 props->active_mtu = min(props->max_mtu, ndev_ib_mtu);
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200464out:
465 if (put_mdev)
466 mlx5_ib_put_native_port_mdev(dev, port_num);
467 return err;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200468}
469
Ilan Tayari095b0922017-05-14 16:04:30 +0300470static int set_roce_addr(struct mlx5_ib_dev *dev, u8 port_num,
471 unsigned int index, const union ib_gid *gid,
472 const struct ib_gid_attr *attr)
Achiad Shochat3cca2602015-12-23 18:47:23 +0200473{
Ilan Tayari095b0922017-05-14 16:04:30 +0300474 enum ib_gid_type gid_type = IB_GID_TYPE_IB;
475 u8 roce_version = 0;
476 u8 roce_l3_type = 0;
477 bool vlan = false;
478 u8 mac[ETH_ALEN];
479 u16 vlan_id = 0;
Achiad Shochat3cca2602015-12-23 18:47:23 +0200480
Ilan Tayari095b0922017-05-14 16:04:30 +0300481 if (gid) {
482 gid_type = attr->gid_type;
483 ether_addr_copy(mac, attr->ndev->dev_addr);
Achiad Shochat3cca2602015-12-23 18:47:23 +0200484
Ilan Tayari095b0922017-05-14 16:04:30 +0300485 if (is_vlan_dev(attr->ndev)) {
486 vlan = true;
487 vlan_id = vlan_dev_vlan_id(attr->ndev);
488 }
Achiad Shochat3cca2602015-12-23 18:47:23 +0200489 }
490
Ilan Tayari095b0922017-05-14 16:04:30 +0300491 switch (gid_type) {
Achiad Shochat3cca2602015-12-23 18:47:23 +0200492 case IB_GID_TYPE_IB:
Ilan Tayari095b0922017-05-14 16:04:30 +0300493 roce_version = MLX5_ROCE_VERSION_1;
Achiad Shochat3cca2602015-12-23 18:47:23 +0200494 break;
495 case IB_GID_TYPE_ROCE_UDP_ENCAP:
Ilan Tayari095b0922017-05-14 16:04:30 +0300496 roce_version = MLX5_ROCE_VERSION_2;
497 if (ipv6_addr_v4mapped((void *)gid))
498 roce_l3_type = MLX5_ROCE_L3_TYPE_IPV4;
499 else
500 roce_l3_type = MLX5_ROCE_L3_TYPE_IPV6;
Achiad Shochat3cca2602015-12-23 18:47:23 +0200501 break;
502
503 default:
Ilan Tayari095b0922017-05-14 16:04:30 +0300504 mlx5_ib_warn(dev, "Unexpected GID type %u\n", gid_type);
Achiad Shochat3cca2602015-12-23 18:47:23 +0200505 }
506
Ilan Tayari095b0922017-05-14 16:04:30 +0300507 return mlx5_core_roce_gid_set(dev->mdev, index, roce_version,
508 roce_l3_type, gid->raw, mac, vlan,
Daniel Jurgenscfe4e372018-01-04 17:25:41 +0200509 vlan_id, port_num);
Achiad Shochat3cca2602015-12-23 18:47:23 +0200510}
511
Parav Pandit414448d2018-04-01 15:08:24 +0300512static int mlx5_ib_add_gid(const union ib_gid *gid,
Achiad Shochat3cca2602015-12-23 18:47:23 +0200513 const struct ib_gid_attr *attr,
514 __always_unused void **context)
515{
Parav Pandit414448d2018-04-01 15:08:24 +0300516 return set_roce_addr(to_mdev(attr->device), attr->port_num,
517 attr->index, gid, attr);
Achiad Shochat3cca2602015-12-23 18:47:23 +0200518}
519
Parav Pandit414448d2018-04-01 15:08:24 +0300520static int mlx5_ib_del_gid(const struct ib_gid_attr *attr,
521 __always_unused void **context)
Achiad Shochat3cca2602015-12-23 18:47:23 +0200522{
Parav Pandit414448d2018-04-01 15:08:24 +0300523 return set_roce_addr(to_mdev(attr->device), attr->port_num,
524 attr->index, NULL, NULL);
Achiad Shochat3cca2602015-12-23 18:47:23 +0200525}
526
Achiad Shochat2811ba52015-12-23 18:47:24 +0200527__be16 mlx5_get_roce_udp_sport(struct mlx5_ib_dev *dev, u8 port_num,
528 int index)
529{
530 struct ib_gid_attr attr;
531 union ib_gid gid;
532
533 if (ib_get_cached_gid(&dev->ib_dev, port_num, index, &gid, &attr))
534 return 0;
535
Achiad Shochat2811ba52015-12-23 18:47:24 +0200536 dev_put(attr.ndev);
537
538 if (attr.gid_type != IB_GID_TYPE_ROCE_UDP_ENCAP)
539 return 0;
540
541 return cpu_to_be16(MLX5_CAP_ROCE(dev->mdev, r_roce_min_src_udp_port));
542}
543
Majd Dibbinyed884512017-01-18 14:10:35 +0200544int mlx5_get_roce_gid_type(struct mlx5_ib_dev *dev, u8 port_num,
545 int index, enum ib_gid_type *gid_type)
546{
547 struct ib_gid_attr attr;
548 union ib_gid gid;
549 int ret;
550
551 ret = ib_get_cached_gid(&dev->ib_dev, port_num, index, &gid, &attr);
552 if (ret)
553 return ret;
554
Majd Dibbinyed884512017-01-18 14:10:35 +0200555 dev_put(attr.ndev);
556
557 *gid_type = attr.gid_type;
558
559 return 0;
560}
561
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300562static int mlx5_use_mad_ifc(struct mlx5_ib_dev *dev)
563{
Noa Osherovich7fae6652016-09-12 19:16:23 +0300564 if (MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_IB)
565 return !MLX5_CAP_GEN(dev->mdev, ib_virt);
566 return 0;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300567}
568
569enum {
570 MLX5_VPORT_ACCESS_METHOD_MAD,
571 MLX5_VPORT_ACCESS_METHOD_HCA,
572 MLX5_VPORT_ACCESS_METHOD_NIC,
573};
574
575static int mlx5_get_vport_access_method(struct ib_device *ibdev)
576{
577 if (mlx5_use_mad_ifc(to_mdev(ibdev)))
578 return MLX5_VPORT_ACCESS_METHOD_MAD;
579
Achiad Shochatebd61f62015-12-23 18:47:16 +0200580 if (mlx5_ib_port_link_layer(ibdev, 1) ==
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300581 IB_LINK_LAYER_ETHERNET)
582 return MLX5_VPORT_ACCESS_METHOD_NIC;
583
584 return MLX5_VPORT_ACCESS_METHOD_HCA;
585}
586
Eran Ben Elishada7525d2015-12-14 16:34:10 +0200587static void get_atomic_caps(struct mlx5_ib_dev *dev,
Moni Shoua776a3902018-01-02 16:19:33 +0200588 u8 atomic_size_qp,
Eran Ben Elishada7525d2015-12-14 16:34:10 +0200589 struct ib_device_attr *props)
590{
591 u8 tmp;
592 u8 atomic_operations = MLX5_CAP_ATOMIC(dev->mdev, atomic_operations);
Eran Ben Elishada7525d2015-12-14 16:34:10 +0200593 u8 atomic_req_8B_endianness_mode =
Or Gerlitzbd108382017-05-28 15:24:17 +0300594 MLX5_CAP_ATOMIC(dev->mdev, atomic_req_8B_endianness_mode);
Eran Ben Elishada7525d2015-12-14 16:34:10 +0200595
596 /* Check if HW supports 8 bytes standard atomic operations and capable
597 * of host endianness respond
598 */
599 tmp = MLX5_ATOMIC_OPS_CMP_SWAP | MLX5_ATOMIC_OPS_FETCH_ADD;
600 if (((atomic_operations & tmp) == tmp) &&
601 (atomic_size_qp & MLX5_ATOMIC_SIZE_QP_8BYTES) &&
602 (atomic_req_8B_endianness_mode)) {
603 props->atomic_cap = IB_ATOMIC_HCA;
604 } else {
605 props->atomic_cap = IB_ATOMIC_NONE;
606 }
607}
608
Moni Shoua776a3902018-01-02 16:19:33 +0200609static void get_atomic_caps_qp(struct mlx5_ib_dev *dev,
610 struct ib_device_attr *props)
611{
612 u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_qp);
613
614 get_atomic_caps(dev, atomic_size_qp, props);
615}
616
617static void get_atomic_caps_dc(struct mlx5_ib_dev *dev,
618 struct ib_device_attr *props)
619{
620 u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_dc);
621
622 get_atomic_caps(dev, atomic_size_qp, props);
623}
624
625bool mlx5_ib_dc_atomic_is_supported(struct mlx5_ib_dev *dev)
626{
627 struct ib_device_attr props = {};
628
629 get_atomic_caps_dc(dev, &props);
630 return (props.atomic_cap == IB_ATOMIC_HCA) ? true : false;
631}
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300632static int mlx5_query_system_image_guid(struct ib_device *ibdev,
633 __be64 *sys_image_guid)
634{
635 struct mlx5_ib_dev *dev = to_mdev(ibdev);
636 struct mlx5_core_dev *mdev = dev->mdev;
637 u64 tmp;
638 int err;
639
640 switch (mlx5_get_vport_access_method(ibdev)) {
641 case MLX5_VPORT_ACCESS_METHOD_MAD:
642 return mlx5_query_mad_ifc_system_image_guid(ibdev,
643 sys_image_guid);
644
645 case MLX5_VPORT_ACCESS_METHOD_HCA:
646 err = mlx5_query_hca_vport_system_image_guid(mdev, &tmp);
Achiad Shochat3f89a642015-12-23 18:47:21 +0200647 break;
648
649 case MLX5_VPORT_ACCESS_METHOD_NIC:
650 err = mlx5_query_nic_vport_system_image_guid(mdev, &tmp);
651 break;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300652
653 default:
654 return -EINVAL;
655 }
Achiad Shochat3f89a642015-12-23 18:47:21 +0200656
657 if (!err)
658 *sys_image_guid = cpu_to_be64(tmp);
659
660 return err;
661
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300662}
663
664static int mlx5_query_max_pkeys(struct ib_device *ibdev,
665 u16 *max_pkeys)
666{
667 struct mlx5_ib_dev *dev = to_mdev(ibdev);
668 struct mlx5_core_dev *mdev = dev->mdev;
669
670 switch (mlx5_get_vport_access_method(ibdev)) {
671 case MLX5_VPORT_ACCESS_METHOD_MAD:
672 return mlx5_query_mad_ifc_max_pkeys(ibdev, max_pkeys);
673
674 case MLX5_VPORT_ACCESS_METHOD_HCA:
675 case MLX5_VPORT_ACCESS_METHOD_NIC:
676 *max_pkeys = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev,
677 pkey_table_size));
678 return 0;
679
680 default:
681 return -EINVAL;
682 }
683}
684
685static int mlx5_query_vendor_id(struct ib_device *ibdev,
686 u32 *vendor_id)
687{
688 struct mlx5_ib_dev *dev = to_mdev(ibdev);
689
690 switch (mlx5_get_vport_access_method(ibdev)) {
691 case MLX5_VPORT_ACCESS_METHOD_MAD:
692 return mlx5_query_mad_ifc_vendor_id(ibdev, vendor_id);
693
694 case MLX5_VPORT_ACCESS_METHOD_HCA:
695 case MLX5_VPORT_ACCESS_METHOD_NIC:
696 return mlx5_core_query_vendor_id(dev->mdev, vendor_id);
697
698 default:
699 return -EINVAL;
700 }
701}
702
703static int mlx5_query_node_guid(struct mlx5_ib_dev *dev,
704 __be64 *node_guid)
705{
706 u64 tmp;
707 int err;
708
709 switch (mlx5_get_vport_access_method(&dev->ib_dev)) {
710 case MLX5_VPORT_ACCESS_METHOD_MAD:
711 return mlx5_query_mad_ifc_node_guid(dev, node_guid);
712
713 case MLX5_VPORT_ACCESS_METHOD_HCA:
714 err = mlx5_query_hca_vport_node_guid(dev->mdev, &tmp);
Achiad Shochat3f89a642015-12-23 18:47:21 +0200715 break;
716
717 case MLX5_VPORT_ACCESS_METHOD_NIC:
718 err = mlx5_query_nic_vport_node_guid(dev->mdev, &tmp);
719 break;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300720
721 default:
722 return -EINVAL;
723 }
Achiad Shochat3f89a642015-12-23 18:47:21 +0200724
725 if (!err)
726 *node_guid = cpu_to_be64(tmp);
727
728 return err;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300729}
730
731struct mlx5_reg_node_desc {
Yuval Shaiabd99fde2016-08-25 10:57:07 -0700732 u8 desc[IB_DEVICE_NODE_DESC_MAX];
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300733};
734
735static int mlx5_query_node_desc(struct mlx5_ib_dev *dev, char *node_desc)
736{
737 struct mlx5_reg_node_desc in;
738
739 if (mlx5_use_mad_ifc(dev))
740 return mlx5_query_mad_ifc_node_desc(dev, node_desc);
741
742 memset(&in, 0, sizeof(in));
743
744 return mlx5_core_access_reg(dev->mdev, &in, sizeof(in), node_desc,
745 sizeof(struct mlx5_reg_node_desc),
746 MLX5_REG_NODE_DESC, 0, 0);
747}
748
Eli Cohene126ba92013-07-07 17:25:49 +0300749static int mlx5_ib_query_device(struct ib_device *ibdev,
Matan Barak2528e332015-06-11 16:35:25 +0300750 struct ib_device_attr *props,
751 struct ib_udata *uhw)
Eli Cohene126ba92013-07-07 17:25:49 +0300752{
753 struct mlx5_ib_dev *dev = to_mdev(ibdev);
Saeed Mahameed938fe832015-05-28 22:28:41 +0300754 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +0300755 int err = -ENOMEM;
Eli Cohen288c01b2016-10-27 16:36:45 +0300756 int max_sq_desc;
Eli Cohene126ba92013-07-07 17:25:49 +0300757 int max_rq_sg;
758 int max_sq_sg;
Sagi Grimberge0238a62015-07-21 14:40:12 +0300759 u64 min_page_size = 1ull << MLX5_CAP_GEN(mdev, log_pg_sz);
Daniel Jurgens85c7c012018-01-04 17:25:43 +0200760 bool raw_support = !mlx5_core_mp_enabled(mdev);
Bodong Wang402ca532016-06-17 15:02:20 +0300761 struct mlx5_ib_query_device_resp resp = {};
762 size_t resp_len;
763 u64 max_tso;
Eli Cohene126ba92013-07-07 17:25:49 +0300764
Bodong Wang402ca532016-06-17 15:02:20 +0300765 resp_len = sizeof(resp.comp_mask) + sizeof(resp.response_length);
766 if (uhw->outlen && uhw->outlen < resp_len)
767 return -EINVAL;
768 else
769 resp.response_length = resp_len;
770
771 if (uhw->inlen && !ib_is_udata_cleared(uhw, 0, uhw->inlen))
Matan Barak2528e332015-06-11 16:35:25 +0300772 return -EINVAL;
773
Eli Cohene126ba92013-07-07 17:25:49 +0300774 memset(props, 0, sizeof(*props));
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300775 err = mlx5_query_system_image_guid(ibdev,
776 &props->sys_image_guid);
777 if (err)
778 return err;
779
780 err = mlx5_query_max_pkeys(ibdev, &props->max_pkeys);
781 if (err)
782 return err;
783
784 err = mlx5_query_vendor_id(ibdev, &props->vendor_id);
785 if (err)
786 return err;
Eli Cohene126ba92013-07-07 17:25:49 +0300787
Jack Morgenstein9603b612014-07-28 23:30:22 +0300788 props->fw_ver = ((u64)fw_rev_maj(dev->mdev) << 32) |
789 (fw_rev_min(dev->mdev) << 16) |
790 fw_rev_sub(dev->mdev);
Eli Cohene126ba92013-07-07 17:25:49 +0300791 props->device_cap_flags = IB_DEVICE_CHANGE_PHY_PORT |
792 IB_DEVICE_PORT_ACTIVE_EVENT |
793 IB_DEVICE_SYS_IMAGE_GUID |
Eli Cohen1a4c3a32014-02-06 17:41:25 +0200794 IB_DEVICE_RC_RNR_NAK_GEN;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300795
796 if (MLX5_CAP_GEN(mdev, pkv))
Eli Cohene126ba92013-07-07 17:25:49 +0300797 props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300798 if (MLX5_CAP_GEN(mdev, qkv))
Eli Cohene126ba92013-07-07 17:25:49 +0300799 props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300800 if (MLX5_CAP_GEN(mdev, apm))
Eli Cohene126ba92013-07-07 17:25:49 +0300801 props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300802 if (MLX5_CAP_GEN(mdev, xrc))
Eli Cohene126ba92013-07-07 17:25:49 +0300803 props->device_cap_flags |= IB_DEVICE_XRC;
Matan Barakd2370e02016-02-29 18:05:30 +0200804 if (MLX5_CAP_GEN(mdev, imaicl)) {
805 props->device_cap_flags |= IB_DEVICE_MEM_WINDOW |
806 IB_DEVICE_MEM_WINDOW_TYPE_2B;
807 props->max_mw = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
Sagi Grimbergb005d312016-02-29 19:07:33 +0200808 /* We support 'Gappy' memory registration too */
809 props->device_cap_flags |= IB_DEVICE_SG_GAPS_REG;
Matan Barakd2370e02016-02-29 18:05:30 +0200810 }
Eli Cohene126ba92013-07-07 17:25:49 +0300811 props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300812 if (MLX5_CAP_GEN(mdev, sho)) {
Sagi Grimberg2dea9092014-02-23 14:19:13 +0200813 props->device_cap_flags |= IB_DEVICE_SIGNATURE_HANDOVER;
814 /* At this stage no support for signature handover */
815 props->sig_prot_cap = IB_PROT_T10DIF_TYPE_1 |
816 IB_PROT_T10DIF_TYPE_2 |
817 IB_PROT_T10DIF_TYPE_3;
818 props->sig_guard_cap = IB_GUARD_T10DIF_CRC |
819 IB_GUARD_T10DIF_CSUM;
820 }
Saeed Mahameed938fe832015-05-28 22:28:41 +0300821 if (MLX5_CAP_GEN(mdev, block_lb_mc))
Eli Cohenf360d882014-04-02 00:10:16 +0300822 props->device_cap_flags |= IB_DEVICE_BLOCK_MULTICAST_LOOPBACK;
Eli Cohene126ba92013-07-07 17:25:49 +0300823
Daniel Jurgens85c7c012018-01-04 17:25:43 +0200824 if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) && raw_support) {
Noa Osheroviche8161332017-01-18 15:40:01 +0200825 if (MLX5_CAP_ETH(mdev, csum_cap)) {
826 /* Legacy bit to support old userspace libraries */
Bodong Wang88115fe2015-12-18 13:53:20 +0200827 props->device_cap_flags |= IB_DEVICE_RAW_IP_CSUM;
Noa Osheroviche8161332017-01-18 15:40:01 +0200828 props->raw_packet_caps |= IB_RAW_PACKET_CAP_IP_CSUM;
829 }
830
831 if (MLX5_CAP_ETH(dev->mdev, vlan_cap))
832 props->raw_packet_caps |=
833 IB_RAW_PACKET_CAP_CVLAN_STRIPPING;
Bodong Wang88115fe2015-12-18 13:53:20 +0200834
Bodong Wang402ca532016-06-17 15:02:20 +0300835 if (field_avail(typeof(resp), tso_caps, uhw->outlen)) {
836 max_tso = MLX5_CAP_ETH(mdev, max_lso_cap);
837 if (max_tso) {
838 resp.tso_caps.max_tso = 1 << max_tso;
839 resp.tso_caps.supported_qpts |=
840 1 << IB_QPT_RAW_PACKET;
841 resp.response_length += sizeof(resp.tso_caps);
842 }
843 }
Yishai Hadas31f69a82016-08-28 11:28:45 +0300844
845 if (field_avail(typeof(resp), rss_caps, uhw->outlen)) {
846 resp.rss_caps.rx_hash_function =
847 MLX5_RX_HASH_FUNC_TOEPLITZ;
848 resp.rss_caps.rx_hash_fields_mask =
849 MLX5_RX_HASH_SRC_IPV4 |
850 MLX5_RX_HASH_DST_IPV4 |
851 MLX5_RX_HASH_SRC_IPV6 |
852 MLX5_RX_HASH_DST_IPV6 |
853 MLX5_RX_HASH_SRC_PORT_TCP |
854 MLX5_RX_HASH_DST_PORT_TCP |
855 MLX5_RX_HASH_SRC_PORT_UDP |
Maor Gottlieb4e2b53a2017-12-24 14:51:25 +0200856 MLX5_RX_HASH_DST_PORT_UDP |
857 MLX5_RX_HASH_INNER;
Yishai Hadas31f69a82016-08-28 11:28:45 +0300858 resp.response_length += sizeof(resp.rss_caps);
859 }
860 } else {
861 if (field_avail(typeof(resp), tso_caps, uhw->outlen))
862 resp.response_length += sizeof(resp.tso_caps);
863 if (field_avail(typeof(resp), rss_caps, uhw->outlen))
864 resp.response_length += sizeof(resp.rss_caps);
Bodong Wang402ca532016-06-17 15:02:20 +0300865 }
866
Erez Shitritf0313962016-02-21 16:27:17 +0200867 if (MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) {
868 props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
869 props->device_cap_flags |= IB_DEVICE_UD_TSO;
870 }
871
Maor Gottlieb03404e82017-05-30 10:29:13 +0300872 if (MLX5_CAP_GEN(dev->mdev, rq_delay_drop) &&
Daniel Jurgens85c7c012018-01-04 17:25:43 +0200873 MLX5_CAP_GEN(dev->mdev, general_notification_event) &&
874 raw_support)
Maor Gottlieb03404e82017-05-30 10:29:13 +0300875 props->raw_packet_caps |= IB_RAW_PACKET_CAP_DELAY_DROP;
876
Yishai Hadas1d54f892017-06-08 16:15:11 +0300877 if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads) &&
878 MLX5_CAP_IPOIB_ENHANCED(mdev, csum_cap))
879 props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
880
Majd Dibbinycff5a0f2016-04-17 17:19:38 +0300881 if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
Daniel Jurgens85c7c012018-01-04 17:25:43 +0200882 MLX5_CAP_ETH(dev->mdev, scatter_fcs) &&
883 raw_support) {
Noa Osheroviche8161332017-01-18 15:40:01 +0200884 /* Legacy bit to support old userspace libraries */
Majd Dibbinycff5a0f2016-04-17 17:19:38 +0300885 props->device_cap_flags |= IB_DEVICE_RAW_SCATTER_FCS;
Noa Osheroviche8161332017-01-18 15:40:01 +0200886 props->raw_packet_caps |= IB_RAW_PACKET_CAP_SCATTER_FCS;
887 }
Majd Dibbinycff5a0f2016-04-17 17:19:38 +0300888
Maor Gottliebda6d6ba32016-06-04 15:15:28 +0300889 if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS))
890 props->device_cap_flags |= IB_DEVICE_MANAGED_FLOW_STEERING;
891
Noa Osherovichb1383aa2017-10-29 13:59:45 +0200892 if (MLX5_CAP_GEN(mdev, end_pad))
893 props->device_cap_flags |= IB_DEVICE_PCI_WRITE_END_PADDING;
894
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300895 props->vendor_part_id = mdev->pdev->device;
896 props->hw_ver = mdev->pdev->revision;
Eli Cohene126ba92013-07-07 17:25:49 +0300897
898 props->max_mr_size = ~0ull;
Sagi Grimberge0238a62015-07-21 14:40:12 +0300899 props->page_size_cap = ~(min_page_size - 1);
Saeed Mahameed938fe832015-05-28 22:28:41 +0300900 props->max_qp = 1 << MLX5_CAP_GEN(mdev, log_max_qp);
901 props->max_qp_wr = 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
902 max_rq_sg = MLX5_CAP_GEN(mdev, max_wqe_sz_rq) /
903 sizeof(struct mlx5_wqe_data_seg);
Eli Cohen288c01b2016-10-27 16:36:45 +0300904 max_sq_desc = min_t(int, MLX5_CAP_GEN(mdev, max_wqe_sz_sq), 512);
905 max_sq_sg = (max_sq_desc - sizeof(struct mlx5_wqe_ctrl_seg) -
906 sizeof(struct mlx5_wqe_raddr_seg)) /
907 sizeof(struct mlx5_wqe_data_seg);
Eli Cohene126ba92013-07-07 17:25:49 +0300908 props->max_sge = min(max_rq_sg, max_sq_sg);
Sagi Grimberg986ef952016-03-31 19:03:25 +0300909 props->max_sge_rd = MLX5_MAX_SGE_RD;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300910 props->max_cq = 1 << MLX5_CAP_GEN(mdev, log_max_cq);
Leon Romanovsky9f177682016-01-14 08:11:40 +0200911 props->max_cqe = (1 << MLX5_CAP_GEN(mdev, log_max_cq_sz)) - 1;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300912 props->max_mr = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
913 props->max_pd = 1 << MLX5_CAP_GEN(mdev, log_max_pd);
914 props->max_qp_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_req_qp);
915 props->max_qp_init_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_res_qp);
916 props->max_srq = 1 << MLX5_CAP_GEN(mdev, log_max_srq);
917 props->max_srq_wr = (1 << MLX5_CAP_GEN(mdev, log_max_srq_sz)) - 1;
918 props->local_ca_ack_delay = MLX5_CAP_GEN(mdev, local_ca_ack_delay);
Eli Cohene126ba92013-07-07 17:25:49 +0300919 props->max_res_rd_atom = props->max_qp_rd_atom * props->max_qp;
Eli Cohene126ba92013-07-07 17:25:49 +0300920 props->max_srq_sge = max_rq_sg - 1;
Sagi Grimberg911f4332016-03-03 13:37:51 +0200921 props->max_fast_reg_page_list_len =
922 1 << MLX5_CAP_GEN(mdev, log_max_klm_list_size);
Moni Shoua776a3902018-01-02 16:19:33 +0200923 get_atomic_caps_qp(dev, props);
Eli Cohen81bea282013-09-11 16:35:30 +0300924 props->masked_atomic_cap = IB_ATOMIC_NONE;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300925 props->max_mcast_grp = 1 << MLX5_CAP_GEN(mdev, log_max_mcg);
926 props->max_mcast_qp_attach = MLX5_CAP_GEN(mdev, max_qp_mcg);
Eli Cohene126ba92013-07-07 17:25:49 +0300927 props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
928 props->max_mcast_grp;
929 props->max_map_per_fmr = INT_MAX; /* no limit in ConnectIB */
Maor Gottlieb86695a62016-10-27 16:36:38 +0300930 props->max_ah = INT_MAX;
Matan Barak7c60bcb2015-12-15 20:30:11 +0200931 props->hca_core_clock = MLX5_CAP_GEN(mdev, device_frequency_khz);
932 props->timestamp_mask = 0x7FFFFFFFFFFFFFFFULL;
Eli Cohene126ba92013-07-07 17:25:49 +0300933
Haggai Eran8cdd3122014-12-11 17:04:20 +0200934#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
Saeed Mahameed938fe832015-05-28 22:28:41 +0300935 if (MLX5_CAP_GEN(mdev, pg))
Haggai Eran8cdd3122014-12-11 17:04:20 +0200936 props->device_cap_flags |= IB_DEVICE_ON_DEMAND_PAGING;
937 props->odp_caps = dev->odp_caps;
938#endif
939
Leon Romanovsky051f2632015-12-20 12:16:11 +0200940 if (MLX5_CAP_GEN(mdev, cd))
941 props->device_cap_flags |= IB_DEVICE_CROSS_CHANNEL;
942
Eli Coheneff901d2016-03-11 22:58:42 +0200943 if (!mlx5_core_is_pf(mdev))
944 props->device_cap_flags |= IB_DEVICE_VIRTUAL_FUNCTION;
945
Yishai Hadas31f69a82016-08-28 11:28:45 +0300946 if (mlx5_ib_port_link_layer(ibdev, 1) ==
Daniel Jurgens85c7c012018-01-04 17:25:43 +0200947 IB_LINK_LAYER_ETHERNET && raw_support) {
Yishai Hadas31f69a82016-08-28 11:28:45 +0300948 props->rss_caps.max_rwq_indirection_tables =
949 1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt);
950 props->rss_caps.max_rwq_indirection_table_size =
951 1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt_size);
952 props->rss_caps.supported_qpts = 1 << IB_QPT_RAW_PACKET;
953 props->max_wq_type_rq =
954 1 << MLX5_CAP_GEN(dev->mdev, log_max_rq);
955 }
956
Artemy Kovalyoveb761892017-08-17 15:52:09 +0300957 if (MLX5_CAP_GEN(mdev, tag_matching)) {
Leon Romanovsky78b1beb2017-09-24 21:46:29 +0300958 props->tm_caps.max_rndv_hdr_size = MLX5_TM_MAX_RNDV_MSG_SIZE;
959 props->tm_caps.max_num_tags =
Artemy Kovalyoveb761892017-08-17 15:52:09 +0300960 (1 << MLX5_CAP_GEN(mdev, log_tag_matching_list_sz)) - 1;
Leon Romanovsky78b1beb2017-09-24 21:46:29 +0300961 props->tm_caps.flags = IB_TM_CAP_RC;
962 props->tm_caps.max_ops =
Artemy Kovalyoveb761892017-08-17 15:52:09 +0300963 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
Leon Romanovsky78b1beb2017-09-24 21:46:29 +0300964 props->tm_caps.max_sge = MLX5_TM_MAX_SGE;
Artemy Kovalyoveb761892017-08-17 15:52:09 +0300965 }
966
Yonatan Cohen87ab3f52017-11-13 10:51:18 +0200967 if (MLX5_CAP_GEN(dev->mdev, cq_moderation)) {
968 props->cq_caps.max_cq_moderation_count =
969 MLX5_MAX_CQ_COUNT;
970 props->cq_caps.max_cq_moderation_period =
971 MLX5_MAX_CQ_PERIOD;
972 }
973
Bodong Wang7e43a2a2016-10-31 12:16:44 +0200974 if (field_avail(typeof(resp), cqe_comp_caps, uhw->outlen)) {
975 resp.cqe_comp_caps.max_num =
976 MLX5_CAP_GEN(dev->mdev, cqe_compression) ?
977 MLX5_CAP_GEN(dev->mdev, cqe_compression_max_num) : 0;
978 resp.cqe_comp_caps.supported_format =
979 MLX5_IB_CQE_RES_FORMAT_HASH |
980 MLX5_IB_CQE_RES_FORMAT_CSUM;
981 resp.response_length += sizeof(resp.cqe_comp_caps);
982 }
983
Daniel Jurgens85c7c012018-01-04 17:25:43 +0200984 if (field_avail(typeof(resp), packet_pacing_caps, uhw->outlen) &&
985 raw_support) {
Bodong Wangd9491672016-12-01 13:43:13 +0200986 if (MLX5_CAP_QOS(mdev, packet_pacing) &&
987 MLX5_CAP_GEN(mdev, qos)) {
988 resp.packet_pacing_caps.qp_rate_limit_max =
989 MLX5_CAP_QOS(mdev, packet_pacing_max_rate);
990 resp.packet_pacing_caps.qp_rate_limit_min =
991 MLX5_CAP_QOS(mdev, packet_pacing_min_rate);
992 resp.packet_pacing_caps.supported_qpts |=
993 1 << IB_QPT_RAW_PACKET;
Bodong Wang61147f32018-03-19 15:10:30 +0200994 if (MLX5_CAP_QOS(mdev, packet_pacing_burst_bound) &&
995 MLX5_CAP_QOS(mdev, packet_pacing_typical_size))
996 resp.packet_pacing_caps.cap_flags |=
997 MLX5_IB_PP_SUPPORT_BURST;
Bodong Wangd9491672016-12-01 13:43:13 +0200998 }
999 resp.response_length += sizeof(resp.packet_pacing_caps);
1000 }
1001
Leon Romanovsky9f885202017-01-02 11:37:39 +02001002 if (field_avail(typeof(resp), mlx5_ib_support_multi_pkt_send_wqes,
1003 uhw->outlen)) {
Bodong Wang795b6092017-08-17 15:52:34 +03001004 if (MLX5_CAP_ETH(mdev, multi_pkt_send_wqe))
1005 resp.mlx5_ib_support_multi_pkt_send_wqes =
1006 MLX5_IB_ALLOW_MPW;
Bodong Wang050da902017-08-17 15:52:35 +03001007
1008 if (MLX5_CAP_ETH(mdev, enhanced_multi_pkt_send_wqe))
1009 resp.mlx5_ib_support_multi_pkt_send_wqes |=
1010 MLX5_IB_SUPPORT_EMPW;
1011
Leon Romanovsky9f885202017-01-02 11:37:39 +02001012 resp.response_length +=
1013 sizeof(resp.mlx5_ib_support_multi_pkt_send_wqes);
1014 }
1015
Guy Levide57f2a2017-10-19 08:25:52 +03001016 if (field_avail(typeof(resp), flags, uhw->outlen)) {
1017 resp.response_length += sizeof(resp.flags);
Guy Levi7a0c8f42017-10-19 08:25:53 +03001018
Guy Levide57f2a2017-10-19 08:25:52 +03001019 if (MLX5_CAP_GEN(mdev, cqe_compression_128))
1020 resp.flags |=
1021 MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_COMP;
Guy Levi7a0c8f42017-10-19 08:25:53 +03001022
1023 if (MLX5_CAP_GEN(mdev, cqe_128_always))
1024 resp.flags |= MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_PAD;
Guy Levide57f2a2017-10-19 08:25:52 +03001025 }
Leon Romanovsky9f885202017-01-02 11:37:39 +02001026
Noa Osherovich96dc3fc2017-08-17 15:52:28 +03001027 if (field_avail(typeof(resp), sw_parsing_caps,
1028 uhw->outlen)) {
1029 resp.response_length += sizeof(resp.sw_parsing_caps);
1030 if (MLX5_CAP_ETH(mdev, swp)) {
1031 resp.sw_parsing_caps.sw_parsing_offloads |=
1032 MLX5_IB_SW_PARSING;
1033
1034 if (MLX5_CAP_ETH(mdev, swp_csum))
1035 resp.sw_parsing_caps.sw_parsing_offloads |=
1036 MLX5_IB_SW_PARSING_CSUM;
1037
1038 if (MLX5_CAP_ETH(mdev, swp_lso))
1039 resp.sw_parsing_caps.sw_parsing_offloads |=
1040 MLX5_IB_SW_PARSING_LSO;
1041
1042 if (resp.sw_parsing_caps.sw_parsing_offloads)
1043 resp.sw_parsing_caps.supported_qpts =
1044 BIT(IB_QPT_RAW_PACKET);
1045 }
1046 }
1047
Daniel Jurgens85c7c012018-01-04 17:25:43 +02001048 if (field_avail(typeof(resp), striding_rq_caps, uhw->outlen) &&
1049 raw_support) {
Noa Osherovichb4f34592017-10-17 18:01:12 +03001050 resp.response_length += sizeof(resp.striding_rq_caps);
1051 if (MLX5_CAP_GEN(mdev, striding_rq)) {
1052 resp.striding_rq_caps.min_single_stride_log_num_of_bytes =
1053 MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES;
1054 resp.striding_rq_caps.max_single_stride_log_num_of_bytes =
1055 MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES;
1056 resp.striding_rq_caps.min_single_wqe_log_num_of_strides =
1057 MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES;
1058 resp.striding_rq_caps.max_single_wqe_log_num_of_strides =
1059 MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES;
1060 resp.striding_rq_caps.supported_qpts =
1061 BIT(IB_QPT_RAW_PACKET);
1062 }
1063 }
1064
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001065 if (field_avail(typeof(resp), tunnel_offloads_caps,
1066 uhw->outlen)) {
1067 resp.response_length += sizeof(resp.tunnel_offloads_caps);
1068 if (MLX5_CAP_ETH(mdev, tunnel_stateless_vxlan))
1069 resp.tunnel_offloads_caps |=
1070 MLX5_IB_TUNNELED_OFFLOADS_VXLAN;
1071 if (MLX5_CAP_ETH(mdev, tunnel_stateless_geneve_rx))
1072 resp.tunnel_offloads_caps |=
1073 MLX5_IB_TUNNELED_OFFLOADS_GENEVE;
1074 if (MLX5_CAP_ETH(mdev, tunnel_stateless_gre))
1075 resp.tunnel_offloads_caps |=
1076 MLX5_IB_TUNNELED_OFFLOADS_GRE;
1077 }
1078
Bodong Wang402ca532016-06-17 15:02:20 +03001079 if (uhw->outlen) {
1080 err = ib_copy_to_udata(uhw, &resp, resp.response_length);
1081
1082 if (err)
1083 return err;
1084 }
1085
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001086 return 0;
1087}
Eli Cohene126ba92013-07-07 17:25:49 +03001088
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001089enum mlx5_ib_width {
1090 MLX5_IB_WIDTH_1X = 1 << 0,
1091 MLX5_IB_WIDTH_2X = 1 << 1,
1092 MLX5_IB_WIDTH_4X = 1 << 2,
1093 MLX5_IB_WIDTH_8X = 1 << 3,
1094 MLX5_IB_WIDTH_12X = 1 << 4
1095};
1096
1097static int translate_active_width(struct ib_device *ibdev, u8 active_width,
1098 u8 *ib_width)
1099{
1100 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1101 int err = 0;
1102
1103 if (active_width & MLX5_IB_WIDTH_1X) {
1104 *ib_width = IB_WIDTH_1X;
1105 } else if (active_width & MLX5_IB_WIDTH_2X) {
1106 mlx5_ib_dbg(dev, "active_width %d is not supported by IB spec\n",
1107 (int)active_width);
1108 err = -EINVAL;
1109 } else if (active_width & MLX5_IB_WIDTH_4X) {
1110 *ib_width = IB_WIDTH_4X;
1111 } else if (active_width & MLX5_IB_WIDTH_8X) {
1112 *ib_width = IB_WIDTH_8X;
1113 } else if (active_width & MLX5_IB_WIDTH_12X) {
1114 *ib_width = IB_WIDTH_12X;
1115 } else {
1116 mlx5_ib_dbg(dev, "Invalid active_width %d\n",
1117 (int)active_width);
1118 err = -EINVAL;
1119 }
1120
1121 return err;
1122}
1123
1124static int mlx5_mtu_to_ib_mtu(int mtu)
1125{
1126 switch (mtu) {
1127 case 256: return 1;
1128 case 512: return 2;
1129 case 1024: return 3;
1130 case 2048: return 4;
1131 case 4096: return 5;
1132 default:
1133 pr_warn("invalid mtu\n");
1134 return -1;
1135 }
1136}
1137
1138enum ib_max_vl_num {
1139 __IB_MAX_VL_0 = 1,
1140 __IB_MAX_VL_0_1 = 2,
1141 __IB_MAX_VL_0_3 = 3,
1142 __IB_MAX_VL_0_7 = 4,
1143 __IB_MAX_VL_0_14 = 5,
1144};
1145
1146enum mlx5_vl_hw_cap {
1147 MLX5_VL_HW_0 = 1,
1148 MLX5_VL_HW_0_1 = 2,
1149 MLX5_VL_HW_0_2 = 3,
1150 MLX5_VL_HW_0_3 = 4,
1151 MLX5_VL_HW_0_4 = 5,
1152 MLX5_VL_HW_0_5 = 6,
1153 MLX5_VL_HW_0_6 = 7,
1154 MLX5_VL_HW_0_7 = 8,
1155 MLX5_VL_HW_0_14 = 15
1156};
1157
1158static int translate_max_vl_num(struct ib_device *ibdev, u8 vl_hw_cap,
1159 u8 *max_vl_num)
1160{
1161 switch (vl_hw_cap) {
1162 case MLX5_VL_HW_0:
1163 *max_vl_num = __IB_MAX_VL_0;
1164 break;
1165 case MLX5_VL_HW_0_1:
1166 *max_vl_num = __IB_MAX_VL_0_1;
1167 break;
1168 case MLX5_VL_HW_0_3:
1169 *max_vl_num = __IB_MAX_VL_0_3;
1170 break;
1171 case MLX5_VL_HW_0_7:
1172 *max_vl_num = __IB_MAX_VL_0_7;
1173 break;
1174 case MLX5_VL_HW_0_14:
1175 *max_vl_num = __IB_MAX_VL_0_14;
1176 break;
1177
1178 default:
1179 return -EINVAL;
1180 }
1181
1182 return 0;
1183}
1184
1185static int mlx5_query_hca_port(struct ib_device *ibdev, u8 port,
1186 struct ib_port_attr *props)
1187{
1188 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1189 struct mlx5_core_dev *mdev = dev->mdev;
1190 struct mlx5_hca_vport_context *rep;
Saeed Mahameed046339e2016-04-22 00:33:03 +03001191 u16 max_mtu;
1192 u16 oper_mtu;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001193 int err;
1194 u8 ib_link_width_oper;
1195 u8 vl_hw_cap;
1196
1197 rep = kzalloc(sizeof(*rep), GFP_KERNEL);
1198 if (!rep) {
1199 err = -ENOMEM;
1200 goto out;
1201 }
1202
Or Gerlitzc4550c62017-01-24 13:02:39 +02001203 /* props being zeroed by the caller, avoid zeroing it here */
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001204
1205 err = mlx5_query_hca_vport_context(mdev, 0, port, 0, rep);
1206 if (err)
1207 goto out;
1208
1209 props->lid = rep->lid;
1210 props->lmc = rep->lmc;
1211 props->sm_lid = rep->sm_lid;
1212 props->sm_sl = rep->sm_sl;
1213 props->state = rep->vport_state;
1214 props->phys_state = rep->port_physical_state;
1215 props->port_cap_flags = rep->cap_mask1;
1216 props->gid_tbl_len = mlx5_get_gid_table_len(MLX5_CAP_GEN(mdev, gid_table_size));
1217 props->max_msg_sz = 1 << MLX5_CAP_GEN(mdev, log_max_msg);
1218 props->pkey_tbl_len = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, pkey_table_size));
1219 props->bad_pkey_cntr = rep->pkey_violation_counter;
1220 props->qkey_viol_cntr = rep->qkey_violation_counter;
1221 props->subnet_timeout = rep->subnet_timeout;
1222 props->init_type_reply = rep->init_type_reply;
Eli Coheneff901d2016-03-11 22:58:42 +02001223 props->grh_required = rep->grh_required;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001224
1225 err = mlx5_query_port_link_width_oper(mdev, &ib_link_width_oper, port);
1226 if (err)
1227 goto out;
1228
1229 err = translate_active_width(ibdev, ib_link_width_oper,
1230 &props->active_width);
1231 if (err)
1232 goto out;
Noa Osherovichd5beb7f2016-06-02 10:47:53 +03001233 err = mlx5_query_port_ib_proto_oper(mdev, &props->active_speed, port);
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001234 if (err)
1235 goto out;
1236
Saeed Mahameedfacc9692015-06-11 14:47:27 +03001237 mlx5_query_port_max_mtu(mdev, &max_mtu, port);
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001238
1239 props->max_mtu = mlx5_mtu_to_ib_mtu(max_mtu);
1240
Saeed Mahameedfacc9692015-06-11 14:47:27 +03001241 mlx5_query_port_oper_mtu(mdev, &oper_mtu, port);
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001242
1243 props->active_mtu = mlx5_mtu_to_ib_mtu(oper_mtu);
1244
1245 err = mlx5_query_port_vl_hw_cap(mdev, &vl_hw_cap, port);
1246 if (err)
1247 goto out;
1248
1249 err = translate_max_vl_num(ibdev, vl_hw_cap,
1250 &props->max_vl_num);
1251out:
1252 kfree(rep);
Eli Cohene126ba92013-07-07 17:25:49 +03001253 return err;
1254}
1255
1256int mlx5_ib_query_port(struct ib_device *ibdev, u8 port,
1257 struct ib_port_attr *props)
1258{
Ilan Tayari095b0922017-05-14 16:04:30 +03001259 unsigned int count;
1260 int ret;
1261
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001262 switch (mlx5_get_vport_access_method(ibdev)) {
1263 case MLX5_VPORT_ACCESS_METHOD_MAD:
Ilan Tayari095b0922017-05-14 16:04:30 +03001264 ret = mlx5_query_mad_ifc_port(ibdev, port, props);
1265 break;
Eli Cohene126ba92013-07-07 17:25:49 +03001266
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001267 case MLX5_VPORT_ACCESS_METHOD_HCA:
Ilan Tayari095b0922017-05-14 16:04:30 +03001268 ret = mlx5_query_hca_port(ibdev, port, props);
1269 break;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001270
Achiad Shochat3f89a642015-12-23 18:47:21 +02001271 case MLX5_VPORT_ACCESS_METHOD_NIC:
Ilan Tayari095b0922017-05-14 16:04:30 +03001272 ret = mlx5_query_port_roce(ibdev, port, props);
1273 break;
Achiad Shochat3f89a642015-12-23 18:47:21 +02001274
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001275 default:
Ilan Tayari095b0922017-05-14 16:04:30 +03001276 ret = -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +03001277 }
Ilan Tayari095b0922017-05-14 16:04:30 +03001278
1279 if (!ret && props) {
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001280 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1281 struct mlx5_core_dev *mdev;
1282 bool put_mdev = true;
1283
1284 mdev = mlx5_ib_get_native_port_mdev(dev, port, NULL);
1285 if (!mdev) {
1286 /* If the port isn't affiliated yet query the master.
1287 * The master and slave will have the same values.
1288 */
1289 mdev = dev->mdev;
1290 port = 1;
1291 put_mdev = false;
1292 }
1293 count = mlx5_core_reserved_gids_count(mdev);
1294 if (put_mdev)
1295 mlx5_ib_put_native_port_mdev(dev, port);
Ilan Tayari095b0922017-05-14 16:04:30 +03001296 props->gid_tbl_len -= count;
1297 }
1298 return ret;
Eli Cohene126ba92013-07-07 17:25:49 +03001299}
1300
Mark Bloch8e6efa32017-11-06 12:22:13 +00001301static int mlx5_ib_rep_query_port(struct ib_device *ibdev, u8 port,
1302 struct ib_port_attr *props)
1303{
1304 int ret;
1305
1306 /* Only link layer == ethernet is valid for representors */
1307 ret = mlx5_query_port_roce(ibdev, port, props);
1308 if (ret || !props)
1309 return ret;
1310
1311 /* We don't support GIDS */
1312 props->gid_tbl_len = 0;
1313
1314 return ret;
1315}
1316
Eli Cohene126ba92013-07-07 17:25:49 +03001317static int mlx5_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
1318 union ib_gid *gid)
1319{
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001320 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1321 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03001322
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001323 switch (mlx5_get_vport_access_method(ibdev)) {
1324 case MLX5_VPORT_ACCESS_METHOD_MAD:
1325 return mlx5_query_mad_ifc_gids(ibdev, port, index, gid);
Eli Cohene126ba92013-07-07 17:25:49 +03001326
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001327 case MLX5_VPORT_ACCESS_METHOD_HCA:
1328 return mlx5_query_hca_vport_gid(mdev, 0, port, 0, index, gid);
Eli Cohene126ba92013-07-07 17:25:49 +03001329
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001330 default:
1331 return -EINVAL;
1332 }
Eli Cohene126ba92013-07-07 17:25:49 +03001333
Eli Cohene126ba92013-07-07 17:25:49 +03001334}
1335
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001336static int mlx5_query_hca_nic_pkey(struct ib_device *ibdev, u8 port,
1337 u16 index, u16 *pkey)
1338{
1339 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1340 struct mlx5_core_dev *mdev;
1341 bool put_mdev = true;
1342 u8 mdev_port_num;
1343 int err;
1344
1345 mdev = mlx5_ib_get_native_port_mdev(dev, port, &mdev_port_num);
1346 if (!mdev) {
1347 /* The port isn't affiliated yet, get the PKey from the master
1348 * port. For RoCE the PKey tables will be the same.
1349 */
1350 put_mdev = false;
1351 mdev = dev->mdev;
1352 mdev_port_num = 1;
1353 }
1354
1355 err = mlx5_query_hca_vport_pkey(mdev, 0, mdev_port_num, 0,
1356 index, pkey);
1357 if (put_mdev)
1358 mlx5_ib_put_native_port_mdev(dev, port);
1359
1360 return err;
1361}
1362
Eli Cohene126ba92013-07-07 17:25:49 +03001363static int mlx5_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
1364 u16 *pkey)
1365{
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001366 switch (mlx5_get_vport_access_method(ibdev)) {
1367 case MLX5_VPORT_ACCESS_METHOD_MAD:
1368 return mlx5_query_mad_ifc_pkey(ibdev, port, index, pkey);
Eli Cohene126ba92013-07-07 17:25:49 +03001369
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001370 case MLX5_VPORT_ACCESS_METHOD_HCA:
1371 case MLX5_VPORT_ACCESS_METHOD_NIC:
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001372 return mlx5_query_hca_nic_pkey(ibdev, port, index, pkey);
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001373 default:
1374 return -EINVAL;
1375 }
Eli Cohene126ba92013-07-07 17:25:49 +03001376}
1377
Eli Cohene126ba92013-07-07 17:25:49 +03001378static int mlx5_ib_modify_device(struct ib_device *ibdev, int mask,
1379 struct ib_device_modify *props)
1380{
1381 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1382 struct mlx5_reg_node_desc in;
1383 struct mlx5_reg_node_desc out;
1384 int err;
1385
1386 if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
1387 return -EOPNOTSUPP;
1388
1389 if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
1390 return 0;
1391
1392 /*
1393 * If possible, pass node desc to FW, so it can generate
1394 * a 144 trap. If cmd fails, just ignore.
1395 */
Yuval Shaiabd99fde2016-08-25 10:57:07 -07001396 memcpy(&in, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
Jack Morgenstein9603b612014-07-28 23:30:22 +03001397 err = mlx5_core_access_reg(dev->mdev, &in, sizeof(in), &out,
Eli Cohene126ba92013-07-07 17:25:49 +03001398 sizeof(out), MLX5_REG_NODE_DESC, 0, 1);
1399 if (err)
1400 return err;
1401
Yuval Shaiabd99fde2016-08-25 10:57:07 -07001402 memcpy(ibdev->node_desc, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
Eli Cohene126ba92013-07-07 17:25:49 +03001403
1404 return err;
1405}
1406
Eli Cohencdbe33d2017-02-14 07:25:38 +02001407static int set_port_caps_atomic(struct mlx5_ib_dev *dev, u8 port_num, u32 mask,
1408 u32 value)
1409{
1410 struct mlx5_hca_vport_context ctx = {};
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001411 struct mlx5_core_dev *mdev;
1412 u8 mdev_port_num;
Eli Cohencdbe33d2017-02-14 07:25:38 +02001413 int err;
1414
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001415 mdev = mlx5_ib_get_native_port_mdev(dev, port_num, &mdev_port_num);
1416 if (!mdev)
1417 return -ENODEV;
1418
1419 err = mlx5_query_hca_vport_context(mdev, 0, mdev_port_num, 0, &ctx);
Eli Cohencdbe33d2017-02-14 07:25:38 +02001420 if (err)
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001421 goto out;
Eli Cohencdbe33d2017-02-14 07:25:38 +02001422
1423 if (~ctx.cap_mask1_perm & mask) {
1424 mlx5_ib_warn(dev, "trying to change bitmask 0x%X but change supported 0x%X\n",
1425 mask, ctx.cap_mask1_perm);
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001426 err = -EINVAL;
1427 goto out;
Eli Cohencdbe33d2017-02-14 07:25:38 +02001428 }
1429
1430 ctx.cap_mask1 = value;
1431 ctx.cap_mask1_perm = mask;
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001432 err = mlx5_core_modify_hca_vport_context(mdev, 0, mdev_port_num,
1433 0, &ctx);
1434
1435out:
1436 mlx5_ib_put_native_port_mdev(dev, port_num);
Eli Cohencdbe33d2017-02-14 07:25:38 +02001437
1438 return err;
1439}
1440
Eli Cohene126ba92013-07-07 17:25:49 +03001441static int mlx5_ib_modify_port(struct ib_device *ibdev, u8 port, int mask,
1442 struct ib_port_modify *props)
1443{
1444 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1445 struct ib_port_attr attr;
1446 u32 tmp;
1447 int err;
Eli Cohencdbe33d2017-02-14 07:25:38 +02001448 u32 change_mask;
1449 u32 value;
1450 bool is_ib = (mlx5_ib_port_link_layer(ibdev, port) ==
1451 IB_LINK_LAYER_INFINIBAND);
1452
Majd Dibbinyec255872017-08-23 08:35:42 +03001453 /* CM layer calls ib_modify_port() regardless of the link layer. For
1454 * Ethernet ports, qkey violation and Port capabilities are meaningless.
1455 */
1456 if (!is_ib)
1457 return 0;
1458
Eli Cohencdbe33d2017-02-14 07:25:38 +02001459 if (MLX5_CAP_GEN(dev->mdev, ib_virt) && is_ib) {
1460 change_mask = props->clr_port_cap_mask | props->set_port_cap_mask;
1461 value = ~props->clr_port_cap_mask | props->set_port_cap_mask;
1462 return set_port_caps_atomic(dev, port, change_mask, value);
1463 }
Eli Cohene126ba92013-07-07 17:25:49 +03001464
1465 mutex_lock(&dev->cap_mask_mutex);
1466
Or Gerlitzc4550c62017-01-24 13:02:39 +02001467 err = ib_query_port(ibdev, port, &attr);
Eli Cohene126ba92013-07-07 17:25:49 +03001468 if (err)
1469 goto out;
1470
1471 tmp = (attr.port_cap_flags | props->set_port_cap_mask) &
1472 ~props->clr_port_cap_mask;
1473
Jack Morgenstein9603b612014-07-28 23:30:22 +03001474 err = mlx5_set_port_caps(dev->mdev, port, tmp);
Eli Cohene126ba92013-07-07 17:25:49 +03001475
1476out:
1477 mutex_unlock(&dev->cap_mask_mutex);
1478 return err;
1479}
1480
Eli Cohen30aa60b2017-01-03 23:55:27 +02001481static void print_lib_caps(struct mlx5_ib_dev *dev, u64 caps)
1482{
1483 mlx5_ib_dbg(dev, "MLX5_LIB_CAP_4K_UAR = %s\n",
1484 caps & MLX5_LIB_CAP_4K_UAR ? "y" : "n");
1485}
1486
Yishai Hadas31a78a52017-12-24 16:31:34 +02001487static u16 calc_dynamic_bfregs(int uars_per_sys_page)
1488{
1489 /* Large page with non 4k uar support might limit the dynamic size */
1490 if (uars_per_sys_page == 1 && PAGE_SIZE > 4096)
1491 return MLX5_MIN_DYN_BFREGS;
1492
1493 return MLX5_MAX_DYN_BFREGS;
1494}
1495
Eli Cohenb037c292017-01-03 23:55:26 +02001496static int calc_total_bfregs(struct mlx5_ib_dev *dev, bool lib_uar_4k,
1497 struct mlx5_ib_alloc_ucontext_req_v2 *req,
Yishai Hadas31a78a52017-12-24 16:31:34 +02001498 struct mlx5_bfreg_info *bfregi)
Eli Cohenb037c292017-01-03 23:55:26 +02001499{
1500 int uars_per_sys_page;
1501 int bfregs_per_sys_page;
1502 int ref_bfregs = req->total_num_bfregs;
1503
1504 if (req->total_num_bfregs == 0)
1505 return -EINVAL;
1506
1507 BUILD_BUG_ON(MLX5_MAX_BFREGS % MLX5_NON_FP_BFREGS_IN_PAGE);
1508 BUILD_BUG_ON(MLX5_MAX_BFREGS < MLX5_NON_FP_BFREGS_IN_PAGE);
1509
1510 if (req->total_num_bfregs > MLX5_MAX_BFREGS)
1511 return -ENOMEM;
1512
1513 uars_per_sys_page = get_uars_per_sys_page(dev, lib_uar_4k);
1514 bfregs_per_sys_page = uars_per_sys_page * MLX5_NON_FP_BFREGS_PER_UAR;
Yishai Hadas31a78a52017-12-24 16:31:34 +02001515 /* This holds the required static allocation asked by the user */
Eli Cohenb037c292017-01-03 23:55:26 +02001516 req->total_num_bfregs = ALIGN(req->total_num_bfregs, bfregs_per_sys_page);
Eli Cohenb037c292017-01-03 23:55:26 +02001517 if (req->num_low_latency_bfregs > req->total_num_bfregs - 1)
1518 return -EINVAL;
1519
Yishai Hadas31a78a52017-12-24 16:31:34 +02001520 bfregi->num_static_sys_pages = req->total_num_bfregs / bfregs_per_sys_page;
1521 bfregi->num_dyn_bfregs = ALIGN(calc_dynamic_bfregs(uars_per_sys_page), bfregs_per_sys_page);
1522 bfregi->total_num_bfregs = req->total_num_bfregs + bfregi->num_dyn_bfregs;
1523 bfregi->num_sys_pages = bfregi->total_num_bfregs / bfregs_per_sys_page;
1524
1525 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 +02001526 MLX5_CAP_GEN(dev->mdev, uar_4k) ? "yes" : "no",
1527 lib_uar_4k ? "yes" : "no", ref_bfregs,
Yishai Hadas31a78a52017-12-24 16:31:34 +02001528 req->total_num_bfregs, bfregi->total_num_bfregs,
1529 bfregi->num_sys_pages);
Eli Cohenb037c292017-01-03 23:55:26 +02001530
1531 return 0;
1532}
1533
1534static int allocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
1535{
1536 struct mlx5_bfreg_info *bfregi;
1537 int err;
1538 int i;
1539
1540 bfregi = &context->bfregi;
Yishai Hadas31a78a52017-12-24 16:31:34 +02001541 for (i = 0; i < bfregi->num_static_sys_pages; i++) {
Eli Cohenb037c292017-01-03 23:55:26 +02001542 err = mlx5_cmd_alloc_uar(dev->mdev, &bfregi->sys_pages[i]);
1543 if (err)
1544 goto error;
1545
1546 mlx5_ib_dbg(dev, "allocated uar %d\n", bfregi->sys_pages[i]);
1547 }
Yishai Hadas4ed131d2017-12-24 16:31:35 +02001548
1549 for (i = bfregi->num_static_sys_pages; i < bfregi->num_sys_pages; i++)
1550 bfregi->sys_pages[i] = MLX5_IB_INVALID_UAR_INDEX;
1551
Eli Cohenb037c292017-01-03 23:55:26 +02001552 return 0;
1553
1554error:
1555 for (--i; i >= 0; i--)
1556 if (mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]))
1557 mlx5_ib_warn(dev, "failed to free uar %d\n", i);
1558
1559 return err;
1560}
1561
1562static int deallocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
1563{
1564 struct mlx5_bfreg_info *bfregi;
1565 int err;
1566 int i;
1567
1568 bfregi = &context->bfregi;
Yishai Hadas4ed131d2017-12-24 16:31:35 +02001569 for (i = 0; i < bfregi->num_sys_pages; i++) {
1570 if (i < bfregi->num_static_sys_pages ||
1571 bfregi->sys_pages[i] != MLX5_IB_INVALID_UAR_INDEX) {
1572 err = mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]);
1573 if (err) {
1574 mlx5_ib_warn(dev, "failed to free uar %d, err=%d\n", i, err);
1575 return err;
1576 }
Eli Cohenb037c292017-01-03 23:55:26 +02001577 }
1578 }
Yishai Hadas4ed131d2017-12-24 16:31:35 +02001579
Eli Cohenb037c292017-01-03 23:55:26 +02001580 return 0;
1581}
1582
Huy Nguyenc85023e2017-05-30 09:42:54 +03001583static int mlx5_ib_alloc_transport_domain(struct mlx5_ib_dev *dev, u32 *tdn)
1584{
1585 int err;
1586
1587 err = mlx5_core_alloc_transport_domain(dev->mdev, tdn);
1588 if (err)
1589 return err;
1590
1591 if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
Eran Ben Elisha8978cc92018-01-09 11:41:10 +02001592 (!MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) &&
1593 !MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
Huy Nguyenc85023e2017-05-30 09:42:54 +03001594 return err;
1595
1596 mutex_lock(&dev->lb_mutex);
1597 dev->user_td++;
1598
1599 if (dev->user_td == 2)
1600 err = mlx5_nic_vport_update_local_lb(dev->mdev, true);
1601
1602 mutex_unlock(&dev->lb_mutex);
1603 return err;
1604}
1605
1606static void mlx5_ib_dealloc_transport_domain(struct mlx5_ib_dev *dev, u32 tdn)
1607{
1608 mlx5_core_dealloc_transport_domain(dev->mdev, tdn);
1609
1610 if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
Eran Ben Elisha8978cc92018-01-09 11:41:10 +02001611 (!MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) &&
1612 !MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
Huy Nguyenc85023e2017-05-30 09:42:54 +03001613 return;
1614
1615 mutex_lock(&dev->lb_mutex);
1616 dev->user_td--;
1617
1618 if (dev->user_td < 2)
1619 mlx5_nic_vport_update_local_lb(dev->mdev, false);
1620
1621 mutex_unlock(&dev->lb_mutex);
1622}
1623
Eli Cohene126ba92013-07-07 17:25:49 +03001624static struct ib_ucontext *mlx5_ib_alloc_ucontext(struct ib_device *ibdev,
1625 struct ib_udata *udata)
1626{
1627 struct mlx5_ib_dev *dev = to_mdev(ibdev);
Matan Barakb368d7c2015-12-15 20:30:12 +02001628 struct mlx5_ib_alloc_ucontext_req_v2 req = {};
1629 struct mlx5_ib_alloc_ucontext_resp resp = {};
Feras Daoud5c99eae2018-01-16 20:08:41 +02001630 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03001631 struct mlx5_ib_ucontext *context;
Eli Cohen2f5ff262017-01-03 23:55:21 +02001632 struct mlx5_bfreg_info *bfregi;
Eli Cohen78c0f982014-01-30 13:49:48 +02001633 int ver;
Eli Cohene126ba92013-07-07 17:25:49 +03001634 int err;
Majd Dibbinya168a41c2016-01-28 17:51:47 +02001635 size_t min_req_v2 = offsetof(struct mlx5_ib_alloc_ucontext_req_v2,
1636 max_cqe_version);
Eli Cohenb037c292017-01-03 23:55:26 +02001637 bool lib_uar_4k;
Eli Cohene126ba92013-07-07 17:25:49 +03001638
1639 if (!dev->ib_active)
1640 return ERR_PTR(-EAGAIN);
1641
Amrani, Rame0931112017-06-27 17:04:42 +03001642 if (udata->inlen == sizeof(struct mlx5_ib_alloc_ucontext_req))
Eli Cohen78c0f982014-01-30 13:49:48 +02001643 ver = 0;
Amrani, Rame0931112017-06-27 17:04:42 +03001644 else if (udata->inlen >= min_req_v2)
Eli Cohen78c0f982014-01-30 13:49:48 +02001645 ver = 2;
1646 else
1647 return ERR_PTR(-EINVAL);
1648
Amrani, Rame0931112017-06-27 17:04:42 +03001649 err = ib_copy_from_udata(&req, udata, min(udata->inlen, sizeof(req)));
Eli Cohene126ba92013-07-07 17:25:49 +03001650 if (err)
1651 return ERR_PTR(err);
1652
Matan Barakb368d7c2015-12-15 20:30:12 +02001653 if (req.flags)
Eli Cohen78c0f982014-01-30 13:49:48 +02001654 return ERR_PTR(-EINVAL);
1655
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001656 if (req.comp_mask || req.reserved0 || req.reserved1 || req.reserved2)
Matan Barakb368d7c2015-12-15 20:30:12 +02001657 return ERR_PTR(-EOPNOTSUPP);
1658
Eli Cohen2f5ff262017-01-03 23:55:21 +02001659 req.total_num_bfregs = ALIGN(req.total_num_bfregs,
1660 MLX5_NON_FP_BFREGS_PER_UAR);
1661 if (req.num_low_latency_bfregs > req.total_num_bfregs - 1)
Eli Cohene126ba92013-07-07 17:25:49 +03001662 return ERR_PTR(-EINVAL);
1663
Saeed Mahameed938fe832015-05-28 22:28:41 +03001664 resp.qp_tab_size = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp);
Noa Osherovich2cc6ad52016-06-04 15:15:33 +03001665 if (mlx5_core_is_pf(dev->mdev) && MLX5_CAP_GEN(dev->mdev, bf))
1666 resp.bf_reg_size = 1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size);
Daniel Jurgensb47bd6e2016-10-25 18:36:24 +03001667 resp.cache_line_size = cache_line_size();
Saeed Mahameed938fe832015-05-28 22:28:41 +03001668 resp.max_sq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq);
1669 resp.max_rq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq);
1670 resp.max_send_wqebb = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1671 resp.max_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1672 resp.max_srq_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz);
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001673 resp.cqe_version = min_t(__u8,
1674 (__u8)MLX5_CAP_GEN(dev->mdev, cqe_version),
1675 req.max_cqe_version);
Eli Cohen30aa60b2017-01-03 23:55:27 +02001676 resp.log_uar_size = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
1677 MLX5_ADAPTER_PAGE_SHIFT : PAGE_SHIFT;
1678 resp.num_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
1679 MLX5_CAP_GEN(dev->mdev, num_of_uars_per_page) : 1;
Matan Barakb368d7c2015-12-15 20:30:12 +02001680 resp.response_length = min(offsetof(typeof(resp), response_length) +
1681 sizeof(resp.response_length), udata->outlen);
Eli Cohene126ba92013-07-07 17:25:49 +03001682
1683 context = kzalloc(sizeof(*context), GFP_KERNEL);
1684 if (!context)
1685 return ERR_PTR(-ENOMEM);
1686
Eli Cohen30aa60b2017-01-03 23:55:27 +02001687 lib_uar_4k = req.lib_caps & MLX5_LIB_CAP_4K_UAR;
Eli Cohen2f5ff262017-01-03 23:55:21 +02001688 bfregi = &context->bfregi;
Eli Cohenb037c292017-01-03 23:55:26 +02001689
1690 /* updates req->total_num_bfregs */
Yishai Hadas31a78a52017-12-24 16:31:34 +02001691 err = calc_total_bfregs(dev, lib_uar_4k, &req, bfregi);
Eli Cohenb037c292017-01-03 23:55:26 +02001692 if (err)
1693 goto out_ctx;
1694
Eli Cohen2f5ff262017-01-03 23:55:21 +02001695 mutex_init(&bfregi->lock);
Eli Cohenb037c292017-01-03 23:55:26 +02001696 bfregi->lib_uar_4k = lib_uar_4k;
Yishai Hadas31a78a52017-12-24 16:31:34 +02001697 bfregi->count = kcalloc(bfregi->total_num_bfregs, sizeof(*bfregi->count),
Eli Cohenb037c292017-01-03 23:55:26 +02001698 GFP_KERNEL);
1699 if (!bfregi->count) {
Eli Cohene126ba92013-07-07 17:25:49 +03001700 err = -ENOMEM;
1701 goto out_ctx;
1702 }
1703
Eli Cohenb037c292017-01-03 23:55:26 +02001704 bfregi->sys_pages = kcalloc(bfregi->num_sys_pages,
1705 sizeof(*bfregi->sys_pages),
1706 GFP_KERNEL);
1707 if (!bfregi->sys_pages) {
Eli Cohene126ba92013-07-07 17:25:49 +03001708 err = -ENOMEM;
Eli Cohenb037c292017-01-03 23:55:26 +02001709 goto out_count;
Eli Cohene126ba92013-07-07 17:25:49 +03001710 }
1711
Eli Cohenb037c292017-01-03 23:55:26 +02001712 err = allocate_uars(dev, context);
1713 if (err)
1714 goto out_sys_pages;
Eli Cohene126ba92013-07-07 17:25:49 +03001715
Haggai Eranb4cfe442014-12-11 17:04:26 +02001716#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
1717 context->ibucontext.invalidate_range = &mlx5_ib_invalidate_range;
1718#endif
1719
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001720 if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain)) {
Huy Nguyenc85023e2017-05-30 09:42:54 +03001721 err = mlx5_ib_alloc_transport_domain(dev, &context->tdn);
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001722 if (err)
Ilya Lesokhinc44ef992018-03-13 15:18:48 +02001723 goto out_uars;
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001724 }
1725
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001726 INIT_LIST_HEAD(&context->vma_private_list);
Majd Dibbinyad9a3662017-12-24 13:54:56 +02001727 mutex_init(&context->vma_private_list_mutex);
Eli Cohene126ba92013-07-07 17:25:49 +03001728 INIT_LIST_HEAD(&context->db_page_list);
1729 mutex_init(&context->db_page_mutex);
1730
Eli Cohen2f5ff262017-01-03 23:55:21 +02001731 resp.tot_bfregs = req.total_num_bfregs;
Daniel Jurgens508562d2018-01-04 17:25:34 +02001732 resp.num_ports = dev->num_ports;
Matan Barakb368d7c2015-12-15 20:30:12 +02001733
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001734 if (field_avail(typeof(resp), cqe_version, udata->outlen))
1735 resp.response_length += sizeof(resp.cqe_version);
Matan Barakb368d7c2015-12-15 20:30:12 +02001736
Bodong Wang402ca532016-06-17 15:02:20 +03001737 if (field_avail(typeof(resp), cmds_supp_uhw, udata->outlen)) {
Moni Shoua6ad279c52016-11-23 08:23:23 +02001738 resp.cmds_supp_uhw |= MLX5_USER_CMDS_SUPP_UHW_QUERY_DEVICE |
1739 MLX5_USER_CMDS_SUPP_UHW_CREATE_AH;
Bodong Wang402ca532016-06-17 15:02:20 +03001740 resp.response_length += sizeof(resp.cmds_supp_uhw);
1741 }
1742
Or Gerlitz78984892016-11-30 20:33:33 +02001743 if (field_avail(typeof(resp), eth_min_inline, udata->outlen)) {
1744 if (mlx5_ib_port_link_layer(ibdev, 1) == IB_LINK_LAYER_ETHERNET) {
1745 mlx5_query_min_inline(dev->mdev, &resp.eth_min_inline);
1746 resp.eth_min_inline++;
1747 }
1748 resp.response_length += sizeof(resp.eth_min_inline);
1749 }
1750
Feras Daoud5c99eae2018-01-16 20:08:41 +02001751 if (field_avail(typeof(resp), clock_info_versions, udata->outlen)) {
1752 if (mdev->clock_info)
1753 resp.clock_info_versions = BIT(MLX5_IB_CLOCK_INFO_V1);
1754 resp.response_length += sizeof(resp.clock_info_versions);
1755 }
1756
Noa Osherovichbc5c6ee2016-06-04 15:15:31 +03001757 /*
1758 * We don't want to expose information from the PCI bar that is located
1759 * after 4096 bytes, so if the arch only supports larger pages, let's
1760 * pretend we don't support reading the HCA's core clock. This is also
1761 * forced by mmap function.
1762 */
Eli Cohende8d6e02017-01-03 23:55:19 +02001763 if (field_avail(typeof(resp), hca_core_clock_offset, udata->outlen)) {
1764 if (PAGE_SIZE <= 4096) {
1765 resp.comp_mask |=
1766 MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_CORE_CLOCK_OFFSET;
1767 resp.hca_core_clock_offset =
1768 offsetof(struct mlx5_init_seg, internal_timer_h) % PAGE_SIZE;
1769 }
Feras Daoud5c99eae2018-01-16 20:08:41 +02001770 resp.response_length += sizeof(resp.hca_core_clock_offset);
Matan Barakb368d7c2015-12-15 20:30:12 +02001771 }
1772
Eli Cohen30aa60b2017-01-03 23:55:27 +02001773 if (field_avail(typeof(resp), log_uar_size, udata->outlen))
1774 resp.response_length += sizeof(resp.log_uar_size);
1775
1776 if (field_avail(typeof(resp), num_uars_per_page, udata->outlen))
1777 resp.response_length += sizeof(resp.num_uars_per_page);
1778
Yishai Hadas31a78a52017-12-24 16:31:34 +02001779 if (field_avail(typeof(resp), num_dyn_bfregs, udata->outlen)) {
1780 resp.num_dyn_bfregs = bfregi->num_dyn_bfregs;
1781 resp.response_length += sizeof(resp.num_dyn_bfregs);
1782 }
1783
Matan Barakb368d7c2015-12-15 20:30:12 +02001784 err = ib_copy_to_udata(udata, &resp, resp.response_length);
Eli Cohene126ba92013-07-07 17:25:49 +03001785 if (err)
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001786 goto out_td;
Eli Cohene126ba92013-07-07 17:25:49 +03001787
Eli Cohen2f5ff262017-01-03 23:55:21 +02001788 bfregi->ver = ver;
1789 bfregi->num_low_latency_bfregs = req.num_low_latency_bfregs;
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001790 context->cqe_version = resp.cqe_version;
Eli Cohen30aa60b2017-01-03 23:55:27 +02001791 context->lib_caps = req.lib_caps;
1792 print_lib_caps(dev, context->lib_caps);
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001793
Eli Cohene126ba92013-07-07 17:25:49 +03001794 return &context->ibucontext;
1795
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001796out_td:
1797 if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
Huy Nguyenc85023e2017-05-30 09:42:54 +03001798 mlx5_ib_dealloc_transport_domain(dev, context->tdn);
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001799
Eli Cohene126ba92013-07-07 17:25:49 +03001800out_uars:
Eli Cohenb037c292017-01-03 23:55:26 +02001801 deallocate_uars(dev, context);
1802
1803out_sys_pages:
1804 kfree(bfregi->sys_pages);
1805
Eli Cohene126ba92013-07-07 17:25:49 +03001806out_count:
Eli Cohen2f5ff262017-01-03 23:55:21 +02001807 kfree(bfregi->count);
Eli Cohene126ba92013-07-07 17:25:49 +03001808
Eli Cohene126ba92013-07-07 17:25:49 +03001809out_ctx:
1810 kfree(context);
Eli Cohenb037c292017-01-03 23:55:26 +02001811
Eli Cohene126ba92013-07-07 17:25:49 +03001812 return ERR_PTR(err);
1813}
1814
1815static int mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
1816{
1817 struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1818 struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
Eli Cohenb037c292017-01-03 23:55:26 +02001819 struct mlx5_bfreg_info *bfregi;
Eli Cohene126ba92013-07-07 17:25:49 +03001820
Eli Cohenb037c292017-01-03 23:55:26 +02001821 bfregi = &context->bfregi;
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001822 if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
Huy Nguyenc85023e2017-05-30 09:42:54 +03001823 mlx5_ib_dealloc_transport_domain(dev, context->tdn);
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001824
Eli Cohenb037c292017-01-03 23:55:26 +02001825 deallocate_uars(dev, context);
1826 kfree(bfregi->sys_pages);
Eli Cohen2f5ff262017-01-03 23:55:21 +02001827 kfree(bfregi->count);
Eli Cohene126ba92013-07-07 17:25:49 +03001828 kfree(context);
1829
1830 return 0;
1831}
1832
Eli Cohenb037c292017-01-03 23:55:26 +02001833static phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev,
Yishai Hadas4ed131d2017-12-24 16:31:35 +02001834 int uar_idx)
Eli Cohene126ba92013-07-07 17:25:49 +03001835{
Eli Cohenb037c292017-01-03 23:55:26 +02001836 int fw_uars_per_page;
1837
1838 fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ? MLX5_UARS_IN_PAGE : 1;
1839
Yishai Hadas4ed131d2017-12-24 16:31:35 +02001840 return (pci_resource_start(dev->mdev->pdev, 0) >> PAGE_SHIFT) + uar_idx / fw_uars_per_page;
Eli Cohene126ba92013-07-07 17:25:49 +03001841}
1842
1843static int get_command(unsigned long offset)
1844{
1845 return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK;
1846}
1847
1848static int get_arg(unsigned long offset)
1849{
1850 return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1);
1851}
1852
1853static int get_index(unsigned long offset)
1854{
1855 return get_arg(offset);
1856}
1857
Yishai Hadas4ed131d2017-12-24 16:31:35 +02001858/* Index resides in an extra byte to enable larger values than 255 */
1859static int get_extended_index(unsigned long offset)
1860{
1861 return get_arg(offset) | ((offset >> 16) & 0xff) << 8;
1862}
1863
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001864static void mlx5_ib_vma_open(struct vm_area_struct *area)
1865{
1866 /* vma_open is called when a new VMA is created on top of our VMA. This
1867 * is done through either mremap flow or split_vma (usually due to
1868 * mlock, madvise, munmap, etc.) We do not support a clone of the VMA,
1869 * as this VMA is strongly hardware related. Therefore we set the
1870 * vm_ops of the newly created/cloned VMA to NULL, to prevent it from
1871 * calling us again and trying to do incorrect actions. We assume that
1872 * the original VMA size is exactly a single page, and therefore all
1873 * "splitting" operation will not happen to it.
1874 */
1875 area->vm_ops = NULL;
1876}
1877
1878static void mlx5_ib_vma_close(struct vm_area_struct *area)
1879{
1880 struct mlx5_ib_vma_private_data *mlx5_ib_vma_priv_data;
1881
1882 /* It's guaranteed that all VMAs opened on a FD are closed before the
1883 * file itself is closed, therefore no sync is needed with the regular
1884 * closing flow. (e.g. mlx5 ib_dealloc_ucontext)
1885 * However need a sync with accessing the vma as part of
1886 * mlx5_ib_disassociate_ucontext.
1887 * The close operation is usually called under mm->mmap_sem except when
1888 * process is exiting.
1889 * The exiting case is handled explicitly as part of
1890 * mlx5_ib_disassociate_ucontext.
1891 */
1892 mlx5_ib_vma_priv_data = (struct mlx5_ib_vma_private_data *)area->vm_private_data;
1893
1894 /* setting the vma context pointer to null in the mlx5_ib driver's
1895 * private data, to protect a race condition in
1896 * mlx5_ib_disassociate_ucontext().
1897 */
1898 mlx5_ib_vma_priv_data->vma = NULL;
Majd Dibbinyad9a3662017-12-24 13:54:56 +02001899 mutex_lock(mlx5_ib_vma_priv_data->vma_private_list_mutex);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001900 list_del(&mlx5_ib_vma_priv_data->list);
Majd Dibbinyad9a3662017-12-24 13:54:56 +02001901 mutex_unlock(mlx5_ib_vma_priv_data->vma_private_list_mutex);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001902 kfree(mlx5_ib_vma_priv_data);
1903}
1904
1905static const struct vm_operations_struct mlx5_ib_vm_ops = {
1906 .open = mlx5_ib_vma_open,
1907 .close = mlx5_ib_vma_close
1908};
1909
1910static int mlx5_ib_set_vma_data(struct vm_area_struct *vma,
1911 struct mlx5_ib_ucontext *ctx)
1912{
1913 struct mlx5_ib_vma_private_data *vma_prv;
1914 struct list_head *vma_head = &ctx->vma_private_list;
1915
1916 vma_prv = kzalloc(sizeof(*vma_prv), GFP_KERNEL);
1917 if (!vma_prv)
1918 return -ENOMEM;
1919
1920 vma_prv->vma = vma;
Majd Dibbinyad9a3662017-12-24 13:54:56 +02001921 vma_prv->vma_private_list_mutex = &ctx->vma_private_list_mutex;
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001922 vma->vm_private_data = vma_prv;
1923 vma->vm_ops = &mlx5_ib_vm_ops;
1924
Majd Dibbinyad9a3662017-12-24 13:54:56 +02001925 mutex_lock(&ctx->vma_private_list_mutex);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001926 list_add(&vma_prv->list, vma_head);
Majd Dibbinyad9a3662017-12-24 13:54:56 +02001927 mutex_unlock(&ctx->vma_private_list_mutex);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001928
1929 return 0;
1930}
1931
1932static void mlx5_ib_disassociate_ucontext(struct ib_ucontext *ibcontext)
1933{
1934 int ret;
1935 struct vm_area_struct *vma;
1936 struct mlx5_ib_vma_private_data *vma_private, *n;
1937 struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1938 struct task_struct *owning_process = NULL;
1939 struct mm_struct *owning_mm = NULL;
1940
1941 owning_process = get_pid_task(ibcontext->tgid, PIDTYPE_PID);
1942 if (!owning_process)
1943 return;
1944
1945 owning_mm = get_task_mm(owning_process);
1946 if (!owning_mm) {
1947 pr_info("no mm, disassociate ucontext is pending task termination\n");
1948 while (1) {
1949 put_task_struct(owning_process);
1950 usleep_range(1000, 2000);
1951 owning_process = get_pid_task(ibcontext->tgid,
1952 PIDTYPE_PID);
1953 if (!owning_process ||
1954 owning_process->state == TASK_DEAD) {
1955 pr_info("disassociate ucontext done, task was terminated\n");
1956 /* in case task was dead need to release the
1957 * task struct.
1958 */
1959 if (owning_process)
1960 put_task_struct(owning_process);
1961 return;
1962 }
1963 }
1964 }
1965
1966 /* need to protect from a race on closing the vma as part of
1967 * mlx5_ib_vma_close.
1968 */
Maor Gottliebecc7d832017-03-29 06:03:02 +03001969 down_write(&owning_mm->mmap_sem);
Majd Dibbinyad9a3662017-12-24 13:54:56 +02001970 mutex_lock(&context->vma_private_list_mutex);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001971 list_for_each_entry_safe(vma_private, n, &context->vma_private_list,
1972 list) {
1973 vma = vma_private->vma;
1974 ret = zap_vma_ptes(vma, vma->vm_start,
1975 PAGE_SIZE);
1976 WARN_ONCE(ret, "%s: zap_vma_ptes failed", __func__);
1977 /* context going to be destroyed, should
1978 * not access ops any more.
1979 */
Maor Gottlieb13776612017-03-29 06:03:03 +03001980 vma->vm_flags &= ~(VM_SHARED | VM_MAYSHARE);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001981 vma->vm_ops = NULL;
1982 list_del(&vma_private->list);
1983 kfree(vma_private);
1984 }
Majd Dibbinyad9a3662017-12-24 13:54:56 +02001985 mutex_unlock(&context->vma_private_list_mutex);
Maor Gottliebecc7d832017-03-29 06:03:02 +03001986 up_write(&owning_mm->mmap_sem);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001987 mmput(owning_mm);
1988 put_task_struct(owning_process);
1989}
1990
Guy Levi37aa5c32016-04-27 16:49:50 +03001991static inline char *mmap_cmd2str(enum mlx5_ib_mmap_cmd cmd)
1992{
1993 switch (cmd) {
1994 case MLX5_IB_MMAP_WC_PAGE:
1995 return "WC";
1996 case MLX5_IB_MMAP_REGULAR_PAGE:
1997 return "best effort WC";
1998 case MLX5_IB_MMAP_NC_PAGE:
1999 return "NC";
2000 default:
2001 return NULL;
2002 }
2003}
2004
Feras Daoud5c99eae2018-01-16 20:08:41 +02002005static int mlx5_ib_mmap_clock_info_page(struct mlx5_ib_dev *dev,
2006 struct vm_area_struct *vma,
2007 struct mlx5_ib_ucontext *context)
2008{
2009 phys_addr_t pfn;
2010 int err;
2011
2012 if (vma->vm_end - vma->vm_start != PAGE_SIZE)
2013 return -EINVAL;
2014
2015 if (get_index(vma->vm_pgoff) != MLX5_IB_CLOCK_INFO_V1)
2016 return -EOPNOTSUPP;
2017
2018 if (vma->vm_flags & VM_WRITE)
2019 return -EPERM;
2020
2021 if (!dev->mdev->clock_info_page)
2022 return -EOPNOTSUPP;
2023
2024 pfn = page_to_pfn(dev->mdev->clock_info_page);
2025 err = remap_pfn_range(vma, vma->vm_start, pfn, PAGE_SIZE,
2026 vma->vm_page_prot);
2027 if (err)
2028 return err;
2029
2030 mlx5_ib_dbg(dev, "mapped clock info at 0x%lx, PA 0x%llx\n",
2031 vma->vm_start,
2032 (unsigned long long)pfn << PAGE_SHIFT);
2033
2034 return mlx5_ib_set_vma_data(vma, context);
2035}
2036
Guy Levi37aa5c32016-04-27 16:49:50 +03002037static int uar_mmap(struct mlx5_ib_dev *dev, enum mlx5_ib_mmap_cmd cmd,
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03002038 struct vm_area_struct *vma,
2039 struct mlx5_ib_ucontext *context)
Guy Levi37aa5c32016-04-27 16:49:50 +03002040{
Eli Cohen2f5ff262017-01-03 23:55:21 +02002041 struct mlx5_bfreg_info *bfregi = &context->bfregi;
Guy Levi37aa5c32016-04-27 16:49:50 +03002042 int err;
2043 unsigned long idx;
2044 phys_addr_t pfn, pa;
2045 pgprot_t prot;
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002046 u32 bfreg_dyn_idx = 0;
2047 u32 uar_index;
2048 int dyn_uar = (cmd == MLX5_IB_MMAP_ALLOC_WC);
2049 int max_valid_idx = dyn_uar ? bfregi->num_sys_pages :
2050 bfregi->num_static_sys_pages;
Eli Cohenb037c292017-01-03 23:55:26 +02002051
2052 if (vma->vm_end - vma->vm_start != PAGE_SIZE)
2053 return -EINVAL;
2054
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002055 if (dyn_uar)
2056 idx = get_extended_index(vma->vm_pgoff) + bfregi->num_static_sys_pages;
2057 else
2058 idx = get_index(vma->vm_pgoff);
2059
2060 if (idx >= max_valid_idx) {
2061 mlx5_ib_warn(dev, "invalid uar index %lu, max=%d\n",
2062 idx, max_valid_idx);
Eli Cohenb037c292017-01-03 23:55:26 +02002063 return -EINVAL;
2064 }
Guy Levi37aa5c32016-04-27 16:49:50 +03002065
2066 switch (cmd) {
2067 case MLX5_IB_MMAP_WC_PAGE:
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002068 case MLX5_IB_MMAP_ALLOC_WC:
Guy Levi37aa5c32016-04-27 16:49:50 +03002069/* Some architectures don't support WC memory */
2070#if defined(CONFIG_X86)
2071 if (!pat_enabled())
2072 return -EPERM;
2073#elif !(defined(CONFIG_PPC) || (defined(CONFIG_ARM) && defined(CONFIG_MMU)))
2074 return -EPERM;
2075#endif
2076 /* fall through */
2077 case MLX5_IB_MMAP_REGULAR_PAGE:
2078 /* For MLX5_IB_MMAP_REGULAR_PAGE do the best effort to get WC */
2079 prot = pgprot_writecombine(vma->vm_page_prot);
2080 break;
2081 case MLX5_IB_MMAP_NC_PAGE:
2082 prot = pgprot_noncached(vma->vm_page_prot);
2083 break;
2084 default:
2085 return -EINVAL;
2086 }
2087
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002088 if (dyn_uar) {
2089 int uars_per_page;
2090
2091 uars_per_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k);
2092 bfreg_dyn_idx = idx * (uars_per_page * MLX5_NON_FP_BFREGS_PER_UAR);
2093 if (bfreg_dyn_idx >= bfregi->total_num_bfregs) {
2094 mlx5_ib_warn(dev, "invalid bfreg_dyn_idx %u, max=%u\n",
2095 bfreg_dyn_idx, bfregi->total_num_bfregs);
2096 return -EINVAL;
2097 }
2098
2099 mutex_lock(&bfregi->lock);
2100 /* Fail if uar already allocated, first bfreg index of each
2101 * page holds its count.
2102 */
2103 if (bfregi->count[bfreg_dyn_idx]) {
2104 mlx5_ib_warn(dev, "wrong offset, idx %lu is busy, bfregn=%u\n", idx, bfreg_dyn_idx);
2105 mutex_unlock(&bfregi->lock);
2106 return -EINVAL;
2107 }
2108
2109 bfregi->count[bfreg_dyn_idx]++;
2110 mutex_unlock(&bfregi->lock);
2111
2112 err = mlx5_cmd_alloc_uar(dev->mdev, &uar_index);
2113 if (err) {
2114 mlx5_ib_warn(dev, "UAR alloc failed\n");
2115 goto free_bfreg;
2116 }
2117 } else {
2118 uar_index = bfregi->sys_pages[idx];
2119 }
2120
2121 pfn = uar_index2pfn(dev, uar_index);
Guy Levi37aa5c32016-04-27 16:49:50 +03002122 mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn %pa\n", idx, &pfn);
2123
2124 vma->vm_page_prot = prot;
2125 err = io_remap_pfn_range(vma, vma->vm_start, pfn,
2126 PAGE_SIZE, vma->vm_page_prot);
2127 if (err) {
2128 mlx5_ib_err(dev, "io_remap_pfn_range failed with error=%d, vm_start=0x%lx, pfn=%pa, mmap_cmd=%s\n",
2129 err, vma->vm_start, &pfn, mmap_cmd2str(cmd));
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002130 err = -EAGAIN;
2131 goto err;
Guy Levi37aa5c32016-04-27 16:49:50 +03002132 }
2133
2134 pa = pfn << PAGE_SHIFT;
2135 mlx5_ib_dbg(dev, "mapped %s at 0x%lx, PA %pa\n", mmap_cmd2str(cmd),
2136 vma->vm_start, &pa);
2137
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002138 err = mlx5_ib_set_vma_data(vma, context);
2139 if (err)
2140 goto err;
2141
2142 if (dyn_uar)
2143 bfregi->sys_pages[idx] = uar_index;
2144 return 0;
2145
2146err:
2147 if (!dyn_uar)
2148 return err;
2149
2150 mlx5_cmd_free_uar(dev->mdev, idx);
2151
2152free_bfreg:
2153 mlx5_ib_free_bfreg(dev, bfregi, bfreg_dyn_idx);
2154
2155 return err;
Guy Levi37aa5c32016-04-27 16:49:50 +03002156}
2157
Eli Cohene126ba92013-07-07 17:25:49 +03002158static int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma)
2159{
2160 struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
2161 struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
Eli Cohene126ba92013-07-07 17:25:49 +03002162 unsigned long command;
Eli Cohene126ba92013-07-07 17:25:49 +03002163 phys_addr_t pfn;
2164
2165 command = get_command(vma->vm_pgoff);
2166 switch (command) {
Guy Levi37aa5c32016-04-27 16:49:50 +03002167 case MLX5_IB_MMAP_WC_PAGE:
2168 case MLX5_IB_MMAP_NC_PAGE:
Eli Cohene126ba92013-07-07 17:25:49 +03002169 case MLX5_IB_MMAP_REGULAR_PAGE:
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002170 case MLX5_IB_MMAP_ALLOC_WC:
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03002171 return uar_mmap(dev, command, vma, context);
Eli Cohene126ba92013-07-07 17:25:49 +03002172
2173 case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES:
2174 return -ENOSYS;
2175
Matan Barakd69e3bc2015-12-15 20:30:13 +02002176 case MLX5_IB_MMAP_CORE_CLOCK:
Matan Barakd69e3bc2015-12-15 20:30:13 +02002177 if (vma->vm_end - vma->vm_start != PAGE_SIZE)
2178 return -EINVAL;
2179
Matan Barak6cbac1e2016-04-14 16:52:10 +03002180 if (vma->vm_flags & VM_WRITE)
Matan Barakd69e3bc2015-12-15 20:30:13 +02002181 return -EPERM;
2182
2183 /* Don't expose to user-space information it shouldn't have */
2184 if (PAGE_SIZE > 4096)
2185 return -EOPNOTSUPP;
2186
2187 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
2188 pfn = (dev->mdev->iseg_base +
2189 offsetof(struct mlx5_init_seg, internal_timer_h)) >>
2190 PAGE_SHIFT;
2191 if (io_remap_pfn_range(vma, vma->vm_start, pfn,
2192 PAGE_SIZE, vma->vm_page_prot))
2193 return -EAGAIN;
2194
2195 mlx5_ib_dbg(dev, "mapped internal timer at 0x%lx, PA 0x%llx\n",
2196 vma->vm_start,
2197 (unsigned long long)pfn << PAGE_SHIFT);
2198 break;
Feras Daoud5c99eae2018-01-16 20:08:41 +02002199 case MLX5_IB_MMAP_CLOCK_INFO:
2200 return mlx5_ib_mmap_clock_info_page(dev, vma, context);
Matan Barakd69e3bc2015-12-15 20:30:13 +02002201
Eli Cohene126ba92013-07-07 17:25:49 +03002202 default:
2203 return -EINVAL;
2204 }
2205
2206 return 0;
2207}
2208
Eli Cohene126ba92013-07-07 17:25:49 +03002209static struct ib_pd *mlx5_ib_alloc_pd(struct ib_device *ibdev,
2210 struct ib_ucontext *context,
2211 struct ib_udata *udata)
2212{
2213 struct mlx5_ib_alloc_pd_resp resp;
2214 struct mlx5_ib_pd *pd;
2215 int err;
2216
2217 pd = kmalloc(sizeof(*pd), GFP_KERNEL);
2218 if (!pd)
2219 return ERR_PTR(-ENOMEM);
2220
Jack Morgenstein9603b612014-07-28 23:30:22 +03002221 err = mlx5_core_alloc_pd(to_mdev(ibdev)->mdev, &pd->pdn);
Eli Cohene126ba92013-07-07 17:25:49 +03002222 if (err) {
2223 kfree(pd);
2224 return ERR_PTR(err);
2225 }
2226
2227 if (context) {
2228 resp.pdn = pd->pdn;
2229 if (ib_copy_to_udata(udata, &resp, sizeof(resp))) {
Jack Morgenstein9603b612014-07-28 23:30:22 +03002230 mlx5_core_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn);
Eli Cohene126ba92013-07-07 17:25:49 +03002231 kfree(pd);
2232 return ERR_PTR(-EFAULT);
2233 }
Eli Cohene126ba92013-07-07 17:25:49 +03002234 }
2235
2236 return &pd->ibpd;
2237}
2238
2239static int mlx5_ib_dealloc_pd(struct ib_pd *pd)
2240{
2241 struct mlx5_ib_dev *mdev = to_mdev(pd->device);
2242 struct mlx5_ib_pd *mpd = to_mpd(pd);
2243
Jack Morgenstein9603b612014-07-28 23:30:22 +03002244 mlx5_core_dealloc_pd(mdev->mdev, mpd->pdn);
Eli Cohene126ba92013-07-07 17:25:49 +03002245 kfree(mpd);
2246
2247 return 0;
2248}
2249
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002250enum {
2251 MATCH_CRITERIA_ENABLE_OUTER_BIT,
2252 MATCH_CRITERIA_ENABLE_MISC_BIT,
2253 MATCH_CRITERIA_ENABLE_INNER_BIT
2254};
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002255
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002256#define HEADER_IS_ZERO(match_criteria, headers) \
2257 !(memchr_inv(MLX5_ADDR_OF(fte_match_param, match_criteria, headers), \
2258 0, MLX5_FLD_SZ_BYTES(fte_match_param, headers))) \
2259
2260static u8 get_match_criteria_enable(u32 *match_criteria)
2261{
2262 u8 match_criteria_enable;
2263
2264 match_criteria_enable =
2265 (!HEADER_IS_ZERO(match_criteria, outer_headers)) <<
2266 MATCH_CRITERIA_ENABLE_OUTER_BIT;
2267 match_criteria_enable |=
2268 (!HEADER_IS_ZERO(match_criteria, misc_parameters)) <<
2269 MATCH_CRITERIA_ENABLE_MISC_BIT;
2270 match_criteria_enable |=
2271 (!HEADER_IS_ZERO(match_criteria, inner_headers)) <<
2272 MATCH_CRITERIA_ENABLE_INNER_BIT;
2273
2274 return match_criteria_enable;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002275}
2276
Maor Gottliebca0d4752016-08-30 16:58:35 +03002277static void set_proto(void *outer_c, void *outer_v, u8 mask, u8 val)
2278{
2279 MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_protocol, mask);
2280 MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_protocol, val);
2281}
2282
Moses Reuben2d1e6972016-11-14 19:04:52 +02002283static void set_flow_label(void *misc_c, void *misc_v, u8 mask, u8 val,
2284 bool inner)
2285{
2286 if (inner) {
2287 MLX5_SET(fte_match_set_misc,
2288 misc_c, inner_ipv6_flow_label, mask);
2289 MLX5_SET(fte_match_set_misc,
2290 misc_v, inner_ipv6_flow_label, val);
2291 } else {
2292 MLX5_SET(fte_match_set_misc,
2293 misc_c, outer_ipv6_flow_label, mask);
2294 MLX5_SET(fte_match_set_misc,
2295 misc_v, outer_ipv6_flow_label, val);
2296 }
2297}
2298
Maor Gottliebca0d4752016-08-30 16:58:35 +03002299static void set_tos(void *outer_c, void *outer_v, u8 mask, u8 val)
2300{
2301 MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_ecn, mask);
2302 MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_ecn, val);
2303 MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_dscp, mask >> 2);
2304 MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_dscp, val >> 2);
2305}
2306
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002307#define LAST_ETH_FIELD vlan_tag
2308#define LAST_IB_FIELD sl
Maor Gottliebca0d4752016-08-30 16:58:35 +03002309#define LAST_IPV4_FIELD tos
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002310#define LAST_IPV6_FIELD traffic_class
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002311#define LAST_TCP_UDP_FIELD src_port
Moses Reubenffb30d82016-11-14 19:04:50 +02002312#define LAST_TUNNEL_FIELD tunnel_id
Moses Reuben2ac693f2017-01-18 14:59:50 +02002313#define LAST_FLOW_TAG_FIELD tag_id
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002314#define LAST_DROP_FIELD size
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002315
2316/* Field is the last supported field */
2317#define FIELDS_NOT_SUPPORTED(filter, field)\
2318 memchr_inv((void *)&filter.field +\
2319 sizeof(filter.field), 0,\
2320 sizeof(filter) -\
2321 offsetof(typeof(filter), field) -\
2322 sizeof(filter.field))
2323
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002324static int parse_flow_attr(struct mlx5_core_dev *mdev, u32 *match_c,
2325 u32 *match_v, const union ib_flow_spec *ib_spec,
Boris Pismenny075572d2017-08-16 09:33:30 +03002326 struct mlx5_flow_act *action)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002327{
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002328 void *misc_params_c = MLX5_ADDR_OF(fte_match_param, match_c,
2329 misc_parameters);
2330 void *misc_params_v = MLX5_ADDR_OF(fte_match_param, match_v,
2331 misc_parameters);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002332 void *headers_c;
2333 void *headers_v;
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002334 int match_ipv;
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002335
Moses Reuben2d1e6972016-11-14 19:04:52 +02002336 if (ib_spec->type & IB_FLOW_SPEC_INNER) {
2337 headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
2338 inner_headers);
2339 headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
2340 inner_headers);
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002341 match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2342 ft_field_support.inner_ip_version);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002343 } else {
2344 headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
2345 outer_headers);
2346 headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
2347 outer_headers);
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002348 match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2349 ft_field_support.outer_ip_version);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002350 }
2351
2352 switch (ib_spec->type & ~IB_FLOW_SPEC_INNER) {
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002353 case IB_FLOW_SPEC_ETH:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002354 if (FIELDS_NOT_SUPPORTED(ib_spec->eth.mask, LAST_ETH_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002355 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002356
Moses Reuben2d1e6972016-11-14 19:04:52 +02002357 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002358 dmac_47_16),
2359 ib_spec->eth.mask.dst_mac);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002360 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002361 dmac_47_16),
2362 ib_spec->eth.val.dst_mac);
2363
Moses Reuben2d1e6972016-11-14 19:04:52 +02002364 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottliebee3da802016-09-12 19:16:24 +03002365 smac_47_16),
2366 ib_spec->eth.mask.src_mac);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002367 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottliebee3da802016-09-12 19:16:24 +03002368 smac_47_16),
2369 ib_spec->eth.val.src_mac);
2370
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002371 if (ib_spec->eth.mask.vlan_tag) {
Moses Reuben2d1e6972016-11-14 19:04:52 +02002372 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Mohamad Haj Yahia10543362016-10-09 16:25:43 +03002373 cvlan_tag, 1);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002374 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Mohamad Haj Yahia10543362016-10-09 16:25:43 +03002375 cvlan_tag, 1);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002376
Moses Reuben2d1e6972016-11-14 19:04:52 +02002377 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002378 first_vid, ntohs(ib_spec->eth.mask.vlan_tag));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002379 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002380 first_vid, ntohs(ib_spec->eth.val.vlan_tag));
2381
Moses Reuben2d1e6972016-11-14 19:04:52 +02002382 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002383 first_cfi,
2384 ntohs(ib_spec->eth.mask.vlan_tag) >> 12);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002385 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002386 first_cfi,
2387 ntohs(ib_spec->eth.val.vlan_tag) >> 12);
2388
Moses Reuben2d1e6972016-11-14 19:04:52 +02002389 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002390 first_prio,
2391 ntohs(ib_spec->eth.mask.vlan_tag) >> 13);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002392 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002393 first_prio,
2394 ntohs(ib_spec->eth.val.vlan_tag) >> 13);
2395 }
Moses Reuben2d1e6972016-11-14 19:04:52 +02002396 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002397 ethertype, ntohs(ib_spec->eth.mask.ether_type));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002398 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002399 ethertype, ntohs(ib_spec->eth.val.ether_type));
2400 break;
2401 case IB_FLOW_SPEC_IPV4:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002402 if (FIELDS_NOT_SUPPORTED(ib_spec->ipv4.mask, LAST_IPV4_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002403 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002404
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002405 if (match_ipv) {
2406 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2407 ip_version, 0xf);
2408 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Boris Pismenny3346c482017-08-20 15:13:08 +03002409 ip_version, MLX5_FS_IPV4_VERSION);
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002410 } else {
2411 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2412 ethertype, 0xffff);
2413 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2414 ethertype, ETH_P_IP);
2415 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002416
Moses Reuben2d1e6972016-11-14 19:04:52 +02002417 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002418 src_ipv4_src_ipv6.ipv4_layout.ipv4),
2419 &ib_spec->ipv4.mask.src_ip,
2420 sizeof(ib_spec->ipv4.mask.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002421 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002422 src_ipv4_src_ipv6.ipv4_layout.ipv4),
2423 &ib_spec->ipv4.val.src_ip,
2424 sizeof(ib_spec->ipv4.val.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002425 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002426 dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2427 &ib_spec->ipv4.mask.dst_ip,
2428 sizeof(ib_spec->ipv4.mask.dst_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002429 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002430 dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2431 &ib_spec->ipv4.val.dst_ip,
2432 sizeof(ib_spec->ipv4.val.dst_ip));
Maor Gottliebca0d4752016-08-30 16:58:35 +03002433
Moses Reuben2d1e6972016-11-14 19:04:52 +02002434 set_tos(headers_c, headers_v,
Maor Gottliebca0d4752016-08-30 16:58:35 +03002435 ib_spec->ipv4.mask.tos, ib_spec->ipv4.val.tos);
2436
Moses Reuben2d1e6972016-11-14 19:04:52 +02002437 set_proto(headers_c, headers_v,
Maor Gottliebca0d4752016-08-30 16:58:35 +03002438 ib_spec->ipv4.mask.proto, ib_spec->ipv4.val.proto);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002439 break;
Maor Gottlieb026bae02016-06-17 15:14:51 +03002440 case IB_FLOW_SPEC_IPV6:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002441 if (FIELDS_NOT_SUPPORTED(ib_spec->ipv6.mask, LAST_IPV6_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002442 return -EOPNOTSUPP;
Maor Gottlieb026bae02016-06-17 15:14:51 +03002443
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002444 if (match_ipv) {
2445 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2446 ip_version, 0xf);
2447 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Boris Pismenny3346c482017-08-20 15:13:08 +03002448 ip_version, MLX5_FS_IPV6_VERSION);
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002449 } else {
2450 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2451 ethertype, 0xffff);
2452 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2453 ethertype, ETH_P_IPV6);
2454 }
Maor Gottlieb026bae02016-06-17 15:14:51 +03002455
Moses Reuben2d1e6972016-11-14 19:04:52 +02002456 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb026bae02016-06-17 15:14:51 +03002457 src_ipv4_src_ipv6.ipv6_layout.ipv6),
2458 &ib_spec->ipv6.mask.src_ip,
2459 sizeof(ib_spec->ipv6.mask.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002460 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb026bae02016-06-17 15:14:51 +03002461 src_ipv4_src_ipv6.ipv6_layout.ipv6),
2462 &ib_spec->ipv6.val.src_ip,
2463 sizeof(ib_spec->ipv6.val.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002464 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb026bae02016-06-17 15:14:51 +03002465 dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2466 &ib_spec->ipv6.mask.dst_ip,
2467 sizeof(ib_spec->ipv6.mask.dst_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002468 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb026bae02016-06-17 15:14:51 +03002469 dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2470 &ib_spec->ipv6.val.dst_ip,
2471 sizeof(ib_spec->ipv6.val.dst_ip));
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002472
Moses Reuben2d1e6972016-11-14 19:04:52 +02002473 set_tos(headers_c, headers_v,
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002474 ib_spec->ipv6.mask.traffic_class,
2475 ib_spec->ipv6.val.traffic_class);
2476
Moses Reuben2d1e6972016-11-14 19:04:52 +02002477 set_proto(headers_c, headers_v,
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002478 ib_spec->ipv6.mask.next_hdr,
2479 ib_spec->ipv6.val.next_hdr);
2480
Moses Reuben2d1e6972016-11-14 19:04:52 +02002481 set_flow_label(misc_params_c, misc_params_v,
2482 ntohl(ib_spec->ipv6.mask.flow_label),
2483 ntohl(ib_spec->ipv6.val.flow_label),
2484 ib_spec->type & IB_FLOW_SPEC_INNER);
2485
Maor Gottlieb026bae02016-06-17 15:14:51 +03002486 break;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002487 case IB_FLOW_SPEC_TCP:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002488 if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
2489 LAST_TCP_UDP_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002490 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002491
Moses Reuben2d1e6972016-11-14 19:04:52 +02002492 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002493 0xff);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002494 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002495 IPPROTO_TCP);
2496
Moses Reuben2d1e6972016-11-14 19:04:52 +02002497 MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002498 ntohs(ib_spec->tcp_udp.mask.src_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002499 MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002500 ntohs(ib_spec->tcp_udp.val.src_port));
2501
Moses Reuben2d1e6972016-11-14 19:04:52 +02002502 MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002503 ntohs(ib_spec->tcp_udp.mask.dst_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002504 MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002505 ntohs(ib_spec->tcp_udp.val.dst_port));
2506 break;
2507 case IB_FLOW_SPEC_UDP:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002508 if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
2509 LAST_TCP_UDP_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002510 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002511
Moses Reuben2d1e6972016-11-14 19:04:52 +02002512 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002513 0xff);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002514 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002515 IPPROTO_UDP);
2516
Moses Reuben2d1e6972016-11-14 19:04:52 +02002517 MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002518 ntohs(ib_spec->tcp_udp.mask.src_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002519 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002520 ntohs(ib_spec->tcp_udp.val.src_port));
2521
Moses Reuben2d1e6972016-11-14 19:04:52 +02002522 MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002523 ntohs(ib_spec->tcp_udp.mask.dst_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002524 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002525 ntohs(ib_spec->tcp_udp.val.dst_port));
2526 break;
Moses Reubenffb30d82016-11-14 19:04:50 +02002527 case IB_FLOW_SPEC_VXLAN_TUNNEL:
2528 if (FIELDS_NOT_SUPPORTED(ib_spec->tunnel.mask,
2529 LAST_TUNNEL_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002530 return -EOPNOTSUPP;
Moses Reubenffb30d82016-11-14 19:04:50 +02002531
2532 MLX5_SET(fte_match_set_misc, misc_params_c, vxlan_vni,
2533 ntohl(ib_spec->tunnel.mask.tunnel_id));
2534 MLX5_SET(fte_match_set_misc, misc_params_v, vxlan_vni,
2535 ntohl(ib_spec->tunnel.val.tunnel_id));
2536 break;
Moses Reuben2ac693f2017-01-18 14:59:50 +02002537 case IB_FLOW_SPEC_ACTION_TAG:
2538 if (FIELDS_NOT_SUPPORTED(ib_spec->flow_tag,
2539 LAST_FLOW_TAG_FIELD))
2540 return -EOPNOTSUPP;
2541 if (ib_spec->flow_tag.tag_id >= BIT(24))
2542 return -EINVAL;
2543
Boris Pismenny075572d2017-08-16 09:33:30 +03002544 action->flow_tag = ib_spec->flow_tag.tag_id;
Matan Baraka9db0ec2017-08-16 09:43:48 +03002545 action->has_flow_tag = true;
Moses Reuben2ac693f2017-01-18 14:59:50 +02002546 break;
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002547 case IB_FLOW_SPEC_ACTION_DROP:
2548 if (FIELDS_NOT_SUPPORTED(ib_spec->drop,
2549 LAST_DROP_FIELD))
2550 return -EOPNOTSUPP;
Boris Pismenny075572d2017-08-16 09:33:30 +03002551 action->action |= MLX5_FLOW_CONTEXT_ACTION_DROP;
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002552 break;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002553 default:
2554 return -EINVAL;
2555 }
2556
2557 return 0;
2558}
2559
2560/* If a flow could catch both multicast and unicast packets,
2561 * it won't fall into the multicast flow steering table and this rule
2562 * could steal other multicast packets.
2563 */
Yishai Hadasa550ddf2017-08-17 15:52:33 +03002564static bool flow_is_multicast_only(const struct ib_flow_attr *ib_attr)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002565{
Yishai Hadas81e30882017-06-08 16:15:09 +03002566 union ib_flow_spec *flow_spec;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002567
2568 if (ib_attr->type != IB_FLOW_ATTR_NORMAL ||
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002569 ib_attr->num_of_specs < 1)
2570 return false;
2571
Yishai Hadas81e30882017-06-08 16:15:09 +03002572 flow_spec = (union ib_flow_spec *)(ib_attr + 1);
2573 if (flow_spec->type == IB_FLOW_SPEC_IPV4) {
2574 struct ib_flow_spec_ipv4 *ipv4_spec;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002575
Yishai Hadas81e30882017-06-08 16:15:09 +03002576 ipv4_spec = (struct ib_flow_spec_ipv4 *)flow_spec;
2577 if (ipv4_is_multicast(ipv4_spec->val.dst_ip))
2578 return true;
2579
2580 return false;
2581 }
2582
2583 if (flow_spec->type == IB_FLOW_SPEC_ETH) {
2584 struct ib_flow_spec_eth *eth_spec;
2585
2586 eth_spec = (struct ib_flow_spec_eth *)flow_spec;
2587 return is_multicast_ether_addr(eth_spec->mask.dst_mac) &&
2588 is_multicast_ether_addr(eth_spec->val.dst_mac);
2589 }
2590
2591 return false;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002592}
2593
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002594static bool is_valid_ethertype(struct mlx5_core_dev *mdev,
2595 const struct ib_flow_attr *flow_attr,
Ariel Levkovich0f750962017-04-03 13:11:02 +03002596 bool check_inner)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002597{
2598 union ib_flow_spec *ib_spec = (union ib_flow_spec *)(flow_attr + 1);
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002599 int match_ipv = check_inner ?
2600 MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2601 ft_field_support.inner_ip_version) :
2602 MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2603 ft_field_support.outer_ip_version);
Ariel Levkovich0f750962017-04-03 13:11:02 +03002604 int inner_bit = check_inner ? IB_FLOW_SPEC_INNER : 0;
2605 bool ipv4_spec_valid, ipv6_spec_valid;
2606 unsigned int ip_spec_type = 0;
2607 bool has_ethertype = false;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002608 unsigned int spec_index;
Ariel Levkovich0f750962017-04-03 13:11:02 +03002609 bool mask_valid = true;
2610 u16 eth_type = 0;
2611 bool type_valid;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002612
2613 /* Validate that ethertype is correct */
2614 for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
Ariel Levkovich0f750962017-04-03 13:11:02 +03002615 if ((ib_spec->type == (IB_FLOW_SPEC_ETH | inner_bit)) &&
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002616 ib_spec->eth.mask.ether_type) {
Ariel Levkovich0f750962017-04-03 13:11:02 +03002617 mask_valid = (ib_spec->eth.mask.ether_type ==
2618 htons(0xffff));
2619 has_ethertype = true;
2620 eth_type = ntohs(ib_spec->eth.val.ether_type);
2621 } else if ((ib_spec->type == (IB_FLOW_SPEC_IPV4 | inner_bit)) ||
2622 (ib_spec->type == (IB_FLOW_SPEC_IPV6 | inner_bit))) {
2623 ip_spec_type = ib_spec->type;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002624 }
2625 ib_spec = (void *)ib_spec + ib_spec->size;
2626 }
Ariel Levkovich0f750962017-04-03 13:11:02 +03002627
2628 type_valid = (!has_ethertype) || (!ip_spec_type);
2629 if (!type_valid && mask_valid) {
2630 ipv4_spec_valid = (eth_type == ETH_P_IP) &&
2631 (ip_spec_type == (IB_FLOW_SPEC_IPV4 | inner_bit));
2632 ipv6_spec_valid = (eth_type == ETH_P_IPV6) &&
2633 (ip_spec_type == (IB_FLOW_SPEC_IPV6 | inner_bit));
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002634
2635 type_valid = (ipv4_spec_valid) || (ipv6_spec_valid) ||
2636 (((eth_type == ETH_P_MPLS_UC) ||
2637 (eth_type == ETH_P_MPLS_MC)) && match_ipv);
Ariel Levkovich0f750962017-04-03 13:11:02 +03002638 }
2639
2640 return type_valid;
2641}
2642
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002643static bool is_valid_attr(struct mlx5_core_dev *mdev,
2644 const struct ib_flow_attr *flow_attr)
Ariel Levkovich0f750962017-04-03 13:11:02 +03002645{
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002646 return is_valid_ethertype(mdev, flow_attr, false) &&
2647 is_valid_ethertype(mdev, flow_attr, true);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002648}
2649
2650static void put_flow_table(struct mlx5_ib_dev *dev,
2651 struct mlx5_ib_flow_prio *prio, bool ft_added)
2652{
2653 prio->refcount -= !!ft_added;
2654 if (!prio->refcount) {
2655 mlx5_destroy_flow_table(prio->flow_table);
2656 prio->flow_table = NULL;
2657 }
2658}
2659
2660static int mlx5_ib_destroy_flow(struct ib_flow *flow_id)
2661{
2662 struct mlx5_ib_dev *dev = to_mdev(flow_id->qp->device);
2663 struct mlx5_ib_flow_handler *handler = container_of(flow_id,
2664 struct mlx5_ib_flow_handler,
2665 ibflow);
2666 struct mlx5_ib_flow_handler *iter, *tmp;
2667
Mark Bloch9a4ca382018-01-16 14:42:35 +00002668 mutex_lock(&dev->flow_db->lock);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002669
2670 list_for_each_entry_safe(iter, tmp, &handler->list, list) {
Mark Bloch74491de2016-08-31 11:24:25 +00002671 mlx5_del_flow_rules(iter->rule);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002672 put_flow_table(dev, iter->prio, true);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002673 list_del(&iter->list);
2674 kfree(iter);
2675 }
2676
Mark Bloch74491de2016-08-31 11:24:25 +00002677 mlx5_del_flow_rules(handler->rule);
Maor Gottlieb5497adc2016-08-28 14:16:31 +03002678 put_flow_table(dev, handler->prio, true);
Mark Bloch9a4ca382018-01-16 14:42:35 +00002679 mutex_unlock(&dev->flow_db->lock);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002680
2681 kfree(handler);
2682
2683 return 0;
2684}
2685
Maor Gottlieb35d190112016-03-07 18:51:47 +02002686static int ib_prio_to_core_prio(unsigned int priority, bool dont_trap)
2687{
2688 priority *= 2;
2689 if (!dont_trap)
2690 priority++;
2691 return priority;
2692}
2693
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002694enum flow_table_type {
2695 MLX5_IB_FT_RX,
2696 MLX5_IB_FT_TX
2697};
2698
Maor Gottlieb00b7c2a2017-03-29 06:09:01 +03002699#define MLX5_FS_MAX_TYPES 6
2700#define MLX5_FS_MAX_ENTRIES BIT(16)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002701static struct mlx5_ib_flow_prio *get_flow_table(struct mlx5_ib_dev *dev,
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002702 struct ib_flow_attr *flow_attr,
2703 enum flow_table_type ft_type)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002704{
Maor Gottlieb35d190112016-03-07 18:51:47 +02002705 bool dont_trap = flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002706 struct mlx5_flow_namespace *ns = NULL;
2707 struct mlx5_ib_flow_prio *prio;
2708 struct mlx5_flow_table *ft;
Maor Gottliebdac388e2017-03-29 06:09:00 +03002709 int max_table_size;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002710 int num_entries;
2711 int num_groups;
2712 int priority;
2713 int err = 0;
2714
Maor Gottliebdac388e2017-03-29 06:09:00 +03002715 max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
2716 log_max_ft_size));
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002717 if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
Maor Gottlieb35d190112016-03-07 18:51:47 +02002718 if (flow_is_multicast_only(flow_attr) &&
2719 !dont_trap)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002720 priority = MLX5_IB_FLOW_MCAST_PRIO;
2721 else
Maor Gottlieb35d190112016-03-07 18:51:47 +02002722 priority = ib_prio_to_core_prio(flow_attr->priority,
2723 dont_trap);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002724 ns = mlx5_get_flow_namespace(dev->mdev,
2725 MLX5_FLOW_NAMESPACE_BYPASS);
2726 num_entries = MLX5_FS_MAX_ENTRIES;
2727 num_groups = MLX5_FS_MAX_TYPES;
Mark Bloch9a4ca382018-01-16 14:42:35 +00002728 prio = &dev->flow_db->prios[priority];
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002729 } else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
2730 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
2731 ns = mlx5_get_flow_namespace(dev->mdev,
2732 MLX5_FLOW_NAMESPACE_LEFTOVERS);
2733 build_leftovers_ft_param(&priority,
2734 &num_entries,
2735 &num_groups);
Mark Bloch9a4ca382018-01-16 14:42:35 +00002736 prio = &dev->flow_db->prios[MLX5_IB_FLOW_LEFTOVERS_PRIO];
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002737 } else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
2738 if (!MLX5_CAP_FLOWTABLE(dev->mdev,
2739 allow_sniffer_and_nic_rx_shared_tir))
2740 return ERR_PTR(-ENOTSUPP);
2741
2742 ns = mlx5_get_flow_namespace(dev->mdev, ft_type == MLX5_IB_FT_RX ?
2743 MLX5_FLOW_NAMESPACE_SNIFFER_RX :
2744 MLX5_FLOW_NAMESPACE_SNIFFER_TX);
2745
Mark Bloch9a4ca382018-01-16 14:42:35 +00002746 prio = &dev->flow_db->sniffer[ft_type];
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002747 priority = 0;
2748 num_entries = 1;
2749 num_groups = 1;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002750 }
2751
2752 if (!ns)
2753 return ERR_PTR(-ENOTSUPP);
2754
Maor Gottliebdac388e2017-03-29 06:09:00 +03002755 if (num_entries > max_table_size)
2756 return ERR_PTR(-ENOMEM);
2757
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002758 ft = prio->flow_table;
2759 if (!ft) {
2760 ft = mlx5_create_auto_grouped_flow_table(ns, priority,
2761 num_entries,
Maor Gottliebd63cd282016-04-29 01:36:35 +03002762 num_groups,
Hadar Hen Zionc9f1b072016-11-07 15:14:44 +02002763 0, 0);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002764
2765 if (!IS_ERR(ft)) {
2766 prio->refcount = 0;
2767 prio->flow_table = ft;
2768 } else {
2769 err = PTR_ERR(ft);
2770 }
2771 }
2772
2773 return err ? ERR_PTR(err) : prio;
2774}
2775
Yishai Hadasa550ddf2017-08-17 15:52:33 +03002776static void set_underlay_qp(struct mlx5_ib_dev *dev,
2777 struct mlx5_flow_spec *spec,
2778 u32 underlay_qpn)
2779{
2780 void *misc_params_c = MLX5_ADDR_OF(fte_match_param,
2781 spec->match_criteria,
2782 misc_parameters);
2783 void *misc_params_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
2784 misc_parameters);
2785
2786 if (underlay_qpn &&
2787 MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
2788 ft_field_support.bth_dst_qp)) {
2789 MLX5_SET(fte_match_set_misc,
2790 misc_params_v, bth_dst_qp, underlay_qpn);
2791 MLX5_SET(fte_match_set_misc,
2792 misc_params_c, bth_dst_qp, 0xffffff);
2793 }
2794}
2795
2796static struct mlx5_ib_flow_handler *_create_flow_rule(struct mlx5_ib_dev *dev,
2797 struct mlx5_ib_flow_prio *ft_prio,
2798 const struct ib_flow_attr *flow_attr,
2799 struct mlx5_flow_destination *dst,
2800 u32 underlay_qpn)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002801{
2802 struct mlx5_flow_table *ft = ft_prio->flow_table;
2803 struct mlx5_ib_flow_handler *handler;
Boris Pismenny075572d2017-08-16 09:33:30 +03002804 struct mlx5_flow_act flow_act = {.flow_tag = MLX5_FS_DEFAULT_FLOW_TAG};
Maor Gottliebc5bb1732016-07-04 17:23:05 +03002805 struct mlx5_flow_spec *spec;
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002806 struct mlx5_flow_destination *rule_dst = dst;
Maor Gottliebdd063d02016-08-28 14:16:32 +03002807 const void *ib_flow = (const void *)flow_attr + sizeof(*flow_attr);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002808 unsigned int spec_index;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002809 int err = 0;
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002810 int dest_num = 1;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002811
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002812 if (!is_valid_attr(dev->mdev, flow_attr))
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002813 return ERR_PTR(-EINVAL);
2814
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03002815 spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002816 handler = kzalloc(sizeof(*handler), GFP_KERNEL);
Maor Gottliebc5bb1732016-07-04 17:23:05 +03002817 if (!handler || !spec) {
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002818 err = -ENOMEM;
2819 goto free;
2820 }
2821
2822 INIT_LIST_HEAD(&handler->list);
2823
2824 for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002825 err = parse_flow_attr(dev->mdev, spec->match_criteria,
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002826 spec->match_value,
Boris Pismenny075572d2017-08-16 09:33:30 +03002827 ib_flow, &flow_act);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002828 if (err < 0)
2829 goto free;
2830
2831 ib_flow += ((union ib_flow_spec *)ib_flow)->size;
2832 }
2833
Yishai Hadasa550ddf2017-08-17 15:52:33 +03002834 if (!flow_is_multicast_only(flow_attr))
2835 set_underlay_qp(dev, spec, underlay_qpn);
2836
Mark Bloch018a94e2018-01-16 14:44:29 +00002837 if (dev->rep) {
2838 void *misc;
2839
2840 misc = MLX5_ADDR_OF(fte_match_param, spec->match_value,
2841 misc_parameters);
2842 MLX5_SET(fte_match_set_misc, misc, source_port,
2843 dev->rep->vport);
2844 misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
2845 misc_parameters);
2846 MLX5_SET_TO_ONES(fte_match_set_misc, misc, source_port);
2847 }
2848
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002849 spec->match_criteria_enable = get_match_criteria_enable(spec->match_criteria);
Boris Pismenny075572d2017-08-16 09:33:30 +03002850 if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_DROP) {
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002851 rule_dst = NULL;
2852 dest_num = 0;
2853 } else {
2854 flow_act.action = dst ? MLX5_FLOW_CONTEXT_ACTION_FWD_DEST :
2855 MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO;
2856 }
Moses Reuben2ac693f2017-01-18 14:59:50 +02002857
Matan Baraka9db0ec2017-08-16 09:43:48 +03002858 if (flow_act.has_flow_tag &&
Moses Reuben2ac693f2017-01-18 14:59:50 +02002859 (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
2860 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT)) {
2861 mlx5_ib_warn(dev, "Flow tag %u and attribute type %x isn't allowed in leftovers\n",
Boris Pismenny075572d2017-08-16 09:33:30 +03002862 flow_act.flow_tag, flow_attr->type);
Moses Reuben2ac693f2017-01-18 14:59:50 +02002863 err = -EINVAL;
2864 goto free;
2865 }
Mark Bloch74491de2016-08-31 11:24:25 +00002866 handler->rule = mlx5_add_flow_rules(ft, spec,
Hadar Hen Zion66958ed2016-11-07 15:14:45 +02002867 &flow_act,
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002868 rule_dst, dest_num);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002869
2870 if (IS_ERR(handler->rule)) {
2871 err = PTR_ERR(handler->rule);
2872 goto free;
2873 }
2874
Maor Gottliebd9d49802016-08-28 14:16:33 +03002875 ft_prio->refcount++;
Maor Gottlieb5497adc2016-08-28 14:16:31 +03002876 handler->prio = ft_prio;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002877
2878 ft_prio->flow_table = ft;
2879free:
2880 if (err)
2881 kfree(handler);
Maor Gottliebc5bb1732016-07-04 17:23:05 +03002882 kvfree(spec);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002883 return err ? ERR_PTR(err) : handler;
2884}
2885
Yishai Hadasa550ddf2017-08-17 15:52:33 +03002886static struct mlx5_ib_flow_handler *create_flow_rule(struct mlx5_ib_dev *dev,
2887 struct mlx5_ib_flow_prio *ft_prio,
2888 const struct ib_flow_attr *flow_attr,
2889 struct mlx5_flow_destination *dst)
2890{
2891 return _create_flow_rule(dev, ft_prio, flow_attr, dst, 0);
2892}
2893
Maor Gottlieb35d190112016-03-07 18:51:47 +02002894static struct mlx5_ib_flow_handler *create_dont_trap_rule(struct mlx5_ib_dev *dev,
2895 struct mlx5_ib_flow_prio *ft_prio,
2896 struct ib_flow_attr *flow_attr,
2897 struct mlx5_flow_destination *dst)
2898{
2899 struct mlx5_ib_flow_handler *handler_dst = NULL;
2900 struct mlx5_ib_flow_handler *handler = NULL;
2901
2902 handler = create_flow_rule(dev, ft_prio, flow_attr, NULL);
2903 if (!IS_ERR(handler)) {
2904 handler_dst = create_flow_rule(dev, ft_prio,
2905 flow_attr, dst);
2906 if (IS_ERR(handler_dst)) {
Mark Bloch74491de2016-08-31 11:24:25 +00002907 mlx5_del_flow_rules(handler->rule);
Maor Gottliebd9d49802016-08-28 14:16:33 +03002908 ft_prio->refcount--;
Maor Gottlieb35d190112016-03-07 18:51:47 +02002909 kfree(handler);
2910 handler = handler_dst;
2911 } else {
2912 list_add(&handler_dst->list, &handler->list);
2913 }
2914 }
2915
2916 return handler;
2917}
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002918enum {
2919 LEFTOVERS_MC,
2920 LEFTOVERS_UC,
2921};
2922
2923static struct mlx5_ib_flow_handler *create_leftovers_rule(struct mlx5_ib_dev *dev,
2924 struct mlx5_ib_flow_prio *ft_prio,
2925 struct ib_flow_attr *flow_attr,
2926 struct mlx5_flow_destination *dst)
2927{
2928 struct mlx5_ib_flow_handler *handler_ucast = NULL;
2929 struct mlx5_ib_flow_handler *handler = NULL;
2930
2931 static struct {
2932 struct ib_flow_attr flow_attr;
2933 struct ib_flow_spec_eth eth_flow;
2934 } leftovers_specs[] = {
2935 [LEFTOVERS_MC] = {
2936 .flow_attr = {
2937 .num_of_specs = 1,
2938 .size = sizeof(leftovers_specs[0])
2939 },
2940 .eth_flow = {
2941 .type = IB_FLOW_SPEC_ETH,
2942 .size = sizeof(struct ib_flow_spec_eth),
2943 .mask = {.dst_mac = {0x1} },
2944 .val = {.dst_mac = {0x1} }
2945 }
2946 },
2947 [LEFTOVERS_UC] = {
2948 .flow_attr = {
2949 .num_of_specs = 1,
2950 .size = sizeof(leftovers_specs[0])
2951 },
2952 .eth_flow = {
2953 .type = IB_FLOW_SPEC_ETH,
2954 .size = sizeof(struct ib_flow_spec_eth),
2955 .mask = {.dst_mac = {0x1} },
2956 .val = {.dst_mac = {} }
2957 }
2958 }
2959 };
2960
2961 handler = create_flow_rule(dev, ft_prio,
2962 &leftovers_specs[LEFTOVERS_MC].flow_attr,
2963 dst);
2964 if (!IS_ERR(handler) &&
2965 flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT) {
2966 handler_ucast = create_flow_rule(dev, ft_prio,
2967 &leftovers_specs[LEFTOVERS_UC].flow_attr,
2968 dst);
2969 if (IS_ERR(handler_ucast)) {
Mark Bloch74491de2016-08-31 11:24:25 +00002970 mlx5_del_flow_rules(handler->rule);
Maor Gottliebd9d49802016-08-28 14:16:33 +03002971 ft_prio->refcount--;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002972 kfree(handler);
2973 handler = handler_ucast;
2974 } else {
2975 list_add(&handler_ucast->list, &handler->list);
2976 }
2977 }
2978
2979 return handler;
2980}
2981
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002982static struct mlx5_ib_flow_handler *create_sniffer_rule(struct mlx5_ib_dev *dev,
2983 struct mlx5_ib_flow_prio *ft_rx,
2984 struct mlx5_ib_flow_prio *ft_tx,
2985 struct mlx5_flow_destination *dst)
2986{
2987 struct mlx5_ib_flow_handler *handler_rx;
2988 struct mlx5_ib_flow_handler *handler_tx;
2989 int err;
2990 static const struct ib_flow_attr flow_attr = {
2991 .num_of_specs = 0,
2992 .size = sizeof(flow_attr)
2993 };
2994
2995 handler_rx = create_flow_rule(dev, ft_rx, &flow_attr, dst);
2996 if (IS_ERR(handler_rx)) {
2997 err = PTR_ERR(handler_rx);
2998 goto err;
2999 }
3000
3001 handler_tx = create_flow_rule(dev, ft_tx, &flow_attr, dst);
3002 if (IS_ERR(handler_tx)) {
3003 err = PTR_ERR(handler_tx);
3004 goto err_tx;
3005 }
3006
3007 list_add(&handler_tx->list, &handler_rx->list);
3008
3009 return handler_rx;
3010
3011err_tx:
Mark Bloch74491de2016-08-31 11:24:25 +00003012 mlx5_del_flow_rules(handler_rx->rule);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003013 ft_rx->refcount--;
3014 kfree(handler_rx);
3015err:
3016 return ERR_PTR(err);
3017}
3018
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003019static struct ib_flow *mlx5_ib_create_flow(struct ib_qp *qp,
3020 struct ib_flow_attr *flow_attr,
3021 int domain)
3022{
3023 struct mlx5_ib_dev *dev = to_mdev(qp->device);
Yishai Hadasd9f88e52016-08-28 10:58:37 +03003024 struct mlx5_ib_qp *mqp = to_mqp(qp);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003025 struct mlx5_ib_flow_handler *handler = NULL;
3026 struct mlx5_flow_destination *dst = NULL;
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003027 struct mlx5_ib_flow_prio *ft_prio_tx = NULL;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003028 struct mlx5_ib_flow_prio *ft_prio;
3029 int err;
Yishai Hadasa550ddf2017-08-17 15:52:33 +03003030 int underlay_qpn;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003031
3032 if (flow_attr->priority > MLX5_IB_FLOW_LAST_PRIO)
Maor Gottliebdac388e2017-03-29 06:09:00 +03003033 return ERR_PTR(-ENOMEM);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003034
3035 if (domain != IB_FLOW_DOMAIN_USER ||
Daniel Jurgens508562d2018-01-04 17:25:34 +02003036 flow_attr->port > dev->num_ports ||
Maor Gottlieb35d190112016-03-07 18:51:47 +02003037 (flow_attr->flags & ~IB_FLOW_ATTR_FLAGS_DONT_TRAP))
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003038 return ERR_PTR(-EINVAL);
3039
3040 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
3041 if (!dst)
3042 return ERR_PTR(-ENOMEM);
3043
Mark Bloch9a4ca382018-01-16 14:42:35 +00003044 mutex_lock(&dev->flow_db->lock);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003045
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003046 ft_prio = get_flow_table(dev, flow_attr, MLX5_IB_FT_RX);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003047 if (IS_ERR(ft_prio)) {
3048 err = PTR_ERR(ft_prio);
3049 goto unlock;
3050 }
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003051 if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
3052 ft_prio_tx = get_flow_table(dev, flow_attr, MLX5_IB_FT_TX);
3053 if (IS_ERR(ft_prio_tx)) {
3054 err = PTR_ERR(ft_prio_tx);
3055 ft_prio_tx = NULL;
3056 goto destroy_ft;
3057 }
3058 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003059
3060 dst->type = MLX5_FLOW_DESTINATION_TYPE_TIR;
Yishai Hadasd9f88e52016-08-28 10:58:37 +03003061 if (mqp->flags & MLX5_IB_QP_RSS)
3062 dst->tir_num = mqp->rss_qp.tirn;
3063 else
3064 dst->tir_num = mqp->raw_packet_qp.rq.tirn;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003065
3066 if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
Maor Gottlieb35d190112016-03-07 18:51:47 +02003067 if (flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP) {
3068 handler = create_dont_trap_rule(dev, ft_prio,
3069 flow_attr, dst);
3070 } else {
Yishai Hadasa550ddf2017-08-17 15:52:33 +03003071 underlay_qpn = (mqp->flags & MLX5_IB_QP_UNDERLAY) ?
3072 mqp->underlay_qpn : 0;
3073 handler = _create_flow_rule(dev, ft_prio, flow_attr,
3074 dst, underlay_qpn);
Maor Gottlieb35d190112016-03-07 18:51:47 +02003075 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003076 } else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
3077 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
3078 handler = create_leftovers_rule(dev, ft_prio, flow_attr,
3079 dst);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003080 } else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
3081 handler = create_sniffer_rule(dev, ft_prio, ft_prio_tx, dst);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003082 } else {
3083 err = -EINVAL;
3084 goto destroy_ft;
3085 }
3086
3087 if (IS_ERR(handler)) {
3088 err = PTR_ERR(handler);
3089 handler = NULL;
3090 goto destroy_ft;
3091 }
3092
Mark Bloch9a4ca382018-01-16 14:42:35 +00003093 mutex_unlock(&dev->flow_db->lock);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003094 kfree(dst);
3095
3096 return &handler->ibflow;
3097
3098destroy_ft:
3099 put_flow_table(dev, ft_prio, false);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003100 if (ft_prio_tx)
3101 put_flow_table(dev, ft_prio_tx, false);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003102unlock:
Mark Bloch9a4ca382018-01-16 14:42:35 +00003103 mutex_unlock(&dev->flow_db->lock);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003104 kfree(dst);
3105 kfree(handler);
3106 return ERR_PTR(err);
3107}
3108
Aviad Yehezkelc6475a02018-03-28 09:27:50 +03003109static u32 mlx5_ib_flow_action_flags_to_accel_xfrm_flags(u32 mlx5_flags)
3110{
3111 u32 flags = 0;
3112
3113 if (mlx5_flags & MLX5_IB_UAPI_FLOW_ACTION_FLAGS_REQUIRE_METADATA)
3114 flags |= MLX5_ACCEL_XFRM_FLAG_REQUIRE_METADATA;
3115
3116 return flags;
3117}
3118
3119#define MLX5_FLOW_ACTION_ESP_CREATE_LAST_SUPPORTED MLX5_IB_UAPI_FLOW_ACTION_FLAGS_REQUIRE_METADATA
3120static struct ib_flow_action *
3121mlx5_ib_create_flow_action_esp(struct ib_device *device,
3122 const struct ib_flow_action_attrs_esp *attr,
3123 struct uverbs_attr_bundle *attrs)
3124{
3125 struct mlx5_ib_dev *mdev = to_mdev(device);
3126 struct ib_uverbs_flow_action_esp_keymat_aes_gcm *aes_gcm;
3127 struct mlx5_accel_esp_xfrm_attrs accel_attrs = {};
3128 struct mlx5_ib_flow_action *action;
3129 u64 action_flags;
3130 u64 flags;
3131 int err = 0;
3132
3133 if (IS_UVERBS_COPY_ERR(uverbs_copy_from(&action_flags, attrs,
3134 MLX5_IB_ATTR_CREATE_FLOW_ACTION_FLAGS)))
3135 return ERR_PTR(-EFAULT);
3136
3137 if (action_flags >= (MLX5_FLOW_ACTION_ESP_CREATE_LAST_SUPPORTED << 1))
3138 return ERR_PTR(-EOPNOTSUPP);
3139
3140 flags = mlx5_ib_flow_action_flags_to_accel_xfrm_flags(action_flags);
3141
3142 /* We current only support a subset of the standard features. Only a
3143 * keymat of type AES_GCM, with icv_len == 16, iv_algo == SEQ and esn
3144 * (with overlap). Full offload mode isn't supported.
3145 */
3146 if (!attr->keymat || attr->replay || attr->encap ||
3147 attr->spi || attr->seq || attr->tfc_pad ||
3148 attr->hard_limit_pkts ||
3149 (attr->flags & ~(IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED |
3150 IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ENCRYPT)))
3151 return ERR_PTR(-EOPNOTSUPP);
3152
3153 if (attr->keymat->protocol !=
3154 IB_UVERBS_FLOW_ACTION_ESP_KEYMAT_AES_GCM)
3155 return ERR_PTR(-EOPNOTSUPP);
3156
3157 aes_gcm = &attr->keymat->keymat.aes_gcm;
3158
3159 if (aes_gcm->icv_len != 16 ||
3160 aes_gcm->iv_algo != IB_UVERBS_FLOW_ACTION_IV_ALGO_SEQ)
3161 return ERR_PTR(-EOPNOTSUPP);
3162
3163 action = kmalloc(sizeof(*action), GFP_KERNEL);
3164 if (!action)
3165 return ERR_PTR(-ENOMEM);
3166
3167 action->esp_aes_gcm.ib_flags = attr->flags;
3168 memcpy(&accel_attrs.keymat.aes_gcm.aes_key, &aes_gcm->aes_key,
3169 sizeof(accel_attrs.keymat.aes_gcm.aes_key));
3170 accel_attrs.keymat.aes_gcm.key_len = aes_gcm->key_len * 8;
3171 memcpy(&accel_attrs.keymat.aes_gcm.salt, &aes_gcm->salt,
3172 sizeof(accel_attrs.keymat.aes_gcm.salt));
3173 memcpy(&accel_attrs.keymat.aes_gcm.seq_iv, &aes_gcm->iv,
3174 sizeof(accel_attrs.keymat.aes_gcm.seq_iv));
3175 accel_attrs.keymat.aes_gcm.icv_len = aes_gcm->icv_len * 8;
3176 accel_attrs.keymat.aes_gcm.iv_algo = MLX5_ACCEL_ESP_AES_GCM_IV_ALGO_SEQ;
3177 accel_attrs.keymat_type = MLX5_ACCEL_ESP_KEYMAT_AES_GCM;
3178
3179 accel_attrs.esn = attr->esn;
3180 if (attr->flags & IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED)
3181 accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_TRIGGERED;
3182 if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW)
3183 accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP;
3184
3185 if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ENCRYPT)
3186 accel_attrs.action |= MLX5_ACCEL_ESP_ACTION_ENCRYPT;
3187
3188 action->esp_aes_gcm.ctx =
3189 mlx5_accel_esp_create_xfrm(mdev->mdev, &accel_attrs, flags);
3190 if (IS_ERR(action->esp_aes_gcm.ctx)) {
3191 err = PTR_ERR(action->esp_aes_gcm.ctx);
3192 goto err_parse;
3193 }
3194
3195 action->esp_aes_gcm.ib_flags = attr->flags;
3196
3197 return &action->ib_action;
3198
3199err_parse:
3200 kfree(action);
3201 return ERR_PTR(err);
3202}
3203
Matan Barak349705c2018-03-28 09:27:51 +03003204static int
3205mlx5_ib_modify_flow_action_esp(struct ib_flow_action *action,
3206 const struct ib_flow_action_attrs_esp *attr,
3207 struct uverbs_attr_bundle *attrs)
3208{
3209 struct mlx5_ib_flow_action *maction = to_mflow_act(action);
3210 struct mlx5_accel_esp_xfrm_attrs accel_attrs;
3211 int err = 0;
3212
3213 if (attr->keymat || attr->replay || attr->encap ||
3214 attr->spi || attr->seq || attr->tfc_pad ||
3215 attr->hard_limit_pkts ||
3216 (attr->flags & ~(IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED |
3217 IB_FLOW_ACTION_ESP_FLAGS_MOD_ESP_ATTRS |
3218 IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW)))
3219 return -EOPNOTSUPP;
3220
3221 /* Only the ESN value or the MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP can
3222 * be modified.
3223 */
3224 if (!(maction->esp_aes_gcm.ib_flags &
3225 IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED) &&
3226 attr->flags & (IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED |
3227 IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW))
3228 return -EINVAL;
3229
3230 memcpy(&accel_attrs, &maction->esp_aes_gcm.ctx->attrs,
3231 sizeof(accel_attrs));
3232
3233 accel_attrs.esn = attr->esn;
3234 if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW)
3235 accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP;
3236 else
3237 accel_attrs.flags &= ~MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP;
3238
3239 err = mlx5_accel_esp_modify_xfrm(maction->esp_aes_gcm.ctx,
3240 &accel_attrs);
3241 if (err)
3242 return err;
3243
3244 maction->esp_aes_gcm.ib_flags &=
3245 ~IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW;
3246 maction->esp_aes_gcm.ib_flags |=
3247 attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW;
3248
3249 return 0;
3250}
3251
Aviad Yehezkelc6475a02018-03-28 09:27:50 +03003252static int mlx5_ib_destroy_flow_action(struct ib_flow_action *action)
3253{
3254 struct mlx5_ib_flow_action *maction = to_mflow_act(action);
3255
3256 switch (action->type) {
3257 case IB_FLOW_ACTION_ESP:
3258 /*
3259 * We only support aes_gcm by now, so we implicitly know this is
3260 * the underline crypto.
3261 */
3262 mlx5_accel_esp_destroy_xfrm(maction->esp_aes_gcm.ctx);
3263 break;
3264 default:
3265 WARN_ON(true);
3266 break;
3267 }
3268
3269 kfree(maction);
3270 return 0;
3271}
3272
Eli Cohene126ba92013-07-07 17:25:49 +03003273static int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
3274{
3275 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
Yishai Hadas81e30882017-06-08 16:15:09 +03003276 struct mlx5_ib_qp *mqp = to_mqp(ibqp);
Eli Cohene126ba92013-07-07 17:25:49 +03003277 int err;
3278
Yishai Hadas81e30882017-06-08 16:15:09 +03003279 if (mqp->flags & MLX5_IB_QP_UNDERLAY) {
3280 mlx5_ib_dbg(dev, "Attaching a multi cast group to underlay QP is not supported\n");
3281 return -EOPNOTSUPP;
3282 }
3283
Jack Morgenstein9603b612014-07-28 23:30:22 +03003284 err = mlx5_core_attach_mcg(dev->mdev, gid, ibqp->qp_num);
Eli Cohene126ba92013-07-07 17:25:49 +03003285 if (err)
3286 mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n",
3287 ibqp->qp_num, gid->raw);
3288
3289 return err;
3290}
3291
3292static int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
3293{
3294 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
3295 int err;
3296
Jack Morgenstein9603b612014-07-28 23:30:22 +03003297 err = mlx5_core_detach_mcg(dev->mdev, gid, ibqp->qp_num);
Eli Cohene126ba92013-07-07 17:25:49 +03003298 if (err)
3299 mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n",
3300 ibqp->qp_num, gid->raw);
3301
3302 return err;
3303}
3304
3305static int init_node_data(struct mlx5_ib_dev *dev)
3306{
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03003307 int err;
Eli Cohene126ba92013-07-07 17:25:49 +03003308
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03003309 err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc);
Eli Cohene126ba92013-07-07 17:25:49 +03003310 if (err)
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03003311 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03003312
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03003313 dev->mdev->rev_id = dev->mdev->pdev->revision;
Eli Cohene126ba92013-07-07 17:25:49 +03003314
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03003315 return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid);
Eli Cohene126ba92013-07-07 17:25:49 +03003316}
3317
3318static ssize_t show_fw_pages(struct device *device, struct device_attribute *attr,
3319 char *buf)
3320{
3321 struct mlx5_ib_dev *dev =
3322 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
3323
Jack Morgenstein9603b612014-07-28 23:30:22 +03003324 return sprintf(buf, "%d\n", dev->mdev->priv.fw_pages);
Eli Cohene126ba92013-07-07 17:25:49 +03003325}
3326
3327static ssize_t show_reg_pages(struct device *device,
3328 struct device_attribute *attr, char *buf)
3329{
3330 struct mlx5_ib_dev *dev =
3331 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
3332
Haggai Eran6aec21f2014-12-11 17:04:23 +02003333 return sprintf(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages));
Eli Cohene126ba92013-07-07 17:25:49 +03003334}
3335
3336static ssize_t show_hca(struct device *device, struct device_attribute *attr,
3337 char *buf)
3338{
3339 struct mlx5_ib_dev *dev =
3340 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
Jack Morgenstein9603b612014-07-28 23:30:22 +03003341 return sprintf(buf, "MT%d\n", dev->mdev->pdev->device);
Eli Cohene126ba92013-07-07 17:25:49 +03003342}
3343
Eli Cohene126ba92013-07-07 17:25:49 +03003344static ssize_t show_rev(struct device *device, struct device_attribute *attr,
3345 char *buf)
3346{
3347 struct mlx5_ib_dev *dev =
3348 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
Jack Morgenstein9603b612014-07-28 23:30:22 +03003349 return sprintf(buf, "%x\n", dev->mdev->rev_id);
Eli Cohene126ba92013-07-07 17:25:49 +03003350}
3351
3352static ssize_t show_board(struct device *device, struct device_attribute *attr,
3353 char *buf)
3354{
3355 struct mlx5_ib_dev *dev =
3356 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
3357 return sprintf(buf, "%.*s\n", MLX5_BOARD_ID_LEN,
Jack Morgenstein9603b612014-07-28 23:30:22 +03003358 dev->mdev->board_id);
Eli Cohene126ba92013-07-07 17:25:49 +03003359}
3360
3361static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03003362static DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL);
3363static DEVICE_ATTR(board_id, S_IRUGO, show_board, NULL);
3364static DEVICE_ATTR(fw_pages, S_IRUGO, show_fw_pages, NULL);
3365static DEVICE_ATTR(reg_pages, S_IRUGO, show_reg_pages, NULL);
3366
3367static struct device_attribute *mlx5_class_attributes[] = {
3368 &dev_attr_hw_rev,
Eli Cohene126ba92013-07-07 17:25:49 +03003369 &dev_attr_hca_type,
3370 &dev_attr_board_id,
3371 &dev_attr_fw_pages,
3372 &dev_attr_reg_pages,
3373};
3374
Haggai Eran7722f472016-02-29 15:45:07 +02003375static void pkey_change_handler(struct work_struct *work)
3376{
3377 struct mlx5_ib_port_resources *ports =
3378 container_of(work, struct mlx5_ib_port_resources,
3379 pkey_change_work);
3380
3381 mutex_lock(&ports->devr->mutex);
3382 mlx5_ib_gsi_pkey_change(ports->gsi);
3383 mutex_unlock(&ports->devr->mutex);
3384}
3385
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03003386static void mlx5_ib_handle_internal_error(struct mlx5_ib_dev *ibdev)
3387{
3388 struct mlx5_ib_qp *mqp;
3389 struct mlx5_ib_cq *send_mcq, *recv_mcq;
3390 struct mlx5_core_cq *mcq;
3391 struct list_head cq_armed_list;
3392 unsigned long flags_qp;
3393 unsigned long flags_cq;
3394 unsigned long flags;
3395
3396 INIT_LIST_HEAD(&cq_armed_list);
3397
3398 /* Go over qp list reside on that ibdev, sync with create/destroy qp.*/
3399 spin_lock_irqsave(&ibdev->reset_flow_resource_lock, flags);
3400 list_for_each_entry(mqp, &ibdev->qp_list, qps_list) {
3401 spin_lock_irqsave(&mqp->sq.lock, flags_qp);
3402 if (mqp->sq.tail != mqp->sq.head) {
3403 send_mcq = to_mcq(mqp->ibqp.send_cq);
3404 spin_lock_irqsave(&send_mcq->lock, flags_cq);
3405 if (send_mcq->mcq.comp &&
3406 mqp->ibqp.send_cq->comp_handler) {
3407 if (!send_mcq->mcq.reset_notify_added) {
3408 send_mcq->mcq.reset_notify_added = 1;
3409 list_add_tail(&send_mcq->mcq.reset_notify,
3410 &cq_armed_list);
3411 }
3412 }
3413 spin_unlock_irqrestore(&send_mcq->lock, flags_cq);
3414 }
3415 spin_unlock_irqrestore(&mqp->sq.lock, flags_qp);
3416 spin_lock_irqsave(&mqp->rq.lock, flags_qp);
3417 /* no handling is needed for SRQ */
3418 if (!mqp->ibqp.srq) {
3419 if (mqp->rq.tail != mqp->rq.head) {
3420 recv_mcq = to_mcq(mqp->ibqp.recv_cq);
3421 spin_lock_irqsave(&recv_mcq->lock, flags_cq);
3422 if (recv_mcq->mcq.comp &&
3423 mqp->ibqp.recv_cq->comp_handler) {
3424 if (!recv_mcq->mcq.reset_notify_added) {
3425 recv_mcq->mcq.reset_notify_added = 1;
3426 list_add_tail(&recv_mcq->mcq.reset_notify,
3427 &cq_armed_list);
3428 }
3429 }
3430 spin_unlock_irqrestore(&recv_mcq->lock,
3431 flags_cq);
3432 }
3433 }
3434 spin_unlock_irqrestore(&mqp->rq.lock, flags_qp);
3435 }
3436 /*At that point all inflight post send were put to be executed as of we
3437 * lock/unlock above locks Now need to arm all involved CQs.
3438 */
3439 list_for_each_entry(mcq, &cq_armed_list, reset_notify) {
3440 mcq->comp(mcq);
3441 }
3442 spin_unlock_irqrestore(&ibdev->reset_flow_resource_lock, flags);
3443}
3444
Maor Gottlieb03404e82017-05-30 10:29:13 +03003445static void delay_drop_handler(struct work_struct *work)
3446{
3447 int err;
3448 struct mlx5_ib_delay_drop *delay_drop =
3449 container_of(work, struct mlx5_ib_delay_drop,
3450 delay_drop_work);
3451
Maor Gottliebfe248c32017-05-30 10:29:14 +03003452 atomic_inc(&delay_drop->events_cnt);
3453
Maor Gottlieb03404e82017-05-30 10:29:13 +03003454 mutex_lock(&delay_drop->lock);
3455 err = mlx5_core_set_delay_drop(delay_drop->dev->mdev,
3456 delay_drop->timeout);
3457 if (err) {
3458 mlx5_ib_warn(delay_drop->dev, "Failed to set delay drop, timeout=%u\n",
3459 delay_drop->timeout);
3460 delay_drop->activate = false;
3461 }
3462 mutex_unlock(&delay_drop->lock);
3463}
3464
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02003465static void mlx5_ib_handle_event(struct work_struct *_work)
Eli Cohene126ba92013-07-07 17:25:49 +03003466{
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02003467 struct mlx5_ib_event_work *work =
3468 container_of(_work, struct mlx5_ib_event_work, work);
3469 struct mlx5_ib_dev *ibdev;
Eli Cohene126ba92013-07-07 17:25:49 +03003470 struct ib_event ibev;
Eli Cohendbaaff22016-10-27 16:36:44 +03003471 bool fatal = false;
Daniel Jurgensaba46212018-02-25 13:39:53 +02003472 u8 port = (u8)work->param;
Eli Cohene126ba92013-07-07 17:25:49 +03003473
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02003474 if (mlx5_core_is_mp_slave(work->dev)) {
3475 ibdev = mlx5_ib_get_ibdev_from_mpi(work->context);
3476 if (!ibdev)
3477 goto out;
3478 } else {
3479 ibdev = work->context;
3480 }
3481
3482 switch (work->event) {
Eli Cohene126ba92013-07-07 17:25:49 +03003483 case MLX5_DEV_EVENT_SYS_ERROR:
Eli Cohene126ba92013-07-07 17:25:49 +03003484 ibev.event = IB_EVENT_DEVICE_FATAL;
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03003485 mlx5_ib_handle_internal_error(ibdev);
Eli Cohendbaaff22016-10-27 16:36:44 +03003486 fatal = true;
Eli Cohene126ba92013-07-07 17:25:49 +03003487 break;
3488
3489 case MLX5_DEV_EVENT_PORT_UP:
Eli Cohene126ba92013-07-07 17:25:49 +03003490 case MLX5_DEV_EVENT_PORT_DOWN:
Noa Osherovich2788cf32016-06-04 15:15:29 +03003491 case MLX5_DEV_EVENT_PORT_INITIALIZED:
Aviv Heller5ec8c832016-09-18 20:48:00 +03003492 /* In RoCE, port up/down events are handled in
3493 * mlx5_netdev_event().
3494 */
3495 if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) ==
3496 IB_LINK_LAYER_ETHERNET)
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02003497 goto out;
Aviv Heller5ec8c832016-09-18 20:48:00 +03003498
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02003499 ibev.event = (work->event == MLX5_DEV_EVENT_PORT_UP) ?
Aviv Heller5ec8c832016-09-18 20:48:00 +03003500 IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
Eli Cohene126ba92013-07-07 17:25:49 +03003501 break;
3502
Eli Cohene126ba92013-07-07 17:25:49 +03003503 case MLX5_DEV_EVENT_LID_CHANGE:
3504 ibev.event = IB_EVENT_LID_CHANGE;
Eli Cohene126ba92013-07-07 17:25:49 +03003505 break;
3506
3507 case MLX5_DEV_EVENT_PKEY_CHANGE:
3508 ibev.event = IB_EVENT_PKEY_CHANGE;
Haggai Eran7722f472016-02-29 15:45:07 +02003509 schedule_work(&ibdev->devr.ports[port - 1].pkey_change_work);
Eli Cohene126ba92013-07-07 17:25:49 +03003510 break;
3511
3512 case MLX5_DEV_EVENT_GUID_CHANGE:
3513 ibev.event = IB_EVENT_GID_CHANGE;
Eli Cohene126ba92013-07-07 17:25:49 +03003514 break;
3515
3516 case MLX5_DEV_EVENT_CLIENT_REREG:
3517 ibev.event = IB_EVENT_CLIENT_REREGISTER;
Eli Cohene126ba92013-07-07 17:25:49 +03003518 break;
Maor Gottlieb03404e82017-05-30 10:29:13 +03003519 case MLX5_DEV_EVENT_DELAY_DROP_TIMEOUT:
3520 schedule_work(&ibdev->delay_drop.delay_drop_work);
3521 goto out;
Saeed Mahameedbdc37922016-09-29 19:35:38 +03003522 default:
Maor Gottlieb03404e82017-05-30 10:29:13 +03003523 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03003524 }
3525
3526 ibev.device = &ibdev->ib_dev;
3527 ibev.element.port_num = port;
3528
Daniel Jurgensaba46212018-02-25 13:39:53 +02003529 if (!rdma_is_port_valid(&ibdev->ib_dev, port)) {
Eli Cohena0c84c32013-09-11 16:35:27 +03003530 mlx5_ib_warn(ibdev, "warning: event on port %d\n", port);
Maor Gottlieb03404e82017-05-30 10:29:13 +03003531 goto out;
Eli Cohena0c84c32013-09-11 16:35:27 +03003532 }
3533
Eli Cohene126ba92013-07-07 17:25:49 +03003534 if (ibdev->ib_active)
3535 ib_dispatch_event(&ibev);
Eli Cohendbaaff22016-10-27 16:36:44 +03003536
3537 if (fatal)
3538 ibdev->ib_active = false;
Maor Gottlieb03404e82017-05-30 10:29:13 +03003539out:
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02003540 kfree(work);
3541}
3542
3543static void mlx5_ib_event(struct mlx5_core_dev *dev, void *context,
3544 enum mlx5_dev_event event, unsigned long param)
3545{
3546 struct mlx5_ib_event_work *work;
3547
3548 work = kmalloc(sizeof(*work), GFP_ATOMIC);
Leon Romanovsky10bea9c2018-01-19 13:07:11 +02003549 if (!work)
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02003550 return;
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02003551
Leon Romanovsky10bea9c2018-01-19 13:07:11 +02003552 INIT_WORK(&work->work, mlx5_ib_handle_event);
3553 work->dev = dev;
3554 work->param = param;
3555 work->context = context;
3556 work->event = event;
3557
3558 queue_work(mlx5_ib_event_wq, &work->work);
Eli Cohene126ba92013-07-07 17:25:49 +03003559}
3560
Maor Gottliebc43f1112017-01-18 14:10:33 +02003561static int set_has_smi_cap(struct mlx5_ib_dev *dev)
3562{
3563 struct mlx5_hca_vport_context vport_ctx;
3564 int err;
3565 int port;
3566
Daniel Jurgens508562d2018-01-04 17:25:34 +02003567 for (port = 1; port <= dev->num_ports; port++) {
Maor Gottliebc43f1112017-01-18 14:10:33 +02003568 dev->mdev->port_caps[port - 1].has_smi = false;
3569 if (MLX5_CAP_GEN(dev->mdev, port_type) ==
3570 MLX5_CAP_PORT_TYPE_IB) {
3571 if (MLX5_CAP_GEN(dev->mdev, ib_virt)) {
3572 err = mlx5_query_hca_vport_context(dev->mdev, 0,
3573 port, 0,
3574 &vport_ctx);
3575 if (err) {
3576 mlx5_ib_err(dev, "query_hca_vport_context for port=%d failed %d\n",
3577 port, err);
3578 return err;
3579 }
3580 dev->mdev->port_caps[port - 1].has_smi =
3581 vport_ctx.has_smi;
3582 } else {
3583 dev->mdev->port_caps[port - 1].has_smi = true;
3584 }
3585 }
3586 }
3587 return 0;
3588}
3589
Eli Cohene126ba92013-07-07 17:25:49 +03003590static void get_ext_port_caps(struct mlx5_ib_dev *dev)
3591{
3592 int port;
3593
Daniel Jurgens508562d2018-01-04 17:25:34 +02003594 for (port = 1; port <= dev->num_ports; port++)
Eli Cohene126ba92013-07-07 17:25:49 +03003595 mlx5_query_ext_port_caps(dev, port);
3596}
3597
Daniel Jurgens32f69e42018-01-04 17:25:36 +02003598static int get_port_caps(struct mlx5_ib_dev *dev, u8 port)
Eli Cohene126ba92013-07-07 17:25:49 +03003599{
3600 struct ib_device_attr *dprops = NULL;
3601 struct ib_port_attr *pprops = NULL;
Dan Carpenterf614fc12015-01-12 11:56:58 +03003602 int err = -ENOMEM;
Matan Barak2528e332015-06-11 16:35:25 +03003603 struct ib_udata uhw = {.inlen = 0, .outlen = 0};
Eli Cohene126ba92013-07-07 17:25:49 +03003604
3605 pprops = kmalloc(sizeof(*pprops), GFP_KERNEL);
3606 if (!pprops)
3607 goto out;
3608
3609 dprops = kmalloc(sizeof(*dprops), GFP_KERNEL);
3610 if (!dprops)
3611 goto out;
3612
Maor Gottliebc43f1112017-01-18 14:10:33 +02003613 err = set_has_smi_cap(dev);
3614 if (err)
3615 goto out;
3616
Matan Barak2528e332015-06-11 16:35:25 +03003617 err = mlx5_ib_query_device(&dev->ib_dev, dprops, &uhw);
Eli Cohene126ba92013-07-07 17:25:49 +03003618 if (err) {
3619 mlx5_ib_warn(dev, "query_device failed %d\n", err);
3620 goto out;
3621 }
3622
Daniel Jurgens32f69e42018-01-04 17:25:36 +02003623 memset(pprops, 0, sizeof(*pprops));
3624 err = mlx5_ib_query_port(&dev->ib_dev, port, pprops);
3625 if (err) {
3626 mlx5_ib_warn(dev, "query_port %d failed %d\n",
3627 port, err);
3628 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03003629 }
3630
Daniel Jurgens32f69e42018-01-04 17:25:36 +02003631 dev->mdev->port_caps[port - 1].pkey_table_len =
3632 dprops->max_pkeys;
3633 dev->mdev->port_caps[port - 1].gid_table_len =
3634 pprops->gid_tbl_len;
3635 mlx5_ib_dbg(dev, "port %d: pkey_table_len %d, gid_table_len %d\n",
3636 port, dprops->max_pkeys, pprops->gid_tbl_len);
3637
Eli Cohene126ba92013-07-07 17:25:49 +03003638out:
3639 kfree(pprops);
3640 kfree(dprops);
3641
3642 return err;
3643}
3644
3645static void destroy_umrc_res(struct mlx5_ib_dev *dev)
3646{
3647 int err;
3648
3649 err = mlx5_mr_cache_cleanup(dev);
3650 if (err)
3651 mlx5_ib_warn(dev, "mr cache cleanup failed\n");
3652
3653 mlx5_ib_destroy_qp(dev->umrc.qp);
Christoph Hellwigadd08d72016-03-03 09:38:22 +01003654 ib_free_cq(dev->umrc.cq);
Eli Cohene126ba92013-07-07 17:25:49 +03003655 ib_dealloc_pd(dev->umrc.pd);
3656}
3657
3658enum {
3659 MAX_UMR_WR = 128,
3660};
3661
3662static int create_umr_res(struct mlx5_ib_dev *dev)
3663{
3664 struct ib_qp_init_attr *init_attr = NULL;
3665 struct ib_qp_attr *attr = NULL;
3666 struct ib_pd *pd;
3667 struct ib_cq *cq;
3668 struct ib_qp *qp;
Eli Cohene126ba92013-07-07 17:25:49 +03003669 int ret;
3670
3671 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
3672 init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL);
3673 if (!attr || !init_attr) {
3674 ret = -ENOMEM;
3675 goto error_0;
3676 }
3677
Christoph Hellwiged082d32016-09-05 12:56:17 +02003678 pd = ib_alloc_pd(&dev->ib_dev, 0);
Eli Cohene126ba92013-07-07 17:25:49 +03003679 if (IS_ERR(pd)) {
3680 mlx5_ib_dbg(dev, "Couldn't create PD for sync UMR QP\n");
3681 ret = PTR_ERR(pd);
3682 goto error_0;
3683 }
3684
Christoph Hellwigadd08d72016-03-03 09:38:22 +01003685 cq = ib_alloc_cq(&dev->ib_dev, NULL, 128, 0, IB_POLL_SOFTIRQ);
Eli Cohene126ba92013-07-07 17:25:49 +03003686 if (IS_ERR(cq)) {
3687 mlx5_ib_dbg(dev, "Couldn't create CQ for sync UMR QP\n");
3688 ret = PTR_ERR(cq);
3689 goto error_2;
3690 }
Eli Cohene126ba92013-07-07 17:25:49 +03003691
3692 init_attr->send_cq = cq;
3693 init_attr->recv_cq = cq;
3694 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
3695 init_attr->cap.max_send_wr = MAX_UMR_WR;
3696 init_attr->cap.max_send_sge = 1;
3697 init_attr->qp_type = MLX5_IB_QPT_REG_UMR;
3698 init_attr->port_num = 1;
3699 qp = mlx5_ib_create_qp(pd, init_attr, NULL);
3700 if (IS_ERR(qp)) {
3701 mlx5_ib_dbg(dev, "Couldn't create sync UMR QP\n");
3702 ret = PTR_ERR(qp);
3703 goto error_3;
3704 }
3705 qp->device = &dev->ib_dev;
3706 qp->real_qp = qp;
3707 qp->uobject = NULL;
3708 qp->qp_type = MLX5_IB_QPT_REG_UMR;
Majd Dibbiny31fde032017-10-30 14:23:13 +02003709 qp->send_cq = init_attr->send_cq;
3710 qp->recv_cq = init_attr->recv_cq;
Eli Cohene126ba92013-07-07 17:25:49 +03003711
3712 attr->qp_state = IB_QPS_INIT;
3713 attr->port_num = 1;
3714 ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_PKEY_INDEX |
3715 IB_QP_PORT, NULL);
3716 if (ret) {
3717 mlx5_ib_dbg(dev, "Couldn't modify UMR QP\n");
3718 goto error_4;
3719 }
3720
3721 memset(attr, 0, sizeof(*attr));
3722 attr->qp_state = IB_QPS_RTR;
3723 attr->path_mtu = IB_MTU_256;
3724
3725 ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
3726 if (ret) {
3727 mlx5_ib_dbg(dev, "Couldn't modify umr QP to rtr\n");
3728 goto error_4;
3729 }
3730
3731 memset(attr, 0, sizeof(*attr));
3732 attr->qp_state = IB_QPS_RTS;
3733 ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
3734 if (ret) {
3735 mlx5_ib_dbg(dev, "Couldn't modify umr QP to rts\n");
3736 goto error_4;
3737 }
3738
3739 dev->umrc.qp = qp;
3740 dev->umrc.cq = cq;
Eli Cohene126ba92013-07-07 17:25:49 +03003741 dev->umrc.pd = pd;
3742
3743 sema_init(&dev->umrc.sem, MAX_UMR_WR);
3744 ret = mlx5_mr_cache_init(dev);
3745 if (ret) {
3746 mlx5_ib_warn(dev, "mr cache init failed %d\n", ret);
3747 goto error_4;
3748 }
3749
3750 kfree(attr);
3751 kfree(init_attr);
3752
3753 return 0;
3754
3755error_4:
3756 mlx5_ib_destroy_qp(qp);
3757
3758error_3:
Christoph Hellwigadd08d72016-03-03 09:38:22 +01003759 ib_free_cq(cq);
Eli Cohene126ba92013-07-07 17:25:49 +03003760
3761error_2:
Eli Cohene126ba92013-07-07 17:25:49 +03003762 ib_dealloc_pd(pd);
3763
3764error_0:
3765 kfree(attr);
3766 kfree(init_attr);
3767 return ret;
3768}
3769
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03003770static u8 mlx5_get_umr_fence(u8 umr_fence_cap)
3771{
3772 switch (umr_fence_cap) {
3773 case MLX5_CAP_UMR_FENCE_NONE:
3774 return MLX5_FENCE_MODE_NONE;
3775 case MLX5_CAP_UMR_FENCE_SMALL:
3776 return MLX5_FENCE_MODE_INITIATOR_SMALL;
3777 default:
3778 return MLX5_FENCE_MODE_STRONG_ORDERING;
3779 }
3780}
3781
Eli Cohene126ba92013-07-07 17:25:49 +03003782static int create_dev_resources(struct mlx5_ib_resources *devr)
3783{
3784 struct ib_srq_init_attr attr;
3785 struct mlx5_ib_dev *dev;
Matan Barakbcf4c1e2015-06-11 16:35:20 +03003786 struct ib_cq_init_attr cq_attr = {.cqe = 1};
Haggai Eran7722f472016-02-29 15:45:07 +02003787 int port;
Eli Cohene126ba92013-07-07 17:25:49 +03003788 int ret = 0;
3789
3790 dev = container_of(devr, struct mlx5_ib_dev, devr);
3791
Haggai Erand16e91d2016-02-29 15:45:05 +02003792 mutex_init(&devr->mutex);
3793
Eli Cohene126ba92013-07-07 17:25:49 +03003794 devr->p0 = mlx5_ib_alloc_pd(&dev->ib_dev, NULL, NULL);
3795 if (IS_ERR(devr->p0)) {
3796 ret = PTR_ERR(devr->p0);
3797 goto error0;
3798 }
3799 devr->p0->device = &dev->ib_dev;
3800 devr->p0->uobject = NULL;
3801 atomic_set(&devr->p0->usecnt, 0);
3802
Matan Barakbcf4c1e2015-06-11 16:35:20 +03003803 devr->c0 = mlx5_ib_create_cq(&dev->ib_dev, &cq_attr, NULL, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03003804 if (IS_ERR(devr->c0)) {
3805 ret = PTR_ERR(devr->c0);
3806 goto error1;
3807 }
3808 devr->c0->device = &dev->ib_dev;
3809 devr->c0->uobject = NULL;
3810 devr->c0->comp_handler = NULL;
3811 devr->c0->event_handler = NULL;
3812 devr->c0->cq_context = NULL;
3813 atomic_set(&devr->c0->usecnt, 0);
3814
3815 devr->x0 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
3816 if (IS_ERR(devr->x0)) {
3817 ret = PTR_ERR(devr->x0);
3818 goto error2;
3819 }
3820 devr->x0->device = &dev->ib_dev;
3821 devr->x0->inode = NULL;
3822 atomic_set(&devr->x0->usecnt, 0);
3823 mutex_init(&devr->x0->tgt_qp_mutex);
3824 INIT_LIST_HEAD(&devr->x0->tgt_qp_list);
3825
3826 devr->x1 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
3827 if (IS_ERR(devr->x1)) {
3828 ret = PTR_ERR(devr->x1);
3829 goto error3;
3830 }
3831 devr->x1->device = &dev->ib_dev;
3832 devr->x1->inode = NULL;
3833 atomic_set(&devr->x1->usecnt, 0);
3834 mutex_init(&devr->x1->tgt_qp_mutex);
3835 INIT_LIST_HEAD(&devr->x1->tgt_qp_list);
3836
3837 memset(&attr, 0, sizeof(attr));
3838 attr.attr.max_sge = 1;
3839 attr.attr.max_wr = 1;
3840 attr.srq_type = IB_SRQT_XRC;
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03003841 attr.ext.cq = devr->c0;
Eli Cohene126ba92013-07-07 17:25:49 +03003842 attr.ext.xrc.xrcd = devr->x0;
3843
3844 devr->s0 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
3845 if (IS_ERR(devr->s0)) {
3846 ret = PTR_ERR(devr->s0);
3847 goto error4;
3848 }
3849 devr->s0->device = &dev->ib_dev;
3850 devr->s0->pd = devr->p0;
3851 devr->s0->uobject = NULL;
3852 devr->s0->event_handler = NULL;
3853 devr->s0->srq_context = NULL;
3854 devr->s0->srq_type = IB_SRQT_XRC;
3855 devr->s0->ext.xrc.xrcd = devr->x0;
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03003856 devr->s0->ext.cq = devr->c0;
Eli Cohene126ba92013-07-07 17:25:49 +03003857 atomic_inc(&devr->s0->ext.xrc.xrcd->usecnt);
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03003858 atomic_inc(&devr->s0->ext.cq->usecnt);
Eli Cohene126ba92013-07-07 17:25:49 +03003859 atomic_inc(&devr->p0->usecnt);
3860 atomic_set(&devr->s0->usecnt, 0);
3861
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03003862 memset(&attr, 0, sizeof(attr));
3863 attr.attr.max_sge = 1;
3864 attr.attr.max_wr = 1;
3865 attr.srq_type = IB_SRQT_BASIC;
3866 devr->s1 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
3867 if (IS_ERR(devr->s1)) {
3868 ret = PTR_ERR(devr->s1);
3869 goto error5;
3870 }
3871 devr->s1->device = &dev->ib_dev;
3872 devr->s1->pd = devr->p0;
3873 devr->s1->uobject = NULL;
3874 devr->s1->event_handler = NULL;
3875 devr->s1->srq_context = NULL;
3876 devr->s1->srq_type = IB_SRQT_BASIC;
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03003877 devr->s1->ext.cq = devr->c0;
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03003878 atomic_inc(&devr->p0->usecnt);
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03003879 atomic_set(&devr->s1->usecnt, 0);
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03003880
Haggai Eran7722f472016-02-29 15:45:07 +02003881 for (port = 0; port < ARRAY_SIZE(devr->ports); ++port) {
3882 INIT_WORK(&devr->ports[port].pkey_change_work,
3883 pkey_change_handler);
3884 devr->ports[port].devr = devr;
3885 }
3886
Eli Cohene126ba92013-07-07 17:25:49 +03003887 return 0;
3888
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03003889error5:
3890 mlx5_ib_destroy_srq(devr->s0);
Eli Cohene126ba92013-07-07 17:25:49 +03003891error4:
3892 mlx5_ib_dealloc_xrcd(devr->x1);
3893error3:
3894 mlx5_ib_dealloc_xrcd(devr->x0);
3895error2:
3896 mlx5_ib_destroy_cq(devr->c0);
3897error1:
3898 mlx5_ib_dealloc_pd(devr->p0);
3899error0:
3900 return ret;
3901}
3902
3903static void destroy_dev_resources(struct mlx5_ib_resources *devr)
3904{
Haggai Eran7722f472016-02-29 15:45:07 +02003905 struct mlx5_ib_dev *dev =
3906 container_of(devr, struct mlx5_ib_dev, devr);
3907 int port;
3908
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03003909 mlx5_ib_destroy_srq(devr->s1);
Eli Cohene126ba92013-07-07 17:25:49 +03003910 mlx5_ib_destroy_srq(devr->s0);
3911 mlx5_ib_dealloc_xrcd(devr->x0);
3912 mlx5_ib_dealloc_xrcd(devr->x1);
3913 mlx5_ib_destroy_cq(devr->c0);
3914 mlx5_ib_dealloc_pd(devr->p0);
Haggai Eran7722f472016-02-29 15:45:07 +02003915
3916 /* Make sure no change P_Key work items are still executing */
3917 for (port = 0; port < dev->num_ports; ++port)
3918 cancel_work_sync(&devr->ports[port].pkey_change_work);
Eli Cohene126ba92013-07-07 17:25:49 +03003919}
3920
Achiad Shochate53505a2015-12-23 18:47:25 +02003921static u32 get_core_cap_flags(struct ib_device *ibdev)
3922{
3923 struct mlx5_ib_dev *dev = to_mdev(ibdev);
3924 enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, 1);
3925 u8 l3_type_cap = MLX5_CAP_ROCE(dev->mdev, l3_type);
3926 u8 roce_version_cap = MLX5_CAP_ROCE(dev->mdev, roce_version);
Daniel Jurgens85c7c012018-01-04 17:25:43 +02003927 bool raw_support = !mlx5_core_mp_enabled(dev->mdev);
Achiad Shochate53505a2015-12-23 18:47:25 +02003928 u32 ret = 0;
3929
3930 if (ll == IB_LINK_LAYER_INFINIBAND)
3931 return RDMA_CORE_PORT_IBA_IB;
3932
Daniel Jurgens85c7c012018-01-04 17:25:43 +02003933 if (raw_support)
3934 ret = RDMA_CORE_PORT_RAW_PACKET;
Or Gerlitz72cd5712017-01-24 13:02:36 +02003935
Achiad Shochate53505a2015-12-23 18:47:25 +02003936 if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV4_CAP))
Or Gerlitz72cd5712017-01-24 13:02:36 +02003937 return ret;
Achiad Shochate53505a2015-12-23 18:47:25 +02003938
3939 if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV6_CAP))
Or Gerlitz72cd5712017-01-24 13:02:36 +02003940 return ret;
Achiad Shochate53505a2015-12-23 18:47:25 +02003941
3942 if (roce_version_cap & MLX5_ROCE_VERSION_1_CAP)
3943 ret |= RDMA_CORE_PORT_IBA_ROCE;
3944
3945 if (roce_version_cap & MLX5_ROCE_VERSION_2_CAP)
3946 ret |= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
3947
3948 return ret;
3949}
3950
Ira Weiny77386132015-05-13 20:02:58 -04003951static int mlx5_port_immutable(struct ib_device *ibdev, u8 port_num,
3952 struct ib_port_immutable *immutable)
3953{
3954 struct ib_port_attr attr;
Or Gerlitzca5b91d2016-11-27 16:51:36 +02003955 struct mlx5_ib_dev *dev = to_mdev(ibdev);
3956 enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, port_num);
Ira Weiny77386132015-05-13 20:02:58 -04003957 int err;
3958
Or Gerlitzc4550c62017-01-24 13:02:39 +02003959 immutable->core_cap_flags = get_core_cap_flags(ibdev);
3960
3961 err = ib_query_port(ibdev, port_num, &attr);
Ira Weiny77386132015-05-13 20:02:58 -04003962 if (err)
3963 return err;
3964
3965 immutable->pkey_tbl_len = attr.pkey_tbl_len;
3966 immutable->gid_tbl_len = attr.gid_tbl_len;
Achiad Shochate53505a2015-12-23 18:47:25 +02003967 immutable->core_cap_flags = get_core_cap_flags(ibdev);
Or Gerlitzca5b91d2016-11-27 16:51:36 +02003968 if ((ll == IB_LINK_LAYER_INFINIBAND) || MLX5_CAP_GEN(dev->mdev, roce))
3969 immutable->max_mad_size = IB_MGMT_MAD_SIZE;
Ira Weiny77386132015-05-13 20:02:58 -04003970
3971 return 0;
3972}
3973
Mark Bloch8e6efa32017-11-06 12:22:13 +00003974static int mlx5_port_rep_immutable(struct ib_device *ibdev, u8 port_num,
3975 struct ib_port_immutable *immutable)
3976{
3977 struct ib_port_attr attr;
3978 int err;
3979
3980 immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET;
3981
3982 err = ib_query_port(ibdev, port_num, &attr);
3983 if (err)
3984 return err;
3985
3986 immutable->pkey_tbl_len = attr.pkey_tbl_len;
3987 immutable->gid_tbl_len = attr.gid_tbl_len;
3988 immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET;
3989
3990 return 0;
3991}
3992
Leon Romanovsky9abb0d12017-06-27 16:49:53 +03003993static void get_dev_fw_str(struct ib_device *ibdev, char *str)
Ira Weinyc7342822016-06-15 02:22:01 -04003994{
3995 struct mlx5_ib_dev *dev =
3996 container_of(ibdev, struct mlx5_ib_dev, ib_dev);
Leon Romanovsky9abb0d12017-06-27 16:49:53 +03003997 snprintf(str, IB_FW_VERSION_NAME_MAX, "%d.%d.%04d",
3998 fw_rev_maj(dev->mdev), fw_rev_min(dev->mdev),
3999 fw_rev_sub(dev->mdev));
Ira Weinyc7342822016-06-15 02:22:01 -04004000}
4001
Or Gerlitz45f95ac2016-11-27 16:51:35 +02004002static int mlx5_eth_lag_init(struct mlx5_ib_dev *dev)
Aviv Heller9ef9c642016-09-18 20:48:01 +03004003{
4004 struct mlx5_core_dev *mdev = dev->mdev;
4005 struct mlx5_flow_namespace *ns = mlx5_get_flow_namespace(mdev,
4006 MLX5_FLOW_NAMESPACE_LAG);
4007 struct mlx5_flow_table *ft;
4008 int err;
4009
4010 if (!ns || !mlx5_lag_is_active(mdev))
4011 return 0;
4012
4013 err = mlx5_cmd_create_vport_lag(mdev);
4014 if (err)
4015 return err;
4016
4017 ft = mlx5_create_lag_demux_flow_table(ns, 0, 0);
4018 if (IS_ERR(ft)) {
4019 err = PTR_ERR(ft);
4020 goto err_destroy_vport_lag;
4021 }
4022
Mark Bloch9a4ca382018-01-16 14:42:35 +00004023 dev->flow_db->lag_demux_ft = ft;
Aviv Heller9ef9c642016-09-18 20:48:01 +03004024 return 0;
4025
4026err_destroy_vport_lag:
4027 mlx5_cmd_destroy_vport_lag(mdev);
4028 return err;
4029}
4030
Or Gerlitz45f95ac2016-11-27 16:51:35 +02004031static void mlx5_eth_lag_cleanup(struct mlx5_ib_dev *dev)
Aviv Heller9ef9c642016-09-18 20:48:01 +03004032{
4033 struct mlx5_core_dev *mdev = dev->mdev;
4034
Mark Bloch9a4ca382018-01-16 14:42:35 +00004035 if (dev->flow_db->lag_demux_ft) {
4036 mlx5_destroy_flow_table(dev->flow_db->lag_demux_ft);
4037 dev->flow_db->lag_demux_ft = NULL;
Aviv Heller9ef9c642016-09-18 20:48:01 +03004038
4039 mlx5_cmd_destroy_vport_lag(mdev);
4040 }
4041}
4042
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +02004043static int mlx5_add_netdev_notifier(struct mlx5_ib_dev *dev, u8 port_num)
Achiad Shochatfc24fc52015-12-23 18:47:17 +02004044{
Achiad Shochate53505a2015-12-23 18:47:25 +02004045 int err;
4046
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +02004047 dev->roce[port_num].nb.notifier_call = mlx5_netdev_event;
4048 err = register_netdevice_notifier(&dev->roce[port_num].nb);
Aviv Heller5ec8c832016-09-18 20:48:00 +03004049 if (err) {
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +02004050 dev->roce[port_num].nb.notifier_call = NULL;
Achiad Shochate53505a2015-12-23 18:47:25 +02004051 return err;
Aviv Heller5ec8c832016-09-18 20:48:00 +03004052 }
Achiad Shochate53505a2015-12-23 18:47:25 +02004053
Or Gerlitzd012f5d2016-11-27 16:51:34 +02004054 return 0;
4055}
Achiad Shochate53505a2015-12-23 18:47:25 +02004056
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +02004057static void mlx5_remove_netdev_notifier(struct mlx5_ib_dev *dev, u8 port_num)
Eli Cohene126ba92013-07-07 17:25:49 +03004058{
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +02004059 if (dev->roce[port_num].nb.notifier_call) {
4060 unregister_netdevice_notifier(&dev->roce[port_num].nb);
4061 dev->roce[port_num].nb.notifier_call = NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03004062 }
4063}
4064
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +02004065static int mlx5_enable_eth(struct mlx5_ib_dev *dev, u8 port_num)
Eli Cohene126ba92013-07-07 17:25:49 +03004066{
Eli Cohene126ba92013-07-07 17:25:49 +03004067 int err;
4068
Or Gerlitzca5b91d2016-11-27 16:51:36 +02004069 if (MLX5_CAP_GEN(dev->mdev, roce)) {
4070 err = mlx5_nic_vport_enable_roce(dev->mdev);
4071 if (err)
Mark Bloch8e6efa32017-11-06 12:22:13 +00004072 return err;
Or Gerlitzca5b91d2016-11-27 16:51:36 +02004073 }
Achiad Shochate53505a2015-12-23 18:47:25 +02004074
Or Gerlitz45f95ac2016-11-27 16:51:35 +02004075 err = mlx5_eth_lag_init(dev);
Aviv Heller9ef9c642016-09-18 20:48:01 +03004076 if (err)
4077 goto err_disable_roce;
4078
Achiad Shochate53505a2015-12-23 18:47:25 +02004079 return 0;
4080
Aviv Heller9ef9c642016-09-18 20:48:01 +03004081err_disable_roce:
Or Gerlitzca5b91d2016-11-27 16:51:36 +02004082 if (MLX5_CAP_GEN(dev->mdev, roce))
4083 mlx5_nic_vport_disable_roce(dev->mdev);
Aviv Heller9ef9c642016-09-18 20:48:01 +03004084
Achiad Shochate53505a2015-12-23 18:47:25 +02004085 return err;
Achiad Shochatfc24fc52015-12-23 18:47:17 +02004086}
4087
Or Gerlitz45f95ac2016-11-27 16:51:35 +02004088static void mlx5_disable_eth(struct mlx5_ib_dev *dev)
Achiad Shochatfc24fc52015-12-23 18:47:17 +02004089{
Or Gerlitz45f95ac2016-11-27 16:51:35 +02004090 mlx5_eth_lag_cleanup(dev);
Or Gerlitzca5b91d2016-11-27 16:51:36 +02004091 if (MLX5_CAP_GEN(dev->mdev, roce))
4092 mlx5_nic_vport_disable_roce(dev->mdev);
Achiad Shochatfc24fc52015-12-23 18:47:17 +02004093}
4094
Parav Pandite1f24a72017-04-16 07:29:29 +03004095struct mlx5_ib_counter {
Kamal Heib7c16f472017-01-18 15:25:09 +02004096 const char *name;
4097 size_t offset;
4098};
4099
4100#define INIT_Q_COUNTER(_name) \
4101 { .name = #_name, .offset = MLX5_BYTE_OFF(query_q_counter_out, _name)}
4102
Parav Pandite1f24a72017-04-16 07:29:29 +03004103static const struct mlx5_ib_counter basic_q_cnts[] = {
Kamal Heib7c16f472017-01-18 15:25:09 +02004104 INIT_Q_COUNTER(rx_write_requests),
4105 INIT_Q_COUNTER(rx_read_requests),
4106 INIT_Q_COUNTER(rx_atomic_requests),
4107 INIT_Q_COUNTER(out_of_buffer),
4108};
4109
Parav Pandite1f24a72017-04-16 07:29:29 +03004110static const struct mlx5_ib_counter out_of_seq_q_cnts[] = {
Kamal Heib7c16f472017-01-18 15:25:09 +02004111 INIT_Q_COUNTER(out_of_sequence),
4112};
4113
Parav Pandite1f24a72017-04-16 07:29:29 +03004114static const struct mlx5_ib_counter retrans_q_cnts[] = {
Kamal Heib7c16f472017-01-18 15:25:09 +02004115 INIT_Q_COUNTER(duplicate_request),
4116 INIT_Q_COUNTER(rnr_nak_retry_err),
4117 INIT_Q_COUNTER(packet_seq_err),
4118 INIT_Q_COUNTER(implied_nak_seq_err),
4119 INIT_Q_COUNTER(local_ack_timeout_err),
4120};
4121
Parav Pandite1f24a72017-04-16 07:29:29 +03004122#define INIT_CONG_COUNTER(_name) \
4123 { .name = #_name, .offset = \
4124 MLX5_BYTE_OFF(query_cong_statistics_out, _name ## _high)}
4125
4126static const struct mlx5_ib_counter cong_cnts[] = {
4127 INIT_CONG_COUNTER(rp_cnp_ignored),
4128 INIT_CONG_COUNTER(rp_cnp_handled),
4129 INIT_CONG_COUNTER(np_ecn_marked_roce_packets),
4130 INIT_CONG_COUNTER(np_cnp_sent),
4131};
4132
Parav Pandit58dcb602017-06-19 07:19:37 +03004133static const struct mlx5_ib_counter extended_err_cnts[] = {
4134 INIT_Q_COUNTER(resp_local_length_error),
4135 INIT_Q_COUNTER(resp_cqe_error),
4136 INIT_Q_COUNTER(req_cqe_error),
4137 INIT_Q_COUNTER(req_remote_invalid_request),
4138 INIT_Q_COUNTER(req_remote_access_errors),
4139 INIT_Q_COUNTER(resp_remote_access_errors),
4140 INIT_Q_COUNTER(resp_cqe_flush_error),
4141 INIT_Q_COUNTER(req_cqe_flush_error),
4142};
4143
Parav Pandite1f24a72017-04-16 07:29:29 +03004144static void mlx5_ib_dealloc_counters(struct mlx5_ib_dev *dev)
Mark Bloch0837e862016-06-17 15:10:55 +03004145{
Daniel Jurgensaac44922018-01-04 17:25:40 +02004146 int i;
Mark Bloch0837e862016-06-17 15:10:55 +03004147
Kamal Heib7c16f472017-01-18 15:25:09 +02004148 for (i = 0; i < dev->num_ports; i++) {
Daniel Jurgensaac44922018-01-04 17:25:40 +02004149 if (dev->port[i].cnts.set_id)
4150 mlx5_core_dealloc_q_counter(dev->mdev,
4151 dev->port[i].cnts.set_id);
Parav Pandite1f24a72017-04-16 07:29:29 +03004152 kfree(dev->port[i].cnts.names);
4153 kfree(dev->port[i].cnts.offsets);
Kamal Heib7c16f472017-01-18 15:25:09 +02004154 }
4155}
4156
Parav Pandite1f24a72017-04-16 07:29:29 +03004157static int __mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev,
4158 struct mlx5_ib_counters *cnts)
Kamal Heib7c16f472017-01-18 15:25:09 +02004159{
4160 u32 num_counters;
4161
4162 num_counters = ARRAY_SIZE(basic_q_cnts);
4163
4164 if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt))
4165 num_counters += ARRAY_SIZE(out_of_seq_q_cnts);
4166
4167 if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters))
4168 num_counters += ARRAY_SIZE(retrans_q_cnts);
Parav Pandit58dcb602017-06-19 07:19:37 +03004169
4170 if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters))
4171 num_counters += ARRAY_SIZE(extended_err_cnts);
4172
Parav Pandite1f24a72017-04-16 07:29:29 +03004173 cnts->num_q_counters = num_counters;
Kamal Heib7c16f472017-01-18 15:25:09 +02004174
Parav Pandite1f24a72017-04-16 07:29:29 +03004175 if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
4176 cnts->num_cong_counters = ARRAY_SIZE(cong_cnts);
4177 num_counters += ARRAY_SIZE(cong_cnts);
4178 }
4179
4180 cnts->names = kcalloc(num_counters, sizeof(cnts->names), GFP_KERNEL);
4181 if (!cnts->names)
Kamal Heib7c16f472017-01-18 15:25:09 +02004182 return -ENOMEM;
4183
Parav Pandite1f24a72017-04-16 07:29:29 +03004184 cnts->offsets = kcalloc(num_counters,
4185 sizeof(cnts->offsets), GFP_KERNEL);
4186 if (!cnts->offsets)
Kamal Heib7c16f472017-01-18 15:25:09 +02004187 goto err_names;
4188
Kamal Heib7c16f472017-01-18 15:25:09 +02004189 return 0;
4190
4191err_names:
Parav Pandite1f24a72017-04-16 07:29:29 +03004192 kfree(cnts->names);
Daniel Jurgensaac44922018-01-04 17:25:40 +02004193 cnts->names = NULL;
Kamal Heib7c16f472017-01-18 15:25:09 +02004194 return -ENOMEM;
4195}
4196
Parav Pandite1f24a72017-04-16 07:29:29 +03004197static void mlx5_ib_fill_counters(struct mlx5_ib_dev *dev,
4198 const char **names,
4199 size_t *offsets)
Kamal Heib7c16f472017-01-18 15:25:09 +02004200{
4201 int i;
4202 int j = 0;
4203
4204 for (i = 0; i < ARRAY_SIZE(basic_q_cnts); i++, j++) {
4205 names[j] = basic_q_cnts[i].name;
4206 offsets[j] = basic_q_cnts[i].offset;
4207 }
4208
4209 if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt)) {
4210 for (i = 0; i < ARRAY_SIZE(out_of_seq_q_cnts); i++, j++) {
4211 names[j] = out_of_seq_q_cnts[i].name;
4212 offsets[j] = out_of_seq_q_cnts[i].offset;
4213 }
4214 }
4215
4216 if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters)) {
4217 for (i = 0; i < ARRAY_SIZE(retrans_q_cnts); i++, j++) {
4218 names[j] = retrans_q_cnts[i].name;
4219 offsets[j] = retrans_q_cnts[i].offset;
4220 }
4221 }
Parav Pandite1f24a72017-04-16 07:29:29 +03004222
Parav Pandit58dcb602017-06-19 07:19:37 +03004223 if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters)) {
4224 for (i = 0; i < ARRAY_SIZE(extended_err_cnts); i++, j++) {
4225 names[j] = extended_err_cnts[i].name;
4226 offsets[j] = extended_err_cnts[i].offset;
4227 }
4228 }
4229
Parav Pandite1f24a72017-04-16 07:29:29 +03004230 if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
4231 for (i = 0; i < ARRAY_SIZE(cong_cnts); i++, j++) {
4232 names[j] = cong_cnts[i].name;
4233 offsets[j] = cong_cnts[i].offset;
4234 }
4235 }
Mark Bloch0837e862016-06-17 15:10:55 +03004236}
4237
Parav Pandite1f24a72017-04-16 07:29:29 +03004238static int mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev)
Mark Bloch0837e862016-06-17 15:10:55 +03004239{
Daniel Jurgensaac44922018-01-04 17:25:40 +02004240 int err = 0;
Mark Bloch0837e862016-06-17 15:10:55 +03004241 int i;
Mark Bloch0837e862016-06-17 15:10:55 +03004242
4243 for (i = 0; i < dev->num_ports; i++) {
Daniel Jurgensaac44922018-01-04 17:25:40 +02004244 err = __mlx5_ib_alloc_counters(dev, &dev->port[i].cnts);
4245 if (err)
4246 goto err_alloc;
Kamal Heib7c16f472017-01-18 15:25:09 +02004247
Daniel Jurgensaac44922018-01-04 17:25:40 +02004248 mlx5_ib_fill_counters(dev, dev->port[i].cnts.names,
4249 dev->port[i].cnts.offsets);
4250
4251 err = mlx5_core_alloc_q_counter(dev->mdev,
4252 &dev->port[i].cnts.set_id);
4253 if (err) {
Mark Bloch0837e862016-06-17 15:10:55 +03004254 mlx5_ib_warn(dev,
4255 "couldn't allocate queue counter for port %d, err %d\n",
Daniel Jurgensaac44922018-01-04 17:25:40 +02004256 i + 1, err);
4257 goto err_alloc;
Mark Bloch0837e862016-06-17 15:10:55 +03004258 }
Daniel Jurgensaac44922018-01-04 17:25:40 +02004259 dev->port[i].cnts.set_id_valid = true;
Mark Bloch0837e862016-06-17 15:10:55 +03004260 }
4261
4262 return 0;
4263
Daniel Jurgensaac44922018-01-04 17:25:40 +02004264err_alloc:
4265 mlx5_ib_dealloc_counters(dev);
4266 return err;
Mark Bloch0837e862016-06-17 15:10:55 +03004267}
4268
Mark Bloch0ad17a82016-06-17 15:10:56 +03004269static struct rdma_hw_stats *mlx5_ib_alloc_hw_stats(struct ib_device *ibdev,
4270 u8 port_num)
4271{
Kamal Heib7c16f472017-01-18 15:25:09 +02004272 struct mlx5_ib_dev *dev = to_mdev(ibdev);
4273 struct mlx5_ib_port *port = &dev->port[port_num - 1];
Mark Bloch0ad17a82016-06-17 15:10:56 +03004274
4275 /* We support only per port stats */
4276 if (port_num == 0)
4277 return NULL;
4278
Parav Pandite1f24a72017-04-16 07:29:29 +03004279 return rdma_alloc_hw_stats_struct(port->cnts.names,
4280 port->cnts.num_q_counters +
4281 port->cnts.num_cong_counters,
Mark Bloch0ad17a82016-06-17 15:10:56 +03004282 RDMA_HW_STATS_DEFAULT_LIFESPAN);
4283}
4284
Daniel Jurgensaac44922018-01-04 17:25:40 +02004285static int mlx5_ib_query_q_counters(struct mlx5_core_dev *mdev,
Parav Pandite1f24a72017-04-16 07:29:29 +03004286 struct mlx5_ib_port *port,
4287 struct rdma_hw_stats *stats)
4288{
4289 int outlen = MLX5_ST_SZ_BYTES(query_q_counter_out);
4290 void *out;
4291 __be32 val;
4292 int ret, i;
4293
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03004294 out = kvzalloc(outlen, GFP_KERNEL);
Parav Pandite1f24a72017-04-16 07:29:29 +03004295 if (!out)
4296 return -ENOMEM;
4297
Daniel Jurgensaac44922018-01-04 17:25:40 +02004298 ret = mlx5_core_query_q_counter(mdev,
Parav Pandite1f24a72017-04-16 07:29:29 +03004299 port->cnts.set_id, 0,
4300 out, outlen);
4301 if (ret)
4302 goto free;
4303
4304 for (i = 0; i < port->cnts.num_q_counters; i++) {
4305 val = *(__be32 *)(out + port->cnts.offsets[i]);
4306 stats->value[i] = (u64)be32_to_cpu(val);
4307 }
4308
4309free:
4310 kvfree(out);
4311 return ret;
4312}
4313
Mark Bloch0ad17a82016-06-17 15:10:56 +03004314static int mlx5_ib_get_hw_stats(struct ib_device *ibdev,
4315 struct rdma_hw_stats *stats,
Kamal Heib7c16f472017-01-18 15:25:09 +02004316 u8 port_num, int index)
Mark Bloch0ad17a82016-06-17 15:10:56 +03004317{
4318 struct mlx5_ib_dev *dev = to_mdev(ibdev);
Kamal Heib7c16f472017-01-18 15:25:09 +02004319 struct mlx5_ib_port *port = &dev->port[port_num - 1];
Daniel Jurgensaac44922018-01-04 17:25:40 +02004320 struct mlx5_core_dev *mdev;
Parav Pandite1f24a72017-04-16 07:29:29 +03004321 int ret, num_counters;
Daniel Jurgensaac44922018-01-04 17:25:40 +02004322 u8 mdev_port_num;
Mark Bloch0ad17a82016-06-17 15:10:56 +03004323
Kamal Heib7c16f472017-01-18 15:25:09 +02004324 if (!stats)
Parav Pandite1f24a72017-04-16 07:29:29 +03004325 return -EINVAL;
Mark Bloch0ad17a82016-06-17 15:10:56 +03004326
Daniel Jurgensaac44922018-01-04 17:25:40 +02004327 num_counters = port->cnts.num_q_counters + port->cnts.num_cong_counters;
4328
4329 /* q_counters are per IB device, query the master mdev */
4330 ret = mlx5_ib_query_q_counters(dev->mdev, port, stats);
Mark Bloch0ad17a82016-06-17 15:10:56 +03004331 if (ret)
Parav Pandite1f24a72017-04-16 07:29:29 +03004332 return ret;
Mark Bloch0ad17a82016-06-17 15:10:56 +03004333
Parav Pandite1f24a72017-04-16 07:29:29 +03004334 if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
Daniel Jurgensaac44922018-01-04 17:25:40 +02004335 mdev = mlx5_ib_get_native_port_mdev(dev, port_num,
4336 &mdev_port_num);
4337 if (!mdev) {
4338 /* If port is not affiliated yet, its in down state
4339 * which doesn't have any counters yet, so it would be
4340 * zero. So no need to read from the HCA.
4341 */
4342 goto done;
4343 }
Majd Dibbiny71a0ff62017-12-21 17:38:26 +02004344 ret = mlx5_lag_query_cong_counters(dev->mdev,
4345 stats->value +
4346 port->cnts.num_q_counters,
4347 port->cnts.num_cong_counters,
4348 port->cnts.offsets +
4349 port->cnts.num_q_counters);
Daniel Jurgensaac44922018-01-04 17:25:40 +02004350
4351 mlx5_ib_put_native_port_mdev(dev, port_num);
Parav Pandite1f24a72017-04-16 07:29:29 +03004352 if (ret)
4353 return ret;
Mark Bloch0ad17a82016-06-17 15:10:56 +03004354 }
Kamal Heib7c16f472017-01-18 15:25:09 +02004355
Daniel Jurgensaac44922018-01-04 17:25:40 +02004356done:
Parav Pandite1f24a72017-04-16 07:29:29 +03004357 return num_counters;
Mark Bloch0ad17a82016-06-17 15:10:56 +03004358}
4359
Niranjana Vishwanathapura8e959602017-06-30 13:14:46 -07004360static void mlx5_ib_free_rdma_netdev(struct net_device *netdev)
4361{
4362 return mlx5_rdma_netdev_free(netdev);
4363}
4364
Erez Shitrit693dfd52017-04-27 17:01:34 +03004365static struct net_device*
4366mlx5_ib_alloc_rdma_netdev(struct ib_device *hca,
4367 u8 port_num,
4368 enum rdma_netdev_t type,
4369 const char *name,
4370 unsigned char name_assign_type,
4371 void (*setup)(struct net_device *))
4372{
Niranjana Vishwanathapura8e959602017-06-30 13:14:46 -07004373 struct net_device *netdev;
4374 struct rdma_netdev *rn;
4375
Erez Shitrit693dfd52017-04-27 17:01:34 +03004376 if (type != RDMA_NETDEV_IPOIB)
4377 return ERR_PTR(-EOPNOTSUPP);
4378
Niranjana Vishwanathapura8e959602017-06-30 13:14:46 -07004379 netdev = mlx5_rdma_netdev_alloc(to_mdev(hca)->mdev, hca,
4380 name, setup);
4381 if (likely(!IS_ERR_OR_NULL(netdev))) {
4382 rn = netdev_priv(netdev);
4383 rn->free_rdma_netdev = mlx5_ib_free_rdma_netdev;
4384 }
4385 return netdev;
Erez Shitrit693dfd52017-04-27 17:01:34 +03004386}
4387
Maor Gottliebfe248c32017-05-30 10:29:14 +03004388static void delay_drop_debugfs_cleanup(struct mlx5_ib_dev *dev)
4389{
4390 if (!dev->delay_drop.dbg)
4391 return;
4392 debugfs_remove_recursive(dev->delay_drop.dbg->dir_debugfs);
4393 kfree(dev->delay_drop.dbg);
4394 dev->delay_drop.dbg = NULL;
4395}
4396
Maor Gottlieb03404e82017-05-30 10:29:13 +03004397static void cancel_delay_drop(struct mlx5_ib_dev *dev)
4398{
4399 if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
4400 return;
4401
4402 cancel_work_sync(&dev->delay_drop.delay_drop_work);
Maor Gottliebfe248c32017-05-30 10:29:14 +03004403 delay_drop_debugfs_cleanup(dev);
4404}
4405
4406static ssize_t delay_drop_timeout_read(struct file *filp, char __user *buf,
4407 size_t count, loff_t *pos)
4408{
4409 struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
4410 char lbuf[20];
4411 int len;
4412
4413 len = snprintf(lbuf, sizeof(lbuf), "%u\n", delay_drop->timeout);
4414 return simple_read_from_buffer(buf, count, pos, lbuf, len);
4415}
4416
4417static ssize_t delay_drop_timeout_write(struct file *filp, const char __user *buf,
4418 size_t count, loff_t *pos)
4419{
4420 struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
4421 u32 timeout;
4422 u32 var;
4423
4424 if (kstrtouint_from_user(buf, count, 0, &var))
4425 return -EFAULT;
4426
4427 timeout = min_t(u32, roundup(var, 100), MLX5_MAX_DELAY_DROP_TIMEOUT_MS *
4428 1000);
4429 if (timeout != var)
4430 mlx5_ib_dbg(delay_drop->dev, "Round delay drop timeout to %u usec\n",
4431 timeout);
4432
4433 delay_drop->timeout = timeout;
4434
4435 return count;
4436}
4437
4438static const struct file_operations fops_delay_drop_timeout = {
4439 .owner = THIS_MODULE,
4440 .open = simple_open,
4441 .write = delay_drop_timeout_write,
4442 .read = delay_drop_timeout_read,
4443};
4444
4445static int delay_drop_debugfs_init(struct mlx5_ib_dev *dev)
4446{
4447 struct mlx5_ib_dbg_delay_drop *dbg;
4448
4449 if (!mlx5_debugfs_root)
4450 return 0;
4451
4452 dbg = kzalloc(sizeof(*dbg), GFP_KERNEL);
4453 if (!dbg)
4454 return -ENOMEM;
4455
Sudip Mukherjeecbafad82017-09-18 12:28:48 +01004456 dev->delay_drop.dbg = dbg;
4457
Maor Gottliebfe248c32017-05-30 10:29:14 +03004458 dbg->dir_debugfs =
4459 debugfs_create_dir("delay_drop",
4460 dev->mdev->priv.dbg_root);
4461 if (!dbg->dir_debugfs)
Sudip Mukherjeecbafad82017-09-18 12:28:48 +01004462 goto out_debugfs;
Maor Gottliebfe248c32017-05-30 10:29:14 +03004463
4464 dbg->events_cnt_debugfs =
4465 debugfs_create_atomic_t("num_timeout_events", 0400,
4466 dbg->dir_debugfs,
4467 &dev->delay_drop.events_cnt);
4468 if (!dbg->events_cnt_debugfs)
4469 goto out_debugfs;
4470
4471 dbg->rqs_cnt_debugfs =
4472 debugfs_create_atomic_t("num_rqs", 0400,
4473 dbg->dir_debugfs,
4474 &dev->delay_drop.rqs_cnt);
4475 if (!dbg->rqs_cnt_debugfs)
4476 goto out_debugfs;
4477
4478 dbg->timeout_debugfs =
4479 debugfs_create_file("timeout", 0600,
4480 dbg->dir_debugfs,
4481 &dev->delay_drop,
4482 &fops_delay_drop_timeout);
4483 if (!dbg->timeout_debugfs)
4484 goto out_debugfs;
4485
4486 return 0;
4487
4488out_debugfs:
4489 delay_drop_debugfs_cleanup(dev);
4490 return -ENOMEM;
Maor Gottlieb03404e82017-05-30 10:29:13 +03004491}
4492
4493static void init_delay_drop(struct mlx5_ib_dev *dev)
4494{
4495 if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
4496 return;
4497
4498 mutex_init(&dev->delay_drop.lock);
4499 dev->delay_drop.dev = dev;
4500 dev->delay_drop.activate = false;
4501 dev->delay_drop.timeout = MLX5_MAX_DELAY_DROP_TIMEOUT_MS * 1000;
4502 INIT_WORK(&dev->delay_drop.delay_drop_work, delay_drop_handler);
Maor Gottliebfe248c32017-05-30 10:29:14 +03004503 atomic_set(&dev->delay_drop.rqs_cnt, 0);
4504 atomic_set(&dev->delay_drop.events_cnt, 0);
4505
4506 if (delay_drop_debugfs_init(dev))
4507 mlx5_ib_warn(dev, "Failed to init delay drop debugfs\n");
Maor Gottlieb03404e82017-05-30 10:29:13 +03004508}
4509
Leon Romanovsky84305d712017-08-17 15:50:53 +03004510static const struct cpumask *
4511mlx5_ib_get_vector_affinity(struct ib_device *ibdev, int comp_vector)
Sagi Grimberg40b24402017-07-13 11:09:42 +03004512{
4513 struct mlx5_ib_dev *dev = to_mdev(ibdev);
4514
4515 return mlx5_get_vector_affinity(dev->mdev, comp_vector);
4516}
4517
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004518/* The mlx5_ib_multiport_mutex should be held when calling this function */
4519static void mlx5_ib_unbind_slave_port(struct mlx5_ib_dev *ibdev,
4520 struct mlx5_ib_multiport_info *mpi)
4521{
4522 u8 port_num = mlx5_core_native_port_num(mpi->mdev) - 1;
4523 struct mlx5_ib_port *port = &ibdev->port[port_num];
4524 int comps;
4525 int err;
4526 int i;
4527
Parav Pandita9e546e2018-01-04 17:25:39 +02004528 mlx5_ib_cleanup_cong_debugfs(ibdev, port_num);
4529
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004530 spin_lock(&port->mp.mpi_lock);
4531 if (!mpi->ibdev) {
4532 spin_unlock(&port->mp.mpi_lock);
4533 return;
4534 }
4535 mpi->ibdev = NULL;
4536
4537 spin_unlock(&port->mp.mpi_lock);
4538 mlx5_remove_netdev_notifier(ibdev, port_num);
4539 spin_lock(&port->mp.mpi_lock);
4540
4541 comps = mpi->mdev_refcnt;
4542 if (comps) {
4543 mpi->unaffiliate = true;
4544 init_completion(&mpi->unref_comp);
4545 spin_unlock(&port->mp.mpi_lock);
4546
4547 for (i = 0; i < comps; i++)
4548 wait_for_completion(&mpi->unref_comp);
4549
4550 spin_lock(&port->mp.mpi_lock);
4551 mpi->unaffiliate = false;
4552 }
4553
4554 port->mp.mpi = NULL;
4555
4556 list_add_tail(&mpi->list, &mlx5_ib_unaffiliated_port_list);
4557
4558 spin_unlock(&port->mp.mpi_lock);
4559
4560 err = mlx5_nic_vport_unaffiliate_multiport(mpi->mdev);
4561
4562 mlx5_ib_dbg(ibdev, "unaffiliated port %d\n", port_num + 1);
4563 /* Log an error, still needed to cleanup the pointers and add
4564 * it back to the list.
4565 */
4566 if (err)
4567 mlx5_ib_err(ibdev, "Failed to unaffiliate port %u\n",
4568 port_num + 1);
4569
4570 ibdev->roce[port_num].last_port_state = IB_PORT_DOWN;
4571}
4572
4573/* The mlx5_ib_multiport_mutex should be held when calling this function */
4574static bool mlx5_ib_bind_slave_port(struct mlx5_ib_dev *ibdev,
4575 struct mlx5_ib_multiport_info *mpi)
4576{
4577 u8 port_num = mlx5_core_native_port_num(mpi->mdev) - 1;
4578 int err;
4579
4580 spin_lock(&ibdev->port[port_num].mp.mpi_lock);
4581 if (ibdev->port[port_num].mp.mpi) {
4582 mlx5_ib_warn(ibdev, "port %d already affiliated.\n",
4583 port_num + 1);
4584 spin_unlock(&ibdev->port[port_num].mp.mpi_lock);
4585 return false;
4586 }
4587
4588 ibdev->port[port_num].mp.mpi = mpi;
4589 mpi->ibdev = ibdev;
4590 spin_unlock(&ibdev->port[port_num].mp.mpi_lock);
4591
4592 err = mlx5_nic_vport_affiliate_multiport(ibdev->mdev, mpi->mdev);
4593 if (err)
4594 goto unbind;
4595
4596 err = get_port_caps(ibdev, mlx5_core_native_port_num(mpi->mdev));
4597 if (err)
4598 goto unbind;
4599
4600 err = mlx5_add_netdev_notifier(ibdev, port_num);
4601 if (err) {
4602 mlx5_ib_err(ibdev, "failed adding netdev notifier for port %u\n",
4603 port_num + 1);
4604 goto unbind;
4605 }
4606
Parav Pandita9e546e2018-01-04 17:25:39 +02004607 err = mlx5_ib_init_cong_debugfs(ibdev, port_num);
4608 if (err)
4609 goto unbind;
4610
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004611 return true;
4612
4613unbind:
4614 mlx5_ib_unbind_slave_port(ibdev, mpi);
4615 return false;
4616}
4617
4618static int mlx5_ib_init_multiport_master(struct mlx5_ib_dev *dev)
4619{
4620 int port_num = mlx5_core_native_port_num(dev->mdev) - 1;
4621 enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev,
4622 port_num + 1);
4623 struct mlx5_ib_multiport_info *mpi;
4624 int err;
4625 int i;
4626
4627 if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
4628 return 0;
4629
4630 err = mlx5_query_nic_vport_system_image_guid(dev->mdev,
4631 &dev->sys_image_guid);
4632 if (err)
4633 return err;
4634
4635 err = mlx5_nic_vport_enable_roce(dev->mdev);
4636 if (err)
4637 return err;
4638
4639 mutex_lock(&mlx5_ib_multiport_mutex);
4640 for (i = 0; i < dev->num_ports; i++) {
4641 bool bound = false;
4642
4643 /* build a stub multiport info struct for the native port. */
4644 if (i == port_num) {
4645 mpi = kzalloc(sizeof(*mpi), GFP_KERNEL);
4646 if (!mpi) {
4647 mutex_unlock(&mlx5_ib_multiport_mutex);
4648 mlx5_nic_vport_disable_roce(dev->mdev);
4649 return -ENOMEM;
4650 }
4651
4652 mpi->is_master = true;
4653 mpi->mdev = dev->mdev;
4654 mpi->sys_image_guid = dev->sys_image_guid;
4655 dev->port[i].mp.mpi = mpi;
4656 mpi->ibdev = dev;
4657 mpi = NULL;
4658 continue;
4659 }
4660
4661 list_for_each_entry(mpi, &mlx5_ib_unaffiliated_port_list,
4662 list) {
4663 if (dev->sys_image_guid == mpi->sys_image_guid &&
4664 (mlx5_core_native_port_num(mpi->mdev) - 1) == i) {
4665 bound = mlx5_ib_bind_slave_port(dev, mpi);
4666 }
4667
4668 if (bound) {
4669 dev_dbg(&mpi->mdev->pdev->dev, "removing port from unaffiliated list.\n");
4670 mlx5_ib_dbg(dev, "port %d bound\n", i + 1);
4671 list_del(&mpi->list);
4672 break;
4673 }
4674 }
4675 if (!bound) {
4676 get_port_caps(dev, i + 1);
4677 mlx5_ib_dbg(dev, "no free port found for port %d\n",
4678 i + 1);
4679 }
4680 }
4681
4682 list_add_tail(&dev->ib_dev_list, &mlx5_ib_dev_list);
4683 mutex_unlock(&mlx5_ib_multiport_mutex);
4684 return err;
4685}
4686
4687static void mlx5_ib_cleanup_multiport_master(struct mlx5_ib_dev *dev)
4688{
4689 int port_num = mlx5_core_native_port_num(dev->mdev) - 1;
4690 enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev,
4691 port_num + 1);
4692 int i;
4693
4694 if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
4695 return;
4696
4697 mutex_lock(&mlx5_ib_multiport_mutex);
4698 for (i = 0; i < dev->num_ports; i++) {
4699 if (dev->port[i].mp.mpi) {
4700 /* Destroy the native port stub */
4701 if (i == port_num) {
4702 kfree(dev->port[i].mp.mpi);
4703 dev->port[i].mp.mpi = NULL;
4704 } else {
4705 mlx5_ib_dbg(dev, "unbinding port_num: %d\n", i + 1);
4706 mlx5_ib_unbind_slave_port(dev, dev->port[i].mp.mpi);
4707 }
4708 }
4709 }
4710
4711 mlx5_ib_dbg(dev, "removing from devlist\n");
4712 list_del(&dev->ib_dev_list);
4713 mutex_unlock(&mlx5_ib_multiport_mutex);
4714
4715 mlx5_nic_vport_disable_roce(dev->mdev);
4716}
4717
Aviad Yehezkelc6475a02018-03-28 09:27:50 +03004718ADD_UVERBS_ATTRIBUTES_SIMPLE(mlx5_ib_flow_action, UVERBS_OBJECT_FLOW_ACTION,
4719 UVERBS_METHOD_FLOW_ACTION_ESP_CREATE,
4720 &UVERBS_ATTR_PTR_IN(MLX5_IB_ATTR_CREATE_FLOW_ACTION_FLAGS,
4721 UVERBS_ATTR_TYPE(u64),
4722 UA_FLAGS(UVERBS_ATTR_SPEC_F_MANDATORY)));
4723
4724#define NUM_TREES 1
Matan Barak8c846602018-03-28 09:27:41 +03004725static int populate_specs_root(struct mlx5_ib_dev *dev)
4726{
4727 const struct uverbs_object_tree_def *default_root[NUM_TREES + 1] = {
4728 uverbs_default_get_objects()};
4729 size_t num_trees = 1;
4730
Aviad Yehezkelc6475a02018-03-28 09:27:50 +03004731 if (mlx5_accel_ipsec_device_caps(dev->mdev) & MLX5_ACCEL_IPSEC_CAP_DEVICE &&
4732 !WARN_ON(num_trees >= ARRAY_SIZE(default_root)))
4733 default_root[num_trees++] = &mlx5_ib_flow_action;
4734
Matan Barak8c846602018-03-28 09:27:41 +03004735 dev->ib_dev.specs_root =
4736 uverbs_alloc_spec_tree(num_trees, default_root);
4737
4738 return PTR_ERR_OR_ZERO(dev->ib_dev.specs_root);
4739}
4740
4741static void depopulate_specs_root(struct mlx5_ib_dev *dev)
4742{
4743 uverbs_free_spec_tree(dev->ib_dev.specs_root);
4744}
4745
Mark Blochb5ca15a2018-01-23 11:16:30 +00004746void mlx5_ib_stage_init_cleanup(struct mlx5_ib_dev *dev)
Eli Cohene126ba92013-07-07 17:25:49 +03004747{
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004748 mlx5_ib_cleanup_multiport_master(dev);
Mark Bloch3cc297d2018-01-01 13:07:03 +02004749#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
4750 cleanup_srcu_struct(&dev->mr_srcu);
4751#endif
Mark Bloch16c19752018-01-01 13:06:58 +02004752 kfree(dev->port);
4753}
4754
Mark Blochb5ca15a2018-01-23 11:16:30 +00004755int mlx5_ib_stage_init_init(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02004756{
4757 struct mlx5_core_dev *mdev = dev->mdev;
Aviv Heller4babcf92016-09-18 20:48:03 +03004758 const char *name;
Eli Cohene126ba92013-07-07 17:25:49 +03004759 int err;
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004760 int i;
Eli Cohene126ba92013-07-07 17:25:49 +03004761
Daniel Jurgens508562d2018-01-04 17:25:34 +02004762 dev->port = kcalloc(dev->num_ports, sizeof(*dev->port),
Mark Bloch0837e862016-06-17 15:10:55 +03004763 GFP_KERNEL);
4764 if (!dev->port)
Mark Bloch16c19752018-01-01 13:06:58 +02004765 return -ENOMEM;
Mark Bloch0837e862016-06-17 15:10:55 +03004766
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004767 for (i = 0; i < dev->num_ports; i++) {
4768 spin_lock_init(&dev->port[i].mp.mpi_lock);
4769 rwlock_init(&dev->roce[i].netdev_lock);
4770 }
4771
4772 err = mlx5_ib_init_multiport_master(dev);
Eli Cohene126ba92013-07-07 17:25:49 +03004773 if (err)
Mark Bloch0837e862016-06-17 15:10:55 +03004774 goto err_free_port;
Eli Cohene126ba92013-07-07 17:25:49 +03004775
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004776 if (!mlx5_core_mp_enabled(mdev)) {
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004777 for (i = 1; i <= dev->num_ports; i++) {
4778 err = get_port_caps(dev, i);
4779 if (err)
4780 break;
4781 }
4782 } else {
4783 err = get_port_caps(dev, mlx5_core_native_port_num(mdev));
4784 }
4785 if (err)
4786 goto err_mp;
4787
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03004788 if (mlx5_use_mad_ifc(dev))
4789 get_ext_port_caps(dev);
Eli Cohene126ba92013-07-07 17:25:49 +03004790
Aviv Heller4babcf92016-09-18 20:48:03 +03004791 if (!mlx5_lag_is_active(mdev))
4792 name = "mlx5_%d";
4793 else
4794 name = "mlx5_bond_%d";
4795
4796 strlcpy(dev->ib_dev.name, name, IB_DEVICE_NAME_MAX);
Eli Cohene126ba92013-07-07 17:25:49 +03004797 dev->ib_dev.owner = THIS_MODULE;
4798 dev->ib_dev.node_type = RDMA_NODE_IB_CA;
Sagi Grimbergc6790aa2015-09-24 10:34:23 +03004799 dev->ib_dev.local_dma_lkey = 0 /* not supported for now */;
Daniel Jurgens508562d2018-01-04 17:25:34 +02004800 dev->ib_dev.phys_port_cnt = dev->num_ports;
Saeed Mahameed233d05d2015-04-02 17:07:32 +03004801 dev->ib_dev.num_comp_vectors =
4802 dev->mdev->priv.eq_table.num_comp_vectors;
Bart Van Assche9b0c2892017-01-20 13:04:21 -08004803 dev->ib_dev.dev.parent = &mdev->pdev->dev;
Eli Cohene126ba92013-07-07 17:25:49 +03004804
Mark Bloch3cc297d2018-01-01 13:07:03 +02004805 mutex_init(&dev->cap_mask_mutex);
4806 INIT_LIST_HEAD(&dev->qp_list);
4807 spin_lock_init(&dev->reset_flow_resource_lock);
4808
4809#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
4810 err = init_srcu_struct(&dev->mr_srcu);
4811 if (err)
4812 goto err_free_port;
4813#endif
4814
Mark Bloch16c19752018-01-01 13:06:58 +02004815 return 0;
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004816err_mp:
4817 mlx5_ib_cleanup_multiport_master(dev);
Mark Bloch16c19752018-01-01 13:06:58 +02004818
4819err_free_port:
4820 kfree(dev->port);
4821
4822 return -ENOMEM;
4823}
4824
Mark Bloch9a4ca382018-01-16 14:42:35 +00004825static int mlx5_ib_stage_flow_db_init(struct mlx5_ib_dev *dev)
4826{
4827 dev->flow_db = kzalloc(sizeof(*dev->flow_db), GFP_KERNEL);
4828
4829 if (!dev->flow_db)
4830 return -ENOMEM;
4831
4832 mutex_init(&dev->flow_db->lock);
4833
4834 return 0;
4835}
4836
Mark Blochb5ca15a2018-01-23 11:16:30 +00004837int mlx5_ib_stage_rep_flow_db_init(struct mlx5_ib_dev *dev)
4838{
4839 struct mlx5_ib_dev *nic_dev;
4840
4841 nic_dev = mlx5_ib_get_uplink_ibdev(dev->mdev->priv.eswitch);
4842
4843 if (!nic_dev)
4844 return -EINVAL;
4845
4846 dev->flow_db = nic_dev->flow_db;
4847
4848 return 0;
4849}
4850
Mark Bloch9a4ca382018-01-16 14:42:35 +00004851static void mlx5_ib_stage_flow_db_cleanup(struct mlx5_ib_dev *dev)
4852{
4853 kfree(dev->flow_db);
4854}
4855
Mark Blochb5ca15a2018-01-23 11:16:30 +00004856int mlx5_ib_stage_caps_init(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02004857{
4858 struct mlx5_core_dev *mdev = dev->mdev;
Mark Bloch16c19752018-01-01 13:06:58 +02004859 int err;
4860
Eli Cohene126ba92013-07-07 17:25:49 +03004861 dev->ib_dev.uverbs_abi_ver = MLX5_IB_UVERBS_ABI_VERSION;
4862 dev->ib_dev.uverbs_cmd_mask =
4863 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
4864 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
4865 (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
4866 (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
4867 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
Moni Shoua41c450f2016-11-23 08:23:26 +02004868 (1ull << IB_USER_VERBS_CMD_CREATE_AH) |
4869 (1ull << IB_USER_VERBS_CMD_DESTROY_AH) |
Eli Cohene126ba92013-07-07 17:25:49 +03004870 (1ull << IB_USER_VERBS_CMD_REG_MR) |
Noa Osherovich56e11d62016-02-29 16:46:51 +02004871 (1ull << IB_USER_VERBS_CMD_REREG_MR) |
Eli Cohene126ba92013-07-07 17:25:49 +03004872 (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
4873 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
4874 (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
4875 (1ull << IB_USER_VERBS_CMD_RESIZE_CQ) |
4876 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
4877 (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
4878 (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
4879 (1ull << IB_USER_VERBS_CMD_QUERY_QP) |
4880 (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
4881 (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST) |
4882 (1ull << IB_USER_VERBS_CMD_DETACH_MCAST) |
4883 (1ull << IB_USER_VERBS_CMD_CREATE_SRQ) |
4884 (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ) |
4885 (1ull << IB_USER_VERBS_CMD_QUERY_SRQ) |
4886 (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ) |
4887 (1ull << IB_USER_VERBS_CMD_CREATE_XSRQ) |
4888 (1ull << IB_USER_VERBS_CMD_OPEN_QP);
Haggai Eran1707cb42015-02-08 13:28:52 +02004889 dev->ib_dev.uverbs_ex_cmd_mask =
Matan Barakd4584dd2016-01-28 17:51:46 +02004890 (1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE) |
4891 (1ull << IB_USER_VERBS_EX_CMD_CREATE_CQ) |
Bodong Wang7d29f342016-12-01 13:43:16 +02004892 (1ull << IB_USER_VERBS_EX_CMD_CREATE_QP) |
Yonatan Cohenb0e9df62017-11-13 10:51:15 +02004893 (1ull << IB_USER_VERBS_EX_CMD_MODIFY_QP) |
4894 (1ull << IB_USER_VERBS_EX_CMD_MODIFY_CQ);
Eli Cohene126ba92013-07-07 17:25:49 +03004895
4896 dev->ib_dev.query_device = mlx5_ib_query_device;
Achiad Shochatebd61f62015-12-23 18:47:16 +02004897 dev->ib_dev.get_link_layer = mlx5_ib_port_link_layer;
Eli Cohene126ba92013-07-07 17:25:49 +03004898 dev->ib_dev.query_gid = mlx5_ib_query_gid;
Achiad Shochat3cca2602015-12-23 18:47:23 +02004899 dev->ib_dev.add_gid = mlx5_ib_add_gid;
4900 dev->ib_dev.del_gid = mlx5_ib_del_gid;
Eli Cohene126ba92013-07-07 17:25:49 +03004901 dev->ib_dev.query_pkey = mlx5_ib_query_pkey;
4902 dev->ib_dev.modify_device = mlx5_ib_modify_device;
4903 dev->ib_dev.modify_port = mlx5_ib_modify_port;
4904 dev->ib_dev.alloc_ucontext = mlx5_ib_alloc_ucontext;
4905 dev->ib_dev.dealloc_ucontext = mlx5_ib_dealloc_ucontext;
4906 dev->ib_dev.mmap = mlx5_ib_mmap;
4907 dev->ib_dev.alloc_pd = mlx5_ib_alloc_pd;
4908 dev->ib_dev.dealloc_pd = mlx5_ib_dealloc_pd;
4909 dev->ib_dev.create_ah = mlx5_ib_create_ah;
4910 dev->ib_dev.query_ah = mlx5_ib_query_ah;
4911 dev->ib_dev.destroy_ah = mlx5_ib_destroy_ah;
4912 dev->ib_dev.create_srq = mlx5_ib_create_srq;
4913 dev->ib_dev.modify_srq = mlx5_ib_modify_srq;
4914 dev->ib_dev.query_srq = mlx5_ib_query_srq;
4915 dev->ib_dev.destroy_srq = mlx5_ib_destroy_srq;
4916 dev->ib_dev.post_srq_recv = mlx5_ib_post_srq_recv;
4917 dev->ib_dev.create_qp = mlx5_ib_create_qp;
4918 dev->ib_dev.modify_qp = mlx5_ib_modify_qp;
4919 dev->ib_dev.query_qp = mlx5_ib_query_qp;
4920 dev->ib_dev.destroy_qp = mlx5_ib_destroy_qp;
4921 dev->ib_dev.post_send = mlx5_ib_post_send;
4922 dev->ib_dev.post_recv = mlx5_ib_post_recv;
4923 dev->ib_dev.create_cq = mlx5_ib_create_cq;
4924 dev->ib_dev.modify_cq = mlx5_ib_modify_cq;
4925 dev->ib_dev.resize_cq = mlx5_ib_resize_cq;
4926 dev->ib_dev.destroy_cq = mlx5_ib_destroy_cq;
4927 dev->ib_dev.poll_cq = mlx5_ib_poll_cq;
4928 dev->ib_dev.req_notify_cq = mlx5_ib_arm_cq;
4929 dev->ib_dev.get_dma_mr = mlx5_ib_get_dma_mr;
4930 dev->ib_dev.reg_user_mr = mlx5_ib_reg_user_mr;
Noa Osherovich56e11d62016-02-29 16:46:51 +02004931 dev->ib_dev.rereg_user_mr = mlx5_ib_rereg_user_mr;
Eli Cohene126ba92013-07-07 17:25:49 +03004932 dev->ib_dev.dereg_mr = mlx5_ib_dereg_mr;
4933 dev->ib_dev.attach_mcast = mlx5_ib_mcg_attach;
4934 dev->ib_dev.detach_mcast = mlx5_ib_mcg_detach;
4935 dev->ib_dev.process_mad = mlx5_ib_process_mad;
Sagi Grimberg9bee1782015-07-30 10:32:35 +03004936 dev->ib_dev.alloc_mr = mlx5_ib_alloc_mr;
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004937 dev->ib_dev.map_mr_sg = mlx5_ib_map_mr_sg;
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02004938 dev->ib_dev.check_mr_status = mlx5_ib_check_mr_status;
Ira Weinyc7342822016-06-15 02:22:01 -04004939 dev->ib_dev.get_dev_fw_str = get_dev_fw_str;
Sagi Grimberg40b24402017-07-13 11:09:42 +03004940 dev->ib_dev.get_vector_affinity = mlx5_ib_get_vector_affinity;
Niranjana Vishwanathapura8e959602017-06-30 13:14:46 -07004941 if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads))
Alex Vesker022d0382017-06-14 09:59:06 +03004942 dev->ib_dev.alloc_rdma_netdev = mlx5_ib_alloc_rdma_netdev;
Niranjana Vishwanathapura8e959602017-06-30 13:14:46 -07004943
Eli Coheneff901d2016-03-11 22:58:42 +02004944 if (mlx5_core_is_pf(mdev)) {
4945 dev->ib_dev.get_vf_config = mlx5_ib_get_vf_config;
4946 dev->ib_dev.set_vf_link_state = mlx5_ib_set_vf_link_state;
4947 dev->ib_dev.get_vf_stats = mlx5_ib_get_vf_stats;
4948 dev->ib_dev.set_vf_guid = mlx5_ib_set_vf_guid;
4949 }
Eli Cohene126ba92013-07-07 17:25:49 +03004950
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03004951 dev->ib_dev.disassociate_ucontext = mlx5_ib_disassociate_ucontext;
4952
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03004953 dev->umr_fence = mlx5_get_umr_fence(MLX5_CAP_GEN(mdev, umr_fence));
4954
Matan Barakd2370e02016-02-29 18:05:30 +02004955 if (MLX5_CAP_GEN(mdev, imaicl)) {
4956 dev->ib_dev.alloc_mw = mlx5_ib_alloc_mw;
4957 dev->ib_dev.dealloc_mw = mlx5_ib_dealloc_mw;
4958 dev->ib_dev.uverbs_cmd_mask |=
4959 (1ull << IB_USER_VERBS_CMD_ALLOC_MW) |
4960 (1ull << IB_USER_VERBS_CMD_DEALLOC_MW);
4961 }
4962
Saeed Mahameed938fe832015-05-28 22:28:41 +03004963 if (MLX5_CAP_GEN(mdev, xrc)) {
Eli Cohene126ba92013-07-07 17:25:49 +03004964 dev->ib_dev.alloc_xrcd = mlx5_ib_alloc_xrcd;
4965 dev->ib_dev.dealloc_xrcd = mlx5_ib_dealloc_xrcd;
4966 dev->ib_dev.uverbs_cmd_mask |=
4967 (1ull << IB_USER_VERBS_CMD_OPEN_XRCD) |
4968 (1ull << IB_USER_VERBS_CMD_CLOSE_XRCD);
4969 }
4970
Yishai Hadas81e30882017-06-08 16:15:09 +03004971 dev->ib_dev.create_flow = mlx5_ib_create_flow;
4972 dev->ib_dev.destroy_flow = mlx5_ib_destroy_flow;
4973 dev->ib_dev.uverbs_ex_cmd_mask |=
4974 (1ull << IB_USER_VERBS_EX_CMD_CREATE_FLOW) |
4975 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_FLOW);
Aviad Yehezkelc6475a02018-03-28 09:27:50 +03004976 dev->ib_dev.create_flow_action_esp = mlx5_ib_create_flow_action_esp;
4977 dev->ib_dev.destroy_flow_action = mlx5_ib_destroy_flow_action;
Matan Barak349705c2018-03-28 09:27:51 +03004978 dev->ib_dev.modify_flow_action_esp = mlx5_ib_modify_flow_action_esp;
Matan Barak0ede73b2018-03-19 15:02:34 +02004979 dev->ib_dev.driver_id = RDMA_DRIVER_MLX5;
Yishai Hadas81e30882017-06-08 16:15:09 +03004980
Eli Cohene126ba92013-07-07 17:25:49 +03004981 err = init_node_data(dev);
4982 if (err)
Mark Bloch16c19752018-01-01 13:06:58 +02004983 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03004984
Mark Blochc8b89922018-01-01 13:07:02 +02004985 if ((MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_ETH) &&
Jason Gunthorpee7996a92018-01-29 13:26:40 -07004986 (MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) ||
4987 MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
Mark Blochc8b89922018-01-01 13:07:02 +02004988 mutex_init(&dev->lb_mutex);
4989
Mark Bloch16c19752018-01-01 13:06:58 +02004990 return 0;
4991}
4992
Mark Bloch8e6efa32017-11-06 12:22:13 +00004993static int mlx5_ib_stage_non_default_cb(struct mlx5_ib_dev *dev)
4994{
4995 dev->ib_dev.get_port_immutable = mlx5_port_immutable;
4996 dev->ib_dev.query_port = mlx5_ib_query_port;
4997
4998 return 0;
4999}
5000
Mark Blochb5ca15a2018-01-23 11:16:30 +00005001int mlx5_ib_stage_rep_non_default_cb(struct mlx5_ib_dev *dev)
Mark Bloch8e6efa32017-11-06 12:22:13 +00005002{
5003 dev->ib_dev.get_port_immutable = mlx5_port_rep_immutable;
5004 dev->ib_dev.query_port = mlx5_ib_rep_query_port;
5005
5006 return 0;
5007}
5008
5009static int mlx5_ib_stage_common_roce_init(struct mlx5_ib_dev *dev,
5010 u8 port_num)
5011{
5012 int i;
5013
5014 for (i = 0; i < dev->num_ports; i++) {
5015 dev->roce[i].dev = dev;
5016 dev->roce[i].native_port_num = i + 1;
5017 dev->roce[i].last_port_state = IB_PORT_DOWN;
5018 }
5019
5020 dev->ib_dev.get_netdev = mlx5_ib_get_netdev;
5021 dev->ib_dev.create_wq = mlx5_ib_create_wq;
5022 dev->ib_dev.modify_wq = mlx5_ib_modify_wq;
5023 dev->ib_dev.destroy_wq = mlx5_ib_destroy_wq;
5024 dev->ib_dev.create_rwq_ind_table = mlx5_ib_create_rwq_ind_table;
5025 dev->ib_dev.destroy_rwq_ind_table = mlx5_ib_destroy_rwq_ind_table;
5026
5027 dev->ib_dev.uverbs_ex_cmd_mask |=
5028 (1ull << IB_USER_VERBS_EX_CMD_CREATE_WQ) |
5029 (1ull << IB_USER_VERBS_EX_CMD_MODIFY_WQ) |
5030 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_WQ) |
5031 (1ull << IB_USER_VERBS_EX_CMD_CREATE_RWQ_IND_TBL) |
5032 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_RWQ_IND_TBL);
5033
5034 return mlx5_add_netdev_notifier(dev, port_num);
5035}
5036
5037static void mlx5_ib_stage_common_roce_cleanup(struct mlx5_ib_dev *dev)
5038{
5039 u8 port_num = mlx5_core_native_port_num(dev->mdev) - 1;
5040
5041 mlx5_remove_netdev_notifier(dev, port_num);
5042}
5043
5044int mlx5_ib_stage_rep_roce_init(struct mlx5_ib_dev *dev)
5045{
5046 struct mlx5_core_dev *mdev = dev->mdev;
5047 enum rdma_link_layer ll;
5048 int port_type_cap;
5049 int err = 0;
5050 u8 port_num;
5051
5052 port_num = mlx5_core_native_port_num(dev->mdev) - 1;
5053 port_type_cap = MLX5_CAP_GEN(mdev, port_type);
5054 ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
5055
5056 if (ll == IB_LINK_LAYER_ETHERNET)
5057 err = mlx5_ib_stage_common_roce_init(dev, port_num);
5058
5059 return err;
5060}
5061
5062void mlx5_ib_stage_rep_roce_cleanup(struct mlx5_ib_dev *dev)
5063{
5064 mlx5_ib_stage_common_roce_cleanup(dev);
5065}
5066
Mark Bloch16c19752018-01-01 13:06:58 +02005067static int mlx5_ib_stage_roce_init(struct mlx5_ib_dev *dev)
5068{
5069 struct mlx5_core_dev *mdev = dev->mdev;
5070 enum rdma_link_layer ll;
5071 int port_type_cap;
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005072 u8 port_num;
Mark Bloch16c19752018-01-01 13:06:58 +02005073 int err;
5074
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005075 port_num = mlx5_core_native_port_num(dev->mdev) - 1;
Mark Bloch16c19752018-01-01 13:06:58 +02005076 port_type_cap = MLX5_CAP_GEN(mdev, port_type);
5077 ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
5078
Achiad Shochatfc24fc52015-12-23 18:47:17 +02005079 if (ll == IB_LINK_LAYER_ETHERNET) {
Mark Bloch8e6efa32017-11-06 12:22:13 +00005080 err = mlx5_ib_stage_common_roce_init(dev, port_num);
Achiad Shochatfc24fc52015-12-23 18:47:17 +02005081 if (err)
Mark Bloch16c19752018-01-01 13:06:58 +02005082 return err;
Mark Bloch8e6efa32017-11-06 12:22:13 +00005083
5084 err = mlx5_enable_eth(dev, port_num);
5085 if (err)
5086 goto cleanup;
Achiad Shochatfc24fc52015-12-23 18:47:17 +02005087 }
5088
Mark Bloch16c19752018-01-01 13:06:58 +02005089 return 0;
Mark Bloch8e6efa32017-11-06 12:22:13 +00005090cleanup:
5091 mlx5_ib_stage_common_roce_cleanup(dev);
5092
5093 return err;
Mark Bloch16c19752018-01-01 13:06:58 +02005094}
Eli Cohene126ba92013-07-07 17:25:49 +03005095
Mark Bloch16c19752018-01-01 13:06:58 +02005096static void mlx5_ib_stage_roce_cleanup(struct mlx5_ib_dev *dev)
5097{
5098 struct mlx5_core_dev *mdev = dev->mdev;
5099 enum rdma_link_layer ll;
5100 int port_type_cap;
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005101 u8 port_num;
Eli Cohene126ba92013-07-07 17:25:49 +03005102
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005103 port_num = mlx5_core_native_port_num(dev->mdev) - 1;
Mark Bloch16c19752018-01-01 13:06:58 +02005104 port_type_cap = MLX5_CAP_GEN(mdev, port_type);
5105 ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
5106
5107 if (ll == IB_LINK_LAYER_ETHERNET) {
5108 mlx5_disable_eth(dev);
Mark Bloch8e6efa32017-11-06 12:22:13 +00005109 mlx5_ib_stage_common_roce_cleanup(dev);
Kamal Heib45bded22017-01-18 14:10:32 +02005110 }
Mark Bloch16c19752018-01-01 13:06:58 +02005111}
Haggai Eran6aec21f2014-12-11 17:04:23 +02005112
Mark Blochb5ca15a2018-01-23 11:16:30 +00005113int mlx5_ib_stage_dev_res_init(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02005114{
5115 return create_dev_resources(&dev->devr);
5116}
Parav Pandit4a2da0b2017-05-30 10:05:15 +03005117
Mark Blochb5ca15a2018-01-23 11:16:30 +00005118void mlx5_ib_stage_dev_res_cleanup(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02005119{
5120 destroy_dev_resources(&dev->devr);
5121}
5122
5123static int mlx5_ib_stage_odp_init(struct mlx5_ib_dev *dev)
5124{
Mark Bloch07321b32018-01-01 13:07:00 +02005125 mlx5_ib_internal_fill_odp_caps(dev);
5126
Mark Bloch16c19752018-01-01 13:06:58 +02005127 return mlx5_ib_odp_init_one(dev);
5128}
5129
Mark Blochb5ca15a2018-01-23 11:16:30 +00005130int mlx5_ib_stage_counters_init(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02005131{
Mark Bloch5e1e7612018-01-01 13:07:01 +02005132 if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt)) {
5133 dev->ib_dev.get_hw_stats = mlx5_ib_get_hw_stats;
5134 dev->ib_dev.alloc_hw_stats = mlx5_ib_alloc_hw_stats;
5135
5136 return mlx5_ib_alloc_counters(dev);
5137 }
Mark Bloch16c19752018-01-01 13:06:58 +02005138
5139 return 0;
5140}
5141
Mark Blochb5ca15a2018-01-23 11:16:30 +00005142void mlx5_ib_stage_counters_cleanup(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02005143{
5144 if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt))
5145 mlx5_ib_dealloc_counters(dev);
5146}
5147
5148static int mlx5_ib_stage_cong_debugfs_init(struct mlx5_ib_dev *dev)
5149{
Parav Pandita9e546e2018-01-04 17:25:39 +02005150 return mlx5_ib_init_cong_debugfs(dev,
5151 mlx5_core_native_port_num(dev->mdev) - 1);
Mark Bloch16c19752018-01-01 13:06:58 +02005152}
5153
5154static void mlx5_ib_stage_cong_debugfs_cleanup(struct mlx5_ib_dev *dev)
5155{
Parav Pandita9e546e2018-01-04 17:25:39 +02005156 mlx5_ib_cleanup_cong_debugfs(dev,
5157 mlx5_core_native_port_num(dev->mdev) - 1);
Mark Bloch16c19752018-01-01 13:06:58 +02005158}
5159
5160static int mlx5_ib_stage_uar_init(struct mlx5_ib_dev *dev)
5161{
Eli Cohen5fe9dec2017-01-03 23:55:25 +02005162 dev->mdev->priv.uar = mlx5_get_uars_page(dev->mdev);
5163 if (!dev->mdev->priv.uar)
Mark Bloch16c19752018-01-01 13:06:58 +02005164 return -ENOMEM;
5165 return 0;
5166}
5167
5168static void mlx5_ib_stage_uar_cleanup(struct mlx5_ib_dev *dev)
5169{
5170 mlx5_put_uars_page(dev->mdev, dev->mdev->priv.uar);
5171}
5172
Mark Blochb5ca15a2018-01-23 11:16:30 +00005173int mlx5_ib_stage_bfrag_init(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02005174{
5175 int err;
Eli Cohen5fe9dec2017-01-03 23:55:25 +02005176
5177 err = mlx5_alloc_bfreg(dev->mdev, &dev->bfreg, false, false);
5178 if (err)
Mark Bloch16c19752018-01-01 13:06:58 +02005179 return err;
Eli Cohen5fe9dec2017-01-03 23:55:25 +02005180
5181 err = mlx5_alloc_bfreg(dev->mdev, &dev->fp_bfreg, false, true);
5182 if (err)
Mark Bloch16c19752018-01-01 13:06:58 +02005183 mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
Eli Cohen5fe9dec2017-01-03 23:55:25 +02005184
Mark Bloch16c19752018-01-01 13:06:58 +02005185 return err;
5186}
Mark Bloch0837e862016-06-17 15:10:55 +03005187
Mark Blochb5ca15a2018-01-23 11:16:30 +00005188void mlx5_ib_stage_bfrag_cleanup(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02005189{
5190 mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
5191 mlx5_free_bfreg(dev->mdev, &dev->bfreg);
5192}
Eli Cohene126ba92013-07-07 17:25:49 +03005193
Matan Barak8c846602018-03-28 09:27:41 +03005194static int mlx5_ib_stage_populate_specs(struct mlx5_ib_dev *dev)
5195{
5196 return populate_specs_root(dev);
5197}
5198
Mark Blochb5ca15a2018-01-23 11:16:30 +00005199int mlx5_ib_stage_ib_reg_init(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02005200{
5201 return ib_register_device(&dev->ib_dev, NULL);
5202}
5203
Matan Barak8c846602018-03-28 09:27:41 +03005204static void mlx5_ib_stage_depopulate_specs(struct mlx5_ib_dev *dev)
5205{
5206 depopulate_specs_root(dev);
5207}
5208
Doug Ledford2d873442018-03-14 18:49:12 -04005209void mlx5_ib_stage_pre_ib_reg_umr_cleanup(struct mlx5_ib_dev *dev)
Mark Bloch42cea832018-03-14 09:14:15 +02005210{
5211 destroy_umrc_res(dev);
5212}
5213
Mark Blochb5ca15a2018-01-23 11:16:30 +00005214void mlx5_ib_stage_ib_reg_cleanup(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02005215{
5216 ib_unregister_device(&dev->ib_dev);
5217}
5218
Doug Ledford2d873442018-03-14 18:49:12 -04005219int mlx5_ib_stage_post_ib_reg_umr_init(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02005220{
5221 return create_umr_res(dev);
5222}
5223
Mark Bloch16c19752018-01-01 13:06:58 +02005224static int mlx5_ib_stage_delay_drop_init(struct mlx5_ib_dev *dev)
5225{
Maor Gottlieb03404e82017-05-30 10:29:13 +03005226 init_delay_drop(dev);
5227
Mark Bloch16c19752018-01-01 13:06:58 +02005228 return 0;
5229}
5230
5231static void mlx5_ib_stage_delay_drop_cleanup(struct mlx5_ib_dev *dev)
5232{
5233 cancel_delay_drop(dev);
5234}
5235
Mark Blochb5ca15a2018-01-23 11:16:30 +00005236int mlx5_ib_stage_class_attr_init(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02005237{
5238 int err;
5239 int i;
5240
Eli Cohene126ba92013-07-07 17:25:49 +03005241 for (i = 0; i < ARRAY_SIZE(mlx5_class_attributes); i++) {
Wei Yongjun281d1a92013-07-30 07:54:26 +08005242 err = device_create_file(&dev->ib_dev.dev,
5243 mlx5_class_attributes[i]);
5244 if (err)
Mark Bloch16c19752018-01-01 13:06:58 +02005245 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03005246 }
5247
Mark Bloch16c19752018-01-01 13:06:58 +02005248 return 0;
5249}
5250
Mark Blochfc385b72018-01-16 14:34:48 +00005251static int mlx5_ib_stage_rep_reg_init(struct mlx5_ib_dev *dev)
5252{
5253 mlx5_ib_register_vport_reps(dev);
5254
5255 return 0;
5256}
5257
5258static void mlx5_ib_stage_rep_reg_cleanup(struct mlx5_ib_dev *dev)
5259{
5260 mlx5_ib_unregister_vport_reps(dev);
5261}
5262
Mark Blochb5ca15a2018-01-23 11:16:30 +00005263void __mlx5_ib_remove(struct mlx5_ib_dev *dev,
5264 const struct mlx5_ib_profile *profile,
5265 int stage)
Mark Bloch16c19752018-01-01 13:06:58 +02005266{
5267 /* Number of stages to cleanup */
5268 while (stage) {
5269 stage--;
5270 if (profile->stage[stage].cleanup)
5271 profile->stage[stage].cleanup(dev);
5272 }
5273
5274 ib_dealloc_device((struct ib_device *)dev);
5275}
5276
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005277static void *mlx5_ib_add_slave_port(struct mlx5_core_dev *mdev, u8 port_num);
5278
Mark Blochb5ca15a2018-01-23 11:16:30 +00005279void *__mlx5_ib_add(struct mlx5_ib_dev *dev,
5280 const struct mlx5_ib_profile *profile)
Mark Bloch16c19752018-01-01 13:06:58 +02005281{
Mark Bloch16c19752018-01-01 13:06:58 +02005282 int err;
5283 int i;
5284
5285 printk_once(KERN_INFO "%s", mlx5_version);
5286
Mark Bloch16c19752018-01-01 13:06:58 +02005287 for (i = 0; i < MLX5_IB_STAGE_MAX; i++) {
5288 if (profile->stage[i].init) {
5289 err = profile->stage[i].init(dev);
5290 if (err)
5291 goto err_out;
5292 }
5293 }
5294
5295 dev->profile = profile;
Eli Cohene126ba92013-07-07 17:25:49 +03005296 dev->ib_active = true;
5297
Jack Morgenstein9603b612014-07-28 23:30:22 +03005298 return dev;
Eli Cohene126ba92013-07-07 17:25:49 +03005299
Mark Bloch16c19752018-01-01 13:06:58 +02005300err_out:
5301 __mlx5_ib_remove(dev, profile, i);
Eli Cohene126ba92013-07-07 17:25:49 +03005302
Jack Morgenstein9603b612014-07-28 23:30:22 +03005303 return NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03005304}
5305
Mark Bloch16c19752018-01-01 13:06:58 +02005306static const struct mlx5_ib_profile pf_profile = {
5307 STAGE_CREATE(MLX5_IB_STAGE_INIT,
5308 mlx5_ib_stage_init_init,
5309 mlx5_ib_stage_init_cleanup),
Mark Bloch9a4ca382018-01-16 14:42:35 +00005310 STAGE_CREATE(MLX5_IB_STAGE_FLOW_DB,
5311 mlx5_ib_stage_flow_db_init,
5312 mlx5_ib_stage_flow_db_cleanup),
Mark Bloch16c19752018-01-01 13:06:58 +02005313 STAGE_CREATE(MLX5_IB_STAGE_CAPS,
5314 mlx5_ib_stage_caps_init,
5315 NULL),
Mark Bloch8e6efa32017-11-06 12:22:13 +00005316 STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB,
5317 mlx5_ib_stage_non_default_cb,
5318 NULL),
Mark Bloch16c19752018-01-01 13:06:58 +02005319 STAGE_CREATE(MLX5_IB_STAGE_ROCE,
5320 mlx5_ib_stage_roce_init,
5321 mlx5_ib_stage_roce_cleanup),
5322 STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES,
5323 mlx5_ib_stage_dev_res_init,
5324 mlx5_ib_stage_dev_res_cleanup),
5325 STAGE_CREATE(MLX5_IB_STAGE_ODP,
5326 mlx5_ib_stage_odp_init,
Mark Bloch3cc297d2018-01-01 13:07:03 +02005327 NULL),
Mark Bloch16c19752018-01-01 13:06:58 +02005328 STAGE_CREATE(MLX5_IB_STAGE_COUNTERS,
5329 mlx5_ib_stage_counters_init,
5330 mlx5_ib_stage_counters_cleanup),
5331 STAGE_CREATE(MLX5_IB_STAGE_CONG_DEBUGFS,
5332 mlx5_ib_stage_cong_debugfs_init,
5333 mlx5_ib_stage_cong_debugfs_cleanup),
5334 STAGE_CREATE(MLX5_IB_STAGE_UAR,
5335 mlx5_ib_stage_uar_init,
5336 mlx5_ib_stage_uar_cleanup),
5337 STAGE_CREATE(MLX5_IB_STAGE_BFREG,
5338 mlx5_ib_stage_bfrag_init,
5339 mlx5_ib_stage_bfrag_cleanup),
Mark Bloch42cea832018-03-14 09:14:15 +02005340 STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR,
5341 NULL,
5342 mlx5_ib_stage_pre_ib_reg_umr_cleanup),
Matan Barak8c846602018-03-28 09:27:41 +03005343 STAGE_CREATE(MLX5_IB_STAGE_SPECS,
5344 mlx5_ib_stage_populate_specs,
5345 mlx5_ib_stage_depopulate_specs),
Mark Bloch16c19752018-01-01 13:06:58 +02005346 STAGE_CREATE(MLX5_IB_STAGE_IB_REG,
5347 mlx5_ib_stage_ib_reg_init,
5348 mlx5_ib_stage_ib_reg_cleanup),
Mark Bloch42cea832018-03-14 09:14:15 +02005349 STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR,
5350 mlx5_ib_stage_post_ib_reg_umr_init,
5351 NULL),
Mark Bloch16c19752018-01-01 13:06:58 +02005352 STAGE_CREATE(MLX5_IB_STAGE_DELAY_DROP,
5353 mlx5_ib_stage_delay_drop_init,
5354 mlx5_ib_stage_delay_drop_cleanup),
5355 STAGE_CREATE(MLX5_IB_STAGE_CLASS_ATTR,
5356 mlx5_ib_stage_class_attr_init,
5357 NULL),
Mark Bloch16c19752018-01-01 13:06:58 +02005358};
5359
Mark Blochb5ca15a2018-01-23 11:16:30 +00005360static const struct mlx5_ib_profile nic_rep_profile = {
5361 STAGE_CREATE(MLX5_IB_STAGE_INIT,
5362 mlx5_ib_stage_init_init,
5363 mlx5_ib_stage_init_cleanup),
5364 STAGE_CREATE(MLX5_IB_STAGE_FLOW_DB,
5365 mlx5_ib_stage_flow_db_init,
5366 mlx5_ib_stage_flow_db_cleanup),
5367 STAGE_CREATE(MLX5_IB_STAGE_CAPS,
5368 mlx5_ib_stage_caps_init,
5369 NULL),
5370 STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB,
5371 mlx5_ib_stage_rep_non_default_cb,
5372 NULL),
5373 STAGE_CREATE(MLX5_IB_STAGE_ROCE,
5374 mlx5_ib_stage_rep_roce_init,
5375 mlx5_ib_stage_rep_roce_cleanup),
5376 STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES,
5377 mlx5_ib_stage_dev_res_init,
5378 mlx5_ib_stage_dev_res_cleanup),
5379 STAGE_CREATE(MLX5_IB_STAGE_COUNTERS,
5380 mlx5_ib_stage_counters_init,
5381 mlx5_ib_stage_counters_cleanup),
5382 STAGE_CREATE(MLX5_IB_STAGE_UAR,
5383 mlx5_ib_stage_uar_init,
5384 mlx5_ib_stage_uar_cleanup),
5385 STAGE_CREATE(MLX5_IB_STAGE_BFREG,
5386 mlx5_ib_stage_bfrag_init,
5387 mlx5_ib_stage_bfrag_cleanup),
Doug Ledford2d873442018-03-14 18:49:12 -04005388 STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR,
5389 NULL,
5390 mlx5_ib_stage_pre_ib_reg_umr_cleanup),
Matan Barak8c846602018-03-28 09:27:41 +03005391 STAGE_CREATE(MLX5_IB_STAGE_SPECS,
5392 mlx5_ib_stage_populate_specs,
5393 mlx5_ib_stage_depopulate_specs),
Mark Blochb5ca15a2018-01-23 11:16:30 +00005394 STAGE_CREATE(MLX5_IB_STAGE_IB_REG,
5395 mlx5_ib_stage_ib_reg_init,
5396 mlx5_ib_stage_ib_reg_cleanup),
Doug Ledford2d873442018-03-14 18:49:12 -04005397 STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR,
5398 mlx5_ib_stage_post_ib_reg_umr_init,
5399 NULL),
Mark Blochb5ca15a2018-01-23 11:16:30 +00005400 STAGE_CREATE(MLX5_IB_STAGE_CLASS_ATTR,
5401 mlx5_ib_stage_class_attr_init,
5402 NULL),
5403 STAGE_CREATE(MLX5_IB_STAGE_REP_REG,
5404 mlx5_ib_stage_rep_reg_init,
5405 mlx5_ib_stage_rep_reg_cleanup),
5406};
5407
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005408static void *mlx5_ib_add_slave_port(struct mlx5_core_dev *mdev, u8 port_num)
5409{
5410 struct mlx5_ib_multiport_info *mpi;
5411 struct mlx5_ib_dev *dev;
5412 bool bound = false;
5413 int err;
5414
5415 mpi = kzalloc(sizeof(*mpi), GFP_KERNEL);
5416 if (!mpi)
5417 return NULL;
5418
5419 mpi->mdev = mdev;
5420
5421 err = mlx5_query_nic_vport_system_image_guid(mdev,
5422 &mpi->sys_image_guid);
5423 if (err) {
5424 kfree(mpi);
5425 return NULL;
5426 }
5427
5428 mutex_lock(&mlx5_ib_multiport_mutex);
5429 list_for_each_entry(dev, &mlx5_ib_dev_list, ib_dev_list) {
5430 if (dev->sys_image_guid == mpi->sys_image_guid)
5431 bound = mlx5_ib_bind_slave_port(dev, mpi);
5432
5433 if (bound) {
5434 rdma_roce_rescan_device(&dev->ib_dev);
5435 break;
5436 }
5437 }
5438
5439 if (!bound) {
5440 list_add_tail(&mpi->list, &mlx5_ib_unaffiliated_port_list);
5441 dev_dbg(&mdev->pdev->dev, "no suitable IB device found to bind to, added to unaffiliated list.\n");
5442 } else {
5443 mlx5_ib_dbg(dev, "bound port %u\n", port_num + 1);
5444 }
5445 mutex_unlock(&mlx5_ib_multiport_mutex);
5446
5447 return mpi;
5448}
5449
Mark Bloch16c19752018-01-01 13:06:58 +02005450static void *mlx5_ib_add(struct mlx5_core_dev *mdev)
5451{
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005452 enum rdma_link_layer ll;
Mark Blochb5ca15a2018-01-23 11:16:30 +00005453 struct mlx5_ib_dev *dev;
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005454 int port_type_cap;
5455
Mark Blochb5ca15a2018-01-23 11:16:30 +00005456 printk_once(KERN_INFO "%s", mlx5_version);
5457
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005458 port_type_cap = MLX5_CAP_GEN(mdev, port_type);
5459 ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
5460
5461 if (mlx5_core_is_mp_slave(mdev) && ll == IB_LINK_LAYER_ETHERNET) {
5462 u8 port_num = mlx5_core_native_port_num(mdev) - 1;
5463
5464 return mlx5_ib_add_slave_port(mdev, port_num);
5465 }
5466
Mark Blochb5ca15a2018-01-23 11:16:30 +00005467 dev = (struct mlx5_ib_dev *)ib_alloc_device(sizeof(*dev));
5468 if (!dev)
5469 return NULL;
5470
5471 dev->mdev = mdev;
5472 dev->num_ports = max(MLX5_CAP_GEN(mdev, num_ports),
5473 MLX5_CAP_GEN(mdev, num_vhca_ports));
5474
5475 if (MLX5_VPORT_MANAGER(mdev) &&
5476 mlx5_ib_eswitch_mode(mdev->priv.eswitch) == SRIOV_OFFLOADS) {
5477 dev->rep = mlx5_ib_vport_rep(mdev->priv.eswitch, 0);
5478
5479 return __mlx5_ib_add(dev, &nic_rep_profile);
5480 }
5481
5482 return __mlx5_ib_add(dev, &pf_profile);
Mark Bloch16c19752018-01-01 13:06:58 +02005483}
5484
Jack Morgenstein9603b612014-07-28 23:30:22 +03005485static void mlx5_ib_remove(struct mlx5_core_dev *mdev, void *context)
Eli Cohene126ba92013-07-07 17:25:49 +03005486{
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005487 struct mlx5_ib_multiport_info *mpi;
5488 struct mlx5_ib_dev *dev;
Haggai Eran6aec21f2014-12-11 17:04:23 +02005489
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005490 if (mlx5_core_is_mp_slave(mdev)) {
5491 mpi = context;
5492 mutex_lock(&mlx5_ib_multiport_mutex);
5493 if (mpi->ibdev)
5494 mlx5_ib_unbind_slave_port(mpi->ibdev, mpi);
5495 list_del(&mpi->list);
5496 mutex_unlock(&mlx5_ib_multiport_mutex);
5497 return;
5498 }
5499
5500 dev = context;
Mark Bloch16c19752018-01-01 13:06:58 +02005501 __mlx5_ib_remove(dev, dev->profile, MLX5_IB_STAGE_MAX);
Eli Cohene126ba92013-07-07 17:25:49 +03005502}
5503
Jack Morgenstein9603b612014-07-28 23:30:22 +03005504static struct mlx5_interface mlx5_ib_interface = {
5505 .add = mlx5_ib_add,
5506 .remove = mlx5_ib_remove,
5507 .event = mlx5_ib_event,
Artemy Kovalyovd9aaed82017-01-02 11:37:46 +02005508#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
5509 .pfault = mlx5_ib_pfault,
5510#endif
Saeed Mahameed64613d942015-04-02 17:07:34 +03005511 .protocol = MLX5_INTERFACE_PROTOCOL_IB,
Eli Cohene126ba92013-07-07 17:25:49 +03005512};
5513
Ilya Lesokhinc44ef992018-03-13 15:18:48 +02005514unsigned long mlx5_ib_get_xlt_emergency_page(void)
5515{
5516 mutex_lock(&xlt_emergency_page_mutex);
5517 return xlt_emergency_page;
5518}
5519
5520void mlx5_ib_put_xlt_emergency_page(void)
5521{
5522 mutex_unlock(&xlt_emergency_page_mutex);
5523}
5524
Eli Cohene126ba92013-07-07 17:25:49 +03005525static int __init mlx5_ib_init(void)
5526{
Haggai Eran6aec21f2014-12-11 17:04:23 +02005527 int err;
5528
Ilya Lesokhinc44ef992018-03-13 15:18:48 +02005529 xlt_emergency_page = __get_free_page(GFP_KERNEL);
5530 if (!xlt_emergency_page)
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02005531 return -ENOMEM;
5532
Ilya Lesokhinc44ef992018-03-13 15:18:48 +02005533 mutex_init(&xlt_emergency_page_mutex);
5534
5535 mlx5_ib_event_wq = alloc_ordered_workqueue("mlx5_ib_event_wq", 0);
5536 if (!mlx5_ib_event_wq) {
5537 free_page(xlt_emergency_page);
5538 return -ENOMEM;
5539 }
5540
Artemy Kovalyov81713d32017-01-18 16:58:11 +02005541 mlx5_ib_odp_init();
Jack Morgenstein9603b612014-07-28 23:30:22 +03005542
Haggai Eran6aec21f2014-12-11 17:04:23 +02005543 err = mlx5_register_interface(&mlx5_ib_interface);
Haggai Eran6aec21f2014-12-11 17:04:23 +02005544
5545 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03005546}
5547
5548static void __exit mlx5_ib_cleanup(void)
5549{
Jack Morgenstein9603b612014-07-28 23:30:22 +03005550 mlx5_unregister_interface(&mlx5_ib_interface);
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02005551 destroy_workqueue(mlx5_ib_event_wq);
Ilya Lesokhinc44ef992018-03-13 15:18:48 +02005552 mutex_destroy(&xlt_emergency_page_mutex);
5553 free_page(xlt_emergency_page);
Eli Cohene126ba92013-07-07 17:25:49 +03005554}
5555
5556module_init(mlx5_ib_init);
5557module_exit(mlx5_ib_cleanup);