1 // SPDX-License-Identifier: GPL-2.0+
3 * CompactPCI Hot Plug Driver PCI functions
5 * Copyright (C) 2002,2005 by SOMA Networks, Inc.
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/pci.h>
15 #include <linux/pci_hotplug.h>
16 #include <linux/proc_fs.h>
18 #include "cpci_hotplug.h"
20 #define MY_NAME "cpci_hotplug"
22 extern int cpci_debug;
24 #define dbg(format, arg...) \
27 printk(KERN_DEBUG "%s: " format "\n", \
30 #define err(format, arg...) printk(KERN_ERR "%s: " format "\n", MY_NAME, ## arg)
31 #define info(format, arg...) printk(KERN_INFO "%s: " format "\n", MY_NAME, ## arg)
32 #define warn(format, arg...) printk(KERN_WARNING "%s: " format "\n", MY_NAME, ## arg)
35 u8 cpci_get_attention_status(struct slot *slot)
40 hs_cap = pci_bus_find_capability(slot->bus,
46 if (pci_bus_read_config_word(slot->bus,
52 return hs_csr & 0x0008 ? 1 : 0;
55 int cpci_set_attention_status(struct slot *slot, int status)
60 hs_cap = pci_bus_find_capability(slot->bus,
65 if (pci_bus_read_config_word(slot->bus,
73 hs_csr &= ~HS_CSR_LOO;
74 if (pci_bus_write_config_word(slot->bus,
82 u16 cpci_get_hs_csr(struct slot *slot)
87 hs_cap = pci_bus_find_capability(slot->bus,
92 if (pci_bus_read_config_word(slot->bus,
100 int cpci_check_and_clear_ins(struct slot *slot)
106 hs_cap = pci_bus_find_capability(slot->bus,
111 if (pci_bus_read_config_word(slot->bus,
116 if (hs_csr & HS_CSR_INS) {
117 /* Clear INS (by setting it) */
118 if (pci_bus_write_config_word(slot->bus,
129 int cpci_check_ext(struct slot *slot)
135 hs_cap = pci_bus_find_capability(slot->bus,
140 if (pci_bus_read_config_word(slot->bus,
145 if (hs_csr & HS_CSR_EXT)
150 int cpci_clear_ext(struct slot *slot)
155 hs_cap = pci_bus_find_capability(slot->bus,
160 if (pci_bus_read_config_word(slot->bus,
165 if (hs_csr & HS_CSR_EXT) {
166 /* Clear EXT (by setting it) */
167 if (pci_bus_write_config_word(slot->bus,
176 int cpci_led_on(struct slot *slot)
181 hs_cap = pci_bus_find_capability(slot->bus,
186 if (pci_bus_read_config_word(slot->bus,
191 if ((hs_csr & HS_CSR_LOO) != HS_CSR_LOO) {
192 hs_csr |= HS_CSR_LOO;
193 if (pci_bus_write_config_word(slot->bus,
197 err("Could not set LOO for slot %s", slot_name(slot));
204 int cpci_led_off(struct slot *slot)
209 hs_cap = pci_bus_find_capability(slot->bus,
214 if (pci_bus_read_config_word(slot->bus,
219 if (hs_csr & HS_CSR_LOO) {
220 hs_csr &= ~HS_CSR_LOO;
221 if (pci_bus_write_config_word(slot->bus,
225 err("Could not clear LOO for slot %s", slot_name(slot));
234 * Device configuration functions
237 int cpci_configure_slot(struct slot *slot)
240 struct pci_bus *parent;
243 dbg("%s - enter", __func__);
245 pci_lock_rescan_remove();
247 if (slot->dev == NULL) {
248 dbg("pci_dev null, finding %02x:%02x:%x",
249 slot->bus->number, PCI_SLOT(slot->devfn), PCI_FUNC(slot->devfn));
250 slot->dev = pci_get_slot(slot->bus, slot->devfn);
253 /* Still NULL? Well then scan for it! */
254 if (slot->dev == NULL) {
256 dbg("pci_dev still null");
259 * This will generate pci_dev structures for all functions, but
260 * we will only call this case when lookup fails.
262 n = pci_scan_slot(slot->bus, slot->devfn);
263 dbg("%s: pci_scan_slot returned %d", __func__, n);
264 slot->dev = pci_get_slot(slot->bus, slot->devfn);
265 if (slot->dev == NULL) {
266 err("Could not find PCI device for slot %02x", slot->number);
271 parent = slot->dev->bus;
273 for_each_pci_bridge(dev, parent) {
274 if (PCI_SLOT(dev->devfn) == PCI_SLOT(slot->devfn))
275 pci_hp_add_bridge(dev);
278 pci_assign_unassigned_bridge_resources(parent->self);
280 pci_bus_add_devices(parent);
283 pci_unlock_rescan_remove();
284 dbg("%s - exit", __func__);
288 int cpci_unconfigure_slot(struct slot *slot)
290 struct pci_dev *dev, *temp;
292 dbg("%s - enter", __func__);
294 err("No device for slot %02x\n", slot->number);
298 pci_lock_rescan_remove();
300 list_for_each_entry_safe(dev, temp, &slot->bus->devices, bus_list) {
301 if (PCI_SLOT(dev->devfn) != PCI_SLOT(slot->devfn))
304 pci_stop_and_remove_bus_device(dev);
307 pci_dev_put(slot->dev);
310 pci_unlock_rescan_remove();
312 dbg("%s - exit", __func__);