summaryrefslogtreecommitdiff
path: root/src/bios_device.c
blob: e04c5b9ea5e483c7d3f54df30fdd8bfaf688d650 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*
 *  Copyright (c) 2006 Dell, Inc.
 *  by Matt Domsch <Matt_Domsch@dell.com>
 *  Licensed under the GNU General Public license, version 2.
 */
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <pci/pci.h>
#include <net/if.h>
#include "list.h"
#include "bios_device.h"
#include "state.h"
#include "libbiosdevname.h"

void free_bios_devices(void *cookie)
{
	struct libbiosdevname_state *state = cookie;
	struct bios_device *dev, *n;
	if (!state)
		return;
	list_for_each_entry_safe(dev, n, &state->bios_devices, node) {
		list_del(&(dev->node));
		free(dev);
	}
}


static void unparse_bios_device(struct bios_device *dev)
{
	char buf[8192];
	memset(buf, 0, sizeof(buf));
	printf("BIOS device: %s\n", dev->bios_name ? dev->bios_name : "");
	if (dev->netdev) {
		unparse_network_device(buf, sizeof(buf), dev->netdev);
		printf("%s", buf);
	}
	else
		printf("  No driver loaded for this device.\n");

	if (is_pci(dev)) {
		unparse_pci_device(buf, sizeof(buf), dev->pcidev);
		printf("%s", buf);
	}
	printf("\n");
	if (dev->duplicate)
		printf("Duplicate: True\n");
}

void unparse_bios_devices(void *cookie)
{
	struct libbiosdevname_state *state = cookie;
	struct bios_device *dev;
	if (!state)
		return;
	list_for_each_entry(dev, &state->bios_devices, node) {
		unparse_bios_device(dev);
	}
}

void unparse_bios_device_by_name(void *cookie,
				 const char *name)
{

	struct libbiosdevname_state *state = cookie;
	struct bios_device *dev;
	if (!state)
		return;
	list_for_each_entry(dev, &state->bios_devices, node) {
		if (dev->netdev && !strcmp(dev->netdev->kernel_name, name))
			unparse_bios_device(dev);
	}
}

char * kern_to_bios(void *cookie,
		    const char *name)
{
	struct libbiosdevname_state *state = cookie;
	struct bios_device *dev;
	if (!state)
		return NULL;
	list_for_each_entry(dev, &state->bios_devices, node) {
		if (dev->netdev && !strcmp(dev->netdev->kernel_name, name)) {
			if (dev->duplicate)
				return NULL;
			return dev->bios_name;
		}
	}
	return NULL;
}

char * pci_to_bios(void *cookie, const char *s)
{
	struct libbiosdevname_state *state = cookie;
	struct bios_device *dev;
	int domain=0, bus=0, device=0, func=0;

	if (parse_pci_name(s, &domain, &bus, &device, &func)) {
		fprintf(stderr, "%s is not a valid pci device name\n", s);
		return NULL;
	}

	if (!state)
		return NULL;

	list_for_each_entry(dev, &state->bios_devices, node) {
		const struct pci_device *pci = dev->pcidev;

		if(pci &&
		   pci->pci_dev->domain == domain &&
		   pci->pci_dev->bus == bus &&
		   pci->pci_dev->dev == device &&
		   pci->pci_dev->func == func)
			return dev->bios_name;
	}
	return NULL;
}

void unparse_bios_device_list(void *cookie)
{
	struct libbiosdevname_state *state = cookie;
	struct bios_device *dev;
	if (!state)
		return;
	list_for_each_entry(dev, &state->bios_devices, node) {
		unparse_bios_device(dev);
	}
}



/*
 * This sorts all the embedded devices first; by PCI bus/dev/fn, then
 * all the add-in devices by slot number, by pci bus/dev/fn.
 * Unknown location devices show up as physical slot INT_MAX, so they
 * come last.
 */

static int sort_pci(const struct bios_device *bdev_a, const struct bios_device *bdev_b)
{
	const struct pci_device *a = bdev_a->pcidev;
	const struct pci_device *b = bdev_b->pcidev;

	if      (a->physical_slot < b->physical_slot) return -1;
	else if (a->physical_slot > b->physical_slot) return 1;

	if      (a->pci_dev->domain < b->pci_dev->domain) return -1;
	else if (a->pci_dev->domain > b->pci_dev->domain) return  1;

	if      (a->pci_dev->bus < b->pci_dev->bus) return -1;
	else if (a->pci_dev->bus > b->pci_dev->bus) return  1;

	if      (a->pci_dev->dev < b->pci_dev->dev) return -1;
	else if (a->pci_dev->dev > b->pci_dev->dev) return  1;

	if      (a->pci_dev->func < b->pci_dev->func) return -1;
	else if (a->pci_dev->func > b->pci_dev->func) return  1;

	return 0;
}

static int sort_smbios(const struct bios_device *x, const struct bios_device *y)
{
	struct pci_device *a, *b;

	if      (x->pcidev && !y->pcidev) return -1;
	else if (!x->pcidev && y->pcidev) return 1;

	a = x->pcidev;
	b = y->pcidev;

	if      (a->physical_slot == 0 && b->physical_slot == 0) {
		if ( a->smbios_type == b->smbios_type) {
			if ( a->smbios_instance < b->smbios_instance) return -1;
			else if (a->smbios_instance > b->smbios_instance) return 1;
		}
	}
	else {
		if      (a->physical_slot < b->physical_slot) return -1;
		else if (a->physical_slot > b->physical_slot) return 1;
	}

	return sort_pci(x, y);
}

enum bios_device_types {
	IS_PCI,
	IS_UNKNOWN_TYPE,
};

static int bios_device_type_num(const struct bios_device *dev)
{
	if (is_pci(dev))
		return IS_PCI;
	return IS_UNKNOWN_TYPE;
}

static int sort_by_type(const struct bios_device *a, const struct bios_device *b)
{
	if (bios_device_type_num(a) < bios_device_type_num(b))
		return -1;
	else if (bios_device_type_num(a) == bios_device_type_num(b)) {
		if (is_pci(a))
			return sort_smbios(a, b);
		else return 0;
	}
	else if (bios_device_type_num(a) > bios_device_type_num(b))
		return 1;
	return 0;
}


static void insertion_sort_devices(struct bios_device *a, struct list_head *list,
				       int (*cmp)(const struct bios_device *, const struct bios_device *))
{
	struct bios_device *b;
	list_for_each_entry(b, list, node) {
		if (cmp(a, b) <= 0) {
			list_move_tail(&a->node, &b->node);
			return;
		}
	}
	list_move_tail(&a->node, list);
}


static void sort_device_list(struct libbiosdevname_state *state)
{
	LIST_HEAD(sorted_devices);
	struct bios_device *dev, *tmp;
	list_for_each_entry_safe(dev, tmp, &state->bios_devices, node) {
		insertion_sort_devices(dev, &sorted_devices, sort_by_type);
	}
	list_splice(&sorted_devices, &state->bios_devices);
}

static void match_eth_and_pci_devs(struct libbiosdevname_state *state)
{
	struct pci_device *p;
	struct bios_device *b;
	struct network_device *n;
	char pci_name[40];

	list_for_each_entry(p, &state->pci_devices, node) {
		if (!is_pci_network(p))
			continue;

		unparse_pci_name(pci_name, sizeof(pci_name), p->pci_dev);
		n = find_net_device_by_bus_info(state, pci_name);

		b = malloc(sizeof(*b));
		if (!b)
			continue;
		memset(b, 0, sizeof(*b));
		INIT_LIST_HEAD(&b->node);
		b->pcidev = p;
		b->netdev = n;
		if (n)
			claim_netdev(b->netdev);
		list_add(&b->node, &state->bios_devices);
	}
}

static void match_unknown_eths(struct libbiosdevname_state *state)
{
	struct bios_device *b;
	struct network_device *n;
	list_for_each_entry(n, &state->network_devices, node)
	{
		if (netdev_is_claimed(n))
			continue;
		if (!drvinfo_valid(n))
			continue;
		if (!is_ethernet(n)) /* for virtual interfaces */
			continue;
		b = malloc(sizeof(*b));
		if (!b)
			continue;
		memset(b, 0, sizeof(*b));
		INIT_LIST_HEAD(&b->node);
		b->netdev = n;
		list_add(&b->node, &state->bios_devices);
	}
}


static void match_all(struct libbiosdevname_state *state)
{
	match_eth_and_pci_devs(state);
	match_unknown_eths(state);
}

static struct libbiosdevname_state * alloc_state(void)
{
	struct libbiosdevname_state *state;
	state = malloc(sizeof(*state));
	if (!state)
		return NULL;
	INIT_LIST_HEAD(&state->bios_devices);
	INIT_LIST_HEAD(&state->pci_devices);
	INIT_LIST_HEAD(&state->network_devices);
	state->pacc = NULL;
	state->pirq_table = NULL;
	return state;
}

void cleanup_bios_devices(void *cookie)
{
	struct libbiosdevname_state *state = cookie;
	if (!state)
		return;
	free_bios_devices(state);
	free_eths(state);
	free_pci_devices(state);
	if (state->pacc)
		pci_cleanup(state->pacc);
	if (state->pirq_table)
		pirq_free_table(state->pirq_table);
}

static int duplicates(struct bios_device *a, struct bios_device *b)
{
	int lenA = -1, lenB = -1, rc = -1;
	if (a->bios_name)
		lenA = strlen(a->bios_name);
	if (b->bios_name)
		lenB = strlen(b->bios_name);
	if (lenA == lenB && lenA > 0)
		rc = strncmp(a->bios_name, b->bios_name, lenA);
	return !rc;
}

static void find_duplicates(struct libbiosdevname_state *state)
{
	struct bios_device *a = NULL, *b = NULL;
	list_for_each_entry(a, &state->bios_devices, node) {
		list_for_each_entry(b, &state->bios_devices, node) {
			if (a == b)
				continue;
			if (duplicates(a, b)) {
				a->duplicate = 1;
				b->duplicate = 1;
			}
		}
	}
}

void * setup_bios_devices(int namingpolicy, const char *prefix)
{
	int rc=1;
	struct libbiosdevname_state *state = alloc_state();

	if (!state)
		return NULL;

	rc = get_pci_devices(state);
	if (rc)
		goto out;

	get_eths(state);
	match_all(state);
	sort_device_list(state);
	rc = assign_bios_network_names(state, namingpolicy, prefix);
	if (rc)
		goto out;
	find_duplicates(state);
	return state;

out:
	cleanup_bios_devices(state);
	free(state);
	return NULL;
}