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