blob: c28b6952b0abf625d0b73409d590276b7209c99f [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
Christoph Hellwigadec6402015-08-28 09:27:19 +020033#include <linux/highmem.h>
Eli Cohene126ba92013-07-07 17:25:49 +030034#include <linux/module.h>
35#include <linux/init.h>
36#include <linux/errno.h>
37#include <linux/pci.h>
38#include <linux/dma-mapping.h>
39#include <linux/slab.h>
Guy Levi37aa5c32016-04-27 16:49:50 +030040#if defined(CONFIG_X86)
41#include <asm/pat.h>
42#endif
Eli Cohene126ba92013-07-07 17:25:49 +030043#include <linux/sched.h>
Ingo Molnar6e84f312017-02-08 18:51:29 +010044#include <linux/sched/mm.h>
Ingo Molnar0881e7b2017-02-05 15:30:50 +010045#include <linux/sched/task.h>
Maor Gottlieb7c2344c2016-06-17 14:56:44 +030046#include <linux/delay.h>
Eli Cohene126ba92013-07-07 17:25:49 +030047#include <rdma/ib_user_verbs.h>
Achiad Shochat3f89a642015-12-23 18:47:21 +020048#include <rdma/ib_addr.h>
Achiad Shochat2811ba52015-12-23 18:47:24 +020049#include <rdma/ib_cache.h>
Achiad Shochatada68c32016-02-22 18:17:23 +020050#include <linux/mlx5/port.h>
Majd Dibbiny1b5daf12015-06-04 19:30:46 +030051#include <linux/mlx5/vport.h>
Maor Gottlieb7c2344c2016-06-17 14:56:44 +030052#include <linux/list.h>
Eli Cohene126ba92013-07-07 17:25:49 +030053#include <rdma/ib_smi.h>
54#include <rdma/ib_umem.h>
Maor Gottlieb038d2ef2016-01-11 10:26:07 +020055#include <linux/in.h>
56#include <linux/etherdevice.h>
57#include <linux/mlx5/fs.h>
Or Gerlitz78984892016-11-30 20:33:33 +020058#include <linux/mlx5/vport.h>
Eli Cohene126ba92013-07-07 17:25:49 +030059#include "mlx5_ib.h"
60
61#define DRIVER_NAME "mlx5_ib"
Amir Vadai169a1d82014-02-19 17:47:31 +020062#define DRIVER_VERSION "2.2-1"
63#define DRIVER_RELDATE "Feb 2014"
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");
68MODULE_VERSION(DRIVER_VERSION);
69
Eli Cohene126ba92013-07-07 17:25:49 +030070static char mlx5_version[] =
71 DRIVER_NAME ": Mellanox Connect-IB Infiniband driver v"
72 DRIVER_VERSION " (" DRIVER_RELDATE ")\n";
73
Eran Ben Elishada7525d2015-12-14 16:34:10 +020074enum {
75 MLX5_ATOMIC_SIZE_QP_8BYTES = 1 << 3,
76};
Majd Dibbiny1b5daf12015-06-04 19:30:46 +030077
Majd Dibbiny1b5daf12015-06-04 19:30:46 +030078static enum rdma_link_layer
Achiad Shochatebd61f62015-12-23 18:47:16 +020079mlx5_port_type_cap_to_rdma_ll(int port_type_cap)
Majd Dibbiny1b5daf12015-06-04 19:30:46 +030080{
Achiad Shochatebd61f62015-12-23 18:47:16 +020081 switch (port_type_cap) {
Majd Dibbiny1b5daf12015-06-04 19:30:46 +030082 case MLX5_CAP_PORT_TYPE_IB:
83 return IB_LINK_LAYER_INFINIBAND;
84 case MLX5_CAP_PORT_TYPE_ETH:
85 return IB_LINK_LAYER_ETHERNET;
86 default:
87 return IB_LINK_LAYER_UNSPECIFIED;
88 }
89}
90
Achiad Shochatebd61f62015-12-23 18:47:16 +020091static enum rdma_link_layer
92mlx5_ib_port_link_layer(struct ib_device *device, u8 port_num)
93{
94 struct mlx5_ib_dev *dev = to_mdev(device);
95 int port_type_cap = MLX5_CAP_GEN(dev->mdev, port_type);
96
97 return mlx5_port_type_cap_to_rdma_ll(port_type_cap);
98}
99
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200100static int mlx5_netdev_event(struct notifier_block *this,
101 unsigned long event, void *ptr)
102{
103 struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
104 struct mlx5_ib_dev *ibdev = container_of(this, struct mlx5_ib_dev,
105 roce.nb);
106
Aviv Heller5ec8c832016-09-18 20:48:00 +0300107 switch (event) {
108 case NETDEV_REGISTER:
109 case NETDEV_UNREGISTER:
110 write_lock(&ibdev->roce.netdev_lock);
111 if (ndev->dev.parent == &ibdev->mdev->pdev->dev)
112 ibdev->roce.netdev = (event == NETDEV_UNREGISTER) ?
113 NULL : ndev;
114 write_unlock(&ibdev->roce.netdev_lock);
115 break;
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200116
Aviv Heller5ec8c832016-09-18 20:48:00 +0300117 case NETDEV_UP:
Aviv Heller88621df2016-09-18 20:48:02 +0300118 case NETDEV_DOWN: {
119 struct net_device *lag_ndev = mlx5_lag_get_roce_netdev(ibdev->mdev);
120 struct net_device *upper = NULL;
121
122 if (lag_ndev) {
123 upper = netdev_master_upper_dev_get(lag_ndev);
124 dev_put(lag_ndev);
125 }
126
127 if ((upper == ndev || (!upper && ndev == ibdev->roce.netdev))
128 && ibdev->ib_active) {
Bart Van Assche626bc022016-12-05 17:18:08 -0800129 struct ib_event ibev = { };
Aviv Heller5ec8c832016-09-18 20:48:00 +0300130
131 ibev.device = &ibdev->ib_dev;
132 ibev.event = (event == NETDEV_UP) ?
133 IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
134 ibev.element.port_num = 1;
135 ib_dispatch_event(&ibev);
136 }
137 break;
Aviv Heller88621df2016-09-18 20:48:02 +0300138 }
Aviv Heller5ec8c832016-09-18 20:48:00 +0300139
140 default:
141 break;
142 }
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200143
144 return NOTIFY_DONE;
145}
146
147static struct net_device *mlx5_ib_get_netdev(struct ib_device *device,
148 u8 port_num)
149{
150 struct mlx5_ib_dev *ibdev = to_mdev(device);
151 struct net_device *ndev;
152
Aviv Heller88621df2016-09-18 20:48:02 +0300153 ndev = mlx5_lag_get_roce_netdev(ibdev->mdev);
154 if (ndev)
155 return ndev;
156
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200157 /* Ensure ndev does not disappear before we invoke dev_hold()
158 */
159 read_lock(&ibdev->roce.netdev_lock);
160 ndev = ibdev->roce.netdev;
161 if (ndev)
162 dev_hold(ndev);
163 read_unlock(&ibdev->roce.netdev_lock);
164
165 return ndev;
166}
167
Achiad Shochat3f89a642015-12-23 18:47:21 +0200168static int mlx5_query_port_roce(struct ib_device *device, u8 port_num,
169 struct ib_port_attr *props)
170{
171 struct mlx5_ib_dev *dev = to_mdev(device);
Aviv Heller88621df2016-09-18 20:48:02 +0300172 struct net_device *ndev, *upper;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200173 enum ib_mtu ndev_ib_mtu;
Leon Romanovskyc876a1b2016-01-09 13:06:25 +0200174 u16 qkey_viol_cntr;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200175
Or Gerlitzc4550c62017-01-24 13:02:39 +0200176 /* props being zeroed by the caller, avoid zeroing it here */
Achiad Shochat3f89a642015-12-23 18:47:21 +0200177
178 props->port_cap_flags |= IB_PORT_CM_SUP;
179 props->port_cap_flags |= IB_PORT_IP_BASED_GIDS;
180
181 props->gid_tbl_len = MLX5_CAP_ROCE(dev->mdev,
182 roce_address_table_size);
183 props->max_mtu = IB_MTU_4096;
184 props->max_msg_sz = 1 << MLX5_CAP_GEN(dev->mdev, log_max_msg);
185 props->pkey_tbl_len = 1;
186 props->state = IB_PORT_DOWN;
187 props->phys_state = 3;
188
Leon Romanovskyc876a1b2016-01-09 13:06:25 +0200189 mlx5_query_nic_vport_qkey_viol_cntr(dev->mdev, &qkey_viol_cntr);
190 props->qkey_viol_cntr = qkey_viol_cntr;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200191
192 ndev = mlx5_ib_get_netdev(device, port_num);
193 if (!ndev)
194 return 0;
195
Aviv Heller88621df2016-09-18 20:48:02 +0300196 if (mlx5_lag_is_active(dev->mdev)) {
197 rcu_read_lock();
198 upper = netdev_master_upper_dev_get_rcu(ndev);
199 if (upper) {
200 dev_put(ndev);
201 ndev = upper;
202 dev_hold(ndev);
203 }
204 rcu_read_unlock();
205 }
206
Achiad Shochat3f89a642015-12-23 18:47:21 +0200207 if (netif_running(ndev) && netif_carrier_ok(ndev)) {
208 props->state = IB_PORT_ACTIVE;
209 props->phys_state = 5;
210 }
211
212 ndev_ib_mtu = iboe_get_mtu(ndev->mtu);
213
214 dev_put(ndev);
215
216 props->active_mtu = min(props->max_mtu, ndev_ib_mtu);
217
218 props->active_width = IB_WIDTH_4X; /* TODO */
219 props->active_speed = IB_SPEED_QDR; /* TODO */
220
221 return 0;
222}
223
Achiad Shochat3cca2602015-12-23 18:47:23 +0200224static void ib_gid_to_mlx5_roce_addr(const union ib_gid *gid,
225 const struct ib_gid_attr *attr,
226 void *mlx5_addr)
227{
228#define MLX5_SET_RA(p, f, v) MLX5_SET(roce_addr_layout, p, f, v)
229 char *mlx5_addr_l3_addr = MLX5_ADDR_OF(roce_addr_layout, mlx5_addr,
230 source_l3_address);
231 void *mlx5_addr_mac = MLX5_ADDR_OF(roce_addr_layout, mlx5_addr,
232 source_mac_47_32);
233
234 if (!gid)
235 return;
236
237 ether_addr_copy(mlx5_addr_mac, attr->ndev->dev_addr);
238
239 if (is_vlan_dev(attr->ndev)) {
240 MLX5_SET_RA(mlx5_addr, vlan_valid, 1);
241 MLX5_SET_RA(mlx5_addr, vlan_id, vlan_dev_vlan_id(attr->ndev));
242 }
243
244 switch (attr->gid_type) {
245 case IB_GID_TYPE_IB:
246 MLX5_SET_RA(mlx5_addr, roce_version, MLX5_ROCE_VERSION_1);
247 break;
248 case IB_GID_TYPE_ROCE_UDP_ENCAP:
249 MLX5_SET_RA(mlx5_addr, roce_version, MLX5_ROCE_VERSION_2);
250 break;
251
252 default:
253 WARN_ON(true);
254 }
255
256 if (attr->gid_type != IB_GID_TYPE_IB) {
257 if (ipv6_addr_v4mapped((void *)gid))
258 MLX5_SET_RA(mlx5_addr, roce_l3_type,
259 MLX5_ROCE_L3_TYPE_IPV4);
260 else
261 MLX5_SET_RA(mlx5_addr, roce_l3_type,
262 MLX5_ROCE_L3_TYPE_IPV6);
263 }
264
265 if ((attr->gid_type == IB_GID_TYPE_IB) ||
266 !ipv6_addr_v4mapped((void *)gid))
267 memcpy(mlx5_addr_l3_addr, gid, sizeof(*gid));
268 else
269 memcpy(&mlx5_addr_l3_addr[12], &gid->raw[12], 4);
270}
271
272static int set_roce_addr(struct ib_device *device, u8 port_num,
273 unsigned int index,
274 const union ib_gid *gid,
275 const struct ib_gid_attr *attr)
276{
Saeed Mahameedc4f287c2016-07-19 20:17:12 +0300277 struct mlx5_ib_dev *dev = to_mdev(device);
278 u32 in[MLX5_ST_SZ_DW(set_roce_address_in)] = {0};
279 u32 out[MLX5_ST_SZ_DW(set_roce_address_out)] = {0};
Achiad Shochat3cca2602015-12-23 18:47:23 +0200280 void *in_addr = MLX5_ADDR_OF(set_roce_address_in, in, roce_address);
281 enum rdma_link_layer ll = mlx5_ib_port_link_layer(device, port_num);
282
283 if (ll != IB_LINK_LAYER_ETHERNET)
284 return -EINVAL;
285
Achiad Shochat3cca2602015-12-23 18:47:23 +0200286 ib_gid_to_mlx5_roce_addr(gid, attr, in_addr);
287
288 MLX5_SET(set_roce_address_in, in, roce_address_index, index);
289 MLX5_SET(set_roce_address_in, in, opcode, MLX5_CMD_OP_SET_ROCE_ADDRESS);
Achiad Shochat3cca2602015-12-23 18:47:23 +0200290 return mlx5_cmd_exec(dev->mdev, in, sizeof(in), out, sizeof(out));
291}
292
293static int mlx5_ib_add_gid(struct ib_device *device, u8 port_num,
294 unsigned int index, const union ib_gid *gid,
295 const struct ib_gid_attr *attr,
296 __always_unused void **context)
297{
298 return set_roce_addr(device, port_num, index, gid, attr);
299}
300
301static int mlx5_ib_del_gid(struct ib_device *device, u8 port_num,
302 unsigned int index, __always_unused void **context)
303{
304 return set_roce_addr(device, port_num, index, NULL, NULL);
305}
306
Achiad Shochat2811ba52015-12-23 18:47:24 +0200307__be16 mlx5_get_roce_udp_sport(struct mlx5_ib_dev *dev, u8 port_num,
308 int index)
309{
310 struct ib_gid_attr attr;
311 union ib_gid gid;
312
313 if (ib_get_cached_gid(&dev->ib_dev, port_num, index, &gid, &attr))
314 return 0;
315
316 if (!attr.ndev)
317 return 0;
318
319 dev_put(attr.ndev);
320
321 if (attr.gid_type != IB_GID_TYPE_ROCE_UDP_ENCAP)
322 return 0;
323
324 return cpu_to_be16(MLX5_CAP_ROCE(dev->mdev, r_roce_min_src_udp_port));
325}
326
Majd Dibbinyed884512017-01-18 14:10:35 +0200327int mlx5_get_roce_gid_type(struct mlx5_ib_dev *dev, u8 port_num,
328 int index, enum ib_gid_type *gid_type)
329{
330 struct ib_gid_attr attr;
331 union ib_gid gid;
332 int ret;
333
334 ret = ib_get_cached_gid(&dev->ib_dev, port_num, index, &gid, &attr);
335 if (ret)
336 return ret;
337
338 if (!attr.ndev)
339 return -ENODEV;
340
341 dev_put(attr.ndev);
342
343 *gid_type = attr.gid_type;
344
345 return 0;
346}
347
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300348static int mlx5_use_mad_ifc(struct mlx5_ib_dev *dev)
349{
Noa Osherovich7fae6652016-09-12 19:16:23 +0300350 if (MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_IB)
351 return !MLX5_CAP_GEN(dev->mdev, ib_virt);
352 return 0;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300353}
354
355enum {
356 MLX5_VPORT_ACCESS_METHOD_MAD,
357 MLX5_VPORT_ACCESS_METHOD_HCA,
358 MLX5_VPORT_ACCESS_METHOD_NIC,
359};
360
361static int mlx5_get_vport_access_method(struct ib_device *ibdev)
362{
363 if (mlx5_use_mad_ifc(to_mdev(ibdev)))
364 return MLX5_VPORT_ACCESS_METHOD_MAD;
365
Achiad Shochatebd61f62015-12-23 18:47:16 +0200366 if (mlx5_ib_port_link_layer(ibdev, 1) ==
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300367 IB_LINK_LAYER_ETHERNET)
368 return MLX5_VPORT_ACCESS_METHOD_NIC;
369
370 return MLX5_VPORT_ACCESS_METHOD_HCA;
371}
372
Eran Ben Elishada7525d2015-12-14 16:34:10 +0200373static void get_atomic_caps(struct mlx5_ib_dev *dev,
374 struct ib_device_attr *props)
375{
376 u8 tmp;
377 u8 atomic_operations = MLX5_CAP_ATOMIC(dev->mdev, atomic_operations);
378 u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_qp);
379 u8 atomic_req_8B_endianness_mode =
380 MLX5_CAP_ATOMIC(dev->mdev, atomic_req_8B_endianess_mode);
381
382 /* Check if HW supports 8 bytes standard atomic operations and capable
383 * of host endianness respond
384 */
385 tmp = MLX5_ATOMIC_OPS_CMP_SWAP | MLX5_ATOMIC_OPS_FETCH_ADD;
386 if (((atomic_operations & tmp) == tmp) &&
387 (atomic_size_qp & MLX5_ATOMIC_SIZE_QP_8BYTES) &&
388 (atomic_req_8B_endianness_mode)) {
389 props->atomic_cap = IB_ATOMIC_HCA;
390 } else {
391 props->atomic_cap = IB_ATOMIC_NONE;
392 }
393}
394
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300395static int mlx5_query_system_image_guid(struct ib_device *ibdev,
396 __be64 *sys_image_guid)
397{
398 struct mlx5_ib_dev *dev = to_mdev(ibdev);
399 struct mlx5_core_dev *mdev = dev->mdev;
400 u64 tmp;
401 int err;
402
403 switch (mlx5_get_vport_access_method(ibdev)) {
404 case MLX5_VPORT_ACCESS_METHOD_MAD:
405 return mlx5_query_mad_ifc_system_image_guid(ibdev,
406 sys_image_guid);
407
408 case MLX5_VPORT_ACCESS_METHOD_HCA:
409 err = mlx5_query_hca_vport_system_image_guid(mdev, &tmp);
Achiad Shochat3f89a642015-12-23 18:47:21 +0200410 break;
411
412 case MLX5_VPORT_ACCESS_METHOD_NIC:
413 err = mlx5_query_nic_vport_system_image_guid(mdev, &tmp);
414 break;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300415
416 default:
417 return -EINVAL;
418 }
Achiad Shochat3f89a642015-12-23 18:47:21 +0200419
420 if (!err)
421 *sys_image_guid = cpu_to_be64(tmp);
422
423 return err;
424
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300425}
426
427static int mlx5_query_max_pkeys(struct ib_device *ibdev,
428 u16 *max_pkeys)
429{
430 struct mlx5_ib_dev *dev = to_mdev(ibdev);
431 struct mlx5_core_dev *mdev = dev->mdev;
432
433 switch (mlx5_get_vport_access_method(ibdev)) {
434 case MLX5_VPORT_ACCESS_METHOD_MAD:
435 return mlx5_query_mad_ifc_max_pkeys(ibdev, max_pkeys);
436
437 case MLX5_VPORT_ACCESS_METHOD_HCA:
438 case MLX5_VPORT_ACCESS_METHOD_NIC:
439 *max_pkeys = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev,
440 pkey_table_size));
441 return 0;
442
443 default:
444 return -EINVAL;
445 }
446}
447
448static int mlx5_query_vendor_id(struct ib_device *ibdev,
449 u32 *vendor_id)
450{
451 struct mlx5_ib_dev *dev = to_mdev(ibdev);
452
453 switch (mlx5_get_vport_access_method(ibdev)) {
454 case MLX5_VPORT_ACCESS_METHOD_MAD:
455 return mlx5_query_mad_ifc_vendor_id(ibdev, vendor_id);
456
457 case MLX5_VPORT_ACCESS_METHOD_HCA:
458 case MLX5_VPORT_ACCESS_METHOD_NIC:
459 return mlx5_core_query_vendor_id(dev->mdev, vendor_id);
460
461 default:
462 return -EINVAL;
463 }
464}
465
466static int mlx5_query_node_guid(struct mlx5_ib_dev *dev,
467 __be64 *node_guid)
468{
469 u64 tmp;
470 int err;
471
472 switch (mlx5_get_vport_access_method(&dev->ib_dev)) {
473 case MLX5_VPORT_ACCESS_METHOD_MAD:
474 return mlx5_query_mad_ifc_node_guid(dev, node_guid);
475
476 case MLX5_VPORT_ACCESS_METHOD_HCA:
477 err = mlx5_query_hca_vport_node_guid(dev->mdev, &tmp);
Achiad Shochat3f89a642015-12-23 18:47:21 +0200478 break;
479
480 case MLX5_VPORT_ACCESS_METHOD_NIC:
481 err = mlx5_query_nic_vport_node_guid(dev->mdev, &tmp);
482 break;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300483
484 default:
485 return -EINVAL;
486 }
Achiad Shochat3f89a642015-12-23 18:47:21 +0200487
488 if (!err)
489 *node_guid = cpu_to_be64(tmp);
490
491 return err;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300492}
493
494struct mlx5_reg_node_desc {
Yuval Shaiabd99fde2016-08-25 10:57:07 -0700495 u8 desc[IB_DEVICE_NODE_DESC_MAX];
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300496};
497
498static int mlx5_query_node_desc(struct mlx5_ib_dev *dev, char *node_desc)
499{
500 struct mlx5_reg_node_desc in;
501
502 if (mlx5_use_mad_ifc(dev))
503 return mlx5_query_mad_ifc_node_desc(dev, node_desc);
504
505 memset(&in, 0, sizeof(in));
506
507 return mlx5_core_access_reg(dev->mdev, &in, sizeof(in), node_desc,
508 sizeof(struct mlx5_reg_node_desc),
509 MLX5_REG_NODE_DESC, 0, 0);
510}
511
Eli Cohene126ba92013-07-07 17:25:49 +0300512static int mlx5_ib_query_device(struct ib_device *ibdev,
Matan Barak2528e332015-06-11 16:35:25 +0300513 struct ib_device_attr *props,
514 struct ib_udata *uhw)
Eli Cohene126ba92013-07-07 17:25:49 +0300515{
516 struct mlx5_ib_dev *dev = to_mdev(ibdev);
Saeed Mahameed938fe832015-05-28 22:28:41 +0300517 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +0300518 int err = -ENOMEM;
Eli Cohen288c01b2016-10-27 16:36:45 +0300519 int max_sq_desc;
Eli Cohene126ba92013-07-07 17:25:49 +0300520 int max_rq_sg;
521 int max_sq_sg;
Sagi Grimberge0238a62015-07-21 14:40:12 +0300522 u64 min_page_size = 1ull << MLX5_CAP_GEN(mdev, log_pg_sz);
Bodong Wang402ca532016-06-17 15:02:20 +0300523 struct mlx5_ib_query_device_resp resp = {};
524 size_t resp_len;
525 u64 max_tso;
Eli Cohene126ba92013-07-07 17:25:49 +0300526
Bodong Wang402ca532016-06-17 15:02:20 +0300527 resp_len = sizeof(resp.comp_mask) + sizeof(resp.response_length);
528 if (uhw->outlen && uhw->outlen < resp_len)
529 return -EINVAL;
530 else
531 resp.response_length = resp_len;
532
533 if (uhw->inlen && !ib_is_udata_cleared(uhw, 0, uhw->inlen))
Matan Barak2528e332015-06-11 16:35:25 +0300534 return -EINVAL;
535
Eli Cohene126ba92013-07-07 17:25:49 +0300536 memset(props, 0, sizeof(*props));
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300537 err = mlx5_query_system_image_guid(ibdev,
538 &props->sys_image_guid);
539 if (err)
540 return err;
541
542 err = mlx5_query_max_pkeys(ibdev, &props->max_pkeys);
543 if (err)
544 return err;
545
546 err = mlx5_query_vendor_id(ibdev, &props->vendor_id);
547 if (err)
548 return err;
Eli Cohene126ba92013-07-07 17:25:49 +0300549
Jack Morgenstein9603b612014-07-28 23:30:22 +0300550 props->fw_ver = ((u64)fw_rev_maj(dev->mdev) << 32) |
551 (fw_rev_min(dev->mdev) << 16) |
552 fw_rev_sub(dev->mdev);
Eli Cohene126ba92013-07-07 17:25:49 +0300553 props->device_cap_flags = IB_DEVICE_CHANGE_PHY_PORT |
554 IB_DEVICE_PORT_ACTIVE_EVENT |
555 IB_DEVICE_SYS_IMAGE_GUID |
Eli Cohen1a4c3a32014-02-06 17:41:25 +0200556 IB_DEVICE_RC_RNR_NAK_GEN;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300557
558 if (MLX5_CAP_GEN(mdev, pkv))
Eli Cohene126ba92013-07-07 17:25:49 +0300559 props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300560 if (MLX5_CAP_GEN(mdev, qkv))
Eli Cohene126ba92013-07-07 17:25:49 +0300561 props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300562 if (MLX5_CAP_GEN(mdev, apm))
Eli Cohene126ba92013-07-07 17:25:49 +0300563 props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300564 if (MLX5_CAP_GEN(mdev, xrc))
Eli Cohene126ba92013-07-07 17:25:49 +0300565 props->device_cap_flags |= IB_DEVICE_XRC;
Matan Barakd2370e02016-02-29 18:05:30 +0200566 if (MLX5_CAP_GEN(mdev, imaicl)) {
567 props->device_cap_flags |= IB_DEVICE_MEM_WINDOW |
568 IB_DEVICE_MEM_WINDOW_TYPE_2B;
569 props->max_mw = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
Sagi Grimbergb005d312016-02-29 19:07:33 +0200570 /* We support 'Gappy' memory registration too */
571 props->device_cap_flags |= IB_DEVICE_SG_GAPS_REG;
Matan Barakd2370e02016-02-29 18:05:30 +0200572 }
Eli Cohene126ba92013-07-07 17:25:49 +0300573 props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300574 if (MLX5_CAP_GEN(mdev, sho)) {
Sagi Grimberg2dea9092014-02-23 14:19:13 +0200575 props->device_cap_flags |= IB_DEVICE_SIGNATURE_HANDOVER;
576 /* At this stage no support for signature handover */
577 props->sig_prot_cap = IB_PROT_T10DIF_TYPE_1 |
578 IB_PROT_T10DIF_TYPE_2 |
579 IB_PROT_T10DIF_TYPE_3;
580 props->sig_guard_cap = IB_GUARD_T10DIF_CRC |
581 IB_GUARD_T10DIF_CSUM;
582 }
Saeed Mahameed938fe832015-05-28 22:28:41 +0300583 if (MLX5_CAP_GEN(mdev, block_lb_mc))
Eli Cohenf360d882014-04-02 00:10:16 +0300584 props->device_cap_flags |= IB_DEVICE_BLOCK_MULTICAST_LOOPBACK;
Eli Cohene126ba92013-07-07 17:25:49 +0300585
Bodong Wang402ca532016-06-17 15:02:20 +0300586 if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads)) {
Noa Osheroviche8161332017-01-18 15:40:01 +0200587 if (MLX5_CAP_ETH(mdev, csum_cap)) {
588 /* Legacy bit to support old userspace libraries */
Bodong Wang88115fe2015-12-18 13:53:20 +0200589 props->device_cap_flags |= IB_DEVICE_RAW_IP_CSUM;
Noa Osheroviche8161332017-01-18 15:40:01 +0200590 props->raw_packet_caps |= IB_RAW_PACKET_CAP_IP_CSUM;
591 }
592
593 if (MLX5_CAP_ETH(dev->mdev, vlan_cap))
594 props->raw_packet_caps |=
595 IB_RAW_PACKET_CAP_CVLAN_STRIPPING;
Bodong Wang88115fe2015-12-18 13:53:20 +0200596
Bodong Wang402ca532016-06-17 15:02:20 +0300597 if (field_avail(typeof(resp), tso_caps, uhw->outlen)) {
598 max_tso = MLX5_CAP_ETH(mdev, max_lso_cap);
599 if (max_tso) {
600 resp.tso_caps.max_tso = 1 << max_tso;
601 resp.tso_caps.supported_qpts |=
602 1 << IB_QPT_RAW_PACKET;
603 resp.response_length += sizeof(resp.tso_caps);
604 }
605 }
Yishai Hadas31f69a82016-08-28 11:28:45 +0300606
607 if (field_avail(typeof(resp), rss_caps, uhw->outlen)) {
608 resp.rss_caps.rx_hash_function =
609 MLX5_RX_HASH_FUNC_TOEPLITZ;
610 resp.rss_caps.rx_hash_fields_mask =
611 MLX5_RX_HASH_SRC_IPV4 |
612 MLX5_RX_HASH_DST_IPV4 |
613 MLX5_RX_HASH_SRC_IPV6 |
614 MLX5_RX_HASH_DST_IPV6 |
615 MLX5_RX_HASH_SRC_PORT_TCP |
616 MLX5_RX_HASH_DST_PORT_TCP |
617 MLX5_RX_HASH_SRC_PORT_UDP |
618 MLX5_RX_HASH_DST_PORT_UDP;
619 resp.response_length += sizeof(resp.rss_caps);
620 }
621 } else {
622 if (field_avail(typeof(resp), tso_caps, uhw->outlen))
623 resp.response_length += sizeof(resp.tso_caps);
624 if (field_avail(typeof(resp), rss_caps, uhw->outlen))
625 resp.response_length += sizeof(resp.rss_caps);
Bodong Wang402ca532016-06-17 15:02:20 +0300626 }
627
Erez Shitritf0313962016-02-21 16:27:17 +0200628 if (MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) {
629 props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
630 props->device_cap_flags |= IB_DEVICE_UD_TSO;
631 }
632
Majd Dibbinycff5a0f2016-04-17 17:19:38 +0300633 if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
Noa Osheroviche8161332017-01-18 15:40:01 +0200634 MLX5_CAP_ETH(dev->mdev, scatter_fcs)) {
635 /* Legacy bit to support old userspace libraries */
Majd Dibbinycff5a0f2016-04-17 17:19:38 +0300636 props->device_cap_flags |= IB_DEVICE_RAW_SCATTER_FCS;
Noa Osheroviche8161332017-01-18 15:40:01 +0200637 props->raw_packet_caps |= IB_RAW_PACKET_CAP_SCATTER_FCS;
638 }
Majd Dibbinycff5a0f2016-04-17 17:19:38 +0300639
Maor Gottliebda6d6ba32016-06-04 15:15:28 +0300640 if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS))
641 props->device_cap_flags |= IB_DEVICE_MANAGED_FLOW_STEERING;
642
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300643 props->vendor_part_id = mdev->pdev->device;
644 props->hw_ver = mdev->pdev->revision;
Eli Cohene126ba92013-07-07 17:25:49 +0300645
646 props->max_mr_size = ~0ull;
Sagi Grimberge0238a62015-07-21 14:40:12 +0300647 props->page_size_cap = ~(min_page_size - 1);
Saeed Mahameed938fe832015-05-28 22:28:41 +0300648 props->max_qp = 1 << MLX5_CAP_GEN(mdev, log_max_qp);
649 props->max_qp_wr = 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
650 max_rq_sg = MLX5_CAP_GEN(mdev, max_wqe_sz_rq) /
651 sizeof(struct mlx5_wqe_data_seg);
Eli Cohen288c01b2016-10-27 16:36:45 +0300652 max_sq_desc = min_t(int, MLX5_CAP_GEN(mdev, max_wqe_sz_sq), 512);
653 max_sq_sg = (max_sq_desc - sizeof(struct mlx5_wqe_ctrl_seg) -
654 sizeof(struct mlx5_wqe_raddr_seg)) /
655 sizeof(struct mlx5_wqe_data_seg);
Eli Cohene126ba92013-07-07 17:25:49 +0300656 props->max_sge = min(max_rq_sg, max_sq_sg);
Sagi Grimberg986ef952016-03-31 19:03:25 +0300657 props->max_sge_rd = MLX5_MAX_SGE_RD;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300658 props->max_cq = 1 << MLX5_CAP_GEN(mdev, log_max_cq);
Leon Romanovsky9f177682016-01-14 08:11:40 +0200659 props->max_cqe = (1 << MLX5_CAP_GEN(mdev, log_max_cq_sz)) - 1;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300660 props->max_mr = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
661 props->max_pd = 1 << MLX5_CAP_GEN(mdev, log_max_pd);
662 props->max_qp_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_req_qp);
663 props->max_qp_init_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_res_qp);
664 props->max_srq = 1 << MLX5_CAP_GEN(mdev, log_max_srq);
665 props->max_srq_wr = (1 << MLX5_CAP_GEN(mdev, log_max_srq_sz)) - 1;
666 props->local_ca_ack_delay = MLX5_CAP_GEN(mdev, local_ca_ack_delay);
Eli Cohene126ba92013-07-07 17:25:49 +0300667 props->max_res_rd_atom = props->max_qp_rd_atom * props->max_qp;
Eli Cohene126ba92013-07-07 17:25:49 +0300668 props->max_srq_sge = max_rq_sg - 1;
Sagi Grimberg911f4332016-03-03 13:37:51 +0200669 props->max_fast_reg_page_list_len =
670 1 << MLX5_CAP_GEN(mdev, log_max_klm_list_size);
Eran Ben Elishada7525d2015-12-14 16:34:10 +0200671 get_atomic_caps(dev, props);
Eli Cohen81bea282013-09-11 16:35:30 +0300672 props->masked_atomic_cap = IB_ATOMIC_NONE;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300673 props->max_mcast_grp = 1 << MLX5_CAP_GEN(mdev, log_max_mcg);
674 props->max_mcast_qp_attach = MLX5_CAP_GEN(mdev, max_qp_mcg);
Eli Cohene126ba92013-07-07 17:25:49 +0300675 props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
676 props->max_mcast_grp;
677 props->max_map_per_fmr = INT_MAX; /* no limit in ConnectIB */
Maor Gottlieb86695a62016-10-27 16:36:38 +0300678 props->max_ah = INT_MAX;
Matan Barak7c60bcb2015-12-15 20:30:11 +0200679 props->hca_core_clock = MLX5_CAP_GEN(mdev, device_frequency_khz);
680 props->timestamp_mask = 0x7FFFFFFFFFFFFFFFULL;
Eli Cohene126ba92013-07-07 17:25:49 +0300681
Haggai Eran8cdd3122014-12-11 17:04:20 +0200682#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
Saeed Mahameed938fe832015-05-28 22:28:41 +0300683 if (MLX5_CAP_GEN(mdev, pg))
Haggai Eran8cdd3122014-12-11 17:04:20 +0200684 props->device_cap_flags |= IB_DEVICE_ON_DEMAND_PAGING;
685 props->odp_caps = dev->odp_caps;
686#endif
687
Leon Romanovsky051f2632015-12-20 12:16:11 +0200688 if (MLX5_CAP_GEN(mdev, cd))
689 props->device_cap_flags |= IB_DEVICE_CROSS_CHANNEL;
690
Eli Coheneff901d2016-03-11 22:58:42 +0200691 if (!mlx5_core_is_pf(mdev))
692 props->device_cap_flags |= IB_DEVICE_VIRTUAL_FUNCTION;
693
Yishai Hadas31f69a82016-08-28 11:28:45 +0300694 if (mlx5_ib_port_link_layer(ibdev, 1) ==
695 IB_LINK_LAYER_ETHERNET) {
696 props->rss_caps.max_rwq_indirection_tables =
697 1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt);
698 props->rss_caps.max_rwq_indirection_table_size =
699 1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt_size);
700 props->rss_caps.supported_qpts = 1 << IB_QPT_RAW_PACKET;
701 props->max_wq_type_rq =
702 1 << MLX5_CAP_GEN(dev->mdev, log_max_rq);
703 }
704
Bodong Wang7e43a2a2016-10-31 12:16:44 +0200705 if (field_avail(typeof(resp), cqe_comp_caps, uhw->outlen)) {
706 resp.cqe_comp_caps.max_num =
707 MLX5_CAP_GEN(dev->mdev, cqe_compression) ?
708 MLX5_CAP_GEN(dev->mdev, cqe_compression_max_num) : 0;
709 resp.cqe_comp_caps.supported_format =
710 MLX5_IB_CQE_RES_FORMAT_HASH |
711 MLX5_IB_CQE_RES_FORMAT_CSUM;
712 resp.response_length += sizeof(resp.cqe_comp_caps);
713 }
714
Bodong Wangd9491672016-12-01 13:43:13 +0200715 if (field_avail(typeof(resp), packet_pacing_caps, uhw->outlen)) {
716 if (MLX5_CAP_QOS(mdev, packet_pacing) &&
717 MLX5_CAP_GEN(mdev, qos)) {
718 resp.packet_pacing_caps.qp_rate_limit_max =
719 MLX5_CAP_QOS(mdev, packet_pacing_max_rate);
720 resp.packet_pacing_caps.qp_rate_limit_min =
721 MLX5_CAP_QOS(mdev, packet_pacing_min_rate);
722 resp.packet_pacing_caps.supported_qpts |=
723 1 << IB_QPT_RAW_PACKET;
724 }
725 resp.response_length += sizeof(resp.packet_pacing_caps);
726 }
727
Leon Romanovsky9f885202017-01-02 11:37:39 +0200728 if (field_avail(typeof(resp), mlx5_ib_support_multi_pkt_send_wqes,
729 uhw->outlen)) {
730 resp.mlx5_ib_support_multi_pkt_send_wqes =
731 MLX5_CAP_ETH(mdev, multi_pkt_send_wqe);
732 resp.response_length +=
733 sizeof(resp.mlx5_ib_support_multi_pkt_send_wqes);
734 }
735
736 if (field_avail(typeof(resp), reserved, uhw->outlen))
737 resp.response_length += sizeof(resp.reserved);
738
Bodong Wang402ca532016-06-17 15:02:20 +0300739 if (uhw->outlen) {
740 err = ib_copy_to_udata(uhw, &resp, resp.response_length);
741
742 if (err)
743 return err;
744 }
745
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300746 return 0;
747}
Eli Cohene126ba92013-07-07 17:25:49 +0300748
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300749enum mlx5_ib_width {
750 MLX5_IB_WIDTH_1X = 1 << 0,
751 MLX5_IB_WIDTH_2X = 1 << 1,
752 MLX5_IB_WIDTH_4X = 1 << 2,
753 MLX5_IB_WIDTH_8X = 1 << 3,
754 MLX5_IB_WIDTH_12X = 1 << 4
755};
756
757static int translate_active_width(struct ib_device *ibdev, u8 active_width,
758 u8 *ib_width)
759{
760 struct mlx5_ib_dev *dev = to_mdev(ibdev);
761 int err = 0;
762
763 if (active_width & MLX5_IB_WIDTH_1X) {
764 *ib_width = IB_WIDTH_1X;
765 } else if (active_width & MLX5_IB_WIDTH_2X) {
766 mlx5_ib_dbg(dev, "active_width %d is not supported by IB spec\n",
767 (int)active_width);
768 err = -EINVAL;
769 } else if (active_width & MLX5_IB_WIDTH_4X) {
770 *ib_width = IB_WIDTH_4X;
771 } else if (active_width & MLX5_IB_WIDTH_8X) {
772 *ib_width = IB_WIDTH_8X;
773 } else if (active_width & MLX5_IB_WIDTH_12X) {
774 *ib_width = IB_WIDTH_12X;
775 } else {
776 mlx5_ib_dbg(dev, "Invalid active_width %d\n",
777 (int)active_width);
778 err = -EINVAL;
779 }
780
781 return err;
782}
783
784static int mlx5_mtu_to_ib_mtu(int mtu)
785{
786 switch (mtu) {
787 case 256: return 1;
788 case 512: return 2;
789 case 1024: return 3;
790 case 2048: return 4;
791 case 4096: return 5;
792 default:
793 pr_warn("invalid mtu\n");
794 return -1;
795 }
796}
797
798enum ib_max_vl_num {
799 __IB_MAX_VL_0 = 1,
800 __IB_MAX_VL_0_1 = 2,
801 __IB_MAX_VL_0_3 = 3,
802 __IB_MAX_VL_0_7 = 4,
803 __IB_MAX_VL_0_14 = 5,
804};
805
806enum mlx5_vl_hw_cap {
807 MLX5_VL_HW_0 = 1,
808 MLX5_VL_HW_0_1 = 2,
809 MLX5_VL_HW_0_2 = 3,
810 MLX5_VL_HW_0_3 = 4,
811 MLX5_VL_HW_0_4 = 5,
812 MLX5_VL_HW_0_5 = 6,
813 MLX5_VL_HW_0_6 = 7,
814 MLX5_VL_HW_0_7 = 8,
815 MLX5_VL_HW_0_14 = 15
816};
817
818static int translate_max_vl_num(struct ib_device *ibdev, u8 vl_hw_cap,
819 u8 *max_vl_num)
820{
821 switch (vl_hw_cap) {
822 case MLX5_VL_HW_0:
823 *max_vl_num = __IB_MAX_VL_0;
824 break;
825 case MLX5_VL_HW_0_1:
826 *max_vl_num = __IB_MAX_VL_0_1;
827 break;
828 case MLX5_VL_HW_0_3:
829 *max_vl_num = __IB_MAX_VL_0_3;
830 break;
831 case MLX5_VL_HW_0_7:
832 *max_vl_num = __IB_MAX_VL_0_7;
833 break;
834 case MLX5_VL_HW_0_14:
835 *max_vl_num = __IB_MAX_VL_0_14;
836 break;
837
838 default:
839 return -EINVAL;
840 }
841
842 return 0;
843}
844
845static int mlx5_query_hca_port(struct ib_device *ibdev, u8 port,
846 struct ib_port_attr *props)
847{
848 struct mlx5_ib_dev *dev = to_mdev(ibdev);
849 struct mlx5_core_dev *mdev = dev->mdev;
850 struct mlx5_hca_vport_context *rep;
Saeed Mahameed046339e2016-04-22 00:33:03 +0300851 u16 max_mtu;
852 u16 oper_mtu;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300853 int err;
854 u8 ib_link_width_oper;
855 u8 vl_hw_cap;
856
857 rep = kzalloc(sizeof(*rep), GFP_KERNEL);
858 if (!rep) {
859 err = -ENOMEM;
860 goto out;
861 }
862
Or Gerlitzc4550c62017-01-24 13:02:39 +0200863 /* props being zeroed by the caller, avoid zeroing it here */
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300864
865 err = mlx5_query_hca_vport_context(mdev, 0, port, 0, rep);
866 if (err)
867 goto out;
868
869 props->lid = rep->lid;
870 props->lmc = rep->lmc;
871 props->sm_lid = rep->sm_lid;
872 props->sm_sl = rep->sm_sl;
873 props->state = rep->vport_state;
874 props->phys_state = rep->port_physical_state;
875 props->port_cap_flags = rep->cap_mask1;
876 props->gid_tbl_len = mlx5_get_gid_table_len(MLX5_CAP_GEN(mdev, gid_table_size));
877 props->max_msg_sz = 1 << MLX5_CAP_GEN(mdev, log_max_msg);
878 props->pkey_tbl_len = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, pkey_table_size));
879 props->bad_pkey_cntr = rep->pkey_violation_counter;
880 props->qkey_viol_cntr = rep->qkey_violation_counter;
881 props->subnet_timeout = rep->subnet_timeout;
882 props->init_type_reply = rep->init_type_reply;
Eli Coheneff901d2016-03-11 22:58:42 +0200883 props->grh_required = rep->grh_required;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300884
885 err = mlx5_query_port_link_width_oper(mdev, &ib_link_width_oper, port);
886 if (err)
887 goto out;
888
889 err = translate_active_width(ibdev, ib_link_width_oper,
890 &props->active_width);
891 if (err)
892 goto out;
Noa Osherovichd5beb7f2016-06-02 10:47:53 +0300893 err = mlx5_query_port_ib_proto_oper(mdev, &props->active_speed, port);
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300894 if (err)
895 goto out;
896
Saeed Mahameedfacc9692015-06-11 14:47:27 +0300897 mlx5_query_port_max_mtu(mdev, &max_mtu, port);
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300898
899 props->max_mtu = mlx5_mtu_to_ib_mtu(max_mtu);
900
Saeed Mahameedfacc9692015-06-11 14:47:27 +0300901 mlx5_query_port_oper_mtu(mdev, &oper_mtu, port);
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300902
903 props->active_mtu = mlx5_mtu_to_ib_mtu(oper_mtu);
904
905 err = mlx5_query_port_vl_hw_cap(mdev, &vl_hw_cap, port);
906 if (err)
907 goto out;
908
909 err = translate_max_vl_num(ibdev, vl_hw_cap,
910 &props->max_vl_num);
911out:
912 kfree(rep);
Eli Cohene126ba92013-07-07 17:25:49 +0300913 return err;
914}
915
916int mlx5_ib_query_port(struct ib_device *ibdev, u8 port,
917 struct ib_port_attr *props)
918{
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300919 switch (mlx5_get_vport_access_method(ibdev)) {
920 case MLX5_VPORT_ACCESS_METHOD_MAD:
921 return mlx5_query_mad_ifc_port(ibdev, port, props);
Eli Cohene126ba92013-07-07 17:25:49 +0300922
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300923 case MLX5_VPORT_ACCESS_METHOD_HCA:
924 return mlx5_query_hca_port(ibdev, port, props);
925
Achiad Shochat3f89a642015-12-23 18:47:21 +0200926 case MLX5_VPORT_ACCESS_METHOD_NIC:
927 return mlx5_query_port_roce(ibdev, port, props);
928
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300929 default:
Eli Cohene126ba92013-07-07 17:25:49 +0300930 return -EINVAL;
931 }
Eli Cohene126ba92013-07-07 17:25:49 +0300932}
933
934static int mlx5_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
935 union ib_gid *gid)
936{
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300937 struct mlx5_ib_dev *dev = to_mdev(ibdev);
938 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +0300939
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300940 switch (mlx5_get_vport_access_method(ibdev)) {
941 case MLX5_VPORT_ACCESS_METHOD_MAD:
942 return mlx5_query_mad_ifc_gids(ibdev, port, index, gid);
Eli Cohene126ba92013-07-07 17:25:49 +0300943
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300944 case MLX5_VPORT_ACCESS_METHOD_HCA:
945 return mlx5_query_hca_vport_gid(mdev, 0, port, 0, index, gid);
Eli Cohene126ba92013-07-07 17:25:49 +0300946
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300947 default:
948 return -EINVAL;
949 }
Eli Cohene126ba92013-07-07 17:25:49 +0300950
Eli Cohene126ba92013-07-07 17:25:49 +0300951}
952
953static int mlx5_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
954 u16 *pkey)
955{
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300956 struct mlx5_ib_dev *dev = to_mdev(ibdev);
957 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +0300958
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300959 switch (mlx5_get_vport_access_method(ibdev)) {
960 case MLX5_VPORT_ACCESS_METHOD_MAD:
961 return mlx5_query_mad_ifc_pkey(ibdev, port, index, pkey);
Eli Cohene126ba92013-07-07 17:25:49 +0300962
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300963 case MLX5_VPORT_ACCESS_METHOD_HCA:
964 case MLX5_VPORT_ACCESS_METHOD_NIC:
965 return mlx5_query_hca_vport_pkey(mdev, 0, port, 0, index,
966 pkey);
967 default:
968 return -EINVAL;
969 }
Eli Cohene126ba92013-07-07 17:25:49 +0300970}
971
Eli Cohene126ba92013-07-07 17:25:49 +0300972static int mlx5_ib_modify_device(struct ib_device *ibdev, int mask,
973 struct ib_device_modify *props)
974{
975 struct mlx5_ib_dev *dev = to_mdev(ibdev);
976 struct mlx5_reg_node_desc in;
977 struct mlx5_reg_node_desc out;
978 int err;
979
980 if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
981 return -EOPNOTSUPP;
982
983 if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
984 return 0;
985
986 /*
987 * If possible, pass node desc to FW, so it can generate
988 * a 144 trap. If cmd fails, just ignore.
989 */
Yuval Shaiabd99fde2016-08-25 10:57:07 -0700990 memcpy(&in, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
Jack Morgenstein9603b612014-07-28 23:30:22 +0300991 err = mlx5_core_access_reg(dev->mdev, &in, sizeof(in), &out,
Eli Cohene126ba92013-07-07 17:25:49 +0300992 sizeof(out), MLX5_REG_NODE_DESC, 0, 1);
993 if (err)
994 return err;
995
Yuval Shaiabd99fde2016-08-25 10:57:07 -0700996 memcpy(ibdev->node_desc, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
Eli Cohene126ba92013-07-07 17:25:49 +0300997
998 return err;
999}
1000
Eli Cohencdbe33d2017-02-14 07:25:38 +02001001static int set_port_caps_atomic(struct mlx5_ib_dev *dev, u8 port_num, u32 mask,
1002 u32 value)
1003{
1004 struct mlx5_hca_vport_context ctx = {};
1005 int err;
1006
1007 err = mlx5_query_hca_vport_context(dev->mdev, 0,
1008 port_num, 0, &ctx);
1009 if (err)
1010 return err;
1011
1012 if (~ctx.cap_mask1_perm & mask) {
1013 mlx5_ib_warn(dev, "trying to change bitmask 0x%X but change supported 0x%X\n",
1014 mask, ctx.cap_mask1_perm);
1015 return -EINVAL;
1016 }
1017
1018 ctx.cap_mask1 = value;
1019 ctx.cap_mask1_perm = mask;
1020 err = mlx5_core_modify_hca_vport_context(dev->mdev, 0,
1021 port_num, 0, &ctx);
1022
1023 return err;
1024}
1025
Eli Cohene126ba92013-07-07 17:25:49 +03001026static int mlx5_ib_modify_port(struct ib_device *ibdev, u8 port, int mask,
1027 struct ib_port_modify *props)
1028{
1029 struct mlx5_ib_dev *dev = to_mdev(ibdev);
1030 struct ib_port_attr attr;
1031 u32 tmp;
1032 int err;
Eli Cohencdbe33d2017-02-14 07:25:38 +02001033 u32 change_mask;
1034 u32 value;
1035 bool is_ib = (mlx5_ib_port_link_layer(ibdev, port) ==
1036 IB_LINK_LAYER_INFINIBAND);
1037
1038 if (MLX5_CAP_GEN(dev->mdev, ib_virt) && is_ib) {
1039 change_mask = props->clr_port_cap_mask | props->set_port_cap_mask;
1040 value = ~props->clr_port_cap_mask | props->set_port_cap_mask;
1041 return set_port_caps_atomic(dev, port, change_mask, value);
1042 }
Eli Cohene126ba92013-07-07 17:25:49 +03001043
1044 mutex_lock(&dev->cap_mask_mutex);
1045
Or Gerlitzc4550c62017-01-24 13:02:39 +02001046 err = ib_query_port(ibdev, port, &attr);
Eli Cohene126ba92013-07-07 17:25:49 +03001047 if (err)
1048 goto out;
1049
1050 tmp = (attr.port_cap_flags | props->set_port_cap_mask) &
1051 ~props->clr_port_cap_mask;
1052
Jack Morgenstein9603b612014-07-28 23:30:22 +03001053 err = mlx5_set_port_caps(dev->mdev, port, tmp);
Eli Cohene126ba92013-07-07 17:25:49 +03001054
1055out:
1056 mutex_unlock(&dev->cap_mask_mutex);
1057 return err;
1058}
1059
Eli Cohen30aa60b2017-01-03 23:55:27 +02001060static void print_lib_caps(struct mlx5_ib_dev *dev, u64 caps)
1061{
1062 mlx5_ib_dbg(dev, "MLX5_LIB_CAP_4K_UAR = %s\n",
1063 caps & MLX5_LIB_CAP_4K_UAR ? "y" : "n");
1064}
1065
Eli Cohenb037c292017-01-03 23:55:26 +02001066static int calc_total_bfregs(struct mlx5_ib_dev *dev, bool lib_uar_4k,
1067 struct mlx5_ib_alloc_ucontext_req_v2 *req,
1068 u32 *num_sys_pages)
1069{
1070 int uars_per_sys_page;
1071 int bfregs_per_sys_page;
1072 int ref_bfregs = req->total_num_bfregs;
1073
1074 if (req->total_num_bfregs == 0)
1075 return -EINVAL;
1076
1077 BUILD_BUG_ON(MLX5_MAX_BFREGS % MLX5_NON_FP_BFREGS_IN_PAGE);
1078 BUILD_BUG_ON(MLX5_MAX_BFREGS < MLX5_NON_FP_BFREGS_IN_PAGE);
1079
1080 if (req->total_num_bfregs > MLX5_MAX_BFREGS)
1081 return -ENOMEM;
1082
1083 uars_per_sys_page = get_uars_per_sys_page(dev, lib_uar_4k);
1084 bfregs_per_sys_page = uars_per_sys_page * MLX5_NON_FP_BFREGS_PER_UAR;
1085 req->total_num_bfregs = ALIGN(req->total_num_bfregs, bfregs_per_sys_page);
1086 *num_sys_pages = req->total_num_bfregs / bfregs_per_sys_page;
1087
1088 if (req->num_low_latency_bfregs > req->total_num_bfregs - 1)
1089 return -EINVAL;
1090
1091 mlx5_ib_dbg(dev, "uar_4k: fw support %s, lib support %s, user requested %d bfregs, alloated %d, using %d sys pages\n",
1092 MLX5_CAP_GEN(dev->mdev, uar_4k) ? "yes" : "no",
1093 lib_uar_4k ? "yes" : "no", ref_bfregs,
1094 req->total_num_bfregs, *num_sys_pages);
1095
1096 return 0;
1097}
1098
1099static int allocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
1100{
1101 struct mlx5_bfreg_info *bfregi;
1102 int err;
1103 int i;
1104
1105 bfregi = &context->bfregi;
1106 for (i = 0; i < bfregi->num_sys_pages; i++) {
1107 err = mlx5_cmd_alloc_uar(dev->mdev, &bfregi->sys_pages[i]);
1108 if (err)
1109 goto error;
1110
1111 mlx5_ib_dbg(dev, "allocated uar %d\n", bfregi->sys_pages[i]);
1112 }
1113 return 0;
1114
1115error:
1116 for (--i; i >= 0; i--)
1117 if (mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]))
1118 mlx5_ib_warn(dev, "failed to free uar %d\n", i);
1119
1120 return err;
1121}
1122
1123static int deallocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
1124{
1125 struct mlx5_bfreg_info *bfregi;
1126 int err;
1127 int i;
1128
1129 bfregi = &context->bfregi;
1130 for (i = 0; i < bfregi->num_sys_pages; i++) {
1131 err = mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]);
1132 if (err) {
1133 mlx5_ib_warn(dev, "failed to free uar %d\n", i);
1134 return err;
1135 }
1136 }
1137 return 0;
1138}
1139
Eli Cohene126ba92013-07-07 17:25:49 +03001140static struct ib_ucontext *mlx5_ib_alloc_ucontext(struct ib_device *ibdev,
1141 struct ib_udata *udata)
1142{
1143 struct mlx5_ib_dev *dev = to_mdev(ibdev);
Matan Barakb368d7c2015-12-15 20:30:12 +02001144 struct mlx5_ib_alloc_ucontext_req_v2 req = {};
1145 struct mlx5_ib_alloc_ucontext_resp resp = {};
Eli Cohene126ba92013-07-07 17:25:49 +03001146 struct mlx5_ib_ucontext *context;
Eli Cohen2f5ff262017-01-03 23:55:21 +02001147 struct mlx5_bfreg_info *bfregi;
Eli Cohen78c0f982014-01-30 13:49:48 +02001148 int ver;
Eli Cohene126ba92013-07-07 17:25:49 +03001149 int err;
Jack Morgensteinf241e742014-07-28 23:30:23 +03001150 size_t reqlen;
Majd Dibbinya168a41c2016-01-28 17:51:47 +02001151 size_t min_req_v2 = offsetof(struct mlx5_ib_alloc_ucontext_req_v2,
1152 max_cqe_version);
Eli Cohenb037c292017-01-03 23:55:26 +02001153 bool lib_uar_4k;
Eli Cohene126ba92013-07-07 17:25:49 +03001154
1155 if (!dev->ib_active)
1156 return ERR_PTR(-EAGAIN);
1157
Haggai Abramovskydfbee852016-01-14 19:12:56 +02001158 if (udata->inlen < sizeof(struct ib_uverbs_cmd_hdr))
1159 return ERR_PTR(-EINVAL);
1160
Eli Cohen78c0f982014-01-30 13:49:48 +02001161 reqlen = udata->inlen - sizeof(struct ib_uverbs_cmd_hdr);
1162 if (reqlen == sizeof(struct mlx5_ib_alloc_ucontext_req))
1163 ver = 0;
Majd Dibbinya168a41c2016-01-28 17:51:47 +02001164 else if (reqlen >= min_req_v2)
Eli Cohen78c0f982014-01-30 13:49:48 +02001165 ver = 2;
1166 else
1167 return ERR_PTR(-EINVAL);
1168
Matan Barakb368d7c2015-12-15 20:30:12 +02001169 err = ib_copy_from_udata(&req, udata, min(reqlen, sizeof(req)));
Eli Cohene126ba92013-07-07 17:25:49 +03001170 if (err)
1171 return ERR_PTR(err);
1172
Matan Barakb368d7c2015-12-15 20:30:12 +02001173 if (req.flags)
Eli Cohen78c0f982014-01-30 13:49:48 +02001174 return ERR_PTR(-EINVAL);
1175
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001176 if (req.comp_mask || req.reserved0 || req.reserved1 || req.reserved2)
Matan Barakb368d7c2015-12-15 20:30:12 +02001177 return ERR_PTR(-EOPNOTSUPP);
1178
Eli Cohen2f5ff262017-01-03 23:55:21 +02001179 req.total_num_bfregs = ALIGN(req.total_num_bfregs,
1180 MLX5_NON_FP_BFREGS_PER_UAR);
1181 if (req.num_low_latency_bfregs > req.total_num_bfregs - 1)
Eli Cohene126ba92013-07-07 17:25:49 +03001182 return ERR_PTR(-EINVAL);
1183
Saeed Mahameed938fe832015-05-28 22:28:41 +03001184 resp.qp_tab_size = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp);
Noa Osherovich2cc6ad52016-06-04 15:15:33 +03001185 if (mlx5_core_is_pf(dev->mdev) && MLX5_CAP_GEN(dev->mdev, bf))
1186 resp.bf_reg_size = 1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size);
Daniel Jurgensb47bd6e2016-10-25 18:36:24 +03001187 resp.cache_line_size = cache_line_size();
Saeed Mahameed938fe832015-05-28 22:28:41 +03001188 resp.max_sq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq);
1189 resp.max_rq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq);
1190 resp.max_send_wqebb = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1191 resp.max_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1192 resp.max_srq_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz);
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001193 resp.cqe_version = min_t(__u8,
1194 (__u8)MLX5_CAP_GEN(dev->mdev, cqe_version),
1195 req.max_cqe_version);
Eli Cohen30aa60b2017-01-03 23:55:27 +02001196 resp.log_uar_size = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
1197 MLX5_ADAPTER_PAGE_SHIFT : PAGE_SHIFT;
1198 resp.num_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
1199 MLX5_CAP_GEN(dev->mdev, num_of_uars_per_page) : 1;
Matan Barakb368d7c2015-12-15 20:30:12 +02001200 resp.response_length = min(offsetof(typeof(resp), response_length) +
1201 sizeof(resp.response_length), udata->outlen);
Eli Cohene126ba92013-07-07 17:25:49 +03001202
1203 context = kzalloc(sizeof(*context), GFP_KERNEL);
1204 if (!context)
1205 return ERR_PTR(-ENOMEM);
1206
Eli Cohen30aa60b2017-01-03 23:55:27 +02001207 lib_uar_4k = req.lib_caps & MLX5_LIB_CAP_4K_UAR;
Eli Cohen2f5ff262017-01-03 23:55:21 +02001208 bfregi = &context->bfregi;
Eli Cohenb037c292017-01-03 23:55:26 +02001209
1210 /* updates req->total_num_bfregs */
1211 err = calc_total_bfregs(dev, lib_uar_4k, &req, &bfregi->num_sys_pages);
1212 if (err)
1213 goto out_ctx;
1214
Eli Cohen2f5ff262017-01-03 23:55:21 +02001215 mutex_init(&bfregi->lock);
Eli Cohenb037c292017-01-03 23:55:26 +02001216 bfregi->lib_uar_4k = lib_uar_4k;
1217 bfregi->count = kcalloc(req.total_num_bfregs, sizeof(*bfregi->count),
1218 GFP_KERNEL);
1219 if (!bfregi->count) {
Eli Cohene126ba92013-07-07 17:25:49 +03001220 err = -ENOMEM;
1221 goto out_ctx;
1222 }
1223
Eli Cohenb037c292017-01-03 23:55:26 +02001224 bfregi->sys_pages = kcalloc(bfregi->num_sys_pages,
1225 sizeof(*bfregi->sys_pages),
1226 GFP_KERNEL);
1227 if (!bfregi->sys_pages) {
Eli Cohene126ba92013-07-07 17:25:49 +03001228 err = -ENOMEM;
Eli Cohenb037c292017-01-03 23:55:26 +02001229 goto out_count;
Eli Cohene126ba92013-07-07 17:25:49 +03001230 }
1231
Eli Cohenb037c292017-01-03 23:55:26 +02001232 err = allocate_uars(dev, context);
1233 if (err)
1234 goto out_sys_pages;
Eli Cohene126ba92013-07-07 17:25:49 +03001235
Haggai Eranb4cfe442014-12-11 17:04:26 +02001236#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
1237 context->ibucontext.invalidate_range = &mlx5_ib_invalidate_range;
1238#endif
1239
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +02001240 context->upd_xlt_page = __get_free_page(GFP_KERNEL);
1241 if (!context->upd_xlt_page) {
1242 err = -ENOMEM;
1243 goto out_uars;
1244 }
1245 mutex_init(&context->upd_xlt_page_mutex);
1246
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001247 if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain)) {
1248 err = mlx5_core_alloc_transport_domain(dev->mdev,
1249 &context->tdn);
1250 if (err)
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +02001251 goto out_page;
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001252 }
1253
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001254 INIT_LIST_HEAD(&context->vma_private_list);
Eli Cohene126ba92013-07-07 17:25:49 +03001255 INIT_LIST_HEAD(&context->db_page_list);
1256 mutex_init(&context->db_page_mutex);
1257
Eli Cohen2f5ff262017-01-03 23:55:21 +02001258 resp.tot_bfregs = req.total_num_bfregs;
Saeed Mahameed938fe832015-05-28 22:28:41 +03001259 resp.num_ports = MLX5_CAP_GEN(dev->mdev, num_ports);
Matan Barakb368d7c2015-12-15 20:30:12 +02001260
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001261 if (field_avail(typeof(resp), cqe_version, udata->outlen))
1262 resp.response_length += sizeof(resp.cqe_version);
Matan Barakb368d7c2015-12-15 20:30:12 +02001263
Bodong Wang402ca532016-06-17 15:02:20 +03001264 if (field_avail(typeof(resp), cmds_supp_uhw, udata->outlen)) {
Moni Shoua6ad279c52016-11-23 08:23:23 +02001265 resp.cmds_supp_uhw |= MLX5_USER_CMDS_SUPP_UHW_QUERY_DEVICE |
1266 MLX5_USER_CMDS_SUPP_UHW_CREATE_AH;
Bodong Wang402ca532016-06-17 15:02:20 +03001267 resp.response_length += sizeof(resp.cmds_supp_uhw);
1268 }
1269
Or Gerlitz78984892016-11-30 20:33:33 +02001270 if (field_avail(typeof(resp), eth_min_inline, udata->outlen)) {
1271 if (mlx5_ib_port_link_layer(ibdev, 1) == IB_LINK_LAYER_ETHERNET) {
1272 mlx5_query_min_inline(dev->mdev, &resp.eth_min_inline);
1273 resp.eth_min_inline++;
1274 }
1275 resp.response_length += sizeof(resp.eth_min_inline);
1276 }
1277
Noa Osherovichbc5c6ee2016-06-04 15:15:31 +03001278 /*
1279 * We don't want to expose information from the PCI bar that is located
1280 * after 4096 bytes, so if the arch only supports larger pages, let's
1281 * pretend we don't support reading the HCA's core clock. This is also
1282 * forced by mmap function.
1283 */
Eli Cohende8d6e02017-01-03 23:55:19 +02001284 if (field_avail(typeof(resp), hca_core_clock_offset, udata->outlen)) {
1285 if (PAGE_SIZE <= 4096) {
1286 resp.comp_mask |=
1287 MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_CORE_CLOCK_OFFSET;
1288 resp.hca_core_clock_offset =
1289 offsetof(struct mlx5_init_seg, internal_timer_h) % PAGE_SIZE;
1290 }
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001291 resp.response_length += sizeof(resp.hca_core_clock_offset) +
Bodong Wang402ca532016-06-17 15:02:20 +03001292 sizeof(resp.reserved2);
Matan Barakb368d7c2015-12-15 20:30:12 +02001293 }
1294
Eli Cohen30aa60b2017-01-03 23:55:27 +02001295 if (field_avail(typeof(resp), log_uar_size, udata->outlen))
1296 resp.response_length += sizeof(resp.log_uar_size);
1297
1298 if (field_avail(typeof(resp), num_uars_per_page, udata->outlen))
1299 resp.response_length += sizeof(resp.num_uars_per_page);
1300
Matan Barakb368d7c2015-12-15 20:30:12 +02001301 err = ib_copy_to_udata(udata, &resp, resp.response_length);
Eli Cohene126ba92013-07-07 17:25:49 +03001302 if (err)
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001303 goto out_td;
Eli Cohene126ba92013-07-07 17:25:49 +03001304
Eli Cohen2f5ff262017-01-03 23:55:21 +02001305 bfregi->ver = ver;
1306 bfregi->num_low_latency_bfregs = req.num_low_latency_bfregs;
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001307 context->cqe_version = resp.cqe_version;
Eli Cohen30aa60b2017-01-03 23:55:27 +02001308 context->lib_caps = req.lib_caps;
1309 print_lib_caps(dev, context->lib_caps);
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001310
Eli Cohene126ba92013-07-07 17:25:49 +03001311 return &context->ibucontext;
1312
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001313out_td:
1314 if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1315 mlx5_core_dealloc_transport_domain(dev->mdev, context->tdn);
1316
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +02001317out_page:
1318 free_page(context->upd_xlt_page);
1319
Eli Cohene126ba92013-07-07 17:25:49 +03001320out_uars:
Eli Cohenb037c292017-01-03 23:55:26 +02001321 deallocate_uars(dev, context);
1322
1323out_sys_pages:
1324 kfree(bfregi->sys_pages);
1325
Eli Cohene126ba92013-07-07 17:25:49 +03001326out_count:
Eli Cohen2f5ff262017-01-03 23:55:21 +02001327 kfree(bfregi->count);
Eli Cohene126ba92013-07-07 17:25:49 +03001328
Eli Cohene126ba92013-07-07 17:25:49 +03001329out_ctx:
1330 kfree(context);
Eli Cohenb037c292017-01-03 23:55:26 +02001331
Eli Cohene126ba92013-07-07 17:25:49 +03001332 return ERR_PTR(err);
1333}
1334
1335static int mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
1336{
1337 struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1338 struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
Eli Cohenb037c292017-01-03 23:55:26 +02001339 struct mlx5_bfreg_info *bfregi;
Eli Cohene126ba92013-07-07 17:25:49 +03001340
Eli Cohenb037c292017-01-03 23:55:26 +02001341 bfregi = &context->bfregi;
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001342 if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1343 mlx5_core_dealloc_transport_domain(dev->mdev, context->tdn);
1344
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +02001345 free_page(context->upd_xlt_page);
Eli Cohenb037c292017-01-03 23:55:26 +02001346 deallocate_uars(dev, context);
1347 kfree(bfregi->sys_pages);
Eli Cohen2f5ff262017-01-03 23:55:21 +02001348 kfree(bfregi->count);
Eli Cohene126ba92013-07-07 17:25:49 +03001349 kfree(context);
1350
1351 return 0;
1352}
1353
Eli Cohenb037c292017-01-03 23:55:26 +02001354static phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev,
1355 struct mlx5_bfreg_info *bfregi,
1356 int idx)
Eli Cohene126ba92013-07-07 17:25:49 +03001357{
Eli Cohenb037c292017-01-03 23:55:26 +02001358 int fw_uars_per_page;
1359
1360 fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ? MLX5_UARS_IN_PAGE : 1;
1361
1362 return (pci_resource_start(dev->mdev->pdev, 0) >> PAGE_SHIFT) +
1363 bfregi->sys_pages[idx] / fw_uars_per_page;
Eli Cohene126ba92013-07-07 17:25:49 +03001364}
1365
1366static int get_command(unsigned long offset)
1367{
1368 return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK;
1369}
1370
1371static int get_arg(unsigned long offset)
1372{
1373 return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1);
1374}
1375
1376static int get_index(unsigned long offset)
1377{
1378 return get_arg(offset);
1379}
1380
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001381static void mlx5_ib_vma_open(struct vm_area_struct *area)
1382{
1383 /* vma_open is called when a new VMA is created on top of our VMA. This
1384 * is done through either mremap flow or split_vma (usually due to
1385 * mlock, madvise, munmap, etc.) We do not support a clone of the VMA,
1386 * as this VMA is strongly hardware related. Therefore we set the
1387 * vm_ops of the newly created/cloned VMA to NULL, to prevent it from
1388 * calling us again and trying to do incorrect actions. We assume that
1389 * the original VMA size is exactly a single page, and therefore all
1390 * "splitting" operation will not happen to it.
1391 */
1392 area->vm_ops = NULL;
1393}
1394
1395static void mlx5_ib_vma_close(struct vm_area_struct *area)
1396{
1397 struct mlx5_ib_vma_private_data *mlx5_ib_vma_priv_data;
1398
1399 /* It's guaranteed that all VMAs opened on a FD are closed before the
1400 * file itself is closed, therefore no sync is needed with the regular
1401 * closing flow. (e.g. mlx5 ib_dealloc_ucontext)
1402 * However need a sync with accessing the vma as part of
1403 * mlx5_ib_disassociate_ucontext.
1404 * The close operation is usually called under mm->mmap_sem except when
1405 * process is exiting.
1406 * The exiting case is handled explicitly as part of
1407 * mlx5_ib_disassociate_ucontext.
1408 */
1409 mlx5_ib_vma_priv_data = (struct mlx5_ib_vma_private_data *)area->vm_private_data;
1410
1411 /* setting the vma context pointer to null in the mlx5_ib driver's
1412 * private data, to protect a race condition in
1413 * mlx5_ib_disassociate_ucontext().
1414 */
1415 mlx5_ib_vma_priv_data->vma = NULL;
1416 list_del(&mlx5_ib_vma_priv_data->list);
1417 kfree(mlx5_ib_vma_priv_data);
1418}
1419
1420static const struct vm_operations_struct mlx5_ib_vm_ops = {
1421 .open = mlx5_ib_vma_open,
1422 .close = mlx5_ib_vma_close
1423};
1424
1425static int mlx5_ib_set_vma_data(struct vm_area_struct *vma,
1426 struct mlx5_ib_ucontext *ctx)
1427{
1428 struct mlx5_ib_vma_private_data *vma_prv;
1429 struct list_head *vma_head = &ctx->vma_private_list;
1430
1431 vma_prv = kzalloc(sizeof(*vma_prv), GFP_KERNEL);
1432 if (!vma_prv)
1433 return -ENOMEM;
1434
1435 vma_prv->vma = vma;
1436 vma->vm_private_data = vma_prv;
1437 vma->vm_ops = &mlx5_ib_vm_ops;
1438
1439 list_add(&vma_prv->list, vma_head);
1440
1441 return 0;
1442}
1443
1444static void mlx5_ib_disassociate_ucontext(struct ib_ucontext *ibcontext)
1445{
1446 int ret;
1447 struct vm_area_struct *vma;
1448 struct mlx5_ib_vma_private_data *vma_private, *n;
1449 struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1450 struct task_struct *owning_process = NULL;
1451 struct mm_struct *owning_mm = NULL;
1452
1453 owning_process = get_pid_task(ibcontext->tgid, PIDTYPE_PID);
1454 if (!owning_process)
1455 return;
1456
1457 owning_mm = get_task_mm(owning_process);
1458 if (!owning_mm) {
1459 pr_info("no mm, disassociate ucontext is pending task termination\n");
1460 while (1) {
1461 put_task_struct(owning_process);
1462 usleep_range(1000, 2000);
1463 owning_process = get_pid_task(ibcontext->tgid,
1464 PIDTYPE_PID);
1465 if (!owning_process ||
1466 owning_process->state == TASK_DEAD) {
1467 pr_info("disassociate ucontext done, task was terminated\n");
1468 /* in case task was dead need to release the
1469 * task struct.
1470 */
1471 if (owning_process)
1472 put_task_struct(owning_process);
1473 return;
1474 }
1475 }
1476 }
1477
1478 /* need to protect from a race on closing the vma as part of
1479 * mlx5_ib_vma_close.
1480 */
Maor Gottliebecc7d832017-03-29 06:03:02 +03001481 down_write(&owning_mm->mmap_sem);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001482 list_for_each_entry_safe(vma_private, n, &context->vma_private_list,
1483 list) {
1484 vma = vma_private->vma;
1485 ret = zap_vma_ptes(vma, vma->vm_start,
1486 PAGE_SIZE);
1487 WARN_ONCE(ret, "%s: zap_vma_ptes failed", __func__);
1488 /* context going to be destroyed, should
1489 * not access ops any more.
1490 */
Maor Gottlieb13776612017-03-29 06:03:03 +03001491 vma->vm_flags &= ~(VM_SHARED | VM_MAYSHARE);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001492 vma->vm_ops = NULL;
1493 list_del(&vma_private->list);
1494 kfree(vma_private);
1495 }
Maor Gottliebecc7d832017-03-29 06:03:02 +03001496 up_write(&owning_mm->mmap_sem);
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001497 mmput(owning_mm);
1498 put_task_struct(owning_process);
1499}
1500
Guy Levi37aa5c32016-04-27 16:49:50 +03001501static inline char *mmap_cmd2str(enum mlx5_ib_mmap_cmd cmd)
1502{
1503 switch (cmd) {
1504 case MLX5_IB_MMAP_WC_PAGE:
1505 return "WC";
1506 case MLX5_IB_MMAP_REGULAR_PAGE:
1507 return "best effort WC";
1508 case MLX5_IB_MMAP_NC_PAGE:
1509 return "NC";
1510 default:
1511 return NULL;
1512 }
1513}
1514
1515static int uar_mmap(struct mlx5_ib_dev *dev, enum mlx5_ib_mmap_cmd cmd,
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001516 struct vm_area_struct *vma,
1517 struct mlx5_ib_ucontext *context)
Guy Levi37aa5c32016-04-27 16:49:50 +03001518{
Eli Cohen2f5ff262017-01-03 23:55:21 +02001519 struct mlx5_bfreg_info *bfregi = &context->bfregi;
Guy Levi37aa5c32016-04-27 16:49:50 +03001520 int err;
1521 unsigned long idx;
1522 phys_addr_t pfn, pa;
1523 pgprot_t prot;
Eli Cohenb037c292017-01-03 23:55:26 +02001524 int uars_per_page;
1525
1526 if (vma->vm_end - vma->vm_start != PAGE_SIZE)
1527 return -EINVAL;
1528
1529 uars_per_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k);
1530 idx = get_index(vma->vm_pgoff);
1531 if (idx % uars_per_page ||
1532 idx * uars_per_page >= bfregi->num_sys_pages) {
1533 mlx5_ib_warn(dev, "invalid uar index %lu\n", idx);
1534 return -EINVAL;
1535 }
Guy Levi37aa5c32016-04-27 16:49:50 +03001536
1537 switch (cmd) {
1538 case MLX5_IB_MMAP_WC_PAGE:
1539/* Some architectures don't support WC memory */
1540#if defined(CONFIG_X86)
1541 if (!pat_enabled())
1542 return -EPERM;
1543#elif !(defined(CONFIG_PPC) || (defined(CONFIG_ARM) && defined(CONFIG_MMU)))
1544 return -EPERM;
1545#endif
1546 /* fall through */
1547 case MLX5_IB_MMAP_REGULAR_PAGE:
1548 /* For MLX5_IB_MMAP_REGULAR_PAGE do the best effort to get WC */
1549 prot = pgprot_writecombine(vma->vm_page_prot);
1550 break;
1551 case MLX5_IB_MMAP_NC_PAGE:
1552 prot = pgprot_noncached(vma->vm_page_prot);
1553 break;
1554 default:
1555 return -EINVAL;
1556 }
1557
Eli Cohenb037c292017-01-03 23:55:26 +02001558 pfn = uar_index2pfn(dev, bfregi, idx);
Guy Levi37aa5c32016-04-27 16:49:50 +03001559 mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn %pa\n", idx, &pfn);
1560
1561 vma->vm_page_prot = prot;
1562 err = io_remap_pfn_range(vma, vma->vm_start, pfn,
1563 PAGE_SIZE, vma->vm_page_prot);
1564 if (err) {
1565 mlx5_ib_err(dev, "io_remap_pfn_range failed with error=%d, vm_start=0x%lx, pfn=%pa, mmap_cmd=%s\n",
1566 err, vma->vm_start, &pfn, mmap_cmd2str(cmd));
1567 return -EAGAIN;
1568 }
1569
1570 pa = pfn << PAGE_SHIFT;
1571 mlx5_ib_dbg(dev, "mapped %s at 0x%lx, PA %pa\n", mmap_cmd2str(cmd),
1572 vma->vm_start, &pa);
1573
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001574 return mlx5_ib_set_vma_data(vma, context);
Guy Levi37aa5c32016-04-27 16:49:50 +03001575}
1576
Eli Cohene126ba92013-07-07 17:25:49 +03001577static int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma)
1578{
1579 struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1580 struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
Eli Cohene126ba92013-07-07 17:25:49 +03001581 unsigned long command;
Eli Cohene126ba92013-07-07 17:25:49 +03001582 phys_addr_t pfn;
1583
1584 command = get_command(vma->vm_pgoff);
1585 switch (command) {
Guy Levi37aa5c32016-04-27 16:49:50 +03001586 case MLX5_IB_MMAP_WC_PAGE:
1587 case MLX5_IB_MMAP_NC_PAGE:
Eli Cohene126ba92013-07-07 17:25:49 +03001588 case MLX5_IB_MMAP_REGULAR_PAGE:
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001589 return uar_mmap(dev, command, vma, context);
Eli Cohene126ba92013-07-07 17:25:49 +03001590
1591 case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES:
1592 return -ENOSYS;
1593
Matan Barakd69e3bc2015-12-15 20:30:13 +02001594 case MLX5_IB_MMAP_CORE_CLOCK:
Matan Barakd69e3bc2015-12-15 20:30:13 +02001595 if (vma->vm_end - vma->vm_start != PAGE_SIZE)
1596 return -EINVAL;
1597
Matan Barak6cbac1e2016-04-14 16:52:10 +03001598 if (vma->vm_flags & VM_WRITE)
Matan Barakd69e3bc2015-12-15 20:30:13 +02001599 return -EPERM;
1600
1601 /* Don't expose to user-space information it shouldn't have */
1602 if (PAGE_SIZE > 4096)
1603 return -EOPNOTSUPP;
1604
1605 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1606 pfn = (dev->mdev->iseg_base +
1607 offsetof(struct mlx5_init_seg, internal_timer_h)) >>
1608 PAGE_SHIFT;
1609 if (io_remap_pfn_range(vma, vma->vm_start, pfn,
1610 PAGE_SIZE, vma->vm_page_prot))
1611 return -EAGAIN;
1612
1613 mlx5_ib_dbg(dev, "mapped internal timer at 0x%lx, PA 0x%llx\n",
1614 vma->vm_start,
1615 (unsigned long long)pfn << PAGE_SHIFT);
1616 break;
Matan Barakd69e3bc2015-12-15 20:30:13 +02001617
Eli Cohene126ba92013-07-07 17:25:49 +03001618 default:
1619 return -EINVAL;
1620 }
1621
1622 return 0;
1623}
1624
Eli Cohene126ba92013-07-07 17:25:49 +03001625static struct ib_pd *mlx5_ib_alloc_pd(struct ib_device *ibdev,
1626 struct ib_ucontext *context,
1627 struct ib_udata *udata)
1628{
1629 struct mlx5_ib_alloc_pd_resp resp;
1630 struct mlx5_ib_pd *pd;
1631 int err;
1632
1633 pd = kmalloc(sizeof(*pd), GFP_KERNEL);
1634 if (!pd)
1635 return ERR_PTR(-ENOMEM);
1636
Jack Morgenstein9603b612014-07-28 23:30:22 +03001637 err = mlx5_core_alloc_pd(to_mdev(ibdev)->mdev, &pd->pdn);
Eli Cohene126ba92013-07-07 17:25:49 +03001638 if (err) {
1639 kfree(pd);
1640 return ERR_PTR(err);
1641 }
1642
1643 if (context) {
1644 resp.pdn = pd->pdn;
1645 if (ib_copy_to_udata(udata, &resp, sizeof(resp))) {
Jack Morgenstein9603b612014-07-28 23:30:22 +03001646 mlx5_core_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn);
Eli Cohene126ba92013-07-07 17:25:49 +03001647 kfree(pd);
1648 return ERR_PTR(-EFAULT);
1649 }
Eli Cohene126ba92013-07-07 17:25:49 +03001650 }
1651
1652 return &pd->ibpd;
1653}
1654
1655static int mlx5_ib_dealloc_pd(struct ib_pd *pd)
1656{
1657 struct mlx5_ib_dev *mdev = to_mdev(pd->device);
1658 struct mlx5_ib_pd *mpd = to_mpd(pd);
1659
Jack Morgenstein9603b612014-07-28 23:30:22 +03001660 mlx5_core_dealloc_pd(mdev->mdev, mpd->pdn);
Eli Cohene126ba92013-07-07 17:25:49 +03001661 kfree(mpd);
1662
1663 return 0;
1664}
1665
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03001666enum {
1667 MATCH_CRITERIA_ENABLE_OUTER_BIT,
1668 MATCH_CRITERIA_ENABLE_MISC_BIT,
1669 MATCH_CRITERIA_ENABLE_INNER_BIT
1670};
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001671
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03001672#define HEADER_IS_ZERO(match_criteria, headers) \
1673 !(memchr_inv(MLX5_ADDR_OF(fte_match_param, match_criteria, headers), \
1674 0, MLX5_FLD_SZ_BYTES(fte_match_param, headers))) \
1675
1676static u8 get_match_criteria_enable(u32 *match_criteria)
1677{
1678 u8 match_criteria_enable;
1679
1680 match_criteria_enable =
1681 (!HEADER_IS_ZERO(match_criteria, outer_headers)) <<
1682 MATCH_CRITERIA_ENABLE_OUTER_BIT;
1683 match_criteria_enable |=
1684 (!HEADER_IS_ZERO(match_criteria, misc_parameters)) <<
1685 MATCH_CRITERIA_ENABLE_MISC_BIT;
1686 match_criteria_enable |=
1687 (!HEADER_IS_ZERO(match_criteria, inner_headers)) <<
1688 MATCH_CRITERIA_ENABLE_INNER_BIT;
1689
1690 return match_criteria_enable;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001691}
1692
Maor Gottliebca0d4752016-08-30 16:58:35 +03001693static void set_proto(void *outer_c, void *outer_v, u8 mask, u8 val)
1694{
1695 MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_protocol, mask);
1696 MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_protocol, val);
1697}
1698
Moses Reuben2d1e6972016-11-14 19:04:52 +02001699static void set_flow_label(void *misc_c, void *misc_v, u8 mask, u8 val,
1700 bool inner)
1701{
1702 if (inner) {
1703 MLX5_SET(fte_match_set_misc,
1704 misc_c, inner_ipv6_flow_label, mask);
1705 MLX5_SET(fte_match_set_misc,
1706 misc_v, inner_ipv6_flow_label, val);
1707 } else {
1708 MLX5_SET(fte_match_set_misc,
1709 misc_c, outer_ipv6_flow_label, mask);
1710 MLX5_SET(fte_match_set_misc,
1711 misc_v, outer_ipv6_flow_label, val);
1712 }
1713}
1714
Maor Gottliebca0d4752016-08-30 16:58:35 +03001715static void set_tos(void *outer_c, void *outer_v, u8 mask, u8 val)
1716{
1717 MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_ecn, mask);
1718 MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_ecn, val);
1719 MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_dscp, mask >> 2);
1720 MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_dscp, val >> 2);
1721}
1722
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03001723#define LAST_ETH_FIELD vlan_tag
1724#define LAST_IB_FIELD sl
Maor Gottliebca0d4752016-08-30 16:58:35 +03001725#define LAST_IPV4_FIELD tos
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03001726#define LAST_IPV6_FIELD traffic_class
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03001727#define LAST_TCP_UDP_FIELD src_port
Moses Reubenffb30d82016-11-14 19:04:50 +02001728#define LAST_TUNNEL_FIELD tunnel_id
Moses Reuben2ac693f2017-01-18 14:59:50 +02001729#define LAST_FLOW_TAG_FIELD tag_id
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03001730
1731/* Field is the last supported field */
1732#define FIELDS_NOT_SUPPORTED(filter, field)\
1733 memchr_inv((void *)&filter.field +\
1734 sizeof(filter.field), 0,\
1735 sizeof(filter) -\
1736 offsetof(typeof(filter), field) -\
1737 sizeof(filter.field))
1738
Ariel Levkovich19cc7522017-04-03 13:11:03 +03001739#define IPV4_VERSION 4
1740#define IPV6_VERSION 6
1741static int parse_flow_attr(struct mlx5_core_dev *mdev, u32 *match_c,
1742 u32 *match_v, const union ib_flow_spec *ib_spec,
1743 u32 *tag_id)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001744{
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03001745 void *misc_params_c = MLX5_ADDR_OF(fte_match_param, match_c,
1746 misc_parameters);
1747 void *misc_params_v = MLX5_ADDR_OF(fte_match_param, match_v,
1748 misc_parameters);
Moses Reuben2d1e6972016-11-14 19:04:52 +02001749 void *headers_c;
1750 void *headers_v;
Ariel Levkovich19cc7522017-04-03 13:11:03 +03001751 int match_ipv;
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03001752
Moses Reuben2d1e6972016-11-14 19:04:52 +02001753 if (ib_spec->type & IB_FLOW_SPEC_INNER) {
1754 headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
1755 inner_headers);
1756 headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
1757 inner_headers);
Ariel Levkovich19cc7522017-04-03 13:11:03 +03001758 match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
1759 ft_field_support.inner_ip_version);
Moses Reuben2d1e6972016-11-14 19:04:52 +02001760 } else {
1761 headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
1762 outer_headers);
1763 headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
1764 outer_headers);
Ariel Levkovich19cc7522017-04-03 13:11:03 +03001765 match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
1766 ft_field_support.outer_ip_version);
Moses Reuben2d1e6972016-11-14 19:04:52 +02001767 }
1768
1769 switch (ib_spec->type & ~IB_FLOW_SPEC_INNER) {
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001770 case IB_FLOW_SPEC_ETH:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03001771 if (FIELDS_NOT_SUPPORTED(ib_spec->eth.mask, LAST_ETH_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02001772 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001773
Moses Reuben2d1e6972016-11-14 19:04:52 +02001774 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001775 dmac_47_16),
1776 ib_spec->eth.mask.dst_mac);
Moses Reuben2d1e6972016-11-14 19:04:52 +02001777 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001778 dmac_47_16),
1779 ib_spec->eth.val.dst_mac);
1780
Moses Reuben2d1e6972016-11-14 19:04:52 +02001781 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottliebee3da802016-09-12 19:16:24 +03001782 smac_47_16),
1783 ib_spec->eth.mask.src_mac);
Moses Reuben2d1e6972016-11-14 19:04:52 +02001784 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottliebee3da802016-09-12 19:16:24 +03001785 smac_47_16),
1786 ib_spec->eth.val.src_mac);
1787
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001788 if (ib_spec->eth.mask.vlan_tag) {
Moses Reuben2d1e6972016-11-14 19:04:52 +02001789 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Mohamad Haj Yahia10543362016-10-09 16:25:43 +03001790 cvlan_tag, 1);
Moses Reuben2d1e6972016-11-14 19:04:52 +02001791 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Mohamad Haj Yahia10543362016-10-09 16:25:43 +03001792 cvlan_tag, 1);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001793
Moses Reuben2d1e6972016-11-14 19:04:52 +02001794 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001795 first_vid, ntohs(ib_spec->eth.mask.vlan_tag));
Moses Reuben2d1e6972016-11-14 19:04:52 +02001796 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001797 first_vid, ntohs(ib_spec->eth.val.vlan_tag));
1798
Moses Reuben2d1e6972016-11-14 19:04:52 +02001799 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001800 first_cfi,
1801 ntohs(ib_spec->eth.mask.vlan_tag) >> 12);
Moses Reuben2d1e6972016-11-14 19:04:52 +02001802 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001803 first_cfi,
1804 ntohs(ib_spec->eth.val.vlan_tag) >> 12);
1805
Moses Reuben2d1e6972016-11-14 19:04:52 +02001806 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001807 first_prio,
1808 ntohs(ib_spec->eth.mask.vlan_tag) >> 13);
Moses Reuben2d1e6972016-11-14 19:04:52 +02001809 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001810 first_prio,
1811 ntohs(ib_spec->eth.val.vlan_tag) >> 13);
1812 }
Moses Reuben2d1e6972016-11-14 19:04:52 +02001813 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001814 ethertype, ntohs(ib_spec->eth.mask.ether_type));
Moses Reuben2d1e6972016-11-14 19:04:52 +02001815 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001816 ethertype, ntohs(ib_spec->eth.val.ether_type));
1817 break;
1818 case IB_FLOW_SPEC_IPV4:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03001819 if (FIELDS_NOT_SUPPORTED(ib_spec->ipv4.mask, LAST_IPV4_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02001820 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001821
Ariel Levkovich19cc7522017-04-03 13:11:03 +03001822 if (match_ipv) {
1823 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1824 ip_version, 0xf);
1825 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1826 ip_version, IPV4_VERSION);
1827 } else {
1828 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1829 ethertype, 0xffff);
1830 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1831 ethertype, ETH_P_IP);
1832 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001833
Moses Reuben2d1e6972016-11-14 19:04:52 +02001834 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001835 src_ipv4_src_ipv6.ipv4_layout.ipv4),
1836 &ib_spec->ipv4.mask.src_ip,
1837 sizeof(ib_spec->ipv4.mask.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02001838 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001839 src_ipv4_src_ipv6.ipv4_layout.ipv4),
1840 &ib_spec->ipv4.val.src_ip,
1841 sizeof(ib_spec->ipv4.val.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02001842 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001843 dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
1844 &ib_spec->ipv4.mask.dst_ip,
1845 sizeof(ib_spec->ipv4.mask.dst_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02001846 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001847 dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
1848 &ib_spec->ipv4.val.dst_ip,
1849 sizeof(ib_spec->ipv4.val.dst_ip));
Maor Gottliebca0d4752016-08-30 16:58:35 +03001850
Moses Reuben2d1e6972016-11-14 19:04:52 +02001851 set_tos(headers_c, headers_v,
Maor Gottliebca0d4752016-08-30 16:58:35 +03001852 ib_spec->ipv4.mask.tos, ib_spec->ipv4.val.tos);
1853
Moses Reuben2d1e6972016-11-14 19:04:52 +02001854 set_proto(headers_c, headers_v,
Maor Gottliebca0d4752016-08-30 16:58:35 +03001855 ib_spec->ipv4.mask.proto, ib_spec->ipv4.val.proto);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001856 break;
Maor Gottlieb026bae02016-06-17 15:14:51 +03001857 case IB_FLOW_SPEC_IPV6:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03001858 if (FIELDS_NOT_SUPPORTED(ib_spec->ipv6.mask, LAST_IPV6_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02001859 return -EOPNOTSUPP;
Maor Gottlieb026bae02016-06-17 15:14:51 +03001860
Ariel Levkovich19cc7522017-04-03 13:11:03 +03001861 if (match_ipv) {
1862 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1863 ip_version, 0xf);
1864 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1865 ip_version, IPV6_VERSION);
1866 } else {
1867 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
1868 ethertype, 0xffff);
1869 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
1870 ethertype, ETH_P_IPV6);
1871 }
Maor Gottlieb026bae02016-06-17 15:14:51 +03001872
Moses Reuben2d1e6972016-11-14 19:04:52 +02001873 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb026bae02016-06-17 15:14:51 +03001874 src_ipv4_src_ipv6.ipv6_layout.ipv6),
1875 &ib_spec->ipv6.mask.src_ip,
1876 sizeof(ib_spec->ipv6.mask.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02001877 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb026bae02016-06-17 15:14:51 +03001878 src_ipv4_src_ipv6.ipv6_layout.ipv6),
1879 &ib_spec->ipv6.val.src_ip,
1880 sizeof(ib_spec->ipv6.val.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02001881 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb026bae02016-06-17 15:14:51 +03001882 dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
1883 &ib_spec->ipv6.mask.dst_ip,
1884 sizeof(ib_spec->ipv6.mask.dst_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02001885 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb026bae02016-06-17 15:14:51 +03001886 dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
1887 &ib_spec->ipv6.val.dst_ip,
1888 sizeof(ib_spec->ipv6.val.dst_ip));
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03001889
Moses Reuben2d1e6972016-11-14 19:04:52 +02001890 set_tos(headers_c, headers_v,
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03001891 ib_spec->ipv6.mask.traffic_class,
1892 ib_spec->ipv6.val.traffic_class);
1893
Moses Reuben2d1e6972016-11-14 19:04:52 +02001894 set_proto(headers_c, headers_v,
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03001895 ib_spec->ipv6.mask.next_hdr,
1896 ib_spec->ipv6.val.next_hdr);
1897
Moses Reuben2d1e6972016-11-14 19:04:52 +02001898 set_flow_label(misc_params_c, misc_params_v,
1899 ntohl(ib_spec->ipv6.mask.flow_label),
1900 ntohl(ib_spec->ipv6.val.flow_label),
1901 ib_spec->type & IB_FLOW_SPEC_INNER);
1902
Maor Gottlieb026bae02016-06-17 15:14:51 +03001903 break;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001904 case IB_FLOW_SPEC_TCP:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03001905 if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
1906 LAST_TCP_UDP_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02001907 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001908
Moses Reuben2d1e6972016-11-14 19:04:52 +02001909 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001910 0xff);
Moses Reuben2d1e6972016-11-14 19:04:52 +02001911 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001912 IPPROTO_TCP);
1913
Moses Reuben2d1e6972016-11-14 19:04:52 +02001914 MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001915 ntohs(ib_spec->tcp_udp.mask.src_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02001916 MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001917 ntohs(ib_spec->tcp_udp.val.src_port));
1918
Moses Reuben2d1e6972016-11-14 19:04:52 +02001919 MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001920 ntohs(ib_spec->tcp_udp.mask.dst_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02001921 MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001922 ntohs(ib_spec->tcp_udp.val.dst_port));
1923 break;
1924 case IB_FLOW_SPEC_UDP:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03001925 if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
1926 LAST_TCP_UDP_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02001927 return -EOPNOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001928
Moses Reuben2d1e6972016-11-14 19:04:52 +02001929 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001930 0xff);
Moses Reuben2d1e6972016-11-14 19:04:52 +02001931 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001932 IPPROTO_UDP);
1933
Moses Reuben2d1e6972016-11-14 19:04:52 +02001934 MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001935 ntohs(ib_spec->tcp_udp.mask.src_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02001936 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001937 ntohs(ib_spec->tcp_udp.val.src_port));
1938
Moses Reuben2d1e6972016-11-14 19:04:52 +02001939 MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001940 ntohs(ib_spec->tcp_udp.mask.dst_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02001941 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001942 ntohs(ib_spec->tcp_udp.val.dst_port));
1943 break;
Moses Reubenffb30d82016-11-14 19:04:50 +02001944 case IB_FLOW_SPEC_VXLAN_TUNNEL:
1945 if (FIELDS_NOT_SUPPORTED(ib_spec->tunnel.mask,
1946 LAST_TUNNEL_FIELD))
Leon Romanovsky1ffd3a22017-01-18 14:59:51 +02001947 return -EOPNOTSUPP;
Moses Reubenffb30d82016-11-14 19:04:50 +02001948
1949 MLX5_SET(fte_match_set_misc, misc_params_c, vxlan_vni,
1950 ntohl(ib_spec->tunnel.mask.tunnel_id));
1951 MLX5_SET(fte_match_set_misc, misc_params_v, vxlan_vni,
1952 ntohl(ib_spec->tunnel.val.tunnel_id));
1953 break;
Moses Reuben2ac693f2017-01-18 14:59:50 +02001954 case IB_FLOW_SPEC_ACTION_TAG:
1955 if (FIELDS_NOT_SUPPORTED(ib_spec->flow_tag,
1956 LAST_FLOW_TAG_FIELD))
1957 return -EOPNOTSUPP;
1958 if (ib_spec->flow_tag.tag_id >= BIT(24))
1959 return -EINVAL;
1960
1961 *tag_id = ib_spec->flow_tag.tag_id;
1962 break;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001963 default:
1964 return -EINVAL;
1965 }
1966
1967 return 0;
1968}
1969
1970/* If a flow could catch both multicast and unicast packets,
1971 * it won't fall into the multicast flow steering table and this rule
1972 * could steal other multicast packets.
1973 */
1974static bool flow_is_multicast_only(struct ib_flow_attr *ib_attr)
1975{
1976 struct ib_flow_spec_eth *eth_spec;
1977
1978 if (ib_attr->type != IB_FLOW_ATTR_NORMAL ||
1979 ib_attr->size < sizeof(struct ib_flow_attr) +
1980 sizeof(struct ib_flow_spec_eth) ||
1981 ib_attr->num_of_specs < 1)
1982 return false;
1983
1984 eth_spec = (struct ib_flow_spec_eth *)(ib_attr + 1);
1985 if (eth_spec->type != IB_FLOW_SPEC_ETH ||
1986 eth_spec->size != sizeof(*eth_spec))
1987 return false;
1988
1989 return is_multicast_ether_addr(eth_spec->mask.dst_mac) &&
1990 is_multicast_ether_addr(eth_spec->val.dst_mac);
1991}
1992
Ariel Levkovich19cc7522017-04-03 13:11:03 +03001993static bool is_valid_ethertype(struct mlx5_core_dev *mdev,
1994 const struct ib_flow_attr *flow_attr,
Ariel Levkovich0f750962017-04-03 13:11:02 +03001995 bool check_inner)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001996{
1997 union ib_flow_spec *ib_spec = (union ib_flow_spec *)(flow_attr + 1);
Ariel Levkovich19cc7522017-04-03 13:11:03 +03001998 int match_ipv = check_inner ?
1999 MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2000 ft_field_support.inner_ip_version) :
2001 MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
2002 ft_field_support.outer_ip_version);
Ariel Levkovich0f750962017-04-03 13:11:02 +03002003 int inner_bit = check_inner ? IB_FLOW_SPEC_INNER : 0;
2004 bool ipv4_spec_valid, ipv6_spec_valid;
2005 unsigned int ip_spec_type = 0;
2006 bool has_ethertype = false;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002007 unsigned int spec_index;
Ariel Levkovich0f750962017-04-03 13:11:02 +03002008 bool mask_valid = true;
2009 u16 eth_type = 0;
2010 bool type_valid;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002011
2012 /* Validate that ethertype is correct */
2013 for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
Ariel Levkovich0f750962017-04-03 13:11:02 +03002014 if ((ib_spec->type == (IB_FLOW_SPEC_ETH | inner_bit)) &&
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002015 ib_spec->eth.mask.ether_type) {
Ariel Levkovich0f750962017-04-03 13:11:02 +03002016 mask_valid = (ib_spec->eth.mask.ether_type ==
2017 htons(0xffff));
2018 has_ethertype = true;
2019 eth_type = ntohs(ib_spec->eth.val.ether_type);
2020 } else if ((ib_spec->type == (IB_FLOW_SPEC_IPV4 | inner_bit)) ||
2021 (ib_spec->type == (IB_FLOW_SPEC_IPV6 | inner_bit))) {
2022 ip_spec_type = ib_spec->type;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002023 }
2024 ib_spec = (void *)ib_spec + ib_spec->size;
2025 }
Ariel Levkovich0f750962017-04-03 13:11:02 +03002026
2027 type_valid = (!has_ethertype) || (!ip_spec_type);
2028 if (!type_valid && mask_valid) {
2029 ipv4_spec_valid = (eth_type == ETH_P_IP) &&
2030 (ip_spec_type == (IB_FLOW_SPEC_IPV4 | inner_bit));
2031 ipv6_spec_valid = (eth_type == ETH_P_IPV6) &&
2032 (ip_spec_type == (IB_FLOW_SPEC_IPV6 | inner_bit));
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002033
2034 type_valid = (ipv4_spec_valid) || (ipv6_spec_valid) ||
2035 (((eth_type == ETH_P_MPLS_UC) ||
2036 (eth_type == ETH_P_MPLS_MC)) && match_ipv);
Ariel Levkovich0f750962017-04-03 13:11:02 +03002037 }
2038
2039 return type_valid;
2040}
2041
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002042static bool is_valid_attr(struct mlx5_core_dev *mdev,
2043 const struct ib_flow_attr *flow_attr)
Ariel Levkovich0f750962017-04-03 13:11:02 +03002044{
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002045 return is_valid_ethertype(mdev, flow_attr, false) &&
2046 is_valid_ethertype(mdev, flow_attr, true);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002047}
2048
2049static void put_flow_table(struct mlx5_ib_dev *dev,
2050 struct mlx5_ib_flow_prio *prio, bool ft_added)
2051{
2052 prio->refcount -= !!ft_added;
2053 if (!prio->refcount) {
2054 mlx5_destroy_flow_table(prio->flow_table);
2055 prio->flow_table = NULL;
2056 }
2057}
2058
2059static int mlx5_ib_destroy_flow(struct ib_flow *flow_id)
2060{
2061 struct mlx5_ib_dev *dev = to_mdev(flow_id->qp->device);
2062 struct mlx5_ib_flow_handler *handler = container_of(flow_id,
2063 struct mlx5_ib_flow_handler,
2064 ibflow);
2065 struct mlx5_ib_flow_handler *iter, *tmp;
2066
2067 mutex_lock(&dev->flow_db.lock);
2068
2069 list_for_each_entry_safe(iter, tmp, &handler->list, list) {
Mark Bloch74491de2016-08-31 11:24:25 +00002070 mlx5_del_flow_rules(iter->rule);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002071 put_flow_table(dev, iter->prio, true);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002072 list_del(&iter->list);
2073 kfree(iter);
2074 }
2075
Mark Bloch74491de2016-08-31 11:24:25 +00002076 mlx5_del_flow_rules(handler->rule);
Maor Gottlieb5497adc2016-08-28 14:16:31 +03002077 put_flow_table(dev, handler->prio, true);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002078 mutex_unlock(&dev->flow_db.lock);
2079
2080 kfree(handler);
2081
2082 return 0;
2083}
2084
Maor Gottlieb35d190112016-03-07 18:51:47 +02002085static int ib_prio_to_core_prio(unsigned int priority, bool dont_trap)
2086{
2087 priority *= 2;
2088 if (!dont_trap)
2089 priority++;
2090 return priority;
2091}
2092
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002093enum flow_table_type {
2094 MLX5_IB_FT_RX,
2095 MLX5_IB_FT_TX
2096};
2097
Maor Gottlieb00b7c2a2017-03-29 06:09:01 +03002098#define MLX5_FS_MAX_TYPES 6
2099#define MLX5_FS_MAX_ENTRIES BIT(16)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002100static struct mlx5_ib_flow_prio *get_flow_table(struct mlx5_ib_dev *dev,
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002101 struct ib_flow_attr *flow_attr,
2102 enum flow_table_type ft_type)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002103{
Maor Gottlieb35d190112016-03-07 18:51:47 +02002104 bool dont_trap = flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002105 struct mlx5_flow_namespace *ns = NULL;
2106 struct mlx5_ib_flow_prio *prio;
2107 struct mlx5_flow_table *ft;
Maor Gottliebdac388e2017-03-29 06:09:00 +03002108 int max_table_size;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002109 int num_entries;
2110 int num_groups;
2111 int priority;
2112 int err = 0;
2113
Maor Gottliebdac388e2017-03-29 06:09:00 +03002114 max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
2115 log_max_ft_size));
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002116 if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
Maor Gottlieb35d190112016-03-07 18:51:47 +02002117 if (flow_is_multicast_only(flow_attr) &&
2118 !dont_trap)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002119 priority = MLX5_IB_FLOW_MCAST_PRIO;
2120 else
Maor Gottlieb35d190112016-03-07 18:51:47 +02002121 priority = ib_prio_to_core_prio(flow_attr->priority,
2122 dont_trap);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002123 ns = mlx5_get_flow_namespace(dev->mdev,
2124 MLX5_FLOW_NAMESPACE_BYPASS);
2125 num_entries = MLX5_FS_MAX_ENTRIES;
2126 num_groups = MLX5_FS_MAX_TYPES;
2127 prio = &dev->flow_db.prios[priority];
2128 } else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
2129 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
2130 ns = mlx5_get_flow_namespace(dev->mdev,
2131 MLX5_FLOW_NAMESPACE_LEFTOVERS);
2132 build_leftovers_ft_param(&priority,
2133 &num_entries,
2134 &num_groups);
2135 prio = &dev->flow_db.prios[MLX5_IB_FLOW_LEFTOVERS_PRIO];
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002136 } else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
2137 if (!MLX5_CAP_FLOWTABLE(dev->mdev,
2138 allow_sniffer_and_nic_rx_shared_tir))
2139 return ERR_PTR(-ENOTSUPP);
2140
2141 ns = mlx5_get_flow_namespace(dev->mdev, ft_type == MLX5_IB_FT_RX ?
2142 MLX5_FLOW_NAMESPACE_SNIFFER_RX :
2143 MLX5_FLOW_NAMESPACE_SNIFFER_TX);
2144
2145 prio = &dev->flow_db.sniffer[ft_type];
2146 priority = 0;
2147 num_entries = 1;
2148 num_groups = 1;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002149 }
2150
2151 if (!ns)
2152 return ERR_PTR(-ENOTSUPP);
2153
Maor Gottliebdac388e2017-03-29 06:09:00 +03002154 if (num_entries > max_table_size)
2155 return ERR_PTR(-ENOMEM);
2156
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002157 ft = prio->flow_table;
2158 if (!ft) {
2159 ft = mlx5_create_auto_grouped_flow_table(ns, priority,
2160 num_entries,
Maor Gottliebd63cd282016-04-29 01:36:35 +03002161 num_groups,
Hadar Hen Zionc9f1b072016-11-07 15:14:44 +02002162 0, 0);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002163
2164 if (!IS_ERR(ft)) {
2165 prio->refcount = 0;
2166 prio->flow_table = ft;
2167 } else {
2168 err = PTR_ERR(ft);
2169 }
2170 }
2171
2172 return err ? ERR_PTR(err) : prio;
2173}
2174
2175static struct mlx5_ib_flow_handler *create_flow_rule(struct mlx5_ib_dev *dev,
2176 struct mlx5_ib_flow_prio *ft_prio,
Maor Gottliebdd063d02016-08-28 14:16:32 +03002177 const struct ib_flow_attr *flow_attr,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002178 struct mlx5_flow_destination *dst)
2179{
2180 struct mlx5_flow_table *ft = ft_prio->flow_table;
2181 struct mlx5_ib_flow_handler *handler;
Hadar Hen Zion66958ed2016-11-07 15:14:45 +02002182 struct mlx5_flow_act flow_act = {0};
Maor Gottliebc5bb1732016-07-04 17:23:05 +03002183 struct mlx5_flow_spec *spec;
Maor Gottliebdd063d02016-08-28 14:16:32 +03002184 const void *ib_flow = (const void *)flow_attr + sizeof(*flow_attr);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002185 unsigned int spec_index;
Moses Reuben2ac693f2017-01-18 14:59:50 +02002186 u32 flow_tag = MLX5_FS_DEFAULT_FLOW_TAG;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002187 int err = 0;
2188
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002189 if (!is_valid_attr(dev->mdev, flow_attr))
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002190 return ERR_PTR(-EINVAL);
2191
Maor Gottliebc5bb1732016-07-04 17:23:05 +03002192 spec = mlx5_vzalloc(sizeof(*spec));
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002193 handler = kzalloc(sizeof(*handler), GFP_KERNEL);
Maor Gottliebc5bb1732016-07-04 17:23:05 +03002194 if (!handler || !spec) {
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002195 err = -ENOMEM;
2196 goto free;
2197 }
2198
2199 INIT_LIST_HEAD(&handler->list);
2200
2201 for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
Ariel Levkovich19cc7522017-04-03 13:11:03 +03002202 err = parse_flow_attr(dev->mdev, spec->match_criteria,
Moses Reuben2ac693f2017-01-18 14:59:50 +02002203 spec->match_value, ib_flow, &flow_tag);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002204 if (err < 0)
2205 goto free;
2206
2207 ib_flow += ((union ib_flow_spec *)ib_flow)->size;
2208 }
2209
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002210 spec->match_criteria_enable = get_match_criteria_enable(spec->match_criteria);
Hadar Hen Zion66958ed2016-11-07 15:14:45 +02002211 flow_act.action = dst ? MLX5_FLOW_CONTEXT_ACTION_FWD_DEST :
Maor Gottlieb35d190112016-03-07 18:51:47 +02002212 MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO;
Moses Reuben2ac693f2017-01-18 14:59:50 +02002213
2214 if (flow_tag != MLX5_FS_DEFAULT_FLOW_TAG &&
2215 (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
2216 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT)) {
2217 mlx5_ib_warn(dev, "Flow tag %u and attribute type %x isn't allowed in leftovers\n",
2218 flow_tag, flow_attr->type);
2219 err = -EINVAL;
2220 goto free;
2221 }
2222 flow_act.flow_tag = flow_tag;
Mark Bloch74491de2016-08-31 11:24:25 +00002223 handler->rule = mlx5_add_flow_rules(ft, spec,
Hadar Hen Zion66958ed2016-11-07 15:14:45 +02002224 &flow_act,
2225 dst, 1);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002226
2227 if (IS_ERR(handler->rule)) {
2228 err = PTR_ERR(handler->rule);
2229 goto free;
2230 }
2231
Maor Gottliebd9d49802016-08-28 14:16:33 +03002232 ft_prio->refcount++;
Maor Gottlieb5497adc2016-08-28 14:16:31 +03002233 handler->prio = ft_prio;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002234
2235 ft_prio->flow_table = ft;
2236free:
2237 if (err)
2238 kfree(handler);
Maor Gottliebc5bb1732016-07-04 17:23:05 +03002239 kvfree(spec);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002240 return err ? ERR_PTR(err) : handler;
2241}
2242
Maor Gottlieb35d190112016-03-07 18:51:47 +02002243static struct mlx5_ib_flow_handler *create_dont_trap_rule(struct mlx5_ib_dev *dev,
2244 struct mlx5_ib_flow_prio *ft_prio,
2245 struct ib_flow_attr *flow_attr,
2246 struct mlx5_flow_destination *dst)
2247{
2248 struct mlx5_ib_flow_handler *handler_dst = NULL;
2249 struct mlx5_ib_flow_handler *handler = NULL;
2250
2251 handler = create_flow_rule(dev, ft_prio, flow_attr, NULL);
2252 if (!IS_ERR(handler)) {
2253 handler_dst = create_flow_rule(dev, ft_prio,
2254 flow_attr, dst);
2255 if (IS_ERR(handler_dst)) {
Mark Bloch74491de2016-08-31 11:24:25 +00002256 mlx5_del_flow_rules(handler->rule);
Maor Gottliebd9d49802016-08-28 14:16:33 +03002257 ft_prio->refcount--;
Maor Gottlieb35d190112016-03-07 18:51:47 +02002258 kfree(handler);
2259 handler = handler_dst;
2260 } else {
2261 list_add(&handler_dst->list, &handler->list);
2262 }
2263 }
2264
2265 return handler;
2266}
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002267enum {
2268 LEFTOVERS_MC,
2269 LEFTOVERS_UC,
2270};
2271
2272static struct mlx5_ib_flow_handler *create_leftovers_rule(struct mlx5_ib_dev *dev,
2273 struct mlx5_ib_flow_prio *ft_prio,
2274 struct ib_flow_attr *flow_attr,
2275 struct mlx5_flow_destination *dst)
2276{
2277 struct mlx5_ib_flow_handler *handler_ucast = NULL;
2278 struct mlx5_ib_flow_handler *handler = NULL;
2279
2280 static struct {
2281 struct ib_flow_attr flow_attr;
2282 struct ib_flow_spec_eth eth_flow;
2283 } leftovers_specs[] = {
2284 [LEFTOVERS_MC] = {
2285 .flow_attr = {
2286 .num_of_specs = 1,
2287 .size = sizeof(leftovers_specs[0])
2288 },
2289 .eth_flow = {
2290 .type = IB_FLOW_SPEC_ETH,
2291 .size = sizeof(struct ib_flow_spec_eth),
2292 .mask = {.dst_mac = {0x1} },
2293 .val = {.dst_mac = {0x1} }
2294 }
2295 },
2296 [LEFTOVERS_UC] = {
2297 .flow_attr = {
2298 .num_of_specs = 1,
2299 .size = sizeof(leftovers_specs[0])
2300 },
2301 .eth_flow = {
2302 .type = IB_FLOW_SPEC_ETH,
2303 .size = sizeof(struct ib_flow_spec_eth),
2304 .mask = {.dst_mac = {0x1} },
2305 .val = {.dst_mac = {} }
2306 }
2307 }
2308 };
2309
2310 handler = create_flow_rule(dev, ft_prio,
2311 &leftovers_specs[LEFTOVERS_MC].flow_attr,
2312 dst);
2313 if (!IS_ERR(handler) &&
2314 flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT) {
2315 handler_ucast = create_flow_rule(dev, ft_prio,
2316 &leftovers_specs[LEFTOVERS_UC].flow_attr,
2317 dst);
2318 if (IS_ERR(handler_ucast)) {
Mark Bloch74491de2016-08-31 11:24:25 +00002319 mlx5_del_flow_rules(handler->rule);
Maor Gottliebd9d49802016-08-28 14:16:33 +03002320 ft_prio->refcount--;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002321 kfree(handler);
2322 handler = handler_ucast;
2323 } else {
2324 list_add(&handler_ucast->list, &handler->list);
2325 }
2326 }
2327
2328 return handler;
2329}
2330
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002331static struct mlx5_ib_flow_handler *create_sniffer_rule(struct mlx5_ib_dev *dev,
2332 struct mlx5_ib_flow_prio *ft_rx,
2333 struct mlx5_ib_flow_prio *ft_tx,
2334 struct mlx5_flow_destination *dst)
2335{
2336 struct mlx5_ib_flow_handler *handler_rx;
2337 struct mlx5_ib_flow_handler *handler_tx;
2338 int err;
2339 static const struct ib_flow_attr flow_attr = {
2340 .num_of_specs = 0,
2341 .size = sizeof(flow_attr)
2342 };
2343
2344 handler_rx = create_flow_rule(dev, ft_rx, &flow_attr, dst);
2345 if (IS_ERR(handler_rx)) {
2346 err = PTR_ERR(handler_rx);
2347 goto err;
2348 }
2349
2350 handler_tx = create_flow_rule(dev, ft_tx, &flow_attr, dst);
2351 if (IS_ERR(handler_tx)) {
2352 err = PTR_ERR(handler_tx);
2353 goto err_tx;
2354 }
2355
2356 list_add(&handler_tx->list, &handler_rx->list);
2357
2358 return handler_rx;
2359
2360err_tx:
Mark Bloch74491de2016-08-31 11:24:25 +00002361 mlx5_del_flow_rules(handler_rx->rule);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002362 ft_rx->refcount--;
2363 kfree(handler_rx);
2364err:
2365 return ERR_PTR(err);
2366}
2367
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002368static struct ib_flow *mlx5_ib_create_flow(struct ib_qp *qp,
2369 struct ib_flow_attr *flow_attr,
2370 int domain)
2371{
2372 struct mlx5_ib_dev *dev = to_mdev(qp->device);
Yishai Hadasd9f88e52016-08-28 10:58:37 +03002373 struct mlx5_ib_qp *mqp = to_mqp(qp);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002374 struct mlx5_ib_flow_handler *handler = NULL;
2375 struct mlx5_flow_destination *dst = NULL;
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002376 struct mlx5_ib_flow_prio *ft_prio_tx = NULL;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002377 struct mlx5_ib_flow_prio *ft_prio;
2378 int err;
2379
2380 if (flow_attr->priority > MLX5_IB_FLOW_LAST_PRIO)
Maor Gottliebdac388e2017-03-29 06:09:00 +03002381 return ERR_PTR(-ENOMEM);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002382
2383 if (domain != IB_FLOW_DOMAIN_USER ||
2384 flow_attr->port > MLX5_CAP_GEN(dev->mdev, num_ports) ||
Maor Gottlieb35d190112016-03-07 18:51:47 +02002385 (flow_attr->flags & ~IB_FLOW_ATTR_FLAGS_DONT_TRAP))
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002386 return ERR_PTR(-EINVAL);
2387
2388 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
2389 if (!dst)
2390 return ERR_PTR(-ENOMEM);
2391
2392 mutex_lock(&dev->flow_db.lock);
2393
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002394 ft_prio = get_flow_table(dev, flow_attr, MLX5_IB_FT_RX);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002395 if (IS_ERR(ft_prio)) {
2396 err = PTR_ERR(ft_prio);
2397 goto unlock;
2398 }
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002399 if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
2400 ft_prio_tx = get_flow_table(dev, flow_attr, MLX5_IB_FT_TX);
2401 if (IS_ERR(ft_prio_tx)) {
2402 err = PTR_ERR(ft_prio_tx);
2403 ft_prio_tx = NULL;
2404 goto destroy_ft;
2405 }
2406 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002407
2408 dst->type = MLX5_FLOW_DESTINATION_TYPE_TIR;
Yishai Hadasd9f88e52016-08-28 10:58:37 +03002409 if (mqp->flags & MLX5_IB_QP_RSS)
2410 dst->tir_num = mqp->rss_qp.tirn;
2411 else
2412 dst->tir_num = mqp->raw_packet_qp.rq.tirn;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002413
2414 if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
Maor Gottlieb35d190112016-03-07 18:51:47 +02002415 if (flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP) {
2416 handler = create_dont_trap_rule(dev, ft_prio,
2417 flow_attr, dst);
2418 } else {
2419 handler = create_flow_rule(dev, ft_prio, flow_attr,
2420 dst);
2421 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002422 } else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
2423 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
2424 handler = create_leftovers_rule(dev, ft_prio, flow_attr,
2425 dst);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002426 } else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
2427 handler = create_sniffer_rule(dev, ft_prio, ft_prio_tx, dst);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002428 } else {
2429 err = -EINVAL;
2430 goto destroy_ft;
2431 }
2432
2433 if (IS_ERR(handler)) {
2434 err = PTR_ERR(handler);
2435 handler = NULL;
2436 goto destroy_ft;
2437 }
2438
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002439 mutex_unlock(&dev->flow_db.lock);
2440 kfree(dst);
2441
2442 return &handler->ibflow;
2443
2444destroy_ft:
2445 put_flow_table(dev, ft_prio, false);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002446 if (ft_prio_tx)
2447 put_flow_table(dev, ft_prio_tx, false);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002448unlock:
2449 mutex_unlock(&dev->flow_db.lock);
2450 kfree(dst);
2451 kfree(handler);
2452 return ERR_PTR(err);
2453}
2454
Eli Cohene126ba92013-07-07 17:25:49 +03002455static int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
2456{
2457 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
2458 int err;
2459
Jack Morgenstein9603b612014-07-28 23:30:22 +03002460 err = mlx5_core_attach_mcg(dev->mdev, gid, ibqp->qp_num);
Eli Cohene126ba92013-07-07 17:25:49 +03002461 if (err)
2462 mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n",
2463 ibqp->qp_num, gid->raw);
2464
2465 return err;
2466}
2467
2468static int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
2469{
2470 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
2471 int err;
2472
Jack Morgenstein9603b612014-07-28 23:30:22 +03002473 err = mlx5_core_detach_mcg(dev->mdev, gid, ibqp->qp_num);
Eli Cohene126ba92013-07-07 17:25:49 +03002474 if (err)
2475 mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n",
2476 ibqp->qp_num, gid->raw);
2477
2478 return err;
2479}
2480
2481static int init_node_data(struct mlx5_ib_dev *dev)
2482{
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03002483 int err;
Eli Cohene126ba92013-07-07 17:25:49 +03002484
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03002485 err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc);
Eli Cohene126ba92013-07-07 17:25:49 +03002486 if (err)
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03002487 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03002488
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03002489 dev->mdev->rev_id = dev->mdev->pdev->revision;
Eli Cohene126ba92013-07-07 17:25:49 +03002490
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03002491 return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid);
Eli Cohene126ba92013-07-07 17:25:49 +03002492}
2493
2494static ssize_t show_fw_pages(struct device *device, struct device_attribute *attr,
2495 char *buf)
2496{
2497 struct mlx5_ib_dev *dev =
2498 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
2499
Jack Morgenstein9603b612014-07-28 23:30:22 +03002500 return sprintf(buf, "%d\n", dev->mdev->priv.fw_pages);
Eli Cohene126ba92013-07-07 17:25:49 +03002501}
2502
2503static ssize_t show_reg_pages(struct device *device,
2504 struct device_attribute *attr, char *buf)
2505{
2506 struct mlx5_ib_dev *dev =
2507 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
2508
Haggai Eran6aec21f2014-12-11 17:04:23 +02002509 return sprintf(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages));
Eli Cohene126ba92013-07-07 17:25:49 +03002510}
2511
2512static ssize_t show_hca(struct device *device, struct device_attribute *attr,
2513 char *buf)
2514{
2515 struct mlx5_ib_dev *dev =
2516 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
Jack Morgenstein9603b612014-07-28 23:30:22 +03002517 return sprintf(buf, "MT%d\n", dev->mdev->pdev->device);
Eli Cohene126ba92013-07-07 17:25:49 +03002518}
2519
Eli Cohene126ba92013-07-07 17:25:49 +03002520static ssize_t show_rev(struct device *device, struct device_attribute *attr,
2521 char *buf)
2522{
2523 struct mlx5_ib_dev *dev =
2524 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
Jack Morgenstein9603b612014-07-28 23:30:22 +03002525 return sprintf(buf, "%x\n", dev->mdev->rev_id);
Eli Cohene126ba92013-07-07 17:25:49 +03002526}
2527
2528static ssize_t show_board(struct device *device, struct device_attribute *attr,
2529 char *buf)
2530{
2531 struct mlx5_ib_dev *dev =
2532 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
2533 return sprintf(buf, "%.*s\n", MLX5_BOARD_ID_LEN,
Jack Morgenstein9603b612014-07-28 23:30:22 +03002534 dev->mdev->board_id);
Eli Cohene126ba92013-07-07 17:25:49 +03002535}
2536
2537static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03002538static DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL);
2539static DEVICE_ATTR(board_id, S_IRUGO, show_board, NULL);
2540static DEVICE_ATTR(fw_pages, S_IRUGO, show_fw_pages, NULL);
2541static DEVICE_ATTR(reg_pages, S_IRUGO, show_reg_pages, NULL);
2542
2543static struct device_attribute *mlx5_class_attributes[] = {
2544 &dev_attr_hw_rev,
Eli Cohene126ba92013-07-07 17:25:49 +03002545 &dev_attr_hca_type,
2546 &dev_attr_board_id,
2547 &dev_attr_fw_pages,
2548 &dev_attr_reg_pages,
2549};
2550
Haggai Eran7722f472016-02-29 15:45:07 +02002551static void pkey_change_handler(struct work_struct *work)
2552{
2553 struct mlx5_ib_port_resources *ports =
2554 container_of(work, struct mlx5_ib_port_resources,
2555 pkey_change_work);
2556
2557 mutex_lock(&ports->devr->mutex);
2558 mlx5_ib_gsi_pkey_change(ports->gsi);
2559 mutex_unlock(&ports->devr->mutex);
2560}
2561
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002562static void mlx5_ib_handle_internal_error(struct mlx5_ib_dev *ibdev)
2563{
2564 struct mlx5_ib_qp *mqp;
2565 struct mlx5_ib_cq *send_mcq, *recv_mcq;
2566 struct mlx5_core_cq *mcq;
2567 struct list_head cq_armed_list;
2568 unsigned long flags_qp;
2569 unsigned long flags_cq;
2570 unsigned long flags;
2571
2572 INIT_LIST_HEAD(&cq_armed_list);
2573
2574 /* Go over qp list reside on that ibdev, sync with create/destroy qp.*/
2575 spin_lock_irqsave(&ibdev->reset_flow_resource_lock, flags);
2576 list_for_each_entry(mqp, &ibdev->qp_list, qps_list) {
2577 spin_lock_irqsave(&mqp->sq.lock, flags_qp);
2578 if (mqp->sq.tail != mqp->sq.head) {
2579 send_mcq = to_mcq(mqp->ibqp.send_cq);
2580 spin_lock_irqsave(&send_mcq->lock, flags_cq);
2581 if (send_mcq->mcq.comp &&
2582 mqp->ibqp.send_cq->comp_handler) {
2583 if (!send_mcq->mcq.reset_notify_added) {
2584 send_mcq->mcq.reset_notify_added = 1;
2585 list_add_tail(&send_mcq->mcq.reset_notify,
2586 &cq_armed_list);
2587 }
2588 }
2589 spin_unlock_irqrestore(&send_mcq->lock, flags_cq);
2590 }
2591 spin_unlock_irqrestore(&mqp->sq.lock, flags_qp);
2592 spin_lock_irqsave(&mqp->rq.lock, flags_qp);
2593 /* no handling is needed for SRQ */
2594 if (!mqp->ibqp.srq) {
2595 if (mqp->rq.tail != mqp->rq.head) {
2596 recv_mcq = to_mcq(mqp->ibqp.recv_cq);
2597 spin_lock_irqsave(&recv_mcq->lock, flags_cq);
2598 if (recv_mcq->mcq.comp &&
2599 mqp->ibqp.recv_cq->comp_handler) {
2600 if (!recv_mcq->mcq.reset_notify_added) {
2601 recv_mcq->mcq.reset_notify_added = 1;
2602 list_add_tail(&recv_mcq->mcq.reset_notify,
2603 &cq_armed_list);
2604 }
2605 }
2606 spin_unlock_irqrestore(&recv_mcq->lock,
2607 flags_cq);
2608 }
2609 }
2610 spin_unlock_irqrestore(&mqp->rq.lock, flags_qp);
2611 }
2612 /*At that point all inflight post send were put to be executed as of we
2613 * lock/unlock above locks Now need to arm all involved CQs.
2614 */
2615 list_for_each_entry(mcq, &cq_armed_list, reset_notify) {
2616 mcq->comp(mcq);
2617 }
2618 spin_unlock_irqrestore(&ibdev->reset_flow_resource_lock, flags);
2619}
2620
Jack Morgenstein9603b612014-07-28 23:30:22 +03002621static void mlx5_ib_event(struct mlx5_core_dev *dev, void *context,
Jack Morgenstein4d2f9bb2014-07-28 23:30:24 +03002622 enum mlx5_dev_event event, unsigned long param)
Eli Cohene126ba92013-07-07 17:25:49 +03002623{
Jack Morgenstein9603b612014-07-28 23:30:22 +03002624 struct mlx5_ib_dev *ibdev = (struct mlx5_ib_dev *)context;
Eli Cohene126ba92013-07-07 17:25:49 +03002625 struct ib_event ibev;
Eli Cohendbaaff22016-10-27 16:36:44 +03002626 bool fatal = false;
Eli Cohene126ba92013-07-07 17:25:49 +03002627 u8 port = 0;
2628
2629 switch (event) {
2630 case MLX5_DEV_EVENT_SYS_ERROR:
Eli Cohene126ba92013-07-07 17:25:49 +03002631 ibev.event = IB_EVENT_DEVICE_FATAL;
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002632 mlx5_ib_handle_internal_error(ibdev);
Eli Cohendbaaff22016-10-27 16:36:44 +03002633 fatal = true;
Eli Cohene126ba92013-07-07 17:25:49 +03002634 break;
2635
2636 case MLX5_DEV_EVENT_PORT_UP:
Eli Cohene126ba92013-07-07 17:25:49 +03002637 case MLX5_DEV_EVENT_PORT_DOWN:
Noa Osherovich2788cf32016-06-04 15:15:29 +03002638 case MLX5_DEV_EVENT_PORT_INITIALIZED:
Jack Morgenstein4d2f9bb2014-07-28 23:30:24 +03002639 port = (u8)param;
Aviv Heller5ec8c832016-09-18 20:48:00 +03002640
2641 /* In RoCE, port up/down events are handled in
2642 * mlx5_netdev_event().
2643 */
2644 if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) ==
2645 IB_LINK_LAYER_ETHERNET)
2646 return;
2647
2648 ibev.event = (event == MLX5_DEV_EVENT_PORT_UP) ?
2649 IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
Eli Cohene126ba92013-07-07 17:25:49 +03002650 break;
2651
Eli Cohene126ba92013-07-07 17:25:49 +03002652 case MLX5_DEV_EVENT_LID_CHANGE:
2653 ibev.event = IB_EVENT_LID_CHANGE;
Jack Morgenstein4d2f9bb2014-07-28 23:30:24 +03002654 port = (u8)param;
Eli Cohene126ba92013-07-07 17:25:49 +03002655 break;
2656
2657 case MLX5_DEV_EVENT_PKEY_CHANGE:
2658 ibev.event = IB_EVENT_PKEY_CHANGE;
Jack Morgenstein4d2f9bb2014-07-28 23:30:24 +03002659 port = (u8)param;
Haggai Eran7722f472016-02-29 15:45:07 +02002660
2661 schedule_work(&ibdev->devr.ports[port - 1].pkey_change_work);
Eli Cohene126ba92013-07-07 17:25:49 +03002662 break;
2663
2664 case MLX5_DEV_EVENT_GUID_CHANGE:
2665 ibev.event = IB_EVENT_GID_CHANGE;
Jack Morgenstein4d2f9bb2014-07-28 23:30:24 +03002666 port = (u8)param;
Eli Cohene126ba92013-07-07 17:25:49 +03002667 break;
2668
2669 case MLX5_DEV_EVENT_CLIENT_REREG:
2670 ibev.event = IB_EVENT_CLIENT_REREGISTER;
Jack Morgenstein4d2f9bb2014-07-28 23:30:24 +03002671 port = (u8)param;
Eli Cohene126ba92013-07-07 17:25:49 +03002672 break;
Saeed Mahameedbdc37922016-09-29 19:35:38 +03002673 default:
2674 return;
Eli Cohene126ba92013-07-07 17:25:49 +03002675 }
2676
2677 ibev.device = &ibdev->ib_dev;
2678 ibev.element.port_num = port;
2679
Eli Cohena0c84c32013-09-11 16:35:27 +03002680 if (port < 1 || port > ibdev->num_ports) {
2681 mlx5_ib_warn(ibdev, "warning: event on port %d\n", port);
2682 return;
2683 }
2684
Eli Cohene126ba92013-07-07 17:25:49 +03002685 if (ibdev->ib_active)
2686 ib_dispatch_event(&ibev);
Eli Cohendbaaff22016-10-27 16:36:44 +03002687
2688 if (fatal)
2689 ibdev->ib_active = false;
Eli Cohene126ba92013-07-07 17:25:49 +03002690}
2691
Maor Gottliebc43f1112017-01-18 14:10:33 +02002692static int set_has_smi_cap(struct mlx5_ib_dev *dev)
2693{
2694 struct mlx5_hca_vport_context vport_ctx;
2695 int err;
2696 int port;
2697
2698 for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++) {
2699 dev->mdev->port_caps[port - 1].has_smi = false;
2700 if (MLX5_CAP_GEN(dev->mdev, port_type) ==
2701 MLX5_CAP_PORT_TYPE_IB) {
2702 if (MLX5_CAP_GEN(dev->mdev, ib_virt)) {
2703 err = mlx5_query_hca_vport_context(dev->mdev, 0,
2704 port, 0,
2705 &vport_ctx);
2706 if (err) {
2707 mlx5_ib_err(dev, "query_hca_vport_context for port=%d failed %d\n",
2708 port, err);
2709 return err;
2710 }
2711 dev->mdev->port_caps[port - 1].has_smi =
2712 vport_ctx.has_smi;
2713 } else {
2714 dev->mdev->port_caps[port - 1].has_smi = true;
2715 }
2716 }
2717 }
2718 return 0;
2719}
2720
Eli Cohene126ba92013-07-07 17:25:49 +03002721static void get_ext_port_caps(struct mlx5_ib_dev *dev)
2722{
2723 int port;
2724
Saeed Mahameed938fe832015-05-28 22:28:41 +03002725 for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++)
Eli Cohene126ba92013-07-07 17:25:49 +03002726 mlx5_query_ext_port_caps(dev, port);
2727}
2728
2729static int get_port_caps(struct mlx5_ib_dev *dev)
2730{
2731 struct ib_device_attr *dprops = NULL;
2732 struct ib_port_attr *pprops = NULL;
Dan Carpenterf614fc12015-01-12 11:56:58 +03002733 int err = -ENOMEM;
Eli Cohene126ba92013-07-07 17:25:49 +03002734 int port;
Matan Barak2528e332015-06-11 16:35:25 +03002735 struct ib_udata uhw = {.inlen = 0, .outlen = 0};
Eli Cohene126ba92013-07-07 17:25:49 +03002736
2737 pprops = kmalloc(sizeof(*pprops), GFP_KERNEL);
2738 if (!pprops)
2739 goto out;
2740
2741 dprops = kmalloc(sizeof(*dprops), GFP_KERNEL);
2742 if (!dprops)
2743 goto out;
2744
Maor Gottliebc43f1112017-01-18 14:10:33 +02002745 err = set_has_smi_cap(dev);
2746 if (err)
2747 goto out;
2748
Matan Barak2528e332015-06-11 16:35:25 +03002749 err = mlx5_ib_query_device(&dev->ib_dev, dprops, &uhw);
Eli Cohene126ba92013-07-07 17:25:49 +03002750 if (err) {
2751 mlx5_ib_warn(dev, "query_device failed %d\n", err);
2752 goto out;
2753 }
2754
Saeed Mahameed938fe832015-05-28 22:28:41 +03002755 for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++) {
Or Gerlitzc4550c62017-01-24 13:02:39 +02002756 memset(pprops, 0, sizeof(*pprops));
Eli Cohene126ba92013-07-07 17:25:49 +03002757 err = mlx5_ib_query_port(&dev->ib_dev, port, pprops);
2758 if (err) {
Saeed Mahameed938fe832015-05-28 22:28:41 +03002759 mlx5_ib_warn(dev, "query_port %d failed %d\n",
2760 port, err);
Eli Cohene126ba92013-07-07 17:25:49 +03002761 break;
2762 }
Saeed Mahameed938fe832015-05-28 22:28:41 +03002763 dev->mdev->port_caps[port - 1].pkey_table_len =
2764 dprops->max_pkeys;
2765 dev->mdev->port_caps[port - 1].gid_table_len =
2766 pprops->gid_tbl_len;
Eli Cohene126ba92013-07-07 17:25:49 +03002767 mlx5_ib_dbg(dev, "pkey_table_len %d, gid_table_len %d\n",
2768 dprops->max_pkeys, pprops->gid_tbl_len);
2769 }
2770
2771out:
2772 kfree(pprops);
2773 kfree(dprops);
2774
2775 return err;
2776}
2777
2778static void destroy_umrc_res(struct mlx5_ib_dev *dev)
2779{
2780 int err;
2781
2782 err = mlx5_mr_cache_cleanup(dev);
2783 if (err)
2784 mlx5_ib_warn(dev, "mr cache cleanup failed\n");
2785
2786 mlx5_ib_destroy_qp(dev->umrc.qp);
Christoph Hellwigadd08d72016-03-03 09:38:22 +01002787 ib_free_cq(dev->umrc.cq);
Eli Cohene126ba92013-07-07 17:25:49 +03002788 ib_dealloc_pd(dev->umrc.pd);
2789}
2790
2791enum {
2792 MAX_UMR_WR = 128,
2793};
2794
2795static int create_umr_res(struct mlx5_ib_dev *dev)
2796{
2797 struct ib_qp_init_attr *init_attr = NULL;
2798 struct ib_qp_attr *attr = NULL;
2799 struct ib_pd *pd;
2800 struct ib_cq *cq;
2801 struct ib_qp *qp;
Eli Cohene126ba92013-07-07 17:25:49 +03002802 int ret;
2803
2804 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
2805 init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL);
2806 if (!attr || !init_attr) {
2807 ret = -ENOMEM;
2808 goto error_0;
2809 }
2810
Christoph Hellwiged082d32016-09-05 12:56:17 +02002811 pd = ib_alloc_pd(&dev->ib_dev, 0);
Eli Cohene126ba92013-07-07 17:25:49 +03002812 if (IS_ERR(pd)) {
2813 mlx5_ib_dbg(dev, "Couldn't create PD for sync UMR QP\n");
2814 ret = PTR_ERR(pd);
2815 goto error_0;
2816 }
2817
Christoph Hellwigadd08d72016-03-03 09:38:22 +01002818 cq = ib_alloc_cq(&dev->ib_dev, NULL, 128, 0, IB_POLL_SOFTIRQ);
Eli Cohene126ba92013-07-07 17:25:49 +03002819 if (IS_ERR(cq)) {
2820 mlx5_ib_dbg(dev, "Couldn't create CQ for sync UMR QP\n");
2821 ret = PTR_ERR(cq);
2822 goto error_2;
2823 }
Eli Cohene126ba92013-07-07 17:25:49 +03002824
2825 init_attr->send_cq = cq;
2826 init_attr->recv_cq = cq;
2827 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
2828 init_attr->cap.max_send_wr = MAX_UMR_WR;
2829 init_attr->cap.max_send_sge = 1;
2830 init_attr->qp_type = MLX5_IB_QPT_REG_UMR;
2831 init_attr->port_num = 1;
2832 qp = mlx5_ib_create_qp(pd, init_attr, NULL);
2833 if (IS_ERR(qp)) {
2834 mlx5_ib_dbg(dev, "Couldn't create sync UMR QP\n");
2835 ret = PTR_ERR(qp);
2836 goto error_3;
2837 }
2838 qp->device = &dev->ib_dev;
2839 qp->real_qp = qp;
2840 qp->uobject = NULL;
2841 qp->qp_type = MLX5_IB_QPT_REG_UMR;
2842
2843 attr->qp_state = IB_QPS_INIT;
2844 attr->port_num = 1;
2845 ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_PKEY_INDEX |
2846 IB_QP_PORT, NULL);
2847 if (ret) {
2848 mlx5_ib_dbg(dev, "Couldn't modify UMR QP\n");
2849 goto error_4;
2850 }
2851
2852 memset(attr, 0, sizeof(*attr));
2853 attr->qp_state = IB_QPS_RTR;
2854 attr->path_mtu = IB_MTU_256;
2855
2856 ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
2857 if (ret) {
2858 mlx5_ib_dbg(dev, "Couldn't modify umr QP to rtr\n");
2859 goto error_4;
2860 }
2861
2862 memset(attr, 0, sizeof(*attr));
2863 attr->qp_state = IB_QPS_RTS;
2864 ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
2865 if (ret) {
2866 mlx5_ib_dbg(dev, "Couldn't modify umr QP to rts\n");
2867 goto error_4;
2868 }
2869
2870 dev->umrc.qp = qp;
2871 dev->umrc.cq = cq;
Eli Cohene126ba92013-07-07 17:25:49 +03002872 dev->umrc.pd = pd;
2873
2874 sema_init(&dev->umrc.sem, MAX_UMR_WR);
2875 ret = mlx5_mr_cache_init(dev);
2876 if (ret) {
2877 mlx5_ib_warn(dev, "mr cache init failed %d\n", ret);
2878 goto error_4;
2879 }
2880
2881 kfree(attr);
2882 kfree(init_attr);
2883
2884 return 0;
2885
2886error_4:
2887 mlx5_ib_destroy_qp(qp);
2888
2889error_3:
Christoph Hellwigadd08d72016-03-03 09:38:22 +01002890 ib_free_cq(cq);
Eli Cohene126ba92013-07-07 17:25:49 +03002891
2892error_2:
Eli Cohene126ba92013-07-07 17:25:49 +03002893 ib_dealloc_pd(pd);
2894
2895error_0:
2896 kfree(attr);
2897 kfree(init_attr);
2898 return ret;
2899}
2900
2901static int create_dev_resources(struct mlx5_ib_resources *devr)
2902{
2903 struct ib_srq_init_attr attr;
2904 struct mlx5_ib_dev *dev;
Matan Barakbcf4c1e2015-06-11 16:35:20 +03002905 struct ib_cq_init_attr cq_attr = {.cqe = 1};
Haggai Eran7722f472016-02-29 15:45:07 +02002906 int port;
Eli Cohene126ba92013-07-07 17:25:49 +03002907 int ret = 0;
2908
2909 dev = container_of(devr, struct mlx5_ib_dev, devr);
2910
Haggai Erand16e91d2016-02-29 15:45:05 +02002911 mutex_init(&devr->mutex);
2912
Eli Cohene126ba92013-07-07 17:25:49 +03002913 devr->p0 = mlx5_ib_alloc_pd(&dev->ib_dev, NULL, NULL);
2914 if (IS_ERR(devr->p0)) {
2915 ret = PTR_ERR(devr->p0);
2916 goto error0;
2917 }
2918 devr->p0->device = &dev->ib_dev;
2919 devr->p0->uobject = NULL;
2920 atomic_set(&devr->p0->usecnt, 0);
2921
Matan Barakbcf4c1e2015-06-11 16:35:20 +03002922 devr->c0 = mlx5_ib_create_cq(&dev->ib_dev, &cq_attr, NULL, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03002923 if (IS_ERR(devr->c0)) {
2924 ret = PTR_ERR(devr->c0);
2925 goto error1;
2926 }
2927 devr->c0->device = &dev->ib_dev;
2928 devr->c0->uobject = NULL;
2929 devr->c0->comp_handler = NULL;
2930 devr->c0->event_handler = NULL;
2931 devr->c0->cq_context = NULL;
2932 atomic_set(&devr->c0->usecnt, 0);
2933
2934 devr->x0 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
2935 if (IS_ERR(devr->x0)) {
2936 ret = PTR_ERR(devr->x0);
2937 goto error2;
2938 }
2939 devr->x0->device = &dev->ib_dev;
2940 devr->x0->inode = NULL;
2941 atomic_set(&devr->x0->usecnt, 0);
2942 mutex_init(&devr->x0->tgt_qp_mutex);
2943 INIT_LIST_HEAD(&devr->x0->tgt_qp_list);
2944
2945 devr->x1 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
2946 if (IS_ERR(devr->x1)) {
2947 ret = PTR_ERR(devr->x1);
2948 goto error3;
2949 }
2950 devr->x1->device = &dev->ib_dev;
2951 devr->x1->inode = NULL;
2952 atomic_set(&devr->x1->usecnt, 0);
2953 mutex_init(&devr->x1->tgt_qp_mutex);
2954 INIT_LIST_HEAD(&devr->x1->tgt_qp_list);
2955
2956 memset(&attr, 0, sizeof(attr));
2957 attr.attr.max_sge = 1;
2958 attr.attr.max_wr = 1;
2959 attr.srq_type = IB_SRQT_XRC;
2960 attr.ext.xrc.cq = devr->c0;
2961 attr.ext.xrc.xrcd = devr->x0;
2962
2963 devr->s0 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
2964 if (IS_ERR(devr->s0)) {
2965 ret = PTR_ERR(devr->s0);
2966 goto error4;
2967 }
2968 devr->s0->device = &dev->ib_dev;
2969 devr->s0->pd = devr->p0;
2970 devr->s0->uobject = NULL;
2971 devr->s0->event_handler = NULL;
2972 devr->s0->srq_context = NULL;
2973 devr->s0->srq_type = IB_SRQT_XRC;
2974 devr->s0->ext.xrc.xrcd = devr->x0;
2975 devr->s0->ext.xrc.cq = devr->c0;
2976 atomic_inc(&devr->s0->ext.xrc.xrcd->usecnt);
2977 atomic_inc(&devr->s0->ext.xrc.cq->usecnt);
2978 atomic_inc(&devr->p0->usecnt);
2979 atomic_set(&devr->s0->usecnt, 0);
2980
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03002981 memset(&attr, 0, sizeof(attr));
2982 attr.attr.max_sge = 1;
2983 attr.attr.max_wr = 1;
2984 attr.srq_type = IB_SRQT_BASIC;
2985 devr->s1 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
2986 if (IS_ERR(devr->s1)) {
2987 ret = PTR_ERR(devr->s1);
2988 goto error5;
2989 }
2990 devr->s1->device = &dev->ib_dev;
2991 devr->s1->pd = devr->p0;
2992 devr->s1->uobject = NULL;
2993 devr->s1->event_handler = NULL;
2994 devr->s1->srq_context = NULL;
2995 devr->s1->srq_type = IB_SRQT_BASIC;
2996 devr->s1->ext.xrc.cq = devr->c0;
2997 atomic_inc(&devr->p0->usecnt);
2998 atomic_set(&devr->s0->usecnt, 0);
2999
Haggai Eran7722f472016-02-29 15:45:07 +02003000 for (port = 0; port < ARRAY_SIZE(devr->ports); ++port) {
3001 INIT_WORK(&devr->ports[port].pkey_change_work,
3002 pkey_change_handler);
3003 devr->ports[port].devr = devr;
3004 }
3005
Eli Cohene126ba92013-07-07 17:25:49 +03003006 return 0;
3007
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03003008error5:
3009 mlx5_ib_destroy_srq(devr->s0);
Eli Cohene126ba92013-07-07 17:25:49 +03003010error4:
3011 mlx5_ib_dealloc_xrcd(devr->x1);
3012error3:
3013 mlx5_ib_dealloc_xrcd(devr->x0);
3014error2:
3015 mlx5_ib_destroy_cq(devr->c0);
3016error1:
3017 mlx5_ib_dealloc_pd(devr->p0);
3018error0:
3019 return ret;
3020}
3021
3022static void destroy_dev_resources(struct mlx5_ib_resources *devr)
3023{
Haggai Eran7722f472016-02-29 15:45:07 +02003024 struct mlx5_ib_dev *dev =
3025 container_of(devr, struct mlx5_ib_dev, devr);
3026 int port;
3027
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03003028 mlx5_ib_destroy_srq(devr->s1);
Eli Cohene126ba92013-07-07 17:25:49 +03003029 mlx5_ib_destroy_srq(devr->s0);
3030 mlx5_ib_dealloc_xrcd(devr->x0);
3031 mlx5_ib_dealloc_xrcd(devr->x1);
3032 mlx5_ib_destroy_cq(devr->c0);
3033 mlx5_ib_dealloc_pd(devr->p0);
Haggai Eran7722f472016-02-29 15:45:07 +02003034
3035 /* Make sure no change P_Key work items are still executing */
3036 for (port = 0; port < dev->num_ports; ++port)
3037 cancel_work_sync(&devr->ports[port].pkey_change_work);
Eli Cohene126ba92013-07-07 17:25:49 +03003038}
3039
Achiad Shochate53505a2015-12-23 18:47:25 +02003040static u32 get_core_cap_flags(struct ib_device *ibdev)
3041{
3042 struct mlx5_ib_dev *dev = to_mdev(ibdev);
3043 enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, 1);
3044 u8 l3_type_cap = MLX5_CAP_ROCE(dev->mdev, l3_type);
3045 u8 roce_version_cap = MLX5_CAP_ROCE(dev->mdev, roce_version);
3046 u32 ret = 0;
3047
3048 if (ll == IB_LINK_LAYER_INFINIBAND)
3049 return RDMA_CORE_PORT_IBA_IB;
3050
Or Gerlitz72cd5712017-01-24 13:02:36 +02003051 ret = RDMA_CORE_PORT_RAW_PACKET;
3052
Achiad Shochate53505a2015-12-23 18:47:25 +02003053 if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV4_CAP))
Or Gerlitz72cd5712017-01-24 13:02:36 +02003054 return ret;
Achiad Shochate53505a2015-12-23 18:47:25 +02003055
3056 if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV6_CAP))
Or Gerlitz72cd5712017-01-24 13:02:36 +02003057 return ret;
Achiad Shochate53505a2015-12-23 18:47:25 +02003058
3059 if (roce_version_cap & MLX5_ROCE_VERSION_1_CAP)
3060 ret |= RDMA_CORE_PORT_IBA_ROCE;
3061
3062 if (roce_version_cap & MLX5_ROCE_VERSION_2_CAP)
3063 ret |= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
3064
3065 return ret;
3066}
3067
Ira Weiny77386132015-05-13 20:02:58 -04003068static int mlx5_port_immutable(struct ib_device *ibdev, u8 port_num,
3069 struct ib_port_immutable *immutable)
3070{
3071 struct ib_port_attr attr;
Or Gerlitzca5b91d2016-11-27 16:51:36 +02003072 struct mlx5_ib_dev *dev = to_mdev(ibdev);
3073 enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, port_num);
Ira Weiny77386132015-05-13 20:02:58 -04003074 int err;
3075
Or Gerlitzc4550c62017-01-24 13:02:39 +02003076 immutable->core_cap_flags = get_core_cap_flags(ibdev);
3077
3078 err = ib_query_port(ibdev, port_num, &attr);
Ira Weiny77386132015-05-13 20:02:58 -04003079 if (err)
3080 return err;
3081
3082 immutable->pkey_tbl_len = attr.pkey_tbl_len;
3083 immutable->gid_tbl_len = attr.gid_tbl_len;
Achiad Shochate53505a2015-12-23 18:47:25 +02003084 immutable->core_cap_flags = get_core_cap_flags(ibdev);
Or Gerlitzca5b91d2016-11-27 16:51:36 +02003085 if ((ll == IB_LINK_LAYER_INFINIBAND) || MLX5_CAP_GEN(dev->mdev, roce))
3086 immutable->max_mad_size = IB_MGMT_MAD_SIZE;
Ira Weiny77386132015-05-13 20:02:58 -04003087
3088 return 0;
3089}
3090
Ira Weinyc7342822016-06-15 02:22:01 -04003091static void get_dev_fw_str(struct ib_device *ibdev, char *str,
3092 size_t str_len)
3093{
3094 struct mlx5_ib_dev *dev =
3095 container_of(ibdev, struct mlx5_ib_dev, ib_dev);
3096 snprintf(str, str_len, "%d.%d.%04d", fw_rev_maj(dev->mdev),
3097 fw_rev_min(dev->mdev), fw_rev_sub(dev->mdev));
3098}
3099
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003100static int mlx5_eth_lag_init(struct mlx5_ib_dev *dev)
Aviv Heller9ef9c642016-09-18 20:48:01 +03003101{
3102 struct mlx5_core_dev *mdev = dev->mdev;
3103 struct mlx5_flow_namespace *ns = mlx5_get_flow_namespace(mdev,
3104 MLX5_FLOW_NAMESPACE_LAG);
3105 struct mlx5_flow_table *ft;
3106 int err;
3107
3108 if (!ns || !mlx5_lag_is_active(mdev))
3109 return 0;
3110
3111 err = mlx5_cmd_create_vport_lag(mdev);
3112 if (err)
3113 return err;
3114
3115 ft = mlx5_create_lag_demux_flow_table(ns, 0, 0);
3116 if (IS_ERR(ft)) {
3117 err = PTR_ERR(ft);
3118 goto err_destroy_vport_lag;
3119 }
3120
3121 dev->flow_db.lag_demux_ft = ft;
3122 return 0;
3123
3124err_destroy_vport_lag:
3125 mlx5_cmd_destroy_vport_lag(mdev);
3126 return err;
3127}
3128
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003129static void mlx5_eth_lag_cleanup(struct mlx5_ib_dev *dev)
Aviv Heller9ef9c642016-09-18 20:48:01 +03003130{
3131 struct mlx5_core_dev *mdev = dev->mdev;
3132
3133 if (dev->flow_db.lag_demux_ft) {
3134 mlx5_destroy_flow_table(dev->flow_db.lag_demux_ft);
3135 dev->flow_db.lag_demux_ft = NULL;
3136
3137 mlx5_cmd_destroy_vport_lag(mdev);
3138 }
3139}
3140
Or Gerlitzd012f5d2016-11-27 16:51:34 +02003141static int mlx5_add_netdev_notifier(struct mlx5_ib_dev *dev)
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003142{
Achiad Shochate53505a2015-12-23 18:47:25 +02003143 int err;
3144
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003145 dev->roce.nb.notifier_call = mlx5_netdev_event;
Achiad Shochate53505a2015-12-23 18:47:25 +02003146 err = register_netdevice_notifier(&dev->roce.nb);
Aviv Heller5ec8c832016-09-18 20:48:00 +03003147 if (err) {
3148 dev->roce.nb.notifier_call = NULL;
Achiad Shochate53505a2015-12-23 18:47:25 +02003149 return err;
Aviv Heller5ec8c832016-09-18 20:48:00 +03003150 }
Achiad Shochate53505a2015-12-23 18:47:25 +02003151
Or Gerlitzd012f5d2016-11-27 16:51:34 +02003152 return 0;
3153}
Achiad Shochate53505a2015-12-23 18:47:25 +02003154
Or Gerlitzd012f5d2016-11-27 16:51:34 +02003155static void mlx5_remove_netdev_notifier(struct mlx5_ib_dev *dev)
Eli Cohene126ba92013-07-07 17:25:49 +03003156{
3157 if (dev->roce.nb.notifier_call) {
3158 unregister_netdevice_notifier(&dev->roce.nb);
3159 dev->roce.nb.notifier_call = NULL;
3160 }
3161}
3162
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003163static int mlx5_enable_eth(struct mlx5_ib_dev *dev)
Eli Cohene126ba92013-07-07 17:25:49 +03003164{
Eli Cohene126ba92013-07-07 17:25:49 +03003165 int err;
3166
Or Gerlitzd012f5d2016-11-27 16:51:34 +02003167 err = mlx5_add_netdev_notifier(dev);
3168 if (err)
Achiad Shochate53505a2015-12-23 18:47:25 +02003169 return err;
Achiad Shochate53505a2015-12-23 18:47:25 +02003170
Or Gerlitzca5b91d2016-11-27 16:51:36 +02003171 if (MLX5_CAP_GEN(dev->mdev, roce)) {
3172 err = mlx5_nic_vport_enable_roce(dev->mdev);
3173 if (err)
3174 goto err_unregister_netdevice_notifier;
3175 }
Achiad Shochate53505a2015-12-23 18:47:25 +02003176
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003177 err = mlx5_eth_lag_init(dev);
Aviv Heller9ef9c642016-09-18 20:48:01 +03003178 if (err)
3179 goto err_disable_roce;
3180
Achiad Shochate53505a2015-12-23 18:47:25 +02003181 return 0;
3182
Aviv Heller9ef9c642016-09-18 20:48:01 +03003183err_disable_roce:
Or Gerlitzca5b91d2016-11-27 16:51:36 +02003184 if (MLX5_CAP_GEN(dev->mdev, roce))
3185 mlx5_nic_vport_disable_roce(dev->mdev);
Aviv Heller9ef9c642016-09-18 20:48:01 +03003186
Achiad Shochate53505a2015-12-23 18:47:25 +02003187err_unregister_netdevice_notifier:
Or Gerlitzd012f5d2016-11-27 16:51:34 +02003188 mlx5_remove_netdev_notifier(dev);
Achiad Shochate53505a2015-12-23 18:47:25 +02003189 return err;
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003190}
3191
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003192static void mlx5_disable_eth(struct mlx5_ib_dev *dev)
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003193{
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003194 mlx5_eth_lag_cleanup(dev);
Or Gerlitzca5b91d2016-11-27 16:51:36 +02003195 if (MLX5_CAP_GEN(dev->mdev, roce))
3196 mlx5_nic_vport_disable_roce(dev->mdev);
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003197}
3198
Kamal Heib7c16f472017-01-18 15:25:09 +02003199struct mlx5_ib_q_counter {
3200 const char *name;
3201 size_t offset;
3202};
3203
3204#define INIT_Q_COUNTER(_name) \
3205 { .name = #_name, .offset = MLX5_BYTE_OFF(query_q_counter_out, _name)}
3206
3207static const struct mlx5_ib_q_counter basic_q_cnts[] = {
3208 INIT_Q_COUNTER(rx_write_requests),
3209 INIT_Q_COUNTER(rx_read_requests),
3210 INIT_Q_COUNTER(rx_atomic_requests),
3211 INIT_Q_COUNTER(out_of_buffer),
3212};
3213
3214static const struct mlx5_ib_q_counter out_of_seq_q_cnts[] = {
3215 INIT_Q_COUNTER(out_of_sequence),
3216};
3217
3218static const struct mlx5_ib_q_counter retrans_q_cnts[] = {
3219 INIT_Q_COUNTER(duplicate_request),
3220 INIT_Q_COUNTER(rnr_nak_retry_err),
3221 INIT_Q_COUNTER(packet_seq_err),
3222 INIT_Q_COUNTER(implied_nak_seq_err),
3223 INIT_Q_COUNTER(local_ack_timeout_err),
3224};
3225
Mark Bloch0837e862016-06-17 15:10:55 +03003226static void mlx5_ib_dealloc_q_counters(struct mlx5_ib_dev *dev)
3227{
3228 unsigned int i;
3229
Kamal Heib7c16f472017-01-18 15:25:09 +02003230 for (i = 0; i < dev->num_ports; i++) {
Mark Bloch0837e862016-06-17 15:10:55 +03003231 mlx5_core_dealloc_q_counter(dev->mdev,
Kamal Heib7c16f472017-01-18 15:25:09 +02003232 dev->port[i].q_cnts.set_id);
3233 kfree(dev->port[i].q_cnts.names);
3234 kfree(dev->port[i].q_cnts.offsets);
3235 }
3236}
3237
3238static int __mlx5_ib_alloc_q_counters(struct mlx5_ib_dev *dev,
3239 const char ***names,
3240 size_t **offsets,
3241 u32 *num)
3242{
3243 u32 num_counters;
3244
3245 num_counters = ARRAY_SIZE(basic_q_cnts);
3246
3247 if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt))
3248 num_counters += ARRAY_SIZE(out_of_seq_q_cnts);
3249
3250 if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters))
3251 num_counters += ARRAY_SIZE(retrans_q_cnts);
3252
3253 *names = kcalloc(num_counters, sizeof(**names), GFP_KERNEL);
3254 if (!*names)
3255 return -ENOMEM;
3256
3257 *offsets = kcalloc(num_counters, sizeof(**offsets), GFP_KERNEL);
3258 if (!*offsets)
3259 goto err_names;
3260
3261 *num = num_counters;
3262
3263 return 0;
3264
3265err_names:
3266 kfree(*names);
3267 return -ENOMEM;
3268}
3269
3270static void mlx5_ib_fill_q_counters(struct mlx5_ib_dev *dev,
3271 const char **names,
3272 size_t *offsets)
3273{
3274 int i;
3275 int j = 0;
3276
3277 for (i = 0; i < ARRAY_SIZE(basic_q_cnts); i++, j++) {
3278 names[j] = basic_q_cnts[i].name;
3279 offsets[j] = basic_q_cnts[i].offset;
3280 }
3281
3282 if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt)) {
3283 for (i = 0; i < ARRAY_SIZE(out_of_seq_q_cnts); i++, j++) {
3284 names[j] = out_of_seq_q_cnts[i].name;
3285 offsets[j] = out_of_seq_q_cnts[i].offset;
3286 }
3287 }
3288
3289 if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters)) {
3290 for (i = 0; i < ARRAY_SIZE(retrans_q_cnts); i++, j++) {
3291 names[j] = retrans_q_cnts[i].name;
3292 offsets[j] = retrans_q_cnts[i].offset;
3293 }
3294 }
Mark Bloch0837e862016-06-17 15:10:55 +03003295}
3296
3297static int mlx5_ib_alloc_q_counters(struct mlx5_ib_dev *dev)
3298{
3299 int i;
3300 int ret;
3301
3302 for (i = 0; i < dev->num_ports; i++) {
Kamal Heib7c16f472017-01-18 15:25:09 +02003303 struct mlx5_ib_port *port = &dev->port[i];
3304
Mark Bloch0837e862016-06-17 15:10:55 +03003305 ret = mlx5_core_alloc_q_counter(dev->mdev,
Kamal Heib7c16f472017-01-18 15:25:09 +02003306 &port->q_cnts.set_id);
Mark Bloch0837e862016-06-17 15:10:55 +03003307 if (ret) {
3308 mlx5_ib_warn(dev,
3309 "couldn't allocate queue counter for port %d, err %d\n",
3310 i + 1, ret);
3311 goto dealloc_counters;
3312 }
Kamal Heib7c16f472017-01-18 15:25:09 +02003313
3314 ret = __mlx5_ib_alloc_q_counters(dev,
3315 &port->q_cnts.names,
3316 &port->q_cnts.offsets,
3317 &port->q_cnts.num_counters);
3318 if (ret)
3319 goto dealloc_counters;
3320
3321 mlx5_ib_fill_q_counters(dev, port->q_cnts.names,
3322 port->q_cnts.offsets);
Mark Bloch0837e862016-06-17 15:10:55 +03003323 }
3324
3325 return 0;
3326
3327dealloc_counters:
3328 while (--i >= 0)
3329 mlx5_core_dealloc_q_counter(dev->mdev,
Kamal Heib7c16f472017-01-18 15:25:09 +02003330 dev->port[i].q_cnts.set_id);
Mark Bloch0837e862016-06-17 15:10:55 +03003331
3332 return ret;
3333}
3334
Mark Bloch0ad17a82016-06-17 15:10:56 +03003335static struct rdma_hw_stats *mlx5_ib_alloc_hw_stats(struct ib_device *ibdev,
3336 u8 port_num)
3337{
Kamal Heib7c16f472017-01-18 15:25:09 +02003338 struct mlx5_ib_dev *dev = to_mdev(ibdev);
3339 struct mlx5_ib_port *port = &dev->port[port_num - 1];
Mark Bloch0ad17a82016-06-17 15:10:56 +03003340
3341 /* We support only per port stats */
3342 if (port_num == 0)
3343 return NULL;
3344
Kamal Heib7c16f472017-01-18 15:25:09 +02003345 return rdma_alloc_hw_stats_struct(port->q_cnts.names,
3346 port->q_cnts.num_counters,
Mark Bloch0ad17a82016-06-17 15:10:56 +03003347 RDMA_HW_STATS_DEFAULT_LIFESPAN);
3348}
3349
3350static int mlx5_ib_get_hw_stats(struct ib_device *ibdev,
3351 struct rdma_hw_stats *stats,
Kamal Heib7c16f472017-01-18 15:25:09 +02003352 u8 port_num, int index)
Mark Bloch0ad17a82016-06-17 15:10:56 +03003353{
3354 struct mlx5_ib_dev *dev = to_mdev(ibdev);
Kamal Heib7c16f472017-01-18 15:25:09 +02003355 struct mlx5_ib_port *port = &dev->port[port_num - 1];
Mark Bloch0ad17a82016-06-17 15:10:56 +03003356 int outlen = MLX5_ST_SZ_BYTES(query_q_counter_out);
3357 void *out;
3358 __be32 val;
3359 int ret;
3360 int i;
3361
Kamal Heib7c16f472017-01-18 15:25:09 +02003362 if (!stats)
Mark Bloch0ad17a82016-06-17 15:10:56 +03003363 return -ENOSYS;
3364
3365 out = mlx5_vzalloc(outlen);
3366 if (!out)
3367 return -ENOMEM;
3368
3369 ret = mlx5_core_query_q_counter(dev->mdev,
Kamal Heib7c16f472017-01-18 15:25:09 +02003370 port->q_cnts.set_id, 0,
Mark Bloch0ad17a82016-06-17 15:10:56 +03003371 out, outlen);
3372 if (ret)
3373 goto free;
3374
Kamal Heib7c16f472017-01-18 15:25:09 +02003375 for (i = 0; i < port->q_cnts.num_counters; i++) {
3376 val = *(__be32 *)(out + port->q_cnts.offsets[i]);
Mark Bloch0ad17a82016-06-17 15:10:56 +03003377 stats->value[i] = (u64)be32_to_cpu(val);
3378 }
Kamal Heib7c16f472017-01-18 15:25:09 +02003379
Mark Bloch0ad17a82016-06-17 15:10:56 +03003380free:
3381 kvfree(out);
Kamal Heib7c16f472017-01-18 15:25:09 +02003382 return port->q_cnts.num_counters;
Mark Bloch0ad17a82016-06-17 15:10:56 +03003383}
3384
Jack Morgenstein9603b612014-07-28 23:30:22 +03003385static void *mlx5_ib_add(struct mlx5_core_dev *mdev)
Eli Cohene126ba92013-07-07 17:25:49 +03003386{
Eli Cohene126ba92013-07-07 17:25:49 +03003387 struct mlx5_ib_dev *dev;
Achiad Shochatebd61f62015-12-23 18:47:16 +02003388 enum rdma_link_layer ll;
3389 int port_type_cap;
Aviv Heller4babcf92016-09-18 20:48:03 +03003390 const char *name;
Eli Cohene126ba92013-07-07 17:25:49 +03003391 int err;
3392 int i;
3393
Achiad Shochatebd61f62015-12-23 18:47:16 +02003394 port_type_cap = MLX5_CAP_GEN(mdev, port_type);
3395 ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
3396
Eli Cohene126ba92013-07-07 17:25:49 +03003397 printk_once(KERN_INFO "%s", mlx5_version);
3398
3399 dev = (struct mlx5_ib_dev *)ib_alloc_device(sizeof(*dev));
3400 if (!dev)
Jack Morgenstein9603b612014-07-28 23:30:22 +03003401 return NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03003402
Jack Morgenstein9603b612014-07-28 23:30:22 +03003403 dev->mdev = mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03003404
Mark Bloch0837e862016-06-17 15:10:55 +03003405 dev->port = kcalloc(MLX5_CAP_GEN(mdev, num_ports), sizeof(*dev->port),
3406 GFP_KERNEL);
3407 if (!dev->port)
3408 goto err_dealloc;
3409
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003410 rwlock_init(&dev->roce.netdev_lock);
Eli Cohene126ba92013-07-07 17:25:49 +03003411 err = get_port_caps(dev);
3412 if (err)
Mark Bloch0837e862016-06-17 15:10:55 +03003413 goto err_free_port;
Eli Cohene126ba92013-07-07 17:25:49 +03003414
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03003415 if (mlx5_use_mad_ifc(dev))
3416 get_ext_port_caps(dev);
Eli Cohene126ba92013-07-07 17:25:49 +03003417
Aviv Heller4babcf92016-09-18 20:48:03 +03003418 if (!mlx5_lag_is_active(mdev))
3419 name = "mlx5_%d";
3420 else
3421 name = "mlx5_bond_%d";
3422
3423 strlcpy(dev->ib_dev.name, name, IB_DEVICE_NAME_MAX);
Eli Cohene126ba92013-07-07 17:25:49 +03003424 dev->ib_dev.owner = THIS_MODULE;
3425 dev->ib_dev.node_type = RDMA_NODE_IB_CA;
Sagi Grimbergc6790aa2015-09-24 10:34:23 +03003426 dev->ib_dev.local_dma_lkey = 0 /* not supported for now */;
Saeed Mahameed938fe832015-05-28 22:28:41 +03003427 dev->num_ports = MLX5_CAP_GEN(mdev, num_ports);
Eli Cohene126ba92013-07-07 17:25:49 +03003428 dev->ib_dev.phys_port_cnt = dev->num_ports;
Saeed Mahameed233d05d2015-04-02 17:07:32 +03003429 dev->ib_dev.num_comp_vectors =
3430 dev->mdev->priv.eq_table.num_comp_vectors;
Bart Van Assche9b0c2892017-01-20 13:04:21 -08003431 dev->ib_dev.dev.parent = &mdev->pdev->dev;
Eli Cohene126ba92013-07-07 17:25:49 +03003432
3433 dev->ib_dev.uverbs_abi_ver = MLX5_IB_UVERBS_ABI_VERSION;
3434 dev->ib_dev.uverbs_cmd_mask =
3435 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
3436 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
3437 (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
3438 (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
3439 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
Moni Shoua41c450f2016-11-23 08:23:26 +02003440 (1ull << IB_USER_VERBS_CMD_CREATE_AH) |
3441 (1ull << IB_USER_VERBS_CMD_DESTROY_AH) |
Eli Cohene126ba92013-07-07 17:25:49 +03003442 (1ull << IB_USER_VERBS_CMD_REG_MR) |
Noa Osherovich56e11d62016-02-29 16:46:51 +02003443 (1ull << IB_USER_VERBS_CMD_REREG_MR) |
Eli Cohene126ba92013-07-07 17:25:49 +03003444 (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
3445 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
3446 (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
3447 (1ull << IB_USER_VERBS_CMD_RESIZE_CQ) |
3448 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
3449 (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
3450 (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
3451 (1ull << IB_USER_VERBS_CMD_QUERY_QP) |
3452 (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
3453 (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST) |
3454 (1ull << IB_USER_VERBS_CMD_DETACH_MCAST) |
3455 (1ull << IB_USER_VERBS_CMD_CREATE_SRQ) |
3456 (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ) |
3457 (1ull << IB_USER_VERBS_CMD_QUERY_SRQ) |
3458 (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ) |
3459 (1ull << IB_USER_VERBS_CMD_CREATE_XSRQ) |
3460 (1ull << IB_USER_VERBS_CMD_OPEN_QP);
Haggai Eran1707cb42015-02-08 13:28:52 +02003461 dev->ib_dev.uverbs_ex_cmd_mask =
Matan Barakd4584dd2016-01-28 17:51:46 +02003462 (1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE) |
3463 (1ull << IB_USER_VERBS_EX_CMD_CREATE_CQ) |
Bodong Wang7d29f342016-12-01 13:43:16 +02003464 (1ull << IB_USER_VERBS_EX_CMD_CREATE_QP) |
3465 (1ull << IB_USER_VERBS_EX_CMD_MODIFY_QP);
Eli Cohene126ba92013-07-07 17:25:49 +03003466
3467 dev->ib_dev.query_device = mlx5_ib_query_device;
3468 dev->ib_dev.query_port = mlx5_ib_query_port;
Achiad Shochatebd61f62015-12-23 18:47:16 +02003469 dev->ib_dev.get_link_layer = mlx5_ib_port_link_layer;
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003470 if (ll == IB_LINK_LAYER_ETHERNET)
3471 dev->ib_dev.get_netdev = mlx5_ib_get_netdev;
Eli Cohene126ba92013-07-07 17:25:49 +03003472 dev->ib_dev.query_gid = mlx5_ib_query_gid;
Achiad Shochat3cca2602015-12-23 18:47:23 +02003473 dev->ib_dev.add_gid = mlx5_ib_add_gid;
3474 dev->ib_dev.del_gid = mlx5_ib_del_gid;
Eli Cohene126ba92013-07-07 17:25:49 +03003475 dev->ib_dev.query_pkey = mlx5_ib_query_pkey;
3476 dev->ib_dev.modify_device = mlx5_ib_modify_device;
3477 dev->ib_dev.modify_port = mlx5_ib_modify_port;
3478 dev->ib_dev.alloc_ucontext = mlx5_ib_alloc_ucontext;
3479 dev->ib_dev.dealloc_ucontext = mlx5_ib_dealloc_ucontext;
3480 dev->ib_dev.mmap = mlx5_ib_mmap;
3481 dev->ib_dev.alloc_pd = mlx5_ib_alloc_pd;
3482 dev->ib_dev.dealloc_pd = mlx5_ib_dealloc_pd;
3483 dev->ib_dev.create_ah = mlx5_ib_create_ah;
3484 dev->ib_dev.query_ah = mlx5_ib_query_ah;
3485 dev->ib_dev.destroy_ah = mlx5_ib_destroy_ah;
3486 dev->ib_dev.create_srq = mlx5_ib_create_srq;
3487 dev->ib_dev.modify_srq = mlx5_ib_modify_srq;
3488 dev->ib_dev.query_srq = mlx5_ib_query_srq;
3489 dev->ib_dev.destroy_srq = mlx5_ib_destroy_srq;
3490 dev->ib_dev.post_srq_recv = mlx5_ib_post_srq_recv;
3491 dev->ib_dev.create_qp = mlx5_ib_create_qp;
3492 dev->ib_dev.modify_qp = mlx5_ib_modify_qp;
3493 dev->ib_dev.query_qp = mlx5_ib_query_qp;
3494 dev->ib_dev.destroy_qp = mlx5_ib_destroy_qp;
3495 dev->ib_dev.post_send = mlx5_ib_post_send;
3496 dev->ib_dev.post_recv = mlx5_ib_post_recv;
3497 dev->ib_dev.create_cq = mlx5_ib_create_cq;
3498 dev->ib_dev.modify_cq = mlx5_ib_modify_cq;
3499 dev->ib_dev.resize_cq = mlx5_ib_resize_cq;
3500 dev->ib_dev.destroy_cq = mlx5_ib_destroy_cq;
3501 dev->ib_dev.poll_cq = mlx5_ib_poll_cq;
3502 dev->ib_dev.req_notify_cq = mlx5_ib_arm_cq;
3503 dev->ib_dev.get_dma_mr = mlx5_ib_get_dma_mr;
3504 dev->ib_dev.reg_user_mr = mlx5_ib_reg_user_mr;
Noa Osherovich56e11d62016-02-29 16:46:51 +02003505 dev->ib_dev.rereg_user_mr = mlx5_ib_rereg_user_mr;
Eli Cohene126ba92013-07-07 17:25:49 +03003506 dev->ib_dev.dereg_mr = mlx5_ib_dereg_mr;
3507 dev->ib_dev.attach_mcast = mlx5_ib_mcg_attach;
3508 dev->ib_dev.detach_mcast = mlx5_ib_mcg_detach;
3509 dev->ib_dev.process_mad = mlx5_ib_process_mad;
Sagi Grimberg9bee1782015-07-30 10:32:35 +03003510 dev->ib_dev.alloc_mr = mlx5_ib_alloc_mr;
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003511 dev->ib_dev.map_mr_sg = mlx5_ib_map_mr_sg;
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02003512 dev->ib_dev.check_mr_status = mlx5_ib_check_mr_status;
Ira Weiny77386132015-05-13 20:02:58 -04003513 dev->ib_dev.get_port_immutable = mlx5_port_immutable;
Ira Weinyc7342822016-06-15 02:22:01 -04003514 dev->ib_dev.get_dev_fw_str = get_dev_fw_str;
Eli Coheneff901d2016-03-11 22:58:42 +02003515 if (mlx5_core_is_pf(mdev)) {
3516 dev->ib_dev.get_vf_config = mlx5_ib_get_vf_config;
3517 dev->ib_dev.set_vf_link_state = mlx5_ib_set_vf_link_state;
3518 dev->ib_dev.get_vf_stats = mlx5_ib_get_vf_stats;
3519 dev->ib_dev.set_vf_guid = mlx5_ib_set_vf_guid;
3520 }
Eli Cohene126ba92013-07-07 17:25:49 +03003521
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03003522 dev->ib_dev.disassociate_ucontext = mlx5_ib_disassociate_ucontext;
3523
Saeed Mahameed938fe832015-05-28 22:28:41 +03003524 mlx5_ib_internal_fill_odp_caps(dev);
Haggai Eran8cdd3122014-12-11 17:04:20 +02003525
Matan Barakd2370e02016-02-29 18:05:30 +02003526 if (MLX5_CAP_GEN(mdev, imaicl)) {
3527 dev->ib_dev.alloc_mw = mlx5_ib_alloc_mw;
3528 dev->ib_dev.dealloc_mw = mlx5_ib_dealloc_mw;
3529 dev->ib_dev.uverbs_cmd_mask |=
3530 (1ull << IB_USER_VERBS_CMD_ALLOC_MW) |
3531 (1ull << IB_USER_VERBS_CMD_DEALLOC_MW);
3532 }
3533
Kamal Heib7c16f472017-01-18 15:25:09 +02003534 if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt)) {
Mark Bloch0ad17a82016-06-17 15:10:56 +03003535 dev->ib_dev.get_hw_stats = mlx5_ib_get_hw_stats;
3536 dev->ib_dev.alloc_hw_stats = mlx5_ib_alloc_hw_stats;
3537 }
3538
Saeed Mahameed938fe832015-05-28 22:28:41 +03003539 if (MLX5_CAP_GEN(mdev, xrc)) {
Eli Cohene126ba92013-07-07 17:25:49 +03003540 dev->ib_dev.alloc_xrcd = mlx5_ib_alloc_xrcd;
3541 dev->ib_dev.dealloc_xrcd = mlx5_ib_dealloc_xrcd;
3542 dev->ib_dev.uverbs_cmd_mask |=
3543 (1ull << IB_USER_VERBS_CMD_OPEN_XRCD) |
3544 (1ull << IB_USER_VERBS_CMD_CLOSE_XRCD);
3545 }
3546
Linus Torvalds048ccca2016-01-23 18:45:06 -08003547 if (mlx5_ib_port_link_layer(&dev->ib_dev, 1) ==
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003548 IB_LINK_LAYER_ETHERNET) {
3549 dev->ib_dev.create_flow = mlx5_ib_create_flow;
3550 dev->ib_dev.destroy_flow = mlx5_ib_destroy_flow;
Yishai Hadas79b20a62016-05-23 15:20:50 +03003551 dev->ib_dev.create_wq = mlx5_ib_create_wq;
3552 dev->ib_dev.modify_wq = mlx5_ib_modify_wq;
3553 dev->ib_dev.destroy_wq = mlx5_ib_destroy_wq;
Yishai Hadasc5f90922016-05-23 15:20:53 +03003554 dev->ib_dev.create_rwq_ind_table = mlx5_ib_create_rwq_ind_table;
3555 dev->ib_dev.destroy_rwq_ind_table = mlx5_ib_destroy_rwq_ind_table;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003556 dev->ib_dev.uverbs_ex_cmd_mask |=
3557 (1ull << IB_USER_VERBS_EX_CMD_CREATE_FLOW) |
Yishai Hadas79b20a62016-05-23 15:20:50 +03003558 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_FLOW) |
3559 (1ull << IB_USER_VERBS_EX_CMD_CREATE_WQ) |
3560 (1ull << IB_USER_VERBS_EX_CMD_MODIFY_WQ) |
Yishai Hadasc5f90922016-05-23 15:20:53 +03003561 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_WQ) |
3562 (1ull << IB_USER_VERBS_EX_CMD_CREATE_RWQ_IND_TBL) |
3563 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_RWQ_IND_TBL);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003564 }
Eli Cohene126ba92013-07-07 17:25:49 +03003565 err = init_node_data(dev);
3566 if (err)
Majd Dibbiny90be7c82016-10-27 16:36:39 +03003567 goto err_free_port;
Eli Cohene126ba92013-07-07 17:25:49 +03003568
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003569 mutex_init(&dev->flow_db.lock);
Eli Cohene126ba92013-07-07 17:25:49 +03003570 mutex_init(&dev->cap_mask_mutex);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03003571 INIT_LIST_HEAD(&dev->qp_list);
3572 spin_lock_init(&dev->reset_flow_resource_lock);
Eli Cohene126ba92013-07-07 17:25:49 +03003573
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003574 if (ll == IB_LINK_LAYER_ETHERNET) {
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003575 err = mlx5_enable_eth(dev);
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003576 if (err)
Majd Dibbiny90be7c82016-10-27 16:36:39 +03003577 goto err_free_port;
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003578 }
3579
Eli Cohene126ba92013-07-07 17:25:49 +03003580 err = create_dev_resources(&dev->devr);
3581 if (err)
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003582 goto err_disable_eth;
Eli Cohene126ba92013-07-07 17:25:49 +03003583
Haggai Eran6aec21f2014-12-11 17:04:23 +02003584 err = mlx5_ib_odp_init_one(dev);
Wei Yongjun281d1a92013-07-30 07:54:26 +08003585 if (err)
Eli Cohene126ba92013-07-07 17:25:49 +03003586 goto err_rsrc;
3587
Kamal Heib45bded22017-01-18 14:10:32 +02003588 if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt)) {
3589 err = mlx5_ib_alloc_q_counters(dev);
3590 if (err)
3591 goto err_odp;
3592 }
Haggai Eran6aec21f2014-12-11 17:04:23 +02003593
Eli Cohen5fe9dec2017-01-03 23:55:25 +02003594 dev->mdev->priv.uar = mlx5_get_uars_page(dev->mdev);
3595 if (!dev->mdev->priv.uar)
3596 goto err_q_cnt;
3597
3598 err = mlx5_alloc_bfreg(dev->mdev, &dev->bfreg, false, false);
3599 if (err)
3600 goto err_uar_page;
3601
3602 err = mlx5_alloc_bfreg(dev->mdev, &dev->fp_bfreg, false, true);
3603 if (err)
3604 goto err_bfreg;
3605
Mark Bloch0837e862016-06-17 15:10:55 +03003606 err = ib_register_device(&dev->ib_dev, NULL);
3607 if (err)
Eli Cohen5fe9dec2017-01-03 23:55:25 +02003608 goto err_fp_bfreg;
Mark Bloch0837e862016-06-17 15:10:55 +03003609
Eli Cohene126ba92013-07-07 17:25:49 +03003610 err = create_umr_res(dev);
3611 if (err)
3612 goto err_dev;
3613
3614 for (i = 0; i < ARRAY_SIZE(mlx5_class_attributes); i++) {
Wei Yongjun281d1a92013-07-30 07:54:26 +08003615 err = device_create_file(&dev->ib_dev.dev,
3616 mlx5_class_attributes[i]);
3617 if (err)
Eli Cohene126ba92013-07-07 17:25:49 +03003618 goto err_umrc;
3619 }
3620
3621 dev->ib_active = true;
3622
Jack Morgenstein9603b612014-07-28 23:30:22 +03003623 return dev;
Eli Cohene126ba92013-07-07 17:25:49 +03003624
3625err_umrc:
3626 destroy_umrc_res(dev);
3627
3628err_dev:
3629 ib_unregister_device(&dev->ib_dev);
3630
Eli Cohen5fe9dec2017-01-03 23:55:25 +02003631err_fp_bfreg:
3632 mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
3633
3634err_bfreg:
3635 mlx5_free_bfreg(dev->mdev, &dev->bfreg);
3636
3637err_uar_page:
3638 mlx5_put_uars_page(dev->mdev, dev->mdev->priv.uar);
3639
Mark Bloch0837e862016-06-17 15:10:55 +03003640err_q_cnt:
Kamal Heib45bded22017-01-18 14:10:32 +02003641 if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt))
3642 mlx5_ib_dealloc_q_counters(dev);
Mark Bloch0837e862016-06-17 15:10:55 +03003643
Haggai Eran6aec21f2014-12-11 17:04:23 +02003644err_odp:
3645 mlx5_ib_odp_remove_one(dev);
3646
Eli Cohene126ba92013-07-07 17:25:49 +03003647err_rsrc:
3648 destroy_dev_resources(&dev->devr);
3649
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003650err_disable_eth:
Aviv Heller5ec8c832016-09-18 20:48:00 +03003651 if (ll == IB_LINK_LAYER_ETHERNET) {
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003652 mlx5_disable_eth(dev);
Or Gerlitzd012f5d2016-11-27 16:51:34 +02003653 mlx5_remove_netdev_notifier(dev);
Aviv Heller5ec8c832016-09-18 20:48:00 +03003654 }
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003655
Mark Bloch0837e862016-06-17 15:10:55 +03003656err_free_port:
3657 kfree(dev->port);
3658
Jack Morgenstein9603b612014-07-28 23:30:22 +03003659err_dealloc:
Eli Cohene126ba92013-07-07 17:25:49 +03003660 ib_dealloc_device((struct ib_device *)dev);
3661
Jack Morgenstein9603b612014-07-28 23:30:22 +03003662 return NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03003663}
3664
Jack Morgenstein9603b612014-07-28 23:30:22 +03003665static void mlx5_ib_remove(struct mlx5_core_dev *mdev, void *context)
Eli Cohene126ba92013-07-07 17:25:49 +03003666{
Jack Morgenstein9603b612014-07-28 23:30:22 +03003667 struct mlx5_ib_dev *dev = context;
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003668 enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev, 1);
Haggai Eran6aec21f2014-12-11 17:04:23 +02003669
Or Gerlitzd012f5d2016-11-27 16:51:34 +02003670 mlx5_remove_netdev_notifier(dev);
Eli Cohene126ba92013-07-07 17:25:49 +03003671 ib_unregister_device(&dev->ib_dev);
Eli Cohen5fe9dec2017-01-03 23:55:25 +02003672 mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
3673 mlx5_free_bfreg(dev->mdev, &dev->bfreg);
3674 mlx5_put_uars_page(dev->mdev, mdev->priv.uar);
Kamal Heib45bded22017-01-18 14:10:32 +02003675 if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt))
3676 mlx5_ib_dealloc_q_counters(dev);
Eli Coheneefd56e2014-09-14 16:47:50 +03003677 destroy_umrc_res(dev);
Haggai Eran6aec21f2014-12-11 17:04:23 +02003678 mlx5_ib_odp_remove_one(dev);
Eli Cohene126ba92013-07-07 17:25:49 +03003679 destroy_dev_resources(&dev->devr);
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003680 if (ll == IB_LINK_LAYER_ETHERNET)
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003681 mlx5_disable_eth(dev);
Mark Bloch0837e862016-06-17 15:10:55 +03003682 kfree(dev->port);
Eli Cohene126ba92013-07-07 17:25:49 +03003683 ib_dealloc_device(&dev->ib_dev);
3684}
3685
Jack Morgenstein9603b612014-07-28 23:30:22 +03003686static struct mlx5_interface mlx5_ib_interface = {
3687 .add = mlx5_ib_add,
3688 .remove = mlx5_ib_remove,
3689 .event = mlx5_ib_event,
Artemy Kovalyovd9aaed82017-01-02 11:37:46 +02003690#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
3691 .pfault = mlx5_ib_pfault,
3692#endif
Saeed Mahameed64613d942015-04-02 17:07:34 +03003693 .protocol = MLX5_INTERFACE_PROTOCOL_IB,
Eli Cohene126ba92013-07-07 17:25:49 +03003694};
3695
3696static int __init mlx5_ib_init(void)
3697{
Haggai Eran6aec21f2014-12-11 17:04:23 +02003698 int err;
3699
Artemy Kovalyov81713d32017-01-18 16:58:11 +02003700 mlx5_ib_odp_init();
Jack Morgenstein9603b612014-07-28 23:30:22 +03003701
Haggai Eran6aec21f2014-12-11 17:04:23 +02003702 err = mlx5_register_interface(&mlx5_ib_interface);
Haggai Eran6aec21f2014-12-11 17:04:23 +02003703
3704 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03003705}
3706
3707static void __exit mlx5_ib_cleanup(void)
3708{
Jack Morgenstein9603b612014-07-28 23:30:22 +03003709 mlx5_unregister_interface(&mlx5_ib_interface);
Eli Cohene126ba92013-07-07 17:25:49 +03003710}
3711
3712module_init(mlx5_ib_init);
3713module_exit(mlx5_ib_cleanup);