shpchp_pci.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 (greg@kroah.com)
  7. * Copyright (C) 2001 IBM Corp.
  8. * Copyright (C) 2003-2004 Intel Corporation
  9. *
  10. * All rights reserved.
  11. *
  12. * Send feedback to <greg@kroah.com>, <kristen.c.accardi@intel.com>
  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. int shpchp_unconfigure_device(struct slot *p_slot)
  56. {
  57. int rc = 0;
  58. u8 bctl = 0;
  59. struct pci_bus *parent = p_slot->ctrl->pci_dev->subordinate;
  60. struct pci_dev *dev, *temp;
  61. struct controller *ctrl = p_slot->ctrl;
  62. ctrl_dbg(ctrl, "%s: domain:bus:dev = %04x:%02x:%02x\n",
  63. __func__, pci_domain_nr(parent), p_slot->bus, p_slot->device);
  64. pci_lock_rescan_remove();
  65. list_for_each_entry_safe(dev, temp, &parent->devices, bus_list) {
  66. if (PCI_SLOT(dev->devfn) != p_slot->device)
  67. continue;
  68. pci_dev_get(dev);
  69. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
  70. pci_read_config_byte(dev, PCI_BRIDGE_CONTROL, &bctl);
  71. if (bctl & PCI_BRIDGE_CTL_VGA) {
  72. ctrl_err(ctrl,
  73. "Cannot remove display device %s\n",
  74. pci_name(dev));
  75. pci_dev_put(dev);
  76. rc = -EINVAL;
  77. break;
  78. }
  79. }
  80. pci_stop_and_remove_bus_device(dev);
  81. pci_dev_put(dev);
  82. }
  83. pci_unlock_rescan_remove();
  84. return rc;
  85. }