rpaphp_pci.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * PCI Hot Plug Controller Driver for RPA-compliant PPC64 platform.
  3. * Copyright (C) 2003 Linda Xie <lxie@us.ibm.com>
  4. *
  5. * All rights reserved.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  15. * NON INFRINGEMENT. See the GNU General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. *
  22. * Send feedback to <lxie@us.ibm.com>
  23. *
  24. */
  25. #include <linux/pci.h>
  26. #include <linux/string.h>
  27. #include <asm/pci-bridge.h>
  28. #include <asm/rtas.h>
  29. #include <asm/machdep.h>
  30. #include "../pci.h" /* for pci_add_new_bus */
  31. #include "rpaphp.h"
  32. static int rpaphp_get_sensor_state(struct slot *slot, int *state)
  33. {
  34. int rc;
  35. int setlevel;
  36. rc = rtas_get_sensor(DR_ENTITY_SENSE, slot->index, state);
  37. if (rc < 0) {
  38. if (rc == -EFAULT || rc == -EEXIST) {
  39. dbg("%s: slot must be power up to get sensor-state\n",
  40. __FUNCTION__);
  41. /* some slots have to be powered up
  42. * before get-sensor will succeed.
  43. */
  44. rc = rtas_set_power_level(slot->power_domain, POWER_ON,
  45. &setlevel);
  46. if (rc < 0) {
  47. dbg("%s: power on slot[%s] failed rc=%d.\n",
  48. __FUNCTION__, slot->name, rc);
  49. } else {
  50. rc = rtas_get_sensor(DR_ENTITY_SENSE,
  51. slot->index, state);
  52. }
  53. } else if (rc == -ENODEV)
  54. info("%s: slot is unusable\n", __FUNCTION__);
  55. else
  56. err("%s failed to get sensor state\n", __FUNCTION__);
  57. }
  58. return rc;
  59. }
  60. /**
  61. * get_pci_adapter_status - get the status of a slot
  62. *
  63. * 0-- slot is empty
  64. * 1-- adapter is configured
  65. * 2-- adapter is not configured
  66. * 3-- not valid
  67. */
  68. int rpaphp_get_pci_adapter_status(struct slot *slot, int is_init, u8 * value)
  69. {
  70. struct pci_bus *bus;
  71. int state, rc;
  72. *value = NOT_VALID;
  73. rc = rpaphp_get_sensor_state(slot, &state);
  74. if (rc)
  75. goto exit;
  76. if (state == EMPTY)
  77. *value = EMPTY;
  78. else if (state == PRESENT) {
  79. if (!is_init) {
  80. /* at run-time slot->state can be changed by */
  81. /* config/unconfig adapter */
  82. *value = slot->state;
  83. } else {
  84. bus = pcibios_find_pci_bus(slot->dn);
  85. if (bus && !list_empty(&bus->devices))
  86. *value = CONFIGURED;
  87. else
  88. *value = NOT_CONFIGURED;
  89. }
  90. }
  91. exit:
  92. return rc;
  93. }
  94. static void print_slot_pci_funcs(struct pci_bus *bus)
  95. {
  96. struct device_node *dn;
  97. struct pci_dev *dev;
  98. dn = pci_bus_to_OF_node(bus);
  99. if (!dn)
  100. return;
  101. dbg("%s: pci_devs of slot[%s]\n", __FUNCTION__, dn->full_name);
  102. list_for_each_entry (dev, &bus->devices, bus_list)
  103. dbg("\t%s\n", pci_name(dev));
  104. return;
  105. }
  106. int rpaphp_unconfig_pci_adapter(struct pci_bus *bus)
  107. {
  108. struct pci_dev *dev, *tmp;
  109. list_for_each_entry_safe(dev, tmp, &bus->devices, bus_list) {
  110. eeh_remove_bus_device(dev);
  111. pci_remove_bus_device(dev);
  112. }
  113. return 0;
  114. }
  115. EXPORT_SYMBOL_GPL(rpaphp_unconfig_pci_adapter);
  116. static int setup_pci_hotplug_slot_info(struct slot *slot)
  117. {
  118. struct hotplug_slot_info *hotplug_slot_info = slot->hotplug_slot->info;
  119. dbg("%s Initilize the PCI slot's hotplug->info structure ...\n",
  120. __FUNCTION__);
  121. rpaphp_get_power_status(slot, &hotplug_slot_info->power_status);
  122. rpaphp_get_pci_adapter_status(slot, 1,
  123. &hotplug_slot_info->adapter_status);
  124. if (hotplug_slot_info->adapter_status == NOT_VALID) {
  125. err("%s: NOT_VALID: skip dn->full_name=%s\n",
  126. __FUNCTION__, slot->dn->full_name);
  127. return -EINVAL;
  128. }
  129. return 0;
  130. }
  131. static void set_slot_name(struct slot *slot)
  132. {
  133. struct pci_bus *bus = slot->bus;
  134. struct pci_dev *bridge;
  135. bridge = bus->self;
  136. if (bridge)
  137. strcpy(slot->name, pci_name(bridge));
  138. else
  139. sprintf(slot->name, "%04x:%02x:00.0", pci_domain_nr(bus),
  140. bus->number);
  141. }
  142. static int setup_pci_slot(struct slot *slot)
  143. {
  144. struct device_node *dn = slot->dn;
  145. struct pci_bus *bus;
  146. BUG_ON(!dn);
  147. bus = pcibios_find_pci_bus(dn);
  148. if (!bus) {
  149. err("%s: no pci_bus for dn %s\n", __FUNCTION__, dn->full_name);
  150. goto exit_rc;
  151. }
  152. slot->bus = bus;
  153. slot->pci_devs = &bus->devices;
  154. set_slot_name(slot);
  155. /* find slot's pci_dev if it's not empty */
  156. if (slot->hotplug_slot->info->adapter_status == EMPTY) {
  157. slot->state = EMPTY; /* slot is empty */
  158. } else {
  159. /* slot is occupied */
  160. if (!dn->child) {
  161. /* non-empty slot has to have child */
  162. err("%s: slot[%s]'s device_node doesn't have child for adapter\n",
  163. __FUNCTION__, slot->name);
  164. goto exit_rc;
  165. }
  166. if (slot->hotplug_slot->info->adapter_status == NOT_CONFIGURED) {
  167. dbg("%s CONFIGURING pci adapter in slot[%s]\n",
  168. __FUNCTION__, slot->name);
  169. pcibios_add_pci_devices(slot->bus);
  170. } else if (slot->hotplug_slot->info->adapter_status != CONFIGURED) {
  171. err("%s: slot[%s]'s adapter_status is NOT_VALID.\n",
  172. __FUNCTION__, slot->name);
  173. goto exit_rc;
  174. }
  175. print_slot_pci_funcs(slot->bus);
  176. if (!list_empty(slot->pci_devs)) {
  177. slot->state = CONFIGURED;
  178. } else {
  179. /* DLPAR add as opposed to
  180. * boot time */
  181. slot->state = NOT_CONFIGURED;
  182. }
  183. }
  184. return 0;
  185. exit_rc:
  186. dealloc_slot_struct(slot);
  187. return -EINVAL;
  188. }
  189. int register_pci_slot(struct slot *slot)
  190. {
  191. int rc = -EINVAL;
  192. if (setup_pci_hotplug_slot_info(slot))
  193. goto exit_rc;
  194. if (setup_pci_slot(slot))
  195. goto exit_rc;
  196. rc = register_slot(slot);
  197. exit_rc:
  198. return rc;
  199. }
  200. int rpaphp_enable_pci_slot(struct slot *slot)
  201. {
  202. int retval = 0, state;
  203. retval = rpaphp_get_sensor_state(slot, &state);
  204. if (retval)
  205. goto exit;
  206. dbg("%s: sensor state[%d]\n", __FUNCTION__, state);
  207. /* if slot is not empty, enable the adapter */
  208. if (state == PRESENT) {
  209. dbg("%s : slot[%s] is occupied.\n", __FUNCTION__, slot->name);
  210. pcibios_add_pci_devices(slot->bus);
  211. slot->state = CONFIGURED;
  212. info("%s: devices in slot[%s] configured\n",
  213. __FUNCTION__, slot->name);
  214. } else if (state == EMPTY) {
  215. dbg("%s : slot[%s] is empty\n", __FUNCTION__, slot->name);
  216. slot->state = EMPTY;
  217. } else {
  218. err("%s: slot[%s] is in invalid state\n", __FUNCTION__,
  219. slot->name);
  220. slot->state = NOT_VALID;
  221. retval = -EINVAL;
  222. }
  223. exit:
  224. dbg("%s - Exit: rc[%d]\n", __FUNCTION__, retval);
  225. return retval;
  226. }