s390_pci_hpc.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * PCI Hot Plug Controller Driver for System z
  4. *
  5. * Copyright 2012 IBM Corp.
  6. *
  7. * Author(s):
  8. * Jan Glauber <jang@linux.vnet.ibm.com>
  9. */
  10. #define KMSG_COMPONENT "zpci"
  11. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  12. #include <linux/kernel.h>
  13. #include <linux/slab.h>
  14. #include <linux/pci.h>
  15. #include <linux/pci_hotplug.h>
  16. #include <asm/pci_debug.h>
  17. #include <asm/sclp.h>
  18. #define SLOT_NAME_SIZE 10
  19. static LIST_HEAD(s390_hotplug_slot_list);
  20. static int zpci_fn_configured(enum zpci_state state)
  21. {
  22. return state == ZPCI_FN_STATE_CONFIGURED ||
  23. state == ZPCI_FN_STATE_ONLINE;
  24. }
  25. /*
  26. * struct slot - slot information for each *physical* slot
  27. */
  28. struct slot {
  29. struct list_head slot_list;
  30. struct hotplug_slot hotplug_slot;
  31. struct zpci_dev *zdev;
  32. };
  33. static inline struct slot *to_slot(struct hotplug_slot *hotplug_slot)
  34. {
  35. return container_of(hotplug_slot, struct slot, hotplug_slot);
  36. }
  37. static inline int slot_configure(struct slot *slot)
  38. {
  39. int ret = sclp_pci_configure(slot->zdev->fid);
  40. zpci_dbg(3, "conf fid:%x, rc:%d\n", slot->zdev->fid, ret);
  41. if (!ret)
  42. slot->zdev->state = ZPCI_FN_STATE_CONFIGURED;
  43. return ret;
  44. }
  45. static inline int slot_deconfigure(struct slot *slot)
  46. {
  47. int ret = sclp_pci_deconfigure(slot->zdev->fid);
  48. zpci_dbg(3, "deconf fid:%x, rc:%d\n", slot->zdev->fid, ret);
  49. if (!ret)
  50. slot->zdev->state = ZPCI_FN_STATE_STANDBY;
  51. return ret;
  52. }
  53. static int enable_slot(struct hotplug_slot *hotplug_slot)
  54. {
  55. struct slot *slot = to_slot(hotplug_slot);
  56. int rc;
  57. if (slot->zdev->state != ZPCI_FN_STATE_STANDBY)
  58. return -EIO;
  59. rc = slot_configure(slot);
  60. if (rc)
  61. return rc;
  62. rc = zpci_enable_device(slot->zdev);
  63. if (rc)
  64. goto out_deconfigure;
  65. pci_scan_slot(slot->zdev->bus, ZPCI_DEVFN);
  66. pci_lock_rescan_remove();
  67. pci_bus_add_devices(slot->zdev->bus);
  68. pci_unlock_rescan_remove();
  69. return rc;
  70. out_deconfigure:
  71. slot_deconfigure(slot);
  72. return rc;
  73. }
  74. static int disable_slot(struct hotplug_slot *hotplug_slot)
  75. {
  76. struct slot *slot = to_slot(hotplug_slot);
  77. struct pci_dev *pdev;
  78. int rc;
  79. if (!zpci_fn_configured(slot->zdev->state))
  80. return -EIO;
  81. pdev = pci_get_slot(slot->zdev->bus, ZPCI_DEVFN);
  82. if (pdev) {
  83. pci_stop_and_remove_bus_device_locked(pdev);
  84. pci_dev_put(pdev);
  85. }
  86. rc = zpci_disable_device(slot->zdev);
  87. if (rc)
  88. return rc;
  89. return slot_deconfigure(slot);
  90. }
  91. static int get_power_status(struct hotplug_slot *hotplug_slot, u8 *value)
  92. {
  93. struct slot *slot = to_slot(hotplug_slot);
  94. switch (slot->zdev->state) {
  95. case ZPCI_FN_STATE_STANDBY:
  96. *value = 0;
  97. break;
  98. default:
  99. *value = 1;
  100. break;
  101. }
  102. return 0;
  103. }
  104. static int get_adapter_status(struct hotplug_slot *hotplug_slot, u8 *value)
  105. {
  106. /* if the slot exits it always contains a function */
  107. *value = 1;
  108. return 0;
  109. }
  110. static const struct hotplug_slot_ops s390_hotplug_slot_ops = {
  111. .enable_slot = enable_slot,
  112. .disable_slot = disable_slot,
  113. .get_power_status = get_power_status,
  114. .get_adapter_status = get_adapter_status,
  115. };
  116. int zpci_init_slot(struct zpci_dev *zdev)
  117. {
  118. char name[SLOT_NAME_SIZE];
  119. struct slot *slot;
  120. int rc;
  121. if (!zdev)
  122. return 0;
  123. slot = kzalloc(sizeof(*slot), GFP_KERNEL);
  124. if (!slot)
  125. goto error;
  126. slot->zdev = zdev;
  127. slot->hotplug_slot.ops = &s390_hotplug_slot_ops;
  128. snprintf(name, SLOT_NAME_SIZE, "%08x", zdev->fid);
  129. rc = pci_hp_register(&slot->hotplug_slot, zdev->bus,
  130. ZPCI_DEVFN, name);
  131. if (rc)
  132. goto error_reg;
  133. list_add(&slot->slot_list, &s390_hotplug_slot_list);
  134. return 0;
  135. error_reg:
  136. kfree(slot);
  137. error:
  138. return -ENOMEM;
  139. }
  140. void zpci_exit_slot(struct zpci_dev *zdev)
  141. {
  142. struct slot *slot, *next;
  143. list_for_each_entry_safe(slot, next, &s390_hotplug_slot_list,
  144. slot_list) {
  145. if (slot->zdev != zdev)
  146. continue;
  147. list_del(&slot->slot_list);
  148. pci_hp_deregister(&slot->hotplug_slot);
  149. kfree(slot);
  150. }
  151. }