rpaphp_slot.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * RPA Virtual I/O device functions
  4. * Copyright (C) 2004 Linda Xie <[email protected]>
  5. *
  6. * All rights reserved.
  7. *
  8. * Send feedback to <[email protected]>
  9. *
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/sysfs.h>
  14. #include <linux/of.h>
  15. #include <linux/pci.h>
  16. #include <linux/string.h>
  17. #include <linux/slab.h>
  18. #include <asm/rtas.h>
  19. #include "rpaphp.h"
  20. /* free up the memory used by a slot */
  21. void dealloc_slot_struct(struct slot *slot)
  22. {
  23. of_node_put(slot->dn);
  24. kfree(slot->name);
  25. kfree(slot);
  26. }
  27. struct slot *alloc_slot_struct(struct device_node *dn,
  28. int drc_index, char *drc_name, int power_domain)
  29. {
  30. struct slot *slot;
  31. slot = kzalloc(sizeof(struct slot), GFP_KERNEL);
  32. if (!slot)
  33. goto error_nomem;
  34. slot->name = kstrdup(drc_name, GFP_KERNEL);
  35. if (!slot->name)
  36. goto error_slot;
  37. slot->dn = of_node_get(dn);
  38. slot->index = drc_index;
  39. slot->power_domain = power_domain;
  40. slot->hotplug_slot.ops = &rpaphp_hotplug_slot_ops;
  41. return (slot);
  42. error_slot:
  43. kfree(slot);
  44. error_nomem:
  45. return NULL;
  46. }
  47. static int is_registered(struct slot *slot)
  48. {
  49. struct slot *tmp_slot;
  50. list_for_each_entry(tmp_slot, &rpaphp_slot_head, rpaphp_slot_list) {
  51. if (!strcmp(tmp_slot->name, slot->name))
  52. return 1;
  53. }
  54. return 0;
  55. }
  56. int rpaphp_deregister_slot(struct slot *slot)
  57. {
  58. int retval = 0;
  59. struct hotplug_slot *php_slot = &slot->hotplug_slot;
  60. dbg("%s - Entry: deregistering slot=%s\n",
  61. __func__, slot->name);
  62. list_del(&slot->rpaphp_slot_list);
  63. pci_hp_deregister(php_slot);
  64. dealloc_slot_struct(slot);
  65. dbg("%s - Exit: rc[%d]\n", __func__, retval);
  66. return retval;
  67. }
  68. EXPORT_SYMBOL_GPL(rpaphp_deregister_slot);
  69. int rpaphp_register_slot(struct slot *slot)
  70. {
  71. struct hotplug_slot *php_slot = &slot->hotplug_slot;
  72. struct device_node *child;
  73. u32 my_index;
  74. int retval;
  75. int slotno = -1;
  76. dbg("%s registering slot:path[%pOF] index[%x], name[%s] pdomain[%x] type[%d]\n",
  77. __func__, slot->dn, slot->index, slot->name,
  78. slot->power_domain, slot->type);
  79. /* should not try to register the same slot twice */
  80. if (is_registered(slot)) {
  81. err("rpaphp_register_slot: slot[%s] is already registered\n", slot->name);
  82. return -EAGAIN;
  83. }
  84. for_each_child_of_node(slot->dn, child) {
  85. retval = of_property_read_u32(child, "ibm,my-drc-index", &my_index);
  86. if (my_index == slot->index) {
  87. slotno = PCI_SLOT(PCI_DN(child)->devfn);
  88. of_node_put(child);
  89. break;
  90. }
  91. }
  92. retval = pci_hp_register(php_slot, slot->bus, slotno, slot->name);
  93. if (retval) {
  94. err("pci_hp_register failed with error %d\n", retval);
  95. return retval;
  96. }
  97. /* add slot to our internal list */
  98. list_add(&slot->rpaphp_slot_list, &rpaphp_slot_head);
  99. info("Slot [%s] registered\n", slot->name);
  100. return 0;
  101. }