0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017 #include <linux/module.h> /* try_module_get & module_put */
0018 #include <linux/moduleparam.h>
0019 #include <linux/kernel.h>
0020 #include <linux/types.h>
0021 #include <linux/list.h>
0022 #include <linux/kobject.h>
0023 #include <linux/sysfs.h>
0024 #include <linux/pagemap.h>
0025 #include <linux/init.h>
0026 #include <linux/mount.h>
0027 #include <linux/namei.h>
0028 #include <linux/mutex.h>
0029 #include <linux/pci.h>
0030 #include <linux/pci_hotplug.h>
0031 #include <linux/uaccess.h>
0032 #include "../pci.h"
0033 #include "cpci_hotplug.h"
0034
0035 #define MY_NAME "pci_hotplug"
0036
0037 #define dbg(fmt, arg...) do { if (debug) printk(KERN_DEBUG "%s: %s: " fmt, MY_NAME, __func__, ## arg); } while (0)
0038 #define err(format, arg...) printk(KERN_ERR "%s: " format, MY_NAME, ## arg)
0039 #define info(format, arg...) printk(KERN_INFO "%s: " format, MY_NAME, ## arg)
0040 #define warn(format, arg...) printk(KERN_WARNING "%s: " format, MY_NAME, ## arg)
0041
0042
0043 static bool debug;
0044
0045 static LIST_HEAD(pci_hotplug_slot_list);
0046 static DEFINE_MUTEX(pci_hp_mutex);
0047
0048
0049 #define GET_STATUS(name, type) \
0050 static int get_##name(struct hotplug_slot *slot, type *value) \
0051 { \
0052 const struct hotplug_slot_ops *ops = slot->ops; \
0053 int retval = 0; \
0054 if (!try_module_get(slot->owner)) \
0055 return -ENODEV; \
0056 if (ops->get_##name) \
0057 retval = ops->get_##name(slot, value); \
0058 module_put(slot->owner); \
0059 return retval; \
0060 }
0061
0062 GET_STATUS(power_status, u8)
0063 GET_STATUS(attention_status, u8)
0064 GET_STATUS(latch_status, u8)
0065 GET_STATUS(adapter_status, u8)
0066
0067 static ssize_t power_read_file(struct pci_slot *pci_slot, char *buf)
0068 {
0069 int retval;
0070 u8 value;
0071
0072 retval = get_power_status(pci_slot->hotplug, &value);
0073 if (retval)
0074 return retval;
0075
0076 return sysfs_emit(buf, "%d\n", value);
0077 }
0078
0079 static ssize_t power_write_file(struct pci_slot *pci_slot, const char *buf,
0080 size_t count)
0081 {
0082 struct hotplug_slot *slot = pci_slot->hotplug;
0083 unsigned long lpower;
0084 u8 power;
0085 int retval = 0;
0086
0087 lpower = simple_strtoul(buf, NULL, 10);
0088 power = (u8)(lpower & 0xff);
0089 dbg("power = %d\n", power);
0090
0091 if (!try_module_get(slot->owner)) {
0092 retval = -ENODEV;
0093 goto exit;
0094 }
0095 switch (power) {
0096 case 0:
0097 if (slot->ops->disable_slot)
0098 retval = slot->ops->disable_slot(slot);
0099 break;
0100
0101 case 1:
0102 if (slot->ops->enable_slot)
0103 retval = slot->ops->enable_slot(slot);
0104 break;
0105
0106 default:
0107 err("Illegal value specified for power\n");
0108 retval = -EINVAL;
0109 }
0110 module_put(slot->owner);
0111
0112 exit:
0113 if (retval)
0114 return retval;
0115 return count;
0116 }
0117
0118 static struct pci_slot_attribute hotplug_slot_attr_power = {
0119 .attr = {.name = "power", .mode = S_IFREG | S_IRUGO | S_IWUSR},
0120 .show = power_read_file,
0121 .store = power_write_file
0122 };
0123
0124 static ssize_t attention_read_file(struct pci_slot *pci_slot, char *buf)
0125 {
0126 int retval;
0127 u8 value;
0128
0129 retval = get_attention_status(pci_slot->hotplug, &value);
0130 if (retval)
0131 return retval;
0132
0133 return sysfs_emit(buf, "%d\n", value);
0134 }
0135
0136 static ssize_t attention_write_file(struct pci_slot *pci_slot, const char *buf,
0137 size_t count)
0138 {
0139 struct hotplug_slot *slot = pci_slot->hotplug;
0140 const struct hotplug_slot_ops *ops = slot->ops;
0141 unsigned long lattention;
0142 u8 attention;
0143 int retval = 0;
0144
0145 lattention = simple_strtoul(buf, NULL, 10);
0146 attention = (u8)(lattention & 0xff);
0147 dbg(" - attention = %d\n", attention);
0148
0149 if (!try_module_get(slot->owner)) {
0150 retval = -ENODEV;
0151 goto exit;
0152 }
0153 if (ops->set_attention_status)
0154 retval = ops->set_attention_status(slot, attention);
0155 module_put(slot->owner);
0156
0157 exit:
0158 if (retval)
0159 return retval;
0160 return count;
0161 }
0162
0163 static struct pci_slot_attribute hotplug_slot_attr_attention = {
0164 .attr = {.name = "attention", .mode = S_IFREG | S_IRUGO | S_IWUSR},
0165 .show = attention_read_file,
0166 .store = attention_write_file
0167 };
0168
0169 static ssize_t latch_read_file(struct pci_slot *pci_slot, char *buf)
0170 {
0171 int retval;
0172 u8 value;
0173
0174 retval = get_latch_status(pci_slot->hotplug, &value);
0175 if (retval)
0176 return retval;
0177
0178 return sysfs_emit(buf, "%d\n", value);
0179 }
0180
0181 static struct pci_slot_attribute hotplug_slot_attr_latch = {
0182 .attr = {.name = "latch", .mode = S_IFREG | S_IRUGO},
0183 .show = latch_read_file,
0184 };
0185
0186 static ssize_t presence_read_file(struct pci_slot *pci_slot, char *buf)
0187 {
0188 int retval;
0189 u8 value;
0190
0191 retval = get_adapter_status(pci_slot->hotplug, &value);
0192 if (retval)
0193 return retval;
0194
0195 return sysfs_emit(buf, "%d\n", value);
0196 }
0197
0198 static struct pci_slot_attribute hotplug_slot_attr_presence = {
0199 .attr = {.name = "adapter", .mode = S_IFREG | S_IRUGO},
0200 .show = presence_read_file,
0201 };
0202
0203 static ssize_t test_write_file(struct pci_slot *pci_slot, const char *buf,
0204 size_t count)
0205 {
0206 struct hotplug_slot *slot = pci_slot->hotplug;
0207 unsigned long ltest;
0208 u32 test;
0209 int retval = 0;
0210
0211 ltest = simple_strtoul(buf, NULL, 10);
0212 test = (u32)(ltest & 0xffffffff);
0213 dbg("test = %d\n", test);
0214
0215 if (!try_module_get(slot->owner)) {
0216 retval = -ENODEV;
0217 goto exit;
0218 }
0219 if (slot->ops->hardware_test)
0220 retval = slot->ops->hardware_test(slot, test);
0221 module_put(slot->owner);
0222
0223 exit:
0224 if (retval)
0225 return retval;
0226 return count;
0227 }
0228
0229 static struct pci_slot_attribute hotplug_slot_attr_test = {
0230 .attr = {.name = "test", .mode = S_IFREG | S_IRUGO | S_IWUSR},
0231 .store = test_write_file
0232 };
0233
0234 static bool has_power_file(struct pci_slot *pci_slot)
0235 {
0236 struct hotplug_slot *slot = pci_slot->hotplug;
0237
0238 if ((!slot) || (!slot->ops))
0239 return false;
0240 if ((slot->ops->enable_slot) ||
0241 (slot->ops->disable_slot) ||
0242 (slot->ops->get_power_status))
0243 return true;
0244 return false;
0245 }
0246
0247 static bool has_attention_file(struct pci_slot *pci_slot)
0248 {
0249 struct hotplug_slot *slot = pci_slot->hotplug;
0250
0251 if ((!slot) || (!slot->ops))
0252 return false;
0253 if ((slot->ops->set_attention_status) ||
0254 (slot->ops->get_attention_status))
0255 return true;
0256 return false;
0257 }
0258
0259 static bool has_latch_file(struct pci_slot *pci_slot)
0260 {
0261 struct hotplug_slot *slot = pci_slot->hotplug;
0262
0263 if ((!slot) || (!slot->ops))
0264 return false;
0265 if (slot->ops->get_latch_status)
0266 return true;
0267 return false;
0268 }
0269
0270 static bool has_adapter_file(struct pci_slot *pci_slot)
0271 {
0272 struct hotplug_slot *slot = pci_slot->hotplug;
0273
0274 if ((!slot) || (!slot->ops))
0275 return false;
0276 if (slot->ops->get_adapter_status)
0277 return true;
0278 return false;
0279 }
0280
0281 static bool has_test_file(struct pci_slot *pci_slot)
0282 {
0283 struct hotplug_slot *slot = pci_slot->hotplug;
0284
0285 if ((!slot) || (!slot->ops))
0286 return false;
0287 if (slot->ops->hardware_test)
0288 return true;
0289 return false;
0290 }
0291
0292 static int fs_add_slot(struct pci_slot *pci_slot)
0293 {
0294 int retval = 0;
0295
0296
0297 pci_hp_create_module_link(pci_slot);
0298
0299 if (has_power_file(pci_slot)) {
0300 retval = sysfs_create_file(&pci_slot->kobj,
0301 &hotplug_slot_attr_power.attr);
0302 if (retval)
0303 goto exit_power;
0304 }
0305
0306 if (has_attention_file(pci_slot)) {
0307 retval = sysfs_create_file(&pci_slot->kobj,
0308 &hotplug_slot_attr_attention.attr);
0309 if (retval)
0310 goto exit_attention;
0311 }
0312
0313 if (has_latch_file(pci_slot)) {
0314 retval = sysfs_create_file(&pci_slot->kobj,
0315 &hotplug_slot_attr_latch.attr);
0316 if (retval)
0317 goto exit_latch;
0318 }
0319
0320 if (has_adapter_file(pci_slot)) {
0321 retval = sysfs_create_file(&pci_slot->kobj,
0322 &hotplug_slot_attr_presence.attr);
0323 if (retval)
0324 goto exit_adapter;
0325 }
0326
0327 if (has_test_file(pci_slot)) {
0328 retval = sysfs_create_file(&pci_slot->kobj,
0329 &hotplug_slot_attr_test.attr);
0330 if (retval)
0331 goto exit_test;
0332 }
0333
0334 goto exit;
0335
0336 exit_test:
0337 if (has_adapter_file(pci_slot))
0338 sysfs_remove_file(&pci_slot->kobj,
0339 &hotplug_slot_attr_presence.attr);
0340 exit_adapter:
0341 if (has_latch_file(pci_slot))
0342 sysfs_remove_file(&pci_slot->kobj, &hotplug_slot_attr_latch.attr);
0343 exit_latch:
0344 if (has_attention_file(pci_slot))
0345 sysfs_remove_file(&pci_slot->kobj,
0346 &hotplug_slot_attr_attention.attr);
0347 exit_attention:
0348 if (has_power_file(pci_slot))
0349 sysfs_remove_file(&pci_slot->kobj, &hotplug_slot_attr_power.attr);
0350 exit_power:
0351 pci_hp_remove_module_link(pci_slot);
0352 exit:
0353 return retval;
0354 }
0355
0356 static void fs_remove_slot(struct pci_slot *pci_slot)
0357 {
0358 if (has_power_file(pci_slot))
0359 sysfs_remove_file(&pci_slot->kobj, &hotplug_slot_attr_power.attr);
0360
0361 if (has_attention_file(pci_slot))
0362 sysfs_remove_file(&pci_slot->kobj,
0363 &hotplug_slot_attr_attention.attr);
0364
0365 if (has_latch_file(pci_slot))
0366 sysfs_remove_file(&pci_slot->kobj, &hotplug_slot_attr_latch.attr);
0367
0368 if (has_adapter_file(pci_slot))
0369 sysfs_remove_file(&pci_slot->kobj,
0370 &hotplug_slot_attr_presence.attr);
0371
0372 if (has_test_file(pci_slot))
0373 sysfs_remove_file(&pci_slot->kobj, &hotplug_slot_attr_test.attr);
0374
0375 pci_hp_remove_module_link(pci_slot);
0376 }
0377
0378 static struct hotplug_slot *get_slot_from_name(const char *name)
0379 {
0380 struct hotplug_slot *slot;
0381
0382 list_for_each_entry(slot, &pci_hotplug_slot_list, slot_list) {
0383 if (strcmp(hotplug_slot_name(slot), name) == 0)
0384 return slot;
0385 }
0386 return NULL;
0387 }
0388
0389
0390
0391
0392
0393
0394
0395
0396
0397
0398
0399
0400
0401
0402
0403
0404 int __pci_hp_register(struct hotplug_slot *slot, struct pci_bus *bus,
0405 int devnr, const char *name,
0406 struct module *owner, const char *mod_name)
0407 {
0408 int result;
0409
0410 result = __pci_hp_initialize(slot, bus, devnr, name, owner, mod_name);
0411 if (result)
0412 return result;
0413
0414 result = pci_hp_add(slot);
0415 if (result)
0416 pci_hp_destroy(slot);
0417
0418 return result;
0419 }
0420 EXPORT_SYMBOL_GPL(__pci_hp_register);
0421
0422
0423
0424
0425
0426
0427
0428
0429
0430
0431
0432
0433
0434
0435
0436
0437
0438 int __pci_hp_initialize(struct hotplug_slot *slot, struct pci_bus *bus,
0439 int devnr, const char *name, struct module *owner,
0440 const char *mod_name)
0441 {
0442 struct pci_slot *pci_slot;
0443
0444 if (slot == NULL)
0445 return -ENODEV;
0446 if (slot->ops == NULL)
0447 return -EINVAL;
0448
0449 slot->owner = owner;
0450 slot->mod_name = mod_name;
0451
0452
0453
0454
0455
0456
0457 pci_slot = pci_create_slot(bus, devnr, name, slot);
0458 if (IS_ERR(pci_slot))
0459 return PTR_ERR(pci_slot);
0460
0461 slot->pci_slot = pci_slot;
0462 pci_slot->hotplug = slot;
0463 return 0;
0464 }
0465 EXPORT_SYMBOL_GPL(__pci_hp_initialize);
0466
0467
0468
0469
0470
0471
0472
0473
0474
0475
0476
0477 int pci_hp_add(struct hotplug_slot *slot)
0478 {
0479 struct pci_slot *pci_slot = slot->pci_slot;
0480 int result;
0481
0482 result = fs_add_slot(pci_slot);
0483 if (result)
0484 return result;
0485
0486 kobject_uevent(&pci_slot->kobj, KOBJ_ADD);
0487 mutex_lock(&pci_hp_mutex);
0488 list_add(&slot->slot_list, &pci_hotplug_slot_list);
0489 mutex_unlock(&pci_hp_mutex);
0490 dbg("Added slot %s to the list\n", hotplug_slot_name(slot));
0491 return 0;
0492 }
0493 EXPORT_SYMBOL_GPL(pci_hp_add);
0494
0495
0496
0497
0498
0499
0500
0501
0502
0503
0504 void pci_hp_deregister(struct hotplug_slot *slot)
0505 {
0506 pci_hp_del(slot);
0507 pci_hp_destroy(slot);
0508 }
0509 EXPORT_SYMBOL_GPL(pci_hp_deregister);
0510
0511
0512
0513
0514
0515
0516
0517
0518
0519 void pci_hp_del(struct hotplug_slot *slot)
0520 {
0521 struct hotplug_slot *temp;
0522
0523 if (WARN_ON(!slot))
0524 return;
0525
0526 mutex_lock(&pci_hp_mutex);
0527 temp = get_slot_from_name(hotplug_slot_name(slot));
0528 if (WARN_ON(temp != slot)) {
0529 mutex_unlock(&pci_hp_mutex);
0530 return;
0531 }
0532
0533 list_del(&slot->slot_list);
0534 mutex_unlock(&pci_hp_mutex);
0535 dbg("Removed slot %s from the list\n", hotplug_slot_name(slot));
0536 fs_remove_slot(slot->pci_slot);
0537 }
0538 EXPORT_SYMBOL_GPL(pci_hp_del);
0539
0540
0541
0542
0543
0544
0545
0546
0547
0548
0549
0550
0551 void pci_hp_destroy(struct hotplug_slot *slot)
0552 {
0553 struct pci_slot *pci_slot = slot->pci_slot;
0554
0555 slot->pci_slot = NULL;
0556 pci_slot->hotplug = NULL;
0557 pci_destroy_slot(pci_slot);
0558 }
0559 EXPORT_SYMBOL_GPL(pci_hp_destroy);
0560
0561 static int __init pci_hotplug_init(void)
0562 {
0563 int result;
0564
0565 result = cpci_hotplug_init(debug);
0566 if (result) {
0567 err("cpci_hotplug_init with error %d\n", result);
0568 return result;
0569 }
0570
0571 return result;
0572 }
0573 device_initcall(pci_hotplug_init);
0574
0575
0576
0577
0578
0579 module_param(debug, bool, 0644);
0580 MODULE_PARM_DESC(debug, "Debugging mode enabled or not");