eeh_pe.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. * The file intends to implement PE based on the information from
  3. * platforms. Basically, there have 3 types of PEs: PHB/Bus/Device.
  4. * All the PEs should be organized as hierarchy tree. The first level
  5. * of the tree will be associated to existing PHBs since the particular
  6. * PE is only meaningful in one PHB domain.
  7. *
  8. * Copyright Benjamin Herrenschmidt & Gavin Shan, IBM Corporation 2012.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. */
  24. #include <linux/export.h>
  25. #include <linux/gfp.h>
  26. #include <linux/init.h>
  27. #include <linux/kernel.h>
  28. #include <linux/pci.h>
  29. #include <linux/string.h>
  30. #include <asm/pci-bridge.h>
  31. #include <asm/ppc-pci.h>
  32. static LIST_HEAD(eeh_phb_pe);
  33. /**
  34. * eeh_pe_alloc - Allocate PE
  35. * @phb: PCI controller
  36. * @type: PE type
  37. *
  38. * Allocate PE instance dynamically.
  39. */
  40. static struct eeh_pe *eeh_pe_alloc(struct pci_controller *phb, int type)
  41. {
  42. struct eeh_pe *pe;
  43. /* Allocate PHB PE */
  44. pe = kzalloc(sizeof(struct eeh_pe), GFP_KERNEL);
  45. if (!pe) return NULL;
  46. /* Initialize PHB PE */
  47. pe->type = type;
  48. pe->phb = phb;
  49. INIT_LIST_HEAD(&pe->child_list);
  50. INIT_LIST_HEAD(&pe->child);
  51. INIT_LIST_HEAD(&pe->edevs);
  52. return pe;
  53. }
  54. /**
  55. * eeh_phb_pe_create - Create PHB PE
  56. * @phb: PCI controller
  57. *
  58. * The function should be called while the PHB is detected during
  59. * system boot or PCI hotplug in order to create PHB PE.
  60. */
  61. int __devinit eeh_phb_pe_create(struct pci_controller *phb)
  62. {
  63. struct eeh_pe *pe;
  64. /* Allocate PHB PE */
  65. pe = eeh_pe_alloc(phb, EEH_PE_PHB);
  66. if (!pe) {
  67. pr_err("%s: out of memory!\n", __func__);
  68. return -ENOMEM;
  69. }
  70. /* Put it into the list */
  71. eeh_lock();
  72. list_add_tail(&pe->child, &eeh_phb_pe);
  73. eeh_unlock();
  74. pr_debug("EEH: Add PE for PHB#%d\n", phb->global_number);
  75. return 0;
  76. }
  77. /**
  78. * eeh_phb_pe_get - Retrieve PHB PE based on the given PHB
  79. * @phb: PCI controller
  80. *
  81. * The overall PEs form hierarchy tree. The first layer of the
  82. * hierarchy tree is composed of PHB PEs. The function is used
  83. * to retrieve the corresponding PHB PE according to the given PHB.
  84. */
  85. static struct eeh_pe *eeh_phb_pe_get(struct pci_controller *phb)
  86. {
  87. struct eeh_pe *pe;
  88. eeh_lock();
  89. list_for_each_entry(pe, &eeh_phb_pe, child) {
  90. /*
  91. * Actually, we needn't check the type since
  92. * the PE for PHB has been determined when that
  93. * was created.
  94. */
  95. if (pe->type == EEH_PE_PHB &&
  96. pe->phb == phb) {
  97. eeh_unlock();
  98. return pe;
  99. }
  100. }
  101. eeh_unlock();
  102. return NULL;
  103. }
  104. /**
  105. * eeh_pe_next - Retrieve the next PE in the tree
  106. * @pe: current PE
  107. * @root: root PE
  108. *
  109. * The function is used to retrieve the next PE in the
  110. * hierarchy PE tree.
  111. */
  112. static struct eeh_pe *eeh_pe_next(struct eeh_pe *pe,
  113. struct eeh_pe *root)
  114. {
  115. struct list_head *next = pe->child_list.next;
  116. if (next == &pe->child_list) {
  117. while (1) {
  118. if (pe == root)
  119. return NULL;
  120. next = pe->child.next;
  121. if (next != &pe->parent->child_list)
  122. break;
  123. pe = pe->parent;
  124. }
  125. }
  126. return list_entry(next, struct eeh_pe, child);
  127. }
  128. /**
  129. * eeh_pe_traverse - Traverse PEs in the specified PHB
  130. * @root: root PE
  131. * @fn: callback
  132. * @flag: extra parameter to callback
  133. *
  134. * The function is used to traverse the specified PE and its
  135. * child PEs. The traversing is to be terminated once the
  136. * callback returns something other than NULL, or no more PEs
  137. * to be traversed.
  138. */
  139. static void *eeh_pe_traverse(struct eeh_pe *root,
  140. eeh_traverse_func fn, void *flag)
  141. {
  142. struct eeh_pe *pe;
  143. void *ret;
  144. for (pe = root; pe; pe = eeh_pe_next(pe, root)) {
  145. ret = fn(pe, flag);
  146. if (ret) return ret;
  147. }
  148. return NULL;
  149. }
  150. /**
  151. * __eeh_pe_get - Check the PE address
  152. * @data: EEH PE
  153. * @flag: EEH device
  154. *
  155. * For one particular PE, it can be identified by PE address
  156. * or tranditional BDF address. BDF address is composed of
  157. * Bus/Device/Function number. The extra data referred by flag
  158. * indicates which type of address should be used.
  159. */
  160. static void *__eeh_pe_get(void *data, void *flag)
  161. {
  162. struct eeh_pe *pe = (struct eeh_pe *)data;
  163. struct eeh_dev *edev = (struct eeh_dev *)flag;
  164. /* Unexpected PHB PE */
  165. if (pe->type == EEH_PE_PHB)
  166. return NULL;
  167. /* We prefer PE address */
  168. if (edev->pe_config_addr &&
  169. (edev->pe_config_addr == pe->addr))
  170. return pe;
  171. /* Try BDF address */
  172. if (edev->pe_config_addr &&
  173. (edev->config_addr == pe->config_addr))
  174. return pe;
  175. return NULL;
  176. }
  177. /**
  178. * eeh_pe_get - Search PE based on the given address
  179. * @edev: EEH device
  180. *
  181. * Search the corresponding PE based on the specified address which
  182. * is included in the eeh device. The function is used to check if
  183. * the associated PE has been created against the PE address. It's
  184. * notable that the PE address has 2 format: traditional PE address
  185. * which is composed of PCI bus/device/function number, or unified
  186. * PE address.
  187. */
  188. static struct eeh_pe *eeh_pe_get(struct eeh_dev *edev)
  189. {
  190. struct eeh_pe *root = eeh_phb_pe_get(edev->phb);
  191. struct eeh_pe *pe;
  192. eeh_lock();
  193. pe = eeh_pe_traverse(root, __eeh_pe_get, edev);
  194. eeh_unlock();
  195. return pe;
  196. }
  197. /**
  198. * eeh_pe_get_parent - Retrieve the parent PE
  199. * @edev: EEH device
  200. *
  201. * The whole PEs existing in the system are organized as hierarchy
  202. * tree. The function is used to retrieve the parent PE according
  203. * to the parent EEH device.
  204. */
  205. static struct eeh_pe *eeh_pe_get_parent(struct eeh_dev *edev)
  206. {
  207. struct device_node *dn;
  208. struct eeh_dev *parent;
  209. /*
  210. * It might have the case for the indirect parent
  211. * EEH device already having associated PE, but
  212. * the direct parent EEH device doesn't have yet.
  213. */
  214. dn = edev->dn->parent;
  215. while (dn) {
  216. /* We're poking out of PCI territory */
  217. if (!PCI_DN(dn)) return NULL;
  218. parent = of_node_to_eeh_dev(dn);
  219. /* We're poking out of PCI territory */
  220. if (!parent) return NULL;
  221. if (parent->pe)
  222. return parent->pe;
  223. dn = dn->parent;
  224. }
  225. return NULL;
  226. }