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 #define dbg(format, arg...) \
25 printk(KERN_DEBUG "%s: " format "\n", \
28 #define err(format, arg...) printk(KERN_ERR "%s: " format "\n", MY_NAME, ## arg)
29 #define info(format, arg...) printk(KERN_INFO "%s: " format "\n", MY_NAME, ## arg)
30 #define warn(format, arg...) printk(KERN_WARNING "%s: " format "\n", MY_NAME, ## arg)
33 u8 cpci_get_attention_status(struct slot *slot)
38 hs_cap = pci_bus_find_capability(slot->bus,
44 if (pci_bus_read_config_word(slot->bus,
50 return hs_csr & 0x0008 ? 1 : 0;
53 int cpci_set_attention_status(struct slot *slot, int status)
58 hs_cap = pci_bus_find_capability(slot->bus,
63 if (pci_bus_read_config_word(slot->bus,
71 hs_csr &= ~HS_CSR_LOO;
72 if (pci_bus_write_config_word(slot->bus,
80 u16 cpci_get_hs_csr(struct slot *slot)
85 hs_cap = pci_bus_find_capability(slot->bus,
90 if (pci_bus_read_config_word(slot->bus,
98 int cpci_check_and_clear_ins(struct slot *slot)
104 hs_cap = pci_bus_find_capability(slot->bus,
109 if (pci_bus_read_config_word(slot->bus,
114 if (hs_csr & HS_CSR_INS) {
115 /* Clear INS (by setting it) */
116 if (pci_bus_write_config_word(slot->bus,
127 int cpci_check_ext(struct slot *slot)
133 hs_cap = pci_bus_find_capability(slot->bus,
138 if (pci_bus_read_config_word(slot->bus,
143 if (hs_csr & HS_CSR_EXT)
148 int cpci_clear_ext(struct slot *slot)
153 hs_cap = pci_bus_find_capability(slot->bus,
158 if (pci_bus_read_config_word(slot->bus,
163 if (hs_csr & HS_CSR_EXT) {
164 /* Clear EXT (by setting it) */
165 if (pci_bus_write_config_word(slot->bus,
174 int cpci_led_on(struct slot *slot)
179 hs_cap = pci_bus_find_capability(slot->bus,
184 if (pci_bus_read_config_word(slot->bus,
189 if ((hs_csr & HS_CSR_LOO) != HS_CSR_LOO) {
190 hs_csr |= HS_CSR_LOO;
191 if (pci_bus_write_config_word(slot->bus,
195 err("Could not set LOO for slot %s", slot_name(slot));
202 int cpci_led_off(struct slot *slot)
207 hs_cap = pci_bus_find_capability(slot->bus,
212 if (pci_bus_read_config_word(slot->bus,
217 if (hs_csr & HS_CSR_LOO) {
218 hs_csr &= ~HS_CSR_LOO;
219 if (pci_bus_write_config_word(slot->bus,
223 err("Could not clear LOO for slot %s", slot_name(slot));
232 * Device configuration functions
235 int cpci_configure_slot(struct slot *slot)
238 struct pci_bus *parent;
241 dbg("%s - enter", __func__);
243 pci_lock_rescan_remove();
245 if (slot->dev == NULL) {
246 dbg("pci_dev null, finding %02x:%02x:%x",
247 slot->bus->number, PCI_SLOT(slot->devfn), PCI_FUNC(slot->devfn));
248 slot->dev = pci_get_slot(slot->bus, slot->devfn);
251 /* Still NULL? Well then scan for it! */
252 if (slot->dev == NULL) {
254 dbg("pci_dev still null");
257 * This will generate pci_dev structures for all functions, but
258 * we will only call this case when lookup fails.
260 n = pci_scan_slot(slot->bus, slot->devfn);
261 dbg("%s: pci_scan_slot returned %d", __func__, n);
262 slot->dev = pci_get_slot(slot->bus, slot->devfn);
263 if (slot->dev == NULL) {
264 err("Could not find PCI device for slot %02x", slot->number);
269 parent = slot->dev->bus;
271 for_each_pci_bridge(dev, parent) {
272 if (PCI_SLOT(dev->devfn) == PCI_SLOT(slot->devfn))
273 pci_hp_add_bridge(dev);
276 pci_assign_unassigned_bridge_resources(parent->self);
278 pci_bus_add_devices(parent);
281 pci_unlock_rescan_remove();
282 dbg("%s - exit", __func__);
286 int cpci_unconfigure_slot(struct slot *slot)
288 struct pci_dev *dev, *temp;
290 dbg("%s - enter", __func__);
292 err("No device for slot %02x\n", slot->number);
296 pci_lock_rescan_remove();
298 list_for_each_entry_safe(dev, temp, &slot->bus->devices, bus_list) {
299 if (PCI_SLOT(dev->devfn) != PCI_SLOT(slot->devfn))
302 pci_stop_and_remove_bus_device(dev);
305 pci_dev_put(slot->dev);
308 pci_unlock_rescan_remove();
310 dbg("%s - exit", __func__);