pci-hotplug.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Derived from "arch/powerpc/platforms/pseries/pci_dlpar.c"
  3. *
  4. * Copyright (C) 2003 Linda Xie <lxie@us.ibm.com>
  5. * Copyright (C) 2005 International Business Machines
  6. *
  7. * Updates, 2005, John Rose <johnrose@austin.ibm.com>
  8. * Updates, 2005, Linas Vepstas <linas@austin.ibm.com>
  9. * Updates, 2013, Gavin Shan <shangw@linux.vnet.ibm.com>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. */
  16. #include <linux/pci.h>
  17. #include <linux/export.h>
  18. #include <asm/pci-bridge.h>
  19. #include <asm/ppc-pci.h>
  20. #include <asm/firmware.h>
  21. #include <asm/eeh.h>
  22. static struct pci_bus *find_bus_among_children(struct pci_bus *bus,
  23. struct device_node *dn)
  24. {
  25. struct pci_bus *child = NULL;
  26. struct pci_bus *tmp;
  27. if (pci_bus_to_OF_node(bus) == dn)
  28. return bus;
  29. list_for_each_entry(tmp, &bus->children, node) {
  30. child = find_bus_among_children(tmp, dn);
  31. if (child)
  32. break;
  33. }
  34. return child;
  35. }
  36. struct pci_bus *pci_find_bus_by_node(struct device_node *dn)
  37. {
  38. struct pci_dn *pdn = PCI_DN(dn);
  39. if (!pdn || !pdn->phb || !pdn->phb->bus)
  40. return NULL;
  41. return find_bus_among_children(pdn->phb->bus, dn);
  42. }
  43. EXPORT_SYMBOL_GPL(pci_find_bus_by_node);
  44. /**
  45. * pcibios_release_device - release PCI device
  46. * @dev: PCI device
  47. *
  48. * The function is called before releasing the indicated PCI device.
  49. */
  50. void pcibios_release_device(struct pci_dev *dev)
  51. {
  52. struct pci_controller *phb = pci_bus_to_host(dev->bus);
  53. eeh_remove_device(dev);
  54. if (phb->controller_ops.release_device)
  55. phb->controller_ops.release_device(dev);
  56. }
  57. /**
  58. * pci_hp_remove_devices - remove all devices under this bus
  59. * @bus: the indicated PCI bus
  60. *
  61. * Remove all of the PCI devices under this bus both from the
  62. * linux pci device tree, and from the powerpc EEH address cache.
  63. */
  64. void pci_hp_remove_devices(struct pci_bus *bus)
  65. {
  66. struct pci_dev *dev, *tmp;
  67. struct pci_bus *child_bus;
  68. /* First go down child busses */
  69. list_for_each_entry(child_bus, &bus->children, node)
  70. pci_hp_remove_devices(child_bus);
  71. pr_debug("PCI: Removing devices on bus %04x:%02x\n",
  72. pci_domain_nr(bus), bus->number);
  73. list_for_each_entry_safe_reverse(dev, tmp, &bus->devices, bus_list) {
  74. pr_debug(" Removing %s...\n", pci_name(dev));
  75. pci_stop_and_remove_bus_device(dev);
  76. }
  77. }
  78. EXPORT_SYMBOL_GPL(pci_hp_remove_devices);
  79. /**
  80. * pci_hp_add_devices - adds new pci devices to bus
  81. * @bus: the indicated PCI bus
  82. *
  83. * This routine will find and fixup new pci devices under
  84. * the indicated bus. This routine presumes that there
  85. * might already be some devices under this bridge, so
  86. * it carefully tries to add only new devices. (And that
  87. * is how this routine differs from other, similar pcibios
  88. * routines.)
  89. */
  90. void pci_hp_add_devices(struct pci_bus *bus)
  91. {
  92. int slotno, mode, pass, max;
  93. struct pci_dev *dev;
  94. struct pci_controller *phb;
  95. struct device_node *dn = pci_bus_to_OF_node(bus);
  96. eeh_add_device_tree_early(PCI_DN(dn));
  97. phb = pci_bus_to_host(bus);
  98. mode = PCI_PROBE_NORMAL;
  99. if (phb->controller_ops.probe_mode)
  100. mode = phb->controller_ops.probe_mode(bus);
  101. if (mode == PCI_PROBE_DEVTREE) {
  102. /* use ofdt-based probe */
  103. of_rescan_bus(dn, bus);
  104. } else if (mode == PCI_PROBE_NORMAL &&
  105. dn->child && PCI_DN(dn->child)) {
  106. /*
  107. * Use legacy probe. In the partial hotplug case, we
  108. * probably have grandchildren devices unplugged. So
  109. * we don't check the return value from pci_scan_slot() in
  110. * order for fully rescan all the way down to pick them up.
  111. * They can have been removed during partial hotplug.
  112. */
  113. slotno = PCI_SLOT(PCI_DN(dn->child)->devfn);
  114. pci_scan_slot(bus, PCI_DEVFN(slotno, 0));
  115. pcibios_setup_bus_devices(bus);
  116. max = bus->busn_res.start;
  117. for (pass = 0; pass < 2; pass++) {
  118. list_for_each_entry(dev, &bus->devices, bus_list) {
  119. if (pci_is_bridge(dev))
  120. max = pci_scan_bridge(bus, dev,
  121. max, pass);
  122. }
  123. }
  124. }
  125. pcibios_finish_adding_to_bus(bus);
  126. }
  127. EXPORT_SYMBOL_GPL(pci_hp_add_devices);