2 * Copyright (c) 2004, 2005 Topspin Communications. All rights reserved.
3 * Copyright (c) 2006 Cisco Systems, Inc. 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
43 #include <sys/types.h>
45 #include <sys/resource.h>
52 #pragma GCC diagnostic ignored "-Wmissing-prototypes"
56 struct ibv_sysfs_dev {
57 char sysfs_name[IBV_SYSFS_NAME_MAX];
58 char ibdev_name[IBV_SYSFS_NAME_MAX];
59 char sysfs_path[IBV_SYSFS_PATH_MAX];
60 char ibdev_path[IBV_SYSFS_PATH_MAX];
61 struct ibv_sysfs_dev *next;
66 struct ibv_driver_name {
68 struct ibv_driver_name *next;
73 verbs_driver_init_func verbs_init_func;
74 struct ibv_driver *next;
77 static struct ibv_sysfs_dev *sysfs_dev_list;
78 static struct ibv_driver_name *driver_name_list;
79 static struct ibv_driver *head_driver, *tail_driver;
81 static int find_sysfs_devs(void)
83 char class_path[IBV_SYSFS_PATH_MAX];
84 struct ibv_sysfs_dev *sysfs_dev = NULL;
89 snprintf(class_path, sizeof class_path, "%s/class/infiniband_verbs",
90 ibv_get_sysfs_path());
92 for (i = 0; i < 256; i++) {
94 sysfs_dev = malloc(sizeof *sysfs_dev);
100 snprintf(sysfs_dev->sysfs_path, sizeof sysfs_dev->sysfs_path,
101 "%s/uverbs%d", class_path, i);
103 snprintf(sysfs_dev->sysfs_name, sizeof sysfs_dev->sysfs_name,
106 if (ibv_read_sysfs_file(sysfs_dev->sysfs_path, "ibdev",
107 sysfs_dev->ibdev_name,
108 sizeof sysfs_dev->ibdev_name) < 0)
111 snprintf(sysfs_dev->ibdev_path, sizeof sysfs_dev->ibdev_path,
112 "%s/class/infiniband/%s", ibv_get_sysfs_path(),
113 sysfs_dev->ibdev_name);
115 sysfs_dev->next = sysfs_dev_list;
116 sysfs_dev->have_driver = 0;
117 if (ibv_read_sysfs_file(sysfs_dev->sysfs_path, "abi_version",
118 value, sizeof value) > 0)
119 sysfs_dev->abi_ver = strtol(value, NULL, 10);
121 sysfs_dev->abi_ver = 0;
123 sysfs_dev_list = sysfs_dev;
134 void verbs_register_driver(const char *name,
135 verbs_driver_init_func verbs_init_func)
137 struct ibv_driver *driver;
139 driver = malloc(sizeof *driver);
141 fprintf(stderr, PFX "Warning: couldn't allocate driver for %s\n", name);
146 driver->verbs_init_func = verbs_init_func;
150 tail_driver->next = driver;
152 head_driver = driver;
153 tail_driver = driver;
156 static struct ibv_device *try_driver(struct ibv_driver *driver,
157 struct ibv_sysfs_dev *sysfs_dev)
159 struct verbs_device *vdev;
160 struct ibv_device *dev;
163 vdev = driver->verbs_init_func(sysfs_dev->sysfs_path, sysfs_dev->abi_ver);
168 assert(dev->_ops._dummy1 == NULL);
169 assert(dev->_ops._dummy2 == NULL);
171 if (ibv_read_sysfs_file(sysfs_dev->ibdev_path, "node_type", value, sizeof value) < 0) {
172 fprintf(stderr, PFX "Warning: no node_type attr under %s.\n",
173 sysfs_dev->ibdev_path);
174 dev->node_type = IBV_NODE_UNKNOWN;
176 dev->node_type = strtol(value, NULL, 10);
177 if (dev->node_type < IBV_NODE_CA || dev->node_type > IBV_NODE_USNIC_UDP)
178 dev->node_type = IBV_NODE_UNKNOWN;
181 switch (dev->node_type) {
183 case IBV_NODE_SWITCH:
184 case IBV_NODE_ROUTER:
185 dev->transport_type = IBV_TRANSPORT_IB;
188 dev->transport_type = IBV_TRANSPORT_IWARP;
191 dev->transport_type = IBV_TRANSPORT_USNIC;
193 case IBV_NODE_USNIC_UDP:
194 dev->transport_type = IBV_TRANSPORT_USNIC_UDP;
197 dev->transport_type = IBV_TRANSPORT_UNKNOWN;
201 strcpy(dev->dev_name, sysfs_dev->sysfs_name);
202 strcpy(dev->dev_path, sysfs_dev->sysfs_path);
203 strcpy(dev->name, sysfs_dev->ibdev_name);
204 strcpy(dev->ibdev_path, sysfs_dev->ibdev_path);
209 static struct ibv_device *try_drivers(struct ibv_sysfs_dev *sysfs_dev)
211 struct ibv_driver *driver;
212 struct ibv_device *dev;
214 for (driver = head_driver; driver; driver = driver->next) {
215 dev = try_driver(driver, sysfs_dev);
223 static int check_abi_version(const char *path)
227 if (ibv_read_sysfs_file(path, "class/infiniband_verbs/abi_version",
228 value, sizeof value) < 0) {
232 abi_ver = strtol(value, NULL, 10);
234 if (abi_ver < IB_USER_VERBS_MIN_ABI_VERSION ||
235 abi_ver > IB_USER_VERBS_MAX_ABI_VERSION) {
236 fprintf(stderr, PFX "Fatal: kernel ABI version %d "
237 "doesn't match library version %d.\n",
238 abi_ver, IB_USER_VERBS_MAX_ABI_VERSION);
245 static void check_memlock_limit(void)
252 if (getrlimit(RLIMIT_MEMLOCK, &rlim)) {
253 fprintf(stderr, PFX "Warning: getrlimit(RLIMIT_MEMLOCK) failed.");
257 if (rlim.rlim_cur <= 32768)
258 fprintf(stderr, PFX "Warning: RLIMIT_MEMLOCK is %lu bytes.\n"
259 " This will severely limit memory registrations.\n",
263 static void add_device(struct ibv_device *dev,
264 struct ibv_device ***dev_list,
268 struct ibv_device **new_list;
270 if (*list_size <= *num_devices) {
271 *list_size = *list_size ? *list_size * 2 : 1;
272 new_list = realloc(*dev_list, *list_size * sizeof (struct ibv_device *));
275 *dev_list = new_list;
278 (*dev_list)[(*num_devices)++] = dev;
281 int ibverbs_init(struct ibv_device ***list)
283 const char *sysfs_path;
284 struct ibv_sysfs_dev *sysfs_dev, *next_dev;
285 struct ibv_device *device;
288 int statically_linked = 0;
294 if (getenv("RDMAV_FORK_SAFE") || getenv("IBV_FORK_SAFE"))
296 fprintf(stderr, PFX "Warning: fork()-safety requested "
297 "but init failed\n");
299 sysfs_path = ibv_get_sysfs_path();
303 ret = check_abi_version(sysfs_path);
307 check_memlock_limit();
309 ret = find_sysfs_devs();
313 for (sysfs_dev = sysfs_dev_list; sysfs_dev; sysfs_dev = sysfs_dev->next) {
314 device = try_drivers(sysfs_dev);
316 add_device(device, list, &num_devices, &list_size);
317 sysfs_dev->have_driver = 1;
326 * Check if we can dlopen() ourselves. If this fails,
327 * libibverbs is probably statically linked into the
328 * executable, and we should just give up, since trying to
329 * dlopen() a driver module will fail spectacularly (loading a
330 * driver .so will bring in dynamic copies of libibverbs and
331 * libdl to go along with the static copies the executable
332 * has, which quickly leads to a crash.
335 void *hand = dlopen(NULL, RTLD_NOW);
337 fprintf(stderr, PFX "Warning: dlopen(NULL) failed, "
338 "assuming static linking.\n");
339 statically_linked = 1;
345 for (sysfs_dev = sysfs_dev_list; sysfs_dev; sysfs_dev = sysfs_dev->next) {
346 if (sysfs_dev->have_driver)
349 device = try_drivers(sysfs_dev);
351 add_device(device, list, &num_devices, &list_size);
352 sysfs_dev->have_driver = 1;
357 for (sysfs_dev = sysfs_dev_list,
358 next_dev = sysfs_dev ? sysfs_dev->next : NULL;
360 sysfs_dev = next_dev, next_dev = sysfs_dev ? sysfs_dev->next : NULL) {
361 if (!sysfs_dev->have_driver && getenv("IBV_SHOW_WARNINGS")) {
362 fprintf(stderr, PFX "Warning: no userspace device-specific "
363 "driver found for %s\n", sysfs_dev->sysfs_path);
364 if (statically_linked)
365 fprintf(stderr, " When linking libibverbs statically, "
366 "driver must be statically linked too.\n");