xref: /openbmc/linux/drivers/infiniband/hw/mlx5/main.c (revision f360d88a2efddf2d2a2d01a8ac76fded34d624b4)
1e126ba97SEli Cohen /*
2e126ba97SEli Cohen  * Copyright (c) 2013, Mellanox Technologies inc.  All rights reserved.
3e126ba97SEli Cohen  *
4e126ba97SEli Cohen  * This software is available to you under a choice of one of two
5e126ba97SEli Cohen  * licenses.  You may choose to be licensed under the terms of the GNU
6e126ba97SEli Cohen  * General Public License (GPL) Version 2, available from the file
7e126ba97SEli Cohen  * COPYING in the main directory of this source tree, or the
8e126ba97SEli Cohen  * OpenIB.org BSD license below:
9e126ba97SEli Cohen  *
10e126ba97SEli Cohen  *     Redistribution and use in source and binary forms, with or
11e126ba97SEli Cohen  *     without modification, are permitted provided that the following
12e126ba97SEli Cohen  *     conditions are met:
13e126ba97SEli Cohen  *
14e126ba97SEli Cohen  *      - Redistributions of source code must retain the above
15e126ba97SEli Cohen  *        copyright notice, this list of conditions and the following
16e126ba97SEli Cohen  *        disclaimer.
17e126ba97SEli Cohen  *
18e126ba97SEli Cohen  *      - Redistributions in binary form must reproduce the above
19e126ba97SEli Cohen  *        copyright notice, this list of conditions and the following
20e126ba97SEli Cohen  *        disclaimer in the documentation and/or other materials
21e126ba97SEli Cohen  *        provided with the distribution.
22e126ba97SEli Cohen  *
23e126ba97SEli Cohen  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24e126ba97SEli Cohen  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25e126ba97SEli Cohen  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26e126ba97SEli Cohen  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27e126ba97SEli Cohen  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28e126ba97SEli Cohen  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29e126ba97SEli Cohen  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30e126ba97SEli Cohen  * SOFTWARE.
31e126ba97SEli Cohen  */
32e126ba97SEli Cohen 
33e126ba97SEli Cohen #include <asm-generic/kmap_types.h>
34e126ba97SEli Cohen #include <linux/module.h>
35e126ba97SEli Cohen #include <linux/init.h>
36e126ba97SEli Cohen #include <linux/errno.h>
37e126ba97SEli Cohen #include <linux/pci.h>
38e126ba97SEli Cohen #include <linux/dma-mapping.h>
39e126ba97SEli Cohen #include <linux/slab.h>
40e126ba97SEli Cohen #include <linux/io-mapping.h>
41e126ba97SEli Cohen #include <linux/sched.h>
42e126ba97SEli Cohen #include <rdma/ib_user_verbs.h>
43e126ba97SEli Cohen #include <rdma/ib_smi.h>
44e126ba97SEli Cohen #include <rdma/ib_umem.h>
45e126ba97SEli Cohen #include "user.h"
46e126ba97SEli Cohen #include "mlx5_ib.h"
47e126ba97SEli Cohen 
48e126ba97SEli Cohen #define DRIVER_NAME "mlx5_ib"
49169a1d85SAmir Vadai #define DRIVER_VERSION "2.2-1"
50169a1d85SAmir Vadai #define DRIVER_RELDATE	"Feb 2014"
51e126ba97SEli Cohen 
52e126ba97SEli Cohen MODULE_AUTHOR("Eli Cohen <eli@mellanox.com>");
53e126ba97SEli Cohen MODULE_DESCRIPTION("Mellanox Connect-IB HCA IB driver");
54e126ba97SEli Cohen MODULE_LICENSE("Dual BSD/GPL");
55e126ba97SEli Cohen MODULE_VERSION(DRIVER_VERSION);
56e126ba97SEli Cohen 
57e126ba97SEli Cohen static int prof_sel = 2;
58e126ba97SEli Cohen module_param_named(prof_sel, prof_sel, int, 0444);
59e126ba97SEli Cohen MODULE_PARM_DESC(prof_sel, "profile selector. Valid range 0 - 2");
60e126ba97SEli Cohen 
61e126ba97SEli Cohen static char mlx5_version[] =
62e126ba97SEli Cohen 	DRIVER_NAME ": Mellanox Connect-IB Infiniband driver v"
63e126ba97SEli Cohen 	DRIVER_VERSION " (" DRIVER_RELDATE ")\n";
64e126ba97SEli Cohen 
65ad32b95fSRoland Dreier static struct mlx5_profile profile[] = {
66e126ba97SEli Cohen 	[0] = {
67e126ba97SEli Cohen 		.mask		= 0,
68e126ba97SEli Cohen 	},
69e126ba97SEli Cohen 	[1] = {
70e126ba97SEli Cohen 		.mask		= MLX5_PROF_MASK_QP_SIZE,
71e126ba97SEli Cohen 		.log_max_qp	= 12,
72e126ba97SEli Cohen 	},
73e126ba97SEli Cohen 	[2] = {
74e126ba97SEli Cohen 		.mask		= MLX5_PROF_MASK_QP_SIZE |
75e126ba97SEli Cohen 				  MLX5_PROF_MASK_MR_CACHE,
76e126ba97SEli Cohen 		.log_max_qp	= 17,
77e126ba97SEli Cohen 		.mr_cache[0]	= {
78e126ba97SEli Cohen 			.size	= 500,
79e126ba97SEli Cohen 			.limit	= 250
80e126ba97SEli Cohen 		},
81e126ba97SEli Cohen 		.mr_cache[1]	= {
82e126ba97SEli Cohen 			.size	= 500,
83e126ba97SEli Cohen 			.limit	= 250
84e126ba97SEli Cohen 		},
85e126ba97SEli Cohen 		.mr_cache[2]	= {
86e126ba97SEli Cohen 			.size	= 500,
87e126ba97SEli Cohen 			.limit	= 250
88e126ba97SEli Cohen 		},
89e126ba97SEli Cohen 		.mr_cache[3]	= {
90e126ba97SEli Cohen 			.size	= 500,
91e126ba97SEli Cohen 			.limit	= 250
92e126ba97SEli Cohen 		},
93e126ba97SEli Cohen 		.mr_cache[4]	= {
94e126ba97SEli Cohen 			.size	= 500,
95e126ba97SEli Cohen 			.limit	= 250
96e126ba97SEli Cohen 		},
97e126ba97SEli Cohen 		.mr_cache[5]	= {
98e126ba97SEli Cohen 			.size	= 500,
99e126ba97SEli Cohen 			.limit	= 250
100e126ba97SEli Cohen 		},
101e126ba97SEli Cohen 		.mr_cache[6]	= {
102e126ba97SEli Cohen 			.size	= 500,
103e126ba97SEli Cohen 			.limit	= 250
104e126ba97SEli Cohen 		},
105e126ba97SEli Cohen 		.mr_cache[7]	= {
106e126ba97SEli Cohen 			.size	= 500,
107e126ba97SEli Cohen 			.limit	= 250
108e126ba97SEli Cohen 		},
109e126ba97SEli Cohen 		.mr_cache[8]	= {
110e126ba97SEli Cohen 			.size	= 500,
111e126ba97SEli Cohen 			.limit	= 250
112e126ba97SEli Cohen 		},
113e126ba97SEli Cohen 		.mr_cache[9]	= {
114e126ba97SEli Cohen 			.size	= 500,
115e126ba97SEli Cohen 			.limit	= 250
116e126ba97SEli Cohen 		},
117e126ba97SEli Cohen 		.mr_cache[10]	= {
118e126ba97SEli Cohen 			.size	= 500,
119e126ba97SEli Cohen 			.limit	= 250
120e126ba97SEli Cohen 		},
121e126ba97SEli Cohen 		.mr_cache[11]	= {
122e126ba97SEli Cohen 			.size	= 500,
123e126ba97SEli Cohen 			.limit	= 250
124e126ba97SEli Cohen 		},
125e126ba97SEli Cohen 		.mr_cache[12]	= {
126e126ba97SEli Cohen 			.size	= 64,
127e126ba97SEli Cohen 			.limit	= 32
128e126ba97SEli Cohen 		},
129e126ba97SEli Cohen 		.mr_cache[13]	= {
130e126ba97SEli Cohen 			.size	= 32,
131e126ba97SEli Cohen 			.limit	= 16
132e126ba97SEli Cohen 		},
133e126ba97SEli Cohen 		.mr_cache[14]	= {
134e126ba97SEli Cohen 			.size	= 16,
135e126ba97SEli Cohen 			.limit	= 8
136e126ba97SEli Cohen 		},
137e126ba97SEli Cohen 		.mr_cache[15]	= {
138e126ba97SEli Cohen 			.size	= 8,
139e126ba97SEli Cohen 			.limit	= 4
140e126ba97SEli Cohen 		},
141e126ba97SEli Cohen 	},
142e126ba97SEli Cohen };
143e126ba97SEli Cohen 
144e126ba97SEli Cohen int mlx5_vector2eqn(struct mlx5_ib_dev *dev, int vector, int *eqn, int *irqn)
145e126ba97SEli Cohen {
146e126ba97SEli Cohen 	struct mlx5_eq_table *table = &dev->mdev.priv.eq_table;
147e126ba97SEli Cohen 	struct mlx5_eq *eq, *n;
148e126ba97SEli Cohen 	int err = -ENOENT;
149e126ba97SEli Cohen 
150e126ba97SEli Cohen 	spin_lock(&table->lock);
151e126ba97SEli Cohen 	list_for_each_entry_safe(eq, n, &dev->eqs_list, list) {
152e126ba97SEli Cohen 		if (eq->index == vector) {
153e126ba97SEli Cohen 			*eqn = eq->eqn;
154e126ba97SEli Cohen 			*irqn = eq->irqn;
155e126ba97SEli Cohen 			err = 0;
156e126ba97SEli Cohen 			break;
157e126ba97SEli Cohen 		}
158e126ba97SEli Cohen 	}
159e126ba97SEli Cohen 	spin_unlock(&table->lock);
160e126ba97SEli Cohen 
161e126ba97SEli Cohen 	return err;
162e126ba97SEli Cohen }
163e126ba97SEli Cohen 
164e126ba97SEli Cohen static int alloc_comp_eqs(struct mlx5_ib_dev *dev)
165e126ba97SEli Cohen {
166e126ba97SEli Cohen 	struct mlx5_eq_table *table = &dev->mdev.priv.eq_table;
167ada9f5d0SSagi Grimberg 	char name[MLX5_MAX_EQ_NAME];
168e126ba97SEli Cohen 	struct mlx5_eq *eq, *n;
169e126ba97SEli Cohen 	int ncomp_vec;
170e126ba97SEli Cohen 	int nent;
171e126ba97SEli Cohen 	int err;
172e126ba97SEli Cohen 	int i;
173e126ba97SEli Cohen 
174e126ba97SEli Cohen 	INIT_LIST_HEAD(&dev->eqs_list);
175e126ba97SEli Cohen 	ncomp_vec = table->num_comp_vectors;
176e126ba97SEli Cohen 	nent = MLX5_COMP_EQ_SIZE;
177e126ba97SEli Cohen 	for (i = 0; i < ncomp_vec; i++) {
178e126ba97SEli Cohen 		eq = kzalloc(sizeof(*eq), GFP_KERNEL);
179e126ba97SEli Cohen 		if (!eq) {
180e126ba97SEli Cohen 			err = -ENOMEM;
181e126ba97SEli Cohen 			goto clean;
182e126ba97SEli Cohen 		}
183e126ba97SEli Cohen 
184ada9f5d0SSagi Grimberg 		snprintf(name, MLX5_MAX_EQ_NAME, "mlx5_comp%d", i);
185e126ba97SEli Cohen 		err = mlx5_create_map_eq(&dev->mdev, eq,
186e126ba97SEli Cohen 					 i + MLX5_EQ_VEC_COMP_BASE, nent, 0,
187ada9f5d0SSagi Grimberg 					 name, &dev->mdev.priv.uuari.uars[0]);
188e126ba97SEli Cohen 		if (err) {
189e126ba97SEli Cohen 			kfree(eq);
190e126ba97SEli Cohen 			goto clean;
191e126ba97SEli Cohen 		}
192e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "allocated completion EQN %d\n", eq->eqn);
193e126ba97SEli Cohen 		eq->index = i;
194e126ba97SEli Cohen 		spin_lock(&table->lock);
195e126ba97SEli Cohen 		list_add_tail(&eq->list, &dev->eqs_list);
196e126ba97SEli Cohen 		spin_unlock(&table->lock);
197e126ba97SEli Cohen 	}
198e126ba97SEli Cohen 
199e126ba97SEli Cohen 	dev->num_comp_vectors = ncomp_vec;
200e126ba97SEli Cohen 	return 0;
201e126ba97SEli Cohen 
202e126ba97SEli Cohen clean:
203e126ba97SEli Cohen 	spin_lock(&table->lock);
204e126ba97SEli Cohen 	list_for_each_entry_safe(eq, n, &dev->eqs_list, list) {
205e126ba97SEli Cohen 		list_del(&eq->list);
206e126ba97SEli Cohen 		spin_unlock(&table->lock);
207e126ba97SEli Cohen 		if (mlx5_destroy_unmap_eq(&dev->mdev, eq))
208e126ba97SEli Cohen 			mlx5_ib_warn(dev, "failed to destroy EQ 0x%x\n", eq->eqn);
209e126ba97SEli Cohen 		kfree(eq);
210e126ba97SEli Cohen 		spin_lock(&table->lock);
211e126ba97SEli Cohen 	}
212e126ba97SEli Cohen 	spin_unlock(&table->lock);
213e126ba97SEli Cohen 	return err;
214e126ba97SEli Cohen }
215e126ba97SEli Cohen 
216e126ba97SEli Cohen static void free_comp_eqs(struct mlx5_ib_dev *dev)
217e126ba97SEli Cohen {
218e126ba97SEli Cohen 	struct mlx5_eq_table *table = &dev->mdev.priv.eq_table;
219e126ba97SEli Cohen 	struct mlx5_eq *eq, *n;
220e126ba97SEli Cohen 
221e126ba97SEli Cohen 	spin_lock(&table->lock);
222e126ba97SEli Cohen 	list_for_each_entry_safe(eq, n, &dev->eqs_list, list) {
223e126ba97SEli Cohen 		list_del(&eq->list);
224e126ba97SEli Cohen 		spin_unlock(&table->lock);
225e126ba97SEli Cohen 		if (mlx5_destroy_unmap_eq(&dev->mdev, eq))
226e126ba97SEli Cohen 			mlx5_ib_warn(dev, "failed to destroy EQ 0x%x\n", eq->eqn);
227e126ba97SEli Cohen 		kfree(eq);
228e126ba97SEli Cohen 		spin_lock(&table->lock);
229e126ba97SEli Cohen 	}
230e126ba97SEli Cohen 	spin_unlock(&table->lock);
231e126ba97SEli Cohen }
232e126ba97SEli Cohen 
233e126ba97SEli Cohen static int mlx5_ib_query_device(struct ib_device *ibdev,
234e126ba97SEli Cohen 				struct ib_device_attr *props)
235e126ba97SEli Cohen {
236e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
237e126ba97SEli Cohen 	struct ib_smp *in_mad  = NULL;
238e126ba97SEli Cohen 	struct ib_smp *out_mad = NULL;
239e126ba97SEli Cohen 	int err = -ENOMEM;
240e126ba97SEli Cohen 	int max_rq_sg;
241e126ba97SEli Cohen 	int max_sq_sg;
242e126ba97SEli Cohen 	u64 flags;
243e126ba97SEli Cohen 
244e126ba97SEli Cohen 	in_mad  = kzalloc(sizeof(*in_mad), GFP_KERNEL);
245e126ba97SEli Cohen 	out_mad = kmalloc(sizeof(*out_mad), GFP_KERNEL);
246e126ba97SEli Cohen 	if (!in_mad || !out_mad)
247e126ba97SEli Cohen 		goto out;
248e126ba97SEli Cohen 
249e126ba97SEli Cohen 	init_query_mad(in_mad);
250e126ba97SEli Cohen 	in_mad->attr_id = IB_SMP_ATTR_NODE_INFO;
251e126ba97SEli Cohen 
252e126ba97SEli Cohen 	err = mlx5_MAD_IFC(to_mdev(ibdev), 1, 1, 1, NULL, NULL, in_mad, out_mad);
253e126ba97SEli Cohen 	if (err)
254e126ba97SEli Cohen 		goto out;
255e126ba97SEli Cohen 
256e126ba97SEli Cohen 	memset(props, 0, sizeof(*props));
257e126ba97SEli Cohen 
258e126ba97SEli Cohen 	props->fw_ver = ((u64)fw_rev_maj(&dev->mdev) << 32) |
259e126ba97SEli Cohen 		(fw_rev_min(&dev->mdev) << 16) |
260e126ba97SEli Cohen 		fw_rev_sub(&dev->mdev);
261e126ba97SEli Cohen 	props->device_cap_flags    = IB_DEVICE_CHANGE_PHY_PORT |
262e126ba97SEli Cohen 		IB_DEVICE_PORT_ACTIVE_EVENT		|
263e126ba97SEli Cohen 		IB_DEVICE_SYS_IMAGE_GUID		|
2641a4c3a3dSEli Cohen 		IB_DEVICE_RC_RNR_NAK_GEN;
265e126ba97SEli Cohen 	flags = dev->mdev.caps.flags;
266e126ba97SEli Cohen 	if (flags & MLX5_DEV_CAP_FLAG_BAD_PKEY_CNTR)
267e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
268e126ba97SEli Cohen 	if (flags & MLX5_DEV_CAP_FLAG_BAD_QKEY_CNTR)
269e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
270e126ba97SEli Cohen 	if (flags & MLX5_DEV_CAP_FLAG_APM)
271e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
272e126ba97SEli Cohen 	props->device_cap_flags |= IB_DEVICE_LOCAL_DMA_LKEY;
273e126ba97SEli Cohen 	if (flags & MLX5_DEV_CAP_FLAG_XRC)
274e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_XRC;
275e126ba97SEli Cohen 	props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
2762dea9094SSagi Grimberg 	if (flags & MLX5_DEV_CAP_FLAG_SIG_HAND_OVER) {
2772dea9094SSagi Grimberg 		props->device_cap_flags |= IB_DEVICE_SIGNATURE_HANDOVER;
2782dea9094SSagi Grimberg 		/* At this stage no support for signature handover */
2792dea9094SSagi Grimberg 		props->sig_prot_cap = IB_PROT_T10DIF_TYPE_1 |
2802dea9094SSagi Grimberg 				      IB_PROT_T10DIF_TYPE_2 |
2812dea9094SSagi Grimberg 				      IB_PROT_T10DIF_TYPE_3;
2822dea9094SSagi Grimberg 		props->sig_guard_cap = IB_GUARD_T10DIF_CRC |
2832dea9094SSagi Grimberg 				       IB_GUARD_T10DIF_CSUM;
2842dea9094SSagi Grimberg 	}
285*f360d88aSEli Cohen 	if (flags & MLX5_DEV_CAP_FLAG_BLOCK_MCAST)
286*f360d88aSEli Cohen 		props->device_cap_flags |= IB_DEVICE_BLOCK_MULTICAST_LOOPBACK;
287e126ba97SEli Cohen 
288e126ba97SEli Cohen 	props->vendor_id	   = be32_to_cpup((__be32 *)(out_mad->data + 36)) &
289e126ba97SEli Cohen 		0xffffff;
290e126ba97SEli Cohen 	props->vendor_part_id	   = be16_to_cpup((__be16 *)(out_mad->data + 30));
291e126ba97SEli Cohen 	props->hw_ver		   = be32_to_cpup((__be32 *)(out_mad->data + 32));
292e126ba97SEli Cohen 	memcpy(&props->sys_image_guid, out_mad->data +	4, 8);
293e126ba97SEli Cohen 
294e126ba97SEli Cohen 	props->max_mr_size	   = ~0ull;
295e126ba97SEli Cohen 	props->page_size_cap	   = dev->mdev.caps.min_page_sz;
296e126ba97SEli Cohen 	props->max_qp		   = 1 << dev->mdev.caps.log_max_qp;
297e126ba97SEli Cohen 	props->max_qp_wr	   = dev->mdev.caps.max_wqes;
298e126ba97SEli Cohen 	max_rq_sg = dev->mdev.caps.max_rq_desc_sz / sizeof(struct mlx5_wqe_data_seg);
299e126ba97SEli Cohen 	max_sq_sg = (dev->mdev.caps.max_sq_desc_sz - sizeof(struct mlx5_wqe_ctrl_seg)) /
300e126ba97SEli Cohen 		sizeof(struct mlx5_wqe_data_seg);
301e126ba97SEli Cohen 	props->max_sge = min(max_rq_sg, max_sq_sg);
302e126ba97SEli Cohen 	props->max_cq		   = 1 << dev->mdev.caps.log_max_cq;
303e126ba97SEli Cohen 	props->max_cqe		   = dev->mdev.caps.max_cqes - 1;
304e126ba97SEli Cohen 	props->max_mr		   = 1 << dev->mdev.caps.log_max_mkey;
305e126ba97SEli Cohen 	props->max_pd		   = 1 << dev->mdev.caps.log_max_pd;
306e126ba97SEli Cohen 	props->max_qp_rd_atom	   = dev->mdev.caps.max_ra_req_qp;
307e126ba97SEli Cohen 	props->max_qp_init_rd_atom = dev->mdev.caps.max_ra_res_qp;
308e126ba97SEli Cohen 	props->max_res_rd_atom	   = props->max_qp_rd_atom * props->max_qp;
309e126ba97SEli Cohen 	props->max_srq		   = 1 << dev->mdev.caps.log_max_srq;
310e126ba97SEli Cohen 	props->max_srq_wr	   = dev->mdev.caps.max_srq_wqes - 1;
311e126ba97SEli Cohen 	props->max_srq_sge	   = max_rq_sg - 1;
312e126ba97SEli Cohen 	props->max_fast_reg_page_list_len = (unsigned int)-1;
313e126ba97SEli Cohen 	props->local_ca_ack_delay  = dev->mdev.caps.local_ca_ack_delay;
31481bea28fSEli Cohen 	props->atomic_cap	   = IB_ATOMIC_NONE;
31581bea28fSEli Cohen 	props->masked_atomic_cap   = IB_ATOMIC_NONE;
316e126ba97SEli Cohen 	props->max_pkeys	   = be16_to_cpup((__be16 *)(out_mad->data + 28));
317e126ba97SEli Cohen 	props->max_mcast_grp	   = 1 << dev->mdev.caps.log_max_mcg;
318e126ba97SEli Cohen 	props->max_mcast_qp_attach = dev->mdev.caps.max_qp_mcg;
319e126ba97SEli Cohen 	props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
320e126ba97SEli Cohen 					   props->max_mcast_grp;
321e126ba97SEli Cohen 	props->max_map_per_fmr = INT_MAX; /* no limit in ConnectIB */
322e126ba97SEli Cohen 
323e126ba97SEli Cohen out:
324e126ba97SEli Cohen 	kfree(in_mad);
325e126ba97SEli Cohen 	kfree(out_mad);
326e126ba97SEli Cohen 
327e126ba97SEli Cohen 	return err;
328e126ba97SEli Cohen }
329e126ba97SEli Cohen 
330e126ba97SEli Cohen int mlx5_ib_query_port(struct ib_device *ibdev, u8 port,
331e126ba97SEli Cohen 		       struct ib_port_attr *props)
332e126ba97SEli Cohen {
333e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
334e126ba97SEli Cohen 	struct ib_smp *in_mad  = NULL;
335e126ba97SEli Cohen 	struct ib_smp *out_mad = NULL;
336e126ba97SEli Cohen 	int ext_active_speed;
337e126ba97SEli Cohen 	int err = -ENOMEM;
338e126ba97SEli Cohen 
339e126ba97SEli Cohen 	if (port < 1 || port > dev->mdev.caps.num_ports) {
340e126ba97SEli Cohen 		mlx5_ib_warn(dev, "invalid port number %d\n", port);
341e126ba97SEli Cohen 		return -EINVAL;
342e126ba97SEli Cohen 	}
343e126ba97SEli Cohen 
344e126ba97SEli Cohen 	in_mad  = kzalloc(sizeof(*in_mad), GFP_KERNEL);
345e126ba97SEli Cohen 	out_mad = kmalloc(sizeof(*out_mad), GFP_KERNEL);
346e126ba97SEli Cohen 	if (!in_mad || !out_mad)
347e126ba97SEli Cohen 		goto out;
348e126ba97SEli Cohen 
349e126ba97SEli Cohen 	memset(props, 0, sizeof(*props));
350e126ba97SEli Cohen 
351e126ba97SEli Cohen 	init_query_mad(in_mad);
352e126ba97SEli Cohen 	in_mad->attr_id  = IB_SMP_ATTR_PORT_INFO;
353e126ba97SEli Cohen 	in_mad->attr_mod = cpu_to_be32(port);
354e126ba97SEli Cohen 
355e126ba97SEli Cohen 	err = mlx5_MAD_IFC(dev, 1, 1, port, NULL, NULL, in_mad, out_mad);
356e126ba97SEli Cohen 	if (err) {
357e126ba97SEli Cohen 		mlx5_ib_warn(dev, "err %d\n", err);
358e126ba97SEli Cohen 		goto out;
359e126ba97SEli Cohen 	}
360e126ba97SEli Cohen 
361e126ba97SEli Cohen 
362e126ba97SEli Cohen 	props->lid		= be16_to_cpup((__be16 *)(out_mad->data + 16));
363e126ba97SEli Cohen 	props->lmc		= out_mad->data[34] & 0x7;
364e126ba97SEli Cohen 	props->sm_lid		= be16_to_cpup((__be16 *)(out_mad->data + 18));
365e126ba97SEli Cohen 	props->sm_sl		= out_mad->data[36] & 0xf;
366e126ba97SEli Cohen 	props->state		= out_mad->data[32] & 0xf;
367e126ba97SEli Cohen 	props->phys_state	= out_mad->data[33] >> 4;
368e126ba97SEli Cohen 	props->port_cap_flags	= be32_to_cpup((__be32 *)(out_mad->data + 20));
369e126ba97SEli Cohen 	props->gid_tbl_len	= out_mad->data[50];
370e126ba97SEli Cohen 	props->max_msg_sz	= 1 << to_mdev(ibdev)->mdev.caps.log_max_msg;
371e126ba97SEli Cohen 	props->pkey_tbl_len	= to_mdev(ibdev)->mdev.caps.port[port - 1].pkey_table_len;
372e126ba97SEli Cohen 	props->bad_pkey_cntr	= be16_to_cpup((__be16 *)(out_mad->data + 46));
373e126ba97SEli Cohen 	props->qkey_viol_cntr	= be16_to_cpup((__be16 *)(out_mad->data + 48));
374e126ba97SEli Cohen 	props->active_width	= out_mad->data[31] & 0xf;
375e126ba97SEli Cohen 	props->active_speed	= out_mad->data[35] >> 4;
376e126ba97SEli Cohen 	props->max_mtu		= out_mad->data[41] & 0xf;
377e126ba97SEli Cohen 	props->active_mtu	= out_mad->data[36] >> 4;
378e126ba97SEli Cohen 	props->subnet_timeout	= out_mad->data[51] & 0x1f;
379e126ba97SEli Cohen 	props->max_vl_num	= out_mad->data[37] >> 4;
380e126ba97SEli Cohen 	props->init_type_reply	= out_mad->data[41] >> 4;
381e126ba97SEli Cohen 
382e126ba97SEli Cohen 	/* Check if extended speeds (EDR/FDR/...) are supported */
383e126ba97SEli Cohen 	if (props->port_cap_flags & IB_PORT_EXTENDED_SPEEDS_SUP) {
384e126ba97SEli Cohen 		ext_active_speed = out_mad->data[62] >> 4;
385e126ba97SEli Cohen 
386e126ba97SEli Cohen 		switch (ext_active_speed) {
387e126ba97SEli Cohen 		case 1:
388e126ba97SEli Cohen 			props->active_speed = 16; /* FDR */
389e126ba97SEli Cohen 			break;
390e126ba97SEli Cohen 		case 2:
391e126ba97SEli Cohen 			props->active_speed = 32; /* EDR */
392e126ba97SEli Cohen 			break;
393e126ba97SEli Cohen 		}
394e126ba97SEli Cohen 	}
395e126ba97SEli Cohen 
396e126ba97SEli Cohen 	/* If reported active speed is QDR, check if is FDR-10 */
397e126ba97SEli Cohen 	if (props->active_speed == 4) {
398e126ba97SEli Cohen 		if (dev->mdev.caps.ext_port_cap[port - 1] &
399e126ba97SEli Cohen 		    MLX_EXT_PORT_CAP_FLAG_EXTENDED_PORT_INFO) {
400e126ba97SEli Cohen 			init_query_mad(in_mad);
401e126ba97SEli Cohen 			in_mad->attr_id = MLX5_ATTR_EXTENDED_PORT_INFO;
402e126ba97SEli Cohen 			in_mad->attr_mod = cpu_to_be32(port);
403e126ba97SEli Cohen 
404e126ba97SEli Cohen 			err = mlx5_MAD_IFC(dev, 1, 1, port,
405e126ba97SEli Cohen 					   NULL, NULL, in_mad, out_mad);
406e126ba97SEli Cohen 			if (err)
407e126ba97SEli Cohen 				goto out;
408e126ba97SEli Cohen 
409e126ba97SEli Cohen 			/* Checking LinkSpeedActive for FDR-10 */
410e126ba97SEli Cohen 			if (out_mad->data[15] & 0x1)
411e126ba97SEli Cohen 				props->active_speed = 8;
412e126ba97SEli Cohen 		}
413e126ba97SEli Cohen 	}
414e126ba97SEli Cohen 
415e126ba97SEli Cohen out:
416e126ba97SEli Cohen 	kfree(in_mad);
417e126ba97SEli Cohen 	kfree(out_mad);
418e126ba97SEli Cohen 
419e126ba97SEli Cohen 	return err;
420e126ba97SEli Cohen }
421e126ba97SEli Cohen 
422e126ba97SEli Cohen static int mlx5_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
423e126ba97SEli Cohen 			     union ib_gid *gid)
424e126ba97SEli Cohen {
425e126ba97SEli Cohen 	struct ib_smp *in_mad  = NULL;
426e126ba97SEli Cohen 	struct ib_smp *out_mad = NULL;
427e126ba97SEli Cohen 	int err = -ENOMEM;
428e126ba97SEli Cohen 
429e126ba97SEli Cohen 	in_mad  = kzalloc(sizeof(*in_mad), GFP_KERNEL);
430e126ba97SEli Cohen 	out_mad = kmalloc(sizeof(*out_mad), GFP_KERNEL);
431e126ba97SEli Cohen 	if (!in_mad || !out_mad)
432e126ba97SEli Cohen 		goto out;
433e126ba97SEli Cohen 
434e126ba97SEli Cohen 	init_query_mad(in_mad);
435e126ba97SEli Cohen 	in_mad->attr_id  = IB_SMP_ATTR_PORT_INFO;
436e126ba97SEli Cohen 	in_mad->attr_mod = cpu_to_be32(port);
437e126ba97SEli Cohen 
438e126ba97SEli Cohen 	err = mlx5_MAD_IFC(to_mdev(ibdev), 1, 1, port, NULL, NULL, in_mad, out_mad);
439e126ba97SEli Cohen 	if (err)
440e126ba97SEli Cohen 		goto out;
441e126ba97SEli Cohen 
442e126ba97SEli Cohen 	memcpy(gid->raw, out_mad->data + 8, 8);
443e126ba97SEli Cohen 
444e126ba97SEli Cohen 	init_query_mad(in_mad);
445e126ba97SEli Cohen 	in_mad->attr_id  = IB_SMP_ATTR_GUID_INFO;
446e126ba97SEli Cohen 	in_mad->attr_mod = cpu_to_be32(index / 8);
447e126ba97SEli Cohen 
448e126ba97SEli Cohen 	err = mlx5_MAD_IFC(to_mdev(ibdev), 1, 1, port, NULL, NULL, in_mad, out_mad);
449e126ba97SEli Cohen 	if (err)
450e126ba97SEli Cohen 		goto out;
451e126ba97SEli Cohen 
452e126ba97SEli Cohen 	memcpy(gid->raw + 8, out_mad->data + (index % 8) * 8, 8);
453e126ba97SEli Cohen 
454e126ba97SEli Cohen out:
455e126ba97SEli Cohen 	kfree(in_mad);
456e126ba97SEli Cohen 	kfree(out_mad);
457e126ba97SEli Cohen 	return err;
458e126ba97SEli Cohen }
459e126ba97SEli Cohen 
460e126ba97SEli Cohen static int mlx5_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
461e126ba97SEli Cohen 			      u16 *pkey)
462e126ba97SEli Cohen {
463e126ba97SEli Cohen 	struct ib_smp *in_mad  = NULL;
464e126ba97SEli Cohen 	struct ib_smp *out_mad = NULL;
465e126ba97SEli Cohen 	int err = -ENOMEM;
466e126ba97SEli Cohen 
467e126ba97SEli Cohen 	in_mad  = kzalloc(sizeof(*in_mad), GFP_KERNEL);
468e126ba97SEli Cohen 	out_mad = kmalloc(sizeof(*out_mad), GFP_KERNEL);
469e126ba97SEli Cohen 	if (!in_mad || !out_mad)
470e126ba97SEli Cohen 		goto out;
471e126ba97SEli Cohen 
472e126ba97SEli Cohen 	init_query_mad(in_mad);
473e126ba97SEli Cohen 	in_mad->attr_id  = IB_SMP_ATTR_PKEY_TABLE;
474e126ba97SEli Cohen 	in_mad->attr_mod = cpu_to_be32(index / 32);
475e126ba97SEli Cohen 
476e126ba97SEli Cohen 	err = mlx5_MAD_IFC(to_mdev(ibdev), 1, 1, port, NULL, NULL, in_mad, out_mad);
477e126ba97SEli Cohen 	if (err)
478e126ba97SEli Cohen 		goto out;
479e126ba97SEli Cohen 
480e126ba97SEli Cohen 	*pkey = be16_to_cpu(((__be16 *)out_mad->data)[index % 32]);
481e126ba97SEli Cohen 
482e126ba97SEli Cohen out:
483e126ba97SEli Cohen 	kfree(in_mad);
484e126ba97SEli Cohen 	kfree(out_mad);
485e126ba97SEli Cohen 	return err;
486e126ba97SEli Cohen }
487e126ba97SEli Cohen 
488e126ba97SEli Cohen struct mlx5_reg_node_desc {
489e126ba97SEli Cohen 	u8	desc[64];
490e126ba97SEli Cohen };
491e126ba97SEli Cohen 
492e126ba97SEli Cohen static int mlx5_ib_modify_device(struct ib_device *ibdev, int mask,
493e126ba97SEli Cohen 				 struct ib_device_modify *props)
494e126ba97SEli Cohen {
495e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
496e126ba97SEli Cohen 	struct mlx5_reg_node_desc in;
497e126ba97SEli Cohen 	struct mlx5_reg_node_desc out;
498e126ba97SEli Cohen 	int err;
499e126ba97SEli Cohen 
500e126ba97SEli Cohen 	if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
501e126ba97SEli Cohen 		return -EOPNOTSUPP;
502e126ba97SEli Cohen 
503e126ba97SEli Cohen 	if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
504e126ba97SEli Cohen 		return 0;
505e126ba97SEli Cohen 
506e126ba97SEli Cohen 	/*
507e126ba97SEli Cohen 	 * If possible, pass node desc to FW, so it can generate
508e126ba97SEli Cohen 	 * a 144 trap.  If cmd fails, just ignore.
509e126ba97SEli Cohen 	 */
510e126ba97SEli Cohen 	memcpy(&in, props->node_desc, 64);
511e126ba97SEli Cohen 	err = mlx5_core_access_reg(&dev->mdev, &in, sizeof(in), &out,
512e126ba97SEli Cohen 				   sizeof(out), MLX5_REG_NODE_DESC, 0, 1);
513e126ba97SEli Cohen 	if (err)
514e126ba97SEli Cohen 		return err;
515e126ba97SEli Cohen 
516e126ba97SEli Cohen 	memcpy(ibdev->node_desc, props->node_desc, 64);
517e126ba97SEli Cohen 
518e126ba97SEli Cohen 	return err;
519e126ba97SEli Cohen }
520e126ba97SEli Cohen 
521e126ba97SEli Cohen static int mlx5_ib_modify_port(struct ib_device *ibdev, u8 port, int mask,
522e126ba97SEli Cohen 			       struct ib_port_modify *props)
523e126ba97SEli Cohen {
524e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
525e126ba97SEli Cohen 	struct ib_port_attr attr;
526e126ba97SEli Cohen 	u32 tmp;
527e126ba97SEli Cohen 	int err;
528e126ba97SEli Cohen 
529e126ba97SEli Cohen 	mutex_lock(&dev->cap_mask_mutex);
530e126ba97SEli Cohen 
531e126ba97SEli Cohen 	err = mlx5_ib_query_port(ibdev, port, &attr);
532e126ba97SEli Cohen 	if (err)
533e126ba97SEli Cohen 		goto out;
534e126ba97SEli Cohen 
535e126ba97SEli Cohen 	tmp = (attr.port_cap_flags | props->set_port_cap_mask) &
536e126ba97SEli Cohen 		~props->clr_port_cap_mask;
537e126ba97SEli Cohen 
538e126ba97SEli Cohen 	err = mlx5_set_port_caps(&dev->mdev, port, tmp);
539e126ba97SEli Cohen 
540e126ba97SEli Cohen out:
541e126ba97SEli Cohen 	mutex_unlock(&dev->cap_mask_mutex);
542e126ba97SEli Cohen 	return err;
543e126ba97SEli Cohen }
544e126ba97SEli Cohen 
545e126ba97SEli Cohen static struct ib_ucontext *mlx5_ib_alloc_ucontext(struct ib_device *ibdev,
546e126ba97SEli Cohen 						  struct ib_udata *udata)
547e126ba97SEli Cohen {
548e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
54978c0f98cSEli Cohen 	struct mlx5_ib_alloc_ucontext_req_v2 req;
550e126ba97SEli Cohen 	struct mlx5_ib_alloc_ucontext_resp resp;
551e126ba97SEli Cohen 	struct mlx5_ib_ucontext *context;
552e126ba97SEli Cohen 	struct mlx5_uuar_info *uuari;
553e126ba97SEli Cohen 	struct mlx5_uar *uars;
554c1be5232SEli Cohen 	int gross_uuars;
555e126ba97SEli Cohen 	int num_uars;
55678c0f98cSEli Cohen 	int ver;
557e126ba97SEli Cohen 	int uuarn;
558e126ba97SEli Cohen 	int err;
559e126ba97SEli Cohen 	int i;
56078c0f98cSEli Cohen 	int reqlen;
561e126ba97SEli Cohen 
562e126ba97SEli Cohen 	if (!dev->ib_active)
563e126ba97SEli Cohen 		return ERR_PTR(-EAGAIN);
564e126ba97SEli Cohen 
56578c0f98cSEli Cohen 	memset(&req, 0, sizeof(req));
56678c0f98cSEli Cohen 	reqlen = udata->inlen - sizeof(struct ib_uverbs_cmd_hdr);
56778c0f98cSEli Cohen 	if (reqlen == sizeof(struct mlx5_ib_alloc_ucontext_req))
56878c0f98cSEli Cohen 		ver = 0;
56978c0f98cSEli Cohen 	else if (reqlen == sizeof(struct mlx5_ib_alloc_ucontext_req_v2))
57078c0f98cSEli Cohen 		ver = 2;
57178c0f98cSEli Cohen 	else
57278c0f98cSEli Cohen 		return ERR_PTR(-EINVAL);
57378c0f98cSEli Cohen 
57478c0f98cSEli Cohen 	err = ib_copy_from_udata(&req, udata, reqlen);
575e126ba97SEli Cohen 	if (err)
576e126ba97SEli Cohen 		return ERR_PTR(err);
577e126ba97SEli Cohen 
57878c0f98cSEli Cohen 	if (req.flags || req.reserved)
57978c0f98cSEli Cohen 		return ERR_PTR(-EINVAL);
58078c0f98cSEli Cohen 
581e126ba97SEli Cohen 	if (req.total_num_uuars > MLX5_MAX_UUARS)
582e126ba97SEli Cohen 		return ERR_PTR(-ENOMEM);
583e126ba97SEli Cohen 
584e126ba97SEli Cohen 	if (req.total_num_uuars == 0)
585e126ba97SEli Cohen 		return ERR_PTR(-EINVAL);
586e126ba97SEli Cohen 
587c1be5232SEli Cohen 	req.total_num_uuars = ALIGN(req.total_num_uuars,
588c1be5232SEli Cohen 				    MLX5_NON_FP_BF_REGS_PER_PAGE);
589e126ba97SEli Cohen 	if (req.num_low_latency_uuars > req.total_num_uuars - 1)
590e126ba97SEli Cohen 		return ERR_PTR(-EINVAL);
591e126ba97SEli Cohen 
592c1be5232SEli Cohen 	num_uars = req.total_num_uuars / MLX5_NON_FP_BF_REGS_PER_PAGE;
593c1be5232SEli Cohen 	gross_uuars = num_uars * MLX5_BF_REGS_PER_PAGE;
594e126ba97SEli Cohen 	resp.qp_tab_size      = 1 << dev->mdev.caps.log_max_qp;
595e126ba97SEli Cohen 	resp.bf_reg_size      = dev->mdev.caps.bf_reg_size;
596e126ba97SEli Cohen 	resp.cache_line_size  = L1_CACHE_BYTES;
597e126ba97SEli Cohen 	resp.max_sq_desc_sz = dev->mdev.caps.max_sq_desc_sz;
598e126ba97SEli Cohen 	resp.max_rq_desc_sz = dev->mdev.caps.max_rq_desc_sz;
599e126ba97SEli Cohen 	resp.max_send_wqebb = dev->mdev.caps.max_wqes;
600e126ba97SEli Cohen 	resp.max_recv_wr = dev->mdev.caps.max_wqes;
601e126ba97SEli Cohen 	resp.max_srq_recv_wr = dev->mdev.caps.max_srq_wqes;
602e126ba97SEli Cohen 
603e126ba97SEli Cohen 	context = kzalloc(sizeof(*context), GFP_KERNEL);
604e126ba97SEli Cohen 	if (!context)
605e126ba97SEli Cohen 		return ERR_PTR(-ENOMEM);
606e126ba97SEli Cohen 
607e126ba97SEli Cohen 	uuari = &context->uuari;
608e126ba97SEli Cohen 	mutex_init(&uuari->lock);
609e126ba97SEli Cohen 	uars = kcalloc(num_uars, sizeof(*uars), GFP_KERNEL);
610e126ba97SEli Cohen 	if (!uars) {
611e126ba97SEli Cohen 		err = -ENOMEM;
612e126ba97SEli Cohen 		goto out_ctx;
613e126ba97SEli Cohen 	}
614e126ba97SEli Cohen 
615c1be5232SEli Cohen 	uuari->bitmap = kcalloc(BITS_TO_LONGS(gross_uuars),
616e126ba97SEli Cohen 				sizeof(*uuari->bitmap),
617e126ba97SEli Cohen 				GFP_KERNEL);
618e126ba97SEli Cohen 	if (!uuari->bitmap) {
619e126ba97SEli Cohen 		err = -ENOMEM;
620e126ba97SEli Cohen 		goto out_uar_ctx;
621e126ba97SEli Cohen 	}
622e126ba97SEli Cohen 	/*
623e126ba97SEli Cohen 	 * clear all fast path uuars
624e126ba97SEli Cohen 	 */
625c1be5232SEli Cohen 	for (i = 0; i < gross_uuars; i++) {
626e126ba97SEli Cohen 		uuarn = i & 3;
627e126ba97SEli Cohen 		if (uuarn == 2 || uuarn == 3)
628e126ba97SEli Cohen 			set_bit(i, uuari->bitmap);
629e126ba97SEli Cohen 	}
630e126ba97SEli Cohen 
631c1be5232SEli Cohen 	uuari->count = kcalloc(gross_uuars, sizeof(*uuari->count), GFP_KERNEL);
632e126ba97SEli Cohen 	if (!uuari->count) {
633e126ba97SEli Cohen 		err = -ENOMEM;
634e126ba97SEli Cohen 		goto out_bitmap;
635e126ba97SEli Cohen 	}
636e126ba97SEli Cohen 
637e126ba97SEli Cohen 	for (i = 0; i < num_uars; i++) {
638e126ba97SEli Cohen 		err = mlx5_cmd_alloc_uar(&dev->mdev, &uars[i].index);
639e126ba97SEli Cohen 		if (err)
640e126ba97SEli Cohen 			goto out_count;
641e126ba97SEli Cohen 	}
642e126ba97SEli Cohen 
643e126ba97SEli Cohen 	INIT_LIST_HEAD(&context->db_page_list);
644e126ba97SEli Cohen 	mutex_init(&context->db_page_mutex);
645e126ba97SEli Cohen 
646e126ba97SEli Cohen 	resp.tot_uuars = req.total_num_uuars;
647e126ba97SEli Cohen 	resp.num_ports = dev->mdev.caps.num_ports;
64892b0ca7cSDan Carpenter 	err = ib_copy_to_udata(udata, &resp,
64992b0ca7cSDan Carpenter 			       sizeof(resp) - sizeof(resp.reserved));
650e126ba97SEli Cohen 	if (err)
651e126ba97SEli Cohen 		goto out_uars;
652e126ba97SEli Cohen 
65378c0f98cSEli Cohen 	uuari->ver = ver;
654e126ba97SEli Cohen 	uuari->num_low_latency_uuars = req.num_low_latency_uuars;
655e126ba97SEli Cohen 	uuari->uars = uars;
656e126ba97SEli Cohen 	uuari->num_uars = num_uars;
657e126ba97SEli Cohen 	return &context->ibucontext;
658e126ba97SEli Cohen 
659e126ba97SEli Cohen out_uars:
660e126ba97SEli Cohen 	for (i--; i >= 0; i--)
661e126ba97SEli Cohen 		mlx5_cmd_free_uar(&dev->mdev, uars[i].index);
662e126ba97SEli Cohen out_count:
663e126ba97SEli Cohen 	kfree(uuari->count);
664e126ba97SEli Cohen 
665e126ba97SEli Cohen out_bitmap:
666e126ba97SEli Cohen 	kfree(uuari->bitmap);
667e126ba97SEli Cohen 
668e126ba97SEli Cohen out_uar_ctx:
669e126ba97SEli Cohen 	kfree(uars);
670e126ba97SEli Cohen 
671e126ba97SEli Cohen out_ctx:
672e126ba97SEli Cohen 	kfree(context);
673e126ba97SEli Cohen 	return ERR_PTR(err);
674e126ba97SEli Cohen }
675e126ba97SEli Cohen 
676e126ba97SEli Cohen static int mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
677e126ba97SEli Cohen {
678e126ba97SEli Cohen 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
679e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
680e126ba97SEli Cohen 	struct mlx5_uuar_info *uuari = &context->uuari;
681e126ba97SEli Cohen 	int i;
682e126ba97SEli Cohen 
683e126ba97SEli Cohen 	for (i = 0; i < uuari->num_uars; i++) {
684e126ba97SEli Cohen 		if (mlx5_cmd_free_uar(&dev->mdev, uuari->uars[i].index))
685e126ba97SEli Cohen 			mlx5_ib_warn(dev, "failed to free UAR 0x%x\n", uuari->uars[i].index);
686e126ba97SEli Cohen 	}
687e126ba97SEli Cohen 
688e126ba97SEli Cohen 	kfree(uuari->count);
689e126ba97SEli Cohen 	kfree(uuari->bitmap);
690e126ba97SEli Cohen 	kfree(uuari->uars);
691e126ba97SEli Cohen 	kfree(context);
692e126ba97SEli Cohen 
693e126ba97SEli Cohen 	return 0;
694e126ba97SEli Cohen }
695e126ba97SEli Cohen 
696e126ba97SEli Cohen static phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev, int index)
697e126ba97SEli Cohen {
698e126ba97SEli Cohen 	return (pci_resource_start(dev->mdev.pdev, 0) >> PAGE_SHIFT) + index;
699e126ba97SEli Cohen }
700e126ba97SEli Cohen 
701e126ba97SEli Cohen static int get_command(unsigned long offset)
702e126ba97SEli Cohen {
703e126ba97SEli Cohen 	return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK;
704e126ba97SEli Cohen }
705e126ba97SEli Cohen 
706e126ba97SEli Cohen static int get_arg(unsigned long offset)
707e126ba97SEli Cohen {
708e126ba97SEli Cohen 	return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1);
709e126ba97SEli Cohen }
710e126ba97SEli Cohen 
711e126ba97SEli Cohen static int get_index(unsigned long offset)
712e126ba97SEli Cohen {
713e126ba97SEli Cohen 	return get_arg(offset);
714e126ba97SEli Cohen }
715e126ba97SEli Cohen 
716e126ba97SEli Cohen static int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma)
717e126ba97SEli Cohen {
718e126ba97SEli Cohen 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
719e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
720e126ba97SEli Cohen 	struct mlx5_uuar_info *uuari = &context->uuari;
721e126ba97SEli Cohen 	unsigned long command;
722e126ba97SEli Cohen 	unsigned long idx;
723e126ba97SEli Cohen 	phys_addr_t pfn;
724e126ba97SEli Cohen 
725e126ba97SEli Cohen 	command = get_command(vma->vm_pgoff);
726e126ba97SEli Cohen 	switch (command) {
727e126ba97SEli Cohen 	case MLX5_IB_MMAP_REGULAR_PAGE:
728e126ba97SEli Cohen 		if (vma->vm_end - vma->vm_start != PAGE_SIZE)
729e126ba97SEli Cohen 			return -EINVAL;
730e126ba97SEli Cohen 
731e126ba97SEli Cohen 		idx = get_index(vma->vm_pgoff);
732e126ba97SEli Cohen 		pfn = uar_index2pfn(dev, uuari->uars[idx].index);
733e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn 0x%llx\n", idx,
734e126ba97SEli Cohen 			    (unsigned long long)pfn);
735e126ba97SEli Cohen 
736e126ba97SEli Cohen 		if (idx >= uuari->num_uars)
737e126ba97SEli Cohen 			return -EINVAL;
738e126ba97SEli Cohen 
739e126ba97SEli Cohen 		vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
740e126ba97SEli Cohen 		if (io_remap_pfn_range(vma, vma->vm_start, pfn,
741e126ba97SEli Cohen 				       PAGE_SIZE, vma->vm_page_prot))
742e126ba97SEli Cohen 			return -EAGAIN;
743e126ba97SEli Cohen 
744e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "mapped WC at 0x%lx, PA 0x%llx\n",
745e126ba97SEli Cohen 			    vma->vm_start,
746e126ba97SEli Cohen 			    (unsigned long long)pfn << PAGE_SHIFT);
747e126ba97SEli Cohen 		break;
748e126ba97SEli Cohen 
749e126ba97SEli Cohen 	case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES:
750e126ba97SEli Cohen 		return -ENOSYS;
751e126ba97SEli Cohen 
752e126ba97SEli Cohen 	default:
753e126ba97SEli Cohen 		return -EINVAL;
754e126ba97SEli Cohen 	}
755e126ba97SEli Cohen 
756e126ba97SEli Cohen 	return 0;
757e126ba97SEli Cohen }
758e126ba97SEli Cohen 
759e126ba97SEli Cohen static int alloc_pa_mkey(struct mlx5_ib_dev *dev, u32 *key, u32 pdn)
760e126ba97SEli Cohen {
761e126ba97SEli Cohen 	struct mlx5_create_mkey_mbox_in *in;
762e126ba97SEli Cohen 	struct mlx5_mkey_seg *seg;
763e126ba97SEli Cohen 	struct mlx5_core_mr mr;
764e126ba97SEli Cohen 	int err;
765e126ba97SEli Cohen 
766e126ba97SEli Cohen 	in = kzalloc(sizeof(*in), GFP_KERNEL);
767e126ba97SEli Cohen 	if (!in)
768e126ba97SEli Cohen 		return -ENOMEM;
769e126ba97SEli Cohen 
770e126ba97SEli Cohen 	seg = &in->seg;
771e126ba97SEli Cohen 	seg->flags = MLX5_PERM_LOCAL_READ | MLX5_ACCESS_MODE_PA;
772e126ba97SEli Cohen 	seg->flags_pd = cpu_to_be32(pdn | MLX5_MKEY_LEN64);
773e126ba97SEli Cohen 	seg->qpn_mkey7_0 = cpu_to_be32(0xffffff << 8);
774e126ba97SEli Cohen 	seg->start_addr = 0;
775e126ba97SEli Cohen 
776746b5583SEli Cohen 	err = mlx5_core_create_mkey(&dev->mdev, &mr, in, sizeof(*in),
777746b5583SEli Cohen 				    NULL, NULL, NULL);
778e126ba97SEli Cohen 	if (err) {
779e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed to create mkey, %d\n", err);
780e126ba97SEli Cohen 		goto err_in;
781e126ba97SEli Cohen 	}
782e126ba97SEli Cohen 
783e126ba97SEli Cohen 	kfree(in);
784e126ba97SEli Cohen 	*key = mr.key;
785e126ba97SEli Cohen 
786e126ba97SEli Cohen 	return 0;
787e126ba97SEli Cohen 
788e126ba97SEli Cohen err_in:
789e126ba97SEli Cohen 	kfree(in);
790e126ba97SEli Cohen 
791e126ba97SEli Cohen 	return err;
792e126ba97SEli Cohen }
793e126ba97SEli Cohen 
794e126ba97SEli Cohen static void free_pa_mkey(struct mlx5_ib_dev *dev, u32 key)
795e126ba97SEli Cohen {
796e126ba97SEli Cohen 	struct mlx5_core_mr mr;
797e126ba97SEli Cohen 	int err;
798e126ba97SEli Cohen 
799e126ba97SEli Cohen 	memset(&mr, 0, sizeof(mr));
800e126ba97SEli Cohen 	mr.key = key;
801e126ba97SEli Cohen 	err = mlx5_core_destroy_mkey(&dev->mdev, &mr);
802e126ba97SEli Cohen 	if (err)
803e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed to destroy mkey 0x%x\n", key);
804e126ba97SEli Cohen }
805e126ba97SEli Cohen 
806e126ba97SEli Cohen static struct ib_pd *mlx5_ib_alloc_pd(struct ib_device *ibdev,
807e126ba97SEli Cohen 				      struct ib_ucontext *context,
808e126ba97SEli Cohen 				      struct ib_udata *udata)
809e126ba97SEli Cohen {
810e126ba97SEli Cohen 	struct mlx5_ib_alloc_pd_resp resp;
811e126ba97SEli Cohen 	struct mlx5_ib_pd *pd;
812e126ba97SEli Cohen 	int err;
813e126ba97SEli Cohen 
814e126ba97SEli Cohen 	pd = kmalloc(sizeof(*pd), GFP_KERNEL);
815e126ba97SEli Cohen 	if (!pd)
816e126ba97SEli Cohen 		return ERR_PTR(-ENOMEM);
817e126ba97SEli Cohen 
818e126ba97SEli Cohen 	err = mlx5_core_alloc_pd(&to_mdev(ibdev)->mdev, &pd->pdn);
819e126ba97SEli Cohen 	if (err) {
820e126ba97SEli Cohen 		kfree(pd);
821e126ba97SEli Cohen 		return ERR_PTR(err);
822e126ba97SEli Cohen 	}
823e126ba97SEli Cohen 
824e126ba97SEli Cohen 	if (context) {
825e126ba97SEli Cohen 		resp.pdn = pd->pdn;
826e126ba97SEli Cohen 		if (ib_copy_to_udata(udata, &resp, sizeof(resp))) {
827e126ba97SEli Cohen 			mlx5_core_dealloc_pd(&to_mdev(ibdev)->mdev, pd->pdn);
828e126ba97SEli Cohen 			kfree(pd);
829e126ba97SEli Cohen 			return ERR_PTR(-EFAULT);
830e126ba97SEli Cohen 		}
831e126ba97SEli Cohen 	} else {
832e126ba97SEli Cohen 		err = alloc_pa_mkey(to_mdev(ibdev), &pd->pa_lkey, pd->pdn);
833e126ba97SEli Cohen 		if (err) {
834e126ba97SEli Cohen 			mlx5_core_dealloc_pd(&to_mdev(ibdev)->mdev, pd->pdn);
835e126ba97SEli Cohen 			kfree(pd);
836e126ba97SEli Cohen 			return ERR_PTR(err);
837e126ba97SEli Cohen 		}
838e126ba97SEli Cohen 	}
839e126ba97SEli Cohen 
840e126ba97SEli Cohen 	return &pd->ibpd;
841e126ba97SEli Cohen }
842e126ba97SEli Cohen 
843e126ba97SEli Cohen static int mlx5_ib_dealloc_pd(struct ib_pd *pd)
844e126ba97SEli Cohen {
845e126ba97SEli Cohen 	struct mlx5_ib_dev *mdev = to_mdev(pd->device);
846e126ba97SEli Cohen 	struct mlx5_ib_pd *mpd = to_mpd(pd);
847e126ba97SEli Cohen 
848e126ba97SEli Cohen 	if (!pd->uobject)
849e126ba97SEli Cohen 		free_pa_mkey(mdev, mpd->pa_lkey);
850e126ba97SEli Cohen 
851e126ba97SEli Cohen 	mlx5_core_dealloc_pd(&mdev->mdev, mpd->pdn);
852e126ba97SEli Cohen 	kfree(mpd);
853e126ba97SEli Cohen 
854e126ba97SEli Cohen 	return 0;
855e126ba97SEli Cohen }
856e126ba97SEli Cohen 
857e126ba97SEli Cohen static int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
858e126ba97SEli Cohen {
859e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
860e126ba97SEli Cohen 	int err;
861e126ba97SEli Cohen 
862e126ba97SEli Cohen 	err = mlx5_core_attach_mcg(&dev->mdev, gid, ibqp->qp_num);
863e126ba97SEli Cohen 	if (err)
864e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n",
865e126ba97SEli Cohen 			     ibqp->qp_num, gid->raw);
866e126ba97SEli Cohen 
867e126ba97SEli Cohen 	return err;
868e126ba97SEli Cohen }
869e126ba97SEli Cohen 
870e126ba97SEli Cohen static int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
871e126ba97SEli Cohen {
872e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
873e126ba97SEli Cohen 	int err;
874e126ba97SEli Cohen 
875e126ba97SEli Cohen 	err = mlx5_core_detach_mcg(&dev->mdev, gid, ibqp->qp_num);
876e126ba97SEli Cohen 	if (err)
877e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n",
878e126ba97SEli Cohen 			     ibqp->qp_num, gid->raw);
879e126ba97SEli Cohen 
880e126ba97SEli Cohen 	return err;
881e126ba97SEli Cohen }
882e126ba97SEli Cohen 
883e126ba97SEli Cohen static int init_node_data(struct mlx5_ib_dev *dev)
884e126ba97SEli Cohen {
885e126ba97SEli Cohen 	struct ib_smp *in_mad  = NULL;
886e126ba97SEli Cohen 	struct ib_smp *out_mad = NULL;
887e126ba97SEli Cohen 	int err = -ENOMEM;
888e126ba97SEli Cohen 
889e126ba97SEli Cohen 	in_mad  = kzalloc(sizeof(*in_mad), GFP_KERNEL);
890e126ba97SEli Cohen 	out_mad = kmalloc(sizeof(*out_mad), GFP_KERNEL);
891e126ba97SEli Cohen 	if (!in_mad || !out_mad)
892e126ba97SEli Cohen 		goto out;
893e126ba97SEli Cohen 
894e126ba97SEli Cohen 	init_query_mad(in_mad);
895e126ba97SEli Cohen 	in_mad->attr_id = IB_SMP_ATTR_NODE_DESC;
896e126ba97SEli Cohen 
897e126ba97SEli Cohen 	err = mlx5_MAD_IFC(dev, 1, 1, 1, NULL, NULL, in_mad, out_mad);
898e126ba97SEli Cohen 	if (err)
899e126ba97SEli Cohen 		goto out;
900e126ba97SEli Cohen 
901e126ba97SEli Cohen 	memcpy(dev->ib_dev.node_desc, out_mad->data, 64);
902e126ba97SEli Cohen 
903e126ba97SEli Cohen 	in_mad->attr_id = IB_SMP_ATTR_NODE_INFO;
904e126ba97SEli Cohen 
905e126ba97SEli Cohen 	err = mlx5_MAD_IFC(dev, 1, 1, 1, NULL, NULL, in_mad, out_mad);
906e126ba97SEli Cohen 	if (err)
907e126ba97SEli Cohen 		goto out;
908e126ba97SEli Cohen 
909e126ba97SEli Cohen 	dev->mdev.rev_id = be32_to_cpup((__be32 *)(out_mad->data + 32));
910e126ba97SEli Cohen 	memcpy(&dev->ib_dev.node_guid, out_mad->data + 12, 8);
911e126ba97SEli Cohen 
912e126ba97SEli Cohen out:
913e126ba97SEli Cohen 	kfree(in_mad);
914e126ba97SEli Cohen 	kfree(out_mad);
915e126ba97SEli Cohen 	return err;
916e126ba97SEli Cohen }
917e126ba97SEli Cohen 
918e126ba97SEli Cohen static ssize_t show_fw_pages(struct device *device, struct device_attribute *attr,
919e126ba97SEli Cohen 			     char *buf)
920e126ba97SEli Cohen {
921e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
922e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
923e126ba97SEli Cohen 
924e126ba97SEli Cohen 	return sprintf(buf, "%d\n", dev->mdev.priv.fw_pages);
925e126ba97SEli Cohen }
926e126ba97SEli Cohen 
927e126ba97SEli Cohen static ssize_t show_reg_pages(struct device *device,
928e126ba97SEli Cohen 			      struct device_attribute *attr, char *buf)
929e126ba97SEli Cohen {
930e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
931e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
932e126ba97SEli Cohen 
933e126ba97SEli Cohen 	return sprintf(buf, "%d\n", dev->mdev.priv.reg_pages);
934e126ba97SEli Cohen }
935e126ba97SEli Cohen 
936e126ba97SEli Cohen static ssize_t show_hca(struct device *device, struct device_attribute *attr,
937e126ba97SEli Cohen 			char *buf)
938e126ba97SEli Cohen {
939e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
940e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
941e126ba97SEli Cohen 	return sprintf(buf, "MT%d\n", dev->mdev.pdev->device);
942e126ba97SEli Cohen }
943e126ba97SEli Cohen 
944e126ba97SEli Cohen static ssize_t show_fw_ver(struct device *device, struct device_attribute *attr,
945e126ba97SEli Cohen 			   char *buf)
946e126ba97SEli Cohen {
947e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
948e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
949e126ba97SEli Cohen 	return sprintf(buf, "%d.%d.%d\n", fw_rev_maj(&dev->mdev),
950e126ba97SEli Cohen 		       fw_rev_min(&dev->mdev), fw_rev_sub(&dev->mdev));
951e126ba97SEli Cohen }
952e126ba97SEli Cohen 
953e126ba97SEli Cohen static ssize_t show_rev(struct device *device, struct device_attribute *attr,
954e126ba97SEli Cohen 			char *buf)
955e126ba97SEli Cohen {
956e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
957e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
958e126ba97SEli Cohen 	return sprintf(buf, "%x\n", dev->mdev.rev_id);
959e126ba97SEli Cohen }
960e126ba97SEli Cohen 
961e126ba97SEli Cohen static ssize_t show_board(struct device *device, struct device_attribute *attr,
962e126ba97SEli Cohen 			  char *buf)
963e126ba97SEli Cohen {
964e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
965e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
966e126ba97SEli Cohen 	return sprintf(buf, "%.*s\n", MLX5_BOARD_ID_LEN,
967e126ba97SEli Cohen 		       dev->mdev.board_id);
968e126ba97SEli Cohen }
969e126ba97SEli Cohen 
970e126ba97SEli Cohen static DEVICE_ATTR(hw_rev,   S_IRUGO, show_rev,    NULL);
971e126ba97SEli Cohen static DEVICE_ATTR(fw_ver,   S_IRUGO, show_fw_ver, NULL);
972e126ba97SEli Cohen static DEVICE_ATTR(hca_type, S_IRUGO, show_hca,    NULL);
973e126ba97SEli Cohen static DEVICE_ATTR(board_id, S_IRUGO, show_board,  NULL);
974e126ba97SEli Cohen static DEVICE_ATTR(fw_pages, S_IRUGO, show_fw_pages, NULL);
975e126ba97SEli Cohen static DEVICE_ATTR(reg_pages, S_IRUGO, show_reg_pages, NULL);
976e126ba97SEli Cohen 
977e126ba97SEli Cohen static struct device_attribute *mlx5_class_attributes[] = {
978e126ba97SEli Cohen 	&dev_attr_hw_rev,
979e126ba97SEli Cohen 	&dev_attr_fw_ver,
980e126ba97SEli Cohen 	&dev_attr_hca_type,
981e126ba97SEli Cohen 	&dev_attr_board_id,
982e126ba97SEli Cohen 	&dev_attr_fw_pages,
983e126ba97SEli Cohen 	&dev_attr_reg_pages,
984e126ba97SEli Cohen };
985e126ba97SEli Cohen 
986e126ba97SEli Cohen static void mlx5_ib_event(struct mlx5_core_dev *dev, enum mlx5_dev_event event,
987e126ba97SEli Cohen 			  void *data)
988e126ba97SEli Cohen {
989e126ba97SEli Cohen 	struct mlx5_ib_dev *ibdev = container_of(dev, struct mlx5_ib_dev, mdev);
990e126ba97SEli Cohen 	struct ib_event ibev;
991e126ba97SEli Cohen 	u8 port = 0;
992e126ba97SEli Cohen 
993e126ba97SEli Cohen 	switch (event) {
994e126ba97SEli Cohen 	case MLX5_DEV_EVENT_SYS_ERROR:
995e126ba97SEli Cohen 		ibdev->ib_active = false;
996e126ba97SEli Cohen 		ibev.event = IB_EVENT_DEVICE_FATAL;
997e126ba97SEli Cohen 		break;
998e126ba97SEli Cohen 
999e126ba97SEli Cohen 	case MLX5_DEV_EVENT_PORT_UP:
1000e126ba97SEli Cohen 		ibev.event = IB_EVENT_PORT_ACTIVE;
1001e126ba97SEli Cohen 		port = *(u8 *)data;
1002e126ba97SEli Cohen 		break;
1003e126ba97SEli Cohen 
1004e126ba97SEli Cohen 	case MLX5_DEV_EVENT_PORT_DOWN:
1005e126ba97SEli Cohen 		ibev.event = IB_EVENT_PORT_ERR;
1006e126ba97SEli Cohen 		port = *(u8 *)data;
1007e126ba97SEli Cohen 		break;
1008e126ba97SEli Cohen 
1009e126ba97SEli Cohen 	case MLX5_DEV_EVENT_PORT_INITIALIZED:
1010e126ba97SEli Cohen 		/* not used by ULPs */
1011e126ba97SEli Cohen 		return;
1012e126ba97SEli Cohen 
1013e126ba97SEli Cohen 	case MLX5_DEV_EVENT_LID_CHANGE:
1014e126ba97SEli Cohen 		ibev.event = IB_EVENT_LID_CHANGE;
1015e126ba97SEli Cohen 		port = *(u8 *)data;
1016e126ba97SEli Cohen 		break;
1017e126ba97SEli Cohen 
1018e126ba97SEli Cohen 	case MLX5_DEV_EVENT_PKEY_CHANGE:
1019e126ba97SEli Cohen 		ibev.event = IB_EVENT_PKEY_CHANGE;
1020e126ba97SEli Cohen 		port = *(u8 *)data;
1021e126ba97SEli Cohen 		break;
1022e126ba97SEli Cohen 
1023e126ba97SEli Cohen 	case MLX5_DEV_EVENT_GUID_CHANGE:
1024e126ba97SEli Cohen 		ibev.event = IB_EVENT_GID_CHANGE;
1025e126ba97SEli Cohen 		port = *(u8 *)data;
1026e126ba97SEli Cohen 		break;
1027e126ba97SEli Cohen 
1028e126ba97SEli Cohen 	case MLX5_DEV_EVENT_CLIENT_REREG:
1029e126ba97SEli Cohen 		ibev.event = IB_EVENT_CLIENT_REREGISTER;
1030e126ba97SEli Cohen 		port = *(u8 *)data;
1031e126ba97SEli Cohen 		break;
1032e126ba97SEli Cohen 	}
1033e126ba97SEli Cohen 
1034e126ba97SEli Cohen 	ibev.device	      = &ibdev->ib_dev;
1035e126ba97SEli Cohen 	ibev.element.port_num = port;
1036e126ba97SEli Cohen 
1037a0c84c32SEli Cohen 	if (port < 1 || port > ibdev->num_ports) {
1038a0c84c32SEli Cohen 		mlx5_ib_warn(ibdev, "warning: event on port %d\n", port);
1039a0c84c32SEli Cohen 		return;
1040a0c84c32SEli Cohen 	}
1041a0c84c32SEli Cohen 
1042e126ba97SEli Cohen 	if (ibdev->ib_active)
1043e126ba97SEli Cohen 		ib_dispatch_event(&ibev);
1044e126ba97SEli Cohen }
1045e126ba97SEli Cohen 
1046e126ba97SEli Cohen static void get_ext_port_caps(struct mlx5_ib_dev *dev)
1047e126ba97SEli Cohen {
1048e126ba97SEli Cohen 	int port;
1049e126ba97SEli Cohen 
1050e126ba97SEli Cohen 	for (port = 1; port <= dev->mdev.caps.num_ports; port++)
1051e126ba97SEli Cohen 		mlx5_query_ext_port_caps(dev, port);
1052e126ba97SEli Cohen }
1053e126ba97SEli Cohen 
1054e126ba97SEli Cohen static int get_port_caps(struct mlx5_ib_dev *dev)
1055e126ba97SEli Cohen {
1056e126ba97SEli Cohen 	struct ib_device_attr *dprops = NULL;
1057e126ba97SEli Cohen 	struct ib_port_attr *pprops = NULL;
1058e126ba97SEli Cohen 	int err = 0;
1059e126ba97SEli Cohen 	int port;
1060e126ba97SEli Cohen 
1061e126ba97SEli Cohen 	pprops = kmalloc(sizeof(*pprops), GFP_KERNEL);
1062e126ba97SEli Cohen 	if (!pprops)
1063e126ba97SEli Cohen 		goto out;
1064e126ba97SEli Cohen 
1065e126ba97SEli Cohen 	dprops = kmalloc(sizeof(*dprops), GFP_KERNEL);
1066e126ba97SEli Cohen 	if (!dprops)
1067e126ba97SEli Cohen 		goto out;
1068e126ba97SEli Cohen 
1069e126ba97SEli Cohen 	err = mlx5_ib_query_device(&dev->ib_dev, dprops);
1070e126ba97SEli Cohen 	if (err) {
1071e126ba97SEli Cohen 		mlx5_ib_warn(dev, "query_device failed %d\n", err);
1072e126ba97SEli Cohen 		goto out;
1073e126ba97SEli Cohen 	}
1074e126ba97SEli Cohen 
1075e126ba97SEli Cohen 	for (port = 1; port <= dev->mdev.caps.num_ports; port++) {
1076e126ba97SEli Cohen 		err = mlx5_ib_query_port(&dev->ib_dev, port, pprops);
1077e126ba97SEli Cohen 		if (err) {
1078e126ba97SEli Cohen 			mlx5_ib_warn(dev, "query_port %d failed %d\n", port, err);
1079e126ba97SEli Cohen 			break;
1080e126ba97SEli Cohen 		}
1081e126ba97SEli Cohen 		dev->mdev.caps.port[port - 1].pkey_table_len = dprops->max_pkeys;
1082e126ba97SEli Cohen 		dev->mdev.caps.port[port - 1].gid_table_len = pprops->gid_tbl_len;
1083e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "pkey_table_len %d, gid_table_len %d\n",
1084e126ba97SEli Cohen 			    dprops->max_pkeys, pprops->gid_tbl_len);
1085e126ba97SEli Cohen 	}
1086e126ba97SEli Cohen 
1087e126ba97SEli Cohen out:
1088e126ba97SEli Cohen 	kfree(pprops);
1089e126ba97SEli Cohen 	kfree(dprops);
1090e126ba97SEli Cohen 
1091e126ba97SEli Cohen 	return err;
1092e126ba97SEli Cohen }
1093e126ba97SEli Cohen 
1094e126ba97SEli Cohen static void destroy_umrc_res(struct mlx5_ib_dev *dev)
1095e126ba97SEli Cohen {
1096e126ba97SEli Cohen 	int err;
1097e126ba97SEli Cohen 
1098e126ba97SEli Cohen 	err = mlx5_mr_cache_cleanup(dev);
1099e126ba97SEli Cohen 	if (err)
1100e126ba97SEli Cohen 		mlx5_ib_warn(dev, "mr cache cleanup failed\n");
1101e126ba97SEli Cohen 
1102e126ba97SEli Cohen 	mlx5_ib_destroy_qp(dev->umrc.qp);
1103e126ba97SEli Cohen 	ib_destroy_cq(dev->umrc.cq);
1104e126ba97SEli Cohen 	ib_dereg_mr(dev->umrc.mr);
1105e126ba97SEli Cohen 	ib_dealloc_pd(dev->umrc.pd);
1106e126ba97SEli Cohen }
1107e126ba97SEli Cohen 
1108e126ba97SEli Cohen enum {
1109e126ba97SEli Cohen 	MAX_UMR_WR = 128,
1110e126ba97SEli Cohen };
1111e126ba97SEli Cohen 
1112e126ba97SEli Cohen static int create_umr_res(struct mlx5_ib_dev *dev)
1113e126ba97SEli Cohen {
1114e126ba97SEli Cohen 	struct ib_qp_init_attr *init_attr = NULL;
1115e126ba97SEli Cohen 	struct ib_qp_attr *attr = NULL;
1116e126ba97SEli Cohen 	struct ib_pd *pd;
1117e126ba97SEli Cohen 	struct ib_cq *cq;
1118e126ba97SEli Cohen 	struct ib_qp *qp;
1119e126ba97SEli Cohen 	struct ib_mr *mr;
1120e126ba97SEli Cohen 	int ret;
1121e126ba97SEli Cohen 
1122e126ba97SEli Cohen 	attr = kzalloc(sizeof(*attr), GFP_KERNEL);
1123e126ba97SEli Cohen 	init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL);
1124e126ba97SEli Cohen 	if (!attr || !init_attr) {
1125e126ba97SEli Cohen 		ret = -ENOMEM;
1126e126ba97SEli Cohen 		goto error_0;
1127e126ba97SEli Cohen 	}
1128e126ba97SEli Cohen 
1129e126ba97SEli Cohen 	pd = ib_alloc_pd(&dev->ib_dev);
1130e126ba97SEli Cohen 	if (IS_ERR(pd)) {
1131e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create PD for sync UMR QP\n");
1132e126ba97SEli Cohen 		ret = PTR_ERR(pd);
1133e126ba97SEli Cohen 		goto error_0;
1134e126ba97SEli Cohen 	}
1135e126ba97SEli Cohen 
1136e126ba97SEli Cohen 	mr = ib_get_dma_mr(pd,  IB_ACCESS_LOCAL_WRITE);
1137e126ba97SEli Cohen 	if (IS_ERR(mr)) {
1138e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create DMA MR for sync UMR QP\n");
1139e126ba97SEli Cohen 		ret = PTR_ERR(mr);
1140e126ba97SEli Cohen 		goto error_1;
1141e126ba97SEli Cohen 	}
1142e126ba97SEli Cohen 
1143e126ba97SEli Cohen 	cq = ib_create_cq(&dev->ib_dev, mlx5_umr_cq_handler, NULL, NULL, 128,
1144e126ba97SEli Cohen 			  0);
1145e126ba97SEli Cohen 	if (IS_ERR(cq)) {
1146e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create CQ for sync UMR QP\n");
1147e126ba97SEli Cohen 		ret = PTR_ERR(cq);
1148e126ba97SEli Cohen 		goto error_2;
1149e126ba97SEli Cohen 	}
1150e126ba97SEli Cohen 	ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
1151e126ba97SEli Cohen 
1152e126ba97SEli Cohen 	init_attr->send_cq = cq;
1153e126ba97SEli Cohen 	init_attr->recv_cq = cq;
1154e126ba97SEli Cohen 	init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
1155e126ba97SEli Cohen 	init_attr->cap.max_send_wr = MAX_UMR_WR;
1156e126ba97SEli Cohen 	init_attr->cap.max_send_sge = 1;
1157e126ba97SEli Cohen 	init_attr->qp_type = MLX5_IB_QPT_REG_UMR;
1158e126ba97SEli Cohen 	init_attr->port_num = 1;
1159e126ba97SEli Cohen 	qp = mlx5_ib_create_qp(pd, init_attr, NULL);
1160e126ba97SEli Cohen 	if (IS_ERR(qp)) {
1161e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create sync UMR QP\n");
1162e126ba97SEli Cohen 		ret = PTR_ERR(qp);
1163e126ba97SEli Cohen 		goto error_3;
1164e126ba97SEli Cohen 	}
1165e126ba97SEli Cohen 	qp->device     = &dev->ib_dev;
1166e126ba97SEli Cohen 	qp->real_qp    = qp;
1167e126ba97SEli Cohen 	qp->uobject    = NULL;
1168e126ba97SEli Cohen 	qp->qp_type    = MLX5_IB_QPT_REG_UMR;
1169e126ba97SEli Cohen 
1170e126ba97SEli Cohen 	attr->qp_state = IB_QPS_INIT;
1171e126ba97SEli Cohen 	attr->port_num = 1;
1172e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_PKEY_INDEX |
1173e126ba97SEli Cohen 				IB_QP_PORT, NULL);
1174e126ba97SEli Cohen 	if (ret) {
1175e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify UMR QP\n");
1176e126ba97SEli Cohen 		goto error_4;
1177e126ba97SEli Cohen 	}
1178e126ba97SEli Cohen 
1179e126ba97SEli Cohen 	memset(attr, 0, sizeof(*attr));
1180e126ba97SEli Cohen 	attr->qp_state = IB_QPS_RTR;
1181e126ba97SEli Cohen 	attr->path_mtu = IB_MTU_256;
1182e126ba97SEli Cohen 
1183e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
1184e126ba97SEli Cohen 	if (ret) {
1185e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify umr QP to rtr\n");
1186e126ba97SEli Cohen 		goto error_4;
1187e126ba97SEli Cohen 	}
1188e126ba97SEli Cohen 
1189e126ba97SEli Cohen 	memset(attr, 0, sizeof(*attr));
1190e126ba97SEli Cohen 	attr->qp_state = IB_QPS_RTS;
1191e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
1192e126ba97SEli Cohen 	if (ret) {
1193e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify umr QP to rts\n");
1194e126ba97SEli Cohen 		goto error_4;
1195e126ba97SEli Cohen 	}
1196e126ba97SEli Cohen 
1197e126ba97SEli Cohen 	dev->umrc.qp = qp;
1198e126ba97SEli Cohen 	dev->umrc.cq = cq;
1199e126ba97SEli Cohen 	dev->umrc.mr = mr;
1200e126ba97SEli Cohen 	dev->umrc.pd = pd;
1201e126ba97SEli Cohen 
1202e126ba97SEli Cohen 	sema_init(&dev->umrc.sem, MAX_UMR_WR);
1203e126ba97SEli Cohen 	ret = mlx5_mr_cache_init(dev);
1204e126ba97SEli Cohen 	if (ret) {
1205e126ba97SEli Cohen 		mlx5_ib_warn(dev, "mr cache init failed %d\n", ret);
1206e126ba97SEli Cohen 		goto error_4;
1207e126ba97SEli Cohen 	}
1208e126ba97SEli Cohen 
1209e126ba97SEli Cohen 	kfree(attr);
1210e126ba97SEli Cohen 	kfree(init_attr);
1211e126ba97SEli Cohen 
1212e126ba97SEli Cohen 	return 0;
1213e126ba97SEli Cohen 
1214e126ba97SEli Cohen error_4:
1215e126ba97SEli Cohen 	mlx5_ib_destroy_qp(qp);
1216e126ba97SEli Cohen 
1217e126ba97SEli Cohen error_3:
1218e126ba97SEli Cohen 	ib_destroy_cq(cq);
1219e126ba97SEli Cohen 
1220e126ba97SEli Cohen error_2:
1221e126ba97SEli Cohen 	ib_dereg_mr(mr);
1222e126ba97SEli Cohen 
1223e126ba97SEli Cohen error_1:
1224e126ba97SEli Cohen 	ib_dealloc_pd(pd);
1225e126ba97SEli Cohen 
1226e126ba97SEli Cohen error_0:
1227e126ba97SEli Cohen 	kfree(attr);
1228e126ba97SEli Cohen 	kfree(init_attr);
1229e126ba97SEli Cohen 	return ret;
1230e126ba97SEli Cohen }
1231e126ba97SEli Cohen 
1232e126ba97SEli Cohen static int create_dev_resources(struct mlx5_ib_resources *devr)
1233e126ba97SEli Cohen {
1234e126ba97SEli Cohen 	struct ib_srq_init_attr attr;
1235e126ba97SEli Cohen 	struct mlx5_ib_dev *dev;
1236e126ba97SEli Cohen 	int ret = 0;
1237e126ba97SEli Cohen 
1238e126ba97SEli Cohen 	dev = container_of(devr, struct mlx5_ib_dev, devr);
1239e126ba97SEli Cohen 
1240e126ba97SEli Cohen 	devr->p0 = mlx5_ib_alloc_pd(&dev->ib_dev, NULL, NULL);
1241e126ba97SEli Cohen 	if (IS_ERR(devr->p0)) {
1242e126ba97SEli Cohen 		ret = PTR_ERR(devr->p0);
1243e126ba97SEli Cohen 		goto error0;
1244e126ba97SEli Cohen 	}
1245e126ba97SEli Cohen 	devr->p0->device  = &dev->ib_dev;
1246e126ba97SEli Cohen 	devr->p0->uobject = NULL;
1247e126ba97SEli Cohen 	atomic_set(&devr->p0->usecnt, 0);
1248e126ba97SEli Cohen 
1249e126ba97SEli Cohen 	devr->c0 = mlx5_ib_create_cq(&dev->ib_dev, 1, 0, NULL, NULL);
1250e126ba97SEli Cohen 	if (IS_ERR(devr->c0)) {
1251e126ba97SEli Cohen 		ret = PTR_ERR(devr->c0);
1252e126ba97SEli Cohen 		goto error1;
1253e126ba97SEli Cohen 	}
1254e126ba97SEli Cohen 	devr->c0->device        = &dev->ib_dev;
1255e126ba97SEli Cohen 	devr->c0->uobject       = NULL;
1256e126ba97SEli Cohen 	devr->c0->comp_handler  = NULL;
1257e126ba97SEli Cohen 	devr->c0->event_handler = NULL;
1258e126ba97SEli Cohen 	devr->c0->cq_context    = NULL;
1259e126ba97SEli Cohen 	atomic_set(&devr->c0->usecnt, 0);
1260e126ba97SEli Cohen 
1261e126ba97SEli Cohen 	devr->x0 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
1262e126ba97SEli Cohen 	if (IS_ERR(devr->x0)) {
1263e126ba97SEli Cohen 		ret = PTR_ERR(devr->x0);
1264e126ba97SEli Cohen 		goto error2;
1265e126ba97SEli Cohen 	}
1266e126ba97SEli Cohen 	devr->x0->device = &dev->ib_dev;
1267e126ba97SEli Cohen 	devr->x0->inode = NULL;
1268e126ba97SEli Cohen 	atomic_set(&devr->x0->usecnt, 0);
1269e126ba97SEli Cohen 	mutex_init(&devr->x0->tgt_qp_mutex);
1270e126ba97SEli Cohen 	INIT_LIST_HEAD(&devr->x0->tgt_qp_list);
1271e126ba97SEli Cohen 
1272e126ba97SEli Cohen 	devr->x1 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
1273e126ba97SEli Cohen 	if (IS_ERR(devr->x1)) {
1274e126ba97SEli Cohen 		ret = PTR_ERR(devr->x1);
1275e126ba97SEli Cohen 		goto error3;
1276e126ba97SEli Cohen 	}
1277e126ba97SEli Cohen 	devr->x1->device = &dev->ib_dev;
1278e126ba97SEli Cohen 	devr->x1->inode = NULL;
1279e126ba97SEli Cohen 	atomic_set(&devr->x1->usecnt, 0);
1280e126ba97SEli Cohen 	mutex_init(&devr->x1->tgt_qp_mutex);
1281e126ba97SEli Cohen 	INIT_LIST_HEAD(&devr->x1->tgt_qp_list);
1282e126ba97SEli Cohen 
1283e126ba97SEli Cohen 	memset(&attr, 0, sizeof(attr));
1284e126ba97SEli Cohen 	attr.attr.max_sge = 1;
1285e126ba97SEli Cohen 	attr.attr.max_wr = 1;
1286e126ba97SEli Cohen 	attr.srq_type = IB_SRQT_XRC;
1287e126ba97SEli Cohen 	attr.ext.xrc.cq = devr->c0;
1288e126ba97SEli Cohen 	attr.ext.xrc.xrcd = devr->x0;
1289e126ba97SEli Cohen 
1290e126ba97SEli Cohen 	devr->s0 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
1291e126ba97SEli Cohen 	if (IS_ERR(devr->s0)) {
1292e126ba97SEli Cohen 		ret = PTR_ERR(devr->s0);
1293e126ba97SEli Cohen 		goto error4;
1294e126ba97SEli Cohen 	}
1295e126ba97SEli Cohen 	devr->s0->device	= &dev->ib_dev;
1296e126ba97SEli Cohen 	devr->s0->pd		= devr->p0;
1297e126ba97SEli Cohen 	devr->s0->uobject       = NULL;
1298e126ba97SEli Cohen 	devr->s0->event_handler = NULL;
1299e126ba97SEli Cohen 	devr->s0->srq_context   = NULL;
1300e126ba97SEli Cohen 	devr->s0->srq_type      = IB_SRQT_XRC;
1301e126ba97SEli Cohen 	devr->s0->ext.xrc.xrcd	= devr->x0;
1302e126ba97SEli Cohen 	devr->s0->ext.xrc.cq	= devr->c0;
1303e126ba97SEli Cohen 	atomic_inc(&devr->s0->ext.xrc.xrcd->usecnt);
1304e126ba97SEli Cohen 	atomic_inc(&devr->s0->ext.xrc.cq->usecnt);
1305e126ba97SEli Cohen 	atomic_inc(&devr->p0->usecnt);
1306e126ba97SEli Cohen 	atomic_set(&devr->s0->usecnt, 0);
1307e126ba97SEli Cohen 
1308e126ba97SEli Cohen 	return 0;
1309e126ba97SEli Cohen 
1310e126ba97SEli Cohen error4:
1311e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x1);
1312e126ba97SEli Cohen error3:
1313e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x0);
1314e126ba97SEli Cohen error2:
1315e126ba97SEli Cohen 	mlx5_ib_destroy_cq(devr->c0);
1316e126ba97SEli Cohen error1:
1317e126ba97SEli Cohen 	mlx5_ib_dealloc_pd(devr->p0);
1318e126ba97SEli Cohen error0:
1319e126ba97SEli Cohen 	return ret;
1320e126ba97SEli Cohen }
1321e126ba97SEli Cohen 
1322e126ba97SEli Cohen static void destroy_dev_resources(struct mlx5_ib_resources *devr)
1323e126ba97SEli Cohen {
1324e126ba97SEli Cohen 	mlx5_ib_destroy_srq(devr->s0);
1325e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x0);
1326e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x1);
1327e126ba97SEli Cohen 	mlx5_ib_destroy_cq(devr->c0);
1328e126ba97SEli Cohen 	mlx5_ib_dealloc_pd(devr->p0);
1329e126ba97SEli Cohen }
1330e126ba97SEli Cohen 
1331e126ba97SEli Cohen static int init_one(struct pci_dev *pdev,
1332e126ba97SEli Cohen 		    const struct pci_device_id *id)
1333e126ba97SEli Cohen {
1334e126ba97SEli Cohen 	struct mlx5_core_dev *mdev;
1335e126ba97SEli Cohen 	struct mlx5_ib_dev *dev;
1336e126ba97SEli Cohen 	int err;
1337e126ba97SEli Cohen 	int i;
1338e126ba97SEli Cohen 
1339e126ba97SEli Cohen 	printk_once(KERN_INFO "%s", mlx5_version);
1340e126ba97SEli Cohen 
1341e126ba97SEli Cohen 	dev = (struct mlx5_ib_dev *)ib_alloc_device(sizeof(*dev));
1342e126ba97SEli Cohen 	if (!dev)
1343e126ba97SEli Cohen 		return -ENOMEM;
1344e126ba97SEli Cohen 
1345e126ba97SEli Cohen 	mdev = &dev->mdev;
1346e126ba97SEli Cohen 	mdev->event = mlx5_ib_event;
1347e126ba97SEli Cohen 	if (prof_sel >= ARRAY_SIZE(profile)) {
1348e126ba97SEli Cohen 		pr_warn("selected pofile out of range, selceting default\n");
1349e126ba97SEli Cohen 		prof_sel = 0;
1350e126ba97SEli Cohen 	}
1351e126ba97SEli Cohen 	mdev->profile = &profile[prof_sel];
1352e126ba97SEli Cohen 	err = mlx5_dev_init(mdev, pdev);
1353e126ba97SEli Cohen 	if (err)
1354e126ba97SEli Cohen 		goto err_free;
1355e126ba97SEli Cohen 
1356e126ba97SEli Cohen 	err = get_port_caps(dev);
1357e126ba97SEli Cohen 	if (err)
1358e126ba97SEli Cohen 		goto err_cleanup;
1359e126ba97SEli Cohen 
1360e126ba97SEli Cohen 	get_ext_port_caps(dev);
1361e126ba97SEli Cohen 
1362e126ba97SEli Cohen 	err = alloc_comp_eqs(dev);
1363e126ba97SEli Cohen 	if (err)
1364e126ba97SEli Cohen 		goto err_cleanup;
1365e126ba97SEli Cohen 
1366e126ba97SEli Cohen 	MLX5_INIT_DOORBELL_LOCK(&dev->uar_lock);
1367e126ba97SEli Cohen 
1368e126ba97SEli Cohen 	strlcpy(dev->ib_dev.name, "mlx5_%d", IB_DEVICE_NAME_MAX);
1369e126ba97SEli Cohen 	dev->ib_dev.owner		= THIS_MODULE;
1370e126ba97SEli Cohen 	dev->ib_dev.node_type		= RDMA_NODE_IB_CA;
1371e126ba97SEli Cohen 	dev->ib_dev.local_dma_lkey	= mdev->caps.reserved_lkey;
1372e126ba97SEli Cohen 	dev->num_ports		= mdev->caps.num_ports;
1373e126ba97SEli Cohen 	dev->ib_dev.phys_port_cnt     = dev->num_ports;
1374e126ba97SEli Cohen 	dev->ib_dev.num_comp_vectors	= dev->num_comp_vectors;
1375e126ba97SEli Cohen 	dev->ib_dev.dma_device	= &mdev->pdev->dev;
1376e126ba97SEli Cohen 
1377e126ba97SEli Cohen 	dev->ib_dev.uverbs_abi_ver	= MLX5_IB_UVERBS_ABI_VERSION;
1378e126ba97SEli Cohen 	dev->ib_dev.uverbs_cmd_mask	=
1379e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_GET_CONTEXT)		|
1380e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_DEVICE)	|
1381e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_PORT)		|
1382e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_ALLOC_PD)		|
1383e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DEALLOC_PD)		|
1384e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_REG_MR)		|
1385e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DEREG_MR)		|
1386e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL)	|
1387e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_CQ)		|
1388e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_RESIZE_CQ)		|
1389e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_CQ)		|
1390e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_QP)		|
1391e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_MODIFY_QP)		|
1392e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_QP)		|
1393e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_QP)		|
1394e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_ATTACH_MCAST)	|
1395e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DETACH_MCAST)	|
1396e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_SRQ)		|
1397e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_MODIFY_SRQ)		|
1398e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_SRQ)		|
1399e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_SRQ)		|
1400e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_XSRQ)		|
1401e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_OPEN_QP);
1402e126ba97SEli Cohen 
1403e126ba97SEli Cohen 	dev->ib_dev.query_device	= mlx5_ib_query_device;
1404e126ba97SEli Cohen 	dev->ib_dev.query_port		= mlx5_ib_query_port;
1405e126ba97SEli Cohen 	dev->ib_dev.query_gid		= mlx5_ib_query_gid;
1406e126ba97SEli Cohen 	dev->ib_dev.query_pkey		= mlx5_ib_query_pkey;
1407e126ba97SEli Cohen 	dev->ib_dev.modify_device	= mlx5_ib_modify_device;
1408e126ba97SEli Cohen 	dev->ib_dev.modify_port		= mlx5_ib_modify_port;
1409e126ba97SEli Cohen 	dev->ib_dev.alloc_ucontext	= mlx5_ib_alloc_ucontext;
1410e126ba97SEli Cohen 	dev->ib_dev.dealloc_ucontext	= mlx5_ib_dealloc_ucontext;
1411e126ba97SEli Cohen 	dev->ib_dev.mmap		= mlx5_ib_mmap;
1412e126ba97SEli Cohen 	dev->ib_dev.alloc_pd		= mlx5_ib_alloc_pd;
1413e126ba97SEli Cohen 	dev->ib_dev.dealloc_pd		= mlx5_ib_dealloc_pd;
1414e126ba97SEli Cohen 	dev->ib_dev.create_ah		= mlx5_ib_create_ah;
1415e126ba97SEli Cohen 	dev->ib_dev.query_ah		= mlx5_ib_query_ah;
1416e126ba97SEli Cohen 	dev->ib_dev.destroy_ah		= mlx5_ib_destroy_ah;
1417e126ba97SEli Cohen 	dev->ib_dev.create_srq		= mlx5_ib_create_srq;
1418e126ba97SEli Cohen 	dev->ib_dev.modify_srq		= mlx5_ib_modify_srq;
1419e126ba97SEli Cohen 	dev->ib_dev.query_srq		= mlx5_ib_query_srq;
1420e126ba97SEli Cohen 	dev->ib_dev.destroy_srq		= mlx5_ib_destroy_srq;
1421e126ba97SEli Cohen 	dev->ib_dev.post_srq_recv	= mlx5_ib_post_srq_recv;
1422e126ba97SEli Cohen 	dev->ib_dev.create_qp		= mlx5_ib_create_qp;
1423e126ba97SEli Cohen 	dev->ib_dev.modify_qp		= mlx5_ib_modify_qp;
1424e126ba97SEli Cohen 	dev->ib_dev.query_qp		= mlx5_ib_query_qp;
1425e126ba97SEli Cohen 	dev->ib_dev.destroy_qp		= mlx5_ib_destroy_qp;
1426e126ba97SEli Cohen 	dev->ib_dev.post_send		= mlx5_ib_post_send;
1427e126ba97SEli Cohen 	dev->ib_dev.post_recv		= mlx5_ib_post_recv;
1428e126ba97SEli Cohen 	dev->ib_dev.create_cq		= mlx5_ib_create_cq;
1429e126ba97SEli Cohen 	dev->ib_dev.modify_cq		= mlx5_ib_modify_cq;
1430e126ba97SEli Cohen 	dev->ib_dev.resize_cq		= mlx5_ib_resize_cq;
1431e126ba97SEli Cohen 	dev->ib_dev.destroy_cq		= mlx5_ib_destroy_cq;
1432e126ba97SEli Cohen 	dev->ib_dev.poll_cq		= mlx5_ib_poll_cq;
1433e126ba97SEli Cohen 	dev->ib_dev.req_notify_cq	= mlx5_ib_arm_cq;
1434e126ba97SEli Cohen 	dev->ib_dev.get_dma_mr		= mlx5_ib_get_dma_mr;
1435e126ba97SEli Cohen 	dev->ib_dev.reg_user_mr		= mlx5_ib_reg_user_mr;
1436e126ba97SEli Cohen 	dev->ib_dev.dereg_mr		= mlx5_ib_dereg_mr;
14373121e3c4SSagi Grimberg 	dev->ib_dev.destroy_mr		= mlx5_ib_destroy_mr;
1438e126ba97SEli Cohen 	dev->ib_dev.attach_mcast	= mlx5_ib_mcg_attach;
1439e126ba97SEli Cohen 	dev->ib_dev.detach_mcast	= mlx5_ib_mcg_detach;
1440e126ba97SEli Cohen 	dev->ib_dev.process_mad		= mlx5_ib_process_mad;
14413121e3c4SSagi Grimberg 	dev->ib_dev.create_mr		= mlx5_ib_create_mr;
1442e126ba97SEli Cohen 	dev->ib_dev.alloc_fast_reg_mr	= mlx5_ib_alloc_fast_reg_mr;
1443e126ba97SEli Cohen 	dev->ib_dev.alloc_fast_reg_page_list = mlx5_ib_alloc_fast_reg_page_list;
1444e126ba97SEli Cohen 	dev->ib_dev.free_fast_reg_page_list  = mlx5_ib_free_fast_reg_page_list;
1445d5436ba0SSagi Grimberg 	dev->ib_dev.check_mr_status	= mlx5_ib_check_mr_status;
1446e126ba97SEli Cohen 
1447e126ba97SEli Cohen 	if (mdev->caps.flags & MLX5_DEV_CAP_FLAG_XRC) {
1448e126ba97SEli Cohen 		dev->ib_dev.alloc_xrcd = mlx5_ib_alloc_xrcd;
1449e126ba97SEli Cohen 		dev->ib_dev.dealloc_xrcd = mlx5_ib_dealloc_xrcd;
1450e126ba97SEli Cohen 		dev->ib_dev.uverbs_cmd_mask |=
1451e126ba97SEli Cohen 			(1ull << IB_USER_VERBS_CMD_OPEN_XRCD) |
1452e126ba97SEli Cohen 			(1ull << IB_USER_VERBS_CMD_CLOSE_XRCD);
1453e126ba97SEli Cohen 	}
1454e126ba97SEli Cohen 
1455e126ba97SEli Cohen 	err = init_node_data(dev);
1456e126ba97SEli Cohen 	if (err)
1457e126ba97SEli Cohen 		goto err_eqs;
1458e126ba97SEli Cohen 
1459e126ba97SEli Cohen 	mutex_init(&dev->cap_mask_mutex);
1460e126ba97SEli Cohen 	spin_lock_init(&dev->mr_lock);
1461e126ba97SEli Cohen 
1462e126ba97SEli Cohen 	err = create_dev_resources(&dev->devr);
1463e126ba97SEli Cohen 	if (err)
1464e126ba97SEli Cohen 		goto err_eqs;
1465e126ba97SEli Cohen 
1466281d1a92SWei Yongjun 	err = ib_register_device(&dev->ib_dev, NULL);
1467281d1a92SWei Yongjun 	if (err)
1468e126ba97SEli Cohen 		goto err_rsrc;
1469e126ba97SEli Cohen 
1470e126ba97SEli Cohen 	err = create_umr_res(dev);
1471e126ba97SEli Cohen 	if (err)
1472e126ba97SEli Cohen 		goto err_dev;
1473e126ba97SEli Cohen 
1474e126ba97SEli Cohen 	for (i = 0; i < ARRAY_SIZE(mlx5_class_attributes); i++) {
1475281d1a92SWei Yongjun 		err = device_create_file(&dev->ib_dev.dev,
1476281d1a92SWei Yongjun 					 mlx5_class_attributes[i]);
1477281d1a92SWei Yongjun 		if (err)
1478e126ba97SEli Cohen 			goto err_umrc;
1479e126ba97SEli Cohen 	}
1480e126ba97SEli Cohen 
1481e126ba97SEli Cohen 	dev->ib_active = true;
1482e126ba97SEli Cohen 
1483e126ba97SEli Cohen 	return 0;
1484e126ba97SEli Cohen 
1485e126ba97SEli Cohen err_umrc:
1486e126ba97SEli Cohen 	destroy_umrc_res(dev);
1487e126ba97SEli Cohen 
1488e126ba97SEli Cohen err_dev:
1489e126ba97SEli Cohen 	ib_unregister_device(&dev->ib_dev);
1490e126ba97SEli Cohen 
1491e126ba97SEli Cohen err_rsrc:
1492e126ba97SEli Cohen 	destroy_dev_resources(&dev->devr);
1493e126ba97SEli Cohen 
1494e126ba97SEli Cohen err_eqs:
1495e126ba97SEli Cohen 	free_comp_eqs(dev);
1496e126ba97SEli Cohen 
1497e126ba97SEli Cohen err_cleanup:
1498e126ba97SEli Cohen 	mlx5_dev_cleanup(mdev);
1499e126ba97SEli Cohen 
1500e126ba97SEli Cohen err_free:
1501e126ba97SEli Cohen 	ib_dealloc_device((struct ib_device *)dev);
1502e126ba97SEli Cohen 
1503e126ba97SEli Cohen 	return err;
1504e126ba97SEli Cohen }
1505e126ba97SEli Cohen 
1506e126ba97SEli Cohen static void remove_one(struct pci_dev *pdev)
1507e126ba97SEli Cohen {
1508e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = mlx5_pci2ibdev(pdev);
1509e126ba97SEli Cohen 
1510e126ba97SEli Cohen 	destroy_umrc_res(dev);
1511e126ba97SEli Cohen 	ib_unregister_device(&dev->ib_dev);
1512e126ba97SEli Cohen 	destroy_dev_resources(&dev->devr);
1513e126ba97SEli Cohen 	free_comp_eqs(dev);
1514e126ba97SEli Cohen 	mlx5_dev_cleanup(&dev->mdev);
1515e126ba97SEli Cohen 	ib_dealloc_device(&dev->ib_dev);
1516e126ba97SEli Cohen }
1517e126ba97SEli Cohen 
1518e126ba97SEli Cohen static DEFINE_PCI_DEVICE_TABLE(mlx5_ib_pci_table) = {
1519e126ba97SEli Cohen 	{ PCI_VDEVICE(MELLANOX, 4113) }, /* MT4113 Connect-IB */
1520e126ba97SEli Cohen 	{ 0, }
1521e126ba97SEli Cohen };
1522e126ba97SEli Cohen 
1523e126ba97SEli Cohen MODULE_DEVICE_TABLE(pci, mlx5_ib_pci_table);
1524e126ba97SEli Cohen 
1525e126ba97SEli Cohen static struct pci_driver mlx5_ib_driver = {
1526e126ba97SEli Cohen 	.name		= DRIVER_NAME,
1527e126ba97SEli Cohen 	.id_table	= mlx5_ib_pci_table,
1528e126ba97SEli Cohen 	.probe		= init_one,
1529e126ba97SEli Cohen 	.remove		= remove_one
1530e126ba97SEli Cohen };
1531e126ba97SEli Cohen 
1532e126ba97SEli Cohen static int __init mlx5_ib_init(void)
1533e126ba97SEli Cohen {
1534e126ba97SEli Cohen 	return pci_register_driver(&mlx5_ib_driver);
1535e126ba97SEli Cohen }
1536e126ba97SEli Cohen 
1537e126ba97SEli Cohen static void __exit mlx5_ib_cleanup(void)
1538e126ba97SEli Cohen {
1539e126ba97SEli Cohen 	pci_unregister_driver(&mlx5_ib_driver);
1540e126ba97SEli Cohen }
1541e126ba97SEli Cohen 
1542e126ba97SEli Cohen module_init(mlx5_ib_init);
1543e126ba97SEli Cohen module_exit(mlx5_ib_cleanup);
1544