1 /* 2 * PCI Hotplug Driver for PowerPC PowerNV platform. 3 * 4 * Copyright Gavin Shan, IBM Corporation 2016. 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 2 of the License, or 9 * (at your option) any later version. 10 */ 11 12 #include <linux/libfdt.h> 13 #include <linux/module.h> 14 #include <linux/pci.h> 15 #include <linux/pci_hotplug.h> 16 17 #include <asm/opal.h> 18 #include <asm/pnv-pci.h> 19 #include <asm/ppc-pci.h> 20 21 #define DRIVER_VERSION "0.1" 22 #define DRIVER_AUTHOR "Gavin Shan, IBM Corporation" 23 #define DRIVER_DESC "PowerPC PowerNV PCI Hotplug Driver" 24 25 struct pnv_php_slot { 26 struct hotplug_slot slot; 27 struct hotplug_slot_info slot_info; 28 uint64_t id; 29 char *name; 30 int slot_no; 31 struct kref kref; 32 #define PNV_PHP_STATE_INITIALIZED 0 33 #define PNV_PHP_STATE_REGISTERED 1 34 #define PNV_PHP_STATE_POPULATED 2 35 #define PNV_PHP_STATE_OFFLINE 3 36 int state; 37 struct device_node *dn; 38 struct pci_dev *pdev; 39 struct pci_bus *bus; 40 bool power_state_check; 41 void *fdt; 42 void *dt; 43 struct of_changeset ocs; 44 struct pnv_php_slot *parent; 45 struct list_head children; 46 struct list_head link; 47 }; 48 49 static LIST_HEAD(pnv_php_slot_list); 50 static DEFINE_SPINLOCK(pnv_php_lock); 51 52 static void pnv_php_register(struct device_node *dn); 53 static void pnv_php_unregister_one(struct device_node *dn); 54 static void pnv_php_unregister(struct device_node *dn); 55 56 static void pnv_php_free_slot(struct kref *kref) 57 { 58 struct pnv_php_slot *php_slot = container_of(kref, 59 struct pnv_php_slot, kref); 60 61 WARN_ON(!list_empty(&php_slot->children)); 62 kfree(php_slot->name); 63 kfree(php_slot); 64 } 65 66 static inline void pnv_php_put_slot(struct pnv_php_slot *php_slot) 67 { 68 69 if (WARN_ON(!php_slot)) 70 return; 71 72 kref_put(&php_slot->kref, pnv_php_free_slot); 73 } 74 75 static struct pnv_php_slot *pnv_php_match(struct device_node *dn, 76 struct pnv_php_slot *php_slot) 77 { 78 struct pnv_php_slot *target, *tmp; 79 80 if (php_slot->dn == dn) { 81 kref_get(&php_slot->kref); 82 return php_slot; 83 } 84 85 list_for_each_entry(tmp, &php_slot->children, link) { 86 target = pnv_php_match(dn, tmp); 87 if (target) 88 return target; 89 } 90 91 return NULL; 92 } 93 94 static struct pnv_php_slot *pnv_php_find_slot(struct device_node *dn) 95 { 96 struct pnv_php_slot *php_slot, *tmp; 97 unsigned long flags; 98 99 spin_lock_irqsave(&pnv_php_lock, flags); 100 list_for_each_entry(tmp, &pnv_php_slot_list, link) { 101 php_slot = pnv_php_match(dn, tmp); 102 if (php_slot) { 103 spin_unlock_irqrestore(&pnv_php_lock, flags); 104 return php_slot; 105 } 106 } 107 spin_unlock_irqrestore(&pnv_php_lock, flags); 108 109 return NULL; 110 } 111 112 /* 113 * Remove pdn for all children of the indicated device node. 114 * The function should remove pdn in a depth-first manner. 115 */ 116 static void pnv_php_rmv_pdns(struct device_node *dn) 117 { 118 struct device_node *child; 119 120 for_each_child_of_node(dn, child) { 121 pnv_php_rmv_pdns(child); 122 123 pci_remove_device_node_info(child); 124 } 125 } 126 127 /* 128 * Detach all child nodes of the indicated device nodes. The 129 * function should handle device nodes in depth-first manner. 130 * 131 * We should not invoke of_node_release() as the memory for 132 * individual device node is part of large memory block. The 133 * large block is allocated from memblock (system bootup) or 134 * kmalloc() when unflattening the device tree by OF changeset. 135 * We can not free the large block allocated from memblock. For 136 * later case, it should be released at once. 137 */ 138 static void pnv_php_detach_device_nodes(struct device_node *parent) 139 { 140 struct device_node *dn; 141 int refcount; 142 143 for_each_child_of_node(parent, dn) { 144 pnv_php_detach_device_nodes(dn); 145 146 of_node_put(dn); 147 refcount = atomic_read(&dn->kobj.kref.refcount); 148 if (unlikely(refcount != 1)) 149 pr_warn("Invalid refcount %d on <%s>\n", 150 refcount, of_node_full_name(dn)); 151 152 of_detach_node(dn); 153 } 154 } 155 156 static void pnv_php_rmv_devtree(struct pnv_php_slot *php_slot) 157 { 158 pnv_php_rmv_pdns(php_slot->dn); 159 160 /* 161 * Decrease the refcount if the device nodes were created 162 * through OF changeset before detaching them. 163 */ 164 if (php_slot->fdt) 165 of_changeset_destroy(&php_slot->ocs); 166 pnv_php_detach_device_nodes(php_slot->dn); 167 168 if (php_slot->fdt) { 169 kfree(php_slot->dt); 170 kfree(php_slot->fdt); 171 php_slot->dt = NULL; 172 php_slot->dn->child = NULL; 173 php_slot->fdt = NULL; 174 } 175 } 176 177 /* 178 * As the nodes in OF changeset are applied in reverse order, we 179 * need revert the nodes in advance so that we have correct node 180 * order after the changeset is applied. 181 */ 182 static void pnv_php_reverse_nodes(struct device_node *parent) 183 { 184 struct device_node *child, *next; 185 186 /* In-depth first */ 187 for_each_child_of_node(parent, child) 188 pnv_php_reverse_nodes(child); 189 190 /* Reverse the nodes in the child list */ 191 child = parent->child; 192 parent->child = NULL; 193 while (child) { 194 next = child->sibling; 195 196 child->sibling = parent->child; 197 parent->child = child; 198 child = next; 199 } 200 } 201 202 static int pnv_php_populate_changeset(struct of_changeset *ocs, 203 struct device_node *dn) 204 { 205 struct device_node *child; 206 int ret = 0; 207 208 for_each_child_of_node(dn, child) { 209 ret = of_changeset_attach_node(ocs, child); 210 if (unlikely(ret)) 211 break; 212 213 ret = pnv_php_populate_changeset(ocs, child); 214 if (unlikely(ret)) 215 break; 216 } 217 218 return ret; 219 } 220 221 static void *pnv_php_add_one_pdn(struct device_node *dn, void *data) 222 { 223 struct pci_controller *hose = (struct pci_controller *)data; 224 struct pci_dn *pdn; 225 226 pdn = pci_add_device_node_info(hose, dn); 227 if (unlikely(!pdn)) 228 return ERR_PTR(-ENOMEM); 229 230 return NULL; 231 } 232 233 static void pnv_php_add_pdns(struct pnv_php_slot *slot) 234 { 235 struct pci_controller *hose = pci_bus_to_host(slot->bus); 236 237 pci_traverse_device_nodes(slot->dn, pnv_php_add_one_pdn, hose); 238 } 239 240 static int pnv_php_add_devtree(struct pnv_php_slot *php_slot) 241 { 242 void *fdt, *fdt1, *dt; 243 int ret; 244 245 /* We don't know the FDT blob size. We try to get it through 246 * maximal memory chunk and then copy it to another chunk that 247 * fits the real size. 248 */ 249 fdt1 = kzalloc(0x10000, GFP_KERNEL); 250 if (unlikely(!fdt1)) { 251 ret = -ENOMEM; 252 dev_warn(&php_slot->pdev->dev, "Cannot alloc FDT blob\n"); 253 goto out; 254 } 255 256 ret = pnv_pci_get_device_tree(php_slot->dn->phandle, fdt1, 0x10000); 257 if (unlikely(ret)) { 258 dev_warn(&php_slot->pdev->dev, "Error %d getting FDT blob\n", 259 ret); 260 goto free_fdt1; 261 } 262 263 fdt = kzalloc(fdt_totalsize(fdt1), GFP_KERNEL); 264 if (unlikely(!fdt)) { 265 ret = -ENOMEM; 266 dev_warn(&php_slot->pdev->dev, "Cannot %d bytes memory\n", 267 fdt_totalsize(fdt1)); 268 goto free_fdt1; 269 } 270 271 /* Unflatten device tree blob */ 272 memcpy(fdt, fdt1, fdt_totalsize(fdt1)); 273 dt = of_fdt_unflatten_tree(fdt, php_slot->dn, NULL); 274 if (unlikely(!dt)) { 275 ret = -EINVAL; 276 dev_warn(&php_slot->pdev->dev, "Cannot unflatten FDT\n"); 277 goto free_fdt; 278 } 279 280 /* Initialize and apply the changeset */ 281 of_changeset_init(&php_slot->ocs); 282 pnv_php_reverse_nodes(php_slot->dn); 283 ret = pnv_php_populate_changeset(&php_slot->ocs, php_slot->dn); 284 if (unlikely(ret)) { 285 pnv_php_reverse_nodes(php_slot->dn); 286 dev_warn(&php_slot->pdev->dev, "Error %d populating changeset\n", 287 ret); 288 goto free_dt; 289 } 290 291 php_slot->dn->child = NULL; 292 ret = of_changeset_apply(&php_slot->ocs); 293 if (unlikely(ret)) { 294 dev_warn(&php_slot->pdev->dev, "Error %d applying changeset\n", 295 ret); 296 goto destroy_changeset; 297 } 298 299 /* Add device node firmware data */ 300 pnv_php_add_pdns(php_slot); 301 php_slot->fdt = fdt; 302 php_slot->dt = dt; 303 kfree(fdt1); 304 goto out; 305 306 destroy_changeset: 307 of_changeset_destroy(&php_slot->ocs); 308 free_dt: 309 kfree(dt); 310 php_slot->dn->child = NULL; 311 free_fdt: 312 kfree(fdt); 313 free_fdt1: 314 kfree(fdt1); 315 out: 316 return ret; 317 } 318 319 static int pnv_php_set_slot_power_state(struct hotplug_slot *slot, 320 uint8_t state) 321 { 322 struct pnv_php_slot *php_slot = slot->private; 323 struct opal_msg msg; 324 int ret; 325 326 ret = pnv_pci_set_power_state(php_slot->id, state, &msg); 327 if (likely(ret > 0)) { 328 if (be64_to_cpu(msg.params[1]) != php_slot->dn->phandle || 329 be64_to_cpu(msg.params[2]) != state || 330 be64_to_cpu(msg.params[3]) != OPAL_SUCCESS) { 331 dev_warn(&php_slot->pdev->dev, "Wrong msg (%lld, %lld, %lld)\n", 332 be64_to_cpu(msg.params[1]), 333 be64_to_cpu(msg.params[2]), 334 be64_to_cpu(msg.params[3])); 335 return -ENOMSG; 336 } 337 } else if (unlikely(ret < 0)) { 338 dev_warn(&php_slot->pdev->dev, "Error %d powering %s\n", 339 ret, (state == OPAL_PCI_SLOT_POWER_ON) ? "on" : "off"); 340 return ret; 341 } 342 343 if (state == OPAL_PCI_SLOT_POWER_OFF) 344 pnv_php_rmv_devtree(php_slot); 345 else 346 ret = pnv_php_add_devtree(php_slot); 347 348 return ret; 349 } 350 351 static int pnv_php_get_power_state(struct hotplug_slot *slot, u8 *state) 352 { 353 struct pnv_php_slot *php_slot = slot->private; 354 uint8_t power_state = OPAL_PCI_SLOT_POWER_ON; 355 int ret; 356 357 /* 358 * Retrieve power status from firmware. If we fail 359 * getting that, the power status fails back to 360 * be on. 361 */ 362 ret = pnv_pci_get_power_state(php_slot->id, &power_state); 363 if (unlikely(ret)) { 364 dev_warn(&php_slot->pdev->dev, "Error %d getting power status\n", 365 ret); 366 } else { 367 *state = power_state; 368 slot->info->power_status = power_state; 369 } 370 371 return 0; 372 } 373 374 static int pnv_php_get_adapter_state(struct hotplug_slot *slot, u8 *state) 375 { 376 struct pnv_php_slot *php_slot = slot->private; 377 uint8_t presence = OPAL_PCI_SLOT_EMPTY; 378 int ret; 379 380 /* 381 * Retrieve presence status from firmware. If we can't 382 * get that, it will fail back to be empty. 383 */ 384 ret = pnv_pci_get_presence_state(php_slot->id, &presence); 385 if (likely(ret >= 0)) { 386 *state = presence; 387 slot->info->adapter_status = presence; 388 ret = 0; 389 } else { 390 dev_warn(&php_slot->pdev->dev, "Error %d getting presence\n", 391 ret); 392 } 393 394 return ret; 395 } 396 397 static int pnv_php_set_attention_state(struct hotplug_slot *slot, u8 state) 398 { 399 /* FIXME: Make it real once firmware supports it */ 400 slot->info->attention_status = state; 401 402 return 0; 403 } 404 405 static int pnv_php_enable(struct pnv_php_slot *php_slot, bool rescan) 406 { 407 struct hotplug_slot *slot = &php_slot->slot; 408 uint8_t presence = OPAL_PCI_SLOT_EMPTY; 409 uint8_t power_status = OPAL_PCI_SLOT_POWER_ON; 410 int ret; 411 412 /* Check if the slot has been configured */ 413 if (php_slot->state != PNV_PHP_STATE_REGISTERED) 414 return 0; 415 416 /* Retrieve slot presence status */ 417 ret = pnv_php_get_adapter_state(slot, &presence); 418 if (unlikely(ret)) 419 return ret; 420 421 /* Proceed if there have nothing behind the slot */ 422 if (presence == OPAL_PCI_SLOT_EMPTY) 423 goto scan; 424 425 /* 426 * If the power supply to the slot is off, we can't detect 427 * adapter presence state. That means we have to turn the 428 * slot on before going to probe slot's presence state. 429 * 430 * On the first time, we don't change the power status to 431 * boost system boot with assumption that the firmware 432 * supplies consistent slot power status: empty slot always 433 * has its power off and non-empty slot has its power on. 434 */ 435 if (!php_slot->power_state_check) { 436 php_slot->power_state_check = true; 437 438 ret = pnv_php_get_power_state(slot, &power_status); 439 if (unlikely(ret)) 440 return ret; 441 442 if (power_status != OPAL_PCI_SLOT_POWER_ON) 443 return 0; 444 } 445 446 /* Check the power status. Scan the slot if it is already on */ 447 ret = pnv_php_get_power_state(slot, &power_status); 448 if (unlikely(ret)) 449 return ret; 450 451 if (power_status == OPAL_PCI_SLOT_POWER_ON) 452 goto scan; 453 454 /* Power is off, turn it on and then scan the slot */ 455 ret = pnv_php_set_slot_power_state(slot, OPAL_PCI_SLOT_POWER_ON); 456 if (unlikely(ret)) 457 return ret; 458 459 scan: 460 if (presence == OPAL_PCI_SLOT_PRESENT) { 461 if (rescan) { 462 pci_lock_rescan_remove(); 463 pci_hp_add_devices(php_slot->bus); 464 pci_unlock_rescan_remove(); 465 } 466 467 /* Rescan for child hotpluggable slots */ 468 php_slot->state = PNV_PHP_STATE_POPULATED; 469 if (rescan) 470 pnv_php_register(php_slot->dn); 471 } else { 472 php_slot->state = PNV_PHP_STATE_POPULATED; 473 } 474 475 return 0; 476 } 477 478 static int pnv_php_enable_slot(struct hotplug_slot *slot) 479 { 480 struct pnv_php_slot *php_slot = container_of(slot, 481 struct pnv_php_slot, slot); 482 483 return pnv_php_enable(php_slot, true); 484 } 485 486 static int pnv_php_disable_slot(struct hotplug_slot *slot) 487 { 488 struct pnv_php_slot *php_slot = slot->private; 489 int ret; 490 491 if (php_slot->state != PNV_PHP_STATE_POPULATED) 492 return 0; 493 494 /* Remove all devices behind the slot */ 495 pci_lock_rescan_remove(); 496 pci_hp_remove_devices(php_slot->bus); 497 pci_unlock_rescan_remove(); 498 499 /* Detach the child hotpluggable slots */ 500 pnv_php_unregister(php_slot->dn); 501 502 /* Notify firmware and remove device nodes */ 503 ret = pnv_php_set_slot_power_state(slot, OPAL_PCI_SLOT_POWER_OFF); 504 505 php_slot->state = PNV_PHP_STATE_REGISTERED; 506 return ret; 507 } 508 509 static struct hotplug_slot_ops php_slot_ops = { 510 .get_power_status = pnv_php_get_power_state, 511 .get_adapter_status = pnv_php_get_adapter_state, 512 .set_attention_status = pnv_php_set_attention_state, 513 .enable_slot = pnv_php_enable_slot, 514 .disable_slot = pnv_php_disable_slot, 515 }; 516 517 static void pnv_php_release(struct hotplug_slot *slot) 518 { 519 struct pnv_php_slot *php_slot = slot->private; 520 unsigned long flags; 521 522 /* Remove from global or child list */ 523 spin_lock_irqsave(&pnv_php_lock, flags); 524 list_del(&php_slot->link); 525 spin_unlock_irqrestore(&pnv_php_lock, flags); 526 527 /* Detach from parent */ 528 pnv_php_put_slot(php_slot); 529 pnv_php_put_slot(php_slot->parent); 530 } 531 532 static struct pnv_php_slot *pnv_php_alloc_slot(struct device_node *dn) 533 { 534 struct pnv_php_slot *php_slot; 535 struct pci_bus *bus; 536 const char *label; 537 uint64_t id; 538 539 label = of_get_property(dn, "ibm,slot-label", NULL); 540 if (unlikely(!label)) 541 return NULL; 542 543 if (pnv_pci_get_slot_id(dn, &id)) 544 return NULL; 545 546 bus = pci_find_bus_by_node(dn); 547 if (unlikely(!bus)) 548 return NULL; 549 550 php_slot = kzalloc(sizeof(*php_slot), GFP_KERNEL); 551 if (unlikely(!php_slot)) 552 return NULL; 553 554 php_slot->name = kstrdup(label, GFP_KERNEL); 555 if (unlikely(!php_slot->name)) { 556 kfree(php_slot); 557 return NULL; 558 } 559 560 if (likely(dn->child && PCI_DN(dn->child))) 561 php_slot->slot_no = PCI_SLOT(PCI_DN(dn->child)->devfn); 562 else 563 php_slot->slot_no = -1; /* Placeholder slot */ 564 565 kref_init(&php_slot->kref); 566 php_slot->state = PNV_PHP_STATE_INITIALIZED; 567 php_slot->dn = dn; 568 php_slot->pdev = bus->self; 569 php_slot->bus = bus; 570 php_slot->id = id; 571 php_slot->power_state_check = false; 572 php_slot->slot.ops = &php_slot_ops; 573 php_slot->slot.info = &php_slot->slot_info; 574 php_slot->slot.release = pnv_php_release; 575 php_slot->slot.private = php_slot; 576 577 INIT_LIST_HEAD(&php_slot->children); 578 INIT_LIST_HEAD(&php_slot->link); 579 580 return php_slot; 581 } 582 583 static int pnv_php_register_slot(struct pnv_php_slot *php_slot) 584 { 585 struct pnv_php_slot *parent; 586 struct device_node *dn = php_slot->dn; 587 unsigned long flags; 588 int ret; 589 590 /* Check if the slot is registered or not */ 591 parent = pnv_php_find_slot(php_slot->dn); 592 if (unlikely(parent)) { 593 pnv_php_put_slot(parent); 594 return -EEXIST; 595 } 596 597 /* Register PCI slot */ 598 ret = pci_hp_register(&php_slot->slot, php_slot->bus, 599 php_slot->slot_no, php_slot->name); 600 if (unlikely(ret)) { 601 dev_warn(&php_slot->pdev->dev, "Error %d registering slot\n", 602 ret); 603 return ret; 604 } 605 606 /* Attach to the parent's child list or global list */ 607 while ((dn = of_get_parent(dn))) { 608 if (!PCI_DN(dn)) { 609 of_node_put(dn); 610 break; 611 } 612 613 parent = pnv_php_find_slot(dn); 614 if (parent) { 615 of_node_put(dn); 616 break; 617 } 618 619 of_node_put(dn); 620 } 621 622 spin_lock_irqsave(&pnv_php_lock, flags); 623 php_slot->parent = parent; 624 if (parent) 625 list_add_tail(&php_slot->link, &parent->children); 626 else 627 list_add_tail(&php_slot->link, &pnv_php_slot_list); 628 spin_unlock_irqrestore(&pnv_php_lock, flags); 629 630 php_slot->state = PNV_PHP_STATE_REGISTERED; 631 return 0; 632 } 633 634 static int pnv_php_register_one(struct device_node *dn) 635 { 636 struct pnv_php_slot *php_slot; 637 const __be32 *prop32; 638 int ret; 639 640 /* Check if it's hotpluggable slot */ 641 prop32 = of_get_property(dn, "ibm,slot-pluggable", NULL); 642 if (!prop32 || !of_read_number(prop32, 1)) 643 return -ENXIO; 644 645 prop32 = of_get_property(dn, "ibm,reset-by-firmware", NULL); 646 if (!prop32 || !of_read_number(prop32, 1)) 647 return -ENXIO; 648 649 php_slot = pnv_php_alloc_slot(dn); 650 if (unlikely(!php_slot)) 651 return -ENODEV; 652 653 ret = pnv_php_register_slot(php_slot); 654 if (unlikely(ret)) 655 goto free_slot; 656 657 ret = pnv_php_enable(php_slot, false); 658 if (unlikely(ret)) 659 goto unregister_slot; 660 661 return 0; 662 663 unregister_slot: 664 pnv_php_unregister_one(php_slot->dn); 665 free_slot: 666 pnv_php_put_slot(php_slot); 667 return ret; 668 } 669 670 static void pnv_php_register(struct device_node *dn) 671 { 672 struct device_node *child; 673 674 /* 675 * The parent slots should be registered before their 676 * child slots. 677 */ 678 for_each_child_of_node(dn, child) { 679 pnv_php_register_one(child); 680 pnv_php_register(child); 681 } 682 } 683 684 static void pnv_php_unregister_one(struct device_node *dn) 685 { 686 struct pnv_php_slot *php_slot; 687 688 php_slot = pnv_php_find_slot(dn); 689 if (!php_slot) 690 return; 691 692 php_slot->state = PNV_PHP_STATE_OFFLINE; 693 pnv_php_put_slot(php_slot); 694 pci_hp_deregister(&php_slot->slot); 695 } 696 697 static void pnv_php_unregister(struct device_node *dn) 698 { 699 struct device_node *child; 700 701 /* The child slots should go before their parent slots */ 702 for_each_child_of_node(dn, child) { 703 pnv_php_unregister(child); 704 pnv_php_unregister_one(child); 705 } 706 } 707 708 static int __init pnv_php_init(void) 709 { 710 struct device_node *dn; 711 712 pr_info(DRIVER_DESC " version: " DRIVER_VERSION "\n"); 713 for_each_compatible_node(dn, NULL, "ibm,ioda2-phb") 714 pnv_php_register(dn); 715 716 return 0; 717 } 718 719 static void __exit pnv_php_exit(void) 720 { 721 struct device_node *dn; 722 723 for_each_compatible_node(dn, NULL, "ibm,ioda2-phb") 724 pnv_php_unregister(dn); 725 } 726 727 module_init(pnv_php_init); 728 module_exit(pnv_php_exit); 729 730 MODULE_VERSION(DRIVER_VERSION); 731 MODULE_LICENSE("GPL v2"); 732 MODULE_AUTHOR(DRIVER_AUTHOR); 733 MODULE_DESCRIPTION(DRIVER_DESC); 734