pci_slot.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /*
  2. * pci_slot.c - ACPI PCI Slot Driver
  3. *
  4. * The code here is heavily leveraged from the acpiphp module.
  5. * Thanks to Matthew Wilcox <matthew@wil.cx> for much guidance.
  6. * Thanks to Kenji Kaneshige <kaneshige.kenji@jp.fujitsu.com> for code
  7. * review and fixes.
  8. *
  9. * Copyright (C) 2007-2008 Hewlett-Packard Development Company, L.P.
  10. * Alex Chiang <achiang@hp.com>
  11. *
  12. * This program is free software; you can redistribute it and/or modify it
  13. * under the terms and conditions of the GNU General Public License,
  14. * version 2, as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful, but
  17. * WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License along
  22. * with this program; if not, write to the Free Software Foundation, Inc.,
  23. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  24. */
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <linux/init.h>
  28. #include <linux/types.h>
  29. #include <linux/pci.h>
  30. #include <linux/acpi.h>
  31. #include <acpi/acpi_bus.h>
  32. #include <acpi/acpi_drivers.h>
  33. static int debug;
  34. static int check_sta_before_sun;
  35. #define DRIVER_VERSION "0.1"
  36. #define DRIVER_AUTHOR "Alex Chiang <achiang@hp.com>"
  37. #define DRIVER_DESC "ACPI PCI Slot Detection Driver"
  38. MODULE_AUTHOR(DRIVER_AUTHOR);
  39. MODULE_DESCRIPTION(DRIVER_DESC);
  40. MODULE_LICENSE("GPL");
  41. MODULE_PARM_DESC(debug, "Debugging mode enabled or not");
  42. module_param(debug, bool, 0644);
  43. #define _COMPONENT ACPI_PCI_COMPONENT
  44. ACPI_MODULE_NAME("pci_slot");
  45. #define MY_NAME "pci_slot"
  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 dbg(format, arg...) \
  49. do { \
  50. if (debug) \
  51. printk(KERN_DEBUG "%s: " format, \
  52. MY_NAME , ## arg); \
  53. } while (0)
  54. #define SLOT_NAME_SIZE 20 /* Inspired by #define in acpiphp.h */
  55. struct acpi_pci_slot {
  56. acpi_handle root_handle; /* handle of the root bridge */
  57. struct pci_slot *pci_slot; /* corresponding pci_slot */
  58. struct list_head list; /* node in the list of slots */
  59. };
  60. static int acpi_pci_slot_add(acpi_handle handle);
  61. static void acpi_pci_slot_remove(acpi_handle handle);
  62. static LIST_HEAD(slot_list);
  63. static DEFINE_MUTEX(slot_list_lock);
  64. static struct acpi_pci_driver acpi_pci_slot_driver = {
  65. .add = acpi_pci_slot_add,
  66. .remove = acpi_pci_slot_remove,
  67. };
  68. static int
  69. check_slot(acpi_handle handle, unsigned long long *sun)
  70. {
  71. int device = -1;
  72. unsigned long long adr, sta;
  73. acpi_status status;
  74. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  75. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  76. dbg("Checking slot on path: %s\n", (char *)buffer.pointer);
  77. if (check_sta_before_sun) {
  78. /* If SxFy doesn't have _STA, we just assume it's there */
  79. status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
  80. if (ACPI_SUCCESS(status) && !(sta & ACPI_STA_DEVICE_PRESENT))
  81. goto out;
  82. }
  83. status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
  84. if (ACPI_FAILURE(status)) {
  85. dbg("_ADR returned %d on %s\n", status, (char *)buffer.pointer);
  86. goto out;
  87. }
  88. /* No _SUN == not a slot == bail */
  89. status = acpi_evaluate_integer(handle, "_SUN", NULL, sun);
  90. if (ACPI_FAILURE(status)) {
  91. dbg("_SUN returned %d on %s\n", status, (char *)buffer.pointer);
  92. goto out;
  93. }
  94. device = (adr >> 16) & 0xffff;
  95. out:
  96. kfree(buffer.pointer);
  97. return device;
  98. }
  99. struct callback_args {
  100. acpi_walk_callback user_function; /* only for walk_p2p_bridge */
  101. struct pci_bus *pci_bus;
  102. acpi_handle root_handle;
  103. };
  104. /*
  105. * register_slot
  106. *
  107. * Called once for each SxFy object in the namespace. Don't worry about
  108. * calling pci_create_slot multiple times for the same pci_bus:device,
  109. * since each subsequent call simply bumps the refcount on the pci_slot.
  110. *
  111. * The number of calls to pci_destroy_slot from unregister_slot is
  112. * symmetrical.
  113. */
  114. static acpi_status
  115. register_slot(acpi_handle handle, u32 lvl, void *context, void **rv)
  116. {
  117. int device;
  118. unsigned long long sun;
  119. char name[SLOT_NAME_SIZE];
  120. struct acpi_pci_slot *slot;
  121. struct pci_slot *pci_slot;
  122. struct callback_args *parent_context = context;
  123. struct pci_bus *pci_bus = parent_context->pci_bus;
  124. device = check_slot(handle, &sun);
  125. if (device < 0)
  126. return AE_OK;
  127. slot = kmalloc(sizeof(*slot), GFP_KERNEL);
  128. if (!slot) {
  129. err("%s: cannot allocate memory\n", __func__);
  130. return AE_OK;
  131. }
  132. snprintf(name, sizeof(name), "%u", (u32)sun);
  133. pci_slot = pci_create_slot(pci_bus, device, name, NULL);
  134. if (IS_ERR(pci_slot)) {
  135. err("pci_create_slot returned %ld\n", PTR_ERR(pci_slot));
  136. kfree(slot);
  137. return AE_OK;
  138. }
  139. slot->root_handle = parent_context->root_handle;
  140. slot->pci_slot = pci_slot;
  141. INIT_LIST_HEAD(&slot->list);
  142. mutex_lock(&slot_list_lock);
  143. list_add(&slot->list, &slot_list);
  144. mutex_unlock(&slot_list_lock);
  145. get_device(&pci_bus->dev);
  146. dbg("pci_slot: %p, pci_bus: %x, device: %d, name: %s\n",
  147. pci_slot, pci_bus->number, device, name);
  148. return AE_OK;
  149. }
  150. /*
  151. * walk_p2p_bridge - discover and walk p2p bridges
  152. * @handle: points to an acpi_pci_root
  153. * @context: p2p_bridge_context pointer
  154. *
  155. * Note that when we call ourselves recursively, we pass a different
  156. * value of pci_bus in the child_context.
  157. */
  158. static acpi_status
  159. walk_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
  160. {
  161. int device, function;
  162. unsigned long long adr;
  163. acpi_status status;
  164. acpi_handle dummy_handle;
  165. acpi_walk_callback user_function;
  166. struct pci_dev *dev;
  167. struct pci_bus *pci_bus;
  168. struct callback_args child_context;
  169. struct callback_args *parent_context = context;
  170. pci_bus = parent_context->pci_bus;
  171. user_function = parent_context->user_function;
  172. status = acpi_get_handle(handle, "_ADR", &dummy_handle);
  173. if (ACPI_FAILURE(status))
  174. return AE_OK;
  175. status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
  176. if (ACPI_FAILURE(status))
  177. return AE_OK;
  178. device = (adr >> 16) & 0xffff;
  179. function = adr & 0xffff;
  180. dev = pci_get_slot(pci_bus, PCI_DEVFN(device, function));
  181. if (!dev || !dev->subordinate)
  182. goto out;
  183. child_context.pci_bus = dev->subordinate;
  184. child_context.user_function = user_function;
  185. child_context.root_handle = parent_context->root_handle;
  186. dbg("p2p bridge walk, pci_bus = %x\n", dev->subordinate->number);
  187. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
  188. user_function, &child_context, NULL);
  189. if (ACPI_FAILURE(status))
  190. goto out;
  191. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
  192. walk_p2p_bridge, &child_context, NULL);
  193. out:
  194. pci_dev_put(dev);
  195. return AE_OK;
  196. }
  197. /*
  198. * walk_root_bridge - generic root bridge walker
  199. * @handle: points to an acpi_pci_root
  200. * @user_function: user callback for slot objects
  201. *
  202. * Call user_function for all objects underneath this root bridge.
  203. * Walk p2p bridges underneath us and call user_function on those too.
  204. */
  205. static int
  206. walk_root_bridge(acpi_handle handle, acpi_walk_callback user_function)
  207. {
  208. int seg, bus;
  209. unsigned long long tmp;
  210. acpi_status status;
  211. acpi_handle dummy_handle;
  212. struct pci_bus *pci_bus;
  213. struct callback_args context;
  214. /* If the bridge doesn't have _STA, we assume it is always there */
  215. status = acpi_get_handle(handle, "_STA", &dummy_handle);
  216. if (ACPI_SUCCESS(status)) {
  217. status = acpi_evaluate_integer(handle, "_STA", NULL, &tmp);
  218. if (ACPI_FAILURE(status)) {
  219. info("%s: _STA evaluation failure\n", __func__);
  220. return 0;
  221. }
  222. if ((tmp & ACPI_STA_DEVICE_FUNCTIONING) == 0)
  223. /* don't register this object */
  224. return 0;
  225. }
  226. status = acpi_evaluate_integer(handle, "_SEG", NULL, &tmp);
  227. seg = ACPI_SUCCESS(status) ? tmp : 0;
  228. status = acpi_evaluate_integer(handle, "_BBN", NULL, &tmp);
  229. bus = ACPI_SUCCESS(status) ? tmp : 0;
  230. pci_bus = pci_find_bus(seg, bus);
  231. if (!pci_bus)
  232. return 0;
  233. context.pci_bus = pci_bus;
  234. context.user_function = user_function;
  235. context.root_handle = handle;
  236. dbg("root bridge walk, pci_bus = %x\n", pci_bus->number);
  237. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
  238. user_function, &context, NULL);
  239. if (ACPI_FAILURE(status))
  240. return status;
  241. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
  242. walk_p2p_bridge, &context, NULL);
  243. if (ACPI_FAILURE(status))
  244. err("%s: walk_p2p_bridge failure - %d\n", __func__, status);
  245. return status;
  246. }
  247. /*
  248. * acpi_pci_slot_add
  249. * @handle: points to an acpi_pci_root
  250. */
  251. static int
  252. acpi_pci_slot_add(acpi_handle handle)
  253. {
  254. acpi_status status;
  255. status = walk_root_bridge(handle, register_slot);
  256. if (ACPI_FAILURE(status))
  257. err("%s: register_slot failure - %d\n", __func__, status);
  258. return status;
  259. }
  260. /*
  261. * acpi_pci_slot_remove
  262. * @handle: points to an acpi_pci_root
  263. */
  264. static void
  265. acpi_pci_slot_remove(acpi_handle handle)
  266. {
  267. struct acpi_pci_slot *slot, *tmp;
  268. struct pci_bus *pbus;
  269. mutex_lock(&slot_list_lock);
  270. list_for_each_entry_safe(slot, tmp, &slot_list, list) {
  271. if (slot->root_handle == handle) {
  272. list_del(&slot->list);
  273. pbus = slot->pci_slot->bus;
  274. pci_destroy_slot(slot->pci_slot);
  275. put_device(&pbus->dev);
  276. kfree(slot);
  277. }
  278. }
  279. mutex_unlock(&slot_list_lock);
  280. }
  281. static int do_sta_before_sun(const struct dmi_system_id *d)
  282. {
  283. info("%s detected: will evaluate _STA before calling _SUN\n", d->ident);
  284. check_sta_before_sun = 1;
  285. return 0;
  286. }
  287. static struct dmi_system_id acpi_pci_slot_dmi_table[] __initdata = {
  288. /*
  289. * Fujitsu Primequest machines will return 1023 to indicate an
  290. * error if the _SUN method is evaluated on SxFy objects that
  291. * are not present (as indicated by _STA), so for those machines,
  292. * we want to check _STA before evaluating _SUN.
  293. */
  294. {
  295. .callback = do_sta_before_sun,
  296. .ident = "Fujitsu PRIMEQUEST",
  297. .matches = {
  298. DMI_MATCH(DMI_BIOS_VENDOR, "FUJITSU LIMITED"),
  299. DMI_MATCH(DMI_BIOS_VERSION, "PRIMEQUEST"),
  300. },
  301. },
  302. {}
  303. };
  304. static int __init
  305. acpi_pci_slot_init(void)
  306. {
  307. dmi_check_system(acpi_pci_slot_dmi_table);
  308. acpi_pci_register_driver(&acpi_pci_slot_driver);
  309. return 0;
  310. }
  311. static void __exit
  312. acpi_pci_slot_exit(void)
  313. {
  314. acpi_pci_unregister_driver(&acpi_pci_slot_driver);
  315. }
  316. module_init(acpi_pci_slot_init);
  317. module_exit(acpi_pci_slot_exit);