shpchp_pci.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Standard Hot Plug Controller Driver
  4. *
  5. * Copyright (C) 1995,2001 Compaq Computer Corporation
  6. * Copyright (C) 2001 Greg Kroah-Hartman ([email protected])
  7. * Copyright (C) 2001 IBM Corp.
  8. * Copyright (C) 2003-2004 Intel Corporation
  9. *
  10. * All rights reserved.
  11. *
  12. * Send feedback to <[email protected]>, <[email protected]>
  13. *
  14. */
  15. #include <linux/module.h>
  16. #include <linux/kernel.h>
  17. #include <linux/types.h>
  18. #include <linux/pci.h>
  19. #include "../pci.h"
  20. #include "shpchp.h"
  21. int shpchp_configure_device(struct slot *p_slot)
  22. {
  23. struct pci_dev *dev;
  24. struct controller *ctrl = p_slot->ctrl;
  25. struct pci_dev *bridge = ctrl->pci_dev;
  26. struct pci_bus *parent = bridge->subordinate;
  27. int num, ret = 0;
  28. pci_lock_rescan_remove();
  29. dev = pci_get_slot(parent, PCI_DEVFN(p_slot->device, 0));
  30. if (dev) {
  31. ctrl_err(ctrl, "Device %s already exists at %04x:%02x:%02x, cannot hot-add\n",
  32. pci_name(dev), pci_domain_nr(parent),
  33. p_slot->bus, p_slot->device);
  34. pci_dev_put(dev);
  35. ret = -EINVAL;
  36. goto out;
  37. }
  38. num = pci_scan_slot(parent, PCI_DEVFN(p_slot->device, 0));
  39. if (num == 0) {
  40. ctrl_err(ctrl, "No new device found\n");
  41. ret = -ENODEV;
  42. goto out;
  43. }
  44. for_each_pci_bridge(dev, parent) {
  45. if (PCI_SLOT(dev->devfn) == p_slot->device)
  46. pci_hp_add_bridge(dev);
  47. }
  48. pci_assign_unassigned_bridge_resources(bridge);
  49. pcie_bus_configure_settings(parent);
  50. pci_bus_add_devices(parent);
  51. out:
  52. pci_unlock_rescan_remove();
  53. return ret;
  54. }
  55. void shpchp_unconfigure_device(struct slot *p_slot)
  56. {
  57. struct pci_bus *parent = p_slot->ctrl->pci_dev->subordinate;
  58. struct pci_dev *dev, *temp;
  59. struct controller *ctrl = p_slot->ctrl;
  60. ctrl_dbg(ctrl, "%s: domain:bus:dev = %04x:%02x:%02x\n",
  61. __func__, pci_domain_nr(parent), p_slot->bus, p_slot->device);
  62. pci_lock_rescan_remove();
  63. list_for_each_entry_safe(dev, temp, &parent->devices, bus_list) {
  64. if (PCI_SLOT(dev->devfn) != p_slot->device)
  65. continue;
  66. pci_dev_get(dev);
  67. pci_stop_and_remove_bus_device(dev);
  68. pci_dev_put(dev);
  69. }
  70. pci_unlock_rescan_remove();
  71. }