File: pci_hotplug_util.c

package info (click to toggle)
kernel-source-2.4.27 2.4.27-10sarge5
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 191,224 kB
  • ctags: 610,077
  • sloc: ansic: 3,299,602; asm: 164,708; makefile: 10,962; sh: 3,725; perl: 2,273; yacc: 1,651; cpp: 820; lex: 752; tcl: 577; awk: 251; lisp: 218; sed: 79
file content (456 lines) | stat: -rw-r--r-- 12,920 bytes parent folder | download
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
/*
 * PCI HotPlug Utility functions
 *
 * Copyright (C) 1995,2001 Compaq Computer Corporation
 * Copyright (C) 2001 Greg Kroah-Hartman (greg@kroah.com)
 * Copyright (C) 2001 IBM Corp.
 *
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
 * NON INFRINGEMENT.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * Send feedback to <greg@kroah.com>
 *
 */

#include <linux/config.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/slab.h>
#include <linux/pci.h>
#include "pci_hotplug.h"


#if !defined(CONFIG_HOTPLUG_PCI_MODULE)
	#define MY_NAME	"pci_hotplug"
#else
	#define MY_NAME	THIS_MODULE->name
#endif

#define dbg(fmt, arg...) do { if (debug) printk(KERN_DEBUG "%s: %s: " fmt , MY_NAME , __FUNCTION__ , ## arg); } while (0)
#define err(format, arg...) printk(KERN_ERR "%s: " format , MY_NAME , ## arg)
#define info(format, arg...) printk(KERN_INFO "%s: " format , MY_NAME , ## arg)
#define warn(format, arg...) printk(KERN_WARNING "%s: " format , MY_NAME , ## arg)


/* local variables */
static int debug;

static int build_dev (struct pci_bus *bus, unsigned int devfn, struct pci_dev **pci_dev)
{
	struct pci_dev *my_dev;

	my_dev = kmalloc (sizeof (struct pci_dev), GFP_KERNEL);
	if (!my_dev)
		return -ENOMEM;

	memset(my_dev, 0, sizeof(struct pci_dev));

	my_dev->devfn = devfn;
	my_dev->bus = bus;
	my_dev->sysdata = bus->sysdata;
	*pci_dev = my_dev;
	return 0;
}

/**
 * pci_bus_read_config_byte - read a byte from a pci device
 * @bus: pointer to the parent bus of the pci device to read from
 * @devfn: the device / function of the pci device to read from
 * @where: the location in the pci address space to read from
 * @value: pointer to where to place the data read
 *
 * Like pci_read_config_byte() but works for pci devices that do not have a
 * pci_dev structure set up yet.
 * Returns 0 on success.
 */
int pci_bus_read_config_byte (struct pci_bus *bus, unsigned int devfn, int where, u8 *value)
{
	struct pci_dev *dev = NULL;
	int result;

	dbg("%p, %d, %d, %p\n", bus, devfn, where, value);
	dev = pci_find_slot(bus->number, devfn);
	if (dev) {
		dbg("using native pci_dev\n");
		return pci_read_config_byte (dev, where, value);
	}
	
	result = build_dev(bus, devfn, &dev);
	if (result)
		return result;
	result = pci_read_config_byte(dev, where, value);
	kfree (dev);
	return result;
}

/**
 * pci_bus_read_config_word - read a word from a pci device
 * @bus: pointer to the parent bus of the pci device to read from
 * @devfn: the device / function of the pci device to read from
 * @where: the location on the pci address space to read from
 * @value: pointer to where to place the data read
 *
 * Like pci_read_config_word() but works for pci devices that do not have a
 * pci_dev structure set up yet. 
 * Returns 0 on success.
 */
int pci_bus_read_config_word (struct pci_bus *bus, unsigned int devfn, int where, u16 *value)
{
	struct pci_dev *dev = NULL;
	int result;

	dbg("%p, %d, %d, %p\n", bus, devfn, where, value);
	dev = pci_find_slot(bus->number, devfn);
	if (dev) {
		dbg("using native pci_dev\n");
		return pci_read_config_word (dev, where, value);
	}
	
	result = build_dev(bus, devfn, &dev);
	if (result)
		return result;
	result = pci_read_config_word(dev, where, value);
	kfree (dev);
	return result;
}

/**
 * pci_bus_read_config_dword - read a dword from a pci device
 * @bus: pointer to the parent bus of the pci device to read from
 * @devfn: the device / function of the pci device to read from
 * @where: the location on the pci address space to read from
 * @value: pointer to where to place the data read
 *
 * Like pci_read_config_dword() but works for pci devices that do not have a
 * pci_dev structure set up yet. 
 * Returns 0 on success.
 */
int pci_bus_read_config_dword (struct pci_bus *bus, unsigned int devfn, int where, u32 *value)
{
	struct pci_dev *dev = NULL;
	int result;

	dbg("%p, %d, %d, %p\n", bus, devfn, where, value);
	dev = pci_find_slot(bus->number, devfn);
	if (dev) {
		dbg("using native pci_dev\n");
		return pci_read_config_dword (dev, where, value);
	}
	
	result = build_dev(bus, devfn, &dev);
	if (result)
		return result;
	result = pci_read_config_dword(dev, where, value);
	kfree (dev);
	return result;
}

/**
 * pci_bus_write_config_byte - write a byte to a pci device
 * @bus: pointer to the parent bus of the pci device to read from
 * @devfn: the device / function of the pci device to read from
 * @where: the location on the pci address space to write to
 * @value: the value to write to the pci device
 *
 * Like pci_write_config_byte() but works for pci devices that do not have a
 * pci_dev structure set up yet. 
 * Returns 0 on success.
 */
int pci_bus_write_config_byte (struct pci_bus *bus, unsigned int devfn, int where, u8 value)
{
	struct pci_dev *dev = NULL;
	int result;

	dbg("%p, %d, %d, %d\n", bus, devfn, where, value);
	dev = pci_find_slot(bus->number, devfn);
	if (dev) {
		dbg("using native pci_dev\n");
		return pci_write_config_byte (dev, where, value);
	}
	
	result = build_dev(bus, devfn, &dev);
	if (result)
		return result;
	result = pci_write_config_byte(dev, where, value);
	kfree (dev);
	return result;
}

/**
 * pci_bus_write_config_word - write a word to a pci device
 * @bus: pointer to the parent bus of the pci device to read from
 * @devfn: the device / function of the pci device to read from
 * @where: the location on the pci address space to write to
 * @value: the value to write to the pci device
 *
 * Like pci_write_config_word() but works for pci devices that do not have a
 * pci_dev structure set up yet. 
 * Returns 0 on success.
 */
int pci_bus_write_config_word (struct pci_bus *bus, unsigned int devfn, int where, u16 value)
{
	struct pci_dev *dev = NULL;
	int result;

	dbg("%p, %d, %d, %d\n", bus, devfn, where, value);
	dev = pci_find_slot(bus->number, devfn);
	if (dev) {
		dbg("using native pci_dev\n");
		return pci_write_config_word (dev, where, value);
	}
	
	result = build_dev(bus, devfn, &dev);
	if (result)
		return result;
	result = pci_write_config_word(dev, where, value);
	kfree (dev);
	return result;
}

/**
 * pci_bus_write_config_dword - write a dword to a pci device
 * @bus: pointer to the parent bus of the pci device to read from
 * @devfn: the device / function of the pci device to read from
 * @where: the location on the pci address space to write to
 * @value: the value to write to the pci device
 *
 * Like pci_write_config_dword() but works for pci devices that do not have a
 * pci_dev structure set up yet. 
 * Returns 0 on success.
 */
int pci_bus_write_config_dword (struct pci_bus *bus, unsigned int devfn, int where, u32 value)
{
	struct pci_dev *dev = NULL;
	int result;

	dbg("%p, %d, %d, %d\n", bus, devfn, where, value);
	dev = pci_find_slot(bus->number, devfn);
	if (dev) {
		dbg("using native pci_dev\n");
		return pci_write_config_dword (dev, where, value);
	}
	
	result = build_dev(bus, devfn, &dev);
	if (result)
		return result;
	result = pci_write_config_dword(dev, where, value);
	kfree (dev);
	return result;
}

/*
 * This is code that scans the pci buses.
 * Every bus and every function is presented to a custom
 * function that can act upon it.
 */

static int pci_visit_bus (struct pci_visit * fn, struct pci_bus_wrapped *wrapped_bus, struct pci_dev_wrapped *wrapped_parent)
{
	struct list_head *ln;
	struct pci_dev *dev;
	struct pci_dev_wrapped wrapped_dev;
	int result = 0;

	dbg("scanning bus %02x\n", wrapped_bus->bus->number);

	if (fn->pre_visit_pci_bus) {
		result = fn->pre_visit_pci_bus(wrapped_bus, wrapped_parent);
		if (result)
			return result;
	}

	ln = wrapped_bus->bus->devices.next; 
	while (ln != &wrapped_bus->bus->devices) {
		dev = pci_dev_b(ln);
		ln = ln->next;

		memset(&wrapped_dev, 0, sizeof(struct pci_dev_wrapped));
		wrapped_dev.dev = dev;

		result = pci_visit_dev(fn, &wrapped_dev, wrapped_bus);
		if (result)
			return result;
	}

	if (fn->post_visit_pci_bus)
		result = fn->post_visit_pci_bus(wrapped_bus, wrapped_parent);

	return result;
}


static int pci_visit_bridge (struct pci_visit * fn, struct pci_dev_wrapped *wrapped_dev, struct pci_bus_wrapped *wrapped_parent)
{
	struct pci_bus *bus = wrapped_dev->dev->subordinate;
	struct pci_bus_wrapped wrapped_bus;
	int result;

	memset(&wrapped_bus, 0, sizeof(struct pci_bus_wrapped));
	wrapped_bus.bus = bus;

	dbg("scanning bridge %02x, %02x\n", wrapped_dev->dev->devfn >> 3,
	    wrapped_dev->dev->devfn & 0x7);

	if (fn->visit_pci_dev) {
		result = fn->visit_pci_dev(wrapped_dev, wrapped_parent);
		if (result)
			return result;
	}

	result = pci_visit_bus(fn, &wrapped_bus, wrapped_dev);
	return result;
}


int pci_visit_dev (struct pci_visit *fn, struct pci_dev_wrapped *wrapped_dev, struct pci_bus_wrapped *wrapped_parent)
{
	struct pci_dev* dev = wrapped_dev ? wrapped_dev->dev : NULL;
	int result = 0;

	if (!dev)
		return 0;

	if (fn->pre_visit_pci_dev) {
		result = fn->pre_visit_pci_dev(wrapped_dev, wrapped_parent);
		if (result)
			return result;
	}

	switch (dev->class >> 8) {
		case PCI_CLASS_BRIDGE_PCI:
			result = pci_visit_bridge(fn, wrapped_dev,
						  wrapped_parent);
			if (result)
				return result;
			break;
		default:
			dbg("scanning device %02x, %02x\n",
			    PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn));
			if (fn->visit_pci_dev) {
				result = fn->visit_pci_dev (wrapped_dev,
							    wrapped_parent);
				if (result)
					return result;
			}
	}

	if (fn->post_visit_pci_dev)
		result = fn->post_visit_pci_dev(wrapped_dev, wrapped_parent);

	return result;
}

/* Compatibility wrapper functions */

static struct pci_bus *alloc_bus(struct pci_ops *ops, u8 bus_nr)
{
	struct pci_bus *bus = kmalloc(sizeof(struct pci_bus), GFP_KERNEL);
	if (!bus)
		return NULL;
	memset(bus, 0, sizeof(struct pci_bus));
	bus->number = bus_nr;
	bus->ops = ops;
	return bus;
}

int pci_read_config_byte_nodev(struct pci_ops *ops, u8 bus_nr, u8 slot, u8 function, int where, u8 *value)
{
	int result;
	struct pci_bus *bus = alloc_bus(ops, bus_nr);
	if (!bus)
		return -ENOMEM;
	result = pci_bus_read_config_byte(bus, PCI_DEVFN(slot, function),
						where, value);
	kfree(bus);
	return result;
}

int pci_read_config_word_nodev(struct pci_ops *ops, u8 bus_nr, u8 slot, u8 function, int where, u16 *value)
{
	int result;
	struct pci_bus *bus = alloc_bus(ops, bus_nr);
	if (!bus)
		return -ENOMEM;
	result = pci_bus_read_config_word(bus, PCI_DEVFN(slot, function),
						where, value);
	kfree(bus);
	return result;
}

int pci_read_config_dword_nodev(struct pci_ops *ops, u8 bus_nr, u8 slot, u8 function, int where, u32 *value)
{
	int result;
	struct pci_bus *bus = alloc_bus(ops, bus_nr);
	if (!bus)
		return -ENOMEM;
	result = pci_bus_read_config_dword(bus, PCI_DEVFN(slot, function),
						where, value);
	kfree(bus);
	return result;
}

int pci_write_config_byte_nodev(struct pci_ops *ops, u8 bus_nr, u8 slot, u8 function, int where, u8 value)
{
	int result;
	struct pci_bus *bus = alloc_bus(ops, bus_nr);
	if (!bus)
		return -ENOMEM;
	result = pci_bus_write_config_byte(bus, PCI_DEVFN(slot, function),
						where, value);
	kfree(bus);
	return result;
}

int pci_write_config_word_nodev(struct pci_ops *ops, u8 bus_nr, u8 slot, u8 function, int where, u16 value)
{
	int result;
	struct pci_bus *bus = alloc_bus(ops, bus_nr);
	if (!bus)
		return -ENOMEM;
	result = pci_bus_write_config_word(bus, PCI_DEVFN(slot, function),
						where, value);
	kfree(bus);
	return result;
}

int pci_write_config_dword_nodev(struct pci_ops *ops, u8 bus_nr, u8 slot, u8 function, int where, u32 value)
{
	int result;
	struct pci_bus *bus = alloc_bus(ops, bus_nr);
	if (!bus)
		return -ENOMEM;
	result = pci_bus_write_config_dword(bus, PCI_DEVFN(slot, function),
						where, value);
	kfree(bus);
	return result;
}

EXPORT_SYMBOL(pci_visit_dev);
EXPORT_SYMBOL(pci_bus_read_config_byte);
EXPORT_SYMBOL(pci_bus_read_config_word);
EXPORT_SYMBOL(pci_bus_read_config_dword);
EXPORT_SYMBOL(pci_bus_write_config_byte);
EXPORT_SYMBOL(pci_bus_write_config_word);
EXPORT_SYMBOL(pci_bus_write_config_dword);
EXPORT_SYMBOL(pci_read_config_byte_nodev);
EXPORT_SYMBOL(pci_read_config_word_nodev);
EXPORT_SYMBOL(pci_read_config_dword_nodev);
EXPORT_SYMBOL(pci_write_config_byte_nodev);
EXPORT_SYMBOL(pci_write_config_word_nodev);
EXPORT_SYMBOL(pci_write_config_dword_nodev);