xref: /freebsd/contrib/ofed/libmlx4/mlx4.c (revision a687910fc4352117413b8e0275383e4c687d4c4c)
1d6b92ffaSHans Petter Selasky /*
2d6b92ffaSHans Petter Selasky  * Copyright (c) 2007 Cisco, Inc.  All rights reserved.
3d6b92ffaSHans Petter Selasky  *
4d6b92ffaSHans Petter Selasky  * This software is available to you under a choice of one of two
5d6b92ffaSHans Petter Selasky  * licenses.  You may choose to be licensed under the terms of the GNU
6d6b92ffaSHans Petter Selasky  * General Public License (GPL) Version 2, available from the file
7d6b92ffaSHans Petter Selasky  * COPYING in the main directory of this source tree, or the
8d6b92ffaSHans Petter Selasky  * OpenIB.org BSD license below:
9d6b92ffaSHans Petter Selasky  *
10d6b92ffaSHans Petter Selasky  *     Redistribution and use in source and binary forms, with or
11d6b92ffaSHans Petter Selasky  *     without modification, are permitted provided that the following
12d6b92ffaSHans Petter Selasky  *     conditions are met:
13d6b92ffaSHans Petter Selasky  *
14d6b92ffaSHans Petter Selasky  *      - Redistributions of source code must retain the above
15d6b92ffaSHans Petter Selasky  *        copyright notice, this list of conditions and the following
16d6b92ffaSHans Petter Selasky  *        disclaimer.
17d6b92ffaSHans Petter Selasky  *
18d6b92ffaSHans Petter Selasky  *      - Redistributions in binary form must reproduce the above
19d6b92ffaSHans Petter Selasky  *        copyright notice, this list of conditions and the following
20d6b92ffaSHans Petter Selasky  *        disclaimer in the documentation and/or other materials
21d6b92ffaSHans Petter Selasky  *        provided with the distribution.
22d6b92ffaSHans Petter Selasky  *
23d6b92ffaSHans Petter Selasky  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24d6b92ffaSHans Petter Selasky  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25d6b92ffaSHans Petter Selasky  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26d6b92ffaSHans Petter Selasky  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27d6b92ffaSHans Petter Selasky  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28d6b92ffaSHans Petter Selasky  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29d6b92ffaSHans Petter Selasky  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30d6b92ffaSHans Petter Selasky  * SOFTWARE.
31d6b92ffaSHans Petter Selasky  */
32d6b92ffaSHans Petter Selasky 
33d6b92ffaSHans Petter Selasky #include <config.h>
34d6b92ffaSHans Petter Selasky 
35d6b92ffaSHans Petter Selasky #include <stdio.h>
36d6b92ffaSHans Petter Selasky #include <stdlib.h>
37d6b92ffaSHans Petter Selasky #include <unistd.h>
38d6b92ffaSHans Petter Selasky #include <errno.h>
39d6b92ffaSHans Petter Selasky #include <sys/mman.h>
40d6b92ffaSHans Petter Selasky #include <pthread.h>
41d6b92ffaSHans Petter Selasky #include <string.h>
42d6b92ffaSHans Petter Selasky 
43d6b92ffaSHans Petter Selasky #include "mlx4.h"
44d6b92ffaSHans Petter Selasky #include "mlx4-abi.h"
45d6b92ffaSHans Petter Selasky 
46d6b92ffaSHans Petter Selasky #ifndef PCI_VENDOR_ID_MELLANOX
47d6b92ffaSHans Petter Selasky #define PCI_VENDOR_ID_MELLANOX			0x15b3
48d6b92ffaSHans Petter Selasky #endif
49d6b92ffaSHans Petter Selasky 
50d6b92ffaSHans Petter Selasky #define HCA(v, d) \
51d6b92ffaSHans Petter Selasky 	{ .vendor = PCI_VENDOR_ID_##v,			\
52d6b92ffaSHans Petter Selasky 	  .device = d }
53d6b92ffaSHans Petter Selasky 
54d6b92ffaSHans Petter Selasky static struct {
55d6b92ffaSHans Petter Selasky 	unsigned		vendor;
56d6b92ffaSHans Petter Selasky 	unsigned		device;
57d6b92ffaSHans Petter Selasky } hca_table[] = {
58d6b92ffaSHans Petter Selasky 	HCA(MELLANOX, 0x6340),	/* MT25408 "Hermon" SDR */
59d6b92ffaSHans Petter Selasky 	HCA(MELLANOX, 0x634a),	/* MT25408 "Hermon" DDR */
60d6b92ffaSHans Petter Selasky 	HCA(MELLANOX, 0x6354),	/* MT25408 "Hermon" QDR */
61d6b92ffaSHans Petter Selasky 	HCA(MELLANOX, 0x6732),	/* MT25408 "Hermon" DDR PCIe gen2 */
62d6b92ffaSHans Petter Selasky 	HCA(MELLANOX, 0x673c),	/* MT25408 "Hermon" QDR PCIe gen2 */
63d6b92ffaSHans Petter Selasky 	HCA(MELLANOX, 0x6368),	/* MT25408 "Hermon" EN 10GigE */
64d6b92ffaSHans Petter Selasky 	HCA(MELLANOX, 0x6750),	/* MT25408 "Hermon" EN 10GigE PCIe gen2 */
65d6b92ffaSHans Petter Selasky 	HCA(MELLANOX, 0x6372),	/* MT25458 ConnectX EN 10GBASE-T 10GigE */
66d6b92ffaSHans Petter Selasky 	HCA(MELLANOX, 0x675a),	/* MT25458 ConnectX EN 10GBASE-T+Gen2 10GigE */
67d6b92ffaSHans Petter Selasky 	HCA(MELLANOX, 0x6764),	/* MT26468 ConnectX EN 10GigE PCIe gen2*/
68d6b92ffaSHans Petter Selasky 	HCA(MELLANOX, 0x6746),	/* MT26438 ConnectX EN 40GigE PCIe gen2 5GT/s */
69d6b92ffaSHans Petter Selasky 	HCA(MELLANOX, 0x676e),	/* MT26478 ConnectX2 40GigE PCIe gen2 */
70d6b92ffaSHans Petter Selasky 	HCA(MELLANOX, 0x1002),	/* MT25400 Family [ConnectX-2 Virtual Function] */
71d6b92ffaSHans Petter Selasky 	HCA(MELLANOX, 0x1003),	/* MT27500 Family [ConnectX-3] */
72d6b92ffaSHans Petter Selasky 	HCA(MELLANOX, 0x1004),	/* MT27500 Family [ConnectX-3 Virtual Function] */
73d6b92ffaSHans Petter Selasky 	HCA(MELLANOX, 0x1005),	/* MT27510 Family */
74d6b92ffaSHans Petter Selasky 	HCA(MELLANOX, 0x1006),	/* MT27511 Family */
75d6b92ffaSHans Petter Selasky 	HCA(MELLANOX, 0x1007),	/* MT27520 Family */
76d6b92ffaSHans Petter Selasky 	HCA(MELLANOX, 0x1008),	/* MT27521 Family */
77d6b92ffaSHans Petter Selasky 	HCA(MELLANOX, 0x1009),	/* MT27530 Family */
78d6b92ffaSHans Petter Selasky 	HCA(MELLANOX, 0x100a),	/* MT27531 Family */
79d6b92ffaSHans Petter Selasky 	HCA(MELLANOX, 0x100b),	/* MT27540 Family */
80d6b92ffaSHans Petter Selasky 	HCA(MELLANOX, 0x100c),	/* MT27541 Family */
81d6b92ffaSHans Petter Selasky 	HCA(MELLANOX, 0x100d),	/* MT27550 Family */
82d6b92ffaSHans Petter Selasky 	HCA(MELLANOX, 0x100e),	/* MT27551 Family */
83d6b92ffaSHans Petter Selasky 	HCA(MELLANOX, 0x100f),	/* MT27560 Family */
84d6b92ffaSHans Petter Selasky 	HCA(MELLANOX, 0x1010),	/* MT27561 Family */
85d6b92ffaSHans Petter Selasky };
86d6b92ffaSHans Petter Selasky 
87d6b92ffaSHans Petter Selasky static struct ibv_context_ops mlx4_ctx_ops = {
88d6b92ffaSHans Petter Selasky 	.query_device  = mlx4_query_device,
89d6b92ffaSHans Petter Selasky 	.query_port    = mlx4_query_port,
90d6b92ffaSHans Petter Selasky 	.alloc_pd      = mlx4_alloc_pd,
91d6b92ffaSHans Petter Selasky 	.dealloc_pd    = mlx4_free_pd,
92d6b92ffaSHans Petter Selasky 	.reg_mr	       = mlx4_reg_mr,
93d6b92ffaSHans Petter Selasky 	.rereg_mr      = mlx4_rereg_mr,
94d6b92ffaSHans Petter Selasky 	.dereg_mr      = mlx4_dereg_mr,
95d6b92ffaSHans Petter Selasky 	.alloc_mw      = mlx4_alloc_mw,
96d6b92ffaSHans Petter Selasky 	.dealloc_mw    = mlx4_dealloc_mw,
97d6b92ffaSHans Petter Selasky 	.bind_mw       = mlx4_bind_mw,
98d6b92ffaSHans Petter Selasky 	.create_cq     = mlx4_create_cq,
99d6b92ffaSHans Petter Selasky 	.poll_cq       = mlx4_poll_cq,
100d6b92ffaSHans Petter Selasky 	.req_notify_cq = mlx4_arm_cq,
101d6b92ffaSHans Petter Selasky 	.cq_event      = mlx4_cq_event,
102d6b92ffaSHans Petter Selasky 	.resize_cq     = mlx4_resize_cq,
103d6b92ffaSHans Petter Selasky 	.destroy_cq    = mlx4_destroy_cq,
104d6b92ffaSHans Petter Selasky 	.create_srq    = mlx4_create_srq,
105d6b92ffaSHans Petter Selasky 	.modify_srq    = mlx4_modify_srq,
106d6b92ffaSHans Petter Selasky 	.query_srq     = mlx4_query_srq,
107d6b92ffaSHans Petter Selasky 	.destroy_srq   = mlx4_destroy_srq,
108d6b92ffaSHans Petter Selasky 	.post_srq_recv = mlx4_post_srq_recv,
109d6b92ffaSHans Petter Selasky 	.create_qp     = mlx4_create_qp,
110d6b92ffaSHans Petter Selasky 	.query_qp      = mlx4_query_qp,
111d6b92ffaSHans Petter Selasky 	.modify_qp     = mlx4_modify_qp,
112d6b92ffaSHans Petter Selasky 	.destroy_qp    = mlx4_destroy_qp,
113d6b92ffaSHans Petter Selasky 	.post_send     = mlx4_post_send,
114d6b92ffaSHans Petter Selasky 	.post_recv     = mlx4_post_recv,
115d6b92ffaSHans Petter Selasky 	.create_ah     = mlx4_create_ah,
116d6b92ffaSHans Petter Selasky 	.destroy_ah    = mlx4_destroy_ah,
117d6b92ffaSHans Petter Selasky 	.attach_mcast  = ibv_cmd_attach_mcast,
118d6b92ffaSHans Petter Selasky 	.detach_mcast  = ibv_cmd_detach_mcast
119d6b92ffaSHans Petter Selasky };
120d6b92ffaSHans Petter Selasky 
mlx4_map_internal_clock(struct mlx4_device * mdev,struct ibv_context * ibv_ctx)121d6b92ffaSHans Petter Selasky static int mlx4_map_internal_clock(struct mlx4_device *mdev,
122d6b92ffaSHans Petter Selasky 				   struct ibv_context *ibv_ctx)
123d6b92ffaSHans Petter Selasky {
124d6b92ffaSHans Petter Selasky 	struct mlx4_context *context = to_mctx(ibv_ctx);
125d6b92ffaSHans Petter Selasky 	void *hca_clock_page;
126d6b92ffaSHans Petter Selasky 
127d6b92ffaSHans Petter Selasky 	hca_clock_page = mmap(NULL, mdev->page_size,
128d6b92ffaSHans Petter Selasky 			      PROT_READ, MAP_SHARED, ibv_ctx->cmd_fd,
129d6b92ffaSHans Petter Selasky 			      mdev->page_size * 3);
130d6b92ffaSHans Petter Selasky 
131d6b92ffaSHans Petter Selasky 	if (hca_clock_page == MAP_FAILED) {
132d6b92ffaSHans Petter Selasky 		fprintf(stderr, PFX
133d6b92ffaSHans Petter Selasky 			"Warning: Timestamp available,\n"
134d6b92ffaSHans Petter Selasky 			"but failed to mmap() hca core clock page.\n");
135d6b92ffaSHans Petter Selasky 		return -1;
136d6b92ffaSHans Petter Selasky 	}
137d6b92ffaSHans Petter Selasky 
138d6b92ffaSHans Petter Selasky 	context->hca_core_clock = hca_clock_page +
139d6b92ffaSHans Petter Selasky 		(context->core_clock.offset & (mdev->page_size - 1));
140d6b92ffaSHans Petter Selasky 	return 0;
141d6b92ffaSHans Petter Selasky }
142d6b92ffaSHans Petter Selasky 
mlx4_init_context(struct verbs_device * v_device,struct ibv_context * ibv_ctx,int cmd_fd)143d6b92ffaSHans Petter Selasky static int mlx4_init_context(struct verbs_device *v_device,
144d6b92ffaSHans Petter Selasky 				struct ibv_context *ibv_ctx, int cmd_fd)
145d6b92ffaSHans Petter Selasky {
146d6b92ffaSHans Petter Selasky 	struct mlx4_context	       *context;
147d6b92ffaSHans Petter Selasky 	struct ibv_get_context		cmd;
148d6b92ffaSHans Petter Selasky 	struct mlx4_alloc_ucontext_resp resp;
149d6b92ffaSHans Petter Selasky 	int				i;
150*a687910fSSean Lim 	int				ret;
151d6b92ffaSHans Petter Selasky 	struct mlx4_alloc_ucontext_resp_v3 resp_v3;
152d6b92ffaSHans Petter Selasky 	__u16				bf_reg_size;
153d6b92ffaSHans Petter Selasky 	struct mlx4_device              *dev = to_mdev(&v_device->device);
154d6b92ffaSHans Petter Selasky 	struct verbs_context *verbs_ctx = verbs_get_ctx(ibv_ctx);
155d6b92ffaSHans Petter Selasky 	struct ibv_device_attr_ex	dev_attrs;
156d6b92ffaSHans Petter Selasky 
157d6b92ffaSHans Petter Selasky 	/* memory footprint of mlx4_context and verbs_context share
158d6b92ffaSHans Petter Selasky 	* struct ibv_context.
159d6b92ffaSHans Petter Selasky 	*/
160d6b92ffaSHans Petter Selasky 	context = to_mctx(ibv_ctx);
161d6b92ffaSHans Petter Selasky 	ibv_ctx->cmd_fd = cmd_fd;
162d6b92ffaSHans Petter Selasky 
163d6b92ffaSHans Petter Selasky 	if (dev->abi_version <= MLX4_UVERBS_NO_DEV_CAPS_ABI_VERSION) {
164d6b92ffaSHans Petter Selasky 		if (ibv_cmd_get_context(ibv_ctx, &cmd, sizeof cmd,
165d6b92ffaSHans Petter Selasky 					&resp_v3.ibv_resp, sizeof resp_v3))
166d6b92ffaSHans Petter Selasky 			return errno;
167d6b92ffaSHans Petter Selasky 
168d6b92ffaSHans Petter Selasky 		context->num_qps  = resp_v3.qp_tab_size;
169d6b92ffaSHans Petter Selasky 		bf_reg_size	  = resp_v3.bf_reg_size;
170d6b92ffaSHans Petter Selasky 		context->cqe_size = sizeof (struct mlx4_cqe);
171d6b92ffaSHans Petter Selasky 	} else  {
172d6b92ffaSHans Petter Selasky 		if (ibv_cmd_get_context(ibv_ctx, &cmd, sizeof cmd,
173d6b92ffaSHans Petter Selasky 					&resp.ibv_resp, sizeof resp))
174d6b92ffaSHans Petter Selasky 			return errno;
175d6b92ffaSHans Petter Selasky 
176d6b92ffaSHans Petter Selasky 		context->num_qps  = resp.qp_tab_size;
177d6b92ffaSHans Petter Selasky 		bf_reg_size	  = resp.bf_reg_size;
178d6b92ffaSHans Petter Selasky 		if (resp.dev_caps & MLX4_USER_DEV_CAP_64B_CQE)
179d6b92ffaSHans Petter Selasky 			context->cqe_size = resp.cqe_size;
180d6b92ffaSHans Petter Selasky 		else
181d6b92ffaSHans Petter Selasky 			context->cqe_size = sizeof (struct mlx4_cqe);
182d6b92ffaSHans Petter Selasky 	}
183d6b92ffaSHans Petter Selasky 
184d6b92ffaSHans Petter Selasky 	context->qp_table_shift = ffs(context->num_qps) - 1 - MLX4_QP_TABLE_BITS;
185d6b92ffaSHans Petter Selasky 	context->qp_table_mask	= (1 << context->qp_table_shift) - 1;
186d6b92ffaSHans Petter Selasky 	for (i = 0; i < MLX4_PORTS_NUM; ++i)
187d6b92ffaSHans Petter Selasky 		context->port_query_cache[i].valid = 0;
188d6b92ffaSHans Petter Selasky 
189*a687910fSSean Lim 	ret = pthread_mutex_init(&context->qp_table_mutex, NULL);
190*a687910fSSean Lim 	if (ret)
191*a687910fSSean Lim 		return ret;
192d6b92ffaSHans Petter Selasky 	for (i = 0; i < MLX4_QP_TABLE_SIZE; ++i)
193d6b92ffaSHans Petter Selasky 		context->qp_table[i].refcnt = 0;
194d6b92ffaSHans Petter Selasky 
195d6b92ffaSHans Petter Selasky 	for (i = 0; i < MLX4_NUM_DB_TYPE; ++i)
196d6b92ffaSHans Petter Selasky 		context->db_list[i] = NULL;
197d6b92ffaSHans Petter Selasky 
198*a687910fSSean Lim 	ret = mlx4_init_xsrq_table(&context->xsrq_table, context->num_qps);
199*a687910fSSean Lim 	if (ret)
200*a687910fSSean Lim 		goto err;
201*a687910fSSean Lim 
202*a687910fSSean Lim 	ret = pthread_mutex_init(&context->db_list_mutex, NULL);
203*a687910fSSean Lim 	if (ret)
204*a687910fSSean Lim 		goto err_xsrq;
205d6b92ffaSHans Petter Selasky 
206d6b92ffaSHans Petter Selasky 	context->uar = mmap(NULL, dev->page_size, PROT_WRITE,
207d6b92ffaSHans Petter Selasky 			    MAP_SHARED, cmd_fd, 0);
208d6b92ffaSHans Petter Selasky 	if (context->uar == MAP_FAILED)
209d6b92ffaSHans Petter Selasky 		return errno;
210d6b92ffaSHans Petter Selasky 
211d6b92ffaSHans Petter Selasky 	if (bf_reg_size) {
212d6b92ffaSHans Petter Selasky 		context->bf_page = mmap(NULL, dev->page_size,
213d6b92ffaSHans Petter Selasky 					PROT_WRITE, MAP_SHARED, cmd_fd,
214d6b92ffaSHans Petter Selasky 					dev->page_size);
215d6b92ffaSHans Petter Selasky 		if (context->bf_page == MAP_FAILED) {
216d6b92ffaSHans Petter Selasky 			fprintf(stderr, PFX "Warning: BlueFlame available, "
217d6b92ffaSHans Petter Selasky 				"but failed to mmap() BlueFlame page.\n");
218d6b92ffaSHans Petter Selasky 				context->bf_page     = NULL;
219d6b92ffaSHans Petter Selasky 				context->bf_buf_size = 0;
220d6b92ffaSHans Petter Selasky 		} else {
221d6b92ffaSHans Petter Selasky 			context->bf_buf_size = bf_reg_size / 2;
222d6b92ffaSHans Petter Selasky 			context->bf_offset   = 0;
223*a687910fSSean Lim 			ret = pthread_spin_init(&context->bf_lock, PTHREAD_PROCESS_PRIVATE);
224*a687910fSSean Lim 			if (ret)
225*a687910fSSean Lim 				goto err_db_list;
226d6b92ffaSHans Petter Selasky 		}
227d6b92ffaSHans Petter Selasky 	} else {
228d6b92ffaSHans Petter Selasky 		context->bf_page     = NULL;
229d6b92ffaSHans Petter Selasky 		context->bf_buf_size = 0;
230d6b92ffaSHans Petter Selasky 	}
231d6b92ffaSHans Petter Selasky 
232*a687910fSSean Lim 	ret = pthread_spin_init(&context->uar_lock, PTHREAD_PROCESS_PRIVATE);
233*a687910fSSean Lim 	if (ret)
234*a687910fSSean Lim 		goto err_bf_lock;
235d6b92ffaSHans Petter Selasky 	ibv_ctx->ops = mlx4_ctx_ops;
236d6b92ffaSHans Petter Selasky 
237d6b92ffaSHans Petter Selasky 	context->hca_core_clock = NULL;
238d6b92ffaSHans Petter Selasky 	memset(&dev_attrs, 0, sizeof(dev_attrs));
239d6b92ffaSHans Petter Selasky 	if (!mlx4_query_device_ex(ibv_ctx, NULL, &dev_attrs,
240d6b92ffaSHans Petter Selasky 				  sizeof(struct ibv_device_attr_ex))) {
241d6b92ffaSHans Petter Selasky 		context->max_qp_wr = dev_attrs.orig_attr.max_qp_wr;
242d6b92ffaSHans Petter Selasky 		context->max_sge = dev_attrs.orig_attr.max_sge;
243d6b92ffaSHans Petter Selasky 		if (context->core_clock.offset_valid)
244d6b92ffaSHans Petter Selasky 			mlx4_map_internal_clock(dev, ibv_ctx);
245d6b92ffaSHans Petter Selasky 	}
246d6b92ffaSHans Petter Selasky 
247d6b92ffaSHans Petter Selasky 	verbs_ctx->has_comp_mask = VERBS_CONTEXT_XRCD | VERBS_CONTEXT_SRQ |
248d6b92ffaSHans Petter Selasky 					VERBS_CONTEXT_QP;
249d6b92ffaSHans Petter Selasky 	verbs_set_ctx_op(verbs_ctx, close_xrcd, mlx4_close_xrcd);
250d6b92ffaSHans Petter Selasky 	verbs_set_ctx_op(verbs_ctx, open_xrcd, mlx4_open_xrcd);
251d6b92ffaSHans Petter Selasky 	verbs_set_ctx_op(verbs_ctx, create_srq_ex, mlx4_create_srq_ex);
252d6b92ffaSHans Petter Selasky 	verbs_set_ctx_op(verbs_ctx, get_srq_num, verbs_get_srq_num);
253d6b92ffaSHans Petter Selasky 	verbs_set_ctx_op(verbs_ctx, create_qp_ex, mlx4_create_qp_ex);
254d6b92ffaSHans Petter Selasky 	verbs_set_ctx_op(verbs_ctx, open_qp, mlx4_open_qp);
255d6b92ffaSHans Petter Selasky 	verbs_set_ctx_op(verbs_ctx, ibv_create_flow, ibv_cmd_create_flow);
256d6b92ffaSHans Petter Selasky 	verbs_set_ctx_op(verbs_ctx, ibv_destroy_flow, ibv_cmd_destroy_flow);
257d6b92ffaSHans Petter Selasky 	verbs_set_ctx_op(verbs_ctx, create_cq_ex, mlx4_create_cq_ex);
258d6b92ffaSHans Petter Selasky 	verbs_set_ctx_op(verbs_ctx, query_device_ex, mlx4_query_device_ex);
259d6b92ffaSHans Petter Selasky 	verbs_set_ctx_op(verbs_ctx, query_rt_values, mlx4_query_rt_values);
260d6b92ffaSHans Petter Selasky 
261d6b92ffaSHans Petter Selasky 	return 0;
262d6b92ffaSHans Petter Selasky 
263*a687910fSSean Lim err_bf_lock:
264*a687910fSSean Lim 	if (context->bf_buf_size)
265*a687910fSSean Lim 		pthread_spin_destroy(&context->bf_lock);
266*a687910fSSean Lim err_db_list:
267*a687910fSSean Lim 	pthread_mutex_destroy(&context->db_list_mutex);
268*a687910fSSean Lim err_xsrq:
269*a687910fSSean Lim 	mlx4_cleanup_xsrq_table(&context->xsrq_table);
270*a687910fSSean Lim err:
271*a687910fSSean Lim 	pthread_mutex_destroy(&context->qp_table_mutex);
272*a687910fSSean Lim 
273*a687910fSSean Lim 	return ret;
274d6b92ffaSHans Petter Selasky }
275d6b92ffaSHans Petter Selasky 
mlx4_uninit_context(struct verbs_device * v_device,struct ibv_context * ibv_ctx)276d6b92ffaSHans Petter Selasky static void mlx4_uninit_context(struct verbs_device *v_device,
277d6b92ffaSHans Petter Selasky 					struct ibv_context *ibv_ctx)
278d6b92ffaSHans Petter Selasky {
279d6b92ffaSHans Petter Selasky 	struct mlx4_context *context = to_mctx(ibv_ctx);
280d6b92ffaSHans Petter Selasky 
281*a687910fSSean Lim 	pthread_mutex_destroy(&context->qp_table_mutex);
282*a687910fSSean Lim 	mlx4_cleanup_xsrq_table(&context->xsrq_table);
283*a687910fSSean Lim 	pthread_mutex_destroy(&context->db_list_mutex);
284*a687910fSSean Lim 	pthread_spin_destroy(&context->bf_lock);
285*a687910fSSean Lim 	pthread_spin_destroy(&context->uar_lock);
286*a687910fSSean Lim 
287d6b92ffaSHans Petter Selasky 	munmap(context->uar, to_mdev(&v_device->device)->page_size);
288d6b92ffaSHans Petter Selasky 	if (context->bf_page)
289d6b92ffaSHans Petter Selasky 		munmap(context->bf_page, to_mdev(&v_device->device)->page_size);
290d6b92ffaSHans Petter Selasky 	if (context->hca_core_clock)
291d6b92ffaSHans Petter Selasky 		munmap(context->hca_core_clock - context->core_clock.offset,
292d6b92ffaSHans Petter Selasky 		       to_mdev(&v_device->device)->page_size);
293d6b92ffaSHans Petter Selasky }
294d6b92ffaSHans Petter Selasky 
295d6b92ffaSHans Petter Selasky static struct verbs_device_ops mlx4_dev_ops = {
296d6b92ffaSHans Petter Selasky 	.init_context = mlx4_init_context,
297d6b92ffaSHans Petter Selasky 	.uninit_context = mlx4_uninit_context,
298d6b92ffaSHans Petter Selasky };
299d6b92ffaSHans Petter Selasky 
mlx4_driver_init(const char * uverbs_sys_path,int abi_version)300d6b92ffaSHans Petter Selasky static struct verbs_device *mlx4_driver_init(const char *uverbs_sys_path, int abi_version)
301d6b92ffaSHans Petter Selasky {
302d6b92ffaSHans Petter Selasky 	char			value[8];
303d6b92ffaSHans Petter Selasky 	struct mlx4_device    *dev;
304d6b92ffaSHans Petter Selasky 	unsigned		vendor, device;
305d6b92ffaSHans Petter Selasky 	int			i;
306d6b92ffaSHans Petter Selasky 
307d6b92ffaSHans Petter Selasky 	if (ibv_read_sysfs_file(uverbs_sys_path, "device/vendor",
308d6b92ffaSHans Petter Selasky 				value, sizeof value) < 0)
309d6b92ffaSHans Petter Selasky 		return NULL;
310d6b92ffaSHans Petter Selasky 	vendor = strtol(value, NULL, 16);
311d6b92ffaSHans Petter Selasky 
312d6b92ffaSHans Petter Selasky 	if (ibv_read_sysfs_file(uverbs_sys_path, "device/device",
313d6b92ffaSHans Petter Selasky 				value, sizeof value) < 0)
314d6b92ffaSHans Petter Selasky 		return NULL;
315d6b92ffaSHans Petter Selasky 	device = strtol(value, NULL, 16);
316d6b92ffaSHans Petter Selasky 
317d6b92ffaSHans Petter Selasky 	for (i = 0; i < sizeof hca_table / sizeof hca_table[0]; ++i)
318d6b92ffaSHans Petter Selasky 		if (vendor == hca_table[i].vendor &&
319d6b92ffaSHans Petter Selasky 		    device == hca_table[i].device)
320d6b92ffaSHans Petter Selasky 			goto found;
321d6b92ffaSHans Petter Selasky 
322d6b92ffaSHans Petter Selasky 	return NULL;
323d6b92ffaSHans Petter Selasky 
324d6b92ffaSHans Petter Selasky found:
325d6b92ffaSHans Petter Selasky 	if (abi_version < MLX4_UVERBS_MIN_ABI_VERSION ||
326d6b92ffaSHans Petter Selasky 	    abi_version > MLX4_UVERBS_MAX_ABI_VERSION) {
327d6b92ffaSHans Petter Selasky 		fprintf(stderr, PFX "Fatal: ABI version %d of %s is not supported "
328d6b92ffaSHans Petter Selasky 			"(min supported %d, max supported %d)\n",
329d6b92ffaSHans Petter Selasky 			abi_version, uverbs_sys_path,
330d6b92ffaSHans Petter Selasky 			MLX4_UVERBS_MIN_ABI_VERSION,
331d6b92ffaSHans Petter Selasky 			MLX4_UVERBS_MAX_ABI_VERSION);
332d6b92ffaSHans Petter Selasky 		return NULL;
333d6b92ffaSHans Petter Selasky 	}
334d6b92ffaSHans Petter Selasky 
335d6b92ffaSHans Petter Selasky 	dev = calloc(1, sizeof *dev);
336d6b92ffaSHans Petter Selasky 	if (!dev) {
337d6b92ffaSHans Petter Selasky 		fprintf(stderr, PFX "Fatal: couldn't allocate device for %s\n",
338d6b92ffaSHans Petter Selasky 			uverbs_sys_path);
339d6b92ffaSHans Petter Selasky 		return NULL;
340d6b92ffaSHans Petter Selasky 	}
341d6b92ffaSHans Petter Selasky 
342d6b92ffaSHans Petter Selasky 	dev->page_size   = sysconf(_SC_PAGESIZE);
343d6b92ffaSHans Petter Selasky 	dev->abi_version = abi_version;
344d6b92ffaSHans Petter Selasky 
345d6b92ffaSHans Petter Selasky 	dev->verbs_dev.ops = &mlx4_dev_ops;
346d6b92ffaSHans Petter Selasky 	dev->verbs_dev.sz = sizeof(*dev);
347d6b92ffaSHans Petter Selasky 	dev->verbs_dev.size_of_context =
348d6b92ffaSHans Petter Selasky 		sizeof(struct mlx4_context) - sizeof(struct ibv_context);
349d6b92ffaSHans Petter Selasky 
350d6b92ffaSHans Petter Selasky 	return &dev->verbs_dev;
351d6b92ffaSHans Petter Selasky }
352d6b92ffaSHans Petter Selasky 
mlx4_register_driver(void)353d6b92ffaSHans Petter Selasky static __attribute__((constructor)) void mlx4_register_driver(void)
354d6b92ffaSHans Petter Selasky {
355d6b92ffaSHans Petter Selasky 	verbs_register_driver("mlx4", mlx4_driver_init);
356d6b92ffaSHans Petter Selasky }
357