2 * Copyright (c) 2004, 2005 Topspin Communications. All rights reserved.
3 * Copyright (c) 2006 Cisco Systems. All rights reserved.
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:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
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.
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
36 #endif /* HAVE_CONFIG_H */
46 #ifndef HAVE_IBV_REGISTER_DRIVER
47 #include <sysfs/libsysfs.h>
50 #ifndef HAVE_IBV_READ_SYSFS_FILE
51 #include <sys/types.h>
57 #include "mthca-abi.h"
59 #ifndef PCI_VENDOR_ID_MELLANOX
60 #define PCI_VENDOR_ID_MELLANOX 0x15b3
63 #ifndef PCI_DEVICE_ID_MELLANOX_TAVOR
64 #define PCI_DEVICE_ID_MELLANOX_TAVOR 0x5a44
67 #ifndef PCI_DEVICE_ID_MELLANOX_ARBEL_COMPAT
68 #define PCI_DEVICE_ID_MELLANOX_ARBEL_COMPAT 0x6278
71 #ifndef PCI_DEVICE_ID_MELLANOX_ARBEL
72 #define PCI_DEVICE_ID_MELLANOX_ARBEL 0x6282
75 #ifndef PCI_DEVICE_ID_MELLANOX_SINAI_OLD
76 #define PCI_DEVICE_ID_MELLANOX_SINAI_OLD 0x5e8c
79 #ifndef PCI_DEVICE_ID_MELLANOX_SINAI
80 #define PCI_DEVICE_ID_MELLANOX_SINAI 0x6274
83 #ifndef PCI_VENDOR_ID_TOPSPIN
84 #define PCI_VENDOR_ID_TOPSPIN 0x1867
87 #define HCA(v, d, t) \
88 { .vendor = PCI_VENDOR_ID_##v, \
89 .device = PCI_DEVICE_ID_MELLANOX_##d, \
95 enum mthca_hca_type type;
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),
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
134 static struct ibv_context *mthca_alloc_context(struct ibv_device *ibdev, int cmd_fd)
136 struct mthca_context *context;
137 struct ibv_get_context cmd;
138 struct mthca_alloc_ucontext_resp resp;
141 context = calloc(1, sizeof *context);
145 context->ibv_ctx.cmd_fd = cmd_fd;
147 if (ibv_cmd_get_context(&context->ibv_ctx, &cmd, sizeof cmd,
148 &resp.ibv_resp, sizeof resp))
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;
156 * Need to set ibv_ctx.device because mthca_is_memfree() will
157 * look at it to figure out the HCA type.
159 context->ibv_ctx.device = ibdev;
161 if (mthca_is_memfree(&context->ibv_ctx)) {
162 context->db_tab = mthca_alloc_db_tab(resp.uarc_size);
163 if (!context->db_tab)
166 context->db_tab = NULL;
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;
172 context->uar = mmap(NULL, to_mdev(ibdev)->page_size, PROT_WRITE,
173 MAP_SHARED, cmd_fd, 0);
174 if (context->uar == MAP_FAILED)
177 pthread_spin_init(&context->uar_lock, PTHREAD_PROCESS_PRIVATE);
179 context->pd = mthca_alloc_pd(&context->ibv_ctx);
183 context->pd->context = &context->ibv_ctx;
185 context->ibv_ctx.ops = mthca_ctx_ops;
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;
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;
201 return &context->ibv_ctx;
204 munmap(context->uar, to_mdev(ibdev)->page_size);
207 mthca_free_db_tab(context->db_tab);
214 static void mthca_free_context(struct ibv_context *ibctx)
216 struct mthca_context *context = to_mctx(ibctx);
218 mthca_free_pd(context->pd);
219 munmap(context->uar, to_mdev(ibctx->device)->page_size);
220 mthca_free_db_tab(context->db_tab);
224 static struct ibv_device_ops mthca_dev_ops = {
225 .alloc_context = mthca_alloc_context,
226 .free_context = mthca_free_context
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."
234 #ifndef HAVE_IBV_READ_SYSFS_FILE
235 static int ibv_read_sysfs_file(const char *dir, const char *file,
236 char *buf, size_t size)
242 snprintf(path, sizeof path, "%s/%s", dir, file);
244 fd = open(path, O_RDONLY);
248 len = read(fd, buf, size);
252 if (len > 0 && buf[len - 1] == '\n')
257 #endif /* HAVE_IBV_READ_SYSFS_FILE */
259 static struct ibv_device *mthca_driver_init(const char *uverbs_sys_path,
263 struct mthca_device *dev;
264 unsigned vendor, device;
267 if (ibv_read_sysfs_file(uverbs_sys_path, "device/vendor",
268 value, sizeof value) < 0)
270 sscanf(value, "%i", &vendor);
272 if (ibv_read_sysfs_file(uverbs_sys_path, "device/device",
273 value, sizeof value) < 0)
275 sscanf(value, "%i", &device);
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)
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);
291 dev = malloc(sizeof *dev);
293 fprintf(stderr, PFX "Fatal: couldn't allocate device for %s\n",
298 dev->ibv_dev.ops = mthca_dev_ops;
299 dev->hca_type = hca_table[i].type;
300 dev->page_size = sysconf(_SC_PAGESIZE);
302 return &dev->ibv_dev;
305 #ifdef HAVE_IBV_REGISTER_DRIVER
306 static __attribute__((constructor)) void mthca_register_driver(void)
308 ibv_register_driver("mthca", mthca_driver_init);
312 * Export the old libsysfs sysfs_class_device-based driver entry point
313 * if libibverbs does not export an ibv_register_driver() function.
315 struct ibv_device *openib_driver_init(struct sysfs_class_device *sysdev)
320 if (ibv_read_sysfs_file(sysdev->path, "abi_version",
321 value, sizeof value) > 0)
322 abi_ver = strtol(value, NULL, 10);
324 return mthca_driver_init(sysdev->path, abi_ver);
326 #endif /* HAVE_IBV_REGISTER_DRIVER */