blob: 91e6b42798e5c6b1a4eec75a066d08f40d3a5c51 [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"
Parav Pandite1f24a72017-04-16 07:29:29 +030060#include "cmd.h"
Eli Cohene126ba92013-07-07 17:25:49 +030061
62#define DRIVER_NAME "mlx5_ib"
Tariq Toukanb3599112017-02-22 17:45:46 +020063#define DRIVER_VERSION "5.0-0"
Eli Cohene126ba92013-07-07 17:25:49 +030064
65MODULE_AUTHOR("Eli Cohen <eli@mellanox.com>");
66MODULE_DESCRIPTION("Mellanox Connect-IB HCA IB driver");
67MODULE_LICENSE("Dual BSD/GPL");
Eli Cohene126ba92013-07-07 17:25:49 +030068
Eli Cohene126ba92013-07-07 17:25:49 +030069static char mlx5_version[] =
70 DRIVER_NAME ": Mellanox Connect-IB Infiniband driver v"
Tariq Toukanb3599112017-02-22 17:45:46 +020071 DRIVER_VERSION "\n";
Eli Cohene126ba92013-07-07 17:25:49 +030072
Daniel Jurgensd69a24e2018-01-04 17:25:37 +020073struct mlx5_ib_event_work {
74 struct work_struct work;
75 struct mlx5_core_dev *dev;
76 void *context;
77 enum mlx5_dev_event event;
78 unsigned long param;
79};
80
Eran Ben Elishada7525d2015-12-14 16:34:10 +020081enum {
82 MLX5_ATOMIC_SIZE_QP_8BYTES = 1 << 3,
83};
Majd Dibbiny1b5daf12015-06-04 19:30:46 +030084
Daniel Jurgensd69a24e2018-01-04 17:25:37 +020085static struct workqueue_struct *mlx5_ib_event_wq;
Daniel Jurgens32f69e42018-01-04 17:25:36 +020086static LIST_HEAD(mlx5_ib_unaffiliated_port_list);
87static LIST_HEAD(mlx5_ib_dev_list);
88/*
89 * This mutex should be held when accessing either of the above lists
90 */
91static DEFINE_MUTEX(mlx5_ib_multiport_mutex);
92
93struct mlx5_ib_dev *mlx5_ib_get_ibdev_from_mpi(struct mlx5_ib_multiport_info *mpi)
94{
95 struct mlx5_ib_dev *dev;
96
97 mutex_lock(&mlx5_ib_multiport_mutex);
98 dev = mpi->ibdev;
99 mutex_unlock(&mlx5_ib_multiport_mutex);
100 return dev;
101}
102
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300103static enum rdma_link_layer
Achiad Shochatebd61f62015-12-23 18:47:16 +0200104mlx5_port_type_cap_to_rdma_ll(int port_type_cap)
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300105{
Achiad Shochatebd61f62015-12-23 18:47:16 +0200106 switch (port_type_cap) {
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300107 case MLX5_CAP_PORT_TYPE_IB:
108 return IB_LINK_LAYER_INFINIBAND;
109 case MLX5_CAP_PORT_TYPE_ETH:
110 return IB_LINK_LAYER_ETHERNET;
111 default:
112 return IB_LINK_LAYER_UNSPECIFIED;
113 }
114}
115
Achiad Shochatebd61f62015-12-23 18:47:16 +0200116static enum rdma_link_layer
117mlx5_ib_port_link_layer(struct ib_device *device, u8 port_num)
118{
119 struct mlx5_ib_dev *dev = to_mdev(device);
120 int port_type_cap = MLX5_CAP_GEN(dev->mdev, port_type);
121
122 return mlx5_port_type_cap_to_rdma_ll(port_type_cap);
123}
124
Moni Shouafd65f1b2017-05-30 09:56:05 +0300125static int get_port_state(struct ib_device *ibdev,
126 u8 port_num,
127 enum ib_port_state *state)
128{
129 struct ib_port_attr attr;
130 int ret;
131
132 memset(&attr, 0, sizeof(attr));
133 ret = mlx5_ib_query_port(ibdev, port_num, &attr);
134 if (!ret)
135 *state = attr.state;
136 return ret;
137}
138
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200139static int mlx5_netdev_event(struct notifier_block *this,
140 unsigned long event, void *ptr)
141{
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200142 struct mlx5_roce *roce = container_of(this, struct mlx5_roce, nb);
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200143 struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200144 u8 port_num = roce->native_port_num;
145 struct mlx5_core_dev *mdev;
146 struct mlx5_ib_dev *ibdev;
147
148 ibdev = roce->dev;
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200149 mdev = mlx5_ib_get_native_port_mdev(ibdev, port_num, NULL);
150 if (!mdev)
151 return NOTIFY_DONE;
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200152
Aviv Heller5ec8c832016-09-18 20:48:00 +0300153 switch (event) {
154 case NETDEV_REGISTER:
155 case NETDEV_UNREGISTER:
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200156 write_lock(&roce->netdev_lock);
157
158 if (ndev->dev.parent == &mdev->pdev->dev)
159 roce->netdev = (event == NETDEV_UNREGISTER) ?
160 NULL : ndev;
161 write_unlock(&roce->netdev_lock);
Aviv Heller5ec8c832016-09-18 20:48:00 +0300162 break;
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200163
Moni Shouafd65f1b2017-05-30 09:56:05 +0300164 case NETDEV_CHANGE:
Aviv Heller5ec8c832016-09-18 20:48:00 +0300165 case NETDEV_UP:
Aviv Heller88621df2016-09-18 20:48:02 +0300166 case NETDEV_DOWN: {
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200167 struct net_device *lag_ndev = mlx5_lag_get_roce_netdev(mdev);
Aviv Heller88621df2016-09-18 20:48:02 +0300168 struct net_device *upper = NULL;
169
170 if (lag_ndev) {
171 upper = netdev_master_upper_dev_get(lag_ndev);
172 dev_put(lag_ndev);
173 }
174
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200175 if ((upper == ndev || (!upper && ndev == roce->netdev))
Aviv Heller88621df2016-09-18 20:48:02 +0300176 && ibdev->ib_active) {
Bart Van Assche626bc022016-12-05 17:18:08 -0800177 struct ib_event ibev = { };
Moni Shouafd65f1b2017-05-30 09:56:05 +0300178 enum ib_port_state port_state;
Aviv Heller5ec8c832016-09-18 20:48:00 +0300179
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200180 if (get_port_state(&ibdev->ib_dev, port_num,
181 &port_state))
182 goto done;
Moni Shouafd65f1b2017-05-30 09:56:05 +0300183
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200184 if (roce->last_port_state == port_state)
185 goto done;
Moni Shouafd65f1b2017-05-30 09:56:05 +0300186
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200187 roce->last_port_state = port_state;
Aviv Heller5ec8c832016-09-18 20:48:00 +0300188 ibev.device = &ibdev->ib_dev;
Moni Shouafd65f1b2017-05-30 09:56:05 +0300189 if (port_state == IB_PORT_DOWN)
190 ibev.event = IB_EVENT_PORT_ERR;
191 else if (port_state == IB_PORT_ACTIVE)
192 ibev.event = IB_EVENT_PORT_ACTIVE;
193 else
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200194 goto done;
Moni Shouafd65f1b2017-05-30 09:56:05 +0300195
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200196 ibev.element.port_num = port_num;
Aviv Heller5ec8c832016-09-18 20:48:00 +0300197 ib_dispatch_event(&ibev);
198 }
199 break;
Aviv Heller88621df2016-09-18 20:48:02 +0300200 }
Aviv Heller5ec8c832016-09-18 20:48:00 +0300201
202 default:
203 break;
204 }
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200205done:
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200206 mlx5_ib_put_native_port_mdev(ibdev, port_num);
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200207 return NOTIFY_DONE;
208}
209
210static struct net_device *mlx5_ib_get_netdev(struct ib_device *device,
211 u8 port_num)
212{
213 struct mlx5_ib_dev *ibdev = to_mdev(device);
214 struct net_device *ndev;
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200215 struct mlx5_core_dev *mdev;
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200216
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200217 mdev = mlx5_ib_get_native_port_mdev(ibdev, port_num, NULL);
218 if (!mdev)
219 return NULL;
220
221 ndev = mlx5_lag_get_roce_netdev(mdev);
Aviv Heller88621df2016-09-18 20:48:02 +0300222 if (ndev)
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200223 goto out;
Aviv Heller88621df2016-09-18 20:48:02 +0300224
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200225 /* Ensure ndev does not disappear before we invoke dev_hold()
226 */
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200227 read_lock(&ibdev->roce[port_num - 1].netdev_lock);
228 ndev = ibdev->roce[port_num - 1].netdev;
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200229 if (ndev)
230 dev_hold(ndev);
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +0200231 read_unlock(&ibdev->roce[port_num - 1].netdev_lock);
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200232
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200233out:
234 mlx5_ib_put_native_port_mdev(ibdev, port_num);
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200235 return ndev;
236}
237
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200238struct mlx5_core_dev *mlx5_ib_get_native_port_mdev(struct mlx5_ib_dev *ibdev,
239 u8 ib_port_num,
240 u8 *native_port_num)
241{
242 enum rdma_link_layer ll = mlx5_ib_port_link_layer(&ibdev->ib_dev,
243 ib_port_num);
244 struct mlx5_core_dev *mdev = NULL;
245 struct mlx5_ib_multiport_info *mpi;
246 struct mlx5_ib_port *port;
247
248 if (native_port_num)
249 *native_port_num = 1;
250
251 if (!mlx5_core_mp_enabled(ibdev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
252 return ibdev->mdev;
253
254 port = &ibdev->port[ib_port_num - 1];
255 if (!port)
256 return NULL;
257
258 spin_lock(&port->mp.mpi_lock);
259 mpi = ibdev->port[ib_port_num - 1].mp.mpi;
260 if (mpi && !mpi->unaffiliate) {
261 mdev = mpi->mdev;
262 /* If it's the master no need to refcount, it'll exist
263 * as long as the ib_dev exists.
264 */
265 if (!mpi->is_master)
266 mpi->mdev_refcnt++;
267 }
268 spin_unlock(&port->mp.mpi_lock);
269
270 return mdev;
271}
272
273void mlx5_ib_put_native_port_mdev(struct mlx5_ib_dev *ibdev, u8 port_num)
274{
275 enum rdma_link_layer ll = mlx5_ib_port_link_layer(&ibdev->ib_dev,
276 port_num);
277 struct mlx5_ib_multiport_info *mpi;
278 struct mlx5_ib_port *port;
279
280 if (!mlx5_core_mp_enabled(ibdev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
281 return;
282
283 port = &ibdev->port[port_num - 1];
284
285 spin_lock(&port->mp.mpi_lock);
286 mpi = ibdev->port[port_num - 1].mp.mpi;
287 if (mpi->is_master)
288 goto out;
289
290 mpi->mdev_refcnt--;
291 if (mpi->unaffiliate)
292 complete(&mpi->unref_comp);
293out:
294 spin_unlock(&port->mp.mpi_lock);
295}
296
Noa Osherovichf1b65df2017-04-20 20:53:33 +0300297static int translate_eth_proto_oper(u32 eth_proto_oper, u8 *active_speed,
298 u8 *active_width)
299{
300 switch (eth_proto_oper) {
301 case MLX5E_PROT_MASK(MLX5E_1000BASE_CX_SGMII):
302 case MLX5E_PROT_MASK(MLX5E_1000BASE_KX):
303 case MLX5E_PROT_MASK(MLX5E_100BASE_TX):
304 case MLX5E_PROT_MASK(MLX5E_1000BASE_T):
305 *active_width = IB_WIDTH_1X;
306 *active_speed = IB_SPEED_SDR;
307 break;
308 case MLX5E_PROT_MASK(MLX5E_10GBASE_T):
309 case MLX5E_PROT_MASK(MLX5E_10GBASE_CX4):
310 case MLX5E_PROT_MASK(MLX5E_10GBASE_KX4):
311 case MLX5E_PROT_MASK(MLX5E_10GBASE_KR):
312 case MLX5E_PROT_MASK(MLX5E_10GBASE_CR):
313 case MLX5E_PROT_MASK(MLX5E_10GBASE_SR):
314 case MLX5E_PROT_MASK(MLX5E_10GBASE_ER):
315 *active_width = IB_WIDTH_1X;
316 *active_speed = IB_SPEED_QDR;
317 break;
318 case MLX5E_PROT_MASK(MLX5E_25GBASE_CR):
319 case MLX5E_PROT_MASK(MLX5E_25GBASE_KR):
320 case MLX5E_PROT_MASK(MLX5E_25GBASE_SR):
321 *active_width = IB_WIDTH_1X;
322 *active_speed = IB_SPEED_EDR;
323 break;
324 case MLX5E_PROT_MASK(MLX5E_40GBASE_CR4):
325 case MLX5E_PROT_MASK(MLX5E_40GBASE_KR4):
326 case MLX5E_PROT_MASK(MLX5E_40GBASE_SR4):
327 case MLX5E_PROT_MASK(MLX5E_40GBASE_LR4):
328 *active_width = IB_WIDTH_4X;
329 *active_speed = IB_SPEED_QDR;
330 break;
331 case MLX5E_PROT_MASK(MLX5E_50GBASE_CR2):
332 case MLX5E_PROT_MASK(MLX5E_50GBASE_KR2):
333 case MLX5E_PROT_MASK(MLX5E_50GBASE_SR2):
334 *active_width = IB_WIDTH_1X;
335 *active_speed = IB_SPEED_HDR;
336 break;
337 case MLX5E_PROT_MASK(MLX5E_56GBASE_R4):
338 *active_width = IB_WIDTH_4X;
339 *active_speed = IB_SPEED_FDR;
340 break;
341 case MLX5E_PROT_MASK(MLX5E_100GBASE_CR4):
342 case MLX5E_PROT_MASK(MLX5E_100GBASE_SR4):
343 case MLX5E_PROT_MASK(MLX5E_100GBASE_KR4):
344 case MLX5E_PROT_MASK(MLX5E_100GBASE_LR4):
345 *active_width = IB_WIDTH_4X;
346 *active_speed = IB_SPEED_EDR;
347 break;
348 default:
349 return -EINVAL;
350 }
351
352 return 0;
353}
354
Ilan Tayari095b0922017-05-14 16:04:30 +0300355static int mlx5_query_port_roce(struct ib_device *device, u8 port_num,
356 struct ib_port_attr *props)
Achiad Shochat3f89a642015-12-23 18:47:21 +0200357{
358 struct mlx5_ib_dev *dev = to_mdev(device);
Colin Ian Kingda005f92018-01-09 15:55:43 +0000359 struct mlx5_core_dev *mdev;
Aviv Heller88621df2016-09-18 20:48:02 +0300360 struct net_device *ndev, *upper;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200361 enum ib_mtu ndev_ib_mtu;
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200362 bool put_mdev = true;
Leon Romanovskyc876a1b2016-01-09 13:06:25 +0200363 u16 qkey_viol_cntr;
Noa Osherovichf1b65df2017-04-20 20:53:33 +0300364 u32 eth_prot_oper;
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200365 u8 mdev_port_num;
Ilan Tayari095b0922017-05-14 16:04:30 +0300366 int err;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200367
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200368 mdev = mlx5_ib_get_native_port_mdev(dev, port_num, &mdev_port_num);
369 if (!mdev) {
370 /* This means the port isn't affiliated yet. Get the
371 * info for the master port instead.
372 */
373 put_mdev = false;
374 mdev = dev->mdev;
375 mdev_port_num = 1;
376 port_num = 1;
377 }
378
Noa Osherovichf1b65df2017-04-20 20:53:33 +0300379 /* Possible bad flows are checked before filling out props so in case
380 * of an error it will still be zeroed out.
Noa Osherovich50f22fd2017-04-20 20:53:32 +0300381 */
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200382 err = mlx5_query_port_eth_proto_oper(mdev, &eth_prot_oper,
383 mdev_port_num);
Ilan Tayari095b0922017-05-14 16:04:30 +0300384 if (err)
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200385 goto out;
Noa Osherovichf1b65df2017-04-20 20:53:33 +0300386
387 translate_eth_proto_oper(eth_prot_oper, &props->active_speed,
388 &props->active_width);
Achiad Shochat3f89a642015-12-23 18:47:21 +0200389
390 props->port_cap_flags |= IB_PORT_CM_SUP;
391 props->port_cap_flags |= IB_PORT_IP_BASED_GIDS;
392
393 props->gid_tbl_len = MLX5_CAP_ROCE(dev->mdev,
394 roce_address_table_size);
395 props->max_mtu = IB_MTU_4096;
396 props->max_msg_sz = 1 << MLX5_CAP_GEN(dev->mdev, log_max_msg);
397 props->pkey_tbl_len = 1;
398 props->state = IB_PORT_DOWN;
399 props->phys_state = 3;
400
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200401 mlx5_query_nic_vport_qkey_viol_cntr(mdev, &qkey_viol_cntr);
Leon Romanovskyc876a1b2016-01-09 13:06:25 +0200402 props->qkey_viol_cntr = qkey_viol_cntr;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200403
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200404 /* If this is a stub query for an unaffiliated port stop here */
405 if (!put_mdev)
406 goto out;
407
Achiad Shochat3f89a642015-12-23 18:47:21 +0200408 ndev = mlx5_ib_get_netdev(device, port_num);
409 if (!ndev)
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200410 goto out;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200411
Aviv Heller88621df2016-09-18 20:48:02 +0300412 if (mlx5_lag_is_active(dev->mdev)) {
413 rcu_read_lock();
414 upper = netdev_master_upper_dev_get_rcu(ndev);
415 if (upper) {
416 dev_put(ndev);
417 ndev = upper;
418 dev_hold(ndev);
419 }
420 rcu_read_unlock();
421 }
422
Achiad Shochat3f89a642015-12-23 18:47:21 +0200423 if (netif_running(ndev) && netif_carrier_ok(ndev)) {
424 props->state = IB_PORT_ACTIVE;
425 props->phys_state = 5;
426 }
427
428 ndev_ib_mtu = iboe_get_mtu(ndev->mtu);
429
430 dev_put(ndev);
431
432 props->active_mtu = min(props->max_mtu, ndev_ib_mtu);
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +0200433out:
434 if (put_mdev)
435 mlx5_ib_put_native_port_mdev(dev, port_num);
436 return err;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200437}
438
Ilan Tayari095b0922017-05-14 16:04:30 +0300439static int set_roce_addr(struct mlx5_ib_dev *dev, u8 port_num,
440 unsigned int index, const union ib_gid *gid,
441 const struct ib_gid_attr *attr)
Achiad Shochat3cca2602015-12-23 18:47:23 +0200442{
Ilan Tayari095b0922017-05-14 16:04:30 +0300443 enum ib_gid_type gid_type = IB_GID_TYPE_IB;
444 u8 roce_version = 0;
445 u8 roce_l3_type = 0;
446 bool vlan = false;
447 u8 mac[ETH_ALEN];
448 u16 vlan_id = 0;
Achiad Shochat3cca2602015-12-23 18:47:23 +0200449
Ilan Tayari095b0922017-05-14 16:04:30 +0300450 if (gid) {
451 gid_type = attr->gid_type;
452 ether_addr_copy(mac, attr->ndev->dev_addr);
Achiad Shochat3cca2602015-12-23 18:47:23 +0200453
Ilan Tayari095b0922017-05-14 16:04:30 +0300454 if (is_vlan_dev(attr->ndev)) {
455 vlan = true;
456 vlan_id = vlan_dev_vlan_id(attr->ndev);
457 }
Achiad Shochat3cca2602015-12-23 18:47:23 +0200458 }
459
Ilan Tayari095b0922017-05-14 16:04:30 +0300460 switch (gid_type) {
Achiad Shochat3cca2602015-12-23 18:47:23 +0200461 case IB_GID_TYPE_IB:
Ilan Tayari095b0922017-05-14 16:04:30 +0300462 roce_version = MLX5_ROCE_VERSION_1;
Achiad Shochat3cca2602015-12-23 18:47:23 +0200463 break;
464 case IB_GID_TYPE_ROCE_UDP_ENCAP:
Ilan Tayari095b0922017-05-14 16:04:30 +0300465 roce_version = MLX5_ROCE_VERSION_2;
466 if (ipv6_addr_v4mapped((void *)gid))
467 roce_l3_type = MLX5_ROCE_L3_TYPE_IPV4;
468 else
469 roce_l3_type = MLX5_ROCE_L3_TYPE_IPV6;
Achiad Shochat3cca2602015-12-23 18:47:23 +0200470 break;
471
472 default:
Ilan Tayari095b0922017-05-14 16:04:30 +0300473 mlx5_ib_warn(dev, "Unexpected GID type %u\n", gid_type);
Achiad Shochat3cca2602015-12-23 18:47:23 +0200474 }
475
Ilan Tayari095b0922017-05-14 16:04:30 +0300476 return mlx5_core_roce_gid_set(dev->mdev, index, roce_version,
477 roce_l3_type, gid->raw, mac, vlan,
Daniel Jurgenscfe4e372018-01-04 17:25:41 +0200478 vlan_id, port_num);
Achiad Shochat3cca2602015-12-23 18:47:23 +0200479}
480
481static int mlx5_ib_add_gid(struct ib_device *device, u8 port_num,
482 unsigned int index, const union ib_gid *gid,
483 const struct ib_gid_attr *attr,
484 __always_unused void **context)
485{
Ilan Tayari095b0922017-05-14 16:04:30 +0300486 return set_roce_addr(to_mdev(device), port_num, index, gid, attr);
Achiad Shochat3cca2602015-12-23 18:47:23 +0200487}
488
489static int mlx5_ib_del_gid(struct ib_device *device, u8 port_num,
490 unsigned int index, __always_unused void **context)
491{
Ilan Tayari095b0922017-05-14 16:04:30 +0300492 return set_roce_addr(to_mdev(device), port_num, index, NULL, NULL);
Achiad Shochat3cca2602015-12-23 18:47:23 +0200493}
494
Achiad Shochat2811ba52015-12-23 18:47:24 +0200495__be16 mlx5_get_roce_udp_sport(struct mlx5_ib_dev *dev, u8 port_num,
496 int index)
497{
498 struct ib_gid_attr attr;
499 union ib_gid gid;
500
501 if (ib_get_cached_gid(&dev->ib_dev, port_num, index, &gid, &attr))
502 return 0;
503
504 if (!attr.ndev)
505 return 0;
506
507 dev_put(attr.ndev);
508
509 if (attr.gid_type != IB_GID_TYPE_ROCE_UDP_ENCAP)
510 return 0;
511
512 return cpu_to_be16(MLX5_CAP_ROCE(dev->mdev, r_roce_min_src_udp_port));
513}
514
Majd Dibbinyed884512017-01-18 14:10:35 +0200515int mlx5_get_roce_gid_type(struct mlx5_ib_dev *dev, u8 port_num,
516 int index, enum ib_gid_type *gid_type)
517{
518 struct ib_gid_attr attr;
519 union ib_gid gid;
520 int ret;
521
522 ret = ib_get_cached_gid(&dev->ib_dev, port_num, index, &gid, &attr);
523 if (ret)
524 return ret;
525
526 if (!attr.ndev)
527 return -ENODEV;
528
529 dev_put(attr.ndev);
530
531 *gid_type = attr.gid_type;
532
533 return 0;
534}
535
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300536static int mlx5_use_mad_ifc(struct mlx5_ib_dev *dev)
537{
Noa Osherovich7fae6652016-09-12 19:16:23 +0300538 if (MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_IB)
539 return !MLX5_CAP_GEN(dev->mdev, ib_virt);
540 return 0;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300541}
542
543enum {
544 MLX5_VPORT_ACCESS_METHOD_MAD,
545 MLX5_VPORT_ACCESS_METHOD_HCA,
546 MLX5_VPORT_ACCESS_METHOD_NIC,
547};
548
549static int mlx5_get_vport_access_method(struct ib_device *ibdev)
550{
551 if (mlx5_use_mad_ifc(to_mdev(ibdev)))
552 return MLX5_VPORT_ACCESS_METHOD_MAD;
553
Achiad Shochatebd61f62015-12-23 18:47:16 +0200554 if (mlx5_ib_port_link_layer(ibdev, 1) ==
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300555 IB_LINK_LAYER_ETHERNET)
556 return MLX5_VPORT_ACCESS_METHOD_NIC;
557
558 return MLX5_VPORT_ACCESS_METHOD_HCA;
559}
560
Eran Ben Elishada7525d2015-12-14 16:34:10 +0200561static void get_atomic_caps(struct mlx5_ib_dev *dev,
Moni Shoua776a3902018-01-02 16:19:33 +0200562 u8 atomic_size_qp,
Eran Ben Elishada7525d2015-12-14 16:34:10 +0200563 struct ib_device_attr *props)
564{
565 u8 tmp;
566 u8 atomic_operations = MLX5_CAP_ATOMIC(dev->mdev, atomic_operations);
Eran Ben Elishada7525d2015-12-14 16:34:10 +0200567 u8 atomic_req_8B_endianness_mode =
Or Gerlitzbd108382017-05-28 15:24:17 +0300568 MLX5_CAP_ATOMIC(dev->mdev, atomic_req_8B_endianness_mode);
Eran Ben Elishada7525d2015-12-14 16:34:10 +0200569
570 /* Check if HW supports 8 bytes standard atomic operations and capable
571 * of host endianness respond
572 */
573 tmp = MLX5_ATOMIC_OPS_CMP_SWAP | MLX5_ATOMIC_OPS_FETCH_ADD;
574 if (((atomic_operations & tmp) == tmp) &&
575 (atomic_size_qp & MLX5_ATOMIC_SIZE_QP_8BYTES) &&
576 (atomic_req_8B_endianness_mode)) {
577 props->atomic_cap = IB_ATOMIC_HCA;
578 } else {
579 props->atomic_cap = IB_ATOMIC_NONE;
580 }
581}
582
Moni Shoua776a3902018-01-02 16:19:33 +0200583static void get_atomic_caps_qp(struct mlx5_ib_dev *dev,
584 struct ib_device_attr *props)
585{
586 u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_qp);
587
588 get_atomic_caps(dev, atomic_size_qp, props);
589}
590
591static void get_atomic_caps_dc(struct mlx5_ib_dev *dev,
592 struct ib_device_attr *props)
593{
594 u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_dc);
595
596 get_atomic_caps(dev, atomic_size_qp, props);
597}
598
599bool mlx5_ib_dc_atomic_is_supported(struct mlx5_ib_dev *dev)
600{
601 struct ib_device_attr props = {};
602
603 get_atomic_caps_dc(dev, &props);
604 return (props.atomic_cap == IB_ATOMIC_HCA) ? true : false;
605}
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300606static int mlx5_query_system_image_guid(struct ib_device *ibdev,
607 __be64 *sys_image_guid)
608{
609 struct mlx5_ib_dev *dev = to_mdev(ibdev);
610 struct mlx5_core_dev *mdev = dev->mdev;
611 u64 tmp;
612 int err;
613
614 switch (mlx5_get_vport_access_method(ibdev)) {
615 case MLX5_VPORT_ACCESS_METHOD_MAD:
616 return mlx5_query_mad_ifc_system_image_guid(ibdev,
617 sys_image_guid);
618
619 case MLX5_VPORT_ACCESS_METHOD_HCA:
620 err = mlx5_query_hca_vport_system_image_guid(mdev, &tmp);
Achiad Shochat3f89a642015-12-23 18:47:21 +0200621 break;
622
623 case MLX5_VPORT_ACCESS_METHOD_NIC:
624 err = mlx5_query_nic_vport_system_image_guid(mdev, &tmp);
625 break;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300626
627 default:
628 return -EINVAL;
629 }
Achiad Shochat3f89a642015-12-23 18:47:21 +0200630
631 if (!err)
632 *sys_image_guid = cpu_to_be64(tmp);
633
634 return err;
635
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300636}
637
638static int mlx5_query_max_pkeys(struct ib_device *ibdev,
639 u16 *max_pkeys)
640{
641 struct mlx5_ib_dev *dev = to_mdev(ibdev);
642 struct mlx5_core_dev *mdev = dev->mdev;
643
644 switch (mlx5_get_vport_access_method(ibdev)) {
645 case MLX5_VPORT_ACCESS_METHOD_MAD:
646 return mlx5_query_mad_ifc_max_pkeys(ibdev, max_pkeys);
647
648 case MLX5_VPORT_ACCESS_METHOD_HCA:
649 case MLX5_VPORT_ACCESS_METHOD_NIC:
650 *max_pkeys = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev,
651 pkey_table_size));
652 return 0;
653
654 default:
655 return -EINVAL;
656 }
657}
658
659static int mlx5_query_vendor_id(struct ib_device *ibdev,
660 u32 *vendor_id)
661{
662 struct mlx5_ib_dev *dev = to_mdev(ibdev);
663
664 switch (mlx5_get_vport_access_method(ibdev)) {
665 case MLX5_VPORT_ACCESS_METHOD_MAD:
666 return mlx5_query_mad_ifc_vendor_id(ibdev, vendor_id);
667
668 case MLX5_VPORT_ACCESS_METHOD_HCA:
669 case MLX5_VPORT_ACCESS_METHOD_NIC:
670 return mlx5_core_query_vendor_id(dev->mdev, vendor_id);
671
672 default:
673 return -EINVAL;
674 }
675}
676
677static int mlx5_query_node_guid(struct mlx5_ib_dev *dev,
678 __be64 *node_guid)
679{
680 u64 tmp;
681 int err;
682
683 switch (mlx5_get_vport_access_method(&dev->ib_dev)) {
684 case MLX5_VPORT_ACCESS_METHOD_MAD:
685 return mlx5_query_mad_ifc_node_guid(dev, node_guid);
686
687 case MLX5_VPORT_ACCESS_METHOD_HCA:
688 err = mlx5_query_hca_vport_node_guid(dev->mdev, &tmp);
Achiad Shochat3f89a642015-12-23 18:47:21 +0200689 break;
690
691 case MLX5_VPORT_ACCESS_METHOD_NIC:
692 err = mlx5_query_nic_vport_node_guid(dev->mdev, &tmp);
693 break;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300694
695 default:
696 return -EINVAL;
697 }
Achiad Shochat3f89a642015-12-23 18:47:21 +0200698
699 if (!err)
700 *node_guid = cpu_to_be64(tmp);
701
702 return err;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300703}
704
705struct mlx5_reg_node_desc {
Yuval Shaiabd99fde2016-08-25 10:57:07 -0700706 u8 desc[IB_DEVICE_NODE_DESC_MAX];
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300707};
708
709static int mlx5_query_node_desc(struct mlx5_ib_dev *dev, char *node_desc)
710{
711 struct mlx5_reg_node_desc in;
712
713 if (mlx5_use_mad_ifc(dev))
714 return mlx5_query_mad_ifc_node_desc(dev, node_desc);
715
716 memset(&in, 0, sizeof(in));
717
718 return mlx5_core_access_reg(dev->mdev, &in, sizeof(in), node_desc,
719 sizeof(struct mlx5_reg_node_desc),
720 MLX5_REG_NODE_DESC, 0, 0);
721}
722
Eli Cohene126ba92013-07-07 17:25:49 +0300723static int mlx5_ib_query_device(struct ib_device *ibdev,
Matan Barak2528e332015-06-11 16:35:25 +0300724 struct ib_device_attr *props,
725 struct ib_udata *uhw)
Eli Cohene126ba92013-07-07 17:25:49 +0300726{
727 struct mlx5_ib_dev *dev = to_mdev(ibdev);
Saeed Mahameed938fe832015-05-28 22:28:41 +0300728 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +0300729 int err = -ENOMEM;
Eli Cohen288c01b2016-10-27 16:36:45 +0300730 int max_sq_desc;
Eli Cohene126ba92013-07-07 17:25:49 +0300731 int max_rq_sg;
732 int max_sq_sg;
Sagi Grimberge0238a62015-07-21 14:40:12 +0300733 u64 min_page_size = 1ull << MLX5_CAP_GEN(mdev, log_pg_sz);
Daniel Jurgens85c7c012018-01-04 17:25:43 +0200734 bool raw_support = !mlx5_core_mp_enabled(mdev);
Bodong Wang402ca532016-06-17 15:02:20 +0300735 struct mlx5_ib_query_device_resp resp = {};
736 size_t resp_len;
737 u64 max_tso;
Eli Cohene126ba92013-07-07 17:25:49 +0300738
Bodong Wang402ca532016-06-17 15:02:20 +0300739 resp_len = sizeof(resp.comp_mask) + sizeof(resp.response_length);
740 if (uhw->outlen && uhw->outlen < resp_len)
741 return -EINVAL;
742 else
743 resp.response_length = resp_len;
744
745 if (uhw->inlen && !ib_is_udata_cleared(uhw, 0, uhw->inlen))
Matan Barak2528e332015-06-11 16:35:25 +0300746 return -EINVAL;
747
Eli Cohene126ba92013-07-07 17:25:49 +0300748 memset(props, 0, sizeof(*props));
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300749 err = mlx5_query_system_image_guid(ibdev,
750 &props->sys_image_guid);
751 if (err)
752 return err;
753
754 err = mlx5_query_max_pkeys(ibdev, &props->max_pkeys);
755 if (err)
756 return err;
757
758 err = mlx5_query_vendor_id(ibdev, &props->vendor_id);
759 if (err)
760 return err;
Eli Cohene126ba92013-07-07 17:25:49 +0300761
Jack Morgenstein9603b612014-07-28 23:30:22 +0300762 props->fw_ver = ((u64)fw_rev_maj(dev->mdev) << 32) |
763 (fw_rev_min(dev->mdev) << 16) |
764 fw_rev_sub(dev->mdev);
Eli Cohene126ba92013-07-07 17:25:49 +0300765 props->device_cap_flags = IB_DEVICE_CHANGE_PHY_PORT |
766 IB_DEVICE_PORT_ACTIVE_EVENT |
767 IB_DEVICE_SYS_IMAGE_GUID |
Eli Cohen1a4c3a32014-02-06 17:41:25 +0200768 IB_DEVICE_RC_RNR_NAK_GEN;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300769
770 if (MLX5_CAP_GEN(mdev, pkv))
Eli Cohene126ba92013-07-07 17:25:49 +0300771 props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300772 if (MLX5_CAP_GEN(mdev, qkv))
Eli Cohene126ba92013-07-07 17:25:49 +0300773 props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300774 if (MLX5_CAP_GEN(mdev, apm))
Eli Cohene126ba92013-07-07 17:25:49 +0300775 props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300776 if (MLX5_CAP_GEN(mdev, xrc))
Eli Cohene126ba92013-07-07 17:25:49 +0300777 props->device_cap_flags |= IB_DEVICE_XRC;
Matan Barakd2370e02016-02-29 18:05:30 +0200778 if (MLX5_CAP_GEN(mdev, imaicl)) {
779 props->device_cap_flags |= IB_DEVICE_MEM_WINDOW |
780 IB_DEVICE_MEM_WINDOW_TYPE_2B;
781 props->max_mw = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
Sagi Grimbergb005d312016-02-29 19:07:33 +0200782 /* We support 'Gappy' memory registration too */
783 props->device_cap_flags |= IB_DEVICE_SG_GAPS_REG;
Matan Barakd2370e02016-02-29 18:05:30 +0200784 }
Eli Cohene126ba92013-07-07 17:25:49 +0300785 props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300786 if (MLX5_CAP_GEN(mdev, sho)) {
Sagi Grimberg2dea9092014-02-23 14:19:13 +0200787 props->device_cap_flags |= IB_DEVICE_SIGNATURE_HANDOVER;
788 /* At this stage no support for signature handover */
789 props->sig_prot_cap = IB_PROT_T10DIF_TYPE_1 |
790 IB_PROT_T10DIF_TYPE_2 |
791 IB_PROT_T10DIF_TYPE_3;
792 props->sig_guard_cap = IB_GUARD_T10DIF_CRC |
793 IB_GUARD_T10DIF_CSUM;
794 }
Saeed Mahameed938fe832015-05-28 22:28:41 +0300795 if (MLX5_CAP_GEN(mdev, block_lb_mc))
Eli Cohenf360d882014-04-02 00:10:16 +0300796 props->device_cap_flags |= IB_DEVICE_BLOCK_MULTICAST_LOOPBACK;
Eli Cohene126ba92013-07-07 17:25:49 +0300797
Daniel Jurgens85c7c012018-01-04 17:25:43 +0200798 if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) && raw_support) {
Noa Osheroviche8161332017-01-18 15:40:01 +0200799 if (MLX5_CAP_ETH(mdev, csum_cap)) {
800 /* Legacy bit to support old userspace libraries */
Bodong Wang88115fe2015-12-18 13:53:20 +0200801 props->device_cap_flags |= IB_DEVICE_RAW_IP_CSUM;
Noa Osheroviche8161332017-01-18 15:40:01 +0200802 props->raw_packet_caps |= IB_RAW_PACKET_CAP_IP_CSUM;
803 }
804
805 if (MLX5_CAP_ETH(dev->mdev, vlan_cap))
806 props->raw_packet_caps |=
807 IB_RAW_PACKET_CAP_CVLAN_STRIPPING;
Bodong Wang88115fe2015-12-18 13:53:20 +0200808
Bodong Wang402ca532016-06-17 15:02:20 +0300809 if (field_avail(typeof(resp), tso_caps, uhw->outlen)) {
810 max_tso = MLX5_CAP_ETH(mdev, max_lso_cap);
811 if (max_tso) {
812 resp.tso_caps.max_tso = 1 << max_tso;
813 resp.tso_caps.supported_qpts |=
814 1 << IB_QPT_RAW_PACKET;
815 resp.response_length += sizeof(resp.tso_caps);
816 }
817 }
Yishai Hadas31f69a82016-08-28 11:28:45 +0300818
819 if (field_avail(typeof(resp), rss_caps, uhw->outlen)) {
820 resp.rss_caps.rx_hash_function =
821 MLX5_RX_HASH_FUNC_TOEPLITZ;
822 resp.rss_caps.rx_hash_fields_mask =
823 MLX5_RX_HASH_SRC_IPV4 |
824 MLX5_RX_HASH_DST_IPV4 |
825 MLX5_RX_HASH_SRC_IPV6 |
826 MLX5_RX_HASH_DST_IPV6 |
827 MLX5_RX_HASH_SRC_PORT_TCP |
828 MLX5_RX_HASH_DST_PORT_TCP |
829 MLX5_RX_HASH_SRC_PORT_UDP |
Maor Gottlieb4e2b53a2017-12-24 14:51:25 +0200830 MLX5_RX_HASH_DST_PORT_UDP |
831 MLX5_RX_HASH_INNER;
Yishai Hadas31f69a82016-08-28 11:28:45 +0300832 resp.response_length += sizeof(resp.rss_caps);
833 }
834 } else {
835 if (field_avail(typeof(resp), tso_caps, uhw->outlen))
836 resp.response_length += sizeof(resp.tso_caps);
837 if (field_avail(typeof(resp), rss_caps, uhw->outlen))
838 resp.response_length += sizeof(resp.rss_caps);
Bodong Wang402ca532016-06-17 15:02:20 +0300839 }
840
Erez Shitritf0313962016-02-21 16:27:17 +0200841 if (MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) {
842 props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
843 props->device_cap_flags |= IB_DEVICE_UD_TSO;
844 }
845
Maor Gottlieb03404e82017-05-30 10:29:13 +0300846 if (MLX5_CAP_GEN(dev->mdev, rq_delay_drop) &&
Daniel Jurgens85c7c012018-01-04 17:25:43 +0200847 MLX5_CAP_GEN(dev->mdev, general_notification_event) &&
848 raw_support)
Maor Gottlieb03404e82017-05-30 10:29:13 +0300849 props->raw_packet_caps |= IB_RAW_PACKET_CAP_DELAY_DROP;
850
Yishai Hadas1d54f892017-06-08 16:15:11 +0300851 if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads) &&
852 MLX5_CAP_IPOIB_ENHANCED(mdev, csum_cap))
853 props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
854
Majd Dibbinycff5a0f2016-04-17 17:19:38 +0300855 if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
Daniel Jurgens85c7c012018-01-04 17:25:43 +0200856 MLX5_CAP_ETH(dev->mdev, scatter_fcs) &&
857 raw_support) {
Noa Osheroviche8161332017-01-18 15:40:01 +0200858 /* Legacy bit to support old userspace libraries */
Majd Dibbinycff5a0f2016-04-17 17:19:38 +0300859 props->device_cap_flags |= IB_DEVICE_RAW_SCATTER_FCS;
Noa Osheroviche8161332017-01-18 15:40:01 +0200860 props->raw_packet_caps |= IB_RAW_PACKET_CAP_SCATTER_FCS;
861 }
Majd Dibbinycff5a0f2016-04-17 17:19:38 +0300862
Maor Gottliebda6d6ba32016-06-04 15:15:28 +0300863 if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS))
864 props->device_cap_flags |= IB_DEVICE_MANAGED_FLOW_STEERING;
865
Noa Osherovichb1383aa2017-10-29 13:59:45 +0200866 if (MLX5_CAP_GEN(mdev, end_pad))
867 props->device_cap_flags |= IB_DEVICE_PCI_WRITE_END_PADDING;
868
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300869 props->vendor_part_id = mdev->pdev->device;
870 props->hw_ver = mdev->pdev->revision;
Eli Cohene126ba92013-07-07 17:25:49 +0300871
872 props->max_mr_size = ~0ull;
Sagi Grimberge0238a62015-07-21 14:40:12 +0300873 props->page_size_cap = ~(min_page_size - 1);
Saeed Mahameed938fe832015-05-28 22:28:41 +0300874 props->max_qp = 1 << MLX5_CAP_GEN(mdev, log_max_qp);
875 props->max_qp_wr = 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
876 max_rq_sg = MLX5_CAP_GEN(mdev, max_wqe_sz_rq) /
877 sizeof(struct mlx5_wqe_data_seg);
Eli Cohen288c01b2016-10-27 16:36:45 +0300878 max_sq_desc = min_t(int, MLX5_CAP_GEN(mdev, max_wqe_sz_sq), 512);
879 max_sq_sg = (max_sq_desc - sizeof(struct mlx5_wqe_ctrl_seg) -
880 sizeof(struct mlx5_wqe_raddr_seg)) /
881 sizeof(struct mlx5_wqe_data_seg);
Eli Cohene126ba92013-07-07 17:25:49 +0300882 props->max_sge = min(max_rq_sg, max_sq_sg);
Sagi Grimberg986ef952016-03-31 19:03:25 +0300883 props->max_sge_rd = MLX5_MAX_SGE_RD;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300884 props->max_cq = 1 << MLX5_CAP_GEN(mdev, log_max_cq);
Leon Romanovsky9f177682016-01-14 08:11:40 +0200885 props->max_cqe = (1 << MLX5_CAP_GEN(mdev, log_max_cq_sz)) - 1;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300886 props->max_mr = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
887 props->max_pd = 1 << MLX5_CAP_GEN(mdev, log_max_pd);
888 props->max_qp_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_req_qp);
889 props->max_qp_init_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_res_qp);
890 props->max_srq = 1 << MLX5_CAP_GEN(mdev, log_max_srq);
891 props->max_srq_wr = (1 << MLX5_CAP_GEN(mdev, log_max_srq_sz)) - 1;
892 props->local_ca_ack_delay = MLX5_CAP_GEN(mdev, local_ca_ack_delay);
Eli Cohene126ba92013-07-07 17:25:49 +0300893 props->max_res_rd_atom = props->max_qp_rd_atom * props->max_qp;
Eli Cohene126ba92013-07-07 17:25:49 +0300894 props->max_srq_sge = max_rq_sg - 1;
Sagi Grimberg911f4332016-03-03 13:37:51 +0200895 props->max_fast_reg_page_list_len =
896 1 << MLX5_CAP_GEN(mdev, log_max_klm_list_size);
Moni Shoua776a3902018-01-02 16:19:33 +0200897 get_atomic_caps_qp(dev, props);
Eli Cohen81bea282013-09-11 16:35:30 +0300898 props->masked_atomic_cap = IB_ATOMIC_NONE;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300899 props->max_mcast_grp = 1 << MLX5_CAP_GEN(mdev, log_max_mcg);
900 props->max_mcast_qp_attach = MLX5_CAP_GEN(mdev, max_qp_mcg);
Eli Cohene126ba92013-07-07 17:25:49 +0300901 props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
902 props->max_mcast_grp;
903 props->max_map_per_fmr = INT_MAX; /* no limit in ConnectIB */
Maor Gottlieb86695a62016-10-27 16:36:38 +0300904 props->max_ah = INT_MAX;
Matan Barak7c60bcb2015-12-15 20:30:11 +0200905 props->hca_core_clock = MLX5_CAP_GEN(mdev, device_frequency_khz);
906 props->timestamp_mask = 0x7FFFFFFFFFFFFFFFULL;
Eli Cohene126ba92013-07-07 17:25:49 +0300907
Haggai Eran8cdd3122014-12-11 17:04:20 +0200908#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
Saeed Mahameed938fe832015-05-28 22:28:41 +0300909 if (MLX5_CAP_GEN(mdev, pg))
Haggai Eran8cdd3122014-12-11 17:04:20 +0200910 props->device_cap_flags |= IB_DEVICE_ON_DEMAND_PAGING;
911 props->odp_caps = dev->odp_caps;
912#endif
913
Leon Romanovsky051f2632015-12-20 12:16:11 +0200914 if (MLX5_CAP_GEN(mdev, cd))
915 props->device_cap_flags |= IB_DEVICE_CROSS_CHANNEL;
916
Eli Coheneff901d2016-03-11 22:58:42 +0200917 if (!mlx5_core_is_pf(mdev))
918 props->device_cap_flags |= IB_DEVICE_VIRTUAL_FUNCTION;
919
Yishai Hadas31f69a82016-08-28 11:28:45 +0300920 if (mlx5_ib_port_link_layer(ibdev, 1) ==
Daniel Jurgens85c7c012018-01-04 17:25:43 +0200921 IB_LINK_LAYER_ETHERNET && raw_support) {
Yishai Hadas31f69a82016-08-28 11:28:45 +0300922 props->rss_caps.max_rwq_indirection_tables =
923 1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt);
924 props->rss_caps.max_rwq_indirection_table_size =
925 1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt_size);
926 props->rss_caps.supported_qpts = 1 << IB_QPT_RAW_PACKET;
927 props->max_wq_type_rq =
928 1 << MLX5_CAP_GEN(dev->mdev, log_max_rq);
929 }
930
Artemy Kovalyoveb761892017-08-17 15:52:09 +0300931 if (MLX5_CAP_GEN(mdev, tag_matching)) {
Leon Romanovsky78b1beb2017-09-24 21:46:29 +0300932 props->tm_caps.max_rndv_hdr_size = MLX5_TM_MAX_RNDV_MSG_SIZE;
933 props->tm_caps.max_num_tags =
Artemy Kovalyoveb761892017-08-17 15:52:09 +0300934 (1 << MLX5_CAP_GEN(mdev, log_tag_matching_list_sz)) - 1;
Leon Romanovsky78b1beb2017-09-24 21:46:29 +0300935 props->tm_caps.flags = IB_TM_CAP_RC;
936 props->tm_caps.max_ops =
Artemy Kovalyoveb761892017-08-17 15:52:09 +0300937 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
Leon Romanovsky78b1beb2017-09-24 21:46:29 +0300938 props->tm_caps.max_sge = MLX5_TM_MAX_SGE;
Artemy Kovalyoveb761892017-08-17 15:52:09 +0300939 }
940
Yonatan Cohen87ab3f52017-11-13 10:51:18 +0200941 if (MLX5_CAP_GEN(dev->mdev, cq_moderation)) {
942 props->cq_caps.max_cq_moderation_count =
943 MLX5_MAX_CQ_COUNT;
944 props->cq_caps.max_cq_moderation_period =
945 MLX5_MAX_CQ_PERIOD;
946 }
947
Bodong Wang7e43a2a2016-10-31 12:16:44 +0200948 if (field_avail(typeof(resp), cqe_comp_caps, uhw->outlen)) {
949 resp.cqe_comp_caps.max_num =
950 MLX5_CAP_GEN(dev->mdev, cqe_compression) ?
951 MLX5_CAP_GEN(dev->mdev, cqe_compression_max_num) : 0;
952 resp.cqe_comp_caps.supported_format =
953 MLX5_IB_CQE_RES_FORMAT_HASH |
954 MLX5_IB_CQE_RES_FORMAT_CSUM;
955 resp.response_length += sizeof(resp.cqe_comp_caps);
956 }
957
Daniel Jurgens85c7c012018-01-04 17:25:43 +0200958 if (field_avail(typeof(resp), packet_pacing_caps, uhw->outlen) &&
959 raw_support) {
Bodong Wangd9491672016-12-01 13:43:13 +0200960 if (MLX5_CAP_QOS(mdev, packet_pacing) &&
961 MLX5_CAP_GEN(mdev, qos)) {
962 resp.packet_pacing_caps.qp_rate_limit_max =
963 MLX5_CAP_QOS(mdev, packet_pacing_max_rate);
964 resp.packet_pacing_caps.qp_rate_limit_min =
965 MLX5_CAP_QOS(mdev, packet_pacing_min_rate);
966 resp.packet_pacing_caps.supported_qpts |=
967 1 << IB_QPT_RAW_PACKET;
968 }
969 resp.response_length += sizeof(resp.packet_pacing_caps);
970 }
971
Leon Romanovsky9f885202017-01-02 11:37:39 +0200972 if (field_avail(typeof(resp), mlx5_ib_support_multi_pkt_send_wqes,
973 uhw->outlen)) {
Bodong Wang795b6092017-08-17 15:52:34 +0300974 if (MLX5_CAP_ETH(mdev, multi_pkt_send_wqe))
975 resp.mlx5_ib_support_multi_pkt_send_wqes =
976 MLX5_IB_ALLOW_MPW;
Bodong Wang050da902017-08-17 15:52:35 +0300977
978 if (MLX5_CAP_ETH(mdev, enhanced_multi_pkt_send_wqe))
979 resp.mlx5_ib_support_multi_pkt_send_wqes |=
980 MLX5_IB_SUPPORT_EMPW;
981
Leon Romanovsky9f885202017-01-02 11:37:39 +0200982 resp.response_length +=
983 sizeof(resp.mlx5_ib_support_multi_pkt_send_wqes);
984 }
985
Guy Levide57f2a2017-10-19 08:25:52 +0300986 if (field_avail(typeof(resp), flags, uhw->outlen)) {
987 resp.response_length += sizeof(resp.flags);
Guy Levi7a0c8f42017-10-19 08:25:53 +0300988
Guy Levide57f2a2017-10-19 08:25:52 +0300989 if (MLX5_CAP_GEN(mdev, cqe_compression_128))
990 resp.flags |=
991 MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_COMP;
Guy Levi7a0c8f42017-10-19 08:25:53 +0300992
993 if (MLX5_CAP_GEN(mdev, cqe_128_always))
994 resp.flags |= MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_PAD;
Guy Levide57f2a2017-10-19 08:25:52 +0300995 }
Leon Romanovsky9f885202017-01-02 11:37:39 +0200996
Noa Osherovich96dc3fc2017-08-17 15:52:28 +0300997 if (field_avail(typeof(resp), sw_parsing_caps,
998 uhw->outlen)) {
999 resp.response_length += sizeof(resp.sw_parsing_caps);
1000 if (MLX5_CAP_ETH(mdev, swp)) {
1001 resp.sw_parsing_caps.sw_parsing_offloads |=
1002 MLX5_IB_SW_PARSING;
1003
1004 if (MLX5_CAP_ETH(mdev, swp_csum))
1005 resp.sw_parsing_caps.sw_parsing_offloads |=
1006 MLX5_IB_SW_PARSING_CSUM;
1007
1008 if (MLX5_CAP_ETH(mdev, swp_lso))
1009 resp.sw_parsing_caps.sw_parsing_offloads |=
1010 MLX5_IB_SW_PARSING_LSO;
1011
1012 if (resp.sw_parsing_caps.sw_parsing_offloads)
1013 resp.sw_parsing_caps.supported_qpts =
1014 BIT(IB_QPT_RAW_PACKET);
1015 }
1016 }
1017
Daniel Jurgens85c7c012018-01-04 17:25:43 +02001018 if (field_avail(typeof(resp), striding_rq_caps, uhw->outlen) &&
1019 raw_support) {
Noa Osherovichb4f34592017-10-17 18:01:12 +03001020 resp.response_length += sizeof(resp.striding_rq_caps);
1021 if (MLX5_CAP_GEN(mdev, striding_rq)) {
1022 resp.striding_rq_caps.min_single_stride_log_num_of_bytes =
1023 MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES;
1024 resp.striding_rq_caps.max_single_stride_log_num_of_bytes =
1025 MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES;
1026 resp.striding_rq_caps.min_single_wqe_log_num_of_strides =
1027 MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES;
1028 resp.striding_rq_caps.max_single_wqe_log_num_of_strides =
1029 MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES;
1030 resp.striding_rq_caps.supported_qpts =
1031 BIT(IB_QPT_RAW_PACKET);
1032 }
1033 }
1034
Maor Gottliebf95ef6c2017-10-19 08:25:55 +03001035 if (field_avail(typeof(resp), tunnel_offloads_caps,
1036 uhw->outlen)) {
1037 resp.response_length += sizeof(resp.tunnel_offloads_caps);
1038 if (MLX5_CAP_ETH(mdev, tunnel_stateless_vxlan))
1039 resp.tunnel_offloads_caps |=
1040 MLX5_IB_TUNNELED_OFFLOADS_VXLAN;
1041 if (MLX5_CAP_ETH(mdev, tunnel_stateless_geneve_rx))
1042 resp.tunnel_offloads_caps |=
1043 MLX5_IB_TUNNELED_OFFLOADS_GENEVE;
1044 if (MLX5_CAP_ETH(mdev, tunnel_stateless_gre))
1045 resp.tunnel_offloads_caps |=
1046 MLX5_IB_TUNNELED_OFFLOADS_GRE;
1047 }
1048
Bodong Wang402ca532016-06-17 15:02:20 +03001049 if (uhw->outlen) {
1050 err = ib_copy_to_udata(uhw, &resp, resp.response_length);
1051
1052 if (err)
1053 return err;
1054 }
1055
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001056 return 0;
1057}
Eli Cohene126ba92013-07-07 17:25:49 +03001058
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001059enum mlx5_ib_width {
1060 MLX5_IB_WIDTH_1X = 1 << 0,
1061 MLX5_IB_WIDTH_2X = 1 << 1,
1062 MLX5_IB_WIDTH_4X = 1 << 2,
1063 MLX5_IB_WIDTH_8X = 1 << 3,
1064 MLX5_IB_WIDTH_12X = 1 << 4
1065};
1066
1067static int translate_active_width(struct ib_device *ibdev, u8 active_width,
1068 u8 *ib_width)
1069{
1070 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1071 int err = 0;
1072
1073 if (active_width & MLX5_IB_WIDTH_1X) {
1074 *ib_width = IB_WIDTH_1X;
1075 } else if (active_width & MLX5_IB_WIDTH_2X) {
1076 mlx5_ib_dbg(dev, "active_width %d is not supported by IB spec\n",
1077 (int)active_width);
1078 err = -EINVAL;
1079 } else if (active_width & MLX5_IB_WIDTH_4X) {
1080 *ib_width = IB_WIDTH_4X;
1081 } else if (active_width & MLX5_IB_WIDTH_8X) {
1082 *ib_width = IB_WIDTH_8X;
1083 } else if (active_width & MLX5_IB_WIDTH_12X) {
1084 *ib_width = IB_WIDTH_12X;
1085 } else {
1086 mlx5_ib_dbg(dev, "Invalid active_width %d\n",
1087 (int)active_width);
1088 err = -EINVAL;
1089 }
1090
1091 return err;
1092}
1093
1094static int mlx5_mtu_to_ib_mtu(int mtu)
1095{
1096 switch (mtu) {
1097 case 256: return 1;
1098 case 512: return 2;
1099 case 1024: return 3;
1100 case 2048: return 4;
1101 case 4096: return 5;
1102 default:
1103 pr_warn("invalid mtu\n");
1104 return -1;
1105 }
1106}
1107
1108enum ib_max_vl_num {
1109 __IB_MAX_VL_0 = 1,
1110 __IB_MAX_VL_0_1 = 2,
1111 __IB_MAX_VL_0_3 = 3,
1112 __IB_MAX_VL_0_7 = 4,
1113 __IB_MAX_VL_0_14 = 5,
1114};
1115
1116enum mlx5_vl_hw_cap {
1117 MLX5_VL_HW_0 = 1,
1118 MLX5_VL_HW_0_1 = 2,
1119 MLX5_VL_HW_0_2 = 3,
1120 MLX5_VL_HW_0_3 = 4,
1121 MLX5_VL_HW_0_4 = 5,
1122 MLX5_VL_HW_0_5 = 6,
1123 MLX5_VL_HW_0_6 = 7,
1124 MLX5_VL_HW_0_7 = 8,
1125 MLX5_VL_HW_0_14 = 15
1126};
1127
1128static int translate_max_vl_num(struct ib_device *ibdev, u8 vl_hw_cap,
1129 u8 *max_vl_num)
1130{
1131 switch (vl_hw_cap) {
1132 case MLX5_VL_HW_0:
1133 *max_vl_num = __IB_MAX_VL_0;
1134 break;
1135 case MLX5_VL_HW_0_1:
1136 *max_vl_num = __IB_MAX_VL_0_1;
1137 break;
1138 case MLX5_VL_HW_0_3:
1139 *max_vl_num = __IB_MAX_VL_0_3;
1140 break;
1141 case MLX5_VL_HW_0_7:
1142 *max_vl_num = __IB_MAX_VL_0_7;
1143 break;
1144 case MLX5_VL_HW_0_14:
1145 *max_vl_num = __IB_MAX_VL_0_14;
1146 break;
1147
1148 default:
1149 return -EINVAL;
1150 }
1151
1152 return 0;
1153}
1154
1155static int mlx5_query_hca_port(struct ib_device *ibdev, u8 port,
1156 struct ib_port_attr *props)
1157{
1158 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1159 struct mlx5_core_dev *mdev = dev->mdev;
1160 struct mlx5_hca_vport_context *rep;
Saeed Mahameed046339e2016-04-22 00:33:03 +03001161 u16 max_mtu;
1162 u16 oper_mtu;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001163 int err;
1164 u8 ib_link_width_oper;
1165 u8 vl_hw_cap;
1166
1167 rep = kzalloc(sizeof(*rep), GFP_KERNEL);
1168 if (!rep) {
1169 err = -ENOMEM;
1170 goto out;
1171 }
1172
Or Gerlitzc4550c62017-01-24 13:02:39 +02001173 /* props being zeroed by the caller, avoid zeroing it here */
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001174
1175 err = mlx5_query_hca_vport_context(mdev, 0, port, 0, rep);
1176 if (err)
1177 goto out;
1178
1179 props->lid = rep->lid;
1180 props->lmc = rep->lmc;
1181 props->sm_lid = rep->sm_lid;
1182 props->sm_sl = rep->sm_sl;
1183 props->state = rep->vport_state;
1184 props->phys_state = rep->port_physical_state;
1185 props->port_cap_flags = rep->cap_mask1;
1186 props->gid_tbl_len = mlx5_get_gid_table_len(MLX5_CAP_GEN(mdev, gid_table_size));
1187 props->max_msg_sz = 1 << MLX5_CAP_GEN(mdev, log_max_msg);
1188 props->pkey_tbl_len = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, pkey_table_size));
1189 props->bad_pkey_cntr = rep->pkey_violation_counter;
1190 props->qkey_viol_cntr = rep->qkey_violation_counter;
1191 props->subnet_timeout = rep->subnet_timeout;
1192 props->init_type_reply = rep->init_type_reply;
Eli Coheneff901d2016-03-11 22:58:42 +02001193 props->grh_required = rep->grh_required;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001194
1195 err = mlx5_query_port_link_width_oper(mdev, &ib_link_width_oper, port);
1196 if (err)
1197 goto out;
1198
1199 err = translate_active_width(ibdev, ib_link_width_oper,
1200 &props->active_width);
1201 if (err)
1202 goto out;
Noa Osherovichd5beb7f2016-06-02 10:47:53 +03001203 err = mlx5_query_port_ib_proto_oper(mdev, &props->active_speed, port);
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001204 if (err)
1205 goto out;
1206
Saeed Mahameedfacc9692015-06-11 14:47:27 +03001207 mlx5_query_port_max_mtu(mdev, &max_mtu, port);
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001208
1209 props->max_mtu = mlx5_mtu_to_ib_mtu(max_mtu);
1210
Saeed Mahameedfacc9692015-06-11 14:47:27 +03001211 mlx5_query_port_oper_mtu(mdev, &oper_mtu, port);
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001212
1213 props->active_mtu = mlx5_mtu_to_ib_mtu(oper_mtu);
1214
1215 err = mlx5_query_port_vl_hw_cap(mdev, &vl_hw_cap, port);
1216 if (err)
1217 goto out;
1218
1219 err = translate_max_vl_num(ibdev, vl_hw_cap,
1220 &props->max_vl_num);
1221out:
1222 kfree(rep);
Eli Cohene126ba92013-07-07 17:25:49 +03001223 return err;
1224}
1225
1226int mlx5_ib_query_port(struct ib_device *ibdev, u8 port,
1227 struct ib_port_attr *props)
1228{
Ilan Tayari095b0922017-05-14 16:04:30 +03001229 unsigned int count;
1230 int ret;
1231
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001232 switch (mlx5_get_vport_access_method(ibdev)) {
1233 case MLX5_VPORT_ACCESS_METHOD_MAD:
Ilan Tayari095b0922017-05-14 16:04:30 +03001234 ret = mlx5_query_mad_ifc_port(ibdev, port, props);
1235 break;
Eli Cohene126ba92013-07-07 17:25:49 +03001236
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001237 case MLX5_VPORT_ACCESS_METHOD_HCA:
Ilan Tayari095b0922017-05-14 16:04:30 +03001238 ret = mlx5_query_hca_port(ibdev, port, props);
1239 break;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001240
Achiad Shochat3f89a642015-12-23 18:47:21 +02001241 case MLX5_VPORT_ACCESS_METHOD_NIC:
Ilan Tayari095b0922017-05-14 16:04:30 +03001242 ret = mlx5_query_port_roce(ibdev, port, props);
1243 break;
Achiad Shochat3f89a642015-12-23 18:47:21 +02001244
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001245 default:
Ilan Tayari095b0922017-05-14 16:04:30 +03001246 ret = -EINVAL;
Eli Cohene126ba92013-07-07 17:25:49 +03001247 }
Ilan Tayari095b0922017-05-14 16:04:30 +03001248
1249 if (!ret && props) {
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001250 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1251 struct mlx5_core_dev *mdev;
1252 bool put_mdev = true;
1253
1254 mdev = mlx5_ib_get_native_port_mdev(dev, port, NULL);
1255 if (!mdev) {
1256 /* If the port isn't affiliated yet query the master.
1257 * The master and slave will have the same values.
1258 */
1259 mdev = dev->mdev;
1260 port = 1;
1261 put_mdev = false;
1262 }
1263 count = mlx5_core_reserved_gids_count(mdev);
1264 if (put_mdev)
1265 mlx5_ib_put_native_port_mdev(dev, port);
Ilan Tayari095b0922017-05-14 16:04:30 +03001266 props->gid_tbl_len -= count;
1267 }
1268 return ret;
Eli Cohene126ba92013-07-07 17:25:49 +03001269}
1270
1271static int mlx5_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
1272 union ib_gid *gid)
1273{
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001274 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1275 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03001276
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001277 switch (mlx5_get_vport_access_method(ibdev)) {
1278 case MLX5_VPORT_ACCESS_METHOD_MAD:
1279 return mlx5_query_mad_ifc_gids(ibdev, port, index, gid);
Eli Cohene126ba92013-07-07 17:25:49 +03001280
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001281 case MLX5_VPORT_ACCESS_METHOD_HCA:
1282 return mlx5_query_hca_vport_gid(mdev, 0, port, 0, index, gid);
Eli Cohene126ba92013-07-07 17:25:49 +03001283
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001284 default:
1285 return -EINVAL;
1286 }
Eli Cohene126ba92013-07-07 17:25:49 +03001287
Eli Cohene126ba92013-07-07 17:25:49 +03001288}
1289
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001290static int mlx5_query_hca_nic_pkey(struct ib_device *ibdev, u8 port,
1291 u16 index, u16 *pkey)
1292{
1293 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1294 struct mlx5_core_dev *mdev;
1295 bool put_mdev = true;
1296 u8 mdev_port_num;
1297 int err;
1298
1299 mdev = mlx5_ib_get_native_port_mdev(dev, port, &mdev_port_num);
1300 if (!mdev) {
1301 /* The port isn't affiliated yet, get the PKey from the master
1302 * port. For RoCE the PKey tables will be the same.
1303 */
1304 put_mdev = false;
1305 mdev = dev->mdev;
1306 mdev_port_num = 1;
1307 }
1308
1309 err = mlx5_query_hca_vport_pkey(mdev, 0, mdev_port_num, 0,
1310 index, pkey);
1311 if (put_mdev)
1312 mlx5_ib_put_native_port_mdev(dev, port);
1313
1314 return err;
1315}
1316
Eli Cohene126ba92013-07-07 17:25:49 +03001317static int mlx5_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
1318 u16 *pkey)
1319{
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001320 switch (mlx5_get_vport_access_method(ibdev)) {
1321 case MLX5_VPORT_ACCESS_METHOD_MAD:
1322 return mlx5_query_mad_ifc_pkey(ibdev, port, index, pkey);
Eli Cohene126ba92013-07-07 17:25:49 +03001323
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001324 case MLX5_VPORT_ACCESS_METHOD_HCA:
1325 case MLX5_VPORT_ACCESS_METHOD_NIC:
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001326 return mlx5_query_hca_nic_pkey(ibdev, port, index, pkey);
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001327 default:
1328 return -EINVAL;
1329 }
Eli Cohene126ba92013-07-07 17:25:49 +03001330}
1331
Eli Cohene126ba92013-07-07 17:25:49 +03001332static int mlx5_ib_modify_device(struct ib_device *ibdev, int mask,
1333 struct ib_device_modify *props)
1334{
1335 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1336 struct mlx5_reg_node_desc in;
1337 struct mlx5_reg_node_desc out;
1338 int err;
1339
1340 if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
1341 return -EOPNOTSUPP;
1342
1343 if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
1344 return 0;
1345
1346 /*
1347 * If possible, pass node desc to FW, so it can generate
1348 * a 144 trap. If cmd fails, just ignore.
1349 */
Yuval Shaiabd99fde2016-08-25 10:57:07 -07001350 memcpy(&in, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
Jack Morgenstein9603b612014-07-28 23:30:22 +03001351 err = mlx5_core_access_reg(dev->mdev, &in, sizeof(in), &out,
Eli Cohene126ba92013-07-07 17:25:49 +03001352 sizeof(out), MLX5_REG_NODE_DESC, 0, 1);
1353 if (err)
1354 return err;
1355
Yuval Shaiabd99fde2016-08-25 10:57:07 -07001356 memcpy(ibdev->node_desc, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
Eli Cohene126ba92013-07-07 17:25:49 +03001357
1358 return err;
1359}
1360
Eli Cohencdbe33d2017-02-14 07:25:38 +02001361static int set_port_caps_atomic(struct mlx5_ib_dev *dev, u8 port_num, u32 mask,
1362 u32 value)
1363{
1364 struct mlx5_hca_vport_context ctx = {};
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001365 struct mlx5_core_dev *mdev;
1366 u8 mdev_port_num;
Eli Cohencdbe33d2017-02-14 07:25:38 +02001367 int err;
1368
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001369 mdev = mlx5_ib_get_native_port_mdev(dev, port_num, &mdev_port_num);
1370 if (!mdev)
1371 return -ENODEV;
1372
1373 err = mlx5_query_hca_vport_context(mdev, 0, mdev_port_num, 0, &ctx);
Eli Cohencdbe33d2017-02-14 07:25:38 +02001374 if (err)
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001375 goto out;
Eli Cohencdbe33d2017-02-14 07:25:38 +02001376
1377 if (~ctx.cap_mask1_perm & mask) {
1378 mlx5_ib_warn(dev, "trying to change bitmask 0x%X but change supported 0x%X\n",
1379 mask, ctx.cap_mask1_perm);
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001380 err = -EINVAL;
1381 goto out;
Eli Cohencdbe33d2017-02-14 07:25:38 +02001382 }
1383
1384 ctx.cap_mask1 = value;
1385 ctx.cap_mask1_perm = mask;
Daniel Jurgensb3cbd6f2018-01-04 17:25:38 +02001386 err = mlx5_core_modify_hca_vport_context(mdev, 0, mdev_port_num,
1387 0, &ctx);
1388
1389out:
1390 mlx5_ib_put_native_port_mdev(dev, port_num);
Eli Cohencdbe33d2017-02-14 07:25:38 +02001391
1392 return err;
1393}
1394
Eli Cohene126ba92013-07-07 17:25:49 +03001395static int mlx5_ib_modify_port(struct ib_device *ibdev, u8 port, int mask,
1396 struct ib_port_modify *props)
1397{
1398 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1399 struct ib_port_attr attr;
1400 u32 tmp;
1401 int err;
Eli Cohencdbe33d2017-02-14 07:25:38 +02001402 u32 change_mask;
1403 u32 value;
1404 bool is_ib = (mlx5_ib_port_link_layer(ibdev, port) ==
1405 IB_LINK_LAYER_INFINIBAND);
1406
Majd Dibbinyec255872017-08-23 08:35:42 +03001407 /* CM layer calls ib_modify_port() regardless of the link layer. For
1408 * Ethernet ports, qkey violation and Port capabilities are meaningless.
1409 */
1410 if (!is_ib)
1411 return 0;
1412
Eli Cohencdbe33d2017-02-14 07:25:38 +02001413 if (MLX5_CAP_GEN(dev->mdev, ib_virt) && is_ib) {
1414 change_mask = props->clr_port_cap_mask | props->set_port_cap_mask;
1415 value = ~props->clr_port_cap_mask | props->set_port_cap_mask;
1416 return set_port_caps_atomic(dev, port, change_mask, value);
1417 }
Eli Cohene126ba92013-07-07 17:25:49 +03001418
1419 mutex_lock(&dev->cap_mask_mutex);
1420
Or Gerlitzc4550c62017-01-24 13:02:39 +02001421 err = ib_query_port(ibdev, port, &attr);
Eli Cohene126ba92013-07-07 17:25:49 +03001422 if (err)
1423 goto out;
1424
1425 tmp = (attr.port_cap_flags | props->set_port_cap_mask) &
1426 ~props->clr_port_cap_mask;
1427
Jack Morgenstein9603b612014-07-28 23:30:22 +03001428 err = mlx5_set_port_caps(dev->mdev, port, tmp);
Eli Cohene126ba92013-07-07 17:25:49 +03001429
1430out:
1431 mutex_unlock(&dev->cap_mask_mutex);
1432 return err;
1433}
1434
Eli Cohen30aa60b2017-01-03 23:55:27 +02001435static void print_lib_caps(struct mlx5_ib_dev *dev, u64 caps)
1436{
1437 mlx5_ib_dbg(dev, "MLX5_LIB_CAP_4K_UAR = %s\n",
1438 caps & MLX5_LIB_CAP_4K_UAR ? "y" : "n");
1439}
1440
Yishai Hadas31a78a52017-12-24 16:31:34 +02001441static u16 calc_dynamic_bfregs(int uars_per_sys_page)
1442{
1443 /* Large page with non 4k uar support might limit the dynamic size */
1444 if (uars_per_sys_page == 1 && PAGE_SIZE > 4096)
1445 return MLX5_MIN_DYN_BFREGS;
1446
1447 return MLX5_MAX_DYN_BFREGS;
1448}
1449
Eli Cohenb037c292017-01-03 23:55:26 +02001450static int calc_total_bfregs(struct mlx5_ib_dev *dev, bool lib_uar_4k,
1451 struct mlx5_ib_alloc_ucontext_req_v2 *req,
Yishai Hadas31a78a52017-12-24 16:31:34 +02001452 struct mlx5_bfreg_info *bfregi)
Eli Cohenb037c292017-01-03 23:55:26 +02001453{
1454 int uars_per_sys_page;
1455 int bfregs_per_sys_page;
1456 int ref_bfregs = req->total_num_bfregs;
1457
1458 if (req->total_num_bfregs == 0)
1459 return -EINVAL;
1460
1461 BUILD_BUG_ON(MLX5_MAX_BFREGS % MLX5_NON_FP_BFREGS_IN_PAGE);
1462 BUILD_BUG_ON(MLX5_MAX_BFREGS < MLX5_NON_FP_BFREGS_IN_PAGE);
1463
1464 if (req->total_num_bfregs > MLX5_MAX_BFREGS)
1465 return -ENOMEM;
1466
1467 uars_per_sys_page = get_uars_per_sys_page(dev, lib_uar_4k);
1468 bfregs_per_sys_page = uars_per_sys_page * MLX5_NON_FP_BFREGS_PER_UAR;
Yishai Hadas31a78a52017-12-24 16:31:34 +02001469 /* This holds the required static allocation asked by the user */
Eli Cohenb037c292017-01-03 23:55:26 +02001470 req->total_num_bfregs = ALIGN(req->total_num_bfregs, bfregs_per_sys_page);
Eli Cohenb037c292017-01-03 23:55:26 +02001471 if (req->num_low_latency_bfregs > req->total_num_bfregs - 1)
1472 return -EINVAL;
1473
Yishai Hadas31a78a52017-12-24 16:31:34 +02001474 bfregi->num_static_sys_pages = req->total_num_bfregs / bfregs_per_sys_page;
1475 bfregi->num_dyn_bfregs = ALIGN(calc_dynamic_bfregs(uars_per_sys_page), bfregs_per_sys_page);
1476 bfregi->total_num_bfregs = req->total_num_bfregs + bfregi->num_dyn_bfregs;
1477 bfregi->num_sys_pages = bfregi->total_num_bfregs / bfregs_per_sys_page;
1478
1479 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 +02001480 MLX5_CAP_GEN(dev->mdev, uar_4k) ? "yes" : "no",
1481 lib_uar_4k ? "yes" : "no", ref_bfregs,
Yishai Hadas31a78a52017-12-24 16:31:34 +02001482 req->total_num_bfregs, bfregi->total_num_bfregs,
1483 bfregi->num_sys_pages);
Eli Cohenb037c292017-01-03 23:55:26 +02001484
1485 return 0;
1486}
1487
1488static int allocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
1489{
1490 struct mlx5_bfreg_info *bfregi;
1491 int err;
1492 int i;
1493
1494 bfregi = &context->bfregi;
Yishai Hadas31a78a52017-12-24 16:31:34 +02001495 for (i = 0; i < bfregi->num_static_sys_pages; i++) {
Eli Cohenb037c292017-01-03 23:55:26 +02001496 err = mlx5_cmd_alloc_uar(dev->mdev, &bfregi->sys_pages[i]);
1497 if (err)
1498 goto error;
1499
1500 mlx5_ib_dbg(dev, "allocated uar %d\n", bfregi->sys_pages[i]);
1501 }
Yishai Hadas4ed131d2017-12-24 16:31:35 +02001502
1503 for (i = bfregi->num_static_sys_pages; i < bfregi->num_sys_pages; i++)
1504 bfregi->sys_pages[i] = MLX5_IB_INVALID_UAR_INDEX;
1505
Eli Cohenb037c292017-01-03 23:55:26 +02001506 return 0;
1507
1508error:
1509 for (--i; i >= 0; i--)
1510 if (mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]))
1511 mlx5_ib_warn(dev, "failed to free uar %d\n", i);
1512
1513 return err;
1514}
1515
1516static int deallocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
1517{
1518 struct mlx5_bfreg_info *bfregi;
1519 int err;
1520 int i;
1521
1522 bfregi = &context->bfregi;
Yishai Hadas4ed131d2017-12-24 16:31:35 +02001523 for (i = 0; i < bfregi->num_sys_pages; i++) {
1524 if (i < bfregi->num_static_sys_pages ||
1525 bfregi->sys_pages[i] != MLX5_IB_INVALID_UAR_INDEX) {
1526 err = mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]);
1527 if (err) {
1528 mlx5_ib_warn(dev, "failed to free uar %d, err=%d\n", i, err);
1529 return err;
1530 }
Eli Cohenb037c292017-01-03 23:55:26 +02001531 }
1532 }
Yishai Hadas4ed131d2017-12-24 16:31:35 +02001533
Eli Cohenb037c292017-01-03 23:55:26 +02001534 return 0;
1535}
1536
Huy Nguyenc85023e2017-05-30 09:42:54 +03001537static int mlx5_ib_alloc_transport_domain(struct mlx5_ib_dev *dev, u32 *tdn)
1538{
1539 int err;
1540
1541 err = mlx5_core_alloc_transport_domain(dev->mdev, tdn);
1542 if (err)
1543 return err;
1544
1545 if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
1546 !MLX5_CAP_GEN(dev->mdev, disable_local_lb))
1547 return err;
1548
1549 mutex_lock(&dev->lb_mutex);
1550 dev->user_td++;
1551
1552 if (dev->user_td == 2)
1553 err = mlx5_nic_vport_update_local_lb(dev->mdev, true);
1554
1555 mutex_unlock(&dev->lb_mutex);
1556 return err;
1557}
1558
1559static void mlx5_ib_dealloc_transport_domain(struct mlx5_ib_dev *dev, u32 tdn)
1560{
1561 mlx5_core_dealloc_transport_domain(dev->mdev, tdn);
1562
1563 if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
1564 !MLX5_CAP_GEN(dev->mdev, disable_local_lb))
1565 return;
1566
1567 mutex_lock(&dev->lb_mutex);
1568 dev->user_td--;
1569
1570 if (dev->user_td < 2)
1571 mlx5_nic_vport_update_local_lb(dev->mdev, false);
1572
1573 mutex_unlock(&dev->lb_mutex);
1574}
1575
Eli Cohene126ba92013-07-07 17:25:49 +03001576static struct ib_ucontext *mlx5_ib_alloc_ucontext(struct ib_device *ibdev,
1577 struct ib_udata *udata)
1578{
1579 struct mlx5_ib_dev *dev = to_mdev(ibdev);
Matan Barakb368d7c2015-12-15 20:30:12 +02001580 struct mlx5_ib_alloc_ucontext_req_v2 req = {};
1581 struct mlx5_ib_alloc_ucontext_resp resp = {};
Eli Cohene126ba92013-07-07 17:25:49 +03001582 struct mlx5_ib_ucontext *context;
Eli Cohen2f5ff262017-01-03 23:55:21 +02001583 struct mlx5_bfreg_info *bfregi;
Eli Cohen78c0f982014-01-30 13:49:48 +02001584 int ver;
Eli Cohene126ba92013-07-07 17:25:49 +03001585 int err;
Majd Dibbinya168a41c2016-01-28 17:51:47 +02001586 size_t min_req_v2 = offsetof(struct mlx5_ib_alloc_ucontext_req_v2,
1587 max_cqe_version);
Eli Cohenb037c292017-01-03 23:55:26 +02001588 bool lib_uar_4k;
Eli Cohene126ba92013-07-07 17:25:49 +03001589
1590 if (!dev->ib_active)
1591 return ERR_PTR(-EAGAIN);
1592
Amrani, Rame0931112017-06-27 17:04:42 +03001593 if (udata->inlen == sizeof(struct mlx5_ib_alloc_ucontext_req))
Eli Cohen78c0f982014-01-30 13:49:48 +02001594 ver = 0;
Amrani, Rame0931112017-06-27 17:04:42 +03001595 else if (udata->inlen >= min_req_v2)
Eli Cohen78c0f982014-01-30 13:49:48 +02001596 ver = 2;
1597 else
1598 return ERR_PTR(-EINVAL);
1599
Amrani, Rame0931112017-06-27 17:04:42 +03001600 err = ib_copy_from_udata(&req, udata, min(udata->inlen, sizeof(req)));
Eli Cohene126ba92013-07-07 17:25:49 +03001601 if (err)
1602 return ERR_PTR(err);
1603
Matan Barakb368d7c2015-12-15 20:30:12 +02001604 if (req.flags)
Eli Cohen78c0f982014-01-30 13:49:48 +02001605 return ERR_PTR(-EINVAL);
1606
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001607 if (req.comp_mask || req.reserved0 || req.reserved1 || req.reserved2)
Matan Barakb368d7c2015-12-15 20:30:12 +02001608 return ERR_PTR(-EOPNOTSUPP);
1609
Eli Cohen2f5ff262017-01-03 23:55:21 +02001610 req.total_num_bfregs = ALIGN(req.total_num_bfregs,
1611 MLX5_NON_FP_BFREGS_PER_UAR);
1612 if (req.num_low_latency_bfregs > req.total_num_bfregs - 1)
Eli Cohene126ba92013-07-07 17:25:49 +03001613 return ERR_PTR(-EINVAL);
1614
Saeed Mahameed938fe832015-05-28 22:28:41 +03001615 resp.qp_tab_size = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp);
Noa Osherovich2cc6ad52016-06-04 15:15:33 +03001616 if (mlx5_core_is_pf(dev->mdev) && MLX5_CAP_GEN(dev->mdev, bf))
1617 resp.bf_reg_size = 1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size);
Daniel Jurgensb47bd6e2016-10-25 18:36:24 +03001618 resp.cache_line_size = cache_line_size();
Saeed Mahameed938fe832015-05-28 22:28:41 +03001619 resp.max_sq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq);
1620 resp.max_rq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq);
1621 resp.max_send_wqebb = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1622 resp.max_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1623 resp.max_srq_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz);
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001624 resp.cqe_version = min_t(__u8,
1625 (__u8)MLX5_CAP_GEN(dev->mdev, cqe_version),
1626 req.max_cqe_version);
Eli Cohen30aa60b2017-01-03 23:55:27 +02001627 resp.log_uar_size = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
1628 MLX5_ADAPTER_PAGE_SHIFT : PAGE_SHIFT;
1629 resp.num_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
1630 MLX5_CAP_GEN(dev->mdev, num_of_uars_per_page) : 1;
Matan Barakb368d7c2015-12-15 20:30:12 +02001631 resp.response_length = min(offsetof(typeof(resp), response_length) +
1632 sizeof(resp.response_length), udata->outlen);
Eli Cohene126ba92013-07-07 17:25:49 +03001633
1634 context = kzalloc(sizeof(*context), GFP_KERNEL);
1635 if (!context)
1636 return ERR_PTR(-ENOMEM);
1637
Eli Cohen30aa60b2017-01-03 23:55:27 +02001638 lib_uar_4k = req.lib_caps & MLX5_LIB_CAP_4K_UAR;
Eli Cohen2f5ff262017-01-03 23:55:21 +02001639 bfregi = &context->bfregi;
Eli Cohenb037c292017-01-03 23:55:26 +02001640
1641 /* updates req->total_num_bfregs */
Yishai Hadas31a78a52017-12-24 16:31:34 +02001642 err = calc_total_bfregs(dev, lib_uar_4k, &req, bfregi);
Eli Cohenb037c292017-01-03 23:55:26 +02001643 if (err)
1644 goto out_ctx;
1645
Eli Cohen2f5ff262017-01-03 23:55:21 +02001646 mutex_init(&bfregi->lock);
Eli Cohenb037c292017-01-03 23:55:26 +02001647 bfregi->lib_uar_4k = lib_uar_4k;
Yishai Hadas31a78a52017-12-24 16:31:34 +02001648 bfregi->count = kcalloc(bfregi->total_num_bfregs, sizeof(*bfregi->count),
Eli Cohenb037c292017-01-03 23:55:26 +02001649 GFP_KERNEL);
1650 if (!bfregi->count) {
Eli Cohene126ba92013-07-07 17:25:49 +03001651 err = -ENOMEM;
1652 goto out_ctx;
1653 }
1654
Eli Cohenb037c292017-01-03 23:55:26 +02001655 bfregi->sys_pages = kcalloc(bfregi->num_sys_pages,
1656 sizeof(*bfregi->sys_pages),
1657 GFP_KERNEL);
1658 if (!bfregi->sys_pages) {
Eli Cohene126ba92013-07-07 17:25:49 +03001659 err = -ENOMEM;
Eli Cohenb037c292017-01-03 23:55:26 +02001660 goto out_count;
Eli Cohene126ba92013-07-07 17:25:49 +03001661 }
1662
Eli Cohenb037c292017-01-03 23:55:26 +02001663 err = allocate_uars(dev, context);
1664 if (err)
1665 goto out_sys_pages;
Eli Cohene126ba92013-07-07 17:25:49 +03001666
Haggai Eranb4cfe442014-12-11 17:04:26 +02001667#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
1668 context->ibucontext.invalidate_range = &mlx5_ib_invalidate_range;
1669#endif
1670
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +02001671 context->upd_xlt_page = __get_free_page(GFP_KERNEL);
1672 if (!context->upd_xlt_page) {
1673 err = -ENOMEM;
1674 goto out_uars;
1675 }
1676 mutex_init(&context->upd_xlt_page_mutex);
1677
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001678 if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain)) {
Huy Nguyenc85023e2017-05-30 09:42:54 +03001679 err = mlx5_ib_alloc_transport_domain(dev, &context->tdn);
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001680 if (err)
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +02001681 goto out_page;
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001682 }
1683
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001684 INIT_LIST_HEAD(&context->vma_private_list);
Majd Dibbinyad9a3662017-12-24 13:54:56 +02001685 mutex_init(&context->vma_private_list_mutex);
Eli Cohene126ba92013-07-07 17:25:49 +03001686 INIT_LIST_HEAD(&context->db_page_list);
1687 mutex_init(&context->db_page_mutex);
1688
Eli Cohen2f5ff262017-01-03 23:55:21 +02001689 resp.tot_bfregs = req.total_num_bfregs;
Daniel Jurgens508562d2018-01-04 17:25:34 +02001690 resp.num_ports = dev->num_ports;
Matan Barakb368d7c2015-12-15 20:30:12 +02001691
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001692 if (field_avail(typeof(resp), cqe_version, udata->outlen))
1693 resp.response_length += sizeof(resp.cqe_version);
Matan Barakb368d7c2015-12-15 20:30:12 +02001694
Bodong Wang402ca532016-06-17 15:02:20 +03001695 if (field_avail(typeof(resp), cmds_supp_uhw, udata->outlen)) {
Moni Shoua6ad279c52016-11-23 08:23:23 +02001696 resp.cmds_supp_uhw |= MLX5_USER_CMDS_SUPP_UHW_QUERY_DEVICE |
1697 MLX5_USER_CMDS_SUPP_UHW_CREATE_AH;
Bodong Wang402ca532016-06-17 15:02:20 +03001698 resp.response_length += sizeof(resp.cmds_supp_uhw);
1699 }
1700
Or Gerlitz78984892016-11-30 20:33:33 +02001701 if (field_avail(typeof(resp), eth_min_inline, udata->outlen)) {
1702 if (mlx5_ib_port_link_layer(ibdev, 1) == IB_LINK_LAYER_ETHERNET) {
1703 mlx5_query_min_inline(dev->mdev, &resp.eth_min_inline);
1704 resp.eth_min_inline++;
1705 }
1706 resp.response_length += sizeof(resp.eth_min_inline);
1707 }
1708
Noa Osherovichbc5c6ee2016-06-04 15:15:31 +03001709 /*
1710 * We don't want to expose information from the PCI bar that is located
1711 * after 4096 bytes, so if the arch only supports larger pages, let's
1712 * pretend we don't support reading the HCA's core clock. This is also
1713 * forced by mmap function.
1714 */
Eli Cohende8d6e02017-01-03 23:55:19 +02001715 if (field_avail(typeof(resp), hca_core_clock_offset, udata->outlen)) {
1716 if (PAGE_SIZE <= 4096) {
1717 resp.comp_mask |=
1718 MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_CORE_CLOCK_OFFSET;
1719 resp.hca_core_clock_offset =
1720 offsetof(struct mlx5_init_seg, internal_timer_h) % PAGE_SIZE;
1721 }
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001722 resp.response_length += sizeof(resp.hca_core_clock_offset) +
Bodong Wang402ca532016-06-17 15:02:20 +03001723 sizeof(resp.reserved2);
Matan Barakb368d7c2015-12-15 20:30:12 +02001724 }
1725
Eli Cohen30aa60b2017-01-03 23:55:27 +02001726 if (field_avail(typeof(resp), log_uar_size, udata->outlen))
1727 resp.response_length += sizeof(resp.log_uar_size);
1728
1729 if (field_avail(typeof(resp), num_uars_per_page, udata->outlen))
1730 resp.response_length += sizeof(resp.num_uars_per_page);
1731
Yishai Hadas31a78a52017-12-24 16:31:34 +02001732 if (field_avail(typeof(resp), num_dyn_bfregs, udata->outlen)) {
1733 resp.num_dyn_bfregs = bfregi->num_dyn_bfregs;
1734 resp.response_length += sizeof(resp.num_dyn_bfregs);
1735 }
1736
Matan Barakb368d7c2015-12-15 20:30:12 +02001737 err = ib_copy_to_udata(udata, &resp, resp.response_length);
Eli Cohene126ba92013-07-07 17:25:49 +03001738 if (err)
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001739 goto out_td;
Eli Cohene126ba92013-07-07 17:25:49 +03001740
Eli Cohen2f5ff262017-01-03 23:55:21 +02001741 bfregi->ver = ver;
1742 bfregi->num_low_latency_bfregs = req.num_low_latency_bfregs;
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001743 context->cqe_version = resp.cqe_version;
Eli Cohen30aa60b2017-01-03 23:55:27 +02001744 context->lib_caps = req.lib_caps;
1745 print_lib_caps(dev, context->lib_caps);
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001746
Eli Cohene126ba92013-07-07 17:25:49 +03001747 return &context->ibucontext;
1748
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001749out_td:
1750 if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
Huy Nguyenc85023e2017-05-30 09:42:54 +03001751 mlx5_ib_dealloc_transport_domain(dev, context->tdn);
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001752
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +02001753out_page:
1754 free_page(context->upd_xlt_page);
1755
Eli Cohene126ba92013-07-07 17:25:49 +03001756out_uars:
Eli Cohenb037c292017-01-03 23:55:26 +02001757 deallocate_uars(dev, context);
1758
1759out_sys_pages:
1760 kfree(bfregi->sys_pages);
1761
Eli Cohene126ba92013-07-07 17:25:49 +03001762out_count:
Eli Cohen2f5ff262017-01-03 23:55:21 +02001763 kfree(bfregi->count);
Eli Cohene126ba92013-07-07 17:25:49 +03001764
Eli Cohene126ba92013-07-07 17:25:49 +03001765out_ctx:
1766 kfree(context);
Eli Cohenb037c292017-01-03 23:55:26 +02001767
Eli Cohene126ba92013-07-07 17:25:49 +03001768 return ERR_PTR(err);
1769}
1770
1771static int mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
1772{
1773 struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1774 struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
Eli Cohenb037c292017-01-03 23:55:26 +02001775 struct mlx5_bfreg_info *bfregi;
Eli Cohene126ba92013-07-07 17:25:49 +03001776
Eli Cohenb037c292017-01-03 23:55:26 +02001777 bfregi = &context->bfregi;
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001778 if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
Huy Nguyenc85023e2017-05-30 09:42:54 +03001779 mlx5_ib_dealloc_transport_domain(dev, context->tdn);
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001780
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +02001781 free_page(context->upd_xlt_page);
Eli Cohenb037c292017-01-03 23:55:26 +02001782 deallocate_uars(dev, context);
1783 kfree(bfregi->sys_pages);
Eli Cohen2f5ff262017-01-03 23:55:21 +02001784 kfree(bfregi->count);
Eli Cohene126ba92013-07-07 17:25:49 +03001785 kfree(context);
1786
1787 return 0;
1788}
1789
Eli Cohenb037c292017-01-03 23:55:26 +02001790static phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev,
Yishai Hadas4ed131d2017-12-24 16:31:35 +02001791 int uar_idx)
Eli Cohene126ba92013-07-07 17:25:49 +03001792{
Eli Cohenb037c292017-01-03 23:55:26 +02001793 int fw_uars_per_page;
1794
1795 fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ? MLX5_UARS_IN_PAGE : 1;
1796
Yishai Hadas4ed131d2017-12-24 16:31:35 +02001797 return (pci_resource_start(dev->mdev->pdev, 0) >> PAGE_SHIFT) + uar_idx / fw_uars_per_page;
Eli Cohene126ba92013-07-07 17:25:49 +03001798}
1799
1800static int get_command(unsigned long offset)
1801{
1802 return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK;
1803}
1804
1805static int get_arg(unsigned long offset)
1806{
1807 return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1);
1808}
1809
1810static int get_index(unsigned long offset)
1811{
1812 return get_arg(offset);
1813}
1814
Yishai Hadas4ed131d2017-12-24 16:31:35 +02001815/* Index resides in an extra byte to enable larger values than 255 */
1816static int get_extended_index(unsigned long offset)
1817{
1818 return get_arg(offset) | ((offset >> 16) & 0xff) << 8;
1819}
1820
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001821static void mlx5_ib_vma_open(struct vm_area_struct *area)
1822{
1823 /* vma_open is called when a new VMA is created on top of our VMA. This
1824 * is done through either mremap flow or split_vma (usually due to
1825 * mlock, madvise, munmap, etc.) We do not support a clone of the VMA,
1826 * as this VMA is strongly hardware related. Therefore we set the
1827 * vm_ops of the newly created/cloned VMA to NULL, to prevent it from
1828 * calling us again and trying to do incorrect actions. We assume that
1829 * the original VMA size is exactly a single page, and therefore all
1830 * "splitting" operation will not happen to it.
1831 */
1832 area->vm_ops = NULL;
1833}
1834
1835static void mlx5_ib_vma_close(struct vm_area_struct *area)
1836{
1837 struct mlx5_ib_vma_private_data *mlx5_ib_vma_priv_data;
1838
1839 /* It's guaranteed that all VMAs opened on a FD are closed before the
1840 * file itself is closed, therefore no sync is needed with the regular
1841 * closing flow. (e.g. mlx5 ib_dealloc_ucontext)
1842 * However need a sync with accessing the vma as part of
1843 * mlx5_ib_disassociate_ucontext.
1844 * The close operation is usually called under mm->mmap_sem except when
1845 * process is exiting.
1846 * The exiting case is handled explicitly as part of
1847 * mlx5_ib_disassociate_ucontext.
1848 */
1849 mlx5_ib_vma_priv_data = (struct mlx5_ib_vma_private_data *)area->vm_private_data;
1850
1851 /* setting the vma context pointer to null in the mlx5_ib driver's
1852 * private data, to protect a race condition in
1853 * mlx5_ib_disassociate_ucontext().
1854 */
1855 mlx5_ib_vma_priv_data->vma = NULL;
Majd Dibbinyad9a3662017-12-24 13:54:56 +02001856 mutex_lock(mlx5_ib_vma_priv_data->vma_private_list_mutex);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001857 list_del(&mlx5_ib_vma_priv_data->list);
Majd Dibbinyad9a3662017-12-24 13:54:56 +02001858 mutex_unlock(mlx5_ib_vma_priv_data->vma_private_list_mutex);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001859 kfree(mlx5_ib_vma_priv_data);
1860}
1861
1862static const struct vm_operations_struct mlx5_ib_vm_ops = {
1863 .open = mlx5_ib_vma_open,
1864 .close = mlx5_ib_vma_close
1865};
1866
1867static int mlx5_ib_set_vma_data(struct vm_area_struct *vma,
1868 struct mlx5_ib_ucontext *ctx)
1869{
1870 struct mlx5_ib_vma_private_data *vma_prv;
1871 struct list_head *vma_head = &ctx->vma_private_list;
1872
1873 vma_prv = kzalloc(sizeof(*vma_prv), GFP_KERNEL);
1874 if (!vma_prv)
1875 return -ENOMEM;
1876
1877 vma_prv->vma = vma;
Majd Dibbinyad9a3662017-12-24 13:54:56 +02001878 vma_prv->vma_private_list_mutex = &ctx->vma_private_list_mutex;
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001879 vma->vm_private_data = vma_prv;
1880 vma->vm_ops = &mlx5_ib_vm_ops;
1881
Majd Dibbinyad9a3662017-12-24 13:54:56 +02001882 mutex_lock(&ctx->vma_private_list_mutex);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001883 list_add(&vma_prv->list, vma_head);
Majd Dibbinyad9a3662017-12-24 13:54:56 +02001884 mutex_unlock(&ctx->vma_private_list_mutex);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001885
1886 return 0;
1887}
1888
1889static void mlx5_ib_disassociate_ucontext(struct ib_ucontext *ibcontext)
1890{
1891 int ret;
1892 struct vm_area_struct *vma;
1893 struct mlx5_ib_vma_private_data *vma_private, *n;
1894 struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1895 struct task_struct *owning_process = NULL;
1896 struct mm_struct *owning_mm = NULL;
1897
1898 owning_process = get_pid_task(ibcontext->tgid, PIDTYPE_PID);
1899 if (!owning_process)
1900 return;
1901
1902 owning_mm = get_task_mm(owning_process);
1903 if (!owning_mm) {
1904 pr_info("no mm, disassociate ucontext is pending task termination\n");
1905 while (1) {
1906 put_task_struct(owning_process);
1907 usleep_range(1000, 2000);
1908 owning_process = get_pid_task(ibcontext->tgid,
1909 PIDTYPE_PID);
1910 if (!owning_process ||
1911 owning_process->state == TASK_DEAD) {
1912 pr_info("disassociate ucontext done, task was terminated\n");
1913 /* in case task was dead need to release the
1914 * task struct.
1915 */
1916 if (owning_process)
1917 put_task_struct(owning_process);
1918 return;
1919 }
1920 }
1921 }
1922
1923 /* need to protect from a race on closing the vma as part of
1924 * mlx5_ib_vma_close.
1925 */
Maor Gottliebecc7d832017-03-29 06:03:02 +03001926 down_write(&owning_mm->mmap_sem);
Majd Dibbinyad9a3662017-12-24 13:54:56 +02001927 mutex_lock(&context->vma_private_list_mutex);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001928 list_for_each_entry_safe(vma_private, n, &context->vma_private_list,
1929 list) {
1930 vma = vma_private->vma;
1931 ret = zap_vma_ptes(vma, vma->vm_start,
1932 PAGE_SIZE);
1933 WARN_ONCE(ret, "%s: zap_vma_ptes failed", __func__);
1934 /* context going to be destroyed, should
1935 * not access ops any more.
1936 */
Maor Gottlieb13776612017-03-29 06:03:03 +03001937 vma->vm_flags &= ~(VM_SHARED | VM_MAYSHARE);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001938 vma->vm_ops = NULL;
1939 list_del(&vma_private->list);
1940 kfree(vma_private);
1941 }
Majd Dibbinyad9a3662017-12-24 13:54:56 +02001942 mutex_unlock(&context->vma_private_list_mutex);
Maor Gottliebecc7d832017-03-29 06:03:02 +03001943 up_write(&owning_mm->mmap_sem);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001944 mmput(owning_mm);
1945 put_task_struct(owning_process);
1946}
1947
Guy Levi37aa5c32016-04-27 16:49:50 +03001948static inline char *mmap_cmd2str(enum mlx5_ib_mmap_cmd cmd)
1949{
1950 switch (cmd) {
1951 case MLX5_IB_MMAP_WC_PAGE:
1952 return "WC";
1953 case MLX5_IB_MMAP_REGULAR_PAGE:
1954 return "best effort WC";
1955 case MLX5_IB_MMAP_NC_PAGE:
1956 return "NC";
1957 default:
1958 return NULL;
1959 }
1960}
1961
1962static int uar_mmap(struct mlx5_ib_dev *dev, enum mlx5_ib_mmap_cmd cmd,
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001963 struct vm_area_struct *vma,
1964 struct mlx5_ib_ucontext *context)
Guy Levi37aa5c32016-04-27 16:49:50 +03001965{
Eli Cohen2f5ff262017-01-03 23:55:21 +02001966 struct mlx5_bfreg_info *bfregi = &context->bfregi;
Guy Levi37aa5c32016-04-27 16:49:50 +03001967 int err;
1968 unsigned long idx;
1969 phys_addr_t pfn, pa;
1970 pgprot_t prot;
Yishai Hadas4ed131d2017-12-24 16:31:35 +02001971 u32 bfreg_dyn_idx = 0;
1972 u32 uar_index;
1973 int dyn_uar = (cmd == MLX5_IB_MMAP_ALLOC_WC);
1974 int max_valid_idx = dyn_uar ? bfregi->num_sys_pages :
1975 bfregi->num_static_sys_pages;
Eli Cohenb037c292017-01-03 23:55:26 +02001976
1977 if (vma->vm_end - vma->vm_start != PAGE_SIZE)
1978 return -EINVAL;
1979
Yishai Hadas4ed131d2017-12-24 16:31:35 +02001980 if (dyn_uar)
1981 idx = get_extended_index(vma->vm_pgoff) + bfregi->num_static_sys_pages;
1982 else
1983 idx = get_index(vma->vm_pgoff);
1984
1985 if (idx >= max_valid_idx) {
1986 mlx5_ib_warn(dev, "invalid uar index %lu, max=%d\n",
1987 idx, max_valid_idx);
Eli Cohenb037c292017-01-03 23:55:26 +02001988 return -EINVAL;
1989 }
Guy Levi37aa5c32016-04-27 16:49:50 +03001990
1991 switch (cmd) {
1992 case MLX5_IB_MMAP_WC_PAGE:
Yishai Hadas4ed131d2017-12-24 16:31:35 +02001993 case MLX5_IB_MMAP_ALLOC_WC:
Guy Levi37aa5c32016-04-27 16:49:50 +03001994/* Some architectures don't support WC memory */
1995#if defined(CONFIG_X86)
1996 if (!pat_enabled())
1997 return -EPERM;
1998#elif !(defined(CONFIG_PPC) || (defined(CONFIG_ARM) && defined(CONFIG_MMU)))
1999 return -EPERM;
2000#endif
2001 /* fall through */
2002 case MLX5_IB_MMAP_REGULAR_PAGE:
2003 /* For MLX5_IB_MMAP_REGULAR_PAGE do the best effort to get WC */
2004 prot = pgprot_writecombine(vma->vm_page_prot);
2005 break;
2006 case MLX5_IB_MMAP_NC_PAGE:
2007 prot = pgprot_noncached(vma->vm_page_prot);
2008 break;
2009 default:
2010 return -EINVAL;
2011 }
2012
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002013 if (dyn_uar) {
2014 int uars_per_page;
2015
2016 uars_per_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k);
2017 bfreg_dyn_idx = idx * (uars_per_page * MLX5_NON_FP_BFREGS_PER_UAR);
2018 if (bfreg_dyn_idx >= bfregi->total_num_bfregs) {
2019 mlx5_ib_warn(dev, "invalid bfreg_dyn_idx %u, max=%u\n",
2020 bfreg_dyn_idx, bfregi->total_num_bfregs);
2021 return -EINVAL;
2022 }
2023
2024 mutex_lock(&bfregi->lock);
2025 /* Fail if uar already allocated, first bfreg index of each
2026 * page holds its count.
2027 */
2028 if (bfregi->count[bfreg_dyn_idx]) {
2029 mlx5_ib_warn(dev, "wrong offset, idx %lu is busy, bfregn=%u\n", idx, bfreg_dyn_idx);
2030 mutex_unlock(&bfregi->lock);
2031 return -EINVAL;
2032 }
2033
2034 bfregi->count[bfreg_dyn_idx]++;
2035 mutex_unlock(&bfregi->lock);
2036
2037 err = mlx5_cmd_alloc_uar(dev->mdev, &uar_index);
2038 if (err) {
2039 mlx5_ib_warn(dev, "UAR alloc failed\n");
2040 goto free_bfreg;
2041 }
2042 } else {
2043 uar_index = bfregi->sys_pages[idx];
2044 }
2045
2046 pfn = uar_index2pfn(dev, uar_index);
Guy Levi37aa5c32016-04-27 16:49:50 +03002047 mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn %pa\n", idx, &pfn);
2048
2049 vma->vm_page_prot = prot;
2050 err = io_remap_pfn_range(vma, vma->vm_start, pfn,
2051 PAGE_SIZE, vma->vm_page_prot);
2052 if (err) {
2053 mlx5_ib_err(dev, "io_remap_pfn_range failed with error=%d, vm_start=0x%lx, pfn=%pa, mmap_cmd=%s\n",
2054 err, vma->vm_start, &pfn, mmap_cmd2str(cmd));
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002055 err = -EAGAIN;
2056 goto err;
Guy Levi37aa5c32016-04-27 16:49:50 +03002057 }
2058
2059 pa = pfn << PAGE_SHIFT;
2060 mlx5_ib_dbg(dev, "mapped %s at 0x%lx, PA %pa\n", mmap_cmd2str(cmd),
2061 vma->vm_start, &pa);
2062
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002063 err = mlx5_ib_set_vma_data(vma, context);
2064 if (err)
2065 goto err;
2066
2067 if (dyn_uar)
2068 bfregi->sys_pages[idx] = uar_index;
2069 return 0;
2070
2071err:
2072 if (!dyn_uar)
2073 return err;
2074
2075 mlx5_cmd_free_uar(dev->mdev, idx);
2076
2077free_bfreg:
2078 mlx5_ib_free_bfreg(dev, bfregi, bfreg_dyn_idx);
2079
2080 return err;
Guy Levi37aa5c32016-04-27 16:49:50 +03002081}
2082
Eli Cohene126ba92013-07-07 17:25:49 +03002083static int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma)
2084{
2085 struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
2086 struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
Eli Cohene126ba92013-07-07 17:25:49 +03002087 unsigned long command;
Eli Cohene126ba92013-07-07 17:25:49 +03002088 phys_addr_t pfn;
2089
2090 command = get_command(vma->vm_pgoff);
2091 switch (command) {
Guy Levi37aa5c32016-04-27 16:49:50 +03002092 case MLX5_IB_MMAP_WC_PAGE:
2093 case MLX5_IB_MMAP_NC_PAGE:
Eli Cohene126ba92013-07-07 17:25:49 +03002094 case MLX5_IB_MMAP_REGULAR_PAGE:
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002095 case MLX5_IB_MMAP_ALLOC_WC:
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03002096 return uar_mmap(dev, command, vma, context);
Eli Cohene126ba92013-07-07 17:25:49 +03002097
2098 case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES:
2099 return -ENOSYS;
2100
Matan Barakd69e3bc2015-12-15 20:30:13 +02002101 case MLX5_IB_MMAP_CORE_CLOCK:
Matan Barakd69e3bc2015-12-15 20:30:13 +02002102 if (vma->vm_end - vma->vm_start != PAGE_SIZE)
2103 return -EINVAL;
2104
Matan Barak6cbac1e2016-04-14 16:52:10 +03002105 if (vma->vm_flags & VM_WRITE)
Matan Barakd69e3bc2015-12-15 20:30:13 +02002106 return -EPERM;
2107
2108 /* Don't expose to user-space information it shouldn't have */
2109 if (PAGE_SIZE > 4096)
2110 return -EOPNOTSUPP;
2111
2112 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
2113 pfn = (dev->mdev->iseg_base +
2114 offsetof(struct mlx5_init_seg, internal_timer_h)) >>
2115 PAGE_SHIFT;
2116 if (io_remap_pfn_range(vma, vma->vm_start, pfn,
2117 PAGE_SIZE, vma->vm_page_prot))
2118 return -EAGAIN;
2119
2120 mlx5_ib_dbg(dev, "mapped internal timer at 0x%lx, PA 0x%llx\n",
2121 vma->vm_start,
2122 (unsigned long long)pfn << PAGE_SHIFT);
2123 break;
Matan Barakd69e3bc2015-12-15 20:30:13 +02002124
Eli Cohene126ba92013-07-07 17:25:49 +03002125 default:
2126 return -EINVAL;
2127 }
2128
2129 return 0;
2130}
2131
Eli Cohene126ba92013-07-07 17:25:49 +03002132static struct ib_pd *mlx5_ib_alloc_pd(struct ib_device *ibdev,
2133 struct ib_ucontext *context,
2134 struct ib_udata *udata)
2135{
2136 struct mlx5_ib_alloc_pd_resp resp;
2137 struct mlx5_ib_pd *pd;
2138 int err;
2139
2140 pd = kmalloc(sizeof(*pd), GFP_KERNEL);
2141 if (!pd)
2142 return ERR_PTR(-ENOMEM);
2143
Jack Morgenstein9603b612014-07-28 23:30:22 +03002144 err = mlx5_core_alloc_pd(to_mdev(ibdev)->mdev, &pd->pdn);
Eli Cohene126ba92013-07-07 17:25:49 +03002145 if (err) {
2146 kfree(pd);
2147 return ERR_PTR(err);
2148 }
2149
2150 if (context) {
2151 resp.pdn = pd->pdn;
2152 if (ib_copy_to_udata(udata, &resp, sizeof(resp))) {
Jack Morgenstein9603b612014-07-28 23:30:22 +03002153 mlx5_core_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn);
Eli Cohene126ba92013-07-07 17:25:49 +03002154 kfree(pd);
2155 return ERR_PTR(-EFAULT);
2156 }
Eli Cohene126ba92013-07-07 17:25:49 +03002157 }
2158
2159 return &pd->ibpd;
2160}
2161
2162static int mlx5_ib_dealloc_pd(struct ib_pd *pd)
2163{
2164 struct mlx5_ib_dev *mdev = to_mdev(pd->device);
2165 struct mlx5_ib_pd *mpd = to_mpd(pd);
2166
Jack Morgenstein9603b612014-07-28 23:30:22 +03002167 mlx5_core_dealloc_pd(mdev->mdev, mpd->pdn);
Eli Cohene126ba92013-07-07 17:25:49 +03002168 kfree(mpd);
2169
2170 return 0;
2171}
2172
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002173enum {
2174 MATCH_CRITERIA_ENABLE_OUTER_BIT,
2175 MATCH_CRITERIA_ENABLE_MISC_BIT,
2176 MATCH_CRITERIA_ENABLE_INNER_BIT
2177};
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002178
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002179#define HEADER_IS_ZERO(match_criteria, headers) \
2180 !(memchr_inv(MLX5_ADDR_OF(fte_match_param, match_criteria, headers), \
2181 0, MLX5_FLD_SZ_BYTES(fte_match_param, headers))) \
2182
2183static u8 get_match_criteria_enable(u32 *match_criteria)
2184{
2185 u8 match_criteria_enable;
2186
2187 match_criteria_enable =
2188 (!HEADER_IS_ZERO(match_criteria, outer_headers)) <<
2189 MATCH_CRITERIA_ENABLE_OUTER_BIT;
2190 match_criteria_enable |=
2191 (!HEADER_IS_ZERO(match_criteria, misc_parameters)) <<
2192 MATCH_CRITERIA_ENABLE_MISC_BIT;
2193 match_criteria_enable |=
2194 (!HEADER_IS_ZERO(match_criteria, inner_headers)) <<
2195 MATCH_CRITERIA_ENABLE_INNER_BIT;
2196
2197 return match_criteria_enable;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002198}
2199
Maor Gottliebca0d4752016-08-30 16:58:35 +03002200static void set_proto(void *outer_c, void *outer_v, u8 mask, u8 val)
2201{
2202 MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_protocol, mask);
2203 MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_protocol, val);
2204}
2205
Moses Reuben2d1e6972016-11-14 19:04:52 +02002206static void set_flow_label(void *misc_c, void *misc_v, u8 mask, u8 val,
2207 bool inner)
2208{
2209 if (inner) {
2210 MLX5_SET(fte_match_set_misc,
2211 misc_c, inner_ipv6_flow_label, mask);
2212 MLX5_SET(fte_match_set_misc,
2213 misc_v, inner_ipv6_flow_label, val);
2214 } else {
2215 MLX5_SET(fte_match_set_misc,
2216 misc_c, outer_ipv6_flow_label, mask);
2217 MLX5_SET(fte_match_set_misc,
2218 misc_v, outer_ipv6_flow_label, val);
2219 }
2220}
2221
Maor Gottliebca0d4752016-08-30 16:58:35 +03002222static void set_tos(void *outer_c, void *outer_v, u8 mask, u8 val)
2223{
2224 MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_ecn, mask);
2225 MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_ecn, val);
2226 MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_dscp, mask >> 2);
2227 MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_dscp, val >> 2);
2228}
2229
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002230#define LAST_ETH_FIELD vlan_tag
2231#define LAST_IB_FIELD sl
Maor Gottliebca0d4752016-08-30 16:58:35 +03002232#define LAST_IPV4_FIELD tos
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002233#define LAST_IPV6_FIELD traffic_class
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002234#define LAST_TCP_UDP_FIELD src_port
Moses Reubenffb30d82016-11-14 19:04:50 +02002235#define LAST_TUNNEL_FIELD tunnel_id
Moses Reuben2ac693f2017-01-18 14:59:50 +02002236#define LAST_FLOW_TAG_FIELD tag_id
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002237#define LAST_DROP_FIELD size
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002238
2239/* Field is the last supported field */
2240#define FIELDS_NOT_SUPPORTED(filter, field)\
2241 memchr_inv((void *)&filter.field +\
2242 sizeof(filter.field), 0,\
2243 sizeof(filter) -\
2244 offsetof(typeof(filter), field) -\
2245 sizeof(filter.field))
2246
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002247#define IPV4_VERSION 4
2248#define IPV6_VERSION 6
2249static int parse_flow_attr(struct mlx5_core_dev *mdev, u32 *match_c,
2250 u32 *match_v, const union ib_flow_spec *ib_spec,
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002251 u32 *tag_id, bool *is_drop)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002252{
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002253 void *misc_params_c = MLX5_ADDR_OF(fte_match_param, match_c,
2254 misc_parameters);
2255 void *misc_params_v = MLX5_ADDR_OF(fte_match_param, match_v,
2256 misc_parameters);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002257 void *headers_c;
2258 void *headers_v;
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002259 int match_ipv;
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002260
Moses Reuben2d1e6972016-11-14 19:04:52 +02002261 if (ib_spec->type & IB_FLOW_SPEC_INNER) {
2262 headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
2263 inner_headers);
2264 headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
2265 inner_headers);
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002266 match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2267 ft_field_support.inner_ip_version);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002268 } else {
2269 headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
2270 outer_headers);
2271 headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
2272 outer_headers);
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002273 match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2274 ft_field_support.outer_ip_version);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002275 }
2276
2277 switch (ib_spec->type & ~IB_FLOW_SPEC_INNER) {
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002278 case IB_FLOW_SPEC_ETH:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002279 if (FIELDS_NOT_SUPPORTED(ib_spec->eth.mask, LAST_ETH_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002280 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002281
Moses Reuben2d1e6972016-11-14 19:04:52 +02002282 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002283 dmac_47_16),
2284 ib_spec->eth.mask.dst_mac);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002285 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002286 dmac_47_16),
2287 ib_spec->eth.val.dst_mac);
2288
Moses Reuben2d1e6972016-11-14 19:04:52 +02002289 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottliebee3da802016-09-12 19:16:24 +03002290 smac_47_16),
2291 ib_spec->eth.mask.src_mac);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002292 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottliebee3da802016-09-12 19:16:24 +03002293 smac_47_16),
2294 ib_spec->eth.val.src_mac);
2295
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002296 if (ib_spec->eth.mask.vlan_tag) {
Moses Reuben2d1e6972016-11-14 19:04:52 +02002297 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Mohamad Haj Yahia10543362016-10-09 16:25:43 +03002298 cvlan_tag, 1);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002299 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Mohamad Haj Yahia10543362016-10-09 16:25:43 +03002300 cvlan_tag, 1);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002301
Moses Reuben2d1e6972016-11-14 19:04:52 +02002302 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002303 first_vid, ntohs(ib_spec->eth.mask.vlan_tag));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002304 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002305 first_vid, ntohs(ib_spec->eth.val.vlan_tag));
2306
Moses Reuben2d1e6972016-11-14 19:04:52 +02002307 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002308 first_cfi,
2309 ntohs(ib_spec->eth.mask.vlan_tag) >> 12);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002310 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002311 first_cfi,
2312 ntohs(ib_spec->eth.val.vlan_tag) >> 12);
2313
Moses Reuben2d1e6972016-11-14 19:04:52 +02002314 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002315 first_prio,
2316 ntohs(ib_spec->eth.mask.vlan_tag) >> 13);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002317 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002318 first_prio,
2319 ntohs(ib_spec->eth.val.vlan_tag) >> 13);
2320 }
Moses Reuben2d1e6972016-11-14 19:04:52 +02002321 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002322 ethertype, ntohs(ib_spec->eth.mask.ether_type));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002323 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002324 ethertype, ntohs(ib_spec->eth.val.ether_type));
2325 break;
2326 case IB_FLOW_SPEC_IPV4:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002327 if (FIELDS_NOT_SUPPORTED(ib_spec->ipv4.mask, LAST_IPV4_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002328 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002329
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002330 if (match_ipv) {
2331 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2332 ip_version, 0xf);
2333 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2334 ip_version, IPV4_VERSION);
2335 } else {
2336 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2337 ethertype, 0xffff);
2338 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2339 ethertype, ETH_P_IP);
2340 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002341
Moses Reuben2d1e6972016-11-14 19:04:52 +02002342 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002343 src_ipv4_src_ipv6.ipv4_layout.ipv4),
2344 &ib_spec->ipv4.mask.src_ip,
2345 sizeof(ib_spec->ipv4.mask.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002346 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002347 src_ipv4_src_ipv6.ipv4_layout.ipv4),
2348 &ib_spec->ipv4.val.src_ip,
2349 sizeof(ib_spec->ipv4.val.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002350 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002351 dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2352 &ib_spec->ipv4.mask.dst_ip,
2353 sizeof(ib_spec->ipv4.mask.dst_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002354 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002355 dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2356 &ib_spec->ipv4.val.dst_ip,
2357 sizeof(ib_spec->ipv4.val.dst_ip));
Maor Gottliebca0d4752016-08-30 16:58:35 +03002358
Moses Reuben2d1e6972016-11-14 19:04:52 +02002359 set_tos(headers_c, headers_v,
Maor Gottliebca0d4752016-08-30 16:58:35 +03002360 ib_spec->ipv4.mask.tos, ib_spec->ipv4.val.tos);
2361
Moses Reuben2d1e6972016-11-14 19:04:52 +02002362 set_proto(headers_c, headers_v,
Maor Gottliebca0d4752016-08-30 16:58:35 +03002363 ib_spec->ipv4.mask.proto, ib_spec->ipv4.val.proto);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002364 break;
Maor Gottlieb026bae02016-06-17 15:14:51 +03002365 case IB_FLOW_SPEC_IPV6:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002366 if (FIELDS_NOT_SUPPORTED(ib_spec->ipv6.mask, LAST_IPV6_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002367 return -EOPNOTSUPP;
Maor Gottlieb026bae02016-06-17 15:14:51 +03002368
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002369 if (match_ipv) {
2370 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2371 ip_version, 0xf);
2372 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2373 ip_version, IPV6_VERSION);
2374 } else {
2375 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2376 ethertype, 0xffff);
2377 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2378 ethertype, ETH_P_IPV6);
2379 }
Maor Gottlieb026bae02016-06-17 15:14:51 +03002380
Moses Reuben2d1e6972016-11-14 19:04:52 +02002381 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb026bae02016-06-17 15:14:51 +03002382 src_ipv4_src_ipv6.ipv6_layout.ipv6),
2383 &ib_spec->ipv6.mask.src_ip,
2384 sizeof(ib_spec->ipv6.mask.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002385 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb026bae02016-06-17 15:14:51 +03002386 src_ipv4_src_ipv6.ipv6_layout.ipv6),
2387 &ib_spec->ipv6.val.src_ip,
2388 sizeof(ib_spec->ipv6.val.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002389 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb026bae02016-06-17 15:14:51 +03002390 dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2391 &ib_spec->ipv6.mask.dst_ip,
2392 sizeof(ib_spec->ipv6.mask.dst_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002393 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb026bae02016-06-17 15:14:51 +03002394 dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2395 &ib_spec->ipv6.val.dst_ip,
2396 sizeof(ib_spec->ipv6.val.dst_ip));
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002397
Moses Reuben2d1e6972016-11-14 19:04:52 +02002398 set_tos(headers_c, headers_v,
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002399 ib_spec->ipv6.mask.traffic_class,
2400 ib_spec->ipv6.val.traffic_class);
2401
Moses Reuben2d1e6972016-11-14 19:04:52 +02002402 set_proto(headers_c, headers_v,
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002403 ib_spec->ipv6.mask.next_hdr,
2404 ib_spec->ipv6.val.next_hdr);
2405
Moses Reuben2d1e6972016-11-14 19:04:52 +02002406 set_flow_label(misc_params_c, misc_params_v,
2407 ntohl(ib_spec->ipv6.mask.flow_label),
2408 ntohl(ib_spec->ipv6.val.flow_label),
2409 ib_spec->type & IB_FLOW_SPEC_INNER);
2410
Maor Gottlieb026bae02016-06-17 15:14:51 +03002411 break;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002412 case IB_FLOW_SPEC_TCP:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002413 if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
2414 LAST_TCP_UDP_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002415 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002416
Moses Reuben2d1e6972016-11-14 19:04:52 +02002417 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002418 0xff);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002419 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002420 IPPROTO_TCP);
2421
Moses Reuben2d1e6972016-11-14 19:04:52 +02002422 MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002423 ntohs(ib_spec->tcp_udp.mask.src_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002424 MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002425 ntohs(ib_spec->tcp_udp.val.src_port));
2426
Moses Reuben2d1e6972016-11-14 19:04:52 +02002427 MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002428 ntohs(ib_spec->tcp_udp.mask.dst_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002429 MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002430 ntohs(ib_spec->tcp_udp.val.dst_port));
2431 break;
2432 case IB_FLOW_SPEC_UDP:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002433 if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
2434 LAST_TCP_UDP_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002435 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002436
Moses Reuben2d1e6972016-11-14 19:04:52 +02002437 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002438 0xff);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002439 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002440 IPPROTO_UDP);
2441
Moses Reuben2d1e6972016-11-14 19:04:52 +02002442 MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002443 ntohs(ib_spec->tcp_udp.mask.src_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002444 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002445 ntohs(ib_spec->tcp_udp.val.src_port));
2446
Moses Reuben2d1e6972016-11-14 19:04:52 +02002447 MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002448 ntohs(ib_spec->tcp_udp.mask.dst_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002449 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002450 ntohs(ib_spec->tcp_udp.val.dst_port));
2451 break;
Moses Reubenffb30d82016-11-14 19:04:50 +02002452 case IB_FLOW_SPEC_VXLAN_TUNNEL:
2453 if (FIELDS_NOT_SUPPORTED(ib_spec->tunnel.mask,
2454 LAST_TUNNEL_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002455 return -EOPNOTSUPP;
Moses Reubenffb30d82016-11-14 19:04:50 +02002456
2457 MLX5_SET(fte_match_set_misc, misc_params_c, vxlan_vni,
2458 ntohl(ib_spec->tunnel.mask.tunnel_id));
2459 MLX5_SET(fte_match_set_misc, misc_params_v, vxlan_vni,
2460 ntohl(ib_spec->tunnel.val.tunnel_id));
2461 break;
Moses Reuben2ac693f2017-01-18 14:59:50 +02002462 case IB_FLOW_SPEC_ACTION_TAG:
2463 if (FIELDS_NOT_SUPPORTED(ib_spec->flow_tag,
2464 LAST_FLOW_TAG_FIELD))
2465 return -EOPNOTSUPP;
2466 if (ib_spec->flow_tag.tag_id >= BIT(24))
2467 return -EINVAL;
2468
2469 *tag_id = ib_spec->flow_tag.tag_id;
2470 break;
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002471 case IB_FLOW_SPEC_ACTION_DROP:
2472 if (FIELDS_NOT_SUPPORTED(ib_spec->drop,
2473 LAST_DROP_FIELD))
2474 return -EOPNOTSUPP;
2475 *is_drop = true;
2476 break;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002477 default:
2478 return -EINVAL;
2479 }
2480
2481 return 0;
2482}
2483
2484/* If a flow could catch both multicast and unicast packets,
2485 * it won't fall into the multicast flow steering table and this rule
2486 * could steal other multicast packets.
2487 */
Yishai Hadasa550ddf2017-08-17 15:52:33 +03002488static bool flow_is_multicast_only(const struct ib_flow_attr *ib_attr)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002489{
Yishai Hadas81e30882017-06-08 16:15:09 +03002490 union ib_flow_spec *flow_spec;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002491
2492 if (ib_attr->type != IB_FLOW_ATTR_NORMAL ||
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002493 ib_attr->num_of_specs < 1)
2494 return false;
2495
Yishai Hadas81e30882017-06-08 16:15:09 +03002496 flow_spec = (union ib_flow_spec *)(ib_attr + 1);
2497 if (flow_spec->type == IB_FLOW_SPEC_IPV4) {
2498 struct ib_flow_spec_ipv4 *ipv4_spec;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002499
Yishai Hadas81e30882017-06-08 16:15:09 +03002500 ipv4_spec = (struct ib_flow_spec_ipv4 *)flow_spec;
2501 if (ipv4_is_multicast(ipv4_spec->val.dst_ip))
2502 return true;
2503
2504 return false;
2505 }
2506
2507 if (flow_spec->type == IB_FLOW_SPEC_ETH) {
2508 struct ib_flow_spec_eth *eth_spec;
2509
2510 eth_spec = (struct ib_flow_spec_eth *)flow_spec;
2511 return is_multicast_ether_addr(eth_spec->mask.dst_mac) &&
2512 is_multicast_ether_addr(eth_spec->val.dst_mac);
2513 }
2514
2515 return false;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002516}
2517
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002518static bool is_valid_ethertype(struct mlx5_core_dev *mdev,
2519 const struct ib_flow_attr *flow_attr,
Ariel Levkovich0f750962017-04-03 13:11:02 +03002520 bool check_inner)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002521{
2522 union ib_flow_spec *ib_spec = (union ib_flow_spec *)(flow_attr + 1);
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002523 int match_ipv = check_inner ?
2524 MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2525 ft_field_support.inner_ip_version) :
2526 MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2527 ft_field_support.outer_ip_version);
Ariel Levkovich0f750962017-04-03 13:11:02 +03002528 int inner_bit = check_inner ? IB_FLOW_SPEC_INNER : 0;
2529 bool ipv4_spec_valid, ipv6_spec_valid;
2530 unsigned int ip_spec_type = 0;
2531 bool has_ethertype = false;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002532 unsigned int spec_index;
Ariel Levkovich0f750962017-04-03 13:11:02 +03002533 bool mask_valid = true;
2534 u16 eth_type = 0;
2535 bool type_valid;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002536
2537 /* Validate that ethertype is correct */
2538 for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
Ariel Levkovich0f750962017-04-03 13:11:02 +03002539 if ((ib_spec->type == (IB_FLOW_SPEC_ETH | inner_bit)) &&
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002540 ib_spec->eth.mask.ether_type) {
Ariel Levkovich0f750962017-04-03 13:11:02 +03002541 mask_valid = (ib_spec->eth.mask.ether_type ==
2542 htons(0xffff));
2543 has_ethertype = true;
2544 eth_type = ntohs(ib_spec->eth.val.ether_type);
2545 } else if ((ib_spec->type == (IB_FLOW_SPEC_IPV4 | inner_bit)) ||
2546 (ib_spec->type == (IB_FLOW_SPEC_IPV6 | inner_bit))) {
2547 ip_spec_type = ib_spec->type;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002548 }
2549 ib_spec = (void *)ib_spec + ib_spec->size;
2550 }
Ariel Levkovich0f750962017-04-03 13:11:02 +03002551
2552 type_valid = (!has_ethertype) || (!ip_spec_type);
2553 if (!type_valid && mask_valid) {
2554 ipv4_spec_valid = (eth_type == ETH_P_IP) &&
2555 (ip_spec_type == (IB_FLOW_SPEC_IPV4 | inner_bit));
2556 ipv6_spec_valid = (eth_type == ETH_P_IPV6) &&
2557 (ip_spec_type == (IB_FLOW_SPEC_IPV6 | inner_bit));
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002558
2559 type_valid = (ipv4_spec_valid) || (ipv6_spec_valid) ||
2560 (((eth_type == ETH_P_MPLS_UC) ||
2561 (eth_type == ETH_P_MPLS_MC)) && match_ipv);
Ariel Levkovich0f750962017-04-03 13:11:02 +03002562 }
2563
2564 return type_valid;
2565}
2566
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002567static bool is_valid_attr(struct mlx5_core_dev *mdev,
2568 const struct ib_flow_attr *flow_attr)
Ariel Levkovich0f750962017-04-03 13:11:02 +03002569{
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002570 return is_valid_ethertype(mdev, flow_attr, false) &&
2571 is_valid_ethertype(mdev, flow_attr, true);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002572}
2573
2574static void put_flow_table(struct mlx5_ib_dev *dev,
2575 struct mlx5_ib_flow_prio *prio, bool ft_added)
2576{
2577 prio->refcount -= !!ft_added;
2578 if (!prio->refcount) {
2579 mlx5_destroy_flow_table(prio->flow_table);
2580 prio->flow_table = NULL;
2581 }
2582}
2583
2584static int mlx5_ib_destroy_flow(struct ib_flow *flow_id)
2585{
2586 struct mlx5_ib_dev *dev = to_mdev(flow_id->qp->device);
2587 struct mlx5_ib_flow_handler *handler = container_of(flow_id,
2588 struct mlx5_ib_flow_handler,
2589 ibflow);
2590 struct mlx5_ib_flow_handler *iter, *tmp;
2591
2592 mutex_lock(&dev->flow_db.lock);
2593
2594 list_for_each_entry_safe(iter, tmp, &handler->list, list) {
Mark Bloch74491de2016-08-31 11:24:25 +00002595 mlx5_del_flow_rules(iter->rule);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002596 put_flow_table(dev, iter->prio, true);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002597 list_del(&iter->list);
2598 kfree(iter);
2599 }
2600
Mark Bloch74491de2016-08-31 11:24:25 +00002601 mlx5_del_flow_rules(handler->rule);
Maor Gottlieb5497adc2016-08-28 14:16:31 +03002602 put_flow_table(dev, handler->prio, true);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002603 mutex_unlock(&dev->flow_db.lock);
2604
2605 kfree(handler);
2606
2607 return 0;
2608}
2609
Maor Gottlieb35d190112016-03-07 18:51:47 +02002610static int ib_prio_to_core_prio(unsigned int priority, bool dont_trap)
2611{
2612 priority *= 2;
2613 if (!dont_trap)
2614 priority++;
2615 return priority;
2616}
2617
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002618enum flow_table_type {
2619 MLX5_IB_FT_RX,
2620 MLX5_IB_FT_TX
2621};
2622
Maor Gottlieb00b7c2a2017-03-29 06:09:01 +03002623#define MLX5_FS_MAX_TYPES 6
2624#define MLX5_FS_MAX_ENTRIES BIT(16)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002625static struct mlx5_ib_flow_prio *get_flow_table(struct mlx5_ib_dev *dev,
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002626 struct ib_flow_attr *flow_attr,
2627 enum flow_table_type ft_type)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002628{
Maor Gottlieb35d190112016-03-07 18:51:47 +02002629 bool dont_trap = flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002630 struct mlx5_flow_namespace *ns = NULL;
2631 struct mlx5_ib_flow_prio *prio;
2632 struct mlx5_flow_table *ft;
Maor Gottliebdac388e2017-03-29 06:09:00 +03002633 int max_table_size;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002634 int num_entries;
2635 int num_groups;
2636 int priority;
2637 int err = 0;
2638
Maor Gottliebdac388e2017-03-29 06:09:00 +03002639 max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
2640 log_max_ft_size));
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002641 if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
Maor Gottlieb35d190112016-03-07 18:51:47 +02002642 if (flow_is_multicast_only(flow_attr) &&
2643 !dont_trap)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002644 priority = MLX5_IB_FLOW_MCAST_PRIO;
2645 else
Maor Gottlieb35d190112016-03-07 18:51:47 +02002646 priority = ib_prio_to_core_prio(flow_attr->priority,
2647 dont_trap);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002648 ns = mlx5_get_flow_namespace(dev->mdev,
2649 MLX5_FLOW_NAMESPACE_BYPASS);
2650 num_entries = MLX5_FS_MAX_ENTRIES;
2651 num_groups = MLX5_FS_MAX_TYPES;
2652 prio = &dev->flow_db.prios[priority];
2653 } else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
2654 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
2655 ns = mlx5_get_flow_namespace(dev->mdev,
2656 MLX5_FLOW_NAMESPACE_LEFTOVERS);
2657 build_leftovers_ft_param(&priority,
2658 &num_entries,
2659 &num_groups);
2660 prio = &dev->flow_db.prios[MLX5_IB_FLOW_LEFTOVERS_PRIO];
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002661 } else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
2662 if (!MLX5_CAP_FLOWTABLE(dev->mdev,
2663 allow_sniffer_and_nic_rx_shared_tir))
2664 return ERR_PTR(-ENOTSUPP);
2665
2666 ns = mlx5_get_flow_namespace(dev->mdev, ft_type == MLX5_IB_FT_RX ?
2667 MLX5_FLOW_NAMESPACE_SNIFFER_RX :
2668 MLX5_FLOW_NAMESPACE_SNIFFER_TX);
2669
2670 prio = &dev->flow_db.sniffer[ft_type];
2671 priority = 0;
2672 num_entries = 1;
2673 num_groups = 1;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002674 }
2675
2676 if (!ns)
2677 return ERR_PTR(-ENOTSUPP);
2678
Maor Gottliebdac388e2017-03-29 06:09:00 +03002679 if (num_entries > max_table_size)
2680 return ERR_PTR(-ENOMEM);
2681
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002682 ft = prio->flow_table;
2683 if (!ft) {
2684 ft = mlx5_create_auto_grouped_flow_table(ns, priority,
2685 num_entries,
Maor Gottliebd63cd282016-04-29 01:36:35 +03002686 num_groups,
Hadar Hen Zionc9f1b072016-11-07 15:14:44 +02002687 0, 0);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002688
2689 if (!IS_ERR(ft)) {
2690 prio->refcount = 0;
2691 prio->flow_table = ft;
2692 } else {
2693 err = PTR_ERR(ft);
2694 }
2695 }
2696
2697 return err ? ERR_PTR(err) : prio;
2698}
2699
Yishai Hadasa550ddf2017-08-17 15:52:33 +03002700static void set_underlay_qp(struct mlx5_ib_dev *dev,
2701 struct mlx5_flow_spec *spec,
2702 u32 underlay_qpn)
2703{
2704 void *misc_params_c = MLX5_ADDR_OF(fte_match_param,
2705 spec->match_criteria,
2706 misc_parameters);
2707 void *misc_params_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
2708 misc_parameters);
2709
2710 if (underlay_qpn &&
2711 MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
2712 ft_field_support.bth_dst_qp)) {
2713 MLX5_SET(fte_match_set_misc,
2714 misc_params_v, bth_dst_qp, underlay_qpn);
2715 MLX5_SET(fte_match_set_misc,
2716 misc_params_c, bth_dst_qp, 0xffffff);
2717 }
2718}
2719
2720static struct mlx5_ib_flow_handler *_create_flow_rule(struct mlx5_ib_dev *dev,
2721 struct mlx5_ib_flow_prio *ft_prio,
2722 const struct ib_flow_attr *flow_attr,
2723 struct mlx5_flow_destination *dst,
2724 u32 underlay_qpn)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002725{
2726 struct mlx5_flow_table *ft = ft_prio->flow_table;
2727 struct mlx5_ib_flow_handler *handler;
Hadar Hen Zion66958ed2016-11-07 15:14:45 +02002728 struct mlx5_flow_act flow_act = {0};
Maor Gottliebc5bb1732016-07-04 17:23:05 +03002729 struct mlx5_flow_spec *spec;
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002730 struct mlx5_flow_destination *rule_dst = dst;
Maor Gottliebdd063d02016-08-28 14:16:32 +03002731 const void *ib_flow = (const void *)flow_attr + sizeof(*flow_attr);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002732 unsigned int spec_index;
Moses Reuben2ac693f2017-01-18 14:59:50 +02002733 u32 flow_tag = MLX5_FS_DEFAULT_FLOW_TAG;
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002734 bool is_drop = false;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002735 int err = 0;
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002736 int dest_num = 1;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002737
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002738 if (!is_valid_attr(dev->mdev, flow_attr))
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002739 return ERR_PTR(-EINVAL);
2740
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03002741 spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002742 handler = kzalloc(sizeof(*handler), GFP_KERNEL);
Maor Gottliebc5bb1732016-07-04 17:23:05 +03002743 if (!handler || !spec) {
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002744 err = -ENOMEM;
2745 goto free;
2746 }
2747
2748 INIT_LIST_HEAD(&handler->list);
2749
2750 for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002751 err = parse_flow_attr(dev->mdev, spec->match_criteria,
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002752 spec->match_value,
2753 ib_flow, &flow_tag, &is_drop);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002754 if (err < 0)
2755 goto free;
2756
2757 ib_flow += ((union ib_flow_spec *)ib_flow)->size;
2758 }
2759
Yishai Hadasa550ddf2017-08-17 15:52:33 +03002760 if (!flow_is_multicast_only(flow_attr))
2761 set_underlay_qp(dev, spec, underlay_qpn);
2762
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002763 spec->match_criteria_enable = get_match_criteria_enable(spec->match_criteria);
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002764 if (is_drop) {
2765 flow_act.action = MLX5_FLOW_CONTEXT_ACTION_DROP;
2766 rule_dst = NULL;
2767 dest_num = 0;
2768 } else {
2769 flow_act.action = dst ? MLX5_FLOW_CONTEXT_ACTION_FWD_DEST :
2770 MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO;
2771 }
Moses Reuben2ac693f2017-01-18 14:59:50 +02002772
2773 if (flow_tag != MLX5_FS_DEFAULT_FLOW_TAG &&
2774 (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
2775 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT)) {
2776 mlx5_ib_warn(dev, "Flow tag %u and attribute type %x isn't allowed in leftovers\n",
2777 flow_tag, flow_attr->type);
2778 err = -EINVAL;
2779 goto free;
2780 }
2781 flow_act.flow_tag = flow_tag;
Mark Bloch74491de2016-08-31 11:24:25 +00002782 handler->rule = mlx5_add_flow_rules(ft, spec,
Hadar Hen Zion66958ed2016-11-07 15:14:45 +02002783 &flow_act,
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002784 rule_dst, dest_num);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002785
2786 if (IS_ERR(handler->rule)) {
2787 err = PTR_ERR(handler->rule);
2788 goto free;
2789 }
2790
Maor Gottliebd9d49802016-08-28 14:16:33 +03002791 ft_prio->refcount++;
Maor Gottlieb5497adc2016-08-28 14:16:31 +03002792 handler->prio = ft_prio;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002793
2794 ft_prio->flow_table = ft;
2795free:
2796 if (err)
2797 kfree(handler);
Maor Gottliebc5bb1732016-07-04 17:23:05 +03002798 kvfree(spec);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002799 return err ? ERR_PTR(err) : handler;
2800}
2801
Yishai Hadasa550ddf2017-08-17 15:52:33 +03002802static struct mlx5_ib_flow_handler *create_flow_rule(struct mlx5_ib_dev *dev,
2803 struct mlx5_ib_flow_prio *ft_prio,
2804 const struct ib_flow_attr *flow_attr,
2805 struct mlx5_flow_destination *dst)
2806{
2807 return _create_flow_rule(dev, ft_prio, flow_attr, dst, 0);
2808}
2809
Maor Gottlieb35d190112016-03-07 18:51:47 +02002810static struct mlx5_ib_flow_handler *create_dont_trap_rule(struct mlx5_ib_dev *dev,
2811 struct mlx5_ib_flow_prio *ft_prio,
2812 struct ib_flow_attr *flow_attr,
2813 struct mlx5_flow_destination *dst)
2814{
2815 struct mlx5_ib_flow_handler *handler_dst = NULL;
2816 struct mlx5_ib_flow_handler *handler = NULL;
2817
2818 handler = create_flow_rule(dev, ft_prio, flow_attr, NULL);
2819 if (!IS_ERR(handler)) {
2820 handler_dst = create_flow_rule(dev, ft_prio,
2821 flow_attr, dst);
2822 if (IS_ERR(handler_dst)) {
Mark Bloch74491de2016-08-31 11:24:25 +00002823 mlx5_del_flow_rules(handler->rule);
Maor Gottliebd9d49802016-08-28 14:16:33 +03002824 ft_prio->refcount--;
Maor Gottlieb35d190112016-03-07 18:51:47 +02002825 kfree(handler);
2826 handler = handler_dst;
2827 } else {
2828 list_add(&handler_dst->list, &handler->list);
2829 }
2830 }
2831
2832 return handler;
2833}
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002834enum {
2835 LEFTOVERS_MC,
2836 LEFTOVERS_UC,
2837};
2838
2839static struct mlx5_ib_flow_handler *create_leftovers_rule(struct mlx5_ib_dev *dev,
2840 struct mlx5_ib_flow_prio *ft_prio,
2841 struct ib_flow_attr *flow_attr,
2842 struct mlx5_flow_destination *dst)
2843{
2844 struct mlx5_ib_flow_handler *handler_ucast = NULL;
2845 struct mlx5_ib_flow_handler *handler = NULL;
2846
2847 static struct {
2848 struct ib_flow_attr flow_attr;
2849 struct ib_flow_spec_eth eth_flow;
2850 } leftovers_specs[] = {
2851 [LEFTOVERS_MC] = {
2852 .flow_attr = {
2853 .num_of_specs = 1,
2854 .size = sizeof(leftovers_specs[0])
2855 },
2856 .eth_flow = {
2857 .type = IB_FLOW_SPEC_ETH,
2858 .size = sizeof(struct ib_flow_spec_eth),
2859 .mask = {.dst_mac = {0x1} },
2860 .val = {.dst_mac = {0x1} }
2861 }
2862 },
2863 [LEFTOVERS_UC] = {
2864 .flow_attr = {
2865 .num_of_specs = 1,
2866 .size = sizeof(leftovers_specs[0])
2867 },
2868 .eth_flow = {
2869 .type = IB_FLOW_SPEC_ETH,
2870 .size = sizeof(struct ib_flow_spec_eth),
2871 .mask = {.dst_mac = {0x1} },
2872 .val = {.dst_mac = {} }
2873 }
2874 }
2875 };
2876
2877 handler = create_flow_rule(dev, ft_prio,
2878 &leftovers_specs[LEFTOVERS_MC].flow_attr,
2879 dst);
2880 if (!IS_ERR(handler) &&
2881 flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT) {
2882 handler_ucast = create_flow_rule(dev, ft_prio,
2883 &leftovers_specs[LEFTOVERS_UC].flow_attr,
2884 dst);
2885 if (IS_ERR(handler_ucast)) {
Mark Bloch74491de2016-08-31 11:24:25 +00002886 mlx5_del_flow_rules(handler->rule);
Maor Gottliebd9d49802016-08-28 14:16:33 +03002887 ft_prio->refcount--;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002888 kfree(handler);
2889 handler = handler_ucast;
2890 } else {
2891 list_add(&handler_ucast->list, &handler->list);
2892 }
2893 }
2894
2895 return handler;
2896}
2897
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002898static struct mlx5_ib_flow_handler *create_sniffer_rule(struct mlx5_ib_dev *dev,
2899 struct mlx5_ib_flow_prio *ft_rx,
2900 struct mlx5_ib_flow_prio *ft_tx,
2901 struct mlx5_flow_destination *dst)
2902{
2903 struct mlx5_ib_flow_handler *handler_rx;
2904 struct mlx5_ib_flow_handler *handler_tx;
2905 int err;
2906 static const struct ib_flow_attr flow_attr = {
2907 .num_of_specs = 0,
2908 .size = sizeof(flow_attr)
2909 };
2910
2911 handler_rx = create_flow_rule(dev, ft_rx, &flow_attr, dst);
2912 if (IS_ERR(handler_rx)) {
2913 err = PTR_ERR(handler_rx);
2914 goto err;
2915 }
2916
2917 handler_tx = create_flow_rule(dev, ft_tx, &flow_attr, dst);
2918 if (IS_ERR(handler_tx)) {
2919 err = PTR_ERR(handler_tx);
2920 goto err_tx;
2921 }
2922
2923 list_add(&handler_tx->list, &handler_rx->list);
2924
2925 return handler_rx;
2926
2927err_tx:
Mark Bloch74491de2016-08-31 11:24:25 +00002928 mlx5_del_flow_rules(handler_rx->rule);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002929 ft_rx->refcount--;
2930 kfree(handler_rx);
2931err:
2932 return ERR_PTR(err);
2933}
2934
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002935static struct ib_flow *mlx5_ib_create_flow(struct ib_qp *qp,
2936 struct ib_flow_attr *flow_attr,
2937 int domain)
2938{
2939 struct mlx5_ib_dev *dev = to_mdev(qp->device);
Yishai Hadasd9f88e52016-08-28 10:58:37 +03002940 struct mlx5_ib_qp *mqp = to_mqp(qp);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002941 struct mlx5_ib_flow_handler *handler = NULL;
2942 struct mlx5_flow_destination *dst = NULL;
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002943 struct mlx5_ib_flow_prio *ft_prio_tx = NULL;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002944 struct mlx5_ib_flow_prio *ft_prio;
2945 int err;
Yishai Hadasa550ddf2017-08-17 15:52:33 +03002946 int underlay_qpn;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002947
2948 if (flow_attr->priority > MLX5_IB_FLOW_LAST_PRIO)
Maor Gottliebdac388e2017-03-29 06:09:00 +03002949 return ERR_PTR(-ENOMEM);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002950
2951 if (domain != IB_FLOW_DOMAIN_USER ||
Daniel Jurgens508562d2018-01-04 17:25:34 +02002952 flow_attr->port > dev->num_ports ||
Maor Gottlieb35d190112016-03-07 18:51:47 +02002953 (flow_attr->flags & ~IB_FLOW_ATTR_FLAGS_DONT_TRAP))
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002954 return ERR_PTR(-EINVAL);
2955
2956 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
2957 if (!dst)
2958 return ERR_PTR(-ENOMEM);
2959
2960 mutex_lock(&dev->flow_db.lock);
2961
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002962 ft_prio = get_flow_table(dev, flow_attr, MLX5_IB_FT_RX);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002963 if (IS_ERR(ft_prio)) {
2964 err = PTR_ERR(ft_prio);
2965 goto unlock;
2966 }
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002967 if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
2968 ft_prio_tx = get_flow_table(dev, flow_attr, MLX5_IB_FT_TX);
2969 if (IS_ERR(ft_prio_tx)) {
2970 err = PTR_ERR(ft_prio_tx);
2971 ft_prio_tx = NULL;
2972 goto destroy_ft;
2973 }
2974 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002975
2976 dst->type = MLX5_FLOW_DESTINATION_TYPE_TIR;
Yishai Hadasd9f88e52016-08-28 10:58:37 +03002977 if (mqp->flags & MLX5_IB_QP_RSS)
2978 dst->tir_num = mqp->rss_qp.tirn;
2979 else
2980 dst->tir_num = mqp->raw_packet_qp.rq.tirn;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002981
2982 if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
Maor Gottlieb35d190112016-03-07 18:51:47 +02002983 if (flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP) {
2984 handler = create_dont_trap_rule(dev, ft_prio,
2985 flow_attr, dst);
2986 } else {
Yishai Hadasa550ddf2017-08-17 15:52:33 +03002987 underlay_qpn = (mqp->flags & MLX5_IB_QP_UNDERLAY) ?
2988 mqp->underlay_qpn : 0;
2989 handler = _create_flow_rule(dev, ft_prio, flow_attr,
2990 dst, underlay_qpn);
Maor Gottlieb35d190112016-03-07 18:51:47 +02002991 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002992 } else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
2993 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
2994 handler = create_leftovers_rule(dev, ft_prio, flow_attr,
2995 dst);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002996 } else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
2997 handler = create_sniffer_rule(dev, ft_prio, ft_prio_tx, dst);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002998 } else {
2999 err = -EINVAL;
3000 goto destroy_ft;
3001 }
3002
3003 if (IS_ERR(handler)) {
3004 err = PTR_ERR(handler);
3005 handler = NULL;
3006 goto destroy_ft;
3007 }
3008
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003009 mutex_unlock(&dev->flow_db.lock);
3010 kfree(dst);
3011
3012 return &handler->ibflow;
3013
3014destroy_ft:
3015 put_flow_table(dev, ft_prio, false);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003016 if (ft_prio_tx)
3017 put_flow_table(dev, ft_prio_tx, false);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003018unlock:
3019 mutex_unlock(&dev->flow_db.lock);
3020 kfree(dst);
3021 kfree(handler);
3022 return ERR_PTR(err);
3023}
3024
Eli Cohene126ba92013-07-07 17:25:49 +03003025static int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
3026{
3027 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
Yishai Hadas81e30882017-06-08 16:15:09 +03003028 struct mlx5_ib_qp *mqp = to_mqp(ibqp);
Eli Cohene126ba92013-07-07 17:25:49 +03003029 int err;
3030
Yishai Hadas81e30882017-06-08 16:15:09 +03003031 if (mqp->flags & MLX5_IB_QP_UNDERLAY) {
3032 mlx5_ib_dbg(dev, "Attaching a multi cast group to underlay QP is not supported\n");
3033 return -EOPNOTSUPP;
3034 }
3035
Jack Morgenstein9603b612014-07-28 23:30:22 +03003036 err = mlx5_core_attach_mcg(dev->mdev, gid, ibqp->qp_num);
Eli Cohene126ba92013-07-07 17:25:49 +03003037 if (err)
3038 mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n",
3039 ibqp->qp_num, gid->raw);
3040
3041 return err;
3042}
3043
3044static int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
3045{
3046 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
3047 int err;
3048
Jack Morgenstein9603b612014-07-28 23:30:22 +03003049 err = mlx5_core_detach_mcg(dev->mdev, gid, ibqp->qp_num);
Eli Cohene126ba92013-07-07 17:25:49 +03003050 if (err)
3051 mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n",
3052 ibqp->qp_num, gid->raw);
3053
3054 return err;
3055}
3056
3057static int init_node_data(struct mlx5_ib_dev *dev)
3058{
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03003059 int err;
Eli Cohene126ba92013-07-07 17:25:49 +03003060
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03003061 err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc);
Eli Cohene126ba92013-07-07 17:25:49 +03003062 if (err)
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03003063 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03003064
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03003065 dev->mdev->rev_id = dev->mdev->pdev->revision;
Eli Cohene126ba92013-07-07 17:25:49 +03003066
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03003067 return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid);
Eli Cohene126ba92013-07-07 17:25:49 +03003068}
3069
3070static ssize_t show_fw_pages(struct device *device, struct device_attribute *attr,
3071 char *buf)
3072{
3073 struct mlx5_ib_dev *dev =
3074 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
3075
Jack Morgenstein9603b612014-07-28 23:30:22 +03003076 return sprintf(buf, "%d\n", dev->mdev->priv.fw_pages);
Eli Cohene126ba92013-07-07 17:25:49 +03003077}
3078
3079static ssize_t show_reg_pages(struct device *device,
3080 struct device_attribute *attr, char *buf)
3081{
3082 struct mlx5_ib_dev *dev =
3083 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
3084
Haggai Eran6aec21f2014-12-11 17:04:23 +02003085 return sprintf(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages));
Eli Cohene126ba92013-07-07 17:25:49 +03003086}
3087
3088static ssize_t show_hca(struct device *device, struct device_attribute *attr,
3089 char *buf)
3090{
3091 struct mlx5_ib_dev *dev =
3092 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
Jack Morgenstein9603b612014-07-28 23:30:22 +03003093 return sprintf(buf, "MT%d\n", dev->mdev->pdev->device);
Eli Cohene126ba92013-07-07 17:25:49 +03003094}
3095
Eli Cohene126ba92013-07-07 17:25:49 +03003096static ssize_t show_rev(struct device *device, struct device_attribute *attr,
3097 char *buf)
3098{
3099 struct mlx5_ib_dev *dev =
3100 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
Jack Morgenstein9603b612014-07-28 23:30:22 +03003101 return sprintf(buf, "%x\n", dev->mdev->rev_id);
Eli Cohene126ba92013-07-07 17:25:49 +03003102}
3103
3104static ssize_t show_board(struct device *device, struct device_attribute *attr,
3105 char *buf)
3106{
3107 struct mlx5_ib_dev *dev =
3108 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
3109 return sprintf(buf, "%.*s\n", MLX5_BOARD_ID_LEN,
Jack Morgenstein9603b612014-07-28 23:30:22 +03003110 dev->mdev->board_id);
Eli Cohene126ba92013-07-07 17:25:49 +03003111}
3112
3113static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03003114static DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL);
3115static DEVICE_ATTR(board_id, S_IRUGO, show_board, NULL);
3116static DEVICE_ATTR(fw_pages, S_IRUGO, show_fw_pages, NULL);
3117static DEVICE_ATTR(reg_pages, S_IRUGO, show_reg_pages, NULL);
3118
3119static struct device_attribute *mlx5_class_attributes[] = {
3120 &dev_attr_hw_rev,
Eli Cohene126ba92013-07-07 17:25:49 +03003121 &dev_attr_hca_type,
3122 &dev_attr_board_id,
3123 &dev_attr_fw_pages,
3124 &dev_attr_reg_pages,
3125};
3126
Haggai Eran7722f472016-02-29 15:45:07 +02003127static void pkey_change_handler(struct work_struct *work)
3128{
3129 struct mlx5_ib_port_resources *ports =
3130 container_of(work, struct mlx5_ib_port_resources,
3131 pkey_change_work);
3132
3133 mutex_lock(&ports->devr->mutex);
3134 mlx5_ib_gsi_pkey_change(ports->gsi);
3135 mutex_unlock(&ports->devr->mutex);
3136}
3137
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03003138static void mlx5_ib_handle_internal_error(struct mlx5_ib_dev *ibdev)
3139{
3140 struct mlx5_ib_qp *mqp;
3141 struct mlx5_ib_cq *send_mcq, *recv_mcq;
3142 struct mlx5_core_cq *mcq;
3143 struct list_head cq_armed_list;
3144 unsigned long flags_qp;
3145 unsigned long flags_cq;
3146 unsigned long flags;
3147
3148 INIT_LIST_HEAD(&cq_armed_list);
3149
3150 /* Go over qp list reside on that ibdev, sync with create/destroy qp.*/
3151 spin_lock_irqsave(&ibdev->reset_flow_resource_lock, flags);
3152 list_for_each_entry(mqp, &ibdev->qp_list, qps_list) {
3153 spin_lock_irqsave(&mqp->sq.lock, flags_qp);
3154 if (mqp->sq.tail != mqp->sq.head) {
3155 send_mcq = to_mcq(mqp->ibqp.send_cq);
3156 spin_lock_irqsave(&send_mcq->lock, flags_cq);
3157 if (send_mcq->mcq.comp &&
3158 mqp->ibqp.send_cq->comp_handler) {
3159 if (!send_mcq->mcq.reset_notify_added) {
3160 send_mcq->mcq.reset_notify_added = 1;
3161 list_add_tail(&send_mcq->mcq.reset_notify,
3162 &cq_armed_list);
3163 }
3164 }
3165 spin_unlock_irqrestore(&send_mcq->lock, flags_cq);
3166 }
3167 spin_unlock_irqrestore(&mqp->sq.lock, flags_qp);
3168 spin_lock_irqsave(&mqp->rq.lock, flags_qp);
3169 /* no handling is needed for SRQ */
3170 if (!mqp->ibqp.srq) {
3171 if (mqp->rq.tail != mqp->rq.head) {
3172 recv_mcq = to_mcq(mqp->ibqp.recv_cq);
3173 spin_lock_irqsave(&recv_mcq->lock, flags_cq);
3174 if (recv_mcq->mcq.comp &&
3175 mqp->ibqp.recv_cq->comp_handler) {
3176 if (!recv_mcq->mcq.reset_notify_added) {
3177 recv_mcq->mcq.reset_notify_added = 1;
3178 list_add_tail(&recv_mcq->mcq.reset_notify,
3179 &cq_armed_list);
3180 }
3181 }
3182 spin_unlock_irqrestore(&recv_mcq->lock,
3183 flags_cq);
3184 }
3185 }
3186 spin_unlock_irqrestore(&mqp->rq.lock, flags_qp);
3187 }
3188 /*At that point all inflight post send were put to be executed as of we
3189 * lock/unlock above locks Now need to arm all involved CQs.
3190 */
3191 list_for_each_entry(mcq, &cq_armed_list, reset_notify) {
3192 mcq->comp(mcq);
3193 }
3194 spin_unlock_irqrestore(&ibdev->reset_flow_resource_lock, flags);
3195}
3196
Maor Gottlieb03404e82017-05-30 10:29:13 +03003197static void delay_drop_handler(struct work_struct *work)
3198{
3199 int err;
3200 struct mlx5_ib_delay_drop *delay_drop =
3201 container_of(work, struct mlx5_ib_delay_drop,
3202 delay_drop_work);
3203
Maor Gottliebfe248c32017-05-30 10:29:14 +03003204 atomic_inc(&delay_drop->events_cnt);
3205
Maor Gottlieb03404e82017-05-30 10:29:13 +03003206 mutex_lock(&delay_drop->lock);
3207 err = mlx5_core_set_delay_drop(delay_drop->dev->mdev,
3208 delay_drop->timeout);
3209 if (err) {
3210 mlx5_ib_warn(delay_drop->dev, "Failed to set delay drop, timeout=%u\n",
3211 delay_drop->timeout);
3212 delay_drop->activate = false;
3213 }
3214 mutex_unlock(&delay_drop->lock);
3215}
3216
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02003217static void mlx5_ib_handle_event(struct work_struct *_work)
Eli Cohene126ba92013-07-07 17:25:49 +03003218{
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02003219 struct mlx5_ib_event_work *work =
3220 container_of(_work, struct mlx5_ib_event_work, work);
3221 struct mlx5_ib_dev *ibdev;
Eli Cohene126ba92013-07-07 17:25:49 +03003222 struct ib_event ibev;
Eli Cohendbaaff22016-10-27 16:36:44 +03003223 bool fatal = false;
Eli Cohene126ba92013-07-07 17:25:49 +03003224 u8 port = 0;
3225
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02003226 if (mlx5_core_is_mp_slave(work->dev)) {
3227 ibdev = mlx5_ib_get_ibdev_from_mpi(work->context);
3228 if (!ibdev)
3229 goto out;
3230 } else {
3231 ibdev = work->context;
3232 }
3233
3234 switch (work->event) {
Eli Cohene126ba92013-07-07 17:25:49 +03003235 case MLX5_DEV_EVENT_SYS_ERROR:
Eli Cohene126ba92013-07-07 17:25:49 +03003236 ibev.event = IB_EVENT_DEVICE_FATAL;
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03003237 mlx5_ib_handle_internal_error(ibdev);
Eli Cohendbaaff22016-10-27 16:36:44 +03003238 fatal = true;
Eli Cohene126ba92013-07-07 17:25:49 +03003239 break;
3240
3241 case MLX5_DEV_EVENT_PORT_UP:
Eli Cohene126ba92013-07-07 17:25:49 +03003242 case MLX5_DEV_EVENT_PORT_DOWN:
Noa Osherovich2788cf32016-06-04 15:15:29 +03003243 case MLX5_DEV_EVENT_PORT_INITIALIZED:
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02003244 port = (u8)work->param;
Aviv Heller5ec8c832016-09-18 20:48:00 +03003245
3246 /* In RoCE, port up/down events are handled in
3247 * mlx5_netdev_event().
3248 */
3249 if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) ==
3250 IB_LINK_LAYER_ETHERNET)
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02003251 goto out;
Aviv Heller5ec8c832016-09-18 20:48:00 +03003252
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02003253 ibev.event = (work->event == MLX5_DEV_EVENT_PORT_UP) ?
Aviv Heller5ec8c832016-09-18 20:48:00 +03003254 IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
Eli Cohene126ba92013-07-07 17:25:49 +03003255 break;
3256
Eli Cohene126ba92013-07-07 17:25:49 +03003257 case MLX5_DEV_EVENT_LID_CHANGE:
3258 ibev.event = IB_EVENT_LID_CHANGE;
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02003259 port = (u8)work->param;
Eli Cohene126ba92013-07-07 17:25:49 +03003260 break;
3261
3262 case MLX5_DEV_EVENT_PKEY_CHANGE:
3263 ibev.event = IB_EVENT_PKEY_CHANGE;
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02003264 port = (u8)work->param;
Haggai Eran7722f472016-02-29 15:45:07 +02003265
3266 schedule_work(&ibdev->devr.ports[port - 1].pkey_change_work);
Eli Cohene126ba92013-07-07 17:25:49 +03003267 break;
3268
3269 case MLX5_DEV_EVENT_GUID_CHANGE:
3270 ibev.event = IB_EVENT_GID_CHANGE;
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02003271 port = (u8)work->param;
Eli Cohene126ba92013-07-07 17:25:49 +03003272 break;
3273
3274 case MLX5_DEV_EVENT_CLIENT_REREG:
3275 ibev.event = IB_EVENT_CLIENT_REREGISTER;
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02003276 port = (u8)work->param;
Eli Cohene126ba92013-07-07 17:25:49 +03003277 break;
Maor Gottlieb03404e82017-05-30 10:29:13 +03003278 case MLX5_DEV_EVENT_DELAY_DROP_TIMEOUT:
3279 schedule_work(&ibdev->delay_drop.delay_drop_work);
3280 goto out;
Saeed Mahameedbdc37922016-09-29 19:35:38 +03003281 default:
Maor Gottlieb03404e82017-05-30 10:29:13 +03003282 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03003283 }
3284
3285 ibev.device = &ibdev->ib_dev;
3286 ibev.element.port_num = port;
3287
Eli Cohena0c84c32013-09-11 16:35:27 +03003288 if (port < 1 || port > ibdev->num_ports) {
3289 mlx5_ib_warn(ibdev, "warning: event on port %d\n", port);
Maor Gottlieb03404e82017-05-30 10:29:13 +03003290 goto out;
Eli Cohena0c84c32013-09-11 16:35:27 +03003291 }
3292
Eli Cohene126ba92013-07-07 17:25:49 +03003293 if (ibdev->ib_active)
3294 ib_dispatch_event(&ibev);
Eli Cohendbaaff22016-10-27 16:36:44 +03003295
3296 if (fatal)
3297 ibdev->ib_active = false;
Maor Gottlieb03404e82017-05-30 10:29:13 +03003298out:
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02003299 kfree(work);
3300}
3301
3302static void mlx5_ib_event(struct mlx5_core_dev *dev, void *context,
3303 enum mlx5_dev_event event, unsigned long param)
3304{
3305 struct mlx5_ib_event_work *work;
3306
3307 work = kmalloc(sizeof(*work), GFP_ATOMIC);
3308 if (work) {
3309 INIT_WORK(&work->work, mlx5_ib_handle_event);
3310 work->dev = dev;
3311 work->param = param;
3312 work->context = context;
3313 work->event = event;
3314
3315 queue_work(mlx5_ib_event_wq, &work->work);
3316 return;
3317 }
3318
3319 dev_warn(&dev->pdev->dev, "%s: mlx5_dev_event: %d, with param: %lu dropped, couldn't allocate memory.\n",
3320 __func__, event, param);
Eli Cohene126ba92013-07-07 17:25:49 +03003321}
3322
Maor Gottliebc43f1112017-01-18 14:10:33 +02003323static int set_has_smi_cap(struct mlx5_ib_dev *dev)
3324{
3325 struct mlx5_hca_vport_context vport_ctx;
3326 int err;
3327 int port;
3328
Daniel Jurgens508562d2018-01-04 17:25:34 +02003329 for (port = 1; port <= dev->num_ports; port++) {
Maor Gottliebc43f1112017-01-18 14:10:33 +02003330 dev->mdev->port_caps[port - 1].has_smi = false;
3331 if (MLX5_CAP_GEN(dev->mdev, port_type) ==
3332 MLX5_CAP_PORT_TYPE_IB) {
3333 if (MLX5_CAP_GEN(dev->mdev, ib_virt)) {
3334 err = mlx5_query_hca_vport_context(dev->mdev, 0,
3335 port, 0,
3336 &vport_ctx);
3337 if (err) {
3338 mlx5_ib_err(dev, "query_hca_vport_context for port=%d failed %d\n",
3339 port, err);
3340 return err;
3341 }
3342 dev->mdev->port_caps[port - 1].has_smi =
3343 vport_ctx.has_smi;
3344 } else {
3345 dev->mdev->port_caps[port - 1].has_smi = true;
3346 }
3347 }
3348 }
3349 return 0;
3350}
3351
Eli Cohene126ba92013-07-07 17:25:49 +03003352static void get_ext_port_caps(struct mlx5_ib_dev *dev)
3353{
3354 int port;
3355
Daniel Jurgens508562d2018-01-04 17:25:34 +02003356 for (port = 1; port <= dev->num_ports; port++)
Eli Cohene126ba92013-07-07 17:25:49 +03003357 mlx5_query_ext_port_caps(dev, port);
3358}
3359
Daniel Jurgens32f69e42018-01-04 17:25:36 +02003360static int get_port_caps(struct mlx5_ib_dev *dev, u8 port)
Eli Cohene126ba92013-07-07 17:25:49 +03003361{
3362 struct ib_device_attr *dprops = NULL;
3363 struct ib_port_attr *pprops = NULL;
Dan Carpenterf614fc12015-01-12 11:56:58 +03003364 int err = -ENOMEM;
Matan Barak2528e332015-06-11 16:35:25 +03003365 struct ib_udata uhw = {.inlen = 0, .outlen = 0};
Eli Cohene126ba92013-07-07 17:25:49 +03003366
3367 pprops = kmalloc(sizeof(*pprops), GFP_KERNEL);
3368 if (!pprops)
3369 goto out;
3370
3371 dprops = kmalloc(sizeof(*dprops), GFP_KERNEL);
3372 if (!dprops)
3373 goto out;
3374
Maor Gottliebc43f1112017-01-18 14:10:33 +02003375 err = set_has_smi_cap(dev);
3376 if (err)
3377 goto out;
3378
Matan Barak2528e332015-06-11 16:35:25 +03003379 err = mlx5_ib_query_device(&dev->ib_dev, dprops, &uhw);
Eli Cohene126ba92013-07-07 17:25:49 +03003380 if (err) {
3381 mlx5_ib_warn(dev, "query_device failed %d\n", err);
3382 goto out;
3383 }
3384
Daniel Jurgens32f69e42018-01-04 17:25:36 +02003385 memset(pprops, 0, sizeof(*pprops));
3386 err = mlx5_ib_query_port(&dev->ib_dev, port, pprops);
3387 if (err) {
3388 mlx5_ib_warn(dev, "query_port %d failed %d\n",
3389 port, err);
3390 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03003391 }
3392
Daniel Jurgens32f69e42018-01-04 17:25:36 +02003393 dev->mdev->port_caps[port - 1].pkey_table_len =
3394 dprops->max_pkeys;
3395 dev->mdev->port_caps[port - 1].gid_table_len =
3396 pprops->gid_tbl_len;
3397 mlx5_ib_dbg(dev, "port %d: pkey_table_len %d, gid_table_len %d\n",
3398 port, dprops->max_pkeys, pprops->gid_tbl_len);
3399
Eli Cohene126ba92013-07-07 17:25:49 +03003400out:
3401 kfree(pprops);
3402 kfree(dprops);
3403
3404 return err;
3405}
3406
3407static void destroy_umrc_res(struct mlx5_ib_dev *dev)
3408{
3409 int err;
3410
3411 err = mlx5_mr_cache_cleanup(dev);
3412 if (err)
3413 mlx5_ib_warn(dev, "mr cache cleanup failed\n");
3414
3415 mlx5_ib_destroy_qp(dev->umrc.qp);
Christoph Hellwigadd08d72016-03-03 09:38:22 +01003416 ib_free_cq(dev->umrc.cq);
Eli Cohene126ba92013-07-07 17:25:49 +03003417 ib_dealloc_pd(dev->umrc.pd);
3418}
3419
3420enum {
3421 MAX_UMR_WR = 128,
3422};
3423
3424static int create_umr_res(struct mlx5_ib_dev *dev)
3425{
3426 struct ib_qp_init_attr *init_attr = NULL;
3427 struct ib_qp_attr *attr = NULL;
3428 struct ib_pd *pd;
3429 struct ib_cq *cq;
3430 struct ib_qp *qp;
Eli Cohene126ba92013-07-07 17:25:49 +03003431 int ret;
3432
3433 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
3434 init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL);
3435 if (!attr || !init_attr) {
3436 ret = -ENOMEM;
3437 goto error_0;
3438 }
3439
Christoph Hellwiged082d32016-09-05 12:56:17 +02003440 pd = ib_alloc_pd(&dev->ib_dev, 0);
Eli Cohene126ba92013-07-07 17:25:49 +03003441 if (IS_ERR(pd)) {
3442 mlx5_ib_dbg(dev, "Couldn't create PD for sync UMR QP\n");
3443 ret = PTR_ERR(pd);
3444 goto error_0;
3445 }
3446
Christoph Hellwigadd08d72016-03-03 09:38:22 +01003447 cq = ib_alloc_cq(&dev->ib_dev, NULL, 128, 0, IB_POLL_SOFTIRQ);
Eli Cohene126ba92013-07-07 17:25:49 +03003448 if (IS_ERR(cq)) {
3449 mlx5_ib_dbg(dev, "Couldn't create CQ for sync UMR QP\n");
3450 ret = PTR_ERR(cq);
3451 goto error_2;
3452 }
Eli Cohene126ba92013-07-07 17:25:49 +03003453
3454 init_attr->send_cq = cq;
3455 init_attr->recv_cq = cq;
3456 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
3457 init_attr->cap.max_send_wr = MAX_UMR_WR;
3458 init_attr->cap.max_send_sge = 1;
3459 init_attr->qp_type = MLX5_IB_QPT_REG_UMR;
3460 init_attr->port_num = 1;
3461 qp = mlx5_ib_create_qp(pd, init_attr, NULL);
3462 if (IS_ERR(qp)) {
3463 mlx5_ib_dbg(dev, "Couldn't create sync UMR QP\n");
3464 ret = PTR_ERR(qp);
3465 goto error_3;
3466 }
3467 qp->device = &dev->ib_dev;
3468 qp->real_qp = qp;
3469 qp->uobject = NULL;
3470 qp->qp_type = MLX5_IB_QPT_REG_UMR;
Majd Dibbiny31fde032017-10-30 14:23:13 +02003471 qp->send_cq = init_attr->send_cq;
3472 qp->recv_cq = init_attr->recv_cq;
Eli Cohene126ba92013-07-07 17:25:49 +03003473
3474 attr->qp_state = IB_QPS_INIT;
3475 attr->port_num = 1;
3476 ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_PKEY_INDEX |
3477 IB_QP_PORT, NULL);
3478 if (ret) {
3479 mlx5_ib_dbg(dev, "Couldn't modify UMR QP\n");
3480 goto error_4;
3481 }
3482
3483 memset(attr, 0, sizeof(*attr));
3484 attr->qp_state = IB_QPS_RTR;
3485 attr->path_mtu = IB_MTU_256;
3486
3487 ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
3488 if (ret) {
3489 mlx5_ib_dbg(dev, "Couldn't modify umr QP to rtr\n");
3490 goto error_4;
3491 }
3492
3493 memset(attr, 0, sizeof(*attr));
3494 attr->qp_state = IB_QPS_RTS;
3495 ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
3496 if (ret) {
3497 mlx5_ib_dbg(dev, "Couldn't modify umr QP to rts\n");
3498 goto error_4;
3499 }
3500
3501 dev->umrc.qp = qp;
3502 dev->umrc.cq = cq;
Eli Cohene126ba92013-07-07 17:25:49 +03003503 dev->umrc.pd = pd;
3504
3505 sema_init(&dev->umrc.sem, MAX_UMR_WR);
3506 ret = mlx5_mr_cache_init(dev);
3507 if (ret) {
3508 mlx5_ib_warn(dev, "mr cache init failed %d\n", ret);
3509 goto error_4;
3510 }
3511
3512 kfree(attr);
3513 kfree(init_attr);
3514
3515 return 0;
3516
3517error_4:
3518 mlx5_ib_destroy_qp(qp);
3519
3520error_3:
Christoph Hellwigadd08d72016-03-03 09:38:22 +01003521 ib_free_cq(cq);
Eli Cohene126ba92013-07-07 17:25:49 +03003522
3523error_2:
Eli Cohene126ba92013-07-07 17:25:49 +03003524 ib_dealloc_pd(pd);
3525
3526error_0:
3527 kfree(attr);
3528 kfree(init_attr);
3529 return ret;
3530}
3531
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03003532static u8 mlx5_get_umr_fence(u8 umr_fence_cap)
3533{
3534 switch (umr_fence_cap) {
3535 case MLX5_CAP_UMR_FENCE_NONE:
3536 return MLX5_FENCE_MODE_NONE;
3537 case MLX5_CAP_UMR_FENCE_SMALL:
3538 return MLX5_FENCE_MODE_INITIATOR_SMALL;
3539 default:
3540 return MLX5_FENCE_MODE_STRONG_ORDERING;
3541 }
3542}
3543
Eli Cohene126ba92013-07-07 17:25:49 +03003544static int create_dev_resources(struct mlx5_ib_resources *devr)
3545{
3546 struct ib_srq_init_attr attr;
3547 struct mlx5_ib_dev *dev;
Matan Barakbcf4c1e2015-06-11 16:35:20 +03003548 struct ib_cq_init_attr cq_attr = {.cqe = 1};
Haggai Eran7722f472016-02-29 15:45:07 +02003549 int port;
Eli Cohene126ba92013-07-07 17:25:49 +03003550 int ret = 0;
3551
3552 dev = container_of(devr, struct mlx5_ib_dev, devr);
3553
Haggai Erand16e91d2016-02-29 15:45:05 +02003554 mutex_init(&devr->mutex);
3555
Eli Cohene126ba92013-07-07 17:25:49 +03003556 devr->p0 = mlx5_ib_alloc_pd(&dev->ib_dev, NULL, NULL);
3557 if (IS_ERR(devr->p0)) {
3558 ret = PTR_ERR(devr->p0);
3559 goto error0;
3560 }
3561 devr->p0->device = &dev->ib_dev;
3562 devr->p0->uobject = NULL;
3563 atomic_set(&devr->p0->usecnt, 0);
3564
Matan Barakbcf4c1e2015-06-11 16:35:20 +03003565 devr->c0 = mlx5_ib_create_cq(&dev->ib_dev, &cq_attr, NULL, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03003566 if (IS_ERR(devr->c0)) {
3567 ret = PTR_ERR(devr->c0);
3568 goto error1;
3569 }
3570 devr->c0->device = &dev->ib_dev;
3571 devr->c0->uobject = NULL;
3572 devr->c0->comp_handler = NULL;
3573 devr->c0->event_handler = NULL;
3574 devr->c0->cq_context = NULL;
3575 atomic_set(&devr->c0->usecnt, 0);
3576
3577 devr->x0 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
3578 if (IS_ERR(devr->x0)) {
3579 ret = PTR_ERR(devr->x0);
3580 goto error2;
3581 }
3582 devr->x0->device = &dev->ib_dev;
3583 devr->x0->inode = NULL;
3584 atomic_set(&devr->x0->usecnt, 0);
3585 mutex_init(&devr->x0->tgt_qp_mutex);
3586 INIT_LIST_HEAD(&devr->x0->tgt_qp_list);
3587
3588 devr->x1 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
3589 if (IS_ERR(devr->x1)) {
3590 ret = PTR_ERR(devr->x1);
3591 goto error3;
3592 }
3593 devr->x1->device = &dev->ib_dev;
3594 devr->x1->inode = NULL;
3595 atomic_set(&devr->x1->usecnt, 0);
3596 mutex_init(&devr->x1->tgt_qp_mutex);
3597 INIT_LIST_HEAD(&devr->x1->tgt_qp_list);
3598
3599 memset(&attr, 0, sizeof(attr));
3600 attr.attr.max_sge = 1;
3601 attr.attr.max_wr = 1;
3602 attr.srq_type = IB_SRQT_XRC;
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03003603 attr.ext.cq = devr->c0;
Eli Cohene126ba92013-07-07 17:25:49 +03003604 attr.ext.xrc.xrcd = devr->x0;
3605
3606 devr->s0 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
3607 if (IS_ERR(devr->s0)) {
3608 ret = PTR_ERR(devr->s0);
3609 goto error4;
3610 }
3611 devr->s0->device = &dev->ib_dev;
3612 devr->s0->pd = devr->p0;
3613 devr->s0->uobject = NULL;
3614 devr->s0->event_handler = NULL;
3615 devr->s0->srq_context = NULL;
3616 devr->s0->srq_type = IB_SRQT_XRC;
3617 devr->s0->ext.xrc.xrcd = devr->x0;
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03003618 devr->s0->ext.cq = devr->c0;
Eli Cohene126ba92013-07-07 17:25:49 +03003619 atomic_inc(&devr->s0->ext.xrc.xrcd->usecnt);
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03003620 atomic_inc(&devr->s0->ext.cq->usecnt);
Eli Cohene126ba92013-07-07 17:25:49 +03003621 atomic_inc(&devr->p0->usecnt);
3622 atomic_set(&devr->s0->usecnt, 0);
3623
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03003624 memset(&attr, 0, sizeof(attr));
3625 attr.attr.max_sge = 1;
3626 attr.attr.max_wr = 1;
3627 attr.srq_type = IB_SRQT_BASIC;
3628 devr->s1 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
3629 if (IS_ERR(devr->s1)) {
3630 ret = PTR_ERR(devr->s1);
3631 goto error5;
3632 }
3633 devr->s1->device = &dev->ib_dev;
3634 devr->s1->pd = devr->p0;
3635 devr->s1->uobject = NULL;
3636 devr->s1->event_handler = NULL;
3637 devr->s1->srq_context = NULL;
3638 devr->s1->srq_type = IB_SRQT_BASIC;
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03003639 devr->s1->ext.cq = devr->c0;
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03003640 atomic_inc(&devr->p0->usecnt);
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03003641 atomic_set(&devr->s1->usecnt, 0);
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03003642
Haggai Eran7722f472016-02-29 15:45:07 +02003643 for (port = 0; port < ARRAY_SIZE(devr->ports); ++port) {
3644 INIT_WORK(&devr->ports[port].pkey_change_work,
3645 pkey_change_handler);
3646 devr->ports[port].devr = devr;
3647 }
3648
Eli Cohene126ba92013-07-07 17:25:49 +03003649 return 0;
3650
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03003651error5:
3652 mlx5_ib_destroy_srq(devr->s0);
Eli Cohene126ba92013-07-07 17:25:49 +03003653error4:
3654 mlx5_ib_dealloc_xrcd(devr->x1);
3655error3:
3656 mlx5_ib_dealloc_xrcd(devr->x0);
3657error2:
3658 mlx5_ib_destroy_cq(devr->c0);
3659error1:
3660 mlx5_ib_dealloc_pd(devr->p0);
3661error0:
3662 return ret;
3663}
3664
3665static void destroy_dev_resources(struct mlx5_ib_resources *devr)
3666{
Haggai Eran7722f472016-02-29 15:45:07 +02003667 struct mlx5_ib_dev *dev =
3668 container_of(devr, struct mlx5_ib_dev, devr);
3669 int port;
3670
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03003671 mlx5_ib_destroy_srq(devr->s1);
Eli Cohene126ba92013-07-07 17:25:49 +03003672 mlx5_ib_destroy_srq(devr->s0);
3673 mlx5_ib_dealloc_xrcd(devr->x0);
3674 mlx5_ib_dealloc_xrcd(devr->x1);
3675 mlx5_ib_destroy_cq(devr->c0);
3676 mlx5_ib_dealloc_pd(devr->p0);
Haggai Eran7722f472016-02-29 15:45:07 +02003677
3678 /* Make sure no change P_Key work items are still executing */
3679 for (port = 0; port < dev->num_ports; ++port)
3680 cancel_work_sync(&devr->ports[port].pkey_change_work);
Eli Cohene126ba92013-07-07 17:25:49 +03003681}
3682
Achiad Shochate53505a2015-12-23 18:47:25 +02003683static u32 get_core_cap_flags(struct ib_device *ibdev)
3684{
3685 struct mlx5_ib_dev *dev = to_mdev(ibdev);
3686 enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, 1);
3687 u8 l3_type_cap = MLX5_CAP_ROCE(dev->mdev, l3_type);
3688 u8 roce_version_cap = MLX5_CAP_ROCE(dev->mdev, roce_version);
Daniel Jurgens85c7c012018-01-04 17:25:43 +02003689 bool raw_support = !mlx5_core_mp_enabled(dev->mdev);
Achiad Shochate53505a2015-12-23 18:47:25 +02003690 u32 ret = 0;
3691
3692 if (ll == IB_LINK_LAYER_INFINIBAND)
3693 return RDMA_CORE_PORT_IBA_IB;
3694
Daniel Jurgens85c7c012018-01-04 17:25:43 +02003695 if (raw_support)
3696 ret = RDMA_CORE_PORT_RAW_PACKET;
Or Gerlitz72cd5712017-01-24 13:02:36 +02003697
Achiad Shochate53505a2015-12-23 18:47:25 +02003698 if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV4_CAP))
Or Gerlitz72cd5712017-01-24 13:02:36 +02003699 return ret;
Achiad Shochate53505a2015-12-23 18:47:25 +02003700
3701 if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV6_CAP))
Or Gerlitz72cd5712017-01-24 13:02:36 +02003702 return ret;
Achiad Shochate53505a2015-12-23 18:47:25 +02003703
3704 if (roce_version_cap & MLX5_ROCE_VERSION_1_CAP)
3705 ret |= RDMA_CORE_PORT_IBA_ROCE;
3706
3707 if (roce_version_cap & MLX5_ROCE_VERSION_2_CAP)
3708 ret |= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
3709
3710 return ret;
3711}
3712
Ira Weiny77386132015-05-13 20:02:58 -04003713static int mlx5_port_immutable(struct ib_device *ibdev, u8 port_num,
3714 struct ib_port_immutable *immutable)
3715{
3716 struct ib_port_attr attr;
Or Gerlitzca5b91d2016-11-27 16:51:36 +02003717 struct mlx5_ib_dev *dev = to_mdev(ibdev);
3718 enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, port_num);
Ira Weiny77386132015-05-13 20:02:58 -04003719 int err;
3720
Or Gerlitzc4550c62017-01-24 13:02:39 +02003721 immutable->core_cap_flags = get_core_cap_flags(ibdev);
3722
3723 err = ib_query_port(ibdev, port_num, &attr);
Ira Weiny77386132015-05-13 20:02:58 -04003724 if (err)
3725 return err;
3726
3727 immutable->pkey_tbl_len = attr.pkey_tbl_len;
3728 immutable->gid_tbl_len = attr.gid_tbl_len;
Achiad Shochate53505a2015-12-23 18:47:25 +02003729 immutable->core_cap_flags = get_core_cap_flags(ibdev);
Or Gerlitzca5b91d2016-11-27 16:51:36 +02003730 if ((ll == IB_LINK_LAYER_INFINIBAND) || MLX5_CAP_GEN(dev->mdev, roce))
3731 immutable->max_mad_size = IB_MGMT_MAD_SIZE;
Ira Weiny77386132015-05-13 20:02:58 -04003732
3733 return 0;
3734}
3735
Leon Romanovsky9abb0d12017-06-27 16:49:53 +03003736static void get_dev_fw_str(struct ib_device *ibdev, char *str)
Ira Weinyc7342822016-06-15 02:22:01 -04003737{
3738 struct mlx5_ib_dev *dev =
3739 container_of(ibdev, struct mlx5_ib_dev, ib_dev);
Leon Romanovsky9abb0d12017-06-27 16:49:53 +03003740 snprintf(str, IB_FW_VERSION_NAME_MAX, "%d.%d.%04d",
3741 fw_rev_maj(dev->mdev), fw_rev_min(dev->mdev),
3742 fw_rev_sub(dev->mdev));
Ira Weinyc7342822016-06-15 02:22:01 -04003743}
3744
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003745static int mlx5_eth_lag_init(struct mlx5_ib_dev *dev)
Aviv Heller9ef9c642016-09-18 20:48:01 +03003746{
3747 struct mlx5_core_dev *mdev = dev->mdev;
3748 struct mlx5_flow_namespace *ns = mlx5_get_flow_namespace(mdev,
3749 MLX5_FLOW_NAMESPACE_LAG);
3750 struct mlx5_flow_table *ft;
3751 int err;
3752
3753 if (!ns || !mlx5_lag_is_active(mdev))
3754 return 0;
3755
3756 err = mlx5_cmd_create_vport_lag(mdev);
3757 if (err)
3758 return err;
3759
3760 ft = mlx5_create_lag_demux_flow_table(ns, 0, 0);
3761 if (IS_ERR(ft)) {
3762 err = PTR_ERR(ft);
3763 goto err_destroy_vport_lag;
3764 }
3765
3766 dev->flow_db.lag_demux_ft = ft;
3767 return 0;
3768
3769err_destroy_vport_lag:
3770 mlx5_cmd_destroy_vport_lag(mdev);
3771 return err;
3772}
3773
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003774static void mlx5_eth_lag_cleanup(struct mlx5_ib_dev *dev)
Aviv Heller9ef9c642016-09-18 20:48:01 +03003775{
3776 struct mlx5_core_dev *mdev = dev->mdev;
3777
3778 if (dev->flow_db.lag_demux_ft) {
3779 mlx5_destroy_flow_table(dev->flow_db.lag_demux_ft);
3780 dev->flow_db.lag_demux_ft = NULL;
3781
3782 mlx5_cmd_destroy_vport_lag(mdev);
3783 }
3784}
3785
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +02003786static int mlx5_add_netdev_notifier(struct mlx5_ib_dev *dev, u8 port_num)
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003787{
Achiad Shochate53505a2015-12-23 18:47:25 +02003788 int err;
3789
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +02003790 dev->roce[port_num].nb.notifier_call = mlx5_netdev_event;
3791 err = register_netdevice_notifier(&dev->roce[port_num].nb);
Aviv Heller5ec8c832016-09-18 20:48:00 +03003792 if (err) {
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +02003793 dev->roce[port_num].nb.notifier_call = NULL;
Achiad Shochate53505a2015-12-23 18:47:25 +02003794 return err;
Aviv Heller5ec8c832016-09-18 20:48:00 +03003795 }
Achiad Shochate53505a2015-12-23 18:47:25 +02003796
Or Gerlitzd012f5d2016-11-27 16:51:34 +02003797 return 0;
3798}
Achiad Shochate53505a2015-12-23 18:47:25 +02003799
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +02003800static void mlx5_remove_netdev_notifier(struct mlx5_ib_dev *dev, u8 port_num)
Eli Cohene126ba92013-07-07 17:25:49 +03003801{
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +02003802 if (dev->roce[port_num].nb.notifier_call) {
3803 unregister_netdevice_notifier(&dev->roce[port_num].nb);
3804 dev->roce[port_num].nb.notifier_call = NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03003805 }
3806}
3807
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +02003808static int mlx5_enable_eth(struct mlx5_ib_dev *dev, u8 port_num)
Eli Cohene126ba92013-07-07 17:25:49 +03003809{
Eli Cohene126ba92013-07-07 17:25:49 +03003810 int err;
3811
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +02003812 err = mlx5_add_netdev_notifier(dev, port_num);
Or Gerlitzd012f5d2016-11-27 16:51:34 +02003813 if (err)
Achiad Shochate53505a2015-12-23 18:47:25 +02003814 return err;
Achiad Shochate53505a2015-12-23 18:47:25 +02003815
Or Gerlitzca5b91d2016-11-27 16:51:36 +02003816 if (MLX5_CAP_GEN(dev->mdev, roce)) {
3817 err = mlx5_nic_vport_enable_roce(dev->mdev);
3818 if (err)
3819 goto err_unregister_netdevice_notifier;
3820 }
Achiad Shochate53505a2015-12-23 18:47:25 +02003821
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003822 err = mlx5_eth_lag_init(dev);
Aviv Heller9ef9c642016-09-18 20:48:01 +03003823 if (err)
3824 goto err_disable_roce;
3825
Achiad Shochate53505a2015-12-23 18:47:25 +02003826 return 0;
3827
Aviv Heller9ef9c642016-09-18 20:48:01 +03003828err_disable_roce:
Or Gerlitzca5b91d2016-11-27 16:51:36 +02003829 if (MLX5_CAP_GEN(dev->mdev, roce))
3830 mlx5_nic_vport_disable_roce(dev->mdev);
Aviv Heller9ef9c642016-09-18 20:48:01 +03003831
Achiad Shochate53505a2015-12-23 18:47:25 +02003832err_unregister_netdevice_notifier:
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +02003833 mlx5_remove_netdev_notifier(dev, port_num);
Achiad Shochate53505a2015-12-23 18:47:25 +02003834 return err;
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003835}
3836
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003837static void mlx5_disable_eth(struct mlx5_ib_dev *dev)
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003838{
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003839 mlx5_eth_lag_cleanup(dev);
Or Gerlitzca5b91d2016-11-27 16:51:36 +02003840 if (MLX5_CAP_GEN(dev->mdev, roce))
3841 mlx5_nic_vport_disable_roce(dev->mdev);
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003842}
3843
Parav Pandite1f24a72017-04-16 07:29:29 +03003844struct mlx5_ib_counter {
Kamal Heib7c16f472017-01-18 15:25:09 +02003845 const char *name;
3846 size_t offset;
3847};
3848
3849#define INIT_Q_COUNTER(_name) \
3850 { .name = #_name, .offset = MLX5_BYTE_OFF(query_q_counter_out, _name)}
3851
Parav Pandite1f24a72017-04-16 07:29:29 +03003852static const struct mlx5_ib_counter basic_q_cnts[] = {
Kamal Heib7c16f472017-01-18 15:25:09 +02003853 INIT_Q_COUNTER(rx_write_requests),
3854 INIT_Q_COUNTER(rx_read_requests),
3855 INIT_Q_COUNTER(rx_atomic_requests),
3856 INIT_Q_COUNTER(out_of_buffer),
3857};
3858
Parav Pandite1f24a72017-04-16 07:29:29 +03003859static const struct mlx5_ib_counter out_of_seq_q_cnts[] = {
Kamal Heib7c16f472017-01-18 15:25:09 +02003860 INIT_Q_COUNTER(out_of_sequence),
3861};
3862
Parav Pandite1f24a72017-04-16 07:29:29 +03003863static const struct mlx5_ib_counter retrans_q_cnts[] = {
Kamal Heib7c16f472017-01-18 15:25:09 +02003864 INIT_Q_COUNTER(duplicate_request),
3865 INIT_Q_COUNTER(rnr_nak_retry_err),
3866 INIT_Q_COUNTER(packet_seq_err),
3867 INIT_Q_COUNTER(implied_nak_seq_err),
3868 INIT_Q_COUNTER(local_ack_timeout_err),
3869};
3870
Parav Pandite1f24a72017-04-16 07:29:29 +03003871#define INIT_CONG_COUNTER(_name) \
3872 { .name = #_name, .offset = \
3873 MLX5_BYTE_OFF(query_cong_statistics_out, _name ## _high)}
3874
3875static const struct mlx5_ib_counter cong_cnts[] = {
3876 INIT_CONG_COUNTER(rp_cnp_ignored),
3877 INIT_CONG_COUNTER(rp_cnp_handled),
3878 INIT_CONG_COUNTER(np_ecn_marked_roce_packets),
3879 INIT_CONG_COUNTER(np_cnp_sent),
3880};
3881
Parav Pandit58dcb602017-06-19 07:19:37 +03003882static const struct mlx5_ib_counter extended_err_cnts[] = {
3883 INIT_Q_COUNTER(resp_local_length_error),
3884 INIT_Q_COUNTER(resp_cqe_error),
3885 INIT_Q_COUNTER(req_cqe_error),
3886 INIT_Q_COUNTER(req_remote_invalid_request),
3887 INIT_Q_COUNTER(req_remote_access_errors),
3888 INIT_Q_COUNTER(resp_remote_access_errors),
3889 INIT_Q_COUNTER(resp_cqe_flush_error),
3890 INIT_Q_COUNTER(req_cqe_flush_error),
3891};
3892
Parav Pandite1f24a72017-04-16 07:29:29 +03003893static void mlx5_ib_dealloc_counters(struct mlx5_ib_dev *dev)
Mark Bloch0837e862016-06-17 15:10:55 +03003894{
Daniel Jurgensaac44922018-01-04 17:25:40 +02003895 int i;
Mark Bloch0837e862016-06-17 15:10:55 +03003896
Kamal Heib7c16f472017-01-18 15:25:09 +02003897 for (i = 0; i < dev->num_ports; i++) {
Daniel Jurgensaac44922018-01-04 17:25:40 +02003898 if (dev->port[i].cnts.set_id)
3899 mlx5_core_dealloc_q_counter(dev->mdev,
3900 dev->port[i].cnts.set_id);
Parav Pandite1f24a72017-04-16 07:29:29 +03003901 kfree(dev->port[i].cnts.names);
3902 kfree(dev->port[i].cnts.offsets);
Kamal Heib7c16f472017-01-18 15:25:09 +02003903 }
3904}
3905
Parav Pandite1f24a72017-04-16 07:29:29 +03003906static int __mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev,
3907 struct mlx5_ib_counters *cnts)
Kamal Heib7c16f472017-01-18 15:25:09 +02003908{
3909 u32 num_counters;
3910
3911 num_counters = ARRAY_SIZE(basic_q_cnts);
3912
3913 if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt))
3914 num_counters += ARRAY_SIZE(out_of_seq_q_cnts);
3915
3916 if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters))
3917 num_counters += ARRAY_SIZE(retrans_q_cnts);
Parav Pandit58dcb602017-06-19 07:19:37 +03003918
3919 if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters))
3920 num_counters += ARRAY_SIZE(extended_err_cnts);
3921
Parav Pandite1f24a72017-04-16 07:29:29 +03003922 cnts->num_q_counters = num_counters;
Kamal Heib7c16f472017-01-18 15:25:09 +02003923
Parav Pandite1f24a72017-04-16 07:29:29 +03003924 if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
3925 cnts->num_cong_counters = ARRAY_SIZE(cong_cnts);
3926 num_counters += ARRAY_SIZE(cong_cnts);
3927 }
3928
3929 cnts->names = kcalloc(num_counters, sizeof(cnts->names), GFP_KERNEL);
3930 if (!cnts->names)
Kamal Heib7c16f472017-01-18 15:25:09 +02003931 return -ENOMEM;
3932
Parav Pandite1f24a72017-04-16 07:29:29 +03003933 cnts->offsets = kcalloc(num_counters,
3934 sizeof(cnts->offsets), GFP_KERNEL);
3935 if (!cnts->offsets)
Kamal Heib7c16f472017-01-18 15:25:09 +02003936 goto err_names;
3937
Kamal Heib7c16f472017-01-18 15:25:09 +02003938 return 0;
3939
3940err_names:
Parav Pandite1f24a72017-04-16 07:29:29 +03003941 kfree(cnts->names);
Daniel Jurgensaac44922018-01-04 17:25:40 +02003942 cnts->names = NULL;
Kamal Heib7c16f472017-01-18 15:25:09 +02003943 return -ENOMEM;
3944}
3945
Parav Pandite1f24a72017-04-16 07:29:29 +03003946static void mlx5_ib_fill_counters(struct mlx5_ib_dev *dev,
3947 const char **names,
3948 size_t *offsets)
Kamal Heib7c16f472017-01-18 15:25:09 +02003949{
3950 int i;
3951 int j = 0;
3952
3953 for (i = 0; i < ARRAY_SIZE(basic_q_cnts); i++, j++) {
3954 names[j] = basic_q_cnts[i].name;
3955 offsets[j] = basic_q_cnts[i].offset;
3956 }
3957
3958 if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt)) {
3959 for (i = 0; i < ARRAY_SIZE(out_of_seq_q_cnts); i++, j++) {
3960 names[j] = out_of_seq_q_cnts[i].name;
3961 offsets[j] = out_of_seq_q_cnts[i].offset;
3962 }
3963 }
3964
3965 if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters)) {
3966 for (i = 0; i < ARRAY_SIZE(retrans_q_cnts); i++, j++) {
3967 names[j] = retrans_q_cnts[i].name;
3968 offsets[j] = retrans_q_cnts[i].offset;
3969 }
3970 }
Parav Pandite1f24a72017-04-16 07:29:29 +03003971
Parav Pandit58dcb602017-06-19 07:19:37 +03003972 if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters)) {
3973 for (i = 0; i < ARRAY_SIZE(extended_err_cnts); i++, j++) {
3974 names[j] = extended_err_cnts[i].name;
3975 offsets[j] = extended_err_cnts[i].offset;
3976 }
3977 }
3978
Parav Pandite1f24a72017-04-16 07:29:29 +03003979 if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
3980 for (i = 0; i < ARRAY_SIZE(cong_cnts); i++, j++) {
3981 names[j] = cong_cnts[i].name;
3982 offsets[j] = cong_cnts[i].offset;
3983 }
3984 }
Mark Bloch0837e862016-06-17 15:10:55 +03003985}
3986
Parav Pandite1f24a72017-04-16 07:29:29 +03003987static int mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev)
Mark Bloch0837e862016-06-17 15:10:55 +03003988{
Daniel Jurgensaac44922018-01-04 17:25:40 +02003989 int err = 0;
Mark Bloch0837e862016-06-17 15:10:55 +03003990 int i;
Mark Bloch0837e862016-06-17 15:10:55 +03003991
3992 for (i = 0; i < dev->num_ports; i++) {
Daniel Jurgensaac44922018-01-04 17:25:40 +02003993 err = __mlx5_ib_alloc_counters(dev, &dev->port[i].cnts);
3994 if (err)
3995 goto err_alloc;
Kamal Heib7c16f472017-01-18 15:25:09 +02003996
Daniel Jurgensaac44922018-01-04 17:25:40 +02003997 mlx5_ib_fill_counters(dev, dev->port[i].cnts.names,
3998 dev->port[i].cnts.offsets);
3999
4000 err = mlx5_core_alloc_q_counter(dev->mdev,
4001 &dev->port[i].cnts.set_id);
4002 if (err) {
Mark Bloch0837e862016-06-17 15:10:55 +03004003 mlx5_ib_warn(dev,
4004 "couldn't allocate queue counter for port %d, err %d\n",
Daniel Jurgensaac44922018-01-04 17:25:40 +02004005 i + 1, err);
4006 goto err_alloc;
Mark Bloch0837e862016-06-17 15:10:55 +03004007 }
Daniel Jurgensaac44922018-01-04 17:25:40 +02004008 dev->port[i].cnts.set_id_valid = true;
Mark Bloch0837e862016-06-17 15:10:55 +03004009 }
4010
4011 return 0;
4012
Daniel Jurgensaac44922018-01-04 17:25:40 +02004013err_alloc:
4014 mlx5_ib_dealloc_counters(dev);
4015 return err;
Mark Bloch0837e862016-06-17 15:10:55 +03004016}
4017
Mark Bloch0ad17a82016-06-17 15:10:56 +03004018static struct rdma_hw_stats *mlx5_ib_alloc_hw_stats(struct ib_device *ibdev,
4019 u8 port_num)
4020{
Kamal Heib7c16f472017-01-18 15:25:09 +02004021 struct mlx5_ib_dev *dev = to_mdev(ibdev);
4022 struct mlx5_ib_port *port = &dev->port[port_num - 1];
Mark Bloch0ad17a82016-06-17 15:10:56 +03004023
4024 /* We support only per port stats */
4025 if (port_num == 0)
4026 return NULL;
4027
Parav Pandite1f24a72017-04-16 07:29:29 +03004028 return rdma_alloc_hw_stats_struct(port->cnts.names,
4029 port->cnts.num_q_counters +
4030 port->cnts.num_cong_counters,
Mark Bloch0ad17a82016-06-17 15:10:56 +03004031 RDMA_HW_STATS_DEFAULT_LIFESPAN);
4032}
4033
Daniel Jurgensaac44922018-01-04 17:25:40 +02004034static int mlx5_ib_query_q_counters(struct mlx5_core_dev *mdev,
Parav Pandite1f24a72017-04-16 07:29:29 +03004035 struct mlx5_ib_port *port,
4036 struct rdma_hw_stats *stats)
4037{
4038 int outlen = MLX5_ST_SZ_BYTES(query_q_counter_out);
4039 void *out;
4040 __be32 val;
4041 int ret, i;
4042
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03004043 out = kvzalloc(outlen, GFP_KERNEL);
Parav Pandite1f24a72017-04-16 07:29:29 +03004044 if (!out)
4045 return -ENOMEM;
4046
Daniel Jurgensaac44922018-01-04 17:25:40 +02004047 ret = mlx5_core_query_q_counter(mdev,
Parav Pandite1f24a72017-04-16 07:29:29 +03004048 port->cnts.set_id, 0,
4049 out, outlen);
4050 if (ret)
4051 goto free;
4052
4053 for (i = 0; i < port->cnts.num_q_counters; i++) {
4054 val = *(__be32 *)(out + port->cnts.offsets[i]);
4055 stats->value[i] = (u64)be32_to_cpu(val);
4056 }
4057
4058free:
4059 kvfree(out);
4060 return ret;
4061}
4062
Mark Bloch0ad17a82016-06-17 15:10:56 +03004063static int mlx5_ib_get_hw_stats(struct ib_device *ibdev,
4064 struct rdma_hw_stats *stats,
Kamal Heib7c16f472017-01-18 15:25:09 +02004065 u8 port_num, int index)
Mark Bloch0ad17a82016-06-17 15:10:56 +03004066{
4067 struct mlx5_ib_dev *dev = to_mdev(ibdev);
Kamal Heib7c16f472017-01-18 15:25:09 +02004068 struct mlx5_ib_port *port = &dev->port[port_num - 1];
Daniel Jurgensaac44922018-01-04 17:25:40 +02004069 struct mlx5_core_dev *mdev;
Parav Pandite1f24a72017-04-16 07:29:29 +03004070 int ret, num_counters;
Daniel Jurgensaac44922018-01-04 17:25:40 +02004071 u8 mdev_port_num;
Mark Bloch0ad17a82016-06-17 15:10:56 +03004072
Kamal Heib7c16f472017-01-18 15:25:09 +02004073 if (!stats)
Parav Pandite1f24a72017-04-16 07:29:29 +03004074 return -EINVAL;
Mark Bloch0ad17a82016-06-17 15:10:56 +03004075
Daniel Jurgensaac44922018-01-04 17:25:40 +02004076 num_counters = port->cnts.num_q_counters + port->cnts.num_cong_counters;
4077
4078 /* q_counters are per IB device, query the master mdev */
4079 ret = mlx5_ib_query_q_counters(dev->mdev, port, stats);
Mark Bloch0ad17a82016-06-17 15:10:56 +03004080 if (ret)
Parav Pandite1f24a72017-04-16 07:29:29 +03004081 return ret;
Mark Bloch0ad17a82016-06-17 15:10:56 +03004082
Parav Pandite1f24a72017-04-16 07:29:29 +03004083 if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
Daniel Jurgensaac44922018-01-04 17:25:40 +02004084 mdev = mlx5_ib_get_native_port_mdev(dev, port_num,
4085 &mdev_port_num);
4086 if (!mdev) {
4087 /* If port is not affiliated yet, its in down state
4088 * which doesn't have any counters yet, so it would be
4089 * zero. So no need to read from the HCA.
4090 */
4091 goto done;
4092 }
Majd Dibbiny71a0ff62017-12-21 17:38:26 +02004093 ret = mlx5_lag_query_cong_counters(dev->mdev,
4094 stats->value +
4095 port->cnts.num_q_counters,
4096 port->cnts.num_cong_counters,
4097 port->cnts.offsets +
4098 port->cnts.num_q_counters);
Daniel Jurgensaac44922018-01-04 17:25:40 +02004099
4100 mlx5_ib_put_native_port_mdev(dev, port_num);
Parav Pandite1f24a72017-04-16 07:29:29 +03004101 if (ret)
4102 return ret;
Mark Bloch0ad17a82016-06-17 15:10:56 +03004103 }
Kamal Heib7c16f472017-01-18 15:25:09 +02004104
Daniel Jurgensaac44922018-01-04 17:25:40 +02004105done:
Parav Pandite1f24a72017-04-16 07:29:29 +03004106 return num_counters;
Mark Bloch0ad17a82016-06-17 15:10:56 +03004107}
4108
Niranjana Vishwanathapura8e959602017-06-30 13:14:46 -07004109static void mlx5_ib_free_rdma_netdev(struct net_device *netdev)
4110{
4111 return mlx5_rdma_netdev_free(netdev);
4112}
4113
Erez Shitrit693dfd52017-04-27 17:01:34 +03004114static struct net_device*
4115mlx5_ib_alloc_rdma_netdev(struct ib_device *hca,
4116 u8 port_num,
4117 enum rdma_netdev_t type,
4118 const char *name,
4119 unsigned char name_assign_type,
4120 void (*setup)(struct net_device *))
4121{
Niranjana Vishwanathapura8e959602017-06-30 13:14:46 -07004122 struct net_device *netdev;
4123 struct rdma_netdev *rn;
4124
Erez Shitrit693dfd52017-04-27 17:01:34 +03004125 if (type != RDMA_NETDEV_IPOIB)
4126 return ERR_PTR(-EOPNOTSUPP);
4127
Niranjana Vishwanathapura8e959602017-06-30 13:14:46 -07004128 netdev = mlx5_rdma_netdev_alloc(to_mdev(hca)->mdev, hca,
4129 name, setup);
4130 if (likely(!IS_ERR_OR_NULL(netdev))) {
4131 rn = netdev_priv(netdev);
4132 rn->free_rdma_netdev = mlx5_ib_free_rdma_netdev;
4133 }
4134 return netdev;
Erez Shitrit693dfd52017-04-27 17:01:34 +03004135}
4136
Maor Gottliebfe248c32017-05-30 10:29:14 +03004137static void delay_drop_debugfs_cleanup(struct mlx5_ib_dev *dev)
4138{
4139 if (!dev->delay_drop.dbg)
4140 return;
4141 debugfs_remove_recursive(dev->delay_drop.dbg->dir_debugfs);
4142 kfree(dev->delay_drop.dbg);
4143 dev->delay_drop.dbg = NULL;
4144}
4145
Maor Gottlieb03404e82017-05-30 10:29:13 +03004146static void cancel_delay_drop(struct mlx5_ib_dev *dev)
4147{
4148 if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
4149 return;
4150
4151 cancel_work_sync(&dev->delay_drop.delay_drop_work);
Maor Gottliebfe248c32017-05-30 10:29:14 +03004152 delay_drop_debugfs_cleanup(dev);
4153}
4154
4155static ssize_t delay_drop_timeout_read(struct file *filp, char __user *buf,
4156 size_t count, loff_t *pos)
4157{
4158 struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
4159 char lbuf[20];
4160 int len;
4161
4162 len = snprintf(lbuf, sizeof(lbuf), "%u\n", delay_drop->timeout);
4163 return simple_read_from_buffer(buf, count, pos, lbuf, len);
4164}
4165
4166static ssize_t delay_drop_timeout_write(struct file *filp, const char __user *buf,
4167 size_t count, loff_t *pos)
4168{
4169 struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
4170 u32 timeout;
4171 u32 var;
4172
4173 if (kstrtouint_from_user(buf, count, 0, &var))
4174 return -EFAULT;
4175
4176 timeout = min_t(u32, roundup(var, 100), MLX5_MAX_DELAY_DROP_TIMEOUT_MS *
4177 1000);
4178 if (timeout != var)
4179 mlx5_ib_dbg(delay_drop->dev, "Round delay drop timeout to %u usec\n",
4180 timeout);
4181
4182 delay_drop->timeout = timeout;
4183
4184 return count;
4185}
4186
4187static const struct file_operations fops_delay_drop_timeout = {
4188 .owner = THIS_MODULE,
4189 .open = simple_open,
4190 .write = delay_drop_timeout_write,
4191 .read = delay_drop_timeout_read,
4192};
4193
4194static int delay_drop_debugfs_init(struct mlx5_ib_dev *dev)
4195{
4196 struct mlx5_ib_dbg_delay_drop *dbg;
4197
4198 if (!mlx5_debugfs_root)
4199 return 0;
4200
4201 dbg = kzalloc(sizeof(*dbg), GFP_KERNEL);
4202 if (!dbg)
4203 return -ENOMEM;
4204
Sudip Mukherjeecbafad82017-09-18 12:28:48 +01004205 dev->delay_drop.dbg = dbg;
4206
Maor Gottliebfe248c32017-05-30 10:29:14 +03004207 dbg->dir_debugfs =
4208 debugfs_create_dir("delay_drop",
4209 dev->mdev->priv.dbg_root);
4210 if (!dbg->dir_debugfs)
Sudip Mukherjeecbafad82017-09-18 12:28:48 +01004211 goto out_debugfs;
Maor Gottliebfe248c32017-05-30 10:29:14 +03004212
4213 dbg->events_cnt_debugfs =
4214 debugfs_create_atomic_t("num_timeout_events", 0400,
4215 dbg->dir_debugfs,
4216 &dev->delay_drop.events_cnt);
4217 if (!dbg->events_cnt_debugfs)
4218 goto out_debugfs;
4219
4220 dbg->rqs_cnt_debugfs =
4221 debugfs_create_atomic_t("num_rqs", 0400,
4222 dbg->dir_debugfs,
4223 &dev->delay_drop.rqs_cnt);
4224 if (!dbg->rqs_cnt_debugfs)
4225 goto out_debugfs;
4226
4227 dbg->timeout_debugfs =
4228 debugfs_create_file("timeout", 0600,
4229 dbg->dir_debugfs,
4230 &dev->delay_drop,
4231 &fops_delay_drop_timeout);
4232 if (!dbg->timeout_debugfs)
4233 goto out_debugfs;
4234
4235 return 0;
4236
4237out_debugfs:
4238 delay_drop_debugfs_cleanup(dev);
4239 return -ENOMEM;
Maor Gottlieb03404e82017-05-30 10:29:13 +03004240}
4241
4242static void init_delay_drop(struct mlx5_ib_dev *dev)
4243{
4244 if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
4245 return;
4246
4247 mutex_init(&dev->delay_drop.lock);
4248 dev->delay_drop.dev = dev;
4249 dev->delay_drop.activate = false;
4250 dev->delay_drop.timeout = MLX5_MAX_DELAY_DROP_TIMEOUT_MS * 1000;
4251 INIT_WORK(&dev->delay_drop.delay_drop_work, delay_drop_handler);
Maor Gottliebfe248c32017-05-30 10:29:14 +03004252 atomic_set(&dev->delay_drop.rqs_cnt, 0);
4253 atomic_set(&dev->delay_drop.events_cnt, 0);
4254
4255 if (delay_drop_debugfs_init(dev))
4256 mlx5_ib_warn(dev, "Failed to init delay drop debugfs\n");
Maor Gottlieb03404e82017-05-30 10:29:13 +03004257}
4258
Leon Romanovsky84305d712017-08-17 15:50:53 +03004259static const struct cpumask *
4260mlx5_ib_get_vector_affinity(struct ib_device *ibdev, int comp_vector)
Sagi Grimberg40b24402017-07-13 11:09:42 +03004261{
4262 struct mlx5_ib_dev *dev = to_mdev(ibdev);
4263
4264 return mlx5_get_vector_affinity(dev->mdev, comp_vector);
4265}
4266
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004267/* The mlx5_ib_multiport_mutex should be held when calling this function */
4268static void mlx5_ib_unbind_slave_port(struct mlx5_ib_dev *ibdev,
4269 struct mlx5_ib_multiport_info *mpi)
4270{
4271 u8 port_num = mlx5_core_native_port_num(mpi->mdev) - 1;
4272 struct mlx5_ib_port *port = &ibdev->port[port_num];
4273 int comps;
4274 int err;
4275 int i;
4276
Parav Pandita9e546e2018-01-04 17:25:39 +02004277 mlx5_ib_cleanup_cong_debugfs(ibdev, port_num);
4278
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004279 spin_lock(&port->mp.mpi_lock);
4280 if (!mpi->ibdev) {
4281 spin_unlock(&port->mp.mpi_lock);
4282 return;
4283 }
4284 mpi->ibdev = NULL;
4285
4286 spin_unlock(&port->mp.mpi_lock);
4287 mlx5_remove_netdev_notifier(ibdev, port_num);
4288 spin_lock(&port->mp.mpi_lock);
4289
4290 comps = mpi->mdev_refcnt;
4291 if (comps) {
4292 mpi->unaffiliate = true;
4293 init_completion(&mpi->unref_comp);
4294 spin_unlock(&port->mp.mpi_lock);
4295
4296 for (i = 0; i < comps; i++)
4297 wait_for_completion(&mpi->unref_comp);
4298
4299 spin_lock(&port->mp.mpi_lock);
4300 mpi->unaffiliate = false;
4301 }
4302
4303 port->mp.mpi = NULL;
4304
4305 list_add_tail(&mpi->list, &mlx5_ib_unaffiliated_port_list);
4306
4307 spin_unlock(&port->mp.mpi_lock);
4308
4309 err = mlx5_nic_vport_unaffiliate_multiport(mpi->mdev);
4310
4311 mlx5_ib_dbg(ibdev, "unaffiliated port %d\n", port_num + 1);
4312 /* Log an error, still needed to cleanup the pointers and add
4313 * it back to the list.
4314 */
4315 if (err)
4316 mlx5_ib_err(ibdev, "Failed to unaffiliate port %u\n",
4317 port_num + 1);
4318
4319 ibdev->roce[port_num].last_port_state = IB_PORT_DOWN;
4320}
4321
4322/* The mlx5_ib_multiport_mutex should be held when calling this function */
4323static bool mlx5_ib_bind_slave_port(struct mlx5_ib_dev *ibdev,
4324 struct mlx5_ib_multiport_info *mpi)
4325{
4326 u8 port_num = mlx5_core_native_port_num(mpi->mdev) - 1;
4327 int err;
4328
4329 spin_lock(&ibdev->port[port_num].mp.mpi_lock);
4330 if (ibdev->port[port_num].mp.mpi) {
4331 mlx5_ib_warn(ibdev, "port %d already affiliated.\n",
4332 port_num + 1);
4333 spin_unlock(&ibdev->port[port_num].mp.mpi_lock);
4334 return false;
4335 }
4336
4337 ibdev->port[port_num].mp.mpi = mpi;
4338 mpi->ibdev = ibdev;
4339 spin_unlock(&ibdev->port[port_num].mp.mpi_lock);
4340
4341 err = mlx5_nic_vport_affiliate_multiport(ibdev->mdev, mpi->mdev);
4342 if (err)
4343 goto unbind;
4344
4345 err = get_port_caps(ibdev, mlx5_core_native_port_num(mpi->mdev));
4346 if (err)
4347 goto unbind;
4348
4349 err = mlx5_add_netdev_notifier(ibdev, port_num);
4350 if (err) {
4351 mlx5_ib_err(ibdev, "failed adding netdev notifier for port %u\n",
4352 port_num + 1);
4353 goto unbind;
4354 }
4355
Parav Pandita9e546e2018-01-04 17:25:39 +02004356 err = mlx5_ib_init_cong_debugfs(ibdev, port_num);
4357 if (err)
4358 goto unbind;
4359
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004360 return true;
4361
4362unbind:
4363 mlx5_ib_unbind_slave_port(ibdev, mpi);
4364 return false;
4365}
4366
4367static int mlx5_ib_init_multiport_master(struct mlx5_ib_dev *dev)
4368{
4369 int port_num = mlx5_core_native_port_num(dev->mdev) - 1;
4370 enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev,
4371 port_num + 1);
4372 struct mlx5_ib_multiport_info *mpi;
4373 int err;
4374 int i;
4375
4376 if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
4377 return 0;
4378
4379 err = mlx5_query_nic_vport_system_image_guid(dev->mdev,
4380 &dev->sys_image_guid);
4381 if (err)
4382 return err;
4383
4384 err = mlx5_nic_vport_enable_roce(dev->mdev);
4385 if (err)
4386 return err;
4387
4388 mutex_lock(&mlx5_ib_multiport_mutex);
4389 for (i = 0; i < dev->num_ports; i++) {
4390 bool bound = false;
4391
4392 /* build a stub multiport info struct for the native port. */
4393 if (i == port_num) {
4394 mpi = kzalloc(sizeof(*mpi), GFP_KERNEL);
4395 if (!mpi) {
4396 mutex_unlock(&mlx5_ib_multiport_mutex);
4397 mlx5_nic_vport_disable_roce(dev->mdev);
4398 return -ENOMEM;
4399 }
4400
4401 mpi->is_master = true;
4402 mpi->mdev = dev->mdev;
4403 mpi->sys_image_guid = dev->sys_image_guid;
4404 dev->port[i].mp.mpi = mpi;
4405 mpi->ibdev = dev;
4406 mpi = NULL;
4407 continue;
4408 }
4409
4410 list_for_each_entry(mpi, &mlx5_ib_unaffiliated_port_list,
4411 list) {
4412 if (dev->sys_image_guid == mpi->sys_image_guid &&
4413 (mlx5_core_native_port_num(mpi->mdev) - 1) == i) {
4414 bound = mlx5_ib_bind_slave_port(dev, mpi);
4415 }
4416
4417 if (bound) {
4418 dev_dbg(&mpi->mdev->pdev->dev, "removing port from unaffiliated list.\n");
4419 mlx5_ib_dbg(dev, "port %d bound\n", i + 1);
4420 list_del(&mpi->list);
4421 break;
4422 }
4423 }
4424 if (!bound) {
4425 get_port_caps(dev, i + 1);
4426 mlx5_ib_dbg(dev, "no free port found for port %d\n",
4427 i + 1);
4428 }
4429 }
4430
4431 list_add_tail(&dev->ib_dev_list, &mlx5_ib_dev_list);
4432 mutex_unlock(&mlx5_ib_multiport_mutex);
4433 return err;
4434}
4435
4436static void mlx5_ib_cleanup_multiport_master(struct mlx5_ib_dev *dev)
4437{
4438 int port_num = mlx5_core_native_port_num(dev->mdev) - 1;
4439 enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev,
4440 port_num + 1);
4441 int i;
4442
4443 if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
4444 return;
4445
4446 mutex_lock(&mlx5_ib_multiport_mutex);
4447 for (i = 0; i < dev->num_ports; i++) {
4448 if (dev->port[i].mp.mpi) {
4449 /* Destroy the native port stub */
4450 if (i == port_num) {
4451 kfree(dev->port[i].mp.mpi);
4452 dev->port[i].mp.mpi = NULL;
4453 } else {
4454 mlx5_ib_dbg(dev, "unbinding port_num: %d\n", i + 1);
4455 mlx5_ib_unbind_slave_port(dev, dev->port[i].mp.mpi);
4456 }
4457 }
4458 }
4459
4460 mlx5_ib_dbg(dev, "removing from devlist\n");
4461 list_del(&dev->ib_dev_list);
4462 mutex_unlock(&mlx5_ib_multiport_mutex);
4463
4464 mlx5_nic_vport_disable_roce(dev->mdev);
4465}
4466
Mark Bloch16c19752018-01-01 13:06:58 +02004467static void mlx5_ib_stage_init_cleanup(struct mlx5_ib_dev *dev)
Eli Cohene126ba92013-07-07 17:25:49 +03004468{
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004469 mlx5_ib_cleanup_multiport_master(dev);
Mark Bloch3cc297d2018-01-01 13:07:03 +02004470#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
4471 cleanup_srcu_struct(&dev->mr_srcu);
4472#endif
Mark Bloch16c19752018-01-01 13:06:58 +02004473 kfree(dev->port);
4474}
4475
4476static int mlx5_ib_stage_init_init(struct mlx5_ib_dev *dev)
4477{
4478 struct mlx5_core_dev *mdev = dev->mdev;
Aviv Heller4babcf92016-09-18 20:48:03 +03004479 const char *name;
Eli Cohene126ba92013-07-07 17:25:49 +03004480 int err;
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004481 int i;
Eli Cohene126ba92013-07-07 17:25:49 +03004482
Daniel Jurgens508562d2018-01-04 17:25:34 +02004483 dev->port = kcalloc(dev->num_ports, sizeof(*dev->port),
Mark Bloch0837e862016-06-17 15:10:55 +03004484 GFP_KERNEL);
4485 if (!dev->port)
Mark Bloch16c19752018-01-01 13:06:58 +02004486 return -ENOMEM;
Mark Bloch0837e862016-06-17 15:10:55 +03004487
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004488 for (i = 0; i < dev->num_ports; i++) {
4489 spin_lock_init(&dev->port[i].mp.mpi_lock);
4490 rwlock_init(&dev->roce[i].netdev_lock);
4491 }
4492
4493 err = mlx5_ib_init_multiport_master(dev);
Eli Cohene126ba92013-07-07 17:25:49 +03004494 if (err)
Mark Bloch0837e862016-06-17 15:10:55 +03004495 goto err_free_port;
Eli Cohene126ba92013-07-07 17:25:49 +03004496
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004497 if (!mlx5_core_mp_enabled(mdev)) {
4498 int i;
4499
4500 for (i = 1; i <= dev->num_ports; i++) {
4501 err = get_port_caps(dev, i);
4502 if (err)
4503 break;
4504 }
4505 } else {
4506 err = get_port_caps(dev, mlx5_core_native_port_num(mdev));
4507 }
4508 if (err)
4509 goto err_mp;
4510
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03004511 if (mlx5_use_mad_ifc(dev))
4512 get_ext_port_caps(dev);
Eli Cohene126ba92013-07-07 17:25:49 +03004513
Aviv Heller4babcf92016-09-18 20:48:03 +03004514 if (!mlx5_lag_is_active(mdev))
4515 name = "mlx5_%d";
4516 else
4517 name = "mlx5_bond_%d";
4518
4519 strlcpy(dev->ib_dev.name, name, IB_DEVICE_NAME_MAX);
Eli Cohene126ba92013-07-07 17:25:49 +03004520 dev->ib_dev.owner = THIS_MODULE;
4521 dev->ib_dev.node_type = RDMA_NODE_IB_CA;
Sagi Grimbergc6790aa2015-09-24 10:34:23 +03004522 dev->ib_dev.local_dma_lkey = 0 /* not supported for now */;
Daniel Jurgens508562d2018-01-04 17:25:34 +02004523 dev->ib_dev.phys_port_cnt = dev->num_ports;
Saeed Mahameed233d05d2015-04-02 17:07:32 +03004524 dev->ib_dev.num_comp_vectors =
4525 dev->mdev->priv.eq_table.num_comp_vectors;
Bart Van Assche9b0c2892017-01-20 13:04:21 -08004526 dev->ib_dev.dev.parent = &mdev->pdev->dev;
Eli Cohene126ba92013-07-07 17:25:49 +03004527
Mark Bloch3cc297d2018-01-01 13:07:03 +02004528 mutex_init(&dev->flow_db.lock);
4529 mutex_init(&dev->cap_mask_mutex);
4530 INIT_LIST_HEAD(&dev->qp_list);
4531 spin_lock_init(&dev->reset_flow_resource_lock);
4532
4533#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
4534 err = init_srcu_struct(&dev->mr_srcu);
4535 if (err)
4536 goto err_free_port;
4537#endif
4538
Mark Bloch16c19752018-01-01 13:06:58 +02004539 return 0;
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004540err_mp:
4541 mlx5_ib_cleanup_multiport_master(dev);
Mark Bloch16c19752018-01-01 13:06:58 +02004542
4543err_free_port:
4544 kfree(dev->port);
4545
4546 return -ENOMEM;
4547}
4548
4549static int mlx5_ib_stage_caps_init(struct mlx5_ib_dev *dev)
4550{
4551 struct mlx5_core_dev *mdev = dev->mdev;
Mark Bloch16c19752018-01-01 13:06:58 +02004552 int err;
4553
Eli Cohene126ba92013-07-07 17:25:49 +03004554 dev->ib_dev.uverbs_abi_ver = MLX5_IB_UVERBS_ABI_VERSION;
4555 dev->ib_dev.uverbs_cmd_mask =
4556 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
4557 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
4558 (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
4559 (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
4560 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
Moni Shoua41c450f2016-11-23 08:23:26 +02004561 (1ull << IB_USER_VERBS_CMD_CREATE_AH) |
4562 (1ull << IB_USER_VERBS_CMD_DESTROY_AH) |
Eli Cohene126ba92013-07-07 17:25:49 +03004563 (1ull << IB_USER_VERBS_CMD_REG_MR) |
Noa Osherovich56e11d62016-02-29 16:46:51 +02004564 (1ull << IB_USER_VERBS_CMD_REREG_MR) |
Eli Cohene126ba92013-07-07 17:25:49 +03004565 (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
4566 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
4567 (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
4568 (1ull << IB_USER_VERBS_CMD_RESIZE_CQ) |
4569 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
4570 (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
4571 (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
4572 (1ull << IB_USER_VERBS_CMD_QUERY_QP) |
4573 (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
4574 (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST) |
4575 (1ull << IB_USER_VERBS_CMD_DETACH_MCAST) |
4576 (1ull << IB_USER_VERBS_CMD_CREATE_SRQ) |
4577 (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ) |
4578 (1ull << IB_USER_VERBS_CMD_QUERY_SRQ) |
4579 (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ) |
4580 (1ull << IB_USER_VERBS_CMD_CREATE_XSRQ) |
4581 (1ull << IB_USER_VERBS_CMD_OPEN_QP);
Haggai Eran1707cb42015-02-08 13:28:52 +02004582 dev->ib_dev.uverbs_ex_cmd_mask =
Matan Barakd4584dd2016-01-28 17:51:46 +02004583 (1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE) |
4584 (1ull << IB_USER_VERBS_EX_CMD_CREATE_CQ) |
Bodong Wang7d29f342016-12-01 13:43:16 +02004585 (1ull << IB_USER_VERBS_EX_CMD_CREATE_QP) |
Yonatan Cohenb0e9df62017-11-13 10:51:15 +02004586 (1ull << IB_USER_VERBS_EX_CMD_MODIFY_QP) |
4587 (1ull << IB_USER_VERBS_EX_CMD_MODIFY_CQ);
Eli Cohene126ba92013-07-07 17:25:49 +03004588
4589 dev->ib_dev.query_device = mlx5_ib_query_device;
4590 dev->ib_dev.query_port = mlx5_ib_query_port;
Achiad Shochatebd61f62015-12-23 18:47:16 +02004591 dev->ib_dev.get_link_layer = mlx5_ib_port_link_layer;
Eli Cohene126ba92013-07-07 17:25:49 +03004592 dev->ib_dev.query_gid = mlx5_ib_query_gid;
Achiad Shochat3cca2602015-12-23 18:47:23 +02004593 dev->ib_dev.add_gid = mlx5_ib_add_gid;
4594 dev->ib_dev.del_gid = mlx5_ib_del_gid;
Eli Cohene126ba92013-07-07 17:25:49 +03004595 dev->ib_dev.query_pkey = mlx5_ib_query_pkey;
4596 dev->ib_dev.modify_device = mlx5_ib_modify_device;
4597 dev->ib_dev.modify_port = mlx5_ib_modify_port;
4598 dev->ib_dev.alloc_ucontext = mlx5_ib_alloc_ucontext;
4599 dev->ib_dev.dealloc_ucontext = mlx5_ib_dealloc_ucontext;
4600 dev->ib_dev.mmap = mlx5_ib_mmap;
4601 dev->ib_dev.alloc_pd = mlx5_ib_alloc_pd;
4602 dev->ib_dev.dealloc_pd = mlx5_ib_dealloc_pd;
4603 dev->ib_dev.create_ah = mlx5_ib_create_ah;
4604 dev->ib_dev.query_ah = mlx5_ib_query_ah;
4605 dev->ib_dev.destroy_ah = mlx5_ib_destroy_ah;
4606 dev->ib_dev.create_srq = mlx5_ib_create_srq;
4607 dev->ib_dev.modify_srq = mlx5_ib_modify_srq;
4608 dev->ib_dev.query_srq = mlx5_ib_query_srq;
4609 dev->ib_dev.destroy_srq = mlx5_ib_destroy_srq;
4610 dev->ib_dev.post_srq_recv = mlx5_ib_post_srq_recv;
4611 dev->ib_dev.create_qp = mlx5_ib_create_qp;
4612 dev->ib_dev.modify_qp = mlx5_ib_modify_qp;
4613 dev->ib_dev.query_qp = mlx5_ib_query_qp;
4614 dev->ib_dev.destroy_qp = mlx5_ib_destroy_qp;
4615 dev->ib_dev.post_send = mlx5_ib_post_send;
4616 dev->ib_dev.post_recv = mlx5_ib_post_recv;
4617 dev->ib_dev.create_cq = mlx5_ib_create_cq;
4618 dev->ib_dev.modify_cq = mlx5_ib_modify_cq;
4619 dev->ib_dev.resize_cq = mlx5_ib_resize_cq;
4620 dev->ib_dev.destroy_cq = mlx5_ib_destroy_cq;
4621 dev->ib_dev.poll_cq = mlx5_ib_poll_cq;
4622 dev->ib_dev.req_notify_cq = mlx5_ib_arm_cq;
4623 dev->ib_dev.get_dma_mr = mlx5_ib_get_dma_mr;
4624 dev->ib_dev.reg_user_mr = mlx5_ib_reg_user_mr;
Noa Osherovich56e11d62016-02-29 16:46:51 +02004625 dev->ib_dev.rereg_user_mr = mlx5_ib_rereg_user_mr;
Eli Cohene126ba92013-07-07 17:25:49 +03004626 dev->ib_dev.dereg_mr = mlx5_ib_dereg_mr;
4627 dev->ib_dev.attach_mcast = mlx5_ib_mcg_attach;
4628 dev->ib_dev.detach_mcast = mlx5_ib_mcg_detach;
4629 dev->ib_dev.process_mad = mlx5_ib_process_mad;
Sagi Grimberg9bee1782015-07-30 10:32:35 +03004630 dev->ib_dev.alloc_mr = mlx5_ib_alloc_mr;
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004631 dev->ib_dev.map_mr_sg = mlx5_ib_map_mr_sg;
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02004632 dev->ib_dev.check_mr_status = mlx5_ib_check_mr_status;
Ira Weiny77386132015-05-13 20:02:58 -04004633 dev->ib_dev.get_port_immutable = mlx5_port_immutable;
Ira Weinyc7342822016-06-15 02:22:01 -04004634 dev->ib_dev.get_dev_fw_str = get_dev_fw_str;
Sagi Grimberg40b24402017-07-13 11:09:42 +03004635 dev->ib_dev.get_vector_affinity = mlx5_ib_get_vector_affinity;
Niranjana Vishwanathapura8e959602017-06-30 13:14:46 -07004636 if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads))
Alex Vesker022d0382017-06-14 09:59:06 +03004637 dev->ib_dev.alloc_rdma_netdev = mlx5_ib_alloc_rdma_netdev;
Niranjana Vishwanathapura8e959602017-06-30 13:14:46 -07004638
Eli Coheneff901d2016-03-11 22:58:42 +02004639 if (mlx5_core_is_pf(mdev)) {
4640 dev->ib_dev.get_vf_config = mlx5_ib_get_vf_config;
4641 dev->ib_dev.set_vf_link_state = mlx5_ib_set_vf_link_state;
4642 dev->ib_dev.get_vf_stats = mlx5_ib_get_vf_stats;
4643 dev->ib_dev.set_vf_guid = mlx5_ib_set_vf_guid;
4644 }
Eli Cohene126ba92013-07-07 17:25:49 +03004645
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03004646 dev->ib_dev.disassociate_ucontext = mlx5_ib_disassociate_ucontext;
4647
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03004648 dev->umr_fence = mlx5_get_umr_fence(MLX5_CAP_GEN(mdev, umr_fence));
4649
Matan Barakd2370e02016-02-29 18:05:30 +02004650 if (MLX5_CAP_GEN(mdev, imaicl)) {
4651 dev->ib_dev.alloc_mw = mlx5_ib_alloc_mw;
4652 dev->ib_dev.dealloc_mw = mlx5_ib_dealloc_mw;
4653 dev->ib_dev.uverbs_cmd_mask |=
4654 (1ull << IB_USER_VERBS_CMD_ALLOC_MW) |
4655 (1ull << IB_USER_VERBS_CMD_DEALLOC_MW);
4656 }
4657
Saeed Mahameed938fe832015-05-28 22:28:41 +03004658 if (MLX5_CAP_GEN(mdev, xrc)) {
Eli Cohene126ba92013-07-07 17:25:49 +03004659 dev->ib_dev.alloc_xrcd = mlx5_ib_alloc_xrcd;
4660 dev->ib_dev.dealloc_xrcd = mlx5_ib_dealloc_xrcd;
4661 dev->ib_dev.uverbs_cmd_mask |=
4662 (1ull << IB_USER_VERBS_CMD_OPEN_XRCD) |
4663 (1ull << IB_USER_VERBS_CMD_CLOSE_XRCD);
4664 }
4665
Yishai Hadas81e30882017-06-08 16:15:09 +03004666 dev->ib_dev.create_flow = mlx5_ib_create_flow;
4667 dev->ib_dev.destroy_flow = mlx5_ib_destroy_flow;
4668 dev->ib_dev.uverbs_ex_cmd_mask |=
4669 (1ull << IB_USER_VERBS_EX_CMD_CREATE_FLOW) |
4670 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_FLOW);
4671
Eli Cohene126ba92013-07-07 17:25:49 +03004672 err = init_node_data(dev);
4673 if (err)
Mark Bloch16c19752018-01-01 13:06:58 +02004674 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03004675
Mark Blochc8b89922018-01-01 13:07:02 +02004676 if ((MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_ETH) &&
4677 MLX5_CAP_GEN(dev->mdev, disable_local_lb))
4678 mutex_init(&dev->lb_mutex);
4679
Mark Bloch16c19752018-01-01 13:06:58 +02004680 return 0;
4681}
4682
4683static int mlx5_ib_stage_roce_init(struct mlx5_ib_dev *dev)
4684{
4685 struct mlx5_core_dev *mdev = dev->mdev;
4686 enum rdma_link_layer ll;
4687 int port_type_cap;
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004688 u8 port_num;
Mark Bloch16c19752018-01-01 13:06:58 +02004689 int err;
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +02004690 int i;
Mark Bloch16c19752018-01-01 13:06:58 +02004691
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004692 port_num = mlx5_core_native_port_num(dev->mdev) - 1;
Mark Bloch16c19752018-01-01 13:06:58 +02004693 port_type_cap = MLX5_CAP_GEN(mdev, port_type);
4694 ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
4695
Achiad Shochatfc24fc52015-12-23 18:47:17 +02004696 if (ll == IB_LINK_LAYER_ETHERNET) {
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +02004697 for (i = 0; i < dev->num_ports; i++) {
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +02004698 dev->roce[i].dev = dev;
4699 dev->roce[i].native_port_num = i + 1;
4700 dev->roce[i].last_port_state = IB_PORT_DOWN;
4701 }
4702
Mark Blochc11a2262018-01-01 13:06:59 +02004703 dev->ib_dev.get_netdev = mlx5_ib_get_netdev;
4704 dev->ib_dev.create_wq = mlx5_ib_create_wq;
4705 dev->ib_dev.modify_wq = mlx5_ib_modify_wq;
4706 dev->ib_dev.destroy_wq = mlx5_ib_destroy_wq;
4707 dev->ib_dev.create_rwq_ind_table = mlx5_ib_create_rwq_ind_table;
4708 dev->ib_dev.destroy_rwq_ind_table = mlx5_ib_destroy_rwq_ind_table;
4709 dev->ib_dev.uverbs_ex_cmd_mask |=
4710 (1ull << IB_USER_VERBS_EX_CMD_CREATE_WQ) |
4711 (1ull << IB_USER_VERBS_EX_CMD_MODIFY_WQ) |
4712 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_WQ) |
4713 (1ull << IB_USER_VERBS_EX_CMD_CREATE_RWQ_IND_TBL) |
4714 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_RWQ_IND_TBL);
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +02004715 err = mlx5_enable_eth(dev, port_num);
Achiad Shochatfc24fc52015-12-23 18:47:17 +02004716 if (err)
Mark Bloch16c19752018-01-01 13:06:58 +02004717 return err;
Achiad Shochatfc24fc52015-12-23 18:47:17 +02004718 }
4719
Mark Bloch16c19752018-01-01 13:06:58 +02004720 return 0;
4721}
Eli Cohene126ba92013-07-07 17:25:49 +03004722
Mark Bloch16c19752018-01-01 13:06:58 +02004723static void mlx5_ib_stage_roce_cleanup(struct mlx5_ib_dev *dev)
4724{
4725 struct mlx5_core_dev *mdev = dev->mdev;
4726 enum rdma_link_layer ll;
4727 int port_type_cap;
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004728 u8 port_num;
Eli Cohene126ba92013-07-07 17:25:49 +03004729
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004730 port_num = mlx5_core_native_port_num(dev->mdev) - 1;
Mark Bloch16c19752018-01-01 13:06:58 +02004731 port_type_cap = MLX5_CAP_GEN(mdev, port_type);
4732 ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
4733
4734 if (ll == IB_LINK_LAYER_ETHERNET) {
4735 mlx5_disable_eth(dev);
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +02004736 mlx5_remove_netdev_notifier(dev, port_num);
Kamal Heib45bded22017-01-18 14:10:32 +02004737 }
Mark Bloch16c19752018-01-01 13:06:58 +02004738}
Haggai Eran6aec21f2014-12-11 17:04:23 +02004739
Mark Bloch16c19752018-01-01 13:06:58 +02004740static int mlx5_ib_stage_dev_res_init(struct mlx5_ib_dev *dev)
4741{
4742 return create_dev_resources(&dev->devr);
4743}
Parav Pandit4a2da0b2017-05-30 10:05:15 +03004744
Mark Bloch16c19752018-01-01 13:06:58 +02004745static void mlx5_ib_stage_dev_res_cleanup(struct mlx5_ib_dev *dev)
4746{
4747 destroy_dev_resources(&dev->devr);
4748}
4749
4750static int mlx5_ib_stage_odp_init(struct mlx5_ib_dev *dev)
4751{
Mark Bloch07321b32018-01-01 13:07:00 +02004752 mlx5_ib_internal_fill_odp_caps(dev);
4753
Mark Bloch16c19752018-01-01 13:06:58 +02004754 return mlx5_ib_odp_init_one(dev);
4755}
4756
Mark Bloch16c19752018-01-01 13:06:58 +02004757static int mlx5_ib_stage_counters_init(struct mlx5_ib_dev *dev)
4758{
Mark Bloch5e1e7612018-01-01 13:07:01 +02004759 if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt)) {
4760 dev->ib_dev.get_hw_stats = mlx5_ib_get_hw_stats;
4761 dev->ib_dev.alloc_hw_stats = mlx5_ib_alloc_hw_stats;
4762
4763 return mlx5_ib_alloc_counters(dev);
4764 }
Mark Bloch16c19752018-01-01 13:06:58 +02004765
4766 return 0;
4767}
4768
4769static void mlx5_ib_stage_counters_cleanup(struct mlx5_ib_dev *dev)
4770{
4771 if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt))
4772 mlx5_ib_dealloc_counters(dev);
4773}
4774
4775static int mlx5_ib_stage_cong_debugfs_init(struct mlx5_ib_dev *dev)
4776{
Parav Pandita9e546e2018-01-04 17:25:39 +02004777 return mlx5_ib_init_cong_debugfs(dev,
4778 mlx5_core_native_port_num(dev->mdev) - 1);
Mark Bloch16c19752018-01-01 13:06:58 +02004779}
4780
4781static void mlx5_ib_stage_cong_debugfs_cleanup(struct mlx5_ib_dev *dev)
4782{
Parav Pandita9e546e2018-01-04 17:25:39 +02004783 mlx5_ib_cleanup_cong_debugfs(dev,
4784 mlx5_core_native_port_num(dev->mdev) - 1);
Mark Bloch16c19752018-01-01 13:06:58 +02004785}
4786
4787static int mlx5_ib_stage_uar_init(struct mlx5_ib_dev *dev)
4788{
Eli Cohen5fe9dec2017-01-03 23:55:25 +02004789 dev->mdev->priv.uar = mlx5_get_uars_page(dev->mdev);
4790 if (!dev->mdev->priv.uar)
Mark Bloch16c19752018-01-01 13:06:58 +02004791 return -ENOMEM;
4792 return 0;
4793}
4794
4795static void mlx5_ib_stage_uar_cleanup(struct mlx5_ib_dev *dev)
4796{
4797 mlx5_put_uars_page(dev->mdev, dev->mdev->priv.uar);
4798}
4799
4800static int mlx5_ib_stage_bfrag_init(struct mlx5_ib_dev *dev)
4801{
4802 int err;
Eli Cohen5fe9dec2017-01-03 23:55:25 +02004803
4804 err = mlx5_alloc_bfreg(dev->mdev, &dev->bfreg, false, false);
4805 if (err)
Mark Bloch16c19752018-01-01 13:06:58 +02004806 return err;
Eli Cohen5fe9dec2017-01-03 23:55:25 +02004807
4808 err = mlx5_alloc_bfreg(dev->mdev, &dev->fp_bfreg, false, true);
4809 if (err)
Mark Bloch16c19752018-01-01 13:06:58 +02004810 mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
Eli Cohen5fe9dec2017-01-03 23:55:25 +02004811
Mark Bloch16c19752018-01-01 13:06:58 +02004812 return err;
4813}
Mark Bloch0837e862016-06-17 15:10:55 +03004814
Mark Bloch16c19752018-01-01 13:06:58 +02004815static void mlx5_ib_stage_bfrag_cleanup(struct mlx5_ib_dev *dev)
4816{
4817 mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
4818 mlx5_free_bfreg(dev->mdev, &dev->bfreg);
4819}
Eli Cohene126ba92013-07-07 17:25:49 +03004820
Mark Bloch16c19752018-01-01 13:06:58 +02004821static int mlx5_ib_stage_ib_reg_init(struct mlx5_ib_dev *dev)
4822{
4823 return ib_register_device(&dev->ib_dev, NULL);
4824}
4825
4826static void mlx5_ib_stage_ib_reg_cleanup(struct mlx5_ib_dev *dev)
4827{
4828 ib_unregister_device(&dev->ib_dev);
4829}
4830
4831static int mlx5_ib_stage_umr_res_init(struct mlx5_ib_dev *dev)
4832{
4833 return create_umr_res(dev);
4834}
4835
4836static void mlx5_ib_stage_umr_res_cleanup(struct mlx5_ib_dev *dev)
4837{
4838 destroy_umrc_res(dev);
4839}
4840
4841static int mlx5_ib_stage_delay_drop_init(struct mlx5_ib_dev *dev)
4842{
Maor Gottlieb03404e82017-05-30 10:29:13 +03004843 init_delay_drop(dev);
4844
Mark Bloch16c19752018-01-01 13:06:58 +02004845 return 0;
4846}
4847
4848static void mlx5_ib_stage_delay_drop_cleanup(struct mlx5_ib_dev *dev)
4849{
4850 cancel_delay_drop(dev);
4851}
4852
4853static int mlx5_ib_stage_class_attr_init(struct mlx5_ib_dev *dev)
4854{
4855 int err;
4856 int i;
4857
Eli Cohene126ba92013-07-07 17:25:49 +03004858 for (i = 0; i < ARRAY_SIZE(mlx5_class_attributes); i++) {
Wei Yongjun281d1a92013-07-30 07:54:26 +08004859 err = device_create_file(&dev->ib_dev.dev,
4860 mlx5_class_attributes[i]);
4861 if (err)
Mark Bloch16c19752018-01-01 13:06:58 +02004862 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03004863 }
4864
Mark Bloch16c19752018-01-01 13:06:58 +02004865 return 0;
4866}
4867
Mark Bloch16c19752018-01-01 13:06:58 +02004868static void __mlx5_ib_remove(struct mlx5_ib_dev *dev,
4869 const struct mlx5_ib_profile *profile,
4870 int stage)
4871{
4872 /* Number of stages to cleanup */
4873 while (stage) {
4874 stage--;
4875 if (profile->stage[stage].cleanup)
4876 profile->stage[stage].cleanup(dev);
4877 }
4878
4879 ib_dealloc_device((struct ib_device *)dev);
4880}
4881
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004882static void *mlx5_ib_add_slave_port(struct mlx5_core_dev *mdev, u8 port_num);
4883
Mark Bloch16c19752018-01-01 13:06:58 +02004884static void *__mlx5_ib_add(struct mlx5_core_dev *mdev,
4885 const struct mlx5_ib_profile *profile)
4886{
4887 struct mlx5_ib_dev *dev;
4888 int err;
4889 int i;
4890
4891 printk_once(KERN_INFO "%s", mlx5_version);
4892
4893 dev = (struct mlx5_ib_dev *)ib_alloc_device(sizeof(*dev));
4894 if (!dev)
4895 return NULL;
4896
4897 dev->mdev = mdev;
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004898 dev->num_ports = max(MLX5_CAP_GEN(mdev, num_ports),
4899 MLX5_CAP_GEN(mdev, num_vhca_ports));
Mark Bloch16c19752018-01-01 13:06:58 +02004900
4901 for (i = 0; i < MLX5_IB_STAGE_MAX; i++) {
4902 if (profile->stage[i].init) {
4903 err = profile->stage[i].init(dev);
4904 if (err)
4905 goto err_out;
4906 }
4907 }
4908
4909 dev->profile = profile;
Eli Cohene126ba92013-07-07 17:25:49 +03004910 dev->ib_active = true;
4911
Jack Morgenstein9603b612014-07-28 23:30:22 +03004912 return dev;
Eli Cohene126ba92013-07-07 17:25:49 +03004913
Mark Bloch16c19752018-01-01 13:06:58 +02004914err_out:
4915 __mlx5_ib_remove(dev, profile, i);
Eli Cohene126ba92013-07-07 17:25:49 +03004916
Jack Morgenstein9603b612014-07-28 23:30:22 +03004917 return NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03004918}
4919
Mark Bloch16c19752018-01-01 13:06:58 +02004920static const struct mlx5_ib_profile pf_profile = {
4921 STAGE_CREATE(MLX5_IB_STAGE_INIT,
4922 mlx5_ib_stage_init_init,
4923 mlx5_ib_stage_init_cleanup),
4924 STAGE_CREATE(MLX5_IB_STAGE_CAPS,
4925 mlx5_ib_stage_caps_init,
4926 NULL),
4927 STAGE_CREATE(MLX5_IB_STAGE_ROCE,
4928 mlx5_ib_stage_roce_init,
4929 mlx5_ib_stage_roce_cleanup),
4930 STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES,
4931 mlx5_ib_stage_dev_res_init,
4932 mlx5_ib_stage_dev_res_cleanup),
4933 STAGE_CREATE(MLX5_IB_STAGE_ODP,
4934 mlx5_ib_stage_odp_init,
Mark Bloch3cc297d2018-01-01 13:07:03 +02004935 NULL),
Mark Bloch16c19752018-01-01 13:06:58 +02004936 STAGE_CREATE(MLX5_IB_STAGE_COUNTERS,
4937 mlx5_ib_stage_counters_init,
4938 mlx5_ib_stage_counters_cleanup),
4939 STAGE_CREATE(MLX5_IB_STAGE_CONG_DEBUGFS,
4940 mlx5_ib_stage_cong_debugfs_init,
4941 mlx5_ib_stage_cong_debugfs_cleanup),
4942 STAGE_CREATE(MLX5_IB_STAGE_UAR,
4943 mlx5_ib_stage_uar_init,
4944 mlx5_ib_stage_uar_cleanup),
4945 STAGE_CREATE(MLX5_IB_STAGE_BFREG,
4946 mlx5_ib_stage_bfrag_init,
4947 mlx5_ib_stage_bfrag_cleanup),
4948 STAGE_CREATE(MLX5_IB_STAGE_IB_REG,
4949 mlx5_ib_stage_ib_reg_init,
4950 mlx5_ib_stage_ib_reg_cleanup),
4951 STAGE_CREATE(MLX5_IB_STAGE_UMR_RESOURCES,
4952 mlx5_ib_stage_umr_res_init,
4953 mlx5_ib_stage_umr_res_cleanup),
4954 STAGE_CREATE(MLX5_IB_STAGE_DELAY_DROP,
4955 mlx5_ib_stage_delay_drop_init,
4956 mlx5_ib_stage_delay_drop_cleanup),
4957 STAGE_CREATE(MLX5_IB_STAGE_CLASS_ATTR,
4958 mlx5_ib_stage_class_attr_init,
4959 NULL),
Mark Bloch16c19752018-01-01 13:06:58 +02004960};
4961
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004962static void *mlx5_ib_add_slave_port(struct mlx5_core_dev *mdev, u8 port_num)
4963{
4964 struct mlx5_ib_multiport_info *mpi;
4965 struct mlx5_ib_dev *dev;
4966 bool bound = false;
4967 int err;
4968
4969 mpi = kzalloc(sizeof(*mpi), GFP_KERNEL);
4970 if (!mpi)
4971 return NULL;
4972
4973 mpi->mdev = mdev;
4974
4975 err = mlx5_query_nic_vport_system_image_guid(mdev,
4976 &mpi->sys_image_guid);
4977 if (err) {
4978 kfree(mpi);
4979 return NULL;
4980 }
4981
4982 mutex_lock(&mlx5_ib_multiport_mutex);
4983 list_for_each_entry(dev, &mlx5_ib_dev_list, ib_dev_list) {
4984 if (dev->sys_image_guid == mpi->sys_image_guid)
4985 bound = mlx5_ib_bind_slave_port(dev, mpi);
4986
4987 if (bound) {
4988 rdma_roce_rescan_device(&dev->ib_dev);
4989 break;
4990 }
4991 }
4992
4993 if (!bound) {
4994 list_add_tail(&mpi->list, &mlx5_ib_unaffiliated_port_list);
4995 dev_dbg(&mdev->pdev->dev, "no suitable IB device found to bind to, added to unaffiliated list.\n");
4996 } else {
4997 mlx5_ib_dbg(dev, "bound port %u\n", port_num + 1);
4998 }
4999 mutex_unlock(&mlx5_ib_multiport_mutex);
5000
5001 return mpi;
5002}
5003
Mark Bloch16c19752018-01-01 13:06:58 +02005004static void *mlx5_ib_add(struct mlx5_core_dev *mdev)
5005{
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005006 enum rdma_link_layer ll;
5007 int port_type_cap;
5008
5009 port_type_cap = MLX5_CAP_GEN(mdev, port_type);
5010 ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
5011
5012 if (mlx5_core_is_mp_slave(mdev) && ll == IB_LINK_LAYER_ETHERNET) {
5013 u8 port_num = mlx5_core_native_port_num(mdev) - 1;
5014
5015 return mlx5_ib_add_slave_port(mdev, port_num);
5016 }
5017
Mark Bloch16c19752018-01-01 13:06:58 +02005018 return __mlx5_ib_add(mdev, &pf_profile);
5019}
5020
Jack Morgenstein9603b612014-07-28 23:30:22 +03005021static void mlx5_ib_remove(struct mlx5_core_dev *mdev, void *context)
Eli Cohene126ba92013-07-07 17:25:49 +03005022{
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005023 struct mlx5_ib_multiport_info *mpi;
5024 struct mlx5_ib_dev *dev;
Haggai Eran6aec21f2014-12-11 17:04:23 +02005025
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005026 if (mlx5_core_is_mp_slave(mdev)) {
5027 mpi = context;
5028 mutex_lock(&mlx5_ib_multiport_mutex);
5029 if (mpi->ibdev)
5030 mlx5_ib_unbind_slave_port(mpi->ibdev, mpi);
5031 list_del(&mpi->list);
5032 mutex_unlock(&mlx5_ib_multiport_mutex);
5033 return;
5034 }
5035
5036 dev = context;
Mark Bloch16c19752018-01-01 13:06:58 +02005037 __mlx5_ib_remove(dev, dev->profile, MLX5_IB_STAGE_MAX);
Eli Cohene126ba92013-07-07 17:25:49 +03005038}
5039
Jack Morgenstein9603b612014-07-28 23:30:22 +03005040static struct mlx5_interface mlx5_ib_interface = {
5041 .add = mlx5_ib_add,
5042 .remove = mlx5_ib_remove,
5043 .event = mlx5_ib_event,
Artemy Kovalyovd9aaed82017-01-02 11:37:46 +02005044#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
5045 .pfault = mlx5_ib_pfault,
5046#endif
Saeed Mahameed64613d942015-04-02 17:07:34 +03005047 .protocol = MLX5_INTERFACE_PROTOCOL_IB,
Eli Cohene126ba92013-07-07 17:25:49 +03005048};
5049
5050static int __init mlx5_ib_init(void)
5051{
Haggai Eran6aec21f2014-12-11 17:04:23 +02005052 int err;
5053
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02005054 mlx5_ib_event_wq = alloc_ordered_workqueue("mlx5_ib_event_wq", 0);
5055 if (!mlx5_ib_event_wq)
5056 return -ENOMEM;
5057
Artemy Kovalyov81713d32017-01-18 16:58:11 +02005058 mlx5_ib_odp_init();
Jack Morgenstein9603b612014-07-28 23:30:22 +03005059
Haggai Eran6aec21f2014-12-11 17:04:23 +02005060 err = mlx5_register_interface(&mlx5_ib_interface);
Haggai Eran6aec21f2014-12-11 17:04:23 +02005061
5062 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03005063}
5064
5065static void __exit mlx5_ib_cleanup(void)
5066{
Jack Morgenstein9603b612014-07-28 23:30:22 +03005067 mlx5_unregister_interface(&mlx5_ib_interface);
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02005068 destroy_workqueue(mlx5_ib_event_wq);
Eli Cohene126ba92013-07-07 17:25:49 +03005069}
5070
5071module_init(mlx5_ib_init);
5072module_exit(mlx5_ib_cleanup);