rpaphp_pci.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. static void set_slot_name(struct slot *slot)
  61. {
  62. struct pci_bus *bus = slot->bus;
  63. struct pci_dev *bridge;
  64. bridge = bus->self;
  65. if (bridge)
  66. strcpy(slot->name, pci_name(bridge));
  67. else
  68. sprintf(slot->name, "%04x:%02x:00.0", pci_domain_nr(bus),
  69. bus->number);
  70. }
  71. /**
  72. * rpaphp_enable_slot - record slot state, config pci device
  73. *
  74. * Initialize values in the slot, and the hotplug_slot info
  75. * structures to indicate if there is a pci card plugged into
  76. * the slot. If the slot is not empty, run the pcibios routine
  77. * to get pcibios stuff correctly set up.
  78. */
  79. int rpaphp_enable_slot(struct slot *slot)
  80. {
  81. int rc, level, state;
  82. struct pci_bus *bus;
  83. struct hotplug_slot_info *info = slot->hotplug_slot->info;
  84. info->adapter_status = NOT_VALID;
  85. slot->state = EMPTY;
  86. /* Find out if the power is turned on for the slot */
  87. rc = rtas_get_power_level(slot->power_domain, &level);
  88. if (rc)
  89. return rc;
  90. info->power_status = level;
  91. /* Figure out if there is an adapter in the slot */
  92. rc = rpaphp_get_sensor_state(slot, &state);
  93. if (rc)
  94. return rc;
  95. bus = pcibios_find_pci_bus(slot->dn);
  96. if (!bus) {
  97. err("%s: no pci_bus for dn %s\n", __FUNCTION__, slot->dn->full_name);
  98. return -EINVAL;
  99. }
  100. info->adapter_status = EMPTY;
  101. slot->bus = bus;
  102. slot->pci_devs = &bus->devices;
  103. set_slot_name(slot);
  104. /* if there's an adapter in the slot, go add the pci devices */
  105. if (state == PRESENT) {
  106. info->adapter_status = NOT_CONFIGURED;
  107. slot->state = NOT_CONFIGURED;
  108. /* non-empty slot has to have child */
  109. if (!slot->dn->child) {
  110. err("%s: slot[%s]'s device_node doesn't have child for adapter\n",
  111. __FUNCTION__, slot->name);
  112. return -EINVAL;
  113. }
  114. if (list_empty(&bus->devices))
  115. pcibios_add_pci_devices(bus);
  116. if (!list_empty(&bus->devices)) {
  117. info->adapter_status = CONFIGURED;
  118. slot->state = CONFIGURED;
  119. }
  120. if (debug) {
  121. struct pci_dev *dev;
  122. dbg("%s: pci_devs of slot[%s]\n", __FUNCTION__, slot->dn->full_name);
  123. list_for_each_entry (dev, &bus->devices, bus_list)
  124. dbg("\t%s\n", pci_name(dev));
  125. }
  126. }
  127. return 0;
  128. }