xref: /freebsd/contrib/ofed/libibverbs/init.c (revision 2a2234c0f41da33b8cfc938e46b54a8234b64135)
1*d6b92ffaSHans Petter Selasky /*
2*d6b92ffaSHans Petter Selasky  * Copyright (c) 2004, 2005 Topspin Communications.  All rights reserved.
3*d6b92ffaSHans Petter Selasky  * Copyright (c) 2006 Cisco Systems, Inc.  All rights reserved.
4*d6b92ffaSHans Petter Selasky  *
5*d6b92ffaSHans Petter Selasky  * This software is available to you under a choice of one of two
6*d6b92ffaSHans Petter Selasky  * licenses.  You may choose to be licensed under the terms of the GNU
7*d6b92ffaSHans Petter Selasky  * General Public License (GPL) Version 2, available from the file
8*d6b92ffaSHans Petter Selasky  * COPYING in the main directory of this source tree, or the
9*d6b92ffaSHans Petter Selasky  * OpenIB.org BSD license below:
10*d6b92ffaSHans Petter Selasky  *
11*d6b92ffaSHans Petter Selasky  *     Redistribution and use in source and binary forms, with or
12*d6b92ffaSHans Petter Selasky  *     without modification, are permitted provided that the following
13*d6b92ffaSHans Petter Selasky  *     conditions are met:
14*d6b92ffaSHans Petter Selasky  *
15*d6b92ffaSHans Petter Selasky  *      - Redistributions of source code must retain the above
16*d6b92ffaSHans Petter Selasky  *        copyright notice, this list of conditions and the following
17*d6b92ffaSHans Petter Selasky  *        disclaimer.
18*d6b92ffaSHans Petter Selasky  *
19*d6b92ffaSHans Petter Selasky  *      - Redistributions in binary form must reproduce the above
20*d6b92ffaSHans Petter Selasky  *        copyright notice, this list of conditions and the following
21*d6b92ffaSHans Petter Selasky  *        disclaimer in the documentation and/or other materials
22*d6b92ffaSHans Petter Selasky  *        provided with the distribution.
23*d6b92ffaSHans Petter Selasky  *
24*d6b92ffaSHans Petter Selasky  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25*d6b92ffaSHans Petter Selasky  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26*d6b92ffaSHans Petter Selasky  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27*d6b92ffaSHans Petter Selasky  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28*d6b92ffaSHans Petter Selasky  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29*d6b92ffaSHans Petter Selasky  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30*d6b92ffaSHans Petter Selasky  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31*d6b92ffaSHans Petter Selasky  * SOFTWARE.
32*d6b92ffaSHans Petter Selasky  */
33*d6b92ffaSHans Petter Selasky #define _GNU_SOURCE
34*d6b92ffaSHans Petter Selasky #include <config.h>
35*d6b92ffaSHans Petter Selasky 
36*d6b92ffaSHans Petter Selasky #include <stdlib.h>
37*d6b92ffaSHans Petter Selasky #include <string.h>
38*d6b92ffaSHans Petter Selasky #include <glob.h>
39*d6b92ffaSHans Petter Selasky #include <stdio.h>
40*d6b92ffaSHans Petter Selasky #include <dlfcn.h>
41*d6b92ffaSHans Petter Selasky #include <unistd.h>
42*d6b92ffaSHans Petter Selasky #include <sys/stat.h>
43*d6b92ffaSHans Petter Selasky #include <sys/types.h>
44*d6b92ffaSHans Petter Selasky #include <sys/time.h>
45*d6b92ffaSHans Petter Selasky #include <sys/resource.h>
46*d6b92ffaSHans Petter Selasky #include <dirent.h>
47*d6b92ffaSHans Petter Selasky #include <errno.h>
48*d6b92ffaSHans Petter Selasky #include <assert.h>
49*d6b92ffaSHans Petter Selasky 
50*d6b92ffaSHans Petter Selasky #include "ibverbs.h"
51*d6b92ffaSHans Petter Selasky 
52*d6b92ffaSHans Petter Selasky #pragma GCC diagnostic ignored "-Wmissing-prototypes"
53*d6b92ffaSHans Petter Selasky 
54*d6b92ffaSHans Petter Selasky int abi_ver;
55*d6b92ffaSHans Petter Selasky 
56*d6b92ffaSHans Petter Selasky struct ibv_sysfs_dev {
57*d6b92ffaSHans Petter Selasky 	char		        sysfs_name[IBV_SYSFS_NAME_MAX];
58*d6b92ffaSHans Petter Selasky 	char		        ibdev_name[IBV_SYSFS_NAME_MAX];
59*d6b92ffaSHans Petter Selasky 	char		        sysfs_path[IBV_SYSFS_PATH_MAX];
60*d6b92ffaSHans Petter Selasky 	char		        ibdev_path[IBV_SYSFS_PATH_MAX];
61*d6b92ffaSHans Petter Selasky 	struct ibv_sysfs_dev   *next;
62*d6b92ffaSHans Petter Selasky 	int			abi_ver;
63*d6b92ffaSHans Petter Selasky 	int			have_driver;
64*d6b92ffaSHans Petter Selasky };
65*d6b92ffaSHans Petter Selasky 
66*d6b92ffaSHans Petter Selasky struct ibv_driver_name {
67*d6b92ffaSHans Petter Selasky 	char		       *name;
68*d6b92ffaSHans Petter Selasky 	struct ibv_driver_name *next;
69*d6b92ffaSHans Petter Selasky };
70*d6b92ffaSHans Petter Selasky 
71*d6b92ffaSHans Petter Selasky struct ibv_driver {
72*d6b92ffaSHans Petter Selasky 	const char	       *name;
73*d6b92ffaSHans Petter Selasky 	verbs_driver_init_func	verbs_init_func;
74*d6b92ffaSHans Petter Selasky 	struct ibv_driver      *next;
75*d6b92ffaSHans Petter Selasky };
76*d6b92ffaSHans Petter Selasky 
77*d6b92ffaSHans Petter Selasky static struct ibv_sysfs_dev *sysfs_dev_list;
78*d6b92ffaSHans Petter Selasky static struct ibv_driver_name *driver_name_list;
79*d6b92ffaSHans Petter Selasky static struct ibv_driver *head_driver, *tail_driver;
80*d6b92ffaSHans Petter Selasky 
find_sysfs_devs(void)81*d6b92ffaSHans Petter Selasky static int find_sysfs_devs(void)
82*d6b92ffaSHans Petter Selasky {
83*d6b92ffaSHans Petter Selasky 	char class_path[IBV_SYSFS_PATH_MAX];
84*d6b92ffaSHans Petter Selasky 	struct ibv_sysfs_dev *sysfs_dev = NULL;
85*d6b92ffaSHans Petter Selasky 	char value[8];
86*d6b92ffaSHans Petter Selasky 	int ret = 0;
87*d6b92ffaSHans Petter Selasky 	int i;
88*d6b92ffaSHans Petter Selasky 
89*d6b92ffaSHans Petter Selasky 	snprintf(class_path, sizeof class_path, "%s/class/infiniband_verbs",
90*d6b92ffaSHans Petter Selasky 		 ibv_get_sysfs_path());
91*d6b92ffaSHans Petter Selasky 
92*d6b92ffaSHans Petter Selasky 	for (i = 0; i < 256; i++) {
93*d6b92ffaSHans Petter Selasky 		if (!sysfs_dev)
94*d6b92ffaSHans Petter Selasky 			sysfs_dev = malloc(sizeof *sysfs_dev);
95*d6b92ffaSHans Petter Selasky 		if (!sysfs_dev) {
96*d6b92ffaSHans Petter Selasky 			ret = ENOMEM;
97*d6b92ffaSHans Petter Selasky 			goto out;
98*d6b92ffaSHans Petter Selasky 		}
99*d6b92ffaSHans Petter Selasky 
100*d6b92ffaSHans Petter Selasky 		snprintf(sysfs_dev->sysfs_path, sizeof sysfs_dev->sysfs_path,
101*d6b92ffaSHans Petter Selasky 			 "%s/uverbs%d", class_path, i);
102*d6b92ffaSHans Petter Selasky 
103*d6b92ffaSHans Petter Selasky 		snprintf(sysfs_dev->sysfs_name, sizeof sysfs_dev->sysfs_name,
104*d6b92ffaSHans Petter Selasky 			"uverbs%d", i);
105*d6b92ffaSHans Petter Selasky 
106*d6b92ffaSHans Petter Selasky 		if (ibv_read_sysfs_file(sysfs_dev->sysfs_path, "ibdev",
107*d6b92ffaSHans Petter Selasky 					sysfs_dev->ibdev_name,
108*d6b92ffaSHans Petter Selasky 					sizeof sysfs_dev->ibdev_name) < 0)
109*d6b92ffaSHans Petter Selasky 			continue;
110*d6b92ffaSHans Petter Selasky 
111*d6b92ffaSHans Petter Selasky 		snprintf(sysfs_dev->ibdev_path, sizeof sysfs_dev->ibdev_path,
112*d6b92ffaSHans Petter Selasky 			 "%s/class/infiniband/%s", ibv_get_sysfs_path(),
113*d6b92ffaSHans Petter Selasky 			 sysfs_dev->ibdev_name);
114*d6b92ffaSHans Petter Selasky 
115*d6b92ffaSHans Petter Selasky 		sysfs_dev->next        = sysfs_dev_list;
116*d6b92ffaSHans Petter Selasky 		sysfs_dev->have_driver = 0;
117*d6b92ffaSHans Petter Selasky 		if (ibv_read_sysfs_file(sysfs_dev->sysfs_path, "abi_version",
118*d6b92ffaSHans Petter Selasky 					value, sizeof value) > 0)
119*d6b92ffaSHans Petter Selasky 			sysfs_dev->abi_ver = strtol(value, NULL, 10);
120*d6b92ffaSHans Petter Selasky 		else
121*d6b92ffaSHans Petter Selasky 			sysfs_dev->abi_ver = 0;
122*d6b92ffaSHans Petter Selasky 
123*d6b92ffaSHans Petter Selasky 		sysfs_dev_list = sysfs_dev;
124*d6b92ffaSHans Petter Selasky 		sysfs_dev      = NULL;
125*d6b92ffaSHans Petter Selasky 	}
126*d6b92ffaSHans Petter Selasky 
127*d6b92ffaSHans Petter Selasky  out:
128*d6b92ffaSHans Petter Selasky 	if (sysfs_dev)
129*d6b92ffaSHans Petter Selasky 		free(sysfs_dev);
130*d6b92ffaSHans Petter Selasky 
131*d6b92ffaSHans Petter Selasky 	return ret;
132*d6b92ffaSHans Petter Selasky }
133*d6b92ffaSHans Petter Selasky 
verbs_register_driver(const char * name,verbs_driver_init_func verbs_init_func)134*d6b92ffaSHans Petter Selasky void verbs_register_driver(const char *name,
135*d6b92ffaSHans Petter Selasky 			   verbs_driver_init_func verbs_init_func)
136*d6b92ffaSHans Petter Selasky {
137*d6b92ffaSHans Petter Selasky 	struct ibv_driver *driver;
138*d6b92ffaSHans Petter Selasky 
139*d6b92ffaSHans Petter Selasky 	driver = malloc(sizeof *driver);
140*d6b92ffaSHans Petter Selasky 	if (!driver) {
141*d6b92ffaSHans Petter Selasky 		fprintf(stderr, PFX "Warning: couldn't allocate driver for %s\n", name);
142*d6b92ffaSHans Petter Selasky 		return;
143*d6b92ffaSHans Petter Selasky 	}
144*d6b92ffaSHans Petter Selasky 
145*d6b92ffaSHans Petter Selasky 	driver->name            = name;
146*d6b92ffaSHans Petter Selasky 	driver->verbs_init_func = verbs_init_func;
147*d6b92ffaSHans Petter Selasky 	driver->next            = NULL;
148*d6b92ffaSHans Petter Selasky 
149*d6b92ffaSHans Petter Selasky 	if (tail_driver)
150*d6b92ffaSHans Petter Selasky 		tail_driver->next = driver;
151*d6b92ffaSHans Petter Selasky 	else
152*d6b92ffaSHans Petter Selasky 		head_driver = driver;
153*d6b92ffaSHans Petter Selasky 	tail_driver = driver;
154*d6b92ffaSHans Petter Selasky }
155*d6b92ffaSHans Petter Selasky 
try_driver(struct ibv_driver * driver,struct ibv_sysfs_dev * sysfs_dev)156*d6b92ffaSHans Petter Selasky static struct ibv_device *try_driver(struct ibv_driver *driver,
157*d6b92ffaSHans Petter Selasky 				     struct ibv_sysfs_dev *sysfs_dev)
158*d6b92ffaSHans Petter Selasky {
159*d6b92ffaSHans Petter Selasky 	struct verbs_device *vdev;
160*d6b92ffaSHans Petter Selasky 	struct ibv_device *dev;
161*d6b92ffaSHans Petter Selasky 	char value[16];
162*d6b92ffaSHans Petter Selasky 
163*d6b92ffaSHans Petter Selasky 	vdev = driver->verbs_init_func(sysfs_dev->sysfs_path, sysfs_dev->abi_ver);
164*d6b92ffaSHans Petter Selasky 	if (!vdev)
165*d6b92ffaSHans Petter Selasky 		return NULL;
166*d6b92ffaSHans Petter Selasky 
167*d6b92ffaSHans Petter Selasky 	dev = &vdev->device;
168*d6b92ffaSHans Petter Selasky 	assert(dev->_ops._dummy1 == NULL);
169*d6b92ffaSHans Petter Selasky 	assert(dev->_ops._dummy2 == NULL);
170*d6b92ffaSHans Petter Selasky 
171*d6b92ffaSHans Petter Selasky 	if (ibv_read_sysfs_file(sysfs_dev->ibdev_path, "node_type", value, sizeof value) < 0) {
172*d6b92ffaSHans Petter Selasky 		fprintf(stderr, PFX "Warning: no node_type attr under %s.\n",
173*d6b92ffaSHans Petter Selasky 			sysfs_dev->ibdev_path);
174*d6b92ffaSHans Petter Selasky 			dev->node_type = IBV_NODE_UNKNOWN;
175*d6b92ffaSHans Petter Selasky 	} else {
176*d6b92ffaSHans Petter Selasky 		dev->node_type = strtol(value, NULL, 10);
177*d6b92ffaSHans Petter Selasky 		if (dev->node_type < IBV_NODE_CA || dev->node_type > IBV_NODE_USNIC_UDP)
178*d6b92ffaSHans Petter Selasky 			dev->node_type = IBV_NODE_UNKNOWN;
179*d6b92ffaSHans Petter Selasky 	}
180*d6b92ffaSHans Petter Selasky 
181*d6b92ffaSHans Petter Selasky 	switch (dev->node_type) {
182*d6b92ffaSHans Petter Selasky 	case IBV_NODE_CA:
183*d6b92ffaSHans Petter Selasky 	case IBV_NODE_SWITCH:
184*d6b92ffaSHans Petter Selasky 	case IBV_NODE_ROUTER:
185*d6b92ffaSHans Petter Selasky 		dev->transport_type = IBV_TRANSPORT_IB;
186*d6b92ffaSHans Petter Selasky 		break;
187*d6b92ffaSHans Petter Selasky 	case IBV_NODE_RNIC:
188*d6b92ffaSHans Petter Selasky 		dev->transport_type = IBV_TRANSPORT_IWARP;
189*d6b92ffaSHans Petter Selasky 		break;
190*d6b92ffaSHans Petter Selasky 	case IBV_NODE_USNIC:
191*d6b92ffaSHans Petter Selasky 		dev->transport_type = IBV_TRANSPORT_USNIC;
192*d6b92ffaSHans Petter Selasky 		break;
193*d6b92ffaSHans Petter Selasky 	case IBV_NODE_USNIC_UDP:
194*d6b92ffaSHans Petter Selasky 		dev->transport_type = IBV_TRANSPORT_USNIC_UDP;
195*d6b92ffaSHans Petter Selasky 		break;
196*d6b92ffaSHans Petter Selasky 	default:
197*d6b92ffaSHans Petter Selasky 		dev->transport_type = IBV_TRANSPORT_UNKNOWN;
198*d6b92ffaSHans Petter Selasky 		break;
199*d6b92ffaSHans Petter Selasky 	}
200*d6b92ffaSHans Petter Selasky 
201*d6b92ffaSHans Petter Selasky 	strcpy(dev->dev_name,   sysfs_dev->sysfs_name);
202*d6b92ffaSHans Petter Selasky 	strcpy(dev->dev_path,   sysfs_dev->sysfs_path);
203*d6b92ffaSHans Petter Selasky 	strcpy(dev->name,       sysfs_dev->ibdev_name);
204*d6b92ffaSHans Petter Selasky 	strcpy(dev->ibdev_path, sysfs_dev->ibdev_path);
205*d6b92ffaSHans Petter Selasky 
206*d6b92ffaSHans Petter Selasky 	return dev;
207*d6b92ffaSHans Petter Selasky }
208*d6b92ffaSHans Petter Selasky 
try_drivers(struct ibv_sysfs_dev * sysfs_dev)209*d6b92ffaSHans Petter Selasky static struct ibv_device *try_drivers(struct ibv_sysfs_dev *sysfs_dev)
210*d6b92ffaSHans Petter Selasky {
211*d6b92ffaSHans Petter Selasky 	struct ibv_driver *driver;
212*d6b92ffaSHans Petter Selasky 	struct ibv_device *dev;
213*d6b92ffaSHans Petter Selasky 
214*d6b92ffaSHans Petter Selasky 	for (driver = head_driver; driver; driver = driver->next) {
215*d6b92ffaSHans Petter Selasky 		dev = try_driver(driver, sysfs_dev);
216*d6b92ffaSHans Petter Selasky 		if (dev)
217*d6b92ffaSHans Petter Selasky 			return dev;
218*d6b92ffaSHans Petter Selasky 	}
219*d6b92ffaSHans Petter Selasky 
220*d6b92ffaSHans Petter Selasky 	return NULL;
221*d6b92ffaSHans Petter Selasky }
222*d6b92ffaSHans Petter Selasky 
check_abi_version(const char * path)223*d6b92ffaSHans Petter Selasky static int check_abi_version(const char *path)
224*d6b92ffaSHans Petter Selasky {
225*d6b92ffaSHans Petter Selasky 	char value[8];
226*d6b92ffaSHans Petter Selasky 
227*d6b92ffaSHans Petter Selasky 	if (ibv_read_sysfs_file(path, "class/infiniband_verbs/abi_version",
228*d6b92ffaSHans Petter Selasky 				value, sizeof value) < 0) {
229*d6b92ffaSHans Petter Selasky 		return ENOSYS;
230*d6b92ffaSHans Petter Selasky 	}
231*d6b92ffaSHans Petter Selasky 
232*d6b92ffaSHans Petter Selasky 	abi_ver = strtol(value, NULL, 10);
233*d6b92ffaSHans Petter Selasky 
234*d6b92ffaSHans Petter Selasky 	if (abi_ver < IB_USER_VERBS_MIN_ABI_VERSION ||
235*d6b92ffaSHans Petter Selasky 	    abi_ver > IB_USER_VERBS_MAX_ABI_VERSION) {
236*d6b92ffaSHans Petter Selasky 		fprintf(stderr, PFX "Fatal: kernel ABI version %d "
237*d6b92ffaSHans Petter Selasky 			"doesn't match library version %d.\n",
238*d6b92ffaSHans Petter Selasky 			abi_ver, IB_USER_VERBS_MAX_ABI_VERSION);
239*d6b92ffaSHans Petter Selasky 		return ENOSYS;
240*d6b92ffaSHans Petter Selasky 	}
241*d6b92ffaSHans Petter Selasky 
242*d6b92ffaSHans Petter Selasky 	return 0;
243*d6b92ffaSHans Petter Selasky }
244*d6b92ffaSHans Petter Selasky 
check_memlock_limit(void)245*d6b92ffaSHans Petter Selasky static void check_memlock_limit(void)
246*d6b92ffaSHans Petter Selasky {
247*d6b92ffaSHans Petter Selasky 	struct rlimit rlim;
248*d6b92ffaSHans Petter Selasky 
249*d6b92ffaSHans Petter Selasky 	if (!geteuid())
250*d6b92ffaSHans Petter Selasky 		return;
251*d6b92ffaSHans Petter Selasky 
252*d6b92ffaSHans Petter Selasky 	if (getrlimit(RLIMIT_MEMLOCK, &rlim)) {
253*d6b92ffaSHans Petter Selasky 		fprintf(stderr, PFX "Warning: getrlimit(RLIMIT_MEMLOCK) failed.");
254*d6b92ffaSHans Petter Selasky 		return;
255*d6b92ffaSHans Petter Selasky 	}
256*d6b92ffaSHans Petter Selasky 
257*d6b92ffaSHans Petter Selasky 	if (rlim.rlim_cur <= 32768)
258*d6b92ffaSHans Petter Selasky 		fprintf(stderr, PFX "Warning: RLIMIT_MEMLOCK is %lu bytes.\n"
259*d6b92ffaSHans Petter Selasky 			"    This will severely limit memory registrations.\n",
260*d6b92ffaSHans Petter Selasky 			rlim.rlim_cur);
261*d6b92ffaSHans Petter Selasky }
262*d6b92ffaSHans Petter Selasky 
add_device(struct ibv_device * dev,struct ibv_device *** dev_list,int * num_devices,int * list_size)263*d6b92ffaSHans Petter Selasky static void add_device(struct ibv_device *dev,
264*d6b92ffaSHans Petter Selasky 		       struct ibv_device ***dev_list,
265*d6b92ffaSHans Petter Selasky 		       int *num_devices,
266*d6b92ffaSHans Petter Selasky 		       int *list_size)
267*d6b92ffaSHans Petter Selasky {
268*d6b92ffaSHans Petter Selasky 	struct ibv_device **new_list;
269*d6b92ffaSHans Petter Selasky 
270*d6b92ffaSHans Petter Selasky 	if (*list_size <= *num_devices) {
271*d6b92ffaSHans Petter Selasky 		*list_size = *list_size ? *list_size * 2 : 1;
272*d6b92ffaSHans Petter Selasky 		new_list = realloc(*dev_list, *list_size * sizeof (struct ibv_device *));
273*d6b92ffaSHans Petter Selasky 		if (!new_list)
274*d6b92ffaSHans Petter Selasky 			return;
275*d6b92ffaSHans Petter Selasky 		*dev_list = new_list;
276*d6b92ffaSHans Petter Selasky 	}
277*d6b92ffaSHans Petter Selasky 
278*d6b92ffaSHans Petter Selasky 	(*dev_list)[(*num_devices)++] = dev;
279*d6b92ffaSHans Petter Selasky }
280*d6b92ffaSHans Petter Selasky 
ibverbs_init(struct ibv_device *** list)281*d6b92ffaSHans Petter Selasky int ibverbs_init(struct ibv_device ***list)
282*d6b92ffaSHans Petter Selasky {
283*d6b92ffaSHans Petter Selasky 	const char *sysfs_path;
284*d6b92ffaSHans Petter Selasky 	struct ibv_sysfs_dev *sysfs_dev, *next_dev;
285*d6b92ffaSHans Petter Selasky 	struct ibv_device *device;
286*d6b92ffaSHans Petter Selasky 	int num_devices = 0;
287*d6b92ffaSHans Petter Selasky 	int list_size = 0;
288*d6b92ffaSHans Petter Selasky 	int statically_linked = 0;
289*d6b92ffaSHans Petter Selasky 	int no_driver = 0;
290*d6b92ffaSHans Petter Selasky 	int ret;
291*d6b92ffaSHans Petter Selasky 
292*d6b92ffaSHans Petter Selasky 	*list = NULL;
293*d6b92ffaSHans Petter Selasky 
294*d6b92ffaSHans Petter Selasky 	if (getenv("RDMAV_FORK_SAFE") || getenv("IBV_FORK_SAFE"))
295*d6b92ffaSHans Petter Selasky 		if (ibv_fork_init())
296*d6b92ffaSHans Petter Selasky 			fprintf(stderr, PFX "Warning: fork()-safety requested "
297*d6b92ffaSHans Petter Selasky 				"but init failed\n");
298*d6b92ffaSHans Petter Selasky 
299*d6b92ffaSHans Petter Selasky 	sysfs_path = ibv_get_sysfs_path();
300*d6b92ffaSHans Petter Selasky 	if (!sysfs_path)
301*d6b92ffaSHans Petter Selasky 		return -ENOSYS;
302*d6b92ffaSHans Petter Selasky 
303*d6b92ffaSHans Petter Selasky 	ret = check_abi_version(sysfs_path);
304*d6b92ffaSHans Petter Selasky 	if (ret)
305*d6b92ffaSHans Petter Selasky 		return -ret;
306*d6b92ffaSHans Petter Selasky 
307*d6b92ffaSHans Petter Selasky 	check_memlock_limit();
308*d6b92ffaSHans Petter Selasky 
309*d6b92ffaSHans Petter Selasky 	ret = find_sysfs_devs();
310*d6b92ffaSHans Petter Selasky 	if (ret)
311*d6b92ffaSHans Petter Selasky 		return -ret;
312*d6b92ffaSHans Petter Selasky 
313*d6b92ffaSHans Petter Selasky 	for (sysfs_dev = sysfs_dev_list; sysfs_dev; sysfs_dev = sysfs_dev->next) {
314*d6b92ffaSHans Petter Selasky 		device = try_drivers(sysfs_dev);
315*d6b92ffaSHans Petter Selasky 		if (device) {
316*d6b92ffaSHans Petter Selasky 			add_device(device, list, &num_devices, &list_size);
317*d6b92ffaSHans Petter Selasky 			sysfs_dev->have_driver = 1;
318*d6b92ffaSHans Petter Selasky 		} else
319*d6b92ffaSHans Petter Selasky 			no_driver = 1;
320*d6b92ffaSHans Petter Selasky 	}
321*d6b92ffaSHans Petter Selasky 
322*d6b92ffaSHans Petter Selasky 	if (!no_driver)
323*d6b92ffaSHans Petter Selasky 		goto out;
324*d6b92ffaSHans Petter Selasky 
325*d6b92ffaSHans Petter Selasky 	/*
326*d6b92ffaSHans Petter Selasky 	 * Check if we can dlopen() ourselves.  If this fails,
327*d6b92ffaSHans Petter Selasky 	 * libibverbs is probably statically linked into the
328*d6b92ffaSHans Petter Selasky 	 * executable, and we should just give up, since trying to
329*d6b92ffaSHans Petter Selasky 	 * dlopen() a driver module will fail spectacularly (loading a
330*d6b92ffaSHans Petter Selasky 	 * driver .so will bring in dynamic copies of libibverbs and
331*d6b92ffaSHans Petter Selasky 	 * libdl to go along with the static copies the executable
332*d6b92ffaSHans Petter Selasky 	 * has, which quickly leads to a crash.
333*d6b92ffaSHans Petter Selasky 	 */
334*d6b92ffaSHans Petter Selasky 	{
335*d6b92ffaSHans Petter Selasky 		void *hand = dlopen(NULL, RTLD_NOW);
336*d6b92ffaSHans Petter Selasky 		if (!hand) {
337*d6b92ffaSHans Petter Selasky 			fprintf(stderr, PFX "Warning: dlopen(NULL) failed, "
338*d6b92ffaSHans Petter Selasky 				"assuming static linking.\n");
339*d6b92ffaSHans Petter Selasky 			statically_linked = 1;
340*d6b92ffaSHans Petter Selasky 			goto out;
341*d6b92ffaSHans Petter Selasky 		}
342*d6b92ffaSHans Petter Selasky 		dlclose(hand);
343*d6b92ffaSHans Petter Selasky 	}
344*d6b92ffaSHans Petter Selasky 
345*d6b92ffaSHans Petter Selasky 	for (sysfs_dev = sysfs_dev_list; sysfs_dev; sysfs_dev = sysfs_dev->next) {
346*d6b92ffaSHans Petter Selasky 		if (sysfs_dev->have_driver)
347*d6b92ffaSHans Petter Selasky 			continue;
348*d6b92ffaSHans Petter Selasky 
349*d6b92ffaSHans Petter Selasky 		device = try_drivers(sysfs_dev);
350*d6b92ffaSHans Petter Selasky 		if (device) {
351*d6b92ffaSHans Petter Selasky 			add_device(device, list, &num_devices, &list_size);
352*d6b92ffaSHans Petter Selasky 			sysfs_dev->have_driver = 1;
353*d6b92ffaSHans Petter Selasky 		}
354*d6b92ffaSHans Petter Selasky 	}
355*d6b92ffaSHans Petter Selasky 
356*d6b92ffaSHans Petter Selasky out:
357*d6b92ffaSHans Petter Selasky 	for (sysfs_dev = sysfs_dev_list,
358*d6b92ffaSHans Petter Selasky 		     next_dev = sysfs_dev ? sysfs_dev->next : NULL;
359*d6b92ffaSHans Petter Selasky 	     sysfs_dev;
360*d6b92ffaSHans Petter Selasky 	     sysfs_dev = next_dev, next_dev = sysfs_dev ? sysfs_dev->next : NULL) {
361*d6b92ffaSHans Petter Selasky 		if (!sysfs_dev->have_driver && getenv("IBV_SHOW_WARNINGS")) {
362*d6b92ffaSHans Petter Selasky 			fprintf(stderr, PFX "Warning: no userspace device-specific "
363*d6b92ffaSHans Petter Selasky 				"driver found for %s\n", sysfs_dev->sysfs_path);
364*d6b92ffaSHans Petter Selasky 			if (statically_linked)
365*d6b92ffaSHans Petter Selasky 				fprintf(stderr, "	When linking libibverbs statically, "
366*d6b92ffaSHans Petter Selasky 					"driver must be statically linked too.\n");
367*d6b92ffaSHans Petter Selasky 		}
368*d6b92ffaSHans Petter Selasky 		free(sysfs_dev);
369*d6b92ffaSHans Petter Selasky 	}
370*d6b92ffaSHans Petter Selasky 
371*d6b92ffaSHans Petter Selasky 	return num_devices;
372*d6b92ffaSHans Petter Selasky }
373