1 /*
2 * Copyright (c) 2004, 2005 Topspin Communications. All rights reserved.
3 * Copyright (c) 2006 Cisco Systems. All rights reserved.
4 *
5 * This software is available to you under a choice of one of two
6 * licenses. You may choose to be licensed under the terms of the GNU
7 * General Public License (GPL) Version 2, available from the file
8 * COPYING in the main directory of this source tree, or the
9 * OpenIB.org BSD license below:
10 *
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
13 * conditions are met:
14 *
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer.
18 *
19 * - Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 * SOFTWARE.
32 */
33
34 #if HAVE_CONFIG_H
35 # include <config.h>
36 #endif /* HAVE_CONFIG_H */
37
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <unistd.h>
41 #include <errno.h>
42 #include <sys/mman.h>
43 #include <pthread.h>
44 #include <string.h>
45
46 #ifndef HAVE_IBV_REGISTER_DRIVER
47 #include <sysfs/libsysfs.h>
48 #endif
49
50 #ifndef HAVE_IBV_READ_SYSFS_FILE
51 #include <sys/types.h>
52 #include <sys/stat.h>
53 #include <fcntl.h>
54 #endif
55
56 #include "mthca.h"
57 #include "mthca-abi.h"
58
59 #ifndef PCI_VENDOR_ID_MELLANOX
60 #define PCI_VENDOR_ID_MELLANOX 0x15b3
61 #endif
62
63 #ifndef PCI_DEVICE_ID_MELLANOX_TAVOR
64 #define PCI_DEVICE_ID_MELLANOX_TAVOR 0x5a44
65 #endif
66
67 #ifndef PCI_DEVICE_ID_MELLANOX_ARBEL_COMPAT
68 #define PCI_DEVICE_ID_MELLANOX_ARBEL_COMPAT 0x6278
69 #endif
70
71 #ifndef PCI_DEVICE_ID_MELLANOX_ARBEL
72 #define PCI_DEVICE_ID_MELLANOX_ARBEL 0x6282
73 #endif
74
75 #ifndef PCI_DEVICE_ID_MELLANOX_SINAI_OLD
76 #define PCI_DEVICE_ID_MELLANOX_SINAI_OLD 0x5e8c
77 #endif
78
79 #ifndef PCI_DEVICE_ID_MELLANOX_SINAI
80 #define PCI_DEVICE_ID_MELLANOX_SINAI 0x6274
81 #endif
82
83 #ifndef PCI_VENDOR_ID_TOPSPIN
84 #define PCI_VENDOR_ID_TOPSPIN 0x1867
85 #endif
86
87 #define HCA(v, d, t) \
88 { .vendor = PCI_VENDOR_ID_##v, \
89 .device = PCI_DEVICE_ID_MELLANOX_##d, \
90 .type = MTHCA_##t }
91
92 struct {
93 unsigned vendor;
94 unsigned device;
95 enum mthca_hca_type type;
96 } hca_table[] = {
97 HCA(MELLANOX, TAVOR, TAVOR),
98 HCA(MELLANOX, ARBEL_COMPAT, TAVOR),
99 HCA(MELLANOX, ARBEL, ARBEL),
100 HCA(MELLANOX, SINAI_OLD, ARBEL),
101 HCA(MELLANOX, SINAI, ARBEL),
102 HCA(TOPSPIN, TAVOR, TAVOR),
103 HCA(TOPSPIN, ARBEL_COMPAT, TAVOR),
104 HCA(TOPSPIN, ARBEL, ARBEL),
105 HCA(TOPSPIN, SINAI_OLD, ARBEL),
106 HCA(TOPSPIN, SINAI, ARBEL),
107 };
108
109 static struct ibv_context_ops mthca_ctx_ops = {
110 .query_device = mthca_query_device,
111 .query_port = mthca_query_port,
112 .alloc_pd = mthca_alloc_pd,
113 .dealloc_pd = mthca_free_pd,
114 .reg_mr = mthca_reg_mr,
115 .dereg_mr = mthca_dereg_mr,
116 .create_cq = mthca_create_cq,
117 .poll_cq = mthca_poll_cq,
118 .resize_cq = mthca_resize_cq,
119 .destroy_cq = mthca_destroy_cq,
120 .create_srq = mthca_create_srq,
121 .modify_srq = mthca_modify_srq,
122 .query_srq = mthca_query_srq,
123 .destroy_srq = mthca_destroy_srq,
124 .create_qp = mthca_create_qp,
125 .query_qp = mthca_query_qp,
126 .modify_qp = mthca_modify_qp,
127 .destroy_qp = mthca_destroy_qp,
128 .create_ah = mthca_create_ah,
129 .destroy_ah = mthca_destroy_ah,
130 .attach_mcast = mthca_attach_mcast,
131 .detach_mcast = mthca_detach_mcast
132 };
133
mthca_alloc_context(struct ibv_device * ibdev,int cmd_fd)134 static struct ibv_context *mthca_alloc_context(struct ibv_device *ibdev, int cmd_fd)
135 {
136 struct mthca_context *context;
137 struct ibv_get_context cmd;
138 struct mthca_alloc_ucontext_resp resp;
139 int i;
140
141 context = calloc(1, sizeof *context);
142 if (!context)
143 return NULL;
144
145 context->ibv_ctx.cmd_fd = cmd_fd;
146
147 if (ibv_cmd_get_context(&context->ibv_ctx, &cmd, sizeof cmd,
148 &resp.ibv_resp, sizeof resp))
149 goto err_free;
150
151 context->num_qps = resp.qp_tab_size;
152 context->qp_table_shift = ffs(context->num_qps) - 1 - MTHCA_QP_TABLE_BITS;
153 context->qp_table_mask = (1 << context->qp_table_shift) - 1;
154
155 /*
156 * Need to set ibv_ctx.device because mthca_is_memfree() will
157 * look at it to figure out the HCA type.
158 */
159 context->ibv_ctx.device = ibdev;
160
161 if (mthca_is_memfree(&context->ibv_ctx)) {
162 context->db_tab = mthca_alloc_db_tab(resp.uarc_size);
163 if (!context->db_tab)
164 goto err_free;
165 } else
166 context->db_tab = NULL;
167
168 pthread_mutex_init(&context->qp_table_mutex, NULL);
169 for (i = 0; i < MTHCA_QP_TABLE_SIZE; ++i)
170 context->qp_table[i].refcnt = 0;
171
172 context->uar = mmap(NULL, to_mdev(ibdev)->page_size, PROT_WRITE,
173 MAP_SHARED, cmd_fd, 0);
174 if (context->uar == MAP_FAILED)
175 goto err_db_tab;
176
177 pthread_spin_init(&context->uar_lock, PTHREAD_PROCESS_PRIVATE);
178
179 context->pd = mthca_alloc_pd(&context->ibv_ctx);
180 if (!context->pd)
181 goto err_unmap;
182
183 context->pd->context = &context->ibv_ctx;
184
185 context->ibv_ctx.ops = mthca_ctx_ops;
186
187 if (mthca_is_memfree(&context->ibv_ctx)) {
188 context->ibv_ctx.ops.req_notify_cq = mthca_arbel_arm_cq;
189 context->ibv_ctx.ops.cq_event = mthca_arbel_cq_event;
190 context->ibv_ctx.ops.post_send = mthca_arbel_post_send;
191 context->ibv_ctx.ops.post_recv = mthca_arbel_post_recv;
192 context->ibv_ctx.ops.post_srq_recv = mthca_arbel_post_srq_recv;
193 } else {
194 context->ibv_ctx.ops.req_notify_cq = mthca_tavor_arm_cq;
195 context->ibv_ctx.ops.cq_event = NULL;
196 context->ibv_ctx.ops.post_send = mthca_tavor_post_send;
197 context->ibv_ctx.ops.post_recv = mthca_tavor_post_recv;
198 context->ibv_ctx.ops.post_srq_recv = mthca_tavor_post_srq_recv;
199 }
200
201 return &context->ibv_ctx;
202
203 err_unmap:
204 munmap(context->uar, to_mdev(ibdev)->page_size);
205
206 err_db_tab:
207 mthca_free_db_tab(context->db_tab);
208
209 err_free:
210 free(context);
211 return NULL;
212 }
213
mthca_free_context(struct ibv_context * ibctx)214 static void mthca_free_context(struct ibv_context *ibctx)
215 {
216 struct mthca_context *context = to_mctx(ibctx);
217
218 mthca_free_pd(context->pd);
219 munmap(context->uar, to_mdev(ibctx->device)->page_size);
220 mthca_free_db_tab(context->db_tab);
221 free(context);
222 }
223
224 static struct ibv_device_ops mthca_dev_ops = {
225 .alloc_context = mthca_alloc_context,
226 .free_context = mthca_free_context
227 };
228
229 /*
230 * Keep a private implementation of HAVE_IBV_READ_SYSFS_FILE to handle
231 * old versions of libibverbs that didn't implement it. This can be
232 * removed when libibverbs 1.0.3 or newer is available "everywhere."
233 */
234 #ifndef HAVE_IBV_READ_SYSFS_FILE
ibv_read_sysfs_file(const char * dir,const char * file,char * buf,size_t size)235 static int ibv_read_sysfs_file(const char *dir, const char *file,
236 char *buf, size_t size)
237 {
238 char path[256];
239 int fd;
240 int len;
241
242 snprintf(path, sizeof path, "%s/%s", dir, file);
243
244 fd = open(path, O_RDONLY);
245 if (fd < 0)
246 return -1;
247
248 len = read(fd, buf, size);
249
250 close(fd);
251
252 if (len > 0 && buf[len - 1] == '\n')
253 buf[--len] = '\0';
254
255 return len;
256 }
257 #endif /* HAVE_IBV_READ_SYSFS_FILE */
258
mthca_driver_init(const char * uverbs_sys_path,int abi_version)259 static struct ibv_device *mthca_driver_init(const char *uverbs_sys_path,
260 int abi_version)
261 {
262 char value[8];
263 struct mthca_device *dev;
264 unsigned vendor, device;
265 int i;
266
267 if (ibv_read_sysfs_file(uverbs_sys_path, "device/vendor",
268 value, sizeof value) < 0)
269 return NULL;
270 sscanf(value, "%i", &vendor);
271
272 if (ibv_read_sysfs_file(uverbs_sys_path, "device/device",
273 value, sizeof value) < 0)
274 return NULL;
275 sscanf(value, "%i", &device);
276
277 for (i = 0; i < sizeof hca_table / sizeof hca_table[0]; ++i)
278 if (vendor == hca_table[i].vendor &&
279 device == hca_table[i].device)
280 goto found;
281
282 return NULL;
283
284 found:
285 if (abi_version > MTHCA_UVERBS_ABI_VERSION) {
286 fprintf(stderr, PFX "Fatal: ABI version %d of %s is too new (expected %d)\n",
287 abi_version, uverbs_sys_path, MTHCA_UVERBS_ABI_VERSION);
288 return NULL;
289 }
290
291 dev = malloc(sizeof *dev);
292 if (!dev) {
293 fprintf(stderr, PFX "Fatal: couldn't allocate device for %s\n",
294 uverbs_sys_path);
295 return NULL;
296 }
297
298 dev->ibv_dev.ops = mthca_dev_ops;
299 dev->hca_type = hca_table[i].type;
300 dev->page_size = sysconf(_SC_PAGESIZE);
301
302 return &dev->ibv_dev;
303 }
304
305 #ifdef HAVE_IBV_REGISTER_DRIVER
mthca_register_driver(void)306 static __attribute__((constructor)) void mthca_register_driver(void)
307 {
308 ibv_register_driver("mthca", mthca_driver_init);
309 }
310 #else
311 /*
312 * Export the old libsysfs sysfs_class_device-based driver entry point
313 * if libibverbs does not export an ibv_register_driver() function.
314 */
openib_driver_init(struct sysfs_class_device * sysdev)315 struct ibv_device *openib_driver_init(struct sysfs_class_device *sysdev)
316 {
317 int abi_ver = 0;
318 char value[8];
319
320 if (ibv_read_sysfs_file(sysdev->path, "abi_version",
321 value, sizeof value) > 0)
322 abi_ver = strtol(value, NULL, 10);
323
324 return mthca_driver_init(sysdev->path, abi_ver);
325 }
326 #endif /* HAVE_IBV_REGISTER_DRIVER */
327