s390_pci_hpc.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 int slot_configure(struct slot *slot)
  34. {
  35. int ret = sclp_pci_configure(slot->zdev->fid);
  36. zpci_dbg(3, "conf fid:%x, rc:%d\n", slot->zdev->fid, ret);
  37. if (!ret)
  38. slot->zdev->state = ZPCI_FN_STATE_CONFIGURED;
  39. return ret;
  40. }
  41. static inline int slot_deconfigure(struct slot *slot)
  42. {
  43. int ret = sclp_pci_deconfigure(slot->zdev->fid);
  44. zpci_dbg(3, "deconf fid:%x, rc:%d\n", slot->zdev->fid, ret);
  45. if (!ret)
  46. slot->zdev->state = ZPCI_FN_STATE_STANDBY;
  47. return ret;
  48. }
  49. static int enable_slot(struct hotplug_slot *hotplug_slot)
  50. {
  51. struct slot *slot = hotplug_slot->private;
  52. int rc;
  53. if (slot->zdev->state != ZPCI_FN_STATE_STANDBY)
  54. return -EIO;
  55. rc = slot_configure(slot);
  56. if (rc)
  57. return rc;
  58. rc = zpci_enable_device(slot->zdev);
  59. if (rc)
  60. goto out_deconfigure;
  61. pci_scan_slot(slot->zdev->bus, ZPCI_DEVFN);
  62. pci_lock_rescan_remove();
  63. pci_bus_add_devices(slot->zdev->bus);
  64. pci_unlock_rescan_remove();
  65. return rc;
  66. out_deconfigure:
  67. slot_deconfigure(slot);
  68. return rc;
  69. }
  70. static int disable_slot(struct hotplug_slot *hotplug_slot)
  71. {
  72. struct slot *slot = hotplug_slot->private;
  73. struct pci_dev *pdev;
  74. int rc;
  75. if (!zpci_fn_configured(slot->zdev->state))
  76. return -EIO;
  77. pdev = pci_get_slot(slot->zdev->bus, ZPCI_DEVFN);
  78. if (pdev) {
  79. pci_stop_and_remove_bus_device_locked(pdev);
  80. pci_dev_put(pdev);
  81. }
  82. rc = zpci_disable_device(slot->zdev);
  83. if (rc)
  84. return rc;
  85. return slot_deconfigure(slot);
  86. }
  87. static int get_power_status(struct hotplug_slot *hotplug_slot, u8 *value)
  88. {
  89. struct slot *slot = hotplug_slot->private;
  90. switch (slot->zdev->state) {
  91. case ZPCI_FN_STATE_STANDBY:
  92. *value = 0;
  93. break;
  94. default:
  95. *value = 1;
  96. break;
  97. }
  98. return 0;
  99. }
  100. static int get_adapter_status(struct hotplug_slot *hotplug_slot, u8 *value)
  101. {
  102. /* if the slot exits it always contains a function */
  103. *value = 1;
  104. return 0;
  105. }
  106. static void release_slot(struct hotplug_slot *hotplug_slot)
  107. {
  108. struct slot *slot = hotplug_slot->private;
  109. kfree(slot->hotplug_slot->info);
  110. kfree(slot->hotplug_slot);
  111. kfree(slot);
  112. }
  113. static struct hotplug_slot_ops s390_hotplug_slot_ops = {
  114. .enable_slot = enable_slot,
  115. .disable_slot = disable_slot,
  116. .get_power_status = get_power_status,
  117. .get_adapter_status = get_adapter_status,
  118. };
  119. int zpci_init_slot(struct zpci_dev *zdev)
  120. {
  121. struct hotplug_slot *hotplug_slot;
  122. struct hotplug_slot_info *info;
  123. char name[SLOT_NAME_SIZE];
  124. struct slot *slot;
  125. int rc;
  126. if (!zdev)
  127. return 0;
  128. slot = kzalloc(sizeof(*slot), GFP_KERNEL);
  129. if (!slot)
  130. goto error;
  131. hotplug_slot = kzalloc(sizeof(*hotplug_slot), GFP_KERNEL);
  132. if (!hotplug_slot)
  133. goto error_hp;
  134. hotplug_slot->private = slot;
  135. slot->hotplug_slot = hotplug_slot;
  136. slot->zdev = zdev;
  137. info = kzalloc(sizeof(*info), GFP_KERNEL);
  138. if (!info)
  139. goto error_info;
  140. hotplug_slot->info = info;
  141. hotplug_slot->ops = &s390_hotplug_slot_ops;
  142. hotplug_slot->release = &release_slot;
  143. get_power_status(hotplug_slot, &info->power_status);
  144. get_adapter_status(hotplug_slot, &info->adapter_status);
  145. snprintf(name, SLOT_NAME_SIZE, "%08x", zdev->fid);
  146. rc = pci_hp_register(slot->hotplug_slot, zdev->bus,
  147. ZPCI_DEVFN, name);
  148. if (rc)
  149. goto error_reg;
  150. list_add(&slot->slot_list, &s390_hotplug_slot_list);
  151. return 0;
  152. error_reg:
  153. kfree(info);
  154. error_info:
  155. kfree(hotplug_slot);
  156. error_hp:
  157. kfree(slot);
  158. error:
  159. return -ENOMEM;
  160. }
  161. void zpci_exit_slot(struct zpci_dev *zdev)
  162. {
  163. struct slot *slot, *next;
  164. list_for_each_entry_safe(slot, next, &s390_hotplug_slot_list,
  165. slot_list) {
  166. if (slot->zdev != zdev)
  167. continue;
  168. list_del(&slot->slot_list);
  169. pci_hp_deregister(slot->hotplug_slot);
  170. }
  171. }