Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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, |
Bjorn Helgaas | f762598 | 2013-11-14 11:28:18 -0700 | [diff] [blame] | 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 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include <linux/module.h> |
| 38 | #include <linux/init.h> |
| 39 | #include <linux/errno.h> |
| 40 | #include <linux/pci.h> |
Tim Schmielau | 4e57b68 | 2005-10-30 15:03:48 -0800 | [diff] [blame] | 41 | #include <linux/string.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | #include "cpci_hotplug.h" |
| 43 | |
| 44 | #define DRIVER_VERSION "0.1" |
| 45 | #define DRIVER_AUTHOR "Scott Murray <scottm@somanetworks.com>" |
| 46 | #define DRIVER_DESC "Generic port I/O CompactPCI Hot Plug Driver" |
| 47 | |
| 48 | #if !defined(MODULE) |
| 49 | #define MY_NAME "cpcihp_generic" |
| 50 | #else |
| 51 | #define MY_NAME THIS_MODULE->name |
| 52 | #endif |
| 53 | |
| 54 | #define dbg(format, arg...) \ |
| 55 | do { \ |
Bjorn Helgaas | f762598 | 2013-11-14 11:28:18 -0700 | [diff] [blame] | 56 | if (debug) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | printk (KERN_DEBUG "%s: " format "\n", \ |
Bjorn Helgaas | f762598 | 2013-11-14 11:28:18 -0700 | [diff] [blame] | 58 | MY_NAME , ## arg); \ |
Quentin Lambert | 382a9c9 | 2014-09-07 20:02:04 +0200 | [diff] [blame] | 59 | } while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | #define err(format, arg...) printk(KERN_ERR "%s: " format "\n", MY_NAME , ## arg) |
| 61 | #define info(format, arg...) printk(KERN_INFO "%s: " format "\n", MY_NAME , ## arg) |
| 62 | #define warn(format, arg...) printk(KERN_WARNING "%s: " format "\n", MY_NAME , ## arg) |
| 63 | |
| 64 | /* local variables */ |
Danny Kukawka | 309c665 | 2012-01-30 23:00:10 +0100 | [diff] [blame] | 65 | static bool debug; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | static char *bridge; |
| 67 | static u8 bridge_busnr; |
| 68 | static u8 bridge_slot; |
| 69 | static struct pci_bus *bus; |
| 70 | static u8 first_slot; |
| 71 | static u8 last_slot; |
| 72 | static u16 port; |
| 73 | static unsigned int enum_bit; |
| 74 | static u8 enum_mask; |
| 75 | |
| 76 | static struct cpci_hp_controller_ops generic_hpc_ops; |
| 77 | static struct cpci_hp_controller generic_hpc; |
| 78 | |
| 79 | static int __init validate_parameters(void) |
| 80 | { |
Ryan Desfosses | 3c78bc6 | 2014-04-18 20:13:49 -0400 | [diff] [blame] | 81 | char *str; |
| 82 | char *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | unsigned long tmp; |
| 84 | |
Quentin Lambert | 382a9c9 | 2014-09-07 20:02:04 +0200 | [diff] [blame] | 85 | if (!bridge) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | info("not configured, disabling."); |
Akinobu Mita | 49c61cc | 2006-10-14 03:07:30 +0900 | [diff] [blame] | 87 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | } |
| 89 | str = bridge; |
Quentin Lambert | 382a9c9 | 2014-09-07 20:02:04 +0200 | [diff] [blame] | 90 | if (!*str) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | return -EINVAL; |
| 92 | |
| 93 | tmp = simple_strtoul(str, &p, 16); |
Quentin Lambert | 382a9c9 | 2014-09-07 20:02:04 +0200 | [diff] [blame] | 94 | if (p == str || tmp > 0xff) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | err("Invalid hotplug bus bridge device bus number"); |
| 96 | return -EINVAL; |
| 97 | } |
| 98 | bridge_busnr = (u8) tmp; |
| 99 | dbg("bridge_busnr = 0x%02x", bridge_busnr); |
Quentin Lambert | 382a9c9 | 2014-09-07 20:02:04 +0200 | [diff] [blame] | 100 | if (*p != ':') { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | err("Invalid hotplug bus bridge device"); |
| 102 | return -EINVAL; |
| 103 | } |
| 104 | str = p + 1; |
| 105 | tmp = simple_strtoul(str, &p, 16); |
Quentin Lambert | 382a9c9 | 2014-09-07 20:02:04 +0200 | [diff] [blame] | 106 | if (p == str || tmp > 0x1f) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | err("Invalid hotplug bus bridge device slot number"); |
| 108 | return -EINVAL; |
| 109 | } |
| 110 | bridge_slot = (u8) tmp; |
| 111 | dbg("bridge_slot = 0x%02x", bridge_slot); |
| 112 | |
| 113 | dbg("first_slot = 0x%02x", first_slot); |
| 114 | dbg("last_slot = 0x%02x", last_slot); |
Quentin Lambert | 382a9c9 | 2014-09-07 20:02:04 +0200 | [diff] [blame] | 115 | if (!(first_slot && last_slot)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | err("Need to specify first_slot and last_slot"); |
| 117 | return -EINVAL; |
| 118 | } |
Quentin Lambert | 382a9c9 | 2014-09-07 20:02:04 +0200 | [diff] [blame] | 119 | if (last_slot < first_slot) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | err("first_slot must be less than last_slot"); |
| 121 | return -EINVAL; |
| 122 | } |
| 123 | |
| 124 | dbg("port = 0x%04x", port); |
| 125 | dbg("enum_bit = 0x%02x", enum_bit); |
Quentin Lambert | 382a9c9 | 2014-09-07 20:02:04 +0200 | [diff] [blame] | 126 | if (enum_bit > 7) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | err("Invalid #ENUM bit"); |
| 128 | return -EINVAL; |
| 129 | } |
| 130 | enum_mask = 1 << enum_bit; |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | static int query_enum(void) |
| 135 | { |
| 136 | u8 value; |
| 137 | |
| 138 | value = inb_p(port); |
| 139 | return ((value & enum_mask) == enum_mask); |
| 140 | } |
| 141 | |
| 142 | static int __init cpcihp_generic_init(void) |
| 143 | { |
| 144 | int status; |
Ryan Desfosses | 3c78bc6 | 2014-04-18 20:13:49 -0400 | [diff] [blame] | 145 | struct resource *r; |
| 146 | struct pci_dev *dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | |
| 148 | info(DRIVER_DESC " version: " DRIVER_VERSION); |
| 149 | status = validate_parameters(); |
Akinobu Mita | 49c61cc | 2006-10-14 03:07:30 +0900 | [diff] [blame] | 150 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | return status; |
| 152 | |
| 153 | r = request_region(port, 1, "#ENUM hotswap signal register"); |
Quentin Lambert | 382a9c9 | 2014-09-07 20:02:04 +0200 | [diff] [blame] | 154 | if (!r) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | return -EBUSY; |
| 156 | |
Jiang Liu | a9d18b3 | 2012-08-28 23:43:56 +0800 | [diff] [blame] | 157 | dev = pci_get_domain_bus_and_slot(0, bridge_busnr, |
| 158 | PCI_DEVFN(bridge_slot, 0)); |
Quentin Lambert | 382a9c9 | 2014-09-07 20:02:04 +0200 | [diff] [blame] | 159 | if (!dev || dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | err("Invalid bridge device %s", bridge); |
Jiri Slaby | 0bf01c3 | 2010-01-20 14:15:54 +0100 | [diff] [blame] | 161 | pci_dev_put(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | return -EINVAL; |
| 163 | } |
| 164 | bus = dev->subordinate; |
Greg Kroah-Hartman | 33ae6ef | 2008-02-13 13:32:24 -0800 | [diff] [blame] | 165 | pci_dev_put(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | |
| 167 | memset(&generic_hpc, 0, sizeof (struct cpci_hp_controller)); |
| 168 | generic_hpc_ops.query_enum = query_enum; |
| 169 | generic_hpc.ops = &generic_hpc_ops; |
| 170 | |
| 171 | status = cpci_hp_register_controller(&generic_hpc); |
Quentin Lambert | 382a9c9 | 2014-09-07 20:02:04 +0200 | [diff] [blame] | 172 | if (status != 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | err("Could not register cPCI hotplug controller"); |
| 174 | return -ENODEV; |
| 175 | } |
| 176 | dbg("registered controller"); |
| 177 | |
| 178 | status = cpci_hp_register_bus(bus, first_slot, last_slot); |
Quentin Lambert | 382a9c9 | 2014-09-07 20:02:04 +0200 | [diff] [blame] | 179 | if (status != 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | err("Could not register cPCI hotplug bus"); |
| 181 | goto init_bus_register_error; |
| 182 | } |
| 183 | dbg("registered bus"); |
| 184 | |
| 185 | status = cpci_hp_start(); |
Quentin Lambert | 382a9c9 | 2014-09-07 20:02:04 +0200 | [diff] [blame] | 186 | if (status != 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | err("Could not started cPCI hotplug system"); |
| 188 | goto init_start_error; |
| 189 | } |
| 190 | dbg("started cpci hp system"); |
| 191 | return 0; |
| 192 | init_start_error: |
| 193 | cpci_hp_unregister_bus(bus); |
| 194 | init_bus_register_error: |
| 195 | cpci_hp_unregister_controller(&generic_hpc); |
| 196 | err("status = %d", status); |
| 197 | return status; |
| 198 | |
| 199 | } |
| 200 | |
| 201 | static void __exit cpcihp_generic_exit(void) |
| 202 | { |
| 203 | cpci_hp_stop(); |
| 204 | cpci_hp_unregister_bus(bus); |
| 205 | cpci_hp_unregister_controller(&generic_hpc); |
| 206 | release_region(port, 1); |
| 207 | } |
| 208 | |
| 209 | module_init(cpcihp_generic_init); |
| 210 | module_exit(cpcihp_generic_exit); |
| 211 | |
| 212 | MODULE_AUTHOR(DRIVER_AUTHOR); |
| 213 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 214 | MODULE_LICENSE("GPL"); |
| 215 | module_param(debug, bool, S_IRUGO | S_IWUSR); |
| 216 | MODULE_PARM_DESC(debug, "Debugging mode enabled or not"); |
| 217 | module_param(bridge, charp, 0); |
| 218 | MODULE_PARM_DESC(bridge, "Hotswap bus bridge device, <bus>:<slot> (bus and slot are in hexadecimal)"); |
| 219 | module_param(first_slot, byte, 0); |
| 220 | MODULE_PARM_DESC(first_slot, "Hotswap bus first slot number"); |
| 221 | module_param(last_slot, byte, 0); |
| 222 | MODULE_PARM_DESC(last_slot, "Hotswap bus last slot number"); |
| 223 | module_param(port, ushort, 0); |
| 224 | MODULE_PARM_DESC(port, "#ENUM signal I/O port"); |
| 225 | module_param(enum_bit, uint, 0); |
| 226 | MODULE_PARM_DESC(enum_bit, "#ENUM signal bit (0-7)"); |