blob: ee55d7d645549e16a4fa3a243c8e307e31f7ad12 [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"
Eli Cohene126ba92013-07-07 17:25:49 +030062
63#define DRIVER_NAME "mlx5_ib"
Tariq Toukanb3599112017-02-22 17:45:46 +020064#define DRIVER_VERSION "5.0-0"
Eli Cohene126ba92013-07-07 17:25:49 +030065
66MODULE_AUTHOR("Eli Cohen <eli@mellanox.com>");
67MODULE_DESCRIPTION("Mellanox Connect-IB HCA IB driver");
68MODULE_LICENSE("Dual BSD/GPL");
Eli Cohene126ba92013-07-07 17:25:49 +030069
Eli Cohene126ba92013-07-07 17:25:49 +030070static char mlx5_version[] =
71 DRIVER_NAME ": Mellanox Connect-IB Infiniband driver v"
Tariq Toukanb3599112017-02-22 17:45:46 +020072 DRIVER_VERSION "\n";
Eli Cohene126ba92013-07-07 17:25:49 +030073
Daniel Jurgensd69a24e2018-01-04 17:25:37 +020074struct mlx5_ib_event_work {
75 struct work_struct work;
76 struct mlx5_core_dev *dev;
77 void *context;
78 enum mlx5_dev_event event;
79 unsigned long param;
80};
81
Eran Ben Elishada7525d2015-12-14 16:34:10 +020082enum {
83 MLX5_ATOMIC_SIZE_QP_8BYTES = 1 << 3,
84};
Majd Dibbiny1b5daf12015-06-04 19:30:46 +030085
Daniel Jurgensd69a24e2018-01-04 17:25:37 +020086static struct workqueue_struct *mlx5_ib_event_wq;
Daniel Jurgens32f69e42018-01-04 17:25:36 +020087static LIST_HEAD(mlx5_ib_unaffiliated_port_list);
88static LIST_HEAD(mlx5_ib_dev_list);
89/*
90 * This mutex should be held when accessing either of the above lists
91 */
92static DEFINE_MUTEX(mlx5_ib_multiport_mutex);
93
94struct mlx5_ib_dev *mlx5_ib_get_ibdev_from_mpi(struct mlx5_ib_multiport_info *mpi)
95{
96 struct mlx5_ib_dev *dev;
97
98 mutex_lock(&mlx5_ib_multiport_mutex);
99 dev = mpi->ibdev;
100 mutex_unlock(&mlx5_ib_multiport_mutex);
101 return dev;
102}
103
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300104static enum rdma_link_layer
Achiad Shochatebd61f62015-12-23 18:47:16 +0200105mlx5_port_type_cap_to_rdma_ll(int port_type_cap)
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300106{
Achiad Shochatebd61f62015-12-23 18:47:16 +0200107 switch (port_type_cap) {
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300108 case MLX5_CAP_PORT_TYPE_IB:
109 return IB_LINK_LAYER_INFINIBAND;
110 case MLX5_CAP_PORT_TYPE_ETH:
111 return IB_LINK_LAYER_ETHERNET;
112 default:
113 return IB_LINK_LAYER_UNSPECIFIED;
114 }
115}
116
Achiad Shochatebd61f62015-12-23 18:47:16 +0200117static enum rdma_link_layer
118mlx5_ib_port_link_layer(struct ib_device *device, u8 port_num)
119{
120 struct mlx5_ib_dev *dev = to_mdev(device);
121 int port_type_cap = MLX5_CAP_GEN(dev->mdev, port_type);
122
123 return mlx5_port_type_cap_to_rdma_ll(port_type_cap);
124}
125
Moni Shouafd65f1b2017-05-30 09:56:05 +0300126static int get_port_state(struct ib_device *ibdev,
127 u8 port_num,
128 enum ib_port_state *state)
129{
130 struct ib_port_attr attr;
131 int ret;
132
133 memset(&attr, 0, sizeof(attr));
Mark Bloch8e6efa32017-11-06 12:22:13 +0000134 ret = ibdev->query_port(ibdev, port_num, &attr);
Moni Shouafd65f1b2017-05-30 09:56:05 +0300135 if (!ret)
136 *state = attr.state;
137 return ret;
138}
139
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200140static int mlx5_netdev_event(struct notifier_block *this,
141 unsigned long event, void *ptr)
142{
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200143 struct mlx5_roce *roce = container_of(this, struct mlx5_roce, nb);
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200144 struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200145 u8 port_num = roce->native_port_num;
146 struct mlx5_core_dev *mdev;
147 struct mlx5_ib_dev *ibdev;
148
149 ibdev = roce->dev;
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200150 mdev = mlx5_ib_get_native_port_mdev(ibdev, port_num, NULL);
151 if (!mdev)
152 return NOTIFY_DONE;
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200153
Aviv Heller5ec8c832016-09-18 20:48:00 +0300154 switch (event) {
155 case NETDEV_REGISTER:
156 case NETDEV_UNREGISTER:
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200157 write_lock(&roce->netdev_lock);
Mark Blochbcf87f12018-01-16 15:02:36 +0000158 if (ibdev->rep) {
159 struct mlx5_eswitch *esw = ibdev->mdev->priv.eswitch;
160 struct net_device *rep_ndev;
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200161
Mark Blochbcf87f12018-01-16 15:02:36 +0000162 rep_ndev = mlx5_ib_get_rep_netdev(esw,
163 ibdev->rep->vport);
164 if (rep_ndev == ndev)
165 roce->netdev = (event == NETDEV_UNREGISTER) ?
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200166 NULL : ndev;
Mark Blochbcf87f12018-01-16 15:02:36 +0000167 } else if (ndev->dev.parent == &ibdev->mdev->pdev->dev) {
168 roce->netdev = (event == NETDEV_UNREGISTER) ?
169 NULL : ndev;
170 }
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200171 write_unlock(&roce->netdev_lock);
Aviv Heller5ec8c832016-09-18 20:48:00 +0300172 break;
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200173
Moni Shouafd65f1b2017-05-30 09:56:05 +0300174 case NETDEV_CHANGE:
Aviv Heller5ec8c832016-09-18 20:48:00 +0300175 case NETDEV_UP:
Aviv Heller88621df2016-09-18 20:48:02 +0300176 case NETDEV_DOWN: {
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200177 struct net_device *lag_ndev = mlx5_lag_get_roce_netdev(mdev);
Aviv Heller88621df2016-09-18 20:48:02 +0300178 struct net_device *upper = NULL;
179
180 if (lag_ndev) {
181 upper = netdev_master_upper_dev_get(lag_ndev);
182 dev_put(lag_ndev);
183 }
184
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200185 if ((upper == ndev || (!upper && ndev == roce->netdev))
Aviv Heller88621df2016-09-18 20:48:02 +0300186 && ibdev->ib_active) {
Bart Van Assche626bc022016-12-05 17:18:08 -0800187 struct ib_event ibev = { };
Moni Shouafd65f1b2017-05-30 09:56:05 +0300188 enum ib_port_state port_state;
Aviv Heller5ec8c832016-09-18 20:48:00 +0300189
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200190 if (get_port_state(&ibdev->ib_dev, port_num,
191 &port_state))
192 goto done;
Moni Shouafd65f1b2017-05-30 09:56:05 +0300193
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200194 if (roce->last_port_state == port_state)
195 goto done;
Moni Shouafd65f1b2017-05-30 09:56:05 +0300196
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200197 roce->last_port_state = port_state;
Aviv Heller5ec8c832016-09-18 20:48:00 +0300198 ibev.device = &ibdev->ib_dev;
Moni Shouafd65f1b2017-05-30 09:56:05 +0300199 if (port_state == IB_PORT_DOWN)
200 ibev.event = IB_EVENT_PORT_ERR;
201 else if (port_state == IB_PORT_ACTIVE)
202 ibev.event = IB_EVENT_PORT_ACTIVE;
203 else
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200204 goto done;
Moni Shouafd65f1b2017-05-30 09:56:05 +0300205
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200206 ibev.element.port_num = port_num;
Aviv Heller5ec8c832016-09-18 20:48:00 +0300207 ib_dispatch_event(&ibev);
208 }
209 break;
Aviv Heller88621df2016-09-18 20:48:02 +0300210 }
Aviv Heller5ec8c832016-09-18 20:48:00 +0300211
212 default:
213 break;
214 }
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200215done:
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200216 mlx5_ib_put_native_port_mdev(ibdev, port_num);
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200217 return NOTIFY_DONE;
218}
219
220static struct net_device *mlx5_ib_get_netdev(struct ib_device *device,
221 u8 port_num)
222{
223 struct mlx5_ib_dev *ibdev = to_mdev(device);
224 struct net_device *ndev;
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200225 struct mlx5_core_dev *mdev;
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200226
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200227 mdev = mlx5_ib_get_native_port_mdev(ibdev, port_num, NULL);
228 if (!mdev)
229 return NULL;
230
231 ndev = mlx5_lag_get_roce_netdev(mdev);
Aviv Heller88621df2016-09-18 20:48:02 +0300232 if (ndev)
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200233 goto out;
Aviv Heller88621df2016-09-18 20:48:02 +0300234
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200235 /* Ensure ndev does not disappear before we invoke dev_hold()
236 */
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200237 read_lock(&ibdev->roce[port_num - 1].netdev_lock);
238 ndev = ibdev->roce[port_num - 1].netdev;
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200239 if (ndev)
240 dev_hold(ndev);
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200241 read_unlock(&ibdev->roce[port_num - 1].netdev_lock);
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200242
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200243out:
244 mlx5_ib_put_native_port_mdev(ibdev, port_num);
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200245 return ndev;
246}
247
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200248struct mlx5_core_dev *mlx5_ib_get_native_port_mdev(struct mlx5_ib_dev *ibdev,
249 u8 ib_port_num,
250 u8 *native_port_num)
251{
252 enum rdma_link_layer ll = mlx5_ib_port_link_layer(&ibdev->ib_dev,
253 ib_port_num);
254 struct mlx5_core_dev *mdev = NULL;
255 struct mlx5_ib_multiport_info *mpi;
256 struct mlx5_ib_port *port;
257
258 if (native_port_num)
259 *native_port_num = 1;
260
261 if (!mlx5_core_mp_enabled(ibdev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
262 return ibdev->mdev;
263
264 port = &ibdev->port[ib_port_num - 1];
265 if (!port)
266 return NULL;
267
268 spin_lock(&port->mp.mpi_lock);
269 mpi = ibdev->port[ib_port_num - 1].mp.mpi;
270 if (mpi && !mpi->unaffiliate) {
271 mdev = mpi->mdev;
272 /* If it's the master no need to refcount, it'll exist
273 * as long as the ib_dev exists.
274 */
275 if (!mpi->is_master)
276 mpi->mdev_refcnt++;
277 }
278 spin_unlock(&port->mp.mpi_lock);
279
280 return mdev;
281}
282
283void mlx5_ib_put_native_port_mdev(struct mlx5_ib_dev *ibdev, u8 port_num)
284{
285 enum rdma_link_layer ll = mlx5_ib_port_link_layer(&ibdev->ib_dev,
286 port_num);
287 struct mlx5_ib_multiport_info *mpi;
288 struct mlx5_ib_port *port;
289
290 if (!mlx5_core_mp_enabled(ibdev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
291 return;
292
293 port = &ibdev->port[port_num - 1];
294
295 spin_lock(&port->mp.mpi_lock);
296 mpi = ibdev->port[port_num - 1].mp.mpi;
297 if (mpi->is_master)
298 goto out;
299
300 mpi->mdev_refcnt--;
301 if (mpi->unaffiliate)
302 complete(&mpi->unref_comp);
303out:
304 spin_unlock(&port->mp.mpi_lock);
305}
306
Noa Osherovichf1b65df2017-04-20 20:53:33 +0300307static int translate_eth_proto_oper(u32 eth_proto_oper, u8 *active_speed,
308 u8 *active_width)
309{
310 switch (eth_proto_oper) {
311 case MLX5E_PROT_MASK(MLX5E_1000BASE_CX_SGMII):
312 case MLX5E_PROT_MASK(MLX5E_1000BASE_KX):
313 case MLX5E_PROT_MASK(MLX5E_100BASE_TX):
314 case MLX5E_PROT_MASK(MLX5E_1000BASE_T):
315 *active_width = IB_WIDTH_1X;
316 *active_speed = IB_SPEED_SDR;
317 break;
318 case MLX5E_PROT_MASK(MLX5E_10GBASE_T):
319 case MLX5E_PROT_MASK(MLX5E_10GBASE_CX4):
320 case MLX5E_PROT_MASK(MLX5E_10GBASE_KX4):
321 case MLX5E_PROT_MASK(MLX5E_10GBASE_KR):
322 case MLX5E_PROT_MASK(MLX5E_10GBASE_CR):
323 case MLX5E_PROT_MASK(MLX5E_10GBASE_SR):
324 case MLX5E_PROT_MASK(MLX5E_10GBASE_ER):
325 *active_width = IB_WIDTH_1X;
326 *active_speed = IB_SPEED_QDR;
327 break;
328 case MLX5E_PROT_MASK(MLX5E_25GBASE_CR):
329 case MLX5E_PROT_MASK(MLX5E_25GBASE_KR):
330 case MLX5E_PROT_MASK(MLX5E_25GBASE_SR):
331 *active_width = IB_WIDTH_1X;
332 *active_speed = IB_SPEED_EDR;
333 break;
334 case MLX5E_PROT_MASK(MLX5E_40GBASE_CR4):
335 case MLX5E_PROT_MASK(MLX5E_40GBASE_KR4):
336 case MLX5E_PROT_MASK(MLX5E_40GBASE_SR4):
337 case MLX5E_PROT_MASK(MLX5E_40GBASE_LR4):
338 *active_width = IB_WIDTH_4X;
339 *active_speed = IB_SPEED_QDR;
340 break;
341 case MLX5E_PROT_MASK(MLX5E_50GBASE_CR2):
342 case MLX5E_PROT_MASK(MLX5E_50GBASE_KR2):
343 case MLX5E_PROT_MASK(MLX5E_50GBASE_SR2):
344 *active_width = IB_WIDTH_1X;
345 *active_speed = IB_SPEED_HDR;
346 break;
347 case MLX5E_PROT_MASK(MLX5E_56GBASE_R4):
348 *active_width = IB_WIDTH_4X;
349 *active_speed = IB_SPEED_FDR;
350 break;
351 case MLX5E_PROT_MASK(MLX5E_100GBASE_CR4):
352 case MLX5E_PROT_MASK(MLX5E_100GBASE_SR4):
353 case MLX5E_PROT_MASK(MLX5E_100GBASE_KR4):
354 case MLX5E_PROT_MASK(MLX5E_100GBASE_LR4):
355 *active_width = IB_WIDTH_4X;
356 *active_speed = IB_SPEED_EDR;
357 break;
358 default:
359 return -EINVAL;
360 }
361
362 return 0;
363}
364
Ilan Tayari095b0922017-05-14 16:04:30 +0300365static int mlx5_query_port_roce(struct ib_device *device, u8 port_num,
366 struct ib_port_attr *props)
Achiad Shochat3f89a642015-12-23 18:47:21 +0200367{
368 struct mlx5_ib_dev *dev = to_mdev(device);
Colin Ian Kingda005f92018-01-09 15:55:43 +0000369 struct mlx5_core_dev *mdev;
Aviv Heller88621df2016-09-18 20:48:02 +0300370 struct net_device *ndev, *upper;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200371 enum ib_mtu ndev_ib_mtu;
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200372 bool put_mdev = true;
Leon Romanovskyc876a1b2016-01-09 13:06:25 +0200373 u16 qkey_viol_cntr;
Noa Osherovichf1b65df2017-04-20 20:53:33 +0300374 u32 eth_prot_oper;
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200375 u8 mdev_port_num;
Ilan Tayari095b0922017-05-14 16:04:30 +0300376 int err;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200377
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200378 mdev = mlx5_ib_get_native_port_mdev(dev, port_num, &mdev_port_num);
379 if (!mdev) {
380 /* This means the port isn't affiliated yet. Get the
381 * info for the master port instead.
382 */
383 put_mdev = false;
384 mdev = dev->mdev;
385 mdev_port_num = 1;
386 port_num = 1;
387 }
388
Noa Osherovichf1b65df2017-04-20 20:53:33 +0300389 /* Possible bad flows are checked before filling out props so in case
390 * of an error it will still be zeroed out.
Noa Osherovich50f22fd2017-04-20 20:53:32 +0300391 */
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200392 err = mlx5_query_port_eth_proto_oper(mdev, &eth_prot_oper,
393 mdev_port_num);
Ilan Tayari095b0922017-05-14 16:04:30 +0300394 if (err)
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200395 goto out;
Noa Osherovichf1b65df2017-04-20 20:53:33 +0300396
397 translate_eth_proto_oper(eth_prot_oper, &props->active_speed,
398 &props->active_width);
Achiad Shochat3f89a642015-12-23 18:47:21 +0200399
400 props->port_cap_flags |= IB_PORT_CM_SUP;
401 props->port_cap_flags |= IB_PORT_IP_BASED_GIDS;
402
403 props->gid_tbl_len = MLX5_CAP_ROCE(dev->mdev,
404 roce_address_table_size);
405 props->max_mtu = IB_MTU_4096;
406 props->max_msg_sz = 1 << MLX5_CAP_GEN(dev->mdev, log_max_msg);
407 props->pkey_tbl_len = 1;
408 props->state = IB_PORT_DOWN;
409 props->phys_state = 3;
410
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200411 mlx5_query_nic_vport_qkey_viol_cntr(mdev, &qkey_viol_cntr);
Leon Romanovskyc876a1b2016-01-09 13:06:25 +0200412 props->qkey_viol_cntr = qkey_viol_cntr;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200413
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200414 /* If this is a stub query for an unaffiliated port stop here */
415 if (!put_mdev)
416 goto out;
417
Achiad Shochat3f89a642015-12-23 18:47:21 +0200418 ndev = mlx5_ib_get_netdev(device, port_num);
419 if (!ndev)
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200420 goto out;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200421
Aviv Heller88621df2016-09-18 20:48:02 +0300422 if (mlx5_lag_is_active(dev->mdev)) {
423 rcu_read_lock();
424 upper = netdev_master_upper_dev_get_rcu(ndev);
425 if (upper) {
426 dev_put(ndev);
427 ndev = upper;
428 dev_hold(ndev);
429 }
430 rcu_read_unlock();
431 }
432
Achiad Shochat3f89a642015-12-23 18:47:21 +0200433 if (netif_running(ndev) && netif_carrier_ok(ndev)) {
434 props->state = IB_PORT_ACTIVE;
435 props->phys_state = 5;
436 }
437
438 ndev_ib_mtu = iboe_get_mtu(ndev->mtu);
439
440 dev_put(ndev);
441
442 props->active_mtu = min(props->max_mtu, ndev_ib_mtu);
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200443out:
444 if (put_mdev)
445 mlx5_ib_put_native_port_mdev(dev, port_num);
446 return err;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200447}
448
Ilan Tayari095b0922017-05-14 16:04:30 +0300449static int set_roce_addr(struct mlx5_ib_dev *dev, u8 port_num,
450 unsigned int index, const union ib_gid *gid,
451 const struct ib_gid_attr *attr)
Achiad Shochat3cca2602015-12-23 18:47:23 +0200452{
Ilan Tayari095b0922017-05-14 16:04:30 +0300453 enum ib_gid_type gid_type = IB_GID_TYPE_IB;
454 u8 roce_version = 0;
455 u8 roce_l3_type = 0;
456 bool vlan = false;
457 u8 mac[ETH_ALEN];
458 u16 vlan_id = 0;
Achiad Shochat3cca2602015-12-23 18:47:23 +0200459
Ilan Tayari095b0922017-05-14 16:04:30 +0300460 if (gid) {
461 gid_type = attr->gid_type;
462 ether_addr_copy(mac, attr->ndev->dev_addr);
Achiad Shochat3cca2602015-12-23 18:47:23 +0200463
Ilan Tayari095b0922017-05-14 16:04:30 +0300464 if (is_vlan_dev(attr->ndev)) {
465 vlan = true;
466 vlan_id = vlan_dev_vlan_id(attr->ndev);
467 }
Achiad Shochat3cca2602015-12-23 18:47:23 +0200468 }
469
Ilan Tayari095b0922017-05-14 16:04:30 +0300470 switch (gid_type) {
Achiad Shochat3cca2602015-12-23 18:47:23 +0200471 case IB_GID_TYPE_IB:
Ilan Tayari095b0922017-05-14 16:04:30 +0300472 roce_version = MLX5_ROCE_VERSION_1;
Achiad Shochat3cca2602015-12-23 18:47:23 +0200473 break;
474 case IB_GID_TYPE_ROCE_UDP_ENCAP:
Ilan Tayari095b0922017-05-14 16:04:30 +0300475 roce_version = MLX5_ROCE_VERSION_2;
476 if (ipv6_addr_v4mapped((void *)gid))
477 roce_l3_type = MLX5_ROCE_L3_TYPE_IPV4;
478 else
479 roce_l3_type = MLX5_ROCE_L3_TYPE_IPV6;
Achiad Shochat3cca2602015-12-23 18:47:23 +0200480 break;
481
482 default:
Ilan Tayari095b0922017-05-14 16:04:30 +0300483 mlx5_ib_warn(dev, "Unexpected GID type %u\n", gid_type);
Achiad Shochat3cca2602015-12-23 18:47:23 +0200484 }
485
Ilan Tayari095b0922017-05-14 16:04:30 +0300486 return mlx5_core_roce_gid_set(dev->mdev, index, roce_version,
487 roce_l3_type, gid->raw, mac, vlan,
Daniel Jurgenscfe4e372018-01-04 17:25:41 +0200488 vlan_id, port_num);
Achiad Shochat3cca2602015-12-23 18:47:23 +0200489}
490
491static int mlx5_ib_add_gid(struct ib_device *device, u8 port_num,
492 unsigned int index, const union ib_gid *gid,
493 const struct ib_gid_attr *attr,
494 __always_unused void **context)
495{
Ilan Tayari095b0922017-05-14 16:04:30 +0300496 return set_roce_addr(to_mdev(device), port_num, index, gid, attr);
Achiad Shochat3cca2602015-12-23 18:47:23 +0200497}
498
499static int mlx5_ib_del_gid(struct ib_device *device, u8 port_num,
500 unsigned int index, __always_unused void **context)
501{
Ilan Tayari095b0922017-05-14 16:04:30 +0300502 return set_roce_addr(to_mdev(device), port_num, index, NULL, NULL);
Achiad Shochat3cca2602015-12-23 18:47:23 +0200503}
504
Achiad Shochat2811ba52015-12-23 18:47:24 +0200505__be16 mlx5_get_roce_udp_sport(struct mlx5_ib_dev *dev, u8 port_num,
506 int index)
507{
508 struct ib_gid_attr attr;
509 union ib_gid gid;
510
511 if (ib_get_cached_gid(&dev->ib_dev, port_num, index, &gid, &attr))
512 return 0;
513
514 if (!attr.ndev)
515 return 0;
516
517 dev_put(attr.ndev);
518
519 if (attr.gid_type != IB_GID_TYPE_ROCE_UDP_ENCAP)
520 return 0;
521
522 return cpu_to_be16(MLX5_CAP_ROCE(dev->mdev, r_roce_min_src_udp_port));
523}
524
Majd Dibbinyed884512017-01-18 14:10:35 +0200525int mlx5_get_roce_gid_type(struct mlx5_ib_dev *dev, u8 port_num,
526 int index, enum ib_gid_type *gid_type)
527{
528 struct ib_gid_attr attr;
529 union ib_gid gid;
530 int ret;
531
532 ret = ib_get_cached_gid(&dev->ib_dev, port_num, index, &gid, &attr);
533 if (ret)
534 return ret;
535
536 if (!attr.ndev)
537 return -ENODEV;
538
539 dev_put(attr.ndev);
540
541 *gid_type = attr.gid_type;
542
543 return 0;
544}
545
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300546static int mlx5_use_mad_ifc(struct mlx5_ib_dev *dev)
547{
Noa Osherovich7fae6652016-09-12 19:16:23 +0300548 if (MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_IB)
549 return !MLX5_CAP_GEN(dev->mdev, ib_virt);
550 return 0;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300551}
552
553enum {
554 MLX5_VPORT_ACCESS_METHOD_MAD,
555 MLX5_VPORT_ACCESS_METHOD_HCA,
556 MLX5_VPORT_ACCESS_METHOD_NIC,
557};
558
559static int mlx5_get_vport_access_method(struct ib_device *ibdev)
560{
561 if (mlx5_use_mad_ifc(to_mdev(ibdev)))
562 return MLX5_VPORT_ACCESS_METHOD_MAD;
563
Achiad Shochatebd61f62015-12-23 18:47:16 +0200564 if (mlx5_ib_port_link_layer(ibdev, 1) ==
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300565 IB_LINK_LAYER_ETHERNET)
566 return MLX5_VPORT_ACCESS_METHOD_NIC;
567
568 return MLX5_VPORT_ACCESS_METHOD_HCA;
569}
570
Eran Ben Elishada7525d2015-12-14 16:34:10 +0200571static void get_atomic_caps(struct mlx5_ib_dev *dev,
Moni Shoua776a3902018-01-02 16:19:33 +0200572 u8 atomic_size_qp,
Eran Ben Elishada7525d2015-12-14 16:34:10 +0200573 struct ib_device_attr *props)
574{
575 u8 tmp;
576 u8 atomic_operations = MLX5_CAP_ATOMIC(dev->mdev, atomic_operations);
Eran Ben Elishada7525d2015-12-14 16:34:10 +0200577 u8 atomic_req_8B_endianness_mode =
Or Gerlitzbd108382017-05-28 15:24:17 +0300578 MLX5_CAP_ATOMIC(dev->mdev, atomic_req_8B_endianness_mode);
Eran Ben Elishada7525d2015-12-14 16:34:10 +0200579
580 /* Check if HW supports 8 bytes standard atomic operations and capable
581 * of host endianness respond
582 */
583 tmp = MLX5_ATOMIC_OPS_CMP_SWAP | MLX5_ATOMIC_OPS_FETCH_ADD;
584 if (((atomic_operations & tmp) == tmp) &&
585 (atomic_size_qp & MLX5_ATOMIC_SIZE_QP_8BYTES) &&
586 (atomic_req_8B_endianness_mode)) {
587 props->atomic_cap = IB_ATOMIC_HCA;
588 } else {
589 props->atomic_cap = IB_ATOMIC_NONE;
590 }
591}
592
Moni Shoua776a3902018-01-02 16:19:33 +0200593static void get_atomic_caps_qp(struct mlx5_ib_dev *dev,
594 struct ib_device_attr *props)
595{
596 u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_qp);
597
598 get_atomic_caps(dev, atomic_size_qp, props);
599}
600
601static void get_atomic_caps_dc(struct mlx5_ib_dev *dev,
602 struct ib_device_attr *props)
603{
604 u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_dc);
605
606 get_atomic_caps(dev, atomic_size_qp, props);
607}
608
609bool mlx5_ib_dc_atomic_is_supported(struct mlx5_ib_dev *dev)
610{
611 struct ib_device_attr props = {};
612
613 get_atomic_caps_dc(dev, &props);
614 return (props.atomic_cap == IB_ATOMIC_HCA) ? true : false;
615}
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300616static int mlx5_query_system_image_guid(struct ib_device *ibdev,
617 __be64 *sys_image_guid)
618{
619 struct mlx5_ib_dev *dev = to_mdev(ibdev);
620 struct mlx5_core_dev *mdev = dev->mdev;
621 u64 tmp;
622 int err;
623
624 switch (mlx5_get_vport_access_method(ibdev)) {
625 case MLX5_VPORT_ACCESS_METHOD_MAD:
626 return mlx5_query_mad_ifc_system_image_guid(ibdev,
627 sys_image_guid);
628
629 case MLX5_VPORT_ACCESS_METHOD_HCA:
630 err = mlx5_query_hca_vport_system_image_guid(mdev, &tmp);
Achiad Shochat3f89a642015-12-23 18:47:21 +0200631 break;
632
633 case MLX5_VPORT_ACCESS_METHOD_NIC:
634 err = mlx5_query_nic_vport_system_image_guid(mdev, &tmp);
635 break;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300636
637 default:
638 return -EINVAL;
639 }
Achiad Shochat3f89a642015-12-23 18:47:21 +0200640
641 if (!err)
642 *sys_image_guid = cpu_to_be64(tmp);
643
644 return err;
645
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300646}
647
648static int mlx5_query_max_pkeys(struct ib_device *ibdev,
649 u16 *max_pkeys)
650{
651 struct mlx5_ib_dev *dev = to_mdev(ibdev);
652 struct mlx5_core_dev *mdev = dev->mdev;
653
654 switch (mlx5_get_vport_access_method(ibdev)) {
655 case MLX5_VPORT_ACCESS_METHOD_MAD:
656 return mlx5_query_mad_ifc_max_pkeys(ibdev, max_pkeys);
657
658 case MLX5_VPORT_ACCESS_METHOD_HCA:
659 case MLX5_VPORT_ACCESS_METHOD_NIC:
660 *max_pkeys = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev,
661 pkey_table_size));
662 return 0;
663
664 default:
665 return -EINVAL;
666 }
667}
668
669static int mlx5_query_vendor_id(struct ib_device *ibdev,
670 u32 *vendor_id)
671{
672 struct mlx5_ib_dev *dev = to_mdev(ibdev);
673
674 switch (mlx5_get_vport_access_method(ibdev)) {
675 case MLX5_VPORT_ACCESS_METHOD_MAD:
676 return mlx5_query_mad_ifc_vendor_id(ibdev, vendor_id);
677
678 case MLX5_VPORT_ACCESS_METHOD_HCA:
679 case MLX5_VPORT_ACCESS_METHOD_NIC:
680 return mlx5_core_query_vendor_id(dev->mdev, vendor_id);
681
682 default:
683 return -EINVAL;
684 }
685}
686
687static int mlx5_query_node_guid(struct mlx5_ib_dev *dev,
688 __be64 *node_guid)
689{
690 u64 tmp;
691 int err;
692
693 switch (mlx5_get_vport_access_method(&dev->ib_dev)) {
694 case MLX5_VPORT_ACCESS_METHOD_MAD:
695 return mlx5_query_mad_ifc_node_guid(dev, node_guid);
696
697 case MLX5_VPORT_ACCESS_METHOD_HCA:
698 err = mlx5_query_hca_vport_node_guid(dev->mdev, &tmp);
Achiad Shochat3f89a642015-12-23 18:47:21 +0200699 break;
700
701 case MLX5_VPORT_ACCESS_METHOD_NIC:
702 err = mlx5_query_nic_vport_node_guid(dev->mdev, &tmp);
703 break;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300704
705 default:
706 return -EINVAL;
707 }
Achiad Shochat3f89a642015-12-23 18:47:21 +0200708
709 if (!err)
710 *node_guid = cpu_to_be64(tmp);
711
712 return err;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300713}
714
715struct mlx5_reg_node_desc {
Yuval Shaiabd99fde2016-08-25 10:57:07 -0700716 u8 desc[IB_DEVICE_NODE_DESC_MAX];
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300717};
718
719static int mlx5_query_node_desc(struct mlx5_ib_dev *dev, char *node_desc)
720{
721 struct mlx5_reg_node_desc in;
722
723 if (mlx5_use_mad_ifc(dev))
724 return mlx5_query_mad_ifc_node_desc(dev, node_desc);
725
726 memset(&in, 0, sizeof(in));
727
728 return mlx5_core_access_reg(dev->mdev, &in, sizeof(in), node_desc,
729 sizeof(struct mlx5_reg_node_desc),
730 MLX5_REG_NODE_DESC, 0, 0);
731}
732
Eli Cohene126ba92013-07-07 17:25:49 +0300733static int mlx5_ib_query_device(struct ib_device *ibdev,
Matan Barak2528e332015-06-11 16:35:25 +0300734 struct ib_device_attr *props,
735 struct ib_udata *uhw)
Eli Cohene126ba92013-07-07 17:25:49 +0300736{
737 struct mlx5_ib_dev *dev = to_mdev(ibdev);
Saeed Mahameed938fe832015-05-28 22:28:41 +0300738 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +0300739 int err = -ENOMEM;
Eli Cohen288c01b2016-10-27 16:36:45 +0300740 int max_sq_desc;
Eli Cohene126ba92013-07-07 17:25:49 +0300741 int max_rq_sg;
742 int max_sq_sg;
Sagi Grimberge0238a62015-07-21 14:40:12 +0300743 u64 min_page_size = 1ull << MLX5_CAP_GEN(mdev, log_pg_sz);
Daniel Jurgens85c7c012018-01-04 17:25:43 +0200744 bool raw_support = !mlx5_core_mp_enabled(mdev);
Bodong Wang402ca532016-06-17 15:02:20 +0300745 struct mlx5_ib_query_device_resp resp = {};
746 size_t resp_len;
747 u64 max_tso;
Eli Cohene126ba92013-07-07 17:25:49 +0300748
Bodong Wang402ca532016-06-17 15:02:20 +0300749 resp_len = sizeof(resp.comp_mask) + sizeof(resp.response_length);
750 if (uhw->outlen && uhw->outlen < resp_len)
751 return -EINVAL;
752 else
753 resp.response_length = resp_len;
754
755 if (uhw->inlen && !ib_is_udata_cleared(uhw, 0, uhw->inlen))
Matan Barak2528e332015-06-11 16:35:25 +0300756 return -EINVAL;
757
Eli Cohene126ba92013-07-07 17:25:49 +0300758 memset(props, 0, sizeof(*props));
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300759 err = mlx5_query_system_image_guid(ibdev,
760 &props->sys_image_guid);
761 if (err)
762 return err;
763
764 err = mlx5_query_max_pkeys(ibdev, &props->max_pkeys);
765 if (err)
766 return err;
767
768 err = mlx5_query_vendor_id(ibdev, &props->vendor_id);
769 if (err)
770 return err;
Eli Cohene126ba92013-07-07 17:25:49 +0300771
Jack Morgenstein9603b612014-07-28 23:30:22 +0300772 props->fw_ver = ((u64)fw_rev_maj(dev->mdev) << 32) |
773 (fw_rev_min(dev->mdev) << 16) |
774 fw_rev_sub(dev->mdev);
Eli Cohene126ba92013-07-07 17:25:49 +0300775 props->device_cap_flags = IB_DEVICE_CHANGE_PHY_PORT |
776 IB_DEVICE_PORT_ACTIVE_EVENT |
777 IB_DEVICE_SYS_IMAGE_GUID |
Eli Cohen1a4c3a32014-02-06 17:41:25 +0200778 IB_DEVICE_RC_RNR_NAK_GEN;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300779
780 if (MLX5_CAP_GEN(mdev, pkv))
Eli Cohene126ba92013-07-07 17:25:49 +0300781 props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300782 if (MLX5_CAP_GEN(mdev, qkv))
Eli Cohene126ba92013-07-07 17:25:49 +0300783 props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300784 if (MLX5_CAP_GEN(mdev, apm))
Eli Cohene126ba92013-07-07 17:25:49 +0300785 props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300786 if (MLX5_CAP_GEN(mdev, xrc))
Eli Cohene126ba92013-07-07 17:25:49 +0300787 props->device_cap_flags |= IB_DEVICE_XRC;
Matan Barakd2370e02016-02-29 18:05:30 +0200788 if (MLX5_CAP_GEN(mdev, imaicl)) {
789 props->device_cap_flags |= IB_DEVICE_MEM_WINDOW |
790 IB_DEVICE_MEM_WINDOW_TYPE_2B;
791 props->max_mw = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
Sagi Grimbergb005d312016-02-29 19:07:33 +0200792 /* We support 'Gappy' memory registration too */
793 props->device_cap_flags |= IB_DEVICE_SG_GAPS_REG;
Matan Barakd2370e02016-02-29 18:05:30 +0200794 }
Eli Cohene126ba92013-07-07 17:25:49 +0300795 props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300796 if (MLX5_CAP_GEN(mdev, sho)) {
Sagi Grimberg2dea9092014-02-23 14:19:13 +0200797 props->device_cap_flags |= IB_DEVICE_SIGNATURE_HANDOVER;
798 /* At this stage no support for signature handover */
799 props->sig_prot_cap = IB_PROT_T10DIF_TYPE_1 |
800 IB_PROT_T10DIF_TYPE_2 |
801 IB_PROT_T10DIF_TYPE_3;
802 props->sig_guard_cap = IB_GUARD_T10DIF_CRC |
803 IB_GUARD_T10DIF_CSUM;
804 }
Saeed Mahameed938fe832015-05-28 22:28:41 +0300805 if (MLX5_CAP_GEN(mdev, block_lb_mc))
Eli Cohenf360d882014-04-02 00:10:16 +0300806 props->device_cap_flags |= IB_DEVICE_BLOCK_MULTICAST_LOOPBACK;
Eli Cohene126ba92013-07-07 17:25:49 +0300807
Daniel Jurgens85c7c012018-01-04 17:25:43 +0200808 if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) && raw_support) {
Noa Osheroviche8161332017-01-18 15:40:01 +0200809 if (MLX5_CAP_ETH(mdev, csum_cap)) {
810 /* Legacy bit to support old userspace libraries */
Bodong Wang88115fe2015-12-18 13:53:20 +0200811 props->device_cap_flags |= IB_DEVICE_RAW_IP_CSUM;
Noa Osheroviche8161332017-01-18 15:40:01 +0200812 props->raw_packet_caps |= IB_RAW_PACKET_CAP_IP_CSUM;
813 }
814
815 if (MLX5_CAP_ETH(dev->mdev, vlan_cap))
816 props->raw_packet_caps |=
817 IB_RAW_PACKET_CAP_CVLAN_STRIPPING;
Bodong Wang88115fe2015-12-18 13:53:20 +0200818
Bodong Wang402ca532016-06-17 15:02:20 +0300819 if (field_avail(typeof(resp), tso_caps, uhw->outlen)) {
820 max_tso = MLX5_CAP_ETH(mdev, max_lso_cap);
821 if (max_tso) {
822 resp.tso_caps.max_tso = 1 << max_tso;
823 resp.tso_caps.supported_qpts |=
824 1 << IB_QPT_RAW_PACKET;
825 resp.response_length += sizeof(resp.tso_caps);
826 }
827 }
Yishai Hadas31f69a82016-08-28 11:28:45 +0300828
829 if (field_avail(typeof(resp), rss_caps, uhw->outlen)) {
830 resp.rss_caps.rx_hash_function =
831 MLX5_RX_HASH_FUNC_TOEPLITZ;
832 resp.rss_caps.rx_hash_fields_mask =
833 MLX5_RX_HASH_SRC_IPV4 |
834 MLX5_RX_HASH_DST_IPV4 |
835 MLX5_RX_HASH_SRC_IPV6 |
836 MLX5_RX_HASH_DST_IPV6 |
837 MLX5_RX_HASH_SRC_PORT_TCP |
838 MLX5_RX_HASH_DST_PORT_TCP |
839 MLX5_RX_HASH_SRC_PORT_UDP |
Maor Gottlieb4e2b53a2017-12-24 14:51:25 +0200840 MLX5_RX_HASH_DST_PORT_UDP |
841 MLX5_RX_HASH_INNER;
Yishai Hadas31f69a82016-08-28 11:28:45 +0300842 resp.response_length += sizeof(resp.rss_caps);
843 }
844 } else {
845 if (field_avail(typeof(resp), tso_caps, uhw->outlen))
846 resp.response_length += sizeof(resp.tso_caps);
847 if (field_avail(typeof(resp), rss_caps, uhw->outlen))
848 resp.response_length += sizeof(resp.rss_caps);
Bodong Wang402ca532016-06-17 15:02:20 +0300849 }
850
Erez Shitritf0313962016-02-21 16:27:17 +0200851 if (MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) {
852 props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
853 props->device_cap_flags |= IB_DEVICE_UD_TSO;
854 }
855
Maor Gottlieb03404e82017-05-30 10:29:13 +0300856 if (MLX5_CAP_GEN(dev->mdev, rq_delay_drop) &&
Daniel Jurgens85c7c012018-01-04 17:25:43 +0200857 MLX5_CAP_GEN(dev->mdev, general_notification_event) &&
858 raw_support)
Maor Gottlieb03404e82017-05-30 10:29:13 +0300859 props->raw_packet_caps |= IB_RAW_PACKET_CAP_DELAY_DROP;
860
Yishai Hadas1d54f892017-06-08 16:15:11 +0300861 if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads) &&
862 MLX5_CAP_IPOIB_ENHANCED(mdev, csum_cap))
863 props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
864
Majd Dibbinycff5a0f2016-04-17 17:19:38 +0300865 if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
Daniel Jurgens85c7c012018-01-04 17:25:43 +0200866 MLX5_CAP_ETH(dev->mdev, scatter_fcs) &&
867 raw_support) {
Noa Osheroviche8161332017-01-18 15:40:01 +0200868 /* Legacy bit to support old userspace libraries */
Majd Dibbinycff5a0f2016-04-17 17:19:38 +0300869 props->device_cap_flags |= IB_DEVICE_RAW_SCATTER_FCS;
Noa Osheroviche8161332017-01-18 15:40:01 +0200870 props->raw_packet_caps |= IB_RAW_PACKET_CAP_SCATTER_FCS;
871 }
Majd Dibbinycff5a0f2016-04-17 17:19:38 +0300872
Maor Gottliebda6d6ba32016-06-04 15:15:28 +0300873 if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS))
874 props->device_cap_flags |= IB_DEVICE_MANAGED_FLOW_STEERING;
875
Noa Osherovichb1383aa2017-10-29 13:59:45 +0200876 if (MLX5_CAP_GEN(mdev, end_pad))
877 props->device_cap_flags |= IB_DEVICE_PCI_WRITE_END_PADDING;
878
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300879 props->vendor_part_id = mdev->pdev->device;
880 props->hw_ver = mdev->pdev->revision;
Eli Cohene126ba92013-07-07 17:25:49 +0300881
882 props->max_mr_size = ~0ull;
Sagi Grimberge0238a62015-07-21 14:40:12 +0300883 props->page_size_cap = ~(min_page_size - 1);
Saeed Mahameed938fe832015-05-28 22:28:41 +0300884 props->max_qp = 1 << MLX5_CAP_GEN(mdev, log_max_qp);
885 props->max_qp_wr = 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
886 max_rq_sg = MLX5_CAP_GEN(mdev, max_wqe_sz_rq) /
887 sizeof(struct mlx5_wqe_data_seg);
Eli Cohen288c01b2016-10-27 16:36:45 +0300888 max_sq_desc = min_t(int, MLX5_CAP_GEN(mdev, max_wqe_sz_sq), 512);
889 max_sq_sg = (max_sq_desc - sizeof(struct mlx5_wqe_ctrl_seg) -
890 sizeof(struct mlx5_wqe_raddr_seg)) /
891 sizeof(struct mlx5_wqe_data_seg);
Eli Cohene126ba92013-07-07 17:25:49 +0300892 props->max_sge = min(max_rq_sg, max_sq_sg);
Sagi Grimberg986ef952016-03-31 19:03:25 +0300893 props->max_sge_rd = MLX5_MAX_SGE_RD;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300894 props->max_cq = 1 << MLX5_CAP_GEN(mdev, log_max_cq);
Leon Romanovsky9f177682016-01-14 08:11:40 +0200895 props->max_cqe = (1 << MLX5_CAP_GEN(mdev, log_max_cq_sz)) - 1;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300896 props->max_mr = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
897 props->max_pd = 1 << MLX5_CAP_GEN(mdev, log_max_pd);
898 props->max_qp_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_req_qp);
899 props->max_qp_init_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_res_qp);
900 props->max_srq = 1 << MLX5_CAP_GEN(mdev, log_max_srq);
901 props->max_srq_wr = (1 << MLX5_CAP_GEN(mdev, log_max_srq_sz)) - 1;
902 props->local_ca_ack_delay = MLX5_CAP_GEN(mdev, local_ca_ack_delay);
Eli Cohene126ba92013-07-07 17:25:49 +0300903 props->max_res_rd_atom = props->max_qp_rd_atom * props->max_qp;
Eli Cohene126ba92013-07-07 17:25:49 +0300904 props->max_srq_sge = max_rq_sg - 1;
Sagi Grimberg911f4332016-03-03 13:37:51 +0200905 props->max_fast_reg_page_list_len =
906 1 << MLX5_CAP_GEN(mdev, log_max_klm_list_size);
Moni Shoua776a3902018-01-02 16:19:33 +0200907 get_atomic_caps_qp(dev, props);
Eli Cohen81bea282013-09-11 16:35:30 +0300908 props->masked_atomic_cap = IB_ATOMIC_NONE;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300909 props->max_mcast_grp = 1 << MLX5_CAP_GEN(mdev, log_max_mcg);
910 props->max_mcast_qp_attach = MLX5_CAP_GEN(mdev, max_qp_mcg);
Eli Cohene126ba92013-07-07 17:25:49 +0300911 props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
912 props->max_mcast_grp;
913 props->max_map_per_fmr = INT_MAX; /* no limit in ConnectIB */
Maor Gottlieb86695a62016-10-27 16:36:38 +0300914 props->max_ah = INT_MAX;
Matan Barak7c60bcb2015-12-15 20:30:11 +0200915 props->hca_core_clock = MLX5_CAP_GEN(mdev, device_frequency_khz);
916 props->timestamp_mask = 0x7FFFFFFFFFFFFFFFULL;
Eli Cohene126ba92013-07-07 17:25:49 +0300917
Haggai Eran8cdd3122014-12-11 17:04:20 +0200918#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
Saeed Mahameed938fe832015-05-28 22:28:41 +0300919 if (MLX5_CAP_GEN(mdev, pg))
Haggai Eran8cdd3122014-12-11 17:04:20 +0200920 props->device_cap_flags |= IB_DEVICE_ON_DEMAND_PAGING;
921 props->odp_caps = dev->odp_caps;
922#endif
923
Leon Romanovsky051f2632015-12-20 12:16:11 +0200924 if (MLX5_CAP_GEN(mdev, cd))
925 props->device_cap_flags |= IB_DEVICE_CROSS_CHANNEL;
926
Eli Coheneff901d2016-03-11 22:58:42 +0200927 if (!mlx5_core_is_pf(mdev))
928 props->device_cap_flags |= IB_DEVICE_VIRTUAL_FUNCTION;
929
Yishai Hadas31f69a82016-08-28 11:28:45 +0300930 if (mlx5_ib_port_link_layer(ibdev, 1) ==
Daniel Jurgens85c7c012018-01-04 17:25:43 +0200931 IB_LINK_LAYER_ETHERNET && raw_support) {
Yishai Hadas31f69a82016-08-28 11:28:45 +0300932 props->rss_caps.max_rwq_indirection_tables =
933 1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt);
934 props->rss_caps.max_rwq_indirection_table_size =
935 1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt_size);
936 props->rss_caps.supported_qpts = 1 << IB_QPT_RAW_PACKET;
937 props->max_wq_type_rq =
938 1 << MLX5_CAP_GEN(dev->mdev, log_max_rq);
939 }
940
Artemy Kovalyoveb761892017-08-17 15:52:09 +0300941 if (MLX5_CAP_GEN(mdev, tag_matching)) {
Leon Romanovsky78b1beb2017-09-24 21:46:29 +0300942 props->tm_caps.max_rndv_hdr_size = MLX5_TM_MAX_RNDV_MSG_SIZE;
943 props->tm_caps.max_num_tags =
Artemy Kovalyoveb761892017-08-17 15:52:09 +0300944 (1 << MLX5_CAP_GEN(mdev, log_tag_matching_list_sz)) - 1;
Leon Romanovsky78b1beb2017-09-24 21:46:29 +0300945 props->tm_caps.flags = IB_TM_CAP_RC;
946 props->tm_caps.max_ops =
Artemy Kovalyoveb761892017-08-17 15:52:09 +0300947 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
Leon Romanovsky78b1beb2017-09-24 21:46:29 +0300948 props->tm_caps.max_sge = MLX5_TM_MAX_SGE;
Artemy Kovalyoveb761892017-08-17 15:52:09 +0300949 }
950
Yonatan Cohen87ab3f52017-11-13 10:51:18 +0200951 if (MLX5_CAP_GEN(dev->mdev, cq_moderation)) {
952 props->cq_caps.max_cq_moderation_count =
953 MLX5_MAX_CQ_COUNT;
954 props->cq_caps.max_cq_moderation_period =
955 MLX5_MAX_CQ_PERIOD;
956 }
957
Bodong Wang7e43a2a2016-10-31 12:16:44 +0200958 if (field_avail(typeof(resp), cqe_comp_caps, uhw->outlen)) {
959 resp.cqe_comp_caps.max_num =
960 MLX5_CAP_GEN(dev->mdev, cqe_compression) ?
961 MLX5_CAP_GEN(dev->mdev, cqe_compression_max_num) : 0;
962 resp.cqe_comp_caps.supported_format =
963 MLX5_IB_CQE_RES_FORMAT_HASH |
964 MLX5_IB_CQE_RES_FORMAT_CSUM;
965 resp.response_length += sizeof(resp.cqe_comp_caps);
966 }
967
Daniel Jurgens85c7c012018-01-04 17:25:43 +0200968 if (field_avail(typeof(resp), packet_pacing_caps, uhw->outlen) &&
969 raw_support) {
Bodong Wangd9491672016-12-01 13:43:13 +0200970 if (MLX5_CAP_QOS(mdev, packet_pacing) &&
971 MLX5_CAP_GEN(mdev, qos)) {
972 resp.packet_pacing_caps.qp_rate_limit_max =
973 MLX5_CAP_QOS(mdev, packet_pacing_max_rate);
974 resp.packet_pacing_caps.qp_rate_limit_min =
975 MLX5_CAP_QOS(mdev, packet_pacing_min_rate);
976 resp.packet_pacing_caps.supported_qpts |=
977 1 << IB_QPT_RAW_PACKET;
978 }
979 resp.response_length += sizeof(resp.packet_pacing_caps);
980 }
981
Leon Romanovsky9f885202017-01-02 11:37:39 +0200982 if (field_avail(typeof(resp), mlx5_ib_support_multi_pkt_send_wqes,
983 uhw->outlen)) {
Bodong Wang795b6092017-08-17 15:52:34 +0300984 if (MLX5_CAP_ETH(mdev, multi_pkt_send_wqe))
985 resp.mlx5_ib_support_multi_pkt_send_wqes =
986 MLX5_IB_ALLOW_MPW;
Bodong Wang050da902017-08-17 15:52:35 +0300987
988 if (MLX5_CAP_ETH(mdev, enhanced_multi_pkt_send_wqe))
989 resp.mlx5_ib_support_multi_pkt_send_wqes |=
990 MLX5_IB_SUPPORT_EMPW;
991
Leon Romanovsky9f885202017-01-02 11:37:39 +0200992 resp.response_length +=
993 sizeof(resp.mlx5_ib_support_multi_pkt_send_wqes);
994 }
995
Guy Levide57f2a2017-10-19 08:25:52 +0300996 if (field_avail(typeof(resp), flags, uhw->outlen)) {
997 resp.response_length += sizeof(resp.flags);
Guy Levi7a0c8f42017-10-19 08:25:53 +0300998
Guy Levide57f2a2017-10-19 08:25:52 +0300999 if (MLX5_CAP_GEN(mdev, cqe_compression_128))
1000 resp.flags |=
1001 MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_COMP;
Guy Levi7a0c8f42017-10-19 08:25:53 +03001002
1003 if (MLX5_CAP_GEN(mdev, cqe_128_always))
1004 resp.flags |= MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_PAD;
Guy Levide57f2a2017-10-19 08:25:52 +03001005 }
Leon Romanovsky9f885202017-01-02 11:37:39 +02001006
Noa Osherovich96dc3fc2017-08-17 15:52:28 +03001007 if (field_avail(typeof(resp), sw_parsing_caps,
1008 uhw->outlen)) {
1009 resp.response_length += sizeof(resp.sw_parsing_caps);
1010 if (MLX5_CAP_ETH(mdev, swp)) {
1011 resp.sw_parsing_caps.sw_parsing_offloads |=
1012 MLX5_IB_SW_PARSING;
1013
1014 if (MLX5_CAP_ETH(mdev, swp_csum))
1015 resp.sw_parsing_caps.sw_parsing_offloads |=
1016 MLX5_IB_SW_PARSING_CSUM;
1017
1018 if (MLX5_CAP_ETH(mdev, swp_lso))
1019 resp.sw_parsing_caps.sw_parsing_offloads |=
1020 MLX5_IB_SW_PARSING_LSO;
1021
1022 if (resp.sw_parsing_caps.sw_parsing_offloads)
1023 resp.sw_parsing_caps.supported_qpts =
1024 BIT(IB_QPT_RAW_PACKET);
1025 }
1026 }
1027
Daniel Jurgens85c7c012018-01-04 17:25:43 +02001028 if (field_avail(typeof(resp), striding_rq_caps, uhw->outlen) &&
1029 raw_support) {
Noa Osherovichb4f34592017-10-17 18:01:12 +03001030 resp.response_length += sizeof(resp.striding_rq_caps);
1031 if (MLX5_CAP_GEN(mdev, striding_rq)) {
1032 resp.striding_rq_caps.min_single_stride_log_num_of_bytes =
1033 MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES;
1034 resp.striding_rq_caps.max_single_stride_log_num_of_bytes =
1035 MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES;
1036 resp.striding_rq_caps.min_single_wqe_log_num_of_strides =
1037 MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES;
1038 resp.striding_rq_caps.max_single_wqe_log_num_of_strides =
1039 MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES;
1040 resp.striding_rq_caps.supported_qpts =
1041 BIT(IB_QPT_RAW_PACKET);
1042 }
1043 }
1044
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001045 if (field_avail(typeof(resp), tunnel_offloads_caps,
1046 uhw->outlen)) {
1047 resp.response_length += sizeof(resp.tunnel_offloads_caps);
1048 if (MLX5_CAP_ETH(mdev, tunnel_stateless_vxlan))
1049 resp.tunnel_offloads_caps |=
1050 MLX5_IB_TUNNELED_OFFLOADS_VXLAN;
1051 if (MLX5_CAP_ETH(mdev, tunnel_stateless_geneve_rx))
1052 resp.tunnel_offloads_caps |=
1053 MLX5_IB_TUNNELED_OFFLOADS_GENEVE;
1054 if (MLX5_CAP_ETH(mdev, tunnel_stateless_gre))
1055 resp.tunnel_offloads_caps |=
1056 MLX5_IB_TUNNELED_OFFLOADS_GRE;
1057 }
1058
Bodong Wang402ca532016-06-17 15:02:20 +03001059 if (uhw->outlen) {
1060 err = ib_copy_to_udata(uhw, &resp, resp.response_length);
1061
1062 if (err)
1063 return err;
1064 }
1065
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001066 return 0;
1067}
Eli Cohene126ba92013-07-07 17:25:49 +03001068
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001069enum mlx5_ib_width {
1070 MLX5_IB_WIDTH_1X = 1 << 0,
1071 MLX5_IB_WIDTH_2X = 1 << 1,
1072 MLX5_IB_WIDTH_4X = 1 << 2,
1073 MLX5_IB_WIDTH_8X = 1 << 3,
1074 MLX5_IB_WIDTH_12X = 1 << 4
1075};
1076
1077static int translate_active_width(struct ib_device *ibdev, u8 active_width,
1078 u8 *ib_width)
1079{
1080 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1081 int err = 0;
1082
1083 if (active_width & MLX5_IB_WIDTH_1X) {
1084 *ib_width = IB_WIDTH_1X;
1085 } else if (active_width & MLX5_IB_WIDTH_2X) {
1086 mlx5_ib_dbg(dev, "active_width %d is not supported by IB spec\n",
1087 (int)active_width);
1088 err = -EINVAL;
1089 } else if (active_width & MLX5_IB_WIDTH_4X) {
1090 *ib_width = IB_WIDTH_4X;
1091 } else if (active_width & MLX5_IB_WIDTH_8X) {
1092 *ib_width = IB_WIDTH_8X;
1093 } else if (active_width & MLX5_IB_WIDTH_12X) {
1094 *ib_width = IB_WIDTH_12X;
1095 } else {
1096 mlx5_ib_dbg(dev, "Invalid active_width %d\n",
1097 (int)active_width);
1098 err = -EINVAL;
1099 }
1100
1101 return err;
1102}
1103
1104static int mlx5_mtu_to_ib_mtu(int mtu)
1105{
1106 switch (mtu) {
1107 case 256: return 1;
1108 case 512: return 2;
1109 case 1024: return 3;
1110 case 2048: return 4;
1111 case 4096: return 5;
1112 default:
1113 pr_warn("invalid mtu\n");
1114 return -1;
1115 }
1116}
1117
1118enum ib_max_vl_num {
1119 __IB_MAX_VL_0 = 1,
1120 __IB_MAX_VL_0_1 = 2,
1121 __IB_MAX_VL_0_3 = 3,
1122 __IB_MAX_VL_0_7 = 4,
1123 __IB_MAX_VL_0_14 = 5,
1124};
1125
1126enum mlx5_vl_hw_cap {
1127 MLX5_VL_HW_0 = 1,
1128 MLX5_VL_HW_0_1 = 2,
1129 MLX5_VL_HW_0_2 = 3,
1130 MLX5_VL_HW_0_3 = 4,
1131 MLX5_VL_HW_0_4 = 5,
1132 MLX5_VL_HW_0_5 = 6,
1133 MLX5_VL_HW_0_6 = 7,
1134 MLX5_VL_HW_0_7 = 8,
1135 MLX5_VL_HW_0_14 = 15
1136};
1137
1138static int translate_max_vl_num(struct ib_device *ibdev, u8 vl_hw_cap,
1139 u8 *max_vl_num)
1140{
1141 switch (vl_hw_cap) {
1142 case MLX5_VL_HW_0:
1143 *max_vl_num = __IB_MAX_VL_0;
1144 break;
1145 case MLX5_VL_HW_0_1:
1146 *max_vl_num = __IB_MAX_VL_0_1;
1147 break;
1148 case MLX5_VL_HW_0_3:
1149 *max_vl_num = __IB_MAX_VL_0_3;
1150 break;
1151 case MLX5_VL_HW_0_7:
1152 *max_vl_num = __IB_MAX_VL_0_7;
1153 break;
1154 case MLX5_VL_HW_0_14:
1155 *max_vl_num = __IB_MAX_VL_0_14;
1156 break;
1157
1158 default:
1159 return -EINVAL;
1160 }
1161
1162 return 0;
1163}
1164
1165static int mlx5_query_hca_port(struct ib_device *ibdev, u8 port,
1166 struct ib_port_attr *props)
1167{
1168 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1169 struct mlx5_core_dev *mdev = dev->mdev;
1170 struct mlx5_hca_vport_context *rep;
Saeed Mahameed046339e2016-04-22 00:33:03 +03001171 u16 max_mtu;
1172 u16 oper_mtu;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001173 int err;
1174 u8 ib_link_width_oper;
1175 u8 vl_hw_cap;
1176
1177 rep = kzalloc(sizeof(*rep), GFP_KERNEL);
1178 if (!rep) {
1179 err = -ENOMEM;
1180 goto out;
1181 }
1182
Or Gerlitzc4550c62017-01-24 13:02:39 +02001183 /* props being zeroed by the caller, avoid zeroing it here */
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001184
1185 err = mlx5_query_hca_vport_context(mdev, 0, port, 0, rep);
1186 if (err)
1187 goto out;
1188
1189 props->lid = rep->lid;
1190 props->lmc = rep->lmc;
1191 props->sm_lid = rep->sm_lid;
1192 props->sm_sl = rep->sm_sl;
1193 props->state = rep->vport_state;
1194 props->phys_state = rep->port_physical_state;
1195 props->port_cap_flags = rep->cap_mask1;
1196 props->gid_tbl_len = mlx5_get_gid_table_len(MLX5_CAP_GEN(mdev, gid_table_size));
1197 props->max_msg_sz = 1 << MLX5_CAP_GEN(mdev, log_max_msg);
1198 props->pkey_tbl_len = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, pkey_table_size));
1199 props->bad_pkey_cntr = rep->pkey_violation_counter;
1200 props->qkey_viol_cntr = rep->qkey_violation_counter;
1201 props->subnet_timeout = rep->subnet_timeout;
1202 props->init_type_reply = rep->init_type_reply;
Eli Coheneff901d2016-03-11 22:58:42 +02001203 props->grh_required = rep->grh_required;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001204
1205 err = mlx5_query_port_link_width_oper(mdev, &ib_link_width_oper, port);
1206 if (err)
1207 goto out;
1208
1209 err = translate_active_width(ibdev, ib_link_width_oper,
1210 &props->active_width);
1211 if (err)
1212 goto out;
Noa Osherovichd5beb7f2016-06-02 10:47:53 +03001213 err = mlx5_query_port_ib_proto_oper(mdev, &props->active_speed, port);
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001214 if (err)
1215 goto out;
1216
Saeed Mahameedfacc9692015-06-11 14:47:27 +03001217 mlx5_query_port_max_mtu(mdev, &max_mtu, port);
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001218
1219 props->max_mtu = mlx5_mtu_to_ib_mtu(max_mtu);
1220
Saeed Mahameedfacc9692015-06-11 14:47:27 +03001221 mlx5_query_port_oper_mtu(mdev, &oper_mtu, port);
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001222
1223 props->active_mtu = mlx5_mtu_to_ib_mtu(oper_mtu);
1224
1225 err = mlx5_query_port_vl_hw_cap(mdev, &vl_hw_cap, port);
1226 if (err)
1227 goto out;
1228
1229 err = translate_max_vl_num(ibdev, vl_hw_cap,
1230 &props->max_vl_num);
1231out:
1232 kfree(rep);
Eli Cohene126ba92013-07-07 17:25:49 +03001233 return err;
1234}
1235
1236int mlx5_ib_query_port(struct ib_device *ibdev, u8 port,
1237 struct ib_port_attr *props)
1238{
Ilan Tayari095b0922017-05-14 16:04:30 +03001239 unsigned int count;
1240 int ret;
1241
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001242 switch (mlx5_get_vport_access_method(ibdev)) {
1243 case MLX5_VPORT_ACCESS_METHOD_MAD:
Ilan Tayari095b0922017-05-14 16:04:30 +03001244 ret = mlx5_query_mad_ifc_port(ibdev, port, props);
1245 break;
Eli Cohene126ba92013-07-07 17:25:49 +03001246
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001247 case MLX5_VPORT_ACCESS_METHOD_HCA:
Ilan Tayari095b0922017-05-14 16:04:30 +03001248 ret = mlx5_query_hca_port(ibdev, port, props);
1249 break;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001250
Achiad Shochat3f89a642015-12-23 18:47:21 +02001251 case MLX5_VPORT_ACCESS_METHOD_NIC:
Ilan Tayari095b0922017-05-14 16:04:30 +03001252 ret = mlx5_query_port_roce(ibdev, port, props);
1253 break;
Achiad Shochat3f89a642015-12-23 18:47:21 +02001254
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001255 default:
Ilan Tayari095b0922017-05-14 16:04:30 +03001256 ret = -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +03001257 }
Ilan Tayari095b0922017-05-14 16:04:30 +03001258
1259 if (!ret && props) {
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001260 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1261 struct mlx5_core_dev *mdev;
1262 bool put_mdev = true;
1263
1264 mdev = mlx5_ib_get_native_port_mdev(dev, port, NULL);
1265 if (!mdev) {
1266 /* If the port isn't affiliated yet query the master.
1267 * The master and slave will have the same values.
1268 */
1269 mdev = dev->mdev;
1270 port = 1;
1271 put_mdev = false;
1272 }
1273 count = mlx5_core_reserved_gids_count(mdev);
1274 if (put_mdev)
1275 mlx5_ib_put_native_port_mdev(dev, port);
Ilan Tayari095b0922017-05-14 16:04:30 +03001276 props->gid_tbl_len -= count;
1277 }
1278 return ret;
Eli Cohene126ba92013-07-07 17:25:49 +03001279}
1280
Mark Bloch8e6efa32017-11-06 12:22:13 +00001281static int mlx5_ib_rep_query_port(struct ib_device *ibdev, u8 port,
1282 struct ib_port_attr *props)
1283{
1284 int ret;
1285
1286 /* Only link layer == ethernet is valid for representors */
1287 ret = mlx5_query_port_roce(ibdev, port, props);
1288 if (ret || !props)
1289 return ret;
1290
1291 /* We don't support GIDS */
1292 props->gid_tbl_len = 0;
1293
1294 return ret;
1295}
1296
Eli Cohene126ba92013-07-07 17:25:49 +03001297static int mlx5_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
1298 union ib_gid *gid)
1299{
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001300 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1301 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03001302
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001303 switch (mlx5_get_vport_access_method(ibdev)) {
1304 case MLX5_VPORT_ACCESS_METHOD_MAD:
1305 return mlx5_query_mad_ifc_gids(ibdev, port, index, gid);
Eli Cohene126ba92013-07-07 17:25:49 +03001306
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001307 case MLX5_VPORT_ACCESS_METHOD_HCA:
1308 return mlx5_query_hca_vport_gid(mdev, 0, port, 0, index, gid);
Eli Cohene126ba92013-07-07 17:25:49 +03001309
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001310 default:
1311 return -EINVAL;
1312 }
Eli Cohene126ba92013-07-07 17:25:49 +03001313
Eli Cohene126ba92013-07-07 17:25:49 +03001314}
1315
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001316static int mlx5_query_hca_nic_pkey(struct ib_device *ibdev, u8 port,
1317 u16 index, u16 *pkey)
1318{
1319 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1320 struct mlx5_core_dev *mdev;
1321 bool put_mdev = true;
1322 u8 mdev_port_num;
1323 int err;
1324
1325 mdev = mlx5_ib_get_native_port_mdev(dev, port, &mdev_port_num);
1326 if (!mdev) {
1327 /* The port isn't affiliated yet, get the PKey from the master
1328 * port. For RoCE the PKey tables will be the same.
1329 */
1330 put_mdev = false;
1331 mdev = dev->mdev;
1332 mdev_port_num = 1;
1333 }
1334
1335 err = mlx5_query_hca_vport_pkey(mdev, 0, mdev_port_num, 0,
1336 index, pkey);
1337 if (put_mdev)
1338 mlx5_ib_put_native_port_mdev(dev, port);
1339
1340 return err;
1341}
1342
Eli Cohene126ba92013-07-07 17:25:49 +03001343static int mlx5_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
1344 u16 *pkey)
1345{
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001346 switch (mlx5_get_vport_access_method(ibdev)) {
1347 case MLX5_VPORT_ACCESS_METHOD_MAD:
1348 return mlx5_query_mad_ifc_pkey(ibdev, port, index, pkey);
Eli Cohene126ba92013-07-07 17:25:49 +03001349
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001350 case MLX5_VPORT_ACCESS_METHOD_HCA:
1351 case MLX5_VPORT_ACCESS_METHOD_NIC:
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001352 return mlx5_query_hca_nic_pkey(ibdev, port, index, pkey);
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001353 default:
1354 return -EINVAL;
1355 }
Eli Cohene126ba92013-07-07 17:25:49 +03001356}
1357
Eli Cohene126ba92013-07-07 17:25:49 +03001358static int mlx5_ib_modify_device(struct ib_device *ibdev, int mask,
1359 struct ib_device_modify *props)
1360{
1361 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1362 struct mlx5_reg_node_desc in;
1363 struct mlx5_reg_node_desc out;
1364 int err;
1365
1366 if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
1367 return -EOPNOTSUPP;
1368
1369 if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
1370 return 0;
1371
1372 /*
1373 * If possible, pass node desc to FW, so it can generate
1374 * a 144 trap. If cmd fails, just ignore.
1375 */
Yuval Shaiabd99fde2016-08-25 10:57:07 -07001376 memcpy(&in, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
Jack Morgenstein9603b612014-07-28 23:30:22 +03001377 err = mlx5_core_access_reg(dev->mdev, &in, sizeof(in), &out,
Eli Cohene126ba92013-07-07 17:25:49 +03001378 sizeof(out), MLX5_REG_NODE_DESC, 0, 1);
1379 if (err)
1380 return err;
1381
Yuval Shaiabd99fde2016-08-25 10:57:07 -07001382 memcpy(ibdev->node_desc, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
Eli Cohene126ba92013-07-07 17:25:49 +03001383
1384 return err;
1385}
1386
Eli Cohencdbe33d2017-02-14 07:25:38 +02001387static int set_port_caps_atomic(struct mlx5_ib_dev *dev, u8 port_num, u32 mask,
1388 u32 value)
1389{
1390 struct mlx5_hca_vport_context ctx = {};
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001391 struct mlx5_core_dev *mdev;
1392 u8 mdev_port_num;
Eli Cohencdbe33d2017-02-14 07:25:38 +02001393 int err;
1394
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001395 mdev = mlx5_ib_get_native_port_mdev(dev, port_num, &mdev_port_num);
1396 if (!mdev)
1397 return -ENODEV;
1398
1399 err = mlx5_query_hca_vport_context(mdev, 0, mdev_port_num, 0, &ctx);
Eli Cohencdbe33d2017-02-14 07:25:38 +02001400 if (err)
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001401 goto out;
Eli Cohencdbe33d2017-02-14 07:25:38 +02001402
1403 if (~ctx.cap_mask1_perm & mask) {
1404 mlx5_ib_warn(dev, "trying to change bitmask 0x%X but change supported 0x%X\n",
1405 mask, ctx.cap_mask1_perm);
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001406 err = -EINVAL;
1407 goto out;
Eli Cohencdbe33d2017-02-14 07:25:38 +02001408 }
1409
1410 ctx.cap_mask1 = value;
1411 ctx.cap_mask1_perm = mask;
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001412 err = mlx5_core_modify_hca_vport_context(mdev, 0, mdev_port_num,
1413 0, &ctx);
1414
1415out:
1416 mlx5_ib_put_native_port_mdev(dev, port_num);
Eli Cohencdbe33d2017-02-14 07:25:38 +02001417
1418 return err;
1419}
1420
Eli Cohene126ba92013-07-07 17:25:49 +03001421static int mlx5_ib_modify_port(struct ib_device *ibdev, u8 port, int mask,
1422 struct ib_port_modify *props)
1423{
1424 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1425 struct ib_port_attr attr;
1426 u32 tmp;
1427 int err;
Eli Cohencdbe33d2017-02-14 07:25:38 +02001428 u32 change_mask;
1429 u32 value;
1430 bool is_ib = (mlx5_ib_port_link_layer(ibdev, port) ==
1431 IB_LINK_LAYER_INFINIBAND);
1432
Majd Dibbinyec255872017-08-23 08:35:42 +03001433 /* CM layer calls ib_modify_port() regardless of the link layer. For
1434 * Ethernet ports, qkey violation and Port capabilities are meaningless.
1435 */
1436 if (!is_ib)
1437 return 0;
1438
Eli Cohencdbe33d2017-02-14 07:25:38 +02001439 if (MLX5_CAP_GEN(dev->mdev, ib_virt) && is_ib) {
1440 change_mask = props->clr_port_cap_mask | props->set_port_cap_mask;
1441 value = ~props->clr_port_cap_mask | props->set_port_cap_mask;
1442 return set_port_caps_atomic(dev, port, change_mask, value);
1443 }
Eli Cohene126ba92013-07-07 17:25:49 +03001444
1445 mutex_lock(&dev->cap_mask_mutex);
1446
Or Gerlitzc4550c62017-01-24 13:02:39 +02001447 err = ib_query_port(ibdev, port, &attr);
Eli Cohene126ba92013-07-07 17:25:49 +03001448 if (err)
1449 goto out;
1450
1451 tmp = (attr.port_cap_flags | props->set_port_cap_mask) &
1452 ~props->clr_port_cap_mask;
1453
Jack Morgenstein9603b612014-07-28 23:30:22 +03001454 err = mlx5_set_port_caps(dev->mdev, port, tmp);
Eli Cohene126ba92013-07-07 17:25:49 +03001455
1456out:
1457 mutex_unlock(&dev->cap_mask_mutex);
1458 return err;
1459}
1460
Eli Cohen30aa60b2017-01-03 23:55:27 +02001461static void print_lib_caps(struct mlx5_ib_dev *dev, u64 caps)
1462{
1463 mlx5_ib_dbg(dev, "MLX5_LIB_CAP_4K_UAR = %s\n",
1464 caps & MLX5_LIB_CAP_4K_UAR ? "y" : "n");
1465}
1466
Yishai Hadas31a78a52017-12-24 16:31:34 +02001467static u16 calc_dynamic_bfregs(int uars_per_sys_page)
1468{
1469 /* Large page with non 4k uar support might limit the dynamic size */
1470 if (uars_per_sys_page == 1 && PAGE_SIZE > 4096)
1471 return MLX5_MIN_DYN_BFREGS;
1472
1473 return MLX5_MAX_DYN_BFREGS;
1474}
1475
Eli Cohenb037c292017-01-03 23:55:26 +02001476static int calc_total_bfregs(struct mlx5_ib_dev *dev, bool lib_uar_4k,
1477 struct mlx5_ib_alloc_ucontext_req_v2 *req,
Yishai Hadas31a78a52017-12-24 16:31:34 +02001478 struct mlx5_bfreg_info *bfregi)
Eli Cohenb037c292017-01-03 23:55:26 +02001479{
1480 int uars_per_sys_page;
1481 int bfregs_per_sys_page;
1482 int ref_bfregs = req->total_num_bfregs;
1483
1484 if (req->total_num_bfregs == 0)
1485 return -EINVAL;
1486
1487 BUILD_BUG_ON(MLX5_MAX_BFREGS % MLX5_NON_FP_BFREGS_IN_PAGE);
1488 BUILD_BUG_ON(MLX5_MAX_BFREGS < MLX5_NON_FP_BFREGS_IN_PAGE);
1489
1490 if (req->total_num_bfregs > MLX5_MAX_BFREGS)
1491 return -ENOMEM;
1492
1493 uars_per_sys_page = get_uars_per_sys_page(dev, lib_uar_4k);
1494 bfregs_per_sys_page = uars_per_sys_page * MLX5_NON_FP_BFREGS_PER_UAR;
Yishai Hadas31a78a52017-12-24 16:31:34 +02001495 /* This holds the required static allocation asked by the user */
Eli Cohenb037c292017-01-03 23:55:26 +02001496 req->total_num_bfregs = ALIGN(req->total_num_bfregs, bfregs_per_sys_page);
Eli Cohenb037c292017-01-03 23:55:26 +02001497 if (req->num_low_latency_bfregs > req->total_num_bfregs - 1)
1498 return -EINVAL;
1499
Yishai Hadas31a78a52017-12-24 16:31:34 +02001500 bfregi->num_static_sys_pages = req->total_num_bfregs / bfregs_per_sys_page;
1501 bfregi->num_dyn_bfregs = ALIGN(calc_dynamic_bfregs(uars_per_sys_page), bfregs_per_sys_page);
1502 bfregi->total_num_bfregs = req->total_num_bfregs + bfregi->num_dyn_bfregs;
1503 bfregi->num_sys_pages = bfregi->total_num_bfregs / bfregs_per_sys_page;
1504
1505 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 +02001506 MLX5_CAP_GEN(dev->mdev, uar_4k) ? "yes" : "no",
1507 lib_uar_4k ? "yes" : "no", ref_bfregs,
Yishai Hadas31a78a52017-12-24 16:31:34 +02001508 req->total_num_bfregs, bfregi->total_num_bfregs,
1509 bfregi->num_sys_pages);
Eli Cohenb037c292017-01-03 23:55:26 +02001510
1511 return 0;
1512}
1513
1514static int allocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
1515{
1516 struct mlx5_bfreg_info *bfregi;
1517 int err;
1518 int i;
1519
1520 bfregi = &context->bfregi;
Yishai Hadas31a78a52017-12-24 16:31:34 +02001521 for (i = 0; i < bfregi->num_static_sys_pages; i++) {
Eli Cohenb037c292017-01-03 23:55:26 +02001522 err = mlx5_cmd_alloc_uar(dev->mdev, &bfregi->sys_pages[i]);
1523 if (err)
1524 goto error;
1525
1526 mlx5_ib_dbg(dev, "allocated uar %d\n", bfregi->sys_pages[i]);
1527 }
Yishai Hadas4ed131d2017-12-24 16:31:35 +02001528
1529 for (i = bfregi->num_static_sys_pages; i < bfregi->num_sys_pages; i++)
1530 bfregi->sys_pages[i] = MLX5_IB_INVALID_UAR_INDEX;
1531
Eli Cohenb037c292017-01-03 23:55:26 +02001532 return 0;
1533
1534error:
1535 for (--i; i >= 0; i--)
1536 if (mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]))
1537 mlx5_ib_warn(dev, "failed to free uar %d\n", i);
1538
1539 return err;
1540}
1541
1542static int deallocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
1543{
1544 struct mlx5_bfreg_info *bfregi;
1545 int err;
1546 int i;
1547
1548 bfregi = &context->bfregi;
Yishai Hadas4ed131d2017-12-24 16:31:35 +02001549 for (i = 0; i < bfregi->num_sys_pages; i++) {
1550 if (i < bfregi->num_static_sys_pages ||
1551 bfregi->sys_pages[i] != MLX5_IB_INVALID_UAR_INDEX) {
1552 err = mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]);
1553 if (err) {
1554 mlx5_ib_warn(dev, "failed to free uar %d, err=%d\n", i, err);
1555 return err;
1556 }
Eli Cohenb037c292017-01-03 23:55:26 +02001557 }
1558 }
Yishai Hadas4ed131d2017-12-24 16:31:35 +02001559
Eli Cohenb037c292017-01-03 23:55:26 +02001560 return 0;
1561}
1562
Huy Nguyenc85023e2017-05-30 09:42:54 +03001563static int mlx5_ib_alloc_transport_domain(struct mlx5_ib_dev *dev, u32 *tdn)
1564{
1565 int err;
1566
1567 err = mlx5_core_alloc_transport_domain(dev->mdev, tdn);
1568 if (err)
1569 return err;
1570
1571 if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
Eran Ben Elisha8978cc92018-01-09 11:41:10 +02001572 (!MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) &&
1573 !MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
Huy Nguyenc85023e2017-05-30 09:42:54 +03001574 return err;
1575
1576 mutex_lock(&dev->lb_mutex);
1577 dev->user_td++;
1578
1579 if (dev->user_td == 2)
1580 err = mlx5_nic_vport_update_local_lb(dev->mdev, true);
1581
1582 mutex_unlock(&dev->lb_mutex);
1583 return err;
1584}
1585
1586static void mlx5_ib_dealloc_transport_domain(struct mlx5_ib_dev *dev, u32 tdn)
1587{
1588 mlx5_core_dealloc_transport_domain(dev->mdev, tdn);
1589
1590 if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
Eran Ben Elisha8978cc92018-01-09 11:41:10 +02001591 (!MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) &&
1592 !MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
Huy Nguyenc85023e2017-05-30 09:42:54 +03001593 return;
1594
1595 mutex_lock(&dev->lb_mutex);
1596 dev->user_td--;
1597
1598 if (dev->user_td < 2)
1599 mlx5_nic_vport_update_local_lb(dev->mdev, false);
1600
1601 mutex_unlock(&dev->lb_mutex);
1602}
1603
Eli Cohene126ba92013-07-07 17:25:49 +03001604static struct ib_ucontext *mlx5_ib_alloc_ucontext(struct ib_device *ibdev,
1605 struct ib_udata *udata)
1606{
1607 struct mlx5_ib_dev *dev = to_mdev(ibdev);
Matan Barakb368d7c2015-12-15 20:30:12 +02001608 struct mlx5_ib_alloc_ucontext_req_v2 req = {};
1609 struct mlx5_ib_alloc_ucontext_resp resp = {};
Feras Daoud5c99eae2018-01-16 20:08:41 +02001610 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03001611 struct mlx5_ib_ucontext *context;
Eli Cohen2f5ff262017-01-03 23:55:21 +02001612 struct mlx5_bfreg_info *bfregi;
Eli Cohen78c0f982014-01-30 13:49:48 +02001613 int ver;
Eli Cohene126ba92013-07-07 17:25:49 +03001614 int err;
Majd Dibbinya168a41c2016-01-28 17:51:47 +02001615 size_t min_req_v2 = offsetof(struct mlx5_ib_alloc_ucontext_req_v2,
1616 max_cqe_version);
Eli Cohenb037c292017-01-03 23:55:26 +02001617 bool lib_uar_4k;
Eli Cohene126ba92013-07-07 17:25:49 +03001618
1619 if (!dev->ib_active)
1620 return ERR_PTR(-EAGAIN);
1621
Amrani, Rame0931112017-06-27 17:04:42 +03001622 if (udata->inlen == sizeof(struct mlx5_ib_alloc_ucontext_req))
Eli Cohen78c0f982014-01-30 13:49:48 +02001623 ver = 0;
Amrani, Rame0931112017-06-27 17:04:42 +03001624 else if (udata->inlen >= min_req_v2)
Eli Cohen78c0f982014-01-30 13:49:48 +02001625 ver = 2;
1626 else
1627 return ERR_PTR(-EINVAL);
1628
Amrani, Rame0931112017-06-27 17:04:42 +03001629 err = ib_copy_from_udata(&req, udata, min(udata->inlen, sizeof(req)));
Eli Cohene126ba92013-07-07 17:25:49 +03001630 if (err)
1631 return ERR_PTR(err);
1632
Matan Barakb368d7c2015-12-15 20:30:12 +02001633 if (req.flags)
Eli Cohen78c0f982014-01-30 13:49:48 +02001634 return ERR_PTR(-EINVAL);
1635
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001636 if (req.comp_mask || req.reserved0 || req.reserved1 || req.reserved2)
Matan Barakb368d7c2015-12-15 20:30:12 +02001637 return ERR_PTR(-EOPNOTSUPP);
1638
Eli Cohen2f5ff262017-01-03 23:55:21 +02001639 req.total_num_bfregs = ALIGN(req.total_num_bfregs,
1640 MLX5_NON_FP_BFREGS_PER_UAR);
1641 if (req.num_low_latency_bfregs > req.total_num_bfregs - 1)
Eli Cohene126ba92013-07-07 17:25:49 +03001642 return ERR_PTR(-EINVAL);
1643
Saeed Mahameed938fe832015-05-28 22:28:41 +03001644 resp.qp_tab_size = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp);
Noa Osherovich2cc6ad52016-06-04 15:15:33 +03001645 if (mlx5_core_is_pf(dev->mdev) && MLX5_CAP_GEN(dev->mdev, bf))
1646 resp.bf_reg_size = 1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size);
Daniel Jurgensb47bd6e2016-10-25 18:36:24 +03001647 resp.cache_line_size = cache_line_size();
Saeed Mahameed938fe832015-05-28 22:28:41 +03001648 resp.max_sq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq);
1649 resp.max_rq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq);
1650 resp.max_send_wqebb = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1651 resp.max_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1652 resp.max_srq_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz);
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001653 resp.cqe_version = min_t(__u8,
1654 (__u8)MLX5_CAP_GEN(dev->mdev, cqe_version),
1655 req.max_cqe_version);
Eli Cohen30aa60b2017-01-03 23:55:27 +02001656 resp.log_uar_size = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
1657 MLX5_ADAPTER_PAGE_SHIFT : PAGE_SHIFT;
1658 resp.num_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
1659 MLX5_CAP_GEN(dev->mdev, num_of_uars_per_page) : 1;
Matan Barakb368d7c2015-12-15 20:30:12 +02001660 resp.response_length = min(offsetof(typeof(resp), response_length) +
1661 sizeof(resp.response_length), udata->outlen);
Eli Cohene126ba92013-07-07 17:25:49 +03001662
1663 context = kzalloc(sizeof(*context), GFP_KERNEL);
1664 if (!context)
1665 return ERR_PTR(-ENOMEM);
1666
Eli Cohen30aa60b2017-01-03 23:55:27 +02001667 lib_uar_4k = req.lib_caps & MLX5_LIB_CAP_4K_UAR;
Eli Cohen2f5ff262017-01-03 23:55:21 +02001668 bfregi = &context->bfregi;
Eli Cohenb037c292017-01-03 23:55:26 +02001669
1670 /* updates req->total_num_bfregs */
Yishai Hadas31a78a52017-12-24 16:31:34 +02001671 err = calc_total_bfregs(dev, lib_uar_4k, &req, bfregi);
Eli Cohenb037c292017-01-03 23:55:26 +02001672 if (err)
1673 goto out_ctx;
1674
Eli Cohen2f5ff262017-01-03 23:55:21 +02001675 mutex_init(&bfregi->lock);
Eli Cohenb037c292017-01-03 23:55:26 +02001676 bfregi->lib_uar_4k = lib_uar_4k;
Yishai Hadas31a78a52017-12-24 16:31:34 +02001677 bfregi->count = kcalloc(bfregi->total_num_bfregs, sizeof(*bfregi->count),
Eli Cohenb037c292017-01-03 23:55:26 +02001678 GFP_KERNEL);
1679 if (!bfregi->count) {
Eli Cohene126ba92013-07-07 17:25:49 +03001680 err = -ENOMEM;
1681 goto out_ctx;
1682 }
1683
Eli Cohenb037c292017-01-03 23:55:26 +02001684 bfregi->sys_pages = kcalloc(bfregi->num_sys_pages,
1685 sizeof(*bfregi->sys_pages),
1686 GFP_KERNEL);
1687 if (!bfregi->sys_pages) {
Eli Cohene126ba92013-07-07 17:25:49 +03001688 err = -ENOMEM;
Eli Cohenb037c292017-01-03 23:55:26 +02001689 goto out_count;
Eli Cohene126ba92013-07-07 17:25:49 +03001690 }
1691
Eli Cohenb037c292017-01-03 23:55:26 +02001692 err = allocate_uars(dev, context);
1693 if (err)
1694 goto out_sys_pages;
Eli Cohene126ba92013-07-07 17:25:49 +03001695
Haggai Eranb4cfe442014-12-11 17:04:26 +02001696#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
1697 context->ibucontext.invalidate_range = &mlx5_ib_invalidate_range;
1698#endif
1699
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +02001700 context->upd_xlt_page = __get_free_page(GFP_KERNEL);
1701 if (!context->upd_xlt_page) {
1702 err = -ENOMEM;
1703 goto out_uars;
1704 }
1705 mutex_init(&context->upd_xlt_page_mutex);
1706
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001707 if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain)) {
Huy Nguyenc85023e2017-05-30 09:42:54 +03001708 err = mlx5_ib_alloc_transport_domain(dev, &context->tdn);
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001709 if (err)
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +02001710 goto out_page;
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001711 }
1712
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001713 INIT_LIST_HEAD(&context->vma_private_list);
Majd Dibbinyad9a3662017-12-24 13:54:56 +02001714 mutex_init(&context->vma_private_list_mutex);
Eli Cohene126ba92013-07-07 17:25:49 +03001715 INIT_LIST_HEAD(&context->db_page_list);
1716 mutex_init(&context->db_page_mutex);
1717
Eli Cohen2f5ff262017-01-03 23:55:21 +02001718 resp.tot_bfregs = req.total_num_bfregs;
Daniel Jurgens508562d2018-01-04 17:25:34 +02001719 resp.num_ports = dev->num_ports;
Matan Barakb368d7c2015-12-15 20:30:12 +02001720
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001721 if (field_avail(typeof(resp), cqe_version, udata->outlen))
1722 resp.response_length += sizeof(resp.cqe_version);
Matan Barakb368d7c2015-12-15 20:30:12 +02001723
Bodong Wang402ca532016-06-17 15:02:20 +03001724 if (field_avail(typeof(resp), cmds_supp_uhw, udata->outlen)) {
Moni Shoua6ad279c52016-11-23 08:23:23 +02001725 resp.cmds_supp_uhw |= MLX5_USER_CMDS_SUPP_UHW_QUERY_DEVICE |
1726 MLX5_USER_CMDS_SUPP_UHW_CREATE_AH;
Bodong Wang402ca532016-06-17 15:02:20 +03001727 resp.response_length += sizeof(resp.cmds_supp_uhw);
1728 }
1729
Or Gerlitz78984892016-11-30 20:33:33 +02001730 if (field_avail(typeof(resp), eth_min_inline, udata->outlen)) {
1731 if (mlx5_ib_port_link_layer(ibdev, 1) == IB_LINK_LAYER_ETHERNET) {
1732 mlx5_query_min_inline(dev->mdev, &resp.eth_min_inline);
1733 resp.eth_min_inline++;
1734 }
1735 resp.response_length += sizeof(resp.eth_min_inline);
1736 }
1737
Feras Daoud5c99eae2018-01-16 20:08:41 +02001738 if (field_avail(typeof(resp), clock_info_versions, udata->outlen)) {
1739 if (mdev->clock_info)
1740 resp.clock_info_versions = BIT(MLX5_IB_CLOCK_INFO_V1);
1741 resp.response_length += sizeof(resp.clock_info_versions);
1742 }
1743
Noa Osherovichbc5c6ee2016-06-04 15:15:31 +03001744 /*
1745 * We don't want to expose information from the PCI bar that is located
1746 * after 4096 bytes, so if the arch only supports larger pages, let's
1747 * pretend we don't support reading the HCA's core clock. This is also
1748 * forced by mmap function.
1749 */
Eli Cohende8d6e02017-01-03 23:55:19 +02001750 if (field_avail(typeof(resp), hca_core_clock_offset, udata->outlen)) {
1751 if (PAGE_SIZE <= 4096) {
1752 resp.comp_mask |=
1753 MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_CORE_CLOCK_OFFSET;
1754 resp.hca_core_clock_offset =
1755 offsetof(struct mlx5_init_seg, internal_timer_h) % PAGE_SIZE;
1756 }
Feras Daoud5c99eae2018-01-16 20:08:41 +02001757 resp.response_length += sizeof(resp.hca_core_clock_offset);
Matan Barakb368d7c2015-12-15 20:30:12 +02001758 }
1759
Eli Cohen30aa60b2017-01-03 23:55:27 +02001760 if (field_avail(typeof(resp), log_uar_size, udata->outlen))
1761 resp.response_length += sizeof(resp.log_uar_size);
1762
1763 if (field_avail(typeof(resp), num_uars_per_page, udata->outlen))
1764 resp.response_length += sizeof(resp.num_uars_per_page);
1765
Yishai Hadas31a78a52017-12-24 16:31:34 +02001766 if (field_avail(typeof(resp), num_dyn_bfregs, udata->outlen)) {
1767 resp.num_dyn_bfregs = bfregi->num_dyn_bfregs;
1768 resp.response_length += sizeof(resp.num_dyn_bfregs);
1769 }
1770
Matan Barakb368d7c2015-12-15 20:30:12 +02001771 err = ib_copy_to_udata(udata, &resp, resp.response_length);
Eli Cohene126ba92013-07-07 17:25:49 +03001772 if (err)
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001773 goto out_td;
Eli Cohene126ba92013-07-07 17:25:49 +03001774
Eli Cohen2f5ff262017-01-03 23:55:21 +02001775 bfregi->ver = ver;
1776 bfregi->num_low_latency_bfregs = req.num_low_latency_bfregs;
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001777 context->cqe_version = resp.cqe_version;
Eli Cohen30aa60b2017-01-03 23:55:27 +02001778 context->lib_caps = req.lib_caps;
1779 print_lib_caps(dev, context->lib_caps);
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001780
Eli Cohene126ba92013-07-07 17:25:49 +03001781 return &context->ibucontext;
1782
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001783out_td:
1784 if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
Huy Nguyenc85023e2017-05-30 09:42:54 +03001785 mlx5_ib_dealloc_transport_domain(dev, context->tdn);
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001786
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +02001787out_page:
1788 free_page(context->upd_xlt_page);
1789
Eli Cohene126ba92013-07-07 17:25:49 +03001790out_uars:
Eli Cohenb037c292017-01-03 23:55:26 +02001791 deallocate_uars(dev, context);
1792
1793out_sys_pages:
1794 kfree(bfregi->sys_pages);
1795
Eli Cohene126ba92013-07-07 17:25:49 +03001796out_count:
Eli Cohen2f5ff262017-01-03 23:55:21 +02001797 kfree(bfregi->count);
Eli Cohene126ba92013-07-07 17:25:49 +03001798
Eli Cohene126ba92013-07-07 17:25:49 +03001799out_ctx:
1800 kfree(context);
Eli Cohenb037c292017-01-03 23:55:26 +02001801
Eli Cohene126ba92013-07-07 17:25:49 +03001802 return ERR_PTR(err);
1803}
1804
1805static int mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
1806{
1807 struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1808 struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
Eli Cohenb037c292017-01-03 23:55:26 +02001809 struct mlx5_bfreg_info *bfregi;
Eli Cohene126ba92013-07-07 17:25:49 +03001810
Eli Cohenb037c292017-01-03 23:55:26 +02001811 bfregi = &context->bfregi;
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001812 if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
Huy Nguyenc85023e2017-05-30 09:42:54 +03001813 mlx5_ib_dealloc_transport_domain(dev, context->tdn);
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001814
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +02001815 free_page(context->upd_xlt_page);
Eli Cohenb037c292017-01-03 23:55:26 +02001816 deallocate_uars(dev, context);
1817 kfree(bfregi->sys_pages);
Eli Cohen2f5ff262017-01-03 23:55:21 +02001818 kfree(bfregi->count);
Eli Cohene126ba92013-07-07 17:25:49 +03001819 kfree(context);
1820
1821 return 0;
1822}
1823
Eli Cohenb037c292017-01-03 23:55:26 +02001824static phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev,
Yishai Hadas4ed131d2017-12-24 16:31:35 +02001825 int uar_idx)
Eli Cohene126ba92013-07-07 17:25:49 +03001826{
Eli Cohenb037c292017-01-03 23:55:26 +02001827 int fw_uars_per_page;
1828
1829 fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ? MLX5_UARS_IN_PAGE : 1;
1830
Yishai Hadas4ed131d2017-12-24 16:31:35 +02001831 return (pci_resource_start(dev->mdev->pdev, 0) >> PAGE_SHIFT) + uar_idx / fw_uars_per_page;
Eli Cohene126ba92013-07-07 17:25:49 +03001832}
1833
1834static int get_command(unsigned long offset)
1835{
1836 return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK;
1837}
1838
1839static int get_arg(unsigned long offset)
1840{
1841 return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1);
1842}
1843
1844static int get_index(unsigned long offset)
1845{
1846 return get_arg(offset);
1847}
1848
Yishai Hadas4ed131d2017-12-24 16:31:35 +02001849/* Index resides in an extra byte to enable larger values than 255 */
1850static int get_extended_index(unsigned long offset)
1851{
1852 return get_arg(offset) | ((offset >> 16) & 0xff) << 8;
1853}
1854
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001855static void mlx5_ib_vma_open(struct vm_area_struct *area)
1856{
1857 /* vma_open is called when a new VMA is created on top of our VMA. This
1858 * is done through either mremap flow or split_vma (usually due to
1859 * mlock, madvise, munmap, etc.) We do not support a clone of the VMA,
1860 * as this VMA is strongly hardware related. Therefore we set the
1861 * vm_ops of the newly created/cloned VMA to NULL, to prevent it from
1862 * calling us again and trying to do incorrect actions. We assume that
1863 * the original VMA size is exactly a single page, and therefore all
1864 * "splitting" operation will not happen to it.
1865 */
1866 area->vm_ops = NULL;
1867}
1868
1869static void mlx5_ib_vma_close(struct vm_area_struct *area)
1870{
1871 struct mlx5_ib_vma_private_data *mlx5_ib_vma_priv_data;
1872
1873 /* It's guaranteed that all VMAs opened on a FD are closed before the
1874 * file itself is closed, therefore no sync is needed with the regular
1875 * closing flow. (e.g. mlx5 ib_dealloc_ucontext)
1876 * However need a sync with accessing the vma as part of
1877 * mlx5_ib_disassociate_ucontext.
1878 * The close operation is usually called under mm->mmap_sem except when
1879 * process is exiting.
1880 * The exiting case is handled explicitly as part of
1881 * mlx5_ib_disassociate_ucontext.
1882 */
1883 mlx5_ib_vma_priv_data = (struct mlx5_ib_vma_private_data *)area->vm_private_data;
1884
1885 /* setting the vma context pointer to null in the mlx5_ib driver's
1886 * private data, to protect a race condition in
1887 * mlx5_ib_disassociate_ucontext().
1888 */
1889 mlx5_ib_vma_priv_data->vma = NULL;
Majd Dibbinyad9a3662017-12-24 13:54:56 +02001890 mutex_lock(mlx5_ib_vma_priv_data->vma_private_list_mutex);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001891 list_del(&mlx5_ib_vma_priv_data->list);
Majd Dibbinyad9a3662017-12-24 13:54:56 +02001892 mutex_unlock(mlx5_ib_vma_priv_data->vma_private_list_mutex);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001893 kfree(mlx5_ib_vma_priv_data);
1894}
1895
1896static const struct vm_operations_struct mlx5_ib_vm_ops = {
1897 .open = mlx5_ib_vma_open,
1898 .close = mlx5_ib_vma_close
1899};
1900
1901static int mlx5_ib_set_vma_data(struct vm_area_struct *vma,
1902 struct mlx5_ib_ucontext *ctx)
1903{
1904 struct mlx5_ib_vma_private_data *vma_prv;
1905 struct list_head *vma_head = &ctx->vma_private_list;
1906
1907 vma_prv = kzalloc(sizeof(*vma_prv), GFP_KERNEL);
1908 if (!vma_prv)
1909 return -ENOMEM;
1910
1911 vma_prv->vma = vma;
Majd Dibbinyad9a3662017-12-24 13:54:56 +02001912 vma_prv->vma_private_list_mutex = &ctx->vma_private_list_mutex;
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001913 vma->vm_private_data = vma_prv;
1914 vma->vm_ops = &mlx5_ib_vm_ops;
1915
Majd Dibbinyad9a3662017-12-24 13:54:56 +02001916 mutex_lock(&ctx->vma_private_list_mutex);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001917 list_add(&vma_prv->list, vma_head);
Majd Dibbinyad9a3662017-12-24 13:54:56 +02001918 mutex_unlock(&ctx->vma_private_list_mutex);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001919
1920 return 0;
1921}
1922
1923static void mlx5_ib_disassociate_ucontext(struct ib_ucontext *ibcontext)
1924{
1925 int ret;
1926 struct vm_area_struct *vma;
1927 struct mlx5_ib_vma_private_data *vma_private, *n;
1928 struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1929 struct task_struct *owning_process = NULL;
1930 struct mm_struct *owning_mm = NULL;
1931
1932 owning_process = get_pid_task(ibcontext->tgid, PIDTYPE_PID);
1933 if (!owning_process)
1934 return;
1935
1936 owning_mm = get_task_mm(owning_process);
1937 if (!owning_mm) {
1938 pr_info("no mm, disassociate ucontext is pending task termination\n");
1939 while (1) {
1940 put_task_struct(owning_process);
1941 usleep_range(1000, 2000);
1942 owning_process = get_pid_task(ibcontext->tgid,
1943 PIDTYPE_PID);
1944 if (!owning_process ||
1945 owning_process->state == TASK_DEAD) {
1946 pr_info("disassociate ucontext done, task was terminated\n");
1947 /* in case task was dead need to release the
1948 * task struct.
1949 */
1950 if (owning_process)
1951 put_task_struct(owning_process);
1952 return;
1953 }
1954 }
1955 }
1956
1957 /* need to protect from a race on closing the vma as part of
1958 * mlx5_ib_vma_close.
1959 */
Maor Gottliebecc7d832017-03-29 06:03:02 +03001960 down_write(&owning_mm->mmap_sem);
Majd Dibbinyad9a3662017-12-24 13:54:56 +02001961 mutex_lock(&context->vma_private_list_mutex);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001962 list_for_each_entry_safe(vma_private, n, &context->vma_private_list,
1963 list) {
1964 vma = vma_private->vma;
1965 ret = zap_vma_ptes(vma, vma->vm_start,
1966 PAGE_SIZE);
1967 WARN_ONCE(ret, "%s: zap_vma_ptes failed", __func__);
1968 /* context going to be destroyed, should
1969 * not access ops any more.
1970 */
Maor Gottlieb13776612017-03-29 06:03:03 +03001971 vma->vm_flags &= ~(VM_SHARED | VM_MAYSHARE);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001972 vma->vm_ops = NULL;
1973 list_del(&vma_private->list);
1974 kfree(vma_private);
1975 }
Majd Dibbinyad9a3662017-12-24 13:54:56 +02001976 mutex_unlock(&context->vma_private_list_mutex);
Maor Gottliebecc7d832017-03-29 06:03:02 +03001977 up_write(&owning_mm->mmap_sem);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001978 mmput(owning_mm);
1979 put_task_struct(owning_process);
1980}
1981
Guy Levi37aa5c32016-04-27 16:49:50 +03001982static inline char *mmap_cmd2str(enum mlx5_ib_mmap_cmd cmd)
1983{
1984 switch (cmd) {
1985 case MLX5_IB_MMAP_WC_PAGE:
1986 return "WC";
1987 case MLX5_IB_MMAP_REGULAR_PAGE:
1988 return "best effort WC";
1989 case MLX5_IB_MMAP_NC_PAGE:
1990 return "NC";
1991 default:
1992 return NULL;
1993 }
1994}
1995
Feras Daoud5c99eae2018-01-16 20:08:41 +02001996static int mlx5_ib_mmap_clock_info_page(struct mlx5_ib_dev *dev,
1997 struct vm_area_struct *vma,
1998 struct mlx5_ib_ucontext *context)
1999{
2000 phys_addr_t pfn;
2001 int err;
2002
2003 if (vma->vm_end - vma->vm_start != PAGE_SIZE)
2004 return -EINVAL;
2005
2006 if (get_index(vma->vm_pgoff) != MLX5_IB_CLOCK_INFO_V1)
2007 return -EOPNOTSUPP;
2008
2009 if (vma->vm_flags & VM_WRITE)
2010 return -EPERM;
2011
2012 if (!dev->mdev->clock_info_page)
2013 return -EOPNOTSUPP;
2014
2015 pfn = page_to_pfn(dev->mdev->clock_info_page);
2016 err = remap_pfn_range(vma, vma->vm_start, pfn, PAGE_SIZE,
2017 vma->vm_page_prot);
2018 if (err)
2019 return err;
2020
2021 mlx5_ib_dbg(dev, "mapped clock info at 0x%lx, PA 0x%llx\n",
2022 vma->vm_start,
2023 (unsigned long long)pfn << PAGE_SHIFT);
2024
2025 return mlx5_ib_set_vma_data(vma, context);
2026}
2027
Guy Levi37aa5c32016-04-27 16:49:50 +03002028static int uar_mmap(struct mlx5_ib_dev *dev, enum mlx5_ib_mmap_cmd cmd,
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03002029 struct vm_area_struct *vma,
2030 struct mlx5_ib_ucontext *context)
Guy Levi37aa5c32016-04-27 16:49:50 +03002031{
Eli Cohen2f5ff262017-01-03 23:55:21 +02002032 struct mlx5_bfreg_info *bfregi = &context->bfregi;
Guy Levi37aa5c32016-04-27 16:49:50 +03002033 int err;
2034 unsigned long idx;
2035 phys_addr_t pfn, pa;
2036 pgprot_t prot;
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002037 u32 bfreg_dyn_idx = 0;
2038 u32 uar_index;
2039 int dyn_uar = (cmd == MLX5_IB_MMAP_ALLOC_WC);
2040 int max_valid_idx = dyn_uar ? bfregi->num_sys_pages :
2041 bfregi->num_static_sys_pages;
Eli Cohenb037c292017-01-03 23:55:26 +02002042
2043 if (vma->vm_end - vma->vm_start != PAGE_SIZE)
2044 return -EINVAL;
2045
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002046 if (dyn_uar)
2047 idx = get_extended_index(vma->vm_pgoff) + bfregi->num_static_sys_pages;
2048 else
2049 idx = get_index(vma->vm_pgoff);
2050
2051 if (idx >= max_valid_idx) {
2052 mlx5_ib_warn(dev, "invalid uar index %lu, max=%d\n",
2053 idx, max_valid_idx);
Eli Cohenb037c292017-01-03 23:55:26 +02002054 return -EINVAL;
2055 }
Guy Levi37aa5c32016-04-27 16:49:50 +03002056
2057 switch (cmd) {
2058 case MLX5_IB_MMAP_WC_PAGE:
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002059 case MLX5_IB_MMAP_ALLOC_WC:
Guy Levi37aa5c32016-04-27 16:49:50 +03002060/* Some architectures don't support WC memory */
2061#if defined(CONFIG_X86)
2062 if (!pat_enabled())
2063 return -EPERM;
2064#elif !(defined(CONFIG_PPC) || (defined(CONFIG_ARM) && defined(CONFIG_MMU)))
2065 return -EPERM;
2066#endif
2067 /* fall through */
2068 case MLX5_IB_MMAP_REGULAR_PAGE:
2069 /* For MLX5_IB_MMAP_REGULAR_PAGE do the best effort to get WC */
2070 prot = pgprot_writecombine(vma->vm_page_prot);
2071 break;
2072 case MLX5_IB_MMAP_NC_PAGE:
2073 prot = pgprot_noncached(vma->vm_page_prot);
2074 break;
2075 default:
2076 return -EINVAL;
2077 }
2078
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002079 if (dyn_uar) {
2080 int uars_per_page;
2081
2082 uars_per_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k);
2083 bfreg_dyn_idx = idx * (uars_per_page * MLX5_NON_FP_BFREGS_PER_UAR);
2084 if (bfreg_dyn_idx >= bfregi->total_num_bfregs) {
2085 mlx5_ib_warn(dev, "invalid bfreg_dyn_idx %u, max=%u\n",
2086 bfreg_dyn_idx, bfregi->total_num_bfregs);
2087 return -EINVAL;
2088 }
2089
2090 mutex_lock(&bfregi->lock);
2091 /* Fail if uar already allocated, first bfreg index of each
2092 * page holds its count.
2093 */
2094 if (bfregi->count[bfreg_dyn_idx]) {
2095 mlx5_ib_warn(dev, "wrong offset, idx %lu is busy, bfregn=%u\n", idx, bfreg_dyn_idx);
2096 mutex_unlock(&bfregi->lock);
2097 return -EINVAL;
2098 }
2099
2100 bfregi->count[bfreg_dyn_idx]++;
2101 mutex_unlock(&bfregi->lock);
2102
2103 err = mlx5_cmd_alloc_uar(dev->mdev, &uar_index);
2104 if (err) {
2105 mlx5_ib_warn(dev, "UAR alloc failed\n");
2106 goto free_bfreg;
2107 }
2108 } else {
2109 uar_index = bfregi->sys_pages[idx];
2110 }
2111
2112 pfn = uar_index2pfn(dev, uar_index);
Guy Levi37aa5c32016-04-27 16:49:50 +03002113 mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn %pa\n", idx, &pfn);
2114
2115 vma->vm_page_prot = prot;
2116 err = io_remap_pfn_range(vma, vma->vm_start, pfn,
2117 PAGE_SIZE, vma->vm_page_prot);
2118 if (err) {
2119 mlx5_ib_err(dev, "io_remap_pfn_range failed with error=%d, vm_start=0x%lx, pfn=%pa, mmap_cmd=%s\n",
2120 err, vma->vm_start, &pfn, mmap_cmd2str(cmd));
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002121 err = -EAGAIN;
2122 goto err;
Guy Levi37aa5c32016-04-27 16:49:50 +03002123 }
2124
2125 pa = pfn << PAGE_SHIFT;
2126 mlx5_ib_dbg(dev, "mapped %s at 0x%lx, PA %pa\n", mmap_cmd2str(cmd),
2127 vma->vm_start, &pa);
2128
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002129 err = mlx5_ib_set_vma_data(vma, context);
2130 if (err)
2131 goto err;
2132
2133 if (dyn_uar)
2134 bfregi->sys_pages[idx] = uar_index;
2135 return 0;
2136
2137err:
2138 if (!dyn_uar)
2139 return err;
2140
2141 mlx5_cmd_free_uar(dev->mdev, idx);
2142
2143free_bfreg:
2144 mlx5_ib_free_bfreg(dev, bfregi, bfreg_dyn_idx);
2145
2146 return err;
Guy Levi37aa5c32016-04-27 16:49:50 +03002147}
2148
Eli Cohene126ba92013-07-07 17:25:49 +03002149static int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma)
2150{
2151 struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
2152 struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
Eli Cohene126ba92013-07-07 17:25:49 +03002153 unsigned long command;
Eli Cohene126ba92013-07-07 17:25:49 +03002154 phys_addr_t pfn;
2155
2156 command = get_command(vma->vm_pgoff);
2157 switch (command) {
Guy Levi37aa5c32016-04-27 16:49:50 +03002158 case MLX5_IB_MMAP_WC_PAGE:
2159 case MLX5_IB_MMAP_NC_PAGE:
Eli Cohene126ba92013-07-07 17:25:49 +03002160 case MLX5_IB_MMAP_REGULAR_PAGE:
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002161 case MLX5_IB_MMAP_ALLOC_WC:
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03002162 return uar_mmap(dev, command, vma, context);
Eli Cohene126ba92013-07-07 17:25:49 +03002163
2164 case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES:
2165 return -ENOSYS;
2166
Matan Barakd69e3bc2015-12-15 20:30:13 +02002167 case MLX5_IB_MMAP_CORE_CLOCK:
Matan Barakd69e3bc2015-12-15 20:30:13 +02002168 if (vma->vm_end - vma->vm_start != PAGE_SIZE)
2169 return -EINVAL;
2170
Matan Barak6cbac1e2016-04-14 16:52:10 +03002171 if (vma->vm_flags & VM_WRITE)
Matan Barakd69e3bc2015-12-15 20:30:13 +02002172 return -EPERM;
2173
2174 /* Don't expose to user-space information it shouldn't have */
2175 if (PAGE_SIZE > 4096)
2176 return -EOPNOTSUPP;
2177
2178 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
2179 pfn = (dev->mdev->iseg_base +
2180 offsetof(struct mlx5_init_seg, internal_timer_h)) >>
2181 PAGE_SHIFT;
2182 if (io_remap_pfn_range(vma, vma->vm_start, pfn,
2183 PAGE_SIZE, vma->vm_page_prot))
2184 return -EAGAIN;
2185
2186 mlx5_ib_dbg(dev, "mapped internal timer at 0x%lx, PA 0x%llx\n",
2187 vma->vm_start,
2188 (unsigned long long)pfn << PAGE_SHIFT);
2189 break;
Feras Daoud5c99eae2018-01-16 20:08:41 +02002190 case MLX5_IB_MMAP_CLOCK_INFO:
2191 return mlx5_ib_mmap_clock_info_page(dev, vma, context);
Matan Barakd69e3bc2015-12-15 20:30:13 +02002192
Eli Cohene126ba92013-07-07 17:25:49 +03002193 default:
2194 return -EINVAL;
2195 }
2196
2197 return 0;
2198}
2199
Eli Cohene126ba92013-07-07 17:25:49 +03002200static struct ib_pd *mlx5_ib_alloc_pd(struct ib_device *ibdev,
2201 struct ib_ucontext *context,
2202 struct ib_udata *udata)
2203{
2204 struct mlx5_ib_alloc_pd_resp resp;
2205 struct mlx5_ib_pd *pd;
2206 int err;
2207
2208 pd = kmalloc(sizeof(*pd), GFP_KERNEL);
2209 if (!pd)
2210 return ERR_PTR(-ENOMEM);
2211
Jack Morgenstein9603b612014-07-28 23:30:22 +03002212 err = mlx5_core_alloc_pd(to_mdev(ibdev)->mdev, &pd->pdn);
Eli Cohene126ba92013-07-07 17:25:49 +03002213 if (err) {
2214 kfree(pd);
2215 return ERR_PTR(err);
2216 }
2217
2218 if (context) {
2219 resp.pdn = pd->pdn;
2220 if (ib_copy_to_udata(udata, &resp, sizeof(resp))) {
Jack Morgenstein9603b612014-07-28 23:30:22 +03002221 mlx5_core_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn);
Eli Cohene126ba92013-07-07 17:25:49 +03002222 kfree(pd);
2223 return ERR_PTR(-EFAULT);
2224 }
Eli Cohene126ba92013-07-07 17:25:49 +03002225 }
2226
2227 return &pd->ibpd;
2228}
2229
2230static int mlx5_ib_dealloc_pd(struct ib_pd *pd)
2231{
2232 struct mlx5_ib_dev *mdev = to_mdev(pd->device);
2233 struct mlx5_ib_pd *mpd = to_mpd(pd);
2234
Jack Morgenstein9603b612014-07-28 23:30:22 +03002235 mlx5_core_dealloc_pd(mdev->mdev, mpd->pdn);
Eli Cohene126ba92013-07-07 17:25:49 +03002236 kfree(mpd);
2237
2238 return 0;
2239}
2240
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002241enum {
2242 MATCH_CRITERIA_ENABLE_OUTER_BIT,
2243 MATCH_CRITERIA_ENABLE_MISC_BIT,
2244 MATCH_CRITERIA_ENABLE_INNER_BIT
2245};
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002246
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002247#define HEADER_IS_ZERO(match_criteria, headers) \
2248 !(memchr_inv(MLX5_ADDR_OF(fte_match_param, match_criteria, headers), \
2249 0, MLX5_FLD_SZ_BYTES(fte_match_param, headers))) \
2250
2251static u8 get_match_criteria_enable(u32 *match_criteria)
2252{
2253 u8 match_criteria_enable;
2254
2255 match_criteria_enable =
2256 (!HEADER_IS_ZERO(match_criteria, outer_headers)) <<
2257 MATCH_CRITERIA_ENABLE_OUTER_BIT;
2258 match_criteria_enable |=
2259 (!HEADER_IS_ZERO(match_criteria, misc_parameters)) <<
2260 MATCH_CRITERIA_ENABLE_MISC_BIT;
2261 match_criteria_enable |=
2262 (!HEADER_IS_ZERO(match_criteria, inner_headers)) <<
2263 MATCH_CRITERIA_ENABLE_INNER_BIT;
2264
2265 return match_criteria_enable;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002266}
2267
Maor Gottliebca0d4752016-08-30 16:58:35 +03002268static void set_proto(void *outer_c, void *outer_v, u8 mask, u8 val)
2269{
2270 MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_protocol, mask);
2271 MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_protocol, val);
2272}
2273
Moses Reuben2d1e6972016-11-14 19:04:52 +02002274static void set_flow_label(void *misc_c, void *misc_v, u8 mask, u8 val,
2275 bool inner)
2276{
2277 if (inner) {
2278 MLX5_SET(fte_match_set_misc,
2279 misc_c, inner_ipv6_flow_label, mask);
2280 MLX5_SET(fte_match_set_misc,
2281 misc_v, inner_ipv6_flow_label, val);
2282 } else {
2283 MLX5_SET(fte_match_set_misc,
2284 misc_c, outer_ipv6_flow_label, mask);
2285 MLX5_SET(fte_match_set_misc,
2286 misc_v, outer_ipv6_flow_label, val);
2287 }
2288}
2289
Maor Gottliebca0d4752016-08-30 16:58:35 +03002290static void set_tos(void *outer_c, void *outer_v, u8 mask, u8 val)
2291{
2292 MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_ecn, mask);
2293 MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_ecn, val);
2294 MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_dscp, mask >> 2);
2295 MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_dscp, val >> 2);
2296}
2297
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002298#define LAST_ETH_FIELD vlan_tag
2299#define LAST_IB_FIELD sl
Maor Gottliebca0d4752016-08-30 16:58:35 +03002300#define LAST_IPV4_FIELD tos
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002301#define LAST_IPV6_FIELD traffic_class
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002302#define LAST_TCP_UDP_FIELD src_port
Moses Reubenffb30d82016-11-14 19:04:50 +02002303#define LAST_TUNNEL_FIELD tunnel_id
Moses Reuben2ac693f2017-01-18 14:59:50 +02002304#define LAST_FLOW_TAG_FIELD tag_id
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002305#define LAST_DROP_FIELD size
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002306
2307/* Field is the last supported field */
2308#define FIELDS_NOT_SUPPORTED(filter, field)\
2309 memchr_inv((void *)&filter.field +\
2310 sizeof(filter.field), 0,\
2311 sizeof(filter) -\
2312 offsetof(typeof(filter), field) -\
2313 sizeof(filter.field))
2314
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002315#define IPV4_VERSION 4
2316#define IPV6_VERSION 6
2317static int parse_flow_attr(struct mlx5_core_dev *mdev, u32 *match_c,
2318 u32 *match_v, const union ib_flow_spec *ib_spec,
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002319 u32 *tag_id, bool *is_drop)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002320{
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002321 void *misc_params_c = MLX5_ADDR_OF(fte_match_param, match_c,
2322 misc_parameters);
2323 void *misc_params_v = MLX5_ADDR_OF(fte_match_param, match_v,
2324 misc_parameters);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002325 void *headers_c;
2326 void *headers_v;
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002327 int match_ipv;
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002328
Moses Reuben2d1e6972016-11-14 19:04:52 +02002329 if (ib_spec->type & IB_FLOW_SPEC_INNER) {
2330 headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
2331 inner_headers);
2332 headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
2333 inner_headers);
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002334 match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2335 ft_field_support.inner_ip_version);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002336 } else {
2337 headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
2338 outer_headers);
2339 headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
2340 outer_headers);
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002341 match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2342 ft_field_support.outer_ip_version);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002343 }
2344
2345 switch (ib_spec->type & ~IB_FLOW_SPEC_INNER) {
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002346 case IB_FLOW_SPEC_ETH:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002347 if (FIELDS_NOT_SUPPORTED(ib_spec->eth.mask, LAST_ETH_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002348 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002349
Moses Reuben2d1e6972016-11-14 19:04:52 +02002350 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002351 dmac_47_16),
2352 ib_spec->eth.mask.dst_mac);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002353 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002354 dmac_47_16),
2355 ib_spec->eth.val.dst_mac);
2356
Moses Reuben2d1e6972016-11-14 19:04:52 +02002357 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottliebee3da802016-09-12 19:16:24 +03002358 smac_47_16),
2359 ib_spec->eth.mask.src_mac);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002360 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottliebee3da802016-09-12 19:16:24 +03002361 smac_47_16),
2362 ib_spec->eth.val.src_mac);
2363
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002364 if (ib_spec->eth.mask.vlan_tag) {
Moses Reuben2d1e6972016-11-14 19:04:52 +02002365 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Mohamad Haj Yahia10543362016-10-09 16:25:43 +03002366 cvlan_tag, 1);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002367 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Mohamad Haj Yahia10543362016-10-09 16:25:43 +03002368 cvlan_tag, 1);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002369
Moses Reuben2d1e6972016-11-14 19:04:52 +02002370 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002371 first_vid, ntohs(ib_spec->eth.mask.vlan_tag));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002372 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002373 first_vid, ntohs(ib_spec->eth.val.vlan_tag));
2374
Moses Reuben2d1e6972016-11-14 19:04:52 +02002375 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002376 first_cfi,
2377 ntohs(ib_spec->eth.mask.vlan_tag) >> 12);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002378 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002379 first_cfi,
2380 ntohs(ib_spec->eth.val.vlan_tag) >> 12);
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_prio,
2384 ntohs(ib_spec->eth.mask.vlan_tag) >> 13);
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_prio,
2387 ntohs(ib_spec->eth.val.vlan_tag) >> 13);
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 ethertype, ntohs(ib_spec->eth.mask.ether_type));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002391 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002392 ethertype, ntohs(ib_spec->eth.val.ether_type));
2393 break;
2394 case IB_FLOW_SPEC_IPV4:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002395 if (FIELDS_NOT_SUPPORTED(ib_spec->ipv4.mask, LAST_IPV4_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002396 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002397
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002398 if (match_ipv) {
2399 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2400 ip_version, 0xf);
2401 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2402 ip_version, IPV4_VERSION);
2403 } else {
2404 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2405 ethertype, 0xffff);
2406 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2407 ethertype, ETH_P_IP);
2408 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002409
Moses Reuben2d1e6972016-11-14 19:04:52 +02002410 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002411 src_ipv4_src_ipv6.ipv4_layout.ipv4),
2412 &ib_spec->ipv4.mask.src_ip,
2413 sizeof(ib_spec->ipv4.mask.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002414 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002415 src_ipv4_src_ipv6.ipv4_layout.ipv4),
2416 &ib_spec->ipv4.val.src_ip,
2417 sizeof(ib_spec->ipv4.val.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002418 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002419 dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2420 &ib_spec->ipv4.mask.dst_ip,
2421 sizeof(ib_spec->ipv4.mask.dst_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002422 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002423 dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2424 &ib_spec->ipv4.val.dst_ip,
2425 sizeof(ib_spec->ipv4.val.dst_ip));
Maor Gottliebca0d4752016-08-30 16:58:35 +03002426
Moses Reuben2d1e6972016-11-14 19:04:52 +02002427 set_tos(headers_c, headers_v,
Maor Gottliebca0d4752016-08-30 16:58:35 +03002428 ib_spec->ipv4.mask.tos, ib_spec->ipv4.val.tos);
2429
Moses Reuben2d1e6972016-11-14 19:04:52 +02002430 set_proto(headers_c, headers_v,
Maor Gottliebca0d4752016-08-30 16:58:35 +03002431 ib_spec->ipv4.mask.proto, ib_spec->ipv4.val.proto);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002432 break;
Maor Gottlieb026bae02016-06-17 15:14:51 +03002433 case IB_FLOW_SPEC_IPV6:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002434 if (FIELDS_NOT_SUPPORTED(ib_spec->ipv6.mask, LAST_IPV6_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002435 return -EOPNOTSUPP;
Maor Gottlieb026bae02016-06-17 15:14:51 +03002436
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002437 if (match_ipv) {
2438 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2439 ip_version, 0xf);
2440 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2441 ip_version, IPV6_VERSION);
2442 } else {
2443 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2444 ethertype, 0xffff);
2445 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2446 ethertype, ETH_P_IPV6);
2447 }
Maor Gottlieb026bae02016-06-17 15:14:51 +03002448
Moses Reuben2d1e6972016-11-14 19:04:52 +02002449 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb026bae02016-06-17 15:14:51 +03002450 src_ipv4_src_ipv6.ipv6_layout.ipv6),
2451 &ib_spec->ipv6.mask.src_ip,
2452 sizeof(ib_spec->ipv6.mask.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002453 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb026bae02016-06-17 15:14:51 +03002454 src_ipv4_src_ipv6.ipv6_layout.ipv6),
2455 &ib_spec->ipv6.val.src_ip,
2456 sizeof(ib_spec->ipv6.val.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002457 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb026bae02016-06-17 15:14:51 +03002458 dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2459 &ib_spec->ipv6.mask.dst_ip,
2460 sizeof(ib_spec->ipv6.mask.dst_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002461 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb026bae02016-06-17 15:14:51 +03002462 dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2463 &ib_spec->ipv6.val.dst_ip,
2464 sizeof(ib_spec->ipv6.val.dst_ip));
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002465
Moses Reuben2d1e6972016-11-14 19:04:52 +02002466 set_tos(headers_c, headers_v,
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002467 ib_spec->ipv6.mask.traffic_class,
2468 ib_spec->ipv6.val.traffic_class);
2469
Moses Reuben2d1e6972016-11-14 19:04:52 +02002470 set_proto(headers_c, headers_v,
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002471 ib_spec->ipv6.mask.next_hdr,
2472 ib_spec->ipv6.val.next_hdr);
2473
Moses Reuben2d1e6972016-11-14 19:04:52 +02002474 set_flow_label(misc_params_c, misc_params_v,
2475 ntohl(ib_spec->ipv6.mask.flow_label),
2476 ntohl(ib_spec->ipv6.val.flow_label),
2477 ib_spec->type & IB_FLOW_SPEC_INNER);
2478
Maor Gottlieb026bae02016-06-17 15:14:51 +03002479 break;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002480 case IB_FLOW_SPEC_TCP:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002481 if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
2482 LAST_TCP_UDP_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002483 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002484
Moses Reuben2d1e6972016-11-14 19:04:52 +02002485 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002486 0xff);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002487 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002488 IPPROTO_TCP);
2489
Moses Reuben2d1e6972016-11-14 19:04:52 +02002490 MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002491 ntohs(ib_spec->tcp_udp.mask.src_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002492 MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002493 ntohs(ib_spec->tcp_udp.val.src_port));
2494
Moses Reuben2d1e6972016-11-14 19:04:52 +02002495 MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002496 ntohs(ib_spec->tcp_udp.mask.dst_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002497 MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002498 ntohs(ib_spec->tcp_udp.val.dst_port));
2499 break;
2500 case IB_FLOW_SPEC_UDP:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002501 if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
2502 LAST_TCP_UDP_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002503 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002504
Moses Reuben2d1e6972016-11-14 19:04:52 +02002505 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002506 0xff);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002507 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002508 IPPROTO_UDP);
2509
Moses Reuben2d1e6972016-11-14 19:04:52 +02002510 MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002511 ntohs(ib_spec->tcp_udp.mask.src_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002512 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002513 ntohs(ib_spec->tcp_udp.val.src_port));
2514
Moses Reuben2d1e6972016-11-14 19:04:52 +02002515 MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002516 ntohs(ib_spec->tcp_udp.mask.dst_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002517 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002518 ntohs(ib_spec->tcp_udp.val.dst_port));
2519 break;
Moses Reubenffb30d82016-11-14 19:04:50 +02002520 case IB_FLOW_SPEC_VXLAN_TUNNEL:
2521 if (FIELDS_NOT_SUPPORTED(ib_spec->tunnel.mask,
2522 LAST_TUNNEL_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002523 return -EOPNOTSUPP;
Moses Reubenffb30d82016-11-14 19:04:50 +02002524
2525 MLX5_SET(fte_match_set_misc, misc_params_c, vxlan_vni,
2526 ntohl(ib_spec->tunnel.mask.tunnel_id));
2527 MLX5_SET(fte_match_set_misc, misc_params_v, vxlan_vni,
2528 ntohl(ib_spec->tunnel.val.tunnel_id));
2529 break;
Moses Reuben2ac693f2017-01-18 14:59:50 +02002530 case IB_FLOW_SPEC_ACTION_TAG:
2531 if (FIELDS_NOT_SUPPORTED(ib_spec->flow_tag,
2532 LAST_FLOW_TAG_FIELD))
2533 return -EOPNOTSUPP;
2534 if (ib_spec->flow_tag.tag_id >= BIT(24))
2535 return -EINVAL;
2536
2537 *tag_id = ib_spec->flow_tag.tag_id;
2538 break;
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002539 case IB_FLOW_SPEC_ACTION_DROP:
2540 if (FIELDS_NOT_SUPPORTED(ib_spec->drop,
2541 LAST_DROP_FIELD))
2542 return -EOPNOTSUPP;
2543 *is_drop = true;
2544 break;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002545 default:
2546 return -EINVAL;
2547 }
2548
2549 return 0;
2550}
2551
2552/* If a flow could catch both multicast and unicast packets,
2553 * it won't fall into the multicast flow steering table and this rule
2554 * could steal other multicast packets.
2555 */
Yishai Hadasa550ddf2017-08-17 15:52:33 +03002556static bool flow_is_multicast_only(const struct ib_flow_attr *ib_attr)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002557{
Yishai Hadas81e30882017-06-08 16:15:09 +03002558 union ib_flow_spec *flow_spec;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002559
2560 if (ib_attr->type != IB_FLOW_ATTR_NORMAL ||
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002561 ib_attr->num_of_specs < 1)
2562 return false;
2563
Yishai Hadas81e30882017-06-08 16:15:09 +03002564 flow_spec = (union ib_flow_spec *)(ib_attr + 1);
2565 if (flow_spec->type == IB_FLOW_SPEC_IPV4) {
2566 struct ib_flow_spec_ipv4 *ipv4_spec;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002567
Yishai Hadas81e30882017-06-08 16:15:09 +03002568 ipv4_spec = (struct ib_flow_spec_ipv4 *)flow_spec;
2569 if (ipv4_is_multicast(ipv4_spec->val.dst_ip))
2570 return true;
2571
2572 return false;
2573 }
2574
2575 if (flow_spec->type == IB_FLOW_SPEC_ETH) {
2576 struct ib_flow_spec_eth *eth_spec;
2577
2578 eth_spec = (struct ib_flow_spec_eth *)flow_spec;
2579 return is_multicast_ether_addr(eth_spec->mask.dst_mac) &&
2580 is_multicast_ether_addr(eth_spec->val.dst_mac);
2581 }
2582
2583 return false;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002584}
2585
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002586static bool is_valid_ethertype(struct mlx5_core_dev *mdev,
2587 const struct ib_flow_attr *flow_attr,
Ariel Levkovich0f750962017-04-03 13:11:02 +03002588 bool check_inner)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002589{
2590 union ib_flow_spec *ib_spec = (union ib_flow_spec *)(flow_attr + 1);
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002591 int match_ipv = check_inner ?
2592 MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2593 ft_field_support.inner_ip_version) :
2594 MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2595 ft_field_support.outer_ip_version);
Ariel Levkovich0f750962017-04-03 13:11:02 +03002596 int inner_bit = check_inner ? IB_FLOW_SPEC_INNER : 0;
2597 bool ipv4_spec_valid, ipv6_spec_valid;
2598 unsigned int ip_spec_type = 0;
2599 bool has_ethertype = false;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002600 unsigned int spec_index;
Ariel Levkovich0f750962017-04-03 13:11:02 +03002601 bool mask_valid = true;
2602 u16 eth_type = 0;
2603 bool type_valid;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002604
2605 /* Validate that ethertype is correct */
2606 for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
Ariel Levkovich0f750962017-04-03 13:11:02 +03002607 if ((ib_spec->type == (IB_FLOW_SPEC_ETH | inner_bit)) &&
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002608 ib_spec->eth.mask.ether_type) {
Ariel Levkovich0f750962017-04-03 13:11:02 +03002609 mask_valid = (ib_spec->eth.mask.ether_type ==
2610 htons(0xffff));
2611 has_ethertype = true;
2612 eth_type = ntohs(ib_spec->eth.val.ether_type);
2613 } else if ((ib_spec->type == (IB_FLOW_SPEC_IPV4 | inner_bit)) ||
2614 (ib_spec->type == (IB_FLOW_SPEC_IPV6 | inner_bit))) {
2615 ip_spec_type = ib_spec->type;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002616 }
2617 ib_spec = (void *)ib_spec + ib_spec->size;
2618 }
Ariel Levkovich0f750962017-04-03 13:11:02 +03002619
2620 type_valid = (!has_ethertype) || (!ip_spec_type);
2621 if (!type_valid && mask_valid) {
2622 ipv4_spec_valid = (eth_type == ETH_P_IP) &&
2623 (ip_spec_type == (IB_FLOW_SPEC_IPV4 | inner_bit));
2624 ipv6_spec_valid = (eth_type == ETH_P_IPV6) &&
2625 (ip_spec_type == (IB_FLOW_SPEC_IPV6 | inner_bit));
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002626
2627 type_valid = (ipv4_spec_valid) || (ipv6_spec_valid) ||
2628 (((eth_type == ETH_P_MPLS_UC) ||
2629 (eth_type == ETH_P_MPLS_MC)) && match_ipv);
Ariel Levkovich0f750962017-04-03 13:11:02 +03002630 }
2631
2632 return type_valid;
2633}
2634
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002635static bool is_valid_attr(struct mlx5_core_dev *mdev,
2636 const struct ib_flow_attr *flow_attr)
Ariel Levkovich0f750962017-04-03 13:11:02 +03002637{
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002638 return is_valid_ethertype(mdev, flow_attr, false) &&
2639 is_valid_ethertype(mdev, flow_attr, true);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002640}
2641
2642static void put_flow_table(struct mlx5_ib_dev *dev,
2643 struct mlx5_ib_flow_prio *prio, bool ft_added)
2644{
2645 prio->refcount -= !!ft_added;
2646 if (!prio->refcount) {
2647 mlx5_destroy_flow_table(prio->flow_table);
2648 prio->flow_table = NULL;
2649 }
2650}
2651
2652static int mlx5_ib_destroy_flow(struct ib_flow *flow_id)
2653{
2654 struct mlx5_ib_dev *dev = to_mdev(flow_id->qp->device);
2655 struct mlx5_ib_flow_handler *handler = container_of(flow_id,
2656 struct mlx5_ib_flow_handler,
2657 ibflow);
2658 struct mlx5_ib_flow_handler *iter, *tmp;
2659
Mark Bloch9a4ca382018-01-16 14:42:35 +00002660 mutex_lock(&dev->flow_db->lock);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002661
2662 list_for_each_entry_safe(iter, tmp, &handler->list, list) {
Mark Bloch74491de2016-08-31 11:24:25 +00002663 mlx5_del_flow_rules(iter->rule);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002664 put_flow_table(dev, iter->prio, true);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002665 list_del(&iter->list);
2666 kfree(iter);
2667 }
2668
Mark Bloch74491de2016-08-31 11:24:25 +00002669 mlx5_del_flow_rules(handler->rule);
Maor Gottlieb5497adc2016-08-28 14:16:31 +03002670 put_flow_table(dev, handler->prio, true);
Mark Bloch9a4ca382018-01-16 14:42:35 +00002671 mutex_unlock(&dev->flow_db->lock);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002672
2673 kfree(handler);
2674
2675 return 0;
2676}
2677
Maor Gottlieb35d190112016-03-07 18:51:47 +02002678static int ib_prio_to_core_prio(unsigned int priority, bool dont_trap)
2679{
2680 priority *= 2;
2681 if (!dont_trap)
2682 priority++;
2683 return priority;
2684}
2685
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002686enum flow_table_type {
2687 MLX5_IB_FT_RX,
2688 MLX5_IB_FT_TX
2689};
2690
Maor Gottlieb00b7c2a2017-03-29 06:09:01 +03002691#define MLX5_FS_MAX_TYPES 6
2692#define MLX5_FS_MAX_ENTRIES BIT(16)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002693static struct mlx5_ib_flow_prio *get_flow_table(struct mlx5_ib_dev *dev,
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002694 struct ib_flow_attr *flow_attr,
2695 enum flow_table_type ft_type)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002696{
Maor Gottlieb35d190112016-03-07 18:51:47 +02002697 bool dont_trap = flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002698 struct mlx5_flow_namespace *ns = NULL;
2699 struct mlx5_ib_flow_prio *prio;
2700 struct mlx5_flow_table *ft;
Maor Gottliebdac388e2017-03-29 06:09:00 +03002701 int max_table_size;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002702 int num_entries;
2703 int num_groups;
2704 int priority;
2705 int err = 0;
2706
Maor Gottliebdac388e2017-03-29 06:09:00 +03002707 max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
2708 log_max_ft_size));
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002709 if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
Maor Gottlieb35d190112016-03-07 18:51:47 +02002710 if (flow_is_multicast_only(flow_attr) &&
2711 !dont_trap)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002712 priority = MLX5_IB_FLOW_MCAST_PRIO;
2713 else
Maor Gottlieb35d190112016-03-07 18:51:47 +02002714 priority = ib_prio_to_core_prio(flow_attr->priority,
2715 dont_trap);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002716 ns = mlx5_get_flow_namespace(dev->mdev,
2717 MLX5_FLOW_NAMESPACE_BYPASS);
2718 num_entries = MLX5_FS_MAX_ENTRIES;
2719 num_groups = MLX5_FS_MAX_TYPES;
Mark Bloch9a4ca382018-01-16 14:42:35 +00002720 prio = &dev->flow_db->prios[priority];
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002721 } else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
2722 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
2723 ns = mlx5_get_flow_namespace(dev->mdev,
2724 MLX5_FLOW_NAMESPACE_LEFTOVERS);
2725 build_leftovers_ft_param(&priority,
2726 &num_entries,
2727 &num_groups);
Mark Bloch9a4ca382018-01-16 14:42:35 +00002728 prio = &dev->flow_db->prios[MLX5_IB_FLOW_LEFTOVERS_PRIO];
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002729 } else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
2730 if (!MLX5_CAP_FLOWTABLE(dev->mdev,
2731 allow_sniffer_and_nic_rx_shared_tir))
2732 return ERR_PTR(-ENOTSUPP);
2733
2734 ns = mlx5_get_flow_namespace(dev->mdev, ft_type == MLX5_IB_FT_RX ?
2735 MLX5_FLOW_NAMESPACE_SNIFFER_RX :
2736 MLX5_FLOW_NAMESPACE_SNIFFER_TX);
2737
Mark Bloch9a4ca382018-01-16 14:42:35 +00002738 prio = &dev->flow_db->sniffer[ft_type];
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002739 priority = 0;
2740 num_entries = 1;
2741 num_groups = 1;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002742 }
2743
2744 if (!ns)
2745 return ERR_PTR(-ENOTSUPP);
2746
Maor Gottliebdac388e2017-03-29 06:09:00 +03002747 if (num_entries > max_table_size)
2748 return ERR_PTR(-ENOMEM);
2749
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002750 ft = prio->flow_table;
2751 if (!ft) {
2752 ft = mlx5_create_auto_grouped_flow_table(ns, priority,
2753 num_entries,
Maor Gottliebd63cd282016-04-29 01:36:35 +03002754 num_groups,
Hadar Hen Zionc9f1b072016-11-07 15:14:44 +02002755 0, 0);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002756
2757 if (!IS_ERR(ft)) {
2758 prio->refcount = 0;
2759 prio->flow_table = ft;
2760 } else {
2761 err = PTR_ERR(ft);
2762 }
2763 }
2764
2765 return err ? ERR_PTR(err) : prio;
2766}
2767
Yishai Hadasa550ddf2017-08-17 15:52:33 +03002768static void set_underlay_qp(struct mlx5_ib_dev *dev,
2769 struct mlx5_flow_spec *spec,
2770 u32 underlay_qpn)
2771{
2772 void *misc_params_c = MLX5_ADDR_OF(fte_match_param,
2773 spec->match_criteria,
2774 misc_parameters);
2775 void *misc_params_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
2776 misc_parameters);
2777
2778 if (underlay_qpn &&
2779 MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
2780 ft_field_support.bth_dst_qp)) {
2781 MLX5_SET(fte_match_set_misc,
2782 misc_params_v, bth_dst_qp, underlay_qpn);
2783 MLX5_SET(fte_match_set_misc,
2784 misc_params_c, bth_dst_qp, 0xffffff);
2785 }
2786}
2787
2788static struct mlx5_ib_flow_handler *_create_flow_rule(struct mlx5_ib_dev *dev,
2789 struct mlx5_ib_flow_prio *ft_prio,
2790 const struct ib_flow_attr *flow_attr,
2791 struct mlx5_flow_destination *dst,
2792 u32 underlay_qpn)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002793{
2794 struct mlx5_flow_table *ft = ft_prio->flow_table;
2795 struct mlx5_ib_flow_handler *handler;
Hadar Hen Zion66958ed2016-11-07 15:14:45 +02002796 struct mlx5_flow_act flow_act = {0};
Maor Gottliebc5bb1732016-07-04 17:23:05 +03002797 struct mlx5_flow_spec *spec;
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002798 struct mlx5_flow_destination *rule_dst = dst;
Maor Gottliebdd063d02016-08-28 14:16:32 +03002799 const void *ib_flow = (const void *)flow_attr + sizeof(*flow_attr);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002800 unsigned int spec_index;
Moses Reuben2ac693f2017-01-18 14:59:50 +02002801 u32 flow_tag = MLX5_FS_DEFAULT_FLOW_TAG;
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002802 bool is_drop = false;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002803 int err = 0;
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002804 int dest_num = 1;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002805
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002806 if (!is_valid_attr(dev->mdev, flow_attr))
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002807 return ERR_PTR(-EINVAL);
2808
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03002809 spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002810 handler = kzalloc(sizeof(*handler), GFP_KERNEL);
Maor Gottliebc5bb1732016-07-04 17:23:05 +03002811 if (!handler || !spec) {
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002812 err = -ENOMEM;
2813 goto free;
2814 }
2815
2816 INIT_LIST_HEAD(&handler->list);
2817
2818 for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002819 err = parse_flow_attr(dev->mdev, spec->match_criteria,
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002820 spec->match_value,
2821 ib_flow, &flow_tag, &is_drop);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002822 if (err < 0)
2823 goto free;
2824
2825 ib_flow += ((union ib_flow_spec *)ib_flow)->size;
2826 }
2827
Yishai Hadasa550ddf2017-08-17 15:52:33 +03002828 if (!flow_is_multicast_only(flow_attr))
2829 set_underlay_qp(dev, spec, underlay_qpn);
2830
Mark Bloch018a94e2018-01-16 14:44:29 +00002831 if (dev->rep) {
2832 void *misc;
2833
2834 misc = MLX5_ADDR_OF(fte_match_param, spec->match_value,
2835 misc_parameters);
2836 MLX5_SET(fte_match_set_misc, misc, source_port,
2837 dev->rep->vport);
2838 misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
2839 misc_parameters);
2840 MLX5_SET_TO_ONES(fte_match_set_misc, misc, source_port);
2841 }
2842
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002843 spec->match_criteria_enable = get_match_criteria_enable(spec->match_criteria);
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002844 if (is_drop) {
2845 flow_act.action = MLX5_FLOW_CONTEXT_ACTION_DROP;
2846 rule_dst = NULL;
2847 dest_num = 0;
2848 } else {
2849 flow_act.action = dst ? MLX5_FLOW_CONTEXT_ACTION_FWD_DEST :
2850 MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO;
2851 }
Moses Reuben2ac693f2017-01-18 14:59:50 +02002852
2853 if (flow_tag != MLX5_FS_DEFAULT_FLOW_TAG &&
2854 (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
2855 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT)) {
2856 mlx5_ib_warn(dev, "Flow tag %u and attribute type %x isn't allowed in leftovers\n",
2857 flow_tag, flow_attr->type);
2858 err = -EINVAL;
2859 goto free;
2860 }
2861 flow_act.flow_tag = flow_tag;
Mark Bloch74491de2016-08-31 11:24:25 +00002862 handler->rule = mlx5_add_flow_rules(ft, spec,
Hadar Hen Zion66958ed2016-11-07 15:14:45 +02002863 &flow_act,
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002864 rule_dst, dest_num);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002865
2866 if (IS_ERR(handler->rule)) {
2867 err = PTR_ERR(handler->rule);
2868 goto free;
2869 }
2870
Maor Gottliebd9d49802016-08-28 14:16:33 +03002871 ft_prio->refcount++;
Maor Gottlieb5497adc2016-08-28 14:16:31 +03002872 handler->prio = ft_prio;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002873
2874 ft_prio->flow_table = ft;
2875free:
2876 if (err)
2877 kfree(handler);
Maor Gottliebc5bb1732016-07-04 17:23:05 +03002878 kvfree(spec);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002879 return err ? ERR_PTR(err) : handler;
2880}
2881
Yishai Hadasa550ddf2017-08-17 15:52:33 +03002882static struct mlx5_ib_flow_handler *create_flow_rule(struct mlx5_ib_dev *dev,
2883 struct mlx5_ib_flow_prio *ft_prio,
2884 const struct ib_flow_attr *flow_attr,
2885 struct mlx5_flow_destination *dst)
2886{
2887 return _create_flow_rule(dev, ft_prio, flow_attr, dst, 0);
2888}
2889
Maor Gottlieb35d190112016-03-07 18:51:47 +02002890static struct mlx5_ib_flow_handler *create_dont_trap_rule(struct mlx5_ib_dev *dev,
2891 struct mlx5_ib_flow_prio *ft_prio,
2892 struct ib_flow_attr *flow_attr,
2893 struct mlx5_flow_destination *dst)
2894{
2895 struct mlx5_ib_flow_handler *handler_dst = NULL;
2896 struct mlx5_ib_flow_handler *handler = NULL;
2897
2898 handler = create_flow_rule(dev, ft_prio, flow_attr, NULL);
2899 if (!IS_ERR(handler)) {
2900 handler_dst = create_flow_rule(dev, ft_prio,
2901 flow_attr, dst);
2902 if (IS_ERR(handler_dst)) {
Mark Bloch74491de2016-08-31 11:24:25 +00002903 mlx5_del_flow_rules(handler->rule);
Maor Gottliebd9d49802016-08-28 14:16:33 +03002904 ft_prio->refcount--;
Maor Gottlieb35d190112016-03-07 18:51:47 +02002905 kfree(handler);
2906 handler = handler_dst;
2907 } else {
2908 list_add(&handler_dst->list, &handler->list);
2909 }
2910 }
2911
2912 return handler;
2913}
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002914enum {
2915 LEFTOVERS_MC,
2916 LEFTOVERS_UC,
2917};
2918
2919static struct mlx5_ib_flow_handler *create_leftovers_rule(struct mlx5_ib_dev *dev,
2920 struct mlx5_ib_flow_prio *ft_prio,
2921 struct ib_flow_attr *flow_attr,
2922 struct mlx5_flow_destination *dst)
2923{
2924 struct mlx5_ib_flow_handler *handler_ucast = NULL;
2925 struct mlx5_ib_flow_handler *handler = NULL;
2926
2927 static struct {
2928 struct ib_flow_attr flow_attr;
2929 struct ib_flow_spec_eth eth_flow;
2930 } leftovers_specs[] = {
2931 [LEFTOVERS_MC] = {
2932 .flow_attr = {
2933 .num_of_specs = 1,
2934 .size = sizeof(leftovers_specs[0])
2935 },
2936 .eth_flow = {
2937 .type = IB_FLOW_SPEC_ETH,
2938 .size = sizeof(struct ib_flow_spec_eth),
2939 .mask = {.dst_mac = {0x1} },
2940 .val = {.dst_mac = {0x1} }
2941 }
2942 },
2943 [LEFTOVERS_UC] = {
2944 .flow_attr = {
2945 .num_of_specs = 1,
2946 .size = sizeof(leftovers_specs[0])
2947 },
2948 .eth_flow = {
2949 .type = IB_FLOW_SPEC_ETH,
2950 .size = sizeof(struct ib_flow_spec_eth),
2951 .mask = {.dst_mac = {0x1} },
2952 .val = {.dst_mac = {} }
2953 }
2954 }
2955 };
2956
2957 handler = create_flow_rule(dev, ft_prio,
2958 &leftovers_specs[LEFTOVERS_MC].flow_attr,
2959 dst);
2960 if (!IS_ERR(handler) &&
2961 flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT) {
2962 handler_ucast = create_flow_rule(dev, ft_prio,
2963 &leftovers_specs[LEFTOVERS_UC].flow_attr,
2964 dst);
2965 if (IS_ERR(handler_ucast)) {
Mark Bloch74491de2016-08-31 11:24:25 +00002966 mlx5_del_flow_rules(handler->rule);
Maor Gottliebd9d49802016-08-28 14:16:33 +03002967 ft_prio->refcount--;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002968 kfree(handler);
2969 handler = handler_ucast;
2970 } else {
2971 list_add(&handler_ucast->list, &handler->list);
2972 }
2973 }
2974
2975 return handler;
2976}
2977
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002978static struct mlx5_ib_flow_handler *create_sniffer_rule(struct mlx5_ib_dev *dev,
2979 struct mlx5_ib_flow_prio *ft_rx,
2980 struct mlx5_ib_flow_prio *ft_tx,
2981 struct mlx5_flow_destination *dst)
2982{
2983 struct mlx5_ib_flow_handler *handler_rx;
2984 struct mlx5_ib_flow_handler *handler_tx;
2985 int err;
2986 static const struct ib_flow_attr flow_attr = {
2987 .num_of_specs = 0,
2988 .size = sizeof(flow_attr)
2989 };
2990
2991 handler_rx = create_flow_rule(dev, ft_rx, &flow_attr, dst);
2992 if (IS_ERR(handler_rx)) {
2993 err = PTR_ERR(handler_rx);
2994 goto err;
2995 }
2996
2997 handler_tx = create_flow_rule(dev, ft_tx, &flow_attr, dst);
2998 if (IS_ERR(handler_tx)) {
2999 err = PTR_ERR(handler_tx);
3000 goto err_tx;
3001 }
3002
3003 list_add(&handler_tx->list, &handler_rx->list);
3004
3005 return handler_rx;
3006
3007err_tx:
Mark Bloch74491de2016-08-31 11:24:25 +00003008 mlx5_del_flow_rules(handler_rx->rule);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003009 ft_rx->refcount--;
3010 kfree(handler_rx);
3011err:
3012 return ERR_PTR(err);
3013}
3014
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003015static struct ib_flow *mlx5_ib_create_flow(struct ib_qp *qp,
3016 struct ib_flow_attr *flow_attr,
3017 int domain)
3018{
3019 struct mlx5_ib_dev *dev = to_mdev(qp->device);
Yishai Hadasd9f88e52016-08-28 10:58:37 +03003020 struct mlx5_ib_qp *mqp = to_mqp(qp);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003021 struct mlx5_ib_flow_handler *handler = NULL;
3022 struct mlx5_flow_destination *dst = NULL;
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003023 struct mlx5_ib_flow_prio *ft_prio_tx = NULL;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003024 struct mlx5_ib_flow_prio *ft_prio;
3025 int err;
Yishai Hadasa550ddf2017-08-17 15:52:33 +03003026 int underlay_qpn;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003027
3028 if (flow_attr->priority > MLX5_IB_FLOW_LAST_PRIO)
Maor Gottliebdac388e2017-03-29 06:09:00 +03003029 return ERR_PTR(-ENOMEM);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003030
3031 if (domain != IB_FLOW_DOMAIN_USER ||
Daniel Jurgens508562d2018-01-04 17:25:34 +02003032 flow_attr->port > dev->num_ports ||
Maor Gottlieb35d190112016-03-07 18:51:47 +02003033 (flow_attr->flags & ~IB_FLOW_ATTR_FLAGS_DONT_TRAP))
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003034 return ERR_PTR(-EINVAL);
3035
3036 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
3037 if (!dst)
3038 return ERR_PTR(-ENOMEM);
3039
Mark Bloch9a4ca382018-01-16 14:42:35 +00003040 mutex_lock(&dev->flow_db->lock);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003041
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003042 ft_prio = get_flow_table(dev, flow_attr, MLX5_IB_FT_RX);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003043 if (IS_ERR(ft_prio)) {
3044 err = PTR_ERR(ft_prio);
3045 goto unlock;
3046 }
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003047 if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
3048 ft_prio_tx = get_flow_table(dev, flow_attr, MLX5_IB_FT_TX);
3049 if (IS_ERR(ft_prio_tx)) {
3050 err = PTR_ERR(ft_prio_tx);
3051 ft_prio_tx = NULL;
3052 goto destroy_ft;
3053 }
3054 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003055
3056 dst->type = MLX5_FLOW_DESTINATION_TYPE_TIR;
Yishai Hadasd9f88e52016-08-28 10:58:37 +03003057 if (mqp->flags & MLX5_IB_QP_RSS)
3058 dst->tir_num = mqp->rss_qp.tirn;
3059 else
3060 dst->tir_num = mqp->raw_packet_qp.rq.tirn;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003061
3062 if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
Maor Gottlieb35d190112016-03-07 18:51:47 +02003063 if (flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP) {
3064 handler = create_dont_trap_rule(dev, ft_prio,
3065 flow_attr, dst);
3066 } else {
Yishai Hadasa550ddf2017-08-17 15:52:33 +03003067 underlay_qpn = (mqp->flags & MLX5_IB_QP_UNDERLAY) ?
3068 mqp->underlay_qpn : 0;
3069 handler = _create_flow_rule(dev, ft_prio, flow_attr,
3070 dst, underlay_qpn);
Maor Gottlieb35d190112016-03-07 18:51:47 +02003071 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003072 } else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
3073 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
3074 handler = create_leftovers_rule(dev, ft_prio, flow_attr,
3075 dst);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003076 } else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
3077 handler = create_sniffer_rule(dev, ft_prio, ft_prio_tx, dst);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003078 } else {
3079 err = -EINVAL;
3080 goto destroy_ft;
3081 }
3082
3083 if (IS_ERR(handler)) {
3084 err = PTR_ERR(handler);
3085 handler = NULL;
3086 goto destroy_ft;
3087 }
3088
Mark Bloch9a4ca382018-01-16 14:42:35 +00003089 mutex_unlock(&dev->flow_db->lock);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003090 kfree(dst);
3091
3092 return &handler->ibflow;
3093
3094destroy_ft:
3095 put_flow_table(dev, ft_prio, false);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003096 if (ft_prio_tx)
3097 put_flow_table(dev, ft_prio_tx, false);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003098unlock:
Mark Bloch9a4ca382018-01-16 14:42:35 +00003099 mutex_unlock(&dev->flow_db->lock);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003100 kfree(dst);
3101 kfree(handler);
3102 return ERR_PTR(err);
3103}
3104
Eli Cohene126ba92013-07-07 17:25:49 +03003105static int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
3106{
3107 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
Yishai Hadas81e30882017-06-08 16:15:09 +03003108 struct mlx5_ib_qp *mqp = to_mqp(ibqp);
Eli Cohene126ba92013-07-07 17:25:49 +03003109 int err;
3110
Yishai Hadas81e30882017-06-08 16:15:09 +03003111 if (mqp->flags & MLX5_IB_QP_UNDERLAY) {
3112 mlx5_ib_dbg(dev, "Attaching a multi cast group to underlay QP is not supported\n");
3113 return -EOPNOTSUPP;
3114 }
3115
Jack Morgenstein9603b612014-07-28 23:30:22 +03003116 err = mlx5_core_attach_mcg(dev->mdev, gid, ibqp->qp_num);
Eli Cohene126ba92013-07-07 17:25:49 +03003117 if (err)
3118 mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n",
3119 ibqp->qp_num, gid->raw);
3120
3121 return err;
3122}
3123
3124static int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
3125{
3126 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
3127 int err;
3128
Jack Morgenstein9603b612014-07-28 23:30:22 +03003129 err = mlx5_core_detach_mcg(dev->mdev, gid, ibqp->qp_num);
Eli Cohene126ba92013-07-07 17:25:49 +03003130 if (err)
3131 mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n",
3132 ibqp->qp_num, gid->raw);
3133
3134 return err;
3135}
3136
3137static int init_node_data(struct mlx5_ib_dev *dev)
3138{
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03003139 int err;
Eli Cohene126ba92013-07-07 17:25:49 +03003140
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03003141 err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc);
Eli Cohene126ba92013-07-07 17:25:49 +03003142 if (err)
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03003143 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03003144
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03003145 dev->mdev->rev_id = dev->mdev->pdev->revision;
Eli Cohene126ba92013-07-07 17:25:49 +03003146
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03003147 return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid);
Eli Cohene126ba92013-07-07 17:25:49 +03003148}
3149
3150static ssize_t show_fw_pages(struct device *device, struct device_attribute *attr,
3151 char *buf)
3152{
3153 struct mlx5_ib_dev *dev =
3154 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
3155
Jack Morgenstein9603b612014-07-28 23:30:22 +03003156 return sprintf(buf, "%d\n", dev->mdev->priv.fw_pages);
Eli Cohene126ba92013-07-07 17:25:49 +03003157}
3158
3159static ssize_t show_reg_pages(struct device *device,
3160 struct device_attribute *attr, char *buf)
3161{
3162 struct mlx5_ib_dev *dev =
3163 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
3164
Haggai Eran6aec21f2014-12-11 17:04:23 +02003165 return sprintf(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages));
Eli Cohene126ba92013-07-07 17:25:49 +03003166}
3167
3168static ssize_t show_hca(struct device *device, struct device_attribute *attr,
3169 char *buf)
3170{
3171 struct mlx5_ib_dev *dev =
3172 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
Jack Morgenstein9603b612014-07-28 23:30:22 +03003173 return sprintf(buf, "MT%d\n", dev->mdev->pdev->device);
Eli Cohene126ba92013-07-07 17:25:49 +03003174}
3175
Eli Cohene126ba92013-07-07 17:25:49 +03003176static ssize_t show_rev(struct device *device, struct device_attribute *attr,
3177 char *buf)
3178{
3179 struct mlx5_ib_dev *dev =
3180 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
Jack Morgenstein9603b612014-07-28 23:30:22 +03003181 return sprintf(buf, "%x\n", dev->mdev->rev_id);
Eli Cohene126ba92013-07-07 17:25:49 +03003182}
3183
3184static ssize_t show_board(struct device *device, struct device_attribute *attr,
3185 char *buf)
3186{
3187 struct mlx5_ib_dev *dev =
3188 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
3189 return sprintf(buf, "%.*s\n", MLX5_BOARD_ID_LEN,
Jack Morgenstein9603b612014-07-28 23:30:22 +03003190 dev->mdev->board_id);
Eli Cohene126ba92013-07-07 17:25:49 +03003191}
3192
3193static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03003194static DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL);
3195static DEVICE_ATTR(board_id, S_IRUGO, show_board, NULL);
3196static DEVICE_ATTR(fw_pages, S_IRUGO, show_fw_pages, NULL);
3197static DEVICE_ATTR(reg_pages, S_IRUGO, show_reg_pages, NULL);
3198
3199static struct device_attribute *mlx5_class_attributes[] = {
3200 &dev_attr_hw_rev,
Eli Cohene126ba92013-07-07 17:25:49 +03003201 &dev_attr_hca_type,
3202 &dev_attr_board_id,
3203 &dev_attr_fw_pages,
3204 &dev_attr_reg_pages,
3205};
3206
Haggai Eran7722f472016-02-29 15:45:07 +02003207static void pkey_change_handler(struct work_struct *work)
3208{
3209 struct mlx5_ib_port_resources *ports =
3210 container_of(work, struct mlx5_ib_port_resources,
3211 pkey_change_work);
3212
3213 mutex_lock(&ports->devr->mutex);
3214 mlx5_ib_gsi_pkey_change(ports->gsi);
3215 mutex_unlock(&ports->devr->mutex);
3216}
3217
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03003218static void mlx5_ib_handle_internal_error(struct mlx5_ib_dev *ibdev)
3219{
3220 struct mlx5_ib_qp *mqp;
3221 struct mlx5_ib_cq *send_mcq, *recv_mcq;
3222 struct mlx5_core_cq *mcq;
3223 struct list_head cq_armed_list;
3224 unsigned long flags_qp;
3225 unsigned long flags_cq;
3226 unsigned long flags;
3227
3228 INIT_LIST_HEAD(&cq_armed_list);
3229
3230 /* Go over qp list reside on that ibdev, sync with create/destroy qp.*/
3231 spin_lock_irqsave(&ibdev->reset_flow_resource_lock, flags);
3232 list_for_each_entry(mqp, &ibdev->qp_list, qps_list) {
3233 spin_lock_irqsave(&mqp->sq.lock, flags_qp);
3234 if (mqp->sq.tail != mqp->sq.head) {
3235 send_mcq = to_mcq(mqp->ibqp.send_cq);
3236 spin_lock_irqsave(&send_mcq->lock, flags_cq);
3237 if (send_mcq->mcq.comp &&
3238 mqp->ibqp.send_cq->comp_handler) {
3239 if (!send_mcq->mcq.reset_notify_added) {
3240 send_mcq->mcq.reset_notify_added = 1;
3241 list_add_tail(&send_mcq->mcq.reset_notify,
3242 &cq_armed_list);
3243 }
3244 }
3245 spin_unlock_irqrestore(&send_mcq->lock, flags_cq);
3246 }
3247 spin_unlock_irqrestore(&mqp->sq.lock, flags_qp);
3248 spin_lock_irqsave(&mqp->rq.lock, flags_qp);
3249 /* no handling is needed for SRQ */
3250 if (!mqp->ibqp.srq) {
3251 if (mqp->rq.tail != mqp->rq.head) {
3252 recv_mcq = to_mcq(mqp->ibqp.recv_cq);
3253 spin_lock_irqsave(&recv_mcq->lock, flags_cq);
3254 if (recv_mcq->mcq.comp &&
3255 mqp->ibqp.recv_cq->comp_handler) {
3256 if (!recv_mcq->mcq.reset_notify_added) {
3257 recv_mcq->mcq.reset_notify_added = 1;
3258 list_add_tail(&recv_mcq->mcq.reset_notify,
3259 &cq_armed_list);
3260 }
3261 }
3262 spin_unlock_irqrestore(&recv_mcq->lock,
3263 flags_cq);
3264 }
3265 }
3266 spin_unlock_irqrestore(&mqp->rq.lock, flags_qp);
3267 }
3268 /*At that point all inflight post send were put to be executed as of we
3269 * lock/unlock above locks Now need to arm all involved CQs.
3270 */
3271 list_for_each_entry(mcq, &cq_armed_list, reset_notify) {
3272 mcq->comp(mcq);
3273 }
3274 spin_unlock_irqrestore(&ibdev->reset_flow_resource_lock, flags);
3275}
3276
Maor Gottlieb03404e82017-05-30 10:29:13 +03003277static void delay_drop_handler(struct work_struct *work)
3278{
3279 int err;
3280 struct mlx5_ib_delay_drop *delay_drop =
3281 container_of(work, struct mlx5_ib_delay_drop,
3282 delay_drop_work);
3283
Maor Gottliebfe248c32017-05-30 10:29:14 +03003284 atomic_inc(&delay_drop->events_cnt);
3285
Maor Gottlieb03404e82017-05-30 10:29:13 +03003286 mutex_lock(&delay_drop->lock);
3287 err = mlx5_core_set_delay_drop(delay_drop->dev->mdev,
3288 delay_drop->timeout);
3289 if (err) {
3290 mlx5_ib_warn(delay_drop->dev, "Failed to set delay drop, timeout=%u\n",
3291 delay_drop->timeout);
3292 delay_drop->activate = false;
3293 }
3294 mutex_unlock(&delay_drop->lock);
3295}
3296
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02003297static void mlx5_ib_handle_event(struct work_struct *_work)
Eli Cohene126ba92013-07-07 17:25:49 +03003298{
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02003299 struct mlx5_ib_event_work *work =
3300 container_of(_work, struct mlx5_ib_event_work, work);
3301 struct mlx5_ib_dev *ibdev;
Eli Cohene126ba92013-07-07 17:25:49 +03003302 struct ib_event ibev;
Eli Cohendbaaff22016-10-27 16:36:44 +03003303 bool fatal = false;
Eli Cohene126ba92013-07-07 17:25:49 +03003304 u8 port = 0;
3305
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02003306 if (mlx5_core_is_mp_slave(work->dev)) {
3307 ibdev = mlx5_ib_get_ibdev_from_mpi(work->context);
3308 if (!ibdev)
3309 goto out;
3310 } else {
3311 ibdev = work->context;
3312 }
3313
3314 switch (work->event) {
Eli Cohene126ba92013-07-07 17:25:49 +03003315 case MLX5_DEV_EVENT_SYS_ERROR:
Eli Cohene126ba92013-07-07 17:25:49 +03003316 ibev.event = IB_EVENT_DEVICE_FATAL;
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03003317 mlx5_ib_handle_internal_error(ibdev);
Eli Cohendbaaff22016-10-27 16:36:44 +03003318 fatal = true;
Eli Cohene126ba92013-07-07 17:25:49 +03003319 break;
3320
3321 case MLX5_DEV_EVENT_PORT_UP:
Eli Cohene126ba92013-07-07 17:25:49 +03003322 case MLX5_DEV_EVENT_PORT_DOWN:
Noa Osherovich2788cf32016-06-04 15:15:29 +03003323 case MLX5_DEV_EVENT_PORT_INITIALIZED:
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02003324 port = (u8)work->param;
Aviv Heller5ec8c832016-09-18 20:48:00 +03003325
3326 /* In RoCE, port up/down events are handled in
3327 * mlx5_netdev_event().
3328 */
3329 if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) ==
3330 IB_LINK_LAYER_ETHERNET)
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02003331 goto out;
Aviv Heller5ec8c832016-09-18 20:48:00 +03003332
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02003333 ibev.event = (work->event == MLX5_DEV_EVENT_PORT_UP) ?
Aviv Heller5ec8c832016-09-18 20:48:00 +03003334 IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
Eli Cohene126ba92013-07-07 17:25:49 +03003335 break;
3336
Eli Cohene126ba92013-07-07 17:25:49 +03003337 case MLX5_DEV_EVENT_LID_CHANGE:
3338 ibev.event = IB_EVENT_LID_CHANGE;
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02003339 port = (u8)work->param;
Eli Cohene126ba92013-07-07 17:25:49 +03003340 break;
3341
3342 case MLX5_DEV_EVENT_PKEY_CHANGE:
3343 ibev.event = IB_EVENT_PKEY_CHANGE;
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02003344 port = (u8)work->param;
Haggai Eran7722f472016-02-29 15:45:07 +02003345
3346 schedule_work(&ibdev->devr.ports[port - 1].pkey_change_work);
Eli Cohene126ba92013-07-07 17:25:49 +03003347 break;
3348
3349 case MLX5_DEV_EVENT_GUID_CHANGE:
3350 ibev.event = IB_EVENT_GID_CHANGE;
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02003351 port = (u8)work->param;
Eli Cohene126ba92013-07-07 17:25:49 +03003352 break;
3353
3354 case MLX5_DEV_EVENT_CLIENT_REREG:
3355 ibev.event = IB_EVENT_CLIENT_REREGISTER;
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02003356 port = (u8)work->param;
Eli Cohene126ba92013-07-07 17:25:49 +03003357 break;
Maor Gottlieb03404e82017-05-30 10:29:13 +03003358 case MLX5_DEV_EVENT_DELAY_DROP_TIMEOUT:
3359 schedule_work(&ibdev->delay_drop.delay_drop_work);
3360 goto out;
Saeed Mahameedbdc37922016-09-29 19:35:38 +03003361 default:
Maor Gottlieb03404e82017-05-30 10:29:13 +03003362 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03003363 }
3364
3365 ibev.device = &ibdev->ib_dev;
3366 ibev.element.port_num = port;
3367
Eli Cohena0c84c32013-09-11 16:35:27 +03003368 if (port < 1 || port > ibdev->num_ports) {
3369 mlx5_ib_warn(ibdev, "warning: event on port %d\n", port);
Maor Gottlieb03404e82017-05-30 10:29:13 +03003370 goto out;
Eli Cohena0c84c32013-09-11 16:35:27 +03003371 }
3372
Eli Cohene126ba92013-07-07 17:25:49 +03003373 if (ibdev->ib_active)
3374 ib_dispatch_event(&ibev);
Eli Cohendbaaff22016-10-27 16:36:44 +03003375
3376 if (fatal)
3377 ibdev->ib_active = false;
Maor Gottlieb03404e82017-05-30 10:29:13 +03003378out:
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02003379 kfree(work);
3380}
3381
3382static void mlx5_ib_event(struct mlx5_core_dev *dev, void *context,
3383 enum mlx5_dev_event event, unsigned long param)
3384{
3385 struct mlx5_ib_event_work *work;
3386
3387 work = kmalloc(sizeof(*work), GFP_ATOMIC);
Leon Romanovsky10bea9c2018-01-19 13:07:11 +02003388 if (!work)
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02003389 return;
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02003390
Leon Romanovsky10bea9c2018-01-19 13:07:11 +02003391 INIT_WORK(&work->work, mlx5_ib_handle_event);
3392 work->dev = dev;
3393 work->param = param;
3394 work->context = context;
3395 work->event = event;
3396
3397 queue_work(mlx5_ib_event_wq, &work->work);
Eli Cohene126ba92013-07-07 17:25:49 +03003398}
3399
Maor Gottliebc43f1112017-01-18 14:10:33 +02003400static int set_has_smi_cap(struct mlx5_ib_dev *dev)
3401{
3402 struct mlx5_hca_vport_context vport_ctx;
3403 int err;
3404 int port;
3405
Daniel Jurgens508562d2018-01-04 17:25:34 +02003406 for (port = 1; port <= dev->num_ports; port++) {
Maor Gottliebc43f1112017-01-18 14:10:33 +02003407 dev->mdev->port_caps[port - 1].has_smi = false;
3408 if (MLX5_CAP_GEN(dev->mdev, port_type) ==
3409 MLX5_CAP_PORT_TYPE_IB) {
3410 if (MLX5_CAP_GEN(dev->mdev, ib_virt)) {
3411 err = mlx5_query_hca_vport_context(dev->mdev, 0,
3412 port, 0,
3413 &vport_ctx);
3414 if (err) {
3415 mlx5_ib_err(dev, "query_hca_vport_context for port=%d failed %d\n",
3416 port, err);
3417 return err;
3418 }
3419 dev->mdev->port_caps[port - 1].has_smi =
3420 vport_ctx.has_smi;
3421 } else {
3422 dev->mdev->port_caps[port - 1].has_smi = true;
3423 }
3424 }
3425 }
3426 return 0;
3427}
3428
Eli Cohene126ba92013-07-07 17:25:49 +03003429static void get_ext_port_caps(struct mlx5_ib_dev *dev)
3430{
3431 int port;
3432
Daniel Jurgens508562d2018-01-04 17:25:34 +02003433 for (port = 1; port <= dev->num_ports; port++)
Eli Cohene126ba92013-07-07 17:25:49 +03003434 mlx5_query_ext_port_caps(dev, port);
3435}
3436
Daniel Jurgens32f69e42018-01-04 17:25:36 +02003437static int get_port_caps(struct mlx5_ib_dev *dev, u8 port)
Eli Cohene126ba92013-07-07 17:25:49 +03003438{
3439 struct ib_device_attr *dprops = NULL;
3440 struct ib_port_attr *pprops = NULL;
Dan Carpenterf614fc12015-01-12 11:56:58 +03003441 int err = -ENOMEM;
Matan Barak2528e332015-06-11 16:35:25 +03003442 struct ib_udata uhw = {.inlen = 0, .outlen = 0};
Eli Cohene126ba92013-07-07 17:25:49 +03003443
3444 pprops = kmalloc(sizeof(*pprops), GFP_KERNEL);
3445 if (!pprops)
3446 goto out;
3447
3448 dprops = kmalloc(sizeof(*dprops), GFP_KERNEL);
3449 if (!dprops)
3450 goto out;
3451
Maor Gottliebc43f1112017-01-18 14:10:33 +02003452 err = set_has_smi_cap(dev);
3453 if (err)
3454 goto out;
3455
Matan Barak2528e332015-06-11 16:35:25 +03003456 err = mlx5_ib_query_device(&dev->ib_dev, dprops, &uhw);
Eli Cohene126ba92013-07-07 17:25:49 +03003457 if (err) {
3458 mlx5_ib_warn(dev, "query_device failed %d\n", err);
3459 goto out;
3460 }
3461
Daniel Jurgens32f69e42018-01-04 17:25:36 +02003462 memset(pprops, 0, sizeof(*pprops));
3463 err = mlx5_ib_query_port(&dev->ib_dev, port, pprops);
3464 if (err) {
3465 mlx5_ib_warn(dev, "query_port %d failed %d\n",
3466 port, err);
3467 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03003468 }
3469
Daniel Jurgens32f69e42018-01-04 17:25:36 +02003470 dev->mdev->port_caps[port - 1].pkey_table_len =
3471 dprops->max_pkeys;
3472 dev->mdev->port_caps[port - 1].gid_table_len =
3473 pprops->gid_tbl_len;
3474 mlx5_ib_dbg(dev, "port %d: pkey_table_len %d, gid_table_len %d\n",
3475 port, dprops->max_pkeys, pprops->gid_tbl_len);
3476
Eli Cohene126ba92013-07-07 17:25:49 +03003477out:
3478 kfree(pprops);
3479 kfree(dprops);
3480
3481 return err;
3482}
3483
3484static void destroy_umrc_res(struct mlx5_ib_dev *dev)
3485{
3486 int err;
3487
3488 err = mlx5_mr_cache_cleanup(dev);
3489 if (err)
3490 mlx5_ib_warn(dev, "mr cache cleanup failed\n");
3491
3492 mlx5_ib_destroy_qp(dev->umrc.qp);
Christoph Hellwigadd08d72016-03-03 09:38:22 +01003493 ib_free_cq(dev->umrc.cq);
Eli Cohene126ba92013-07-07 17:25:49 +03003494 ib_dealloc_pd(dev->umrc.pd);
3495}
3496
3497enum {
3498 MAX_UMR_WR = 128,
3499};
3500
3501static int create_umr_res(struct mlx5_ib_dev *dev)
3502{
3503 struct ib_qp_init_attr *init_attr = NULL;
3504 struct ib_qp_attr *attr = NULL;
3505 struct ib_pd *pd;
3506 struct ib_cq *cq;
3507 struct ib_qp *qp;
Eli Cohene126ba92013-07-07 17:25:49 +03003508 int ret;
3509
3510 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
3511 init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL);
3512 if (!attr || !init_attr) {
3513 ret = -ENOMEM;
3514 goto error_0;
3515 }
3516
Christoph Hellwiged082d32016-09-05 12:56:17 +02003517 pd = ib_alloc_pd(&dev->ib_dev, 0);
Eli Cohene126ba92013-07-07 17:25:49 +03003518 if (IS_ERR(pd)) {
3519 mlx5_ib_dbg(dev, "Couldn't create PD for sync UMR QP\n");
3520 ret = PTR_ERR(pd);
3521 goto error_0;
3522 }
3523
Christoph Hellwigadd08d72016-03-03 09:38:22 +01003524 cq = ib_alloc_cq(&dev->ib_dev, NULL, 128, 0, IB_POLL_SOFTIRQ);
Eli Cohene126ba92013-07-07 17:25:49 +03003525 if (IS_ERR(cq)) {
3526 mlx5_ib_dbg(dev, "Couldn't create CQ for sync UMR QP\n");
3527 ret = PTR_ERR(cq);
3528 goto error_2;
3529 }
Eli Cohene126ba92013-07-07 17:25:49 +03003530
3531 init_attr->send_cq = cq;
3532 init_attr->recv_cq = cq;
3533 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
3534 init_attr->cap.max_send_wr = MAX_UMR_WR;
3535 init_attr->cap.max_send_sge = 1;
3536 init_attr->qp_type = MLX5_IB_QPT_REG_UMR;
3537 init_attr->port_num = 1;
3538 qp = mlx5_ib_create_qp(pd, init_attr, NULL);
3539 if (IS_ERR(qp)) {
3540 mlx5_ib_dbg(dev, "Couldn't create sync UMR QP\n");
3541 ret = PTR_ERR(qp);
3542 goto error_3;
3543 }
3544 qp->device = &dev->ib_dev;
3545 qp->real_qp = qp;
3546 qp->uobject = NULL;
3547 qp->qp_type = MLX5_IB_QPT_REG_UMR;
Majd Dibbiny31fde032017-10-30 14:23:13 +02003548 qp->send_cq = init_attr->send_cq;
3549 qp->recv_cq = init_attr->recv_cq;
Eli Cohene126ba92013-07-07 17:25:49 +03003550
3551 attr->qp_state = IB_QPS_INIT;
3552 attr->port_num = 1;
3553 ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_PKEY_INDEX |
3554 IB_QP_PORT, NULL);
3555 if (ret) {
3556 mlx5_ib_dbg(dev, "Couldn't modify UMR QP\n");
3557 goto error_4;
3558 }
3559
3560 memset(attr, 0, sizeof(*attr));
3561 attr->qp_state = IB_QPS_RTR;
3562 attr->path_mtu = IB_MTU_256;
3563
3564 ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
3565 if (ret) {
3566 mlx5_ib_dbg(dev, "Couldn't modify umr QP to rtr\n");
3567 goto error_4;
3568 }
3569
3570 memset(attr, 0, sizeof(*attr));
3571 attr->qp_state = IB_QPS_RTS;
3572 ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
3573 if (ret) {
3574 mlx5_ib_dbg(dev, "Couldn't modify umr QP to rts\n");
3575 goto error_4;
3576 }
3577
3578 dev->umrc.qp = qp;
3579 dev->umrc.cq = cq;
Eli Cohene126ba92013-07-07 17:25:49 +03003580 dev->umrc.pd = pd;
3581
3582 sema_init(&dev->umrc.sem, MAX_UMR_WR);
3583 ret = mlx5_mr_cache_init(dev);
3584 if (ret) {
3585 mlx5_ib_warn(dev, "mr cache init failed %d\n", ret);
3586 goto error_4;
3587 }
3588
3589 kfree(attr);
3590 kfree(init_attr);
3591
3592 return 0;
3593
3594error_4:
3595 mlx5_ib_destroy_qp(qp);
3596
3597error_3:
Christoph Hellwigadd08d72016-03-03 09:38:22 +01003598 ib_free_cq(cq);
Eli Cohene126ba92013-07-07 17:25:49 +03003599
3600error_2:
Eli Cohene126ba92013-07-07 17:25:49 +03003601 ib_dealloc_pd(pd);
3602
3603error_0:
3604 kfree(attr);
3605 kfree(init_attr);
3606 return ret;
3607}
3608
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03003609static u8 mlx5_get_umr_fence(u8 umr_fence_cap)
3610{
3611 switch (umr_fence_cap) {
3612 case MLX5_CAP_UMR_FENCE_NONE:
3613 return MLX5_FENCE_MODE_NONE;
3614 case MLX5_CAP_UMR_FENCE_SMALL:
3615 return MLX5_FENCE_MODE_INITIATOR_SMALL;
3616 default:
3617 return MLX5_FENCE_MODE_STRONG_ORDERING;
3618 }
3619}
3620
Eli Cohene126ba92013-07-07 17:25:49 +03003621static int create_dev_resources(struct mlx5_ib_resources *devr)
3622{
3623 struct ib_srq_init_attr attr;
3624 struct mlx5_ib_dev *dev;
Matan Barakbcf4c1e2015-06-11 16:35:20 +03003625 struct ib_cq_init_attr cq_attr = {.cqe = 1};
Haggai Eran7722f472016-02-29 15:45:07 +02003626 int port;
Eli Cohene126ba92013-07-07 17:25:49 +03003627 int ret = 0;
3628
3629 dev = container_of(devr, struct mlx5_ib_dev, devr);
3630
Haggai Erand16e91d2016-02-29 15:45:05 +02003631 mutex_init(&devr->mutex);
3632
Eli Cohene126ba92013-07-07 17:25:49 +03003633 devr->p0 = mlx5_ib_alloc_pd(&dev->ib_dev, NULL, NULL);
3634 if (IS_ERR(devr->p0)) {
3635 ret = PTR_ERR(devr->p0);
3636 goto error0;
3637 }
3638 devr->p0->device = &dev->ib_dev;
3639 devr->p0->uobject = NULL;
3640 atomic_set(&devr->p0->usecnt, 0);
3641
Matan Barakbcf4c1e2015-06-11 16:35:20 +03003642 devr->c0 = mlx5_ib_create_cq(&dev->ib_dev, &cq_attr, NULL, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03003643 if (IS_ERR(devr->c0)) {
3644 ret = PTR_ERR(devr->c0);
3645 goto error1;
3646 }
3647 devr->c0->device = &dev->ib_dev;
3648 devr->c0->uobject = NULL;
3649 devr->c0->comp_handler = NULL;
3650 devr->c0->event_handler = NULL;
3651 devr->c0->cq_context = NULL;
3652 atomic_set(&devr->c0->usecnt, 0);
3653
3654 devr->x0 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
3655 if (IS_ERR(devr->x0)) {
3656 ret = PTR_ERR(devr->x0);
3657 goto error2;
3658 }
3659 devr->x0->device = &dev->ib_dev;
3660 devr->x0->inode = NULL;
3661 atomic_set(&devr->x0->usecnt, 0);
3662 mutex_init(&devr->x0->tgt_qp_mutex);
3663 INIT_LIST_HEAD(&devr->x0->tgt_qp_list);
3664
3665 devr->x1 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
3666 if (IS_ERR(devr->x1)) {
3667 ret = PTR_ERR(devr->x1);
3668 goto error3;
3669 }
3670 devr->x1->device = &dev->ib_dev;
3671 devr->x1->inode = NULL;
3672 atomic_set(&devr->x1->usecnt, 0);
3673 mutex_init(&devr->x1->tgt_qp_mutex);
3674 INIT_LIST_HEAD(&devr->x1->tgt_qp_list);
3675
3676 memset(&attr, 0, sizeof(attr));
3677 attr.attr.max_sge = 1;
3678 attr.attr.max_wr = 1;
3679 attr.srq_type = IB_SRQT_XRC;
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03003680 attr.ext.cq = devr->c0;
Eli Cohene126ba92013-07-07 17:25:49 +03003681 attr.ext.xrc.xrcd = devr->x0;
3682
3683 devr->s0 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
3684 if (IS_ERR(devr->s0)) {
3685 ret = PTR_ERR(devr->s0);
3686 goto error4;
3687 }
3688 devr->s0->device = &dev->ib_dev;
3689 devr->s0->pd = devr->p0;
3690 devr->s0->uobject = NULL;
3691 devr->s0->event_handler = NULL;
3692 devr->s0->srq_context = NULL;
3693 devr->s0->srq_type = IB_SRQT_XRC;
3694 devr->s0->ext.xrc.xrcd = devr->x0;
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03003695 devr->s0->ext.cq = devr->c0;
Eli Cohene126ba92013-07-07 17:25:49 +03003696 atomic_inc(&devr->s0->ext.xrc.xrcd->usecnt);
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03003697 atomic_inc(&devr->s0->ext.cq->usecnt);
Eli Cohene126ba92013-07-07 17:25:49 +03003698 atomic_inc(&devr->p0->usecnt);
3699 atomic_set(&devr->s0->usecnt, 0);
3700
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03003701 memset(&attr, 0, sizeof(attr));
3702 attr.attr.max_sge = 1;
3703 attr.attr.max_wr = 1;
3704 attr.srq_type = IB_SRQT_BASIC;
3705 devr->s1 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
3706 if (IS_ERR(devr->s1)) {
3707 ret = PTR_ERR(devr->s1);
3708 goto error5;
3709 }
3710 devr->s1->device = &dev->ib_dev;
3711 devr->s1->pd = devr->p0;
3712 devr->s1->uobject = NULL;
3713 devr->s1->event_handler = NULL;
3714 devr->s1->srq_context = NULL;
3715 devr->s1->srq_type = IB_SRQT_BASIC;
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03003716 devr->s1->ext.cq = devr->c0;
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03003717 atomic_inc(&devr->p0->usecnt);
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03003718 atomic_set(&devr->s1->usecnt, 0);
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03003719
Haggai Eran7722f472016-02-29 15:45:07 +02003720 for (port = 0; port < ARRAY_SIZE(devr->ports); ++port) {
3721 INIT_WORK(&devr->ports[port].pkey_change_work,
3722 pkey_change_handler);
3723 devr->ports[port].devr = devr;
3724 }
3725
Eli Cohene126ba92013-07-07 17:25:49 +03003726 return 0;
3727
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03003728error5:
3729 mlx5_ib_destroy_srq(devr->s0);
Eli Cohene126ba92013-07-07 17:25:49 +03003730error4:
3731 mlx5_ib_dealloc_xrcd(devr->x1);
3732error3:
3733 mlx5_ib_dealloc_xrcd(devr->x0);
3734error2:
3735 mlx5_ib_destroy_cq(devr->c0);
3736error1:
3737 mlx5_ib_dealloc_pd(devr->p0);
3738error0:
3739 return ret;
3740}
3741
3742static void destroy_dev_resources(struct mlx5_ib_resources *devr)
3743{
Haggai Eran7722f472016-02-29 15:45:07 +02003744 struct mlx5_ib_dev *dev =
3745 container_of(devr, struct mlx5_ib_dev, devr);
3746 int port;
3747
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03003748 mlx5_ib_destroy_srq(devr->s1);
Eli Cohene126ba92013-07-07 17:25:49 +03003749 mlx5_ib_destroy_srq(devr->s0);
3750 mlx5_ib_dealloc_xrcd(devr->x0);
3751 mlx5_ib_dealloc_xrcd(devr->x1);
3752 mlx5_ib_destroy_cq(devr->c0);
3753 mlx5_ib_dealloc_pd(devr->p0);
Haggai Eran7722f472016-02-29 15:45:07 +02003754
3755 /* Make sure no change P_Key work items are still executing */
3756 for (port = 0; port < dev->num_ports; ++port)
3757 cancel_work_sync(&devr->ports[port].pkey_change_work);
Eli Cohene126ba92013-07-07 17:25:49 +03003758}
3759
Achiad Shochate53505a2015-12-23 18:47:25 +02003760static u32 get_core_cap_flags(struct ib_device *ibdev)
3761{
3762 struct mlx5_ib_dev *dev = to_mdev(ibdev);
3763 enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, 1);
3764 u8 l3_type_cap = MLX5_CAP_ROCE(dev->mdev, l3_type);
3765 u8 roce_version_cap = MLX5_CAP_ROCE(dev->mdev, roce_version);
Daniel Jurgens85c7c012018-01-04 17:25:43 +02003766 bool raw_support = !mlx5_core_mp_enabled(dev->mdev);
Achiad Shochate53505a2015-12-23 18:47:25 +02003767 u32 ret = 0;
3768
3769 if (ll == IB_LINK_LAYER_INFINIBAND)
3770 return RDMA_CORE_PORT_IBA_IB;
3771
Daniel Jurgens85c7c012018-01-04 17:25:43 +02003772 if (raw_support)
3773 ret = RDMA_CORE_PORT_RAW_PACKET;
Or Gerlitz72cd5712017-01-24 13:02:36 +02003774
Achiad Shochate53505a2015-12-23 18:47:25 +02003775 if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV4_CAP))
Or Gerlitz72cd5712017-01-24 13:02:36 +02003776 return ret;
Achiad Shochate53505a2015-12-23 18:47:25 +02003777
3778 if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV6_CAP))
Or Gerlitz72cd5712017-01-24 13:02:36 +02003779 return ret;
Achiad Shochate53505a2015-12-23 18:47:25 +02003780
3781 if (roce_version_cap & MLX5_ROCE_VERSION_1_CAP)
3782 ret |= RDMA_CORE_PORT_IBA_ROCE;
3783
3784 if (roce_version_cap & MLX5_ROCE_VERSION_2_CAP)
3785 ret |= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
3786
3787 return ret;
3788}
3789
Ira Weiny77386132015-05-13 20:02:58 -04003790static int mlx5_port_immutable(struct ib_device *ibdev, u8 port_num,
3791 struct ib_port_immutable *immutable)
3792{
3793 struct ib_port_attr attr;
Or Gerlitzca5b91d2016-11-27 16:51:36 +02003794 struct mlx5_ib_dev *dev = to_mdev(ibdev);
3795 enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, port_num);
Ira Weiny77386132015-05-13 20:02:58 -04003796 int err;
3797
Or Gerlitzc4550c62017-01-24 13:02:39 +02003798 immutable->core_cap_flags = get_core_cap_flags(ibdev);
3799
3800 err = ib_query_port(ibdev, port_num, &attr);
Ira Weiny77386132015-05-13 20:02:58 -04003801 if (err)
3802 return err;
3803
3804 immutable->pkey_tbl_len = attr.pkey_tbl_len;
3805 immutable->gid_tbl_len = attr.gid_tbl_len;
Achiad Shochate53505a2015-12-23 18:47:25 +02003806 immutable->core_cap_flags = get_core_cap_flags(ibdev);
Or Gerlitzca5b91d2016-11-27 16:51:36 +02003807 if ((ll == IB_LINK_LAYER_INFINIBAND) || MLX5_CAP_GEN(dev->mdev, roce))
3808 immutable->max_mad_size = IB_MGMT_MAD_SIZE;
Ira Weiny77386132015-05-13 20:02:58 -04003809
3810 return 0;
3811}
3812
Mark Bloch8e6efa32017-11-06 12:22:13 +00003813static int mlx5_port_rep_immutable(struct ib_device *ibdev, u8 port_num,
3814 struct ib_port_immutable *immutable)
3815{
3816 struct ib_port_attr attr;
3817 int err;
3818
3819 immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET;
3820
3821 err = ib_query_port(ibdev, port_num, &attr);
3822 if (err)
3823 return err;
3824
3825 immutable->pkey_tbl_len = attr.pkey_tbl_len;
3826 immutable->gid_tbl_len = attr.gid_tbl_len;
3827 immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET;
3828
3829 return 0;
3830}
3831
Leon Romanovsky9abb0d12017-06-27 16:49:53 +03003832static void get_dev_fw_str(struct ib_device *ibdev, char *str)
Ira Weinyc7342822016-06-15 02:22:01 -04003833{
3834 struct mlx5_ib_dev *dev =
3835 container_of(ibdev, struct mlx5_ib_dev, ib_dev);
Leon Romanovsky9abb0d12017-06-27 16:49:53 +03003836 snprintf(str, IB_FW_VERSION_NAME_MAX, "%d.%d.%04d",
3837 fw_rev_maj(dev->mdev), fw_rev_min(dev->mdev),
3838 fw_rev_sub(dev->mdev));
Ira Weinyc7342822016-06-15 02:22:01 -04003839}
3840
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003841static int mlx5_eth_lag_init(struct mlx5_ib_dev *dev)
Aviv Heller9ef9c642016-09-18 20:48:01 +03003842{
3843 struct mlx5_core_dev *mdev = dev->mdev;
3844 struct mlx5_flow_namespace *ns = mlx5_get_flow_namespace(mdev,
3845 MLX5_FLOW_NAMESPACE_LAG);
3846 struct mlx5_flow_table *ft;
3847 int err;
3848
3849 if (!ns || !mlx5_lag_is_active(mdev))
3850 return 0;
3851
3852 err = mlx5_cmd_create_vport_lag(mdev);
3853 if (err)
3854 return err;
3855
3856 ft = mlx5_create_lag_demux_flow_table(ns, 0, 0);
3857 if (IS_ERR(ft)) {
3858 err = PTR_ERR(ft);
3859 goto err_destroy_vport_lag;
3860 }
3861
Mark Bloch9a4ca382018-01-16 14:42:35 +00003862 dev->flow_db->lag_demux_ft = ft;
Aviv Heller9ef9c642016-09-18 20:48:01 +03003863 return 0;
3864
3865err_destroy_vport_lag:
3866 mlx5_cmd_destroy_vport_lag(mdev);
3867 return err;
3868}
3869
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003870static void mlx5_eth_lag_cleanup(struct mlx5_ib_dev *dev)
Aviv Heller9ef9c642016-09-18 20:48:01 +03003871{
3872 struct mlx5_core_dev *mdev = dev->mdev;
3873
Mark Bloch9a4ca382018-01-16 14:42:35 +00003874 if (dev->flow_db->lag_demux_ft) {
3875 mlx5_destroy_flow_table(dev->flow_db->lag_demux_ft);
3876 dev->flow_db->lag_demux_ft = NULL;
Aviv Heller9ef9c642016-09-18 20:48:01 +03003877
3878 mlx5_cmd_destroy_vport_lag(mdev);
3879 }
3880}
3881
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +02003882static int mlx5_add_netdev_notifier(struct mlx5_ib_dev *dev, u8 port_num)
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003883{
Achiad Shochate53505a2015-12-23 18:47:25 +02003884 int err;
3885
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +02003886 dev->roce[port_num].nb.notifier_call = mlx5_netdev_event;
3887 err = register_netdevice_notifier(&dev->roce[port_num].nb);
Aviv Heller5ec8c832016-09-18 20:48:00 +03003888 if (err) {
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +02003889 dev->roce[port_num].nb.notifier_call = NULL;
Achiad Shochate53505a2015-12-23 18:47:25 +02003890 return err;
Aviv Heller5ec8c832016-09-18 20:48:00 +03003891 }
Achiad Shochate53505a2015-12-23 18:47:25 +02003892
Or Gerlitzd012f5d2016-11-27 16:51:34 +02003893 return 0;
3894}
Achiad Shochate53505a2015-12-23 18:47:25 +02003895
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +02003896static void mlx5_remove_netdev_notifier(struct mlx5_ib_dev *dev, u8 port_num)
Eli Cohene126ba92013-07-07 17:25:49 +03003897{
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +02003898 if (dev->roce[port_num].nb.notifier_call) {
3899 unregister_netdevice_notifier(&dev->roce[port_num].nb);
3900 dev->roce[port_num].nb.notifier_call = NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03003901 }
3902}
3903
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +02003904static int mlx5_enable_eth(struct mlx5_ib_dev *dev, u8 port_num)
Eli Cohene126ba92013-07-07 17:25:49 +03003905{
Eli Cohene126ba92013-07-07 17:25:49 +03003906 int err;
3907
Or Gerlitzca5b91d2016-11-27 16:51:36 +02003908 if (MLX5_CAP_GEN(dev->mdev, roce)) {
3909 err = mlx5_nic_vport_enable_roce(dev->mdev);
3910 if (err)
Mark Bloch8e6efa32017-11-06 12:22:13 +00003911 return err;
Or Gerlitzca5b91d2016-11-27 16:51:36 +02003912 }
Achiad Shochate53505a2015-12-23 18:47:25 +02003913
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003914 err = mlx5_eth_lag_init(dev);
Aviv Heller9ef9c642016-09-18 20:48:01 +03003915 if (err)
3916 goto err_disable_roce;
3917
Achiad Shochate53505a2015-12-23 18:47:25 +02003918 return 0;
3919
Aviv Heller9ef9c642016-09-18 20:48:01 +03003920err_disable_roce:
Or Gerlitzca5b91d2016-11-27 16:51:36 +02003921 if (MLX5_CAP_GEN(dev->mdev, roce))
3922 mlx5_nic_vport_disable_roce(dev->mdev);
Aviv Heller9ef9c642016-09-18 20:48:01 +03003923
Achiad Shochate53505a2015-12-23 18:47:25 +02003924 return err;
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003925}
3926
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003927static void mlx5_disable_eth(struct mlx5_ib_dev *dev)
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003928{
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003929 mlx5_eth_lag_cleanup(dev);
Or Gerlitzca5b91d2016-11-27 16:51:36 +02003930 if (MLX5_CAP_GEN(dev->mdev, roce))
3931 mlx5_nic_vport_disable_roce(dev->mdev);
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003932}
3933
Parav Pandite1f24a72017-04-16 07:29:29 +03003934struct mlx5_ib_counter {
Kamal Heib7c16f472017-01-18 15:25:09 +02003935 const char *name;
3936 size_t offset;
3937};
3938
3939#define INIT_Q_COUNTER(_name) \
3940 { .name = #_name, .offset = MLX5_BYTE_OFF(query_q_counter_out, _name)}
3941
Parav Pandite1f24a72017-04-16 07:29:29 +03003942static const struct mlx5_ib_counter basic_q_cnts[] = {
Kamal Heib7c16f472017-01-18 15:25:09 +02003943 INIT_Q_COUNTER(rx_write_requests),
3944 INIT_Q_COUNTER(rx_read_requests),
3945 INIT_Q_COUNTER(rx_atomic_requests),
3946 INIT_Q_COUNTER(out_of_buffer),
3947};
3948
Parav Pandite1f24a72017-04-16 07:29:29 +03003949static const struct mlx5_ib_counter out_of_seq_q_cnts[] = {
Kamal Heib7c16f472017-01-18 15:25:09 +02003950 INIT_Q_COUNTER(out_of_sequence),
3951};
3952
Parav Pandite1f24a72017-04-16 07:29:29 +03003953static const struct mlx5_ib_counter retrans_q_cnts[] = {
Kamal Heib7c16f472017-01-18 15:25:09 +02003954 INIT_Q_COUNTER(duplicate_request),
3955 INIT_Q_COUNTER(rnr_nak_retry_err),
3956 INIT_Q_COUNTER(packet_seq_err),
3957 INIT_Q_COUNTER(implied_nak_seq_err),
3958 INIT_Q_COUNTER(local_ack_timeout_err),
3959};
3960
Parav Pandite1f24a72017-04-16 07:29:29 +03003961#define INIT_CONG_COUNTER(_name) \
3962 { .name = #_name, .offset = \
3963 MLX5_BYTE_OFF(query_cong_statistics_out, _name ## _high)}
3964
3965static const struct mlx5_ib_counter cong_cnts[] = {
3966 INIT_CONG_COUNTER(rp_cnp_ignored),
3967 INIT_CONG_COUNTER(rp_cnp_handled),
3968 INIT_CONG_COUNTER(np_ecn_marked_roce_packets),
3969 INIT_CONG_COUNTER(np_cnp_sent),
3970};
3971
Parav Pandit58dcb602017-06-19 07:19:37 +03003972static const struct mlx5_ib_counter extended_err_cnts[] = {
3973 INIT_Q_COUNTER(resp_local_length_error),
3974 INIT_Q_COUNTER(resp_cqe_error),
3975 INIT_Q_COUNTER(req_cqe_error),
3976 INIT_Q_COUNTER(req_remote_invalid_request),
3977 INIT_Q_COUNTER(req_remote_access_errors),
3978 INIT_Q_COUNTER(resp_remote_access_errors),
3979 INIT_Q_COUNTER(resp_cqe_flush_error),
3980 INIT_Q_COUNTER(req_cqe_flush_error),
3981};
3982
Parav Pandite1f24a72017-04-16 07:29:29 +03003983static void mlx5_ib_dealloc_counters(struct mlx5_ib_dev *dev)
Mark Bloch0837e862016-06-17 15:10:55 +03003984{
Daniel Jurgensaac44922018-01-04 17:25:40 +02003985 int i;
Mark Bloch0837e862016-06-17 15:10:55 +03003986
Kamal Heib7c16f472017-01-18 15:25:09 +02003987 for (i = 0; i < dev->num_ports; i++) {
Daniel Jurgensaac44922018-01-04 17:25:40 +02003988 if (dev->port[i].cnts.set_id)
3989 mlx5_core_dealloc_q_counter(dev->mdev,
3990 dev->port[i].cnts.set_id);
Parav Pandite1f24a72017-04-16 07:29:29 +03003991 kfree(dev->port[i].cnts.names);
3992 kfree(dev->port[i].cnts.offsets);
Kamal Heib7c16f472017-01-18 15:25:09 +02003993 }
3994}
3995
Parav Pandite1f24a72017-04-16 07:29:29 +03003996static int __mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev,
3997 struct mlx5_ib_counters *cnts)
Kamal Heib7c16f472017-01-18 15:25:09 +02003998{
3999 u32 num_counters;
4000
4001 num_counters = ARRAY_SIZE(basic_q_cnts);
4002
4003 if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt))
4004 num_counters += ARRAY_SIZE(out_of_seq_q_cnts);
4005
4006 if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters))
4007 num_counters += ARRAY_SIZE(retrans_q_cnts);
Parav Pandit58dcb602017-06-19 07:19:37 +03004008
4009 if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters))
4010 num_counters += ARRAY_SIZE(extended_err_cnts);
4011
Parav Pandite1f24a72017-04-16 07:29:29 +03004012 cnts->num_q_counters = num_counters;
Kamal Heib7c16f472017-01-18 15:25:09 +02004013
Parav Pandite1f24a72017-04-16 07:29:29 +03004014 if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
4015 cnts->num_cong_counters = ARRAY_SIZE(cong_cnts);
4016 num_counters += ARRAY_SIZE(cong_cnts);
4017 }
4018
4019 cnts->names = kcalloc(num_counters, sizeof(cnts->names), GFP_KERNEL);
4020 if (!cnts->names)
Kamal Heib7c16f472017-01-18 15:25:09 +02004021 return -ENOMEM;
4022
Parav Pandite1f24a72017-04-16 07:29:29 +03004023 cnts->offsets = kcalloc(num_counters,
4024 sizeof(cnts->offsets), GFP_KERNEL);
4025 if (!cnts->offsets)
Kamal Heib7c16f472017-01-18 15:25:09 +02004026 goto err_names;
4027
Kamal Heib7c16f472017-01-18 15:25:09 +02004028 return 0;
4029
4030err_names:
Parav Pandite1f24a72017-04-16 07:29:29 +03004031 kfree(cnts->names);
Daniel Jurgensaac44922018-01-04 17:25:40 +02004032 cnts->names = NULL;
Kamal Heib7c16f472017-01-18 15:25:09 +02004033 return -ENOMEM;
4034}
4035
Parav Pandite1f24a72017-04-16 07:29:29 +03004036static void mlx5_ib_fill_counters(struct mlx5_ib_dev *dev,
4037 const char **names,
4038 size_t *offsets)
Kamal Heib7c16f472017-01-18 15:25:09 +02004039{
4040 int i;
4041 int j = 0;
4042
4043 for (i = 0; i < ARRAY_SIZE(basic_q_cnts); i++, j++) {
4044 names[j] = basic_q_cnts[i].name;
4045 offsets[j] = basic_q_cnts[i].offset;
4046 }
4047
4048 if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt)) {
4049 for (i = 0; i < ARRAY_SIZE(out_of_seq_q_cnts); i++, j++) {
4050 names[j] = out_of_seq_q_cnts[i].name;
4051 offsets[j] = out_of_seq_q_cnts[i].offset;
4052 }
4053 }
4054
4055 if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters)) {
4056 for (i = 0; i < ARRAY_SIZE(retrans_q_cnts); i++, j++) {
4057 names[j] = retrans_q_cnts[i].name;
4058 offsets[j] = retrans_q_cnts[i].offset;
4059 }
4060 }
Parav Pandite1f24a72017-04-16 07:29:29 +03004061
Parav Pandit58dcb602017-06-19 07:19:37 +03004062 if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters)) {
4063 for (i = 0; i < ARRAY_SIZE(extended_err_cnts); i++, j++) {
4064 names[j] = extended_err_cnts[i].name;
4065 offsets[j] = extended_err_cnts[i].offset;
4066 }
4067 }
4068
Parav Pandite1f24a72017-04-16 07:29:29 +03004069 if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
4070 for (i = 0; i < ARRAY_SIZE(cong_cnts); i++, j++) {
4071 names[j] = cong_cnts[i].name;
4072 offsets[j] = cong_cnts[i].offset;
4073 }
4074 }
Mark Bloch0837e862016-06-17 15:10:55 +03004075}
4076
Parav Pandite1f24a72017-04-16 07:29:29 +03004077static int mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev)
Mark Bloch0837e862016-06-17 15:10:55 +03004078{
Daniel Jurgensaac44922018-01-04 17:25:40 +02004079 int err = 0;
Mark Bloch0837e862016-06-17 15:10:55 +03004080 int i;
Mark Bloch0837e862016-06-17 15:10:55 +03004081
4082 for (i = 0; i < dev->num_ports; i++) {
Daniel Jurgensaac44922018-01-04 17:25:40 +02004083 err = __mlx5_ib_alloc_counters(dev, &dev->port[i].cnts);
4084 if (err)
4085 goto err_alloc;
Kamal Heib7c16f472017-01-18 15:25:09 +02004086
Daniel Jurgensaac44922018-01-04 17:25:40 +02004087 mlx5_ib_fill_counters(dev, dev->port[i].cnts.names,
4088 dev->port[i].cnts.offsets);
4089
4090 err = mlx5_core_alloc_q_counter(dev->mdev,
4091 &dev->port[i].cnts.set_id);
4092 if (err) {
Mark Bloch0837e862016-06-17 15:10:55 +03004093 mlx5_ib_warn(dev,
4094 "couldn't allocate queue counter for port %d, err %d\n",
Daniel Jurgensaac44922018-01-04 17:25:40 +02004095 i + 1, err);
4096 goto err_alloc;
Mark Bloch0837e862016-06-17 15:10:55 +03004097 }
Daniel Jurgensaac44922018-01-04 17:25:40 +02004098 dev->port[i].cnts.set_id_valid = true;
Mark Bloch0837e862016-06-17 15:10:55 +03004099 }
4100
4101 return 0;
4102
Daniel Jurgensaac44922018-01-04 17:25:40 +02004103err_alloc:
4104 mlx5_ib_dealloc_counters(dev);
4105 return err;
Mark Bloch0837e862016-06-17 15:10:55 +03004106}
4107
Mark Bloch0ad17a82016-06-17 15:10:56 +03004108static struct rdma_hw_stats *mlx5_ib_alloc_hw_stats(struct ib_device *ibdev,
4109 u8 port_num)
4110{
Kamal Heib7c16f472017-01-18 15:25:09 +02004111 struct mlx5_ib_dev *dev = to_mdev(ibdev);
4112 struct mlx5_ib_port *port = &dev->port[port_num - 1];
Mark Bloch0ad17a82016-06-17 15:10:56 +03004113
4114 /* We support only per port stats */
4115 if (port_num == 0)
4116 return NULL;
4117
Parav Pandite1f24a72017-04-16 07:29:29 +03004118 return rdma_alloc_hw_stats_struct(port->cnts.names,
4119 port->cnts.num_q_counters +
4120 port->cnts.num_cong_counters,
Mark Bloch0ad17a82016-06-17 15:10:56 +03004121 RDMA_HW_STATS_DEFAULT_LIFESPAN);
4122}
4123
Daniel Jurgensaac44922018-01-04 17:25:40 +02004124static int mlx5_ib_query_q_counters(struct mlx5_core_dev *mdev,
Parav Pandite1f24a72017-04-16 07:29:29 +03004125 struct mlx5_ib_port *port,
4126 struct rdma_hw_stats *stats)
4127{
4128 int outlen = MLX5_ST_SZ_BYTES(query_q_counter_out);
4129 void *out;
4130 __be32 val;
4131 int ret, i;
4132
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03004133 out = kvzalloc(outlen, GFP_KERNEL);
Parav Pandite1f24a72017-04-16 07:29:29 +03004134 if (!out)
4135 return -ENOMEM;
4136
Daniel Jurgensaac44922018-01-04 17:25:40 +02004137 ret = mlx5_core_query_q_counter(mdev,
Parav Pandite1f24a72017-04-16 07:29:29 +03004138 port->cnts.set_id, 0,
4139 out, outlen);
4140 if (ret)
4141 goto free;
4142
4143 for (i = 0; i < port->cnts.num_q_counters; i++) {
4144 val = *(__be32 *)(out + port->cnts.offsets[i]);
4145 stats->value[i] = (u64)be32_to_cpu(val);
4146 }
4147
4148free:
4149 kvfree(out);
4150 return ret;
4151}
4152
Mark Bloch0ad17a82016-06-17 15:10:56 +03004153static int mlx5_ib_get_hw_stats(struct ib_device *ibdev,
4154 struct rdma_hw_stats *stats,
Kamal Heib7c16f472017-01-18 15:25:09 +02004155 u8 port_num, int index)
Mark Bloch0ad17a82016-06-17 15:10:56 +03004156{
4157 struct mlx5_ib_dev *dev = to_mdev(ibdev);
Kamal Heib7c16f472017-01-18 15:25:09 +02004158 struct mlx5_ib_port *port = &dev->port[port_num - 1];
Daniel Jurgensaac44922018-01-04 17:25:40 +02004159 struct mlx5_core_dev *mdev;
Parav Pandite1f24a72017-04-16 07:29:29 +03004160 int ret, num_counters;
Daniel Jurgensaac44922018-01-04 17:25:40 +02004161 u8 mdev_port_num;
Mark Bloch0ad17a82016-06-17 15:10:56 +03004162
Kamal Heib7c16f472017-01-18 15:25:09 +02004163 if (!stats)
Parav Pandite1f24a72017-04-16 07:29:29 +03004164 return -EINVAL;
Mark Bloch0ad17a82016-06-17 15:10:56 +03004165
Daniel Jurgensaac44922018-01-04 17:25:40 +02004166 num_counters = port->cnts.num_q_counters + port->cnts.num_cong_counters;
4167
4168 /* q_counters are per IB device, query the master mdev */
4169 ret = mlx5_ib_query_q_counters(dev->mdev, port, stats);
Mark Bloch0ad17a82016-06-17 15:10:56 +03004170 if (ret)
Parav Pandite1f24a72017-04-16 07:29:29 +03004171 return ret;
Mark Bloch0ad17a82016-06-17 15:10:56 +03004172
Parav Pandite1f24a72017-04-16 07:29:29 +03004173 if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
Daniel Jurgensaac44922018-01-04 17:25:40 +02004174 mdev = mlx5_ib_get_native_port_mdev(dev, port_num,
4175 &mdev_port_num);
4176 if (!mdev) {
4177 /* If port is not affiliated yet, its in down state
4178 * which doesn't have any counters yet, so it would be
4179 * zero. So no need to read from the HCA.
4180 */
4181 goto done;
4182 }
Majd Dibbiny71a0ff62017-12-21 17:38:26 +02004183 ret = mlx5_lag_query_cong_counters(dev->mdev,
4184 stats->value +
4185 port->cnts.num_q_counters,
4186 port->cnts.num_cong_counters,
4187 port->cnts.offsets +
4188 port->cnts.num_q_counters);
Daniel Jurgensaac44922018-01-04 17:25:40 +02004189
4190 mlx5_ib_put_native_port_mdev(dev, port_num);
Parav Pandite1f24a72017-04-16 07:29:29 +03004191 if (ret)
4192 return ret;
Mark Bloch0ad17a82016-06-17 15:10:56 +03004193 }
Kamal Heib7c16f472017-01-18 15:25:09 +02004194
Daniel Jurgensaac44922018-01-04 17:25:40 +02004195done:
Parav Pandite1f24a72017-04-16 07:29:29 +03004196 return num_counters;
Mark Bloch0ad17a82016-06-17 15:10:56 +03004197}
4198
Niranjana Vishwanathapura8e959602017-06-30 13:14:46 -07004199static void mlx5_ib_free_rdma_netdev(struct net_device *netdev)
4200{
4201 return mlx5_rdma_netdev_free(netdev);
4202}
4203
Erez Shitrit693dfd52017-04-27 17:01:34 +03004204static struct net_device*
4205mlx5_ib_alloc_rdma_netdev(struct ib_device *hca,
4206 u8 port_num,
4207 enum rdma_netdev_t type,
4208 const char *name,
4209 unsigned char name_assign_type,
4210 void (*setup)(struct net_device *))
4211{
Niranjana Vishwanathapura8e959602017-06-30 13:14:46 -07004212 struct net_device *netdev;
4213 struct rdma_netdev *rn;
4214
Erez Shitrit693dfd52017-04-27 17:01:34 +03004215 if (type != RDMA_NETDEV_IPOIB)
4216 return ERR_PTR(-EOPNOTSUPP);
4217
Niranjana Vishwanathapura8e959602017-06-30 13:14:46 -07004218 netdev = mlx5_rdma_netdev_alloc(to_mdev(hca)->mdev, hca,
4219 name, setup);
4220 if (likely(!IS_ERR_OR_NULL(netdev))) {
4221 rn = netdev_priv(netdev);
4222 rn->free_rdma_netdev = mlx5_ib_free_rdma_netdev;
4223 }
4224 return netdev;
Erez Shitrit693dfd52017-04-27 17:01:34 +03004225}
4226
Maor Gottliebfe248c32017-05-30 10:29:14 +03004227static void delay_drop_debugfs_cleanup(struct mlx5_ib_dev *dev)
4228{
4229 if (!dev->delay_drop.dbg)
4230 return;
4231 debugfs_remove_recursive(dev->delay_drop.dbg->dir_debugfs);
4232 kfree(dev->delay_drop.dbg);
4233 dev->delay_drop.dbg = NULL;
4234}
4235
Maor Gottlieb03404e82017-05-30 10:29:13 +03004236static void cancel_delay_drop(struct mlx5_ib_dev *dev)
4237{
4238 if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
4239 return;
4240
4241 cancel_work_sync(&dev->delay_drop.delay_drop_work);
Maor Gottliebfe248c32017-05-30 10:29:14 +03004242 delay_drop_debugfs_cleanup(dev);
4243}
4244
4245static ssize_t delay_drop_timeout_read(struct file *filp, char __user *buf,
4246 size_t count, loff_t *pos)
4247{
4248 struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
4249 char lbuf[20];
4250 int len;
4251
4252 len = snprintf(lbuf, sizeof(lbuf), "%u\n", delay_drop->timeout);
4253 return simple_read_from_buffer(buf, count, pos, lbuf, len);
4254}
4255
4256static ssize_t delay_drop_timeout_write(struct file *filp, const char __user *buf,
4257 size_t count, loff_t *pos)
4258{
4259 struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
4260 u32 timeout;
4261 u32 var;
4262
4263 if (kstrtouint_from_user(buf, count, 0, &var))
4264 return -EFAULT;
4265
4266 timeout = min_t(u32, roundup(var, 100), MLX5_MAX_DELAY_DROP_TIMEOUT_MS *
4267 1000);
4268 if (timeout != var)
4269 mlx5_ib_dbg(delay_drop->dev, "Round delay drop timeout to %u usec\n",
4270 timeout);
4271
4272 delay_drop->timeout = timeout;
4273
4274 return count;
4275}
4276
4277static const struct file_operations fops_delay_drop_timeout = {
4278 .owner = THIS_MODULE,
4279 .open = simple_open,
4280 .write = delay_drop_timeout_write,
4281 .read = delay_drop_timeout_read,
4282};
4283
4284static int delay_drop_debugfs_init(struct mlx5_ib_dev *dev)
4285{
4286 struct mlx5_ib_dbg_delay_drop *dbg;
4287
4288 if (!mlx5_debugfs_root)
4289 return 0;
4290
4291 dbg = kzalloc(sizeof(*dbg), GFP_KERNEL);
4292 if (!dbg)
4293 return -ENOMEM;
4294
Sudip Mukherjeecbafad82017-09-18 12:28:48 +01004295 dev->delay_drop.dbg = dbg;
4296
Maor Gottliebfe248c32017-05-30 10:29:14 +03004297 dbg->dir_debugfs =
4298 debugfs_create_dir("delay_drop",
4299 dev->mdev->priv.dbg_root);
4300 if (!dbg->dir_debugfs)
Sudip Mukherjeecbafad82017-09-18 12:28:48 +01004301 goto out_debugfs;
Maor Gottliebfe248c32017-05-30 10:29:14 +03004302
4303 dbg->events_cnt_debugfs =
4304 debugfs_create_atomic_t("num_timeout_events", 0400,
4305 dbg->dir_debugfs,
4306 &dev->delay_drop.events_cnt);
4307 if (!dbg->events_cnt_debugfs)
4308 goto out_debugfs;
4309
4310 dbg->rqs_cnt_debugfs =
4311 debugfs_create_atomic_t("num_rqs", 0400,
4312 dbg->dir_debugfs,
4313 &dev->delay_drop.rqs_cnt);
4314 if (!dbg->rqs_cnt_debugfs)
4315 goto out_debugfs;
4316
4317 dbg->timeout_debugfs =
4318 debugfs_create_file("timeout", 0600,
4319 dbg->dir_debugfs,
4320 &dev->delay_drop,
4321 &fops_delay_drop_timeout);
4322 if (!dbg->timeout_debugfs)
4323 goto out_debugfs;
4324
4325 return 0;
4326
4327out_debugfs:
4328 delay_drop_debugfs_cleanup(dev);
4329 return -ENOMEM;
Maor Gottlieb03404e82017-05-30 10:29:13 +03004330}
4331
4332static void init_delay_drop(struct mlx5_ib_dev *dev)
4333{
4334 if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
4335 return;
4336
4337 mutex_init(&dev->delay_drop.lock);
4338 dev->delay_drop.dev = dev;
4339 dev->delay_drop.activate = false;
4340 dev->delay_drop.timeout = MLX5_MAX_DELAY_DROP_TIMEOUT_MS * 1000;
4341 INIT_WORK(&dev->delay_drop.delay_drop_work, delay_drop_handler);
Maor Gottliebfe248c32017-05-30 10:29:14 +03004342 atomic_set(&dev->delay_drop.rqs_cnt, 0);
4343 atomic_set(&dev->delay_drop.events_cnt, 0);
4344
4345 if (delay_drop_debugfs_init(dev))
4346 mlx5_ib_warn(dev, "Failed to init delay drop debugfs\n");
Maor Gottlieb03404e82017-05-30 10:29:13 +03004347}
4348
Leon Romanovsky84305d712017-08-17 15:50:53 +03004349static const struct cpumask *
4350mlx5_ib_get_vector_affinity(struct ib_device *ibdev, int comp_vector)
Sagi Grimberg40b24402017-07-13 11:09:42 +03004351{
4352 struct mlx5_ib_dev *dev = to_mdev(ibdev);
4353
4354 return mlx5_get_vector_affinity(dev->mdev, comp_vector);
4355}
4356
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004357/* The mlx5_ib_multiport_mutex should be held when calling this function */
4358static void mlx5_ib_unbind_slave_port(struct mlx5_ib_dev *ibdev,
4359 struct mlx5_ib_multiport_info *mpi)
4360{
4361 u8 port_num = mlx5_core_native_port_num(mpi->mdev) - 1;
4362 struct mlx5_ib_port *port = &ibdev->port[port_num];
4363 int comps;
4364 int err;
4365 int i;
4366
Parav Pandita9e546e2018-01-04 17:25:39 +02004367 mlx5_ib_cleanup_cong_debugfs(ibdev, port_num);
4368
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004369 spin_lock(&port->mp.mpi_lock);
4370 if (!mpi->ibdev) {
4371 spin_unlock(&port->mp.mpi_lock);
4372 return;
4373 }
4374 mpi->ibdev = NULL;
4375
4376 spin_unlock(&port->mp.mpi_lock);
4377 mlx5_remove_netdev_notifier(ibdev, port_num);
4378 spin_lock(&port->mp.mpi_lock);
4379
4380 comps = mpi->mdev_refcnt;
4381 if (comps) {
4382 mpi->unaffiliate = true;
4383 init_completion(&mpi->unref_comp);
4384 spin_unlock(&port->mp.mpi_lock);
4385
4386 for (i = 0; i < comps; i++)
4387 wait_for_completion(&mpi->unref_comp);
4388
4389 spin_lock(&port->mp.mpi_lock);
4390 mpi->unaffiliate = false;
4391 }
4392
4393 port->mp.mpi = NULL;
4394
4395 list_add_tail(&mpi->list, &mlx5_ib_unaffiliated_port_list);
4396
4397 spin_unlock(&port->mp.mpi_lock);
4398
4399 err = mlx5_nic_vport_unaffiliate_multiport(mpi->mdev);
4400
4401 mlx5_ib_dbg(ibdev, "unaffiliated port %d\n", port_num + 1);
4402 /* Log an error, still needed to cleanup the pointers and add
4403 * it back to the list.
4404 */
4405 if (err)
4406 mlx5_ib_err(ibdev, "Failed to unaffiliate port %u\n",
4407 port_num + 1);
4408
4409 ibdev->roce[port_num].last_port_state = IB_PORT_DOWN;
4410}
4411
4412/* The mlx5_ib_multiport_mutex should be held when calling this function */
4413static bool mlx5_ib_bind_slave_port(struct mlx5_ib_dev *ibdev,
4414 struct mlx5_ib_multiport_info *mpi)
4415{
4416 u8 port_num = mlx5_core_native_port_num(mpi->mdev) - 1;
4417 int err;
4418
4419 spin_lock(&ibdev->port[port_num].mp.mpi_lock);
4420 if (ibdev->port[port_num].mp.mpi) {
4421 mlx5_ib_warn(ibdev, "port %d already affiliated.\n",
4422 port_num + 1);
4423 spin_unlock(&ibdev->port[port_num].mp.mpi_lock);
4424 return false;
4425 }
4426
4427 ibdev->port[port_num].mp.mpi = mpi;
4428 mpi->ibdev = ibdev;
4429 spin_unlock(&ibdev->port[port_num].mp.mpi_lock);
4430
4431 err = mlx5_nic_vport_affiliate_multiport(ibdev->mdev, mpi->mdev);
4432 if (err)
4433 goto unbind;
4434
4435 err = get_port_caps(ibdev, mlx5_core_native_port_num(mpi->mdev));
4436 if (err)
4437 goto unbind;
4438
4439 err = mlx5_add_netdev_notifier(ibdev, port_num);
4440 if (err) {
4441 mlx5_ib_err(ibdev, "failed adding netdev notifier for port %u\n",
4442 port_num + 1);
4443 goto unbind;
4444 }
4445
Parav Pandita9e546e2018-01-04 17:25:39 +02004446 err = mlx5_ib_init_cong_debugfs(ibdev, port_num);
4447 if (err)
4448 goto unbind;
4449
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004450 return true;
4451
4452unbind:
4453 mlx5_ib_unbind_slave_port(ibdev, mpi);
4454 return false;
4455}
4456
4457static int mlx5_ib_init_multiport_master(struct mlx5_ib_dev *dev)
4458{
4459 int port_num = mlx5_core_native_port_num(dev->mdev) - 1;
4460 enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev,
4461 port_num + 1);
4462 struct mlx5_ib_multiport_info *mpi;
4463 int err;
4464 int i;
4465
4466 if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
4467 return 0;
4468
4469 err = mlx5_query_nic_vport_system_image_guid(dev->mdev,
4470 &dev->sys_image_guid);
4471 if (err)
4472 return err;
4473
4474 err = mlx5_nic_vport_enable_roce(dev->mdev);
4475 if (err)
4476 return err;
4477
4478 mutex_lock(&mlx5_ib_multiport_mutex);
4479 for (i = 0; i < dev->num_ports; i++) {
4480 bool bound = false;
4481
4482 /* build a stub multiport info struct for the native port. */
4483 if (i == port_num) {
4484 mpi = kzalloc(sizeof(*mpi), GFP_KERNEL);
4485 if (!mpi) {
4486 mutex_unlock(&mlx5_ib_multiport_mutex);
4487 mlx5_nic_vport_disable_roce(dev->mdev);
4488 return -ENOMEM;
4489 }
4490
4491 mpi->is_master = true;
4492 mpi->mdev = dev->mdev;
4493 mpi->sys_image_guid = dev->sys_image_guid;
4494 dev->port[i].mp.mpi = mpi;
4495 mpi->ibdev = dev;
4496 mpi = NULL;
4497 continue;
4498 }
4499
4500 list_for_each_entry(mpi, &mlx5_ib_unaffiliated_port_list,
4501 list) {
4502 if (dev->sys_image_guid == mpi->sys_image_guid &&
4503 (mlx5_core_native_port_num(mpi->mdev) - 1) == i) {
4504 bound = mlx5_ib_bind_slave_port(dev, mpi);
4505 }
4506
4507 if (bound) {
4508 dev_dbg(&mpi->mdev->pdev->dev, "removing port from unaffiliated list.\n");
4509 mlx5_ib_dbg(dev, "port %d bound\n", i + 1);
4510 list_del(&mpi->list);
4511 break;
4512 }
4513 }
4514 if (!bound) {
4515 get_port_caps(dev, i + 1);
4516 mlx5_ib_dbg(dev, "no free port found for port %d\n",
4517 i + 1);
4518 }
4519 }
4520
4521 list_add_tail(&dev->ib_dev_list, &mlx5_ib_dev_list);
4522 mutex_unlock(&mlx5_ib_multiport_mutex);
4523 return err;
4524}
4525
4526static void mlx5_ib_cleanup_multiport_master(struct mlx5_ib_dev *dev)
4527{
4528 int port_num = mlx5_core_native_port_num(dev->mdev) - 1;
4529 enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev,
4530 port_num + 1);
4531 int i;
4532
4533 if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
4534 return;
4535
4536 mutex_lock(&mlx5_ib_multiport_mutex);
4537 for (i = 0; i < dev->num_ports; i++) {
4538 if (dev->port[i].mp.mpi) {
4539 /* Destroy the native port stub */
4540 if (i == port_num) {
4541 kfree(dev->port[i].mp.mpi);
4542 dev->port[i].mp.mpi = NULL;
4543 } else {
4544 mlx5_ib_dbg(dev, "unbinding port_num: %d\n", i + 1);
4545 mlx5_ib_unbind_slave_port(dev, dev->port[i].mp.mpi);
4546 }
4547 }
4548 }
4549
4550 mlx5_ib_dbg(dev, "removing from devlist\n");
4551 list_del(&dev->ib_dev_list);
4552 mutex_unlock(&mlx5_ib_multiport_mutex);
4553
4554 mlx5_nic_vport_disable_roce(dev->mdev);
4555}
4556
Mark Blochb5ca15a2018-01-23 11:16:30 +00004557void mlx5_ib_stage_init_cleanup(struct mlx5_ib_dev *dev)
Eli Cohene126ba92013-07-07 17:25:49 +03004558{
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004559 mlx5_ib_cleanup_multiport_master(dev);
Mark Bloch3cc297d2018-01-01 13:07:03 +02004560#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
4561 cleanup_srcu_struct(&dev->mr_srcu);
4562#endif
Mark Bloch16c19752018-01-01 13:06:58 +02004563 kfree(dev->port);
4564}
4565
Mark Blochb5ca15a2018-01-23 11:16:30 +00004566int mlx5_ib_stage_init_init(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02004567{
4568 struct mlx5_core_dev *mdev = dev->mdev;
Aviv Heller4babcf92016-09-18 20:48:03 +03004569 const char *name;
Eli Cohene126ba92013-07-07 17:25:49 +03004570 int err;
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004571 int i;
Eli Cohene126ba92013-07-07 17:25:49 +03004572
Daniel Jurgens508562d2018-01-04 17:25:34 +02004573 dev->port = kcalloc(dev->num_ports, sizeof(*dev->port),
Mark Bloch0837e862016-06-17 15:10:55 +03004574 GFP_KERNEL);
4575 if (!dev->port)
Mark Bloch16c19752018-01-01 13:06:58 +02004576 return -ENOMEM;
Mark Bloch0837e862016-06-17 15:10:55 +03004577
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004578 for (i = 0; i < dev->num_ports; i++) {
4579 spin_lock_init(&dev->port[i].mp.mpi_lock);
4580 rwlock_init(&dev->roce[i].netdev_lock);
4581 }
4582
4583 err = mlx5_ib_init_multiport_master(dev);
Eli Cohene126ba92013-07-07 17:25:49 +03004584 if (err)
Mark Bloch0837e862016-06-17 15:10:55 +03004585 goto err_free_port;
Eli Cohene126ba92013-07-07 17:25:49 +03004586
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004587 if (!mlx5_core_mp_enabled(mdev)) {
4588 int i;
4589
4590 for (i = 1; i <= dev->num_ports; i++) {
4591 err = get_port_caps(dev, i);
4592 if (err)
4593 break;
4594 }
4595 } else {
4596 err = get_port_caps(dev, mlx5_core_native_port_num(mdev));
4597 }
4598 if (err)
4599 goto err_mp;
4600
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03004601 if (mlx5_use_mad_ifc(dev))
4602 get_ext_port_caps(dev);
Eli Cohene126ba92013-07-07 17:25:49 +03004603
Aviv Heller4babcf92016-09-18 20:48:03 +03004604 if (!mlx5_lag_is_active(mdev))
4605 name = "mlx5_%d";
4606 else
4607 name = "mlx5_bond_%d";
4608
4609 strlcpy(dev->ib_dev.name, name, IB_DEVICE_NAME_MAX);
Eli Cohene126ba92013-07-07 17:25:49 +03004610 dev->ib_dev.owner = THIS_MODULE;
4611 dev->ib_dev.node_type = RDMA_NODE_IB_CA;
Sagi Grimbergc6790aa2015-09-24 10:34:23 +03004612 dev->ib_dev.local_dma_lkey = 0 /* not supported for now */;
Daniel Jurgens508562d2018-01-04 17:25:34 +02004613 dev->ib_dev.phys_port_cnt = dev->num_ports;
Saeed Mahameed233d05d2015-04-02 17:07:32 +03004614 dev->ib_dev.num_comp_vectors =
4615 dev->mdev->priv.eq_table.num_comp_vectors;
Bart Van Assche9b0c2892017-01-20 13:04:21 -08004616 dev->ib_dev.dev.parent = &mdev->pdev->dev;
Eli Cohene126ba92013-07-07 17:25:49 +03004617
Mark Bloch3cc297d2018-01-01 13:07:03 +02004618 mutex_init(&dev->cap_mask_mutex);
4619 INIT_LIST_HEAD(&dev->qp_list);
4620 spin_lock_init(&dev->reset_flow_resource_lock);
4621
4622#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
4623 err = init_srcu_struct(&dev->mr_srcu);
4624 if (err)
4625 goto err_free_port;
4626#endif
4627
Mark Bloch16c19752018-01-01 13:06:58 +02004628 return 0;
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004629err_mp:
4630 mlx5_ib_cleanup_multiport_master(dev);
Mark Bloch16c19752018-01-01 13:06:58 +02004631
4632err_free_port:
4633 kfree(dev->port);
4634
4635 return -ENOMEM;
4636}
4637
Mark Bloch9a4ca382018-01-16 14:42:35 +00004638static int mlx5_ib_stage_flow_db_init(struct mlx5_ib_dev *dev)
4639{
4640 dev->flow_db = kzalloc(sizeof(*dev->flow_db), GFP_KERNEL);
4641
4642 if (!dev->flow_db)
4643 return -ENOMEM;
4644
4645 mutex_init(&dev->flow_db->lock);
4646
4647 return 0;
4648}
4649
Mark Blochb5ca15a2018-01-23 11:16:30 +00004650int mlx5_ib_stage_rep_flow_db_init(struct mlx5_ib_dev *dev)
4651{
4652 struct mlx5_ib_dev *nic_dev;
4653
4654 nic_dev = mlx5_ib_get_uplink_ibdev(dev->mdev->priv.eswitch);
4655
4656 if (!nic_dev)
4657 return -EINVAL;
4658
4659 dev->flow_db = nic_dev->flow_db;
4660
4661 return 0;
4662}
4663
Mark Bloch9a4ca382018-01-16 14:42:35 +00004664static void mlx5_ib_stage_flow_db_cleanup(struct mlx5_ib_dev *dev)
4665{
4666 kfree(dev->flow_db);
4667}
4668
Mark Blochb5ca15a2018-01-23 11:16:30 +00004669int mlx5_ib_stage_caps_init(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02004670{
4671 struct mlx5_core_dev *mdev = dev->mdev;
Mark Bloch16c19752018-01-01 13:06:58 +02004672 int err;
4673
Eli Cohene126ba92013-07-07 17:25:49 +03004674 dev->ib_dev.uverbs_abi_ver = MLX5_IB_UVERBS_ABI_VERSION;
4675 dev->ib_dev.uverbs_cmd_mask =
4676 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
4677 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
4678 (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
4679 (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
4680 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
Moni Shoua41c450f2016-11-23 08:23:26 +02004681 (1ull << IB_USER_VERBS_CMD_CREATE_AH) |
4682 (1ull << IB_USER_VERBS_CMD_DESTROY_AH) |
Eli Cohene126ba92013-07-07 17:25:49 +03004683 (1ull << IB_USER_VERBS_CMD_REG_MR) |
Noa Osherovich56e11d62016-02-29 16:46:51 +02004684 (1ull << IB_USER_VERBS_CMD_REREG_MR) |
Eli Cohene126ba92013-07-07 17:25:49 +03004685 (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
4686 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
4687 (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
4688 (1ull << IB_USER_VERBS_CMD_RESIZE_CQ) |
4689 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
4690 (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
4691 (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
4692 (1ull << IB_USER_VERBS_CMD_QUERY_QP) |
4693 (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
4694 (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST) |
4695 (1ull << IB_USER_VERBS_CMD_DETACH_MCAST) |
4696 (1ull << IB_USER_VERBS_CMD_CREATE_SRQ) |
4697 (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ) |
4698 (1ull << IB_USER_VERBS_CMD_QUERY_SRQ) |
4699 (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ) |
4700 (1ull << IB_USER_VERBS_CMD_CREATE_XSRQ) |
4701 (1ull << IB_USER_VERBS_CMD_OPEN_QP);
Haggai Eran1707cb42015-02-08 13:28:52 +02004702 dev->ib_dev.uverbs_ex_cmd_mask =
Matan Barakd4584dd2016-01-28 17:51:46 +02004703 (1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE) |
4704 (1ull << IB_USER_VERBS_EX_CMD_CREATE_CQ) |
Bodong Wang7d29f342016-12-01 13:43:16 +02004705 (1ull << IB_USER_VERBS_EX_CMD_CREATE_QP) |
Yonatan Cohenb0e9df62017-11-13 10:51:15 +02004706 (1ull << IB_USER_VERBS_EX_CMD_MODIFY_QP) |
4707 (1ull << IB_USER_VERBS_EX_CMD_MODIFY_CQ);
Eli Cohene126ba92013-07-07 17:25:49 +03004708
4709 dev->ib_dev.query_device = mlx5_ib_query_device;
Achiad Shochatebd61f62015-12-23 18:47:16 +02004710 dev->ib_dev.get_link_layer = mlx5_ib_port_link_layer;
Eli Cohene126ba92013-07-07 17:25:49 +03004711 dev->ib_dev.query_gid = mlx5_ib_query_gid;
Achiad Shochat3cca2602015-12-23 18:47:23 +02004712 dev->ib_dev.add_gid = mlx5_ib_add_gid;
4713 dev->ib_dev.del_gid = mlx5_ib_del_gid;
Eli Cohene126ba92013-07-07 17:25:49 +03004714 dev->ib_dev.query_pkey = mlx5_ib_query_pkey;
4715 dev->ib_dev.modify_device = mlx5_ib_modify_device;
4716 dev->ib_dev.modify_port = mlx5_ib_modify_port;
4717 dev->ib_dev.alloc_ucontext = mlx5_ib_alloc_ucontext;
4718 dev->ib_dev.dealloc_ucontext = mlx5_ib_dealloc_ucontext;
4719 dev->ib_dev.mmap = mlx5_ib_mmap;
4720 dev->ib_dev.alloc_pd = mlx5_ib_alloc_pd;
4721 dev->ib_dev.dealloc_pd = mlx5_ib_dealloc_pd;
4722 dev->ib_dev.create_ah = mlx5_ib_create_ah;
4723 dev->ib_dev.query_ah = mlx5_ib_query_ah;
4724 dev->ib_dev.destroy_ah = mlx5_ib_destroy_ah;
4725 dev->ib_dev.create_srq = mlx5_ib_create_srq;
4726 dev->ib_dev.modify_srq = mlx5_ib_modify_srq;
4727 dev->ib_dev.query_srq = mlx5_ib_query_srq;
4728 dev->ib_dev.destroy_srq = mlx5_ib_destroy_srq;
4729 dev->ib_dev.post_srq_recv = mlx5_ib_post_srq_recv;
4730 dev->ib_dev.create_qp = mlx5_ib_create_qp;
4731 dev->ib_dev.modify_qp = mlx5_ib_modify_qp;
4732 dev->ib_dev.query_qp = mlx5_ib_query_qp;
4733 dev->ib_dev.destroy_qp = mlx5_ib_destroy_qp;
4734 dev->ib_dev.post_send = mlx5_ib_post_send;
4735 dev->ib_dev.post_recv = mlx5_ib_post_recv;
4736 dev->ib_dev.create_cq = mlx5_ib_create_cq;
4737 dev->ib_dev.modify_cq = mlx5_ib_modify_cq;
4738 dev->ib_dev.resize_cq = mlx5_ib_resize_cq;
4739 dev->ib_dev.destroy_cq = mlx5_ib_destroy_cq;
4740 dev->ib_dev.poll_cq = mlx5_ib_poll_cq;
4741 dev->ib_dev.req_notify_cq = mlx5_ib_arm_cq;
4742 dev->ib_dev.get_dma_mr = mlx5_ib_get_dma_mr;
4743 dev->ib_dev.reg_user_mr = mlx5_ib_reg_user_mr;
Noa Osherovich56e11d62016-02-29 16:46:51 +02004744 dev->ib_dev.rereg_user_mr = mlx5_ib_rereg_user_mr;
Eli Cohene126ba92013-07-07 17:25:49 +03004745 dev->ib_dev.dereg_mr = mlx5_ib_dereg_mr;
4746 dev->ib_dev.attach_mcast = mlx5_ib_mcg_attach;
4747 dev->ib_dev.detach_mcast = mlx5_ib_mcg_detach;
4748 dev->ib_dev.process_mad = mlx5_ib_process_mad;
Sagi Grimberg9bee1782015-07-30 10:32:35 +03004749 dev->ib_dev.alloc_mr = mlx5_ib_alloc_mr;
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004750 dev->ib_dev.map_mr_sg = mlx5_ib_map_mr_sg;
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02004751 dev->ib_dev.check_mr_status = mlx5_ib_check_mr_status;
Ira Weinyc7342822016-06-15 02:22:01 -04004752 dev->ib_dev.get_dev_fw_str = get_dev_fw_str;
Sagi Grimberg40b24402017-07-13 11:09:42 +03004753 dev->ib_dev.get_vector_affinity = mlx5_ib_get_vector_affinity;
Niranjana Vishwanathapura8e959602017-06-30 13:14:46 -07004754 if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads))
Alex Vesker022d0382017-06-14 09:59:06 +03004755 dev->ib_dev.alloc_rdma_netdev = mlx5_ib_alloc_rdma_netdev;
Niranjana Vishwanathapura8e959602017-06-30 13:14:46 -07004756
Eli Coheneff901d2016-03-11 22:58:42 +02004757 if (mlx5_core_is_pf(mdev)) {
4758 dev->ib_dev.get_vf_config = mlx5_ib_get_vf_config;
4759 dev->ib_dev.set_vf_link_state = mlx5_ib_set_vf_link_state;
4760 dev->ib_dev.get_vf_stats = mlx5_ib_get_vf_stats;
4761 dev->ib_dev.set_vf_guid = mlx5_ib_set_vf_guid;
4762 }
Eli Cohene126ba92013-07-07 17:25:49 +03004763
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03004764 dev->ib_dev.disassociate_ucontext = mlx5_ib_disassociate_ucontext;
4765
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03004766 dev->umr_fence = mlx5_get_umr_fence(MLX5_CAP_GEN(mdev, umr_fence));
4767
Matan Barakd2370e02016-02-29 18:05:30 +02004768 if (MLX5_CAP_GEN(mdev, imaicl)) {
4769 dev->ib_dev.alloc_mw = mlx5_ib_alloc_mw;
4770 dev->ib_dev.dealloc_mw = mlx5_ib_dealloc_mw;
4771 dev->ib_dev.uverbs_cmd_mask |=
4772 (1ull << IB_USER_VERBS_CMD_ALLOC_MW) |
4773 (1ull << IB_USER_VERBS_CMD_DEALLOC_MW);
4774 }
4775
Saeed Mahameed938fe832015-05-28 22:28:41 +03004776 if (MLX5_CAP_GEN(mdev, xrc)) {
Eli Cohene126ba92013-07-07 17:25:49 +03004777 dev->ib_dev.alloc_xrcd = mlx5_ib_alloc_xrcd;
4778 dev->ib_dev.dealloc_xrcd = mlx5_ib_dealloc_xrcd;
4779 dev->ib_dev.uverbs_cmd_mask |=
4780 (1ull << IB_USER_VERBS_CMD_OPEN_XRCD) |
4781 (1ull << IB_USER_VERBS_CMD_CLOSE_XRCD);
4782 }
4783
Yishai Hadas81e30882017-06-08 16:15:09 +03004784 dev->ib_dev.create_flow = mlx5_ib_create_flow;
4785 dev->ib_dev.destroy_flow = mlx5_ib_destroy_flow;
4786 dev->ib_dev.uverbs_ex_cmd_mask |=
4787 (1ull << IB_USER_VERBS_EX_CMD_CREATE_FLOW) |
4788 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_FLOW);
4789
Eli Cohene126ba92013-07-07 17:25:49 +03004790 err = init_node_data(dev);
4791 if (err)
Mark Bloch16c19752018-01-01 13:06:58 +02004792 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03004793
Mark Blochc8b89922018-01-01 13:07:02 +02004794 if ((MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_ETH) &&
Jason Gunthorpee7996a92018-01-29 13:26:40 -07004795 (MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) ||
4796 MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
Mark Blochc8b89922018-01-01 13:07:02 +02004797 mutex_init(&dev->lb_mutex);
4798
Mark Bloch16c19752018-01-01 13:06:58 +02004799 return 0;
4800}
4801
Mark Bloch8e6efa32017-11-06 12:22:13 +00004802static int mlx5_ib_stage_non_default_cb(struct mlx5_ib_dev *dev)
4803{
4804 dev->ib_dev.get_port_immutable = mlx5_port_immutable;
4805 dev->ib_dev.query_port = mlx5_ib_query_port;
4806
4807 return 0;
4808}
4809
Mark Blochb5ca15a2018-01-23 11:16:30 +00004810int mlx5_ib_stage_rep_non_default_cb(struct mlx5_ib_dev *dev)
Mark Bloch8e6efa32017-11-06 12:22:13 +00004811{
4812 dev->ib_dev.get_port_immutable = mlx5_port_rep_immutable;
4813 dev->ib_dev.query_port = mlx5_ib_rep_query_port;
4814
4815 return 0;
4816}
4817
4818static int mlx5_ib_stage_common_roce_init(struct mlx5_ib_dev *dev,
4819 u8 port_num)
4820{
4821 int i;
4822
4823 for (i = 0; i < dev->num_ports; i++) {
4824 dev->roce[i].dev = dev;
4825 dev->roce[i].native_port_num = i + 1;
4826 dev->roce[i].last_port_state = IB_PORT_DOWN;
4827 }
4828
4829 dev->ib_dev.get_netdev = mlx5_ib_get_netdev;
4830 dev->ib_dev.create_wq = mlx5_ib_create_wq;
4831 dev->ib_dev.modify_wq = mlx5_ib_modify_wq;
4832 dev->ib_dev.destroy_wq = mlx5_ib_destroy_wq;
4833 dev->ib_dev.create_rwq_ind_table = mlx5_ib_create_rwq_ind_table;
4834 dev->ib_dev.destroy_rwq_ind_table = mlx5_ib_destroy_rwq_ind_table;
4835
4836 dev->ib_dev.uverbs_ex_cmd_mask |=
4837 (1ull << IB_USER_VERBS_EX_CMD_CREATE_WQ) |
4838 (1ull << IB_USER_VERBS_EX_CMD_MODIFY_WQ) |
4839 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_WQ) |
4840 (1ull << IB_USER_VERBS_EX_CMD_CREATE_RWQ_IND_TBL) |
4841 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_RWQ_IND_TBL);
4842
4843 return mlx5_add_netdev_notifier(dev, port_num);
4844}
4845
4846static void mlx5_ib_stage_common_roce_cleanup(struct mlx5_ib_dev *dev)
4847{
4848 u8 port_num = mlx5_core_native_port_num(dev->mdev) - 1;
4849
4850 mlx5_remove_netdev_notifier(dev, port_num);
4851}
4852
4853int mlx5_ib_stage_rep_roce_init(struct mlx5_ib_dev *dev)
4854{
4855 struct mlx5_core_dev *mdev = dev->mdev;
4856 enum rdma_link_layer ll;
4857 int port_type_cap;
4858 int err = 0;
4859 u8 port_num;
4860
4861 port_num = mlx5_core_native_port_num(dev->mdev) - 1;
4862 port_type_cap = MLX5_CAP_GEN(mdev, port_type);
4863 ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
4864
4865 if (ll == IB_LINK_LAYER_ETHERNET)
4866 err = mlx5_ib_stage_common_roce_init(dev, port_num);
4867
4868 return err;
4869}
4870
4871void mlx5_ib_stage_rep_roce_cleanup(struct mlx5_ib_dev *dev)
4872{
4873 mlx5_ib_stage_common_roce_cleanup(dev);
4874}
4875
Mark Bloch16c19752018-01-01 13:06:58 +02004876static int mlx5_ib_stage_roce_init(struct mlx5_ib_dev *dev)
4877{
4878 struct mlx5_core_dev *mdev = dev->mdev;
4879 enum rdma_link_layer ll;
4880 int port_type_cap;
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004881 u8 port_num;
Mark Bloch16c19752018-01-01 13:06:58 +02004882 int err;
4883
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004884 port_num = mlx5_core_native_port_num(dev->mdev) - 1;
Mark Bloch16c19752018-01-01 13:06:58 +02004885 port_type_cap = MLX5_CAP_GEN(mdev, port_type);
4886 ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
4887
Achiad Shochatfc24fc52015-12-23 18:47:17 +02004888 if (ll == IB_LINK_LAYER_ETHERNET) {
Mark Bloch8e6efa32017-11-06 12:22:13 +00004889 err = mlx5_ib_stage_common_roce_init(dev, port_num);
Achiad Shochatfc24fc52015-12-23 18:47:17 +02004890 if (err)
Mark Bloch16c19752018-01-01 13:06:58 +02004891 return err;
Mark Bloch8e6efa32017-11-06 12:22:13 +00004892
4893 err = mlx5_enable_eth(dev, port_num);
4894 if (err)
4895 goto cleanup;
Achiad Shochatfc24fc52015-12-23 18:47:17 +02004896 }
4897
Mark Bloch16c19752018-01-01 13:06:58 +02004898 return 0;
Mark Bloch8e6efa32017-11-06 12:22:13 +00004899cleanup:
4900 mlx5_ib_stage_common_roce_cleanup(dev);
4901
4902 return err;
Mark Bloch16c19752018-01-01 13:06:58 +02004903}
Eli Cohene126ba92013-07-07 17:25:49 +03004904
Mark Bloch16c19752018-01-01 13:06:58 +02004905static void mlx5_ib_stage_roce_cleanup(struct mlx5_ib_dev *dev)
4906{
4907 struct mlx5_core_dev *mdev = dev->mdev;
4908 enum rdma_link_layer ll;
4909 int port_type_cap;
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004910 u8 port_num;
Eli Cohene126ba92013-07-07 17:25:49 +03004911
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004912 port_num = mlx5_core_native_port_num(dev->mdev) - 1;
Mark Bloch16c19752018-01-01 13:06:58 +02004913 port_type_cap = MLX5_CAP_GEN(mdev, port_type);
4914 ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
4915
4916 if (ll == IB_LINK_LAYER_ETHERNET) {
4917 mlx5_disable_eth(dev);
Mark Bloch8e6efa32017-11-06 12:22:13 +00004918 mlx5_ib_stage_common_roce_cleanup(dev);
Kamal Heib45bded22017-01-18 14:10:32 +02004919 }
Mark Bloch16c19752018-01-01 13:06:58 +02004920}
Haggai Eran6aec21f2014-12-11 17:04:23 +02004921
Mark Blochb5ca15a2018-01-23 11:16:30 +00004922int mlx5_ib_stage_dev_res_init(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02004923{
4924 return create_dev_resources(&dev->devr);
4925}
Parav Pandit4a2da0b2017-05-30 10:05:15 +03004926
Mark Blochb5ca15a2018-01-23 11:16:30 +00004927void mlx5_ib_stage_dev_res_cleanup(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02004928{
4929 destroy_dev_resources(&dev->devr);
4930}
4931
4932static int mlx5_ib_stage_odp_init(struct mlx5_ib_dev *dev)
4933{
Mark Bloch07321b32018-01-01 13:07:00 +02004934 mlx5_ib_internal_fill_odp_caps(dev);
4935
Mark Bloch16c19752018-01-01 13:06:58 +02004936 return mlx5_ib_odp_init_one(dev);
4937}
4938
Mark Blochb5ca15a2018-01-23 11:16:30 +00004939int mlx5_ib_stage_counters_init(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02004940{
Mark Bloch5e1e7612018-01-01 13:07:01 +02004941 if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt)) {
4942 dev->ib_dev.get_hw_stats = mlx5_ib_get_hw_stats;
4943 dev->ib_dev.alloc_hw_stats = mlx5_ib_alloc_hw_stats;
4944
4945 return mlx5_ib_alloc_counters(dev);
4946 }
Mark Bloch16c19752018-01-01 13:06:58 +02004947
4948 return 0;
4949}
4950
Mark Blochb5ca15a2018-01-23 11:16:30 +00004951void mlx5_ib_stage_counters_cleanup(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02004952{
4953 if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt))
4954 mlx5_ib_dealloc_counters(dev);
4955}
4956
4957static int mlx5_ib_stage_cong_debugfs_init(struct mlx5_ib_dev *dev)
4958{
Parav Pandita9e546e2018-01-04 17:25:39 +02004959 return mlx5_ib_init_cong_debugfs(dev,
4960 mlx5_core_native_port_num(dev->mdev) - 1);
Mark Bloch16c19752018-01-01 13:06:58 +02004961}
4962
4963static void mlx5_ib_stage_cong_debugfs_cleanup(struct mlx5_ib_dev *dev)
4964{
Parav Pandita9e546e2018-01-04 17:25:39 +02004965 mlx5_ib_cleanup_cong_debugfs(dev,
4966 mlx5_core_native_port_num(dev->mdev) - 1);
Mark Bloch16c19752018-01-01 13:06:58 +02004967}
4968
4969static int mlx5_ib_stage_uar_init(struct mlx5_ib_dev *dev)
4970{
Eli Cohen5fe9dec2017-01-03 23:55:25 +02004971 dev->mdev->priv.uar = mlx5_get_uars_page(dev->mdev);
4972 if (!dev->mdev->priv.uar)
Mark Bloch16c19752018-01-01 13:06:58 +02004973 return -ENOMEM;
4974 return 0;
4975}
4976
4977static void mlx5_ib_stage_uar_cleanup(struct mlx5_ib_dev *dev)
4978{
4979 mlx5_put_uars_page(dev->mdev, dev->mdev->priv.uar);
4980}
4981
Mark Blochb5ca15a2018-01-23 11:16:30 +00004982int mlx5_ib_stage_bfrag_init(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02004983{
4984 int err;
Eli Cohen5fe9dec2017-01-03 23:55:25 +02004985
4986 err = mlx5_alloc_bfreg(dev->mdev, &dev->bfreg, false, false);
4987 if (err)
Mark Bloch16c19752018-01-01 13:06:58 +02004988 return err;
Eli Cohen5fe9dec2017-01-03 23:55:25 +02004989
4990 err = mlx5_alloc_bfreg(dev->mdev, &dev->fp_bfreg, false, true);
4991 if (err)
Mark Bloch16c19752018-01-01 13:06:58 +02004992 mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
Eli Cohen5fe9dec2017-01-03 23:55:25 +02004993
Mark Bloch16c19752018-01-01 13:06:58 +02004994 return err;
4995}
Mark Bloch0837e862016-06-17 15:10:55 +03004996
Mark Blochb5ca15a2018-01-23 11:16:30 +00004997void mlx5_ib_stage_bfrag_cleanup(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02004998{
4999 mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
5000 mlx5_free_bfreg(dev->mdev, &dev->bfreg);
5001}
Eli Cohene126ba92013-07-07 17:25:49 +03005002
Mark Blochb5ca15a2018-01-23 11:16:30 +00005003int mlx5_ib_stage_ib_reg_init(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02005004{
5005 return ib_register_device(&dev->ib_dev, NULL);
5006}
5007
Mark Blochb5ca15a2018-01-23 11:16:30 +00005008void mlx5_ib_stage_ib_reg_cleanup(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02005009{
5010 ib_unregister_device(&dev->ib_dev);
5011}
5012
Mark Blochb5ca15a2018-01-23 11:16:30 +00005013int mlx5_ib_stage_umr_res_init(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02005014{
5015 return create_umr_res(dev);
5016}
5017
Mark Blochb5ca15a2018-01-23 11:16:30 +00005018void mlx5_ib_stage_umr_res_cleanup(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02005019{
5020 destroy_umrc_res(dev);
5021}
5022
5023static int mlx5_ib_stage_delay_drop_init(struct mlx5_ib_dev *dev)
5024{
Maor Gottlieb03404e82017-05-30 10:29:13 +03005025 init_delay_drop(dev);
5026
Mark Bloch16c19752018-01-01 13:06:58 +02005027 return 0;
5028}
5029
5030static void mlx5_ib_stage_delay_drop_cleanup(struct mlx5_ib_dev *dev)
5031{
5032 cancel_delay_drop(dev);
5033}
5034
Mark Blochb5ca15a2018-01-23 11:16:30 +00005035int mlx5_ib_stage_class_attr_init(struct mlx5_ib_dev *dev)
Mark Bloch16c19752018-01-01 13:06:58 +02005036{
5037 int err;
5038 int i;
5039
Eli Cohene126ba92013-07-07 17:25:49 +03005040 for (i = 0; i < ARRAY_SIZE(mlx5_class_attributes); i++) {
Wei Yongjun281d1a92013-07-30 07:54:26 +08005041 err = device_create_file(&dev->ib_dev.dev,
5042 mlx5_class_attributes[i]);
5043 if (err)
Mark Bloch16c19752018-01-01 13:06:58 +02005044 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03005045 }
5046
Mark Bloch16c19752018-01-01 13:06:58 +02005047 return 0;
5048}
5049
Mark Blochfc385b72018-01-16 14:34:48 +00005050static int mlx5_ib_stage_rep_reg_init(struct mlx5_ib_dev *dev)
5051{
5052 mlx5_ib_register_vport_reps(dev);
5053
5054 return 0;
5055}
5056
5057static void mlx5_ib_stage_rep_reg_cleanup(struct mlx5_ib_dev *dev)
5058{
5059 mlx5_ib_unregister_vport_reps(dev);
5060}
5061
Mark Blochb5ca15a2018-01-23 11:16:30 +00005062void __mlx5_ib_remove(struct mlx5_ib_dev *dev,
5063 const struct mlx5_ib_profile *profile,
5064 int stage)
Mark Bloch16c19752018-01-01 13:06:58 +02005065{
5066 /* Number of stages to cleanup */
5067 while (stage) {
5068 stage--;
5069 if (profile->stage[stage].cleanup)
5070 profile->stage[stage].cleanup(dev);
5071 }
5072
5073 ib_dealloc_device((struct ib_device *)dev);
5074}
5075
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005076static void *mlx5_ib_add_slave_port(struct mlx5_core_dev *mdev, u8 port_num);
5077
Mark Blochb5ca15a2018-01-23 11:16:30 +00005078void *__mlx5_ib_add(struct mlx5_ib_dev *dev,
5079 const struct mlx5_ib_profile *profile)
Mark Bloch16c19752018-01-01 13:06:58 +02005080{
Mark Bloch16c19752018-01-01 13:06:58 +02005081 int err;
5082 int i;
5083
5084 printk_once(KERN_INFO "%s", mlx5_version);
5085
Mark Bloch16c19752018-01-01 13:06:58 +02005086 for (i = 0; i < MLX5_IB_STAGE_MAX; i++) {
5087 if (profile->stage[i].init) {
5088 err = profile->stage[i].init(dev);
5089 if (err)
5090 goto err_out;
5091 }
5092 }
5093
5094 dev->profile = profile;
Eli Cohene126ba92013-07-07 17:25:49 +03005095 dev->ib_active = true;
5096
Jack Morgenstein9603b612014-07-28 23:30:22 +03005097 return dev;
Eli Cohene126ba92013-07-07 17:25:49 +03005098
Mark Bloch16c19752018-01-01 13:06:58 +02005099err_out:
5100 __mlx5_ib_remove(dev, profile, i);
Eli Cohene126ba92013-07-07 17:25:49 +03005101
Jack Morgenstein9603b612014-07-28 23:30:22 +03005102 return NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03005103}
5104
Mark Bloch16c19752018-01-01 13:06:58 +02005105static const struct mlx5_ib_profile pf_profile = {
5106 STAGE_CREATE(MLX5_IB_STAGE_INIT,
5107 mlx5_ib_stage_init_init,
5108 mlx5_ib_stage_init_cleanup),
Mark Bloch9a4ca382018-01-16 14:42:35 +00005109 STAGE_CREATE(MLX5_IB_STAGE_FLOW_DB,
5110 mlx5_ib_stage_flow_db_init,
5111 mlx5_ib_stage_flow_db_cleanup),
Mark Bloch16c19752018-01-01 13:06:58 +02005112 STAGE_CREATE(MLX5_IB_STAGE_CAPS,
5113 mlx5_ib_stage_caps_init,
5114 NULL),
Mark Bloch8e6efa32017-11-06 12:22:13 +00005115 STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB,
5116 mlx5_ib_stage_non_default_cb,
5117 NULL),
Mark Bloch16c19752018-01-01 13:06:58 +02005118 STAGE_CREATE(MLX5_IB_STAGE_ROCE,
5119 mlx5_ib_stage_roce_init,
5120 mlx5_ib_stage_roce_cleanup),
5121 STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES,
5122 mlx5_ib_stage_dev_res_init,
5123 mlx5_ib_stage_dev_res_cleanup),
5124 STAGE_CREATE(MLX5_IB_STAGE_ODP,
5125 mlx5_ib_stage_odp_init,
Mark Bloch3cc297d2018-01-01 13:07:03 +02005126 NULL),
Mark Bloch16c19752018-01-01 13:06:58 +02005127 STAGE_CREATE(MLX5_IB_STAGE_COUNTERS,
5128 mlx5_ib_stage_counters_init,
5129 mlx5_ib_stage_counters_cleanup),
5130 STAGE_CREATE(MLX5_IB_STAGE_CONG_DEBUGFS,
5131 mlx5_ib_stage_cong_debugfs_init,
5132 mlx5_ib_stage_cong_debugfs_cleanup),
5133 STAGE_CREATE(MLX5_IB_STAGE_UAR,
5134 mlx5_ib_stage_uar_init,
5135 mlx5_ib_stage_uar_cleanup),
5136 STAGE_CREATE(MLX5_IB_STAGE_BFREG,
5137 mlx5_ib_stage_bfrag_init,
5138 mlx5_ib_stage_bfrag_cleanup),
5139 STAGE_CREATE(MLX5_IB_STAGE_IB_REG,
5140 mlx5_ib_stage_ib_reg_init,
5141 mlx5_ib_stage_ib_reg_cleanup),
5142 STAGE_CREATE(MLX5_IB_STAGE_UMR_RESOURCES,
5143 mlx5_ib_stage_umr_res_init,
5144 mlx5_ib_stage_umr_res_cleanup),
5145 STAGE_CREATE(MLX5_IB_STAGE_DELAY_DROP,
5146 mlx5_ib_stage_delay_drop_init,
5147 mlx5_ib_stage_delay_drop_cleanup),
5148 STAGE_CREATE(MLX5_IB_STAGE_CLASS_ATTR,
5149 mlx5_ib_stage_class_attr_init,
5150 NULL),
Mark Bloch16c19752018-01-01 13:06:58 +02005151};
5152
Mark Blochb5ca15a2018-01-23 11:16:30 +00005153static const struct mlx5_ib_profile nic_rep_profile = {
5154 STAGE_CREATE(MLX5_IB_STAGE_INIT,
5155 mlx5_ib_stage_init_init,
5156 mlx5_ib_stage_init_cleanup),
5157 STAGE_CREATE(MLX5_IB_STAGE_FLOW_DB,
5158 mlx5_ib_stage_flow_db_init,
5159 mlx5_ib_stage_flow_db_cleanup),
5160 STAGE_CREATE(MLX5_IB_STAGE_CAPS,
5161 mlx5_ib_stage_caps_init,
5162 NULL),
5163 STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB,
5164 mlx5_ib_stage_rep_non_default_cb,
5165 NULL),
5166 STAGE_CREATE(MLX5_IB_STAGE_ROCE,
5167 mlx5_ib_stage_rep_roce_init,
5168 mlx5_ib_stage_rep_roce_cleanup),
5169 STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES,
5170 mlx5_ib_stage_dev_res_init,
5171 mlx5_ib_stage_dev_res_cleanup),
5172 STAGE_CREATE(MLX5_IB_STAGE_COUNTERS,
5173 mlx5_ib_stage_counters_init,
5174 mlx5_ib_stage_counters_cleanup),
5175 STAGE_CREATE(MLX5_IB_STAGE_UAR,
5176 mlx5_ib_stage_uar_init,
5177 mlx5_ib_stage_uar_cleanup),
5178 STAGE_CREATE(MLX5_IB_STAGE_BFREG,
5179 mlx5_ib_stage_bfrag_init,
5180 mlx5_ib_stage_bfrag_cleanup),
5181 STAGE_CREATE(MLX5_IB_STAGE_IB_REG,
5182 mlx5_ib_stage_ib_reg_init,
5183 mlx5_ib_stage_ib_reg_cleanup),
5184 STAGE_CREATE(MLX5_IB_STAGE_UMR_RESOURCES,
5185 mlx5_ib_stage_umr_res_init,
5186 mlx5_ib_stage_umr_res_cleanup),
5187 STAGE_CREATE(MLX5_IB_STAGE_CLASS_ATTR,
5188 mlx5_ib_stage_class_attr_init,
5189 NULL),
5190 STAGE_CREATE(MLX5_IB_STAGE_REP_REG,
5191 mlx5_ib_stage_rep_reg_init,
5192 mlx5_ib_stage_rep_reg_cleanup),
5193};
5194
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005195static void *mlx5_ib_add_slave_port(struct mlx5_core_dev *mdev, u8 port_num)
5196{
5197 struct mlx5_ib_multiport_info *mpi;
5198 struct mlx5_ib_dev *dev;
5199 bool bound = false;
5200 int err;
5201
5202 mpi = kzalloc(sizeof(*mpi), GFP_KERNEL);
5203 if (!mpi)
5204 return NULL;
5205
5206 mpi->mdev = mdev;
5207
5208 err = mlx5_query_nic_vport_system_image_guid(mdev,
5209 &mpi->sys_image_guid);
5210 if (err) {
5211 kfree(mpi);
5212 return NULL;
5213 }
5214
5215 mutex_lock(&mlx5_ib_multiport_mutex);
5216 list_for_each_entry(dev, &mlx5_ib_dev_list, ib_dev_list) {
5217 if (dev->sys_image_guid == mpi->sys_image_guid)
5218 bound = mlx5_ib_bind_slave_port(dev, mpi);
5219
5220 if (bound) {
5221 rdma_roce_rescan_device(&dev->ib_dev);
5222 break;
5223 }
5224 }
5225
5226 if (!bound) {
5227 list_add_tail(&mpi->list, &mlx5_ib_unaffiliated_port_list);
5228 dev_dbg(&mdev->pdev->dev, "no suitable IB device found to bind to, added to unaffiliated list.\n");
5229 } else {
5230 mlx5_ib_dbg(dev, "bound port %u\n", port_num + 1);
5231 }
5232 mutex_unlock(&mlx5_ib_multiport_mutex);
5233
5234 return mpi;
5235}
5236
Mark Bloch16c19752018-01-01 13:06:58 +02005237static void *mlx5_ib_add(struct mlx5_core_dev *mdev)
5238{
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005239 enum rdma_link_layer ll;
Mark Blochb5ca15a2018-01-23 11:16:30 +00005240 struct mlx5_ib_dev *dev;
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005241 int port_type_cap;
5242
Mark Blochb5ca15a2018-01-23 11:16:30 +00005243 printk_once(KERN_INFO "%s", mlx5_version);
5244
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005245 port_type_cap = MLX5_CAP_GEN(mdev, port_type);
5246 ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
5247
5248 if (mlx5_core_is_mp_slave(mdev) && ll == IB_LINK_LAYER_ETHERNET) {
5249 u8 port_num = mlx5_core_native_port_num(mdev) - 1;
5250
5251 return mlx5_ib_add_slave_port(mdev, port_num);
5252 }
5253
Mark Blochb5ca15a2018-01-23 11:16:30 +00005254 dev = (struct mlx5_ib_dev *)ib_alloc_device(sizeof(*dev));
5255 if (!dev)
5256 return NULL;
5257
5258 dev->mdev = mdev;
5259 dev->num_ports = max(MLX5_CAP_GEN(mdev, num_ports),
5260 MLX5_CAP_GEN(mdev, num_vhca_ports));
5261
5262 if (MLX5_VPORT_MANAGER(mdev) &&
5263 mlx5_ib_eswitch_mode(mdev->priv.eswitch) == SRIOV_OFFLOADS) {
5264 dev->rep = mlx5_ib_vport_rep(mdev->priv.eswitch, 0);
5265
5266 return __mlx5_ib_add(dev, &nic_rep_profile);
5267 }
5268
5269 return __mlx5_ib_add(dev, &pf_profile);
Mark Bloch16c19752018-01-01 13:06:58 +02005270}
5271
Jack Morgenstein9603b612014-07-28 23:30:22 +03005272static void mlx5_ib_remove(struct mlx5_core_dev *mdev, void *context)
Eli Cohene126ba92013-07-07 17:25:49 +03005273{
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005274 struct mlx5_ib_multiport_info *mpi;
5275 struct mlx5_ib_dev *dev;
Haggai Eran6aec21f2014-12-11 17:04:23 +02005276
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005277 if (mlx5_core_is_mp_slave(mdev)) {
5278 mpi = context;
5279 mutex_lock(&mlx5_ib_multiport_mutex);
5280 if (mpi->ibdev)
5281 mlx5_ib_unbind_slave_port(mpi->ibdev, mpi);
5282 list_del(&mpi->list);
5283 mutex_unlock(&mlx5_ib_multiport_mutex);
5284 return;
5285 }
5286
5287 dev = context;
Mark Bloch16c19752018-01-01 13:06:58 +02005288 __mlx5_ib_remove(dev, dev->profile, MLX5_IB_STAGE_MAX);
Eli Cohene126ba92013-07-07 17:25:49 +03005289}
5290
Jack Morgenstein9603b612014-07-28 23:30:22 +03005291static struct mlx5_interface mlx5_ib_interface = {
5292 .add = mlx5_ib_add,
5293 .remove = mlx5_ib_remove,
5294 .event = mlx5_ib_event,
Artemy Kovalyovd9aaed82017-01-02 11:37:46 +02005295#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
5296 .pfault = mlx5_ib_pfault,
5297#endif
Saeed Mahameed64613d942015-04-02 17:07:34 +03005298 .protocol = MLX5_INTERFACE_PROTOCOL_IB,
Eli Cohene126ba92013-07-07 17:25:49 +03005299};
5300
5301static int __init mlx5_ib_init(void)
5302{
Haggai Eran6aec21f2014-12-11 17:04:23 +02005303 int err;
5304
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02005305 mlx5_ib_event_wq = alloc_ordered_workqueue("mlx5_ib_event_wq", 0);
5306 if (!mlx5_ib_event_wq)
5307 return -ENOMEM;
5308
Artemy Kovalyov81713d32017-01-18 16:58:11 +02005309 mlx5_ib_odp_init();
Jack Morgenstein9603b612014-07-28 23:30:22 +03005310
Haggai Eran6aec21f2014-12-11 17:04:23 +02005311 err = mlx5_register_interface(&mlx5_ib_interface);
Haggai Eran6aec21f2014-12-11 17:04:23 +02005312
5313 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03005314}
5315
5316static void __exit mlx5_ib_cleanup(void)
5317{
Jack Morgenstein9603b612014-07-28 23:30:22 +03005318 mlx5_unregister_interface(&mlx5_ib_interface);
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02005319 destroy_workqueue(mlx5_ib_event_wq);
Eli Cohene126ba92013-07-07 17:25:49 +03005320}
5321
5322module_init(mlx5_ib_init);
5323module_exit(mlx5_ib_cleanup);