blob: 5007280321b6f37c41fb8884e2947b3f365a9671 [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 = {};
Feras Daoud5c99eae2018-01-16 20:08:41 +02001582 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03001583 struct mlx5_ib_ucontext *context;
Eli Cohen2f5ff262017-01-03 23:55:21 +02001584 struct mlx5_bfreg_info *bfregi;
Eli Cohen78c0f982014-01-30 13:49:48 +02001585 int ver;
Eli Cohene126ba92013-07-07 17:25:49 +03001586 int err;
Majd Dibbinya168a41c2016-01-28 17:51:47 +02001587 size_t min_req_v2 = offsetof(struct mlx5_ib_alloc_ucontext_req_v2,
1588 max_cqe_version);
Eli Cohenb037c292017-01-03 23:55:26 +02001589 bool lib_uar_4k;
Eli Cohene126ba92013-07-07 17:25:49 +03001590
1591 if (!dev->ib_active)
1592 return ERR_PTR(-EAGAIN);
1593
Amrani, Rame0931112017-06-27 17:04:42 +03001594 if (udata->inlen == sizeof(struct mlx5_ib_alloc_ucontext_req))
Eli Cohen78c0f982014-01-30 13:49:48 +02001595 ver = 0;
Amrani, Rame0931112017-06-27 17:04:42 +03001596 else if (udata->inlen >= min_req_v2)
Eli Cohen78c0f982014-01-30 13:49:48 +02001597 ver = 2;
1598 else
1599 return ERR_PTR(-EINVAL);
1600
Amrani, Rame0931112017-06-27 17:04:42 +03001601 err = ib_copy_from_udata(&req, udata, min(udata->inlen, sizeof(req)));
Eli Cohene126ba92013-07-07 17:25:49 +03001602 if (err)
1603 return ERR_PTR(err);
1604
Matan Barakb368d7c2015-12-15 20:30:12 +02001605 if (req.flags)
Eli Cohen78c0f982014-01-30 13:49:48 +02001606 return ERR_PTR(-EINVAL);
1607
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001608 if (req.comp_mask || req.reserved0 || req.reserved1 || req.reserved2)
Matan Barakb368d7c2015-12-15 20:30:12 +02001609 return ERR_PTR(-EOPNOTSUPP);
1610
Eli Cohen2f5ff262017-01-03 23:55:21 +02001611 req.total_num_bfregs = ALIGN(req.total_num_bfregs,
1612 MLX5_NON_FP_BFREGS_PER_UAR);
1613 if (req.num_low_latency_bfregs > req.total_num_bfregs - 1)
Eli Cohene126ba92013-07-07 17:25:49 +03001614 return ERR_PTR(-EINVAL);
1615
Saeed Mahameed938fe832015-05-28 22:28:41 +03001616 resp.qp_tab_size = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp);
Noa Osherovich2cc6ad52016-06-04 15:15:33 +03001617 if (mlx5_core_is_pf(dev->mdev) && MLX5_CAP_GEN(dev->mdev, bf))
1618 resp.bf_reg_size = 1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size);
Daniel Jurgensb47bd6e2016-10-25 18:36:24 +03001619 resp.cache_line_size = cache_line_size();
Saeed Mahameed938fe832015-05-28 22:28:41 +03001620 resp.max_sq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq);
1621 resp.max_rq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq);
1622 resp.max_send_wqebb = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1623 resp.max_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1624 resp.max_srq_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz);
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001625 resp.cqe_version = min_t(__u8,
1626 (__u8)MLX5_CAP_GEN(dev->mdev, cqe_version),
1627 req.max_cqe_version);
Eli Cohen30aa60b2017-01-03 23:55:27 +02001628 resp.log_uar_size = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
1629 MLX5_ADAPTER_PAGE_SHIFT : PAGE_SHIFT;
1630 resp.num_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
1631 MLX5_CAP_GEN(dev->mdev, num_of_uars_per_page) : 1;
Matan Barakb368d7c2015-12-15 20:30:12 +02001632 resp.response_length = min(offsetof(typeof(resp), response_length) +
1633 sizeof(resp.response_length), udata->outlen);
Eli Cohene126ba92013-07-07 17:25:49 +03001634
1635 context = kzalloc(sizeof(*context), GFP_KERNEL);
1636 if (!context)
1637 return ERR_PTR(-ENOMEM);
1638
Eli Cohen30aa60b2017-01-03 23:55:27 +02001639 lib_uar_4k = req.lib_caps & MLX5_LIB_CAP_4K_UAR;
Eli Cohen2f5ff262017-01-03 23:55:21 +02001640 bfregi = &context->bfregi;
Eli Cohenb037c292017-01-03 23:55:26 +02001641
1642 /* updates req->total_num_bfregs */
Yishai Hadas31a78a52017-12-24 16:31:34 +02001643 err = calc_total_bfregs(dev, lib_uar_4k, &req, bfregi);
Eli Cohenb037c292017-01-03 23:55:26 +02001644 if (err)
1645 goto out_ctx;
1646
Eli Cohen2f5ff262017-01-03 23:55:21 +02001647 mutex_init(&bfregi->lock);
Eli Cohenb037c292017-01-03 23:55:26 +02001648 bfregi->lib_uar_4k = lib_uar_4k;
Yishai Hadas31a78a52017-12-24 16:31:34 +02001649 bfregi->count = kcalloc(bfregi->total_num_bfregs, sizeof(*bfregi->count),
Eli Cohenb037c292017-01-03 23:55:26 +02001650 GFP_KERNEL);
1651 if (!bfregi->count) {
Eli Cohene126ba92013-07-07 17:25:49 +03001652 err = -ENOMEM;
1653 goto out_ctx;
1654 }
1655
Eli Cohenb037c292017-01-03 23:55:26 +02001656 bfregi->sys_pages = kcalloc(bfregi->num_sys_pages,
1657 sizeof(*bfregi->sys_pages),
1658 GFP_KERNEL);
1659 if (!bfregi->sys_pages) {
Eli Cohene126ba92013-07-07 17:25:49 +03001660 err = -ENOMEM;
Eli Cohenb037c292017-01-03 23:55:26 +02001661 goto out_count;
Eli Cohene126ba92013-07-07 17:25:49 +03001662 }
1663
Eli Cohenb037c292017-01-03 23:55:26 +02001664 err = allocate_uars(dev, context);
1665 if (err)
1666 goto out_sys_pages;
Eli Cohene126ba92013-07-07 17:25:49 +03001667
Haggai Eranb4cfe442014-12-11 17:04:26 +02001668#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
1669 context->ibucontext.invalidate_range = &mlx5_ib_invalidate_range;
1670#endif
1671
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +02001672 context->upd_xlt_page = __get_free_page(GFP_KERNEL);
1673 if (!context->upd_xlt_page) {
1674 err = -ENOMEM;
1675 goto out_uars;
1676 }
1677 mutex_init(&context->upd_xlt_page_mutex);
1678
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001679 if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain)) {
Huy Nguyenc85023e2017-05-30 09:42:54 +03001680 err = mlx5_ib_alloc_transport_domain(dev, &context->tdn);
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001681 if (err)
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +02001682 goto out_page;
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001683 }
1684
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001685 INIT_LIST_HEAD(&context->vma_private_list);
Majd Dibbinyad9a3662017-12-24 13:54:56 +02001686 mutex_init(&context->vma_private_list_mutex);
Eli Cohene126ba92013-07-07 17:25:49 +03001687 INIT_LIST_HEAD(&context->db_page_list);
1688 mutex_init(&context->db_page_mutex);
1689
Eli Cohen2f5ff262017-01-03 23:55:21 +02001690 resp.tot_bfregs = req.total_num_bfregs;
Daniel Jurgens508562d2018-01-04 17:25:34 +02001691 resp.num_ports = dev->num_ports;
Matan Barakb368d7c2015-12-15 20:30:12 +02001692
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001693 if (field_avail(typeof(resp), cqe_version, udata->outlen))
1694 resp.response_length += sizeof(resp.cqe_version);
Matan Barakb368d7c2015-12-15 20:30:12 +02001695
Bodong Wang402ca532016-06-17 15:02:20 +03001696 if (field_avail(typeof(resp), cmds_supp_uhw, udata->outlen)) {
Moni Shoua6ad279c52016-11-23 08:23:23 +02001697 resp.cmds_supp_uhw |= MLX5_USER_CMDS_SUPP_UHW_QUERY_DEVICE |
1698 MLX5_USER_CMDS_SUPP_UHW_CREATE_AH;
Bodong Wang402ca532016-06-17 15:02:20 +03001699 resp.response_length += sizeof(resp.cmds_supp_uhw);
1700 }
1701
Or Gerlitz78984892016-11-30 20:33:33 +02001702 if (field_avail(typeof(resp), eth_min_inline, udata->outlen)) {
1703 if (mlx5_ib_port_link_layer(ibdev, 1) == IB_LINK_LAYER_ETHERNET) {
1704 mlx5_query_min_inline(dev->mdev, &resp.eth_min_inline);
1705 resp.eth_min_inline++;
1706 }
1707 resp.response_length += sizeof(resp.eth_min_inline);
1708 }
1709
Feras Daoud5c99eae2018-01-16 20:08:41 +02001710 if (field_avail(typeof(resp), clock_info_versions, udata->outlen)) {
1711 if (mdev->clock_info)
1712 resp.clock_info_versions = BIT(MLX5_IB_CLOCK_INFO_V1);
1713 resp.response_length += sizeof(resp.clock_info_versions);
1714 }
1715
Noa Osherovichbc5c6ee2016-06-04 15:15:31 +03001716 /*
1717 * We don't want to expose information from the PCI bar that is located
1718 * after 4096 bytes, so if the arch only supports larger pages, let's
1719 * pretend we don't support reading the HCA's core clock. This is also
1720 * forced by mmap function.
1721 */
Eli Cohende8d6e02017-01-03 23:55:19 +02001722 if (field_avail(typeof(resp), hca_core_clock_offset, udata->outlen)) {
1723 if (PAGE_SIZE <= 4096) {
1724 resp.comp_mask |=
1725 MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_CORE_CLOCK_OFFSET;
1726 resp.hca_core_clock_offset =
1727 offsetof(struct mlx5_init_seg, internal_timer_h) % PAGE_SIZE;
1728 }
Feras Daoud5c99eae2018-01-16 20:08:41 +02001729 resp.response_length += sizeof(resp.hca_core_clock_offset);
Matan Barakb368d7c2015-12-15 20:30:12 +02001730 }
1731
Eli Cohen30aa60b2017-01-03 23:55:27 +02001732 if (field_avail(typeof(resp), log_uar_size, udata->outlen))
1733 resp.response_length += sizeof(resp.log_uar_size);
1734
1735 if (field_avail(typeof(resp), num_uars_per_page, udata->outlen))
1736 resp.response_length += sizeof(resp.num_uars_per_page);
1737
Yishai Hadas31a78a52017-12-24 16:31:34 +02001738 if (field_avail(typeof(resp), num_dyn_bfregs, udata->outlen)) {
1739 resp.num_dyn_bfregs = bfregi->num_dyn_bfregs;
1740 resp.response_length += sizeof(resp.num_dyn_bfregs);
1741 }
1742
Matan Barakb368d7c2015-12-15 20:30:12 +02001743 err = ib_copy_to_udata(udata, &resp, resp.response_length);
Eli Cohene126ba92013-07-07 17:25:49 +03001744 if (err)
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001745 goto out_td;
Eli Cohene126ba92013-07-07 17:25:49 +03001746
Eli Cohen2f5ff262017-01-03 23:55:21 +02001747 bfregi->ver = ver;
1748 bfregi->num_low_latency_bfregs = req.num_low_latency_bfregs;
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001749 context->cqe_version = resp.cqe_version;
Eli Cohen30aa60b2017-01-03 23:55:27 +02001750 context->lib_caps = req.lib_caps;
1751 print_lib_caps(dev, context->lib_caps);
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001752
Eli Cohene126ba92013-07-07 17:25:49 +03001753 return &context->ibucontext;
1754
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001755out_td:
1756 if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
Huy Nguyenc85023e2017-05-30 09:42:54 +03001757 mlx5_ib_dealloc_transport_domain(dev, context->tdn);
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001758
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +02001759out_page:
1760 free_page(context->upd_xlt_page);
1761
Eli Cohene126ba92013-07-07 17:25:49 +03001762out_uars:
Eli Cohenb037c292017-01-03 23:55:26 +02001763 deallocate_uars(dev, context);
1764
1765out_sys_pages:
1766 kfree(bfregi->sys_pages);
1767
Eli Cohene126ba92013-07-07 17:25:49 +03001768out_count:
Eli Cohen2f5ff262017-01-03 23:55:21 +02001769 kfree(bfregi->count);
Eli Cohene126ba92013-07-07 17:25:49 +03001770
Eli Cohene126ba92013-07-07 17:25:49 +03001771out_ctx:
1772 kfree(context);
Eli Cohenb037c292017-01-03 23:55:26 +02001773
Eli Cohene126ba92013-07-07 17:25:49 +03001774 return ERR_PTR(err);
1775}
1776
1777static int mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
1778{
1779 struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1780 struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
Eli Cohenb037c292017-01-03 23:55:26 +02001781 struct mlx5_bfreg_info *bfregi;
Eli Cohene126ba92013-07-07 17:25:49 +03001782
Eli Cohenb037c292017-01-03 23:55:26 +02001783 bfregi = &context->bfregi;
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001784 if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
Huy Nguyenc85023e2017-05-30 09:42:54 +03001785 mlx5_ib_dealloc_transport_domain(dev, context->tdn);
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001786
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +02001787 free_page(context->upd_xlt_page);
Eli Cohenb037c292017-01-03 23:55:26 +02001788 deallocate_uars(dev, context);
1789 kfree(bfregi->sys_pages);
Eli Cohen2f5ff262017-01-03 23:55:21 +02001790 kfree(bfregi->count);
Eli Cohene126ba92013-07-07 17:25:49 +03001791 kfree(context);
1792
1793 return 0;
1794}
1795
Eli Cohenb037c292017-01-03 23:55:26 +02001796static phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev,
Yishai Hadas4ed131d2017-12-24 16:31:35 +02001797 int uar_idx)
Eli Cohene126ba92013-07-07 17:25:49 +03001798{
Eli Cohenb037c292017-01-03 23:55:26 +02001799 int fw_uars_per_page;
1800
1801 fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ? MLX5_UARS_IN_PAGE : 1;
1802
Yishai Hadas4ed131d2017-12-24 16:31:35 +02001803 return (pci_resource_start(dev->mdev->pdev, 0) >> PAGE_SHIFT) + uar_idx / fw_uars_per_page;
Eli Cohene126ba92013-07-07 17:25:49 +03001804}
1805
1806static int get_command(unsigned long offset)
1807{
1808 return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK;
1809}
1810
1811static int get_arg(unsigned long offset)
1812{
1813 return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1);
1814}
1815
1816static int get_index(unsigned long offset)
1817{
1818 return get_arg(offset);
1819}
1820
Yishai Hadas4ed131d2017-12-24 16:31:35 +02001821/* Index resides in an extra byte to enable larger values than 255 */
1822static int get_extended_index(unsigned long offset)
1823{
1824 return get_arg(offset) | ((offset >> 16) & 0xff) << 8;
1825}
1826
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001827static void mlx5_ib_vma_open(struct vm_area_struct *area)
1828{
1829 /* vma_open is called when a new VMA is created on top of our VMA. This
1830 * is done through either mremap flow or split_vma (usually due to
1831 * mlock, madvise, munmap, etc.) We do not support a clone of the VMA,
1832 * as this VMA is strongly hardware related. Therefore we set the
1833 * vm_ops of the newly created/cloned VMA to NULL, to prevent it from
1834 * calling us again and trying to do incorrect actions. We assume that
1835 * the original VMA size is exactly a single page, and therefore all
1836 * "splitting" operation will not happen to it.
1837 */
1838 area->vm_ops = NULL;
1839}
1840
1841static void mlx5_ib_vma_close(struct vm_area_struct *area)
1842{
1843 struct mlx5_ib_vma_private_data *mlx5_ib_vma_priv_data;
1844
1845 /* It's guaranteed that all VMAs opened on a FD are closed before the
1846 * file itself is closed, therefore no sync is needed with the regular
1847 * closing flow. (e.g. mlx5 ib_dealloc_ucontext)
1848 * However need a sync with accessing the vma as part of
1849 * mlx5_ib_disassociate_ucontext.
1850 * The close operation is usually called under mm->mmap_sem except when
1851 * process is exiting.
1852 * The exiting case is handled explicitly as part of
1853 * mlx5_ib_disassociate_ucontext.
1854 */
1855 mlx5_ib_vma_priv_data = (struct mlx5_ib_vma_private_data *)area->vm_private_data;
1856
1857 /* setting the vma context pointer to null in the mlx5_ib driver's
1858 * private data, to protect a race condition in
1859 * mlx5_ib_disassociate_ucontext().
1860 */
1861 mlx5_ib_vma_priv_data->vma = NULL;
Majd Dibbinyad9a3662017-12-24 13:54:56 +02001862 mutex_lock(mlx5_ib_vma_priv_data->vma_private_list_mutex);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001863 list_del(&mlx5_ib_vma_priv_data->list);
Majd Dibbinyad9a3662017-12-24 13:54:56 +02001864 mutex_unlock(mlx5_ib_vma_priv_data->vma_private_list_mutex);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001865 kfree(mlx5_ib_vma_priv_data);
1866}
1867
1868static const struct vm_operations_struct mlx5_ib_vm_ops = {
1869 .open = mlx5_ib_vma_open,
1870 .close = mlx5_ib_vma_close
1871};
1872
1873static int mlx5_ib_set_vma_data(struct vm_area_struct *vma,
1874 struct mlx5_ib_ucontext *ctx)
1875{
1876 struct mlx5_ib_vma_private_data *vma_prv;
1877 struct list_head *vma_head = &ctx->vma_private_list;
1878
1879 vma_prv = kzalloc(sizeof(*vma_prv), GFP_KERNEL);
1880 if (!vma_prv)
1881 return -ENOMEM;
1882
1883 vma_prv->vma = vma;
Majd Dibbinyad9a3662017-12-24 13:54:56 +02001884 vma_prv->vma_private_list_mutex = &ctx->vma_private_list_mutex;
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001885 vma->vm_private_data = vma_prv;
1886 vma->vm_ops = &mlx5_ib_vm_ops;
1887
Majd Dibbinyad9a3662017-12-24 13:54:56 +02001888 mutex_lock(&ctx->vma_private_list_mutex);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001889 list_add(&vma_prv->list, vma_head);
Majd Dibbinyad9a3662017-12-24 13:54:56 +02001890 mutex_unlock(&ctx->vma_private_list_mutex);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001891
1892 return 0;
1893}
1894
1895static void mlx5_ib_disassociate_ucontext(struct ib_ucontext *ibcontext)
1896{
1897 int ret;
1898 struct vm_area_struct *vma;
1899 struct mlx5_ib_vma_private_data *vma_private, *n;
1900 struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1901 struct task_struct *owning_process = NULL;
1902 struct mm_struct *owning_mm = NULL;
1903
1904 owning_process = get_pid_task(ibcontext->tgid, PIDTYPE_PID);
1905 if (!owning_process)
1906 return;
1907
1908 owning_mm = get_task_mm(owning_process);
1909 if (!owning_mm) {
1910 pr_info("no mm, disassociate ucontext is pending task termination\n");
1911 while (1) {
1912 put_task_struct(owning_process);
1913 usleep_range(1000, 2000);
1914 owning_process = get_pid_task(ibcontext->tgid,
1915 PIDTYPE_PID);
1916 if (!owning_process ||
1917 owning_process->state == TASK_DEAD) {
1918 pr_info("disassociate ucontext done, task was terminated\n");
1919 /* in case task was dead need to release the
1920 * task struct.
1921 */
1922 if (owning_process)
1923 put_task_struct(owning_process);
1924 return;
1925 }
1926 }
1927 }
1928
1929 /* need to protect from a race on closing the vma as part of
1930 * mlx5_ib_vma_close.
1931 */
Maor Gottliebecc7d832017-03-29 06:03:02 +03001932 down_write(&owning_mm->mmap_sem);
Majd Dibbinyad9a3662017-12-24 13:54:56 +02001933 mutex_lock(&context->vma_private_list_mutex);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001934 list_for_each_entry_safe(vma_private, n, &context->vma_private_list,
1935 list) {
1936 vma = vma_private->vma;
1937 ret = zap_vma_ptes(vma, vma->vm_start,
1938 PAGE_SIZE);
1939 WARN_ONCE(ret, "%s: zap_vma_ptes failed", __func__);
1940 /* context going to be destroyed, should
1941 * not access ops any more.
1942 */
Maor Gottlieb13776612017-03-29 06:03:03 +03001943 vma->vm_flags &= ~(VM_SHARED | VM_MAYSHARE);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001944 vma->vm_ops = NULL;
1945 list_del(&vma_private->list);
1946 kfree(vma_private);
1947 }
Majd Dibbinyad9a3662017-12-24 13:54:56 +02001948 mutex_unlock(&context->vma_private_list_mutex);
Maor Gottliebecc7d832017-03-29 06:03:02 +03001949 up_write(&owning_mm->mmap_sem);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001950 mmput(owning_mm);
1951 put_task_struct(owning_process);
1952}
1953
Guy Levi37aa5c32016-04-27 16:49:50 +03001954static inline char *mmap_cmd2str(enum mlx5_ib_mmap_cmd cmd)
1955{
1956 switch (cmd) {
1957 case MLX5_IB_MMAP_WC_PAGE:
1958 return "WC";
1959 case MLX5_IB_MMAP_REGULAR_PAGE:
1960 return "best effort WC";
1961 case MLX5_IB_MMAP_NC_PAGE:
1962 return "NC";
1963 default:
1964 return NULL;
1965 }
1966}
1967
Feras Daoud5c99eae2018-01-16 20:08:41 +02001968static int mlx5_ib_mmap_clock_info_page(struct mlx5_ib_dev *dev,
1969 struct vm_area_struct *vma,
1970 struct mlx5_ib_ucontext *context)
1971{
1972 phys_addr_t pfn;
1973 int err;
1974
1975 if (vma->vm_end - vma->vm_start != PAGE_SIZE)
1976 return -EINVAL;
1977
1978 if (get_index(vma->vm_pgoff) != MLX5_IB_CLOCK_INFO_V1)
1979 return -EOPNOTSUPP;
1980
1981 if (vma->vm_flags & VM_WRITE)
1982 return -EPERM;
1983
1984 if (!dev->mdev->clock_info_page)
1985 return -EOPNOTSUPP;
1986
1987 pfn = page_to_pfn(dev->mdev->clock_info_page);
1988 err = remap_pfn_range(vma, vma->vm_start, pfn, PAGE_SIZE,
1989 vma->vm_page_prot);
1990 if (err)
1991 return err;
1992
1993 mlx5_ib_dbg(dev, "mapped clock info at 0x%lx, PA 0x%llx\n",
1994 vma->vm_start,
1995 (unsigned long long)pfn << PAGE_SHIFT);
1996
1997 return mlx5_ib_set_vma_data(vma, context);
1998}
1999
Guy Levi37aa5c32016-04-27 16:49:50 +03002000static int uar_mmap(struct mlx5_ib_dev *dev, enum mlx5_ib_mmap_cmd cmd,
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03002001 struct vm_area_struct *vma,
2002 struct mlx5_ib_ucontext *context)
Guy Levi37aa5c32016-04-27 16:49:50 +03002003{
Eli Cohen2f5ff262017-01-03 23:55:21 +02002004 struct mlx5_bfreg_info *bfregi = &context->bfregi;
Guy Levi37aa5c32016-04-27 16:49:50 +03002005 int err;
2006 unsigned long idx;
2007 phys_addr_t pfn, pa;
2008 pgprot_t prot;
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002009 u32 bfreg_dyn_idx = 0;
2010 u32 uar_index;
2011 int dyn_uar = (cmd == MLX5_IB_MMAP_ALLOC_WC);
2012 int max_valid_idx = dyn_uar ? bfregi->num_sys_pages :
2013 bfregi->num_static_sys_pages;
Eli Cohenb037c292017-01-03 23:55:26 +02002014
2015 if (vma->vm_end - vma->vm_start != PAGE_SIZE)
2016 return -EINVAL;
2017
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002018 if (dyn_uar)
2019 idx = get_extended_index(vma->vm_pgoff) + bfregi->num_static_sys_pages;
2020 else
2021 idx = get_index(vma->vm_pgoff);
2022
2023 if (idx >= max_valid_idx) {
2024 mlx5_ib_warn(dev, "invalid uar index %lu, max=%d\n",
2025 idx, max_valid_idx);
Eli Cohenb037c292017-01-03 23:55:26 +02002026 return -EINVAL;
2027 }
Guy Levi37aa5c32016-04-27 16:49:50 +03002028
2029 switch (cmd) {
2030 case MLX5_IB_MMAP_WC_PAGE:
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002031 case MLX5_IB_MMAP_ALLOC_WC:
Guy Levi37aa5c32016-04-27 16:49:50 +03002032/* Some architectures don't support WC memory */
2033#if defined(CONFIG_X86)
2034 if (!pat_enabled())
2035 return -EPERM;
2036#elif !(defined(CONFIG_PPC) || (defined(CONFIG_ARM) && defined(CONFIG_MMU)))
2037 return -EPERM;
2038#endif
2039 /* fall through */
2040 case MLX5_IB_MMAP_REGULAR_PAGE:
2041 /* For MLX5_IB_MMAP_REGULAR_PAGE do the best effort to get WC */
2042 prot = pgprot_writecombine(vma->vm_page_prot);
2043 break;
2044 case MLX5_IB_MMAP_NC_PAGE:
2045 prot = pgprot_noncached(vma->vm_page_prot);
2046 break;
2047 default:
2048 return -EINVAL;
2049 }
2050
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002051 if (dyn_uar) {
2052 int uars_per_page;
2053
2054 uars_per_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k);
2055 bfreg_dyn_idx = idx * (uars_per_page * MLX5_NON_FP_BFREGS_PER_UAR);
2056 if (bfreg_dyn_idx >= bfregi->total_num_bfregs) {
2057 mlx5_ib_warn(dev, "invalid bfreg_dyn_idx %u, max=%u\n",
2058 bfreg_dyn_idx, bfregi->total_num_bfregs);
2059 return -EINVAL;
2060 }
2061
2062 mutex_lock(&bfregi->lock);
2063 /* Fail if uar already allocated, first bfreg index of each
2064 * page holds its count.
2065 */
2066 if (bfregi->count[bfreg_dyn_idx]) {
2067 mlx5_ib_warn(dev, "wrong offset, idx %lu is busy, bfregn=%u\n", idx, bfreg_dyn_idx);
2068 mutex_unlock(&bfregi->lock);
2069 return -EINVAL;
2070 }
2071
2072 bfregi->count[bfreg_dyn_idx]++;
2073 mutex_unlock(&bfregi->lock);
2074
2075 err = mlx5_cmd_alloc_uar(dev->mdev, &uar_index);
2076 if (err) {
2077 mlx5_ib_warn(dev, "UAR alloc failed\n");
2078 goto free_bfreg;
2079 }
2080 } else {
2081 uar_index = bfregi->sys_pages[idx];
2082 }
2083
2084 pfn = uar_index2pfn(dev, uar_index);
Guy Levi37aa5c32016-04-27 16:49:50 +03002085 mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn %pa\n", idx, &pfn);
2086
2087 vma->vm_page_prot = prot;
2088 err = io_remap_pfn_range(vma, vma->vm_start, pfn,
2089 PAGE_SIZE, vma->vm_page_prot);
2090 if (err) {
2091 mlx5_ib_err(dev, "io_remap_pfn_range failed with error=%d, vm_start=0x%lx, pfn=%pa, mmap_cmd=%s\n",
2092 err, vma->vm_start, &pfn, mmap_cmd2str(cmd));
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002093 err = -EAGAIN;
2094 goto err;
Guy Levi37aa5c32016-04-27 16:49:50 +03002095 }
2096
2097 pa = pfn << PAGE_SHIFT;
2098 mlx5_ib_dbg(dev, "mapped %s at 0x%lx, PA %pa\n", mmap_cmd2str(cmd),
2099 vma->vm_start, &pa);
2100
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002101 err = mlx5_ib_set_vma_data(vma, context);
2102 if (err)
2103 goto err;
2104
2105 if (dyn_uar)
2106 bfregi->sys_pages[idx] = uar_index;
2107 return 0;
2108
2109err:
2110 if (!dyn_uar)
2111 return err;
2112
2113 mlx5_cmd_free_uar(dev->mdev, idx);
2114
2115free_bfreg:
2116 mlx5_ib_free_bfreg(dev, bfregi, bfreg_dyn_idx);
2117
2118 return err;
Guy Levi37aa5c32016-04-27 16:49:50 +03002119}
2120
Eli Cohene126ba92013-07-07 17:25:49 +03002121static int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma)
2122{
2123 struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
2124 struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
Eli Cohene126ba92013-07-07 17:25:49 +03002125 unsigned long command;
Eli Cohene126ba92013-07-07 17:25:49 +03002126 phys_addr_t pfn;
2127
2128 command = get_command(vma->vm_pgoff);
2129 switch (command) {
Guy Levi37aa5c32016-04-27 16:49:50 +03002130 case MLX5_IB_MMAP_WC_PAGE:
2131 case MLX5_IB_MMAP_NC_PAGE:
Eli Cohene126ba92013-07-07 17:25:49 +03002132 case MLX5_IB_MMAP_REGULAR_PAGE:
Yishai Hadas4ed131d2017-12-24 16:31:35 +02002133 case MLX5_IB_MMAP_ALLOC_WC:
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03002134 return uar_mmap(dev, command, vma, context);
Eli Cohene126ba92013-07-07 17:25:49 +03002135
2136 case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES:
2137 return -ENOSYS;
2138
Matan Barakd69e3bc2015-12-15 20:30:13 +02002139 case MLX5_IB_MMAP_CORE_CLOCK:
Matan Barakd69e3bc2015-12-15 20:30:13 +02002140 if (vma->vm_end - vma->vm_start != PAGE_SIZE)
2141 return -EINVAL;
2142
Matan Barak6cbac1e2016-04-14 16:52:10 +03002143 if (vma->vm_flags & VM_WRITE)
Matan Barakd69e3bc2015-12-15 20:30:13 +02002144 return -EPERM;
2145
2146 /* Don't expose to user-space information it shouldn't have */
2147 if (PAGE_SIZE > 4096)
2148 return -EOPNOTSUPP;
2149
2150 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
2151 pfn = (dev->mdev->iseg_base +
2152 offsetof(struct mlx5_init_seg, internal_timer_h)) >>
2153 PAGE_SHIFT;
2154 if (io_remap_pfn_range(vma, vma->vm_start, pfn,
2155 PAGE_SIZE, vma->vm_page_prot))
2156 return -EAGAIN;
2157
2158 mlx5_ib_dbg(dev, "mapped internal timer at 0x%lx, PA 0x%llx\n",
2159 vma->vm_start,
2160 (unsigned long long)pfn << PAGE_SHIFT);
2161 break;
Feras Daoud5c99eae2018-01-16 20:08:41 +02002162 case MLX5_IB_MMAP_CLOCK_INFO:
2163 return mlx5_ib_mmap_clock_info_page(dev, vma, context);
Matan Barakd69e3bc2015-12-15 20:30:13 +02002164
Eli Cohene126ba92013-07-07 17:25:49 +03002165 default:
2166 return -EINVAL;
2167 }
2168
2169 return 0;
2170}
2171
Eli Cohene126ba92013-07-07 17:25:49 +03002172static struct ib_pd *mlx5_ib_alloc_pd(struct ib_device *ibdev,
2173 struct ib_ucontext *context,
2174 struct ib_udata *udata)
2175{
2176 struct mlx5_ib_alloc_pd_resp resp;
2177 struct mlx5_ib_pd *pd;
2178 int err;
2179
2180 pd = kmalloc(sizeof(*pd), GFP_KERNEL);
2181 if (!pd)
2182 return ERR_PTR(-ENOMEM);
2183
Jack Morgenstein9603b612014-07-28 23:30:22 +03002184 err = mlx5_core_alloc_pd(to_mdev(ibdev)->mdev, &pd->pdn);
Eli Cohene126ba92013-07-07 17:25:49 +03002185 if (err) {
2186 kfree(pd);
2187 return ERR_PTR(err);
2188 }
2189
2190 if (context) {
2191 resp.pdn = pd->pdn;
2192 if (ib_copy_to_udata(udata, &resp, sizeof(resp))) {
Jack Morgenstein9603b612014-07-28 23:30:22 +03002193 mlx5_core_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn);
Eli Cohene126ba92013-07-07 17:25:49 +03002194 kfree(pd);
2195 return ERR_PTR(-EFAULT);
2196 }
Eli Cohene126ba92013-07-07 17:25:49 +03002197 }
2198
2199 return &pd->ibpd;
2200}
2201
2202static int mlx5_ib_dealloc_pd(struct ib_pd *pd)
2203{
2204 struct mlx5_ib_dev *mdev = to_mdev(pd->device);
2205 struct mlx5_ib_pd *mpd = to_mpd(pd);
2206
Jack Morgenstein9603b612014-07-28 23:30:22 +03002207 mlx5_core_dealloc_pd(mdev->mdev, mpd->pdn);
Eli Cohene126ba92013-07-07 17:25:49 +03002208 kfree(mpd);
2209
2210 return 0;
2211}
2212
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002213enum {
2214 MATCH_CRITERIA_ENABLE_OUTER_BIT,
2215 MATCH_CRITERIA_ENABLE_MISC_BIT,
2216 MATCH_CRITERIA_ENABLE_INNER_BIT
2217};
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002218
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002219#define HEADER_IS_ZERO(match_criteria, headers) \
2220 !(memchr_inv(MLX5_ADDR_OF(fte_match_param, match_criteria, headers), \
2221 0, MLX5_FLD_SZ_BYTES(fte_match_param, headers))) \
2222
2223static u8 get_match_criteria_enable(u32 *match_criteria)
2224{
2225 u8 match_criteria_enable;
2226
2227 match_criteria_enable =
2228 (!HEADER_IS_ZERO(match_criteria, outer_headers)) <<
2229 MATCH_CRITERIA_ENABLE_OUTER_BIT;
2230 match_criteria_enable |=
2231 (!HEADER_IS_ZERO(match_criteria, misc_parameters)) <<
2232 MATCH_CRITERIA_ENABLE_MISC_BIT;
2233 match_criteria_enable |=
2234 (!HEADER_IS_ZERO(match_criteria, inner_headers)) <<
2235 MATCH_CRITERIA_ENABLE_INNER_BIT;
2236
2237 return match_criteria_enable;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002238}
2239
Maor Gottliebca0d4752016-08-30 16:58:35 +03002240static void set_proto(void *outer_c, void *outer_v, u8 mask, u8 val)
2241{
2242 MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_protocol, mask);
2243 MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_protocol, val);
2244}
2245
Moses Reuben2d1e6972016-11-14 19:04:52 +02002246static void set_flow_label(void *misc_c, void *misc_v, u8 mask, u8 val,
2247 bool inner)
2248{
2249 if (inner) {
2250 MLX5_SET(fte_match_set_misc,
2251 misc_c, inner_ipv6_flow_label, mask);
2252 MLX5_SET(fte_match_set_misc,
2253 misc_v, inner_ipv6_flow_label, val);
2254 } else {
2255 MLX5_SET(fte_match_set_misc,
2256 misc_c, outer_ipv6_flow_label, mask);
2257 MLX5_SET(fte_match_set_misc,
2258 misc_v, outer_ipv6_flow_label, val);
2259 }
2260}
2261
Maor Gottliebca0d4752016-08-30 16:58:35 +03002262static void set_tos(void *outer_c, void *outer_v, u8 mask, u8 val)
2263{
2264 MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_ecn, mask);
2265 MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_ecn, val);
2266 MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_dscp, mask >> 2);
2267 MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_dscp, val >> 2);
2268}
2269
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002270#define LAST_ETH_FIELD vlan_tag
2271#define LAST_IB_FIELD sl
Maor Gottliebca0d4752016-08-30 16:58:35 +03002272#define LAST_IPV4_FIELD tos
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002273#define LAST_IPV6_FIELD traffic_class
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002274#define LAST_TCP_UDP_FIELD src_port
Moses Reubenffb30d82016-11-14 19:04:50 +02002275#define LAST_TUNNEL_FIELD tunnel_id
Moses Reuben2ac693f2017-01-18 14:59:50 +02002276#define LAST_FLOW_TAG_FIELD tag_id
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002277#define LAST_DROP_FIELD size
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002278
2279/* Field is the last supported field */
2280#define FIELDS_NOT_SUPPORTED(filter, field)\
2281 memchr_inv((void *)&filter.field +\
2282 sizeof(filter.field), 0,\
2283 sizeof(filter) -\
2284 offsetof(typeof(filter), field) -\
2285 sizeof(filter.field))
2286
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002287#define IPV4_VERSION 4
2288#define IPV6_VERSION 6
2289static int parse_flow_attr(struct mlx5_core_dev *mdev, u32 *match_c,
2290 u32 *match_v, const union ib_flow_spec *ib_spec,
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002291 u32 *tag_id, bool *is_drop)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002292{
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002293 void *misc_params_c = MLX5_ADDR_OF(fte_match_param, match_c,
2294 misc_parameters);
2295 void *misc_params_v = MLX5_ADDR_OF(fte_match_param, match_v,
2296 misc_parameters);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002297 void *headers_c;
2298 void *headers_v;
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002299 int match_ipv;
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002300
Moses Reuben2d1e6972016-11-14 19:04:52 +02002301 if (ib_spec->type & IB_FLOW_SPEC_INNER) {
2302 headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
2303 inner_headers);
2304 headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
2305 inner_headers);
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002306 match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2307 ft_field_support.inner_ip_version);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002308 } else {
2309 headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
2310 outer_headers);
2311 headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
2312 outer_headers);
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002313 match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2314 ft_field_support.outer_ip_version);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002315 }
2316
2317 switch (ib_spec->type & ~IB_FLOW_SPEC_INNER) {
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002318 case IB_FLOW_SPEC_ETH:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002319 if (FIELDS_NOT_SUPPORTED(ib_spec->eth.mask, LAST_ETH_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002320 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002321
Moses Reuben2d1e6972016-11-14 19:04:52 +02002322 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002323 dmac_47_16),
2324 ib_spec->eth.mask.dst_mac);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002325 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002326 dmac_47_16),
2327 ib_spec->eth.val.dst_mac);
2328
Moses Reuben2d1e6972016-11-14 19:04:52 +02002329 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottliebee3da802016-09-12 19:16:24 +03002330 smac_47_16),
2331 ib_spec->eth.mask.src_mac);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002332 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottliebee3da802016-09-12 19:16:24 +03002333 smac_47_16),
2334 ib_spec->eth.val.src_mac);
2335
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002336 if (ib_spec->eth.mask.vlan_tag) {
Moses Reuben2d1e6972016-11-14 19:04:52 +02002337 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Mohamad Haj Yahia10543362016-10-09 16:25:43 +03002338 cvlan_tag, 1);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002339 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Mohamad Haj Yahia10543362016-10-09 16:25:43 +03002340 cvlan_tag, 1);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002341
Moses Reuben2d1e6972016-11-14 19:04:52 +02002342 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002343 first_vid, ntohs(ib_spec->eth.mask.vlan_tag));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002344 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002345 first_vid, ntohs(ib_spec->eth.val.vlan_tag));
2346
Moses Reuben2d1e6972016-11-14 19:04:52 +02002347 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002348 first_cfi,
2349 ntohs(ib_spec->eth.mask.vlan_tag) >> 12);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002350 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002351 first_cfi,
2352 ntohs(ib_spec->eth.val.vlan_tag) >> 12);
2353
Moses Reuben2d1e6972016-11-14 19:04:52 +02002354 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002355 first_prio,
2356 ntohs(ib_spec->eth.mask.vlan_tag) >> 13);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002357 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002358 first_prio,
2359 ntohs(ib_spec->eth.val.vlan_tag) >> 13);
2360 }
Moses Reuben2d1e6972016-11-14 19:04:52 +02002361 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002362 ethertype, ntohs(ib_spec->eth.mask.ether_type));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002363 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002364 ethertype, ntohs(ib_spec->eth.val.ether_type));
2365 break;
2366 case IB_FLOW_SPEC_IPV4:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002367 if (FIELDS_NOT_SUPPORTED(ib_spec->ipv4.mask, LAST_IPV4_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002368 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002369
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002370 if (match_ipv) {
2371 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2372 ip_version, 0xf);
2373 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2374 ip_version, IPV4_VERSION);
2375 } else {
2376 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2377 ethertype, 0xffff);
2378 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2379 ethertype, ETH_P_IP);
2380 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002381
Moses Reuben2d1e6972016-11-14 19:04:52 +02002382 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002383 src_ipv4_src_ipv6.ipv4_layout.ipv4),
2384 &ib_spec->ipv4.mask.src_ip,
2385 sizeof(ib_spec->ipv4.mask.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002386 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002387 src_ipv4_src_ipv6.ipv4_layout.ipv4),
2388 &ib_spec->ipv4.val.src_ip,
2389 sizeof(ib_spec->ipv4.val.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002390 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002391 dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2392 &ib_spec->ipv4.mask.dst_ip,
2393 sizeof(ib_spec->ipv4.mask.dst_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002394 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002395 dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2396 &ib_spec->ipv4.val.dst_ip,
2397 sizeof(ib_spec->ipv4.val.dst_ip));
Maor Gottliebca0d4752016-08-30 16:58:35 +03002398
Moses Reuben2d1e6972016-11-14 19:04:52 +02002399 set_tos(headers_c, headers_v,
Maor Gottliebca0d4752016-08-30 16:58:35 +03002400 ib_spec->ipv4.mask.tos, ib_spec->ipv4.val.tos);
2401
Moses Reuben2d1e6972016-11-14 19:04:52 +02002402 set_proto(headers_c, headers_v,
Maor Gottliebca0d4752016-08-30 16:58:35 +03002403 ib_spec->ipv4.mask.proto, ib_spec->ipv4.val.proto);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002404 break;
Maor Gottlieb026bae02016-06-17 15:14:51 +03002405 case IB_FLOW_SPEC_IPV6:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002406 if (FIELDS_NOT_SUPPORTED(ib_spec->ipv6.mask, LAST_IPV6_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002407 return -EOPNOTSUPP;
Maor Gottlieb026bae02016-06-17 15:14:51 +03002408
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002409 if (match_ipv) {
2410 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2411 ip_version, 0xf);
2412 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2413 ip_version, IPV6_VERSION);
2414 } else {
2415 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2416 ethertype, 0xffff);
2417 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2418 ethertype, ETH_P_IPV6);
2419 }
Maor Gottlieb026bae02016-06-17 15:14:51 +03002420
Moses Reuben2d1e6972016-11-14 19:04:52 +02002421 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb026bae02016-06-17 15:14:51 +03002422 src_ipv4_src_ipv6.ipv6_layout.ipv6),
2423 &ib_spec->ipv6.mask.src_ip,
2424 sizeof(ib_spec->ipv6.mask.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002425 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb026bae02016-06-17 15:14:51 +03002426 src_ipv4_src_ipv6.ipv6_layout.ipv6),
2427 &ib_spec->ipv6.val.src_ip,
2428 sizeof(ib_spec->ipv6.val.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002429 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb026bae02016-06-17 15:14:51 +03002430 dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2431 &ib_spec->ipv6.mask.dst_ip,
2432 sizeof(ib_spec->ipv6.mask.dst_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002433 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb026bae02016-06-17 15:14:51 +03002434 dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2435 &ib_spec->ipv6.val.dst_ip,
2436 sizeof(ib_spec->ipv6.val.dst_ip));
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002437
Moses Reuben2d1e6972016-11-14 19:04:52 +02002438 set_tos(headers_c, headers_v,
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002439 ib_spec->ipv6.mask.traffic_class,
2440 ib_spec->ipv6.val.traffic_class);
2441
Moses Reuben2d1e6972016-11-14 19:04:52 +02002442 set_proto(headers_c, headers_v,
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002443 ib_spec->ipv6.mask.next_hdr,
2444 ib_spec->ipv6.val.next_hdr);
2445
Moses Reuben2d1e6972016-11-14 19:04:52 +02002446 set_flow_label(misc_params_c, misc_params_v,
2447 ntohl(ib_spec->ipv6.mask.flow_label),
2448 ntohl(ib_spec->ipv6.val.flow_label),
2449 ib_spec->type & IB_FLOW_SPEC_INNER);
2450
Maor Gottlieb026bae02016-06-17 15:14:51 +03002451 break;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002452 case IB_FLOW_SPEC_TCP:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002453 if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
2454 LAST_TCP_UDP_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002455 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002456
Moses Reuben2d1e6972016-11-14 19:04:52 +02002457 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002458 0xff);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002459 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002460 IPPROTO_TCP);
2461
Moses Reuben2d1e6972016-11-14 19:04:52 +02002462 MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002463 ntohs(ib_spec->tcp_udp.mask.src_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002464 MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002465 ntohs(ib_spec->tcp_udp.val.src_port));
2466
Moses Reuben2d1e6972016-11-14 19:04:52 +02002467 MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002468 ntohs(ib_spec->tcp_udp.mask.dst_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002469 MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002470 ntohs(ib_spec->tcp_udp.val.dst_port));
2471 break;
2472 case IB_FLOW_SPEC_UDP:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03002473 if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
2474 LAST_TCP_UDP_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002475 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002476
Moses Reuben2d1e6972016-11-14 19:04:52 +02002477 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002478 0xff);
Moses Reuben2d1e6972016-11-14 19:04:52 +02002479 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002480 IPPROTO_UDP);
2481
Moses Reuben2d1e6972016-11-14 19:04:52 +02002482 MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002483 ntohs(ib_spec->tcp_udp.mask.src_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002484 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002485 ntohs(ib_spec->tcp_udp.val.src_port));
2486
Moses Reuben2d1e6972016-11-14 19:04:52 +02002487 MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002488 ntohs(ib_spec->tcp_udp.mask.dst_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02002489 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002490 ntohs(ib_spec->tcp_udp.val.dst_port));
2491 break;
Moses Reubenffb30d82016-11-14 19:04:50 +02002492 case IB_FLOW_SPEC_VXLAN_TUNNEL:
2493 if (FIELDS_NOT_SUPPORTED(ib_spec->tunnel.mask,
2494 LAST_TUNNEL_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02002495 return -EOPNOTSUPP;
Moses Reubenffb30d82016-11-14 19:04:50 +02002496
2497 MLX5_SET(fte_match_set_misc, misc_params_c, vxlan_vni,
2498 ntohl(ib_spec->tunnel.mask.tunnel_id));
2499 MLX5_SET(fte_match_set_misc, misc_params_v, vxlan_vni,
2500 ntohl(ib_spec->tunnel.val.tunnel_id));
2501 break;
Moses Reuben2ac693f2017-01-18 14:59:50 +02002502 case IB_FLOW_SPEC_ACTION_TAG:
2503 if (FIELDS_NOT_SUPPORTED(ib_spec->flow_tag,
2504 LAST_FLOW_TAG_FIELD))
2505 return -EOPNOTSUPP;
2506 if (ib_spec->flow_tag.tag_id >= BIT(24))
2507 return -EINVAL;
2508
2509 *tag_id = ib_spec->flow_tag.tag_id;
2510 break;
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002511 case IB_FLOW_SPEC_ACTION_DROP:
2512 if (FIELDS_NOT_SUPPORTED(ib_spec->drop,
2513 LAST_DROP_FIELD))
2514 return -EOPNOTSUPP;
2515 *is_drop = true;
2516 break;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002517 default:
2518 return -EINVAL;
2519 }
2520
2521 return 0;
2522}
2523
2524/* If a flow could catch both multicast and unicast packets,
2525 * it won't fall into the multicast flow steering table and this rule
2526 * could steal other multicast packets.
2527 */
Yishai Hadasa550ddf2017-08-17 15:52:33 +03002528static bool flow_is_multicast_only(const struct ib_flow_attr *ib_attr)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002529{
Yishai Hadas81e30882017-06-08 16:15:09 +03002530 union ib_flow_spec *flow_spec;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002531
2532 if (ib_attr->type != IB_FLOW_ATTR_NORMAL ||
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002533 ib_attr->num_of_specs < 1)
2534 return false;
2535
Yishai Hadas81e30882017-06-08 16:15:09 +03002536 flow_spec = (union ib_flow_spec *)(ib_attr + 1);
2537 if (flow_spec->type == IB_FLOW_SPEC_IPV4) {
2538 struct ib_flow_spec_ipv4 *ipv4_spec;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002539
Yishai Hadas81e30882017-06-08 16:15:09 +03002540 ipv4_spec = (struct ib_flow_spec_ipv4 *)flow_spec;
2541 if (ipv4_is_multicast(ipv4_spec->val.dst_ip))
2542 return true;
2543
2544 return false;
2545 }
2546
2547 if (flow_spec->type == IB_FLOW_SPEC_ETH) {
2548 struct ib_flow_spec_eth *eth_spec;
2549
2550 eth_spec = (struct ib_flow_spec_eth *)flow_spec;
2551 return is_multicast_ether_addr(eth_spec->mask.dst_mac) &&
2552 is_multicast_ether_addr(eth_spec->val.dst_mac);
2553 }
2554
2555 return false;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002556}
2557
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002558static bool is_valid_ethertype(struct mlx5_core_dev *mdev,
2559 const struct ib_flow_attr *flow_attr,
Ariel Levkovich0f750962017-04-03 13:11:02 +03002560 bool check_inner)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002561{
2562 union ib_flow_spec *ib_spec = (union ib_flow_spec *)(flow_attr + 1);
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002563 int match_ipv = check_inner ?
2564 MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2565 ft_field_support.inner_ip_version) :
2566 MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2567 ft_field_support.outer_ip_version);
Ariel Levkovich0f750962017-04-03 13:11:02 +03002568 int inner_bit = check_inner ? IB_FLOW_SPEC_INNER : 0;
2569 bool ipv4_spec_valid, ipv6_spec_valid;
2570 unsigned int ip_spec_type = 0;
2571 bool has_ethertype = false;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002572 unsigned int spec_index;
Ariel Levkovich0f750962017-04-03 13:11:02 +03002573 bool mask_valid = true;
2574 u16 eth_type = 0;
2575 bool type_valid;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002576
2577 /* Validate that ethertype is correct */
2578 for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
Ariel Levkovich0f750962017-04-03 13:11:02 +03002579 if ((ib_spec->type == (IB_FLOW_SPEC_ETH | inner_bit)) &&
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002580 ib_spec->eth.mask.ether_type) {
Ariel Levkovich0f750962017-04-03 13:11:02 +03002581 mask_valid = (ib_spec->eth.mask.ether_type ==
2582 htons(0xffff));
2583 has_ethertype = true;
2584 eth_type = ntohs(ib_spec->eth.val.ether_type);
2585 } else if ((ib_spec->type == (IB_FLOW_SPEC_IPV4 | inner_bit)) ||
2586 (ib_spec->type == (IB_FLOW_SPEC_IPV6 | inner_bit))) {
2587 ip_spec_type = ib_spec->type;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002588 }
2589 ib_spec = (void *)ib_spec + ib_spec->size;
2590 }
Ariel Levkovich0f750962017-04-03 13:11:02 +03002591
2592 type_valid = (!has_ethertype) || (!ip_spec_type);
2593 if (!type_valid && mask_valid) {
2594 ipv4_spec_valid = (eth_type == ETH_P_IP) &&
2595 (ip_spec_type == (IB_FLOW_SPEC_IPV4 | inner_bit));
2596 ipv6_spec_valid = (eth_type == ETH_P_IPV6) &&
2597 (ip_spec_type == (IB_FLOW_SPEC_IPV6 | inner_bit));
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002598
2599 type_valid = (ipv4_spec_valid) || (ipv6_spec_valid) ||
2600 (((eth_type == ETH_P_MPLS_UC) ||
2601 (eth_type == ETH_P_MPLS_MC)) && match_ipv);
Ariel Levkovich0f750962017-04-03 13:11:02 +03002602 }
2603
2604 return type_valid;
2605}
2606
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002607static bool is_valid_attr(struct mlx5_core_dev *mdev,
2608 const struct ib_flow_attr *flow_attr)
Ariel Levkovich0f750962017-04-03 13:11:02 +03002609{
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002610 return is_valid_ethertype(mdev, flow_attr, false) &&
2611 is_valid_ethertype(mdev, flow_attr, true);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002612}
2613
2614static void put_flow_table(struct mlx5_ib_dev *dev,
2615 struct mlx5_ib_flow_prio *prio, bool ft_added)
2616{
2617 prio->refcount -= !!ft_added;
2618 if (!prio->refcount) {
2619 mlx5_destroy_flow_table(prio->flow_table);
2620 prio->flow_table = NULL;
2621 }
2622}
2623
2624static int mlx5_ib_destroy_flow(struct ib_flow *flow_id)
2625{
2626 struct mlx5_ib_dev *dev = to_mdev(flow_id->qp->device);
2627 struct mlx5_ib_flow_handler *handler = container_of(flow_id,
2628 struct mlx5_ib_flow_handler,
2629 ibflow);
2630 struct mlx5_ib_flow_handler *iter, *tmp;
2631
2632 mutex_lock(&dev->flow_db.lock);
2633
2634 list_for_each_entry_safe(iter, tmp, &handler->list, list) {
Mark Bloch74491de2016-08-31 11:24:25 +00002635 mlx5_del_flow_rules(iter->rule);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002636 put_flow_table(dev, iter->prio, true);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002637 list_del(&iter->list);
2638 kfree(iter);
2639 }
2640
Mark Bloch74491de2016-08-31 11:24:25 +00002641 mlx5_del_flow_rules(handler->rule);
Maor Gottlieb5497adc2016-08-28 14:16:31 +03002642 put_flow_table(dev, handler->prio, true);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002643 mutex_unlock(&dev->flow_db.lock);
2644
2645 kfree(handler);
2646
2647 return 0;
2648}
2649
Maor Gottlieb35d190112016-03-07 18:51:47 +02002650static int ib_prio_to_core_prio(unsigned int priority, bool dont_trap)
2651{
2652 priority *= 2;
2653 if (!dont_trap)
2654 priority++;
2655 return priority;
2656}
2657
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002658enum flow_table_type {
2659 MLX5_IB_FT_RX,
2660 MLX5_IB_FT_TX
2661};
2662
Maor Gottlieb00b7c2a2017-03-29 06:09:01 +03002663#define MLX5_FS_MAX_TYPES 6
2664#define MLX5_FS_MAX_ENTRIES BIT(16)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002665static struct mlx5_ib_flow_prio *get_flow_table(struct mlx5_ib_dev *dev,
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002666 struct ib_flow_attr *flow_attr,
2667 enum flow_table_type ft_type)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002668{
Maor Gottlieb35d190112016-03-07 18:51:47 +02002669 bool dont_trap = flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002670 struct mlx5_flow_namespace *ns = NULL;
2671 struct mlx5_ib_flow_prio *prio;
2672 struct mlx5_flow_table *ft;
Maor Gottliebdac388e2017-03-29 06:09:00 +03002673 int max_table_size;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002674 int num_entries;
2675 int num_groups;
2676 int priority;
2677 int err = 0;
2678
Maor Gottliebdac388e2017-03-29 06:09:00 +03002679 max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
2680 log_max_ft_size));
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002681 if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
Maor Gottlieb35d190112016-03-07 18:51:47 +02002682 if (flow_is_multicast_only(flow_attr) &&
2683 !dont_trap)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002684 priority = MLX5_IB_FLOW_MCAST_PRIO;
2685 else
Maor Gottlieb35d190112016-03-07 18:51:47 +02002686 priority = ib_prio_to_core_prio(flow_attr->priority,
2687 dont_trap);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002688 ns = mlx5_get_flow_namespace(dev->mdev,
2689 MLX5_FLOW_NAMESPACE_BYPASS);
2690 num_entries = MLX5_FS_MAX_ENTRIES;
2691 num_groups = MLX5_FS_MAX_TYPES;
2692 prio = &dev->flow_db.prios[priority];
2693 } else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
2694 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
2695 ns = mlx5_get_flow_namespace(dev->mdev,
2696 MLX5_FLOW_NAMESPACE_LEFTOVERS);
2697 build_leftovers_ft_param(&priority,
2698 &num_entries,
2699 &num_groups);
2700 prio = &dev->flow_db.prios[MLX5_IB_FLOW_LEFTOVERS_PRIO];
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002701 } else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
2702 if (!MLX5_CAP_FLOWTABLE(dev->mdev,
2703 allow_sniffer_and_nic_rx_shared_tir))
2704 return ERR_PTR(-ENOTSUPP);
2705
2706 ns = mlx5_get_flow_namespace(dev->mdev, ft_type == MLX5_IB_FT_RX ?
2707 MLX5_FLOW_NAMESPACE_SNIFFER_RX :
2708 MLX5_FLOW_NAMESPACE_SNIFFER_TX);
2709
2710 prio = &dev->flow_db.sniffer[ft_type];
2711 priority = 0;
2712 num_entries = 1;
2713 num_groups = 1;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002714 }
2715
2716 if (!ns)
2717 return ERR_PTR(-ENOTSUPP);
2718
Maor Gottliebdac388e2017-03-29 06:09:00 +03002719 if (num_entries > max_table_size)
2720 return ERR_PTR(-ENOMEM);
2721
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002722 ft = prio->flow_table;
2723 if (!ft) {
2724 ft = mlx5_create_auto_grouped_flow_table(ns, priority,
2725 num_entries,
Maor Gottliebd63cd282016-04-29 01:36:35 +03002726 num_groups,
Hadar Hen Zionc9f1b072016-11-07 15:14:44 +02002727 0, 0);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002728
2729 if (!IS_ERR(ft)) {
2730 prio->refcount = 0;
2731 prio->flow_table = ft;
2732 } else {
2733 err = PTR_ERR(ft);
2734 }
2735 }
2736
2737 return err ? ERR_PTR(err) : prio;
2738}
2739
Yishai Hadasa550ddf2017-08-17 15:52:33 +03002740static void set_underlay_qp(struct mlx5_ib_dev *dev,
2741 struct mlx5_flow_spec *spec,
2742 u32 underlay_qpn)
2743{
2744 void *misc_params_c = MLX5_ADDR_OF(fte_match_param,
2745 spec->match_criteria,
2746 misc_parameters);
2747 void *misc_params_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
2748 misc_parameters);
2749
2750 if (underlay_qpn &&
2751 MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
2752 ft_field_support.bth_dst_qp)) {
2753 MLX5_SET(fte_match_set_misc,
2754 misc_params_v, bth_dst_qp, underlay_qpn);
2755 MLX5_SET(fte_match_set_misc,
2756 misc_params_c, bth_dst_qp, 0xffffff);
2757 }
2758}
2759
2760static struct mlx5_ib_flow_handler *_create_flow_rule(struct mlx5_ib_dev *dev,
2761 struct mlx5_ib_flow_prio *ft_prio,
2762 const struct ib_flow_attr *flow_attr,
2763 struct mlx5_flow_destination *dst,
2764 u32 underlay_qpn)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002765{
2766 struct mlx5_flow_table *ft = ft_prio->flow_table;
2767 struct mlx5_ib_flow_handler *handler;
Hadar Hen Zion66958ed2016-11-07 15:14:45 +02002768 struct mlx5_flow_act flow_act = {0};
Maor Gottliebc5bb1732016-07-04 17:23:05 +03002769 struct mlx5_flow_spec *spec;
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002770 struct mlx5_flow_destination *rule_dst = dst;
Maor Gottliebdd063d02016-08-28 14:16:32 +03002771 const void *ib_flow = (const void *)flow_attr + sizeof(*flow_attr);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002772 unsigned int spec_index;
Moses Reuben2ac693f2017-01-18 14:59:50 +02002773 u32 flow_tag = MLX5_FS_DEFAULT_FLOW_TAG;
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002774 bool is_drop = false;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002775 int err = 0;
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002776 int dest_num = 1;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002777
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002778 if (!is_valid_attr(dev->mdev, flow_attr))
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002779 return ERR_PTR(-EINVAL);
2780
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03002781 spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002782 handler = kzalloc(sizeof(*handler), GFP_KERNEL);
Maor Gottliebc5bb1732016-07-04 17:23:05 +03002783 if (!handler || !spec) {
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002784 err = -ENOMEM;
2785 goto free;
2786 }
2787
2788 INIT_LIST_HEAD(&handler->list);
2789
2790 for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002791 err = parse_flow_attr(dev->mdev, spec->match_criteria,
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002792 spec->match_value,
2793 ib_flow, &flow_tag, &is_drop);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002794 if (err < 0)
2795 goto free;
2796
2797 ib_flow += ((union ib_flow_spec *)ib_flow)->size;
2798 }
2799
Yishai Hadasa550ddf2017-08-17 15:52:33 +03002800 if (!flow_is_multicast_only(flow_attr))
2801 set_underlay_qp(dev, spec, underlay_qpn);
2802
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002803 spec->match_criteria_enable = get_match_criteria_enable(spec->match_criteria);
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002804 if (is_drop) {
2805 flow_act.action = MLX5_FLOW_CONTEXT_ACTION_DROP;
2806 rule_dst = NULL;
2807 dest_num = 0;
2808 } else {
2809 flow_act.action = dst ? MLX5_FLOW_CONTEXT_ACTION_FWD_DEST :
2810 MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO;
2811 }
Moses Reuben2ac693f2017-01-18 14:59:50 +02002812
2813 if (flow_tag != MLX5_FS_DEFAULT_FLOW_TAG &&
2814 (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
2815 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT)) {
2816 mlx5_ib_warn(dev, "Flow tag %u and attribute type %x isn't allowed in leftovers\n",
2817 flow_tag, flow_attr->type);
2818 err = -EINVAL;
2819 goto free;
2820 }
2821 flow_act.flow_tag = flow_tag;
Mark Bloch74491de2016-08-31 11:24:25 +00002822 handler->rule = mlx5_add_flow_rules(ft, spec,
Hadar Hen Zion66958ed2016-11-07 15:14:45 +02002823 &flow_act,
Slava Shwartsmana22ed862017-04-03 13:13:52 +03002824 rule_dst, dest_num);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002825
2826 if (IS_ERR(handler->rule)) {
2827 err = PTR_ERR(handler->rule);
2828 goto free;
2829 }
2830
Maor Gottliebd9d49802016-08-28 14:16:33 +03002831 ft_prio->refcount++;
Maor Gottlieb5497adc2016-08-28 14:16:31 +03002832 handler->prio = ft_prio;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002833
2834 ft_prio->flow_table = ft;
2835free:
2836 if (err)
2837 kfree(handler);
Maor Gottliebc5bb1732016-07-04 17:23:05 +03002838 kvfree(spec);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002839 return err ? ERR_PTR(err) : handler;
2840}
2841
Yishai Hadasa550ddf2017-08-17 15:52:33 +03002842static struct mlx5_ib_flow_handler *create_flow_rule(struct mlx5_ib_dev *dev,
2843 struct mlx5_ib_flow_prio *ft_prio,
2844 const struct ib_flow_attr *flow_attr,
2845 struct mlx5_flow_destination *dst)
2846{
2847 return _create_flow_rule(dev, ft_prio, flow_attr, dst, 0);
2848}
2849
Maor Gottlieb35d190112016-03-07 18:51:47 +02002850static struct mlx5_ib_flow_handler *create_dont_trap_rule(struct mlx5_ib_dev *dev,
2851 struct mlx5_ib_flow_prio *ft_prio,
2852 struct ib_flow_attr *flow_attr,
2853 struct mlx5_flow_destination *dst)
2854{
2855 struct mlx5_ib_flow_handler *handler_dst = NULL;
2856 struct mlx5_ib_flow_handler *handler = NULL;
2857
2858 handler = create_flow_rule(dev, ft_prio, flow_attr, NULL);
2859 if (!IS_ERR(handler)) {
2860 handler_dst = create_flow_rule(dev, ft_prio,
2861 flow_attr, dst);
2862 if (IS_ERR(handler_dst)) {
Mark Bloch74491de2016-08-31 11:24:25 +00002863 mlx5_del_flow_rules(handler->rule);
Maor Gottliebd9d49802016-08-28 14:16:33 +03002864 ft_prio->refcount--;
Maor Gottlieb35d190112016-03-07 18:51:47 +02002865 kfree(handler);
2866 handler = handler_dst;
2867 } else {
2868 list_add(&handler_dst->list, &handler->list);
2869 }
2870 }
2871
2872 return handler;
2873}
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002874enum {
2875 LEFTOVERS_MC,
2876 LEFTOVERS_UC,
2877};
2878
2879static struct mlx5_ib_flow_handler *create_leftovers_rule(struct mlx5_ib_dev *dev,
2880 struct mlx5_ib_flow_prio *ft_prio,
2881 struct ib_flow_attr *flow_attr,
2882 struct mlx5_flow_destination *dst)
2883{
2884 struct mlx5_ib_flow_handler *handler_ucast = NULL;
2885 struct mlx5_ib_flow_handler *handler = NULL;
2886
2887 static struct {
2888 struct ib_flow_attr flow_attr;
2889 struct ib_flow_spec_eth eth_flow;
2890 } leftovers_specs[] = {
2891 [LEFTOVERS_MC] = {
2892 .flow_attr = {
2893 .num_of_specs = 1,
2894 .size = sizeof(leftovers_specs[0])
2895 },
2896 .eth_flow = {
2897 .type = IB_FLOW_SPEC_ETH,
2898 .size = sizeof(struct ib_flow_spec_eth),
2899 .mask = {.dst_mac = {0x1} },
2900 .val = {.dst_mac = {0x1} }
2901 }
2902 },
2903 [LEFTOVERS_UC] = {
2904 .flow_attr = {
2905 .num_of_specs = 1,
2906 .size = sizeof(leftovers_specs[0])
2907 },
2908 .eth_flow = {
2909 .type = IB_FLOW_SPEC_ETH,
2910 .size = sizeof(struct ib_flow_spec_eth),
2911 .mask = {.dst_mac = {0x1} },
2912 .val = {.dst_mac = {} }
2913 }
2914 }
2915 };
2916
2917 handler = create_flow_rule(dev, ft_prio,
2918 &leftovers_specs[LEFTOVERS_MC].flow_attr,
2919 dst);
2920 if (!IS_ERR(handler) &&
2921 flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT) {
2922 handler_ucast = create_flow_rule(dev, ft_prio,
2923 &leftovers_specs[LEFTOVERS_UC].flow_attr,
2924 dst);
2925 if (IS_ERR(handler_ucast)) {
Mark Bloch74491de2016-08-31 11:24:25 +00002926 mlx5_del_flow_rules(handler->rule);
Maor Gottliebd9d49802016-08-28 14:16:33 +03002927 ft_prio->refcount--;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002928 kfree(handler);
2929 handler = handler_ucast;
2930 } else {
2931 list_add(&handler_ucast->list, &handler->list);
2932 }
2933 }
2934
2935 return handler;
2936}
2937
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002938static struct mlx5_ib_flow_handler *create_sniffer_rule(struct mlx5_ib_dev *dev,
2939 struct mlx5_ib_flow_prio *ft_rx,
2940 struct mlx5_ib_flow_prio *ft_tx,
2941 struct mlx5_flow_destination *dst)
2942{
2943 struct mlx5_ib_flow_handler *handler_rx;
2944 struct mlx5_ib_flow_handler *handler_tx;
2945 int err;
2946 static const struct ib_flow_attr flow_attr = {
2947 .num_of_specs = 0,
2948 .size = sizeof(flow_attr)
2949 };
2950
2951 handler_rx = create_flow_rule(dev, ft_rx, &flow_attr, dst);
2952 if (IS_ERR(handler_rx)) {
2953 err = PTR_ERR(handler_rx);
2954 goto err;
2955 }
2956
2957 handler_tx = create_flow_rule(dev, ft_tx, &flow_attr, dst);
2958 if (IS_ERR(handler_tx)) {
2959 err = PTR_ERR(handler_tx);
2960 goto err_tx;
2961 }
2962
2963 list_add(&handler_tx->list, &handler_rx->list);
2964
2965 return handler_rx;
2966
2967err_tx:
Mark Bloch74491de2016-08-31 11:24:25 +00002968 mlx5_del_flow_rules(handler_rx->rule);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002969 ft_rx->refcount--;
2970 kfree(handler_rx);
2971err:
2972 return ERR_PTR(err);
2973}
2974
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002975static struct ib_flow *mlx5_ib_create_flow(struct ib_qp *qp,
2976 struct ib_flow_attr *flow_attr,
2977 int domain)
2978{
2979 struct mlx5_ib_dev *dev = to_mdev(qp->device);
Yishai Hadasd9f88e52016-08-28 10:58:37 +03002980 struct mlx5_ib_qp *mqp = to_mqp(qp);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002981 struct mlx5_ib_flow_handler *handler = NULL;
2982 struct mlx5_flow_destination *dst = NULL;
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002983 struct mlx5_ib_flow_prio *ft_prio_tx = NULL;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002984 struct mlx5_ib_flow_prio *ft_prio;
2985 int err;
Yishai Hadasa550ddf2017-08-17 15:52:33 +03002986 int underlay_qpn;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002987
2988 if (flow_attr->priority > MLX5_IB_FLOW_LAST_PRIO)
Maor Gottliebdac388e2017-03-29 06:09:00 +03002989 return ERR_PTR(-ENOMEM);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002990
2991 if (domain != IB_FLOW_DOMAIN_USER ||
Daniel Jurgens508562d2018-01-04 17:25:34 +02002992 flow_attr->port > dev->num_ports ||
Maor Gottlieb35d190112016-03-07 18:51:47 +02002993 (flow_attr->flags & ~IB_FLOW_ATTR_FLAGS_DONT_TRAP))
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002994 return ERR_PTR(-EINVAL);
2995
2996 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
2997 if (!dst)
2998 return ERR_PTR(-ENOMEM);
2999
3000 mutex_lock(&dev->flow_db.lock);
3001
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003002 ft_prio = get_flow_table(dev, flow_attr, MLX5_IB_FT_RX);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003003 if (IS_ERR(ft_prio)) {
3004 err = PTR_ERR(ft_prio);
3005 goto unlock;
3006 }
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003007 if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
3008 ft_prio_tx = get_flow_table(dev, flow_attr, MLX5_IB_FT_TX);
3009 if (IS_ERR(ft_prio_tx)) {
3010 err = PTR_ERR(ft_prio_tx);
3011 ft_prio_tx = NULL;
3012 goto destroy_ft;
3013 }
3014 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003015
3016 dst->type = MLX5_FLOW_DESTINATION_TYPE_TIR;
Yishai Hadasd9f88e52016-08-28 10:58:37 +03003017 if (mqp->flags & MLX5_IB_QP_RSS)
3018 dst->tir_num = mqp->rss_qp.tirn;
3019 else
3020 dst->tir_num = mqp->raw_packet_qp.rq.tirn;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003021
3022 if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
Maor Gottlieb35d190112016-03-07 18:51:47 +02003023 if (flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP) {
3024 handler = create_dont_trap_rule(dev, ft_prio,
3025 flow_attr, dst);
3026 } else {
Yishai Hadasa550ddf2017-08-17 15:52:33 +03003027 underlay_qpn = (mqp->flags & MLX5_IB_QP_UNDERLAY) ?
3028 mqp->underlay_qpn : 0;
3029 handler = _create_flow_rule(dev, ft_prio, flow_attr,
3030 dst, underlay_qpn);
Maor Gottlieb35d190112016-03-07 18:51:47 +02003031 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003032 } else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
3033 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
3034 handler = create_leftovers_rule(dev, ft_prio, flow_attr,
3035 dst);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003036 } else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
3037 handler = create_sniffer_rule(dev, ft_prio, ft_prio_tx, dst);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003038 } else {
3039 err = -EINVAL;
3040 goto destroy_ft;
3041 }
3042
3043 if (IS_ERR(handler)) {
3044 err = PTR_ERR(handler);
3045 handler = NULL;
3046 goto destroy_ft;
3047 }
3048
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003049 mutex_unlock(&dev->flow_db.lock);
3050 kfree(dst);
3051
3052 return &handler->ibflow;
3053
3054destroy_ft:
3055 put_flow_table(dev, ft_prio, false);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03003056 if (ft_prio_tx)
3057 put_flow_table(dev, ft_prio_tx, false);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003058unlock:
3059 mutex_unlock(&dev->flow_db.lock);
3060 kfree(dst);
3061 kfree(handler);
3062 return ERR_PTR(err);
3063}
3064
Eli Cohene126ba92013-07-07 17:25:49 +03003065static int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
3066{
3067 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
Yishai Hadas81e30882017-06-08 16:15:09 +03003068 struct mlx5_ib_qp *mqp = to_mqp(ibqp);
Eli Cohene126ba92013-07-07 17:25:49 +03003069 int err;
3070
Yishai Hadas81e30882017-06-08 16:15:09 +03003071 if (mqp->flags & MLX5_IB_QP_UNDERLAY) {
3072 mlx5_ib_dbg(dev, "Attaching a multi cast group to underlay QP is not supported\n");
3073 return -EOPNOTSUPP;
3074 }
3075
Jack Morgenstein9603b612014-07-28 23:30:22 +03003076 err = mlx5_core_attach_mcg(dev->mdev, gid, ibqp->qp_num);
Eli Cohene126ba92013-07-07 17:25:49 +03003077 if (err)
3078 mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n",
3079 ibqp->qp_num, gid->raw);
3080
3081 return err;
3082}
3083
3084static int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
3085{
3086 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
3087 int err;
3088
Jack Morgenstein9603b612014-07-28 23:30:22 +03003089 err = mlx5_core_detach_mcg(dev->mdev, gid, ibqp->qp_num);
Eli Cohene126ba92013-07-07 17:25:49 +03003090 if (err)
3091 mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n",
3092 ibqp->qp_num, gid->raw);
3093
3094 return err;
3095}
3096
3097static int init_node_data(struct mlx5_ib_dev *dev)
3098{
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03003099 int err;
Eli Cohene126ba92013-07-07 17:25:49 +03003100
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03003101 err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc);
Eli Cohene126ba92013-07-07 17:25:49 +03003102 if (err)
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03003103 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03003104
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03003105 dev->mdev->rev_id = dev->mdev->pdev->revision;
Eli Cohene126ba92013-07-07 17:25:49 +03003106
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03003107 return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid);
Eli Cohene126ba92013-07-07 17:25:49 +03003108}
3109
3110static ssize_t show_fw_pages(struct device *device, struct device_attribute *attr,
3111 char *buf)
3112{
3113 struct mlx5_ib_dev *dev =
3114 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
3115
Jack Morgenstein9603b612014-07-28 23:30:22 +03003116 return sprintf(buf, "%d\n", dev->mdev->priv.fw_pages);
Eli Cohene126ba92013-07-07 17:25:49 +03003117}
3118
3119static ssize_t show_reg_pages(struct device *device,
3120 struct device_attribute *attr, char *buf)
3121{
3122 struct mlx5_ib_dev *dev =
3123 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
3124
Haggai Eran6aec21f2014-12-11 17:04:23 +02003125 return sprintf(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages));
Eli Cohene126ba92013-07-07 17:25:49 +03003126}
3127
3128static ssize_t show_hca(struct device *device, struct device_attribute *attr,
3129 char *buf)
3130{
3131 struct mlx5_ib_dev *dev =
3132 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
Jack Morgenstein9603b612014-07-28 23:30:22 +03003133 return sprintf(buf, "MT%d\n", dev->mdev->pdev->device);
Eli Cohene126ba92013-07-07 17:25:49 +03003134}
3135
Eli Cohene126ba92013-07-07 17:25:49 +03003136static ssize_t show_rev(struct device *device, struct device_attribute *attr,
3137 char *buf)
3138{
3139 struct mlx5_ib_dev *dev =
3140 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
Jack Morgenstein9603b612014-07-28 23:30:22 +03003141 return sprintf(buf, "%x\n", dev->mdev->rev_id);
Eli Cohene126ba92013-07-07 17:25:49 +03003142}
3143
3144static ssize_t show_board(struct device *device, struct device_attribute *attr,
3145 char *buf)
3146{
3147 struct mlx5_ib_dev *dev =
3148 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
3149 return sprintf(buf, "%.*s\n", MLX5_BOARD_ID_LEN,
Jack Morgenstein9603b612014-07-28 23:30:22 +03003150 dev->mdev->board_id);
Eli Cohene126ba92013-07-07 17:25:49 +03003151}
3152
3153static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03003154static DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL);
3155static DEVICE_ATTR(board_id, S_IRUGO, show_board, NULL);
3156static DEVICE_ATTR(fw_pages, S_IRUGO, show_fw_pages, NULL);
3157static DEVICE_ATTR(reg_pages, S_IRUGO, show_reg_pages, NULL);
3158
3159static struct device_attribute *mlx5_class_attributes[] = {
3160 &dev_attr_hw_rev,
Eli Cohene126ba92013-07-07 17:25:49 +03003161 &dev_attr_hca_type,
3162 &dev_attr_board_id,
3163 &dev_attr_fw_pages,
3164 &dev_attr_reg_pages,
3165};
3166
Haggai Eran7722f472016-02-29 15:45:07 +02003167static void pkey_change_handler(struct work_struct *work)
3168{
3169 struct mlx5_ib_port_resources *ports =
3170 container_of(work, struct mlx5_ib_port_resources,
3171 pkey_change_work);
3172
3173 mutex_lock(&ports->devr->mutex);
3174 mlx5_ib_gsi_pkey_change(ports->gsi);
3175 mutex_unlock(&ports->devr->mutex);
3176}
3177
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03003178static void mlx5_ib_handle_internal_error(struct mlx5_ib_dev *ibdev)
3179{
3180 struct mlx5_ib_qp *mqp;
3181 struct mlx5_ib_cq *send_mcq, *recv_mcq;
3182 struct mlx5_core_cq *mcq;
3183 struct list_head cq_armed_list;
3184 unsigned long flags_qp;
3185 unsigned long flags_cq;
3186 unsigned long flags;
3187
3188 INIT_LIST_HEAD(&cq_armed_list);
3189
3190 /* Go over qp list reside on that ibdev, sync with create/destroy qp.*/
3191 spin_lock_irqsave(&ibdev->reset_flow_resource_lock, flags);
3192 list_for_each_entry(mqp, &ibdev->qp_list, qps_list) {
3193 spin_lock_irqsave(&mqp->sq.lock, flags_qp);
3194 if (mqp->sq.tail != mqp->sq.head) {
3195 send_mcq = to_mcq(mqp->ibqp.send_cq);
3196 spin_lock_irqsave(&send_mcq->lock, flags_cq);
3197 if (send_mcq->mcq.comp &&
3198 mqp->ibqp.send_cq->comp_handler) {
3199 if (!send_mcq->mcq.reset_notify_added) {
3200 send_mcq->mcq.reset_notify_added = 1;
3201 list_add_tail(&send_mcq->mcq.reset_notify,
3202 &cq_armed_list);
3203 }
3204 }
3205 spin_unlock_irqrestore(&send_mcq->lock, flags_cq);
3206 }
3207 spin_unlock_irqrestore(&mqp->sq.lock, flags_qp);
3208 spin_lock_irqsave(&mqp->rq.lock, flags_qp);
3209 /* no handling is needed for SRQ */
3210 if (!mqp->ibqp.srq) {
3211 if (mqp->rq.tail != mqp->rq.head) {
3212 recv_mcq = to_mcq(mqp->ibqp.recv_cq);
3213 spin_lock_irqsave(&recv_mcq->lock, flags_cq);
3214 if (recv_mcq->mcq.comp &&
3215 mqp->ibqp.recv_cq->comp_handler) {
3216 if (!recv_mcq->mcq.reset_notify_added) {
3217 recv_mcq->mcq.reset_notify_added = 1;
3218 list_add_tail(&recv_mcq->mcq.reset_notify,
3219 &cq_armed_list);
3220 }
3221 }
3222 spin_unlock_irqrestore(&recv_mcq->lock,
3223 flags_cq);
3224 }
3225 }
3226 spin_unlock_irqrestore(&mqp->rq.lock, flags_qp);
3227 }
3228 /*At that point all inflight post send were put to be executed as of we
3229 * lock/unlock above locks Now need to arm all involved CQs.
3230 */
3231 list_for_each_entry(mcq, &cq_armed_list, reset_notify) {
3232 mcq->comp(mcq);
3233 }
3234 spin_unlock_irqrestore(&ibdev->reset_flow_resource_lock, flags);
3235}
3236
Maor Gottlieb03404e82017-05-30 10:29:13 +03003237static void delay_drop_handler(struct work_struct *work)
3238{
3239 int err;
3240 struct mlx5_ib_delay_drop *delay_drop =
3241 container_of(work, struct mlx5_ib_delay_drop,
3242 delay_drop_work);
3243
Maor Gottliebfe248c32017-05-30 10:29:14 +03003244 atomic_inc(&delay_drop->events_cnt);
3245
Maor Gottlieb03404e82017-05-30 10:29:13 +03003246 mutex_lock(&delay_drop->lock);
3247 err = mlx5_core_set_delay_drop(delay_drop->dev->mdev,
3248 delay_drop->timeout);
3249 if (err) {
3250 mlx5_ib_warn(delay_drop->dev, "Failed to set delay drop, timeout=%u\n",
3251 delay_drop->timeout);
3252 delay_drop->activate = false;
3253 }
3254 mutex_unlock(&delay_drop->lock);
3255}
3256
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02003257static void mlx5_ib_handle_event(struct work_struct *_work)
Eli Cohene126ba92013-07-07 17:25:49 +03003258{
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02003259 struct mlx5_ib_event_work *work =
3260 container_of(_work, struct mlx5_ib_event_work, work);
3261 struct mlx5_ib_dev *ibdev;
Eli Cohene126ba92013-07-07 17:25:49 +03003262 struct ib_event ibev;
Eli Cohendbaaff22016-10-27 16:36:44 +03003263 bool fatal = false;
Eli Cohene126ba92013-07-07 17:25:49 +03003264 u8 port = 0;
3265
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02003266 if (mlx5_core_is_mp_slave(work->dev)) {
3267 ibdev = mlx5_ib_get_ibdev_from_mpi(work->context);
3268 if (!ibdev)
3269 goto out;
3270 } else {
3271 ibdev = work->context;
3272 }
3273
3274 switch (work->event) {
Eli Cohene126ba92013-07-07 17:25:49 +03003275 case MLX5_DEV_EVENT_SYS_ERROR:
Eli Cohene126ba92013-07-07 17:25:49 +03003276 ibev.event = IB_EVENT_DEVICE_FATAL;
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03003277 mlx5_ib_handle_internal_error(ibdev);
Eli Cohendbaaff22016-10-27 16:36:44 +03003278 fatal = true;
Eli Cohene126ba92013-07-07 17:25:49 +03003279 break;
3280
3281 case MLX5_DEV_EVENT_PORT_UP:
Eli Cohene126ba92013-07-07 17:25:49 +03003282 case MLX5_DEV_EVENT_PORT_DOWN:
Noa Osherovich2788cf32016-06-04 15:15:29 +03003283 case MLX5_DEV_EVENT_PORT_INITIALIZED:
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02003284 port = (u8)work->param;
Aviv Heller5ec8c832016-09-18 20:48:00 +03003285
3286 /* In RoCE, port up/down events are handled in
3287 * mlx5_netdev_event().
3288 */
3289 if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) ==
3290 IB_LINK_LAYER_ETHERNET)
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02003291 goto out;
Aviv Heller5ec8c832016-09-18 20:48:00 +03003292
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02003293 ibev.event = (work->event == MLX5_DEV_EVENT_PORT_UP) ?
Aviv Heller5ec8c832016-09-18 20:48:00 +03003294 IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
Eli Cohene126ba92013-07-07 17:25:49 +03003295 break;
3296
Eli Cohene126ba92013-07-07 17:25:49 +03003297 case MLX5_DEV_EVENT_LID_CHANGE:
3298 ibev.event = IB_EVENT_LID_CHANGE;
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02003299 port = (u8)work->param;
Eli Cohene126ba92013-07-07 17:25:49 +03003300 break;
3301
3302 case MLX5_DEV_EVENT_PKEY_CHANGE:
3303 ibev.event = IB_EVENT_PKEY_CHANGE;
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02003304 port = (u8)work->param;
Haggai Eran7722f472016-02-29 15:45:07 +02003305
3306 schedule_work(&ibdev->devr.ports[port - 1].pkey_change_work);
Eli Cohene126ba92013-07-07 17:25:49 +03003307 break;
3308
3309 case MLX5_DEV_EVENT_GUID_CHANGE:
3310 ibev.event = IB_EVENT_GID_CHANGE;
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02003311 port = (u8)work->param;
Eli Cohene126ba92013-07-07 17:25:49 +03003312 break;
3313
3314 case MLX5_DEV_EVENT_CLIENT_REREG:
3315 ibev.event = IB_EVENT_CLIENT_REREGISTER;
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02003316 port = (u8)work->param;
Eli Cohene126ba92013-07-07 17:25:49 +03003317 break;
Maor Gottlieb03404e82017-05-30 10:29:13 +03003318 case MLX5_DEV_EVENT_DELAY_DROP_TIMEOUT:
3319 schedule_work(&ibdev->delay_drop.delay_drop_work);
3320 goto out;
Saeed Mahameedbdc37922016-09-29 19:35:38 +03003321 default:
Maor Gottlieb03404e82017-05-30 10:29:13 +03003322 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03003323 }
3324
3325 ibev.device = &ibdev->ib_dev;
3326 ibev.element.port_num = port;
3327
Eli Cohena0c84c32013-09-11 16:35:27 +03003328 if (port < 1 || port > ibdev->num_ports) {
3329 mlx5_ib_warn(ibdev, "warning: event on port %d\n", port);
Maor Gottlieb03404e82017-05-30 10:29:13 +03003330 goto out;
Eli Cohena0c84c32013-09-11 16:35:27 +03003331 }
3332
Eli Cohene126ba92013-07-07 17:25:49 +03003333 if (ibdev->ib_active)
3334 ib_dispatch_event(&ibev);
Eli Cohendbaaff22016-10-27 16:36:44 +03003335
3336 if (fatal)
3337 ibdev->ib_active = false;
Maor Gottlieb03404e82017-05-30 10:29:13 +03003338out:
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02003339 kfree(work);
3340}
3341
3342static void mlx5_ib_event(struct mlx5_core_dev *dev, void *context,
3343 enum mlx5_dev_event event, unsigned long param)
3344{
3345 struct mlx5_ib_event_work *work;
3346
3347 work = kmalloc(sizeof(*work), GFP_ATOMIC);
Leon Romanovsky10bea9c2018-01-19 13:07:11 +02003348 if (!work)
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02003349 return;
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02003350
Leon Romanovsky10bea9c2018-01-19 13:07:11 +02003351 INIT_WORK(&work->work, mlx5_ib_handle_event);
3352 work->dev = dev;
3353 work->param = param;
3354 work->context = context;
3355 work->event = event;
3356
3357 queue_work(mlx5_ib_event_wq, &work->work);
Eli Cohene126ba92013-07-07 17:25:49 +03003358}
3359
Maor Gottliebc43f1112017-01-18 14:10:33 +02003360static int set_has_smi_cap(struct mlx5_ib_dev *dev)
3361{
3362 struct mlx5_hca_vport_context vport_ctx;
3363 int err;
3364 int port;
3365
Daniel Jurgens508562d2018-01-04 17:25:34 +02003366 for (port = 1; port <= dev->num_ports; port++) {
Maor Gottliebc43f1112017-01-18 14:10:33 +02003367 dev->mdev->port_caps[port - 1].has_smi = false;
3368 if (MLX5_CAP_GEN(dev->mdev, port_type) ==
3369 MLX5_CAP_PORT_TYPE_IB) {
3370 if (MLX5_CAP_GEN(dev->mdev, ib_virt)) {
3371 err = mlx5_query_hca_vport_context(dev->mdev, 0,
3372 port, 0,
3373 &vport_ctx);
3374 if (err) {
3375 mlx5_ib_err(dev, "query_hca_vport_context for port=%d failed %d\n",
3376 port, err);
3377 return err;
3378 }
3379 dev->mdev->port_caps[port - 1].has_smi =
3380 vport_ctx.has_smi;
3381 } else {
3382 dev->mdev->port_caps[port - 1].has_smi = true;
3383 }
3384 }
3385 }
3386 return 0;
3387}
3388
Eli Cohene126ba92013-07-07 17:25:49 +03003389static void get_ext_port_caps(struct mlx5_ib_dev *dev)
3390{
3391 int port;
3392
Daniel Jurgens508562d2018-01-04 17:25:34 +02003393 for (port = 1; port <= dev->num_ports; port++)
Eli Cohene126ba92013-07-07 17:25:49 +03003394 mlx5_query_ext_port_caps(dev, port);
3395}
3396
Daniel Jurgens32f69e42018-01-04 17:25:36 +02003397static int get_port_caps(struct mlx5_ib_dev *dev, u8 port)
Eli Cohene126ba92013-07-07 17:25:49 +03003398{
3399 struct ib_device_attr *dprops = NULL;
3400 struct ib_port_attr *pprops = NULL;
Dan Carpenterf614fc12015-01-12 11:56:58 +03003401 int err = -ENOMEM;
Matan Barak2528e332015-06-11 16:35:25 +03003402 struct ib_udata uhw = {.inlen = 0, .outlen = 0};
Eli Cohene126ba92013-07-07 17:25:49 +03003403
3404 pprops = kmalloc(sizeof(*pprops), GFP_KERNEL);
3405 if (!pprops)
3406 goto out;
3407
3408 dprops = kmalloc(sizeof(*dprops), GFP_KERNEL);
3409 if (!dprops)
3410 goto out;
3411
Maor Gottliebc43f1112017-01-18 14:10:33 +02003412 err = set_has_smi_cap(dev);
3413 if (err)
3414 goto out;
3415
Matan Barak2528e332015-06-11 16:35:25 +03003416 err = mlx5_ib_query_device(&dev->ib_dev, dprops, &uhw);
Eli Cohene126ba92013-07-07 17:25:49 +03003417 if (err) {
3418 mlx5_ib_warn(dev, "query_device failed %d\n", err);
3419 goto out;
3420 }
3421
Daniel Jurgens32f69e42018-01-04 17:25:36 +02003422 memset(pprops, 0, sizeof(*pprops));
3423 err = mlx5_ib_query_port(&dev->ib_dev, port, pprops);
3424 if (err) {
3425 mlx5_ib_warn(dev, "query_port %d failed %d\n",
3426 port, err);
3427 goto out;
Eli Cohene126ba92013-07-07 17:25:49 +03003428 }
3429
Daniel Jurgens32f69e42018-01-04 17:25:36 +02003430 dev->mdev->port_caps[port - 1].pkey_table_len =
3431 dprops->max_pkeys;
3432 dev->mdev->port_caps[port - 1].gid_table_len =
3433 pprops->gid_tbl_len;
3434 mlx5_ib_dbg(dev, "port %d: pkey_table_len %d, gid_table_len %d\n",
3435 port, dprops->max_pkeys, pprops->gid_tbl_len);
3436
Eli Cohene126ba92013-07-07 17:25:49 +03003437out:
3438 kfree(pprops);
3439 kfree(dprops);
3440
3441 return err;
3442}
3443
3444static void destroy_umrc_res(struct mlx5_ib_dev *dev)
3445{
3446 int err;
3447
3448 err = mlx5_mr_cache_cleanup(dev);
3449 if (err)
3450 mlx5_ib_warn(dev, "mr cache cleanup failed\n");
3451
3452 mlx5_ib_destroy_qp(dev->umrc.qp);
Christoph Hellwigadd08d72016-03-03 09:38:22 +01003453 ib_free_cq(dev->umrc.cq);
Eli Cohene126ba92013-07-07 17:25:49 +03003454 ib_dealloc_pd(dev->umrc.pd);
3455}
3456
3457enum {
3458 MAX_UMR_WR = 128,
3459};
3460
3461static int create_umr_res(struct mlx5_ib_dev *dev)
3462{
3463 struct ib_qp_init_attr *init_attr = NULL;
3464 struct ib_qp_attr *attr = NULL;
3465 struct ib_pd *pd;
3466 struct ib_cq *cq;
3467 struct ib_qp *qp;
Eli Cohene126ba92013-07-07 17:25:49 +03003468 int ret;
3469
3470 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
3471 init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL);
3472 if (!attr || !init_attr) {
3473 ret = -ENOMEM;
3474 goto error_0;
3475 }
3476
Christoph Hellwiged082d32016-09-05 12:56:17 +02003477 pd = ib_alloc_pd(&dev->ib_dev, 0);
Eli Cohene126ba92013-07-07 17:25:49 +03003478 if (IS_ERR(pd)) {
3479 mlx5_ib_dbg(dev, "Couldn't create PD for sync UMR QP\n");
3480 ret = PTR_ERR(pd);
3481 goto error_0;
3482 }
3483
Christoph Hellwigadd08d72016-03-03 09:38:22 +01003484 cq = ib_alloc_cq(&dev->ib_dev, NULL, 128, 0, IB_POLL_SOFTIRQ);
Eli Cohene126ba92013-07-07 17:25:49 +03003485 if (IS_ERR(cq)) {
3486 mlx5_ib_dbg(dev, "Couldn't create CQ for sync UMR QP\n");
3487 ret = PTR_ERR(cq);
3488 goto error_2;
3489 }
Eli Cohene126ba92013-07-07 17:25:49 +03003490
3491 init_attr->send_cq = cq;
3492 init_attr->recv_cq = cq;
3493 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
3494 init_attr->cap.max_send_wr = MAX_UMR_WR;
3495 init_attr->cap.max_send_sge = 1;
3496 init_attr->qp_type = MLX5_IB_QPT_REG_UMR;
3497 init_attr->port_num = 1;
3498 qp = mlx5_ib_create_qp(pd, init_attr, NULL);
3499 if (IS_ERR(qp)) {
3500 mlx5_ib_dbg(dev, "Couldn't create sync UMR QP\n");
3501 ret = PTR_ERR(qp);
3502 goto error_3;
3503 }
3504 qp->device = &dev->ib_dev;
3505 qp->real_qp = qp;
3506 qp->uobject = NULL;
3507 qp->qp_type = MLX5_IB_QPT_REG_UMR;
Majd Dibbiny31fde032017-10-30 14:23:13 +02003508 qp->send_cq = init_attr->send_cq;
3509 qp->recv_cq = init_attr->recv_cq;
Eli Cohene126ba92013-07-07 17:25:49 +03003510
3511 attr->qp_state = IB_QPS_INIT;
3512 attr->port_num = 1;
3513 ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_PKEY_INDEX |
3514 IB_QP_PORT, NULL);
3515 if (ret) {
3516 mlx5_ib_dbg(dev, "Couldn't modify UMR QP\n");
3517 goto error_4;
3518 }
3519
3520 memset(attr, 0, sizeof(*attr));
3521 attr->qp_state = IB_QPS_RTR;
3522 attr->path_mtu = IB_MTU_256;
3523
3524 ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
3525 if (ret) {
3526 mlx5_ib_dbg(dev, "Couldn't modify umr QP to rtr\n");
3527 goto error_4;
3528 }
3529
3530 memset(attr, 0, sizeof(*attr));
3531 attr->qp_state = IB_QPS_RTS;
3532 ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
3533 if (ret) {
3534 mlx5_ib_dbg(dev, "Couldn't modify umr QP to rts\n");
3535 goto error_4;
3536 }
3537
3538 dev->umrc.qp = qp;
3539 dev->umrc.cq = cq;
Eli Cohene126ba92013-07-07 17:25:49 +03003540 dev->umrc.pd = pd;
3541
3542 sema_init(&dev->umrc.sem, MAX_UMR_WR);
3543 ret = mlx5_mr_cache_init(dev);
3544 if (ret) {
3545 mlx5_ib_warn(dev, "mr cache init failed %d\n", ret);
3546 goto error_4;
3547 }
3548
3549 kfree(attr);
3550 kfree(init_attr);
3551
3552 return 0;
3553
3554error_4:
3555 mlx5_ib_destroy_qp(qp);
3556
3557error_3:
Christoph Hellwigadd08d72016-03-03 09:38:22 +01003558 ib_free_cq(cq);
Eli Cohene126ba92013-07-07 17:25:49 +03003559
3560error_2:
Eli Cohene126ba92013-07-07 17:25:49 +03003561 ib_dealloc_pd(pd);
3562
3563error_0:
3564 kfree(attr);
3565 kfree(init_attr);
3566 return ret;
3567}
3568
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03003569static u8 mlx5_get_umr_fence(u8 umr_fence_cap)
3570{
3571 switch (umr_fence_cap) {
3572 case MLX5_CAP_UMR_FENCE_NONE:
3573 return MLX5_FENCE_MODE_NONE;
3574 case MLX5_CAP_UMR_FENCE_SMALL:
3575 return MLX5_FENCE_MODE_INITIATOR_SMALL;
3576 default:
3577 return MLX5_FENCE_MODE_STRONG_ORDERING;
3578 }
3579}
3580
Eli Cohene126ba92013-07-07 17:25:49 +03003581static int create_dev_resources(struct mlx5_ib_resources *devr)
3582{
3583 struct ib_srq_init_attr attr;
3584 struct mlx5_ib_dev *dev;
Matan Barakbcf4c1e2015-06-11 16:35:20 +03003585 struct ib_cq_init_attr cq_attr = {.cqe = 1};
Haggai Eran7722f472016-02-29 15:45:07 +02003586 int port;
Eli Cohene126ba92013-07-07 17:25:49 +03003587 int ret = 0;
3588
3589 dev = container_of(devr, struct mlx5_ib_dev, devr);
3590
Haggai Erand16e91d2016-02-29 15:45:05 +02003591 mutex_init(&devr->mutex);
3592
Eli Cohene126ba92013-07-07 17:25:49 +03003593 devr->p0 = mlx5_ib_alloc_pd(&dev->ib_dev, NULL, NULL);
3594 if (IS_ERR(devr->p0)) {
3595 ret = PTR_ERR(devr->p0);
3596 goto error0;
3597 }
3598 devr->p0->device = &dev->ib_dev;
3599 devr->p0->uobject = NULL;
3600 atomic_set(&devr->p0->usecnt, 0);
3601
Matan Barakbcf4c1e2015-06-11 16:35:20 +03003602 devr->c0 = mlx5_ib_create_cq(&dev->ib_dev, &cq_attr, NULL, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03003603 if (IS_ERR(devr->c0)) {
3604 ret = PTR_ERR(devr->c0);
3605 goto error1;
3606 }
3607 devr->c0->device = &dev->ib_dev;
3608 devr->c0->uobject = NULL;
3609 devr->c0->comp_handler = NULL;
3610 devr->c0->event_handler = NULL;
3611 devr->c0->cq_context = NULL;
3612 atomic_set(&devr->c0->usecnt, 0);
3613
3614 devr->x0 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
3615 if (IS_ERR(devr->x0)) {
3616 ret = PTR_ERR(devr->x0);
3617 goto error2;
3618 }
3619 devr->x0->device = &dev->ib_dev;
3620 devr->x0->inode = NULL;
3621 atomic_set(&devr->x0->usecnt, 0);
3622 mutex_init(&devr->x0->tgt_qp_mutex);
3623 INIT_LIST_HEAD(&devr->x0->tgt_qp_list);
3624
3625 devr->x1 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
3626 if (IS_ERR(devr->x1)) {
3627 ret = PTR_ERR(devr->x1);
3628 goto error3;
3629 }
3630 devr->x1->device = &dev->ib_dev;
3631 devr->x1->inode = NULL;
3632 atomic_set(&devr->x1->usecnt, 0);
3633 mutex_init(&devr->x1->tgt_qp_mutex);
3634 INIT_LIST_HEAD(&devr->x1->tgt_qp_list);
3635
3636 memset(&attr, 0, sizeof(attr));
3637 attr.attr.max_sge = 1;
3638 attr.attr.max_wr = 1;
3639 attr.srq_type = IB_SRQT_XRC;
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03003640 attr.ext.cq = devr->c0;
Eli Cohene126ba92013-07-07 17:25:49 +03003641 attr.ext.xrc.xrcd = devr->x0;
3642
3643 devr->s0 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
3644 if (IS_ERR(devr->s0)) {
3645 ret = PTR_ERR(devr->s0);
3646 goto error4;
3647 }
3648 devr->s0->device = &dev->ib_dev;
3649 devr->s0->pd = devr->p0;
3650 devr->s0->uobject = NULL;
3651 devr->s0->event_handler = NULL;
3652 devr->s0->srq_context = NULL;
3653 devr->s0->srq_type = IB_SRQT_XRC;
3654 devr->s0->ext.xrc.xrcd = devr->x0;
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03003655 devr->s0->ext.cq = devr->c0;
Eli Cohene126ba92013-07-07 17:25:49 +03003656 atomic_inc(&devr->s0->ext.xrc.xrcd->usecnt);
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03003657 atomic_inc(&devr->s0->ext.cq->usecnt);
Eli Cohene126ba92013-07-07 17:25:49 +03003658 atomic_inc(&devr->p0->usecnt);
3659 atomic_set(&devr->s0->usecnt, 0);
3660
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03003661 memset(&attr, 0, sizeof(attr));
3662 attr.attr.max_sge = 1;
3663 attr.attr.max_wr = 1;
3664 attr.srq_type = IB_SRQT_BASIC;
3665 devr->s1 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
3666 if (IS_ERR(devr->s1)) {
3667 ret = PTR_ERR(devr->s1);
3668 goto error5;
3669 }
3670 devr->s1->device = &dev->ib_dev;
3671 devr->s1->pd = devr->p0;
3672 devr->s1->uobject = NULL;
3673 devr->s1->event_handler = NULL;
3674 devr->s1->srq_context = NULL;
3675 devr->s1->srq_type = IB_SRQT_BASIC;
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03003676 devr->s1->ext.cq = devr->c0;
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03003677 atomic_inc(&devr->p0->usecnt);
Artemy Kovalyov1a56ff62017-08-17 15:52:04 +03003678 atomic_set(&devr->s1->usecnt, 0);
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03003679
Haggai Eran7722f472016-02-29 15:45:07 +02003680 for (port = 0; port < ARRAY_SIZE(devr->ports); ++port) {
3681 INIT_WORK(&devr->ports[port].pkey_change_work,
3682 pkey_change_handler);
3683 devr->ports[port].devr = devr;
3684 }
3685
Eli Cohene126ba92013-07-07 17:25:49 +03003686 return 0;
3687
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03003688error5:
3689 mlx5_ib_destroy_srq(devr->s0);
Eli Cohene126ba92013-07-07 17:25:49 +03003690error4:
3691 mlx5_ib_dealloc_xrcd(devr->x1);
3692error3:
3693 mlx5_ib_dealloc_xrcd(devr->x0);
3694error2:
3695 mlx5_ib_destroy_cq(devr->c0);
3696error1:
3697 mlx5_ib_dealloc_pd(devr->p0);
3698error0:
3699 return ret;
3700}
3701
3702static void destroy_dev_resources(struct mlx5_ib_resources *devr)
3703{
Haggai Eran7722f472016-02-29 15:45:07 +02003704 struct mlx5_ib_dev *dev =
3705 container_of(devr, struct mlx5_ib_dev, devr);
3706 int port;
3707
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03003708 mlx5_ib_destroy_srq(devr->s1);
Eli Cohene126ba92013-07-07 17:25:49 +03003709 mlx5_ib_destroy_srq(devr->s0);
3710 mlx5_ib_dealloc_xrcd(devr->x0);
3711 mlx5_ib_dealloc_xrcd(devr->x1);
3712 mlx5_ib_destroy_cq(devr->c0);
3713 mlx5_ib_dealloc_pd(devr->p0);
Haggai Eran7722f472016-02-29 15:45:07 +02003714
3715 /* Make sure no change P_Key work items are still executing */
3716 for (port = 0; port < dev->num_ports; ++port)
3717 cancel_work_sync(&devr->ports[port].pkey_change_work);
Eli Cohene126ba92013-07-07 17:25:49 +03003718}
3719
Achiad Shochate53505a2015-12-23 18:47:25 +02003720static u32 get_core_cap_flags(struct ib_device *ibdev)
3721{
3722 struct mlx5_ib_dev *dev = to_mdev(ibdev);
3723 enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, 1);
3724 u8 l3_type_cap = MLX5_CAP_ROCE(dev->mdev, l3_type);
3725 u8 roce_version_cap = MLX5_CAP_ROCE(dev->mdev, roce_version);
Daniel Jurgens85c7c012018-01-04 17:25:43 +02003726 bool raw_support = !mlx5_core_mp_enabled(dev->mdev);
Achiad Shochate53505a2015-12-23 18:47:25 +02003727 u32 ret = 0;
3728
3729 if (ll == IB_LINK_LAYER_INFINIBAND)
3730 return RDMA_CORE_PORT_IBA_IB;
3731
Daniel Jurgens85c7c012018-01-04 17:25:43 +02003732 if (raw_support)
3733 ret = RDMA_CORE_PORT_RAW_PACKET;
Or Gerlitz72cd5712017-01-24 13:02:36 +02003734
Achiad Shochate53505a2015-12-23 18:47:25 +02003735 if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV4_CAP))
Or Gerlitz72cd5712017-01-24 13:02:36 +02003736 return ret;
Achiad Shochate53505a2015-12-23 18:47:25 +02003737
3738 if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV6_CAP))
Or Gerlitz72cd5712017-01-24 13:02:36 +02003739 return ret;
Achiad Shochate53505a2015-12-23 18:47:25 +02003740
3741 if (roce_version_cap & MLX5_ROCE_VERSION_1_CAP)
3742 ret |= RDMA_CORE_PORT_IBA_ROCE;
3743
3744 if (roce_version_cap & MLX5_ROCE_VERSION_2_CAP)
3745 ret |= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
3746
3747 return ret;
3748}
3749
Ira Weiny77386132015-05-13 20:02:58 -04003750static int mlx5_port_immutable(struct ib_device *ibdev, u8 port_num,
3751 struct ib_port_immutable *immutable)
3752{
3753 struct ib_port_attr attr;
Or Gerlitzca5b91d2016-11-27 16:51:36 +02003754 struct mlx5_ib_dev *dev = to_mdev(ibdev);
3755 enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, port_num);
Ira Weiny77386132015-05-13 20:02:58 -04003756 int err;
3757
Or Gerlitzc4550c62017-01-24 13:02:39 +02003758 immutable->core_cap_flags = get_core_cap_flags(ibdev);
3759
3760 err = ib_query_port(ibdev, port_num, &attr);
Ira Weiny77386132015-05-13 20:02:58 -04003761 if (err)
3762 return err;
3763
3764 immutable->pkey_tbl_len = attr.pkey_tbl_len;
3765 immutable->gid_tbl_len = attr.gid_tbl_len;
Achiad Shochate53505a2015-12-23 18:47:25 +02003766 immutable->core_cap_flags = get_core_cap_flags(ibdev);
Or Gerlitzca5b91d2016-11-27 16:51:36 +02003767 if ((ll == IB_LINK_LAYER_INFINIBAND) || MLX5_CAP_GEN(dev->mdev, roce))
3768 immutable->max_mad_size = IB_MGMT_MAD_SIZE;
Ira Weiny77386132015-05-13 20:02:58 -04003769
3770 return 0;
3771}
3772
Leon Romanovsky9abb0d12017-06-27 16:49:53 +03003773static void get_dev_fw_str(struct ib_device *ibdev, char *str)
Ira Weinyc7342822016-06-15 02:22:01 -04003774{
3775 struct mlx5_ib_dev *dev =
3776 container_of(ibdev, struct mlx5_ib_dev, ib_dev);
Leon Romanovsky9abb0d12017-06-27 16:49:53 +03003777 snprintf(str, IB_FW_VERSION_NAME_MAX, "%d.%d.%04d",
3778 fw_rev_maj(dev->mdev), fw_rev_min(dev->mdev),
3779 fw_rev_sub(dev->mdev));
Ira Weinyc7342822016-06-15 02:22:01 -04003780}
3781
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003782static int mlx5_eth_lag_init(struct mlx5_ib_dev *dev)
Aviv Heller9ef9c642016-09-18 20:48:01 +03003783{
3784 struct mlx5_core_dev *mdev = dev->mdev;
3785 struct mlx5_flow_namespace *ns = mlx5_get_flow_namespace(mdev,
3786 MLX5_FLOW_NAMESPACE_LAG);
3787 struct mlx5_flow_table *ft;
3788 int err;
3789
3790 if (!ns || !mlx5_lag_is_active(mdev))
3791 return 0;
3792
3793 err = mlx5_cmd_create_vport_lag(mdev);
3794 if (err)
3795 return err;
3796
3797 ft = mlx5_create_lag_demux_flow_table(ns, 0, 0);
3798 if (IS_ERR(ft)) {
3799 err = PTR_ERR(ft);
3800 goto err_destroy_vport_lag;
3801 }
3802
3803 dev->flow_db.lag_demux_ft = ft;
3804 return 0;
3805
3806err_destroy_vport_lag:
3807 mlx5_cmd_destroy_vport_lag(mdev);
3808 return err;
3809}
3810
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003811static void mlx5_eth_lag_cleanup(struct mlx5_ib_dev *dev)
Aviv Heller9ef9c642016-09-18 20:48:01 +03003812{
3813 struct mlx5_core_dev *mdev = dev->mdev;
3814
3815 if (dev->flow_db.lag_demux_ft) {
3816 mlx5_destroy_flow_table(dev->flow_db.lag_demux_ft);
3817 dev->flow_db.lag_demux_ft = NULL;
3818
3819 mlx5_cmd_destroy_vport_lag(mdev);
3820 }
3821}
3822
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +02003823static int mlx5_add_netdev_notifier(struct mlx5_ib_dev *dev, u8 port_num)
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003824{
Achiad Shochate53505a2015-12-23 18:47:25 +02003825 int err;
3826
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +02003827 dev->roce[port_num].nb.notifier_call = mlx5_netdev_event;
3828 err = register_netdevice_notifier(&dev->roce[port_num].nb);
Aviv Heller5ec8c832016-09-18 20:48:00 +03003829 if (err) {
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +02003830 dev->roce[port_num].nb.notifier_call = NULL;
Achiad Shochate53505a2015-12-23 18:47:25 +02003831 return err;
Aviv Heller5ec8c832016-09-18 20:48:00 +03003832 }
Achiad Shochate53505a2015-12-23 18:47:25 +02003833
Or Gerlitzd012f5d2016-11-27 16:51:34 +02003834 return 0;
3835}
Achiad Shochate53505a2015-12-23 18:47:25 +02003836
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +02003837static void mlx5_remove_netdev_notifier(struct mlx5_ib_dev *dev, u8 port_num)
Eli Cohene126ba92013-07-07 17:25:49 +03003838{
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +02003839 if (dev->roce[port_num].nb.notifier_call) {
3840 unregister_netdevice_notifier(&dev->roce[port_num].nb);
3841 dev->roce[port_num].nb.notifier_call = NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03003842 }
3843}
3844
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +02003845static int mlx5_enable_eth(struct mlx5_ib_dev *dev, u8 port_num)
Eli Cohene126ba92013-07-07 17:25:49 +03003846{
Eli Cohene126ba92013-07-07 17:25:49 +03003847 int err;
3848
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +02003849 err = mlx5_add_netdev_notifier(dev, port_num);
Or Gerlitzd012f5d2016-11-27 16:51:34 +02003850 if (err)
Achiad Shochate53505a2015-12-23 18:47:25 +02003851 return err;
Achiad Shochate53505a2015-12-23 18:47:25 +02003852
Or Gerlitzca5b91d2016-11-27 16:51:36 +02003853 if (MLX5_CAP_GEN(dev->mdev, roce)) {
3854 err = mlx5_nic_vport_enable_roce(dev->mdev);
3855 if (err)
3856 goto err_unregister_netdevice_notifier;
3857 }
Achiad Shochate53505a2015-12-23 18:47:25 +02003858
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003859 err = mlx5_eth_lag_init(dev);
Aviv Heller9ef9c642016-09-18 20:48:01 +03003860 if (err)
3861 goto err_disable_roce;
3862
Achiad Shochate53505a2015-12-23 18:47:25 +02003863 return 0;
3864
Aviv Heller9ef9c642016-09-18 20:48:01 +03003865err_disable_roce:
Or Gerlitzca5b91d2016-11-27 16:51:36 +02003866 if (MLX5_CAP_GEN(dev->mdev, roce))
3867 mlx5_nic_vport_disable_roce(dev->mdev);
Aviv Heller9ef9c642016-09-18 20:48:01 +03003868
Achiad Shochate53505a2015-12-23 18:47:25 +02003869err_unregister_netdevice_notifier:
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +02003870 mlx5_remove_netdev_notifier(dev, port_num);
Achiad Shochate53505a2015-12-23 18:47:25 +02003871 return err;
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003872}
3873
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003874static void mlx5_disable_eth(struct mlx5_ib_dev *dev)
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003875{
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003876 mlx5_eth_lag_cleanup(dev);
Or Gerlitzca5b91d2016-11-27 16:51:36 +02003877 if (MLX5_CAP_GEN(dev->mdev, roce))
3878 mlx5_nic_vport_disable_roce(dev->mdev);
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003879}
3880
Parav Pandite1f24a72017-04-16 07:29:29 +03003881struct mlx5_ib_counter {
Kamal Heib7c16f472017-01-18 15:25:09 +02003882 const char *name;
3883 size_t offset;
3884};
3885
3886#define INIT_Q_COUNTER(_name) \
3887 { .name = #_name, .offset = MLX5_BYTE_OFF(query_q_counter_out, _name)}
3888
Parav Pandite1f24a72017-04-16 07:29:29 +03003889static const struct mlx5_ib_counter basic_q_cnts[] = {
Kamal Heib7c16f472017-01-18 15:25:09 +02003890 INIT_Q_COUNTER(rx_write_requests),
3891 INIT_Q_COUNTER(rx_read_requests),
3892 INIT_Q_COUNTER(rx_atomic_requests),
3893 INIT_Q_COUNTER(out_of_buffer),
3894};
3895
Parav Pandite1f24a72017-04-16 07:29:29 +03003896static const struct mlx5_ib_counter out_of_seq_q_cnts[] = {
Kamal Heib7c16f472017-01-18 15:25:09 +02003897 INIT_Q_COUNTER(out_of_sequence),
3898};
3899
Parav Pandite1f24a72017-04-16 07:29:29 +03003900static const struct mlx5_ib_counter retrans_q_cnts[] = {
Kamal Heib7c16f472017-01-18 15:25:09 +02003901 INIT_Q_COUNTER(duplicate_request),
3902 INIT_Q_COUNTER(rnr_nak_retry_err),
3903 INIT_Q_COUNTER(packet_seq_err),
3904 INIT_Q_COUNTER(implied_nak_seq_err),
3905 INIT_Q_COUNTER(local_ack_timeout_err),
3906};
3907
Parav Pandite1f24a72017-04-16 07:29:29 +03003908#define INIT_CONG_COUNTER(_name) \
3909 { .name = #_name, .offset = \
3910 MLX5_BYTE_OFF(query_cong_statistics_out, _name ## _high)}
3911
3912static const struct mlx5_ib_counter cong_cnts[] = {
3913 INIT_CONG_COUNTER(rp_cnp_ignored),
3914 INIT_CONG_COUNTER(rp_cnp_handled),
3915 INIT_CONG_COUNTER(np_ecn_marked_roce_packets),
3916 INIT_CONG_COUNTER(np_cnp_sent),
3917};
3918
Parav Pandit58dcb602017-06-19 07:19:37 +03003919static const struct mlx5_ib_counter extended_err_cnts[] = {
3920 INIT_Q_COUNTER(resp_local_length_error),
3921 INIT_Q_COUNTER(resp_cqe_error),
3922 INIT_Q_COUNTER(req_cqe_error),
3923 INIT_Q_COUNTER(req_remote_invalid_request),
3924 INIT_Q_COUNTER(req_remote_access_errors),
3925 INIT_Q_COUNTER(resp_remote_access_errors),
3926 INIT_Q_COUNTER(resp_cqe_flush_error),
3927 INIT_Q_COUNTER(req_cqe_flush_error),
3928};
3929
Parav Pandite1f24a72017-04-16 07:29:29 +03003930static void mlx5_ib_dealloc_counters(struct mlx5_ib_dev *dev)
Mark Bloch0837e862016-06-17 15:10:55 +03003931{
Daniel Jurgensaac44922018-01-04 17:25:40 +02003932 int i;
Mark Bloch0837e862016-06-17 15:10:55 +03003933
Kamal Heib7c16f472017-01-18 15:25:09 +02003934 for (i = 0; i < dev->num_ports; i++) {
Daniel Jurgensaac44922018-01-04 17:25:40 +02003935 if (dev->port[i].cnts.set_id)
3936 mlx5_core_dealloc_q_counter(dev->mdev,
3937 dev->port[i].cnts.set_id);
Parav Pandite1f24a72017-04-16 07:29:29 +03003938 kfree(dev->port[i].cnts.names);
3939 kfree(dev->port[i].cnts.offsets);
Kamal Heib7c16f472017-01-18 15:25:09 +02003940 }
3941}
3942
Parav Pandite1f24a72017-04-16 07:29:29 +03003943static int __mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev,
3944 struct mlx5_ib_counters *cnts)
Kamal Heib7c16f472017-01-18 15:25:09 +02003945{
3946 u32 num_counters;
3947
3948 num_counters = ARRAY_SIZE(basic_q_cnts);
3949
3950 if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt))
3951 num_counters += ARRAY_SIZE(out_of_seq_q_cnts);
3952
3953 if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters))
3954 num_counters += ARRAY_SIZE(retrans_q_cnts);
Parav Pandit58dcb602017-06-19 07:19:37 +03003955
3956 if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters))
3957 num_counters += ARRAY_SIZE(extended_err_cnts);
3958
Parav Pandite1f24a72017-04-16 07:29:29 +03003959 cnts->num_q_counters = num_counters;
Kamal Heib7c16f472017-01-18 15:25:09 +02003960
Parav Pandite1f24a72017-04-16 07:29:29 +03003961 if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
3962 cnts->num_cong_counters = ARRAY_SIZE(cong_cnts);
3963 num_counters += ARRAY_SIZE(cong_cnts);
3964 }
3965
3966 cnts->names = kcalloc(num_counters, sizeof(cnts->names), GFP_KERNEL);
3967 if (!cnts->names)
Kamal Heib7c16f472017-01-18 15:25:09 +02003968 return -ENOMEM;
3969
Parav Pandite1f24a72017-04-16 07:29:29 +03003970 cnts->offsets = kcalloc(num_counters,
3971 sizeof(cnts->offsets), GFP_KERNEL);
3972 if (!cnts->offsets)
Kamal Heib7c16f472017-01-18 15:25:09 +02003973 goto err_names;
3974
Kamal Heib7c16f472017-01-18 15:25:09 +02003975 return 0;
3976
3977err_names:
Parav Pandite1f24a72017-04-16 07:29:29 +03003978 kfree(cnts->names);
Daniel Jurgensaac44922018-01-04 17:25:40 +02003979 cnts->names = NULL;
Kamal Heib7c16f472017-01-18 15:25:09 +02003980 return -ENOMEM;
3981}
3982
Parav Pandite1f24a72017-04-16 07:29:29 +03003983static void mlx5_ib_fill_counters(struct mlx5_ib_dev *dev,
3984 const char **names,
3985 size_t *offsets)
Kamal Heib7c16f472017-01-18 15:25:09 +02003986{
3987 int i;
3988 int j = 0;
3989
3990 for (i = 0; i < ARRAY_SIZE(basic_q_cnts); i++, j++) {
3991 names[j] = basic_q_cnts[i].name;
3992 offsets[j] = basic_q_cnts[i].offset;
3993 }
3994
3995 if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt)) {
3996 for (i = 0; i < ARRAY_SIZE(out_of_seq_q_cnts); i++, j++) {
3997 names[j] = out_of_seq_q_cnts[i].name;
3998 offsets[j] = out_of_seq_q_cnts[i].offset;
3999 }
4000 }
4001
4002 if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters)) {
4003 for (i = 0; i < ARRAY_SIZE(retrans_q_cnts); i++, j++) {
4004 names[j] = retrans_q_cnts[i].name;
4005 offsets[j] = retrans_q_cnts[i].offset;
4006 }
4007 }
Parav Pandite1f24a72017-04-16 07:29:29 +03004008
Parav Pandit58dcb602017-06-19 07:19:37 +03004009 if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters)) {
4010 for (i = 0; i < ARRAY_SIZE(extended_err_cnts); i++, j++) {
4011 names[j] = extended_err_cnts[i].name;
4012 offsets[j] = extended_err_cnts[i].offset;
4013 }
4014 }
4015
Parav Pandite1f24a72017-04-16 07:29:29 +03004016 if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
4017 for (i = 0; i < ARRAY_SIZE(cong_cnts); i++, j++) {
4018 names[j] = cong_cnts[i].name;
4019 offsets[j] = cong_cnts[i].offset;
4020 }
4021 }
Mark Bloch0837e862016-06-17 15:10:55 +03004022}
4023
Parav Pandite1f24a72017-04-16 07:29:29 +03004024static int mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev)
Mark Bloch0837e862016-06-17 15:10:55 +03004025{
Daniel Jurgensaac44922018-01-04 17:25:40 +02004026 int err = 0;
Mark Bloch0837e862016-06-17 15:10:55 +03004027 int i;
Mark Bloch0837e862016-06-17 15:10:55 +03004028
4029 for (i = 0; i < dev->num_ports; i++) {
Daniel Jurgensaac44922018-01-04 17:25:40 +02004030 err = __mlx5_ib_alloc_counters(dev, &dev->port[i].cnts);
4031 if (err)
4032 goto err_alloc;
Kamal Heib7c16f472017-01-18 15:25:09 +02004033
Daniel Jurgensaac44922018-01-04 17:25:40 +02004034 mlx5_ib_fill_counters(dev, dev->port[i].cnts.names,
4035 dev->port[i].cnts.offsets);
4036
4037 err = mlx5_core_alloc_q_counter(dev->mdev,
4038 &dev->port[i].cnts.set_id);
4039 if (err) {
Mark Bloch0837e862016-06-17 15:10:55 +03004040 mlx5_ib_warn(dev,
4041 "couldn't allocate queue counter for port %d, err %d\n",
Daniel Jurgensaac44922018-01-04 17:25:40 +02004042 i + 1, err);
4043 goto err_alloc;
Mark Bloch0837e862016-06-17 15:10:55 +03004044 }
Daniel Jurgensaac44922018-01-04 17:25:40 +02004045 dev->port[i].cnts.set_id_valid = true;
Mark Bloch0837e862016-06-17 15:10:55 +03004046 }
4047
4048 return 0;
4049
Daniel Jurgensaac44922018-01-04 17:25:40 +02004050err_alloc:
4051 mlx5_ib_dealloc_counters(dev);
4052 return err;
Mark Bloch0837e862016-06-17 15:10:55 +03004053}
4054
Mark Bloch0ad17a82016-06-17 15:10:56 +03004055static struct rdma_hw_stats *mlx5_ib_alloc_hw_stats(struct ib_device *ibdev,
4056 u8 port_num)
4057{
Kamal Heib7c16f472017-01-18 15:25:09 +02004058 struct mlx5_ib_dev *dev = to_mdev(ibdev);
4059 struct mlx5_ib_port *port = &dev->port[port_num - 1];
Mark Bloch0ad17a82016-06-17 15:10:56 +03004060
4061 /* We support only per port stats */
4062 if (port_num == 0)
4063 return NULL;
4064
Parav Pandite1f24a72017-04-16 07:29:29 +03004065 return rdma_alloc_hw_stats_struct(port->cnts.names,
4066 port->cnts.num_q_counters +
4067 port->cnts.num_cong_counters,
Mark Bloch0ad17a82016-06-17 15:10:56 +03004068 RDMA_HW_STATS_DEFAULT_LIFESPAN);
4069}
4070
Daniel Jurgensaac44922018-01-04 17:25:40 +02004071static int mlx5_ib_query_q_counters(struct mlx5_core_dev *mdev,
Parav Pandite1f24a72017-04-16 07:29:29 +03004072 struct mlx5_ib_port *port,
4073 struct rdma_hw_stats *stats)
4074{
4075 int outlen = MLX5_ST_SZ_BYTES(query_q_counter_out);
4076 void *out;
4077 __be32 val;
4078 int ret, i;
4079
Leon Romanovsky1b9a07e2017-05-10 21:32:18 +03004080 out = kvzalloc(outlen, GFP_KERNEL);
Parav Pandite1f24a72017-04-16 07:29:29 +03004081 if (!out)
4082 return -ENOMEM;
4083
Daniel Jurgensaac44922018-01-04 17:25:40 +02004084 ret = mlx5_core_query_q_counter(mdev,
Parav Pandite1f24a72017-04-16 07:29:29 +03004085 port->cnts.set_id, 0,
4086 out, outlen);
4087 if (ret)
4088 goto free;
4089
4090 for (i = 0; i < port->cnts.num_q_counters; i++) {
4091 val = *(__be32 *)(out + port->cnts.offsets[i]);
4092 stats->value[i] = (u64)be32_to_cpu(val);
4093 }
4094
4095free:
4096 kvfree(out);
4097 return ret;
4098}
4099
Mark Bloch0ad17a82016-06-17 15:10:56 +03004100static int mlx5_ib_get_hw_stats(struct ib_device *ibdev,
4101 struct rdma_hw_stats *stats,
Kamal Heib7c16f472017-01-18 15:25:09 +02004102 u8 port_num, int index)
Mark Bloch0ad17a82016-06-17 15:10:56 +03004103{
4104 struct mlx5_ib_dev *dev = to_mdev(ibdev);
Kamal Heib7c16f472017-01-18 15:25:09 +02004105 struct mlx5_ib_port *port = &dev->port[port_num - 1];
Daniel Jurgensaac44922018-01-04 17:25:40 +02004106 struct mlx5_core_dev *mdev;
Parav Pandite1f24a72017-04-16 07:29:29 +03004107 int ret, num_counters;
Daniel Jurgensaac44922018-01-04 17:25:40 +02004108 u8 mdev_port_num;
Mark Bloch0ad17a82016-06-17 15:10:56 +03004109
Kamal Heib7c16f472017-01-18 15:25:09 +02004110 if (!stats)
Parav Pandite1f24a72017-04-16 07:29:29 +03004111 return -EINVAL;
Mark Bloch0ad17a82016-06-17 15:10:56 +03004112
Daniel Jurgensaac44922018-01-04 17:25:40 +02004113 num_counters = port->cnts.num_q_counters + port->cnts.num_cong_counters;
4114
4115 /* q_counters are per IB device, query the master mdev */
4116 ret = mlx5_ib_query_q_counters(dev->mdev, port, stats);
Mark Bloch0ad17a82016-06-17 15:10:56 +03004117 if (ret)
Parav Pandite1f24a72017-04-16 07:29:29 +03004118 return ret;
Mark Bloch0ad17a82016-06-17 15:10:56 +03004119
Parav Pandite1f24a72017-04-16 07:29:29 +03004120 if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
Daniel Jurgensaac44922018-01-04 17:25:40 +02004121 mdev = mlx5_ib_get_native_port_mdev(dev, port_num,
4122 &mdev_port_num);
4123 if (!mdev) {
4124 /* If port is not affiliated yet, its in down state
4125 * which doesn't have any counters yet, so it would be
4126 * zero. So no need to read from the HCA.
4127 */
4128 goto done;
4129 }
Majd Dibbiny71a0ff62017-12-21 17:38:26 +02004130 ret = mlx5_lag_query_cong_counters(dev->mdev,
4131 stats->value +
4132 port->cnts.num_q_counters,
4133 port->cnts.num_cong_counters,
4134 port->cnts.offsets +
4135 port->cnts.num_q_counters);
Daniel Jurgensaac44922018-01-04 17:25:40 +02004136
4137 mlx5_ib_put_native_port_mdev(dev, port_num);
Parav Pandite1f24a72017-04-16 07:29:29 +03004138 if (ret)
4139 return ret;
Mark Bloch0ad17a82016-06-17 15:10:56 +03004140 }
Kamal Heib7c16f472017-01-18 15:25:09 +02004141
Daniel Jurgensaac44922018-01-04 17:25:40 +02004142done:
Parav Pandite1f24a72017-04-16 07:29:29 +03004143 return num_counters;
Mark Bloch0ad17a82016-06-17 15:10:56 +03004144}
4145
Niranjana Vishwanathapura8e959602017-06-30 13:14:46 -07004146static void mlx5_ib_free_rdma_netdev(struct net_device *netdev)
4147{
4148 return mlx5_rdma_netdev_free(netdev);
4149}
4150
Erez Shitrit693dfd52017-04-27 17:01:34 +03004151static struct net_device*
4152mlx5_ib_alloc_rdma_netdev(struct ib_device *hca,
4153 u8 port_num,
4154 enum rdma_netdev_t type,
4155 const char *name,
4156 unsigned char name_assign_type,
4157 void (*setup)(struct net_device *))
4158{
Niranjana Vishwanathapura8e959602017-06-30 13:14:46 -07004159 struct net_device *netdev;
4160 struct rdma_netdev *rn;
4161
Erez Shitrit693dfd52017-04-27 17:01:34 +03004162 if (type != RDMA_NETDEV_IPOIB)
4163 return ERR_PTR(-EOPNOTSUPP);
4164
Niranjana Vishwanathapura8e959602017-06-30 13:14:46 -07004165 netdev = mlx5_rdma_netdev_alloc(to_mdev(hca)->mdev, hca,
4166 name, setup);
4167 if (likely(!IS_ERR_OR_NULL(netdev))) {
4168 rn = netdev_priv(netdev);
4169 rn->free_rdma_netdev = mlx5_ib_free_rdma_netdev;
4170 }
4171 return netdev;
Erez Shitrit693dfd52017-04-27 17:01:34 +03004172}
4173
Maor Gottliebfe248c32017-05-30 10:29:14 +03004174static void delay_drop_debugfs_cleanup(struct mlx5_ib_dev *dev)
4175{
4176 if (!dev->delay_drop.dbg)
4177 return;
4178 debugfs_remove_recursive(dev->delay_drop.dbg->dir_debugfs);
4179 kfree(dev->delay_drop.dbg);
4180 dev->delay_drop.dbg = NULL;
4181}
4182
Maor Gottlieb03404e82017-05-30 10:29:13 +03004183static void cancel_delay_drop(struct mlx5_ib_dev *dev)
4184{
4185 if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
4186 return;
4187
4188 cancel_work_sync(&dev->delay_drop.delay_drop_work);
Maor Gottliebfe248c32017-05-30 10:29:14 +03004189 delay_drop_debugfs_cleanup(dev);
4190}
4191
4192static ssize_t delay_drop_timeout_read(struct file *filp, char __user *buf,
4193 size_t count, loff_t *pos)
4194{
4195 struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
4196 char lbuf[20];
4197 int len;
4198
4199 len = snprintf(lbuf, sizeof(lbuf), "%u\n", delay_drop->timeout);
4200 return simple_read_from_buffer(buf, count, pos, lbuf, len);
4201}
4202
4203static ssize_t delay_drop_timeout_write(struct file *filp, const char __user *buf,
4204 size_t count, loff_t *pos)
4205{
4206 struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
4207 u32 timeout;
4208 u32 var;
4209
4210 if (kstrtouint_from_user(buf, count, 0, &var))
4211 return -EFAULT;
4212
4213 timeout = min_t(u32, roundup(var, 100), MLX5_MAX_DELAY_DROP_TIMEOUT_MS *
4214 1000);
4215 if (timeout != var)
4216 mlx5_ib_dbg(delay_drop->dev, "Round delay drop timeout to %u usec\n",
4217 timeout);
4218
4219 delay_drop->timeout = timeout;
4220
4221 return count;
4222}
4223
4224static const struct file_operations fops_delay_drop_timeout = {
4225 .owner = THIS_MODULE,
4226 .open = simple_open,
4227 .write = delay_drop_timeout_write,
4228 .read = delay_drop_timeout_read,
4229};
4230
4231static int delay_drop_debugfs_init(struct mlx5_ib_dev *dev)
4232{
4233 struct mlx5_ib_dbg_delay_drop *dbg;
4234
4235 if (!mlx5_debugfs_root)
4236 return 0;
4237
4238 dbg = kzalloc(sizeof(*dbg), GFP_KERNEL);
4239 if (!dbg)
4240 return -ENOMEM;
4241
Sudip Mukherjeecbafad82017-09-18 12:28:48 +01004242 dev->delay_drop.dbg = dbg;
4243
Maor Gottliebfe248c32017-05-30 10:29:14 +03004244 dbg->dir_debugfs =
4245 debugfs_create_dir("delay_drop",
4246 dev->mdev->priv.dbg_root);
4247 if (!dbg->dir_debugfs)
Sudip Mukherjeecbafad82017-09-18 12:28:48 +01004248 goto out_debugfs;
Maor Gottliebfe248c32017-05-30 10:29:14 +03004249
4250 dbg->events_cnt_debugfs =
4251 debugfs_create_atomic_t("num_timeout_events", 0400,
4252 dbg->dir_debugfs,
4253 &dev->delay_drop.events_cnt);
4254 if (!dbg->events_cnt_debugfs)
4255 goto out_debugfs;
4256
4257 dbg->rqs_cnt_debugfs =
4258 debugfs_create_atomic_t("num_rqs", 0400,
4259 dbg->dir_debugfs,
4260 &dev->delay_drop.rqs_cnt);
4261 if (!dbg->rqs_cnt_debugfs)
4262 goto out_debugfs;
4263
4264 dbg->timeout_debugfs =
4265 debugfs_create_file("timeout", 0600,
4266 dbg->dir_debugfs,
4267 &dev->delay_drop,
4268 &fops_delay_drop_timeout);
4269 if (!dbg->timeout_debugfs)
4270 goto out_debugfs;
4271
4272 return 0;
4273
4274out_debugfs:
4275 delay_drop_debugfs_cleanup(dev);
4276 return -ENOMEM;
Maor Gottlieb03404e82017-05-30 10:29:13 +03004277}
4278
4279static void init_delay_drop(struct mlx5_ib_dev *dev)
4280{
4281 if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
4282 return;
4283
4284 mutex_init(&dev->delay_drop.lock);
4285 dev->delay_drop.dev = dev;
4286 dev->delay_drop.activate = false;
4287 dev->delay_drop.timeout = MLX5_MAX_DELAY_DROP_TIMEOUT_MS * 1000;
4288 INIT_WORK(&dev->delay_drop.delay_drop_work, delay_drop_handler);
Maor Gottliebfe248c32017-05-30 10:29:14 +03004289 atomic_set(&dev->delay_drop.rqs_cnt, 0);
4290 atomic_set(&dev->delay_drop.events_cnt, 0);
4291
4292 if (delay_drop_debugfs_init(dev))
4293 mlx5_ib_warn(dev, "Failed to init delay drop debugfs\n");
Maor Gottlieb03404e82017-05-30 10:29:13 +03004294}
4295
Leon Romanovsky84305d712017-08-17 15:50:53 +03004296static const struct cpumask *
4297mlx5_ib_get_vector_affinity(struct ib_device *ibdev, int comp_vector)
Sagi Grimberg40b24402017-07-13 11:09:42 +03004298{
4299 struct mlx5_ib_dev *dev = to_mdev(ibdev);
4300
4301 return mlx5_get_vector_affinity(dev->mdev, comp_vector);
4302}
4303
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004304/* The mlx5_ib_multiport_mutex should be held when calling this function */
4305static void mlx5_ib_unbind_slave_port(struct mlx5_ib_dev *ibdev,
4306 struct mlx5_ib_multiport_info *mpi)
4307{
4308 u8 port_num = mlx5_core_native_port_num(mpi->mdev) - 1;
4309 struct mlx5_ib_port *port = &ibdev->port[port_num];
4310 int comps;
4311 int err;
4312 int i;
4313
Parav Pandita9e546e2018-01-04 17:25:39 +02004314 mlx5_ib_cleanup_cong_debugfs(ibdev, port_num);
4315
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004316 spin_lock(&port->mp.mpi_lock);
4317 if (!mpi->ibdev) {
4318 spin_unlock(&port->mp.mpi_lock);
4319 return;
4320 }
4321 mpi->ibdev = NULL;
4322
4323 spin_unlock(&port->mp.mpi_lock);
4324 mlx5_remove_netdev_notifier(ibdev, port_num);
4325 spin_lock(&port->mp.mpi_lock);
4326
4327 comps = mpi->mdev_refcnt;
4328 if (comps) {
4329 mpi->unaffiliate = true;
4330 init_completion(&mpi->unref_comp);
4331 spin_unlock(&port->mp.mpi_lock);
4332
4333 for (i = 0; i < comps; i++)
4334 wait_for_completion(&mpi->unref_comp);
4335
4336 spin_lock(&port->mp.mpi_lock);
4337 mpi->unaffiliate = false;
4338 }
4339
4340 port->mp.mpi = NULL;
4341
4342 list_add_tail(&mpi->list, &mlx5_ib_unaffiliated_port_list);
4343
4344 spin_unlock(&port->mp.mpi_lock);
4345
4346 err = mlx5_nic_vport_unaffiliate_multiport(mpi->mdev);
4347
4348 mlx5_ib_dbg(ibdev, "unaffiliated port %d\n", port_num + 1);
4349 /* Log an error, still needed to cleanup the pointers and add
4350 * it back to the list.
4351 */
4352 if (err)
4353 mlx5_ib_err(ibdev, "Failed to unaffiliate port %u\n",
4354 port_num + 1);
4355
4356 ibdev->roce[port_num].last_port_state = IB_PORT_DOWN;
4357}
4358
4359/* The mlx5_ib_multiport_mutex should be held when calling this function */
4360static bool mlx5_ib_bind_slave_port(struct mlx5_ib_dev *ibdev,
4361 struct mlx5_ib_multiport_info *mpi)
4362{
4363 u8 port_num = mlx5_core_native_port_num(mpi->mdev) - 1;
4364 int err;
4365
4366 spin_lock(&ibdev->port[port_num].mp.mpi_lock);
4367 if (ibdev->port[port_num].mp.mpi) {
4368 mlx5_ib_warn(ibdev, "port %d already affiliated.\n",
4369 port_num + 1);
4370 spin_unlock(&ibdev->port[port_num].mp.mpi_lock);
4371 return false;
4372 }
4373
4374 ibdev->port[port_num].mp.mpi = mpi;
4375 mpi->ibdev = ibdev;
4376 spin_unlock(&ibdev->port[port_num].mp.mpi_lock);
4377
4378 err = mlx5_nic_vport_affiliate_multiport(ibdev->mdev, mpi->mdev);
4379 if (err)
4380 goto unbind;
4381
4382 err = get_port_caps(ibdev, mlx5_core_native_port_num(mpi->mdev));
4383 if (err)
4384 goto unbind;
4385
4386 err = mlx5_add_netdev_notifier(ibdev, port_num);
4387 if (err) {
4388 mlx5_ib_err(ibdev, "failed adding netdev notifier for port %u\n",
4389 port_num + 1);
4390 goto unbind;
4391 }
4392
Parav Pandita9e546e2018-01-04 17:25:39 +02004393 err = mlx5_ib_init_cong_debugfs(ibdev, port_num);
4394 if (err)
4395 goto unbind;
4396
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004397 return true;
4398
4399unbind:
4400 mlx5_ib_unbind_slave_port(ibdev, mpi);
4401 return false;
4402}
4403
4404static int mlx5_ib_init_multiport_master(struct mlx5_ib_dev *dev)
4405{
4406 int port_num = mlx5_core_native_port_num(dev->mdev) - 1;
4407 enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev,
4408 port_num + 1);
4409 struct mlx5_ib_multiport_info *mpi;
4410 int err;
4411 int i;
4412
4413 if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
4414 return 0;
4415
4416 err = mlx5_query_nic_vport_system_image_guid(dev->mdev,
4417 &dev->sys_image_guid);
4418 if (err)
4419 return err;
4420
4421 err = mlx5_nic_vport_enable_roce(dev->mdev);
4422 if (err)
4423 return err;
4424
4425 mutex_lock(&mlx5_ib_multiport_mutex);
4426 for (i = 0; i < dev->num_ports; i++) {
4427 bool bound = false;
4428
4429 /* build a stub multiport info struct for the native port. */
4430 if (i == port_num) {
4431 mpi = kzalloc(sizeof(*mpi), GFP_KERNEL);
4432 if (!mpi) {
4433 mutex_unlock(&mlx5_ib_multiport_mutex);
4434 mlx5_nic_vport_disable_roce(dev->mdev);
4435 return -ENOMEM;
4436 }
4437
4438 mpi->is_master = true;
4439 mpi->mdev = dev->mdev;
4440 mpi->sys_image_guid = dev->sys_image_guid;
4441 dev->port[i].mp.mpi = mpi;
4442 mpi->ibdev = dev;
4443 mpi = NULL;
4444 continue;
4445 }
4446
4447 list_for_each_entry(mpi, &mlx5_ib_unaffiliated_port_list,
4448 list) {
4449 if (dev->sys_image_guid == mpi->sys_image_guid &&
4450 (mlx5_core_native_port_num(mpi->mdev) - 1) == i) {
4451 bound = mlx5_ib_bind_slave_port(dev, mpi);
4452 }
4453
4454 if (bound) {
4455 dev_dbg(&mpi->mdev->pdev->dev, "removing port from unaffiliated list.\n");
4456 mlx5_ib_dbg(dev, "port %d bound\n", i + 1);
4457 list_del(&mpi->list);
4458 break;
4459 }
4460 }
4461 if (!bound) {
4462 get_port_caps(dev, i + 1);
4463 mlx5_ib_dbg(dev, "no free port found for port %d\n",
4464 i + 1);
4465 }
4466 }
4467
4468 list_add_tail(&dev->ib_dev_list, &mlx5_ib_dev_list);
4469 mutex_unlock(&mlx5_ib_multiport_mutex);
4470 return err;
4471}
4472
4473static void mlx5_ib_cleanup_multiport_master(struct mlx5_ib_dev *dev)
4474{
4475 int port_num = mlx5_core_native_port_num(dev->mdev) - 1;
4476 enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev,
4477 port_num + 1);
4478 int i;
4479
4480 if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
4481 return;
4482
4483 mutex_lock(&mlx5_ib_multiport_mutex);
4484 for (i = 0; i < dev->num_ports; i++) {
4485 if (dev->port[i].mp.mpi) {
4486 /* Destroy the native port stub */
4487 if (i == port_num) {
4488 kfree(dev->port[i].mp.mpi);
4489 dev->port[i].mp.mpi = NULL;
4490 } else {
4491 mlx5_ib_dbg(dev, "unbinding port_num: %d\n", i + 1);
4492 mlx5_ib_unbind_slave_port(dev, dev->port[i].mp.mpi);
4493 }
4494 }
4495 }
4496
4497 mlx5_ib_dbg(dev, "removing from devlist\n");
4498 list_del(&dev->ib_dev_list);
4499 mutex_unlock(&mlx5_ib_multiport_mutex);
4500
4501 mlx5_nic_vport_disable_roce(dev->mdev);
4502}
4503
Mark Bloch16c19752018-01-01 13:06:58 +02004504static void mlx5_ib_stage_init_cleanup(struct mlx5_ib_dev *dev)
Eli Cohene126ba92013-07-07 17:25:49 +03004505{
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004506 mlx5_ib_cleanup_multiport_master(dev);
Mark Bloch3cc297d2018-01-01 13:07:03 +02004507#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
4508 cleanup_srcu_struct(&dev->mr_srcu);
4509#endif
Mark Bloch16c19752018-01-01 13:06:58 +02004510 kfree(dev->port);
4511}
4512
4513static int mlx5_ib_stage_init_init(struct mlx5_ib_dev *dev)
4514{
4515 struct mlx5_core_dev *mdev = dev->mdev;
Aviv Heller4babcf92016-09-18 20:48:03 +03004516 const char *name;
Eli Cohene126ba92013-07-07 17:25:49 +03004517 int err;
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004518 int i;
Eli Cohene126ba92013-07-07 17:25:49 +03004519
Daniel Jurgens508562d2018-01-04 17:25:34 +02004520 dev->port = kcalloc(dev->num_ports, sizeof(*dev->port),
Mark Bloch0837e862016-06-17 15:10:55 +03004521 GFP_KERNEL);
4522 if (!dev->port)
Mark Bloch16c19752018-01-01 13:06:58 +02004523 return -ENOMEM;
Mark Bloch0837e862016-06-17 15:10:55 +03004524
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004525 for (i = 0; i < dev->num_ports; i++) {
4526 spin_lock_init(&dev->port[i].mp.mpi_lock);
4527 rwlock_init(&dev->roce[i].netdev_lock);
4528 }
4529
4530 err = mlx5_ib_init_multiport_master(dev);
Eli Cohene126ba92013-07-07 17:25:49 +03004531 if (err)
Mark Bloch0837e862016-06-17 15:10:55 +03004532 goto err_free_port;
Eli Cohene126ba92013-07-07 17:25:49 +03004533
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004534 if (!mlx5_core_mp_enabled(mdev)) {
4535 int i;
4536
4537 for (i = 1; i <= dev->num_ports; i++) {
4538 err = get_port_caps(dev, i);
4539 if (err)
4540 break;
4541 }
4542 } else {
4543 err = get_port_caps(dev, mlx5_core_native_port_num(mdev));
4544 }
4545 if (err)
4546 goto err_mp;
4547
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03004548 if (mlx5_use_mad_ifc(dev))
4549 get_ext_port_caps(dev);
Eli Cohene126ba92013-07-07 17:25:49 +03004550
Aviv Heller4babcf92016-09-18 20:48:03 +03004551 if (!mlx5_lag_is_active(mdev))
4552 name = "mlx5_%d";
4553 else
4554 name = "mlx5_bond_%d";
4555
4556 strlcpy(dev->ib_dev.name, name, IB_DEVICE_NAME_MAX);
Eli Cohene126ba92013-07-07 17:25:49 +03004557 dev->ib_dev.owner = THIS_MODULE;
4558 dev->ib_dev.node_type = RDMA_NODE_IB_CA;
Sagi Grimbergc6790aa2015-09-24 10:34:23 +03004559 dev->ib_dev.local_dma_lkey = 0 /* not supported for now */;
Daniel Jurgens508562d2018-01-04 17:25:34 +02004560 dev->ib_dev.phys_port_cnt = dev->num_ports;
Saeed Mahameed233d05d2015-04-02 17:07:32 +03004561 dev->ib_dev.num_comp_vectors =
4562 dev->mdev->priv.eq_table.num_comp_vectors;
Bart Van Assche9b0c2892017-01-20 13:04:21 -08004563 dev->ib_dev.dev.parent = &mdev->pdev->dev;
Eli Cohene126ba92013-07-07 17:25:49 +03004564
Mark Bloch3cc297d2018-01-01 13:07:03 +02004565 mutex_init(&dev->flow_db.lock);
4566 mutex_init(&dev->cap_mask_mutex);
4567 INIT_LIST_HEAD(&dev->qp_list);
4568 spin_lock_init(&dev->reset_flow_resource_lock);
4569
4570#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
4571 err = init_srcu_struct(&dev->mr_srcu);
4572 if (err)
4573 goto err_free_port;
4574#endif
4575
Mark Bloch16c19752018-01-01 13:06:58 +02004576 return 0;
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004577err_mp:
4578 mlx5_ib_cleanup_multiport_master(dev);
Mark Bloch16c19752018-01-01 13:06:58 +02004579
4580err_free_port:
4581 kfree(dev->port);
4582
4583 return -ENOMEM;
4584}
4585
4586static int mlx5_ib_stage_caps_init(struct mlx5_ib_dev *dev)
4587{
4588 struct mlx5_core_dev *mdev = dev->mdev;
Mark Bloch16c19752018-01-01 13:06:58 +02004589 int err;
4590
Eli Cohene126ba92013-07-07 17:25:49 +03004591 dev->ib_dev.uverbs_abi_ver = MLX5_IB_UVERBS_ABI_VERSION;
4592 dev->ib_dev.uverbs_cmd_mask =
4593 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
4594 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
4595 (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
4596 (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
4597 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
Moni Shoua41c450f2016-11-23 08:23:26 +02004598 (1ull << IB_USER_VERBS_CMD_CREATE_AH) |
4599 (1ull << IB_USER_VERBS_CMD_DESTROY_AH) |
Eli Cohene126ba92013-07-07 17:25:49 +03004600 (1ull << IB_USER_VERBS_CMD_REG_MR) |
Noa Osherovich56e11d62016-02-29 16:46:51 +02004601 (1ull << IB_USER_VERBS_CMD_REREG_MR) |
Eli Cohene126ba92013-07-07 17:25:49 +03004602 (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
4603 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
4604 (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
4605 (1ull << IB_USER_VERBS_CMD_RESIZE_CQ) |
4606 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
4607 (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
4608 (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
4609 (1ull << IB_USER_VERBS_CMD_QUERY_QP) |
4610 (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
4611 (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST) |
4612 (1ull << IB_USER_VERBS_CMD_DETACH_MCAST) |
4613 (1ull << IB_USER_VERBS_CMD_CREATE_SRQ) |
4614 (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ) |
4615 (1ull << IB_USER_VERBS_CMD_QUERY_SRQ) |
4616 (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ) |
4617 (1ull << IB_USER_VERBS_CMD_CREATE_XSRQ) |
4618 (1ull << IB_USER_VERBS_CMD_OPEN_QP);
Haggai Eran1707cb42015-02-08 13:28:52 +02004619 dev->ib_dev.uverbs_ex_cmd_mask =
Matan Barakd4584dd2016-01-28 17:51:46 +02004620 (1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE) |
4621 (1ull << IB_USER_VERBS_EX_CMD_CREATE_CQ) |
Bodong Wang7d29f342016-12-01 13:43:16 +02004622 (1ull << IB_USER_VERBS_EX_CMD_CREATE_QP) |
Yonatan Cohenb0e9df62017-11-13 10:51:15 +02004623 (1ull << IB_USER_VERBS_EX_CMD_MODIFY_QP) |
4624 (1ull << IB_USER_VERBS_EX_CMD_MODIFY_CQ);
Eli Cohene126ba92013-07-07 17:25:49 +03004625
4626 dev->ib_dev.query_device = mlx5_ib_query_device;
4627 dev->ib_dev.query_port = mlx5_ib_query_port;
Achiad Shochatebd61f62015-12-23 18:47:16 +02004628 dev->ib_dev.get_link_layer = mlx5_ib_port_link_layer;
Eli Cohene126ba92013-07-07 17:25:49 +03004629 dev->ib_dev.query_gid = mlx5_ib_query_gid;
Achiad Shochat3cca2602015-12-23 18:47:23 +02004630 dev->ib_dev.add_gid = mlx5_ib_add_gid;
4631 dev->ib_dev.del_gid = mlx5_ib_del_gid;
Eli Cohene126ba92013-07-07 17:25:49 +03004632 dev->ib_dev.query_pkey = mlx5_ib_query_pkey;
4633 dev->ib_dev.modify_device = mlx5_ib_modify_device;
4634 dev->ib_dev.modify_port = mlx5_ib_modify_port;
4635 dev->ib_dev.alloc_ucontext = mlx5_ib_alloc_ucontext;
4636 dev->ib_dev.dealloc_ucontext = mlx5_ib_dealloc_ucontext;
4637 dev->ib_dev.mmap = mlx5_ib_mmap;
4638 dev->ib_dev.alloc_pd = mlx5_ib_alloc_pd;
4639 dev->ib_dev.dealloc_pd = mlx5_ib_dealloc_pd;
4640 dev->ib_dev.create_ah = mlx5_ib_create_ah;
4641 dev->ib_dev.query_ah = mlx5_ib_query_ah;
4642 dev->ib_dev.destroy_ah = mlx5_ib_destroy_ah;
4643 dev->ib_dev.create_srq = mlx5_ib_create_srq;
4644 dev->ib_dev.modify_srq = mlx5_ib_modify_srq;
4645 dev->ib_dev.query_srq = mlx5_ib_query_srq;
4646 dev->ib_dev.destroy_srq = mlx5_ib_destroy_srq;
4647 dev->ib_dev.post_srq_recv = mlx5_ib_post_srq_recv;
4648 dev->ib_dev.create_qp = mlx5_ib_create_qp;
4649 dev->ib_dev.modify_qp = mlx5_ib_modify_qp;
4650 dev->ib_dev.query_qp = mlx5_ib_query_qp;
4651 dev->ib_dev.destroy_qp = mlx5_ib_destroy_qp;
4652 dev->ib_dev.post_send = mlx5_ib_post_send;
4653 dev->ib_dev.post_recv = mlx5_ib_post_recv;
4654 dev->ib_dev.create_cq = mlx5_ib_create_cq;
4655 dev->ib_dev.modify_cq = mlx5_ib_modify_cq;
4656 dev->ib_dev.resize_cq = mlx5_ib_resize_cq;
4657 dev->ib_dev.destroy_cq = mlx5_ib_destroy_cq;
4658 dev->ib_dev.poll_cq = mlx5_ib_poll_cq;
4659 dev->ib_dev.req_notify_cq = mlx5_ib_arm_cq;
4660 dev->ib_dev.get_dma_mr = mlx5_ib_get_dma_mr;
4661 dev->ib_dev.reg_user_mr = mlx5_ib_reg_user_mr;
Noa Osherovich56e11d62016-02-29 16:46:51 +02004662 dev->ib_dev.rereg_user_mr = mlx5_ib_rereg_user_mr;
Eli Cohene126ba92013-07-07 17:25:49 +03004663 dev->ib_dev.dereg_mr = mlx5_ib_dereg_mr;
4664 dev->ib_dev.attach_mcast = mlx5_ib_mcg_attach;
4665 dev->ib_dev.detach_mcast = mlx5_ib_mcg_detach;
4666 dev->ib_dev.process_mad = mlx5_ib_process_mad;
Sagi Grimberg9bee1782015-07-30 10:32:35 +03004667 dev->ib_dev.alloc_mr = mlx5_ib_alloc_mr;
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03004668 dev->ib_dev.map_mr_sg = mlx5_ib_map_mr_sg;
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02004669 dev->ib_dev.check_mr_status = mlx5_ib_check_mr_status;
Ira Weiny77386132015-05-13 20:02:58 -04004670 dev->ib_dev.get_port_immutable = mlx5_port_immutable;
Ira Weinyc7342822016-06-15 02:22:01 -04004671 dev->ib_dev.get_dev_fw_str = get_dev_fw_str;
Sagi Grimberg40b24402017-07-13 11:09:42 +03004672 dev->ib_dev.get_vector_affinity = mlx5_ib_get_vector_affinity;
Niranjana Vishwanathapura8e959602017-06-30 13:14:46 -07004673 if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads))
Alex Vesker022d0382017-06-14 09:59:06 +03004674 dev->ib_dev.alloc_rdma_netdev = mlx5_ib_alloc_rdma_netdev;
Niranjana Vishwanathapura8e959602017-06-30 13:14:46 -07004675
Eli Coheneff901d2016-03-11 22:58:42 +02004676 if (mlx5_core_is_pf(mdev)) {
4677 dev->ib_dev.get_vf_config = mlx5_ib_get_vf_config;
4678 dev->ib_dev.set_vf_link_state = mlx5_ib_set_vf_link_state;
4679 dev->ib_dev.get_vf_stats = mlx5_ib_get_vf_stats;
4680 dev->ib_dev.set_vf_guid = mlx5_ib_set_vf_guid;
4681 }
Eli Cohene126ba92013-07-07 17:25:49 +03004682
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03004683 dev->ib_dev.disassociate_ucontext = mlx5_ib_disassociate_ucontext;
4684
Max Gurtovoy6e8484c2017-05-28 10:53:11 +03004685 dev->umr_fence = mlx5_get_umr_fence(MLX5_CAP_GEN(mdev, umr_fence));
4686
Matan Barakd2370e02016-02-29 18:05:30 +02004687 if (MLX5_CAP_GEN(mdev, imaicl)) {
4688 dev->ib_dev.alloc_mw = mlx5_ib_alloc_mw;
4689 dev->ib_dev.dealloc_mw = mlx5_ib_dealloc_mw;
4690 dev->ib_dev.uverbs_cmd_mask |=
4691 (1ull << IB_USER_VERBS_CMD_ALLOC_MW) |
4692 (1ull << IB_USER_VERBS_CMD_DEALLOC_MW);
4693 }
4694
Saeed Mahameed938fe832015-05-28 22:28:41 +03004695 if (MLX5_CAP_GEN(mdev, xrc)) {
Eli Cohene126ba92013-07-07 17:25:49 +03004696 dev->ib_dev.alloc_xrcd = mlx5_ib_alloc_xrcd;
4697 dev->ib_dev.dealloc_xrcd = mlx5_ib_dealloc_xrcd;
4698 dev->ib_dev.uverbs_cmd_mask |=
4699 (1ull << IB_USER_VERBS_CMD_OPEN_XRCD) |
4700 (1ull << IB_USER_VERBS_CMD_CLOSE_XRCD);
4701 }
4702
Yishai Hadas81e30882017-06-08 16:15:09 +03004703 dev->ib_dev.create_flow = mlx5_ib_create_flow;
4704 dev->ib_dev.destroy_flow = mlx5_ib_destroy_flow;
4705 dev->ib_dev.uverbs_ex_cmd_mask |=
4706 (1ull << IB_USER_VERBS_EX_CMD_CREATE_FLOW) |
4707 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_FLOW);
4708
Eli Cohene126ba92013-07-07 17:25:49 +03004709 err = init_node_data(dev);
4710 if (err)
Mark Bloch16c19752018-01-01 13:06:58 +02004711 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03004712
Mark Blochc8b89922018-01-01 13:07:02 +02004713 if ((MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_ETH) &&
4714 MLX5_CAP_GEN(dev->mdev, disable_local_lb))
4715 mutex_init(&dev->lb_mutex);
4716
Mark Bloch16c19752018-01-01 13:06:58 +02004717 return 0;
4718}
4719
4720static int mlx5_ib_stage_roce_init(struct mlx5_ib_dev *dev)
4721{
4722 struct mlx5_core_dev *mdev = dev->mdev;
4723 enum rdma_link_layer ll;
4724 int port_type_cap;
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004725 u8 port_num;
Mark Bloch16c19752018-01-01 13:06:58 +02004726 int err;
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +02004727 int i;
Mark Bloch16c19752018-01-01 13:06:58 +02004728
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004729 port_num = mlx5_core_native_port_num(dev->mdev) - 1;
Mark Bloch16c19752018-01-01 13:06:58 +02004730 port_type_cap = MLX5_CAP_GEN(mdev, port_type);
4731 ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
4732
Achiad Shochatfc24fc52015-12-23 18:47:17 +02004733 if (ll == IB_LINK_LAYER_ETHERNET) {
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +02004734 for (i = 0; i < dev->num_ports; i++) {
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +02004735 dev->roce[i].dev = dev;
4736 dev->roce[i].native_port_num = i + 1;
4737 dev->roce[i].last_port_state = IB_PORT_DOWN;
4738 }
4739
Mark Blochc11a2262018-01-01 13:06:59 +02004740 dev->ib_dev.get_netdev = mlx5_ib_get_netdev;
4741 dev->ib_dev.create_wq = mlx5_ib_create_wq;
4742 dev->ib_dev.modify_wq = mlx5_ib_modify_wq;
4743 dev->ib_dev.destroy_wq = mlx5_ib_destroy_wq;
4744 dev->ib_dev.create_rwq_ind_table = mlx5_ib_create_rwq_ind_table;
4745 dev->ib_dev.destroy_rwq_ind_table = mlx5_ib_destroy_rwq_ind_table;
4746 dev->ib_dev.uverbs_ex_cmd_mask |=
4747 (1ull << IB_USER_VERBS_EX_CMD_CREATE_WQ) |
4748 (1ull << IB_USER_VERBS_EX_CMD_MODIFY_WQ) |
4749 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_WQ) |
4750 (1ull << IB_USER_VERBS_EX_CMD_CREATE_RWQ_IND_TBL) |
4751 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_RWQ_IND_TBL);
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +02004752 err = mlx5_enable_eth(dev, port_num);
Achiad Shochatfc24fc52015-12-23 18:47:17 +02004753 if (err)
Mark Bloch16c19752018-01-01 13:06:58 +02004754 return err;
Achiad Shochatfc24fc52015-12-23 18:47:17 +02004755 }
4756
Mark Bloch16c19752018-01-01 13:06:58 +02004757 return 0;
4758}
Eli Cohene126ba92013-07-07 17:25:49 +03004759
Mark Bloch16c19752018-01-01 13:06:58 +02004760static void mlx5_ib_stage_roce_cleanup(struct mlx5_ib_dev *dev)
4761{
4762 struct mlx5_core_dev *mdev = dev->mdev;
4763 enum rdma_link_layer ll;
4764 int port_type_cap;
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004765 u8 port_num;
Eli Cohene126ba92013-07-07 17:25:49 +03004766
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004767 port_num = mlx5_core_native_port_num(dev->mdev) - 1;
Mark Bloch16c19752018-01-01 13:06:58 +02004768 port_type_cap = MLX5_CAP_GEN(mdev, port_type);
4769 ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
4770
4771 if (ll == IB_LINK_LAYER_ETHERNET) {
4772 mlx5_disable_eth(dev);
Daniel Jurgens7fd8aef2018-01-04 17:25:35 +02004773 mlx5_remove_netdev_notifier(dev, port_num);
Kamal Heib45bded22017-01-18 14:10:32 +02004774 }
Mark Bloch16c19752018-01-01 13:06:58 +02004775}
Haggai Eran6aec21f2014-12-11 17:04:23 +02004776
Mark Bloch16c19752018-01-01 13:06:58 +02004777static int mlx5_ib_stage_dev_res_init(struct mlx5_ib_dev *dev)
4778{
4779 return create_dev_resources(&dev->devr);
4780}
Parav Pandit4a2da0b2017-05-30 10:05:15 +03004781
Mark Bloch16c19752018-01-01 13:06:58 +02004782static void mlx5_ib_stage_dev_res_cleanup(struct mlx5_ib_dev *dev)
4783{
4784 destroy_dev_resources(&dev->devr);
4785}
4786
4787static int mlx5_ib_stage_odp_init(struct mlx5_ib_dev *dev)
4788{
Mark Bloch07321b32018-01-01 13:07:00 +02004789 mlx5_ib_internal_fill_odp_caps(dev);
4790
Mark Bloch16c19752018-01-01 13:06:58 +02004791 return mlx5_ib_odp_init_one(dev);
4792}
4793
Mark Bloch16c19752018-01-01 13:06:58 +02004794static int mlx5_ib_stage_counters_init(struct mlx5_ib_dev *dev)
4795{
Mark Bloch5e1e7612018-01-01 13:07:01 +02004796 if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt)) {
4797 dev->ib_dev.get_hw_stats = mlx5_ib_get_hw_stats;
4798 dev->ib_dev.alloc_hw_stats = mlx5_ib_alloc_hw_stats;
4799
4800 return mlx5_ib_alloc_counters(dev);
4801 }
Mark Bloch16c19752018-01-01 13:06:58 +02004802
4803 return 0;
4804}
4805
4806static void mlx5_ib_stage_counters_cleanup(struct mlx5_ib_dev *dev)
4807{
4808 if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt))
4809 mlx5_ib_dealloc_counters(dev);
4810}
4811
4812static int mlx5_ib_stage_cong_debugfs_init(struct mlx5_ib_dev *dev)
4813{
Parav Pandita9e546e2018-01-04 17:25:39 +02004814 return mlx5_ib_init_cong_debugfs(dev,
4815 mlx5_core_native_port_num(dev->mdev) - 1);
Mark Bloch16c19752018-01-01 13:06:58 +02004816}
4817
4818static void mlx5_ib_stage_cong_debugfs_cleanup(struct mlx5_ib_dev *dev)
4819{
Parav Pandita9e546e2018-01-04 17:25:39 +02004820 mlx5_ib_cleanup_cong_debugfs(dev,
4821 mlx5_core_native_port_num(dev->mdev) - 1);
Mark Bloch16c19752018-01-01 13:06:58 +02004822}
4823
4824static int mlx5_ib_stage_uar_init(struct mlx5_ib_dev *dev)
4825{
Eli Cohen5fe9dec2017-01-03 23:55:25 +02004826 dev->mdev->priv.uar = mlx5_get_uars_page(dev->mdev);
4827 if (!dev->mdev->priv.uar)
Mark Bloch16c19752018-01-01 13:06:58 +02004828 return -ENOMEM;
4829 return 0;
4830}
4831
4832static void mlx5_ib_stage_uar_cleanup(struct mlx5_ib_dev *dev)
4833{
4834 mlx5_put_uars_page(dev->mdev, dev->mdev->priv.uar);
4835}
4836
4837static int mlx5_ib_stage_bfrag_init(struct mlx5_ib_dev *dev)
4838{
4839 int err;
Eli Cohen5fe9dec2017-01-03 23:55:25 +02004840
4841 err = mlx5_alloc_bfreg(dev->mdev, &dev->bfreg, false, false);
4842 if (err)
Mark Bloch16c19752018-01-01 13:06:58 +02004843 return err;
Eli Cohen5fe9dec2017-01-03 23:55:25 +02004844
4845 err = mlx5_alloc_bfreg(dev->mdev, &dev->fp_bfreg, false, true);
4846 if (err)
Mark Bloch16c19752018-01-01 13:06:58 +02004847 mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
Eli Cohen5fe9dec2017-01-03 23:55:25 +02004848
Mark Bloch16c19752018-01-01 13:06:58 +02004849 return err;
4850}
Mark Bloch0837e862016-06-17 15:10:55 +03004851
Mark Bloch16c19752018-01-01 13:06:58 +02004852static void mlx5_ib_stage_bfrag_cleanup(struct mlx5_ib_dev *dev)
4853{
4854 mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
4855 mlx5_free_bfreg(dev->mdev, &dev->bfreg);
4856}
Eli Cohene126ba92013-07-07 17:25:49 +03004857
Mark Bloch16c19752018-01-01 13:06:58 +02004858static int mlx5_ib_stage_ib_reg_init(struct mlx5_ib_dev *dev)
4859{
4860 return ib_register_device(&dev->ib_dev, NULL);
4861}
4862
4863static void mlx5_ib_stage_ib_reg_cleanup(struct mlx5_ib_dev *dev)
4864{
4865 ib_unregister_device(&dev->ib_dev);
4866}
4867
4868static int mlx5_ib_stage_umr_res_init(struct mlx5_ib_dev *dev)
4869{
4870 return create_umr_res(dev);
4871}
4872
4873static void mlx5_ib_stage_umr_res_cleanup(struct mlx5_ib_dev *dev)
4874{
4875 destroy_umrc_res(dev);
4876}
4877
4878static int mlx5_ib_stage_delay_drop_init(struct mlx5_ib_dev *dev)
4879{
Maor Gottlieb03404e82017-05-30 10:29:13 +03004880 init_delay_drop(dev);
4881
Mark Bloch16c19752018-01-01 13:06:58 +02004882 return 0;
4883}
4884
4885static void mlx5_ib_stage_delay_drop_cleanup(struct mlx5_ib_dev *dev)
4886{
4887 cancel_delay_drop(dev);
4888}
4889
4890static int mlx5_ib_stage_class_attr_init(struct mlx5_ib_dev *dev)
4891{
4892 int err;
4893 int i;
4894
Eli Cohene126ba92013-07-07 17:25:49 +03004895 for (i = 0; i < ARRAY_SIZE(mlx5_class_attributes); i++) {
Wei Yongjun281d1a92013-07-30 07:54:26 +08004896 err = device_create_file(&dev->ib_dev.dev,
4897 mlx5_class_attributes[i]);
4898 if (err)
Mark Bloch16c19752018-01-01 13:06:58 +02004899 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03004900 }
4901
Mark Bloch16c19752018-01-01 13:06:58 +02004902 return 0;
4903}
4904
Mark Bloch16c19752018-01-01 13:06:58 +02004905static void __mlx5_ib_remove(struct mlx5_ib_dev *dev,
4906 const struct mlx5_ib_profile *profile,
4907 int stage)
4908{
4909 /* Number of stages to cleanup */
4910 while (stage) {
4911 stage--;
4912 if (profile->stage[stage].cleanup)
4913 profile->stage[stage].cleanup(dev);
4914 }
4915
4916 ib_dealloc_device((struct ib_device *)dev);
4917}
4918
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004919static void *mlx5_ib_add_slave_port(struct mlx5_core_dev *mdev, u8 port_num);
4920
Mark Bloch16c19752018-01-01 13:06:58 +02004921static void *__mlx5_ib_add(struct mlx5_core_dev *mdev,
4922 const struct mlx5_ib_profile *profile)
4923{
4924 struct mlx5_ib_dev *dev;
4925 int err;
4926 int i;
4927
4928 printk_once(KERN_INFO "%s", mlx5_version);
4929
4930 dev = (struct mlx5_ib_dev *)ib_alloc_device(sizeof(*dev));
4931 if (!dev)
4932 return NULL;
4933
4934 dev->mdev = mdev;
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004935 dev->num_ports = max(MLX5_CAP_GEN(mdev, num_ports),
4936 MLX5_CAP_GEN(mdev, num_vhca_ports));
Mark Bloch16c19752018-01-01 13:06:58 +02004937
4938 for (i = 0; i < MLX5_IB_STAGE_MAX; i++) {
4939 if (profile->stage[i].init) {
4940 err = profile->stage[i].init(dev);
4941 if (err)
4942 goto err_out;
4943 }
4944 }
4945
4946 dev->profile = profile;
Eli Cohene126ba92013-07-07 17:25:49 +03004947 dev->ib_active = true;
4948
Jack Morgenstein9603b612014-07-28 23:30:22 +03004949 return dev;
Eli Cohene126ba92013-07-07 17:25:49 +03004950
Mark Bloch16c19752018-01-01 13:06:58 +02004951err_out:
4952 __mlx5_ib_remove(dev, profile, i);
Eli Cohene126ba92013-07-07 17:25:49 +03004953
Jack Morgenstein9603b612014-07-28 23:30:22 +03004954 return NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03004955}
4956
Mark Bloch16c19752018-01-01 13:06:58 +02004957static const struct mlx5_ib_profile pf_profile = {
4958 STAGE_CREATE(MLX5_IB_STAGE_INIT,
4959 mlx5_ib_stage_init_init,
4960 mlx5_ib_stage_init_cleanup),
4961 STAGE_CREATE(MLX5_IB_STAGE_CAPS,
4962 mlx5_ib_stage_caps_init,
4963 NULL),
4964 STAGE_CREATE(MLX5_IB_STAGE_ROCE,
4965 mlx5_ib_stage_roce_init,
4966 mlx5_ib_stage_roce_cleanup),
4967 STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES,
4968 mlx5_ib_stage_dev_res_init,
4969 mlx5_ib_stage_dev_res_cleanup),
4970 STAGE_CREATE(MLX5_IB_STAGE_ODP,
4971 mlx5_ib_stage_odp_init,
Mark Bloch3cc297d2018-01-01 13:07:03 +02004972 NULL),
Mark Bloch16c19752018-01-01 13:06:58 +02004973 STAGE_CREATE(MLX5_IB_STAGE_COUNTERS,
4974 mlx5_ib_stage_counters_init,
4975 mlx5_ib_stage_counters_cleanup),
4976 STAGE_CREATE(MLX5_IB_STAGE_CONG_DEBUGFS,
4977 mlx5_ib_stage_cong_debugfs_init,
4978 mlx5_ib_stage_cong_debugfs_cleanup),
4979 STAGE_CREATE(MLX5_IB_STAGE_UAR,
4980 mlx5_ib_stage_uar_init,
4981 mlx5_ib_stage_uar_cleanup),
4982 STAGE_CREATE(MLX5_IB_STAGE_BFREG,
4983 mlx5_ib_stage_bfrag_init,
4984 mlx5_ib_stage_bfrag_cleanup),
4985 STAGE_CREATE(MLX5_IB_STAGE_IB_REG,
4986 mlx5_ib_stage_ib_reg_init,
4987 mlx5_ib_stage_ib_reg_cleanup),
4988 STAGE_CREATE(MLX5_IB_STAGE_UMR_RESOURCES,
4989 mlx5_ib_stage_umr_res_init,
4990 mlx5_ib_stage_umr_res_cleanup),
4991 STAGE_CREATE(MLX5_IB_STAGE_DELAY_DROP,
4992 mlx5_ib_stage_delay_drop_init,
4993 mlx5_ib_stage_delay_drop_cleanup),
4994 STAGE_CREATE(MLX5_IB_STAGE_CLASS_ATTR,
4995 mlx5_ib_stage_class_attr_init,
4996 NULL),
Mark Bloch16c19752018-01-01 13:06:58 +02004997};
4998
Daniel Jurgens32f69e42018-01-04 17:25:36 +02004999static void *mlx5_ib_add_slave_port(struct mlx5_core_dev *mdev, u8 port_num)
5000{
5001 struct mlx5_ib_multiport_info *mpi;
5002 struct mlx5_ib_dev *dev;
5003 bool bound = false;
5004 int err;
5005
5006 mpi = kzalloc(sizeof(*mpi), GFP_KERNEL);
5007 if (!mpi)
5008 return NULL;
5009
5010 mpi->mdev = mdev;
5011
5012 err = mlx5_query_nic_vport_system_image_guid(mdev,
5013 &mpi->sys_image_guid);
5014 if (err) {
5015 kfree(mpi);
5016 return NULL;
5017 }
5018
5019 mutex_lock(&mlx5_ib_multiport_mutex);
5020 list_for_each_entry(dev, &mlx5_ib_dev_list, ib_dev_list) {
5021 if (dev->sys_image_guid == mpi->sys_image_guid)
5022 bound = mlx5_ib_bind_slave_port(dev, mpi);
5023
5024 if (bound) {
5025 rdma_roce_rescan_device(&dev->ib_dev);
5026 break;
5027 }
5028 }
5029
5030 if (!bound) {
5031 list_add_tail(&mpi->list, &mlx5_ib_unaffiliated_port_list);
5032 dev_dbg(&mdev->pdev->dev, "no suitable IB device found to bind to, added to unaffiliated list.\n");
5033 } else {
5034 mlx5_ib_dbg(dev, "bound port %u\n", port_num + 1);
5035 }
5036 mutex_unlock(&mlx5_ib_multiport_mutex);
5037
5038 return mpi;
5039}
5040
Mark Bloch16c19752018-01-01 13:06:58 +02005041static void *mlx5_ib_add(struct mlx5_core_dev *mdev)
5042{
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005043 enum rdma_link_layer ll;
5044 int port_type_cap;
5045
5046 port_type_cap = MLX5_CAP_GEN(mdev, port_type);
5047 ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
5048
5049 if (mlx5_core_is_mp_slave(mdev) && ll == IB_LINK_LAYER_ETHERNET) {
5050 u8 port_num = mlx5_core_native_port_num(mdev) - 1;
5051
5052 return mlx5_ib_add_slave_port(mdev, port_num);
5053 }
5054
Mark Bloch16c19752018-01-01 13:06:58 +02005055 return __mlx5_ib_add(mdev, &pf_profile);
5056}
5057
Jack Morgenstein9603b612014-07-28 23:30:22 +03005058static void mlx5_ib_remove(struct mlx5_core_dev *mdev, void *context)
Eli Cohene126ba92013-07-07 17:25:49 +03005059{
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005060 struct mlx5_ib_multiport_info *mpi;
5061 struct mlx5_ib_dev *dev;
Haggai Eran6aec21f2014-12-11 17:04:23 +02005062
Daniel Jurgens32f69e42018-01-04 17:25:36 +02005063 if (mlx5_core_is_mp_slave(mdev)) {
5064 mpi = context;
5065 mutex_lock(&mlx5_ib_multiport_mutex);
5066 if (mpi->ibdev)
5067 mlx5_ib_unbind_slave_port(mpi->ibdev, mpi);
5068 list_del(&mpi->list);
5069 mutex_unlock(&mlx5_ib_multiport_mutex);
5070 return;
5071 }
5072
5073 dev = context;
Mark Bloch16c19752018-01-01 13:06:58 +02005074 __mlx5_ib_remove(dev, dev->profile, MLX5_IB_STAGE_MAX);
Eli Cohene126ba92013-07-07 17:25:49 +03005075}
5076
Jack Morgenstein9603b612014-07-28 23:30:22 +03005077static struct mlx5_interface mlx5_ib_interface = {
5078 .add = mlx5_ib_add,
5079 .remove = mlx5_ib_remove,
5080 .event = mlx5_ib_event,
Artemy Kovalyovd9aaed82017-01-02 11:37:46 +02005081#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
5082 .pfault = mlx5_ib_pfault,
5083#endif
Saeed Mahameed64613d942015-04-02 17:07:34 +03005084 .protocol = MLX5_INTERFACE_PROTOCOL_IB,
Eli Cohene126ba92013-07-07 17:25:49 +03005085};
5086
5087static int __init mlx5_ib_init(void)
5088{
Haggai Eran6aec21f2014-12-11 17:04:23 +02005089 int err;
5090
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02005091 mlx5_ib_event_wq = alloc_ordered_workqueue("mlx5_ib_event_wq", 0);
5092 if (!mlx5_ib_event_wq)
5093 return -ENOMEM;
5094
Artemy Kovalyov81713d32017-01-18 16:58:11 +02005095 mlx5_ib_odp_init();
Jack Morgenstein9603b612014-07-28 23:30:22 +03005096
Haggai Eran6aec21f2014-12-11 17:04:23 +02005097 err = mlx5_register_interface(&mlx5_ib_interface);
Haggai Eran6aec21f2014-12-11 17:04:23 +02005098
5099 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03005100}
5101
5102static void __exit mlx5_ib_cleanup(void)
5103{
Jack Morgenstein9603b612014-07-28 23:30:22 +03005104 mlx5_unregister_interface(&mlx5_ib_interface);
Daniel Jurgensd69a24e2018-01-04 17:25:37 +02005105 destroy_workqueue(mlx5_ib_event_wq);
Eli Cohene126ba92013-07-07 17:25:49 +03005106}
5107
5108module_init(mlx5_ib_init);
5109module_exit(mlx5_ib_cleanup);