blob: a16207c333336be21da109ecf08c333af2f4309c [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) {
Aviv Heller5ec8c832016-09-18 20:48:00 +0300130 struct ib_event ibev = {0};
131
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 Dibbiny1b5daf12015-06-04 19:30:46 +0300328static int mlx5_use_mad_ifc(struct mlx5_ib_dev *dev)
329{
Noa Osherovich7fae6652016-09-12 19:16:23 +0300330 if (MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_IB)
331 return !MLX5_CAP_GEN(dev->mdev, ib_virt);
332 return 0;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300333}
334
335enum {
336 MLX5_VPORT_ACCESS_METHOD_MAD,
337 MLX5_VPORT_ACCESS_METHOD_HCA,
338 MLX5_VPORT_ACCESS_METHOD_NIC,
339};
340
341static int mlx5_get_vport_access_method(struct ib_device *ibdev)
342{
343 if (mlx5_use_mad_ifc(to_mdev(ibdev)))
344 return MLX5_VPORT_ACCESS_METHOD_MAD;
345
Achiad Shochatebd61f62015-12-23 18:47:16 +0200346 if (mlx5_ib_port_link_layer(ibdev, 1) ==
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300347 IB_LINK_LAYER_ETHERNET)
348 return MLX5_VPORT_ACCESS_METHOD_NIC;
349
350 return MLX5_VPORT_ACCESS_METHOD_HCA;
351}
352
Eran Ben Elishada7525d2015-12-14 16:34:10 +0200353static void get_atomic_caps(struct mlx5_ib_dev *dev,
354 struct ib_device_attr *props)
355{
356 u8 tmp;
357 u8 atomic_operations = MLX5_CAP_ATOMIC(dev->mdev, atomic_operations);
358 u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_qp);
359 u8 atomic_req_8B_endianness_mode =
360 MLX5_CAP_ATOMIC(dev->mdev, atomic_req_8B_endianess_mode);
361
362 /* Check if HW supports 8 bytes standard atomic operations and capable
363 * of host endianness respond
364 */
365 tmp = MLX5_ATOMIC_OPS_CMP_SWAP | MLX5_ATOMIC_OPS_FETCH_ADD;
366 if (((atomic_operations & tmp) == tmp) &&
367 (atomic_size_qp & MLX5_ATOMIC_SIZE_QP_8BYTES) &&
368 (atomic_req_8B_endianness_mode)) {
369 props->atomic_cap = IB_ATOMIC_HCA;
370 } else {
371 props->atomic_cap = IB_ATOMIC_NONE;
372 }
373}
374
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300375static int mlx5_query_system_image_guid(struct ib_device *ibdev,
376 __be64 *sys_image_guid)
377{
378 struct mlx5_ib_dev *dev = to_mdev(ibdev);
379 struct mlx5_core_dev *mdev = dev->mdev;
380 u64 tmp;
381 int err;
382
383 switch (mlx5_get_vport_access_method(ibdev)) {
384 case MLX5_VPORT_ACCESS_METHOD_MAD:
385 return mlx5_query_mad_ifc_system_image_guid(ibdev,
386 sys_image_guid);
387
388 case MLX5_VPORT_ACCESS_METHOD_HCA:
389 err = mlx5_query_hca_vport_system_image_guid(mdev, &tmp);
Achiad Shochat3f89a642015-12-23 18:47:21 +0200390 break;
391
392 case MLX5_VPORT_ACCESS_METHOD_NIC:
393 err = mlx5_query_nic_vport_system_image_guid(mdev, &tmp);
394 break;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300395
396 default:
397 return -EINVAL;
398 }
Achiad Shochat3f89a642015-12-23 18:47:21 +0200399
400 if (!err)
401 *sys_image_guid = cpu_to_be64(tmp);
402
403 return err;
404
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300405}
406
407static int mlx5_query_max_pkeys(struct ib_device *ibdev,
408 u16 *max_pkeys)
409{
410 struct mlx5_ib_dev *dev = to_mdev(ibdev);
411 struct mlx5_core_dev *mdev = dev->mdev;
412
413 switch (mlx5_get_vport_access_method(ibdev)) {
414 case MLX5_VPORT_ACCESS_METHOD_MAD:
415 return mlx5_query_mad_ifc_max_pkeys(ibdev, max_pkeys);
416
417 case MLX5_VPORT_ACCESS_METHOD_HCA:
418 case MLX5_VPORT_ACCESS_METHOD_NIC:
419 *max_pkeys = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev,
420 pkey_table_size));
421 return 0;
422
423 default:
424 return -EINVAL;
425 }
426}
427
428static int mlx5_query_vendor_id(struct ib_device *ibdev,
429 u32 *vendor_id)
430{
431 struct mlx5_ib_dev *dev = to_mdev(ibdev);
432
433 switch (mlx5_get_vport_access_method(ibdev)) {
434 case MLX5_VPORT_ACCESS_METHOD_MAD:
435 return mlx5_query_mad_ifc_vendor_id(ibdev, vendor_id);
436
437 case MLX5_VPORT_ACCESS_METHOD_HCA:
438 case MLX5_VPORT_ACCESS_METHOD_NIC:
439 return mlx5_core_query_vendor_id(dev->mdev, vendor_id);
440
441 default:
442 return -EINVAL;
443 }
444}
445
446static int mlx5_query_node_guid(struct mlx5_ib_dev *dev,
447 __be64 *node_guid)
448{
449 u64 tmp;
450 int err;
451
452 switch (mlx5_get_vport_access_method(&dev->ib_dev)) {
453 case MLX5_VPORT_ACCESS_METHOD_MAD:
454 return mlx5_query_mad_ifc_node_guid(dev, node_guid);
455
456 case MLX5_VPORT_ACCESS_METHOD_HCA:
457 err = mlx5_query_hca_vport_node_guid(dev->mdev, &tmp);
Achiad Shochat3f89a642015-12-23 18:47:21 +0200458 break;
459
460 case MLX5_VPORT_ACCESS_METHOD_NIC:
461 err = mlx5_query_nic_vport_node_guid(dev->mdev, &tmp);
462 break;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300463
464 default:
465 return -EINVAL;
466 }
Achiad Shochat3f89a642015-12-23 18:47:21 +0200467
468 if (!err)
469 *node_guid = cpu_to_be64(tmp);
470
471 return err;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300472}
473
474struct mlx5_reg_node_desc {
Yuval Shaiabd99fde2016-08-25 10:57:07 -0700475 u8 desc[IB_DEVICE_NODE_DESC_MAX];
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300476};
477
478static int mlx5_query_node_desc(struct mlx5_ib_dev *dev, char *node_desc)
479{
480 struct mlx5_reg_node_desc in;
481
482 if (mlx5_use_mad_ifc(dev))
483 return mlx5_query_mad_ifc_node_desc(dev, node_desc);
484
485 memset(&in, 0, sizeof(in));
486
487 return mlx5_core_access_reg(dev->mdev, &in, sizeof(in), node_desc,
488 sizeof(struct mlx5_reg_node_desc),
489 MLX5_REG_NODE_DESC, 0, 0);
490}
491
Eli Cohene126ba92013-07-07 17:25:49 +0300492static int mlx5_ib_query_device(struct ib_device *ibdev,
Matan Barak2528e332015-06-11 16:35:25 +0300493 struct ib_device_attr *props,
494 struct ib_udata *uhw)
Eli Cohene126ba92013-07-07 17:25:49 +0300495{
496 struct mlx5_ib_dev *dev = to_mdev(ibdev);
Saeed Mahameed938fe832015-05-28 22:28:41 +0300497 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +0300498 int err = -ENOMEM;
Eli Cohen288c01b2016-10-27 16:36:45 +0300499 int max_sq_desc;
Eli Cohene126ba92013-07-07 17:25:49 +0300500 int max_rq_sg;
501 int max_sq_sg;
Sagi Grimberge0238a62015-07-21 14:40:12 +0300502 u64 min_page_size = 1ull << MLX5_CAP_GEN(mdev, log_pg_sz);
Bodong Wang402ca532016-06-17 15:02:20 +0300503 struct mlx5_ib_query_device_resp resp = {};
504 size_t resp_len;
505 u64 max_tso;
Eli Cohene126ba92013-07-07 17:25:49 +0300506
Bodong Wang402ca532016-06-17 15:02:20 +0300507 resp_len = sizeof(resp.comp_mask) + sizeof(resp.response_length);
508 if (uhw->outlen && uhw->outlen < resp_len)
509 return -EINVAL;
510 else
511 resp.response_length = resp_len;
512
513 if (uhw->inlen && !ib_is_udata_cleared(uhw, 0, uhw->inlen))
Matan Barak2528e332015-06-11 16:35:25 +0300514 return -EINVAL;
515
Eli Cohene126ba92013-07-07 17:25:49 +0300516 memset(props, 0, sizeof(*props));
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300517 err = mlx5_query_system_image_guid(ibdev,
518 &props->sys_image_guid);
519 if (err)
520 return err;
521
522 err = mlx5_query_max_pkeys(ibdev, &props->max_pkeys);
523 if (err)
524 return err;
525
526 err = mlx5_query_vendor_id(ibdev, &props->vendor_id);
527 if (err)
528 return err;
Eli Cohene126ba92013-07-07 17:25:49 +0300529
Jack Morgenstein9603b612014-07-28 23:30:22 +0300530 props->fw_ver = ((u64)fw_rev_maj(dev->mdev) << 32) |
531 (fw_rev_min(dev->mdev) << 16) |
532 fw_rev_sub(dev->mdev);
Eli Cohene126ba92013-07-07 17:25:49 +0300533 props->device_cap_flags = IB_DEVICE_CHANGE_PHY_PORT |
534 IB_DEVICE_PORT_ACTIVE_EVENT |
535 IB_DEVICE_SYS_IMAGE_GUID |
Eli Cohen1a4c3a32014-02-06 17:41:25 +0200536 IB_DEVICE_RC_RNR_NAK_GEN;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300537
538 if (MLX5_CAP_GEN(mdev, pkv))
Eli Cohene126ba92013-07-07 17:25:49 +0300539 props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300540 if (MLX5_CAP_GEN(mdev, qkv))
Eli Cohene126ba92013-07-07 17:25:49 +0300541 props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300542 if (MLX5_CAP_GEN(mdev, apm))
Eli Cohene126ba92013-07-07 17:25:49 +0300543 props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300544 if (MLX5_CAP_GEN(mdev, xrc))
Eli Cohene126ba92013-07-07 17:25:49 +0300545 props->device_cap_flags |= IB_DEVICE_XRC;
Matan Barakd2370e02016-02-29 18:05:30 +0200546 if (MLX5_CAP_GEN(mdev, imaicl)) {
547 props->device_cap_flags |= IB_DEVICE_MEM_WINDOW |
548 IB_DEVICE_MEM_WINDOW_TYPE_2B;
549 props->max_mw = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
Sagi Grimbergb005d312016-02-29 19:07:33 +0200550 /* We support 'Gappy' memory registration too */
551 props->device_cap_flags |= IB_DEVICE_SG_GAPS_REG;
Matan Barakd2370e02016-02-29 18:05:30 +0200552 }
Eli Cohene126ba92013-07-07 17:25:49 +0300553 props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300554 if (MLX5_CAP_GEN(mdev, sho)) {
Sagi Grimberg2dea9092014-02-23 14:19:13 +0200555 props->device_cap_flags |= IB_DEVICE_SIGNATURE_HANDOVER;
556 /* At this stage no support for signature handover */
557 props->sig_prot_cap = IB_PROT_T10DIF_TYPE_1 |
558 IB_PROT_T10DIF_TYPE_2 |
559 IB_PROT_T10DIF_TYPE_3;
560 props->sig_guard_cap = IB_GUARD_T10DIF_CRC |
561 IB_GUARD_T10DIF_CSUM;
562 }
Saeed Mahameed938fe832015-05-28 22:28:41 +0300563 if (MLX5_CAP_GEN(mdev, block_lb_mc))
Eli Cohenf360d882014-04-02 00:10:16 +0300564 props->device_cap_flags |= IB_DEVICE_BLOCK_MULTICAST_LOOPBACK;
Eli Cohene126ba92013-07-07 17:25:49 +0300565
Bodong Wang402ca532016-06-17 15:02:20 +0300566 if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads)) {
567 if (MLX5_CAP_ETH(mdev, csum_cap))
Bodong Wang88115fe2015-12-18 13:53:20 +0200568 props->device_cap_flags |= IB_DEVICE_RAW_IP_CSUM;
569
Bodong Wang402ca532016-06-17 15:02:20 +0300570 if (field_avail(typeof(resp), tso_caps, uhw->outlen)) {
571 max_tso = MLX5_CAP_ETH(mdev, max_lso_cap);
572 if (max_tso) {
573 resp.tso_caps.max_tso = 1 << max_tso;
574 resp.tso_caps.supported_qpts |=
575 1 << IB_QPT_RAW_PACKET;
576 resp.response_length += sizeof(resp.tso_caps);
577 }
578 }
Yishai Hadas31f69a82016-08-28 11:28:45 +0300579
580 if (field_avail(typeof(resp), rss_caps, uhw->outlen)) {
581 resp.rss_caps.rx_hash_function =
582 MLX5_RX_HASH_FUNC_TOEPLITZ;
583 resp.rss_caps.rx_hash_fields_mask =
584 MLX5_RX_HASH_SRC_IPV4 |
585 MLX5_RX_HASH_DST_IPV4 |
586 MLX5_RX_HASH_SRC_IPV6 |
587 MLX5_RX_HASH_DST_IPV6 |
588 MLX5_RX_HASH_SRC_PORT_TCP |
589 MLX5_RX_HASH_DST_PORT_TCP |
590 MLX5_RX_HASH_SRC_PORT_UDP |
591 MLX5_RX_HASH_DST_PORT_UDP;
592 resp.response_length += sizeof(resp.rss_caps);
593 }
594 } else {
595 if (field_avail(typeof(resp), tso_caps, uhw->outlen))
596 resp.response_length += sizeof(resp.tso_caps);
597 if (field_avail(typeof(resp), rss_caps, uhw->outlen))
598 resp.response_length += sizeof(resp.rss_caps);
Bodong Wang402ca532016-06-17 15:02:20 +0300599 }
600
Erez Shitritf0313962016-02-21 16:27:17 +0200601 if (MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) {
602 props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
603 props->device_cap_flags |= IB_DEVICE_UD_TSO;
604 }
605
Majd Dibbinycff5a0f2016-04-17 17:19:38 +0300606 if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
607 MLX5_CAP_ETH(dev->mdev, scatter_fcs))
608 props->device_cap_flags |= IB_DEVICE_RAW_SCATTER_FCS;
609
Maor Gottliebda6d6ba32016-06-04 15:15:28 +0300610 if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS))
611 props->device_cap_flags |= IB_DEVICE_MANAGED_FLOW_STEERING;
612
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300613 props->vendor_part_id = mdev->pdev->device;
614 props->hw_ver = mdev->pdev->revision;
Eli Cohene126ba92013-07-07 17:25:49 +0300615
616 props->max_mr_size = ~0ull;
Sagi Grimberge0238a62015-07-21 14:40:12 +0300617 props->page_size_cap = ~(min_page_size - 1);
Saeed Mahameed938fe832015-05-28 22:28:41 +0300618 props->max_qp = 1 << MLX5_CAP_GEN(mdev, log_max_qp);
619 props->max_qp_wr = 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
620 max_rq_sg = MLX5_CAP_GEN(mdev, max_wqe_sz_rq) /
621 sizeof(struct mlx5_wqe_data_seg);
Eli Cohen288c01b2016-10-27 16:36:45 +0300622 max_sq_desc = min_t(int, MLX5_CAP_GEN(mdev, max_wqe_sz_sq), 512);
623 max_sq_sg = (max_sq_desc - sizeof(struct mlx5_wqe_ctrl_seg) -
624 sizeof(struct mlx5_wqe_raddr_seg)) /
625 sizeof(struct mlx5_wqe_data_seg);
Eli Cohene126ba92013-07-07 17:25:49 +0300626 props->max_sge = min(max_rq_sg, max_sq_sg);
Sagi Grimberg986ef952016-03-31 19:03:25 +0300627 props->max_sge_rd = MLX5_MAX_SGE_RD;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300628 props->max_cq = 1 << MLX5_CAP_GEN(mdev, log_max_cq);
Leon Romanovsky9f177682016-01-14 08:11:40 +0200629 props->max_cqe = (1 << MLX5_CAP_GEN(mdev, log_max_cq_sz)) - 1;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300630 props->max_mr = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
631 props->max_pd = 1 << MLX5_CAP_GEN(mdev, log_max_pd);
632 props->max_qp_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_req_qp);
633 props->max_qp_init_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_res_qp);
634 props->max_srq = 1 << MLX5_CAP_GEN(mdev, log_max_srq);
635 props->max_srq_wr = (1 << MLX5_CAP_GEN(mdev, log_max_srq_sz)) - 1;
636 props->local_ca_ack_delay = MLX5_CAP_GEN(mdev, local_ca_ack_delay);
Eli Cohene126ba92013-07-07 17:25:49 +0300637 props->max_res_rd_atom = props->max_qp_rd_atom * props->max_qp;
Eli Cohene126ba92013-07-07 17:25:49 +0300638 props->max_srq_sge = max_rq_sg - 1;
Sagi Grimberg911f4332016-03-03 13:37:51 +0200639 props->max_fast_reg_page_list_len =
640 1 << MLX5_CAP_GEN(mdev, log_max_klm_list_size);
Eran Ben Elishada7525d2015-12-14 16:34:10 +0200641 get_atomic_caps(dev, props);
Eli Cohen81bea282013-09-11 16:35:30 +0300642 props->masked_atomic_cap = IB_ATOMIC_NONE;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300643 props->max_mcast_grp = 1 << MLX5_CAP_GEN(mdev, log_max_mcg);
644 props->max_mcast_qp_attach = MLX5_CAP_GEN(mdev, max_qp_mcg);
Eli Cohene126ba92013-07-07 17:25:49 +0300645 props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
646 props->max_mcast_grp;
647 props->max_map_per_fmr = INT_MAX; /* no limit in ConnectIB */
Maor Gottlieb86695a62016-10-27 16:36:38 +0300648 props->max_ah = INT_MAX;
Matan Barak7c60bcb2015-12-15 20:30:11 +0200649 props->hca_core_clock = MLX5_CAP_GEN(mdev, device_frequency_khz);
650 props->timestamp_mask = 0x7FFFFFFFFFFFFFFFULL;
Eli Cohene126ba92013-07-07 17:25:49 +0300651
Haggai Eran8cdd3122014-12-11 17:04:20 +0200652#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
Saeed Mahameed938fe832015-05-28 22:28:41 +0300653 if (MLX5_CAP_GEN(mdev, pg))
Haggai Eran8cdd3122014-12-11 17:04:20 +0200654 props->device_cap_flags |= IB_DEVICE_ON_DEMAND_PAGING;
655 props->odp_caps = dev->odp_caps;
656#endif
657
Leon Romanovsky051f2632015-12-20 12:16:11 +0200658 if (MLX5_CAP_GEN(mdev, cd))
659 props->device_cap_flags |= IB_DEVICE_CROSS_CHANNEL;
660
Eli Coheneff901d2016-03-11 22:58:42 +0200661 if (!mlx5_core_is_pf(mdev))
662 props->device_cap_flags |= IB_DEVICE_VIRTUAL_FUNCTION;
663
Yishai Hadas31f69a82016-08-28 11:28:45 +0300664 if (mlx5_ib_port_link_layer(ibdev, 1) ==
665 IB_LINK_LAYER_ETHERNET) {
666 props->rss_caps.max_rwq_indirection_tables =
667 1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt);
668 props->rss_caps.max_rwq_indirection_table_size =
669 1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt_size);
670 props->rss_caps.supported_qpts = 1 << IB_QPT_RAW_PACKET;
671 props->max_wq_type_rq =
672 1 << MLX5_CAP_GEN(dev->mdev, log_max_rq);
673 }
674
Bodong Wang191ded42016-10-31 12:15:21 +0200675 if (field_avail(typeof(resp), mlx5_ib_support_multi_pkt_send_wqes,
676 uhw->outlen)) {
677 resp.mlx5_ib_support_multi_pkt_send_wqes =
678 MLX5_CAP_ETH(mdev, multi_pkt_send_wqe);
679 resp.response_length +=
680 sizeof(resp.mlx5_ib_support_multi_pkt_send_wqes);
681 }
682
683 if (field_avail(typeof(resp), reserved, uhw->outlen))
684 resp.response_length += sizeof(resp.reserved);
685
Bodong Wang402ca532016-06-17 15:02:20 +0300686 if (uhw->outlen) {
687 err = ib_copy_to_udata(uhw, &resp, resp.response_length);
688
689 if (err)
690 return err;
691 }
692
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300693 return 0;
694}
Eli Cohene126ba92013-07-07 17:25:49 +0300695
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300696enum mlx5_ib_width {
697 MLX5_IB_WIDTH_1X = 1 << 0,
698 MLX5_IB_WIDTH_2X = 1 << 1,
699 MLX5_IB_WIDTH_4X = 1 << 2,
700 MLX5_IB_WIDTH_8X = 1 << 3,
701 MLX5_IB_WIDTH_12X = 1 << 4
702};
703
704static int translate_active_width(struct ib_device *ibdev, u8 active_width,
705 u8 *ib_width)
706{
707 struct mlx5_ib_dev *dev = to_mdev(ibdev);
708 int err = 0;
709
710 if (active_width & MLX5_IB_WIDTH_1X) {
711 *ib_width = IB_WIDTH_1X;
712 } else if (active_width & MLX5_IB_WIDTH_2X) {
713 mlx5_ib_dbg(dev, "active_width %d is not supported by IB spec\n",
714 (int)active_width);
715 err = -EINVAL;
716 } else if (active_width & MLX5_IB_WIDTH_4X) {
717 *ib_width = IB_WIDTH_4X;
718 } else if (active_width & MLX5_IB_WIDTH_8X) {
719 *ib_width = IB_WIDTH_8X;
720 } else if (active_width & MLX5_IB_WIDTH_12X) {
721 *ib_width = IB_WIDTH_12X;
722 } else {
723 mlx5_ib_dbg(dev, "Invalid active_width %d\n",
724 (int)active_width);
725 err = -EINVAL;
726 }
727
728 return err;
729}
730
731static int mlx5_mtu_to_ib_mtu(int mtu)
732{
733 switch (mtu) {
734 case 256: return 1;
735 case 512: return 2;
736 case 1024: return 3;
737 case 2048: return 4;
738 case 4096: return 5;
739 default:
740 pr_warn("invalid mtu\n");
741 return -1;
742 }
743}
744
745enum ib_max_vl_num {
746 __IB_MAX_VL_0 = 1,
747 __IB_MAX_VL_0_1 = 2,
748 __IB_MAX_VL_0_3 = 3,
749 __IB_MAX_VL_0_7 = 4,
750 __IB_MAX_VL_0_14 = 5,
751};
752
753enum mlx5_vl_hw_cap {
754 MLX5_VL_HW_0 = 1,
755 MLX5_VL_HW_0_1 = 2,
756 MLX5_VL_HW_0_2 = 3,
757 MLX5_VL_HW_0_3 = 4,
758 MLX5_VL_HW_0_4 = 5,
759 MLX5_VL_HW_0_5 = 6,
760 MLX5_VL_HW_0_6 = 7,
761 MLX5_VL_HW_0_7 = 8,
762 MLX5_VL_HW_0_14 = 15
763};
764
765static int translate_max_vl_num(struct ib_device *ibdev, u8 vl_hw_cap,
766 u8 *max_vl_num)
767{
768 switch (vl_hw_cap) {
769 case MLX5_VL_HW_0:
770 *max_vl_num = __IB_MAX_VL_0;
771 break;
772 case MLX5_VL_HW_0_1:
773 *max_vl_num = __IB_MAX_VL_0_1;
774 break;
775 case MLX5_VL_HW_0_3:
776 *max_vl_num = __IB_MAX_VL_0_3;
777 break;
778 case MLX5_VL_HW_0_7:
779 *max_vl_num = __IB_MAX_VL_0_7;
780 break;
781 case MLX5_VL_HW_0_14:
782 *max_vl_num = __IB_MAX_VL_0_14;
783 break;
784
785 default:
786 return -EINVAL;
787 }
788
789 return 0;
790}
791
792static int mlx5_query_hca_port(struct ib_device *ibdev, u8 port,
793 struct ib_port_attr *props)
794{
795 struct mlx5_ib_dev *dev = to_mdev(ibdev);
796 struct mlx5_core_dev *mdev = dev->mdev;
797 struct mlx5_hca_vport_context *rep;
Saeed Mahameed046339e2016-04-22 00:33:03 +0300798 u16 max_mtu;
799 u16 oper_mtu;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300800 int err;
801 u8 ib_link_width_oper;
802 u8 vl_hw_cap;
803
804 rep = kzalloc(sizeof(*rep), GFP_KERNEL);
805 if (!rep) {
806 err = -ENOMEM;
807 goto out;
808 }
809
810 memset(props, 0, sizeof(*props));
811
812 err = mlx5_query_hca_vport_context(mdev, 0, port, 0, rep);
813 if (err)
814 goto out;
815
816 props->lid = rep->lid;
817 props->lmc = rep->lmc;
818 props->sm_lid = rep->sm_lid;
819 props->sm_sl = rep->sm_sl;
820 props->state = rep->vport_state;
821 props->phys_state = rep->port_physical_state;
822 props->port_cap_flags = rep->cap_mask1;
823 props->gid_tbl_len = mlx5_get_gid_table_len(MLX5_CAP_GEN(mdev, gid_table_size));
824 props->max_msg_sz = 1 << MLX5_CAP_GEN(mdev, log_max_msg);
825 props->pkey_tbl_len = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, pkey_table_size));
826 props->bad_pkey_cntr = rep->pkey_violation_counter;
827 props->qkey_viol_cntr = rep->qkey_violation_counter;
828 props->subnet_timeout = rep->subnet_timeout;
829 props->init_type_reply = rep->init_type_reply;
Eli Coheneff901d2016-03-11 22:58:42 +0200830 props->grh_required = rep->grh_required;
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300831
832 err = mlx5_query_port_link_width_oper(mdev, &ib_link_width_oper, port);
833 if (err)
834 goto out;
835
836 err = translate_active_width(ibdev, ib_link_width_oper,
837 &props->active_width);
838 if (err)
839 goto out;
Noa Osherovichd5beb7f2016-06-02 10:47:53 +0300840 err = mlx5_query_port_ib_proto_oper(mdev, &props->active_speed, port);
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300841 if (err)
842 goto out;
843
Saeed Mahameedfacc9692015-06-11 14:47:27 +0300844 mlx5_query_port_max_mtu(mdev, &max_mtu, port);
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300845
846 props->max_mtu = mlx5_mtu_to_ib_mtu(max_mtu);
847
Saeed Mahameedfacc9692015-06-11 14:47:27 +0300848 mlx5_query_port_oper_mtu(mdev, &oper_mtu, port);
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300849
850 props->active_mtu = mlx5_mtu_to_ib_mtu(oper_mtu);
851
852 err = mlx5_query_port_vl_hw_cap(mdev, &vl_hw_cap, port);
853 if (err)
854 goto out;
855
856 err = translate_max_vl_num(ibdev, vl_hw_cap,
857 &props->max_vl_num);
858out:
859 kfree(rep);
Eli Cohene126ba92013-07-07 17:25:49 +0300860 return err;
861}
862
863int mlx5_ib_query_port(struct ib_device *ibdev, u8 port,
864 struct ib_port_attr *props)
865{
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300866 switch (mlx5_get_vport_access_method(ibdev)) {
867 case MLX5_VPORT_ACCESS_METHOD_MAD:
868 return mlx5_query_mad_ifc_port(ibdev, port, props);
Eli Cohene126ba92013-07-07 17:25:49 +0300869
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300870 case MLX5_VPORT_ACCESS_METHOD_HCA:
871 return mlx5_query_hca_port(ibdev, port, props);
872
Achiad Shochat3f89a642015-12-23 18:47:21 +0200873 case MLX5_VPORT_ACCESS_METHOD_NIC:
874 return mlx5_query_port_roce(ibdev, port, props);
875
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300876 default:
Eli Cohene126ba92013-07-07 17:25:49 +0300877 return -EINVAL;
878 }
Eli Cohene126ba92013-07-07 17:25:49 +0300879}
880
881static int mlx5_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
882 union ib_gid *gid)
883{
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300884 struct mlx5_ib_dev *dev = to_mdev(ibdev);
885 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +0300886
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300887 switch (mlx5_get_vport_access_method(ibdev)) {
888 case MLX5_VPORT_ACCESS_METHOD_MAD:
889 return mlx5_query_mad_ifc_gids(ibdev, port, index, gid);
Eli Cohene126ba92013-07-07 17:25:49 +0300890
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300891 case MLX5_VPORT_ACCESS_METHOD_HCA:
892 return mlx5_query_hca_vport_gid(mdev, 0, port, 0, index, gid);
Eli Cohene126ba92013-07-07 17:25:49 +0300893
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300894 default:
895 return -EINVAL;
896 }
Eli Cohene126ba92013-07-07 17:25:49 +0300897
Eli Cohene126ba92013-07-07 17:25:49 +0300898}
899
900static int mlx5_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
901 u16 *pkey)
902{
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300903 struct mlx5_ib_dev *dev = to_mdev(ibdev);
904 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +0300905
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300906 switch (mlx5_get_vport_access_method(ibdev)) {
907 case MLX5_VPORT_ACCESS_METHOD_MAD:
908 return mlx5_query_mad_ifc_pkey(ibdev, port, index, pkey);
Eli Cohene126ba92013-07-07 17:25:49 +0300909
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300910 case MLX5_VPORT_ACCESS_METHOD_HCA:
911 case MLX5_VPORT_ACCESS_METHOD_NIC:
912 return mlx5_query_hca_vport_pkey(mdev, 0, port, 0, index,
913 pkey);
914 default:
915 return -EINVAL;
916 }
Eli Cohene126ba92013-07-07 17:25:49 +0300917}
918
Eli Cohene126ba92013-07-07 17:25:49 +0300919static int mlx5_ib_modify_device(struct ib_device *ibdev, int mask,
920 struct ib_device_modify *props)
921{
922 struct mlx5_ib_dev *dev = to_mdev(ibdev);
923 struct mlx5_reg_node_desc in;
924 struct mlx5_reg_node_desc out;
925 int err;
926
927 if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
928 return -EOPNOTSUPP;
929
930 if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
931 return 0;
932
933 /*
934 * If possible, pass node desc to FW, so it can generate
935 * a 144 trap. If cmd fails, just ignore.
936 */
Yuval Shaiabd99fde2016-08-25 10:57:07 -0700937 memcpy(&in, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
Jack Morgenstein9603b612014-07-28 23:30:22 +0300938 err = mlx5_core_access_reg(dev->mdev, &in, sizeof(in), &out,
Eli Cohene126ba92013-07-07 17:25:49 +0300939 sizeof(out), MLX5_REG_NODE_DESC, 0, 1);
940 if (err)
941 return err;
942
Yuval Shaiabd99fde2016-08-25 10:57:07 -0700943 memcpy(ibdev->node_desc, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
Eli Cohene126ba92013-07-07 17:25:49 +0300944
945 return err;
946}
947
948static int mlx5_ib_modify_port(struct ib_device *ibdev, u8 port, int mask,
949 struct ib_port_modify *props)
950{
951 struct mlx5_ib_dev *dev = to_mdev(ibdev);
952 struct ib_port_attr attr;
953 u32 tmp;
954 int err;
955
956 mutex_lock(&dev->cap_mask_mutex);
957
958 err = mlx5_ib_query_port(ibdev, port, &attr);
959 if (err)
960 goto out;
961
962 tmp = (attr.port_cap_flags | props->set_port_cap_mask) &
963 ~props->clr_port_cap_mask;
964
Jack Morgenstein9603b612014-07-28 23:30:22 +0300965 err = mlx5_set_port_caps(dev->mdev, port, tmp);
Eli Cohene126ba92013-07-07 17:25:49 +0300966
967out:
968 mutex_unlock(&dev->cap_mask_mutex);
969 return err;
970}
971
972static struct ib_ucontext *mlx5_ib_alloc_ucontext(struct ib_device *ibdev,
973 struct ib_udata *udata)
974{
975 struct mlx5_ib_dev *dev = to_mdev(ibdev);
Matan Barakb368d7c2015-12-15 20:30:12 +0200976 struct mlx5_ib_alloc_ucontext_req_v2 req = {};
977 struct mlx5_ib_alloc_ucontext_resp resp = {};
Eli Cohene126ba92013-07-07 17:25:49 +0300978 struct mlx5_ib_ucontext *context;
979 struct mlx5_uuar_info *uuari;
980 struct mlx5_uar *uars;
Eli Cohenc1be5232014-01-14 17:45:12 +0200981 int gross_uuars;
Eli Cohene126ba92013-07-07 17:25:49 +0300982 int num_uars;
Eli Cohen78c0f982014-01-30 13:49:48 +0200983 int ver;
Eli Cohene126ba92013-07-07 17:25:49 +0300984 int uuarn;
985 int err;
986 int i;
Jack Morgensteinf241e742014-07-28 23:30:23 +0300987 size_t reqlen;
Majd Dibbinya168a41c2016-01-28 17:51:47 +0200988 size_t min_req_v2 = offsetof(struct mlx5_ib_alloc_ucontext_req_v2,
989 max_cqe_version);
Eli Cohene126ba92013-07-07 17:25:49 +0300990
991 if (!dev->ib_active)
992 return ERR_PTR(-EAGAIN);
993
Haggai Abramovskydfbee852016-01-14 19:12:56 +0200994 if (udata->inlen < sizeof(struct ib_uverbs_cmd_hdr))
995 return ERR_PTR(-EINVAL);
996
Eli Cohen78c0f982014-01-30 13:49:48 +0200997 reqlen = udata->inlen - sizeof(struct ib_uverbs_cmd_hdr);
998 if (reqlen == sizeof(struct mlx5_ib_alloc_ucontext_req))
999 ver = 0;
Majd Dibbinya168a41c2016-01-28 17:51:47 +02001000 else if (reqlen >= min_req_v2)
Eli Cohen78c0f982014-01-30 13:49:48 +02001001 ver = 2;
1002 else
1003 return ERR_PTR(-EINVAL);
1004
Matan Barakb368d7c2015-12-15 20:30:12 +02001005 err = ib_copy_from_udata(&req, udata, min(reqlen, sizeof(req)));
Eli Cohene126ba92013-07-07 17:25:49 +03001006 if (err)
1007 return ERR_PTR(err);
1008
Matan Barakb368d7c2015-12-15 20:30:12 +02001009 if (req.flags)
Eli Cohen78c0f982014-01-30 13:49:48 +02001010 return ERR_PTR(-EINVAL);
1011
Eli Cohene126ba92013-07-07 17:25:49 +03001012 if (req.total_num_uuars > MLX5_MAX_UUARS)
1013 return ERR_PTR(-ENOMEM);
1014
1015 if (req.total_num_uuars == 0)
1016 return ERR_PTR(-EINVAL);
1017
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001018 if (req.comp_mask || req.reserved0 || req.reserved1 || req.reserved2)
Matan Barakb368d7c2015-12-15 20:30:12 +02001019 return ERR_PTR(-EOPNOTSUPP);
1020
1021 if (reqlen > sizeof(req) &&
1022 !ib_is_udata_cleared(udata, sizeof(req),
Haggai Abramovskydfbee852016-01-14 19:12:56 +02001023 reqlen - sizeof(req)))
Matan Barakb368d7c2015-12-15 20:30:12 +02001024 return ERR_PTR(-EOPNOTSUPP);
1025
Eli Cohenc1be5232014-01-14 17:45:12 +02001026 req.total_num_uuars = ALIGN(req.total_num_uuars,
1027 MLX5_NON_FP_BF_REGS_PER_PAGE);
Eli Cohene126ba92013-07-07 17:25:49 +03001028 if (req.num_low_latency_uuars > req.total_num_uuars - 1)
1029 return ERR_PTR(-EINVAL);
1030
Eli Cohenc1be5232014-01-14 17:45:12 +02001031 num_uars = req.total_num_uuars / MLX5_NON_FP_BF_REGS_PER_PAGE;
1032 gross_uuars = num_uars * MLX5_BF_REGS_PER_PAGE;
Saeed Mahameed938fe832015-05-28 22:28:41 +03001033 resp.qp_tab_size = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp);
Noa Osherovich2cc6ad52016-06-04 15:15:33 +03001034 if (mlx5_core_is_pf(dev->mdev) && MLX5_CAP_GEN(dev->mdev, bf))
1035 resp.bf_reg_size = 1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size);
Saeed Mahameed938fe832015-05-28 22:28:41 +03001036 resp.cache_line_size = L1_CACHE_BYTES;
1037 resp.max_sq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq);
1038 resp.max_rq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq);
1039 resp.max_send_wqebb = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1040 resp.max_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1041 resp.max_srq_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz);
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001042 resp.cqe_version = min_t(__u8,
1043 (__u8)MLX5_CAP_GEN(dev->mdev, cqe_version),
1044 req.max_cqe_version);
Matan Barakb368d7c2015-12-15 20:30:12 +02001045 resp.response_length = min(offsetof(typeof(resp), response_length) +
1046 sizeof(resp.response_length), udata->outlen);
Eli Cohene126ba92013-07-07 17:25:49 +03001047
1048 context = kzalloc(sizeof(*context), GFP_KERNEL);
1049 if (!context)
1050 return ERR_PTR(-ENOMEM);
1051
1052 uuari = &context->uuari;
1053 mutex_init(&uuari->lock);
1054 uars = kcalloc(num_uars, sizeof(*uars), GFP_KERNEL);
1055 if (!uars) {
1056 err = -ENOMEM;
1057 goto out_ctx;
1058 }
1059
Eli Cohenc1be5232014-01-14 17:45:12 +02001060 uuari->bitmap = kcalloc(BITS_TO_LONGS(gross_uuars),
Eli Cohene126ba92013-07-07 17:25:49 +03001061 sizeof(*uuari->bitmap),
1062 GFP_KERNEL);
1063 if (!uuari->bitmap) {
1064 err = -ENOMEM;
1065 goto out_uar_ctx;
1066 }
1067 /*
1068 * clear all fast path uuars
1069 */
Eli Cohenc1be5232014-01-14 17:45:12 +02001070 for (i = 0; i < gross_uuars; i++) {
Eli Cohene126ba92013-07-07 17:25:49 +03001071 uuarn = i & 3;
1072 if (uuarn == 2 || uuarn == 3)
1073 set_bit(i, uuari->bitmap);
1074 }
1075
Eli Cohenc1be5232014-01-14 17:45:12 +02001076 uuari->count = kcalloc(gross_uuars, sizeof(*uuari->count), GFP_KERNEL);
Eli Cohene126ba92013-07-07 17:25:49 +03001077 if (!uuari->count) {
1078 err = -ENOMEM;
1079 goto out_bitmap;
1080 }
1081
1082 for (i = 0; i < num_uars; i++) {
Jack Morgenstein9603b612014-07-28 23:30:22 +03001083 err = mlx5_cmd_alloc_uar(dev->mdev, &uars[i].index);
Eli Cohene126ba92013-07-07 17:25:49 +03001084 if (err)
1085 goto out_count;
1086 }
1087
Haggai Eranb4cfe442014-12-11 17:04:26 +02001088#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
1089 context->ibucontext.invalidate_range = &mlx5_ib_invalidate_range;
1090#endif
1091
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001092 if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain)) {
1093 err = mlx5_core_alloc_transport_domain(dev->mdev,
1094 &context->tdn);
1095 if (err)
1096 goto out_uars;
1097 }
1098
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001099 INIT_LIST_HEAD(&context->vma_private_list);
Eli Cohene126ba92013-07-07 17:25:49 +03001100 INIT_LIST_HEAD(&context->db_page_list);
1101 mutex_init(&context->db_page_mutex);
1102
1103 resp.tot_uuars = req.total_num_uuars;
Saeed Mahameed938fe832015-05-28 22:28:41 +03001104 resp.num_ports = MLX5_CAP_GEN(dev->mdev, num_ports);
Matan Barakb368d7c2015-12-15 20:30:12 +02001105
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001106 if (field_avail(typeof(resp), cqe_version, udata->outlen))
1107 resp.response_length += sizeof(resp.cqe_version);
Matan Barakb368d7c2015-12-15 20:30:12 +02001108
Bodong Wang402ca532016-06-17 15:02:20 +03001109 if (field_avail(typeof(resp), cmds_supp_uhw, udata->outlen)) {
1110 resp.cmds_supp_uhw |= MLX5_USER_CMDS_SUPP_UHW_QUERY_DEVICE;
1111 resp.response_length += sizeof(resp.cmds_supp_uhw);
1112 }
1113
Noa Osherovichbc5c6ee2016-06-04 15:15:31 +03001114 /*
1115 * We don't want to expose information from the PCI bar that is located
1116 * after 4096 bytes, so if the arch only supports larger pages, let's
1117 * pretend we don't support reading the HCA's core clock. This is also
1118 * forced by mmap function.
1119 */
1120 if (PAGE_SIZE <= 4096 &&
1121 field_avail(typeof(resp), hca_core_clock_offset, udata->outlen)) {
Matan Barakb368d7c2015-12-15 20:30:12 +02001122 resp.comp_mask |=
1123 MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_CORE_CLOCK_OFFSET;
1124 resp.hca_core_clock_offset =
1125 offsetof(struct mlx5_init_seg, internal_timer_h) %
1126 PAGE_SIZE;
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001127 resp.response_length += sizeof(resp.hca_core_clock_offset) +
Bodong Wang402ca532016-06-17 15:02:20 +03001128 sizeof(resp.reserved2);
Matan Barakb368d7c2015-12-15 20:30:12 +02001129 }
1130
1131 err = ib_copy_to_udata(udata, &resp, resp.response_length);
Eli Cohene126ba92013-07-07 17:25:49 +03001132 if (err)
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001133 goto out_td;
Eli Cohene126ba92013-07-07 17:25:49 +03001134
Eli Cohen78c0f982014-01-30 13:49:48 +02001135 uuari->ver = ver;
Eli Cohene126ba92013-07-07 17:25:49 +03001136 uuari->num_low_latency_uuars = req.num_low_latency_uuars;
1137 uuari->uars = uars;
1138 uuari->num_uars = num_uars;
Haggai Abramovskyf72300c2016-01-14 19:12:58 +02001139 context->cqe_version = resp.cqe_version;
1140
Eli Cohene126ba92013-07-07 17:25:49 +03001141 return &context->ibucontext;
1142
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001143out_td:
1144 if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1145 mlx5_core_dealloc_transport_domain(dev->mdev, context->tdn);
1146
Eli Cohene126ba92013-07-07 17:25:49 +03001147out_uars:
1148 for (i--; i >= 0; i--)
Jack Morgenstein9603b612014-07-28 23:30:22 +03001149 mlx5_cmd_free_uar(dev->mdev, uars[i].index);
Eli Cohene126ba92013-07-07 17:25:49 +03001150out_count:
1151 kfree(uuari->count);
1152
1153out_bitmap:
1154 kfree(uuari->bitmap);
1155
1156out_uar_ctx:
1157 kfree(uars);
1158
1159out_ctx:
1160 kfree(context);
1161 return ERR_PTR(err);
1162}
1163
1164static int mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
1165{
1166 struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1167 struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
1168 struct mlx5_uuar_info *uuari = &context->uuari;
1169 int i;
1170
majd@mellanox.com146d2f12016-01-14 19:13:02 +02001171 if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1172 mlx5_core_dealloc_transport_domain(dev->mdev, context->tdn);
1173
Eli Cohene126ba92013-07-07 17:25:49 +03001174 for (i = 0; i < uuari->num_uars; i++) {
Jack Morgenstein9603b612014-07-28 23:30:22 +03001175 if (mlx5_cmd_free_uar(dev->mdev, uuari->uars[i].index))
Eli Cohene126ba92013-07-07 17:25:49 +03001176 mlx5_ib_warn(dev, "failed to free UAR 0x%x\n", uuari->uars[i].index);
1177 }
1178
1179 kfree(uuari->count);
1180 kfree(uuari->bitmap);
1181 kfree(uuari->uars);
1182 kfree(context);
1183
1184 return 0;
1185}
1186
1187static phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev, int index)
1188{
Jack Morgenstein9603b612014-07-28 23:30:22 +03001189 return (pci_resource_start(dev->mdev->pdev, 0) >> PAGE_SHIFT) + index;
Eli Cohene126ba92013-07-07 17:25:49 +03001190}
1191
1192static int get_command(unsigned long offset)
1193{
1194 return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK;
1195}
1196
1197static int get_arg(unsigned long offset)
1198{
1199 return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1);
1200}
1201
1202static int get_index(unsigned long offset)
1203{
1204 return get_arg(offset);
1205}
1206
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001207static void mlx5_ib_vma_open(struct vm_area_struct *area)
1208{
1209 /* vma_open is called when a new VMA is created on top of our VMA. This
1210 * is done through either mremap flow or split_vma (usually due to
1211 * mlock, madvise, munmap, etc.) We do not support a clone of the VMA,
1212 * as this VMA is strongly hardware related. Therefore we set the
1213 * vm_ops of the newly created/cloned VMA to NULL, to prevent it from
1214 * calling us again and trying to do incorrect actions. We assume that
1215 * the original VMA size is exactly a single page, and therefore all
1216 * "splitting" operation will not happen to it.
1217 */
1218 area->vm_ops = NULL;
1219}
1220
1221static void mlx5_ib_vma_close(struct vm_area_struct *area)
1222{
1223 struct mlx5_ib_vma_private_data *mlx5_ib_vma_priv_data;
1224
1225 /* It's guaranteed that all VMAs opened on a FD are closed before the
1226 * file itself is closed, therefore no sync is needed with the regular
1227 * closing flow. (e.g. mlx5 ib_dealloc_ucontext)
1228 * However need a sync with accessing the vma as part of
1229 * mlx5_ib_disassociate_ucontext.
1230 * The close operation is usually called under mm->mmap_sem except when
1231 * process is exiting.
1232 * The exiting case is handled explicitly as part of
1233 * mlx5_ib_disassociate_ucontext.
1234 */
1235 mlx5_ib_vma_priv_data = (struct mlx5_ib_vma_private_data *)area->vm_private_data;
1236
1237 /* setting the vma context pointer to null in the mlx5_ib driver's
1238 * private data, to protect a race condition in
1239 * mlx5_ib_disassociate_ucontext().
1240 */
1241 mlx5_ib_vma_priv_data->vma = NULL;
1242 list_del(&mlx5_ib_vma_priv_data->list);
1243 kfree(mlx5_ib_vma_priv_data);
1244}
1245
1246static const struct vm_operations_struct mlx5_ib_vm_ops = {
1247 .open = mlx5_ib_vma_open,
1248 .close = mlx5_ib_vma_close
1249};
1250
1251static int mlx5_ib_set_vma_data(struct vm_area_struct *vma,
1252 struct mlx5_ib_ucontext *ctx)
1253{
1254 struct mlx5_ib_vma_private_data *vma_prv;
1255 struct list_head *vma_head = &ctx->vma_private_list;
1256
1257 vma_prv = kzalloc(sizeof(*vma_prv), GFP_KERNEL);
1258 if (!vma_prv)
1259 return -ENOMEM;
1260
1261 vma_prv->vma = vma;
1262 vma->vm_private_data = vma_prv;
1263 vma->vm_ops = &mlx5_ib_vm_ops;
1264
1265 list_add(&vma_prv->list, vma_head);
1266
1267 return 0;
1268}
1269
1270static void mlx5_ib_disassociate_ucontext(struct ib_ucontext *ibcontext)
1271{
1272 int ret;
1273 struct vm_area_struct *vma;
1274 struct mlx5_ib_vma_private_data *vma_private, *n;
1275 struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1276 struct task_struct *owning_process = NULL;
1277 struct mm_struct *owning_mm = NULL;
1278
1279 owning_process = get_pid_task(ibcontext->tgid, PIDTYPE_PID);
1280 if (!owning_process)
1281 return;
1282
1283 owning_mm = get_task_mm(owning_process);
1284 if (!owning_mm) {
1285 pr_info("no mm, disassociate ucontext is pending task termination\n");
1286 while (1) {
1287 put_task_struct(owning_process);
1288 usleep_range(1000, 2000);
1289 owning_process = get_pid_task(ibcontext->tgid,
1290 PIDTYPE_PID);
1291 if (!owning_process ||
1292 owning_process->state == TASK_DEAD) {
1293 pr_info("disassociate ucontext done, task was terminated\n");
1294 /* in case task was dead need to release the
1295 * task struct.
1296 */
1297 if (owning_process)
1298 put_task_struct(owning_process);
1299 return;
1300 }
1301 }
1302 }
1303
1304 /* need to protect from a race on closing the vma as part of
1305 * mlx5_ib_vma_close.
1306 */
1307 down_read(&owning_mm->mmap_sem);
1308 list_for_each_entry_safe(vma_private, n, &context->vma_private_list,
1309 list) {
1310 vma = vma_private->vma;
1311 ret = zap_vma_ptes(vma, vma->vm_start,
1312 PAGE_SIZE);
1313 WARN_ONCE(ret, "%s: zap_vma_ptes failed", __func__);
1314 /* context going to be destroyed, should
1315 * not access ops any more.
1316 */
1317 vma->vm_ops = NULL;
1318 list_del(&vma_private->list);
1319 kfree(vma_private);
1320 }
1321 up_read(&owning_mm->mmap_sem);
1322 mmput(owning_mm);
1323 put_task_struct(owning_process);
1324}
1325
Guy Levi37aa5c32016-04-27 16:49:50 +03001326static inline char *mmap_cmd2str(enum mlx5_ib_mmap_cmd cmd)
1327{
1328 switch (cmd) {
1329 case MLX5_IB_MMAP_WC_PAGE:
1330 return "WC";
1331 case MLX5_IB_MMAP_REGULAR_PAGE:
1332 return "best effort WC";
1333 case MLX5_IB_MMAP_NC_PAGE:
1334 return "NC";
1335 default:
1336 return NULL;
1337 }
1338}
1339
1340static int uar_mmap(struct mlx5_ib_dev *dev, enum mlx5_ib_mmap_cmd cmd,
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001341 struct vm_area_struct *vma,
1342 struct mlx5_ib_ucontext *context)
Guy Levi37aa5c32016-04-27 16:49:50 +03001343{
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001344 struct mlx5_uuar_info *uuari = &context->uuari;
Guy Levi37aa5c32016-04-27 16:49:50 +03001345 int err;
1346 unsigned long idx;
1347 phys_addr_t pfn, pa;
1348 pgprot_t prot;
1349
1350 switch (cmd) {
1351 case MLX5_IB_MMAP_WC_PAGE:
1352/* Some architectures don't support WC memory */
1353#if defined(CONFIG_X86)
1354 if (!pat_enabled())
1355 return -EPERM;
1356#elif !(defined(CONFIG_PPC) || (defined(CONFIG_ARM) && defined(CONFIG_MMU)))
1357 return -EPERM;
1358#endif
1359 /* fall through */
1360 case MLX5_IB_MMAP_REGULAR_PAGE:
1361 /* For MLX5_IB_MMAP_REGULAR_PAGE do the best effort to get WC */
1362 prot = pgprot_writecombine(vma->vm_page_prot);
1363 break;
1364 case MLX5_IB_MMAP_NC_PAGE:
1365 prot = pgprot_noncached(vma->vm_page_prot);
1366 break;
1367 default:
1368 return -EINVAL;
1369 }
1370
1371 if (vma->vm_end - vma->vm_start != PAGE_SIZE)
1372 return -EINVAL;
1373
1374 idx = get_index(vma->vm_pgoff);
1375 if (idx >= uuari->num_uars)
1376 return -EINVAL;
1377
1378 pfn = uar_index2pfn(dev, uuari->uars[idx].index);
1379 mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn %pa\n", idx, &pfn);
1380
1381 vma->vm_page_prot = prot;
1382 err = io_remap_pfn_range(vma, vma->vm_start, pfn,
1383 PAGE_SIZE, vma->vm_page_prot);
1384 if (err) {
1385 mlx5_ib_err(dev, "io_remap_pfn_range failed with error=%d, vm_start=0x%lx, pfn=%pa, mmap_cmd=%s\n",
1386 err, vma->vm_start, &pfn, mmap_cmd2str(cmd));
1387 return -EAGAIN;
1388 }
1389
1390 pa = pfn << PAGE_SHIFT;
1391 mlx5_ib_dbg(dev, "mapped %s at 0x%lx, PA %pa\n", mmap_cmd2str(cmd),
1392 vma->vm_start, &pa);
1393
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001394 return mlx5_ib_set_vma_data(vma, context);
Guy Levi37aa5c32016-04-27 16:49:50 +03001395}
1396
Eli Cohene126ba92013-07-07 17:25:49 +03001397static int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma)
1398{
1399 struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1400 struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
Eli Cohene126ba92013-07-07 17:25:49 +03001401 unsigned long command;
Eli Cohene126ba92013-07-07 17:25:49 +03001402 phys_addr_t pfn;
1403
1404 command = get_command(vma->vm_pgoff);
1405 switch (command) {
Guy Levi37aa5c32016-04-27 16:49:50 +03001406 case MLX5_IB_MMAP_WC_PAGE:
1407 case MLX5_IB_MMAP_NC_PAGE:
Eli Cohene126ba92013-07-07 17:25:49 +03001408 case MLX5_IB_MMAP_REGULAR_PAGE:
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03001409 return uar_mmap(dev, command, vma, context);
Eli Cohene126ba92013-07-07 17:25:49 +03001410
1411 case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES:
1412 return -ENOSYS;
1413
Matan Barakd69e3bc2015-12-15 20:30:13 +02001414 case MLX5_IB_MMAP_CORE_CLOCK:
Matan Barakd69e3bc2015-12-15 20:30:13 +02001415 if (vma->vm_end - vma->vm_start != PAGE_SIZE)
1416 return -EINVAL;
1417
Matan Barak6cbac1e2016-04-14 16:52:10 +03001418 if (vma->vm_flags & VM_WRITE)
Matan Barakd69e3bc2015-12-15 20:30:13 +02001419 return -EPERM;
1420
1421 /* Don't expose to user-space information it shouldn't have */
1422 if (PAGE_SIZE > 4096)
1423 return -EOPNOTSUPP;
1424
1425 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1426 pfn = (dev->mdev->iseg_base +
1427 offsetof(struct mlx5_init_seg, internal_timer_h)) >>
1428 PAGE_SHIFT;
1429 if (io_remap_pfn_range(vma, vma->vm_start, pfn,
1430 PAGE_SIZE, vma->vm_page_prot))
1431 return -EAGAIN;
1432
1433 mlx5_ib_dbg(dev, "mapped internal timer at 0x%lx, PA 0x%llx\n",
1434 vma->vm_start,
1435 (unsigned long long)pfn << PAGE_SHIFT);
1436 break;
Matan Barakd69e3bc2015-12-15 20:30:13 +02001437
Eli Cohene126ba92013-07-07 17:25:49 +03001438 default:
1439 return -EINVAL;
1440 }
1441
1442 return 0;
1443}
1444
Eli Cohene126ba92013-07-07 17:25:49 +03001445static struct ib_pd *mlx5_ib_alloc_pd(struct ib_device *ibdev,
1446 struct ib_ucontext *context,
1447 struct ib_udata *udata)
1448{
1449 struct mlx5_ib_alloc_pd_resp resp;
1450 struct mlx5_ib_pd *pd;
1451 int err;
1452
1453 pd = kmalloc(sizeof(*pd), GFP_KERNEL);
1454 if (!pd)
1455 return ERR_PTR(-ENOMEM);
1456
Jack Morgenstein9603b612014-07-28 23:30:22 +03001457 err = mlx5_core_alloc_pd(to_mdev(ibdev)->mdev, &pd->pdn);
Eli Cohene126ba92013-07-07 17:25:49 +03001458 if (err) {
1459 kfree(pd);
1460 return ERR_PTR(err);
1461 }
1462
1463 if (context) {
1464 resp.pdn = pd->pdn;
1465 if (ib_copy_to_udata(udata, &resp, sizeof(resp))) {
Jack Morgenstein9603b612014-07-28 23:30:22 +03001466 mlx5_core_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn);
Eli Cohene126ba92013-07-07 17:25:49 +03001467 kfree(pd);
1468 return ERR_PTR(-EFAULT);
1469 }
Eli Cohene126ba92013-07-07 17:25:49 +03001470 }
1471
1472 return &pd->ibpd;
1473}
1474
1475static int mlx5_ib_dealloc_pd(struct ib_pd *pd)
1476{
1477 struct mlx5_ib_dev *mdev = to_mdev(pd->device);
1478 struct mlx5_ib_pd *mpd = to_mpd(pd);
1479
Jack Morgenstein9603b612014-07-28 23:30:22 +03001480 mlx5_core_dealloc_pd(mdev->mdev, mpd->pdn);
Eli Cohene126ba92013-07-07 17:25:49 +03001481 kfree(mpd);
1482
1483 return 0;
1484}
1485
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03001486enum {
1487 MATCH_CRITERIA_ENABLE_OUTER_BIT,
1488 MATCH_CRITERIA_ENABLE_MISC_BIT,
1489 MATCH_CRITERIA_ENABLE_INNER_BIT
1490};
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001491
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03001492#define HEADER_IS_ZERO(match_criteria, headers) \
1493 !(memchr_inv(MLX5_ADDR_OF(fte_match_param, match_criteria, headers), \
1494 0, MLX5_FLD_SZ_BYTES(fte_match_param, headers))) \
1495
1496static u8 get_match_criteria_enable(u32 *match_criteria)
1497{
1498 u8 match_criteria_enable;
1499
1500 match_criteria_enable =
1501 (!HEADER_IS_ZERO(match_criteria, outer_headers)) <<
1502 MATCH_CRITERIA_ENABLE_OUTER_BIT;
1503 match_criteria_enable |=
1504 (!HEADER_IS_ZERO(match_criteria, misc_parameters)) <<
1505 MATCH_CRITERIA_ENABLE_MISC_BIT;
1506 match_criteria_enable |=
1507 (!HEADER_IS_ZERO(match_criteria, inner_headers)) <<
1508 MATCH_CRITERIA_ENABLE_INNER_BIT;
1509
1510 return match_criteria_enable;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001511}
1512
Maor Gottliebca0d4752016-08-30 16:58:35 +03001513static void set_proto(void *outer_c, void *outer_v, u8 mask, u8 val)
1514{
1515 MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_protocol, mask);
1516 MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_protocol, val);
1517}
1518
1519static void set_tos(void *outer_c, void *outer_v, u8 mask, u8 val)
1520{
1521 MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_ecn, mask);
1522 MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_ecn, val);
1523 MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_dscp, mask >> 2);
1524 MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_dscp, val >> 2);
1525}
1526
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03001527#define LAST_ETH_FIELD vlan_tag
1528#define LAST_IB_FIELD sl
Maor Gottliebca0d4752016-08-30 16:58:35 +03001529#define LAST_IPV4_FIELD tos
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03001530#define LAST_IPV6_FIELD traffic_class
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03001531#define LAST_TCP_UDP_FIELD src_port
1532
1533/* Field is the last supported field */
1534#define FIELDS_NOT_SUPPORTED(filter, field)\
1535 memchr_inv((void *)&filter.field +\
1536 sizeof(filter.field), 0,\
1537 sizeof(filter) -\
1538 offsetof(typeof(filter), field) -\
1539 sizeof(filter.field))
1540
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001541static int parse_flow_attr(u32 *match_c, u32 *match_v,
Maor Gottliebdd063d02016-08-28 14:16:32 +03001542 const union ib_flow_spec *ib_spec)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001543{
1544 void *outer_headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
1545 outer_headers);
1546 void *outer_headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
1547 outer_headers);
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03001548 void *misc_params_c = MLX5_ADDR_OF(fte_match_param, match_c,
1549 misc_parameters);
1550 void *misc_params_v = MLX5_ADDR_OF(fte_match_param, match_v,
1551 misc_parameters);
1552
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001553 switch (ib_spec->type) {
1554 case IB_FLOW_SPEC_ETH:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03001555 if (FIELDS_NOT_SUPPORTED(ib_spec->eth.mask, LAST_ETH_FIELD))
1556 return -ENOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001557
1558 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_c,
1559 dmac_47_16),
1560 ib_spec->eth.mask.dst_mac);
1561 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_v,
1562 dmac_47_16),
1563 ib_spec->eth.val.dst_mac);
1564
Maor Gottliebee3da802016-09-12 19:16:24 +03001565 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_c,
1566 smac_47_16),
1567 ib_spec->eth.mask.src_mac);
1568 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_v,
1569 smac_47_16),
1570 ib_spec->eth.val.src_mac);
1571
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001572 if (ib_spec->eth.mask.vlan_tag) {
1573 MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c,
1574 vlan_tag, 1);
1575 MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v,
1576 vlan_tag, 1);
1577
1578 MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c,
1579 first_vid, ntohs(ib_spec->eth.mask.vlan_tag));
1580 MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v,
1581 first_vid, ntohs(ib_spec->eth.val.vlan_tag));
1582
1583 MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c,
1584 first_cfi,
1585 ntohs(ib_spec->eth.mask.vlan_tag) >> 12);
1586 MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v,
1587 first_cfi,
1588 ntohs(ib_spec->eth.val.vlan_tag) >> 12);
1589
1590 MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c,
1591 first_prio,
1592 ntohs(ib_spec->eth.mask.vlan_tag) >> 13);
1593 MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v,
1594 first_prio,
1595 ntohs(ib_spec->eth.val.vlan_tag) >> 13);
1596 }
1597 MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c,
1598 ethertype, ntohs(ib_spec->eth.mask.ether_type));
1599 MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v,
1600 ethertype, ntohs(ib_spec->eth.val.ether_type));
1601 break;
1602 case IB_FLOW_SPEC_IPV4:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03001603 if (FIELDS_NOT_SUPPORTED(ib_spec->ipv4.mask, LAST_IPV4_FIELD))
1604 return -ENOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001605
1606 MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c,
1607 ethertype, 0xffff);
1608 MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v,
1609 ethertype, ETH_P_IP);
1610
1611 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_c,
1612 src_ipv4_src_ipv6.ipv4_layout.ipv4),
1613 &ib_spec->ipv4.mask.src_ip,
1614 sizeof(ib_spec->ipv4.mask.src_ip));
1615 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_v,
1616 src_ipv4_src_ipv6.ipv4_layout.ipv4),
1617 &ib_spec->ipv4.val.src_ip,
1618 sizeof(ib_spec->ipv4.val.src_ip));
1619 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_c,
1620 dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
1621 &ib_spec->ipv4.mask.dst_ip,
1622 sizeof(ib_spec->ipv4.mask.dst_ip));
1623 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_v,
1624 dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
1625 &ib_spec->ipv4.val.dst_ip,
1626 sizeof(ib_spec->ipv4.val.dst_ip));
Maor Gottliebca0d4752016-08-30 16:58:35 +03001627
1628 set_tos(outer_headers_c, outer_headers_v,
1629 ib_spec->ipv4.mask.tos, ib_spec->ipv4.val.tos);
1630
1631 set_proto(outer_headers_c, outer_headers_v,
1632 ib_spec->ipv4.mask.proto, ib_spec->ipv4.val.proto);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001633 break;
Maor Gottlieb026bae02016-06-17 15:14:51 +03001634 case IB_FLOW_SPEC_IPV6:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03001635 if (FIELDS_NOT_SUPPORTED(ib_spec->ipv6.mask, LAST_IPV6_FIELD))
1636 return -ENOTSUPP;
Maor Gottlieb026bae02016-06-17 15:14:51 +03001637
1638 MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c,
1639 ethertype, 0xffff);
1640 MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v,
1641 ethertype, ETH_P_IPV6);
1642
1643 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_c,
1644 src_ipv4_src_ipv6.ipv6_layout.ipv6),
1645 &ib_spec->ipv6.mask.src_ip,
1646 sizeof(ib_spec->ipv6.mask.src_ip));
1647 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_v,
1648 src_ipv4_src_ipv6.ipv6_layout.ipv6),
1649 &ib_spec->ipv6.val.src_ip,
1650 sizeof(ib_spec->ipv6.val.src_ip));
1651 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_c,
1652 dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
1653 &ib_spec->ipv6.mask.dst_ip,
1654 sizeof(ib_spec->ipv6.mask.dst_ip));
1655 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_v,
1656 dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
1657 &ib_spec->ipv6.val.dst_ip,
1658 sizeof(ib_spec->ipv6.val.dst_ip));
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03001659
1660 set_tos(outer_headers_c, outer_headers_v,
1661 ib_spec->ipv6.mask.traffic_class,
1662 ib_spec->ipv6.val.traffic_class);
1663
1664 set_proto(outer_headers_c, outer_headers_v,
1665 ib_spec->ipv6.mask.next_hdr,
1666 ib_spec->ipv6.val.next_hdr);
1667
1668 MLX5_SET(fte_match_set_misc, misc_params_c,
1669 outer_ipv6_flow_label,
1670 ntohl(ib_spec->ipv6.mask.flow_label));
1671 MLX5_SET(fte_match_set_misc, misc_params_v,
1672 outer_ipv6_flow_label,
1673 ntohl(ib_spec->ipv6.val.flow_label));
Maor Gottlieb026bae02016-06-17 15:14:51 +03001674 break;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001675 case IB_FLOW_SPEC_TCP:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03001676 if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
1677 LAST_TCP_UDP_FIELD))
1678 return -ENOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001679
1680 MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, ip_protocol,
1681 0xff);
1682 MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, ip_protocol,
1683 IPPROTO_TCP);
1684
1685 MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, tcp_sport,
1686 ntohs(ib_spec->tcp_udp.mask.src_port));
1687 MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, tcp_sport,
1688 ntohs(ib_spec->tcp_udp.val.src_port));
1689
1690 MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, tcp_dport,
1691 ntohs(ib_spec->tcp_udp.mask.dst_port));
1692 MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, tcp_dport,
1693 ntohs(ib_spec->tcp_udp.val.dst_port));
1694 break;
1695 case IB_FLOW_SPEC_UDP:
Maor Gottliebc47ac6a2016-08-30 16:58:31 +03001696 if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
1697 LAST_TCP_UDP_FIELD))
1698 return -ENOTSUPP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001699
1700 MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, ip_protocol,
1701 0xff);
1702 MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, ip_protocol,
1703 IPPROTO_UDP);
1704
1705 MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, udp_sport,
1706 ntohs(ib_spec->tcp_udp.mask.src_port));
1707 MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, udp_sport,
1708 ntohs(ib_spec->tcp_udp.val.src_port));
1709
1710 MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, udp_dport,
1711 ntohs(ib_spec->tcp_udp.mask.dst_port));
1712 MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, udp_dport,
1713 ntohs(ib_spec->tcp_udp.val.dst_port));
1714 break;
1715 default:
1716 return -EINVAL;
1717 }
1718
1719 return 0;
1720}
1721
1722/* If a flow could catch both multicast and unicast packets,
1723 * it won't fall into the multicast flow steering table and this rule
1724 * could steal other multicast packets.
1725 */
1726static bool flow_is_multicast_only(struct ib_flow_attr *ib_attr)
1727{
1728 struct ib_flow_spec_eth *eth_spec;
1729
1730 if (ib_attr->type != IB_FLOW_ATTR_NORMAL ||
1731 ib_attr->size < sizeof(struct ib_flow_attr) +
1732 sizeof(struct ib_flow_spec_eth) ||
1733 ib_attr->num_of_specs < 1)
1734 return false;
1735
1736 eth_spec = (struct ib_flow_spec_eth *)(ib_attr + 1);
1737 if (eth_spec->type != IB_FLOW_SPEC_ETH ||
1738 eth_spec->size != sizeof(*eth_spec))
1739 return false;
1740
1741 return is_multicast_ether_addr(eth_spec->mask.dst_mac) &&
1742 is_multicast_ether_addr(eth_spec->val.dst_mac);
1743}
1744
Maor Gottliebdd063d02016-08-28 14:16:32 +03001745static bool is_valid_attr(const struct ib_flow_attr *flow_attr)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001746{
1747 union ib_flow_spec *ib_spec = (union ib_flow_spec *)(flow_attr + 1);
1748 bool has_ipv4_spec = false;
1749 bool eth_type_ipv4 = true;
1750 unsigned int spec_index;
1751
1752 /* Validate that ethertype is correct */
1753 for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
1754 if (ib_spec->type == IB_FLOW_SPEC_ETH &&
1755 ib_spec->eth.mask.ether_type) {
1756 if (!((ib_spec->eth.mask.ether_type == htons(0xffff)) &&
1757 ib_spec->eth.val.ether_type == htons(ETH_P_IP)))
1758 eth_type_ipv4 = false;
1759 } else if (ib_spec->type == IB_FLOW_SPEC_IPV4) {
1760 has_ipv4_spec = true;
1761 }
1762 ib_spec = (void *)ib_spec + ib_spec->size;
1763 }
1764 return !has_ipv4_spec || eth_type_ipv4;
1765}
1766
1767static void put_flow_table(struct mlx5_ib_dev *dev,
1768 struct mlx5_ib_flow_prio *prio, bool ft_added)
1769{
1770 prio->refcount -= !!ft_added;
1771 if (!prio->refcount) {
1772 mlx5_destroy_flow_table(prio->flow_table);
1773 prio->flow_table = NULL;
1774 }
1775}
1776
1777static int mlx5_ib_destroy_flow(struct ib_flow *flow_id)
1778{
1779 struct mlx5_ib_dev *dev = to_mdev(flow_id->qp->device);
1780 struct mlx5_ib_flow_handler *handler = container_of(flow_id,
1781 struct mlx5_ib_flow_handler,
1782 ibflow);
1783 struct mlx5_ib_flow_handler *iter, *tmp;
1784
1785 mutex_lock(&dev->flow_db.lock);
1786
1787 list_for_each_entry_safe(iter, tmp, &handler->list, list) {
Mark Bloch74491de2016-08-31 11:24:25 +00001788 mlx5_del_flow_rules(iter->rule);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03001789 put_flow_table(dev, iter->prio, true);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001790 list_del(&iter->list);
1791 kfree(iter);
1792 }
1793
Mark Bloch74491de2016-08-31 11:24:25 +00001794 mlx5_del_flow_rules(handler->rule);
Maor Gottlieb5497adc2016-08-28 14:16:31 +03001795 put_flow_table(dev, handler->prio, true);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001796 mutex_unlock(&dev->flow_db.lock);
1797
1798 kfree(handler);
1799
1800 return 0;
1801}
1802
Maor Gottlieb35d190112016-03-07 18:51:47 +02001803static int ib_prio_to_core_prio(unsigned int priority, bool dont_trap)
1804{
1805 priority *= 2;
1806 if (!dont_trap)
1807 priority++;
1808 return priority;
1809}
1810
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03001811enum flow_table_type {
1812 MLX5_IB_FT_RX,
1813 MLX5_IB_FT_TX
1814};
1815
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001816#define MLX5_FS_MAX_TYPES 10
1817#define MLX5_FS_MAX_ENTRIES 32000UL
1818static struct mlx5_ib_flow_prio *get_flow_table(struct mlx5_ib_dev *dev,
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03001819 struct ib_flow_attr *flow_attr,
1820 enum flow_table_type ft_type)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001821{
Maor Gottlieb35d190112016-03-07 18:51:47 +02001822 bool dont_trap = flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001823 struct mlx5_flow_namespace *ns = NULL;
1824 struct mlx5_ib_flow_prio *prio;
1825 struct mlx5_flow_table *ft;
1826 int num_entries;
1827 int num_groups;
1828 int priority;
1829 int err = 0;
1830
1831 if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
Maor Gottlieb35d190112016-03-07 18:51:47 +02001832 if (flow_is_multicast_only(flow_attr) &&
1833 !dont_trap)
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001834 priority = MLX5_IB_FLOW_MCAST_PRIO;
1835 else
Maor Gottlieb35d190112016-03-07 18:51:47 +02001836 priority = ib_prio_to_core_prio(flow_attr->priority,
1837 dont_trap);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001838 ns = mlx5_get_flow_namespace(dev->mdev,
1839 MLX5_FLOW_NAMESPACE_BYPASS);
1840 num_entries = MLX5_FS_MAX_ENTRIES;
1841 num_groups = MLX5_FS_MAX_TYPES;
1842 prio = &dev->flow_db.prios[priority];
1843 } else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
1844 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
1845 ns = mlx5_get_flow_namespace(dev->mdev,
1846 MLX5_FLOW_NAMESPACE_LEFTOVERS);
1847 build_leftovers_ft_param(&priority,
1848 &num_entries,
1849 &num_groups);
1850 prio = &dev->flow_db.prios[MLX5_IB_FLOW_LEFTOVERS_PRIO];
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03001851 } else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
1852 if (!MLX5_CAP_FLOWTABLE(dev->mdev,
1853 allow_sniffer_and_nic_rx_shared_tir))
1854 return ERR_PTR(-ENOTSUPP);
1855
1856 ns = mlx5_get_flow_namespace(dev->mdev, ft_type == MLX5_IB_FT_RX ?
1857 MLX5_FLOW_NAMESPACE_SNIFFER_RX :
1858 MLX5_FLOW_NAMESPACE_SNIFFER_TX);
1859
1860 prio = &dev->flow_db.sniffer[ft_type];
1861 priority = 0;
1862 num_entries = 1;
1863 num_groups = 1;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001864 }
1865
1866 if (!ns)
1867 return ERR_PTR(-ENOTSUPP);
1868
1869 ft = prio->flow_table;
1870 if (!ft) {
1871 ft = mlx5_create_auto_grouped_flow_table(ns, priority,
1872 num_entries,
Maor Gottliebd63cd282016-04-29 01:36:35 +03001873 num_groups,
1874 0);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001875
1876 if (!IS_ERR(ft)) {
1877 prio->refcount = 0;
1878 prio->flow_table = ft;
1879 } else {
1880 err = PTR_ERR(ft);
1881 }
1882 }
1883
1884 return err ? ERR_PTR(err) : prio;
1885}
1886
1887static struct mlx5_ib_flow_handler *create_flow_rule(struct mlx5_ib_dev *dev,
1888 struct mlx5_ib_flow_prio *ft_prio,
Maor Gottliebdd063d02016-08-28 14:16:32 +03001889 const struct ib_flow_attr *flow_attr,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001890 struct mlx5_flow_destination *dst)
1891{
1892 struct mlx5_flow_table *ft = ft_prio->flow_table;
1893 struct mlx5_ib_flow_handler *handler;
Maor Gottliebc5bb1732016-07-04 17:23:05 +03001894 struct mlx5_flow_spec *spec;
Maor Gottliebdd063d02016-08-28 14:16:32 +03001895 const void *ib_flow = (const void *)flow_attr + sizeof(*flow_attr);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001896 unsigned int spec_index;
Maor Gottlieb35d190112016-03-07 18:51:47 +02001897 u32 action;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001898 int err = 0;
1899
1900 if (!is_valid_attr(flow_attr))
1901 return ERR_PTR(-EINVAL);
1902
Maor Gottliebc5bb1732016-07-04 17:23:05 +03001903 spec = mlx5_vzalloc(sizeof(*spec));
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001904 handler = kzalloc(sizeof(*handler), GFP_KERNEL);
Maor Gottliebc5bb1732016-07-04 17:23:05 +03001905 if (!handler || !spec) {
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001906 err = -ENOMEM;
1907 goto free;
1908 }
1909
1910 INIT_LIST_HEAD(&handler->list);
1911
1912 for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
Maor Gottliebc5bb1732016-07-04 17:23:05 +03001913 err = parse_flow_attr(spec->match_criteria,
1914 spec->match_value, ib_flow);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001915 if (err < 0)
1916 goto free;
1917
1918 ib_flow += ((union ib_flow_spec *)ib_flow)->size;
1919 }
1920
Maor Gottlieb466fa6d2016-08-30 16:58:36 +03001921 spec->match_criteria_enable = get_match_criteria_enable(spec->match_criteria);
Maor Gottlieb35d190112016-03-07 18:51:47 +02001922 action = dst ? MLX5_FLOW_CONTEXT_ACTION_FWD_DEST :
1923 MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO;
Mark Bloch74491de2016-08-31 11:24:25 +00001924 handler->rule = mlx5_add_flow_rules(ft, spec,
Maor Gottlieb35d190112016-03-07 18:51:47 +02001925 action,
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001926 MLX5_FS_DEFAULT_FLOW_TAG,
Mark Bloch74491de2016-08-31 11:24:25 +00001927 dst, 1);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001928
1929 if (IS_ERR(handler->rule)) {
1930 err = PTR_ERR(handler->rule);
1931 goto free;
1932 }
1933
Maor Gottliebd9d49802016-08-28 14:16:33 +03001934 ft_prio->refcount++;
Maor Gottlieb5497adc2016-08-28 14:16:31 +03001935 handler->prio = ft_prio;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001936
1937 ft_prio->flow_table = ft;
1938free:
1939 if (err)
1940 kfree(handler);
Maor Gottliebc5bb1732016-07-04 17:23:05 +03001941 kvfree(spec);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001942 return err ? ERR_PTR(err) : handler;
1943}
1944
Maor Gottlieb35d190112016-03-07 18:51:47 +02001945static struct mlx5_ib_flow_handler *create_dont_trap_rule(struct mlx5_ib_dev *dev,
1946 struct mlx5_ib_flow_prio *ft_prio,
1947 struct ib_flow_attr *flow_attr,
1948 struct mlx5_flow_destination *dst)
1949{
1950 struct mlx5_ib_flow_handler *handler_dst = NULL;
1951 struct mlx5_ib_flow_handler *handler = NULL;
1952
1953 handler = create_flow_rule(dev, ft_prio, flow_attr, NULL);
1954 if (!IS_ERR(handler)) {
1955 handler_dst = create_flow_rule(dev, ft_prio,
1956 flow_attr, dst);
1957 if (IS_ERR(handler_dst)) {
Mark Bloch74491de2016-08-31 11:24:25 +00001958 mlx5_del_flow_rules(handler->rule);
Maor Gottliebd9d49802016-08-28 14:16:33 +03001959 ft_prio->refcount--;
Maor Gottlieb35d190112016-03-07 18:51:47 +02001960 kfree(handler);
1961 handler = handler_dst;
1962 } else {
1963 list_add(&handler_dst->list, &handler->list);
1964 }
1965 }
1966
1967 return handler;
1968}
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02001969enum {
1970 LEFTOVERS_MC,
1971 LEFTOVERS_UC,
1972};
1973
1974static struct mlx5_ib_flow_handler *create_leftovers_rule(struct mlx5_ib_dev *dev,
1975 struct mlx5_ib_flow_prio *ft_prio,
1976 struct ib_flow_attr *flow_attr,
1977 struct mlx5_flow_destination *dst)
1978{
1979 struct mlx5_ib_flow_handler *handler_ucast = NULL;
1980 struct mlx5_ib_flow_handler *handler = NULL;
1981
1982 static struct {
1983 struct ib_flow_attr flow_attr;
1984 struct ib_flow_spec_eth eth_flow;
1985 } leftovers_specs[] = {
1986 [LEFTOVERS_MC] = {
1987 .flow_attr = {
1988 .num_of_specs = 1,
1989 .size = sizeof(leftovers_specs[0])
1990 },
1991 .eth_flow = {
1992 .type = IB_FLOW_SPEC_ETH,
1993 .size = sizeof(struct ib_flow_spec_eth),
1994 .mask = {.dst_mac = {0x1} },
1995 .val = {.dst_mac = {0x1} }
1996 }
1997 },
1998 [LEFTOVERS_UC] = {
1999 .flow_attr = {
2000 .num_of_specs = 1,
2001 .size = sizeof(leftovers_specs[0])
2002 },
2003 .eth_flow = {
2004 .type = IB_FLOW_SPEC_ETH,
2005 .size = sizeof(struct ib_flow_spec_eth),
2006 .mask = {.dst_mac = {0x1} },
2007 .val = {.dst_mac = {} }
2008 }
2009 }
2010 };
2011
2012 handler = create_flow_rule(dev, ft_prio,
2013 &leftovers_specs[LEFTOVERS_MC].flow_attr,
2014 dst);
2015 if (!IS_ERR(handler) &&
2016 flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT) {
2017 handler_ucast = create_flow_rule(dev, ft_prio,
2018 &leftovers_specs[LEFTOVERS_UC].flow_attr,
2019 dst);
2020 if (IS_ERR(handler_ucast)) {
Mark Bloch74491de2016-08-31 11:24:25 +00002021 mlx5_del_flow_rules(handler->rule);
Maor Gottliebd9d49802016-08-28 14:16:33 +03002022 ft_prio->refcount--;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002023 kfree(handler);
2024 handler = handler_ucast;
2025 } else {
2026 list_add(&handler_ucast->list, &handler->list);
2027 }
2028 }
2029
2030 return handler;
2031}
2032
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002033static struct mlx5_ib_flow_handler *create_sniffer_rule(struct mlx5_ib_dev *dev,
2034 struct mlx5_ib_flow_prio *ft_rx,
2035 struct mlx5_ib_flow_prio *ft_tx,
2036 struct mlx5_flow_destination *dst)
2037{
2038 struct mlx5_ib_flow_handler *handler_rx;
2039 struct mlx5_ib_flow_handler *handler_tx;
2040 int err;
2041 static const struct ib_flow_attr flow_attr = {
2042 .num_of_specs = 0,
2043 .size = sizeof(flow_attr)
2044 };
2045
2046 handler_rx = create_flow_rule(dev, ft_rx, &flow_attr, dst);
2047 if (IS_ERR(handler_rx)) {
2048 err = PTR_ERR(handler_rx);
2049 goto err;
2050 }
2051
2052 handler_tx = create_flow_rule(dev, ft_tx, &flow_attr, dst);
2053 if (IS_ERR(handler_tx)) {
2054 err = PTR_ERR(handler_tx);
2055 goto err_tx;
2056 }
2057
2058 list_add(&handler_tx->list, &handler_rx->list);
2059
2060 return handler_rx;
2061
2062err_tx:
Mark Bloch74491de2016-08-31 11:24:25 +00002063 mlx5_del_flow_rules(handler_rx->rule);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002064 ft_rx->refcount--;
2065 kfree(handler_rx);
2066err:
2067 return ERR_PTR(err);
2068}
2069
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002070static struct ib_flow *mlx5_ib_create_flow(struct ib_qp *qp,
2071 struct ib_flow_attr *flow_attr,
2072 int domain)
2073{
2074 struct mlx5_ib_dev *dev = to_mdev(qp->device);
Yishai Hadasd9f88e52016-08-28 10:58:37 +03002075 struct mlx5_ib_qp *mqp = to_mqp(qp);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002076 struct mlx5_ib_flow_handler *handler = NULL;
2077 struct mlx5_flow_destination *dst = NULL;
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002078 struct mlx5_ib_flow_prio *ft_prio_tx = NULL;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002079 struct mlx5_ib_flow_prio *ft_prio;
2080 int err;
2081
2082 if (flow_attr->priority > MLX5_IB_FLOW_LAST_PRIO)
2083 return ERR_PTR(-ENOSPC);
2084
2085 if (domain != IB_FLOW_DOMAIN_USER ||
2086 flow_attr->port > MLX5_CAP_GEN(dev->mdev, num_ports) ||
Maor Gottlieb35d190112016-03-07 18:51:47 +02002087 (flow_attr->flags & ~IB_FLOW_ATTR_FLAGS_DONT_TRAP))
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002088 return ERR_PTR(-EINVAL);
2089
2090 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
2091 if (!dst)
2092 return ERR_PTR(-ENOMEM);
2093
2094 mutex_lock(&dev->flow_db.lock);
2095
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002096 ft_prio = get_flow_table(dev, flow_attr, MLX5_IB_FT_RX);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002097 if (IS_ERR(ft_prio)) {
2098 err = PTR_ERR(ft_prio);
2099 goto unlock;
2100 }
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002101 if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
2102 ft_prio_tx = get_flow_table(dev, flow_attr, MLX5_IB_FT_TX);
2103 if (IS_ERR(ft_prio_tx)) {
2104 err = PTR_ERR(ft_prio_tx);
2105 ft_prio_tx = NULL;
2106 goto destroy_ft;
2107 }
2108 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002109
2110 dst->type = MLX5_FLOW_DESTINATION_TYPE_TIR;
Yishai Hadasd9f88e52016-08-28 10:58:37 +03002111 if (mqp->flags & MLX5_IB_QP_RSS)
2112 dst->tir_num = mqp->rss_qp.tirn;
2113 else
2114 dst->tir_num = mqp->raw_packet_qp.rq.tirn;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002115
2116 if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
Maor Gottlieb35d190112016-03-07 18:51:47 +02002117 if (flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP) {
2118 handler = create_dont_trap_rule(dev, ft_prio,
2119 flow_attr, dst);
2120 } else {
2121 handler = create_flow_rule(dev, ft_prio, flow_attr,
2122 dst);
2123 }
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002124 } else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
2125 flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
2126 handler = create_leftovers_rule(dev, ft_prio, flow_attr,
2127 dst);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002128 } else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
2129 handler = create_sniffer_rule(dev, ft_prio, ft_prio_tx, dst);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002130 } else {
2131 err = -EINVAL;
2132 goto destroy_ft;
2133 }
2134
2135 if (IS_ERR(handler)) {
2136 err = PTR_ERR(handler);
2137 handler = NULL;
2138 goto destroy_ft;
2139 }
2140
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002141 mutex_unlock(&dev->flow_db.lock);
2142 kfree(dst);
2143
2144 return &handler->ibflow;
2145
2146destroy_ft:
2147 put_flow_table(dev, ft_prio, false);
Maor Gottliebcc0e5d42016-08-28 14:16:34 +03002148 if (ft_prio_tx)
2149 put_flow_table(dev, ft_prio_tx, false);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02002150unlock:
2151 mutex_unlock(&dev->flow_db.lock);
2152 kfree(dst);
2153 kfree(handler);
2154 return ERR_PTR(err);
2155}
2156
Eli Cohene126ba92013-07-07 17:25:49 +03002157static int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
2158{
2159 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
2160 int err;
2161
Jack Morgenstein9603b612014-07-28 23:30:22 +03002162 err = mlx5_core_attach_mcg(dev->mdev, gid, ibqp->qp_num);
Eli Cohene126ba92013-07-07 17:25:49 +03002163 if (err)
2164 mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n",
2165 ibqp->qp_num, gid->raw);
2166
2167 return err;
2168}
2169
2170static int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
2171{
2172 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
2173 int err;
2174
Jack Morgenstein9603b612014-07-28 23:30:22 +03002175 err = mlx5_core_detach_mcg(dev->mdev, gid, ibqp->qp_num);
Eli Cohene126ba92013-07-07 17:25:49 +03002176 if (err)
2177 mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n",
2178 ibqp->qp_num, gid->raw);
2179
2180 return err;
2181}
2182
2183static int init_node_data(struct mlx5_ib_dev *dev)
2184{
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03002185 int err;
Eli Cohene126ba92013-07-07 17:25:49 +03002186
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03002187 err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc);
Eli Cohene126ba92013-07-07 17:25:49 +03002188 if (err)
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03002189 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03002190
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03002191 dev->mdev->rev_id = dev->mdev->pdev->revision;
Eli Cohene126ba92013-07-07 17:25:49 +03002192
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03002193 return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid);
Eli Cohene126ba92013-07-07 17:25:49 +03002194}
2195
2196static ssize_t show_fw_pages(struct device *device, struct device_attribute *attr,
2197 char *buf)
2198{
2199 struct mlx5_ib_dev *dev =
2200 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
2201
Jack Morgenstein9603b612014-07-28 23:30:22 +03002202 return sprintf(buf, "%d\n", dev->mdev->priv.fw_pages);
Eli Cohene126ba92013-07-07 17:25:49 +03002203}
2204
2205static ssize_t show_reg_pages(struct device *device,
2206 struct device_attribute *attr, char *buf)
2207{
2208 struct mlx5_ib_dev *dev =
2209 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
2210
Haggai Eran6aec21f2014-12-11 17:04:23 +02002211 return sprintf(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages));
Eli Cohene126ba92013-07-07 17:25:49 +03002212}
2213
2214static ssize_t show_hca(struct device *device, struct device_attribute *attr,
2215 char *buf)
2216{
2217 struct mlx5_ib_dev *dev =
2218 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
Jack Morgenstein9603b612014-07-28 23:30:22 +03002219 return sprintf(buf, "MT%d\n", dev->mdev->pdev->device);
Eli Cohene126ba92013-07-07 17:25:49 +03002220}
2221
Eli Cohene126ba92013-07-07 17:25:49 +03002222static ssize_t show_rev(struct device *device, struct device_attribute *attr,
2223 char *buf)
2224{
2225 struct mlx5_ib_dev *dev =
2226 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
Jack Morgenstein9603b612014-07-28 23:30:22 +03002227 return sprintf(buf, "%x\n", dev->mdev->rev_id);
Eli Cohene126ba92013-07-07 17:25:49 +03002228}
2229
2230static ssize_t show_board(struct device *device, struct device_attribute *attr,
2231 char *buf)
2232{
2233 struct mlx5_ib_dev *dev =
2234 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
2235 return sprintf(buf, "%.*s\n", MLX5_BOARD_ID_LEN,
Jack Morgenstein9603b612014-07-28 23:30:22 +03002236 dev->mdev->board_id);
Eli Cohene126ba92013-07-07 17:25:49 +03002237}
2238
2239static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03002240static DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL);
2241static DEVICE_ATTR(board_id, S_IRUGO, show_board, NULL);
2242static DEVICE_ATTR(fw_pages, S_IRUGO, show_fw_pages, NULL);
2243static DEVICE_ATTR(reg_pages, S_IRUGO, show_reg_pages, NULL);
2244
2245static struct device_attribute *mlx5_class_attributes[] = {
2246 &dev_attr_hw_rev,
Eli Cohene126ba92013-07-07 17:25:49 +03002247 &dev_attr_hca_type,
2248 &dev_attr_board_id,
2249 &dev_attr_fw_pages,
2250 &dev_attr_reg_pages,
2251};
2252
Haggai Eran7722f472016-02-29 15:45:07 +02002253static void pkey_change_handler(struct work_struct *work)
2254{
2255 struct mlx5_ib_port_resources *ports =
2256 container_of(work, struct mlx5_ib_port_resources,
2257 pkey_change_work);
2258
2259 mutex_lock(&ports->devr->mutex);
2260 mlx5_ib_gsi_pkey_change(ports->gsi);
2261 mutex_unlock(&ports->devr->mutex);
2262}
2263
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002264static void mlx5_ib_handle_internal_error(struct mlx5_ib_dev *ibdev)
2265{
2266 struct mlx5_ib_qp *mqp;
2267 struct mlx5_ib_cq *send_mcq, *recv_mcq;
2268 struct mlx5_core_cq *mcq;
2269 struct list_head cq_armed_list;
2270 unsigned long flags_qp;
2271 unsigned long flags_cq;
2272 unsigned long flags;
2273
2274 INIT_LIST_HEAD(&cq_armed_list);
2275
2276 /* Go over qp list reside on that ibdev, sync with create/destroy qp.*/
2277 spin_lock_irqsave(&ibdev->reset_flow_resource_lock, flags);
2278 list_for_each_entry(mqp, &ibdev->qp_list, qps_list) {
2279 spin_lock_irqsave(&mqp->sq.lock, flags_qp);
2280 if (mqp->sq.tail != mqp->sq.head) {
2281 send_mcq = to_mcq(mqp->ibqp.send_cq);
2282 spin_lock_irqsave(&send_mcq->lock, flags_cq);
2283 if (send_mcq->mcq.comp &&
2284 mqp->ibqp.send_cq->comp_handler) {
2285 if (!send_mcq->mcq.reset_notify_added) {
2286 send_mcq->mcq.reset_notify_added = 1;
2287 list_add_tail(&send_mcq->mcq.reset_notify,
2288 &cq_armed_list);
2289 }
2290 }
2291 spin_unlock_irqrestore(&send_mcq->lock, flags_cq);
2292 }
2293 spin_unlock_irqrestore(&mqp->sq.lock, flags_qp);
2294 spin_lock_irqsave(&mqp->rq.lock, flags_qp);
2295 /* no handling is needed for SRQ */
2296 if (!mqp->ibqp.srq) {
2297 if (mqp->rq.tail != mqp->rq.head) {
2298 recv_mcq = to_mcq(mqp->ibqp.recv_cq);
2299 spin_lock_irqsave(&recv_mcq->lock, flags_cq);
2300 if (recv_mcq->mcq.comp &&
2301 mqp->ibqp.recv_cq->comp_handler) {
2302 if (!recv_mcq->mcq.reset_notify_added) {
2303 recv_mcq->mcq.reset_notify_added = 1;
2304 list_add_tail(&recv_mcq->mcq.reset_notify,
2305 &cq_armed_list);
2306 }
2307 }
2308 spin_unlock_irqrestore(&recv_mcq->lock,
2309 flags_cq);
2310 }
2311 }
2312 spin_unlock_irqrestore(&mqp->rq.lock, flags_qp);
2313 }
2314 /*At that point all inflight post send were put to be executed as of we
2315 * lock/unlock above locks Now need to arm all involved CQs.
2316 */
2317 list_for_each_entry(mcq, &cq_armed_list, reset_notify) {
2318 mcq->comp(mcq);
2319 }
2320 spin_unlock_irqrestore(&ibdev->reset_flow_resource_lock, flags);
2321}
2322
Jack Morgenstein9603b612014-07-28 23:30:22 +03002323static void mlx5_ib_event(struct mlx5_core_dev *dev, void *context,
Jack Morgenstein4d2f9bb2014-07-28 23:30:24 +03002324 enum mlx5_dev_event event, unsigned long param)
Eli Cohene126ba92013-07-07 17:25:49 +03002325{
Jack Morgenstein9603b612014-07-28 23:30:22 +03002326 struct mlx5_ib_dev *ibdev = (struct mlx5_ib_dev *)context;
Eli Cohene126ba92013-07-07 17:25:49 +03002327 struct ib_event ibev;
Jack Morgenstein9603b612014-07-28 23:30:22 +03002328
Eli Cohene126ba92013-07-07 17:25:49 +03002329 u8 port = 0;
2330
2331 switch (event) {
2332 case MLX5_DEV_EVENT_SYS_ERROR:
2333 ibdev->ib_active = false;
2334 ibev.event = IB_EVENT_DEVICE_FATAL;
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03002335 mlx5_ib_handle_internal_error(ibdev);
Eli Cohene126ba92013-07-07 17:25:49 +03002336 break;
2337
2338 case MLX5_DEV_EVENT_PORT_UP:
Eli Cohene126ba92013-07-07 17:25:49 +03002339 case MLX5_DEV_EVENT_PORT_DOWN:
Noa Osherovich2788cf32016-06-04 15:15:29 +03002340 case MLX5_DEV_EVENT_PORT_INITIALIZED:
Jack Morgenstein4d2f9bb2014-07-28 23:30:24 +03002341 port = (u8)param;
Aviv Heller5ec8c832016-09-18 20:48:00 +03002342
2343 /* In RoCE, port up/down events are handled in
2344 * mlx5_netdev_event().
2345 */
2346 if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) ==
2347 IB_LINK_LAYER_ETHERNET)
2348 return;
2349
2350 ibev.event = (event == MLX5_DEV_EVENT_PORT_UP) ?
2351 IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
Eli Cohene126ba92013-07-07 17:25:49 +03002352 break;
2353
Eli Cohene126ba92013-07-07 17:25:49 +03002354 case MLX5_DEV_EVENT_LID_CHANGE:
2355 ibev.event = IB_EVENT_LID_CHANGE;
Jack Morgenstein4d2f9bb2014-07-28 23:30:24 +03002356 port = (u8)param;
Eli Cohene126ba92013-07-07 17:25:49 +03002357 break;
2358
2359 case MLX5_DEV_EVENT_PKEY_CHANGE:
2360 ibev.event = IB_EVENT_PKEY_CHANGE;
Jack Morgenstein4d2f9bb2014-07-28 23:30:24 +03002361 port = (u8)param;
Haggai Eran7722f472016-02-29 15:45:07 +02002362
2363 schedule_work(&ibdev->devr.ports[port - 1].pkey_change_work);
Eli Cohene126ba92013-07-07 17:25:49 +03002364 break;
2365
2366 case MLX5_DEV_EVENT_GUID_CHANGE:
2367 ibev.event = IB_EVENT_GID_CHANGE;
Jack Morgenstein4d2f9bb2014-07-28 23:30:24 +03002368 port = (u8)param;
Eli Cohene126ba92013-07-07 17:25:49 +03002369 break;
2370
2371 case MLX5_DEV_EVENT_CLIENT_REREG:
2372 ibev.event = IB_EVENT_CLIENT_REREGISTER;
Jack Morgenstein4d2f9bb2014-07-28 23:30:24 +03002373 port = (u8)param;
Eli Cohene126ba92013-07-07 17:25:49 +03002374 break;
Saeed Mahameedbdc37922016-09-29 19:35:38 +03002375 default:
2376 return;
Eli Cohene126ba92013-07-07 17:25:49 +03002377 }
2378
2379 ibev.device = &ibdev->ib_dev;
2380 ibev.element.port_num = port;
2381
Eli Cohena0c84c32013-09-11 16:35:27 +03002382 if (port < 1 || port > ibdev->num_ports) {
2383 mlx5_ib_warn(ibdev, "warning: event on port %d\n", port);
2384 return;
2385 }
2386
Eli Cohene126ba92013-07-07 17:25:49 +03002387 if (ibdev->ib_active)
2388 ib_dispatch_event(&ibev);
2389}
2390
2391static void get_ext_port_caps(struct mlx5_ib_dev *dev)
2392{
2393 int port;
2394
Saeed Mahameed938fe832015-05-28 22:28:41 +03002395 for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++)
Eli Cohene126ba92013-07-07 17:25:49 +03002396 mlx5_query_ext_port_caps(dev, port);
2397}
2398
2399static int get_port_caps(struct mlx5_ib_dev *dev)
2400{
2401 struct ib_device_attr *dprops = NULL;
2402 struct ib_port_attr *pprops = NULL;
Dan Carpenterf614fc12015-01-12 11:56:58 +03002403 int err = -ENOMEM;
Eli Cohene126ba92013-07-07 17:25:49 +03002404 int port;
Matan Barak2528e332015-06-11 16:35:25 +03002405 struct ib_udata uhw = {.inlen = 0, .outlen = 0};
Eli Cohene126ba92013-07-07 17:25:49 +03002406
2407 pprops = kmalloc(sizeof(*pprops), GFP_KERNEL);
2408 if (!pprops)
2409 goto out;
2410
2411 dprops = kmalloc(sizeof(*dprops), GFP_KERNEL);
2412 if (!dprops)
2413 goto out;
2414
Matan Barak2528e332015-06-11 16:35:25 +03002415 err = mlx5_ib_query_device(&dev->ib_dev, dprops, &uhw);
Eli Cohene126ba92013-07-07 17:25:49 +03002416 if (err) {
2417 mlx5_ib_warn(dev, "query_device failed %d\n", err);
2418 goto out;
2419 }
2420
Saeed Mahameed938fe832015-05-28 22:28:41 +03002421 for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++) {
Eli Cohene126ba92013-07-07 17:25:49 +03002422 err = mlx5_ib_query_port(&dev->ib_dev, port, pprops);
2423 if (err) {
Saeed Mahameed938fe832015-05-28 22:28:41 +03002424 mlx5_ib_warn(dev, "query_port %d failed %d\n",
2425 port, err);
Eli Cohene126ba92013-07-07 17:25:49 +03002426 break;
2427 }
Saeed Mahameed938fe832015-05-28 22:28:41 +03002428 dev->mdev->port_caps[port - 1].pkey_table_len =
2429 dprops->max_pkeys;
2430 dev->mdev->port_caps[port - 1].gid_table_len =
2431 pprops->gid_tbl_len;
Eli Cohene126ba92013-07-07 17:25:49 +03002432 mlx5_ib_dbg(dev, "pkey_table_len %d, gid_table_len %d\n",
2433 dprops->max_pkeys, pprops->gid_tbl_len);
2434 }
2435
2436out:
2437 kfree(pprops);
2438 kfree(dprops);
2439
2440 return err;
2441}
2442
2443static void destroy_umrc_res(struct mlx5_ib_dev *dev)
2444{
2445 int err;
2446
2447 err = mlx5_mr_cache_cleanup(dev);
2448 if (err)
2449 mlx5_ib_warn(dev, "mr cache cleanup failed\n");
2450
2451 mlx5_ib_destroy_qp(dev->umrc.qp);
Christoph Hellwigadd08d72016-03-03 09:38:22 +01002452 ib_free_cq(dev->umrc.cq);
Eli Cohene126ba92013-07-07 17:25:49 +03002453 ib_dealloc_pd(dev->umrc.pd);
2454}
2455
2456enum {
2457 MAX_UMR_WR = 128,
2458};
2459
2460static int create_umr_res(struct mlx5_ib_dev *dev)
2461{
2462 struct ib_qp_init_attr *init_attr = NULL;
2463 struct ib_qp_attr *attr = NULL;
2464 struct ib_pd *pd;
2465 struct ib_cq *cq;
2466 struct ib_qp *qp;
Eli Cohene126ba92013-07-07 17:25:49 +03002467 int ret;
2468
2469 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
2470 init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL);
2471 if (!attr || !init_attr) {
2472 ret = -ENOMEM;
2473 goto error_0;
2474 }
2475
Christoph Hellwiged082d32016-09-05 12:56:17 +02002476 pd = ib_alloc_pd(&dev->ib_dev, 0);
Eli Cohene126ba92013-07-07 17:25:49 +03002477 if (IS_ERR(pd)) {
2478 mlx5_ib_dbg(dev, "Couldn't create PD for sync UMR QP\n");
2479 ret = PTR_ERR(pd);
2480 goto error_0;
2481 }
2482
Christoph Hellwigadd08d72016-03-03 09:38:22 +01002483 cq = ib_alloc_cq(&dev->ib_dev, NULL, 128, 0, IB_POLL_SOFTIRQ);
Eli Cohene126ba92013-07-07 17:25:49 +03002484 if (IS_ERR(cq)) {
2485 mlx5_ib_dbg(dev, "Couldn't create CQ for sync UMR QP\n");
2486 ret = PTR_ERR(cq);
2487 goto error_2;
2488 }
Eli Cohene126ba92013-07-07 17:25:49 +03002489
2490 init_attr->send_cq = cq;
2491 init_attr->recv_cq = cq;
2492 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
2493 init_attr->cap.max_send_wr = MAX_UMR_WR;
2494 init_attr->cap.max_send_sge = 1;
2495 init_attr->qp_type = MLX5_IB_QPT_REG_UMR;
2496 init_attr->port_num = 1;
2497 qp = mlx5_ib_create_qp(pd, init_attr, NULL);
2498 if (IS_ERR(qp)) {
2499 mlx5_ib_dbg(dev, "Couldn't create sync UMR QP\n");
2500 ret = PTR_ERR(qp);
2501 goto error_3;
2502 }
2503 qp->device = &dev->ib_dev;
2504 qp->real_qp = qp;
2505 qp->uobject = NULL;
2506 qp->qp_type = MLX5_IB_QPT_REG_UMR;
2507
2508 attr->qp_state = IB_QPS_INIT;
2509 attr->port_num = 1;
2510 ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_PKEY_INDEX |
2511 IB_QP_PORT, NULL);
2512 if (ret) {
2513 mlx5_ib_dbg(dev, "Couldn't modify UMR QP\n");
2514 goto error_4;
2515 }
2516
2517 memset(attr, 0, sizeof(*attr));
2518 attr->qp_state = IB_QPS_RTR;
2519 attr->path_mtu = IB_MTU_256;
2520
2521 ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
2522 if (ret) {
2523 mlx5_ib_dbg(dev, "Couldn't modify umr QP to rtr\n");
2524 goto error_4;
2525 }
2526
2527 memset(attr, 0, sizeof(*attr));
2528 attr->qp_state = IB_QPS_RTS;
2529 ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
2530 if (ret) {
2531 mlx5_ib_dbg(dev, "Couldn't modify umr QP to rts\n");
2532 goto error_4;
2533 }
2534
2535 dev->umrc.qp = qp;
2536 dev->umrc.cq = cq;
Eli Cohene126ba92013-07-07 17:25:49 +03002537 dev->umrc.pd = pd;
2538
2539 sema_init(&dev->umrc.sem, MAX_UMR_WR);
2540 ret = mlx5_mr_cache_init(dev);
2541 if (ret) {
2542 mlx5_ib_warn(dev, "mr cache init failed %d\n", ret);
2543 goto error_4;
2544 }
2545
2546 kfree(attr);
2547 kfree(init_attr);
2548
2549 return 0;
2550
2551error_4:
2552 mlx5_ib_destroy_qp(qp);
2553
2554error_3:
Christoph Hellwigadd08d72016-03-03 09:38:22 +01002555 ib_free_cq(cq);
Eli Cohene126ba92013-07-07 17:25:49 +03002556
2557error_2:
Eli Cohene126ba92013-07-07 17:25:49 +03002558 ib_dealloc_pd(pd);
2559
2560error_0:
2561 kfree(attr);
2562 kfree(init_attr);
2563 return ret;
2564}
2565
2566static int create_dev_resources(struct mlx5_ib_resources *devr)
2567{
2568 struct ib_srq_init_attr attr;
2569 struct mlx5_ib_dev *dev;
Matan Barakbcf4c1e2015-06-11 16:35:20 +03002570 struct ib_cq_init_attr cq_attr = {.cqe = 1};
Haggai Eran7722f472016-02-29 15:45:07 +02002571 int port;
Eli Cohene126ba92013-07-07 17:25:49 +03002572 int ret = 0;
2573
2574 dev = container_of(devr, struct mlx5_ib_dev, devr);
2575
Haggai Erand16e91d2016-02-29 15:45:05 +02002576 mutex_init(&devr->mutex);
2577
Eli Cohene126ba92013-07-07 17:25:49 +03002578 devr->p0 = mlx5_ib_alloc_pd(&dev->ib_dev, NULL, NULL);
2579 if (IS_ERR(devr->p0)) {
2580 ret = PTR_ERR(devr->p0);
2581 goto error0;
2582 }
2583 devr->p0->device = &dev->ib_dev;
2584 devr->p0->uobject = NULL;
2585 atomic_set(&devr->p0->usecnt, 0);
2586
Matan Barakbcf4c1e2015-06-11 16:35:20 +03002587 devr->c0 = mlx5_ib_create_cq(&dev->ib_dev, &cq_attr, NULL, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03002588 if (IS_ERR(devr->c0)) {
2589 ret = PTR_ERR(devr->c0);
2590 goto error1;
2591 }
2592 devr->c0->device = &dev->ib_dev;
2593 devr->c0->uobject = NULL;
2594 devr->c0->comp_handler = NULL;
2595 devr->c0->event_handler = NULL;
2596 devr->c0->cq_context = NULL;
2597 atomic_set(&devr->c0->usecnt, 0);
2598
2599 devr->x0 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
2600 if (IS_ERR(devr->x0)) {
2601 ret = PTR_ERR(devr->x0);
2602 goto error2;
2603 }
2604 devr->x0->device = &dev->ib_dev;
2605 devr->x0->inode = NULL;
2606 atomic_set(&devr->x0->usecnt, 0);
2607 mutex_init(&devr->x0->tgt_qp_mutex);
2608 INIT_LIST_HEAD(&devr->x0->tgt_qp_list);
2609
2610 devr->x1 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
2611 if (IS_ERR(devr->x1)) {
2612 ret = PTR_ERR(devr->x1);
2613 goto error3;
2614 }
2615 devr->x1->device = &dev->ib_dev;
2616 devr->x1->inode = NULL;
2617 atomic_set(&devr->x1->usecnt, 0);
2618 mutex_init(&devr->x1->tgt_qp_mutex);
2619 INIT_LIST_HEAD(&devr->x1->tgt_qp_list);
2620
2621 memset(&attr, 0, sizeof(attr));
2622 attr.attr.max_sge = 1;
2623 attr.attr.max_wr = 1;
2624 attr.srq_type = IB_SRQT_XRC;
2625 attr.ext.xrc.cq = devr->c0;
2626 attr.ext.xrc.xrcd = devr->x0;
2627
2628 devr->s0 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
2629 if (IS_ERR(devr->s0)) {
2630 ret = PTR_ERR(devr->s0);
2631 goto error4;
2632 }
2633 devr->s0->device = &dev->ib_dev;
2634 devr->s0->pd = devr->p0;
2635 devr->s0->uobject = NULL;
2636 devr->s0->event_handler = NULL;
2637 devr->s0->srq_context = NULL;
2638 devr->s0->srq_type = IB_SRQT_XRC;
2639 devr->s0->ext.xrc.xrcd = devr->x0;
2640 devr->s0->ext.xrc.cq = devr->c0;
2641 atomic_inc(&devr->s0->ext.xrc.xrcd->usecnt);
2642 atomic_inc(&devr->s0->ext.xrc.cq->usecnt);
2643 atomic_inc(&devr->p0->usecnt);
2644 atomic_set(&devr->s0->usecnt, 0);
2645
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03002646 memset(&attr, 0, sizeof(attr));
2647 attr.attr.max_sge = 1;
2648 attr.attr.max_wr = 1;
2649 attr.srq_type = IB_SRQT_BASIC;
2650 devr->s1 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
2651 if (IS_ERR(devr->s1)) {
2652 ret = PTR_ERR(devr->s1);
2653 goto error5;
2654 }
2655 devr->s1->device = &dev->ib_dev;
2656 devr->s1->pd = devr->p0;
2657 devr->s1->uobject = NULL;
2658 devr->s1->event_handler = NULL;
2659 devr->s1->srq_context = NULL;
2660 devr->s1->srq_type = IB_SRQT_BASIC;
2661 devr->s1->ext.xrc.cq = devr->c0;
2662 atomic_inc(&devr->p0->usecnt);
2663 atomic_set(&devr->s0->usecnt, 0);
2664
Haggai Eran7722f472016-02-29 15:45:07 +02002665 for (port = 0; port < ARRAY_SIZE(devr->ports); ++port) {
2666 INIT_WORK(&devr->ports[port].pkey_change_work,
2667 pkey_change_handler);
2668 devr->ports[port].devr = devr;
2669 }
2670
Eli Cohene126ba92013-07-07 17:25:49 +03002671 return 0;
2672
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03002673error5:
2674 mlx5_ib_destroy_srq(devr->s0);
Eli Cohene126ba92013-07-07 17:25:49 +03002675error4:
2676 mlx5_ib_dealloc_xrcd(devr->x1);
2677error3:
2678 mlx5_ib_dealloc_xrcd(devr->x0);
2679error2:
2680 mlx5_ib_destroy_cq(devr->c0);
2681error1:
2682 mlx5_ib_dealloc_pd(devr->p0);
2683error0:
2684 return ret;
2685}
2686
2687static void destroy_dev_resources(struct mlx5_ib_resources *devr)
2688{
Haggai Eran7722f472016-02-29 15:45:07 +02002689 struct mlx5_ib_dev *dev =
2690 container_of(devr, struct mlx5_ib_dev, devr);
2691 int port;
2692
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03002693 mlx5_ib_destroy_srq(devr->s1);
Eli Cohene126ba92013-07-07 17:25:49 +03002694 mlx5_ib_destroy_srq(devr->s0);
2695 mlx5_ib_dealloc_xrcd(devr->x0);
2696 mlx5_ib_dealloc_xrcd(devr->x1);
2697 mlx5_ib_destroy_cq(devr->c0);
2698 mlx5_ib_dealloc_pd(devr->p0);
Haggai Eran7722f472016-02-29 15:45:07 +02002699
2700 /* Make sure no change P_Key work items are still executing */
2701 for (port = 0; port < dev->num_ports; ++port)
2702 cancel_work_sync(&devr->ports[port].pkey_change_work);
Eli Cohene126ba92013-07-07 17:25:49 +03002703}
2704
Achiad Shochate53505a2015-12-23 18:47:25 +02002705static u32 get_core_cap_flags(struct ib_device *ibdev)
2706{
2707 struct mlx5_ib_dev *dev = to_mdev(ibdev);
2708 enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, 1);
2709 u8 l3_type_cap = MLX5_CAP_ROCE(dev->mdev, l3_type);
2710 u8 roce_version_cap = MLX5_CAP_ROCE(dev->mdev, roce_version);
2711 u32 ret = 0;
2712
2713 if (ll == IB_LINK_LAYER_INFINIBAND)
2714 return RDMA_CORE_PORT_IBA_IB;
2715
2716 if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV4_CAP))
2717 return 0;
2718
2719 if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV6_CAP))
2720 return 0;
2721
2722 if (roce_version_cap & MLX5_ROCE_VERSION_1_CAP)
2723 ret |= RDMA_CORE_PORT_IBA_ROCE;
2724
2725 if (roce_version_cap & MLX5_ROCE_VERSION_2_CAP)
2726 ret |= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
2727
2728 return ret;
2729}
2730
Ira Weiny77386132015-05-13 20:02:58 -04002731static int mlx5_port_immutable(struct ib_device *ibdev, u8 port_num,
2732 struct ib_port_immutable *immutable)
2733{
2734 struct ib_port_attr attr;
2735 int err;
2736
2737 err = mlx5_ib_query_port(ibdev, port_num, &attr);
2738 if (err)
2739 return err;
2740
2741 immutable->pkey_tbl_len = attr.pkey_tbl_len;
2742 immutable->gid_tbl_len = attr.gid_tbl_len;
Achiad Shochate53505a2015-12-23 18:47:25 +02002743 immutable->core_cap_flags = get_core_cap_flags(ibdev);
Ira Weiny337877a2015-06-06 14:38:29 -04002744 immutable->max_mad_size = IB_MGMT_MAD_SIZE;
Ira Weiny77386132015-05-13 20:02:58 -04002745
2746 return 0;
2747}
2748
Ira Weinyc7342822016-06-15 02:22:01 -04002749static void get_dev_fw_str(struct ib_device *ibdev, char *str,
2750 size_t str_len)
2751{
2752 struct mlx5_ib_dev *dev =
2753 container_of(ibdev, struct mlx5_ib_dev, ib_dev);
2754 snprintf(str, str_len, "%d.%d.%04d", fw_rev_maj(dev->mdev),
2755 fw_rev_min(dev->mdev), fw_rev_sub(dev->mdev));
2756}
2757
Aviv Heller9ef9c642016-09-18 20:48:01 +03002758static int mlx5_roce_lag_init(struct mlx5_ib_dev *dev)
2759{
2760 struct mlx5_core_dev *mdev = dev->mdev;
2761 struct mlx5_flow_namespace *ns = mlx5_get_flow_namespace(mdev,
2762 MLX5_FLOW_NAMESPACE_LAG);
2763 struct mlx5_flow_table *ft;
2764 int err;
2765
2766 if (!ns || !mlx5_lag_is_active(mdev))
2767 return 0;
2768
2769 err = mlx5_cmd_create_vport_lag(mdev);
2770 if (err)
2771 return err;
2772
2773 ft = mlx5_create_lag_demux_flow_table(ns, 0, 0);
2774 if (IS_ERR(ft)) {
2775 err = PTR_ERR(ft);
2776 goto err_destroy_vport_lag;
2777 }
2778
2779 dev->flow_db.lag_demux_ft = ft;
2780 return 0;
2781
2782err_destroy_vport_lag:
2783 mlx5_cmd_destroy_vport_lag(mdev);
2784 return err;
2785}
2786
2787static void mlx5_roce_lag_cleanup(struct mlx5_ib_dev *dev)
2788{
2789 struct mlx5_core_dev *mdev = dev->mdev;
2790
2791 if (dev->flow_db.lag_demux_ft) {
2792 mlx5_destroy_flow_table(dev->flow_db.lag_demux_ft);
2793 dev->flow_db.lag_demux_ft = NULL;
2794
2795 mlx5_cmd_destroy_vport_lag(mdev);
2796 }
2797}
2798
Aviv Heller5ec8c832016-09-18 20:48:00 +03002799static void mlx5_remove_roce_notifier(struct mlx5_ib_dev *dev)
2800{
2801 if (dev->roce.nb.notifier_call) {
2802 unregister_netdevice_notifier(&dev->roce.nb);
2803 dev->roce.nb.notifier_call = NULL;
2804 }
2805}
2806
Achiad Shochatfc24fc52015-12-23 18:47:17 +02002807static int mlx5_enable_roce(struct mlx5_ib_dev *dev)
2808{
Achiad Shochate53505a2015-12-23 18:47:25 +02002809 int err;
2810
Achiad Shochatfc24fc52015-12-23 18:47:17 +02002811 dev->roce.nb.notifier_call = mlx5_netdev_event;
Achiad Shochate53505a2015-12-23 18:47:25 +02002812 err = register_netdevice_notifier(&dev->roce.nb);
Aviv Heller5ec8c832016-09-18 20:48:00 +03002813 if (err) {
2814 dev->roce.nb.notifier_call = NULL;
Achiad Shochate53505a2015-12-23 18:47:25 +02002815 return err;
Aviv Heller5ec8c832016-09-18 20:48:00 +03002816 }
Achiad Shochate53505a2015-12-23 18:47:25 +02002817
2818 err = mlx5_nic_vport_enable_roce(dev->mdev);
2819 if (err)
2820 goto err_unregister_netdevice_notifier;
2821
Aviv Heller9ef9c642016-09-18 20:48:01 +03002822 err = mlx5_roce_lag_init(dev);
2823 if (err)
2824 goto err_disable_roce;
2825
Achiad Shochate53505a2015-12-23 18:47:25 +02002826 return 0;
2827
Aviv Heller9ef9c642016-09-18 20:48:01 +03002828err_disable_roce:
2829 mlx5_nic_vport_disable_roce(dev->mdev);
2830
Achiad Shochate53505a2015-12-23 18:47:25 +02002831err_unregister_netdevice_notifier:
Aviv Heller5ec8c832016-09-18 20:48:00 +03002832 mlx5_remove_roce_notifier(dev);
Achiad Shochate53505a2015-12-23 18:47:25 +02002833 return err;
Achiad Shochatfc24fc52015-12-23 18:47:17 +02002834}
2835
2836static void mlx5_disable_roce(struct mlx5_ib_dev *dev)
2837{
Aviv Heller9ef9c642016-09-18 20:48:01 +03002838 mlx5_roce_lag_cleanup(dev);
Achiad Shochate53505a2015-12-23 18:47:25 +02002839 mlx5_nic_vport_disable_roce(dev->mdev);
Achiad Shochatfc24fc52015-12-23 18:47:17 +02002840}
2841
Mark Bloch0837e862016-06-17 15:10:55 +03002842static void mlx5_ib_dealloc_q_counters(struct mlx5_ib_dev *dev)
2843{
2844 unsigned int i;
2845
2846 for (i = 0; i < dev->num_ports; i++)
2847 mlx5_core_dealloc_q_counter(dev->mdev,
2848 dev->port[i].q_cnt_id);
2849}
2850
2851static int mlx5_ib_alloc_q_counters(struct mlx5_ib_dev *dev)
2852{
2853 int i;
2854 int ret;
2855
2856 for (i = 0; i < dev->num_ports; i++) {
2857 ret = mlx5_core_alloc_q_counter(dev->mdev,
2858 &dev->port[i].q_cnt_id);
2859 if (ret) {
2860 mlx5_ib_warn(dev,
2861 "couldn't allocate queue counter for port %d, err %d\n",
2862 i + 1, ret);
2863 goto dealloc_counters;
2864 }
2865 }
2866
2867 return 0;
2868
2869dealloc_counters:
2870 while (--i >= 0)
2871 mlx5_core_dealloc_q_counter(dev->mdev,
2872 dev->port[i].q_cnt_id);
2873
2874 return ret;
2875}
2876
Wei Yongjun61961502016-07-12 11:32:47 +00002877static const char * const names[] = {
Mark Bloch0ad17a82016-06-17 15:10:56 +03002878 "rx_write_requests",
2879 "rx_read_requests",
2880 "rx_atomic_requests",
2881 "out_of_buffer",
2882 "out_of_sequence",
2883 "duplicate_request",
2884 "rnr_nak_retry_err",
2885 "packet_seq_err",
2886 "implied_nak_seq_err",
2887 "local_ack_timeout_err",
2888};
2889
2890static const size_t stats_offsets[] = {
2891 MLX5_BYTE_OFF(query_q_counter_out, rx_write_requests),
2892 MLX5_BYTE_OFF(query_q_counter_out, rx_read_requests),
2893 MLX5_BYTE_OFF(query_q_counter_out, rx_atomic_requests),
2894 MLX5_BYTE_OFF(query_q_counter_out, out_of_buffer),
2895 MLX5_BYTE_OFF(query_q_counter_out, out_of_sequence),
2896 MLX5_BYTE_OFF(query_q_counter_out, duplicate_request),
2897 MLX5_BYTE_OFF(query_q_counter_out, rnr_nak_retry_err),
2898 MLX5_BYTE_OFF(query_q_counter_out, packet_seq_err),
2899 MLX5_BYTE_OFF(query_q_counter_out, implied_nak_seq_err),
2900 MLX5_BYTE_OFF(query_q_counter_out, local_ack_timeout_err),
2901};
2902
2903static struct rdma_hw_stats *mlx5_ib_alloc_hw_stats(struct ib_device *ibdev,
2904 u8 port_num)
2905{
2906 BUILD_BUG_ON(ARRAY_SIZE(names) != ARRAY_SIZE(stats_offsets));
2907
2908 /* We support only per port stats */
2909 if (port_num == 0)
2910 return NULL;
2911
2912 return rdma_alloc_hw_stats_struct(names, ARRAY_SIZE(names),
2913 RDMA_HW_STATS_DEFAULT_LIFESPAN);
2914}
2915
2916static int mlx5_ib_get_hw_stats(struct ib_device *ibdev,
2917 struct rdma_hw_stats *stats,
2918 u8 port, int index)
2919{
2920 struct mlx5_ib_dev *dev = to_mdev(ibdev);
2921 int outlen = MLX5_ST_SZ_BYTES(query_q_counter_out);
2922 void *out;
2923 __be32 val;
2924 int ret;
2925 int i;
2926
2927 if (!port || !stats)
2928 return -ENOSYS;
2929
2930 out = mlx5_vzalloc(outlen);
2931 if (!out)
2932 return -ENOMEM;
2933
2934 ret = mlx5_core_query_q_counter(dev->mdev,
2935 dev->port[port - 1].q_cnt_id, 0,
2936 out, outlen);
2937 if (ret)
2938 goto free;
2939
2940 for (i = 0; i < ARRAY_SIZE(names); i++) {
2941 val = *(__be32 *)(out + stats_offsets[i]);
2942 stats->value[i] = (u64)be32_to_cpu(val);
2943 }
2944free:
2945 kvfree(out);
2946 return ARRAY_SIZE(names);
2947}
2948
Jack Morgenstein9603b612014-07-28 23:30:22 +03002949static void *mlx5_ib_add(struct mlx5_core_dev *mdev)
Eli Cohene126ba92013-07-07 17:25:49 +03002950{
Eli Cohene126ba92013-07-07 17:25:49 +03002951 struct mlx5_ib_dev *dev;
Achiad Shochatebd61f62015-12-23 18:47:16 +02002952 enum rdma_link_layer ll;
2953 int port_type_cap;
Aviv Heller4babcf92016-09-18 20:48:03 +03002954 const char *name;
Eli Cohene126ba92013-07-07 17:25:49 +03002955 int err;
2956 int i;
2957
Achiad Shochatebd61f62015-12-23 18:47:16 +02002958 port_type_cap = MLX5_CAP_GEN(mdev, port_type);
2959 ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
2960
Achiad Shochate53505a2015-12-23 18:47:25 +02002961 if ((ll == IB_LINK_LAYER_ETHERNET) && !MLX5_CAP_GEN(mdev, roce))
Majd Dibbiny647241e2015-06-04 19:30:47 +03002962 return NULL;
2963
Eli Cohene126ba92013-07-07 17:25:49 +03002964 printk_once(KERN_INFO "%s", mlx5_version);
2965
2966 dev = (struct mlx5_ib_dev *)ib_alloc_device(sizeof(*dev));
2967 if (!dev)
Jack Morgenstein9603b612014-07-28 23:30:22 +03002968 return NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03002969
Jack Morgenstein9603b612014-07-28 23:30:22 +03002970 dev->mdev = mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03002971
Mark Bloch0837e862016-06-17 15:10:55 +03002972 dev->port = kcalloc(MLX5_CAP_GEN(mdev, num_ports), sizeof(*dev->port),
2973 GFP_KERNEL);
2974 if (!dev->port)
2975 goto err_dealloc;
2976
Achiad Shochatfc24fc52015-12-23 18:47:17 +02002977 rwlock_init(&dev->roce.netdev_lock);
Eli Cohene126ba92013-07-07 17:25:49 +03002978 err = get_port_caps(dev);
2979 if (err)
Mark Bloch0837e862016-06-17 15:10:55 +03002980 goto err_free_port;
Eli Cohene126ba92013-07-07 17:25:49 +03002981
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03002982 if (mlx5_use_mad_ifc(dev))
2983 get_ext_port_caps(dev);
Eli Cohene126ba92013-07-07 17:25:49 +03002984
Eli Cohene126ba92013-07-07 17:25:49 +03002985 MLX5_INIT_DOORBELL_LOCK(&dev->uar_lock);
2986
Aviv Heller4babcf92016-09-18 20:48:03 +03002987 if (!mlx5_lag_is_active(mdev))
2988 name = "mlx5_%d";
2989 else
2990 name = "mlx5_bond_%d";
2991
2992 strlcpy(dev->ib_dev.name, name, IB_DEVICE_NAME_MAX);
Eli Cohene126ba92013-07-07 17:25:49 +03002993 dev->ib_dev.owner = THIS_MODULE;
2994 dev->ib_dev.node_type = RDMA_NODE_IB_CA;
Sagi Grimbergc6790aa2015-09-24 10:34:23 +03002995 dev->ib_dev.local_dma_lkey = 0 /* not supported for now */;
Saeed Mahameed938fe832015-05-28 22:28:41 +03002996 dev->num_ports = MLX5_CAP_GEN(mdev, num_ports);
Eli Cohene126ba92013-07-07 17:25:49 +03002997 dev->ib_dev.phys_port_cnt = dev->num_ports;
Saeed Mahameed233d05d2015-04-02 17:07:32 +03002998 dev->ib_dev.num_comp_vectors =
2999 dev->mdev->priv.eq_table.num_comp_vectors;
Eli Cohene126ba92013-07-07 17:25:49 +03003000 dev->ib_dev.dma_device = &mdev->pdev->dev;
3001
3002 dev->ib_dev.uverbs_abi_ver = MLX5_IB_UVERBS_ABI_VERSION;
3003 dev->ib_dev.uverbs_cmd_mask =
3004 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
3005 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
3006 (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
3007 (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
3008 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
3009 (1ull << IB_USER_VERBS_CMD_REG_MR) |
Noa Osherovich56e11d62016-02-29 16:46:51 +02003010 (1ull << IB_USER_VERBS_CMD_REREG_MR) |
Eli Cohene126ba92013-07-07 17:25:49 +03003011 (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
3012 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
3013 (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
3014 (1ull << IB_USER_VERBS_CMD_RESIZE_CQ) |
3015 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
3016 (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
3017 (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
3018 (1ull << IB_USER_VERBS_CMD_QUERY_QP) |
3019 (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
3020 (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST) |
3021 (1ull << IB_USER_VERBS_CMD_DETACH_MCAST) |
3022 (1ull << IB_USER_VERBS_CMD_CREATE_SRQ) |
3023 (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ) |
3024 (1ull << IB_USER_VERBS_CMD_QUERY_SRQ) |
3025 (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ) |
3026 (1ull << IB_USER_VERBS_CMD_CREATE_XSRQ) |
3027 (1ull << IB_USER_VERBS_CMD_OPEN_QP);
Haggai Eran1707cb42015-02-08 13:28:52 +02003028 dev->ib_dev.uverbs_ex_cmd_mask =
Matan Barakd4584dd2016-01-28 17:51:46 +02003029 (1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE) |
3030 (1ull << IB_USER_VERBS_EX_CMD_CREATE_CQ) |
3031 (1ull << IB_USER_VERBS_EX_CMD_CREATE_QP);
Eli Cohene126ba92013-07-07 17:25:49 +03003032
3033 dev->ib_dev.query_device = mlx5_ib_query_device;
3034 dev->ib_dev.query_port = mlx5_ib_query_port;
Achiad Shochatebd61f62015-12-23 18:47:16 +02003035 dev->ib_dev.get_link_layer = mlx5_ib_port_link_layer;
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003036 if (ll == IB_LINK_LAYER_ETHERNET)
3037 dev->ib_dev.get_netdev = mlx5_ib_get_netdev;
Eli Cohene126ba92013-07-07 17:25:49 +03003038 dev->ib_dev.query_gid = mlx5_ib_query_gid;
Achiad Shochat3cca2602015-12-23 18:47:23 +02003039 dev->ib_dev.add_gid = mlx5_ib_add_gid;
3040 dev->ib_dev.del_gid = mlx5_ib_del_gid;
Eli Cohene126ba92013-07-07 17:25:49 +03003041 dev->ib_dev.query_pkey = mlx5_ib_query_pkey;
3042 dev->ib_dev.modify_device = mlx5_ib_modify_device;
3043 dev->ib_dev.modify_port = mlx5_ib_modify_port;
3044 dev->ib_dev.alloc_ucontext = mlx5_ib_alloc_ucontext;
3045 dev->ib_dev.dealloc_ucontext = mlx5_ib_dealloc_ucontext;
3046 dev->ib_dev.mmap = mlx5_ib_mmap;
3047 dev->ib_dev.alloc_pd = mlx5_ib_alloc_pd;
3048 dev->ib_dev.dealloc_pd = mlx5_ib_dealloc_pd;
3049 dev->ib_dev.create_ah = mlx5_ib_create_ah;
3050 dev->ib_dev.query_ah = mlx5_ib_query_ah;
3051 dev->ib_dev.destroy_ah = mlx5_ib_destroy_ah;
3052 dev->ib_dev.create_srq = mlx5_ib_create_srq;
3053 dev->ib_dev.modify_srq = mlx5_ib_modify_srq;
3054 dev->ib_dev.query_srq = mlx5_ib_query_srq;
3055 dev->ib_dev.destroy_srq = mlx5_ib_destroy_srq;
3056 dev->ib_dev.post_srq_recv = mlx5_ib_post_srq_recv;
3057 dev->ib_dev.create_qp = mlx5_ib_create_qp;
3058 dev->ib_dev.modify_qp = mlx5_ib_modify_qp;
3059 dev->ib_dev.query_qp = mlx5_ib_query_qp;
3060 dev->ib_dev.destroy_qp = mlx5_ib_destroy_qp;
3061 dev->ib_dev.post_send = mlx5_ib_post_send;
3062 dev->ib_dev.post_recv = mlx5_ib_post_recv;
3063 dev->ib_dev.create_cq = mlx5_ib_create_cq;
3064 dev->ib_dev.modify_cq = mlx5_ib_modify_cq;
3065 dev->ib_dev.resize_cq = mlx5_ib_resize_cq;
3066 dev->ib_dev.destroy_cq = mlx5_ib_destroy_cq;
3067 dev->ib_dev.poll_cq = mlx5_ib_poll_cq;
3068 dev->ib_dev.req_notify_cq = mlx5_ib_arm_cq;
3069 dev->ib_dev.get_dma_mr = mlx5_ib_get_dma_mr;
3070 dev->ib_dev.reg_user_mr = mlx5_ib_reg_user_mr;
Noa Osherovich56e11d62016-02-29 16:46:51 +02003071 dev->ib_dev.rereg_user_mr = mlx5_ib_rereg_user_mr;
Eli Cohene126ba92013-07-07 17:25:49 +03003072 dev->ib_dev.dereg_mr = mlx5_ib_dereg_mr;
3073 dev->ib_dev.attach_mcast = mlx5_ib_mcg_attach;
3074 dev->ib_dev.detach_mcast = mlx5_ib_mcg_detach;
3075 dev->ib_dev.process_mad = mlx5_ib_process_mad;
Sagi Grimberg9bee1782015-07-30 10:32:35 +03003076 dev->ib_dev.alloc_mr = mlx5_ib_alloc_mr;
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03003077 dev->ib_dev.map_mr_sg = mlx5_ib_map_mr_sg;
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02003078 dev->ib_dev.check_mr_status = mlx5_ib_check_mr_status;
Ira Weiny77386132015-05-13 20:02:58 -04003079 dev->ib_dev.get_port_immutable = mlx5_port_immutable;
Ira Weinyc7342822016-06-15 02:22:01 -04003080 dev->ib_dev.get_dev_fw_str = get_dev_fw_str;
Eli Coheneff901d2016-03-11 22:58:42 +02003081 if (mlx5_core_is_pf(mdev)) {
3082 dev->ib_dev.get_vf_config = mlx5_ib_get_vf_config;
3083 dev->ib_dev.set_vf_link_state = mlx5_ib_set_vf_link_state;
3084 dev->ib_dev.get_vf_stats = mlx5_ib_get_vf_stats;
3085 dev->ib_dev.set_vf_guid = mlx5_ib_set_vf_guid;
3086 }
Eli Cohene126ba92013-07-07 17:25:49 +03003087
Maor Gottlieb7c2344c2016-06-17 14:56:44 +03003088 dev->ib_dev.disassociate_ucontext = mlx5_ib_disassociate_ucontext;
3089
Saeed Mahameed938fe832015-05-28 22:28:41 +03003090 mlx5_ib_internal_fill_odp_caps(dev);
Haggai Eran8cdd3122014-12-11 17:04:20 +02003091
Matan Barakd2370e02016-02-29 18:05:30 +02003092 if (MLX5_CAP_GEN(mdev, imaicl)) {
3093 dev->ib_dev.alloc_mw = mlx5_ib_alloc_mw;
3094 dev->ib_dev.dealloc_mw = mlx5_ib_dealloc_mw;
3095 dev->ib_dev.uverbs_cmd_mask |=
3096 (1ull << IB_USER_VERBS_CMD_ALLOC_MW) |
3097 (1ull << IB_USER_VERBS_CMD_DEALLOC_MW);
3098 }
3099
Mark Bloch0ad17a82016-06-17 15:10:56 +03003100 if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt) &&
3101 MLX5_CAP_GEN(dev->mdev, retransmission_q_counters)) {
3102 dev->ib_dev.get_hw_stats = mlx5_ib_get_hw_stats;
3103 dev->ib_dev.alloc_hw_stats = mlx5_ib_alloc_hw_stats;
3104 }
3105
Saeed Mahameed938fe832015-05-28 22:28:41 +03003106 if (MLX5_CAP_GEN(mdev, xrc)) {
Eli Cohene126ba92013-07-07 17:25:49 +03003107 dev->ib_dev.alloc_xrcd = mlx5_ib_alloc_xrcd;
3108 dev->ib_dev.dealloc_xrcd = mlx5_ib_dealloc_xrcd;
3109 dev->ib_dev.uverbs_cmd_mask |=
3110 (1ull << IB_USER_VERBS_CMD_OPEN_XRCD) |
3111 (1ull << IB_USER_VERBS_CMD_CLOSE_XRCD);
3112 }
3113
Linus Torvalds048ccca2016-01-23 18:45:06 -08003114 if (mlx5_ib_port_link_layer(&dev->ib_dev, 1) ==
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003115 IB_LINK_LAYER_ETHERNET) {
3116 dev->ib_dev.create_flow = mlx5_ib_create_flow;
3117 dev->ib_dev.destroy_flow = mlx5_ib_destroy_flow;
Yishai Hadas79b20a62016-05-23 15:20:50 +03003118 dev->ib_dev.create_wq = mlx5_ib_create_wq;
3119 dev->ib_dev.modify_wq = mlx5_ib_modify_wq;
3120 dev->ib_dev.destroy_wq = mlx5_ib_destroy_wq;
Yishai Hadasc5f90922016-05-23 15:20:53 +03003121 dev->ib_dev.create_rwq_ind_table = mlx5_ib_create_rwq_ind_table;
3122 dev->ib_dev.destroy_rwq_ind_table = mlx5_ib_destroy_rwq_ind_table;
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003123 dev->ib_dev.uverbs_ex_cmd_mask |=
3124 (1ull << IB_USER_VERBS_EX_CMD_CREATE_FLOW) |
Yishai Hadas79b20a62016-05-23 15:20:50 +03003125 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_FLOW) |
3126 (1ull << IB_USER_VERBS_EX_CMD_CREATE_WQ) |
3127 (1ull << IB_USER_VERBS_EX_CMD_MODIFY_WQ) |
Yishai Hadasc5f90922016-05-23 15:20:53 +03003128 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_WQ) |
3129 (1ull << IB_USER_VERBS_EX_CMD_CREATE_RWQ_IND_TBL) |
3130 (1ull << IB_USER_VERBS_EX_CMD_DESTROY_RWQ_IND_TBL);
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003131 }
Eli Cohene126ba92013-07-07 17:25:49 +03003132 err = init_node_data(dev);
3133 if (err)
Saeed Mahameed233d05d2015-04-02 17:07:32 +03003134 goto err_dealloc;
Eli Cohene126ba92013-07-07 17:25:49 +03003135
Maor Gottlieb038d2ef2016-01-11 10:26:07 +02003136 mutex_init(&dev->flow_db.lock);
Eli Cohene126ba92013-07-07 17:25:49 +03003137 mutex_init(&dev->cap_mask_mutex);
Maor Gottlieb89ea94a72016-06-17 15:01:38 +03003138 INIT_LIST_HEAD(&dev->qp_list);
3139 spin_lock_init(&dev->reset_flow_resource_lock);
Eli Cohene126ba92013-07-07 17:25:49 +03003140
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003141 if (ll == IB_LINK_LAYER_ETHERNET) {
3142 err = mlx5_enable_roce(dev);
3143 if (err)
3144 goto err_dealloc;
3145 }
3146
Eli Cohene126ba92013-07-07 17:25:49 +03003147 err = create_dev_resources(&dev->devr);
3148 if (err)
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003149 goto err_disable_roce;
Eli Cohene126ba92013-07-07 17:25:49 +03003150
Haggai Eran6aec21f2014-12-11 17:04:23 +02003151 err = mlx5_ib_odp_init_one(dev);
Wei Yongjun281d1a92013-07-30 07:54:26 +08003152 if (err)
Eli Cohene126ba92013-07-07 17:25:49 +03003153 goto err_rsrc;
3154
Mark Bloch0837e862016-06-17 15:10:55 +03003155 err = mlx5_ib_alloc_q_counters(dev);
Haggai Eran6aec21f2014-12-11 17:04:23 +02003156 if (err)
3157 goto err_odp;
3158
Mark Bloch0837e862016-06-17 15:10:55 +03003159 err = ib_register_device(&dev->ib_dev, NULL);
3160 if (err)
3161 goto err_q_cnt;
3162
Eli Cohene126ba92013-07-07 17:25:49 +03003163 err = create_umr_res(dev);
3164 if (err)
3165 goto err_dev;
3166
3167 for (i = 0; i < ARRAY_SIZE(mlx5_class_attributes); i++) {
Wei Yongjun281d1a92013-07-30 07:54:26 +08003168 err = device_create_file(&dev->ib_dev.dev,
3169 mlx5_class_attributes[i]);
3170 if (err)
Eli Cohene126ba92013-07-07 17:25:49 +03003171 goto err_umrc;
3172 }
3173
3174 dev->ib_active = true;
3175
Jack Morgenstein9603b612014-07-28 23:30:22 +03003176 return dev;
Eli Cohene126ba92013-07-07 17:25:49 +03003177
3178err_umrc:
3179 destroy_umrc_res(dev);
3180
3181err_dev:
3182 ib_unregister_device(&dev->ib_dev);
3183
Mark Bloch0837e862016-06-17 15:10:55 +03003184err_q_cnt:
3185 mlx5_ib_dealloc_q_counters(dev);
3186
Haggai Eran6aec21f2014-12-11 17:04:23 +02003187err_odp:
3188 mlx5_ib_odp_remove_one(dev);
3189
Eli Cohene126ba92013-07-07 17:25:49 +03003190err_rsrc:
3191 destroy_dev_resources(&dev->devr);
3192
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003193err_disable_roce:
Aviv Heller5ec8c832016-09-18 20:48:00 +03003194 if (ll == IB_LINK_LAYER_ETHERNET) {
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003195 mlx5_disable_roce(dev);
Aviv Heller5ec8c832016-09-18 20:48:00 +03003196 mlx5_remove_roce_notifier(dev);
3197 }
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003198
Mark Bloch0837e862016-06-17 15:10:55 +03003199err_free_port:
3200 kfree(dev->port);
3201
Jack Morgenstein9603b612014-07-28 23:30:22 +03003202err_dealloc:
Eli Cohene126ba92013-07-07 17:25:49 +03003203 ib_dealloc_device((struct ib_device *)dev);
3204
Jack Morgenstein9603b612014-07-28 23:30:22 +03003205 return NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03003206}
3207
Jack Morgenstein9603b612014-07-28 23:30:22 +03003208static void mlx5_ib_remove(struct mlx5_core_dev *mdev, void *context)
Eli Cohene126ba92013-07-07 17:25:49 +03003209{
Jack Morgenstein9603b612014-07-28 23:30:22 +03003210 struct mlx5_ib_dev *dev = context;
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003211 enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev, 1);
Haggai Eran6aec21f2014-12-11 17:04:23 +02003212
Aviv Heller5ec8c832016-09-18 20:48:00 +03003213 mlx5_remove_roce_notifier(dev);
Eli Cohene126ba92013-07-07 17:25:49 +03003214 ib_unregister_device(&dev->ib_dev);
Mark Bloch0837e862016-06-17 15:10:55 +03003215 mlx5_ib_dealloc_q_counters(dev);
Eli Coheneefd56e2014-09-14 16:47:50 +03003216 destroy_umrc_res(dev);
Haggai Eran6aec21f2014-12-11 17:04:23 +02003217 mlx5_ib_odp_remove_one(dev);
Eli Cohene126ba92013-07-07 17:25:49 +03003218 destroy_dev_resources(&dev->devr);
Achiad Shochatfc24fc52015-12-23 18:47:17 +02003219 if (ll == IB_LINK_LAYER_ETHERNET)
3220 mlx5_disable_roce(dev);
Mark Bloch0837e862016-06-17 15:10:55 +03003221 kfree(dev->port);
Eli Cohene126ba92013-07-07 17:25:49 +03003222 ib_dealloc_device(&dev->ib_dev);
3223}
3224
Jack Morgenstein9603b612014-07-28 23:30:22 +03003225static struct mlx5_interface mlx5_ib_interface = {
3226 .add = mlx5_ib_add,
3227 .remove = mlx5_ib_remove,
3228 .event = mlx5_ib_event,
Saeed Mahameed64613d942015-04-02 17:07:34 +03003229 .protocol = MLX5_INTERFACE_PROTOCOL_IB,
Eli Cohene126ba92013-07-07 17:25:49 +03003230};
3231
3232static int __init mlx5_ib_init(void)
3233{
Haggai Eran6aec21f2014-12-11 17:04:23 +02003234 int err;
3235
Jack Morgenstein9603b612014-07-28 23:30:22 +03003236 if (deprecated_prof_sel != 2)
3237 pr_warn("prof_sel is deprecated for mlx5_ib, set it for mlx5_core\n");
3238
Haggai Eran6aec21f2014-12-11 17:04:23 +02003239 err = mlx5_ib_odp_init();
3240 if (err)
3241 return err;
3242
3243 err = mlx5_register_interface(&mlx5_ib_interface);
3244 if (err)
3245 goto clean_odp;
3246
3247 return err;
3248
3249clean_odp:
3250 mlx5_ib_odp_cleanup();
3251 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03003252}
3253
3254static void __exit mlx5_ib_cleanup(void)
3255{
Jack Morgenstein9603b612014-07-28 23:30:22 +03003256 mlx5_unregister_interface(&mlx5_ib_interface);
Haggai Eran6aec21f2014-12-11 17:04:23 +02003257 mlx5_ib_odp_cleanup();
Eli Cohene126ba92013-07-07 17:25:49 +03003258}
3259
3260module_init(mlx5_ib_init);
3261module_exit(mlx5_ib_cleanup);