blob: 3f518dea856d34e4fcd21fd58f849a142cb2b6fa [file] [log] [blame]
Bjorn Helgaas736759e2018-01-26 14:22:04 -06001// SPDX-License-Identifier: GPL-2.0+
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * PCI Express 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 *
Kristen Accardi8cf4c192005-08-16 15:16:10 -070012 * Send feedback to <greg@kroah.com>, <kristen.c.accardi@intel.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
14 */
15
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/module.h>
17#include <linux/kernel.h>
18#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/pci.h>
20#include "../pci.h"
21#include "pciehp.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
rajesh.shah@intel.com71b720c2005-10-31 16:20:06 -080023int pciehp_configure_device(struct slot *p_slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -070024{
rajesh.shah@intel.com71b720c2005-10-31 16:20:06 -080025 struct pci_dev *dev;
Yinghai Lu9789ac92010-01-22 01:02:26 -080026 struct pci_dev *bridge = p_slot->ctrl->pcie->port;
27 struct pci_bus *parent = bridge->subordinate;
Rafael J. Wysockic4ec84c2014-01-14 12:03:14 -070028 int num, ret = 0;
Taku Izumi7f2feec2008-09-05 12:11:26 +090029 struct controller *ctrl = p_slot->ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Rafael J. Wysockic4ec84c2014-01-14 12:03:14 -070031 pci_lock_rescan_remove();
32
Kenji Kaneshiged689f7e2009-09-15 17:26:56 +090033 dev = pci_get_slot(parent, PCI_DEVFN(0, 0));
rajesh.shah@intel.com71b720c2005-10-31 16:20:06 -080034 if (dev) {
Mika Westerberg499022392017-10-13 21:35:46 +030035 /*
36 * The device is already there. Either configured by the
37 * boot firmware or a previous hotplug event.
38 */
39 ctrl_dbg(ctrl, "Device %s already exists at %04x:%02x:00, skipping hot-add\n",
Ryan Desfosses227f0642014-04-18 20:13:50 -040040 pci_name(dev), pci_domain_nr(parent), parent->number);
Kenji Kaneshige56bfada2006-05-12 11:22:24 +090041 pci_dev_put(dev);
Yijing Wang50277c82014-02-11 17:36:51 -070042 ret = -EEXIST;
Rafael J. Wysockic4ec84c2014-01-14 12:03:14 -070043 goto out;
rajesh.shah@intel.com71b720c2005-10-31 16:20:06 -080044 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Kenji Kaneshiged689f7e2009-09-15 17:26:56 +090046 num = pci_scan_slot(parent, PCI_DEVFN(0, 0));
rajesh.shah@intel.com71b720c2005-10-31 16:20:06 -080047 if (num == 0) {
Taku Izumi7f2feec2008-09-05 12:11:26 +090048 ctrl_err(ctrl, "No new device found\n");
Rafael J. Wysockic4ec84c2014-01-14 12:03:14 -070049 ret = -ENODEV;
50 goto out;
rajesh.shah@intel.com71b720c2005-10-31 16:20:06 -080051 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Andy Shevchenko24a0c652017-10-20 15:38:54 -050053 for_each_pci_bridge(dev, parent)
54 pci_hp_add_bridge(dev);
Yinghai Lu9789ac92010-01-22 01:02:26 -080055
56 pci_assign_unassigned_bridge_resources(bridge);
Bjorn Helgaas77094fb2014-08-28 12:13:51 -060057 pcie_bus_configure_settings(parent);
rajesh.shah@intel.com71b720c2005-10-31 16:20:06 -080058 pci_bus_add_devices(parent);
Yinghai Lu9789ac92010-01-22 01:02:26 -080059
Rafael J. Wysockic4ec84c2014-01-14 12:03:14 -070060 out:
61 pci_unlock_rescan_remove();
62 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063}
64
rajesh.shah@intel.comca22a5e2005-10-31 16:20:08 -080065int pciehp_unconfigure_device(struct slot *p_slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
Bjorn Helgaas6dae6202013-12-14 13:06:16 -070067 int rc = 0;
Kristen Carlson Accardi1cf53d52007-08-09 16:09:31 -070068 u8 presence = 0;
Yijing Wangba518e32013-01-15 11:12:19 +080069 struct pci_dev *dev, *temp;
Kenji Kaneshige385e2492009-09-15 17:30:14 +090070 struct pci_bus *parent = p_slot->ctrl->pcie->port->subordinate;
Kenji Kaneshige2326e2b2007-12-20 19:46:33 +090071 u16 command;
Taku Izumi7f2feec2008-09-05 12:11:26 +090072 struct controller *ctrl = p_slot->ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Kenji Kaneshiged689f7e2009-09-15 17:26:56 +090074 ctrl_dbg(ctrl, "%s: domain:bus:dev = %04x:%02x:00\n",
75 __func__, pci_domain_nr(parent), parent->number);
Bjorn Helgaas6dae6202013-12-14 13:06:16 -070076 pciehp_get_adapter_status(p_slot, &presence);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Rafael J. Wysockic4ec84c2014-01-14 12:03:14 -070078 pci_lock_rescan_remove();
79
Yinghai Lu29ed1f22013-07-19 12:14:16 -070080 /*
81 * Stopping an SR-IOV PF device removes all the associated VFs,
82 * which will update the bus->devices list and confuse the
83 * iterator. Therefore, iterate in reverse so we remove the VFs
84 * first, then the PF. We do the same in pci_stop_bus_device().
85 */
86 list_for_each_entry_safe_reverse(dev, temp, &parent->devices,
87 bus_list) {
Yijing Wangba518e32013-01-15 11:12:19 +080088 pci_dev_get(dev);
Keith Busch89ee9f72017-03-29 22:48:59 -050089 if (!presence) {
90 pci_dev_set_disconnected(dev, NULL);
91 if (pci_has_subordinate(dev))
92 pci_walk_bus(dev->subordinate,
93 pci_dev_set_disconnected, NULL);
94 }
Yijing Wangba518e32013-01-15 11:12:19 +080095 pci_stop_and_remove_bus_device(dev);
Kenji Kaneshige2326e2b2007-12-20 19:46:33 +090096 /*
97 * Ensure that no new Requests will be generated from
98 * the device.
99 */
100 if (presence) {
Yijing Wangba518e32013-01-15 11:12:19 +0800101 pci_read_config_word(dev, PCI_COMMAND, &command);
Kenji Kaneshige2326e2b2007-12-20 19:46:33 +0900102 command &= ~(PCI_COMMAND_MASTER | PCI_COMMAND_SERR);
103 command |= PCI_COMMAND_INTX_DISABLE;
Yijing Wangba518e32013-01-15 11:12:19 +0800104 pci_write_config_word(dev, PCI_COMMAND, command);
Kenji Kaneshige2326e2b2007-12-20 19:46:33 +0900105 }
Yijing Wangba518e32013-01-15 11:12:19 +0800106 pci_dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 }
Kenji Kaneshige9fe81642007-08-09 16:09:37 -0700108
Rafael J. Wysockic4ec84c2014-01-14 12:03:14 -0700109 pci_unlock_rescan_remove();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 return rc;
111}