xref: /linux/drivers/pci/hotplug/cpcihp_generic.c (revision 49c61cca2b6591a28ffa4abb73c718091f569746)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * cpcihp_generic.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Generic port I/O CompactPCI driver
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  * Copyright 2002 SOMA Networks, Inc.
71da177e4SLinus Torvalds  * Copyright 2001 Intel San Luis Obispo
81da177e4SLinus Torvalds  * Copyright 2000,2001 MontaVista Software Inc.
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  * This program is free software; you can redistribute it and/or modify it
111da177e4SLinus Torvalds  * under the terms of the GNU General Public License as published by the
121da177e4SLinus Torvalds  * Free Software Foundation; either version 2 of the License, or (at your
131da177e4SLinus Torvalds  * option) any later version.
141da177e4SLinus Torvalds  *
151da177e4SLinus Torvalds  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
161da177e4SLinus Torvalds  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
171da177e4SLinus Torvalds  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
181da177e4SLinus Torvalds  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
191da177e4SLinus Torvalds  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
201da177e4SLinus Torvalds  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
211da177e4SLinus Torvalds  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
221da177e4SLinus Torvalds  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
231da177e4SLinus Torvalds  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
241da177e4SLinus Torvalds  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
251da177e4SLinus Torvalds  *
261da177e4SLinus Torvalds  * You should have received a copy of the GNU General Public License along
271da177e4SLinus Torvalds  * with this program; if not, write to the Free Software Foundation, Inc.,
281da177e4SLinus Torvalds  * 675 Mass Ave, Cambridge, MA 02139, USA.
291da177e4SLinus Torvalds  *
301da177e4SLinus Torvalds  * This generic CompactPCI hotplug driver should allow using the PCI hotplug
311da177e4SLinus Torvalds  * mechanism on any CompactPCI board that exposes the #ENUM signal as a bit
321da177e4SLinus Torvalds  * in a system register that can be read through standard port I/O.
331da177e4SLinus Torvalds  *
341da177e4SLinus Torvalds  * Send feedback to <scottm@somanetworks.com>
351da177e4SLinus Torvalds  */
361da177e4SLinus Torvalds 
371da177e4SLinus Torvalds #include <linux/module.h>
381da177e4SLinus Torvalds #include <linux/init.h>
391da177e4SLinus Torvalds #include <linux/errno.h>
401da177e4SLinus Torvalds #include <linux/pci.h>
414e57b681STim Schmielau #include <linux/string.h>
421da177e4SLinus Torvalds #include "cpci_hotplug.h"
431da177e4SLinus Torvalds 
441da177e4SLinus Torvalds #define DRIVER_VERSION	"0.1"
451da177e4SLinus Torvalds #define DRIVER_AUTHOR	"Scott Murray <scottm@somanetworks.com>"
461da177e4SLinus Torvalds #define DRIVER_DESC	"Generic port I/O CompactPCI Hot Plug Driver"
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds #if !defined(MODULE)
491da177e4SLinus Torvalds #define MY_NAME	"cpcihp_generic"
501da177e4SLinus Torvalds #else
511da177e4SLinus Torvalds #define MY_NAME	THIS_MODULE->name
521da177e4SLinus Torvalds #endif
531da177e4SLinus Torvalds 
541da177e4SLinus Torvalds #define dbg(format, arg...)					\
551da177e4SLinus Torvalds 	do {							\
561da177e4SLinus Torvalds 		if(debug)					\
571da177e4SLinus Torvalds 			printk (KERN_DEBUG "%s: " format "\n",	\
581da177e4SLinus Torvalds 				MY_NAME , ## arg); 		\
591da177e4SLinus Torvalds 	} while(0)
601da177e4SLinus Torvalds #define err(format, arg...) printk(KERN_ERR "%s: " format "\n", MY_NAME , ## arg)
611da177e4SLinus Torvalds #define info(format, arg...) printk(KERN_INFO "%s: " format "\n", MY_NAME , ## arg)
621da177e4SLinus Torvalds #define warn(format, arg...) printk(KERN_WARNING "%s: " format "\n", MY_NAME , ## arg)
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds /* local variables */
651da177e4SLinus Torvalds static int debug;
661da177e4SLinus Torvalds static char *bridge;
671da177e4SLinus Torvalds static u8 bridge_busnr;
681da177e4SLinus Torvalds static u8 bridge_slot;
691da177e4SLinus Torvalds static struct pci_bus *bus;
701da177e4SLinus Torvalds static u8 first_slot;
711da177e4SLinus Torvalds static u8 last_slot;
721da177e4SLinus Torvalds static u16 port;
731da177e4SLinus Torvalds static unsigned int enum_bit;
741da177e4SLinus Torvalds static u8 enum_mask;
751da177e4SLinus Torvalds 
761da177e4SLinus Torvalds static struct cpci_hp_controller_ops generic_hpc_ops;
771da177e4SLinus Torvalds static struct cpci_hp_controller generic_hpc;
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds static int __init validate_parameters(void)
801da177e4SLinus Torvalds {
811da177e4SLinus Torvalds 	char* str;
821da177e4SLinus Torvalds 	char* p;
831da177e4SLinus Torvalds 	unsigned long tmp;
841da177e4SLinus Torvalds 
851da177e4SLinus Torvalds 	if(!bridge) {
861da177e4SLinus Torvalds 		info("not configured, disabling.");
87*49c61ccaSAkinobu Mita 		return -EINVAL;
881da177e4SLinus Torvalds 	}
891da177e4SLinus Torvalds 	str = bridge;
901da177e4SLinus Torvalds 	if(!*str)
911da177e4SLinus Torvalds 		return -EINVAL;
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds 	tmp = simple_strtoul(str, &p, 16);
941da177e4SLinus Torvalds 	if(p == str || tmp > 0xff) {
951da177e4SLinus Torvalds 		err("Invalid hotplug bus bridge device bus number");
961da177e4SLinus Torvalds 		return -EINVAL;
971da177e4SLinus Torvalds 	}
981da177e4SLinus Torvalds 	bridge_busnr = (u8) tmp;
991da177e4SLinus Torvalds 	dbg("bridge_busnr = 0x%02x", bridge_busnr);
1001da177e4SLinus Torvalds 	if(*p != ':') {
1011da177e4SLinus Torvalds 		err("Invalid hotplug bus bridge device");
1021da177e4SLinus Torvalds 		return -EINVAL;
1031da177e4SLinus Torvalds 	}
1041da177e4SLinus Torvalds 	str = p + 1;
1051da177e4SLinus Torvalds 	tmp = simple_strtoul(str, &p, 16);
1061da177e4SLinus Torvalds 	if(p == str || tmp > 0x1f) {
1071da177e4SLinus Torvalds 		err("Invalid hotplug bus bridge device slot number");
1081da177e4SLinus Torvalds 		return -EINVAL;
1091da177e4SLinus Torvalds 	}
1101da177e4SLinus Torvalds 	bridge_slot = (u8) tmp;
1111da177e4SLinus Torvalds 	dbg("bridge_slot = 0x%02x", bridge_slot);
1121da177e4SLinus Torvalds 
1131da177e4SLinus Torvalds 	dbg("first_slot = 0x%02x", first_slot);
1141da177e4SLinus Torvalds 	dbg("last_slot = 0x%02x", last_slot);
1151da177e4SLinus Torvalds 	if(!(first_slot && last_slot)) {
1161da177e4SLinus Torvalds 		err("Need to specify first_slot and last_slot");
1171da177e4SLinus Torvalds 		return -EINVAL;
1181da177e4SLinus Torvalds 	}
1191da177e4SLinus Torvalds 	if(last_slot < first_slot) {
1201da177e4SLinus Torvalds 		err("first_slot must be less than last_slot");
1211da177e4SLinus Torvalds 		return -EINVAL;
1221da177e4SLinus Torvalds 	}
1231da177e4SLinus Torvalds 
1241da177e4SLinus Torvalds 	dbg("port = 0x%04x", port);
1251da177e4SLinus Torvalds 	dbg("enum_bit = 0x%02x", enum_bit);
1261da177e4SLinus Torvalds 	if(enum_bit > 7) {
1271da177e4SLinus Torvalds 		err("Invalid #ENUM bit");
1281da177e4SLinus Torvalds 		return -EINVAL;
1291da177e4SLinus Torvalds 	}
1301da177e4SLinus Torvalds 	enum_mask = 1 << enum_bit;
1311da177e4SLinus Torvalds 	return 0;
1321da177e4SLinus Torvalds }
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds static int query_enum(void)
1351da177e4SLinus Torvalds {
1361da177e4SLinus Torvalds 	u8 value;
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds 	value = inb_p(port);
1391da177e4SLinus Torvalds 	return ((value & enum_mask) == enum_mask);
1401da177e4SLinus Torvalds }
1411da177e4SLinus Torvalds 
1421da177e4SLinus Torvalds static int __init cpcihp_generic_init(void)
1431da177e4SLinus Torvalds {
1441da177e4SLinus Torvalds 	int status;
1451da177e4SLinus Torvalds 	struct resource* r;
1461da177e4SLinus Torvalds 	struct pci_dev* dev;
1471da177e4SLinus Torvalds 
1481da177e4SLinus Torvalds 	info(DRIVER_DESC " version: " DRIVER_VERSION);
1491da177e4SLinus Torvalds 	status = validate_parameters();
150*49c61ccaSAkinobu Mita 	if (status)
1511da177e4SLinus Torvalds 		return status;
1521da177e4SLinus Torvalds 
1531da177e4SLinus Torvalds 	r = request_region(port, 1, "#ENUM hotswap signal register");
1541da177e4SLinus Torvalds 	if(!r)
1551da177e4SLinus Torvalds 		return -EBUSY;
1561da177e4SLinus Torvalds 
1571da177e4SLinus Torvalds 	dev = pci_find_slot(bridge_busnr, PCI_DEVFN(bridge_slot, 0));
1581da177e4SLinus Torvalds 	if(!dev || dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
1591da177e4SLinus Torvalds 		err("Invalid bridge device %s", bridge);
1601da177e4SLinus Torvalds 		return -EINVAL;
1611da177e4SLinus Torvalds 	}
1621da177e4SLinus Torvalds 	bus = dev->subordinate;
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds 	memset(&generic_hpc, 0, sizeof (struct cpci_hp_controller));
1651da177e4SLinus Torvalds 	generic_hpc_ops.query_enum = query_enum;
1661da177e4SLinus Torvalds 	generic_hpc.ops = &generic_hpc_ops;
1671da177e4SLinus Torvalds 
1681da177e4SLinus Torvalds 	status = cpci_hp_register_controller(&generic_hpc);
1691da177e4SLinus Torvalds 	if(status != 0) {
1701da177e4SLinus Torvalds 		err("Could not register cPCI hotplug controller");
1711da177e4SLinus Torvalds 		return -ENODEV;
1721da177e4SLinus Torvalds 	}
1731da177e4SLinus Torvalds 	dbg("registered controller");
1741da177e4SLinus Torvalds 
1751da177e4SLinus Torvalds 	status = cpci_hp_register_bus(bus, first_slot, last_slot);
1761da177e4SLinus Torvalds 	if(status != 0) {
1771da177e4SLinus Torvalds 		err("Could not register cPCI hotplug bus");
1781da177e4SLinus Torvalds 		goto init_bus_register_error;
1791da177e4SLinus Torvalds 	}
1801da177e4SLinus Torvalds 	dbg("registered bus");
1811da177e4SLinus Torvalds 
1821da177e4SLinus Torvalds 	status = cpci_hp_start();
1831da177e4SLinus Torvalds 	if(status != 0) {
1841da177e4SLinus Torvalds 		err("Could not started cPCI hotplug system");
1851da177e4SLinus Torvalds 		goto init_start_error;
1861da177e4SLinus Torvalds 	}
1871da177e4SLinus Torvalds 	dbg("started cpci hp system");
1881da177e4SLinus Torvalds 	return 0;
1891da177e4SLinus Torvalds init_start_error:
1901da177e4SLinus Torvalds 	cpci_hp_unregister_bus(bus);
1911da177e4SLinus Torvalds init_bus_register_error:
1921da177e4SLinus Torvalds 	cpci_hp_unregister_controller(&generic_hpc);
1931da177e4SLinus Torvalds 	err("status = %d", status);
1941da177e4SLinus Torvalds 	return status;
1951da177e4SLinus Torvalds 
1961da177e4SLinus Torvalds }
1971da177e4SLinus Torvalds 
1981da177e4SLinus Torvalds static void __exit cpcihp_generic_exit(void)
1991da177e4SLinus Torvalds {
2001da177e4SLinus Torvalds 	cpci_hp_stop();
2011da177e4SLinus Torvalds 	cpci_hp_unregister_bus(bus);
2021da177e4SLinus Torvalds 	cpci_hp_unregister_controller(&generic_hpc);
2031da177e4SLinus Torvalds 	release_region(port, 1);
2041da177e4SLinus Torvalds }
2051da177e4SLinus Torvalds 
2061da177e4SLinus Torvalds module_init(cpcihp_generic_init);
2071da177e4SLinus Torvalds module_exit(cpcihp_generic_exit);
2081da177e4SLinus Torvalds 
2091da177e4SLinus Torvalds MODULE_AUTHOR(DRIVER_AUTHOR);
2101da177e4SLinus Torvalds MODULE_DESCRIPTION(DRIVER_DESC);
2111da177e4SLinus Torvalds MODULE_LICENSE("GPL");
2121da177e4SLinus Torvalds module_param(debug, bool, S_IRUGO | S_IWUSR);
2131da177e4SLinus Torvalds MODULE_PARM_DESC(debug, "Debugging mode enabled or not");
2141da177e4SLinus Torvalds module_param(bridge, charp, 0);
2151da177e4SLinus Torvalds MODULE_PARM_DESC(bridge, "Hotswap bus bridge device, <bus>:<slot> (bus and slot are in hexadecimal)");
2161da177e4SLinus Torvalds module_param(first_slot, byte, 0);
2171da177e4SLinus Torvalds MODULE_PARM_DESC(first_slot, "Hotswap bus first slot number");
2181da177e4SLinus Torvalds module_param(last_slot, byte, 0);
2191da177e4SLinus Torvalds MODULE_PARM_DESC(last_slot, "Hotswap bus last slot number");
2201da177e4SLinus Torvalds module_param(port, ushort, 0);
2211da177e4SLinus Torvalds MODULE_PARM_DESC(port, "#ENUM signal I/O port");
2221da177e4SLinus Torvalds module_param(enum_bit, uint, 0);
2231da177e4SLinus Torvalds MODULE_PARM_DESC(enum_bit, "#ENUM signal bit (0-7)");
224