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