rpaphp_pci.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. 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. static int setup_pci_hotplug_slot_info(struct slot *slot)
  107. {
  108. struct hotplug_slot_info *hotplug_slot_info = slot->hotplug_slot->info;
  109. dbg("%s Initilize the PCI slot's hotplug->info structure ...\n",
  110. __FUNCTION__);
  111. rpaphp_get_power_status(slot, &hotplug_slot_info->power_status);
  112. rpaphp_get_pci_adapter_status(slot, 1,
  113. &hotplug_slot_info->adapter_status);
  114. if (hotplug_slot_info->adapter_status == NOT_VALID) {
  115. err("%s: NOT_VALID: skip dn->full_name=%s\n",
  116. __FUNCTION__, slot->dn->full_name);
  117. return -EINVAL;
  118. }
  119. return 0;
  120. }
  121. static void set_slot_name(struct slot *slot)
  122. {
  123. struct pci_bus *bus = slot->bus;
  124. struct pci_dev *bridge;
  125. bridge = bus->self;
  126. if (bridge)
  127. strcpy(slot->name, pci_name(bridge));
  128. else
  129. sprintf(slot->name, "%04x:%02x:00.0", pci_domain_nr(bus),
  130. bus->number);
  131. }
  132. static int setup_pci_slot(struct slot *slot)
  133. {
  134. struct device_node *dn = slot->dn;
  135. struct pci_bus *bus;
  136. BUG_ON(!dn);
  137. bus = pcibios_find_pci_bus(dn);
  138. if (!bus) {
  139. err("%s: no pci_bus for dn %s\n", __FUNCTION__, dn->full_name);
  140. goto exit_rc;
  141. }
  142. slot->bus = bus;
  143. slot->pci_devs = &bus->devices;
  144. set_slot_name(slot);
  145. /* find slot's pci_dev if it's not empty */
  146. if (slot->hotplug_slot->info->adapter_status == EMPTY) {
  147. slot->state = EMPTY; /* slot is empty */
  148. } else {
  149. /* slot is occupied */
  150. if (!dn->child) {
  151. /* non-empty slot has to have child */
  152. err("%s: slot[%s]'s device_node doesn't have child for adapter\n",
  153. __FUNCTION__, slot->name);
  154. goto exit_rc;
  155. }
  156. if (slot->hotplug_slot->info->adapter_status == NOT_CONFIGURED) {
  157. dbg("%s CONFIGURING pci adapter in slot[%s]\n",
  158. __FUNCTION__, slot->name);
  159. pcibios_add_pci_devices(slot->bus);
  160. } else if (slot->hotplug_slot->info->adapter_status != CONFIGURED) {
  161. err("%s: slot[%s]'s adapter_status is NOT_VALID.\n",
  162. __FUNCTION__, slot->name);
  163. goto exit_rc;
  164. }
  165. print_slot_pci_funcs(slot->bus);
  166. if (!list_empty(slot->pci_devs)) {
  167. slot->state = CONFIGURED;
  168. } else {
  169. /* DLPAR add as opposed to
  170. * boot time */
  171. slot->state = NOT_CONFIGURED;
  172. }
  173. }
  174. return 0;
  175. exit_rc:
  176. dealloc_slot_struct(slot);
  177. return -EINVAL;
  178. }
  179. int rpaphp_register_pci_slot(struct slot *slot)
  180. {
  181. int rc = -EINVAL;
  182. if (setup_pci_hotplug_slot_info(slot))
  183. goto exit_rc;
  184. if (setup_pci_slot(slot))
  185. goto exit_rc;
  186. rc = rpaphp_register_slot(slot);
  187. exit_rc:
  188. return rc;
  189. }