acpiphp_core.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. /*
  2. * ACPI PCI Hot Plug Controller Driver
  3. *
  4. * Copyright (C) 1995,2001 Compaq Computer Corporation
  5. * Copyright (C) 2001 Greg Kroah-Hartman (greg@kroah.com)
  6. * Copyright (C) 2001 IBM Corp.
  7. * Copyright (C) 2002 Hiroshi Aono (h-aono@ap.jp.nec.com)
  8. * Copyright (C) 2002,2003 Takayoshi Kochi (t-kochi@bq.jp.nec.com)
  9. * Copyright (C) 2002,2003 NEC Corporation
  10. * Copyright (C) 2003-2005 Matthew Wilcox (matthew.wilcox@hp.com)
  11. * Copyright (C) 2003-2005 Hewlett Packard
  12. *
  13. * All rights reserved.
  14. *
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation; either version 2 of the License, or (at
  18. * your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful, but
  21. * WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  23. * NON INFRINGEMENT. See the GNU General Public License for more
  24. * details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program; if not, write to the Free Software
  28. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  29. *
  30. * Send feedback to <gregkh@us.ibm.com>,
  31. * <t-kochi@bq.jp.nec.com>
  32. *
  33. */
  34. #include <linux/init.h>
  35. #include <linux/module.h>
  36. #include <linux/moduleparam.h>
  37. #include <linux/kernel.h>
  38. #include <linux/pci.h>
  39. #include <linux/slab.h>
  40. #include <linux/smp.h>
  41. #include <linux/smp_lock.h>
  42. #include "pci_hotplug.h"
  43. #include "acpiphp.h"
  44. static LIST_HEAD(slot_list);
  45. #define MY_NAME "acpiphp"
  46. static int debug;
  47. int acpiphp_debug;
  48. /* local variables */
  49. static int num_slots;
  50. static struct acpiphp_attention_info *attention_info;
  51. #define DRIVER_VERSION "0.5"
  52. #define DRIVER_AUTHOR "Greg Kroah-Hartman <gregkh@us.ibm.com>, Takayoshi Kochi <t-kochi@bq.jp.nec.com>, Matthew Wilcox <willy@hp.com>"
  53. #define DRIVER_DESC "ACPI Hot Plug PCI Controller Driver"
  54. MODULE_AUTHOR(DRIVER_AUTHOR);
  55. MODULE_DESCRIPTION(DRIVER_DESC);
  56. MODULE_LICENSE("GPL");
  57. MODULE_PARM_DESC(debug, "Debugging mode enabled or not");
  58. module_param(debug, bool, 0644);
  59. /* export the attention callback registration methods */
  60. EXPORT_SYMBOL_GPL(acpiphp_register_attention);
  61. EXPORT_SYMBOL_GPL(acpiphp_unregister_attention);
  62. static int enable_slot (struct hotplug_slot *slot);
  63. static int disable_slot (struct hotplug_slot *slot);
  64. static int set_attention_status (struct hotplug_slot *slot, u8 value);
  65. static int get_power_status (struct hotplug_slot *slot, u8 *value);
  66. static int get_attention_status (struct hotplug_slot *slot, u8 *value);
  67. static int get_address (struct hotplug_slot *slot, u32 *value);
  68. static int get_latch_status (struct hotplug_slot *slot, u8 *value);
  69. static int get_adapter_status (struct hotplug_slot *slot, u8 *value);
  70. static struct hotplug_slot_ops acpi_hotplug_slot_ops = {
  71. .owner = THIS_MODULE,
  72. .enable_slot = enable_slot,
  73. .disable_slot = disable_slot,
  74. .set_attention_status = set_attention_status,
  75. .get_power_status = get_power_status,
  76. .get_attention_status = get_attention_status,
  77. .get_latch_status = get_latch_status,
  78. .get_adapter_status = get_adapter_status,
  79. .get_address = get_address,
  80. };
  81. /**
  82. * acpiphp_register_attention - set attention LED callback
  83. * @info: must be completely filled with LED callbacks
  84. *
  85. * Description: this is used to register a hardware specific ACPI
  86. * driver that manipulates the attention LED. All the fields in
  87. * info must be set.
  88. **/
  89. int acpiphp_register_attention(struct acpiphp_attention_info *info)
  90. {
  91. int retval = -EINVAL;
  92. if (info && info->owner && info->set_attn &&
  93. info->get_attn && !attention_info) {
  94. retval = 0;
  95. attention_info = info;
  96. }
  97. return retval;
  98. }
  99. /**
  100. * acpiphp_unregister_attention - unset attention LED callback
  101. * @info: must match the pointer used to register
  102. *
  103. * Description: this is used to un-register a hardware specific acpi
  104. * driver that manipulates the attention LED. The pointer to the
  105. * info struct must be the same as the one used to set it.
  106. **/
  107. int acpiphp_unregister_attention(struct acpiphp_attention_info *info)
  108. {
  109. int retval = -EINVAL;
  110. if (info && attention_info == info) {
  111. attention_info = NULL;
  112. retval = 0;
  113. }
  114. return retval;
  115. }
  116. /**
  117. * enable_slot - power on and enable a slot
  118. * @hotplug_slot: slot to enable
  119. *
  120. * Actual tasks are done in acpiphp_enable_slot()
  121. *
  122. */
  123. static int enable_slot(struct hotplug_slot *hotplug_slot)
  124. {
  125. struct slot *slot = hotplug_slot->private;
  126. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  127. /* enable the specified slot */
  128. return acpiphp_enable_slot(slot->acpi_slot);
  129. }
  130. /**
  131. * disable_slot - disable and power off a slot
  132. * @hotplug_slot: slot to disable
  133. *
  134. * Actual tasks are done in acpiphp_disable_slot()
  135. *
  136. */
  137. static int disable_slot(struct hotplug_slot *hotplug_slot)
  138. {
  139. struct slot *slot = hotplug_slot->private;
  140. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  141. /* disable the specified slot */
  142. return acpiphp_disable_slot(slot->acpi_slot);
  143. }
  144. /**
  145. * set_attention_status - set attention LED
  146. * @hotplug_slot: slot to set attention LED on
  147. * @status: value to set attention LED to (0 or 1)
  148. *
  149. * attention status LED, so we use a callback that
  150. * was registered with us. This allows hardware specific
  151. * ACPI implementations to blink the light for us.
  152. **/
  153. static int set_attention_status(struct hotplug_slot *hotplug_slot, u8 status)
  154. {
  155. int retval = -ENODEV;
  156. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  157. if (attention_info && try_module_get(attention_info->owner)) {
  158. retval = attention_info->set_attn(hotplug_slot, status);
  159. module_put(attention_info->owner);
  160. } else
  161. attention_info = NULL;
  162. return retval;
  163. }
  164. /**
  165. * get_power_status - get power status of a slot
  166. * @hotplug_slot: slot to get status
  167. * @value: pointer to store status
  168. *
  169. * Some platforms may not implement _STA method properly.
  170. * In that case, the value returned may not be reliable.
  171. *
  172. */
  173. static int get_power_status(struct hotplug_slot *hotplug_slot, u8 *value)
  174. {
  175. struct slot *slot = hotplug_slot->private;
  176. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  177. *value = acpiphp_get_power_status(slot->acpi_slot);
  178. return 0;
  179. }
  180. /**
  181. * get_attention_status - get attention LED status
  182. * @hotplug_slot: slot to get status from
  183. * @value: returns with value of attention LED
  184. *
  185. * ACPI doesn't have known method to determine the state
  186. * of the attention status LED, so we use a callback that
  187. * was registered with us. This allows hardware specific
  188. * ACPI implementations to determine its state
  189. **/
  190. static int get_attention_status(struct hotplug_slot *hotplug_slot, u8 *value)
  191. {
  192. int retval = -EINVAL;
  193. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  194. if (attention_info && try_module_get(attention_info->owner)) {
  195. retval = attention_info->get_attn(hotplug_slot, value);
  196. module_put(attention_info->owner);
  197. } else
  198. attention_info = NULL;
  199. return retval;
  200. }
  201. /**
  202. * get_latch_status - get latch status of a slot
  203. * @hotplug_slot: slot to get status
  204. * @value: pointer to store status
  205. *
  206. * ACPI doesn't provide any formal means to access latch status.
  207. * Instead, we fake latch status from _STA
  208. *
  209. */
  210. static int get_latch_status(struct hotplug_slot *hotplug_slot, u8 *value)
  211. {
  212. struct slot *slot = hotplug_slot->private;
  213. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  214. *value = acpiphp_get_latch_status(slot->acpi_slot);
  215. return 0;
  216. }
  217. /**
  218. * get_adapter_status - get adapter status of a slot
  219. * @hotplug_slot: slot to get status
  220. * @value: pointer to store status
  221. *
  222. * ACPI doesn't provide any formal means to access adapter status.
  223. * Instead, we fake adapter status from _STA
  224. *
  225. */
  226. static int get_adapter_status(struct hotplug_slot *hotplug_slot, u8 *value)
  227. {
  228. struct slot *slot = hotplug_slot->private;
  229. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  230. *value = acpiphp_get_adapter_status(slot->acpi_slot);
  231. return 0;
  232. }
  233. /**
  234. * get_address - get pci address of a slot
  235. * @hotplug_slot: slot to get status
  236. * @value: pointer to struct pci_busdev (seg, bus, dev)
  237. */
  238. static int get_address(struct hotplug_slot *hotplug_slot, u32 *value)
  239. {
  240. struct slot *slot = hotplug_slot->private;
  241. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  242. *value = acpiphp_get_address(slot->acpi_slot);
  243. return 0;
  244. }
  245. static int __init init_acpi(void)
  246. {
  247. int retval;
  248. /* initialize internal data structure etc. */
  249. retval = acpiphp_glue_init();
  250. /* read initial number of slots */
  251. if (!retval) {
  252. num_slots = acpiphp_get_num_slots();
  253. if (num_slots == 0)
  254. retval = -ENODEV;
  255. }
  256. return retval;
  257. }
  258. /**
  259. * make_slot_name - make a slot name that appears in pcihpfs
  260. * @slot: slot to name
  261. *
  262. */
  263. static void make_slot_name(struct slot *slot)
  264. {
  265. snprintf(slot->hotplug_slot->name, SLOT_NAME_SIZE, "%u",
  266. slot->acpi_slot->sun);
  267. }
  268. /**
  269. * release_slot - free up the memory used by a slot
  270. * @hotplug_slot: slot to free
  271. */
  272. static void release_slot(struct hotplug_slot *hotplug_slot)
  273. {
  274. struct slot *slot = hotplug_slot->private;
  275. dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
  276. kfree(slot->hotplug_slot->info);
  277. kfree(slot->hotplug_slot->name);
  278. kfree(slot->hotplug_slot);
  279. kfree(slot);
  280. }
  281. /**
  282. * init_slots - initialize 'struct slot' structures for each slot
  283. *
  284. */
  285. static int __init init_slots(void)
  286. {
  287. struct slot *slot;
  288. int retval = -ENOMEM;
  289. int i;
  290. for (i = 0; i < num_slots; ++i) {
  291. slot = kmalloc(sizeof(struct slot), GFP_KERNEL);
  292. if (!slot)
  293. goto error;
  294. memset(slot, 0, sizeof(struct slot));
  295. slot->hotplug_slot = kmalloc(sizeof(struct hotplug_slot), GFP_KERNEL);
  296. if (!slot->hotplug_slot)
  297. goto error_slot;
  298. memset(slot->hotplug_slot, 0, sizeof(struct hotplug_slot));
  299. slot->hotplug_slot->info = kmalloc(sizeof(struct hotplug_slot_info), GFP_KERNEL);
  300. if (!slot->hotplug_slot->info)
  301. goto error_hpslot;
  302. memset(slot->hotplug_slot->info, 0, sizeof(struct hotplug_slot_info));
  303. slot->hotplug_slot->name = kmalloc(SLOT_NAME_SIZE, GFP_KERNEL);
  304. if (!slot->hotplug_slot->name)
  305. goto error_info;
  306. slot->number = i;
  307. slot->hotplug_slot->private = slot;
  308. slot->hotplug_slot->release = &release_slot;
  309. slot->hotplug_slot->ops = &acpi_hotplug_slot_ops;
  310. slot->acpi_slot = get_slot_from_id(i);
  311. slot->hotplug_slot->info->power_status = acpiphp_get_power_status(slot->acpi_slot);
  312. slot->hotplug_slot->info->attention_status = 0;
  313. slot->hotplug_slot->info->latch_status = acpiphp_get_latch_status(slot->acpi_slot);
  314. slot->hotplug_slot->info->adapter_status = acpiphp_get_adapter_status(slot->acpi_slot);
  315. slot->hotplug_slot->info->max_bus_speed = PCI_SPEED_UNKNOWN;
  316. slot->hotplug_slot->info->cur_bus_speed = PCI_SPEED_UNKNOWN;
  317. make_slot_name(slot);
  318. retval = pci_hp_register(slot->hotplug_slot);
  319. if (retval) {
  320. err("pci_hp_register failed with error %d\n", retval);
  321. goto error_name;
  322. }
  323. /* add slot to our internal list */
  324. list_add(&slot->slot_list, &slot_list);
  325. info("Slot [%s] registered\n", slot->hotplug_slot->name);
  326. }
  327. return 0;
  328. error_name:
  329. kfree(slot->hotplug_slot->name);
  330. error_info:
  331. kfree(slot->hotplug_slot->info);
  332. error_hpslot:
  333. kfree(slot->hotplug_slot);
  334. error_slot:
  335. kfree(slot);
  336. error:
  337. return retval;
  338. }
  339. static void __exit cleanup_slots (void)
  340. {
  341. struct list_head *tmp, *n;
  342. struct slot *slot;
  343. list_for_each_safe (tmp, n, &slot_list) {
  344. /* memory will be freed in release_slot callback */
  345. slot = list_entry(tmp, struct slot, slot_list);
  346. list_del(&slot->slot_list);
  347. pci_hp_deregister(slot->hotplug_slot);
  348. }
  349. }
  350. static int __init acpiphp_init(void)
  351. {
  352. int retval;
  353. info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
  354. acpiphp_debug = debug;
  355. /* read all the ACPI info from the system */
  356. retval = init_acpi();
  357. if (retval)
  358. return retval;
  359. return init_slots();
  360. }
  361. static void __exit acpiphp_exit(void)
  362. {
  363. cleanup_slots();
  364. /* deallocate internal data structures etc. */
  365. acpiphp_glue_exit();
  366. }
  367. module_init(acpiphp_init);
  368. module_exit(acpiphp_exit);