pci_hotplug_core.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  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. 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. struct list_head *tmp;
  333. list_for_each(tmp, &pci_hotplug_slot_list) {
  334. slot = list_entry(tmp, struct hotplug_slot, slot_list);
  335. if (strcmp(hotplug_slot_name(slot), name) == 0)
  336. return slot;
  337. }
  338. return NULL;
  339. }
  340. /**
  341. * __pci_hp_register - register a hotplug_slot with the PCI hotplug subsystem
  342. * @bus: bus this slot is on
  343. * @slot: pointer to the &struct hotplug_slot to register
  344. * @devnr: device number
  345. * @name: name registered with kobject core
  346. * @owner: caller module owner
  347. * @mod_name: caller module name
  348. *
  349. * Registers a hotplug slot with the pci hotplug subsystem, which will allow
  350. * userspace interaction to the slot.
  351. *
  352. * Returns 0 if successful, anything else for an error.
  353. */
  354. int __pci_hp_register(struct hotplug_slot *slot, struct pci_bus *bus,
  355. int devnr, const char *name,
  356. struct module *owner, const char *mod_name)
  357. {
  358. int result;
  359. struct pci_slot *pci_slot;
  360. if (slot == NULL)
  361. return -ENODEV;
  362. if ((slot->info == NULL) || (slot->ops == NULL))
  363. return -EINVAL;
  364. if (slot->release == NULL) {
  365. dbg("Why are you trying to register a hotplug slot without a proper release function?\n");
  366. return -EINVAL;
  367. }
  368. slot->ops->owner = owner;
  369. slot->ops->mod_name = mod_name;
  370. mutex_lock(&pci_hp_mutex);
  371. /*
  372. * No problems if we call this interface from both ACPI_PCI_SLOT
  373. * driver and call it here again. If we've already created the
  374. * pci_slot, the interface will simply bump the refcount.
  375. */
  376. pci_slot = pci_create_slot(bus, devnr, name, slot);
  377. if (IS_ERR(pci_slot)) {
  378. result = PTR_ERR(pci_slot);
  379. goto out;
  380. }
  381. slot->pci_slot = pci_slot;
  382. pci_slot->hotplug = slot;
  383. list_add(&slot->slot_list, &pci_hotplug_slot_list);
  384. result = fs_add_slot(pci_slot);
  385. if (result)
  386. goto err_list_del;
  387. kobject_uevent(&pci_slot->kobj, KOBJ_ADD);
  388. dbg("Added slot %s to the list\n", name);
  389. goto out;
  390. err_list_del:
  391. list_del(&slot->slot_list);
  392. pci_slot->hotplug = NULL;
  393. pci_destroy_slot(pci_slot);
  394. out:
  395. mutex_unlock(&pci_hp_mutex);
  396. return result;
  397. }
  398. EXPORT_SYMBOL_GPL(__pci_hp_register);
  399. /**
  400. * pci_hp_deregister - deregister a hotplug_slot with the PCI hotplug subsystem
  401. * @slot: pointer to the &struct hotplug_slot to deregister
  402. *
  403. * The @slot must have been registered with the pci hotplug subsystem
  404. * previously with a call to pci_hp_register().
  405. *
  406. * Returns 0 if successful, anything else for an error.
  407. */
  408. int pci_hp_deregister(struct hotplug_slot *slot)
  409. {
  410. struct hotplug_slot *temp;
  411. struct pci_slot *pci_slot;
  412. if (!slot)
  413. return -ENODEV;
  414. mutex_lock(&pci_hp_mutex);
  415. temp = get_slot_from_name(hotplug_slot_name(slot));
  416. if (temp != slot) {
  417. mutex_unlock(&pci_hp_mutex);
  418. return -ENODEV;
  419. }
  420. list_del(&slot->slot_list);
  421. pci_slot = slot->pci_slot;
  422. fs_remove_slot(pci_slot);
  423. dbg("Removed slot %s from the list\n", hotplug_slot_name(slot));
  424. slot->release(slot);
  425. pci_slot->hotplug = NULL;
  426. pci_destroy_slot(pci_slot);
  427. mutex_unlock(&pci_hp_mutex);
  428. return 0;
  429. }
  430. EXPORT_SYMBOL_GPL(pci_hp_deregister);
  431. /**
  432. * pci_hp_change_slot_info - changes the slot's information structure in the core
  433. * @slot: pointer to the slot whose info has changed
  434. * @info: pointer to the info copy into the slot's info structure
  435. *
  436. * @slot must have been registered with the pci
  437. * hotplug subsystem previously with a call to pci_hp_register().
  438. *
  439. * Returns 0 if successful, anything else for an error.
  440. */
  441. int pci_hp_change_slot_info(struct hotplug_slot *slot,
  442. struct hotplug_slot_info *info)
  443. {
  444. if (!slot || !info)
  445. return -ENODEV;
  446. memcpy(slot->info, info, sizeof(struct hotplug_slot_info));
  447. return 0;
  448. }
  449. EXPORT_SYMBOL_GPL(pci_hp_change_slot_info);
  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. return result;
  457. }
  458. info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
  459. return result;
  460. }
  461. static void __exit pci_hotplug_exit(void)
  462. {
  463. cpci_hotplug_exit();
  464. }
  465. module_init(pci_hotplug_init);
  466. module_exit(pci_hotplug_exit);
  467. MODULE_AUTHOR(DRIVER_AUTHOR);
  468. MODULE_DESCRIPTION(DRIVER_DESC);
  469. MODULE_LICENSE("GPL");
  470. module_param(debug, bool, 0644);
  471. MODULE_PARM_DESC(debug, "Debugging mode enabled or not");