pci_hotplug_core.c 15 KB

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