xref: /linux/drivers/pci/hotplug/cpcihp_generic.c (revision 1da177e4c3f41524e886b7f1b8a0c1fc7321cac2)
1*1da177e4SLinus Torvalds /*
2*1da177e4SLinus Torvalds  * cpcihp_generic.c
3*1da177e4SLinus Torvalds  *
4*1da177e4SLinus Torvalds  * Generic port I/O CompactPCI driver
5*1da177e4SLinus Torvalds  *
6*1da177e4SLinus Torvalds  * Copyright 2002 SOMA Networks, Inc.
7*1da177e4SLinus Torvalds  * Copyright 2001 Intel San Luis Obispo
8*1da177e4SLinus Torvalds  * Copyright 2000,2001 MontaVista Software Inc.
9*1da177e4SLinus Torvalds  *
10*1da177e4SLinus Torvalds  * This program is free software; you can redistribute it and/or modify it
11*1da177e4SLinus Torvalds  * under the terms of the GNU General Public License as published by the
12*1da177e4SLinus Torvalds  * Free Software Foundation; either version 2 of the License, or (at your
13*1da177e4SLinus Torvalds  * option) any later version.
14*1da177e4SLinus Torvalds  *
15*1da177e4SLinus Torvalds  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
16*1da177e4SLinus Torvalds  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
17*1da177e4SLinus Torvalds  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
18*1da177e4SLinus Torvalds  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19*1da177e4SLinus Torvalds  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20*1da177e4SLinus Torvalds  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21*1da177e4SLinus Torvalds  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
22*1da177e4SLinus Torvalds  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
23*1da177e4SLinus Torvalds  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24*1da177e4SLinus Torvalds  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25*1da177e4SLinus Torvalds  *
26*1da177e4SLinus Torvalds  * You should have received a copy of the GNU General Public License along
27*1da177e4SLinus Torvalds  * with this program; if not, write to the Free Software Foundation, Inc.,
28*1da177e4SLinus Torvalds  * 675 Mass Ave, Cambridge, MA 02139, USA.
29*1da177e4SLinus Torvalds  *
30*1da177e4SLinus Torvalds  * This generic CompactPCI hotplug driver should allow using the PCI hotplug
31*1da177e4SLinus Torvalds  * mechanism on any CompactPCI board that exposes the #ENUM signal as a bit
32*1da177e4SLinus Torvalds  * in a system register that can be read through standard port I/O.
33*1da177e4SLinus Torvalds  *
34*1da177e4SLinus Torvalds  * Send feedback to <scottm@somanetworks.com>
35*1da177e4SLinus Torvalds  */
36*1da177e4SLinus Torvalds 
37*1da177e4SLinus Torvalds #include <linux/config.h>
38*1da177e4SLinus Torvalds #include <linux/module.h>
39*1da177e4SLinus Torvalds #include <linux/init.h>
40*1da177e4SLinus Torvalds #include <linux/errno.h>
41*1da177e4SLinus Torvalds #include <linux/pci.h>
42*1da177e4SLinus Torvalds #include "cpci_hotplug.h"
43*1da177e4SLinus Torvalds 
44*1da177e4SLinus Torvalds #define DRIVER_VERSION	"0.1"
45*1da177e4SLinus Torvalds #define DRIVER_AUTHOR	"Scott Murray <scottm@somanetworks.com>"
46*1da177e4SLinus Torvalds #define DRIVER_DESC	"Generic port I/O CompactPCI Hot Plug Driver"
47*1da177e4SLinus Torvalds 
48*1da177e4SLinus Torvalds #if !defined(MODULE)
49*1da177e4SLinus Torvalds #define MY_NAME	"cpcihp_generic"
50*1da177e4SLinus Torvalds #else
51*1da177e4SLinus Torvalds #define MY_NAME	THIS_MODULE->name
52*1da177e4SLinus Torvalds #endif
53*1da177e4SLinus Torvalds 
54*1da177e4SLinus Torvalds #define dbg(format, arg...)					\
55*1da177e4SLinus Torvalds 	do {							\
56*1da177e4SLinus Torvalds 		if(debug)					\
57*1da177e4SLinus Torvalds 			printk (KERN_DEBUG "%s: " format "\n",	\
58*1da177e4SLinus Torvalds 				MY_NAME , ## arg); 		\
59*1da177e4SLinus Torvalds 	} while(0)
60*1da177e4SLinus Torvalds #define err(format, arg...) printk(KERN_ERR "%s: " format "\n", MY_NAME , ## arg)
61*1da177e4SLinus Torvalds #define info(format, arg...) printk(KERN_INFO "%s: " format "\n", MY_NAME , ## arg)
62*1da177e4SLinus Torvalds #define warn(format, arg...) printk(KERN_WARNING "%s: " format "\n", MY_NAME , ## arg)
63*1da177e4SLinus Torvalds 
64*1da177e4SLinus Torvalds /* local variables */
65*1da177e4SLinus Torvalds static int debug;
66*1da177e4SLinus Torvalds static char *bridge;
67*1da177e4SLinus Torvalds static u8 bridge_busnr;
68*1da177e4SLinus Torvalds static u8 bridge_slot;
69*1da177e4SLinus Torvalds static struct pci_bus *bus;
70*1da177e4SLinus Torvalds static u8 first_slot;
71*1da177e4SLinus Torvalds static u8 last_slot;
72*1da177e4SLinus Torvalds static u16 port;
73*1da177e4SLinus Torvalds static unsigned int enum_bit;
74*1da177e4SLinus Torvalds static u8 enum_mask;
75*1da177e4SLinus Torvalds 
76*1da177e4SLinus Torvalds static struct cpci_hp_controller_ops generic_hpc_ops;
77*1da177e4SLinus Torvalds static struct cpci_hp_controller generic_hpc;
78*1da177e4SLinus Torvalds 
79*1da177e4SLinus Torvalds static int __init validate_parameters(void)
80*1da177e4SLinus Torvalds {
81*1da177e4SLinus Torvalds 	char* str;
82*1da177e4SLinus Torvalds 	char* p;
83*1da177e4SLinus Torvalds 	unsigned long tmp;
84*1da177e4SLinus Torvalds 
85*1da177e4SLinus Torvalds 	if(!bridge) {
86*1da177e4SLinus Torvalds 		info("not configured, disabling.");
87*1da177e4SLinus Torvalds 		return 1;
88*1da177e4SLinus Torvalds 	}
89*1da177e4SLinus Torvalds 	str = bridge;
90*1da177e4SLinus Torvalds 	if(!*str)
91*1da177e4SLinus Torvalds 		return -EINVAL;
92*1da177e4SLinus Torvalds 
93*1da177e4SLinus Torvalds 	tmp = simple_strtoul(str, &p, 16);
94*1da177e4SLinus Torvalds 	if(p == str || tmp > 0xff) {
95*1da177e4SLinus Torvalds 		err("Invalid hotplug bus bridge device bus number");
96*1da177e4SLinus Torvalds 		return -EINVAL;
97*1da177e4SLinus Torvalds 	}
98*1da177e4SLinus Torvalds 	bridge_busnr = (u8) tmp;
99*1da177e4SLinus Torvalds 	dbg("bridge_busnr = 0x%02x", bridge_busnr);
100*1da177e4SLinus Torvalds 	if(*p != ':') {
101*1da177e4SLinus Torvalds 		err("Invalid hotplug bus bridge device");
102*1da177e4SLinus Torvalds 		return -EINVAL;
103*1da177e4SLinus Torvalds 	}
104*1da177e4SLinus Torvalds 	str = p + 1;
105*1da177e4SLinus Torvalds 	tmp = simple_strtoul(str, &p, 16);
106*1da177e4SLinus Torvalds 	if(p == str || tmp > 0x1f) {
107*1da177e4SLinus Torvalds 		err("Invalid hotplug bus bridge device slot number");
108*1da177e4SLinus Torvalds 		return -EINVAL;
109*1da177e4SLinus Torvalds 	}
110*1da177e4SLinus Torvalds 	bridge_slot = (u8) tmp;
111*1da177e4SLinus Torvalds 	dbg("bridge_slot = 0x%02x", bridge_slot);
112*1da177e4SLinus Torvalds 
113*1da177e4SLinus Torvalds 	dbg("first_slot = 0x%02x", first_slot);
114*1da177e4SLinus Torvalds 	dbg("last_slot = 0x%02x", last_slot);
115*1da177e4SLinus Torvalds 	if(!(first_slot && last_slot)) {
116*1da177e4SLinus Torvalds 		err("Need to specify first_slot and last_slot");
117*1da177e4SLinus Torvalds 		return -EINVAL;
118*1da177e4SLinus Torvalds 	}
119*1da177e4SLinus Torvalds 	if(last_slot < first_slot) {
120*1da177e4SLinus Torvalds 		err("first_slot must be less than last_slot");
121*1da177e4SLinus Torvalds 		return -EINVAL;
122*1da177e4SLinus Torvalds 	}
123*1da177e4SLinus Torvalds 
124*1da177e4SLinus Torvalds 	dbg("port = 0x%04x", port);
125*1da177e4SLinus Torvalds 	dbg("enum_bit = 0x%02x", enum_bit);
126*1da177e4SLinus Torvalds 	if(enum_bit > 7) {
127*1da177e4SLinus Torvalds 		err("Invalid #ENUM bit");
128*1da177e4SLinus Torvalds 		return -EINVAL;
129*1da177e4SLinus Torvalds 	}
130*1da177e4SLinus Torvalds 	enum_mask = 1 << enum_bit;
131*1da177e4SLinus Torvalds 	return 0;
132*1da177e4SLinus Torvalds }
133*1da177e4SLinus Torvalds 
134*1da177e4SLinus Torvalds static int query_enum(void)
135*1da177e4SLinus Torvalds {
136*1da177e4SLinus Torvalds 	u8 value;
137*1da177e4SLinus Torvalds 
138*1da177e4SLinus Torvalds 	value = inb_p(port);
139*1da177e4SLinus Torvalds 	return ((value & enum_mask) == enum_mask);
140*1da177e4SLinus Torvalds }
141*1da177e4SLinus Torvalds 
142*1da177e4SLinus Torvalds static int __init cpcihp_generic_init(void)
143*1da177e4SLinus Torvalds {
144*1da177e4SLinus Torvalds 	int status;
145*1da177e4SLinus Torvalds 	struct resource* r;
146*1da177e4SLinus Torvalds 	struct pci_dev* dev;
147*1da177e4SLinus Torvalds 
148*1da177e4SLinus Torvalds 	info(DRIVER_DESC " version: " DRIVER_VERSION);
149*1da177e4SLinus Torvalds 	status = validate_parameters();
150*1da177e4SLinus Torvalds 	if(status != 0)
151*1da177e4SLinus Torvalds 		return status;
152*1da177e4SLinus Torvalds 
153*1da177e4SLinus Torvalds 	r = request_region(port, 1, "#ENUM hotswap signal register");
154*1da177e4SLinus Torvalds 	if(!r)
155*1da177e4SLinus Torvalds 		return -EBUSY;
156*1da177e4SLinus Torvalds 
157*1da177e4SLinus Torvalds 	dev = pci_find_slot(bridge_busnr, PCI_DEVFN(bridge_slot, 0));
158*1da177e4SLinus Torvalds 	if(!dev || dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
159*1da177e4SLinus Torvalds 		err("Invalid bridge device %s", bridge);
160*1da177e4SLinus Torvalds 		return -EINVAL;
161*1da177e4SLinus Torvalds 	}
162*1da177e4SLinus Torvalds 	bus = dev->subordinate;
163*1da177e4SLinus Torvalds 
164*1da177e4SLinus Torvalds 	memset(&generic_hpc, 0, sizeof (struct cpci_hp_controller));
165*1da177e4SLinus Torvalds 	generic_hpc_ops.query_enum = query_enum;
166*1da177e4SLinus Torvalds 	generic_hpc.ops = &generic_hpc_ops;
167*1da177e4SLinus Torvalds 
168*1da177e4SLinus Torvalds 	status = cpci_hp_register_controller(&generic_hpc);
169*1da177e4SLinus Torvalds 	if(status != 0) {
170*1da177e4SLinus Torvalds 		err("Could not register cPCI hotplug controller");
171*1da177e4SLinus Torvalds 		return -ENODEV;
172*1da177e4SLinus Torvalds 	}
173*1da177e4SLinus Torvalds 	dbg("registered controller");
174*1da177e4SLinus Torvalds 
175*1da177e4SLinus Torvalds 	status = cpci_hp_register_bus(bus, first_slot, last_slot);
176*1da177e4SLinus Torvalds 	if(status != 0) {
177*1da177e4SLinus Torvalds 		err("Could not register cPCI hotplug bus");
178*1da177e4SLinus Torvalds 		goto init_bus_register_error;
179*1da177e4SLinus Torvalds 	}
180*1da177e4SLinus Torvalds 	dbg("registered bus");
181*1da177e4SLinus Torvalds 
182*1da177e4SLinus Torvalds 	status = cpci_hp_start();
183*1da177e4SLinus Torvalds 	if(status != 0) {
184*1da177e4SLinus Torvalds 		err("Could not started cPCI hotplug system");
185*1da177e4SLinus Torvalds 		goto init_start_error;
186*1da177e4SLinus Torvalds 	}
187*1da177e4SLinus Torvalds 	dbg("started cpci hp system");
188*1da177e4SLinus Torvalds 	return 0;
189*1da177e4SLinus Torvalds init_start_error:
190*1da177e4SLinus Torvalds 	cpci_hp_unregister_bus(bus);
191*1da177e4SLinus Torvalds init_bus_register_error:
192*1da177e4SLinus Torvalds 	cpci_hp_unregister_controller(&generic_hpc);
193*1da177e4SLinus Torvalds 	err("status = %d", status);
194*1da177e4SLinus Torvalds 	return status;
195*1da177e4SLinus Torvalds 
196*1da177e4SLinus Torvalds }
197*1da177e4SLinus Torvalds 
198*1da177e4SLinus Torvalds static void __exit cpcihp_generic_exit(void)
199*1da177e4SLinus Torvalds {
200*1da177e4SLinus Torvalds 	cpci_hp_stop();
201*1da177e4SLinus Torvalds 	cpci_hp_unregister_bus(bus);
202*1da177e4SLinus Torvalds 	cpci_hp_unregister_controller(&generic_hpc);
203*1da177e4SLinus Torvalds 	release_region(port, 1);
204*1da177e4SLinus Torvalds }
205*1da177e4SLinus Torvalds 
206*1da177e4SLinus Torvalds module_init(cpcihp_generic_init);
207*1da177e4SLinus Torvalds module_exit(cpcihp_generic_exit);
208*1da177e4SLinus Torvalds 
209*1da177e4SLinus Torvalds MODULE_AUTHOR(DRIVER_AUTHOR);
210*1da177e4SLinus Torvalds MODULE_DESCRIPTION(DRIVER_DESC);
211*1da177e4SLinus Torvalds MODULE_LICENSE("GPL");
212*1da177e4SLinus Torvalds module_param(debug, bool, S_IRUGO | S_IWUSR);
213*1da177e4SLinus Torvalds MODULE_PARM_DESC(debug, "Debugging mode enabled or not");
214*1da177e4SLinus Torvalds module_param(bridge, charp, 0);
215*1da177e4SLinus Torvalds MODULE_PARM_DESC(bridge, "Hotswap bus bridge device, <bus>:<slot> (bus and slot are in hexadecimal)");
216*1da177e4SLinus Torvalds module_param(first_slot, byte, 0);
217*1da177e4SLinus Torvalds MODULE_PARM_DESC(first_slot, "Hotswap bus first slot number");
218*1da177e4SLinus Torvalds module_param(last_slot, byte, 0);
219*1da177e4SLinus Torvalds MODULE_PARM_DESC(last_slot, "Hotswap bus last slot number");
220*1da177e4SLinus Torvalds module_param(port, ushort, 0);
221*1da177e4SLinus Torvalds MODULE_PARM_DESC(port, "#ENUM signal I/O port");
222*1da177e4SLinus Torvalds module_param(enum_bit, uint, 0);
223*1da177e4SLinus Torvalds MODULE_PARM_DESC(enum_bit, "#ENUM signal bit (0-7)");
224