pci_hotplug_core.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * PCI HotPlug Controller Core
  4. *
  5. * Copyright (C) 2001-2002 Greg Kroah-Hartman (greg@kroah.com)
  6. * Copyright (C) 2001-2002 IBM Corp.
  7. *
  8. * All rights reserved.
  9. *
  10. * Send feedback to <kristen.c.accardi@intel.com>
  11. *
  12. * Authors:
  13. * Greg Kroah-Hartman <greg@kroah.com>
  14. * Scott Murray <scottm@somanetworks.com>
  15. */
  16. #include <linux/module.h> /* try_module_get & module_put */
  17. #include <linux/moduleparam.h>
  18. #include <linux/kernel.h>
  19. #include <linux/types.h>
  20. #include <linux/list.h>
  21. #include <linux/kobject.h>
  22. #include <linux/sysfs.h>
  23. #include <linux/pagemap.h>
  24. #include <linux/init.h>
  25. #include <linux/mount.h>
  26. #include <linux/namei.h>
  27. #include <linux/mutex.h>
  28. #include <linux/pci.h>
  29. #include <linux/pci_hotplug.h>
  30. #include <linux/uaccess.h>
  31. #include "../pci.h"
  32. #include "cpci_hotplug.h"
  33. #define MY_NAME "pci_hotplug"
  34. #define dbg(fmt, arg...) do { if (debug) printk(KERN_DEBUG "%s: %s: " fmt, MY_NAME, __func__, ## arg); } while (0)
  35. #define err(format, arg...) printk(KERN_ERR "%s: " format, MY_NAME, ## arg)
  36. #define info(format, arg...) printk(KERN_INFO "%s: " format, MY_NAME, ## arg)
  37. #define warn(format, arg...) printk(KERN_WARNING "%s: " format, MY_NAME, ## arg)
  38. /* local variables */
  39. static bool debug;
  40. static LIST_HEAD(pci_hotplug_slot_list);
  41. static DEFINE_MUTEX(pci_hp_mutex);
  42. /* Weee, fun with macros... */
  43. #define GET_STATUS(name, type) \
  44. static int get_##name(struct hotplug_slot *slot, type *value) \
  45. { \
  46. const struct hotplug_slot_ops *ops = slot->ops; \
  47. int retval = 0; \
  48. if (!try_module_get(slot->owner)) \
  49. return -ENODEV; \
  50. if (ops->get_##name) \
  51. retval = ops->get_##name(slot, value); \
  52. module_put(slot->owner); \
  53. return retval; \
  54. }
  55. GET_STATUS(power_status, u8)
  56. GET_STATUS(attention_status, u8)
  57. GET_STATUS(latch_status, u8)
  58. GET_STATUS(adapter_status, u8)
  59. static ssize_t power_read_file(struct pci_slot *pci_slot, char *buf)
  60. {
  61. int retval;
  62. u8 value;
  63. retval = get_power_status(pci_slot->hotplug, &value);
  64. if (retval)
  65. return retval;
  66. return sprintf(buf, "%d\n", value);
  67. }
  68. static ssize_t power_write_file(struct pci_slot *pci_slot, const char *buf,
  69. size_t count)
  70. {
  71. struct hotplug_slot *slot = pci_slot->hotplug;
  72. unsigned long lpower;
  73. u8 power;
  74. int retval = 0;
  75. lpower = simple_strtoul(buf, NULL, 10);
  76. power = (u8)(lpower & 0xff);
  77. dbg("power = %d\n", power);
  78. if (!try_module_get(slot->owner)) {
  79. retval = -ENODEV;
  80. goto exit;
  81. }
  82. switch (power) {
  83. case 0:
  84. if (slot->ops->disable_slot)
  85. retval = slot->ops->disable_slot(slot);
  86. break;
  87. case 1:
  88. if (slot->ops->enable_slot)
  89. retval = slot->ops->enable_slot(slot);
  90. break;
  91. default:
  92. err("Illegal value specified for power\n");
  93. retval = -EINVAL;
  94. }
  95. module_put(slot->owner);
  96. exit:
  97. if (retval)
  98. return retval;
  99. return count;
  100. }
  101. static struct pci_slot_attribute hotplug_slot_attr_power = {
  102. .attr = {.name = "power", .mode = S_IFREG | S_IRUGO | S_IWUSR},
  103. .show = power_read_file,
  104. .store = power_write_file
  105. };
  106. static ssize_t attention_read_file(struct pci_slot *pci_slot, char *buf)
  107. {
  108. int retval;
  109. u8 value;
  110. retval = get_attention_status(pci_slot->hotplug, &value);
  111. if (retval)
  112. return retval;
  113. return sprintf(buf, "%d\n", value);
  114. }
  115. static ssize_t attention_write_file(struct pci_slot *pci_slot, const char *buf,
  116. size_t count)
  117. {
  118. struct hotplug_slot *slot = pci_slot->hotplug;
  119. const struct hotplug_slot_ops *ops = slot->ops;
  120. unsigned long lattention;
  121. u8 attention;
  122. int retval = 0;
  123. lattention = simple_strtoul(buf, NULL, 10);
  124. attention = (u8)(lattention & 0xff);
  125. dbg(" - attention = %d\n", attention);
  126. if (!try_module_get(slot->owner)) {
  127. retval = -ENODEV;
  128. goto exit;
  129. }
  130. if (ops->set_attention_status)
  131. retval = ops->set_attention_status(slot, attention);
  132. module_put(slot->owner);
  133. exit:
  134. if (retval)
  135. return retval;
  136. return count;
  137. }
  138. static struct pci_slot_attribute hotplug_slot_attr_attention = {
  139. .attr = {.name = "attention", .mode = S_IFREG | S_IRUGO | S_IWUSR},
  140. .show = attention_read_file,
  141. .store = attention_write_file
  142. };
  143. static ssize_t latch_read_file(struct pci_slot *pci_slot, char *buf)
  144. {
  145. int retval;
  146. u8 value;
  147. retval = get_latch_status(pci_slot->hotplug, &value);
  148. if (retval)
  149. return retval;
  150. return sprintf(buf, "%d\n", value);
  151. }
  152. static struct pci_slot_attribute hotplug_slot_attr_latch = {
  153. .attr = {.name = "latch", .mode = S_IFREG | S_IRUGO},
  154. .show = latch_read_file,
  155. };
  156. static ssize_t presence_read_file(struct pci_slot *pci_slot, char *buf)
  157. {
  158. int retval;
  159. u8 value;
  160. retval = get_adapter_status(pci_slot->hotplug, &value);
  161. if (retval)
  162. return retval;
  163. return sprintf(buf, "%d\n", value);
  164. }
  165. static struct pci_slot_attribute hotplug_slot_attr_presence = {
  166. .attr = {.name = "adapter", .mode = S_IFREG | S_IRUGO},
  167. .show = presence_read_file,
  168. };
  169. static ssize_t test_write_file(struct pci_slot *pci_slot, const char *buf,
  170. size_t count)
  171. {
  172. struct hotplug_slot *slot = pci_slot->hotplug;
  173. unsigned long ltest;
  174. u32 test;
  175. int retval = 0;
  176. ltest = simple_strtoul(buf, NULL, 10);
  177. test = (u32)(ltest & 0xffffffff);
  178. dbg("test = %d\n", test);
  179. if (!try_module_get(slot->owner)) {
  180. retval = -ENODEV;
  181. goto exit;
  182. }
  183. if (slot->ops->hardware_test)
  184. retval = slot->ops->hardware_test(slot, test);
  185. module_put(slot->owner);
  186. exit:
  187. if (retval)
  188. return retval;
  189. return count;
  190. }
  191. static struct pci_slot_attribute hotplug_slot_attr_test = {
  192. .attr = {.name = "test", .mode = S_IFREG | S_IRUGO | S_IWUSR},
  193. .store = test_write_file
  194. };
  195. static bool has_power_file(struct pci_slot *pci_slot)
  196. {
  197. struct hotplug_slot *slot = pci_slot->hotplug;
  198. if ((!slot) || (!slot->ops))
  199. return false;
  200. if ((slot->ops->enable_slot) ||
  201. (slot->ops->disable_slot) ||
  202. (slot->ops->get_power_status))
  203. return true;
  204. return false;
  205. }
  206. static bool has_attention_file(struct pci_slot *pci_slot)
  207. {
  208. struct hotplug_slot *slot = pci_slot->hotplug;
  209. if ((!slot) || (!slot->ops))
  210. return false;
  211. if ((slot->ops->set_attention_status) ||
  212. (slot->ops->get_attention_status))
  213. return true;
  214. return false;
  215. }
  216. static bool has_latch_file(struct pci_slot *pci_slot)
  217. {
  218. struct hotplug_slot *slot = pci_slot->hotplug;
  219. if ((!slot) || (!slot->ops))
  220. return false;
  221. if (slot->ops->get_latch_status)
  222. return true;
  223. return false;
  224. }
  225. static bool has_adapter_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->get_adapter_status)
  231. return true;
  232. return false;
  233. }
  234. static bool has_test_file(struct pci_slot *pci_slot)
  235. {
  236. struct hotplug_slot *slot = pci_slot->hotplug;
  237. if ((!slot) || (!slot->ops))
  238. return false;
  239. if (slot->ops->hardware_test)
  240. return true;
  241. return false;
  242. }
  243. static int fs_add_slot(struct pci_slot *pci_slot)
  244. {
  245. int retval = 0;
  246. /* Create symbolic link to the hotplug driver module */
  247. pci_hp_create_module_link(pci_slot);
  248. if (has_power_file(pci_slot)) {
  249. retval = sysfs_create_file(&pci_slot->kobj,
  250. &hotplug_slot_attr_power.attr);
  251. if (retval)
  252. goto exit_power;
  253. }
  254. if (has_attention_file(pci_slot)) {
  255. retval = sysfs_create_file(&pci_slot->kobj,
  256. &hotplug_slot_attr_attention.attr);
  257. if (retval)
  258. goto exit_attention;
  259. }
  260. if (has_latch_file(pci_slot)) {
  261. retval = sysfs_create_file(&pci_slot->kobj,
  262. &hotplug_slot_attr_latch.attr);
  263. if (retval)
  264. goto exit_latch;
  265. }
  266. if (has_adapter_file(pci_slot)) {
  267. retval = sysfs_create_file(&pci_slot->kobj,
  268. &hotplug_slot_attr_presence.attr);
  269. if (retval)
  270. goto exit_adapter;
  271. }
  272. if (has_test_file(pci_slot)) {
  273. retval = sysfs_create_file(&pci_slot->kobj,
  274. &hotplug_slot_attr_test.attr);
  275. if (retval)
  276. goto exit_test;
  277. }
  278. goto exit;
  279. exit_test:
  280. if (has_adapter_file(pci_slot))
  281. sysfs_remove_file(&pci_slot->kobj,
  282. &hotplug_slot_attr_presence.attr);
  283. exit_adapter:
  284. if (has_latch_file(pci_slot))
  285. sysfs_remove_file(&pci_slot->kobj, &hotplug_slot_attr_latch.attr);
  286. exit_latch:
  287. if (has_attention_file(pci_slot))
  288. sysfs_remove_file(&pci_slot->kobj,
  289. &hotplug_slot_attr_attention.attr);
  290. exit_attention:
  291. if (has_power_file(pci_slot))
  292. sysfs_remove_file(&pci_slot->kobj, &hotplug_slot_attr_power.attr);
  293. exit_power:
  294. pci_hp_remove_module_link(pci_slot);
  295. exit:
  296. return retval;
  297. }
  298. static void fs_remove_slot(struct pci_slot *pci_slot)
  299. {
  300. if (has_power_file(pci_slot))
  301. sysfs_remove_file(&pci_slot->kobj, &hotplug_slot_attr_power.attr);
  302. if (has_attention_file(pci_slot))
  303. sysfs_remove_file(&pci_slot->kobj,
  304. &hotplug_slot_attr_attention.attr);
  305. if (has_latch_file(pci_slot))
  306. sysfs_remove_file(&pci_slot->kobj, &hotplug_slot_attr_latch.attr);
  307. if (has_adapter_file(pci_slot))
  308. sysfs_remove_file(&pci_slot->kobj,
  309. &hotplug_slot_attr_presence.attr);
  310. if (has_test_file(pci_slot))
  311. sysfs_remove_file(&pci_slot->kobj, &hotplug_slot_attr_test.attr);
  312. pci_hp_remove_module_link(pci_slot);
  313. }
  314. static struct hotplug_slot *get_slot_from_name(const char *name)
  315. {
  316. struct hotplug_slot *slot;
  317. list_for_each_entry(slot, &pci_hotplug_slot_list, slot_list) {
  318. if (strcmp(hotplug_slot_name(slot), name) == 0)
  319. return slot;
  320. }
  321. return NULL;
  322. }
  323. /**
  324. * __pci_hp_register - register a hotplug_slot with the PCI hotplug subsystem
  325. * @bus: bus this slot is on
  326. * @slot: pointer to the &struct hotplug_slot to register
  327. * @devnr: device number
  328. * @name: name registered with kobject core
  329. * @owner: caller module owner
  330. * @mod_name: caller module name
  331. *
  332. * Prepares a hotplug slot for in-kernel use and immediately publishes it to
  333. * user space in one go. Drivers may alternatively carry out the two steps
  334. * separately by invoking pci_hp_initialize() and pci_hp_add().
  335. *
  336. * Returns 0 if successful, anything else for an error.
  337. */
  338. int __pci_hp_register(struct hotplug_slot *slot, struct pci_bus *bus,
  339. int devnr, const char *name,
  340. struct module *owner, const char *mod_name)
  341. {
  342. int result;
  343. result = __pci_hp_initialize(slot, bus, devnr, name, owner, mod_name);
  344. if (result)
  345. return result;
  346. result = pci_hp_add(slot);
  347. if (result)
  348. pci_hp_destroy(slot);
  349. return result;
  350. }
  351. EXPORT_SYMBOL_GPL(__pci_hp_register);
  352. /**
  353. * __pci_hp_initialize - prepare hotplug slot for in-kernel use
  354. * @slot: pointer to the &struct hotplug_slot to initialize
  355. * @bus: bus this slot is on
  356. * @devnr: slot number
  357. * @name: name registered with kobject core
  358. * @owner: caller module owner
  359. * @mod_name: caller module name
  360. *
  361. * Allocate and fill in a PCI slot for use by a hotplug driver. Once this has
  362. * been called, the driver may invoke hotplug_slot_name() to get the slot's
  363. * unique name. The driver must be prepared to handle a ->reset_slot callback
  364. * from this point on.
  365. *
  366. * Returns 0 on success or a negative int on error.
  367. */
  368. int __pci_hp_initialize(struct hotplug_slot *slot, struct pci_bus *bus,
  369. int devnr, const char *name, struct module *owner,
  370. const char *mod_name)
  371. {
  372. struct pci_slot *pci_slot;
  373. if (slot == NULL)
  374. return -ENODEV;
  375. if (slot->ops == NULL)
  376. return -EINVAL;
  377. slot->owner = owner;
  378. slot->mod_name = mod_name;
  379. /*
  380. * No problems if we call this interface from both ACPI_PCI_SLOT
  381. * driver and call it here again. If we've already created the
  382. * pci_slot, the interface will simply bump the refcount.
  383. */
  384. pci_slot = pci_create_slot(bus, devnr, name, slot);
  385. if (IS_ERR(pci_slot))
  386. return PTR_ERR(pci_slot);
  387. slot->pci_slot = pci_slot;
  388. pci_slot->hotplug = slot;
  389. return 0;
  390. }
  391. EXPORT_SYMBOL_GPL(__pci_hp_initialize);
  392. /**
  393. * pci_hp_add - publish hotplug slot to user space
  394. * @slot: pointer to the &struct hotplug_slot to publish
  395. *
  396. * Make a hotplug slot's sysfs interface available and inform user space of its
  397. * addition by sending a uevent. The hotplug driver must be prepared to handle
  398. * all &struct hotplug_slot_ops callbacks from this point on.
  399. *
  400. * Returns 0 on success or a negative int on error.
  401. */
  402. int pci_hp_add(struct hotplug_slot *slot)
  403. {
  404. struct pci_slot *pci_slot = slot->pci_slot;
  405. int result;
  406. result = fs_add_slot(pci_slot);
  407. if (result)
  408. return result;
  409. kobject_uevent(&pci_slot->kobj, KOBJ_ADD);
  410. mutex_lock(&pci_hp_mutex);
  411. list_add(&slot->slot_list, &pci_hotplug_slot_list);
  412. mutex_unlock(&pci_hp_mutex);
  413. dbg("Added slot %s to the list\n", hotplug_slot_name(slot));
  414. return 0;
  415. }
  416. EXPORT_SYMBOL_GPL(pci_hp_add);
  417. /**
  418. * pci_hp_deregister - deregister a hotplug_slot with the PCI hotplug subsystem
  419. * @slot: pointer to the &struct hotplug_slot to deregister
  420. *
  421. * The @slot must have been registered with the pci hotplug subsystem
  422. * previously with a call to pci_hp_register().
  423. *
  424. * Returns 0 if successful, anything else for an error.
  425. */
  426. void pci_hp_deregister(struct hotplug_slot *slot)
  427. {
  428. pci_hp_del(slot);
  429. pci_hp_destroy(slot);
  430. }
  431. EXPORT_SYMBOL_GPL(pci_hp_deregister);
  432. /**
  433. * pci_hp_del - unpublish hotplug slot from user space
  434. * @slot: pointer to the &struct hotplug_slot to unpublish
  435. *
  436. * Remove a hotplug slot's sysfs interface.
  437. *
  438. * Returns 0 on success or a negative int on error.
  439. */
  440. void pci_hp_del(struct hotplug_slot *slot)
  441. {
  442. struct hotplug_slot *temp;
  443. if (WARN_ON(!slot))
  444. return;
  445. mutex_lock(&pci_hp_mutex);
  446. temp = get_slot_from_name(hotplug_slot_name(slot));
  447. if (WARN_ON(temp != slot)) {
  448. mutex_unlock(&pci_hp_mutex);
  449. return;
  450. }
  451. list_del(&slot->slot_list);
  452. mutex_unlock(&pci_hp_mutex);
  453. dbg("Removed slot %s from the list\n", hotplug_slot_name(slot));
  454. fs_remove_slot(slot->pci_slot);
  455. }
  456. EXPORT_SYMBOL_GPL(pci_hp_del);
  457. /**
  458. * pci_hp_destroy - remove hotplug slot from in-kernel use
  459. * @slot: pointer to the &struct hotplug_slot to destroy
  460. *
  461. * Destroy a PCI slot used by a hotplug driver. Once this has been called,
  462. * the driver may no longer invoke hotplug_slot_name() to get the slot's
  463. * unique name. The driver no longer needs to handle a ->reset_slot callback
  464. * from this point on.
  465. *
  466. * Returns 0 on success or a negative int on error.
  467. */
  468. void pci_hp_destroy(struct hotplug_slot *slot)
  469. {
  470. struct pci_slot *pci_slot = slot->pci_slot;
  471. slot->pci_slot = NULL;
  472. pci_slot->hotplug = NULL;
  473. pci_destroy_slot(pci_slot);
  474. }
  475. EXPORT_SYMBOL_GPL(pci_hp_destroy);
  476. static int __init pci_hotplug_init(void)
  477. {
  478. int result;
  479. result = cpci_hotplug_init(debug);
  480. if (result) {
  481. err("cpci_hotplug_init with error %d\n", result);
  482. return result;
  483. }
  484. return result;
  485. }
  486. device_initcall(pci_hotplug_init);
  487. /*
  488. * not really modular, but the easiest way to keep compat with existing
  489. * bootargs behaviour is to continue using module_param here.
  490. */
  491. module_param(debug, bool, 0644);
  492. MODULE_PARM_DESC(debug, "Debugging mode enabled or not");