2005-04-16 18:20:36 -04:00
|
|
|
/*
|
|
|
|
* Fake PCI Hot Plug Controller Driver
|
|
|
|
*
|
|
|
|
* Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
|
|
|
|
* Copyright (C) 2003 IBM Corp.
|
|
|
|
* Copyright (C) 2003 Rolf Eike Beer <eike-kernel@sf-tec.de>
|
|
|
|
*
|
|
|
|
* Based on ideas and code from:
|
|
|
|
* Vladimir Kondratiev <vladimir.kondratiev@intel.com>
|
|
|
|
* Rolf Eike Beer <eike-kernel@sf-tec.de>
|
|
|
|
*
|
|
|
|
* 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, version 2 of the License.
|
|
|
|
*
|
|
|
|
* Send feedback to <greg@kroah.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
*
|
|
|
|
* This driver will "emulate" removing PCI devices from the system. If
|
|
|
|
* the "power" file is written to with "0" then the specified PCI device
|
|
|
|
* will be completely removed from the kernel.
|
|
|
|
*
|
|
|
|
* WARNING, this does NOT turn off the power to the PCI device. This is
|
|
|
|
* a "logical" removal, not a physical or electrical removal.
|
|
|
|
*
|
|
|
|
* Use this module at your own risk, you have been warned!
|
|
|
|
*
|
|
|
|
* Enabling PCI devices is left as an exercise for the reader...
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/pci.h>
|
2006-10-13 23:05:19 -04:00
|
|
|
#include <linux/pci_hotplug.h>
|
2005-04-16 18:20:36 -04:00
|
|
|
#include <linux/init.h>
|
2005-10-30 18:03:48 -05:00
|
|
|
#include <linux/string.h>
|
|
|
|
#include <linux/slab.h>
|
2008-01-25 11:23:56 -05:00
|
|
|
#include <linux/workqueue.h>
|
2005-04-16 18:20:36 -04:00
|
|
|
#include "../pci.h"
|
|
|
|
|
|
|
|
#if !defined(MODULE)
|
|
|
|
#define MY_NAME "fakephp"
|
|
|
|
#else
|
|
|
|
#define MY_NAME THIS_MODULE->name
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define dbg(format, arg...) \
|
|
|
|
do { \
|
|
|
|
if (debug) \
|
|
|
|
printk(KERN_DEBUG "%s: " format, \
|
|
|
|
MY_NAME , ## 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 DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>"
|
|
|
|
#define DRIVER_DESC "Fake PCI Hot Plug Controller Driver"
|
|
|
|
|
|
|
|
struct dummy_slot {
|
|
|
|
struct list_head node;
|
|
|
|
struct hotplug_slot *slot;
|
|
|
|
struct pci_dev *dev;
|
2008-01-25 11:23:56 -05:00
|
|
|
struct work_struct remove_work;
|
|
|
|
unsigned long removed;
|
2005-04-16 18:20:36 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
static int debug;
|
2008-10-20 19:42:03 -04:00
|
|
|
static int dup_slots;
|
2005-04-16 18:20:36 -04:00
|
|
|
static LIST_HEAD(slot_list);
|
2008-01-25 11:23:56 -05:00
|
|
|
static struct workqueue_struct *dummyphp_wq;
|
|
|
|
|
|
|
|
static void pci_rescan_worker(struct work_struct *work);
|
|
|
|
static DECLARE_WORK(pci_rescan_work, pci_rescan_worker);
|
2005-04-16 18:20:36 -04:00
|
|
|
|
|
|
|
static int enable_slot (struct hotplug_slot *slot);
|
|
|
|
static int disable_slot (struct hotplug_slot *slot);
|
|
|
|
|
|
|
|
static struct hotplug_slot_ops dummy_hotplug_slot_ops = {
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.enable_slot = enable_slot,
|
|
|
|
.disable_slot = disable_slot,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void dummy_release(struct hotplug_slot *slot)
|
|
|
|
{
|
|
|
|
struct dummy_slot *dslot = slot->private;
|
|
|
|
|
|
|
|
list_del(&dslot->node);
|
|
|
|
kfree(dslot->slot->info);
|
|
|
|
kfree(dslot->slot);
|
|
|
|
pci_dev_put(dslot->dev);
|
|
|
|
kfree(dslot);
|
|
|
|
}
|
|
|
|
|
2008-10-20 19:41:28 -04:00
|
|
|
#define SLOT_NAME_SIZE 8
|
|
|
|
|
2005-04-16 18:20:36 -04:00
|
|
|
static int add_slot(struct pci_dev *dev)
|
|
|
|
{
|
|
|
|
struct dummy_slot *dslot;
|
|
|
|
struct hotplug_slot *slot;
|
2008-10-20 19:41:28 -04:00
|
|
|
char name[SLOT_NAME_SIZE];
|
2005-04-16 18:20:36 -04:00
|
|
|
int retval = -ENOMEM;
|
2008-06-10 17:27:37 -04:00
|
|
|
static int count = 1;
|
2005-04-16 18:20:36 -04:00
|
|
|
|
2006-02-28 09:34:49 -05:00
|
|
|
slot = kzalloc(sizeof(struct hotplug_slot), GFP_KERNEL);
|
2005-04-16 18:20:36 -04:00
|
|
|
if (!slot)
|
|
|
|
goto error;
|
|
|
|
|
2006-02-28 09:34:49 -05:00
|
|
|
slot->info = kzalloc(sizeof(struct hotplug_slot_info), GFP_KERNEL);
|
2005-04-16 18:20:36 -04:00
|
|
|
if (!slot->info)
|
|
|
|
goto error_slot;
|
|
|
|
|
|
|
|
slot->info->power_status = 1;
|
|
|
|
slot->info->max_bus_speed = PCI_SPEED_UNKNOWN;
|
|
|
|
slot->info->cur_bus_speed = PCI_SPEED_UNKNOWN;
|
|
|
|
|
2008-01-25 11:23:56 -05:00
|
|
|
dslot = kzalloc(sizeof(struct dummy_slot), GFP_KERNEL);
|
2005-04-16 18:20:36 -04:00
|
|
|
if (!dslot)
|
|
|
|
goto error_info;
|
|
|
|
|
2008-10-20 19:42:03 -04:00
|
|
|
if (dup_slots)
|
|
|
|
snprintf(name, SLOT_NAME_SIZE, "fake");
|
|
|
|
else
|
|
|
|
snprintf(name, SLOT_NAME_SIZE, "fake%d", count++);
|
|
|
|
dbg("slot->name = %s\n", name);
|
2005-04-16 18:20:36 -04:00
|
|
|
slot->ops = &dummy_hotplug_slot_ops;
|
|
|
|
slot->release = &dummy_release;
|
|
|
|
slot->private = dslot;
|
|
|
|
|
2008-10-20 19:41:28 -04:00
|
|
|
retval = pci_hp_register(slot, dev->bus, PCI_SLOT(dev->devfn), name);
|
2005-04-16 18:20:36 -04:00
|
|
|
if (retval) {
|
|
|
|
err("pci_hp_register failed with error %d\n", retval);
|
|
|
|
goto error_dslot;
|
|
|
|
}
|
|
|
|
|
2008-10-20 19:41:28 -04:00
|
|
|
dbg("slot->name = %s\n", hotplug_slot_name(slot));
|
2005-04-16 18:20:36 -04:00
|
|
|
dslot->slot = slot;
|
|
|
|
dslot->dev = pci_dev_get(dev);
|
|
|
|
list_add (&dslot->node, &slot_list);
|
|
|
|
return retval;
|
|
|
|
|
|
|
|
error_dslot:
|
|
|
|
kfree(dslot);
|
|
|
|
error_info:
|
|
|
|
kfree(slot->info);
|
|
|
|
error_slot:
|
|
|
|
kfree(slot);
|
|
|
|
error:
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int __init pci_scan_buses(void)
|
|
|
|
{
|
|
|
|
struct pci_dev *dev = NULL;
|
2008-06-10 17:27:37 -04:00
|
|
|
int lastslot = 0;
|
2005-04-16 18:20:36 -04:00
|
|
|
|
|
|
|
while ((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
|
2008-06-10 17:27:37 -04:00
|
|
|
if (PCI_FUNC(dev->devfn) > 0 &&
|
|
|
|
lastslot == PCI_SLOT(dev->devfn))
|
|
|
|
continue;
|
|
|
|
lastslot = PCI_SLOT(dev->devfn);
|
|
|
|
add_slot(dev);
|
2005-04-16 18:20:36 -04:00
|
|
|
}
|
|
|
|
|
2008-06-10 17:27:37 -04:00
|
|
|
return 0;
|
2005-04-16 18:20:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void remove_slot(struct dummy_slot *dslot)
|
|
|
|
{
|
|
|
|
int retval;
|
|
|
|
|
2008-10-20 19:41:28 -04:00
|
|
|
dbg("removing slot %s\n", hotplug_slot_name(dslot->slot));
|
2005-04-16 18:20:36 -04:00
|
|
|
retval = pci_hp_deregister(dslot->slot);
|
|
|
|
if (retval)
|
2008-10-20 19:41:28 -04:00
|
|
|
err("Problem unregistering a slot %s\n",
|
|
|
|
hotplug_slot_name(dslot->slot));
|
2005-04-16 18:20:36 -04:00
|
|
|
}
|
|
|
|
|
2008-01-25 11:23:56 -05:00
|
|
|
/* called from the single-threaded workqueue handler to remove a slot */
|
|
|
|
static void remove_slot_worker(struct work_struct *work)
|
|
|
|
{
|
|
|
|
struct dummy_slot *dslot =
|
|
|
|
container_of(work, struct dummy_slot, remove_work);
|
|
|
|
remove_slot(dslot);
|
|
|
|
}
|
|
|
|
|
2005-04-16 18:20:36 -04:00
|
|
|
/**
|
2007-11-28 12:04:30 -05:00
|
|
|
* pci_rescan_slot - Rescan slot
|
|
|
|
* @temp: Device template. Should be set: bus and devfn.
|
2005-04-16 18:20:36 -04:00
|
|
|
*
|
2007-11-28 12:04:30 -05:00
|
|
|
* Tries hard not to re-enable already existing devices;
|
|
|
|
* also handles scanning of subfunctions.
|
2005-04-16 18:20:36 -04:00
|
|
|
*/
|
|
|
|
static void pci_rescan_slot(struct pci_dev *temp)
|
|
|
|
{
|
|
|
|
struct pci_bus *bus = temp->bus;
|
|
|
|
struct pci_dev *dev;
|
|
|
|
int func;
|
2006-08-28 14:43:25 -04:00
|
|
|
int retval;
|
2005-04-16 18:20:36 -04:00
|
|
|
u8 hdr_type;
|
2006-08-28 14:43:25 -04:00
|
|
|
|
2005-04-16 18:20:36 -04:00
|
|
|
if (!pci_read_config_byte(temp, PCI_HEADER_TYPE, &hdr_type)) {
|
|
|
|
temp->hdr_type = hdr_type & 0x7f;
|
2006-09-29 13:36:15 -04:00
|
|
|
if ((dev = pci_get_slot(bus, temp->devfn)) != NULL)
|
|
|
|
pci_dev_put(dev);
|
|
|
|
else {
|
2005-04-16 18:20:36 -04:00
|
|
|
dev = pci_scan_single_device(bus, temp->devfn);
|
|
|
|
if (dev) {
|
|
|
|
dbg("New device on %s function %x:%x\n",
|
|
|
|
bus->name, temp->devfn >> 3,
|
|
|
|
temp->devfn & 7);
|
2006-08-28 14:43:25 -04:00
|
|
|
retval = pci_bus_add_device(dev);
|
|
|
|
if (retval)
|
|
|
|
dev_err(&dev->dev, "error adding "
|
|
|
|
"device, continuing.\n");
|
|
|
|
else
|
|
|
|
add_slot(dev);
|
2005-04-16 18:20:36 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* multifunction device? */
|
|
|
|
if (!(hdr_type & 0x80))
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* continue scanning for other functions */
|
|
|
|
for (func = 1, temp->devfn++; func < 8; func++, temp->devfn++) {
|
|
|
|
if (pci_read_config_byte(temp, PCI_HEADER_TYPE, &hdr_type))
|
|
|
|
continue;
|
|
|
|
temp->hdr_type = hdr_type & 0x7f;
|
|
|
|
|
2006-09-29 13:36:15 -04:00
|
|
|
if ((dev = pci_get_slot(bus, temp->devfn)) != NULL)
|
|
|
|
pci_dev_put(dev);
|
|
|
|
else {
|
2005-04-16 18:20:36 -04:00
|
|
|
dev = pci_scan_single_device(bus, temp->devfn);
|
|
|
|
if (dev) {
|
|
|
|
dbg("New device on %s function %x:%x\n",
|
|
|
|
bus->name, temp->devfn >> 3,
|
|
|
|
temp->devfn & 7);
|
2006-08-28 14:43:25 -04:00
|
|
|
retval = pci_bus_add_device(dev);
|
|
|
|
if (retval)
|
|
|
|
dev_err(&dev->dev, "error adding "
|
|
|
|
"device, continuing.\n");
|
|
|
|
else
|
|
|
|
add_slot(dev);
|
2005-04-16 18:20:36 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2007-11-28 12:04:30 -05:00
|
|
|
* pci_rescan_bus - Rescan PCI bus
|
|
|
|
* @bus: the PCI bus to rescan
|
2005-04-16 18:20:36 -04:00
|
|
|
*
|
2007-11-28 12:04:30 -05:00
|
|
|
* Call pci_rescan_slot for each possible function of the bus.
|
2005-04-16 18:20:36 -04:00
|
|
|
*/
|
|
|
|
static void pci_rescan_bus(const struct pci_bus *bus)
|
|
|
|
{
|
|
|
|
unsigned int devfn;
|
|
|
|
struct pci_dev *dev;
|
2007-04-05 03:19:09 -04:00
|
|
|
dev = alloc_pci_dev();
|
2005-04-16 18:20:36 -04:00
|
|
|
if (!dev)
|
|
|
|
return;
|
|
|
|
|
|
|
|
dev->bus = (struct pci_bus*)bus;
|
|
|
|
dev->sysdata = bus->sysdata;
|
|
|
|
for (devfn = 0; devfn < 0x100; devfn += 8) {
|
|
|
|
dev->devfn = devfn;
|
|
|
|
pci_rescan_slot(dev);
|
|
|
|
}
|
|
|
|
kfree(dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* recursively scan all buses */
|
|
|
|
static void pci_rescan_buses(const struct list_head *list)
|
|
|
|
{
|
|
|
|
const struct list_head *l;
|
|
|
|
list_for_each(l,list) {
|
|
|
|
const struct pci_bus *b = pci_bus_b(l);
|
|
|
|
pci_rescan_bus(b);
|
|
|
|
pci_rescan_buses(&b->children);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* initiate rescan of all pci buses */
|
|
|
|
static inline void pci_rescan(void) {
|
|
|
|
pci_rescan_buses(&pci_root_buses);
|
|
|
|
}
|
|
|
|
|
2008-01-25 11:23:56 -05:00
|
|
|
/* called from the single-threaded workqueue handler to rescan all pci buses */
|
|
|
|
static void pci_rescan_worker(struct work_struct *work)
|
|
|
|
{
|
|
|
|
pci_rescan();
|
|
|
|
}
|
2005-04-16 18:20:36 -04:00
|
|
|
|
|
|
|
static int enable_slot(struct hotplug_slot *hotplug_slot)
|
|
|
|
{
|
|
|
|
/* mis-use enable_slot for rescanning of the pci bus */
|
2008-01-25 11:23:56 -05:00
|
|
|
cancel_work_sync(&pci_rescan_work);
|
|
|
|
queue_work(dummyphp_wq, &pci_rescan_work);
|
PCI: Hotplug: fakephp: Return success, not ENODEV, when bus rescan is triggered
The 'power' attribute of the fakephp driver originally only let one turn a
slot off. If one tried to turn a slot on (echo 1 > .../power), it would
return ENODEV, as fakephp did not support this function.
An old (pre-git) patch changed this:
2004/11/11 16:33:31-08:00 jdittmer
[PATCH] fakephp: add pci bus rescan ability
http://article.gmane.org/gmane.linux.kernel/251183
Now writing "1" to the power attribute has the effect of triggering a bus
rescan, but it still returns ENODEV, probably an oversight in the above
patch.
Using the BusyBox echo will not produce an error message, but will
trigger *two* bus rescans (and return an exit code of 1):
~ # strace echo -n 1 > /sys/bus/pci/slots/0000:00:00.0/power
...
write(1, "1", 1) = -1 ENODEV (No such device)
write(1, "1", 1) = -1 ENODEV (No such device)
exit(1) = ?
Using cp gives a write error, even though the write did happen and a rescan
was triggered:
~ # echo -n 1 > tmp ; cp tmp /sys/bus/pci/slots/0000:00:00.0/power
cp: Write Error: No such device
It seems much better to return success instead of failure. The actual
status of the bus rescan is hard to return. It happens asynchronously in a
work thread, so the sysfs store functions returns before any status is
ready (the whole point of the work queue). And even if it didn't do this,
the rescan doesn't have any clear status to return.
Signed-off-by: Trent Piepho <tpiepho@freescale.com>
CC: Jan Dittmer <jdittmer@ppp0.net>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
2008-04-07 19:04:16 -04:00
|
|
|
return 0;
|
2005-04-16 18:20:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int disable_slot(struct hotplug_slot *slot)
|
|
|
|
{
|
|
|
|
struct dummy_slot *dslot;
|
|
|
|
struct pci_dev *dev;
|
|
|
|
int func;
|
|
|
|
|
|
|
|
if (!slot)
|
|
|
|
return -ENODEV;
|
|
|
|
dslot = slot->private;
|
|
|
|
|
2008-10-20 19:41:28 -04:00
|
|
|
dbg("%s - physical_slot = %s\n", __func__, hotplug_slot_name(slot));
|
2005-04-16 18:20:36 -04:00
|
|
|
|
2008-06-10 17:27:37 -04:00
|
|
|
for (func = 7; func >= 0; func--) {
|
|
|
|
dev = pci_get_slot(dslot->dev->bus, dslot->dev->devfn + func);
|
|
|
|
if (!dev)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (test_and_set_bit(0, &dslot->removed)) {
|
|
|
|
dbg("Slot already scheduled for removal\n");
|
|
|
|
return -ENODEV;
|
2005-04-16 18:20:36 -04:00
|
|
|
}
|
|
|
|
|
2008-09-05 17:05:03 -04:00
|
|
|
/* remove the device from the pci core */
|
|
|
|
pci_remove_bus_device(dev);
|
|
|
|
|
2008-06-10 17:27:37 -04:00
|
|
|
/* queue work item to blow away this sysfs entry and other
|
|
|
|
* parts.
|
|
|
|
*/
|
|
|
|
INIT_WORK(&dslot->remove_work, remove_slot_worker);
|
|
|
|
queue_work(dummyphp_wq, &dslot->remove_work);
|
2005-04-16 18:20:36 -04:00
|
|
|
|
2008-06-10 17:27:37 -04:00
|
|
|
pci_dev_put(dev);
|
|
|
|
}
|
2005-04-16 18:20:36 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void cleanup_slots (void)
|
|
|
|
{
|
|
|
|
struct list_head *tmp;
|
|
|
|
struct list_head *next;
|
|
|
|
struct dummy_slot *dslot;
|
|
|
|
|
2008-01-25 11:23:56 -05:00
|
|
|
destroy_workqueue(dummyphp_wq);
|
2005-04-16 18:20:36 -04:00
|
|
|
list_for_each_safe (tmp, next, &slot_list) {
|
|
|
|
dslot = list_entry (tmp, struct dummy_slot, node);
|
|
|
|
remove_slot(dslot);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static int __init dummyphp_init(void)
|
|
|
|
{
|
|
|
|
info(DRIVER_DESC "\n");
|
|
|
|
|
2008-01-25 11:23:56 -05:00
|
|
|
dummyphp_wq = create_singlethread_workqueue(MY_NAME);
|
|
|
|
if (!dummyphp_wq)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2005-04-16 18:20:36 -04:00
|
|
|
return pci_scan_buses();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void __exit dummyphp_exit(void)
|
|
|
|
{
|
|
|
|
cleanup_slots();
|
|
|
|
}
|
|
|
|
|
|
|
|
module_init(dummyphp_init);
|
|
|
|
module_exit(dummyphp_exit);
|
|
|
|
|
|
|
|
MODULE_AUTHOR(DRIVER_AUTHOR);
|
|
|
|
MODULE_DESCRIPTION(DRIVER_DESC);
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
module_param(debug, bool, S_IRUGO | S_IWUSR);
|
|
|
|
MODULE_PARM_DESC(debug, "Debugging mode enabled or not");
|
2008-10-20 19:42:03 -04:00
|
|
|
module_param(dup_slots, bool, S_IRUGO | S_IWUSR);
|
|
|
|
MODULE_PARM_DESC(dup_slots, "Force duplicate slot names for debugging");
|