Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Interface for Dynamic Logical Partitioning of I/O Slots on |
| 3 | * RPA-compliant PPC64 platform. |
| 4 | * |
| 5 | * John Rose <johnrose@austin.ibm.com> |
| 6 | * Linda Xie <lxie@us.ibm.com> |
| 7 | * |
| 8 | * October 2003 |
| 9 | * |
| 10 | * Copyright (C) 2003 IBM. |
| 11 | * |
| 12 | * This program is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU General Public License |
| 14 | * as published by the Free Software Foundation; either version |
| 15 | * 2 of the License, or (at your option) any later version. |
| 16 | */ |
Benjamin Herrenschmidt | fd6852c | 2008-10-27 19:48:52 +0000 | [diff] [blame] | 17 | |
| 18 | #undef DEBUG |
| 19 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/init.h> |
Paul Gortmaker | eefa9cf | 2011-05-27 09:42:30 -0400 | [diff] [blame] | 21 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/pci.h> |
Tim Schmielau | 4e57b68 | 2005-10-30 15:03:48 -0800 | [diff] [blame] | 23 | #include <linux/string.h> |
Benjamin Herrenschmidt | b4a26be | 2010-04-06 15:03:40 +0000 | [diff] [blame] | 24 | #include <linux/vmalloc.h> |
Tim Schmielau | 4e57b68 | 2005-10-30 15:03:48 -0800 | [diff] [blame] | 25 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <asm/pci-bridge.h> |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 27 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <asm/rtas.h> |
John Rose | 5eeb8c6 | 2005-07-25 10:16:42 -0500 | [diff] [blame] | 29 | #include <asm/vio.h> |
Tim Schmielau | 4e57b68 | 2005-10-30 15:03:48 -0800 | [diff] [blame] | 30 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include "../pci.h" |
| 32 | #include "rpaphp.h" |
| 33 | #include "rpadlpar.h" |
| 34 | |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 35 | static DEFINE_MUTEX(rpadlpar_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
John Rose | 56d8456 | 2005-07-25 10:17:03 -0500 | [diff] [blame] | 37 | #define DLPAR_MODULE_NAME "rpadlpar_io" |
| 38 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #define NODE_TYPE_VIO 1 |
| 40 | #define NODE_TYPE_SLOT 2 |
| 41 | #define NODE_TYPE_PHB 3 |
| 42 | |
John Rose | 5eeb8c6 | 2005-07-25 10:16:42 -0500 | [diff] [blame] | 43 | static struct device_node *find_vio_slot_node(char *drc_name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | struct device_node *parent = of_find_node_by_name(NULL, "vdevice"); |
John Rose | 5eeb8c6 | 2005-07-25 10:16:42 -0500 | [diff] [blame] | 46 | struct device_node *dn = NULL; |
| 47 | char *name; |
| 48 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
| 50 | if (!parent) |
| 51 | return NULL; |
| 52 | |
John Rose | 5eeb8c6 | 2005-07-25 10:16:42 -0500 | [diff] [blame] | 53 | while ((dn = of_get_next_child(parent, dn))) { |
| 54 | rc = rpaphp_get_drc_props(dn, NULL, &name, NULL, NULL); |
| 55 | if ((rc == 0) && (!strcmp(drc_name, name))) |
| 56 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | } |
Tyrel Datwyler | 8b19d72 | 2019-03-22 13:27:21 -0500 | [diff] [blame] | 58 | of_node_put(parent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
John Rose | 5eeb8c6 | 2005-07-25 10:16:42 -0500 | [diff] [blame] | 60 | return dn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | /* Find dlpar-capable pci node that contains the specified name and type */ |
| 64 | static struct device_node *find_php_slot_pci_node(char *drc_name, |
| 65 | char *drc_type) |
| 66 | { |
| 67 | struct device_node *np = NULL; |
| 68 | char *name; |
| 69 | char *type; |
| 70 | int rc; |
| 71 | |
John Rose | a57ed79 | 2006-11-13 15:12:52 -0800 | [diff] [blame] | 72 | while ((np = of_find_node_by_name(np, "pci"))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | rc = rpaphp_get_drc_props(np, NULL, &name, &type, NULL); |
| 74 | if (rc == 0) |
| 75 | if (!strcmp(drc_name, name) && !strcmp(drc_type, type)) |
| 76 | break; |
| 77 | } |
| 78 | |
| 79 | return np; |
| 80 | } |
| 81 | |
Tyrel Datwyler | 8b19d72 | 2019-03-22 13:27:21 -0500 | [diff] [blame] | 82 | /* Returns a device_node with its reference count incremented */ |
John Rose | 5eeb8c6 | 2005-07-25 10:16:42 -0500 | [diff] [blame] | 83 | static struct device_node *find_dlpar_node(char *drc_name, int *node_type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | { |
| 85 | struct device_node *dn; |
| 86 | |
| 87 | dn = find_php_slot_pci_node(drc_name, "SLOT"); |
| 88 | if (dn) { |
| 89 | *node_type = NODE_TYPE_SLOT; |
| 90 | return dn; |
| 91 | } |
| 92 | |
| 93 | dn = find_php_slot_pci_node(drc_name, "PHB"); |
| 94 | if (dn) { |
| 95 | *node_type = NODE_TYPE_PHB; |
| 96 | return dn; |
| 97 | } |
| 98 | |
John Rose | 5eeb8c6 | 2005-07-25 10:16:42 -0500 | [diff] [blame] | 99 | dn = find_vio_slot_node(drc_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | if (dn) { |
| 101 | *node_type = NODE_TYPE_VIO; |
| 102 | return dn; |
| 103 | } |
| 104 | |
| 105 | return NULL; |
| 106 | } |
| 107 | |
Linas Vepstas | 8485d1a | 2007-04-13 15:34:21 -0700 | [diff] [blame] | 108 | /** |
| 109 | * find_php_slot - return hotplug slot structure for device node |
Randy Dunlap | 26e6c66 | 2007-11-28 09:04:30 -0800 | [diff] [blame] | 110 | * @dn: target &device_node |
Linas Vepstas | 8485d1a | 2007-04-13 15:34:21 -0700 | [diff] [blame] | 111 | * |
| 112 | * This routine will return the hotplug slot structure |
| 113 | * for a given device node. Note that built-in PCI slots |
| 114 | * may be dlpar-able, but not hot-pluggable, so this routine |
| 115 | * will return NULL for built-in PCI slots. |
| 116 | */ |
| 117 | static struct slot *find_php_slot(struct device_node *dn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | { |
Geliang Tang | 2ac83cc | 2015-12-12 21:36:57 +0800 | [diff] [blame] | 119 | struct slot *slot, *next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | |
Geliang Tang | 2ac83cc | 2015-12-12 21:36:57 +0800 | [diff] [blame] | 121 | list_for_each_entry_safe(slot, next, &rpaphp_slot_head, |
| 122 | rpaphp_slot_list) { |
linas@austin.ibm.com | 8737d6a | 2006-01-12 18:35:23 -0600 | [diff] [blame] | 123 | if (slot->dn == dn) |
| 124 | return slot; |
| 125 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | |
linas@austin.ibm.com | 8737d6a | 2006-01-12 18:35:23 -0600 | [diff] [blame] | 127 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | } |
| 129 | |
John Rose | 0945cd5 | 2005-07-25 10:16:53 -0500 | [diff] [blame] | 130 | static struct pci_dev *dlpar_find_new_dev(struct pci_bus *parent, |
| 131 | struct device_node *dev_dn) |
| 132 | { |
| 133 | struct pci_dev *tmp = NULL; |
| 134 | struct device_node *child_dn; |
| 135 | |
| 136 | list_for_each_entry(tmp, &parent->devices, bus_list) { |
| 137 | child_dn = pci_device_to_OF_node(tmp); |
| 138 | if (child_dn == dev_dn) |
| 139 | return tmp; |
| 140 | } |
| 141 | return NULL; |
| 142 | } |
| 143 | |
linas@austin.ibm.com | 8737d6a | 2006-01-12 18:35:23 -0600 | [diff] [blame] | 144 | static void dlpar_pci_add_bus(struct device_node *dn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | { |
linas@austin.ibm.com | 8737d6a | 2006-01-12 18:35:23 -0600 | [diff] [blame] | 146 | struct pci_dn *pdn = PCI_DN(dn); |
John Rose | 5fa80fc | 2005-11-04 15:38:50 -0600 | [diff] [blame] | 147 | struct pci_controller *phb = pdn->phb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | struct pci_dev *dev = NULL; |
| 149 | |
Gavin Shan | ff57b45 | 2015-03-17 16:15:06 +1100 | [diff] [blame] | 150 | eeh_add_device_tree_early(pdn); |
linas@austin.ibm.com | d681db4 | 2005-12-01 18:56:14 -0600 | [diff] [blame] | 151 | |
John Rose | 5fa80fc | 2005-11-04 15:38:50 -0600 | [diff] [blame] | 152 | /* Add EADS device to PHB bus, adding new entry to bus->devices */ |
| 153 | dev = of_create_pci_dev(dn, phb->bus, pdn->devfn); |
| 154 | if (!dev) { |
| 155 | printk(KERN_ERR "%s: failed to create pci dev for %s\n", |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 156 | __func__, dn->full_name); |
linas@austin.ibm.com | 8737d6a | 2006-01-12 18:35:23 -0600 | [diff] [blame] | 157 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Benjamin Herrenschmidt | fd6852c | 2008-10-27 19:48:52 +0000 | [diff] [blame] | 160 | /* Scan below the new bridge */ |
Yijing Wang | f86e1f1 | 2014-05-04 12:23:43 +0800 | [diff] [blame] | 161 | if (pci_is_bridge(dev)) |
Benjamin Herrenschmidt | 98d9f30c8 | 2011-04-11 11:37:07 +1000 | [diff] [blame] | 162 | of_scan_pci_bridge(dev); |
John Rose | 5fa80fc | 2005-11-04 15:38:50 -0600 | [diff] [blame] | 163 | |
Benjamin Herrenschmidt | 3d5134e | 2007-06-04 15:15:36 +1000 | [diff] [blame] | 164 | /* Map IO space for child bus, which may or may not succeed */ |
| 165 | pcibios_map_io_space(dev->subordinate); |
John Rose | 5fa80fc | 2005-11-04 15:38:50 -0600 | [diff] [blame] | 166 | |
Benjamin Herrenschmidt | fd6852c | 2008-10-27 19:48:52 +0000 | [diff] [blame] | 167 | /* Finish adding it : resource allocation, adding devices, etc... |
| 168 | * Note that we need to perform the finish pass on the -parent- |
| 169 | * bus of the EADS bridge so the bridge device itself gets |
| 170 | * properly added |
| 171 | */ |
| 172 | pcibios_finish_adding_to_bus(phb->bus); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | } |
| 174 | |
John Rose | 940903c | 2005-07-25 10:16:58 -0500 | [diff] [blame] | 175 | static int dlpar_add_pci_slot(char *drc_name, struct device_node *dn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | { |
| 177 | struct pci_dev *dev; |
linas@austin.ibm.com | 8737d6a | 2006-01-12 18:35:23 -0600 | [diff] [blame] | 178 | struct pci_controller *phb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | |
Gavin Shan | 3773dd2 | 2016-05-03 15:41:38 +1000 | [diff] [blame] | 180 | if (pci_find_bus_by_node(dn)) |
John Rose | 56d8456 | 2005-07-25 10:17:03 -0500 | [diff] [blame] | 181 | return -EINVAL; |
| 182 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | /* Add pci bus */ |
linas@austin.ibm.com | 8737d6a | 2006-01-12 18:35:23 -0600 | [diff] [blame] | 184 | dlpar_pci_add_bus(dn); |
| 185 | |
| 186 | /* Confirm new bridge dev was created */ |
| 187 | phb = PCI_DN(dn)->phb; |
| 188 | dev = dlpar_find_new_dev(phb->bus, dn); |
| 189 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | if (!dev) { |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 191 | printk(KERN_ERR "%s: unable to add bus %s\n", __func__, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | drc_name); |
| 193 | return -EIO; |
| 194 | } |
| 195 | |
linas@austin.ibm.com | 8737d6a | 2006-01-12 18:35:23 -0600 | [diff] [blame] | 196 | if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) { |
| 197 | printk(KERN_ERR "%s: unexpected header type %d, unable to add bus %s\n", |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 198 | __func__, dev->hdr_type, drc_name); |
linas@austin.ibm.com | 8737d6a | 2006-01-12 18:35:23 -0600 | [diff] [blame] | 199 | return -EIO; |
| 200 | } |
| 201 | |
John Rose | 5eeb8c6 | 2005-07-25 10:16:42 -0500 | [diff] [blame] | 202 | /* Add hotplug slot */ |
| 203 | if (rpaphp_add_slot(dn)) { |
| 204 | printk(KERN_ERR "%s: unable to add hotplug slot %s\n", |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 205 | __func__, drc_name); |
John Rose | 5eeb8c6 | 2005-07-25 10:16:42 -0500 | [diff] [blame] | 206 | return -EIO; |
| 207 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | return 0; |
| 209 | } |
| 210 | |
John Rose | 56d8456 | 2005-07-25 10:17:03 -0500 | [diff] [blame] | 211 | static int dlpar_remove_phb(char *drc_name, struct device_node *dn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | { |
John Rose | 56d8456 | 2005-07-25 10:17:03 -0500 | [diff] [blame] | 213 | struct slot *slot; |
Paul Mackerras | 1635317 | 2005-09-06 13:17:54 +1000 | [diff] [blame] | 214 | struct pci_dn *pdn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | int rc = 0; |
| 216 | |
Gavin Shan | 3773dd2 | 2016-05-03 15:41:38 +1000 | [diff] [blame] | 217 | if (!pci_find_bus_by_node(dn)) |
John Rose | 56d8456 | 2005-07-25 10:17:03 -0500 | [diff] [blame] | 218 | return -EINVAL; |
| 219 | |
Bjorn Helgaas | f762598 | 2013-11-14 11:28:18 -0700 | [diff] [blame] | 220 | /* If pci slot is hotpluggable, use hotplug to remove it */ |
Linas Vepstas | 8485d1a | 2007-04-13 15:34:21 -0700 | [diff] [blame] | 221 | slot = find_php_slot(dn); |
Benjamin Herrenschmidt | fd6852c | 2008-10-27 19:48:52 +0000 | [diff] [blame] | 222 | if (slot && rpaphp_deregister_slot(slot)) { |
| 223 | printk(KERN_ERR "%s: unable to remove hotplug slot %s\n", |
| 224 | __func__, drc_name); |
| 225 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Paul Mackerras | 1635317 | 2005-09-06 13:17:54 +1000 | [diff] [blame] | 228 | pdn = dn->data; |
| 229 | BUG_ON(!pdn || !pdn->phb); |
Benjamin Herrenschmidt | fd6852c | 2008-10-27 19:48:52 +0000 | [diff] [blame] | 230 | rc = remove_phb_dynamic(pdn->phb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | if (rc < 0) |
| 232 | return rc; |
| 233 | |
Paul Mackerras | 1635317 | 2005-09-06 13:17:54 +1000 | [diff] [blame] | 234 | pdn->phb = NULL; |
John Rose | 56d8456 | 2005-07-25 10:17:03 -0500 | [diff] [blame] | 235 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | return 0; |
| 237 | } |
| 238 | |
John Rose | 5eeb8c6 | 2005-07-25 10:16:42 -0500 | [diff] [blame] | 239 | static int dlpar_add_phb(char *drc_name, struct device_node *dn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | { |
| 241 | struct pci_controller *phb; |
| 242 | |
Linas Vepstas | fe98aea | 2005-11-03 18:51:17 -0600 | [diff] [blame] | 243 | if (PCI_DN(dn) && PCI_DN(dn)->phb) { |
John Rose | 56d8456 | 2005-07-25 10:17:03 -0500 | [diff] [blame] | 244 | /* PHB already exists */ |
| 245 | return -EINVAL; |
| 246 | } |
| 247 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | phb = init_phb_dynamic(dn); |
| 249 | if (!phb) |
John Rose | 56d8456 | 2005-07-25 10:17:03 -0500 | [diff] [blame] | 250 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | |
John Rose | 5eeb8c6 | 2005-07-25 10:16:42 -0500 | [diff] [blame] | 252 | if (rpaphp_add_slot(dn)) { |
| 253 | printk(KERN_ERR "%s: unable to add hotplug slot %s\n", |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 254 | __func__, drc_name); |
John Rose | 5eeb8c6 | 2005-07-25 10:16:42 -0500 | [diff] [blame] | 255 | return -EIO; |
| 256 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | return 0; |
| 258 | } |
| 259 | |
John Rose | 56d8456 | 2005-07-25 10:17:03 -0500 | [diff] [blame] | 260 | static int dlpar_add_vio_slot(char *drc_name, struct device_node *dn) |
| 261 | { |
Johan Hovold | b3539f8 | 2016-11-01 16:26:03 +0100 | [diff] [blame] | 262 | struct vio_dev *vio_dev; |
| 263 | |
| 264 | vio_dev = vio_find_node(dn); |
| 265 | if (vio_dev) { |
| 266 | put_device(&vio_dev->dev); |
John Rose | 56d8456 | 2005-07-25 10:17:03 -0500 | [diff] [blame] | 267 | return -EINVAL; |
Johan Hovold | b3539f8 | 2016-11-01 16:26:03 +0100 | [diff] [blame] | 268 | } |
John Rose | 56d8456 | 2005-07-25 10:17:03 -0500 | [diff] [blame] | 269 | |
| 270 | if (!vio_register_device_node(dn)) { |
| 271 | printk(KERN_ERR |
| 272 | "%s: failed to register vio node %s\n", |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 273 | __func__, drc_name); |
John Rose | 56d8456 | 2005-07-25 10:17:03 -0500 | [diff] [blame] | 274 | return -EIO; |
| 275 | } |
| 276 | return 0; |
| 277 | } |
| 278 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | /** |
| 280 | * dlpar_add_slot - DLPAR add an I/O Slot |
| 281 | * @drc_name: drc-name of newly added slot |
| 282 | * |
Randy Dunlap | 26e6c66 | 2007-11-28 09:04:30 -0800 | [diff] [blame] | 283 | * Make the hotplug module and the kernel aware of a newly added I/O Slot. |
| 284 | * Return Codes: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | * 0 Success |
| 286 | * -ENODEV Not a valid drc_name |
| 287 | * -EINVAL Slot already added |
| 288 | * -ERESTARTSYS Signalled before obtaining lock |
| 289 | * -EIO Internal PCI Error |
| 290 | */ |
| 291 | int dlpar_add_slot(char *drc_name) |
| 292 | { |
| 293 | struct device_node *dn = NULL; |
| 294 | int node_type; |
John Rose | 56d8456 | 2005-07-25 10:17:03 -0500 | [diff] [blame] | 295 | int rc = -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 297 | if (mutex_lock_interruptible(&rpadlpar_mutex)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | return -ERESTARTSYS; |
| 299 | |
John Rose | 5eeb8c6 | 2005-07-25 10:16:42 -0500 | [diff] [blame] | 300 | /* Find newly added node */ |
| 301 | dn = find_dlpar_node(drc_name, &node_type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | if (!dn) { |
| 303 | rc = -ENODEV; |
| 304 | goto exit; |
| 305 | } |
| 306 | |
| 307 | switch (node_type) { |
| 308 | case NODE_TYPE_VIO: |
John Rose | 56d8456 | 2005-07-25 10:17:03 -0500 | [diff] [blame] | 309 | rc = dlpar_add_vio_slot(drc_name, dn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | break; |
| 311 | case NODE_TYPE_SLOT: |
| 312 | rc = dlpar_add_pci_slot(drc_name, dn); |
| 313 | break; |
| 314 | case NODE_TYPE_PHB: |
John Rose | 5eeb8c6 | 2005-07-25 10:16:42 -0500 | [diff] [blame] | 315 | rc = dlpar_add_phb(drc_name, dn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | } |
Tyrel Datwyler | 8b19d72 | 2019-03-22 13:27:21 -0500 | [diff] [blame] | 318 | of_node_put(dn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | |
John Rose | 56d8456 | 2005-07-25 10:17:03 -0500 | [diff] [blame] | 320 | printk(KERN_INFO "%s: slot %s added\n", DLPAR_MODULE_NAME, drc_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | exit: |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 322 | mutex_unlock(&rpadlpar_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | return rc; |
| 324 | } |
| 325 | |
| 326 | /** |
| 327 | * dlpar_remove_vio_slot - DLPAR remove a virtual I/O Slot |
| 328 | * @drc_name: drc-name of newly added slot |
Randy Dunlap | 26e6c66 | 2007-11-28 09:04:30 -0800 | [diff] [blame] | 329 | * @dn: &device_node |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | * |
Randy Dunlap | 26e6c66 | 2007-11-28 09:04:30 -0800 | [diff] [blame] | 331 | * Remove the kernel and hotplug representations of an I/O Slot. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | * Return Codes: |
| 333 | * 0 Success |
John Rose | 56d8456 | 2005-07-25 10:17:03 -0500 | [diff] [blame] | 334 | * -EINVAL Vio dev doesn't exist |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | */ |
John Rose | 56d8456 | 2005-07-25 10:17:03 -0500 | [diff] [blame] | 336 | static int dlpar_remove_vio_slot(char *drc_name, struct device_node *dn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | { |
John Rose | 5eeb8c6 | 2005-07-25 10:16:42 -0500 | [diff] [blame] | 338 | struct vio_dev *vio_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | |
John Rose | 5eeb8c6 | 2005-07-25 10:16:42 -0500 | [diff] [blame] | 340 | vio_dev = vio_find_node(dn); |
John Rose | 56d8456 | 2005-07-25 10:17:03 -0500 | [diff] [blame] | 341 | if (!vio_dev) |
| 342 | return -EINVAL; |
John Rose | 5eeb8c6 | 2005-07-25 10:16:42 -0500 | [diff] [blame] | 343 | |
| 344 | vio_unregister_device(vio_dev); |
Johan Hovold | b3539f8 | 2016-11-01 16:26:03 +0100 | [diff] [blame] | 345 | |
| 346 | put_device(&vio_dev->dev); |
| 347 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | return 0; |
| 349 | } |
| 350 | |
| 351 | /** |
Randy Dunlap | 26e6c66 | 2007-11-28 09:04:30 -0800 | [diff] [blame] | 352 | * dlpar_remove_pci_slot - DLPAR remove a PCI I/O Slot |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | * @drc_name: drc-name of newly added slot |
Randy Dunlap | 26e6c66 | 2007-11-28 09:04:30 -0800 | [diff] [blame] | 354 | * @dn: &device_node |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | * |
Randy Dunlap | 26e6c66 | 2007-11-28 09:04:30 -0800 | [diff] [blame] | 356 | * Remove the kernel and hotplug representations of a PCI I/O Slot. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | * Return Codes: |
| 358 | * 0 Success |
| 359 | * -ENODEV Not a valid drc_name |
| 360 | * -EIO Internal PCI Error |
| 361 | */ |
John Rose | 56d8456 | 2005-07-25 10:17:03 -0500 | [diff] [blame] | 362 | int dlpar_remove_pci_slot(char *drc_name, struct device_node *dn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | { |
John Rose | 56d8456 | 2005-07-25 10:17:03 -0500 | [diff] [blame] | 364 | struct pci_bus *bus; |
| 365 | struct slot *slot; |
Rafael J. Wysocki | c4ec84c | 2014-01-14 12:03:14 -0700 | [diff] [blame] | 366 | int ret = 0; |
| 367 | |
| 368 | pci_lock_rescan_remove(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | |
Gavin Shan | 3773dd2 | 2016-05-03 15:41:38 +1000 | [diff] [blame] | 370 | bus = pci_find_bus_by_node(dn); |
Rafael J. Wysocki | c4ec84c | 2014-01-14 12:03:14 -0700 | [diff] [blame] | 371 | if (!bus) { |
| 372 | ret = -EINVAL; |
| 373 | goto out; |
| 374 | } |
John Rose | 56d8456 | 2005-07-25 10:17:03 -0500 | [diff] [blame] | 375 | |
Benjamin Herrenschmidt | fd6852c | 2008-10-27 19:48:52 +0000 | [diff] [blame] | 376 | pr_debug("PCI: Removing PCI slot below EADS bridge %s\n", |
| 377 | bus->self ? pci_name(bus->self) : "<!PHB!>"); |
| 378 | |
Linas Vepstas | 8485d1a | 2007-04-13 15:34:21 -0700 | [diff] [blame] | 379 | slot = find_php_slot(dn); |
John Rose | 56d8456 | 2005-07-25 10:17:03 -0500 | [diff] [blame] | 380 | if (slot) { |
Benjamin Herrenschmidt | fd6852c | 2008-10-27 19:48:52 +0000 | [diff] [blame] | 381 | pr_debug("PCI: Removing hotplug slot for %04x:%02x...\n", |
| 382 | pci_domain_nr(bus), bus->number); |
| 383 | |
linas@austin.ibm.com | f6afbad | 2006-01-12 18:31:01 -0600 | [diff] [blame] | 384 | if (rpaphp_deregister_slot(slot)) { |
John Rose | 56d8456 | 2005-07-25 10:17:03 -0500 | [diff] [blame] | 385 | printk(KERN_ERR |
| 386 | "%s: unable to remove hotplug slot %s\n", |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 387 | __func__, drc_name); |
Rafael J. Wysocki | c4ec84c | 2014-01-14 12:03:14 -0700 | [diff] [blame] | 388 | ret = -EIO; |
| 389 | goto out; |
John Rose | 56d8456 | 2005-07-25 10:17:03 -0500 | [diff] [blame] | 390 | } |
Benjamin Herrenschmidt | fd6852c | 2008-10-27 19:48:52 +0000 | [diff] [blame] | 391 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | |
Benjamin Herrenschmidt | fd6852c | 2008-10-27 19:48:52 +0000 | [diff] [blame] | 393 | /* Remove all devices below slot */ |
Gavin Shan | bd251b8 | 2016-05-03 15:41:37 +1000 | [diff] [blame] | 394 | pci_hp_remove_devices(bus); |
Benjamin Herrenschmidt | fd6852c | 2008-10-27 19:48:52 +0000 | [diff] [blame] | 395 | |
| 396 | /* Unmap PCI IO space */ |
Benjamin Herrenschmidt | 3d5134e | 2007-06-04 15:15:36 +1000 | [diff] [blame] | 397 | if (pcibios_unmap_io_space(bus)) { |
John Rose | 9c209c9 | 2005-07-25 11:13:38 -0500 | [diff] [blame] | 398 | printk(KERN_ERR "%s: failed to unmap bus range\n", |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 399 | __func__); |
Rafael J. Wysocki | c4ec84c | 2014-01-14 12:03:14 -0700 | [diff] [blame] | 400 | ret = -ERANGE; |
| 401 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | } |
John Rose | 9c209c9 | 2005-07-25 11:13:38 -0500 | [diff] [blame] | 403 | |
Benjamin Herrenschmidt | fd6852c | 2008-10-27 19:48:52 +0000 | [diff] [blame] | 404 | /* Remove the EADS bridge device itself */ |
John Rose | 9c209c9 | 2005-07-25 11:13:38 -0500 | [diff] [blame] | 405 | BUG_ON(!bus->self); |
Benjamin Herrenschmidt | fd6852c | 2008-10-27 19:48:52 +0000 | [diff] [blame] | 406 | pr_debug("PCI: Now removing bridge device %s\n", pci_name(bus->self)); |
Yinghai Lu | 210647a | 2012-02-25 13:54:20 -0800 | [diff] [blame] | 407 | pci_stop_and_remove_bus_device(bus->self); |
Benjamin Herrenschmidt | fd6852c | 2008-10-27 19:48:52 +0000 | [diff] [blame] | 408 | |
Rafael J. Wysocki | c4ec84c | 2014-01-14 12:03:14 -0700 | [diff] [blame] | 409 | out: |
| 410 | pci_unlock_rescan_remove(); |
| 411 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | /** |
| 415 | * dlpar_remove_slot - DLPAR remove an I/O Slot |
| 416 | * @drc_name: drc-name of newly added slot |
| 417 | * |
Randy Dunlap | 26e6c66 | 2007-11-28 09:04:30 -0800 | [diff] [blame] | 418 | * Remove the kernel and hotplug representations of an I/O Slot. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | * Return Codes: |
| 420 | * 0 Success |
| 421 | * -ENODEV Not a valid drc_name |
| 422 | * -EINVAL Slot already removed |
| 423 | * -ERESTARTSYS Signalled before obtaining lock |
| 424 | * -EIO Internal Error |
| 425 | */ |
| 426 | int dlpar_remove_slot(char *drc_name) |
| 427 | { |
John Rose | 5eeb8c6 | 2005-07-25 10:16:42 -0500 | [diff] [blame] | 428 | struct device_node *dn; |
John Rose | 5eeb8c6 | 2005-07-25 10:16:42 -0500 | [diff] [blame] | 429 | int node_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | int rc = 0; |
| 431 | |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 432 | if (mutex_lock_interruptible(&rpadlpar_mutex)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | return -ERESTARTSYS; |
| 434 | |
John Rose | 5eeb8c6 | 2005-07-25 10:16:42 -0500 | [diff] [blame] | 435 | dn = find_dlpar_node(drc_name, &node_type); |
| 436 | if (!dn) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | rc = -ENODEV; |
| 438 | goto exit; |
| 439 | } |
| 440 | |
John Rose | 56d8456 | 2005-07-25 10:17:03 -0500 | [diff] [blame] | 441 | switch (node_type) { |
| 442 | case NODE_TYPE_VIO: |
| 443 | rc = dlpar_remove_vio_slot(drc_name, dn); |
| 444 | break; |
| 445 | case NODE_TYPE_PHB: |
| 446 | rc = dlpar_remove_phb(drc_name, dn); |
| 447 | break; |
| 448 | case NODE_TYPE_SLOT: |
| 449 | rc = dlpar_remove_pci_slot(drc_name, dn); |
| 450 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | } |
Tyrel Datwyler | 8b19d72 | 2019-03-22 13:27:21 -0500 | [diff] [blame] | 452 | of_node_put(dn); |
Benjamin Herrenschmidt | b4a26be | 2010-04-06 15:03:40 +0000 | [diff] [blame] | 453 | vm_unmap_aliases(); |
| 454 | |
John Rose | 56d8456 | 2005-07-25 10:17:03 -0500 | [diff] [blame] | 455 | printk(KERN_INFO "%s: slot %s removed\n", DLPAR_MODULE_NAME, drc_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | exit: |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 457 | mutex_unlock(&rpadlpar_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | return rc; |
| 459 | } |
| 460 | |
| 461 | static inline int is_dlpar_capable(void) |
| 462 | { |
| 463 | int rc = rtas_token("ibm,configure-connector"); |
| 464 | |
| 465 | return (int) (rc != RTAS_UNKNOWN_SERVICE); |
| 466 | } |
| 467 | |
| 468 | int __init rpadlpar_io_init(void) |
| 469 | { |
| 470 | int rc = 0; |
| 471 | |
| 472 | if (!is_dlpar_capable()) { |
| 473 | printk(KERN_WARNING "%s: partition not DLPAR capable\n", |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 474 | __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | return -EPERM; |
| 476 | } |
| 477 | |
| 478 | rc = dlpar_sysfs_init(); |
| 479 | return rc; |
| 480 | } |
| 481 | |
| 482 | void rpadlpar_io_exit(void) |
| 483 | { |
| 484 | dlpar_sysfs_exit(); |
| 485 | return; |
| 486 | } |
| 487 | |
| 488 | module_init(rpadlpar_io_init); |
| 489 | module_exit(rpadlpar_io_exit); |
| 490 | MODULE_LICENSE("GPL"); |