PCI: hotplug: Embed hotplug_slot
[linux/fpc-iii.git] / drivers / pci / hotplug / cpci_hotplug_pci.c
blob2c16adb7f4ec1b92ca931eddda34f5ee5903335c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * CompactPCI Hot Plug Driver PCI functions
5 * Copyright (C) 2002,2005 by SOMA Networks, Inc.
7 * All rights reserved.
9 * Send feedback to <scottm@somanetworks.com>
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>
17 #include "../pci.h"
18 #include "cpci_hotplug.h"
20 #define MY_NAME "cpci_hotplug"
22 extern int cpci_debug;
24 #define dbg(format, arg...) \
25 do { \
26 if (cpci_debug) \
27 printk(KERN_DEBUG "%s: " format "\n", \
28 MY_NAME, ## arg); \
29 } while (0)
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)
37 int hs_cap;
38 u16 hs_csr;
40 hs_cap = pci_bus_find_capability(slot->bus,
41 slot->devfn,
42 PCI_CAP_ID_CHSWP);
43 if (!hs_cap)
44 return 0;
46 if (pci_bus_read_config_word(slot->bus,
47 slot->devfn,
48 hs_cap + 2,
49 &hs_csr))
50 return 0;
52 return hs_csr & 0x0008 ? 1 : 0;
55 int cpci_set_attention_status(struct slot *slot, int status)
57 int hs_cap;
58 u16 hs_csr;
60 hs_cap = pci_bus_find_capability(slot->bus,
61 slot->devfn,
62 PCI_CAP_ID_CHSWP);
63 if (!hs_cap)
64 return 0;
65 if (pci_bus_read_config_word(slot->bus,
66 slot->devfn,
67 hs_cap + 2,
68 &hs_csr))
69 return 0;
70 if (status)
71 hs_csr |= HS_CSR_LOO;
72 else
73 hs_csr &= ~HS_CSR_LOO;
74 if (pci_bus_write_config_word(slot->bus,
75 slot->devfn,
76 hs_cap + 2,
77 hs_csr))
78 return 0;
79 return 1;
82 u16 cpci_get_hs_csr(struct slot *slot)
84 int hs_cap;
85 u16 hs_csr;
87 hs_cap = pci_bus_find_capability(slot->bus,
88 slot->devfn,
89 PCI_CAP_ID_CHSWP);
90 if (!hs_cap)
91 return 0xFFFF;
92 if (pci_bus_read_config_word(slot->bus,
93 slot->devfn,
94 hs_cap + 2,
95 &hs_csr))
96 return 0xFFFF;
97 return hs_csr;
100 int cpci_check_and_clear_ins(struct slot *slot)
102 int hs_cap;
103 u16 hs_csr;
104 int ins = 0;
106 hs_cap = pci_bus_find_capability(slot->bus,
107 slot->devfn,
108 PCI_CAP_ID_CHSWP);
109 if (!hs_cap)
110 return 0;
111 if (pci_bus_read_config_word(slot->bus,
112 slot->devfn,
113 hs_cap + 2,
114 &hs_csr))
115 return 0;
116 if (hs_csr & HS_CSR_INS) {
117 /* Clear INS (by setting it) */
118 if (pci_bus_write_config_word(slot->bus,
119 slot->devfn,
120 hs_cap + 2,
121 hs_csr))
122 ins = 0;
123 else
124 ins = 1;
126 return ins;
129 int cpci_check_ext(struct slot *slot)
131 int hs_cap;
132 u16 hs_csr;
133 int ext = 0;
135 hs_cap = pci_bus_find_capability(slot->bus,
136 slot->devfn,
137 PCI_CAP_ID_CHSWP);
138 if (!hs_cap)
139 return 0;
140 if (pci_bus_read_config_word(slot->bus,
141 slot->devfn,
142 hs_cap + 2,
143 &hs_csr))
144 return 0;
145 if (hs_csr & HS_CSR_EXT)
146 ext = 1;
147 return ext;
150 int cpci_clear_ext(struct slot *slot)
152 int hs_cap;
153 u16 hs_csr;
155 hs_cap = pci_bus_find_capability(slot->bus,
156 slot->devfn,
157 PCI_CAP_ID_CHSWP);
158 if (!hs_cap)
159 return -ENODEV;
160 if (pci_bus_read_config_word(slot->bus,
161 slot->devfn,
162 hs_cap + 2,
163 &hs_csr))
164 return -ENODEV;
165 if (hs_csr & HS_CSR_EXT) {
166 /* Clear EXT (by setting it) */
167 if (pci_bus_write_config_word(slot->bus,
168 slot->devfn,
169 hs_cap + 2,
170 hs_csr))
171 return -ENODEV;
173 return 0;
176 int cpci_led_on(struct slot *slot)
178 int hs_cap;
179 u16 hs_csr;
181 hs_cap = pci_bus_find_capability(slot->bus,
182 slot->devfn,
183 PCI_CAP_ID_CHSWP);
184 if (!hs_cap)
185 return -ENODEV;
186 if (pci_bus_read_config_word(slot->bus,
187 slot->devfn,
188 hs_cap + 2,
189 &hs_csr))
190 return -ENODEV;
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,
194 slot->devfn,
195 hs_cap + 2,
196 hs_csr)) {
197 err("Could not set LOO for slot %s", slot_name(slot));
198 return -ENODEV;
201 return 0;
204 int cpci_led_off(struct slot *slot)
206 int hs_cap;
207 u16 hs_csr;
209 hs_cap = pci_bus_find_capability(slot->bus,
210 slot->devfn,
211 PCI_CAP_ID_CHSWP);
212 if (!hs_cap)
213 return -ENODEV;
214 if (pci_bus_read_config_word(slot->bus,
215 slot->devfn,
216 hs_cap + 2,
217 &hs_csr))
218 return -ENODEV;
219 if (hs_csr & HS_CSR_LOO) {
220 hs_csr &= ~HS_CSR_LOO;
221 if (pci_bus_write_config_word(slot->bus,
222 slot->devfn,
223 hs_cap + 2,
224 hs_csr)) {
225 err("Could not clear LOO for slot %s", slot_name(slot));
226 return -ENODEV;
229 return 0;
234 * Device configuration functions
237 int cpci_configure_slot(struct slot *slot)
239 struct pci_dev *dev;
240 struct pci_bus *parent;
241 int ret = 0;
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) {
255 int n;
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);
267 ret = -ENODEV;
268 goto out;
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);
282 out:
283 pci_unlock_rescan_remove();
284 dbg("%s - exit", __func__);
285 return ret;
288 int cpci_unconfigure_slot(struct slot *slot)
290 struct pci_dev *dev, *temp;
292 dbg("%s - enter", __func__);
293 if (!slot->dev) {
294 err("No device for slot %02x\n", slot->number);
295 return -ENODEV;
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))
302 continue;
303 pci_dev_get(dev);
304 pci_stop_and_remove_bus_device(dev);
305 pci_dev_put(dev);
307 pci_dev_put(slot->dev);
308 slot->dev = NULL;
310 pci_unlock_rescan_remove();
312 dbg("%s - exit", __func__);
313 return 0;