rpaphp_slot.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * RPA Virtual I/O device functions
  3. * Copyright (C) 2004 Linda Xie <lxie@us.ibm.com>
  4. *
  5. * All rights reserved.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  15. * NON INFRINGEMENT. See the GNU General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. *
  22. * Send feedback to <lxie@us.ibm.com>
  23. *
  24. */
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <linux/sysfs.h>
  28. #include <linux/pci.h>
  29. #include <linux/string.h>
  30. #include <linux/slab.h>
  31. #include <asm/rtas.h>
  32. #include "rpaphp.h"
  33. /* free up the memory used by a slot */
  34. static void rpaphp_release_slot(struct hotplug_slot *hotplug_slot)
  35. {
  36. struct slot *slot = (struct slot *) hotplug_slot->private;
  37. dealloc_slot_struct(slot);
  38. }
  39. void dealloc_slot_struct(struct slot *slot)
  40. {
  41. kfree(slot->hotplug_slot->info);
  42. kfree(slot->name);
  43. kfree(slot->hotplug_slot);
  44. kfree(slot);
  45. }
  46. struct slot *alloc_slot_struct(struct device_node *dn,
  47. int drc_index, char *drc_name, int power_domain)
  48. {
  49. struct slot *slot;
  50. slot = kzalloc(sizeof(struct slot), GFP_KERNEL);
  51. if (!slot)
  52. goto error_nomem;
  53. slot->hotplug_slot = kzalloc(sizeof(struct hotplug_slot), GFP_KERNEL);
  54. if (!slot->hotplug_slot)
  55. goto error_slot;
  56. slot->hotplug_slot->info = kzalloc(sizeof(struct hotplug_slot_info),
  57. GFP_KERNEL);
  58. if (!slot->hotplug_slot->info)
  59. goto error_hpslot;
  60. slot->name = kstrdup(drc_name, GFP_KERNEL);
  61. if (!slot->name)
  62. goto error_info;
  63. slot->dn = dn;
  64. slot->index = drc_index;
  65. slot->power_domain = power_domain;
  66. slot->hotplug_slot->private = slot;
  67. slot->hotplug_slot->ops = &rpaphp_hotplug_slot_ops;
  68. slot->hotplug_slot->release = &rpaphp_release_slot;
  69. return (slot);
  70. error_info:
  71. kfree(slot->hotplug_slot->info);
  72. error_hpslot:
  73. kfree(slot->hotplug_slot);
  74. error_slot:
  75. kfree(slot);
  76. error_nomem:
  77. return NULL;
  78. }
  79. static int is_registered(struct slot *slot)
  80. {
  81. struct slot *tmp_slot;
  82. list_for_each_entry(tmp_slot, &rpaphp_slot_head, rpaphp_slot_list) {
  83. if (!strcmp(tmp_slot->name, slot->name))
  84. return 1;
  85. }
  86. return 0;
  87. }
  88. int rpaphp_deregister_slot(struct slot *slot)
  89. {
  90. int retval = 0;
  91. struct hotplug_slot *php_slot = slot->hotplug_slot;
  92. dbg("%s - Entry: deregistering slot=%s\n",
  93. __func__, slot->name);
  94. list_del(&slot->rpaphp_slot_list);
  95. retval = pci_hp_deregister(php_slot);
  96. if (retval)
  97. err("Problem unregistering a slot %s\n", slot->name);
  98. dbg("%s - Exit: rc[%d]\n", __func__, retval);
  99. return retval;
  100. }
  101. EXPORT_SYMBOL_GPL(rpaphp_deregister_slot);
  102. int rpaphp_register_slot(struct slot *slot)
  103. {
  104. struct hotplug_slot *php_slot = slot->hotplug_slot;
  105. struct device_node *child;
  106. u32 my_index;
  107. int retval;
  108. int slotno = -1;
  109. dbg("%s registering slot:path[%s] index[%x], name[%s] pdomain[%x] type[%d]\n",
  110. __func__, slot->dn->full_name, slot->index, slot->name,
  111. slot->power_domain, slot->type);
  112. /* should not try to register the same slot twice */
  113. if (is_registered(slot)) {
  114. err("rpaphp_register_slot: slot[%s] is already registered\n", slot->name);
  115. return -EAGAIN;
  116. }
  117. for_each_child_of_node(slot->dn, child) {
  118. retval = of_property_read_u32(child, "ibm,my-drc-index", &my_index);
  119. if (my_index == slot->index) {
  120. slotno = PCI_SLOT(PCI_DN(child)->devfn);
  121. of_node_put(child);
  122. break;
  123. }
  124. }
  125. retval = pci_hp_register(php_slot, slot->bus, slotno, slot->name);
  126. if (retval) {
  127. err("pci_hp_register failed with error %d\n", retval);
  128. return retval;
  129. }
  130. /* add slot to our internal list */
  131. list_add(&slot->rpaphp_slot_list, &rpaphp_slot_head);
  132. info("Slot [%s] registered\n", slot->name);
  133. return 0;
  134. }