pci_hotplug_core.c 13 KB

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