2 * CompactPCI Hot Plug Driver PCI functions
4 * Copyright (C) 2002,2005 by SOMA Networks, Inc.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or (at
11 * your option) any later version.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
16 * NON INFRINGEMENT. See the GNU General Public License for more
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 * Send feedback to <scottm@somanetworks.com>
26 #include <linux/module.h>
27 #include <linux/kernel.h>
28 #include <linux/pci.h>
29 #include <linux/pci_hotplug.h>
30 #include <linux/proc_fs.h>
32 #include "cpci_hotplug.h"
34 #define MY_NAME "cpci_hotplug"
36 extern int cpci_debug
;
38 #define dbg(format, arg...) \
41 printk (KERN_DEBUG "%s: " format "\n", \
44 #define err(format, arg...) printk(KERN_ERR "%s: " format "\n", MY_NAME , ## arg)
45 #define info(format, arg...) printk(KERN_INFO "%s: " format "\n", MY_NAME , ## arg)
46 #define warn(format, arg...) printk(KERN_WARNING "%s: " format "\n", MY_NAME , ## arg)
48 #define ROUND_UP(x, a) (((x) + (a) - 1) & ~((a) - 1))
51 u8
cpci_get_attention_status(struct slot
* slot
)
56 hs_cap
= pci_bus_find_capability(slot
->bus
,
62 if (pci_bus_read_config_word(slot
->bus
,
68 return hs_csr
& 0x0008 ? 1 : 0;
71 int cpci_set_attention_status(struct slot
* slot
, int status
)
76 hs_cap
= pci_bus_find_capability(slot
->bus
,
81 if (pci_bus_read_config_word(slot
->bus
,
89 hs_csr
&= ~HS_CSR_LOO
;
90 if (pci_bus_write_config_word(slot
->bus
,
98 u16
cpci_get_hs_csr(struct slot
* slot
)
103 hs_cap
= pci_bus_find_capability(slot
->bus
,
108 if (pci_bus_read_config_word(slot
->bus
,
116 int cpci_check_and_clear_ins(struct slot
* slot
)
122 hs_cap
= pci_bus_find_capability(slot
->bus
,
127 if (pci_bus_read_config_word(slot
->bus
,
132 if (hs_csr
& HS_CSR_INS
) {
133 /* Clear INS (by setting it) */
134 if (pci_bus_write_config_word(slot
->bus
,
145 int cpci_check_ext(struct slot
* slot
)
151 hs_cap
= pci_bus_find_capability(slot
->bus
,
156 if (pci_bus_read_config_word(slot
->bus
,
161 if (hs_csr
& HS_CSR_EXT
)
166 int cpci_clear_ext(struct slot
* slot
)
171 hs_cap
= pci_bus_find_capability(slot
->bus
,
176 if (pci_bus_read_config_word(slot
->bus
,
181 if (hs_csr
& HS_CSR_EXT
) {
182 /* Clear EXT (by setting it) */
183 if (pci_bus_write_config_word(slot
->bus
,
192 int cpci_led_on(struct slot
* slot
)
197 hs_cap
= pci_bus_find_capability(slot
->bus
,
202 if (pci_bus_read_config_word(slot
->bus
,
207 if ((hs_csr
& HS_CSR_LOO
) != HS_CSR_LOO
) {
208 hs_csr
|= HS_CSR_LOO
;
209 if (pci_bus_write_config_word(slot
->bus
,
213 err("Could not set LOO for slot %s",
214 slot
->hotplug_slot
->name
);
221 int cpci_led_off(struct slot
* slot
)
226 hs_cap
= pci_bus_find_capability(slot
->bus
,
231 if (pci_bus_read_config_word(slot
->bus
,
236 if (hs_csr
& HS_CSR_LOO
) {
237 hs_csr
&= ~HS_CSR_LOO
;
238 if (pci_bus_write_config_word(slot
->bus
,
242 err("Could not clear LOO for slot %s",
243 slot
->hotplug_slot
->name
);
252 * Device configuration functions
255 int cpci_configure_slot(struct slot
* slot
)
257 struct pci_bus
*parent
;
260 dbg("%s - enter", __FUNCTION__
);
262 if (slot
->dev
== NULL
) {
263 dbg("pci_dev null, finding %02x:%02x:%x",
264 slot
->bus
->number
, PCI_SLOT(slot
->devfn
), PCI_FUNC(slot
->devfn
));
265 slot
->dev
= pci_get_slot(slot
->bus
, slot
->devfn
);
268 /* Still NULL? Well then scan for it! */
269 if (slot
->dev
== NULL
) {
271 dbg("pci_dev still null");
274 * This will generate pci_dev structures for all functions, but
275 * we will only call this case when lookup fails.
277 n
= pci_scan_slot(slot
->bus
, slot
->devfn
);
278 dbg("%s: pci_scan_slot returned %d", __FUNCTION__
, n
);
279 slot
->dev
= pci_get_slot(slot
->bus
, slot
->devfn
);
280 if (slot
->dev
== NULL
) {
281 err("Could not find PCI device for slot %02x", slot
->number
);
285 parent
= slot
->dev
->bus
;
287 for (fn
= 0; fn
< 8; fn
++) {
290 dev
= pci_get_slot(parent
, PCI_DEVFN(PCI_SLOT(slot
->devfn
), fn
));
293 if ((dev
->hdr_type
== PCI_HEADER_TYPE_BRIDGE
) ||
294 (dev
->hdr_type
== PCI_HEADER_TYPE_CARDBUS
)) {
295 /* Find an unused bus number for the new bridge */
296 struct pci_bus
*child
;
297 unsigned char busnr
, start
= parent
->secondary
;
298 unsigned char end
= parent
->subordinate
;
300 for (busnr
= start
; busnr
<= end
; busnr
++) {
301 if (!pci_find_bus(pci_domain_nr(parent
),
306 err("No free bus for hot-added bridge\n");
310 child
= pci_add_new_bus(parent
, dev
, busnr
);
312 err("Cannot add new bus for %s\n",
317 child
->subordinate
= pci_do_scan_bus(child
);
318 pci_bus_size_bridges(child
);
323 pci_bus_assign_resources(parent
);
324 pci_bus_add_devices(parent
);
325 pci_enable_bridges(parent
);
327 dbg("%s - exit", __FUNCTION__
);
331 int cpci_unconfigure_slot(struct slot
* slot
)
336 dbg("%s - enter", __FUNCTION__
);
338 err("No device for slot %02x\n", slot
->number
);
342 for (i
= 0; i
< 8; i
++) {
343 dev
= pci_get_slot(slot
->bus
,
344 PCI_DEVFN(PCI_SLOT(slot
->devfn
), i
));
346 pci_remove_bus_device(dev
);
350 pci_dev_put(slot
->dev
);
353 dbg("%s - exit", __FUNCTION__
);