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_event { 26 bool added; 27 struct pnv_php_slot *php_slot; 28 struct work_struct work; 29 }; 30 31 static LIST_HEAD(pnv_php_slot_list); 32 static DEFINE_SPINLOCK(pnv_php_lock); 33 34 static void pnv_php_register(struct device_node *dn); 35 static void pnv_php_unregister_one(struct device_node *dn); 36 static void pnv_php_unregister(struct device_node *dn); 37 38 static void pnv_php_disable_irq(struct pnv_php_slot *php_slot) 39 { 40 struct pci_dev *pdev = php_slot->pdev; 41 u16 ctrl; 42 43 if (php_slot->irq > 0) { 44 pcie_capability_read_word(pdev, PCI_EXP_SLTCTL, &ctrl); 45 ctrl &= ~(PCI_EXP_SLTCTL_HPIE | 46 PCI_EXP_SLTCTL_PDCE | 47 PCI_EXP_SLTCTL_DLLSCE); 48 pcie_capability_write_word(pdev, PCI_EXP_SLTCTL, ctrl); 49 50 free_irq(php_slot->irq, php_slot); 51 php_slot->irq = 0; 52 } 53 54 if (php_slot->wq) { 55 destroy_workqueue(php_slot->wq); 56 php_slot->wq = NULL; 57 } 58 59 if (pdev->msix_enabled) 60 pci_disable_msix(pdev); 61 else if (pdev->msi_enabled) 62 pci_disable_msi(pdev); 63 } 64 65 static void pnv_php_free_slot(struct kref *kref) 66 { 67 struct pnv_php_slot *php_slot = container_of(kref, 68 struct pnv_php_slot, kref); 69 70 WARN_ON(!list_empty(&php_slot->children)); 71 pnv_php_disable_irq(php_slot); 72 kfree(php_slot->name); 73 kfree(php_slot); 74 } 75 76 static inline void pnv_php_put_slot(struct pnv_php_slot *php_slot) 77 { 78 79 if (WARN_ON(!php_slot)) 80 return; 81 82 kref_put(&php_slot->kref, pnv_php_free_slot); 83 } 84 85 static struct pnv_php_slot *pnv_php_match(struct device_node *dn, 86 struct pnv_php_slot *php_slot) 87 { 88 struct pnv_php_slot *target, *tmp; 89 90 if (php_slot->dn == dn) { 91 kref_get(&php_slot->kref); 92 return php_slot; 93 } 94 95 list_for_each_entry(tmp, &php_slot->children, link) { 96 target = pnv_php_match(dn, tmp); 97 if (target) 98 return target; 99 } 100 101 return NULL; 102 } 103 104 struct pnv_php_slot *pnv_php_find_slot(struct device_node *dn) 105 { 106 struct pnv_php_slot *php_slot, *tmp; 107 unsigned long flags; 108 109 spin_lock_irqsave(&pnv_php_lock, flags); 110 list_for_each_entry(tmp, &pnv_php_slot_list, link) { 111 php_slot = pnv_php_match(dn, tmp); 112 if (php_slot) { 113 spin_unlock_irqrestore(&pnv_php_lock, flags); 114 return php_slot; 115 } 116 } 117 spin_unlock_irqrestore(&pnv_php_lock, flags); 118 119 return NULL; 120 } 121 EXPORT_SYMBOL_GPL(pnv_php_find_slot); 122 123 /* 124 * Remove pdn for all children of the indicated device node. 125 * The function should remove pdn in a depth-first manner. 126 */ 127 static void pnv_php_rmv_pdns(struct device_node *dn) 128 { 129 struct device_node *child; 130 131 for_each_child_of_node(dn, child) { 132 pnv_php_rmv_pdns(child); 133 134 pci_remove_device_node_info(child); 135 } 136 } 137 138 /* 139 * Detach all child nodes of the indicated device nodes. The 140 * function should handle device nodes in depth-first manner. 141 * 142 * We should not invoke of_node_release() as the memory for 143 * individual device node is part of large memory block. The 144 * large block is allocated from memblock (system bootup) or 145 * kmalloc() when unflattening the device tree by OF changeset. 146 * We can not free the large block allocated from memblock. For 147 * later case, it should be released at once. 148 */ 149 static void pnv_php_detach_device_nodes(struct device_node *parent) 150 { 151 struct device_node *dn; 152 int refcount; 153 154 for_each_child_of_node(parent, dn) { 155 pnv_php_detach_device_nodes(dn); 156 157 of_node_put(dn); 158 refcount = atomic_read(&dn->kobj.kref.refcount); 159 if (refcount != 1) 160 pr_warn("Invalid refcount %d on <%s>\n", 161 refcount, of_node_full_name(dn)); 162 163 of_detach_node(dn); 164 } 165 } 166 167 static void pnv_php_rmv_devtree(struct pnv_php_slot *php_slot) 168 { 169 pnv_php_rmv_pdns(php_slot->dn); 170 171 /* 172 * Decrease the refcount if the device nodes were created 173 * through OF changeset before detaching them. 174 */ 175 if (php_slot->fdt) 176 of_changeset_destroy(&php_slot->ocs); 177 pnv_php_detach_device_nodes(php_slot->dn); 178 179 if (php_slot->fdt) { 180 kfree(php_slot->dt); 181 kfree(php_slot->fdt); 182 php_slot->dt = NULL; 183 php_slot->dn->child = NULL; 184 php_slot->fdt = NULL; 185 } 186 } 187 188 /* 189 * As the nodes in OF changeset are applied in reverse order, we 190 * need revert the nodes in advance so that we have correct node 191 * order after the changeset is applied. 192 */ 193 static void pnv_php_reverse_nodes(struct device_node *parent) 194 { 195 struct device_node *child, *next; 196 197 /* In-depth first */ 198 for_each_child_of_node(parent, child) 199 pnv_php_reverse_nodes(child); 200 201 /* Reverse the nodes in the child list */ 202 child = parent->child; 203 parent->child = NULL; 204 while (child) { 205 next = child->sibling; 206 207 child->sibling = parent->child; 208 parent->child = child; 209 child = next; 210 } 211 } 212 213 static int pnv_php_populate_changeset(struct of_changeset *ocs, 214 struct device_node *dn) 215 { 216 struct device_node *child; 217 int ret = 0; 218 219 for_each_child_of_node(dn, child) { 220 ret = of_changeset_attach_node(ocs, child); 221 if (ret) 222 break; 223 224 ret = pnv_php_populate_changeset(ocs, child); 225 if (ret) 226 break; 227 } 228 229 return ret; 230 } 231 232 static void *pnv_php_add_one_pdn(struct device_node *dn, void *data) 233 { 234 struct pci_controller *hose = (struct pci_controller *)data; 235 struct pci_dn *pdn; 236 237 pdn = pci_add_device_node_info(hose, dn); 238 if (!pdn) 239 return ERR_PTR(-ENOMEM); 240 241 return NULL; 242 } 243 244 static void pnv_php_add_pdns(struct pnv_php_slot *slot) 245 { 246 struct pci_controller *hose = pci_bus_to_host(slot->bus); 247 248 pci_traverse_device_nodes(slot->dn, pnv_php_add_one_pdn, hose); 249 } 250 251 static int pnv_php_add_devtree(struct pnv_php_slot *php_slot) 252 { 253 void *fdt, *fdt1, *dt; 254 int ret; 255 256 /* We don't know the FDT blob size. We try to get it through 257 * maximal memory chunk and then copy it to another chunk that 258 * fits the real size. 259 */ 260 fdt1 = kzalloc(0x10000, GFP_KERNEL); 261 if (!fdt1) { 262 ret = -ENOMEM; 263 dev_warn(&php_slot->pdev->dev, "Cannot alloc FDT blob\n"); 264 goto out; 265 } 266 267 ret = pnv_pci_get_device_tree(php_slot->dn->phandle, fdt1, 0x10000); 268 if (ret) { 269 dev_warn(&php_slot->pdev->dev, "Error %d getting FDT blob\n", 270 ret); 271 goto free_fdt1; 272 } 273 274 fdt = kzalloc(fdt_totalsize(fdt1), GFP_KERNEL); 275 if (!fdt) { 276 ret = -ENOMEM; 277 dev_warn(&php_slot->pdev->dev, "Cannot %d bytes memory\n", 278 fdt_totalsize(fdt1)); 279 goto free_fdt1; 280 } 281 282 /* Unflatten device tree blob */ 283 memcpy(fdt, fdt1, fdt_totalsize(fdt1)); 284 dt = of_fdt_unflatten_tree(fdt, php_slot->dn, NULL); 285 if (!dt) { 286 ret = -EINVAL; 287 dev_warn(&php_slot->pdev->dev, "Cannot unflatten FDT\n"); 288 goto free_fdt; 289 } 290 291 /* Initialize and apply the changeset */ 292 of_changeset_init(&php_slot->ocs); 293 pnv_php_reverse_nodes(php_slot->dn); 294 ret = pnv_php_populate_changeset(&php_slot->ocs, php_slot->dn); 295 if (ret) { 296 pnv_php_reverse_nodes(php_slot->dn); 297 dev_warn(&php_slot->pdev->dev, "Error %d populating changeset\n", 298 ret); 299 goto free_dt; 300 } 301 302 php_slot->dn->child = NULL; 303 ret = of_changeset_apply(&php_slot->ocs); 304 if (ret) { 305 dev_warn(&php_slot->pdev->dev, "Error %d applying changeset\n", 306 ret); 307 goto destroy_changeset; 308 } 309 310 /* Add device node firmware data */ 311 pnv_php_add_pdns(php_slot); 312 php_slot->fdt = fdt; 313 php_slot->dt = dt; 314 kfree(fdt1); 315 goto out; 316 317 destroy_changeset: 318 of_changeset_destroy(&php_slot->ocs); 319 free_dt: 320 kfree(dt); 321 php_slot->dn->child = NULL; 322 free_fdt: 323 kfree(fdt); 324 free_fdt1: 325 kfree(fdt1); 326 out: 327 return ret; 328 } 329 330 int pnv_php_set_slot_power_state(struct hotplug_slot *slot, 331 uint8_t state) 332 { 333 struct pnv_php_slot *php_slot = slot->private; 334 struct opal_msg msg; 335 int ret; 336 337 ret = pnv_pci_set_power_state(php_slot->id, state, &msg); 338 if (ret > 0) { 339 if (be64_to_cpu(msg.params[1]) != php_slot->dn->phandle || 340 be64_to_cpu(msg.params[2]) != state || 341 be64_to_cpu(msg.params[3]) != OPAL_SUCCESS) { 342 dev_warn(&php_slot->pdev->dev, "Wrong msg (%lld, %lld, %lld)\n", 343 be64_to_cpu(msg.params[1]), 344 be64_to_cpu(msg.params[2]), 345 be64_to_cpu(msg.params[3])); 346 return -ENOMSG; 347 } 348 } else if (ret < 0) { 349 dev_warn(&php_slot->pdev->dev, "Error %d powering %s\n", 350 ret, (state == OPAL_PCI_SLOT_POWER_ON) ? "on" : "off"); 351 return ret; 352 } 353 354 if (state == OPAL_PCI_SLOT_POWER_OFF || state == OPAL_PCI_SLOT_OFFLINE) 355 pnv_php_rmv_devtree(php_slot); 356 else 357 ret = pnv_php_add_devtree(php_slot); 358 359 return ret; 360 } 361 EXPORT_SYMBOL_GPL(pnv_php_set_slot_power_state); 362 363 static int pnv_php_get_power_state(struct hotplug_slot *slot, u8 *state) 364 { 365 struct pnv_php_slot *php_slot = slot->private; 366 uint8_t power_state = OPAL_PCI_SLOT_POWER_ON; 367 int ret; 368 369 /* 370 * Retrieve power status from firmware. If we fail 371 * getting that, the power status fails back to 372 * be on. 373 */ 374 ret = pnv_pci_get_power_state(php_slot->id, &power_state); 375 if (ret) { 376 dev_warn(&php_slot->pdev->dev, "Error %d getting power status\n", 377 ret); 378 } else { 379 *state = power_state; 380 slot->info->power_status = power_state; 381 } 382 383 return 0; 384 } 385 386 static int pnv_php_get_adapter_state(struct hotplug_slot *slot, u8 *state) 387 { 388 struct pnv_php_slot *php_slot = slot->private; 389 uint8_t presence = OPAL_PCI_SLOT_EMPTY; 390 int ret; 391 392 /* 393 * Retrieve presence status from firmware. If we can't 394 * get that, it will fail back to be empty. 395 */ 396 ret = pnv_pci_get_presence_state(php_slot->id, &presence); 397 if (ret >= 0) { 398 *state = presence; 399 slot->info->adapter_status = presence; 400 ret = 0; 401 } else { 402 dev_warn(&php_slot->pdev->dev, "Error %d getting presence\n", 403 ret); 404 } 405 406 return ret; 407 } 408 409 static int pnv_php_set_attention_state(struct hotplug_slot *slot, u8 state) 410 { 411 /* FIXME: Make it real once firmware supports it */ 412 slot->info->attention_status = state; 413 414 return 0; 415 } 416 417 static int pnv_php_enable(struct pnv_php_slot *php_slot, bool rescan) 418 { 419 struct hotplug_slot *slot = &php_slot->slot; 420 uint8_t presence = OPAL_PCI_SLOT_EMPTY; 421 uint8_t power_status = OPAL_PCI_SLOT_POWER_ON; 422 int ret; 423 424 /* Check if the slot has been configured */ 425 if (php_slot->state != PNV_PHP_STATE_REGISTERED) 426 return 0; 427 428 /* Retrieve slot presence status */ 429 ret = pnv_php_get_adapter_state(slot, &presence); 430 if (ret) 431 return ret; 432 433 /* Proceed if there have nothing behind the slot */ 434 if (presence == OPAL_PCI_SLOT_EMPTY) 435 goto scan; 436 437 /* 438 * If the power supply to the slot is off, we can't detect 439 * adapter presence state. That means we have to turn the 440 * slot on before going to probe slot's presence state. 441 * 442 * On the first time, we don't change the power status to 443 * boost system boot with assumption that the firmware 444 * supplies consistent slot power status: empty slot always 445 * has its power off and non-empty slot has its power on. 446 */ 447 if (!php_slot->power_state_check) { 448 php_slot->power_state_check = true; 449 450 ret = pnv_php_get_power_state(slot, &power_status); 451 if (ret) 452 return ret; 453 454 if (power_status != OPAL_PCI_SLOT_POWER_ON) 455 return 0; 456 } 457 458 /* Check the power status. Scan the slot if it is already on */ 459 ret = pnv_php_get_power_state(slot, &power_status); 460 if (ret) 461 return ret; 462 463 if (power_status == OPAL_PCI_SLOT_POWER_ON) 464 goto scan; 465 466 /* Power is off, turn it on and then scan the slot */ 467 ret = pnv_php_set_slot_power_state(slot, OPAL_PCI_SLOT_POWER_ON); 468 if (ret) 469 return ret; 470 471 scan: 472 if (presence == OPAL_PCI_SLOT_PRESENT) { 473 if (rescan) { 474 pci_lock_rescan_remove(); 475 pci_hp_add_devices(php_slot->bus); 476 pci_unlock_rescan_remove(); 477 } 478 479 /* Rescan for child hotpluggable slots */ 480 php_slot->state = PNV_PHP_STATE_POPULATED; 481 if (rescan) 482 pnv_php_register(php_slot->dn); 483 } else { 484 php_slot->state = PNV_PHP_STATE_POPULATED; 485 } 486 487 return 0; 488 } 489 490 static int pnv_php_enable_slot(struct hotplug_slot *slot) 491 { 492 struct pnv_php_slot *php_slot = container_of(slot, 493 struct pnv_php_slot, slot); 494 495 return pnv_php_enable(php_slot, true); 496 } 497 498 static int pnv_php_disable_slot(struct hotplug_slot *slot) 499 { 500 struct pnv_php_slot *php_slot = slot->private; 501 int ret; 502 503 if (php_slot->state != PNV_PHP_STATE_POPULATED) 504 return 0; 505 506 /* Remove all devices behind the slot */ 507 pci_lock_rescan_remove(); 508 pci_hp_remove_devices(php_slot->bus); 509 pci_unlock_rescan_remove(); 510 511 /* Detach the child hotpluggable slots */ 512 pnv_php_unregister(php_slot->dn); 513 514 /* Notify firmware and remove device nodes */ 515 ret = pnv_php_set_slot_power_state(slot, OPAL_PCI_SLOT_POWER_OFF); 516 517 php_slot->state = PNV_PHP_STATE_REGISTERED; 518 return ret; 519 } 520 521 static struct hotplug_slot_ops php_slot_ops = { 522 .get_power_status = pnv_php_get_power_state, 523 .get_adapter_status = pnv_php_get_adapter_state, 524 .set_attention_status = pnv_php_set_attention_state, 525 .enable_slot = pnv_php_enable_slot, 526 .disable_slot = pnv_php_disable_slot, 527 }; 528 529 static void pnv_php_release(struct hotplug_slot *slot) 530 { 531 struct pnv_php_slot *php_slot = slot->private; 532 unsigned long flags; 533 534 /* Remove from global or child list */ 535 spin_lock_irqsave(&pnv_php_lock, flags); 536 list_del(&php_slot->link); 537 spin_unlock_irqrestore(&pnv_php_lock, flags); 538 539 /* Detach from parent */ 540 pnv_php_put_slot(php_slot); 541 pnv_php_put_slot(php_slot->parent); 542 } 543 544 static struct pnv_php_slot *pnv_php_alloc_slot(struct device_node *dn) 545 { 546 struct pnv_php_slot *php_slot; 547 struct pci_bus *bus; 548 const char *label; 549 uint64_t id; 550 int ret; 551 552 ret = of_property_read_string(dn, "ibm,slot-label", &label); 553 if (ret) 554 return NULL; 555 556 if (pnv_pci_get_slot_id(dn, &id)) 557 return NULL; 558 559 bus = pci_find_bus_by_node(dn); 560 if (!bus) 561 return NULL; 562 563 php_slot = kzalloc(sizeof(*php_slot), GFP_KERNEL); 564 if (!php_slot) 565 return NULL; 566 567 php_slot->name = kstrdup(label, GFP_KERNEL); 568 if (!php_slot->name) { 569 kfree(php_slot); 570 return NULL; 571 } 572 573 if (dn->child && PCI_DN(dn->child)) 574 php_slot->slot_no = PCI_SLOT(PCI_DN(dn->child)->devfn); 575 else 576 php_slot->slot_no = -1; /* Placeholder slot */ 577 578 kref_init(&php_slot->kref); 579 php_slot->state = PNV_PHP_STATE_INITIALIZED; 580 php_slot->dn = dn; 581 php_slot->pdev = bus->self; 582 php_slot->bus = bus; 583 php_slot->id = id; 584 php_slot->power_state_check = false; 585 php_slot->slot.ops = &php_slot_ops; 586 php_slot->slot.info = &php_slot->slot_info; 587 php_slot->slot.release = pnv_php_release; 588 php_slot->slot.private = php_slot; 589 590 INIT_LIST_HEAD(&php_slot->children); 591 INIT_LIST_HEAD(&php_slot->link); 592 593 return php_slot; 594 } 595 596 static int pnv_php_register_slot(struct pnv_php_slot *php_slot) 597 { 598 struct pnv_php_slot *parent; 599 struct device_node *dn = php_slot->dn; 600 unsigned long flags; 601 int ret; 602 603 /* Check if the slot is registered or not */ 604 parent = pnv_php_find_slot(php_slot->dn); 605 if (parent) { 606 pnv_php_put_slot(parent); 607 return -EEXIST; 608 } 609 610 /* Register PCI slot */ 611 ret = pci_hp_register(&php_slot->slot, php_slot->bus, 612 php_slot->slot_no, php_slot->name); 613 if (ret) { 614 dev_warn(&php_slot->pdev->dev, "Error %d registering slot\n", 615 ret); 616 return ret; 617 } 618 619 /* Attach to the parent's child list or global list */ 620 while ((dn = of_get_parent(dn))) { 621 if (!PCI_DN(dn)) { 622 of_node_put(dn); 623 break; 624 } 625 626 parent = pnv_php_find_slot(dn); 627 if (parent) { 628 of_node_put(dn); 629 break; 630 } 631 632 of_node_put(dn); 633 } 634 635 spin_lock_irqsave(&pnv_php_lock, flags); 636 php_slot->parent = parent; 637 if (parent) 638 list_add_tail(&php_slot->link, &parent->children); 639 else 640 list_add_tail(&php_slot->link, &pnv_php_slot_list); 641 spin_unlock_irqrestore(&pnv_php_lock, flags); 642 643 php_slot->state = PNV_PHP_STATE_REGISTERED; 644 return 0; 645 } 646 647 static int pnv_php_enable_msix(struct pnv_php_slot *php_slot) 648 { 649 struct pci_dev *pdev = php_slot->pdev; 650 struct msix_entry entry; 651 int nr_entries, ret; 652 u16 pcie_flag; 653 654 /* Get total number of MSIx entries */ 655 nr_entries = pci_msix_vec_count(pdev); 656 if (nr_entries < 0) 657 return nr_entries; 658 659 /* Check hotplug MSIx entry is in range */ 660 pcie_capability_read_word(pdev, PCI_EXP_FLAGS, &pcie_flag); 661 entry.entry = (pcie_flag & PCI_EXP_FLAGS_IRQ) >> 9; 662 if (entry.entry >= nr_entries) 663 return -ERANGE; 664 665 /* Enable MSIx */ 666 ret = pci_enable_msix_exact(pdev, &entry, 1); 667 if (ret) { 668 dev_warn(&pdev->dev, "Error %d enabling MSIx\n", ret); 669 return ret; 670 } 671 672 return entry.vector; 673 } 674 675 static void pnv_php_event_handler(struct work_struct *work) 676 { 677 struct pnv_php_event *event = 678 container_of(work, struct pnv_php_event, work); 679 struct pnv_php_slot *php_slot = event->php_slot; 680 681 if (event->added) 682 pnv_php_enable_slot(&php_slot->slot); 683 else 684 pnv_php_disable_slot(&php_slot->slot); 685 686 kfree(event); 687 } 688 689 static irqreturn_t pnv_php_interrupt(int irq, void *data) 690 { 691 struct pnv_php_slot *php_slot = data; 692 struct pci_dev *pchild, *pdev = php_slot->pdev; 693 struct eeh_dev *edev; 694 struct eeh_pe *pe; 695 struct pnv_php_event *event; 696 u16 sts, lsts; 697 u8 presence; 698 bool added; 699 unsigned long flags; 700 int ret; 701 702 pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &sts); 703 sts &= (PCI_EXP_SLTSTA_PDC | PCI_EXP_SLTSTA_DLLSC); 704 pcie_capability_write_word(pdev, PCI_EXP_SLTSTA, sts); 705 if (sts & PCI_EXP_SLTSTA_DLLSC) { 706 pcie_capability_read_word(pdev, PCI_EXP_LNKSTA, &lsts); 707 added = !!(lsts & PCI_EXP_LNKSTA_DLLLA); 708 } else if (sts & PCI_EXP_SLTSTA_PDC) { 709 ret = pnv_pci_get_presence_state(php_slot->id, &presence); 710 if (!ret) 711 return IRQ_HANDLED; 712 added = !!(presence == OPAL_PCI_SLOT_PRESENT); 713 } else { 714 return IRQ_NONE; 715 } 716 717 /* Freeze the removed PE to avoid unexpected error reporting */ 718 if (!added) { 719 pchild = list_first_entry_or_null(&php_slot->bus->devices, 720 struct pci_dev, bus_list); 721 edev = pchild ? pci_dev_to_eeh_dev(pchild) : NULL; 722 pe = edev ? edev->pe : NULL; 723 if (pe) { 724 eeh_serialize_lock(&flags); 725 eeh_pe_state_mark(pe, EEH_PE_ISOLATED); 726 eeh_serialize_unlock(flags); 727 eeh_pe_set_option(pe, EEH_OPT_FREEZE_PE); 728 } 729 } 730 731 /* 732 * The PE is left in frozen state if the event is missed. It's 733 * fine as the PCI devices (PE) aren't functional any more. 734 */ 735 event = kzalloc(sizeof(*event), GFP_ATOMIC); 736 if (!event) { 737 dev_warn(&pdev->dev, "PCI slot [%s] missed hotplug event 0x%04x\n", 738 php_slot->name, sts); 739 return IRQ_HANDLED; 740 } 741 742 dev_info(&pdev->dev, "PCI slot [%s] %s (IRQ: %d)\n", 743 php_slot->name, added ? "added" : "removed", irq); 744 INIT_WORK(&event->work, pnv_php_event_handler); 745 event->added = added; 746 event->php_slot = php_slot; 747 queue_work(php_slot->wq, &event->work); 748 749 return IRQ_HANDLED; 750 } 751 752 static void pnv_php_init_irq(struct pnv_php_slot *php_slot, int irq) 753 { 754 struct pci_dev *pdev = php_slot->pdev; 755 u16 sts, ctrl; 756 int ret; 757 758 /* Allocate workqueue */ 759 php_slot->wq = alloc_workqueue("pciehp-%s", 0, 0, php_slot->name); 760 if (!php_slot->wq) { 761 dev_warn(&pdev->dev, "Cannot alloc workqueue\n"); 762 pnv_php_disable_irq(php_slot); 763 return; 764 } 765 766 /* Clear pending interrupts */ 767 pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &sts); 768 sts |= (PCI_EXP_SLTSTA_PDC | PCI_EXP_SLTSTA_DLLSC); 769 pcie_capability_write_word(pdev, PCI_EXP_SLTSTA, sts); 770 771 /* Request the interrupt */ 772 ret = request_irq(irq, pnv_php_interrupt, IRQF_SHARED, 773 php_slot->name, php_slot); 774 if (ret) { 775 pnv_php_disable_irq(php_slot); 776 dev_warn(&pdev->dev, "Error %d enabling IRQ %d\n", ret, irq); 777 return; 778 } 779 780 /* Enable the interrupts */ 781 pcie_capability_read_word(pdev, PCI_EXP_SLTCTL, &ctrl); 782 ctrl |= (PCI_EXP_SLTCTL_HPIE | 783 PCI_EXP_SLTCTL_PDCE | 784 PCI_EXP_SLTCTL_DLLSCE); 785 pcie_capability_write_word(pdev, PCI_EXP_SLTCTL, ctrl); 786 787 /* The interrupt is initialized successfully when @irq is valid */ 788 php_slot->irq = irq; 789 } 790 791 static void pnv_php_enable_irq(struct pnv_php_slot *php_slot) 792 { 793 struct pci_dev *pdev = php_slot->pdev; 794 int irq, ret; 795 796 ret = pci_enable_device(pdev); 797 if (ret) { 798 dev_warn(&pdev->dev, "Error %d enabling device\n", ret); 799 return; 800 } 801 802 pci_set_master(pdev); 803 804 /* Enable MSIx interrupt */ 805 irq = pnv_php_enable_msix(php_slot); 806 if (irq > 0) { 807 pnv_php_init_irq(php_slot, irq); 808 return; 809 } 810 811 /* 812 * Use MSI if MSIx doesn't work. Fail back to legacy INTx 813 * if MSI doesn't work either 814 */ 815 ret = pci_enable_msi(pdev); 816 if (!ret || pdev->irq) { 817 irq = pdev->irq; 818 pnv_php_init_irq(php_slot, irq); 819 } 820 } 821 822 static int pnv_php_register_one(struct device_node *dn) 823 { 824 struct pnv_php_slot *php_slot; 825 u32 prop32; 826 int ret; 827 828 /* Check if it's hotpluggable slot */ 829 ret = of_property_read_u32(dn, "ibm,slot-pluggable", &prop32); 830 if (ret || !prop32) 831 return -ENXIO; 832 833 ret = of_property_read_u32(dn, "ibm,reset-by-firmware", &prop32); 834 if (ret || !prop32) 835 return -ENXIO; 836 837 php_slot = pnv_php_alloc_slot(dn); 838 if (!php_slot) 839 return -ENODEV; 840 841 ret = pnv_php_register_slot(php_slot); 842 if (ret) 843 goto free_slot; 844 845 ret = pnv_php_enable(php_slot, false); 846 if (ret) 847 goto unregister_slot; 848 849 /* Enable interrupt if the slot supports surprise hotplug */ 850 ret = of_property_read_u32(dn, "ibm,slot-surprise-pluggable", &prop32); 851 if (!ret && prop32) 852 pnv_php_enable_irq(php_slot); 853 854 return 0; 855 856 unregister_slot: 857 pnv_php_unregister_one(php_slot->dn); 858 free_slot: 859 pnv_php_put_slot(php_slot); 860 return ret; 861 } 862 863 static void pnv_php_register(struct device_node *dn) 864 { 865 struct device_node *child; 866 867 /* 868 * The parent slots should be registered before their 869 * child slots. 870 */ 871 for_each_child_of_node(dn, child) { 872 pnv_php_register_one(child); 873 pnv_php_register(child); 874 } 875 } 876 877 static void pnv_php_unregister_one(struct device_node *dn) 878 { 879 struct pnv_php_slot *php_slot; 880 881 php_slot = pnv_php_find_slot(dn); 882 if (!php_slot) 883 return; 884 885 php_slot->state = PNV_PHP_STATE_OFFLINE; 886 pnv_php_put_slot(php_slot); 887 pci_hp_deregister(&php_slot->slot); 888 } 889 890 static void pnv_php_unregister(struct device_node *dn) 891 { 892 struct device_node *child; 893 894 /* The child slots should go before their parent slots */ 895 for_each_child_of_node(dn, child) { 896 pnv_php_unregister(child); 897 pnv_php_unregister_one(child); 898 } 899 } 900 901 static int __init pnv_php_init(void) 902 { 903 struct device_node *dn; 904 905 pr_info(DRIVER_DESC " version: " DRIVER_VERSION "\n"); 906 for_each_compatible_node(dn, NULL, "ibm,ioda2-phb") 907 pnv_php_register(dn); 908 909 return 0; 910 } 911 912 static void __exit pnv_php_exit(void) 913 { 914 struct device_node *dn; 915 916 for_each_compatible_node(dn, NULL, "ibm,ioda2-phb") 917 pnv_php_unregister(dn); 918 } 919 920 module_init(pnv_php_init); 921 module_exit(pnv_php_exit); 922 923 MODULE_VERSION(DRIVER_VERSION); 924 MODULE_LICENSE("GPL v2"); 925 MODULE_AUTHOR(DRIVER_AUTHOR); 926 MODULE_DESCRIPTION(DRIVER_DESC); 927