rpaphp_pci.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * PCI Hot Plug Controller Driver for RPA-compliant PPC64 platform.
  4. * Copyright (C) 2003 Linda Xie <lxie@us.ibm.com>
  5. *
  6. * All rights reserved.
  7. *
  8. * Send feedback to <lxie@us.ibm.com>
  9. *
  10. */
  11. #include <linux/of.h>
  12. #include <linux/pci.h>
  13. #include <linux/string.h>
  14. #include <asm/pci-bridge.h>
  15. #include <asm/rtas.h>
  16. #include <asm/machdep.h>
  17. #include "../pci.h" /* for pci_add_new_bus */
  18. #include "rpaphp.h"
  19. /*
  20. * RTAS call get-sensor-state(DR_ENTITY_SENSE) return values as per PAPR:
  21. * -- generic return codes ---
  22. * -1: Hardware Error
  23. * -2: RTAS_BUSY
  24. * -3: Invalid sensor. RTAS Parameter Error.
  25. * -- rtas_get_sensor function specific return codes ---
  26. * -9000: Need DR entity to be powered up and unisolated before RTAS call
  27. * -9001: Need DR entity to be powered up, but not unisolated, before RTAS call
  28. * -9002: DR entity unusable
  29. * 990x: Extended delay - where x is a number in the range of 0-5
  30. */
  31. #define RTAS_SLOT_UNISOLATED -9000
  32. #define RTAS_SLOT_NOT_UNISOLATED -9001
  33. #define RTAS_SLOT_NOT_USABLE -9002
  34. static int rtas_get_sensor_errno(int rtas_rc)
  35. {
  36. switch (rtas_rc) {
  37. case 0:
  38. /* Success case */
  39. return 0;
  40. case RTAS_SLOT_UNISOLATED:
  41. case RTAS_SLOT_NOT_UNISOLATED:
  42. return -EFAULT;
  43. case RTAS_SLOT_NOT_USABLE:
  44. return -ENODEV;
  45. case RTAS_BUSY:
  46. case RTAS_EXTENDED_DELAY_MIN...RTAS_EXTENDED_DELAY_MAX:
  47. return -EBUSY;
  48. default:
  49. return rtas_error_rc(rtas_rc);
  50. }
  51. }
  52. /*
  53. * get_adapter_status() can be called by the EEH handler during EEH recovery.
  54. * On certain PHB failures, the RTAS call rtas_call(get-sensor-state) returns
  55. * extended busy error (9902) until PHB is recovered by pHyp. The RTAS call
  56. * interface rtas_get_sensor() loops over the RTAS call on extended delay
  57. * return code (9902) until the return value is either success (0) or error
  58. * (-1). This causes the EEH handler to get stuck for ~6 seconds before it
  59. * could notify that the PCI error has been detected and stop any active
  60. * operations. This sometimes causes EEH recovery to fail. To avoid this issue,
  61. * invoke rtas_call(get-sensor-state) directly if the respective PE is in EEH
  62. * recovery state and return -EBUSY error based on RTAS return status. This
  63. * will help the EEH handler to notify the driver about the PCI error
  64. * immediately and successfully proceed with EEH recovery steps.
  65. */
  66. static int __rpaphp_get_sensor_state(struct slot *slot, int *state)
  67. {
  68. int rc;
  69. int token = rtas_token("get-sensor-state");
  70. struct pci_dn *pdn;
  71. struct eeh_pe *pe;
  72. struct pci_controller *phb = PCI_DN(slot->dn)->phb;
  73. if (token == RTAS_UNKNOWN_SERVICE)
  74. return -ENOENT;
  75. /*
  76. * Fallback to existing method for empty slot or PE isn't in EEH
  77. * recovery.
  78. */
  79. pdn = list_first_entry_or_null(&PCI_DN(phb->dn)->child_list,
  80. struct pci_dn, list);
  81. if (!pdn)
  82. goto fallback;
  83. pe = eeh_dev_to_pe(pdn->edev);
  84. if (pe && (pe->state & EEH_PE_RECOVERING)) {
  85. rc = rtas_call(token, 2, 2, state, DR_ENTITY_SENSE,
  86. slot->index);
  87. return rtas_get_sensor_errno(rc);
  88. }
  89. fallback:
  90. return rtas_get_sensor(DR_ENTITY_SENSE, slot->index, state);
  91. }
  92. int rpaphp_get_sensor_state(struct slot *slot, int *state)
  93. {
  94. int rc;
  95. int setlevel;
  96. rc = __rpaphp_get_sensor_state(slot, state);
  97. if (rc < 0) {
  98. if (rc == -EFAULT || rc == -EEXIST) {
  99. dbg("%s: slot must be power up to get sensor-state\n",
  100. __func__);
  101. /* some slots have to be powered up
  102. * before get-sensor will succeed.
  103. */
  104. rc = rtas_set_power_level(slot->power_domain, POWER_ON,
  105. &setlevel);
  106. if (rc < 0) {
  107. dbg("%s: power on slot[%s] failed rc=%d.\n",
  108. __func__, slot->name, rc);
  109. } else {
  110. rc = __rpaphp_get_sensor_state(slot, state);
  111. }
  112. } else if (rc == -ENODEV)
  113. info("%s: slot is unusable\n", __func__);
  114. else
  115. err("%s failed to get sensor state\n", __func__);
  116. }
  117. return rc;
  118. }
  119. /**
  120. * rpaphp_enable_slot - record slot state, config pci device
  121. * @slot: target &slot
  122. *
  123. * Initialize values in the slot structure to indicate if there is a pci card
  124. * plugged into the slot. If the slot is not empty, run the pcibios routine
  125. * to get pcibios stuff correctly set up.
  126. */
  127. int rpaphp_enable_slot(struct slot *slot)
  128. {
  129. int rc, level, state;
  130. struct pci_bus *bus;
  131. slot->state = EMPTY;
  132. /* Find out if the power is turned on for the slot */
  133. rc = rtas_get_power_level(slot->power_domain, &level);
  134. if (rc)
  135. return rc;
  136. /* Figure out if there is an adapter in the slot */
  137. rc = rpaphp_get_sensor_state(slot, &state);
  138. if (rc)
  139. return rc;
  140. bus = pci_find_bus_by_node(slot->dn);
  141. if (!bus) {
  142. err("%s: no pci_bus for dn %pOF\n", __func__, slot->dn);
  143. return -EINVAL;
  144. }
  145. slot->bus = bus;
  146. slot->pci_devs = &bus->devices;
  147. /* if there's an adapter in the slot, go add the pci devices */
  148. if (state == PRESENT) {
  149. slot->state = NOT_CONFIGURED;
  150. /* non-empty slot has to have child */
  151. if (!slot->dn->child) {
  152. err("%s: slot[%s]'s device_node doesn't have child for adapter\n",
  153. __func__, slot->name);
  154. return -EINVAL;
  155. }
  156. if (list_empty(&bus->devices)) {
  157. pseries_eeh_init_edev_recursive(PCI_DN(slot->dn));
  158. pci_hp_add_devices(bus);
  159. }
  160. if (!list_empty(&bus->devices)) {
  161. slot->state = CONFIGURED;
  162. }
  163. if (rpaphp_debug) {
  164. struct pci_dev *dev;
  165. dbg("%s: pci_devs of slot[%pOF]\n", __func__, slot->dn);
  166. list_for_each_entry(dev, &bus->devices, bus_list)
  167. dbg("\t%s\n", pci_name(dev));
  168. }
  169. }
  170. return 0;
  171. }