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