pci_hotplug_core.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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>
  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/slab.h>
  36. #include <linux/init.h>
  37. #include <linux/mount.h>
  38. #include <linux/namei.h>
  39. #include <linux/mutex.h>
  40. #include <linux/pci.h>
  41. #include <linux/pci_hotplug.h>
  42. #include <asm/uaccess.h>
  43. #include "../pci.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 int 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. //////////////////////////////////////////////////////////////////
  55. static LIST_HEAD(pci_hotplug_slot_list);
  56. static DEFINE_MUTEX(pci_hp_mutex);
  57. #ifdef CONFIG_HOTPLUG_PCI_CPCI
  58. extern int cpci_hotplug_init(int debug);
  59. extern void cpci_hotplug_exit(void);
  60. #else
  61. static inline int cpci_hotplug_init(int debug) { return 0; }
  62. static inline void cpci_hotplug_exit(void) { }
  63. #endif
  64. /* Weee, fun with macros... */
  65. #define GET_STATUS(name,type) \
  66. static int get_##name (struct hotplug_slot *slot, type *value) \
  67. { \
  68. struct hotplug_slot_ops *ops = slot->ops; \
  69. int retval = 0; \
  70. if (!try_module_get(ops->owner)) \
  71. return -ENODEV; \
  72. if (ops->get_##name) \
  73. retval = ops->get_##name(slot, value); \
  74. else \
  75. *value = slot->info->name; \
  76. module_put(ops->owner); \
  77. return retval; \
  78. }
  79. GET_STATUS(power_status, u8)
  80. GET_STATUS(attention_status, u8)
  81. GET_STATUS(latch_status, u8)
  82. GET_STATUS(adapter_status, u8)
  83. static ssize_t power_read_file(struct pci_slot *slot, char *buf)
  84. {
  85. int retval;
  86. u8 value;
  87. retval = get_power_status(slot->hotplug, &value);
  88. if (retval)
  89. goto exit;
  90. retval = sprintf (buf, "%d\n", value);
  91. exit:
  92. return retval;
  93. }
  94. static ssize_t power_write_file(struct pci_slot *pci_slot, const char *buf,
  95. size_t count)
  96. {
  97. struct hotplug_slot *slot = pci_slot->hotplug;
  98. unsigned long lpower;
  99. u8 power;
  100. int retval = 0;
  101. lpower = simple_strtoul (buf, NULL, 10);
  102. power = (u8)(lpower & 0xff);
  103. dbg ("power = %d\n", power);
  104. if (!try_module_get(slot->ops->owner)) {
  105. retval = -ENODEV;
  106. goto exit;
  107. }
  108. switch (power) {
  109. case 0:
  110. if (slot->ops->disable_slot)
  111. retval = slot->ops->disable_slot(slot);
  112. break;
  113. case 1:
  114. if (slot->ops->enable_slot)
  115. retval = slot->ops->enable_slot(slot);
  116. break;
  117. default:
  118. err ("Illegal value specified for power\n");
  119. retval = -EINVAL;
  120. }
  121. module_put(slot->ops->owner);
  122. exit:
  123. if (retval)
  124. return retval;
  125. return count;
  126. }
  127. static struct pci_slot_attribute hotplug_slot_attr_power = {
  128. .attr = {.name = "power", .mode = S_IFREG | S_IRUGO | S_IWUSR},
  129. .show = power_read_file,
  130. .store = power_write_file
  131. };
  132. static ssize_t attention_read_file(struct pci_slot *slot, char *buf)
  133. {
  134. int retval;
  135. u8 value;
  136. retval = get_attention_status(slot->hotplug, &value);
  137. if (retval)
  138. goto exit;
  139. retval = sprintf(buf, "%d\n", value);
  140. exit:
  141. return retval;
  142. }
  143. static ssize_t attention_write_file(struct pci_slot *slot, const char *buf,
  144. size_t count)
  145. {
  146. struct hotplug_slot_ops *ops = slot->hotplug->ops;
  147. unsigned long lattention;
  148. u8 attention;
  149. int retval = 0;
  150. lattention = simple_strtoul (buf, NULL, 10);
  151. attention = (u8)(lattention & 0xff);
  152. dbg (" - attention = %d\n", attention);
  153. if (!try_module_get(ops->owner)) {
  154. retval = -ENODEV;
  155. goto exit;
  156. }
  157. if (ops->set_attention_status)
  158. retval = ops->set_attention_status(slot->hotplug, attention);
  159. module_put(ops->owner);
  160. exit:
  161. if (retval)
  162. return retval;
  163. return count;
  164. }
  165. static struct pci_slot_attribute hotplug_slot_attr_attention = {
  166. .attr = {.name = "attention", .mode = S_IFREG | S_IRUGO | S_IWUSR},
  167. .show = attention_read_file,
  168. .store = attention_write_file
  169. };
  170. static ssize_t latch_read_file(struct pci_slot *slot, char *buf)
  171. {
  172. int retval;
  173. u8 value;
  174. retval = get_latch_status(slot->hotplug, &value);
  175. if (retval)
  176. goto exit;
  177. retval = sprintf (buf, "%d\n", value);
  178. exit:
  179. return retval;
  180. }
  181. static struct pci_slot_attribute hotplug_slot_attr_latch = {
  182. .attr = {.name = "latch", .mode = S_IFREG | S_IRUGO},
  183. .show = latch_read_file,
  184. };
  185. static ssize_t presence_read_file(struct pci_slot *slot, char *buf)
  186. {
  187. int retval;
  188. u8 value;
  189. retval = get_adapter_status(slot->hotplug, &value);
  190. if (retval)
  191. goto exit;
  192. retval = sprintf (buf, "%d\n", value);
  193. exit:
  194. return retval;
  195. }
  196. static struct pci_slot_attribute hotplug_slot_attr_presence = {
  197. .attr = {.name = "adapter", .mode = S_IFREG | S_IRUGO},
  198. .show = presence_read_file,
  199. };
  200. static ssize_t test_write_file(struct pci_slot *pci_slot, const char *buf,
  201. size_t count)
  202. {
  203. struct hotplug_slot *slot = pci_slot->hotplug;
  204. unsigned long ltest;
  205. u32 test;
  206. int retval = 0;
  207. ltest = simple_strtoul (buf, NULL, 10);
  208. test = (u32)(ltest & 0xffffffff);
  209. dbg ("test = %d\n", test);
  210. if (!try_module_get(slot->ops->owner)) {
  211. retval = -ENODEV;
  212. goto exit;
  213. }
  214. if (slot->ops->hardware_test)
  215. retval = slot->ops->hardware_test(slot, test);
  216. module_put(slot->ops->owner);
  217. exit:
  218. if (retval)
  219. return retval;
  220. return count;
  221. }
  222. static struct pci_slot_attribute hotplug_slot_attr_test = {
  223. .attr = {.name = "test", .mode = S_IFREG | S_IRUGO | S_IWUSR},
  224. .store = test_write_file
  225. };
  226. static bool has_power_file(struct pci_slot *pci_slot)
  227. {
  228. struct hotplug_slot *slot = pci_slot->hotplug;
  229. if ((!slot) || (!slot->ops))
  230. return false;
  231. if ((slot->ops->enable_slot) ||
  232. (slot->ops->disable_slot) ||
  233. (slot->ops->get_power_status))
  234. return true;
  235. return false;
  236. }
  237. static bool has_attention_file(struct pci_slot *pci_slot)
  238. {
  239. struct hotplug_slot *slot = pci_slot->hotplug;
  240. if ((!slot) || (!slot->ops))
  241. return false;
  242. if ((slot->ops->set_attention_status) ||
  243. (slot->ops->get_attention_status))
  244. return true;
  245. return false;
  246. }
  247. static bool has_latch_file(struct pci_slot *pci_slot)
  248. {
  249. struct hotplug_slot *slot = pci_slot->hotplug;
  250. if ((!slot) || (!slot->ops))
  251. return false;
  252. if (slot->ops->get_latch_status)
  253. return true;
  254. return false;
  255. }
  256. static bool has_adapter_file(struct pci_slot *pci_slot)
  257. {
  258. struct hotplug_slot *slot = pci_slot->hotplug;
  259. if ((!slot) || (!slot->ops))
  260. return false;
  261. if (slot->ops->get_adapter_status)
  262. return true;
  263. return false;
  264. }
  265. static bool has_test_file(struct pci_slot *pci_slot)
  266. {
  267. struct hotplug_slot *slot = pci_slot->hotplug;
  268. if ((!slot) || (!slot->ops))
  269. return false;
  270. if (slot->ops->hardware_test)
  271. return true;
  272. return false;
  273. }
  274. static int fs_add_slot(struct pci_slot *slot)
  275. {
  276. int retval = 0;
  277. /* Create symbolic link to the hotplug driver module */
  278. pci_hp_create_module_link(slot);
  279. if (has_power_file(slot)) {
  280. retval = sysfs_create_file(&slot->kobj,
  281. &hotplug_slot_attr_power.attr);
  282. if (retval)
  283. goto exit_power;
  284. }
  285. if (has_attention_file(slot)) {
  286. retval = sysfs_create_file(&slot->kobj,
  287. &hotplug_slot_attr_attention.attr);
  288. if (retval)
  289. goto exit_attention;
  290. }
  291. if (has_latch_file(slot)) {
  292. retval = sysfs_create_file(&slot->kobj,
  293. &hotplug_slot_attr_latch.attr);
  294. if (retval)
  295. goto exit_latch;
  296. }
  297. if (has_adapter_file(slot)) {
  298. retval = sysfs_create_file(&slot->kobj,
  299. &hotplug_slot_attr_presence.attr);
  300. if (retval)
  301. goto exit_adapter;
  302. }
  303. if (has_test_file(slot)) {
  304. retval = sysfs_create_file(&slot->kobj,
  305. &hotplug_slot_attr_test.attr);
  306. if (retval)
  307. goto exit_test;
  308. }
  309. goto exit;
  310. exit_test:
  311. if (has_adapter_file(slot))
  312. sysfs_remove_file(&slot->kobj,
  313. &hotplug_slot_attr_presence.attr);
  314. exit_adapter:
  315. if (has_latch_file(slot))
  316. sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_latch.attr);
  317. exit_latch:
  318. if (has_attention_file(slot))
  319. sysfs_remove_file(&slot->kobj,
  320. &hotplug_slot_attr_attention.attr);
  321. exit_attention:
  322. if (has_power_file(slot))
  323. sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_power.attr);
  324. exit_power:
  325. pci_hp_remove_module_link(slot);
  326. exit:
  327. return retval;
  328. }
  329. static void fs_remove_slot(struct pci_slot *slot)
  330. {
  331. if (has_power_file(slot))
  332. sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_power.attr);
  333. if (has_attention_file(slot))
  334. sysfs_remove_file(&slot->kobj,
  335. &hotplug_slot_attr_attention.attr);
  336. if (has_latch_file(slot))
  337. sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_latch.attr);
  338. if (has_adapter_file(slot))
  339. sysfs_remove_file(&slot->kobj,
  340. &hotplug_slot_attr_presence.attr);
  341. if (has_test_file(slot))
  342. sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_test.attr);
  343. pci_hp_remove_module_link(slot);
  344. }
  345. static struct hotplug_slot *get_slot_from_name (const char *name)
  346. {
  347. struct hotplug_slot *slot;
  348. struct list_head *tmp;
  349. list_for_each (tmp, &pci_hotplug_slot_list) {
  350. slot = list_entry (tmp, struct hotplug_slot, slot_list);
  351. if (strcmp(hotplug_slot_name(slot), name) == 0)
  352. return slot;
  353. }
  354. return NULL;
  355. }
  356. /**
  357. * __pci_hp_register - register a hotplug_slot with the PCI hotplug subsystem
  358. * @bus: bus this slot is on
  359. * @slot: pointer to the &struct hotplug_slot to register
  360. * @devnr: device number
  361. * @name: name registered with kobject core
  362. * @owner: caller module owner
  363. * @mod_name: caller module name
  364. *
  365. * Registers a hotplug slot with the pci hotplug subsystem, which will allow
  366. * userspace interaction to the slot.
  367. *
  368. * Returns 0 if successful, anything else for an error.
  369. */
  370. int __pci_hp_register(struct hotplug_slot *slot, struct pci_bus *bus,
  371. int devnr, const char *name,
  372. struct module *owner, const char *mod_name)
  373. {
  374. int result;
  375. struct pci_slot *pci_slot;
  376. if (slot == NULL)
  377. return -ENODEV;
  378. if ((slot->info == NULL) || (slot->ops == NULL))
  379. return -EINVAL;
  380. if (slot->release == NULL) {
  381. dbg("Why are you trying to register a hotplug slot "
  382. "without a proper release function?\n");
  383. return -EINVAL;
  384. }
  385. slot->ops->owner = owner;
  386. slot->ops->mod_name = mod_name;
  387. mutex_lock(&pci_hp_mutex);
  388. /*
  389. * No problems if we call this interface from both ACPI_PCI_SLOT
  390. * driver and call it here again. If we've already created the
  391. * pci_slot, the interface will simply bump the refcount.
  392. */
  393. pci_slot = pci_create_slot(bus, devnr, name, slot);
  394. if (IS_ERR(pci_slot)) {
  395. result = PTR_ERR(pci_slot);
  396. goto out;
  397. }
  398. slot->pci_slot = pci_slot;
  399. pci_slot->hotplug = slot;
  400. list_add(&slot->slot_list, &pci_hotplug_slot_list);
  401. result = fs_add_slot(pci_slot);
  402. kobject_uevent(&pci_slot->kobj, KOBJ_ADD);
  403. dbg("Added slot %s to the list\n", name);
  404. out:
  405. mutex_unlock(&pci_hp_mutex);
  406. return result;
  407. }
  408. /**
  409. * pci_hp_deregister - deregister a hotplug_slot with the PCI hotplug subsystem
  410. * @hotplug: pointer to the &struct hotplug_slot to deregister
  411. *
  412. * The @slot must have been registered with the pci hotplug subsystem
  413. * previously with a call to pci_hp_register().
  414. *
  415. * Returns 0 if successful, anything else for an error.
  416. */
  417. int pci_hp_deregister(struct hotplug_slot *hotplug)
  418. {
  419. struct hotplug_slot *temp;
  420. struct pci_slot *slot;
  421. if (!hotplug)
  422. return -ENODEV;
  423. mutex_lock(&pci_hp_mutex);
  424. temp = get_slot_from_name(hotplug_slot_name(hotplug));
  425. if (temp != hotplug) {
  426. mutex_unlock(&pci_hp_mutex);
  427. return -ENODEV;
  428. }
  429. list_del(&hotplug->slot_list);
  430. slot = hotplug->pci_slot;
  431. fs_remove_slot(slot);
  432. dbg("Removed slot %s from the list\n", hotplug_slot_name(hotplug));
  433. hotplug->release(hotplug);
  434. slot->hotplug = NULL;
  435. pci_destroy_slot(slot);
  436. mutex_unlock(&pci_hp_mutex);
  437. return 0;
  438. }
  439. /**
  440. * pci_hp_change_slot_info - changes the slot's information structure in the core
  441. * @hotplug: pointer to the slot whose info has changed
  442. * @info: pointer to the info copy into the slot's info structure
  443. *
  444. * @slot must have been registered with the pci
  445. * hotplug subsystem previously with a call to pci_hp_register().
  446. *
  447. * Returns 0 if successful, anything else for an error.
  448. */
  449. int __must_check pci_hp_change_slot_info(struct hotplug_slot *hotplug,
  450. struct hotplug_slot_info *info)
  451. {
  452. struct pci_slot *slot;
  453. if (!hotplug || !info)
  454. return -ENODEV;
  455. slot = hotplug->pci_slot;
  456. memcpy(hotplug->info, info, sizeof(struct hotplug_slot_info));
  457. return 0;
  458. }
  459. static int __init pci_hotplug_init (void)
  460. {
  461. int result;
  462. result = cpci_hotplug_init(debug);
  463. if (result) {
  464. err ("cpci_hotplug_init with error %d\n", result);
  465. goto err_cpci;
  466. }
  467. info (DRIVER_DESC " version: " DRIVER_VERSION "\n");
  468. err_cpci:
  469. return result;
  470. }
  471. static void __exit pci_hotplug_exit (void)
  472. {
  473. cpci_hotplug_exit();
  474. }
  475. module_init(pci_hotplug_init);
  476. module_exit(pci_hotplug_exit);
  477. MODULE_AUTHOR(DRIVER_AUTHOR);
  478. MODULE_DESCRIPTION(DRIVER_DESC);
  479. MODULE_LICENSE("GPL");
  480. module_param(debug, bool, 0644);
  481. MODULE_PARM_DESC(debug, "Debugging mode enabled or not");
  482. EXPORT_SYMBOL_GPL(__pci_hp_register);
  483. EXPORT_SYMBOL_GPL(pci_hp_deregister);
  484. EXPORT_SYMBOL_GPL(pci_hp_change_slot_info);