summaryrefslogtreecommitdiff
path: root/src/bios_device.c
blob: 5c275c88b535961a6506a15c12a2372c62cc14c4 (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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/*
 *  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 <net/if.h>
#include "list.h"
#include "bios_device.h"
#include "state.h"
#include "libbiosdevname.h"
#include "dmidecode/dmidecode.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[200];
	printf("BIOS device: %s\n", 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);
	}
	else if (is_pcmcia(dev)) {
		unparse_pcmcia_device(buf, sizeof(buf), dev->pcmciadev);
		printf("%s", buf);
	}

	printf("\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))
			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);
}


static int sort_pcmcia(const struct bios_device *bdev_a, const struct bios_device *bdev_b)
{
	const struct pcmcia_device *a = bdev_a->pcmciadev;
	const struct pcmcia_device *b = bdev_b->pcmciadev;

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

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

	return 0;
}

enum bios_device_types {
	IS_PCI,
	IS_PCMCIA,
	IS_UNKNOWN_TYPE,
};

static int bios_device_type_num(const struct bios_device *dev)
{
	if (is_pci(dev))
		return IS_PCI;
	else if (is_pcmcia(dev))
		return IS_PCMCIA;
	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 if (is_pcmcia(a))
			return sort_pcmcia(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 int set_slot_index(struct libbiosdevname_state *state)
{
	struct bios_device *dev;
	int prevslot=-1;
	int index=0;

	list_for_each_entry(dev, &state->bios_devices, node) {
		if (!dev->pcidev)
			continue;
		if (dev->pcidev->physical_slot != prevslot)
			index=0;
		else
			index++;
		dev->pcidev->index_in_slot = index;
		prevslot = dev->pcidev->physical_slot;
	}
	return 0;
}

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);
	set_slot_index(state);
}

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);
		if (!n)
			continue;

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

static void match_eth_and_pcmcia(struct libbiosdevname_state *state)
{
	struct pcmcia_device *p;
	struct bios_device *b;
	char pcmcia_name[40];

	list_for_each_entry(p, &state->pcmcia_devices, node) {
		if (!is_pcmcia_network(p))
			continue;

		b = malloc(sizeof(*b));
		if (!b)
			continue;
		memset(b, 0, sizeof(*b));
		INIT_LIST_HEAD(&b->node);
		b->pcmciadev = p;

		unparse_pcmcia_name(pcmcia_name, sizeof(pcmcia_name), p);
		b->netdev = find_net_device_by_bus_info(state, pcmcia_name);

		memset(b->bios_name, 0, sizeof(b->bios_name));
		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_eth_and_pcmcia(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);
	INIT_LIST_HEAD(&state->pcmcia_devices);
	return state;
}

void cleanup_bios_devices(void *cookie)
{
	struct libbiosdevname_state *state = cookie;
	if (!state)
		return;
	free_bios_devices(state);
	free_eths(state);
	free_pcmcia_devices(state);
	free_pci_devices(state);
}

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

	if (!state)
		return NULL;

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

	rc = get_pcmcia_devices(state);
	if (rc)
		goto out;
	rc = dmidecode_main(state);
	if (rc)
		goto out;
	get_eths(state);
	match_all(state);
	if (sortroutine != nosort) {
		sort_device_list(state);
	}
	assign_bios_network_names(state, sortroutine, namingpolicy);
	return state;

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