xref: /linux/drivers/pci/hotplug/cpcihp_generic.c (revision 552c69b36ebd966186573b9c7a286b390935cce1)
1*736759efSBjorn Helgaas // SPDX-License-Identifier: GPL-2.0+
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * cpcihp_generic.c
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * Generic port I/O CompactPCI driver
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  * Copyright 2002 SOMA Networks, Inc.
81da177e4SLinus Torvalds  * Copyright 2001 Intel San Luis Obispo
91da177e4SLinus Torvalds  * Copyright 2000,2001 MontaVista Software Inc.
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  * This generic CompactPCI hotplug driver should allow using the PCI hotplug
121da177e4SLinus Torvalds  * mechanism on any CompactPCI board that exposes the #ENUM signal as a bit
131da177e4SLinus Torvalds  * in a system register that can be read through standard port I/O.
141da177e4SLinus Torvalds  *
151da177e4SLinus Torvalds  * Send feedback to <scottm@somanetworks.com>
161da177e4SLinus Torvalds  */
171da177e4SLinus Torvalds 
181da177e4SLinus Torvalds #include <linux/module.h>
191da177e4SLinus Torvalds #include <linux/init.h>
201da177e4SLinus Torvalds #include <linux/errno.h>
211da177e4SLinus Torvalds #include <linux/pci.h>
224e57b681STim Schmielau #include <linux/string.h>
231da177e4SLinus Torvalds #include "cpci_hotplug.h"
241da177e4SLinus Torvalds 
251da177e4SLinus Torvalds #define DRIVER_VERSION	"0.1"
261da177e4SLinus Torvalds #define DRIVER_AUTHOR	"Scott Murray <scottm@somanetworks.com>"
271da177e4SLinus Torvalds #define DRIVER_DESC	"Generic port I/O CompactPCI Hot Plug Driver"
281da177e4SLinus Torvalds 
291da177e4SLinus Torvalds #if !defined(MODULE)
301da177e4SLinus Torvalds #define MY_NAME	"cpcihp_generic"
311da177e4SLinus Torvalds #else
321da177e4SLinus Torvalds #define MY_NAME	THIS_MODULE->name
331da177e4SLinus Torvalds #endif
341da177e4SLinus Torvalds 
351da177e4SLinus Torvalds #define dbg(format, arg...)					\
361da177e4SLinus Torvalds 	do {							\
371da177e4SLinus Torvalds 		if (debug)					\
381da177e4SLinus Torvalds 			printk(KERN_DEBUG "%s: " format "\n",	\
391da177e4SLinus Torvalds 				MY_NAME, ## arg);		\
401da177e4SLinus Torvalds 	} while (0)
411da177e4SLinus Torvalds #define err(format, arg...) printk(KERN_ERR "%s: " format "\n", MY_NAME, ## arg)
421da177e4SLinus Torvalds #define info(format, arg...) printk(KERN_INFO "%s: " format "\n", MY_NAME, ## arg)
431da177e4SLinus Torvalds #define warn(format, arg...) printk(KERN_WARNING "%s: " format "\n", MY_NAME, ## arg)
441da177e4SLinus Torvalds 
451da177e4SLinus Torvalds /* local variables */
46309c6651SDanny Kukawka static bool debug;
471da177e4SLinus Torvalds static char *bridge;
481da177e4SLinus Torvalds static u8 bridge_busnr;
491da177e4SLinus Torvalds static u8 bridge_slot;
501da177e4SLinus Torvalds static struct pci_bus *bus;
511da177e4SLinus Torvalds static u8 first_slot;
521da177e4SLinus Torvalds static u8 last_slot;
531da177e4SLinus Torvalds static u16 port;
541da177e4SLinus Torvalds static unsigned int enum_bit;
551da177e4SLinus Torvalds static u8 enum_mask;
561da177e4SLinus Torvalds 
571da177e4SLinus Torvalds static struct cpci_hp_controller_ops generic_hpc_ops;
581da177e4SLinus Torvalds static struct cpci_hp_controller generic_hpc;
591da177e4SLinus Torvalds 
validate_parameters(void)601da177e4SLinus Torvalds static int __init validate_parameters(void)
611da177e4SLinus Torvalds {
621da177e4SLinus Torvalds 	char *str;
631da177e4SLinus Torvalds 	char *p;
641da177e4SLinus Torvalds 	unsigned long tmp;
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds 	if (!bridge) {
671da177e4SLinus Torvalds 		info("not configured, disabling.");
6849c61ccaSAkinobu Mita 		return -EINVAL;
691da177e4SLinus Torvalds 	}
701da177e4SLinus Torvalds 	str = bridge;
711da177e4SLinus Torvalds 	if (!*str)
721da177e4SLinus Torvalds 		return -EINVAL;
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds 	tmp = simple_strtoul(str, &p, 16);
751da177e4SLinus Torvalds 	if (p == str || tmp > 0xff) {
761da177e4SLinus Torvalds 		err("Invalid hotplug bus bridge device bus number");
771da177e4SLinus Torvalds 		return -EINVAL;
781da177e4SLinus Torvalds 	}
791da177e4SLinus Torvalds 	bridge_busnr = (u8) tmp;
801da177e4SLinus Torvalds 	dbg("bridge_busnr = 0x%02x", bridge_busnr);
811da177e4SLinus Torvalds 	if (*p != ':') {
821da177e4SLinus Torvalds 		err("Invalid hotplug bus bridge device");
831da177e4SLinus Torvalds 		return -EINVAL;
841da177e4SLinus Torvalds 	}
851da177e4SLinus Torvalds 	str = p + 1;
861da177e4SLinus Torvalds 	tmp = simple_strtoul(str, &p, 16);
871da177e4SLinus Torvalds 	if (p == str || tmp > 0x1f) {
881da177e4SLinus Torvalds 		err("Invalid hotplug bus bridge device slot number");
891da177e4SLinus Torvalds 		return -EINVAL;
901da177e4SLinus Torvalds 	}
911da177e4SLinus Torvalds 	bridge_slot = (u8) tmp;
921da177e4SLinus Torvalds 	dbg("bridge_slot = 0x%02x", bridge_slot);
931da177e4SLinus Torvalds 
941da177e4SLinus Torvalds 	dbg("first_slot = 0x%02x", first_slot);
951da177e4SLinus Torvalds 	dbg("last_slot = 0x%02x", last_slot);
961da177e4SLinus Torvalds 	if (!(first_slot && last_slot)) {
971da177e4SLinus Torvalds 		err("Need to specify first_slot and last_slot");
981da177e4SLinus Torvalds 		return -EINVAL;
991da177e4SLinus Torvalds 	}
1001da177e4SLinus Torvalds 	if (last_slot < first_slot) {
1011da177e4SLinus Torvalds 		err("first_slot must be less than last_slot");
1021da177e4SLinus Torvalds 		return -EINVAL;
1031da177e4SLinus Torvalds 	}
1041da177e4SLinus Torvalds 
1051da177e4SLinus Torvalds 	dbg("port = 0x%04x", port);
1061da177e4SLinus Torvalds 	dbg("enum_bit = 0x%02x", enum_bit);
1071da177e4SLinus Torvalds 	if (enum_bit > 7) {
1081da177e4SLinus Torvalds 		err("Invalid #ENUM bit");
1091da177e4SLinus Torvalds 		return -EINVAL;
1101da177e4SLinus Torvalds 	}
1111da177e4SLinus Torvalds 	enum_mask = 1 << enum_bit;
1121da177e4SLinus Torvalds 	return 0;
1131da177e4SLinus Torvalds }
1141da177e4SLinus Torvalds 
query_enum(void)1151da177e4SLinus Torvalds static int query_enum(void)
1161da177e4SLinus Torvalds {
1171da177e4SLinus Torvalds 	u8 value;
1181da177e4SLinus Torvalds 
1191da177e4SLinus Torvalds 	value = inb_p(port);
1201da177e4SLinus Torvalds 	return ((value & enum_mask) == enum_mask);
1211da177e4SLinus Torvalds }
1221da177e4SLinus Torvalds 
cpcihp_generic_init(void)1231da177e4SLinus Torvalds static int __init cpcihp_generic_init(void)
1241da177e4SLinus Torvalds {
1251da177e4SLinus Torvalds 	int status;
1261da177e4SLinus Torvalds 	struct resource *r;
1271da177e4SLinus Torvalds 	struct pci_dev *dev;
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds 	info(DRIVER_DESC " version: " DRIVER_VERSION);
1301da177e4SLinus Torvalds 	status = validate_parameters();
13149c61ccaSAkinobu Mita 	if (status)
1321da177e4SLinus Torvalds 		return status;
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds 	r = request_region(port, 1, "#ENUM hotswap signal register");
1351da177e4SLinus Torvalds 	if (!r)
1361da177e4SLinus Torvalds 		return -EBUSY;
1371da177e4SLinus Torvalds 
138a9d18b37SJiang Liu 	dev = pci_get_domain_bus_and_slot(0, bridge_busnr,
139a9d18b37SJiang Liu 					  PCI_DEVFN(bridge_slot, 0));
1401da177e4SLinus Torvalds 	if (!dev || dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
1411da177e4SLinus Torvalds 		err("Invalid bridge device %s", bridge);
1420bf01c3cSJiri Slaby 		pci_dev_put(dev);
1431da177e4SLinus Torvalds 		return -EINVAL;
1441da177e4SLinus Torvalds 	}
1451da177e4SLinus Torvalds 	bus = dev->subordinate;
14633ae6ef2SGreg Kroah-Hartman 	pci_dev_put(dev);
1471da177e4SLinus Torvalds 
1481da177e4SLinus Torvalds 	memset(&generic_hpc, 0, sizeof(struct cpci_hp_controller));
1491da177e4SLinus Torvalds 	generic_hpc_ops.query_enum = query_enum;
1501da177e4SLinus Torvalds 	generic_hpc.ops = &generic_hpc_ops;
1511da177e4SLinus Torvalds 
1521da177e4SLinus Torvalds 	status = cpci_hp_register_controller(&generic_hpc);
1531da177e4SLinus Torvalds 	if (status != 0) {
1541da177e4SLinus Torvalds 		err("Could not register cPCI hotplug controller");
1551da177e4SLinus Torvalds 		return -ENODEV;
1561da177e4SLinus Torvalds 	}
1571da177e4SLinus Torvalds 	dbg("registered controller");
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds 	status = cpci_hp_register_bus(bus, first_slot, last_slot);
1601da177e4SLinus Torvalds 	if (status != 0) {
1611da177e4SLinus Torvalds 		err("Could not register cPCI hotplug bus");
1621da177e4SLinus Torvalds 		goto init_bus_register_error;
1631da177e4SLinus Torvalds 	}
1641da177e4SLinus Torvalds 	dbg("registered bus");
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds 	status = cpci_hp_start();
1671da177e4SLinus Torvalds 	if (status != 0) {
1681da177e4SLinus Torvalds 		err("Could not started cPCI hotplug system");
1691da177e4SLinus Torvalds 		goto init_start_error;
1701da177e4SLinus Torvalds 	}
1711da177e4SLinus Torvalds 	dbg("started cpci hp system");
1721da177e4SLinus Torvalds 	return 0;
1731da177e4SLinus Torvalds init_start_error:
1741da177e4SLinus Torvalds 	cpci_hp_unregister_bus(bus);
1751da177e4SLinus Torvalds init_bus_register_error:
1761da177e4SLinus Torvalds 	cpci_hp_unregister_controller(&generic_hpc);
1771da177e4SLinus Torvalds 	err("status = %d", status);
1781da177e4SLinus Torvalds 	return status;
1791da177e4SLinus Torvalds 
1801da177e4SLinus Torvalds }
1811da177e4SLinus Torvalds 
cpcihp_generic_exit(void)1821da177e4SLinus Torvalds static void __exit cpcihp_generic_exit(void)
1831da177e4SLinus Torvalds {
1841da177e4SLinus Torvalds 	cpci_hp_stop();
1851da177e4SLinus Torvalds 	cpci_hp_unregister_bus(bus);
1861da177e4SLinus Torvalds 	cpci_hp_unregister_controller(&generic_hpc);
1871da177e4SLinus Torvalds 	release_region(port, 1);
1881da177e4SLinus Torvalds }
1891da177e4SLinus Torvalds 
1901da177e4SLinus Torvalds module_init(cpcihp_generic_init);
1911da177e4SLinus Torvalds module_exit(cpcihp_generic_exit);
1921da177e4SLinus Torvalds 
1931da177e4SLinus Torvalds MODULE_AUTHOR(DRIVER_AUTHOR);
1941da177e4SLinus Torvalds MODULE_DESCRIPTION(DRIVER_DESC);
1951da177e4SLinus Torvalds MODULE_LICENSE("GPL");
1961da177e4SLinus Torvalds module_param(debug, bool, S_IRUGO | S_IWUSR);
1971da177e4SLinus Torvalds MODULE_PARM_DESC(debug, "Debugging mode enabled or not");
1981da177e4SLinus Torvalds module_param(bridge, charp, 0);
1991da177e4SLinus Torvalds MODULE_PARM_DESC(bridge, "Hotswap bus bridge device, <bus>:<slot> (bus and slot are in hexadecimal)");
2001da177e4SLinus Torvalds module_param(first_slot, byte, 0);
2011da177e4SLinus Torvalds MODULE_PARM_DESC(first_slot, "Hotswap bus first slot number");
2021da177e4SLinus Torvalds module_param(last_slot, byte, 0);
2031da177e4SLinus Torvalds MODULE_PARM_DESC(last_slot, "Hotswap bus last slot number");
20401b961b7SDavid Howells module_param_hw(port, ushort, ioport, 0);
2051da177e4SLinus Torvalds MODULE_PARM_DESC(port, "#ENUM signal I/O port");
2061da177e4SLinus Torvalds module_param(enum_bit, uint, 0);
2071da177e4SLinus Torvalds MODULE_PARM_DESC(enum_bit, "#ENUM signal bit (0-7)");
208