pci_hotplug_core.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. /*
  2. * PCI HotPlug Controller Core
  3. *
  4. * Copyright (C) 2001-2002 Greg Kroah-Hartman (greg@kroah.com)
  5. * Copyright (C) 2001-2002 IBM Corp.
  6. *
  7. * All rights reserved.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or (at
  12. * your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  17. * NON INFRINGEMENT. See the GNU General Public License for more
  18. * details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23. *
  24. * Send feedback to <kristen.c.accardi@intel.com>
  25. *
  26. */
  27. #include <linux/module.h> /* try_module_get & module_put */
  28. #include <linux/moduleparam.h>
  29. #include <linux/kernel.h>
  30. #include <linux/types.h>
  31. #include <linux/list.h>
  32. #include <linux/kobject.h>
  33. #include <linux/sysfs.h>
  34. #include <linux/pagemap.h>
  35. #include <linux/init.h>
  36. #include <linux/mount.h>
  37. #include <linux/namei.h>
  38. #include <linux/mutex.h>
  39. #include <linux/pci.h>
  40. #include <linux/pci_hotplug.h>
  41. #include <asm/uaccess.h>
  42. #include "../pci.h"
  43. #include "cpci_hotplug.h"
  44. #define MY_NAME "pci_hotplug"
  45. #define dbg(fmt, arg...) do { if (debug) printk(KERN_DEBUG "%s: %s: " fmt, MY_NAME, __func__, ## arg); } while (0)
  46. #define err(format, arg...) printk(KERN_ERR "%s: " format, MY_NAME, ## arg)
  47. #define info(format, arg...) printk(KERN_INFO "%s: " format, MY_NAME, ## arg)
  48. #define warn(format, arg...) printk(KERN_WARNING "%s: " format, MY_NAME, ## arg)
  49. /* local variables */
  50. static bool debug;
  51. #define DRIVER_VERSION "0.5"
  52. #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>, Scott Murray <scottm@somanetworks.com>"
  53. #define DRIVER_DESC "PCI Hot Plug PCI Core"
  54. static LIST_HEAD(pci_hotplug_slot_list);
  55. static DEFINE_MUTEX(pci_hp_mutex);
  56. /* Weee, fun with macros... */
  57. #define GET_STATUS(name, type) \
  58. static int get_##name(struct hotplug_slot *slot, type *value) \
  59. { \
  60. struct hotplug_slot_ops *ops = slot->ops; \
  61. int retval = 0; \
  62. if (!try_module_get(ops->owner)) \
  63. return -ENODEV; \
  64. if (ops->get_##name) \
  65. retval = ops->get_##name(slot, value); \
  66. else \
  67. *value = slot->info->name; \
  68. module_put(ops->owner); \
  69. return retval; \
  70. }
  71. GET_STATUS(power_status, u8)
  72. GET_STATUS(attention_status, u8)
  73. GET_STATUS(latch_status, u8)
  74. GET_STATUS(adapter_status, u8)
  75. static ssize_t power_read_file(struct pci_slot *pci_slot, char *buf)
  76. {
  77. int retval;
  78. u8 value;
  79. retval = get_power_status(pci_slot->hotplug, &value);
  80. if (retval)
  81. return retval;
  82. return sprintf(buf, "%d\n", value);
  83. }
  84. static ssize_t power_write_file(struct pci_slot *pci_slot, const char *buf,
  85. size_t count)
  86. {
  87. struct hotplug_slot *slot = pci_slot->hotplug;
  88. unsigned long lpower;
  89. u8 power;
  90. int retval = 0;
  91. lpower = simple_strtoul(buf, NULL, 10);
  92. power = (u8)(lpower & 0xff);
  93. dbg("power = %d\n", power);
  94. if (!try_module_get(slot->ops->owner)) {
  95. retval = -ENODEV;
  96. goto exit;
  97. }
  98. switch (power) {
  99. case 0:
  100. if (slot->ops->disable_slot)
  101. retval = slot->ops->disable_slot(slot);
  102. break;
  103. case 1:
  104. if (slot->ops->enable_slot)
  105. retval = slot->ops->enable_slot(slot);
  106. break;
  107. default:
  108. err("Illegal value specified for power\n");
  109. retval = -EINVAL;
  110. }
  111. module_put(slot->ops->owner);
  112. exit:
  113. if (retval)
  114. return retval;
  115. return count;
  116. }
  117. static struct pci_slot_attribute hotplug_slot_attr_power = {
  118. .attr = {.name = "power", .mode = S_IFREG | S_IRUGO | S_IWUSR},
  119. .show = power_read_file,
  120. .store = power_write_file
  121. };
  122. static ssize_t attention_read_file(struct pci_slot *pci_slot, char *buf)
  123. {
  124. int retval;
  125. u8 value;
  126. retval = get_attention_status(pci_slot->hotplug, &value);
  127. if (retval)
  128. return retval;
  129. return sprintf(buf, "%d\n", value);
  130. }
  131. static ssize_t attention_write_file(struct pci_slot *pci_slot, const char *buf,
  132. size_t count)
  133. {
  134. struct hotplug_slot_ops *ops = pci_slot->hotplug->ops;
  135. unsigned long lattention;
  136. u8 attention;
  137. int retval = 0;
  138. lattention = simple_strtoul(buf, NULL, 10);
  139. attention = (u8)(lattention & 0xff);
  140. dbg(" - attention = %d\n", attention);
  141. if (!try_module_get(ops->owner)) {
  142. retval = -ENODEV;
  143. goto exit;
  144. }
  145. if (ops->set_attention_status)
  146. retval = ops->set_attention_status(pci_slot->hotplug, attention);
  147. module_put(ops->owner);
  148. exit:
  149. if (retval)
  150. return retval;
  151. return count;
  152. }
  153. static struct pci_slot_attribute hotplug_slot_attr_attention = {
  154. .attr = {.name = "attention", .mode = S_IFREG | S_IRUGO | S_IWUSR},
  155. .show = attention_read_file,
  156. .store = attention_write_file
  157. };
  158. static ssize_t latch_read_file(struct pci_slot *pci_slot, char *buf)
  159. {
  160. int retval;
  161. u8 value;
  162. retval = get_latch_status(pci_slot->hotplug, &value);
  163. if (retval)
  164. return retval;
  165. return sprintf(buf, "%d\n", value);
  166. }
  167. static struct pci_slot_attribute hotplug_slot_attr_latch = {
  168. .attr = {.name = "latch", .mode = S_IFREG | S_IRUGO},
  169. .show = latch_read_file,
  170. };
  171. static ssize_t presence_read_file(struct pci_slot *pci_slot, char *buf)
  172. {
  173. int retval;
  174. u8 value;
  175. retval = get_adapter_status(pci_slot->hotplug, &value);
  176. if (retval)
  177. return retval;
  178. return sprintf(buf, "%d\n", value);
  179. }
  180. static struct pci_slot_attribute hotplug_slot_attr_presence = {
  181. .attr = {.name = "adapter", .mode = S_IFREG | S_IRUGO},
  182. .show = presence_read_file,
  183. };
  184. static ssize_t test_write_file(struct pci_slot *pci_slot, const char *buf,
  185. size_t count)
  186. {
  187. struct hotplug_slot *slot = pci_slot->hotplug;
  188. unsigned long ltest;
  189. u32 test;
  190. int retval = 0;
  191. ltest = simple_strtoul(buf, NULL, 10);
  192. test = (u32)(ltest & 0xffffffff);
  193. dbg("test = %d\n", test);
  194. if (!try_module_get(slot->ops->owner)) {
  195. retval = -ENODEV;
  196. goto exit;
  197. }
  198. if (slot->ops->hardware_test)
  199. retval = slot->ops->hardware_test(slot, test);
  200. module_put(slot->ops->owner);
  201. exit:
  202. if (retval)
  203. return retval;
  204. return count;
  205. }
  206. static struct pci_slot_attribute hotplug_slot_attr_test = {
  207. .attr = {.name = "test", .mode = S_IFREG | S_IRUGO | S_IWUSR},
  208. .store = test_write_file
  209. };
  210. static bool has_power_file(struct pci_slot *pci_slot)
  211. {
  212. struct hotplug_slot *slot = pci_slot->hotplug;
  213. if ((!slot) || (!slot->ops))
  214. return false;
  215. if ((slot->ops->enable_slot) ||
  216. (slot->ops->disable_slot) ||
  217. (slot->ops->get_power_status))
  218. return true;
  219. return false;
  220. }
  221. static bool has_attention_file(struct pci_slot *pci_slot)
  222. {
  223. struct hotplug_slot *slot = pci_slot->hotplug;
  224. if ((!slot) || (!slot->ops))
  225. return false;
  226. if ((slot->ops->set_attention_status) ||
  227. (slot->ops->get_attention_status))
  228. return true;
  229. return false;
  230. }
  231. static bool has_latch_file(struct pci_slot *pci_slot)
  232. {
  233. struct hotplug_slot *slot = pci_slot->hotplug;
  234. if ((!slot) || (!slot->ops))
  235. return false;
  236. if (slot->ops->get_latch_status)
  237. return true;
  238. return false;
  239. }
  240. static bool has_adapter_file(struct pci_slot *pci_slot)
  241. {
  242. struct hotplug_slot *slot = pci_slot->hotplug;
  243. if ((!slot) || (!slot->ops))
  244. return false;
  245. if (slot->ops->get_adapter_status)
  246. return true;
  247. return false;
  248. }
  249. static bool has_test_file(struct pci_slot *pci_slot)
  250. {
  251. struct hotplug_slot *slot = pci_slot->hotplug;
  252. if ((!slot) || (!slot->ops))
  253. return false;
  254. if (slot->ops->hardware_test)
  255. return true;
  256. return false;
  257. }
  258. static int fs_add_slot(struct pci_slot *pci_slot)
  259. {
  260. int retval = 0;
  261. /* Create symbolic link to the hotplug driver module */
  262. pci_hp_create_module_link(pci_slot);
  263. if (has_power_file(pci_slot)) {
  264. retval = sysfs_create_file(&pci_slot->kobj,
  265. &hotplug_slot_attr_power.attr);
  266. if (retval)
  267. goto exit_power;
  268. }
  269. if (has_attention_file(pci_slot)) {
  270. retval = sysfs_create_file(&pci_slot->kobj,
  271. &hotplug_slot_attr_attention.attr);
  272. if (retval)
  273. goto exit_attention;
  274. }
  275. if (has_latch_file(pci_slot)) {
  276. retval = sysfs_create_file(&pci_slot->kobj,
  277. &hotplug_slot_attr_latch.attr);
  278. if (retval)
  279. goto exit_latch;
  280. }
  281. if (has_adapter_file(pci_slot)) {
  282. retval = sysfs_create_file(&pci_slot->kobj,
  283. &hotplug_slot_attr_presence.attr);
  284. if (retval)
  285. goto exit_adapter;
  286. }
  287. if (has_test_file(pci_slot)) {
  288. retval = sysfs_create_file(&pci_slot->kobj,
  289. &hotplug_slot_attr_test.attr);
  290. if (retval)
  291. goto exit_test;
  292. }
  293. goto exit;
  294. exit_test:
  295. if (has_adapter_file(pci_slot))
  296. sysfs_remove_file(&pci_slot->kobj,
  297. &hotplug_slot_attr_presence.attr);
  298. exit_adapter:
  299. if (has_latch_file(pci_slot))
  300. sysfs_remove_file(&pci_slot->kobj, &hotplug_slot_attr_latch.attr);
  301. exit_latch:
  302. if (has_attention_file(pci_slot))
  303. sysfs_remove_file(&pci_slot->kobj,
  304. &hotplug_slot_attr_attention.attr);
  305. exit_attention:
  306. if (has_power_file(pci_slot))
  307. sysfs_remove_file(&pci_slot->kobj, &hotplug_slot_attr_power.attr);
  308. exit_power:
  309. pci_hp_remove_module_link(pci_slot);
  310. exit:
  311. return retval;
  312. }
  313. static void fs_remove_slot(struct pci_slot *pci_slot)
  314. {
  315. if (has_power_file(pci_slot))
  316. sysfs_remove_file(&pci_slot->kobj, &hotplug_slot_attr_power.attr);
  317. if (has_attention_file(pci_slot))
  318. sysfs_remove_file(&pci_slot->kobj,
  319. &hotplug_slot_attr_attention.attr);
  320. if (has_latch_file(pci_slot))
  321. sysfs_remove_file(&pci_slot->kobj, &hotplug_slot_attr_latch.attr);
  322. if (has_adapter_file(pci_slot))
  323. sysfs_remove_file(&pci_slot->kobj,
  324. &hotplug_slot_attr_presence.attr);
  325. if (has_test_file(pci_slot))
  326. sysfs_remove_file(&pci_slot->kobj, &hotplug_slot_attr_test.attr);
  327. pci_hp_remove_module_link(pci_slot);
  328. }
  329. static struct hotplug_slot *get_slot_from_name(const char *name)
  330. {
  331. struct hotplug_slot *slot;
  332. list_for_each_entry(slot, &pci_hotplug_slot_list, slot_list) {
  333. if (strcmp(hotplug_slot_name(slot), name) == 0)
  334. return slot;
  335. }
  336. return NULL;
  337. }
  338. /**
  339. * __pci_hp_register - register a hotplug_slot with the PCI hotplug subsystem
  340. * @bus: bus this slot is on
  341. * @slot: pointer to the &struct hotplug_slot to register
  342. * @devnr: device number
  343. * @name: name registered with kobject core
  344. * @owner: caller module owner
  345. * @mod_name: caller module name
  346. *
  347. * Registers a hotplug slot with the pci hotplug subsystem, which will allow
  348. * userspace interaction to the slot.
  349. *
  350. * Returns 0 if successful, anything else for an error.
  351. */
  352. int __pci_hp_register(struct hotplug_slot *slot, struct pci_bus *bus,
  353. int devnr, const char *name,
  354. struct module *owner, const char *mod_name)
  355. {
  356. int result;
  357. struct pci_slot *pci_slot;
  358. if (slot == NULL)
  359. return -ENODEV;
  360. if ((slot->info == NULL) || (slot->ops == NULL))
  361. return -EINVAL;
  362. if (slot->release == NULL) {
  363. dbg("Why are you trying to register a hotplug slot without a proper release function?\n");
  364. return -EINVAL;
  365. }
  366. slot->ops->owner = owner;
  367. slot->ops->mod_name = mod_name;
  368. mutex_lock(&pci_hp_mutex);
  369. /*
  370. * No problems if we call this interface from both ACPI_PCI_SLOT
  371. * driver and call it here again. If we've already created the
  372. * pci_slot, the interface will simply bump the refcount.
  373. */
  374. pci_slot = pci_create_slot(bus, devnr, name, slot);
  375. if (IS_ERR(pci_slot)) {
  376. result = PTR_ERR(pci_slot);
  377. goto out;
  378. }
  379. slot->pci_slot = pci_slot;
  380. pci_slot->hotplug = slot;
  381. list_add(&slot->slot_list, &pci_hotplug_slot_list);
  382. result = fs_add_slot(pci_slot);
  383. kobject_uevent(&pci_slot->kobj, KOBJ_ADD);
  384. dbg("Added slot %s to the list\n", name);
  385. out:
  386. mutex_unlock(&pci_hp_mutex);
  387. return result;
  388. }
  389. EXPORT_SYMBOL_GPL(__pci_hp_register);
  390. /**
  391. * pci_hp_deregister - deregister a hotplug_slot with the PCI hotplug subsystem
  392. * @slot: pointer to the &struct hotplug_slot to deregister
  393. *
  394. * The @slot must have been registered with the pci hotplug subsystem
  395. * previously with a call to pci_hp_register().
  396. *
  397. * Returns 0 if successful, anything else for an error.
  398. */
  399. int pci_hp_deregister(struct hotplug_slot *slot)
  400. {
  401. struct hotplug_slot *temp;
  402. struct pci_slot *pci_slot;
  403. if (!slot)
  404. return -ENODEV;
  405. mutex_lock(&pci_hp_mutex);
  406. temp = get_slot_from_name(hotplug_slot_name(slot));
  407. if (temp != slot) {
  408. mutex_unlock(&pci_hp_mutex);
  409. return -ENODEV;
  410. }
  411. list_del(&slot->slot_list);
  412. pci_slot = slot->pci_slot;
  413. fs_remove_slot(pci_slot);
  414. dbg("Removed slot %s from the list\n", hotplug_slot_name(slot));
  415. slot->release(slot);
  416. pci_slot->hotplug = NULL;
  417. pci_destroy_slot(pci_slot);
  418. mutex_unlock(&pci_hp_mutex);
  419. return 0;
  420. }
  421. EXPORT_SYMBOL_GPL(pci_hp_deregister);
  422. /**
  423. * pci_hp_change_slot_info - changes the slot's information structure in the core
  424. * @slot: pointer to the slot whose info has changed
  425. * @info: pointer to the info copy into the slot's info structure
  426. *
  427. * @slot must have been registered with the pci
  428. * hotplug subsystem previously with a call to pci_hp_register().
  429. *
  430. * Returns 0 if successful, anything else for an error.
  431. */
  432. int pci_hp_change_slot_info(struct hotplug_slot *slot,
  433. struct hotplug_slot_info *info)
  434. {
  435. if (!slot || !info)
  436. return -ENODEV;
  437. memcpy(slot->info, info, sizeof(struct hotplug_slot_info));
  438. return 0;
  439. }
  440. EXPORT_SYMBOL_GPL(pci_hp_change_slot_info);
  441. static int __init pci_hotplug_init(void)
  442. {
  443. int result;
  444. result = cpci_hotplug_init(debug);
  445. if (result) {
  446. err("cpci_hotplug_init with error %d\n", result);
  447. return result;
  448. }
  449. info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
  450. return result;
  451. }
  452. device_initcall(pci_hotplug_init);
  453. /*
  454. * not really modular, but the easiest way to keep compat with existing
  455. * bootargs behaviour is to continue using module_param here.
  456. */
  457. module_param(debug, bool, 0644);
  458. MODULE_PARM_DESC(debug, "Debugging mode enabled or not");