Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * PCI Express Hot Plug Controller Driver
0004  *
0005  * Copyright (C) 1995,2001 Compaq Computer Corporation
0006  * Copyright (C) 2001 Greg Kroah-Hartman (greg@kroah.com)
0007  * Copyright (C) 2001 IBM Corp.
0008  * Copyright (C) 2003-2004 Intel Corporation
0009  *
0010  * All rights reserved.
0011  *
0012  * Send feedback to <greg@kroah.com>, <kristen.c.accardi@intel.com>
0013  *
0014  */
0015 
0016 #define dev_fmt(fmt) "pciehp: " fmt
0017 
0018 #include <linux/kernel.h>
0019 #include <linux/types.h>
0020 #include <linux/pci.h>
0021 #include "../pci.h"
0022 #include "pciehp.h"
0023 
0024 /**
0025  * pciehp_configure_device() - enumerate PCI devices below a hotplug bridge
0026  * @ctrl: PCIe hotplug controller
0027  *
0028  * Enumerate PCI devices below a hotplug bridge and add them to the system.
0029  * Return 0 on success, %-EEXIST if the devices are already enumerated or
0030  * %-ENODEV if enumeration failed.
0031  */
0032 int pciehp_configure_device(struct controller *ctrl)
0033 {
0034     struct pci_dev *dev;
0035     struct pci_dev *bridge = ctrl->pcie->port;
0036     struct pci_bus *parent = bridge->subordinate;
0037     int num, ret = 0;
0038 
0039     pci_lock_rescan_remove();
0040 
0041     dev = pci_get_slot(parent, PCI_DEVFN(0, 0));
0042     if (dev) {
0043         /*
0044          * The device is already there. Either configured by the
0045          * boot firmware or a previous hotplug event.
0046          */
0047         ctrl_dbg(ctrl, "Device %s already exists at %04x:%02x:00, skipping hot-add\n",
0048              pci_name(dev), pci_domain_nr(parent), parent->number);
0049         pci_dev_put(dev);
0050         ret = -EEXIST;
0051         goto out;
0052     }
0053 
0054     num = pci_scan_slot(parent, PCI_DEVFN(0, 0));
0055     if (num == 0) {
0056         ctrl_err(ctrl, "No new device found\n");
0057         ret = -ENODEV;
0058         goto out;
0059     }
0060 
0061     for_each_pci_bridge(dev, parent)
0062         pci_hp_add_bridge(dev);
0063 
0064     pci_assign_unassigned_bridge_resources(bridge);
0065     pcie_bus_configure_settings(parent);
0066     pci_bus_add_devices(parent);
0067 
0068  out:
0069     pci_unlock_rescan_remove();
0070     return ret;
0071 }
0072 
0073 /**
0074  * pciehp_unconfigure_device() - remove PCI devices below a hotplug bridge
0075  * @ctrl: PCIe hotplug controller
0076  * @presence: whether the card is still present in the slot;
0077  *  true for safe removal via sysfs or an Attention Button press,
0078  *  false for surprise removal
0079  *
0080  * Unbind PCI devices below a hotplug bridge from their drivers and remove
0081  * them from the system.  Safely removed devices are quiesced.  Surprise
0082  * removed devices are marked as such to prevent further accesses.
0083  */
0084 void pciehp_unconfigure_device(struct controller *ctrl, bool presence)
0085 {
0086     struct pci_dev *dev, *temp;
0087     struct pci_bus *parent = ctrl->pcie->port->subordinate;
0088     u16 command;
0089 
0090     ctrl_dbg(ctrl, "%s: domain:bus:dev = %04x:%02x:00\n",
0091          __func__, pci_domain_nr(parent), parent->number);
0092 
0093     if (!presence)
0094         pci_walk_bus(parent, pci_dev_set_disconnected, NULL);
0095 
0096     pci_lock_rescan_remove();
0097 
0098     /*
0099      * Stopping an SR-IOV PF device removes all the associated VFs,
0100      * which will update the bus->devices list and confuse the
0101      * iterator.  Therefore, iterate in reverse so we remove the VFs
0102      * first, then the PF.  We do the same in pci_stop_bus_device().
0103      */
0104     list_for_each_entry_safe_reverse(dev, temp, &parent->devices,
0105                      bus_list) {
0106         pci_dev_get(dev);
0107         pci_stop_and_remove_bus_device(dev);
0108         /*
0109          * Ensure that no new Requests will be generated from
0110          * the device.
0111          */
0112         if (presence) {
0113             pci_read_config_word(dev, PCI_COMMAND, &command);
0114             command &= ~(PCI_COMMAND_MASTER | PCI_COMMAND_SERR);
0115             command |= PCI_COMMAND_INTX_DISABLE;
0116             pci_write_config_word(dev, PCI_COMMAND, command);
0117         }
0118         pci_dev_put(dev);
0119     }
0120 
0121     pci_unlock_rescan_remove();
0122 }