blob: 8c6e144ae75b3337624c2d2ba3e1892785c4d2c8 [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>
40#include <linux/io-mapping.h>
41#include <linux/sched.h>
42#include <rdma/ib_user_verbs.h>
Majd Dibbiny1b5daf12015-06-04 19:30:46 +030043#include <linux/mlx5/vport.h>
Eli Cohene126ba92013-07-07 17:25:49 +030044#include <rdma/ib_smi.h>
45#include <rdma/ib_umem.h>
46#include "user.h"
47#include "mlx5_ib.h"
48
49#define DRIVER_NAME "mlx5_ib"
Amir Vadai169a1d82014-02-19 17:47:31 +020050#define DRIVER_VERSION "2.2-1"
51#define DRIVER_RELDATE "Feb 2014"
Eli Cohene126ba92013-07-07 17:25:49 +030052
53MODULE_AUTHOR("Eli Cohen <eli@mellanox.com>");
54MODULE_DESCRIPTION("Mellanox Connect-IB HCA IB driver");
55MODULE_LICENSE("Dual BSD/GPL");
56MODULE_VERSION(DRIVER_VERSION);
57
Jack Morgenstein9603b612014-07-28 23:30:22 +030058static int deprecated_prof_sel = 2;
59module_param_named(prof_sel, deprecated_prof_sel, int, 0444);
60MODULE_PARM_DESC(prof_sel, "profile selector. Deprecated here. Moved to module mlx5_core");
Eli Cohene126ba92013-07-07 17:25:49 +030061
62static char mlx5_version[] =
63 DRIVER_NAME ": Mellanox Connect-IB Infiniband driver v"
64 DRIVER_VERSION " (" DRIVER_RELDATE ")\n";
65
Majd Dibbiny1b5daf12015-06-04 19:30:46 +030066static enum rdma_link_layer
Achiad Shochatebd61f62015-12-23 18:47:16 +020067mlx5_port_type_cap_to_rdma_ll(int port_type_cap)
Majd Dibbiny1b5daf12015-06-04 19:30:46 +030068{
Achiad Shochatebd61f62015-12-23 18:47:16 +020069 switch (port_type_cap) {
Majd Dibbiny1b5daf12015-06-04 19:30:46 +030070 case MLX5_CAP_PORT_TYPE_IB:
71 return IB_LINK_LAYER_INFINIBAND;
72 case MLX5_CAP_PORT_TYPE_ETH:
73 return IB_LINK_LAYER_ETHERNET;
74 default:
75 return IB_LINK_LAYER_UNSPECIFIED;
76 }
77}
78
Achiad Shochatebd61f62015-12-23 18:47:16 +020079static enum rdma_link_layer
80mlx5_ib_port_link_layer(struct ib_device *device, u8 port_num)
81{
82 struct mlx5_ib_dev *dev = to_mdev(device);
83 int port_type_cap = MLX5_CAP_GEN(dev->mdev, port_type);
84
85 return mlx5_port_type_cap_to_rdma_ll(port_type_cap);
86}
87
Achiad Shochatfc24fc52015-12-23 18:47:17 +020088static int mlx5_netdev_event(struct notifier_block *this,
89 unsigned long event, void *ptr)
90{
91 struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
92 struct mlx5_ib_dev *ibdev = container_of(this, struct mlx5_ib_dev,
93 roce.nb);
94
95 if ((event != NETDEV_UNREGISTER) && (event != NETDEV_REGISTER))
96 return NOTIFY_DONE;
97
98 write_lock(&ibdev->roce.netdev_lock);
99 if (ndev->dev.parent == &ibdev->mdev->pdev->dev)
100 ibdev->roce.netdev = (event == NETDEV_UNREGISTER) ? NULL : ndev;
101 write_unlock(&ibdev->roce.netdev_lock);
102
103 return NOTIFY_DONE;
104}
105
106static struct net_device *mlx5_ib_get_netdev(struct ib_device *device,
107 u8 port_num)
108{
109 struct mlx5_ib_dev *ibdev = to_mdev(device);
110 struct net_device *ndev;
111
112 /* Ensure ndev does not disappear before we invoke dev_hold()
113 */
114 read_lock(&ibdev->roce.netdev_lock);
115 ndev = ibdev->roce.netdev;
116 if (ndev)
117 dev_hold(ndev);
118 read_unlock(&ibdev->roce.netdev_lock);
119
120 return ndev;
121}
122
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300123static int mlx5_use_mad_ifc(struct mlx5_ib_dev *dev)
124{
125 return !dev->mdev->issi;
126}
127
128enum {
129 MLX5_VPORT_ACCESS_METHOD_MAD,
130 MLX5_VPORT_ACCESS_METHOD_HCA,
131 MLX5_VPORT_ACCESS_METHOD_NIC,
132};
133
134static int mlx5_get_vport_access_method(struct ib_device *ibdev)
135{
136 if (mlx5_use_mad_ifc(to_mdev(ibdev)))
137 return MLX5_VPORT_ACCESS_METHOD_MAD;
138
Achiad Shochatebd61f62015-12-23 18:47:16 +0200139 if (mlx5_ib_port_link_layer(ibdev, 1) ==
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300140 IB_LINK_LAYER_ETHERNET)
141 return MLX5_VPORT_ACCESS_METHOD_NIC;
142
143 return MLX5_VPORT_ACCESS_METHOD_HCA;
144}
145
146static int mlx5_query_system_image_guid(struct ib_device *ibdev,
147 __be64 *sys_image_guid)
148{
149 struct mlx5_ib_dev *dev = to_mdev(ibdev);
150 struct mlx5_core_dev *mdev = dev->mdev;
151 u64 tmp;
152 int err;
153
154 switch (mlx5_get_vport_access_method(ibdev)) {
155 case MLX5_VPORT_ACCESS_METHOD_MAD:
156 return mlx5_query_mad_ifc_system_image_guid(ibdev,
157 sys_image_guid);
158
159 case MLX5_VPORT_ACCESS_METHOD_HCA:
160 err = mlx5_query_hca_vport_system_image_guid(mdev, &tmp);
161 if (!err)
162 *sys_image_guid = cpu_to_be64(tmp);
163 return err;
164
165 default:
166 return -EINVAL;
167 }
168}
169
170static int mlx5_query_max_pkeys(struct ib_device *ibdev,
171 u16 *max_pkeys)
172{
173 struct mlx5_ib_dev *dev = to_mdev(ibdev);
174 struct mlx5_core_dev *mdev = dev->mdev;
175
176 switch (mlx5_get_vport_access_method(ibdev)) {
177 case MLX5_VPORT_ACCESS_METHOD_MAD:
178 return mlx5_query_mad_ifc_max_pkeys(ibdev, max_pkeys);
179
180 case MLX5_VPORT_ACCESS_METHOD_HCA:
181 case MLX5_VPORT_ACCESS_METHOD_NIC:
182 *max_pkeys = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev,
183 pkey_table_size));
184 return 0;
185
186 default:
187 return -EINVAL;
188 }
189}
190
191static int mlx5_query_vendor_id(struct ib_device *ibdev,
192 u32 *vendor_id)
193{
194 struct mlx5_ib_dev *dev = to_mdev(ibdev);
195
196 switch (mlx5_get_vport_access_method(ibdev)) {
197 case MLX5_VPORT_ACCESS_METHOD_MAD:
198 return mlx5_query_mad_ifc_vendor_id(ibdev, vendor_id);
199
200 case MLX5_VPORT_ACCESS_METHOD_HCA:
201 case MLX5_VPORT_ACCESS_METHOD_NIC:
202 return mlx5_core_query_vendor_id(dev->mdev, vendor_id);
203
204 default:
205 return -EINVAL;
206 }
207}
208
209static int mlx5_query_node_guid(struct mlx5_ib_dev *dev,
210 __be64 *node_guid)
211{
212 u64 tmp;
213 int err;
214
215 switch (mlx5_get_vport_access_method(&dev->ib_dev)) {
216 case MLX5_VPORT_ACCESS_METHOD_MAD:
217 return mlx5_query_mad_ifc_node_guid(dev, node_guid);
218
219 case MLX5_VPORT_ACCESS_METHOD_HCA:
220 err = mlx5_query_hca_vport_node_guid(dev->mdev, &tmp);
221 if (!err)
222 *node_guid = cpu_to_be64(tmp);
223 return err;
224
225 default:
226 return -EINVAL;
227 }
228}
229
230struct mlx5_reg_node_desc {
231 u8 desc[64];
232};
233
234static int mlx5_query_node_desc(struct mlx5_ib_dev *dev, char *node_desc)
235{
236 struct mlx5_reg_node_desc in;
237
238 if (mlx5_use_mad_ifc(dev))
239 return mlx5_query_mad_ifc_node_desc(dev, node_desc);
240
241 memset(&in, 0, sizeof(in));
242
243 return mlx5_core_access_reg(dev->mdev, &in, sizeof(in), node_desc,
244 sizeof(struct mlx5_reg_node_desc),
245 MLX5_REG_NODE_DESC, 0, 0);
246}
247
Eli Cohene126ba92013-07-07 17:25:49 +0300248static int mlx5_ib_query_device(struct ib_device *ibdev,
Matan Barak2528e332015-06-11 16:35:25 +0300249 struct ib_device_attr *props,
250 struct ib_udata *uhw)
Eli Cohene126ba92013-07-07 17:25:49 +0300251{
252 struct mlx5_ib_dev *dev = to_mdev(ibdev);
Saeed Mahameed938fe832015-05-28 22:28:41 +0300253 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +0300254 int err = -ENOMEM;
255 int max_rq_sg;
256 int max_sq_sg;
Sagi Grimberge0238a62015-07-21 14:40:12 +0300257 u64 min_page_size = 1ull << MLX5_CAP_GEN(mdev, log_pg_sz);
Eli Cohene126ba92013-07-07 17:25:49 +0300258
Matan Barak2528e332015-06-11 16:35:25 +0300259 if (uhw->inlen || uhw->outlen)
260 return -EINVAL;
261
Eli Cohene126ba92013-07-07 17:25:49 +0300262 memset(props, 0, sizeof(*props));
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300263 err = mlx5_query_system_image_guid(ibdev,
264 &props->sys_image_guid);
265 if (err)
266 return err;
267
268 err = mlx5_query_max_pkeys(ibdev, &props->max_pkeys);
269 if (err)
270 return err;
271
272 err = mlx5_query_vendor_id(ibdev, &props->vendor_id);
273 if (err)
274 return err;
Eli Cohene126ba92013-07-07 17:25:49 +0300275
Jack Morgenstein9603b612014-07-28 23:30:22 +0300276 props->fw_ver = ((u64)fw_rev_maj(dev->mdev) << 32) |
277 (fw_rev_min(dev->mdev) << 16) |
278 fw_rev_sub(dev->mdev);
Eli Cohene126ba92013-07-07 17:25:49 +0300279 props->device_cap_flags = IB_DEVICE_CHANGE_PHY_PORT |
280 IB_DEVICE_PORT_ACTIVE_EVENT |
281 IB_DEVICE_SYS_IMAGE_GUID |
Eli Cohen1a4c3a32014-02-06 17:41:25 +0200282 IB_DEVICE_RC_RNR_NAK_GEN;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300283
284 if (MLX5_CAP_GEN(mdev, pkv))
Eli Cohene126ba92013-07-07 17:25:49 +0300285 props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300286 if (MLX5_CAP_GEN(mdev, qkv))
Eli Cohene126ba92013-07-07 17:25:49 +0300287 props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300288 if (MLX5_CAP_GEN(mdev, apm))
Eli Cohene126ba92013-07-07 17:25:49 +0300289 props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300290 if (MLX5_CAP_GEN(mdev, xrc))
Eli Cohene126ba92013-07-07 17:25:49 +0300291 props->device_cap_flags |= IB_DEVICE_XRC;
292 props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300293 if (MLX5_CAP_GEN(mdev, sho)) {
Sagi Grimberg2dea9092014-02-23 14:19:13 +0200294 props->device_cap_flags |= IB_DEVICE_SIGNATURE_HANDOVER;
295 /* At this stage no support for signature handover */
296 props->sig_prot_cap = IB_PROT_T10DIF_TYPE_1 |
297 IB_PROT_T10DIF_TYPE_2 |
298 IB_PROT_T10DIF_TYPE_3;
299 props->sig_guard_cap = IB_GUARD_T10DIF_CRC |
300 IB_GUARD_T10DIF_CSUM;
301 }
Saeed Mahameed938fe832015-05-28 22:28:41 +0300302 if (MLX5_CAP_GEN(mdev, block_lb_mc))
Eli Cohenf360d882014-04-02 00:10:16 +0300303 props->device_cap_flags |= IB_DEVICE_BLOCK_MULTICAST_LOOPBACK;
Eli Cohene126ba92013-07-07 17:25:49 +0300304
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300305 props->vendor_part_id = mdev->pdev->device;
306 props->hw_ver = mdev->pdev->revision;
Eli Cohene126ba92013-07-07 17:25:49 +0300307
308 props->max_mr_size = ~0ull;
Sagi Grimberge0238a62015-07-21 14:40:12 +0300309 props->page_size_cap = ~(min_page_size - 1);
Saeed Mahameed938fe832015-05-28 22:28:41 +0300310 props->max_qp = 1 << MLX5_CAP_GEN(mdev, log_max_qp);
311 props->max_qp_wr = 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
312 max_rq_sg = MLX5_CAP_GEN(mdev, max_wqe_sz_rq) /
313 sizeof(struct mlx5_wqe_data_seg);
314 max_sq_sg = (MLX5_CAP_GEN(mdev, max_wqe_sz_sq) -
315 sizeof(struct mlx5_wqe_ctrl_seg)) /
316 sizeof(struct mlx5_wqe_data_seg);
Eli Cohene126ba92013-07-07 17:25:49 +0300317 props->max_sge = min(max_rq_sg, max_sq_sg);
Sagi Grimberg18ebd402015-07-27 18:10:01 -0500318 props->max_sge_rd = props->max_sge;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300319 props->max_cq = 1 << MLX5_CAP_GEN(mdev, log_max_cq);
320 props->max_cqe = (1 << MLX5_CAP_GEN(mdev, log_max_eq_sz)) - 1;
321 props->max_mr = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
322 props->max_pd = 1 << MLX5_CAP_GEN(mdev, log_max_pd);
323 props->max_qp_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_req_qp);
324 props->max_qp_init_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_res_qp);
325 props->max_srq = 1 << MLX5_CAP_GEN(mdev, log_max_srq);
326 props->max_srq_wr = (1 << MLX5_CAP_GEN(mdev, log_max_srq_sz)) - 1;
327 props->local_ca_ack_delay = MLX5_CAP_GEN(mdev, local_ca_ack_delay);
Eli Cohene126ba92013-07-07 17:25:49 +0300328 props->max_res_rd_atom = props->max_qp_rd_atom * props->max_qp;
Eli Cohene126ba92013-07-07 17:25:49 +0300329 props->max_srq_sge = max_rq_sg - 1;
330 props->max_fast_reg_page_list_len = (unsigned int)-1;
Eli Cohen81bea282013-09-11 16:35:30 +0300331 props->atomic_cap = IB_ATOMIC_NONE;
332 props->masked_atomic_cap = IB_ATOMIC_NONE;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300333 props->max_mcast_grp = 1 << MLX5_CAP_GEN(mdev, log_max_mcg);
334 props->max_mcast_qp_attach = MLX5_CAP_GEN(mdev, max_qp_mcg);
Eli Cohene126ba92013-07-07 17:25:49 +0300335 props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
336 props->max_mcast_grp;
337 props->max_map_per_fmr = INT_MAX; /* no limit in ConnectIB */
338
Haggai Eran8cdd3122014-12-11 17:04:20 +0200339#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
Saeed Mahameed938fe832015-05-28 22:28:41 +0300340 if (MLX5_CAP_GEN(mdev, pg))
Haggai Eran8cdd3122014-12-11 17:04:20 +0200341 props->device_cap_flags |= IB_DEVICE_ON_DEMAND_PAGING;
342 props->odp_caps = dev->odp_caps;
343#endif
344
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300345 return 0;
346}
Eli Cohene126ba92013-07-07 17:25:49 +0300347
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300348enum mlx5_ib_width {
349 MLX5_IB_WIDTH_1X = 1 << 0,
350 MLX5_IB_WIDTH_2X = 1 << 1,
351 MLX5_IB_WIDTH_4X = 1 << 2,
352 MLX5_IB_WIDTH_8X = 1 << 3,
353 MLX5_IB_WIDTH_12X = 1 << 4
354};
355
356static int translate_active_width(struct ib_device *ibdev, u8 active_width,
357 u8 *ib_width)
358{
359 struct mlx5_ib_dev *dev = to_mdev(ibdev);
360 int err = 0;
361
362 if (active_width & MLX5_IB_WIDTH_1X) {
363 *ib_width = IB_WIDTH_1X;
364 } else if (active_width & MLX5_IB_WIDTH_2X) {
365 mlx5_ib_dbg(dev, "active_width %d is not supported by IB spec\n",
366 (int)active_width);
367 err = -EINVAL;
368 } else if (active_width & MLX5_IB_WIDTH_4X) {
369 *ib_width = IB_WIDTH_4X;
370 } else if (active_width & MLX5_IB_WIDTH_8X) {
371 *ib_width = IB_WIDTH_8X;
372 } else if (active_width & MLX5_IB_WIDTH_12X) {
373 *ib_width = IB_WIDTH_12X;
374 } else {
375 mlx5_ib_dbg(dev, "Invalid active_width %d\n",
376 (int)active_width);
377 err = -EINVAL;
378 }
379
380 return err;
381}
382
383static int mlx5_mtu_to_ib_mtu(int mtu)
384{
385 switch (mtu) {
386 case 256: return 1;
387 case 512: return 2;
388 case 1024: return 3;
389 case 2048: return 4;
390 case 4096: return 5;
391 default:
392 pr_warn("invalid mtu\n");
393 return -1;
394 }
395}
396
397enum ib_max_vl_num {
398 __IB_MAX_VL_0 = 1,
399 __IB_MAX_VL_0_1 = 2,
400 __IB_MAX_VL_0_3 = 3,
401 __IB_MAX_VL_0_7 = 4,
402 __IB_MAX_VL_0_14 = 5,
403};
404
405enum mlx5_vl_hw_cap {
406 MLX5_VL_HW_0 = 1,
407 MLX5_VL_HW_0_1 = 2,
408 MLX5_VL_HW_0_2 = 3,
409 MLX5_VL_HW_0_3 = 4,
410 MLX5_VL_HW_0_4 = 5,
411 MLX5_VL_HW_0_5 = 6,
412 MLX5_VL_HW_0_6 = 7,
413 MLX5_VL_HW_0_7 = 8,
414 MLX5_VL_HW_0_14 = 15
415};
416
417static int translate_max_vl_num(struct ib_device *ibdev, u8 vl_hw_cap,
418 u8 *max_vl_num)
419{
420 switch (vl_hw_cap) {
421 case MLX5_VL_HW_0:
422 *max_vl_num = __IB_MAX_VL_0;
423 break;
424 case MLX5_VL_HW_0_1:
425 *max_vl_num = __IB_MAX_VL_0_1;
426 break;
427 case MLX5_VL_HW_0_3:
428 *max_vl_num = __IB_MAX_VL_0_3;
429 break;
430 case MLX5_VL_HW_0_7:
431 *max_vl_num = __IB_MAX_VL_0_7;
432 break;
433 case MLX5_VL_HW_0_14:
434 *max_vl_num = __IB_MAX_VL_0_14;
435 break;
436
437 default:
438 return -EINVAL;
439 }
440
441 return 0;
442}
443
444static int mlx5_query_hca_port(struct ib_device *ibdev, u8 port,
445 struct ib_port_attr *props)
446{
447 struct mlx5_ib_dev *dev = to_mdev(ibdev);
448 struct mlx5_core_dev *mdev = dev->mdev;
449 struct mlx5_hca_vport_context *rep;
450 int max_mtu;
451 int oper_mtu;
452 int err;
453 u8 ib_link_width_oper;
454 u8 vl_hw_cap;
455
456 rep = kzalloc(sizeof(*rep), GFP_KERNEL);
457 if (!rep) {
458 err = -ENOMEM;
459 goto out;
460 }
461
462 memset(props, 0, sizeof(*props));
463
464 err = mlx5_query_hca_vport_context(mdev, 0, port, 0, rep);
465 if (err)
466 goto out;
467
468 props->lid = rep->lid;
469 props->lmc = rep->lmc;
470 props->sm_lid = rep->sm_lid;
471 props->sm_sl = rep->sm_sl;
472 props->state = rep->vport_state;
473 props->phys_state = rep->port_physical_state;
474 props->port_cap_flags = rep->cap_mask1;
475 props->gid_tbl_len = mlx5_get_gid_table_len(MLX5_CAP_GEN(mdev, gid_table_size));
476 props->max_msg_sz = 1 << MLX5_CAP_GEN(mdev, log_max_msg);
477 props->pkey_tbl_len = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, pkey_table_size));
478 props->bad_pkey_cntr = rep->pkey_violation_counter;
479 props->qkey_viol_cntr = rep->qkey_violation_counter;
480 props->subnet_timeout = rep->subnet_timeout;
481 props->init_type_reply = rep->init_type_reply;
482
483 err = mlx5_query_port_link_width_oper(mdev, &ib_link_width_oper, port);
484 if (err)
485 goto out;
486
487 err = translate_active_width(ibdev, ib_link_width_oper,
488 &props->active_width);
489 if (err)
490 goto out;
491 err = mlx5_query_port_proto_oper(mdev, &props->active_speed, MLX5_PTYS_IB,
492 port);
493 if (err)
494 goto out;
495
Saeed Mahameedfacc9692015-06-11 14:47:27 +0300496 mlx5_query_port_max_mtu(mdev, &max_mtu, port);
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300497
498 props->max_mtu = mlx5_mtu_to_ib_mtu(max_mtu);
499
Saeed Mahameedfacc9692015-06-11 14:47:27 +0300500 mlx5_query_port_oper_mtu(mdev, &oper_mtu, port);
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300501
502 props->active_mtu = mlx5_mtu_to_ib_mtu(oper_mtu);
503
504 err = mlx5_query_port_vl_hw_cap(mdev, &vl_hw_cap, port);
505 if (err)
506 goto out;
507
508 err = translate_max_vl_num(ibdev, vl_hw_cap,
509 &props->max_vl_num);
510out:
511 kfree(rep);
Eli Cohene126ba92013-07-07 17:25:49 +0300512 return err;
513}
514
515int mlx5_ib_query_port(struct ib_device *ibdev, u8 port,
516 struct ib_port_attr *props)
517{
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300518 switch (mlx5_get_vport_access_method(ibdev)) {
519 case MLX5_VPORT_ACCESS_METHOD_MAD:
520 return mlx5_query_mad_ifc_port(ibdev, port, props);
Eli Cohene126ba92013-07-07 17:25:49 +0300521
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300522 case MLX5_VPORT_ACCESS_METHOD_HCA:
523 return mlx5_query_hca_port(ibdev, port, props);
524
525 default:
Eli Cohene126ba92013-07-07 17:25:49 +0300526 return -EINVAL;
527 }
Eli Cohene126ba92013-07-07 17:25:49 +0300528}
529
530static int mlx5_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
531 union ib_gid *gid)
532{
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300533 struct mlx5_ib_dev *dev = to_mdev(ibdev);
534 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +0300535
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300536 switch (mlx5_get_vport_access_method(ibdev)) {
537 case MLX5_VPORT_ACCESS_METHOD_MAD:
538 return mlx5_query_mad_ifc_gids(ibdev, port, index, gid);
Eli Cohene126ba92013-07-07 17:25:49 +0300539
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300540 case MLX5_VPORT_ACCESS_METHOD_HCA:
541 return mlx5_query_hca_vport_gid(mdev, 0, port, 0, index, gid);
Eli Cohene126ba92013-07-07 17:25:49 +0300542
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300543 default:
544 return -EINVAL;
545 }
Eli Cohene126ba92013-07-07 17:25:49 +0300546
Eli Cohene126ba92013-07-07 17:25:49 +0300547}
548
549static int mlx5_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
550 u16 *pkey)
551{
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300552 struct mlx5_ib_dev *dev = to_mdev(ibdev);
553 struct mlx5_core_dev *mdev = dev->mdev;
Eli Cohene126ba92013-07-07 17:25:49 +0300554
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300555 switch (mlx5_get_vport_access_method(ibdev)) {
556 case MLX5_VPORT_ACCESS_METHOD_MAD:
557 return mlx5_query_mad_ifc_pkey(ibdev, port, index, pkey);
Eli Cohene126ba92013-07-07 17:25:49 +0300558
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300559 case MLX5_VPORT_ACCESS_METHOD_HCA:
560 case MLX5_VPORT_ACCESS_METHOD_NIC:
561 return mlx5_query_hca_vport_pkey(mdev, 0, port, 0, index,
562 pkey);
563 default:
564 return -EINVAL;
565 }
Eli Cohene126ba92013-07-07 17:25:49 +0300566}
567
Eli Cohene126ba92013-07-07 17:25:49 +0300568static int mlx5_ib_modify_device(struct ib_device *ibdev, int mask,
569 struct ib_device_modify *props)
570{
571 struct mlx5_ib_dev *dev = to_mdev(ibdev);
572 struct mlx5_reg_node_desc in;
573 struct mlx5_reg_node_desc out;
574 int err;
575
576 if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
577 return -EOPNOTSUPP;
578
579 if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
580 return 0;
581
582 /*
583 * If possible, pass node desc to FW, so it can generate
584 * a 144 trap. If cmd fails, just ignore.
585 */
586 memcpy(&in, props->node_desc, 64);
Jack Morgenstein9603b612014-07-28 23:30:22 +0300587 err = mlx5_core_access_reg(dev->mdev, &in, sizeof(in), &out,
Eli Cohene126ba92013-07-07 17:25:49 +0300588 sizeof(out), MLX5_REG_NODE_DESC, 0, 1);
589 if (err)
590 return err;
591
592 memcpy(ibdev->node_desc, props->node_desc, 64);
593
594 return err;
595}
596
597static int mlx5_ib_modify_port(struct ib_device *ibdev, u8 port, int mask,
598 struct ib_port_modify *props)
599{
600 struct mlx5_ib_dev *dev = to_mdev(ibdev);
601 struct ib_port_attr attr;
602 u32 tmp;
603 int err;
604
605 mutex_lock(&dev->cap_mask_mutex);
606
607 err = mlx5_ib_query_port(ibdev, port, &attr);
608 if (err)
609 goto out;
610
611 tmp = (attr.port_cap_flags | props->set_port_cap_mask) &
612 ~props->clr_port_cap_mask;
613
Jack Morgenstein9603b612014-07-28 23:30:22 +0300614 err = mlx5_set_port_caps(dev->mdev, port, tmp);
Eli Cohene126ba92013-07-07 17:25:49 +0300615
616out:
617 mutex_unlock(&dev->cap_mask_mutex);
618 return err;
619}
620
621static struct ib_ucontext *mlx5_ib_alloc_ucontext(struct ib_device *ibdev,
622 struct ib_udata *udata)
623{
624 struct mlx5_ib_dev *dev = to_mdev(ibdev);
Eli Cohen78c0f982014-01-30 13:49:48 +0200625 struct mlx5_ib_alloc_ucontext_req_v2 req;
Eli Cohene126ba92013-07-07 17:25:49 +0300626 struct mlx5_ib_alloc_ucontext_resp resp;
627 struct mlx5_ib_ucontext *context;
628 struct mlx5_uuar_info *uuari;
629 struct mlx5_uar *uars;
Eli Cohenc1be5232014-01-14 17:45:12 +0200630 int gross_uuars;
Eli Cohene126ba92013-07-07 17:25:49 +0300631 int num_uars;
Eli Cohen78c0f982014-01-30 13:49:48 +0200632 int ver;
Eli Cohene126ba92013-07-07 17:25:49 +0300633 int uuarn;
634 int err;
635 int i;
Jack Morgensteinf241e742014-07-28 23:30:23 +0300636 size_t reqlen;
Eli Cohene126ba92013-07-07 17:25:49 +0300637
638 if (!dev->ib_active)
639 return ERR_PTR(-EAGAIN);
640
Eli Cohen78c0f982014-01-30 13:49:48 +0200641 memset(&req, 0, sizeof(req));
642 reqlen = udata->inlen - sizeof(struct ib_uverbs_cmd_hdr);
643 if (reqlen == sizeof(struct mlx5_ib_alloc_ucontext_req))
644 ver = 0;
645 else if (reqlen == sizeof(struct mlx5_ib_alloc_ucontext_req_v2))
646 ver = 2;
647 else
648 return ERR_PTR(-EINVAL);
649
650 err = ib_copy_from_udata(&req, udata, reqlen);
Eli Cohene126ba92013-07-07 17:25:49 +0300651 if (err)
652 return ERR_PTR(err);
653
Eli Cohen78c0f982014-01-30 13:49:48 +0200654 if (req.flags || req.reserved)
655 return ERR_PTR(-EINVAL);
656
Eli Cohene126ba92013-07-07 17:25:49 +0300657 if (req.total_num_uuars > MLX5_MAX_UUARS)
658 return ERR_PTR(-ENOMEM);
659
660 if (req.total_num_uuars == 0)
661 return ERR_PTR(-EINVAL);
662
Eli Cohenc1be5232014-01-14 17:45:12 +0200663 req.total_num_uuars = ALIGN(req.total_num_uuars,
664 MLX5_NON_FP_BF_REGS_PER_PAGE);
Eli Cohene126ba92013-07-07 17:25:49 +0300665 if (req.num_low_latency_uuars > req.total_num_uuars - 1)
666 return ERR_PTR(-EINVAL);
667
Eli Cohenc1be5232014-01-14 17:45:12 +0200668 num_uars = req.total_num_uuars / MLX5_NON_FP_BF_REGS_PER_PAGE;
669 gross_uuars = num_uars * MLX5_BF_REGS_PER_PAGE;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300670 resp.qp_tab_size = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp);
671 resp.bf_reg_size = 1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size);
672 resp.cache_line_size = L1_CACHE_BYTES;
673 resp.max_sq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq);
674 resp.max_rq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq);
675 resp.max_send_wqebb = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
676 resp.max_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
677 resp.max_srq_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz);
Eli Cohene126ba92013-07-07 17:25:49 +0300678
679 context = kzalloc(sizeof(*context), GFP_KERNEL);
680 if (!context)
681 return ERR_PTR(-ENOMEM);
682
683 uuari = &context->uuari;
684 mutex_init(&uuari->lock);
685 uars = kcalloc(num_uars, sizeof(*uars), GFP_KERNEL);
686 if (!uars) {
687 err = -ENOMEM;
688 goto out_ctx;
689 }
690
Eli Cohenc1be5232014-01-14 17:45:12 +0200691 uuari->bitmap = kcalloc(BITS_TO_LONGS(gross_uuars),
Eli Cohene126ba92013-07-07 17:25:49 +0300692 sizeof(*uuari->bitmap),
693 GFP_KERNEL);
694 if (!uuari->bitmap) {
695 err = -ENOMEM;
696 goto out_uar_ctx;
697 }
698 /*
699 * clear all fast path uuars
700 */
Eli Cohenc1be5232014-01-14 17:45:12 +0200701 for (i = 0; i < gross_uuars; i++) {
Eli Cohene126ba92013-07-07 17:25:49 +0300702 uuarn = i & 3;
703 if (uuarn == 2 || uuarn == 3)
704 set_bit(i, uuari->bitmap);
705 }
706
Eli Cohenc1be5232014-01-14 17:45:12 +0200707 uuari->count = kcalloc(gross_uuars, sizeof(*uuari->count), GFP_KERNEL);
Eli Cohene126ba92013-07-07 17:25:49 +0300708 if (!uuari->count) {
709 err = -ENOMEM;
710 goto out_bitmap;
711 }
712
713 for (i = 0; i < num_uars; i++) {
Jack Morgenstein9603b612014-07-28 23:30:22 +0300714 err = mlx5_cmd_alloc_uar(dev->mdev, &uars[i].index);
Eli Cohene126ba92013-07-07 17:25:49 +0300715 if (err)
716 goto out_count;
717 }
718
Haggai Eranb4cfe442014-12-11 17:04:26 +0200719#ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
720 context->ibucontext.invalidate_range = &mlx5_ib_invalidate_range;
721#endif
722
Eli Cohene126ba92013-07-07 17:25:49 +0300723 INIT_LIST_HEAD(&context->db_page_list);
724 mutex_init(&context->db_page_mutex);
725
726 resp.tot_uuars = req.total_num_uuars;
Saeed Mahameed938fe832015-05-28 22:28:41 +0300727 resp.num_ports = MLX5_CAP_GEN(dev->mdev, num_ports);
Dan Carpenter92b0ca72013-07-25 20:04:36 +0300728 err = ib_copy_to_udata(udata, &resp,
729 sizeof(resp) - sizeof(resp.reserved));
Eli Cohene126ba92013-07-07 17:25:49 +0300730 if (err)
731 goto out_uars;
732
Eli Cohen78c0f982014-01-30 13:49:48 +0200733 uuari->ver = ver;
Eli Cohene126ba92013-07-07 17:25:49 +0300734 uuari->num_low_latency_uuars = req.num_low_latency_uuars;
735 uuari->uars = uars;
736 uuari->num_uars = num_uars;
737 return &context->ibucontext;
738
739out_uars:
740 for (i--; i >= 0; i--)
Jack Morgenstein9603b612014-07-28 23:30:22 +0300741 mlx5_cmd_free_uar(dev->mdev, uars[i].index);
Eli Cohene126ba92013-07-07 17:25:49 +0300742out_count:
743 kfree(uuari->count);
744
745out_bitmap:
746 kfree(uuari->bitmap);
747
748out_uar_ctx:
749 kfree(uars);
750
751out_ctx:
752 kfree(context);
753 return ERR_PTR(err);
754}
755
756static int mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
757{
758 struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
759 struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
760 struct mlx5_uuar_info *uuari = &context->uuari;
761 int i;
762
763 for (i = 0; i < uuari->num_uars; i++) {
Jack Morgenstein9603b612014-07-28 23:30:22 +0300764 if (mlx5_cmd_free_uar(dev->mdev, uuari->uars[i].index))
Eli Cohene126ba92013-07-07 17:25:49 +0300765 mlx5_ib_warn(dev, "failed to free UAR 0x%x\n", uuari->uars[i].index);
766 }
767
768 kfree(uuari->count);
769 kfree(uuari->bitmap);
770 kfree(uuari->uars);
771 kfree(context);
772
773 return 0;
774}
775
776static phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev, int index)
777{
Jack Morgenstein9603b612014-07-28 23:30:22 +0300778 return (pci_resource_start(dev->mdev->pdev, 0) >> PAGE_SHIFT) + index;
Eli Cohene126ba92013-07-07 17:25:49 +0300779}
780
781static int get_command(unsigned long offset)
782{
783 return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK;
784}
785
786static int get_arg(unsigned long offset)
787{
788 return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1);
789}
790
791static int get_index(unsigned long offset)
792{
793 return get_arg(offset);
794}
795
796static int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma)
797{
798 struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
799 struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
800 struct mlx5_uuar_info *uuari = &context->uuari;
801 unsigned long command;
802 unsigned long idx;
803 phys_addr_t pfn;
804
805 command = get_command(vma->vm_pgoff);
806 switch (command) {
807 case MLX5_IB_MMAP_REGULAR_PAGE:
808 if (vma->vm_end - vma->vm_start != PAGE_SIZE)
809 return -EINVAL;
810
811 idx = get_index(vma->vm_pgoff);
Eli Cohen1c3ce902014-09-14 16:47:53 +0300812 if (idx >= uuari->num_uars)
813 return -EINVAL;
814
Eli Cohene126ba92013-07-07 17:25:49 +0300815 pfn = uar_index2pfn(dev, uuari->uars[idx].index);
816 mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn 0x%llx\n", idx,
817 (unsigned long long)pfn);
818
Eli Cohene126ba92013-07-07 17:25:49 +0300819 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
820 if (io_remap_pfn_range(vma, vma->vm_start, pfn,
821 PAGE_SIZE, vma->vm_page_prot))
822 return -EAGAIN;
823
824 mlx5_ib_dbg(dev, "mapped WC at 0x%lx, PA 0x%llx\n",
825 vma->vm_start,
826 (unsigned long long)pfn << PAGE_SHIFT);
827 break;
828
829 case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES:
830 return -ENOSYS;
831
832 default:
833 return -EINVAL;
834 }
835
836 return 0;
837}
838
Eli Cohene126ba92013-07-07 17:25:49 +0300839static struct ib_pd *mlx5_ib_alloc_pd(struct ib_device *ibdev,
840 struct ib_ucontext *context,
841 struct ib_udata *udata)
842{
843 struct mlx5_ib_alloc_pd_resp resp;
844 struct mlx5_ib_pd *pd;
845 int err;
846
847 pd = kmalloc(sizeof(*pd), GFP_KERNEL);
848 if (!pd)
849 return ERR_PTR(-ENOMEM);
850
Jack Morgenstein9603b612014-07-28 23:30:22 +0300851 err = mlx5_core_alloc_pd(to_mdev(ibdev)->mdev, &pd->pdn);
Eli Cohene126ba92013-07-07 17:25:49 +0300852 if (err) {
853 kfree(pd);
854 return ERR_PTR(err);
855 }
856
857 if (context) {
858 resp.pdn = pd->pdn;
859 if (ib_copy_to_udata(udata, &resp, sizeof(resp))) {
Jack Morgenstein9603b612014-07-28 23:30:22 +0300860 mlx5_core_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn);
Eli Cohene126ba92013-07-07 17:25:49 +0300861 kfree(pd);
862 return ERR_PTR(-EFAULT);
863 }
Eli Cohene126ba92013-07-07 17:25:49 +0300864 }
865
866 return &pd->ibpd;
867}
868
869static int mlx5_ib_dealloc_pd(struct ib_pd *pd)
870{
871 struct mlx5_ib_dev *mdev = to_mdev(pd->device);
872 struct mlx5_ib_pd *mpd = to_mpd(pd);
873
Jack Morgenstein9603b612014-07-28 23:30:22 +0300874 mlx5_core_dealloc_pd(mdev->mdev, mpd->pdn);
Eli Cohene126ba92013-07-07 17:25:49 +0300875 kfree(mpd);
876
877 return 0;
878}
879
880static int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
881{
882 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
883 int err;
884
Jack Morgenstein9603b612014-07-28 23:30:22 +0300885 err = mlx5_core_attach_mcg(dev->mdev, gid, ibqp->qp_num);
Eli Cohene126ba92013-07-07 17:25:49 +0300886 if (err)
887 mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n",
888 ibqp->qp_num, gid->raw);
889
890 return err;
891}
892
893static int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
894{
895 struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
896 int err;
897
Jack Morgenstein9603b612014-07-28 23:30:22 +0300898 err = mlx5_core_detach_mcg(dev->mdev, gid, ibqp->qp_num);
Eli Cohene126ba92013-07-07 17:25:49 +0300899 if (err)
900 mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n",
901 ibqp->qp_num, gid->raw);
902
903 return err;
904}
905
906static int init_node_data(struct mlx5_ib_dev *dev)
907{
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300908 int err;
Eli Cohene126ba92013-07-07 17:25:49 +0300909
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300910 err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc);
Eli Cohene126ba92013-07-07 17:25:49 +0300911 if (err)
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300912 return err;
Eli Cohene126ba92013-07-07 17:25:49 +0300913
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300914 dev->mdev->rev_id = dev->mdev->pdev->revision;
Eli Cohene126ba92013-07-07 17:25:49 +0300915
Majd Dibbiny1b5daf12015-06-04 19:30:46 +0300916 return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid);
Eli Cohene126ba92013-07-07 17:25:49 +0300917}
918
919static ssize_t show_fw_pages(struct device *device, struct device_attribute *attr,
920 char *buf)
921{
922 struct mlx5_ib_dev *dev =
923 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
924
Jack Morgenstein9603b612014-07-28 23:30:22 +0300925 return sprintf(buf, "%d\n", dev->mdev->priv.fw_pages);
Eli Cohene126ba92013-07-07 17:25:49 +0300926}
927
928static ssize_t show_reg_pages(struct device *device,
929 struct device_attribute *attr, char *buf)
930{
931 struct mlx5_ib_dev *dev =
932 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
933
Haggai Eran6aec21f2014-12-11 17:04:23 +0200934 return sprintf(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages));
Eli Cohene126ba92013-07-07 17:25:49 +0300935}
936
937static ssize_t show_hca(struct device *device, struct device_attribute *attr,
938 char *buf)
939{
940 struct mlx5_ib_dev *dev =
941 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
Jack Morgenstein9603b612014-07-28 23:30:22 +0300942 return sprintf(buf, "MT%d\n", dev->mdev->pdev->device);
Eli Cohene126ba92013-07-07 17:25:49 +0300943}
944
945static ssize_t show_fw_ver(struct device *device, struct device_attribute *attr,
946 char *buf)
947{
948 struct mlx5_ib_dev *dev =
949 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
Jack Morgenstein9603b612014-07-28 23:30:22 +0300950 return sprintf(buf, "%d.%d.%d\n", fw_rev_maj(dev->mdev),
951 fw_rev_min(dev->mdev), fw_rev_sub(dev->mdev));
Eli Cohene126ba92013-07-07 17:25:49 +0300952}
953
954static ssize_t show_rev(struct device *device, struct device_attribute *attr,
955 char *buf)
956{
957 struct mlx5_ib_dev *dev =
958 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
Jack Morgenstein9603b612014-07-28 23:30:22 +0300959 return sprintf(buf, "%x\n", dev->mdev->rev_id);
Eli Cohene126ba92013-07-07 17:25:49 +0300960}
961
962static ssize_t show_board(struct device *device, struct device_attribute *attr,
963 char *buf)
964{
965 struct mlx5_ib_dev *dev =
966 container_of(device, struct mlx5_ib_dev, ib_dev.dev);
967 return sprintf(buf, "%.*s\n", MLX5_BOARD_ID_LEN,
Jack Morgenstein9603b612014-07-28 23:30:22 +0300968 dev->mdev->board_id);
Eli Cohene126ba92013-07-07 17:25:49 +0300969}
970
971static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
972static DEVICE_ATTR(fw_ver, S_IRUGO, show_fw_ver, NULL);
973static DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL);
974static DEVICE_ATTR(board_id, S_IRUGO, show_board, NULL);
975static DEVICE_ATTR(fw_pages, S_IRUGO, show_fw_pages, NULL);
976static DEVICE_ATTR(reg_pages, S_IRUGO, show_reg_pages, NULL);
977
978static struct device_attribute *mlx5_class_attributes[] = {
979 &dev_attr_hw_rev,
980 &dev_attr_fw_ver,
981 &dev_attr_hca_type,
982 &dev_attr_board_id,
983 &dev_attr_fw_pages,
984 &dev_attr_reg_pages,
985};
986
Jack Morgenstein9603b612014-07-28 23:30:22 +0300987static void mlx5_ib_event(struct mlx5_core_dev *dev, void *context,
Jack Morgenstein4d2f9bb2014-07-28 23:30:24 +0300988 enum mlx5_dev_event event, unsigned long param)
Eli Cohene126ba92013-07-07 17:25:49 +0300989{
Jack Morgenstein9603b612014-07-28 23:30:22 +0300990 struct mlx5_ib_dev *ibdev = (struct mlx5_ib_dev *)context;
Eli Cohene126ba92013-07-07 17:25:49 +0300991 struct ib_event ibev;
Jack Morgenstein9603b612014-07-28 23:30:22 +0300992
Eli Cohene126ba92013-07-07 17:25:49 +0300993 u8 port = 0;
994
995 switch (event) {
996 case MLX5_DEV_EVENT_SYS_ERROR:
997 ibdev->ib_active = false;
998 ibev.event = IB_EVENT_DEVICE_FATAL;
999 break;
1000
1001 case MLX5_DEV_EVENT_PORT_UP:
1002 ibev.event = IB_EVENT_PORT_ACTIVE;
Jack Morgenstein4d2f9bb2014-07-28 23:30:24 +03001003 port = (u8)param;
Eli Cohene126ba92013-07-07 17:25:49 +03001004 break;
1005
1006 case MLX5_DEV_EVENT_PORT_DOWN:
1007 ibev.event = IB_EVENT_PORT_ERR;
Jack Morgenstein4d2f9bb2014-07-28 23:30:24 +03001008 port = (u8)param;
Eli Cohene126ba92013-07-07 17:25:49 +03001009 break;
1010
1011 case MLX5_DEV_EVENT_PORT_INITIALIZED:
1012 /* not used by ULPs */
1013 return;
1014
1015 case MLX5_DEV_EVENT_LID_CHANGE:
1016 ibev.event = IB_EVENT_LID_CHANGE;
Jack Morgenstein4d2f9bb2014-07-28 23:30:24 +03001017 port = (u8)param;
Eli Cohene126ba92013-07-07 17:25:49 +03001018 break;
1019
1020 case MLX5_DEV_EVENT_PKEY_CHANGE:
1021 ibev.event = IB_EVENT_PKEY_CHANGE;
Jack Morgenstein4d2f9bb2014-07-28 23:30:24 +03001022 port = (u8)param;
Eli Cohene126ba92013-07-07 17:25:49 +03001023 break;
1024
1025 case MLX5_DEV_EVENT_GUID_CHANGE:
1026 ibev.event = IB_EVENT_GID_CHANGE;
Jack Morgenstein4d2f9bb2014-07-28 23:30:24 +03001027 port = (u8)param;
Eli Cohene126ba92013-07-07 17:25:49 +03001028 break;
1029
1030 case MLX5_DEV_EVENT_CLIENT_REREG:
1031 ibev.event = IB_EVENT_CLIENT_REREGISTER;
Jack Morgenstein4d2f9bb2014-07-28 23:30:24 +03001032 port = (u8)param;
Eli Cohene126ba92013-07-07 17:25:49 +03001033 break;
1034 }
1035
1036 ibev.device = &ibdev->ib_dev;
1037 ibev.element.port_num = port;
1038
Eli Cohena0c84c32013-09-11 16:35:27 +03001039 if (port < 1 || port > ibdev->num_ports) {
1040 mlx5_ib_warn(ibdev, "warning: event on port %d\n", port);
1041 return;
1042 }
1043
Eli Cohene126ba92013-07-07 17:25:49 +03001044 if (ibdev->ib_active)
1045 ib_dispatch_event(&ibev);
1046}
1047
1048static void get_ext_port_caps(struct mlx5_ib_dev *dev)
1049{
1050 int port;
1051
Saeed Mahameed938fe832015-05-28 22:28:41 +03001052 for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++)
Eli Cohene126ba92013-07-07 17:25:49 +03001053 mlx5_query_ext_port_caps(dev, port);
1054}
1055
1056static int get_port_caps(struct mlx5_ib_dev *dev)
1057{
1058 struct ib_device_attr *dprops = NULL;
1059 struct ib_port_attr *pprops = NULL;
Dan Carpenterf614fc12015-01-12 11:56:58 +03001060 int err = -ENOMEM;
Eli Cohene126ba92013-07-07 17:25:49 +03001061 int port;
Matan Barak2528e332015-06-11 16:35:25 +03001062 struct ib_udata uhw = {.inlen = 0, .outlen = 0};
Eli Cohene126ba92013-07-07 17:25:49 +03001063
1064 pprops = kmalloc(sizeof(*pprops), GFP_KERNEL);
1065 if (!pprops)
1066 goto out;
1067
1068 dprops = kmalloc(sizeof(*dprops), GFP_KERNEL);
1069 if (!dprops)
1070 goto out;
1071
Matan Barak2528e332015-06-11 16:35:25 +03001072 err = mlx5_ib_query_device(&dev->ib_dev, dprops, &uhw);
Eli Cohene126ba92013-07-07 17:25:49 +03001073 if (err) {
1074 mlx5_ib_warn(dev, "query_device failed %d\n", err);
1075 goto out;
1076 }
1077
Saeed Mahameed938fe832015-05-28 22:28:41 +03001078 for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++) {
Eli Cohene126ba92013-07-07 17:25:49 +03001079 err = mlx5_ib_query_port(&dev->ib_dev, port, pprops);
1080 if (err) {
Saeed Mahameed938fe832015-05-28 22:28:41 +03001081 mlx5_ib_warn(dev, "query_port %d failed %d\n",
1082 port, err);
Eli Cohene126ba92013-07-07 17:25:49 +03001083 break;
1084 }
Saeed Mahameed938fe832015-05-28 22:28:41 +03001085 dev->mdev->port_caps[port - 1].pkey_table_len =
1086 dprops->max_pkeys;
1087 dev->mdev->port_caps[port - 1].gid_table_len =
1088 pprops->gid_tbl_len;
Eli Cohene126ba92013-07-07 17:25:49 +03001089 mlx5_ib_dbg(dev, "pkey_table_len %d, gid_table_len %d\n",
1090 dprops->max_pkeys, pprops->gid_tbl_len);
1091 }
1092
1093out:
1094 kfree(pprops);
1095 kfree(dprops);
1096
1097 return err;
1098}
1099
1100static void destroy_umrc_res(struct mlx5_ib_dev *dev)
1101{
1102 int err;
1103
1104 err = mlx5_mr_cache_cleanup(dev);
1105 if (err)
1106 mlx5_ib_warn(dev, "mr cache cleanup failed\n");
1107
1108 mlx5_ib_destroy_qp(dev->umrc.qp);
1109 ib_destroy_cq(dev->umrc.cq);
Eli Cohene126ba92013-07-07 17:25:49 +03001110 ib_dealloc_pd(dev->umrc.pd);
1111}
1112
1113enum {
1114 MAX_UMR_WR = 128,
1115};
1116
1117static int create_umr_res(struct mlx5_ib_dev *dev)
1118{
1119 struct ib_qp_init_attr *init_attr = NULL;
1120 struct ib_qp_attr *attr = NULL;
1121 struct ib_pd *pd;
1122 struct ib_cq *cq;
1123 struct ib_qp *qp;
Matan Barak8e372102015-06-11 16:35:21 +03001124 struct ib_cq_init_attr cq_attr = {};
Eli Cohene126ba92013-07-07 17:25:49 +03001125 int ret;
1126
1127 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
1128 init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL);
1129 if (!attr || !init_attr) {
1130 ret = -ENOMEM;
1131 goto error_0;
1132 }
1133
1134 pd = ib_alloc_pd(&dev->ib_dev);
1135 if (IS_ERR(pd)) {
1136 mlx5_ib_dbg(dev, "Couldn't create PD for sync UMR QP\n");
1137 ret = PTR_ERR(pd);
1138 goto error_0;
1139 }
1140
Matan Barak8e372102015-06-11 16:35:21 +03001141 cq_attr.cqe = 128;
1142 cq = ib_create_cq(&dev->ib_dev, mlx5_umr_cq_handler, NULL, NULL,
1143 &cq_attr);
Eli Cohene126ba92013-07-07 17:25:49 +03001144 if (IS_ERR(cq)) {
1145 mlx5_ib_dbg(dev, "Couldn't create CQ for sync UMR QP\n");
1146 ret = PTR_ERR(cq);
1147 goto error_2;
1148 }
1149 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
1150
1151 init_attr->send_cq = cq;
1152 init_attr->recv_cq = cq;
1153 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
1154 init_attr->cap.max_send_wr = MAX_UMR_WR;
1155 init_attr->cap.max_send_sge = 1;
1156 init_attr->qp_type = MLX5_IB_QPT_REG_UMR;
1157 init_attr->port_num = 1;
1158 qp = mlx5_ib_create_qp(pd, init_attr, NULL);
1159 if (IS_ERR(qp)) {
1160 mlx5_ib_dbg(dev, "Couldn't create sync UMR QP\n");
1161 ret = PTR_ERR(qp);
1162 goto error_3;
1163 }
1164 qp->device = &dev->ib_dev;
1165 qp->real_qp = qp;
1166 qp->uobject = NULL;
1167 qp->qp_type = MLX5_IB_QPT_REG_UMR;
1168
1169 attr->qp_state = IB_QPS_INIT;
1170 attr->port_num = 1;
1171 ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_PKEY_INDEX |
1172 IB_QP_PORT, NULL);
1173 if (ret) {
1174 mlx5_ib_dbg(dev, "Couldn't modify UMR QP\n");
1175 goto error_4;
1176 }
1177
1178 memset(attr, 0, sizeof(*attr));
1179 attr->qp_state = IB_QPS_RTR;
1180 attr->path_mtu = IB_MTU_256;
1181
1182 ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
1183 if (ret) {
1184 mlx5_ib_dbg(dev, "Couldn't modify umr QP to rtr\n");
1185 goto error_4;
1186 }
1187
1188 memset(attr, 0, sizeof(*attr));
1189 attr->qp_state = IB_QPS_RTS;
1190 ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
1191 if (ret) {
1192 mlx5_ib_dbg(dev, "Couldn't modify umr QP to rts\n");
1193 goto error_4;
1194 }
1195
1196 dev->umrc.qp = qp;
1197 dev->umrc.cq = cq;
Eli Cohene126ba92013-07-07 17:25:49 +03001198 dev->umrc.pd = pd;
1199
1200 sema_init(&dev->umrc.sem, MAX_UMR_WR);
1201 ret = mlx5_mr_cache_init(dev);
1202 if (ret) {
1203 mlx5_ib_warn(dev, "mr cache init failed %d\n", ret);
1204 goto error_4;
1205 }
1206
1207 kfree(attr);
1208 kfree(init_attr);
1209
1210 return 0;
1211
1212error_4:
1213 mlx5_ib_destroy_qp(qp);
1214
1215error_3:
1216 ib_destroy_cq(cq);
1217
1218error_2:
Eli Cohene126ba92013-07-07 17:25:49 +03001219 ib_dealloc_pd(pd);
1220
1221error_0:
1222 kfree(attr);
1223 kfree(init_attr);
1224 return ret;
1225}
1226
1227static int create_dev_resources(struct mlx5_ib_resources *devr)
1228{
1229 struct ib_srq_init_attr attr;
1230 struct mlx5_ib_dev *dev;
Matan Barakbcf4c1e2015-06-11 16:35:20 +03001231 struct ib_cq_init_attr cq_attr = {.cqe = 1};
Eli Cohene126ba92013-07-07 17:25:49 +03001232 int ret = 0;
1233
1234 dev = container_of(devr, struct mlx5_ib_dev, devr);
1235
1236 devr->p0 = mlx5_ib_alloc_pd(&dev->ib_dev, NULL, NULL);
1237 if (IS_ERR(devr->p0)) {
1238 ret = PTR_ERR(devr->p0);
1239 goto error0;
1240 }
1241 devr->p0->device = &dev->ib_dev;
1242 devr->p0->uobject = NULL;
1243 atomic_set(&devr->p0->usecnt, 0);
1244
Matan Barakbcf4c1e2015-06-11 16:35:20 +03001245 devr->c0 = mlx5_ib_create_cq(&dev->ib_dev, &cq_attr, NULL, NULL);
Eli Cohene126ba92013-07-07 17:25:49 +03001246 if (IS_ERR(devr->c0)) {
1247 ret = PTR_ERR(devr->c0);
1248 goto error1;
1249 }
1250 devr->c0->device = &dev->ib_dev;
1251 devr->c0->uobject = NULL;
1252 devr->c0->comp_handler = NULL;
1253 devr->c0->event_handler = NULL;
1254 devr->c0->cq_context = NULL;
1255 atomic_set(&devr->c0->usecnt, 0);
1256
1257 devr->x0 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
1258 if (IS_ERR(devr->x0)) {
1259 ret = PTR_ERR(devr->x0);
1260 goto error2;
1261 }
1262 devr->x0->device = &dev->ib_dev;
1263 devr->x0->inode = NULL;
1264 atomic_set(&devr->x0->usecnt, 0);
1265 mutex_init(&devr->x0->tgt_qp_mutex);
1266 INIT_LIST_HEAD(&devr->x0->tgt_qp_list);
1267
1268 devr->x1 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
1269 if (IS_ERR(devr->x1)) {
1270 ret = PTR_ERR(devr->x1);
1271 goto error3;
1272 }
1273 devr->x1->device = &dev->ib_dev;
1274 devr->x1->inode = NULL;
1275 atomic_set(&devr->x1->usecnt, 0);
1276 mutex_init(&devr->x1->tgt_qp_mutex);
1277 INIT_LIST_HEAD(&devr->x1->tgt_qp_list);
1278
1279 memset(&attr, 0, sizeof(attr));
1280 attr.attr.max_sge = 1;
1281 attr.attr.max_wr = 1;
1282 attr.srq_type = IB_SRQT_XRC;
1283 attr.ext.xrc.cq = devr->c0;
1284 attr.ext.xrc.xrcd = devr->x0;
1285
1286 devr->s0 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
1287 if (IS_ERR(devr->s0)) {
1288 ret = PTR_ERR(devr->s0);
1289 goto error4;
1290 }
1291 devr->s0->device = &dev->ib_dev;
1292 devr->s0->pd = devr->p0;
1293 devr->s0->uobject = NULL;
1294 devr->s0->event_handler = NULL;
1295 devr->s0->srq_context = NULL;
1296 devr->s0->srq_type = IB_SRQT_XRC;
1297 devr->s0->ext.xrc.xrcd = devr->x0;
1298 devr->s0->ext.xrc.cq = devr->c0;
1299 atomic_inc(&devr->s0->ext.xrc.xrcd->usecnt);
1300 atomic_inc(&devr->s0->ext.xrc.cq->usecnt);
1301 atomic_inc(&devr->p0->usecnt);
1302 atomic_set(&devr->s0->usecnt, 0);
1303
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03001304 memset(&attr, 0, sizeof(attr));
1305 attr.attr.max_sge = 1;
1306 attr.attr.max_wr = 1;
1307 attr.srq_type = IB_SRQT_BASIC;
1308 devr->s1 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
1309 if (IS_ERR(devr->s1)) {
1310 ret = PTR_ERR(devr->s1);
1311 goto error5;
1312 }
1313 devr->s1->device = &dev->ib_dev;
1314 devr->s1->pd = devr->p0;
1315 devr->s1->uobject = NULL;
1316 devr->s1->event_handler = NULL;
1317 devr->s1->srq_context = NULL;
1318 devr->s1->srq_type = IB_SRQT_BASIC;
1319 devr->s1->ext.xrc.cq = devr->c0;
1320 atomic_inc(&devr->p0->usecnt);
1321 atomic_set(&devr->s0->usecnt, 0);
1322
Eli Cohene126ba92013-07-07 17:25:49 +03001323 return 0;
1324
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03001325error5:
1326 mlx5_ib_destroy_srq(devr->s0);
Eli Cohene126ba92013-07-07 17:25:49 +03001327error4:
1328 mlx5_ib_dealloc_xrcd(devr->x1);
1329error3:
1330 mlx5_ib_dealloc_xrcd(devr->x0);
1331error2:
1332 mlx5_ib_destroy_cq(devr->c0);
1333error1:
1334 mlx5_ib_dealloc_pd(devr->p0);
1335error0:
1336 return ret;
1337}
1338
1339static void destroy_dev_resources(struct mlx5_ib_resources *devr)
1340{
Haggai Abramonvsky4aa17b22015-06-04 19:30:48 +03001341 mlx5_ib_destroy_srq(devr->s1);
Eli Cohene126ba92013-07-07 17:25:49 +03001342 mlx5_ib_destroy_srq(devr->s0);
1343 mlx5_ib_dealloc_xrcd(devr->x0);
1344 mlx5_ib_dealloc_xrcd(devr->x1);
1345 mlx5_ib_destroy_cq(devr->c0);
1346 mlx5_ib_dealloc_pd(devr->p0);
1347}
1348
Ira Weiny77386132015-05-13 20:02:58 -04001349static int mlx5_port_immutable(struct ib_device *ibdev, u8 port_num,
1350 struct ib_port_immutable *immutable)
1351{
1352 struct ib_port_attr attr;
1353 int err;
1354
1355 err = mlx5_ib_query_port(ibdev, port_num, &attr);
1356 if (err)
1357 return err;
1358
1359 immutable->pkey_tbl_len = attr.pkey_tbl_len;
1360 immutable->gid_tbl_len = attr.gid_tbl_len;
Ira Weinyf9b22e32015-05-13 20:02:59 -04001361 immutable->core_cap_flags = RDMA_CORE_PORT_IBA_IB;
Ira Weiny337877a2015-06-06 14:38:29 -04001362 immutable->max_mad_size = IB_MGMT_MAD_SIZE;
Ira Weiny77386132015-05-13 20:02:58 -04001363
1364 return 0;
1365}
1366
Achiad Shochatfc24fc52015-12-23 18:47:17 +02001367static int mlx5_enable_roce(struct mlx5_ib_dev *dev)
1368{
1369 dev->roce.nb.notifier_call = mlx5_netdev_event;
1370 return register_netdevice_notifier(&dev->roce.nb);
1371}
1372
1373static void mlx5_disable_roce(struct mlx5_ib_dev *dev)
1374{
1375 unregister_netdevice_notifier(&dev->roce.nb);
1376}
1377
Jack Morgenstein9603b612014-07-28 23:30:22 +03001378static void *mlx5_ib_add(struct mlx5_core_dev *mdev)
Eli Cohene126ba92013-07-07 17:25:49 +03001379{
Eli Cohene126ba92013-07-07 17:25:49 +03001380 struct mlx5_ib_dev *dev;
Achiad Shochatebd61f62015-12-23 18:47:16 +02001381 enum rdma_link_layer ll;
1382 int port_type_cap;
Eli Cohene126ba92013-07-07 17:25:49 +03001383 int err;
1384 int i;
1385
Achiad Shochatebd61f62015-12-23 18:47:16 +02001386 port_type_cap = MLX5_CAP_GEN(mdev, port_type);
1387 ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
1388
Majd Dibbiny647241e2015-06-04 19:30:47 +03001389 /* don't create IB instance over Eth ports, no RoCE yet! */
Achiad Shochatebd61f62015-12-23 18:47:16 +02001390 if (ll == IB_LINK_LAYER_ETHERNET)
Majd Dibbiny647241e2015-06-04 19:30:47 +03001391 return NULL;
1392
Eli Cohene126ba92013-07-07 17:25:49 +03001393 printk_once(KERN_INFO "%s", mlx5_version);
1394
1395 dev = (struct mlx5_ib_dev *)ib_alloc_device(sizeof(*dev));
1396 if (!dev)
Jack Morgenstein9603b612014-07-28 23:30:22 +03001397 return NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03001398
Jack Morgenstein9603b612014-07-28 23:30:22 +03001399 dev->mdev = mdev;
Eli Cohene126ba92013-07-07 17:25:49 +03001400
Achiad Shochatfc24fc52015-12-23 18:47:17 +02001401 rwlock_init(&dev->roce.netdev_lock);
Eli Cohene126ba92013-07-07 17:25:49 +03001402 err = get_port_caps(dev);
1403 if (err)
Jack Morgenstein9603b612014-07-28 23:30:22 +03001404 goto err_dealloc;
Eli Cohene126ba92013-07-07 17:25:49 +03001405
Majd Dibbiny1b5daf12015-06-04 19:30:46 +03001406 if (mlx5_use_mad_ifc(dev))
1407 get_ext_port_caps(dev);
Eli Cohene126ba92013-07-07 17:25:49 +03001408
Eli Cohene126ba92013-07-07 17:25:49 +03001409 MLX5_INIT_DOORBELL_LOCK(&dev->uar_lock);
1410
1411 strlcpy(dev->ib_dev.name, "mlx5_%d", IB_DEVICE_NAME_MAX);
1412 dev->ib_dev.owner = THIS_MODULE;
1413 dev->ib_dev.node_type = RDMA_NODE_IB_CA;
Sagi Grimbergc6790aa2015-09-24 10:34:23 +03001414 dev->ib_dev.local_dma_lkey = 0 /* not supported for now */;
Saeed Mahameed938fe832015-05-28 22:28:41 +03001415 dev->num_ports = MLX5_CAP_GEN(mdev, num_ports);
Eli Cohene126ba92013-07-07 17:25:49 +03001416 dev->ib_dev.phys_port_cnt = dev->num_ports;
Saeed Mahameed233d05d2015-04-02 17:07:32 +03001417 dev->ib_dev.num_comp_vectors =
1418 dev->mdev->priv.eq_table.num_comp_vectors;
Eli Cohene126ba92013-07-07 17:25:49 +03001419 dev->ib_dev.dma_device = &mdev->pdev->dev;
1420
1421 dev->ib_dev.uverbs_abi_ver = MLX5_IB_UVERBS_ABI_VERSION;
1422 dev->ib_dev.uverbs_cmd_mask =
1423 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
1424 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
1425 (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
1426 (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
1427 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
1428 (1ull << IB_USER_VERBS_CMD_REG_MR) |
1429 (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
1430 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
1431 (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
1432 (1ull << IB_USER_VERBS_CMD_RESIZE_CQ) |
1433 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
1434 (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
1435 (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
1436 (1ull << IB_USER_VERBS_CMD_QUERY_QP) |
1437 (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
1438 (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST) |
1439 (1ull << IB_USER_VERBS_CMD_DETACH_MCAST) |
1440 (1ull << IB_USER_VERBS_CMD_CREATE_SRQ) |
1441 (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ) |
1442 (1ull << IB_USER_VERBS_CMD_QUERY_SRQ) |
1443 (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ) |
1444 (1ull << IB_USER_VERBS_CMD_CREATE_XSRQ) |
1445 (1ull << IB_USER_VERBS_CMD_OPEN_QP);
Haggai Eran1707cb42015-02-08 13:28:52 +02001446 dev->ib_dev.uverbs_ex_cmd_mask =
1447 (1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE);
Eli Cohene126ba92013-07-07 17:25:49 +03001448
1449 dev->ib_dev.query_device = mlx5_ib_query_device;
1450 dev->ib_dev.query_port = mlx5_ib_query_port;
Achiad Shochatebd61f62015-12-23 18:47:16 +02001451 dev->ib_dev.get_link_layer = mlx5_ib_port_link_layer;
Achiad Shochatfc24fc52015-12-23 18:47:17 +02001452 if (ll == IB_LINK_LAYER_ETHERNET)
1453 dev->ib_dev.get_netdev = mlx5_ib_get_netdev;
Eli Cohene126ba92013-07-07 17:25:49 +03001454 dev->ib_dev.query_gid = mlx5_ib_query_gid;
1455 dev->ib_dev.query_pkey = mlx5_ib_query_pkey;
1456 dev->ib_dev.modify_device = mlx5_ib_modify_device;
1457 dev->ib_dev.modify_port = mlx5_ib_modify_port;
1458 dev->ib_dev.alloc_ucontext = mlx5_ib_alloc_ucontext;
1459 dev->ib_dev.dealloc_ucontext = mlx5_ib_dealloc_ucontext;
1460 dev->ib_dev.mmap = mlx5_ib_mmap;
1461 dev->ib_dev.alloc_pd = mlx5_ib_alloc_pd;
1462 dev->ib_dev.dealloc_pd = mlx5_ib_dealloc_pd;
1463 dev->ib_dev.create_ah = mlx5_ib_create_ah;
1464 dev->ib_dev.query_ah = mlx5_ib_query_ah;
1465 dev->ib_dev.destroy_ah = mlx5_ib_destroy_ah;
1466 dev->ib_dev.create_srq = mlx5_ib_create_srq;
1467 dev->ib_dev.modify_srq = mlx5_ib_modify_srq;
1468 dev->ib_dev.query_srq = mlx5_ib_query_srq;
1469 dev->ib_dev.destroy_srq = mlx5_ib_destroy_srq;
1470 dev->ib_dev.post_srq_recv = mlx5_ib_post_srq_recv;
1471 dev->ib_dev.create_qp = mlx5_ib_create_qp;
1472 dev->ib_dev.modify_qp = mlx5_ib_modify_qp;
1473 dev->ib_dev.query_qp = mlx5_ib_query_qp;
1474 dev->ib_dev.destroy_qp = mlx5_ib_destroy_qp;
1475 dev->ib_dev.post_send = mlx5_ib_post_send;
1476 dev->ib_dev.post_recv = mlx5_ib_post_recv;
1477 dev->ib_dev.create_cq = mlx5_ib_create_cq;
1478 dev->ib_dev.modify_cq = mlx5_ib_modify_cq;
1479 dev->ib_dev.resize_cq = mlx5_ib_resize_cq;
1480 dev->ib_dev.destroy_cq = mlx5_ib_destroy_cq;
1481 dev->ib_dev.poll_cq = mlx5_ib_poll_cq;
1482 dev->ib_dev.req_notify_cq = mlx5_ib_arm_cq;
1483 dev->ib_dev.get_dma_mr = mlx5_ib_get_dma_mr;
1484 dev->ib_dev.reg_user_mr = mlx5_ib_reg_user_mr;
1485 dev->ib_dev.dereg_mr = mlx5_ib_dereg_mr;
1486 dev->ib_dev.attach_mcast = mlx5_ib_mcg_attach;
1487 dev->ib_dev.detach_mcast = mlx5_ib_mcg_detach;
1488 dev->ib_dev.process_mad = mlx5_ib_process_mad;
Sagi Grimberg9bee1782015-07-30 10:32:35 +03001489 dev->ib_dev.alloc_mr = mlx5_ib_alloc_mr;
Sagi Grimberg8a187ee2015-10-13 19:11:26 +03001490 dev->ib_dev.map_mr_sg = mlx5_ib_map_mr_sg;
Sagi Grimbergd5436ba2014-02-23 14:19:12 +02001491 dev->ib_dev.check_mr_status = mlx5_ib_check_mr_status;
Ira Weiny77386132015-05-13 20:02:58 -04001492 dev->ib_dev.get_port_immutable = mlx5_port_immutable;
Eli Cohene126ba92013-07-07 17:25:49 +03001493
Saeed Mahameed938fe832015-05-28 22:28:41 +03001494 mlx5_ib_internal_fill_odp_caps(dev);
Haggai Eran8cdd3122014-12-11 17:04:20 +02001495
Saeed Mahameed938fe832015-05-28 22:28:41 +03001496 if (MLX5_CAP_GEN(mdev, xrc)) {
Eli Cohene126ba92013-07-07 17:25:49 +03001497 dev->ib_dev.alloc_xrcd = mlx5_ib_alloc_xrcd;
1498 dev->ib_dev.dealloc_xrcd = mlx5_ib_dealloc_xrcd;
1499 dev->ib_dev.uverbs_cmd_mask |=
1500 (1ull << IB_USER_VERBS_CMD_OPEN_XRCD) |
1501 (1ull << IB_USER_VERBS_CMD_CLOSE_XRCD);
1502 }
1503
1504 err = init_node_data(dev);
1505 if (err)
Saeed Mahameed233d05d2015-04-02 17:07:32 +03001506 goto err_dealloc;
Eli Cohene126ba92013-07-07 17:25:49 +03001507
1508 mutex_init(&dev->cap_mask_mutex);
Eli Cohene126ba92013-07-07 17:25:49 +03001509
Achiad Shochatfc24fc52015-12-23 18:47:17 +02001510 if (ll == IB_LINK_LAYER_ETHERNET) {
1511 err = mlx5_enable_roce(dev);
1512 if (err)
1513 goto err_dealloc;
1514 }
1515
Eli Cohene126ba92013-07-07 17:25:49 +03001516 err = create_dev_resources(&dev->devr);
1517 if (err)
Achiad Shochatfc24fc52015-12-23 18:47:17 +02001518 goto err_disable_roce;
Eli Cohene126ba92013-07-07 17:25:49 +03001519
Haggai Eran6aec21f2014-12-11 17:04:23 +02001520 err = mlx5_ib_odp_init_one(dev);
Wei Yongjun281d1a92013-07-30 07:54:26 +08001521 if (err)
Eli Cohene126ba92013-07-07 17:25:49 +03001522 goto err_rsrc;
1523
Haggai Eran6aec21f2014-12-11 17:04:23 +02001524 err = ib_register_device(&dev->ib_dev, NULL);
1525 if (err)
1526 goto err_odp;
1527
Eli Cohene126ba92013-07-07 17:25:49 +03001528 err = create_umr_res(dev);
1529 if (err)
1530 goto err_dev;
1531
1532 for (i = 0; i < ARRAY_SIZE(mlx5_class_attributes); i++) {
Wei Yongjun281d1a92013-07-30 07:54:26 +08001533 err = device_create_file(&dev->ib_dev.dev,
1534 mlx5_class_attributes[i]);
1535 if (err)
Eli Cohene126ba92013-07-07 17:25:49 +03001536 goto err_umrc;
1537 }
1538
1539 dev->ib_active = true;
1540
Jack Morgenstein9603b612014-07-28 23:30:22 +03001541 return dev;
Eli Cohene126ba92013-07-07 17:25:49 +03001542
1543err_umrc:
1544 destroy_umrc_res(dev);
1545
1546err_dev:
1547 ib_unregister_device(&dev->ib_dev);
1548
Haggai Eran6aec21f2014-12-11 17:04:23 +02001549err_odp:
1550 mlx5_ib_odp_remove_one(dev);
1551
Eli Cohene126ba92013-07-07 17:25:49 +03001552err_rsrc:
1553 destroy_dev_resources(&dev->devr);
1554
Achiad Shochatfc24fc52015-12-23 18:47:17 +02001555err_disable_roce:
1556 if (ll == IB_LINK_LAYER_ETHERNET)
1557 mlx5_disable_roce(dev);
1558
Jack Morgenstein9603b612014-07-28 23:30:22 +03001559err_dealloc:
Eli Cohene126ba92013-07-07 17:25:49 +03001560 ib_dealloc_device((struct ib_device *)dev);
1561
Jack Morgenstein9603b612014-07-28 23:30:22 +03001562 return NULL;
Eli Cohene126ba92013-07-07 17:25:49 +03001563}
1564
Jack Morgenstein9603b612014-07-28 23:30:22 +03001565static void mlx5_ib_remove(struct mlx5_core_dev *mdev, void *context)
Eli Cohene126ba92013-07-07 17:25:49 +03001566{
Jack Morgenstein9603b612014-07-28 23:30:22 +03001567 struct mlx5_ib_dev *dev = context;
Achiad Shochatfc24fc52015-12-23 18:47:17 +02001568 enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev, 1);
Haggai Eran6aec21f2014-12-11 17:04:23 +02001569
Eli Cohene126ba92013-07-07 17:25:49 +03001570 ib_unregister_device(&dev->ib_dev);
Eli Coheneefd56e2014-09-14 16:47:50 +03001571 destroy_umrc_res(dev);
Haggai Eran6aec21f2014-12-11 17:04:23 +02001572 mlx5_ib_odp_remove_one(dev);
Eli Cohene126ba92013-07-07 17:25:49 +03001573 destroy_dev_resources(&dev->devr);
Achiad Shochatfc24fc52015-12-23 18:47:17 +02001574 if (ll == IB_LINK_LAYER_ETHERNET)
1575 mlx5_disable_roce(dev);
Eli Cohene126ba92013-07-07 17:25:49 +03001576 ib_dealloc_device(&dev->ib_dev);
1577}
1578
Jack Morgenstein9603b612014-07-28 23:30:22 +03001579static struct mlx5_interface mlx5_ib_interface = {
1580 .add = mlx5_ib_add,
1581 .remove = mlx5_ib_remove,
1582 .event = mlx5_ib_event,
Saeed Mahameed64613d942015-04-02 17:07:34 +03001583 .protocol = MLX5_INTERFACE_PROTOCOL_IB,
Eli Cohene126ba92013-07-07 17:25:49 +03001584};
1585
1586static int __init mlx5_ib_init(void)
1587{
Haggai Eran6aec21f2014-12-11 17:04:23 +02001588 int err;
1589
Jack Morgenstein9603b612014-07-28 23:30:22 +03001590 if (deprecated_prof_sel != 2)
1591 pr_warn("prof_sel is deprecated for mlx5_ib, set it for mlx5_core\n");
1592
Haggai Eran6aec21f2014-12-11 17:04:23 +02001593 err = mlx5_ib_odp_init();
1594 if (err)
1595 return err;
1596
1597 err = mlx5_register_interface(&mlx5_ib_interface);
1598 if (err)
1599 goto clean_odp;
1600
1601 return err;
1602
1603clean_odp:
1604 mlx5_ib_odp_cleanup();
1605 return err;
Eli Cohene126ba92013-07-07 17:25:49 +03001606}
1607
1608static void __exit mlx5_ib_cleanup(void)
1609{
Jack Morgenstein9603b612014-07-28 23:30:22 +03001610 mlx5_unregister_interface(&mlx5_ib_interface);
Haggai Eran6aec21f2014-12-11 17:04:23 +02001611 mlx5_ib_odp_cleanup();
Eli Cohene126ba92013-07-07 17:25:49 +03001612}
1613
1614module_init(mlx5_ib_init);
1615module_exit(mlx5_ib_cleanup);