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 */ 17 #include <linux/init.h> 18 #include <linux/pci.h> 19 #include <linux/string.h> 20 21 #include <asm/pci-bridge.h> 22 #include <asm/semaphore.h> 23 #include <asm/rtas.h> 24 #include <asm/vio.h> 25 26 #include "../pci.h" 27 #include "rpaphp.h" 28 #include "rpadlpar.h" 29 30 static DECLARE_MUTEX(rpadlpar_sem); 31 32 #define DLPAR_MODULE_NAME "rpadlpar_io" 33 34 #define NODE_TYPE_VIO 1 35 #define NODE_TYPE_SLOT 2 36 #define NODE_TYPE_PHB 3 37 38 static struct device_node *find_vio_slot_node(char *drc_name) 39 { 40 struct device_node *parent = of_find_node_by_name(NULL, "vdevice"); 41 struct device_node *dn = NULL; 42 char *name; 43 int rc; 44 45 if (!parent) 46 return NULL; 47 48 while ((dn = of_get_next_child(parent, dn))) { 49 rc = rpaphp_get_drc_props(dn, NULL, &name, NULL, NULL); 50 if ((rc == 0) && (!strcmp(drc_name, name))) 51 break; 52 } 53 54 return dn; 55 } 56 57 /* Find dlpar-capable pci node that contains the specified name and type */ 58 static struct device_node *find_php_slot_pci_node(char *drc_name, 59 char *drc_type) 60 { 61 struct device_node *np = NULL; 62 char *name; 63 char *type; 64 int rc; 65 66 while ((np = of_find_node_by_type(np, "pci"))) { 67 rc = rpaphp_get_drc_props(np, NULL, &name, &type, NULL); 68 if (rc == 0) 69 if (!strcmp(drc_name, name) && !strcmp(drc_type, type)) 70 break; 71 } 72 73 return np; 74 } 75 76 static struct device_node *find_dlpar_node(char *drc_name, int *node_type) 77 { 78 struct device_node *dn; 79 80 dn = find_php_slot_pci_node(drc_name, "SLOT"); 81 if (dn) { 82 *node_type = NODE_TYPE_SLOT; 83 return dn; 84 } 85 86 dn = find_php_slot_pci_node(drc_name, "PHB"); 87 if (dn) { 88 *node_type = NODE_TYPE_PHB; 89 return dn; 90 } 91 92 dn = find_vio_slot_node(drc_name); 93 if (dn) { 94 *node_type = NODE_TYPE_VIO; 95 return dn; 96 } 97 98 return NULL; 99 } 100 101 static struct slot *find_slot(struct device_node *dn) 102 { 103 struct list_head *tmp, *n; 104 struct slot *slot; 105 106 list_for_each_safe(tmp, n, &rpaphp_slot_head) { 107 slot = list_entry(tmp, struct slot, rpaphp_slot_list); 108 if (slot->dn == dn) 109 return slot; 110 } 111 112 return NULL; 113 } 114 115 static void rpadlpar_claim_one_bus(struct pci_bus *b) 116 { 117 struct list_head *ld; 118 struct pci_bus *child_bus; 119 120 for (ld = b->devices.next; ld != &b->devices; ld = ld->next) { 121 struct pci_dev *dev = pci_dev_b(ld); 122 int i; 123 124 for (i = 0; i < PCI_NUM_RESOURCES; i++) { 125 struct resource *r = &dev->resource[i]; 126 127 if (r->parent || !r->start || !r->flags) 128 continue; 129 rpaphp_claim_resource(dev, i); 130 } 131 } 132 133 list_for_each_entry(child_bus, &b->children, node) 134 rpadlpar_claim_one_bus(child_bus); 135 } 136 137 static struct pci_dev *dlpar_find_new_dev(struct pci_bus *parent, 138 struct device_node *dev_dn) 139 { 140 struct pci_dev *tmp = NULL; 141 struct device_node *child_dn; 142 143 list_for_each_entry(tmp, &parent->devices, bus_list) { 144 child_dn = pci_device_to_OF_node(tmp); 145 if (child_dn == dev_dn) 146 return tmp; 147 } 148 return NULL; 149 } 150 151 static struct pci_dev *dlpar_pci_add_bus(struct device_node *dn) 152 { 153 struct pci_dn *pdn = dn->data; 154 struct pci_controller *phb = pdn->phb; 155 struct pci_dev *dev = NULL; 156 157 rpaphp_eeh_init_nodes(dn); 158 /* Add EADS device to PHB bus, adding new entry to bus->devices */ 159 dev = of_create_pci_dev(dn, phb->bus, pdn->devfn); 160 if (!dev) { 161 printk(KERN_ERR "%s: failed to create pci dev for %s\n", 162 __FUNCTION__, dn->full_name); 163 return NULL; 164 } 165 166 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE || 167 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) 168 of_scan_pci_bridge(dn, dev); 169 170 rpaphp_init_new_devs(dev->subordinate); 171 172 /* Claim new bus resources */ 173 rpadlpar_claim_one_bus(dev->bus); 174 175 /* ioremap() for child bus, which may or may not succeed */ 176 (void) remap_bus_range(dev->bus); 177 178 /* Add new devices to global lists. Register in proc, sysfs. */ 179 pci_bus_add_devices(phb->bus); 180 181 /* Confirm new bridge dev was created */ 182 dev = dlpar_find_new_dev(phb->bus, dn); 183 if (dev) { 184 if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) { 185 printk(KERN_ERR "%s: unexpected header type %d\n", 186 __FUNCTION__, dev->hdr_type); 187 return NULL; 188 } 189 } 190 191 return dev; 192 } 193 194 static int dlpar_add_pci_slot(char *drc_name, struct device_node *dn) 195 { 196 struct pci_dev *dev; 197 198 if (rpaphp_find_pci_bus(dn)) 199 return -EINVAL; 200 201 /* Add pci bus */ 202 dev = dlpar_pci_add_bus(dn); 203 if (!dev) { 204 printk(KERN_ERR "%s: unable to add bus %s\n", __FUNCTION__, 205 drc_name); 206 return -EIO; 207 } 208 209 /* Add hotplug slot */ 210 if (rpaphp_add_slot(dn)) { 211 printk(KERN_ERR "%s: unable to add hotplug slot %s\n", 212 __FUNCTION__, drc_name); 213 return -EIO; 214 } 215 return 0; 216 } 217 218 static int dlpar_remove_root_bus(struct pci_controller *phb) 219 { 220 struct pci_bus *phb_bus; 221 int rc; 222 223 phb_bus = phb->bus; 224 if (!(list_empty(&phb_bus->children) && 225 list_empty(&phb_bus->devices))) { 226 return -EBUSY; 227 } 228 229 rc = pcibios_remove_root_bus(phb); 230 if (rc) 231 return -EIO; 232 233 device_unregister(phb_bus->bridge); 234 pci_remove_bus(phb_bus); 235 236 return 0; 237 } 238 239 static int dlpar_remove_phb(char *drc_name, struct device_node *dn) 240 { 241 struct slot *slot; 242 struct pci_dn *pdn; 243 int rc = 0; 244 245 if (!rpaphp_find_pci_bus(dn)) 246 return -EINVAL; 247 248 slot = find_slot(dn); 249 if (slot) { 250 /* Remove hotplug slot */ 251 if (rpaphp_remove_slot(slot)) { 252 printk(KERN_ERR 253 "%s: unable to remove hotplug slot %s\n", 254 __FUNCTION__, drc_name); 255 return -EIO; 256 } 257 } 258 259 pdn = dn->data; 260 BUG_ON(!pdn || !pdn->phb); 261 rc = dlpar_remove_root_bus(pdn->phb); 262 if (rc < 0) 263 return rc; 264 265 pdn->phb = NULL; 266 267 return 0; 268 } 269 270 static int dlpar_add_phb(char *drc_name, struct device_node *dn) 271 { 272 struct pci_controller *phb; 273 274 if (PCI_DN(dn) && PCI_DN(dn)->phb) { 275 /* PHB already exists */ 276 return -EINVAL; 277 } 278 279 phb = init_phb_dynamic(dn); 280 if (!phb) 281 return -EIO; 282 283 if (rpaphp_add_slot(dn)) { 284 printk(KERN_ERR "%s: unable to add hotplug slot %s\n", 285 __FUNCTION__, drc_name); 286 return -EIO; 287 } 288 return 0; 289 } 290 291 static int dlpar_add_vio_slot(char *drc_name, struct device_node *dn) 292 { 293 if (vio_find_node(dn)) 294 return -EINVAL; 295 296 if (!vio_register_device_node(dn)) { 297 printk(KERN_ERR 298 "%s: failed to register vio node %s\n", 299 __FUNCTION__, drc_name); 300 return -EIO; 301 } 302 return 0; 303 } 304 305 /** 306 * dlpar_add_slot - DLPAR add an I/O Slot 307 * @drc_name: drc-name of newly added slot 308 * 309 * Make the hotplug module and the kernel aware 310 * of a newly added I/O Slot. 311 * Return Codes - 312 * 0 Success 313 * -ENODEV Not a valid drc_name 314 * -EINVAL Slot already added 315 * -ERESTARTSYS Signalled before obtaining lock 316 * -EIO Internal PCI Error 317 */ 318 int dlpar_add_slot(char *drc_name) 319 { 320 struct device_node *dn = NULL; 321 int node_type; 322 int rc = -EIO; 323 324 if (down_interruptible(&rpadlpar_sem)) 325 return -ERESTARTSYS; 326 327 /* Find newly added node */ 328 dn = find_dlpar_node(drc_name, &node_type); 329 if (!dn) { 330 rc = -ENODEV; 331 goto exit; 332 } 333 334 switch (node_type) { 335 case NODE_TYPE_VIO: 336 rc = dlpar_add_vio_slot(drc_name, dn); 337 break; 338 case NODE_TYPE_SLOT: 339 rc = dlpar_add_pci_slot(drc_name, dn); 340 break; 341 case NODE_TYPE_PHB: 342 rc = dlpar_add_phb(drc_name, dn); 343 break; 344 } 345 346 printk(KERN_INFO "%s: slot %s added\n", DLPAR_MODULE_NAME, drc_name); 347 exit: 348 up(&rpadlpar_sem); 349 return rc; 350 } 351 352 /** 353 * dlpar_remove_vio_slot - DLPAR remove a virtual I/O Slot 354 * @drc_name: drc-name of newly added slot 355 * 356 * Remove the kernel and hotplug representations 357 * of an I/O Slot. 358 * Return Codes: 359 * 0 Success 360 * -EINVAL Vio dev doesn't exist 361 */ 362 static int dlpar_remove_vio_slot(char *drc_name, struct device_node *dn) 363 { 364 struct vio_dev *vio_dev; 365 366 vio_dev = vio_find_node(dn); 367 if (!vio_dev) 368 return -EINVAL; 369 370 vio_unregister_device(vio_dev); 371 return 0; 372 } 373 374 /** 375 * dlpar_remove_slot - DLPAR remove a PCI I/O Slot 376 * @drc_name: drc-name of newly added slot 377 * 378 * Remove the kernel and hotplug representations 379 * of a PCI I/O Slot. 380 * Return Codes: 381 * 0 Success 382 * -ENODEV Not a valid drc_name 383 * -EIO Internal PCI Error 384 */ 385 int dlpar_remove_pci_slot(char *drc_name, struct device_node *dn) 386 { 387 struct pci_bus *bus; 388 struct slot *slot; 389 390 bus = rpaphp_find_pci_bus(dn); 391 if (!bus) 392 return -EINVAL; 393 394 slot = find_slot(dn); 395 if (slot) { 396 /* Remove hotplug slot */ 397 if (rpaphp_remove_slot(slot)) { 398 printk(KERN_ERR 399 "%s: unable to remove hotplug slot %s\n", 400 __FUNCTION__, drc_name); 401 return -EIO; 402 } 403 } else { 404 rpaphp_unconfig_pci_adapter(bus); 405 } 406 407 if (unmap_bus_range(bus)) { 408 printk(KERN_ERR "%s: failed to unmap bus range\n", 409 __FUNCTION__); 410 return -ERANGE; 411 } 412 413 BUG_ON(!bus->self); 414 pci_remove_bus_device(bus->self); 415 return 0; 416 } 417 418 /** 419 * dlpar_remove_slot - DLPAR remove an I/O Slot 420 * @drc_name: drc-name of newly added slot 421 * 422 * Remove the kernel and hotplug representations 423 * of an I/O Slot. 424 * Return Codes: 425 * 0 Success 426 * -ENODEV Not a valid drc_name 427 * -EINVAL Slot already removed 428 * -ERESTARTSYS Signalled before obtaining lock 429 * -EIO Internal Error 430 */ 431 int dlpar_remove_slot(char *drc_name) 432 { 433 struct device_node *dn; 434 int node_type; 435 int rc = 0; 436 437 if (down_interruptible(&rpadlpar_sem)) 438 return -ERESTARTSYS; 439 440 dn = find_dlpar_node(drc_name, &node_type); 441 if (!dn) { 442 rc = -ENODEV; 443 goto exit; 444 } 445 446 switch (node_type) { 447 case NODE_TYPE_VIO: 448 rc = dlpar_remove_vio_slot(drc_name, dn); 449 break; 450 case NODE_TYPE_PHB: 451 rc = dlpar_remove_phb(drc_name, dn); 452 break; 453 case NODE_TYPE_SLOT: 454 rc = dlpar_remove_pci_slot(drc_name, dn); 455 break; 456 } 457 printk(KERN_INFO "%s: slot %s removed\n", DLPAR_MODULE_NAME, drc_name); 458 exit: 459 up(&rpadlpar_sem); 460 return rc; 461 } 462 463 static inline int is_dlpar_capable(void) 464 { 465 int rc = rtas_token("ibm,configure-connector"); 466 467 return (int) (rc != RTAS_UNKNOWN_SERVICE); 468 } 469 470 int __init rpadlpar_io_init(void) 471 { 472 int rc = 0; 473 474 if (!is_dlpar_capable()) { 475 printk(KERN_WARNING "%s: partition not DLPAR capable\n", 476 __FUNCTION__); 477 return -EPERM; 478 } 479 480 rc = dlpar_sysfs_init(); 481 return rc; 482 } 483 484 void rpadlpar_io_exit(void) 485 { 486 dlpar_sysfs_exit(); 487 return; 488 } 489 490 module_init(rpadlpar_io_init); 491 module_exit(rpadlpar_io_exit); 492 MODULE_LICENSE("GPL"); 493