]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/pci/hotplug/fakephp.c
PCI: fix pcie_portdrv_restore_config undefined without CONFIG_PM error
[mirror_ubuntu-bionic-kernel.git] / drivers / pci / hotplug / fakephp.c
CommitLineData
1da177e4
LT
1/*
2 * Fake PCI Hot Plug Controller Driver
3 *
4 * Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
5 * Copyright (C) 2003 IBM Corp.
6 * Copyright (C) 2003 Rolf Eike Beer <eike-kernel@sf-tec.de>
7 *
8 * Based on ideas and code from:
9 * Vladimir Kondratiev <vladimir.kondratiev@intel.com>
10 * Rolf Eike Beer <eike-kernel@sf-tec.de>
11 *
12 * All rights reserved.
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, version 2 of the License.
17 *
18 * Send feedback to <greg@kroah.com>
19 */
20
21/*
22 *
23 * This driver will "emulate" removing PCI devices from the system. If
24 * the "power" file is written to with "0" then the specified PCI device
25 * will be completely removed from the kernel.
26 *
27 * WARNING, this does NOT turn off the power to the PCI device. This is
28 * a "logical" removal, not a physical or electrical removal.
29 *
30 * Use this module at your own risk, you have been warned!
31 *
32 * Enabling PCI devices is left as an exercise for the reader...
33 *
34 */
1da177e4
LT
35#include <linux/kernel.h>
36#include <linux/module.h>
37#include <linux/pci.h>
38#include <linux/init.h>
4e57b681
TS
39#include <linux/string.h>
40#include <linux/slab.h>
1da177e4
LT
41#include "pci_hotplug.h"
42#include "../pci.h"
43
44#if !defined(MODULE)
45 #define MY_NAME "fakephp"
46#else
47 #define MY_NAME THIS_MODULE->name
48#endif
49
50#define dbg(format, arg...) \
51 do { \
52 if (debug) \
53 printk(KERN_DEBUG "%s: " format, \
54 MY_NAME , ## arg); \
55 } while (0)
56#define err(format, arg...) printk(KERN_ERR "%s: " format, MY_NAME , ## arg)
57#define info(format, arg...) printk(KERN_INFO "%s: " format, MY_NAME , ## arg)
58
59#define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>"
60#define DRIVER_DESC "Fake PCI Hot Plug Controller Driver"
61
62struct dummy_slot {
63 struct list_head node;
64 struct hotplug_slot *slot;
65 struct pci_dev *dev;
66};
67
68static int debug;
69static LIST_HEAD(slot_list);
70
71static int enable_slot (struct hotplug_slot *slot);
72static int disable_slot (struct hotplug_slot *slot);
73
74static struct hotplug_slot_ops dummy_hotplug_slot_ops = {
75 .owner = THIS_MODULE,
76 .enable_slot = enable_slot,
77 .disable_slot = disable_slot,
78};
79
80static void dummy_release(struct hotplug_slot *slot)
81{
82 struct dummy_slot *dslot = slot->private;
83
84 list_del(&dslot->node);
85 kfree(dslot->slot->info);
86 kfree(dslot->slot);
87 pci_dev_put(dslot->dev);
88 kfree(dslot);
89}
90
91static int add_slot(struct pci_dev *dev)
92{
93 struct dummy_slot *dslot;
94 struct hotplug_slot *slot;
95 int retval = -ENOMEM;
96
f5afe806 97 slot = kzalloc(sizeof(struct hotplug_slot), GFP_KERNEL);
1da177e4
LT
98 if (!slot)
99 goto error;
1da177e4 100
f5afe806 101 slot->info = kzalloc(sizeof(struct hotplug_slot_info), GFP_KERNEL);
1da177e4
LT
102 if (!slot->info)
103 goto error_slot;
1da177e4
LT
104
105 slot->info->power_status = 1;
106 slot->info->max_bus_speed = PCI_SPEED_UNKNOWN;
107 slot->info->cur_bus_speed = PCI_SPEED_UNKNOWN;
108
109 slot->name = &dev->dev.bus_id[0];
110 dbg("slot->name = %s\n", slot->name);
111
112 dslot = kmalloc(sizeof(struct dummy_slot), GFP_KERNEL);
113 if (!dslot)
114 goto error_info;
115
116 slot->ops = &dummy_hotplug_slot_ops;
117 slot->release = &dummy_release;
118 slot->private = dslot;
119
120 retval = pci_hp_register(slot);
121 if (retval) {
122 err("pci_hp_register failed with error %d\n", retval);
123 goto error_dslot;
124 }
125
126 dslot->slot = slot;
127 dslot->dev = pci_dev_get(dev);
128 list_add (&dslot->node, &slot_list);
129 return retval;
130
131error_dslot:
132 kfree(dslot);
133error_info:
134 kfree(slot->info);
135error_slot:
136 kfree(slot);
137error:
138 return retval;
139}
140
141static int __init pci_scan_buses(void)
142{
143 struct pci_dev *dev = NULL;
144 int retval = 0;
145
146 while ((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
147 retval = add_slot(dev);
148 if (retval) {
149 pci_dev_put(dev);
150 break;
151 }
152 }
153
154 return retval;
155}
156
157static void remove_slot(struct dummy_slot *dslot)
158{
159 int retval;
160
161 dbg("removing slot %s\n", dslot->slot->name);
162 retval = pci_hp_deregister(dslot->slot);
163 if (retval)
164 err("Problem unregistering a slot %s\n", dslot->slot->name);
165}
166
167/**
168 * Rescan slot.
169 * Tries hard not to re-enable already existing devices
170 * also handles scanning of subfunctions
171 *
172 * @param temp Device template. Should be set: bus and devfn.
173 */
174static void pci_rescan_slot(struct pci_dev *temp)
175{
176 struct pci_bus *bus = temp->bus;
177 struct pci_dev *dev;
178 int func;
b19441af 179 int retval;
1da177e4 180 u8 hdr_type;
b19441af 181
1da177e4
LT
182 if (!pci_read_config_byte(temp, PCI_HEADER_TYPE, &hdr_type)) {
183 temp->hdr_type = hdr_type & 0x7f;
184 if (!pci_find_slot(bus->number, temp->devfn)) {
185 dev = pci_scan_single_device(bus, temp->devfn);
186 if (dev) {
187 dbg("New device on %s function %x:%x\n",
188 bus->name, temp->devfn >> 3,
189 temp->devfn & 7);
b19441af
GKH
190 retval = pci_bus_add_device(dev);
191 if (retval)
192 dev_err(&dev->dev, "error adding "
193 "device, continuing.\n");
194 else
195 add_slot(dev);
1da177e4
LT
196 }
197 }
198 /* multifunction device? */
199 if (!(hdr_type & 0x80))
200 return;
201
202 /* continue scanning for other functions */
203 for (func = 1, temp->devfn++; func < 8; func++, temp->devfn++) {
204 if (pci_read_config_byte(temp, PCI_HEADER_TYPE, &hdr_type))
205 continue;
206 temp->hdr_type = hdr_type & 0x7f;
207
208 if (!pci_find_slot(bus->number, temp->devfn)) {
209 dev = pci_scan_single_device(bus, temp->devfn);
210 if (dev) {
211 dbg("New device on %s function %x:%x\n",
212 bus->name, temp->devfn >> 3,
213 temp->devfn & 7);
b19441af
GKH
214 retval = pci_bus_add_device(dev);
215 if (retval)
216 dev_err(&dev->dev, "error adding "
217 "device, continuing.\n");
218 else
219 add_slot(dev);
1da177e4
LT
220 }
221 }
222 }
223 }
224}
225
226
227/**
228 * Rescan PCI bus.
229 * call pci_rescan_slot for each possible function of the bus
230 *
231 * @param bus
232 */
233static void pci_rescan_bus(const struct pci_bus *bus)
234{
235 unsigned int devfn;
236 struct pci_dev *dev;
f5afe806 237 dev = kzalloc(sizeof(struct pci_dev), GFP_KERNEL);
1da177e4
LT
238 if (!dev)
239 return;
240
1da177e4
LT
241 dev->bus = (struct pci_bus*)bus;
242 dev->sysdata = bus->sysdata;
243 for (devfn = 0; devfn < 0x100; devfn += 8) {
244 dev->devfn = devfn;
245 pci_rescan_slot(dev);
246 }
247 kfree(dev);
248}
249
250/* recursively scan all buses */
251static void pci_rescan_buses(const struct list_head *list)
252{
253 const struct list_head *l;
254 list_for_each(l,list) {
255 const struct pci_bus *b = pci_bus_b(l);
256 pci_rescan_bus(b);
257 pci_rescan_buses(&b->children);
258 }
259}
260
261/* initiate rescan of all pci buses */
262static inline void pci_rescan(void) {
263 pci_rescan_buses(&pci_root_buses);
264}
265
266
267static int enable_slot(struct hotplug_slot *hotplug_slot)
268{
269 /* mis-use enable_slot for rescanning of the pci bus */
270 pci_rescan();
271 return -ENODEV;
272}
273
274/* find the hotplug_slot for the pci_dev */
275static struct hotplug_slot *get_slot_from_dev(struct pci_dev *dev)
276{
277 struct dummy_slot *dslot;
278
279 list_for_each_entry(dslot, &slot_list, node) {
280 if (dslot->dev == dev)
281 return dslot->slot;
282 }
283 return NULL;
284}
285
286
287static int disable_slot(struct hotplug_slot *slot)
288{
289 struct dummy_slot *dslot;
290 struct hotplug_slot *hslot;
291 struct pci_dev *dev;
292 int func;
293
294 if (!slot)
295 return -ENODEV;
296 dslot = slot->private;
297
298 dbg("%s - physical_slot = %s\n", __FUNCTION__, slot->name);
299
300 /* don't disable bridged devices just yet, we can't handle them easily... */
301 if (dslot->dev->subordinate) {
302 err("Can't remove PCI devices with other PCI devices behind it yet.\n");
303 return -ENODEV;
304 }
305 /* search for subfunctions and disable them first */
306 if (!(dslot->dev->devfn & 7)) {
307 for (func = 1; func < 8; func++) {
308 dev = pci_find_slot(dslot->dev->bus->number,
309 dslot->dev->devfn + func);
310 if (dev) {
311 hslot = get_slot_from_dev(dev);
312 if (hslot)
313 disable_slot(hslot);
314 else {
315 err("Hotplug slot not found for subfunction of PCI device\n");
316 return -ENODEV;
317 }
318 } else
319 dbg("No device in slot found\n");
320 }
321 }
322
323 /* remove the device from the pci core */
324 pci_remove_bus_device(dslot->dev);
325
326 /* blow away this sysfs entry and other parts. */
327 remove_slot(dslot);
328
329 return 0;
330}
331
332static void cleanup_slots (void)
333{
334 struct list_head *tmp;
335 struct list_head *next;
336 struct dummy_slot *dslot;
337
338 list_for_each_safe (tmp, next, &slot_list) {
339 dslot = list_entry (tmp, struct dummy_slot, node);
340 remove_slot(dslot);
341 }
342
343}
344
345static int __init dummyphp_init(void)
346{
347 info(DRIVER_DESC "\n");
348
349 return pci_scan_buses();
350}
351
352
353static void __exit dummyphp_exit(void)
354{
355 cleanup_slots();
356}
357
358module_init(dummyphp_init);
359module_exit(dummyphp_exit);
360
361MODULE_AUTHOR(DRIVER_AUTHOR);
362MODULE_DESCRIPTION(DRIVER_DESC);
363MODULE_LICENSE("GPL");
364module_param(debug, bool, S_IRUGO | S_IWUSR);
365MODULE_PARM_DESC(debug, "Debugging mode enabled or not");
366