blob: 6a81f0273f4545d0f2d5c9918f83014a95ea9aaf [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>
Maor Gottlieb7c2344c2016-06-17 14:56:44 +030044#include <linux/delay.h>
Eli Cohene126ba92013-07-07 17:25:49 +030045#include <rdma/ib_user_verbs.h>
Achiad Shochat3f89a642015-12-23 18:47:21 +020046#include <rdma/ib_addr.h>
Achiad Shochat2811ba52015-12-23 18:47:24 +020047#include <rdma/ib_cache.h>
Achiad Shochatada68c32016-02-22 18:17:23 +020048#include <linux/mlx5/port.h>
Majd Dibbiny1b5daf12015-06-04 19:30:46 +030049#include <linux/mlx5/vport.h>
Maor Gottlieb7c2344c2016-06-17 14:56:44 +030050#include <linux/list.h>
Eli Cohene126ba92013-07-07 17:25:49 +030051#include <rdma/ib_smi.h>
52#include <rdma/ib_umem.h>
Maor Gottlieb038d2ef2016-01-11 10:26:07 +020053#include <linux/in.h>
54#include <linux/etherdevice.h>
55#include <linux/mlx5/fs.h>
Eli Cohene126ba92013-07-07 17:25:49 +030056#include "mlx5_ib.h"
57
58#define DRIVER_NAME "mlx5_ib"
Amir Vadai169a1d82014-02-19 17:47:31 +020059#define DRIVER_VERSION "2.2-1"
60#define DRIVER_RELDATE "Feb 2014"
Eli Cohene126ba92013-07-07 17:25:49 +030061
62MODULE_AUTHOR("Eli Cohen <eli@mellanox.com>");
63MODULE_DESCRIPTION("Mellanox Connect-IB HCA IB driver");
64MODULE_LICENSE("Dual BSD/GPL");
65MODULE_VERSION(DRIVER_VERSION);
66
Jack Morgenstein9603b612014-07-28 23:30:22 +030067static int deprecated_prof_sel = 2;
68module_param_named(prof_sel, deprecated_prof_sel, int, 0444);
69MODULE_PARM_DESC(prof_sel, "profile selector. Deprecated here. Moved to module mlx5_core");
Eli Cohene126ba92013-07-07 17:25:49 +030070
71static char mlx5_version[] =
72 DRIVER_NAME ": Mellanox Connect-IB Infiniband driver v"
73 DRIVER_VERSION " (" DRIVER_RELDATE ")\n";
74
Eran Ben Elishada7525d2015-12-14 16:34:10 +020075enum {
76 MLX5_ATOMIC_SIZE_QP_8BYTES = 1 << 3,
77};
Majd Dibbiny1b5daf12015-06-04 19:30:46 +030078
Majd Dibbiny1b5daf12015-06-04 19:30:46 +030079static enum rdma_link_layer
Achiad Shochatebd61f62015-12-23 18:47:16 +020080mlx5_port_type_cap_to_rdma_ll(int port_type_cap)
Majd Dibbiny1b5daf12015-06-04 19:30:46 +030081{
Achiad Shochatebd61f62015-12-23 18:47:16 +020082 switch (port_type_cap) {
Majd Dibbiny1b5daf12015-06-04 19:30:46 +030083 case MLX5_CAP_PORT_TYPE_IB:
84 return IB_LINK_LAYER_INFINIBAND;
85 case MLX5_CAP_PORT_TYPE_ETH:
86 return IB_LINK_LAYER_ETHERNET;
87 default:
88 return IB_LINK_LAYER_UNSPECIFIED;
89 }
90}
91
Achiad Shochatebd61f62015-12-23 18:47:16 +020092static enum rdma_link_layer
93mlx5_ib_port_link_layer(struct ib_device *device, u8 port_num)
94{
95 struct mlx5_ib_dev *dev = to_mdev(device);
96 int port_type_cap = MLX5_CAP_GEN(dev->mdev, port_type);
97
98 return mlx5_port_type_cap_to_rdma_ll(port_type_cap);
99}
100
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200101static int mlx5_netdev_event(struct notifier_block *this,
102 unsigned long event, void *ptr)
103{
104 struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
105 struct mlx5_ib_dev *ibdev = container_of(this, struct mlx5_ib_dev,
106 roce.nb);
107
Aviv Heller5ec8c832016-09-18 20:48:00 +0300108 switch (event) {
109 case NETDEV_REGISTER:
110 case NETDEV_UNREGISTER:
111 write_lock(&ibdev->roce.netdev_lock);
112 if (ndev->dev.parent == &ibdev->mdev->pdev->dev)
113 ibdev->roce.netdev = (event == NETDEV_UNREGISTER) ?
114 NULL : ndev;
115 write_unlock(&ibdev->roce.netdev_lock);
116 break;
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200117
Aviv Heller5ec8c832016-09-18 20:48:00 +0300118 case NETDEV_UP:
Aviv Heller88621df2016-09-18 20:48:02 +0300119 case NETDEV_DOWN: {
120 struct net_device *lag_ndev = mlx5_lag_get_roce_netdev(ibdev->mdev);
121 struct net_device *upper = NULL;
122
123 if (lag_ndev) {
124 upper = netdev_master_upper_dev_get(lag_ndev);
125 dev_put(lag_ndev);
126 }
127
128 if ((upper == ndev || (!upper && ndev == ibdev->roce.netdev))
129 && ibdev->ib_active) {
Bart Van Assche626bc022016-12-05 17:18:08 -0800130 struct ib_event ibev = { };
Aviv Heller5ec8c832016-09-18 20:48:00 +0300131
132 ibev.device = &ibdev->ib_dev;
133 ibev.event = (event == NETDEV_UP) ?
134 IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
135 ibev.element.port_num = 1;
136 ib_dispatch_event(&ibev);
137 }
138 break;
Aviv Heller88621df2016-09-18 20:48:02 +0300139 }
Aviv Heller5ec8c832016-09-18 20:48:00 +0300140
141 default:
142 break;
143 }
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200144
145 return NOTIFY_DONE;
146}
147
148static struct net_device *mlx5_ib_get_netdev(struct ib_device *device,
149 u8 port_num)
150{
151 struct mlx5_ib_dev *ibdev = to_mdev(device);
152 struct net_device *ndev;
153
Aviv Heller88621df2016-09-18 20:48:02 +0300154 ndev = mlx5_lag_get_roce_netdev(ibdev->mdev);
155 if (ndev)
156 return ndev;
157
Achiad Shochatfc24fc52015-12-23 18:47:17 +0200158 /* Ensure ndev does not disappear before we invoke dev_hold()
159 */
160 read_lock(&ibdev->roce.netdev_lock);
161 ndev = ibdev->roce.netdev;
162 if (ndev)
163 dev_hold(ndev);
164 read_unlock(&ibdev->roce.netdev_lock);
165
166 return ndev;
167}
168
Achiad Shochat3f89a642015-12-23 18:47:21 +0200169static int mlx5_query_port_roce(struct ib_device *device, u8 port_num,
170 struct ib_port_attr *props)
171{
172 struct mlx5_ib_dev *dev = to_mdev(device);
Aviv Heller88621df2016-09-18 20:48:02 +0300173 struct net_device *ndev, *upper;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200174 enum ib_mtu ndev_ib_mtu;
Leon Romanovskyc876a1b2016-01-09 13:06:25 +0200175 u16 qkey_viol_cntr;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200176
177 memset(props, 0, sizeof(*props));
178
179 props->port_cap_flags |= IB_PORT_CM_SUP;
180 props->port_cap_flags |= IB_PORT_IP_BASED_GIDS;
181
182 props->gid_tbl_len = MLX5_CAP_ROCE(dev->mdev,
183 roce_address_table_size);
184 props->max_mtu = IB_MTU_4096;
185 props->max_msg_sz = 1 << MLX5_CAP_GEN(dev->mdev, log_max_msg);
186 props->pkey_tbl_len = 1;
187 props->state = IB_PORT_DOWN;
188 props->phys_state = 3;
189
Leon Romanovskyc876a1b2016-01-09 13:06:25 +0200190 mlx5_query_nic_vport_qkey_viol_cntr(dev->mdev, &qkey_viol_cntr);
191 props->qkey_viol_cntr = qkey_viol_cntr;
Achiad Shochat3f89a642015-12-23 18:47:21 +0200192
193 ndev = mlx5_ib_get_netdev(device, port_num);
194 if (!ndev)
195 return 0;
196
Aviv Heller88621df2016-09-18 20:48:02 +0300197 if (mlx5_lag_is_active(dev->mdev)) {
198 rcu_read_lock();
199 upper = netdev_master_upper_dev_get_rcu(ndev);
200 if (upper) {
201 dev_put(ndev);
202 ndev = upper;
203 dev_hold(ndev);
204 }
205 rcu_read_unlock();
206 }
207
Achiad Shochat3f89a642015-12-23 18:47:21 +0200208 if (netif_running(ndev) && netif_carrier_ok(ndev)) {
209 props->state = IB_PORT_ACTIVE;
210 props->phys_state = 5;
211 }
212
213 ndev_ib_mtu = iboe_get_mtu(ndev->mtu);
214
215 dev_put(ndev);
216
217 props->active_mtu = min(props->max_mtu, ndev_ib_mtu);
218
219 props->active_width = IB_WIDTH_4X; /* TODO */
220 props->active_speed = IB_SPEED_QDR; /* TODO */
221
222 return 0;
223}
224
Achiad Shochat3cca2602015-12-23 18:47:23 +0200225static void ib_gid_to_mlx5_roce_addr(const union ib_gid *gid,
226 const struct ib_gid_attr *attr,
227 void *mlx5_addr)
228{
229#define MLX5_SET_RA(p, f, v) MLX5_SET(roce_addr_layout, p, f, v)
230 char *mlx5_addr_l3_addr = MLX5_ADDR_OF(roce_addr_layout, mlx5_addr,
231 source_l3_address);
232 void *mlx5_addr_mac = MLX5_ADDR_OF(roce_addr_layout, mlx5_addr,
233 source_mac_47_32);
234
235 if (!gid)
236 return;
237
238 ether_addr_copy(mlx5_addr_mac, attr->ndev->dev_addr);
239
240 if (is_vlan_dev(attr->ndev)) {
241 MLX5_SET_RA(mlx5_addr, vlan_valid, 1);
242 MLX5_SET_RA(mlx5_addr, vlan_id, vlan_dev_vlan_id(attr->ndev));
243 }
244
245 switch (attr->gid_type) {
246 case IB_GID_TYPE_IB:
247 MLX5_SET_RA(mlx5_addr, roce_version, MLX5_ROCE_VERSION_1);
248 break;
249 case IB_GID_TYPE_ROCE_UDP_ENCAP:
250 MLX5_SET_RA(mlx5_addr, roce_version, MLX5_ROCE_VERSION_2);
251 break;
252
253 default:
254 WARN_ON(true);
255 }
256
257 if (attr->gid_type != IB_GID_TYPE_IB) {
258 if (ipv6_addr_v4mapped((void *)gid))
259 MLX5_SET_RA(mlx5_addr, roce_l3_type,
260 MLX5_ROCE_L3_TYPE_IPV4);
261 else
262 MLX5_SET_RA(mlx5_addr, roce_l3_type,
263 MLX5_ROCE_L3_TYPE_IPV6);
264 }
265
266 if ((attr->gid_type == IB_GID_TYPE_IB) ||
267 !ipv6_addr_v4mapped((void *)gid))
268 memcpy(mlx5_addr_l3_addr, gid, sizeof(*gid));
269 else
270 memcpy(&mlx5_addr_l3_addr[12], &gid->raw[12], 4);
271}
272
273static int set_roce_addr(struct ib_device *device, u8 port_num,
274 unsigned int index,
275 const union ib_gid *gid,
276 const struct ib_gid_attr *attr)
277{
Saeed Mahameedc4f287c2016-07-19 20:17:12 +0300278 struct mlx5_ib_dev *dev = to_mdev(device);
279 u32 in[MLX5_ST_SZ_DW(set_roce_address_in)] = {0};
280 u32 out[MLX5_ST_SZ_DW(set_roce_address_out)] = {0};
Achiad Shochat3cca2602015-12-23 18:47:23 +0200281 void *in_addr = MLX5_ADDR_OF(set_roce_address_in, in, roce_address);
282 enum rdma_link_layer ll = mlx5_ib_port_link_layer(device, port_num);
283
284 if (ll != IB_LINK_LAYER_ETHERNET)
285 return -EINVAL;
286
Achiad Shochat3cca2602015-12-23 18:47:23 +0200287 ib_gid_to_mlx5_roce_addr(gid, attr, in_addr);
288
289 MLX5_SET(set_roce_address_in, in, roce_address_index, index);
290 MLX5_SET(set_roce_address_in, in, opcode, MLX5_CMD_OP_SET_ROCE_ADDRESS);
Achiad Shochat3cca2602015-12-23 18:47:23 +0200291 return mlx5_cmd_exec(dev->mdev, in, sizeof(in), out, sizeof(out));
292}
293
294static int mlx5_ib_add_gid(struct ib_device *device, u8 port_num,
295 unsigned int index, const union ib_gid *gid,
296 const struct ib_gid_attr *attr,
297 __always_unused void **context)
298{
299 return set_roce_addr(device, port_num, index, gid, attr);
300}
301
302static int mlx5_ib_del_gid(struct ib_device *device, u8 port_num,
303 unsigned int index, __always_unused void **context)
304{
305 return set_roce_addr(device, port_num, index, NULL, NULL);
306}
307
Achiad Shochat2811ba52015-12-23 18:47:24 +0200308__be16 mlx5_get_roce_udp_sport(struct mlx5_ib_dev *dev, u8 port_num,
309 int index)
310{
311 struct ib_gid_attr attr;
312 union ib_gid gid;
313
314 if (ib_get_cached_gid(&dev->ib_dev, port_num, index, &gid, &attr))
315 return 0;
316
317 if (!attr.ndev)
318 return 0;
319
320 dev_put(attr.ndev);
321
322 if (attr.gid_type != IB_GID_TYPE_ROCE_UDP_ENCAP)
323 return 0;
324
325 return cpu_to_be16(MLX5_CAP_ROCE(dev->mdev, r_roce_min_src_udp_port));
326}
327
Majd Dibbinyed884512017-01-18 14:10:35 +0200328int mlx5_get_roce_gid_type(struct mlx5_ib_dev *dev, u8 port_num,
329 int index, enum ib_gid_type *gid_type)
330{
331 struct ib_gid_attr attr;
332 union ib_gid gid;
333 int ret;
334
335 ret = ib_get_cached_gid(&dev->ib_dev, port_num, index, &gid, &attr);
336 if (ret)
337 return ret;
338
339 if (!attr.ndev)
340 return -ENODEV;
341
342 dev_put(attr.ndev);
343
344 *gid_type = attr.gid_type;
345
346 return 0;
347}
348
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300349static int mlx5_use_mad_ifc(struct mlx5_ib_dev *dev)
350{
Noa Osherovich7fae6652016-09-12 19:16:23 +0300351 if (MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_IB)
352 return !MLX5_CAP_GEN(dev->mdev, ib_virt);
353 return 0;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300354}
355
356enum {
357 MLX5_VPORT_ACCESS_METHOD_MAD,
358 MLX5_VPORT_ACCESS_METHOD_HCA,
359 MLX5_VPORT_ACCESS_METHOD_NIC,
360};
361
362static int mlx5_get_vport_access_method(struct ib_device *ibdev)
363{
364 if (mlx5_use_mad_ifc(to_mdev(ibdev)))
365 return MLX5_VPORT_ACCESS_METHOD_MAD;
366
Achiad Shochatebd61f62015-12-23 18:47:16 +0200367 if (mlx5_ib_port_link_layer(ibdev, 1) ==
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300368 IB_LINK_LAYER_ETHERNET)
369 return MLX5_VPORT_ACCESS_METHOD_NIC;
370
371 return MLX5_VPORT_ACCESS_METHOD_HCA;
372}
373
Eran Ben Elishada7525d2015-12-14 16:34:10 +0200374static void get_atomic_caps(struct mlx5_ib_dev *dev,
375 struct ib_device_attr *props)
376{
377 u8 tmp;
378 u8 atomic_operations = MLX5_CAP_ATOMIC(dev->mdev, atomic_operations);
379 u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_qp);
380 u8 atomic_req_8B_endianness_mode =
381 MLX5_CAP_ATOMIC(dev->mdev, atomic_req_8B_endianess_mode);
382
383 /* Check if HW supports 8 bytes standard atomic operations and capable
384 * of host endianness respond
385 */
386 tmp = MLX5_ATOMIC_OPS_CMP_SWAP | MLX5_ATOMIC_OPS_FETCH_ADD;
387 if (((atomic_operations & tmp) == tmp) &&
388 (atomic_size_qp & MLX5_ATOMIC_SIZE_QP_8BYTES) &&
389 (atomic_req_8B_endianness_mode)) {
390 props->atomic_cap = IB_ATOMIC_HCA;
391 } else {
392 props->atomic_cap = IB_ATOMIC_NONE;
393 }
394}
395
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300396static int mlx5_query_system_image_guid(struct ib_device *ibdev,
397 __be64 *sys_image_guid)
398{
399 struct mlx5_ib_dev *dev = to_mdev(ibdev);
400 struct mlx5_core_dev *mdev = dev->mdev;
401 u64 tmp;
402 int err;
403
404 switch (mlx5_get_vport_access_method(ibdev)) {
405 case MLX5_VPORT_ACCESS_METHOD_MAD:
406 return mlx5_query_mad_ifc_system_image_guid(ibdev,
407 sys_image_guid);
408
409 case MLX5_VPORT_ACCESS_METHOD_HCA:
410 err = mlx5_query_hca_vport_system_image_guid(mdev, &tmp);
Achiad Shochat3f89a642015-12-23 18:47:21 +0200411 break;
412
413 case MLX5_VPORT_ACCESS_METHOD_NIC:
414 err = mlx5_query_nic_vport_system_image_guid(mdev, &tmp);
415 break;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300416
417 default:
418 return -EINVAL;
419 }
Achiad Shochat3f89a642015-12-23 18:47:21 +0200420
421 if (!err)
422 *sys_image_guid = cpu_to_be64(tmp);
423
424 return err;
425
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300426}
427
428static int mlx5_query_max_pkeys(struct ib_device *ibdev,
429 u16 *max_pkeys)
430{
431 struct mlx5_ib_dev *dev = to_mdev(ibdev);
432 struct mlx5_core_dev *mdev = dev->mdev;
433
434 switch (mlx5_get_vport_access_method(ibdev)) {
435 case MLX5_VPORT_ACCESS_METHOD_MAD:
436 return mlx5_query_mad_ifc_max_pkeys(ibdev, max_pkeys);
437
438 case MLX5_VPORT_ACCESS_METHOD_HCA:
439 case MLX5_VPORT_ACCESS_METHOD_NIC:
440 *max_pkeys = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev,
441 pkey_table_size));
442 return 0;
443
444 default:
445 return -EINVAL;
446 }
447}
448
449static int mlx5_query_vendor_id(struct ib_device *ibdev,
450 u32 *vendor_id)
451{
452 struct mlx5_ib_dev *dev = to_mdev(ibdev);
453
454 switch (mlx5_get_vport_access_method(ibdev)) {
455 case MLX5_VPORT_ACCESS_METHOD_MAD:
456 return mlx5_query_mad_ifc_vendor_id(ibdev, vendor_id);
457
458 case MLX5_VPORT_ACCESS_METHOD_HCA:
459 case MLX5_VPORT_ACCESS_METHOD_NIC:
460 return mlx5_core_query_vendor_id(dev->mdev, vendor_id);
461
462 default:
463 return -EINVAL;
464 }
465}
466
467static int mlx5_query_node_guid(struct mlx5_ib_dev *dev,
468 __be64 *node_guid)
469{
470 u64 tmp;
471 int err;
472
473 switch (mlx5_get_vport_access_method(&dev->ib_dev)) {
474 case MLX5_VPORT_ACCESS_METHOD_MAD:
475 return mlx5_query_mad_ifc_node_guid(dev, node_guid);
476
477 case MLX5_VPORT_ACCESS_METHOD_HCA:
478 err = mlx5_query_hca_vport_node_guid(dev->mdev, &tmp);
Achiad Shochat3f89a642015-12-23 18:47:21 +0200479 break;
480
481 case MLX5_VPORT_ACCESS_METHOD_NIC:
482 err = mlx5_query_nic_vport_node_guid(dev->mdev, &tmp);
483 break;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300484
485 default:
486 return -EINVAL;
487 }
Achiad Shochat3f89a642015-12-23 18:47:21 +0200488
489 if (!err)
490 *node_guid = cpu_to_be64(tmp);
491
492 return err;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300493}
494
495struct mlx5_reg_node_desc {
Yuval Shaiabd99fde2016-08-25 10:57:07 -0700496 u8 desc[IB_DEVICE_NODE_DESC_MAX];
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300497};
498
499static int mlx5_query_node_desc(struct mlx5_ib_dev *dev, char *node_desc)
500{
501 struct mlx5_reg_node_desc in;
502
503 if (mlx5_use_mad_ifc(dev))
504 return mlx5_query_mad_ifc_node_desc(dev, node_desc);
505
506 memset(&in, 0, sizeof(in));
507
508 return mlx5_core_access_reg(dev->mdev, &in, sizeof(in), node_desc,
509 sizeof(struct mlx5_reg_node_desc),
510 MLX5_REG_NODE_DESC, 0, 0);
511}
512
Eli Cohene126ba92013-07-07 17:25:49 +0300513static int mlx5_ib_query_device(struct ib_device *ibdev,
Matan Barak2528e332015-06-11 16:35:25 +0300514 struct ib_device_attr *props,
515 struct ib_udata *uhw)
Eli Cohene126ba92013-07-07 17:25:49 +0300516{
517 struct mlx5_ib_dev *dev = to_mdev(ibdev);
Saeed Mahameed938fe832015-05-28 22:28:41 +0300518 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +0300519 int err = -ENOMEM;
Eli Cohen288c01b2016-10-27 16:36:45 +0300520 int max_sq_desc;
Eli Cohene126ba92013-07-07 17:25:49 +0300521 int max_rq_sg;
522 int max_sq_sg;
Sagi Grimberge0238a62015-07-21 14:40:12 +0300523 u64 min_page_size = 1ull << MLX5_CAP_GEN(mdev, log_pg_sz);
Bodong Wang402ca532016-06-17 15:02:20 +0300524 struct mlx5_ib_query_device_resp resp = {};
525 size_t resp_len;
526 u64 max_tso;
Eli Cohene126ba92013-07-07 17:25:49 +0300527
Bodong Wang402ca532016-06-17 15:02:20 +0300528 resp_len = sizeof(resp.comp_mask) + sizeof(resp.response_length);
529 if (uhw->outlen && uhw->outlen < resp_len)
530 return -EINVAL;
531 else
532 resp.response_length = resp_len;
533
534 if (uhw->inlen && !ib_is_udata_cleared(uhw, 0, uhw->inlen))
Matan Barak2528e332015-06-11 16:35:25 +0300535 return -EINVAL;
536
Eli Cohene126ba92013-07-07 17:25:49 +0300537 memset(props, 0, sizeof(*props));
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300538 err = mlx5_query_system_image_guid(ibdev,
539 &props->sys_image_guid);
540 if (err)
541 return err;
542
543 err = mlx5_query_max_pkeys(ibdev, &props->max_pkeys);
544 if (err)
545 return err;
546
547 err = mlx5_query_vendor_id(ibdev, &props->vendor_id);
548 if (err)
549 return err;
Eli Cohene126ba92013-07-07 17:25:49 +0300550
Jack Morgenstein9603b612014-07-28 23:30:22 +0300551 props->fw_ver = ((u64)fw_rev_maj(dev->mdev) << 32) |
552 (fw_rev_min(dev->mdev) << 16) |
553 fw_rev_sub(dev->mdev);
Eli Cohene126ba92013-07-07 17:25:49 +0300554 props->device_cap_flags = IB_DEVICE_CHANGE_PHY_PORT |
555 IB_DEVICE_PORT_ACTIVE_EVENT |
556 IB_DEVICE_SYS_IMAGE_GUID |
Eli Cohen1a4c3a32014-02-06 17:41:25 +0200557 IB_DEVICE_RC_RNR_NAK_GEN;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300558
559 if (MLX5_CAP_GEN(mdev, pkv))
Eli Cohene126ba92013-07-07 17:25:49 +0300560 props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300561 if (MLX5_CAP_GEN(mdev, qkv))
Eli Cohene126ba92013-07-07 17:25:49 +0300562 props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300563 if (MLX5_CAP_GEN(mdev, apm))
Eli Cohene126ba92013-07-07 17:25:49 +0300564 props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300565 if (MLX5_CAP_GEN(mdev, xrc))
Eli Cohene126ba92013-07-07 17:25:49 +0300566 props->device_cap_flags |= IB_DEVICE_XRC;
Matan Barakd2370e02016-02-29 18:05:30 +0200567 if (MLX5_CAP_GEN(mdev, imaicl)) {
568 props->device_cap_flags |= IB_DEVICE_MEM_WINDOW |
569 IB_DEVICE_MEM_WINDOW_TYPE_2B;
570 props->max_mw = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
Sagi Grimbergb005d312016-02-29 19:07:33 +0200571 /* We support 'Gappy' memory registration too */
572 props->device_cap_flags |= IB_DEVICE_SG_GAPS_REG;
Matan Barakd2370e02016-02-29 18:05:30 +0200573 }
Eli Cohene126ba92013-07-07 17:25:49 +0300574 props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300575 if (MLX5_CAP_GEN(mdev, sho)) {
Sagi Grimberg2dea9092014-02-23 14:19:13 +0200576 props->device_cap_flags |= IB_DEVICE_SIGNATURE_HANDOVER;
577 /* At this stage no support for signature handover */
578 props->sig_prot_cap = IB_PROT_T10DIF_TYPE_1 |
579 IB_PROT_T10DIF_TYPE_2 |
580 IB_PROT_T10DIF_TYPE_3;
581 props->sig_guard_cap = IB_GUARD_T10DIF_CRC |
582 IB_GUARD_T10DIF_CSUM;
583 }
Saeed Mahameed938fe832015-05-28 22:28:41 +0300584 if (MLX5_CAP_GEN(mdev, block_lb_mc))
Eli Cohenf360d882014-04-02 00:10:16 +0300585 props->device_cap_flags |= IB_DEVICE_BLOCK_MULTICAST_LOOPBACK;
Eli Cohene126ba92013-07-07 17:25:49 +0300586
Bodong Wang402ca532016-06-17 15:02:20 +0300587 if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads)) {
588 if (MLX5_CAP_ETH(mdev, csum_cap))
Bodong Wang88115fe2015-12-18 13:53:20 +0200589 props->device_cap_flags |= IB_DEVICE_RAW_IP_CSUM;
590
Bodong Wang402ca532016-06-17 15:02:20 +0300591 if (field_avail(typeof(resp), tso_caps, uhw->outlen)) {
592 max_tso = MLX5_CAP_ETH(mdev, max_lso_cap);
593 if (max_tso) {
594 resp.tso_caps.max_tso = 1 << max_tso;
595 resp.tso_caps.supported_qpts |=
596 1 << IB_QPT_RAW_PACKET;
597 resp.response_length += sizeof(resp.tso_caps);
598 }
599 }
Yishai Hadas31f69a82016-08-28 11:28:45 +0300600
601 if (field_avail(typeof(resp), rss_caps, uhw->outlen)) {
602 resp.rss_caps.rx_hash_function =
603 MLX5_RX_HASH_FUNC_TOEPLITZ;
604 resp.rss_caps.rx_hash_fields_mask =
605 MLX5_RX_HASH_SRC_IPV4 |
606 MLX5_RX_HASH_DST_IPV4 |
607 MLX5_RX_HASH_SRC_IPV6 |
608 MLX5_RX_HASH_DST_IPV6 |
609 MLX5_RX_HASH_SRC_PORT_TCP |
610 MLX5_RX_HASH_DST_PORT_TCP |
611 MLX5_RX_HASH_SRC_PORT_UDP |
612 MLX5_RX_HASH_DST_PORT_UDP;
613 resp.response_length += sizeof(resp.rss_caps);
614 }
615 } else {
616 if (field_avail(typeof(resp), tso_caps, uhw->outlen))
617 resp.response_length += sizeof(resp.tso_caps);
618 if (field_avail(typeof(resp), rss_caps, uhw->outlen))
619 resp.response_length += sizeof(resp.rss_caps);
Bodong Wang402ca532016-06-17 15:02:20 +0300620 }
621
Erez Shitritf0313962016-02-21 16:27:17 +0200622 if (MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) {
623 props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
624 props->device_cap_flags |= IB_DEVICE_UD_TSO;
625 }
626
Majd Dibbinycff5a0f2016-04-17 17:19:38 +0300627 if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
628 MLX5_CAP_ETH(dev->mdev, scatter_fcs))
629 props->device_cap_flags |= IB_DEVICE_RAW_SCATTER_FCS;
630
Maor Gottliebda6d6ba32016-06-04 15:15:28 +0300631 if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS))
632 props->device_cap_flags |= IB_DEVICE_MANAGED_FLOW_STEERING;
633
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300634 props->vendor_part_id = mdev->pdev->device;
635 props->hw_ver = mdev->pdev->revision;
Eli Cohene126ba92013-07-07 17:25:49 +0300636
637 props->max_mr_size = ~0ull;
Sagi Grimberge0238a62015-07-21 14:40:12 +0300638 props->page_size_cap = ~(min_page_size - 1);
Saeed Mahameed938fe832015-05-28 22:28:41 +0300639 props->max_qp = 1 << MLX5_CAP_GEN(mdev, log_max_qp);
640 props->max_qp_wr = 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
641 max_rq_sg = MLX5_CAP_GEN(mdev, max_wqe_sz_rq) /
642 sizeof(struct mlx5_wqe_data_seg);
Eli Cohen288c01b2016-10-27 16:36:45 +0300643 max_sq_desc = min_t(int, MLX5_CAP_GEN(mdev, max_wqe_sz_sq), 512);
644 max_sq_sg = (max_sq_desc - sizeof(struct mlx5_wqe_ctrl_seg) -
645 sizeof(struct mlx5_wqe_raddr_seg)) /
646 sizeof(struct mlx5_wqe_data_seg);
Eli Cohene126ba92013-07-07 17:25:49 +0300647 props->max_sge = min(max_rq_sg, max_sq_sg);
Sagi Grimberg986ef952016-03-31 19:03:25 +0300648 props->max_sge_rd = MLX5_MAX_SGE_RD;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300649 props->max_cq = 1 << MLX5_CAP_GEN(mdev, log_max_cq);
Leon Romanovsky9f177682016-01-14 08:11:40 +0200650 props->max_cqe = (1 << MLX5_CAP_GEN(mdev, log_max_cq_sz)) - 1;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300651 props->max_mr = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
652 props->max_pd = 1 << MLX5_CAP_GEN(mdev, log_max_pd);
653 props->max_qp_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_req_qp);
654 props->max_qp_init_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_res_qp);
655 props->max_srq = 1 << MLX5_CAP_GEN(mdev, log_max_srq);
656 props->max_srq_wr = (1 << MLX5_CAP_GEN(mdev, log_max_srq_sz)) - 1;
657 props->local_ca_ack_delay = MLX5_CAP_GEN(mdev, local_ca_ack_delay);
Eli Cohene126ba92013-07-07 17:25:49 +0300658 props->max_res_rd_atom = props->max_qp_rd_atom * props->max_qp;
Eli Cohene126ba92013-07-07 17:25:49 +0300659 props->max_srq_sge = max_rq_sg - 1;
Sagi Grimberg911f4332016-03-03 13:37:51 +0200660 props->max_fast_reg_page_list_len =
661 1 << MLX5_CAP_GEN(mdev, log_max_klm_list_size);
Eran Ben Elishada7525d2015-12-14 16:34:10 +0200662 get_atomic_caps(dev, props);
Eli Cohen81bea282013-09-11 16:35:30 +0300663 props->masked_atomic_cap = IB_ATOMIC_NONE;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300664 props->max_mcast_grp = 1 << MLX5_CAP_GEN(mdev, log_max_mcg);
665 props->max_mcast_qp_attach = MLX5_CAP_GEN(mdev, max_qp_mcg);
Eli Cohene126ba92013-07-07 17:25:49 +0300666 props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
667 props->max_mcast_grp;
668 props->max_map_per_fmr = INT_MAX; /* no limit in ConnectIB */
Maor Gottlieb86695a62016-10-27 16:36:38 +0300669 props->max_ah = INT_MAX;
Matan Barak7c60bcb2015-12-15 20:30:11 +0200670 props->hca_core_clock = MLX5_CAP_GEN(mdev, device_frequency_khz);
671 props->timestamp_mask = 0x7FFFFFFFFFFFFFFFULL;
Eli Cohene126ba92013-07-07 17:25:49 +0300672
Haggai Eran8cdd3122014-12-11 17:04:20 +0200673#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
Saeed Mahameed938fe832015-05-28 22:28:41 +0300674 if (MLX5_CAP_GEN(mdev, pg))
Haggai Eran8cdd3122014-12-11 17:04:20 +0200675 props->device_cap_flags |= IB_DEVICE_ON_DEMAND_PAGING;
676 props->odp_caps = dev->odp_caps;
677#endif
678
Leon Romanovsky051f2632015-12-20 12:16:11 +0200679 if (MLX5_CAP_GEN(mdev, cd))
680 props->device_cap_flags |= IB_DEVICE_CROSS_CHANNEL;
681
Eli Coheneff901d2016-03-11 22:58:42 +0200682 if (!mlx5_core_is_pf(mdev))
683 props->device_cap_flags |= IB_DEVICE_VIRTUAL_FUNCTION;
684
Yishai Hadas31f69a82016-08-28 11:28:45 +0300685 if (mlx5_ib_port_link_layer(ibdev, 1) ==
686 IB_LINK_LAYER_ETHERNET) {
687 props->rss_caps.max_rwq_indirection_tables =
688 1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt);
689 props->rss_caps.max_rwq_indirection_table_size =
690 1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt_size);
691 props->rss_caps.supported_qpts = 1 << IB_QPT_RAW_PACKET;
692 props->max_wq_type_rq =
693 1 << MLX5_CAP_GEN(dev->mdev, log_max_rq);
694 }
695
Bodong Wang7e43a2a2016-10-31 12:16:44 +0200696 if (field_avail(typeof(resp), cqe_comp_caps, uhw->outlen)) {
697 resp.cqe_comp_caps.max_num =
698 MLX5_CAP_GEN(dev->mdev, cqe_compression) ?
699 MLX5_CAP_GEN(dev->mdev, cqe_compression_max_num) : 0;
700 resp.cqe_comp_caps.supported_format =
701 MLX5_IB_CQE_RES_FORMAT_HASH |
702 MLX5_IB_CQE_RES_FORMAT_CSUM;
703 resp.response_length += sizeof(resp.cqe_comp_caps);
704 }
705
Bodong Wangd9491672016-12-01 13:43:13 +0200706 if (field_avail(typeof(resp), packet_pacing_caps, uhw->outlen)) {
707 if (MLX5_CAP_QOS(mdev, packet_pacing) &&
708 MLX5_CAP_GEN(mdev, qos)) {
709 resp.packet_pacing_caps.qp_rate_limit_max =
710 MLX5_CAP_QOS(mdev, packet_pacing_max_rate);
711 resp.packet_pacing_caps.qp_rate_limit_min =
712 MLX5_CAP_QOS(mdev, packet_pacing_min_rate);
713 resp.packet_pacing_caps.supported_qpts |=
714 1 << IB_QPT_RAW_PACKET;
715 }
716 resp.response_length += sizeof(resp.packet_pacing_caps);
717 }
718
Leon Romanovsky9f885202017-01-02 11:37:39 +0200719 if (field_avail(typeof(resp), mlx5_ib_support_multi_pkt_send_wqes,
720 uhw->outlen)) {
721 resp.mlx5_ib_support_multi_pkt_send_wqes =
722 MLX5_CAP_ETH(mdev, multi_pkt_send_wqe);
723 resp.response_length +=
724 sizeof(resp.mlx5_ib_support_multi_pkt_send_wqes);
725 }
726
727 if (field_avail(typeof(resp), reserved, uhw->outlen))
728 resp.response_length += sizeof(resp.reserved);
729
Bodong Wang402ca532016-06-17 15:02:20 +0300730 if (uhw->outlen) {
731 err = ib_copy_to_udata(uhw, &resp, resp.response_length);
732
733 if (err)
734 return err;
735 }
736
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300737 return 0;
738}
Eli Cohene126ba92013-07-07 17:25:49 +0300739
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300740enum mlx5_ib_width {
741 MLX5_IB_WIDTH_1X = 1 << 0,
742 MLX5_IB_WIDTH_2X = 1 << 1,
743 MLX5_IB_WIDTH_4X = 1 << 2,
744 MLX5_IB_WIDTH_8X = 1 << 3,
745 MLX5_IB_WIDTH_12X = 1 << 4
746};
747
748static int translate_active_width(struct ib_device *ibdev, u8 active_width,
749 u8 *ib_width)
750{
751 struct mlx5_ib_dev *dev = to_mdev(ibdev);
752 int err = 0;
753
754 if (active_width & MLX5_IB_WIDTH_1X) {
755 *ib_width = IB_WIDTH_1X;
756 } else if (active_width & MLX5_IB_WIDTH_2X) {
757 mlx5_ib_dbg(dev, "active_width %d is not supported by IB spec\n",
758 (int)active_width);
759 err = -EINVAL;
760 } else if (active_width & MLX5_IB_WIDTH_4X) {
761 *ib_width = IB_WIDTH_4X;
762 } else if (active_width & MLX5_IB_WIDTH_8X) {
763 *ib_width = IB_WIDTH_8X;
764 } else if (active_width & MLX5_IB_WIDTH_12X) {
765 *ib_width = IB_WIDTH_12X;
766 } else {
767 mlx5_ib_dbg(dev, "Invalid active_width %d\n",
768 (int)active_width);
769 err = -EINVAL;
770 }
771
772 return err;
773}
774
775static int mlx5_mtu_to_ib_mtu(int mtu)
776{
777 switch (mtu) {
778 case 256: return 1;
779 case 512: return 2;
780 case 1024: return 3;
781 case 2048: return 4;
782 case 4096: return 5;
783 default:
784 pr_warn("invalid mtu\n");
785 return -1;
786 }
787}
788
789enum ib_max_vl_num {
790 __IB_MAX_VL_0 = 1,
791 __IB_MAX_VL_0_1 = 2,
792 __IB_MAX_VL_0_3 = 3,
793 __IB_MAX_VL_0_7 = 4,
794 __IB_MAX_VL_0_14 = 5,
795};
796
797enum mlx5_vl_hw_cap {
798 MLX5_VL_HW_0 = 1,
799 MLX5_VL_HW_0_1 = 2,
800 MLX5_VL_HW_0_2 = 3,
801 MLX5_VL_HW_0_3 = 4,
802 MLX5_VL_HW_0_4 = 5,
803 MLX5_VL_HW_0_5 = 6,
804 MLX5_VL_HW_0_6 = 7,
805 MLX5_VL_HW_0_7 = 8,
806 MLX5_VL_HW_0_14 = 15
807};
808
809static int translate_max_vl_num(struct ib_device *ibdev, u8 vl_hw_cap,
810 u8 *max_vl_num)
811{
812 switch (vl_hw_cap) {
813 case MLX5_VL_HW_0:
814 *max_vl_num = __IB_MAX_VL_0;
815 break;
816 case MLX5_VL_HW_0_1:
817 *max_vl_num = __IB_MAX_VL_0_1;
818 break;
819 case MLX5_VL_HW_0_3:
820 *max_vl_num = __IB_MAX_VL_0_3;
821 break;
822 case MLX5_VL_HW_0_7:
823 *max_vl_num = __IB_MAX_VL_0_7;
824 break;
825 case MLX5_VL_HW_0_14:
826 *max_vl_num = __IB_MAX_VL_0_14;
827 break;
828
829 default:
830 return -EINVAL;
831 }
832
833 return 0;
834}
835
836static int mlx5_query_hca_port(struct ib_device *ibdev, u8 port,
837 struct ib_port_attr *props)
838{
839 struct mlx5_ib_dev *dev = to_mdev(ibdev);
840 struct mlx5_core_dev *mdev = dev->mdev;
841 struct mlx5_hca_vport_context *rep;
Saeed Mahameed046339e2016-04-22 00:33:03 +0300842 u16 max_mtu;
843 u16 oper_mtu;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300844 int err;
845 u8 ib_link_width_oper;
846 u8 vl_hw_cap;
847
848 rep = kzalloc(sizeof(*rep), GFP_KERNEL);
849 if (!rep) {
850 err = -ENOMEM;
851 goto out;
852 }
853
854 memset(props, 0, sizeof(*props));
855
856 err = mlx5_query_hca_vport_context(mdev, 0, port, 0, rep);
857 if (err)
858 goto out;
859
860 props->lid = rep->lid;
861 props->lmc = rep->lmc;
862 props->sm_lid = rep->sm_lid;
863 props->sm_sl = rep->sm_sl;
864 props->state = rep->vport_state;
865 props->phys_state = rep->port_physical_state;
866 props->port_cap_flags = rep->cap_mask1;
867 props->gid_tbl_len = mlx5_get_gid_table_len(MLX5_CAP_GEN(mdev, gid_table_size));
868 props->max_msg_sz = 1 << MLX5_CAP_GEN(mdev, log_max_msg);
869 props->pkey_tbl_len = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, pkey_table_size));
870 props->bad_pkey_cntr = rep->pkey_violation_counter;
871 props->qkey_viol_cntr = rep->qkey_violation_counter;
872 props->subnet_timeout = rep->subnet_timeout;
873 props->init_type_reply = rep->init_type_reply;
Eli Coheneff901d2016-03-11 22:58:42 +0200874 props->grh_required = rep->grh_required;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300875
876 err = mlx5_query_port_link_width_oper(mdev, &ib_link_width_oper, port);
877 if (err)
878 goto out;
879
880 err = translate_active_width(ibdev, ib_link_width_oper,
881 &props->active_width);
882 if (err)
883 goto out;
Noa Osherovichd5beb7f2016-06-02 10:47:53 +0300884 err = mlx5_query_port_ib_proto_oper(mdev, &props->active_speed, port);
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300885 if (err)
886 goto out;
887
Saeed Mahameedfacc9692015-06-11 14:47:27 +0300888 mlx5_query_port_max_mtu(mdev, &max_mtu, port);
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300889
890 props->max_mtu = mlx5_mtu_to_ib_mtu(max_mtu);
891
Saeed Mahameedfacc9692015-06-11 14:47:27 +0300892 mlx5_query_port_oper_mtu(mdev, &oper_mtu, port);
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300893
894 props->active_mtu = mlx5_mtu_to_ib_mtu(oper_mtu);
895
896 err = mlx5_query_port_vl_hw_cap(mdev, &vl_hw_cap, port);
897 if (err)
898 goto out;
899
900 err = translate_max_vl_num(ibdev, vl_hw_cap,
901 &props->max_vl_num);
902out:
903 kfree(rep);
Eli Cohene126ba92013-07-07 17:25:49 +0300904 return err;
905}
906
907int mlx5_ib_query_port(struct ib_device *ibdev, u8 port,
908 struct ib_port_attr *props)
909{
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300910 switch (mlx5_get_vport_access_method(ibdev)) {
911 case MLX5_VPORT_ACCESS_METHOD_MAD:
912 return mlx5_query_mad_ifc_port(ibdev, port, props);
Eli Cohene126ba92013-07-07 17:25:49 +0300913
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300914 case MLX5_VPORT_ACCESS_METHOD_HCA:
915 return mlx5_query_hca_port(ibdev, port, props);
916
Achiad Shochat3f89a642015-12-23 18:47:21 +0200917 case MLX5_VPORT_ACCESS_METHOD_NIC:
918 return mlx5_query_port_roce(ibdev, port, props);
919
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300920 default:
Eli Cohene126ba92013-07-07 17:25:49 +0300921 return -EINVAL;
922 }
Eli Cohene126ba92013-07-07 17:25:49 +0300923}
924
925static int mlx5_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
926 union ib_gid *gid)
927{
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300928 struct mlx5_ib_dev *dev = to_mdev(ibdev);
929 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +0300930
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300931 switch (mlx5_get_vport_access_method(ibdev)) {
932 case MLX5_VPORT_ACCESS_METHOD_MAD:
933 return mlx5_query_mad_ifc_gids(ibdev, port, index, gid);
Eli Cohene126ba92013-07-07 17:25:49 +0300934
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300935 case MLX5_VPORT_ACCESS_METHOD_HCA:
936 return mlx5_query_hca_vport_gid(mdev, 0, port, 0, index, gid);
Eli Cohene126ba92013-07-07 17:25:49 +0300937
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300938 default:
939 return -EINVAL;
940 }
Eli Cohene126ba92013-07-07 17:25:49 +0300941
Eli Cohene126ba92013-07-07 17:25:49 +0300942}
943
944static int mlx5_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
945 u16 *pkey)
946{
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300947 struct mlx5_ib_dev *dev = to_mdev(ibdev);
948 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +0300949
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300950 switch (mlx5_get_vport_access_method(ibdev)) {
951 case MLX5_VPORT_ACCESS_METHOD_MAD:
952 return mlx5_query_mad_ifc_pkey(ibdev, port, index, pkey);
Eli Cohene126ba92013-07-07 17:25:49 +0300953
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300954 case MLX5_VPORT_ACCESS_METHOD_HCA:
955 case MLX5_VPORT_ACCESS_METHOD_NIC:
956 return mlx5_query_hca_vport_pkey(mdev, 0, port, 0, index,
957 pkey);
958 default:
959 return -EINVAL;
960 }
Eli Cohene126ba92013-07-07 17:25:49 +0300961}
962
Eli Cohene126ba92013-07-07 17:25:49 +0300963static int mlx5_ib_modify_device(struct ib_device *ibdev, int mask,
964 struct ib_device_modify *props)
965{
966 struct mlx5_ib_dev *dev = to_mdev(ibdev);
967 struct mlx5_reg_node_desc in;
968 struct mlx5_reg_node_desc out;
969 int err;
970
971 if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
972 return -EOPNOTSUPP;
973
974 if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
975 return 0;
976
977 /*
978 * If possible, pass node desc to FW, so it can generate
979 * a 144 trap. If cmd fails, just ignore.
980 */
Yuval Shaiabd99fde2016-08-25 10:57:07 -0700981 memcpy(&in, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
Jack Morgenstein9603b612014-07-28 23:30:22 +0300982 err = mlx5_core_access_reg(dev->mdev, &in, sizeof(in), &out,
Eli Cohene126ba92013-07-07 17:25:49 +0300983 sizeof(out), MLX5_REG_NODE_DESC, 0, 1);
984 if (err)
985 return err;
986
Yuval Shaiabd99fde2016-08-25 10:57:07 -0700987 memcpy(ibdev->node_desc, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
Eli Cohene126ba92013-07-07 17:25:49 +0300988
989 return err;
990}
991
992static int mlx5_ib_modify_port(struct ib_device *ibdev, u8 port, int mask,
993 struct ib_port_modify *props)
994{
995 struct mlx5_ib_dev *dev = to_mdev(ibdev);
996 struct ib_port_attr attr;
997 u32 tmp;
998 int err;
999
1000 mutex_lock(&dev->cap_mask_mutex);
1001
1002 err = mlx5_ib_query_port(ibdev, port, &attr);
1003 if (err)
1004 goto out;
1005
1006 tmp = (attr.port_cap_flags | props->set_port_cap_mask) &
1007 ~props->clr_port_cap_mask;
1008
Jack Morgenstein9603b612014-07-28 23:30:22 +03001009 err = mlx5_set_port_caps(dev->mdev, port, tmp);
Eli Cohene126ba92013-07-07 17:25:49 +03001010
1011out:
1012 mutex_unlock(&dev->cap_mask_mutex);
1013 return err;
1014}
1015
Eli Cohen30aa60b2017-01-03 23:55:27 +02001016static void print_lib_caps(struct mlx5_ib_dev *dev, u64 caps)
1017{
1018 mlx5_ib_dbg(dev, "MLX5_LIB_CAP_4K_UAR = %s\n",
1019 caps & MLX5_LIB_CAP_4K_UAR ? "y" : "n");
1020}
1021
Eli Cohenb037c292017-01-03 23:55:26 +02001022static int calc_total_bfregs(struct mlx5_ib_dev *dev, bool lib_uar_4k,
1023 struct mlx5_ib_alloc_ucontext_req_v2 *req,
1024 u32 *num_sys_pages)
1025{
1026 int uars_per_sys_page;
1027 int bfregs_per_sys_page;
1028 int ref_bfregs = req->total_num_bfregs;
1029
1030 if (req->total_num_bfregs == 0)
1031 return -EINVAL;
1032
1033 BUILD_BUG_ON(MLX5_MAX_BFREGS % MLX5_NON_FP_BFREGS_IN_PAGE);
1034 BUILD_BUG_ON(MLX5_MAX_BFREGS < MLX5_NON_FP_BFREGS_IN_PAGE);
1035
1036 if (req->total_num_bfregs > MLX5_MAX_BFREGS)
1037 return -ENOMEM;
1038
1039 uars_per_sys_page = get_uars_per_sys_page(dev, lib_uar_4k);
1040 bfregs_per_sys_page = uars_per_sys_page * MLX5_NON_FP_BFREGS_PER_UAR;
1041 req->total_num_bfregs = ALIGN(req->total_num_bfregs, bfregs_per_sys_page);
1042 *num_sys_pages = req->total_num_bfregs / bfregs_per_sys_page;
1043
1044 if (req->num_low_latency_bfregs > req->total_num_bfregs - 1)
1045 return -EINVAL;
1046
1047 mlx5_ib_dbg(dev, "uar_4k: fw support %s, lib support %s, user requested %d bfregs, alloated %d, using %d sys pages\n",
1048 MLX5_CAP_GEN(dev->mdev, uar_4k) ? "yes" : "no",
1049 lib_uar_4k ? "yes" : "no", ref_bfregs,
1050 req->total_num_bfregs, *num_sys_pages);
1051
1052 return 0;
1053}
1054
1055static int allocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
1056{
1057 struct mlx5_bfreg_info *bfregi;
1058 int err;
1059 int i;
1060
1061 bfregi = &context->bfregi;
1062 for (i = 0; i < bfregi->num_sys_pages; i++) {
1063 err = mlx5_cmd_alloc_uar(dev->mdev, &bfregi->sys_pages[i]);
1064 if (err)
1065 goto error;
1066
1067 mlx5_ib_dbg(dev, "allocated uar %d\n", bfregi->sys_pages[i]);
1068 }
1069 return 0;
1070
1071error:
1072 for (--i; i >= 0; i--)
1073 if (mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]))
1074 mlx5_ib_warn(dev, "failed to free uar %d\n", i);
1075
1076 return err;
1077}
1078
1079static int deallocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
1080{
1081 struct mlx5_bfreg_info *bfregi;
1082 int err;
1083 int i;
1084
1085 bfregi = &context->bfregi;
1086 for (i = 0; i < bfregi->num_sys_pages; i++) {
1087 err = mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]);
1088 if (err) {
1089 mlx5_ib_warn(dev, "failed to free uar %d\n", i);
1090 return err;
1091 }
1092 }
1093 return 0;
1094}
1095
Eli Cohene126ba92013-07-07 17:25:49 +03001096static struct ib_ucontext *mlx5_ib_alloc_ucontext(struct ib_device *ibdev,
1097 struct ib_udata *udata)
1098{
1099 struct mlx5_ib_dev *dev = to_mdev(ibdev);
Matan Barakb368d7c2015-12-15 20:30:12 +02001100 struct mlx5_ib_alloc_ucontext_req_v2 req = {};
1101 struct mlx5_ib_alloc_ucontext_resp resp = {};
Eli Cohene126ba92013-07-07 17:25:49 +03001102 struct mlx5_ib_ucontext *context;
Eli Cohen2f5ff262017-01-03 23:55:21 +02001103 struct mlx5_bfreg_info *bfregi;
Eli Cohen78c0f982014-01-30 13:49:48 +02001104 int ver;
Eli Cohene126ba92013-07-07 17:25:49 +03001105 int err;
Jack Morgensteinf241e742014-07-28 23:30:23 +03001106 size_t reqlen;
Majd Dibbinya168a41c2016-01-28 17:51:47 +02001107 size_t min_req_v2 = offsetof(struct mlx5_ib_alloc_ucontext_req_v2,
1108 max_cqe_version);
Eli Cohenb037c292017-01-03 23:55:26 +02001109 bool lib_uar_4k;
Eli Cohene126ba92013-07-07 17:25:49 +03001110
1111 if (!dev->ib_active)
1112 return ERR_PTR(-EAGAIN);
1113
Haggai Abramovskydfbee852016-01-14 19:12:56 +02001114 if (udata->inlen < sizeof(struct ib_uverbs_cmd_hdr))
1115 return ERR_PTR(-EINVAL);
1116
Eli Cohen78c0f982014-01-30 13:49:48 +02001117 reqlen = udata->inlen - sizeof(struct ib_uverbs_cmd_hdr);
1118 if (reqlen == sizeof(struct mlx5_ib_alloc_ucontext_req))
1119 ver = 0;
Majd Dibbinya168a41c2016-01-28 17:51:47 +02001120 else if (reqlen >= min_req_v2)
Eli Cohen78c0f982014-01-30 13:49:48 +02001121 ver = 2;
1122 else
1123 return ERR_PTR(-EINVAL);
1124
Matan Barakb368d7c2015-12-15 20:30:12 +02001125 err = ib_copy_from_udata(&req, udata, min(reqlen, sizeof(req)));
Eli Cohene126ba92013-07-07 17:25:49 +03001126 if (err)
1127 return ERR_PTR(err);
1128
Matan Barakb368d7c2015-12-15 20:30:12 +02001129 if (req.flags)
Eli Cohen78c0f982014-01-30 13:49:48 +02001130 return ERR_PTR(-EINVAL);
1131
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001132 if (req.comp_mask || req.reserved0 || req.reserved1 || req.reserved2)
Matan Barakb368d7c2015-12-15 20:30:12 +02001133 return ERR_PTR(-EOPNOTSUPP);
1134
Eli Cohen2f5ff262017-01-03 23:55:21 +02001135 req.total_num_bfregs = ALIGN(req.total_num_bfregs,
1136 MLX5_NON_FP_BFREGS_PER_UAR);
1137 if (req.num_low_latency_bfregs > req.total_num_bfregs - 1)
Eli Cohene126ba92013-07-07 17:25:49 +03001138 return ERR_PTR(-EINVAL);
1139
Saeed Mahameed938fe832015-05-28 22:28:41 +03001140 resp.qp_tab_size = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp);
Noa Osherovich2cc6ad52016-06-04 15:15:33 +03001141 if (mlx5_core_is_pf(dev->mdev) && MLX5_CAP_GEN(dev->mdev, bf))
1142 resp.bf_reg_size = 1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size);
Daniel Jurgensb47bd6e2016-10-25 18:36:24 +03001143 resp.cache_line_size = cache_line_size();
Saeed Mahameed938fe832015-05-28 22:28:41 +03001144 resp.max_sq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq);
1145 resp.max_rq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq);
1146 resp.max_send_wqebb = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1147 resp.max_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1148 resp.max_srq_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz);
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001149 resp.cqe_version = min_t(__u8,
1150 (__u8)MLX5_CAP_GEN(dev->mdev, cqe_version),
1151 req.max_cqe_version);
Eli Cohen30aa60b2017-01-03 23:55:27 +02001152 resp.log_uar_size = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
1153 MLX5_ADAPTER_PAGE_SHIFT : PAGE_SHIFT;
1154 resp.num_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
1155 MLX5_CAP_GEN(dev->mdev, num_of_uars_per_page) : 1;
Matan Barakb368d7c2015-12-15 20:30:12 +02001156 resp.response_length = min(offsetof(typeof(resp), response_length) +
1157 sizeof(resp.response_length), udata->outlen);
Eli Cohene126ba92013-07-07 17:25:49 +03001158
1159 context = kzalloc(sizeof(*context), GFP_KERNEL);
1160 if (!context)
1161 return ERR_PTR(-ENOMEM);
1162
Eli Cohen30aa60b2017-01-03 23:55:27 +02001163 lib_uar_4k = req.lib_caps & MLX5_LIB_CAP_4K_UAR;
Eli Cohen2f5ff262017-01-03 23:55:21 +02001164 bfregi = &context->bfregi;
Eli Cohenb037c292017-01-03 23:55:26 +02001165
1166 /* updates req->total_num_bfregs */
1167 err = calc_total_bfregs(dev, lib_uar_4k, &req, &bfregi->num_sys_pages);
1168 if (err)
1169 goto out_ctx;
1170
Eli Cohen2f5ff262017-01-03 23:55:21 +02001171 mutex_init(&bfregi->lock);
Eli Cohenb037c292017-01-03 23:55:26 +02001172 bfregi->lib_uar_4k = lib_uar_4k;
1173 bfregi->count = kcalloc(req.total_num_bfregs, sizeof(*bfregi->count),
1174 GFP_KERNEL);
1175 if (!bfregi->count) {
Eli Cohene126ba92013-07-07 17:25:49 +03001176 err = -ENOMEM;
1177 goto out_ctx;
1178 }
1179
Eli Cohenb037c292017-01-03 23:55:26 +02001180 bfregi->sys_pages = kcalloc(bfregi->num_sys_pages,
1181 sizeof(*bfregi->sys_pages),
1182 GFP_KERNEL);
1183 if (!bfregi->sys_pages) {
Eli Cohene126ba92013-07-07 17:25:49 +03001184 err = -ENOMEM;
Eli Cohenb037c292017-01-03 23:55:26 +02001185 goto out_count;
Eli Cohene126ba92013-07-07 17:25:49 +03001186 }
1187
Eli Cohenb037c292017-01-03 23:55:26 +02001188 err = allocate_uars(dev, context);
1189 if (err)
1190 goto out_sys_pages;
Eli Cohene126ba92013-07-07 17:25:49 +03001191
Haggai Eranb4cfe442014-12-11 17:04:26 +02001192#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
1193 context->ibucontext.invalidate_range = &mlx5_ib_invalidate_range;
1194#endif
1195
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +02001196 context->upd_xlt_page = __get_free_page(GFP_KERNEL);
1197 if (!context->upd_xlt_page) {
1198 err = -ENOMEM;
1199 goto out_uars;
1200 }
1201 mutex_init(&context->upd_xlt_page_mutex);
1202
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001203 if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain)) {
1204 err = mlx5_core_alloc_transport_domain(dev->mdev,
1205 &context->tdn);
1206 if (err)
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +02001207 goto out_page;
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001208 }
1209
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001210 INIT_LIST_HEAD(&context->vma_private_list);
Eli Cohene126ba92013-07-07 17:25:49 +03001211 INIT_LIST_HEAD(&context->db_page_list);
1212 mutex_init(&context->db_page_mutex);
1213
Eli Cohen2f5ff262017-01-03 23:55:21 +02001214 resp.tot_bfregs = req.total_num_bfregs;
Saeed Mahameed938fe832015-05-28 22:28:41 +03001215 resp.num_ports = MLX5_CAP_GEN(dev->mdev, num_ports);
Matan Barakb368d7c2015-12-15 20:30:12 +02001216
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001217 if (field_avail(typeof(resp), cqe_version, udata->outlen))
1218 resp.response_length += sizeof(resp.cqe_version);
Matan Barakb368d7c2015-12-15 20:30:12 +02001219
Bodong Wang402ca532016-06-17 15:02:20 +03001220 if (field_avail(typeof(resp), cmds_supp_uhw, udata->outlen)) {
Moni Shoua6ad279c52016-11-23 08:23:23 +02001221 resp.cmds_supp_uhw |= MLX5_USER_CMDS_SUPP_UHW_QUERY_DEVICE |
1222 MLX5_USER_CMDS_SUPP_UHW_CREATE_AH;
Bodong Wang402ca532016-06-17 15:02:20 +03001223 resp.response_length += sizeof(resp.cmds_supp_uhw);
1224 }
1225
Noa Osherovichbc5c6ee2016-06-04 15:15:31 +03001226 /*
1227 * We don't want to expose information from the PCI bar that is located
1228 * after 4096 bytes, so if the arch only supports larger pages, let's
1229 * pretend we don't support reading the HCA's core clock. This is also
1230 * forced by mmap function.
1231 */
Eli Cohende8d6e02017-01-03 23:55:19 +02001232 if (field_avail(typeof(resp), hca_core_clock_offset, udata->outlen)) {
1233 if (PAGE_SIZE <= 4096) {
1234 resp.comp_mask |=
1235 MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_CORE_CLOCK_OFFSET;
1236 resp.hca_core_clock_offset =
1237 offsetof(struct mlx5_init_seg, internal_timer_h) % PAGE_SIZE;
1238 }
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001239 resp.response_length += sizeof(resp.hca_core_clock_offset) +
Bodong Wang402ca532016-06-17 15:02:20 +03001240 sizeof(resp.reserved2);
Matan Barakb368d7c2015-12-15 20:30:12 +02001241 }
1242
Eli Cohen30aa60b2017-01-03 23:55:27 +02001243 if (field_avail(typeof(resp), log_uar_size, udata->outlen))
1244 resp.response_length += sizeof(resp.log_uar_size);
1245
1246 if (field_avail(typeof(resp), num_uars_per_page, udata->outlen))
1247 resp.response_length += sizeof(resp.num_uars_per_page);
1248
Matan Barakb368d7c2015-12-15 20:30:12 +02001249 err = ib_copy_to_udata(udata, &resp, resp.response_length);
Eli Cohene126ba92013-07-07 17:25:49 +03001250 if (err)
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001251 goto out_td;
Eli Cohene126ba92013-07-07 17:25:49 +03001252
Eli Cohen2f5ff262017-01-03 23:55:21 +02001253 bfregi->ver = ver;
1254 bfregi->num_low_latency_bfregs = req.num_low_latency_bfregs;
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001255 context->cqe_version = resp.cqe_version;
Eli Cohen30aa60b2017-01-03 23:55:27 +02001256 context->lib_caps = req.lib_caps;
1257 print_lib_caps(dev, context->lib_caps);
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001258
Eli Cohene126ba92013-07-07 17:25:49 +03001259 return &context->ibucontext;
1260
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001261out_td:
1262 if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1263 mlx5_core_dealloc_transport_domain(dev->mdev, context->tdn);
1264
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +02001265out_page:
1266 free_page(context->upd_xlt_page);
1267
Eli Cohene126ba92013-07-07 17:25:49 +03001268out_uars:
Eli Cohenb037c292017-01-03 23:55:26 +02001269 deallocate_uars(dev, context);
1270
1271out_sys_pages:
1272 kfree(bfregi->sys_pages);
1273
Eli Cohene126ba92013-07-07 17:25:49 +03001274out_count:
Eli Cohen2f5ff262017-01-03 23:55:21 +02001275 kfree(bfregi->count);
Eli Cohene126ba92013-07-07 17:25:49 +03001276
Eli Cohene126ba92013-07-07 17:25:49 +03001277out_ctx:
1278 kfree(context);
Eli Cohenb037c292017-01-03 23:55:26 +02001279
Eli Cohene126ba92013-07-07 17:25:49 +03001280 return ERR_PTR(err);
1281}
1282
1283static int mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
1284{
1285 struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1286 struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
Eli Cohenb037c292017-01-03 23:55:26 +02001287 struct mlx5_bfreg_info *bfregi;
Eli Cohene126ba92013-07-07 17:25:49 +03001288
Eli Cohenb037c292017-01-03 23:55:26 +02001289 bfregi = &context->bfregi;
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001290 if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1291 mlx5_core_dealloc_transport_domain(dev->mdev, context->tdn);
1292
Artemy Kovalyov7d0cc6e2017-01-02 11:37:44 +02001293 free_page(context->upd_xlt_page);
Eli Cohenb037c292017-01-03 23:55:26 +02001294 deallocate_uars(dev, context);
1295 kfree(bfregi->sys_pages);
Eli Cohen2f5ff262017-01-03 23:55:21 +02001296 kfree(bfregi->count);
Eli Cohene126ba92013-07-07 17:25:49 +03001297 kfree(context);
1298
1299 return 0;
1300}
1301
Eli Cohenb037c292017-01-03 23:55:26 +02001302static phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev,
1303 struct mlx5_bfreg_info *bfregi,
1304 int idx)
Eli Cohene126ba92013-07-07 17:25:49 +03001305{
Eli Cohenb037c292017-01-03 23:55:26 +02001306 int fw_uars_per_page;
1307
1308 fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ? MLX5_UARS_IN_PAGE : 1;
1309
1310 return (pci_resource_start(dev->mdev->pdev, 0) >> PAGE_SHIFT) +
1311 bfregi->sys_pages[idx] / fw_uars_per_page;
Eli Cohene126ba92013-07-07 17:25:49 +03001312}
1313
1314static int get_command(unsigned long offset)
1315{
1316 return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK;
1317}
1318
1319static int get_arg(unsigned long offset)
1320{
1321 return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1);
1322}
1323
1324static int get_index(unsigned long offset)
1325{
1326 return get_arg(offset);
1327}
1328
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001329static void mlx5_ib_vma_open(struct vm_area_struct *area)
1330{
1331 /* vma_open is called when a new VMA is created on top of our VMA. This
1332 * is done through either mremap flow or split_vma (usually due to
1333 * mlock, madvise, munmap, etc.) We do not support a clone of the VMA,
1334 * as this VMA is strongly hardware related. Therefore we set the
1335 * vm_ops of the newly created/cloned VMA to NULL, to prevent it from
1336 * calling us again and trying to do incorrect actions. We assume that
1337 * the original VMA size is exactly a single page, and therefore all
1338 * "splitting" operation will not happen to it.
1339 */
1340 area->vm_ops = NULL;
1341}
1342
1343static void mlx5_ib_vma_close(struct vm_area_struct *area)
1344{
1345 struct mlx5_ib_vma_private_data *mlx5_ib_vma_priv_data;
1346
1347 /* It's guaranteed that all VMAs opened on a FD are closed before the
1348 * file itself is closed, therefore no sync is needed with the regular
1349 * closing flow. (e.g. mlx5 ib_dealloc_ucontext)
1350 * However need a sync with accessing the vma as part of
1351 * mlx5_ib_disassociate_ucontext.
1352 * The close operation is usually called under mm->mmap_sem except when
1353 * process is exiting.
1354 * The exiting case is handled explicitly as part of
1355 * mlx5_ib_disassociate_ucontext.
1356 */
1357 mlx5_ib_vma_priv_data = (struct mlx5_ib_vma_private_data *)area->vm_private_data;
1358
1359 /* setting the vma context pointer to null in the mlx5_ib driver's
1360 * private data, to protect a race condition in
1361 * mlx5_ib_disassociate_ucontext().
1362 */
1363 mlx5_ib_vma_priv_data->vma = NULL;
1364 list_del(&mlx5_ib_vma_priv_data->list);
1365 kfree(mlx5_ib_vma_priv_data);
1366}
1367
1368static const struct vm_operations_struct mlx5_ib_vm_ops = {
1369 .open = mlx5_ib_vma_open,
1370 .close = mlx5_ib_vma_close
1371};
1372
1373static int mlx5_ib_set_vma_data(struct vm_area_struct *vma,
1374 struct mlx5_ib_ucontext *ctx)
1375{
1376 struct mlx5_ib_vma_private_data *vma_prv;
1377 struct list_head *vma_head = &ctx->vma_private_list;
1378
1379 vma_prv = kzalloc(sizeof(*vma_prv), GFP_KERNEL);
1380 if (!vma_prv)
1381 return -ENOMEM;
1382
1383 vma_prv->vma = vma;
1384 vma->vm_private_data = vma_prv;
1385 vma->vm_ops = &mlx5_ib_vm_ops;
1386
1387 list_add(&vma_prv->list, vma_head);
1388
1389 return 0;
1390}
1391
1392static void mlx5_ib_disassociate_ucontext(struct ib_ucontext *ibcontext)
1393{
1394 int ret;
1395 struct vm_area_struct *vma;
1396 struct mlx5_ib_vma_private_data *vma_private, *n;
1397 struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1398 struct task_struct *owning_process = NULL;
1399 struct mm_struct *owning_mm = NULL;
1400
1401 owning_process = get_pid_task(ibcontext->tgid, PIDTYPE_PID);
1402 if (!owning_process)
1403 return;
1404
1405 owning_mm = get_task_mm(owning_process);
1406 if (!owning_mm) {
1407 pr_info("no mm, disassociate ucontext is pending task termination\n");
1408 while (1) {
1409 put_task_struct(owning_process);
1410 usleep_range(1000, 2000);
1411 owning_process = get_pid_task(ibcontext->tgid,
1412 PIDTYPE_PID);
1413 if (!owning_process ||
1414 owning_process->state == TASK_DEAD) {
1415 pr_info("disassociate ucontext done, task was terminated\n");
1416 /* in case task was dead need to release the
1417 * task struct.
1418 */
1419 if (owning_process)
1420 put_task_struct(owning_process);
1421 return;
1422 }
1423 }
1424 }
1425
1426 /* need to protect from a race on closing the vma as part of
1427 * mlx5_ib_vma_close.
1428 */
1429 down_read(&owning_mm->mmap_sem);
1430 list_for_each_entry_safe(vma_private, n, &context->vma_private_list,
1431 list) {
1432 vma = vma_private->vma;
1433 ret = zap_vma_ptes(vma, vma->vm_start,
1434 PAGE_SIZE);
1435 WARN_ONCE(ret, "%s: zap_vma_ptes failed", __func__);
1436 /* context going to be destroyed, should
1437 * not access ops any more.
1438 */
1439 vma->vm_ops = NULL;
1440 list_del(&vma_private->list);
1441 kfree(vma_private);
1442 }
1443 up_read(&owning_mm->mmap_sem);
1444 mmput(owning_mm);
1445 put_task_struct(owning_process);
1446}
1447
Guy Levi37aa5c32016-04-27 16:49:50 +03001448static inline char *mmap_cmd2str(enum mlx5_ib_mmap_cmd cmd)
1449{
1450 switch (cmd) {
1451 case MLX5_IB_MMAP_WC_PAGE:
1452 return "WC";
1453 case MLX5_IB_MMAP_REGULAR_PAGE:
1454 return "best effort WC";
1455 case MLX5_IB_MMAP_NC_PAGE:
1456 return "NC";
1457 default:
1458 return NULL;
1459 }
1460}
1461
1462static int uar_mmap(struct mlx5_ib_dev *dev, enum mlx5_ib_mmap_cmd cmd,
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001463 struct vm_area_struct *vma,
1464 struct mlx5_ib_ucontext *context)
Guy Levi37aa5c32016-04-27 16:49:50 +03001465{
Eli Cohen2f5ff262017-01-03 23:55:21 +02001466 struct mlx5_bfreg_info *bfregi = &context->bfregi;
Guy Levi37aa5c32016-04-27 16:49:50 +03001467 int err;
1468 unsigned long idx;
1469 phys_addr_t pfn, pa;
1470 pgprot_t prot;
Eli Cohenb037c292017-01-03 23:55:26 +02001471 int uars_per_page;
1472
1473 if (vma->vm_end - vma->vm_start != PAGE_SIZE)
1474 return -EINVAL;
1475
1476 uars_per_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k);
1477 idx = get_index(vma->vm_pgoff);
1478 if (idx % uars_per_page ||
1479 idx * uars_per_page >= bfregi->num_sys_pages) {
1480 mlx5_ib_warn(dev, "invalid uar index %lu\n", idx);
1481 return -EINVAL;
1482 }
Guy Levi37aa5c32016-04-27 16:49:50 +03001483
1484 switch (cmd) {
1485 case MLX5_IB_MMAP_WC_PAGE:
1486/* Some architectures don't support WC memory */
1487#if defined(CONFIG_X86)
1488 if (!pat_enabled())
1489 return -EPERM;
1490#elif !(defined(CONFIG_PPC) || (defined(CONFIG_ARM) && defined(CONFIG_MMU)))
1491 return -EPERM;
1492#endif
1493 /* fall through */
1494 case MLX5_IB_MMAP_REGULAR_PAGE:
1495 /* For MLX5_IB_MMAP_REGULAR_PAGE do the best effort to get WC */
1496 prot = pgprot_writecombine(vma->vm_page_prot);
1497 break;
1498 case MLX5_IB_MMAP_NC_PAGE:
1499 prot = pgprot_noncached(vma->vm_page_prot);
1500 break;
1501 default:
1502 return -EINVAL;
1503 }
1504
Eli Cohenb037c292017-01-03 23:55:26 +02001505 pfn = uar_index2pfn(dev, bfregi, idx);
Guy Levi37aa5c32016-04-27 16:49:50 +03001506 mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn %pa\n", idx, &pfn);
1507
1508 vma->vm_page_prot = prot;
1509 err = io_remap_pfn_range(vma, vma->vm_start, pfn,
1510 PAGE_SIZE, vma->vm_page_prot);
1511 if (err) {
1512 mlx5_ib_err(dev, "io_remap_pfn_range failed with error=%d, vm_start=0x%lx, pfn=%pa, mmap_cmd=%s\n",
1513 err, vma->vm_start, &pfn, mmap_cmd2str(cmd));
1514 return -EAGAIN;
1515 }
1516
1517 pa = pfn << PAGE_SHIFT;
1518 mlx5_ib_dbg(dev, "mapped %s at 0x%lx, PA %pa\n", mmap_cmd2str(cmd),
1519 vma->vm_start, &pa);
1520
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001521 return mlx5_ib_set_vma_data(vma, context);
Guy Levi37aa5c32016-04-27 16:49:50 +03001522}
1523
Eli Cohene126ba92013-07-07 17:25:49 +03001524static int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma)
1525{
1526 struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1527 struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
Eli Cohene126ba92013-07-07 17:25:49 +03001528 unsigned long command;
Eli Cohene126ba92013-07-07 17:25:49 +03001529 phys_addr_t pfn;
1530
1531 command = get_command(vma->vm_pgoff);
1532 switch (command) {
Guy Levi37aa5c32016-04-27 16:49:50 +03001533 case MLX5_IB_MMAP_WC_PAGE:
1534 case MLX5_IB_MMAP_NC_PAGE:
Eli Cohene126ba92013-07-07 17:25:49 +03001535 case MLX5_IB_MMAP_REGULAR_PAGE:
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001536 return uar_mmap(dev, command, vma, context);
Eli Cohene126ba92013-07-07 17:25:49 +03001537
1538 case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES:
1539 return -ENOSYS;
1540
Matan Barakd69e3bc2015-12-15 20:30:13 +02001541 case MLX5_IB_MMAP_CORE_CLOCK:
Matan Barakd69e3bc2015-12-15 20:30:13 +02001542 if (vma->vm_end - vma->vm_start != PAGE_SIZE)
1543 return -EINVAL;
1544
Matan Barak6cbac1e2016-04-14 16:52:10 +03001545 if (vma->vm_flags & VM_WRITE)
Matan Barakd69e3bc2015-12-15 20:30:13 +02001546 return -EPERM;
1547
1548 /* Don't expose to user-space information it shouldn't have */
1549 if (PAGE_SIZE > 4096)
1550 return -EOPNOTSUPP;
1551
1552 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1553 pfn = (dev->mdev->iseg_base +
1554 offsetof(struct mlx5_init_seg, internal_timer_h)) >>
1555 PAGE_SHIFT;
1556 if (io_remap_pfn_range(vma, vma->vm_start, pfn,
1557 PAGE_SIZE, vma->vm_page_prot))
1558 return -EAGAIN;
1559
1560 mlx5_ib_dbg(dev, "mapped internal timer at 0x%lx, PA 0x%llx\n",
1561 vma->vm_start,
1562 (unsigned long long)pfn << PAGE_SHIFT);
1563 break;
Matan Barakd69e3bc2015-12-15 20:30:13 +02001564
Eli Cohene126ba92013-07-07 17:25:49 +03001565 default:
1566 return -EINVAL;
1567 }
1568
1569 return 0;
1570}
1571
Eli Cohene126ba92013-07-07 17:25:49 +03001572static struct ib_pd *mlx5_ib_alloc_pd(struct ib_device *ibdev,
1573 struct ib_ucontext *context,
1574 struct ib_udata *udata)
1575{
1576 struct mlx5_ib_alloc_pd_resp resp;
1577 struct mlx5_ib_pd *pd;
1578 int err;
1579
1580 pd = kmalloc(sizeof(*pd), GFP_KERNEL);
1581 if (!pd)
1582 return ERR_PTR(-ENOMEM);
1583
Jack Morgenstein9603b612014-07-28 23:30:22 +03001584 err = mlx5_core_alloc_pd(to_mdev(ibdev)->mdev, &pd->pdn);
Eli Cohene126ba92013-07-07 17:25:49 +03001585 if (err) {
1586 kfree(pd);
1587 return ERR_PTR(err);
1588 }
1589
1590 if (context) {
1591 resp.pdn = pd->pdn;
1592 if (ib_copy_to_udata(udata, &resp, sizeof(resp))) {
Jack Morgenstein9603b612014-07-28 23:30:22 +03001593 mlx5_core_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn);
Eli Cohene126ba92013-07-07 17:25:49 +03001594 kfree(pd);
1595 return ERR_PTR(-EFAULT);
1596 }
Eli Cohene126ba92013-07-07 17:25:49 +03001597 }
1598
1599 return &pd->ibpd;
1600}
1601
1602static int mlx5_ib_dealloc_pd(struct ib_pd *pd)
1603{
1604 struct mlx5_ib_dev *mdev = to_mdev(pd->device);
1605 struct mlx5_ib_pd *mpd = to_mpd(pd);
1606
Jack Morgenstein9603b612014-07-28 23:30:22 +03001607 mlx5_core_dealloc_pd(mdev->mdev, mpd->pdn);
Eli Cohene126ba92013-07-07 17:25:49 +03001608 kfree(mpd);
1609
1610 return 0;
1611}
1612
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03001613enum {
1614 MATCH_CRITERIA_ENABLE_OUTER_BIT,
1615 MATCH_CRITERIA_ENABLE_MISC_BIT,
1616 MATCH_CRITERIA_ENABLE_INNER_BIT
1617};
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001618
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03001619#define HEADER_IS_ZERO(match_criteria, headers) \
1620 !(memchr_inv(MLX5_ADDR_OF(fte_match_param, match_criteria, headers), \
1621 0, MLX5_FLD_SZ_BYTES(fte_match_param, headers))) \
1622
1623static u8 get_match_criteria_enable(u32 *match_criteria)
1624{
1625 u8 match_criteria_enable;
1626
1627 match_criteria_enable =
1628 (!HEADER_IS_ZERO(match_criteria, outer_headers)) <<
1629 MATCH_CRITERIA_ENABLE_OUTER_BIT;
1630 match_criteria_enable |=
1631 (!HEADER_IS_ZERO(match_criteria, misc_parameters)) <<
1632 MATCH_CRITERIA_ENABLE_MISC_BIT;
1633 match_criteria_enable |=
1634 (!HEADER_IS_ZERO(match_criteria, inner_headers)) <<
1635 MATCH_CRITERIA_ENABLE_INNER_BIT;
1636
1637 return match_criteria_enable;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001638}
1639
Maor Gottliebca0d4752016-08-30 16:58:35 +03001640static void set_proto(void *outer_c, void *outer_v, u8 mask, u8 val)
1641{
1642 MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_protocol, mask);
1643 MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_protocol, val);
1644}
1645
Moses Reuben2d1e6972016-11-14 19:04:52 +02001646static void set_flow_label(void *misc_c, void *misc_v, u8 mask, u8 val,
1647 bool inner)
1648{
1649 if (inner) {
1650 MLX5_SET(fte_match_set_misc,
1651 misc_c, inner_ipv6_flow_label, mask);
1652 MLX5_SET(fte_match_set_misc,
1653 misc_v, inner_ipv6_flow_label, val);
1654 } else {
1655 MLX5_SET(fte_match_set_misc,
1656 misc_c, outer_ipv6_flow_label, mask);
1657 MLX5_SET(fte_match_set_misc,
1658 misc_v, outer_ipv6_flow_label, val);
1659 }
1660}
1661
Maor Gottliebca0d4752016-08-30 16:58:35 +03001662static void set_tos(void *outer_c, void *outer_v, u8 mask, u8 val)
1663{
1664 MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_ecn, mask);
1665 MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_ecn, val);
1666 MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_dscp, mask >> 2);
1667 MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_dscp, val >> 2);
1668}
1669
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03001670#define LAST_ETH_FIELD vlan_tag
1671#define LAST_IB_FIELD sl
Maor Gottliebca0d4752016-08-30 16:58:35 +03001672#define LAST_IPV4_FIELD tos
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03001673#define LAST_IPV6_FIELD traffic_class
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03001674#define LAST_TCP_UDP_FIELD src_port
Moses Reubenffb30d82016-11-14 19:04:50 +02001675#define LAST_TUNNEL_FIELD tunnel_id
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03001676
1677/* Field is the last supported field */
1678#define FIELDS_NOT_SUPPORTED(filter, field)\
1679 memchr_inv((void *)&filter.field +\
1680 sizeof(filter.field), 0,\
1681 sizeof(filter) -\
1682 offsetof(typeof(filter), field) -\
1683 sizeof(filter.field))
1684
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001685static int parse_flow_attr(u32 *match_c, u32 *match_v,
Maor Gottliebdd063d02016-08-28 14:16:32 +03001686 const union ib_flow_spec *ib_spec)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001687{
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03001688 void *misc_params_c = MLX5_ADDR_OF(fte_match_param, match_c,
1689 misc_parameters);
1690 void *misc_params_v = MLX5_ADDR_OF(fte_match_param, match_v,
1691 misc_parameters);
Moses Reuben2d1e6972016-11-14 19:04:52 +02001692 void *headers_c;
1693 void *headers_v;
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03001694
Moses Reuben2d1e6972016-11-14 19:04:52 +02001695 if (ib_spec->type & IB_FLOW_SPEC_INNER) {
1696 headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
1697 inner_headers);
1698 headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
1699 inner_headers);
1700 } else {
1701 headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
1702 outer_headers);
1703 headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
1704 outer_headers);
1705 }
1706
1707 switch (ib_spec->type & ~IB_FLOW_SPEC_INNER) {
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001708 case IB_FLOW_SPEC_ETH:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03001709 if (FIELDS_NOT_SUPPORTED(ib_spec->eth.mask, LAST_ETH_FIELD))
1710 return -ENOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001711
Moses Reuben2d1e6972016-11-14 19:04:52 +02001712 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001713 dmac_47_16),
1714 ib_spec->eth.mask.dst_mac);
Moses Reuben2d1e6972016-11-14 19:04:52 +02001715 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001716 dmac_47_16),
1717 ib_spec->eth.val.dst_mac);
1718
Moses Reuben2d1e6972016-11-14 19:04:52 +02001719 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottliebee3da802016-09-12 19:16:24 +03001720 smac_47_16),
1721 ib_spec->eth.mask.src_mac);
Moses Reuben2d1e6972016-11-14 19:04:52 +02001722 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottliebee3da802016-09-12 19:16:24 +03001723 smac_47_16),
1724 ib_spec->eth.val.src_mac);
1725
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001726 if (ib_spec->eth.mask.vlan_tag) {
Moses Reuben2d1e6972016-11-14 19:04:52 +02001727 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001728 vlan_tag, 1);
Moses Reuben2d1e6972016-11-14 19:04:52 +02001729 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001730 vlan_tag, 1);
1731
Moses Reuben2d1e6972016-11-14 19:04:52 +02001732 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001733 first_vid, ntohs(ib_spec->eth.mask.vlan_tag));
Moses Reuben2d1e6972016-11-14 19:04:52 +02001734 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001735 first_vid, ntohs(ib_spec->eth.val.vlan_tag));
1736
Moses Reuben2d1e6972016-11-14 19:04:52 +02001737 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001738 first_cfi,
1739 ntohs(ib_spec->eth.mask.vlan_tag) >> 12);
Moses Reuben2d1e6972016-11-14 19:04:52 +02001740 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001741 first_cfi,
1742 ntohs(ib_spec->eth.val.vlan_tag) >> 12);
1743
Moses Reuben2d1e6972016-11-14 19:04:52 +02001744 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001745 first_prio,
1746 ntohs(ib_spec->eth.mask.vlan_tag) >> 13);
Moses Reuben2d1e6972016-11-14 19:04:52 +02001747 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001748 first_prio,
1749 ntohs(ib_spec->eth.val.vlan_tag) >> 13);
1750 }
Moses Reuben2d1e6972016-11-14 19:04:52 +02001751 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001752 ethertype, ntohs(ib_spec->eth.mask.ether_type));
Moses Reuben2d1e6972016-11-14 19:04:52 +02001753 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001754 ethertype, ntohs(ib_spec->eth.val.ether_type));
1755 break;
1756 case IB_FLOW_SPEC_IPV4:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03001757 if (FIELDS_NOT_SUPPORTED(ib_spec->ipv4.mask, LAST_IPV4_FIELD))
1758 return -ENOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001759
Moses Reuben2d1e6972016-11-14 19:04:52 +02001760 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001761 ethertype, 0xffff);
Moses Reuben2d1e6972016-11-14 19:04:52 +02001762 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001763 ethertype, ETH_P_IP);
1764
Moses Reuben2d1e6972016-11-14 19:04:52 +02001765 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001766 src_ipv4_src_ipv6.ipv4_layout.ipv4),
1767 &ib_spec->ipv4.mask.src_ip,
1768 sizeof(ib_spec->ipv4.mask.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02001769 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001770 src_ipv4_src_ipv6.ipv4_layout.ipv4),
1771 &ib_spec->ipv4.val.src_ip,
1772 sizeof(ib_spec->ipv4.val.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02001773 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001774 dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
1775 &ib_spec->ipv4.mask.dst_ip,
1776 sizeof(ib_spec->ipv4.mask.dst_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02001777 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001778 dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
1779 &ib_spec->ipv4.val.dst_ip,
1780 sizeof(ib_spec->ipv4.val.dst_ip));
Maor Gottliebca0d4752016-08-30 16:58:35 +03001781
Moses Reuben2d1e6972016-11-14 19:04:52 +02001782 set_tos(headers_c, headers_v,
Maor Gottliebca0d4752016-08-30 16:58:35 +03001783 ib_spec->ipv4.mask.tos, ib_spec->ipv4.val.tos);
1784
Moses Reuben2d1e6972016-11-14 19:04:52 +02001785 set_proto(headers_c, headers_v,
Maor Gottliebca0d4752016-08-30 16:58:35 +03001786 ib_spec->ipv4.mask.proto, ib_spec->ipv4.val.proto);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001787 break;
Maor Gottlieb026bae02016-06-17 15:14:51 +03001788 case IB_FLOW_SPEC_IPV6:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03001789 if (FIELDS_NOT_SUPPORTED(ib_spec->ipv6.mask, LAST_IPV6_FIELD))
1790 return -ENOTSUPP;
Maor Gottlieb026bae02016-06-17 15:14:51 +03001791
Moses Reuben2d1e6972016-11-14 19:04:52 +02001792 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb026bae02016-06-17 15:14:51 +03001793 ethertype, 0xffff);
Moses Reuben2d1e6972016-11-14 19:04:52 +02001794 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb026bae02016-06-17 15:14:51 +03001795 ethertype, ETH_P_IPV6);
1796
Moses Reuben2d1e6972016-11-14 19:04:52 +02001797 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb026bae02016-06-17 15:14:51 +03001798 src_ipv4_src_ipv6.ipv6_layout.ipv6),
1799 &ib_spec->ipv6.mask.src_ip,
1800 sizeof(ib_spec->ipv6.mask.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02001801 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb026bae02016-06-17 15:14:51 +03001802 src_ipv4_src_ipv6.ipv6_layout.ipv6),
1803 &ib_spec->ipv6.val.src_ip,
1804 sizeof(ib_spec->ipv6.val.src_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02001805 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
Maor Gottlieb026bae02016-06-17 15:14:51 +03001806 dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
1807 &ib_spec->ipv6.mask.dst_ip,
1808 sizeof(ib_spec->ipv6.mask.dst_ip));
Moses Reuben2d1e6972016-11-14 19:04:52 +02001809 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
Maor Gottlieb026bae02016-06-17 15:14:51 +03001810 dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
1811 &ib_spec->ipv6.val.dst_ip,
1812 sizeof(ib_spec->ipv6.val.dst_ip));
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03001813
Moses Reuben2d1e6972016-11-14 19:04:52 +02001814 set_tos(headers_c, headers_v,
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03001815 ib_spec->ipv6.mask.traffic_class,
1816 ib_spec->ipv6.val.traffic_class);
1817
Moses Reuben2d1e6972016-11-14 19:04:52 +02001818 set_proto(headers_c, headers_v,
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03001819 ib_spec->ipv6.mask.next_hdr,
1820 ib_spec->ipv6.val.next_hdr);
1821
Moses Reuben2d1e6972016-11-14 19:04:52 +02001822 set_flow_label(misc_params_c, misc_params_v,
1823 ntohl(ib_spec->ipv6.mask.flow_label),
1824 ntohl(ib_spec->ipv6.val.flow_label),
1825 ib_spec->type & IB_FLOW_SPEC_INNER);
1826
Maor Gottlieb026bae02016-06-17 15:14:51 +03001827 break;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001828 case IB_FLOW_SPEC_TCP:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03001829 if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
1830 LAST_TCP_UDP_FIELD))
1831 return -ENOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001832
Moses Reuben2d1e6972016-11-14 19:04:52 +02001833 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001834 0xff);
Moses Reuben2d1e6972016-11-14 19:04:52 +02001835 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001836 IPPROTO_TCP);
1837
Moses Reuben2d1e6972016-11-14 19:04:52 +02001838 MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001839 ntohs(ib_spec->tcp_udp.mask.src_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02001840 MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001841 ntohs(ib_spec->tcp_udp.val.src_port));
1842
Moses Reuben2d1e6972016-11-14 19:04:52 +02001843 MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001844 ntohs(ib_spec->tcp_udp.mask.dst_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02001845 MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001846 ntohs(ib_spec->tcp_udp.val.dst_port));
1847 break;
1848 case IB_FLOW_SPEC_UDP:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03001849 if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
1850 LAST_TCP_UDP_FIELD))
1851 return -ENOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001852
Moses Reuben2d1e6972016-11-14 19:04:52 +02001853 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001854 0xff);
Moses Reuben2d1e6972016-11-14 19:04:52 +02001855 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001856 IPPROTO_UDP);
1857
Moses Reuben2d1e6972016-11-14 19:04:52 +02001858 MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001859 ntohs(ib_spec->tcp_udp.mask.src_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02001860 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_sport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001861 ntohs(ib_spec->tcp_udp.val.src_port));
1862
Moses Reuben2d1e6972016-11-14 19:04:52 +02001863 MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001864 ntohs(ib_spec->tcp_udp.mask.dst_port));
Moses Reuben2d1e6972016-11-14 19:04:52 +02001865 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001866 ntohs(ib_spec->tcp_udp.val.dst_port));
1867 break;
Moses Reubenffb30d82016-11-14 19:04:50 +02001868 case IB_FLOW_SPEC_VXLAN_TUNNEL:
1869 if (FIELDS_NOT_SUPPORTED(ib_spec->tunnel.mask,
1870 LAST_TUNNEL_FIELD))
1871 return -ENOTSUPP;
1872
1873 MLX5_SET(fte_match_set_misc, misc_params_c, vxlan_vni,
1874 ntohl(ib_spec->tunnel.mask.tunnel_id));
1875 MLX5_SET(fte_match_set_misc, misc_params_v, vxlan_vni,
1876 ntohl(ib_spec->tunnel.val.tunnel_id));
1877 break;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001878 default:
1879 return -EINVAL;
1880 }
1881
1882 return 0;
1883}
1884
1885/* If a flow could catch both multicast and unicast packets,
1886 * it won't fall into the multicast flow steering table and this rule
1887 * could steal other multicast packets.
1888 */
1889static bool flow_is_multicast_only(struct ib_flow_attr *ib_attr)
1890{
1891 struct ib_flow_spec_eth *eth_spec;
1892
1893 if (ib_attr->type != IB_FLOW_ATTR_NORMAL ||
1894 ib_attr->size < sizeof(struct ib_flow_attr) +
1895 sizeof(struct ib_flow_spec_eth) ||
1896 ib_attr->num_of_specs < 1)
1897 return false;
1898
1899 eth_spec = (struct ib_flow_spec_eth *)(ib_attr + 1);
1900 if (eth_spec->type != IB_FLOW_SPEC_ETH ||
1901 eth_spec->size != sizeof(*eth_spec))
1902 return false;
1903
1904 return is_multicast_ether_addr(eth_spec->mask.dst_mac) &&
1905 is_multicast_ether_addr(eth_spec->val.dst_mac);
1906}
1907
Maor Gottliebdd063d02016-08-28 14:16:32 +03001908static bool is_valid_attr(const struct ib_flow_attr *flow_attr)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001909{
1910 union ib_flow_spec *ib_spec = (union ib_flow_spec *)(flow_attr + 1);
1911 bool has_ipv4_spec = false;
1912 bool eth_type_ipv4 = true;
1913 unsigned int spec_index;
1914
1915 /* Validate that ethertype is correct */
1916 for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
1917 if (ib_spec->type == IB_FLOW_SPEC_ETH &&
1918 ib_spec->eth.mask.ether_type) {
1919 if (!((ib_spec->eth.mask.ether_type == htons(0xffff)) &&
1920 ib_spec->eth.val.ether_type == htons(ETH_P_IP)))
1921 eth_type_ipv4 = false;
1922 } else if (ib_spec->type == IB_FLOW_SPEC_IPV4) {
1923 has_ipv4_spec = true;
1924 }
1925 ib_spec = (void *)ib_spec + ib_spec->size;
1926 }
1927 return !has_ipv4_spec || eth_type_ipv4;
1928}
1929
1930static void put_flow_table(struct mlx5_ib_dev *dev,
1931 struct mlx5_ib_flow_prio *prio, bool ft_added)
1932{
1933 prio->refcount -= !!ft_added;
1934 if (!prio->refcount) {
1935 mlx5_destroy_flow_table(prio->flow_table);
1936 prio->flow_table = NULL;
1937 }
1938}
1939
1940static int mlx5_ib_destroy_flow(struct ib_flow *flow_id)
1941{
1942 struct mlx5_ib_dev *dev = to_mdev(flow_id->qp->device);
1943 struct mlx5_ib_flow_handler *handler = container_of(flow_id,
1944 struct mlx5_ib_flow_handler,
1945 ibflow);
1946 struct mlx5_ib_flow_handler *iter, *tmp;
1947
1948 mutex_lock(&dev->flow_db.lock);
1949
1950 list_for_each_entry_safe(iter, tmp, &handler->list, list) {
Mark Bloch74491de2016-08-31 11:24:25 +00001951 mlx5_del_flow_rules(iter->rule);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03001952 put_flow_table(dev, iter->prio, true);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001953 list_del(&iter->list);
1954 kfree(iter);
1955 }
1956
Mark Bloch74491de2016-08-31 11:24:25 +00001957 mlx5_del_flow_rules(handler->rule);
Maor Gottlieb5497adc2016-08-28 14:16:31 +03001958 put_flow_table(dev, handler->prio, true);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001959 mutex_unlock(&dev->flow_db.lock);
1960
1961 kfree(handler);
1962
1963 return 0;
1964}
1965
Maor Gottlieb35d190112016-03-07 18:51:47 +02001966static int ib_prio_to_core_prio(unsigned int priority, bool dont_trap)
1967{
1968 priority *= 2;
1969 if (!dont_trap)
1970 priority++;
1971 return priority;
1972}
1973
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03001974enum flow_table_type {
1975 MLX5_IB_FT_RX,
1976 MLX5_IB_FT_TX
1977};
1978
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001979#define MLX5_FS_MAX_TYPES 10
1980#define MLX5_FS_MAX_ENTRIES 32000UL
1981static struct mlx5_ib_flow_prio *get_flow_table(struct mlx5_ib_dev *dev,
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03001982 struct ib_flow_attr *flow_attr,
1983 enum flow_table_type ft_type)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001984{
Maor Gottlieb35d190112016-03-07 18:51:47 +02001985 bool dont_trap = flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001986 struct mlx5_flow_namespace *ns = NULL;
1987 struct mlx5_ib_flow_prio *prio;
1988 struct mlx5_flow_table *ft;
1989 int num_entries;
1990 int num_groups;
1991 int priority;
1992 int err = 0;
1993
1994 if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
Maor Gottlieb35d190112016-03-07 18:51:47 +02001995 if (flow_is_multicast_only(flow_attr) &&
1996 !dont_trap)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001997 priority = MLX5_IB_FLOW_MCAST_PRIO;
1998 else
Maor Gottlieb35d190112016-03-07 18:51:47 +02001999 priority = ib_prio_to_core_prio(flow_attr->priority,
2000 dont_trap);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002001 ns = mlx5_get_flow_namespace(dev->mdev,
2002 MLX5_FLOW_NAMESPACE_BYPASS);
2003 num_entries = MLX5_FS_MAX_ENTRIES;
2004 num_groups = MLX5_FS_MAX_TYPES;
2005 prio = &dev->flow_db.prios[priority];
2006 } else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
2007 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
2008 ns = mlx5_get_flow_namespace(dev->mdev,
2009 MLX5_FLOW_NAMESPACE_LEFTOVERS);
2010 build_leftovers_ft_param(&priority,
2011 &num_entries,
2012 &num_groups);
2013 prio = &dev->flow_db.prios[MLX5_IB_FLOW_LEFTOVERS_PRIO];
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002014 } else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
2015 if (!MLX5_CAP_FLOWTABLE(dev->mdev,
2016 allow_sniffer_and_nic_rx_shared_tir))
2017 return ERR_PTR(-ENOTSUPP);
2018
2019 ns = mlx5_get_flow_namespace(dev->mdev, ft_type == MLX5_IB_FT_RX ?
2020 MLX5_FLOW_NAMESPACE_SNIFFER_RX :
2021 MLX5_FLOW_NAMESPACE_SNIFFER_TX);
2022
2023 prio = &dev->flow_db.sniffer[ft_type];
2024 priority = 0;
2025 num_entries = 1;
2026 num_groups = 1;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002027 }
2028
2029 if (!ns)
2030 return ERR_PTR(-ENOTSUPP);
2031
2032 ft = prio->flow_table;
2033 if (!ft) {
2034 ft = mlx5_create_auto_grouped_flow_table(ns, priority,
2035 num_entries,
Maor Gottliebd63cd282016-04-29 01:36:35 +03002036 num_groups,
Hadar Hen Zionc9f1b072016-11-07 15:14:44 +02002037 0, 0);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002038
2039 if (!IS_ERR(ft)) {
2040 prio->refcount = 0;
2041 prio->flow_table = ft;
2042 } else {
2043 err = PTR_ERR(ft);
2044 }
2045 }
2046
2047 return err ? ERR_PTR(err) : prio;
2048}
2049
2050static struct mlx5_ib_flow_handler *create_flow_rule(struct mlx5_ib_dev *dev,
2051 struct mlx5_ib_flow_prio *ft_prio,
Maor Gottliebdd063d02016-08-28 14:16:32 +03002052 const struct ib_flow_attr *flow_attr,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002053 struct mlx5_flow_destination *dst)
2054{
2055 struct mlx5_flow_table *ft = ft_prio->flow_table;
2056 struct mlx5_ib_flow_handler *handler;
Hadar Hen Zion66958ed2016-11-07 15:14:45 +02002057 struct mlx5_flow_act flow_act = {0};
Maor Gottliebc5bb1732016-07-04 17:23:05 +03002058 struct mlx5_flow_spec *spec;
Maor Gottliebdd063d02016-08-28 14:16:32 +03002059 const void *ib_flow = (const void *)flow_attr + sizeof(*flow_attr);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002060 unsigned int spec_index;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002061 int err = 0;
2062
2063 if (!is_valid_attr(flow_attr))
2064 return ERR_PTR(-EINVAL);
2065
Maor Gottliebc5bb1732016-07-04 17:23:05 +03002066 spec = mlx5_vzalloc(sizeof(*spec));
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002067 handler = kzalloc(sizeof(*handler), GFP_KERNEL);
Maor Gottliebc5bb1732016-07-04 17:23:05 +03002068 if (!handler || !spec) {
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002069 err = -ENOMEM;
2070 goto free;
2071 }
2072
2073 INIT_LIST_HEAD(&handler->list);
2074
2075 for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
Maor Gottliebc5bb1732016-07-04 17:23:05 +03002076 err = parse_flow_attr(spec->match_criteria,
2077 spec->match_value, ib_flow);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002078 if (err < 0)
2079 goto free;
2080
2081 ib_flow += ((union ib_flow_spec *)ib_flow)->size;
2082 }
2083
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03002084 spec->match_criteria_enable = get_match_criteria_enable(spec->match_criteria);
Hadar Hen Zion66958ed2016-11-07 15:14:45 +02002085 flow_act.action = dst ? MLX5_FLOW_CONTEXT_ACTION_FWD_DEST :
Maor Gottlieb35d190112016-03-07 18:51:47 +02002086 MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO;
Hadar Hen Zion66958ed2016-11-07 15:14:45 +02002087 flow_act.flow_tag = MLX5_FS_DEFAULT_FLOW_TAG;
Mark Bloch74491de2016-08-31 11:24:25 +00002088 handler->rule = mlx5_add_flow_rules(ft, spec,
Hadar Hen Zion66958ed2016-11-07 15:14:45 +02002089 &flow_act,
2090 dst, 1);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002091
2092 if (IS_ERR(handler->rule)) {
2093 err = PTR_ERR(handler->rule);
2094 goto free;
2095 }
2096
Maor Gottliebd9d49802016-08-28 14:16:33 +03002097 ft_prio->refcount++;
Maor Gottlieb5497adc2016-08-28 14:16:31 +03002098 handler->prio = ft_prio;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002099
2100 ft_prio->flow_table = ft;
2101free:
2102 if (err)
2103 kfree(handler);
Maor Gottliebc5bb1732016-07-04 17:23:05 +03002104 kvfree(spec);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002105 return err ? ERR_PTR(err) : handler;
2106}
2107
Maor Gottlieb35d190112016-03-07 18:51:47 +02002108static struct mlx5_ib_flow_handler *create_dont_trap_rule(struct mlx5_ib_dev *dev,
2109 struct mlx5_ib_flow_prio *ft_prio,
2110 struct ib_flow_attr *flow_attr,
2111 struct mlx5_flow_destination *dst)
2112{
2113 struct mlx5_ib_flow_handler *handler_dst = NULL;
2114 struct mlx5_ib_flow_handler *handler = NULL;
2115
2116 handler = create_flow_rule(dev, ft_prio, flow_attr, NULL);
2117 if (!IS_ERR(handler)) {
2118 handler_dst = create_flow_rule(dev, ft_prio,
2119 flow_attr, dst);
2120 if (IS_ERR(handler_dst)) {
Mark Bloch74491de2016-08-31 11:24:25 +00002121 mlx5_del_flow_rules(handler->rule);
Maor Gottliebd9d49802016-08-28 14:16:33 +03002122 ft_prio->refcount--;
Maor Gottlieb35d190112016-03-07 18:51:47 +02002123 kfree(handler);
2124 handler = handler_dst;
2125 } else {
2126 list_add(&handler_dst->list, &handler->list);
2127 }
2128 }
2129
2130 return handler;
2131}
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002132enum {
2133 LEFTOVERS_MC,
2134 LEFTOVERS_UC,
2135};
2136
2137static struct mlx5_ib_flow_handler *create_leftovers_rule(struct mlx5_ib_dev *dev,
2138 struct mlx5_ib_flow_prio *ft_prio,
2139 struct ib_flow_attr *flow_attr,
2140 struct mlx5_flow_destination *dst)
2141{
2142 struct mlx5_ib_flow_handler *handler_ucast = NULL;
2143 struct mlx5_ib_flow_handler *handler = NULL;
2144
2145 static struct {
2146 struct ib_flow_attr flow_attr;
2147 struct ib_flow_spec_eth eth_flow;
2148 } leftovers_specs[] = {
2149 [LEFTOVERS_MC] = {
2150 .flow_attr = {
2151 .num_of_specs = 1,
2152 .size = sizeof(leftovers_specs[0])
2153 },
2154 .eth_flow = {
2155 .type = IB_FLOW_SPEC_ETH,
2156 .size = sizeof(struct ib_flow_spec_eth),
2157 .mask = {.dst_mac = {0x1} },
2158 .val = {.dst_mac = {0x1} }
2159 }
2160 },
2161 [LEFTOVERS_UC] = {
2162 .flow_attr = {
2163 .num_of_specs = 1,
2164 .size = sizeof(leftovers_specs[0])
2165 },
2166 .eth_flow = {
2167 .type = IB_FLOW_SPEC_ETH,
2168 .size = sizeof(struct ib_flow_spec_eth),
2169 .mask = {.dst_mac = {0x1} },
2170 .val = {.dst_mac = {} }
2171 }
2172 }
2173 };
2174
2175 handler = create_flow_rule(dev, ft_prio,
2176 &leftovers_specs[LEFTOVERS_MC].flow_attr,
2177 dst);
2178 if (!IS_ERR(handler) &&
2179 flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT) {
2180 handler_ucast = create_flow_rule(dev, ft_prio,
2181 &leftovers_specs[LEFTOVERS_UC].flow_attr,
2182 dst);
2183 if (IS_ERR(handler_ucast)) {
Mark Bloch74491de2016-08-31 11:24:25 +00002184 mlx5_del_flow_rules(handler->rule);
Maor Gottliebd9d49802016-08-28 14:16:33 +03002185 ft_prio->refcount--;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002186 kfree(handler);
2187 handler = handler_ucast;
2188 } else {
2189 list_add(&handler_ucast->list, &handler->list);
2190 }
2191 }
2192
2193 return handler;
2194}
2195
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002196static struct mlx5_ib_flow_handler *create_sniffer_rule(struct mlx5_ib_dev *dev,
2197 struct mlx5_ib_flow_prio *ft_rx,
2198 struct mlx5_ib_flow_prio *ft_tx,
2199 struct mlx5_flow_destination *dst)
2200{
2201 struct mlx5_ib_flow_handler *handler_rx;
2202 struct mlx5_ib_flow_handler *handler_tx;
2203 int err;
2204 static const struct ib_flow_attr flow_attr = {
2205 .num_of_specs = 0,
2206 .size = sizeof(flow_attr)
2207 };
2208
2209 handler_rx = create_flow_rule(dev, ft_rx, &flow_attr, dst);
2210 if (IS_ERR(handler_rx)) {
2211 err = PTR_ERR(handler_rx);
2212 goto err;
2213 }
2214
2215 handler_tx = create_flow_rule(dev, ft_tx, &flow_attr, dst);
2216 if (IS_ERR(handler_tx)) {
2217 err = PTR_ERR(handler_tx);
2218 goto err_tx;
2219 }
2220
2221 list_add(&handler_tx->list, &handler_rx->list);
2222
2223 return handler_rx;
2224
2225err_tx:
Mark Bloch74491de2016-08-31 11:24:25 +00002226 mlx5_del_flow_rules(handler_rx->rule);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002227 ft_rx->refcount--;
2228 kfree(handler_rx);
2229err:
2230 return ERR_PTR(err);
2231}
2232
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002233static struct ib_flow *mlx5_ib_create_flow(struct ib_qp *qp,
2234 struct ib_flow_attr *flow_attr,
2235 int domain)
2236{
2237 struct mlx5_ib_dev *dev = to_mdev(qp->device);
Yishai Hadasd9f88e52016-08-28 10:58:37 +03002238 struct mlx5_ib_qp *mqp = to_mqp(qp);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002239 struct mlx5_ib_flow_handler *handler = NULL;
2240 struct mlx5_flow_destination *dst = NULL;
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002241 struct mlx5_ib_flow_prio *ft_prio_tx = NULL;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002242 struct mlx5_ib_flow_prio *ft_prio;
2243 int err;
2244
2245 if (flow_attr->priority > MLX5_IB_FLOW_LAST_PRIO)
2246 return ERR_PTR(-ENOSPC);
2247
2248 if (domain != IB_FLOW_DOMAIN_USER ||
2249 flow_attr->port > MLX5_CAP_GEN(dev->mdev, num_ports) ||
Maor Gottlieb35d190112016-03-07 18:51:47 +02002250 (flow_attr->flags & ~IB_FLOW_ATTR_FLAGS_DONT_TRAP))
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002251 return ERR_PTR(-EINVAL);
2252
2253 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
2254 if (!dst)
2255 return ERR_PTR(-ENOMEM);
2256
2257 mutex_lock(&dev->flow_db.lock);
2258
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002259 ft_prio = get_flow_table(dev, flow_attr, MLX5_IB_FT_RX);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002260 if (IS_ERR(ft_prio)) {
2261 err = PTR_ERR(ft_prio);
2262 goto unlock;
2263 }
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002264 if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
2265 ft_prio_tx = get_flow_table(dev, flow_attr, MLX5_IB_FT_TX);
2266 if (IS_ERR(ft_prio_tx)) {
2267 err = PTR_ERR(ft_prio_tx);
2268 ft_prio_tx = NULL;
2269 goto destroy_ft;
2270 }
2271 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002272
2273 dst->type = MLX5_FLOW_DESTINATION_TYPE_TIR;
Yishai Hadasd9f88e52016-08-28 10:58:37 +03002274 if (mqp->flags & MLX5_IB_QP_RSS)
2275 dst->tir_num = mqp->rss_qp.tirn;
2276 else
2277 dst->tir_num = mqp->raw_packet_qp.rq.tirn;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002278
2279 if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
Maor Gottlieb35d190112016-03-07 18:51:47 +02002280 if (flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP) {
2281 handler = create_dont_trap_rule(dev, ft_prio,
2282 flow_attr, dst);
2283 } else {
2284 handler = create_flow_rule(dev, ft_prio, flow_attr,
2285 dst);
2286 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002287 } else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
2288 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
2289 handler = create_leftovers_rule(dev, ft_prio, flow_attr,
2290 dst);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002291 } else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
2292 handler = create_sniffer_rule(dev, ft_prio, ft_prio_tx, dst);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002293 } else {
2294 err = -EINVAL;
2295 goto destroy_ft;
2296 }
2297
2298 if (IS_ERR(handler)) {
2299 err = PTR_ERR(handler);
2300 handler = NULL;
2301 goto destroy_ft;
2302 }
2303
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002304 mutex_unlock(&dev->flow_db.lock);
2305 kfree(dst);
2306
2307 return &handler->ibflow;
2308
2309destroy_ft:
2310 put_flow_table(dev, ft_prio, false);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002311 if (ft_prio_tx)
2312 put_flow_table(dev, ft_prio_tx, false);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002313unlock:
2314 mutex_unlock(&dev->flow_db.lock);
2315 kfree(dst);
2316 kfree(handler);
2317 return ERR_PTR(err);
2318}
2319
Eli Cohene126ba92013-07-07 17:25:49 +03002320static int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
2321{
2322 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
2323 int err;
2324
Jack Morgenstein9603b612014-07-28 23:30:22 +03002325 err = mlx5_core_attach_mcg(dev->mdev, gid, ibqp->qp_num);
Eli Cohene126ba92013-07-07 17:25:49 +03002326 if (err)
2327 mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n",
2328 ibqp->qp_num, gid->raw);
2329
2330 return err;
2331}
2332
2333static int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
2334{
2335 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
2336 int err;
2337
Jack Morgenstein9603b612014-07-28 23:30:22 +03002338 err = mlx5_core_detach_mcg(dev->mdev, gid, ibqp->qp_num);
Eli Cohene126ba92013-07-07 17:25:49 +03002339 if (err)
2340 mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n",
2341 ibqp->qp_num, gid->raw);
2342
2343 return err;
2344}
2345
2346static int init_node_data(struct mlx5_ib_dev *dev)
2347{
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03002348 int err;
Eli Cohene126ba92013-07-07 17:25:49 +03002349
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03002350 err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc);
Eli Cohene126ba92013-07-07 17:25:49 +03002351 if (err)
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03002352 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03002353
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03002354 dev->mdev->rev_id = dev->mdev->pdev->revision;
Eli Cohene126ba92013-07-07 17:25:49 +03002355
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03002356 return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid);
Eli Cohene126ba92013-07-07 17:25:49 +03002357}
2358
2359static ssize_t show_fw_pages(struct device *device, struct device_attribute *attr,
2360 char *buf)
2361{
2362 struct mlx5_ib_dev *dev =
2363 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
2364
Jack Morgenstein9603b612014-07-28 23:30:22 +03002365 return sprintf(buf, "%d\n", dev->mdev->priv.fw_pages);
Eli Cohene126ba92013-07-07 17:25:49 +03002366}
2367
2368static ssize_t show_reg_pages(struct device *device,
2369 struct device_attribute *attr, char *buf)
2370{
2371 struct mlx5_ib_dev *dev =
2372 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
2373
Haggai Eran6aec21f2014-12-11 17:04:23 +02002374 return sprintf(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages));
Eli Cohene126ba92013-07-07 17:25:49 +03002375}
2376
2377static ssize_t show_hca(struct device *device, struct device_attribute *attr,
2378 char *buf)
2379{
2380 struct mlx5_ib_dev *dev =
2381 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
Jack Morgenstein9603b612014-07-28 23:30:22 +03002382 return sprintf(buf, "MT%d\n", dev->mdev->pdev->device);
Eli Cohene126ba92013-07-07 17:25:49 +03002383}
2384
Eli Cohene126ba92013-07-07 17:25:49 +03002385static ssize_t show_rev(struct device *device, struct device_attribute *attr,
2386 char *buf)
2387{
2388 struct mlx5_ib_dev *dev =
2389 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
Jack Morgenstein9603b612014-07-28 23:30:22 +03002390 return sprintf(buf, "%x\n", dev->mdev->rev_id);
Eli Cohene126ba92013-07-07 17:25:49 +03002391}
2392
2393static ssize_t show_board(struct device *device, struct device_attribute *attr,
2394 char *buf)
2395{
2396 struct mlx5_ib_dev *dev =
2397 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
2398 return sprintf(buf, "%.*s\n", MLX5_BOARD_ID_LEN,
Jack Morgenstein9603b612014-07-28 23:30:22 +03002399 dev->mdev->board_id);
Eli Cohene126ba92013-07-07 17:25:49 +03002400}
2401
2402static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03002403static DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL);
2404static DEVICE_ATTR(board_id, S_IRUGO, show_board, NULL);
2405static DEVICE_ATTR(fw_pages, S_IRUGO, show_fw_pages, NULL);
2406static DEVICE_ATTR(reg_pages, S_IRUGO, show_reg_pages, NULL);
2407
2408static struct device_attribute *mlx5_class_attributes[] = {
2409 &dev_attr_hw_rev,
Eli Cohene126ba92013-07-07 17:25:49 +03002410 &dev_attr_hca_type,
2411 &dev_attr_board_id,
2412 &dev_attr_fw_pages,
2413 &dev_attr_reg_pages,
2414};
2415
Haggai Eran7722f472016-02-29 15:45:07 +02002416static void pkey_change_handler(struct work_struct *work)
2417{
2418 struct mlx5_ib_port_resources *ports =
2419 container_of(work, struct mlx5_ib_port_resources,
2420 pkey_change_work);
2421
2422 mutex_lock(&ports->devr->mutex);
2423 mlx5_ib_gsi_pkey_change(ports->gsi);
2424 mutex_unlock(&ports->devr->mutex);
2425}
2426
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002427static void mlx5_ib_handle_internal_error(struct mlx5_ib_dev *ibdev)
2428{
2429 struct mlx5_ib_qp *mqp;
2430 struct mlx5_ib_cq *send_mcq, *recv_mcq;
2431 struct mlx5_core_cq *mcq;
2432 struct list_head cq_armed_list;
2433 unsigned long flags_qp;
2434 unsigned long flags_cq;
2435 unsigned long flags;
2436
2437 INIT_LIST_HEAD(&cq_armed_list);
2438
2439 /* Go over qp list reside on that ibdev, sync with create/destroy qp.*/
2440 spin_lock_irqsave(&ibdev->reset_flow_resource_lock, flags);
2441 list_for_each_entry(mqp, &ibdev->qp_list, qps_list) {
2442 spin_lock_irqsave(&mqp->sq.lock, flags_qp);
2443 if (mqp->sq.tail != mqp->sq.head) {
2444 send_mcq = to_mcq(mqp->ibqp.send_cq);
2445 spin_lock_irqsave(&send_mcq->lock, flags_cq);
2446 if (send_mcq->mcq.comp &&
2447 mqp->ibqp.send_cq->comp_handler) {
2448 if (!send_mcq->mcq.reset_notify_added) {
2449 send_mcq->mcq.reset_notify_added = 1;
2450 list_add_tail(&send_mcq->mcq.reset_notify,
2451 &cq_armed_list);
2452 }
2453 }
2454 spin_unlock_irqrestore(&send_mcq->lock, flags_cq);
2455 }
2456 spin_unlock_irqrestore(&mqp->sq.lock, flags_qp);
2457 spin_lock_irqsave(&mqp->rq.lock, flags_qp);
2458 /* no handling is needed for SRQ */
2459 if (!mqp->ibqp.srq) {
2460 if (mqp->rq.tail != mqp->rq.head) {
2461 recv_mcq = to_mcq(mqp->ibqp.recv_cq);
2462 spin_lock_irqsave(&recv_mcq->lock, flags_cq);
2463 if (recv_mcq->mcq.comp &&
2464 mqp->ibqp.recv_cq->comp_handler) {
2465 if (!recv_mcq->mcq.reset_notify_added) {
2466 recv_mcq->mcq.reset_notify_added = 1;
2467 list_add_tail(&recv_mcq->mcq.reset_notify,
2468 &cq_armed_list);
2469 }
2470 }
2471 spin_unlock_irqrestore(&recv_mcq->lock,
2472 flags_cq);
2473 }
2474 }
2475 spin_unlock_irqrestore(&mqp->rq.lock, flags_qp);
2476 }
2477 /*At that point all inflight post send were put to be executed as of we
2478 * lock/unlock above locks Now need to arm all involved CQs.
2479 */
2480 list_for_each_entry(mcq, &cq_armed_list, reset_notify) {
2481 mcq->comp(mcq);
2482 }
2483 spin_unlock_irqrestore(&ibdev->reset_flow_resource_lock, flags);
2484}
2485
Jack Morgenstein9603b612014-07-28 23:30:22 +03002486static void mlx5_ib_event(struct mlx5_core_dev *dev, void *context,
Jack Morgenstein4d2f9bb2014-07-28 23:30:24 +03002487 enum mlx5_dev_event event, unsigned long param)
Eli Cohene126ba92013-07-07 17:25:49 +03002488{
Jack Morgenstein9603b612014-07-28 23:30:22 +03002489 struct mlx5_ib_dev *ibdev = (struct mlx5_ib_dev *)context;
Eli Cohene126ba92013-07-07 17:25:49 +03002490 struct ib_event ibev;
Eli Cohendbaaff22016-10-27 16:36:44 +03002491 bool fatal = false;
Eli Cohene126ba92013-07-07 17:25:49 +03002492 u8 port = 0;
2493
2494 switch (event) {
2495 case MLX5_DEV_EVENT_SYS_ERROR:
Eli Cohene126ba92013-07-07 17:25:49 +03002496 ibev.event = IB_EVENT_DEVICE_FATAL;
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002497 mlx5_ib_handle_internal_error(ibdev);
Eli Cohendbaaff22016-10-27 16:36:44 +03002498 fatal = true;
Eli Cohene126ba92013-07-07 17:25:49 +03002499 break;
2500
2501 case MLX5_DEV_EVENT_PORT_UP:
Eli Cohene126ba92013-07-07 17:25:49 +03002502 case MLX5_DEV_EVENT_PORT_DOWN:
Noa Osherovich2788cf32016-06-04 15:15:29 +03002503 case MLX5_DEV_EVENT_PORT_INITIALIZED:
Jack Morgenstein4d2f9bb2014-07-28 23:30:24 +03002504 port = (u8)param;
Aviv Heller5ec8c832016-09-18 20:48:00 +03002505
2506 /* In RoCE, port up/down events are handled in
2507 * mlx5_netdev_event().
2508 */
2509 if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) ==
2510 IB_LINK_LAYER_ETHERNET)
2511 return;
2512
2513 ibev.event = (event == MLX5_DEV_EVENT_PORT_UP) ?
2514 IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
Eli Cohene126ba92013-07-07 17:25:49 +03002515 break;
2516
Eli Cohene126ba92013-07-07 17:25:49 +03002517 case MLX5_DEV_EVENT_LID_CHANGE:
2518 ibev.event = IB_EVENT_LID_CHANGE;
Jack Morgenstein4d2f9bb2014-07-28 23:30:24 +03002519 port = (u8)param;
Eli Cohene126ba92013-07-07 17:25:49 +03002520 break;
2521
2522 case MLX5_DEV_EVENT_PKEY_CHANGE:
2523 ibev.event = IB_EVENT_PKEY_CHANGE;
Jack Morgenstein4d2f9bb2014-07-28 23:30:24 +03002524 port = (u8)param;
Haggai Eran7722f472016-02-29 15:45:07 +02002525
2526 schedule_work(&ibdev->devr.ports[port - 1].pkey_change_work);
Eli Cohene126ba92013-07-07 17:25:49 +03002527 break;
2528
2529 case MLX5_DEV_EVENT_GUID_CHANGE:
2530 ibev.event = IB_EVENT_GID_CHANGE;
Jack Morgenstein4d2f9bb2014-07-28 23:30:24 +03002531 port = (u8)param;
Eli Cohene126ba92013-07-07 17:25:49 +03002532 break;
2533
2534 case MLX5_DEV_EVENT_CLIENT_REREG:
2535 ibev.event = IB_EVENT_CLIENT_REREGISTER;
Jack Morgenstein4d2f9bb2014-07-28 23:30:24 +03002536 port = (u8)param;
Eli Cohene126ba92013-07-07 17:25:49 +03002537 break;
Saeed Mahameedbdc37922016-09-29 19:35:38 +03002538 default:
2539 return;
Eli Cohene126ba92013-07-07 17:25:49 +03002540 }
2541
2542 ibev.device = &ibdev->ib_dev;
2543 ibev.element.port_num = port;
2544
Eli Cohena0c84c32013-09-11 16:35:27 +03002545 if (port < 1 || port > ibdev->num_ports) {
2546 mlx5_ib_warn(ibdev, "warning: event on port %d\n", port);
2547 return;
2548 }
2549
Eli Cohene126ba92013-07-07 17:25:49 +03002550 if (ibdev->ib_active)
2551 ib_dispatch_event(&ibev);
Eli Cohendbaaff22016-10-27 16:36:44 +03002552
2553 if (fatal)
2554 ibdev->ib_active = false;
Eli Cohene126ba92013-07-07 17:25:49 +03002555}
2556
Maor Gottliebc43f1112017-01-18 14:10:33 +02002557static int set_has_smi_cap(struct mlx5_ib_dev *dev)
2558{
2559 struct mlx5_hca_vport_context vport_ctx;
2560 int err;
2561 int port;
2562
2563 for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++) {
2564 dev->mdev->port_caps[port - 1].has_smi = false;
2565 if (MLX5_CAP_GEN(dev->mdev, port_type) ==
2566 MLX5_CAP_PORT_TYPE_IB) {
2567 if (MLX5_CAP_GEN(dev->mdev, ib_virt)) {
2568 err = mlx5_query_hca_vport_context(dev->mdev, 0,
2569 port, 0,
2570 &vport_ctx);
2571 if (err) {
2572 mlx5_ib_err(dev, "query_hca_vport_context for port=%d failed %d\n",
2573 port, err);
2574 return err;
2575 }
2576 dev->mdev->port_caps[port - 1].has_smi =
2577 vport_ctx.has_smi;
2578 } else {
2579 dev->mdev->port_caps[port - 1].has_smi = true;
2580 }
2581 }
2582 }
2583 return 0;
2584}
2585
Eli Cohene126ba92013-07-07 17:25:49 +03002586static void get_ext_port_caps(struct mlx5_ib_dev *dev)
2587{
2588 int port;
2589
Saeed Mahameed938fe832015-05-28 22:28:41 +03002590 for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++)
Eli Cohene126ba92013-07-07 17:25:49 +03002591 mlx5_query_ext_port_caps(dev, port);
2592}
2593
2594static int get_port_caps(struct mlx5_ib_dev *dev)
2595{
2596 struct ib_device_attr *dprops = NULL;
2597 struct ib_port_attr *pprops = NULL;
Dan Carpenterf614fc12015-01-12 11:56:58 +03002598 int err = -ENOMEM;
Eli Cohene126ba92013-07-07 17:25:49 +03002599 int port;
Matan Barak2528e332015-06-11 16:35:25 +03002600 struct ib_udata uhw = {.inlen = 0, .outlen = 0};
Eli Cohene126ba92013-07-07 17:25:49 +03002601
2602 pprops = kmalloc(sizeof(*pprops), GFP_KERNEL);
2603 if (!pprops)
2604 goto out;
2605
2606 dprops = kmalloc(sizeof(*dprops), GFP_KERNEL);
2607 if (!dprops)
2608 goto out;
2609
Maor Gottliebc43f1112017-01-18 14:10:33 +02002610 err = set_has_smi_cap(dev);
2611 if (err)
2612 goto out;
2613
Matan Barak2528e332015-06-11 16:35:25 +03002614 err = mlx5_ib_query_device(&dev->ib_dev, dprops, &uhw);
Eli Cohene126ba92013-07-07 17:25:49 +03002615 if (err) {
2616 mlx5_ib_warn(dev, "query_device failed %d\n", err);
2617 goto out;
2618 }
2619
Saeed Mahameed938fe832015-05-28 22:28:41 +03002620 for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++) {
Eli Cohene126ba92013-07-07 17:25:49 +03002621 err = mlx5_ib_query_port(&dev->ib_dev, port, pprops);
2622 if (err) {
Saeed Mahameed938fe832015-05-28 22:28:41 +03002623 mlx5_ib_warn(dev, "query_port %d failed %d\n",
2624 port, err);
Eli Cohene126ba92013-07-07 17:25:49 +03002625 break;
2626 }
Saeed Mahameed938fe832015-05-28 22:28:41 +03002627 dev->mdev->port_caps[port - 1].pkey_table_len =
2628 dprops->max_pkeys;
2629 dev->mdev->port_caps[port - 1].gid_table_len =
2630 pprops->gid_tbl_len;
Eli Cohene126ba92013-07-07 17:25:49 +03002631 mlx5_ib_dbg(dev, "pkey_table_len %d, gid_table_len %d\n",
2632 dprops->max_pkeys, pprops->gid_tbl_len);
2633 }
2634
2635out:
2636 kfree(pprops);
2637 kfree(dprops);
2638
2639 return err;
2640}
2641
2642static void destroy_umrc_res(struct mlx5_ib_dev *dev)
2643{
2644 int err;
2645
2646 err = mlx5_mr_cache_cleanup(dev);
2647 if (err)
2648 mlx5_ib_warn(dev, "mr cache cleanup failed\n");
2649
2650 mlx5_ib_destroy_qp(dev->umrc.qp);
Christoph Hellwigadd08d72016-03-03 09:38:22 +01002651 ib_free_cq(dev->umrc.cq);
Eli Cohene126ba92013-07-07 17:25:49 +03002652 ib_dealloc_pd(dev->umrc.pd);
2653}
2654
2655enum {
2656 MAX_UMR_WR = 128,
2657};
2658
2659static int create_umr_res(struct mlx5_ib_dev *dev)
2660{
2661 struct ib_qp_init_attr *init_attr = NULL;
2662 struct ib_qp_attr *attr = NULL;
2663 struct ib_pd *pd;
2664 struct ib_cq *cq;
2665 struct ib_qp *qp;
Eli Cohene126ba92013-07-07 17:25:49 +03002666 int ret;
2667
2668 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
2669 init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL);
2670 if (!attr || !init_attr) {
2671 ret = -ENOMEM;
2672 goto error_0;
2673 }
2674
Christoph Hellwiged082d32016-09-05 12:56:17 +02002675 pd = ib_alloc_pd(&dev->ib_dev, 0);
Eli Cohene126ba92013-07-07 17:25:49 +03002676 if (IS_ERR(pd)) {
2677 mlx5_ib_dbg(dev, "Couldn't create PD for sync UMR QP\n");
2678 ret = PTR_ERR(pd);
2679 goto error_0;
2680 }
2681
Christoph Hellwigadd08d72016-03-03 09:38:22 +01002682 cq = ib_alloc_cq(&dev->ib_dev, NULL, 128, 0, IB_POLL_SOFTIRQ);
Eli Cohene126ba92013-07-07 17:25:49 +03002683 if (IS_ERR(cq)) {
2684 mlx5_ib_dbg(dev, "Couldn't create CQ for sync UMR QP\n");
2685 ret = PTR_ERR(cq);
2686 goto error_2;
2687 }
Eli Cohene126ba92013-07-07 17:25:49 +03002688
2689 init_attr->send_cq = cq;
2690 init_attr->recv_cq = cq;
2691 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
2692 init_attr->cap.max_send_wr = MAX_UMR_WR;
2693 init_attr->cap.max_send_sge = 1;
2694 init_attr->qp_type = MLX5_IB_QPT_REG_UMR;
2695 init_attr->port_num = 1;
2696 qp = mlx5_ib_create_qp(pd, init_attr, NULL);
2697 if (IS_ERR(qp)) {
2698 mlx5_ib_dbg(dev, "Couldn't create sync UMR QP\n");
2699 ret = PTR_ERR(qp);
2700 goto error_3;
2701 }
2702 qp->device = &dev->ib_dev;
2703 qp->real_qp = qp;
2704 qp->uobject = NULL;
2705 qp->qp_type = MLX5_IB_QPT_REG_UMR;
2706
2707 attr->qp_state = IB_QPS_INIT;
2708 attr->port_num = 1;
2709 ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_PKEY_INDEX |
2710 IB_QP_PORT, NULL);
2711 if (ret) {
2712 mlx5_ib_dbg(dev, "Couldn't modify UMR QP\n");
2713 goto error_4;
2714 }
2715
2716 memset(attr, 0, sizeof(*attr));
2717 attr->qp_state = IB_QPS_RTR;
2718 attr->path_mtu = IB_MTU_256;
2719
2720 ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
2721 if (ret) {
2722 mlx5_ib_dbg(dev, "Couldn't modify umr QP to rtr\n");
2723 goto error_4;
2724 }
2725
2726 memset(attr, 0, sizeof(*attr));
2727 attr->qp_state = IB_QPS_RTS;
2728 ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
2729 if (ret) {
2730 mlx5_ib_dbg(dev, "Couldn't modify umr QP to rts\n");
2731 goto error_4;
2732 }
2733
2734 dev->umrc.qp = qp;
2735 dev->umrc.cq = cq;
Eli Cohene126ba92013-07-07 17:25:49 +03002736 dev->umrc.pd = pd;
2737
2738 sema_init(&dev->umrc.sem, MAX_UMR_WR);
2739 ret = mlx5_mr_cache_init(dev);
2740 if (ret) {
2741 mlx5_ib_warn(dev, "mr cache init failed %d\n", ret);
2742 goto error_4;
2743 }
2744
2745 kfree(attr);
2746 kfree(init_attr);
2747
2748 return 0;
2749
2750error_4:
2751 mlx5_ib_destroy_qp(qp);
2752
2753error_3:
Christoph Hellwigadd08d72016-03-03 09:38:22 +01002754 ib_free_cq(cq);
Eli Cohene126ba92013-07-07 17:25:49 +03002755
2756error_2:
Eli Cohene126ba92013-07-07 17:25:49 +03002757 ib_dealloc_pd(pd);
2758
2759error_0:
2760 kfree(attr);
2761 kfree(init_attr);
2762 return ret;
2763}
2764
2765static int create_dev_resources(struct mlx5_ib_resources *devr)
2766{
2767 struct ib_srq_init_attr attr;
2768 struct mlx5_ib_dev *dev;
Matan Barakbcf4c1e2015-06-11 16:35:20 +03002769 struct ib_cq_init_attr cq_attr = {.cqe = 1};
Haggai Eran7722f472016-02-29 15:45:07 +02002770 int port;
Eli Cohene126ba92013-07-07 17:25:49 +03002771 int ret = 0;
2772
2773 dev = container_of(devr, struct mlx5_ib_dev, devr);
2774
Haggai Erand16e91d2016-02-29 15:45:05 +02002775 mutex_init(&devr->mutex);
2776
Eli Cohene126ba92013-07-07 17:25:49 +03002777 devr->p0 = mlx5_ib_alloc_pd(&dev->ib_dev, NULL, NULL);
2778 if (IS_ERR(devr->p0)) {
2779 ret = PTR_ERR(devr->p0);
2780 goto error0;
2781 }
2782 devr->p0->device = &dev->ib_dev;
2783 devr->p0->uobject = NULL;
2784 atomic_set(&devr->p0->usecnt, 0);
2785
Matan Barakbcf4c1e2015-06-11 16:35:20 +03002786 devr->c0 = mlx5_ib_create_cq(&dev->ib_dev, &cq_attr, NULL, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03002787 if (IS_ERR(devr->c0)) {
2788 ret = PTR_ERR(devr->c0);
2789 goto error1;
2790 }
2791 devr->c0->device = &dev->ib_dev;
2792 devr->c0->uobject = NULL;
2793 devr->c0->comp_handler = NULL;
2794 devr->c0->event_handler = NULL;
2795 devr->c0->cq_context = NULL;
2796 atomic_set(&devr->c0->usecnt, 0);
2797
2798 devr->x0 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
2799 if (IS_ERR(devr->x0)) {
2800 ret = PTR_ERR(devr->x0);
2801 goto error2;
2802 }
2803 devr->x0->device = &dev->ib_dev;
2804 devr->x0->inode = NULL;
2805 atomic_set(&devr->x0->usecnt, 0);
2806 mutex_init(&devr->x0->tgt_qp_mutex);
2807 INIT_LIST_HEAD(&devr->x0->tgt_qp_list);
2808
2809 devr->x1 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
2810 if (IS_ERR(devr->x1)) {
2811 ret = PTR_ERR(devr->x1);
2812 goto error3;
2813 }
2814 devr->x1->device = &dev->ib_dev;
2815 devr->x1->inode = NULL;
2816 atomic_set(&devr->x1->usecnt, 0);
2817 mutex_init(&devr->x1->tgt_qp_mutex);
2818 INIT_LIST_HEAD(&devr->x1->tgt_qp_list);
2819
2820 memset(&attr, 0, sizeof(attr));
2821 attr.attr.max_sge = 1;
2822 attr.attr.max_wr = 1;
2823 attr.srq_type = IB_SRQT_XRC;
2824 attr.ext.xrc.cq = devr->c0;
2825 attr.ext.xrc.xrcd = devr->x0;
2826
2827 devr->s0 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
2828 if (IS_ERR(devr->s0)) {
2829 ret = PTR_ERR(devr->s0);
2830 goto error4;
2831 }
2832 devr->s0->device = &dev->ib_dev;
2833 devr->s0->pd = devr->p0;
2834 devr->s0->uobject = NULL;
2835 devr->s0->event_handler = NULL;
2836 devr->s0->srq_context = NULL;
2837 devr->s0->srq_type = IB_SRQT_XRC;
2838 devr->s0->ext.xrc.xrcd = devr->x0;
2839 devr->s0->ext.xrc.cq = devr->c0;
2840 atomic_inc(&devr->s0->ext.xrc.xrcd->usecnt);
2841 atomic_inc(&devr->s0->ext.xrc.cq->usecnt);
2842 atomic_inc(&devr->p0->usecnt);
2843 atomic_set(&devr->s0->usecnt, 0);
2844
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03002845 memset(&attr, 0, sizeof(attr));
2846 attr.attr.max_sge = 1;
2847 attr.attr.max_wr = 1;
2848 attr.srq_type = IB_SRQT_BASIC;
2849 devr->s1 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
2850 if (IS_ERR(devr->s1)) {
2851 ret = PTR_ERR(devr->s1);
2852 goto error5;
2853 }
2854 devr->s1->device = &dev->ib_dev;
2855 devr->s1->pd = devr->p0;
2856 devr->s1->uobject = NULL;
2857 devr->s1->event_handler = NULL;
2858 devr->s1->srq_context = NULL;
2859 devr->s1->srq_type = IB_SRQT_BASIC;
2860 devr->s1->ext.xrc.cq = devr->c0;
2861 atomic_inc(&devr->p0->usecnt);
2862 atomic_set(&devr->s0->usecnt, 0);
2863
Haggai Eran7722f472016-02-29 15:45:07 +02002864 for (port = 0; port < ARRAY_SIZE(devr->ports); ++port) {
2865 INIT_WORK(&devr->ports[port].pkey_change_work,
2866 pkey_change_handler);
2867 devr->ports[port].devr = devr;
2868 }
2869
Eli Cohene126ba92013-07-07 17:25:49 +03002870 return 0;
2871
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03002872error5:
2873 mlx5_ib_destroy_srq(devr->s0);
Eli Cohene126ba92013-07-07 17:25:49 +03002874error4:
2875 mlx5_ib_dealloc_xrcd(devr->x1);
2876error3:
2877 mlx5_ib_dealloc_xrcd(devr->x0);
2878error2:
2879 mlx5_ib_destroy_cq(devr->c0);
2880error1:
2881 mlx5_ib_dealloc_pd(devr->p0);
2882error0:
2883 return ret;
2884}
2885
2886static void destroy_dev_resources(struct mlx5_ib_resources *devr)
2887{
Haggai Eran7722f472016-02-29 15:45:07 +02002888 struct mlx5_ib_dev *dev =
2889 container_of(devr, struct mlx5_ib_dev, devr);
2890 int port;
2891
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03002892 mlx5_ib_destroy_srq(devr->s1);
Eli Cohene126ba92013-07-07 17:25:49 +03002893 mlx5_ib_destroy_srq(devr->s0);
2894 mlx5_ib_dealloc_xrcd(devr->x0);
2895 mlx5_ib_dealloc_xrcd(devr->x1);
2896 mlx5_ib_destroy_cq(devr->c0);
2897 mlx5_ib_dealloc_pd(devr->p0);
Haggai Eran7722f472016-02-29 15:45:07 +02002898
2899 /* Make sure no change P_Key work items are still executing */
2900 for (port = 0; port < dev->num_ports; ++port)
2901 cancel_work_sync(&devr->ports[port].pkey_change_work);
Eli Cohene126ba92013-07-07 17:25:49 +03002902}
2903
Achiad Shochate53505a2015-12-23 18:47:25 +02002904static u32 get_core_cap_flags(struct ib_device *ibdev)
2905{
2906 struct mlx5_ib_dev *dev = to_mdev(ibdev);
2907 enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, 1);
2908 u8 l3_type_cap = MLX5_CAP_ROCE(dev->mdev, l3_type);
2909 u8 roce_version_cap = MLX5_CAP_ROCE(dev->mdev, roce_version);
2910 u32 ret = 0;
2911
2912 if (ll == IB_LINK_LAYER_INFINIBAND)
2913 return RDMA_CORE_PORT_IBA_IB;
2914
2915 if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV4_CAP))
2916 return 0;
2917
2918 if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV6_CAP))
2919 return 0;
2920
2921 if (roce_version_cap & MLX5_ROCE_VERSION_1_CAP)
2922 ret |= RDMA_CORE_PORT_IBA_ROCE;
2923
2924 if (roce_version_cap & MLX5_ROCE_VERSION_2_CAP)
2925 ret |= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
2926
2927 return ret;
2928}
2929
Ira Weiny77386132015-05-13 20:02:58 -04002930static int mlx5_port_immutable(struct ib_device *ibdev, u8 port_num,
2931 struct ib_port_immutable *immutable)
2932{
2933 struct ib_port_attr attr;
Or Gerlitzca5b91d2016-11-27 16:51:36 +02002934 struct mlx5_ib_dev *dev = to_mdev(ibdev);
2935 enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, port_num);
Ira Weiny77386132015-05-13 20:02:58 -04002936 int err;
2937
2938 err = mlx5_ib_query_port(ibdev, port_num, &attr);
2939 if (err)
2940 return err;
2941
2942 immutable->pkey_tbl_len = attr.pkey_tbl_len;
2943 immutable->gid_tbl_len = attr.gid_tbl_len;
Achiad Shochate53505a2015-12-23 18:47:25 +02002944 immutable->core_cap_flags = get_core_cap_flags(ibdev);
Or Gerlitzca5b91d2016-11-27 16:51:36 +02002945 if ((ll == IB_LINK_LAYER_INFINIBAND) || MLX5_CAP_GEN(dev->mdev, roce))
2946 immutable->max_mad_size = IB_MGMT_MAD_SIZE;
Ira Weiny77386132015-05-13 20:02:58 -04002947
2948 return 0;
2949}
2950
Ira Weinyc7342822016-06-15 02:22:01 -04002951static void get_dev_fw_str(struct ib_device *ibdev, char *str,
2952 size_t str_len)
2953{
2954 struct mlx5_ib_dev *dev =
2955 container_of(ibdev, struct mlx5_ib_dev, ib_dev);
2956 snprintf(str, str_len, "%d.%d.%04d", fw_rev_maj(dev->mdev),
2957 fw_rev_min(dev->mdev), fw_rev_sub(dev->mdev));
2958}
2959
Or Gerlitz45f95ac2016-11-27 16:51:35 +02002960static int mlx5_eth_lag_init(struct mlx5_ib_dev *dev)
Aviv Heller9ef9c642016-09-18 20:48:01 +03002961{
2962 struct mlx5_core_dev *mdev = dev->mdev;
2963 struct mlx5_flow_namespace *ns = mlx5_get_flow_namespace(mdev,
2964 MLX5_FLOW_NAMESPACE_LAG);
2965 struct mlx5_flow_table *ft;
2966 int err;
2967
2968 if (!ns || !mlx5_lag_is_active(mdev))
2969 return 0;
2970
2971 err = mlx5_cmd_create_vport_lag(mdev);
2972 if (err)
2973 return err;
2974
2975 ft = mlx5_create_lag_demux_flow_table(ns, 0, 0);
2976 if (IS_ERR(ft)) {
2977 err = PTR_ERR(ft);
2978 goto err_destroy_vport_lag;
2979 }
2980
2981 dev->flow_db.lag_demux_ft = ft;
2982 return 0;
2983
2984err_destroy_vport_lag:
2985 mlx5_cmd_destroy_vport_lag(mdev);
2986 return err;
2987}
2988
Or Gerlitz45f95ac2016-11-27 16:51:35 +02002989static void mlx5_eth_lag_cleanup(struct mlx5_ib_dev *dev)
Aviv Heller9ef9c642016-09-18 20:48:01 +03002990{
2991 struct mlx5_core_dev *mdev = dev->mdev;
2992
2993 if (dev->flow_db.lag_demux_ft) {
2994 mlx5_destroy_flow_table(dev->flow_db.lag_demux_ft);
2995 dev->flow_db.lag_demux_ft = NULL;
2996
2997 mlx5_cmd_destroy_vport_lag(mdev);
2998 }
2999}
3000
Or Gerlitzd012f5d2016-11-27 16:51:34 +02003001static int mlx5_add_netdev_notifier(struct mlx5_ib_dev *dev)
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003002{
Achiad Shochate53505a2015-12-23 18:47:25 +02003003 int err;
3004
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003005 dev->roce.nb.notifier_call = mlx5_netdev_event;
Achiad Shochate53505a2015-12-23 18:47:25 +02003006 err = register_netdevice_notifier(&dev->roce.nb);
Aviv Heller5ec8c832016-09-18 20:48:00 +03003007 if (err) {
3008 dev->roce.nb.notifier_call = NULL;
Achiad Shochate53505a2015-12-23 18:47:25 +02003009 return err;
Aviv Heller5ec8c832016-09-18 20:48:00 +03003010 }
Achiad Shochate53505a2015-12-23 18:47:25 +02003011
Or Gerlitzd012f5d2016-11-27 16:51:34 +02003012 return 0;
3013}
Achiad Shochate53505a2015-12-23 18:47:25 +02003014
Or Gerlitzd012f5d2016-11-27 16:51:34 +02003015static void mlx5_remove_netdev_notifier(struct mlx5_ib_dev *dev)
Eli Cohene126ba92013-07-07 17:25:49 +03003016{
3017 if (dev->roce.nb.notifier_call) {
3018 unregister_netdevice_notifier(&dev->roce.nb);
3019 dev->roce.nb.notifier_call = NULL;
3020 }
3021}
3022
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003023static int mlx5_enable_eth(struct mlx5_ib_dev *dev)
Eli Cohene126ba92013-07-07 17:25:49 +03003024{
Eli Cohene126ba92013-07-07 17:25:49 +03003025 int err;
3026
Or Gerlitzd012f5d2016-11-27 16:51:34 +02003027 err = mlx5_add_netdev_notifier(dev);
3028 if (err)
Achiad Shochate53505a2015-12-23 18:47:25 +02003029 return err;
Achiad Shochate53505a2015-12-23 18:47:25 +02003030
Or Gerlitzca5b91d2016-11-27 16:51:36 +02003031 if (MLX5_CAP_GEN(dev->mdev, roce)) {
3032 err = mlx5_nic_vport_enable_roce(dev->mdev);
3033 if (err)
3034 goto err_unregister_netdevice_notifier;
3035 }
Achiad Shochate53505a2015-12-23 18:47:25 +02003036
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003037 err = mlx5_eth_lag_init(dev);
Aviv Heller9ef9c642016-09-18 20:48:01 +03003038 if (err)
3039 goto err_disable_roce;
3040
Achiad Shochate53505a2015-12-23 18:47:25 +02003041 return 0;
3042
Aviv Heller9ef9c642016-09-18 20:48:01 +03003043err_disable_roce:
Or Gerlitzca5b91d2016-11-27 16:51:36 +02003044 if (MLX5_CAP_GEN(dev->mdev, roce))
3045 mlx5_nic_vport_disable_roce(dev->mdev);
Aviv Heller9ef9c642016-09-18 20:48:01 +03003046
Achiad Shochate53505a2015-12-23 18:47:25 +02003047err_unregister_netdevice_notifier:
Or Gerlitzd012f5d2016-11-27 16:51:34 +02003048 mlx5_remove_netdev_notifier(dev);
Achiad Shochate53505a2015-12-23 18:47:25 +02003049 return err;
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003050}
3051
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003052static void mlx5_disable_eth(struct mlx5_ib_dev *dev)
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003053{
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003054 mlx5_eth_lag_cleanup(dev);
Or Gerlitzca5b91d2016-11-27 16:51:36 +02003055 if (MLX5_CAP_GEN(dev->mdev, roce))
3056 mlx5_nic_vport_disable_roce(dev->mdev);
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003057}
3058
Mark Bloch0837e862016-06-17 15:10:55 +03003059static void mlx5_ib_dealloc_q_counters(struct mlx5_ib_dev *dev)
3060{
3061 unsigned int i;
3062
3063 for (i = 0; i < dev->num_ports; i++)
3064 mlx5_core_dealloc_q_counter(dev->mdev,
3065 dev->port[i].q_cnt_id);
3066}
3067
3068static int mlx5_ib_alloc_q_counters(struct mlx5_ib_dev *dev)
3069{
3070 int i;
3071 int ret;
3072
3073 for (i = 0; i < dev->num_ports; i++) {
3074 ret = mlx5_core_alloc_q_counter(dev->mdev,
3075 &dev->port[i].q_cnt_id);
3076 if (ret) {
3077 mlx5_ib_warn(dev,
3078 "couldn't allocate queue counter for port %d, err %d\n",
3079 i + 1, ret);
3080 goto dealloc_counters;
3081 }
3082 }
3083
3084 return 0;
3085
3086dealloc_counters:
3087 while (--i >= 0)
3088 mlx5_core_dealloc_q_counter(dev->mdev,
3089 dev->port[i].q_cnt_id);
3090
3091 return ret;
3092}
3093
Wei Yongjun61961502016-07-12 11:32:47 +00003094static const char * const names[] = {
Mark Bloch0ad17a82016-06-17 15:10:56 +03003095 "rx_write_requests",
3096 "rx_read_requests",
3097 "rx_atomic_requests",
3098 "out_of_buffer",
3099 "out_of_sequence",
3100 "duplicate_request",
3101 "rnr_nak_retry_err",
3102 "packet_seq_err",
3103 "implied_nak_seq_err",
3104 "local_ack_timeout_err",
3105};
3106
3107static const size_t stats_offsets[] = {
3108 MLX5_BYTE_OFF(query_q_counter_out, rx_write_requests),
3109 MLX5_BYTE_OFF(query_q_counter_out, rx_read_requests),
3110 MLX5_BYTE_OFF(query_q_counter_out, rx_atomic_requests),
3111 MLX5_BYTE_OFF(query_q_counter_out, out_of_buffer),
3112 MLX5_BYTE_OFF(query_q_counter_out, out_of_sequence),
3113 MLX5_BYTE_OFF(query_q_counter_out, duplicate_request),
3114 MLX5_BYTE_OFF(query_q_counter_out, rnr_nak_retry_err),
3115 MLX5_BYTE_OFF(query_q_counter_out, packet_seq_err),
3116 MLX5_BYTE_OFF(query_q_counter_out, implied_nak_seq_err),
3117 MLX5_BYTE_OFF(query_q_counter_out, local_ack_timeout_err),
3118};
3119
3120static struct rdma_hw_stats *mlx5_ib_alloc_hw_stats(struct ib_device *ibdev,
3121 u8 port_num)
3122{
3123 BUILD_BUG_ON(ARRAY_SIZE(names) != ARRAY_SIZE(stats_offsets));
3124
3125 /* We support only per port stats */
3126 if (port_num == 0)
3127 return NULL;
3128
3129 return rdma_alloc_hw_stats_struct(names, ARRAY_SIZE(names),
3130 RDMA_HW_STATS_DEFAULT_LIFESPAN);
3131}
3132
3133static int mlx5_ib_get_hw_stats(struct ib_device *ibdev,
3134 struct rdma_hw_stats *stats,
3135 u8 port, int index)
3136{
3137 struct mlx5_ib_dev *dev = to_mdev(ibdev);
3138 int outlen = MLX5_ST_SZ_BYTES(query_q_counter_out);
3139 void *out;
3140 __be32 val;
3141 int ret;
3142 int i;
3143
3144 if (!port || !stats)
3145 return -ENOSYS;
3146
3147 out = mlx5_vzalloc(outlen);
3148 if (!out)
3149 return -ENOMEM;
3150
3151 ret = mlx5_core_query_q_counter(dev->mdev,
3152 dev->port[port - 1].q_cnt_id, 0,
3153 out, outlen);
3154 if (ret)
3155 goto free;
3156
3157 for (i = 0; i < ARRAY_SIZE(names); i++) {
3158 val = *(__be32 *)(out + stats_offsets[i]);
3159 stats->value[i] = (u64)be32_to_cpu(val);
3160 }
3161free:
3162 kvfree(out);
3163 return ARRAY_SIZE(names);
3164}
3165
Jack Morgenstein9603b612014-07-28 23:30:22 +03003166static void *mlx5_ib_add(struct mlx5_core_dev *mdev)
Eli Cohene126ba92013-07-07 17:25:49 +03003167{
Eli Cohene126ba92013-07-07 17:25:49 +03003168 struct mlx5_ib_dev *dev;
Achiad Shochatebd61f62015-12-23 18:47:16 +02003169 enum rdma_link_layer ll;
3170 int port_type_cap;
Aviv Heller4babcf92016-09-18 20:48:03 +03003171 const char *name;
Eli Cohene126ba92013-07-07 17:25:49 +03003172 int err;
3173 int i;
3174
Achiad Shochatebd61f62015-12-23 18:47:16 +02003175 port_type_cap = MLX5_CAP_GEN(mdev, port_type);
3176 ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
3177
Eli Cohene126ba92013-07-07 17:25:49 +03003178 printk_once(KERN_INFO "%s", mlx5_version);
3179
3180 dev = (struct mlx5_ib_dev *)ib_alloc_device(sizeof(*dev));
3181 if (!dev)
Jack Morgenstein9603b612014-07-28 23:30:22 +03003182 return NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03003183
Jack Morgenstein9603b612014-07-28 23:30:22 +03003184 dev->mdev = mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03003185
Mark Bloch0837e862016-06-17 15:10:55 +03003186 dev->port = kcalloc(MLX5_CAP_GEN(mdev, num_ports), sizeof(*dev->port),
3187 GFP_KERNEL);
3188 if (!dev->port)
3189 goto err_dealloc;
3190
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003191 rwlock_init(&dev->roce.netdev_lock);
Eli Cohene126ba92013-07-07 17:25:49 +03003192 err = get_port_caps(dev);
3193 if (err)
Mark Bloch0837e862016-06-17 15:10:55 +03003194 goto err_free_port;
Eli Cohene126ba92013-07-07 17:25:49 +03003195
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03003196 if (mlx5_use_mad_ifc(dev))
3197 get_ext_port_caps(dev);
Eli Cohene126ba92013-07-07 17:25:49 +03003198
Aviv Heller4babcf92016-09-18 20:48:03 +03003199 if (!mlx5_lag_is_active(mdev))
3200 name = "mlx5_%d";
3201 else
3202 name = "mlx5_bond_%d";
3203
3204 strlcpy(dev->ib_dev.name, name, IB_DEVICE_NAME_MAX);
Eli Cohene126ba92013-07-07 17:25:49 +03003205 dev->ib_dev.owner = THIS_MODULE;
3206 dev->ib_dev.node_type = RDMA_NODE_IB_CA;
Sagi Grimbergc6790aa2015-09-24 10:34:23 +03003207 dev->ib_dev.local_dma_lkey = 0 /* not supported for now */;
Saeed Mahameed938fe832015-05-28 22:28:41 +03003208 dev->num_ports = MLX5_CAP_GEN(mdev, num_ports);
Eli Cohene126ba92013-07-07 17:25:49 +03003209 dev->ib_dev.phys_port_cnt = dev->num_ports;
Saeed Mahameed233d05d2015-04-02 17:07:32 +03003210 dev->ib_dev.num_comp_vectors =
3211 dev->mdev->priv.eq_table.num_comp_vectors;
Eli Cohene126ba92013-07-07 17:25:49 +03003212 dev->ib_dev.dma_device = &mdev->pdev->dev;
3213
3214 dev->ib_dev.uverbs_abi_ver = MLX5_IB_UVERBS_ABI_VERSION;
3215 dev->ib_dev.uverbs_cmd_mask =
3216 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
3217 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
3218 (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
3219 (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
3220 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
Moni Shoua41c450f2016-11-23 08:23:26 +02003221 (1ull << IB_USER_VERBS_CMD_CREATE_AH) |
3222 (1ull << IB_USER_VERBS_CMD_DESTROY_AH) |
Eli Cohene126ba92013-07-07 17:25:49 +03003223 (1ull << IB_USER_VERBS_CMD_REG_MR) |
Noa Osherovich56e11d62016-02-29 16:46:51 +02003224 (1ull << IB_USER_VERBS_CMD_REREG_MR) |
Eli Cohene126ba92013-07-07 17:25:49 +03003225 (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
3226 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
3227 (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
3228 (1ull << IB_USER_VERBS_CMD_RESIZE_CQ) |
3229 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
3230 (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
3231 (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
3232 (1ull << IB_USER_VERBS_CMD_QUERY_QP) |
3233 (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
3234 (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST) |
3235 (1ull << IB_USER_VERBS_CMD_DETACH_MCAST) |
3236 (1ull << IB_USER_VERBS_CMD_CREATE_SRQ) |
3237 (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ) |
3238 (1ull << IB_USER_VERBS_CMD_QUERY_SRQ) |
3239 (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ) |
3240 (1ull << IB_USER_VERBS_CMD_CREATE_XSRQ) |
3241 (1ull << IB_USER_VERBS_CMD_OPEN_QP);
Haggai Eran1707cb42015-02-08 13:28:52 +02003242 dev->ib_dev.uverbs_ex_cmd_mask =
Matan Barakd4584dd2016-01-28 17:51:46 +02003243 (1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE) |
3244 (1ull << IB_USER_VERBS_EX_CMD_CREATE_CQ) |
Bodong Wang7d29f342016-12-01 13:43:16 +02003245 (1ull << IB_USER_VERBS_EX_CMD_CREATE_QP) |
3246 (1ull << IB_USER_VERBS_EX_CMD_MODIFY_QP);
Eli Cohene126ba92013-07-07 17:25:49 +03003247
3248 dev->ib_dev.query_device = mlx5_ib_query_device;
3249 dev->ib_dev.query_port = mlx5_ib_query_port;
Achiad Shochatebd61f62015-12-23 18:47:16 +02003250 dev->ib_dev.get_link_layer = mlx5_ib_port_link_layer;
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003251 if (ll == IB_LINK_LAYER_ETHERNET)
3252 dev->ib_dev.get_netdev = mlx5_ib_get_netdev;
Eli Cohene126ba92013-07-07 17:25:49 +03003253 dev->ib_dev.query_gid = mlx5_ib_query_gid;
Achiad Shochat3cca2602015-12-23 18:47:23 +02003254 dev->ib_dev.add_gid = mlx5_ib_add_gid;
3255 dev->ib_dev.del_gid = mlx5_ib_del_gid;
Eli Cohene126ba92013-07-07 17:25:49 +03003256 dev->ib_dev.query_pkey = mlx5_ib_query_pkey;
3257 dev->ib_dev.modify_device = mlx5_ib_modify_device;
3258 dev->ib_dev.modify_port = mlx5_ib_modify_port;
3259 dev->ib_dev.alloc_ucontext = mlx5_ib_alloc_ucontext;
3260 dev->ib_dev.dealloc_ucontext = mlx5_ib_dealloc_ucontext;
3261 dev->ib_dev.mmap = mlx5_ib_mmap;
3262 dev->ib_dev.alloc_pd = mlx5_ib_alloc_pd;
3263 dev->ib_dev.dealloc_pd = mlx5_ib_dealloc_pd;
3264 dev->ib_dev.create_ah = mlx5_ib_create_ah;
3265 dev->ib_dev.query_ah = mlx5_ib_query_ah;
3266 dev->ib_dev.destroy_ah = mlx5_ib_destroy_ah;
3267 dev->ib_dev.create_srq = mlx5_ib_create_srq;
3268 dev->ib_dev.modify_srq = mlx5_ib_modify_srq;
3269 dev->ib_dev.query_srq = mlx5_ib_query_srq;
3270 dev->ib_dev.destroy_srq = mlx5_ib_destroy_srq;
3271 dev->ib_dev.post_srq_recv = mlx5_ib_post_srq_recv;
3272 dev->ib_dev.create_qp = mlx5_ib_create_qp;
3273 dev->ib_dev.modify_qp = mlx5_ib_modify_qp;
3274 dev->ib_dev.query_qp = mlx5_ib_query_qp;
3275 dev->ib_dev.destroy_qp = mlx5_ib_destroy_qp;
3276 dev->ib_dev.post_send = mlx5_ib_post_send;
3277 dev->ib_dev.post_recv = mlx5_ib_post_recv;
3278 dev->ib_dev.create_cq = mlx5_ib_create_cq;
3279 dev->ib_dev.modify_cq = mlx5_ib_modify_cq;
3280 dev->ib_dev.resize_cq = mlx5_ib_resize_cq;
3281 dev->ib_dev.destroy_cq = mlx5_ib_destroy_cq;
3282 dev->ib_dev.poll_cq = mlx5_ib_poll_cq;
3283 dev->ib_dev.req_notify_cq = mlx5_ib_arm_cq;
3284 dev->ib_dev.get_dma_mr = mlx5_ib_get_dma_mr;
3285 dev->ib_dev.reg_user_mr = mlx5_ib_reg_user_mr;
Noa Osherovich56e11d62016-02-29 16:46:51 +02003286 dev->ib_dev.rereg_user_mr = mlx5_ib_rereg_user_mr;
Eli Cohene126ba92013-07-07 17:25:49 +03003287 dev->ib_dev.dereg_mr = mlx5_ib_dereg_mr;
3288 dev->ib_dev.attach_mcast = mlx5_ib_mcg_attach;
3289 dev->ib_dev.detach_mcast = mlx5_ib_mcg_detach;
3290 dev->ib_dev.process_mad = mlx5_ib_process_mad;
Sagi Grimberg9bee1782015-07-30 10:32:35 +03003291 dev->ib_dev.alloc_mr = mlx5_ib_alloc_mr;
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003292 dev->ib_dev.map_mr_sg = mlx5_ib_map_mr_sg;
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02003293 dev->ib_dev.check_mr_status = mlx5_ib_check_mr_status;
Ira Weiny77386132015-05-13 20:02:58 -04003294 dev->ib_dev.get_port_immutable = mlx5_port_immutable;
Ira Weinyc7342822016-06-15 02:22:01 -04003295 dev->ib_dev.get_dev_fw_str = get_dev_fw_str;
Eli Coheneff901d2016-03-11 22:58:42 +02003296 if (mlx5_core_is_pf(mdev)) {
3297 dev->ib_dev.get_vf_config = mlx5_ib_get_vf_config;
3298 dev->ib_dev.set_vf_link_state = mlx5_ib_set_vf_link_state;
3299 dev->ib_dev.get_vf_stats = mlx5_ib_get_vf_stats;
3300 dev->ib_dev.set_vf_guid = mlx5_ib_set_vf_guid;
3301 }
Eli Cohene126ba92013-07-07 17:25:49 +03003302
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03003303 dev->ib_dev.disassociate_ucontext = mlx5_ib_disassociate_ucontext;
3304
Saeed Mahameed938fe832015-05-28 22:28:41 +03003305 mlx5_ib_internal_fill_odp_caps(dev);
Haggai Eran8cdd3122014-12-11 17:04:20 +02003306
Matan Barakd2370e02016-02-29 18:05:30 +02003307 if (MLX5_CAP_GEN(mdev, imaicl)) {
3308 dev->ib_dev.alloc_mw = mlx5_ib_alloc_mw;
3309 dev->ib_dev.dealloc_mw = mlx5_ib_dealloc_mw;
3310 dev->ib_dev.uverbs_cmd_mask |=
3311 (1ull << IB_USER_VERBS_CMD_ALLOC_MW) |
3312 (1ull << IB_USER_VERBS_CMD_DEALLOC_MW);
3313 }
3314
Mark Bloch0ad17a82016-06-17 15:10:56 +03003315 if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt) &&
3316 MLX5_CAP_GEN(dev->mdev, retransmission_q_counters)) {
3317 dev->ib_dev.get_hw_stats = mlx5_ib_get_hw_stats;
3318 dev->ib_dev.alloc_hw_stats = mlx5_ib_alloc_hw_stats;
3319 }
3320
Saeed Mahameed938fe832015-05-28 22:28:41 +03003321 if (MLX5_CAP_GEN(mdev, xrc)) {
Eli Cohene126ba92013-07-07 17:25:49 +03003322 dev->ib_dev.alloc_xrcd = mlx5_ib_alloc_xrcd;
3323 dev->ib_dev.dealloc_xrcd = mlx5_ib_dealloc_xrcd;
3324 dev->ib_dev.uverbs_cmd_mask |=
3325 (1ull << IB_USER_VERBS_CMD_OPEN_XRCD) |
3326 (1ull << IB_USER_VERBS_CMD_CLOSE_XRCD);
3327 }
3328
Linus Torvalds048ccca2016-01-23 18:45:06 -08003329 if (mlx5_ib_port_link_layer(&dev->ib_dev, 1) ==
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003330 IB_LINK_LAYER_ETHERNET) {
3331 dev->ib_dev.create_flow = mlx5_ib_create_flow;
3332 dev->ib_dev.destroy_flow = mlx5_ib_destroy_flow;
Yishai Hadas79b20a62016-05-23 15:20:50 +03003333 dev->ib_dev.create_wq = mlx5_ib_create_wq;
3334 dev->ib_dev.modify_wq = mlx5_ib_modify_wq;
3335 dev->ib_dev.destroy_wq = mlx5_ib_destroy_wq;
Yishai Hadasc5f90922016-05-23 15:20:53 +03003336 dev->ib_dev.create_rwq_ind_table = mlx5_ib_create_rwq_ind_table;
3337 dev->ib_dev.destroy_rwq_ind_table = mlx5_ib_destroy_rwq_ind_table;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003338 dev->ib_dev.uverbs_ex_cmd_mask |=
3339 (1ull << IB_USER_VERBS_EX_CMD_CREATE_FLOW) |
Yishai Hadas79b20a62016-05-23 15:20:50 +03003340 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_FLOW) |
3341 (1ull << IB_USER_VERBS_EX_CMD_CREATE_WQ) |
3342 (1ull << IB_USER_VERBS_EX_CMD_MODIFY_WQ) |
Yishai Hadasc5f90922016-05-23 15:20:53 +03003343 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_WQ) |
3344 (1ull << IB_USER_VERBS_EX_CMD_CREATE_RWQ_IND_TBL) |
3345 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_RWQ_IND_TBL);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003346 }
Eli Cohene126ba92013-07-07 17:25:49 +03003347 err = init_node_data(dev);
3348 if (err)
Majd Dibbiny90be7c82016-10-27 16:36:39 +03003349 goto err_free_port;
Eli Cohene126ba92013-07-07 17:25:49 +03003350
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003351 mutex_init(&dev->flow_db.lock);
Eli Cohene126ba92013-07-07 17:25:49 +03003352 mutex_init(&dev->cap_mask_mutex);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03003353 INIT_LIST_HEAD(&dev->qp_list);
3354 spin_lock_init(&dev->reset_flow_resource_lock);
Eli Cohene126ba92013-07-07 17:25:49 +03003355
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003356 if (ll == IB_LINK_LAYER_ETHERNET) {
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003357 err = mlx5_enable_eth(dev);
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003358 if (err)
Majd Dibbiny90be7c82016-10-27 16:36:39 +03003359 goto err_free_port;
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003360 }
3361
Eli Cohene126ba92013-07-07 17:25:49 +03003362 err = create_dev_resources(&dev->devr);
3363 if (err)
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003364 goto err_disable_eth;
Eli Cohene126ba92013-07-07 17:25:49 +03003365
Haggai Eran6aec21f2014-12-11 17:04:23 +02003366 err = mlx5_ib_odp_init_one(dev);
Wei Yongjun281d1a92013-07-30 07:54:26 +08003367 if (err)
Eli Cohene126ba92013-07-07 17:25:49 +03003368 goto err_rsrc;
3369
Kamal Heib45bded22017-01-18 14:10:32 +02003370 if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt)) {
3371 err = mlx5_ib_alloc_q_counters(dev);
3372 if (err)
3373 goto err_odp;
3374 }
Haggai Eran6aec21f2014-12-11 17:04:23 +02003375
Eli Cohen5fe9dec2017-01-03 23:55:25 +02003376 dev->mdev->priv.uar = mlx5_get_uars_page(dev->mdev);
3377 if (!dev->mdev->priv.uar)
3378 goto err_q_cnt;
3379
3380 err = mlx5_alloc_bfreg(dev->mdev, &dev->bfreg, false, false);
3381 if (err)
3382 goto err_uar_page;
3383
3384 err = mlx5_alloc_bfreg(dev->mdev, &dev->fp_bfreg, false, true);
3385 if (err)
3386 goto err_bfreg;
3387
Mark Bloch0837e862016-06-17 15:10:55 +03003388 err = ib_register_device(&dev->ib_dev, NULL);
3389 if (err)
Eli Cohen5fe9dec2017-01-03 23:55:25 +02003390 goto err_fp_bfreg;
Mark Bloch0837e862016-06-17 15:10:55 +03003391
Eli Cohene126ba92013-07-07 17:25:49 +03003392 err = create_umr_res(dev);
3393 if (err)
3394 goto err_dev;
3395
3396 for (i = 0; i < ARRAY_SIZE(mlx5_class_attributes); i++) {
Wei Yongjun281d1a92013-07-30 07:54:26 +08003397 err = device_create_file(&dev->ib_dev.dev,
3398 mlx5_class_attributes[i]);
3399 if (err)
Eli Cohene126ba92013-07-07 17:25:49 +03003400 goto err_umrc;
3401 }
3402
3403 dev->ib_active = true;
3404
Jack Morgenstein9603b612014-07-28 23:30:22 +03003405 return dev;
Eli Cohene126ba92013-07-07 17:25:49 +03003406
3407err_umrc:
3408 destroy_umrc_res(dev);
3409
3410err_dev:
3411 ib_unregister_device(&dev->ib_dev);
3412
Eli Cohen5fe9dec2017-01-03 23:55:25 +02003413err_fp_bfreg:
3414 mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
3415
3416err_bfreg:
3417 mlx5_free_bfreg(dev->mdev, &dev->bfreg);
3418
3419err_uar_page:
3420 mlx5_put_uars_page(dev->mdev, dev->mdev->priv.uar);
3421
Mark Bloch0837e862016-06-17 15:10:55 +03003422err_q_cnt:
Kamal Heib45bded22017-01-18 14:10:32 +02003423 if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt))
3424 mlx5_ib_dealloc_q_counters(dev);
Mark Bloch0837e862016-06-17 15:10:55 +03003425
Haggai Eran6aec21f2014-12-11 17:04:23 +02003426err_odp:
3427 mlx5_ib_odp_remove_one(dev);
3428
Eli Cohene126ba92013-07-07 17:25:49 +03003429err_rsrc:
3430 destroy_dev_resources(&dev->devr);
3431
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003432err_disable_eth:
Aviv Heller5ec8c832016-09-18 20:48:00 +03003433 if (ll == IB_LINK_LAYER_ETHERNET) {
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003434 mlx5_disable_eth(dev);
Or Gerlitzd012f5d2016-11-27 16:51:34 +02003435 mlx5_remove_netdev_notifier(dev);
Aviv Heller5ec8c832016-09-18 20:48:00 +03003436 }
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003437
Mark Bloch0837e862016-06-17 15:10:55 +03003438err_free_port:
3439 kfree(dev->port);
3440
Jack Morgenstein9603b612014-07-28 23:30:22 +03003441err_dealloc:
Eli Cohene126ba92013-07-07 17:25:49 +03003442 ib_dealloc_device((struct ib_device *)dev);
3443
Jack Morgenstein9603b612014-07-28 23:30:22 +03003444 return NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03003445}
3446
Jack Morgenstein9603b612014-07-28 23:30:22 +03003447static void mlx5_ib_remove(struct mlx5_core_dev *mdev, void *context)
Eli Cohene126ba92013-07-07 17:25:49 +03003448{
Jack Morgenstein9603b612014-07-28 23:30:22 +03003449 struct mlx5_ib_dev *dev = context;
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003450 enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev, 1);
Haggai Eran6aec21f2014-12-11 17:04:23 +02003451
Or Gerlitzd012f5d2016-11-27 16:51:34 +02003452 mlx5_remove_netdev_notifier(dev);
Eli Cohene126ba92013-07-07 17:25:49 +03003453 ib_unregister_device(&dev->ib_dev);
Eli Cohen5fe9dec2017-01-03 23:55:25 +02003454 mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
3455 mlx5_free_bfreg(dev->mdev, &dev->bfreg);
3456 mlx5_put_uars_page(dev->mdev, mdev->priv.uar);
Kamal Heib45bded22017-01-18 14:10:32 +02003457 if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt))
3458 mlx5_ib_dealloc_q_counters(dev);
Eli Coheneefd56e2014-09-14 16:47:50 +03003459 destroy_umrc_res(dev);
Haggai Eran6aec21f2014-12-11 17:04:23 +02003460 mlx5_ib_odp_remove_one(dev);
Eli Cohene126ba92013-07-07 17:25:49 +03003461 destroy_dev_resources(&dev->devr);
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003462 if (ll == IB_LINK_LAYER_ETHERNET)
Or Gerlitz45f95ac2016-11-27 16:51:35 +02003463 mlx5_disable_eth(dev);
Mark Bloch0837e862016-06-17 15:10:55 +03003464 kfree(dev->port);
Eli Cohene126ba92013-07-07 17:25:49 +03003465 ib_dealloc_device(&dev->ib_dev);
3466}
3467
Jack Morgenstein9603b612014-07-28 23:30:22 +03003468static struct mlx5_interface mlx5_ib_interface = {
3469 .add = mlx5_ib_add,
3470 .remove = mlx5_ib_remove,
3471 .event = mlx5_ib_event,
Artemy Kovalyovd9aaed82017-01-02 11:37:46 +02003472#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
3473 .pfault = mlx5_ib_pfault,
3474#endif
Saeed Mahameed64613d942015-04-02 17:07:34 +03003475 .protocol = MLX5_INTERFACE_PROTOCOL_IB,
Eli Cohene126ba92013-07-07 17:25:49 +03003476};
3477
3478static int __init mlx5_ib_init(void)
3479{
Haggai Eran6aec21f2014-12-11 17:04:23 +02003480 int err;
3481
Jack Morgenstein9603b612014-07-28 23:30:22 +03003482 if (deprecated_prof_sel != 2)
3483 pr_warn("prof_sel is deprecated for mlx5_ib, set it for mlx5_core\n");
3484
Haggai Eran6aec21f2014-12-11 17:04:23 +02003485 err = mlx5_register_interface(&mlx5_ib_interface);
Haggai Eran6aec21f2014-12-11 17:04:23 +02003486
3487 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03003488}
3489
3490static void __exit mlx5_ib_cleanup(void)
3491{
Jack Morgenstein9603b612014-07-28 23:30:22 +03003492 mlx5_unregister_interface(&mlx5_ib_interface);
Eli Cohene126ba92013-07-07 17:25:49 +03003493}
3494
3495module_init(mlx5_ib_init);
3496module_exit(mlx5_ib_cleanup);